﻿// Collection of functions related to the modal dialogs

/* Function:    alertModal(msg, icon[optional], title[optional])
Usage:       This is used to replace the normal js alert function. 
ALERT!!!:    This function will ALWAYS return false. 
Params:    
msg:            Text to be display in the confirm box.
icon:           1 = info icon, 2 = alert icon. [optional]
title:          Text to be displayed as the title of the confirm box. [optional]
*/
function alertModal(msg, icon, title, callbackOK) {
    jsReplace_Confirm_Show(msg, icon, title);
    var modalDivBtnOK = document.getElementById("mp_jsReplace_Confirm_BtnOK");
    var modalDivBtnCancel = document.getElementById("mp_jsReplace_Confirm_BtnCancel");
    modalDivBtnOK.onclick = jsReplace_Confirm_Hide;
    modalDivBtnCancel.style.display = "none";
    //alert('set focus here');

    if (callbackOK) {
        if (typeof (callbackOK) == 'function') {
            modalDivBtnOK.onclick = function() { jsReplace_Confirm_Hide(); callbackOK(); }
        } else {
            alert('error: incorrect modal set up');
        }
    }


    modalDivBtnOK.focus();
    return false;
}

/* Function:    confirmModal(callback, msg, icon[optional], title[optional], callbackCancel[optional])
Usage:       This is used to replace the normal js confirm function. 
ALERT!!!:    This function will ALWAYS return false. 
Params:    
callbackOK:       When confirmModal is used on the onclientclick="return confirmModal...) 
the callback param should be set to 'this'.
When confirmModal is used within another piece of javascript, callback should be 
the name of the js function to call when OK is pressed.
This cannot be null
msg:            Text to be display in the confirm box.
icon:           1 = info icon, 2 = alert icon. [optional]
title:          Text to be displayed as the title of the confirm box. [optional]
callbackCancel: Same as the callbackOK param. [optional]      
*/
function confirmModal(callbackOK, msg, commandArgOK, icon, title, callbackCancel, commandArgCancel) {
    jsReplace_Confirm_Show(msg, icon, title);
    var modalDivBtnOK = document.getElementById("mp_jsReplace_Confirm_BtnOK");
    var modalDivBtnCancel = document.getElementById("mp_jsReplace_Confirm_BtnCancel");
    if (typeof (callbackOK) == 'function') {
        modalDivBtnOK.onclick = function() { jsReplace_Confirm_Hide(); callbackOK(); }
    } else {
        if (!commandArgOK) {
            commandArgOK = '';
        }
        modalDivBtnOK.onclick = function() { jsReplace_Confirm_Hide(); __doPostBack(callbackOK.name, commandArgOK); }
    }
    modalDivBtnCancel.style.display = "inline";
    modalDivBtnCancel.onclick = jsReplace_Confirm_Hide;
    if (callbackCancel) {
        if (typeof (callbackCancel) == 'function') {
            modalDivBtnCancel.onclick = function() { jsReplace_Confirm_Hide(); callbackCancel(); }
        } else {
            if (!commandArgCancel) {
                commandArgCancel = '';
            }
            modalDivBtnCancel.onclick = function() { jsReplace_Confirm_Hide(); __doPostBack(callbackCancel.name, commandArgCancel); }
        }
    }

    modalDivBtnOK.focus();
    return false;
}

function jsReplace_Confirm_Show(msg, icon, title) {
    var modalDiv = document.getElementById("mp_jsReplace_Confirm");
    var modalDivMsg = document.getElementById("mp_jsReplace_Confirm_msg");
    var modalDivTitle = document.getElementById("mp_jsReplace_Confirm_title");
    var modalDivIcon = document.getElementById("mp_jsReplace_Confirm_icon");
    modalDivMsg.innerHTML = msg
    window.onscroll = function() { modalDiv.style.top = document.body.scrollTop; };
    modalDiv.style.display = "block";
    modalDiv.style.top = document.body.scrollTop;
    modalDivTitle.style.display = "none";
    if (icon) {
        modalDivIcon.src = "/images/Modal/" + icon + ".gif";
    }
    if (title) {
        modalDivTitle.innerHTML = title;
        modalDivTitle.style.display = "block";
    }
}

function jsReplace_Confirm_Hide() {
    document.getElementById("mp_jsReplace_Confirm").style.display = "none";
}