// Copyright Marcus Haas
function addSmiley(id) {
	if (document.selection && document.selection.createRange().text != '')		document.selection.createRange().text = document.selection.createRange().text + "["+id+"]";
	else {
		document.getElementsByTagName('textarea')[0].value += "["+id+"]";
		document.getElementsByTagName('textarea')[0].focus();
	}
}
function header() 		{edit("[h]","[/h]","header");}
function list() 		{edit("[list]","[/list]","list");}
function color(color) 	{edit("[color="+color+"]","[/color]","color");}
function fett() 		{edit("[b]","[/b]","fett");}
function italic() 		{edit("[i]","[/i]","italic");}
function uline() 		{edit("[u]","[/u]","uline");}
function cite() 		{edit("[Zitat]","[/Zitat]","cite");}
function url() {
	url = prompt("Bitte geben Sie die gewünschte URL ein:", "http://");
	edit("[url="+url+"]","[/url]","url");
}
function edit(start,end,id) {
	var textfield = document.getElementsByTagName('textarea')[0];
	if (textfield.selectionStart || textfield.selectionStart == '0') {
		textfield.focus();
		var startPos = textfield.selectionStart;
		var endPos = textfield.selectionEnd;
		strSelection = start + textfield.value.substring(startPos, endPos)+ end;
		textfield.value = textfield.value.substring(0, startPos) + strSelection + textfield.value.substring(endPos, textfield.value.length);
	}
	else if (document.selection && document.selection.createRange().text != '') {
		document.selection.createRange().text = start + document.selection.createRange().text + end;
	}
	else {
		if (document.getElementById(id).style.color=="#aaa") {
			textfield.value += end;
			document.getElementById(id).style.color="blue";
		}
		else {
			textfield.value += start;
			document.getElementById(id).style.color="#aaa";
		}
	}
}
function ccolor(farbe)
{
	if (farbe!='') {
		if (document.selection && document.selection.createRange().text != '')
			document.selection.createRange().text = "["+farbe+"]" + document.selection.createRange().text + "[/"+farbe+"]";
		else 
			document.getElementsByTagName('textarea')[0].value += "["+farbe+"] [/"+farbe+"]";
	}
}
function vorschau() {
	var textarea = document.getElementsByTagName('textarea')[0].value.split('[');
	vorschautext = textarea[0];
	for (var i=0; i<textarea.length; i++) {
		bracket = textarea[i].split(']')[0];
		if (bracket == 'url') {
			vorschautext += '<a href="'+textarea[i].split('b]')[1].split('/url]')[0]+'">' + textarea[i].split('b]')[1].split('/url]')[0] + '</a>';
		}
		if (bracket == 'b') {
			vorschautext += '<span style="font-weight: bolder;">' + textarea[i].split('b]')[1].split('/b]')[0];
		}
		if (bracket == 'i') {
			vorschautext += '<span style="font-style: italic;">' + textarea[i].split('i]')[1].split('/i]')[0];
		}
		if (bracket == 'u') {
			vorschautext += '<span style="text-decoration: underline;">' + textarea[i].split('u]')[1].split('/u]')[0];
		}
		if (bracket == '/b' || bracket == '/i' || bracket == '/u') {
			vorschautext += '</span>' + textarea[i].split(']')[1];
		}
		else {
			if (document.getElementById(bracket) && bracket != '') {
				vorschautext +=  '<img src="'+document.getElementById(bracket).src+'">'+textarea[i].split(']')[1];
			}
		}
	}
//	document.getElementById("bbcode").appendChild(vorschau_Element);
}