///////////////////
// AJAX FUNCTION //
///////////////////
function Ajax()
{
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'text';
	this.mimeType = null;

	/////////////////////////////
	// INITIALIZATION FUNCTION //
	/////////////////////////////
	this.init = function()
	{
		if (!this.req)
		{
			try
			{
				this.req = new XMLHttpRequest(); // Firefox, Safari, IE7
			}
			catch (e)
			{
				try
				{
					this.req = new ActiveXObject('MSXML2.XMLHTTP'); // Later versions of IE
				}
				catch (e)
				{
					try
					{
						this.req = new ActiveXObject('Microsoft.XMLHTTP'); // Early versions of IE
					}
					catch (e)
					{
						// Error
						return false;
					}
				}
			}
		}
		return this.req;
	};

	//////////////////////
	// REQUEST FUNCTION //
	//////////////////////
	this.doReq = function()
	{
		if (!this.init())
		{
			alert('Browser Error: Could not create XMLHttpRequest object.');
			alert('You should use the old version of the Edit News page.');
			return;
		}
		this.req.open(this.method, this.url, this.async);
		if (this.mimeType)
		{
			try
			{
				req.overrideMimeType(this.mimeType);
			}
			catch (e)
			{
				// Could not override MIME type - does not work in IE or Opera 8
			}
		}
		if (this.method == "POST")
		{
			this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		var self = this; // Loss of scope
		this.req.onreadystatechange = function()
		{
			var resp = null;
			if (self.req.readyState == 4)
			{
				// Good to go - handle response
				switch (self.responseFormat)
				{
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}
				if (self.req.status >= 200 && self.req.status <= 299)
				{
					self.handleResp(resp);
				}
				else
				{
					self.handleError(resp);
				}
			}
		};
		this.req.send(this.postData);
	};

	////////////////////////////////
	// MIME TYPE SETTING FUNCTION //
	////////////////////////////////
	this.setMimeType = function(mimeType)
	{
		this.mimeType = mimeType;
	};

	/////////////////////////////
	// ERROR HANDLING FUNCTION //
	/////////////////////////////
	this.handleError = function()
	{
		alert('Server Error: Could not successfully complete request.\n\n'
		+ 'Status Code: ' + this.req.status + '\n'
		+ 'Status Description: ' + this.req.statusText);
	};

	////////////////////
	// ABORT FUNCTION //
	////////////////////
	this.abort = function()
	{
		if (this.req)
		{
			this.req.onreadystatechange = function() { };
			this.req.abort();
			this.req = null;
		}
	};

	//////////////////
	// GET FUNCTION //
	//////////////////
	this.doGet = function(url, handle, format)
	{
		this.url= url;
		this.handleResp = handle;
		this.responseFormat = format || 'text';
		this.doReq();
	};

	///////////////////
	// POST FUNCTION //
	///////////////////
	this.doPost = function(url, postData, handle, format)
	{
		this.url = url;
		this.handleResp = handle;
		this.responseFormat = format || 'text';
		this.method = 'POST';
		this.postData = postData;
		this.doReq();
	};
	this.setHandlerResp = function(funcRef) {
		this.handleResp = funcRef;
	};
}