If you have the selected text, you can do something like this:
I just took a quick look at the sourcecode of this forum to see how they did it. And as I feared it's a fairly complex task to insert text around a selection. But the forum manages to do it both in IE and Mozilla based browsers.