

//  $Id: fieldvalidate.js,v 1.12 2004/11/23 21:49:14 dsimon Exp $
//
//  $Log: fieldvalidate.js,v $
//  Revision 1.12  2004/11/23 21:49:14  dsimon
//  Bug 4925, calling objFld.select() after alert to prevent button from staying depressed
//
//  Revision 1.11  2004/11/19 19:27:36  dsimon
//  Bug 4925, added param to checkField() to clear out field
//
//  Revision 1.10  2004/08/04 18:01:28  jreed
//  Added validation for credit card id
//
//  Revision 1.9  2004/08/03 19:25:38  jreed
//  Modified checkfield to select the invalid value rather than blanking it
//  Also, do select before alert
//
//  Revision 1.8  2004/06/23 17:04:37  jseverson
//  Minor changes but editor converted tabs to spaces
//
//  Revision 1.7  2002/07/12 00:34:38  vhegde
//  cleared value of credit card field
//
//  Revision 1.6  2002/07/10 01:48:28  vhegde
//  added credit card validation function
//
//  Revision 1.5  2002/03/31 21:32:29  jneil
//  Added reference to isSsn function.
//
//  Revision 1.4  2002/02/20 18:42:46  nwiggins
//
//  # removes script tags - forces usage as a javascript src include. no php includes
//
//  Revision 1.3  2001/07/18 23:07:57  jneil
//  Fixed some problems.
//
//  Revision 1.2  2001/07/02 06:11:18  vhegde
//  Revision 1.1  2001/05/21 15:53:53  jneil
//  Initial Upload.
//
//

/**********************************************************************
Depends on:   alltrim(), isEmpty(), replaceall() functions of string.js library
    isFloat(), isDigit(), isZipcode() isSsn() functions of ctype.js library

NOTE:
  -- always alltrim field value before calling any of these functions
  -- if field is empty, the function call will return true
**********************************************************************/

function checkField(objFld, strType, clearField)
{
  objFld.value = alltrim(objFld.value);
  if (isEmpty(objFld.value))
    return true;

  // Remove commas from Integer and Float fields
  if ((strType == "Integer") || (strType == "Float"))
    objFld.value = replaceall(objFld.value, ",", "");

  if ((strType == "Digit") && !isDigit(objFld.value))
  {
    if(clearField){
      objFld.value = "";
    }
    alert("Please enter a response in numerical format (please " +
      "exclude dollar signs, commas, parenthesis or any other " +
      "non-numeric symbols).");
    objFld.select();    
    return false;
  }

  if (strType == "Integer")
    if (!isInteger(parseInt(objFld.value)))
    {
      if(clearField){
        objFld.value = "";
      }
           
      alert("Please enter a response in numerical format (please " +
        "exclude dollar signs, commas, parenthesis or any other " +
        "non-numeric symbols).");
      objFld.select();
      return false;
    }
    else
      objFld.value = parseInt(objFld.value);

  if (strType == "Float")
    if (!isFloat(parseFloat(objFld.value)))
    {
      if(clearField){
        objFld.value = "";
      }
      alert("Please enter a response in numerical format (please " +
        "exclude dollar signs, commas, parenthesis or any other " +
        "non-numeric symbols).");
      objFld.select();
      return false;
    }
    else
      objFld.value = parseFloat(objFld.value);

  if (strType == "ZIP")
    if (!isZipcode(objFld.value))
    {
      if(clearField){
        objFld.value = "";
      }
      alert("Please enter valid zip code value");
      objFld.select();
      return false;
    }

  if (strType == "Date")
    if (!isDate(objFld.value))
    {
      if(clearField){
        objFld.value = "";
      }
      alert("Please enter valid date value (MM/DD/YYYY)");
      objFld.select();
      return false;
    }

  if (strType == "DatePartYear" && checkField(objFld, "Digit"))
  {
    if (!isDatePart(objFld.value, "Year"))
    {
      if(clearField){
        objFld.value = "";
      }
      alert("Please enter valid year value");
      objFld.select();
      return false;
    }
    else if (objFld.value < 100)
      objFld.value = (objFld.value < 50 ? 2000:1900) + parseInt(objFld.value);
  }

   if (strType == "CreditCard") {
      if (!validate_card(objFld.value)) {
          if(clearField){
            objFld.value = "";
          }
          alert("Please enter a valid credit card number.");
          objFld.select();
          return false;
      }
    }

   if (strType == "CreditCardId") {
      if (!validate_card_code(objFld.value)) {
          if(clearField){
            objFld.value = "";
          }
          alert("Please enter a valid credit card id.");
          objFld.select();
          return false;
      }
    }
    validate_card_code

  return true;
}

/*******************************************************************************
 checkRange:  Check the range of field value.
 Parameters:  objFld - Field reference
    strType - "Inclusive" = include boundary values
      - "Exclusive" = exclude boundary values
      - "LeftInclusive" = include only left boundary value
      - "RightInclusive" = include only right boundary value
*********************************************************************************/
function checkRange(objFld, fLower, fUpper, strType)
{
  var fVal = 0.0;

  if (isEmpty(objFld.value))
    return true;
  if (!isFloat(objFld.value))
    return false;

  fVal = parseFloat(objFld.value);

  if ((strType == "Inclusive") && (fVal >= fLower) && (fVal <= fUpper))
    return true;
  else if ((strType == "Exclusive") && (fVal > fLower) && (fVal < fUpper))
    return true;
  else if ((strType == "LeftInclusive") && (fVal >= fLower) && (fVal < fUpper))
    return true;
  else if ((strType == "RightInclusive") && (fVal > fLower) && (fVal <= fUpper))
    return true;

  alert("Value should be between " + fLower + " and " + fUpper);
  objFld.value = "";
  objFld.focus();

  return false;
}

/************************************************************
 checkLength:   Check the length of the field value.
    Useful for fields like TEXTAREA
    where MAXLENGTH attribute is not available
 Parameters:  objFld - Field reference
************************************************************/
function checkLength(objFld, nLen)
{
  var strVal = new String(objFld.value);

  if (strVal.length > nLen) {
    alert("Maximum length allowed is " + nLen + " characters. " +
      "Value has been truncated to the size of " + nLen + ".");
    objFld.value = strVal.substring(0, nLen);
    objFld.focus();
    return false;
  }

  return true;
}

function validate_card(cardnum) {
  var re = new RegExp("[^0-9]", "g");
  cardnum = cardnum.replace(re, "");
  var cardname = "Invalid Card";
  var len_req = 0;

  if (cardnum.substring(0, 3) >= "300" && cardnum.substring(0, 3) <= "305") {
    cardname = "Diners Club";
  }
  if (cardnum.substring(0, 2) == "36") {
    cardname = "Diners Club";
  }
  if (cardnum.substring(0, 4) >= "3800" && cardnum.substring(0, 4) <= "3889") {
    cardname = "Diners Club";
  }
  if (cardnum.substring(0, 2) == "34") {
    cardname = "American Express";
  }
  if (cardnum.substring(0, 2) == "37") {
    cardname = "American Express";
  }
  if (cardnum.substring(0, 4) >= "3528" && cardnum.substring(0, 4) <= "3589") {
    cardname = "JCB";
  }
  if (cardnum.substring(0, 3) == "389") {
    cardname = "Carte Blanche";
  }
  if (cardnum.substring(0, 1) == "4") {
    cardname = "Visa";
  }
  if (cardnum.substring(0, 2) >= "51" && cardnum.substring(0, 2) <= "55") {
    cardname = "MasterCard";
  }
  if (cardnum.substring(0, 4) == "5610") {
    cardname = "Australian BankCard";
  }
  if (cardnum.substring(0, 4) == "6011") {
    cardname = "Discover/Novus";
  }

  switch (cardname) {
    case "Diners Club":
    case "Carte Blanche":
      len_req = 14;
      break;
    case "American Express":
      len_req = 15;
      break;
    case "JCB":
    case "MasterCard":
    case "Discover/Novus":
      len_req = 16;
      break;
    case "Visa":
      len_req = 16;
      if (cardnum.length < 14)
        len_req = 13;
      break;
    default:
      return false;
  }
 if (cardnum.length != len_req)
    return false;

  var checksum = 0;

  for (i = 1 - (cardnum.length % 2); i < cardnum.length; i += 2) {
    checksum += parseInt(cardnum.substr(i, 1));
  }

  for (i = (cardnum.length % 2); i < cardnum.length; i += 2) {
    if (cardnum.substr(i, 1) < 5) {
      checksum += parseInt(cardnum.substr(i, 1))*2;
    } else {
      checksum += parseInt(cardnum.substr(i, 1))*2 - 9;
    }
  }

  return (checksum % 10 == 0);
}

function validate_card_code(s) {
  var i;
  for (i = 0; i < s.length; i++)
  {
      var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}

