// Deckchairs - JavaScript functions

$(document).ready(function() {

	$('#menu li.pagelabel').hover( function() { $('li.menulabel ul').css({'visibility' : 'hidden'}); }, function() {} );
	$('.menulabel').hover( function() { $('li.menulabel ul').css({'visibility' : 'hidden'}); $(this).children().children().css({'visibility' : 'visible'}); }, function() {} );
	$('#menu').mouseenter(function(){ clearTimeout(menuTimer); });
	$('#menu').mouseleave(function(){ menuTimer = setTimeout( "closeMenu()", 1500); });

	$('input.qty').keyup(function() {
		thisbox = this;
		pid =  $(thisbox).attr("name");
		qty =  $(thisbox).attr("value");
		$(thisbox).css({ 'background-color' : 'yellow'});
		if (qty.match(/^[0-9]+$/)&&qty>0) { 
			$.post("/ajax/session-pid-qty.php", { pid: pid, qty: qty },
				function(data) { 
					if (data==1) { $(thisbox).css({ 'background-color' : '' }); }
					else { $(thisbox).css({ 'background-color' : 'red' }); }
				}
			);
		}
		else {
			$(thisbox).attr("value","");
			$.post("/ajax/session-pid-qty.php", { pid: pid, qty: 0 },
				function(data) { 
					if (data==1) { $(thisbox).css({ 'background-color' : '' }); }
					else { $(thisbox).css({ 'background-color' : 'red' }); }
				}
			);
		}
		linetot = fmtPrice(qty * $(thisbox).prev().attr("value"));
		$(thisbox).next("span").html("<b>&pound;"+linetot+"</b> <a href=\"cart.php\" title=\"Click here to view your items\" ><img src=\"images/cart.gif\" class=\"carticon\"></a>");
	});
	
	$('#sameaddress').change(function() { copyaddress($("#sameaddress:checked").length); });
	$('#rememberme').change(function() { rememberme($("#rememberme:checked").length); });

	$("span.thumbs a").wTooltip({ content: true, id: "wTooltipIMG", style: false, 
		callBefore: function(tooltip, node) { 
			productREF = "str:"+node; // converts value node to string
			productREF = productREF.substring((productREF.lastIndexOf("/")+1),(productREF.indexOf(".html")));
			html="<img src=\"/file/images/products/medium/"+productREF+".jpg\" />";
			$(tooltip).html(html);
		} 
	});

	$(".labeller a").wTooltip({ id: "wTooltipTEXT", style: false });


});

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

function closeMenu() {
	$('#menu li.menulabel ul').css({'visibility' : 'hidden'});
	clearTimeout(menuTimer);
}

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;
}