// General Validation JavaScript Script

function validateEmail(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.
*************************************************/
var objRegExp = /(^[a-z]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)([.][a-z]{3})$)|(^[a-z]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)([.][a-z]{2})$)|(^[a-z]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
  //check for valid email
  return objRegExp.test(strValue);
}

function validateFiscalCode(strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid Italian Fiscal Code pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp = /([A-Z,a-z]{6})([0-9]{2})([A-Z,a-z]{1})([0-9]{2})([A-Z,a-z]{1})([0-9]{3})([A-Z,a-z]{1})/i;

	return objRegExp.test(strValue);
}

function  validateNumeric( strValue ) {
/*****************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}

function validateDate(strValue, us) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
	strValue - String to be tested for validity
	us - US Format (true - false)

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else {
	  
    var strSeparator = strValue.substring(2,3);
    var arrayDate = strValue.split(strSeparator);
	
    //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}
						
	if (us) {
    	var intDay = parseInt(arrayDate[1],10);
		var intMonth = parseInt(arrayDate[0],10);
		var indexDay = 0; 
	} else {
		var intDay = parseInt(arrayDate[0],10);
		var intMonth = parseInt(arrayDate[1],10);
		var indexDay = 1; 
	}
	
    //check if month value and day value agree
    if(arrayLookup[arrayDate[indexDay]] != null) {
      if(intDay <= arrayLookup[arrayDate[indexDay]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
	
  }  
  return false; //any other values, bad date
}

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);
}

function isNull(val) { return (val == null); }
