<!--

//  Javascript functions used on the Search and Summary-of-Matches pages

var w1 = null, is_nav = 0, is_nav3 = 0, is_nav4 = 0, is_nav4up = 0, is_ie  = 0;       
var is_ie3 = 0, is_ie4 = 0, is_ie4up = 0, is_ie5 = 0, is_ie5up = 0;

function DetermineBrowserTypeVersion()
{
	   var agt=navigator.userAgent.toLowerCase(); 
			   
	   // Note: On IE5, these return 4, so use is_ie5up to detect IE5. 
	   var is_major = parseInt(navigator.appVersion); 
	   var is_minor = parseFloat(navigator.appVersion); 


	   // Navigator Checks
	   is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('compatible') == -1)); 
       if( is_nav )
	   {
         is_nav3 = (is_nav && (is_major == 3)); 
         is_nav4 = (is_nav && (is_major == 4)); 
         is_nav4up = (is_nav && (is_major >= 4)); 
	   }

	   // IE Checks
	   is_ie   = (agt.indexOf("msie") != -1);
   if( is_ie )
   {
	 is_ie3  = (is_ie && (is_major < 4)); 
         is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")==-1) && (agt.indexOf("msie 6.0")==-1)); 
         is_ie4up  = (is_ie  && (is_major >= 4)); 
         is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1)); 
         is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
   }
}

function CreateSatellite( theURL, name, width, height, pos, options )
{
		DetermineBrowserTypeVersion();
		// Different Browsers have different ways to get the 
		// position and size of the browser window.
		if( is_nav4up && pos == "center" )
		{
		 	var x = window.screenX;
    	  	var y = window.screenY;
		  	var w = window.outerWidth;
		  	var h = window.outerHeight;
		  	var midx = x + (w - width)/2; 
		  	var midy = y + (h - height)/2; 
			w1 = window.open( theURL, name, "left=" + midx + ",top=" + midy + ",height="+ height + ",width=" +  width + options );
		}else if( is_nav4up && pos == "topright" )
		{
		 	var x = window.screenX;
    	  	var y = window.screenY;
		  	var w = window.innerWidth;
		  	var rightx = x + (w - width); 
		  	var topy = y + ( window.outerHeight - window.innerHeight ); 
			w1 = window.open( theURL, name, "left=" + rightx + ",top=" + topy + ",height="+ height + ",width=" +  width + options );
		}else if( is_ie5up && pos == "center" ) 
		{
			var x = window.screenLeft;
         	var y = window.screenTop;
			var w = document.body.clientWidth;
		 	var h = document.body.clientHeight;
			var midx = x + (w - width)/2; 
			var midy = y + (h - height)/2; 

			// Next bit forces IE5 to give focus and display the window in the correct position
		  	if( w1 != null && !w1.closed ){w1.close(); w1 = null;}			    
   		      
			w1 = window.open(theURL, name, "left=" + midx + ",top=" + midy + ",height="+ height + ",width=" +  width + options );
		}else if( is_ie5up && pos == "topright" ) 
		{
			var x = window.screenLeft;
         	var y = window.screenTop;
			var w = document.body.clientWidth;
		 	var h = document.body.clientHeight;
			var rightx = x + (w - width); 
			var topy = y; 

			// Next bit forces IE5 to give focus and display the window in the correct position
		  	if( w1 != null && !w1.closed ){w1.close(); w1 = null;}			       		      
			w1 = window.open(theURL, name, "left=" + rightx + ",top=" + topy + ",height="+ height + ",width=" +  width + options );
		}else{  
		    // Older browsers can't determine browser window pos/size and IE4 has probs
		    // closing and re-opening a window  	      
			w1 = window.open(theURL, name, "height="+ height + ",width=" +  width + options );
		}
		w1.focus();
		return w1;
}

//  Function which sets the currently selected option in a drop-down
//    to match a given value (e.g. from a substituted CGI parameter)
//

function set_select_list_from_parameter( selectlist, value, defaultvalue )
{
	var i;

	//  First try to select the option matching the supplied value
	for ( i = 0; i < selectlist.options.length; i++ )
	{
		var optionvalue = selectlist.options[ i ].value;
		if ( optionvalue.toUpperCase() == value.toUpperCase() )
		{
			selectlist.options[ i ].selected = true;
			return;
		}
	}

	//  Otherwise select any default (if supplied)
	if ( "" + defaultvalue != "undefined" )
	{
		for ( i = 0; i < selectlist.options.length; i++ )
		{
			var optionvalue = selectlist.options[ i ].value;
			if ( optionvalue.toUpperCase() == defaultvalue.toUpperCase() )
			{
				selectlist.options[ i ].selected = true;
				return;
			}
		}

	}
}

//  This function is called upon loading (or reloading) the Advanced
//    Search page.
//
//  It checks whether a value is currently selected for the drop-down
//    indicating whether the Citation and Full-Text Keyword searches
//    are combined with an AND or an OR operator.  If no value is
//    currently selected, it defaults to the AND operator.
//
//  Note that this drop-down gets reset during submissions which don't
//    include both kinds of keyword search (so as to prevent the operator
//    from appearing unnecessarily in the search criteria) and hence this
//    function needs to be called whenever the Advanced Search form is
//    being (re)displayed (e.g. when using the browser's back button from
//    the search results page)
//
function set_combine_keywords_option()
{
	if ( document.SEARCHFORM.COMBINE_KEYWORDS &&
		 document.SEARCHFORM.COMBINE_KEYWORDS.selectedIndex == -1 )
	{
		document.SEARCHFORM.COMBINE_KEYWORDS.selectedIndex = 0;
		//alert( "Set the Combine Keywords drop-down to AND" );
	}
}

//  Reset all the 'B_...' hidden fields in the search form.
//
//  These are used when displaying a browse list (when the field which
//    corresponds to whichever browse list needs to be displayed gets
//    filled with the value 'Submit').
//
//  This function is used to clear these fields prior to other types of
//    form submission (e.g. when executing a search, or switching between
//    the Basic and Advanced search screens)
//
function reset_browse_fields()
{
	//  Reset all the 'B_...' hidden fields to empty values
	var i;
	var elements = document.SEARCHFORM.elements;
	for ( i = 0; i < elements.length; i++ )
	{
		if ( elements[ i ].name.indexOf( "B_" ) == 0 )
		{
			elements[ i ].value = "";
		}
	}
}

//  Display a browse list when one of the 'Select from list' links on
//    the Basic or Advanced Search page is clicked
//
//  This populates the hidden form field corresponding to the browse
//    list (with a "B_..." name) and submits the search form.
//
//  The SCREEN form field, which specifies the search screen to return
//    to after using the list, is also set.
//
function show_browse_list( fieldname, screenname )
{
	//  Reset all the other browse list submission fields
	//
	reset_browse_fields();

	//  Set the SCREEN parameter to the name of the search screen
	//
	document.SEARCHFORM.SCREEN.value = screenname;

	//  Set the requested field to the value "Submit", to indicate
	//    which of the browse lists needs to be displayed
	//
	document.SEARCHFORM.elements[ fieldname ].value = "Submit";

	//  Set the ACTION parameter to "SearchOrBrowse" -- this will
	//    cause it to display the requested browse list
	//
	document.SEARCHFORM.ACTION.value = "SearchOrBrowse";

	//  Submit the form
	document.SEARCHFORM.submit();
	//alert( "Displaying browse list for: " + fieldname );
}

//  Display a browse list when one of the 'Select from list' links on
//    the Basic or Advanced Search page is clicked
//
//  This populates the hidden form field corresponding to the browse
//    list (with a "B_..." name) and submits the search form.
//
//  The SCREEN form field, which specifies the search screen to return
//    to after using the list, is also set.
//
function display_browse( fieldname, screenname, browsefile )
{
	//  Reset all the other browse list submission fields
	//
	reset_browse_fields();

	//  Set the SCREEN parameter to the name of the search screen
	//
	document.SEARCHFORM.SCREEN.value = screenname;

	//  Set the BFILE parameter to the name of the browse file
	//
	document.SEARCHFORM.BFILE.value = browsefile;

	//  Set the requested field to the value "Submit", to indicate
	//    which of the browse lists needs to be displayed
	//
	document.SEARCHFORM.elements[ fieldname ].value = "Submit";

	//  Set the ACTION parameter to "SearchOrBrowse" -- this will
	//    cause it to display the requested browse list
	//
	document.SEARCHFORM.ACTION.value = "SearchOrBrowse";

	//  Submit the form
	document.SEARCHFORM.submit();
	//alert( "Displaying browse list for: " + fieldname );
}

function display_es_browse( fieldname, screenname)
{
	//  Reset all the other browse list submission fields
	//
	reset_browse_fields();

	//  Set the SCREEN parameter to the name of the search screen
	//
	document.SEARCHFORM.SCREEN.value = screenname;

	//  Set the BFILE parameter to the name of the browse file
	//  Hardwired as this is a special case for the EEBO/ECCO subject browse
	//
	document.SEARCHFORM.BFILE.value = "slfs/subject_phrase_all.slf";
	document.SEARCHFORM.BSCFILE.value = "slfs/subject_component_all.slf";

	//  Set the requested field to the value "Submit", to indicate
	//    which of the browse lists needs to be displayed
	//
	document.SEARCHFORM.elements[ fieldname ].value = "Submit";

	//  Set the ACTION parameter to "SearchOrBrowse" -- this will
	//    cause it to display the requested browse list
	//
	document.SEARCHFORM.ACTION.value = "SearchOrBrowse";

	//  Submit the form
	document.SEARCHFORM.submit();
	//alert( "Displaying browse list for: " + fieldname );
}

//  Switch to an alternative Search page
//
//  This is used by the 'Advanced Search >>' link on the Basic Search
//    page, and vice versa.  (If these links just contained the fixed
//    URL for the search page, then it would be displayed with all
//    fields cleared -- when what we want is to pass through the
//    existing field values.)
//
//  This function displays the new search page by submitting all the
//    current form fields, as well as requesting an alternative
//    screen.
//
function switch_search_page( newpage )
{
	reset_browse_fields();

	//  Set the SCREEN parameter in the search form to be the
	//    requested alternative search template
	//
	document.SEARCHFORM.SCREEN.value = newpage;

	//  Set the ACTION parameter in the search form to be empty
	//    -- the default action is just to display the search form
	//
	document.SEARCHFORM.ACTION.value = "";

	//  Submit the form
	//
	document.SEARCHFORM.submit();
}

//  Output a 'Context-of-Matches' link (at the end of the SoM entry)
//    -- but only for those documents that have a non-zero hit count
//
function output_com_link( idref, hitcount, entryno, numentries, file, searchscreen, display, source, lionus, lionuk)
{
	if ( hitcount > 0 )
	{
		var url = "/search/com" +
				  "?action=Display";

		if ( "" + source == "var_spell.cfg" )
		{
			url += "&SOURCE=var_spell_com.cfg";
			url += "&USEALTCONFIG=Y";
		}
		else
		{
			url += "&SOURCE=config_com.cfg";
			url += "&USEALTCONFIG=Y";
		}

		url += "&ID=" + idref +
				  "&SUBSET=" + entryno +
				  "&ENTRIES=" + numentries +
				  "&FILE=" + file +
				  "&SEARCHSCREEN=" + searchscreen +
				  "&DISPLAY=" + display +
				  "&HITCOUNT=" + hitcount + 
				  "&SUBSCRIBER_TCP=Y";
		if ( lionus == "Y")
		{
				url +=  "&SUBSCRIBER_LIONAUTHUS=Y";
		}
		if ( lionuk == "Y")
		{
				url +=  "&SUBSCRIBER_LIONAUTHUK=Y";
		}

		var text = "Context of Matches (" +
				   ( hitcount == 0 ? "no hits" :
					 hitcount == 1 ? "1 hit" :
					 hitcount + " hits" ) + ") " +
				   "&gt;&gt;";
		document.write( '<BR>\n' +
						'<A HREF="' + url + '">' + text + '</A>\n' );
	}
}

//  Output the 'Your search produced N records' message, optionally
//    followed by the list of collection subset(s) that were searched
//    (for users who are subscribed to only part of the product)
//
//  This message follows the list of search criteria which is of
//    the form:
//    "You searched on: fieldname <b>value</b>; ..." or
//    "You searched for all records"
//
//  Note that, because of the contortions involved in implementing the
//    
//
function output_number_of_records_message( numrecords, numhits, subsets )
{
	var subsetmessage = "";
	if ( subsets == "ALL" )
	{
		//  User is subscribed to all of the product.  No message needed
	}
	else if ( subsets != "" )
	{
		subsetmessage = " (in subsets: " + subsets + ")";
	}
	if (numrecords < 0)
	{
		// if Opentext returns a negative number it has gone a bit wrong,
		// but there will be no hits...
		numrecords = 0;
	}

	var hitmessage = "" + numhits + " hit" + ( numhits > 1 ? "s" : "" );
	var numbermessage = ( numrecords == 0 ) ?
							"<br>Your search produced no records (see " +
							"<A HREF=\"/help/srch_res.htm#zero\">" +
							"help on search results</A>)" :
						( numrecords == 1 && numhits == 0 ) ?
							"<br>Your search produced one record" :
						( numrecords == 1 && numhits == 1 ) ?
							"<br>Your search produced one hit in one record" :
						( numrecords == 1 && numhits > 1 ) ?
							"<br>Your search produced " + numhits +
							" hits in one record" :
						( numrecords > 1 && numhits == 0 ) ?
							"<br>Your search produced " + numrecords + " records" :
						( numrecords > 1 && numhits == 1 ) ?
							"<br>Your search produced one hit in " +
							numrecords + " records" :
						"<br>Your search produced " + numhits + " hits in " +
							numrecords + " records";

	var message = numbermessage +
				  ( subsetmessage ? " &nbsp; " + subsetmessage : "" );

	document.write( message );
}

//  Extract the value of a single search term from a string of criteria
//    supplied to the 'output_you_searched_on' function
//
//  Returns an empty string if the requested term is not present
//
function get_search_term( term, criteria )
{
	var terms = criteria.split( "|" );

	var i;
	var pos, name, value;
	for ( i = 0; i < terms.length; i++ )
	{
		//  Split this term into name and value
		pos   = terms[ i ].indexOf( "=" );
		name  = terms[ i ].substring( 0, pos );
		value = terms[ i ].substring( pos + 1, terms[ i ].length + 1 );

		//  Remove surrounding whitespace from the name
		name = trim( name );

		//  Check if the requested term has been found
		if ( name.toUpperCase() == term.toUpperCase() )
		{
			//  Return the value of the term
			value = trim( value );
			//alert( "get_search_term: " + term + " = '" + value + "'" );
			return value;
		}
	}

	//  Term not found.  Return an empty string
	//alert( "get_search_term: " + term + " not found" );
	return "";
}

//  This function is the OnSubmit handler for the 'Basic Search'
//    form.
//
//  For a non-TCP user, the keyword search just applies to citations,
//    so the form just displays the 'CIT_KEYWORD' which is used for
//    the citation search, and no additional action is required.
//
//  For a TCP user, the keyword search applies to both citations and
//    full-text.  The form contains a 'BOTH_KEYWORD' box, and the
//    contents of this need to be copied into the 'CIT_KEYWORD' and
//    'FT_KEYWORD' fields that are used for the search.
//
function set_basic_search_fields()
{
	var recomma = new RegExp("\\,", "g");

	//  Set the fields associated with the 'BOTH_KEYWORD' text-box
	//    (which is displayed for a TCP subscriber only)
	//
	if ( document.SEARCHFORM.BOTH_KEYWORD )
	{
		var keywords = document.SEARCHFORM.BOTH_KEYWORD.value;
		var fixedkws = keywords.replace(recomma, "&#044;");
		document.SEARCHFORM.CIT_KEYWORD.value = fixedkws;
		document.SEARCHFORM.FT_KEYWORD.value = fixedkws;

		//  If the keywords field is non-blank then combine the
		//    citation and full-text fields with an "OR" operator;
		//    otherwise, when no keyword search is being performed,
		//    blank out the operator so it doesn't appear in the
		//    list of search criteria
		//
		document.SEARCHFORM.COMBINE_KEYWORDS.value = keywords ? "OPT_Or" : "";
	}

	//  Set the ACTION parameter in the search form to "SearchOrBrowse"
	//    so that the search will be executed.
	//
	//  This needs to be set explicitly, because it gets reset when
	//    following the 'Basic Search / Advanced Search' links to
	//    switch between the two search screens.
	//
	document.SEARCHFORM.ACTION.value = "SearchOrBrowse";

	//  Return true so that the form will be submitted
	return true;
}

//  This function is the OnSubmit handler for the 'Advanced Search'
//    form.
//
//  For a TCP user, it checks whether both types of keyword search
//    are being performed (on citations and full-text); if not, then
//    the option for combining these (with AND or OR) is suppressed,
//    so as not to appear in the 'You searched on' header on the
//    Search Results page (which would be ugly and confusing)
//
function set_advanced_search_fields()
{
	//alert( "Running set_advanced_search_fields ..." );

	//  Check whether both Citation and Full-Text Keywords have been
	//    specified.  If not, then blank out the select list which
	//    indicates how these two terms are combined (with AND or OR)
	//
	//  Note that these fields are only present for TCP subscribers,
	//    and so we need to first test for their existence
	//
	if ( document.SEARCHFORM.COMBINE_KEYWORDS &&
		 document.SEARCHFORM.CIT_KEYWORD &&
		 document.SEARCHFORM.FT_KEYWORD )
	{
		if ( document.SEARCHFORM.CIT_KEYWORD.value == "" ||
			 document.SEARCHFORM.FT_KEYWORD.value == "" )
		{
			document.SEARCHFORM.COMBINE_KEYWORDS.selectedIndex = -1;
			//alert( "Reset COMBINE_KEYWORDS drop-down" );
		}
	}

	//  For the multi-select lists where the first option is an instruction
	//    'Select a ...', need to de-select this option, since it will not
	//    be handled properly when used in conjunction with other options.
	//
	document.SEARCHFORM.ILLUSTYPE.options[ 0 ].selected = false;
	document.SEARCHFORM.LANGCODE.options[ 0 ].selected = false;
	document.SEARCHFORM.COUNTRY.options[ 0 ].selected = false;

	//  If no 'Full Text Keyword' has been entered, unset any of the
	//    selected options in the 'Limit Full Text Keyword Within'
	//    multi-select list.
	//
	//  Note that these fields are only present for TCP subscribers,
	//    and so we need to first test for their existence
	//
	var i;
	if ( document.SEARCHFORM.FT_KEYWORD &&
		 document.SEARCHFORM.FT_LIMIT &&
		 document.SEARCHFORM.FT_KEYWORD.value == "" )
	{
		for ( i = 0; i < document.SEARCHFORM.FT_LIMIT.options.length; i++ )
		{
			document.SEARCHFORM.FT_LIMIT.options[ i ].selected = false;
		}
	}

	//  Set the hidden fields corresponding to the options selected
	//    in the FT_LIMIT select box.  Each such selection corresponds
	//    to a separate search expression, so the default method for
	//    combining options in a mult-select list (which is to produce
	//    a composite value '"option1" OR "option2" ...') will not work
	//    in this case.
	//
	if ( document.SEARCHFORM.FT_LIMIT )
	{
		for ( i = 0; i < document.SEARCHFORM.FT_LIMIT.options.length; i++ )
		{
			var optname = document.SEARCHFORM.FT_LIMIT.options[ i ].value;
			document.SEARCHFORM.elements[ "FTLIMIT_"+optname ].value =
				document.SEARCHFORM.FT_LIMIT.options[ i ].selected ? "Y" : "";
		}
	}

	//  Set the ACTION parameter in the search form to "SearchOrBrowse"
	//    so that the search will be executed.
	//
	//  This needs to be set explicitly, because it gets reset when
	//    following the 'Basic Search / Advanced Search' links to
	//    switch between the two search screens.
	//
	document.SEARCHFORM.ACTION.value = "SearchOrBrowse";

	//  Return true so that the form will be submitted
	return true;
}

//  Trim leading and trailing whitespace from a string
//
function trim( text )
{
	//  Remove any leading spaces
	while ( text.length > 0 &&
			text.substring( 0, 1 ) == " " )
	{
		text = text.substring( 1, text.length + 1 );
	}

	//  Remove any trailing spaces
	while ( text.length > 0 &&
			text.substring( text.length, text.length + 1 ) == " " )
	{
		text = text.substring( 0, text.length );
	}

	return text;
}

//  Output successive entries for the 'Bib Name / Number' field on the
//    'Summary of Matches' or 'Full Citation' pages.
//
//  This needs to be truncated after a certain number of entries, so
//    as to avoid an excessively long field (e.g. for periodicals
//    with hundreds of issues).
//
//  Note that, when used on the 'Summary of Matches' page, the variable
//    'num_bibnames' needs to be reset to zero between each result.
//
var num_bibnames = 0;
var max_bibnames = 4;
function write_bibname( bibname, separator )
{
	if ( num_bibnames == 0 )
	{
		document.write( bibname );
	}
	else if ( num_bibnames < max_bibnames )
	{
		document.write( separator + bibname );
	}
	else if ( num_bibnames == max_bibnames )
	{
		document.write( separator + "etc." );
	}
	num_bibnames ++;
}

function do_export_cits( type )
{
	document.SEARCHFORM.DLTYPE.value = type;
	//  Submit the form
	document.SEARCHFORM.submit();
}

function fnRefWorks()
{
	var sCookies = cookieRead('ChEEBOMarked');
        // var sTemp = 'http://eebo.chadwyck.com:8086/citations/download/dorw?' + sCookies;
        // var sTemp = 'http://eebo.chadwyck.com:8086/citations/download/dorw';

	// Use the following line when using the 8086 test port
        // var sTemp = 'http://www.refworks.com/express/ExpressImport.asp?vendor=ProQuest&url=http://eebo.chadwyck.com:8086%2Fcitations%2Fdownload%2Fdorw?' + sCookies;
	// use the following line for the live 80 port
        var sTemp = 'http://www.refworks.com/express/ExpressImport.asp?vendor=ProQuest&url=http://eebo.chadwyck.com%2Fcitations%2Fdownload%2Fdorw?' + sCookies;

        // var sTemp = 'http://www.refworks.com/express/ExpressImport.asp?vendor=ProQuest&url=http://eebo.chadwyck.com:8086%2Fcitations%2Fdownload%2Fdorw';
        CreateSatellite(sTemp, 'RefWorks',800,600,"center"," scrollbars, status=yes, location=yes, toolbar=yes, menubar=yes ,resizable=1");
}
// -->
