// Search Template JavaScript Functions
// File:    searchTemplateSimple.js
// Author:  Clinton Farleigh
// WEBCAT:  2.2
// Version: 1.0
// History:
//    26 Oct 2001  Initial creation
//    15 Nov 2001  Added search in clear into DisplaySystemControl...CJF
//    13 Dec 2001  Added flag to submitSearchRequest...CJF
//     9 Jan 2002  Changed close button to back...CJF
//     4 Feb 2002  Added DisplaySystemControl2 for the second layout of search screen...CJF
//    29 Jul 2002  Added tab stops and function to check if browse is displayed before displaying browse help...CJF
//    13 Sep 2002  Cleaned up source code...CJF
//     6 Dec 2002  Added trapShortcutKey function to trap enter keypress...CJF
//    04 Dec 2006  Fixed display bug in Firefox...SRK

function DisplayBrowseHelp( ) {
	if ( top.BrowseDisplay.browseData ) {
		displaySimpleHelp( );
	}
}

function DisplayHelpLayout2( ) {
	top.parent.OtherDisplay.document.open  ( ) ;
	top.parent.OtherDisplay.document.writeln( aHelpTextPrompt ) ;
	top.parent.OtherDisplay.document.close ( ) ;
	top.parent.BrowseDisplay.document.open ( ) ;
	top.parent.BrowseDisplay.document.writeln( aHelpTextProcess ) ;
	top.parent.BrowseDisplay.document.close( ) ;
}

function generateSimplePopulatedPointerPrompt( 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 ' + prName + '" TABINDEX="1">\n';
	if ( flBlank == true ) {
		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 clearSearchLayout2( ) {
	top.BrowseControl.document.SearchForm.reset( ) ;
	DisplayHelpLayout2( ) ;
}

function swapImagesSearch( imageName, imageSource ) {
	if (imageName.name) {
		this.src = imageSource ;
		alert( this.src ) ;
	} else {
		document.images[imageName].src = imageSource ;
	}
}

//-----------------------------------------------------------------------------
// Handler (button onClick): Submit the search request and send reply to BrowseDisplay control frame. 
// Based on function - submitSearchRequest (searchCommon.js)
function buildControlSearchRequest( ) {
	top.BrowseControl.document.SearchForm.target = top.BrowseDisplay.name ;
	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" ) )
	   ) {
		document.SearchForm.action += "&pointerExpansion=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" ;
	}
	return true ;
}

function submitControlSearchRequest( ) {
	top.BrowseControl.SearchForm.submit( ) ;
}

function oldsubmitControlSearchRequest(cmdName,inbSave) {
	if ( top.parent.BrowseDisplay.bSearchSubmit == true ) return true;
	if (!validateRequiredPrompts()) return false;
	var requestURI = cmd_action
		+ "&" + cmdName + "="
		+ "&PromptID=" + escape(sys_promptid)
		+ "&ParamID=" + escape(sys_paramid) ;
	for (var i = 0; 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" ) )
	   )
	{
		requestURI += "&pointerExpansion=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" ;
        }
        top.parent.BrowseDisplay.bSearchSubmit = true ;
        submitSearch( requestURI ) ;
}


//-----------------------------------------------------------------------------
// Utility to submit a browseRequest.
// NOTE : This is required for Netscape 6 to function correctly.
function submitSearch( requestURI ) {
    parent.BrowseDisplay.window.location = requestURI ;
}

// Utility: submit search request on enter.1
function submitControlEnter() {
	submitControlSearchRequest( 'CMD_(SearchRequest)[0]', false ) ;
}

// Use for hyperlinks in the public search help.
// *** REMOVE THIS FUNCTION LATER ***
function showWindow( insURL, insName, w, h, x, y ) {
	var aWin = new windowProperties( "1|0|0|1|1|1|0|0|" + ( w * 100 ).toString( ) 
	+ "|" 
	+ ( x * 100 ).toString( ) 
	+ "|" 
	+ ( y * 100 ).toString( ) 
	+ "|" 
	+ ( w * 100 ).toString( ) 
	+ "|"
	+ "|"
	) ;
	( aWin.openNewWindow( insName, insURL ) ).focus( ) ;
}

// Utility: submit search request on enter
function trapShortcutKey( event, browser )  {
	if ( browser && browser != "ie" ) {
		if ( window.event.keyCode == 13 ) {
			buildSearchRequest( top.windowInfo ) ;
			submitSearchRequest( 'CMD_(SearchRequest)[0]' ) ;
		}
	}
}
