var Old_HTML = '';
var New_HTML = '';

function HiliteText()
{
	if (!document.body) { return; }
	if (!document.body.innerHTML) { return; }
	if (!RegExp) { return; }

	var HiliteWords = GetHiliteWords();

	if (HiliteWords) {

		Old_HTML = document.body.innerHTML;
		New_HTML = "<span></span>" + Old_HTML + "<span></span>";
		
		var hl_class = new Array("hl1","hl2","hl3","hl4","hl5")

		for (var i = 0; i < HiliteWords.length; i++) {
			New_HTML = DoHilite(New_HTML, HiliteWords[i],'<span class="' + hl_class[i%hl_class.length] + '">', '</span>')
		}

		document.body.innerHTML = New_HTML;
	}
}


function DoHilite(HTMLstr, HTMLword, Prefix, Suffix)
{
	var re_text_only = new RegExp("(>[^<]*)(" + HTMLword + ")([^>]*<)", 'gi');
	re_text_only.compile;
	HTMLstr = HTMLstr.replace(re_text_only, "$1" + Prefix + "$2" + Suffix + "$3");
	return HTMLstr;
}


function GetHiliteWords()
{
	var search_string = unescape(location.search);

	// if there are no terms, bug out now
	if (search_string.indexOf('hq=') == -1) { return; }

	var re, a, smode;

	// get the search boolean mode
	re = new RegExp("[&?]smode=([^&]+)");
	re.compile;
	a = search_string.match(re);

	// set a default if one wasnt passed
	if (!a) { smode = 'or'; } else { smode = a[1]; }

	// get the search keywords
	re = new RegExp("[&?]hq=([^&]+)");
	re.compile;
	a = search_string.match(re);

	// just in case there arnt actually any
	if (!a) { return; }

	var words = a[1];

	if (smode == 'exact') {
		if (words.indexOf('+') > 0) {
			words = words.replace(/\+/g, ' ');
		}
		// return the phrase as a single element array
		words = new Array(words);
	} else {
		if (words.indexOf(' ') > 0) {
			words = words.split(' ');
		} else {
			words = words.split('+');
		}
	}

	return words;
}
