var version = '';
var agent   = '';
if (navigator.userAgent.search('MSIE') >= 0) {
	if (navigator.userAgent.search('MSIE 4') >= 0 || navigator.userAgent.search('MSIE 3') >= 0) {
		var agent = '';
	} else {
		var agent = 'ie';
		version = parseFloat(navigator.userAgent.substr(navigator.userAgent.indexOf('MSIE ')+5));
	}
} else if (navigator.userAgent.search('Konqueror') >= 0 || navigator.userAgent.search('Safari') >= 0) {
	var agent = 'konqueror';
} else if (navigator.userAgent.search('Gecko') >= 0) {
	var agent = 'gecko';
} else if (navigator.appName == 'Netscape' && navigator.userAgent.search('Mozilla/4') >= 0) {
	var agent = 'ns4';
} else {
	var agent = '';
}

if (navigator.userAgent.search('Mac') >= 0) {
	var os = 'Mac';
} else {
	var os = '';
}

function ng_dump(obj) {
	var result = '';
	for (var i in obj) if (arguments.length == 1 || i.search(arguments[1]) >= 0) result += '.' + i + ' = ' + obj[i] +'<br />\n';
	return result;
}

var have_acroread = false;
function hasPlugin(mimeType) {

	if (navigator.plugins != null && navigator.plugins.length > 0) {

		for (var i = 0; i < navigator.plugins.length; i++) {
			for (j = 0; j < navigator.plugins[i].length; j++) {
				if (navigator.plugins[i][j].type == mimeType) return true;
			}
		}

	} else if (navigator.userAgent.indexOf("MSIE") != -1 && parseInt(navigator.appVersion) >= 6 && navigator.userAgent.indexOf("Win")!=-1 && navigator.userAgent.indexOf("16bit")==-1) {

		if (mimeType == 'application/pdf') return have_acroread;
		return false;

		var obj = new Array();
		switch (mimeType) {
			case 'application/pdf':
				obj[obj.length] = 'PDF.PdfCtrl.5';
				obj[obj.length] = 'PDF.PdfCtrl.1';
				break;
		}
		var isavail =  false;
		if (obj.length > 0) {
			document.write('<scr' + 'ipt language="VBScript"\> \n');
			document.write('on error resume next \n');
			for (var i = 0; i < obj.length; i++) {
				document.write('if IsObject(CreateObject("'+obj[i]+'")) then isavail = true \n');
			}
			document.write('</scr' + 'ipt\> \n');
		}
		return isavail;

	}
	return false;
}

function Point(x,y) {
	this.x=x; this.y=y; return this;
}

function getPagePos(node) {
	if (agent == 'ns4') return new Point(node.pageX, node.pageY);
	var x = 0;
	var y = 0;
	while (node && node.tagName != 'BODY') {
		x += node.offsetLeft;
		y += node.offsetTop;
		if (node.tagName == 'TD' && os == 'Mac' && agent == 'ie') y += node.parentNode.offsetTop;
		node = node.offsetParent;
	}
	// Bug is fixed on MacOS X IE5.21 (maybe earlier?)

	if (!node && os == 'Mac' && agent == 'ie' && version < 5.21) {
		x += (document.body.leftMargin*1);
		y += (document.body.topMargin*1);
	}
	return new Point(x, y);
}

function formRestrictions(form) {

	this.form = form;
	this.elements = new Object();
	this.currentElement = null;
	this.errorClass = 'restrictionwarning';
	this.add = function _add(formElementName, regex, errorMessage) {
		if (this.form[formElementName]) {
			this.elements[formElementName] = new restriction(this.form[formElementName], regex, errorMessage);
			this.elements[formElementName].parent = this;
		} else {
			alert('RESTRICTIONS\nThere is no such form element as \"'+formElementName+'\"');
		}
	}
	this.test = function _test() {
		for (var i in this.elements) {
			this.elements[i].test(1);
		}
	}
	this.report = function _report() {
		var str = '';
		for (var i in this.elements) {
			if (!this.elements[i].isValid()) str += '  > '+this.elements[i].errorMessage+'\n';
		}
		if (str != '') {
			this.errorClass = 'restrictionerror';
			this.test();
			alert('The following errors occured:\n'+str);
			return false;
		}
		return true;
	}

	function restriction(formElement, regex, errorMessage) {
		this.parent = null;
		this.formElement = formElement;
		this.formElement.restriction = this;
		this.regex = regex;
		this.errorMessage = errorMessage;
		this.timeoutId = null;
		this.warningImg = document.createElement('img');
		this.warningImg.setAttribute('src', '/img/warning.gif');
		this.warningImg.setAttribute('align', 'absmiddle');
		this.warningImg.setAttribute('hspace', '3');
		this.warningImg.setAttribute('title', errorMessage);
		this.warningImg.setAttribute('alt', errorMessage);
		this.warningImg.visibility = 'hidden';

		if (this.formElement.nextSibling) {
			this.formElement.parentNode.insertBefore(this.warningImg, this.formElement.nextSibling);
		} else {
			this.formElement.parentNode.appendChild(this.warningImg);
		}

		this.isValid = function _isValid() {
			if (this.formElement.type == 'select-one') {
				var value = this.formElement.options[this.formElement.selectedIndex].value;
			} else {
				var value = this.formElement.value;
			}
			if (typeof this.regex == 'function') {
				return this.regex(value);
			} else {
				var re = new RegExp(this.regex, 'gi');
				return re.test(value);
			}
		}

		this.test = function _test(delay) {
			if (this.timeoutId > 0) clearTimeout(this.timeoutId);
			if (this.isValid()) {
				this.parent.changeStatus(this.formElement.name, true);
			} else if (this.formElement.type == 'select-one') {
				this.parent.changeStatus(this.formElement.name, false);
			} else {
				this.timeoutId = setTimeout('restrictions.changeStatus(\''+this.formElement.name+'\', false)', delay);
			}
		}

		function test() {
			this.restriction.test(1000);
		}
		if (this.formElement.type == 'select-one') {
			this.formElement.onchange = test;
		} else {
			this.formElement.onkeyup = test;
		}
	}

	this.changeStatus = function _changeStatus(elementName, status) {
		this.elements[elementName].formElement.className = (status ? '' : this.errorClass);
		this.elements[elementName].warningImg.style.visibility = (status ? 'hidden' : 'visible');
	}

}

var restrictions;

function disableUnchanged(form) {
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type != 'submit' && form.elements[i].type != 'image' && form.elements[i].type != 'hidden') {
			if (!hasChanged(form.elements[i])) form.elements[i].disabled = true;
		}
	}
}

function hasAnyChanged(form) {
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type != 'submit' && form.elements[i].type != 'image' && form.elements[i].type != 'hidden') {
			if (hasChanged(form.elements[i])) return true;
		}
	}
	return false;
}

function hasChanged(formElement) {
	if (typeof formElement.defaultValue != 'undefined') {
		if (formElement.defaultValue != formElement.value || formElement.defaultChecked != formElement.checked) return true;
	} else if (typeof formElement.options != 'undefined') {
		for (var j = 0; j < formElement.options.length; j++) {
			if (formElement.options[j].defaultSelected != formElement.options[j].selected) return true;
		}
	} else {
		for (var j = 0; j < formElement.length; j++) {
			if (hasChanged(formElement[j])) return true;
		}
	}
	return false;
}

function initLabels() {
	if (agent == 'gecko') return;
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		if (!labels[i].getAttribute('for'))	labels[i].onclick = selectLabel;
	}

	function selectLabel(e) {
		if ((typeof e == 'undefined' && window.event.srcElement.nodeName == 'INPUT') || (typeof e != 'undefined' && e.target.nodeName == 'INPUT')) return;
		var formElements = this.getElementsByTagName('input');
		for (var i = 0; i < formElements.length; i++) {
			if (formElements[i]) {
				if (typeof formElements[i].checked != 'undefined') formElements[i].checked = !formElements[i].checked;
				formElements[i].focus();
			}
		}
	}
}

function over(obj) {
	obj.style.backgroundColor='#eae8e9';
	obj.style.color='#fe3d3e'
}
function over2(obj) {
	obj.style.backgroundColor='#80ceff';
	obj.style.color='#093d6f'
}
function out(obj) {
	obj.style.backgroundColor='';
	obj.style.color='';
}

function unhashEmail(a) {
	if (a.href && a.href.indexOf('mailto:') != -1 && hashkey) {
		idkey = a.href.substr(7);
		key = hashkey;
		var xorkey = "";
		var idx = 0;
		while (xorkey.length < idkey.length) {
			if (idx > key.length) idx = 0;
			xorkey+= key.substr(idx, 1);
			idx++;
		}
		var hashed = idkey;
		var string = "";
		var count = 0;
		for (i=0; i < hashed.length; i+=2) {
			string+= String.fromCharCode(parseInt('0x'+hashed.substr(i,2)) ^ xorkey.charCodeAt((i/2)%xorkey.length));
		}
		a.href = 'mailto:'+string;
	}
}
function unhashEmailText(key) {
	var tags = document.getElementsByTagName('SPAN');
	for (i=0; i < tags.length; i++) {
		var s = tags[i];
		if (s.className == 'unhashme') {
			hashval = s.innerHTML;
			var xorkey = "";
			var idx = 0;
			while (xorkey.length < hashval.length) {
				if (idx > key.length) idx = 0;
				xorkey+= key.substr(idx, 1);
				idx++;
			}
			var hashed = hashval;
			var string = "";
			var count = 0;
			for (o=0; o < hashed.length; o+=2) {
				string+= String.fromCharCode(parseInt('0x'+hashed.substr(o,2)) ^ xorkey.charCodeAt((o/2)%xorkey.length));
			}

			s.innerHTML = string;
		}
	}
}