Patch adding insert at caret in phpBB 2.0.x for Firefox/Mozilla browsers and
insert improvements for IE 5.x, 6 and later versions.

Copyright (C) 2005 Hugues Fournier

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

--- posting_body.tpl.old
+++ posting_body.tpl
@@ -91,17 +91,39 @@
 	}
 }

+// Insert at caret patch
+// Copyright (C) 2005 Hugues Fournier
+// According to the copyright legislations and terms of sections 1 and 2 of the GNU GPL
+// you should not remove this copyright notice in any use or derived use of this code
+
+function insertatcaret(text) {
+	var txtarea = document.post.message;
+	if (txtarea.createTextRange && txtarea.caretPos) {
+		var caretPos = txtarea.caretPos;
+		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
+		txtarea.focus();
+	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
+		var startselPos = txtarea.selectionStart;
+		var endselPos = txtarea.selectionEnd;
+		var beforeScroll = txtarea.scrollTop;
+		txtarea.value = txtarea.value.substring(0, startselPos)
+				+ text
+				+ txtarea.value.substring(endselPos, txtarea.value.length);
+		txtarea.focus();
+		txtarea.selectionStart = startselPos + text.length;
+		txtarea.selectionEnd = startselPos + text.length;
+		txtarea.scrollTop = beforeScroll;
+	} else {
+		txtarea.value  += text;
+		txtarea.focus();
+	}
+}
+
+ //End of insert at caret patch
+
 function emoticon(text) {
-	var txtarea = document.post.message;
 	text = ' ' + text + ' ';
-	if (txtarea.createTextRange && txtarea.caretPos) {
-		var caretPos = txtarea.caretPos;
-		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
-		txtarea.focus();
-	} else {
-		txtarea.value  += text;
-		txtarea.focus();
-	}
+	insertatcaret(text); //Insert at caret patch
 }

 function bbfontstyle(bbopen, bbclose) {
@@ -143,7 +165,7 @@
 	if (bbnumber == -1) { // Close all open tags & default button names
 		while (bbcode[0]) {
 			butnumber = arraypop(bbcode) - 1;
-			txtarea.value += bbtags[butnumber + 1];
+			insertatcaret(bbtags[butnumber + 1]); // Insert at caret patch
 			buttext = eval('document.post.addbbcode' + butnumber + '.value');
 			eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
 		}
@@ -180,7 +202,7 @@
 	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
 		while (bbcode[bblast]) {
 				butnumber = arraypop(bbcode) - 1;
-				txtarea.value += bbtags[butnumber + 1];
+				insertatcaret(bbtags[butnumber + 1]); //Insert at caret patch
 				buttext = eval('document.post.addbbcode' + butnumber + '.value');
 				eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
 				imageTag = false;
@@ -190,7 +212,7 @@
 	} else { // Open tags
 	
 		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
-			txtarea.value += bbtags[15];
+			insertatcaret(bbtags[15]); //Insert at caret patch
 			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
 			document.post.addbbcode14.value = "Img";	// Return button back to normal state
 			imageTag = false;
--- posting_smilies.tpl.old
+++ posting_smilies.tpl
@@ -1,17 +1,39 @@

 <script language="javascript" type="text/javascript">
 <!--
-function emoticon(text) {
-	text = ' ' + text + ' ';
-	if (opener.document.forms['post'].message.createTextRange && opener.document.forms['post'].message.caretPos) {
-		var caretPos = opener.document.forms['post'].message.caretPos;
-		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
-		opener.document.forms['post'].message.focus();
+// Insert at caret patch
+// Copyright (C) 2005 Hugues Fournier
+// According to the copyright legislations and terms of sections 1 and 2 of the GNU GPL version 2
+// you should not remove this copyright notice in any use or derived use of this code
+
+function insertatcaret(text) {
+	var txtarea = opener.document.forms['post'].message;
+	if (txtarea.createTextRange && txtarea.caretPos) {
+		var caretPos = txtarea.caretPos;
+		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
+		txtarea.focus();
+	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
+		var startselPos = txtarea.selectionStart;
+		var endselPos = txtarea.selectionEnd;
+		var beforeScroll = txtarea.scrollTop;
+		txtarea.value = txtarea.value.substring(0, startselPos)
+			+ text
+			+ txtarea.value.substring(endselPos, txtarea.value.length);
+		txtarea.focus();
+		txtarea.selectionStart = startselPos + text.length;
+		txtarea.selectionEnd = startselPos + text.length;
+		txtarea.scrollTop = beforeScroll;
 	} else {
-	opener.document.forms['post'].message.value  += text;
-	opener.document.forms['post'].message.focus();
+		txtarea.value  += text;
+		txtarea.focus();
 	}
 }
+
+function emoticon(text) {
+	text = ' ' + text + ' ';
+	insertatcaret(text);
+}
+//End of insert at caret patch
 //-->
 </script>

CC-GNU GPL