/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* 	
	Modified 06/15/2006 by Martin Baer
	* to look at URL instead of refer (for internal searching, not necessarily coming in from a search engine)
	* exclude boolean and proximity operators from being highlighted
	* remove asterisks (wildcard charachter)
	* search for quoted strings together
	* if double-quoted term, include a single pair of quotes in the highlighted search.
*/
if (document.URL.toLowerCase().indexOf('search2.asp') < 0 ) {
	function highlightWord(node,word) {
		// Iterate into this nodes childNodes
		if (node.hasChildNodes) {
			var hi_cn;
			for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
				highlightWord(node.childNodes[hi_cn],word);
			}
		}
		putAnchor = false;
		// And do this node itself
		if (node.nodeType == 3) { // text node
			tempNodeVal = node.nodeValue.toLowerCase();
			tempWordVal = word.toLowerCase();
			if (tempNodeVal.indexOf(tempWordVal) != -1) {
				pn = node.parentNode;
				if (pn.className != "searchword") {
					// word has not already been highlighted!
					nv = node.nodeValue;
					foundString = foundString + nv.replace(/\n/g,' ... ') + ' ... ';
					ni = tempNodeVal.indexOf(tempWordVal);
					// Create a load of replacement nodes
					before = document.createTextNode(nv.substr(0,ni));
					docWordVal = nv.substr(ni,word.length);
					after = document.createTextNode(nv.substr(ni+word.length));
					hiwordtext = document.createTextNode(docWordVal);
					hiword = document.createElement("span");
					hiword.className = "searchword";
					hiword.appendChild(hiwordtext);
					pn.insertBefore(before,node);
					pn.insertBefore(hiword,node);
					pn.insertBefore(after,node);
					pn.removeChild(node);
				}
			}
					
		}
	}
	
	function SearchHighlight() {
	
		// quit if this function has already been called
		if (arguments.callee.done) return;
	
		// flag this function so we don't do the same thing twice
		arguments.callee.done = true;
	
		// kill the timer
		if (_timer) {
			clearInterval(_timer);
			_timer = null;
		}
		
		if (!document.createElement) return;
		ref = document.URL;
	
		foundString = 'Abstract: ';
		qs = ref.substr(ref.indexOf('?')+1);
		qsa = qs.split('&');
		for (i=0;i<qsa.length;i++) {
			qsip = qsa[i].split('=');
				if (qsip.length == 1) continue;
				if (qsip[0] == 'q') { 
					words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);				
						for (w=0;w<words.length;w++) {
							dub = false; // set double quote flag to false for this term
							if (words[w].toLowerCase() != 'and' && words[w].toLowerCase() != 'or' && words[w].toLowerCase() != '&' && words[w].toLowerCase() != 'not' && words[w].toLowerCase() != 'near') {
								words[w]=words[w].replace(/\*/g,''); //remove all asterisks
								if (words[w].indexOf('"') != -1)  {  //found a "
									if (words[w].indexOf('""') != -1) // if double quote, set flag to put one pair back in.
										dub = true;
									if (words.length ==1) { // only one search term
										findWord=words[w].replace(/"/g,''); //remove all quotes
										 if (dub == true )
											findWord = '"' + findWord + '"'; //add one pair of quotes back in
									} // close if for one search term
									else { // more than one search term
										findWord = words[w].substring(words[w].indexOf('"')+1,words[w].length) + ' '; 
										for (x=w+1;x<words.length;x++) { // loop through the rest of the words to find closing "
											if (words[x].indexOf('"') != -1) { //found the closing "
												for (z=w+1; z<x; z++) { // add all previous words to the string 
													findWord = findWord +words[z] + ' ';
												}
												findWord = findWord + words[x].substring(words[x],words[x].lastIndexOf('"')) // add closing " word to string
												w=x; // continue to find words after the closing " word
												x=words.length; // force exit of x loop
											} 
										} // for (x=w+1;x<words.length;x++)
									} // end else for only one search term
									w++;
								} // close if for checking quotes
								else
									findWord = words[w];
								
								highlightWord(document.getElementsByTagName("body")[0],findWord);
							} // end boolean exclusion
						} //for (w=0;w<words.length;w++) {
				} //  if (qsip[0] == 'q')
		} //  for (i=0;i<qsa.length;i++)
	
	}  //  function
	
	/* http://dean.edwards.name/weblog/2006/06/again/ */
	/* for Mozilla */
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", SearchHighlight, null);
	}
	
	/* for Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
		var proto = "javascript:void(0)";
		if (location.protocol == "https:") proto = "https:///";
		document.write("<script id=__ie_onload defer src=" + proto + "><\/script>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				SearchHighlight(); // call the onload handler
			}
		};
	/*@end @*/
	
			
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)) { // sniff
		var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				SearchHighlight(); // call the onload handler
			}
		}, 10);
	}
	
	/* for other browsers */
	window.onload = SearchHighlight;
}