
String.prototype.highlight = function(f, c, i) {

	var r = this, t = /([(){}|*+?.,^$\[\]\\]\n\r\s)/g, i = !i ? "i" : "", rf = function(t, i){
		//return r.lastIndexOf("<", i) > r.lastIndexOf(">", i) ? t : c(t, p); // works in IE

		var outsideTag = r.lastIndexOf(">", i) > r.lastIndexOf("<", i);

		//var withinContent = (r.lastIndexOf('class="body', i) >= r.lastIndexOf('class="docdates"', i)) && (r.lastIndexOf('class="body', i) >= r.lastIndexOf('class="title"', i)); // ok for non-IE

		var bodyPos = r.lastIndexOf('class="body', i);
		if (bodyPos==-1) // maybe IE didn't see it yet coz it strips the quotes
		{
			bodyPos = r.lastIndexOf('class=body', i);
		}

		var docdatesPos = r.lastIndexOf('class="docdates"', i)
		if (docdatesPos==-1) // maybe IE didn't see it yet coz it strips the quotes
		{
			docdatesPos = r.lastIndexOf('class=docdates', i);
		}

		var titlePos = r.lastIndexOf('class="title"', i);
		if (titlePos==-1) // maybe IE didn't see it yet coz it strips the quotes
		{
			titlePos = r.lastIndexOf('class=title', i);
		}

		//fixed bug #180 set bodyPos to -1 instead of 0
		var withinContent = bodyPos<-1 || (bodyPos > docdatesPos) && bodyPos<0 || (bodyPos > titlePos);

		var outsideScriptTags = r.lastIndexOf('/script>', i) >= r.lastIndexOf('<script', i);

		//alert( r.lastIndexOf('class="body', i) );
		//alert("outsideTag = "+outsideTag+"\nwithinContent = "+withinContent+"\noutsideScriptTags = "+outsideScriptTags);

		var doHilite = outsideTag && withinContent && outsideScriptTags;
		var ret = ( doHilite ) ? c(t, p) : t;

		return ret;
	};

	// org: var strStopchars = ' \n\r';
	var strStopchars = '[ \n\r]+';

	for(var p = -1, l = (f = f instanceof Array ? f : [f]).length; ++p < l;)
	{
		//alert("p="+p+" f.l="+f[p].length + " f[]=" + f[p][(f[p].length)-1]);
		if (f[p].substr(f[p].length-1,1)=='*') // wildcard used
		{
			f[p] = new RegExp("\\b"+f[p].replace(/\*$/,"\\w*"), "gm" + i);
			//alert("fp=" + f[p]);
		}
		else
		{
			f[p] = "\\b" + f[p] + "\\b";
			f[p] = new RegExp(f[p].replace(t, "\\\$1"), "gm" + i);
			//alert(f[p]);
		}

		//f[p] = f[p].replace(t, "\\\$1"));

		r = r.replace(f[p], rf);
	}
	return r;
}


function trimAll(sString)
{
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1,sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function specialChars(sString)
{
	var sNewstring = '';
	var sSpchars = "+\\\/\-";
	var sReplace = '[\n\t\r\\\s\~\+\/\-]+';


	for (n=0;n<sString.length;n++)
	{
		if (sSpchars.indexOf(sString.charAt(n))>=0)
		{
			sNewstring += sReplace;
		}
		else
		{
			sNewstring += sString.charAt(n);
		}
	}
	return sNewstring;
}

function addHighlightTags(s, i){
    return '<span class="highlight">' + s + '</span>';
}

/*
 * 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(searchText, treatAsPhrase, 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

	var strHaystack = document.getElementById('fulldoc');
	if ( !strHaystack || typeof(strHaystack.innerHTML) == "undefined" ) {
		return false;
	}
	var bodyText = strHaystack.innerHTML;
	//alert(bodyText);

	searchText = specialChars(searchText);

	if (treatAsPhrase) {
		var searchArray = [searchText];
	} else {
		var searchArray = trimAll(searchText).split("`");
	}

	/*
	for (var i = 0; i < searchArray.length; i++) {
		bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
	}
	*/

	bodyText = bodyText.highlight(searchArray,addHighlightTags);

	strHaystack.innerHTML = bodyText;
	return true;
}


// dummy function to prevent javascript error while navigating trough resultset
function CountCheck(){}
