﻿///<summary>
///Creates an instance of an Ajax object which allows asynchornous communication with the server from client script.
///</summary>
///<param name="url">The url to whcih the request is made.</param>
///<param name="params">An array containing the parameters to pass with the request.</param>
///<param name="callback">A pointer to a function to handle the response of the ajax request.</param>
function Ajax( url, params, callback )
{
	///<summary>
	///Stores the value of the url to make the ajax call to.
	///</summary>
	this._url = url;
	
	///<summary>
	///Stores an array of parameters to pass to the ajax url.
	///</summary>
	this._params = params;
	
	///<summary>
	///Stores a reference to the xml http object for the browser.
	///</summary>
	this._ajax = null;
	
	///<summary>
	///Stores a reference to the callback function.
	///</summary>
	this._callback = callback;
	
	///<summary>
	///Stores the value returned by the ajax request.
	///</summary>
	this._result = "";
	
	//Get a reference to the xml http object for the specific browser.
	try
	{
		this._ajax = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			this._ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				this._ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser is outdated and does not support Ajax.");
				return false;
			}
		}
	}
	
	//Add a reference to this object to the xml http object, and set the OnReadyStateChanged event handler.
	this._ajax.parent = this;
	this._ajax.onreadystatechange = _OnReadyStateChanged;
	
	//Set the function to perform the ajax GET request.
	this.Get = _Get;
	
	//Set the function to perform the ajax POST request.
	this.Post = _Post;
}


///<summary>
///Handles the OnReadyStateChanged event of the xml http object.
///</summary>
function _OnReadyStateChanged()
{
	try
	{
		if( this.readyState == 4 )
		{
			var success = this.responseXML.getElementsByTagName("success")[0].firstChild.nodeValue
			var result = this.responseXML.getElementsByTagName("result")[0].firstChild.nodeValue
			this.parent._result = result;
			this.parent._callback(this.parent._result);
		}
	}
	catch(e)
	{
		alert(e);
	}
}


///<summary>
///Performs the ajax GET request.
///</summary>
function _Get()
{
	var url=this._url;
	if(this._params.length&&(this._params.length>0))
	{
		url+="?"
		for(var i=0;i<this._params.length;i++)
		{
			url+="p"+i+"="+this._params[i]+"&";
		}
		url+="sid="+Math.random();
	}
	else
	{
		url+="?sid="+Math.random();
	}
	this._ajax.open( "GET", url, true );
	this._ajax.send( null );
}


///<summary>
///Performs the ajax POST request.
///</summary>
function _Post()
{
	var params = "";
	if(this._params.length&&(this._params.length>0))
	{
		for(var i=0;i<this._params.length;i++)
		{
			params+="p"+i+"="+escape(this._params[i])+"&";
		}
		params+="sid="+Math.random();
	}
	else
	{
		params+="sid="+Math.random();
	}
	this._ajax.open( "POST", this._url, true );
	this._ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	this._ajax.setRequestHeader("Content-length", params.length);
	this._ajax.setRequestHeader("Connection", "close");
	this._ajax.send( params );
}

