/* ********************

This is the AJAX object.  Multiple AJAX calls can be made at once and they will not interfere with each other (since they are objects).

@URL			- The script on the server to call (e.g. get_user.php)
@Parameters		- Parameters to pass into the script (e.g. firstname=john&lastname=doe)
@CallBackFunction	- The function that gets called after the AJAX is done.  The "text" and "XML" responses are sent too.


Sample call:

function AjaxGetUser(ID)
{
	var MyAjax = new AjaxConnection("GetUser.php", "GetUser=1&UserID=" + ID, "AfterGetUser"); 
}


Sample return:

function AfterGetUser(xml, text)
{
	//xml comes in like this:
	//<files>
	//	<file>cat.html</file>
	//	<file>dog.html</file>
	//	<file>bird.html</file>
	//</files>

	alert(xml.getElementsByTagName("files").length);
	alert(xml.getElementsByTagName("files")[0].childNodes.length);

	for (i = 0; i < xml.getElementsByTagName("files")[0].childNodes.length; i++)
	{
		alert(i + ": " + xml.getElementsByTagName("files")[0].childNodes[i].childNodes[0].nodeValue);
	}

	alert("User length: " + xml.getElementsByTagName("User").length);
	alert("ContactInfo length: " + xml.getElementsByTagName("ContactInfo").length);

	alert("user_id: " + xml.getElementsByTagName("ContactInfo")[0].childNodes[0].childNodes[0].nodeValue);

	alert("xml: " + xml);
	alert("text: " + text);


	for (i = 0; i < xml.getElementsByTagName("User")[0].childNodes.length; i++)
	{
		alert(i + ": " + xml.getElementsByTagName("User")[0].childNodes[i].childNodes[0].nodeValue);
	}
}
******************** */


function AjaxConnection(URL, Parameters, CallBackFunction) 
{
	this.Parameters = Parameters;
	this.connect = connect;
	this.uri = URL;
	
	//Run
	this.connect(CallBackFunction);
} 

function connect(return_func)
{
	with(this)
	{
		var y = init_object();
		y.open("POST", uri, true);
		y.onreadystatechange =	function()
					{
						if (y.readyState != 4)
							return;
						eval(return_func + '(y.responseXML, y.responseText)');
						delete y;
					}
		y.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		y.send(Parameters);
	}
}


function init_object()
{
	var x;
	
	if(typeof XMLHttpRequest != "undefined")
		x = new XMLHttpRequest();
	else
	{
		try
		{
			x = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				x = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (oc)
			{
				x = null;
			}
		}
	}
	
	if (x)
		return x;

}

