function insertTag(t, p) {
  var el = document.getElementById("comment-body");
  var start = el.selectionStart;
  var end   = el.selectionEnd;
  var body = el.value;
  
  body = body.substring(0, end) + "</" + t + ">" + body.substring(end);
  body = body.substring(0, start) + "<" + t + ((p)? " " + p: "") + ">" + body.substring(start);
  el.value = body;
  
  var selection = start + 2 + t.length + ((p)? p.length + 1: 0) + ((start == end)? 0: end - start + 3 + t.length);
  el.selectionStart = selection;
  el.selectionEnd =  selection;
  el.focus();
}
function insertLink() {
  var url;
  var el = document.getElementById("comment-body");
  if (url = prompt("Enter URL.", "http://")) {
    insertTag("a", "href=\"" + url + "\"");
  }
}

var el = document.getElementById("cbody");
var tagbuttonstyle = "<style type=\"text/css\">.tagbutton {border-bottom:0 !important;}\n.tagbutton img {margin-right:10px;}\n.tagbutton:hover img {background-color: #e1d4c0;}</style>";
var tagbutton = "<a href=\"javascript:insertTag('b', '');\" class=\"tagbutton\"><img src=\"http://tools.kuribo.info/tagbutton/gl.bold.gif\" width=\"18\" height=\"18\" alt=\"b\" title=\"Insert B Tags.\"/></a><a href=\"javascript:insertTag('i', '');\" class=\"tagbutton\"><img src=\"http://tools.kuribo.info/tagbutton/gl.italic.gif\" width=\"18\" height=\"18\" alt=\"i\" title=\"Insert I Tags.\" /></a><a href=\"javascript:insertLink();\" class=\"tagbutton\"><img src=\"http://tools.kuribo.info/tagbutton/gl.link.gif\" width=\"18\" height=\"18\" alt=\"a\" title=\"Insert A Tags.\" /></a>";
el.innerHTML = el.innerHTML.replace("<textarea name=\"postBody\"", tagbuttonstyle + tagbutton + "<textarea name=\"postBody\"");
//alert(el.innerHTML);
