function writediv(element, texte)
{
	document.getElementById(element).innerHTML = texte;
}

function file(fichier)
{
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	
	xhr_object.open("GET", fichier, false);
	try{
		xhr_object.send(null);
	}catch(e){}
	if(xhr_object.readyState == 4) 
		return(xhr_object.responseText);
	else 
		return(false);
}

function verifMail(mail,type)
{
	if( type == "societe" ){
		couleur = "white";
	}
	else{
		couleur = "red";
	}
	var valid = 0;

	if(mail != "")
	{
		if(validMail(mail) != true)
		{
			writediv("mailbox", "<span style='color:red'><b>"+mail+" :</b> ce courriel n'est pas valide</span>");
		}
		else
		{
			writediv("mailbox", "<span style='color:#1A7917'><b>"+mail+" :</b> ce courriel est valid</span>");
				valid = 1;
		}
	}
	else
	{
		writediv("mailbox", "<span style='color:red'>Veuillez entrer une adresse courrielle</span>");
	}
	
	return valid;
}

function validMail(mail)
{
	EmailValid = 0;
	EmailAt = 0;
	EmailPeriod = 0;
	EmailControlEspace = 0;
	EmailControlSlash = 0;
	EmailControlDiese = 0;
	EmailControlAntiSlash = 0;
	var valid = 0;
	
	for (i = 0;  i < mail.length;  i++)
	{
		ch = mail.charAt(i);
		if ( ch == "@"){ EmailAt += parseInt(1); }
		if ( ch == "."){ EmailPeriod += parseInt(1); }
	}
	
	if( EmailAt == 1 &&  EmailPeriod >= 1 ){
		for (i = 0;  i < mail.length;  i++)
		{
			ch = mail.charAt(i);
			if ( ch == " "){ EmailControlEspace += parseInt(1); }
			if ( ch == "/"){ EmailControlSlash += parseInt(1);}
			if ( ch == "#"){ EmailControlDiese += parseInt(1);}
			if ( ch == "\\"){ EmailControlAntiSlash += parseInt(1); }
		}
		
		if( EmailControlEspace + EmailControlSlash + EmailControlDiese + EmailControlAntiSlash == 0 ){
			var valid = 0;
		}
		else{
			valid = 1;
		}
	}
	else{
		valid = 1;
	}
	
	if(valid == 0 ){
		EmailValid = true;
	}
	else{
		EmailValid = false;
	}
	
	return EmailValid;
}

function verifCaptcha(hncaptcha,hncaptcha_public_key,captcha,textefield) {
	
	var valid = 0; 
	texte = 0;

	 //alert("include/ajax/valid_captcha.php?hncaptcha=" + escape(hncaptcha) + "&hncaptcha_public_key=" + escape(hncaptcha_public_key) + "&hncaptcha_private_key=" + escape(captcha) + "&type=" + escape(box));
	 if( !textefield.disabled ){
		 if (captcha.length != 5)
		  {
			writediv("captchabox", " <span style='color:red'>Le captcha doit faire 5 caract&egrave;res</span>");
			
		  }
		  else if(texte = file("include/ajax/valid_captcha.php?hncaptcha=" + escape(hncaptcha) + "&hncaptcha_public_key=" + escape(hncaptcha_public_key) + "&hncaptcha_private_key=" + escape(captcha)))
			{
				//alert("include/ajax/valid_captcha.php?hncaptcha_private_key=" + escape(captcha) + "&type=" + escape(box));
				if(texte == 1)
				{
				writediv("captchabox", "<span style='color:green'> OK</span>");
				textefield.disabled = true;
				valid = 1;
				}
				else if(texte == 2)
				{
				essai = file("include/ajax/valid_captcha.php?essai=ask");
				reste = 5 - essai;
				writediv("captchabox", "<span style='color:red'> non valide, il vous reste " + reste + " tentatives</span>");
				
				var valid = 0;
				}
				else if(texte == 3)
				{
				writediv("captchabox", "<span style='color:red'> Nombre d'essai &eacute;puis&eacute;</span>");
				var valid = 0;
				}
				else
				{
				writediv("captchabox", texte);
				var valid = 0;
				}
		  }
	 }
	 else{
		 valid = 1;
	 }
	
	 return valid;
}

function verifNom(nom){
	var valid = 0;
	
	if( nom == ''){
		writediv("nombox", "<span style='color:red'>Veuillez entrer un nom</span>");
		var valid = 0;
	}
	else{
		writediv("nombox", "<span style='color:green'>OK</span>");
		valid = 1;
	}
	
	return valid;
}

function verifTel(tel){
	var valid = 0;
	
	if( tel == ''){
		writediv("telbox", "<span style='color:red'>Veuillez entrer un num&eacute;ro de t&eacute;l&eacute;phone</span>");
		var valid = 0;
	}
	else{
		writediv("telbox", "<span style='color:green'>OK</span>");
		valid = 1;
	}
	
	return valid;
}

function verifPrenom(prenom){
	var valid = 0;
	
	if( prenom == ''){
		writediv("prenombox", "<span style='color:red'>Veuillez entrer un prénom</span>");
		var valid = 0;
	}
	else{
		writediv("prenombox", "<span style='color:green'>OK</span>");
		valid = 1;
	}
	
	return valid;
}	

function verifForm(){
	var valid = 0;
	
	nom = verifNom(document.getElementById('nom').value);
	prenom = verifPrenom(document.getElementById('prenom').value);
	tel = verifTel(document.getElementById('telephone').value);
	
	email = verifMail(document.getElementById('courriel').value);
	captcha = verifCaptcha(document.getElementsByName('hncaptcha').value,document.getElementsByName('hncaptcha_public_key').value,document.getElementById('hncaptcha_private_key').value,document.getElementById('hncaptcha_private_key'));
	
	valid = nom + prenom + email + captcha + tel;
	
	if (valid == 5)
	{
		document.getElementById('submit').disabled = false;
	}
	else
	{
		document.getElementById('submit').disabled = true;
	}

}
