var siteURL = "http://"+document.domain+"/";

function toggleMenu(target, pos) {
	targetMenu = (document.getElementById) ? document.getElementById(target).style : eval("document." + target);
	targetMenu.top = pos;
}

$(document).ready(function() {
	$('ul.square li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
});

function loadProduct(cat) {
	var objID = "product"+cat;
	var serverPage = siteURL+"modules/loadproducts.php";
	var obj = document.getElementById(objID);
	$.post(serverPage, {cat:cat}, function(data){
		obj.innerHTML=data;
	});	
}

function updatePrice(id, qty, unitprice, serves, name) {
	var serverPage = siteURL+"modules/updateorder.php";
	$.post(serverPage, {id:id, qty:qty, unitprice:unitprice, serves:serves, name:name}, function(data){
		//$('#show').html(data);
		$('#totalprice').html(data);
	});
}

function checkOrder() {
	var price = parseFloat($('#totalprice').html());
	if (price == '0.00') {
		$('#errorText').html("Please place your order!<br /><br />");
	} else if (price < '60.00') {
		$('#errorText').html("Sorry, minimum order &pound;60!<br /><br />");
		$('#errorFloat').html("Sorry, minimum order &pound;60!<br /><br />");
	} else {
		location.href='checkout.php';
	}
}

function checkEmail(emailField, errorContainer) {
	if (emailField.length == 0) {
		$(errorContainer).html("You must provide your e-mail address.");
		return false;
	}
	if (emailField.indexOf(" ") > -1) {
		$(errorContainer).html("E-mail address has invalid space.");
		return false;
	}
	if (emailField.indexOf("/") > -1) {
		$(errorContainer).html("E-mail address has invalid character: /");
		return false;
	}
	if (emailField.indexOf(":") > -1) {
		$(errorContainer).html("E-mail address has invalid character: :");
		return false;
	}
	if (emailField.indexOf(",") > -1) {
		$(errorContainer).html("E-mail address has invalid character: ,");
		return false;
	}
	if (emailField.indexOf(";") > -1) {
		$(errorContainer).html("E-mail address has invalid character: ;");
		return false;
	}
	if (emailField.indexOf("@") < 0) {
		$(errorContainer).html("E-mail address is missing @");
		return false;
	}
	if (emailField.indexOf("\.") < 0) {
		$(errorContainer).html("E-mail address is missing .");
		return false;
	}
	return true;
}


function checkFormDetail(formObj) {
	var formOK = true;

	formOK = checkEmail (formObj.email.value, '#error_email');

	if (formObj.name.value == "") {
		$('#error_name').html("You must provide your name.");
		formOK = false;
	}
	if (formObj.tel.value == "") {
		$('#error_tel').html("You must provide your telephone number.");
		formOK = false;
	}
	if (formObj.address.value == "") {
		$('#error_address').html("You must provide your address.");
		formOK = false;
	}
	if (formObj.postcode.value == "") {
		$('#error_postcode').html("You must provide your post code.");
		formOK = false;
	}
	if (formObj.parish.value == "") {
		$('#error_parish').html("You must provide your parish.");
		formOK = false;
	}

	if (formOK == true) {
		formObj.submit();
	}
}


/* favourite, ajax*/
function addFavourite(uid, item_id){
	//The location we are loading the page into.
	var objID = "my_favourite_box";
	var serverPage = siteURL+"/modules/mod_favourite/add.php";
	var obj = document.getElementById(objID);

	$.get(serverPage, {uid:uid, item_id:item_id}, function(data){
		obj.innerHTML=data;
	});	
}

function delFavourite(uid, item_id){	
	//The location we are loading the page into.
	var objID = "my_favourite_box";
	var serverPage = siteURL+"/modules/mod_favourite/del.php";
	var obj = document.getElementById(objID);

	$.get(serverPage, {uid:uid, item_id:item_id}, function(data){
		obj.innerHTML=data;
	});	
}

