// Browsercheck copyright ©2000-2002 DHTMLCentral.com, Bratta Communications. All rights reserved.
// Modified 2003/11 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 getNN4Element(elementId, document);
		}
	}
}
function getNN4Element(elementId, doc) 
{
	if(!doc)
		doc = document;
	
	var ret;
	
	for(var 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(var i = 0; i < document.layers.length; i++)
	{
		ret = document.layers[i];
		if(ret.id == elementId)
			return ret;
		else
		{
			ret = getNN4Element(elementId, ret);
			if(ret)
				return ret;
		}
	}
	return null;
}
/*********************************************************************

 Helper methods
 
*********************************************************************/
function debug(message) 
{
	if(message)
		alert('debug: ' + message);
	else
		alert('debug');
}

// simulate a submit button click
function __doSubmit(action, actionHref) 
{
	__unblockSubmit();
	document.forms[0].__action.value = action;
	if(__canSubmit())
	{
		if(actionHref)
			document.forms[0].action = actionHref;
		document.forms[0].submit();
	}
	else
	{
		document.forms[0].__action.value = '';
		document.forms[0].__value.value = '';
	}
	
}

// simulate a submit button click
function __doSubmitValue(action, value, actionHref) 
{
	document.forms[0].__value.value = value;
	__doSubmit(action, actionHref);
}

function __getSubmitAction()
{
	return document.forms[0].__action.value;
}

function __getSubmitValue()
{
	return document.forms[0].__value.value;
}
// find a form element
function __getElementByName(name)
{
	if(name == null)
		return null;
	else
		return document.forms[0].elements[name];
}

// resetting a form
var __allowReset = false;

function __doReset()
{
	__allowReset = true;
	document.forms[0].reset();
	__allowReset = false;
}

// check if we can submit
function __canSubmit()
{
	if(__blockedSubmit)
	{
		return false;
	}
	else 
	{
		__processValidators();
		
		if(__isValid)
			return true;
		
		alert(__getErrors());
		return false;
	}
}

// set a field value
function __doSetValue(name, value) 
{
	var item = document.forms[0].elements[name];
	if(item != null && typeof item != 'undefined')
		item.value = value;
}

// simulate an anchor click
function __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 __setFocus(elt) 
{
	if(typeof elt == 'string')
		elt = document.getElementById(elt);
	if(elt != null) 
		elt.focus();
}

// recalculate dynamic expressions (IE)
function __recalculatePage() 
{
	if(typeof document.recalc != 'undefined') document.recalc();
}

// click element (or element specified by id)
function __clickElement(id) 
{
	var elt = document.getElementById(id);
	if(elt != null) 
		elt.click();
}

// cancel event bubbling
function __cancelBubble()
{
	if(window.event)
		window.event.cancelBubble = true;
}

// Submitblocking
var __blockedSubmit = false;
function __blockSubmit()
{
	__blockedSubmit = true;
}
function __unblockSubmit()
{
	__blockedSubmit = false;
}

// validation
var __isValid = true, 
    __validators = [], 
    __validation_messages = [];

// registers a control to be validated
function __registerValidation(name, method)
{
	__validators[__validators.length] = new __validator_item(name, method);
	
}

// item used to hold the validator
function __validator_item(name, method)
{
	this.name     = name;
	this.method   = method;
	this.validate = __validator_item_validate;
}

function __validator_item_validate()
{
	this.method(this.name);
}

// process the validators
function __processValidators()
{
	__isValid = true;
	__validation_messages = [];
	if(__validators.length > 0)
	{
		for(var i = 0 ; i < __validators.length ; i++)
			__validators[i].validate();
	}
}

// registers an error message to the validation mechanism
function __registerErrorMessage(control, message)
{
	__validation_messages[__validation_messages.length] = message;
	__isValid = false;
}

// returns the validation errors as a nicely formatted string
function __getErrors()
{
	if(__validation_messages.length > 0)
	{
		var message = '';
		
		for(var i = 0; i < __validation_messages.length ; i++)
		{
			if(i > 0)
				message += '\n';
			message += '* ' + __validation_messages[i];
		}
		
		return message;
	}
	else
		return 'no errors.';
	
}

/*********************************************************************

 Helper methods, DOM only
 
*********************************************************************/
// set class of element (or element specified by id)
function __setClass(elt, cls) 
{
	if(typeof elt == 'string')
		elt = document.getElementById(elt);
	
	if(elt != null)
	{
		var className = elt.className;
		
		if((' ' + className + ' ').indexOf(' ' + cls + ' ') == -1)
			elt.className = className + ' ' + cls;
	}
}

// remove class from element (or element specified by id)
function __removeClass(elt, cls)
{
	if(cls.length == 0)
		return;
	
	if(typeof elt == 'string')
		elt = document.getElementById(elt);
	
	if(elt != null)
	{
		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 __toggleClass(elt, cls)
{
	if(typeof elt == 'string')
		elt = document.getElementById(elt);
	
	if(elt != null)
	{
		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 __hasClass(elt, cls)
{
	if(typeof elt == 'string')
		elt = document.getElementById(elt);
	
	if(elt != null)
	{
		var className = elt.className;
		
		if(className.length < cls.length)
			return false;
		else
			return (' ' + className + ' ').indexOf(' ' + cls + ' ') != -1;
	}
	else
		return false;
}

// Offsets
function __offsetLeft(elt)
{
	var x = elt.offsetLeft;
	if (elt.offsetParent != null)
		x += __offsetLeft(elt.offsetParent);

	return x;
}
function __offsetTop(elt)
{
	var y = elt.offsetTop;
	if (elt.offsetParent != null)
		y += __offsetTop(elt.offsetParent);

	return y;
}

/*********************************************************************

 Mozilla IE 5 emulation (other way around is not possible :-)
 
*********************************************************************/
if(bw.ns6) 
{
	
	// 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']);
}
