// 
//	TYGR Productions Interactive Statistics
//	
//	This file contains JavaScript support functions for TYGR Productions' Interactive Statistics.  The code contained herein
//  is NOT in the public domain.  This file is provided for download to personal computers and is only authorized for personal 
//  use in conjuction with TYGR Productions' Interactive Statistics.  Copying or reposting this file in whole or in part on 
//  any web server or other network server, or reusing any portion of the code is not allowed without express written consent 
//  of the author.  Any commercial use is strictly prohibited.
//	
//	Copyright (c) 2005 by Jon T. Camp.  All rights reserved.
// 
function appendBreak(parentElement) 
{
	return appendElement(parentElement, "br");
}

function appendCell(parentElement, cText, cClassName, nWidth, nColSpan, nRowSpan) 
{
	var td = appendElement(parentElement, "td");
	
	if (cText != null) appendText(td, cText);
	if (cClassName != null) td.className = cClassName;
	if (nWidth != null) td.width = nWidth;
	if (nColSpan != null) td.colSpan = nColSpan;
	if (nRowSpan != null) td.rowSpan = nRowSpan;
	
	return td;
}

function appendCellHTML(parentElement, cHTML, cClassName, nWidth, nColSpan, nRowSpan) 
{
	var td = appendElement(parentElement, "td");
	
	td.innerHTML = cHTML;
	if (cClassName != null) td.className = cClassName;
	if (nWidth != null) td.width = nWidth;
	if (nColSpan != null) td.colSpan = nColSpan;
	if (nRowSpan != null) td.rowSpan = nRowSpan;
	
	return td;
}

function appendElement(parentElement, cElementName) 
{
	var elementChild = document.createElement(cElementName);
	parentElement.appendChild(elementChild);
	return elementChild;
}

function appendHeaderCell(parentElement, cText, cClassName, nWidth, nColSpan, nRowSpan) 
{
	var th = appendElement(parentElement, "th");
	
	if (cText != null) appendText(th, cText);
	if (cClassName != null) th.className = cClassName;
	if (nWidth != null) th.width = nWidth;
	if (nColSpan != null) th.colSpan = nColSpan;
	if (nRowSpan != null) th.rowSpan = nRowSpan;
	
	return th;
}

function appendHeaderCellHTML(parentElement, cHTML, cClassName, nWidth, nColSpan, nRowSpan) 
{
	var th = appendElement(parentElement, "th");
	
	th.innerHTML = cHTML;
	if (cClassName != null) th.className = cClassName;
	if (nWidth != null) th.width = nWidth;
	if (nColSpan != null) th.colSpan = nColSpan;
	if (nRowSpan != null) th.rowSpan = nRowSpan;
	
	return th;
}

function appendRow(parentElement, cClassName, nHeight) 
{
	var tr = appendElement(parentElement, "tr");
	
	if (cClassName != null) tr.className = cClassName;
	//else tr.bgColor = "#FFFFFF";
	if (nHeight != null) tr.height = nHeight;
	
	return tr;
}

function appendText(parentElement, cText) 
{
	var text = document.createTextNode(cText);
	parentElement.appendChild(text);
	return text;
}

function appendTable(nodeStat) 
{
	var cStatName = nodeStat.nodeName;
	var cCaption = nodeStat.attributes.getNamedItem("caption").value;
	var cStatusId = nodeStat.attributes.getNamedItem("statusId").value;
	var cTableId = nodeStat.attributes.getNamedItem("tableId").value;
	var td;
	var tr;
	var tableStats;
	var tableStatus;
		
	var tableLayout = document.getElementById("tableStatsLayoutId");
	
	if (tableLayout)
	{
		tr = appendRow(tableLayout.tBodies[0]);
		
		td = appendCell(tr);
		td.align = "left";
		//if ((cTableId == "tablePassingOffenseId") || (cTableId == "tableRushingOffenseId") || (cTableId == "tableTotalOffenseId")) td.bgColor = "#CF98D2";
		//else td.bgColor = "#FFF5DE";
		
		tableStats = appendElement(td, "table");
		tableStatus = appendElement(td, "table");
		appendBreak(td);
		appendBreak(td);
	}
	else 
	{
		tableStats = appendElement(docbody, "table");
		tableStatus = appendElement(docbody, "table");
		appendBreak(docbody);
		appendBreak(docbody);
	}

	tableStats.id = cTableId;
	tableStats.width = 760;
	
	if (bDefaultFormat) 
	{
		tableStats.border = 1;
	}
	else 
	{
		tableStats.border = 0;
		tableStats.cellPadding = 0;
		tableStats.cellSpacing = 0;
	}
	
	appendElement(tableStats, "thead");
	appendElement(tableStats, "tbody");
	appendElement(tableStats, "tbody");
	
	tableStatus.border = tableStats.border;
	tableStatus.width = tableStats.width;
	var tbody = appendElement(tableStatus, "tbody");
	tr = appendRow(tbody);
	
	if (bDefaultFormat) 
	{
		td = appendCell(tr, "Click on column heading to sort");
		td.id = cStatusId;
		td.className = "status"
	}
	else 
	{
		appendCell(tr, "", "border", 1);
		td = appendCellHTML(tr, "&nbsp;Click on column heading to sort");
		td.id = cStatusId;
		td.className = "status"
		appendCell(tr, "", "border", 1);
		tr = appendRow(tbody, "border", 1);
		td = appendCell(tr);
		td.colSpan = 3;
		tableStatus.cellPadding = tableStats.cellPadding;
		tableStatus.cellSpacing = tableStats.cellSpacing;
	}
		
	var tableData = appendTableData(nodeStat, cCaption, cStatName, cStatusId, cTableId);

	initTable(tableData);
}

function appendTableData(nodeStat, cCaption, cStatName, cStatusId, cTableId) 
{
	var nodeElement;
	var tableData = new Object();
	
	tableData.ByValue = [];
	tableData.Caption = cCaption;
	tableData.Captions = [];
	tableData.ColWidths = [];
	tableData.Descending = [];
	tableData.Elements = [];
	tableData.IDs = [];
	tableData.IDHeaderRows = [];
	tableData.IDHeaderRowSpans = [];
	tableData.LeftAlign = [];
	tableData.Node = nodeStat;
	tableData.Ptr = [];
	tableData.Rank = [];
	tableData.SortIndex = 0;
	tableData.Status = [];
	tableData.StatusElement = document.getElementById(cStatusId);
	tableData.Table = document.getElementById(cTableId);
	
	for (var i = 0; i < tableData.Node.childNodes.length; i++) 
	{
		nodeElement = tableData.Node.childNodes.item(i);
		tableData.Elements[i] = nodeElement.nodeName;
	}
	
	tableData.DataColCount = tableData.Elements.length;
	if (bDefaultFormat) tableData.TableColCount = tableData.DataColCount
	else tableData.TableColCount = (tableData.DataColCount * 2) + 1;
	aTableData[aTableData.length] = tableData;
	
	return tableData;
}
