var wert = "BBCode";

function insertBBCode(aTag,eTag,Smilies) {
	document.getElementById("BBBarColorPicker").style.display = 'none';
	document.getElementById("BBBarFontPicker").style.display = 'none';
	
	if (Smilies == 'FALSE') {
		document.getElementById("BBBarSmilies").style.display = 'none';
	}
	
	var input = document.getElementById("CommentBox");
	var opentag;
	var closetag;
	
	/*if(document.getElementById("Radio_BBCode")){
		if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
	}
	if(document.getElementById("Radio_Html")){
		if (document.getElementById('Radio_Html').checked) wert = 'Html';
	}
	if(document.getElementById("Radio_Text")){
		if (document.getElementById('Radio_Text').checked) wert = 'Text';
	}*/
	
	if (wert != 'Text') {
		if (wert == 'Html') {
			opentag = '<';
			closetag = '>';
		}
		if (wert == 'BBCode') {
			opentag = '[';
			closetag = ']';
		}
		input.focus();
		/* für Internet Explorer */
		if(typeof document.selection != 'undefined') {
			/* Einfügen des Formatierungscodes */
			var range = document.selection.createRange();
			var insText = range.text;
			range.text = opentag + aTag + closetag + insText + opentag + eTag + closetag;
			/* Anpassen der Cursorposition */
			range = document.selection.createRange();
			if (insText.length == 0) {
				range.move('character', -eTag.length);
			}
			else {
				range.moveStart('character', aTag.length + insText.length + eTag.length);      
			}
			range.select();
		}
		/* für neuere auf Gecko basierende Browser */
		else if(typeof input.selectionStart != 'undefined'){
			/* Einfügen des Formatierungscodes */
			var start = input.selectionStart;
			var end = input.selectionEnd;
			var insText = input.value.substring(start, end);
			input.value = input.value.substr(0, start) + opentag + aTag + closetag +  insText + opentag +  eTag + closetag + input.value.substr(end) ;
			/* Anpassen der Cursorposition */
			var pos;
			if (insText.length == 0) {
				pos = start + aTag.length+opentag.length+closetag.length;
			} else {
				pos = start + aTag.length + insText.length + eTag.length;
			}
			input.selectionStart = pos;
			input.selectionEnd = pos;
		}
		/* für die übrigen Browser */
		else
		{
			/* Abfrage der Einfügeposition */
			var pos;
			var re = new RegExp('^[0-9]{0,3}$');
			while(!re.test(pos)) {
				pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
			}
			if(pos > input.value.length) {
				pos = input.value.length;
			}
			/* Einfügen des Formatierungscodes */
			var insText = prompt("Please insert the text, that should be formatted:");
			input.value = input.value.substr(0, pos) + openTag+  aTag + closeTag+ insText +openTag+  eTag + closeTag+ input.value.substr(pos);
		}
	}
}

function insertImage(Smilies){
document.getElementById("BBBarColorPicker").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';
if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert != 'Text')
{
	image = prompt("Input the link where the image ist hosted:","http://www.");
	var input = document.getElementById("CommentBox");
	input.focus();
    var start = input.selectionStart;
    var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (wert == 'BBCode' & image != 'http://www.' & image != '' & image != 'NULL')   input.value = input.value.substr(0, start) + '[img]'+ image + '[/img]' + input.value.substr(end);
	if (wert == 'Html' & image != 'http://www.' & image != '' & image != 'NULL')   input.value = input.value.substr(0, start) + '<img src="'+ image + '" />' + input.value.substr(end);
}}

function insertURL(Smilies){
document.getElementById("BBBarColorPicker").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';
if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert != 'Text')
{
	linkZiel = prompt("Input the destination for this Link","http://");
	linkBeschreibung = prompt("Input the Title of this Link","");
	var input = document.getElementById("CommentBox");
	input.focus();
    var start = input.selectionStart;
    var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (linkBeschreibung == '') linkBeschreibung = linkZiel;
	if (wert == 'BBCode' & linkZiel != ''& linkZiel != 'http://' & linkZiel != 'NULL') input.value = input.value.substr(0, start) + '[url=' + linkZiel + ']'+ linkBeschreibung + '[/url]' + input.value.substr(end);
	if (wert == 'Html' & linkZiel != ''& linkZiel != 'http://' & linkZiel != 'NULL') input.value = input.value.substr(0, start) + '<a href="' + linkZiel + '">'+ linkBeschreibung + '</a>' + input.value.substr(end);
}}

function align(direction,Smilies){
var wert = "BBCode";
document.getElementById("BBBarColorPicker").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';
if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert != 'Text')
{
	var input = document.getElementById("CommentBox");
	input.focus();
    var start = input.selectionStart;
    var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (wert == 'BBCode')   input.value = input.value.substr(0, start) + '[align='+direction+']'+ insText + '[/align]' + input.value.substr(end);
	if (wert == 'Html')   input.value = input.value.substr(0, start) + '<div style="text-align: '+ direction + ';">'+insText+'</div>' + input.value.substr(end);
}}

function quotetext(Smilies){
var wert = "BBCode";
document.getElementById("BBBarColorPicker").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';
if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert != 'Text')
{
	var input = document.getElementById("CommentBox");
	input.focus();
    var start = input.selectionStart;
    var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (wert == 'BBCode')   input.value = input.value.substr(0, start) + '[quote]'+ insText + '[/quote]' + input.value.substr(end);
	if (wert == 'Html')   input.value = input.value.substr(0, start) + '<blockquote>'+insText+'</blockquote>' + input.value.substr(end);
}}

function showColor(Smilies){
	var wert = "BBCode";
if (document.getElementById("BBBarColorPicker").style.display == 'inherit') wert = 'none';
if (document.getElementById("BBBarColorPicker").style.display == 'none') wert = 'inherit';
if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';
document.getElementById("BBBarColorPicker").style.display = wert;
}


function writeColor(color){
document.getElementById("BBBarColorPicker").style.display = 'none';
var wert = "BBCode";
if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert != 'Text')
{
	input = document.getElementById("CommentBox");
	input.focus();
    var start = input.selectionStart;
    var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (wert == 'BBCode')   input.value = input.value.substr(0, start) + '[color=#'+color+']'+ insText + '[/color]' + input.value.substr(end);
	if (wert == 'Html')   input.value = input.value.substr(0, start) + '<font color="#'+ color + '">'+insText+'</font>' + input.value.substr(end);
}

}

function insertselFont(){
if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert != 'Text')
{
	input = document.getElementById("CommentBox");
	var font = document.forms[0].fontpicker.value;
	document.forms[0].fontpicker.value = '';
	input.focus();
    var start = input.selectionStart;
    var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (wert == 'BBCode')   input.value = input.value.substr(0, start) + '[font="'+ font +'"]'+ insText + '[/font]' + input.value.substr(end);
	if (wert == 'Html')   input.value = input.value.substr(0, start) + '<font face="'+ font + '">'+insText+'</font>' + input.value.substr(end);
}}
function BBformatselectchanged()
{
document.getElementById("BBBarColorPicker").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';
document.getElementById("BBBarSmilies").style.display = 'none';
document.getElementById("Vanillacons").style.display = 'none';

if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}
if (wert == 'Html')document.getElementById("BBBar").style.display = 'inherit';
if (wert == 'BBCode') document.getElementById("BBBar").style.display = 'inherit';
if (wert == 'Text') document.getElementById("BBBar").style.display = 'none';
}
function BBformatselectchangedwv()
{
document.getElementById("BBBarColorPicker").style.display = 'none';
document.getElementById("BBBarFontPicker").style.display = 'none';

if(document.getElementById("Radio_BBCode")){
	if (document.getElementById('Radio_BBCode').checked) wert = 'BBCode';
}
if(document.getElementById("Radio_Html")){
	if (document.getElementById('Radio_Html').checked) wert = 'Html';
}
if(document.getElementById("Radio_Text")){
	if (document.getElementById('Radio_Text').checked) wert = 'Text';
}

if (wert == 'Html') document.getElementById("BBBar").style.display = 'inherit';
if (wert == 'BBCode') document.getElementById("BBBar").style.display = 'inherit';
if (wert == 'Text') document.getElementById("BBBar").style.display = 'none';
}

function insertBBSmilie(){
	if (document.getElementById("BBBarSmilies").style.display == 'inherit') wert = 'none';
	if (document.getElementById("BBBarSmilies").style.display == 'none') wert = 'inherit';
	document.getElementById("BBBarColorPicker").style.display = 'none';
	document.getElementById("BBBarFontPicker").style.display = 'none';
	document.getElementById("BBBarSmilies").style.display = wert;
}

