var products = new Array(); // hold product codes for use in doTotals
  products[0] = "s_88x31";
  products[1] = "s_234x60";
  products[2] = "s_120x60";
  products[3] = "s_120x90";
  products[4] = "s_100x100";
  products[5] = "s_120x120";
  products[6] = "s_125x125";
  products[7] = "s_468x60";
  products[8] = "s_120x240";
  products[9] = "s_160x240";
  products[10] = "s_728x90";
  products[11] = "s_120x600";
  products[12] = "s_160x600";
  
  products[13] = "s_gold";
  products[14] = "s_platinum";
  products[15] = "s_diamond";
  products[16] = "s_diamondpro";
  products[17] = "s_1000x145";
  
  products[18] = "s_250x250";
  products[19] = "s_300x250";
  products[20] = "s_336x280";

function getProductTotal(field, form) {
	if (field.value=="") field.value=0;
	if ( !isPosInt(form, field, field.value) ) return;
	else {
		var product = field.name.slice(0, field.name.lastIndexOf("_") ); 
        var price = form[product + "_price"].value;
		var buypsd = form[product + "_psd"];
		var cntext = form[product + "_cnt"];
		var amt = field.value * price;
		var extraadd = field.value * 5 * cntext.value;
		//alert(extraadd);
		
		form[product + "_tot"].value= formatDecimal(amt);
		setRush(form);
	}
}

function doTotals(form) {
	var sub_tot_amt=0, tax_amt=0, g_tot_amt=0;
		for (var i=0; i < 21; i++) {
			var cur_field = form[ products[i] + "_qty" ]; 
			
			var cur_field3 = form[ products[i] + "_cnt" ];
			if ( !isPosInt(form, cur_field, cur_field.value) ) return; 
			else
			{
				sub_tot_amt += parseFloat(cur_field.value) * parseFloat( form[ products[i] + "_price" ].value );
				
			}
    }
		if ( form.sales_tax && form.sales_tax.checked ) {
			tax_amt = 0 * sub_tot_amt;
			form.tax_amt.value = formatDecimal(tax_amt);
		}
		if (sub_tot_amt==0) g_tot_amt=0;
		else g_tot_amt = sub_tot_amt + tax_amt + 0;
		form.grand_tot.value = formatDecimal(g_tot_amt);
		tem2 = g_tot_amt*3.006;
		form.grand_totRM.value = formatDecimal(tem2, 2);
}

function inspectOptions(btn, field, form) {
	field.value = formatDecimal(btn.value);
	if (form.sub_tot.value > 0) doTotals(form);
}

function doSalesTax(field,form) {
	if (field.checked) 
		form.tax_amt.value = formatDecimal( 
      0 * form.sub_tot.value );
	else form.tax_amt.value = 0;
	if (form.sub_tot.value > 0) doTotals(form);
}

function finalCheck(form) {
	// final check of quantity entries' validity
	for (var i=0; i < 9; i++) {
		var cur_field = form[ products[i] + "_qty" ]; 
		if ( !isPosInt(form, cur_field, cur_field.value) ) return;
	}
	// check if a quantity entered
	if (form.grand_tot.value == 0) {
		alert("You haven't ordered anything.");




		return false;
	} else {
		if ( !isValidEmail(form, form.email, form.email.value) ) return;
	  form.submit();
	}
}

function checkValue(field) {
  if (field.value == 0) field.value = "";
}

function reCheckValue(field) {
  if (field.value == "") field.value = 0;
}

// ie needs delay
function setFocus(fld) { fld.focus();	fld.select(); }

function isPosInt(frm,fld,val) {
	var re = /^\d+$/
	if ( !re.test(val) ) {
		alert("Please enter whole numbers only.");
		if ( document.forms[frm.name] ) { // protect ns4 in case of nesting
    	setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])", 100);
    } else {
      fld.focus(); fld.select();
    }
		return false;
	} else return true;
}

function isValidEmail(frm,fld,entry) {
	var re = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
	if (!re.test(entry)) {
		alert("The email address is not valid.")
		if ( document.forms[frm.name] ) { // protect ns4 in case of nesting
    	setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);
    } else {
      fld.focus(); fld.select();
    }
		return false;
	} else return true;
}

// format val to n number of decimal places
// modified version of Danny Goodman's (JS Bible)
function formatDecimal(val, n) {
  n = n || 2;
  var str = "" + Math.round ( parseFloat(val) * Math.pow(10, n) );
  while (str.length <= n) str = "0" + str;
  var pt = str.length - n;
  return str.slice(0,pt) + "." + str.slice(pt);
}

function setRush(form)
{
	doTotals(form);
	if (form.rush_delivery.checked == true)
	{
		var newTotal = form.grand_tot.value * 1.4;
		newTotal = formatDecimal(newTotal, 2);
		newTotaltem2 = newTotal * 3.6;
		
		form.grand_tot.value = newTotal;
		form.grand_totRM.value = formatDecimal(newTotaltem2, 2);
	}
	if (form.psd_delivery.checked == true)
	{
		var newTotal2 = form.grand_tot.value;
		
		var quantity=0;
		for (var i=0; i < 21; i++) {
			var cur_field = form[ products[i] + "_qty" ]; 
			var cnttext = form[ products[i] + "_cnt" ];
			var b = cnttext.value;
			//alert("hehe");
			//alert(b);
			quantity += Number(cur_field.value)*Number(b);		
     	}
		var psd_price = quantity *5;
		newTotal2 = Number(newTotal2) + psd_price;	
		newTotal2 = formatDecimal(newTotal2, 2);
		newTotal2tem2 = newTotal2 * 3.006;
		form.grand_tot.value = newTotal2;
		form.grand_totRM.value  = formatDecimal(newTotal2tem2, 2);
	}
	return 1;
}
