// jQuery no Conflict 
jQuery.noConflict();

// initiate JCEUtilities
MediaObject.init({'flash':"10,0,22,87",'windowmedia':"5,1,52,701",'quicktime':"6,0,2,0",'realmedia':"7,0,0,0",'shockwave':"8,5,1,0"});window.addEvent('domready', function(){var jcepopup=new JCEUtilities({'popup':{'legacy':0,'resize':1,'icons':1,'overlay':1,'overlayopacity':0.8,'overlaycolor':"#000000",'fadespeed':500,'scalespeed':500,'hideobjects':1,'scrollpopup':1,'theme':"standard",'themecustom':"",'themepath':"/wo/jce/jceutilities/themes"},'tootlip':{'classname':"tooltip",'opacity':1,'speed':150,'position':"br",'offsets':"{'x': 16, 'y': 16}"},'imgpath':"/wo/jce/jceutilities/img",'pngfix':0,'wmode':0});});
window.addEvent('domready', function(){ var JTooltips = new Tips($$('.hasTip'), { maxTitleChars: 50, fixed: false}); });

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
    this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
}

function reloadWeinliste() {
	jQuery.ajax({ type: "POST", url: "/templates/jagdhof/weinkarte.php", cache: false, success: function(html){
			jQuery("#orders").html(html);
		}
	});
}

	// Options for main form 
	var orderFormOptions = {
		beforeSubmit: checkValues,
		//dataType:  'json',
		type: 'POST',
		url: '/templates/jagdhof/weinkarte_checkout.php',
		target: '#orders',
		success: orderFormResponse
	}; 
	
	function isNumeric(form_value) 
{ 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
} 
	
	function checkValues() {
		var sel = 0;
		jQuery(".weine").each(function(i){
			if(jQuery(this).val() != "") {
			sel = 1;
			return false;
			}
		});
		
		var val = 0;
		jQuery(".weine").each(function(i){
			if(jQuery(this).val() != "" && !isNumeric(jQuery(this).val())) {
				val = 1;
			}
		});
		
		 
		
		if(sel == 0) {
			return false;
		}
		if(val == 1) {
			alert("Bitte überprüfen Sie Ihre Eingaben");
			return false;
		}
		return true;
	}
	
	function orderFormResponse() {

	}
	
	
		// Options for checkout page
	var checkoutFormOptions = {
		beforeSubmit: validate,
		//dataType:  'json',
		type: 'POST',
		url: '/templates/jagdhof/weinkarte_order.php',
		target: '#orders'
	}; 
	
			// Options for checkout page
	var changeFormOptions = {
		//beforeSubmit: validate,
		//dataType:  'json',
		type: 'POST',
		url: '/templates/jagdhof/weinkarte.php',
		target: '#orders'
	}; 
	
	function validate(formData, jqForm, options) { 
    var form = jqForm[0];
	var message = 'Bitte füllen Sie folgende Felder aus: \n';
	var fields = '';
		if (!form.Name.value) { 
			fields += 'Name\n';
		}
		if (!form.Adresse.value) { 
			fields += 'Adresse\n';
		}
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  		var address = form.Email.value;
  		if(reg.test(address) == false) {
      		fields += 'E-Mail\n';
		}
	if (fields != '') {
    alert(message + fields);
	return false;
	}
}



jQuery(function() {
jQuery('#orders').center();

	jQuery(".order").click(function() {
		jQuery(document.body).append('<div id="overlay" class="overlayBG"></div>');
		jQuery('#overlay').fadeIn();
		jQuery('#orders').css("width", 0)
						.css("height", 0)
						.animate({ 
							width: "800",
							height: "697",
							opacity: 1,
							top: 0
						  }, 500, function() {
									jQuery.ajax({ type: "GET", url: "/templates/jagdhof/weinkarte.php", cache: false, success: function(html){
											jQuery("#orders").html(html);
										}
									});
							});
		return false;
	})
	
	jQuery('a.orderClose').livequery('click',function() {
		jQuery('#orders').fadeOut();
		jQuery('#overlay').fadeOut( function() {
			jQuery('#overlay').remove();
		});
		return false;
	});
	
	jQuery('a.toggleDiv').livequery('click',function() {
		var id = jQuery(this).attr("id").substring(6);
		jQuery("#toggleDiv-" + id).slideToggle();
		return false;
	});
	
	jQuery("#overview").livequery( function() {
		jQuery("#sortable1, #sortable2, #sortable3, #sortable4").sortable({
				update : function () {
				var order = jQuery(this).sortable('serialize');
				jQuery("#info").load("/templates/jagdhof/weinkarte_sort.php?"+order);

			  }
		}).disableSelection();
	});
	
	jQuery('.toggleOrderstatus').livequery('click',function() {
		var id = jQuery(this).attr("orderid");
		var s =	 jQuery(this).attr("status");
		var who = jQuery(this);
	    jQuery.ajax({ type: "POST", url: "/templates/jagdhof/weinkarte_status.php", data: "id=" + id + "&s=" + s, success: function(status){
			who.attr("status", status);
			who.find("img").attr("src", "/wo/img/page_status_" + status + ".gif")
			}
		});
		return false;
	});
	
	jQuery('#deleteOrder').livequery('click',function() {
		if(confirm("Möchten Sie diesen Eintrag wirklich löschen?")) {
			var id = jQuery("#deleteOrder").attr("orderid");
			jQuery.ajax({ type: "POST", url: "/templates/jagdhof/weinkarte_delete.php", data: "id=" + id, success: function(){
		jQuery("#listItem_"+id).fadeOut( function() {
						jQuery("#listItem_"+id).remove();						  
												  });																										
				
				}
			});
		}
		return false;
	});
	
	jQuery('#orderForm').livequery(function() {
		jQuery('#orderForm').ajaxForm(orderFormOptions);
		return false;
	});
	
	jQuery('.orderSave').livequery('click',function() {
		jQuery('#orderForm').ajaxSubmit(orderFormOptions);
		return false;
	});
	
	jQuery('#checkoutForm').livequery(function() {
		jQuery('#checkoutForm').ajaxForm(checkoutFormOptions);
		return false;
	});
	
	jQuery('.orderPlace').livequery('click',function() {
		jQuery('#checkoutForm').ajaxSubmit(checkoutFormOptions);
		return false;
	});
	
	jQuery('.orderChange').livequery('click',function() {
		jQuery('#checkoutForm').ajaxSubmit(changeFormOptions);
		return false;
	});
	
});


