var timer = "";
var messageAlert = "";
//fonction pour tester la valeur numérique d'un champ (accepte les ".")
function isNum(idChamp)
	{
	n = document.getElementById(idChamp).value;
	if (!(isNaN (n)) == false)
		{
		nomChamp = getLabel(idChamp);
		var errmess = document.getElementById("error_number").innerHTML;
		messageAlert = messageAlert + "\n - " + nomChamp + " : " + errmess;
		document.getElementById(idChamp).style.border = "1px red solid" ;
		}
	else
		{
		document.getElementById(idChamp).style.border = "1px #ADB0B0 solid" ;
		}
	}

//fonction de controle des confirmation de valeur de champ
function controlEquivalence(idChamp1, idChamp2)
	{
	var valeur1 = document.getElementById(idChamp1).value;
	var valeur2 = document.getElementById(idChamp2).value;
	nomChamp1 = getLabel(idChamp1);
	nomChamp2 = getLabel(idChamp2);
	if(valeur1 != "" && valeur2 != "")//si les deux champs = "" on laisse faire controlMandatory
		{
		if(valeur1 != valeur2)
			{
			var errmess = document.getElementById("error_equivalence").innerHTML;
			messageAlert = messageAlert + "\n - " + nomChamp1 + " & " + nomChamp2 + " : " + errmess;
			document.getElementById(idChamp1).style.border = "1px red solid" ;
			document.getElementById(idChamp2).style.border = "1px red solid" ;
			}
		else
			{
			document.getElementById(idChamp1).style.border = "1px #ADB0B0 solid" ;
			document.getElementById(idChamp2).style.border = "1px #ADB0B0 solid" ;
			}
		}
	}
//fonction de controle des champs email
function controlEmail(idChamp) 
	{ 
	var email = document.getElementById(idChamp).value.toLowerCase(); 
	//document.getElementById(idChamp).value = email;
    if (email.search(/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9]{2,4})*$/) == -1)
	//[^._-]
        { 
       	nomChamp = getLabel(idChamp);
		var errmess = document.getElementById("error_email").innerHTML;
		messageAlert = messageAlert + "\n - " + nomChamp + " : " + errmess;
		document.getElementById(idChamp).style.border = "1px red solid" ;
      	} 
	else
		{
		document.getElementById(idChamp).style.border = "1px #ADB0B0 solid" ;
		}
	}
//fonction de controle des champs obligatoires
function controlMandatory(champ) {
	submitit = true;
	var errmess = "";
	if (document.getElementById("error_mandatory"))
		{
		var errmess = document.getElementById("error_mandatory").innerHTML;
		}
		else
	{
	var errmess = "";
		}	
	if(document.getElementById("error_mandatory")!=null)
	var messBase = document.getElementById("error_mandatory").innerHTML;
	if(champ==undefined)//cas d'une liste de champ obligatoire dans un input hidden de la page
		{
		
		var listeChamps = document.getElementById("mandatory_id").value.split("//");
		var nbChamps = listeChamps.length;
		for(i=0;i<nbChamps;i++)
			{
			idChamp = listeChamps[i];
			//alert(idChamp);
			//if(document.getElementById(idChamp).value == "" ) 
			idChampCopie=idChamp;
			if (document.address_form)
			if (i==nbChamps-1)idChamp= idChamp+document.address_form.subject.selectedIndex;
			if(isEmpty(trim(document.getElementById(idChamp).value)))
				{
				
					submitit = false;
				errmess = errmess + "\n - " + getLabel(idChampCopie) + " : " + "Campo obligatorio";
					document.getElementById(idChamp).style.border = "1px red solid" ;
				}
			else
				{
				document.getElementById(idChamp).style.border = "1px #ADB0B0 solid" ;
				}
			}
		if(submitit == false)
			{
			messageAlert = messageAlert+errmess;
			}
		}
	else //cas du controle d'un seul champ
		{
		if(document.getElementById(champ).value == "") 
			{
			submitit = false;
			messageAlert = messageAlert + "\n - " + getLabel(champ) + " : " + messBase;
			document.getElementById(champ).style.border = "1px red solid" ;
			}
		else
			{
			document.getElementById(champ).style.border = "1px #ADB0B0 solid" ;
			}
		}
	}
	


function validSubmit()
	{
	if(messageAlert != "")
		{
		var message_base = document.getElementById("error_form").innerHTML;
		alert(message_base + messageAlert);
		return false;
		}
	else
		{
		return true
		}
	}
	
function getLabel(valeur)
	{
	var element_label = "";
	var txt_label = "";
	var liste = "";
	var nomChamp = "";
	//var formulaire = document.getElementById(nomform);
	for(var i=0; i<document.body.getElementsByTagName("label").length; i++ )
	 	{
		//determination de label a comparer
		element_label = document.body.getElementsByTagName("label")[i];
		//récupération de la valeur texte contenue dans le label
		txt_label = element_label.firstChild.nodeValue;
		//récupération de l'attribut for du label
		if(element_label.getAttribute("for"))
			{contFor = element_label.getAttribute("for");}
		else
			{contFor = element_label.getAttribute("htmlFor");}
		//liste = liste + "\n||" + valeur + "?=" + contFor + "||"; 
		//comparaison de l'attribut for a la valeur entree dans l'appel de fonction
		if(contFor == valeur) 
			{
			nomChamp = txt_label;
			break;
			}
		}
	return nomChamp;	
	}

function controleDiags(formname)
	{
	var nomObjet = "";
	var nb = 0;
	var n = 0;//nombre d'élément radio !=
	var m = 0;//nombre de questions avec checkbox
	var arrObjet = new Array(); //tableau contenant les différents nom d'objet et leur nombre d'occurence
	var test = false; 
	var presence = false;
	var complete = true;//variable définissant l'état completé ou non du formulaire
	var racine = "";
	//Etape 1 : identification des différents nom de radio et insertion dans un tableau js
	if(formname == undefined)
		{
		racine = document.body.getElementsByTagName("input");
		}
	else
		{
		racine = document.getElementById(formname).getElementsByTagName("input");
		}
		
	for(var i=0; i<racine.length; i++ )
	 	{
		elementRadio = racine[i];
		nomObjet = elementRadio.name;
		typeObject = elementRadio.type;
		if(typeObject == "radio" || typeObject == "checkbox")
			{
			presence = false;
			if(typeObject == "checkbox")
				{
				arrNomQuestion = nomObjet.split("_");
				nomObjet = arrNomQuestion[0];
				}
			for(j=0;j<n;j++)//parcours des noms de radio button deja testés
				{
				if(arrObjet[j][0] == nomObjet) 
					{
					presence = true;
					nb = arrObjet[j][1] + 1;
					arrObjet[j][1] = nb;
					break;
					}
				}
			if(presence == false)
				{
				arrObjet[n] = new Array(3);
				arrObjet[n][0] = nomObjet;
				arrObjet[n][1] = 1;
				arrObjet[n][2] = typeObject;
				n = n+1;
				}
			}
		}
	//etape 2 : tests des checked
	for(i=0;i<n;i++)
		{
		nb = arrObjet[i][1];
		test = false;
		for(k=0;k<nb;k++)
			{
			nomObjet = arrObjet[i][0]+"_"+(k+1);
			//alert("objet : "+nomObjet);
			test = document.getElementById(nomObjet).checked;
			if(test == true)
				{
				break;
				}
			}
		if(test == false) complete = false;
		}
	
	if(complete == false)
		{
		messageAlert = "\n - " + document.getElementById("error_diags_mandatory").innerHTML;
		}
	}

function controlSequence(champ1,champ2)
	{
	if(document.getElementById(champ1).value == "")
		{
		document.getElementById(champ1).value = document.getElementById(champ2).value;
		document.getElementById(champ2).value = "";
		}
	}
function changeSelect(champ1, idContainer, url)
	{
	var valeurSelect = document.getElementById(champ1).value;
	affichDetail(url+valeurSelect, idContainer);
	}
	
function controlAccept(champ, divError)
	{
	if(document.getElementById(champ).checked == false)
		{
		document.getElementById(divError).className = "";
		return false
		}
	else
		{
		document.getElementById(divError).className = "off";
		return true;
		}
	}
	
function clearValue () {
	var inputvalue = document.getElementsByTagName("input");
	for(var i=0; i<inputvalue.length; i++ )
	{
		var boucle = inputvalue[i];
		if(boucle.type == 'text' && boucle.defaultValue) {
			boucle.onfocus = function(){
				this.value='';
			};
			boucle.onblur = function(){
				if(/^\s*$/.test(this.value)) {this.value=this.defaultValue;}
			};
		}
	}
}

//addEvent(window, "load", clearValue );	
	
	
// ---------------------------------------------------------
// Phone number formatter, created for Foodry
// For more info, visit:
// http://www.foodry.com/blog
// control de champs Téléphone pour l'us uniquement
// ---------------------------------------------------------
function formatPhone(elm, e) {
	e = e || window.event || null;

    // If used in onkeypress, pass in the event and this will
    // grab the character and do the right thing. This allows
    // for a smoother user experience than if the chars are
    // being visibly deleted.
    if (e) {
        var keychar, keynum;
        if (e.keyCode) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }   

    // Allow a backspace + tab to go through, so the user
    // can correct any typos.
    if(keynum && (keynum == 9 || keynum == 8)) {
    	return true;
    } else {
        var p = elm.value + keychar;

        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
        p = p.replace(/^[01]/,"");
        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length > 0 && p.length < 3) {
            p = "("+p;
        }
        else if (p.length >= 3 && p.length < 7) {
            p = "("+p.substring(0,3)+") "+p.substring(3);
        }
        else if (p.length >= 7 && p.length < 10) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
        }
        else if (p.length) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
        }
        elm.value = p;

        return false;
    }
}