//Global vars to hold connection to web pages
var xmlHttp1
var xmlHttp2
var xmlHttp3

var n
function checkChambreNb(nb) { 
	document.getElementById("message").innerHTML="<img src=\"/images/ajax-loader.gif\" border=\"0\"/>";
	
	n=nb;
	xmlHttp3=GetXmlHttpObject()

	if (xmlHttp3==null ) {
	 	alert ("Browser does not support HTTP Request")
	 	return
	}
 
	var snd="";
	var idhotel = document.getElementById("idhotel");
	var nbr_adultes = document.getElementById("nbr_adultes"+nb);
	var nbr_enfants = document.getElementById("nbr_enfants"+nb);
	var chambre = document.getElementById("chambre"+nb);
	if(document.getElementById("regime"+nb)!=null){
		var regime = document.getElementById("regime"+nb);
		snd="&regime"+nb+"=" + escape(regime.value);
	}
	snd="nb=" + escape(nb)+ "&nbr_adultes"+nb+"="+ escape(nbr_adultes.value)+ "&nbr_enfants"+nb+"="+ escape(nbr_enfants.value)+ snd + "&idhotel=" + escape(idhotel.value)+ "&chambre"+nb+"=" + escape(chambre.value);
	var url="setChambres.php?"
	url=url+snd
	url=url+"&sid="+Math.random()
	xmlHttp3.onreadystatechange=message14
	xmlHttp3.open("GET",url,true)
	xmlHttp3.send(null)
	setTimeout("checkHotelDispo()",2500);
}

function checkHotelSuppl() { 
	document.getElementById("message").innerHTML="<img src=\"/images/ajax-loader.gif\" border=\"0\"/>";
	
	xmlHttp2=GetXmlHttpObject()

	if (xmlHttp2==null ) {
	 	alert ("Browser does not support HTTP Request")
	 	return
	}
 
	var idhotel = document.getElementById("idhotel").value;
    var debut = document.getElementById("datepicker1").value;
    var fin = document.getElementById("datepicker2").value;
    
     //if(compare(fin, debut)==-1){
     // alert("La date de d\351part doit \352tre supp\351rieur \340 la date d'arriv\351e!");
     // document.getElementById("message").innerHTML="";
     // return false;
   // }
   
    
	var url="getSuppl.php"
	url=url+"?debut=" + escape(debut)
	url=url+"&fin=" + escape(fin)
	url=url+"&idhotel=" + escape(idhotel)
	url=url+"&sid="+Math.random()
	//Once the page finished loading put it into the div
	xmlHttp2.onreadystatechange=message5 

	//Get the php page
	xmlHttp2.open("GET",url,true)
	xmlHttp2.send(null)
	setTimeout("checkHotelDispo()",2500);
}

function checkHotelDispo() { 

	document.getElementById("message").innerHTML="<img src=\"/images/ajax-loader.gif\" border=\"0\"/>";
	
	xmlHttp1=GetXmlHttpObject()
	//If we cant do the request error out
	if (xmlHttp1==null) {
	 	alert ("Browser does not support HTTP Request")
	 	return
	}
	
	var idhotel = document.getElementById("idhotel");
	var debut = document.getElementById("datepicker1");
    var fin = document.getElementById("datepicker2");
	var nbv = document.getElementById("nbr_chambre").value;
	var snd="nb=" + escape(nbv) +"&idhotel=" + escape(idhotel.value) +"&debut=" + escape(debut.value) + "&fin=" + escape(fin.value);
	for(var nb = 1; nb <= nbv; nb++){
		var chambre = document.getElementById("chambre"+nb);
		if(document.getElementById("regime"+nb)!=null){
			var regime = document.getElementById("regime"+nb);
			snd+="&regime"+nb+"=" + escape(regime.value);
		}
	    var nbr_adultes = document.getElementById("nbr_adultes"+nb);
	    var nbr_enfants = document.getElementById("nbr_enfants"+nb);
	    if(document.getElementById("nbr_bebe"+nb)!=null){
			var nbr_bebe = document.getElementById("nbr_bebe"+nb);
			snd+="&nbr_bebe"+nb+"=" + escape(nbr_bebe.value);
		}
	    snd+="&chambre"+nb+"=" + escape(chambre.value) + "&nbr_adultes"+nb+"=" + escape(nbr_adultes.value) +"&nbr_enfants"+nb+"=" + escape(nbr_enfants.value);
	}
	
	if(document.form2.suppl!=null){
		var suppl = getSelectedCheckboxValue(document.form2.suppl);
		document.getElementById("supplSelected").value=suppl;
		snd+="&suppl=" + escape(suppl);
	}

	var url="checkSaison.php?"
	url=url+snd
	url=url+"&sid="+Math.random()
	
	//Once the page finished loading put it into the div
	xmlHttp1.onreadystatechange=message1
	
	//Get the php page
	xmlHttp1.open("GET",url,true)
	xmlHttp1.send(null)
}

function message5() { 
	//IF this is getting called when the page is done loading then fill the pagination div
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
	 	document.getElementById("message5").innerHTML=xmlHttp2.responseText 
	} 
}

function message14() { 
	//IF this is getting called when the page is done loading then fill the pagination div
	if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
	 	document.getElementById("message"+n).innerHTML=xmlHttp3.responseText 
	} 
}

function message1() { 
	//IF this is getting called when the page is done loading then fill the pagination div
	if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
	 	document.getElementById("message").innerHTML=xmlHttp1.responseText 
	} 
}

function GetXmlHttpObject() {
	//Determine what browser we are on and make a httprequest connection for ajax
	var xmlHttp=null;

	try {
	 	// Firefox, Opera 8.0+, Safari
	 	xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	 	//Internet Explorer
	 	try {
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  	}
	 	catch (e) {
	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  	}
	}
	
	return xmlHttp;
}
