// JavaScript Document
/* gVerifFrom */
GVerifForm_log = Class.create();
GVerifForm_log.prototype = {
		initialize: function(conteneur,ancre,saisie,id_retour) {
			
			this.erreur ="";
			this.conteneur = $(conteneur)
			this.ancre = ancre;
			this.saisie = $(saisie);
			this.id_retour = id_retour;
			this.ligne_intro = '<span class="titre_verif_form">INFORMATIONS MAL SAISIES</span><br/><span class="intro_verif_form">Les informations suivantes sont manquantes ou erronées :</span>';
			this.liste = "";
			this.conteneur_verif = new Array;
		},
		ecrire: function(){
			this.conteneur.innerHTML = "<div>" + this.ligne_intro + this.liste + "</div><div class=\"article\"><a id=\""+this.id_retour+"\" href=\"#_\" class=\"pe-fl-orange\">retour</a></div>";
			//this.conteneur.innerHTML = "<div>dd</div>";
			this.retour = $(this.id_retour);
			this.retour.onclick = this.alterner.bindAsEventListener(this);
		},
		alterner: function(){
			switch_opacity(this.saisie.id,this.conteneur.id)
		},
		supprimer: function(){
			this.conteneur.innerHTML = ''
		},
		affiche: function(){
			return (this.conteneur.innerHTML == '') ? false : true
		},
		add:function(element){
			this.conteneur_verif.push(element);
		},
		traitement:function(element){
			var traitement = $A(this.conteneur_verif);
			var liste = "";
			traitement.each(function(element){
				if(element.traitement()!="")
				{
					switch(element.type)
					{
						case "select":
							liste += "<li>Veuillez "+element.intitule+".</li>";
						break;
						case "radio":
							liste += "<li>Veuillez choisir un "+element.intitule+".</li>";
						break;
						case "condition":
							liste += "<li>"+element.intitule+".</li>";
						break;

						default:
							liste += "<li>Le champ "+element.intitule+" est"+element.traitement()+"</li>";
						break;
					}
					element.changeStyle(true)
				}
			});	
			// si erreur
			if(liste!="")
			{
				this.liste = "<ul>"+liste+"</ul>";
				this.ecrire();
				switch_opacity(this.conteneur.id,this.saisie.id)
				return false
			}
			else
			{
				return true
			}
		}
};
/* /gVerifFrom */

/* VerifFormTxt */
VerifFormTxt_log = Class.create();
VerifFormTxt_log.prototype = {
	initialize: function(element,intitule,obligatoire,taille_min,taille_max,carac_autorise,type) {
		this.element = $(element);
		this.intitule = intitule;
		this.valeur = $F(this.element);
		this.obligatoire = obligatoire;
		this.type=type;
		this.taille_min = taille_min;
		this.taille_max = (taille_max=='max') ? this.element.maxLength : taille_max;
		this.regex = null;
		switch (carac_autorise)
		{
			case "chiffre":
				this.carac_autorise = "0123456789";
			break;
			case "nombre":
				this.carac_autorise = "0123456789., -";
			break;
			case "date":
				this.carac_autorise = "0123456789/";
			break;
			case "tel":
				this.carac_autorise = "0123456789. -";
			break;
			case "alpha":
				this.carac_autorise = "abcdefghijklmnopqrstuvwxyzéèêëôöîïàâäûüç";
			break;
			case "alphachiffre":
				this.carac_autorise = "abcdefghijklmnopqrstuvwxyzéèêëôöîïàâäûüç0123456789";
			break;
			case "alphanombre":
				this.carac_autorise = "abcdefghijklmnopqrstuvwxyzéèêëôöîïàâäûüç0123456789., -";
			break;
			case "alpha_2":
				this.carac_autorise = "abcdefghijklmnopqrstuvwxyzéèêëôöîïàâäûüç0123456789.,- '()€$£%[]{}&=+@?!/°;:*~_’";
			break;
			default:
				this.carac_autorise = "";
			break;
		}
		this.type = type;
	},
	changeStyle: function(change){
		if(change)
		{
			Element.addClassName(this.element,"verif_form_erreur")
		}
		else
		{
			Element.removeClassName(this.element,"verif_form_erreur")
		}
	},
	traitement: function(){
		this.changeStyle(false)
		switch (this.type)
		{
			/* texte */
			case "texte" :
				return this.verification()
			break;

			/* email */
			case "email" :
				this.regex = /^[a-z0-9_.-]+@[a-z0-9_.-]+\.([a-z]{2,4})$/;
				return this.verification()
			break;

			/* date */
			case "date" :
				this.regex = /^([0123])([0123456789])\/([01])([0123456789])\/([012])([0123456789])([0123456789])([0123456789])$/;
				return this.verification()
			break;

			/* defaut */
			default:
				return ""
			break;
		}
	},
	verification: function(){
		var valeur_t = this.valeur.toLowerCase();
		if (valeur_t.length==0) {
			if(this.obligatoire) return " obligatoire - taille : "+this.taille_min+((this.taille_min!=this.taille_max)?" &#224; "+this.taille_max:"")+" caract&#232;res."
		}
		else {
			if (this.taille_min==this.taille_max&&valeur_t.length!=this.taille_min) return " de taille incorrecte - taille : "+this.taille_max+" caract&#232;res."
			if (this.taille_max!=0 && valeur_t.length>this.taille_max) return " trop long - taille maximum : "+this.taille_max+" caract&#232;res."
			if (valeur_t.length<this.taille_min) return " trop court - taille minimum : "+this.taille_min+" caract&#232;res."
		}

		if (this.carac_autorise=="") return "" // si rien ? v?rifier renvoi ok

		for (var i=0;i<valeur_t.length;i++) {
			if (this.carac_autorise.indexOf(valeur_t.charAt(i))==-1 && valeur_t.charAt(i)!="\n" && escape(valeur_t.charAt(i))!="%0D" ) {
				return " mal renseign&#233; - caract&#232;re non autoris&#233; : "+valeur_t.charAt(i)+".";
			}
		}

		if(this.regex != null)
		{
			if(!this.regex.test(valeur_t)&&valeur_t.length!=0) {return " mal renseign&#233;."}
		}
		return ""
	}

};
/* VerifFormTxt */
function switch_opacity(bloc_a_afficher,bloc_a_desafficher)
{
	 if(!browser.isIE6x)
	 {
		 new Effect.Opacity($(bloc_a_desafficher), {duration:0.5, from:1.0, to:0.0, beforeUpdate:function() {$(bloc_a_afficher).style.display="block";}, afterFinish:function(){$(bloc_a_desafficher).style.display="none";}});
		 new Effect.Opacity($(bloc_a_afficher), {duration:0.5, from:0.0, to:1.0});
	 }
	 else
	 {
		 //alert(bloc_a_desafficher)
		 $(bloc_a_desafficher).style.display="none";
		 //alert(bloc_a_afficher)
		 $(bloc_a_afficher).style.display="block";
	 }
}
