/**
 * 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 #0067B2 solid";

		//styles du message d'erreur
		this.errorStyle = '#FF0000';
		this.errorStylePadding = '2px;';

		//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.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.link = verifForm_link;					//verification d'une url http://

		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.textLength=verifForm_textLength;
		this.required=verifForm_required;

		this.init();
		
	}

	/****************************************
	* trim
	* Supprime les espaces de d???but et de fin d'une string
	*
	* @param : string
	*****************************************/
	function verifForm_trim(str){
		if(str!=null){
   			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){

		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){
					//var error = "\n\n 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){
					//var error="\n\n 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, mess, 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,mess);
			}
		}
	}

	/**
	   * verifie la longueur d'un champ
	   *
	   */
	function verifForm_textLength(champ_text,mess, min_taille, max_taille){

		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.length<min_taille || champ_text.value.length>max_taille){
				this.addError(champ_text,mess);
			}else{
				this.removeError(champ_text);
			}
		}
	}

	/**
	   * verifie qu'un champ est rempli
	   *
	   */
	function verifForm_required(champ_text, mess){

		if(this.form.elements[champ_text].options!=null){
			//c une liste
			champ_liste = this.form.elements[champ_text];
//modified by damien 06dec06
			if(this.form.elements[champ_text].options.length==0 || champ_liste.options[champ_liste.selectedIndex].value<0 || !champ_liste.options[champ_liste.selectedIndex].value){
				this.addError(champ_liste,mess);
			}
		}else{
			//c un champ texte
			champ_text = this.form.elements[champ_text];
		
			champ_text.value = this.trim(champ_text.value);
		
			if(champ_text.value.length==0)
					this.addError(champ_text,mess);			
		}

	}

	/**
	   * verifie la syntaxe d'un champs par le biais d'une regex
	   *
	   */
	function verifForm_ereg(champ_text,ereg, mess){

		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);
		}
	}

	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="\n\n 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,mess){

		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})(\]?)$/;
				re_two = /^[a-zA-Z0-9_\-\.]+\@(\[?)[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!");
			}
		}
	}

	function verifForm_number(champ_number, mess, min_val, max_val){
		champ_number = this.form.elements[champ_number];
		valeur =  parseFloat(this.trim(champ_number.value));
	
		if(champ_number.value != "" && (isNaN(valeur) || ((max_val != min_val) && (valeur>=max_val || valeur<=min_val)))){
			this.addError(champ_number, mess);
		}else{
			champ_number.value =valeur;	
		}
		
	}



	function verifForm_link(champ_text, mess){

		var ereg = /^((http|https|ftp):\/\/){1}((www|[0-9a-zA-Z]+)\.){1}([0-9a-zA-Z]+(-[0-9a-zA-Z]+)*)(\.[a-zA-Z]{2,3}){1}(\/(-_[0-9a-zA-Z])+)*/;

		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);
		}
	}

	/*function verifForm_number(champ_number, nom_a_afficher, type_taille, val_taille, obligatoire){

		champ_number = this.form.elements[champ_number];

		//la var de ferif interne:
		var verifOk=true;

		//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){
					verifOk=false;
					var error="\n\n Le caract???re '"+e.charAt(i)+"' n'est pas accept??? dans "+
					      "le champ "+nom_a_afficher+"!";
					this.erreur+=error;
					this.array_err.push(error);
					this.array_el.push(champ_number);
					break;
				}
			}

			switch(type_taille){

				case "fixed":

					if(champ_number.value.length!=val_taille){

						verifOk=false;
						var error="\n\n Le champ "+nom_a_afficher+" doit comporter "+val_taille+" chiffres!";
						this.erreur+=error;
						this.array_err.push(error);
						this.array_el.push(champ_number);
					}
					break;

				case "minimum":

					if(champ_number.value.length<val_taille){
						verifOk=false;
						 var error="\n\n Le champ "+nom_a_afficher+" doit comporter au moins "+val_taille+" chiffres!";
						 this.erreur+=error;
						this.array_err.push(error);
						this.array_el.push(champ_number);
					}
					break;
			}

		}else{
			if(obligatoire){
				verifOk=false;
				var error="\n\n Le champ "+nom_a_afficher+" est obligatoire!";
				this.array_el.push(champ_number);
				this.erreur+=error;
				this.array_err.push(error);
			}
		}

		//on traite le r???sultat
		if(verifOk){
			champ_number.style.border=this.borderOk;
		}else{
			champ_number.style.border=this.borderErr;
			this.verification=false;
		}
	}*/

	/****************************************
	* trim
	* Supprime les espaces de d???but et de fin d'une string
	*
	* @param : string
	*****************************************/

	function verifForm_displayError(showAll){
		
		if(showAll==null) showAll=true;
						
		for(var i=0; i<this.array_err.length; 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.className = nouveauB.className+" requiredError";

 			/*nouveauB.style.padding=this.errorStylePadding;*/
			
			this.array_el[0].focus();
					
 			//insertion du noeud ds le document
 			var parent_node=this.array_el[i].parentNode;
 			parent_node.insertBefore(nouveauB,this.array_el[i]);
 			
 			if(!showAll) return false;
		}		
	}

	function verifForm_init(){

		var els = this.form.elements;
				
		for(var i=0; i<els.length; i++){
			var el = this.form.elements[i];
			el.style.borderColor='';
			/*var el_errors = document.getElementsByTagName('div');
			//el.style.border='auto'; //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);
				}
			}
		}		*/
			
			var parent=el.parentNode;
			var childs=parent.childNodes;
			
			for(var f=0; f<childs.length; f++){
				
				var child=childs[f];
				
				if(child.nodeName=="DIV" && child.getAttribute('name')!= null && child.getAttribute('name')== ('div_'+el.name)){
					parent.removeChild(child);
					f--;
					/* console.log("name="+child.getAttribute('name')); */
				}
			}
		}
	}

	function verifForm_removeError(el){
			var div= document.getElementById('div_error');
			//el.style.border=this.borderOk;
			el.style.border='';
			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;
	}

