// JavaScript Document


<!-- Original:  Mike McGrath   (mike_mcgrath@lineone.net) -->
<!-- Web Site:  http://website.lineone.net/~mike_mcgrath/ -->
<!--

var idarray = new Array();
idarray[0] = new Array("Number of people registering for Summersing", "16.00");
idarray[1] = new Array("Number of people for BBQ Dinner", "16.00")
idarray[2] = new Array("I would like to support the scholarship program with a donation of $", "1.00");



function count(f,n)
{
/*
// This commented code would search by "ID" to find the index of the line array.  Once index was found, it could be used
// to index the line_sum array.  This would have made the order of products and pricing independent of their location in 
// the idarray.  But because the products/prices must be printed in a specific order on the form, we might as well
// use the known index from idarray.

    for(n=0; n < f.line.length; n++)		// loop through "line" array looking for its ID, and determine its index in the line array
    {
		if (f.line[n].id == idname)  break;
    }
*/

  var u = f.cost[n].value;
  f.line_sum[n].value = f.line[n].value * u;
  f.line_sum[n].value = Math.ceil(f.line_sum[n].value * 1000) / 1000;
  f.line_sum[n].value = Math.floor(f.line_sum[n].value * 1000) / 1000;
  f.line_sum[n].value = Math.round(f.line_sum[n].value * 100) / 100;
  if(f.line_sum[n].value == "NaN")
  {
    alert("Error:\nYou may only enter numbers...\nPlease retry");
    f.line[n].value = f.line[n].value.substring(0, f.line[n].value.length-1);
    f.line_sum[n].value = f.line[n].value * u;
    if(f.line_sum[n].value == "0") f.line_sum[n].value = "";
  }
  else
  {
    var gt = 0;
    for(i=0; i < f.line_sum.length; i++)
    {
      gt += Math.ceil(f.line_sum[i].value * 1000) / 1000;
    }
    gt = Math.round(gt * 1000) / 1000;
    f.grand_total.value = "$ " + gt;
    decimal(f);
   }
}

function get_data(f)
{
  var order_data = "This Order is ...\n";
  for(i=0; i < f.line.length; i++)
  {
  	/* alert(f.line[i].id); */
    if (f.line[i].value == "") f.line[i].value = "0";
    order_data += "(" +i+ ") " + f.line[i].id +" = " + f.line[i].value + " Qty, subtotal = " + f.line_sum[i].value + "\n";
  }
  if (f.grand_total.value == "") f.grand_total.value = "Nil";
  order_data += "*** TOTAL ORDER VALUE = " + f.grand_total.value;
  f.order.value = order_data;
}

function decimal(f)
{
  for(i=0; i<f.line_sum.length; i++)
  {
    var d = f.line_sum[i].value.indexOf(".");
    if(d == -1 && f.line[i].value != 0) f.line_sum[i].value += ".00";
    if(d == (f.line_sum[i].value.length-2)) f.line_sum[i].value += "0";
    if(f.line_sum[i].value == "00") f.line_sum[i].value="";
  }
  d = f.grand_total.value.indexOf(".");
  if(d == -1) f.grand_total.value += ".00";
  if(d == (f.grand_total.value.length-2)) f.grand_total.value += "0";
}

function send_data(f)
{
  get_data(f);
  if(f.grand_total.value == "Nil")
  {
    var conf = confirm("No items are entered - \nDo you want to submit a blank order?");
    if(conf)f.submit(); else init();
  }
  else f.submit();							/* will not work if FORM has an <input> of type "submit" */
}

function init()
{

/*** Don't to reset on initialization because if page is re-loaded (ie., the BACK button) values are cleared
	for (i=0; i < document.forms.length; i++) {
		document.forms[i].reset();					// will not work if FORM has an input of type "reset"
	}
***/
  	for (j=0; j < document.Regform.line.length; j++) {
		document.Regform.line[j].id = idarray[j][0];	// set IDs of each line dynamically, not via the ID="" attribute in the <input> tags
		document.Regform.cost[j].value = idarray[j][1];	// initialize dollar amounts dynamically
	}

	count(document.Regform, MandatoryIndex);			// Make sure mandatory fee is added to grand-total
	// document.Regform.firstname.select();
    // document.Regform.firstname.focus();

}


// -->