function email(addr) {
	var n = 0;
	var addr2 = '';
	for(var i = 0; i < addr.length;i++) { 
		n = addr.charCodeAt(i); 
		if (n >= 8364) {
			n = 128;
		}
		addr2 += String.fromCharCode(n - 2); 
	}
	document.location.href = 'mailto:' + addr2;
}

function toggle(id) {
	if (document.getElementById("t_" + id) == undefined) {
		return;
	}
	d = document.getElementById("t_" + id).style.display;
	if (d == "none") {
		d = "inline";
		c = "collapse";
	} else {
		d = "none";
		c = "expand";
	}
	document.getElementById("t_" + id).style.display = d;
	document.getElementById("tp_" + id).className = c;
}
function check_childs(id) {
	var c = document.getElementById("perm_" + id).checked;
	var i = 0;
	var this_childs = childs["c" + id];
	for(i=0;i<this_childs.length;i++) {
		document.getElementById("perm_" + this_childs[i]).checked = c;
	}
	for(i=0;i<this_childs.length;i++) {
		if (childs["c" + this_childs[i]] != undefined) {
			check_childs(this_childs[i]);
		}
	}
}

function popupUserSearch(id) {
	window.open("/tools/search_user.php?field=" + id, "searchPopup", "dependent=yes,height=400,width=400");
}
function selectNickname(select_id,text_id) {
	var i = document.getElementById(select_id).selectedIndex;
	var t = document.getElementById(select_id).options[i].text;
	var v = document.getElementById(select_id).options[i].value;
	if (v != "") {
		document.getElementById(text_id).value = t;
	} else {
		document.getElementById(select_id).selectedIndex = 0;
	}
}

// Message-Funktionen
// ==================
function smiley (msgbox, code) {
	setSelection(msgbox, ' ' + code + ' ');
}

function getSelection (msgbox) {
	var field = document.getElementById(msgbox);
	if (typeof(field.createTextRange) != "undefined" && field.caretPos) {
		var caretPos = field.caretPos;
		return caretPos.text;
	} else if (field.selectionStart || field.selectionStart == '0') {
		var start_selection = field.selectionStart;
		var end_selection = field.selectionEnd;
		return field.value.substring(start_selection, end_selection);
	} else {
		return '';
	}
}

function setSelection (msgbox, text)
{
	var field = document.getElementById(msgbox);
	if (typeof(field.createTextRange) != "undefined" && field.caretPos) {
		var caretPos = field.caretPos;
		caretPos.text = text;
		caretPos.select();
	} else if (field.selectionStart || field.selectionStart == '0') {
		var start_selection = field.selectionStart;
		var end_selection = field.selectionEnd;
		var start = field.value.substring(0, start_selection);
		var end = field.value.substring(end_selection, field.textLength);
		field.value = start + text + end;
		field.focus();
		field.selectionStart = end_selection + text.length;
		field.selectionEnd = start_selection + text.length;
	} else {
		field.value += text;
		field.focus();
	}
}

function bbcode (msgbox, tag) {
	setSelection (msgbox, '[' + tag + ']' + getSelection(msgbox) + '[/' + tag + ']');
}
function bbcode_url (msgbox) {
	var sel = getSelection(msgbox);
	if (sel.substr(0,7) == 'http://' || sel.substr(0,8) == 'https://' || sel.substr(0,6) == 'ftp://' ||  sel.substr(0,7) == 'ftps://') {
		setSelection(msgbox, '[url]' + sel + '[/url]');
	} else {
		url = prompt('Zieladresse:', 'http://');
		if ((url.substr(0,7) == 'http://' && url != 'http://') || url.substr(0,8) == 'https://' || url.substr(0,6) == 'ftp://' ||  url.substr(0,7) == 'ftps://') {
			setSelection(msgbox, '[url=' + url + ']' + sel + '[/url]');
		} else {
			alert ('Zieladresse fehlerhaft.');
		}
	}
}
function bbcode_email (msgbox) {
	var sel = getSelection(msgbox);
	if (sel.match(/^[a-zA-Z0-9_\.-]{2,}@[a-z0-9\.-]{3,}\.[a-z]{2,4}|museum$/)) {
		setSelection(msgbox, '[email]' + sel + '[/email]');
	} else {
		email = prompt('E-Mail-Adresse:');
		if (email.match(/^[a-zA-Z0-9_\.-]{2,}@[a-z0-9\.-]{3,}\.[a-z]{2,4}|museum$/)) {
			setSelection(msgbox, '[email=' + email + ']' + sel + '[/email]');
		} else {
			alert ('E-Mail-Adresse fehlerhaft.');
		}
	}
}
function bbcode_img (msgbox) {
	var sel = getSelection(msgbox);
	if (sel.substr(0,7) == 'http://' || sel.substr(0,8) == 'https://' || sel.substr(0,6) == 'ftp://' ||  sel.substr(0,7) == 'ftps://') {
		setSelection(msgbox, '[img]' + sel + '[/img]');
	} else {
		image = prompt('Bild:');
		if (image.substr(0,7) == 'http://' || image.substr(0,8) == 'https://' || image.substr(0,6) == 'ftp://' || image.substr(0,7) == 'ftps://') {
			setSelection(msgbox, '[img]' + image + '[/img]');
		} else {
			alert ('Zieladresse fehlerhaft.');
		}
	}
}
function bbcode_color (msgbox, selbox) {
	var sel = getSelection(msgbox);
	if (selbox.selectedIndex > 0) {
		setSelection (msgbox, '[color=' + selbox.options[selbox.selectedIndex].value + ']' + sel + '[/color]');
	}
}
function bbcode_size (msgbox, selbox) {
	var sel = getSelection(msgbox);
	if (selbox.selectedIndex > 0) {
		setSelection (msgbox, '[size=' + selbox.options[selbox.selectedIndex].value + ']' + sel + '[/size]');
	}
}
function bbcode_user (msgbox) {
	window.open("/tools/search_user.php?msgfield=" + msgbox, "searchPopup", "dependent=yes,height=400,width=400");
}
function bbcode_user2 (msgbox, id, nick) {
	var sel = getSelection(msgbox);
	if (sel != '') {
		setSelection (msgbox, '[user=' + id + ']' + sel + '[/user]');
	} else {
		setSelection (msgbox, '[user=' + id + ']' + nick + '[/user]');	
	}
}
function bbcode_preview (msgbox, config, title) {
	window.open("/tools/message_preview.php?msgfield=" + msgbox + "&config=" + config + "&title=" + title, "previewPopup", "dependent=yes,height=400,width=600,scrollbars=yes");
}


function resizeImg (bild) {
	if (bild.width > 600) {
		bild.height = (600 / bild.width) * bild.height;
		bild.width = 600;
	}
	if (bild.height > 600) {
		bild.width = (600 / bild.height) * bild.width;
		bild.height = 600;
	}
}

// Maus-Position
// =============

var mouseX = 0;
var mouseY = 0;
var display_userinfo = 0;
var display_option_view = 0;

function getmouseposition(evt) {
	if (evt) {
		mouseX = evt.pageX;
		mouseY = evt.pageY;
	} else if (window.event.clientX) {
		mouseX = window.document.body.scrollLeft+window.event.clientX;
		mouseY = window.document.body.scrollTop+window.event.clientY;
	}
	if (display_userinfo > 0) {
		ViewUserInfo(display_userinfo, display_option_view);
	}
}

document.onmousemove = getmouseposition;

function getPosX(id) {
	var ret=0;
	var obj = document.getElementById(id);
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			ret += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		ret += obj.x;
	}
	return ret;
}
function getPosY(id) {
	var ret=0;
	var obj = document.getElementById(id);
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			ret += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		ret += obj.y;
	}
	return ret;
}
function getHeight(id) {
	var ret=0;
	var obj = document.getElementById(id);
	if (obj.offsetHeight) {
		return obj.offsetHeight;
	}
}
function getWidth(id) {
	var ret=0;
	var obj = document.getElementById(id);
	if (obj.offsetWidth) {
		return obj.offsetWidth;
	}
}

