<!--

/* ----------------------------------------------- */
// Initiate Ajax Handler
/* ----------------------------------------------- */
ajax_request = function(_async)
{
	this.async        = _async;
	this.handler      = null;
	this.responseText = '';
	this.loader	  = null;

	/* ----------------------------------------------- */
	// Create loading message
	//
	// @param	string		message
	/* ----------------------------------------------- */
	this.displayLoader = function(loaderid, loadmessage)
	{
		if ( !loadmessage )
		{
			loadmessage = 'Loading...';
		}

		this.loader = fetch_object(loaderid);
		fetch_object(loaderid).innerHTML = '<img src="images/ajax-loader.gif" alt="Ajax Loader" title="Loading..." /> ' + loadmessage;
		fetch_object(loaderid).style.display = '';
	}

	/* ----------------------------------------------- */
	// Close loader
	/* ----------------------------------------------- */
	this.closeLoader = function()
	{
		this.loader.style.display = 'none';
	}

	/* ----------------------------------------------- */
	// Display error message
	/* ----------------------------------------------- */
	this.showError = function(errmessage)
	{
		this.closeLoader();

		fetch_object('AjaxErrorMessage').innerHTML = errmessage;
		fetch_object('AjaxError').style.display = '';
	}

	/* ----------------------------------------------- */
	// Close error message
	/* ----------------------------------------------- */
	this.closeError = function(errmessage)
	{
		fetch_object('ajaxError').style.display = 'none';
	}

	/* ----------------------------------------------- */
	// Create XMLHTTPRequest Handler
	/* ----------------------------------------------- */
	this.createhandler = function()
	{
		try
		{
			this.handler = new XMLHttpRequest
			return this.handler.setRequestHeader ? true : false
		}
		catch(e)
		{
			try
			{
				this.handler = new ActiveXObject('Microsoft.XMLHTTP')
				return true
			}
			catch (e)
			{
				return false
			}
		}

		return false
	}

	/* ----------------------------------------------- */
	// Response Handler
	//
	// @param	string		function name
	/* ----------------------------------------------- */
	this.onreadystatechange = function(_func)
	{
		if (this.handler == null)
		{
			if (!this.createhandler())
			{
				return false
			}		
		}

		this.handler.onreadystatechange = _func
		return true
	}

	/* ----------------------------------------------- */
	// Encode Datastream
	//
	// @param	string		datastream to encode
	/* ----------------------------------------------- */
	this.encode_datastream = function(_datastream)
	{
		_datastream_arr = _datastream.split('&')
		_datastream = '';

		for (key in _datastream_arr)
		{
			if (_datastream_arr[key].match(/\=/))
			{
				bits = _datastream_arr[key].split('=')


				if (bits.length > 2)
				{
					for (i=2;i<bits.length;i++)
					{
						bits[1] += '$#61;' + bits[i]
					}
				}

				_datastream_arr[key] = bits[0] + '=' + encodeURIComponent(bits[1])
			}

			_datastream += _datastream_arr[key] + '&'
		}

		_datastream = _datastream.substring(0,_datastream.length-1)
		return _datastream
	}

	/* ----------------------------------------------- */
	// Send to server
	//
	// @param	string		datastream
	// @param	string		POST or GET
	// @param	string		filename
	/* ----------------------------------------------- */	
	this.send = function(_datastream, _type, _file)
	{
		if (!this.handler.readyState && !this.handler.readyState < 4)
		{
			_datastream = this.encode_datastream(_datastream)

			if (_file.split("?").length == 2) 
			{
				_file = _file + '&usesAjax=1&nocache=' + (5 * Math.random()*1.33) + ((_type == 'GET') ? '&' + _datastream : '');
			}
			else
			{
				_file = _file + '?usesAjax=1&nocache=' + (5 * Math.random()*1.33) + ((_type == 'GET') ? '&' + _datastream : '');
			}

			this.handler.open(_type, _file, this.async)

			if (_type == 'POST') 
			{
				this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset:ISO-8859-1')
			}

			this.handler.send(_datastream)
		}
	}

	/* ----------------------------------------------- */
	// Check response is ready and is okay
	/* ----------------------------------------------- */
	this.ready_and_okay = function()
	{
		if ( this.handler.readyState == 4 && this.handler.status == 200 )
		{
			this.responseText = this.handler.responseText;
			this.responseText = this.responseText.replace(/\$#61;/g, '=');

			if ( this.responseText.indexOf('error:') == 0 )
			{
				var errmessage = this.responseText.substr(6);
				this.showError(errmessage);
				return true;
			}
			
			if ( this.responseText.indexOf('redirect:') == 0 )
			{
				var redirect = this.responseText.substr(9);
				window.location = redirect;
				return true;
			}

			return true;
		}		

		return false;
	}

}

-->