function validateOnSubmit(theForm) {
	var elem;
	var errs=0;
	// execute all element validations in reverse order, so focus gets
	// set to the first one in error.

	//if (!validateSelected (theForm.duration, 'duration_error')) errs += 1;
	//if (!validatePresent (theForm.position, 'position_error')) errs += 1;
	if (!validateRadiogroup (theForm.Payment_method, 'payment_error')) errs += 1;
	if (!validatePresent(theForm.Phone, 'phone_error', true)) errs += 1;
	if (!validatePresent(theForm.Delivery_address, 'delivery_error', true)) errs += 1;
	if (!validateEmail(theForm.email, 'email_error', true)) errs += 1;
	if (!validateRadiogroup (theForm.Delivery_island, 'island_error')) errs += 1; <!-- field IDs, NOT names -->
	if (!validatePresent(theForm.Name, 'name_error')) errs += 1;
	
	if (errs>=1)  alert('There are fields which need correction before sending');

	return (errs==0);
	};


var vitroclean500Price = 12.95;
var vitroclean2lPrice = 33.95;
var vitroclean5lPrice = 76.50;
var silverLadyCleanerPrice = 3.75;

var ruralDeliveryPrice = 5;
var islandPrice = new Array(6, 9.50);	//north island, south island

var freightAmt = 0;
var totalAmt = 0;
var price;

function Total() {
        
        totalAmt = 0;
        
        var vitroclean500Qty = document.form.Vitroclean_500ml_qty.value;
        var vitroclean2lQty = document.form.Vitroclean_2l_qty.value;
        var vitroclean5lQty = document.form.Vitroclean_5l_qty.value;
        var silverLadyCleanerQty = document.form.Silver_Lady_Cleaner_qty.value;
        
        price = Currency(vitroclean500Qty * vitroclean500Price);
	vitroclean500Qty > 0 ? document.form.Vitroclean_500ml.value = " QTY: " + vitroclean500Qty + "  PRICE: " + price : document.form.Vitroclean_500ml.value = "";
	document.getElementById('Vitroclean_500ml_price').innerHTML = price;
	totalAmt += vitroclean500Qty * vitroclean500Price;
	
	price = Currency(vitroclean2lQty * vitroclean2lPrice);
	vitroclean2lQty > 0 ? document.form.Vitroclean_2l.value = " QTY: " + vitroclean2lQty + "  PRICE: " + price : document.form.Vitroclean_2l.value = "";
	document.getElementById('Vitroclean_2l_price').innerHTML = price;
	totalAmt += vitroclean2lQty * vitroclean2lPrice;
	
	price = Currency(vitroclean5lQty * vitroclean5lPrice);
	vitroclean5lQty > 0 ? document.form.Vitroclean_5l.value = " QTY: " + vitroclean5lQty + "  PRICE: " + price : document.form.Vitroclean_5l.value = "";
	document.getElementById('Vitroclean_5l_price').innerHTML = price;
	totalAmt += vitroclean5lQty * vitroclean5lPrice;
	
	price = Currency(silverLadyCleanerQty * silverLadyCleanerPrice);
	silverLadyCleanerQty > 0 ? document.form.Silver_Lady_Cleaner.value = " QTY: " + silverLadyCleanerQty + "  PRICE: " + price : document.form.Silver_Lady_Cleaner.value = "";
	document.getElementById('Silver_Lady_Cleaner_price').innerHTML = price;
	totalAmt += silverLadyCleanerQty * silverLadyCleanerPrice;
	
	CalculateTotalAmount();
	}


function CalculateFreight() {
	freightAmt = 0;
	
	for (i = 0; i < 2; i++) {
		if (document.form.Delivery_island[i].checked)
			freightAmt = islandPrice[i];
    		}
    	
    	if(document.form.Rural.checked)
		freightAmt += ruralDeliveryPrice;
    	
	freightAmt > 0 ? document.form.Freight_amount.value = Currency(freightAmt) : document.form.Freight_amount.value = "";
	
	document.getElementById('displayfreight').innerHTML = document.form.Freight_amount.value;
	
	CalculateTotalAmount();
	}

function CalculateTotalAmount() {
	document.getElementById('displaytotal').innerHTML = Currency(totalAmt + freightAmt);
	
	document.form.Subtotal_amount.value= Currency(totalAmt);
	document.form.Total_amount.value = Currency(totalAmt + freightAmt);
	}

function Currency(anynum) {
        //returns number as string in $xxx,xxx.xx format.
        anynum = "" + eval(anynum);  //evaluate (in case an expression sent)
        intnum = parseInt(anynum);  //isolate integer portion
        intnum = Math.abs(intnum);
        intstr = ""+intnum;
        
        //add comma in thousands place.
        if (intnum >= 1000) {
                intlen = intstr.length;
                temp1=parseInt(""+(intnum/1000));
                temp2=intstr.substring(intlen-3,intlen);
                intstr = temp1+","+temp2;
        	}
        if (intnum >= 1000000) {
                intlen = intstr.length;
                temp1=parseInt(""+(intnum/1000000));
                temp2=intstr.substring(intlen-7,intlen);
                intstr = temp1+","+temp2;
        	}

        decnum = Math.abs(parseFloat(anynum)-parseInt(anynum)); //isolate decimal portion
        decnum = decnum * 100; // multiply decimal portion by 100.
        decstr = "" + Math.abs(Math.round(decnum));
        if (decstr.length>2) {decstr=decstr.substring(0,2)}
        while (decstr.length < 2) {decstr="0"+decstr}
        retval = intstr + "." + decstr;
        if (anynum < 0) {
                retval="("+retval+")";
        	}
        return "$"+retval;
	}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;

	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
	}