/*
 * Inline Form Validation Engine 1.6.4, 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 and Teddy Limousin 
 * and everyone helping me find bugs on the forum
 * Licenced under the MIT Licence
 */
 
(function($) {
	$(".formError").live("click",function(){	 // REMOVE BOX ON CLICK
		$(this).fadeOut(150,function(){
			$(this).remove();
		}) 
	})	
	$.validationEngine = {
		buildPrompt : function(caller,promptText,type,promptPosition,noarrow,id_dopisane) {			// ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
			if (!(promptPosition)) promptPosition = 'topRight';
			if (id_dopisane) deleteItself = "." + id_dopisane + "formError";
			else deleteItself = "." + $(caller).attr("id") + "formError";
	
			if($(deleteItself)[0]){
				$(deleteItself).stop();
				$(deleteItself).remove();
			}
			var divFormError = document.createElement('div');
			var formErrorContent = document.createElement('div');
			linkTofield = $.validationEngine.linkTofield(caller, id_dopisane)
			$(divFormError).addClass("formError")
		
			if(type == "pass"){ $(divFormError).addClass("greenPopup") }
			if(type == "load"){ $(divFormError).addClass("blackPopup") }
		
			$(divFormError).addClass(linkTofield);
			$(formErrorContent).addClass("formErrorContent");
		
			$("body").append(divFormError);
			$(divFormError).append(formErrorContent);
			
			if (!(noarrow)) {
				var arrow = document.createElement('div');
				$(arrow).addClass("formErrorArrow");
				$(divFormError).append(arrow);
				if(promptPosition == "bottomLeft" || promptPosition == "bottomRight"){
				$(arrow).addClass("formErrorArrowBottom")
				$(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(promptPosition == "topLeft" || promptPosition == "topRight"){
					$(divFormError).append(arrow);
					$(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>');
				}
			}
			$(formErrorContent).html(promptText)
	
			callerTopPosition = $(caller).offset().top;
			callerleftPosition = $(caller).offset().left;
			callerWidth =  $(caller).width();
			inputHeight = $(divFormError).height();
	
			/* POSITIONNING */
			if(promptPosition == "topRight"){callerleftPosition +=  callerWidth -30; callerTopPosition += -inputHeight -10; }
			if(promptPosition == "topLeft"){ callerTopPosition += -inputHeight -10; }
		
			if(promptPosition == "centerRight"){ callerleftPosition +=  callerWidth +13; }
		
			if(promptPosition == "bottomLeft"){
				callerHeight =  $(caller).height();
				callerleftPosition = callerleftPosition;
				callerTopPosition = callerTopPosition + callerHeight + 15;
			}
			if(promptPosition == "bottomRight"){
				callerHeight =  $(caller).height();
				callerleftPosition +=  callerWidth -30;
				callerTopPosition +=  callerHeight + 15;
			}
			$(divFormError).css({
				top:callerTopPosition,
				left:callerleftPosition,
				opacity:0
			})
			return $(divFormError).animate({"opacity":0.87},function(){return true;});	
		},
		linkTofield : function(caller, id_dopisane){
			if (id_dopisane) linkTofield = id_dopisane + "formError";
			else linkTofield = $(caller).attr("id") + "formError";
			linkTofield = linkTofield.replace(/\[/g,""); 
			linkTofield = linkTofield.replace(/\]/g,"");
			return linkTofield;
		}
	}
})(jQuery);

