function byId(id)
{
	return document.getElementById(id);
}

// Gets the dimensions of the user's window for many different browsers--
// Returns an array of pageWidth, pageHeight, windowWidth, and windowHeight
function getPageSize()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	// all but Explorer Mac
	{
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	{
		xScroll = document.body.offsetWidth - 250;
		yScroll = document.body.offsetHeight - 250;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight)
	// all except Explorer
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	// other Explorers
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth)
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

// Inspiration from nsftools.com (http://www.nsftools.com/tips/NotesTips.htm#datepicker)
function drawCSSMessageBox(data, x, y, width, height, strtitle)
{
	// Toggle fading of the background the background
	toggleMessageBoxScreen();

	// Create CSSMessageBox if it doesn't already exist
	if (!byId('CSSMessageBox'))
	{
		// don't use innerHTML to update the body, because it can cause global variables
		// that are currently pointing to objects on the page to have bad references
		var newNode = document.createElement("div");
		newNode.setAttribute("id", 'CSSMessageBox');
		newNode.setAttribute("style", "visibility: hidden;");
		document.body.appendChild(newNode);
	}else{
		// move the CSSMessageBox div to the proper x,y coordinate and toggle the visiblity
		var CSSMessageBox = byId('CSSMessageBox');
		CSSMessageBox.style.position = "absolute";
		CSSMessageBox.className = "AlertBG";
		CSSMessageBox.style.left = x + "px";
		CSSMessageBox.style.top = y + "px";
		CSSMessageBox.style.width = width + "px";
		CSSMessageBox.style.height = height + "px";
		CSSMessageBox.style.zIndex = 10000;
		CSSMessageBox.style.display = '';
		CSSMessageBox.innerHTML = '<TABLE WIDTH=\"100%\"><TR><TD CLASS=\"AlertTitle\">'+strtitle+'</TD></TR><TR><TD CLASS=\'AlertBG\'>'+data+'</TD></TR></TABLE>';

		// toggle the visibility of CSSMessageBoxScreen and CSSMessageBox
		CSSMessageBox.style.visibility =
		(CSSMessageBox.style.visibility == "visible" ? "hidden" : "visible");
		CSSMessageBox.style.display =
		(CSSMessageBox.style.display == "block" ? "none" : "block");
	}

	// move the CSSMessageBox div to the proper x,y coordinate and toggle the visiblity
	var CSSMessageBox = byId('CSSMessageBox');
	CSSMessageBox.style.position = "absolute";
	CSSMessageBox.className = "AlertBG";
	CSSMessageBox.style.left = x + "px";
	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		CSSMessageBox.style.top = y / 2 + "px";	
	} else {
		CSSMessageBox.style.top = y + "px";	
	}
	CSSMessageBox.style.width = width + "px";
	CSSMessageBox.style.height = height + "px";
	CSSMessageBox.style.zIndex = 10000;
	CSSMessageBox.style.display = '';
	CSSMessageBox.innerHTML = '<TABLE WIDTH=\"100%\"><TR><TD CLASS=\"AlertTitle\">'+strtitle+'</TD></TR><TR><TD CLASS=\'AlertBG\'>'+data+'</TD></TR></TABLE>';

	// toggle the visibility of CSSMessageBoxScreen and CSSMessageBox
	CSSMessageBox.style.visibility =
	(CSSMessageBox.style.visibility == "visible" ? "hidden" : "visible");
	CSSMessageBox.style.display =
	(CSSMessageBox.style.display == "block" ? "none" : "block");
}

function toggleMessageBoxScreen()
{
	// Create MessageBoxScreen if it doesn't already exist
	if (!byId('MessageBoxScreen'))
	{
		var newNode = document.createElement("div");
		newNode.setAttribute("id", 'MessageBoxScreen');
		newNode.setAttribute("style", "visibility: hidden;");
		document.body.appendChild(newNode);
	} else {
		// move CSSMessageBoxScreen to cover the entire page with its dim-ness-ocity
		var MessageBoxScreen = byId('MessageBoxScreen');
		MessageBoxScreen.style.position = "absolute";
		MessageBoxScreen.style.left = "0px";
		MessageBoxScreen.style.top = "0px";
		MessageBoxScreen.style.width = getPageSize()[0] + "px";
		MessageBoxScreen.style.height = getPageSize()[1] + "px";
		MessageBoxScreen.className = "trans50";
		MessageBoxScreen.style.backgroundColor = "#FFFFFF";
		MessageBoxScreen.style.zIndex = 9999;

		// toggle the visibility
		MessageBoxScreen.style.visibility =
		(MessageBoxScreen.style.visibility == "visible" ? "hidden" : "visible");
		MessageBoxScreen.style.display =
		(MessageBoxScreen.style.display == "block" ? "none" : "block");
	}

	// move CSSMessageBoxScreen to cover the entire page with its dim-ness-ocity
	var MessageBoxScreen = byId('MessageBoxScreen');
	MessageBoxScreen.style.position = "absolute";
	MessageBoxScreen.style.left = "0px";
	MessageBoxScreen.style.top = "0px";
	MessageBoxScreen.style.width = getPageSize()[0] + "px";
	MessageBoxScreen.style.height = getPageSize()[1] + "px";
	MessageBoxScreen.className = "trans50";
	MessageBoxScreen.style.backgroundColor = "#FFFFFF";
	MessageBoxScreen.style.zIndex = 9999;

	// toggle the visibility
	MessageBoxScreen.style.visibility =
	(MessageBoxScreen.style.visibility == "visible" ? "hidden" : "visible");
	MessageBoxScreen.style.display =
	(MessageBoxScreen.style.display == "block" ? "none" : "block");
}

var winW = 630, winH = 460;

function winSize()
{
	if (parseInt(navigator.appVersion)>3)
	{
 		if (navigator.appName=="Netscape")
 		{
  			winW = window.innerWidth;
  			winH = window.innerHeight;
 		}
 		if (navigator.appName.indexOf("Microsoft")!=-1)
 		{
  		winW = document.body.offsetWidth;
  		winH = document.body.offsetHeight;
 		}
	}
}

function openAJaxWindow(divContent,strTitle,strWidth,strHeight)
{
	winSize();
	drawCSSMessageBox(divContent, (winW / 2) - 200, (winH / 2) - 70 + document.body.scrollTop, strWidth, strHeight,strTitle);
	return false;
}

function cancelAJaxWindow()
{
	try{parent.document.body.removeChild(parent.document.getElementById('MessageBoxScreen'))}catch(e){};
	try{parent.document.body.removeChild(parent.document.getElementById('CSSMessageBox'))}catch(e){};
	return false;
}

function getScrollTop() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function aOpenWin(divContent,strTitle,strWidth,strHeight)
{
	winSize();
	drawCSSMessageBox(divContent, (winW / 2) - (strWidth / 2), (winH / 2) - (strHeight / 2) - 70 + getScrollTop(), strWidth, strHeight,strTitle);
	return false;
}

function aAlertWin(aText)
{
	aOpenWin(aText.replace(/\n/g,'<BR>') + '<P ALIGN=\"Center\"><INPUT TYPE=\"Button\" onClick=\"aCancelWin()\" CLASS=\"AlertBut\" VALUE=\" Ok \"></P>','Alert...','350','100');
}

function aAlertHref(aHref,sTitle,sOkText,sOkFunc,sCancel)
{
	var vOkText = '';
	var vOkFunc = '';
	var vCancel = '';
	
	if(sOkText == null){vOkText = 'Ok'}else{vOkText = sOkText}
	if(sOkFunc == null){vOkFunc = 'aCancelWin()'}else{vOkFunc = sOkFunc}
	if(sCancel == null){}else{vCancel = '<INPUT TYPE=\"Button\" onClick=\"aCancelWin()\" CLASS=\"AlertBut\" VALUE=\" Cancel \">'}
	
	aOpenWin('<IFRAME NAME=\'aAlertHrefFrame\' WIDTH=\'400\' HEIGHT=\'350\' BORDER=\'0\' SRC=\''+aHref + '\'></IFRAME><P ALIGN=\"Center\"><INPUT TYPE=\"Button\" onClick=\"'+vOkFunc+'\" CLASS=\"AlertBut\" VALUE=\" '+vOkText+' \">'+vCancel+'</P>',sTitle,'400','350')
}

function aCancelWin()
{
	var CSSMessageBox = document.getElementById('CSSMessageBox');
	var MessageBoxScreen = document.getElementById('MessageBoxScreen');

	CSSMessageBox.style.position = "absolute";
	CSSMessageBox.style.left = "0px";
	CSSMessageBox.style.top = "0px";
	CSSMessageBox.style.width = "0px";
	CSSMessageBox.style.height = "0px";
	CSSMessageBox.className = "";
	CSSMessageBox.style.zIndex = 0;
	CSSMessageBox.style.display = 'none';

	MessageBoxScreen.style.position = "absolute";
	MessageBoxScreen.style.left = "0px";
	MessageBoxScreen.style.top = "0px";
	MessageBoxScreen.style.width = "0px";
	MessageBoxScreen.style.height = "0px";
	MessageBoxScreen.className = "";
	MessageBoxScreen.style.zIndex = 0;

	return false;
}