﻿function msgAlerta(titulo, mensagem, tipo) {

    switch (tipo) {
        case 'success':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification success').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'warning':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification warning').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'error':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification error').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'info':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification info').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'tip':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification tip').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'secure':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification secure').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'message':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification message').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'download':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification download').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'purchase':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification purchase').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        case 'print':
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification print').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
        default:
            $('#overlay').fadeIn('fast', function () {
                $('#Alerta').attr('class', 'notification message').css({ display: 'block' }).animate({ 'top': '160px' }, 500);
                $('#msgAlerta').html('<strong>' + titulo + '</strong>' + mensagem);
            });
            break;
    }


    // Botão X que fecha a janela e manda ela embora.
    $('.notification span').click(function () {
        $('#Alerta').animate({ 'top': '-200px' }, 800, function () {
            $(this).parents('.notification').fadeOut(800);
            $('#Alerta').fadeOut(400).empty();
            $('#Alerta').html(
            " <span></span> " +
            " <div class='text'> " +
            "     <p id='msgAlerta'></p> " +
            " </div> "
            );
            $('#overlay').fadeOut(800);
        });
    });
}

//----------- Functions do modalPopUp


//   Limitar tamanho textbox ------------------------------------------------------------------------------------------------------------------
function doKeypress(textBox) {
    maxLength = textBox.attributes["maxLength"].value;
    value = textBox.value;

    if (maxLength && value.length > maxLength) {
        textBox.value = textBox.value.substring(0, maxLength);
    }
}


//---------------------------------------------------------------------------------------------------------------------------------------------
function showModalPopup(url, title, params, scriptAoFechar) {

    var widthModal = 980;
    var heightModal = 550;
    var isShowPrinter = false;

    if (params != null && typeof params != "undefined") {
        var params = params.split(",");

        for (var i = 0; i <= params.length; i++) {
            if (typeof params[i] != "undefined" && params[i].toLowerCase().indexOf("height") > -1)
                heightModal = parseInt(params[i].split("=")[1].toLowerCase());

            else if (typeof params[i] != "undefined" && params[i].toLowerCase().indexOf("width") > -1)
                widthModal = parseInt(params[i].split("=")[1].toLowerCase());

            else if (typeof params[i] != "undefined" && params[i].toLowerCase().indexOf("showprinter") > -1)
                isShowPrinter = eval(params[i].split("=")[1].toLowerCase());
        }
    }

    var areaPrincipal = document.createElement("div");
    areaPrincipal.id = "areaPrincipal";
    document.body.appendChild(areaPrincipal);

    var areaModal = document.createElement("div");
    areaModal.id = "areaModal";
    areaModal.className = "window";
    areaModal.style.width = widthModal + "px";
    areaModal.style.height = heightModal + "px";
    areaPrincipal.appendChild(areaModal);

    var areaTopoModal = document.createElement("div");
    areaTopoModal.id = "areaTopoModal";
    areaTopoModal.innerHTML = ((isShowPrinter) ? "<a href='#' id='imprimirModal' class='imprimirModal' onclick=\"$get('iframeModal').contentWindow.focus(); $get('iframeModal').contentWindow.print()\">imprimir</a>&nbsp;" : "") + "<a href='#' id='fecharModal' class='fecharModal'>Fechar</a>";
    areaModal.appendChild(areaTopoModal);

    var areaCarregando = document.createElement("div");
    areaCarregando.id = "areaCarregando";
    areaCarregando.className = "areaCarregando";
    areaCarregando.innerHTML = "     Carregando...";
    areaModal.appendChild(areaCarregando);

    var iframeModal = document.createElement("iframe");
    iframeModal.id = "iframeModal";

    if (!document.all)
        iframeModal.onload = function () { areaCarregando.parentNode.removeChild(areaCarregando); }

    else
        iframeModal.onreadystatechange = function () { if (iframeModal.readyState == "complete") areaCarregando.parentNode.removeChild(areaCarregando); }

    iframeModal.setAttribute("frameborder", "no");
    iframeModal.src = url;
    iframeModal.style.width = "100%";
    areaModal.appendChild(iframeModal);

    $("#iframeModal").css('height', $("#areaModal").height() - 20);

    var areaMask = document.createElement("div");
    areaMask.id = "mask";
    areaPrincipal.appendChild(areaMask);

    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    $('#mask').fadeIn(0);
    $('#mask').fadeTo("slow", 0.6);

    var winH = $(window).height();
    var winW = $(window).width();

    $("#areaModal").css('top', winH / 2 - $("#areaModal").height() / 2);
    $("#areaModal").css('left', winW / 2 - $("#areaModal").width() / 2);

    $("#areaModal").fadeIn(0);

    setTimeout(function () { $("body").attr("oldscroll", $("html").scrollTop()); $("body").css("overflow", "hidden"); if (window.parent.location.href != self.location.href) { var oTopoModalParent = window.parent.document.getElementById("areaTopoModal"); if (oTopoModalParent != null) oTopoModalParent.style.visibility = "hidden"; } redimensionarModal(); }, 0);

    setTimeout(function () { $("html").scrollTop(0); $("body").scrollTop(0); }, 50);
    setTimeout(function () { $("html").scrollTop(0); $("body").scrollTop(0); }, 100);
    setTimeout(function () { $("html").scrollTop(0); $("body").scrollTop(0); }, 200);
    setTimeout(function () { $("html").scrollTop(0); $("body").scrollTop(0); }, 300);
    setTimeout(function () { $("html").scrollTop(0); $("body").scrollTop(0); }, 400);

    $("#fecharModal").click(function (e) {
        if (scriptAoFechar != null && scriptAoFechar != '' && scriptAoFechar != 'null')
            eval(scriptAoFechar);
        e.preventDefault();
        setTimeout(function () { $("body").css("overflow", "auto"); $("html").scrollTop(parseInt($("body").attr("oldscroll"))) }, 0);
        if (window.parent.location.href != self.location.href) {
            var oTopoModalParent = window.parent.document.getElementById("areaTopoModal");
            if (oTopoModalParent != null)
                oTopoModalParent.style.visibility = "visible";
        }
        document.body.removeChild(document.getElementById("areaPrincipal"));
    });
}

window.onresize = function () { redimensionarModal(); };


//---------------------------------------------------------------------------------------------------------------------------------------------
function redimensionarModal() {
    try {
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        var winH = $(window).height();
        var winW = $(window).width();

        $("#areaModal").css('top', winH / 2 - $("#areaModal").height() / 2);
        $("#areaModal").css('left', winW / 2 - $("#areaModal").width() / 2);
    }
    catch (e) { }
}


//---------------------------------------------------------------------------------------------------------------------------------------------
function showModalBlocked(opacity, aviso, redirect) {
    var opacity = (opacity == null) ? 0.0 : opacity;

    var areaPrincipal = document.createElement("div");
    areaPrincipal.id = "areaPrincipal";
    document.body.appendChild(areaPrincipal);

    var areaMask = document.createElement("div");
    areaMask.id = "mask";
    areaPrincipal.appendChild(areaMask);

    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

    //$('#mask').fadeIn(0);
    $('#mask').fadeTo("fast", opacity);

    if (aviso != null) {
        setTimeout(function () { alert(aviso); if (redirect != null) location.href = redirect; }, 500);
    }
}


//---------------------------------------------------------------------------------------------------------------------------------------------
function closeModalPopup() {
    if (window.parent.location.href != self.location.href) {
        var oTopoModalParent = window.parent.document.getElementById("areaTopoModal");

        if (oTopoModalParent != null) {
            oTopoModalParent.style.visibility = "visible";
        }
    }
    window.parent.document.body.removeChild(window.parent.document.getElementById("areaPrincipal"));
}





