var REGEX_FIRSTNAME = /^[a-zA-Z0-9_\. \-\/]{2,50}$/;
var REGEX_LASTNAME = /^[a-zA-Z0-9_\. \-\/]{2,50}$/;
var REGEX_ADDRESS = /^[a-zA-Z0-9\.\#\' \-\(\)]{2,200}$/;
var REGEX_CITY = /^[a-zA-Z0-9\.\' \-\(\)]{2,99}$/;
var REGEX_STATE = /^[a-zA-Z0-9\.\' \-\(\)]{2}$/;
var REGEX_ZIP = /^[a-zA-Z0-9\-]{3,10}$/;
var REGEX_PHONE = /^[0-9\-\.\ \()?]{10,15}$/;
var REGEX_EMAIL = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

function FixPhoneNum(str){
	if (str != null && str.length > 0)
	{
		var n = str.match(/[0-9]+/g);
		
		if (n != null)
			n = n.join("");
		
		if (n != null && n.length > 10 && n.substr(0,1) == 1)
			n = n.substr(1);
		
		return n;
	} else {
		return "";	
	}
}
	
$(document).ready(function() {
	// Add onclick handler to checkbox
	$('.formfield').focus(function() {
		$(this).addClass('formFieldActive');
	}).blur(function () {
		$(this).removeClass('formFieldActive');
	});
	
	$("#form_requestkit").submit(function() {
		var _error = "";
		
		if (!REGEX_FIRSTNAME.test($("#first_name").val()))
		{
			_error += "Invalid first name.\r\n";
		}
		if (!REGEX_LASTNAME.test($("#last_name").val()))
		{
			_error += "Invalid last name.\r\n";
		}
		if (!REGEX_ADDRESS.test($("#address").val()))
		{
			_error += "Invalid address.\r\n";
		}		
		if (!REGEX_CITY.test($("#city").val()))
		{
			_error += "Invalid city.\r\n";
		}		
		if (!REGEX_STATE.test($("#state").val()))
		{
			_error += "Invalid state.\r\n";
		}		
		if (!REGEX_ZIP.test($("#zip").val()))
		{
			_error += "Invalid zip.\r\n";
		}
		
		$("#phone").val(FixPhoneNum($("#phone").val()));
		if (!REGEX_PHONE.test($("#phone").val()))
		{
			_error += "Invalid phone number.\r\n";
		}
		
		if (!REGEX_EMAIL.test($("#email").val()))
		{
			_error += "Invalid email address.\r\n";
		}
		
		if ($("#term").attr('checked') != true)
		{		
			_error += "Please Agree To Our Terms & Conditions.\r\n";
		}

		// Set Errors...
		if (_error != "")
		{
			alert(_error);
			//$('input[type=image]', this).removeAttr('disabled');			
			return false;
		} else {
			//$("#submitbtn").val("Please wait ...");
			//$("#submitbtn").attr("disabled", "disabled");
			return true;
		}
	});
});
