// holds an instance of XMLHttpRequest
var xmlHttp_cal = createXmlHttpRequestObject_cal();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject_cal()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp_cal;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp_cal = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
						'MSXML2.XMLHTTP.5.0',
						'MSXML2.XMLHTTP.4.0',
						'MSXML2.XMLHTTP.3.0',
						'MSXML2.XMLHTTP',
						'Microsoft.XMLHTTP');
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp_cal; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp_cal = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp_cal)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp_cal;
}

// read a file from the server
function process(what)
{
	// only continue if xmlHttp_cal isn't void
	if (xmlHttp_cal)
	{
	// try to connect to the server
		try
		{
			// initiate reading a file from the server
			xmlHttp_cal.open("GET", absoluter_pfad + "includes/calendar.php?xml=true&cal="+what, true);
			xmlHttp_cal.onreadystatechange = handleRequestStateChange_cal;
			xmlHttp_cal.send(null);
		}
		// display the error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

// function called when the state of the HTTP request changes
function handleRequestStateChange_cal()
{
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp_cal.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if (xmlHttp_cal.status == 200)
		{
			try
			{
				// do something with the response from the server
				handleServerResponse_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_cal.statusText);
		}
	}
}


// handles the response received from the server
function handleServerResponse_cal()
{
	// read the message from the server
	var xmlResponse = xmlHttp_cal.responseXML;

	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	// obtain arrays with book titles and ISBNs
	cal_month = xmlRoot.getElementsByTagName("month")[0].firstChild.data;
	cal_today = xmlRoot.getElementsByTagName("today")[0].firstChild.data;
	cal_before = xmlRoot.getElementsByTagName("before")[0].firstChild.data;
	cal_next = xmlRoot.getElementsByTagName("next")[0].firstChild.data;
	cal_innerhtml = xmlRoot.getElementsByTagName("innerhtml")[0].firstChild.data;

	document.getElementById("calmonth").innerHTML = cal_month;

	document.getElementById("caltoday").href = document.getElementById("caltoday").href.replace(/\d+-\d+/,cal_today);
	document.getElementById("caltoday").onclick = new Function("return cal('"+cal_today+"');");

	document.getElementById("calbefore").href = document.getElementById("calbefore").href.replace(/\d+-\d+/,cal_before);
	document.getElementById("calbefore").onclick = new Function("return cal('"+cal_before+"');");

	document.getElementById("calnext").href = document.getElementById("calnext").href.replace(/\d+-\d+/,cal_next);
	document.getElementById("calnext").onclick = new Function("return cal('"+cal_next+"');");

	if (!window.ActiveXObject)
	{
		document.getElementById("calbody").innerHTML = cal_innerhtml;
	}
}
