// Browsercheck copyright ©2000-2002 DHTMLCentral.com, Bratta Communications. All rights reserved.
// Modified 2003, 2004 Tijd Beursmedia.
function lib_bwcheck() {
	this.ver = navigator.appVersion;
	this.agent = navigator.userAgent;
	this.dom = document.getElementById ? true : false;
	
	var major = parseInt(navigator.appVersion);
	
	this.opera = navigator.userAgent.indexOf("Opera") > -1;
	this.opera2 = navigator.userAgent.indexOf("Opera 2") > -1;
	this.opera3 = navigator.userAgent.indexOf("Opera 3") > -1;
	this.opera4 = navigator.userAgent.indexOf("Opera 4") > -1;
	this.opera5 = navigator.userAgent.indexOf("Opera 5") > -1;
	this.opera6 = navigator.userAgent.indexOf("Opera 6") > -1;
	
	this.ie = this.ver.indexOf("MSIE") >- 1 && !this.opera;
	this.ie3 = this.ie && major < 4 && !this.opera;
	this.ie4 = this.ver.indexOf("MSIE 4") >- 1 && !this.opera;
	this.ie5 = this.ver.indexOf("MSIE 5") >- 1 && !this.opera;
	this.ie55 = this.ver.indexOf("MSIE 5.5") >- 1 && !this.opera;
	this.ie6 = this.ver.indexOf("MSIE 6") >- 1 && !this.opera;
	
	this.mac = this.agent.indexOf("Mac") >- 1;
	
	this.gecko = navigator.product == 'Gecko';
	
	this.ns4 = document.layers && !this.dom;
	this.ns6 = this.gecko && this.ver.indexOf('Netscape6') > -1;
	this.ns7 = this.gecko && this.ver.indexOf('Netscape/7') > -1;
	this.ns = this.ns4 || this.ns6 || this.ns7;
	
	this.ie5up = this.ie && !(this.ie3 || this.ie4);
	this.ie55up = this.ie5up && !this.ie5;
	this.ie6up = this.ie55up && !this.ie55;
	
	this.opera5up = this.opera && !(this.opera2 || this.opera3 || this.opera4);
	this.opera6up = this.opera5up && !this.opera5;
	
	this.ns6up = this.gecko;
	this.ns7up = this.gecko && !this.ns6;
	
	this.bw = (this.ie || this.opera || this.ns || this.gecko)
	
	return this;
}
var bw=new lib_bwcheck() //Browsercheck object

if (!document.getElementById) {
	if (bw.ie4) {
		document.getElementById = function(elementId) {
			return document.all[elementId];
		}
	} else if (bw.ns4) {
		document.getElementById = function(elementId) {
			return lib__getNN4Element(elementId, document);
		}
	}
}
function lib__getNN4Element(elementId, doc) {
	if (!doc)
		doc = document;
	
	var ret, i;
	
	for (i = 0; i < document.forms.length; i++) {
		var eltarray = document.forms[i].elements;
		for (var j = 0; j < eltarray.length; j++) {
			ret = eltarray[j];
			if (ret.id == elementId)
				return ret;
		}
	}
	
	for (i = 0; i < document.layers.length; i++) {
		ret = document.layers[i];
		if (ret.id == elementId)
			return ret;
		else {
			ret = lib__getNN4Element(elementId, ret);
			if (ret)
				return ret;
		}
	}
	return null;
}

// find a form element
function lib_getElementByName(name) {
	if (name == null)
		return null;
	else {
		var namedElement = document.forms[0].elements[name];
		if (namedElement)
			return namedElement;
		else
			return document.getElementById(name);
	}
}

function lib_getParentForm(elt) {
	if (typeof(elt) == "string")
		elt = document.getElementById(elt);
	
	if (elt) {
		if (elt.parentElement) {
			while (elt && elt.tagName != "FORM")
				elt.parentElement;
		} else if (elt.parentNode) {
			while (elt && elt.tagName != "FORM")
				elt.parentNode;
		} else
			return null;
			
		return elt;
	}
	
	return null;
}

// set a field value
function lib_doSetValue(name, value) {
	var item = lib_getElementByName(name);
	if (item)
		item.value = value;
}

// simulate an anchor click
function lib_navigateTo(url) {
	if (url.substring(0, 11) == 'javascript:')
		eval(url.substr(11));
	else
		document.location.href = url;
}

// focus element (or element specified by id)
function lib_setFocus(elt) {
	if (typeof(elt) == 'string')
		elt = document.getElementById(elt);
	if (elt && elt.focus) 
		elt.focus();
}

// recalculate dynamic expressions (IE)
function lib_recalculatePage() {
	if (document.recalc) 
		document.recalc();
}

// click element (or element specified by id)
function lib_clickElement(id) {
	var elt = id;
	if (typeof(elt) == "string")
		elt = document.getElementById(elt);
		
	if (elt) {
		if (elt.click) 
			elt.click();
		else if (elt.onclick)
			elt.onclick();
		else if (elt.tagName == 'A')
			lib_navigateTo(elt.href);
	}
}

// set default button behavior
function lib_setDefaultButton(sourceid, targetid) {
	var source = sourceid, target = targetid;
	if (typeof(source) == "string")
		source = document.getElementById(source);
	if (typeof(target) == "string")
		target = document.getElementById(target);
	
	if (source && target) {
		lib_addEventListner(source, "keydown", function(e) { 
			var keyCode; 
			if (e) 
				keyCode = e.keyCode; 
			else 
				keyCode = window.event.keyCode; 
			
			if (keyCode == 13) { 
				if (window.event) 
					window.event.cancelBubble = true; 
				target.focus(); 
				lib_clickElement(target); 
				return false;
			}
		} );
	}
	else {
		alert("Invalid default button specified.");
	}
}

/*********************************************************************

 Helper methods, DOM only
 
*********************************************************************/
// set class of element (or element specified by id)
function lib_setClass(elt, cls) {
	if (typeof(elt) == 'string')
		elt = document.getElementById(elt);
	
	if (elt) {
		var className = elt.className;
		
		if ((' ' + className + ' ').indexOf(' ' + cls + ' ') == -1)
			elt.className = className + ' ' + cls;
	}
}

// remove class from element (or element specified by id)
function lib_removeClass(elt, cls) {
	if (cls.length == 0)
		return;
	
	if (typeof(elt) == 'string')
		elt = document.getElementById(elt);
	
	if (elt) {
		var className = elt.className;
		
		if (className.length >= cls.length) {
			var spacedClassName = (' ' + className + ' ').replace(' ' + cls + ' ', ' ');
				
			if (spacedClassName.length < 3)
				elt.className = '';
			else
				elt.className = spacedClassName.substr(1, spacedClassName.length - 2);
		}
	}
}

// toggles a class on an element (or element specified by id)
function lib_toggleClass(elt, cls) {
	if (typeof(elt) == 'string')
		elt = document.getElementById(elt);
	
	if (elt) {
		var className = elt.className;
		
		if (className.length == 0 || className.length < cls.length)
			elt.className = className + ' ' + cls;
		else {
			var spacedClass = ' ' + cls + ' ';
			var spacedClassName = ' ' + className + ' '
		
			if (spacedClassName.indexOf(spacedClass) == -1)
				elt.className = className + ' ' + cls;
			else {
				spacedClassName = spacedClassName.replace(spacedClass, ' ');
				
				if(spacedClassName.length < 3)
					elt.className = '';
				else
					elt.className = spacedClassName.substr(1, spacedClassName.length - 2);
			}
		}
	}
}

// checks to see if element has class
function lib_hasClass(elt, cls) {
	if (typeof(elt) == 'string')
		elt = document.getElementById(elt);
	
	if (elt) {
		var className = elt.className;
		
		if (className.length < cls.length)
			return false;
		else
			return (' ' + className + ' ').indexOf(' ' + cls + ' ') != -1;
	}
	else
		return false;
}

function lib_getLocation(element, rooted) {
	var rect = { left : 0, top : 0, width : 0, height : 0, bottom : 0, right : 0 };
	
	if (typeof(element) == "string")
		element = document.getElementById(element);
	
	if (element) {
	
		rect.width = element.offsetWidth;
		rect.height = element.offsetHeight;
		
		do {
			rect.top += element.offsetTop;
			rect.left += element.offsetLeft;
			
			if (element != document.body) {
				element.scrollTop -= element.scrollTop;
				element.scrollLeft -= element.scrollLeft;
			}
			
			// check adjusts for TABLEs
			if (bw.ie) {
				if (!bw.mac && (element.tagName == "TD" || element.tagName == "TH")) {
					rect.left += element.clientLeft; // append cell border width
					rect.top += element.clientTop;
				}
			} else if (bw.gecko) {
				// borders on TABLEs alter offsets in gecko
				if (element.tagName == "TABLE") {
					var border = parseInt(element.border, 10);
					if (isNaN(border)) { // but frames as well
						var frame = element.getAttribute('frame');
						if (frame) {
							rect.left += 1;
							rect.top += 1;
						}
					} else if (border > 0) {
						rect.left += border;
						rect.top += border;
					}
				}
			} 

			element = element.offsetParent;
			
			if (element != null
			&& !rooted 
			&& (element.style.position == 'relative' 
				|| element.style.position == 'absolute' 
				|| element.style.position == 'fixed'))
				break;
		} while(element && element != document.body);
		
		rect.bottom = rect.top + rect.height;
		rect.right = rect.left + rect.width;
	}
	return rect;
}

function lib_addEventListner(target, eventName, handlerName) { 
	var handler = handlerName;
	
	if (typeof(handlerName) == "string") {
		if (target.getAttribute)
			handlerName = function(e) { target.getAttribute(handlerName)(e); };
		else
			handlerName = target[handlerName];
	}
	
	if ( target.addEventListener ) { 
		target.addEventListener(eventName, handler, false);
	} else if ( target.attachEvent ) { 
		target.attachEvent("on" + eventName, handler);
	} else { 
		var originalHandler = target["on" + eventName]; 
		if ( originalHandler ) { 
			target["on" + eventName] = function(e) { originalHandler(e); handler(e); }; 
		} else { 
			target["on" + eventName] = handler; 
		} 
	}
}
/*********************************************************************

 Mozilla IE 5 emulation (other way around is not possible :-)
 
*********************************************************************/
if(bw.gecko) {
	
	// IE parentElement
	HTMLElement.prototype.__defineGetter__(
		"parentElement", 
		function () 
		{
			if (this.parentNode == this.ownerDocument) 
				return null;
			return this.parentNode;
		});

	// offsets
	Event.prototype.__defineGetter__("offsetX", function () 
	{
		return this.layerX;
	});
	Event.prototype.__defineGetter__("offsetY", function () 
	{
		return this.layerY;
	});
	
	// IE Events
	HTMLElement.prototype.attachEvent = function (sType, fHandler) 
	{
		var shortTypeName = sType.replace(/^on/, "");
		fHandler._ieEmuEventHandler = function (e) 
		{
			window.event = e;
			return fHandler();
		};
		this.addEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
	};
	HTMLElement.prototype.detachEvent = function (sType, fHandler) 
	{
		var shortTypeName = sType.replace(/on/, "");
		if (typeof fHandler._ieEmuEventHandler == "function")
			this.removeEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
		else   // we can always try :-)
			this.removeEventListener(shortTypeName, fHandler, true);
	};
	Event.prototype.__defineSetter__(
		"cancelBubble", 
		function (b) 
		{
			if (b) this.stopPropagation();
		});

	function lib_emulateEventHandlers(eventNames) 
	{
		for (var i = 0; i < eventNames.length; i++) 
		{
			document.addEventListener(
				eventNames[i], 
				function (e) 
				{
					window.event = e;
				},
				true); // using capture
		}
	}
	// activate IE event emulation
	lib_emulateEventHandlers(['click', 'keydown', 'keyup', 'keypress', 'mouseover', 'mouseout', 'blur', 'focus', 'change']);
}

function lib_debug_clear()
{
	var layer = document.getElementById("lib_debug_panel");
	if (layer != null)
		layer.innerHTML = "";
}
function lib_debug(str, cat)
{
	var layer = document.getElementById("lib_debug_panel");
	if (layer == null)
	{
		var holder = document.createElement("DIV");
		document.body.appendChild(holder);
		if (bw.ie)
			holder.style.cssText = "padding: 3px; border: solid black 1px; background-color: #eeeeee; color: black; font-family: Tahoma, sans-serif; font-size: 8pt; font-weight: normal; position: absolute; bottom: 0px; right: 0px; width: 400px;";
		else
			holder.setAttribute("style", "padding: 3px; border: solid black 1px; background-color: #eeeeee; color: black; font-family: Tahoma, sans-serif; font-size: 8pt; font-weight: normal; position: absolute; bottom: 0px; right: 0px; width: 400px;");

		layer = document.createElement("DIV");
		holder.appendChild(layer);
		layer.id = "lib_debug_panel";
		if (layer.ie)
			layer.style.cssText = "margin: 3px; padding-left: 3px; padding-right: 3px; padding-top: 1px; padding-bottom: 1px; border: black 1px inset; background-color: white; color: black;"
		else
			layer.setAttribute("style", "margin: 3px; padding-left: 3px; padding-right: 3px; padding-top: 1px; padding-bottom: 1px; border: black 1px inset; background-color: white; color: black;");

		var resetLayer = document.createElement("DIV");
		holder.appendChild(resetLayer);
		if (resetLayer.ie)
			resetLayer.style.textAlign = "right";
		else
			resetLayer.setAttribute("style", "text-align: right");
		resetLayer.innerHTML = "<button onclick=\"lib_debug_clear(); return false\" style=\"font-family: Tahoma, sans-serif; font-size: 8pt;\">Clear</button>";
	}
	
	if (cat)
		layer.innerHTML += "<strong>" + cat + "</strong>";
	layer.innerHTML += str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\n", "<br>") + "<br/>";
}