//------------------------------------------------------------------------
//------------- Classe de validation de formulaires ----------------------
//------------------------------------------------------------------------


function Validation(normColor,errColor)
{
 	this.errors=new Array();
	this.errorCount=0;
	this.normalColor="#000000";		// couleur normale
	this.errorColor="#ff0000";		// couleur d'erreur
	this.items=new Array();
}

Validation.prototype.correctField=function(field)
{
	if(this.errors[field.id]!=true){return;}
    	field.style.color=this.normalColor;
     field.value="";
     this.errors[field.id]=false;
     this.errorCount--;

     return;
}

Validation.prototype.validate=function()
{
	if(this.afficheur!=undefined)
	{
     	document.getElementById(this.afficheur).innerHTML="";
//     	document.getElementById(this.afficheur).style.display="none";
 	}

	for(index in this.items)
	{
		if(this.items[index][1]==undefined){continue;}

		switch(this.items[index][1])
		{
			case "texte":	// vérifie si un champ texte est rempli - résultat dans le champ
				if(document.getElementById(this.items[index][0]).value=="")
				{
					document.getElementById(this.items[index][0]).style.color=this.errorColor;
					document.getElementById(this.items[index][0]).value=this.items[index][2];
					this.errors[this.items[index][0]]=true;
					this.errorCount++;
				}
				break;

			case "email":	// verifie si un champ texte est rempli avec un mail valable - résultat dans le champ
				if(document.getElementById(this.items[index][0]).value=="")
				{
                         document.getElementById(this.items[index][0]).style.color=this.errorColor;
					document.getElementById(this.items[index][0]).value=this.items[index][2];
					this.errors[this.items[index][0]]=true;
					this.errorCount++;
				}
				else if(!new RegExp("^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$","gi").test(document.getElementById(this.items[index][0]).value) && this.errors[this.items[index][0]]!=true)
         			{
         				document.getElementById(this.items[index][0]).style.color=this.errorColor;
					document.getElementById(this.items[index][0]).value=this.items[index][3];
					this.errors[this.items[index][0]]=true;
					this.errorCount++;
         			}
         			break;

			case "checkbox":	// vérifie si une checkbox est cochée - résultat dans l'afficheur
                    if(document.getElementById(this.items[index][0]).checked==false)
				{
					document.getElementById(this.afficheur).innerHTML=this.items[index][2];
//     				document.getElementById(this.afficheur).style.display="block";
     				
     				if(this.errors[this.items[index][0]]==true){break;}
                     	this.errors[this.items[index][0]]=true;
					this.errorCount++;
				}
				break;

			case "fonction":	// vérifie le retour d'une fonction - résultat dans l'afficheur
				if(eval(this.items[index][2])!=true)
				{
					document.getElementById(this.afficheur).innerHTML=this.items[index][3];
//     				document.getElementById(this.afficheur).style.display="block";

     				if(this.errors[this.items[index][0]]==true){break;}
                     	this.errors[this.items[index][0]]=true;
					this.errorCount++;
				}
				else if(eval(this.items[index][2])==true)		// auto-correction
				{
					if(this.errors[this.items[index][0]]!=true){break;}
				     this.errors[this.items[index][0]]=false;
				     this.errorCount--;
				}
				break;
				
			case "hidden":		// vérifie si une valeur existe pour un champ "hidden" - pas de résultat affiché
               	if(document.getElementById(this.items[index][0]).value=="")
				{
					if(this.errors[this.items[index][0]]==true){break;}
					this.errors[this.items[index][0]]=true;
					this.errorCount++;
				}
				else		// auto-correction
				{
					if(this.errors[this.items[index][0]]!=true){break;}
					this.errors[this.items[index][0]]=false;
					this.errorCount--;
				}
				break;

               case "fichier":	// vérifie si un champ fichier est rempli - résultat dans le champ
				if(document.getElementById(this.items[index][0]).value=="")
				{
					document.getElementById(this.afficheur).innerHTML=this.items[index][2];
					this.errors[this.items[index][0]]=true;
					this.errorCount++;
				}
				break;
		}
	}

     if(this.errorCount==0)
     {
     	return true;
     }

     return false;
}
