	function updateCosts(showVatMsg) 
	{
		// initialise to the total displayed in basket's table
		totalCost = Number(document.frmShipping.GRANDTOTAL.value);
		
		// add the shipping costs to the Grand Total if order > 420
		document.frmShipping.shippingCost.value = 0;
		if (document.frmShipping.shipping[0].checked)
		{
			if (totalCost < 420) 
			{
				totalCost += Number(document.frmShipping.shipping[0].value);
				document.frmShipping.shippingCost.value = Number(document.frmShipping.shipping[0].value);
			}
		}
		else
		{
			totalCost += Number(document.frmShipping.shipping[1].value);
			document.frmShipping.shippingCost.value = Number(document.frmShipping.shipping[1].value);
		}
		
		document.frmShipping.subtotal.value = formatAsMoney(totalCost);
		
		// if Country is SA, add VAT else VAT is 0
		VAT = 0;
		if (document.frmShipping.country.value == "ZA") 
		{
			if (showVatMsg)
				alert('Please note that VAT will now be added.\nShipping shall be via SpeedServices overnight counter-to-counter\nwhich requires collection from your local post office.');
				
		    VAT = totalCost * 14/100;
		}
		  
		document.frmShipping.vat.value = formatAsMoney(VAT);
		document.frmShipping.total_cost.value = formatAsMoney(totalCost+VAT);
		document.frmShipping.AM.value = (formatAsMoney(totalCost+VAT)*100);
	}
	
	function formatAsMoney(mnt) 
	{
   	 	mnt -= 0;
    	mnt = (Math.round(mnt*100))/100;
    	return (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
	}

function 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=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
  
  if (document.frmShipping.confirm_email_address.value != document.frmShipping.email_address.value)
  {
  	errors += "The email address and the confirmed email address must be the same";
  }	
  
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.returnValue = (errors == '');
}

	function isNumeric(val)
	{ 
		return(parseFloat(val,10)==(val*1));
	}
	
	function checkNull(value) 
	{
		if (value == "") 
		{
			value = "0";
		}

		return value;
	}
	
	function calculate() 
	{
		// determine number of products to calculate
		totalCost = 0;
		discountVal = 0;
		
		// initialise the controls
		document.frmShop.subtotal.value = formatAsMoney(totalCost);
		document.frmShop.AM.value = 0;
		
		numProducts = new Number(document.frmShop.prodcount.value);
		for (i=1;i<=numProducts;i++) 
		{
			// get the product name, code, price and qty controls
			productname=eval('document.frmShop.proddesc'+i).value;
			productcode=eval('document.frmShop.prodcode'+i).value;
			price=eval('document.frmShop.price'+i).value;
			qtyinput = new String(eval('document.frmShop.qtyinput'+i).value);
			inputs = qtyinput.split(",");
			
			totalqty = 0;
			for (inputidx=0; inputidx<inputs.length;inputidx++) 
			{
				qtyBox = eval('document.frmShop.'+inputs[inputidx]);
				if (typeof(qtyBox) != "undefined") 
				{
					qtyVal = qtyBox.value;
					qtyVal = checkNull(qtyVal);
					
					// check the value in the input box IS a number
					if (!isNumeric(qtyVal))
					{
						qtyBox.focus();
						alert('Quantities entered must be a numeric value.\n"'+qtyVal + '" is not a numeric value.\nCalculation has been cancelled.');
						return;
					}
					
					// check the value is greater than zero
					if (qtyVal < 0) 
					{
						qtyBox.focus();
						alert('Quantities entered must be a greater than zero i.e. No negative values or zero are accepted.');
						return;
					}
					
					// determine if can have a discount
					totalqty +=Number(qtyVal);
					totalCost += price*qtyVal;
				}
			}
			
			if (i == 1)
			{
				discountVal += getDiscount(price, totalqty);
			}					

		}
		
		document.frmShop.subtotal.value = formatAsMoney(totalCost);
		document.frmShop.discount.value = formatAsMoney(discountVal);
		document.frmShop.total.value = formatAsMoney(totalCost-discountVal);
		document.frmShop.AM.value = document.frmShop.total.value*100;
	}
	
	function getDiscount(price, qty) 
	{
		//x.amount.value = 0;
		//x.subtot.value = 0;
		
		var n = 0;
		var m = 0;
		var b,c,d;
		d = true;
	
		b = Number(price);
		c = Number(qty);
		
		if(c < 0) 
		{
			d = false;
			c = 0;
			qty = c;
		}
			
		m+=c;
		if(m==3) 
		{
			n += m*b*(1-1/1.1);
		} 
		
		if(m>3) 
		{
			n += m*b*(1-1/1.15);
		} 
		
		return Number(n);
	}