/*
==============================================================================

  © Copyright 2002-2009 Media Monitors. All Rights Reserved.

  Media Monitors Company Confidential; Media Monitors, 445 Hamilton Ave. White Plains, NY 10601

  This code and all associated files are the proprietary property of Media Monitors.

  No portion nor portions may be used without the express written permission of Media Monitors.

==============================================================================

	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 type="text/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 = "xp_error.gif";
	confirmation.ALERTICON = "icoAlert.gif";
	confirmation.CONFIRMICON = "xp_confirm.gif";

	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 topCanDo (_functionName)
		{
		if (this != top && top[_functionName])
			return true;
		}

	function confirmYesNo (_msg, _yesCode, _noCode, _icon)
		{
		if (topCanDo("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.replace(/\n/g, "<br>");
		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 (topCanDo("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.replace(/\n/g, "<br>");
		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 (topCanDo("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.replace(/\n/g, "<br>");
		confirmation.stack[idx].okayCode = _okayCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmOkayCancel (_msg, _okayCode, _cancelCode, _icon)
		{
		if (topCanDo("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.replace(/\n/g, "<br>");
		confirmation.stack[idx].okayCode = _okayCode;
		confirmation.stack[idx].cancelCode = _cancelCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function confirmWithClose (_msg, _closeCode, _icon)
		{
		if (topCanDo("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.replace(/\n/g, "<br>");
		confirmation.stack[idx].closeCode = _closeCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	function promptForData (_msg, _okayCode, _cancelCode, _defaultValue, _maxlen, _icon)
		{
		if (topCanDo("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.replace(/\n/g, "<br>");
		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, _id)
		{
		if (topCanDo("confirmViewReport"))
			return top.confirmViewReport(_msg, _viewReportCode, _closeCode, _icon);
		var idx = confirmation.stack.length;
		if (confirmation.isBeingShown && confirmation.nowShowingIdx > -1 && confirmation.stack[confirmation.nowShowingIdx].id == _id)
			{
			confirmation.message = document.getElementById("cb_message");
			confirmation.message.innerHTML = (_icon) ? "<table><tr><td valign='top'><img src='images/"+_icon+"'>&nbsp;</td><td>" + _msg + "</td></tr></table>" : _msg;
			return;
			}
		if (_id)
			for (idx = confirmation.nowShowingIdx + 1; idx < confirmation.stack.length; ++idx)
				if (confirmation.stack[idx].id == _id)
					break;
		confirmation.stack[idx] = new Object ();
		confirmation.stack[idx].id = _id;
		confirmation.stack[idx].ctype = confirmation.VIEWREPORT;
		confirmation.stack[idx].msg = _msg.replace(/\n/g, "<br>");
		confirmation.stack[idx].viewReportCode = _viewReportCode;
		confirmation.stack[idx].closeCode = _closeCode;
		confirmation.stack[idx].icon = _icon;
		CONF_showConfirmations();
		}

	var cb_hidingCount = 0;

	function hideSelectBoxes ()
		{
		var is_IE50 = (navigator.appVersion.indexOf("MSIE 5.0") == -1) ? false : true;
		var is_IE55 = (navigator.appVersion.indexOf("MSIE 5.5") == -1) ? false : true;
		var is_IE60 = (navigator.appVersion.indexOf("MSIE 6.0") == -1) ? false : true;
		if (!is_IE50 && !is_IE55 && !is_IE60)
			return;

		if (cb_hidingCount++)
			return;

		var allSelects = document.getElementsByTagName("select");
		//alert(document.location + ": hide, count = "+cb_hidingCount+", selects="+allSelects.length);
		if (allSelects && allSelects.length > 0)
			{
			for (var i = 0; i < allSelects.length; ++i)
				{
				//alert(allSelects[i].id + " " + allSelects[i].hide);
				if (allSelects[i].hide != "no")
					{
					if ((!allSelects[i].saveVisibility || allSelects[i].saveVisibility == "") && allSelects[i].style.visibility)
						allSelects[i].saveVisibility = allSelects[i].style.visibility;
					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].hide != "no")
					{
					if ((!allObjects[i].saveVisibility || allObjects[i].saveVisibility == "") && allObjects[i].style.visibility)
						allObjects[i].saveVisibility = allObjects[i].style.visibility;
					allObjects[i].style.visibility = "hidden";
					}
				}
			}

		for (var i = 0; i < window.frames.length; ++i)
			{
			if (window.frames[i].hideSelectBoxes)
				window.frames[i].hideSelectBoxes();
			}
		//alert("hidden");
		}

	function showSelectBoxes ()
		{
		var is_IE50 = (navigator.appVersion.indexOf("MSIE 5.0") == -1) ? false : true;
		var is_IE55 = (navigator.appVersion.indexOf("MSIE 5.5") == -1) ? false : true;
		var is_IE60 = (navigator.appVersion.indexOf("MSIE 6.0") == -1) ? false : true;
		if (!is_IE50 && !is_IE55 && !is_IE60)
			return;

		if (--cb_hidingCount)
			return;

		var allSelects = document.getElementsByTagName("select");
		//alert(document.location + ": show, count = "+cb_hidingCount+", selects="+allSelects.length);
		if (allSelects && allSelects.length > 0)
			{
			for (var i = 0; i < allSelects.length; ++i)
				{
				if (allSelects[i].hide != "no")
					{
					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)
				{
				if (allObjects[i].hide != "no")
					{
					allObjects[i].style.visibility = (allObjects[i].saveVisibility) ? allObjects[i].saveVisibility : "visible";
					allObjects[i].saveVisibility = null;
					}
				}

		for (var i = 0; i < window.frames.length; ++i)
			if (window.frames[i].showSelectBoxes)
				window.frames[i].showSelectBoxes();
		//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]);
				confirmation.isBeingShown = true;
				break;

			case confirmation.YESNOCANCEL:
				CONF_confirmYesNoCancel(confirmation.stack[idx]);
				confirmation.isBeingShown = true;
				break;

			case confirmation.OK:
				CONF_confirmOkay(confirmation.stack[idx]);
				confirmation.isBeingShown = true;
				break;

			case confirmation.OKCANCEL:
				CONF_confirmOkayCancel(confirmation.stack[idx]);
				confirmation.isBeingShown = true;
				break;

			case confirmation.CLOSE:
				CONF_confirmWithClose(confirmation.stack[idx]);
				confirmation.isBeingShown = true;
				break;

			case confirmation.PROMPT:
				CONF_promptForData(confirmation.stack[idx]);
				confirmation.isBeingShown = true;
				break;

			case confirmation.VIEWREPORT:
				CONF_confirmViewReport(confirmation.stack[idx]);
				confirmation.isBeingShown = true;
				break;
			}
		}

	function CONF_confirmYesNo (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.yesCode = _conf.yesCode;
		confirmation.noCode = _conf.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 (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.yesCode = _conf.yesCode;
		confirmation.noCode = _conf.noCode;
		confirmation.cancelCode = _conf.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 (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.okayCode = _conf.okayCode;
		document.getElementById("cb_ok").style.display = "inline";
		document.getElementById("cb_ok").focus();
		CONF_enableConfirmations();
		}

	function CONF_confirmOkayCancel (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.okayCode = _conf.okayCode;
		confirmation.cancelCode = _conf.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 (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.closeCode = _conf.closeCode;
		document.getElementById("cb_close").style.display = "inline";
		document.getElementById("cb_close").focus();
		CONF_enableConfirmations();
		}

	function CONF_promptForData (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.okayCode = _conf.okayCode;
		confirmation.cancelCode = _conf.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 = (_conf.defaultValue ? _conf.defaultValue : "");
		document.getElementById("cb_data").maxLength = (_conf.maxlen ? _conf.maxlen : 50);
		document.getElementById("cb_data").focus();
		document.getElementById("cb_data").select();
		CONF_enableConfirmations();
		}

	function CONF_confirmViewReport (_conf)
		{
		CONF_disableConfirmations();
		confirmation.box = document.getElementById("confirmationBox");
		confirmation.message = document.getElementById("cb_message");
		confirmation.message.innerHTML = (_conf.icon) ? "<table><tr><td valign='top'><img src='images/"+_conf.icon+"'></td><td>" + _conf.msg + "</td></tr></table>" : _conf.msg;
		CONF_doShowConfirmation();
		confirmation.viewReportCode = _conf.viewReportCode;
		confirmation.closeCode = _conf.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("doHideConfirmation()", confirmation.messageTimeout * 1000)
		}

	function CONF_doShowConfirmation ()
		{
		// initially hide the pop-up until we know its final height;
		// this way we can correctly position it vertically on the screen;

		confirmation.box.style.top = -2000;
		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" background="images/diamondplate.png" onkeypress="event.cancelBubble=true;" style="background-image: url(images/diamondplate.png);">';
		newHTML += '	<br>';
		newHTML += '	<div id="cb_message" style="padding:5px; position:relative; left:-10px; background-image:url(images/halfmask.png); border:1px #EFEFEF solid;">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" width=100%>&nbsp;</td>';
		newHTML += '			<td align="right" width=1><button id="cb_yes"        class="confirm_btn" style="background-color:#40BA54; width:40px;"  onkeypress="return CONF_checkKeypress()" onclick="CONF_goYes()"        ondblclick="CONF_goYes()"        value="yes"          >yes</button></td>';
		newHTML += '			<td align="right" width=1><button id="cb_no"         class="confirm_btn" style="background-color:#BA4040; width:40px;"  onkeypress="return CONF_checkKeypress()" onclick="CONF_goNo()"         ondblclick="CONF_goNo()"         value="no"           >no</button></td>';
		newHTML += '			<td align="right" width=1><button id="cb_ok"         class="confirm_btn" style="background-color:#40BA54; width:20px;"  onkeypress="return CONF_checkKeypress()" onclick="CONF_goOkay()"       ondblclick="CONF_goOkay()"       value="ok"           >ok</button></td>';
		newHTML += '			<td align="right" width=1><button id="cb_cancel"     class="confirm_btn" style="background-color:#BA4040; width:20px;"  onkeypress="return CONF_checkKeypress()" onclick="CONF_goCancel()"     ondblclick="CONF_goCancel()"     value="x"            >x</button></td>';
		newHTML += '			<td align="right" width=1><button id="cb_viewReport" class="confirm_btn" style="background-color:#DB961F; width:100px;" onkeypress="return CONF_checkKeypress()" onclick="CONF_goViewReport()" ondblclick="CONF_goViewReport()" value="view details" >view details</button></td>';
		newHTML += '			<td align="right" width=1><button id="cb_close"      class="confirm_btn" style="background-color:#DB961F; width:40px;"  onkeypress="return CONF_checkKeypress()" onclick="CONF_goClose()"      ondblclick="CONF_goClose()"      value="close"        >close</button></td>';
		newHTML += '		</tr>';
		newHTML += '	</table>';
		newHTML += '</div>';

		document.write(newHTML);
		}

