function validatePaymentForm()
{
   //------------------------------------------------------------------- 
   // Validates the payment information form for null values and email address 
   // format. Alert the user if the email address is invalid.
   //    returns true if the payment form validates, else false.
   //-------------------------------------------------------------------

   var objForm = document.shoppingCart;
   var errorArray = new Array ("none","none","none","none","none","none","none","none","none","none","none","none","none");

   if (!isEmptyFields (objForm.billname))
       errorArray[0] = "block";
   if (!isEmptyFields (objForm.billaddress1))
       errorArray[1] = "block";
   if (!isEmptyFields (objForm.billcity))
       errorArray[2] = "block";

   if (!isZipCode(objForm.billzip.value))
       errorArray[3] = "block";
   if (!isPhone(objForm.billphone))
       errorArray[4] = "block";

   if (!isValidEmail(objForm.clientemail.value))
       errorArray[5] = "block";

   if (!isEmptyFields (objForm.shiptoname))
       errorArray[6] = "block";
   if (!isEmptyFields (objForm.shiptoaddress1))
       errorArray[7] = "block";
   if (!isEmptyFields (objForm.shiptocity))
       errorArray[8] = "block";
   if (!isZipCode(objForm.shiptozip.value))
       errorArray[9] = "block";
   if (!isPhone(objForm.shiptophone))
       errorArray[10] = "block";

   if (!isDigit(objForm.ccnum.value))
       errorArray[11] = "block";
   if (!isExpirationDate(objForm.ccexp.value))
       errorArray[12] = "block";

   var str = errorArray.join("");
   if (str.indexOf('block') != -1)
       return displayErrorBox (errorArray);

   return true;
}

function toggleCheckedField (isCheckedBill)
{
   //-------------------------------------------------------------------
   // Display (or hide) form fields when user toggles the checkbox.
   // If the checkbox is checked, the form field collapses and hides the other form fields.
   //-------------------------------------------------------------------

   var objForm = document.shoppingCart;

   if (isCheckedBill)
   {
       // We need to check if the fields have been filled before transfering the values.

       objForm.shiptoname.value = objForm.billname.value;
       objForm.cardname.value = objForm.billname.value;
       objForm.shiptoaddress1.value = objForm.billaddress1.value;
       objForm.shiptoaddress2.value = objForm.billaddress2.value;
       objForm.shiptocity.value = objForm.billcity.value;

       var listIndex = objForm.billstate.selectedIndex;
       objForm.shiptostate.options[listIndex].selected = true;

       objForm.shiptozip.value = objForm.billzip.value;
       objForm.shiptophone.value = objForm.billphone.value;
   }

   return true;
}

function isPhone(object)
{
   //-------------------------------------------------------------------
   // We need to verify if the phone number that has been entered is valid or not.
   // Alert the user if an invalid phone number has been entered.
   //    returns true if the phone field validates, else false.
   //-------------------------------------------------------------------

   var num = object.value;
   num = num.replace (/[-()]/g, "");

   if ((isNaN(num)) || (num.length == 0))
       return false;

   return true;
}

function isDigit(num)
{
   //-------------------------------------------------------------------
   //    returns true if value is a number
   //-------------------------------------------------------------------

   // We now need to test if the first digit is 1-9 and the rest digits.
   var regNum = /^\d+$/;
   if (regNum.test(num))
       return true;

   return false;
}

function isExpirationDate(num)
{
   //-------------------------------------------------------------------
   // We need to verify if the expiration date that has been entered is valid or not.
   // Alert the user if an invalid expiration date has been entered.
   //    returns true if the expiration date field validates, else false.
   //-------------------------------------------------------------------

   var regNum = /^[01][\d]0[4-9]$/;
   if (regNum.test(num))
   {
       // We also need to verify if the date entered is already exipired.
       var now = new Date();
       var mm = leadingZero (now.getMonth() + 1);
       var yy = leadingZero (now.getFullYear() - 2000);
       var currdate = mm + yy;

       var expired_month = num.substr(0,2);
       var expired_year = num.substr(2,2);

       if (expired_month > mm)
           return true;
       else if (expired_year > yy)
           return true;
       else if (num == currdate)
           return true;
   }

   return false;
}

function validateQty()
{
   //-------------------------------------------------------------------
   // We need to verify if the quantity of a particular item has been entered valid. 
   // Alert the user if an invalid quantity on a particular item has been entered.
   //    returns true if the quantity field validates, else false.
   //-------------------------------------------------------------------

   var objForm = document.shoppingCart;
 
   for (var i=1; i < objForm.length; i+=5)

       if ((objForm.elements[i].type == "text") && (objForm.elements[i].name  == "qty[]"))
       {
           if (!isDigit(objForm.elements[i].value))
           {
               alert ('You have entered an invalid quantity value.\n Please enter a valid number.');
               objForm.elements[i].value = "";
               objForm.elements[i].focus();
               return false;
           }
           else
           {
               if (parseInt(objForm.elements[i].value) > parseInt(objForm.elements[i+3].value))
               {
                   alert ('You have exceeded the maximum quantity for this item. ');
                   objForm.elements[i].value = "";
                   objForm.elements[i].focus();
                   return false;
               }
           }
       }

   return true;
}

function validateQuantity(quota)
{
   //-------------------------------------------------------------------
   // We need to verify if the quantity of a particular item has been entered valid. 
   // Alert the user if an invalid quantity on a particular item has been entered.
   //    returns true if the quantity field validates, else false.
   //-------------------------------------------------------------------

   var objForm = document.itemForm;
 
   if (!isDigit(objForm.quantity.value))
   {
       alert ('You have entered an invalid quantity value.\n Please enter a valid number.');
       objForm.quantity.value = "";
       objForm.quantity.focus();
       return false;
   }
   
   if (objForm.quantity.value > quota)
   {
       alert ('I am sorry but the maximum order quantity for this item is ' + quota + '.');
       objForm.quantity.value = quota;
       objForm.quantity.focus();
       return false;
   }

   return true;
}

function calculateSubTotal()
{
   //-------------------------------------------------------------------
   // This function calculates the subtotal field and the grand subtotal when the user clicks the "recalculate" button
   // and "checkout" button.
   //-------------------------------------------------------------------

   var qty, cost, subtotal = 0;
   var objForm = document.shoppingCart;

   for (var i=1; i < objForm.length; i+=5)

       if ((objForm.elements[i].type == "text") && (objForm.elements[i].name == "qty[]"))
       {
           qty = parseInt(objForm.elements[i].value);
           cost = objForm.elements[i+1].value;
           subtotal += qty * cost;
           objForm.elements[i+2].value = addCents(roundFloat(qty * cost));
       }

   objForm.subtotal.value = addCents(roundFloat(subtotal));
   return subtotal;
}

function calculateTotal(flag_id)
{
   //-------------------------------------------------------------------
   // This function calculates the total field once we validate the quantity fields. If a user enters an invalid
   // data in the quantity field, alert the user.
   //    returns true if the quantity field validates, else false.
   //-------------------------------------------------------------------

   var subtotal, total = 0;
   var shipping_cost;

   if (flag_id)
       if (!validateQty())
           return false;

   subtotal = calculateSubTotal();
   shipping_cost = calculateShipping(subtotal);
   document.shoppingCart.shipping.value = shipping_cost;

   document.shoppingCart.total.value = addCents(roundFloat(parseFloat(shipping_cost) + subtotal));
   return true;
}

function calculateShipping(value)
{
   //-------------------------------------------------------------------
   //    returns a new shipping cost based on the subtotal of the cart.
   //-------------------------------------------------------------------
   var cost = 0.00;
   return cost;
}

function roundFloat(value)
{
   //-------------------------------------------------------------------
   //    returns a new value rounded to 2 decimal places.
   //-------------------------------------------------------------------

   return Math.round(value * 100) / 100;
}

function addCents(num)
{
   //-------------------------------------------------------------------
   //    returns a new string that appends 0s at the end of the currency.
   //-------------------------------------------------------------------

   var str = new String(num);
   if (str.indexOf ('.') < 0)
       return num + ".00";

   if (str.indexOf ('.') == (str.length - 2))
       return num + "0";

   return num;
}

function leadingZero(num)
{
   if (num < 10) num = "0" + num;
       return num;
}