var counter = 0;

function moreFields()
{
  counter++;
  var newFields = document.getElementById('readroot').cloneNode(true);
  newFields.id = '';
  newFields.style.display = 'block';
  var newField = newFields.childNodes;
  for (var i=0;i < newField.length;i++)
  {
    var theName = newField[i].name
    if (theName) newField[i].name = theName + counter;
  }
  var insertHere = document.getElementById('writeroot');
  insertHere.parentNode.insertBefore(newFields,insertHere);
}

function toggle(){
  var i, j, args;
  args=toggle.arguments;
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    i.disabled=!i.disabled;
    if( i.disabled ) { i.style.background='gray'; }
    else { i.style.background='white'; }
  }
}

function collapse(){
  var i, j, args;
  args=collapse.arguments;
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    i.style.display ='none';
  }
}

function expand(){
  var i, j, args;
  args=expand.arguments;
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    i.style.display ='block';
  }
}

function expandCollapse(){
  var i, j, args;
  args=expandCollapse.arguments;
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    if( i.style.display != 'none' ) { i.style.display = 'none'; }
    else { i.style.display = 'block'; }
  }
}

function colorBoxes(){
  var i,j;
  var args = new Array('mutseq','codon_radio','random','strict','scored','tbio','weight_check','mut_radio');
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    i.style.background='#6699CC';
  }
}

function enableInput(){
  var i, j, args;
  args=enableInput.arguments;
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    i.disabled=false;
    i.style.background='white';
  }
}

function disableInput(){
  var i, j, args;
  args=disableInput.arguments;
  for (j = 0; j < args.length; j++) {
    i=document.getElementById(args[j]);
    i.disabled=true;
    i.style.background='gray';
  }
}

function enableInputSeq(){
/* This function enables a field that ends with the same number as the
 current radio */
  var i=0, args, input, array, curr;
  args=enableInputSeq.arguments;           // get the arguments
  array=(args[0].name).split("_");         // split the first arg (radio) by "_"
  curr=args[1] + array[1];                 // create the current form name
  input=document.forms["mainform"]                  // create a list of all forms (with same name) in the document
  while (typeof(input[i])!='undefined'){
    if(input[i].name==curr) {
          input[i].disabled=false  // if the constructed name matches, enable it
          input[i].style.background='white';
    }
        i++
  }
}

function disableInputSeq(){
  var i=0, args, input, array, curr;
  args=disableInputSeq.arguments;
  array=(args[0].name).split("_");
  curr=args[1] + array[1];
  input=document.forms["mainform"]
  while (typeof(input[i])!='undefined'){
    if(input[i].name==curr) {
          input[i].disabled=true;
      input[i].style.background='gray';
    }
        i++
  }
}


function saveData() {
  var val=MM_findObj(saveData.arguments[0]);
  if (val) temp_data = val.value
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((content=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=content.indexOf('@');
       if (p<1 || p==(content.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(content);
        if (isNaN(content)) {
                  errors+='- '+nm+' must contain a number!\n'
                }
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  }
  if (errors) {
    alert('The following error(s) occurred:\n'+errors)
        val.value=temp_data
  }
  document.MM_returnValue = (errors == '');
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function PopUp(ref)
{
  var strFeatures="toolbar=no,status=no,menubar=no,location=no"
  strFeatures=strFeatures+",scrollbars=yes,resizable=yes,height=400,width=500"
  newWin = window.open(ref,"TellObj",strFeatures);
  newWin.opener = top;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
window.onload = moreFields;
