tableBackgroundColor 	= "#FCFFB3";
tableTextColor 				= "#608C66";
tableHiliteTextColor 	= "#003333";

/*********************************************************************************************
*  NAME
*  common.js
*
*  DESCRIPTION
*  common javascript code
*
*  MODIFICATION HISTORY
*  ng		2/11/2001		created
*
*********************************************************************************************/


//handles clicking of buttons on form
function buttonClicked(element) {
	if (!element) {
		return;
	}

	if (element.className.toUpperCase() == 'BUTTONDISABLED') {
		return;
	}

	var href = element.href;
	
	if (href.indexOf("javascript:") > 0) {
		var code = href.substr(href.indexOf("javascript:")+1);
		eval(code);
	} else {
		var target = element.target;
		
		if (target != null) {
			if (target == '_blank') {
				window.open(href);
			}
		} else {
			document.location.href = href;
		}
	}
}

//FUNCTION: resizeToText(element)
//Resizes 1 line textarea to allow for text within by calculating height of a single
//row, height of the scroll area to estimate the number of rows required to display
//all the text within the textarea. 
//Requires: textarea attribute: maxrows=<integer>
//
function resizeToText(x, minRows) {
	
	if (minRows == null)// if not provided use the attribute in the text area.
	{
	  minRows = x.minrows
	}

	if(typeof x != 'undefined'){
		heightPixels=x.offsetHeight-6;
		rowheightPixels=((heightPixels-(heightPixels%(x.rows)))/x.rows);
		scrollRows=(x.scrollHeight-((x.scrollHeight)%rowheightPixels))/rowheightPixels;
		//if (scrollRows>x.maxrows) {scrollRows=x.maxrows}
		if (scrollRows>minRows) {x.rows=scrollRows}
	}
}


//call from a cancel button
function cancel() {
  window.history.go(-1);
}

//call to submit the form
function doSubmit() {
	document.forms[0].submit();
}



//writes to the action field
function setAction(actionValue) {
	if (document.forms[0]['action']) {
		document.forms[0]['action'].value = actionValue;
	}
}

/*
* function called when a TR of class "SELECTABLEROW" is clicked
* makes row appear highlighted or unhighlighted
*/
function rowSelected(selectedRowElement) {
	if (selectedRowElement == null) {
		return;
	}
	if (selectedRowElement.tagName.toUpperCase() != 'TR') {
		return;
	}

	var isCurrentlySelected = selectedRowElement.isSelected;

	if (isCurrentlySelected == null) {
		isCurrentlySelected = false;
	}

	//clear all currently selected rows
	var tableElement = selectedRowElement.parentElement;	
	var rowElements = tableElement.children;
	for (i=0; i<rowElements.length; i++) {
		if (rowElements[i].isSelected == true) {
			var rowToUnselect = rowElements[i];
			colorCells(rowToUnselect, false);
			rowToUnselect.isSelected = false;
		}
	}
	
	//if the row clicked on was not originally selected, make it selected
	if (isCurrentlySelected == false) {
		selectedRowElement.isSelected = true;	
		colorCells(selectedRowElement, true);
		window.selectedRow = selectedRowElement;
		loadPopup(selectedRowElement.menuoptions);
	}
}


function rowHoveredOver(selectedRowElement) {
	if (selectedRowElement == null) {
		return;
	}
	if (selectedRowElement.tagName.toUpperCase() != 'TR') {
		return;
	}

	
	var isCurrentlySelected = selectedRowElement.isSelected;

	if (isCurrentlySelected == null) {
		isCurrentlySelected = false;
	}

	//clear all currently selected rows
	var tableElement = selectedRowElement.parentElement;	
	var rowElements = tableElement.children;
	for (i=0; i<rowElements.length; i++) {
		if (rowElements[i].isSelected == true) {
			var rowToUnselect = rowElements[i];
			colorCells(rowToUnselect, false);
			rowToUnselect.isSelected = false;
		}
	}
	
	//if the row clicked on was not originally selected, make it selected
	if (isCurrentlySelected == false) {
		selectedRowElement.isSelected = true;	
		colorCells(selectedRowElement, true);
		//window.selectedRow = selectedRowElement;
		//loadPopup(selectedRowElement.menuoptions);
	}
}

/*
*	sets the text color & background color for all child TD's of a TR
*/
function colorCells(selectedRowElement, makeSelected) {
	if (makeSelected == true) {
		backgroundColor = tableBackgroundColor;
		textColor 			= tableHiliteTextColor;
	} else {
		backgroundColor = tableTextColor;
		textColor 			= tableBackgroundColor;
	}

	if (selectedRowElement.tagName.toUpperCase() != 'TR') {
		return;
	}

	var tdElements = selectedRowElement.children;
	for (i=0; i<tdElements.length; i++) {
		tdElement = tdElements[i];
		tdElement.style.backgroundColor = backgroundColor;
		tdElement.style.color = textColor;
	}
}



//unhides the popup menu <div> and positions it next to the mouse cursor
function displayPopupMenu() {

	var popupMenu = document.all.popupMenu;

	var popupWidth = popupMenu.clientWidth + 4 + 10;
	var popupHeight = popupMenu.clientHeight + 4 + 10;
	var browserWidth = document.body.clientWidth;
	var browserHeight = document.body.clientHeight;
	var popupXPos = event.clientX;
	var popupYPos = event.clientY;
	var xOffset = 4;
	var yOffset = 2;

	//debugging alerts....
	// alert('cursor-x = ' + popupXPos + '   browser width = ' + browserWidth);
	// alert('cursor-y = ' + popupYPos + '   browser height = ' + browserHeight);
	// alert('x-offset = ' + document.body.scrollTop + ' y-offset = ' + document.body.scrollLeft);

	if ((popupXPos + popupWidth > browserWidth) && (popupXPos > popupWidth)) {
		popupXPos = popupXPos - popupWidth - xOffset;
	} else {
		popupXPos = popupXPos + xOffset;
	}

	if ((popupYPos + popupHeight > browserHeight) && (browserHeight > popupHeight)) {
		popupYPos = browserHeight - popupHeight - yOffset;
	} else {
		popupYPos = popupYPos + yOffset;
	}

	popupMenu.style.visibility = 'visible';
	popupMenu.style.left = popupXPos + document.body.scrollLeft;
	popupMenu.style.top = popupYPos + document.body.scrollTop;

	//update global browser-window flags
	window.isPopupDisplayed = true;
}

//globally available function to close the popup menu
function closePopup() {
	if (window.isPopupDisplayed == true) {
		var popupMenu = document.all.popupMenu;
		popupMenu.style.visibility = 'hidden';
		window.isPopupDisplayed = false;
		window.intervalID = null;

		rowSelected(window.selectedRow);
	}
	resetAutoCloseTimer();
}



function startAutoCloseTimer() {
	if (window.isPopupDisplayed == true && window.intervalID == null) {	
		window.intervalID = setTimeout('window.closePopup()', 700);
	}

}

function resetAutoCloseTimer() {
	if (window.intervalID != null) {
		clearTimeout(window.intervalID);
		window.intervalID = null;
	}
}


function loadPopup(menuOptionsStr) {

	if (menuOptionsStr == null) {
		return;
	}

	menuHeaderText = 'select';

	var menuOptions = menuOptionsStr.split("~");
	var menuItemDivs = "";

	for (var i=0; i<menuOptions.length; i++) {
		var menuOptionStr = menuOptions[i];
		var menuItemText = menuOptionStr.substring(0, menuOptionStr.indexOf("="));
		var menuItemActionRef = menuOptionStr.substr(menuOptionStr.indexOf("=")+1);

		if (menuItemActionRef.lastIndexOf(";") < menuItemActionRef.length-1){
			menuItemActionRef = menuItemActionRef + ";"			
		}
		menuItemActionRef = menuItemActionRef + "closePopup();";
	
		if (menuItemText == '') menuItemText = '&nbsp';	
		//define a <div> for the menu item, depending on whether we implement a behaviour or a JS function
		menuItemDivs += '<tr class=\"popupMenuOption\"><td nowrap id=\"menu' + i + '\" style=\"cursor: hand; visibility: inherit; padding-left: 3px; padding-right: 2px;\" onMouseOver=\"parentElement.className=\'popupMenuOptionHiLight\';\" onMouseOut=\"parentElement.className=\'popupMenuOption\';\" onClick=\"'+ menuItemActionRef + '\">' + menuItemText + '</td></tr>';			
	}


	//if the menu has never been displayed, then create the menu definition (at the start of the <body> html)
	if (!document.all.popupMenu) {
		menuDiv = '<div class=\"popupMenu\" id=\"popupMenu\" onmouseover=\"resetAutoCloseTimer()\" onmouseout=\"startAutoCloseTimer()\" style=\"position: absolute; visibility: hidden; z-index: 1000;\"></div>';
		document.body.insertAdjacentHTML('afterBegin', menuDiv);
	}

	var headerDiv = '<tr class=\"popupMenuHeader\"><td nowrap id=\"menuHeader\" style=\"visibility: inherit; border-bottom: ridge black 1px;\">' + menuHeaderText + '</td></tr>';
	var popupMenu = document.all.popupMenu;

	popupMenu.style.left = 0;
	popupMenu.style.top = 0;
	var popupMenuDefinition = '<table width=1 border=0 cellspacing=0 cellpadding=0><tr><td nowrap><table border=0 cellspacing=0 cellpadding=2>' + headerDiv + menuItemDivs + '</table></td></tr></table>';
	popupMenu.innerHTML = popupMenuDefinition;
	
	//now display the menu
	displayPopupMenu();
}


//standard js alert, for warnings
function warnAlert(msg, confirm) {
	var header = "Warning" + "\n------------\n"
	var displayMsg = header + msg;
	if (confirm) {
		displayMsg = displayMsg + "\n\nContinue?...";
		var returnVal = window.confirm(displayMsg);
		return returnVal;
	} else {
		alert(displayMsg);
	}
}

//standard js alert, to be used for validation
function validationAlert(msg) {
	var header = "Validation Error" + "\n--------------------\n";
	var displayMsg = header + msg;
	alert(displayMsg);
}


//standard js alert, for information messages
function infoAlert(msg, confirm) {
	var header = "Information" + "\n---------------\n"
	var displayMsg = header + msg;
	if (confirm) {
		displayMsg = displayMsg + "\n\nContinue?...";
		var returnVal = window.confirm(displayMsg);
		return returnVal;
	} else {
		alert(displayMsg);
	}
}


//standard js alert, for system messages
function systemAlert(msg) {
	var header = "SYSTEM ERROR" + "\n------------------\n"
	var displayMsg = header + msg;
	alert(displayMsg);
}


function showHelp(url) {
	window.open("../" + url,"Help","HEIGHT=501,WIDTH=601");	
}


function showGuidance(url) {
	window.open("../" + url,"Guidance","HEIGHT=501,WIDTH=601");	
}


/**** utility function ****/
function trim(strText) { 
    // gets rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // gets rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 


/**** random functions ****/
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/**** random functions ****/
function goToURL(pageToJumpTo) {
	if(pageToJumpTo != '') {
		here		= self.location.href;
		here = removeExcessQueryString(here);
		here = removeLastDir(here);
/*
		alert(pageToJumpTo);
		pageToJumpTo = removeStartingSlashes(pageToJumpTo);
		alert(pageToJumpTo);
		pageToJumpTo = removeStartingSlashes(pageToJumpTo);
		alert(pageToJumpTo);
*/
		there	= here + pageToJumpTo;

		self.location = there;
	}
}

function mainGoToURL(pageToJumpTo) {
	if(pageToJumpTo != '') {
		parent.document.all.main.src=pageToJumpTo;
	}
}

function display(text) {
	REQUIRED_TEXT = "This field must be filled in to save the form";

	if (!text) {
		text = "<?php echo date(\"l, jS F Y\") ?>";
		window.status = "";
	} else if (text=='required field') {
		window.status = REQUIRED_TEXT;
	} else {
		window.status = text;
	}

	//document.displayform.displayfield.value = text;
	return true;
}

function highlightField(fieldName) {
	//HIGHLIGHTS A FIELD BACKGROUND IF THERE IS SOMETHING WRONG WITH IT
	//alert('inside highlightField(' +fieldName +')'); 

	if (fieldName == '') {
		return false;
	} else {
		//document.all[fieldName].style.backgroundColor = '#CCCCFF';
		document.all[fieldName].className='selected';
		return true;
	}
}

function unHighlightField(fieldName) {
//UNHIGHLIGHTS A FIELD BACKGROUND IF THE PROBLEM HAS BEEN CORRECTED BY THE USER
	//alert('inside unHighlightField(' +fieldName +')'); 

	if (fieldName == '') {
		return false;
	} else {
		//document.all[fieldName].style.backgroundColor = '#F0F0F8';
		document.all[fieldName].className='';
		return true;
	}
}

function determineScreenSize() {
	var screenW = 640, screenH = 480;
	if (parseInt(navigator.appVersion)>3) {
	 screenW = screen.width;
	 screenH = screen.height;
	}
	else if (navigator.appName == "Netscape"
		&& parseInt(navigator.appVersion)==3
		&& navigator.javaEnabled()
	   )
	{
	 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
	 var jScreenSize = jToolkit.getScreenSize();
	 screenW = jScreenSize.width;
	 screenH = jScreenSize.height;
	}

	//alert("Screen width = " +screenW +", Screen height = " +screenH)	
	return(screenH);
}
