2 * bbCode control by subBlue design [ www.subBlue.com ]
3 * Includes unixsafe colour palette selector by SHS`
8 var theSelection = false;
10 // Check for Browser & Platform for PC & IE specific bits
11 // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
12 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
13 var clientVer = parseInt(navigator.appVersion); // Get browser version
15 var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
16 var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
21 * Shows the help messages in the helpline window
23 function helpline(help)
25 document.forms[form_name].helpbox.value = help_line[help];
29 * Fix a bug involving the TextRange object. From
30 * http://www.frostjedi.com/terra/scripts/demo/caretBug.html
32 function initInsertions()
35 if(document.forms[form_name])
41 doc = opener.document;
44 var textarea = doc.forms[form_name].elements[text_name];
45 if (is_ie && typeof(baseHeight) != 'number')
48 baseHeight = doc.selection.createRange().duplicate().boundingHeight;
50 if (!document.forms[form_name])
52 document.body.focus();
60 function bbstyle(bbnumber)
64 bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
69 document.forms[form_name].elements[text_name].focus();
76 function bbfontstyle(bbopen, bbclose)
80 var textarea = document.forms[form_name].elements[text_name];
84 if ((clientVer >= 4) && is_ie && is_win)
87 theSelection = document.selection.createRange().text;
91 // Add tags around selection
92 document.selection.createRange().text = bbopen + theSelection + bbclose;
93 document.forms[form_name].elements[text_name].focus();
98 else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
100 mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
101 document.forms[form_name].elements[text_name].focus();
106 //The new position for the cursor after adding the bbcode
107 var caret_pos = getCaretPosition(textarea).start;
108 var new_pos = caret_pos + bbopen.length;
111 insert_text(bbopen + bbclose);
113 // Center the cursor when we don't have a selection
114 // Gecko and proper browsers
115 if (!isNaN(textarea.selectionStart))
117 textarea.selectionStart = new_pos;
118 textarea.selectionEnd = new_pos;
121 else if (document.selection)
123 var range = textarea.createTextRange();
124 range.move("character", new_pos);
126 storeCaret(textarea);
134 * Insert text at position
136 function insert_text(text, spaces, popup)
142 textarea = document.forms[form_name].elements[text_name];
146 textarea = opener.document.forms[form_name].elements[text_name];
151 text = ' ' + text + ' ';
154 if (!isNaN(textarea.selectionStart))
156 var sel_start = textarea.selectionStart;
157 var sel_end = textarea.selectionEnd;
159 mozWrap(textarea, text, '');
160 textarea.selectionStart = sel_start + text.length;
161 textarea.selectionEnd = sel_end + text.length;
164 else if (textarea.createTextRange && textarea.caretPos)
166 if (baseHeight != textarea.caretPos.boundingHeight)
169 storeCaret(textarea);
171 var caret_pos = textarea.caretPos;
172 caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
177 textarea.value = textarea.value + text;
188 * Add inline attachment at position
190 function attach_inline(index, filename)
192 insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
193 document.forms[form_name].elements[text_name].focus();
197 * Add quote text to message
199 function addquote(post_id, username)
201 var message_name = 'message_' + post_id;
202 var theSelection = '';
207 divarea = document.all[message_name];
211 divarea = document.getElementById(message_name);
214 // Get text selection - not only the post content :(
215 if (window.getSelection)
217 theSelection = window.getSelection().toString();
219 else if (document.getSelection)
221 theSelection = document.getSelection();
223 else if (document.selection)
225 theSelection = document.selection.createRange().text;
228 if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
230 if (divarea.innerHTML)
232 theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
233 theSelection = theSelection.replace(/<br\/>/ig, '\n');
234 theSelection = theSelection.replace(/<\;/ig, '<');
235 theSelection = theSelection.replace(/>\;/ig, '>');
236 theSelection = theSelection.replace(/&\;/ig, '&');
237 theSelection = theSelection.replace(/ \;/ig, ' ');
239 else if (document.all)
241 theSelection = divarea.innerText;
243 else if (divarea.textContent)
245 theSelection = divarea.textContent;
247 else if (divarea.firstChild.nodeValue)
249 theSelection = divarea.firstChild.nodeValue;
255 insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
262 * From http://www.massless.org/mozedit/
264 function mozWrap(txtarea, open, close)
266 var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
267 var selStart = txtarea.selectionStart;
268 var selEnd = txtarea.selectionEnd;
269 var scrollTop = txtarea.scrollTop;
271 if (selEnd == 1 || selEnd == 2)
276 var s1 = (txtarea.value).substring(0,selStart);
277 var s2 = (txtarea.value).substring(selStart, selEnd);
278 var s3 = (txtarea.value).substring(selEnd, selLength);
280 txtarea.value = s1 + open + s2 + close + s3;
281 txtarea.selectionStart = selStart + open.length;
282 txtarea.selectionEnd = selEnd + open.length;
284 txtarea.scrollTop = scrollTop;
290 * Insert at Caret position. Code from
291 * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
293 function storeCaret(textEl)
295 if (textEl.createTextRange)
297 textEl.caretPos = document.selection.createRange().duplicate();
304 function colorPalette(dir, width, height)
306 var r = 0, g = 0, b = 0;
307 var numberList = new Array(6);
310 numberList[0] = '00';
311 numberList[1] = '40';
312 numberList[2] = '80';
313 numberList[3] = 'BF';
314 numberList[4] = 'FF';
316 document.writeln('<table class="type2">');
318 for (r = 0; r < 5; r++)
322 document.writeln('<tr>');
325 for (g = 0; g < 5; g++)
329 document.writeln('<tr>');
332 for (b = 0; b < 5; b++)
334 color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
335 document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
336 document.write('<a href="#" onclick="bbfontstyle(\'[color=#' + color + ']\', \'[/color]\'); return false;"><img src="images/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></a>');
337 document.writeln('</td>');
342 document.writeln('</tr>');
348 document.writeln('</tr>');
351 document.writeln('</table>');
356 * Caret Position object
358 function caretPosition()
366 * Get the caret position in an textarea
368 function getCaretPosition(txtarea)
370 var caretPos = new caretPosition();
372 // simple Gecko/Opera way
373 if (txtarea.selectionStart || txtarea.selectionStart == 0)
375 caretPos.start = txtarea.selectionStart;
376 caretPos.end = txtarea.selectionEnd;
378 // dirty and slow IE way
379 else if (document.selection)
381 // get current selection
382 var range = document.selection.createRange();
384 // a new selection of the whole textarea
385 var range_all = document.body.createTextRange();
386 range_all.moveToElementText(txtarea);
388 // calculate selection start point by moving beginning of range_all to beginning of range
390 for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
392 range_all.moveStart('character', 1);
395 txtarea.sel_start = sel_start;
397 // we ignore the end value for IE, this is already dirty enough and we don't need it
398 caretPos.start = txtarea.sel_start;
399 caretPos.end = txtarea.sel_start;