var regEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
$(function(){
	preloadImages("/jscripts/images/indicator.gif","/jscripts/images/alert.gif");
	$(".frmPayPalAdd").submit(function(){
		var targetID	= $(this).attr("id").split("_")[1];
		var qty			= $("#qty_"+targetID).val();
		if(parseInt(qty)<=0){
			alert("Ooops! Put correct quantity first..");
			return false;
		}
		$("#basketContent").html('<img src="/jscripts/images/indicator.gif" style="margin: 10px;" />');
		$("#sidebar-storeCart").html('<img src="/jscripts/images/indicator.gif" style="margin: 10px;" />');
		$("#sidebar-storeContent").html('<img src="/jscripts/images/indicator.gif" style="margin: 10px;" />');
		var priceLabel	= $("#amount_"+targetID).find("option:selected").text();
		$("#isAjaxCall_"+targetID).val(1);
		$("#priceLabel_"+targetID).val(priceLabel);
		var formData = $(this).fastSerialize();
		$.post(
			ajaxURL,
			formData,
			function(txt){
				var parts = txt.split("{|}");
				
				$("#theCart")
					.css("height",superFishCartHeight+"px")
					.slideDown("slow");

				$("#basketContent").html(parts[0]);
				
				$("#sidebar-storeCart").html(parts[1]);
				if(!$("#sidebar-storeCartHolder").is(":visible")){
					$("#sidebar-storeCartHolder").slideDown("slow");
				}
				
				$("#sidebar-storeContent").html(parts[2]);
				if(!$("#sidebar-storeContentHolder").is(":visible")){
					$("#sidebar-storeContentHolder").slideDown("slow");
				}
				
				bindEmpty();
				bindRemove();
				txt = null;
			}
		);
		return false;
	});
	$("#ms-paypal-review-form").submit(function(){
		$(".required").removeClass("error");
		$(".errorIndicator").remove();
		$("#errormsg").remove();
		var bError = false;
		$("#ms-paypal-review-form .required").each(function(){
			if(
				($(this).attr("name").toLowerCase().indexOf("email") != -1 && !$(this).val().match(regEmail))
				||
				($(this).parents(".form").length > 0 && $(this).parents(".form").is(":visible") && $(this).val() == "")
				||
				($(this).parents(".form").length == 0 && $(this).val() == "")
			  ){
				$(this)
					.addClass("error")
					.before('<img class="errorIndicator" alt="Error" src="/jscripts/images/alert.gif" title="This field has an error!" />');
				bError = true;
			}
		});
		if(bError){
			$("#ms-paypal-review-form")
				.after('<div id="errormsg"><p>Please enter or fix the fields marked with <img class="imgError" alt="Error" src="/jscripts/images/alert.gif" title="This field has an error!" /></p></div>');
			bindUndoErrors();
			return false;
		}
		return true;
	});
	$("#frmCoupon").submit(function(){
		var coupon = $("#coupon").val();
		if(coupon==""){
			alert("Ooops! You forgot to enter coupon..");
			$("#coupon").focus();
			return false;
		}
		$.post(
			ajaxURL,
			{
				Option:		"addDiscount",
				coupon:		coupon,
				isAjaxCall:	1
			},
			function(txt){
				var parts = txt.split("{|}");
				$("#frmCouponResult").html(parts[0]);
				$("#frmCouponResult p.help")
					.animate({opacity:0.999},10000)
					.fadeOut();
				$("#storeBasket").html(parts[1]);
				$("#listAppliedCoupons").html(parts[2]);
				$("#coupon").val("");
				txt = null;
			}
		);
		return false;
	});
	bindEmpty();
	bindRemove();
	bindUpdate();
	bindRemoveCheckout();
});
function bindUndoErrors(){
	$(".error").unbind().keypress(function(){
		$(".required").removeClass("error");
		$(".errorIndicator").remove();
		$("#errormsg").remove();
	});
}
function bindEmpty(){
	$(".ms-empty-cart").click(function(){
		$("#theCart").slideUp("slow");
		$.post(
			ajaxURL,
			{
				Option:		"EmptyCart",
				admid:		contentid,
				isAjaxCall:	1
			},
			function(txt){
				var parts = txt.split("{|}");
				$("#basketContent").html(parts[0]);
				$("#sidebar-storeCart").html(parts[1]);
				$("#sidebar-storeContent").html(parts[2]);

				txt = null;
			}
		);
		return false;
	});
}
function bindUpdate(){
	$(".frmUpdate").submit(function(){
		var targetID	= $(this).attr("id").split("_")[1];
		var prodid		= $("#cart_id_"+targetID).val();
		var prodOptions	= $("#cart_options_"+targetID).val();
		var priceLabel	= $("#cart_priceLabel_"+targetID).val();
		var qty			= $("#cart_qty_"+targetID).val();
		$.post(
			ajaxURL,
			{
				Option:		"updateQty",
				admid:		contentid,
				id:			prodid,
				options:	prodOptions,
				priceLabel:	priceLabel,
				qty:		qty,
				isAjaxCall: 1
			},
			function(txt){
				var parts = txt.split("{|}");
				$("#storeBasket").html(parts[0]);
				$("#sidebar-storeCart").html(parts[1]);
				$("#sidebar-storeContent").html(parts[2]);
				bindUpdate();
				bindRemoveCheckout();
				txt = null;
			}
		);
		return false;
	});
}
function bindRemoveCheckout(){
	$(".ms-remove-cart").click(function(){
		var targetID	= $(this).attr("id").split("_")[1];
		var prodid		= $("#cart_id_"+targetID).val();
		var prodOptions	= $("#cart_options_"+targetID).val();
		var priceLabel	= $("#cart_priceLabel_"+targetID).val();
		$.post(
			ajaxURL,
			{
				Option:		"removeFromCartCheckout",
				admid:		contentid,
				id:			prodid,
				options:	prodOptions,
				priceLabel:	priceLabel,
				isAjaxCall: 1
			},
			function(txt){
				if(txt!=""){
					var parts = txt.split("{|}");
					if(parts[0]!=""){
						$("#storeBasket").html(parts[0]);
						bindUpdate();
						bindRemoveCheckout();
					}else{
						$("#storeBasket").html(parts[1]);
						$("#ms-paypal-review-form").hide();
					}
					$("#sidebar-storeCart").html(parts[1]);
					$("#sidebar-storeContent").html(parts[2]);
					bindUpdate();
					bindRemoveCheckout();
					txt = null;
				}
			}
		);
		return false;
	});
}
function bindRemove(){
	$(".ms-remove").click(function(){
		var targetID	= $(this).attr("id").split("_")[1];
		var prodid		= $("#remove_id_"+targetID).val();
		var prodOptions	= $("#remove_options_"+targetID).val();
		var priceLabel	= $("#remove_priceLabel_"+targetID).val();
		$.post(
			ajaxURL,
			{
				Option:		"removeFromCart",
				admid:		contentid,
				id:			prodid,
				options:	prodOptions,
				priceLabel:	priceLabel,
				isAjaxCall: 1
			},
			function(txt){
				var parts = txt.split("{|}");
				$("#basketContent").html(parts[0]);
				$("#sidebar-storeCart").html(parts[1]);
				$("#sidebar-storeContent").html(parts[2]);
				bindEmpty();
				bindRemove();
				bindUpdate();
				bindRemoveCheckout();
				if(typeof $("p.ms-empty")[0] != "undefined"){
					$("#theCart").slideUp("slow");
				}
				txt = null;
			}
		);
		return false;
	});
}
function preloadImages(){
	for(var i=0;i<arguments.length; i++){
		$("<img>").attr("src", arguments[i]);
	}
}