// Set global variables, group by function 
var n = 0; // Counter for finding a string 

var marklist = new Array(); // Array of all words marked, for performing unmark

var thekey = "?q="; // Marks how searchterms are denoted in the URL
var keysplit = "|"; // Marks how searchterms are separated in the URL
var keyend = "*" // Marks how searchterms are ended in the URL

// BEGIN function to find a string sequentially
function findinpage(str,win) {

	if (domData.isNN6Up) { 
	//	alert("Text marking is not supported by Netscape 6.");
		return false;
	} else if (domData.isIEMac) { 
	//	alert("Text marking is not supported by Macintosh versions of IE.");
		return false;
	}

	var txt, i, found;
	if (win == null) { win = this; }
	if (str == "") { return false; }

	if (domData.isNN4) {
		if (!win.find(str)) {
			while(win.find(str,false,true)) { 
				n++; 
			}
		} else { 
			n++;
		}
		if (n == 0) {
		//	alert(str + " was not found on this page.");
		}
	}

	if ((domData.isIE4Up) || (domData.isIE6) ) {
		txt = win.document.body.createTextRange();
		for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
			txt.moveStart("character", 1);
			txt.moveEnd("textedit");
		}
		if (found) {
			txt.moveStart("character", -1);
			txt.findText(str);
			txt.select();
			txt.scrollIntoView();
			n++;
		} else if (n > 0) {
			n = 0;
			findinpage(str,win);
		} else {
		//	alert(str + " was not found on this page.");
		}
	}

	return false;

} // END findinpage(str,win)


// BEGIN function to mark all occurences of string in a page
function markinpage(thestr,thewin) {

	if ((domData.isNN4) || (domData.isNN6Up)) { 
	//	alert("Text marking is not supported by Netscape browsers.");
		return false;
	} else if (domData.isIEMac) { 
	//	alert("Text marking is not supported by Macintosh versions of IE.");
		return false;
	}

	// get rid of any quote in the highlight string
	thestr = replaceChar(thestr, "\"", "");

	var txt, i;
	if (thewin == null) { thewin = this; }
	if (thestr == "") { return false; }

	if (domData.isIE4up) {
		txt = thewin.document.body.createTextRange();
		if (!txt.findText(thestr)) {
			//alert("\" " + thestr + "\" was not found on this page.");
			return false;
		} else if (marklist[thestr] != null) {
			//alert("\" " + thestr + "\" is already marked.");
			return false;
		}
		for (i = 0; txt.findText(thestr) != false; i++) {
			txt.execCommand("BackColor","false","#ffff55");
			txt.collapse(false);
		}
		marklist[thestr] = i;
	}

	return false;

} // END markinpage(str,win)



// BEGIN function to unmark a marked string
function unmarkinpage(thestr,thewin) {

	if ((domData.isNN4) || (domData.isNN6Up)){ 
	//	alert("Text marking is not supported by Netscape browsers.");
		return false;
	} else if (domData.isIEMac) { 
	//	alert("Text marking is not supported by Macintosh versions of IE.");
		return false;
	}
	
	var txt, i;
	if (thewin == null) { thewin = this; }
	if (thestr == "") { return false; }

	if ((domData.isIE4Up) || (domData.isIE6) ) {
		txt = thewin.document.body.createTextRange();
		if (!txt.findText(thestr)) {
			//alert("\" " + thestr + "\" was not found on this page.");
			return false;
		} else if (marklist[thestr] == null) {
			//alert("\" " + thestr + "\" is not marked.");
			return false;
		}
		for (i = 0; txt.findText(thestr) != false; i++) {
			txt.execCommand("RemoveFormat");
			txt.collapse(false);
		}
		marklist[thestr] = null;
	}

	return false;

} // END unmarkinpage(str,win)



// BEGIN function create a URL with searchterms
function markurl(thefile,thestr) {
	var i, theurl, thechar;
	var q2 = false;
	var thenewstr = "";
	if (thefile == null) { return false; }
	if (thestr == "") { return false; }
	for (i=0; i < thestr.length; i++) {
		thechar = thestr.charAt(i);
		if (thechar == " ") { 
			if (q2) { 
				thechar = "%20"; 
			} else {
				thechar = keysplit;
			}
		}
		if (thechar == "\"") {
			if (q2) {
				q2 = false;
			} else {
				q2 = true;
			}			
		} else {
			thenewstr += thechar;
		}
		// Could add additional code to convert special characters -- then use the uncode function on the other end to handle them
	}
	theurl = thefile + thekey + thenewstr + keyend;
	var newwin = window.open("","newguy")
	newwin.location = theurl;
	newwin.focus();
} // End



// BEGIN function to extract a searchterm from a URL
function getterms(theref) {
	var pos, start, end;
	var txt, i;
	pos = theref.indexOf(thekey);
	if (pos == -1) { return false; } // The reference has no key
	start = pos + thekey.length; 
	end = theref.indexOf(keyend,start); 
	if (end == -1) { end = theref.length; }
	if (start == end) {return false; } // The key has no contents
	txt = theref.substring(start,end);
	var theterms = new Array();
	theterms = txt.split(keysplit);
	for (i = 0; i < theterms.length; i++) {
		txt = uncode(theterms[i]);
		markinpage(txt);
	}
} // End getterms(theref)



// BEGIN function to convert special characters as necessary
// Currently on
function uncode(thestr) {
	var respace = /%20/;
	thestr = thestr.replace(respace," ")
	return thestr;
} // END function to convert special characters as necessary



// BEGIN function to convert on character by another in all occurrences
// in the string
function replaceChar(theString, oldChar, newChar) {
	var i = 0;
	var j = theString.length;

	for(i=0; i < theString.length; i++) {
		if(theString.charAt(i) == oldChar) {
			theString = theString.substring(0,i) + newChar + theString.substring(i+1,theString.length);
			if(i > j) { // loop-killer, just in case we mess with the code
				break;
			}
		}
	}
	return theString;
} // End replaceChar(theString, oldChar, newChar)


