// Scripts related to the Highlighting functions

   function getHighlighted() {
      var txt='';
      if(window.getSelection) {
         txt=window.getSelection();
      } else if(document.getSelection) {
         txt=document.getSelection();
      } else if(document.selection) {
         txt=document.selection.createRange().text;
      }
      return txt;
   }

   function highlightSearchTerms(searchText,highlightStartTag) {
      if (!document.getElementById("storyface") || typeof(document.getElementById("storyface").innerHTML) == "undefined") {
         if (warnOnFailure) {
            alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
         }
      }
      var bodyText = document.getElementById("storyface").innerHTML;
      bodyText = doHighlight(bodyText, searchText, highlightStartTag);
      document.getElementById("storyface").innerHTML = bodyText;
   }

   function doHighlight(bodyText, searchTerm) {
      highlightStartTag="<font style='color:#62809C;'>";
      var highlightEndTag = "</font>";

      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 {
            if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
               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;
   }
