function setCampoCEP(id, valor, style){
if(style==null) style = 2; 
	
	if(style==1)	document.getElementById(id).style.color='red';
	if(style==2)	document.getElementById(id).style.color='black';
	if(style==3)	document.getElementById(id).style.color='green';

document.getElementById(id).value = valor;
}

function checaEndereco(url, campoCEP, campoCidade, campoEndereco, campoBairro, selectIdEstado){
var xmlHttp;
var cod_cep = document.getElementById(campoCEP).value; 

try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
} catch (e) {
	// Internet Explorer
	try {
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e){
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			alert("Seu navegador não suporta AJAX!");
			return false;
		}
	}
}
xmlHttp.onreadystatechange=function(){
	if(xmlHttp.readyState==4) {
	var resultado 	= xmlHttp.responseText;

		if(xmlHttp.responseText=="NADA"){
			setCampoCEP('resCep', 'Nenhum endereço encontrado.', 1);
		} else {
				var string 		= resultado.split("#");
				if(string[0]!="") setCampoCEP(campoCidade, string[0]);
				if(string[1]!="") setCampoCEP(campoEndereco, string[1]);
				if(string[2]!="") setCampoCEP(campoBairro, string[2]);
				if(string[3]!="") setCampoCEP(selectIdEstado, string[3]);
				setCampoCEP('resCep', 'Endereço Encontrado.', 3);
		}	
	}
}
url 	= url + cod_cep;

xmlHttp.open("GET", url,true);
xmlHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
xmlHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Pragma", "no-cache");
xmlHttp.send(null);
}

/*
MODO DE USO (CPF):

*/