﻿//
// SharpSpell v3.0 - Copyright (c) 2006 Tachyon Labs (tachyon-labs.com)
//
// Real-time spell-as-you-type for the web spell checker.
// For more information, visit http://www.tachyon-labs.com/sharpspell/
//

var SharpSpellLive_Instances = [ ];

function SharpSpellLive (textArea, langId) {
    if (!textArea) { return; }
    // Live spell checker only supported in IE and Mozilla
    if (SharpSpell.IsIE || SharpSpell.IsGecko) {
	    this._textArea = textArea;
	    this._checkerUrl = "sharpspell.ashx?lcid=" + langId;
	    this._cssUrl = SharpSpell.SupportPath + "sharpspell.css";
    	
	    this._identity = SharpSpellLive_Instances.length;
	    SharpSpellLive_Instances.push(this);
    	
	    this.Initialize();
	}
}

SharpSpellLive.prototype.Update = function () 
{
    var self = this;
   
    while (self._spellArea.firstChild) {
        self._spellArea.removeChild(self._spellArea.firstChild);
    }
    
    var lines = self._textArea.value.split("\n");
    var delta = 0;
    for (var i = 0; i < lines.length; i++) 
    {
        var line = lines[i];//.replace(" ", "\u00a0");
        
        this.wrapWordsOnLine(self._spellArea, line, delta);
        delta += line.length + 1;
        //self._spellArea.appendChild(document.createTextNode(line));
        
        self._spellArea.appendChild(document.createElement("br"));
    }
    //this.wrapWords();
    self._spellArea.appendChild(document.createElement("div")).style.height='1000px';
    
    if ( SharpSpell.IsIE ) 
    {
        this._spellCont.style.width = this._textArea.clientWidth + 'px';
        if (this._textArea.scrollWidth - 12 >= 0)
        {
            this._spellArea.style.width = this._textArea.scrollWidth - 12 +'px';
        }
    }
    if ( SharpSpell.IsGecko ) 
    {
        this._spellCont.style.width = this._textArea.clientWidth + 'px';
        this._spellArea.style.width = this._textArea.scrollWidth +'px';
    }    
};

SharpSpellLive.prototype.Refresh = function () 
{
    this._lastTextAreaLength = this._textArea.value.length;
    this.Update();
    this.doSpellCheck();
};


// used to detect cut/paste operations made by mouse
SharpSpellLive.prototype.checkLengthDiff = function () 
{
    var self = this;
    if (this._textArea.value.length != this._lastTextAreaLength)
    {
        this.Refresh();
    }
    this._lastTextAreaLength = this._textArea.value.length;
    
    this._timerId2 = setTimeout( function () { self.checkLengthDiff(); }, 100 );
};

SharpSpellLive.prototype.getSelection = function() {
	if (document.selection && document.selection.createRange) {
		var selrng, rng;
		selrng = document.selection.createRange();
		rng = selrng.duplicate();
		rng.moveToElementText(this._textArea);
		rng.setEndPoint('EndToEnd', selrng);
		this._selStart = rng.text.length - selrng.text.length;
		this._selEnd   = this._selStart + selrng.text.length;
	} else if (typeof(this._textArea.selectionStart) != 'undefined') {
		this._selStart = this._textArea.selectionStart;
		this._selEnd   = this._textArea.selectionEnd;
	}
};

SharpSpellLive.prototype.TimerSyncScroll = function () {
    var self = this;
    this.SyncScroll();
    setTimeout( function () { self.TimerSyncScroll() }, 10);
};

SharpSpellLive.prototype.ShowProgress = function () {
	this._spellCheckInProgressDiv.style.top = this._textArea.offsetTop + this._textArea.offsetHeight - 45 + 'px';
	this._spellCheckInProgressDiv.style.left = this._textArea.offsetLeft + this._textArea.offsetWidth - 45 + 'px';
  	this._spellCheckInProgressDiv.style.visibility = 'visible';
}

SharpSpellLive.prototype.HideProgress = function () {
  	this._spellCheckInProgressDiv.style.visibility = 'hidden';
}

SharpSpellLive.prototype.Initialize = function () {
	var self = this;
	
	var spellArea = document.createElement("div");
	var spellCont = document.createElement("div");

	var spellCheckInProgressDiv = document.createElement("div");
	spellCheckInProgressDiv.className = "SharpSpell_SpellCheckInProgress";
	spellCheckInProgressDiv.style.position = 'absolute';
	spellCheckInProgressDiv.style.visibility = 'hidden';
	
	this._spellCheckInProgressDiv = spellCheckInProgressDiv;
	 
	this._spellArea = spellArea;
	this._spellCont = spellCont;
	
	this._textArea.style.overflow = 'scroll';
	
	// Disable Firefox 2.0 spell checker
	this._textArea.setAttribute("spellcheck", "false");
	
	this._spellCont.style.position = 'absolute';
	this._spellCont.style.top = SharpSpell.getStyle(this._textArea, "top");
	this._spellCont.style.left = SharpSpell.getStyle(this._textArea, "left");
	
    this._spellCont.style.height = this._textArea.clientHeight+4 + 'px'; 
    this._spellCont.style.width = this._textArea.clientWidth+4 + 'px';
	this._spellCont.style.overflow = 'hidden';
	
	
	this._styleFontFamily = SharpSpell.getStyle(this._textArea, "font-family");
    this._styleFontStyle = SharpSpell.getStyle(this._textArea, "font-style");
    this._styleFontVariant = SharpSpell.getStyle(this._textArea, "font-variant");
    this._styleFontWeight = SharpSpell.getStyle(this._textArea, "font-weight");
    this._styleFontSize = SharpSpell.getStyle(this._textArea, "font-size");
    this._styleFontColor = SharpSpell.getStyle(this._spellCont, "background-color");

	if ( SharpSpell.IsGecko )
	{
        this._spellArea.style.whiteSpace = '-moz-pre-wrap';
	}


    if ( SharpSpell.IsIE ) 
    {
        //this._spellCont.style.whiteSpace = "pre";
        //adjust for IE
        this._spellCont.style.height = this._textArea.clientHeight + 'px'; 
        this._spellCont.style.width = this._textArea.scrollWidth + 'px';
    }
    

	
	
	// Workaround for IE
	this._spellArea.onselectstart = function () { window.event.cancelBubble = true; return false; };
	this._spellCont.onselectstart = function () { window.event.cancelBubble = true; return false; };
	this._spellArea.onclick = function () { self._textArea.focus(); };
	this._spellCont.onclick = function () { self._textArea.focus(); };
	this._spellArea.style.cursor = "text";
	this._spellCont.style.cursor = "text";

	
	this.Update();
	
	var padTopDelta = parseInt(this._textArea.style.borderTopWidth);
	var padLeftDelta = parseInt(this._textArea.style.borderLeftWidth);
	var padRightDelta = parseInt(this._textArea.style.borderRightWidth);
	var padBottomDelta = parseInt(this._textArea.style.borderBottomWidth);
	
	padTopDelta = padTopDelta ? padTopDelta : 2 ;
	padLeftDelta = padLeftDelta ? padLeftDelta : 2 ;
	padRightDelta = padRightDelta ? padRightDelta : 2 ;
	padBottomDelta = padBottomDelta ? padBottomDelta : 2 ;

    this._spellCont.style.marginTop = padTopDelta + 1 + 'px'; 
    this._spellCont.style.marginLeft = padLeftDelta + 'px'; 
	
	this._spellArea.style.paddingTop = 2  + 'px';
	this._spellArea.style.paddingRight = padRightDelta + 'px';
	this._spellArea.style.paddingBottom = padBottomDelta + 'px';
	this._spellArea.style.paddingLeft = 1 + 'px';
	
	if (SharpSpell.IsGecko)
	{
		this._spellArea.style.paddingTop = parseInt(this._spellArea.style.paddingTop) - 1 + 'px';
		this._spellArea.style.paddingLeft = parseInt(this._spellArea.style.paddingLeft) - 1 + 'px';
		
	}
	
	
    this._spellArea.style.fontFamily = SharpSpell.getStyle(this._textArea, "font-family");
    this._spellArea.style.fontStyle = SharpSpell.getStyle(this._textArea, "font-style");
    this._spellArea.style.fontVariant = SharpSpell.getStyle(this._textArea, "font-variant");
    this._spellArea.style.fontWeight = SharpSpell.getStyle(this._textArea, "font-weight");
    this._spellArea.style.fontSize = SharpSpell.getStyle(this._textArea, "font-size");
	
	/*
	if (SharpSpell.getStyle(this._textArea, "font"))
	    { this._spellArea.style.font = SharpSpell.getStyle(this._textArea, "font"); }
	    
	if (SharpSpell.getStyle(this._textArea, "font-family"))
	    { this._spellArea.style.fontFamily = SharpSpell.getStyle(this._textArea, "font-family"); }
	else
	    { this._textArea.style.fontFamily = 'monospace'; this._spellArea.style.fontFamily = 'monospace';}

	if (SharpSpell.getStyle(this._textArea, "font-size"))
	    { this._spellArea.style.fontSize = SharpSpell.getStyle(this._textArea, "font-size"); }
	else
	    { this._textArea.style.fontSize = '10pt'; this._spellArea.style.fontSize = '10pt';}
	*/
		
    if (SharpSpell.getStyle(this._textArea, "position") != 'absolute')
    {
        this._textArea.style.position = 'relative';
    }
    
	if (SharpSpell.getStyle(this._textArea, "background-color"))
	{ 
	    this._spellCont.style.backgroundColor = SharpSpell.getStyle(this._textArea, "background-color"); 	
	}
	else 
	    { this._spellCont.style.backgroundColor = 'white'; }
	    
    this._spellArea.style.color = SharpSpell.getStyle(this._textArea, "background-color");

    this._textArea.style.backgroundColor = 'transparent';
    this._spellCont.style.zIndex = '0';
    this._textArea.style.zIndex = '99';
        
	this._spellCont.appendChild(this._spellArea);
	this._textArea.parentNode.insertBefore(this._spellCont, this._textArea);
	this._textArea.parentNode.insertBefore(spellCheckInProgressDiv, this._textArea);
	
	this._response = document.createElement("div");
	
	SharpSpell.addEvent(this._textArea, "keydown", function(e) { self.onKeyDown(e?e:window.event); } );
	SharpSpell.addEvent(this._textArea, "keyup", function(e) { self.onKeyUp(e?e:window.event); } );
	SharpSpell.addEvent(this._textArea, "scroll", function() { self.SyncScroll(); } );
	SharpSpell.addEvent(this._textArea, "select", function() { self.SyncScroll(); } );
	SharpSpell.addEvent(this._textArea, "mousedown", function() { self.SyncScroll(); } );
	SharpSpell.addEvent(this._textArea, "mouseup", function() { self.SyncScroll(); } );
	SharpSpell.addEvent(this._textArea, "change", function() { self.SyncScroll(); } );
	SharpSpell.addEvent(this._textArea, "mousemove", function (e) { self.SyncScroll(); return self.updateMousePos(e?e:window.event); });

    // Gecko has a bug with the scroll event for textareas
    if (SharpSpell.IsGecko) {
        setTimeout( function () { self.TimerSyncScroll(); }, 10 );
    }
    
	SharpSpell.addEvent(this._textArea, "contextmenu", 
	    function(e) 
	    { 
	        e=(e)?e:window.event; 
	        var x, y;
	        var scroll = SharpSpell.getScrollXY();

	        x = e.clientX - SharpSpell.getRealCoords(self._textArea, "x") + scroll.x;
	        y = e.clientY - SharpSpell.getRealCoords(self._textArea, "y") + scroll.y;

	        if (self.HitTest(x, y))
	        {
	            e.cancelBubble = true;
	            if (e.preventDefault) { e.preventDefault(); }
	            return false;
	        }
	    } 
	);
	
	this._textArea.SharpSpell_Instance = this;
	this._textArea.SharpSpell_Refresh = function () { self.Refresh(); };
	
	if (self._textArea.value.length > 0 ) {
    	self.initSpell();
	}	
	
    this._lastTextAreaLength = this._textArea.value.length;
	this._timerId2 = setTimeout( function () { self.checkLengthDiff(); }, 100 );
};

SharpSpellLive.prototype.onKeyDown = function (e) 
{
    var self = this;
    if (e.keyCode >= 37 && e.keyCode <=40) { return; }
    if (e.keyCode == 16) { return; }
    if (e.keyCode == 17) { return; }
    if (e.keyCode == 18) { return; }
    if (e.keyCode == 20) { return; }
    if (e.keyCode == 9) { return; }
    if (e.keyCode == 33) { return; }
    if (e.keyCode == 34) { return; }
    if (e.keyCode == 35) { return; }
    if (e.keyCode == 36) { return; }
    if (e.keyCode == 45) { return; }
    if (e.keyCode == 27) { return; }
    if (e.keyCode == 144) { return; }
    
    // copy
    if(e.ctrlKey && e.keyCode == 67 ) { return; }
    
    clearTimeout(this._timerId2);
    
    this.getSelection();
    if (this._selEnd > this._selStart) { this.Update(); }
    
    this.getSelection(); 
    this.updateChar(e.keyCode);
    this.SyncScroll(); 
    this.initSpell();
};

SharpSpellLive.prototype.onKeyUp = function (e) 
{
    var self = this;
    
    this._lastTextAreaLength = this._textArea.value.length;
    clearTimeout(this._timerId2);
    this._timerId2 = setTimeout( function () { self.checkLengthDiff(); }, 1000 );
};

SharpSpellLive.prototype.updateChar = function (charCode)
{    
	var self = this;
    var index = this._selStart;
    var node = this._spellArea;
    var charPos = 0;

    for (var c = node.firstChild; c; c = c.nextSibling) {
        if (c.nodeType == 3 || (c.firstChild && c.firstChild.nodeType == 3)) 
        {
            if (c.nodeType == 3) 
            {
                node = c;
            }
            else if (c.firstChild.nodeType == 3)
            {
                node = c.firstChild;
            }
            else
            {
                continue;
            }

            if ((charPos <= index) && (charPos + node.nodeValue.length >= index))
            {
                // word in this node
                var start = index - charPos;
                var pn, nv, before, after;
                pn = node.parentNode;
                nv = node.nodeValue;
                if (charCode == 8) { //backspace
                    this.removeTextInNode(node, start-1, 1);
				} else if (charCode == 46) { //delete key
                    this.removeTextInNode(node, start+1, 1);
				} else if (charCode == 32 || charCode == 13) {
				    //whitespace handling
				    if (c.nodeType == 1){
				        if (start < node.nodeValue.length) { c.className = ""; }
    				        
				        if (charCode == 13) {
				            this.insertObjInNode(c.firstChild, start, document.createElement("br"));
				        } else {
				            node = c.nextSibling;
				            node.nodeValue = " " + node.nodeValue;
				        }   
				    } else {
				        if (charCode == 13) {
				            this.insertObjInNode(c, start, document.createElement("br"));
				        } else {
				            this.insertTextInNode(node, start, " "); 
				        }
				    }
				    return;
				} else {
				    this.insertTextInNode(node, start, " ");
				    if (c.nodeType == 1) { c.className = ""; }
				}
				

				return; 
            }
        }
        if (c.nodeName == "BR") 
            { charPos += 1; }
        else 
	        { charPos += SharpSpell.getTextInternal(c, false).length; }
    }
};

SharpSpellLive.prototype.removeTextInNode = function (node, position, len) {
    if (node.nodeType == 3) {
        if (position == -1){
            if (node.previousSibling && node.previousSibling.nodeName == "BR")
            {
                node.parentNode.removeChild(node.previousSibling);
            }    
            else
            {
                var n = node.parentNode;
                while ((n = n.previousSibling)) 
                {
                    if (n.nodeName == "BR") {
                        n.parentNode.removeChild(n);
                        return;
                    }
                }            
            }
        } else if (position > node.nodeValue.length) { 
            if (node.nextSibling && node.nextSibling.nodeName == "BR")
            {
                node.parentNode.removeChild(node.nextSibling);
            }    
            else
            {
                var n = node.parentNode;
                while ((n = n.nextSibling)) 
                {
                    if (n.nodeName == "BR") {
                        n.parentNode.removeChild(n);
                        return;
                    }
                }            
            }
        } else {
            var nodeValue = node.nodeValue;
            var leftPart = nodeValue.substr(0, position);
            var rightPart = nodeValue.substr(position + len, nodeValue.length);
            
            node.nodeValue = leftPart + rightPart;
        }
    }
}

SharpSpellLive.prototype.insertTextInNode = function (node, position, text) {
    if (node.nodeType == 3) {
        var nodeValue = node.nodeValue;
        var leftPart = nodeValue.substr(0, position);
        var midPart = text;
        var rightPart = nodeValue.substr(position, nodeValue.length);
        
        node.nodeValue = leftPart + midPart + rightPart;
    }
}

SharpSpellLive.prototype.insertObjInNode = function (node, position, obj) {
    if (node.nodeType == 3) {
        var nodeValue = node.nodeValue;
        var leftPart = nodeValue.substr(0, position);
        var rightPart = nodeValue.substr(position, nodeValue.length);
        node.parentNode.insertBefore(obj, node.nextSibling);
        node.nodeValue = leftPart;
        if (rightPart.length) 
        {
            node.parentNode.insertBefore(document.createTextNode(rightPart), obj.nextSibling);
        }
    }
}

SharpSpellLive.prototype.HitTest = function (x, y) {
    var self = this;
	var arrElements = SharpSpell.getElementsByClassName(this._spellArea, "span", "SharpSpell_Misspelling");
	for(var i=0; i<arrElements.length; i++)
	{
	    var elLeft = arrElements[i].offsetLeft - this._spellCont.scrollLeft;
	    var elRight = elLeft + arrElements[i].offsetWidth;
	    var elTop = arrElements[i].offsetTop - this._spellCont.scrollTop;
	    var elBottom = elTop + arrElements[i].offsetHeight;
        if (x>=elLeft && x<=elRight && y>=elTop && y<=elBottom)
        {   
            SharpSpellMenu.ShowMenu(document.getElementById(arrElements[i].id), SharpSpellLive.HandleMenu);
            return true;
        }
	}
};

SharpSpellLive.prototype.SyncScroll = function () 
{
    this._spellCont.scrollTop = this._textArea.scrollTop;
    this._spellCont.scrollLeft = this._textArea.scrollLeft;
};
   
SharpSpellLive.prototype.initSpell = function () {
	var self = this;
	clearTimeout(this._timerId);
	this._timerId = setTimeout(function() { self.doSpellCheck(); }, 300);

};

SharpSpellLive.prototype.doSpellCheck = function () {
	var self = this;

    if (!(this._textArea.value)) { return; }
    var xmlhttp = SharpSpell.createXMLHTTP();

	xmlhttp.open( "POST" , this._checkerUrl );
	xmlhttp.onreadystatechange =
		function () 
		{   
			if (xmlhttp.readyState == 4) {
				self.HideProgress();
			    if (xmlhttp.responseText) {
			        self._response.innerHTML = xmlhttp.responseText;
				    self.RewireEvents();
				}
				xmlhttp.abort();
			}
		};

	this.ShowProgress();
	xmlhttp.send( this._textArea.value );
};

SharpSpellLive.prototype.GetText = function() {
	return SharpSpell.getTextInternal(this._spellArea, false);
};

SharpSpellLive.prototype.GetTextHtml = function() {
	return SharpSpell.getTextInternal(this._spellArea, true);
};

SharpSpellLive.prototype.RewireEvents = function () 
{
    var self = this;
   	this.Update();

    var userdic, ignore;
    userdic = SharpSpell.getUserDictionary();
    ignore = SharpSpell.getUserIgnoreList();

	var arrElements = SharpSpell.getElementsByClassName(this._response, "a", "SharpSpell_Misspelling");
	var lastNode;
	for(var i=0; i<arrElements.length; i++){
	    arrElements[i].instance = this;
        if (userdic.indexOf("|" + arrElements[i].innerHTML.toLowerCase() + "|") == -1 && ignore.indexOf("|" + arrElements[i].innerHTML.toLowerCase() + "|") == -1)
        {
   		    lastNode = this.highlightWord(this._spellArea, arrElements[i], lastNode);
   		}
   		else
   		{
   		    arrElements[i].className = "";
   		}
	}
	this._lastNode = null;
};



SharpSpellLive.prototype.highlightWord = function (node, wordObj, startNode)
{    
	var self = this;
    var word = wordObj.innerHTML;
    var suggestions = wordObj.getAttribute("suggestions");
    var index = parseInt(wordObj.getAttribute("index"));
    

    if (!startNode) 
    {
        startNode = node.firstChild;
    }
    
    var fInner = false;
    for (var c = startNode; c; c = c.nextSibling) {
        var objNode = c;
        if (c.nodeType == 1 && c.firstChild) 
        { 
            objNode = c.firstChild; 
        }
        if (objNode.nodeType == 3) 
        {
            var el, elText;
            var pn, nv, before, after, docWordVal;
            pn = objNode.parentNode;
            nv = objNode.nodeValue;
            nlen = (c.innerText)?c.innerText.length:objNode.length;
            
            if (c.startIndex <= index && (index+word.length) <= (c.startIndex+nlen))
            {
                var start = (index-c.startIndex);
                if (fInner) { start = index-(nlen-start) + 1; }
                
 			    el = document.createElement("span");
			    el.id = "SharpSpell_Misspelling_" + this._identity + "_" + index;
			    el.className = "SharpSpell_Misspelling";
			    el.setAttribute("suggestions", suggestions);
			    el.setAttribute("index", index);
			    el.instance = this;
			    el.style.whiteSpace = "nowrap";
			    el.style.position = "relative"; // IE workaround
			    el.style.display = "inline-block" // set hasLayout to true otherwise CSS bugs occur
			    
            el.style.fontFamily = this._styleFontFamily;
            el.style.fontStyle = this._styleFontStyle;
            el.style.fontVariant = this._styleFontVariant;
            el.style.fontWeight = this._styleFontWeight;
            el.style.fontSize = this._styleFontSize;
            el.style.color = this._styleFontColor;

			    el.appendChild(document.createTextNode(word));

                before = document.createTextNode(nv.substr(0,start));
			    after = document.createTextNode(nv.substr(start + word.length));
			    if (before.length){
			        pn.insertBefore(before,objNode);
			    }
			    pn.insertBefore(el,objNode);
			    if (after.length){
			        pn.insertBefore(after,objNode);
			    }
			    pn.removeChild(objNode);       
			    
			    return pn; 
            }
            
            
        }

    }
};

SharpSpellLive.prototype.wrapWordsOnLine = function (parent, line, delta)
{
    var charPos = delta;
    
    var words = line.split(/[\s\n-]/);
    //var words = line.split(" ");

    for (var i = 0; i < words.length; i++) 
    {
        if (words[i].length == 0) {
            parent.appendChild(document.createTextNode("\u00a0"));          
            charPos++;  
        }
        else{
            var span = document.createElement("span");
            span.startIndex = charPos;
            span.style.whiteSpace = "nowrap";
            span.appendChild(document.createTextNode(words[i]));

            span.style.fontFamily = this._styleFontFamily;
            span.style.fontStyle = this._styleFontStyle;
            span.style.fontVariant = this._styleFontVariant;
            span.style.fontWeight = this._styleFontWeight;
            span.style.fontSize = this._styleFontSize;
            span.style.color = this._styleFontColor;
            
            span.style.backgroundColor = "transparent";

            parent.appendChild(span);
            parent.appendChild(document.createTextNode(" "));
            charPos += words[i].length + 1;
        }
    }
    this._lastCharPos = charPos;
};

SharpSpellLive.prototype.updateMousePos = function(e)
{
	SharpSpell.MouseY = e.clientY;
	SharpSpell.MouseX = e.clientX;
};



SharpSpellLive.HandleMenu = function (selword, owner)
{
	//var selword;
	//selword = SharpSpell.getTextInternal(sender);
	selword = unescape(selword);
    
    if (selword.split(";").length > 1)
    {
   	    switch (selword.split(";")[1])
   	    {
 	        case "AddToDictionary":
                SharpSpell.addWordToDictionary(SharpSpell.getTextInternal(owner).toLowerCase());
                owner.instance.Update();
                owner.instance.RewireEvents();
                return;
	        case "Ignore":
                SharpSpell.addWordToIgnore(SharpSpell.getTextInternal(owner).toLowerCase());
                owner.instance.Update();
                owner.instance.RewireEvents();
                return;
            case "ClearCustom":
                if (confirm("Are you sure you want to clear your custom dictionary?"))
                {
                    SharpSpell.clearUserSettings();
                    owner.instance.Refresh();
                }
                return;
	        case "NoSuggestions":
    	        return;
        }   
    }
	else
	{
	    var scrollX, scrollY;
	    scrollX = owner.instance._textArea.scrollLeft;
	    scrollY = owner.instance._textArea.scrollTop;
        if (selword) {
            var str = owner.instance._textArea.value;
            var len = str.length;
            var start = Number(owner.getAttribute("index"));
            var end = start + owner.innerHTML.length ;
            
            var newVal = str.substring(0, start) + selword + str.substring(end, len);
            
            owner.instance._textArea.value = newVal;
		    owner.className = "";
		    owner.firstChild.nodeValue = selword;
	    }  
	    owner.instance._textArea.scrollTop = scrollY;
	    owner.instance._textArea.scrollLeft = scrollX;
    }
}

