function insertAtCursor(textarea, text) {
 
	textarea.focus();
	
	var start, end, sel, scrollPos, subst;
	
	if (typeof(document["selection"]) != 'undefined') {
	
		sel = document.selection.createRange().text
	
	} else if (typeof(textarea["setSelectionRange"]) != "undefined") {
	
		start		= textarea.selectionStart;
		end			= textarea.selectionEnd;
		scrollPos 	= textarea.scrollTop;
		sel 		= textarea.value.substring(start, end);
		
	}
	
	textarea.value = textarea.value.substring(0, start) + text + textarea.value.substring(end);
	textarea.setSelectionRange(start + text.length, start + text.length);
	textarea.scrollTop = scrollPos;
 
}

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE :-)