// Common Search JavaScript Functions
// File:    searchCommon.js
// Author:  Eric Baars
// WEBCAT:  3.1
// Version: 1.1
// History:
//    24 Jan 2001  Initial creation
//    31 Jan 2001  Add generatePopulatedPointerPrompt()...ETB
//    26 Feb 2001  Required prompt processing...ETB
//    12 Mar 2001  Options already in order for populated prompt...ETB
//     1 May 2001  Spaces are a bad thing in a URI...ETB
//    28 Aug 2001  Added action type search...CJF
//    12 Oct 2001  Added Window Features to submitsearchrequest...CJF
//    13 Dec 2001  Modified function submitSearchRequest to add paramters for saving
//                 when parameters are blank...CJF
//	  28 Feb 2002  Added arguements to submitSearchRequest for auto take...CJF
//    23 Aug 2002  Modified submitSearchRequest to use POST (for Unicode search)...CJF
//    19 Sep 2002  Modified submitSearchRequest to handle logic requests...CJF
//    16 Apr 2003  Stopped query from being executed if validation fails...CJF

// Majority of JavaScript code moved out of XSL file to speed processing.

//-- Global Variables
//-----------------------------------------------------------------------------
var sl_current = new list();
var ss_current = new list();
var sl_template = "";

var helpParameters = new Array();

//-----------------------------------------------------------------------------
// Utility: Bring menu window to foreground
function activateMenu() {
	if (top.opener && top.opener.top.typeID == "Menu") {
		top.opener.top.focus();
	} else {
		alert(txt_menuwindowclosed);
	}
}

// Utility: Close any subordinate windows
function closeWindows() {
        window.close( ) ;
        //top.commonWPObject.closeWindow( ) ;
}

//-----------------------------------------------------------------------------
// Utility: Build dropdown list for populated prompt from array
// Parameters:
//	promptID		: prompt id; equals 0 for search expert
//	aryOption		: array of options
//	rows			: number of rows to display
//	flBlank			: blank row option flag; true shows blank row
//	curSelections	: currently selected options; search expert
function generatePopulatedPointerPrompt(promptID,aryOption,rows,flBlank,curSelections) {
	if (promptID == "") return "";
	if (aryOption == null || aryOption == "" || aryOption.length == 0) return "";
	var prName = (promptID=="0"? '' : 'NAME="POI30_' + promptID + '" ');
	var prMultiSel = (rows=="1"? '' : 'MULTIPLE ');
	var sDropdownDefinition ='<SELECT TABINDEX="1" ' + prName + '">\n';
	if (flBlank == true)
	{
		// For some reason, ignores the first one, but shows the second.
		sDropdownDefinition += '<OPTION VALUE=""> </OPTION>\n';
		sDropdownDefinition += '<OPTION VALUE=""> </OPTION>\n';
	}
	for (var jDA = 0; jDA < aryOption.length; jDA++) {
              var currentOption = aryOption[jDA].split('|');
			  var searchText = " " + currentOption[0] + " ";
              sDropdownDefinition += '<OPTION VALUE="' + currentOption[0] + '"';
              if (curSelections.indexOf(searchText) != -1) {
                      sDropdownDefinition += ' SELECTED';
              }
              sDropdownDefinition += '>';
              sDropdownDefinition += unescapeUTF(currentOption[1]);
              sDropdownDefinition += '<\/OPTION>\n';
	}
	sDropdownDefinition += '<\/SELECT>\n';
	return sDropdownDefinition;
}

function submitSearchRequest( cmdName ) {
	// for requests that are not search requests (default).
	if ( cmdName ) {
		document.SearchForm[0].name = cmdName ;
	}
	document.SearchForm.submit( ) ;
	// reset value - search request must be default 
	//  on next call to function.
	document.SearchForm[0].name = "" ;
}

function buildSearchRequest( inWindowInfo ) {
	if ( validateRequiredPrompts() == false ) {
		return false;
	}
    var aWindowInfo = new windowProperties( inWindowInfo ) ;
	var temp = top.name.split('_');
	var kind = (temp[1] == '0') ? 'bro' : temp[0];
	// Action Add-Ons.
	if ( self.layoutID && self.layoutID == 2 ) {
		document.SearchForm.action += "&browseData=1" ;
	}
	if ( top.callerTop && 
		 top.callerTop.opener && 
		 top.callerTop.opener.top.bExpansionReq &&
		 ( ( top.callerTop.opener.parent && 
		     top.callerTop.opener.parent.typeID=="Entry" ) ||
		   ( top.callerTop.opener.typeID=="Entry" ) )
	   ) {
		var sURL = top.callerTop.opener.window.location.toString( ) ;
		var sTPID = "" ;
		var iStart = sURL.indexOf( "TemplateProcessID=" ) + 18 ;
		var iEnd = sURL.indexOf( "&", iStart ) ;
		if ( iStart > iEnd )
		{
		  iEnd = sURL.length ;
		}
		if ( iStart != -1 && iEnd != -1 )
		{
		  sTPID = sURL.substring( iStart, iEnd ) ;
		}
		document.SearchForm.action 	+= "&pointerExpansion=1&pointerExpansionTemplateProcessID=" 
									+ sTPID 
									+ "&pointerExpansionPromptID=" 
									+ top.opener.top.ExpandPromptID ;
	}
	else if ( top.callerTop &&
			  top.callerTop.opener &&
			  ( top.callerTop.opener.parent.typeID=="Entry" ||
			  top.callerTop.opener.typeID=="Entry" ) ) {
		// Assume that pointer expansion is already taken care of as only when pointerExpansion fails.
		document.SearchForm.action += "&entryRead=1" ;
	}
	if ( top.opener != null && top.opener.typeID=="Search" && top.opener.subTypeID=="Expert" ) {
		actionType = "Search" ;
	}
	// END Action Add-Ons.
	wHandle = aWindowInfo.openNewWindow( kind, "" ) ;
    wHandle.focus( ) ;
	document.SearchForm.target = wHandle.name ;
	return true ;
}

//-----------------------------------------------------------------------------
// Handler (button onClick): Submit the search request and get the reply in a new child window
// Template can have required prompts; check them before submitting
function oldSubmitSearchRequest(cmdName,inWindowInfo,inbSave) {
	if (!validateRequiredPrompts()) return false;
	var requestURI = cmd_action
		+ "&" + cmdName + "="
		+ "&PromptID=" + escape(sys_promptid)
		+ "&ParamID=" + escape(sys_paramid) ;
	for (var i = 1; i < document.SearchForm.elements.length; i++) {
		var elementType = document.SearchForm.elements[i].type;
		var elementName = document.SearchForm.elements[i].name;
		if (elementType.substr(0,7) == "select-") {
			for (var j = 0; j < document.SearchForm.elements[i].options.length; j++) {
				if (document.SearchForm.elements[i].options[j].selected) {
					requestURI += "&" + escape(elementName) + "="
						+ escape(document.SearchForm.elements[i].options[j].value);
				}
			}
		} else if (elementType != "button" && elementType != "reset") {
			var elementValue = document.SearchForm.elements[i].value;
			// if no value in prompt, skip it
			if ( inbSave != true ) {
			   if (elementValue == "") continue;
			}
			// ignore request if processing special requests
			if (elementValue == "/" || elementValue == " ") return;
			requestURI += "&" + escape(elementName) + "=" + escape(elementValue);
		}
	}
	if ( self.layoutID && self.layoutID == 2 )
	{
		requestURI += "&browseData=1" ;
	}
	//
	if ( top.callerTop && 
		 top.callerTop.opener && 
		 top.callerTop.opener.top.bExpansionReq &&
		 ( ( top.callerTop.opener.parent && 
		     top.callerTop.opener.parent.typeID=="Entry" ) ||
		   ( top.callerTop.opener.typeID=="Entry" ) )
	   )
	{
		var sURL = top.callerTop.opener.window.location.toString( ) ;
		var sTPID = "" ;
		var iStart = sURL.indexOf( "TemplateProcessID=" ) + 18 ;
		var iEnd = sURL.indexOf( "&", iStart ) ;
		if ( iStart > iEnd )
		{
		  iEnd = sURL.length ;
		}
		if ( iStart != -1 && iEnd != -1 )
		{
		  sTPID = sURL.substring( iStart, iEnd ) ;
		}
		requestURI 	+= "&pointerExpansion=1&pointerExpansionTemplateProcessID=" 
					+ sTPID 
					+ "&pointerExpansionPromptID=" 
					+ top.opener.top.ExpandPromptID ;
	}
	else if ( top.callerTop &&
			  top.callerTop.opener &&
			  ( top.callerTop.opener.parent.typeID=="Entry" ||
			  top.callerTop.opener.typeID=="Entry" ) )
	{
		// Assume that pointer expansion is already taken care of as only when pointerExpansion fails.
		requestURI += "&entryRead=1" ;
	}
	var temp = top.name.split('_');
	var kind = (temp[1] == '0') ? 'bro' : temp[0];
	tableID = "";
	if ( top.opener != null && top.opener.typeID=="Search" && top.opener.subTypeID=="Expert" ) {
		actionType = "Search" ;
	}
	var aWindowInfo = new windowProperties( inWindowInfo ) ;
	wHandle = aWindowInfo.openNewWindow( kind, requestURI ) ;
	wHandle.focus( ) ;
}
