function getAjax() {
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function limpaSelect(campo) {
	alvo = document.getElementById(campo);
	var size = alvo.length;
	for ( i=(size-1); i>=0; i--) {
		alvo.options[i] = null;
	}
	alvo.options[0]= new Option("Todas");
	alvo.options[0].value = "Todas";
}

function pesquisaLocalidade(tipo, id, campo) {
	xmlhttp = getAjax();
	x = Math.random()*554987987;
	xmlhttp.open("GET", "ajax_busca_localidade.php?tipo="+tipo+"&id="+id+"&x="+x,true);
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var retorno = xmlhttp.responseText
			retorno=retorno.replace(/\+/g," ")
            retorno=unescape(retorno)
			dados = retorno.split("<br />");
			
			limpaSelect(campo);
			
			for (i=0; i<dados.length; i++) {
				valor = dados[i].split("=");
				alvo.options[i]= new Option(valor[0]);
				alvo.options[i].value = valor[1];
			}
		}
	}
	xmlhttp.send(null)
}

function pesquisaCEP(nomeCampo) {
	var cep = document.getElementById("cep").value;
	document.getElementById("aguarde").style.visibility = 'visible'
		
	xmlhttp = getAjax();
	x = Math.random()*554987987;
	xmlhttp.open("GET", "ajax_busca_endereco.php?cep="+cep+"&x="+x,true);
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var retorno = xmlhttp.responseText
			retorno=retorno.replace(/\+/g," ")
            retorno=unescape(retorno)
			
			dados = retorno.split("<br>");
			
			if (dados[0] == 'OK') {
				if (dados[1].length > 0) {
					document.getElementById("endereco").value = dados[4]
					document.getElementById("bairro").value = dados[3]
					document.getElementById("cidade").value = dados[2]
					
					for(i=0; i<document.getElementById("uf").length; i++) {
						if(document.getElementById("uf")[i].text==dados[1])
						document.getElementById("uf")[i].selected = true;
					}
					
					document.getElementById("numero").focus();
				} else {
					alert("O CEP digitado não foi localizado");	
				}
			} else {
				alert(dados[0]);
			}
			
			document.getElementById("aguarde").style.visibility = 'hidden'
		}
	}
	xmlhttp.send(null)
}


function pesquisaCEPNovo(campoCep, campoEndereco, campoNumero, campoBairro, CampoCidade, CampoUf) {
	var cep = document.getElementById(campoCep).value;
		
	xmlhttp = getAjax();
	x = Math.random()*554987987;
	xmlhttp.open("GET", "ajax/busca_endereco.php?cep="+cep+"&x="+x,true);
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var retorno = xmlhttp.responseText
			retorno=retorno.replace(/\+/g," ")
            retorno=unescape(retorno)
			
			dados = retorno.split("<br>");
			
			if (dados[0] == 'OK') {
				if (dados[1].length > 0) {
					document.getElementById(campoEndereco).value = dados[4]
					document.getElementById(campoBairro).value = dados[3]
					document.getElementById(CampoCidade).value = dados[2]
					
					for(i=0; i<document.getElementById(CampoUf).length; i++) {
						if(document.getElementById(CampoUf)[i].text==dados[1])
						document.getElementById(CampoUf)[i].selected = true;
					}
					//esse if aqui é um gato pra quando o campo uf não for um select mas sim um input. 
					if (document.getElementById(CampoUf).length == undefined) {
						document.getElementById(CampoUf).value = dados[1];
					}
					
					document.getElementById(campoNumero).focus();
				} else {
					alert("O CEP digitado não foi localizado");	
				}
			} else {
				alert(dados[0]);
			}
		}
	}
	xmlhttp.send(null)
}