function setfocus(id) {
	var e = document.getElementById(id);
	if(e && e.focus) e.focus();
}

function savesize(formname) {
	var W = 0; H = 0;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			W = window.innerWidth;
			H = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			W = document.body.offsetWidth;
			H = document.body.offsetHeight;
		}
	}
	//alert(String(W) + '/' + String(H));
	setCookie('bbcode_formheight_' + formname, String(H));
	setCookie('bbcode_formwidth_' + formname, String(W));
}

// ----- \\ Crispys Code from http://www.webreference.com/js/column8/functions.html // -----
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
// ----- \\ End of Crispys Code // -----

/* ------------------------------------ [ LIST ] ------------------------------------ */
// ----- \\ Babvailiicas Code from http://www.babailiica.com/js/reorderizator.html // -----
function additem(obj, text, value, index, id, classname, color, bg, selected) { /* NEW added from version 1.1 */
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" || text == "")
		return;
	obj.length++
	if (typeof index == "number" && index < obj.length-1) {
		var i = Number();
		for (i=obj.length-2; i>index-1; i--) {
			obj[i+1].text = obj[i].text;
			obj[i+1].value = obj[i].value;
			obj[i+1].id = obj[i].id;
			obj[i+1].className = obj[i].className;
			obj[i+1].style.color = obj[i].style.color;
			obj[i+1].style.backgroundColor = obj[i].style.backgroundColor;
		}
	} else {
		index = obj.length - 1;
	}
	obj = obj[index];
	obj.text = text;
	obj.value = value;
	obj.id = id ? id : '';
	obj.className = classname ? classname : '';
	obj.style.color = color ? color : '';
	obj.style.backgroundColor = bg ? bg : '';
	obj.selected = selected;
	return obj;
}

function removeitem(obj, index) { /* NEW added from version 1.1 */
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" || obj.length == 0)
		return;
	if (index === true) {
		for (index=obj.length-1; index>=0; index--) {
			if (obj[index].selected) {
				obj[index] = null;
			}
		}
	} else {
		obj[((typeof index != "number") || index > (obj.length - 1) || index < 0 ? obj.length - 1 : index)] = null;
	}
}

function viceversa(obj) {
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var elements = new Array();
	for (var i=obj.length-1; i>-1; i--) {
		elements[elements.length] = new Array(obj[i].text, obj[i].value, obj[i].style.color, obj[i].style.backgroundColor, obj[i].className, obj[i].id, obj[i].selected);
	}
	for (i=0; i<obj.length; i++) {
		obj[i].text = elements[i][0];
		obj[i].value = elements[i][1];
		obj[i].style.color = elements[i][2];
		obj[i].style.backgroundColor = elements[i][3];
		obj[i].className = elements[i][4];
		obj[i].id = elements[i][5];
		obj[i].selected = elements[i][6];
	}
}

function top(obj) {
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var elements = new Array();
	for (var i=0; i<obj.length; i++) {
		if (obj[i].selected) {
			elements[elements.length] = new Array(obj[i].text, obj[i].value, obj[i].style.color, obj[i].style.backgroundColor, obj[i].className, obj[i].id, obj[i].selected);
		}
	}
	for (i=0; i<obj.length; i++) {
		if (!obj[i].selected) {
			elements[elements.length] = new Array(obj[i].text, obj[i].value, obj[i].style.color, obj[i].style.backgroundColor, obj[i].className, obj[i].id, obj[i].selected);
		}
	}
	for (i=0; i<obj.length; i++) {
		obj[i].text = elements[i][0];
		obj[i].value = elements[i][1];
		obj[i].style.color = elements[i][2];
		obj[i].style.backgroundColor = elements[i][3];
		obj[i].className = elements[i][4];
		obj[i].id = elements[i][5];
		obj[i].selected = elements[i][6];
	}
}

function bottom(obj) {
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var elements = new Array();
	for (var i=0; i<obj.length; i++) {
		if (!obj[i].selected) {
			elements[elements.length] = new Array(obj[i].text, obj[i].value, obj[i].style.color, obj[i].style.backgroundColor, obj[i].className, obj[i].id, obj[i].selected);
		}
	}
	for (i=0; i<obj.length; i++) {
		if (obj[i].selected) {
			elements[elements.length] = new Array(obj[i].text, obj[i].value, obj[i].style.color, obj[i].style.backgroundColor, obj[i].className, obj[i].id, obj[i].selected);
		}
	}
	for (i=obj.length-1; i>-1; i--) {
		obj[i].text = elements[i][0];
		obj[i].value = elements[i][1];
		obj[i].style.color = elements[i][2];
		obj[i].style.backgroundColor = elements[i][3];
		obj[i].className = elements[i][4];
		obj[i].id = elements[i][5];
		obj[i].selected = elements[i][6];
	}
}

function up(obj) {
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var sel = new Array();
	for (var i=0; i<obj.length; i++) {
		if (obj[i].selected == true) {
			sel[sel.length] = i;
		}
	}
	for (i in sel) {
		if (sel[i] != 0 && !obj[sel[i]-1].selected) {
			var tmp = new Array(obj[sel[i]-1].text, obj[sel[i]-1].value, obj[sel[i]-1].style.color, obj[sel[i]-1].style.backgroundColor, obj[sel[i]-1].className, obj[sel[i]-1].id);
			obj[sel[i]-1].text = obj[sel[i]].text;
			obj[sel[i]-1].value = obj[sel[i]].value;
			obj[sel[i]-1].style.color = obj[sel[i]].style.color;
			obj[sel[i]-1].style.backgroundColor = obj[sel[i]].style.backgroundColor;
			obj[sel[i]-1].className = obj[sel[i]].className;
			obj[sel[i]-1].id = obj[sel[i]].id;
			obj[sel[i]].text = tmp[0];
			obj[sel[i]].value = tmp[1];
			obj[sel[i]].style.color = tmp[2];
			obj[sel[i]].style.backgroundColor = tmp[3];
			obj[sel[i]].className = tmp[4];
			obj[sel[i]].id = tmp[5];
			obj[sel[i]-1].selected = true;
			obj[sel[i]].selected = false;
		}
	}
}

function down(obj) {
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var sel = new Array();
	for (var i=obj.length-1; i>-1; i--) {
		if (obj[i].selected == true) {
			sel[sel.length] = i;
		}
	}
	for (i in sel) {
		if (sel[i] != obj.length-1 && !obj[sel[i]+1].selected) {
			var tmp = new Array(obj[sel[i]+1].text, obj[sel[i]+1].value, obj[sel[i]+1].style.color, obj[sel[i]+1].style.backgroundColor, obj[sel[i]+1].className, obj[sel[i]+1].id);
			obj[sel[i]+1].text = obj[sel[i]].text;
			obj[sel[i]+1].value = obj[sel[i]].value;
			obj[sel[i]+1].style.color = obj[sel[i]].style.color;
			obj[sel[i]+1].style.backgroundColor = obj[sel[i]].style.backgroundColor;
			obj[sel[i]+1].className = obj[sel[i]].className;
			obj[sel[i]+1].id = obj[sel[i]].id;
			obj[sel[i]].text = tmp[0];
			obj[sel[i]].value = tmp[1];
			obj[sel[i]].style.color = tmp[2];
			obj[sel[i]].style.backgroundColor = tmp[3];
			obj[sel[i]].className = tmp[4];
			obj[sel[i]].id = tmp[5];
			obj[sel[i]+1].selected = true;
			obj[sel[i]].selected = false;
		}
	}
}
// ----- \\ End of Babvailiicas Code // -----

function list_build() {
	var list = document.getElementById('list');
	var numbered = document.getElementById('numbered');
	var text = document.getElementById('currentitem');
	var s = '';
	var p = '';
	
	if(list.length >= 1) {
		for(var i = 0; i <= list.length - 1; i++) {
			if(list.options[i].value == '-noentry-') continue;
			s += ' [*]' + list.options[i].text + '\n';
		}
	} else { s = ' [*]' + text.value; }
	
	if(s != '') {
		if(opener.selstyle != '') p = ' style="' + opener.selstyle + '"';
		opener.resetselstyle(); 
		if(numbered.checked) 
			p += ((p == '') ? '' : ' format') + '="numbered"';
		
		s = '[list' + p + ']\n' + s + '[/list]';
	}
	
	window.opener.inserttext(s, formname);
	window.close();
	return false;
}

function list_add() {
	var list = document.getElementById('list');
	var text = document.getElementById('currentitem');
	
	if(text.value != '') {
		if(list.options[0].value == '-noentry-') removeitem(list, 0);
		additem(list, text.value);
	}
	if(list.options[0].value != '-noentry-') {
		list.selectedIndex = list.length - 1;
		list_select();
	}
	if((typeof text.selectionStart) != 'undefined') {
		text.selectionStart = 0;
		text.selectionEnd = text.value.length;
	}
	text.focus();
}

function list_remove() {
	var list = document.getElementById('list');
	var text = document.getElementById('currentitem');
	
	if(list.selectedIndex >= 0) {
		removeitem(list, true);
		list.selectedIndex = list.length - 1;
		list_select();
	}
	if(list.length == 0) {
		additem(list, 'Noch kein Element', '-noentry-').disabled = true;
	} else {
		if((typeof text.selectionStart) != 'undefined') {
			text.selectionStart = 0;
			text.selectionEnd = text.value.length;
		}
	}
	text.focus();
}

function list_select() {
	var list = document.getElementById('list');
	var text = document.getElementById('currentitem');
	
	if(list.selectedIndex >= 0) {
		text.value = list.options[list.selectedIndex].text;
	}
	if((typeof text.selectionStart) != 'undefined') {
		text.selectionStart = 0;
		text.selectionEnd = text.value.length;
	}
	text.focus();
}

function list_update() {
	var list = document.getElementById('list');
	var text = document.getElementById('currentitem');
	
	if(list.selectedIndex >= 0) {
		list.options[list.selectedIndex].text = text.value;
	}
	if((typeof text.selectionStart) != 'undefined') {
		text.selectionStart = 0;
		text.selectionEnd = text.value.length;
	}
	text.focus();
}

function list_top() {
	var list = document.getElementById('list');
	top(list);
}

function list_up() {
	var list = document.getElementById('list');
	up(list);
}

function list_reverse() {
	var list = document.getElementById('list');
	viceversa(list);
}

function list_down() {
	var list = document.getElementById('list');
	down(list);
}

function list_bottom() {
	var list = document.getElementById('list');
	bottom(list);
}



/* ------------------------------------ [ LINK ] ------------------------------------ */
function link_use(id) {
	var dest = document.getElementById('dest');
	dest.value = id.toString();
	dest.focus();
}

function link_build() {
	var dest = document.getElementById('dest');
	var target = document.getElementById('target');
	var anchor = document.getElementById('anchor');
	var notext = document.getElementById('notext');
	var s = '';
	
	if(((dest) && dest.value != '') || ((anchor) && anchor.text != '')) {
		if((notext) && notext.checked && ((anchor) ? (anchor.text == '') : false)) {
			if(((target) && target.value != '') || ((anchor) && anchor.value !='' )) {
				if((target) && target.value != '')
					s += ' target="' + target.value + '"';
				if((anchor) && anchor.value != '')
					s += ' anchor="' + anchor.value + '"';
				if(opener.selstyle != '') 
					s += ' style="' + opener.selstyle + '"';
				opener.resetselstyle();
   			}
   			if((s != '') || (dest.value != 'http://' && dest.value != '')) {
				s = '[link' + s + ']' + ((dest.value == 'http://') ? '' : dest.value) + '[/link]';
				window.opener.inserttext(s, formname);
			}
		} else {
			if((target) && target.value != '')
				s += ' target="' + target.value + '"';
			if((anchor) && anchor.value != '')
				s += ' anchor="' +  anchor.value + '"';		
			if((dest) && dest.value != '' && dest.value != 'http://') 
				s += ((s == '') ? '' : ' dest') + '="' + dest.value + '"'
			if(opener.selstyle != '') 
				s += ' style="' + opener.selstyle + '"';
			opener.resetselstyle();
			
			s = '[link' + s + ']';
			window.opener.inserttags(s, '[/link]', formname);
		}
	}
	
	window.close();
	return false;
}

/* ------------------------------------ [ COLOR ] ----------------------------------- */
function color_use(color) {
	var colorvalue = document.getElementById('colorvalue');
	colorvalue.value = color;
	color_build();
}

function color_build() {
	var colorvalue = document.getElementById('colorvalue');
	if(colorvalue.value != '') {
		window.opener.inserttags('[' + type + '="' + colorvalue.value + '"]', '[/' + type + ']', formname);
	}
	
	window.close();
	return false;
}

/* ------------------------------------ [ MAIL ] ------------------------------------ */
function mail_build() {
	var email = document.getElementById('email');
	var name = document.getElementById('name');
	var subject = document.getElementById('subject');
	var notext = document.getElementById('notext');
	var s = '';
	
	if(email.value != '') {
		if(opener.selstyle != '') s = ' style="' + opener.selstyle + '"';
		opener.resetselstyle();

		if(name.value != '') {
			s += ' name="' + name.value + '"';
		}
		if(subject.value != '') {
			s += ' subject="' + subject.value + '"';
		}
		if((!notext) || !notext.checked) {
			s += ((s == '') ? '' : ' adress') + '="' + email.value + '"';
		}
		if((notext) && notext.checked) {
			window.opener.inserttext('[email' + s + ']' + email.value + '[/email]', formname);
		} else {
			window.opener.inserttags('[email' + s + ']', '[/email]', formname);
		}
	}
	
	window.close();
	return false;
}

/* ---------------------------------- [ FILELINK ] ------------------------------------ */
function filelink_use(id) {
	var notext = document.getElementById('notext');
	var s = '';
	
	if(opener.selstyle != '') s = ' style="' + opener.selstyle + '"';
	opener.resetselstyle();
	
	if((notext) && notext.checked) {
		window.opener.inserttext('[filelink' + s + ']' + id + '[/filelink]', formname);
	} else {
		s += ((s == '') ? '' : ' id') + '="' + id + '"';
		window.opener.inserttags('[filelink' + s + ']', '[/filelink]', formname);
	}
	
	window.close();
	return false;
}

/* ---------------------------------- [ CHARMAP ] ------------------------------------- */
function charmap_view(code, desc) {
	var charname = document.getElementById('charname');
	var charpreview = document.getElementById('charpreview');
	
	if(code != '') {
		charname.innerHTML = '<i>&amp;' + code + ';</i> - <b>' + desc + '</b>';
		if(charpreview.innerHTML) {
			charpreview.innerHTML = '&' + code + ';';
		}
	} else { charname.innerHTML = '&nbsp;'; charpreview.innerHTML = '&nbsp;'; }
}

function charmap_use(code) {
	window.opener.inserttext('&' + code + '; ', formname);
	
	window.close();
	return false;
}

/* ----------------------------------- [ ANCHOR ] ----------------------------------- */
function anchor_build() {
	var notext = document.getElementById('notext');
	var anchorid = document.getElementById('anchorid');
	var s = '';
	
	if(opener.selstyle != '') s = ' style="' + opener.selstyle + '"';
	opener.resetselstyle();
	
	if(anchorid.value != '') {
		if((notext) && notext.checked) {
			window.opener.inserttext('[anchor' + s + ']' + anchorid.value + '[/anchor]', formname);
		} else {
			s += ((s == '') ? '' : ' id') + '="' + anchorid.value + '"';
			window.opener.inserttags('[anchor' + s + ']', '[/anchor]', formname);
		}
	}
	
	window.close();
	return false;
}

/* ------------------------------------ [ ABBR ] ------------------------------------ */
function abbr_build() {
	var title = document.getElementById('title');
	var lang = document.getElementById('lang');
	var s = '';
	
	if((lang) && (lang.value != '')) {
		s = ' lang="' + lang.value + '"';
	}
	if(opener.selstyle != '') s += ' style="' + opener.selstyle + '"';
	opener.resetselstyle();
	if((title) && (title.value != '')) {
		s = ((s == '') ? '' : ' title') + '="' + title.value + '"' + s;
	}
	window.opener.inserttags('[' + type + s + ']', '[/' + type + ']', formname);
	window.close();
	return false;
}

/* ------------------------------------- [ IMG ] ------------------------------------ */
function img_use(id, titlestr, preview, w, h, img, t) {
	var src = document.getElementById('src');
	var title = document.getElementById('title');
	var prev = document.getElementById('preview');
	var prevlink = document.getElementById('preview_link');
	
	if(src) src.value = id.toString();
	if(title) title.value = titlestr;
	if(prev) {
		prev.src = mediapath + preview;
		prev.title = t;
		prev.alt = t;
	}
	if(prevlink) {
		prevlink.href = mediapath + img;
		prevlink.title = t + ',' + w + ',' + h;
	}

	
	src.focus();
}

function img_build() {
	var src = document.getElementById('src');
	var title = document.getElementById('title');
	var height = document.getElementById('height');
	var width = document.getElementById('width');
	
	var s = '';
	
	if(opener.selstyle != '') s += ' style="' + opener.selstyle + '"';
	opener.resetselstyle();
	
	if(height.value != '') s = ' height="' + height.value + '"';
	if(width.value != '') s += ' width="' + width.value + '"';
	if(title.value != '') {
		if(s == '') s = '="' + title.value + '"' + s; else s = ' title="' + title.value + '"' + s;
	}
	
	window.opener.inserttext('[img' + s + ']' + src.value + '[/img]', formname);
	window.close();
	return false;
}

/* ------------------------------------ [ TABLE ] ----------------------------------- */
function table_build() {
	var rows = document.getElementById('rows');
	var cols = document.getElementById('cols');
	var caption = document.getElementById('caption');
	var irows = 0; var icols = 0;
	var irow = 0; var icol = 0;
	var s = '';
	
	if(rows.value != '') irows = parseInt(rows.value);
	if(cols.value != '') icols = parseInt(cols.value);
	
	
	if(opener.selstyle != '') s += ' style="' + opener.selstyle + '"';
	opener.resetselstyle();
	if((caption) && (caption.value != '')) s += ((s == '') ? '' : ' caption') + '="' + caption.value + '"';
	s = '[table' + s + "]\n";
	for(irow = 1; irow <= irows; irow++) {
	 s += " [row]\n";
	 for(icol = 1; icol <= icols; icol++) {
	  s += '  [cell]' + "Zelle " + irow.toString() + '/' + icol.toString() + "[/cell]\n";
	 }
	 s += " [/row]\n";
	}
	s += "[/table]\n";
	
	window.opener.inserttext(s, formname);
	window.close();
	return false;
}

/* ---------------------------------- [ PIC-POPUP ] --------------------------------- */
var pic_popup = null
function open_pic_popup(url, data)
{
	var tpop_bildschirmx = screen.availWidth;
	var tpop_bildschirmy = screen.availHeight;
	var opos = 0;
	var pos = data.indexOf(',');
	var windowtitle = (pos != -1 ? data.substring(0, pos) : '');
	opos = pos + 1;
	pos = data.indexOf(',', opos);
	var b = (pos != -1 ? data.substring(opos, pos) : '');
	opos = pos + 1;
	var h = (pos != -1 ? data.substring(opos) : '');
		
	var tpop_breite = b;
	var tpop_hoehe = h;

	var tpop_startx = Math.round(tpop_bildschirmx / 2) - (tpop_breite / 2);
	var tpop_starty = Math.round(tpop_bildschirmy / 2) - (tpop_hoehe / 2);

    if (pic_popup && !pic_popup.closed && pic_popup.close) pic_popup.close()
        
	pic_popup = window.open('about:blank', 'popup', "width="+ b + ", height=" + h
					+ ", status=no, toolbar=no, menubar=no, resizable=yes, scrollbars=no, screenX=" + tpop_startx
					+ ", left=" + tpop_startx + ", screenY=" + tpop_starty + ", top=" + tpop_starty + " ");
					
	pic_popup.focus();
	pic_popup.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	pic_popup.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" lang="de" xml:lang="de">');
	pic_popup.document.writeln('<head><title>' + windowtitle + '</title>');
	pic_popup.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
	pic_popup.document.writeln('<meta http-equiv="ImageToolbar" content="no" />');
	pic_popup.document.writeln('<meta http-equiv="Language" content="de" />');
	pic_popup.document.writeln('<style type="text/css"><!--')
	pic_popup.document.writeln('body,div{margin:0;padding:0;overflow:hidden;}img{border:0 none;margin:0;padding:0;display:block;}')
	pic_popup.document.writeln('--></style></head>')
	pic_popup.document.writeln('<body><div>')
	pic_popup.document.writeln('<a href="#" onclick="self.close()"><img src="'+ url +'" alt="'+ windowtitle + '" /></a>')
	pic_popup.document.writeln('</div></body></html>');
	pic_popup.document.close();

	return false;
}