Salome HOME
Duplicated menu after a second study opening is fixed.
[tools/siman.git] / Workspace / Siman / WebContent / rtef / richtext.js
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Richtext Editor: Fork (RTEF) VERSION: 0.002
4 // Released: 8/11/2006
5 // For the latest release visit http://richtext.cabspace.com
6 // For support visit http://cabspace.com/forums/?mforum=richtext
7 //
8 ////////////////////////////////////////////////////////////////////////////////
9
10 ////////////////////////////////////////////////////////////////////////////////
11 //
12 // The MIT License
13 //
14 // Copyright (c) 2006 Timothy Bell
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining a copy 
17 // of this software and associated documentation files (the "Software"), to deal
18 // in the Software without restriction, including without limitation the rights 
19 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
20 // copies of the Software, and to permit persons to whom the Software is 
21 // furnished to do so, subject to the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be included in 
24 // all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
27 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
28 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
29 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
30 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
32 // SOFTWARE.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35
36 // Constants
37 var minWidth = 450;                                     // minumum width
38 var wrapWidth = 1245;                       //width at which all icons will appear on one bar
39 var maxchar = 64000;                // maximum number of characters per save
40 var lang = "en";                                        //xhtml language
41 var lang_direction = "ltr";         //language direction : ltr = left-to-right, rtl = right-to-left 
42 var encoding = "utf-8";                     //xhtml encoding
43 var zeroBorder = "#c0c0c0";         //guideline color - see showGuidelines()
44 var btnText = "submit";                     //Button value for non-designMode() & fullsceen rte
45 var resize_fullsrcreen = true;
46 // (resize_fullsrcreen) limited in that: 1)won't auto wrap icons. 2)won't
47 // shrink to less than (wrapWidth)px if screen was initized over (wrapWidth)px;
48
49 var keep_absolute = true; // !!!Disabled - see line 456 for details!!!!!  
50 // By default IE will try to convery all hyperlinks to absolute paths. By
51 // setting this value to "false" it will retain the relative path.
52
53 // Pointers
54 var InsertChar;
55 var InsertTable;
56 var InsertLink;
57 var InsertImg;
58 var dlgReplace;
59 var dlgPasteText;
60 var dlgPasteWord;
61
62 //Init Variables & Attributes
63 var ua = navigator.userAgent.toLowerCase();
64 var isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1))? true:false;
65 var isIE7 = ((isIE) && (ua.indexOf("msie 7.") != -1))? true:false;
66 var     isGecko = (ua.indexOf("gecko") != -1)? true:false;
67 var     isSafari = (ua.indexOf("safari") != -1)? true:false;
68 var safariVersion = parseFloat(ua.substring(ua.lastIndexOf("safari/") + 7));
69 var     isKonqueror = (ua.indexOf("konqueror") != -1)? true:false;
70 var rng;
71 var currentRTE;
72 var allRTEs = "";
73 var obj_width;
74 var obj_height;
75 var imagesPath;
76 var includesPath;
77 var cssFile;
78 var generateXHTML = true;
79 var isRichText = false;
80 //check to see if designMode mode is available
81 //Safari/Konqueror think they are designMode capable even though they are not
82 //if(document.getElementById && document.designMode && !isSafari && !isKonqueror){
83 if(document.getElementById && document.designMode) {
84         isRichText = true;
85 } else if ((isSafari && safariVersion >= 312) || isKonqueror) {
86         //Safari 1.3+ is capable of designMode, Safari 1.3 = webkit build 312
87         isRichText = true;
88 }
89 //for testing standard textarea, uncomment the following line
90 //isRichText = false;
91
92 var replacements = new Array (
93         new RegExp(String.fromCharCode(145),'g'), "'",
94         new RegExp(String.fromCharCode(146),'g'), "'",
95         new RegExp("'"), "'",
96         //convert all types of double quotes
97         new RegExp(String.fromCharCode(147),'g'), "\"",
98         new RegExp(String.fromCharCode(148),'g'), "\"",
99         new RegExp("\""), """,              //doesn't work
100         //replace carriage returns & line feeds
101         new RegExp("[\r\n]",'g'), " ");
102
103 function rteSafe(html) {
104         html = trim(html);
105         for (i=0; i<replacements.length; i = i+2) {
106                 html = html.replace(replacements[i], replacements[i+1]);
107         }
108         return html;
109 }
110
111 // [DBC]
112 var nbcommand     = 0;              // For not duplicating separators when no command between
113 var defaultFont   = "Times New Roman, Times, serif";
114 var defaultSize   = "13px";
115 var activeCommand = {               // Strait-forward HashMap
116         put : function(foo, bar)   {this[foo] = bar;},
117         get : function(foo) {return this[foo];}
118 }
119
120 function initTextInput() {
121     activeCommand.put("cut", lblCut);
122     activeCommand.put("copy", lblCopy);
123     activeCommand.put("paste", lblPaste);
124     activeCommand.put("undo", lblUndo);
125     activeCommand.put("redo", lblRedo);
126     activeCommand.put("bold", lblBold);
127     activeCommand.put("italic", lblItalic);
128     activeCommand.put("underline", lblUnderline);
129     activeCommand.put("list", lblUL);
130     activeCommand.put("numbered_list", lblOL);
131     activeCommand.put("indent", lblIndent);
132     activeCommand.put("outdent", lblOutdent);
133     activeCommand.put("replace", lblSearch);
134     activeCommand.put("hyperlink", lblInsertLink);
135
136     wrapWidth   = 14 * 24;
137     defaultFont = "Arial, Helvetica, sans-serif";
138     initRTE("../rtef/images/", "", "", true);
139 }
140
141 function initTextEditor() {
142     activeCommand.put("save", lblSave);
143 //  activeCommand.put("print", lblPrint);
144     activeCommand.put("cut", lblCut);
145     activeCommand.put("copy", lblCopy);
146     activeCommand.put("paste", lblPaste);
147     activeCommand.put("undo", lblUndo);
148     activeCommand.put("redo", lblRedo);
149     activeCommand.put("bold", lblBold);
150     activeCommand.put("italic", lblItalic);
151     activeCommand.put("underline", lblUnderline);
152     activeCommand.put("list", lblUL);
153     activeCommand.put("numbered_list", lblOL);
154     activeCommand.put("indent", lblIndent);
155     activeCommand.put("outdent", lblOutdent);
156     activeCommand.put("replace", lblSearch);
157     activeCommand.put("hyperlink", lblInsertLink);
158
159     wrapWidth   = 15 * 24;
160     defaultFont = "Arial, Helvetica, sans-serif";
161     initRTE("../rtef/images/", "", "", true);
162 }
163
164 function addCommand(name, action) {
165     var label = activeCommand.get(name);
166     if (label != null) {
167         insertImg(label, name + ".gif", action);
168         nbcommand += 1;
169     }
170 }
171
172 function displayTextEditor(inputname, initialtext, width, height) {
173     writeRichText(inputname, rteSafe(initialtext), '', width, height, true, false, false);
174 }
175 // [/DBC]
176
177 function initRTE(imgPath, incPath, css, genXHTML){
178         // CM 05/04/05 check args for compatibility with old RTE implementations
179         if (arguments.length == 3) {
180                 genXHTML = generateXHTML;
181         }
182         //set paths vars
183         imagesPath = imgPath;
184         includesPath = incPath;
185         cssFile = css;
186         generateXHTML = genXHTML;
187         if(isRichText){
188         document.writeln('<style type="text/css">@import "' + includesPath + 'rte.css";</style>');
189         }
190         if(!isIE){
191                 minWidth = minWidth-48;
192                 wrapWidth = wrapWidth-102;
193         }
194 }
195
196 function writeRichText(rte, html, css, width, height, buttons, readOnly, fullscreen) {
197         currentRTE = rte;
198         if(allRTEs.length > 0){
199         allRTEs += ";";
200         }
201         allRTEs += rte;
202         // CM 06/04/05 stops single quotes from messing everything up
203         html=replaceIt(html,'\'','&apos;');
204         // CM 05/04/05 a bit of juggling for compatibility with old RTE implementations
205         if (arguments.length == 6) {
206                 fullscreen = false;
207                 readOnly = buttons;
208                 buttons = height;
209                 height = width;
210                 width = css;
211                 css = "";
212         }
213         var iconWrapWidth = wrapWidth;
214         var tablewidth;
215         if(readOnly) {
216                 buttons = false;
217         }
218         if(fullscreen) {
219                 readOnly = false; // fullscreen is not readOnly and must show buttons
220                 buttons = true;
221                 // resize rte on resize if the option resize_fullsrcreen = true.
222                 if(isRichText && resize_fullsrcreen) {
223                         window.onresize = resizeRTE;
224                 }
225                 document.body.style.margin = "0px";
226                 document.body.style.overflow = "hidden";
227                 //adjust maximum table widths
228                 findSize("");
229                 width = obj_width;
230                 if(width < iconWrapWidth) {
231                         height = (obj_height - 83);
232                 } else {
233                         height = (obj_height - 55);
234                 }
235                 if (width < minWidth) {
236                         document.body.style.overflow = "auto";
237                         if(isIE) {
238                                 height = obj_height-22;
239                         } else {
240                                 height = obj_height-24;
241                         }
242                         width = minWidth;
243                 }
244                 tablewidth = width;
245         } else {
246                 fullscreen = false;
247                 iconWrapWidth = iconWrapWidth-25;
248                 //adjust minimum table widths
249                 if(buttons && (width < minWidth)) {
250                   width = minWidth;
251           }
252           if(isIE){
253                   tablewidth = width;
254           } else {
255                   tablewidth = width + 4;
256           }
257   }
258   if(isRichText) {
259           var rte_css = "";
260           if(css.length > 0) {
261                   rte_css = css;
262           } else {
263                   rte_css = cssFile;
264           }
265           document.writeln('<span class="rteDiv">');
266           if(buttons) {
267                   document.writeln('<table class="rteBk" cellpadding=0 cellspacing=0 id="Buttons1_'+rte+'" width="' + tablewidth + '">');
268                   document.writeln('<tbody><tr>');
269                   insertBar();
270                   if(fullscreen || activeCommand.get("save") != null){
271                           document.writeln('<td><input type=image class="rteImg" src="'+imagesPath+'save.gif" alt="'+lblSave+'" title="'+lblSave+'" onmouseover="this.className=\'rteImgUp\'" onmouseout="this.className=\'rteImg\'" onmousedown="this.className=\'rteImgDn\'" onmouseup="this.className=\'rteImgUp\'" onClick="submitEdit()"></td>');
272                           nbcommand += 1;
273                   }
274                   if(!isSafari && !isKonqueror) {
275                           addCommand("print","rtePrint('"+rte+"')");
276 //                        insertImg(lblPrint,"print.gif","rtePrint('"+rte+"')");
277                           insertSep();
278                           addCommand("selectall","toggleSelection('"+rte+"')");
279 //                        insertImg(lblSelectAll,"selectall.gif","toggleSelection('"+rte+"')");
280                           addCommand("unformat","rteCommand('"+rte+"','removeformat')");
281 //                        insertImg(lblUnformat,"unformat.gif","rteCommand('"+rte+"','removeformat')");
282                           insertSep();
283                   }
284                   if(isIE || isSafari || isKonqueror) {
285                           addCommand("cut","rteCommand('"+rte+"','cut')");
286 //                        insertImg(lblCut,"cut.gif","rteCommand('"+rte+"','cut')");
287                           addCommand("copy","rteCommand('"+rte+"','copy')");
288 //                        insertImg(lblCopy,"copy.gif","rteCommand('"+rte+"','copy')");
289                   }
290                   if(isSafari || isKonqueror) {
291                           addCommand("paste","rteCommand('"+rte+"','InsertText')");
292 //                        insertImg(lblPaste,"paste.gif","rteCommand('"+rte+"','InsertText')");
293                   }
294                   if(isIE) {
295                           addCommand("paste","rteCommand('"+rte+"','paste')");
296 //                        insertImg(lblPaste,"paste.gif","rteCommand('"+rte+"','paste')");
297                   }
298                   if(!isSafari && !isKonqueror) {
299                           addCommand("pastetext","dlgLaunch('"+rte+"','text')");
300 //                        insertImg(lblPasteText,"pastetext.gif","dlgLaunch('"+rte+"','text')");
301                           addCommand("pasteword","dlgLaunch('"+rte+"','word')");
302 //                        insertImg(lblPasteWord,"pasteword.gif","dlgLaunch('"+rte+"','word')");
303                   }
304                   insertSep();
305                   addCommand("undo","rteCommand('"+rte+"','undo')");
306 //                insertImg(lblUndo,"undo.gif","rteCommand('"+rte+"','undo')");
307                   addCommand("redo","rteCommand('"+rte+"','redo')");
308 //                insertImg(lblRedo,"redo.gif","rteCommand('"+rte+"','redo')");
309                   if(!isSafari && !isKonqueror) {
310 //                        insertSep();
311 //        document.writeln('<td>');
312 //        document.writeln('<select id="formatblock_'+rte+'" onchange="selectFont(\''+rte+'\', this.id);return false" style="font-size:14px;width:105px;height:20px;margin:1px;">');
313 //                        document.writeln(lblFormat);
314 //                        document.writeln('</select></td><td>');
315 //                        document.writeln('<select id="fontname_'+rte+'" onchange="selectFont(\''+rte+'\', this.id);return false" style="font-size:14px;width:125px;height:20px;margin:1px;">');
316 //                        document.writeln(lblFont);
317 //                        document.writeln('</select></td><td>');
318 //                        document.writeln('<select unselectable="on" id="fontsize_'+rte+'" onchange="selectFont(\''+rte+'\', this.id);return false" style="font-size:14px;width:75px;height:20px;margin:1px;">');
319 //                        document.writeln(lblSize);
320 //                        document.writeln('</select></td>');
321                   }
322                   if(tablewidth < iconWrapWidth){
323                           document.writeln('<td width="100%"></td></tr></tbody></table>');
324                           document.writeln('<table class="rteBk" cellpadding="0" cellspacing="0" id="Buttons2_'+rte+'" width="' + tablewidth + '">');
325                           document.writeln('<tbody><tr>');
326                   }
327                   insertBar();
328                   addCommand("bold","rteCommand('"+rte+"','bold')");
329 //                insertImg(lblBold,"bold.gif","rteCommand('"+rte+"','bold')");
330                   addCommand("italic","rteCommand('"+rte+"','italic')");
331 //                insertImg(lblItalic,"italic.gif","rteCommand('"+rte+"','italic')");
332                   addCommand("underline","rteCommand('"+rte+"','underline')");
333 //                insertImg(lblUnderline,"underline.gif","rteCommand('"+rte+"','underline')");
334                   //if(!isSafari && !isKonqueror) insertImg(lblStrikeThrough,"strikethrough.gif","rteCommand('"+rte+"','strikethrough')");
335                   //insertImg(lblSuperscript,"superscript.gif","rteCommand('"+rte+"','superscript')");
336                   //if(!isSafari && !isKonqueror){
337       //insertImg(lblSubscript,"subscript.gif","rteCommand('"+rte+"','subscript')");
338       //} else {
339       //insertImg(lblSubscript,"subscript.gif","rteCommand('"+rte+"','StyleChange',sub)");
340       //}
341                   insertSep();
342                   addCommand("left_just","rteCommand('"+rte+"','justifyleft')");
343 //                insertImg(lblAlgnLeft,"left_just.gif","rteCommand('"+rte+"','justifyleft')");
344                   addCommand("centre","rteCommand('"+rte+"','justifycenter')");
345 //                insertImg(lblAlgnCenter,"centre.gif","rteCommand('"+rte+"','justifycenter')");
346                   addCommand("right_just","rteCommand('"+rte+"','justifyright')");
347 //                insertImg(lblAlgnRight,"right_just.gif","rteCommand('"+rte+"','justifyright')");
348                   addCommand("justifyfull","rteCommand('"+rte+"','justifyfull')");
349 //                insertImg(lblJustifyFull,"justifyfull.gif","rteCommand('"+rte+"','justifyfull')");
350                   if(!isSafari && !isKonqueror) {
351                         insertSep();
352                           addCommand("numbered_list","rteCommand('"+rte+"','insertorderedlist')");
353 //                        insertImg(lblOL,"numbered_list.gif","rteCommand('"+rte+"','insertorderedlist')");
354                           addCommand("list","rteCommand('"+rte+"','insertunorderedlist')");
355 //                        insertImg(lblUL,"list.gif","rteCommand('"+rte+"','insertunorderedlist')");
356                           addCommand("outdent","rteCommand('"+rte+"','outdent')");
357 //                        insertImg(lblOutdent,"outdent.gif","rteCommand('"+rte+"','outdent')");
358                           addCommand("indent","rteCommand('"+rte+"','indent')");
359 //                        insertImg(lblIndent,"indent.gif","rteCommand('"+rte+"','indent')");
360                           insertSep();
361                           addCommand("textcolor","dlgColorPalette('"+rte+"','forecolor')","forecolor_"+rte);
362 //                        insertImg(lblTextColor,"textcolor.gif","dlgColorPalette('"+rte+"','forecolor')","forecolor_"+rte);
363                           addCommand("bgcolor","dlgColorPalette('"+rte+"','hilitecolor')","hilitecolor_"+rte);
364 //                        insertImg(lblBgColor,"bgcolor.gif","dlgColorPalette('"+rte+"','hilitecolor')","hilitecolor_"+rte);
365                           insertSep();
366                           addCommand("hr","rteCommand('"+rte+"','inserthorizontalrule')");
367 //                        insertImg(lblHR,"hr.gif","rteCommand('"+rte+"','inserthorizontalrule')");
368                           insertSep();
369                           addCommand("special_char","dlgLaunch('"+rte+"','char')");
370 //                        insertImg(lblInsertChar,"special_char.gif","dlgLaunch('"+rte+"','char')");
371                           addCommand("hyperlink","dlgLaunch('"+rte+"','link')");
372 //                        insertImg(lblInsertLink,"hyperlink.gif","dlgLaunch('"+rte+"','link')");
373                           addCommand("image","dlgLaunch('"+rte+"','image')");
374 //                        insertImg(lblAddImage,"image.gif","dlgLaunch('"+rte+"','image')");
375                           addCommand("insert_table","dlgLaunch('"+rte+"','table')");
376 //                        insertImg(lblInsertTable,"insert_table.gif","dlgLaunch('"+rte+"','table')");
377                   }
378                   insertSep();
379                   if(!isSafari && !isKonqueror) {
380                           addCommand("replace","dlgLaunch('"+rte+"','replace')");
381 //                        insertImg(lblSearch,"replace.gif","dlgLaunch('"+rte+"','replace')");
382                   }
383                   addCommand("word_count","countWords('"+rte+"')");
384 //                insertImg(lblWordCount,"word_count.gif","countWords('"+rte+"')");
385                   if(isIE) {
386                           addCommand("spellcheck","checkspell()");
387 //                        insertImg(lblSpellCheck,"spellcheck.gif","checkspell()");
388                   }
389                   document.writeln('<td width="100%"></td></tr></tbody></table>');
390           }
391           document.writeln('<iframe id="'+rte+'" width="' + (tablewidth - 2) + 'px" height="' + height + 'px" frameborder=0 style="border: 1px solid #d2d2d2" src="' + includesPath + 'blank.htm" onfocus="dlgCleanUp();"></iframe>');
392           if(!readOnly){
393                   document.writeln('<table id="vs'+rte+'" name="vs'+rte+'" class="rteBk" cellpadding=0 cellspacing=0 border=0 width="' + tablewidth + '"><tr>');
394                   document.writeln('<td onclick="toggleHTMLSrc(\''+rte+'\', ' + buttons + ');" nowrap><img class="rteBar" src="'+imagesPath+'bar.gif" alt="" align=absmiddle><span id="imgSrc'+rte+'"><img src="'+imagesPath+'code.gif" alt="" title="" style="margin:1px;" align=absmiddle></span><span id="_xtSrc'+rte+'" style="font-family:tahoma,sans-serif;font-size:12px;color:#0000ff;CURSOR: default;">'+lblModeHTML+'</span></td>');
395                   document.writeln('<td width="100%" nowrap>&nbsp;</td></tr></table>');
396           }
397           document.writeln('<iframe width="142" height="98" id="cp'+rte+'" src="' + includesPath + 'palette.htm" scrolling="no" frameborder=0 style="margin:0;border:0;visibility:hidden;position:absolute;border:1px solid #cdcdcd;top:-1000px;left:-1000px"></iframe>');
398           document.writeln('<input type="hidden" id="hdn'+rte+'" name="'+rte+'" value="" style="position: absolute;left:-1000px;top:-1000px;">');
399           if(!fullscreen){
400                   document.writeln('<input type="hidden" id="size'+rte+'" name="size'+rte+'" value="'+height+'" style="position: absolute;left:-1000px;top:-1000px;">');
401           }
402           document.writeln('</span>');
403           document.getElementById('hdn'+rte).value = html;
404     enableDesignMode(rte, html, rte_css, readOnly);
405         } else {
406                 buttons = false;
407                 if(fullscreen && height > 90) {
408                         height = (height - 75);tablewidth=tablewidth-30;
409                 }
410                 // CM non-designMode() UI
411                 html = parseBreaks(html);
412                 document.writeln('<div style="font:12px Verdana, Arial, Helvetica, sans-serif;width: ' + tablewidth + 'px;padding:15px;">');
413                 if(!readOnly) {
414                         document.writeln('<div style="color:gray">'+lblnon_designMode+'</div><br>');
415                         document.writeln('<input type="radio" name="' + rte + '_autobr" value="1" checked="checked" onclick="autoBRon(\'' + rte + '\');" /> '+lblAutoBR+'<input type="radio" name="' + rte + '_autobr" value="0" onclick="autoBRoff(\'' + rte + '\');" />'+lblRawHTML+'<br>');
416                         document.writeln('<textarea name="'+rte+'" id="'+rte+'" style="width: ' + tablewidth + 'px; height: ' + height + 'px;">' + html + '</textarea>');
417                 } else {
418                         document.writeln('<textarea name="'+rte+'" id="'+rte+'" style="width: ' + tablewidth + 'px; height: ' + height + 'px;" readonly=readonly>' + html + '</textarea>');
419                 }
420                 if(fullscreen) document.writeln('<br><input type="submit" value="'+btnText+'" />');
421                 document.writeln('</div>');
422         }
423 }
424
425 function insertBar() {
426         document.writeln('<td><img class="rteBar" src="'+imagesPath+'bar.gif" alt=""></td>');
427         nbcommand = 0;
428 }
429 function insertSep() {
430         if (nbcommand > 0) {
431           document.writeln('<td><img class="rteSep" src="'+imagesPath+'blackdot.gif" alt=""></td>');
432           nbcommand = 0;
433         }
434 }
435 function insertImg(name, image, command, id){
436           var td = "<td>";
437           if(id!=null){
438             td = "<td id='"+id+"'>";
439       }
440           document.writeln(td+'<img class="rteImg" src="'+imagesPath+image+'" alt="'+name+'" title="'+name+'" onMouseDown="'+command+';return false" onmouseover="this.className=\'rteImgUp\'" onmouseout="this.className=\'rteImg\'" onmousedown="this.className=\'rteImgDn\'" onmouseup="this.className=\'rteImgUp\'"></td>');
441 }
442
443 function enableDesignMode(rte, html, css, readOnly) {
444         var frameHtml = "<html dir='" + lang_direction + "' lang='" + lang + "' id='" + rte + "'>\n<head>\n";
445         frameHtml += "<meta http-equiv='Content-Type' content='text/html; charset=" + encoding + "'>\n";
446         frameHtml += "<meta http-equiv='Content-Language' content='" + lang + "'>\n";
447         //to reference your stylesheet, set href property below to your stylesheet path and uncomment
448         if(css.length > 0) {
449                 frameHtml += "<link media=\"all\" type=\"text/css\" href=\"" + css + "\" rel=\"stylesheet\">\n";
450         } else {
451                 frameHtml += "<style>@charset \"utf-8\"; body {background:#FFFFFF;margin:8px;padding:0px;font-family:"+defaultFont+";font-size:"+defaultSize+";}</style>\n";
452         }
453         frameHtml += "</head><body>\n"+html+"\n</body></html>";
454         if(!isSafari && !isKonqueror) var oRTE = returnRTE(rte).document;
455         if (document.all) {
456                 if(isSafari || isKonqueror) var oRTE = frames[rte].document;
457                 oRTE.open("text/html","replace");
458                 oRTE.write(frameHtml);
459                 oRTE.close();
460                 if (!readOnly) {
461                         oRTE.designMode = "On";
462                 }
463         } else {
464                 try {
465                         // Commented out the following line to confront a bug when loading multiple RTEs on one page in a MOZ browser
466                         // Fix provided by "Kings". Safari may have problems with this snytax - unable to test because I don't own a MAC.(Tim Bell)
467                         //
468                         // if(!readOnly) document.getElementById(rte).contentDocument.designMode = "on";
469                         if(!readOnly && !isKonqueror && !isSafari) {
470                                 addLoadEvent(function() { document.getElementById(rte).contentDocument.designMode = "on"; });
471                         } else if(!readOnly) {
472                                 if (!readOnly) document.getElementById(rte).contentDocument.designMode = "on";
473                         }
474                         try {
475                                 
476                                 if(isSafari || isKonqueror) var oRTE = document.getElementById(rte).contentWindow.document;
477                                 oRTE.open("text/html","replace");
478                                 oRTE.write(frameHtml);
479                                 oRTE.close();
480                                 if(isGecko && !readOnly) {
481                                         //attach a keyboard handler for gecko browsers to make keyboard shortcuts work
482                                         oRTE.addEventListener("keypress", geckoKeyPress, true);
483                                         oRTE.addEventListener("focus", function (){dlgCleanUp();}, false);
484                                 }
485                         } catch(e) {
486                                 alert(lblErrorPreload);
487                         }
488                 } catch(e) {
489                         //gecko may take some time to enable design mode.
490                         //Keep looping until able to set.
491                         if(isGecko){
492                                 setTimeout("enableDesignMode('"+rte+"', '"+html+"', '"+css+"', "+readOnly+");", 200);
493                         }else{
494                                 return false;
495                         }
496                 }
497         }       
498         setTimeout('showGuidelines("'+rte+'")',300);
499 }
500
501 function addLoadEvent(func) {
502         var oldonload = window.onload;
503         if (typeof window.onload != 'function') {
504                 window.onload = func;
505         } else {
506                 window.onload = function() {
507                         oldonload();
508                         func();
509                 };
510         }
511 }
512
513 function returnRTE(rte) {
514         var rtn;
515         if(document.all){
516                 rtn = frames[rte];
517         } else {
518                 rtn = document.getElementById(rte).contentWindow;
519         }       
520         return rtn;
521 }
522
523 function updateRTE(rte) {
524         if(isRichText) {
525           dlgCleanUp(); //      Closes Pop-ups
526           stripGuidelines(rte); // Removes Table Guidelines
527   }
528         parseRTE(rte);
529 }
530
531 function updateRTEs(){
532         var vRTEs = allRTEs.split(";");
533         for(var i=0; i<vRTEs.length; i++){
534                 updateRTE(vRTEs[i]);
535         }
536 }
537
538 function parseRTE(rte) {
539         if (!isRichText) {
540                  autoBRoff(rte); // sorts out autoBR
541                  return false;
542         }
543         //check for readOnly mode
544         var readOnly = false;
545         var oRTE = returnRTE(rte);
546   if(document.all){
547                 if(oRTE.document.designMode != "On"){
548       readOnly = true;
549     }
550         }else{
551                 if(oRTE.document.designMode != "on"){
552       readOnly = true;
553     }
554         }
555         if(isRichText && !readOnly){
556                 //if viewing source, switch back to design view
557                 if(document.getElementById("_xtSrc"+rte).innerHTML == lblModeRichText){
558                          if(document.getElementById("Buttons1_"+rte)){
559                                  toggleHTMLSrc(rte, true);
560                          }else{
561          toggleHTMLSrc(rte, false);
562                          }
563                  stripGuidelines(rte);
564                 }
565                 setHiddenVal(rte);
566         }
567 }
568
569 function setHiddenVal(rte){
570         //set hidden form field value for current rte
571         var oHdnField = document.getElementById('hdn'+rte);
572         //convert html output to xhtml (thanks Timothy Bell and Vyacheslav Smolin!)
573         if(oHdnField.value==null){
574     oHdnField.value = "";
575         }
576   var sRTE = returnRTE(rte).document.body;
577         if(generateXHTML){
578           try{
579                   oHdnField.value = getXHTML(sRTE.innerHTML);
580           }catch(e){
581       oHdnField.value = sRTE.innerHTML;
582     }
583         }else{
584           oHdnField.value = sRTE.innerHTML;
585         }
586         // fix to replace special characters added here: 
587   oHdnField.value = replaceSpecialChars(oHdnField.value); 
588   //if there is no content (other than formatting) set value to nothing
589         if(stripHTML(oHdnField.value.replace("&nbsp;", " ")) == "" &&
590                 oHdnField.value.toLowerCase().search("<hr") == -1 &&
591                 oHdnField.value.toLowerCase().search("<img") == -1){
592     oHdnField.value = "";
593   }
594 }
595
596 function rteCommand(rte, command, option){
597         dlgCleanUp();
598   //function to perform command
599         var oRTE = returnRTE(rte);
600         try{
601                 oRTE.focus();
602                 oRTE.document.execCommand(command, false, option);
603                 oRTE.focus();
604         }catch(e){
605 //              alert(e);
606 //              setTimeout("rteCommand('" + rte + "', '" + command + "', '" + option + "');", 10);
607         }
608 }
609
610 function toggleHTMLSrc(rte, buttons){
611         dlgCleanUp();
612   //contributed by Bob Hutzel (thanks Bob!)
613   var cRTE = document.getElementById(rte);
614   var hRTE = document.getElementById('hdn'+rte);
615   var sRTE = document.getElementById("size"+rte);
616   var tRTE = document.getElementById("_xtSrc"+rte);
617   var iRTE = document.getElementById("imgSrc"+rte);
618   var oRTE = returnRTE(rte).document;
619   var htmlSrc;
620         if(sRTE){
621           obj_height = parseInt(sRTE.value);
622   }else{
623     findSize(rte);
624         }
625         if(tRTE.innerHTML == lblModeHTML){
626                 //we are checking the box
627                 tRTE.innerHTML = lblModeRichText;
628                 stripGuidelines(rte);
629                 if(buttons){
630                         showHideElement("Buttons1_" + rte, "hide", true);
631                         if(document.getElementById("Buttons2_"+rte)){
632                                 showHideElement("Buttons2_" + rte, "hide", true);
633                           cRTE.style.height = obj_height+56;
634                         }else{
635                                 cRTE.style.height = obj_height+28;
636                         }
637                 }
638                 setHiddenVal(rte);
639         if(document.all){
640                 oRTE.body.innerText = hRTE.value;
641                 }else{
642                 htmlSrc = oRTE.createTextNode(hRTE.value);
643                         oRTE.body.innerHTML = "";
644                         oRTE.body.appendChild(htmlSrc);
645                 }
646                 iRTE.innerHTML = '<img src="'+imagesPath+'design.gif" alt="Switch Mode" style="margin:1px;" align=absmiddle>';
647         }else{
648                 //we are unchecking the box
649                 obj_height = parseInt(cRTE.style.height);
650                 tRTE.innerHTML = lblModeHTML;
651                 if(buttons){
652                         showHideElement("Buttons1_" + rte, "show", true);
653                   if(document.getElementById("Buttons2_"+rte)){
654         showHideElement("Buttons2_" + rte, "show", true);
655                           cRTE.style.height = obj_height-56;
656                         }else{
657         cRTE.style.height = obj_height-28;
658       }
659                 }
660                 if(document.all){
661                         //fix for IE
662                         var output = escape(oRTE.body.innerText);
663                         output = output.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
664                         output = output.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");
665                         oRTE.body.innerHTML = unescape(output);
666                         // Disabled due to flaw in the regular expressions, this fix 
667       // does not work with the revamped's enhanced insert link dialog window.
668       //
669       // Prevent links from changing to absolute paths
670       if(!keep_absolute){
671         var tagfix = unescape(output).match(/<a[^>]*href=(['"])([^\1>]*)\1[^>]*>/ig); 
672         var coll = oRTE.body.all.tags('A'); 
673         for(i=0; i<coll.length; i++){ 
674           // the 2 alerts below show when we hinder the links from becoming absolute                    
675           //alert(tagfix[i]); 
676           coll[i].href = tagfix[i].replace(/.*href=(['"])([^\1]*)\1.*/i,"$2"); 
677           //alert(RegExp.$1 + " " + RegExp.$2 + " " + RegExp.$3); 
678         } 
679         var imgfix = unescape(output).match(/<img[^>]*src=['"][^'"]*['"][^>]*>/ig); 
680         var coll2 = oRTE.body.all.tags('IMG'); 
681         for(i=0; i<coll2.length; i++){ 
682           coll2[i].src = imgfix[i].replace(/.*src=['"]([^'"]*)['"].*/i,"$1"); 
683         } 
684       }
685       //end path fix                    
686                 }else{
687                 htmlSrc = oRTE.body.ownerDocument.createRange();
688                         htmlSrc.selectNodeContents(oRTE.body);
689                         oRTE.body.innerHTML = htmlSrc.toString();
690                 }
691                 oRTE.body.innerHTML = replaceSpecialChars(oRTE.body.innerHTML);
692                 showGuidelines(rte);
693                 // (IE Only)This prevents an undo operation from displaying a pervious HTML mode
694                 // This resets the undo/redo buffer.
695                 if(document.all){
696                         parseRTE(rte);
697           }
698           iRTE.innerHTML = '<img src="'+imagesPath+'code.gif" alt="Switch Mode" style="margin:1px;" align=absmiddle>';
699         }
700 }
701
702 function toggleSelection(rte) {
703   var rng = setRange(rte);
704   var oRTE = returnRTE(rte).document;
705         var length1;
706   var length2;
707         if(document.all){
708                 length1 = rng.text.length;
709                 var output = escape(oRTE.body.innerText);
710                 output = output.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
711                 output = output.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");
712                 length2 = unescape(output).length;
713         }else{
714                 length1 = rng.toString().length;
715                 var htmlSrc = oRTE.body.ownerDocument.createRange();
716                 htmlSrc.selectNodeContents(oRTE.body);
717           length2 = htmlSrc.toString().length;
718         }
719         if(length1 < length2){
720           rteCommand(rte,'selectall','');
721         } else {
722                 if(!document.all){
723                   oRTE.designMode = "off";
724                   oRTE.designMode = "on";
725                 }else{
726                   rteCommand(rte,'unselect','');
727                 }
728         }
729 }
730
731 function dlgColorPalette(rte, command) {
732         //function to display or hide color palettes
733         setRange(rte);
734         //get dialog position
735         var oDialog = document.getElementById('cp' + rte);
736         var buttonElement = document.getElementById(command+"_"+rte);
737         var iLeftPos = buttonElement.offsetLeft+5; 
738   var iTopPos = buttonElement.offsetTop+53;
739   if (!document.getElementById('Buttons2_'+rte)){
740     iTopPos = iTopPos-28;
741         }
742   oDialog.style.left = iLeftPos + "px";
743         oDialog.style.top = iTopPos + "px";
744         
745   if((command == parent.command)&&(rte == currentRTE)){
746                 //if current command dialog is currently open, close it
747                 if(oDialog.style.visibility == "hidden"){
748                         showHideElement(oDialog, 'show', false);
749                 }else{
750                         showHideElement(oDialog, 'hide', false);
751                 }
752         }else{
753                 //if opening a new dialog, close all others
754                 var vRTEs = allRTEs.split(";");
755                 for(var i = 0; i<vRTEs.length; i++){
756                         showHideElement('cp' + vRTEs[i], 'hide', false);
757                 }
758                 showHideElement(oDialog, 'show', false);
759         }
760         //save current values
761         currentRTE = rte;
762         parent.command = command;
763 }
764
765 function dlgLaunch(rte, command) {
766         var selectedText = '';
767   //save current values
768         parent.command = command;
769         currentRTE = rte;
770         switch(command){
771   case "char":
772     InsertChar = popUpWin(includesPath+'insert_char.htm', 'InsertChar', 50, 50, 'status=yes,');
773     break;
774   case "table":
775     InsertTable = popUpWin(includesPath + 'insert_table.htm', 'InsertTable', 50, 50, 'status=yes,');
776     break;
777   case "image":
778     setRange(rte);
779     parseRTE(rte);
780     InsertImg = popUpWin(includesPath + 'insert_img.htm','AddImage', 50, 50, 'status=yes,');
781     break;
782   case "link":
783     selectedText = getText(rte);
784     InsertLink = popUpWin(includesPath + 'insert_link.htm', 'InsertLink', 50, 50, 'status=yes,');
785     setFormText("0", selectedText);
786     break;
787   case "replace":
788     selectedText = getText(rte);
789     dlgReplace = popUpWin(includesPath + 'replace.htm', 'dlgReplace', 50, 50, 'status=yes,');
790     setFormText("1", selectedText);
791     break;
792   case "text":
793     dlgPasteText = popUpWin(includesPath + 'paste_text.htm', 'dlgPasteText', 50, 50, 'status=yes,');
794     break;
795   case "word":
796     dlgPasteWord = popUpWin(includesPath + 'paste_word.htm', 'dlgPasteWord', 50, 50, 'status=yes,');
797     break;
798   }
799 }
800
801 function getText(rte) {
802         //get currently highlighted text and set link text value
803   setRange(rte);
804         var rtn = '';
805         if (isIE) {
806           rtn = stripHTML(rng.htmlText);
807         } else {
808           rtn = stripHTML(rng.toString());
809         }
810         parseRTE(rte);
811         if(document.all){
812     rtn = rtn.replace("'","\\\\\\'");
813   }else{
814     rtn = rtn.replace("'","\\'");
815   }
816   return rtn;
817 }
818
819 function setFormText(popup, content){
820         //set link text value in dialog windows
821         if(content != "undefined")
822         {
823           try{
824                   switch(popup){
825                     case "0": InsertLink.document.getElementById("linkText").value = content; break;
826                     case "1": dlgReplace.document.getElementById("searchText").value = content; break;
827                   }
828           }catch(e){
829                   //may take some time to create dialog window.
830                   //Keep looping until able to set.
831                   setTimeout("setFormText('"+popup+"','" + content + "');", 10);
832           }
833         }
834 }
835
836 function dlgCleanUp(){
837         var vRTEs = allRTEs.split(";");
838         for(var i = 0; i < vRTEs.length; i++){
839           showHideElement('cp' + vRTEs[i], 'hide', false);
840         }
841         if(InsertChar != null){
842     InsertChar.close();
843     InsertChar=null;
844         }
845   if(InsertTable != null){
846     InsertTable.close();
847     InsertTable=null;
848         }
849   if(InsertLink != null){
850     InsertLink.close();
851     InsertLink=null;
852         }
853   if(InsertImg != null){
854     InsertImg.close();
855     InsertImg=null;
856         }
857   if(dlgReplace != null){
858     dlgReplace.close();
859     dlgReplace=null;
860         }
861   if(dlgPasteText != null){
862     dlgPasteText.close();
863     dlgPasteText=null;
864         }
865   if(dlgPasteWord != null){
866     dlgPasteWord.close();
867     dlgPasteWord=null;
868   }
869 }
870
871 function popUpWin (url, win, width, height, options) {
872   dlgCleanUp();
873         var leftPos = (screen.availWidth - width) / 2;
874         var topPos = (screen.availHeight - height) / 2;
875         options += 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
876         return window.open(url, win, options);
877 }
878
879 function setColor(color) {
880         //function to set color
881         var rte = currentRTE;
882         var parentCommand = parent.command;
883         if(document.all){
884                 if(parentCommand == "hilitecolor"){
885       parentCommand = "backcolor";
886     }
887                 //retrieve selected range
888                 rng.select();
889         }
890         rteCommand(rte, parentCommand, color);
891         showHideElement('cp'+rte, "hide", false);
892 }
893
894 function addImage(rte) {
895   dlgCleanUp();
896         //function to add image
897         imagePath = prompt('Enter Image URL:', 'http://');                              
898         if((imagePath != null)&&(imagePath != "")){
899                 rteCommand(rte, 'InsertImage', imagePath);
900         }
901 }
902
903 function rtePrint(rte) {
904   dlgCleanUp();
905         if(isIE){
906           document.getElementById(rte).contentWindow.document.execCommand('Print');
907   }else{
908           document.getElementById(rte).contentWindow.print();
909         }
910 }
911
912 function selectFont(rte, selectname){
913         //function to handle font changes
914         var idx = document.getElementById(selectname).selectedIndex;
915         // First one is always a label
916         if(idx != 0){
917                 var selected = document.getElementById(selectname).options[idx].value;
918                 var cmd = selectname.replace('_'+rte, '');
919                 rteCommand(rte, cmd, selected);
920                 document.getElementById(selectname).selectedIndex = 0;
921         }
922 }
923
924 function insertHTML(html){
925         //function to add HTML -- thanks dannyuk1982
926         var rte = currentRTE;
927         var oRTE = returnRTE(rte);
928         oRTE.focus();
929         if(document.all){
930                 var oRng = oRTE.document.selection.createRange();
931                 oRng.pasteHTML(html);
932                 oRng.collapse(false);
933                 oRng.select();
934         }else{
935                 oRTE.document.execCommand('insertHTML', false, html);
936         }
937 }
938
939 function replaceHTML(tmpContent, searchFor, replaceWith) {
940   var runCount = 0;
941         var intBefore = 0;
942         var intAfter = 0;
943         var tmpOutput = "";
944         while(tmpContent.toUpperCase().indexOf(searchFor.toUpperCase()) > -1) {
945     runCount = runCount+1;
946     // Get all content before the match
947     intBefore = tmpContent.toUpperCase().indexOf(searchFor.toUpperCase());
948     tmpBefore = tmpContent.substring(0, intBefore);
949     tmpOutput = tmpOutput + tmpBefore;
950     // Get the string to replace
951     tmpOutput = tmpOutput + replaceWith;
952     // Get the rest of the content after the match until
953     // the next match or the end of the content
954     intAfter = tmpContent.length - searchFor.length + 1;
955     tmpContent = tmpContent.substring(intBefore + searchFor.length);
956   }
957   return runCount+"|^|"+tmpOutput+tmpContent;
958 }
959
960 function replaceSpecialChars(html){ 
961    var specials = new Array("&cent;","&euro;","&pound;","&curren;","&yen;","&copy;","&reg;","&trade;","&divide;","&times;","&plusmn;","&frac14;","&frac12;","&frac34;","&deg;","&sup1;","&sup2;","&sup3;","&micro;","&laquo;","&raquo;","&lsquo;","&rsquo;","&lsaquo;","&rsaquo;","&sbquo;","&bdquo;","&ldquo;","&rdquo;","&iexcl;","&brvbar;","&sect;","&not;","&macr;","&para;","&middot;","&cedil;","&iquest;","&fnof;","&mdash;","&ndash;","&bull;","&hellip;","&permil;","&ordf;","&ordm;","&szlig;","&dagger;","&Dagger;","&eth;","&ETH;","&oslash;","&Oslash;","&thorn;","&THORN;","&oelig;","&OElig;","&scaron;","&Scaron;","&acute;","&circ;","&tilde;","&uml;","&agrave;","&aacute;","&acirc;","&atilde;","&auml;","&aring;","&aelig;","&Agrave;","&Aacute;","&Acirc;","&Atilde;","&Auml;","&Aring;","&AElig;","&ccedil;","&Ccedil;","&egrave;","&eacute;","&ecirc;","&euml;","&Egrave;","&Eacute;","&Ecirc;","&Euml;","&igrave;","&iacute;","&icirc;","&iuml;","&Igrave;","&Iacute;","&Icirc;","&Iuml;","&ntilde;","&Ntilde;","&ograve;","&oacute;","&ocirc;","&otilde;","&ouml;","&Ograve;","&Oacute;","&Ocirc;","&Otilde;","&Ouml;","&ugrave;","&uacute;","&ucirc;","&uuml;","&Ugrave;","&Uacute;","&Ucirc;","&Uuml;","&yacute;","&yuml;","&Yacute;","&Yuml;"); 
962    var unicodes = new Array("\u00a2","\u20ac","\u00a3","\u00a4","\u00a5","\u00a9","\u00ae","\u2122","\u00f7","\u00d7","\u00b1","\u00bc","\u00bd","\u00be","\u00b0","\u00b9","\u00b2","\u00b3","\u00b5","\u00ab","\u00bb","\u2018","\u2019","\u2039","\u203a","\u201a","\u201e","\u201c","\u201d","\u00a1","\u00a6","\u00a7","\u00ac","\u00af","\u00b6","\u00b7","\u00b8","\u00bf","\u0192","\u2014","\u2013","\u2022","\u2026","\u2030","\u00aa","\u00ba","\u00df","\u2020","\u2021","\u00f0","\u00d0","\u00f8","\u00d8","\u00fe","\u00de","\u0153","\u0152","\u0161","\u0160","\u00b4","\u02c6","\u02dc","\u00a8","\u00e0","\u00e1","\u00e2","\u00e3","\u00e4","\u00e5","\u00e6","\u00c0","\u00c1","\u00c2","\u00c3","\u00c4","\u00c5","\u00c6","\u00e7","\u00c7","\u00e8","\u00e9","\u00ea","\u00eb","\u00c8","\u00c9","\u00ca","\u00cb","\u00ec","\u00ed","\u00ee","\u00ef","\u00cc","\u00cd","\u00ce","\u00cf","\u00f1","\u00d1","\u00f2","\u00f3","\u00f4","\u00f5","\u00f6","\u00d2","\u00d3","\u00d4","\u00d5","\u00d6","\u00f9","\u00fa","\u00fb","\u00fc","\u00d9","\u00da","\u00db","\u00dc","\u00fd","\u00ff","\u00dd","\u0178"); 
963    for(var i=0; i<specials.length; i++){ 
964       html = replaceIt(html,unicodes[i],specials[i]); 
965    } 
966    return html; 
967 }
968
969 function SearchAndReplace(searchFor, replaceWith, matchCase, wholeWord) {
970    var cfrmMsg = lblSearchConfirm.replace("SF",searchFor).replace("RW",replaceWith);
971    var rte = currentRTE;
972    stripGuidelines(rte);
973    var oRTE = returnRTE(rte);
974    var tmpContent = oRTE.document.body.innerHTML.replace("'", "\'").replace('"', '\"');
975    var strRegex;
976    if (matchCase && wholeWord) {
977       strRegex = "/(?!<[^>]*)(\\b(" + searchFor + ")\\b)(?![^<]*>)/g";
978    }
979    else if (matchCase) {
980       strRegex = "/(?!<[^>]*)(" + searchFor + ")(?![^<]*>)/g";
981    }
982    else if (wholeWord) {
983       strRegex = "/(?!<[^>]*)(\\b(" + searchFor + ")\\b)(?![^<]*>)/gi";
984    } else {
985       strRegex = "/(?!<[^>]*)(" + searchFor + ")(?![^<]*>)/gi";
986    }
987    var cmpRegex=eval(strRegex);
988    var runCount = 0;
989    var tmpNext = tmpContent;
990    var intFound = tmpNext.search(cmpRegex);
991    while(intFound > -1) {
992       runCount = runCount+1;
993       tmpNext = tmpNext.substr(intFound + searchFor.length);
994       intFound = tmpNext.search(cmpRegex);
995    }
996    if (runCount > 0) {
997       cfrmMsg = cfrmMsg.replace("[RUNCOUNT]",runCount);
998       if(confirm(cfrmMsg)) {
999          tmpContent=tmpContent.replace(cmpRegex,replaceWith);
1000          oRTE.document.body.innerHTML = tmpContent.replace("\'", "'").replace('\"', '"');
1001       } else {
1002        alert(lblSearchAbort);
1003     }
1004       showGuidelines(rte);
1005    }
1006    else {
1007      showGuidelines(rte);
1008       alert("["+searchFor+"] "+lblSearchNotFound);
1009    }
1010 }
1011                 
1012 function showHideElement(element, showHide, rePosition){
1013         //function to show or hide elements
1014         //element variable can be string or object
1015         if(document.getElementById(element)){
1016                 element = document.getElementById(element);
1017         }
1018         if(showHide == "show"){
1019                 element.style.visibility = "visible";
1020           if(rePosition){
1021                   element.style.position = "relative";
1022             element.style.left = "auto";
1023             element.style.top = "auto";
1024     }
1025         }else if(showHide == "hide"){
1026                 element.style.visibility = "hidden";
1027     if(rePosition){
1028                         element.style.position = "absolute";
1029         element.style.left = "-1000px";
1030         element.style.top = "-1000px";
1031           }
1032   }
1033 }
1034
1035 function setRange(rte){
1036         //function to store range of current selection
1037         var oRTE = returnRTE(rte);
1038         var selection;
1039         if(document.all){
1040                 selection = oRTE.document.selection;
1041                 if(selection != null){
1042       rng = selection.createRange();
1043     }
1044         }else{
1045                 selection = oRTE.getSelection();
1046                 rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
1047         }
1048         return rng;
1049 }
1050
1051 function stripHTML(strU) {
1052         //strip all html
1053         var strN = strU.replace(/(<([^>]+)>)/ig,"");
1054         //replace carriage returns and line feeds
1055   strN = strN.replace(/\r\n/g," ");
1056   strN = strN.replace(/\n/g," ");
1057   strN = strN.replace(/\r/g," ");
1058         strN = trim(strN);
1059         return strN;
1060 }
1061
1062 function trim(inputString) {
1063   if (typeof inputString != "string"){
1064     return inputString;
1065   }
1066   inputString = inputString.replace(/^\s+|\s+$/g, "").replace(/\s{2,}/g, "");
1067   return inputString;
1068 }
1069
1070 function showGuidelines(rte) { 
1071   if(rte.length == 0) rte = currentRTE; 
1072   var oRTE = returnRTE(rte); 
1073   var tables = oRTE.document.getElementsByTagName("table"); 
1074   var sty = "dashed 1px "+zeroBorder;
1075   for(var i=0; i<tables.length; i++){
1076     if(tables[i].getAttribute("border") == 0){
1077       if(document.all){
1078         var trs = tables[i].getElementsByTagName("tr");
1079         for(var j=0; j<trs.length; j++){
1080           var tds = trs[j].getElementsByTagName("td");
1081           for(var k=0; k<tds.length; k++){
1082                                   if(j == 0 && k == 0){
1083                                    tds[k].style.border = sty;
1084                                           }else if(j == 0 && k != 0){
1085                       tds[k].style.borderBottom = sty;
1086                                                   tds[k].style.borderTop = sty;
1087                                                   tds[k].style.borderRight = sty;
1088                   }else if(j != 0 && k == 0) {
1089                                 tds[k].style.borderBottom = sty;
1090                                             tds[k].style.borderLeft = sty;
1091                                                   tds[k].style.borderRight = sty;
1092             }else if(j != 0 && k != 0) {
1093               tds[k].style.borderBottom = sty;
1094                                             tds[k].style.borderRight = sty;
1095                         }
1096           }
1097         }  
1098       }else{  
1099         tables[i].removeAttribute("border"); 
1100         tables[i].setAttribute("style","border: " + sty); 
1101         tables[i].setAttribute("rules", "all");
1102       } 
1103     } 
1104   } 
1105
1106
1107 function stripGuidelines(rte) { 
1108   var oRTE = returnRTE(rte); 
1109   var tbls = oRTE.document.getElementsByTagName("table"); 
1110   for(var j=0; j<tbls.length; j++) { 
1111     if(tbls[j].getAttribute("border") == 0 || tbls[j].getAttribute("border") == null){
1112       if(document.all) {
1113         var tds = tbls[j].getElementsByTagName("td");
1114         for(var k=0; k<tds.length; k++) {
1115           tds[k].removeAttribute("style");
1116         }
1117       } else { 
1118         tbls[j].removeAttribute("style"); 
1119         tbls[j].removeAttribute("rules"); 
1120         tbls[j].setAttribute("border","0");
1121       } 
1122     } 
1123   } 
1124 }
1125
1126 function findSize(obj) {
1127   if(obj.length > 0 && document.all) {
1128           obj = frames[obj];
1129   } else if(obj.length > 0 && !document.all) {
1130           obj = document.getElementById(obj).contentWindow;
1131   } else {
1132     obj = this;
1133         }
1134         if ( typeof( obj.window.innerWidth ) == 'number' ) {
1135     //Non-IE
1136     obj_width = obj.window.innerWidth;
1137     obj_height = obj.window.innerHeight;
1138   } else if( obj.document.documentElement && ( obj.document.documentElement.clientWidth || obj.document.documentElement.clientHeight ) ) {
1139     //IE 6+ in 'standards compliant mode'
1140     obj_width = document.documentElement.clientWidth;
1141     obj_height = document.documentElement.clientHeight;
1142   } else if( obj.document.body && ( obj.document.body.clientWidth || obj.document.body.clientHeight ) ) {
1143     //IE 4 compatible
1144     obj_width = obj.document.body.clientWidth;
1145     obj_height = obj.document.body.clientHeight;
1146   }
1147 }
1148
1149 function resizeRTE() {
1150   document.body.style.overflow = "hidden";
1151         var rte = currentRTE;
1152   var oRTE = document.getElementById(rte);
1153   var oBut1 = document.getElementById('Buttons1_'+rte);
1154   var oBut2;
1155   var oVS = document.getElementById('vs'+rte);
1156   findSize("");
1157   width = obj_width;
1158   if (width < minWidth){
1159                 document.body.style.overflow = "auto";
1160                 width = minWidth;
1161         }
1162   var height = obj_height - 83;
1163         if (document.getElementById("_xtSrc"+rte).innerHTML == lblModeRichText){
1164           height = obj_height-28;
1165                 if (!document.getElementById('Buttons2_'+rte) && width < wrapWidth) {
1166             document.body.style.overflow = "auto";
1167                         width = wrapWidth;
1168                 }
1169                 if (document.getElementById('Buttons2_'+rte)){
1170       document.getElementById('Buttons2_'+rte).style.width = width;
1171     }
1172   } else {
1173     if (document.getElementById('Buttons2_'+rte)) {
1174                   document.getElementById('Buttons2_'+rte).style.width = width;
1175     } else {
1176                         height = obj_height - 55;
1177                         if(width < wrapWidth){
1178                           document.body.style.overflow = "auto";
1179                     width = wrapWidth;
1180                         }
1181     }
1182   }
1183   if(document.body.style.overflow == "auto" && isIE){
1184     height = height-18;
1185   } 
1186   if(document.body.style.overflow == "auto" && !isIE){
1187     height = height-24;
1188   }
1189   oBut1.style.width = width;
1190   oVS.style.width = width;
1191         oRTE.style.width = width-2;
1192   oRTE.style.height = height;
1193   if(!document.all){
1194     oRTE.contentDocument.designMode = "on";
1195   }
1196 }
1197
1198 function replaceIt(string,text,by) {
1199   // CM 19/10/04 custom replace function
1200   var strLength = string.length, _xtLength = text.length;
1201   if ((strLength == 0) || (_xtLength == 0)){
1202     return string;
1203   }
1204   var i = string.indexOf(text);
1205   if ((!i) && (text != string.substring(0,_xtLength))){
1206   return string;
1207   }
1208   if(i == -1){
1209     return string;
1210   }
1211   var newstr = string.substring(0,i) + by;
1212   if(i+_xtLength < strLength){
1213     newstr += replaceIt(string.substring(i+_xtLength,strLength),text,by);
1214   }
1215   return newstr;
1216 }
1217
1218 function countWords(rte){
1219   parseRTE(rte);
1220   var words = document.getElementById("hdn"+rte).value;
1221   var str = stripHTML(words);
1222   var chars = trim(words);
1223   chars = chars.length;
1224   chars = maxchar - chars;
1225   str = str+" a ";  // word added to avoid error
1226   str = trim(str.replace(/&nbsp;/gi,' ').replace(/([\n\r\t])/g,' ').replace(/&(.*);/g,' '));
1227   var count = 0;
1228   for(x=0;x<str.length;x++){
1229     if(str.charAt(x)==" " && str.charAt(x-1)!=" "){
1230       count++;
1231     }  
1232   }
1233   if(str.charAt(str.length-1) != " "){
1234     count++;
1235   } 
1236         count = count - 1; // extra word removed
1237         var alarm = "";
1238   if(chars<0){
1239     alarm = "\n\n"+lblCountCharWarn;
1240   }
1241   alert(lblCountTotal+": "+count+ "\n\n"+lblCountChar+": "+chars+alarm);
1242 }
1243
1244 //********************
1245 // Non-designMode() Functions
1246 //********************
1247 function autoBRon(rte) {
1248   // CM 19/10/04 used for non RTE browsers to deal with auto <BR> (and clean up other muck)
1249         var oRTE = document.forms[0].elements[rte];
1250         oRTE.value=parseBreaks(oRTE.value);
1251         oRTE.value=replaceIt(oRTE.value,'&apos;','\'');
1252 }
1253
1254 function autoBRoff(rte) {
1255   // CM 19/10/04 used for non RTE browsers to deal with auto <BR> (auto carried out when the form is submitted)
1256         var oRTE = document.forms[0].elements[rte];
1257         oRTE.value=replaceIt(oRTE.value,'\n','<br />');
1258         oRTE.value=replaceIt(oRTE.value,'\'','&apos;');
1259 }
1260
1261 function parseBreaks(argIn) {
1262   // CM 19/10/04 used for non RTE browsers to deal with auto <BR> (and clean up other muck)
1263   argIn=replaceIt(argIn,'<br>','\n');
1264         argIn=replaceIt(argIn,'<BR>','\n');
1265         argIn=replaceIt(argIn,'<br/>','\n');
1266         argIn=replaceIt(argIn,'<br />','\n');
1267         argIn=replaceIt(argIn,'\t',' ');
1268         argIn=replaceIt(argIn,'\n ','\n');
1269         argIn=replaceIt(argIn,' <p>','<p>');
1270         argIn=replaceIt(argIn,'</p><p>','\n\n');
1271         argIn=replaceIt(argIn,'&apos;','\'');
1272         argIn = trim(argIn);
1273         return argIn;
1274 }
1275
1276 //********************
1277 //Gecko-Only Functions
1278 //********************
1279 function geckoKeyPress(evt) {
1280         //function to add bold, italic, and underline shortcut commands to gecko RTEs
1281         //contributed by Anti Veeranna (thanks Anti!)
1282         var rte = evt.target.id;
1283         if (evt.ctrlKey) {
1284                 var key = String.fromCharCode(evt.charCode).toLowerCase();
1285                 var cmd = '';
1286                 switch (key) {
1287                         case 'b': cmd = "bold"; break;
1288                         case 'i': cmd = "italic"; break;
1289                         case 'u': cmd = "underline"; break;
1290                 }
1291                 if (cmd) {
1292                         rteCommand(rte, cmd, null);
1293                         // stop the event bubble
1294                         evt.preventDefault();
1295                         evt.stopPropagation();
1296                 }
1297         }
1298 }
1299
1300 //*****************
1301 //IE-Only Functions
1302 //*****************
1303 function checkspell() {
1304         dlgCleanUp();
1305         //function to perform spell check
1306         try {
1307                 var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
1308                 tmpis.CheckAllLinkedDocuments(document);
1309         }
1310         catch(exception) {
1311                 if(exception.number==-2146827859) {
1312                         if(confirm("ieSpell not detected.  Click Ok to go to download page.")){
1313         window.open("http://www.iespell.com/download.php","DownLoad");
1314       }
1315                 }else{
1316                         alert("Error Loading ieSpell: Exception " + exception.number);
1317                 }
1318         }
1319 }