// JavaScript Document



function createXMLHTTP() {

    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            //Do nothing
        }
      }
    }
    throw new Error("XMLHttp object could be created.");
}


function BuscarCEP()
{
	if(document.formCadastro.cep.value == "")
	{
		alert("Informe um CEP para consulta !");
		document.formCadastro.cep.focus();
	}
	else
	{
		SleepButton(document.formCadastro.BTCep, false, "Aguarde, pesquisando cep . . .");
		
		var oHTTPRequest2 = createXMLHTTP(); 
		oHTTPRequest2.open("post", "Execs/BuscaCEP.asp", true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
		oHTTPRequest2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oHTTPRequest2.onreadystatechange = function()
		{
			if (oHTTPRequest2.readyState == 4)
			{	
				
				SleepButton(document.formCadastro.BTCep, true, "Buscar");
				
				if(oHTTPRequest2.responseText == "")
				{
					alert("CEP não econtrado !");
					document.formCadastro.cep.select();				
				}
				else
				{
					var iDados = new Array();
					iDados = oHTTPRequest2.responseText.split("|");
					
					if(iDados.length > 0)
					{			
						document.formCadastro.rua.value = iDados[0].toString();
						document.formCadastro.bairro.value = iDados[1].toString();
						document.formCadastro.cidade.value = iDados[2].toString();
						document.formCadastro.estado.value = iDados[3].toString();	
						
						if(document.formCadastro.unidade != null)
						{
							document.formCadastro.unidade.value = iDados[4].toString();	
						}
						
						document.formCadastro.numero.focus();				
					}
					else
					{
						alert(oHTTPRequest2.responseText);
					}
					
				}
								
			}
		}
		
		var strVariaveisForm = "cep=" + document.formCadastro.cep.value;	
		oHTTPRequest2.send(strVariaveisForm);		
		
	}

}

function BuscarCEP2()
{
	if(document.formCadastro.cep.value == "")
	{
		alert("Informe um CEP para consulta !");
		document.formCadastro.cep.focus();
	}
	else
	{
		//SleepButton(document.formCadastro.BTCep, false, "Aguarde, pesquisando cep . . .");
		
		var oHTTPRequest2 = createXMLHTTP(); 
		oHTTPRequest2.open("post", "../Execs/BuscaCEP.asp", true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
		oHTTPRequest2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oHTTPRequest2.onreadystatechange = function()
		{
			if (oHTTPRequest2.readyState == 4)
			{	
				
				//SleepButton(document.formCadastro.BTCep, true, "Buscar");
				
				if(oHTTPRequest2.responseText == "")
				{
					alert("CEP não encontrado !");
					document.formCadastro.cep.select();			
				}
				else
				{
					var iDados = new Array();
					iDados = oHTTPRequest2.responseText.split("|");
					if(iDados.length > 0)
					{		
						document.formCadastro.cidade.value = iDados[2].toString();
						document.formCadastro.estado.value = iDados[3].toString();		
						if(document.formCadastro.unidade != null)
						{
							document.formCadastro.unidade.value = iDados[4].toString();	
						}
						
					}
					else
					{
						alert(oHTTPRequest2.responseText);
					}
					
				}
								
			}
		}
		
		var strVariaveisForm = "cep=" + document.formCadastro.cep.value;	
		oHTTPRequest2.send(strVariaveisForm);		
		
	}

}
