﻿var listaSelecionados = Array();

function AtualizaSelecionados(lista, indice) {
    parent.document.forms[0].hdTeste.value = lista;
    listaSelecionados = lista;
    indiceSelecionados = indice;
}
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 RetornaIndice() {
    return indiceSelecionados;
}
function VerificaCheckOK(tipobusca) {
    var i = 0;
    var sConteudoSel = "";
    var sTodaSelecao = "";
    var iQtdSel = 0;
    var bairro = '';
    var iQtdItensSel = 0;
    var sUsuario = "";

    document.getElementById("hdBairro").value = '';
    document.getElementById("hdCidade").value = '';

    if (tipobusca == 'buscar') {
        sUsuario = LeCookieContabilizaHome();
        GravaNavegacaoHome('16', '' + sUsuario + '', '0');
    }

    listaSelecionados = document.getElementById("hdTeste").value.split(',');

    if (tipobusca == 'buscar') {
        if (listaSelecionados.length == 0) {
            alert("Por favor selecionar um item.");
            return;
        }
        if (listaSelecionados.length == 1 && listaSelecionados[0] == '') {
            alert("Por favor selecionar um item.");
            return;
        }
    }

    for (i = 0; i < listaSelecionados.length; i++) {
        conteudo = listaSelecionados[i];
        if (conteudo.length > 0) {
            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 (tipobusca == 'buscar') 
            {
                if (document.getElementById("hdSelecionadoNome").value != '')
                    document.getElementById("hdSelecionadoNome").value += '||';
                document.getElementById("hdSelecionadoNome").value += conteudo.split("|")[3].replace(/^\s+|\s+$/g, '');
            }

            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++;
            tipo = tipo.toUpperCase();
            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;
            }
        }
    }

    // este item será atualizado pq o usuário pode ter clicado em cancelar e selecionado alguns itens.
    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');

    if (tipobusca == 'buscar') {
        GravaNavegacaoHome('36', '' + sUsuario + '', '' + iQtdItensSel + '');
        Confirmar('BuscarBairro', document.forms[0].imgBuscar);
    }
}