//FILE table_tricks.js
//FUNCTIONS TO CREATE AND MANIPULATE HTML TABLE;
//MODIFIED BY BS 2008 MAR
//These codes might not be the most efficient one. But they work for me.

//SOURCE:
//http://developer.mozilla.org/en/docs/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces#Introduction


	function readData(row,column)
	{
			//read cell data
		var mybody      = document.getElementsByTagName("body")[0];
		var mytable     = mybody.getElementsByTagName("table")[0];
		var mytablebody = mytable.getElementsByTagName("tbody")[0];
		var myrow       = mytablebody.getElementsByTagName("tr")[row];
		var mycel       = myrow.getElementsByTagName("td")[column];
	
		// first item element of the childNodes list of mycel
		var myceltext=mycel.childNodes[0];
	
		// content of currenttext is the data content of myceltext
		var currenttext=document.createTextNode(myceltext.data);
		//mybody.appendChild(currenttext);
		return (myceltext.data);

	}

	function writeData(row,column,data)
{	//write data into a cell in a table
	var mybody      = document.getElementsByTagName("body")[0];
	var mytable     = mybody.getElementsByTagName("table")[0];
	var mytablebody = mytable.getElementsByTagName("tbody")[0];
	var myrow       = mytablebody.getElementsByTagName("tr")[row];
	var mycel       = myrow.getElementsByTagName("td")[column];

	// first item element of the childNodes list of mycel
	var myceltext=mycel.childNodes[0];

	//if the data is number, store it in the value property 
	if(!isNaN(data)){mycel.setAttribute('value',data);}		//the value will be hidden and useful even though what shows in the cell is changed by jquery

	myceltext.data=data;
}

    function makeTable(rows,columns) {

	//Remove any table if exists
	removeTables();

        // get the reference for the body
        var body = document.getElementsByTagName("body")[0];

        // creates a <table> element and a <tbody> element
        var tbl     = document.createElement("table");
        var tblBody = document.createElement("tbody");

        // creating all cells
        for (var j = 0; j < rows; j++) {
            // creates a table row
            var row = document.createElement("tr");

            for (var i = 0; i < columns; i++) {
                // Create a <td> element and a text node, make the text
                // node the contents of the <td>, and put the <td> at
                // the end of the table row
                var cell = document.createElement("td");
	
                var cellText = document.createTextNode("R"+j+", C"+i);
                cell.appendChild(cellText);
	cell.setAttribute("align","right");	//alignment
                row.appendChild(cell);
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }

        // put the <tbody> in the <table>
        tbl.appendChild(tblBody);
        // appends <table> into <body>
        body.appendChild(tbl);
        // sets the border attribute of tbl to 1;
        tbl.setAttribute("border", "1");
        tbl.setAttribute("cellspacing", "0");
    }


function removeTables()
{
	//Remove all tables in html page
	var body=document.getElementsByTagName("body")[0];

	//count table
	var count=(body.getElementsByTagName("table").length);

	//alert("Tables found = "+count);

	//if table exists, remove
	if(count>0)
	{
		for(var i=0;i<count;i++)
		{
			var aTable=body.getElementsByTagName("table")[0];
			body.removeChild(aTable);
			//alert("Table " + i + " vaporized!");
		}
	}
	else
	{
		//do nothing
	}

}

function indentTable()
{
	//trying to indent the only table in html
	mytable     = document.body.getElementsByTagName("table")[0];
	mytable.setAttribute("style","margin-left:52px;");
	//alert("table indented now?");
}

	function countMyRows(tableIndex)
{	//count total rows in the table
	mybody      = document.getElementsByTagName("body")[tableIndex];
	mytable     = mybody.getElementsByTagName("table")[tableIndex];
	mytablebody = mytable.getElementsByTagName("tbody")[tableIndex];

	var count=(mytablebody.getElementsByTagName("tr").length);
	//alert("rows counted = "+'\n'+count+'\ncalled from table_tricks.js');
	return count;
}

function countMyColumns(tableId)
{
	var t=document.getElementById(tableId);
	return t.getElementsByTagName('td')[0].length
}
