//      var defwidth  = <?PHP echo $pwidth ?>;
//      var defheight = <?PHP echo $pheight ?>;
      var defwidth  = 300;
      var defheight = 200;


//
//    for NN 4.x
//    eg.: width="&{tw(0)};"
//    to omit NN 1 pixel border call with neg values
//
      function th (diff) {
        x = false;
        if (document.layers) {
          x = window.innerHeight - diff;
        }
        return x;
      };
      function tw (diff) {
        x = false;
        if (document.layers) {
          x = window.innerWidth  - diff;
        }
        return x;
      };

      function showpic (f, pname, path, max) {
        var h = f.options [f.selectedIndex].value;
        if (!h)
          h = f.options [f.selectedIndex].text;
        if (f.selectedIndex == f.options.length - 1) {
          document.images [pname].src = "fil.gif";
          return;
        }
        if (String.fromCharCode) {
          var h2 = f.options [f.selectedIndex].text;
          var s = String.fromCharCode (160);
          var m = h2.substr (h2.indexOf (s) + 1);
//        alert (h2 + "#" +h2.indexOf (s) + "#" + s + "#" + m + "#");
//      nbsp; wird zu Char (160);
          m = m.match ('[0-9]+x[0-9]+');
          if (m) {
            m = m[0].split ("x");
            var pw = Number (m [0]);
            var ph = Number (m [1]);
            document.images [pname].pwidth  = pw;
            document.images [pname].pheight = ph;
          } else {
            document.images [pname].pwidth  = 0;
            document.images [pname].pheight = 0;
          }
        }
        if (!document.images [pname].ppw) {
          var p = document.images [pname].offsetParent;
          if (p.tagName != 'TD')
            p = p.offsetParent;
          document.images [pname].ppw = p.offsetWidth;
          document.images [pname].pph = p.offsetHeight;
        }

        document.images [pname].src = path + h;
        if (pw) {
          var nw = pw;
          var nh = ph;
          if (pw > document.images [pname].ppw || ph > document.images [pname].pph) {
            if (pw / ph > document.images [pname].ppw / document.images [pname].pph) {
              nw = document.images [pname].ppw;
              nh = Math.round (ph * document.images [pname].ppw / pw);
            } else {
              nw = Math.round (pw * document.images [pname].pph / ph);
              nh = document.images [pname].pph;
            }
          }
          document.images [pname].width   = nw;
          document.images [pname].height  = nh;
//          window.status = pw + 'x' + ph;
        }
      }

      var newwin;
      var cnt = 0;
      function popup (name) {
        var url = document.images ["p" + name].src;
        if (url.indexOf ("fil.gif") >= 0)
          return;
        if (!document.images ["p" + name].pwidth) {
          var width   = defwidth;
          var height  = defheight;
          var pwidth  = "100%";
          var pheight = "100%";
        } else {
          var width   = document.images ["p" + name].pwidth;
          var height  = document.images ["p" + name].pheight;
          var pwidth  = width;
          var pheight = height;
        }
        cnt++;
        var pname = "ppopup" + cnt;
        var picname = url.substr (url.lastIndexOf ('/') + 1);
        var w = "";
        if (x) {
          var sx = Math.max (0, Math.round ((x - width) / 2));
          var sy = Math.max (0, Math.round ((y - height) / 2));
          width = Math.min (width, x - 12);
          height = Math.min (height, y - 12);
          w = ",screenX=" + sx + ",left=" + sx + ",screenY=" + sy + ",top=" + sy;
        }
        var f =
          "width=" + width + ",height=" + height + w +
          ",toolbar=0,menubar=0,location=0,status=0,resizable=1,scrollbars=0";
//        alert (url + "#" + name + "#" + pname + "#" + f);
        newwin = window.open ("", pname, f);
        var s =
          '<HTML>\n' +
          '<HEAD>\n' +
          '<TITLE>' + picname + '</TITLE>\n' +
          '</HEAD>\n' +
          '<BODY MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0">\n' +
          '<IMG SRC="' + url + '" WIDTH="'+ pwidth + '" HEIGHT="' + pheight + '" BORDER="0" ALT="">' +
          '</BODY>' +
          '</HTML>';
          newwin.document.write (s);
          newwin.document.close ();
        if(newwin.focus) {
          setTimeout ('newwin.focus()', 200);
        }
      }
      function ipopup (url, width, height) {
        if (url == '')
          return false;
        var pwidth  = width;
        var pheight = height;
        cnt++;
        var pname = "ppopup" + cnt;
        var picname = url.substr (url.lastIndexOf ('/') + 1);
        var w = "";
        if (x) {
          var sx = Math.max (0, Math.round ((x - width) / 2));
          var sy = Math.max (0, Math.round ((y - height) / 2));
          width = Math.min (width, x - 12);
          height = Math.min (height, y - 12);
          w = ",screenX=" + sx + ",left=" + sx + ",screenY=" + sy + ",top=" + sy;
        }
        var f =
          "width=" + width + ",height=" + height + w +
          ",toolbar=0,menubar=0,location=0,status=0,resizable=1,scrollbars=0";
//        alert (url + "#" + name + "#" + pname + "#" + f);
        newwin = window.open ("", pname, f);
        var s =
          '<HTML>\n' +
          '<HEAD>\n' +
          '<TITLE>' + picname + '</TITLE>\n' +
          '</HEAD>\n' +
          '<BODY MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0">' +
          '<IMG SRC="' + url + '" WIDTH="'+ pwidth + '" HEIGHT="' + pheight + '" BORDER="0" ALT="' + url + '">' +
          '</BODY>\n' +
          '</HTML>';
          newwin.document.write (s);
          newwin.document.close ();
        if(newwin.focus) {
          setTimeout ('newwin.focus()', 200);
        }
        return false;
      }

      function fpopup (name) {
        cnt++;
        var pname = "fpopup" + cnt;
        var width = x - 200;
        var height = y - 200;
        if (x) {
          var sx = Math.max (0, Math.round ((x - width) / 2));
          var sy = Math.max (0, Math.round ((y - height) / 2));
          width = Math.min (width, x - 12);
          height = Math.min (height, y - 12);
          w = ",screenX=" + sx + ",left=" + sx + ",screenY=" + sy + ",top=" + sy;
        }
        var f =
          "width=" + width + ",height=" + height + w +
          ",toolbar=0,menubar=0,location=0,status=0,resizable=1,scrollbars=0";
        newwin = window.open (name, pname, f);
//        if(newwin.focus) {
//          setTimeout ('newwin.focus()', 200);
//        }
      }

      function mpopup (url, name, width, height, scrollBars) {
//        cnt++;
//        name = name + cnt;
        var w = "";
        if (x) {
          var sx = Math.round ((x - width) / 2);
          var sy = Math.round ((y - height) / 2);
          w = ",screenX=" + sx + ",left=" + sx + ",screenY=" + sy + ",top=" + sy;
        }
        if (!scrollBars)
          scrollBars = '0';
        else
          scrollBars = '1';
        var f =
          "width=" + width + ",height=" + height + w +
          ",toolbar=0,menubar=0,location=0,status=0,resizable=1,scrollbars=" + scrollBars;
//        alert (url + ", " + name + ", " + f);
        newwin = window.top.open (url, name, f);
        if (window.focus) {
          newwin.focus ();
          window.setTimeout ('newwin.focus()', 200);
        }
        return false;
      }
      var x = 0;
      var y = 0;
      if (window.screen && window.screen.availWidth) {
        var x = window.screen.availWidth;
        var y = window.screen.availHeight;
      }
      function toHex (v, vk) {
        var z = "0123456789ABCDEF";
        var h = "";
        while (vk-- || v) {
          h = z.substr ((v & 15), 1) + h;
          v >>= 4;
        }
        return h;
      }
      function toDez (v) {
        return eval ("0x" + v);
      }
      function xrgb (red, green, blue) {
        return "#" + toHex ((red * 256 + green) * 256 + blue, 6);
      }
      function Red (hexColor) {
        return (toDez (hexColor.substr (1)) & 0xFF0000) >> 16;
      }
      function Green (hexColor) {
        return (toDez (hexColor.substr (1)) & 0xFF00) >> 8;
      }
      function Blue (hexColor) {
        return (toDez (hexColor.substr (1))) & 0xFF;
      }
      function NextColor (c) {
        if (!isNaN) {
          return "";
        } else {
          if (isNaN ("0x" + c.substr (1))) {
//          alert ("not a number");
            c = "#000000";
          }
        }
        var r = Red (c);
        var g = Green (c);
        var b = Blue (c);
        r = Math.round (r / 51) * 51;
        g = Math.round (g / 51) * 51;
        b = Math.round (b / 51) * 51;
        nc = xrgb (r, g, b);
//        alert (c + " " + nc);
        return nc;

      }
      var cnt = 0;

      function ShowColors (fname, form) {
        var rgb = new Array ('00', '33', '66', '99', 'CC', 'FF');
        var pname = "cpopup" + cnt;
        var width  = 12 * 15;
        var height = 18 * 10;
        var w = "";
        var nextvalue = document.forms [form].elements ['NewValues[' + fname + ']'].value;
        nextvalue = NextColor (nextvalue);
        if (x) {
          var sx = Math.max (0, Math.round ((x - width) / 2));
          var sy = Math.max (0, Math.round ((y - height) / 2));
          width = Math.min (width, x - 12);
          height = Math.min (height, y - 12);
          w = ",screenX=" + sx + ",left=" + sx + ",screenY=" + sy + ",top=" + sy;
        }
        var f =
          "width=" + width + ",height=" + height + w +
          ",toolbar=0,menubar=0,location=0,status=0,resizable=1,scrollbars=0";
        newwin = window.open ("fil.gif", pname, f);
        s = '<HTML>\n';
        s += '<HEAD>\n';
        s += '<TITLE>Colors<\/TITLE>\n';
        s += '<SCRIPT LANGUAGE="JavaScript">\n';
        s += '  var f    = opener.document.forms [' + form + '].elements [\'NewValues[' + fname + ']\'];\n';
        s += '  var ov   = f.value;\n';
        s += '  var done = false;\n';
        s += '  function ul (co) {\n';
        s += '    f.value = co;\n';
        s += '    f.onchange ();\n';
        s += '    return false\n';
        s += '  }\n';
        s += '<\/SCRIPT>\n';
        s += '<\/HEAD>\n';
        s += '<BODY onUnload="if (!done) {f.value=ov;f.onchange();}" MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0">\n';
        s += '<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">\n';
        var s1 = '<TD BGCOLOR="';
        var s2 = '"><A href="#" onclick="done=true;window.close()" ONMOUSEOVER="ul (\'';
        var s3 = '\'); return false"><IMG SRC="fil.gif" WIDTH="15" HEIGHT="10" BORDER="0"><\/A><\/TD>\n';
        var jetzt = new Date ();
        var nvi = false;
        for (i = 0; i < 18; i++) {
          s += '<TR>\n';
          for (j = 0; j < 12; j++) {
            r = i % 6;
            g = j % 6;
            b = Math.floor (i / 6) * 2 + Math.floor (j / 6);
            h = "#" + rgb [r] + rgb [g] + rgb [b];
            if (h == nextvalue)
              nvi = i * 12 + j;
            s += s1 + h +  s2 + h + s3;
          }
          s += '<\/TR>\n';
        }
        s += '<\/TABLE>\n';
        var ujetzt = new Date ();
//        s += ujetzt.getTime() - jetzt.getTime();
        s += '<\/BODY>\n';
        s += '<\/HTML>';
//        newwin.document.clear();
        newwin.document.open('text/html', 'replace');
//        newwin.document.close ();
//        newwin.document.clear ();
        newwin.document.write (s);
        newwin.document.close ();
//      NN Patch
        if (document.layers) {
          newwin.resizeBy (1, 0);
          newwin.resizeBy (-1, 0);
        }
        if(newwin.focus)
          setTimeout ('newwin.focus()', 300);
        if (nvi)
          setTimeout ('newwin.document.images [' + nvi + '].src = "colsel.gif";', 320);
      }

      var oldcolor = 0;
      function RefreshColor (inp, id) {
        id = 'cf' + id;
        var color = inp.value;
        if (!oldcolor)
          oldcolor = color;
        if (document.layers)
          document[id].bgColor = color == 'transparent' ? null : color;
        else if (document.all)
          document.all[id].style.backgroundColor = color;
        else if (document.getElementById)
          document.getElementById(id).style.backgroundColor = color;
      }

      function ResizeToContent () {
        if (window.opera || (!document.all && !document.getElementById))
          return false;

        var fixedW = 370;
        var fixedH = 300;
        window.resizeTo (fixedW, fixedH);

        var myWidth  = 0;
        var myHeight = 0;
        if (typeof (window.innerWidth) == 'number') {
          //Non-IE
          myWidth = window.innerWidth;
          myHeight = window.innerHeight;
        } else {
          if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
          } else {
            if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
              //IE 4 compatible
              myWidth   = document.body.clientWidth;
              myHeight  = document.body.clientHeight;
            }
          }
        }
        if (document.all) {
          var cWidth  = tb.offsetWidth;
          var cHeight = tb.offsetHeight;
        } else {
          var cWidth  = document.getElementById ("tb").offsetWidth;
          var cHeight = document.getElementById ("tb").offsetHeight;
        }
        if (cWidth + fixedW - myWidth < 200 || cHeight + fixedH - myHeight < 200) {
//          alert ("error " + cHeight + " " + fixedH + " " + myHeight + ", " + cWidth + " " + fixedW + " " + myWidth);
        } else {
          window.resizeTo (cWidth + fixedW - myWidth, cHeight + fixedH - myHeight);
//          alert ("resized " + cHeight + " " + fixedH + " " + myHeight + ", " + cWidth + " " + fixedW + " " + myWidth);
        }
        return true;
      }

      function update (eddion, loc) {
        if (!opener || opener.closed)
          return false;
        if (loc.substr(0, 1) == '?')
          opener.top.location.search  = loc + (eddion ? "&EditOn=true" : "");
        else
          opener.top.location = loc + (eddion ? "&EditOn=true" : "");
        return true;
      }

      function Reload () {
        history.go (0);
      }

      function Init () {
        return false;
//        var h = "" + location.search;
//
//      no more - patch opera td 100% bahaviour
//
        if (window.opera && document.all ['td'] && document.all ['td'].style) {
//          window.onresize = Reload;
          var newsize =
            2 * document.all ['td'].offsetHeight - document.all ['tb'].offsetHeight;
            alert (window.resizing + " " + newsize + " " + document.getElementById ('td').offsetHeight + " " + document.getElementById ('tb').offsetHeight);
          document.all ['td'].style.height = newsize;
        }
//      no need when textarea with height of 100%
//        if (h.indexOf ("Resized=true") < 0) {
//          ResizeToContent ();
//        }
        return false;
      }
      function moveOpt (sel, xup) {

        if (!sel) {
          alert ("kein Eintrag ausgewählt " + sel)
          return false;
        }

        var opts = sel.options;
        if (sel.selectedIndex < 0) {
          alert ("kein Eintrag ausgewählt")
          return false;
        }

        var opts = sel.options;
        var j, s, h;
        if (xup > 0)
          s = opts.length - 2;
        else
          s = 1;
        if (xup == 0) {
          j   = 0;
          xup = -1;
          s   = sel.selectedIndex;
        } else
          j = s + xup;
        for (var i = s; i >= 0 && i < opts.length; i = i - xup) {
          if (opts [i].selected && !opts[j].selected && j >= 0 && j < opts.length) {
            hv = opts [j].value;
            ht = opts [j].text;
            hs = opts [j].selected;
            opts [j].value    = opts [i].value;
            opts [j].text     = opts [i].text;
            opts [j].selected = opts [i].selected;
            opts [i].value    = hv;
            opts [i].text     = ht;
            opts [i].selected = hs;
          }
          j = j - xup;
        }
        return false;
      }

      function moveOptTop (sel) {

        if (!sel) {
          alert ("nichts ausgewählt " + sel)
          return false;
        }

        var opts = sel.options;
        if (sel.selectedIndex < 0) {
          alert ("nichts ausgewählt ")
          return false;
        }

        var i      = opts.length - 1;
        var j      = i;
        var texts  = new Array();
        var values = new Array();

        while (i >= 0) {

          if (j < 0) {
    //        alert ("restore " + i + " with saved " + (-j - 1));
            opts [i].value    = values [-j - 1];
            opts [i].text     = texts  [-j - 1];
            opts [i].selected = true;
            i--;
            j--;
            continue;
          }

          if (opts [j].selected) {
    //        alert ("save " + j + " into " + (i - j));
            texts  [i - j] = opts [j].text;
            values [i - j] = opts [j].value;
            j--;
            continue;
          }

          if (i > j) {
    //        alert ("shift " + j + " to " + i);
            opts [i].value    = opts [j].value;
            opts [i].text     = opts [j].text;
            opts [i].selected = false;
          }

          i--;
          j--;
        }

        return false;
      }

      function wiki_popup (url, name, width, height, alt) {
        if (url == '')
          return false;
        var pwidth  = width;
        var pheight = height;
        cnt++;
        var pname = "ppopup" + cnt;
        var w = "";
        if (!alt)
          alt = url;
        if (x) {
          var sx = Math.max (0, Math.round ((x - width) / 2));
          var sy = Math.max (0, Math.round ((y - height) / 2));
          width = Math.min (width, x - 12);
          height = Math.min (height, y - 12);
          w = ",screenX=" + sx + ",left=" + sx + ",screenY=" + sy + ",top=" + sy;
        }
        var f =
          "width=" + width + ",height=" + height + w +
          ",toolbar=0,menubar=0,location=0,status=0,resizable=1,scrollbars=0";
//        alert (url + "#" + name + "#" + pname + "#" + f);
        newwin = window.open ("", pname, f);

        var s =
          '<html>\n' +
          '<head>\n' +
          '<title>' + name + '</title>\n' +
          '<style type="text/css"><!--' + '\n' +
            'html, body {overflow: hidden; margin: 0; padding: 0}' + '\n' +
          '--></style>\n' +
/*
//        Safari title bug

          '<script type="text/javascript"><!--' + '\n' +
            'window.onload = function () {alert (\'hallo\'); document.title = \'hallo\'}' + '\n' +
          '//--></script>\n' +
*/
          '</head>\n' +
          '<body>' +
          '<img src="' + url + '" width="'+ pwidth + '" height="' + pheight + '" border="0" alt="' + alt + '">' +
          '</body>\n' +
          '</html>';

          newwin.document.write (s);
          newwin.document.close ();
        if(newwin.focus) {
/*
          newwin.showTitle = function () {
            alert ('loaded');
            this.document.title = 'hallo ' + name;
          }
*/
          setTimeout ('newwin.focus()', 100);
        }
        return false;
      }

      function external_document_write (s) {
        document.write (s);
      }

      var x = 0;
      var y = 0;
      if (window.screen && window.screen.availWidth) {
        var x = window.screen.availWidth;
        var y = window.screen.availHeight;
      }

      function CookieSave (item, value) {
        if (value) {
          var expires = '';
        } else {
          var expires = new Date ();
          expires = '; expires=' + new Date (new Date ().getTime () + 1000*60*60*24*365).toGMTString() + ';';
        }
        document.cookie = item + '=' + value + expires;
//        alert (document.cookie);
        return true;
      }

      function CookieGet (item) {
//        alert ('Cookie ' + document.cookie);
        var found = false;
        var pars = document.cookie.split (';');
        for (var i = 0; i < pars.length; i++) {
          if (pars [i].indexOf (item + '=') == 0) {
            found = pars [i].split ('=');
            found = found [1];
            break;
          }
        }
        return found;
      }
/*    ----------------------------------------------------------------------------
      Wiki Editor TEXTAREA
      ---------------------------------------------------------------------------- */
      
      function switchFont (ele) {
        if (!ele.fontSwitched) {
          ele.fontSwitched = true;
          ele.style.fontFamily = 'Arial, Helvetica, sans-serif';
        } else {
          ele.fontSwitched = false;
          ele.style.fontFamily = 'Lucida Console, Courier New, monospaced';
        }
//      save cookie
        CookieSave ('eddiFontSwitched', ele.fontSwitched ? 'true' : '');
        return true;
      }

      function switchWrap (ele) {
        if (!ele.wrapOff) {
          ele.wrapOff = true;
          if (ele.setAttribute && !document.all) {
            ele.setAttribute ("wrap", "off");
            var parNod = ele.parentNode, nxtSib = ele.nextSibling;
            parNod.removeChild(ele);
            parNod.insertBefore (ele, nxtSib);
          } else {
            ele.saveWrap = ele.wrap;
            ele.wrap = 'off';
          }
        } else {
          ele.wrapOff = false;
          if (ele.setAttribute && !document.all) {
            ele.setAttribute ("wrap", 'virtual');
            var parNod = ele.parentNode, nxtSib = ele.nextSibling;
            parNod.removeChild(ele); parNod.insertBefore(ele, nxtSib);
          } else {
            ele.wrap = ele.saveWrap;
          }
        }
//      save cookie
        CookieSave ('eddiWrapOff', ele.wrapOff ? 'true' : '');
        return true;
      }

      function showSpacing (ele) {
        ele.oldTop  = ele.scrollTop;
        ele.oldLeft = ele.scrollLeft;
        window.status = ele.oldLeft + ' ' + ele.oldTop;
        ele.pos = getInputSelection (ele);
        ele.value = ele.value.replace (/ /g, '·');
        if (document.all)
          ele.value = ele.value.replace (/\r\n/g, '¶\r\n');
        else
          ele.value = ele.value.replace (/\n/g, '¶\n');
        setInputSelection(ele, ele.pos.start, ele.pos.end);
        ele.scrollTop = ele.oldTop;
        ele.scrollLeft = ele.oldLeft;
      }
      function hideSpacing (ele) {
        ele.pos = getInputSelection (ele);
        ele.value = ele.value.replace (/·/g, ' ');
        if (document.all)
          ele.value = ele.value.replace (/¶\r\n/g, '\r\n');
        else
          ele.value = ele.value.replace (/¶\n/g, '\n');
        setInputSelection(ele, ele.pos.start, ele.pos.end);
        ele.scrollTop = ele.oldTop;
        ele.scrollLeft = ele.oldLeft;
      }

      function txkeydown (evt) {
        evt = evt || window.event;
        window.status = 'Code ' + evt.keyCode;
        if (evt.keyCode == 9) {
          this.pos = getInputSelection (this);
          insertAtCaret (this, '  ');
          setInputSelection(this, this.pos.start, this.pos.end);
          return false;
        }
/*
        window.status = 'keydown';
        if (!ele.saveValue) {
          ele.saveValue = ele.value;
          ele.saveScrollTop = ele.scrollTop;
          ele.saveScrollLeft = ele.scrollLeft;
          ele.scrollTop = ele.saveScrollTop;
          ele.scrollLeft = ele.saveScrollLeft;
        }
*/
      }

      function tabHandleKeyDown(evt) {
        var e = window.event || evt;
        var k = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
        
        /* no tab key -> exit */
        if (k != 9 || e.ctrlKey || e.altKey) {
          return;
        }

        /* my tabs are 2 spaces */
        var tab = '  ';
        var deIndent = e.shiftKey;
        var t = e.target ? e.target : e.srcElement ? e.srcElement : e.which;
        var scrollTop = t.scrollTop;
        var scrollLeft = t.scrollLeft;

        if (typeof t.getSelectionRange == 'undefined') {
          t.getSelectionRange = function () {
            if (typeof this.selectionStart !== 'undefined') {
              return {
                selectionStart: this.selectionStart,
                selectionEnd:   this.selectionEnd
              }
            }
//            this.focus ();

            var pos = getInputSelection (this);
            return {
              selectionStart: pos.start,
              selectionEnd:   pos.end
            };
          }
        }
        if (typeof t.setSelectionRange == 'undefined') {
          t.setSelectionRange = function (s, e) {
            setInputSelection(this, s, e);
            return;
          }
        }

        if (e.preventDefault) {
          e.preventDefault ();
          var lf = '\n';
        } else {
          e.returnValue = false;
          var lf = '\r\n';
        }
        
        var s     = t.value;
        var range = t.getSelectionRange ();
        var ss    = range.selectionStart;
        var se    = range.selectionEnd;
        
        var pre   = s.slice (0, ss);
        var sel   = s.slice (ss, se);
        var post  = s.slice (se);

        /* single line selection */
        if (ss == se || sel.indexOf (lf) == -1 && post.slice (0, lf.length) != lf) {
          if (!deIndent) {
            pre = pre.concat (tab);
          } else if (pre.slice (-tab.length) == tab) {
            pre = pre.slice (0, -tab.length);
          }
        } else {
          /* Multi line selection */
          /* extend to start of line before selction */
          if(ss > 0){
            ss = pre.lastIndexOf (lf) + lf.length;
          }
          /* and end of line after selecton */
          if (se > ss && sel.slice (-lf.length) == lf) {
            se -= lf.length;
          } else {
            var h = post.indexOf (lf);
            se += h != -1 ? h : post.length;
          }
          
          var pre   = s.slice (0, ss);
          var sel   = s.slice (ss, se);
          var post  = s.slice (se);
          
          if (!deIndent){
            sel = tab + sel.replace (new RegExp (lf, 'g'), lf + tab);
          } else {
            sel = sel.replace (new RegExp ('^' + tab, 'gm'), '');
          }
        }
        t.value       = pre.concat (sel, post);
        t.setSelectionRange (pre.length, pre.length + sel.length);
   	    t.scrollTop   = scrollTop;
   	    t.scrollLeft  = scrollLeft;
        return;
     	}

      function txkeyup (evt) {
        evt = evt || window.event;
//        window.status = 'keyup';
/*
        if (ele.saveValue) {
          ele.value = ele.saveValue;
          ele.scrollTop = ele.saveScrollTop;
          ele.scrollLeft = ele.saveScrollLeft;
          ele.saveValue = '';
        }
*/
        var cnt = this.name+ '_cnt';
        document.getElementById (cnt).innerHTML = this.value.length;
      }

      function getInputSelection (el) {
        var
          start = 0,
          end = 0,
          normalizedValue,
          range,
          textInputRange,
          len,
          endRange;

        if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
            start = el.selectionStart;
            end = el.selectionEnd;
        } else {
          range = document.selection.createRange();

          if (range && range.parentElement() == el) {
            len = el.value.length;
            normalizedValue = el.value.replace(/\r\n/g, "\n");

            // Create a working TextRange that lives only in the input
            textInputRange = el.createTextRange();
            textInputRange.moveToBookmark(range.getBookmark());

            // Check if the start and end of the selection are at the very end
            // of the input, since moveStart/moveEnd doesn't return what we want
            // in those cases
            endRange = el.createTextRange();
            endRange.collapse(false);

            if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
              start = end = len;
            } else {
              start = -textInputRange.moveStart("character", -len);
              start += normalizedValue.slice(0, start).split("\n").length - 1;

              if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                end = len;
              } else {
                end = -textInputRange.moveEnd("character", -len);
                end += normalizedValue.slice(0, end).split("\n").length - 1;
              }
            }
          }
        }

        return {
          start: start,
          end: end
        };
      }

      function offsetToRangeCharacterMove(el, offset) {
        return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
      }

      function setInputSelection(el, startOffset, endOffset) {
        if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
          el.selectionStart = startOffset;
          el.selectionEnd = endOffset;
        } else {
          var range = el.createTextRange();
          var startCharMove = offsetToRangeCharacterMove(el, startOffset);
          range.collapse(true);
          if (startOffset == endOffset) {
            range.move("character", startCharMove);
          } else {
            range.moveEnd("character", offsetToRangeCharacterMove(el, endOffset));
            range.moveStart("character", startCharMove);
          }
          range.select();
        }
      }

      function check (f, cond, messs) {
        if (cond) {
          if (f.style)
            f.style.backgroundColor = '';
        } else {
          if (f.style)
            f.style.backgroundColor = '#fcc';
          alert (messs);
          if (f.focus)
            try {
              f.focus ();
            } catch (e) {};
          if (f.select)
            try {
              f.select ();
            } catch (e) {};
          return false;
        }
        return true;
      }

      lastWiKiTextArea = false;


      function insertLink (obj) {
        var iLDiv = document.getElementById ('insertLink');
        iLDiv.style.display = 'block';
        iLDiv.style.zIndex = iLDiv.style.zIndex + 1;
        var ilSelect = iLDiv.getElementsByTagName ('select')[0];
        if (ilSelect.options.length < 2) {
          var sGoOptions = document.getElementById ('sGo').options;
          for (var i = 0; i < sGoOptions.length; i++) {
            ilSelect.appendChild (sGoOptions [i].cloneNode (true));
          }
        }
        ilSelect.selectedIndex = 0;
        
        lastWikiTextArea = obj;
        return false;
      }

      function insertFormLink (form, doit) {
        document.getElementById ('insertLink').style.display = 'none';
        document.getElementById ('insertLink').style.zIndex = '99';
        if (doit) {
          var s = '[';
          if (form.newWindow.checked) {
            s += '> ';
          }
          if (form.intLink.selectedIndex) {
            s += form.intLink.options[form.intLink.selectedIndex].value;
          } else {
            s += form.exLink.value;
          }
          if (form.linkText) {
            s += ' ' + form.linkText.value;
          }
          s += ']';
          insertAtCaret (lastWikiTextArea, s);
        }
        return false;
      }
      
      function insertPic (obj) {
        var iLDiv = document.getElementById ('insertPic');
        iLDiv.style.display = 'block';
        iLDiv.style.zIndex = iLDiv.style.zIndex + 1;
        var ilSelect = iLDiv.getElementsByTagName ('select')[0];
        ilSelect.selectedIndex = 0;
        lastWikiTextArea = obj;
        return false;
      }

      function insertFormPic (form, doit) {
        document.getElementById ('insertPic').style.display = 'none';
        document.getElementById ('insertPic').style.zIndex = '99';
        if (doit) {
          var s = '[';
          if (form.orientation) {
            s += form.orientation.value;
          }
          if (form.intPic.selectedIndex) {
            s += form.intPic.options[form.intPic.selectedIndex].value;
          }
          if (form.altText.value) {
            s += ' ' + form.altText.value;
          }
          s += ']';
          insertAtCaret (lastWikiTextArea, s);
        }
        return false;
      }

      function insertWikiFileOption (sel, wikiCmd) {
        if (!sel) {
          alert ("nichts ausgewählt " + sel)
          return false;
        }

        if (typeof (wikiCmd) === 'undefined') {
          wikiCmd = '';
        }
        if (sel.selectedIndex < 0) {
          alert ("nichts ausgewählt ")
          return false;
        }

        /* find the text area */
        if (lastWiKiTextArea) {
          var t = lastWiKiTextArea;
        } else {
          var e = sel.form.elements;
          var s = '';
          var t;
          for(var i = 0; i < e.length; i++) {

  //          alert (e [i].tagName);
            if (e [i].tagName && e [i].tagName == 'TEXTAREA' && e [i].name.indexOf('[Text')) {
              t = e [i];
            }
          }
          if (!t) {
            alert ("kein Textfeld zum Einfügen gefunden: " + s);
            return false;
          }
        }
        
        /* find selected options */
        var opts = sel.options;
        var insText = '';
        for (i = 0; i < opts.length; i++) {
          if (opts [i].selected && opts [i].value) {
            insText += '[' + wikiCmd + opts [i].value + /* ' ' + opts [i].value + */ ']';
          }
        }

        if (!insText) {
          alert ("bitte eine Datei auswählen: " + s);
          return false;
        }
        insertAtCaret (t, insText);
        return false;
      }

      function insertAtCaret (obj, string) {
//      obj.value += string;

        obj.focus ();

        if (typeof (document.selection) != 'undefined') {
          var range = document.selection.createRange ();
          if (range.parentElement () != obj)
            return;
          range.text = string;
          range.moveStart ('character', -string.length);
          range.select ();
        } else {
          if (typeof (obj.selectionStart) != 'undefined') {
            var start = obj.selectionStart;

            obj.value =
              obj.value.substr (0, start) +
              string +
              obj.value.substr (obj.selectionEnd);

            obj.setSelectionRange (start, start + string.length);
          } else {
            obj.value += string;
          }
        }

        obj.focus ();
      }
      
      function loadImage (inpFile) {
        var msg = document.getElementById ('msg');
        msg.innerHTML = 'Bitte warten - Datei wird an den Server gesendet ...';
        inpFile.form.submit ();
        return true;
      }

