isIE = (navigator.appName.indexOf('Microsoft') != -1);

function storeCursor (what) {
		txtarea = what;
    if (txtarea.createTextRange) 
    txtarea.cursorPos = document.selection.createRange();
 }

function insertURL(ins) {
  if (isIE) { 
       if (txtarea.createTextRange && txtarea.cursorPos) {
         var cursorPos = txtarea.cursorPos;
         cursorPos.text =
           cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ?
             ins + ' ' : ins;
       } else {
         txtarea.value  = ins;
       }
	} else {		 
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    if (selEnd==1 || selEnd==2) selEnd=selLength;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
			if (s2=='') {
				txtarea.value = s1 + ins + s2 +'' + s3;
			} else {
				txtarea.value = s1 + ins +'' + s3;
			}	
  }
}

function insertEM(ins1, ins2) {
  if (isIE) { 
       if (txtarea.createTextRange && txtarea.cursorPos) {
         var cursorPos = txtarea.cursorPos;
         cursorPos.text = cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ?  ins1 + cursorPos.text + ins2 + ' ' : ins1 + cursorPos.text + ins2;
       } 
	} else {		 
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    if (selEnd==1 || selEnd==2) selEnd=selLength;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + ins1 + s2 + ins2 + s3;
  }
}

