/****************************************************************
  VALIDATION FUNCTIONS:
  isEmail      - is email address?
  isPhone      - is US phone number?
  isNumeric    - is numeric value?
  isInteger    - is integer value?
  isNotEmpty   - is blank form field?
  isZip        - is US zip code?
  isDate       - is date in US format?

  FORMAT FUNCTIONS:
  rtrim        - remove trailing spaces from a string
  ltrim        - remove leading spaces from a string
  trim         - remove leading and trailing spaces from a string
  killCurrency - remove currency formatting characters (), $
  addCurrency  - insert currency formatting characters
  killCommas   - remove comma separators from a number
  addCommas    - adds comma separators to a number

*******************************************************************/

function isEmail( 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;
  //check for valid email
  return objRegExp.test(strValue);
}

function isPhone( strValue ) {
  /* (999) 999-9999 or (999)999-9999 */
  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);
}

function isNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  return objRegExp.test(strValue);
}

function isInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;
  //check for integer characters
  return objRegExp.test(strValue);
}

function isNotEmpty( strValue ) {
  var strTemp = strValue;
  strTemp = trim(strTemp);
  if(strTemp.length > 0) { return true; }
  return false;
}

function isZip( strValue ) {
  var objRegExp  = /(^\d{5}$)/;
  return objRegExp.test(strValue);
}

function isDate( strValue ) {
  // mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
  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}
    var intDay = parseInt(arrayDate[1],10);
    //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 (leap year)
    var intMonth = parseInt(arrayDate[0],10);
    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 rtrim( strValue ) {
  var objRegExp = /^([\w\W]*)(\b\s*)$/;
  if(objRegExp.test(strValue)) {
    //remove trailing a whitespace characters
    strValue = strValue.replace(objRegExp, '$1');
  }
  return strValue;
}

function ltrim( strValue ) {
  var objRegExp = /^(\s*)(\b[\w\W]*)$/;
  if(objRegExp.test(strValue)) {
    //remove leading a whitespace characters
    strValue = strValue.replace(objRegExp, '$2');
  }
  return strValue;
}

function trim( 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;
}

function killCurrency( strValue ) {
  var objRegExp = /\(/;
  var strMinus = '';
  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }
  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function addCurrency( strValue ) {
  // Assumes number passed is numeric value rounded to 2 decimal s
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;
  if( objRegExp.test(strValue)) {
    objRegExp.compile('^-');
    strValue = addCommas(strValue);
    if (objRegExp.test(strValue)) { strValue = '(' + strValue.replace(objRegExp,'') + ')'; }
    return '$' + strValue;
  }
  else { return strValue; }
}

function killCommas( strValue ) {
  var objRegExp = /,/g; //search for commas globally
  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}

function addCommas( strValue ) {
  // Inserts commas into numeric string.
  // Used with integers or numbers with 2 or less decimal places.
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');
  //check for match to search criteria
  while(objRegExp.test(strValue)) {
    //replace original string with first group match,
    //a comma, then second group match
    strValue = strValue.replace(objRegExp, '$1,$2');
  }
  return strValue;
}

