var subformCount = 0;

/**
* go through every child node of root
**/
function depthTraverse (root, fn, params ) {
  var c = root, n = null;
  do {
    n = c.firstChild;
    if (n == null) {
      // visit c
      fn(c, params);
      // done visit c
      n = c.nextSibling;
    }
    if (n == null) {
      var tmp = c;
      do {
        n = tmp.parentNode;
        if (n == root) break;
        // visit n
        fn(n, params);
        // done visit n
        tmp = n;
        n = n.nextSibling;
      } while (n == null)
    }
    c = n;
  } while (c != root);
}

function renumberFormField(node, childType) {
  if (node.tagName == 'INPUT' || node.tagName == 'TEXTAREA' ) {
    var theName = node.name; // strip off tmpl_
    theName = theName.replace("tmpl_","");
    if (theName)  node.name = childType+'[' + subformCount + '][' + theName + ']' ;
   var theID = node.id
   theID = theID.replace("tmpl_","");
   if (theID)
     node.id = childType+'_' + subformCount + '_' + theID ;
  }
  if (node.tagName == 'LABEL') {
    var theName = node.getAttribute('for'); // strip off tmpl_
    theName = theName.replace("tmpl_","");
    if (theName) node.htmlFor = childType+'_' + subformCount + '_' + theName;
  }
} 

/*
function addSubform(childtype) {
  subformCount++;
  var mysubform = document.getElementById(childtype+'_tmpl').cloneNode(true);
  mysubform.id = '';
  mysubform.style.display = 'block';
  // traverse all of the childnodes of mysubform
  depthTraverse (mysubform, renumberFormField, childtype );
  var insertHere = document.getElementById(childtype+'_insert');
  insertHere.parentNode.insertBefore(mysubform,insertHere);
  return false;
}  

// <input id="people_1_id" type="hidden" value="" name="people[1][id]"/>

*/

function addSubform(childtype) {
  ++subformCount;
  var thisType = childtype;
  var mysubform = $('#'+childtype+'_tmpl').clone().appendTo('#'+childtype+'_insert');
  mysubform.attr('id',childtype+'_'+subformCount).show();
  var i_tag = '#'+childtype+'_'+subformCount+' > ol > ';
  var p_tag = i_tag+' li > ';
  $( i_tag+'input, '+p_tag+'input, '+p_tag+'label, '+p_tag+'textarea').each(function(index) { 
    var mine = this;
    /* old_school starts here ...*/
    var prefix = thisType;
    if (mine.tagName == 'INPUT' || mine.tagName == 'TEXTAREA' ) {
      var theName = mine.name; // strip off tmpl_
      theName = theName.replace("tmpl_","");
      if (theName) mine.name = prefix+'[' + subformCount + '][' + theName + ']' ;
     var theID = mine.id
     theID = theID.replace("tmpl_","");
     if (theID)
       mine.id = prefix+'_' + subformCount + '_' + theID ;
    }
    if (mine.tagName == 'LABEL') {
      var theName = mine.getAttribute('for'); // strip off tmpl_
      theName = theName.replace("tmpl_","");
      if (theName) mine.htmlFor = prefix+'_' + subformCount + '_' + theName;
    }
    /* .. and has ended by here */
  });;
  return false;
}


function addSingleSubform() { 
  subformCount++;
  var mysubform = document.getElementById(childtype+'_tmpl').cloneNode(true);
  mysubform.id = '';
  mysubform.style.display = 'block';
  // traverse all of the childnodes of mysubform
  depthTraverse (mysubform, renumberFormField, childtype );
  var insertHere = document.getElementById(childtype+'_insert');
  insertHere.parentNode.insertBefore(mysubform,insertHere);
  // now hide the add button!!!
  thebutton = document.getElementById('add_'+childtype);
  thebutton.style.display = "none";
  return false;
}

function toggleVisible(myItem){
  //this is the ID of the hidden item
  var myItem = document.getElementById(myItem);
  if (myItem.style.display != "none") { 
    myItem.style.display = "none";  // items visible, so hide them
  } else {
    myItem.style.display = "block"; // items hidden, so show them
  }
}

function hide(myItem){
  //this is the ID of the hidden item
  var myItem = document.getElementById(myItem);
  myItem.style.display = "none";  // hide item
}

function show(myItem){
  //this is the ID of the hidden item
  var myItem = document.getElementById(myItem);
  myItem.style.display = "block";  // show item them
}

