
/**| openWYSIWYG v1.?? Copyright (c) 2006 openWebWare.com |****************************************
 *
 * An open source WYSIWYG editor for use in web based applications.
 * For full source code and docs, visit http://www.openwebware.com/
 *
 * Global Variables: Set global variables such as images directory,
 * WYSIWYG Height, Width, and CSS Directory.
 *
 * note: This Version of Openwysiwyg is hardly modified and hopefully returns some
 * more xhtml compilant html code. Tested this in IE6, FF, Opera9 - works good so far.
 * Note that most of this code is modded so hard that this will no longer work as standalone
 * wysiwyg script. it requires the fxajax js lib and is mostly modified to work with my own
 * simple cms solution. feel free if you could require some code for your purposes...
 *
 * no.copyright (c) by scorp 2008 for stylefx.de
 *
***************************************************************************************************
 */

   // GET USER BROWSER TO VAR
   var browserName = navigator.appName;

   // Wysiwyg Images Directory
   var imagesDir = "engine/images/icons/";

   // CSS Directory
   var cssDir = "engine/styles/";

   // Popups Directory
   var popupsDir = "engine/" + wysiwygDir + "/popups/";

   // Default Font Size
   var fontSIZE = "85%";

   //Default Font Family
   var fontFAM = "Arial,Tahoma";

   //Default FONT COLOR
   var fontCOL = "#000000";

   //Default BG COLOR
   var backCOL = "#ffffff";

   // WYSIWYG Width %
   var wysiwygWidth = "100";

   // WYSIWYG Height px
   var wysiwygHeight = "375";

// Include Style Sheet
//document.write('<link rel="stylesheet" type="text/css" href="' +templatepath + 'stylesheet.php++">\n');



/* ------------------------------------------------------------------------------------------------------------------- *\
     Toolbar Settings: Set the features and buttons available in the WYSIWYG Editor
\* ------------------------------------------------------------------------------------------------------------------- */

  var BlockFormats = new Array();
   BlockFormats[0] = "Textabsatz";
   BlockFormats[1] = "Überschrift 1";
   BlockFormats[2] = "Überschrift 2";
   BlockFormats[3] = "Überschrift 3";
   BlockFormats[4] = "Überschrift 4";
   BlockFormats[5] = "Überschrift 5";
   BlockFormats[6] = "Überschrift 6";
   BlockFormats[7] = "Vorformatiert";


 var BlockTags = {
  "Textabsatz"    : '<p>',
  "Überschrift 1" : '<h1>',
  "Überschrift 2" : '<h2>',
  "Überschrift 3" : '<h3>',
  "Überschrift 4" : '<h4>',
  "Überschrift 5" : '<h5>',
  "Überschrift 6" : '<h6>',
  "Vorformatiert" : '<pre>'
 };


// Order of available commands in toolbar ( only 1 toolbar)
   var buttonName = new Array();
   buttonName[0]  = "";
   buttonName[1]  = "bold";
   buttonName[2]  = "italic";
   buttonName[3]  = "underline";
   buttonName[4]  = "justifyleft";
   buttonName[5]  = "justifycenter";
   buttonName[6]  = "justifyright";
   buttonName[7]  = "justifyfull";
   buttonName[8]  = "unorderedlist";
   buttonName[9]  = "insertpdf";
   buttonName[10] = "insertimage";
   buttonName[11] = "createlink";
   buttonName[12] = "enlarge";
   buttonName[13] = "shrink";
   buttonName[14] = "viewSource";
   buttonName[15] = "syntax";


/** Toolbar Settings: Sets Features and Buttons available in the WYSIWYG

Name                    buttonID                  buttonTitle          buttonImage                           buttonImage-Rollover
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ **/
var ToolbarList = {

"bold":                 ['Bold',                 'Fettschrift',         imagesDir + 'bold.gif',               imagesDir + 'bold_on.gif'],
"italic":               ['Italic',               'Kursiv',              imagesDir + 'italics.gif',            imagesDir + 'italics_on.gif'],
"underline":            ['Underline',            'Unterstrichen',       imagesDir + 'underline.gif',          imagesDir + 'underline_on.gif'],
"strikethrough":        ['Strikethrough',        'Strikethrough',       imagesDir + 'strikethrough.gif',      imagesDir + 'strikethrough_on.gif'],
"seperator":            ['',                     '',                    imagesDir + 'seperator.gif',          imagesDir + 'seperator.gif'],
"subscript":            ['Subscript',            'Subscript',           imagesDir + 'subscript.gif',          imagesDir + 'subscript_on.gif'],
"superscript":          ['Superscript',          'Superscript',         imagesDir + 'superscript.gif',        imagesDir + 'superscript_on.gif'],
"justifyleft":          ['Justifyleft',          'Links ausrichten',    imagesDir + 'justify_left.gif',       imagesDir + 'justify_left_on.gif'],
"justifycenter":        ['Justifycenter',        'Zentriert ausrichten',imagesDir + 'justify_center.gif',     imagesDir + 'justify_center_on.gif'],
"justifyright":         ['Justifyright',         'Rechts ausrichten',   imagesDir + 'justify_right.gif',      imagesDir + 'justify_right_on.gif'],
"justifyfull":          ['Justifyfull',          'Blocksatz',           imagesDir + 'justify_justify.gif',    imagesDir + 'justify_justify_on.gif'],
"unorderedlist":        ['InsertUnorderedList',  'Liste erstellen',     imagesDir + 'list_unordered.gif',     imagesDir + 'list_unordered_on.gif'],
"orderedlist":          ['InsertOrderedList',    'InsertOrderedList',   imagesDir + 'list_ordered.gif',       imagesDir + 'list_ordered_on.gif'],
"outdent":              ['Outdent',              'Ausrücken',           imagesDir + 'indent_left.gif',        imagesDir + 'indent_left_on.gif'],
"indent":               ['Indent',               'Einrücken',           imagesDir + 'indent_right.gif',       imagesDir + 'indent_right_on.gif'],
"cut":                  ['Cut',                  'Ausschneiden',        imagesDir + 'cut.gif',                imagesDir + 'cut_on.gif'],
"copy":                 ['Copy',                 'Kopieren',            imagesDir + 'copy.gif',               imagesDir + 'copy_on.gif'],
"paste":                ['Paste',                'Einfügen',            imagesDir + 'paste.gif',              imagesDir + 'paste_on.gif'],
"forecolor":            ['ForeColor',            'Vordergrundfarbe',    imagesDir + 'forecolor.gif',          imagesDir + 'forecolor_on.gif'],
"backcolor":            ['BackColor',            'Hintergrundfarbe',    imagesDir + 'backcolor.gif',          imagesDir + 'backcolor_on.gif'],
"undo":                 ['Undo',                 'Undo',                imagesDir + 'undo.gif',               imagesDir + 'undo_on.gif'],
"redo":                 ['Redo',                 'Redo',                imagesDir + 'redo.gif',               imagesDir + 'redo_on.gif'],
"inserttable":          ['InsertTable',          'Tabelle einfügen',    imagesDir + 'insert_table.gif',       imagesDir + 'insert_table_on.gif'],
"insertimage":          ['InsertImage',          'Bild einfügen',       imagesDir + 'insert_picture.gif',     imagesDir + 'insert_picture_on.gif'],
"insertpdf":            ['InsertFile',           'Datei einfügen',      imagesDir + 'insert_pdf.gif',         imagesDir + 'insert_pdf_on.gif'],
"createlink":           ['CreateLink',           'Hyperlink erstellen', imagesDir + 'insert_hyperlink.gif',   imagesDir + 'insert_hyperlink_on.gif'],
"viewSource":           ['ViewSource',           'HTML Code einsehen',  imagesDir + 'view_source.gif',        imagesDir + 'view_source_on.gif'],
"viewText":             ['ViewText',             'Zum Textmodus',       imagesDir + 'view_text.gif',          imagesDir + 'view_text_on.gif'],
"selectstyle":          ['SelectStyle',          'Absatzformat',        imagesDir + 'select_format.gif',      imagesDir + 'select_format_on.gif'],
"enlarge":              ['enlargeview',          'Editor vergrößern',   imagesDir + 'undo.gif',               imagesDir + 'undo_on.gif'],
"shrink":               ['shrinkview',           'Editor verkleinern',  imagesDir + 'redo.gif',               imagesDir + 'redo_on.gif'],
"help":                 ['Help',                 'Help',                imagesDir + 'help.gif',               imagesDir + 'help_on.gif'],
"syntax":               ['syntaxHighlight',      'SyntaxHighlight',     imagesDir + 'syntax_hilite.gif',      imagesDir + 'syntax_hilite_on.gif'],
"selectfont":           ['SelectFont',           'SelectFont',          imagesDir + 'select_font.gif',        imagesDir + 'select_font_on.gif'],
"selectsize":           ['SelectSize',           'SelectSize',          imagesDir + 'select_size.gif',        imagesDir + 'select_size_on.gif']
};



if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
   HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
   {
     switch (where)
     {
      case 'beforeBegin':
        this.parentNode.insertBefore(parsedNode,this)
        break;

      case 'afterBegin':
        this.insertBefore(parsedNode,this.firstChild);
        break;

      case 'beforeEnd':
        this.appendChild(parsedNode);
        break;

      case 'afterEnd':
        if (this.nextSibling)
        this.parentNode.insertBefore(parsedNode,this.nextSibling);
        else this.parentNode.appendChild(parsedNode);
        break;
    }

  }

   HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
   {
    var r = this.ownerDocument.createRange();
    r.setStartBefore(this);
    var parsedHTML = r.createContextualFragment(htmlStr);
    this.insertAdjacentElement(where,parsedHTML)
   }

   HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
   {
    var parsedText = document.createTextNode(txtStr)
    this.insertAdjacentElement(where,parsedText)
   }

};

// Create viewTextMode global variable and set to 0
// enabling all toolbar commands while in HTML mode
   var viewTextMode = 0; var html;


/* ---------------------------------------------------------------------- *\
     Function    : generate_wysiwyg()
     Description : replace textarea with wysiwyg editor
\* ---------------------------------------------------------------------- */
function generate_wysiwyg(textareaID)
{
   // Hide the textarea, set Var
   var n = textareaID;
   fx.style(textareaID).display = "none";


   // Get Textarea Height
   var txtareaheight = fx.style(n).height;

   // Toolbars width is 2 pixels wider than the wysiwygs
   toolbarWidth = parseInt(wysiwygWidth);

   // Generate toolbar one
   var toolbar;

   toolbar  = '<table cellpadding="0" cellspacing="0" border="0" class="toolbar1" style="width:' + toolbarWidth + '%;"><tr><td style="width: 6px;"> </td>';
   toolbar += '<td style="width:90px;"><span id="StyleSelect' + n + '"></span></td>';

   // Output all command buttons that belong to toolbar one
   for(var i = 0; i <= buttonName.length;)
   {
     if(buttonName[i])
     {
        var buttonObj = ToolbarList[buttonName[i]];
        var buttonID  = buttonObj[0];
        var buttonTitle = buttonObj[1];
        var buttonImage = buttonObj[2];
        var buttonImageRollover = buttonObj[3];

        if (buttonName[i] == "seperator")
        {
          toolbar += '<td style="width: 12px;" align="center"><img src="' +buttonImage+ '" border=0 unselectable="on" width="2" height="18" hspace="2" unselectable="on"></td>';
        }
        else if (buttonName[i] == "viewSource")
        {
          toolbar += '<td style="width: 12px;">';
          toolbar += '<span id="HTMLMode' + n + '"><img src="'  +buttonImage+  '" border=0 unselectable="on" title="' +buttonTitle+ '" id="' +buttonID+ '" class="button" onClick="formatText(this.id,\'' + n + '\');" onmouseover="if(className==\'button\'){className=\'buttonOver\'}; this.src=\'' +buttonImageRollover+ '\';" onmouseout="if(className==\'buttonOver\'){className=\'button\'}; this.src=\'' + buttonImage + '\';" unselectable="on"  width="20" height="20"></span>';
          toolbar += '<span id="textMode' + n + '"><img src="' +imagesDir+ 'view_text.gif" border=0 unselectable="on" title="viewText" id="ViewText" class="button" onClick="formatText(this.id,\'' + n + '\');" onmouseover="if(className==\'button\'){className=\'buttonOver\'}; this.src=\'' +imagesDir+ 'view_text_on.gif\';" onmouseout="if(className==\'buttonOver\'){className=\'button\'}; this.src=\'' +imagesDir+ 'view_text.gif\';" unselectable="on"  width="20" height="20"></span>';
          toolbar += '</td>';
        }
        else
        {
          toolbar += '<td style="width: 12px;"><img src="' +buttonImage+ '" border=0 unselectable="on" title="' +buttonTitle+ '" id="' +buttonID+ '" class="button" onClick="formatText(this.id,\'' + n + '\');" onmouseover="if(className==\'button\'){className=\'buttonOver\'}; this.src=\'' + buttonImageRollover + '\';" onmouseout="if(className==\'buttonOver\'){className=\'button\'}; this.src=\'' + buttonImage + '\';" unselectable="on" width="20" height="20"></td>';
        }
      }
   i++;
   }

   toolbar += '<td>&nbsp;</td></tr></table>';

   /* ONLY Internet Explorer */
   if(browserName == "Microsoft Internet Explorer")
   {
   var iframe = '<iframe frameborder="0" id="wysiwyg' + n + '" style="width:' + wysiwygWidth + '%; height:' + wysiwygHeight + 'px; margin:0px 0px 0px -1px; border:1px solid #999;"></iframe>\n';
   }
   else if(browserName == "Opera")
   {
   var iframe = '<div id="operarel" style="position:relative; height:1px; line-height:1px; margin:0px; padding:0px;">\n';
   iframe += '<div id="operaabs'+ n +'" style="position:absolute; top:'+ txtareaheight +'; width:' + wysiwygWidth + '%; height:3px; padding:0px; margin:0px; z-index:99999;" onmouseover="updateTextAreaOpera(n);" onmouseout="updateTextAreaOpera(n);"></div></div>\n';
   iframe += '<iframe frameborder="0" id="wysiwyg' + n + '" style="width:' + wysiwygWidth + '%; height:' + wysiwygHeight + 'px; margin:0px 0px 0px -1px; border:1px solid #999; overflow-y:scroll;"></iframe>\n';
   }
   else
   {
   var iframe = '<iframe frameborder="0" id="wysiwyg' + n + '" style="width:' + wysiwygWidth + '%; height:' + wysiwygHeight + 'px; margin:0px 0px 0px -1px; border:1px solid #999; overflow-y:scroll;"></iframe>\n';
   }


   // Insert after the textArea both toolbar one and two// document.getElementById(n).insertAdjacentHTML("afterEnd", toolbar + toolbar2 + iframe);
    fx.getId(n).insertAdjacentHTML("beforeBegin", toolbar + iframe);
    fx.getId(n).style.border='1px solid #cccccc';
    fx.getId(n).style.margin='0px';

   // Insert the StyleType drop downs into the toolbar
    outputStyleSelect(n);

   // Hide the dynamic drop down list for the Styles
    hideStyles(n);

   // Hide the "Text Mode" button
    fx.getId("textMode" + n).style.display = 'none';

   // Give the iframe the global wysiwyg height and width
    fx.getId("wysiwyg" + n).style.height = wysiwygHeight + "px";
    fx.getId("wysiwyg" + n).style.width  = wysiwygWidth  + "%";

   // Pass the textarea's existing text over to the content variable
    var doc     = fx.getId("wysiwyg" + n).contentWindow.document;
    var content = fx.getId(n).value;

   content = content.replace(/<span style="font-weight: normal;">(.*|.*?\s+.*?)<\/span>/gi, '$1');
   content = content.replace(/<span style="(.*?)">( |)<\/span>/gi, '$2');
   content = content.replace(/<\/span>( |)<\/span>/gi, '</span></span>$1');


     if(browserName == "Netscape" || browserName == "Firefox")
     {
            content = content.replace(/<u>(.*?|.*\s+.*?|.*?\s.*?\s.*?|.*?\s.*?\s.*?\s.*?)<\/u>/gi, '<span style="text-decoration: underline;">$1</span>');
            content = content.replace(/<strong>(.*?|.*\s+.*?|.*?\s.*?\s.*?|.*?\s.*?\s.*?\s.*?)<\/strong>/gi, '<span style="font-weight: bold;">$1</span>');
            content = content.replace(/<em>(.*?|.*\s+.*?|.*?\s.*?\s.*?|.*?\s.*?\s.*?\s.*?)<\/em>/gi, '<span style="font-style: italic;">$1</span>');
            content = content.replace(/<strong><em>(.*?|.*\s+.*?|.*?\s.*?\s.*?|.*?\s.*?\s.*?\s.*?)<\/em><\/strong>/gi, '<span style="font-weight:bold; font-style: italic;">$1</span>');
            content = content.replace(/<em><strong>(.*?|.*\s+.*?|.*?\s.*?\s.*?|.*?\s.*?\s.*?\s.*?)<\/strong><\/em>/gi, '<span style="font-weight:bold; font-style: italic;">$1</span>');
     }

   // Write the textarea's content into the iframe
   // Add additional CSS Classes to be present in Editmode

    doc.open();
    doc.write('<style type="text/css" media="screen">');
    doc.write('body {color: ' + fontCOL + '; font-size: ' + fontSIZE + '; font-family: ' + fontFAM + '; background: ' + backCOL + ';}');
    doc.write('h1{margin:0px;padding:0px;} h2{margin:0px;padding:0px;} h3{margin:0px;padding:0px;} h4{margin:0px;padding:0px;} h5{margin:0px;padding:0px;} h6{margin:0px;padding:0px;}');
    doc.write('.pleft {text-align:left; float:left; width:60%; margin:4px 0px 3px 3px;}');
    doc.write('.pright {text-align:right; float:right; width:80px; margin:4px 3px 3px 0px;}');
    doc.write('.clr{clear:both;}');
    doc.write('img{border:0px;} img a{border:1px solid #444;} a:link,a:visited{text-decoration:none;font-weight:bold;}');
    doc.write('</style>');
   // doc.write('<link rel="stylesheet" type="text/css" href="' +templatepath + 'stylesheet.php'+basequery+'">');
    doc.write(content);
    doc.close();


   // Make editable for OP,SF,FF,IE
    doc.body.contentEditable = true;
    doc.designMode = "on";



    if(IE)
    {
       for (var idx=0; idx < document.forms.length; idx++)
       {
        document.forms[idx].attachEvent('onsubmit', function() { updateTextArea(n); });
       }
    }
    else if(OP)
    {
       for (var idx=0; idx < document.forms.length; idx++)
       {
        document.forms[idx].addEventListener('change', function(){ updateTextAreaOpera(n); }, true);
        document.forms[idx].addEventListener('mouseout', function(){ updateTextAreaOpera(n); }, true);
       }
    }
    else
    {
       for(var idx=0; idx < document.forms.length; idx++)
       {
       // fx.addEvent(window,\'submit\',function(){FX_ElementWidth("piclinklabel'.$object.'");});
        document.forms[idx].addEventListener('submit',function(){ updateTextArea(n); }, true);
       }
    }
};




/* ---------------------------------------------------------------------- *\
  Function    : formatText()
  Description : replace textarea with wysiwyg editor
  Usage       : formatText(id, n, selected);
  Arguments   : id - The execCommand (e.g. Bold)
\* ---------------------------------------------------------------------- */
function formatText(id, n, selected)
{
  // When user clicks toolbar button make sure it always targets WYSIWYG
  document.getElementById("wysiwyg" + n).contentWindow.focus();

  // When in Text Mode these execCommands are disabled
  var formatIDs = new Array("FontSize","FontName","Bold","Italic","SelectStyle","Underline","Subscript","Superscript","Strikethrough","Justifyleft","Justifyright","Justifycenter","InsertUnorderedList","InsertOrderedList","Indent","Outdent","ForeColor","BackColor","InsertFile","InsertImage","InsertTable","CreateLink");

  // Check if button clicked is in disabled list
  for (var i = 0; i <= formatIDs.length;)
  {
    if(formatIDs[i]==id) var disabled_id=1;
    i++;
  }


  // Check if in Text Mode and disabled button was clicked
  if(viewTextMode == 1 && disabled_id == 1)
  {
  alert("Not available for HTML View");
  }
  else
  {

     switch(id)
     {
       case "InsertImage":     fx.createWindow('insimage'+n,popupsDir+'insert_image.php?web='+web+'&wysiwyg='+n,755,485,'Image Management','1','0','1');
                               break;

       case "InsertFile":      window.open(popupsDir+'insert_pdf.php?web='+web+'&wysiwyg='+n,'popup','location=0,status=0,scrollbars=0,resizable=0,width=400,height=175');
                               break;

       case "CreateLink":      fx.createWindow('link'+n,popupsDir+'insert_hyperlink.php?web='+web+'&wysiwyg='+n,755,485,'HyperLink Management','1','0','1');
                               break;

       case "ViewSource":      viewSource(n);
                               break;

       case "ViewText":        viewText(n);
                               break;

       case "syntaxHighlight": fx.createWindow('syntax'+n,popupsDir+'syntax.php?web='+web+'&wysiwyg=' + n, 580,700);
                               break;

       case "StyleName":       var getDocument = fx.getId("wysiwyg" + n).contentWindow.document;
                               getDocument.execCommand("FormatBlock", false, BlockTags[selected]);
                               break;

       case "enlargeview":     var wysiwyg         = fx.getId("wysiwyg"+n);
                               var textarea        = fx.getId(n);
                               if(OP){var operaabs = fx.getId("operaabs"+n)}

                               if(wysiwyg.style.height==''||textarea.style.height=='')
                               { wysiwyg.style.height      = "375px";
                                 textarea.style.height     = "375px";
                                 if(OP){operaabs.style.top = "375px"}
                               }else
                               { var pattern               = new RegExp("\\d+","ig");
                                 var currHeight            = wysiwyg.style.height.match(pattern);
                                 var newHeight             = + currHeight + 100;
                                 wysiwyg.style.height      = newHeight + "px";
                                 textarea.style.height     = newHeight + "px";
                                 if(OP){operaabs.style.top = newHeight + "px"}
                               } break;

       case "shrinkview":      var wysiwyg         = fx.getId("wysiwyg"+n);
                               var textarea        = fx.getId(n);
                               if(OP){var operaabs = fx.getId("operaabs"+n)}

                               if(wysiwyg.style.height==''||textarea.style.height=='')
                               { wysiwyg.style.height      = "275px";
                                 textarea.style.height     = "275px";
                                 if(OP){operaabs.style.top = "275px"}
                               }
                               else
                               { var pattern = new RegExp("\\d+","ig");
                                 var currHeight            = wysiwyg.style.height.match(pattern);
                                 var newHeight             = + currHeight -100;
                                 wysiwyg.style.height      = newHeight+"px";
                                 textarea.style.height     = newHeight+"px";
                                 if(OP){operaabs.style.top = newHeight+"px"}
                               } break;


       default:                fx.getId("wysiwyg"+n).contentWindow.document.execCommand(id, false, null);
                               betterformat(n);
                               break;
     }
   }
};


/* ---------------------------------------------------------------------- *\
  Function    : betterFormat()
  Description : inserts better HTMLSource Code
\* ---------------------------------------------------------------------- */
function betterformat(n)
{
   var textarea=fx.getId(n);
   var wysiwyg=fx.getId("wysiwyg"+n);
   var getDocument = fx.getId("wysiwyg"+n).contentWindow.document;
   var iHTML = getDocument.body.innerHTML

if(browserName == "Netscape" || browserName == "Mozilla Firefox")
{
   iHTML=iHTML.replace(/<div style="text-align: (left|center|right|justify);"><br( \/?|\/?)>(.*?|.*\s.*?)<\/div>/gi, "");
   iHTML=iHTML.replace(/<p style="text-align: (left|center|right|justify);"><br( \/?|\/?)>(.*?|.*\s.*?)<\/p>/gi, "");
   iHTML=iHTML.replace(/<strong style="(.*?|.*\s.*?)">(.*?|.*?\s.*)<\/strong>/gi, "<span style=\"font-weight:bold; $1\">$2</span>");
   iHTML=iHTML.replace(/<em style="(.*?|.*\s.*)">(.*?|.*\s.*?)<\/em>/gi, "<span style=\"font-style: italic; $1\">$2</span>");
   iHTML=iHTML.replace(/<strong>(.*?|.*?\s*.*?)<\/strong>/gi, "<span style=\"font-weight: bold;\">$1</span>");
   iHTML=iHTML.replace(/<em>(.*?|.*?\s*.*?)<\/em>/gi, "<span style=\"font-style: italic;\">$1</span>");
   }

if(!browserName == "Netscape")
{
   iHTML=iHTML.replace(/<(\/?)strong(\s+|>)/gi,'<$1b$2');
   iHTML=iHTML.replace(/<(\/?)em(\s+|>)/gi,'<$1i$2');
   iHTML=iHTML.replace(/<div align="(.*)">(.*|\s+.*)<\/div>/gi, '<p align=\"$1\">$2</p>');
   iHTML=iHTML.replace(/<div style="text-align: (.*|\s+.*)<\/div>/gi, '<p align=\"$1</p>');
   iHTML=iHTML.replace(/font-weight: 700;/gi, "font-weight: bold;");
   iHTML=iHTML.replace(/<div align="(.*)">(.*|\s.*)<\/div>/gi, '<p align=\"$1\">$2</p>');
   iHTML=iHTML.replace(/<div style="text-align: (.*|\s.*)<\/div>/gi, '<p align=\"$1</p>');
   }

   /* ONLY Internet Explorer */
if(browserName == "Microsoft Internet Explorer")
{
   iHTML=iHTML.replace(/style="(.*?)"/gi, 'style=\"$1;\"');
   iHTML=iHTML.replace(/style="text-align: (left|center|right|justify)(;|)"/gi, "align=\"$1\"");
}

   getDocument.body.innerHTML = iHTML;

}

/* ---------------------------------------------------------------------- *\
  Function    : insertHTML()
  Description : insert HTML into WYSIWYG in rich text
\* ---------------------------------------------------------------------- */

function insertHTML(html, n)
{
   if(browserName == "Microsoft Internet Explorer")
   {
   fx.getId('wysiwyg' + n).contentWindow.document.selection.createRange().pasteHTML(html);
   }
   else
   {
        // var div = fx.getId('wysiwyg' + n).contentWindow.document.createElement("div");
        // div.innerHTML = html;
           var i = fx.getId('wysiwyg' + n).contentWindow.document.createElement("i");
             i.innerHTML = html;

        //var textarea=fx.getId(n);
        //var wysiwyg=fx.getId("wysiwyg"+n);
        //var getDocument = fx.getId("wysiwyg"+n).contentWindow.document;
          var node = insertNodeAtSelection(i,n);

   }
}


/* ---------------------------------------------------------------------- *\
  Function    : insertNodeAtSelection()
  Description : insert HTML into WYSIWYG in rich text (firefox)
\* ---------------------------------------------------------------------- */
function insertNodeAtSelection(insertNode, n)
{ var sel = fx.getId('wysiwyg' + n).contentWindow.getSelection();
  var range = sel.getRangeAt(0);
  sel.removeAllRanges();
  range.deleteContents();
  var container = range.startContainer;
  var pos = range.startOffset;
  range = document.createRange();

  if(container.nodeType==3 && insertNode.nodeType==3)
  {
    container.insertData(pos, insertNode.nodeValue);
    range.setEnd(container, pos+insertNode.length);
    range.setStart(container, pos+insertNode.length);
  }
  else
  {
   var afterNode;
   if(container.nodeType==3)
   {
    var textNode = container;
    container = textNode.parentNode;
    var text = textNode.nodeValue;
    var textBefore = text.substr(0,pos);
    var textAfter = text.substr(pos);
    var beforeNode = document.createTextNode(textBefore);
    afterNode = document.createTextNode(textAfter);
    container.insertBefore(afterNode, textNode);
    container.insertBefore(insertNode, afterNode);
    container.insertBefore(beforeNode, insertNode);
    container.removeChild(textNode);
   }
   else
   {
    afterNode = container.childNodes[pos];
    container.insertBefore(insertNode, afterNode);
   }

    range.setEnd(afterNode, 0);
    range.setStart(afterNode, 0);

  }

sel.addRange(range);

};





/* ----------------------------------------------------------------------------------------------------------------------------- *\
  Function    : outputStyleSelect() // eb: inserted
  Description : creates the Style Select drop down and inserts it into the toolbar
\* ----------------------------------------------------------------------------------------------------------------------------- */
function outputStyleSelect(n)
{
  var StyleSelectObj = ToolbarList['selectstyle'];
  var StyleSelect = StyleSelectObj[2];
  var StyleSelectOn = StyleSelectObj[3];
  var StyleSelectDropDown = new Array;

   StyleSelectDropDown[n]='<table border="0" cellpadding="0" cellspacing="0"><tr><td onMouseOver="fx.getId(\'selectStyle' + n + '\').src=\'' +StyleSelectOn+ '\';" onMouseOut="document.getElementById(\'selectStyle' +n+ '\').src=\'' +StyleSelect+ '\';"><img src="' +StyleSelect+ '" id="selectStyle' + n + '" width="85" height="20" onClick="showStyles(\'' +n+ '\');" unselectable="on"><br/>';
   StyleSelectDropDown[n]+='<span id="BlockFormats' +n+ '" class="dropdown" style="width:145px;">';

   for(var i = 0; i <= BlockFormats.length;)
   {
        if(BlockFormats[i])
        {
        StyleSelectDropDown[n]+='<button type="button" onclick="formatText(\'StyleName\',\'' +n+ '\',\'' +BlockFormats[i]+ '\')\; hideStyles(\'' +n+ '\');" onMouseOver="this.className=\'mouseOver\'" onMouseOut="this.className=\'mouseOut\'" class="mouseOut" style="width:120px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="left" style="font-size: 12px;">' + BlockFormats[i] + '</td></tr></table></button><br/>';
        }

    i++;

   }
        StyleSelectDropDown[n]+='</span></td></tr></table>';
        fx.getId('StyleSelect'+n).insertAdjacentHTML("afterBegin",StyleSelectDropDown[n]);

};



/* --------------------------------------------------------------------------------------------------------------- *\
  Function    : hideStyles() // eb: inserted
  Description : Hides the list of font names in the font select drop down
\* ---------------------------------------------------------------------------------------------------------------- */
   function hideStyles(n)
   {
     fx.getId('BlockFormats' + n).style.display = 'none';
   };


/* ---------------------------------------------------------------------------------------------------------------- *\
  Function    : showStyles() // eb: inserted
  Description : Shows the list of font names in the font select drop down
\* ---------------------------------------------------------------------------------------------------------------- */
   function showStyles(n)
   {
     if(fx.style('BlockFormats' + n).display == 'block')
      {
        fx.style('BlockFormats' + n).display  = 'none';
      }
      else
      {
        fx.style('BlockFormats' + n).display  = 'block';
        fx.style('BlockFormats' + n).position = 'absolute';
      }

   };


/*-------------------------------------------------------------------------------- *\
   FUNKTION: TEXTAREA CONTENT TO IFRAME
\*-------------------------------------------------------------------------------- */
function textareaUpdate(n)
{
   var textarea = fx.getId(n);
   var wysiwyg  = fx.getId("wysiwyg"+n);

   html = wysiwyg.contentWindow.document.body.innerHTML;

  /* Remove leading and trailing whitespace */
   html = html.replace(/^\s+/gi, "");
   html = html.replace(/\s+$/gi, "");
  /* Replace improper IMGs */
   html = html.replace(/(<img [^>]+[^\/])>/gi, "$1 />");
  /* Replace bold tags with strong */
   html=html.replace(/<(\/?)b(\s+|>)/gi,'<$1strong$2');
  /* Replace italic tags with em */
   html=html.replace(/<(\/?)i(\s+|>)/gi,'<$1em$2');
  /* Textnodestyles to SPAN */
   html=html.replace(/<strong style="(.*?|.*\s.*)">(.*?|.*?\s.*)<\/strong>/gi, "<span style=\"$1\">$2</span>");
   html=html.replace(/<em style="(.*?|.*\s.*)">(.*|.*\s.*)<\/em>/gi, "<span style=\"$1\">$2</span>");
  /* <ELEMENT names to lowercase */
   html = html.replace(/<[^script>]*/gi, function(matchingelements){ return matchingelements.toLowerCase(); });
  /* simplify html code if possible and remove unneeded tags */
   html=html.replace(/<strong>(.*|\s.*)<strong>(.*|\s.*)<\/strong>(.*|.*\s*?.*?)<\/strong>/gi, "<strong>$1$2$3</strong>");
   html=html.replace(/<em>(.*|\s.*)<em>(.*|\s.*)<\/em>(.*|\s.*)<\/em>/gi, "<em>$1$2$3</em>");
   html=html.replace(/<br\/>\s*<\/(h1|h2|h3|h4|h5|h6|div|li|p)/gi, "</$1");

  if(browserName == "Opera")
  {
    html = html.replace(/class='([^=*]+)'/gi, "class=\"$1;\"");
    html = html.replace(/style='([^=*]+)'/gi, "style=\"$1;\"");
    html = html.replace(/<[^>]*/gi, function(matchingelements){ return matchingelements.toLowerCase(); });
  }

  if(browserName == "Netscape")
  {
    html=html.replace(/<br style="(\s+|.*)">/gi, "<br style=\"$1\"/>");
    html=html.replace(/<br style="(font-style: italic;\"|font-weight: bold;\"|font-weight: normal;\")( \/?|\/?)>/gi, "<br$2>");
    html=html.replace(/<p style="text-align: (left|center|right|justify);"><br( \/?|\/?)>(.*?|.*\s.*?)<\/p>/gi, "");
    html=html.replace(/<strong>(.*?|\s.*?)<strong>(.*?|\s.*?)<\/strong>(.*?|.*\s*?.*?)<\/strong>/gi, "<strong>$1$2$3</strong>");
    html=html.replace(/<em>(.*?|.*?\s.*?)<em>(.*?|.*?\s.*?)<\/em>(.*?|.*?\s.*?)<\/em>/gi, "<em>$1$2$3</em>");
    html=html.replace(/<strong>(.*?|.*?\s*.*?)<\/strong>/gi, "<span style=\"font-weight: bold;\">$1</span>");
    html=html.replace(/<em>(.*?|.*?\s*.*?)<\/em>/gi, "<span style=\"font-style: italic;\">$1</span>");
  }

    html=html.replace(/<span style="font-weight: normal;">(.*|.*?\s+.*?)<\/span>/gi, '$1');
    html=html.replace(/<span style="(.*?)">( |)<\/span>/gi, '$2');
    html=html.replace(/<\/span>( |)<\/span>/gi, '</span></span>$1');

  if(!browserName == "Netscape" || !browserName == "Mozilla Firefox")
  {
    html=html.replace(/<span style="font-weight: bold;">(.*|\s+.*)<\/span>/gi, '<strong>$1</strong>');
    html=html.replace(/<span style="font-style: italic;">(.*|\s+.*)<\/span>/gi, '<em>$1</em>');
  }


  if(browserName == "Microsoft Internet Explorer")
  {
    html = html.replace(/<[^>]*/gi, function(matchingelements){
    return matchingelements.toLowerCase(); });
  }

   /* some better source code view in the textarea */
    html=html.replace(/<br>/gi, '<br/>\n');
    html=html.replace(/<br(.*)\/>\n/gi, "<br$1/>\r");
    html=html.replace(/<p(.*?)>/gi, '<p$1>\n');
    html=html.replace(/<p(.*?)>\n/gi, '<p$1>\r');
    html=html.replace(/<(\/p.*?)>/gi, '</p>\n');
    html=html.replace(/<(\/p.*?)>\n/gi, '</p>\r');

  return html;

}



/* ---------------------------------------------------------------------- *\
  Function    : viewSource() -- THIS SHOWS THE TEXTAREA AND HIDES THE IFRAME
  Description : Shows the HTML source code generated by the WYSIWYG editor
\* ---------------------------------------------------------------------- */

function viewSource(n)
{
   var textarea=fx.getId(n);
   var wysiwyg=fx.getId("wysiwyg"+n);

   textarea.style.display="block";
   wysiwyg.style.display ="none";
   textareaUpdate(n);


  textarea.value = html;


   fx.getId('HTMLMode'+n).style.display = "none";
   fx.getId('textMode'+n).style.display ="block";

   viewTextMode = 1;

};


/* ---------------------------------------------------------------------- *\
   Function    : viewText() -this shows the WYSIWYG-IFRAME BACK-
   Description : Shows the HTML sourcecode generated by the WYSIWYG editor
\* ---------------------------------------------------------------------- */

function viewText(n)
{
   var textarea = fx.getId(n);
   var wysiwyg = fx.getId("wysiwyg" +n);

   textarea.style.display ='none';
   wysiwyg.style.display = 'block';
   textareaUpdate(n);

   wysiwyg.contentWindow.document.body.innerHTML = textarea.value;

   fx.getId('HTMLMode'+n).style.display = "block";
   fx.getId('textMode'+n).style.display =  "none";

 viewTextMode = 0;

};


/* ---------------------------------------------------------------------- *\
  Function    : updateTextArea(),
  Description : Updates the textarea value before submitting it, with the HTML source that got created
\* ---------------------------------------------------------------------- */
function updateTextArea(n)
{
   var textarea=fx.getId(n);
   var wysiwyg=fx.getId("wysiwyg" + n);

   textareaUpdate(n);

  if(viewTextMode == 0)
  {
      if(browserName == "Netscape")
      {
        var foxhtml = wysiwyg.contentWindow.document.body.innerHTML;
        foxhtml=foxhtml.replace(/<u>(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/u>/gi, '<span style="text-decoration: underline;">$1</span>');
        foxhtml=foxhtml.replace(/<span style="font-weight: bold;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<strong>$1</strong>');
        foxhtml=foxhtml.replace(/<span style="font-style: italic;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<em>$1</em>');
        foxhtml=foxhtml.replace(/<span style="font-style: italic; font-weight:bold;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<strong><em>$1</em></strong>');
        foxhtml=foxhtml.replace(/<span style="font-weight:bold; font-style: italic;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<strong><em>$1</em></strong>');
        textarea.value = foxhtml;
      }
      else
      {
        textarea.value = wysiwyg.contentWindow.document.body.innerHTML;
      }

  viewTextMode = 0;

  }
  else if(viewTextMode == 1)
  {

     if(browserName == "Netscape")
     {
       var foxhtml = textarea.value;
       foxhtml = foxhtml.replace(/<u>(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/u>/gi, '<span style="text-decoration: underline;">$1</span>');
       foxhtml = foxhtml.replace(/<span style="font-weight: bold;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<strong>$1</strong>');
       foxhtml = foxhtml.replace(/<span style="font-style: italic;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<em>$1</em>');
       foxhtml = foxhtml.replace(/<span style="font-style: italic; font-weight:bold;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<strong><em>$1</em></strong>');
       foxhtml = foxhtml.replace(/<span style="font-weight:bold; font-style: italic;">(.*?|.*\s+.*?|.*?\s.*?\s.*?)<\/span>/gi, '<strong><em>$1</em></strong>');
       textarea.value = foxhtml;
     }
     else
     {
      textarea.value = textarea.value;
     }

  viewTextMode = 1;

  }


}


function updateTextAreaOpera(n)
{
   var textarea=fx.getId(n);
   var wysiwyg=fx.getId("wysiwyg" + n);
   textareaUpdate(n);

    if(viewTextMode == 0)
    {
    textarea.value = html;
    }
    else if(viewTextMode == 1)
    {
    wysiwyg.contentWindow.document.body.innerHTML = textarea.value;
    }

}

