function valid(form,lien,optverif,tabverif)
{
	if(optverif=='n')
	{
		err='';
	}else{
		err=verif(tabverif);
	}
	if(err=='')
	{
		document.getElementById(form).action=lien;
		//alert(lien);
		document.getElementById(form).submit();
	}else{
		alert(err)
	}
}
function verif(tabverif)
{
	err=''
	for(var champ in tabverif)
	{
		for(var opt in tabverif[champ])
		{
			//alert(champ+"->"+opt);
			switch(opt)
			{

				case 'oblig':err+=champ_oblig(champ,tabverif[champ]['label']);break;
				case 'longMax':err+=champ_longmax(champ,tabverif[champ]['label'],tabverif[champ][opt]);break;
				case 'longMin':err+=champ_longmin(champ,tabverif[champ]['label'],tabverif[champ][opt]);break;
				case 'num':err+=champ_num(champ,tabverif[champ]['label']);break;
				case 'mail':err+=champ_mail(champ,tabverif[champ]['label']);break;
				case 'dateFR':err+=champ_dateFR(champ,tabverif[champ]['label']);break;
				case 'valeurSup':err+=champ_valeurSup(champ,tabverif[champ]['label'],tabverif[champ][opt]);break;
				case 'valeurInf':err+=champ_valeurInf(champ,tabverif[champ]['label'],tabverif[champ][opt]);break;
				case 'valeurEg':err+=champ_valeurEg(champ,tabverif[champ]['label'],tabverif[champ][opt]);break;
			}
		}
	}
	return err;
}
function champ_oblig(champ,label)
{
	err='';
	if(document.getElementById(champ).value=='')err="-Le champ "+label+" est obligaoire.\r\n";
	return err;
}
function champ_longmax(champ,label,longmax)
{
	err='';
	if(document.getElementById(champ).value!="" && document.getElementById(champ).value.length>longmax)err="-Le champ "+label+" doit être inférieur à "+longmax+"\r\n";
	return err;
}
function champ_longmin(champ,label,longmin)
{
	err='';
	if(document.getElementById(champ).value!="" && document.getElementById(champ).value.length<longmin)err="-Le champ "+label+" doit être supprérieur à "+longmin+"\r\n";
	return err;
}
function champ_num(champ,label)
{
	err='';
	if(document.getElementById(champ).value!="" && isNaN(document.getElementById(champ).value))err="-Le champ "+label+" doit être numérique.\r\n";
	return err;
}
function champ_mail(champ,label)
{
	err='';
	if(document.getElementById(champ).value!="")
	{
		//alert(document.getElementById(champ).value.match([_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-\.]+(\.[_a-zA-Z\d\-]));
		//if(document.getElementById(champ).value.match(/^([\w]+)@([\w]+)\.([\w]+)$/)==null) err="-Le format de l'adresse e-mail du champ "+label+" n'est pas valide.\r\n";
		/*var re=/^[a-z\d]+((\.|-|_)[a-z\d]+)*@((?![-\d])[a-z\d-]{0,62}[a-z\d]\.){1,4}[a-z]{2,6}$/gi;
		if(sMail.match(re)==sMail)&&(substr(sMail.lastIndexOf("@")+1).length<=255);
		{
			err='';
		}else
		{
			err="-Le format de l'adresse e-mail du champ "+label+" n'est pas valide.\r\n";
		}*/
		adresse = document.getElementById(champ).value;
		var place = adresse.indexOf("@",1);
		var point = adresse.indexOf(".",place+1);
		if ((place > -1)&&(adresse.length >2)&&(point > 1))
		{
			err='';
		}
		else
		{
			err="-Le format de l'adresse e-mail du champ "+label+" n'est pas valide.\r\n";
		}
	}
	/*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 (sMail.match(re)==sMail)&&(substr(sMail.lastIndexOf("@")+1).length<=255);*/
	
	return err;
}
function champ_dateFR(champ,label)
{
	err='';
	valeur=document.getElementById(champ).value;
	if(valeur!="")
	{
		if(isNaN(valeur)&&(valeur.length==10||valeur.length==8))
		{
			if (valeur.substr(0,2)<=0 || valeur.substr(0,2)>31 ||isNaN(valeur.substr(0,2))|| valeur.substr(3,2)<=0 || valeur.substr(3,2)>12 ||isNaN(valeur.substr(3,2))|| valeur.substr(6,(valeur.length-6))<=0 ||isNaN(valeur.substr(6,(valeur.length-6))))
			{
				err="-Le format de la date du champ "+label+" est incorrect \r\n";
			}
		}else
		{
			if(valeur.length==6 || valeur.length==8)
			{
				if (valeur.substr(0,2)<=0 || valeur.substr(0,2)>31 || valeur.substr(2,2)<=0 || valeur.substr(2,2)>12 || valeur.substr(4,(valeur.length-4))<=0)
				{
					err="-Le format de la date du champ "+label+" est incorrect \r\n";
				}
			}else{
				
				err="-Le format de la date du champ "+label+" est incorrect \r\n";
			}
		}
	}
	return err;
}
function champ_valeurSup(champ,label,valeur)
{
	err='';
	if (document.getElementById(champ)!="" && document.getElementById(champ).value<=valeur)err="-Le champ "+label+" doit être suppérieur à "+valeur+"\r\n";
	return err;
}
function champ_valeurInf(champ,label,valeur)
{
	err='';
	if (document.getElementById(champ)!="" && document.getElementById(champ).value>=valeur)err="-Le champ "+label+" doit être inférieur à "+valeur+"\r\n";
	return err;
}
function champ_valeurEg(champ,label,valeur)
{
	err='';
	if (document.getElementById(champ)!="" && document.getElementById(champ).value!=valeur)err="-Le champ "+label+" doit être égale à "+valeur+"\r\n";
	return err;
}
function limiteCract(champ,taille)
{
	if(document.getElementById(champ).value.length>=taille)
	{
		document.getElementById(champ).value=document.getElementById(champ).value.substr(0,taille);
		alert("La taille du champ est limité à "+taille+" caractères");
	}
}


function CreerListe(nom,id,form,idsearch, hauteur, largeur,js) {
	this.nom=nom; this.hauteur=hauteur; this.largeur=largeur;
	this.id=id;
	this.form=form;
	this.idsearch=idsearch;
	this.js=js;
	this.search="";
	this.nb=0; 
	this.Add=AjouterItem;
	this.Afficher=AfficherListe;
	this.MAJ=MAJListe;
	this.Val=valider;
}

function AjouterItem(item) {
	this[this.nb]=item
	this.nb++;
}

function AfficherListe() {
	onch='onchange=validclick('+this.form+',"'+this.idsearch+'","'+this.nom+'","'+this.js+'")';
	if (document.layers) {
		var Z="<SELECT size="+this.hauteur+" id="+this.id+" "+onch+">";
	} else {
		var Z="<SELECT 	id="+this.id+" size="+this.hauteur+" style='width:"+this.largeur+"' "+onch+">";
	}
	for (var i=0; i<this.nb; i++) {
		Z+="<OPTION value=\""+eval(this.nom+'Optid[i]')+"\">"+this[i]+"</OPTION>"		
	}
	Z+="</SELECT>"
	document.write(Z);
}

function MAJListe(txt,f) {
	if (txt!=this.search) {
		this.search=txt
		f.elements[this.nom].options.length=0; 
		for (var i=0; i<this.nb; i++) {
			if ( this[i].substring(0,txt.length).toUpperCase()==txt.toUpperCase() ) {
				var o=new Option(this[i],eval(this.nom+'Optid[i]'));
				f.elements[this.nom].options[f.elements[this.nom].options.length]=o;
			}
		}
		if (f.elements[this.nom].options.length==1) {
			f.elements[this.nom].selectedIndex=0;

		}
	}
}
/*function valider(f,nomsearch,send)
{
		if(send=='true')
		{
			//val=document.getElementById(this.id).value
			alert(eval(this.form+".elements[this.nom].selectedIndex"));
			if(eval(this.form+".elements[this.nom].selectedIndex")!=-1)
			{
				val=eval(this.form+".elements[this.nom].options["+this.form+".elements[this.nom].selectedIndex].value");
				//ind=eval(this.nom+'Optname.indexOf(val)');
				alert(val);
				document.getElementById(nomsearch).value=val;
				//alert(this.js);
				eval(this.js);
			}
		}
}*/
function valider(f,idsearch,send)
{
	if(f.elements[this.nom].selectedIndex!=-1)
	{
		val=f.elements[this.nom].options[f.elements[this.nom].selectedIndex].value;
		document.getElementById(idsearch).value=val;
		if(send==true)
		{
			eval(this.js);
		}
	}
}

function validclick(f,idsearch,nom,js)
{
	if(f.elements[nom].selectedIndex!=-1)
	{
		val=f.elements[nom].options[f.elements[nom].selectedIndex].value;
		document.getElementById(idsearch).value=val;
		eval(js);
	}
	
}
function ListeCheck(nomliste,nomsearch,nomform,send) {
	//Liste.MAJ(document.forms["Form1"].search.value,document.forms["Form1"])
	//alert(nomliste);
	eval(nomliste+".MAJ(document.getElementById(nomsearch+'sh').value,document.getElementById(nomform))");
	//alert(eval(nomliste+'shOld'));
	//alert(document.getElementById(nomsearch+'sh').value);
	if(document.getElementById(nomsearch+'sh').value==eval(nomliste+'shOld'))send=false;
	else eval(nomliste+'shOld=document.getElementById(nomsearch+"sh").value');
	eval(nomliste+".Val(document.getElementById(nomform),nomsearch,send)");
	//alert(document.getElementById(nomliste).value);
	/*if (document.layers) {
		setTimeout("ListeCheck()", 1001)
	} else {
		setTimeout("ListeCheck()", 100)
	}*/

}

function placer_focus(tabCh)
{
	/*flagfocus='false';
	i=0;
	for each (champs in tabfocus)
	{
		if((document.getElementById(champs).value=="" || i==(tabfocus.length-1)) && flagfocus=='false')
		{
			document.getElementById(champs).focus();
			flagfocus='true';
		}
		i++;
	}*/
	flagfocus='false'
	for(i=0;i<tabCh.length;i++)
	{
		if((document.getElementById(tabCh[i]).value=="" || i==(tabCh.length-1)) && flagfocus=='false')
		{
			
			document.getElementById(tabCh[i]).focus();
			flagfocus='true';
		}
	}
}
