
function retorneResposta(
    necessitaResposta, 
    tipoDadosResposta, 
    modoResposta, 
    idDestinoResposta, 
    exibirCarregando, 
    idDestinoCarregando, 
    conteudoCarregando, 
    tratarErro, 
    idDestinoMensagemErro, 
    metodoRequisicao, 
    urlRequisicao, 
    estadoAssincrono, 
    dadosEnviar) {

	focarInput = modoResposta[2];
	limparInput = modoResposta[1];
	modoResposta = modoResposta[0];
    
    //alert("arguments: "+arguments[0]+","+arguments[1]+","+arguments[2]+","+arguments[3]+","+arguments[4]+","+arguments[5]+","+arguments[6]+","+arguments[7]+","+arguments[8]+","+arguments[9]+","+arguments[10]+","+arguments[11]+","+arguments[12]);

	var xmlHttp;

	var tipoErro;

	tipoErro = 0;
	xmlHttp = false;

	try {
		// Firefox, Opera 8.0+, Safari, Chrome
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			// Versão 6
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				// Versão 5.5
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				tipoErro = 1; // O navegador não suporta AJAX!
			}
		}
	}

	if (xmlHttp.overrideMimeType) {
		xmlHttp.overrideMimeType('text/xml');
	}

	if (!xmlHttp) {
		tipoErro = 1; // O navegador não suporta AJAX!
	}

	if (tratarErro) {
		if (modoResposta == "innerHTML") {
			window.document.getElementById(idDestinoMensagemErro).innerHTML = tipoErro;
            return false;
		}
		if (modoResposta == "alert") {
			if (tipoErro != 0) {
				window.alert(tipoErro);
				if (limparInput) {
					window.document.getElementById(idDestinoMensagemErro).value = "";
				}
				if (focarInput) {
					setTimeout('',250);
					window.document.getElementById(idDestinoMensagemErro).focus();
				}
				return false;
			}
		}
	}

	function receberResposta() {
        //alert("Fazendo requisição!");
		if (xmlHttp.readyState < 4) {
			if (exibirCarregando) {
				var respostaRequisicao = conteudoCarregando;
				if (modoResposta == "innerHTML") {
					if (respostaRequisicao != "") {
						window.document.getElementById(idDestinoCarregando).innerHTML = respostaRequisicao;
					} else {
						return false;
					}
				}
				if (modoResposta == "alert") {
					if (respostaRequisicao != "") {
						window.alert(respostaRequisicao);
						if (limparInput) {
							window.document.getElementById(idDestinoCarregando).value = "";
						}
						if (focarInput) {
							setTimeout('',250);
							window.document.getElementById(idDestinoCarregando).focus();
						}
					} else {
						return false;
					}
				}
			}
		}
		if (xmlHttp.readyState == 4) {
			if (necessitaResposta) {
				var respostaRequisicao = "";
				if (tipoDadosResposta == "texto") {
					respostaRequisicao = xmlHttp.responseText;
				}
				if (tipoDadosResposta == "xml") {
					respostaRequisicao = xmlHttp.responseXml;
				}
                //alert("readyState == 4");
				//alert("'" + respostaRequisicao + "'");
				if (modoResposta == "innerHTML") {
					if (respostaRequisicao != "") {
						window.document.getElementById(idDestinoResposta).innerHTML = "";
						window.document.getElementById(idDestinoResposta).innerHTML = respostaRequisicao;
						//alert("'" + window.document.getElementById(idDestinoResposta).innerHTML + "'");
                        //window.document.getElementById(idDestinoMensagemErro).innerHTML = respostaRequisicao;
					} else {
						return false;
					}
				}
				if (modoResposta == "alert") {
					if (respostaRequisicao != "") {
						window.alert(respostaRequisicao);
						if (limparInput) {
							window.document.getElementById(idDestinoResposta).value = "";
						}
						if (focarInput) {
							setTimeout('',250);
							window.document.getElementById(idDestinoResposta).focus();
						}
					} else {
						return false;
					}
				}
			}
		}
	}

	xmlHttp.onreadystatechange = receberResposta;

	xmlHttp.open(metodoRequisicao, urlRequisicao, estadoAssincrono);

	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlHttp.send(dadosEnviar);

}



/*
// Exemplo de invocação.

necessitaResposta = true; // ou false para receber a resposta.
tipoDadosResposta = "texto"; // ou "xml"
modoResposta = ["innerHTML"]; // ou "alert".
idDestinoResposta = "idDestinoResposta";
exibirCarregando = false; // ou true para exibir o carregando.
idDestinoCarregando = "idDestinoCarregando";
conteudoCarregando = "Conteúdo Carregando..."; // Conteúdo com ou sem HTML.
tratarErro = false; // ou true para tratar erro.
idDestinoMensagemErro = "idDestinoMensagemErro";
metodoRequisicao = "GET"; // ou "POST". Utilizar apenas GET.
urlRequisicao = "pagina.php?parametros"; // Para GET.
estadoAssincrono = true; // Utilizar apenas true.
dadosEnviar = null; // Utilizar apenas null.

retorneResposta(necessitaResposta, tipoDadosResposta, modoResposta, idDestinoResposta, exibirCarregando, idDestinoCarregando, conteudoCarregando, tratarErro, idDestinoMensagemErro, metodoRequisicao, urlRequisicao, estadoAssincrono, dadosEnviar);
*/
