$(document).ready(function() { 

function initAddRemoveRowHandlers(){
			  $("tr td a.add").unbind('click');
			  $("tr td a.add").bind('click', function() {
							  var tableRow = $(this).parents('tr:first');
							  var newTableRow = $("<tr>").html(tableRow.html());
							  var table  = tableRow.parents('table:first')
							  tableRow.children('td:last').children('a').remove();
							  newTableRow.appendTo(table);

							  // only show remove button if there are more than 1 row in table
							  if($(newTableRow).parents('table').find('tr').length > 2){
											 newTableRow.find('a.remove').show();
							  }else{
											 newTableRow.find('a.remove').hide();
							  }
							  initAddRemoveRowHandlers();
			  });

			  $("tr td a.remove").unbind('click');
			  $("tr td a.remove").bind('click', function(){
			  				  var prevRow = $(this).parents('tr').prev(); 
							  prevRow.children('td:last').html($(this).parent().html());
							  $(this).parents('tr').remove();

							  // only show remove button if there are more than 1 row in table
							  if(prevRow.parents('table').find('tr').length > 2){
									prevRow.find('a.remove').show();
							  }else{
									prevRow.find('a.remove').hide();
							  }

							  initAddRemoveRowHandlers();
			  });
}
initAddRemoveRowHandlers();

});
