﻿
// START - Message Manager
//-------------------------------------------------------------------------------------------
var aMsgMngr = new Array();

function DisplayMsg(iIndex, sMsgType, sMessage, sTitle, fnFunction, RedirectURL) {
    if (iIndex === undefined || iIndex == null || iIndex == '') iIndex = 0;
    iIndex = parseInt(iIndex);

    if (iIndex == -1) {
        var counter = 0;
        var modalPopupIndex;
        for (var i in aMsgMngr) {
            if (aMsgMngr[i].mode == 'inline') {
                if (i >= 0) {
                    DisplayMsg(i, sMsgType, sMessage, sTitle, fnFunction, RedirectURL);
                    ++counter;
                }
            }
            else if (aMsgMngr[i].mode == 'modal-popup') {
                iIndex = i;
            }
        }

        if (counter > 0 || iIndex < 0) return;
    }

    var errorMsg = '';
    var iconClass = '';
    var bckgClass = '';
    var msgContiner;

    var mngr = aMsgMngr[iIndex];
    if (mngr === undefined || mngr == null || mngr == '') {
        alert('Invalid message manager index (' + iIndex + ').');
        return;
    }

    var displayMode = mngr.mode;
    if (displayMode === undefined || displayMode == null || displayMode == '') {
        displayMode = 'modal-popup';
        mngr.mode = displayMode;
    }

    if (sMsgType == 'error') {
        iconClass = 'ui-icon ui-icon-alert';
        bckgClass = 'ui-state-error ui-corner-all';
    }
    else {
        errorType = '';
        iconClass = 'ui-icon ui-icon-info';
        bckgClass = 'ui-state-highlight ui-corner-all';
    }

    if (displayMode == 'inline') {
        msgContiner = $('#litInlineErr_' + iIndex);
        $('.msg-manager-inl-text', msgContiner).html(sMessage);

        var backg = $('> div', msgContiner);
        backg.removeClass();
        backg.addClass(bckgClass + ' msg-manager-inl-wrapper');

        var icon = $('span.msg-manager-inl-img', msgContiner);
        icon.removeClass();
        icon.addClass(iconClass + ' msg-manager-inl-img');
        window.scrollTo(0, 0);
        msgContiner.show();
    }
    else if (displayMode == 'modal-popup') {
        msgContiner = $('#litModalErr_' + iIndex);
        $('.msg-manager-modal-text', msgContiner).html(sMessage);

        var backg = $(msgContiner);
        backg.removeClass();
        backg.addClass('ui-state-highlight ui-corner-all');

        var icon = $('span.msg-manager-modal-img', msgContiner);
        icon.removeClass();
        icon.addClass(iconClass + ' msg-manager-modal-img');

        $('#litModalErr_' + iIndex + ':ui-dialog').dialog('destroy');

        var msgWidth = parseInt(mngr.width);
        $(msgContiner).dialog({ modal: true, title: sTitle, zIndex: 9999, resizable: false,
            buttons: { Ok: function() {
                if (RedirectURL != null && RedirectURL != '') {
                    location.href = RedirectURL;
                }
                $(this).dialog('close');
            }
            }
        });

        if (msgWidth > 0) $(msgContiner).dialog("option", "width", msgWidth);

        $(msgContiner).unbind('dialogclose');
        if (fnFunction != null && fnFunction != '') {
            $(msgContiner).bind('dialogclose', fnFunction);
        }

        if (window.DisplayOverlay && window.HideProcessingOverLay) {
            DisplayProcessingOverlay();
            $(msgContiner).bind('dialogclose', function() { HideProcessingOverLay(); });
        }
    }
    else {
        alert(sMessage);
    }
}

// Displays the confirmation message box with two buttons (OK and Cancel).
function DisplayConfirmationMsg(sMessage, sTitle,
            sOKButtonText, fnOKButtonFunction,
            sCancelButtonText, fnCancelButtonFunction) {

    if (aMsgMngr.length <= 0) {
        alert('Error: No message manager found.');
        return;
    }

    var iIndex, mngr;
    for (var i in aMsgMngr) {
        iIndex = parseInt(i);
        mngr = aMsgMngr[i];
        break;
    }

    if (sOKButtonText === undefined || sOKButtonText == null || sOKButtonText == '') sOKButtonText = 'OK';
    if (sCancelButtonText === undefined || sCancelButtonText == null || sCancelButtonText == '') sCancelButtonText = 'Cancel';

    var errorMsg = '';
    var iconClass = '';
    var bckgClass = '';

    var msgContiner = $('#litModalErr_' + iIndex);
    $('.msg-manager-modal-text', msgContiner).html(sMessage);

    var backg = $(msgContiner);
    backg.removeClass();
    backg.addClass('ui-state-highlight ui-corner-all');

    var icon = $('span.msg-manager-modal-img', msgContiner);
    icon.removeClass();
    icon.addClass('ui-icon ui-icon-help msg-manager-modal-img');

    $('#litModalErr_' + iIndex + ':ui-dialog').dialog('destroy');

    var msgWidth = parseInt(mngr.width);
    $(msgContiner).dialog({ modal: true, title: sTitle, zIndex: 9999, resizable: false,
        buttons: [
            {
                text: sOKButtonText,
                click: function() {
                    if (fnOKButtonFunction !== undefined && fnOKButtonFunction != null && fnOKButtonFunction != '') {
                        fnOKButtonFunction();
                    }
                    $(this).dialog('close');
                }
            },
            {
                text: sCancelButtonText,
                click: function() {
                    if (fnCancelButtonFunction !== undefined && fnCancelButtonFunction != null && fnCancelButtonFunction != '') {
                        fnCancelButtonFunction();
                    }
                    $(this).dialog('close');
                }
            }
        ]
    });

    if (msgWidth > 0) $(msgContiner).dialog("option", "width", msgWidth);

    $(msgContiner).unbind('dialogclose');

    if (window.DisplayOverlay && window.HideProcessingOverLay) {
        DisplayProcessingOverlay();
        $(msgContiner).bind('dialogclose', function() { HideProcessingOverLay(); });
    }
}

// Displays the confirmation message box with three buttons (Yes, No and Cancel).
function DisplayConfirmationMsg3(sMessage, sTitle,
            sYesButtonText, fnYesButtonFunction,
            sNoButtonText, fnNoButtonFunction,
            sCancelButtonText, fnCancelButtonFunction) {

    if (aMsgMngr.length <= 0) {
        alert('Error: No message manager found.');
        return;
    }

    var iIndex, mngr;
    for (var i in aMsgMngr) {
        iIndex = parseInt(i);
        mngr = aMsgMngr[i];
        break;
    }

    if (sYesButtonText === undefined || sYesButtonText == null || sYesButtonText == '') sYesButtonText = 'Yes';
    if (sNoButtonText === undefined || sNoButtonText == null || sNoButtonText == '') sNoButtonText = 'No';
    if (sCancelButtonText === undefined || sCancelButtonText == null || sCancelButtonText == '') sCancelButtonText = 'Cancel';

    var errorMsg = '';
    var iconClass = '';
    var bckgClass = '';

    var msgContiner = $('#litModalErr_' + iIndex);
    $('.msg-manager-modal-text', msgContiner).html(sMessage);

    var backg = $(msgContiner);
    backg.removeClass();
    backg.addClass('ui-state-highlight ui-corner-all');

    var icon = $('span.msg-manager-modal-img', msgContiner);
    icon.removeClass();
    icon.addClass('ui-icon ui-icon-help msg-manager-modal-img');

    $('#litModalErr_' + iIndex + ':ui-dialog').dialog('destroy');

    var msgWidth = parseInt(mngr.width);
    $(msgContiner).dialog({ modal: true, title: sTitle, zIndex: 9999, resizable: false,
        buttons: [
        {
            text: sYesButtonText,
            click: function() {
                if (fnYesButtonFunction !== undefined && fnYesButtonFunction != null && fnYesButtonFunction != '') {
                    fnYesButtonFunction();
                }
                $(this).dialog('close');
            }
        },
            {
                text: sNoButtonText,
                click: function() {
                    if (fnNoButtonFunction !== undefined && fnNoButtonFunction != null && fnNoButtonFunction != '') {
                        fnNoButtonFunction();
                    }
                    $(this).dialog('close');
                }
            },
            {
                text: sCancelButtonText,
                click: function() {
                    if (fnCancelButtonFunction !== undefined && fnCancelButtonFunction != null && fnCancelButtonFunction != '') {
                        fnCancelButtonFunction();
                    }
                    $(this).dialog('close');
                }
            }

        ]
    });

    if (msgWidth > 0) $(msgContiner).dialog("option", "width", msgWidth);

    $(msgContiner).unbind('dialogclose');

    if (window.DisplayOverlay && window.HideProcessingOverLay) {
        DisplayProcessingOverlay();
        $(msgContiner).bind('dialogclose', function() { HideProcessingOverLay(); });
    }
}


function HideMsg(iIndex) {

    if (iIndex === undefined || iIndex == null || iIndex == '') iIndex = 0;
    iIndex = parseInt(iIndex);
    if (iIndex == -1) {
        for (var i in aMsgMngr) {
            if (i >= 0 && aMsgMngr[i].mode == 'inline') {
                HideMsg(i);
            }
        }
        return;
    }

    var mngr = aMsgMngr[iIndex];
    if (mngr === undefined || mngr == null || mngr == '') {
        alert('Invalid message manager index (' + iIndex + ').');
        return;
    }

    var displayMode = mngr.mode;
    if (displayMode === undefined || displayMode == null || displayMode == '') displayMode = 'modal-popup';

    if (displayMode == 'inline') {
        $('#litInlineErr_' + iIndex).hide();
    }
    else if (displayMode == 'modal-popup') {
        $('#litModalErr_' + iIndex).dialog('close');
    }
}


