// Construtor
function Ajax()
{
	this.xmlHttp = null;
	this.onreadystatechange = null;
	
	this.div = null;
	this.divID = 'my_div_top_aja';
	
	this.linha = -1;
	this.lista = new Array();
	
	this.ocupado = 0;
	this.tempo = null;
	
	// Carregando o Objeto xmlHttp.
	this.iniciar();
	
	// ...
	window._ajax = this;
}

Ajax.prototype.iniciar = function()
{
	try
	{
		this.xmlHttp = new XMLHttpRequest();
	}
	catch( e )
	{
		try
		{
			this.xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch( ex )
		{
			this.xmlHttp = null;
		}
	}
};

Ajax.prototype.requisitar = function( iUrl, metodo, data )
{
	metodo = ( metodo == 'POST' ) ? 'POST' : 'GET';
	
	iUrl = this.fixUrl( iUrl );
	iUrl = this.noCache( iUrl );
	
	if ( this.ocupado != 0 )
	{
		this.lista.push( [ iUrl, metodo, data ] );
	}
	
	this.ocupado = 1;
	this.showMensagem();
	
	var objA = this;
	var obj = this.xmlHttp;
	var funcao = null;
	
	if ( this.onreadystatechange )
	{
		funcao = function() { objA.onreadystatechange(); objA.processar(); };
	}
	else
	{
		funcao = function() { objA.processar(); };
	}
	
	obj.open( metodo, iUrl, true );
	obj.onreadystatechange = funcao;
	
	if ( metodo == 'GET' )
	{
		obj.send( null );
	}
	else
	{
		if ( typeof( obj.setRequestHeader ) != 'undefined' )
		{
			obj.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded;' );
		}
		
		// Hum...
		data = this.montaPost( data );
		
		obj.send( data );
	}
};

Ajax.prototype.processar = function()
{
	var a = this.lista;
	
	if ( this.linha != -1 )
		this.lista = a.splice( 1, a.length );
	
	this.ocupado = 0;
	this.esconder();
	
	if ( this.lista.length > 0 )
	{
		if ( this.linha != 0 )
			this.linha = 0;
		
		setTimeout( 'window._ajax.requisitar("'+ this.lista[ 0 ] +'","'+ this.lista[ 1 ] +'","'+ this.lista[ 2 ] +'");', 20 );
	}
};

Ajax.prototype.montaPost = function( a )
{
	var c  = '';
	
	if ( ! a )
	{
		return;
	}
	
	if ( typeof( a ) == 'object' )
	{
		for ( var i in a )
		{
			c += '&'+ i +'='+ escape( a[ i ] );
		}
	}
	
	return c;
};

Ajax.prototype.noCache = function( iurl )
{
	if ( iurl.indexOf( '__i=' ) != -1 )
	{
		return;
	}
	
	var i = ( iurl.indexOf( '?' ) != -1 ) ? '&' : '?';
	var a = iurl + i + '__i='+ ( new Date() ).getTime();
	
	return a;
};

Ajax.prototype.fixUrl = function( s )
{
	var d = window.location.toString().split( '/' );
	var d = d[ 2 ];
	
	var d2 = _url.toString().split( '/' );
	var d2 = d2[ 2 ];
	
	return s.replace( new RegExp( '^http://'+ d2 ), 'http://'+ d );
};

/*

	Exibe uma mensagem de "carregando" no topo do site.

*/

Ajax.prototype.showMensagem = function()
{

};

Ajax.prototype.esconder = function()
{

};

