function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<span class='hilite'>";
    highlightEndTag = "</span>";
  }

  // find the search term in the body text,
  // wrap tags around them to highlight them.
  // Uses string functions instead of regular expressions
  // to avoid messing up HTML tags, etc. Honors word breaks.

  // only look between these two tags
  var beginTag = "<!-- BEGIN_HIGHLIGHT (do not remove)-->";
  var endTag = "<!-- END_HIGHLIGHT (do not remove) -->";

  //find the begin tag
  igStartIdx = bodyText.toLowerCase().indexOf(beginTag.toLowerCase());
  
  // initialize the newText variable with everything up through the begin tag
  var newText = bodyText.substr(0, igStartIdx + beginTag.length);

  // remove everything before the begin tag from the bodyText
  bodyText=bodyText.substr( igStartIdx + beginTag.length );

  // find the end tag
  igEndIdx = bodyText.toLowerCase().indexOf(endTag.toLowerCase());

  // initialize this variable.
  var endNewText = "";

  // stick everyting to be ignored into endNewText;
  if (igEndIdx != -1 ) {
    endNewText = bodyText.substr( igEndIdx, bodyText.length - igEndIdx );

    // remove everything to be ignored from bodyText;
    bodyText = bodyText.substr( 0, igEndIdx );
  }

  // now, finally, do the main search and replace work.

  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();

  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
        // make sure there are word boundaries on either end
          if (lcBodyText.substr(i-1,2).match(/(\s|.)\b(\s|.)$/) && lcBodyText.substr(i-1+lcSearchTerm.length,2).match(/^\w\b(\S|\W)/)  ) {
			  newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
			  bodyText = bodyText.substr(i + searchTerm.length);
			  lcBodyText = bodyText.toLowerCase();
			  i = -1;
		  }
        }
      }
    }
  }

  return newText + endNewText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }

  if (!document.body || typeof(document.body.innerHTML) == "undefined" || !document.getElementById("documentcontent") || typeof(document.getElementById("documentcontent").innerHTML) == "undefined") {
    if (warnOnFailure) {
      //alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }

  // var bodyText = document.body.innerHTML;
  var bodyText = document.getElementById("documentcontent").innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    // STOP LIST //
    var termLC = searchArray[i].toLowerCase();
    if ( termLC != "and" && termLC != "of" && termLC != "or"  && termLC != "when"  && termLC != "a" && termLC != "the" && termLC != "not" && termLC.length != 0) {
    	bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
    }
  }

  // document.body.innerHTML = bodyText;
  document.getElementById("documentcontent").innerHTML = bodyText;
  return true;
}


/*
 * This function takes a referer/referrer string and parses it
 * to determine if it contains any search terms. If it does, the
 * search terms are passed to the highlightSearchTerms function
 * so they can be highlighted on the current page.
 */
function highlightForrSearchTerms(referrer, pageType) {
  // call the function in the onload event of your <body> tag,
  // like this:
  //   <body onload='highlightForrSearchTerms(document.referrer);'>

  // does a browser check to see if it supports innerHTML
  if(typeof document.body.innerHTML != 'string') {
    return false;
  }

  // does a browser check to see if it is Safari
  if(navigator.userAgent.toLowerCase().indexOf("safari") != -1) {
    return false;
  }

  //var referrer = document.referrer;
  if (!referrer) {
    return false;
  }

  if(!pageType) {
    pageType = "document";
  }

  var queryPrefix = "ntt=";
  var highlightStartTag = '<SPAN class="hilite">';
  var highlightEndTag = "</span>";
  if(pageType == "document") {
    if(readCookie('forrSearchPrefUrl') != null && readCookie('forrSearchPrefUrl').match("highdoc=0")) {
      highlightStartTag = '<SPAN class="hiliteoff">';
    }
  } else if(pageType == "surveyexplorer") {
    // use the highlightStartTag as is
    queryPrefix = "newsearch=";
  } else {
    if(referrer.match("high=0")) {
      highlightStartTag = '<SPAN class="hiliteoff">';
    } else if(readCookie('forrSearchPrefUrl') != null && readCookie('forrSearchPrefUrl').match("high=0")) {
      highlightStartTag = '<SPAN class="hiliteoff">';
    }
  }

  var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  
  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }

  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }

  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);
  // urldecode string, change pipes and pluses to spaces
  queryString = unescape(queryString);
  queryString = queryString.replace(/\|/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");

  // filter out 'not' phrases
  queryString = queryString.replace(/\s*\bnot\b.*?(\s\band\b|\s\bor\b|$)+/gi, "$1");

  // grab phrases in quotes and highlight them
  var re = new RegExp("\"(.*?)\"");
  while(queryString.match(re)) {
    // send phrase to get highlighted without space at beginning or end
    highlightSearchTerms(RegExp.$1.replace(/^\s*(.*?)\s*$/gi, "$1"), true, false, highlightStartTag, highlightEndTag);
    // remove the phrase from the querystring
    queryString = queryString.replace(re, "");
  }

  // remove ':', as it confuses things
  queryString = queryString.replace(/:/gi, "");
  // remove the quotes
  queryString = queryString.replace(/\"/gi, "");
  // remove whitespace at the beginning and end of the string
  queryString = queryString.replace(/^\s*(.*?)\s*$/gi, "$1");

  // if there is anything left to highlight, send it to the highlight function
  if(queryString.length != 0) {
    return highlightSearchTerms(queryString, false, false, highlightStartTag, highlightEndTag);
  } else {
    return false;
  }
}

function toggleHighlight () {
	for ( var j=0; j < document.links.length; j++ ) {
		if ( document.links[j].id == "toggle" ) {
			if ( document.links[j].innerHTML == "Turn Off Highlighting" ) {
				document.links[j].innerHTML = "Turn On Highlighting";
			} else if ( document.links[j].innerHTML == "Turn On Highlighting") {
				document.links[j].innerHTML = "Turn Off Highlighting";
			}
		}
	}
	var spans = document.getElementsByTagName("span");
	for ( var i = 0; i < spans.length; i++ ) {
		if (spans[i].className == "hilite") {
			spans[i].className = "hiliteoff";
		} else {
			if (spans[i].className == "hiliteoff") {
				spans[i].className = "hilite";
			}
		}
	}
}

