// JavaScript Document

function updateTips(t) {
	$("#tips").text(t).effect("highlight",{},1500);
}

function validate(){
	var bValid = true;
	
	bValid = bValid && checkLength( $("#Full_Name"), "Name", 2, 30 );
	bValid = bValid && checkRegexp( $("#Full_Name"),/^[a-zA-Z\s]{3,}$/, "Please ensure your name has only letters and spaces.");
	bValid = bValid && checkRegexp( $("#Email_Address"), /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "Please make sure your email conforms to proper standards. e.g. web@qualitystripingandsealcoat.com");
	bValid = bValid && checkRegexp( $("#Daytime_PhoneNumber"), /^(\(([0-9]){3}\)|([0-9]{3}(.|-)))([0-9]){3}(.|-)([0-9]){4}/,
	"Please make sure your phone is formatted for (xxx) xxx-xxxx or xxx.xxx.xxxx");
	
	return bValid;
	
}

function checkLength(o,n,min,max) {

	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		updateTips("Length of " + n + " must be between "+min+" and "+max+".");
		return false;
	} else {
		return true;
	}

}

function checkRegexp(o,regexp,n) {

	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		updateTips(n);
		return false;
	} else {
		return true;
	}

}

$(document).ready(function(){

	// prepare Options Object 
	var options = { 
		target:     '#form_wrapper',
		beforeSubmit: validate
	}; 
	$("#EMAIL_FORM").ajaxForm(options);
		
});
