function addTag(txtarea, codeIndex) {
	
	var codes = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[zitat]','[/zitat]');
	
	if(codeIndex != 0 && codeIndex != 2 && codeIndex != 4 && codeIndex != 6) {
	   return;
	}
	var startTag = codes[codeIndex];
	var endTag = codes[codeIndex + 1];
	txtarea.focus();
	
	/* Internet Explorer */
  	if(typeof document.selection != 'undefined') {
		 theSelection = document.selection.createRange().text;
		 if(theSelection) {
			document.selection.createRange().text = startTag + theSelection + endTag;
		  	theSelection = '';
		 }
		 else
			  document.selection.createRange().text = getTagToInsert(txtarea, startTag, endTag);  
	}
  	else if(typeof txtarea.selectionStart != 'undefined') { //auf Gecko basierende Browser
		if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	   	  mozWrap(txtarea, codes[codeIndex], codes[codeIndex+1]);
		else {
		    var tagToInsert = getTagToInsert(txtarea, startTag, endTag);
			  var currentCursorIndex = txtarea.selectionStart;
		    txtarea.value = (txtarea.value).substring(0,currentCursorIndex) + tagToInsert + (txtarea.value).substring(currentCursorIndex, txtarea.textLength);
		    //Anpassen Cursorposition
		    txtarea.selectionStart = currentCursorIndex + tagToInsert.length;
		    txtarea.selectionEnd = currentCursorIndex + tagToInsert.length;
	  }
	}
	txtarea.focus();
	return;
}

function getTagToInsert(txtarea, startTag, endTag) {
	var textFieldText = txtarea.value;
	return (textFieldText.split(startTag).length == textFieldText.split(endTag).length) ? startTag : endTag;
}

function mozWrap(txtarea, open, close) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		  selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	var partValue = s1 + open + s2 + close;
	txtarea.value = partValue + s3;
	
	//Anpassen Cursorposition
	txtarea.selectionStart = partValue.length;
	txtarea.selectionEnd = partValue.length;
	
	return;
}

function showHelpText(textField, codeIndex) {
	helpText = "";
	if(codeIndex == 0) {
	   helpText = "Text in fett: [b]Mein Text[/b]";
	}
	else if(codeIndex == 2) {
	   helpText = "Text in kursiv: [i]Mein Text[/i]";
	}
	else if(codeIndex == 4) {
	   helpText = "Unterstrichener Text: [u]Mein Text[/u] ";
	}
	else if(codeIndex == 6) {
	   helpText = "Zitat: [zitat]Mein Text[/zitat] ";
	}
	textField.value = helpText;
}
