$(function(){
    // Dialog
    $('.dialog_certificados').dialog({
        autoOpen: false,
        width: 366
    });

    // Dialog Link
    if ($("#dialog_link_9001").get(0)) {
        $('#dialog_link_9001').click(function(){
            $('#dialog_9001').dialog('open');
            return false;
        });
        $('#dialog_link_14001').click(function(){
            $('#dialog_14001').dialog('open');
            return false;
        });
        $('#dialog_link_18001').click(function(){
            $('#dialog_18001').dialog('open');
            return false;
        });
    }


    if($("a[rel^='prettyPhoto']").get(0)) {
        $("a[rel^='prettyPhoto']").prettyPhoto({
            theme:'facebook'
        });
    }

    if ($("#frmEmail").get(0)) {
        $("#btnEnviar").click(function(){
            var valid = '';
            var isr = ' - campo obrigatório.';
            var destinatario = $("#destinatario").val();
            var remetente = $("#remetente").val();
            var assunto = $("#assunto").val();
            var mensagem = $("#mensagem").val();

            if (!remetente.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
                valid += '<br />Informe um e-mail válido.'+isr;
                $("#remetente").addClass('fundoVerm');
            }
            if (assunto.length<1) {
                valid += '<br />Assunto'+isr;
                $("#assunto").addClass('fundoVerm');
            }
            if (mensagem.length<1) {
                valid += '<br />Mensagem'+isr;
                $("#mensagem").addClass('fundoVerm');
            }
            if (valid!='') {
                $("#response").fadeIn("slow");
                $("#response").addClass('msgErro');
                $("#response").html("Erro: O(s) campo(s) em vermelho é(são) obrigatório(s).");
            //$("#response").html("Erro: Verifique os campos em vermelho"+valid);
            }
            else {
                var datastr = $("#frmEmail").serialize();
                
                //var datastr ='destinatario=' + destinatario + '&remetente=' + remetente + '&assunto=' + assunto + '&mensagem=' + mensagem;
                $("#response").css("display", "block");

                $("#response").removeClass('msgErro');
                $("#remetente").removeClass('fundoVerm');
                $("#assunto").removeClass('fundoVerm');
                $("#mensagem").removeClass('fundoVerm');
                
                $("#response").addClass('msgAtencao');
                $("#response").html("Enviando mensagem .... ");
                $("#response").fadeIn("slow");
                setTimeout("send('"+datastr+"','email.php','#frmEmail')",2000);
            }
            return false;
        });
    }

    $('#cancelar').click(function(){
        $('.fundoVerm').removeClass('fundoVerm');
        $("#response").fadeOut("slow");
    });

}); // Fecha document ready jQuery

// Enviar Email
function send(datastr, arquivo, IdForm){
    $.ajax({
        type: "POST",
        url: arquivo,
        data: datastr,
        cache: false,
        success: function(html){
            $("#response").removeClass('msgErro');
            $("#response").removeClass('msgAtencao');
            $("#response").addClass('msgConfirma');
            $("#response").fadeIn("slow");
            $("#response").html(html);
            setTimeout('$("#response").fadeOut("slow")',2000);
            clear_form_elements(IdForm);
        }
    });
}
// Limpar dados do Form
function clear_form_elements(ele) {
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}

function FormataValor(campo,tammax,teclapres) {
    var tecla = teclapres.keyCode;
    // keycode = 9  - representa a tecla TAB
    if (tecla==9) {
        event.keyCode=9;
        return event.keyCode;
    }
    if (tecla == 46) {
        event.keyCode = 8;
        return event.keyCode;
    }
    vr = document.getElementById(campo).value;
    //alert (campo);
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( ",", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    tam = vr.length;
    //alert (tam);
    if (tam < tammax && tecla != 8){
        tam = vr.length + 1;
    }
    if (tecla == 8 ){
        tam = tam - 1;
    }
    if ( tecla == 109 || tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
        if ( tam <= 2 ){
            document.getElementById(campo).value = vr;
        }
        if ( (tam > 2) && (tam <= 5) ){
            document.getElementById(campo).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
        }
        if ( (tam >= 6) && (tam <= 8) ){
            document.getElementById(campo).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
        }
        if ( (tam >= 9) && (tam <= 11) ){
            document.getElementById(campo).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
        }
        if ( (tam >= 12) && (tam <= 14) ){
            document.getElementById(campo).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
        }
        if ( (tam >= 15) && (tam <= 17) ){
            document.getElementById(campo).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
        }
    } else {
        return false;
    }
}
