//b2c.js
//Developed by Billy clauges for Response Computer Group

var defaultEmptyOK = false
var mSuffix = "You did not enter a value in a required field. Please enter it now."
var whitespace = " \t\n\r";
var custom_sauces = new Array();

function printWindow()
{
   document.all.top_menu.style.visibility="hidden";
   window.print();
   document.all.top_menu.style.visibility="visible";
}


function validateShipTo(form)
{   
    return (
      checkString(form.elements["ST_SHIP_ID"],"") &&
      checkString(form.elements["ST_NAME"],"") &&
      checkString(form.elements["ST_ADDRESS1"],"") &&
      checkString(form.elements["ST_CITY"],"") && 
      checkString(form.elements["ST_STATE"],"") &&
      checkString(form.elements["ST_ZIP"],"") 
    )
}

function validateBillTo(form)
{   
    return (
      checkString(form.elements["SB_BILL_ID"],"") &&
      checkString(form.elements["SB_NAME"],"") &&
      checkString(form.elements["SB_ADDRESS1"],"") &&
      checkString(form.elements["SB_CITY"],"") && 
      checkString(form.elements["SB_STATE"],"") &&
      checkString(form.elements["SB_ZIP"],"") 
    )
}

function validateCheckout4(form)
{
    return (
      checkString(form.elements["PO_NUM"],"") &&
      checkString(form.elements["ORDERED_BY"],"")
    )
}


function validateCard(form)
{   
    return (
      checkString(form.elements["CC_TYPE"],"") &&
      checkString(form.elements["CC_NUMBER"],"") &&
      checkString(form.elements["CC_MONTH"],"") && 
      checkString(form.elements["CC_YEAR"],"") &&
      checkString(form.elements["CC_CARDHOLDER"],"") &&
checkString(form.elements["CC_CVV"],"Credit Card CVV Code must be entered.") &&
      checkString(form.elements["CC_ZIP"],"") 
    )
}

function validateCardId(form)
{   
    return (
      checkString(form.elements["CC_CARD_ID"],"") 
    )
}


function validatePass(form)
{   
    return (
      checkString(form.elements["PASSWORD1"],"") &&
      checkString(form.elements["PASSWORD2"],"") 
    )
}


function validateCreateAccount(form)
{   
    return (
      checkString(form.elements["USER_ID"],"") &&
      checkString(form.elements["USER_PASS1"],"") &&
      checkString(form.elements["USER_PASS2"],"") &&
      checkString(form.elements["USER_NAME"],"") &&
      checkString(form.elements["USER_ADDRESS1"],"") &&
      checkString(form.elements["USER_CITY"],"") && 
      checkString(form.elements["USER_STATE"],"") &&
      checkString(form.elements["USER_EMAIL"],"") &&
      checkString(form.elements["USER_ZIP"],"") 
    )
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mSuffix)
    return false
}

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}


function chkQtys()
{
   var totqty =0;
   for (var n=0; n < document.frmMain.length; n++)
   {
       if (document.frmMain.elements[n].type=='text')
      {
      if (isNaN(document.frmMain.elements[n].value) && document.frmMain.elements[n].length != 0)
         {alert("Quantities must be numeric.");return false;}
      else
         if (isNaN(parseInt(document.frmMain.elements[n].value))){ } else {totqty=parseInt(totqty)+parseInt(document.frmMain.elements[n].value);}
      }
   }
   
   if (totqty == 0 || isNaN(totqty))
      {alert("Quantity must be entered in at least one item.");return false;}
   else
   {   
      if (document.frmMain.ITEM.value =='TRC-CUSTOM          ')
      {
         if (!(totqty % 4))
				    return true
         else
         {
            alert("Total quantity must be a multiple of 4.");
            return false
         }
      }
      else
      {
         return true
      }
   }
}


function chkForm(pgm,flag)
{
   if (chkQtys() == true)
      addItem(pgm,flag);
}


function chkCartQtys()
{
   var totqty=0;

   if (custom_sauces.length != 0)
   {
      for (var n=0; n < custom_sauces.length; n++)
      {
         if (isNaN(custom_sauces[n]))
         {}
         else 
         {
            totqty = parseInt(totqty) + parseInt(custom_sauces[n]);
         }
      }
      if (totqty != 0)
      {
         if (!(totqty % 4))
         {
            document.frmMain.submit();
         }
         else
         {
            alert("Total sauce quantity must be a multiple of 4.");
         }
      }
      else
      {
         document.frmMain.submit();
      }

   }
   else
   {
      document.frmMain.submit();
   }
}

