// Online Ordering - client-side automation
// Version 2.03; Created 19-Apr-2005; Latest: 19-Apr-2005
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function WWW_setInnerHTML(id,s) { //HW v0.2.3
	var x=MM_findObj(id);
	if(x!=null) x.innerHTML=s;
}
function WWW_getInnerHTML(id) { //WWW20040401
	var x=MM_findObj(id);
	if(x!=null) return x.innerHTML;
	else return "";
}
function WWW_addInnerHTML(id,s) { //WWW20040401
	var x=MM_findObj(id);
	if(x!=null) { sHTML=x.innerHTML; x.innerHTML=sHTML+s; }
}

function WWW_isValidEmail(e) { //HW v0.7.3.0 14-Nov-2003
	r=/^\w+(.\w+)*@\w+(.\w+)+$/; // start: 1+\w 0+(1.1+\w) 1@ 1+\w 1+(1.1+\w) :end
	return r.test(e);
}

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

document.WWW = new Object();
document.WWW.OrderFormItem = new Array();

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 oSTot=MM_findObj('orderformSubTotal'),aItem=document.WWW.OrderFormItem;
	var oSDel=MM_findObj('orderformDelivery');
	var n=aItem.length,i,total=0,sTotal;
	var qty=0;
	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
		// Find total items
		x=MM_findObj('quantity_'+aItem[i]);
		if (x==null || x.value=="") continue; //can't find or empty, so skip
		qty += parseInt(x.value);
	}
	if (total>0) {
		oSTot.value=total.toFixed(2);//sTotal.substring(0,iPence)+'.'+sTotal.substring(iPence);
	}
	else {
		oSTot.value=0;
	}
}
