// 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 = /[^a-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 is a US phone number with any format.
function isPhoneNumber(str) {
	var phone = stripNonNumeric(str);
	return isLength(phone, 10);
}
// 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;
}
// Removes any non-numeric characters
function stripNonNumeric(str) {
	var result = new String(str);
	return result.replace(/[^0-9]/g, '');
}

function validate_contact_form() {
	var form = document.forms['form_contact'];
	var first_name = form['first_name'].value;
	var last_name = form['last_name'].value;
	var phone = form['phone'].value;
	var email = form['email'].value;
	var state = form['state'].value;
	var program = form['program'].value;
	var errors = '';
	if (isEmpty(first_name)) errors += 'Please enter your first name.\n';
	if (isEmpty(last_name)) errors += 'Please enter your last name.\n';
	if (isEmpty(phone)) {
		errors += 'Please enter your phone number.\n';
	}
	else if (!isPhoneNumber(phone)) {
		errors += 'Please enter a valid phone number. (###) ###-####\n';
	}
	if (isEmpty(email)) {
		errors += 'Please enter your email address.\n';
	}
	else if (!isEmail(email)) {
		errors += 'Please enter a valid email address.\n';
	}
	if (isEmpty(state) || !isLength(state, 2)) errors += 'Please select the state where you live.\n';

	if (errors != '') {
		alert(errors);
		return false;
	}
	return true;
}
			
