// Cross-Browser Rich Text Editor  v2.0
// http://www.kevinroth.com/rte/demo.htm
// Written by Kevin Roth (kevin@NOSPAMkevinroth.com - remove NOSPAM)

//init variables
var isRichText = false;
var rng;
var currentRTE;


function initRTE() {
   //check to see if designMode mode is available
   if (document.getElementById) {
      if (document.all) {
         isRichText = true;
         //check for internet explorer 5.5+
//       if (document.URLUnencoded) isRichText = true;
      } else {
         //check for browsers that support designmode
         if (document.designMode) isRichText = true;
      }
      if (navigator.userAgent.indexOf('Mac')        >= 0) {  isRichText = false; }
      if (navigator.userAgent.indexOf('Windows CE') >= 0) {  isRichText = false; }
      if (navigator.userAgent.indexOf('Opera')      >= 0) {  isRichText = false; }
      
   }
}

function writeRichText(rte, html, width, height, buttons, stylesheet, useRte) {
   if (isRichText && useRte) {
      writeRTE(rte, html, width, height, buttons, stylesheet);
   } else {
      writeDefault(rte, html, width, height, buttons);
   }
}

function writeDefault(rte, html, width, height, buttons) {
   //updated for Simpleboard
   document.writeln('<textarea class="inputbox" name="' + rte + '" id="' + rte + '" style="width: ' + width + 'px; height: ' + height + 'px;" wrap="virtual">' + html + '</textarea>');
   document.writeln('   </td>');
   document.writeln('</tr>');
   document.writeln('<tr>');
   document.writeln('   <td width="150"><strong><?php echo _GEN_EMOTICONS;?>Emoticons</strong>: </td>');
   document.writeln('   <td>');
   document.writeln('      <table border=0 cellspacing=3 cellpadding=0>');
   document.writeln('         <tr>');
   document.writeln('<td  onClick=\"javascript:emo(\' B) \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/sun.gif" width="19" height="19" alt="B)"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' ;) \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/wink.gif" width="19" height="19" alt=";)"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :) \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/happy.gif" width="19" height="19" alt=":)"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :laugh: \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/laugh.gif" width="19" height="19" alt=":laugh:"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :ohmy: \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/ohmy.gif" width="19" height="19" alt=":ohmy:"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :x \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/sick.gif" width="19" height="19" alt=":x"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :mad: \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/angry.gif" width="19" height="19" alt=":mad:"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :blink: \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/blink.gif" width="19" height="19" alt=":blink:"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :P \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/tongue.gif" width="19" height="19" alt=":P"/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :( \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/unhappy.gif" width="19" height="19" alt=":("/></td>');
   document.writeln('<td  onClick=\"javascript:emo(\' :unsure: \');\" style=\"cursor:pointer;\"><img class="btnImage" src="components/com_simpleboard/emoticons/unsure.gif" width="19" height="19" alt=":unsure:"/></td>');
   document.writeln('            </td>');
   document.writeln('         </tr>');
   document.writeln('      </table>');
   document.writeln('   </td>');
   document.writeln('</tr>');
   document.writeln('<tr>');
   document.writeln('   <td width="150"><strong><a href=index.php?option=com_simpleboard&Itemid=56&func=faq#boardcode">boardcode</a></strong>:</td>');
   document.writeln('   <td>');
   document.writeln('      <table border=0 cellspacing=3 cellpadding=0>');
   document.writeln('         <tr>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[b]\" onClick=\"javascript:emo(\' [b] \');\" style=\"cursor:pointer;\" title=\"Open BOLD tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/b]\" onClick=\"javascript:emo(\' [/b] \');\" style=\"cursor:pointer;\" title=\"Close BOLD tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[quote]\" onClick=\"javascript:emo(\' [quote] \');\" style=\"cursor:pointer;\" title=\"Open Quote tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/quote]\" onClick=\"javascript:emo(\' [/quote] \');\" style=\"cursor:pointer;\" title=\"Close Quote tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[img]\" onClick=\"javascript:emo(\' [img=150]link-to-image \');\" style=\"cursor:pointer;\" title=\"Open image tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/img]\" onClick=\"javascript:emo(\' [/img] \');\" style=\"cursor:pointer;\" title=\"Close image tag\"></td>');
   document.writeln('          </tr><tr>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[i]\" onClick=\"javascript:emo(\' [i] \');\" style=\"cursor:pointer;\" title=\"Open Italics tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/i]\" onClick=\"javascript:emo(\' [/i] \');\" style=\"cursor:pointer;\" title=\"Close Italics tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[code]\" onClick=\"javascript:emo(\' [code] \');\" style=\"cursor:pointer;\" title=\"Open code tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/code]\" onClick=\"javascript:emo(\' [/code] \');\" style=\"cursor:pointer;\" title=\"Close code tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[url]\" onClick=\"javascript:emo(\' [url=http://]link name \');\" style=\"cursor:pointer;\" title=\"Open url tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/url]\" onClick=\"javascript:emo(\' [/url] \');\" style=\"cursor:pointer;\" title=\"Close url tag\"></td>');
   document.writeln('          </tr><tr>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[u]\" onClick=\"javascript:emo(\' [u] \');\" style=\"cursor:pointer;\" title=\"Open Underline tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/u]\" onClick=\"javascript:emo(\' [/u] \');\" style=\"cursor:pointer;\" title=\"Close Underline tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[color]\" onClick=\"javascript:emo(\' [color=blue] \');\" style=\"cursor:pointer;\" title=\"Open color tag \"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/color]\" onClick=\"javascript:emo(\' [/color] \');\" style=\"cursor:pointer;\" title=\"Close color tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[size]\" onClick=\"javascript:emo(\' [size=2] \');\" style=\"cursor:pointer;\" title=\"Open size tag (relative, from 1 to 7)\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/size]\" onClick=\"javascript:emo(\' [/size] \');\" style=\"cursor:pointer;\" title=\"Close size tag\"></td>');
   document.writeln('          </tr><tr>');
   document.writeln('            <td colspan=\"2\">&nbsp;</td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[list]\" onClick=\"javascript:emo(\' [list] \');\" style=\"cursor:pointer;\" title=\"Open list tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[/list]\" onClick=\"javascript:emo(\' [/list] \');\" style=\"cursor:pointer;\" title=\"Close list tag\"></td>');
   document.writeln('            <td><input type=\"button\" class=\"button\" value=\"[*]\" onClick=\"javascript:emo(\' [*] \');\" style=\"cursor:pointer;\" title=\"List Item tag\"></td>');
   document.writeln('         </tr>');
   document.writeln('      </table>');

}

function writeRTE(rte, html, width, height, buttons, stylesheet) {
   if (buttons == true) {
      document.writeln('<style type="text/css">');
      document.writeln('.btnImage {cursor: pointer; cursor: hand;}');
      document.writeln('</style>');
      document.writeln('<table id="Buttons1_' + rte + '">');
      document.writeln('   <tr>');
      document.writeln('      <td>');
      document.writeln('         <select id="fontname" name="selectFont" onchange="Select(\'' + rte + '\', this.id)">');
      document.writeln('            <option value="Font" selected>Font</option>');
      document.writeln('            <option value="Arial, Helvetica, sans-serif">Arial</option>');
      document.writeln('            <option value="Courier New, Courier, mono">Courier New</option>');
      document.writeln('            <option value="Times New Roman, Times, serif">Times New Roman</option>');
      document.writeln('            <option value="Verdana, Arial, Helvetica, sans-serif">Verdana</option>');
      document.writeln('         </select>');
      document.writeln('      </td>');
      document.writeln('      <td>');
      document.writeln('         <select unselectable="on" id="fontsize" onchange="Select(\'' + rte + '\', this.id);">');
      document.writeln('            <option value="Size">Size</option>');
      document.writeln('            <option value="1">1</option>');
      document.writeln('            <option value="2">2</option>');
      document.writeln('            <option value="3">3</option>');
      document.writeln('            <option value="4">4</option>');
      document.writeln('            <option value="5">5</option>');
      document.writeln('            <option value="6">6</option>');
      document.writeln('            <option value="7">7</option>');
      document.writeln('         </select>');
      document.writeln('      </td>');
      document.writeln('   </tr>');
      document.writeln('</table>');
      document.writeln('<table id="Buttons2_' + rte + '" cellpadding="1" cellspacing="0" >');
      document.writeln('   <tr>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_bold.gif" width="25" height="24" alt="Bold" title="Bold" onClick="FormatText(\'' + rte + '\', \'bold\', \'\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_italic.gif" width="25" height="24" alt="Italic" title="Italic" onClick="FormatText(\'' + rte + '\', \'italic\', \'\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_underline.gif" width="25" height="24" alt="Underline" title="Underline" onClick="FormatText(\'' + rte + '\', \'underline\', \'\')"></td>');
      document.writeln('      <td>&nbsp;</td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_left_just.gif" width="25" height="24" alt="Align Left" title="Align Left" onClick="FormatText(\'' + rte + '\', \'justifyleft\', \'\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_centre.gif" width="25" height="24" alt="Center" title="Center" onClick="FormatText(\'' + rte + '\', \'justifycenter\', \'\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_right_just.gif" width="25" height="24" alt="Align Right" title="Align Right" onClick="FormatText(\'' + rte + '\', \'justifyright\', \'\')"></td>');
      document.writeln('      <td>&nbsp;</td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_numbered_list.gif" width="25" height="24" alt="Ordered List" title="Ordered List" onClick="FormatText(\'' + rte + '\', \'insertorderedlist\', \'\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_list.gif" width="25" height="24" alt="Unordered List" title="Unordered List" onClick="FormatText(\'' + rte + '\', \'insertunorderedlist\', \'\')"></td>');
      if ( width <= 450 ) {
         document.writeln('</tr><tr>');
      }else{
         document.writeln('   <td>&nbsp;</td>');
      }
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_outdent.gif" width="25" height="24" alt="Outdent" title="Outdent" onClick="FormatText(\'' + rte + '\', \'outdent\', \'\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_indent.gif" width="25" height="24" alt="Indent" title="Indent" onClick="FormatText(\'' + rte + '\', \'indent\', \'\')"></td>');
      document.writeln('      <td><div id="forecolor"><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_textcolor.gif" width="25" height="24" alt="Text Color" title="Text Color" onClick="FormatText(\'' + rte + '\', \'forecolor\', \'\')"></div></td>');
      document.writeln('      <td><div id="hilitecolor"><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_bgcolor.gif" width="25" height="24" alt="Background Color" title="Background Color" onClick="FormatText(\'' + rte + '\', \'hilitecolor\', \'\')"></div></td>');
      document.writeln('      <td>&nbsp;</td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_hyperlink.gif" width="25" height="24" alt="Insert Link" title="Insert Link" onClick="FormatText(\'' + rte + '\', \'createlink\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_image.gif" width="25" height="24" alt="Add Image" title="Add Image" onClick="AddImage(\'' + rte + '\')"></td>');
//    if (document.all) document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_spellcheck.gif" width="25" height="24" alt="Spell Check" title="Spell Check" onClick="checkspell()"></td>');
//    document.writeln('      <td>&nbsp;</td>');
//    document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_cut.gif" width="25" height="24" alt="Cut" title="Cut" onClick="FormatText(\'' + rte + '\', \'cut\')"></td>');
//    document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_copy.gif" width="25" height="24" alt="Copy" title="Copy" onClick="FormatText(\'' + rte + '\', \'copy\')"></td>');
//    document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_paste.gif" width="25" height="24" alt="Paste" title="Paste" onClick="FormatText(\'' + rte + '\', \'paste\')"></td>');
//    document.writeln('      <td>&nbsp;</td>');
//    document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_undo.gif" width="25" height="24" alt="Undo" title="Undo" onClick="FormatText(\'' + rte + '\', \'undo\')"></td>');
//    document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/rte/images/post_button_redo.gif" width="25" height="24" alt="Redo" title="Redo" onClick="FormatText(\'' + rte + '\', \'redo\')"></td>');
      document.writeln('   </tr>');
      document.writeln('   <tr> ');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/sun.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/sun.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/wink.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/wink.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/happy.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/happy.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/laugh.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/laugh.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/ohmy.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/ohmy.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/sick.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/sick.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/angry.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/angry.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/blink.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/blink.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/tongue.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/tongue.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/unhappy.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/unhappy.gif\')"></td>');
      document.writeln('      <td><img class="btnImage" src="components/com_simpleboard/emoticons/unsure.gif" width="19" height="19" alt="Add Smilie" title="Add Smilie" onClick="AddSmile(\'' + rte + '\',\'components/com_simpleboard/emoticons/unsure.gif\')"></td>');
      document.writeln('   </tr>');
      document.writeln('</table>');
   }
   document.writeln('<iframe id="' + rte + '" width="' + width + 'px" height="' + height + 'px"></iframe>');
   document.writeln('<table width="100%"><tr><td><input type="checkbox" id="chkSrc' + rte + '" onclick="toggleHTMLSrc(\'' + rte + '\');" />&nbsp;View Source</td>');
   document.writeln('<td align="right"><input type="button" class="button" value="preview" onclick="HtmlPreview(\'' + rte + '\',\'' + stylesheet + '\');" /></td></tr></table>');
   document.writeln('<iframe width="254" height="174" id="cp' + rte + '" src="components/com_simpleboard/rte/palette.htm" marginwidth="0" marginheight="0" scrolling="no" style="visibility:hidden; position: absolute;"></iframe>');
   document.writeln('<input type="hidden" id="hdn' + rte + '" name="' + rte + '" value="">');
   document.getElementById('hdn' + rte).value = html;
   //setTimeout('enableDesignMode(\'' + rte + '\', \'' + html + '\')', 250);
   enableDesignMode(rte, html, stylesheet);
}

function enableDesignMode(rte, html, stylesheet) {
   var oRTE;
   var frameHtml = "<html>\n";
   frameHtml += "<head>\n";
   frameHtml += "<link media=\"all\" type=\"text/css\" href=\""+ stylesheet + "\" rel=\"stylesheet\">\n";
   frameHtml += "</head>\n";
   frameHtml += "<body class=\"inputbox\"><p>\n";
   frameHtml += html;
   frameHtml += "</p></body>\n";
   frameHtml += "</html>";
   
   if (document.all) {
      oRTE = frames[rte].document;
      oRTE.open();
      oRTE.write(frameHtml);
      oRTE.close();
      oRTE.designMode = "On";
   }
   else {
      oRTE = document.getElementById(rte).contentWindow.document;
//    document.getElementById(rte).contentDocument.designMode = "On";
      oRTE.designMode = "On";
      oRTE.open();
      oRTE.write(frameHtml);
      oRTE.close();
      //attach a keyboard handler for Mozilla to make keyboard shortcuts for formatting text work
      document.getElementById(rte).contentWindow.document.addEventListener("keypress", kb_handler, true);
   }
}

function updateRTE(rte) {
   //set message value
   var oHdnMessage = document.getElementById('hdn' + rte);
   var oRTE = document.getElementById(rte);
   
   if (isRichText) {
      if (oHdnMessage.value == null) oHdnMessage.value = "";
      if (document.all) {
         oHdnMessage.value = frames[rte].document.body.innerHTML;
      }
      else {
         oHdnMessage.value = oRTE.contentWindow.document.body.innerHTML;
         //exception for Mozilla
         if (oHdnMessage.value.indexOf('<br>') > -1 && oHdnMessage.value.length == 8) oHdnMessage.value = "";
      }
   }
}

function HtmlPreview(rte, stylesheet) {
   //set message value
   var oHdnMessage = document.getElementById('hdn' + rte);
   var oRTE = document.getElementById(rte);
   
   if (isRichText) {
      if (oHdnMessage.value == null) oHdnMessage.value = "";
      if (document.all) {
         oHdnMessage.value = frames[rte].document.body.innerHTML;
      }
      else {
         oHdnMessage.value = oRTE.contentWindow.document.body.innerHTML;
         //exception for Mozilla
         if (oHdnMessage.value.indexOf('<br>') > -1 && oHdnMessage.value.length == 8) oHdnMessage.value = "";
      }
   }

win = window.open(", ", 'popup', 'width=640, height=480, toolbar = no, status = no');
win.document.write("<link media=\"all\" type=\"text/css\" href=\""+ stylesheet + "\" rel=\"stylesheet\">");
win.document.write("<DIV style=\"margin-left:10px; margin-top:10px;margin-right:10px;margin-bottom:10px\">");
win.document.write("<hr size=1 width=\"20%\" align=\"left\">");
win.document.write("" + oHdnMessage.value + "");
win.document.write("<hr size=1 width=\"20%\" align=\"left\">");
win.document.write("<a href=\"javascript:window.close()\"> Close this window </A> ");
win.document.write("</DIV>");
}

function toggleHTMLSrc(rte) {
   //contributed by Bob Hutzel (thanks Bob!)
   var oRTE;
   if (document.all) {
      oRTE = frames[rte].document;
   }
   else {
      oRTE = document.getElementById(rte).contentWindow.document;
   }
   
   if (document.getElementById("chkSrc" + rte).checked) {
   
      document.getElementById("Buttons1_" + rte).style.visibility = "hidden";
      document.getElementById("Buttons2_" + rte).style.visibility = "hidden";
      if (document.all) {
         oRTE.body.innerText = oRTE.body.innerHTML;
      } else {
         var htmlSrc = doc.createTextNode(oRTE.body.innerHTML);
         oRTE.body.innerHTML = "";
         oRTE.body.appendChild(htmlSrc);
      }

   } else {
      document.getElementById("Buttons1_" + rte).style.visibility = "visible";
      document.getElementById("Buttons2_" + rte).style.visibility = "visible";
      if (document.all) {
         oRTE.body.innerHTML = oRTE.body.innerText;
      } else {
         var htmlSrc = oRTE.body.ownerDocument.createRange();
         htmlSrc.selectNodeContents(oRTE.body);
         oRTE.body.innerHTML = htmlSrc.toString();
      }
   }
}

//Function to format text in the text box
function FormatText(rte, command, option) {
   var oRTE;
   if (document.all) {
      oRTE = frames[rte];
   }
   else {
      oRTE = document.getElementById(rte).contentWindow;
   }
   
   if ((command == "forecolor") || (command == "hilitecolor")) {
      parent.command = command;
      buttonElement = document.getElementById(command);
      document.getElementById('cp' + rte).style.left = getOffsetLeft(buttonElement) + "px";
      document.getElementById('cp' + rte).style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";
      if (document.getElementById('cp' + rte).style.visibility == "hidden")
         document.getElementById('cp' + rte).style.visibility="visible";
      else {
         document.getElementById('cp' + rte).style.visibility="hidden";
      }
      
      //get current selected rte
      currentRTE = rte;
      
      //get current selected range
      var sel = oRTE.document.selection; 
      if (sel != null) {
         rng = sel.createRange();
      }
   }
   else if (command == "createlink") {
      var szURL = prompt("Enter a URL:", "http://");
      oRTE.document.execCommand("Unlink",false,null)
      oRTE.document.execCommand("CreateLink",false,szURL)
   }
   else {
      oRTE.focus();
      oRTE.document.execCommand(command, false, option);
      oRTE.focus();
   }
}

//Function to set color
function setColor(color) {
   var rte = currentRTE;
   var oRTE;
   if (document.all) {
      oRTE = frames[rte];
   }
   else {
      oRTE = document.getElementById(rte).contentWindow;
   }
   
   var parentCommand = parent.command;
   if (document.all) {
      //retrieve selected range
      var sel = oRTE.document.selection; 
      if (parentCommand == "hilitecolor") parentCommand = "backcolor";
      if (sel != null) {
         var newRng = sel.createRange();
         newRng = rng;
         newRng.select();
      }
   }
   else {
      oRTE.focus();
   }
   oRTE.document.execCommand(parentCommand, false, color);
   oRTE.focus();
   document.getElementById('cp' + rte).style.visibility = "hidden";
}

//Function to add image
function AddImage(rte) {
   var oRTE;
   if (document.all) {
      oRTE = frames[rte];
   }
   else {
      oRTE = document.getElementById(rte).contentWindow;
   }
   
   imagePath = prompt('Enter Image URL:', 'http://');          
   if ((imagePath != null) && (imagePath != "")) {
      oRTE.focus()
      oRTE.document.execCommand('InsertImage', false, imagePath);
   }
   oRTE.focus()
}

//Function to add smilie
function AddSmile(rte, imagePath) {
   var oRTE;
   if (document.all) {
      oRTE = frames[rte];
   }
   else {
      oRTE = document.getElementById(rte).contentWindow;
   }
   
   //imagePath = prompt('Enter Image URL:', 'http://');          
   if ((imagePath != null) && (imagePath != "")) {
      oRTE.focus()
      oRTE.document.execCommand('InsertImage', false, imagePath);
   }
   oRTE.focus()
}

//function to perform spell check
function checkspell() {
   try {
      var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
      tmpis.CheckAllLinkedDocuments(document);
   }
   catch(exception) {
      if(exception.number==-2146827859) {
         if (confirm("ieSpell not detected.  Click Ok to go to download page."))
            window.open("http://www.iespell.com/download.php","DownLoad");
      }
      else
         alert("Error Loading ieSpell: Exception " + exception.number);
   }
}

function getOffsetTop(elm) {
   var mOffsetTop = elm.offsetTop;
   var mOffsetParent = elm.offsetParent;
   
   while(mOffsetParent){
      mOffsetTop += mOffsetParent.offsetTop;
      mOffsetParent = mOffsetParent.offsetParent;
   }
   
   return mOffsetTop;
}

function getOffsetLeft(elm) {
   var mOffsetLeft = elm.offsetLeft;
   var mOffsetParent = elm.offsetParent;
   
   while(mOffsetParent) {
      mOffsetLeft += mOffsetParent.offsetLeft;
      mOffsetParent = mOffsetParent.offsetParent;
   }
   
   return mOffsetLeft;
}

function Select(rte, selectname) {
   var oRTE;
   if (document.all) {
      oRTE = frames[rte];
   }
   else {
      oRTE = document.getElementById(rte).contentWindow;
   }
   
   var cursel = document.getElementById(selectname).selectedIndex;
   // First one is always a label
   if (cursel != 0) {
      var selected = document.getElementById(selectname).options[cursel].value;
      oRTE.document.execCommand(selectname, false, selected);
      document.getElementById(selectname).selectedIndex = 0;
   }
   oRTE.focus();
}

function kb_handler(evt, rte) {
   //contributed by Anti Veeranna (thanks Anti!)
   if (evt.ctrlKey) {
      var key = String.fromCharCode(evt.charCode).toLowerCase();
      var cmd = '';
      switch (key) {
         case 'b': cmd = "bold"; break;
         case 'i': cmd = "italic"; break;
         case 'u': cmd = "underline"; break;
      };

      if (cmd) {
         evt.target.ownerDocument.execCommand(cmd,false,true);
         // stop the event bubble
         evt.preventDefault();
         evt.stopPropagation();
      }
   }
}

initRTE();

