function desabilitarenter(event)
{
    if (event && event.which == 13)
        return false;
}

function verificaSuporteAjax()
{
    //verifica se o browser tem suporte a ajax
    try 
    {
        ajax = new ActiveXObject("Microsoft.XMLHTTP");
        return true;
    } 
    catch(e) 
    {
        try 
        {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
            return true;
        }
        catch(ex) 
        {
            try 
            {
                ajax = new XMLHttpRequest();
                return true;
            }
            catch(exc) 
            {
                alert("Esse browser não tem recursos para uso do Ajax");
                ajax = null;
                return false;
            }
        }
    }
}
function Dados(tipo,valor) 
{
    //se tiver suporte ajax
    if(verificaSuporteAjax()) 
    {
        //deixa apenas o elemento 1 no option, os outros são excluídos
        document.pesquisa.valor.options.length = 1;
        idOpcao  = document.getElementById("opcoes");
        ajax.open("POST", "valores.php?valor="+valor+"&tipo="+tipo, true);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
        ajax.onreadystatechange = function() 
        {
            if(ajax.readyState == 1) 
            {
                idOpcao.innerHTML = "Carregando...!";   
            }
            if(ajax.readyState == 4 ) 
            {
                if(ajax.responseXML) 
                    processXML(ajax.responseXML);
            }
        }
        var params = "tipo="+tipo+"&valor="+valor;
        ajax.send(params);
    }
}

function processXML(obj)
{
    var dataArray   = obj.getElementsByTagName("valor");
    if(dataArray.length > 0) 
    {
        for(var i = 0 ; i < dataArray.length ; i++) 
        {
            var item = dataArray[i];
            var codigo    =  item.getElementsByTagName("id")[0].firstChild.nodeValue;
            var formato    =  item.getElementsByTagName("formato")[0].firstChild.nodeValue;
            idOpcao.innerHTML = "---Selecione ---";
        
            //cria um novo option dinamicamente  
            var novo = document.createElement("option");
            //atribui um ID a esse elemento
            novo.setAttribute("id", "valor");
            //atribui um valor
            novo.value = codigo;
            //atribui um texto
            novo.text  = formato;
            //finalmente adiciona o novo elemento
            document.pesquisa.valor.options.add(novo);
        }
    }
}

function CarregaTipoImovel(tipo) 
{
    //verifica se o browser tem suporte a ajax
    try 
    {
        ajaxa = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch(e) 
    {
        try 
        {
            ajaxa = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(ex) 
        {
            try 
            {
                ajaxa = new XMLHttpRequest();
            }
            catch(exc) 
            {
                alert("Esse browser não tem recursos para uso do Ajax");
                ajaxa = null;
            }
        }
    }

        //deixa apenas o elemento 1 no option, os outros são excluídos
        document.pesquisa.tipo_imovel.options.length = 1;
        idOpcao2  = document.getElementById("tipo_opcoes");
        ajaxa.open("POST", "tipo_imovel.php?tipo="+tipo, true);
        ajaxa.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
        ajaxa.onreadystatechange = function() 
        {
            if(ajaxa.readyState == 1) 
            {
                idOpcao2.innerHTML = "Carregando...!";   
            }
            if(ajaxa.readyState == 4 ) 
            {
                if(ajaxa.responseXML) 
                    processXMLTipoImovel(ajaxa.responseXML);
            }
        }
        var params = "tipo="+tipo;
        ajaxa.send(params);
    
}

function processXMLTipoImovel(obj)
{
    var dataArray   = obj.getElementsByTagName("tipo_imovel");
    if(dataArray.length > 0) 
    {
        for(var i = 0 ; i < dataArray.length ; i++) 
        {
            var item = dataArray[i];
            var codigo    =  item.getElementsByTagName("id")[0].firstChild.nodeValue;
            var titulo    =  item.getElementsByTagName("titulo")[0].firstChild.nodeValue;
            idOpcao2.innerHTML = "---Selecione ---";
        
            //cria um novo option dinamicamente  
            var novo = document.createElement("option");
            //atribui um ID a esse elemento
            novo.setAttribute("id", "tipo_imovel");
            //atribui um valor
            novo.value = codigo;
            //atribui um texto
            novo.text  = titulo;
            //finalmente adiciona o novo elemento
            document.pesquisa.tipo_imovel.options.add(novo);
        }
    }
}
function CarregaICidades(uf) 
{
    try {
        ajaxa = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e) 
    {
        try {
            ajaxa = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(ex) 
        {
            try {
                ajaxa = new XMLHttpRequest();
        	}catch(exc) 
            {
                alert("Esse browser não tem recursos para uso do Ajax");
                ajaxa = null;
            }
        }
    }
    document.frmImovel.i_cidade.options.length = 1;
    idOpcao3  = document.getElementById("cid_opcoes");
    ajaxa.open("POST", "cidades.php?uf="+uf, true);
    ajaxa.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    ajaxa.onreadystatechange = function() 
    {
        if(ajaxa.readyState == 1) 
        {
            idOpcao3.innerHTML = "Carregando...!";   
        }
        if(ajaxa.readyState == 4 ) 
        {
            if(ajaxa.responseXML) 
                processXMLICidades(ajaxa.responseXML);
        }
    }
    var params = "uf="+uf;
    ajaxa.send(params);
}

function processXMLICidades(obj)
{
    var dataArray   = obj.getElementsByTagName("i_cidade");
    if(dataArray.length > 0) 
    {
        for(var i = 0 ; i < dataArray.length ; i++) 
        {
            var item = dataArray[i];
            var codigo    =  item.getElementsByTagName("id")[0].firstChild.nodeValue;
            var titulo    =  item.getElementsByTagName("titulo")[0].firstChild.nodeValue;
            idOpcao3.innerHTML = "---Selecione ---";
        
            //cria um novo option dinamicamente  
            var novo = document.createElement("option");
            //atribui um ID a esse elemento
            novo.setAttribute("id", "cid_opcoes");
            //atribui um valor
            novo.value = codigo;
            //atribui um texto
            novo.text  = titulo;
            //finalmente adiciona o novo elemento
            document.frmImovel.i_cidade.options.add(novo);
        }
    }
}
function CarregaIBairros(cid) 
{
    try {
        ajaxa = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e) 
    {
        try {
            ajaxa = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(ex) 
        {
            try {
                ajaxa = new XMLHttpRequest();
        	}catch(exc) 
            {
                alert("Esse browser não tem recursos para uso do Ajax");
                ajaxa = null;
            }
        }
    }
    document.frmImovel.i_bairro.options.length = 1;
    idOpcao4  = document.getElementById("bairro_opcoes");
    ajaxa.open("POST", "bairros.php?cid="+cid, true);
    ajaxa.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    ajaxa.onreadystatechange = function() 
    {
        if(ajaxa.readyState == 1) 
        {
            idOpcao4.innerHTML = "Carregando...!";   
        }
        if(ajaxa.readyState == 4 ) 
        {
            if(ajaxa.responseXML) 
                processXMLIBairros(ajaxa.responseXML);
        }
    }
    var params = "cid="+cid;
    ajaxa.send(params);
}

function processXMLIBairros(obj)
{
    var dataArray   = obj.getElementsByTagName("i_bairro");
    if(dataArray.length > 0) 
    {
        for(var i = 0 ; i < dataArray.length ; i++) 
        {
            var item = dataArray[i];
            var codigo    =  item.getElementsByTagName("id")[0].firstChild.nodeValue;
            var titulo    =  item.getElementsByTagName("titulo")[0].firstChild.nodeValue;
            idOpcao4.innerHTML = "---Selecione ---";
        
            //cria um novo option dinamicamente  
            var novo = document.createElement("option");
            //atribui um ID a esse elemento
            novo.setAttribute("id", "bairro_opcoes");
            //atribui um valor
            novo.value = codigo;
            //atribui um texto
            novo.text  = titulo;
            //finalmente adiciona o novo elemento
            document.frmImovel.i_bairro.options.add(novo);
        }
    }
}