//
// Javascript routines used to extend the standard Browser Objects.
//
// (c) 2003
//
// All material in this document is copyright Mark Bertenshaw.  None of this can be reproduced in any manner without the express permission of the author.
//

// Constants.
navigator.BT_UNKNOWN = 0;
navigator.BT_NETSCAPE = 1;
navigator.BT_INTERNET_EXPLORER = 2;
navigator.BT_OPERA = 3;

// DOM version stuff.
var DOM1 = false;
var DOM2 = false;
var DOM2_Events = false;

// Returns the index in the href of the last separator, whatever that is (/ or \).
function getIndexOfLastSeparator(loc) {
	var pathname = loc.pathname;
	var idx;
	if (loc.protocol == "file:") {
		idx = pathname.lastIndexOf("\\");
		if (idx > 0) return idx;
	}
	return pathname.lastIndexOf("/");
}

//
// BEGIN Extensions to Location object.
//

var l = document.location;

// Just the filename part of the URL.
l.filename = l.pathname.slice(getIndexOfLastSeparator(l) + 1);

// Everything other than the final part of the URL.
l.prefix = l.protocol + "/" + l.pathname.slice(0, getIndexOfLastSeparator(l) + 1);

//
// END Extensions to Location object.
//


//
// BEGIN Extensions to the Navigator object.
//

var n = window.navigator;
var appVersion = n.appVersion;
var idx;

switch(n.appName) {
case "Netscape":
	n.browserType = n.BT_NETSCAPE;
	idx = appVersion.indexOf(" ");
	n.browserVersion = appVersion.slice(0, idx);
	if (n.browserVersion >= 5)
	{
		n.browserVersion = n.vendorSub;
	}
	break;
case "Microsoft Internet Explorer":
	n.browserType = n.BT_INTERNET_EXPLORER;
	idx = appVersion.indexOf("MSIE");
	n.browserVersion = appVersion.slice(idx + 5, appVersion.indexOf(";", idx));
	break;
case "Opera":
	n.browserType = BT_OPERA;
	break;
default:
	browserType = BT_UNKNOWN;
	n.browserVersion = n.appVersion;
}

//
// END Extensions to the Navigator object.
//


if (document.getElementById) {
	DOM1 = true;
	if (document.implementation) {
		if (document.implementation.hasFeature("Events", "2.0")) {
			DOM2_Events = true;
		}
	}
} else {
	// Set up compatibility with non DOM compliant browsers.
	if (navigator.browserType == navigator.BT_INTERNET_EXPLORER && navigator.browserVersion >= 4) {
		//
		// getElementById method for IE 4 - 5.01
		//
		document.getElementById = function (id)
		{
			return document.all[id];
		}
	} else {
		//
		// getElementById method for IE 3 and N 3 - 4
		//
		document.getElementById = function (id) 
		{
			var forms = document.forms;
			for (var fIdx = 0; fIdx < forms.length; fIdx++) {
				var elems = forms[fIdx].elements;
				for (var eIdx = 0; eIdx < elems.length; eIdx++) {
					var elem = elems[eIdx];
					if (elem.name == id) return elem;
				}
			}
			return undefined;
		}
	}
}

// DOM/IE compatibility:  Adds <eventName> to <elemId>, calling <eventProc>.
function addEventById(elemId, eventName, eventProc, cascade)
{
	addEventByElem(document.getElementById(elemId), eventName, eventProc, cascade);
}

// DOM/IE compatibility:  Adds <eventName> to <elem>, calling <eventProc>.
function addEventByElem(elem, eventName, eventProc, cascade)
{
	if (eventName == "click")  {
		elem.onclick = eventProc;
	} else if (eventName == "load") {
		elem.onload = eventProc;
	} else {
		if (DOM2_Events) {
			// DOM2 standard
			elem.addEventListener(eventName, eventProc, false);
		} else {
			// IE specific
			elem.attachEvent(eventName, eventProc);
		}
	}	
}

// DOM/IE Compatibility function. Returns the element from which the event came.
function getEventOrg(e)
{
	if (e == undefined) {
		return window.event.srcElement;

	} else {
		return e.target;
	}
}

// Replacement for IE innerText property.
function getCodedInnerText(e)
{
	var innerHTML = e.innerHTML;
	var innerText = "";
	
	var posCloseChevron = -1;
	var posOpenChevron = innerHTML.indexOf("<");
	
	while (posOpenChevron != -1) {
		innerText += innerHTML.slice(posCloseChevron + 1, posOpenChevron) + " ";
		posCloseChevron = innerHTML.indexOf(">", posOpenChevron + 1);
		posOpenChevron = innerHTML.indexOf("<", posCloseChevron + 1);
	}
	
	innerText += innerHTML.slice(posCloseChevron + 1);
	
	return innerText;
}

function getInnerText(elem)
{
	var nodes = elem.childNodes;
	var innerText = "";
	for (var i=0; i<nodes.length; i++) {
		var n = nodes[i];
		switch (n.nodeType)
		{
		case 1:
			{
				innerText += getInnerText(n);
				break;
			}
		case 3:
			{
				innerText += n.nodeValue + " ";
				break;
			}
		}
	}
	
	return innerText;
}