﻿function FE_checarCampos() {

}
function FE_verificar_login() {

}
function FE_verificar_recuperar_senha() {

}
function FE_checarCampos_ADM() {
    var hidden = $("td.FE_formulario_botoes input[type='hidden']")[0];
    var pns, nome;
    var num = 0;
    var validos = true;
    pns = nome = "";

    $("input[class*='fe_pn']").each(function () {
        var valor = $.trim(this.value);
        var numero = this.title;
        if (numero != 0) {
            if (pns == "")
                pns = numero + "#" + valor;
            else
                pns += ";" + numero + "#" + valor;
        }
        else {
            validos = false;
        }
    });

    if (!validos) 
        if (!confirm("Só serão salvos os parceiros de negócios validados.\nDeseja prosseguir?")) return false;

    hidden.value = pns;
    if (pns != "") return true;

    alert("informe ao menos um parceiro de negócios válido");
    return false;
}

function FE_exibirCPF(checado) {
    if (checado) {
        $(".FE_linha_CPF").show();
        $(".FE_linha_Email").hide();
    }
    else {
        $(".FE_linha_CPF").hide();
        $(".FE_linha_Email").show();
    }
}

function FE_exibirDivInstalacoes(IDHidden, el, ehAtribuicao) {
    var usuario = $("." + IDHidden).val().split(";");

    $(".FE_UsuarioAtribuicao > input[type='hidden']").val(usuario[1]);

    if (ehAtribuicao) {
        $(".tituloAtribuicao").html("Selecione Instalações para Atribuir [" + usuario[0] + "]:");
        $(".spanAviso").hide();
        $(".FE_checksAtribuicao").show();
        $(".FE_checksDesatribuicao").hide();
        $(".FE_blocoAtribuir > input[type='hidden']").val("true");
    } else {
        $(".tituloAtribuicao").html("Selecione Instalações para Desatribuir [" + usuario[0] + "]:");
        $(".spanAviso").show();

        var instalacoes = $("." + IDHidden + "Ins").val().split(";");
        $(".FE_checksAtribuicao").hide();
        var checks = "";
        $(".FE_checksDesatribuicao").html("");
        $(instalacoes).each(function () {
            if (this != "") {
                checks += "<input type='checkbox' class='checksDesatribuicao' value='" + $.trim(this.split('-')[0]) + "' /><span class='checksDesatribuicao'>" + this + "</span><br />";
            }
        });
        $(".FE_checksDesatribuicao").append(checks);
        $(".FE_checksDesatribuicao").show();
        $(".FE_blocoAtribuir > input[type='hidden']").val("false");
    }

    $(".FE_checksAtribuicao input[type='checkbox']").attr("checked", "");
    $(".FE_checksDesatribuicao input[type='checkbox']").attr("checked", "");

    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    $('.FE_divMask').css({ 'width': maskWidth, 'height': maskHeight });

    $('.FE_divMask').fadeIn(1000);
    $('.FE_divMask').fadeTo("slow", 0.8);

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    $(".FE_blocoInstalacoes").css('top', winH / 2 - $(".FE_blocoInstalacoes").height() / 2);
    $(".FE_blocoInstalacoes").css('left', winW / 2 - $(".FE_blocoInstalacoes").width() / 2);

    $(".FE_blocoInstalacoes").fadeIn(2000);
}

function FE_checksDesatribuicao() {
    var desatribuicao = "";
    $(".FE_checksDesatribuicao input[type='checkbox']:checked").each(function () {
        desatribuicao += this.value + ";";
    });
    $(".FE_hiddenDesatribuicao > input[type='hidden']").val(desatribuicao);
}

function FE_retirarSom_Captcha() {
    $("a#recaptcha_switch_audio_btn").parent().html("<img width='25' height='16' alt='' src='/_layouts/FaturaEletronica/img/audioBranco.png'>");
}

function FE_checarTodos() {
    $(".FE_checkBoxParaTodos input").click(function () {
        if (this.checked) {
            $(".FE_checksAtribuicao input[type='checkbox']:enabled").prop("checked", "checked");
            $(".FE_checksDesatribuicao input[type='checkbox']:enabled").prop("checked", "checked");
        } else {
            $(".FE_checksAtribuicao input[type='checkbox']:enabled").prop("checked", "");
            $(".FE_checksDesatribuicao input[type='checkbox']:enabled").prop("checked", "");
        }
    });
}
function FE_so_Numeros(e) {
    var codigo = (window.event) ? event.keyCode : e.which;
    if ((codigo >= 48 && codigo <= 57) || (codigo >= 96 && codigo <= 105) || (codigo >= 37 && codigo <= 40) || codigo == 8 || codigo == 46) return true;
    return false;
}
function FE_so_Numeros_Valida(e, el) {
    var codigo = (window.event) ? event.keyCode : e.which;
    if ((codigo >= 48 && codigo <= 57) || (codigo >= 96 && codigo <= 105) || codigo == 8 || codigo == 46) {
        el.style.border = "solid 1px #FFA500";
        el.title = "0";
        return true;
    }
    if (codigo >= 37 && codigo <= 40) return true;
    return false;
}
function FE_validar_PN(classe) {
    var er = new RegExp(/^[0-9]{1,10}$/);
    var el = $("." + classe)[0];

    if (el.title == "0") {
        if (er.test(el.value)) {

            var jaExiste = false;
            $("input[class*='fe_pn']").each(function () {
                if (this.title != 0) {
                    if (el.value == this.title) {
                        alert("Este PN já foi validado anteriomente para este administrador");
                        jaExiste = true;
                        return;
                    }
                }
            });
            $(".fe_pn_form span").each(function () {
                if (this.title != 0) {
                    if (el.value == this.title) {
                        alert("Este PN já atribuido anteriomente a este administrador");
                        jaExiste = true;
                        return;
                    }
                }
            });

            if (jaExiste) return false;

            var url = location.protocol + "//" + location.host + "/_layouts/FaturaEletronica/ValidaPN.aspx?NumeroPN=" + el.value;

            $.ajax({
                url: url,
                success: function (data) {
                    var valor = data.split('<span id="retorno">')[1].split("</span>")[0];
                    if (valor.length == 1) {
                        switch (eval(valor)) {
                            case 1:
                                alert("PN inválido");
                                break;
                            case 2:
                                alert("Serviço temporariamente indisponivel");
                                break;
                            case 3:
                                alert("Serviço temporariamente indisponivel tente novamente em instantes");
                                break;
                        }
                    } else {
                        el.style.border = "solid 1px #8CC63F";
                        el.title = el.value;
                        el.value = valor;
                    }
                }
            });

        } else
            alert("PN inválido");
    } else {
        alert("PN já válido");
    }

    return false;
}
var FE_pnAtual = 0;
function FE_adicionar_PN_form() {
    FE_pnAtual++;
    $("td.fe_pn_form").append("<div style='margin-top:8px'><input maxlength='100' title='0' class='caixaTexto fe_pn_" + FE_pnAtual + "' onkeydown='return FE_so_Numeros_Valida(event, this)' style='width:290px;border:solid 1px #FFA500;' type='text'><a href='#' onclick='return FE_validar_PN(\"fe_pn_" + FE_pnAtual + "\")' style='font-size:10px;'> * validar </a><a href='#' onclick='return FE_remover_PN(this)' style='font-size:10px;'> - remover</a></div>");
    return false;
}
function FE_adicionar_PN_form_PostBack() {
    $(document).ready(function () {
        var pns = $("td.FE_formulario_botoes input[type='hidden']").val();
        if (pns == "") return;

        pns = pns.split(";");
        for (var i = 0; i < pns.length; i++) {
            var nPNs = pns[i].split("#");
            if (i == 0) {
                var el = $(".fe_pn")[0];
                el.style.border = "solid 1px #8CC63F";
                el.title = nPNs[0];
                el.value = nPNs[1];
            } else {
                FE_pnAtual++;
                $("td.fe_pn_form").append("<div style='margin-top:8px'><input maxlength='100' value='" + nPNs[1] + "' title='" + nPNs[0] + "' class='caixaTexto fe_pn_" + FE_pnAtual + "' onkeydown='return FE_so_Numeros_Valida(event, this)' style='width:290px;border:solid 1px #8CC63F;' type='text'><a href='#' onclick='return FE_validar_PN(\"fe_pn_" + FE_pnAtual + "\")' style='font-size:10px;'> * validar </a><a href='#' onclick='return FE_remover_PN(this)' style='font-size:10px;'> - remover</a></div>");
            }
        }
    });
}
function FE_remover_PN(el) {
    $(el).parent().remove();
    return false;
}
function FE_fecharDivInstalacoes() {
    $('.FE_divMask').fadeOut();
    $('.FE_blocoInstalacoes').fadeOut();
    $(".FE_checkBoxParaTodos > input").attr("checked", "");
}
function FE_MesReferencia() {
    $(".mesReferencia").change(function () {
        if (this.value == -1) {
            $(".tableexibirfaturas tr").show();
        } else {
            $(".tableexibirfaturas tr").hide();
            $($(".tableexibirfaturas tr")[0]).show();
            $(".tableexibirfaturas .linhaMes" + eval(this.value)).show();
        }
    });
}
function FE_montaGrafico(xml, idDiv) {
    var myChart = new FusionCharts("/_layouts/FaturaEletronica/fusionCharts/FCF_Column3D.swf", "myChartId", "600", "350");
    myChart.setDataXML(xml);
    myChart.render(idDiv);
}
function FE_TrocarGuiaGrafico(el) {
    if ($(el).attr("class") == "FE_AbaGrafico FE_AbaGraficoAtivo") {
        return false;
    } else {
        $("div.FE_AbaGrafico").attr("class", "FE_AbaGrafico");
        $(el).attr("class", "FE_AbaGrafico FE_AbaGraficoAtivo");
    }

    if ($("div#graficoValores").is(":hidden")) {
        $("div#graficoConsumo").hide();
        $("div#graficoValores").show();
    } else {
        $("div#graficoValores").hide();
        $("div#graficoConsumo").show();
    }
}
function FE_InibeBotao(el, idSpan) {
    $(el).hide();
    $("#" + idSpan).show();
    
    if(navigator.appName == "Microsoft Internet Explorer"){
        $("#" + idSpan + " img").remove();
        $("#" + idSpan).html("<img src='/_layouts/faturaeletronica/img/loading.gif' /> " + $("#" + idSpan).html());
    }
}
function FE_naoAceitarTermoUso() {
    return confirm('Você tem certeza que deseja não aceitar os termos de confidencialidade e privacidade? \nCaso não aceite os termos, não será possível prosseguir com o cadastro para a utilização do Portal da Fatura Eletrônica.');
}

function FE_checkTodosInstalacoes(el) {

    if (el.checked) {
        $(el).parent().parent().parent().find("input[type='checkbox']").prop("checked", "checked");
    } else {
        $(el).parent().parent().parent().find("input[type='checkbox']").prop("checked", "");
    }
    return true;
}
