var DEBUG = 0;
var dCdiv;
var beginsWithInput;
var gotoindex;
var entrytypes;
var entryvolume;
var currentDir = document.location.href.replace(/\/[^\/]*$/, '/');
var pagesize = {Entries:15, SubjectIndex:20, AuthorIndex:20};

var latest = 0;

var index_classes = {on: {LI:'on', A:'on', DIV:'indexresults menus_on'}, off: {LI:'', A:'', DIV:'indexresults'}};

function bid(id){
    return document.getElementById(id);
}

function do_request(url, func, start) {
    if (DEBUG) {
        alert(url);
    }
  var req;
  // W3C version
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.ms_0 = start;
    req.onreadystatechange = function(){
	func(req);
    };
    req.open("get", url, true);
    req.send(null);
  }
  else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
        req.onReadyStateChange = function(){
	    func(req);
	};
        req.open("GET", url, true);
        req.send();
    }
  }
}

function resultToDCDiv(req){
    if (req.readyState == 4) { // only if req shows "loaded"
        //set_waiting(false);
        if (req.status == 200) { // only if "OK"
           if (req.ms_0 > latest || !latest){
              latest = req.ms_0;
	          dCDiv.innerHTML = req.responseText;
           }
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                  req.statusText);
        }
    }

}

function contentRequest(beginsWith, page, contentType){
    beginsWith = escape(beginsWith);
    do_request(currentDir + contentType + 'Browse.html?beginsWith=' + beginsWith + '&pagenum=' + page + '&pagesize=' + pagesize[contentType] + '', resultToDCDiv, new Date().getTime());
}

function contentRequestWithTypes(beginsWith, page, contentType, types){
    beginsWith = escape(beginsWith);
    do_request(currentDir + contentType + 'Browse.html?beginsWith=' + beginsWith + '&pagenum=' + page + '&pagesize=' + pagesize[contentType] + '&entrytypes=' + types + '', resultToDCDiv, new Date().getTime());
}

function contentRequestWithTypesAndVolume(beginsWith, page, contentType, types, volume){
    beginsWith = escape(beginsWith);
    do_request(currentDir + contentType + 'Browse.html?beginsWith=' + beginsWith 
    	+ '&pagenum=' + page + '&pagesize=' + pagesize[contentType] 
    	+ '&entrytypes=' + types + '&volume=' + volume + '', resultToDCDiv, new Date().getTime());
}

function getCheckedValue(input){
    for (var i = 0; i < input.length; i++){
        if (input[i].checked){
            return input[i].value;
        }
    }
    return '';
}

function unselect_all(){
  var browseDiv = document.getElementById("dynamicContent");
  unselect_children(browseDiv);
}

function unselect_children(node){
 for(var i = 0; i < node.childNodes.length; i++){
  var child = node.childNodes[i];
  if (!child.nodeName) {
    continue;
  }
  switch (child.nodeName.toUpperCase()){
   case "UL":
   case "LI":
       if (child.className == "on"){
           child.className = "";
       }
       unselect_children(child);
       break;
  case "A":
      child.className = "";
      break;
  case "DIV":
      if (child.className == "indexresults menus_on"){
          child.className = "indexresults";
      } 
      unselect_children(child);
      break;
  }
 }
}
