var popup = null; 	// All popups share the same window object named "popup"
var lastPopupURL; 	// Remember the URL of the last popup page the user was on
var formName; 		// The name of the search form, e.g. Basic, Advanced
var fieldName; 		// The field that requested the popup, e.g. Author field

var mnemonic;		// Mnemonic used for the selected item, e.g. SO for publications
var popupText;		// What to paste back into the search form
var undefined;      // NEVER set this variable. It must remain undefined.

function popupClose() {
	if (window.popup && (window.popup.closed !== undefined) && ! window.popup.closed) // If popup window exists and is open, close it
		window.popup.close(); 
}

/**
 * Lunch a new popup window with the specified URL
 * @param popupURL -- URL of the popup window
 */
function launchAPopup(popupURL) {
	var popup = window.popup;

	if (popup == null || popup.closed) {
		popup = window.open(popupURL,'popup','menubar=no,toolbar=yes,location=yes,status=no,scrollbars=yes,resizable=yes,height=400,width=540,left=150,top=0');
	} else {
		if(popupURL != lastPopupURL) {
			// popup window is open and new popup URL is requested
			popup.location = popupURL;
			lastPopupURL = popupURL;
		}
	}
	popup.focus();

	// update the global variables
	window.popup = popup;
	window.lastPopupURL = popupURL;
}

/**
 * Open a popup window which will show a list of selections. When a selection in the
 * popup window is slected, the value of the selection will be inserted back into
 * the form field which requests the popup window.
 * This function also will remember the name of the form field and the form, which asks
 * for the popup window, in the parent window's javascript global variables.
 *
 * @param openerFormName -- The name of the search form which contains the "openerField"
 * @param openerFieldName -- The name of the field that requested the popup, e.g. Author field
 * @param popupURL -- The URL of the popup window
 */
function popupLaunch(popupURL, openerFormName, openerFieldName) {
	if (!openerFormName) {
		alert("\"openerFormName\" is missing from the function input parameters.");
		return;
	}

	if (!openerFieldName) {
		alert("\"openerFieldName\" is missing from the function input parameters.");
		return;
	}

	window.formName = openerFormName;
	window.fieldName = openerFieldName;

	launchAPopup(popupURL);

	/*
	alert("window.formName = " + window.formName + "\n" +
	      "window.fieldName =" +  window.fieldName + "\n" +
	      "window.lastPopupURL =" +  window.lastPopupURL);
	*/
}

/**
 * By clicking the "Add to Search" button in the popup window,
 * populate single selection from the popup window back to the
 * correspondent input field of the parent window
 * @param popupSlectionText -- The text of the selection of the
 * 		  popup window.
 */
function popupSingleTextAddToSearch(popupSlectionText) {
	// get the popup-requesting field name object by its name and its form's name
	// which are stored in the JavaScript global variable
	var openerFormFieldObj = eval("window.document." + window.formName + "." + window.fieldName);
	openerFormFieldObj.value = popupSlectionText;
	window.focus();
}
