// Creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	
	// xmlHttp will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// Try to instantiate the native XMLHttpRequest object
	try
	{
		// Create an XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// Assume IE6 or older
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e) { }
	}
	// Return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		
		return xmlHttp;
}


/* *************************** */
// Function executed when the state of the request changes
function handleRequestStateChange()
{
	// Continue if the process is completed
	if (xmlHttp.readyState == 4)
	{
		// Continue only if HTTP status is "OK"
		if (xmlHttp.status == 200)
		{
			try
			{
			// Retrieve the response
			response = xmlHttp.responseText;
			// Do something with the response
			
			//alert(response);
			var idChalet = response.substr(0,1);
			var cal = response.substr(1);
			idChalet = "boiteCalendrier"+idChalet;
			document.getElementById(idChalet).innerHTML = cal;
			
			}
			catch(e)
			{
			// Display error message
			alert("Error reading the response: " + e.toString());
			
			}
		}
		else
		{
			// Display status message
			alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}

/* create ajax object */
/*
// Hide the "Updating..." message
document.getElementById('updating').style.visibility = 'hidden';
*/
var xmlHttp = createXmlHttpRequestObject();



/*
*	ajax calls
*/

// Performs a server request and assigns a callback function
function changeCalMonth(idChalet,prefix,date)
{
	// Continue only if xmlHttp isn't void
	if (xmlHttp)
	{
		// Try to connect to the server
		try
		{
			// Initiate server request
			
			//document.getElementById('updating').style.visibility = 'visible';
			var mytime= "&ms="+new Date().getTime();
			var url = "cal_chg_month.php?"+prefix+"="+date+"&idChalet="+idChalet+mytime;
			xmlHttp.open("GET", url, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// Display an error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}
