// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret (textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function insertAtCaret(textEl, text) {
	if (textEl.createTextRange && textEl.caretPos) {
		var caretPos = textEl.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else {
		textEl.value = text;
	}
}

function DoSmilie(smilie) {
	var currentMessage = document.forms(0).comment.value;
	insertAtCaret(document.forms(0).comment, smilie);
	document.forms(0).comment.focus();
	return;
}

function DoPrompt(kbcode) {
	var currentMessage = document.forms(0).comment.value;

	var text_eingabe = "Bitte geben Sie einen Text ein:";
	var url_eingabe = "Geben Sie die volle Adresse des Links ein:";
	var email_eingabe = "Bitte geben Sie eine E-Mail Adresse ein:";
	var linkname_eingabe = "Gebe einen Linknamen ein (optional):";
	var kommentar_eingabe = "Bitte geben Sie ein Kommentar ein:";
	var bild_eingabe = "Bitte geben Sie die URL des Bildes ein:";

	if(kbcode == "url") {
		var thisURL = prompt(url_eingabe, "http://");
		if(thisURL==null){return;}
		var thisTitle = prompt(linkname_eingabe, "Link Name");
		if(thisTitle==null){
			insertAtCaret(document.forms(0).comment, "[url]" + thisURL + "[/url]" );
			document.forms(0).comment.focus();
			return;
		}
		insertAtCaret(document.forms(0).comment, "[url=" + thisURL + "]" + thisTitle + "[/url]" );
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "email") {
		var thisMAIL = prompt(email_eingabe, "");
		if (thisMAIL == null){return;}
		var thisTitle = prompt(linkname_eingabe, "Link Name");
		if (thisTitle == null){
			insertAtCaret(document.forms(0).comment, "[mail]" + thisMAIL + "[/mail]" );
			document.forms(0).comment.focus();
			return;
		}
		insertAtCaret(document.forms(0).comment, "[mail=" + thisMAIL + "]" + thisTitle + "[/mail]" );
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "kommentar") {
		var thisComment = prompt(kommentar_eingabe, "");
		if(thisComment==null){return;}
		insertAtCaret(document.forms(0).comment, "\n[kommentar]\n" + thisComment);
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "bold") {
		var thisBold = prompt(text_eingabe, "");
		if(thisBold==null){return;}
		insertAtCaret(document.forms(0).comment, "[F]" + thisBold + "[/F]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "italics") {
		var thisItal = prompt(text_eingabe, "");
		if(thisItal==null){return;}
		insertAtCaret(document.forms(0).comment, "[K]" + thisItal + "[/K]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "underline") {
		var thisUnderl = prompt(text_eingabe, "");
		if(thisUnderl==null){return;}
		insertAtCaret(document.forms(0).comment, "[U]" + thisUnderl + "[/U]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "streichen") {
		var thisUnderl = prompt(text_eingabe, "");
		if(thisUnderl==null){return;}
		insertAtCaret(document.forms(0).comment, "[D]" + thisUnderl + "[/D]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "image") {
		var thisImage = prompt(bild_eingabe, "http://");
		if (thisImage==null){return;}
		insertAtCaret(document.forms(0).comment, "[IMG]" + thisImage + "[/IMG]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "size") {
		var thisSize = document.forms(0).size.value;
		var thisSize2 = prompt(text_eingabe, "");
		if(thisSize2==null){return;}
		document.forms(0).size.options(0).selected = true;
		insertAtCaret(document.forms(0).comment, "[SIZE=" + thisSize + "]" +thisSize2 + "[/SIZE]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "font") {
		var thisFont = document.forms(0).font.value;
		var thisFont2 = prompt(text_eingabe, "");
		if(thisFont2==null){return;}
		document.forms(0).font.options(0).selected = true;
		insertAtCaret(document.forms(0).comment, "[FONT=" + thisFont + "]" +thisFont2 + "[/FONT]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "color") {
		var thisColor = document.forms(0).color.value;
		var thisColor2 = prompt(text_eingabe, "");
		if(thisColor2==null){return;}
		document.forms(0).color.options(0).selected = true;
		insertAtCaret(document.forms(0).comment, "[COLOR=" + thisColor + "]" +thisColor2 + "[/COLOR]");
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "align") {
		var thisAlign = document.forms(0).align.value;
		var thisAlign2 = prompt(text_eingabe, "");
		if(thisAlign2==null){return;}
		document.forms(0).align.options(0).selected = true;
		if(thisAlign=="marquee") {
			insertAtCaret(document.forms(0).comment, "[MARQUEE]" + thisAlign2 + "[/MARQUEE]");
		} else {
			insertAtCaret(document.forms(0).comment, "[ALIGN=" + thisAlign + "]" +thisAlign2 + "[/ALIGN]");
		}
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "point") {
		var thisPoint = prompt(text_eingabe, "");
		if(thisPoint==null){return;}
		insertAtCaret(document.forms(0).comment, "[*]" + thisPoint);
		document.forms(0).comment.focus();
		return;
	}
	if(kbcode == "quote") {
		var thisQuote = prompt(text_eingabe, "");
		if(thisQuote==null){return;}
		insertAtCaret(document.forms(0).comment, "[ZITAT]" + thisQuote + "[/ZITAT]");
		document.forms(0).comment.focus();
		return;
	}
}
