//cart functions 
function cartAdd(id,price,quantity) {
	$.ajax({
	   type: "POST",
		url: baseUrl+"/cart/-/ajax/",
	   data: {action:"cartAdd",productId: id,price: price,quantity: quantity},
	   success: function(msg){
		 $('#messageArea'+id).hide();
		 //$('#messageArea').html('<span>This image was added to your cart.</span>');
		 $('#messageArea'+id).fadeIn("normal").wait().fadeOut("slow");
		 cartRefresh();
	   }
	 });
}

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

function cartUpdate(idx,price,quantity) {
	if (quantity == 0) {
		cartRemove(idx);
		return;
	}
	$.ajax({
		type: "POST",
		url: baseUrl+"/cart/-/ajax/",
	   data: {action:"cartUpdate",index: idx,quantity: quantity},
	   success: function(msg){
		 cartRefresh();
		 cartRefreshSubtotals();
	   }
	 });
}

function cartSetCountry(country) {
	jQuery.ajax({
		type: "POST",
		url: baseUrl+"/cart/-/ajax/",
		dataType: "json",
	   data: {action:"cartSetCountry",country: country},
	   success: function(msg){
		 cartRefreshSubtotals(msg);
	   }
	 });
}

function cartRefresh() {
	$('#CartTab').load(baseUrl+"/cart/-/ajax/",{action:"cartRefresh"});
}

function cartRefreshSubtotals(setCountry,setPromoCode) {
	$('#CartSubtotals').load(baseUrl+'/cart/-/ajax/', {action:"cartSubtotals",country:setCountry,promoCode:setPromoCode});	
}




function cartRemove(idx) {
	$.ajax({
	   type: "POST",
		url: baseUrl+"/cart/-/ajax/",
	   data: {action:"cartRemove",index: idx},
	   success: function(msg){
	   	itemsLeft = msg;
		if (itemsLeft == 0) document.location = document.location;
		else $('#cartitems_'+idx+'').remove();
		cartRefresh();
		cartRefreshSubtotals();
	   }
	 });
}

function deleteRow(theId) {
	$(theId).remove();
}


//login functions
function cartLogin(form) {
	$.ajax({
		type: "POST",
		url: baseUrl+"/cart/-/ajax/",
		data: $(form).serialize(),
		success: function(msg){
			if (msg=="") window.location.reload();
			else $('.errorMessage',$(form)).show();
		   }
	});
	return false;
}

function cartLogout() {
	$.ajax({
		type: "POST",
		url: baseUrl+"/cart/-/ajax/",
		data: {action: 'doLogout'},
		success: function(msg){
			if (msg=="") window.location.reload();
		   }
	});
	return false;
}

function doAjaxAction(action,callback) {
	var params="ajax=&action="+action;
	new Ajax.Request('/Page.php', { parameters: params, onSuccess: callback} );
}

function doAjaxUpdate(action,element) {
	if ($(action)) var params = Form.serialize(action)+"&ajax=";
	else var params="ajax=&action="+action;
	new Ajax.Updater(element,'/Page.php', { parameters: params } );	
}



function submitForm(form,callback) {
	
	
	var params = Form.serialize(form)+"&ajax=";
	new Ajax.Request('/Page.php', { parameters: params, 
									onSuccess: callback
								}
					)
	return false;
}

function LoginFormCallback(response) {
	if (response.readyState != 4) {
	    return;
	}
	else {
		if (response.responseText == null || response.responseText == "") window.location.reload();
		else $('loginFailedMessage').show();
	}
}

function LoginFormCallback2(response) {
	if (response.readyState != 4) {
	    return;
	}
	else {
		if (response.responseText == null || response.responseText == "") window.location.reload();
		else $('loginFailedMessage2').show();
	}
}


function Validate(id) {
	var theForm = $(id);
	$(".required",theForm).after('<span style="color: red;">' + '*' + '</span>');
	theForm.submit(
		function () {
			var error = false;
			//test required fields
			$('#advice-required').hide();
			var passed = true;
			$(".required",this).each(function() { if (this.value == '') passed=false });
			if (!passed) {
				$('#advice-required').show();
				error = true;
			}
			//test email field
			$('#advice-validate-email').hide();
			var passed = true;
			$(".validate-email",this).each(function() {
				var test = this.value == "" || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(this.value);
				if (!test) passed = false;
			} );
			if (!passed) {
				$('#advice-validate-email').show();
				error = true;
			}
			//test password field
			$('#advice-validate-password').hide();
			var passed = true;
			$(".validate-password",this).each(function() {
				var test = (this.value == $('#cart\\[yourdetails\\]\\[password\\]').val());
				if (!test) passed = false;
			} );
			if (!passed) {
				$('#advice-validate-password').show();
				error = true;
			}
			return !error;
		}
	);
}
