// Page version: v1.8, released 21 July 2004
	
var objPopup = null;
var strLastLookUpValue = null;
var strLastLookUpFunction = null;
var strCurrentLookUpValue = null;
var strCurrentLookUpFunction = null;
var lngCurrentRow = 1;
var lngTotalItems = 0;
var scrollbarWidth = 22;
var scrollbarHeight = 22;
var popupPadding = 3;

var currentField = null;
var current2Field = null;
var currentIDField = null;
var currentImg = null;
var currentEvent = null;

var gblWidth;
var gblHeight;
var gblHeadings;
var gblSrcElement;
var gblFunctionName;
var gblColumns;
var gblAHeadings;
var gblFocusElement;
var gblParams;
var gblServerURL;
var gblEventDriven = false;
var ifrmPopUp;

document.write("<iframe id=ifrmPopUp name=ifrmPopUp class=Popup style=\"DISPLAY:'none';\"></iframe>");
document.write("<div id=divPopUp name=divPopUp class=Popup onkeydown=\"CheckKeyPress()\" style=\"DISPLAY:'none';\"></div>");

ifrmPopUp = document.all.ifrmPopUp;

function resetLastLookup() {
	strLastLookUpValue = null;
	strLastLookUpFunction = null;
}

function LaunchSearch(srcElement, focusElement, divWidth, divHeight, RunFunctionName, lngColumns, blnHeadings, aHeadings, obj){

	if(strLastLookUpFunction != strCurrentLookUpFunction || strLastLookUpValue != strCurrentLookUpValue){
		strLastLookUpValue = strCurrentLookUpValue;
		strLastLookUpFunction = strCurrentLookUpFunction;

		gblWidth = divWidth;
		gblHeight = divHeight;
		gblHeadings = blnHeadings;
		gblSrcElement = srcElement;
		gblFunctionName = RunFunctionName;
		gblColumns = lngColumns;
		gblAHeadings = aHeadings;
		gblFocusElement = focusElement;
		
		CreatePopupTable(obj, RunFunctionName, lngColumns, blnHeadings, aHeadings, focusElement);
	}
	ShowPopUp(srcElement, divWidth, divHeight, blnHeadings);
}
	 
function Lookup(srcElement, focusElement, divWidth, divHeight, RunFunctionName, lngColumns, blnHeadings, aHeadings, serverURL, params)
{ 
	window.event.cancelBubble = true;
	if(strLastLookUpFunction != strCurrentLookUpFunction || strLastLookUpValue != strCurrentLookUpValue){
		strLastLookUpValue = strCurrentLookUpValue;
		strLastLookUpFunction = strCurrentLookUpFunction;
		currentEvent = window.event;
		
		gblWidth = divWidth;
		gblHeight = divHeight;
		gblHeadings = blnHeadings;
		gblSrcElement = srcElement;
		gblFunctionName = RunFunctionName;
		gblColumns = lngColumns;
		gblAHeadings = aHeadings;
		gblFocusElement = focusElement;
		
		gblParams = params;
		gblServerURL = serverURL;

		jsrsExecute(serverURL, lookup_Return, strLastLookUpFunction, params);
		
	}else{	
		ShowPopUp(srcElement, divWidth, divHeight, blnHeadings);
	}
}

function LookupNext() {
	window.event.cancelBubble = true;
	
	divPopUp.innerHTML = "Loading...";
	divPopUp.style.cursor = "wait";
	
	if (gblParams.length == 2) {
		gblParams.push(gblParams[1] + 1)
	} else if (gblParams.length == 3) {	
		gblParams[2] = gblParams[2] + gblParams[1];
	}
	jsrsExecute(gblServerURL, lookup_Return, strLastLookUpFunction, gblParams);
}

function LookupPrevious() {
	window.event.cancelBubble = true;
	
	divPopUp.innerHTML = "Loading...";
	divPopUp.style.cursor = "wait";	
	
	if (gblParams.length == 3) {	
		gblParams[2] = gblParams[2] - gblParams[1];
	}
	jsrsExecute(gblServerURL, lookup_Return, strLastLookUpFunction, gblParams);
}


function lookup_Return(obj) {	
	//alert(obj);
	divPopUp.style.cursor = "default";
	CreatePopupTable(obj, gblFunctionName, gblColumns, gblHeadings, gblAHeadings, gblFocusElement);
	ShowPopUp(gblSrcElement, gblWidth, gblHeight, gblHeadings);
}
	
function CreatePopupTable(obj, RunFunctionName, lngColumns, blnHeadings, aHeadings, focusElement){
	var i;
	var text;
	
	lngTotalItems = obj.length;

	text = "<table border=0 cellpadding=" + popupPadding + " cellspacing=0 width=100%><tr><td>";
	text += "<table id=tblMenu name=tblMenu border=0 cellpadding=2 cellspacing=0 width=100% focusElement='" + focusElement + "'>";
	
	//write column headings
	if (blnHeadings) {
		text +="<tr id=trMenu0>";
					
		for (i=0; i < lngColumns; i++ ) {
			text += "<td valign=top class=underline><b>" + aHeadings[i] + "</b></td>";
		}
					
		text +="</tr>";
	}
	
	objPopup = new Array();	
	
	for (i=0; i < lngTotalItems ; i++ ) {
		if (typeof(obj[i]) == "object") {					
			text += "<tr valign=center id=trMenu" + (i + 1) + " onclick='eval(objPopup[" + i + "])' onmouseover=\"trOnMouseOver(" + (i + 1) + ")\" >";

			//create function to run when selecting the item
			var func = RunFunctionName + "(";
			for (x = 0; x < obj[i].length; x++) {
				if (x > 0) {
					func += ",";
				}
				if (typeof(obj[i][x]) == "string") {
					func += "\"" + formatJS(obj[i][x]) + "\"";
				} else {
					func += obj[i][x];
				}
			}
			objPopup[i] = func + ")";			
			
			//write column								
			for (x = 1; x <= lngColumns; x++) {
				if (obj[i][x] == null) {
					text += "<td valign=top>&nbsp;</td>";
				}else if (typeof(obj[i][x]) == "string") {
					text += "<td valign=top>" + formatHTML(obj[i][x]) + "</td>";
				} else{
					text += "<td valign=top>" + obj[i][x] + "</td>";
				}
			}
						
			text += "</tr>";
			
		} else {
			text +="<tr id=trMenu" + (i + 1) + " >";		
			text += "<td valign=top colspan=" + lngColumns + ">" + formatHTML(obj[i]) + "</td>";		
			text +="</tr>";
		}
	}
	text += "</table></td></tr></table>";	
	divPopUp.innerHTML = text;
}

function ShowPopUp(srcElement, divWidth, divHeight, blnHeadings) {
	if (gblEventDriven) {	
		var evt;
		
		if (window.event)
			evt = window.event
		else
			evt = currentEvent;
			
		var posleft = evt.clientX;	
		if (posleft + divWidth > document.body.offsetWidth - scrollbarWidth)
			posleft -= divWidth;

		var postop = evt.clientY;
		if (postop + divHeight > document.body.offsetHeight - scrollbarHeight) {
			if (postop - divHeight > 0) {
				postop = postop - divHeight;
			}else{
				var maxabove = postop;
				var maxbelow = document.body.offsetHeight - scrollbarHeight - postop;
				if (maxabove > maxbelow){
					divHeight = maxabove;
					postop = 0;
				}else{
					divHeight = maxbelow;
				}
			}
		}
				
	}else{
		var posleft = findCoordinate(srcElement, "LEFT") + srcElement.offsetWidth;				
		if (posleft + divWidth > document.body.offsetWidth - scrollbarWidth) {
			if (posleft - divWidth - srcElement.offsetWidth > 0) {
				posleft = posleft - divWidth - srcElement.offsetWidth;
			} else {
				if (divWidth > document.body.offsetWidth - scrollbarWidth) {
					divWidth = document.body.offsetWidth - scrollbarWidth;
				}
				posleft = document.body.offsetWidth - scrollbarWidth - divWidth;
			}
		}

		var postop = findCoordinate(srcElement, "TOP");
		if (postop + divHeight > document.body.offsetHeight - scrollbarHeight) {
			if (postop - divHeight + srcElement.offsetHeight > 0) {
				postop = postop - divHeight + srcElement.offsetHeight;
			} else {
				if (divHeight > document.body.offsetHeight - scrollbarHeight) {
					divHeight = document.body.offsetHeight - scrollbarHeight;
				}
				postop = document.body.offsetHeight - scrollbarHeight - divHeight;
			}
		}	
	}

	divPopUp.style.left = posleft;
	divPopUp.style.top = postop;						
	divPopUp.style.width = divWidth;
	divPopUp.style.height = divHeight;

	ifrmPopUp.style.left = posleft;
	ifrmPopUp.style.top = postop;						
	ifrmPopUp.style.width = divWidth;
	ifrmPopUp.style.height = divHeight;
	
	divPopUp.style.display = "block";	
	ifrmPopUp.style.display = "block";
	//HideTags(divPopUp, "SELECT");	
			
	if (document.all("trMenu1")){
		trUnHighlightAll(blnHeadings);
		lngCurrentRow = 1;
		trHighlight(trMenu1);
	}
	
	try {			
		divPopUp.focus();		
	} catch(e) {}
	divPopUp.scrollTop = 0;
	gblEventDriven = false;
}
	
function HidePopUp(popup){
	if (popup.style.display == "block"){
		popup.style.display = "none";
		//ShowTags("SELECT");
	}
}	

function closePopup() {
	HidePopUp(divPopUp);
	HidePopUp(ifrmPopUp);
}

function HideTags(popup, elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (!obj || !obj.offsetParent)
			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft = obj.offsetLeft;
		objTop = obj.offsetTop;
		objParent = obj.offsetParent;

		while (objParent != null && objParent.tagName.toUpperCase() != "BODY")
		{
			objLeft += objParent.offsetLeft;
			objTop += objParent.offsetTop;
			objParent = objParent.offsetParent;
		} 
		objRight = objLeft + obj.offsetWidth;
		objBottom = objTop + obj.offsetHeight;

		divLeft = popup.offsetLeft;
		divTop = popup.offsetTop;
		divRight = divLeft + popup.offsetWidth;
		divBottom = divTop + popup.offsetHeight;

		if ((objLeft > divLeft && objLeft < divRight)||(objRight > divLeft && objRight < divRight)||(objLeft < divLeft && objRight > divRight)){
			if ((objTop > divTop && objTop < divBottom)||(objBottom > divTop && objBottom < divBottom)||(objTop < divTop && objBottom > divBottom)){
				obj.style.visibility = "hidden";
			}
		}
	}
}

function findCoordinate(obj, coordinate) {
	objLeft = obj.offsetLeft;
	objTop = obj.offsetTop;
	objParent = obj.offsetParent;

	while (objParent != null && objParent.tagName.toUpperCase() != "BODY")
	{
		objLeft += (objParent.offsetLeft - objParent.scrollLeft);
		objTop += (objParent.offsetTop - objParent.scrollTop);
		objParent = objParent.offsetParent;
	} 
	objRight = objLeft + obj.offsetWidth;
	objBottom = objTop + obj.offsetHeight;
	
	if (coordinate == "LEFT")
		return objLeft;
	else if (coordinate == "RIGHT")
		return objRight;
	else if (coordinate == "TOP")
		return objTop;
	else if (coordinate == "BOTTOM")
		return objBottom;	
}

function ShowTags(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}

function CheckKeyPress() {

	switch (window.event.keyCode){
		case 38: //Up
			if (tblMenu.rows.length > 0) {
				if (lngCurrentRow > 1){
					trUnHighlight(tblMenu.rows("trMenu" + lngCurrentRow));	
					divPopUp.scrollTop = tblMenu.rows("trMenu" + lngCurrentRow).offsetTop;		
					lngCurrentRow--;
					trHighlight(tblMenu.rows("trMenu" + lngCurrentRow));
				}
			}
			break;
			
		case 40: //Down
			if (tblMenu.rows.length > 0) {
				if (lngCurrentRow < lngTotalItems){
					trUnHighlight(tblMenu.rows("trMenu" + lngCurrentRow));
					divPopUp.scrollTop = tblMenu.rows("trMenu" + lngCurrentRow).offsetTop;
					lngCurrentRow++;
					trHighlight(tblMenu.rows("trMenu" + lngCurrentRow));
				}
			}
			break;
			
		case 27: //Esc
			closePopup();
			if(tblMenu.focusElement && tblMenu.focusElement.length > 0) {
				resetLookup();
			}
			break;
		
		case 13: //Enter
			if (tblMenu.rows.length > 0) {
				tblMenu.rows("trMenu" + lngCurrentRow).click();
			}
			break;
			
		case 9: //Tab
			if(divPopUp.style.display == "block")
				if (tblMenu.rows.length > 0) {
					tblMenu.rows("trMenu" + lngCurrentRow).click();
				}
			break;
			
		default:
	}
	
	window.event.cancelBubble = true;
	return false;
}

function trUnHighlightAll(blnHeadings){
	var lngRows;
		
	if (blnHeadings)
		lngRows = tblMenu.rows.length - 1;
	else
		lngRows = tblMenu.rows.length;
			
	for (var i = 1; i <= lngRows; i++){
		trUnHighlight(tblMenu.rows("trMenu" + i));
	}
}

function trHighlight(trMenu){
	trMenu.className = "MenuHover";
}

function trOnMouseOver(lngRowIndex){
	trUnHighlight(tblMenu.rows("trMenu" + lngCurrentRow));
	lngCurrentRow = lngRowIndex;
	trHighlight(tblMenu.rows("trMenu" + lngCurrentRow));
}

function trUnHighlight(trMenu){
	trMenu.className = "Menu";
}
