var globalelement = "";

function flashCart(){
	$('cart-glow').style.display='';
	Effect.Fade('cart-glow');
}


function addToCart(productid,howmany){
	var attribs = $('prod'+productid).getElementsByTagName("select");
	var attributelist = "";
	var selectattributes = false;
	for(var i = 0; i < attribs.length; i++) { 
		if(attribs[i].value){
			attributelist = attributelist + "|" + attribs[i].value;
		}else{
			selectattributes = true;
		}
	}
	if(selectattributes==true){
		alert("Please finish selecting product options before continuing");
	}else{
		var query = "productid="+productid+"&quantity="+document.getElementById("quantity"+productid).value+"&attributes="+attributelist;
		document.getElementById("quantity"+productid).value = "1";
		//new Effect.Highlight('prod'+productid,{startcolor:'#C8CDE3',endcolor:'#C8CDE3'});
		flashCart();
		var ajax = new Ajax.Request('ajax/addtocart.php',
									{asynchronous: true,
									method: 'get',
									onComplete: processResponse,
									parameters: query
									});
	}
	return false;
}

function processResponse(req) {
	$('cart-inner').innerHTML = req.responseText;
}

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 addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function processQuantity(req){
	var	response = req.responseText.split("|");
	var totalexvat, justvat, totalincvat; 

	totalexvat = response[1].replace(/\,/g,'');
	justvat	= (0.15 * totalexvat).toFixed(2);
	totalincvat	= (1.15 * totalexvat).toFixed(2);

	justvat	= addCommas(justvat);
	totalincvat	= addCommas(totalincvat);

	if(response[2]=="0"){
		Element.hide("prod"+globalelement);
	}else{
		$("q"+globalelement).innerHTML = response[2];
		new Effect.Highlight("q"+globalelement,{startcolor:'#FADBEA'});
		new Effect.Highlight("ex"+globalelement,{startcolor:'#FADBEA'});
	}
	$('minicart').innerHTML = response[0]+" items | &pound; "+response[1];
	$('minicart').innerHTML = '<strong>'+response[0]+'</strong> items<br /><br />Total Excl Vat:<br />&pound;'+response[1]+'<br />Total Incl Vat:<br />&pound;'+totalincvat;

	/*$('finaltotal').innerHTML = response[1];*/
	
	$('finaltotal').innerHTML = totalexvat;
	$('vattotal').innerHTML = justvat;
	$('carttotal').innerHTML = totalincvat;
	
	flashCart();
	$('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:'#6E6E6E'});
	new Effect.Highlight("vattotal",{startcolor:'#6E6E6E'});
	new Effect.Highlight("carttotal",{startcolor:'#6E6E6E'});
}

function normal(element){
	if($(element).value != ''){
		$(element).className = 'normal';
	}
}

function checkOut(){
	new Ajax.Updater('cartcontainer', 'ajax/checkout.php', {asynchronous:true});
}

function confirmOrder() {
	var cont = true;
	if($('CustomerName').value == ""){
		$('CustomerName').className = 'warning';
		cont = false;
	}
	if($('ContactNumber').value == ""){
		$('ContactNumber').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($('DeliverySame').checked==false){
		if($('DeliveryAddress').value == ""){
			$('DeliveryAddress').className = 'warning';
			cont = false;
		}
		if($('DeliveryPostCode').value == ""){
			$('DeliveryPostCode').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';
	}
}