// JavaScript Document
/* gVerifFrom */
GVerifForm = Class.create();
GVerifForm.prototype = {
		initialize: function(conteneur,ancre) {
			this.erreur ="";
			this.conteneur = $(conteneur)
			this.ancre = ancre;
			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 = "";
			if(!browser.isIE)
			{
				this.ghost_conteneur = document.createElement('div')
				this.ghost_conteneur.className = this.conteneur.className
				this.ghost_conteneur.style.height = '0'
				this.ghost_conteneur.style.overflow = 'hidden'
				this.conteneur.parentNode.appendChild(this.ghost_conteneur);
			}
			this.conteneur_verif = new Array;
		},
		ecrire: function(){
			this.conteneur.innerHTML = "<div>" + this.ligne_intro + this.liste + "</div>";
			if(!browser.isIE)
			{
				this.ghost_conteneur.innerHTML = this.conteneur.innerHTML;
				this.h = Element.getHeight(this.ghost_conteneur.firstChild);
				//alert(this.h)
			}
		},
		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()!="")
				{
					liste += "<li>Le champ "+element.intitule+" est"+element.traitement()+"</li>";
					element.changeStyle(true)
				}
			});	
			// si erreur
			if(liste!="")
			{
				this.liste = "<ul>"+liste+"</ul>";
				
				if(this.affiche())
				{
					Element.setStyle(this.conteneur,{height:'auto'});
					this.ecrire();
				}
				else
				{
					//defixer la taille pour que la liste ne prenne pas toute sa place initiale
					this.ecrire()
					if(browser.isIE){Element.fixeElement(this.conteneur)}
					if(!browser.isIE){Element.setStyle(this.conteneur,{height:this.h+'px'});}
					new Effect.BlindDown(this.conteneur);
				}
				// test "ancre"
				if(this.ancre!="")
				{
					Element.versPosition(this.ancre)
				}
				
				return false
			}
			else
			{
				if(this.affiche())
				{
					Element.fixeElement(this.conteneur)
					this.supprimer()
					Effect.BlindUp(this.conteneur);
				}
				return true
			}
		}


};
/* /gVerifFrom */
/* VerifFrom */
VerifForm = Class.create();
VerifForm.prototype = {
	initialize: function(element,intitule,obligatoire,taille_min,taille_max,carac_autorise,type) {
		this.element = $(element);
		this.intitule = intitule;
		this.valeur = Form.Element.getValue(this.element);
		this.obligatoire = obligatoire;
		this.taille_min = taille_min;
		this.taille_max = (taille_max=='max') ? this.element.maxLength : taille_max;
		switch (carac_autorise)
		{
			case "chiffre":
				this.carac_autorise = "0123456789";
				
			break;
			case "nombre":
				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" :
				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 && this.taille_max!=0)?" à "+this.taille_max:"")+" caractères."
				}
				else {
					if (this.taille_min==this.taille_max&&valeur_t.length!=this.taille_min) return " de taille incorrecte - taille: "+this.taille_max+" caractères."
					if (this.taille_max!=0 && valeur_t.length>this.taille_max) return " trop long - taille maximum: "+this.taille_max+" caractères."
					if (valeur_t.length<this.taille_min) return " trop court - taille minimum: "+this.taille_min+" caractè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é - caractère non autorisé: "+valeur_t.charAt(i)+".";
					}
				}
				return ""
			break;
			
			/* email */
			case "email" :
				this.carac_autorise = "abcdefghijklmnopqrstuvwxyzéèêëôöîïàâäûüç0123456789.,- '()€$£%[]{}&=+@?!/°;:*~_’";
				var unemail = /^((\w+(\-\w+)*)*\.?(\w+(\-\w+)*))+@((\w+(\-\w+)*)*\.(\w+(\-\w+)*))+$/;
				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)?" à "+this.taille_max:"")+" caractères."
				}
				else {
					if (this.taille_min==this.taille_max&&valeur_t.length!=this.taille_min) return " de taille incorrecte - taille: "+this.taille_max+" caractères."
					if (this.taille_max!=0 && valeur_t.length>this.taille_max) return " trop long - taille maximum: "+this.taille_max+" caractères."
					if (valeur_t.length<this.taille_min) return " trop court - taille minimum: "+this.taille_min+" caractè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é - caractère non autorisé: "+valeur_t.charAt(i)+".";
					}
				}
				
				if(!unemail.test(valeur_t)&&valeur_t.length!=0) {return " mal renseigné."} 
				
				return ""
			break;

			/* defaut */
			default:
				return ""
			break;
		}
	}
};
/* VerifFrom */
