/*
 * Display dialog box that allows a user to enter their own search
 * term(s) to highlight on the page, and then pass the search text
 * or phrase to the highlightSearchTerms function. All parameters
 * are optional.
 */
function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
{
  // Prompt the user for any words to be highlighted on this web page
  if (!defaultText) {
    defaultText = "";
  }
  // Optionally specify custom highlight tag values
  if ((!textColor) || (!bgColor)) {
    highlightStartTag = "";
    highlightEndTag = "";
  } else {
    highlightStartTag = "<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";
    highlightEndTag = "</font>";
  }
  if (treatAsPhrase) {
    promptText = "Enter all or part of phrase to be highlighted:";
  } else {
    promptText = "Enter full or part word(s) separated by spaces:";
  }
  searchText = prompt(promptText, defaultText);
  if (!searchText)  {
    alert("No search terms entered - nothing to highlight.");
    return false;
  }
  return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
}
/*
 * Optionally parse search text into separate terms and transform the
 * text on the current web page. Only "searchText" parameter required,
 * all other parameters optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // If treatAsPhrase parameter is true then search for entire phrase
  // entered, otherwise split search string so that each term searched
  // for and highlighted individually.
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable.");
    }
    return false;
  }
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  document.body.innerHTML = bodyText;
  return true;
}
/*
 * Highlight a text string by adding HTML tags before and after
 * all occurrences of the search term(s). Tags can be passed or if
 * highlightStartTag or highlightEndTag parameters are omitted
 * or empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='background-color:#FFCC00;'>";
    highlightEndTag = "</font>";
  }
  // Find all occurrences of the search term(s) in the given text,
  // and add some "highlight" tags to them after filtering out
  // matches that occur within HTML tags and script blocks
  var newText = "";
  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)) {
          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;
}
