//*******************************************************************//
//
//  DEBUGING: DOMTREE          
//
//*******************************************************************//

// From www7.ewebcity.com/cyanide7

// globals
var objects = new Array();
var browser = null;
var expanded = null;

// Opens the popup window and builds the main doc
function openDOMBrowser(activeElement)
  {

  objects[0] = new Array(document,"_document",false);

  activeIndex = arrayIndexOf(objects, activeElement,1);
  objects[activeIndex][2] = !objects[activeIndex][2]; // [2] is expand/coll
  args="width=500,height=500,left=20,top=20,scrollbars,resizable,top=0,left=0";
  browser=window.open('',"DOMBrowser",args);
  browser.focus();

  expanded=new Array();
  expanded["_document"]=true;
  
  browser.document.open("text/html","replace");
  browser.document.writeln("<HTML><HEAD><TITLE>DOM Browser</TITLE></HEAD>"
       + "<body bgcolor='BBBBBB' link='FFFFFF' vlink='FFFFFF'>"
       + "<h3>document</h3><ul>");
  
  getProps(parent.document.body.firstChild.contentWindow.document);//.contentDocument.getElementById('homeref')); //put here what to browse
  browser.document.writeln("</ul></body></html>");
  browser.document.close();
  return false;
  }

function getProps(obj)
  {
  for (var prop in obj)
    {
    browser.document.writeln("<li>");
    if (typeof(obj[prop])=="object" && obj[prop]!=null)
      {
      valIndex = arrayIndexOf(objects, obj[prop], 0);
      if (valIndex==-1)
        {
        valIndex=objects.length;
        key=((new Date()).getTime()%10000) + "_" + (Math.floor(Math.random()*10000));
        objects[valIndex] = new Array(obj[prop], key, false);
        }
      
      browser.document.writeln("<b>" + prop + 
        '</b> : <a href=\"javascript:void(0)\" onClick=\"window.opener.openDOMBrowser(\''
        + objects[valIndex][1]+'\');return false;">' 
        + (new String(obj[prop])).replace(/</g,"<")+"</a>");

      if (objects[valIndex][2])
        {
        if (!(expanded[objects[valIndex][1]]))
          {
          expanded[objects[valIndex][1]]=true;
          browser.document.writeln("<ul>");
          getProps(obj[prop]);
          browser.document.writeln("</ul>");
          }
        }
      }
    else // not an object => just print
      {
      browser.document.writeln("<b>" + prop + "</b> : "
        + (new String(obj[prop])).replace(/</g,"<") + "</li>");
      }
    }
  }

function arrayIndexOf(array,value,field)
  {
  var found = false;
  var index = 0;
  while (!found && (index < array.length))
    {
    if (array[index][field]==value)
      {
      found=true;
      }
    else
      {
      index++;
      }
    }
  return (found)?index:-1;
  }

function printDOMBrowserLink()
  {
  document.writeln("<br/>"
    + "<a href=\"#\" onClick=\"openDOMBrowser('_document');\">"
    + "Browse dom</a><br/>");
  }

function printProps()
  {
  document.write("<ul>");
  
  for (var prop in parent.document)
    {
    document.write(prop+"<br />");
    }
  }

