var xhr = null;

function getXhr()
{
  if(window.XMLHttpRequest)
   {
    xhr = new XMLHttpRequest();
   }
  else if(window.ActiveXOject)
   {
    try
     {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
     }
    catch(e)
     {
      try
       {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
       }
      catch(el)
       {
        xhr = null;
       }
     }
   }
  else
   {
    alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest\nVeuillez le mettre à jour");
   }
   return xhr;
}


/*------------ Chargement des infos de location Arkianne ---------------*/
function charge_location(semaine) {
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200) {
			var xmldoc 		= xhr.responseXML.documentElement;
			var nb_debut 	= xmldoc.getElementsByTagName("date_debut").length;
	  		var nb_fin	 	= xmldoc.getElementsByTagName("date_fin").length;
			var nb_pers	 	= xmldoc.getElementsByTagName("id_pers").length;
			var jl			= xmldoc.getElementsByTagName("jl")[0].childNodes[0].nodeValue;

			node_debut = document.getElementById('date_debut');
			node_fin   = document.getElementById('date_fin');
			node_pers  = document.getElementById('nb_pers');
			
			if (nb_debut > 0) {
				var valeur_select = '<select name="date_debut" id="choix_debut" style="width:150px;">';	
				
				for(i=0;i<nb_debut;i++) {
					var date_debut = xmldoc.getElementsByTagName("date_debut")[i].childNodes[0].nodeValue;

					valeur_select = valeur_select + '<option value="' + date_debut + '">' + date_debut + '</option>';
				}	  
			  	valeur_select = valeur_select + '</select>';
				node_debut.innerHTML = valeur_select;
			}
			$("#choix_debut").change(function(){
				document.getElementById('date_fin').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';
				document.getElementById('nb_pers').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';
				charge_fin(this.value,jl);
			});
			
			if (nb_fin > 0) {
				var valeur_select = '<select name="date_fin" id="choix_fin" style="width:150px;">';	
				
				for(i=0;i<nb_fin;i++) {
					var date_fin = xmldoc.getElementsByTagName("date_fin")[i].childNodes[0].nodeValue;

					valeur_select = valeur_select + '<option value="' + date_fin + '">' + date_fin + '</option>';
				}	  
			  	valeur_select = valeur_select + '</select>';
				node_fin.innerHTML = valeur_select;
			}
			$("#choix_fin").change(function(){
				document.getElementById('nb_pers').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';
				charge_pers(document.getElementById("choix_debut").value, this.value);
			});
			
			if (nb_pers > 0) {
				var valeur_select = '<select name="pers" id="choix_pers" style="width:150px;">';	
				
				for(i=0;i<nb_pers;i++) {
					var id_pers = xmldoc.getElementsByTagName("id_pers")[i].childNodes[0].nodeValue;
					var texte_pers = xmldoc.getElementsByTagName("texte_pers")[i].childNodes[0].nodeValue;
					
					valeur_select = valeur_select + '<option value="' + id_pers + '">' + texte_pers + '</option>';
				}	  
			  	valeur_select = valeur_select + '</select>';
				node_pers.innerHTML = valeur_select;
			}
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    
	}
	
	document.getElementById('date_debut').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';
	document.getElementById('date_fin').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';
	document.getElementById('nb_pers').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';

	xhr.open("POST",'javascript/charge_location.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("semaine=" + semaine);	
}

/*------------ Chargement des date de fin quand on change la date de debut ---------------*/
function charge_fin(date_debut, jl) {
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200) {
			var xmldoc 		= xhr.responseXML.documentElement;
	  		var nb_fin	 	= xmldoc.getElementsByTagName("date_fin").length;
			var nb_pers	 	= xmldoc.getElementsByTagName("id_pers").length;
			
			if (nb_fin > 0) {
				var valeur_select = '<select name="date_fin" id="choix_fin" style="width:150px;">';	
				
				for(i=0;i<nb_fin;i++) {
					var date_fin = xmldoc.getElementsByTagName("date_fin")[i].childNodes[0].nodeValue;

					valeur_select = valeur_select + '<option value="' + date_fin + '">' + date_fin + '</option>';
				}	  
			  	valeur_select = valeur_select + '</select>';
				node_fin.innerHTML = valeur_select;
			}
			$("#choix_fin").change(function(){
				document.getElementById('nb_pers').innerHTML = '<img src="images/loading.gif" style="padding-left:2px;">';
				charge_pers(document.getElementById("choix_debut").value, this.value);
			});
			
			if (nb_pers > 0) {
				var valeur_select = '<select name="pers" id="choix_pers" style="width:150px;">';	
				
				for(i=0;i<nb_pers;i++) {
					var id_pers = xmldoc.getElementsByTagName("id_pers")[i].childNodes[0].nodeValue;
					var texte_pers = xmldoc.getElementsByTagName("texte_pers")[i].childNodes[0].nodeValue;
					
					valeur_select = valeur_select + '<option value="' + id_pers + '">' + texte_pers + '</option>';
				}	  
			  	valeur_select = valeur_select + '</select>';
				node_pers.innerHTML = valeur_select;
			}

		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    
	}
	
	//alert("date_debut=" + date_debut + "&jl=" + jl);

	xhr.open("POST",'javascript/charge_fin.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("date_debut=" + date_debut + "&jl=" + jl);	
}

/*------------ Chargement du nombre de personne quand on change la date de fin ---------------*/
function charge_pers(date_debut, date_fin) {
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200) {
			var xmldoc 		= xhr.responseXML.documentElement;
			var nb_pers	 	= xmldoc.getElementsByTagName("id_pers").length;

			if (nb_pers > 0) {
				var valeur_select = '<select name="pers" id="choix_pers" style="width:150px;">';	
				
				for(i=0;i<nb_pers;i++) {
					var id_pers = xmldoc.getElementsByTagName("id_pers")[i].childNodes[0].nodeValue;
					var texte_pers = xmldoc.getElementsByTagName("texte_pers")[i].childNodes[0].nodeValue;
					
					valeur_select = valeur_select + '<option value="' + id_pers + '">' + texte_pers + '</option>';
				}	  
			  	valeur_select = valeur_select + '</select>';
				node_pers.innerHTML = valeur_select;
			}

		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    
	}

	xhr.open("POST",'javascript/charge_pers.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("date_debut=" + date_debut + "&date_fin=" + date_fin);	
}


 //******************************* ajout d'un logement a la selection********************
function add_logement(id_logement){
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			var xmldoc 			= xhr.responseXML.documentElement;
			var zone_geo		= xmldoc.getElementsByTagName("zone_geo")[0].childNodes[0].nodeValue;
			var type_logement 	= xmldoc.getElementsByTagName("type_logement")[0].childNodes[0].nodeValue;
			var superficie 		= xmldoc.getElementsByTagName("superficie")[0].childNodes[0].nodeValue;
			var prix 			= xmldoc.getElementsByTagName("prix")[0].childNodes[0].nodeValue;
			var id_logement		= xmldoc.getElementsByTagName("id_logement")[0].childNodes[0].nodeValue;
			var ajout			= xmldoc.getElementsByTagName("ajout")[0].childNodes[0].nodeValue;
			var lien_fiche	 	= xmldoc.getElementsByTagName("lien_fiche")[0].childNodes[0].nodeValue;
				
			if (ajout == 1) {
				document.getElementById("nb_select").value = parseInt(document.getElementById("nb_select").value) + 1;
				
				var tr = document.createElement('tr');
				tr.className = 'tr_lien';
				document.getElementById('selection').appendChild(tr);
				
				var td1 = document.createElement('td');
				td1.innerHTML = '<span style="font-size:11px;">' + zone_geo + '</span>';
				td1.style.borderBottom = "1px solid #aaaaaa";
				//td1.onclick = function(){location.href=type_logement + "-" + zone_geo + ","  + id_logement + ",f.html"};
				td1.onclick = function(){location.href=lien_fiche};
				tr.appendChild(td1);
				
				var td2 = document.createElement('td');
				td2.innerHTML = '<span style="font-size:10px;">' + type_logement + ' - ' + superficie + 'm&sup2;</span><br>' + prix + ' &euro;';
				td2.style.borderBottom = "1px solid #aaaaaa";
				//td2.onclick = function(){location.href=type_logement + "-" + zone_geo + ","  + id_logement + ",f.html"};
				td2.onclick = function(){location.href=lien_fiche};
				tr.appendChild(td2);
				
				var td3 = document.createElement('td');
				td3.innerHTML = '<img onclick="del_logement(' + id_logement + '); this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);" style="cursor:pointer;" src="images/bouton/croix.gif" width="19" height="17" border="0" onmouseover="this.src=\'images/bouton/croix2.gif\'" onmouseout="this.src=\'images/bouton/croix.gif\'" />';
				td3.style.borderBottom = "1px solid #aaaaaa";
				tr.appendChild(td3);

				document.getElementById("pas_logement").style.display = "none";
				document.getElementById("ajout_"+id_logement).src = "images/bouton/ajouter2.png";
				document.getElementById("selection").parentNode.style.display = "";
				document.getElementById("selection").style.display = "";
			}

			$("img.chargement").css("display","none");
			$("img.ajout_selecteur").css("display","");
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    

  	}

	$("img.ajout_selecteur").css("display","none");
	$("img.chargement").css("display","");
    
	xhr.open("POST",'javascript/add_logement.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_logement=" + id_logement);
}


 //******************************* suppression d'un logement de la selection ********************
function del_logement(id_logement){
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200) {
			document.getElementById("nb_select").value = parseInt(document.getElementById("nb_select").value) - 1;
			if (document.getElementById("nb_select").value == 0) {
				document.getElementById("pas_logement").style.display = "";
				document.getElementById("selection").style.display = "none";	
			}
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    
	}

	xhr.open("POST",'javascript/del_logement.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_logement="+id_logement);
}


 //******************************* ajout d'un logement j'aime ********************
function add_jaime(id_logement){
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			$("img.chargement").css("display","none");
			$("img.ajout_selecteur").css("display","");
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    

  	}

	$("img.ajout_selecteur").css("display","none");
	$("img.chargement").css("display","");
    
	xhr.open("POST",'javascript/add_jaime.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_logement=" + id_logement);
}

 //******************************* suppression d'un logement j'aime ********************
function del_jaime(id_logement){
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			$("img.chargement").css("display","none");
			$("img.ajout_selecteur").css("display","");
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    

  	}

	$("img.ajout_selecteur").css("display","none");
	$("img.chargement").css("display","");
    
	xhr.open("POST",'javascript/del_jaime.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_logement=" + id_logement);
}

 //******************************* ajout d'un logement j'aime pas ********************
function add_jaimepas(id_logement){
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			$("img.chargement").css("display","none");
			$("img.ajout_selecteur").css("display","");
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    

  	}

	$("img.ajout_selecteur").css("display","none");
	$("img.chargement").css("display","");
    
	xhr.open("POST",'javascript/add_jaimepas.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_logement=" + id_logement);
}

 //******************************* suppression d'un logement j'aime pas ********************
function del_jaimepas(id_logement){
	getXhr()
  	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			$("img.chargement").css("display","none");
			$("img.ajout_selecteur").css("display","");
		}
		else {
			//alert("xhr.readyState : " + xhr.readyState + " xhr.status : " + xhr.status);
		}    

  	}

	$("img.ajout_selecteur").css("display","none");
	$("img.chargement").css("display","");
    
	xhr.open("POST",'javascript/del_jaimepas.asp',true)
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_logement=" + id_logement);
}
 
