Element.implement({tidy:function(){this.set("value",this.get("value").tidy())},getTextInRange:function(start,end){return this.get("value").substring(start,end)},getSelectedText:function(){if(this.setSelectionRange){return this.getTextInRange(this.getSelectionStart(),this.getSelectionEnd())}return document.selection.createRange().text},getSelectedRange:function(){if(this.selectionStart!=null){return{start:this.selectionStart,end:this.selectionEnd}}var pos={start:0,end:0};var range=this.getDocument().selection.createRange();if(!range||range.parentElement()!=this){return pos}var duplicate=range.duplicate();if(this.type=="text"){pos.start=0-duplicate.moveStart("character",-100000);pos.end=pos.start+range.text.length}else{var value=this.get("value");var offset=value.length;duplicate.moveToElementText(this);duplicate.setEndPoint("StartToEnd",range);if(duplicate.text.length){offset-=value.match(/[\n\r]*$/)[0].length}pos.end=offset-duplicate.text.length;duplicate.setEndPoint("StartToStart",range);pos.start=offset-duplicate.text.length}return pos},getSelectionStart:function(){return this.getSelectedRange().start},getSelectionEnd:function(){return this.getSelectedRange().end},setCaretPosition:function(pos){if(pos=="end"){pos=this.get("value").length}this.selectRange(pos,pos);return this},getCaretPosition:function(){return this.getSelectedRange().start},selectRange:function(start,end){if(this.setSelectionRange){this.focus();this.setSelectionRange(start,end)}else{var value=this.get("value");var diff=value.substr(start,end-start).replace(/\r/g,"").length;start=value.substr(0,start).replace(/\r/g,"").length;var range=this.createTextRange();range.collapse(true);range.moveEnd("character",start+diff);range.moveStart("character",start);range.select()}return this},insertAtCursor:function(value,select){var pos=this.getSelectedRange();var text=this.get("value");this.set("value",text.substring(0,pos.start)+value+text.substring(pos.end,text.length));if(select!==false){this.selectRange(pos.start,pos.start+value.length)}else{this.setCaretPosition(pos.start+value.length)}return this},insertAroundCursor:function(options,select){options=Object.append({before:"",defaultMiddle:"",after:""},options);var value=this.getSelectedText()||options.defaultMiddle;var pos=this.getSelectedRange();var text=this.get("value");if(pos.start==pos.end){this.set("value",text.substring(0,pos.start)+options.before+value+options.after+text.substring(pos.end,text.length));this.selectRange(pos.start+options.before.length,pos.end+options.before.length+value.length)}else{var current=text.substring(pos.start,pos.end);this.set("value",text.substring(0,pos.start)+options.before+current+options.after+text.substring(pos.end,text.length));var selStart=pos.start+options.before.length;if(select!==false){this.selectRange(selStart,selStart+current.length)}else{this.setCaretPosition(selStart+text.length)}}return this}});