// Renvoie une instance de XmlHttpRequester
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 updDpt(dpt)
{
	var xhr = getXhr();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			resp = xhr.responseText;
			document.getElementById('ville').innerHTML = resp;
			updVille(dpt, null);
		}
	}
	xhr.open("POST","ville_combo.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	iddpt = dpt.options[dpt.selectedIndex].value;
	xhr.send("dpt="+iddpt);
}

function updVille(dpt, ville)
{
	var xhr = getXhr();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			resp = xhr.responseText;
			document.getElementById('type').innerHTML = resp;
			updType(dpt, null, null);
		}
	}
	xhr.open("POST","type_combo.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	iddpt = dpt.options[dpt.selectedIndex].value;
	idville = (ville != null && (typeof(ville) != "undefined"))?ville.options[ville.selectedIndex].value:"undefined";
	xhr.send("dpt="+iddpt+"&ville="+idville);
}

function updType(dpt, ville, type)
{
	var xhr = getXhr();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			resp = xhr.responseText;
			document.getElementById('cpt').innerHTML = resp;
		}
	}
	xhr.open("POST","cpt_combo.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	iddpt = dpt.options[dpt.selectedIndex].value;
	idville = (ville != null && (typeof(ville) != "undefined"))?ville.options[ville.selectedIndex].value:"undefined";
	idtype = (type != null && (typeof(type) != "undefined"))?type.options[type.selectedIndex].value:"undefined";
	xhr.send("dpt="+iddpt+"&ville="+idville+"&type="+idtype);
}

//*************************************

function updDispo(an,ref)
{
	var xhr = getXhr();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			resp = xhr.responseText;
			document.getElementById('calendar').innerHTML = resp;
		}
	}
	xhr.open("POST","dispo.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("an="+an+"&ref="+ref);
}


//*************************************
// CALCUL RESA EN TEMPS REEL
//*************************************
function updRtPrix(ref,dep,nbj,ass,fd)
{
	iddep = dep.options[dep.selectedIndex].value;
	idnbj = nbj.options[nbj.selectedIndex].value;
	for (i=0 ; i < 2 ; i++) 
	{ 
		if (ass[i].checked == true) idass = ass[i].value;
	}
	
	if (iddep != "" && idnbj != "")
	{
		var xhr = getXhr();
		
		// Sejour
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				resp = xhr.responseText;
				document.getElementById('rt_px_sejour').innerHTML = resp;
				
				updRtAssur(ref,dep,nbj,ass,fd);
			}
		}
		xhr.open("POST","calcul.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("type=1&ref="+ref+"&dep="+iddep+"&nbj="+idnbj+"&ass="+idass+"&fd="+fd);
		
		
	}

}

function updRtAssur(ref,dep,nbj,ass,fd)
{
	var xhr = getXhr();
	
	// Assurance
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			resp = xhr.responseText;
			document.getElementById('rt_px_assur').innerHTML = resp;
			
			updRtTotal(ref,dep,nbj,ass,fd);
		}
	}
	xhr.open("POST","calcul.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("type=2&ref="+ref+"&dep="+iddep+"&nbj="+idnbj+"&ass="+idass+"&fd="+fd);
}

function updRtTotal(ref,dep,nbj,ass,fd)
{
	var xhr = getXhr();
	
	// Total
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			resp = xhr.responseText;
			document.getElementById('rt_px_total').innerHTML = resp;
		}
	}
	xhr.open("POST","calcul.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("type=3&ref="+ref+"&dep="+iddep+"&nbj="+idnbj+"&ass="+idass+"&fd="+fd);
}

/*********************************/

function sendSearch()
{
	document.form1.submit();
}

