// JavaScript Document

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);		
		
	}

}