function getHTTPObject() {
	var xhr = false;//set to false, so if it fails, do nothing
	if(window.XMLHttpRequest) {//detect to see if browser allows this method
		var xhr = new XMLHttpRequest();//set var the new request
	} else if(window.ActiveXObject) {//detect to see if browser allows this method
		try {
			var xhr = new ActiveXObject("Msxml2.XMLHTTP");//try this method first
		} catch(e) {//if it fails move onto the next
			try {
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");//try this method next
			} catch(e) {//if that also fails return false.
			xhr = false;
			}
		}
	}
	return xhr;//return the value of xhr
}

function funcaowebservicecep()
{
	var http = getHTTPObject();
		
	document.getElementById("endereco").value = "Carregando...";
	document.getElementById("bairro").value = "Carregando...";
	document.getElementById("cidade").value = "Carregando...";	

	http.open("GET", 'consultacep.php?cep='+document.getElementById("cep").value, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
	return;

	function handleHttpResponse()
	{
		if (http.readyState == 4)
	{
		var response = http.responseText;
		var arr; //array com os dados retornados
		eval("var arr = "+response); //cria objeto com o resultado
		if (arr.endereco!=" ") {
			document.getElementById("endereco").value = arr.endereco;
			document.getElementById("bairro").value = arr.bairro;
			document.getElementById("cidade").value = arr.cidade;
			document.getElementById("estado").value = arr.estado;
			document.getElementById("numero").focus();
		} else {
			document.getElementById("endereco").value = "CEP inválido!";
			document.getElementById("bairro").value = "CEP inválido!";
			document.getElementById("cidade").value = "CEP inválido!";
		}
	}
}
}