/**
 * class javascript fournissant des methodes pour la validation 
 * de formulaire
 * 
 * @package vt2
 * 
 * @author Damien ROCH
 * @copyright (c)Copyright 2005 VT-Design, {@link http://www.vt-design.com}
 * @since 1.0.0 3 mai 2003
 * @version $Id: ValidForm.class.js 10 juin 2005 15:20:00 droch $ 
 */

	/****************************************
	* constructeur
	* permet de créer un objet ValidForm
	*
	* @param : nom du formulaire (attribut name dans le html)
	*****************************************/	
	function ValidForm(form_){
	
		/*========================================================================== variables d'instance */
		
		//boolean de validation
		this.verification=true;
		
		//tab des elements à valider
		this.array_el=new Array();
		
		//tab des erreurs correspondantes
		this.array_err=new Array();
		
		//styles de l'élement
		this.borderErr="1px #ff0000 solid";
		this.borderOk="1px #0099FF solid";
		
		//styles du message d'erreur
		this.errorStyle 			= '#FF0000';
		this.errorStylePadding = '2px';
		
		//booleen - affiche toutes les erreurs ou une par une
		this.displayAll				=	false;
		
		//objet formulaire
		this.form = document.forms[form_];
		
		/*========================================================================== methode d'instance */
		
		this.text						=	verifForm_text; 					// verification de text
		this.email					=	verifForm_mail; 					// verification de mail
		this.number				=	verifForm_number; 				// verification de nombre
		this.numberVal			=	verifForm_numberVal; 			// comparaison de nombre
		this.compFields			=	veriForm_compFields			//	comparaison entre 2 champs
		this.liste					=	verifForm_liste;					// verification de liste select
		this.radio					=	verifForm_radio;					//verification si radio checked
		this.ereg 					= 	verifForm_ereg;					// verification d'un champs a partir d'une exprg
		this.password 			= 	verifForm_password;			// verification du champ de confirmation de password
		this.tel 						= 	verifForm_tel;						// verification du format dun champ telephone
		this.trim 					= 	verifForm_trim;					//trim une string
		
		this.displayError			=	verifForm_displayError;	//affiche les messages d'erreur
		this.init						=	verifForm_init;					//reinit la liste en supprimant les mess d'erreur
		this.removeError 		= 	verifForm_removeError;	
		this.addError 				=	verifForm_addError;
		
		
		
		this.init();
	}
	
	/****************************************
	* trim
	* Supprime les espaces de début et de fin d'une string
	*
	* @param : string
	*****************************************/	
	function verifForm_trim(str){
   		regex=new RegExp("(^ +)|( +$)", "g");
   		str=str.replace(regex, "");
   		return str;
	}

	
	/****************************************
	* input > verification des champs input
	*
	* @param : 2 inputs selected
	*****************************************/
	
	function verifForm_text(champ_text,nom_a_afficher,type_taille, val_taille, txt_opt){
	
		champ_text = this.form.elements[champ_text];
		champ_text.value = this.trim(champ_text.value);
		
		//si la taille fixe a été défini > !=0
		
		switch(type_taille){
			
			case "fixed": 
			
				if(champ_text.value.length!=val_taille){
					if(txt_opt != undefined)
					{
						var error = txt_opt;
					}else{
						var error = "Le champ "+nom_a_afficher+" doit comporter "+val_taille+" caractères !";
					}
					this.addError(champ_text,error);
				}else{					
					this.removeError(champ_text);
				}
				break;
			
			case "minimum":
				
				if(champ_text.value.length<val_taille){	
					if(txt_opt != undefined)
					{
						var error = txt_opt;
					}else{
						var error =	"Le champ "+nom_a_afficher+" doit comporter au moins "+val_taille+" caractères !";
					}
					this.addError(champ_text,error);
				}else{					
					this.removeError(champ_text);
				}
				break;
		}
	}
	
	function verifForm_tel(champ_text, obligatoire,separator){
	
		champ_text = this.form.elements[champ_text];
		valeur = this.trim(champ_text.value);
		champ_text.value = valeur;
			
		if(valeur.length!=0){
			
			if(separator==null) separator='.';
			
			//le telephone doit être de cette forme
			// /!\ Remarque [ \.\-] != [ \-\.] ?????????????
			var re = new RegExp("^([0-9]{2}[ \.\-]{0,1}){5}$");
			var re2 = new RegExp("^[\+]{1}[0-9]{1,3}[ \.\-]{1}[0-9]{1}[ \.\-]{0,1}([0-9]{2}[ \.\-]{0,1}){4}$");
			
			//on le reformate
			if(re.test(valeur)){				
				this.removeError(champ_text);
				var finaltxt = valeur.replace(/([0-9]{2})[ \-\.]{0,1}/g, "$1"+separator);
				finaltxt = finaltxt.replace(/[ \-\.]{0,1}$/, '');
				champ_text.value=finaltxt;	
			}else
			if(re2.test(valeur)){			
				this.removeError(champ_text);
				var finaltxt = valeur.replace(/([\+]{1}[0-9]{1,3})[ \-\.]{1}([0-9]{1})/, "$1-$2"+separator);
				finaltxt = finaltxt.replace(/([0-9]{2})[ \-\.]{0,1}/g, "$1"+separator);			
				finaltxt = finaltxt.replace(/[ \-\.]{0,1}$/, '');
				champ_text.value=finaltxt;		
			}else{
				this.addError(champ_text,'Le champ téléphone est incorrect!');
			}
		}else{
			if(obligatoire)
				this.addError(champ_text,'Le champ téléphone est obligatoire!');
		}
	}
	
	function verifForm_ereg(champ_text,ereg, mess, obligatoire, txt_opt){
	
		champ_text = this.form.elements[champ_text];
		champ_text.value = this.trim(champ_text.value);
		
		if(champ_text.value.length!=0){		
			if(champ_text.value.match(ereg))
				this.removeError(champ_text);			
			else			
				this.addError(champ_text,mess);	
		}else{
			if(obligatoire)
				if(txt_opt != undefined)
				{
					var error = txt_opt;
				}else{
					var error =	"Ce champ est obligatoire";
				}
				this.addError(champ_text, error);	
		}		
	}
	
	function verifForm_password(champ_pass1, champ_pass2){
		
		champ_pass1 = this.form.elements[champ_pass1];
		champ_pass2 = this.form.elements[champ_pass2];
		
		if(champ_pass1!= null){
			if(champ_pass1.value == champ_pass2.value){
				this.removeError(champ_pass2);
			}else{
				this.addError(champ_pass2,'Les mots de passe ne correspondent pas!');
			}
		}
	}
	
	function verifForm_liste(champ_liste,nom_a_afficher){
	
		champ_liste = this.form.elements[champ_liste];
		
		if(champ_liste.options[champ_liste.selectedIndex].value<=0){
			
			var error="Veuillez choisir une option dans la liste "+nom_a_afficher+" !";
			this.addError(champ_liste,error);
		}
	}
	
	function verifForm_radio(bt_radio,error,el_){
		
		bt_radio = this.form.elements[bt_radio];
		
		select = false;
		for (var i=0; i<bt_radio.length; i++){
			if(bt_radio[i].checked ==true) select=true;
		}
		
		if(!select){
			if(el_)
				this.addError(el_,error);
			else
				this.addError(bt_radio[0],error);
		}
	}
	
	
	
	function verifForm_mail(champ_mail,nom_a_afficher,obligatoire){
		
		champ_mail = this.form.elements[champ_mail];		
		valeur = this.trim(champ_mail.value);
		champ_mail.value = valeur;
				
		if(champ_mail.value.length!=0){
			
			var e = champ_mail.value;
		
			if (document.images){			
				re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

				if (!e.match(re_two))				
					this.addError(champ_mail,"Le format de l'email n'est pas valide!");					
			}
		}else{			
			if(obligatoire)
				this.addError(champ_mail,"Le champ "+nom_a_afficher+" est obligatoire!");
		}
	}
	
	
	function verifForm_number(champ_number, nom_a_afficher, type_taille, val_taille, obligatoire, txt_opt){
		
		champ_number = this.form.elements[champ_number];
		
		//chaine contenant ts les caraceteres acceptés:
		var ok = "1234567890";
		
		//si le champ n'est pas vide, on le teste
		if(champ_number.value.length!=0){
			
			var e=champ_number.value;
			//on teste chaque char
			for(i=0; i < e.length ;i++){

				if(ok.indexOf(e.charAt(i))<0){
					var error="Le caractère '"+e.charAt(i)+"' n'est pas accepté dans "+"le champ "+nom_a_afficher+"!";
					this.addError(champ_number, error);
					//break;
				}
			}
		
			switch(type_taille){
				
				case "fixed": 
					
					if(champ_number.value.length!=val_taille){
						var error="Le champ "+nom_a_afficher+" doit comporter "+val_taille+" chiffres!";
						this.addError(champ_number, error);
					}
					break;
				
				case "minimum":
				
					if(champ_number.value.length<val_taille){
						var error="Le champ "+nom_a_afficher+" doit comporter au moins "+val_taille+" chiffres!";
						this.addError(champ_number, error);
					}
					break;
			}
			
		}else{
			if(obligatoire){
				if(txt_opt != undefined)
				{
					var error = txt_opt;
				}else{
					var error= "Le champ "+nom_a_afficher+" est obligatoire!";
				}
				this.addError(champ_number, error);
			}
		}
	}
	
	
	function verifForm_numberVal(champ_number, nom_a_afficher, type_comp, val_comp, txt_opt)
	{
		
		champ_number 		= 	this.form.elements[champ_number];
		
		switch(type_comp)
		{
			case "<":
				if(champ_number.value >= val_comp){
					var error="Le champ "+nom_a_afficher+" doit être strictement inférieur à "+val_comp;
					if(typeof(txt_opt) == "string")error += " "+txt_opt;
					this.addError(champ_number, error);
				}
			break;
			
			case "<=":
				if(champ_number.value > val_comp){
					
					if(txt_opt != undefined)
						var error 	=	txt_opt;
					else
						var error	=	"Le champ "+nom_a_afficher+" doit être inférieur ou égal à "+val_comp;
						
					this.addError(champ_number, error);
				}
			break;
			
			case ">":
				if(champ_number.value <= val_comp){
					
					if(txt_opt != undefined)
						var error 	=	txt_opt;
					else
						var error	=	"Le champ "+nom_a_afficher+" doit être strictement supérieur à "+val_comp;
						
					this.addError(champ_number, error);
				}
			break;
			
			case ">=":
				if(champ_number.value < val_comp){
					var error="Le champ "+nom_a_afficher+" doit être supérieur ou égal à "+val_comp;
					if(typeof(txt_opt) == "string")error += " "+txt_opt;
					this.addError(champ_number, error);
				}
			break;
			
		}
		
	}
	
	/****************************************
	* compFields
	* Compare la valeur du champ 1 avec celle du champ 2 suivant le type spécifié (< >)
	*
	* @param : champ_number1, champ_number2, type_comp
	*****************************************/
	
	function veriForm_compFields(champ_number1, champ_number2, nom_a_afficher1, nom_a_afficher2, type_comp, txt_opt)
	{
		champ_number1 		= 	this.form.elements[champ_number1];
		champ_number2 		= 	this.form.elements[champ_number2];
		
		switch(type_comp)
		{
			case "<" :
				if(Number(champ_number1.value) >= Number(champ_number2.value)){
					
					if(txt_opt != undefined)
						var error 	=	txt_opt;
					else
						var error	=	"Le champ "+nom_a_afficher1+" doit être inférieur au champ "+nom_a_afficher2;
						
					this.addError(champ_number1, error);
				}
			break;
			
			case "<=" :
				if(Number(champ_number1.value) > Number(champ_number2.value)){
					
					if(txt_opt != undefined)
						var error 	=	txt_opt;
					else
						var error="Le champ "+nom_a_afficher1+" doit être inférieur ou égal au champ "+nom_a_afficher2;
						
					this.addError(champ_number1, error);
				}
			break;
			
			case ">" :
				if(Number(champ_number1.value) <= Number(champ_number2.value)){
					var error="Le champ "+nom_a_afficher1+" doit être supérieur au champ "+nom_a_afficher2;
					this.addError(champ_number1, error);
				}
			break;
			
			case ">=" :
				if(Number(champ_number1.value) < Number(champ_number2.value)){
					
					if(txt_opt != undefined)
						var error 	=	txt_opt;
					else
						var error="Le champ "+nom_a_afficher1+" doit être supérieur ou égal au champ "+nom_a_afficher2;
					
					this.addError(champ_number1, error);
				}
			break;
		}
		
	}
	
	
	/****************************************
	* trim
	* Supprime les espaces de début et de fin d'une string
	*
	* @param : string
	*****************************************/
	
	function verifForm_displayError(){
		var nbError_disp = 0;
		
		if(this.displayAll)
		{
			nbError_disp	=	this.array_err.length;
		}else{
			this.array_err.length == 0 ? nbError_disp = 0 : nbError_disp = 1; 
		}
		
		for(var i=0; i<nbError_disp; i++){	
			//nouveau noeud div
			var nouveauB = document.createElement("div");
			//noeud text du div
			var texte_nouveauB = document.createTextNode(this.array_err[i]);
			nouveauB.appendChild(texte_nouveauB);
			
			//nouveau noeud attribut NAME du div
			nouveauB.setAttribute('name', 'div_'+this.array_el[i].name);
				
 			//style nouveau div 			
 			nouveauB.style.color		=	this.errorStyle;	
 			nouveauB.style.padding	=	this.errorStylePadding;
			//nouveauB.style.cssFloat	=	"right";
 			//nouveauB.setAttribute('style', 'padding:'+this.errorStylePadding+'; color:'+this.errorStyle+';');

 			//insertion du noeud ds le document
 			var parent_node=this.array_el[i].parentNode;
			parent_node.insertBefore(nouveauB,this.array_el[i]);
		}
	}
	
	function verifForm_init(){
		
		var els = this.form.elements;
		for(var i=0; i<els.length; i++){
			var el = this.form.elements[i];					
			var el_errors = document.getElementsByTagName('div');
			//el.style.border=this.borderOk;
			for(var j=0; j<el_errors.length; j++){			
				var div = el_errors[j];
				if(div.getAttribute('name')!= null && div.getAttribute('name')== ('div_'+el.name)){
					var parent_node=el.parentNode;				
					parent_node.removeChild(div);
				}
			}			
		}
	}
	
	function verifForm_removeError(el){		
			var div= document.getElementById('div_error');
			//el.style.border=this.borderOk;
			var parent_node=el.parentNode;
			//parent_node.removeChild(div);
		
			
			
	}
	
	function verifForm_addError(el, mess){
		this.verification=false;
		this.erreur+=mess;
		this.array_err.push(mess);
		this.array_el.push(el);
		//el.style.border=this.borderErr;
		this.verification=false;
	}
	

