if (jsStartLoad('Msg')) {

jsRequire('Utils');
jsRequire('MinToolkit');

/*
To set up msgs:
Either define a variable msgFile with the location of the msg file
or make a file in /Content/msgs/ called {Controller}{View}.txt e.g. for
/Home/Index/ it would be /Content/msgs/HomeIndex.txt. Msg file defines
objects with properties "key" and "text".
Also, define an absolutely positioned element on the page id "msgWindow"
containing "msgKey" and "msgText" elements.
To use:
onclick="PopMsg(key)" where key matches one of the msg objects.
The object's "text" property will be displayed.
Alternatively PopMsg(key, text) will use the given text directly
*/

var msgArray, msgWindow, msgKey, msgText;
var notYet;

if (!msgFile) {
	// assume /x/y/ and assign msgFile = /Content/msg/xy.txt
	var msgFile = window.location.pathname.match(/^\/([^\/]+)\/([^\/]+)/);
	if (msgFile) {
		msgFile = '/Content/msgs/' + msgFile[1] + msgFile[2] + '.txt';
	}
}

function findMsgWindow() {
    msgWindow = ElId('msgWindow');
    msgKey = ElId('msgKey');
    msgText = ElId('msgText');
	if (!msgKey || !msgText)
		msgWindow = null;
}

function msgDocHere(rq) {
	if (!rq.status || (rq.status == 200)) {
        msgArray = eval(rq.responseText);
	}
}

// if text, use that - else search key in msgArray (from file) and use corresponding text
// withOK == 0 or 1
// if withOK popup will be modal, "OK" link added, and centred to window
// if !withOK popup will be fragile and placed on mouse
function PopMsg(key, text, withOK, onOK, withNo, onNo) {
    if (!msgWindow) { findMsgWindow(); }
    if (!msgWindow || (!text && !msgArray)) return false;
    if (!text) {
        for (var i in msgArray) {
            var msgObj = msgArray[i];
            if (msgObj.key == key) {
                text = msgObj.text;
                break;
            }
        }
    }
    withOK = withOK ? 2 : 0;
    if (!text) {
        text = 'no message found';
    }
    if (withOK) {
        text += '<div><a href="#" onclick="'+(onOK?onOK:'')+';UnPop(myBox(this));return false;" class="whiteF">OK</a>'+(withNo?' <a href="#" onclick="'+(onNo?onNo:'')+';UnPop(myBox(this));return false;" class="whiteF">Cancel</a>':'')+'</div>';
    }
    msgKey.innerHTML = key;
    msgText.innerHTML = text;
    PopIt(msgWindow, withOK, !withOK, 1);
    return false;
}
// Popup the msgWindow, centred, withOK, fragile
function PopInfo(key, text) {
    if (!msgWindow) { findMsgWindow(); }
    if (!msgWindow || (!text && !msgArray)) return false;
    msgKey.innerHTML = key;
    msgText.innerHTML = text;
    PopIt(msgWindow,0,0,1);
    setTimeout('UnPop(msgWindow);', 2500);
    return false;
}

addListener(window, '_domload', function() { getRq(msgFile, msgDocHere, 0, 0); });

} // Msg
