/*
 * Inline Form Validation Engine 1.6.2, jQuery plugin
 * 
 * Copyright(c) 2009, Cedric Dugas
 * http://www.position-relative.net
 *	
 * Form validation engine allowing custom regex rules to be added.
 * Thanks to Francois Duquette
 * Licenced under the MIT Licence
 */
 
(function(jQuery) {
	
jQuery.validationEngine = {
	defaultSetting : function(caller) {		
		settings = {
			
			validationEventTriggers:"blur",					
			inlineValidation: true,	
			returnIsValid:false,
			animateSubmit:true,
			unbindEngine:true,
			ajaxSubmit: false,
			promptPosition: "topRight",	// OPENNING BOX POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft, centerRight, bottomRight
			success : false,
			failure : function() {}
		}	
		jQuery.validationEngine.settings = settings;
	},

	buildPrompt : function(caller,promptText,type,ajaxed) {			// ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
		if(!jQuery.validationEngine.settings){
			jQuery.validationEngine.defaultSetting()
		}
		var divFormError = document.createElement('div');
		var formErrorContent = document.createElement('div');
		
		linkTofield = jQuery.validationEngine.linkTofield(caller)
		jQuery(divFormError).addClass("formError")
		
		if(type == "pass"){ jQuery(divFormError).addClass("greenPopup") }
		if(type == "load"){ jQuery(divFormError).addClass("blackPopup") }
		if(ajaxed){ jQuery(divFormError).addClass("ajaxed") }
		
		jQuery(divFormError).addClass(linkTofield);
		jQuery(formErrorContent).addClass("formErrorContent");
		
		jQuery("body").append(divFormError);
		jQuery(divFormError).append(formErrorContent);
			
		if(jQuery.validationEngine.showTriangle != false){		// NO TRIANGLE ON MAX CHECKBOX AND RADIO
			var arrow = document.createElement('div');
			jQuery(arrow).addClass("formErrorArrow");
			jQuery(divFormError).append(arrow);
			if(jQuery.validationEngine.settings.promptPosition == "bottomLeft" || jQuery.validationEngine.settings.promptPosition == "bottomRight"){
			jQuery(arrow).addClass("formErrorArrowBottom")
			jQuery(arrow).html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
		}
			if(jQuery.validationEngine.settings.promptPosition == "topLeft" || jQuery.validationEngine.settings.promptPosition == "topRight"){
				jQuery(divFormError).append(arrow);
				jQuery(arrow).html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
			}
		}
		jQuery(formErrorContent).html(promptText)
		
		
	
		callerTopPosition = jQuery(caller).offset().top;
		callerleftPosition = jQuery(caller).offset().left;
		callerWidth =  jQuery(caller).width();
		inputHeight = jQuery(divFormError).height();
	
		/* POSITIONNING */
		if(jQuery.validationEngine.settings.promptPosition == "topRight"){callerleftPosition +=  callerWidth -30; callerTopPosition += -inputHeight -10; }
		if(jQuery.validationEngine.settings.promptPosition == "topLeft"){ callerTopPosition += -inputHeight -10; }
		
		if(jQuery.validationEngine.settings.promptPosition == "centerRight"){ callerleftPosition +=  callerWidth +13; }
		
		if(jQuery.validationEngine.settings.promptPosition == "bottomLeft"){
			callerHeight =  jQuery(caller).height();
			callerleftPosition = callerleftPosition;
			callerTopPosition = callerTopPosition + callerHeight + 15;
		}
		if(jQuery.validationEngine.settings.promptPosition == "bottomRight"){
			callerHeight =  jQuery(caller).height();
			callerleftPosition +=  callerWidth -30;
			callerTopPosition +=  callerHeight + 15;
		}
		jQuery(divFormError).css({
			top:callerTopPosition,
			left:callerleftPosition,
			opacity:0
		})
		return jQuery(divFormError).animate({"opacity":0.87},function(){return true;});	
	},
	updatePromptText : function(caller,promptText,type,ajaxed) {	// UPDATE TEXT ERROR IF AN ERROR IS ALREADY DISPLAYED
		
		linkTofield = jQuery.validationEngine.linkTofield(caller);
		var updateThisPrompt =  "."+linkTofield;
		
		if(type == "pass") { jQuery(updateThisPrompt).addClass("greenPopup") }else{ jQuery(updateThisPrompt).removeClass("greenPopup")};
		if(type == "load") { jQuery(updateThisPrompt).addClass("blackPopup") }else{ jQuery(updateThisPrompt).removeClass("blackPopup")};
		if(ajaxed) { jQuery(updateThisPrompt).addClass("ajaxed") }else{ jQuery(updateThisPrompt).removeClass("ajaxed")};
	
		jQuery(updateThisPrompt).find(".formErrorContent").html(promptText);
		callerTopPosition  = jQuery(caller).offset().top;
		inputHeight = jQuery(updateThisPrompt).height();
		
		if(jQuery.validationEngine.settings.promptPosition == "bottomLeft" || jQuery.validationEngine.settings.promptPosition == "bottomRight"){
			callerHeight =  jQuery(caller).height();
			callerTopPosition =  callerTopPosition + callerHeight + 15;
		}
		if(jQuery.validationEngine.settings.promptPosition == "centerRight"){  callerleftPosition +=  callerWidth +13;}
		if(jQuery.validationEngine.settings.promptPosition == "topLeft" || jQuery.validationEngine.settings.promptPosition == "topRight"){
			callerTopPosition = callerTopPosition  -inputHeight -10;
		}
		jQuery(updateThisPrompt).animate({ top:callerTopPosition });
	},
	linkTofield : function(caller){
		linkTofield = jQuery(caller).attr("id") + "formError";
		linkTofield = linkTofield.replace(/\[/g,""); 
		linkTofield = linkTofield.replace(/\]/g,"");
		return linkTofield;
	},
	closePrompt : function(caller,outside) {						// CLOSE PROMPT WHEN ERROR CORRECTED
		if(!jQuery.validationEngine.settings){
			jQuery.validationEngine.defaultSetting()
		}
		if(outside){
			if(validationErrorFade == true)
			{
				jQuery(caller).fadeTo("fast",0,function(){
					jQuery(caller).remove();
				});
			} else {jQuery(caller).remove();}
			return false;
		}		
		var ajaxValidate=false; //added by Balazs
		if(!ajaxValidate){
			linkTofield = jQuery.validationEngine.linkTofield(caller);
			closingPrompt = "."+linkTofield;
			if(validationErrorFade == true)
			{
				jQuery(closingPrompt).fadeTo("fast",0,function(){
					jQuery(closingPrompt).remove();
				});
			} else {jQuery(closingPrompt).remove();}
		}
	}
}
})(jQuery);
