/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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 ValidateInteger(strValue) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid integer number.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
**************************************************/
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}


function ValidateDateFormat(strValue,strDateFormat) {
/************************************************
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

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
  //alert(strValue);
  //alert(objRegExp.test(strValue));
//  if(!objRegExp.test(strValue))
//    return false; //doesn't match pattern, bad date
//  else{
    var strSeparator="";
    if(strDateFormat.indexOf("/", 0)>=0){
        strSeparator="/";
    }else{
        if(strDateFormat.indexOf("-", 0)>=0){
            strSeparator="-";
        }else{
            if(strDateFormat.indexOf(".", 0)>=0){
                strSeparator=".";
            }else{

            }
        }
    }
    
    var arrayDate = strValue.split(strSeparator);
    var arrayFormat=strDateFormat.split(strSeparator);

    var dtmDate;
    var dtmMonth;
    var dtmYear;
    for(var i=0;i<arrayFormat.length;i++){
        switch(arrayFormat[i].substr(0,1)){
            case "d":
                dtmDate=arrayDate[i];
                break;
            case "M":
                dtmMonth=arrayDate[i];
                break;
            case "y":
                dtmYear=arrayDate[i];
                break
        }
    }

    if(!ValidateInteger(arrayDate[0]) || !ValidateInteger(arrayDate[1]) || !ValidateInteger(arrayDate[2])){
        return false;
    }else{
    //alert(arrayDate[0]+","+arrayDate[1]+","+arrayDate[2]);
    //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}
    var intDay = parseInt(dtmDate,10);

    //check if month value and day value agree
    if(arrayLookup[dtmMonth] != null) {
      if(intDay <= arrayLookup[dtmMonth] && 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
    var intMonth = parseInt(dtmMonth,10);
    if (intMonth == 2) {
       var intYear = parseInt(dtmYear,10);
       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
}
