
/*
 * This is the function that highlights all occurrences of the search term. 
 */
function doColour(bodyText, searchTerm, index, tag) 
{
    if (tag==true){
		switch (index) {
			case 1: highlightStartTag = "<font style='color:blue; background-color:#FF0000;'>";
				highlightEndTag = "</font>"; break;
			case 2: highlightStartTag = "<font style='color:blue; background-color:#00FF00;'>";
				highlightEndTag = "</font>"; break;
			case 3: highlightStartTag = "<font style='color:blue; background-color:#66FFFF;'>";
				highlightEndTag = "</font>"; break;
			case 4: highlightStartTag = "<font style='color:blue; background-color:#FF00FF;'>";
				highlightEndTag = "</font>"; break;
			default: highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
				highlightEndTag = "</font>";
			}}
	else {
		highlightStartTag = "<font style='color:blue; background-color:#FFFFFF;'>";
		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;
}

function colourSearchTerms()
{
  
  var searchArray = getSearchTerms().split(" ");
  var bodyText = document.getElementById("label1").innerHTML;
  
  for (var i = 0; i < searchArray.length; i++) {
  
	  bodyText = doColour(bodyText, searchArray[i], i, done);
  }
  
  document.getElementById("label1").innerHTML = bodyText;
  return false;
}

function highlightSearchTerms()
{
  var searchArray = getSearchTerms().split(" ");
  var bodyText = document.getElementById("label1").innerHTML;
  
  for (var i = 0; i < searchArray.length; i++) {
        bodyText = doHighlight(bodyText, searchArray[i]);
  }
  
  document.getElementById("label1").innerHTML = bodyText;
  return false;
}

function doHighlight(bodyText, searchTerm) 
{
  highlightStartTag = "<b>";
  highlightEndTag = "</b>"; 
  
  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;
}



function highlight()
{
	var strSearch = document.getElementById("txtSearch").value; 
	if (done) {colourSearchTerms(); done = false;} else {colourSearchTerms(); done = true;}
}
