/****************************************************************
FILE: RegExpValidate.js

DESCRIPTION: This file contains a library of validation functions
  using javascript regular expressions.  Library also contains
  functions that reformat fields for display or for storage.


  VALIDATION FUNCTIONS:

  validateEmail - checks format of email address
    validateUSPhone - checks format of US phone number
    validateNumeric - checks for valid numeric value
  validateInteger - checks for valid integer value
    validateNotEmpty - checks for blank form field
  validateUSZip - checks for valid US zip code
  validateUSDate - checks for valid date in US format
  validateValue - checks a string against supplied pattern

  FORMAT FUNCTIONS:

  rightTrim - removes trailing spaces from a string
  leftTrim - removes leading spaces from a string
  trimAll - removes leading and trailing spaces from a string
  removeCurrency - removes currency formatting characters (), $
  addCurrency - inserts currency formatting characters
  removeCommas - removes comma separators from a number
  addCommas - adds comma separators to a number
  removeCharacters - removes characters from a string that match
  passed pattern


AUTHOR: Karen Gayda

DATE: 03/24/2000

UPDATED BY: Endre Pálfi on 01_19_10

*******************************************************************/

    function validateUSZip( source, args ) {
    /************************************************
    DESCRIPTION: Validates that a string a United
      States zip code in 5 digit format or zip+4
      format. 99999 or 99999-9999

    PARAMETERS:
       strValue - String to be tested for validity

    RETURNS:
       True if valid, otherwise false.

    *************************************************/
      var oValidatedControl = document.getElementById(source.controltovalidate);

      var objRegExp = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

      bValidationTest = true;
      bValidationTest = objRegExp.test(args.Value);

      //check for valid US Zipcode
      if (args.Value.length == 0) {
        //empty text message is checked by different means
        args.IsValid = false;
        return false;
      } else {
        if (args.Value.length <= 10) {
          args.IsValid = bValidationTest;
          if (bValidationTest == true) {
            return true;
          } else {
            args.IsValid = true;
            alert("Please enter a valid U.S. zip code in the following format XXXXX or XXXXX-XXXX.");
            oValidatedControl.focus();
            return true;
          }
        } else {
          args.IsValid = false;
          alert("Please enter a valid U.S. zip code in the following format XXXXX or XXXXX-XXXX.");
          oValidatedControl.focus();
          return false;
        }
      }
      return bValidationTest;
    }

function validateUSPhone( source, args ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code

      var oValidatedControl = document.getElementById(source.controltovalidate);

      bValidationTest = true;
      bValidationTest = objRegExp.test(args.Value);

      //check for valid US Zipcode
      if (args.Value.length == 0) {
        //empty text message is checked by different means
        args.IsValid = false;
        return false;
      } else {
        if (args.Value.length <= 14) {
          args.IsValid = bValidationTest;
          if (bValidationTest == true) {
            return true;
          } else {
            args.IsValid = true;
            alert("Please enter a valid U.S. phone number in the following format (XXX)XXX-XXXX or (XXX) XXX-XXXX.");
            oValidatedControl.focus();
            return true;
          }
        } else {
          args.IsValid = false;
          alert("Please enter a valid U.S. phone number in the following format (XXX)XXX-XXXX or (XXX) XXX-XXXX.");
          oValidatedControl.focus();
          return false;
        }
      }
      return bValidationTest;

}

function validateUSFax( source, args ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code

      var oValidatedControl = document.getElementById(source.controltovalidate);

      bValidationTest = true;
      bValidationTest = objRegExp.test(args.Value);

      //check for valid US Fax Number
      if (args.Value.length == 0) {
        //empty text message is checked by different means
        args.IsValid = false;
        return false;
      } else {
        if (args.Value.length <= 14) {
          args.IsValid = bValidationTest;
          if (bValidationTest == true) {
            return true;
          } else {
            args.IsValid = true;
            alert("Please enter a valid U.S. fax number in the following format (XXX)XXX-XXXX or (XXX) XXX-XXXX.");
            oValidatedControl.focus();
            return true;
          }
        } else {
          args.IsValid = false;
          alert("Please enter a valid U.S. fax number in the following format (XXX)XXX-XXXX or (XXX) XXX-XXXX.");
          oValidatedControl.focus();
          return false;
        }
      }
      return bValidationTest;

}

function validateEmail( source, args ) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
var objRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

  //check for valid email

      var oValidatedControl = document.getElementById(source.controltovalidate);

      bValidationTest = true;
      bValidationTest = objRegExp.test(args.Value);

      //check for valid email
      if (args.Value.length == 0) {
        //empty text message is checked by different means
        args.IsValid = false;
        return false;
      } else {
        if (args.Value.length <= 255) {
          args.IsValid = bValidationTest;
          if (bValidationTest == true) {
            return true;
          } else {
            args.IsValid = true;
            alert("Please enter a valid email address in the following format username@domain.tld.");
            oValidatedControl.focus();
            return true;
          }
        } else {
          args.IsValid = false;
          alert("Please enter a valid email address in the following format username@domain.tld.");
          oValidatedControl.focus();
          return false;
        }
      }
      return bValidationTest;

}


