function storeCaret (textEl) {
	if (textEl.createTextRange)
		textEl.caretPos = document.selection.createRange().duplicate();
}

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  = textEl.value + text;
	}

	return true;
}

function DoSmilie(action) {
 var currentMessage = document.form.comment.value;
 insertAtCaret(document.form.comment, " " + action);
 document.form.comment.focus();
 return;
}

function DoPrompt(action) {
 var currentMessage = document.form.comment.value;

 if(action=="url") {
  var thisURL = prompt("Gebe die komplette URL an!", "http://");
  if(thisURL==null){return;}
  var thisTitle = prompt("Jetzt kannst du einen Namen für den Link eingeben. (muss aber nicht!)", "Link Name");
  if(thisTitle==null){
   insertAtCaret(document.form.comment, " [url]" + thisURL + "[/url]" );
   document.form.comment.focus();
   return;
  }
  insertAtCaret(document.form.comment, " [url=" + thisURL + "]" + thisTitle + "[/url]" );
  document.form.comment.focus();
  return;
 }

 if(action=="email") {
  var thisMAIL = prompt("Gebe deine E-Mail Adresse an!", "");
  if (thisMAIL == null){return;}
  var thisTitle = prompt("Jetzt kannst du einen Namen für den Link eingeben. (muss aber nicht!)", "Link Name");
  if (thisTitle == null){
   insertAtCaret(document.form.comment, " [mail]" + thisMAIL + "[/mail]" );
   document.form.comment.focus();
   return;
  }
  insertAtCaret(document.form.comment, " [mail=" + thisMAIL + "]" + thisTitle + "[/mail]" );
  document.form.comment.focus();
  return;
 }

 if(action=="kommentar") {
  var thisComment = prompt("Bitte gebe ein Kommentar ein...", "");
  if(thisComment==null){return;}
  insertAtCaret(document.form.comment, "\n[kommentar]\n" + thisComment);
  document.form.comment.focus();
  return;
 }

 if(action=="bold") {
  var thisBold = prompt("Gebe den Text ein, den du FETT haben möchtest!", "");
  if(thisBold==null){return;}
  insertAtCaret(document.form.comment, " [F]" + thisBold + "[/F]");
  document.form.comment.focus();
  return;
 }

 if(action=="italics") {
  var thisItal = prompt("Gebe den Text an, den du KURSIV haben möchtest!", "");
  if (thisItal==null){return;}
  insertAtCaret(document.form.comment, " [K]" + thisItal + "[/K]");
  document.form.comment.focus();
  return;
 }

 if(action=="underline") {
  var thisUnderl = prompt("Gebe den Text an, den du UNTERSTRICHEN haben möchtest!", "");
  if(thisUnderl==null){return;}
  insertAtCaret(document.form.comment, " [U]" + thisUnderl + "[/U]");
  document.form.comment.focus();
  return;
 }

 if(action=="streichen") {
  var thisUnderl = prompt("Gebe den Text an, den du DURCHGESTRICHEN haben möchtest!", "");
  if(thisUnderl==null){return;}
  insertAtCaret(document.form.comment, " [D]" + thisUnderl + "[/D]");
  document.form.comment.focus();
  return;
 }

 if(action=="image") {
  var thisImage = prompt("Gebe die URL zu dem Bild an!", "http://");
  if (thisImage==null){return;}
  insertAtCaret(document.form.comment, " [IMG]" + thisImage + "[/IMG]");
  document.form.comment.focus();
  return;
 }

 if(action=="size") {
  var thisSize = document.form.size.value;
  var thisSize2 = prompt("Gebe einen Text ein...", "");
  if(thisSize2==null){return;}
  document.form.size.options(0).selected = true;
  insertAtCaret(document.form.comment, " [SIZE=" + thisSize + "]" +thisSize2 + "[/SIZE]");
  document.form.comment.focus();
  return;
 }

 if(action=="font") {
  var thisFont = document.form.font.value;
  var thisFont2 = prompt("Gebe einen Text ein...", "");
  if(thisFont2==null){return;}
  document.form.font.options(0).selected = true;
  insertAtCaret(document.form.comment, " [FONT=" + thisFont + "]" +thisFont2 + "[/FONT]");
  document.form.comment.focus();
  return;
 }

 if(action=="color") {
  var thisColor = document.form.color.value;
  var thisColor2 = prompt("Gebe einen Text ein...", "");
  if(thisColor2==null){return;}
  document.form.color.options(0).selected = true;
  insertAtCaret(document.form.comment, " [COLOR=" + thisColor + "]" +thisColor2 + "[/COLOR]");
  document.form.comment.focus();
  return;
 }


 if(action=="align") {
  var thisAlign = document.form.align.value;
  var thisAlign2 = prompt("Gebe einen Text ein...", "");
  if(thisAlign2==null){return;}
  document.form.align.options(0).selected = true;
  if(thisAlign=="marquee") {
  insertAtCaret(document.form.comment, " [MARQUEE]" + thisAlign2 + "[/MARQUEE]");
  } else {
  insertAtCaret(document.form.comment, " [ALIGN=" + thisAlign + "]" +thisAlign2 + "[/ALIGN]");
  }
  document.form.comment.focus();
  return;
 }

 if(action=="point") {
  var thisPoint = prompt("Gebe einen Text ein...", "");
  if(thisPoint==null){return;}
  insertAtCaret(document.form.comment, "[*]" + thisPoint);
  document.form.comment.focus();
  return;
 }

 if(action=="quote") {
  var thisQuote = prompt("Gebe einen Text ein...", "");
  if(thisQuote==null){return;}
  insertAtCaret(document.form.comment, "[ZITAT]" + thisQuote + "[/ZITAT]");
  document.form.comment.focus();
  return;
 }
}
