///////////////////////////
// Abre o objeto XMLHTTP //
///////////////////////////
function openAjax() {
var Ajax;
try {Ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers mais populares, como: Firefox, Safari, dentre outros.
	}catch(ee) {
	try {Ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
		}catch(e) {
	try {Ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
		}catch(e) {Ajax = false;
		}
	}
}
return Ajax;
} 


function Ajax(url, metodo, modo, tagRetorno, parametros) {
        //document.getElementById(tagRetorno).innerHTML='<div align="center" class="carregando"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="40" align="center" valign="bottom"><img src="Images/Carregando.gif"></td></tr><tr><td height="30" align="center"><strong><font color="#006699" size="2" face="Arial, Helvetica, sans-serif">CARREGANDO</font></strong></td></tr></table></div>'
			
			var xmlhttp = openAjax();
            if(metodo == "GET") {
                xmlhttp.open("GET", url+"?"+parametros+"&rnd"+ Math.random(), modo);
            } else {        
                xmlhttp.open("POST", url, modo);
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
                xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
                xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
                xmlhttp.setRequestHeader("Pragma", "no-cache");
            }    
            
            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4) {
                    retorno=xmlhttp.responseText
					retorno = retorno.replace(/\+/g," ");
					retorno = unescape(retorno);
                    document.getElementById(tagRetorno).innerHTML=retorno;
                }
            }
            if(metodo == "GET") {
                xmlhttp.send(null);
            } else {        
                xmlhttp.send(parametros);
            }
}


// A função abaixo pega a versão mais nova do xmlhttp do IE e verifica se é Firefox. Funciona nos dois.
function createXMLHTTP() {
  try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e) {
	  try {
		  ajax = new ActiveXObject("Msxml2.XMLHTTP");
		  alert(ajax);
	  }
  	  catch(ex) {
		  try {
			  ajax = new XMLHttpRequest();
		  }
		  catch(exc) {
			  alert("Esse browser não tem recursos para uso do Ajax");
			  ajax = null;
		  }
	  }
	  return ajax;
  }
  var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
  for (var i=0; i < arrSignatures.length; i++) {
	try {
		var oRequest = new ActiveXObject(arrSignatures[i]);
		return oRequest;
	} 
	catch (oError) { }
  }           
  throw new Error("MSXML não está instalado em seu sistema.");
}

function Ajax1(Path, Objeto, tipo) {
	var oHTTPRequest = createXMLHTTP();
	var oStringSend = "";
	if (tipo.length == 0)
		tipo = 'get';
	if (tipo.toLowerCase() == 'post') {
		oHTTPRequest.open("post", Path, false);
		oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	} else {
		oHTTPRequest.open("get", Path, false);
	}
  	oHTTPRequest.onreadystatechange=function() {
    	if (oHTTPRequest.readyState==4) {
			Objeto.value = oHTTPRequest.responseText;
			if (typeof(Objeto.onchange) == 'function') {
				Objeto.onchange();
			}
      	}
  	}
	if (typeof(Objeto) == 'object') {
		if (typeof(Objeto.name) == 'string') {
			if (Objeto.name == 'TOculto') {
				for(i=0;i<=Objeto.count()-1;i++) {
					if (oStringSend.length == 0) {
						if ( (Objeto.itens[i].Campo != '') && (Objeto.itens[i].Valor != '') )
							oStringSend = Objeto.itens[i].Campo + '=' + Objeto.itens[i].Valor;
					} else {
						if ( (Objeto.itens[i].Campo != '') && (Objeto.itens[i].Valor != '') )
							oStringSend += '&' + Objeto.itens[i].Campo + '=' + Objeto.itens[i].Valor;
					}
				}
			}
		}
	}
	oHTTPRequest.send(oStringSend);
}