var globalelement = "";

function addToCart(productid,howmany){
	var query = "productid="+productid+"&quantity="+document.getElementById("quantity"+productid).value+"&attributes="+document.getElementById("attributelist"+productid).value;
	if(howmany > 0){
		document.getElementById("addcart"+productid).style.display = "none";
		document.getElementById("a"+productid+"1").style.display = "";
	}
	document.getElementById("attributelist"+productid).value = "";
	document.getElementById("quantity"+productid).value = "1";
	new Effect.Highlight('prod'+productid,{startcolor:'#E6E5F9'});
	new Effect.Highlight('shopcart',{startcolor:'#E6E5F9'});
	var ajax = new Ajax.Request('ajax/addtocart.php',
								{asynchronous: true,
								method: 'get',
								onComplete: processResponse,
								parameters: query
								});
	return false;
}
function processResponse(req) {
	$('minicart').innerHTML = req.responseText;
}
function addAttribute(productid,attributeid,optionid,islast,current){
	if(islast==true){
		document.getElementById("a"+productid+current).style.display = "none";
		document.getElementById("addcart"+productid).style.display = "";
		if(document.getElementById("select"+attributeid)){
			document.getElementById("select"+attributeid).value = "";
		}
	}else{
		var next = parseFloat(current) + 1;
		document.getElementById("a"+productid+current).style.display = "none";
		document.getElementById("a"+productid+next).style.display = "";
		if(document.getElementById("select"+attributeid)){
			document.getElementById("select"+attributeid).value = "";
		}
	}
	document.getElementById("attributelist"+productid).value = document.getElementById("attributelist"+productid).value + "|" + attributeid + "-" + optionid;
}
function updateCart(productid,quantity,attributes,uniqueid){
	globalelement = uniqueid;
	var query = "productid="+productid+"&update=true&quantity="+quantity+"&attributes="+attributes;
	var ajax = new Ajax.Request('ajax/addtocart.php',
								{asynchronous: true,
								method: 'get',
								onComplete: processQuantity,
								parameters: query
								});
	return false;
}
function processQuantity(req){
	var	response = req.responseText.split("|");
	if(response[2]==0){
		$("prod"+globalelement).style.display = 'none';
		Element.hide("prod"+globalelement);
	}else{
		if(response[2]==$("q"+globalelement).innerHTML){
			alert("You can not increase the quantity of this product. We only have "+response[2]+" in stock");
		}else{
			$("q"+globalelement).innerHTML = response[2];
			new Effect.Highlight("q"+globalelement,{startcolor:'#E6E5F9'});
			new Effect.Highlight("ex"+globalelement,{startcolor:'#E6E5F9'});
		}
	}
	$('minicart').innerHTML = response[0]+" items<br /> <strong>&pound; "+response[1]+"</strong><br />";
	$('finaltotal').innerHTML = response[1];
	$('ex'+globalelement).innerHTML = (parseFloat($("ea"+globalelement).innerHTML) * parseFloat(response[2])).toFixed(2);
	if($('q'+globalelement).innerHTML=="0"){
		$('prod'+globalelement).style.display = "none";
	}
	new Effect.Highlight("finaltotal",{startcolor:'#E6E5F9'});
}
function normal(element){
	if($(element).value != ''){
		$(element).className = 'normal';
	}
}

function checkOut(){
	var pars = 'country='+$('country').value;
	$('cartcontainer').innerHTML = "<img src=\"i/layout/process.gif\" alt=\"Loading...\" />";
	var ajax = new Ajax.Request('ajax/checkout.php',
								{asynchronous: true,
								method: 'post',
								onComplete: proCheckout,
								parameters: pars
								});
	//new Ajax.Updater('cartcontainer', 'ajax/checkout.php', {asynchronous:true});
}

function proCheckout(req){
	$('cartcontainer').innerHTML = req.responseText;
}

function confirmOrder() {
	var cont = true;
	if($('CustomerName').value == ""){
		$('CustomerName').className = 'warning';
		cont = false;
	}
	if($('BillingAddress').value == ""){
		$('BillingAddress').className = 'warning';
		cont = false;
	}
	if($('BillingPostCode').value == ""){
		$('BillingPostCode').className = 'warning';
		cont = false;
	}
	if($('CustomerEmail').value == ""){
		$('CustomerEmail').className = 'warning';
		cont = false;
	}else{
		if(CheckEmail($('CustomerEmail').value)==false){
			$('CustomerEmail').className = 'warning';
			cont = false;
		}
	}
	if(cont==false){
		$('errormsg').innerHTML = "Please Enter all required fields, and make sure E-Mail is valid.";
		return false;
	}else{
		return true;
		
	}
}
function CheckEmail(email) {
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	
	if (AtPos == -1 || StopPos == -1) {
		return false;
	}else{
		return true;
	}
}
function checkTerms(){
	if($('Terms').checked == true){
		$('submit').disabled = '';
	}else{
		$('submit').disabled = 'disabled';
	}
}