
/* This method handles validating the create account form
 * @param form The form to validate
 * @return True (if okay), false otherwise.
 */
function ValidateCreateAccountForm(form)
{
   var strFormUserID    = form.UserID.value;
   var strFormPassword1 = form.Password1.value;
   var strFormPassword2 = form.Password2.value;

   if (isEmpty(strFormUserID))
   {
      alert(ErrorMsg("USERID_EMPTY"));
      form.UserID.focus();
      return false;
   }

   if (!isLengthOK(strFormUserID, 6, 15))
   {
      alert(ErrorMsg("USERID_LENGTH_INVALID"));
      form.UserID.focus();
      form.UserID.select();
      return false;
   }

   if (isEmpty(strFormPassword1))
   {
      alert(ErrorMsg("PASSWORD_EMPTY"));
      form.Password1.focus();
      return false;
   }

   if (!isLengthOK(strFormPassword1, 6, 15))
   {
      alert(ErrorMsg("PASSWORD_LENGTH_INVALID"));
      form.Password1.focus();
      form.Password1.select();
      return false;
   }

   if (strFormPassword1 != strFormPassword2)
   {
      alert(ErrorMsg("PASSWORDS_NOT_EQUAL"));
      form.Password1.focus();
      form.Password1.select();
      return false;
   }

   return true;
}

/* This method handles validating the login form.
 * @param form The form to validate
 * @return True (if okay), false otherwise.
 */
function ValidateLoginForm(form)
{
   var strFormUserID    = form.UserID.value;
   var strFormPassword1 = form.Password1.value;

   if (isEmpty(strFormUserID))
   {
      alert(ErrorMsg("USERID_EMPTY"));
      form.UserID.focus();
      return false;
   }

   if (isEmpty(strFormPassword1))
   {
      alert(ErrorMsg("PASSWORD_EMPTY"));
      form.Password1.focus();
      return false;
   }

   return true;
}

/* This method handles validating the email string passed.
 * @param strInput The email string to validate.
 * @return True (if okay), false otherwise.
 */
function ValidateEmailElement(strInput)
{
   if (isEmpty(strInput))
   {
      alert(ErrorMsg("EMAIL_EMTPY"));
      return false;
   }

   if (!isEmailValid(strInput))
   {
      alert(ErrorMsg("EMAIL_INVALID_FORMAT"));
      return false;
   }

   return true;
}

/* This method handles validating the email form passed.
 * @param form The email form to validate.
 * @return true (if okay), false otherwise.
 */
function ValidateEmailAsForm(form)
{
   strEmail = form.Email.value;

   if (!ValidateEmailElement(strEmail))
   {
      form.Email.focus();
      form.Email.select();
      return false;
   }

   return true;
}

/* This method handles validating both the billing and shipping forms
 * (provided they are both on the same form).
 * @param form The form to validate containing the shipping and billing information
 * @return True (if okay), false otherwise.
 */
function ValidateBillingShippingForm(form)
{
   if (ValidateCustomerBillingInformation(form))
   {
      if (ValidateCustomerShippingInformation(form))
         return true;
      else
         return false;
   }
   else
      return false;

   return true;
}

/* This method handles validating both the billing and shipping forms
 * (provided they are both on the same form) aswell as the create account fields,
 * again, all on the same form.
 * @param form The form to validate containing the shipping, billing, account information
 * @return True (if okay), false otherwise.
 */
function ValidateCreateAccountBillingShippingForm(form)
{
   if (!ValidateCreateAccountForm(form))
      return false;
   else
   {
      if (ValidateCustomerBillingInformation(form))
      {
         if (ValidateCustomerShippingInformation(form))
            return true;
         else
            return false;
      }
      else
         return false;
   }

   return true;
}


/* This method handles validating the change password form.
 * @param form The form to validate
 * @return True (if okay), false otherwise.
 */
function ValidateChangePasswordForm(form)
{
   var strFormOldPassword = form.OldPassword.value;
   var strFormPassword1   = form.Password1.value;
   var strFormPassword2   = form.Password2.value;

   if (isEmpty(strFormOldPassword))
   {
      alert(ErrorMsg("OLD_PASSWORD_EMPTY"));
      form.OldPassword.focus();
      return false;
   }

   /*if (!isLengthOK(strFormOldPassword, 6, 15))
   {
      alert(ErrorMsg("OLD_PASSWORD_LENGTH_INVALID"));
      form.OldPassword.focus();
      form.OldPassword.select();
      return false;
   }*/

   if (isEmpty(strFormPassword1))
   {
      alert(ErrorMsg("PASSWORD_EMPTY"));
      form.Password1.focus();
      return false;
   }

   if (!isLengthOK(strFormPassword1, 6, 15))
   {
      alert(ErrorMsg("PASSWORD_LENGTH_INVALID"));
      form.Password1.focus();
      form.Password1.select();
      return false;
   }

   if(strFormPassword1 != strFormPassword2)
   {
      alert(ErrorMsg("PASSWORDS_NOT_EQUAL"));
      form.Password1.focus();
      form.Password1.select();
      return false;
   }

   return true;
}

/* This method handles validating the customers billing information.
 * @param form The form with the billing information to validate.
 * @return True (if okay), false otherwise.
 */
function ValidateCustomerBillingInformation(form)
{
   var strFormBillName     = form.BillName.value;
   var strFormBillCompany  = form.BillCompany.value;
   var strFormBillAddress1 = form.BillAddress1.value;
   var strFormBillAddress2 = form.BillAddress2.value;
   var strFormBillCity     = form.BillCity.value;
   var strFormBillState    = form.BillState.value;
   var strFormBillCountry  = form.BillCountry.value;
   var strFormBillZIP      = form.BillZIP.value;
   var strFormBillPhone    = form.BillPhone.value;
   var strFormBillFax      = form.BillFax.value;
   var strFormBillEmail    = form.Email.value;

   if (isEmpty(strFormBillName))
   {
      alert(ErrorMsg("BILLING_NAME_EMPTY"));
      form.BillName.focus();
      return false;
   }
/*
   if (isEmpty(strFormBillCompany))
   {
      alert(ErrorMsg("BILLING_COMPANY_EMPTY"));
      form.BillCompany.focus();
      return false;
   }
*/
   if (isEmpty(strFormBillAddress1))
   {
      alert(ErrorMsg("BILLING_ADDRESS1_EMPTY"));
      form.BillAddress1.focus();
      return false;
   }
/*
   if (isEmpty(strBillAddress2))
   {
      alert(ErrorMsg("BILLING_ADDRESS2_EMPTY"));
      form.BillAddress2.focus();
      return false;
   }
*/
   if (isEmpty(strFormBillCity))
   {
      alert(ErrorMsg("BILLING_CITY_EMPTY"));
      form.BillCity.focus();
      return false;
   }

   if (isEmpty(strFormBillState))
   {
      alert(ErrorMsg("BILLING_STATE_EMPTY"));
      form.BillState.focus();
      return false;
   }

   if (isEmpty(strFormBillCountry))
   {
      alert(ErrorMsg("BILLING_COUNTRY_EMPTY"));
      form.BillCountry.focus();
      return false;
   }

   if (isEmpty(strFormBillZIP))
   {
      alert(ErrorMsg("BILLING_ZIP_EMPTY"));
      form.BillZIP.focus();
      return false;
   }

   if (isEmpty(strFormBillPhone))
   {
      alert(ErrorMsg("BILLING_PHONE_EMPTY"));
      form.BillPhone.focus();
      return false;
   }
/*
   if (isEmpty(strFormBillFax))
   {
      alert(ErrorMsg("BILLING_FAX_EMPTY"));
      form.BillFax.focus();
      return false;
   }
*/
   if (!ValidateEmailElement(strFormBillEmail ))
   {
      form.Email.focus();
      return false;
   }

   return true;
}

/* This method handles validating the customers shipping information.
 * @param form The form with the shipping information to validate.
 * @return True (if okay), false otherwise.
 */
function ValidateCustomerShippingInformation(form)
{
   var strFormShipContact  = form.ShipContact.value;
   var strFormShipAddress1 = form.ShipAddress1.value;
   var strFormShipAddress2 = form.ShipAddress2.value;
   var strFormShipCity     = form.ShipCity.value;
   var strFormShipState    = form.ShipState.value;
   var strFormShipCountry  = form.ShipCountry.value;
   var strFormShipZIP      = form.ShipZIP.value;
   var strFormShipPhone    = form.ShipPhone.value;
   var strFormShipFax      = form.ShipFax.value;

   if (isEmpty(strFormShipContact))
   {
      alert(ErrorMsg("SHIPPING_CONTACT_EMPTY"));
      form.ShipContact.focus();
      return false;
   }

   if (isEmpty(strFormShipAddress1))
   {
      alert(ErrorMsg("SHIPPING_ADDRESS1_EMPTY"));
      form.ShipAddress1.focus();
      return false;
   }
/*
   if (isEmpty(strFormShipAddress2))
   {
      alert(ErrorMsg("SHIPPING_ADDRESS2_EMPTY"));
      form.ShipAddress2.focus();
      return false;
   }
*/
   if (isEmpty(strFormShipCity))
   {
      alert(ErrorMsg("SHIPPING_CITY_EMPTY"));
      form.ShipCity.focus();
      return false;
   }

   if (isEmpty(strFormShipState))
   {
      alert(ErrorMsg("SHIPPING_STATE_EMPTY"));
      form.ShipState.focus();
      return false;
   }

   if (isEmpty(strFormShipCountry))
   {
      alert(ErrorMsg("SHIPPING_COUNTRY_EMPTY"));
      form.ShipCountry.focus();
      return false;
   }

   if (isEmpty(strFormShipZIP))
   {
      alert(ErrorMsg("SHIPPING_ZIP_EMPTY"));
      form.ShipZIP.focus();
      return false;
   }

   if (isEmpty(strFormShipPhone))
   {
      alert(ErrorMsg("SHIPPING_PHONE_EMPTY"));
      form.ShipPhone.focus();
      return false;
   }
/*
   if (isEmpty(strFormShipFax))
   {
      alert(ErrorMsg("SHIPPING_FAX_EMPTY"));
      form.ShipFax.focus();
      return false;
   }
*/
   return true;
}

/* This method handles copying the billing information into the
 * shipping information form.
 * @param elem The element (checkbox) that was clicked (use 'this').
 */
function CopyBillingToShipping( elem )
{
   var form = elem.form;

   if ( !elem.checked )
   {
      with ( form )
      {
         ShipName.value       = "";
         ShipAddress1.value   = "";
         ShipAddress2.value   = "";
         ShipCity.value       = "";
         ShipState.value      = "";
         ShipCountry.value    = "";
         ShipZIP.value        = "";
         ShipPhone.value      = "";
         ShipFax.value        = "";
      }

      return;
   }

   with ( form )
   {
      ShipName.value      = BillName.value;
      ShipAddress1.value  = BillAddress1.value;
      ShipAddress2.value  = BillAddress2.value;
      ShipCity.value      = BillCity.value;
      ShipState.value     = BillState.value;
      ShipCountry.value   = BillCountry.value;
      ShipZIP.value       = BillZIP.value;
      ShipPhone.value     = BillPhone.value;
      ShipFax.value       = BillFax.value;
   }
}

/* This method checks to see if the passed variable has the correct email
 * format.
 * @param strInput The intput data to validate for the correct email format.
 * @return True (if okay), false otherwise.
 */
function isEmailValid(strInput)
{
   if (strInput.indexOf("@") >= 0 && strInput.indexOf(".") >= 0)
      return true;
   else
      return false;
}

/* This method checks to see if the passed variable has been entered (if it
 * is empty.
 * @param strInput The intput data to validate.
 * @return True (if empty), false otherwise.
 */
function isEmpty(strInput)
{
   if (strInput == null || strInput == "")
      return true;
   else
      return false;
}

/* This method verifies the length of the passed variable.
 * @param strInput The intput data to validate.
 * @param nMinLength The minimum allowed length of the data.
 * @param nMaxLength The maximum allowed length of the data.
 * @return True (if length okay), false otherwise.
 */
function isLengthOK(strInput, nMinLength, nMaxLength)
{
   if (strInput.length >= nMinLength && strInput.length <= nMaxLength)
      return true;
   else
      return false;
}

/* This method takes care of returning the proper error message, based upon
 * the error key returned.
 * @param strInput The intput data containing the key of an error message.
 * @return The string containing the error message.
 */
function ErrorMsg(strInput)
{
   /* UserID / Password Messages */
   if (strInput == "USERID_EMPTY")                return ("You must provide a User Name.");
   if (strInput == "USERID_LENGTH_INVALID")       return ("The User Name provided must be 6 to 15 characters in length.");
   if (strInput == "OLD_PASSWORD_EMPTY")          return ("The old password cannot be blank.");
   if (strInput == "OLD_PASSWORD_LENGTH_INVALID") return ("The old password must be 6 to 15 characters in length.");
   if (strInput == "PASSWORD_EMPTY")              return ("The password cannot be blank.");
   if (strInput == "PASSWORD_LENGTH_INVALID")     return ("The password must be 6 to 15 characters in length.");
   if (strInput == "PASSWORDS_NOT_EQUAL")         return ("The passwords entered must be the same.");
   if (strInput == "PASSWORD_HINT_EMPTY")         return ("The password hint field cannot be left blank.");

   /* Shipping Messages */
   if (strInput == "SHIPPING_CONTACT_EMPTY")      return ("You must supply a shipping name.");
   if (strInput == "SHIPPING_ADDRESS1_EMPTY")     return ("You must supply shipping address line 1.");
   if (strInput == "SHIPPING_ADDRESS2_EMPTY")     return ("You must supply shipping address line 2.");
   if (strInput == "SHIPPING_CITY_EMPTY")         return ("You must supply a shipping city.");
   if (strInput == "SHIPPING_STATE_EMPTY")        return ("You must supply a shipping state.");
   if (strInput == "SHIPPING_COUNTRY_EMPTY")      return ("You must supply a shipping country.");
   if (strInput == "SHIPPING_ZIP_EMPTY")          return ("You must supply a shipping zip / postal code.");
   if (strInput == "SHIPPING_PHONE_EMPTY")        return ("You must supply a shipping phone number.");
   if (strInput == "SHIPPING_FAX_EMPTY")          return ("You must supply a shipping fax number.");

   /* Billing Messages */
   if (strInput == "BILLING_NAME_EMPTY")          return ("You must supply a billing name.");
   if (strInput == "BILLING_COMPANY_EMPTY")       return ("You must supply a billing company name.");
   if (strInput == "BILLING_ADDRESS1_EMPTY")      return ("You must supply billing address line 1.");
   if (strInput == "BILLING_ADDRESS2_EMPTY")      return ("You must supply billing address line 2.");
   if (strInput == "BILLING_CITY_EMPTY")          return ("You must supply a billing city.");
   if (strInput == "BILLING_STATE_EMPTY")         return ("You must supply a billing state.");
   if (strInput == "BILLING_COUNTRY_EMPTY")       return ("You must supply a billing country.");
   if (strInput == "BILLING_ZIP_EMPTY")           return ("You must supply a billing zip / postal code.");
   if (strInput == "BILLING_PHONE_EMPTY")         return ("You must supply a billing phone number.");
   if (strInput == "BILLING_FAX_EMPTY")           return ("You must supply a billing fax number.");

   /* Email Messages */
   if (strInput == "EMAIL_EMTPY")                 return ("You must supply an email address.");
   if (strInput == "EMAIL_INVALID_FORMAT")        return ("The email address supplied is not in the correct email address format.");
}