/*
'---
'--- Validation.js - Fonctions de validation en JavaScript
'---
'--- Par Francois Cusson-Lafrenaye, ing.
'---     Seconde Vision [www.sv.qc.ca]
'---
'--- Ce fichier doit être inclus dans la section <HEAD>. Chacun des champs à valider peut
'--- être indiqué ainsi :
'---
'--- <SCRIPT>AjoutValidation("NomForm.NomChamp", VAL_NONVIDE + VAL_NOMBRE, "Vous devez indiquer de quoi de valides!");</SCRIPT>
'---
'--- REVISIONS
'--- 2008/09/30, JPB-	Adaptation
'---
*/

<!-- Fonctions de validation -->

var VAL_NONVIDE = 1;
var VAL_NOMBRE = 2;

var ArrayValidation = new Array();

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * AjoutValidation() - Ajoute un item à valider
 */
function AjoutValidation(iChamp, iType, iMessageErreur)
{	
	ArrayValidation[ArrayValidation.length] = new Array(iChamp, iType, iMessageErreur);
}


function EnleverValidation(iChamp, iType, iMessageErreur, WhichTextbox_)
{
	var valeurRechercher = iChamp + "," + iType + "," + iMessageErreur;
	var indexTrouver = -1;

	for(i=0; i<ArrayValidation.length; i++)
	{
		if(ArrayValidation[i] == valeurRechercher)
		{
			indexTrouver = i;
		}
	}

	if(indexTrouver != -1)
	{
		ArrayValidation.splice(indexTrouver, 1);
		document.getElementById(WhichTextbox_).value = "";
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * DoValidation() - Valide chacun des items enregistrés dans ArrayValidation
 */
function DoValidation()
{
	var i = 0;
	var Champ;

	for ( ; i < ArrayValidation.length; i++)
	{
		// Accès à l'objet Champ avec gestion des [] dans les noms en PHP
		if (ArrayValidation[i][0].indexOf("[]") < 0)
		{
			Champ = eval("document." + ArrayValidation[i][0]);
		}
		else
		{
			// On utilise la forme NomForm.elements['NomChamp[]']
			var NomChamp = ArrayValidation[i][0].replace(".", ".elements['") + "']";
			Champ = eval("document." + NomChamp);
		}
		
		// Si le champ est valide...
		if (typeof(Champ) != "undefined")
		{
            // Sinon, si le champ est une liste (select)...
            if (typeof(Champ.options) != "undefined")
            {
				// Non-vide?
				if (ArrayValidation[i][1] & VAL_NONVIDE)
				{
					// On s'assure que la valeur sélectionnée a une valeur non-vide
					var Selection = Champ.selectedIndex;
					var Valeur = "" + Champ.options[Selection].value;
					
					if (Valeur == "")
					{
						alert(ArrayValidation[i][2]);

						if (Champ.type != "hidden") Champ.focus();

						return false;
					}
                }
            }

			// Sinon, si le champ est un array...
			else if (typeof(Champ.length) != "undefined")
			{
				// Non-vide?
				if (ArrayValidation[i][1] & VAL_NONVIDE)
				{
					// On s'assure qu'on a au moins une valeur de choisie (attention si TEXT !)
					var Okay = false;
					var j = 0;

					for ( ; j < Champ.length; j++)
					{
						Okay |= Champ[j].checked;
					}

					if (!Okay)
					{
						alert(ArrayValidation[i][2]);

						if (Champ.type != "hidden") Champ[0].focus();

						return false;
					}
				}

				// Nombre?
				if (ArrayValidation[i][1] & VAL_NOMBRE)
				{
					// On regarde chaque champ et, s'il contient une valeur, elle doit etre numérique
					var j = 0;

					for ( ; j < Champ.length; j++)
					{
                        if (Champ[j].value != "")
					    {
                            // Protection contre les virgules et les espaces
						    if (Champ[j].value.indexOf(",") > -1 || Champ[j].value.indexOf(" ") > -1)
						    {
                                alert(ArrayValidation[i][2]);

							     if (Champ[j].type != "hidden") Champ[j].focus();

							     return false;
						    }
						    else if (isNaN(parseFloat(Champ[j].value)))
						    {
							     alert(ArrayValidation[i][2]);

							     if (Champ[j].type != "hidden") Champ[j].focus();

							     return false;
						    }
						    else
						    {
							     Champ.value = parseFloat(Champ.value);
						    }
						}
					}
				}
			}

			// Sinon, si le champ est un champ "ordinaire"...
			else if (typeof(Champ.length) == "undefined")
			{
				// Non-vide?
				if (ArrayValidation[i][1] & VAL_NONVIDE)
				{
					if (Champ.value == "" || Champ.value == " " || Champ.value == "  ")
					{
						alert(ArrayValidation[i][2]);

						if (Champ.type != "hidden") Champ.focus();

						return false;
					}
				}

				// Nombre?
				if (ArrayValidation[i][1] & VAL_NOMBRE)
				{
					if (Champ.value != "")
					{
						// Protection contre les virgules et les espaces
						if (Champ.value.indexOf(",") > -1 || Champ.value.indexOf(" ") > -1)
						{
							alert(ArrayValidation[i][2]);

							if (Champ.type != "hidden") Champ.focus();

							return false;
						}
						else if (isNaN(parseFloat(Champ.value)))
						{
							alert(ArrayValidation[i][2]);

							if (Champ.type != "hidden") Champ.focus();

							return false;
						}
						else
						{
							Champ.value = parseFloat(Champ.value);
						}
					}
				}
			}
			
			// Sinon, on a un probleme avec la validation!
			else 
			{
                alert("Le champ " + ArrayValidation[i][0] + " ne peut pas être validé.");
            }
		}
	}

	return true;
}


function ValideCourriel(iCourriel) 
{ 
    var re=/^[a-z\d]+((\.|-|_)[a-z\d]+)*@((?![-\d])[a-z\d-]{0,62}[a-z\d]\.){1,4}[a-z]{2,6}$/gi;
    return (iCourriel.match(re)==iCourriel)&&(iCourriel.substr(iCourriel.lastIndexOf("@")).length<=256);   
}

function VerificationDomaine(iCourriel)
{
    // Liste des domaines possibles
    var re=/^(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw|aero|arpa|biz|com|coop|edu|eu|gov|info|int|mil|museum|name|net|org|pro|jobs|travel)$/gi;
    
    return iCourriel.substr(iCourriel.lastIndexOf(".") + 1).match(re)!=null;
}



/* DÉBUT FONCTION DE VALIDATION USERFRIENDLY (sans alert()) */
function ValiderFormulaireInscriptionEvenement()
{
	var Erreur = 0;
	
	// Reset les classes d'erreurs des champs Dates si c'est le cas
	document.getElementById("TD_Date1").className = "TD_Label"
	document.getElementById("TD_Date2").className = "TD_Label"
	document.getElementById("TD_Date3").className = "TD_Label"
	document.getElementById("TD_Date4").className = "TD_Label"
	document.getElementById("TD_Date5").className = "TD_Label"
	document.getElementById("TD_Date6").className = "TD_Label"
	
	// Valide Prenom
	if(document.getElementById("Prenom").value == "")
	{
		Erreur = 1;
		document.getElementById("TD_MessageErreur").innerHTML = "<span>Votre prénom est obligatoire.</span>"
		document.getElementById("TD_Prenom").className = "TD_LabelErreur"
		document.getElementById('Prenom').focus();
		return false;
	}
	else
	{
		Erreur = 0;
		document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;"
		document.getElementById("TD_Prenom").className = "TD_Label"
	}
	
	// Valide Nom
	if(document.getElementById("Nom").value == "")
	{
		Erreur = 1;
		document.getElementById("TD_MessageErreur").innerHTML = "<span>Votre nom est obligatoire.</span>"
		document.getElementById("TD_Nom").className = "TD_LabelErreur"
		document.getElementById('Nom').focus();
		return false;
	}
	else
	{
		Erreur = 0;
		document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;"
		document.getElementById("TD_Nom").className = "TD_Label"
	}
	
	// Valide Courriel
	if(document.getElementById('Courriel').value == "" || document.getElementById("Courriel").value.indexOf("@") == -1 || document.getElementById("Courriel").value.indexOf(".") == -1)
	{
		Erreur = 1;
		document.getElementById("TD_MessageErreur").innerHTML = "<span>Votre courriel est obligatoire.</span>"
		document.getElementById("TD_Courriel").className = "TD_LabelErreur"
		document.getElementById('Courriel').focus();
		return false;
	}
	else
	{
		Erreur = 0;
		document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;"
		document.getElementById("TD_Courriel").className = "TD_Label"
	}
	
	
	if(Erreur == 0)
		return true;
	else
		return false;
}

function Clear_ToutesErreurValidationFormulaireInscriptionEvenement()
{
	document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;"
	document.getElementById("TD_Prenom").className = "TD_Label"
	document.getElementById("TD_Nom").className = "TD_Label"
	document.getElementById("TD_Courriel").className = "TD_Label"
	
	document.getElementById("TD_Date1").className = "TD_Label"
	document.getElementById("TD_Date2").className = "TD_Label"
	document.getElementById("TD_Date3").className = "TD_Label"
	document.getElementById("TD_Date4").className = "TD_Label"
	document.getElementById("TD_Date5").className = "TD_Label"
	document.getElementById("TD_Date6").className = "TD_Label"
}

/*function Clear_ErreursCoordonnees_ValidationFormulaireInscriptionEvenement()
{
	document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;"
	document.getElementById("TD_Prenom").className = "TD_Label"
	document.getElementById("TD_Nom").className = "TD_Label"
	document.getElementById("TD_Courriel").className = "TD_Label"
}
function Clear_ErreursDates_ValidationFormulaireInscriptionEvenement()
{
	//document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;"
	document.getElementById("TD_Date1").className = "TD_Label"
	document.getElementById("TD_Date2").className = "TD_Label"
	document.getElementById("TD_Date3").className = "TD_Label"
	document.getElementById("TD_Date4").className = "TD_Label"
	document.getElementById("TD_Date5").className = "TD_Label"
	document.getElementById("TD_Date6").className = "TD_Label"
}*/



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * DoValidation_Formulaire() - Valide chacun des items enregistrés dans ArrayValidation
 */
function DoValidation_Formulaire()
{
	var i = 0;
	var Champ;

	for ( ; i < ArrayValidation.length; i++)
	{
		// Accès à l'objet Champ avec gestion des [] dans les noms en PHP
		if (ArrayValidation[i][0].indexOf("[]") < 0)
		{
			Champ = eval("document." + ArrayValidation[i][0]);
		}
		else
		{
			// On utilise la forme NomForm.elements['NomChamp[]']
			var NomChamp = ArrayValidation[i][0].replace(".", ".elements['") + "']";
			Champ = eval("document." + NomChamp);
		}
		
		// Si le champ est valide...
		if (typeof(Champ) != "undefined")
		{
            // Sinon, si le champ est une liste (select)...
            if (typeof(Champ.options) != "undefined")
            {
				// Non-vide?
				if (ArrayValidation[i][1] & VAL_NONVIDE)
				{
					// On s'assure que la valeur sélectionnée a une valeur non-vide
					var Selection = Champ.selectedIndex;
					var Valeur = "" + Champ.options[Selection].value;
					
					if (Valeur == "")
					{
						//alert(ArrayValidation[i][2]);
						document.getElementById("TD_MessageErreur").innerHTML = "<span>" + ArrayValidation[i][2] + "</span>";
						document.getElementById("TD_Question_" + parseInt(i + 1)).className = "TD_QuestionErreur";

						if (Champ.type != "hidden") Champ.focus();

						return false;
					}
					else
					{
						document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;";
						document.getElementById("TD_Question_" + parseInt(i + 1)).className = "TD_Question";
					}
                }
            }

			// Sinon, si le champ est un array...
			else if (typeof(Champ.length) != "undefined")
			{
				// Non-vide?
				if (ArrayValidation[i][1] & VAL_NONVIDE)
				{
					// On s'assure qu'on a au moins une valeur de choisie (attention si TEXT !)
					var Okay = false;
					var j = 0;

					for ( ; j < Champ.length; j++)
					{
						Okay |= Champ[j].checked;
					}

					if (!Okay)
					{
						//alert(ArrayValidation[i][2]);
						document.getElementById("TD_MessageErreur").innerHTML = "<span>" + ArrayValidation[i][2] + "</span>";
						document.getElementById("TD_Question_" + parseInt(i + 1)).className = "TD_QuestionErreur";

						if (Champ.type != "hidden") Champ[0].focus();

						return false;
					}
					else
					{
						document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;";
						document.getElementById("TD_Question_" + parseInt(i + 1)).className = "TD_Question";
					}
				}

				// Nombre?
				if (ArrayValidation[i][1] & VAL_NOMBRE)
				{
					// On regarde chaque champ et, s'il contient une valeur, elle doit etre numérique
					var j = 0;

					for ( ; j < Champ.length; j++)
					{
                        if (Champ[j].value != "")
					    {
                            // Protection contre les virgules et les espaces
						    if (Champ[j].value.indexOf(",") > -1 || Champ[j].value.indexOf(" ") > -1)
						    {
                                alert(ArrayValidation[i][2]);

							     if (Champ[j].type != "hidden") Champ[j].focus();

							     return false;
						    }
						    else if (isNaN(parseFloat(Champ[j].value)))
						    {
							     alert(ArrayValidation[i][2]);

							     if (Champ[j].type != "hidden") Champ[j].focus();

							     return false;
						    }
						    else
						    {
							     Champ.value = parseFloat(Champ.value);
						    }
						}
					}
				}
			}

			// Sinon, si le champ est un champ "ordinaire"...
			else if (typeof(Champ.length) == "undefined")
			{
				// Non-vide?
				if (ArrayValidation[i][1] & VAL_NONVIDE)
				{
					if (Champ.value == "" || Champ.value == " " || Champ.value == "  ")
					{
						//alert(ArrayValidation[i][2]);
						document.getElementById("TD_MessageErreur").innerHTML = "<span>" + ArrayValidation[i][2] + "</span>";
						document.getElementById("TD_Question_" + parseInt(i + 1)).className = "TD_QuestionErreur";

						if (Champ.type != "hidden") Champ.focus();

						return false;
					}
					else
					{
						document.getElementById("TD_MessageErreur").innerHTML = "&nbsp;";
						document.getElementById("TD_Question_" + parseInt(i + 1)).className = "TD_Question";
					}
				}

				// Nombre?
				if (ArrayValidation[i][1] & VAL_NOMBRE)
				{
					if (Champ.value != "")
					{
						// Protection contre les virgules et les espaces
						if (Champ.value.indexOf(",") > -1 || Champ.value.indexOf(" ") > -1)
						{
							alert(ArrayValidation[i][2]);

							if (Champ.type != "hidden") Champ.focus();

							return false;
						}
						else if (isNaN(parseFloat(Champ.value)))
						{
							alert(ArrayValidation[i][2]);

							if (Champ.type != "hidden") Champ.focus();

							return false;
						}
						else
						{
							Champ.value = parseFloat(Champ.value);
						}
					}
				}
			}
			
			// Sinon, on a un probleme avec la validation!
			else 
			{
                alert("Le champ " + ArrayValidation[i][0] + " ne peut pas être validé.");
            }
		}
	}

	return true;
}
/* FIN FONCTION DE VALIDATION USERFRIENDLY (sans alert()) */






