/*
Ajax Script - Dan Hearnah 01/04/2009
*/

/* The Ajax Class */
function makeDanAJAX()
{
	this.ajaxRequest;
	try 
	{
		this.ajaxRequest = new XMLHttpRequest();
		if (this.ajaxRequest.overrideMimeType) 
		{
			this.ajaxRequest.overrideMimeType('text/xml');
		}
	} 
	catch (trymicrosoft) 
	{
		try 
		{
			this.ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (othermicrosoft) 
		{
			try 
			{
				this.ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (failed) 
			{
				this.ajaxRequest = false;
			}
		}
	}
	
	if (!this.ajaxRequest)
	alert("Error initializing XMLHttpRequest!");
	return this.ajaxRequest;
}

/* Make the Object */
DansAjaxRequest = new makeDanAJAX();

/* The Functions */
function doPush(month,day,year)
{
	var url = "calinc/getcal.php?";
	var postvars = "month="+month+"&day="+day+"&year="+year;
	/* open the conenction post or get */
	DansAjaxRequest.open("POST", url, true);
	/* define what function to run as the callback */
	DansAjaxRequest.onreadystatechange = updatePage;
	/* build http headers */
	DansAjaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	DansAjaxRequest.setRequestHeader("Content-length", postvars.length);
	DansAjaxRequest.setRequestHeader("Connection", "close");
	/* go and do it then */
	DansAjaxRequest.send(postvars);  
}

function updatePage()
{
	if (DansAjaxRequest.readyState == 4) 
	{
		// hide  waiting dialogue here
		
		if (DansAjaxRequest.status == 200) 
		{
			var output = DansAjaxRequest.responseText;
			// pull out the displayed content
			var xhtmlstart = output.search(/<xhtml>/);
			var xhtmlend = output.search(/<\/xhtml>/);
			if(xhtmlstart != -1)
			{
				var thexhtml = output.substring((xhtmlstart + 7) ,xhtmlend);
				// pull out the element id to update
				var elementstart = output.search(/<element>/);
				var elementend = output.search(/<\/element>/);
				var theElement = "";
				if(elementstart != -1)
					theElement = output.substring((elementstart + 9) ,elementend);
				if(document.getElementById(theElement))
					document.getElementById(theElement).innerHTML = unescape(thexhtml);
			}
			/* 
			else
			{
				// there was an error the output wasn't in my defined xml schema
			}
			*/
		}
		else
		{
				if(document.getElementById(theElement))
					document.getElementById(theElement).innerHTML = "There was an Error in the Ajax Request Document Communication";
		}
	}
	/* 
	else
	{
		// trigger to show the waiting dialogue in here
	}
	*/
}

