
var stMask = null;
var stContainer = null;
var stIsShown = false;
var stHideSelects = false;

function StartIt(divID) {
	// stMask = document.getElementById('mask');
	// stMask.style.display="block";
	// setMaskSize();
	// return;

	//create the timeout message
	createModalDialog(divID);

	//assemble message parts
	var dialogMessage = '<div class="modalContent">';

	// show dialog
	displayModalDialog(885,280,dialogMessage); // parameters are for centering, not container width/height

	// give the window focus
	self.focus();
}

function closeIt() {
	stMask.style.display = "none";
	stContainer.style.display = "none";
	theBody = document.getElementsByTagName('BODY')[0];
	var olddiv = document.getElementById("mask");
	theBody.removeChild(olddiv);

	// display all SELECT boxes : IE 6 z-index bug
	if (stHideSelects == true) {showAllSelects()}
}

function createModalDialog(divID) {
	theBody = document.getElementsByTagName('BODY')[0];
	theMask = document.createElement('div');
	theMask.id = 'mask';
	theBody.appendChild(theMask);

	stMask = document.getElementById('mask');
	stContainer = document.getElementById(divID);

	// check to see if this is IE version 6 or lower. hide select boxes if so
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf('MSIE') > -1) {
		stHideSelects = true;
	}
}

function displayModalDialog(width,height,dialogMessage) {
	stIsShown = true;

	stMask.style.display = 'block';
	stContainer.style.display = 'block';

	// place window on screen : coordinates
	centerModalDialog(width,height);
	stContainer.style.width = width + 'px';
	stContainer.style.height = height + 'px';
	setMaskSize();

	// hide all SELECT boxes : IE 6 z-index bug
	if (stHideSelects == true) {hideAllSelects()}
}


function setMaskSize() {
	var theBody = document.getElementsByTagName('BODY')[0];

	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	var popHeight = 0;
	var popWidth = 0;

	// determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {popHeight = fullHeight}
	else {popHeight = theBody.scrollHeight}

	if (fullWidth > theBody.scrollWidth) {popWidth = fullWidth}
	else {popWidth = theBody.scrollWidth}
	popHeight = popHeight + 1500;
	stMask.style.height = (popHeight) + 'px';
	if (!window.attachEvent){stMask.style.height = (popHeight+16) + 'px';} // correct for scroll bars (FF)
	stMask.style.width = popWidth + 'px';
	if (!window.attachEvent){stMask.style.width = (popWidth-16) + 'px';} // correct for scroll bars (FF)
}

/* hide all SELECT boxes : IE 6 z-index bug */
function hideAllSelects() {
	var targets = document.getElementsByTagName('SELECT');
	for(var i=0; i<targets.length; i++){
		targets[i].style.visibility='hidden';
	}
}

/* display all SELECT boxes : IE 6 z-index bug */
function showAllSelects() {
	var targets = document.getElementsByTagName('SELECT');
	for(var i=0; i<targets.length; i++){
		targets[i].style.visibility='visible';
	}
}

function centerModalDialog(width,height) {
	if (stIsShown == true) {
		if (width == null || isNaN(width)) {
			width = stContainer.offsetWidth;
		}
		if (height == null) {
			height = stContainer.offsetHeight;
		}

		var theBody = document.getElementsByTagName('BODY')[0];
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);

		setMaskSize();

		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		fullHeight = fullHeight - 200;


		stContainer.style.top = (scTop + ((fullHeight - height) / 2)) + 'px';
		stContainer.style.left = (scLeft + ((fullWidth - width) / 2)) + 'px';
	}
}

function getScrollTop() {
	if (self.pageYOffset) {return self.pageYOffset} // non IE browsers
	else if (document.documentElement && document.documentElement.scrollTop) {return document.documentElement.scrollTop} // IE 6 Strict
	else if (document.body) {return document.body.scrollTop} // all other IEs
}

// ---------------------------------------------------------------------------------

// returns height of inner window

function getViewportHeight() {
	// standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	// IE6 in standards compliant mode
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	// IE older versions
	if (document.body) return document.body.clientHeight;

	return window.undefined;
}

// ---------------------------------------------------------------------------------

// returns width of inner window

function getViewportWidth() {
	// standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	// IE6 in standards compliant mode
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	// older versions of IE
	if (document.body) return document.body.clientWidth;

	return window.undefined;
}
