if(document.getElementById) {
  var getRefById = function(id) {
    return document.getElementById(id);
  };
} else if(document.all) {
  var getRefById = function(id) {
    return document.all[id];
  };
} else {
  var getRefById = function() {
return null;
};
}

function insertText(str) {
// ID of textarea = mess         ex: <textarea id="mess" cols="25" rows="5"></textarea> 
var e = getRefById('mess'), s = null, r = null;

if(e) {
  if((s = document.selection) && s.createRange) {
  var repos = false;

/* Element must have focus otherwise text
* will be written into other elements. */
e.focus();
if((r = s.createRange())) {
  if(r.text.length) {repos = true;}
r.text = str;
/* Clear the selection to stop continuous
* overwriting of inserted text. */
     if(repos && s.empty) {s.empty();}
 }
} else {
   e.value += str;
  }
 }
}