var evento = [];

$(document).ready(function () {
    $("input[id*=nomeAlertaRI]").focus(function () {
        $("input[id*=nomeAlertaRI]").val("");
    });

    $("input[id*=emailAlertaRI]").focus(function () {
        $("input[id*=emailAlertaRI]").val("");
    });

    $("#EnviarLembrete").click(function () {
        enviaAlerta();

    });
});

$(document).ready(function () {
    $(".textoCamposAlertasRi").css({ height: "30px", paddingLeft: "15px" });
    $('input[id*=controlMailing]').attr("style", "display:none");
});

function Marcar() {

    
    //if ($('#controlMailingPort').is(':checked') != true && $('#controlMailingIng').is(':checked') != true) {
    if ($('input[type=radio]:checked').length == 0){
        alert("Selecione pelo menos um marcador.")
        return false;
    }
    if (document.getElementById('nome_modal').value == "") {
        alert("É necessário um nome.")
        return false;
    }
    if (document.getElementById('email_modal').value == "") {
        alert("É necessário um email.")
        return false;
    }
    return true;
}

function enviaAlerta() {

    if (Marcar()) {
        var MailingPort = "";
        var MailingIng = "";

        var nomeAlerta = $('#nome_modal').val();
        var emailAlerta = $('#email_modal').val();
        var telefone = $('#controlTelefone').val();
        var empresa = $('#controlEmpresa').val();
        var cargo = $('#controlCargo').val();
        var perfil = $('#controlPerfil').val();

        if (document.getElementById('controlMailingPort').checked == true) {
            var MailingPort = $('#controlMailingPort').val();
        }
        if (document.getElementById('controlMailingIng').checked == true) {
            var MailingIng = $('#controlMailingIng').val();
        }

        var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (!filter.test(emailAlerta)) {
            alert('E-mail Inválido!');
            return false;
        }

        var filtertel = /^\d+$/i;
        if (!filtertel.test(telefone)) {
            alert('Telefone Inválido!');
            return false;
        }

        if (Trim(nomeAlerta) == "") {
        alert('Digite um nome!');
        }

        CallServer("alerta;" + nomeAlerta + ";" + emailAlerta + ";" + telefone + ";" + empresa + ";" + cargo + ";" + perfil + ";" + MailingPort + ";" + MailingIng);
    }
}

function abreBoxMailing(nome, email) {

    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;


    if (filter.test(email) && Trim(nome) != "") {
        document.getElementById('nome_modal').value = nome;
        document.getElementById('email_modal').value = email;
        $('#fade').show();
        $('#formMailing').show();
        posicionaFormMailing();
    }
    else if (Trim(nome) == "") {
        alert('Digite um nome!');
        document.getElementById('nome').value = "";
        document.getElementById('nome').focus();
    }
    else if (!filter.test(email)) {
        alert('E-mail Inválido!');
        document.getElementById('email').value = "";
        document.getElementById('email').focus();
    }

    if ($('input[type=radio]').length > 0) {
        $('input[type=radio]')[0].checked = 1;
        $('input[type=radio]')[1].checked = 0;
    }
}

function Trim(str) { return str.replace(/^\s+|\s+$/g, ""); }

function fechaBoxAlerta() {
    $('#fade').hide();
    $('#formMailing').hide();
    $('div[id*=formMailing]').find('input[type=text]').val('');
}


function c_Mascara() {
    this.Mascara = function (o, f) {
        this.setFun(f);
        this.setObj(o);
        setTimeout("classMascara.exe()", 1);
    }

    this.exe = function () {
        switch (this.getFun()) {
            case "Telefone": obj = this.getObj();
                obj.value = this.Telefone(this.getObj());
                break;
        }
    }

    this.Telefone = function (o) {
        valor = o.value;
        valor = valor.replace(/\D/g, "")
        valor = valor.replace(/^(\d\d)(\d)/g, "($1) $2")
        valor = valor.replace(/(\d{4})(\d)/, "$1-$2")
        return valor;
    }

    this.getObj = function () { return ob; }
    this.setObj = function (o) { ob = o; }
    this.getFun = function () { return fun; }
    this.setFun = function (f) { fun = f; }
}
classMascara = new c_Mascara();

function posicionaFormMailing() {
    $('div[id*=formMailing]').css({position: "fixed", top: "50%"});
}
