Doesn't look like the script is removing the original length of the selected string, when recreating the selection. If you select one character, it will offset the selection by an extra character, at the end. Select two, click a button, off by two, after your initial selection.
txtarea.selectionEnd = intEnd + text.length;
should be
txtarea.selectionEnd = intEnd + before.length + after.length;
txtarea.selectionEnd = intEnd + text.length;
should be
txtarea.selectionEnd = intEnd + before.length + after.length;






























