﻿// JScript File

function doHighlight(bodyText, searchTerm) 
{
    var highlightStartTag = "<font style='color:#333333; background-color:#FFCC33;'>";
    var highlightEndTag = "</font>";
    var newText = "";
    var i = -1;
  
    if (searchTerm.length > 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;
		            }
	            }
	        }
        }
    }
    else newText = bodyText;
    return newText;
}


function highlightSearchTerms(searchText, warnOnFailure)
{
    if(document.form1.Highlight.checked) {
        var queryString = document.form1.AuthorTextBox.value + "|" + document.form1.KeywordsTextBox.value;
            var searchArray = "";
            if (queryString != "|") {
                searchArray = queryString.split("|");
            }
  
    if (!document.body || typeof(document.body.innerHTML) == "undefined") 
    {
	    if (warnOnFailure) 
	    {
	        alert("Sorry, for an unknown reason the keywords cannot be highlighted.");
	    }
	return false;
    }
  
    var bodyText = document.body.innerHTML;
    for (var i = 0; i < searchArray.length; i++) 
    {
	    bodyText = doHighlight(bodyText, searchArray[i]);
    }
  
    document.body.innerHTML = bodyText;
    return true;
    }
}

function toggle_visibility(SearchOptions) 
{
   var e = document.getElementById(SearchOptions);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}