$(document).ready(function() {

	$('.highlight').bind(focus,function(){$(this).removeClass('highlight');})
	$('#form').validate({
		rules: {
			email: { email: true },
			cell_1: { digits: true, minlength: 3 },
			cell_2: { digits: true, minlength: 3 },
			cell_3: { digits: true, minlength: 4 },
			captcha: "captcha",
			county: "selectBox",
			state: "selectBox"
		},
			
		messages: { 
			first_name: "",
			last_name: "",
			cell_1: "", cell_2: "", cell_3: "",
			county: "Please select a county!",
			state: "Please select a state!",
			email: "",
			captcha: "Incorrect Code!"
			
		},
		
		highlight: function(e, errorClass) {
     		$(e).addClass('highlight');
		},
		unhighlight: function(e, errorClass) {
			$(e).removeClass('highlight');
	  	},
		
		focusCleanup: true,
		
		focusInvalid: false
	
	});
	
	$('#form_optout').validate({
		rules: {
			cell_1: { digits: true, minlength: 3 },
			cell_2: { digits: true, minlength: 3 },
			cell_3: { digits: true, minlength: 4 },
		},
			
		messages: {
			cell_1: "", cell_2: "", cell_3: ""			
		},
		
		highlight: function(e, errorClass) {
     		$(e).addClass('highlight');
		},
		unhighlight: function(e, errorClass) {
			$(e).removeClass('highlight');
	  	},
		
		focusCleanup: true,
		
		focusInvalid: false
		
	});

});

$.validator.addMethod("captcha", 
	function(v, e){
		var checkCode = "false";
		checkCode = $.ajax({ url: "processForm.cfm?captcha=" + v.toLowerCase().trim(), async: false }).responseText;
		return this.optional(e) || checkCode.trim() == "true";
		
	}, "Incorrect Code!"
);

$.validator.addMethod("selectBox",
	function(v, e){
		return this.optional(e) || v != "";
	}, ""
);

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");	
}