//  This function displays the nag screen when a field hasn't been filled in.
function nag(form, field, x)
{       
        ufield=field.toUpperCase();
        alert("Oops... You didn't give us your " + ufield + "!" + "\n\nPlease fill in the " + ufield + " field and submit the form again.");
        form.elements[x].focus();
}


//  This function is used to  round the tax amount to the nearest hundreths.
function roundPrice(price)
{
  //  save a copy of the price in case it is an even dollar amount.
  var workPrice = price;

  // Make the price a string by adding a string 0 to the end.
  workPrice  += "0"; 

  // Find out where the decimal point is.
  var pointIndex = workPrice.indexOf(".",0);

// If there is a decimal point now check to see if it needs to be rounded up.
if (pointIndex >= "0") {

  //  Set an index for the thousands digit.
  var thousands = pointIndex +3;

  // if the third number past the decimal point is greater than or 
  // equal to 5, then we need to round up the hundredth digit.
  if (workPrice.charAt(thousands) >= "5") 
  {
   //  Turn the price into a number.
   workPrice=parseFloat(price);
   
   //  Round up the price.
   workPrice=workPrice + .01;

   //  Turn it back into a string.
   workPrice=workPrice + "0";

   //  Cut off the thousands on down.
     rPrice = workPrice.substring(0,thousands);

   } else {

   //  We didn't need to round the price up so cut off the
   //  thousands on down and return the price
   rPrice = workPrice.substring(0,thousands);
   }

//  Its an even dollar amount so just put on the .00 on the end.
} else {
  rPrice = price + ".00";
}

 // Now return the rounded price.
 return rPrice;
}


//  This function calculates the form.
function updatePrice()
{
   //  Reset the subtotal price
   var addPrice = 0;

   //  Reset the running total prices.
   var nowPrice = 0;

   //  Reset the nubmer of part ordered.
   var partcount = 0;

   //  Step through each element in the form.
   for (i = 0; i < parseInt(self.document.forms[0].elements.length); i++)
   {

      //  If the form element has "qty" in the name then we need to process it.
      if (self.document.forms[0].elements[i].name.substring(0,3) == 'qty')
      {

         // If the item has a quantity of not 0, then we need to process it.
         if (self.document.forms[0].elements[i].value != 0)
         {

			// Locate the cost
			costIndex = i + 1;

            //  Increment the part counter.
            partcount++;

            //  Get the actual value for the quantity.
            nowQty = eval(self.document.forms[0].elements[i].value);

            //  Get the cost of the item.
            nowPrice = eval(self.document.forms[0].elements[costIndex].value);

            //  Calculate the extended cost (i.e., quanty * cost).
            nowPrice = eval(nowPrice * nowQty);

	    //  Add to the subtotal.
            addPrice += nowPrice;
         }  
      }
   }

   //  Round off the subTotal price.
   subTotal=roundPrice(addPrice);

   //  Put the sub total price into the form.
   self.document.forms[0].subtot.value = subTotal;

   // Figure the tax.
   tax = parseFloat(self.document.forms[0].taxrate.value * addPrice);

   //  Round off the tax price.
   totalTax = roundPrice(tax);

   //  Put the total tax into the form.
   self.document.forms[0].totaltax.value = totalTax;


   // Start figuring the total including tax.
   //  Turn the price string into a number.
   subPrice =  parseFloat(addPrice);

   // Turn the tax string into a number.
   addTax = parseFloat(totalTax);

   //  Add the tax and subtotal to get the total price.
   totalPrice = (addTax + subPrice);

   //  Round the total price.
   finalPrice = roundPrice(totalPrice);

   //  Update the form with the total cost.
   self.document.forms[0].cost.value = finalPrice;

   //  Update the form with the number of line items.
   self.document.forms[0].items.value = partcount;
}

function orderIt(form)
{
  //  If the order is zero, display a message.
  if (form.cost.value == "0.00")
  {
      alert("You have not ordered anything.  Please go back to the top of the form, pick the item(s) you want, and type in a quantity other than zero.")
  } else {

    if (form.elements[176].value == "")
        {
                nag(form, form.elements[176].name,176);
        } else if (form.elements[177].value == "")
        {
                nag(form, form.elements[177].name, 177);
        } else if (form.elements[178].value == "")
        {
                nag(form, form.elements[178].name, 178);
        } else if (form.elements[179].value == "")
        {
        nag(form, form.elements[179].name, 179);
        } else if (form.elements[180].value == "")
        {
        nag(form, form.elements[180].name, 180);
        } else if (form.elements[182].value == "")
        {
        nag(form, form.elements[182].name, 182);

     } else {
   var message = "You are about to order the following items:\n";
   message = message + "Qty\tItem Num\tDescription\n";

   // Step through each element in the form.
   for (i = 0; i < parseInt(self.document.forms[0].elements.length); i++)
   {

      //  If the form element has "qty" in the name then we need to process it.
      if (self.document.forms[0].elements[i].name.substring(0,3) == 'qty' && self.document.forms[0].elements[i].value !=0)
      {

         //  Get the quantity.
         qtyItem = self.document.forms[0].elements[i].value; 

		 partItem = self.document.forms[0].elements[i-2].value;
   
         //  Get the description.
         descItem = self.document.forms[0].elements[i-1].value;

         //  add the line item to the confirmation message.
         message = message + qtyItem + "\t" + partItem + "\t" + descItem + "\n";
      }
      
   }

   message = message + "\nTotal of Items: $" + self.document.forms[0].cost.value
	if (confirm(message)) 
	{ 
	self.document.forms[0].submit();
	}
      }
   }
}


