var idMunicipio;
var nomeMunicipio;
var telaPopup;

function acessarSistemaBotaoClick(){
    if (document.getElementById("loginTb").value == ""){
        alert("O campo 'Login' é obrigatório!");
        document.getElementById("loginTb").focus();
        return false;
    }
    if (document.getElementById("senhaTb").value == ""){
        alert("O campo 'Senha' é obrigatório!");
        document.getElementById("senhaTb").focus();
        return false;
    }
    document.getElementById("pwcod").value = hex_md5(document.getElementById("senhaTb").value);
    document.getElementById("senhaTb").value = "";
    document.getElementById("action").value = "acessarSistema";
    document.efetuarLoginForm.submit();
    document.getElementById("efetuarLoginForm").innerHTML = "<center><img src='imgs/aguarde.gif' alt='Aguarde' /> Por favor, aguarde! </center>";
    return true;
}


function editField(field, site, id){
    if (window.ActiveXObject)
    {
        window.open("manutencaoTextoCampo.jsp?field=" + field + "&site=" + site + "&frameId=" + id, "EditarCampo", 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, overflow-x=no, resizable=no, copyhistory=no,modal=yes, width=800px, height=500px');
    }else{
        window.showModalDialog("manutencaoTextoCampo.jsp?field=" + field + "&site=" + site + "&frameId=" + id, "EditarCampo","dialogWidth:1000px; dialogHeight:500px; center:yes;status:off;resizable:no");
    }
}

function salvarEdicaoCampo(field,site, id){
    document.getElementById('action').value = "salvar";
    document.getElementById('manutencaoTextoCampoForm').submit();
    //requisitarPaginaAjax('manutencaoTextoCampo.jsp?action=salvar&field=' + field + '&site=' + site + "&frameId=" + id);
}

function popupCenter(pageURL, title,w,h) {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    if (window.showModalDialog) {
        window.showModalDialog(pageURL, title,"dialogWidth:" + w + "px; dialogHeight:" + h + "px; center:yes;status:off;resizable:no");
    } else {
        telaPopup = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, overflow-x=no, resizable=no, copyhistory=no,modal=yes, width='+w+', height='+h+', top='+top+', left='+left);
    }
}

function abrirPagina(pagina){
    if (document.getElementById("idChave") != null){
        document.getElementById("idChave").value = "0";
    }
    requisitarPaginaAjax(pagina);
}

function logarGestao(){
    document.getElementById("pwcod").value = hex_md5(document.getElementById("senhaTb").value);
    document.getElementById("senhaTb").value = "";
    document.getElementById("action").value = "efetuarLogin";
    document.getElementById("loginGestaoForm").submit();
}
function logarSistema(){
    document.getElementById("pwcod").value = hex_md5(document.getElementById("senhaTb").value);
    document.getElementById("senhaTb").value = "";
    document.getElementById("action").value = "efetuarLogin";
    document.getElementById("efetuarLoginForm").submit();
}

function limparBotaoClick(){
    location = location;
}
function alterarSenhaBotaoClick()
{
    location = "alterarSenha.jsp";
}

function logarSistema(){
    document.getElementById("pwcod").value = hex_md5(document.getElementById("senhaTb").value);
    document.getElementById("senhaTb").value = "";
    document.getElementById("action").value = "efetuarLogin";
    document.getElementById("efetuarLoginForm").submit();
}

function listarFilhos(idPai){
    document.getElementById("idChave").value = "0";
    requisitarPaginaAjax("autenticacaoSeguranca/cadastroMenus.jsp?fmPai=" + idPai);
}
function listarMenus(idChave){
    document.getElementById("idChave").value = idChave;
    requisitarPaginaAjax("autenticacaoSeguranca/cadastroMenus.jsp?action=exibir");
}
function salvarMenuClick(idChave){
    document.getElementById("idChave").value = idChave;
    if (campoPreenchimentoObrigatorio("Descrição", "descricao_menu")){
        if (campoPreenchimentoObrigatorio("Ordem", "ordem_menu")){
            requisitarPaginaAjax("autenticacaoSeguranca/cadastroMenus.jsp?action=salvar");
        }
    }
}

function excluirAuditoriaClick(){
    if (campoPreenchimentoObrigatorio("Data inicial", "dataInicial")){
        if (campoPreenchimentoObrigatorio("Data final", "dataFinal")){
            acaoExcluirAjax("logs de auditoria", "autenticacaoSeguranca/cadastroAuditoria.jsp");
        }
    }
}
function adicionarPerfilUsuario(idChave, idMenu){
    if (document.getElementById("perfisCb").value == 0){
        alert("Selecione um perfil de usuários");
        document.getElementById("perfisCb").focus();
    }else if (confirm("Inserir nova permissão. Você tem certeza?")){
        requisitarPaginaAjax("autenticacaoSeguranca/permitirAcessoPerfilUsuarios.jsp?idMenu=" + idMenu + "&action=novo&idChave=" + idChave);
    }
}
function excluirPerfilUsuario(idMenu){
    if (confirm("Você deseja excluir permissão para os perfis marcados?")){
        var i = 0;
        var dadosExclusao = "";
        while(document.getElementById("marcadoCk_" + i) != null){
            if (document.getElementById("marcadoCk_" + i).checked){
                dadosExclusao += document.getElementById("opcao_" + i).value + ",";
            }
            i++;
        }
        dadosExclusao = dadosExclusao.substring(0,dadosExclusao.length - 1);
        document.getElementById("dadosExclusao").value = dadosExclusao;
        requisitarPaginaAjax("autenticacaoSeguranca/permitirAcessoPerfilUsuarios.jsp?idMenu=" + idMenu + "&action=excluir");
    }
}
