// Highlights a text string by adding a <span> tag around all occurrences of the supplied search term
function doHitHighlight(bodyText, searchTerm) 
{
	highlightStartTag = "<span class='HitHighlight'>";
	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 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 {
			// 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;
				}
			}
		}
	}
	
	return newText;
}

// This is a wrapper function for doHighlight() that has been customized for use with DtSearch.
function highlightDtSearchTerms(ContainerID, doc)
{
	if(!doc) doc = document;
	
	if(!ContainerID || typeof ContainerID == "undefined") {
		ContainerID = 'SpContent_Container';
	}
	
	var container = doc.getElementById(ContainerID);
	
	if(!container) container = doc.body;
	
	if(!container || typeof container.innerHTML == "undefined") return;
	
	var rx = new RegExp(/(\&|\?)hhSearchTerms=([^\&]+)/gi);
	var match = rx.exec(doc.location.search);
		
	if(!match || match.length<2) return;
	
	var searchText = urlDecode(match[2]);
	
	if(searchText.length==0) return;
	
	//strip "not", "+", "-" operators and double-quotes from search text
	searchText = searchText.replace(/\bnot\b|\b\+|\b-|\"/gi, "");
	
	//split the string on "and", "or" operators into an array of words and phrases
	var searchArray = searchText.split(/\band\b|\bor\b/gi);
	
	if(searchArray.length==0) return;
	
	var newHTML = container.innerHTML;
	for (var i = 0; i < searchArray.length; i++) {
		var searchTerm = searchArray[i];
		
		//strip leading and trailing whitespace from the search term
		searchTerm = searchTerm.replace(/^[\s]+/, "");
		searchTerm = searchTerm.replace(/[\s]+$/, "");
		
		newHTML = doHitHighlight(newHTML, searchTerm);
	}
	
	container.innerHTML = newHTML;
	return;
}
