//-- BOF qtz_code
function qtz_code(qtz_tag) 
{
	var aTag, eTag;
	 
	switch (qtz_tag) {
		case 'B':
			aTag = '<strong>';
			eTag = '</strong>';
		break;	
		case 'I':
			aTag = '<em>';
			eTag = '</em>';
		break;	
		case 'U':
			aTag = '<span style="text-decoration: underline">';
			eTag = '</span>';
		break;	
		case 'S':
			aTag = '<strike>';
			eTag = '</strike>';
		break;
		case 'Url':
			qtz_url();
		return;
		case 'Quote':
			qtz_quote();
		return;
		default: // Smilies
			aTag = ' ' + qtz_tag + ' ';
			eTag = '';
	}	
	qtz_insert(aTag, eTag);
}

//- qtz_url
function qtz_url() {	
	var url = prompt('Link mit http://:', 'http://');
	if(url)
	{
		qtz_insert('<a href="' + url + '">', '</a>');
	}
}

//- qtz_quote
function qtz_quote() {	
	var author = prompt('Autor Name (Kann auch leer gelassen werden):', '');
	if(author.length > 0)	{
		var aTag = '<blockquote><strong>' + author + ': </strong>';
	}
	else {
		var aTag = '<blockquote>';
	}		
	qtz_insert(aTag, '</blockquote>');
}

//-- qtz_insert
//-  Original source: http://aktuell.de.selfhtml.org/artikel/javascript/bbcode/
//-  Castrated and edited by ZFeN - http://www.zfen.de/
function qtz_insert(aTag, eTag)
{
	var input = document.getElementById('comment');
	input.focus();    
    
	//- Gecko Area
	if(typeof input.selectionStart != 'undefined') {
		var start = input.selectionStart;
		var end = input.selectionEnd;		
		
		var scrollTop = input.scrollTop;
		
		var insText = qtz_rTrimString(input.value.substring(start, end));
		input.value = input.value.substr(0, start) + aTag + insText + eTag + qtz_whitespace + input.value.substr(end);
        
		var pos;
		if (insText.length == 0) {
			pos = start + aTag.length;
		}
		else {
			pos = start + aTag.length + insText.length + eTag.length;
		}
		input.selectionStart = pos;
		input.selectionEnd = pos;
		input.scrollTop = scrollTop;
	}    
	//- IE Area
	else if(typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		var insText = qtz_rTrimString(range.text);
		range.text = aTag + insText + eTag + qtz_whitespace;
		
		range = document.selection.createRange();
		if (insText.length == 0) {
			range.move('character', -eTag.length);
		}
		else {
			range.moveStart('character', aTag.length + insText.length + eTag.length + qtz_whitespace.length); 
		}
		range.select();
	}
}

//-- Last sign is a whitespace? yes = remove it
function qtz_rTrimString(myString) {
	qtz_whitespace = '';
	var lastSign = myString.substring(myString.length-1);
	if( lastSign == ' ') {
		qtz_whitespace = ' ';
		return myString.replace( /\s+$/g, "" );
	}
	else {
		return myString;	
	}
} //-- EOF qtz_insertCode


//-- BOF qtz_resizeTextarea
var actualSize = 200;
function qtz_resizeTextarea(size) {
	if((actualSize < 2000 && size > 0) || (actualSize >= 200 && size < 0)) {
		actualSize += size;
		document.getElementById("comment").style.height = actualSize + "px";
	}
} //-- EOF qtz_resizeTextarea
		
	
//-- BOF qtz_toggle
//- Original source: http://www.dustindiaz.com/top-ten-javascript/
function qtz_toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' )	{
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
} //-- EOF qtz_toggle


function $(id) {
	return document.getElementById(id);
}



window.onload = function() 
{
	//- Bold Button
	$('qtz_button_bold').onclick = function () {
		qtz_code('B');
	}
	//- Italic Button
	$('qtz_button_italic').onclick = function () {
		qtz_code('I');
	}
	//- Underlined Button
	$('qtz_button_underline').onclick = function () {
		qtz_code('U');
	}
	//- Strike Button
	$('qtz_button_strike').onclick = function () {
		qtz_code('S');
	}
	//- Link Button
	$('qtz_button_link').onclick = function () {
		qtz_code('Url');
	}
	//- Quote Button
	$('qtz_button_quote').onclick = function () {
		qtz_code('Quote');
	}
	//- Increase Button
	$('qtz_button_increase').onclick = function () {
		qtz_resizeTextarea(100);
	}
	//- Decrease Button
	$('qtz_button_decrease').onclick = function () {
		qtz_resizeTextarea(-100);
	}
	
	//- Zmilie Button
	$('qtz_button_zmilies').onclick = function ()	{
		if(this.title == 'Zmilies anzeigen')	{
			this.title = 'Zmilies verstecken';
		}
		else {
			this.title = 'Zmilies anzeigen';
		}
		qtz_toggle('qtz_zmilies');
	}
}