// to use
// 1. include this line: 
//	<script language="javascript" type="text/javascript" SRC="../../_js/money-sum.js"></script>
// 2. create a price list like so (note 'QUANTITY' in the front of every name):
// 	<script language="javascript">
//		this.prices = new Array
//		(
//		new Array("QUANTITY ALL-Myself with spouse-160", 160.00),
//		...
//		new Array("QUANTITY Additional_Banquet_Tickets-30", 30.00)
//		);
//	</script>
// 3. make your form like this:
//	<form onSubmit="return sumAll();" METHOD="post" ACTION="/send_mail.php">
//	<input type="hidden" name="recipient" value="--fill in the blank--@mail.ctsfw.edu">
//	<input type="hidden" name="subject" value="Online Registration Form For --fill in the blank--">
// 4. create your textfields to match names in the prices array
//	<input type="text" name="QUANTITY ALL-Myself with spouse-160" value="0" onchange="this.form.total.value=checkChoice(this);">
// 5. add total and hiddentotal fields:
//	<input type="hidden" name="hiddentotal" value="0">
//      <input type="text" name="total" value="" readonly="READONLY" onblur="this.value = formatCurrency(hiddentotal.value);">
var load_page=true;

function onLoadPageForm(myform)
{
	var whichbox;
	var val=0.00;
	for (i = 0; i < myform.elements.length; i++)
	{
		whichbox = myform.elements[i];
		if (whichbox.name.substring(0, 8) == "QUANTITY")
		{
			val=0.00;
			for (j = 0; j < prices.length; j++)
			{
				if (prices[j][0] == whichbox.name)
				{
					val = prices[j][1];
					break;
				}
			}

			if (!whichbox.price)
			{
				whichbox.price = val;
				whichbox.priorval = 0;
			}

		}
	}
}

function sumAll(myform)
{
	var whichbox;
	var retval=true;

	for (i = 0; i < myform.elements.length; i++)
	{
		whichbox = myform.elements[i];
		if (whichbox.name.substring(0, 8) == "QUANTITY")
		{
			myform.total.value=checkChoice(whichbox);
		}
	}
	return retval;
}

function checkChoice(whichbox) {
	with (whichbox.form) 
	{
	//Ensure negative or non-numeric values were not entered:
	//Otherwise, set field back to prior value.
	if (isNaN(whichbox.value)) 
		{ 
		whichbox.value=whichbox.priorval;
		whichbox.focus();
		}
	whichbox.value=Math.abs(whichbox.value);
	//Ensure a decimal place was not keyed, otherwise set field back to prior value,
	// show error and leave function while passing current (unchanged) total value:
	var dec = whichbox.value.indexOf('.', 1)
	if (dec > 0)
		{ 
		alert('No decimal places allowed for \"' +whichbox.name +'\" !');
		whichbox.value=whichbox.priorval;
		whichbox.focus();
		}

	//First, back out the prior qty from the total:
	hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.price * whichbox.priorval);

	//Then, save the current qty:
	whichbox.priorval=whichbox.value;

	//Now, apply the current qty to the total:
	hiddentotal.value = eval(hiddentotal.value) + eval(whichbox.price * whichbox.value);
	return(formatCurrency(hiddentotal.value));
	}
}

function formatCurrency(num) {
	<!-- Function courtesy of:  Cyanide_7 (leo7278@hotmail.com) -->
	<!-- Web Site:  http://www7.ewebcity.com/cyanide7 -->
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return ("$" + num + "." + cents);
}
