

function insert(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 = '>';
	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 insertURL(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");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var linkBeschreibung = input.value.substring(start, end);
	linkZiel = prompt("Input the destination for this Link","http://");
	if( linkBeschreibung == '' ){
		linkBeschreibung = prompt("Input the Title of this Link","");
	}
	if (linkBeschreibung == '') linkBeschreibung = linkZiel;
	if (linkZiel != ''& linkZiel != 'http://' & linkZiel != 'NULL') input.value = input.value.substr(0, start) + '<a href="' + linkZiel + '">'+ linkBeschreibung + '</a>' + input.value.substr(end);
}

function insertVideo(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");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var videotype = prompt("What site is this video coming from?\n\n(youtube, google, or myspace)","youtube");
	if( (videotype == 'youtube') || (videotype == 'google') || (videotype == 'myspace' ) ){
	var videoid = prompt("What is the id of the video?\n\ne.g: 8Gpy4Y2OdzY");
	if( ( ( videoid != '' ) && (videoid != 'NULL' ) ) ){
		input.value = input.value.substr(0,start) + '<video type="' + videotype + '">' + videoid + '</video>' + input.value.substr(end);
		}else{
			alert( 'You must enter a video id.' );
		}
	}else{
		alert( 'Video type must be youtube, google, or myspace.' );
	}
}


function insertMail(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 start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	linkZiel = 'mailto://' + prompt("Input the E-Mail Adress","");
	if (insText == '') linkBeschreibung = prompt("Input the title for this Mail Link","");
	input.focus();
	if (linkBeschreibung == '') linkBeschreibung = linkZiel;  
	if (linkZiel != '')	{
		if (linkZiel != '' & linkZiel != 'mailto://' & linkZiel != 'NULL'){ 
			if (insText != '') input.value = input.value.substr(0, start) + '<a href="' + linkZiel + '">'+ insText + '</a>' + input.value.substr(end);
		if (insText == '') input.value = input.value.substr(0, start) + '<a href="' + linkZiel + '">'+ linkBeschreibung + '</a>' + input.value.substr(end);
	}
	}
}


function insertImage(Smilies){
	document.getElementById("BBBarColorPicker").style.display = 'none';
	document.getElementById("BBBarFontPicker").style.display = 'none';
	if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
	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 (image != 'http://www.' & image != '' & image != 'NULL')   input.value = input.value.substr(0, start) + '<img src="'+ image + '" />' + input.value.substr(end);
}

function align(direction,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");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	input.value = input.value.substr(0, start) + '<div style="text-align: '+ direction + ';">'+insText+'</div>' + input.value.substr(end);
}

function list(type,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");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	if (type == 'u')
	{
		input.value = input.value.substr(0, start) + '<ul>\n<li>'+insText+'</li>\n</ul>' + input.value.substr(end);
	}
	if (type == 'o')
	{
		input.value = input.value.substr(0, start) + '<ol>\n<li>'+insText+'</li>\n</ol>' + input.value.substr(end);
	}

}

function quotetext(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");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	input.value = input.value.substr(0, start) + '<blockquote>'+insText+'</blockquote>' + input.value.substr(end);
}	

function showColor(Smilies){
	var wert;
	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';
	input = document.getElementById("CommentBox");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	input.value = input.value.substr(0, start) + '<font color="#'+ color + '">'+insText+'</font>' + input.value.substr(end);

}


function showFont(Smilies){
	var wert;
	if (document.getElementById("BBBarFontPicker").style.display == 'inherit') wert = 'none';
	if (document.getElementById("BBBarFontPicker").style.display == 'none') wert = 'inherit';
	document.getElementById("BBBarColorPicker").style.display = 'none';
	if (Smilies == 'FALSE') document.getElementById("BBBarSmilies").style.display = 'none';
	document.getElementById("BBBarFontPicker").style.display = wert;
}

function writeFont(font){
	document.getElementById("BBBarFontPicker").style.display = 'none';
	input = document.getElementById("CommentBox");
	input.focus();
	var start = input.selectionStart;
	var end = input.selectionEnd;
	var insText = input.value.substring(start, end);
	input.value = input.value.substr(0, start) + '<font face="'+ font + '">'+insText+'</font>' + input.value.substr(end);
}

function insertselFont(){
	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);
	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';
	document.getElementById("BBBar").style.display = 'inherit';
}

function BBformatselectchangedwv()
{
	document.getElementById("BBBarColorPicker").style.display = 'none';
	document.getElementById("BBBarFontPicker").style.display = 'none';
	document.getElementById("BBBar").style.display = 'inherit';
}

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;
}

