
var newList = [['','Select'],['1st Adult, :40','1st Adult'],['Senior, :25','Senior'],['1st Child, :25','1st Child'],['Young Child, :0','Young Child'],['Guest/Babysitter, :25','Guest/Babysitter']];

var aAccum = 0;
var cAccum = 0;


function SetList(row){

var prevVal = document.getElementById("productpr" + row).value;

var adultArray = [['1st Adult, :40','1st Adult'],['2nd Adult, :35','2nd Adult'],['Addtl Adult, :25','Additional Adult']];
var childArray = [['1st Child, :25','1st Child'],['2nd Child, :20','2nd Child'],['Addtl Child, :15','Additional Child']];


// This will match the value selected in the previous member type selection box.  Once matched to the poper array, it will increase the accumulator for that array and set a new value for the NewList array.  If the accumulators are greater than two, it sets the NewList array values to the final value in the adultArray and/or childArray.
 	
	for (var i = 0; i <= 2 ; i++)
	{
		if ( prevVal == adultArray[i][0])
		{
			if (aAccum < 2)
			{
				aAccum++;
				newList[1][0] = adultArray[aAccum][0];
				newList[1][1] = adultArray[aAccum][1];
			}
			else
			{
				newList[1][0] = adultArray[2][0];
				newList[1][1] = adultArray[2][1];
			}
		
		}		
		else if ( prevVal == childArray[i][0])
		{
			if (cAccum < 2)
			{
				cAccum++;
				newList[3][0] = childArray[cAccum][0];
				newList[3][1] = childArray[cAccum][1];
			}
			else
			{
				newList[3][0] = childArray[2][0];
				newList[3][1] = childArray[2][1];
			}
		}
	}

}	

function addRowToTable()
{
  var tbl = document.getElementById('mTable');
  var lastRow = tbl.rows.length;
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);  
  var prevRow  = iteration - 1;
  

  // cell Person Number
  var cellpNum = row.insertCell(0);
  cellpNum.innerHTML = '<h2> Person ' + iteration + '</h2>';

  
  // cell Member Type
  var cellmType = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'productpr' + iteration;
  sel.id = 'productpr' + iteration;
  
  //Calls the function and passes it the value of the previous selection box
  SetList(prevRow);
  
    
  //Creates the options using the NewList array for the new selction box
  for (var i = 0; i < newList.length; i++)
  {
	  sel.options[i] = new Option(newList[i][1],newList[i][0]);
  }

  cellmType.appendChild(sel);


  // This creates/inserts the third cell into row
  var cellMemName = row.insertCell(2);
  
  //This creates & inserts the Member Name Text Box for the last cell
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'product' + iteration + '[]';
  el.id = 'product' + iteration;
  el.size = 30;
  cellMemName.appendChild(el);

  // This creates/inserts the third cell into row
  var cellDOB = row.insertCell(3);

  //This creates & inserts the DOB Text Box for the last cell
  var dob = document.createElement('input');
  dob.type = 'text';
  dob.name = 'product' + iteration + '[]';
  dob.id = 'product' + iteration;
  dob.size = 15;
  cellDOB.appendChild(dob);
  
  //This creates & inserts the Hidden field for qty for the last cell
  var Qty = document.createElement('input');
  Qty.type = 'hidden';
  Qty.name = 'qty' + iteration;
  Qty.value = 1;
  cellDOB.appendChild(Qty);

}

function addOption(selectbox, value, text)
{
	var optn = document.createElement("OPTION");
	optn.value = value;
	optn.text = text;
	

	selectbox.options.add(optn);
}

function addRunner()
{
  var tbl = document.getElementById('mTable');
  var lastRow = tbl.rows.length;
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  var prevRow  = iteration - 1;
  

  // cell Person Number
  var cellpNum = row.insertCell(0);
  cellpNum.innerHTML = '<h2> Runner ' + iteration + '</h2>';

  
  // cell Member Type
  var cellmType = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'productpr' + iteration;
  sel.id = 'productpr' + iteration;
  
  addOption(sel,'','Select');
  addOption(sel,'15k, :35','15k');
  addOption(sel,'5k, :25','5k');
  
  cellmType.appendChild(sel);


  // This creates/inserts the third cell into row
  var cellMemName = row.insertCell(2);
  
  //This creates & inserts the Member Name Text Box for the last cell
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'product' + iteration + '[]';
  el.id = 'product' + iteration;
  el.size = 25;

  cellMemName.appendChild(el);

  // This creates/inserts the third cell into row
  var cellDOB = row.insertCell(3);

  //This creates & inserts the DOB Text Box 
  var dob = document.createElement('input');
  dob.type = 'text';
  dob.name = 'product' + iteration + '[]';
  dob.id = 'product' + iteration;
  dob.size = 10;
  cellDOB.appendChild(dob);
  
  //This creates & inserts the Gender cell & selection box 
  var cellGender = row.insertCell(4);
  var gender = document.createElement('select');
  gender.name = 'product' + iteration + '[]';
  gender.id = 'productpr' + iteration;

  addOption(gender,'','Select');
  addOption(gender,'Female','Female');
  addOption(gender,'Male','Male');
  
  cellGender.appendChild(gender);
  
 /* //This creates & inserts the Discount Cell and checkbox input
  
  var cellMCU = row.insertCell(5);
  var MCU = document.createElement('input');
  MCU.type = 'checkbox';
  MCU.name = 'runner' + iteration;
 
  cellMCU.appendChild(MCU);
//  <input name="member1" type="checkbox" value="" onselect="applyDiscount()" />
 */ 
  
  //This creates & inserts the Hidden field for qty for the last cell
  var Qty = document.createElement('input');
  Qty.type = 'hidden';
  Qty.name = 'qty' + iteration;
  Qty.value = 1;
  cellGender.appendChild(Qty);

}

function applyDiscount()
{
	  var tbl = document.getElementById('mTable');
	  var lastRow = tbl.rows.length;
	  var iteration = lastRow - 1;
	  
  	  document.getElementById('qty11').value = iteration;
	  
	  
	  //var testVal = document.getElementById('qty11').value;
	  //document.getElementById('test').innerHTML = testVal;
	  
}