﻿$(document).ready(function() {

    var isTemaEpsilon = $('head link[href$="tema-epsilon.css"]').length > 0;

    $('table.tableABCR tr:even').addClass('linha-par');
    $('table.tableABCR tr:odd').addClass('linha-impar');

    if (isTemaEpsilon) {
        $('#area-conteudo .tinymce table').attr('width', '100%');
        $('#area-conteudo .tinymce table tr').each(function(i, tr) {
            $('td', tr).each(function(j, cel) {
                if (j == 0) {
                    $(cel).addClass('alL');
                }
                else {
                    $(cel).addClass('alC');
                }
            });
        });
    }

    $('.setor-numeros-tabela').each(function(t, tab) {
        $('tr:has(td)', tab).attr('class', '');

        $('tr:has(td):even', tab).addClass('tabela-linha-0');
        $('tr:has(td):odd', tab).addClass('tabela-linha-1');
    });

    $('#divUploadIndiceAbcr').hide();

    $('#pictoVoltar').click(function(event) {
        history.go(-1);
        event.preventDefault();
    });

    $('#pictoImprimir').click(function(event) {
        window.print();
        event.preventDefault();
    });

    $('#pictoEnviar').click(function(event) {
        if ($('#divEnviar').is(':visible')) {
            $('#divEnviar div.mensagem-excecao').hide();
            $('#divEnviar').slideUp();
        }
        else {
            $('#divEnviar').slideDown();
            $('#divEnviar input:text, #divEnviar textarea').val('');
        }

        event.preventDefault();
    });

    $('#btnCancelarMensagem').click(function() {
        $('#pictoEnviar').trigger('click');
    });

    $('#btnEnviarMensagem').click(function(event) {
        $('#divEnviar div.mensagem-excecao').hide();
        var meuNome = $('#txtMeuNome').val();
        if (meuNome.length == 0) {
            $('#divErroMeuNome').show();
            event.preventDefault();
            return;
        }

        var nome = $('#txtEnviarNome').val();
        if (nome.length == 0) {
            $('#divErroDestinatario').show();
            event.preventDefault();
            return;
        }

        var email = $('#txtEnviarEmail').val();
        if (email.length == 0 || !/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email)) {
            $('#divErroEmail').show();
            event.preventDefault();
            return;
        }
        var titulo = $('#txtEnviarTitulo').val();
        if (titulo.length == 0) {
            //alert('título obrigatório.');
            $('#divErroTitulo').show();
            event.preventDefault();
            return;
        }

        var mensagem = $('#txtEnviarMensagem').val();
        if (mensagem.length > 200) {
            $('#divErroMensagem').show();
            event.preventDefault();
            return;
        }

        // enviar
        $.ajax({
            type: "POST",
            url: '/WS/EnviarPara.asmx/EncaminharLink',
            data: JSON.stringify({
                url: window.location.toString(),
                meuNome: meuNome,
                nome: nome,
                email: email,
                titulo: titulo,
                mensagem: mensagem
            }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(resultado) {
                alert('A URL deste documento foi encaminhada para o destinatário com sucesso.');
                $('#pictoEnviar').trigger('click');
            },
            error: function(erro) {
                if (erro) {
                    alert('Houve um problema ao enviar o e-mail. Tente novamente em alguns instantes, ou comunique a ABCR (link fale conosco).');
                }
            }
        });

        event.preventDefault();

    });
});

function MascaraTelefone(objeto, ev) {
    var tecla = (ev.which) ? ev.which : ev.keyCode;

    var teclaValida = false;  // supõe que não deve processar

    if (tecla == 8) return true;
    if (tecla == 9) return true;

    // avalia as teclas digitadas: só aceita [0-9] ou backspace
    if (tecla >= 48 && tecla <= 57) {
        teclaValida = true;
    }
    else if ( tecla >= 96 && tecla <= 105){
        teclaValida = true;
        tecla -= 48;
    }
        

    // se tecla válida, pode "formatar"
    if (teclaValida && objeto.value.length <13) {
        if (objeto.value.length == 0)
            objeto.value = '(' + objeto.value;

        if (objeto.value.length == 3)
            objeto.value = objeto.value + ')';

        if (objeto.value.length == 8)
            objeto.value = objeto.value + '-';

        objeto.value += String.fromCharCode(tecla);
    }

    // indica se a tecla digitada era válida ou não
    return false;
}
