function tag(t){
//tag selected text in textarea
var r=document.selection.createRange();
var t1=t;
if (t=='a') t1=t+' href="'+prompt('URL:','http://')+'"';
else if (t=='img') t1=t+' src="'+prompt('filename:','pictures/')+'"';
if (r.parentElement().tagName=='TEXTAREA'){
r.text='<'+t1+'>'+r.text+'</'+t+'>';
r.select();
}
}

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

function insertAtCaret (textEl) {
var fn='';
var text='';
if (fn=prompt('file:','pictures/')) text='<img src="'+fn+'" class="picture">';
        if (textEl.createTextRange && textEl.caretPos) {
         var caretPos = textEl.caretPos;
         caretPos.text =
           caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
             text + ' ' : text;
      }
       else
       textEl.value  = text;
}
