﻿document.getElementById("ApresentaLocalidade").style.display = "none";
document.getElementById("ApresentaBairro").style.display = "none";

if (textoBuscaRapida == "BAIRRO") {
    if (document.getElementById("hdBairro").value != "" || document.getElementById("hdCidade").value != "")
        RedirecionaResultado();
    document.getElementById("hdBairro").value = '';
    document.getElementById("hdCidade").value = '';

}
else if (textoBuscaRapida == "PESQUISA") {
    RedirecionaPesquisa();
}

function RemoveNode(id) {
    var ob = document.getElementById(id);
    if (ob != null) ob.parentNode.removeChild(ob);
}
function VerificaHidden() {
    if (document.getElementById("hdBuscaRapida").value != "BAIRRO" && document.getElementById("hdBuscaRapida").value != "LANCAMENTO") {
        document.forms[0].submit();
    }
    else if (document.getElementById("hdBuscaRapida").value == "BAIRRO" || document.getElementById("hdBuscaRapida").value == "LANCAMENTO") {
        Busca();
        Segmentacao();

        RemoveNode('__VIEWSTATE');
        RemoveNode('__EVENTARGUMENT');
        RemoveNode('__EVENTTARGET');

        Workarounds()

        document.forms[0].action = 'resultado-busca.aspx';
        document.forms[0].submit();
    }
}
function Workarounds()
{
    document.getElementById("hdNomeLocalidade").value = document.getElementById("hdNomeLocalidade").value.replace(/,/g, '||')
    var hdBairro = document.getElementById("hdBairro")
    var hdCidade = document.getElementById("hdCidade")
    var hdCidadeCodigo = document.getElementById("hdCidadeCodigo")
    document.getElementById("hdCidadeCodigo").value = "";
    hdCidadeCodigo.value = hdCidade.value;
    if (hdBairro.value != '' && hdCidade.value != '')
    {
        hdCidade.value = '';
    }
}

function Segmentacao() {
    var sCookie = "";
    var Tipo = "";
    var SubTipo = "";
    var Transacao = "";
    var Dormitorio = "";
    var ValorMin = "";
    var ValorMaximo = "";
    var AreaMin = "";
    var AreaMax = "";
    var ZonaGrupo = "";
    var i;
    var sConteudo = "";
    var sCidade = "";
    var sLocalidade = "";
    var indice = 0;
    var sBairro = "";
    var sArrayLocalidade;
    var sArrayBairro;
    var UF = "";
    var CanalLancamento = "";

    sLocalidade = document.getElementById("cboLocalidade").value;
    sLocalidade = sLocalidade.substr(0, sLocalidade.length - 1);
    sBairro = document.getElementById("hdBairro").value;
    sArrayLocalidade = sLocalidade.split(" ");
    sArrayBairro = sBairro.split(";");
    //foi selecionada + de uma localidade
    if (sArrayLocalidade.length > 1) {
        indice = Math.floor(Math.random() * sArrayLocalidade.length);
        if (indice == 0 || indice == '')
            indice = 1;

        if (sArrayLocalidade[indice].split("||").length > 1) {
            sCidade = sArrayLocalidade[indice].split("||")[0];
            sLocalidade = sArrayLocalidade[indice].split("||")[1];
            ZonaGrupo = sArrayLocalidade[indice].split("||")[2];
        }
        else {
            sCidade = sArrayLocalidade[indice];
            sLocalidade = "0";
            ZonaGrupo = "0";
        }
    }
    else {
        if (sArrayLocalidade[0].split('||').length > 1) {
            sCidade = sArrayLocalidade[0].split('||')[0];
            sLocalidade = sArrayLocalidade[0].split('||')[1];
            ZonaGrupo = sArrayLocalidade[0].split('||')[2];
        }
        else {
            ZonaGrupo = "0";
            sCidade = sArrayLocalidade[0];
            sLocalidade = "0";
        }
    }

    if (sArrayBairro.length > 1) {
        indice = Math.floor(Math.random() * sArrayBairro.length);
        if (indice == 0)
            indice = 1;
        sBairro = sArrayBairro[indice];
    }
    else {
        sBairro = sArrayBairro[0];
    }

    var sCanalLancamento = document.getElementById("hdCanalLancamento").value;
    sCanalLancamento = sCanalLancamento.split("|");

    if (sCanalLancamento != '') {
        if (sCanalLancamento.length > 1) {
            indice = Math.floor(Math.random() * sCanalLancamento.length);
            if (indice == 0 || indice == '')
                indice = 1;
            CanalLancamento = sCanalLancamento[indice];
        }
        else {
            CanalLancamento = sCanalLancamento;
        }
    }

    if (document.getElementById("hdCodUFLancamento").value != '') {
        UF = document.getElementById("hdCodUFLancamento").value;
    }

    if (CanalLancamento == '') {
        sTipo = document.getElementById("cboTipo").value;
        Tipo = sTipo.split("|")[0];
        SubTipo = sTipo.split("|")[1];
        Transacao = document.getElementById("cboTransacao").value;
        Dormitorio = document.getElementById("cboQtdDormitorio").value;
        ValorMin = '';
        ValorMaximo = '';
        AreaMin = document.getElementById("hdAreaMinima").value;
        AreaMax = '';
        UF = document.getElementById("hdUF").value;
    }
    else {
        sLocalidade = "";
        ZonaGrupo = "";
    }

    var sCookieValue = "";
    sCookieValue = UF + '-' + sLocalidade + '-' + sCidade + '-' + sBairro + '--' + Tipo + '-' + SubTipo + '-' + Transacao + '-' + Dormitorio + '--';
    sCookieValue += ValorMin + '-' + ValorMaximo + '-' + AreaMin + '-' + AreaMax + '-' + ZonaGrupo + '-' + sCidade + '---' + CanalLancamento;
    DeleteCookie('segmentacao-imoveis');
    setCookie('segmentacao-imoveis', sCookieValue, '31/12/2010');

}
function RemoveAcentos(conteudo) {
    var texto = conteudo.split('');
    var len = conteudo.length;
    var Alterado = false;
    
    var acento = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž^´`~"';
    var semacento = ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'c', 'c', 'd', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'n', 'n', 's', 's', 'y', 'y', 'y', 'z', '', '', '', '', ''];
    
    for (var x = 0; x < len; x++) {
        if (acento.indexOf(texto[x]) > -1)
        {
            texto[x] = semacento[acento.indexOf(texto[x])];
            Alterado = Alterado || true;
        }
    }
    conteudo = texto.join('');
    return conteudo;
}
function Busca() {

    var sConteudo = '';
    sConteudo = 'IDTransacao=' + document.getElementById("cboTransacao").value + '&Transacao=' + document.getElementById("hdNomeTransacao").value + '&Dormitorio=' + document.getElementById("cboQtdDormitorio").value;
    sConteudo += '&CodFaixaValorHome=' + document.getElementById("cboValor").value + '&NomeFaixaValorHome='

    if (document.getElementById("hdNomeFaixaValor").value == '' || document.getElementById("hdNomeFaixaValor").value == 'selecione')
        sConteudo += '';
    else
        sConteudo += document.getElementById("hdNomeFaixaValor").value;

    if (document.getElementById("cboTipo").value.split("|")[0] == "9999") {
        sConteudo += '&IDTipo=1&Categoria=1|10|11'
    }
    else
        sConteudo += '&IDTipo=' + document.getElementById("cboTipo").value.split("|")[0] + '&Categoria='

    sConteudo += '&IDSubTipo=' + document.getElementById("cboTipo").value.split("|")[1];
    sConteudo += '&Tipo=&SubTipo=&IDUF=' + document.getElementById("hdUF").value + '&UF=&BairrosJuntos=';

    sLocalidade = document.getElementById("hdLocalidade").value;
    sLocalidade = sLocalidade.substr(0, sLocalidade.length - 1);


    if (sLocalidade.split(" ").length = 1) {
        if (sLocalidade.split("||").length > 1) {
            sConteudo += '&IDLocalidade=&ZonaGrupo='
            if (sLocalidade.indexOf("63371") > 0)
                sConteudo += '&Cidade=63371'
            else
                sConteudo += '&Cidade='
        }
        else {
            sConteudo += '&IDLocalidade=&ZonaGrupo=&Cidade=';
        }
    }
    else {
        sConteudo += '&IDLocalidade=&ZonaGrupo=&Cidade=';
    }


    sConteudo += '&Localidade=';

    sDistrito = document.getElementById("cboDistrito").value;
    /*    if (sDistrito.split(';').length = 1)
    sConteudo += '&IDDistrito=' + sDistrito;
    else
    sConteudo += '&IDDistrito=';*/

    sConteudo += '&IDDistrito=&IDUFLancamento=' + document.getElementById("hdCodUFLancamento").value + '&UFLancamento=' + document.getElementById("hdUFLancamento").value + '&IDLocalidadeLancamento=' + document.getElementById("hdCodCidadeLancamento").value + '&AreaMinima=' + document.getElementById("hdAreaMinima").value;
    sConteudo += '&LocalidadeLancamento=' + document.getElementById("hdCidadeLancamento").value + '&IDCanalLancamento=' + document.getElementById("hdCanalLancamento").value + '&ZAPIDCORPORATIVO=';

    sConteudo = RemoveAcentos(sConteudo);
    DeleteCookie('sParamBuscaImovel');
    setCookie('sParamBuscaImovel', sConteudo, '31/12/2010');
}
function readCookie(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function setCookie(cookie_name, cookie_value, cookie_expire) {
    if (String(window.location).indexOf('zap.com.br') > 0) {
        document.cookie = cookie_name + "=" + cookie_value + "; path=/; domain=.zap.com.br; expires=" + cookie_expire;
    }
    else {
        //document.cookie = cookie_name + "=" + unescape(encodeURIComponent(cookie_value)) + "; expires=" + cookie_expire ;
        document.cookie = cookie_name + "=" + cookie_value + "; path=/; expires=" + cookie_expire;
    }
}
function DeleteCookie(cookie) {
    setCookie(cookie, '', '31/12/2000');
}
function BuscaLancamento() {
    var oIframe = window.frames["if_codigosbusca"];
    var obj = oIframe.document.getElementById("CodUFLancamento");
    obj.value = document.getElementById("hdCodUFLancamento").value;
    var obj1 = oIframe.document.getElementById("CodCidadeLancamento");
    obj1.value = document.getElementById("hdCodCidadeLancamento").value;
    var obj2 = oIframe.document.getElementById("CodCanalLancamento");
    obj2.value = document.getElementById("hdCanalLancamento").value;
    var obj3 = oIframe.document.getElementById("UFLancamento");
    obj3.value = document.getElementById("hdUFLancamento").value;
    var obj4 = oIframe.document.getElementById("CidadeLancamento");
    obj4.value = document.getElementById("hdCidadeLancamento").value;
    oIframe.Enviar();
}
function RedirecionaResultado() {
    var oIframe = window.frames["if_codigosbusca"];
    var obj = oIframe.document.getElementById("CodBairro");
    obj.value = document.getElementById("hdBairro").value;
    var obj1 = oIframe.document.getElementById("CodCidade");
    obj1.value = document.getElementById("hdCidade").value;
    var obj2 = oIframe.document.getElementById("LocalidadeSel");
    obj2.value = document.getElementById("hdNomeLocalidade").value;
    var obj3 = oIframe.document.getElementById("CodLocalidade");
    obj3.value = document.getElementById("hdLocalidadeSeg").value;
    oIframe.Enviar();
}
function ValidarMateria() {
    if (document.getElementById("txtProcurarNoticia").value == '') {
        alert('É obrigatório o preenchimento do campo buscar dica ou matéria');
        document.getElementById("txtProcurarNoticia").focus();
    }
    else {
        document.getElementById("hdBuscaRapida").value = "NOTICIAS"
        VerificaHidden()
    }
}
function RedirecionaPesquisa() {
    var strURL = 'pesquisa-imoveis_bairro.aspx?localidade=' + document.getElementById("hdLocalidadePesquisa").value + '&uf=' + document.getElementById("hdUFPesquisa").value + '&transacao=' + document.getElementById("cboTransacao").value + '&tipo=' + document.getElementById("cboTipo").value + '&subtipo=' + document.getElementById("cboSubtipo").value;
    document.getElementById("if_pesquisa_imovel").src = strURL;
}

var listaSelecionados = Array();
var indiceSelecionados = Array();

function AtualizaSelecionados(lista, indice) {
    listaSelecionados = lista;
    indiceSelecionados = indice;
}

function RetornaIndice() {
    return indiceSelecionados;
}

function VerificaCheck() {
    var i = 0;
    var bSelecionado = false;
    var sConteudoSel = "";
    var sTodaSelecao = "";
    var iQtdSel = 0;
    var bairro = '';
    var iQtdItensSel = 0;

    var sUsuario = "";
    sUsuario = LeCookieContabilizaHome();
    GravaNavegacaoHome('16', '' + sUsuario + '', '0');

    for (i = 0; i < listaSelecionados.length; i++) {
        bSelecionado = true;
        conteudo = listaSelecionados[i];
        if (conteudo != null) {
            if (conteudo.split("|")[2].indexOf('Alphaville') >= 0 || conteudo.split("|")[2].indexOf('VIANNA') >= 0 || conteudo.split("|")[2].indexOf('Niter') >= 0) {
                tipo = 'BAIRRO';
            }
            else {
                tipo = conteudo.split("|")[0];
            }

            valor = conteudo.split("|")[1];

            if (bairro == '')
                bairro = conteudo.split("|")[3];
            else
                bairro += ', ' + conteudo.split("|")[3];

            if (sConteudoSel != conteudo.split("|")[2]) {
                sConteudoSel = conteudo.split("|")[2];
                iQtdSel++;
                if (sTodaSelecao == "")
                    sTodaSelecao = conteudo.split("|")[2];
                else
                    sTodaSelecao = sTodaSelecao + " - " + conteudo.split("|")[2];
            }
            iQtdItensSel++;
            if (tipo == "CIDADE") {
                if (document.getElementById("hdCidade").value == "")
                    document.getElementById("hdCidade").value = valor;
                else
                    document.getElementById("hdCidade").value = document.getElementById("hdCidade").value + ';' + valor;
            }
            else if (tipo == "BAIRRO") {
                if (document.getElementById("hdBairro").value == "")
                    document.getElementById("hdBairro").value = valor;
                else
                    document.getElementById("hdBairro").value = document.getElementById("hdBairro").value + ';' + valor;
            }
        }
    }

    if (bSelecionado == false) {
        alert("Por favor selecionar um item.");
    }
    else {
        GravaNavegacaoHome('17', '' + sUsuario + '', '' + iQtdItensSel + '');
        if (iQtdSel > 1)
            document.getElementById("hdNomeLocalidade").value = "";
        else
            document.getElementById("hdNomeLocalidade").value = sTodaSelecao;


        if (trim(document.getElementById("hdCidade").value) == "")
            document.getElementById("cboDistrito").value = document.getElementById("hdBairro").value;
        else
            document.getElementById("cboDistrito").value = document.getElementById("hdCidade").value;

        sLocalidade = document.getElementById("cboLocalidade").value;
        sLocalidade = sLocalidade.substr(0, sLocalidade.length - 1);

        if (document.getElementById("hdCidade").value == "")
            AtualizaCampoBairro(document.getElementById("hdBairro").value, bairro, 'BAIRRO');
        else
            AtualizaCampoBairro(document.getElementById("hdCidade").value, bairro, 'CIDADE');
    }
}