/* Constructor for text object */
function TextObject(object)
{
    this.target = object;

    // A pointer on itself for textarea
    this.target.carretHandler = this; 
    this.target.onchange = _CursorPos;
    this.target.onclick = _CursorPos;
    this.target.onkeyup = _CursorPos;
    this.target.onfocus = _CursorPos;

    // For Mozilla
    if(!document.selection)
    {
        this.target.onSelect = _CursorPos;
    }
    
    // For IE
    this.IE = null;

    this.start = -1;
    this.end = -1;
    this.scroll = -1;

}

/* Getting selected text */
TextObject.prototype.GetSelectedText = function()
{
     return this.IE? this.IE.text: (this.start >= 0 && this.end > this.start)? this.target.value.substring(this.start,this.end): "";
}

/* * * * *
* Inserting text after the cursor, if ther is no cursor, inserting in the end of object's text
* If we have selected text, inserting tags before the start and after the end of selected text
*/
TextObject.prototype.InsertTags = function(tag1, tag2)
{
    /* for IE */
    if(this.IE)
    {
        if(typeof(tag2)=="string")
        {
            var length = this.IE.text.length;
            this.IE.text = tag1 + this.IE.text + tag2;
            this.IE.moveEnd("character", - tag2.length);
            this.IE.moveStart("character", -length);   
        }
        else
        {
            this.IE.text = tag1;
        }
        this.IE.select();
    }
    /* If we have selected text */
    else if(this.start >= 0 && this.end >= this.start) 
    {
        var left = this.target.value.substring(0,this.start);
        var right = this.target.value.substr(this.end);
        var scont = this.target.value.substring(this.start, this.end);
        if(typeof(tag2)=="string")
        {
            this.target.value = left + tag1 + scont + tag2 + right;
            this.end = this.target.selectionEnd = this.start + tag1.length + scont.length;
            this.start = this.target.selectionStart = this.start + tag1.length;    
        }
        else
        {
            this.target.value = left + tag1 + right;
            this.end = this.target.selectionEnd = this.start + tag1.length;
            this.start = this.target.selectionStart = this.start + tag1.length;
        }
        this.target.scrollTop = this.scroll;
        this.target.focus();
    }
    else
    {
        this.target.value += tag1 + ((typeof(tag2)=="string")? tag2: "");
        if(this.scroll >= 0)
        {
            this.target.scrollTop = this.scroll;
        }
    }
}

TextObject.prototype.getText=function()
{
    return this.target.value;
}

TextObject.prototype.setText=function(text)
{
    this.target.value = text;
}

/* Getting cursor position */
function _CursorPos()
{
    if(document.selection)
    {
        this.carretHandler.IE = document.selection.createRange().duplicate();
    }
    else if(typeof(this.selectionStart) != "undefined")
    {
        this.carretHandler.start = this.selectionStart;
        this.carretHandler.end = this.selectionEnd;
        this.carretHandler.scroll = this.scrollTop;
    }
    else
    {
        this.carretHandler.start = this.carretHandler.end = -1;
    }
}

onload = function ()
{
    text = new TextObject(document.getElementById("textblock_text"));
    paper = new TextObject(document.getElementById("paper_text"));
}
