/*
Text marker code 
Ashok Hariharan
*/


/*helper function , just does search & replace */

function replaceMe(str, phrase, chg) {
var pattern = new RegExp (phrase ,'ig');
return str.replace( pattern, chg);
}


/*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();
	}
	*/
}

/*main marker function*/
function markText(txtKeyword, inputHtml) 
{
           var re; 						/*regex object*/
           var varMatches; 					/*matches array*/
           var outHtml; 					/*output html*/
           var replaceText;
           replaceText = '<span style="background-color:yellow;color:red;font-weight:bold;">'+txtKeyword+ '</span>';
	   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 ....
	   */

	   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+=replaceMe(varMatches[2], txtKeyword, replaceText); 
	   }
	   return outHtml;
}


function main()
{
	/*get the href of the current page*/
	var ref = document.location.href;
	/*this stores the name of the query string that contains the keyword to be higlighted*/
	var hiliteUrl = new String('hilite');
	/*array for temp. use*/
	var arr;
	/*stores the keywords that are to be highlighted*/
	var hiliteText;
	

	/*split into array using & as a separator*/
	arr = ref.split('&');
	for (var i=0 ; i < arr.length; i++)
	{
		var idx = arr[i].indexOf(hiliteUrl + '=');	
		if (idx != -1)
		{
			/*unescape the string to be on the safe side*/
			hiliteText =  unescape(arr[i].substring(idx+hiliteUrl.length+1));
		
			/*replace any + character which might have been used to pad a space, with a space */
			hiliteText = replaceMe(hiliteText, '\\+', ' ');
		
			if (hiliteText.length != 0)
			{
				/*we have successfully parsed out the url now pass it to the highlight func.*/
				var inHtml;
				var outHtml;
				/*get the innerHtml content from within the div*/
				inHtml = '<em></em>'+getLayerHtml('contentdiv');		
				/*run the hilite function to hilite stuff*/
				outHtml = markText(hiliteText, inHtml);
				/*write the hilited stuff back to the div*/
				setLayerHtml(outHtml,'contentdiv');
			}
			return;
		}
	}
}

function onLoad()
{
/*1 - we parse out the hilite= querystring value */
main();
return;
}



