var xhr = null;
var DOCUMENT_ROOT = '/~dsimprim';

function creerRequete() {
	xhr = new XMLHttpRequest();
	
	if (xhr == null) {
		// console.log("Impossible de creer l'objet requete !");
	}
}

function getNewPassword() {
	creerRequete();
	
	var url = DOCUMENT_ROOT + "/FRONT/ajax/generateNewPassword.php?email=" + $("#lostPassword").val();
	
	xhr.open("GET", url, true);
	xhr.onreadystatechange = displayMessageNewPassword;
	xhr.send(null);
}

function displayMessageNewPassword() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var docXML = xhr.responseXML;
			
			var texte = docXML.getElementsByTagName("texte");
			texte = texte[0].firstChild.nodeValue;
			
			var urlRetour = '<p><a href="#" onclick="disableLostPasswordPopin(); return false;">Fermer la popin.</a></p>';
			
			$("#informationsLostPassword").html("");
			$("#informationsLostPassword").html(texte + urlRetour);
		}
	}
}

function getDeliveryAddress(id_adresse) {
	creerRequete();
	
	var url = DOCUMENT_ROOT + "/FRONT/ajax/getDeliveryAddress.php?id_adresse=" + id_adresse;
	
	xhr.open("GET", url, true);
	xhr.onreadystatechange = fillDeliveryAddress;
	xhr.send(null);
}

function fillDeliveryAddress() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var docXML = xhr.responseXML;
			
			var id_adresse = docXML.getElementsByTagName("id");
			id_adresse = id_adresse[0].firstChild.nodeValue;
			
			var label = docXML.getElementsByTagName("label");
			label = label[0].firstChild.nodeValue;
			
			var civilite = docXML.getElementsByTagName("civilite");
			civilite = civilite[0].firstChild.nodeValue;
			
			var prenom = docXML.getElementsByTagName("prenom");
			prenom = prenom[0].firstChild.nodeValue;
			
			var nom = docXML.getElementsByTagName("nom");
			nom = nom[0].firstChild.nodeValue;
			
			var voie = docXML.getElementsByTagName("voie");
			voie = voie[0].firstChild.nodeValue;
			
			var complement = docXML.getElementsByTagName("complement");
			complement = complement[0].firstChild.nodeValue;
			
			var codePostal = docXML.getElementsByTagName("codePostal");
			codePostal = codePostal[0].firstChild.nodeValue;
			
			var ville = docXML.getElementsByTagName("ville");
			ville = ville[0].firstChild.nodeValue;
			
			$("#popinDeliveryAddress #label").val(label);
			$("#popinDeliveryAddress #civility").val(civilite);
			$("#popinDeliveryAddress #firstname").val(prenom);
			$("#popinDeliveryAddress #lastname").val(nom);
			$("#popinDeliveryAddress #address").val(voie);
			$("#popinDeliveryAddress #complement").val(complement);
			$("#popinDeliveryAddress #zipCode").val(codePostal);
			$("#popinDeliveryAddress #city").val(ville);
		}
	}		
}
