/*******************************************************
 * @author	: Bhogendraraj Gurung (vogen)
 * Purpose	: To add/remove input, textbox using DOM
 * Date		: 10 December 2003
 * Note		: This code is be free to modify or use
 * email	: vogendrix@hotmail.com
 *******************************************************/

// global variables
var inc_num3 = 1;
var inc_num2 = 1;
var inc_num1 = 1;


// function name	: insertRowTable3
// purpose			: to insert row in table 3
function insertRowTable3(tablename) {
 // -- get the table id="table1"
 var t = document.getElementById(tablename);
  // - get the first tbody tag belonging to var t (this is where our inputs are )
 var tb = t.getElementsByTagName('tbody')[0];
 var tr0 = tb.childNodes[0];

 var myClone = tr0.cloneNode(true);
 // -- get the number of inputs contained in the cloned row
 var newInpt = myClone.getElementsByTagName('input');
 var newTa = myClone.getElementsByTagName('textarea');
 // -- for Mac IE 5, the appendChild statement must be here instead of
 // -- the end of the function or the row won't be added
 // -- tb.appendChild(myClone)
 for (i=0; i < newInpt.length; i++){
  // -- get only the non-numeric part of the name using a regular expression; 
  // -- concatinate it with inc_num to create the new name
 var newName = newInpt[i].name.substring(newInpt[i].name);
 newInpt[i].setAttribute('name', newName);
 newInpt[i].setAttribute('value', "");
 }
 for (i=0; i<newTa.length; i++){
  var newName = newTa[i].name.substring(newTa[i].name);
  newTa[i].setAttribute('name',newName);
  newTa[i].setAttribute('value', "");
 }
inc_num3++;
tb.appendChild(myClone);

}

// function name	: insertRowTable2
// purpose			: to insert row in table 2
function insertRowTable2(tablename) {
  // -- get the table id="table1"
 var t = document.getElementById(tablename);
  // - get the first tbody tag belonging to var t (this is where our inputs are )
 var tb = t.getElementsByTagName('span')[0];
 var tr0 = tb.childNodes[0];

 var myClone = tr0.cloneNode(true);
 // -- get the number of inputs contained in the cloned row
 var newInpt = myClone.getElementsByTagName('input');
 
 // -- for Mac IE 5, the appendChild statement must be here instead of
 // -- the end of the function or the row won't be added
 // -- tb.appendChild(myClone)

for (i=0; i < newInpt.length; i++){
  // -- get only the non-numeric part of the name using a regular expression; 
  // -- concatinate it with inc_num to create the new name
 var newName = newInpt[i].name.substring(newInpt[i].name);
 newInpt[i].setAttribute('name',newName);
 newInpt[i].setAttribute('value', "");
 }
inc_num2++;

tb.appendChild(myClone);
}

// function name	: insertRowTable1
// purpose			: to insert row in table 1
function insertRowTable1(tablename) {
  // -- get the table id="table1"
 var t = document.getElementById(tablename);
  // - get the first tbody tag belonging to var t (this is where our inputs are )
 var tb = t.getElementsByTagName('tbody')[0];
 var tr0 = tb.childNodes[0];

 var myClone = tr0.cloneNode(true);
 // -- get the number of inputs contained in the cloned row
 var newInpt = myClone.getElementsByTagName('input');
 
 // -- for Mac IE 5, the appendChild statement must be here instead of
 // -- the end of the function or the row won't be added
 // -- tb.appendChild(myClone)

for (i=0; i < newInpt.length; i++){
  // -- get only the non-numeric part of the name using a regular expression; 
  // -- concatinate it with inc_num to create the new name
 var newName = newInpt[i].name.substring(newInpt[i].name);
 newInpt[i].setAttribute('name',newName);
 newInpt[i].setAttribute('value', "");
 }
 inc_num1++;

tb.appendChild(myClone);
}

// function name	: removeRowT1
// purpose			: to remove row from table 1
function removeRowT1(tablename)
{
  // -- get the table id="table1"
  var t = document.getElementById(tablename);
  // - get the first tbody tag belonging to var t (this is where our inputs are )
  var tb = t.getElementsByTagName('tbody')[0];
  var tr0 = tb.lastChild;
  if (inc_num1 == 1) {
	 // do nothing
  } else {
	 tb.removeChild(tr0); 
	 inc_num1--;
  }	
}

// function name	: removeRowT2
// purpose			: to remove row from table 2
function removeRowT2(tablename)
{
  // -- get the table id="table2"
  var t = document.getElementById(tablename);
  // - get the first tbody tag belonging to var t (this is where our inputs are )
  var tb = t.getElementsByTagName('span')[0];
  var tr0 = tb.lastChild;

  if (inc_num2 == 1) {
	 // do nothing
  } else {
	tb.removeChild(tr0); 
	inc_num2--;
  }	
}

// function name	: removeRowT3
// purpose			: to remove row from table 3
function removeRowT3(tablename)
{
  // -- get the table id="table3"
  var t = document.getElementById(tablename);
  // - get the first tbody tag belonging to var t (this is where our inputs are )
  var tb = t.getElementsByTagName('tbody')[0];
  var tr0 = tb.lastChild;
  if (inc_num3 == 1) {
	 // do nothing
  } else {
	 tb.removeChild(tr0); 
	 inc_num3--;
  }	
}
