/*
Text marker code 
Ashok Hariharan
http://www.unganisha.org/home/pages/dhtmlmarker/

Butchered by Chris Maunder to allow multiple word match
and preservation of search word case. http://www.codeproject.com

SMX: Added regexp highlight function for whole word matching
*/


/*helper function , gets innerHtml of a layer */
/*code ripped from ppk : http://xs4all.nsl/~ppk/js/ */
function getLayerHtml(id)
{
	var inHtml = new String('');
	if (document.getElementById)
	{
		x = document.getElementById(id);
		inHtml = x.innerHTML;
	}
	else
	 if (document.all)
	{
		x = document.all[id];
		inHtml = x.innerHTML;
	}
	return inHtml;
}

/*helper function , sets the innerHtml of a layer */
/*code ripped from ppk : http://xs4all.nsl/~ppk/js/ */
function setLayerHtml(text,id)
{
	if (document.getElementById)
	{
		elemObj = document.getElementById(id);
		elemObj.innerHTML = text;
	}
	else if (document.all)
	{
		elemObj = document.all[id];
		elemObj.innerHTML = text;
	}
	/*
	this is ns4 compatible code ...
	*/
	/*
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>;';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
	*/
}

// Simple word replace
function replaceMe(str, phrase, chg) 
{
	var pattern = new RegExp (phrase ,'ig');
	return str.replace( pattern, chg);
}


// Wrap a word in a phrase with two strings.
// Input string contains 'phrase', is plain text and has no HTML tags.
// Output will be the string with 'phrase' wrapped by prefix and suffix strings.
function WrapMe(str, phrase, prefix, suffix) 
{
	var nPos;
	var result = "";
	var start	= 0;
	var Searchable = str.toLowerCase();
	var Length = phrase.length;
	
	phrase = phrase.toLowerCase();
	
	// mod SMX regular expression	and * masking
	// * and ? has to be fixed!!!!!!!!!!!!!!!1
	reStr = "(" + phrase + ")";
	if (phrase.indexOf("*") == 0) {
		phrase = phrase.slice(1);
		reStr = phrase;
	}
	else {
		reStr = "\\b" + reStr;
	}
	if (phrase.lastIndexOf("*") == phrase.length-1) {
		phrase = phrase.slice(0, -1);
		reStr = reStr.slice(0, -1);
	}
	else {
		reStr = reStr + "\\b";
	}
	//alert(reStr)
	var re = new RegExp(reStr,"gi");

	result = str.replace(re, prefix + "$1" + suffix);
	/* original code
	while ( (nPos = Searchable.indexOf(phrase, start)) >= 0 )
	{
		result += str.substr(start, nPos-start);
		result += prefix + str.substr(nPos, Length) + suffix;
		start = nPos + Length;
	}
	result += str.substr(start);
	*/
	
	return result;
}

// Adds markup to text by sequentially searching for blocks of non-marked up text 
// that contains the search phrase
function markText(txtKeyword, inputHtml, prefix, suffix) 
{

	var re = new RegExp("(\<[^>][^<]*\>)([^<]*)","g");	// create non-greedy regex match

	/*
	note : 
	in 99% of cases <[^>]*\>)([^<]*) 
	should also work , i did have the rare case where some invisible 
	characters caused this to crap out ....
	*/
	
	var varMatches;											// matches array 
	var outHtml = new String('');							// init html string 
	while ((varMatches = re.exec(inputHtml)) != null)		// exec sequentially to apply span tags
	{
		outHtml += varMatches[1]; 							// html tag part
		outHtml += WrapMe(varMatches[2], txtKeyword, prefix, suffix); 
	}
	return outHtml;

}

var sourceHtml = new Array();

// Highlights a bunch of words.
function Highlight(Keywords, container, refresh, prefix, suffix)
{
	if (sourceHtml[container] == null) {
		sourceHtml[container] = getLayerHtml(container);
	}
	var inHtml = (refresh) ? sourceHtml[container] : getLayerHtml(container);
	for (var i = 0; i < Keywords.length; i++) {
		inHtml = markText(Keywords[i], inHtml, prefix, suffix);
	}
	setLayerHtml(inHtml, container);
}



