var start=0;
var end=0;
var range;
var scrollTop=0;
function InsertText(text){
var obj=$("content");
rnCount=0;
for(var i = 0; i <= text.length-1; i ++){
if(text.charAt(i) == '\n'){
rnCount++;
}
}

if(obj.createTextRange){
var range_all = document.body.createTextRange();
range_all.moveToElementText(obj);
for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
range_all.moveStart('character', 1);
var caretPos = obj.caretPos;   
caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text; 
var rng=obj.createTextRange();
rng.moveStart("character",start+text.length-rnCount);
rng.collapse(true);                           
rng.select();
}
else{
if(obj.value==""){
obj.value=text;
var position=text.length-rnCount;
obj.focus();
obj.setSelectionRange(position, position);
TraceCursorPosition(obj);
}
else{
obj.value = obj.value.slice(0, start) + text + obj.value.slice(end);
var tempScrollTop=scrollTop;
var position=start+text.length-rnCount;
obj.focus();
obj.setSelectionRange(position, position);
obj.scrollTop=tempScrollTop;
TraceCursorPosition(obj);
}
}
}

function TraceCursorPosition(obj){ 
if(typeof(obj.selectionStart)=="number"){
scrollTop = obj.scrollTop;
start = obj.selectionStart;
end = obj.selectionEnd;
}
else if(document.selection){
range = document.selection.createRange();
obj.caretPos = document.selection.createRange().duplicate();    
}
}