// JavaScript Document
onload = function (){
	
	// Mise en place des focntions 
	
	// Test de validation du formulaire
	var form = window.document.getElementById('form_performanse');
	if (form) {
		form.onsubmit = function () {
			return verificationFormulaire(this);
		}
	}
	
	// Affichage ou masquage du bloc adresse
	var cocheAdresse = window.document.getElementById('doc_courrier');
	cocheAdresse.onclick = function () {

		afficheAdresse(this);
	}

	
	// Vérification de la coche adresse lors du chargement du formulaire
	var blocAdresse = window.document.getElementById("bloc_adresse");
	if(cocheAdresse.checked == true) {
		blocAdresse.style.display = "block";
	} else {
		blocAdresse.style.display = "none";
	}
	
	// fonction de vérification de l'adresse email
	var champsEmail = window.document.getElementById('email');
	if(champsEmail) {
		champsEmail.onchange = function() {
			emailVerification(this);
		}
	}
	var formulaire = window.document.getElementById('form_contact');
	if(formulaire) {
		formulaire.onsubmit = function() {
			return formulaireVerification(this);
		}
	}
	

}

function afficheAdresse(objet) {
	if(objet.checked == true) {
		window.document.getElementById("bloc_adresse").style.display = "block";
	} else {
		window.document.getElementById("bloc_adresse").style.display = "none";
	}
}


// Fonction de vérification de l'adresse email

function formulaireVerification (objet) {
	var champsEmailCorrect = window.document.getElementById('email_correct');
	// Lecture des champs pour correction de la casse
	var champsEmail  = window.document.getElementById('email');
	var champsNom    = window.document.getElementById('Nom');
	var champsPrenom = window.document.getElementById('Prenom');
	var champsVille  = window.document.getElementById('ville');
	
	champsEmail.value  = champsEmail.value.toLowerCase()
	champsNom.value    = champsNom.value.toUpperCase()
	champsPrenom.value = champsPrenom.value.substr(0,1).toUpperCase() + champsPrenom.value.substr(1).toLowerCase() 
//	toUpperCase()
	champsVille.value  = champsVille.value.toUpperCase()
	
	if (champsEmailCorrect.value == 1)  {
		//objet.submit();
		
		return( true);
	} else {
		var message ='Email Incorrect\n';
		message = message + 'Vous devez fournir une adresse email valide pour vous enregistrer';
		alert(message);
		return(false);
	}
	return false; 
		
	
}
// Fonction de vérification de l'adresse email
function emailVerification(object) {
	// Lecture de la valeur du champs
	var texte = object.value; 
	var champsEmail = window.document.getElementById('email');
	var texteEtatEmail = window.document.getElementById('etat_email');
	var champsEmailCorrect = window.document.getElementById('email_correct');
	
	
	
	// Suppression des accents
	texte = supprAccent(texte);
	
	// correction du champs avant la vérification
	object.value = texte.toLowerCase();
	
	// Si on a tapé au moins 3 caractères
	if(texte.length > 0) {
		
		var texteRequete = 'experianVerifEmail.php?email=' + texte  ;
		
		var requete = getXMLHttpRequest(); 
		
		
		requete.open("get" , texteRequete , false); 
		requete.send(null); 
			
		var rep = eval("(" + requete.responseText + ")");
		
		// Inscription du commentaire sur l'email
		var leTexte = ''; 
		if(rep.email.ok == true) {
			leTexte = 'Email Ok';
			texteEtatEmail.style.color = "#0C0";
			champsEmail.style.backgroundColor = "#0C0";
			champsEmailCorrect.value = 1;
		} else {
			leTexte = 'Email incorrect';
			texteEtatEmail.style.color = "#F00";
			champsEmail.style.backgroundColor = "#F00";
			champsEmailCorrect.value = 1;
			
			if(rep.email.corrections[0] != null) { //'' && rep.email.corrections[0] != 'undefined' ){ 
				champsEmail.value =  rep.email.corrections[0];
				leTexte = 'Email corrigé';
				texteEtatEmail.style.color = "#FC3";
				champsEmail.style.backgroundColor = "#FC3";
				champsEmailCorrect.value = 1;
			} else {
				leTexte = 'Email incorrect';
				texteEtatEmail.style.color = "#F00";
				champsEmail.style.backgroundColor = "#F00";
				champsEmailCorrect.value = 0;
				
			}
		}	
		
		var enfant = texteEtatEmail.firstChild;
		if(enfant != null) 
			enfant.nodeValue = leTexte;
		
	} else {
		texteEtatEmail.style.color = "#FFF";
		champsEmail.style.backgroundColor = "#EEE";
		// Mettre ce champs 0 pour email obligatoire et à 1 pour email facultatif
		champsEmailCorrect.value = 1; 
		
	}

}

function getXMLHttpRequest() {
	
	if(window.XMLHttpRequest) {
		var xmlHttpReq = new XMLHttpRequest(); 
		if(xmlHttpReq.overrideMimeType) {
			xmlHttpReq.overrideMimeType("text/xml");
		}
		return xmlHttpReq; 
	} else {
		try {
			return new activeXObect("Msxml2.XMLHTTP");
		} catch (err) {}
		try {
			return new activeXObect("Microsoft.XMLHTTP");
		} catch (err) {}
	}
	throw new Error("Impossible de créer l'objet XMLHttpRequest pour le navigateur");
}

function supprAccent (texte) {
	var accent = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûüÿÑñÇç ";
	var noAccent = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuyNnCc-";
	
	var sansAccent = ""; 
	
	for(var i = 0 ; i < accent.length ; i++) {
		
		texte = texte.replace(accent.charAt(i), noAccent.charAt(i));
	}
	return texte;
		  
}
