/**
 *  index.js
 *
 *  Implement dynamic functionality specific to the home page.
 *
 *  History:
 *	2010/11/21	add copyright notice
 *			add extra cell in tabs table
 *	2011/11/10	click anywhere in a tab displays the associated page
 *			improve separation of HTML and Javascript
 *
 *  Copyright &copy; 2011 James Cobban
 **/

window.onload	= indexLoaded;

/**
 *  indexLoaded
 *	
 *  This method is called when the document has been loaded.
 **/
function indexLoaded()
{
    var	tabsTable	= document.getElementById("tabsTable");
    var tabsRow		= tabsTable.rows[0];
    var	cells		= tabsRow.getElementsByTagName("TD");
    for (var i = 0; i < cells.length; i++)
    {		// for each data cell in the row
	var cell	= cells[i];
	cell.onclick	= tabSel;	// activate an event method
    }		// for each data cell in the row
}		// indexLoaded

/**
 *  tabSel
 *
 *  This method is called when the user clicks on a tab.
 **/
function tabSel(e)
{
    if (!e)
	e	= window.event;
    var	tabsTable	= document.getElementById("tabsTable");
    var tabsRow		= tabsTable.rows[0];
    var	cells		= tabsRow.getElementsByTagName("TD");
    for (var i = 0; i < cells.length - 1; i++)
    {		// for each data cell in the row
	var cell	= cells[i];
	if (cell != this)
	    cell.className	= "tabs";	// set to standard style
    }		// for each data cell in the row
    this.className		= "tabsFront";
    for (i = 0; i < this.childNodes.length; i++)
    {
	var child	= this.childNodes[i];
	if (child.nodeName == 'A' && child.href.length > 0)
	{	// hyperlink
	    window.open(child.href, "frame");
	    break;
	}	// hyperlink
    }
}		// tabSel

/**
 *  getElt
 *
 *  This method finds the first instance of a child node
 *  that matches the supplied tag name.
 **/
function getElt(parent, tagName)
{
	for (var i = 0; i < parent.childNodes.length; i++)
	{
		var	cNode	= parent.childNodes[i];
		if ((cNode.nodeType == 1) && (cNode.nodeName == tagName))
				return cNode;
	}		// loop through children of parent
	return null;
}		// getElt

/**
 *  getEltId
 *
 *  This method finds the first instance of a child node
 *  that matches the supplied tag name and id value.
 **/
function getEltId(parent, tagName, idValue)
{
//	var alertTxt = "getEltId(,\"" + tagName + "\", \"" + idValue + "\")\n";
	for (var i = 0; i < parent.childNodes.length; i++)
	{
		var	cNode	= parent.childNodes[i];
//		if(cNode.nodeType == 1) 
//				alertTxt	+= "<" + cNode.nodeName
//						+ " id=\"" + cNode.id + "\">\n";
		if ((cNode.nodeType == 1) 
			&& (cNode.nodeName == tagName)
			&& (cNode.id == idValue))
		{
			//alert(alertTxt);
			return cNode;
		}
	}		// loop through children of parent
	//alert(alertTxt + " returning null\n");
	return null;
}		// getElt

/**
 *  getText
 *
 *  This method accumulates the text nodes
 *  under a specified node.
 **/
function getText(parent)
{
	var		text	= "";
	for (var j = 0; j < parent.childNodes.length; ++j)
	{
		var	sub	= parent.childNodes[j];
		if (sub.nodeType == 3)
		{		// text node
			if (text.length > 0)
				text	+= " ";
			text 		+= sub.nodeValue;
		}		// concatenate all text elements
	}		// loop through children of source node
	return text;
}		// getText

/**
 *  tagToString
 *
 *  Extract a the string representing the XML or HTML corresponding
 *  to the specified node and its children.
 *
 *  Parameters:
 *	node	the top of the tree of nodes to be interpreted
 *
 *  Returns:
 *	String representation of the node and its children.
 **/
function tagToString(node)
{
    var	retval;
    if (node.nodeType == 1)
    {		// element
	retval	= "<" + node.nodeName;
    }
    else
    if (node.nodeType == 3)
    {		// text
    }
    else
    {
	retval	= "<nodeType=" + node.nodeType;
    }

    if (node.attributes)	
    {
	for (var i = 0; i < node.attributes.length; i++)
	{
	    var attrname	= node.attributes[i].name;
	    if (attrname.substr(0,2) == "on")
		continue;		// ignore event methods			
	    if (attrname.substr(0,4) == "aria")
		continue;		// ignore M$ aria attributes
	    var	value	= node.attributes[i].value;
	    if (value.length > 24)
		value	= value.substr(0,21) + "...";
	    retval	+= " " + node.attributes[i].name + "=\'" 
		+ value + "\'";
	}
    }

    if (node.nodeType == 3)
    {		// text node
	if (node.nodeValue !== undefined)
	    retval += node.nodeValue.trim();
    }		// text node
    else
    {		// not a text node
	retval	+= ">\n";
	for (var i = 0; i < node.childNodes.length; i++)
	{
	    var	child	= node.childNodes[i];
	    if (child.nodeType == 1)
		retval	+= tagToString(child);
	    else
	    if (child.nodeType == 3)
		if (child.nodeValue !== undefined)
		    retval += child.nodeValue.trim();
		
	}
    
	if (node.nodeValue)
	{		// text
	    retval += "value=\"" + node.nodeValue.trim() + "\"\n";
	}		// text
    }		// not a text node

    if (node.nodeType == 1)
    {		// close element
	retval	+= "</" + node.nodeName + ">\n";
    }		// close element
    else
    if (node.nodeType == 3)
    {		// close text
    }		// close text
    else
    {		// close other node type
	retval	+= "</nodeType=" + node.nodeType + ">\n";
    }		// close other node type
    return retval;
}		// tagToString

