// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^\sa-zA-Z0-9-,.]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string's length equals "len"
function isLength(str, len){
	return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}
// returns true if the string's length is "min"
function isLengthMin(str, min){
	return (str.length >= min);
}

// returns true if the string is a US phone number 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);
}

// 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);
//}
//--------------------------------------
var dtCh="/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	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 = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to 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){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    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
}

function isDate(dtStr){
	dtCh="/";
	var strMonthAux
	var strDayAux
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	if (pos1==-1 || pos2==-1){
		dtCh= "-";
		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)
	var strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDayAux=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonthAux=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	var month=parseInt(strMonthAux)
	var day=parseInt(strDayAux)
	var year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length != 2 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length != 2 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}
//--------------------------------------

// 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;
}
// validate the form
function validateForm(f, preCheck){
	var errors = '';
	if(preCheck != null) errors += preCheck;
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.name;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				errors += n+' cannot be empty.\n'; continue;
			}
			if(v == e.defaultValue){
				errors += n+' cannot use the default value.\n'; continue;
			}
			if(e.isAlpha){
				if(!isAlpha(v)){
					errors += n+' can only contain characters A-Z a-z.\n'; continue;
				}
			}
			if(e.isNumeric){
				if(!isNumeric(v)){
					errors += n+' can only contain characters 0-9.\n'; continue;
				}
			}
			if(e.isAlphaNumeric){
				if(!isAlphaNumeric(v)){
					errors += n+' can only contain characters A-Z a-z 0-9.\n'; continue;
				}
			}
			if(e.isEmail){
				if(!isEmail(v)){
					errors += v+' is not a valid email.\n'; continue;
				}
			}
			if(e.isLength != null){
				var len = e.isLength;
				if(!isLength(v,len)){
					errors += n+' must contain only '+len+' characters.\n'; continue;
				}
			}
			if(e.isLengthBetween != null){
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)){
					errors += n+' cannot contain less than '+min+' or more than '+max+' characters.\n'; continue;
				}
			}
			if(e.isLengthMin != null){
				var len = e.isLengthMin;
				if(!isLengthMin(v,len)){
					errors += n+' must contain at least '+len+' characters.\n'; continue;
				}
			}
			if(e.isPhoneNumber){
				if(!isPhoneNumber(v)){
					errors += v+' is not a valid phone number [(000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000].\n'; continue;
				}
			}
			if(e.isDate){
				if(!isDate(v)){
					errors += v+' is not a valid date.\n'; continue;
				}
			}
			if(e.isMatch != null){
				if(!isMatch(v, e.isMatch)){
					errors += n+' does not match.\n'; continue;
				}
			}
		}
		if(t.indexOf('select') != -1){
			if(isEmpty(e.options[e.selectedIndex].value)){
				errors += n+' needs an option selected.\n'; continue;
			}
		}
		if(t == 'file'){
			if(isEmpty(v)){
				errors += n+' needs a file to upload.\n'; continue;
			}
		}
	}
	if(errors != '') alert(errors);
	return errors == '';
}