/* MENUS.JS is used by the main SRL screen to allow the drop down
   menus to operate. It also contains all the JavaScript that is used
   to run the Search Engine as well.*/

/* Set Global Variables */
//var moused = 0;
var eOpenMenu = null;				//Active open menu
var form = ""
var mouse = 0;
var multi = false;


//Browser Sniffer
var IE = false;
var N4 = false;
var N6 = false;
if (document.all) {
	IE = true;
} else if (document.layers) {
	N4 = true;
} else if (document.getElementById) {
	N6 = true;
} 

var theClass = "className"
if (N6) {
	theClass = "class"
}

/* Called when the main menu heading is clicked -- opens sub-menu. */
function OpenMenu(eMenu, eSrc){	
		/* Moves the first one just a bit more to the right. */
		if ( eMenu.id == "divMenuTopics" ) {
			eMenu.style.left = eSrc.offsetLeft + document.getElementById("divMenuBar").offsetLeft;
		/* Moves the last one just a bit more to the left. */
		} else if ( eMenu.id == "divMenuCustomize" ){
			eMenu.style.left = eSrc.offsetLeft + document.getElementById("divMenuBar").offsetLeft - 20; 
		} else {
		eMenu.style.left = eSrc.offsetLeft + document.getElementById("divMenuBar").offsetLeft - 10;
		}
		eMenu.style.top = document.getElementById("divMenuBar").offsetHeight + document.getElementById("divMenuBar").offsetTop;
		eMenu.style.visibility = "visible";
		eOpenMenu = eMenu;
}

/* Closes the menu sent to it */
function CloseMenu(eMenu){
		eMenu.style.visibility = "hidden";
		eOpenMenu = null;
}

/* Launched on mouseover -- changes color */
function menumouseover( theName ){
	var element = document.getElementById( theName );
	var ident = element.getAttribute("id");
	if ( ident.indexOf("tdMenuBarItem") > -1 ){
		element.style.color = "yellow"; 
	}
}

/* Attached to both I-Frames to close menus that were left open */	
function closeOpenMenu(){
	if (eOpenMenu) {
		CloseMenu(eOpenMenu)
	}
}

/* Launched when a sub-menu item is clicked */	
function menuonclick( menuName ){
		eSrc = document.getElementById( menuName );
		menuName = menuName.replace("tdMenuBarItem","divMenu");
		eMenu = document.getElementById( menuName );
		// Closes any currently open window
		if (eOpenMenu && eOpenMenu != eMenu) 
			{
				CloseMenu(eOpenMenu);
			}
			if (eMenu) 
			{
				OpenMenu(eMenu, eSrc);
			}
}

/* Changes color of main item when mouse leaves */	
function menumouseout( theName ){
	var element = document.getElementById( theName );
	var ident = element.getAttribute("id");
	if ( ident.indexOf("tdMenuBarItem") > -1 ){
		element.style.color = ""; 
	}
}

/* Changes color of sub-menu item when mouse crosses over it. */
function submouseover( eSrc ) {
	if (eSrc.style.color != "gray"){
		eSrc.style.color = "yellow"; 
	}
}
	
/* Changes color of sub-menu item when mouse leaves it. */
function submouseout( eSrc ){
	if (eSrc.style.color != "gray") {
		eSrc.style.color = "white";
	}
} 


function breakout() {
   if (parent.frames.length > 0) {
   	  parent.frames.location.href = "http://www.summitsite.com/"
    }
}


function CloseFloater () {
if (window.floater) {
		curURL = "" + window.floater.location
		if (curURL == "") {
		    }
		else {
			floater.close()}
	}
}

function fixReSize() {
	xslContainer.library.style.top = 150;
	xslContainer.library.style.left = 220;
	xslContainer.library.style.width = Math.max(315,document.body.offsetWidth - 220-4);
	xslContainer.library.style.height = Math.max(72,document.body.offsetHeight - 150-4);

	xslContainer.menu.style.height = Math.max(66,document.body.offsetHeight - 70 - 90 -20);
	
}

function setSearchText(theForm) {
	document.getElementById("topich").innerHTML = "Searching . . ."
	form = theForm
	setTimeout("PerformSearch(form)",10);
}

function closeAllWindows() {
	// Closes all windows that may be open.  Launched on refresh.  
	if (window.adWindow) {
		curURL = "" + window.adWindow.location
		if (curURL == "") {
		    }
		else {
			adWindow.close()}
	}
	if (window.adWindow1) {
		curURL = "" + window.adWindow1.location
		if (curURL == "") {
		    }
		else {
			adWindow1.close()}
	}
	if (window.adWindow3) {
		curURL = "" + window.adWindow3.location
		if (curURL == "") {
		    }
		else {
			adWindow3.close()}
	}
	if (window.adWindow4) {
		curURL = "" + window.adWindow4.location
		if (curURL == "") {
		    }
		else {
			adWindow4.close()}
	}
	if (window.adWindow5) {
		curURL = "" + window.adWindow5.location
		if (curURL == "") {
		    }
		else {
			adWindow5.close()}
	}
}

function checkHits() {
	if ( getCookie('M') == 0 || getCookie('M') == null ){
		top.document.getElementById("topich").innerHTML = "Searching . . .";
		top.document.getElementById("annot").style.visibility = "hidden";
	}
	setTimeout("checkHitsYes()",10);
}
	

/** The checkHits() function is used by the Lucene search engine to display the hits in the appropriate screen.  It basically just writes out all the information received from the search engine along with certain top-level formatting instructions. */
function checkHitsYes() {
	var error = false;
	if ( getCookie('M') == 0 || getCookie('M') == null ){
		var lib = top.frames[1].document;
		lib.write ("");
		lib.writeln ("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head>");
		lib.writeln ( "<title>Search Results</title>" );
		lib.writeln ( "<script>if(screen.availWidth <=800 || screen.availHeight <=600){document.write('<link rel=\"stylesheet\" href=\"css/summit600.css\" type=\"text/css\">');}else{document.write('<link rel=\"stylesheet\" href=\"css/summit.css\" type=\"text/css\">');}</script>");
		lib.writeln ( "<script src='js/Master.js' language ='JavaScript'></script></head>");
		lib.writeln ( "<body onunload = 'top.document.getElementById(\"topich\").innerHTML = \"Processing Document . . . Please Wait.\"' onload='pageTopper(\"\", \"\", \"Search Results\", \"\")'>" );
		// Check for Error
		if (searchIndex( document.getElementById("searchWord").value) == "Error"){
			lib.writeln ("<h2>Error</h2><p>There is an error in the format of your search query.  Please re-enter it. For details on how to word your query, please see <a href='SearchTips.html' >Search Engine Tips</a>.</p>");
			error = true;
		// Check for no hits
		} else if  (searchIndex( document.getElementById("searchWord").value) == ""){
			lib.writeln ( "<h2>Nothing Found</h2><p>There were no hits found on that search. Making your search request more general may result in more information being found. For other tips on how to use the search feature, please see <a href='SearchTips.html'>Search Engine Tips</a></p>");
		} else {
			lib.writeln ( searchIndex( document.getElementById("searchWord").value ) );
		}	 
		lib.writeln ( "</body></html>" );
		lib.close();
	
		// Run pageTopper (since onload won't run right away). 
		pageTopper ("", "", "Search Results for: " + document.getElementById("searchWord").value, "" );
	} else {
		winStats='toolbar=no,location=no,directories=no,menubar=no,'
		winStats+='scrollbars=yes,resizable=yes'
   		if (navigator.appName.indexOf("Microsoft")>=0) {
      		winStats+=',left=25,top=100,width=700,height=500'
    	} else {
      		winStats+=',screenX=25,screenY=100,width=700,height=500'
    	}
		if (window.floater) {
			curURL = "" + window.floater.location
			if (curURL == "") {
		   		floater = window.open("","",winStats) 
   				floater.focus() 
			} else {
			floater.focus()
			}
		} else {
   			floater = window.open("","",winStats) 
   			floater.focus() 
		}
		floater.document.write ("");
		floater.document.writeln ("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head>");
		floater.document.writeln ( "<title>Search Results</title>" );
		floater.document.writeln ( "<script>if(screen.availWidth <=800 || screen.availHeight <=600){document.write('<link rel=\"stylesheet\" href=\"css/summit600.css\" type=\"text/css\">');}else{document.write('<link rel=\"stylesheet\" href=\"css/summit.css\" type=\"text/css\">');}opener.firstSearchPage = true;</script>");
		floater.document.writeln ( "<script>function setOnClick() {theLinks = document.getElementsByTagName('A'); for ( a=0; a < theLinks.length; a++) { theLinks(a).onclick = setVariable;} } function setVariable() { opener.firstSearchPage = true; }</script>");
		floater.document.writeln ("</head><body onload='setOnClick()'> <base target='library'>");
		// Check for Error
		if (searchIndex( document.getElementById("searchWord").value) == "Error"){
			floater.document.writeln ("<h2>Error</h2><p>There is an error in the format of your search query.  Please re-enter it. For details on how to word your query, please see <a href='SearchTips.html' >Search Engine Tips</a>.</p>");
		error = true;
		// Check for no hits
		} else if  (searchIndex( document.getElementById("searchWord").value) == ""){
			floater.document.writeln ( "<h2>Nothing Found</h2><p>There were no hits found on that search. Making your search request more general may result in more information being found. For other tips on how to use the search feature, please see <a href='SearchTips.html'>Search Engine Tips</a></p>");
		} else {
			floater.document.writeln ( searchIndex( document.getElementById("searchWord").value ) );
		}	 
		floater.document.writeln ( "</body></html>" );
		floater.document.close();
	}
	// Placed parsed words into wordBuffer for use by new screens.
	if (!error) {
		document.getElementById("wordBuffer").innerHTML = breakOutWords( document.getElementById("searchWord").value);
	}
}


/* Takes the formatting out of the parsed text so the page search can
   highlight all the words in the query. */  
function breakOutWords( text ) {
	// Run text through parser that search engine uses (applet method)
	parsedText = document.getElementById("ap").parseInput(text);

	
	// Remove +, ", (, )
	var amper1 = new RegExp ( "[\+\(\)]", "gi" );
	var checkAmper = amper1.exec( parsedText );
	if ( !checkAmper ) {
		return parsedText;
	}
	parsedText = parsedText.replace ( amper1, "" );
		
	//Remove "NEAR" from query
	var near        = new RegExp( " near","gi" );
	var checkNear   = near.exec( parsedText );
	if ( checkNear ) {
		parsedText = parsedText.replace ( near, "" );
	}
	
	//Remove "AND" from query
	var andWord    = new RegExp( " and ","gi" );
	var checkAnd   = andWord.exec( parsedText );
	if ( checkAnd ) {
		parsedText = parsedText.replace ( andWord, "" );
	}
	
	//Remove "OR" from query
	var orWord     = new RegExp( " or ","gi" );
	var checkOr    = orWord.exec( parsedText );
	if ( checkOr ) {
		parsedText = parsedText.replace ( orWord, "" );
	}
	
	//Remove "NOT" from query
	var notWord     = new RegExp( " not ","gi" );
	var checkNot    = notWord.exec( parsedText );
	if ( checkNot ) {
		parsedText = parsedText.replace ( notWord, "" );
	}
	return parsedText; 
}
	
	
function searchIndex( text ){
	theText = text;
	if ( theText.indexOf( "\"" ) < 0 ) {
		theText = "\"" + theText + "\"";
	}
	return document.getElementById("ap").findHits( theText );
}


