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:'#D7EBFF'});
	new Effect.Highlight('cart',{startcolor:'#D7EBFF'});
	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"){
		Element.hide("prod"+globalelement);
	}else{
		$("q"+globalelement).innerHTML = response[2];
		new Effect.Highlight("q"+globalelement,{startcolor:'#D7EBFF', endcolor:'#FFFFFF', restorecolor:'#FFFFFF'});
		new Effect.Highlight("ex"+globalelement,{startcolor:'#D7EBFF', endcolor:'#FFFFFF', restorecolor:'#FFFFFF'});
	}
	$('minicart').innerHTML = response[0]+" items | &pound; "+response[1];
	new Effect.Highlight('minicart',{startcolor:'#D7EBFF'});
	$('ex'+globalelement).innerHTML = parseFloat($("ea"+globalelement).innerHTML * response[2]).toFixed(2);
	$('finaltotal').innerHTML = (parseFloat(response[1])).toFixed(2);
	new Effect.Highlight("finaltotal",{startcolor:'#D7EBFF', endcolor:'#FFFFFF', restorecolor:'#FFFFFF'});
}
function normal(element){
	if($(element).value != ''){
		$(element).className = 'normal';
	}
}
function confirmOrder(){
	var cont = true;
	if($('Name').value == ""){
		$('Name').className = 'warning';
		cont = false;
	}
	if($('Address').value == ""){
		$('Address').className = 'warning';
		cont = false;
	}
	if($('Email').value == ""){
		$('Email').className = 'warning';
		cont = false;
	}
	if(cont==false){
		$('errormsg').innerHTML = "Please Enter all required fields";
		return false;
	}else{
		$('cpayment').style.display = "none";
		$('cpaymenti').innerHTML = "<img src=\"i/layout/process.gif\" alt=\"Loading...\" />";
		return true;
	}
}
function checkOut(){
	$('cartcontainer').innerHTML = "<img src=\"i/layout/process.gif\" alt=\"Loading...\" />";
	new Ajax.Updater('cartcontainer', 'ajax/checkout.php', {asynchronous:true});
}
function checkTerms(){
	if($('Terms').checked == true){
		$('submit').disabled = '';
	}else{
		$('submit').disabled = 'disabled';
	}
}