// JavaScript Document
document.write('<script src="/static/js/formulaire/liste_deroulante.js" language="javascript"></script>');

var code=false;
var mail_ok=false;


/******************************************************/
// On vérifie la saisie du formulaire
/******************************************************/
function ckeck_mail()
{
	var str= window.document.inscription.email.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
	{
		verif_mail(document.inscription.email.value,0,"inscription");
	}
	else
	{
		document.confirm_mail.src=document.confirm_mail.src.replace('true.png','false.png');
		return false;
	}
}




function check_input(){
	var erreur=false;

	for(var i = 0; i < window.document.inscription.length ; i++) 
	{ 
		switch(window.document.inscription.elements[i].name)
		{
			case "nom":
					
					if(window.document.inscription.elements[i].value.length>=1)
					{
						window.document.confirm_nom.src=window.document.confirm_nom.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_nom.src=window.document.confirm_nom.src.replace("true.png","false.png");
						erreur=true;
					}
					
					window.document.inscription.elements[i].value=nompropre(window.document.inscription.elements[i].value);
			break;
			
			case "prenom":
					if(window.document.inscription.elements[i].value.length>=1)
					{
						window.document.confirm_prenom.src=window.document.confirm_prenom.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_prenom.src=window.document.confirm_prenom.src.replace("true.png","false.png");
						erreur=true;
					}
					window.document.inscription.elements[i].value=nompropre(window.document.inscription.elements[i].value);					
			break;
			
			case "adresse":
					if(window.document.inscription.elements[i].value.length>=1)
					{
						window.document.confirm_adresse.src=window.document.confirm_adresse.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_adresse.src=window.document.confirm_adresse.src.replace("true.png","false.png");
						erreur=true;
					}
			break;
			
			
			case "cp":
					if(window.document.inscription.elements[i].value.length>=2)
					{
						window.document.confirm_cp.src=window.document.confirm_cp.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_cp.src=window.document.confirm_cp.src.replace("true.png","false.png");
						erreur=true;
					}
			break;
					
			case "ville":
					if(window.document.inscription.elements[i].value.length>=1)
					{
						window.document.confirm_ville.src=window.document.confirm_ville.src.replace("false.png","true.png");
						
					}
					else
					{
						window.document.confirm_ville.src=window.document.confirm_ville.src.replace("true.png","false.png");
						erreur=true;
					}
			break;	
			
			case "pass":
					if(window.document.inscription.elements[i].value.length>=6)
					{
						window.document.confirm_pass.src=window.document.confirm_pass.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_pass.src=window.document.confirm_pass.src.replace("true.png","false.png");
						erreur=true;
					}
			break;
			
			case "confpass":
					if(window.document.inscription.pass.value.length==window.document.inscription.confpass.value.length && window.document.inscription.pass.value==window.document.inscription.confpass.value && window.document.inscription.confpass.value.length>=6)
					{
						window.document.confirm_confpass.src=window.document.confirm_confpass.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_confpass.src=window.document.confirm_confpass.src.replace("true.png","false.png");
						erreur=true;
					}
			break;
			
			case "confemail":
					if(window.document.inscription.email.value.length==window.document.inscription.confemail.value.length && window.document.inscription.email.value==window.document.inscription.confemail.value && window.document.inscription.confemail.value.length!=0)
					{
						window.document.confirm_confmail.src=window.document.confirm_confmail.src.replace("false.png","true.png");
					}
					else
					{
						window.document.confirm_confmail.src=window.document.confirm_confmail.src.replace("true.png","false.png");
						erreur=true;
					}
			break;
			
		}
		
	}	
	return erreur;
	
} 


/*******************************************************/
// Vérification de l'identification
/*******************************************************/
function acc_identification()
{
	var xhr_object = null; 
   if(window.XMLHttpRequest)
   {
	   // Firefox 
	  xhr_object = new XMLHttpRequest(); 
   }
   else if(window.ActiveXObject)
   {
	   // Internet Explorer 
	  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
   }
   else 
   { 
   	  // XMLHttpRequest non supporté par le navigateur 
	  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	  return; 
   } 
 
   xhr_object.open("POST", "/ajax/identification.php", true); 
	 
   xhr_object.onreadystatechange = function()
   { 
	  if(xhr_object.readyState == 4) 
		 eval(xhr_object.responseText); 
   } 
 
   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
   
   var data = "mail="+document.identification.mail.value+"&pass="+document.identification.pass.value;

   xhr_object.send(data); 
	
}

/****************************************************/
// On formate le mot avec la premiere lettre en maj
/****************************************************/
function nompropre(mot) {
      var m=mot.charAt(0).toUpperCase() +
       mot.substring(1).toLowerCase();
      return m;
}

/**********************************************************/
//Vérification de l'adresse mail dans la base de données
//si exist=1, on vérifie que le mail existe et si exist=0 on vérifie que le mail n'existe pas
/**********************************************************/
function verif_mail(mail,exist,action)
{
	mail_ok=false;
	var xhr_object = null; 
	 
   if(window.XMLHttpRequest)
   {
	   // Firefox 
	  xhr_object = new XMLHttpRequest(); 
   }
   else if(window.ActiveXObject)
   {
	   // Internet Explorer 
	  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
   }
   else 
   { 
   	  // XMLHttpRequest non supporté par le navigateur 
	  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	  return; 
   } 
 
   xhr_object.open("POST", "/ajax/verif_mail.php", true); 
	 
   xhr_object.onreadystatechange = function()
   { 
	  if(xhr_object.readyState == 4)
		 eval(xhr_object.responseText);
		 if(mail==1)
			{
				document.getElementById('verif_mail').style.visibility='hidden';
				document.confirm_mail.src=document.confirm_mail.src.replace('false.png','true.png');
				mail_ok=true;
				if (action == "nouveau_mdp")
				{	document.motdepasse.submit();	}
	
			}
			else
			{			
				document.getElementById('verif_mail').style.visibility='visible';
				document.confirm_mail.src=document.confirm_mail.src.replace('true.png','false.png');
				mail_ok=false;
			}
   } 
 
   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

   var data = "mail="+mail+"&exist="+exist;
   xhr_object.send(data); 	
	
}


/**********************************************************/
//Vérification de la bonne saisie du cryptogramme
/**********************************************************/
function verif_code(nom_form)
{
		var xhr_object = null; 
		 
	   if(window.XMLHttpRequest)
	   {
		   // Firefox 
		  xhr_object = new XMLHttpRequest(); 
	   }
	   else if(window.ActiveXObject)
	   {
		   // Internet Explorer 
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	   }
	   else 
	   { 
		  // XMLHttpRequest non supporté par le navigateur 
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		  return; 
	   } 
	 
	   xhr_object.open("POST", "/ajax/verif_code.php", true); 
		 
	   xhr_object.onreadystatechange = function()
	   { 
		  if(xhr_object.readyState == 4) 
		  {
			 eval(xhr_object.responseText);
		  }
	   } 
	 
	   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	
	   var	code = eval ("document."+nom_form+".code.value");
	
	   var data = "code="+code+"&nom_form="+nom_form;
	   
	   xhr_object.send(data); 	

}


/**************************************************/
// Vérification de l'identification client
/**************************************************/
function acc_identification_form()
{
	if(document.identification_form.mail.value =="" || document.identification_form.pass.value=="")
	{
		alert("Merci de bien vouloir renseigner les champs correctements");
	}
	else
	{	
		var xhr_object = null; 
	   if(window.XMLHttpRequest)
	   {
		   // Firefox 
		  xhr_object = new XMLHttpRequest(); 
	   }
	   else if(window.ActiveXObject)
	   {
		   // Internet Explorer 
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	   }
	   else 
	   { 
		  // XMLHttpRequest non supporté par le navigateur 
		  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		  return; 
	   } 
	 
	   xhr_object.open("POST", "/ajax/identification_form.php", true); 
		 
	   xhr_object.onreadystatechange = function()
	   { 
		  if(xhr_object.readyState == 4) 
			 eval(xhr_object.responseText); 
	   } 
	 
	   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	   
	   var data = "mail="+document.identification_form.mail.value+"&pass="+document.identification_form.pass.value;
	
	   xhr_object.send(data); 
	}
}


/*************************************************************************/
// Vérification de l'adresse e-mail pour envoi d'un nouveau mot de passe
/*************************************************************************/
function check_email_mdp ()
{
	var mail= window.document.motdepasse.email.value;
	var testm=false;
	
	for (var j=1 ; j<(mail.length) ; j++) 
	{
		if (mail.charAt(j)=='@') 
		{
			if (j<(mail.length-4))
			{
				for (var k=j ; k<(mail.length-2) ; k++) 
				{
					if (mail.charAt(k)=='.') testm=true; 
				}
			}
		}
	}
	if(testm)
	{
		verif_mail(mail,1,"nouveau_mdp");
	}
	else
	{
		alert ("Votre adresse email est incorrect");
		document.confirm_mail.src=document.confirm_mail.src.replace('true.png','false.png');
	}		
}

/*************************************************************************/
// Vérification de l'adresse e-mail et de l'identite pour envoi à un ami
/*************************************************************************/
function check_ami()
{
	var mail= window.document.envoi_ami.email.value;
	var identite= window.document.envoi_ami.identite.value;
	var testm=false;
	var testi=false;
	
	for (var j=1 ; j<(mail.length) ; j++) 
	{
		if (mail.charAt(j)=='@') 
		{
			if (j<(mail.length-4))
			{
				for (var k=j ; k<(mail.length-2) ; k++) 
				{
					if (mail.charAt(k)=='.')
					{
						testm=true; 
						document.confirm_mail.src=document.confirm_mail.src.replace('false.png','true.png');
					}
				}
			}
		}
	}
	if(identite!="")
	{
		testi=true;
	}
	else
	{
		testi=false;
		alert ("Votre identité doit être renseignée.");
	}
	if(!testm)
	{
		document.confirm_mail.src=document.confirm_mail.src.replace('true.png','false.png');
		alert("L'adresse mail est incorrecte.");
	}
	if(testm && testi)
	{	
		verif_code("envoi_ami");
	}
	else
	{
		return false;
	}		
}

function inscription_charte()
{
	if(document.inscription.charte_confiance.checked)
	{
		verif_code('inscription');
	}
	else
	{
		alert("Veuillez accepter la charte de confiance et de confidentialité ainsi que les conditions générales d'utilisation.");
	}
	
}
