var _popupWindow;
var _valueElement;
var _displayElement;

/**
 * Show a popup window with supplied url, width and height
 */
function showPopup(url, width, height, scroll) {
	var top = ((screen.height - height)/2);
	var left = ((screen.width - width)/2);

	// close the window if its already open
	if (_popupWindow != null) {
		try {
			_popupWindow.close();
		}
		catch (ex) {
			/* swallow */
		}
	}
	
	if (scroll == null) {
		scroll = "no";
	}

	_popupWindow = window.open(url, "popupSelector", "width=" + width + ",height=" + height + ",status=no,resizable=yes,scrollbars=" + scroll + ",top=" + top + ",left=" + left);
	_popupWindow.opener = window;
	_popupWindow.focus();
}

/*********************************************************************************/
/* Single value popup

/**
 * Show a popup list that returns a single value, which
 * is set as the value attribute of the element supplied
 */
function showPopupList(url, elementId, listType) {
	_valueElement = document.getElementById(elementId);
	
  	showPopup(url + "?type=" + listType + "&selectedValue=" + escape(_valueElement.value), 400, 492);
}

/**
 * Set the valueElement's value attribute on OK
 */
function popupListOk(selectedItem) {
	if (_valueElement != null) {
		_valueElement.value = selectedItem;
	}
}

/**
 * Do nothing on Cancel
 */
function popupListCancel() {
	/* do nothing */
}

/**
 * Initialise the popup list, designed to be called
 * from the popup itself
 */
function initPopupList() {
	var selectedValue = unescape(location.getParameter("selectedValue"));
	
	var items = document.getElementsByTagName("a");

	for (var i=0; i<items.length; i++) {
		var item = items[i];
		
		if (item.className && item.className != 'button') {
			if (item.getAttribute("value") == selectedValue) {
				item.className = "opt selected";
			}
		}
	}
}

/**
 * Call cancel and close the window
 */
function popupListCancelClicked() {
	if (window.opener != null)
		window.opener.popupListCancel();

	window.close();
}

/**
 * Call Ok and close the window
 */
function selItem(elementId) {
	var selectedItem = document.getElementById(elementId).getAttribute("value");

	if (window.opener != null)
		window.opener.popupListOk(selectedItem);

	window.close();
}

/*********************************************************************************/
/* Object popup

/**
 * Show a popup list that returns a name/value pair
 */
function showObjectPopupList(url, valueElementId, displayElementId, restrictionElementId) {
	_valueElement = document.getElementById(valueElementId);
	_displayElement = document.getElementById(displayElementId);

	var restrictionElement = document.getElementById(restrictionElementId);
	
	if (url.indexOf("?") == -1) {
		url += "?";
	}
	else {
		url += "&";
	}
	
	if (restrictionElement != null) {
		var restrictionElementStripped = restrictionElement.id;
		
		if (restrictionElementStripped.indexOf(".") != -1) {
			restrictionElementStripped = restrictionElementStripped.substr(restrictionElementStripped.lastIndexOf(".") + 1);
		}
		
		url += "selectedValue=" + escape(_valueElement.value) + "&" + restrictionElementStripped + "=" + escape(restrictionElement.value);
	}
	else {
		url += "selectedValue=" + escape(_valueElement.value);
	}
	showPopup(url, 600, 492, "yes");
}

/**
 * Call Ok and close the window
 */
function selObj(value, displayValue) {
	if (window.opener != null)
		window.opener.popupObjectListOk(value, displayValue);

	window.close();
}

/**
 * Set the valueElement's value attribute and the displayElement's html on OK
 */
function popupObjectListOk(selectedValue, selectedDisplayValue) {
	if (_valueElement != null) {
		_valueElement.value = selectedValue;
	}
	if (_displayElement != null) {
		// clear existing nodes
		while(_displayElement.firstChild) {
			_displayElement.removeChild(_displayElement.firstChild);
		}
		
		_displayElement.appendChild(document.createTextNode(selectedDisplayValue)); 
	}
}

/**
 * Clear the selection
 */
function popupObjectListClear() {
	if (_valueElement != null) {
		_valueElement.value = "";
	}
	if (_displayElement != null) {
		// clear existing nodes
		while(_displayElement.firstChild) {
			_displayElement.removeChild(_displayElement.firstChild);
		}
		
		_displayElement.appendChild(document.createTextNode("(none)")); 
	}
}

/**
 * Do nothing on Cancel
 */
function popupObjectListCancel() {
	/* do nothing */
}

/**
 * Call cancel and close the window
 */
function popupObjectListCancelClicked() {
	if (window.opener != null)
		window.opener.popupObjectListCancel();

	window.close();
}

/**
 * Clear the selection
 */
function popupObjectListClearClicked() {
	if (window.opener != null)
		window.opener.popupObjectListClear();

	window.close();
}

/**
 * Call Ok and close the window
 */
function selObject(value, displayValue) {
	if (window.opener != null)
		window.opener.popupObjectListOk(value, displayValue);

	window.close();
}
