//@BEGINVERSIONINFO

//@APPVERSION: 50.4013.0.2

//@FILENAME: sfcheckerrors.asp

//@DESCRIPTION: Checkes sfErrors

//@STARTCOPYRIGHT
//The contents of this file is protected under the United States
//copyright laws as an unpublished work, and is confidential and proprietary to
//LaGarde, Incorporated.  Its use or disclosure in whole or in part without the
//expressed written permission of LaGarde, Incorporated is expressly prohibited.

//(c) Copyright 2000 by LaGarde, Incorporated.  All rights reserved.
// modified 5/18/03  fjl - added tests for payx_ fields

//@ENDCOPYRIGHT

//@ENDVERSIONINFO
var G_focus_this_flag=null

function G_focus_this_error(cell)
{
  if (G_focus_this_flag == null)
  {
    if (cell.focus != null)
    {
      G_focus_this_flag = cell
      cell.focus()
    }
  }
}

function specialCase(e, form)
{
  if ((e.name == "CardName")||(e.name == "CardNumber")||(e.name == "CardExpiryMonth")||(e.name == "CardExpiryYear"))
  {
    if (((form.CardName.value.length <= 0)||(form.CardNumber.value.length <= 0)||(form.CardExpiryMonth.value.length <= 0)||(form.CardExpiryYear.value.length <= 0)) &&
        ((form.CardName.value.length > 0)||(form.CardNumber.value.length > 0)||(form.CardExpiryMonth.value.length > 0)||(form.CardExpiryYear.value.length > 0)))
    {
      return "Please enter all Credit Card Information.\n";
    }

    if ((form.CardName.value.length > 0) && (form.CardNumber.value.length > 0) && (form.CardExpiryMonth.value.length > 0) && (form.CardExpiryYear.value.length > 0))
    {
      if (!isCardDateValid(form.CardExpiryYear.value, form.CardExpiryMonth.value))
      {
        if (!isNumber(form.CardExpiryYear.value))
        {
          return "The Credit Card expiration year is invalid.\n";
        }
        else if (!isNumber(form.CardExpiryMonth.value))
        {
          return "The Credit Card expiration month is invalid.\n";
        }
        else
        {
          return "The Credit Card has Expired.\n";
        }
      }

      if (isCardNumValid(form.CardNumber.value))
      {
        return "The Credit Card Number is an invalid format.\n";
      }
    }
  }

  if ((e.name == "CheckNumber")||(e.name == "BankName")||(e.name == "RoutingNumber")||(e.name == "CheckingAccountNumber"))
  {
    if (((form.CheckNumber.value.length <= 0)||(form.BankName.value.length <= 0)||(form.RoutingNumber.value.length <= 0)) &&
        ((form.CheckNumber.value.length > 0)||(form.BankName.value.length > 0)||(form.RoutingNumber.value.length > 0)))
    {
      if ((form.CheckNumber.value.length <= 0) && (e.name == "CheckNumber"))
      {
        G_focus_this_error(e)
      }

      if ((form.BankName.value.length <= 0) && (e.name == "BankName"))
      {
        G_focus_this_error(e)
      }

      if ((form.RoutingNumber.value.length <= 0) && (e.name == "RoutingNumber"))
      {
        G_focus_this_error(e)
      }

      return "Please enter all Tele-Check Information.\n";
    }
    else
    {
      if (e.name == "CheckNumber")
      {
        if ((e.value.length < 3) || (e.value.length > 8))
        {
          G_focus_this_error(e)
          return "Please enter a valid check number. ( between 3 and 8 digits )\n";
        }
      }
      else if (e.name == "RoutingNumber")
      {
        if ((form.CheckingAccountNumber.value.length == 0) && ((e.value.length < 17) || (e.value.length > 30)))
        {
          G_focus_this_error(e)
          return "Please enter a valid Routing and Checking number combination. ( 17 - 30 digits )\n";
        }
        else if ((form.CheckingAccountNumber.value.length != 0) && ((e.value.length < 17) || (e.value.length > 30)))
        {
          G_focus_this_error(e)
          return "Please enter a valid Routing number. ( 9 digits )\n";
        }
      }
      else if (e.name == "CheckingAccountNumber")
      {
        //if (form.RoutingNumber.value.length <= 9)
        //{
        //  if ((e.value.length < 7) || (e.value.length > 10))
        //  {
        //    return "Please enter a valid Checking Account number. ( between 7 and 10 digits )\n";
        //  }
        //}
        //else if (e.value.length > 0)
        //{
        //  return "Remove the Checking Account number\nwhen using the combined Routing/Checking Number method.\n";
        //}
      }
    }
  }

  if (e.name.indexOf("payx_") != -1)
  {
    if ((e.name == "payx_isclient"))
    {
      if ((form.payx_isclient[0].checked == false) &&
        (form.payx_isclient[1].checked == false))
      {
        //return "Please select a Paychex client choice, Yes or No.\n";
        return "Client status not indicated.\n";
      }
    }

    if ((e.name == "payx_repeatcust"))
    {
      if ((form.payx_repeatcust[0].checked == false) &&
         (form.payx_repeatcust[1].checked == false))
      {
        //return "Please select Yes or No if you have ordered from Paychex before.\n";
        return "Prior ordering status not indicated.\n";
      }
    }

    //if ((e.name == "payx_taxexempt"))
    //{
    //  if ((form.payx_taxexempt.value.length > 0) &&
    //     (form.payx_taxexempt.value.length != 10))
    //  {
    //    return "Please enter a valid Federal Tax ID number (xx-xxxxxxx).\n";
    //  }
    //}
  }

  return "";
}

function stripChar(sValue, sChar)
{
  var i, tempChar, buildString;
  buildString = ""
  for (var i=0; i<sValue.length; i++)
  {
    tempChar = sValue.charAt(i);
    if (tempChar != sChar)
    {
      buildString = buildString + tempChar;
    }
  }
  return buildString;
}

function isCardDateValid(year, month)
{
  var dateCheck, now;
  var numck1
  var numck2

  numck1 = parseInt(year)
  numck2 = parseInt(month)

  if (!isNumber(year) || !isNumber(month))
  {
    return false
  }

  if (year.length == 2)
  {
    if (parseInt(year) < 50)
    {
      year = "20" + year;
    }
  }

  now = new Date();
  dateCheck = new Date(year, month);

  if (now > dateCheck)
  {
    return false;
  }
  else
  {
    return true;
  }
}

function isCardNumValid(num)
{
  var num1, num2, tempNum;
  if (!isNumber(num))
  {
    return true;
  }
  num1 = ""
  if (!(num.length%2==0))
  {
    for(var j=0; j < num.length; j++)
    {
      if ((j+1)%2==0){
        tempNum = 2 * num.charAt(j);
      }
      else
      {
        tempNum = 1 * num.charAt(j);
      }
      num1 = num1 + tempNum.toString();
    }
  }
  else
  {
    for(var j=0; j < num.length; j++)
    {
      if ((j+1)%2==0)
      {
        tempNum = 1 * num.charAt(j);
      }
      else
      {
        tempNum = 2 * num.charAt(j);
      }
      num1 = num1 + tempNum.toString();
    }
  }

  num2 = 0;
  for (var j = 0; j < num1.length; j++)
  {
    num2 = num2 + parseInt(num1.charAt(j));
  }

  if (num2%10==0)
  {
    return false;
  }
  else
  {
    return true;
  }
}

function isNumber(value) {
  for (var i=0; i < value.length; i++)
  {
    a = parseInt(value.charAt(i));
    if (isNaN(a))
    {
      return false;
      break;
    }
  }
  return true;
}

function sfCheck(form)
{
  var e, title, empty_fields, char_check, invalid_card, invalid_cvv, month, year, invalid_date, eMail, invalid_eMail
  var iQuantity, quantity_check, checkSpecial, tempError, special_Error, msg, upperLine, upperline2, lowerLine, lowerline2
  var num, invalid_phoneNumber, passwd_mismatch, invalid_textsize, invalid_month, invalid_year
  msg = "";
  var lowrcell='';
  var not_in_full='not provided in full.';
  empty_fields = "";
  char_check = "";
  special_Error = "";
  tempError = "";
  invalid_eMail = false;
  num = form.length
  G_focus_this_flag = null

  for (var i = 0; i < form.length; i++)
  {
    e = form.elements[i]
    if ((e.title == null)||(e.title == ""))
    {
      title = e.name;
    }
    else
    {
      title = e.title;
    }

    if (((e.type == "text") || (e.type == "textarea")||(e.type == "password")) && !e.special && !e.disabled)
    {
      if (e.value.length <= 0 && !e.optional && (e.name.indexOf("Ship") == -1))
      {
        G_focus_this_error(e)
        // need a big if tree here to add the appropriate text...
        lowrcell = title.toLowerCase();
        switch(lowrcell)
        {
          case 'firstname':
            empty_fields += "\n        First Name " + not_in_full;
            break;

          case 'lastname':
            empty_fields += "\n        Last Name " + not_in_full;
            break;

          case 'address1':
            empty_fields += "\n        Street Address not entered in full.";
            break;

          case 'city':
            empty_fields += "\n        City not identified.";
            break;

          case 'zip':
            empty_fields += "\n        ZIP/Postal Code not properly entered.";
            break;

          default:
            empty_fields += "\n        " + title + " " + not_in_full;
            //empty_fields += "\n        " + title;
            break;
        }

        continue;
      }

      if (e.number)
      {
        num = e.value;
        num = stripChar(num, ".");
        num = stripChar(num, ",");
        if (!isNumber(num))
        {
          char_check += "\n         " + title;
          G_focus_this_error(e)
        }
      }

      if (e.creditCardNumber)
      {
        e.value = stripChar(e.value, " ");
        e.value = stripChar(e.value, "-");
        invalid_card = isCardNumValid(e.value);
      }

      if (e.textcomment)
      {
        if ( e.value.length > 500 )
        {
          invalid_textsize = true;
          G_focus_this_error(e)
        }
      }

      if ((e.creditCardExpMonth)||(e.creditCardExpYear))
      {
        if (e.creditCardExpMonth)
        {
          month = e.value;
          month = stripChar(month, " ")
          if (!isNumber(month))
          {
            invalid_month = true;
            G_focus_this_error(e)
            month = null;
          }
        }

        if (e.creditCardExpYear)
        {
          year = e.value;
          year = stripChar(year, " ")
          if (!isNumber(year))
          {
            invalid_year = true;
            G_focus_this_error(e)
            year = null;
          }
        }

        if ((month != null) && (year != null))
        {
          if(!isCardDateValid(year, month))
          {
            invalid_date = true;
            G_focus_this_error(e)
          }
        }
      }

      if (e.eMail)
      {
        eMail = e.value;
        if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1))
        {
          if (!invalid_eMail)
          {
            invalid_eMail = false;
          }
        }
        else
        {
          invalid_eMail = true;
          G_focus_this_error(e)
        }
      }

      if (e.name == "txtEmail")
      {
        eMail = e.value;
        if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1))
        {
          if (!invalid_eMail)
          {
            invalid_eMail = false;
          }
        }
        else
        {
          invalid_eMail = true;
          G_focus_this_error(e)
        }
      }

      if (e.name == "txtFriend")
      {
        eMail = e.value;
        if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1))
        {
          if (!invalid_eMail)
          {
            invalid_eMail = false;
          }
        }
        else
        {
          invalid_eMail = true;
          G_focus_this_error(e)
        }
      }

      if (e.phoneNumber)
      {
        num = e.value;
        num = stripChar(num, " ");
        num = stripChar(num, "(");
        num = stripChar(num, ")");
        num = stripChar(num, "-");
        num = stripChar(num, "+");
        if (num.length < 10)
        {
          invalid_phoneNumber = true;
          G_focus_this_error(e)
        }
      }
    }

    if (e.quantityBox)
    {
      iQuantity = e.value;
      if (!isNumber(iQuantity))
      {
        quantity_check = true;
        G_focus_this_error(e)
      }

      if (parseInt(iQuantity) < 0)
      {
        quantity_check = true;
        G_focus_this_error(e)
      }

      if ((iQuantity) < 1)
      {
        quantity_check = true;
        G_focus_this_error(e)
      }

    }

    if (e.password)
    {
      if (form.Password.value != form.Password2.value)
      {
        passwd_mismatch = true;
        G_focus_this_error(e)
      }
    }

    if (e.cvv_req)
    {
      if ((form.CardVV.value.length != 3) && (form.CardVV.value.length != 4))
      {
        invalid_cvv = true;
        G_focus_this_error(e)
      }
      else if (!isNumber(form.CardVV.value))
      {
        // check format of CVV
        invalid_cvv = true;
        G_focus_this_error(e)
      }
    }

    if (e.special)
    {
      checkSpecial = specialCase(e, form);

      if (tempError != checkSpecial)
      {
        G_focus_this_error(e)
        special_Error = special_Error + checkSpecial
      }

      tempError = checkSpecial;
    }

    if (((e.type == "select-one") || (e.type == "dropdown")) && !e.optional)
    {
      if ((e.options[e.selectedIndex].value == "")||(e.options[e.selectedIndex].value == " "))
      {
        empty_fields += "\n        " + title + " not selected.";
        G_focus_this_error(e)
        continue;
      }
    }
  }

  if (!invalid_cvv && !empty_fields && !char_check && !special_Error && !invalid_card && !invalid_date && !invalid_month && !invalid_year && !invalid_eMail && !quantity_check && !invalid_phoneNumber && !passwd_mismatch && !invalid_textsize)
  {
    return true
  }

  msg = "This form was not submited due to the error(s) listed below.\n";
  msg = msg + "Please correct them and resubmit the form.\n";

  upperLine = "\n_________________________________________________________\n\n";
  upperLine2 = "\n\n";
  lowerLine = "_________________________________________________________\n";
  lowerline2 =""

  if (empty_fields)
  {
    msg += upperLine;
    msg += "The following field(s) must be filled in:\n";
    msg += lowerLine;
    msg += empty_fields;
  }

  if (char_check)
  {
    msg += upperLine;
    msg += "The following field(s) need a numeric value:\n";
    msg += lowerLine;
    msg += char_check;
  }

  if (quantity_check)
  {
    msg += upperLine;
    msg += "Please Enter a Positive Integer.\n"
    msg += lowerLine;
  }

  if (invalid_card)
  {
    msg += upperLine;
    msg += "The Credit Card Number is an invalid format.\n";
    msg += lowerLine;
  }

  if (invalid_date)
  {
    msg += upperLine;
    msg += "The Credit Card has Expired.\n";
    msg += lowerLine;
  }

  if (invalid_month)
  {
    msg += upperLine;
    msg += "The Credit Card expiration month is invalid.\n";
    msg += lowerLine;
  }

  if (invalid_year)
  {
    msg += upperLine;
    msg += "The Credit Card expiration year is invalid.\n";
    msg += lowerLine;
  }

  if (invalid_cvv)
  {
    msg += upperLine;
    msg += "The Credit Card Security Code is invalid.\n";
    msg += lowerLine;
  }

  if (invalid_eMail)
  {
    if (empty_fields)
    {
      msg += "\n\n        E-mail Address format not valid.\n           (e.g., yourname@domain.com)\n";
    }
    else
    {
      msg += upperLine;
      msg += "E-mail Address format not valid.\n   (e.g., yourname@domain.com)\n";
    }
    msg += lowerLine;
  }

  if (invalid_phoneNumber)
  {
    msg += upperLine;
    msg += "Phone Number not completed.\n (Include area code, or country and city codes.)\n";
    msg += lowerLine;
  }

  if (invalid_textsize)
  {
    msg += upperLine;
    msg += "Please shorten your comment, it must be less than 500 characters.\n";
    msg += lowerLine;
  }

  if (special_Error)
  {
    msg += upperLine;
    msg += special_Error + "\n";
    msg += lowerLine;
  }

  if (passwd_mismatch)
  {
    msg += upperLine;
    msg += "Your new password entries do not match. Please try again.\n";
    msg += lowerLine;
  }

  alert(msg);
  return false;
}

function sfCheckPlus(frm)
{
  if ((frm.ShipFirstName.value != "") || (frm.ShipMiddleInitial.value != "") || (frm.ShipLastName.value != "") || (frm.ShipCompany.value != "") || (frm.ShipAddress1.value != "") || (frm.ShipAddress2.value != "") || (frm.ShipCity.value != "") || ((frm.ShipState.value != "") && (frm.ShipState.value != " ")) || (frm.ShipZip.value != "") || (frm.ShipCountry.value != "") || (frm.ShipPhone.value != "" ) || (frm.ShipFax.value != ""))
  {
    if ((frm.ShipFirstName.value == "") || (frm.ShipLastName.value == "") || (frm.ShipAddress1.value == "") || (frm.ShipCity.value == "") || (frm.ShipState.value == "") || (frm.ShipState.value == " ") || (frm.ShipZip.value == "") || (frm.ShipCountry.value == "") || (frm.ShipPhone.value == "") || (frm.ShipEmail.value == ""))
    {
      window.alert("Your shipping information is incomplete. Please complete the\nentire section or check 'Copy from Billing Information.'\n");
      return false
    }
    else
    {
      return sfCheck(frm)
    }
  }
}


