//generic form validation functions
//
//parameters:     strValue - string to trim of spaces
//                theformobject - form element to check

function trim_string( 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 and remove
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validate_text(theformobject){
  strTemp = trim_string(theformobject.value);
  if(strTemp.length <= 0){   
    alert("Invalid entry!");
    theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }    
}

function validate_date(theformobject){
theDate = new Date(theformobject.value);
  if (isNaN(theDate)){
    alert("Invalid date! Please enter mm/dd/yyyy");
    theformobject.focus();   
    return false;
  }else{
    var theDateStr = (theDate.getMonth()+1) + "/" + theDate.getDate() + "/" + theDate.getFullYear();
    theformobject.value = theDateStr;
    return true;
  }

}

function validate_number(theformobject){
  var strTemp = trim_string(theformobject.value);
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  if (!objRegExp.test(strTemp)){
    alert("Invalid number!");
    theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }

}

function validate_integer(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /(^-?\d\d*$)/;
 if (!objRegExp.test(strTemp)){
  alert("Invalid integer!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}

function validate_currency(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /(^\$\d{1,3}(,\d{3})*\.\d{2}$)|(^\(\$\d{1,3}(,\d{3})*\.\d{2}\)$)/;
 if (!objRegExp.test(strTemp)){
  alert("Invalid Currency!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}

function validate_time(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d)?$/;
 if (!objRegExp.test(strTemp)){
  alert("Invalid integer!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}


function validate_SSN(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /^\d{3}\-\d{2}\-\d{4}$/;
 if (!objRegExp.test(strTemp)){
  alert("Invalid SSN!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}

function validate_Email(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /(^[a-z]([a-z_\-\d\.]*)@([a-z_\-\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\-\d\.]*)@([a-z_\-\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
 if (!objRegExp.test(strTemp)){
  alert("Invalid Email!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}


function validate_Phone(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
 if (!objRegExp.test(strTemp)){
  alert("Invalid Phone Number!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}

function validate_Phone2(theformobject){
 var strTemp = trim_string(theformobject.value);
 var objRegExp  = /^\D*[1-9]\d{2}\D*\s?\d{3}\D*\d{4}$/;
 if (!objRegExp.test(strTemp)){
  alert("Invalid Phone Number!");
  theformobject.focus();   
    return false;
  }else{
    theformobject.value = strTemp;
    return true;
  }
}
