
  // (c) Russ Abbott, 2000, 2001. RAbbott@CalStateLA.edu.
  // Distributed under terms of the GNU public license:
  // http://www.gnu.org/copyleft/gpl.html

  objs=new Array();
  inputStrings = new Array()
  index=-1;
  browsedFileWindow = null;
  browsedFileDoc = null;
  browsedFileHTML = null;
  objsIndex = -1;
  backButton = document.getElementById("back");
  forwardButton = document.getElementById("forward");
  
function displayIt(str) {ejs.display.value = str;}  
   
function evalString(s) {
  if (s == "") s = '"<empty string>"';
  index=objs.length;
  inputStrings[index] = s;
  objs[index] = eval(s);
  backButton = document.getElementById("back");
  forwardButton = document.getElementById("forward");
  backButton.disabled=false;
  forwardButton.disabled=false;
  if (objsIndex==-1) forwardButton.disabled=true;
  if (objsIndex==0) backButton.disabled=true;
  return toStringWProps(objs[index]);
}

function examinePage(fileName) {
  // Replace all backslashes with forward slashes.
  ejs.input.value  = "browsedFileWindow = window.open('" + fileName.replace(/\\/g, '/') + "');\n";
  displayIt(evalString(document.ejs.input.value));
  browsedFileDoc = browsedFileWindow.document;
  for (i = 0; i < browsedFileDoc != null && browsedFileDoc.childNodes.length; i++) {
     browsedFileHTML = browsedFileDoc.childNodes[i];
     if (browsedFileHTML.nodeName.toLowerCase() == "html") break;
  }
  if (browsedFileDoc != null) {
    document.getElementById("buttons1").style.display="inline";
    document.getElementById("buttons2").style.display="inline";
  }
}


function getEBI(id) {
  index = inputStrings.length;
  objs[index]=browsedFileDoc.getElementById(id);
  inputStrings[index] = 'browsedFileDoc.getElementById("'  + id + '")';
  ejs.input.value=inputStrings[index];
  displayIt(toStringWProps(objs[index]));
}

function objProps(obj) {
  var properties = new Array();
  var j = 0;
  for (var prop in obj) properties[j++] = prop;
  return properties.sort();  
}

function openInstrs() {
  // Open a window with the instructions file.
  // Use a local reference whether the interpreter is local or remote.
  examinePage('evalJavaScriptDoc.htm');
  // Resize that window.
  browsedFileWindow.resizeTo(width-70, height);
  // Position that window.
  browsedFileWindow.moveTo(width+2, 5);
  // Position this window.
  window.moveTo(0, 5);
  window.resizeTo(width, height)
  window.scrollTo(5, 0);
}


var searchCount=0;
  
function searchFor(obj, ss, count){
  if (obj.nodeName=="!") return null;
  if (obj.childNodes!=null && 
    typeof(obj.childNodes) == "object" && 
	   obj.childNodes.length > 0) {
    for (var i = 0; i < obj.childNodes.length; i++) {
      var nodeI = obj.childNodes[i];
      var s1 = propValOrEmpty(nodeI, 'outerHTML'); 
      var n = stringInstances(s1, ss);
      if (n >= count) return searchFor(nodeI, ss, count);
      count -= n;
    }
  }
  return obj;
}
  
function propValOrEmpty(obj, prop) {
  return ((prop  in obj) ? obj[prop]  : "")
}

function searchForString(searchString) {
  index = inputStrings.length;
  inputStrings[index] = 'searchFor(browsedFileHTML, "'  + 
                         searchString + '", ' + (++searchCount) + ')';
  objs[index] = (stringInstances(browsedFileHTML.outerHTML, searchString) == 0) ? 
                  null :
                  searchFor(browsedFileHTML, searchString, searchCount);
  ejs.input.value=inputStrings[index];
  displayIt(toStringWProps(objs[index]));
}
  
function stringInstances(targetString, searchString) {
  if (typeof(targetString) != "string") return 0;
  var pos = targetString.toLowerCase().indexOf(searchString.toLowerCase());
  if (pos == -1) return 0;
    return 1 + stringInstances(targetString.substring(pos+1, targetString.length), searchString);
}
  
// var resultStart = '<br><br>==========================================================<br><br>';
// var resultNL = "<br>"

var resultStart = '';
var resultNL = "\n"

function toStringWProps(value) {
  if (value == null) return null;
  var s = resultStart;
  if (value["id"] != null && value["id"]!="") s += 'id: ' + value["id"] + resultNL;
  if (value["name"] != null) s += 'name: ' + value["name"] + resultNL;
  if (value["nodeName"] != null) s += 'nodeName: ' + value["nodeName"] + resultNL;

  s += 'datatype: ' + typeof(value) + resultNL + 
       'value: ' + value + resultNL +
       'properties: ';
  // Collect the properties of the value, if any.
  var properties = objProps(value);
  if (properties.length==0) return s + "none";
  s += resultNL;
  for (j = 0; j < properties.length; j++) {
    var prop = properties[j];
    // IE seems to abort when attempting to access these properties.
    // It used to abort on 'dynSrc' and 'filters' also.
    if (prop != "frameElement" && prop != "filters")
      s += "  " + prop + ": " +  value[prop] + resultNL;
  }
  return s;
} 
  
  
// Define an error handler.
onerror=
  function(a, b, c) {
    displayIt('Error: ' + a);
    return true;
  }

// resizeTo(width in pixels, height in pixels)
var width = 610;
var height = 450;

resizeTo(width, 830);
moveTo(5, 5);
  