function toggletarget(checkbox)
{
  var td = checkbox.parentNode;
  while(td = td.nextSibling) {
    if(td.nodeType != td.ELEMENT_NODE)
      continue;

    var i = td.getElementsByTagName("input")[0];
    if(!i)
      continue;
    if(checkbox.checked)
     i.removeAttribute("disabled");
    else
     i.setAttribute("disabled", "disabled");

  }
}

function autoshortname()
{
  if(document.forms[0].shortname.value != '' ||
     document.forms[0].name.value == '')
    return;

  var sn = document.forms[0].name.value.toLowerCase().replace(/[^a-z]/g,'');
  document.forms[0].shortname.value = sn;
  autoid();
}

function autoid()
{
  if(document.forms[0].id.value != '' ||
     document.forms[0].shortname.value == '')
    return;

  var sn = document.forms[0].shortname.value;
  var n = document.forms[0].author.value.toLowerCase().replace(/\s+/g,'.').replace(/[^a-z\.]/g,'');
  var d = (domain != '') ? domain : n;
  document.forms[0].id.value = sn + '@' + d;
}

function addcontributor() {
  var tbl = document.getElementById("contributors");
  var tr = tbl.firstChild.cloneNode(true);
  tr.removeAttribute("style");
/*
  var input = document.createElement("input");
  input.setAttribute("type","text");
  input.setAttribute("name","contributor");
  var td = document.createElement("td");
  var tr = document.createElement("tr");
  td.appendChild(input);
  tr.appendChild(td);
*/
  tbl.appendChild(tr);
  checkbuttonenabled("remcontributor", "contributors", 1);
}

function removecontributor(btn) {
 var tbl = document.getElementById("contributors");
 tbl.removeChild(tbl.lastChild);
 checkbuttonenabled("remcontributor", "contributors", 1);
}

function checkbuttonenabled(btn, tbl, min) {
  tbl = document.getElementById(tbl);
  if(tbl.hasChildNodes()  && tbl.childNodes.length > min) {
    document.getElementById(btn).removeAttribute("disabled");
  }
  else {
    document.getElementById(btn).setAttribute("disabled", "disabled");
  }
}

function doforallchildren(node, f, data) {
 f(node,data);
 if(node.hasChildNodes()) {
  for(var i=0; i<node.childNodes.length; i++) {
    doforallchildren(node.childNodes[i], f, data);
  }
 }
}

function replacedepzero(node, num) {
  if(node.id != null && node.id != "") {
   node.id = node.id.replace(/dep0/, "dep" + num);
  }
  if("name" in node && node.getAttribute("name") != null) {
   var n = node.getAttribute("name");
   node.setAttribute("name", n.replace(/dep0/, "dep" + num));
  }
}

function adddependency() {
  var tbl = document.getElementById("tbl-dependencies");
  var numdeps = document.getElementById("numdependencies");
  numdeps.value++;
  var tr = tbl.firstChild.cloneNode(true);
  tr.removeAttribute("style");
  doforallchildren(tr, replacedepzero, numdeps.value);
  tbl.appendChild(tr);
  checkbuttonenabled("remdependency", "tbl-dependencies", 1);
}

function removedependency() {
  var tbl = document.getElementById("tbl-dependencies");
  var numdeps = document.getElementById("numdependencies");
  numdeps.value--;
  tbl.removeChild(tbl.lastChild);
  checkbuttonenabled("remdependency", "tbl-dependencies", 1);
}
