/*

	Code for inserting, activating and accessing various confirmation boxes
	with selectable buttons from the set { Yes|No|OK|Cancel } and optional text entry field

	Required files:

		script/confirm.js [this file]
		css/confirm.css
		images/bgdialog1.gif

	Syntax for use:

	<link rel="stylesheet" text="text/css" href="css/confirm.css"></link>
	<script language="javascript" src="script/confirm.js"></script>

	e.g.,

	<script>confirmYesNo("Are you sure", "yesFunctionName()", "noFunctionName()");</script>
	<script>confirmOkayCancel("About to delete all data ... this cannot be undone", "yesFunctionName()", "noFunctionName()");</script>
	<script>confirmOkay("No data was found", "yesFunctionName()");</script>
	<script>promptForData("Please enter your last name", "yesFunctionName()", "noFunctionName()", "Smith", "28");</script>

*/

// public:

	var confirmation = new Object ();
	confirmation.allowEnabling = true;
	confirmation.enabled = confirmation.allowEnabling ? false : true;
	confirmation.nowShowingIdx = -1;
	confirmation.isBeingShown = false;

	confirmation.YESNO = 0;
	confirmation.YESNOCANCEL = 1;
	confirmation.OK = 2;
	confirmation.OKCANCEL = 3;
	confirmation.CLOSE = 4;
	confirmation.PROMPT = 5;
	confirmation.VIEWREPORT = 6;

	confirmation.ERRORICON = "jc_error.gif";
	confirmation.ALERTICON = "jc_warning.gif";
	confirmation.CONFIRMICON = "jc_query.gif";
	confirmation.OKICON = "jc_ok.png";

	var Keyboard = new Object ();
	Keyboard.ENTER_KEY = 13;
	Keyboard.ENTER = 13;
	Keyboard.SPACE = 32;
	Keyboard.TAB = 9;
	Keyboard.UP_ARROW = 38;
	Keyboard.DOWN_ARROW = 40;
	Keyboard.DELETE = 46;
	Keyboard.ESC = 27;

	confirmation.messageTimeout = 0;		// for OK box only!

	confirmation.stack = new Array ();

	function confirmYesNo (_msg, _yesCode, _noCode, _icon)
		{
		if (this != top && top.confirmYesNo)
			return top.confirmYesNo(_msg, _yesCode, _noCode, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.YESNO;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].yesCode = _yesCode;
		confirmation.stack[idx].noCode = _noCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmYesNoCancel (_msg, _yesCode, _noCode, _cancelCode, _icon)
		{
		if (this != top && top.confirmYesNoCancel)
			return top.confirmYesNoCancel(_msg, _yesCode, _noCode, _cancelCode, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.YESNOCANCEL;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].yesCode = _yesCode;
		confirmation.stack[idx].noCode = _noCode;
		confirmation.stack[idx].cancelCode = _cancelCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmOkay (_msg, _okayCode, _icon)
		{
		//alert("confirmOkay called");
		if (this != top && top.confirmOkay)
			return top.confirmOkay(_msg, _okayCode, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.OK;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].okayCode = _okayCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmOkayCancel (_msg, _okayCode, _cancelCode, _icon)
		{
		if (this != top && top.confirmOkayCancel)
			return top.confirmOkayCancel(_msg, _okayCode, _cancelCode, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.OKCANCEL;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].okayCode = _okayCode;
		confirmation.stack[idx].cancelCode = _cancelCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmWithClose (_msg, _closeCode, _icon)
		{
		if (this != top && top.confirmWithClose)
			return top.confirmWithClose(_msg, _closeCode, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.CLOSE;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].closeCode = _closeCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function promptForData (_msg, _okayCode, _cancelCode, _defaultValue, _maxlen, _icon)
		{
		if (this != top && top.promptForData)
			return top.promptForData(_msg, _okayCode, _cancelCode, _defaultValue, _maxlen, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.PROMPT;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].okayCode = _okayCode;
		confirmation.stack[idx].cancelCode = _cancelCode;
		confirmation.stack[idx].defaultValue = _defaultValue;
		confirmation.stack[idx].maxlen = _maxlen;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmViewReport (_msg, _viewReportCode, _closeCode, _icon)
		{
		if (this != top && top.confirmViewReport)
			return top.confirmViewReport(_msg, _viewReportCode, _closeCode, _icon);
		var idx = confirmation.stack.length;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].ctype = confirmation.VIEWREPORT;
		confirmation.stack[idx].msg = _msg;
		confirmation.stack[idx].viewReportCode = _viewReportCode;
		confirmation.stack[idx].closeCode = _closeCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	var cb_hidingCount = 0;

	function hideSelectBoxes ()
		{
		//alert("hide - "+cb_hidingCount);
		if (cb_hidingCount++)
			return;
		var allSelects = top.document.getElementsByTagName("select");
		if (allSelects && allSelects.length)
			{
			for (var i = 0; i < allSelects.length; ++i)
				{
				if ((!allSelects[i].saveVisibility || allSelects[i].saveVisibility == "") && allSelects[i].style.visibility)
					allSelects[i].saveVisibility = allSelects[i].style.visibility;
				if (allSelects[i].hide != "no")
					allSelects[i].style.visibility = "hidden";
				}
			}
		var allObjects = document.getElementsByTagName("object");
		if (allObjects && allObjects.length)
			{
			for (var i = 0; i < allObjects.length; ++i)
				{
				if ((!allObjects[i].saveVisibility || allObjects[i].saveVisibility == "") && allObjects[i].style.visibility)
					allObjects[i].saveVisibility = allObjects[i].style.visibility;
				if (allObjects[i].hide != "no")
					allObjects[i].style.visibility = "hidden";
				}
			}
		//alert("hidden");
		}

	function showSelectBoxes ()
		{
		//alert("show - "+cb_hidingCount);
		if (--cb_hidingCount)
			return;
		var allSelects = top.document.getElementsByTagName("select");
		if (allSelects && allSelects.length)
			for (var i = 0; i < allSelects.length; ++i)
				{
				allSelects[i].style.visibility = (allSelects[i].saveVisibility) ? allSelects[i].saveVisibility : "visible";
				allSelects[i].saveVisibility = null;
				}
		var allObjects = document.getElementsByTagName("object");
		if (allObjects && allObjects.length)
			for (var i = 0; i < allObjects.length; ++i)
				{
				allObjects[i].style.visibility = (allObjects[i].saveVisibility) ? allObjects[i].saveVisibility : "visible";
				allObjects[i].saveVisibility = null;
				}
		//alert("shown");
		}

	function partialEscape (_text)
		{
		return _text.replace(/\%/g, "%25").replace(/'/g, "%27").replace(/\"/g, "%22").replace(/\\/g, "%5C");
		}

// private:

	function CONF_showConfirmations ()
		{
		if (confirmation.nowShowingIdx >= confirmation.stack.length - 1)
			return;

		if (confirmation.isBeingShown)
			return;

		var idx = ++confirmation.nowShowingIdx;
		//alert("now showing msg " + (idx+1) + " of " + confirmation.stack.length);
		switch (confirmation.stack[idx].ctype)
			{
			case confirmation.YESNO:
				CONF_confirmYesNo(confirmation.stack[idx].msg, confirmation.stack[idx].yesCode, confirmation.stack[idx].noCode, confirmation.stack[idx].icon);
				confirmation.isBeingShown = true;
				break;

			case confirmation.YESNOCANCEL:
				CONF_confirmYesNoCancel(confirmation.stack[idx].msg, confirmation.stack[idx].yesCode, confirmation.stack[idx].noCode, confirmation.stack[idx].cancelCode, confirmation.stack[idx].icon)
				confirmation.isBeingShown = true;
				break;

			case confirmation.OK:
				CONF_confirmOkay(confirmation.stack[idx].msg, confirmation.stack[idx].okayCode, confirmation.stack[idx].icon);
				confirmation.isBeingShown = true;
				break;

			case confirmation.OKCANCEL:
				CONF_confirmOkayCancel(confirmation.stack[idx].msg, confirmation.stack[idx].okayCode, confirmation.stack[idx].cancelCode, confirmation.stack[idx].icon);
				confirmation.isBeingShown = true;
				break;

			case confirmation.CLOSE:
				CONF_confirmWithClose(confirmation.stack[idx].msg, confirmation.stack[idx].closeCode, confirmation.stack[idx].icon);
				confirmation.isBeingShown = true;
				break;

			case confirmation.PROMPT:
				CONF_promptForData(confirmation.stack[idx].msg, confirmation.stack[idx].okayCode, confirmation.stack[idx].cancelCode, confirmation.stack[idx].defaultValue, confirmation.stack[idx].maxlen, confirmation.stack[idx].icon);
				confirmation.isBeingShown = true;
				break;

			case confirmation.VIEWREPORT:
				CONF_confirmViewReport(confirmation.stack[idx].msg, confirmation.stack[idx].viewReportCode, confirmation.stack[idx].closeCode, confirmation.stack[idx].icon);
				confirmation.isBeingShown = true;
				break;
			}
		}

	function CONF_confirmYesNo (_msg, _yesCode, _noCode, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.yesCode = _yesCode;
		confirmation.noCode = _noCode;
		document.getElementById("cb_yes").style.display = "inline";
		document.getElementById("cb_no").style.display = "inline";
		document.getElementById("cb_yes").focus();
		CONF_enableConfirmations();
		}

	function CONF_confirmYesNoCancel (_msg, _yesCode, _noCode, _cancelCode, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.yesCode = _yesCode;
		confirmation.noCode = _noCode;
		confirmation.cancelCode = _cancelCode;
		document.getElementById("cb_yes").style.display = "inline";
		document.getElementById("cb_no").style.display = "inline";
		document.getElementById("cb_cancel").style.display = "inline";
		document.getElementById("cb_yes").focus();
		CONF_enableConfirmations();
		}

	function CONF_confirmOkay (_msg, _okayCode, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.okayCode = _okayCode;
		document.getElementById("cb_ok").style.display = "inline";
		document.getElementById("cb_ok").focus();
		CONF_enableConfirmations();
		}

	function CONF_confirmOkayCancel (_msg, _okayCode, _cancelCode, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.okayCode = _okayCode;
		confirmation.cancelCode = _cancelCode;
		document.getElementById("cb_ok").style.display = "inline";
		document.getElementById("cb_cancel").style.display = "inline";
		document.getElementById("cb_ok").focus();
		CONF_enableConfirmations();
		}

	function CONF_confirmWithClose (_msg, _closeCode, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.closeCode = _closeCode;
		document.getElementById("cb_close").style.display = "inline";
		document.getElementById("cb_close").focus();
		CONF_enableConfirmations();
		}

	function CONF_promptForData (_msg, _okayCode, _cancelCode, _defaultValue, _maxlen, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.okayCode = _okayCode;
		confirmation.cancelCode = _cancelCode;
		document.getElementById("cb_ok").style.display = "inline";
		document.getElementById("cb_cancel").style.display = "inline";
		document.getElementById("cb_input").style.display = "inline";
		document.getElementById("cb_data").value = (_defaultValue ? _defaultValue : "");
		document.getElementById("cb_data").maxLength = (_maxlen ? _maxlen : 50);
		document.getElementById("cb_data").focus();
		document.getElementById("cb_data").select();
		CONF_enableConfirmations();
		}

	function CONF_confirmViewReport (_msg, _viewReportCode, _closeCode, _icon)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'></td><td>" + _msg + "</td></tr></table>" : _msg;
		CONF_doShowConfirmation();
		confirmation.viewReportCode = _viewReportCode;
		confirmation.closeCode = _closeCode;
		document.getElementById("cb_viewReport").style.display = "inline";
		document.getElementById("cb_close").style.display = "inline";
		document.getElementById("cb_close").focus();
		CONF_enableConfirmations();
		}

	function CONF_enableConfirmations ()
		{
		if (confirmation.allowEnabling)
			setTimeout("confirmation.enabled = true;", 50);
		}

	function CONF_disableConfirmations ()
		{
		if (confirmation.allowEnabling)
			confirmation.enabled = false;
		}

	function CONF_clearButtons ()
		{
		document.getElementById("cb_yes").style.display = "none";
		document.getElementById("cb_no").style.display = "none";
		document.getElementById("cb_ok").style.display = "none";
		document.getElementById("cb_cancel").style.display = "none";
		document.getElementById("cb_input").style.display = "none";
		document.getElementById("cb_viewReport").style.display = "none";
		document.getElementById("cb_close").style.display = "none";
		}

	function CONF_clearCallbacks ()
		{
		confirmation.yesCode = "";
		confirmation.noCode = "";
		confirmation.okayCode = "";
		confirmation.cancelCode = "";
		confirmation.closeCode = "";
		confirmation.viewReportCode = "";
		}

	function CONF_setCloser ()
		{
		if (confirmation.messageTimeout)
			confirmation.messageTimer = setTimeout("CONF_doHideConfirmation()", confirmation.messageTimeout * 1000)
		}

	function CONF_doShowConfirmation ()
		{
		confirmation.box.style.top = Math.max(0, document.body.scrollTop + (document.body.clientHeight / 2) - (confirmation.box.clientHeight / 1.25));
		confirmation.box.style.display = "inline";
		hideSelectBoxes();
		CONF_clearButtons();
		CONF_clearCallbacks();
		setTimeout("confirmation.box.style.top = Math.max(0, document.body.scrollTop + (document.body.clientHeight / 2) - (confirmation.box.clientHeight / 1.25));", 1);
		CONF_setCloser();
		}

	function CONF_doHideConfirmation ()
		{
		confirmation.box.style.display = 'none';
		confirmation.isBeingShown = false;
		CONF_showConfirmations();
		showSelectBoxes();
		}

	function CONF_checkKeypress ()
		{
		//alert(event.keyCode);
		if (confirmation.enabled == false)
			return false;
		if (event.keyCode == Keyboard.ESC)
			{
			if (confirmation.cancelCode > "")
				CONF_goCancel();
			else if (confirmation.closeCode > "")
				CONF_goClose();
			else if (confirmation.noCode > "")
				CONF_goNo();
			else if (confirmation.okayCode > "")	// no cancel/close/no button
				CONF_goOkay();
			else									// no callbacks
				CONF_doHideConfirmation();
			}
		else if (event.keyCode == Keyboard.ENTER)
			{
			confirmation.data = document.getElementById("cb_data").value;
			if (confirmation.yesCode > "")
				CONF_goYes();
			else if (confirmation.okayCode > "")
				CONF_goOkay();
			else if (confirmation.viewReportCode > "")
				CONF_goViewReport();
			}
		return true;
		}

	function CONF_goYes ()
		{
		confirmation.data = document.getElementById("cb_data").value;
		eval(confirmation.yesCode);
		CONF_doHideConfirmation();
		}

	function CONF_goNo ()
		{
		eval(confirmation.noCode);
		CONF_doHideConfirmation();
		}

	function CONF_goOkay ()
		{
		confirmation.data = document.getElementById("cb_data").value;
		eval(confirmation.okayCode);
		CONF_doHideConfirmation();
		}

	function CONF_goCancel ()
		{
		eval(confirmation.cancelCode);
		CONF_doHideConfirmation();
		}

	function CONF_goClose ()
		{
		eval(confirmation.closeCode);
		CONF_doHideConfirmation();
		}

	function CONF_goViewReport ()
		{
		eval(confirmation.viewReportCode);
		CONF_doHideConfirmation();
		}

	/* *   D I R E C T   C O D E   * */

	if (this == top || !top.confirmYesNo)
		{
		var newHTML = '<!-- CONFIRMATION BOX -->';
		newHTML += '<div id="confirmationBox" class="confirm_dialog" onkeypress="event.cancelBubble=true;" return CONF_checkKeypress()">';
		newHTML += '	<br>';
		newHTML += '	<div id="cb_message" style="padding-right:10px;">Okay?</div>';
		newHTML += '	<br><br>';
		newHTML += '	<div id="cb_input">';
		newHTML += '		<input type="text" id="cb_data" onselectstart="event.cancelBubble=true;" onkeypress="return CONF_checkKeypress()"></input>';
		newHTML += '	</div>';
		newHTML += '	<br>';
		newHTML += '	<table cellspacing=5 cellpadding=1 width=100%>';
		newHTML += '		<tr>';
		newHTML += '			<td align="right">';
		newHTML += '				&nbsp;<button id="cb_yes"        class="confirm_btn" onkeypress="return CONF_checkKeypress()" onclick="CONF_goYes()"        ondblclick="CONF_goYes()"         > Yes </button>';
		newHTML += '				&nbsp;<button id="cb_no"         class="confirm_btn" onkeypress="return CONF_checkKeypress()" onclick="CONF_goNo()"         ondblclick="CONF_goNo()"          > No </button>';
		newHTML += '				&nbsp;<button id="cb_ok"         class="confirm_btn" onkeypress="return CONF_checkKeypress()" onclick="CONF_goOkay()"       ondblclick="CONF_goOkay()"        > OK </button>';
		newHTML += '				&nbsp;<button id="cb_cancel"     class="confirm_btn" onkeypress="return CONF_checkKeypress()" onclick="CONF_goCancel()"     ondblclick="CONF_goCancel()"      > Cancel </button>';
		newHTML += '				&nbsp;<button id="cb_viewReport" class="confirm_btn" onkeypress="return CONF_checkKeypress()" onclick="CONF_goViewReport()" ondblclick="CONF_goViewReport()" style="width:100px;"> View Details </button>';
		newHTML += '				&nbsp;<button id="cb_close"      class="confirm_btn" onkeypress="return CONF_checkKeypress()" onclick="CONF_goClose()"      ondblclick="CONF_goClose()"       > Close </button>';
		newHTML += '			</td>';
		newHTML += '		</tr>';
		newHTML += '	</table>';
		newHTML += '</div>';

		document.write(newHTML);
		}


