(function($) {
	
	var wrong_obg="<p class=\"champ-note\"><span>Requis</span></p>";
	var good="<p class=\"champ-note\"><span class=\"cool\">Ok !</span></p>";
	var wrong_format="<p class=\"champ-note\"><span class=\"pascool\">Incorrect</span></p>";
	var opts;
	
	

	/*
	 * Private methods 
	 */
	
	function valid_form_obg(input){
		var ok=true;
		// Si le champ est vide, msg d'erreur
		if($(input).val()=="") {	
			if($(input).closest("fieldset").find("p.champ-note").length==0) $(input).closest("p").after(wrong_obg);
			else $(input).closest("fieldset").find("p.champ-note").eq(0).replaceWith(wrong_obg);
			$(input).addClass("wronginput");
			$(input).removeClass("goodinput");
			ok=false;
		}
		else {
			// Si on affiche le message "ok"
			if(opts.affiche_good){
			  if($(input).closest("fieldset").find("p.champ-note").length==0) $(input).closest("p").after(good)
			  else $(input).closest("fieldset").find("p.champ-note").eq(0).replaceWith(good);
			}
			// Si on affiche pas le message "ok"
			else {
			  $(input).closest("fieldset").find("p.champ-note").eq(0).remove();
			}
			$(input).removeClass("wronginput");				  
			$(input).addClass("goodinput");
		}
		return ok;
	}
	
	function valid_form_email(input){
		var ok=true;
		var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-zA-Z]{2,3}$/;
		// Si le format ne correspond pas
		if(reg.exec($(input).val())==null){
			if($(input).closest("fieldset").find("p.champ-note").length==0) $(input).closest("p").after(wrong_format);
			else $(input).closest("fieldset").find("p.champ-note").eq(0).replaceWith(wrong_format);
			$(input).addClass("wronginput");
			$(input).removeClass("goodinput");
			ok=false;
		}
		else {
			// Si on affiche le message "ok"
			if(opts.affiche_good){
			  if($(input).closest("fieldset").find("p.champ-note").length==0) $(input).closest("p").after(good)
			  else $(input).closest("fieldset").find("p.champ-note").eq(0).replaceWith(good);
			}
			// Si on affiche pas le message "ok"
			else {
			  $(input).closest("fieldset").find("p.champ-note").eq(0).remove();
			}
			$(input).removeClass("wronginput");				  
			$(input).addClass("goodinput");
		}
		return ok;
	}
	
	function check_all_valid(f){
		var passed=true;
		// Les champs obligatoires
		$("input.obg, textarea.obg", f).each(function(){
			if(!valid_form_obg(this)) passed=false;
		});
		
		// Les champs mail
		$("input.email", f).each(function(){
			if(!valid_form_email(this)) passed=false;
		});
		
		return passed;
	}
	
	function is_submit_green(f){
		//alert($("input.obg:not(.goodinput), textarea.obg:not(.goodinput), input.email:not(.goodinput)").length);
		 if($("input.obg:not(.goodinput), textarea.obg:not(.goodinput), input.email:not(.goodinput)",f).length==0) $("input[type=submit]",f).parents("span.btn").eq(0).addClass("btn-ok");
		 else $("input[type=submit]",f).parents("span.btn").eq(0).removeClass("btn-ok");
	}
	
	$.fn.validform_ms = function(options){
		
		// Recup du cookie
		var cookie=getCookie("form_infos");
		var tab=unserialize(cookie);
		
		for(var cle in tab){
			$("input.cookie[name="+cle+"]").val(escape(tab[cle]).replace(/\+/g, ' '));
		}

		var form_actu=this;
		
		// Quand on submit le form
		this.submit(function(){
			
			var passed=check_all_valid(this);
			
			if(passed) opts.callback();
			return false;
		});
		
		// Onblur sur les champs
		$("input.obg, textarea.obg", this).blur(function(){	
			 valid_form_obg(this);
			 
			 // Le submit devient vert
			 is_submit_green(form_actu);
		}); 
		$("input.email", this).blur(function(){	
			 valid_form_email(this);
			 
			 // Le submit devient vert
			 is_submit_green(form_actu);
		}); 
		
		// Options par défaut
		var defaults= {
			affiche_good:1,
			callback:function(){}
		}
		opts = $.extend(defaults, options);
	}
	
	
})(jQuery);


