function jsTextEare()
{

};
jsTextEare.setEnterKey = function(ctrl) {
    try {
        jsTextEare.TraceCursorPosition(ctrl);
    }
    catch (e) {
        alert("ERROR FOR jsTextEare.setEnterKey:" + e.message);
    }
    CollectGarbage();
};
// 跟踪光标位置
jsTextEare.cursPos=null;
jsTextEare.TraceCursorPosition = function(obj) {
    jsTextEare.cursPos = jsTextEare.CursorPosition(obj);
    if (event.keyCode == "13") {
        var arrTmep = obj.value.slice(0, jsTextEare.cursPos.start).split('\n');
        var startValueLen = (obj.value.slice(0, jsTextEare.cursPos.start)).length;
        obj.value = obj.value.slice(0, jsTextEare.cursPos.start) + '\n' + obj.value.slice(jsTextEare.cursPos.end);
        var range = obj.createTextRange();
        range.moveEnd("character", -obj.value.length);
        range.moveEnd("character", startValueLen - arrTmep.length + 2);
        range.moveStart("character", startValueLen - arrTmep.length + 2);
        range.select();
    }
    obj.focus();
};
jsTextEare.CursorPosition=function(textBox)
{
    var start = 0, end = 0;
    //如果是Firefox(1.5)的话，方法很简单
    if(typeof(textBox.selectionStart) == "number"){
        start = textBox.selectionStart;
        end = textBox.selectionEnd;
    }
    //下面是IE(6.0)的方法，麻烦得很，还要计算上'\n'
    else if(document.selection) {
        var range = document.selection.createRange();
        if(range.parentElement().id == textBox.id) {
        // create a selection of the whole textarea
        var range_all = document.body.createTextRange();
        range_all.moveToElementText(textBox);
        //两个range，一个是已经选择的text(range)，一个是整个textarea(range_all)
        //range_all.compareEndPoints()比较两个端点，如果range_all比range更往左(further to the left)，则                
        //返回小于0的值，则range_all往右移一点，直到两个range的start相同。
        // calculate selection start point by moving beginning of range_all to beginning of range
        for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
            range_all.moveStart('character', 1);
            // get number of line breaks from textarea start to selection start and add them to start
            // 计算一下\n
            for (var i = 0; i <= start; i ++) {
                if (textBox.value.charAt(i) == '\n')
                    start++;
            }
            // create a selection of the whole textarea
            var range_all = document.body.createTextRange();
            range_all.moveToElementText(textBox);
            // calculate selection end point by moving beginning of range_all to end of range
            for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end ++) {
                range_all.moveStart('character', 1);
            }
            // get number of line breaks from textarea start to selection end and add them to end
            for (var i = 0; i <= end; i ++) {
                if (textBox.value.charAt(i) == '\n')
                    end ++;
            }
        }
    }
    //return [start, end]; // 包括选中区域的起始位置
    // modified to return as Object
    return {"start": start, "end": end, "item": [start, end]};
};