<script type="text/javascript">

function formatString(arg, inp, beg, end, msg) {
  // var str = prompt("Enter text to be *"+msg+"*:", inp);
  var str = inp;
  if (arg.createTextRange && arg.selection) {
    if (str != null) {
      str = (arg.selection.text.charAt(arg.selection.text.length - 1) == ' ')
      ? str.replace(/\s+$/,"") : str;
      str = beg + str + end;
    } else {
      arg.selection = null;
    }
  } else {
    if (str != null) {
      str = beg + str + end;
    }
  }
  return str;
}

function insertAtCaret(objTxt, beg, end, msg) {
  if (objTxt.createTextRange){
    var str = (objTxt.selection) ? objTxt.selection.text : "";
    str = formatString(objTxt, str, beg, end, msg);
    if (str == null) return true;
    if (objTxt.selection) {
      var objTxtRange = objTxt.selection;
      objTxtRange.text = (objTxtRange.text.charAt(objTxtRange.text.length - 1) == ' ')
      ? str + ' ' : str;
      objTxt.selection = null;
    } else {
      objTxt.value += str;
    }
    objTxt.focus();
  }
  return true;
}

function storeCaret(objTxt) {
  if (objTxt.createTextRange){
    objTxt.selection = document.selection.createRange().duplicate();
  }
  return true;
}

</script>
