$ = function(){return document.getElementById(arguments[0]);}

conexaoAjax = function() {
	var objAjax;
	var metodo;
	var url;
	var camposFormulario;
	var txtRetorno;
	var xmlRetorno;
	var objThis = this;
	var noXml;


	this.getTxtRetorno = function() {
		return txtRetorno;
	}
	
	this.getXmlRetorno = function() {
		try { 
			return xmlRetorno.getElementsByTagName(noXml)[arguments[0]].firstChild.data; 
		} catch(e) { 
			return null; 
		}
	}	

	this.getCamposFormulario = function() {
		return camposFormulario;
	}

	this.getMetodo = function() {
		return metodo;
	}
	
	this.setXmlRetorno = function() {
		noXml = arguments[0];
	}

	this.setTxtRetorno = function() {
		txtRetorno = arguments[0];
	}

	this.setUrl = function() {
		url = arguments[0];
	}
	
	this.requisicaoCompleta = function() {}	
	
	this.setMetodo = function() {
		switch(arguments[0].toUpperCase()) {
			case 'GET':
				metodo = 'GET';
			break;
			
			case 'POST':
				metodo = 'POST';
			break;
		}
	}


	this.criar = function() {
		if (window.ActiveXObject) {
			try {
				objAjax = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			} catch(e) {
				try {
					objAjax = new ActiveXObject("Msxml2.XMLHTTP.3.0");
				} catch(e) {
					try {
						objAjax = new ActiveXObject("Msxml2.XMLHTTP");
					} catch(e) {
						try {
							objAjax = new ActiveXObject("Microsoft.XMLHTTP");
						} catch(e) {
							objAjax = null;
						}
					}
				}
			}
		} else if (window.XMLHttpRequest) {
			objAjax = new XMLHttpRequest();
		}
	}	


	this.setCampo = function() {
		if (camposFormulario > '')
			camposFormulario += '&' + arguments[0] + "=" + encodeURIComponent(arguments[1]);
		else
			camposFormulario = arguments[0] + "=" + encodeURIComponent(arguments[1]);
	}
		
	this.executar = function() {
		objAjax.open(metodo, url, true);		
		objAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		objAjax.setRequestHeader("CharSet", "UTF-8");
		objAjax.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
		objAjax.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
		objAjax.setRequestHeader("Pragma", "no-cache");		
		
		objAjax.onreadystatechange = function() {			
			if(objAjax.readyState == 4) {				
				if(objAjax.status == 200) {
					txtRetorno = objAjax.responseText;
					xmlRetorno = objAjax.responseXML;
					objThis.requisicaoCompleta();
					//txtRetorno = objAjax.responseText;
					//callback = callback || function(){};
					//return txtRetorno;
					
				}else{
					alert('Falha na comunicação');
				}
			}
		}
		
		if (this.getMetodo() != 'GET')
			objAjax.send(this.getCamposFormulario());
		else
			objAjax.send(null);
		
	}
}
