
// Created By: Shawn Bullock
//  3/29/2003
//
var DEBUG_ASSERT_VALIDATION = 0;


function PhoneNumberTypes() {
   this.International = -1;
   this.Automatic = 0;
   this.USA = 1;
}
PhoneNumberTypes = new PhoneNumberTypes();

function ZipCodeTypes() {
   this.Automatic = 0;
   this.USA = 1;
}
ZipCodeTypes = new ZipCodeTypes();

function DateFormat() {
   this.Automatic = 0;
   this.mm_dd_yy = 1;
   this.mm_dd_yyyy = 2;
}
DateFormat = new DateFormat();

function CaseOptions() {
   this.Automatic = 0;
   this.CaseSensitive = 1;
   this.CaseInsensitive = 2;
}
CaseOptions = new CaseOptions();

function NationalIDType() {
   this.Automatic = 0;
   this.USA_SSN = 1;
   this.USA_EIN = 2;
}
NationalIDType = new NationalIDType();

function NumberFormat() {
   this.Automatic = 0;
   this.NoCommas = 1;
   this.HasCommas = 2;
   this.Scientific = 3;
   this.NoCommasWithDecimal = 4;
   this.HasCommasWithDecimal = 5;
}
NumberFormat = new NumberFormat();

function CurrencyOptions() {
   this.Automatic = 0;
   this.RequiresDecimal = 1;
}
CurrencyOptions = new CurrencyOptions();


function IsExpectedLength(value, length)
{
   if (value == null) {
      return false;
   }

   if ((value.length <= length) && length > 0) {
      return true;
   }

   return false;
}


function IsExactLength(value, length) 
{
   if (value == null) {
      return false;
   }

   if (value.length == length) {
      return true;
   }

   return false;
}

function IsMinimumLength(value, length)
{
   if (value == null) {
      return false;
   }

   if (length < 0) {
      return false;
   }
   
   if (value.length >= length) {
      return true;
   }
   
   return false;
}


// Validate if the given string is a phone number
//
//   : ###-###-####
//   : ### ###-####
//   : (###) ###-####
//   : (###)-###-####
//
function IsPhoneNumber(value, type)
{      
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert(type);
   }
   
   switch (type)
   {
      case PhoneNumberTypes.USA:
         return /^(\(\d{3}\)|\d{3})(\s?|-)\d{3}-\d{4}$/.test(value);
      
      case PhoneNumberTypes.International:
         return /^\d(\d|-){7,20}/.test(value);
         
         
      case PhoneNumberTypes.Automatic:
         if (IsPhoneNumber(value, PhoneNumberTypes.USA)) {
            return true;
         }
         else if (IsPhoneNumber(value, PhoneNumberTypes.International)) {
            return true;
         }
         break;
    
   }
        
   return false;
}


// Validate if teh given string is a postal code
//
function IsPostalCode(value, type)
{
   return IsZipCode(value, type);
}


// Validate if the given string is a Zip Code
//
//  USA:
//
//   : #####
//   : #####-####
//
function IsZipCode(value, type)
{
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert(type);
   }
   
   switch (type)
   {
      case ZipCodeTypes.USA:
         return /^\d{5}(-\d{4})?$/.test(value);
         
         
      case ZipCodeTypes.Automatic:
         if (IsZipCode(value, ZipCodeTypes.USA)) {
            return true;
         }
   }
   
   return false;
}


// Validate if the given string is an email address
//
//   : someone@somewhere.com / .net / .mil / .gov / .org
//   : someone@somewhere.co.uk
//   : someone@somewhere.info
//
function IsEmail(value)
{
   return /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(value);
}


// Validate if the given string is an IP address
//
//   : ###.###.###.### (### cannot be greater thatn 255)
//
function IsIPAddress(value)
{
   return /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/.test(value);
}


// Validate if the given string is a Date
//
//   : ##/##/##
//   : ##/##/####
//   : ##-##-##
//   : ##-##-####
//   : ##.##.##
//   : ##.##.####
//
function IsDate(value, type) 
{
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert(type);
   }
   
   switch (type) 
   {
      case DateFormat.mm_dd_yy:
         return /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/.test(value);

      case DateFormat.mm_dd_yyyy:
         return /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test(value);


      case DateFormat.Automatic:
         if (IsDate(value, DateFormat.mm_dd_yy)) {
            return true;
         }
         else if (IsDate(value, DateFormat.mm_dd_yyyy)) {
            return true;
         }
         break;
   }

   return false;
}


// Validate if the given string is a State abbr.
//
//   : @@
//
function IsStateUSA(value, options)
{
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert(options);
   }
   
   if (IsExactLength(value, 2)) {      
      switch (options) 
      {
         case CaseOptions.CaseSensitive:
            return /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/.test(value);

         case CaseOptions.CaseInsensitive:
            return /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i.test(value);


         case CaseOptions.Automatic:
            return IsStateUSA(value, CaseOptions.CaseInsensitive);

      }
   }
   
   return false;
}


// Validate if the value is a specific type of ID pertaining to a particular
//  nationality.
//
function IsNationalID(value, type)
{
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert(type);
   }
   
   switch (type)
   {
      case NationalIDType.USA_SSN:
         if (IsExactLength(value, 9) || IsExactLength(value, 11)) {
            return /^\d{3}\-\d{2}\-\d{4}$/.test(value);
         }
         break;
         
      case NationalIDType.USA_EIN:
         return /^[0-9]{2}-[0-9]{7}$/.test(value);
         
         
      case NationalIDType.Automatic:
         if (IsNationalID(value, NationalIDType.USA_SSN)) {
            return true;
         }
         else if (IsNationalID(value, NationalIDType.USA_EIN)) {
            return true;
         }
         break;
   
   }

   return false; 
}


// Validate if the given string is a SSN
//
//   : ###-##-####
//
function IsSSN(value)
{
   return IsNationalID(value, NationalIDType.USA_SSN);
}


// Validate if the given string is a time
//
//   : ##:##
//
function IsTime(value)
{
   return /^([1-9]|1[0-2]):[0-5]\d$/.test(value);
}


// Validate if the string has any potential malicious scripting code, assuming
//  this is a querystring.
//
function IsSafeQuerystring(value) 
{
   if (value == null || value == '') {
      return true;
   }
   
   var Result =  /(<script>|<\/script>|;|\{|\}|')/i.test(value);
   return !Result;
}


// Validate if the string is number
//
//   : ########
//   : ###,###
//   : #####.###
//   : ###,###.##
//   : +##.##
//   : -##.##
//   : +###.e+##
//   : etc.
//
function IsNumber(value, type)
{
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert(type);
   }
   
   if (value == null) {
      return false;
   }
   
   switch (type) 
   {
      case NumberFormat.NoCommas:
         return /^(\d+)$|^\-(\d+)$|^\((\d+)\)$/.test(value);

      case NumberFormat.HasCommas:
         return /^(\d{1,3}(\,\d{3})*)$|^\-(\d{1,3}(,\d{3})*)$|^\((\d{1,3}(,\d{3})*)\)$/.test(value);

      case NumberFormat.Scientific:
         return /^[-+]{1}?\d*(\.\d+|\d*)(e[+-]?\d+)?$/.test(value);

      case NumberFormat.NoCommasWithDecimal:
         return /^\d+(\.\d+)$|^\-\d+(\.\d+)$|^\(\d+(\.\d+)\)$/.test(value);

      case NumberFormat.HasCommasWithDecimal:
         return  /^\d{1,3}(,\d{3})*(\.\d+)$|^\-\d{1,3}(,\d{3})*(\.\d+)$|^\(\d{1,3}(,\d{3})*(\.\d+)\)$/.test(value);
   

      case NumberFormat.Automatic:
         if (IsNumber(value, NumberFormat.NoCommas)) {
            return true;
         }
         else if (IsNumber(value, NumberFormat.HasCommas)) {
            return true;
         }
         else if (IsNumber(value, NumberFormat.Scientific)) {
            return true;
         }
         else if (IsNumber(value, NumberFormat.NoCommasWithDecimal)) {
            return true;
         }
         else if (IsNumber(value, NumberFormat.HasCommasWithDecimal)) {
            return true;
         }
         break;
   }

   return false;
}


// Validate if the given string is currency
//
//   : $###,###.##
//
function IsCurrency(value, precision)
{
   var Result;
   
   if (DEBUG_ASSERT_VALIDATION == 1) {
      alert('IsCurrency: Precision := ' + precision);
   }
   
   if (value == null) {
      return false;
   }

   if (precision < 0) {
      return false;
   }
   else if (precision == 0) {
      precision = '1,';
   }
   
   Result = new RegExp("^\\$\\d{1,3}(,\\d{3})*\\.\\d{" + precision + "}|^\\-\\$\\d{1,3}(,\\d{3})*\\.\\d{" + precision + "}|^\\(\\$\\d{1,3}(,\\d{3})*\\.\\d{" + precision + "}\\)");
   return Result.test(value);
}


// Validate if the string is alphanumeric (spaces not accepted, instead, use _)
//
function IsAlphaNumeric(value)
{
   if (value == null) {
      return false;
   }
   
   return /^([a-zA-Z\d_]+)$/.test(value);
}


// Validate if the string is alphanumeric (spaces not accepted, instead use _) for
//  querystring.  % symbol must be accepted, as well as a few others. Also, as an
//  added bonus, it will return false if any of the JavaScript/JScript function 
//  characters are found, { } ; <></> as in <script> </script>.  But to check
//  for malicious code specifically, use IsSafeQuerystring instead.
//
function IsAlphaNumericQS(value)
{
   if (value == null) {
      return false;
   }

   return /^([a-zA-Z\d_\-%#@!:^~\$+]+)$/.test(value);
}


// Validate if the string is alpha characters only
//
function IsAlpha(value)
{
   if (value == null) {
      return false;
   }

   return /^([a-zA-Z_\s]+)$/.test(value);
}


// Validate if the supplied string matches a certain criteria, for example, if
//  a UserName meets conditions, or a Password meets conditions...
//
function IsCriteriaAcceptable( 
      value, 
      minCaps, 
      minSpecialChars, 
      minNumbers, 
      minLength,
      maxLength
   )
{
   
   var Result;
   
   // First check and ensure the minimum length is met
   //
   if (!IsMinimumLength(value, minLength)) {
      return false;
   }
   
   // Make sure the text isn//t more than the maximum length specified
   //
   if (value.length > maxLength) {
      return false;
   }

   // Check is see if there are minimum number of caps supplied
   //
   Result = new RegExp("([A-Z].*){" + minCaps + "}");
   if (!Result.test(value)) {
      return false;
   }

   // Check to see if there are minimum number of numbers supplied
   //
   Result = new RegExp("([0-9].*){" + minNumbers + "}");
   if (!Result.test(value)) {
      return false;
   }
   
   // Check to see if there are minimum number of special characters supplied
   //
   Result = new RegExp("([_~!@#\$%^&\-_+=;:\.,].*){" + minSpecialChars + "}");
   if (!Result.test(value)) {
      return false;
   }

   return true;
}


// Validate if the value is a variant of boolean.  Case insensitive.
//
function IsBoolean(value)
{
   if (value == null) {
      return false;
   }

   return /^(1|0)$|^(TRUE|FALSE)$|^(YES|NO)$|^(ON|OFF)$/i.test(value);
}


// Validate gender.  Case insensitive.
//
function IsGender(value)
{
   if (value == null) {
      return false;
   }

   return /^(MALE|FEMALE)$|^(M|F)$|^(1|0)$/i.test(value);
}


// Validate if the value is USA Employer TaxID Number.
//
function IsEIN(value)
{
   return IsNationalID(value, NationalIDType.USA_EIN);
}