﻿function VerificaCampos(formId) {
    var valid = true;

    $(formId + ' .obrigatorio').each(function() {
        if ($(this).val() != null && $(this).val().length <= 0) {
            $(this).prev().css({ 'color': 'red', 'font-weight': 'bold' });
            valid = false;
        }
    });
    if (valid) {
        $(formId).submit();
    }
    else {
        alert('Preencha todos os campos obrigatórios.');
    }
}

function BlockPage(elemento, largura, altura) {
    if (largura == null)
        largura = 600;

    if (altura == null)
        altura = 500;

    $.blockUI({
        message: $(elemento),
        css: {
            width: largura,
            height: altura,
            top: (($(window).height() - altura) / 2),
            left: (($(window).width() - largura) / 2) + $(window).scrollLeft(),
            backgroundColor: '#FFF'
        }
    });
}

function CarregaPaginas(url, parameters, elemento) {
    $.ajax({
        type: 'POST',
        url: url,
        async: false,
        cache: false,
        data: parameters,
        success: function(html) {
            if (elemento != null) {
                $(elemento).empty();
                $(elemento).html(html);
            }
            else {
                alert(html);
            }
        },
        error: function(data, mensagem) {
            var erro = 'Erro ao carregar. Tente novamente.';
            if (elemento != null) {
                $(elemento).empty();
                $(elemento).html(erro);
            }
            else {
                alert(erro);
            }
        }
    });
}

function CKE(id) {
        $('#editor' + id).show();
        CKEDITOR.replace('editor' + id,
        {
            toolbar: 'Admin',
            language: 'pt-br',
            toolbar_Admin: [
                ['TextColor', '-', 'Bold', '-', 'Italic', '-', 'Underline', '-'],
                ['NumberedList', '-', 'BulletedList'],
                ['Link', '-', 'Unlink']
            ],
            width: '550px',
            height: '200px',
            enterMode: CKEDITOR.ENTER_BR,
            forcePasteAsPlainText: true,
            startupFocus: false,
            resize_enabled: false,
            skin: 'kama'
        });
//        CKEDITOR.on('instanceReady', function(ev) {
//            RedimensionaBtSourceCK();
//        });
}    

 

function CloseCKEditorAdmin() {
    try {
        CKEDITOR.instances.editor1.destroy();
    }
    catch (e) {
        alert(e);
    }
}


function activeMenu(id) {
    $(id + ' a').addClass('menuAtivo');
}

function RedimensionaBtSourceCK() {
    $('.cke_skin_kama').each(function() {
        var obj = $(this).find('.cke_toolbar:last');
        obj.css({ 'width': '110px' });
        obj.find('.cke_toolgroup').eq(0).css({ 'width': '110px' });
    });
}

function ChecaValorCKEditor(id, titulo) {
    var texto = $.trim(CKEDITOR.instances["editor" + id].getData().replace('<br />', ''));
    if (texto.length == 0) {
        alert("O campo " + titulo + " está vazio!");
        return false;
    }
    return true;
}
