function dicionario(codBase, grupo) {
  document.getElementById("frameDicionario").src = "dicionario.jsp?"
    + "codBase=" + codBase + "&grupo=" + grupo + "&vazio=true";
}

function dicionarioPorTag(codBase, tag) {
  document.getElementById("frameDicionario").src = "dicionario.jsp?"
    + "codBase=" + codBase + "&tag=" + tag + "&vazio=true";
}

function dicionarioPorSubcampo(codBase, tag, subcampo) {
  document.getElementById("frameDicionario").src = "dicionario.jsp?"
      + "codBase=" + codBase + "&tag=" + tag + "&sub=" + subcampo
      + "&vazio=true";
}

function setCookie(name, value, expire) {
    document.cookie = name + "=" + escape(value) +
        ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(name) {   
    var search = name + "="   
    if (document.cookie.length > 0) { // if there are any cookies      
        offset = document.cookie.indexOf(search)       
        if (offset != -1) { // if cookie exists          
            offset += search.length          
            // set index of beginning of value         
            end = document.cookie.indexOf(";", offset)          
            // set index of end of cookie value         
            if (end == -1)             
                end = document.cookie.length         
            return unescape(document.cookie.substring(offset, end))
        }    
    }
}

function getCookieValue(name) {
    var cookie = getCookie(name);
    if (cookie != null) {
        offset = cookie.indexOf("=") + 1;
        cval = cookie.substring(offset, cookie.length);
        return cval;
    } else
        return null;   
}

function deleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function trataCarregamento() {
    for(k = 0; k < document.forms.length; k++) {
        if (document.forms[k].selecao != null) {
            cval = getCookieValue("selecao");
            if (cval != null) {
                if (cval.indexOf(document.forms[k].selecao.value + "-") != -1)
                    document.forms[k].selecao.checked = true;
            }
        }
    }
}

function trataSelecao(chk, mfn) {          
    cval = getCookieValue("selecao");
    if (chk.checked) {               
        if (cval != null) {                                                                          
            setCookie("selecao", cval + mfn + "-");                   
        } else {
            setCookie("selecao", mfn + "-");                   
        }     
    } else {                               
        if (cval != null) {
            search = mfn + "-";
            offset = cval.indexOf(search);
            if (offset != -1) {                                
                cval = cval.substring(0, offset) + cval.substring(offset + search.length, cval.length);
                setCookie("selecao", cval);                   
            }
        }
    }
}

function selecionarTodos() {
    for (k = 0; k < document.forms.length; k++) {
        selecao = document.forms[k].selecao;
        if (selecao != null) { 
            selecao.checked = true;
            trataSelecao(selecao, selecao.value);
        }
    }
}

function deselecionarTodos() {
    for (k = 0; k < document.forms.length; k++) {
        selecao = document.forms[k].selecao;
        if (selecao != null) {
            selecao.checked = false;
            trataSelecao(selecao, selecao.value);
        }
    }
    deleteCookie("selecao");
}

function trataClickResumo(mfn) {
    if (layerVisivel("resumo" + mfn)) {
        ocultaLayer("resumo" + mfn);
        document.getElementById("descricaoResumo" + mfn).innerHTML = "Mostrar Resumo";             
    } else {
        mostraLayer("resumo" + mfn);
        document.getElementById("descricaoResumo" + mfn).innerHTML = 'Ocultar Resumo';              
    }
}

function trataClickNotasConteudo(mfn) {
    if (layerVisivel("notasConteudo" + mfn)) {
        ocultaLayer("notasConteudo" + mfn);
        document.getElementById("descricaoNotasConteudo" + mfn).innerHTML = "Mostrar Notas de Conteúdo";             
    } else {
        mostraLayer("notasConteudo" + mfn);
        document.getElementById("descricaoNotasConteudo" + mfn).innerHTML = 'Ocultar Notas de Conteúdo';              
    }
}

function trataClickExemplares(mfn) {
    if (layerVisivel("exemplares" + mfn)) {
       ocultaLayer("exemplares" + mfn);
       document.getElementById("descricaoExemplares" + mfn).innerHTML = 'Mostrar Exemplares';             
    } else {
       mostraLayer("exemplares" + mfn);
       document.getElementById("descricaoExemplares" + mfn).innerHTML = 'Ocultar Exemplares';              
    }
}

function trataClickPalavrasChave(mfn) {
    if (layerVisivel("palavrasChave" + mfn)) {
       ocultaLayer("palavrasChave" + mfn);
       document.getElementById("descricaoPalavrasChave" + mfn).innerHTML = 'Mostrar Palavras-chave';             
    } else {
       mostraLayer("palavrasChave" + mfn);
       document.getElementById("descricaoPalavrasChave" + mfn).innerHTML = 'Ocultar Palavras-chave';              
    }
}

function trataClickColecaoKardex(mfn) {
    if (layerVisivel("colecaoKardex" + mfn)) {
       ocultaLayer("colecaoKardex" + mfn);
       document.getElementById("descricaoColecaoKardex" + mfn).innerHTML = 'Mostrar Coleção';             
    } else {
       mostraLayer("colecaoKardex" + mfn);
       document.getElementById("descricaoColecaoKardex" + mfn).innerHTML = 'Ocultar Coleção';              
    }
}

function abreSituacao(url) {
    win = window.open(url, 'situacao', 'height=350, width=725, menubar=no, toolbar=no, location=no, resizable=no, scrollbars=yes,top=0,left=0');
    win.focus();
}

function abreRenovacao(context) {
    url = context + "/faces/cliente/emprestimo/seguro/renovacaoInicio.jsp";
    win = window.open(url, "renovacao", "height=350, width=590, menubar=no, toolbar=no, location=no, resizable=yes, scrollbars=yes, top=0, left=0");
    win.focus();
}

function aquisicoes(base) {
  document.forms[0].action = "pagina.jsp?conteudo=aquisicoes.jsp&codBase=" + base;
  document.forms[0].submit();
}

function link(campo, valor) {
    campos = document.formLink.getElementsByTagName("input");
    for (k = 0; k < campos.length; k++) 
        if (campos[k].name.substring(0, 3) == "uf_") campos[k].value = "";    
    eval("document.formLink." + "uf_" + campo).value = valor;
    document.formLink.submit();
}

function mostraFormato(prefixoBase, nomeBase, formato, mfn) {
    win = window.open('mostraFormato.jsp?prefixoBase=' + prefixoBase +
        '&nomeBase=' + nomeBase + '&formato=' + formato + '&mfn=' + mfn,
        'visualizacao',
        'height=350, width=500, menubar=no, toolbar=no, location=no, ' +
        'resizable=yes, scrollbars=yes, top=10, left=10');
    win.focus();
} 

function visualiza(codBase, mfn, classeFormato) {
    win = window.open('visualizacao.jsp?codBase=' + codBase
            + '&mfn=' + mfn
            + '&classeFormato=' + classeFormato,
        'visualizacao',
        'height=350, width=650, menubar=no, toolbar=no, location=no, ' +
        'resizable=yes, scrollbars=yes, top=10, left=10');
    win.focus();
}

function trataClickArquivos(mfn) {
    if (layerVisivel("arquivos" + mfn)) {
       ocultaLayer("arquivos" + mfn);
       document.getElementById("descricaoArquivos" + mfn).innerHTML = 'Mostrar Arquivos';             
    } else {
       mostraLayer("arquivos" + mfn);
       document.getElementById("descricaoArquivos" + mfn).innerHTML = 'Ocultar Arquivos';              
    }
}

function inicializaMask() {
  mascaraData = new Mask("dd/mm/yyyy", "date");
  mascaraData.attach(document.forms[0].dataInicial);
  mascaraData.attach(document.forms[0].dataFinal);
}

