/****************************************

	Insert Row
	version: 1.0
	last revision: 01.11.2005
	steve@slayeroffice.com

	Should you improve upon this code
	please let me know so that I can 
	update the version hosted at slayeroffice

	Please leave this notice in tact.

****************************************/

window.onload = table_init;

function table_init() {
	assignRowIndex();
	initButtonClickEvent();
}

function initButtonClickEvent() {
	// reference to the table element
	mTable = document.getElementById("nTable");
	// reference to all the A elements in the table
	btn = mTable.getElementsByTagName("a");
	for(i=0;i<btn.length;i++) {
		// if the anchor tags class name is "button", assign the onclick event
		if(btn[i].className == "button") {
			btn[i].onclick = function() { createNewRow(this,this.parentNode.parentNode.xindex); }
		}
	}
}

function assignRowIndex() {
	// reference pointer to the table element
	mTable = document.getElementById("nTable");
	// loop over the length of rows in the table, assigning them an index and zebra striping them
	for(i=0;i<mTable.rows.length;i++) {
		mTable.rows[i].xindex = i;
		mTable.rows[i].className = i%2?"rowOn":"rowOff";
	}
}

function resetButtonText() {
	mTable = document.getElementById("nTable");
	btn = mTable.getElementsByTagName("a");
	for(i=0;i<btn.length;i++) {
		if(btn[i].className == "button" && getTextNodeValue(btn[i]) == "Close") {
			removeTextNodes(btn[i]);
			btn[i].appendChild(document.createTextNode("More"));
			break;
		}
	}
}

function createNewRow(node,nodeIndex) {
	// check to see if an inserted row already exists
	if(document.getElementById("tmpRow")) {
		// get the index of the inserted row relative to the hard coded rows
		prevIndex = document.getElementById("tmpRow").tmpIndex;
		// if that index is less than the index passed in, decrement nodeIndex to compensate for the previously inserted
		// row taking up an index in the row collection
		if(prevIndex<nodeIndex)nodeIndex--;
		// remove the previously inserted row from the DOM
		killPreviousRow();
		// the user has clicked on the same A they did before. change the button text to "More"
		if(prevIndex == nodeIndex) {
			removeTextNodes(node);
			node.appendChild(document.createTextNode("More"));
			assignRowIndex();
			// nothing left to do, return out.
			return;
		}
	}
	// make sure the buttons say what they are supposed to.
	resetButtonText();

	// make the button that was just clicked say "Close" instead of "More"
	removeTextNodes(node);
	node.appendChild(document.createTextNode("Close"));

	// reference to the table
	nParentNode = document.getElementById("nTable");
	// create the new row
	r = nParentNode.insertRow(nodeIndex+1);
	// assign attributes to the new row
	r.setAttribute("id","tmpRow");
	r.tmpIndex = nodeIndex;
	// append a TD to the row and assign attributes
	td = r.appendChild(document.createElement("td"));
	td.setAttribute("colSpan",4);
	td.setAttribute("class","newRow");
	td.appendChild(document.createTextNode("This is the new row from row " + nodeIndex));
	assignRowIndex();
}

function killPreviousRow() {
	// remove the previous row from the DOM
	pElement = document.getElementById("tmpRow").parentNode;
	pElement.removeChild(document.getElementById("tmpRow"));
	assignRowIndex();
}

// utility methods

function removeTextNodes(parentObj) {
	_i=0;
	while(parentObj.childNodes[_i]) {
		if(parentObj.childNodes[_i].nodeType == 3) parentObj.removeChild(parentObj.childNodes[_i]);
		_i++;
	}
}

function getTextNodeValue(parentObj) {
	returnString = "";
	_i=0;
	while(parentObj.childNodes[_i]) {
		if (parentObj.childNodes[_i].nodeType == 3) returnString+=parentObj.childNodes[_i].nodeValue;
		_i++;
	}
	return returnString;
}
