﻿function addToolTip(node,word,toolTipText) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
		//alert("addToolTip function called");
		//added by sj
			addToolTip(node.childNodes[hi_cn],word,toolTipText);
		}
	}

	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		//if ((tempNodeVal.indexOf(tempWordVal) != -1)) {
		if ((tempNodeVal.match(tempWordVal) == tempWordVal)) {

		var test=tempNodeVal.match(tempWordVal);
		
		//alert(test.toString() + "  "+tempWordVal.toString() + " match found");
		ni1 = tempNodeVal.indexOf(tempWordVal);
		        ws= tempNodeVal.charAt(ni1-1);
				len=tempWordVal.length;
				ws1= tempNodeVal.charAt(ni1+len);
				//alert(ws1);
				if (((ws1==" ")||(ws1=='')||(ws1=='.'))&&((ws==" ")||(ws=='')||(ws=='.'))) {
			pn = node.parentNode;
			if (pn.className != "searchword") {
			//alert("match found 1");
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				//alert(ni.toString());
				//alert(nv.substr(0,ni));
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				//alert(docWordVal);
				after = document.createTextNode(nv.substr(ni+word.length));
				//alert(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				//Code to provide tool tip with the word.
				hiword = document.createElement("span");
				//alert("appended span");
				// added by Sj
				//hiword.title="test";	
				hiword.title=toolTipText;	
				
				hiword.removeAttribute("style");
                hiword.setAttribute("style", "cursor:default;");
                hiword.style.cssText = "cursor:default; text-decoration:underline;";
               // hiword = document.createElement("a");
               // hiword.style.cssText = "text-decoration:underline;";

				// added by SJ for cursor set as hand	
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				//alert("appended");
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
				//alert("removed");
				
			}
		}
	}
	}
}

function unhighlight(node) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			unhighlight(node.childNodes[hi_cn]);
		}
	}

	// And do this node itself
	if (node.nodeType == 3) { // text node
		pn = node.parentNode;
		if( pn.className == "searchword" ) {
			prevSib = pn.previousSibling;
			nextSib = pn.nextSibling;
			nextSib.nodeValue = prevSib.nodeValue + node.nodeValue + nextSib.nodeValue;
			prevSib.nodeValue = '';
			node.nodeValue = '';
		}
	}
}
// Added by SJ
function localSearchHighlight(searchStr,varText) {
	if (!document.createElement) return;
        if (searchStr == '') return;
	// Trim leading and trailing spaces after unescaping
	searchstr = unescape(searchStr).replace(/^\s+|\s+$/g, "");
	if( searchStr == '' ) return;
	phrases = searchStr.replace(/\+/g,' ').split(/\"/);
	// Use this next line if you would like to force the script to always
	// search for phrases. See below as well!!!
	//phrases = new Array(); phrases[0] = ''; phrases[1] = searchStr.replace(/\+/g,' ');
	for(p=0;p<phrases.length;p++) {
	        phrases[p] = unescape(phrases[p]).replace(/^\s+|\s+$/g, "");
		if( phrases[p] == '' ) continue;
		if( p % 2 == 0 ) words = phrases[p].replace(/([+,()]|%(29|28)|\W+(AND|OR)\W+)/g,' ').split(/\s+/);
		else { words=Array(1); words[0] = phrases[p]; }
               	for (w=0;w<words.length;w++) {
			if( words[w] == '' ) continue;
			//Added by SJ
			addToolTip(document.getElementsByTagName("body")[0],searchStr,varText);
        	}
	}
}// 


