/////////////////////////
//validate.js
//Written on May 11, 2008 
//Written by Andrew Grant
//fromether@gmail.com
/////////////////////////

//validation regexes:
var NATelephoneRegex = /\(?[2-9]\d\d\)?[ -]?[2-9]\d\d[ -]?\d{4}/; //matches 10 digit North American phone numbers - optional () and -
var emailRegex = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i; //simple, effective email matching
var NAPostalRegex = /^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/; //matches US and Canadian Zip/Postal
var IPv4Regex = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/; //matches 0-255 in groups, seperated by .
var hexColourRegex = /^#(?:(?:[a-f\d]{3}){1,2})$/i; //matches #333, 333, #333333, 333333
var mmddyyRegex = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //in MM/DD/YYYY format

function showEmpty(elemID, origStyle, errorStyle){
	var rv = false;
	if(isEmpty(elemID)){
		element = document.getElementById(elemID);
		element.setAttribute("style", errorStyle);
		rv = true;
	} else {
		element.setAttribute("style", origStyle);
	}
	return rv;
}

function isEmpty(elemID){
	var rv = false;
	element = document.getElementById(elemID);
	if(element.value.length == 0){
		rv = true;
	}
	return rv;
}

function checkEmail( elemID ) {
	return emailRegex.test( document.getElementById(elemID).value );
}
function checkTelephone( elemID ) {
	return NATelephoneRegex.test( document.getElementById(elemID).value );
}
function checkPostal( elemID ) {
	return NAPostalRegex.test( document.getElementById(elemID).value );
}