var currentemail='<a href="mailto:sales@tamarorganics.co.uk">sales@tamarorganics.co.uk</a>';
var finalorder;
var cartName=new Array();
var cartQuant=new Array();
var cartCost=new Array();
var cartCode=new Array();
var cartId=new Array();

var ccd=0;

var val;
var val2;
var t;
var timer_is_on=0;

var code;
var quantity;
var orderval;
var id;

var discountline="";
var carriageline="";
var purchases;
var optcart=15;
var altcart=0;
var cartlist;
var altcartlist;
var Mcartlist;
var altMcartlist;
var totalcartprice=0;
var Mtotalcartprice=0;
var mailcart;

var ordercode;
var ordervalue=0;

var basketstatus;
var purchase=0;

var winW=0;
var winH=0;
var lpos=0;
var tpos=0;

var pnp=1.60;
var pnplim=50.00;
fertdiscount = new Array();
fertdiscount[0]=6;
fertdiscount[1]=0;
fertdiscount[2]=1.55;
fertdiscount[3]=3.80;
fertdiscount[4]=9.05;
fertdiscount[5]=14.30;
fertdiscount[6]=19.55;
var fertquant=0;
var discount=0;
var finalcost=0;
var fruitcarriagecharge=12.50;
var myfruitcarriage=0;
var potcarriage1=4.50;
var potcarriage2=6.50;
var potcarriage3=11.00;
var potcarriage12=4.50;
var potcarriage22=12.50;
var potcarriage32=20.00;
var potcarriage13=4.50;
var potcarriage23=13.50;
var potcarriage33=22.00;
var potconstant=4.25;
var carriage=0;
var kgs=0;
var buyoBj;
var potcode="POAA25,POAD25,POAM25,POAN25,POAR25,POAT25,POBA25,POBQ25,POCA25,POCB25,POCH25,POCN25,POCO25,PODE25,PODU25,POED25,POFR25,POGW25,POHB25,POHU25,POIJ25,POLA25,POLI25,POLZ25,POMA25,POMB25,POML25,POMP25,POMV25,PONI25,POOR25,POPF25,POPJ25,POPR25,PORB25,PORC25,PORE25,PORO25,PORT25,POSA25,POSB25,POSE25,POSH25,POSM25,POSW25,POTR25,POTU25,POVA25";

var fruits="APAE,APAK,APBL,APBO,APBR,APCH,APCV,APCX,APDA,APDS,APEG,APEO,APFA,APFI,APGD,APHE,APHG,APJG,APKA,APLL,APLS,APRJ,APSP,APSU,APTO,APWP,APYA,CAJD,CARS,CTLA,CTME,CTMR,CTPE,CTSS,CTST,CTSU,PEBE,PEBT,PEDC,PELB,PEOD,PLBL,PLBT,PLCA,PLCG,PLCZ,PLEP,PLFA,PLMS,PLOG,PLOP,PLPU,PLSA,PLSP,PLVA,QUMP,QUVR";
					 
var ferts="CSF1,DOL1,DOL25,GRE1,GREE,SM01,SM20,TM51,TM525,TMK1,TMK25,TMNK1,TMNK25";
          
var reg1="KW1,KW2,KW3,KW4,KW5,KW6,KW7,KW8,KW9,KW10,KW11,KW12,KW13,KW14,IV,PA34,PA37,PA38,PA39,PH19,PH20,PH21,PH22,PH23,PH24,PH25,PH26,PH30,PH31,PH32,PH33,PH34,PH35,PH36,PH37,PH38,PH39,PH40,PH49,PH50,TH41";
var reg2="BT10,BT11,BT12,BT13,BT14,BT15,BT16,BT17,BT19,BT2,BT20,BT21,BT22,BT23,BT24,BT25,BT26,BT27,BT28,BT29,BT3,BT30,BT31,BT32,BT33,BT34,BT35,BT36,BT37,BT38,BT39,BT4,BT40,BT41,BT42,BT43,BT44,BT45,BT46,BT48,BT49,BT5,BT51,BT52,BT53,BT54,BT55,BT56,BT57,BT6,BT61,BT62,BT63,BT64,BT65,BT66,BT67,BT69,BT7,BT70,BT71,BT74,BT75,BT76,BT77,BT78,BT79,BT8,BT80,BT81,BT82,BT9,BT92,BT93,BT94,IM1,IM3,IM4,IM5,IM6,IM7,IM8,IM9,PO30,PO31,PO32,PO33,PO34,PO35,PO36,PO37,PO38,PO39,PO40,PO41";

function resavecart(){
	location='catalogue.php';
}

function round2(orignum){
	var rounded=Math.round(orignum*100)/100
	return rounded;
}

function potatocarriage(code,num){
	if(potcode.match(code)){
		kgs=parseFloat(kgs+(num*25))
	}
}

function fruitcarriage(code){
	if(fruits.match(code) && myfruitcarriage==0){
			carriage=round2(parseFloat(carriage+fruitcarriagecharge));
			myfruitcarriage=fruitcarriagecharge;
		}
}

function calcarriage(){
	var region="";
	if(document.checkoutform){
		regions=document.checkoutform.pcode.value.split(" ");
		region=regions[0];
	}
	if(kgs>0){	
		if(region && reg1.match(region)){
			if(kgs<=3){carriage=round2(parseFloat(carriage+potcarriage12));}
			if(kgs>3 && kgs <=9){carriage=round2(parseFloat(carriage+potcarriage22));}
			if(kgs>9){carriage=round2(parseFloat(carriage+potcarriage32));}
		}
		else{
			if(region && reg2.match(region)){
				if(kgs<=3){carriage=round2(parseFloat(carriage+potcarriage13));}
				if(kgs>3 && kgs <=9){carriage=round2(parseFloat(carriage+potcarriage23));}
				if(kgs>9){carriage=round2(parseFloat(carriage+potcarriage33));}
			}
			else{
				if(kgs<=3){carriage=round2(parseFloat(carriage+potcarriage1));}
				if(kgs>3 && kgs <=25){carriage=round2(parseFloat(carriage+potcarriage2));}
				if(kgs>25 && kgs <=50){carriage=round2(parseFloat(carriage+potcarriage3));}
				if(kgs>50){extra=parseFloat(Math.ceil((kgs-50)/25)*potconstant);carriage=round2(parseFloat(carriage+potcarriage3+extra));}
			}
		}
	}
	if(carriage==0 && totalcartprice<pnplim){
		carriage=pnp;
	}
}

function fertdiscounts(code,ind){
	if(ferts.match(code)){
		fertquant=parseInt(fertquant+(ind*1));
	}
}

function calcfertdiscount(){
		if(fertquant>0 && fertquant<=6){
				newdiscount=fertdiscount[fertquant];
				discount=round2(parseFloat(discount+newdiscount));
		}
		else{
			if(fertquant>6){
				newdiscount=(fertquant-6)*(4.30)+19.55;
				discount=round2(parseFloat(discount+newdiscount));
			}
		}
}


function checkCookieItems(savedval,oldcart){
	if(savedval){
		createSavedCartCookie(savedval);
		if(!readCookie('cart')){
			retrievecart(oldcart);
		}
	}
	var mycart=readCookie('cart');
	if(mycart){
		purchases = mycart.split(',');
		var items = purchases.length;
		if(items>0){
			document.getElementById('cartview').style.display='block';
			document.getElementById('getcartlink').style.display='none';
		}
		else{
			document.getElementById('cartview').style.display='none';
		}
	}
	else{
			document.getElementById('getcartlink').style.display='block';
			}
	if(cartstatus()){
		document.getElementById('storecartlink').style.display='block';
	}
}

function cartstatus(){
	if(readCookie('savedcart') != null){
		var mysavedcart = readCookie('savedcart').split(':')
		var cartid=mysavedcart[0];
		var cartemail=mysavedcart[1];
		
		if(cartid && !cartemail){
			return 1;
		}
	}
}

function createSavedCartCookie(value){
	var days=14;
	var name='savedcart';
	var mydate = new Date();
	mydate.setTime(mydate.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+mydate.toGMTString();
	if(value)
	document.cookie = name+"="+value+expires+"; path=/";
}

function loadCheckout(oid){
	if(!oid){
		var codes="";
		var quants="";
		var mycart=readCookie('cart');
		var mailcart="";
		if(mycart){
			purchases = mycart.split(',');
			var items = purchases.length;
			if(items>0){
				document.getElementById('cartview').style.display='block';
			}
			totalcartprice=0;
			cartlist="";
			altcartlist="";
			discount=0;
			carriage=0;
			fertquant=0;
			myfruitcarriage=0;
			kgs=0;
			
			purchases.length>(optcart+5) ? altcart=parseInt(purchases.length-optcart) : altcart=0;
			
			for(i=0;i<purchases.length;i++){
				
				prodfeatures=purchases[i].split(':');
				
				cartCode[i]=prodfeatures[1];
				cartQuant[i]=prodfeatures[2];
				cartCost[i]=parseFloat(prodfeatures[3]);
				cartName[i]=prodfeatures[0];
				cartId[i]=prodfeatures[4];
				totalcartprice=totalcartprice+cartCost[i];
				fruitcarriage(cartCode[i]);
				fertdiscounts(cartCode[i],cartQuant[i]);
				potatocarriage(cartCode[i],cartQuant[i]);
				cartlist=cartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartName[i]+'</div><div class=cartItem>'+cartCode[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
				mailcart=mailcart+cartQuant[i]+' '+cartName[i]+' '+cartCode[i]+' '+cartCost[i].toFixed(2)+'\r\n';
				if(altcart && i>=altcart)
				altcartlist=altcartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartName[i]+'</div><div class=cartItem>'+cartCode[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
				codes=codes+cartCode[i]+',';
				quants=quants+cartQuant[i]+',';
			}
			calcarriage();
			calcfertdiscount()
			codes=codes.substring(0, codes.length - 1);
			quants=quants.substring(0, quants.length - 1)
			ordervalue=parseFloat(totalcartprice-discount+carriage);
		}
		document.checkoutform.mailcart.value=mailcart+'\r\nDiscount : -'+discount+'\r\nCarriage : '+carriage+'\r\n\r\nTotal: £ '+ordervalue.toFixed(2);
		document.checkoutform.ordervalue.value=ordervalue.toFixed(2);
		document.checkoutform.discount.value=discount.toFixed(2);
		document.checkoutform.carriage.value=carriage.toFixed(2);
		document.checkoutform.ordervaluedisplay.value=ordervalue.toFixed(2);
		document.checkoutform.codes.value=codes;
		document.checkoutform.quants.value=quants;
	}
	else{
		killcart(oid);
	}
}

function createCart(savedval){
	if(savedval){
		createSavedCartCookie(savedval);
	}
	var mycart=readCookie('cart');
	if(mycart){
		purchases = mycart.split(',');
		var items = purchases.length;
		buildMCart()
	}
	else{
		eraseCookie('cart');
		if(cart=readCookie('savedcart')){
			var mycart=cart.split(':');
			var cartid=mycart[0];
			eraseCookie('savedcart');
		}
		else{
			var cartid='';
		}
		location='http://www.tamarorganicspro.co.uk/pages/catalogue.php?delcartid='+cartid;
	}
}

function buildMCart(){
	totalcartprice=0;
	Mcartlist="";
	discount=0;
	carriage=0;
	fertquant=0;
	myfruitcarriage=0;
	kgs=0;
	for(i=0;i<purchases.length;i++){
		
		prodfeatures=purchases[i].split(':');
		
		cartCode[i]=prodfeatures[1];
		cartQuant[i]=prodfeatures[2];
		cartCost[i]=parseFloat(prodfeatures[3]);
		cartName[i]=prodfeatures[0];
		cartId[i]=prodfeatures[4];
		totalcartprice=totalcartprice+cartCost[i];
		fertdiscounts(cartCode[i],cartQuant[i]);
		fruitcarriage(cartCode[i]);
		potatocarriage(cartCode[i],cartQuant[i]);
		Mcartlist=Mcartlist+'<div class=Mcartrow><div class=Mcartquant><div title=\"click to decrease quantity\" class=dec onClick=\"decCart('+i+')\"><</div><div class=quant title=\"click on arrows to change the quantity\">'+cartQuant[i]+'</div><div title=\"click to increase quantity\" class=inc onClick=\"incCart('+i+')\">></div></div><div class=cartitem><div class=Mcartname>'+cartName[i]+'</div><div class=Mcartcode>'+cartCode[i]+'</div><div class=Mcartprice>'+cartCost[i].toFixed(2)+'</div></div><div class=del title=\"delete this item\" onClick=\"delCart(this,'+i+',0)\">X</div></div>';
	}
	calcarriage();
	calcfertdiscount()
	revealMCart();
}

function revealMCart(){
	var discountline="";
	var carriageline="";
	Mtotalcartprice=parseFloat(totalcartprice-discount+carriage);
	if(discount){
		discountline='<div class=discountMcartrow><div class=McartDiscount>DISCOUNT</div><div class=discountMprice>- '+discount.toFixed(2)+'</div></div>';
	}
	if(carriage){
		carriageline='<div class=discountMcartrow><div class=McartDiscount>CARRIAGE</div><div class=discountMprice> '+carriage.toFixed(2)+'</div></div>';
	}
	else{
		carriagline='<div id=ukpnprow><div id=ukpnp><div id=ukpnpcost>&nbsp;</div><div id=ukpnptitle>* plus UK Carriage :</div></div></div>';
	}
	document.getElementById('maxicart').innerHTML=Mcartlist+discountline+carriageline+"<div id=Mcarttotalrow><img src=\"\../images/delcart15a.gif\" onClick=\"eraseCart(this,0)\" id=cartdel title=\"click to delete cart\"><div id=Mcartotal>£"+Mtotalcartprice.toFixed(2)+"</div><div id=Mcartotaltitle>Total:</div></div>";
}



function addtocart(thisname,thiscode,thisquantity,thisorderval,thisid,oBj){
	buyoBj=oBj;
	code=thiscode;
	var nam=thisname;
	newname=nam.replace(/,/g,"");
	var value=newname+':'+thiscode+':'+thisquantity+':'+thisorderval+':'+thisid;
	
	createCookie('cart',value,14);
	checkCookieItems();
	if(purchase==1)
	confirmpurchase(thisname,thiscode,oBj);
}

function confirmpurchase(thisname,thiscode,oBj){
	var pos=findPos(oBj);
	document.getElementById('salebox').innerHTML="<div style=\"width: 300px;height: 20px;margin: 5px 0px 0px 0px\">"+thisname+" : "+thiscode+" purchased</div>";
	document.getElementById('salebox').style.left=(pos[0]-150)+'px';
	document.getElementById('salebox').style.top=(pos[1]-50)+'px';
	document.getElementById('salebox').style.display='block';
	t=setTimeout("hidesalebox()",2000)
}

function alreadybought(){
	var pos=findPos(buyoBj);
	document.getElementById('salebox').innerHTML="<div style=\"width: 300px;height: 20px;margin: 5px 0px 0px 0px\">you already have that in your shopping cart</div>";
	document.getElementById('salebox').style.left=(pos[0]-150)+'px';
	document.getElementById('salebox').style.top=(pos[1]-50)+'px';
	document.getElementById('salebox').style.display='block';
	t=setTimeout("hidesalebox()",2000)
}

function hidesalebox(){
	document.getElementById('salebox').style.display='none';
	purchase=0;
}

function retrievecart(value){
	createCookie('cart',value,14);
	checkCookieItems();
}

function addtocheckout(thiscode,thisquantity,thisorderval){
	code=thiscode;
	var value=thiscode+':'+thisquantity+':'+thisorderval+':'+id;
	createCookie('cart',value,14);
	checkCookieItems2();
}

function incCart(ind){
	var basecost=parseFloat(cartCost[ind])/parseInt(cartQuant[ind]);
	cartQuant[ind]=parseInt(cartQuant[ind])+1;
	cartCost[ind]=basecost*cartQuant[ind];
	reCreateCookie();
}

function decCart(ind){
	var basecost=parseFloat(cartCost[ind])/parseInt(cartQuant[ind]);
	if(cartQuant[ind] !=1){
		cartQuant[ind]=(cartQuant[ind]-1);
		cartCost[ind]=basecost*cartQuant[ind];
		reCreateCookie();
	}
}

function delCart(oBj2,ind,decision){
	var conftxt='click to delete this item?';
	switch(decision){
		case 1:
			document.getElementById('confirmbox').style.display='none';
			cartCode.splice(ind,1)
			cartName.splice(ind,1)
			cartQuant.splice(ind,1)
			cartCost.splice(ind,1)
			cartId.splice(ind,1)
			reCreateCookie();
			break;
		case 2:
			document.getElementById('confirmbox').style.display='none';
			break;
		default:
			document.getElementById('confirmbox').innerHTML="<div id=confirmtext>"+conftxt+"</div><div id=confirmbutL onClick=\"delCart(this,"+ind+",1)\">Yes</div><div id=confirmbutR onClick=\"delCart(this,"+ind+",2)\">No</div>";
			var pos=findPos(oBj2);
			document.getElementById('confirmbox').style.left=(pos[0]-100)+'px';
			document.getElementById('confirmbox').style.top=(pos[1]-100)+'px';
			document.getElementById('confirmbox').style.display='block';
	}
}

function reCreateCookie(){
	var name='cart';
	var value="",cartcode="",cartquantity=0,cartorderval=0,cartid="",cartname="";
	var date = new Date();
	date.setTime(date.getTime()+(14*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	for(i=0;i<cartCode.length;i++){
		cartname=cartName[i];
		cartcode=cartCode[i];
		cartquantity=cartQuant[i];
		cartorderval=cartCost[i];
		cartid=cartId[i];
		
		value!="" ? value=value+','+cartname+':'+cartcode+':'+cartquantity+':'+cartorderval+':'+cartid : value=cartname+':'+cartcode+':'+cartquantity+':'+cartorderval+':'+cartid;
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
	if(document.cookieform)
		document.cookieform.cartcontents.value=value;
	createCart();
}

function createCookie(name,value,days) {
	purchase=1;
	var oldvalue=readCookie('cart');
	var compcode=':'+code+':';
	if(oldvalue){
		//alert(compcode)
		if(!oldvalue.match(compcode)){
			value=oldvalue+','+value;
		}
		else{
			//alert('you already have that in your shopping cart');
			alreadybought();
			value=oldvalue;
			purchase=0;
		}
	}
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function eraseCart(oBj2,decision) {
	var conftxt='click to delete entire cart?';
	switch(decision){
		case 1:
			document.getElementById('confirmbox').style.display='none';
			var len=cartCode.length;
			var ind=0;
			cartCode.splice(ind,len)
			cartName.splice(ind,len)
			cartQuant.splice(ind,len)
			cartCost.splice(ind,len)
			cartId.splice(ind,len)
			reCreateCookie();
			break;
		case 2:
			document.getElementById('confirmbox').style.display='none';
			break;
		default:
			document.getElementById('confirmbox').innerHTML="<div id=confirmtext>"+conftxt+"</div><div id=confirmbutL onClick=\"eraseCart(this,1)\">Yes</div><div id=confirmbutR onClick=\"eraseCart(this,2)\">No</div>";
			var pos=findPos(oBj2);
			document.getElementById('confirmbox').style.left=(pos[0]-100)+'px';
			document.getElementById('confirmbox').style.top=(pos[1]-100)+'px';
			document.getElementById('confirmbox').style.display='block';
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function resetorderval(){
	var quant=parseInt(document.purchaseform.quantity.value);
	var curprice=parseFloat(document.purchaseform.price.value);
	document.purchaseform.orderval.value=(quant*curprice).toFixed(2);
}

function counter(){
	val=parseInt(document.purchaseform.quantity.value);
	(val==1 && val2==-1) ? val2=0 : val2=val2;
	document.purchaseform.quantity.value=parseInt(val+val2);
	resetorderval();
	t=setTimeout("counter()",500);
}

function doTimer(num){
	num=='inc' ? val2=parseInt(1) : val2=parseInt(-1);
	if (!timer_is_on){
  	timer_is_on=1;
  	counter();
  }
}

function stopCount(){
clearTimeout(t);
timer_is_on=0;
}

function viewcartpro(){
	location='http://www.tamarorganicspro.co.uk/pages/cart.php';
}

function checkout(){
	document.cookieform.submit();
}

function catalogue(){
	location='http://www.tamarorganicspro.co.uk/pages/catalogue.php';
}

function hideMiniCart(){
	document.getElementById('minicart').style.display='none';
}



function showMiniCart(){
	var mycart=readCookie('cart');
	if(mycart){
		totalcartprice=0;
		cartlist="";
		altcartlist="";
		carriage=0;
		discount=0;
		fertquant=0;
		myfruitcarriage=0;
		kgs=0;
		
		purchases.length>(optcart+5) ? altcart=parseInt(purchases.length-optcart) : altcart=0;
		
		for(i=0;i<purchases.length;i++){
			
			prodfeatures=purchases[i].split(':');
			
			cartCode[i]=prodfeatures[1];
			cartQuant[i]=prodfeatures[2];
			cartCost[i]=parseFloat(prodfeatures[3]);
			cartName[i]=prodfeatures[0];
			cartId[i]=prodfeatures[4];
			totalcartprice=totalcartprice+cartCost[i];
			fertdiscounts(cartCode[i],cartQuant[i]);
			fruitcarriage(cartCode[i]);
			potatocarriage(cartCode[i],cartQuant[i]);
			cartlist=cartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartName[i]+'</div><div class=cartItem>'+cartCode[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
			if(altcart && i>=altcart)
			altcartlist=altcartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartName[i]+'</div><div class=cartItem>'+cartCode[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
		}
		calcarriage();
		calcfertdiscount()
		revealCart();
	}	
}

function revealCart(){
	winsize();
	findcenter('width=500 height=50');
	if(discount){
		discountline='<div class=discountrow><div class=cartDiscount>DISCOUNT</div><div class=discountprice>- '+discount.toFixed(2)+'</div></div>';
	}
	if(carriage){
		carriageline='<div class=discountrow><div class=cartDiscount>CARRIAGE</div><div class=discountprice> '+carriage.toFixed(2)+'</div></div>';
	}
	ordervalue=parseFloat(totalcartprice-discount+carriage);
	if(altcart>0){
		document.getElementById('minicart').innerHTML='<div class=cartrowtitle>Your Summarised Shopping Cart<br>(shows last '+(purchases.length-altcart)+' items - see full cart in Checkout One)</div>'+altcartlist+discountline+carriageline+'<div id=cartotal>Total: £ '+ordervalue.toFixed(2)+'</div></div>';
	}
	else{
		document.getElementById('minicart').innerHTML='<div class=cartrowtitle>Your Shopping Cart</div>'+cartlist+discountline+carriageline+'<div id=cartotal>Total: £ '+ordervalue.toFixed(2)+'</div></div>';
	}
	document.getElementById('minicart').style.left=lpos+'px';
	document.getElementById('minicart').style.top=50+'px';
	
	document.getElementById('minicart').style.display='block';
}

function savecart(){
	winsize();
	findcenter('width=400 height=150');
	
	document.getElementById('storecartbox').style.left=lpos+'px';
	document.getElementById('storecartbox').style.top=tpos+'px';
	
	document.getElementById('storecartbox').style.display='block';
}

function sendStoreBox(){
	if(document.storecart.email.value){
		document.storecart.submit()
	}else{
		myalert('please enter value for email')
	}
}

function greyup(a){
	a.style.color='#0E09F5';
	a.style.backgroundColor='#fff';
	window.status='';return true;
}

function greydown(a){
	a.style.color='#373737';
	a.style.backgroundColor='#eee';
}

function closeit(){
	document.getElementById('storecartbox').style.display='none';
}

function retrievecartbox(){
	winsize();
	findcenter('width=400 height=150');
	
	document.getElementById('getcartbox').style.left=lpos+'px';
	document.getElementById('getcartbox').style.top=tpos+'px';
	
	document.getElementById('getcartbox').style.display='block';
}

function sendGetBox(){
	if(document.getcart.getemail.value){
		document.getcart.submit()
	}else{
		myalert('please enter value for email')
	}
}

function closeit2(){
	document.getElementById('getcartbox').style.display='none';
}

function selectcard(cardcode,card){
	document.checkoutform.card.value=card;
	document.checkoutform.cardcode.value=cardcode;
}

function showlist(oBj2,oBj){
	var pos=findPos(oBj2);
	
	document.getElementById(oBj).style.left=(pos[0]+2)+'px';
	document.getElementById(oBj).style.top=(pos[1]+22)+'px';
	document.getElementById(oBj).style.display='block';
}

function hidelist(oBj){
	document.getElementById(oBj).style.display='none';
}

function checkcard(){
	var cardcode = document.checkoutform.cardcode.value;
	var cardnumber = document.checkoutform.cardnumber.value;
	verifyCard(cardcode,cardnumber);
}

function clearit(oBj){
	oBj.value="";
	oBj.style.color='#373737';
}

function mailchecker(){
	document.checkoutform.mail.value==0 ? document.checkoutform.mail.value=1 : document.checkoutform.mail.value=0;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

function check4(oBj){
	if(oBj.value.length<4){
		alert(oBj.value+' Please input year in the format YYYY');
		oBj.value='';
	}
}

function processorder(oBj2,decision){
	var expmon = document.checkoutform.expirymonth.value;
	var expyear = document.checkoutform.expiryear.value
	var startmon = document.checkoutform.startmonth.value;
	var startyear = document.checkoutform.startyear.value
	var marker=0;
	var conftxt='Click on OK to confirm this purchase?';
	switch(decision){
		case 1:
			document.getElementById('confirmbox').style.display='none';
			if(document.checkoutform.lname.value){
					
					for(i=0;i<document.checkoutform.elements.length;i++){
						document.checkoutform.elements[i].style.borderColor='#ccc';
						if(document.checkoutform.elements[i].id=='check' && (document.checkoutform.elements[i].value=="" || document.checkoutform.elements[i].value=="MM" || document.checkoutform.elements[i].value=="YYYY")){
							document.checkoutform.elements[i].style.borderColor='#9E9EFC';
							marker++;
						}
					}
					
					if(marker>0){
						myalert('please complete any fields marked in blue')
					}
					else{
						document.checkoutform.expiry.value=expmon+'/'+expyear;
						document.checkoutform.startdate.value=startmon+'/'+startyear;
						document.checkoutform.confirmorder.value=1;
						document.checkoutform.submit();
					}
			}
			else{
				myalert('please enter some personal details')
			}
			break;
		case 2:
			document.getElementById('confirmbox').style.display='none';
			break;
		default:
			document.getElementById('confirmbox').innerHTML="<div id=confirmtext>"+conftxt+"</div><div id=confirmbutL onClick=\"processorder(this,1)\">Yes</div><div id=confirmbutR onClick=\"processorder(this,2)\">No</div>";
			var pos=findPos(oBj2);
			document.getElementById('confirmbox').style.left=(pos[0]-100)+'px';
			document.getElementById('confirmbox').style.top=(pos[1]-100)+'px';
			document.getElementById('confirmbox').style.display='block';
	}
	
}

function processorderpro(oBj2,decision){
	var expmon = document.checkoutform.expirymonth.value;
	var expyear = document.checkoutform.expiryear.value
	var startmon = document.checkoutform.startmonth.value;
	var startyear = document.checkoutform.startyear.value
	var marker=0;
	
	var conftxt='Click on OK to confirm this purchase?';
	switch(decision){
		case 1:
			document.getElementById('confirmbox').style.display='none';
			if(document.checkoutform.lname.value || document.checkoutform.account.value){
					if(document.checkoutform.account.value){
						document.checkoutform.confirmorder.value=1;
						document.checkoutform.submit();
					}
					else{
						for(i=0;i<document.checkoutform.elements.length;i++){
							document.checkoutform.elements[i].style.borderColor='#ccc';
							if(document.checkoutform.elements[i].id=='check' && (document.checkoutform.elements[i].value=="" || document.checkoutform.elements[i].value=="MM" || document.checkoutform.elements[i].value=="YYYY")){
								document.checkoutform.elements[i].style.borderColor='#9E9EFC';
								marker++;
							}
						}
						
						if(marker>0){
							myalert('please complete any fields marked in blue')
						}
						else{
							document.checkoutform.expiry.value=expmon+'/'+expyear;
							document.checkoutform.startdate.value=startmon+'/'+startyear;
							document.checkoutform.confirmorder.value=1;
							document.checkoutform.submit();
						}
					}
			}
			else{
				myalert('please enter some personal details or an account number')
			}
			break;
		case 2:
			document.getElementById('confirmbox').style.display='none';
			break;
		default:
			document.getElementById('confirmbox').innerHTML="<div id=confirmtext>"+conftxt+"</div><div id=confirmbutL onClick=\"processorder(this,1)\">Yes</div><div id=confirmbutR onClick=\"processorder(this,2)\">No</div>";
			var pos=findPos(oBj2);
			document.getElementById('confirmbox').style.left=(pos[0]-100)+'px';
			document.getElementById('confirmbox').style.top=(pos[1]-100)+'px';
			document.getElementById('confirmbox').style.display='block';
	}
}

function myclientlogin(){
	document.clientlogin.loginemail.value != "" ? document.clientlogin.submit() : myalert('you must enter a valid email address')
}

function hideboxes(){
	if(document.checkoutform.account.value){
		for(i=0;i<document.checkoutform.elements.length;i++){
			if(document.checkoutform.elements[i].name != 'account'){
				document.checkoutform.elements[i].readonly=true;
				document.checkoutform.elements[i].style.backgroundColor='#F4F4F4';
			}
		}
		document.clientlogin.loginemail.readonly=true;
		document.clientlogin.loginemail.style.backgroundColor='#F4F4F4';
	}
	else{
		for(i=0;i<document.checkoutform.elements.length;i++){
			if(document.checkoutform.elements[i].name != 'account'){
				document.checkoutform.elements[i].readonly=false;
				document.checkoutform.elements[i].style.backgroundColor='#ffffff';
			}
		}
		document.clientlogin.loginemail.readonly=false;
		document.clientlogin.loginemail.style.backgroundColor='#ffffff';
	}alert(totalcartprice)
}

function finalcreditcardiscount(){
	if(totalcartprice>100.00){
		
		if(ccd){
			totalcartprice=round2(parseFloat(totalcartprice*0.95))
		}
	}
}

function revealFinalOrder(oid,ccd){
	winsize();
	findcenter('width=500 height=50');
	var ccdstatement="";
	var codes="";
	var quants="";
	var mycart=readCookie('cart');
	var mailcart="";
	if(mycart){
		purchases = mycart.split(',');
		totalcartprice=0;
		cartlist="";
		altcartlist="";
		discount=0;
		carriage=0;
		fertquant=0;
		myfruitcarriage=0;
		kgs=0;
		
		purchases.length>(optcart+5) ? altcart=parseInt(purchases.length-optcart) : altcart=0;
		
		for(i=0;i<purchases.length;i++){
			prodfeatures=purchases[i].split(':');
			cartCode[i]=prodfeatures[1];
			cartQuant[i]=prodfeatures[2];
			cartCost[i]=parseFloat(prodfeatures[3]);
			cartName[i]=prodfeatures[0];
			cartId[i]=prodfeatures[4];
			totalcartprice=totalcartprice+cartCost[i];
			fruitcarriage(cartCode[i]);
			fertdiscounts(cartCode[i],cartQuant[i]);
			potatocarriage(cartCode[i],cartQuant[i]);
			cartlist=cartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartName[i]+'</div><div class=cartItem>'+cartCode[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
			if(altcart && i>=altcart)
				altcartlist=altcartlist+'<div class=cartrow><div class=cartquant>'+cartQuant[i]+'</div><div class=cartItem>'+cartName[i]+'</div><div class=cartItem>'+cartCode[i]+'</div><div class=cartprice>'+cartCost[i].toFixed(2)+'</div></div>';
		}
		calcarriage();
		calcfertdiscount()
		if(totalcartprice>100.00){
			if(ccd==1){
				totalcartprice=round2(parseFloat(totalcartprice*0.95))
				ccdstatement=" incl. credit card discount ";
			}
		}
		ordervalue=parseFloat(totalcartprice-discount+carriage);
	}
	if(discount)
		discountline='<div class=discountrow><div class=cartDiscount>DISCOUNT</div><div class=discountprice>- '+discount.toFixed(2)+'</div></div>';
	if(carriage)
		carriageline='<div class=discountrow><div class=cartDiscount>CARRIAGE</div><div class=discountprice> '+carriage.toFixed(2)+'</div></div>';
	ordervalue=parseFloat(totalcartprice-discount+carriage);
	if(altcart>0){
		finalorder='<div id=finalcarttitle>Thank you for shopping with Tamar Organics, here is a summary of your order number '+oid+'<br>(shows last '+(purchases.length-altcart)+' items - see full cart in confirmation email)<br>You should also receive a confirmation email, if it fails to arrive please let us know.</div>'+altcartlist+discountline+carriageline+'<div id=cartotal>Total'+ccdstatement+': £ '+ordervalue.toFixed(2)+'</div></div>';
	}
	else{
		finalorder='<div id=finalcarttitle>Thank you for shopping with Tamar Organics, here is your order number '+oid+'<br>You should also receive a confirmation email, if it fails to arrive please let us know.</div>'+cartlist+discountline+carriageline+'<div id=cartotal>Total'+ccdstatement+': £ '+ordervalue.toFixed(2)+'</div></div>';
	}
	document.getElementById('minicart').style.left=lpos+'px';
	document.getElementById('minicart').style.top=175+'px';
	document.getElementById('minicart').innerHTML=finalorder;
	document.getElementById('minicart').style.display='block';
}

function killcart(oid){
	ordercodes=oid.split(":");
	oid=ordercodes[0];
	ccd=ordercodes[1];
	revealFinalOrder(oid,ccd);
	eraseCookie('cart');
	eraseCookie('savedcart');
}

function amendetails(oBj2,decision){
	var marker=0;
	var conftxt='click to save these details?';
	switch(decision){
		case 1:
			if(document.checkoutform.lname.value){
					for(i=0;i<10;i++){
							document.checkoutform.elements[i].style.borderColor='#ccc';
							if(document.checkoutform.elements[i].id=='check' && (document.checkoutform.elements[i].value=="")){
								document.checkoutform.elements[i].style.borderColor='#9E9EFC';
								marker++;
							}
					}
					if(marker>0){
						myalert('please complete any fields marked in blue')
					}
					else{
						document.checkoutform.savepersonaldetails.value=1;
						document.checkoutform.submit();
					}
			}
			else{
				myalert('please enter some personal details')
			}
			break;
		case 2:
			document.getElementById('confirmbox').style.display='none';
			break;
		default:
			document.getElementById('confirmbox').innerHTML="<div id=confirmtext>"+conftxt+"</div><div id=confirmbutL onClick=\"amendetails(this,1)\">Yes</div><div id=confirmbutR onClick=\"amendetails(this,2)\">No</div>";
			var pos=findPos(oBj2);
			document.getElementById('confirmbox').style.left=(pos[0]-100)+'px';
			document.getElementById('confirmbox').style.top=(pos[1]-100)+'px';
			document.getElementById('confirmbox').style.display='block';
	}
}

function myemail(){
	document.getElementById('myemailbox').innerHTML=currentemail;
}

function aporem(oBj){
	var apoRegExp = /\'/g;
	oBj.value=oBj.value.replace(apoRegExp," ");
}

function searchveg(){
	if(document.getElementById('tagbox').value!=""){
		location='http://www.tamarorganicspro.co.uk/pages/searchresults.php?tag='+document.getElementById('tagbox').value;
	}
}

function searchlink(status,prod,cat,typename,xprod,tag){
	location='catalogue.php?status='+status+'&prod='+prod+'&cat='+cat+'&typename='+typename+'&xprod='+xprod+'&tag='+tag;
}
