function Traverser(noderoot, condition, descend) {
  this.position = noderoot;
  this.root=noderoot;
  this.condition=condition;
  this.istovisit=true;
  this.descend=descend;
  
  
  this.next = function() {
    while (this.getNextElement()) {
      var currnode = this.position;
      if (this.condition(currnode)) {
        this.istovisit=this.descend;
        return currnode;
      };
    };
    return false;
  };
  
  this.getNextElement = function() {
    while(true) {
      //move forward once
      if (this.istovisit && this.position.hasChildNodes()) {
        this.position=this.position.firstChild;
      } else if (this.position.nextSibling != null) {
        this.position=this.position.nextSibling;
        this.istovisit=true;
      } else if (this.position.parentNode != this.root) {
        this.position=this.position.parentNode;
        this.istovisit=false;
      } else {
        //return false if nowhere to go
        return false;
      }
      
      //return true if is element and istovisit
      if (this.istovisit && this.position.nodeType==1) {
        return true;
      };
    };
  };
  
};

function Footer() {
  return null;
};

Footer.init = function() {
  Footer.headertraverser = new Traverser(document.getElementsByTagName("body")[0],
					 function (node) {
					   return (node.name && (node.name.match(/^author_/))); 
					 },
					 false);
  var header = Footer.headertraverser.next();
  while(header) {
    var footer = header.parentNode.parentNode.nextSibling;
    Footer.insertFooter(footer,header);
    header = Footer.headertraverser.next();
  }
}

Footer.insertFooter = function(footer,header) {
  var td = document.createElement("td");
  var tr = document.createElement("tr");
  var inputs = header.parentNode.getElementsByTagName("input") 
  for (var i=0;i<inputs.length;i++){
    td.appendChild(inputs[i].cloneNode(false));
  }
  tr.appendChild(td);
  if (footer.nextSibling) {
    footer.parentNode.insertBefore(tr,footer.nextSibling);
  } else {
    footer.parentNode.appendChild(tr);
  }
  while (inputs.length>0) {
    inputs[0].parentNode.removeChild(inputs[0]);
  }
}


if (window.onload) {
  var f=window.onload;
  window.onload= function() {Footer.init();f()};
} else {
  window.onload = function() {Footer.init();};
}


