String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

addLoadListener(initPup);
function initPup() {
	// The "popup pedigree" links will only work
	// if JavaScript is supported and turned on, and
	// the buttons are inactive by default. This method
	// will be called if JavaScript is available,
	// and it will turn turn them back on, attaching
	// special handlers to open the pedigree near
	// where the mouse is clicked.
	var ePups = getElementsByClass(document, "pupbutton", "input");

	for (var i = 0; i < ePups.length; i++) {
		attachEventListener(ePups[i], "click", pup, false);
	}
	return true;
}

function pup(event) {
	// Get target event, so we can get it's id
	var target = getEventTarget(event);
	while (target.nodeName.toLowerCase() != "input") {
		target = target.parentNode;
	}
	var myID = target.id;

	// With our ID, we can determine data ID
	// Get the popup pedigree data
	var partsID = myID.replace(/pup/i, 'pupd');
	var sContent = getPupContent(partsID);

	var scrollingPosition = getScrollingPosition();
	var cursorPosition = [0, 0];

	if (typeof event.pageX != "undefined" && typeof event.x != "undefined") {
		cursorPosition[0] = event.pageX;
		cursorPosition[1] = event.pageY;
	}
	else if (event.clientX) {
		cursorPosition[0] = event.clientX + scrollingPosition[0];
		cursorPosition[1] = event.clientY + scrollingPosition[1];
	}
	else {
		cursorPosition[0] = scrollingPosition[0];
		cursorPosition[1] = scrollingPosition[1] + 100;
	}

	var ePup = document.getElementById('pup');
	ePup.innerHTML = sContent;

	ePup.style.left = cursorPosition[0] + 0 + "px";
	ePup.style.top = cursorPosition[1] + 20 + "px";
	ePup.style.display = 'block';
}

function getPupContent(partsID) {
	/* Initialize the arrays */
	var msg = "";
	var names = new Array(7);
	var stats = new Array(7);
	var links = new Array(7);
	var i, j;

	for (j=0; j<7; j++) {
		names[j] = "";
		stats[j] = "";
		links[j] = "";
	}

	// Get the data from the hidden DIV
	var ePups = document.getElementById(partsID);
	var sParts = ePups.innerHTML;
	
	/* Fix some potential encoding issues */
	sParts = sParts.replace(/\\n/gi, '<br>');
	sParts = sParts.replace(/&lt;/gi, '<');
	sParts = sParts.replace(/&gt;/gi, '>');

	/* Parse the sParts into the things we need */
	var args = sParts.split("|");

	for (j=0, i=0; i<args.length && j<7;i++, j++) {
		names[j] = args[i++];
		if (names[j] == "") names[j]="&nbsp;";

		stats[j] = args[i++];

		if (args[i] != "") {
			links[j] = '<a href="'+args[i]+'">'+names[j]+'</A>';
		} else {
			links[j] = names[j];
		}
	}

	/* Start the content */
	var s1='<table><tbody>\n';

	/* Build the table from the parts */

	/* Row 1: 3 cells (0, 1, 3) */
	s1 += '<tr><td width="30%" rowspan="4" class="pupsubject">'+
			links[0]+'<br>'+stats[0]+'</td>';
	s1 += '<td width="30%" rowspan="2" class="pupmale">';
	s1 += links[1]+'<br>'+stats[1]+'</td>';
	s1 += '<td width="40%" class="pupmale">';
	s1 += links[3]+'<br>'+stats[3]+'</td></tr>\n';

	/* Row 2: 1 cell (4) */
	s1 += '<tr><td width="40%" class="pupfemale">'+links[4]+'<br>'+
			stats[4]+'</td></tr>\n';

	/* Row 3: 2 cells (2, 5) */
	s1 += '<tr><td width="30%" rowspan="2" class="pupfemale">'+links[2]
			+'<br>'+stats[2]+'</td>';
	s1 += '<td width="40%" class="pupmale">';
	s1 += links[5]+'<br>'+stats[5]+'</td></tr>\n';

	/* Row 4: 1 cell (6) */
	s1 += '<tr><td width="40%" class="pupfemale">'+links[6]+'<br>'+
			stats[6]+'</td></tr>\n';

	s1 += '<tr><td colspan="3"><input type="button" class="pupbutton" value="Close"'+
			' onclick="elementDisplay(\'pup\',\'none\')"></td></tr>\n';

	/* End the content */
	s1 += '</tbody></table>\n';

	return s1;
}

function elementDisplay(element_id, display) {
	var e = document.getElementById(element_id);
	e.style.display = display;
}

addLoadListener(initPageToc);
function initPageToc() {
	// The page "table of contents" creates list of
	// h2 tags on the page if it finds an element with
	// the id "pagetoc". It only functions if
	// JavaScript is supported, and enabled, and if
	// the user has added HTML similar to the
	// following in one of the static content
	// sections:
	
	// <div id="pagetoc"><ul></ul></div>
	
	// 2007-04-06 JFC Now allows pagetoc on UL or OL, or on
	//                container DIV

	var eOuterElement = document.getElementById('pagetoc');
	var eTocElement = null;

	if (eOuterElement != null) {
		if (eOuterElement.tagName == "DIV") {
			eTocElement = getElementsByTagNames("UL,OL", eOuterElement)[0];
		}
		else {
			eTocElement = eOuterElement;
		}
		// Toggling display from none to block speeds
		// up execution, presumably because the browser
		// ignores the new content as it is being
		// added, and only computes locations, size, etc.
		// once at the end. That's just a guess based
		// on observing Firefox. If it actually isn't
		// faster, it seems like it, and that's good
		// enough for me in this case.
		if (eTocElement != null) {
			eOuterElement.style.display = 'none';
			makePageToc(eTocElement);
			eOuterElement.style.display = 'block';
		}
	}
	return true;
}

function makePageToc(eTocElement) {
	var eContent = document.getElementById('content');
	var eH2;

	for (var iHeader=0; (eH2 = eContent.getElementsByTagName("h2")[iHeader]); iHeader++) {
		var eListElement = document.createElement('li');
		eListElement.className = 'toc'+eH2.tagName.toLowerCase();

		/* Get text of H2 and add to TOC as link */
		var sItemText = getElementText(eH2);
		var nText = document.createTextNode(sItemText);
		var eLinkElement = document.createElement('a');
		eLinkElement.appendChild(nText);
		eListElement.appendChild(eLinkElement);

		/* Set ID of H2 node if it doesn't have one */
		if (!eH2.id) {
			eH2.id = 't'+iHeader;
		}

		/* Set target of the A tag we are addingto ID of H2 */
		eLinkElement.href = '#' + eH2.id;
		eTocElement.appendChild(eListElement);
	}
	return true;
}

function getElementText(element) {
	/* Concatenate text from all textNode children of given element.
		Not recursive; direct children only. */

	var sResult = "";

	for (var iChild=0; (eChild = element.childNodes[iChild]); iChild++) {
		if (eChild.nodeType == 3) {
			sResult += eChild.nodeValue;
		}
	}
	return sResult;
}

addLoadListener(initQuote);
function initQuote() {
	// Initializes the "random quote" facility
	
	// <ul class="randomquote"><li> ... </li> etc. </ul>
	//      or
	// <div class="randomquote"><ul><li> ... </li> etc. </ul></div>

	var eQuoteList = getElementsByClass(document, 'randomquote', 'ul');
	for (var iQuote=0; iQuote<eQuoteList.length; iQuote++) {
		var eUL = eQuoteList[iQuote];
		pickQuote(eUL);
		eUL.style.display = 'block';
	}

	eDivList = getElementsByClass(document, 'randomquote', 'div');
	for (var iDiv=0; iDiv<eDivList.length; iDiv++) {
		eQuoteList = eDivList[iDiv].getElementsByTagName('ul');
		for (var iQuote=0; iQuote<eQuoteList.length; iQuote++) {
			var eUL = eQuoteList[iQuote];
			pickQuote(eUL);
			eUL.style.display = 'block';
		}
		eDivList[iDiv].style.display = 'block';
	}
	return true;
}

function pickQuote(eUL) {
	// Hide all but one child (LI) element
	var eListItems = eUL.getElementsByTagName('li');
	var iIndex = Math.round(Math.random()*(eListItems.length-1));

	// Hide all but one LI element
	for (var iListItem=0; iListItem<eListItems.length; iListItem++) {
		var e = eListItems[iListItem];
		if (iListItem!=iIndex) {
			e.style.display = 'none';
		}
		else {
			e.style.display = 'block';
		}
	}
}

function hemlink(part1, part2) {
	var loc = '';

	loc = 'm'+"A"+"i";loc=loc+"l"+"to"+":";
	loc = loc.toLowerCase() + part1+"@"+part2;
	loc = loc+'?SUBJECT='+document.title;
	location.href=loc;
}

function hemlinknc(part1, part2) {
	var loc = '';

	loc = 'm'+"A"+"i";loc=loc+"l"+"to"+":";
	loc = loc.toLowerCase() + part1+"@"+part2;
	location.href=loc;
}
