function insertTags(tagOpen, tagClose, sampleText, txtarea) {
	txtarea = document.getElementsByName(txtarea)[0];
	// IE
	if (document.selection) {
		var theSelection = document.selection.createRange().text;
		if (!theSelection) {
			theSelection=sampleText;
		}
		txtarea.focus();
		if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		} else {
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}

	// Mozilla
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
		var replaced = false;
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		if (endPos-startPos) {
			replaced = true;
		}
		var scrollTop = txtarea.scrollTop;
		var myText = (txtarea.value).substring(startPos, endPos);
		if (!myText) {
			myText=sampleText;
		}
		var subst;
		if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
		} else {
			subst = tagOpen + myText + tagClose;
		}
		txtarea.value = txtarea.value.substring(0, startPos) + subst +
			txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();
		//set new selection
		if (replaced) {
			var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
			txtarea.selectionStart = cPos;
			txtarea.selectionEnd = cPos;
		} else {
			txtarea.selectionStart = startPos+tagOpen.length;
			txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
		}
		txtarea.scrollTop = scrollTop;

	// All other browsers get no toolbar.
	// There was previously support for a crippled "help"
	// bar, but that caused more problems than it solved.
	}
	// reposition cursor if possible
	if (txtarea.createTextRange) {
		txtarea.caretPos = document.selection.createRange().duplicate();
	}
} 
function warning() {
	if (warn = document.getElementById('warning')) {
		var msg = warn.firstChild.nodeValue;
		warn.firstChild.nodeValue = "";
		alert(msg);
	}
}
function confirmclick(text,link) {
	Check = confirm(text);
	if(Check == true) self.location.href=link;
}
function writeto (ID,text) {
	if (document.getElementById(ID) == null) return;
	if (document.getElementById(ID).firstChild == null) {
		var ausgabe = document.createTextNode(text);
		document.getElementById(ID).appendChild(ausgabe);
	}
	else {
		document.getElementById(ID).firstChild.nodeValue = text;
		ausgabe = document.getElementById(ID).firstChild;
	}
}	
function focus_onload() {
	if (window != top) return;
	list = document.getElementsByTagName('input');
	for (i=0; i<list.length; i++) {
		if ((list[i].type == "text" || list[i].type == "password") && !list[i].disabled && !list[i].readOnly) {
			list[i].focus();
			break;
		}
	}
} 
var xmlHttp
function validateRequest(params) {
	xmlHttp = false;
	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
	    xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
	    // Internet Explorer 6 und älter
	    try {
	        xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e) {
	        try {
	            xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch(e) {
	            xmlHttp  = false;
	        }
	    }
	}
	if (xmlHttp) {
		document.body.style.cursor="wait"; 
		xmlHttp.open('GET', '.?validate=1&'+params, true);
		xmlHttp.onreadystatechange = validateReply;
		xmlHttp.send(null);
		
	}
}
function validateReply() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200 && xmlHttp.responseText != "") {
			
			list = document.getElementById('checkme').getElementsByTagName('input');
			var marked =0;
			for (i=0; i< list.length; i++) {
				if (list[i].getAttribute('type') != 'text') continue;
				if (list[i].getAttribute('name') == xmlHttp.responseText) {
					highlight(list[i],'error');
					marked++;
				}
			}
			if (marked == 0) alert(xmlHttp.responseText);
			else alert("Entry already exists or invalid");			
		}
		else if(xmlHttp.status==200) {
			var error=0;
			list = document.getElementById('checkme').getElementsByTagName('input');
			for (i=0; i< list.length; i++) {
				if (list[i].readOnly || list[i].disabled) continue;
				if (list[i].className.match(/int/) && list[i].value != "" &&!list[i].value.match(/^\d+$/)) {
					 highlight(list[i],'int_error'); error=1;
				}
				else if (list[i].className.match(/number/) && !list[i].value.match(/^\d+([,\.]\d+)?$/)) {
					highlight(list[i],'number_error'); error=1;
				}
			}
			if (!error) document.getElementById('checkme').submit();
			else alert("Invalid entry");
		}
		else alert("Problem retrieving validation data");
		document.body.style.cursor="default";
	}
}
function highlight (node,newclass) {
	var test = node.className;
	if (test == newclass || (test == 'text' && newclass == '') ) return;
	if (newclass == "error" && test == "number") newclass = "number_error";
	if (newclass == "error" && test == "int") newclass = "int_error";
	node.setAttribute("oldclass",test);
	node.onmouseover = function () { this.className=this.getAttribute('oldclass'); }
	node.className = newclass;
}
function toggleImage(node) {
	if (!node.complete) {
		window.setTimeout("toggleImage(document.getElementById('"+node.id+"'))",100);
		return;
	}
	var ar = node.width / node.height;
	var prevwidth = node.width;
	var prevheight = node.height;
	var match;
	
	if (node.width > 600) {
		node.width = 600
		node.height = 600 / ar;
		node.onclick = function () { toggleImage(this); }
		node.style.cursor = 'pointer';
		node.setAttribute("alt",prevwidth+"x"+prevheight);
		node.title = "Click to enlarge";
	}
	else if ((match = node.getAttribute("alt").match(/(\d+)x(\d+)/))) {
		node.width = match[1];
		node.height = match[2];
		node.setAttribute("alt","");
		node.onclick = function () { toggleImage(this); }
		node.style.cursor = 'pointer';
		node.title = "Click to shrink";
	}
}
function displaywarnings() {
	var warning = document.getElementById("warning");
	if (warning) {
		alert(warning.firstChild.nodeValue);
	}
}
