var PAGELOADED = false;
function onDocLoad() {
	PAGELOADED = true;
}
function onDocUnload() {
	PAGELOADED = false;
}
function unselectOld() {
	if (lastActive != null) {
		removeClass(lastActive, "highlight");
	}
	lastActive = null;
}
function selectNew(element) {
	lastActive = element;
	addClass(lastActive, "highlight");
}
function remoteUnselect() {
	unselectOld();
}
function remoteSelect(id) {
	selectNew(document.getElementById(id));
}
function addClass(elementtoaddto, stringtoadd) {
	elementtoaddto.className += (hasClass(elementtoaddto, stringtoadd) ? "" : (elementtoaddto.className == "" ? "" : " ") + stringtoadd);
}
function hasClass(elementtocheck, classtofind) {
	return containsString(elementtocheck.className, classtofind, " ", false);
}
function containsString(stringtocheck, stringtofind, delimiter, casesensitive) {

	/* Takes a string that consists of several strings seperated by a delimiter.
	 * Returns whether or not another string is part of the string list.
	 */
	
	if (!casesensitive) {
		stringtocheck = stringtocheck.toLowerCase();
		stringtofind = stringtofind.toLowerCase();
	}

	return (stringtocheck == stringtofind) || // equals
		(stringtocheck.indexOf(delimiter + stringtofind + delimiter) != -1) || // contains
		(stringtocheck.indexOf(stringtofind + delimiter) == 0) || // begins with
		(stringtocheck.lastIndexOf(delimiter + stringtofind) != -1
			&& stringtocheck.lastIndexOf(delimiter + stringtofind) == stringtocheck.length - stringtofind.length - delimiter.length); // ends with
}
function removeClass(elementtocheck, stringtoremove) {
	stringtoremove = stringtoremove.toLowerCase();
	var elementclass = elementtocheck.className.toLowerCase();
	
	var i;
	if (elementclass == stringtoremove) {
		elementtocheck.className = ""; // equals
	} else {
		i = elementclass.indexOf(" " + stringtoremove + " "); // contains
		if (i != -1) {
			elementtocheck.className = elementclass.slice(0, i) + elementclass.slice(i + 1 + stringtoremove.length, elementclass.length);
		} else {
			i = elementclass.indexOf(stringtoremove + " "); // begins with
			if (i == 0) {
				elementtocheck.className = elementclass.slice(stringtoremove.length + 1, elementclass.length);
			} else {
				i = elementclass.lastIndexOf(" " + stringtoremove);
				if (i != -1 && i == elementclass.length - stringtoremove.length - 1) { // ends with
					elementtocheck.className = elementclass.slice(0, i);
				}
			}
		}
	}
}
