// JavaScript Document
<!--
var xtable				= null;
var spare				= null;
var lineIndex 			= 0;
var rowID				= 0;

function closedate(e)
{
	// find the table cell containing the button clicked and hide the following lines
	setNode(e, 'src', 'common/icons/' + 'plus.gif', opendate);
	hideRows(xtable.rows[lineIndex], 'divider');
}

function opendate(e)
{
	// find the table cell containing the button clicked and show the following lines
	setNode(e, 'src', 'common/icons/' + 'minus.gif', closedate);
	recoverRows();
}

function closebreakout(e)
{
	// find the table cell containing the button clicked and hide the following lines
	setNode(e, 'src', 'common/icons/' + 'plus.gif', openbreakout);
	hideRows(xtable.rows[lineIndex], 'breakout');
}

function openbreakout(e)
{
	// find the table cell containing the button clicked and show the following lines
	setNode(e, 'src', 'common/icons/' + 'minus.gif', closebreakout);
	recoverRows();
}

// find the node containing the image clicked on.
function findNode(e)
{
	if (xtable == null || spare == null) { initcopy(); }
	// find which table cell was clicked on
	lineIndex 	= gv_findCell(e);
  	var x		= xtable.rows[lineIndex];			// Start row
	// find the 1st cell with an img with src
	for(i=0; i<x.childNodes.length; i++)
	{
		var cell = x.childNodes[i];
		var node = cell.childNodes[0];
		if(node.getAttribute('src')) { return node; }
	}
	return false;
}

function setNode(e, attr, val, func)
{
	var node = findNode(e);
	if(node) 
	{ 
		node.setAttribute(attr, val);
		node.onclick 	= func;
	}

}

function initcopy()
{
	// find the spare table, and move all the breakout rows into it.
	if (xtable == null) { xtable	= new gv_getObj('subfile').obj; }
	if (spare == null)  { spare		= new gv_getObj('sparetable').obj; }
	for(lineIndex=0; lineIndex<xtable.rows.length; lineIndex++)
	{
	  	var x			= xtable.rows[lineIndex];
		// if a breakout header, hide the following rows 
		if(x.getAttribute('class') == 'breakout') { hideRows(x, 'breakout'); }
	}
}

function hideRows(row, endclass)
{
	lineIndex++;
	var ID	 			= null;
	var firstID			= null;
	var rowclass		= row.getAttribute('class');
	if(!rowclass) { rowclass = "noclass"; }
// remove any previous start and end IDs
	var s				= rowclass.indexOf(' start');
	if(s > 0) { rowclass	= rowclass.substring(0, s) };
	for(i=lineIndex; i<xtable.rows.length; i++)
	{
	  	var x			= xtable.rows[i];
		var c 			= x.getAttribute('class');
		if(c != null) 
		{ 
		// stop at row of the given class 
		//   class might by now have row numkbers and such on the end,
		//   so stop if the 1st part of the class string matches.
			if( c.substring(0, endclass.length) == endclass) { break; }  
		}
		ID 				= hideRow(x);
		i--;		// moving the row renumbers the following rows
		if(firstID == null) 
		{ 
			firstID		= ID;
			rowclass	+= ' start' + ID; 
		}
	}
	if(ID != null) 
	{ 
		rowclass		+= ' end' + ID; 
		row.setAttribute('class', rowclass);
	}
}

function hideRow(row)
{
	// get an ID, and mark the row with the ID
	rowID++;
	var endString				= null;
	var c 						= row.getAttribute('class');
	if (c == null || c == "") { c					= "noclass"; }
	var startCol				= c.indexOf(' row') -1;
	var endCol					= c.indexOf(' start');
	if(endCol >= 0) { endString		= c.substring(endCol); }
	if (startCol < 0) { startCol 	= endCol; }
	if (startCol < 0) { startCol 	= c.length; }
	var newclass				= c.substring(0, startCol) + ' row' + rowID;
	if(endString != null) { newclass 	= newclass + endString; }
	row.setAttribute('class', newclass);
	// move the row to the spare table
	spare.tBodies[0].appendChild(row);
	// return the ID
	return rowID;
}

function recoverRows()
{
	// find the row we are to insert before
	var i			= lineIndex + 1;
	var nextRow		= null;
	if (i < xtable.rows.length) { nextRow = xtable.rows[i]; }
	// find the row & get it start and end ids from its class
	var forRow		= xtable.rows[lineIndex];
	var c			= forRow.getAttribute('class');
	var startcol	= c.indexOf(' start') + 6;
	var endcol		= c.indexOf(' end');
	var start		= parseInt(c.substring(startcol, endcol));
	var upto		= parseInt(c.substring(endcol + 4));
	// loop through the rows in the spare table
	for(i=0; i<spare.rows.length; i++)
	{
	  	var x		= spare.rows[i];
		// for any in the right range, move them back to the main table
		var c2		= x.getAttribute('class');
		if (c2 == null || c2 == "") { c2 = "noclass"; }
		startcol	= c2.indexOf(' row') + 4;
		endcol		= c2.indexOf(' start');
		if(endcol < 0) { endcol = c2.length; }
		thisrow		= parseInt(c2.substring(startcol, endcol));
		if(start <= thisrow && thisrow <= upto) 
		{
			xtable.tBodies[0].insertBefore(x, nextRow)
			i--;
		}
	}
}
	