// (c) 2007 by Steve Grantz
//
// ==UserScript==
// @name           UBB Insert at Cursor
// @namespace      http://www.visi.com/~sgrantz
// @description    Overloads javascript used to insert at cursor so that the function will also work with Firefox
// @include        http://forumserver.twoplustwo.com/newreply.php*
// @include        http://forumserver.twoplustwo.com/newpost.php*
// @include        http://forumserver.twoplustwo.com/addreply.php*
// @include        http://forumserver.twoplustwo.com/addpost.php*
// @include        http://forumserver.twoplustwo.com/editreply.php*
// @include        http://forumserver.twoplustwo.com/editpost.php*
// ==/UserScript==

    var xpath = "//body";
    var candidates = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

    var s = document.createElement('script');
    s.setAttribute('type','text/javascript');
    var scriptText = document.createTextNode(
           'function insertAtCaret (textEl, text) {\n' +

           ' if (textEl.selectionStart || textEl.selectionStart == "0") {\n' +
           '   var startPos = textEl.selectionStart;\n' +
           '   var endPos = textEl.selectionEnd;\n' +
           '   textEl.value = textEl.value.substring(0, startPos)\n' +
           '   + text\n' +
           '   + textEl.value.substring(endPos, textEl.value.length);\n' +
	   '   endPos = startPos + text.length;\n' +
	   '   textEl.setSelectionRange(endPos ,endPos);\n' +
           ' } else {\n' +
           '      textEl.value  = textEl.value + text;\n' +
           ' }\n' +
	   ' return true;\n' +
           '}'
                     );
    s.appendChild(scriptText);
    cand = candidates.snapshotItem(0);
    cand.appendChild(s);

