/**
 * @file	:	ajax.class.js
 * @desc	:	AJAX interface, methods to perform all AJAX-related function
 * @lmod	:	29.12.2005
 *
 * @changelog:
 * -- 29.12.2005:
 * -- * initialize Ajax object on every request (not only at the first one)
 */

var Ajax =
{

/**
 * @desc: Initialize XMLHTTPRequest object
 *
 */
	init: function()
	{
		try
		{
			this.xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			try
			{
				this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					return false;
				}
			}
		}
		
		this.onUnitialized = null;
		this.onLoading = null;
		this.onLoaded = null;
		this.onInteractive = null;
		this.onComplete = null;
	},
	
/**
 * @desc						:	Request data
 * @param	uri		:	string	:	URI
 * @param	method	:	string	:	request method ('get' || 'post')
 * @param	args	:	string	:	serialized string of request arguments (param&param&...)
 * @param	triggers:	array	:	event to function bindings
 * return			:	boolean	:	status
 */
	
	request: function(uri, method, args, triggers)
	{
		// for now() initialize ajax object on every request
		// if (!this.xmlhttp) this.init();
		this.init();
			
		this.states = [ 'Unitialized', 'Loading', 'Loaded', 'Interactive', 'Complete' ];
		
		for(i=0; i < triggers.length; i=(i+2))
		{
			eval('this.on'+triggers[i]+' = '+triggers[i+1]);
		}
		
		this.xmlhttp.onreadystatechange = this.stateChange.bind(this);
		
		// check method, send data
		if(method == 'get')
		{
			this.xmlhttp.open('GET', uri+'?'+args, true);
			this.xmlhttp.send(null);
		}
		else if(method == 'post')
		{
			this.xmlhttp.open('POST', uri, true);
			this.xmlhttp.setRequestHeader('X-AJAX-Application', 'true');
			this.xmlhttp.setRequestHeader('Method', 'POST '+uri+' HTTP/1.1');
			this.xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			this.xmlhttp.setRequestHeader('Content-length', args.length);
            
			/* Force "Connection: close" for Mozilla browsers to work around
			 * a bug where XMLHttpRequest sends an incorrect Content-length
			 * header. See Mozilla Bugzilla #246651. 
			 */
			if (this.xmlhttp.overrideMimeType) this.xmlhttp.setRequestHeader('Connection', 'close');
			
			this.xmlhttp.send(args);
		}
		else
		{
			window.alert('AJAX Request error');
			return false;
		}
		
		return true;
	},
	
/**
 * @desc:	Handle state changes, Capture event and redirect to the handleState
 *
 */
	stateChange: function()
	{
		if(this.xmlhttp.readyState != 0)
		{
			this.handleState(this.xmlhttp.readyState);
		}
	},
		
/*
 * @desc							:	Perform functions, based on the current connection state
 * @param	state	:	integer		:	connection state code
 * return			:	boolean		:	status
 */
	handleState: function(state)
	{
		// check if we're in the 'Complete' state
		// and do a status check if we are
		if (this.states[state] == 'Complete')
		{
			if (this.xmlhttp.status != '200')
			{
				// call onError, our xmlhttp.status error handler
				if (this.onError)
				{
					this.onError(this.xmlhttp);
					return false;
				}
			}
		}
		
		// function to call
		var _func = eval('this.on'+this.states[state]);
		
		// check if function exists and if it does, call it with this.xmlhttp as an argument
		if(_func)
		{
			_func(this.xmlhttp);
		}
			
		return true;
	},
	
/*
 * @desc					:	Handle HTTP status errors, not implemented yet
 * @param	obj	:	object	:	the xmlhttp object
 * @return		:	boolean	:	true (just for the sport :)
 */
	onError: function(obj)
	{
		// do nothing
		alert('AJAX: There was an error!');
		return true;
	}
}

