String.prototype.trim = function()
{ return this.replace(/(^\s*)|(\s*$)/g, ""); }

function getXhr(){
	var xhr = null;
	if(window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest();
	else if(window.ActiveXObject){ // Internet Explorer
	try {
		xhr = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false;
	}
	return xhr;
}

function envoiEmail(formulaire){
	var xhr = getXhr();	
	
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le	serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			strReponse = xhr.responseText;			
			strReponse = strReponse.trim();		
			
			if(strReponse == "Valide"){
				effaceForm(formulaire);
				alert("votre demande est prise en compte");
			}else{
				alert("impossible d'envoyer votre message");
			}
		}
	}
	// Ici on va voir comment faire du post
	xhr.open("POST","./envoiEmail.php",true);
	// ne pas oublier ça pour le post
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	argument ="personne=" + document.getElementById("personne").value + "&email=" + document.getElementById("email").value + "&periode=" + document.getElementById("periode").value + "&information=" + document.getElementById("information").value;
	
	//argument
	
	xhr.send(argument);
}

function controleForm(formulaire){
	var valide = true;
	var tabInformation = document.getElementById(formulaire).getElementsByTagName("input");	
	
	for(i=0;i<tabInformation.length;i++){
		if(tabInformation[i].type=="text"){
			if(tabInformation[i].value == ""){			
				tabInformation[i].style.backgroundColor = "#123456";
				tabInformation[i].style.color="#FFF";
				
				valide=false;
			}
			else{
				tabInformation[i].style.backgroundColor = "#FFFFFF";
				tabInformation[i].style.color="#000";
			}
		}
	}
	
	var tabInformation = document.getElementById(formulaire).getElementsByTagName("textarea");	
	
	for(i=0;i<tabInformation.length;i++){
		if(tabInformation[i].value == ""){
			
			tabInformation[i].style.backgroundColor = "#123456";
			tabInformation[i].style.color="#FFF";
			
			valide=false;
		}
		else{
			tabInformation[i].style.backgroundColor = "#FFFFFF";
			tabInformation[i].style.color="#000";
		}
	}
	
	if(valide){
		envoiEmail(formulaire);
		
	}else{
		alert("Les champs de couleur bleu sont obligatoires !!!!");
	}
	
}

function effaceForm(formulaire){
	var tabInformation = document.getElementById(formulaire).getElementsByTagName("input");	
	
	for(i=0;i<tabInformation.length;i++){
		if(tabInformation[i].type=="text"){
			tabInformation[i].value = "";
		}
	}
	
	var tabInformation = document.getElementById(formulaire).getElementsByTagName("textarea");	
	
	for(i=0;i<tabInformation.length;i++){
		tabInformation[i].value = "";
	}
	
}