/***********************************************************************
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/																												_/
_/																												_/
_/									Snunit - Environment Wizard Tool							_/
_/									~~~~~~~~~~~~~~~~~~~~~~~~~~~							_/
_/																												_/
_/												validateForm.js											_/
_/					יייייייייייייייייייייייייייייייייייייייייייייייייייייייייייייי					_/
_/																												_/
_/																												_/
_/	Description:	This file contains a library of functions for form validation		_/			
_/	~~~~~~~~~	using Regexp.																	_/
_/																												_/
_/ Author:			Julia Zilber																			_/			
_/	~~~~~~																									_/
_/																												_/
_/ Date:			15/12/2004																			_/			
_/	~~~~																										_/
_/																												_/
_/	Functions List: 																						_/
_/ ~~~~~~~~~~~~																						_/	
_/			validateTime() - Checks for valid 24 hours time format						_/
_/			validateDate() - Checks for valid date of D(D)/M(M)/YYYY format		_/
_/			validateWeekDay() - Checks for valid Week Day in Hebrew				_/
_/			validateYear() - Checks for valid Year	(4 digits,  at most 2010)			_/
_/			validateBirthYear() - Checks for valid Year (valid Year, at least 1905)	_/
_/			validatePhone() - Checks for valid phone number								_/
_/			validateCellular() - Checks for valid cell phone number						_/
_/			validateZip() - Checks for valid zip code											_/
_/			validateEmail() - Checks for valid email address								_/
_/			validateURL() - Checks for valid URL												_/
_/			validateNumeric() - Checks for valid numeric value							_/
_/			validateInteger() - Checks for valid integer										_/
_/			validateEnglish() - Checks for valid string in english							_/
_/			validateHebrew() - Checks for valid string in hebrew							_/
_/			validateEngWord() - Checks for valid word in english							_/
_/			validateHebWord() - Checks for valid word in english						_/
_/			validateUsername() - Checks for valid username								_/
_/			validateImage() - Checks for valid image file, besides .bmp				_/
_/			validateColor() - Checks for valid hexadecimal color code					_/
_/			validateNonEmpty() - Checks for not empty field							_/
_/			validateValue() - Checks according to given pattern							_/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

***********************************************************************/

/*############################### Validate Time ###############################*/
function validateTime ( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or contains a 
  valid 24 hour time format. Seconds are optional.
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

REMARKS: Returns True for time formats such as:
  HH:MM or HH:MM:SS or HH:MM:SS
*************************************************/
   if (! strValue)
	  return true;
  var objRegExp = /^(\d|1\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/;

  return objRegExp.test( strValue );
}

/*############################### Validate Date ###############################*/


function validateDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or contains only 
    valid dates with  digit day,  digit month, 
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. dd/mm/yyyy or dd-mm-yyyy or dd.mm.yyyy
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
   if (! strValue)
	  return true;
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}(\-|\/|\.)\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[0],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[1],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

/*############################### Validate Date ###############################*/
function validateWekDay( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or one of the Week Days in hebrew.
	True value is returned for "א","ב","ג","ד","ה","ו","ש"
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
 if (! strValue)
	  return true;
var objRegExp = /[אבגדהוש]/;
  return objRegExp.test( strValue );
}

/*############################### Validate Year ###############################*/
function validateYear( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or represents a valid year
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
 if (! strValue)
	  return true;
var intYear = parseInt(strValue,10); 
  return  (intYear>=1000 && intYear < 2010);
}

/*############################### Validate Birth Year ###############################*/
function validateYear( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or represents a valid birth year.
	The earliest birth year is assigned to 1905, the latest - 2004
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
 if (! strValue)
	  return true;
var intYear = parseInt(strValue,10); 
  return  (intYear>=1905 && intYear <= 2004);
}

/*############################### Validate Phone Number ###############################*/

function validatePhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or 
contains valid Israeli phone pattern. 
  Ex. 02-6777777 or 972-2-6777777
  
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
 if (!strValue)
	  return true;
  var objRegExp  = /^(0\d\-|\+?972\-\d\-)\d{7}$/;
 
  //check for valid us phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}

/*############################### Validate Cellular Phone Number ###############################*/

function validateCellular( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or 
contains valid Israeli cellular phone pattern. 
  Ex. 054-6777777
  
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
 if (! strValue)
	  return true;
  var objRegExp  = /^0\d\d\-\d{7}$/;
 
  //check for valid cellular phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}

/*############################### Validate Zip Code ###############################*/

function validateZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty 
or contains valid Israeli zip code pattern

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
  if (! strValue)
	  return true;
  var objRegExp  = /^(\d){5}$/;
 
  //check for valid us phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}

/*############################### Validate Cellular Phone Number ###############################*/

function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string is empty or 
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.) and optionally,
  a valid country suffix.  Since email has many
  forms this expression only tests for near valid
  address.  Some additional validation may be
  required.
*************************************************/
 if (! strValue)
	  return true;
var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(strValue);
}

/*############################### ValidateURL ###############################*/
function validateURL( strValue) {
/************************************************
DESCRIPTION: Validates that a string is empty 
or contains a valid URL pattern. 
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS: 
  does not validate valid URL type 
  (.com, .gov, etc.) and optionally, a valid
  country suffix.  Since URLmauy have many
  forms this expression only tests for near valid
  address.  Some additional validation may be
  required.
*************************************************/
 if (! strValue)
	  return true;
var objRegExp  = /^http:\/\/[a-z0-9]([a-z0-9_\-]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
//var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(strValue);
}

/*############################### Validate Numeric Value ###############################*/
function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string is empty 
or contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
   if (! strValue)
	  return true;
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
  //check for numeric characters 
  return objRegExp.test(strValue);
}

/*############################### Validate Integer ###############################*/
function validateInteger( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or
contains only valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
   if (! strValue)
	  return true;
  var objRegExp  = /(^-?\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate English String###############################*/
function validateEnglish( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    letters [A-Za-z], spaces or new line separators.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /^([A-Za-z]|\s|\n|\t)*$/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate English Word###############################*/
function validateEngWord( strValue ) {
/************************************************
DESCRIPTION: Validates that a string only 
   a word in english
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
   var objRegExp  = /^[A-Za-z]*$/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate Hebrew String###############################*/
function validateHebrew( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or contains only 
    letters א-ת, spaces or new line separators.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
   if (! strValue)
	  return true;
  var objRegExp  = /^([א-ת]|\s|\n|\t)*$/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate Hebrew Word###############################*/
function validateHebWord( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or 
   contains only a word in hebrew
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
   if (! strValue)
	  return true;
  var objRegExp  = /^[א-ת]*$/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate Username###############################*/
function validateUsername( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or contains only 
   a word consisting of letters, numbers, underscores, 
   4 to 10 characters
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
   if (! strValue)
	  return true;
  var objRegExp  = /^\w{4,10}$/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate Image File###############################*/
function validateImage( strValue ) {
	
/************************************************
DESCRIPTION: Validates that a string contains is empty or only 
   a valid image file, one of the following types:

    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  if (! strValue)
	  return true; 
  var objRegExp  = /\.(jpg|jpeg|jpe|png|gif|)$/i;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate Color Code###############################*/
function validateColor( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is empty or contains only 
   a valid hexadecimal color code

    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  if (! strValue)
	  return true;
  var objRegExp  =  /^#?([0-9a-f]{1,2}){3}$/i; 
 
  //check for integer characters
  return objRegExp.test(strValue);
}

/*############################### Validate Non-empty value###############################*/
function validateNonEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);

   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

/*############################### Validate value###############################*/
function validateValue( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Validates that a string a matches
  a valid regular expression value.
    
PARAMETERS:
   strValue - String to be tested for validity
   strMatchPattern - String containing a valid
      regular expression match pattern.
      
RETURNS:
   True if valid, otherwise false.
*************************************************/
var objRegExp = new RegExp( strMatchPattern);
 
 //check if string matches pattern
 return objRegExp.test(strValue);
}

/*###########################Trim all #############################*/
function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 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;
}

