// JavaScript Document
try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function formataCep(id, tipo){

	cep = document.getElementById(id).value;
	document.getElementById(tipo+"cep").value = "Buscando ...";

	url = "busca_cep.php?cep="+cep;
	xmlhttp.open("GET", url, true);

	xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState==4){

			valores = xmlhttp.responseText.split(";");

			if(valores[0] != 0){

				document.getElementById(tipo+"cep").value = cep;
				document.getElementById(tipo+"endereco").value = valores[5]+" "+valores[6];
				document.getElementById(tipo+"bairro").value = valores[4];

				switch(valores[2]){

					case "AC" : document.getElementById(tipo+"estado").value = "1"; 	break;
					case "AL" : document.getElementById(tipo+"estado").value = "2"; 	break;
					case "AM" : document.getElementById(tipo+"estado").value = "4"; 	break;
					case "AP" : document.getElementById(tipo+"estado").value = "3"; 	break;
					case "BA" : document.getElementById(tipo+"estado").value = "5"; 	break;

					case "CE" : document.getElementById(tipo+"estado").value = "6"; 	break;
					case "DF" : document.getElementById(tipo+"estado").value = "7"; 	break;
					case "ES" : document.getElementById(tipo+"estado").value = "8"; 	break;
					case "GO" : document.getElementById(tipo+"estado").value = "9"; 	break;
					case "MA" : document.getElementById(tipo+"estado").value = "10"; 	break;

					case "MG" : document.getElementById(tipo+"estado").value = "13"; 	break;
					case "MS" : document.getElementById(tipo+"estado").value = "12"; 	break;
					case "MT" : document.getElementById(tipo+"estado").value = "11"; 	break;
					case "PA" : document.getElementById(tipo+"estado").value = "14"; 	break;
					case "PB" : document.getElementById(tipo+"estado").value = "15"; 	break;

					case "PE" : document.getElementById(tipo+"estado").value = "17"; 	break;
					case "PI" : document.getElementById(tipo+"estado").value = "18"; 	break;
					case "PR" : document.getElementById(tipo+"estado").value = "16"; 	break;
					case "RJ" : document.getElementById(tipo+"estado").value = "19"; 	break;
					case "RS" : document.getElementById(tipo+"estado").value = "20"; 	break;

					case "RO" : document.getElementById(tipo+"estado").value = "22"; 	break;
					case "RR" : document.getElementById(tipo+"estado").value = "23"; 	break;
					case "RS" : document.getElementById(tipo+"estado").value = "21"; 	break;
					case "SC" : document.getElementById(tipo+"estado").value = "24"; 	break;
					case "SE" : document.getElementById(tipo+"estado").value = "26"; 	break;

					case "SP" : document.getElementById(tipo+"estado").value = "25"; 	break;
					case "TO" : document.getElementById(tipo+"estado").value = "27"; 	break;

				}

				ajaxLink('combo_cidade','ajax/carregaMunicipio.php?id='+document.getElementById(tipo+"estado").value+'&nomecid='+valores[3]);

			}else{

				alert("CEP errado ou inexistente! Por favor tente novamente com um CEP correto.");

				document.getElementById(tipo+"cep").value = "";
				document.getElementById(tipo+"cep").focus();
				document.getElementById(tipo+"cep").select();

				document.getElementById(tipo+"endereco").value 	= "";
				document.getElementById(tipo+"bairro").value 	= "";

				document.getElementById(tipo+"estado").value 	= "";
				ajaxLink('combo_cidade','ajax/carregaMunicipio.php?id=0');

			}
		}
	}
	xmlhttp.send(null)
}
