<!--
//these global arrays are used to store item quantities and cost
//for summing the totals
if(!g_arrayQty)
{
   //The following arrays will cache the values from the form
   //elements, *Qty, *Cost. The index of these 
   //arrays is based on the *Qty elements. That is, the corresponding 
   //elements will use the same index. Examp: if "0-9657985-1-8Qty" 
   //is at index 3 in the form's elements collection, then its value 
   //will be cached at index 3 and the values for "0-9657985-1-8Cost" 
   //and "0-9657985-1-8DiscSchd" will be cached at index 3.

   var g_arrayQty = new Array();
   var g_arrayCost = new Array();
   var g_nSize = 0;

   var g_bShipping = (false);
   var g_bTax = (false);
   var g_bTaxOn = (false);

   //for message construction
   var g_strStart = "";
   var g_strShipping = "";
   var g_strTax = "";

   var g_bInit = (false);

}

//initOrderForm( ) initializes the global arrays and their size.
function initOrderForm( )
{	
   var i;
   var strTempName;	   //an element name
   var strTempCost;	   //the cost value as a string
   var strTempQty;	   //the qty value as a string
   var strISBN;            //the ISBN part of the element name
   var nEnd;               //the index of the last character of the ISBN string
   var strShipping;        //the shipping cost as a string
   var strTax;		   // the tax as a string
   var shippingtemp;       //variable used to calculate the shipping costs
   var taxtemp;		   //variable used to calculate the tax


   g_nSize = document.OrderForm.elements.length;
   
   for(i=0; i<g_nSize; i++)
   {
      strTempName = document.OrderForm.elements[i].name;

      //find the *Qty items
      nEnd = strTempName.indexOf("Qty");
      if(nEnd!=-1 && strTempName!="TotalQty")
      {
         //save the *Qty value
         strTempQty = document.OrderForm.elements[i].value;
         if(strTempQty=='')
         {
            g_arrayQty[i] = 0;
         }
         else
         {
            g_arrayQty[i] = parseInt(strTempQty);
         }

         //get the *Cost item that corresponds with
         //this *Qty item.
         strISBN = strTempName.substring(0,nEnd);
         strTempCost = GetFormObj("OrderForm", strISBN+"Cost").value;
         if(strTempCost.indexOf("$")==-1)
         {
            g_arrayCost[i] = parseFloat(strTempCost);
         }
         else
         {
            g_arrayCost[i] = parseFloat(strTempCost.substr(1));
         }

      }

      //this isn't a *Qty item, so just set defaults
      else
      {
         g_arrayQty[i] = 0;
         g_arrayCost[i] = 0.00;
      }
   }

//   getCookie();

   g_bInit = (true);
}

function OrderForm_Validator()
{
   //validate contact information
   var objForm;
   var strTemp;

   objForm = GetFormObj( "OrderForm", "first_name" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("First Name is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "last_name" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("Last Name is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "address1" );
   if(objForm==null)
   {
      return(false);
   }
   strTemp = objForm.value;

   objForm = null;
   objForm = GetFormObj( "OrderForm", "address2" );
   if(objForm==null)
   {
      return(false);
   }
   strTemp = strTemp + objForm.value;

   if(strTemp=="")
   {
      alert("Address is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "city" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("City is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "state" );
   if(objForm==null)
   {
      return(false);
   }
   strTemp = objForm.selectedIndex;
    
   objForm = null;
   objForm = GetFormObj( "OrderForm", "country" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("Country is required.");
      return(false);
   }

   if(objForm.value=="USA")
   {
      if(strTemp=="0")
      {
         alert("State is required.");
         return(false);
      }
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "zip_code" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("Zip or Post Code is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "email" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("Email address is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "daytime_phone" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("Daytime Phone is required.");
      return(false);
   }

   objForm = null;
   objForm = GetFormObj( "OrderForm", "number_symbol" );
   if(objForm==null)
   {
      return(false);
   }
   if(objForm.value=="")
   {
      alert("Number code is required.");
      return(false);
   }
   if(objForm.value!="33")
   {
      alert("Number code is incorrect.");
      return(false);
   }

   //make sure items were selected
   var objTotalQty;
   
   objTotalQty = GetFormObj( "OrderForm", "TotalQty" );
   if(objTotalQty==null)
   {
      return (false);
   }

   if(objTotalQty.value == 0)
   {
      alert("No items have been selected for purchase.");
      return (false);
   }

//   makeCookie();

   return (true); 
   
}

//SetCosts() calculates the cost for the items ordered and updates the *Cost
//form elements and the TotalCost and TotalQty form elements.
function SetCosts(strItem)
{

   var i;
   var objPrice, objCost, objQty;
   var objTotalQty, objTotalCost, objTotalDue, objShipping, objTax;
   var nQtyIndex, nCostIndex;

   //make sure globals are initialized.
   if( !g_bInit )
   {
      initOrderForm( );
   }

   //get the form objects and indices for the qty and cost items
   nQtyIndex = GetFormElm("OrderForm",strItem+"Qty");
   if( nQtyIndex == -1 )
   {
      return (false);
   }
   objQty = document.OrderForm.elements[nQtyIndex];

   nCostIndex = GetFormElm("OrderForm", strItem + "Cost");
   if( nCostIndex == -1 )
   {
      return (false);
   }
   objCost = document.OrderForm.elements[nCostIndex];

   objPrice = GetFormObj( "OrderForm", strItem + "Price" );
   if(objPrice==null)
   {
      return (false);
   }

   objTotalQty = GetFormObj( "OrderForm", "TotalQty" );
   if(objTotalQty==null)
   {
      return (false);
   } 

   objTotalCost = GetFormObj( "OrderForm", "TotalCost" );
   if(objTotalCost==null)
   {
      return (false);
   }

   objShipping = GetFormObj( "OrderForm", "Shipping" );
   if(objTotalCost==null)
   {
      return (false);
   }

   objTaxOn = GetFormObj( "OrderForm", "TaxOn" );
   if(objTaxOn==null)
   {
      return (false);
   }

   if(objTaxOn.checked)
   {
      objTax = GetFormObj( "OrderForm", "Tax" );
      if(objTax==null)
      {
         return (false);
      }
   }

   objTotalDue = GetFormObj( "OrderForm", "TotalDue" );
   if(objTotalDue==null)
   {
      return (false);
   }
   //make sure quantity is a number
   if(isNaN(objQty.value))
   {
      alert("Quantity must be an integer.");
      objQty.select();
      return (false);
   }

   //make sure the quantity is an integer
   objQty.value = Math.round(objQty.value);

   //calculate the cost	
   var fCost;
   fCost = parseFloat(objPrice.value) * parseInt(objQty.value);

   //save the cost and quanity in the global arrays
   var strQty = objQty.value;
   if(strQty == '')
   {
      g_arrayQty[nQtyIndex] = 0;
   }
   else
   {
      g_arrayQty[nQtyIndex] = parseInt( strQty );
   }
   g_arrayCost[nQtyIndex] = fCost;

   objCost.value = FormatDollars( fCost );

   //calculate the total cost and quantities
   var nQty, taxtemp;
   nQty = GetSum( g_arrayQty, g_nSize );
   fCost = GetSum( g_arrayCost, g_nSize );
   objTotalQty.value = nQty;
   objTotalCost.value = FormatDollars( fCost );

   //calculate the tax if needed
   taxtemp = 0;
   if(objTaxOn.checked)
   {    
      if (objTotalQty.value >= 1)
      {
         taxtemp = 0.0825 * fCost;
      }

      objTax.value = FormatDollars(taxtemp);
   }

   //calculate the shipping
   var shippingtemp = 0;
  
   if (objTotalQty.value > 0) 
   {
      shippingtemp = ((((objTotalQty.value) - 1) * .75) +3);
   }

   objShipping.value = FormatDollars(shippingtemp);

   //calculate the Total Due
   var fTotalDue;
   if(objTaxOn.checked)
   {
      fTotalDue = fCost + shippingtemp + taxtemp;
   }
   else 
   {
      fTotalDue = fCost + shippingtemp;
   }

   objTotalDue.value = FormatDollars(fTotalDue);

   return(true);
}

//CalculateTax() calculates the tax if the tax TaxOn checkbox is checked.
function CalculateTax( )
{
   var fTotalCost=0.0, fTotalDue=0.0, fShipping=0.0, fTax=0.0;
   var objTotalCost, objTotalDue, objShipping, objTax;

   objTaxOn = GetFormObj( "OrderForm", "TaxOn" );
   if(objTaxOn==null)
   {
      return (false);
   }

   objTax = GetFormObj( "OrderForm", "Tax" );
   if(objTax==null)
   {
      return (false);
   }

   objTotalCost = GetFormObj( "OrderForm", "TotalCost" );
   if(objTotalCost==null)
   {
      return (false);
   }

   objShipping = GetFormObj( "OrderForm", "Shipping" );
   if(objTotalCost==null)
   {
      return (false);
   }

   objTotalDue = GetFormObj( "OrderForm", "TotalDue" );
   if(objTotalDue==null)
   {
      return (false);
   }

   var strTemp;
   strTemp = objTotalCost.value;
   if(isNaN(strTemp))
   {
      strTemp = strTemp.substr(1);
   }
   
   if(objTaxOn.checked)
   {
      fTax = parseFloat(strTemp) * 0.0825;
   }
   else
   {
      fTax = 0.00;
   }

   strTemp = objTotalCost.value;
   if(isNaN(strTemp))
   {
     strTemp = strTemp.substr(1);
   }
   fTotalCost = parseFloat(strTemp);

   strTemp = objShipping.value;
   if(isNaN(strTemp))
   {
     strTemp = strTemp.substr(1);
   }
   fShipping = parseFloat(strTemp);

   fTotalDue = fTotalCost + fTax + fShipping;

   objTax.value = FormatDollars(fTax);
   objTotalDue.value = FormatDollars(fTotalDue);

   return (true);
}

//GetFormObj() finds the element named "strName" in the form 
//named "strForm" and returns a reference to the object. If 
//the element is not found, the return value is null.
function GetFormObj( strForm, strName )
{
   var i;
   var objForm;
   var nCount;

   objForm = eval("document." + strForm);
   nCount = objForm.elements.length;

   for(i=0; i<nCount; i++)
   {
      strTempName = objForm.elements[i].name;
      if(strTempName == strName)
      {
         return objForm.elements[i];
      }
   }

   return (null);
}

//GetFormElm() finds the element named "strName" in the form 
//named "strForm" and returns its index. If 
//the element is not found, the return value is -1.
function GetFormElm( strForm, strName )
{
   var i;
   var objForm;
   var nCount;

   objForm = eval("document." + strForm);
   nCount = objForm.elements.length;

   for(i=0; i<nCount; i++)
   {
      strTempName = objForm.elements[i].name;
      if(strTempName == strName)
      {
         return i;
      }
   }

   return -1;
}

//GetSum() sums the values in the array "array".
function GetSum( array, nSize )
{
   var i;
		
   nSum = 0;
	
   for( i=0; i<nSize; i++ )
   {
      nSum += array[i];		
   }
   return nSum;
}

//FormatDollars() takes the float in fNumber and formats it
//into a string as $#.##
function FormatDollars( fNumber )
{
   var strDollars;
   var arrayTemp;
   var fDollars;
   var nLength;
   var strTemp;

   //round number to nearest penny
   fDollars = Math.round(fNumber*100);
   strDollars = fDollars.toString();
   nLength = strDollars.length;
   return "$" + strDollars.substring(0,nLength-2) + "." + strDollars.substr(nLength-2);
}


function orderCancel()
{
   document.location.href = "order-form.asp";
}

function getCookie()
{
alert("getting cookie");
   var strBookOrder;
   var nIndex;
   strBookOrder = "";


alert("cookie = " + document.cookie);

   //if there's a cookie, read it.
   if(document.cookie != "")
   {
      nIndex = document.cookie.indexOf("BookOrder=");
      strBookOrder = document.cookie.substr(57);
alert("strBookOrder = " + strBookOrder);
      document.OrderForm.first_name.value = strBookOrder.split(",")[0];
      document.OrderForm.last_name.value = strBookOrder.split(",")[1];
   }
   return true;
}

function makeCookie(theForm)
{

alert("making a cookie");
   //set the expiration date for the cookie
   expireDate = new Date;
   expireDate.setMonth(expireDate.getMonth()+6);

   //local storage of checkbox states, becomes the value in the cookie.
   var strBookOrder;
   strBookOrder = "Karen" + "," + "Boudreaux";
alert("strBookOrder = " + strBookOrder);

   //set the cookie with the new input value.
   document.cookie = "BookOrder=" + strBookOrder + ";expires=" + expireDate.toGMTString();
   
}


//-->