// 
// File:	PageGrid.js
// Author:	Rob Allen
// Created:	January 15, 2002
//
// Purpose:	To provide a simple page grid, having multiple columns of 
//		one or more records with the labels located at the top of the 
//		grid. Fields are defined by the FieldFactory class. 
//
// Copyright (c) 2002, ThoughtByDesign. All rights reserved.
// 
// This computer program is protected by copyright law and international treaties.
// Unauthorized reproduction or distribution of this program, or any portion of it,
// may result in severe civil and criminal penalties, and will be prosecuted to the
// maximum extent possible under the law.
// 
// Modification History: 
//	RLA, July 28, 2002, Creation and initial definition
// 


function PageGrid(title) {
	this._title = title;
	this._labels = new Array();
	this._spacing = 2;
	this._headerStyleName = "clsTableHeader";
	this._padding = 0;
	this._gridWidth = "90%";
	this._columnStyleName = new Array();
	this._totalColumn = PageGrid.k_NO_TOTAL;
	this._totalValue = PageGrid.k_NO_TOTAL;
}


PageGrid.prototype.setColumnLabels = function(columnLabels) {
	for (var i = 0; i < columnLabels.length; i++) {
		this._labels[i] = {_primary: columnLabels[i], _sub: null};
	} // for
} // setColumns()


PageGrid.prototype.addSubLabels = function(primaryIdx, labels) {
	
	this._labels[primaryIdx]._sub = labels;
	
} // addSubLabels()


PageGrid.prototype.setData = function(data) {
	this._data = data;
} // setData()


PageGrid.prototype.setSpacing = function(spacing) {
	this._spacing = spacing;
} // setSpacing()


PageGrid.prototype.setPadding = function(padding) {
	this._padding = padding;
} // setPadding()


PageGrid.prototype.setHeaderStyle = function(styleName) {
	this._headerStyleName = styleName;
} // setHeaderStyle()


PageGrid.prototype.setColumnStyle = function (whichColumn, styleName) {
	this._columnStyleName[whichColumn] = styleName;
} // setColumnStyle()


PageGrid.prototype.setGridWidth = function(gridWidth) {
	this._gridWidth = gridWidth;
} // setGridWidth()


PageGrid.prototype.setTotal = function(totalColumn, totalValue) {
	this._totalColumn = totalColumn;
	this._totalValue = totalValue;
} // useTotal


PageGrid.prototype.hasTotal = function() {
	return (this._totalColumn != PageGrid.k_NO_TOTAL);
} // hasTotal()


PageGrid.prototype.getHTML = function() {
	var html = "";
	var labelLine = "";
	
	var multiLine = false;
	
	for (var i = 0; i < this._labels.length && multiLine == false; i++) {
		if (this._labels[i]._sub != null && this._labels[i]._sub.length > 0) {
			multiLine = true;
		} // if
	} // for
	
	var subLine = "";
	for (var i = 0; i < this._labels.length; i++) {
		var rowSpan = 1;
		var colSpan = 1;
		if (this._labels[i]._sub != null && this._labels[i]._sub.length > 0) {
			subLine += "</tr><tr>";
			for (var j = 0; j < this._labels[i]._sub.length; j++) {
				subLine += "<td style=\"padding:4px\" class=\""+this._headerStyleName+"\">"+this._labels[i]._sub[j]+"</td>";
			} // for
			colSpan = this._labels[i]._sub.length;
		} // if
		else if (multiLine == true) {
			rowSpan = 2;
		} // else if
		labelLine += "<td style=\"padding:4px\" class=\""+this._headerStyleName+"\" rowspan='"+rowSpan+"' colspan='"+colSpan+"'>"+this._labels[i]._primary+"</td>";
	} // for
	labelLine += subLine;
	
	if (this._title != null) {
		html += this._title;
	} // if

	html += "<table name=\"page0\" id=\"page0\" width=\""+this._gridWidth+"\" cellspacing=\""+this._spacing+"\" cellpadding=\""+this._padding+"\" border=\"0\"><tr>";

	html += labelLine;
	html += "</tr>";
	for (var i = 0; i < this._data.length; i++) {
		var displayClass="clsTableData";
		if (this._data[i]._displayClass != null) {
			displayClass = this._data[i]._displayClass;
		} // if
		html += "<tr>";
		for (var j = 0; j < this._data[i]._fields.length; j++) {
			var columnDisplayClass = displayClass;
			if (this._columnStyleName[j] != null) {
				columnDisplayClass = this._columnStyleName[j];
			} // if
			html += "<td class=\""+columnDisplayClass+"\" valign=\"top\">"+this._data[i]._fields[j]+"</td>";
		} // for
		html += "</tr>";
	} // for
	if (this.hasTotal() == true && this._data.length > 0) {
		html += "<tr><td colspan="+this._data[0]._fields.length+" style='background-color:black; height: 2px; padding:0px; margin: 0px'></td></tr>";
		html += "<tr>";
		for (var i = 0; i < this._data[0]._fields.length; i++) {
			if (i == this._totalColumn-1) {
				html += "<td align='right' class='clsText'><b>Total: </b></td>";
			} // if
			else if (i == this._totalColumn) {
				var columnDisplayClass = "clsTableData";
				if (this._columnStyleName[j] != null) {
					columnDisplayClass = this._columnStyleName[j];
				} // if
				html += "<td align=right class=\""+columnDisplayClass+"\">";
				html += this._totalValue;
				html += "</td>";
			} // if
			else {
				html += "<td></td>";
			} // else
		} // for
		html += "</tr>";
	} // if
	html += "</table>";
	
	return html;
} // getHTML()

	
// Constants
PageGrid.k_NO_TOTAL = -1;