// This file is shared/linked. Be careful when editing! MRG 12/18/07

var m_curModalDialog = 0;		// id of div of any open modalDialog, or 0

var m_isIe = (navigator.userAgent.toLowerCase().indexOf('msie') >= 0);

var m_overlayDiv = 0;	// the gray background

var m_resetModelDialogSize = 0;
var m_scrollBarsHidden = false;

// Note: m_doctypeDtdStrict will be set external to this file,
// based on the value read in from the ColdFusion request scope.

/**
 * modalDialogName should be a div with class "modalDialog",
 * which means visibility = hidden (and borders, etc)
 */
function showAsModalDialog(modalDialogName, topPos, leftPos) {
	var modalDialog = getRawObject(modalDialogName);
	
	if (m_overlayDiv == 0) {
		m_overlayDiv = document.createElement("div");
		m_overlayDiv.setAttribute("id", "modalOverlay");
		m_overlayDiv.className = "modalOverlay";
		document.body.appendChild(m_overlayDiv);

		if (m_isIe) {
			// We must get a usable reference to the div
			// now that the div was inserted into the document - IE issue.
			m_overlayDiv = getRawObject("modalOverlay");
		}
	}
	
	if (modalDialog.style.visibility == "visible") {
		// Close modalDialog if trying to activate a second time...
		closeModalDialog();
		return;
	}

	// Close already-open modalDialog first
	closeModalDialog();

	// Get the overlay up and covering as much of the
	// document as we need to. 
	// We use a different style for IE because it doesn't
	// have a way to get the document size, and because
	// FF doesn't like toggling the scrollbars off (which
	// is the core of our IE work-around)
	var scrolled = scrolledAmt();
	var windowSize = getWindowSize();
	
	if (m_isIe) {
		if (m_doctypeDtdStrict == "strict") {
			// IE in strict mode supports 
			// fixed well, so there is no need
			// to put the overlay over the view-port: fixed positioning
			// does this automatically.
			m_overlayDiv.style.top = "0px";
			m_overlayDiv.style.left = "0px";
		} else if (m_doctypeDtdStrict == "ie6Loose") {
			// Hide scroll bars for IE, because IE scrolls the overlay
			// (which hiding the scrollbars prevents) but FF incorrectly(?)
			// scrolls the window to the top if scrollbars are hidden! MRG 8/3/06
			
			document.body.parentNode.style.overflow = 'hidden';
			m_scrollBarsHidden = true;
	
			// Place the modal overlay at the current scrolled position
			m_overlayDiv.style.top = document.documentElement.scrollTop + "px";
			m_overlayDiv.style.left = document.documentElement.scrollLeft + "px";			
		} else {
			// Hide scroll bars for IE, because IE scrolls the overlay
			// (which hiding the scrollbars prevents) but FF incorrectly(?)
			// scrolls the window to the top if scrollbars are hidden! MRG 8/3/06
			document.body.style.overflow = 'hidden';
			m_scrollBarsHidden = true;
	
			// Place the modal overlay at the current scrolled position
			m_overlayDiv.style.top = scrolled.y + "px";
			m_overlayDiv.style.left = scrolled.x + "px";			
		}
		
		m_overlayDiv.style.height = windowSize.height; // window size
		m_overlayDiv.style.width = "100%"; // window size
		
		// IE will not put anything above SELECT combo boxes,
		// no matter what the z-order says, so we have to hide them. 
		// Lame! MRG 8/18/06
		_modalDialogHideSelects(true);
	} else {
		// Place the modal overlay over the entire document
		m_overlayDiv.style.top = "0px";
		m_overlayDiv.style.left = "0px";
		var docSize = getObjectSize(document.body);	// works in FF, not IE
		//m_overlayDiv.style.height = Math.max(docSize.height, windowSize.height);
		m_overlayDiv.style.height = document.body.scrollHeight;
		m_overlayDiv.style.width = Math.max(docSize.width, windowSize.width);
	}
	m_overlayDiv.style.visibility = "visible";

	// Place the modalDialog in the center of the screen.
	// Note: the div has to be visible before we can get its size...
	modalDialog.style.visibility = "visible";
	
	var modalDialogSize = getObjectSize(modalDialog);
	if (topPos) {
		var dialogTop = topPos;
	} else {
		var dialogTop = Math.round((windowSize.height - modalDialogSize.height)/2);
	}
	
	if (leftPos) {
		var dialogLeft = leftPos;
	} else {
		var dialogLeft = Math.round((windowSize.width - modalDialogSize.width)/2);
	}
	var minMarginAmt = 5;
	
	
	if (m_doctypeDtdStrict == "ie6Loose") {		
		modalDialog.style.top = (document.documentElement.scrollTop + ((dialogTop > minMarginAmt) ? dialogTop : minMarginAmt)) + "px";
		modalDialog.style.left = (document.documentElement.scrollLeft + ((dialogLeft > minMarginAmt) ? dialogLeft : minMarginAmt)) + "px";
	} else {
		modalDialog.style.top = (scrolled.y + ((dialogTop > minMarginAmt) ? dialogTop : minMarginAmt)) + "px";
		modalDialog.style.left = (scrolled.x + ((dialogLeft > minMarginAmt) ? dialogLeft : minMarginAmt)) + "px";
	}

	if (m_isIe) {
		// For IE, since we've removed the scroll bars we want to make sure
		// that the entire modal dialog is visible to the user. If it isn't
		// then we'll resize the modal dialog and add scroll bars to the modal 
		// div itself.
		var modalDialogSize = getObjectSize(modalDialog);
		var maxHeight = windowSize.height - minMarginAmt*2;
		var maxWidth = windowSize.width - minMarginAmt*2;
		if (m_scrollBarsHidden && ((modalDialogSize.height > maxHeight) || (modalDialogSize.width > maxWidth))) {
			//alert(modalDialogSize.height + " vs " + maxHeight + ", " + modalDialogSize.width + " vs " + maxWidth);

			// Save current "size" (however it is defined) so we can restore it later
			m_resetModelDialogSize = { height: modalDialog.style.height, width: modalDialog.style.width };
			
			// Reduce the height & width as needed to fit and add scroll bars
			if (modalDialogSize.height > maxHeight) {
				modalDialog.style.height = maxHeight + "px";
			}
			if (modalDialogSize.width > maxWidth) {
				modalDialog.style.width = maxWidth + "px";
			}
			modalDialog.style.overflow = 'scroll';
		} else {
			modalDialog.style.overflow = 'hidden';
			m_resetModelDialogSize = 0;
		}
		
	}
	
	m_curModalDialog = modalDialogName;
}

/**
 * Close the currently-open modal dialog
 */
function closeModalDialog() {
	if (m_curModalDialog != 0) {
		var modalDialog = getRawObject(m_curModalDialog);
		
		modalDialog.style.visibility = "hidden";
		m_curModalDialog = 0;
		
		if (typeof m_resetModelDialogSize == 'object') {
			modalDialog.style.height = m_resetModelDialogSize.height;
			modalDialog.style.width = m_resetModelDialogSize.width;
		}
	}

	m_overlayDiv.style.visibility = "hidden";
	
	// Restore scroll bars and selects (for IE)
	if (m_isIe) {
		if (m_scrollBarsHidden) {
			// Restore scroll bars if they were hidden
			
			if (m_doctypeDtdStrict == "ie6Loose") {
				document.body.parentNode.style.overflow = 'auto';	
			} else {
				document.body.style.overflow = 'auto';	
			}			
		}
		_modalDialogHideSelects(false);
		//m_modalShim.style.display = "none";
	}
}

var m_modalDialogHiddenSelects = new Array();

// for IE...
function _modalDialogHideSelects(hideMode) {
	// I could not get the this work reliably... it would
	// hide the selects in my modal dialog too.
	// I tried to avoid this by only hiding currently-visible
	// selects (see code below), but I could not figure out
	// how to know, for sure, that the select element was
	// really hidden. In fact, I never got a non-blank 
	// value for "visibility", even with parent elements
	// of class modalDialog, which I *know* is defined to
	// have visibility = hidden. I give up for now. MRG 8/18/06
	return;
	
	if (hideMode) {
		m_modalDialogHiddenSelects = new Array();
		var selects = document.getElementsByTagName('select');
		var hideIt = false;
		var p;

		for (var i = 0; i < selects.length; i++) {
			//alert(selects[i].id + " " + selects[i].name + " = " + selects[i].style.visibility);
			if (selects[i].style.visibility != "hidden") {
				hideIt = true;
				// are all parents visible too?
				p = (selects[i].parent) ? selects[i].parent : selects[i].offsetParent;
				//alert(p);
				while (p && hideIt) {
					if (p.className != "") {
						// even "modalDialog" classes are showing up as not hidden!  MRG 8/18/06
						alert("p " + p.className + " " + p.style.visibility);
					}
					if (p.style.visibility == "hidden") {
						// I never got this to fire! MRG 8/18/06
						hideIt = false;
						alert("dont hide it!");
					}
					p = (p.parent) ? p.parent : p.offsetParent;
				}
				if (hideIt) {
				//alert("hide it!");
					selects[i].style.visibility = "hidden";
					m_modalDialogHiddenSelects[m_modalDialogHiddenSelects.length] = selects[i];
				}
			}
		}
	} else {
		// Unhide the selects that we just hid.
		for (var i = 0; i < m_modalDialogHiddenSelects.length; i++) {
			m_modalDialogHiddenSelects[i].style.visibility = "";
		}
	}
}
