/*======================================================================*\
|| #################################################################### ||
|| # JAKCMS PRO 2.2.3                                                 # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright 2011 JAKCMS All Rights Reserved.                       # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- JAKCMS IS NOT FREE SOFTWARE ----------------    # ||
|| # http://www.jakcms.com | http://www.jakcms.com/license-agreement  # ||
|| #################################################################### ||
\*======================================================================*/

$(document).ready(function() {
			$(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[type=text],input[type=password],textarea",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:582},590);
						}
						
						function doBlur(obj) {
							if (validate(obj)) {
								isGood(obj);
							}
						}
						
						function reportErr(obj, message) {
							$(obj).addClass("error").parents("label").removeClass("isgood").addClass("required").addClass("error").find(".infopop").html(message).addClass("errorpop").animate({opacity:1,left:582},590);
						}
						
						function isGood(obj) {
							$(obj).removeClass("error").removeClass("active").parents("label").addClass("isgood").removeClass("error").removeClass("active").find(".infopop").removeClass("errorpop").animate({opacity:0,left:582},590);
						} 	
			
						function validate(obj) {
							// Extend jQuery object to include Regular expression masks assigned to properties
							mask = jQuery.extend({textfieldmask:/^[a-z0-9\.\s-]{3,}$/i,numrmask:/^[0-9\(\)\+\.\s-]{3,}$/i,passwordmask:/^\w{5,}$/,emailmask:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/,humanmask:/^[a-z0-9\.\s-]{3,}$/i,textareamask:/^[a-z0-9\.\s-]{20,}$/i});
							// Extend jQuery object to include error messages assigned to properties
							errmsg = jQuery.extend({textfielderr:jakCMS.jakcms_msg,emailerr:jakCMS.jakcms_msg2,numrerr:jakCMS.jakcms_msg1,passworderr:jakCMS.jakcms_msg3,passworderr:jakCMS.jakcms_msg4,humanerr:jakCMS.jakcms_msg5});
						
							// 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.title) {
					case "username": 	masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "1": 			masktouse="textfieldmask"; 		errtouse="textfielderr"; 	break;
					case "2":	 		masktouse="numrmask"; 			errtouse="numrerr"; 		break;
					case "3": 			masktouse="emailmask"; 			errtouse="emailerr"; 		break;
					case "human": 		masktouse="humanmask"; 			errtouse="humanerr"; 		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!
								}
								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;
							}
						}
});
