/*****************************************************************/
/*  This file contains functions related to the highlighting     */
/*  routine that occurs once a user clicks on a link returned on */
/*  the result page subsequent to a search.                      */
/*****************************************************************/

// set the value of the searchText variable to its French default value
var searchText = "Chercher...";

// if the loaded page is in English, as determined
// by the "isEnglish()" function located in the "translate.js" 
// file, set the searchText variable to its English default value
if (isEnglish()) { searchText = "Search..."; }

// recover, with the "read_cookie()" function located in the "jse_form.js"
// file, any search term(s) from the cookie created upon triggering a
// search and keep it (them) in memory for future use.
var originald1 = read_cookie("mampurdc");

/*****************************************************************/
/*  Function that loads search terms from form field into        */
/*  the searchText variable when called.                         */
/*****************************************************************/
function setsearchText() { searchText = document.jse_Form.d.value; }

/*****************************************************************/
/*  Function that deletes a cookie by setting its expiry date to */
/*  yesterday.                                                   */
/*****************************************************************/
function delete_thiscookie(name) {
	var date = new Date();
	date.setTime(date.getTime()-(24*60*60*1000)); // minus one day (i.e. yesterday)
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

/*****************************************************************/
/*  Function that loads the search terms into the form field and */
/*  clears the cookie.                                           */
/*****************************************************************/
function return_pagequery() {
	if (originald1 != "") { // if there was a cookie when this file was loaded, its value would be in originald1
	document.jse_Form.d.value = originald1;
	document.cookie = "mampurdc=; path=/"; // erasing cookie content (but not the cookie itself)
	setsearchText(); // inserts the search terms from the form field into the searchText variable		
	}
}

/*****************************************************************/
/*  This is the function that actually highlights a text string  */ 
/*  by adding <SPAN>...</SPAN> tags before and after all         */
/*  occurrences of a search term. The <SPAN> tag's highlight     */
/*  color and font can be altered in a css style sheet with a    */
/*  simple entry in the form of:                                 */
/*  span.searchword {background-color:#D9EFB9; color:blue;}      */
/*****************************************************************/
function highlightWord(node,word) {
	// Iterate into this node's childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}
	// And do this node itself
	if (node.nodeType == 3) { // text node
		// make the highlight function case insensitive
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		// remove accents to account for those that do not have a QWERTY keyboard
		// i.e. set it up so that a search for État, état, Etat or etat, for example,
		// return the same results and are all properly highlighted!
		tempNodeVal = stripVowelAccent(tempNodeVal);
		tempWordVal = stripVowelAccent(tempWordVal);
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span"); // word will be highlighted with a <span> element
				hiword.className = "searchword";         // associated with a searchword class.
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

/*****************************************************************/
/*  This is sort of a wrapper function to the "highlightWord"    */
/*  function. It takes the searchText that you pass, optionally  */
/*  splits it intoseparate 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. Note: this script must be called in the <body> tag  */
/*  with onload="highlightSearchTerms('searchText');"            */
/*****************************************************************/
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure) {
	if((searchText != "Chercher...")&&(searchText != "Search...")) { // i.e. if there are search terms
		// 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 (the latter is the default setting)
		if (treatAsPhrase) {
		searchArray = [searchText];
		} else {
		searchArray = searchText.split(" ");
		}

		if (!document.body || typeof(document.body.innerHTML) == "undefined") {
			if (warnOnFailure) {
			alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
			}
		return false;
		}

		for (var i = 0; i < searchArray.length; i++) {
		// for each word in the search form, go through the highlighting routine
		highlightWord(document.getElementsByTagName("body")[0],searchArray[i]);
		}
		originald1 = ""; // erasing search terms
		return true;
	}
}

