///////////////////////////////////////////////////////////////////////
//  Function List
//	"value" stands for the form element value to be passed
//
//		isLength(value,length)
//			- validates string to be less than number passed in
//			- returns true or false
//
//		isSelected(value)
//			- validates select value to not be zero
//			- returns true or false
//		isEmail(value)
//			- validates email address
//			- returns true or false
//		isTelephone(value)
//			- validates telephone numbers in the format (999)999-9999 and extensions are optional
//			- returns true or false
//		isLetters(value)
//			- validates value to have only characters
//			- returns true or false
//		isZip(value)
//			- validates zip codes with 5 digits or 9 digits with the -
//			- returns true or false
//		isSSN(value)
//			- validates format of a Social Security Number
//			- returns true or false
//		isCurrency(value)
//			- validates field to have the format $999,999.99
//			- returns true or false
//		isCreditCard(type, value)
//			- must pass type of credit cards values are Visa, MC, Disc, AmEx, and Diners
//			- validates beginning values for each type as well as legnth and performs mod 10 validation
//			- returns true or false
//		isNumeric(value)
//			- Validates numberic value, can be negitive and be float
//			- returns true or false
//		isInteger(value)
//			- validates value to be whole pasitive number
//			- returns true or false
//		isDate(value)
//			- validates dates between 1900 and 2100, also validates leap year
//			- date string must be seperated by /
//			- returns true or false
//
///////////////////////////////////////////////////////////////////////
// Check string legnth
function isLength(string,length) {
	if (string.length <= length)
		return true;
	else
		return false;
}
// Select Box check
function isSelected(string) {
	if (string == 0) { return false;}
	else return true;
}
// Email check
function isEmail(string) {
	if (string == 0) { return true;}
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}
// Telephone check
function isTelephone(string) {
	if (string.length == 0) { return true;}
	expression = '^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$';
	if (string.search(expression) != -1)
		return true;
	else
		return false;
}

// All Characters check
function isLetters(string) {
	if (string.length == 0) { return true;}
	if (string.search(/^([a-zA-Z]+\s?)+$/) != -1)
		return true;
	else
		return false;
}
// Zip Code check
function isZip(string) {
	if (string.length == 0) { return true;}
	if (string.search(/^\d{5}$|^\d{5}[\-\s]?\d{4}$/) != -1)
		return true;
	else
		return false;
}
// Social Security Number check
function isSSN(string) {
	if (string.length == 0) { return true;}
	if (string.search(/^\d{3}-\d{2}-\d{4}$/) != -1)
		return true;
	else
		return false;
}
// Currency check
function isCurrency(string) {
	if (string.length == 0) { return true;}
	if (string.search(/\$\d{1,3}(,\d{3})*\.\d{2}/) != -1)
		return true;
	else
		return false;
}
// Credit Card Check
function isCreditCard(type, string) {
	if (string.length == 0) { return true;}
	if (type == "Visa") {
	// Visa: length 16, prefix 4, dashes optional.
	var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(string)) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(string.length % 2)); i<=string.length; i+=2) {
      checksum += parseInt(string.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(string.length % 2) + 1; i<string.length; i+=2) {
      var digit = parseInt(string.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}
// Check Numeric
function IsNumeric(string) {
	if (string.length == 0) { return true;}

	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (string.length == 0) return false;

	for (i = 0; i < string.length && blnResult == true; i++){
		strChar = string.charAt(i);
	if (strValidChars.indexOf(strChar) == -1){
		blnResult = false;
		}
	}
	return blnResult;
}
// Integer Check
function isInteger(s){
	if (s.length == 0) { return true;}
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
// Date Check
function isDate(dtStr){
	if (dtStr.length == 0) { return true;}
	var dtCh = '/';
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<1900 || year>2100){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}