/**
*
* Elaborado por Carlos André Oliveira - caseol@gmail.com
* Casetec Informática Ltda - www.casetec.com.br
*/

/**
* Retorna o objeto xmlHttpRequest
*
* @param toExec É o script que será executado no servidor.
* @param writeOn É o container Html que receberá a resposta do servidor após a execução.
* @param callBack É funcao que deve receber o resultado da requisi&ccedil;&atilde;o após a reposta do servidor.
* @param msgArea É o container Html que recebe mensagens do sistema.
*/
function getXmlHttpRequest(toExec, writeOn, callBack, msgArea) {
	var xmlHttp;
	var execCallBack = eval(callBack);
	
	try {  // Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e){  // Internet Explorer  
		try {    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
  		catch (e){    
			try{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}
    		catch (e){      
				alert("Seu navegador não suporta AJAX!");      
				return false;      
			}
		}  
	}
	xmlHttp.onreadystatechange=function(){
		switch(xmlHttp.readyState){
			case 4:
				if (writeOn != null) document.getElementById(writeOn).innerHTML = xmlHttp.responseText;
				if (callBack != null) execCallBack (4);
				break;
			case 3:
				if (msgArea != null) document.getElementById(msgArea).innerHTML = "Requisição enviada!";
				if (callBack != null) execCallBack (3);
				break;
			case 2:
				if (msgArea != null) document.getElementById(msgArea).innerHTML = "Processando...";
				if (callBack != null) execCallBack (2);
				break;
			case 1:
				if (msgArea != null) document.getElementById(msgArea).innerHTML = "Aguarde...";
				if (callBack != null) execCallBack (1);
				break;
			default:
		}
	}
  	xmlHttp.open("POST",toExec,true);
  	xmlHttp.send(callBack);
	
	return xmlHttp;
}
