
// Author: Brian Strickland
// Date Created: 10/5/07
// Last Modified: 10/8/07

// Note: Works with IE and Firefox.

// Used to keep track of what author is being added or deleted, also indexes the table for inserting
// and deleting rows.  (Couldn probably get rid of this and find a more non static approach.)
// Set to the row_index of where you want your dynamic content to start (counting from 0)
var count = 2;  
var start = 2;  

// This function will dynamcially insert a new row with 2 columns into the given table.
function add_row(){

	if(count > 30)
		return;
	
	// Gather and create all of the elements
	var table = document.getElementById('my_table');
	var input = document.createElement('input');
	var check = document.createElement('input');
	var text = document.createTextNode('Undergraduate Author');
	var author = document.createTextNode('Author #' + count);
	var remove = document.createElement('a');
	
	// Add apporitate attributes to the two inputs and anchor elements.
	input.type = 'text';
	input.setAttribute('id', 'auth'+ count);
	input.setAttribute('name', 'auth'+ count);
	check.type = 'checkbox';
	check.setAttribute('id', 'undergrad'+ count);
	check.setAttribute('name', 'undergrad'+ count);
	remove.setAttribute('href', '#');

	// This is used to keep track of what row the element is a child of.
	remove.setAttribute('id', count);
		
	// Set the anchor name ie: <a ...>Name</a>
	if(navigator.appName == "Microsoft Internet Explorer"){
		remove.innerText = '(Remove Author)';
		remove.onclick=function() { delete_row(this); };
	}
	else {
		remove.innerHTML = '(Remove Author)';
		remove.setAttribute('onclick', 'delete_row(this);');
	}
	
	// Create a new row in the apportiate position of the table
	var row = table.insertRow(count);

	// Add the first column and insert the author text into the cell.
	var col = row.insertCell(-1);
	col.appendChild(author);

	// Add extra spacing when outputted
	col.appendChild(document.createTextNode('                  '));
	col.appendChild(remove);
	
	// Add the second column and insert the textbox, checkbox and text.
	var col2 = row.insertCell(-1);
	col2.appendChild(input);
	col2.appendChild(check);
	col2.appendChild(text);

	count++;

}

/*
	This function just deletes the specified row from the table.
*/

function delete_row(row){

	// Find the row that needs to be deleted.
	var table = document.getElementById('my_table');
	var row_index = row.getAttribute('id');

	// Delete the columns and the row from the table.
	table.deleteRow(row_index);
	
	count--;	// Remove number of authors
	// Make the numbers look right.  (At least to the user, PHP script won't care)
	reset_count(table);

}

/*
	After a user deletes a node from the table.  The table needs to reset itself...
	It resets itself by setting the Author # and <a> id  to the new corresponding
	rows that they occupy.
*/
function reset_count(table){

	// This gives every row in the table.
	var rows = table.getElementsByTagName("TR");
	var author = '';
	
	while(start < count){
	
		// Chaning the value of HTMLLabelElement
		if(rows[start].childNodes[0].childNodes[0] != null){

			// Change display data.
			rows[start].childNodes[0].childNodes[0].data = 'Author #' + start;
		
			// Change the Anchor id
			rows[start].childNodes[0].childNodes[2].setAttribute('id', start);		
		}
		start++;
	}
	start = 2;

}