var nb_language = 1;

function validForm_modify(email)
{
	var arr = {};
	var errorObj;

	arr[0] = verifyRequiredSelect();
	arr[1] = verifyRequiredforms();
	

	cleanInput(document.form1.jour);
	cleanInput(document.form1.mois);
	cleanInput(document.form1.an);
	errorObj = document.getElementById('birthDate_error');
	hideObj(errorObj);
		
	if (document.form1.jour.value == "0" || document.form1.mois.value  == "0"|| document.form1.an.value == "0")
	{
		colorInput(document.form1.jour);
		colorInput(document.form1.mois);
		colorInput(document.form1.an);
		errorObj = document.getElementById('birthDate_error');
		showObj(errorObj);
		arr[2] = false;
	}
	
	cleanInput(document.form1.adresse);
	errorObj = document.getElementById('adresse_error');
	hideObj(errorObj);
		
	if (trim(document.form1.adresse.value) == "")
	{
		colorInput(document.form1.adresse);
		errorObj = document.getElementById('adresse_error');
		showObj(errorObj);
		arr[3] = false;
	}
	
	cleanInput(document.form1.password);
	cleanInput(document.form1.password_conf);
	if (verifyPassword() == false)
	{
		colorInput(document.form1.password);
		colorInput(document.form1.password_conf);
		arr[4] = false;
	}	

	var t = document.getElementsByTagName("select");
	var n=0;
	var cmp = 0;
	var tab = new Array();
	for(k=0;k<t.length;k++)
		if(t[k].className == "tester")
			tab.push(t[k]);
	
	//alert(tab.length);
	for(k=0;k<tab.length;k=k+2)
	{
		if(tab[k].value != 0 && tab[k+1].value == 0)
		{
			alert("Vous devez choisir le niveau de langue");
			arr[5] = false;
		}
	
	
		if (tab[k].value == 0 && tab[k+1].value != 0)
		{
			alert("Vous devez choisir la langue");
			arr[6] = false;
		}
	}
	
	for(k=0;k<tab.length;k=k+2)
		for(i=0;i<k && i!=k;i=i+2)
			if(tab[k].value == tab[i].value)
			{
				alert("Vous ne pouvez pas choisir la même langue deux fois");
				arr[7] = false;
			}
			
	cleanInput(document.form1.email); 
	cleanInput(document.form1.cemail);
	if (verifyEmail(document.form1.email.value,email,"inscr") == false)
	{
		colorInput(document.form1.email);
		arr[9] = false;
	}
	
	if (document.form1.email.value != document.form1.cemail.value)
	{
		colorInput(document.form1.email);
		colorInput(document.form1.cemail);
		errorObj = document.getElementById('cemail_error');
		showObj(errorObj);
		arr[10] = false;
	}
		
	
	for (var i = 0; i <= 10; i++)
		if (arr[i] == false)
			return;
			
	document.form1.submit();
}



function validForm(email)
{
	var arr = {};
	var errorObj;
	// cleanner tous les champs.
	arr[0] = verifyRequiredSelect();
	arr[1] = verifyRequiredforms();
	
	cleanInput(document.form1.jour);
	cleanInput(document.form1.mois);
	cleanInput(document.form1.an);
	errorObj = document.getElementById('birthDate_error');
	hideObj(errorObj);
	if (document.form1.jour.value == "0" || document.form1.mois.value  == "0"|| document.form1.an.value == "0")
	{
		colorInput(document.form1.jour);
		colorInput(document.form1.mois);
		colorInput(document.form1.an);
		errorObj = document.getElementById('birthDate_error');
		showObj(errorObj);
		arr[2] = false;
	}
	if(document.form1.civility.value=="")
	{
	colorInput(document.form1.civility);
	}
	cleanInput(document.form1.adresse);
	errorObj = document.getElementById('adresse_error');
	hideObj(errorObj);
		
	if (trim(document.form1.adresse.value) == "")
	{
		colorInput(document.form1.adresse);
		errorObj = document.getElementById('adresse_error');
		showObj(errorObj);
		arr[3] = false;
	}
	
	cleanInput(document.form1.email);
	if (verifyEmail(document.form1.email.value,email,"inscr") == false)
	{
		colorInput(document.form1.email);
		colorInput(document.form1.conf_email);
		arr[4] = false;
	}
	
	cleanInput(document.form1.password);
	cleanInput(document.form1.password_conf);
	if (verifyPassword() == false)
	{
		colorInput(document.form1.password);
		colorInput(document.form1.password_conf);
		arr[5] = false;
	}
	cleanInput(document.form1.conf_email);
	if (document.form1.email.value != document.form1.conf_email.value)
	{
		colorInput(document.form1.conf_email);
		errorObj = document.getElementById('conf_email_error');
		showObj(errorObj);
		arr[6] = false;
	}
	
	var t = document.getElementsByTagName("select");
	var n=0;
	var cmp = 0;
	var tab = new Array();
	for(k=0;k<t.length;k++)
		if(t[k].className == "tester")
			tab.push(t[k]);
	
	for(k=0;k<tab.length;k=k+2)
	{
		if(tab[k].value != 0 && tab[k+1].value == 0)
		{
			alert("Vous devez choisir le niveau de langue");
			arr[7] = false;
		}
	
	
		if (tab[k].value == 0 && tab[k+1].value != 0)
		{
			alert("Vous devez choisir la langue");
			arr[8] = false;
		}
	}
	
	for(k=0;k<tab.length;k=k+2)
		for(i=0;i<k && i!=k;i=i+2)
			if(tab[k].value == tab[i].value)
			{
				alert("Vous ne pouvez pas choisir la même langue deux fois");
				arr[9] = false;
			}
	
	for (var i = 0; i <= 8; i++)
		if (arr[i] == false)
			return;
	document.form1.submit();
}


function emailwritediv(texte, error, type)
 {
	 var res = " ";
	 if(type == "newsletter")
	 {
		 if (error == false)
		 	res = "<div class='validate1'>";
		 else
		 	res = "<div class='error1'>";
	 }
	 if(type == "inscr")
	 {
		 if (error == false)
		 	res = "<div class='validate'>";
		 else
		 	res = "<div class='error'>";
	 }
	 document.getElementById('email_error').innerHTML = res + texte + "</div>";
 }
 
 function verifyEmail(value,email,type)
 {
	if(value != email)
	{
		var exp_reg = /[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+/;
		if (value.length == 0)
		{
			emailwritediv("Veuillez remplir ce champ.", true, type);
			//colorInput(document.form1.email);
			return false;
		}
		if (value.length < 3)
		{
			emailwritediv("Votre email est trop court", true, type);
		 	return false;
		}
		if (exp_reg.test(value) == false)
		{
 			emailwritediv("Votre email est invalide", true, type);			
			return false;
		}
	
		var path = null;
		if(type == "newsletter")
			path = "script/verify_newsletter.php?nocache=" + Math.random() + "&email="+escape(value);
		if(type == "inscr")
			path = "script/verify_email.php?email="+escape(value);
		
		if(texte = file(path))
	 	{
		 	if (texte == "1")
		 	{
			 	emailwritediv("Cet email existe déjà.", true, type);
			 	return false;
			}
		 	else
			 	emailwritediv("Cet email est valide.", false, type);
	 	}	
	}
	 return true;
 }
 
 function verifyPassword()
 {
	 var error1 = "";
	 var error2 = "";
	 var flag = true;
	 if (document.form1.password.value.length < 6)
	 {
	 	error1 = "Veuillez entrer un mot de passe de plus de 6 charactères";
		flag = false;
	 }
	if (document.form1.password_conf.value != document.form1.password.value)
	{
		error2 = "La confirmation ne correspond pas au mot de passe.";
		flag = false;
	}
	document.getElementById('password_error').innerHTML = error1;
	document.getElementById('password_conf_error').innerHTML = error2;
	return flag;
 }
 
 function verifyConfEmail()
 {
	 var error1 = "";
	 var flag = true;
	 
	if (document.form1.conf_email.value != document.form1.email.value)
	{
		error1= "La confirmation ne correspond pas à l'email";
		flag = false;
	}
	
	document.getElementById('conf_email_error').innerHTML = error1;
	return flag;
 }
 
 function deleteLanguageBlock(id)
 {
	obj =	document.getElementById('language_' + id);
	obj.parentNode.removeChild(obj);
	cptLangue--;
 }
 
 function addLanguageBlock()
 {
	 if (cptLangue < 5)
	 {
		 var elem = document.getElementById('language_template').cloneNode(true);
		elem.getElementsByTagName("a")[0].firstChild.nodeValue = "[supprimer]";
		
		elem.getElementsByTagName("a")[0].setAttribute("onclick",'deleteLanguageBlock(' + cptLangue + ');');
		var htmlCode = elem.innerHTML;
		document.getElementById('other_language').innerHTML += "<div id='language_" + cptLangue + "' class='smargint'>" + htmlCode + "</div>";
//		nb_language++;
		cptLangue++;
	 }
	 else
	 	alert("5 langues maximum.");
 }
 
 function validateNewsletter()
 {
	 if (verifyEmail(document.newsletterForm.email.value,"newsletter") == false)
	{
		
		colorInput(document.newsletterForm.email);
		return;
	}
	document.newsletterForm.submit();
 }
 
function validFormCompany()
{
	var arr = {};
	var errorObj;
	// cleanner tous les champs.
	arr[0] = verifyRequiredSelect();
	arr[1] = verifyRequiredforms();

	
	if (verifyEmail(document.form1.email.value,'###',"inscr") == false)
	{
		colorInput(document.form1.email);
		arr[2] = false;
	}
	
	cleanInput(document.form1.adresse);
	errorObj = document.getElementById('adresse_error');
	hideObj(errorObj);
	if (trim(document.form1.adresse.value) == "")
	{
		colorInput(document.form1.adresse);
		errorObj = document.getElementById('adresse_error');
		showObj(errorObj);
		arr[3] = false;
	}
	
	cleanInput(document.form1.email);
	if (verifyEmail(document.form1.email.value,"","inscr") == false)
	{
		colorInput(document.form1.email);
		//colorInput(document.form1.conf_email);
		arr[4] = false;
	}
	
	for (var i = 0; i < 5; i++)
		if (arr[i] == false)
		{
			document.getElementById("baspage").style.top="760px";
			return;
		}
	document.form1.submit();	
}

function validFormCompany_modify()
{
	var arr = {};
	var errorObj;
	// cleanner tous les champs.
	arr[0] = verifyRequiredSelect();
	arr[1] = verifyRequiredforms();

	
	cleanInput(document.form1.adresse);
	errorObj = document.getElementById('adresse_error');
	hideObj(errorObj);
	if (trim(document.form1.adresse.value) == "")
	{
		colorInput(document.form1.adresse);
		errorObj = document.getElementById('adresse_error');
		showObj(errorObj);
		arr[2] = false;
	}
	if (verifyEmail(document.form1.email.value,document.form1.email.value,"inscr") == false)
	{
		colorInput(document.form1.email);
		arr[3] = false;
	}
		
	for (var i = 0; i <= 3; i++)
		if (arr[i] == false)
		{
			return;
		}
	document.form1.submit();	
}

function newsletterManage(email)
{
	if(document.getElementById('inscr').checked)
	{
		if(verifyEmail(email,'','newsletter') == true)
			document.form1.submit();
	}
	
	if(document.getElementById('desinscr').checked)
	{
		if(verifyEmail(email,'','newsletter') == false)
			document.form1.submit();
		else
			emailwritediv("Cet adresse mail n'existe pas.", true, "newsletter");
	}
}
	 
	 
