// Online Ordering - client-side automation
// Version 2.03; Created 19-Apr-2005; Latest: 19-Apr-2005

document.WWW.OrderFormItem = new Array();

function fixDP(i,nDP)
{
	var div=Math.pow(10,nDP);
	i=Math.round(i * div) / div;
	return i;
}

function orderformCalculateSubtotal(x,price)
{ //x contains name of item, used for finding quantity & subtotal fields
	var oQty=MM_findObj('quantity_'+x),oST=MM_findObj('subtotal_'+x);
	var subtotal,sST,iPence;
	var r=/^\d+$/; //all digits
	if (!r.test(oQty.value)) oQty.value="";
	else if (oQty.value<=0) oQty.value="";
	if (oQty.value!=""){
		subtotal=parseInt(oQty.value)*price;
		sST = subtotal/100;
		oST.value = sST.toFixed(2);
	}
	else oST.value="";
	orderformCalculateTotal();
}

function orderformCalculateTotal()
{
	var oTot=MM_findObj('orderformTotal'),oSTot=MM_findObj('orderformSubTotal'),aItem=document.WWW.OrderFormItem;
	var oDel=MM_findObj('delivery'), oSDel=MM_findObj('orderformDelivery');
	var n=aItem.length,i,total=0,sTotal,iTotQty=0,iTotDel;
	for(i=0;i<n;++i){
		x=MM_findObj('subtotal_'+aItem[i]);
		if (x==null || x.value=="") continue; //can't find or empty, so skip
		total += parseFloat(x.value); //convert to pence and add
		
		y=MM_findObj('quantity_'+aItem[i]);
		iTotQty += parseFloat(y.value);
		
		//Shipping costs
		s=MM_findObj('shipping_'+aItem[i]);
		s2=MM_findObj('shipping2_'+aItem[i]);
		s.value=oDel.value;
		s2.value=oDel.value;
	}
	iTotDel = oDel.value * iTotQty;
	oSDel.value = iTotDel.toFixed(2);
	if (total>0) {
		oSTot.value=total.toFixed(2);//sTotal.substring(0,iPence)+'.'+sTotal.substring(iPence);
	}
	total += parseFloat(oSDel.value);
	if (total>0) {
		oTot.value=total.toFixed(2);//sTotal.substring(0,iPence)+'.'+sTotal.substring(iPence);
	}
	else oTot.value="";
}
