function InicializarAjax()
{
	var ajax = null;
	try
	{	 
		ajax = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari	
	}
	catch(e)
	{	
		try
		{		 
			ajax = new ActiveXObject("Microsoft.XMLHTTP");	  
		}
		catch(e)
		{
			try
			{	 
				ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
			}
			catch(e)
			{	 
				alert("Seu browser não suporta AJAX.");	
				ajax = false;	
			}	 
		}
	}
	
	return ajax;	 
}

function Ajax(url, method, data) {
	this.clientHttpHandler = "";
	this.httpMethod = method || "GET"; 	// GET ou POST
	this.serverUrl = url || ""; 		// URL com ou sem parametros EX: http://www.hbsis.com/?id=1
	this.isAsync = true;				// Chamada assincrona ou sincrona
	this.data = data || "";				// Dados formatados para envio via POST
	this.callback = "";					// Método a ser chamado após a requisição 
	
	this.create = function() {
		var xmlHttpRequest = false;
		//Internet Explorer
		try{
			xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (xml2Exception){
			try{
				xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (xmlException){
				xmlHttpRequest = false;
			}
		}	
		//Netscape, Mozila, Firefox, Safari, Opera
		if (!xmlHttpRequest && typeof XMLHTTPRequest == "undefined"){
			try{
				xmlHttpRequest = new XMLHttpRequest();
			}catch (genException){
				XMLHttpRequest = false;
			}		
		}		
		return xmlHttpRequest;
	};

	/*
	 * readystate: 0 – uninitialized, 1 – loading, 2 – loaded, 3 – interactive, 4 – complete
	 */
	this.receive = function() {
		var status = null;
		try{
			if (this.clientHttpHandler.readyState == 4){
				status = this.clientHttpHandler.status;
				if (status == 200 && this.callback){
					this.callback(this.clientHttpHandler.responseText, this.clientHttpHandler.responseXML);
				}
			}
		}catch (genException){
			alert("Erro ao processar a requisição.\nStatus retornado: " + status + "\nMensagem: " + genException.message);
		}
	};
	
	this.getText = function() {
		if (this.clientHttpHandler)
			return this.clientHttpHandler.responseText;
		return "";
	};
	
	this.getXml = function() {
		if (this.clientHttpHandler)
			return this.clientHttpHandler.responseXML;
		return "";
	}
	
	this.send = function () {
	
		if(!this.clientHttpHandler) {
			this.clientHttpHandler = this.create();
		}
	
		if(this.serverUrl != "") {
			var _this = this; // Garante o escopo
			this.clientHttpHandler.open(this.httpMethod, this.serverUrl, this.isAsync);
			this.clientHttpHandler.onreadystatechange = function() { _this.receive() };
			this.clientHttpHandler.send(data);
		}
	};
}

