//#################################################################################################

function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
  //alert(lcSearchTerm);
  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)) {			
			//#//  Debug  alert("||" + Trim(bodyText.substr(i-1,searchTerm.length+1)).toLowerCase() + "|| # ||" + searchTerm + "|| ## ||" + Trim(bodyText.substr(i-1,1)) + "||");			
			//alert(bodyText.substr(i,searchTerm.length));
			if ( (bodyText.substr(i,searchTerm.length).toLowerCase() == lcSearchTerm ) || ( Trim(bodyText.substr(i-1,1)) == ">"))
			{
			  newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
				//newWindow = window.open('', 'newWin')
				//newWindow.document.write("####################");
				//newWindow.document.write(newText);
				//newWindow.document.write("####################");
				//newWindow.document.close() 
			  bodyText = bodyText.substr(i + searchTerm.length);
			  lcBodyText = bodyText.toLowerCase();
			  i = -1;
		}
        }
      }
    }
  }
  
  return newText;
}
//#################################################################################################				
function highlightSearchTerms(searchText, bodyText,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") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
   if (bodyText == null)
  {
	bodyText = document.body.innerHTML;
  }
  var SiteArr = new Array (5);
   SiteArr[0] = "<font style='color:blue; background-color:yellow;'>";
   SiteArr[1] = "<font style='color:blue; background-color:#FFCCFF;'>";
   SiteArr[2] = "<font style='color:blue; background-color:#FFCC99;'>";
   SiteArr[3] = "<font style='color:blue; background-color:#99FFCC;'>";
   SiteArr[4] = "<font style='color:blue; background-color:#CCFFFF;'>";
  for (var i = 0; i < searchArray.length; i++) {
	  var ColorIndex = (i%5);
	highlightStartTag= SiteArr[ColorIndex];	
	//#// Highlight only strings with more the 1 char
	if (searchArray[i].length >= 1)
	{
		bodyText = doHighlight(bodyText, searchArray[i] , highlightStartTag, "</font>");
	}
  }
  
  document.body.innerHTML = bodyText;
  return true;
}			


function highlightBody()
	{
	var strQueryString = new String(window.location.search);
	if (strQueryString != "")
		{
		var intSearchTermStart = strQueryString.indexOf("st=",0);
		if (intSearchTermStart > -1)
			{			
			var TSearchTerm = document.getElementById("KupaRightBar1_SearchBox1_txtSearchTerm");			
			if (TSearchTerm != null && TSearchTerm.value != "") 
				{
				highlightSearchTerms(TSearchTerm.value);			
				}			
			}			
		}		
	}
	
//##############################################################################################
	function GetKeyCode(keyCode) 
	{
	  		
	if (keyCode == 13 )
		{		
			document.getElementById("KupaRightBar1_SearchBox1_btnSearch").click();
		}	
	}