//-------------------------------------------------------------------------------------------------
//	DynForm.js - Javascript for Dynamic form processing
//-------------------------------------------------------------------------------------------------

function noEntry() {		//do not allow field entry
  return false;
}

function numberOnly(e) {
var keycode;
	if (window.event)  
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	return ( keycode >= 48 && keycode <= 57);
}

function parseDecimal(d, zeros, trunc) {
//d=d.replace(/[^\d\.]/g,"");
d=d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g,"");
while (d.indexOf(".") != d.lastIndexOf("."))
	d=d.replace(/\./,"");
if (typeof zeros == 'undefined' || zeros == "") {
	return parseFloat(d);
	}
else {
	var mult = Math.pow(10,zeros);
	if (typeof trunc == 'undefined' || (trunc) == false)
		return parseFloat(Math.round(d*mult)/mult);
	else
		return parseFloat(Math.floor(d*mult)/mult);
	}
}

function FormCalc(p) {		// P is the 'this' part of a form item!
var i 		= 0;
var n 		= 0;
var	sum 	= 0.0;
var	optTotal= 0.0;
var id 		= p.id;

	if (eval("document.form.Qty_" + id) != null) {
		optQty = parseInt	(eval("document.form.Qty_" + id + ".value"));
		}
	else {
		optQty = 0;
		eval("document.form.Qty_" + id + ".value =  optQty");		
		}
			
	if (eval("document.form.Price_" + id) != null) {
		optCost = parseDecimal(eval("document.form.Price_" + id + ".value"), 2);
		if (optCost == "NaN") {
			optCost = 0.0;
			alert("Price_" + id + " invalid");
			eval("document.form.Price_" + id + ".value = 0.0")
			}
		}
	else {
		optCost = 0.0;
		alert("Price_" + id + " invalid");
		}	

	optTotal = optQty * optCost;
	if (optTotal == 'NaN')
		alert("NaN");
	eval("document.form.Total_" + id + ".value =  optTotal.toFixed(2)");	

	if (document.all || document.getElementById) {
		for (i=0; i<document.form.length; i++) {
			var tempobj=form.elements[i];
			if(tempobj.name.substring(0,6)=="Total_") {
				if (tempobj.value.length > 0) {
					if (tempobj.value > 0) {
						n = n + 1;
						sum = sum + parseDecimal(tempobj.value, 2);
						}
				}
			}
		}
	}
//alert("sum="+sum);
	var ProcessFee = parseDecimal(document.form.ProcessFee.value, 2);
//alert("ProcessFee="+ProcessFee);
	var feeamt = ProcessFee * n;
//alert("feeamt="+feeamt);
	document.form.feeamt.value = feeamt.toFixed(2);

	var TotalAmt = sum + feeamt;
//alert("TotalAmt="+TotalAmt);
	document.form.TotalAmt.value = TotalAmt.toFixed(2);
}

