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);
// JavaScript Document
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;
$(document).ready(function(){
	nbINPUT=getNBINPUT();
	$("#infos").fadeOut("fast");
	$("input:text").each(function(){
	   	if($("#"+this).attr("class")!=""){
			sRules = this.className.split(' ');
			sRequired = sRules[1]; // determines if field is required or not
			if(sRequired=="required"){
				$(this).keyup(function(){verification();});
				$(this).change(function(){verification();});
				$(this).click(function(){verification();});
			}
	   }
							 });//fin du each
	verification();
		   });
function verification(){
	var N=0;
	gErrors=0;
	$("input:text").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){
								$("#submite").fadeIn("fast");
							}
							else if(N==nbINPUT && gErrors!=0){
								//alert(N+"<"+nbINPUT+" err="+gErrors);
								$("#submite").fadeOut("fast");
							}
					  }
					 });
			}
		}
								  });
}
function affiche_resultat(msg){
	sResults = msg.split(",");
	var aa=false;
	if(sResults[0]=="ok"){//valide!
		indice=sResults[1];
		gShow=document.getElementById(sResults[2]);
		$(gShow).html("<img class='image_erreur' src='"+chemin_ok+"' height=15 width=15");
		aa=true;
		//$(gShow).bind("click",function(){return false});
	}
	else{
		gShow=document.getElementById(sResults[2]);
		type_erreur=sResults[0];
		switch(type_erreur){
							case "vide":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 >");
								//onMouseOver='ShowInfo(\"Champs necessaire\")'
								$(gShow).mouseover(function(){
														ShowInfo("champ necessaire",gShow);
								});
								break;
							case "court":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height='15' width='15'>");
								$(gShow).mouseover(function(){
														ShowInfo("Trop court",gShow);
								});
								break;
							case "email":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 >");
								$(gShow).mouseover(function(){
														ShowInfo("E-mail invalide",gShow);
								});
								break;

							case "tel":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 >");
								$(gShow).mouseover(function(){
														ShowInfo("Téléphone invalide",gShow);
								});
								break;
							case "cp":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 ");
								$(gShow).mouseover(function(){
														ShowInfo("Code postal invalide",gShow);
								});
								break;
							case "adresse":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 >");
								$(gShow).mouseover(function(){
														ShowInfo("Adresse non valide",gShow);
								});
								break;
							case "nonalpha":
								$(gShow).html("<img class='image_erreur' src='"+chemin_erreur+"' height=15 width=15 >");
								$(gShow).mouseover(function(){
														ShowInfo("CAractères invalides",gShow);
								});
								break;
							default :

								break;


		}
	}
	return aa;
}

function ShowInfo(texte,gg){
	$(gg).mouseover(function(){return false;});
	temp=document.getElementById("infos");
	temp.zIndex=1;
	//$("#infos").fadeOut("fast");
	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").fadeOut("fast",function(){
										$(gg).mouseover(function(){ShowInfo(texte,gg);});
																   });

							   });
	$("#infos").mouseout(function(){$("#infos").fadeOut("fast",function(){
										$(gg).mouseover(function(){ShowInfo(texte,gg);});
										});
								  });

}
function getNBINPUT(){
	var aa=0;
	$("input:text").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;
}
