2 * bbCode control by subBlue design [ www.subBlue.com ]
3 * Includes unixsafe colour palette selector by SHS`
8 var theSelection = false;
9 var bbcodeEnabled = true;
11 // Check for Browser & Platform for PC & IE specific bits
12 // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
13 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
14 var clientVer = parseInt(navigator.appVersion); // Get browser version
16 var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
17 var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
22 * Shows the help messages in the helpline window
24 function helpline(help)
26 document.forms[form_name].helpbox.value = help_line[help];
30 * Fix a bug involving the TextRange object. From
31 * http://www.frostjedi.com/terra/scripts/demo/caretBug.html
33 function initInsertions()
37 if (document.forms[form_name])
43 doc = opener.document;
46 var textarea = doc.forms[form_name].elements[text_name];
47 if (is_ie && typeof(baseHeight) != 'number')
50 baseHeight = doc.selection.createRange().duplicate().boundingHeight;
52 if (!document.forms[form_name])
54 document.body.focus();
62 function bbstyle(bbnumber)
66 bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
71 document.forms[form_name].elements[text_name].focus();
78 function bbfontstyle(bbopen, bbclose)
82 var textarea = document.forms[form_name].elements[text_name];
86 if ((clientVer >= 4) && is_ie && is_win)
89 theSelection = document.selection.createRange().text;
93 // Add tags around selection
94 document.selection.createRange().text = bbopen + theSelection + bbclose;
95 document.forms[form_name].elements[text_name].focus();
100 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))
102 mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
103 document.forms[form_name].elements[text_name].focus();
108 //The new position for the cursor after adding the bbcode
109 var caret_pos = getCaretPosition(textarea).start;
110 var new_pos = caret_pos + bbopen.length;
113 insert_text(bbopen + bbclose);
115 // Center the cursor when we don't have a selection
116 // Gecko and proper browsers
117 if (!isNaN(textarea.selectionStart))
119 textarea.selectionStart = new_pos;
120 textarea.selectionEnd = new_pos;
123 else if (document.selection)
125 var range = textarea.createTextRange();
126 range.move("character", new_pos);
128 storeCaret(textarea);
136 * Insert text at position
138 function insert_text(text, spaces, popup)
144 textarea = document.forms[form_name].elements[text_name];
148 textarea = opener.document.forms[form_name].elements[text_name];
152 text = ' ' + text + ' ';
155 // Since IE9, IE also has textarea.selectionStart, but it still needs to be treated the old way.
156 // Therefore we simply add a !is_ie here until IE fixes the text-selection completely.
157 if (!isNaN(textarea.selectionStart) && !is_ie)
159 var sel_start = textarea.selectionStart;
160 var sel_end = textarea.selectionEnd;
162 mozWrap(textarea, text, '');
163 textarea.selectionStart = sel_start + text.length;
164 textarea.selectionEnd = sel_end + text.length;
167 else if (textarea.createTextRange && textarea.caretPos)
169 if (baseHeight != textarea.caretPos.boundingHeight)
172 storeCaret(textarea);
174 var caret_pos = textarea.caretPos;
175 caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
180 textarea.value = textarea.value + text;
190 * Add inline attachment at position
192 function attach_inline(index, filename)
194 insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
195 document.forms[form_name].elements[text_name].focus();
199 * Add quote text to message
201 function addquote(post_id, username, l_wrote)
203 var message_name = 'message_' + post_id;
204 var theSelection = '';
207 if (l_wrote === undefined)
209 // Backwards compatibility
215 divarea = document.all[message_name];
219 divarea = document.getElementById(message_name);
222 // Get text selection - not only the post content :(
223 // IE9 must use the document.selection method but has the *.getSelection so we just force no IE
224 if (window.getSelection && !is_ie)
226 theSelection = window.getSelection().toString();
228 else if (document.getSelection && !is_ie)
230 theSelection = document.getSelection();
232 else if (document.selection)
234 theSelection = document.selection.createRange().text;
237 if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
239 if (divarea.innerHTML)
241 theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
242 theSelection = theSelection.replace(/<br\/>/ig, '\n');
243 theSelection = theSelection.replace(/<\;/ig, '<');
244 theSelection = theSelection.replace(/>\;/ig, '>');
245 theSelection = theSelection.replace(/&\;/ig, '&');
246 theSelection = theSelection.replace(/ \;/ig, ' ');
248 else if (document.all)
250 theSelection = divarea.innerText;
252 else if (divarea.textContent)
254 theSelection = divarea.textContent;
256 else if (divarea.firstChild.nodeValue)
258 theSelection = divarea.firstChild.nodeValue;
266 insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
270 insert_text(username + ' ' + l_wrote + ':' + '\n');
271 var lines = split_lines(theSelection);
272 for (i = 0; i < lines.length; i++)
274 insert_text('> ' + lines[i] + '\n');
283 function split_lines(text)
285 var lines = text.split('\n');
286 var splitLines = new Array();
288 for(i = 0; i < lines.length; i++)
290 if (lines[i].length <= 80)
292 splitLines[j] = lines[i];
300 var splitAt = line.indexOf(' ', 80);
304 splitLines[j] = line;
309 splitLines[j] = line.substring(0, splitAt);
310 line = line.substring(splitAt);
314 while(splitAt != -1);
321 * From http://www.massless.org/mozedit/
323 function mozWrap(txtarea, open, close)
325 var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
326 var selStart = txtarea.selectionStart;
327 var selEnd = txtarea.selectionEnd;
328 var scrollTop = txtarea.scrollTop;
330 if (selEnd == 1 || selEnd == 2)
335 var s1 = (txtarea.value).substring(0,selStart);
336 var s2 = (txtarea.value).substring(selStart, selEnd);
337 var s3 = (txtarea.value).substring(selEnd, selLength);
339 txtarea.value = s1 + open + s2 + close + s3;
340 txtarea.selectionStart = selStart + open.length;
341 txtarea.selectionEnd = selEnd + open.length;
343 txtarea.scrollTop = scrollTop;
349 * Insert at Caret position. Code from
350 * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
352 function storeCaret(textEl)
354 if (textEl.createTextRange)
356 textEl.caretPos = document.selection.createRange().duplicate();
363 function colorPalette(dir, width, height)
365 var r = 0, g = 0, b = 0;
366 var numberList = new Array(6);
369 numberList[0] = '00';
370 numberList[1] = '40';
371 numberList[2] = '80';
372 numberList[3] = 'BF';
373 numberList[4] = 'FF';
375 document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
377 for (r = 0; r < 5; r++)
381 document.writeln('<tr>');
384 for (g = 0; g < 5; g++)
388 document.writeln('<tr>');
391 for (b = 0; b < 5; b++)
393 color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
394 document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
395 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>');
396 document.writeln('</td>');
401 document.writeln('</tr>');
407 document.writeln('</tr>');
410 document.writeln('</table>');
415 * Caret Position object
417 function caretPosition()
425 * Get the caret position in an textarea
427 function getCaretPosition(txtarea)
429 var caretPos = new caretPosition();
431 // simple Gecko/Opera way
432 if(txtarea.selectionStart || txtarea.selectionStart == 0)
434 caretPos.start = txtarea.selectionStart;
435 caretPos.end = txtarea.selectionEnd;
437 // dirty and slow IE way
438 else if(document.selection)
440 // get current selection
441 var range = document.selection.createRange();
443 // a new selection of the whole textarea
444 var range_all = document.body.createTextRange();
445 range_all.moveToElementText(txtarea);
447 // calculate selection start point by moving beginning of range_all to beginning of range
449 for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
451 range_all.moveStart('character', 1);
454 txtarea.sel_start = sel_start;
456 // we ignore the end value for IE, this is already dirty enough and we don't need it
457 caretPos.start = txtarea.sel_start;
458 caretPos.end = txtarea.sel_start;