//------------------------------------------------------------------------
// Web Service CEP, desenvolvido por Evanil Rosano de Paula.
// Este Web Service está habilitado para funcionar em qualquer servidor,
// no entanto terá melhor desempenho em sites hospedados pela Via Virtual.
// Visite nosso site e conheça nossos serviços.
// Via Virtual - Solucões WEB
// http://www.viavirtual.com.br
//-------------------------------------------------------------------------

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        xmlhttp = false;
        }
      }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      } catch (e) {
      xmlhttp = false;
      }
    }
  return xmlhttp;
  }
var http = getHTTPObject();

function funcaowebservicecep()
{
	http.open("GET", 'http://carlospozzobon.com.br/wp-content/themes/carlospozzobon/buscarendereco.php?cep='+document.getElementById("cep").value, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);

	var arr; //array com os dados retornados
	function handleHttpResponse()
	{
		if (http.readyState == 4)
		{
			var response = http.responseText;
			eval("var arr = "+response); //cria objeto com o resultado
			document.getElementById("rua").value = arr.rua;
                        if (arr.rua != ''){
                            document.getElementById("rua").disabled = true;
                        }
			document.getElementById("bairro").value = arr.bairro;
                        if (arr.bairro != ''){
                            document.getElementById("bairro").disabled = true;
                        }
			document.getElementById("cidade").value = arr.cidade;
                        if (arr.cidade != ''){
                            document.getElementById("cidade").disabled = true;
                        }
                        var ddl = document.getElementById("estado");
                            for (var i = 0; i < ddl.options.length; i++) {
                                if (ddl.options[i].value == arr.uf) {
                                    if (ddl.selectedIndex != i) {
                                        ddl.selectedIndex = i;
                                        if (change)
                                            ddl.onchange();
                                    }

                               }
                           }
                        document.getElementById("estado").disabled=true;
		}
	}
}
