// ------------------------------------------------
// MessageBox.js
// Author:	jpotter@troupscreeksoftware.com
// Date:	Nov 3, 2006
// Desc:	Message Box Utility
// Copyright 2006 Troups Creek Software LLC
// ------------------------------------------------
// Provides a standard message display area for the application. Messages
// are categorized as level-0 (status), level-1 (info), level-2 (warning), 
// level-3 (error), and level-4 (critical/must-acknowledge). 
// 
// Level-1 to level-3 messages are displayed until they timeout or are replaced 
// by a new message and no user interaction is necessary, although the user may click 
// on the button to dismiss it.
//
// Level-4 messages are displayed until they are acknowledged.
//
// Frequent level-0 (status) messages are displayed separately.
// ------------------------------------------------
// MessageBox::MessageBox
function MessageBox()
{
	// data -------------------	
	this.msgObj = null;		// DHTMLSuite modal message object
	this.msgIcon = ["", "Images/Level1.gif", "Images/Level2.gif", "Images/Level3.gif", "Images/Level4.gif"];
	this.msgLifetime = [0, 5, 10, 15, 90];		// seconds before display is automatically cleared
	this.msgClass = ["", "TCSMsg_level1", "TCSMsg_level2", "TCSMsg_level3", "TCSMsg_level4"];
	this.msgLevelTxt = ["", "Infomation", "Warning", "Error", "Critical Error"];
	this.timerId = 0;

	// -------------------------------------------------------------------------------------------
	//            P U B L I C 
	// -------------------------------------------------------------------------------------------
	// Methods intended for public use.

	// -----------------------------------------
	// MessageBox::clearMsg
	this.clearMsg = function()
	{	// clear the text message & icon
		if(this.timerId != 0)
		{	// we're already timing on a message - kill the timer now
			clearTimeout(this.timerId);
		}
		this.msgObj.close();
	}
	
	// -----------------------------------------
	// MessageBox::displayMsg
	this.displayMsg = function(level, txt)
	{	// show the text and associated icon in the message area
		if(txt != "")
		{	// don't bother for garbage
			var htmlTxt = "";
			if(level == 0)
			{	// just a status message
				this.displayStatus(txt);
			}
			else if(level > 0 && level < 4)
			{	// a message needs to be displayed, but it will time out
				// on its own if the user ignores it
				if(this.timerId != 0)
				{	// we're already timing on one message - kill the timer now
					clearTimeout(this.timerId);
				}
				htmlTxt = this.formatMsg(level, txt);
				htmlTxt += '<div class="TCSMsgTxt"><i>(Automatically dismissed after ';
				htmlTxt += this.msgLifetime[level] + ' seconds.)</i></div>';
				this.msgObj.setHtmlContent(htmlTxt);
				this.msgObj.setCssClassMessageBox(this.msgClass[level]);
				this.msgObj.display();
				this.timerId = setTimeout("gMsg.removeMsg()", this.msgLifetime[level]*1000);
			}
			else if(level == 4)
			{	// user must acknowledge this message - it never times out
				if(this.timerId != 0)
				{	// we're already timing on one message - kill the timer now
					clearTimeout(this.timerId);
				}
				htmlTxt = this.formatMsg(level, txt);
				this.msgObj.setHtmlContent(htmlTxt);
				this.msgObj.setCssClassMessageBox(this.msgClass[level]);
				this.msgObj.display();
			}
		}
	}

	// -------------------------------------------------------------------------------------------
	//            U T I L I T I E S 
	// -------------------------------------------------------------------------------------------
	// Fundamental methods.
	
	// -----------------------------------------
	// MessageBox::init
	this.init = function()
	{	// DHTMLSuite Info/Error message display
		this.msgObj = new DHTMLSuite.modalMessage();
		this.msgObj.setShadowOffset(5);
		this.msgObj.setSize(250,150);
		this.msgObj.setSource(false);	// no html source since we want to use a static message here.
		this.msgObj.setShadowDivVisible(true);	// Enable shadow for these boxes
	}

	// -----------------------------------------
	// MessageBox::clearStatus
	this.clearStatus = function()
	{	// clear the status text
		window.status = "";
	}

	// -----------------------------------------
	// MessageBox::displayStatus
	this.displayStatus = function(txt)
	{	// show the text in the status message area
		window.status = txt;
	}

	// -----------------------------------------
	// MessageBox::formatMsg
	this.formatMsg = function(level, txt)
	{
		var htmlTxt = '<div><h5>' + this.msgLevelTxt[level] + '</h5></div>';
		htmlTxt += '<table cellPadding="0" border="0" cellspacing="0">';
		htmlTxt += '<tr><td id="msgicon" width="45px" valign="top">';
		htmlTxt += '<img height="32" width="32" src="' + this.msgIcon[level] + '">';
		htmlTxt += '</td>';
		htmlTxt += '<td><div id="msgtxt">';
		htmlTxt += txt;
		htmlTxt += '</div></td></tr>';
		htmlTxt += '<tr><td><hr></td><td><hr></td></tr><tr><td></td><td align="center">';
		htmlTxt += '<input id="butAck" type="button" value=" Dismiss " class="detail" name="ack" onclick="gMsg.clearMsg()">'
		htmlTxt += '</td></tr></table>';
		return htmlTxt;
	}

	// -----------------------------------------
	// MessageBox::removeMsg
	this.removeMsg = function()
	{	// it's time to remove the message
		this.timerId = 0;
		this.clearMsg();
	}
}



