$(function(){
	// Grab each form element
	$("label[title]").each(function(){
		$(this).append("<div class=\"infopop\">");	
		titletext = $(this).attr("title");
		$(this).removeAttr("title");
		$(".infopop",this).css({opacity:0}).html(titletext);
		$("input",this).focus(function(){
			// Mouseover
			doFocus(this);
		}).blur(function(){
			// MouseOut
			doBlur(this);
		});
	});
});

function doFocus(obj) {
	$(obj).addClass("active").parents("label").addClass("active").find(".infopop").animate({opacity:1,left:392},400);
}

function doBlur(obj) {
	if (validate(obj)) { 
		isGood(obj);
	}
}

function reportErr(obj, message) {
	$(obj).addClass("errorjq").parents("label").removeClass("isgood").addClass("required").addClass("errorjq").find(".infopop").html(message).addClass("errorpop").animate({opacity:1,left:392},400);
}

function isGood(obj) {
	$(obj).removeClass("errorjq").removeClass("active").parents("label").addClass("isgood").removeClass("errorjq").removeClass("active").find(".infopop").removeClass("errorpop").animate({opacity:0,left:413},400);
} 			

function validate(obj) {
	// Extend jQuery object to include Regular expression masks assigned to properties
	mask = jQuery.extend({textfieldmask: /^[a-z\.\s-]{2,}$/i,phonemask: /^[0-9\(\)\+\.\s-]{8,}$/i,passwordmask: /^\w{5,}$/, frommask:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/,tomask:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/});
	// Extend jQuery object to include errorjq messages assigned to properties
	errmsg = jQuery.extend({textfielderr:"2 or more letters",captchaerr: "2 or more letters",passworderr:"Minimum 5 characters",fromerr:"Invalid email address",toerr:"Invalid email address",messageerr:"Write something!",matcherr: "Must match"});

	// Set up variables to hold details of which mask to use and whether the field should match another field
	var masktouse = null;
	var mustmatch = null;
	// Determine the type of mask we're going to validate against
	switch(obj.name) {
		case "name": 		masktouse="textfieldmask";
		case "message": 	masktouse="textfieldmask";		errtouse="messageerr"; 		break;
		case "captcha": 	masktouse="textfieldmask"; 		errtouse="captchaerr"; 		break;
		case "to": 	masktouse="tomask"; 		errtouse="toerr"; 	break;
		case "subject": 	masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
		case "from": 		masktouse="frommask"; 			errtouse="fromerr"; 		break;
		case "password": 	masktouse="passwordmask"; 		errtouse="passworderr"; 	mustmatch="verpassword"; 	break;
		case "verpassword": masktouse="passwordmask"; 		errtouse="passworderr"; 	mustmatch="password"; 		break;
	}
	// Check that the element is a required field before validating against it.
	if($(obj).parents("label").hasClass("required") && masktouse) {
		// Set up a quick way of accessing the object we're validating
		pointer = $(obj);
		// Test the value of the field against the Regular Expression
		if (mask[masktouse].test(pointer.val())) {
			// The field validated successfully!
			
			// Check to see if the field needs to match another field in the form
			if (mustmatch) {
				// It does need to match, so grab the object it needs to match
				matchobj = $("#"+mustmatch);
				if (matchobj.val()!='' && matchobj.val()!=pointer.val()) {
					// The fields don't match, so report an errorjq on both of them
					reportErr(obj,errmsg["matcherr"]);	
					reportErr(matchobj,errmsg["matcherr"]);
				}
				else {
					// Either the fields match, or the other field hasn't been completed yet
					// If the other field has been completed, call the isGood function to clear any errorjq message showing
					if (matchobj.val()!='') { isGood(matchobj);}
					return true;
				}
			}
			else {
				// No match is required, so return true - validation passed!
				return true;
			} 
		}
		else { 
			// The field failed to validate against the Regular Expression
			reportErr(obj,errmsg[errtouse]);
			return false; 
		}
	} 
	else {	
		// This isn't a required field, so we won't validate it against anything			
		return true;
	}
}

