// Deckchairs - JavaScript functions

$(document).ready(function() {

	$('#additem').click(function() {
		button = this;
		pid =  $(button).attr("name");
		$.post("/ajax/session-pid.php", { pid: pid },
			function(data) { 
				if (data==1) { $(button).html("Item Added"); }
				else { $(button).html("Error"); }
			}
		);
		$('#cartbutn').html("<a href=\"/cart.php\" class=\"button black\">View Cart</a> &nbsp;");
	});

	$('.qtyinc').click(function() {
		button = this;
		pid =  $(button).attr("name");
		$.post("/ajax/session-qtyinc.php", { pid: pid },
			function(data) { 
				if (data==1) { location.reload(); }
				else { $(button).html("Error"); }
			}
		);
	});

	$('.qtydec').click(function() {
		button = this;
		pid =  $(button).attr("name");
		$.post("/ajax/session-qtydec.php", { pid: pid },
			function(data) { 
				if (data==1) { location.reload(); }
				else { $(button).html("Error"); }
			}
		);
	});

	$('#sameaddress').change(function() { copyaddress($("#sameaddress:checked").length); });
	$('#rememberme').change(function() { rememberme($("#rememberme:checked").length); });

});

//----------------------------------------------------------------------------------------------------------

function linetotal(price,qty) {
	if (qty>0) { output = "<b>&pound;"+fmtPrice(price*qty)+"</b>"; }
	else { output = ""; }
	return(output);
}

function fmtPrice(value) {
   result = Math.floor(value)+".";
   var pence = 100*(value-Math.floor(value))+0.5;
   result += Math.floor(pence/10);
   result += Math.floor(pence%10);
   return(result);
}

function copyaddress(copy) {
	delivery = (copy) ? $("#name").attr("value") + "\n" + $("#cardaddress").attr("value") : "";
	$("#deliverto").text(delivery)
}

function rememberme(remember) {
	setCookie("name",$("#name").attr("value"), (remember) ? 1095 : 0 );
	setCookie("cardaddress",$("#cardaddress").attr("value"), (remember) ? 1095 : 0 );
	setCookie("telephone",$("#telephone").attr("value"), (remember) ? 1095 : 0 );
	setCookie("email",$("#email").attr("value"), (remember) ? 1095 : 0 );
	setCookie("deliverto",$("#deliverto").attr("value"), (remember) ? 1095 : 0 );
	setCookie("comments",$("#comments").attr("value"), (remember) ? 1095 : 0 );
	setCookie("rememberme", "true", (remember) ? 1095 : 0 );
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function validate_orderform(order) {
	if (order.amount.value=="0.00") { alert("Sorry, we can't send out an order for £0.00"); return false; }
	if (!order.name.value) { alert("Please enter your name"); return false; }
	if (!order.cardaddress.value) { alert("Please enter your address"); return false; }
	if (!order.telephone.value) { alert("Please enter a telephone number, in case we need to contact you about your order."); return false; }
	if (!order.email.value) { alert("Please enter your email address in case we need to contact you about your order, or 'None' if you do not have one."); return false; }
	if (!order.deliverto.value) { alert("These items are all quite large, please ensure that you have provided a delivery address, click on 'same as above' to use the card holder address."); return false; }
	if (!order.comments.value) { alert("Please enter your delivery instructions or 'None' if not applicable."); return false; }
	if (!order.termsconditions.checked) { alert("Please confirm that you have read the delivery information and terms & conditions"); return false; }	
	if (order.name.value=="test") { return false; }
	return true;
}
