﻿// JScript File
		function addLoadEvent(func) {
		  var oldonload = window.onload;
		  if (typeof window.onload != 'function') {
			window.onload = func;
		  } else {
			window.onload = function() {
			  oldonload();
			  func();
			}
		  }
		}
		addLoadEvent(DisplayRating);
		function CheckExpand(ClientID)
			{
				var ExpandAll = document.getElementById(ClientID);	
				if (ExpandAll.value == 'true')
				{
					ExpandTreeView(ClientID);
					ExpandAll.value = 'false';
				}
			}
		function DisplayRating()
		{
			var allRatings = document.getElementsByTagName("div");
			for(var i=0; i < allRatings.length; i++)
			{
				var uniqueID = allRatings[i].id;
				if (uniqueID.indexOf("DisplayRating") > 0)
				{
					var children = allRatings[i].childNodes;
					for (var j=0; j < children.length; j++)
					{
						if (children[j].type == 'hidden')
						{
							SetRating(parseInt(children[j].value), allRatings[i]);
						}
					}
				}
			}
		}
		function SetRating(rating, ratingInstance)
		{
			switch (rating)
				{
					case 1:
						ratingInstance.setAttribute("title", "OK");
						break;
					case 2:
						ratingInstance.setAttribute("title", "Helpful");
						break;
					case 3:
						ratingInstance.setAttribute("title", "Very Helpful");
						break;
					case 4:
						ratingInstance.setAttribute("title", "Extremely Helpful");
						break;
					case 5:
						ratingInstance.setAttribute("title", "Perfect");
						break;
					default:break;
				}
		}
        function ShowRating()
         {
			var modal = document.getElementById("ModalDiv");
			var RatingDiv = document.getElementById("RatingPopup");
			modal.style.display = 'block';
			RatingDiv.style.display = 'block';
         }
         function CloseRating()
         {
			 var modal = document.getElementById("ModalDiv");
			var RatingDiv = document.getElementById("RatingPopup");
			modal.style.display = 'none';
			RatingDiv.style.display = 'none';
         }
         function ExpandTreeView(ClientID)
         {
			//make treeview divs visible
			var allDivs = document.getElementsByTagName("div");
			for(var i= 0; i < allDivs.length; i++)
			{
				if (allDivs[i].id.indexOf("TreeView") > 0)
				{
					allDivs[i].style.display = 'block';
				}
			}
			//change toggle image
			var allImgs = document.getElementsByTagName("img");
			for (var j =0; j < allImgs.length; j++)
			{
				var temp = allImgs[j].getAttribute("alt");
				if (temp.indexOf("Expand") >= 0)
				{
					allImgs[j].src = "../Images/toggle_collapse.gif";
				}
			}
			//set hidden variable
			var ExpandAll =  document.getElementById(ClientID);;
			ExpandAll.value = "true";
         }
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}
/****************************Script for highlighting search terms**************************/
function doHighlight(ClientID, aHighlight) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
    highlightStartTag = "<span class='highlight'>";
    highlightEndTag = "</span>";
  // 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 elementToHighlight = document.getElementById(ClientID);
  var newText = "";
  var i = -1;
  var bodyText = elementToHighlight.innerHTML;
  var lcBodyText = bodyText.toLowerCase();
	while (bodyText.length > 0) 
		{
		 for (var j=0; j < aHighlight.length-1; j ++)
		  {
			var searchTerm = aHighlight[j];
			var lcSearchTerm = aHighlight[j].toLowerCase();
			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;
				}
			  }
			}
		  }
		  }
  elementToHighlight.innerHTML = newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(ClientID, searchText)
{
  if (trim(searchText) == '')
	return;
  var SearchWords = new Array();
  var count = 0;
  var nextQuote, firstQuote;
  firstQuote = searchText.indexOf('"');
  nextQuote = searchText.substring(firstQuote+1).indexOf('"');
  for (var j=0; j < searchText.length; j++)
  {		
		SearchWords[count] = searchText.slice(firstQuote+1, nextQuote+1);
		count++;
		searchText =  searchText.substring(nextQuote+1);
		firstQuote = searchText.indexOf('"');
        nextQuote = searchText.substring(firstQuote+1).indexOf('"');
  }
  var strHighlight = SearchWords[0]+",";
  for (var i=1; i < SearchWords.length; i++)
  {
	if (SearchWords[i].match("not"))
	{
		i++;
		continue;
	}
	else if (SearchWords[i].match("and"))
		continue;
	else if (SearchWords[i].match("or"))
		continue;
	else
		strHighlight = strHighlight + SearchWords[i]+",";
  }
  var aHighlight = strHighlight.split(',');
  doHighlight(ClientID, aHighlight);
}
