

//  $Id: fieldvalidate.js,v 1.1 2005/06/09 12:37:58 PILOTGROUP\anton Exp $
//
//  $Log: fieldvalidate.js,v $
//  Revision 1.1  2005/06/09 12:37:58  PILOTGROUP\anton
//  Mortgage Site Solution
//
//  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)
{
	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))
	{
		alert("Please enter a response in numerical format (please " + 
			"exclude dollar signs, commas, parenthesis or any other " + 
			"non-numeric symbols).");
		objFld.value = "";
		objFld.focus();
		return false;
	}

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

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

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

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

	if (strType == "DatePartYear" && checkField(objFld, "Digit"))
	{
		if (!isDatePart(objFld.value, "Year"))
		{
			alert("Please enter valid year value");
			objFld.value = "";
			objFld.focus();
			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)) {
      		alert("Please enter a valid credit card number in this field.");
			objFld.value="";
      		objFld.focus();
      		return false;
    	}
  	}

	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);
}

