﻿/*****************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************/
function  validateNumeric( strValue ) 
{
    var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

    //check for numeric characters
    return objRegExp.test(strValue);
}


/************************************************
DESCRIPTION: Validates that a string contains 
    only a valid integer number.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
**************************************************/
function validateInteger( strValue ) 
{
    var objRegExp = /(^-?\d\d*$)/;

    //check for integer characters
    return objRegExp.test(strValue);
}


/************************************************
DESCRIPTION: Validates that a string contains a
  valid URL pattern.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: the URL may begin with either http://www. 
or https://www. or ftp://www. or just www. once 
followed by any word character one or more times, 
followed by a dot and any number of word characters 
at least once but no more than twice.
*************************************************/
function validateUrl( strValue )
{
    //var objRegExp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
    var objRegExp = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    
    //check for valid URL
    return objRegExp.test(strValue);
}


/************************************************
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.
*************************************************/
function validateEmail( strValue ) 
{
   var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
   //some other possible email expressions.....
   //var objRegExp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
   //var objRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

   //check for valid email
   //return objRegExp.test(strValue);
   
   if (objRegExp.test(strValue))
   {
      //test email for illegal characters
      var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/
      if (strValue.match(illegalChars)) 
      {
          return false;
      }
   }
   return true;
}


/************************************************
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.
*************************************************/
function validateUSPhone( strValue ) 
{
    var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

    //check for valid us phone with or without space between
    //area code
    return objRegExp.test(strValue);
}


/************************************************
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.

*************************************************/
function validateUSZip( strValue ) 
{
    var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

    //check for valid US Zipcode
    return objRegExp.test(strValue);
}


/************************************************
DESCRIPTION: Trims trailing whitespace chars.

PARAMETERS:
   strValue - String to be trimmed.

RETURNS:
   Source string with right whitespaces removed.
*************************************************/
function rightTrim( strValue ) 
{
    var objRegExp = /^([\w\W]*)(\b\s*)$/;

    if(objRegExp.test(strValue))
    {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
    return strValue;
}


/************************************************
DESCRIPTION: Trims leading whitespace chars.

PARAMETERS:
   strValue - String to be trimmed

RETURNS:
   Source string with left whitespaces removed.
*************************************************/
function leftTrim( strValue )
{
    var objRegExp = /^(\s*)(\b[\w\W]*)$/;

    if(objRegExp.test(strValue)) 
    {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
    return strValue;
}


/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimAll( strValue )
{
    var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) 
    {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

    //check for leading & trailing spaces
    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
    if(objRegExp.test(strValue)) 
    {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
    return strValue;
}

/************************************************
DESCRIPTION: Determines if date is valid.

PARAMETERS: 3 integers: Month, Day and Year

RETURNS: true if date is valid, otherwise false.
*************************************************/
function IsValidDate(Mn,Day,Yr)
{
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);

    if(dt.getDate() != Day)
    {
        //alert('The Day of this date value is not valid.');
        return false;
    }
    else if(dt.getMonth() != Mn-1)
    {
        //this is for the purpose JavaScript starts the month from 0
        //alert('The Month of this date value is not valid.');
        return false;
    }
    else if(dt.getFullYear() != Yr)
    {
        //alert('The Year of this date value is not valid.');
        return false;
    }
        
    return true;
}
