/**
 * AJAX specific methods
 *
 * Depends: DOMHelper.class
 */
function AJAX ()
{
	this.xmlHttpRequest = this.getXMLHTTPObject();
	this.working = false;
	this.method = null;
	this.url = null;
	this.data = null;
	this.assynchronous = true;
	this.listenerObjList = new Array();
}

/**
 * Add a handler for this XmlHttp request
 *
 * @param Object obj		the object whose method will be called
 * @param Function method	the method that will be called
 * @param Number status		(optional) HTTP status code to wait for; defaults to 200
 * @return Boolean			whether the listener was successfully added
 */
AJAX.prototype.addRequestListener = function ( obj, method, status )
{
	if( !status )
	{
		status = 200; // default status for listeners
	}
	
	if( typeof status != 'number' )
	{
		if( ( status = parseInt( status ) ) == NaN )
		{
			return false;
		}
	}

	if( typeof obj != 'object' || typeof method != 'function' )
	{
		return false;
	}
	
	if( !this.listenerObjList[status] )
	{
		this.listenerObjList[status] = new Array();
	}
	
	this.listenerObjList[status].push( new Array( obj, method ) );
	
	return true;
}

AJAX.prototype.submit = function ( formObj )
{
	this.method = formObj.getAttribute("method");
	this.url = formObj.getAttribute("action");
	this.data = oDOMHelper.getFormData( formObj );
	this.open();
}

AJAX.prototype.open = function()
{
	if( !this.working ) 
	{
		var me = this;
		this.xmlHttpRequest.open( this.method, this.url, this.assynchronous );
		this.xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
		this.xmlHttpRequest.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
		this.xmlHttpRequest.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
		this.xmlHttpRequest.setRequestHeader("Pragma", "no-cache");
		this.xmlHttpRequest.onreadystatechange = function () { me.getRequestAnswer() };
		this.xmlHttpRequest.send( this.data+"&isAjaxRequest=1" );
		this.working = true;
	} 
}

AJAX.prototype.getRequestAnswer = function ()
{
	if( this.xmlHttpRequest.readyState == 4 )
	{
		/*
		 * Remove all listeners before processing, to avoid double call.
		 * They need to be cleaned before processing so they don't interfere with recursive listeners.
		 */
		var tempListenerObjList = this.listenerObjList;
		this.listenerObjList = null;
		this.listenerObjList = new Array();

		this.working = false;

		if( tempListenerObjList[this.xmlHttpRequest.status] ) 
		{		
			for( var i = 0; i < tempListenerObjList[this.xmlHttpRequest.status].length; i++ )
			{
				var handler = tempListenerObjList[this.xmlHttpRequest.status][i];
				handler[1].call( handler[0], this.xmlHttpRequest.responseXML, this.xmlHttpRequest.responseText );
			}
		}
		else
		{
			//alert( 'Ocorreu um erro desconhecido: '+this.xmlHttpRequest.status );
		}
	}
}

AJAX.prototype.getXMLHTTPObject = function()
{
	var xmlhttp;
	/*@cc_on
	@if ( @_jscript_version >= 5 )
	try 
	{
		xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
	}
	catch (e)
	{
		try 
		{
			xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	@else
		xmlhttp = false;
	@end @*/
	if ( !xmlhttp && typeof XMLHttpRequest != 'undefined' )
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType( "text/xml" ); 
		}
		catch( e )
		{
			xmlhttp = false; 
		}
	}
	return xmlhttp;
}
