//
// ----------------------------------------------------------------------
// Richtext Editor for MD-Pro
// Copyright (C) 2002 by the ProWings Group.
// http://www.drachentasche.de/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// based on:
// Freeware 09.07.02 Open Source writen by ngoCanh 5-2002
// Original by Vietdev  http://vietdev.sourceforge.net
// Release 07.07.2002  R9.0
// ----------------------------------------------------------------------
// Original Author of file: Stefan Koehler
// Purpose of file:  RteMulti Javascript kernels (Editor and toolbars)
// last change:      2002/09/25        WindMeUp   complete re-code
// ----------------------------------------------------------------------
//

// init all setup TEXTAREA in document (global: TASETUP)
function changeSetupTextareaToEditors()
{
//TASETUP = 'storytext,bodytext';
  var ta = new Array();
  ta = TASETUP.split(',');
//        var bd = document.getElementsByTagName("body");
//alert(bd.onload);
//       var comment = new HTMLArea('comment');
//       comment.generate();
//initDocument();

        for(var j=0;j<ta.length;j++)
        {
                if(TASETUP == null) { TASETUP = 'XYZxyz'; }
                  var i=0;

                // Begin while
                  while(document.getElementsByTagName("textarea")[i])
                {
                        var textarea = document.getElementsByTagName("textarea")[i];
                        if(textarea.name == ta[j])
                        {
                                changetoIframeEditor(textarea,i);
                                break;
                        }
                        i++
                   } // End while

        } // End for
};

// Initial function to activate RTE
function changetoIframeEditor(elx,id)
{
/**
   var wi= '', hi= '';
   if(elx.style.height) hiTop= elx.style.height
   else if(elx.rows) hiTop= '90'
   if(elx.style.height) hi=  elx.style.height
   else if(elx.rows) hi= (20*elx.rows+ADDHEIGHT)
   if(elx.style.width) wi= elx.style.width
   else if(elx.cols) wi= (4*elx.cols+ADDWIDTH)
   if(wi < 540) wi = '540'
   var parent= elx.parentElement
   while(parent.tagName != 'FORM') parent= parent.parentElement
   var oform= parent
   var fidx=0; while(document.forms[fidx] != oform) fidx++ ; // form index

   var val='';
   if(elx.tagName=='TEXTAREA'){ fID= fidx+'VDevID'+elx.name; val= elx.innerText; }
   else {fID= fidx+'VDevID'+elx.id;}
**/

//initDocument();
//        BAR1 = [1,2,3];
//        BAR2 = [4,5,6,7,8];
//        BAR3 = [9,10];
        /* We must have a minimum setup for toolbar1 */
        if(typeof BAR1 == "undefined") {
                BAR1 = [11];
        }
        if(typeof BAR2 == "undefined") {
                BAR2 = [" "];
        }
        if(typeof BAR3 == "undefined") {
                BAR3 = [" "];
        }
        BLOCK = new Array();
        BLOCK[1]  = ["htmlmode", "separator", "cut", "copy", "paste", "separator", "selectall", "removeformat", "wordclean"];
        BLOCK[2]  = ["inserttable", "toggleborders", "insertimage", "insertfile", "createlink", "unlink"];
        BLOCK[3]  = ["showhelp"/**, "about"  , "popupeditor" **/];
        BLOCK[4]  = ["bold", "italic", "underline", "strikethrough", "subscript", "superscript"];
        BLOCK[5]  = ["justifyleft", "justifycenter", "justifyright", "justifyfull"];
        BLOCK[6]  = ["righttoleft", "separator", "template"];
        BLOCK[7]  = ["orderedlist", "unorderedlist", "outdent", "indent", "separator", "horizontal"];
        BLOCK[8]  = ["undo", "redo"];
        BLOCK[9]  = ["formatblock", "separator", "fontname", "separator", "fontsize"];
        BLOCK[10]  = ["forecolor", "hilitecolor", "separator", "textindicator", "separator", "bgcolor", "bgimage"];
	BLOCK[11]  = ["htmlmode", "wordclean", "barstart", "showhelp", "about"];

    var TOOLBAR1 = [];
                for(var i in BAR1) {
                        for(var j in BLOCK[BAR1[i]]) {
                                TOOLBAR1 = TOOLBAR1.concat(BLOCK[BAR1[i]][j]);
                        }
                }
    var TOOLBAR2 = [];
                for(var i in BAR2) {
                        for(var j in BLOCK[BAR2[i]]) {
                                TOOLBAR2 = TOOLBAR2.concat(BLOCK[BAR2[i]][j]);
                        }
                }
    var TOOLBAR3 = [];
                for(var i in BAR3) {
                        for(var j in BLOCK[BAR3[i]]) {
                                TOOLBAR3 = TOOLBAR3.concat(BLOCK[BAR3[i]][j]);
                        }
                }
        var strx = new Array();
       strx[id] = new HTMLArea(elx.name);
/* To build own toolbars using this: */
           strx[id].config.toolbar = [
                        TOOLBAR1, TOOLBAR2, TOOLBAR3
                 ];

/**/
           strx[id].config.hideSomeButtons(OPTIONS);
           strx[id].registerPlugin(InsertFile);
//           strx[id].config.toolbar.push(["linebreak", "barstart", "bgcolor", "bgimage"]); // add the new button to the toolbar
        if(VISUALBOX == 1) {
       strx[id].registerPlugin(ContextMenu);

        }
        var reg= eval("/inserttable/gi") ;
           if( !reg.test(OPTIONS) ) {
       strx[id].registerPlugin(TableOperations);
    }
           strx[id].generate();
};

function ccParser(html)
{

//alert("Source: "+html);
        html = html.replace(/@/gi,"_AT_");
        html = html.replace(/#/gi,"_HASH_");
        html = html.replace(/%/gi,"_PERCENT_");
        html = html.replace(/</gi,"_LOWER_");
        html = html.replace(/>/gi,"_GREATER_");
//        html = html.replace(/%/gi,"_PERCENT_");
//        html = html.replace(/%/gi,"_PERCENT_");
        html = html.replace(/&amp;nbsp;/gi,"&nbsp;");
        //html = html.replace(/<br />/gi,"");

        //var htmltag = /(&lt;[\w\/]+[ ]*[\w\=\"\'\.\/\;\: \)\(-]*&gt;)/gi;
                  var htmltag =   /&lt;([\w\/]+[ ]*)(.)*?&gt;/gi;
                  //var htmltag =   /&lt;([\w\/])(.|\n)*?&gt;(.|\n)*?&lt;\/\1&gt;/gi;
        html = html.replace(htmltag,"<span class=ccp_tag>$&</span>");

        var imgtag = /<span class=ccp_tag>(&lt;IMG[ ]*[\w\=\"\'\.\/\;\: \)\(-]*&gt;)<\/span>/gi;
//--Farbe        html = html.replace(imgtag,"<span style='color = #800080'>$1</span>");
        html = html.replace(imgtag,"<span style='color:rgb(128,0,128)'>$1</span>");

        var spantag = /<span class=ccp_tag>(&lt;[\/]*(span|SPAN){1}[ ]*[\w\=\"\'\.\/\;\:\)\(-]*&gt;)<\/span>/gi;
//--Farbe        html = html.replace(imgtag,"<span style='color = #800080'>$1</span>");
        html = html.replace(spantag,"<span style='color:rgb(128,0,128)'>$1</span>");

        var brtag = /<span class=ccp_tag>(&lt;BR[ ]*[\w\=\"\'\.\/\;\: \)\(-]*&gt;)<\/span>/gi;
//--Farbe        html = html.replace(brtag,"<span style='color = #FF0000'>$1</span>");
        html = html.replace(brtag,"<span style='color:rgb(255,0,0)'>$1</span>");

        var formtag = /<span class=ccp_tag>(&lt;[\/]*(form|input|font){1}[ ]*[\w\=\"\'\.\/\;\: \)\(-]*&gt;)<\/span>/gi;
//--Farbe        html = html.replace(formtag,"<span style='color = #FF9900'>$1</span>");
        html = html.replace(formtag,"<span style='color:rgb(255,153,0)'>$1</span>");

        var tabletag = /<span class=ccp_tag>(&lt;[\/]*(table|tbody|th|tr|td){1}([ ]*[\w\=\"\'\.\/\;\:\)\(-]*){0,}&gt;)<\/span>/gi;
//--Farbe        html = html.replace(tabletag,"<span style='color = #008080'>$1</span>");
        html = html.replace(tabletag,"<span style='color:rgb(0,128,128)'>$1</span>");

        var chartag = /<span class=ccp_tag>(&lt;[\/]*(b|i|u|strike|strong|STRONG){1}([ ]*[\w\=\"\'\.\/\;\:\)\(-]*){0,}&gt;)<\/span>/gi;
//--Farbe        html = html.replace(chartag,"<span style='color = #000099; font-weight=bold;'>$1</span>");
        html = html.replace(chartag,"<span style='color:rgb(0,0,153); font-weight=bold;'>$1</span>");

        var listtag = /<span class=ccp_tag>(&lt;[\/]*(ul|ol|li){1}([ ]*[\w\=\"\'\.\/\;\:\)\(-]*){0,}&gt;)<\/span>/gi;
//--Farbe        html = html.replace(listtag,"<span style:'color = #993300'>$1</span>");
        html = html.replace(listtag,"<span style='color:rgb(153,51,0)'>$1</span>");

        var crtag = /<span class=ccp_tag>(&lt;[\/]*(p){1}([ ]*[\w\=\"\'\.\/\;\:\)\(-]*){0,}&gt;)<\/span>/gi;
//--Farbe        html = html.replace(crtag,"<span style='color = #FF0000'>$1</span>");
        html = html.replace(crtag,"<span style='color:rgb(255,0,0)'>$1</span>");

        //--> für Gecko: var Atag = /<span class=ccp_tag>(&lt;a [\W _\w\=\"\'\.\/\;\:\)\(-]+&gt;){1,}<\/span>/gi;
        //--> für MSIE: var Atag = /<span class=ccp_tag>(&lt;[\/]*(a){1}([ ]*[\w\=\"\'\.\/\;\:\)\(/\?\&-]*){0,}&gt;)<\/span>/gi;
        var Atag = /<span class=ccp_tag>(&lt;[\/]*(a|A){1}([ ]*[\w\=\"\'\.\/\;\:\)\(\?\&-]*){0,}&gt;)<\/span>/gi;
//--Farbe        html = html.replace(Atag,"<span style='color=#008000'>$1</span>");
        html = html.replace(Atag,"<span style='color:rgb(0,128,0)'>$1</span>");

        var Parameter = /=("[ _\w\'\.\/\;\:\)\(\?\&\=-]*"|'[ _\w\"\.\/\;\:\)\(\?\&\=-]*')/gi;
//--Farbe        html = html.replace(Parameter,"=<span style='color=#000066';>$1</span>");
        html = html.replace(Parameter,"=<span style='color:rgb(0,0,102)'>$1</span>");

        var nbspentity = /&amp;nbsp;/gi;
//--Farbe        html = html.replace(nbspentity,"<span style='color=#999999; font-weight=bold;'>&nbsp;</span>");
        html = html.replace(nbspentity,"<span style='color:rgb(153,153,153); font-weight=bold;'>&amp;nbsp;</span>");

        var ampentity = /&amp;amp;/gi;
//--Farbe        html = html.replace(ampentity,"<span style='color=#999999'>&amp;</span>");
        html = html.replace(ampentity,"<span style='color:rgb(153,153,153)'>&</span>");

        var ampentity = /&amp;lt;/gi;
//--Farbe        html = html.replace(ampentity,"<span style='color=#999999'>&lt;</span>");
        html = html.replace(ampentity,"<span style='color:rgb(153,153,153)'>&lt;</span>");

        var ampentity = /&amp;gt;/gi;
//--Farbe        html = html.replace(ampentity,"<span style='color=#999999'>&gt;</span>");
        html = html.replace(ampentity,"<span style='color:rgb(153,153,153)'>&gt;</span>");

        var comment = /(&lt;\!--[\W _\w\=\"\'\.\/\;\:\)\(-]*--&gt;)/gi;
//--Farbe        html = html.replace(comment,"<span style='color = #999999';>$1</span>");
        html = html.replace(comment,"<span style='color:rgb(153,153,153)'>$1</span>");

//alert("Converted: "+html);

        html = html.replace(/_AT_/gi,"@");
        html = html.replace(/_HASH_/gi,"#");
        html = html.replace(/_PERCENT_/gi,"%");
        html = html.replace(/_GREATER_/gi,">");
        html = html.replace(/_LOWER_/gi,"<");
        html = html.replace(/&nbsp;/gi," ");

alert("Converted: "+html);

        return (html);
};

function findText(TXTOBJ)
{
  TXTOBJ._doc.focus();
  var urlx = QBPATH + '/dfindtext.php?curlang='+LANG;

  var newWin=window.open(urlx,"find","toolbar=no, width=350px,height=220px,directories=no,status=yes,scrollbars=yes,resizable=yes,menubar=yes;scroll=no");
  obj = TXTOBJ;
  newWin.moveTo(screen.width-500,50);
  //reset()
  newWin.focus();
};

function findTextHotKey(TXTOBJ,forward)
{
  var el= TXTOBJ;
  el.focus();

  var rng = el.document.selection.createRange();
  el.curword=rng.duplicate();

  if(!FWORD && !el.curword.text ){ alert('No find string definition'); return; }
  else if(el.curword.text)FWORD= el.curword.text;

  if(el.curword.text)
   {
     if(forward==1) rng.moveEnd("character", -1 );
         else rng.moveStart("character", 1);
   }

  if(rng.findText(FWORD,100000,FLAGS+forward)==true)
   { rng.select();  rng.scrollIntoView(); return; }

  alert("Finish");
  return;

};

function highLight(key)
{
  if(!fID && !TXTOBJ){alert('Please click to select the editor');return}
  if(fID) el= document.frames[fID]
  else el= TXTOBJ;
  objF = el.document;

  function doDefFormat()
        {
     var el= document.frames[fID]
     el.focus();
         var rng = el.document.selection.createRange();
     rng.moveEnd("character", 1);
         rng.select();
         el.curword=rng.duplicate();
         if(el.curword.text=='') doFormatHot('RemoveFormat',objF);
         else
          {
            rng.moveEnd("character", -1);
               rng.select();
                doFormatHot('ForeColor,',objF); doFormatHot('BackColor,',objF);
          }
    }

  switch(key)
        {
          case 48: doDefFormat(); break; // ctrl+0  no highlight
          case 49: doFormatHot('ForeColor,red',objF); break; // ctrl+1
          case 50: doFormatHot('ForeColor,green',objF); break; // ctrl+2
          case 51: doFormatHot('ForeColor,blue',objF); break; // ctrl+3
      case 52: doFormatHot('ForeColor,#00AAFF',objF); break; // ctrl+4
      case 53: doFormatHot('ForeColor,magenta',objF); break; // ctrl+5
          case 54: doFormatHot('BackColor,yellow',objF); doFormatHot('ForeColor,black',objF); break; // ctrl+6
          case 55: doFormatHot('BackColor,cyan',objF); doFormatHot('ForeColor,black',objF); break; // ctrl+7
          case 56: doFormatHot('BackColor,#00FF00',objF); doFormatHot('ForeColor,black',objF); break; // ctrl+8
          case 57: doFormatHot('BackColor,#FF00AA',objF); doFormatHot('ForeColor,white',objF); break; // ctrl+9
    }
};

function displayHelp()
{
  var urlx= QBPATH + '/edithelp.php?curlang='+LANG;

  var newWin=window.open(urlx,"help","toolbar=no, width=600px,height=400px,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no;scroll=no");
  reset();
  newWin.focus();
};



// VISUAL=0 : Textarea to Editor after confirmation

// VISUAL=1 : all Textarea to Editor
if(VISUAL==1) changeAllTextareaToEditors();

// VISUAL=2 : some Textarea to Editor (setup in global: TASETUP)
if(VISUAL==2) changeSetupTextareaToEditors(TASETUP);

// VISUAL=3 : all Iframe to Editor
if(VISUAL==3) changeAllIframeToEditors();

// VISUAL=4 : change only specific textarea
if(VISUAL==4) changetoIframeEditor(document.forms[xxx].yyy); // please replace xxx=formIndex and yyy=textareaName

// VISUAL=5 : some specific iframes
if(VISUAL==5) changeIframeToEditor('contents1','contents'); // please replace contents.. = frameId

// VISUAL>5 : no Visual-Editor, only use Rightmouse-Control

