/*****************************  XmlHttpRequest  *****************************

Author:	        Luke Breuer, labreuer+xmlhttprequest@gmail.com
Created:        4/19/05
Modified:       11/14/06
Documentation:  http://luke.breuer.com/xmlhttprequest.aspx

The purpose of this object is to send requests to webservers and receive 
responses all in javascript.  This translates into asynchronus communication;
where web browsing is usually do-stuff, submit the webpage, get a new one,
do more stuff, etc.

If you use this code without significantly modifying it, I require that you
maintain a link to http://luke.breuer.com/xmlhttprequest.aspx in this
file and that you include my name and contact information in this file.

Note that this implementation only supports using one XmlHttpRequest object
at a time; this is to keep complexity to a minimum.
*/

var callbackFunction = null
var http;

/*
This is our event handler for response to our requests.  It simply calls the callback
  function when data is received and complete:
  
Object status:
  0 = uninitialized
  1 = loading
  2 = loaded
  3 = interactive
  4 = complete
*/
function handleHttpResponse(element)
{
	if (http.readyState == 4 && callbackFunction != null)
		callbackFunction(http.responseText);
}

/*
Here is where you send a request -- either to the current page (in which 
  case you can simply put in the querystring, with no question mark),
  or to a different page.
  
Usually you want your webserver to process pages sent by XmlHttpRequest
  objects differently than normal requests.  (You would do this by recognizing
  certain variables in the get or post data.)
  
This script is asynchronous -- it continues execution immediately after sending
  a request to the webserver.
  
Note that you can do more than just send(null) -- I just don't use more than
  that functionality.  Neither should you, until you get the basic stuff working.
*/
function sendQuerystring(querystringOrUrl) 
{
	http.abort();

	http.onreadystatechange = handleHttpResponse; 
	http.open("GET", (querystringOrUrl.indexOf('?') < 0 ? '?' : '') + querystringOrUrl, true); 
	http.send(null);
}

/*
We need this function because of browser nonconformity. 
All you really have to know is that if it returns null, you're screwed.
  (i.e. you can't do cool XmlHttpRequest stuff, but perhaps IFrames will work)
*/
function getXmlHttpObject() 
{
	var o;
	
	if (window.XMLHttpRequest !== undefined)
		return new XMLHttpRequest();
	
	try
	{
		return new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			return null;
		}
	}
	
	return null;
}

/*
Pass the function that should be called when data is received from the server.
  This function should accept a string, which will be whatever the webserver spits out.
Returns true if the XmlHttpRequest object was created successfully -- if it wasn't,
  then you aren't going to be doing any XmlHttpRequest'ing.
*/
function initXmlHttpRequest(callback)
{
	callbackFunction = callback;
	http = getXmlHttpObject();
	
	return http != null;
}