//V 1.6


/*
			nom="NOM DE L'OBJET DU FORMULAIRE"
			,"NOM DU FORMULAIRE"
			,"TEXTE DE L'ALERTE POUR CHAMP VIDE"
			,"TEXTE DE L'ALERTE POUR CHAMP AVEC TYPE INCORRECT"
			,"TYPE DE L'OBJET"   ("select"  /  "radio"  /  "checkbox")
			,"FORMAT DE L'OBJET" ("jj/mm/aaaa"  "aaaa/mm/jj" ... )
			,0 (0 <=> REQUIS   |   1 <=> NON REQUIS)
			,500 (Nbre max de caracteres)
			,"CONDITION JAVASCRIPT QUI DECLENCHE L'ALERTE POUR CHAMP VIDE"
			,"CONDITION JAVASCRIPT SUPPLEMENTAIRE"
			,"TEXTE DE L'ALERTE QUAND CONDITION SPECIALE VALIDE"
*/


tpsClignotte=100
MAC=(navigator.userAgent.indexOf('Mac')!=-1)?1:0;
NS4=(document.layers)?1:0; var IE=(document.all)?1:0;

numErreur=0
nomForm=""

function newChp()
{
	var a=newChp.arguments
	args="";virg=""
	for(i=0;i<a.length;i++){args+=virg+"\""+a[i]+"\"";virg=","}
	eval("Chp"+(++indexChp)+"=new C_Chp("+args+")")
	H_NomsIndex[nom]=indexChp;
}

function C_Chp()
{
	var a=C_Chp.arguments
	this.nom=a[0]
	this.nom_form=a[1]
	this.alertVide=a[2]
	this.alertType=a[3]
	this.type=a[4]
	this.format=a[5]
	this.vide=a[6]
	this.maxChars=a[7]
	this.specialCondAnd=a[8]
	this.specialCondPlus=a[9]
	this.alertSpecialCondPlus=a[10]
	return
}

function getObj(indexChp, nomProprerty)
{
	return eval("Chp"+indexChp+"."+nomProprerty)
}

function fct_alert(obj, nom, str_alert)
{
	obj.focus()
	alert(str_alert)
	if(!NS4 && !MAC && type!="radio" && type!="checkbox" && clignotteActif)clignotte(nom,0,10,0)
	return false
}

function setCouleurErreur(nom)
{
	setCouleurBG(nom, couleurErreur)
}

function setCouleurBG(nom, couleur)
{
	eval('document.'+nomForm+'.'+nom+'.style.background="'+couleur+'"')
}
	
function clignotte(nom,c,nbre,indexC)
{
	eval('document.'+nomForm+'.'+nom+'.style.background=('+c+')?"'+couleurClignotte1+'":"'+couleurClignotte0+'"')
	c=1-c
	indexC++
	if(indexC>nbre)
	{
		if(changeCouleur) setCouleurErreur(nom)
		return
	}
	setTimeout("clignotte('"+nom+"',"+c+","+nbre+","+indexC+")",tpsClignotte)
}

function test_form(formulaire)
{
	var val;
	nomForm=formulaire.name
	for(var indexChp=0;indexChp<nbreChp;indexChp++)
	{
		nom=getObj(indexChp, "nom")
		exceptField=0
		for(var i=0;i<tab_except_fields_of_test_form.length;i++)
		{
			if(tab_except_fields_of_test_form[i] == nom)
			{
				exceptField=1
				break
			}
		}
		if(exceptField) continue
		type=getObj(indexChp, "type")
		var format=getObj(indexChp, "format")
		var str_alert_vide=getObj(indexChp, "alertVide")
		var str_alert_type=getObj(indexChp, "alertType")
		var str_specialCondPlus=getObj(indexChp, "specialCondPlus")
		var str_specialCondAnd=getObj(indexChp, "specialCondAnd")
		var str_alert_special_cond=getObj(indexChp, "alertSpecialCondPlus")
		eval("var obj=formulaire."+nom)

		// CAS OU IL Y A 2 FORMULAIRES DS LA MEME PAGE
		if(getObj(indexChp, "nom_form")!=nomForm) continue;

		
		if(typeof(obj)=="undefined")
		{
			alert("ERREUR: le champ '"+nom+"' n'existe pas")
			return false
		}
		if(type=="radio")
		{
			var str="";ok=0
			for(i=0;i<obj.length;i++)
				if(obj[i].checked)
				{
					str=obj[i].value
					obj=obj[i]
					ok=1
				}
			if(!ok) {obj=obj[0];nom=nom+"[0]"}
		}
		else if(type=="select")
		{
			var str=obj.options[obj.selectedIndex].value
		}
		else if(type=="checkbox")
		{
		
		}
		else
		{
			if(!NS4 && typeof(obj.selectedIndex)!="undefined") alert("ERREUR: preciser type => 'select'")
			var str=obj.value
		}

		if(str_specialCondAnd=="") specialCondAnd=1
		else specialCondAnd=eval(str_specialCondAnd)
		
		
		// TEST CHECKBOX
		if(type=="checkbox" && !obj.checked && specialCondAnd)
			return fct_alert(obj, nom, str_alert_type)

		// TEST CHP VIDE
		if(getObj(indexChp, "vide")==0 && str.length==0 && specialCondAnd)
			return fct_alert(obj, nom, str_alert_vide)
			
		// TEST CHP DATE
		if(type=='date' && str!="")
		{
			if(format!="")
			{
				posj=format.search(/jj/i)
				posm=format.search(/mm/i)
				posa=format.search(/aaaa/i)
				poss=format.search(/\W/)
				poss2=format.substring(poss+1, format.length).search(/\W/)+poss+1
				s1=format.substring(poss, poss+1)
				s2=format.substring(poss2, poss2+1)

				s_jj=str.substring(posj, posj+2)
				s_mm=str.substring(posm, posm+2)
				s_aaaa=str.substring(posa, posa+4)
				jj=parseInt(s_jj*1)
				mm=parseInt(s_mm*1)
				aaaa=parseInt(s_aaaa*1)
				sep1=str.substring(poss, poss+1)
				sep2=str.substring(poss2, poss2+1)

				if(
				(
					isNaN(jj) || isNaN(mm) || isNaN(aaaa)
					|| jj<1 || jj>31 || mm<1 || mm>12 || aaaa<1900 || aaaa>3000
					|| s1!=sep1 || s2!=sep2
					|| s_jj.indexOf(" ")!="-1" || s_mm.indexOf(" ")!="-1" || s_aaaa.indexOf(" ")!="-1"
				)	&& specialCondAnd
					)
					return fct_alert(obj, nom, str_alert_type)
			}
			else
				return alert("ERREUR: Format du champ '"+nom+"' non renseigné !")
		}
		
		// TEST CHP HEURE
		if(type=='heure' && str!="")
		{
			val=parseInt(str)
			if(val<0 || val>23 || isNaN(val) || specialCondAnd)
				return fct_alert(obj, nom, str_alert_type)
			obj.value=val
		}

		// TEST CHP MINUTES
		if(type=='min' && str!="")
		{
			val=parseInt(str)
			if(val<0 || val>59 || isNaN(val) || specialCondAnd)
				return fct_alert(obj, nom, str_alert_type)
			obj.value=val
		}

		// TEST CHP MAIL
		if(type=='email' &&  str!="")
		{
			if (!isEmail(obj) && specialCondAnd)
				return fct_alert(obj, nom, str_alert_type)
		}

		// TEST CHP NUM
		if(type=='num' && str!="")
		{
			val=parseInt(str)
			if(isNaN(val) && specialCondAnd)
				return fct_alert(obj, nom, str_alert_type)
//			obj.value=val
		}

		// TEST CONDITON SPECIALE PLUS
		if(str_specialCondPlus!="" && eval(str_specialCondPlus))
			return fct_alert(obj, nom, str_alert_special_cond)

		//
		if(type!='radio' && type!='checkbox'  && !NS4 && !MAC)
			setCouleurBG(nom, couleurDefaut)
	}
	return true
}

function isEmail(eml)
{
	a = eml.value.search(/^[-0-9_.a-z]+@([-0-9a-z][-0-9a-z]+\.)+[a-z][a-z]+$/i)
	if(a!=-1)
		return true
	else
		return false
}

function CheckLen(nom)
{
	maxLength=getObj(H_NomsIndex[nom], "maxChars")
	if(maxLength==0) return 
	Target=eval("document."+nomForm+"."+nom)
	StrLen = Target.value.length

	if (StrLen > maxLength)
	{
		Target.value = Target.value.substring(0,maxLength)
		CharsLeft = maxLength
	}
	else
	{
		CharsLeft = StrLen;
	}
	if(typeof(eval('document.'+nomForm+'.CarCount_'+nom))!="undefined")
		eval('document.'+nomForm+'.CarCount_'+nom+'.value = maxLength - CharsLeft;')
}

