function copyT(areaid) {
   if (typeof (document.getElementById(areaid).caretPos) != 'undefined') {
      if (navigator.userAgent.indexOf("Opera/9.6") >= 0) { text=document.selection.createRange().text; }
      else if (window.getSelection) { text=window.getSelection() }
      else if (document.selection) { text=document.selection.createRange().text; }
   }
   else if (typeof(document.getElementById(areaid).selectionStart) != 'undefined') {
      text=document.getElementById(areaid).value.substring(document.getElementById(areaid).selectionStart,document.getElementById(areaid).selectionEnd)
   }
}
function storeCaret(element) {
   if (typeof(document.selection) != 'undefined' && typeof(document.selection.createRange) != 'undefined')
   element.caretPos=document.selection.createRange().duplicate();
}
function insTxt(code, areaid) {
   copyT(areaid);
   text='<'+code+'>'+text+'</'+code+'>'
   return insDT(areaid);
}
function insSm(code, areaid) {
   copyT(areaid);
   text=code;
   return insDT(areaid);
}
function insA(areaid) {
   copyT(areaid);
   var msg = prompt('Введите URL:','http://');
   if (!text) {
      text = prompt('Имя ссылки:','');
   }
   text='<a href=\"'+msg+'\" target=\"_blank\">'+text+'</a>'
   return insDT(areaid);
}
function insDT(areaid) {
   var element = document.getElementById(areaid)
   if (!element)
   return;
   element.focus();
   if (typeof(element.caretPos) != 'undefined')
      element.caretPos.text = text;
   else if (typeof(element.selectionStart) != 'undefined') {
      var newPos = element.selectionEnd + text.length;
      element.value = element.value.substring(0, element.selectionStart) + text + element.value.substring(element.selectionEnd, element.value.length);
      element.setSelectionRange(newPos, newPos);
   }
   else
      element.value += text;
}

