/* =============================================================
Global Functions for form validation
Created:		Feb 16 2006
Last Mod:		Feb 16 2006
Note:			Much of this code originally from podlob.com, 
				which is now squidfingers.com.
================================================================= */

   function strip(filter,str){
      var i,curChar;
      var retStr = '';
      var len = str.length;
      for(i=0; i<len; i++){
         curChar = str.charAt(i);
         if(filter.indexOf(curChar)<0) 
           //not in filter, keep it
            retStr += curChar;
      }
      return retStr;
   }
   function reformat(str){
      var arg;
      var pos = 0;
      var retStr = '';
      var len = reformat.arguments.length;
      for(var i=1; i<len; i++){
         arg = reformat.arguments[i];
         if(i%2==1)
            retStr += arg;
         else{
            retStr += str.substring(pos, pos + arg);
            pos += arg;
         }
      }
      return retStr;
   }

	function validateUSPhone(str){
      str = strip("*() -./_\n\r\t\\",str);
      if(str.length == 10 || str.length == 7)
         return true;
      else
         return false;
	}
	function validateEMail(str){
     str = strip(" \n\r\t",str);
      if(str.indexOf("@") > -1 && str.indexOf(".") > -1)
         return true;
      else
         return false;
	}

function isEmail(str){ // returns true if the string is a valid email
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
function isAlpha(str){ // returns true if the string only contains characters A-Z or a-z
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}
function isNumeric(str){ // returns true if the string only contains characters 0-9
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
function wht(targ) { // changes error boxes back to white after user makes correction
	targ.style.background="white";
}
function validateInteger( strValue ) { // Validates that a string contains only valid integer number. 
  var objRegExp  = /(^-?\d\d*$)/;
  return objRegExp.test(strValue); //check for integer characters
}
function isAlphaNumeric(str){ // returns true if the string only contains characters A-Z, a-z or 0-9
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}
function isEmpty(str){ // returns true if the string is empty
	return (str == null) || (str.length == 0);
}
function isLength(str, len){ // returns true if the string's length equals "len"
	return str.length == len;
}
function isMinLength(str, minlen){ // returns true if the string's length equals or surpasses "min"
	return str.length >= minlen;
}
function isLengthBetween(str, min, max){ // returns true if the string's length is between "min" and "max"
	return (str.length >= min)&&(str.length <= max);
}
// returns true if a US phone number is formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}

function validateZip(str){
   str = strip("- \n\r\t",str);
   if(isNumeric(str) && (str.length==9 || str.length==5))
   return true;
   else
   return false;
}

	function validateCC(str,type){
      str = strip("-./_\n\r\t\\",str);
      if(type=="VS")
         if(str.charAt(0)!="4")
            return false;
      if(type=="MC")
         if(str.charAt(0)!="5")
            return false;
      if(type=="DV")
         if(str.charAt(0)!="6")
            return false;
      if(type=="AX")
         if(str.charAt(0)!="3")
            return false;
      if(validateInteger(str) && 
         ((str.length==15&&type=="AX") || str.length==16))
         return true;
      else
         return false;
   }

// returns true if the string is a valid date formatted as: mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	var y = parseInt(result[3]);
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}
	return (d >= 1 && d <= days);
}
// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
	return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}