//
//  Debug
//  displays debugging info "text" with various methods
//
  var debMode   = 0; // holds current debugging window
  var debWindow = 1; // debug to window
  var debStatus = 2; // debug to status display
  var debAlert  = 3; // debug to alerts
  var dbw       = 0; // holds current debugging window
  function Debug (text) {
    if (debMode == debAlert) {
      text = "Window " + window.name + "\n" + text;
      alert (text);
    } else
      text = window.name + ': ' + text;
    if (debMode == debStatus)
      window.status = text;
    if (debMode != debWindow)
      return;
    dbw.document.writeln ("<font size=1>" + text + "</FONT><BR>");
    dbw.scrollBy (0, 30);
    dbw.focus ();
  }
//
//    dbw.document.open("text/plain");
//  Out --------------------------------
//  debugs an object or variable and all it´s fields
//
  function Out (obj) {
    if (typeof (obj) != "object") {
      Debug (obj);
      return;
    }
    var j = 0;
    var result = "";
    name = "";
    if (obj.name)
      name = obj.name;
    else if (obj.id)
      name = obj.id;
    result = "Out (" + obj + "): " + name;
    if (debMode == debWindow)
      result += "<BR>"
    else
      result += "\n";
    for (var i in obj) {
      if (typeof (obj[i]) != "unknown") {
        s = "" + eval("obj." + i);
      }
      if (s.length > 70) {
        s = s.substr (0,70);
      }
      result += (debMode == debWindow ? "&nbsp;&nbsp;&nbsp;" : "   ") + i + " = " + s + (debMode == debWindow ? "<BR>" : "\n");
      j++;
      if (j > 30 && debMode != debWindow) {
        Debug (result);
        j = 0;
        result = "";
      }
    }
    if (j > 0)
      Debug (result);
  }
//  obj - object to be inspected
//  objName - for subsequent calls - the name of the sub object
  var isw = null;
  var sobj = 0;
  var i = 0;
  function Inspector (obj, objName) {

    isw = window.open ('','Inspector',',width=450,height=450,top=200,left=550,status=yes,scrollbars=yes,resizable=yes');
    if (!isw) {
      alert ("kann fenster nicht öffnen!");
      return;
    }

//  first call by given object, retrieve object name from id or name
    if (obj) {
      if (obj.name)
        objName = obj.name;
      else if (obj.id)
        objName = obj.id;
      if (!objName)
        objName = "Inspector";
//    save object with window of caller
      window.ispObj = obj;
    }
//  split object name into element names
    var subName = "";
    var parentName = "";
    objName += "";
    var names = objName.split (".");
//  name has elements?
    if (names.length <= 1) {
      obj = window.ispObj;
    } else {
      for (i = 0; i < names.length - 1; i++) {
        if (i > 0)
          parentName += ".";
        parentName += names [i];
      }
      for (i = 1; i < names.length; i++) {
        if (i > 1)
          subName += ".";
        subName += names [i];
      }
      varName = "window.ispObj";
      for (i = 1; i < names.length; i++) {
        if (isNaN (names [i])) {
          varName += "." + names [i];
        } else {
          varName += "[" + names [i] + "]";
        }
      }
//    name subobject replace ".0." with [0];
//      alert (varName);
      obj = eval (varName);
    }

//  write header
    isw.document.open ();
    var s  = '<HTML>\n';
    s += '<HEAD>\n';
    s += '<TITLE>' + objName + '</TITLE>\n';
//    s += '<TITLE>' + objName + " [" + typeof (obj) + ']</TITLE>\n';
//  wenn ich was inkuldiere, stürzt es ab, wenn nicht file-zugriff
//    s += '<SCRIPT SRC="debug.js" LANGUAGE="JavaScript"><\/SCRIPT>\n';
    s += '</HEAD>\n';
    s += '<BODY LEFTMARGIN="0" TOPMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0">\n';
    s += '<TABLE BGCOLOR="lightgrey" WIDTH="100%" CELLSPACING="" CELLPADDING="2" BORDER="1">\n';
    if (parentName) {
      s += '<TR>\n';
      s += '<TD WIDTH="1"><FONT FACE="arial" SIZE="2">' + parentName + '</FONT></TD>\n';
      s += '<TD WIDTH="1">&nbsp;</TD>\n';
      s += '<TD WIDTH="100%" BGCOLOR="white"><FONT FACE="arial" SIZE="2">\n';
        s += '  <A HREF=JavaScript:opener.Inspector(null,"' + parentName + '")>..</A></FONT></TD>\n';
      s += '</TR>\n';
    }
    isw.document.write (s);

//  loop trough elements of object
    for (i in obj) {
//    get object element type
//    NS no type of document.applets and document.embeds
      if (document.layers && (i == "applets" || i == "embeds"))
        t = "unknown";
      else
        t = typeof (obj [i]);
//    get elements text value
      v = "";
      if (t != "unknown") {
//      convert to string
        v = '';
        v = v.concat (obj [i]);
//      no html
        v = v.replace (/</g, "&lt;");
//      line breaks
        v = v.replace (/\n/g, "<BR>");
//      no empty values
        if (v == "")
          v = "&nbsp;";
      }
//    progress status
//      alert (i + " " + t + " " + v);
//      window.status = i + " " + t + " " + v;
//    output to table
      s  = '<TR>\n';
      s += '<TD WIDTH="1" VALIGN="top"><FONT FACE="arial" SIZE="2"><B>' + i + '</B></FONT></TD>\n';
      s += '<TD WIDTH="1" VALIGN="top" ALIGN="center"><FONT FACE="arial" SIZE="2">[' + t + ']</FONT></TD>\n';
      s += '<TD WIDTH="100%" BGCOLOR="white"><FONT FACE="arial" SIZE="2">';
//      if (t == "object" && v != "null")
      if (t == "object")
        s += '  <A HREF="JavaScript:opener.Inspector(null,\'' + objName + '.' + i + '\')">\n';
      s += v;
      if (t == "object")
        s += '</A>';
      s += '</FONT></TD>\n';
      s += '</TR>';
      isw.document.write (s);
    }

    s  = '</TABLE>\n';
    s += '</BODY>\n';
    s += '</HTML>';
    isw.document.write (s);
    isw.document.close ();
    isw.focus ();
  }
//
//  Watch -------------------------------------------------------
//  Debugs a variable to the current debug device all watchDelay ms
//
//
  watchCtr = 0;
  watchMax = 20;
  watchDelay = 20;
  function Watch (varName) {
    if (watchCtr < watchCtrMax) {
      setTimeout ("Watch ('" + varName + "')", 250);
      var v = eval (varName);
      Debug ("Watch  ('" + varName + "') [" + watchCtr + "]: " + v);
      watchCtr++;
    }
  }
//
//  DebInit
//  Initializes debugging mode
//
  function DebInit (debmode) {
    debMode = debmode;
    if (debMode == debWindow) {
      dbw = window.open ('','Debugger',',width=850,height=450,top=200,left=200,scrollbars=yes,resizable=yes');
      Debug ("Debugging started ..");
    }       
  }
//
//  Let´s go
//
  DebInit (0);