function initSondage()
{
	if (document.getElementById('isens_sondage'))
	{
		var isens_sondage = document.getElementById('isens_sondage');
		isens_sondage.onsubmit = function ()
		{
			return canSubmit(this);
		}
	}
}

var tabElem;
function canSubmit(form)
{
	var ok = 1;
	var listInput = form.getElementsByTagName('INPUT');
	tabElem = new Array();
	
	for(var no = 0;no < listInput.length;no++)
	{
		if (listInput[no].getAttribute("required") == "email")
		{
			if (listInput[no].type.toUpperCase() == "TEXT")
			{
				var oktmp = 0;
				
				if (listInput[no].value == "") oktmp = 1;
				else if (!controleReg(listInput[no],'^[a-zA-Z0-9._-]+@[A-Za-z0-9-]{2,}([.][A-Za-z0-9-]{2,}){1,3}$')) oktmp = 1;
				
				if (oktmp)
				{
					listInput[no].style.borderColor = couleur_erreur_sondage;
					listInput[no].onkeydown = function(){ this.style.borderColor = ""; }
					ok = 0;
				}
			}
			
			
		}
		else if (listInput[no].getAttribute("required") == "text")
		{
			if ((listInput[no].type.toUpperCase() == "TEXT") && (listInput[no].value == ""))
			{
				var oktmp = 1;
				
				// Cas des champs de détails pour une option "Autres, précisez..."
				var tmp = listInput[no].name;
				if (tmp.indexOf("text_") > -1)
				{
					var tmp1 = tmp.replace("text_","");
					if (!document.getElementById(tmp1).checked)
					{
						oktmp = 0;
						listInput[no].style.borderColor = "";
					}
				}
				
				if (oktmp)
				{
					listInput[no].style.borderColor = couleur_erreur_sondage;
					listInput[no].onkeydown = function(){ this.style.borderColor = ""; }
					ok = 0;
				}
			}
			
			if ((listInput[no].type.toUpperCase() == "CHECKBOX") || (listInput[no].type.toUpperCase() == "RADIO"))
			{
				// On récupère le "3" du nom "question_3_1"
				var tmp1 = listInput[no].getAttribute("id");
				var tmp2 = tmp1.split("_");
				
				listInput[no].onclick = function()
				{
					// Cas du checkbox déselectionné, on enlève le rouge du champ (optionnel) complémentaire
					if (this.type.toUpperCase() == "CHECKBOX")
					{
						if (!this.checked)
						{
							if (document.getElementById("text_" + this.name))
								document.getElementById("text_" + this.name).style.borderColor = "";
						}
					}
					
					// Cas du radio, on passe tous les boutons radios pour voir s'il n'y a pas de champ texte en rouge
					if (this.type.toUpperCase() == "RADIO")
					{
						var listInput = form.getElementsByTagName('INPUT');
						for(var no = 0;no < listInput.length;no++)
						{
							if ((listInput[no].getAttribute("required") == "text") && (listInput[no].type.toUpperCase() == "RADIO"))
							{
								var tmp1 = listInput[no].getAttribute("id");
								var tmp2 = tmp1.split("_");
								var tmp3 = "question_" + tmp2[1] + "_";
								var tmp4 = this.name + "_";
								
								if (tmp1.indexOf(tmp4) > -1)
								{
									if (document.getElementById("text_" + tmp1))
									{
										document.getElementById("text_" + tmp1).style.borderColor = "";
									}
								}
							}
						}
					}
				};
				
				if (listInput[no].checked) tabElem[tmp2[1]] = "0";
				
				if (typeof(tabElem[tmp2[1]]) == "undefined") tabElem[tmp2[1]] = "";
				
				if (tabElem[tmp2[1]] != "0") tabElem[tmp2[1]] += listInput[no].getAttribute("id") + "#";
			}
		}
	}
	
	for(var i = 0;i < tabElem.length;i++)
	{
		if ((typeof(tabElem[i]) != "undefined") && (tabElem[i] != "0"))
		{
			var tmp = tabElem[i];
			var tmpTab = tmp.split("#");
			for(j = 0;j < tmpTab.length;j++)
			{
				if (tmpTab[j] != "")
				{
					if (document.getElementById("label_" + tmpTab[j]))
						document.getElementById("label_" + tmpTab[j]).style.color = couleur_erreur_sondage;
					else
					{
						// Cas des matrices
						var idcourant = tmpTab[j];
						var tmp1 = idcourant.split("_");
						idcourant = tmp1[0] + "_" + tmp1[1] + "_" + tmp1[2];
						
						document.getElementById("label_" + idcourant).style.color = couleur_erreur_sondage;
					}
					
					document.getElementById(tmpTab[j]).onclick = function()
					{
						var tmp1 = this.getAttribute("id");
						var tmp2 = tmp1.split("_");
						var tmp3 = tabElem[tmp2[1]];
						
						var tmpTab = tmp3.split("#");
						for(j = 0;j < tmpTab.length;j++)
						{
							if (tmpTab[j] != "")
							{
								if (document.getElementById("label_" + tmpTab[j]))
									document.getElementById("label_" + tmpTab[j]).style.color = "";
								else
								{
									// Cas des matrices
									var idcourant = tmpTab[j];
									var tmp1 = idcourant.split("_");
									idcourant = tmp1[0] + "_" + tmp1[1] + "_" + tmp1[2];
									
									document.getElementById("label_" + idcourant).style.color = "";
								}
								
								if (document.getElementById("text_" + tmpTab[j]))
									document.getElementById("text_" + tmpTab[j]).style.borderColor = "";
							}
						}
					}
					
				}
			}
			ok = 0;
		}
	}
	
	var listSelect = form.getElementsByTagName('SELECT');
	for(var no = 0;no < listSelect.length;no++)
	{
		if (listSelect[no].getAttribute("required") == "text")
		{
			var ko = 0;
			if ((listSelect[no].getAttribute("multiple") == true) && (listSelect[no].selectedIndex == -1)) ko = 1;
			if ((listSelect[no].getAttribute("multiple") != true) && (listSelect[no].selectedIndex == 0)) ko = 1;
			
			if (ko == 1)
			{
				listSelect[no].style.backgroundColor = couleur_erreur_sondage;
				listSelect[no].onchange = function(){ this.style.backgroundColor = ""; }
				ok = 0;
			}
		}
	}
	
	var listTextarea = form.getElementsByTagName('TEXTAREA');
	for(var no = 0;no < listTextarea.length;no++)
	{
		if ((listTextarea[no].getAttribute("required") == "text") && (listTextarea[no].value == ""))
		{
			listTextarea[no].style.borderColor = couleur_erreur_sondage;
			listTextarea[no].onkeydown = function(){ this.style.borderColor = ""; }
			ok = 0;
		}
	}
		
	if (ok) return true;
	else return false;
}

function selectOption(obj)
{
	var tmp1 = obj.name;
	var tmp2 = tmp1.replace("text_","");
	
	document.getElementById(tmp2).checked = true;
}

function controleInteger(control)
{
	var intval = parseInt(control.value, 10);
	if (isNaN(intval)) control.value="";
	else control.value = intval;
}

function controleHeure(control)
{
	var v = control.value;
	var v1 = parseInt(control.value);
	control.value = '';
	var a = v.indexOf(':');
	if (a > 0)
	{
		var h = parseInt(v.substr(0,a), 10);
		var m = parseInt(v.substring(a+1), 10);
		
		if (m > 59) m = 59;
		if (m < 0) m = 0;
		
		if (!isNaN(h) && !isNaN(m) && h >= 0 && h < 24)
		{
			if (m.toString().length>1)
			{
				control.value = h+':'+m;
			}
			else
			{
				control.value = h+':0'+m;
			}
			if (control.value.length == 4) control.value = '0' + control.value;
		}
	}
	else if (!isNaN(v1) && v1 >= 0 && v1 < 24)
	{
		control.value = v1+':00';
		if (control.value.length == 4) control.value = '0' + control.value;
	}
}

function controleReg(control,expr)
{
	var regControl = new RegExp(expr,"");
	if (!regControl.exec(control.value))
	{
		if (control.value != "") control.style.borderColor = couleur_erreur_sondage;
		else control.style.borderColor = "";
			
		return false;
	}
	else
	{
		control.style.borderColor = "";
		return true;
	}
}

function controleDate(control)
{
	var v = control.value;
	v = v.replace(/-/g,"/");
	v = v.replace(/ /g,"/");
	
	control.value = '';
	var a = v.indexOf('/');
	var b = v.lastIndexOf('/');
	if (a != -1 && b > a+1 && b+1 < v.length)
	{
		var d = parseInt(v.substr(0,a), 10);
		var m = parseInt(v.substring(a+1,b), 10);
		var y = parseInt(v.substr(b+1), 10);
		if (!isNaN(y) && y < 100) y = 2000+y;
		if (!isNaN(d) && !isNaN(m) && !isNaN(y))
		{
			var p = new Date(y,m-1,d);
			
			var jour = p.getDate();
			if (jour < 10) jour = "0" + jour;
			
			var mois = p.getMonth()+1;
			if (mois < 10) mois = "0"+mois;
			
			control.value = jour + '/' + mois + '/' + p.getFullYear();
		}
	}
}

function controleCodePostal(control)
{
	var temp = control.value;
	temp = temp.replace(/ /gi,"");
	temp = temp.replace(/([^0-9]*)/gi,"");
	temp = temp.substring(0,5);
	control.value = temp;
}

function controleTelephone(control)
{
	var temp = control.value;
	temp = temp.replace(/ /gi,"");
	temp = temp.replace(/([^0-9]*)/gi,"");
	temp = temp.substring(0,10);
	control.value = formatage(temp,2," ");
}

function formatage(chaine,nb,separateur)
{
	var resultat = "";
	if (chaine.length == 10)
	{
		var tableau = new Array();
	
		for (i = 0; i < chaine.length;i += nb)
		{
			resultat += chaine.substr(i,nb) + separateur;
		}
	
		resultat = resultat.substring(0,(resultat.length-1));
		return (resultat);
	}
	else return (chaine);
}

if (window.addEventListener) window.addEventListener("load", initSondage,false);
else window.attachEvent("onload", initSondage);
