$(document).ready(function() {
	
	//trigger global pour chaque envoi de form
	$("form.validation").submit(function() {
		return $(this).checkFields();
	})
	
	//redirection suivant type de champs
	$.fn.checkFields = function() {
		
		var formValid = true;
			
		$(this).find("input, textarea").each(function() {
			
			if($(this).hasClass("validate-length")) {
				
				var maxLength = $(this).getParameter("length");
				if(maxLength == null || maxLength == undefined) maxLength = 4;
				
				if($(this).checkLength(maxLength, $(this).attr("title")) == false) { formValid = false; }
				else { if(formValid == true) formValid = true; }
			}
			
			if($(this).hasClass("validate-confirm")) {
			
				var targetField = $(this).getParameter("confirm");
				
				if($(this).checkConfirm(targetField) == false) { formValid = false; }
				else { if(formValid == true) formValid = true; }
				
			}
			
			if($(this).hasClass("validate-email")) {
				//alert($(this).attr("class"));
				if($(this).checkMail() == false) { formValid = false; }
				else { if(formValid == true) formValid = true; }
			}
			
			if($(this).hasClass("validate-tel")) {
				if($(this).checkTel() == false) { formValid = false;
				} else { if(formValid == true) formValid = true; }
			}
			
		});
		
		return formValid;
		
	}
	
	$.fn.getParameter = function(type) {
		
		var expr = new RegExp(type+"\\[([a-z0-9]+)\\]", "gi");
		var result = expr.exec($(this).attr("class"));
		
		if(result != null) { return result[1];	
		} else { return 3; }
		
	}
	
	//verification des mals
	$.fn.checkMail = function() { return $(this).checkRegex(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$/, $(this).attr("title")); }
	
	//verification des tel
	$.fn.checkTel = function() { return $(this).checkRegex(/^[-.0-9\s+]{6}/, $(this).attr("title")); }
	
	//comparateur de regex
	$.fn.regex = function(pattern){ return ($(this[0]).val().match(pattern)) ? true : false; }
		
	//comparer les confirm
	$.fn.checkConfirm = function(targetField) {
		
		if($("#"+targetField).val() == $(this).val()) {
			
			$(this).hideWarning();
			return true;
			
		} else {
			
			$(this).displayWarning("Le champs doit être identique");
			return false;
			
		}
		
	}
		
	//verif des regex
	$.fn.checkRegex = function(pattern, error) {
		if($(this).regex(pattern) == true) {
			return true;
		} else {
			$(this).displayWarning(error);
			return false;
		}
	}
	
	//verif de la longueur
	$.fn.checkLength = function(length, error) {
		if($(this).val().length < length) {
			$(this).displayWarning(error);
			return false;
		} else {
			$(this).hideWarning();
			return true;
		}
	}
	
	//afficher les avertissement : span.warning qui suit le field
	$.fn.displayWarning = function(error) {
		$(this).addClass("warning").next("span.warning").text(error).fadeIn("fast");
	}
	
	//masquer les avertissement : span.warning qui suit le field
	$.fn.hideWarning = function(target) {
		$(this).removeClass("warning").next("span.warning").fadeOut("fast", function() {
			$(this).next("span.warning").text("");
		});
	}
	
})
