
var	lastAjaxRequestUrl = "";
var AjaxEnableDebugMsg = false;

//////////////////////////////////////////////////////////////////////////////////////
// Send AJAX response to server using POST
//////////////////////////////////////////////////////////////////////////////////////

function sendAjaxPost(url,data)
{
	// Add a time code onto the end of the request URL so that it will always be
	// different, even if we're asking for the same thing as something else
	// that we may have done in the recent past, in order to prevent browser caching.
	d = new Date()
	t = d.getTime();
	url = url + "&nc=" + t;
	
	var AjaxRequest = null;
	// Get a request object	
	try
	{
		AjaxRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				AjaxRequest = null;
				alert("Your web browser is incompatible with the editing functions on this page.");
			}
		}
	}

	// Send the request
	lastAjaxRequestUrl = url;
	AjaxRequest.open("POST", url);
	AjaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    AjaxRequest.setRequestHeader("Content-length", data.length);
    AjaxRequest.setRequestHeader("Connection", "close");
	
	// Use anonymous inner function as callback
	AjaxRequest.onreadystatechange = function() { ajaxIncomingCallback(AjaxRequest,null); }
	AjaxRequest.send(data);
}

//////////////////////////////////////////////////////////////////////////////////////
// Send AJAX response to server using POST, with response to be handled by specified callback function
//////////////////////////////////////////////////////////////////////////////////////

function sendAjaxPostCallback(url,callbackfunction,data)
{
	// Add a time code onto the end of the request URL so that it will always be
	// different, even if we're asking for the same thing as something else
	// that we may have done in the recent past, in order to prevent browser caching.
	d = new Date()
	t = d.getTime();
	url = url + "&nc=" + t;
	
	var AjaxRequest = null;
	// Get a request object	
	try
	{
		AjaxRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				AjaxRequest = null;
				alert("Your web browser is incompatible with the editing functions on this page.");
			}
		}
	}

	// Send the request
	lastAjaxRequestUrl = url;
	AjaxRequest.open("POST", url);
	AjaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    AjaxRequest.setRequestHeader("Content-length", data.length);
    AjaxRequest.setRequestHeader("Connection", "close");
	
	// Use anonymous inner function as callback
	AjaxRequest.onreadystatechange = function() { ajaxIncomingCallback(AjaxRequest,callbackfunction); }
	AjaxRequest.send(data);
}


//////////////////////////////////////////////////////////////////////////////////////
// Send AJAX response to server, with response to be handled by specified callback function
//////////////////////////////////////////////////////////////////////////////////////

function sendAjaxCallback(url,callbackfunction)
{
	// Add a time code onto the end of the request URL so that it will always be
	// different, even if we're asking for the same thing as something else
	// that we may have done in the recent past, in order to prevent browser caching.
	d = new Date()
	t = d.getTime();
	url = url + "&nc=" + t;
	
	var AjaxRequest = null;
	// Get a request object	
	try
	{
		AjaxRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				AjaxRequest = null;
				alert("Your web browser is incompatible with the editing functions on this page.");
			}
		}
	}

	// Send the request
	lastAjaxRequestUrl = url;
	AjaxRequest.open("GET", url, true);
	
	// Use anonymous inner function as callback
	AjaxRequest.onreadystatechange = function() { ajaxIncomingCallback(AjaxRequest,callbackfunction); }
	
	AjaxRequest.send(" ");	// Firefox doesn't like NULL so send a space	
}

//////////////////////////////////////////////////////////////////////////////////////
// Receive AJAX response from server, pass response text to designated callback function
//////////////////////////////////////////////////////////////////////////////////////

function ajaxIncomingCallback(AjaxRequest, callbackfunction)
{

	if (AjaxRequest.readyState == 4)
	{	
		// Check for zero because of stupid goddamn fucking Firefox
		if ( (AjaxRequest.status == 0) || (AjaxRequest.status == 200) )
		{
			if( callbackfunction )
				callbackfunction(AjaxRequest.responseText);
		}
		else if (AjaxRequest.status == 404 )
		{
			// file not found?
			msg = "Error!  Status = " + AjaxRequest.status + "\n";
			msg += lastAjaxRequestUrl;
		}
		else
		{
			//alert("Error! Request status is " + AjaxRequest.status);
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////
// Send AJAX response to server, with response text to be placed into target element
//////////////////////////////////////////////////////////////////////////////////////

function sendAjaxTargeted(url,targetelem, addtimestamp )
{
	if( arguments.length < 3 )
		addtimestamp = false;
	
	// Add a time code onto the end of the request URL so that it will always be
	// different, even if we're asking for the same thing as something else
	// that we may have done in the recent past, in order to prevent browser caching.
	if( addtimestamp )
	{
		d = new Date()
		t = d.getTime();
		url = url + "&nc=" + t;
	}
	
	var AjaxRequest = null;
	// Get a request object	
	try
	{
		AjaxRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				AjaxRequest = null;
				alert("Your web browser is incompatible with the editing functions on this page.");
			}
		}
	}

	// Send the request
	lastAjaxRequestUrl = url;
	AjaxRequest.open("GET", url, true);
	
	// Use anonymous inner function as callback
	AjaxRequest.onreadystatechange = function() { ajaxIncomingTargeted(AjaxRequest,targetelem); }
	
	AjaxRequest.send(" ");	// Firefox doesn't like NULL so send a space	
}

//////////////////////////////////////////////////////////////////////////////////////
// Receive AJAX response from server, pass response text to designated target element
//////////////////////////////////////////////////////////////////////////////////////

function ajaxIncomingTargeted(AjaxRequest, elem )
{
	if (AjaxRequest.readyState == 4)
	{	
		// Check for zero because of stupid goddamn fucking Firefox
		if ( (AjaxRequest.status == 0) || (AjaxRequest.status == 200) || (AjaxRequest.status == 300) )
		{
			document.getElementById(elem).innerHTML = AjaxRequest.responseText;
		}
		else if (AjaxRequest.status == 404 )
		{
			// file not found?
			msg = "Error!  Status = " + AjaxRequest.status + "\n";
			msg += lastAjaxRequestUrl;
		}
		else
		{
			//alert("Error! Request status is " + AjaxRequest.status);
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////
// Send AJAX request to server, using generic callback & response parser
//////////////////////////////////////////////////////////////////////////////////////

function sendAjax(url)
{
	// Add a time code onto the end of the request URL so that it will always be
	// different, even if we're asking for the same thing as something else
	// that we may have done in the recent past, in order to prevent browser caching.
	d = new Date()
	t = d.getTime();
	url = url + "&nc=" + t;
	
	var AjaxRequest = null;
	// Get a request object	
	try
	{
		AjaxRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				AjaxRequest = null;
				alert("Your web browser is incompatible with the editing functions on this page.");
			}
		}
	}

	// Send the request
	lastAjaxRequestUrl = url;
	AjaxRequest.open("GET", url, true);
	
	// Use anonymous inner function as callback
	AjaxRequest.onreadystatechange = function() { ajaxIncoming(AjaxRequest); }
	
	AjaxRequest.send(" ");	// Firefox doesn't like NULL so send a space	
}

//////////////////////////////////////////////////////////////////////////////////////
// Generic callback function
//////////////////////////////////////////////////////////////////////////////////////

function ajaxIncoming(AjaxRequest)
{

	if (AjaxRequest.readyState == 4)
	{	
		// Check for zero because of stupid goddamn fucking Firefox
		if ( (AjaxRequest.status == 0) || (AjaxRequest.status == 200) )
		{
			parseAjaxResponse(AjaxRequest.responseText);
		}
		else if (AjaxRequest.status == 404 )
		{
			// file not found?
			msg = "Error!  Status = " + AjaxRequest.status + "\n";
			msg += lastAjaxRequestUrl;
		}
		else
		{
			//alert("Error! Request status is " + AjaxRequest.status);
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////
// Generic response parser called by generic callback
//////////////////////////////////////////////////////////////////////////////////////

function parseAjaxResponse(theText)
{
	if( AjaxEnableDebugMsg )
	{
		if( theText.indexOf("Query:") >= 0 )
		{
			if( document.getElementById("ajaxdebugmsg") )
				document.getElementById("ajaxdebugmsg").innerHTML = lastAjaxRequestUrl + "<br>" + theText.substr(theText.substr(theText.indexOf("Query:")+6));
		}
	
		if( theText.indexOf("AjaxDebugMessage:") >= 0 )
		{
			if( document.getElementById("ajaxdebugmsg") )
				document.getElementById("ajaxdebugmsg").innerHTML = lastAjaxRequestUrl + "<br>" + theText.substr(theText.indexOf("AjaxDebugMessage:")+17);
		}
	}
}