var mouse_x = 0, mouse_y = 0;
function ajad_updateMousePosition(context){
    if (document.captureEvents) {
        mouse_x = Number(context.pageX);
        mouse_y = Number(context.pageY);
    }
    else
        if (window.event.clientX) {
            mouse_x = Number(window.event.clientX + document.documentElement.scrollLeft);
            mouse_y = Number(window.event.clientY + document.documentElement.scrollTop);
        }
}
var N=0;
$(document.body).mousemove(ajad_updateMousePosition);

window.onload = attachFormHandlers;

var chemin_ok="http://www.adiantus.com/images/common/okay.png";
var chemin_erreur="http://www.adiantus.com/images/common/erreur.png";
var gShow; //variable holding the id where feedback will be sent to.
var sUrl = "validation_formulaire.php?val=";
var gErrors = 0; //number of errors is set to none to begin with
var nbINPUT=null;

function attachFormHandlers(){
	$("#erreur").hide();
	$(":input").each(function(test){
	   	if($("#"+this).attr("class")!=""){
			sRules = this.className.split(' ');
			sRequired = sRules[1]; // determines if field is required or not
			if(sRequired=="required"){
				$(this).keyup(function(){
					sRules = this.className.split(' ');
					sRequired = sRules[1]; // determines if field is required or not
					if(sRequired=="required"){
						sVal = $(this).val();
						parametre = sUrl + sVal + "&sRequired=required&sTypeCheck=" + sRules[2] + "&indiceTAB="+i+"&tderreur="+sRules[3]+"&longueurmin="+sRules[4];
						$.ajax({
							  url: parametre,
							  success: function(msg){
									affiche_resultat(msg);
							  }
							 });
					}
					});
				$(this).click(function(){
					sRules = this.className.split(' ');
					sRequired = sRules[1]; // determines if field is required or not
					if(sRequired=="required"){
						sVal = $(this).val();
						parametre = sUrl + sVal + "&sRequired=required&sTypeCheck=" + sRules[2] + "&indiceTAB="+i+"&tderreur="+sRules[3]+"&longueurmin="+sRules[4];
						$.ajax({
							  url: parametre,
							  success: function(msg){
									affiche_resultat(msg);
							  }
							 });
					}
					});
			}
	   }
	   });//fin du each
	verification_total("zero");
}
function verification_total(nom_form){
	var N=0;
	gErrors=0;
	nbINPUT=getNBINPUT();
	$("#infos").hide();

	$(":input").each(function(i){
	   	if($("#"+this).attr("class")!=""){
			sRules = this.className.split(' ');
			sRequired = sRules[1]; // determines if field is required or not
			if(sRequired=="required"){
				sVal = $(this).val();
				parametre = sUrl + sVal + "&sRequired=required&sTypeCheck=" + sRules[2] + "&indiceTAB="+i+"&tderreur="+sRules[3]+"&longueurmin="+sRules[4];
				$.ajax({
					  url: parametre,
					  success: function(msg){
					  		N++;
							if(!affiche_resultat(msg)){
								gErrors++;
							}
							if(N==nbINPUT && gErrors==0 && nom_form!="zero"){
								$("#erreur").fadeOut("slow");
								$("#"+nom_form).submit();
							}
							else if(N==nbINPUT && gErrors!=0 && nom_form!="zero"){
								$("#erreur").show();
							}
					  }
					 });
			}
		}
								  });
}
function affiche_resultat(msg){
	sResults = msg.split(",");
	var aa=false;
	gShow=document.getElementById(sResults[2]);
	if(sResults[0]=="ok"){//valide!
		indice=sResults[1];
		$(gShow).html("<img class='image_erreur' src='"+chemin_ok+"' height=15 width=15");
		$(gShow).mouseover(function(){});
		$(gShow).css({ cursor: "pointer" });
		aa=true;
	}
	else{
		$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 >");
		if(sResults[0]=="vide"){//type d'erreur
							$(gShow).mouseover(function(){
							ShowInfo("champ necessaire",gShow);
						})}
		else if (sResults[0]=="court"){$(gShow).mouseover(function(){ShowInfo("Trop court",gShow);})}
		else if (sResults[0]=="email"){$(gShow).mouseover(function(){ShowInfo("E-mail invalide",gShow);})}
		else if (sResults[0]=="tel"){$(gShow).mouseover(function(){ShowInfo("Téléphone invalide",gShow);})}
		else if (sResults[0]=="cp"){$(gShow).mouseover(function(){ShowInfo("Code postal invalide",gShow);})}
		else if (sResults[0]=="adresse"){$(gShow).mouseover(function(){ShowInfo("Adresse non valide",gShow);})}
		else if (sResults[0]=="nonalpha"){$(gShow).mouseover(function(){ShowInfo("CAractères invalides",gShow);})}
		$(gShow).css({cursor:"help"});
		$(gShow).mouseout(function(){$("#infos").hide();});
	}

	return aa;
}

function ShowInfo(texte,gg){
	//alert(texte+" - "+gg);
	$(gg).mouseover(function(){return false;});
	temp=document.getElementById("infos");
	temp.zIndex=1;
	MY = mouse_y-100;
	MX = mouse_x+10;
	$("#infos").css({top : MY , left: MX });
	$("#infos").html("<br/><br/>"+texte);
	$("#infos").fadeIn("slow");
	$("#infos").click(function(){
							   $("#infos").hide(function(){
										$(gg).mouseover(function(){ShowInfo(texte,gg);});
																   });
							   });
	$("#infos").mouseout(function(){$("#infos").hide(function(){
										$(gg).mouseover(function(){ShowInfo(texte,gg);});
										});
								  });
}
function getNBINPUT(){
	var aa=0;
	$("input").each(function(){
	   	if($("#"+this).attr("class")!=""){
			sRules = this.className.split(' ');
			sRequired = sRules[1]; // determines if field is required or not
			if(sRequired=="required"){				aa++;			}
		}
								  });
	return aa;
}

