/*
//	||-------------||
//	|| Classe Ajax ||
//	||-------------||
//	Autor: Guilherme Policicio Rey
//	Versão: 1.0
//	Data de criação: 05/08/2009
//	Última Modificação: 07/09/2009
//		____________________________________________________________________________________
//		Atributos:
//			- divDisplay: Div aonde será escrito o retorno da página
//			- divLoading: Div aonde será mostrada um html de loading ( se não for setada, a divDisplay é usada )
//			- loadContent: Conteudo da div de loading
//			- xmlHttp: Objeto XmlHttpRequest
//			- errorMsg: Caso a chamada retorne um erro, qual string que representa o erro.
//			- alertErrorMsg: Mensagem de erro a ser mostrada no alert
//		____________________________________________________________________________________
//		Exemplo de utilização:
//			var ajaxObj = new Ajax();
//				ajaxObj.divDisplay = document.getElementById("divMsg");
//				ajaxObj.divLoading = document.getElementById("divLoading");
//				ajaxObj.loadContent = "Carregando...";
//				ajaxObj.errorMsg = "erro";
//				ajaxObj.alertErrorMsg = "Ocorreu um erro na chamada Ajax";
//				ajaxObj.send("fazAlgo.asp?v=1", funcaoParaDispararDepoisDaExecucaoAjax());
*/
function Ajax()
{
	this.divDisplay = null;		
	this.divLoading = null;		
	this.loadContent = null;	
	this.xmlHttp = null;		
	this.errorMsg = null;		
	this.alertErrorMsg = null;	
	this.useDivDisplay = null;
	
	// Váriaveis globais (Necessárias para o funcionamento no IE6)
	var _alertErrorMsg;
	var _errorMsg;
	var _divDisplay;
	var _divLoading;
	var _loadContent;
	var _callBack = null;
	var _xmlHttp = null;
	var _useDivDisplay = null;
	// Setando as váriaveis globais //
	this.setGlobals = function(callBack) {
		_alertErrorMsg = this.alertErrorMsg;
		_errorMsg = this.errorMsg;
		_divDisplay = this.divDisplay;
		_divLoading = this.divLoading;
		_loadContent = this.loadContent;
		_xmlHttp = this.xmlHttp;
		_useDivDisplay = this.useDivDisplay;
		_callBack = callBack != null || callBack != "undefined" ? callBack : null;
	}
	// --- //

	// Setando o objeto xmlHttp
	if(window.XMLHttpRequest)
		this.xmlHttp = new XMLHttpRequest();
	else
		if (window.ActiveXObject)
			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	// --- //
	
	// Setando o evento de state change do ajax
	this.xmlHttp.onreadystatechange = function()
	{
		if(parseInt(_xmlHttp.readyState) == 4)
		{
			if(this.responseText == _errorMsg)
			{
				_divDisplay.innerHTML = _errorMsg;
				return;
			}
			else {
				_divDisplay.innerHTML = _xmlHttp.responseText;
				if( typeof(_divLoading) != undefined && _divLoading != null) {
					_divLoading.innerHTML = "" ;
				}
				if(_callBack != null) _callBack;
			}
		}
		else
		{
			if(typeof(_divLoading == "undefined") || _divLoading == null)
				_divDisplay.innerHTML = _loadContent;
			else
				_divLoading.innerHTML = _loadContent;
		}
	};
	
	this.send = function(url, callBack)	// Método que executa a chamada Ajax
	{	
		this.setGlobals(callBack);
		this.xmlHttp.open("get", url, true);
		this.xmlHttp.send(null);
	};
}
