// page_GasCatalog.js - javascript for the gas catalog display pages
//
// Actions:

function GasCatalogViewPageObject ()
{}

GasCatalogViewPageObject.prototype.vars =
{
	timerID: null,
	timerRunning: false
};

GasCatalogViewPageObject.prototype.view = function(jsonText, displayQOH)
{
//	alert('In GasCatalogViewPageObject.view().  diasplayQOH:'+displayQOH);
	var jsonT = jsonText.parseJSON();
//	alert("MR success");

//	The table is created in the base document with a single
//	header row.  Save this row, clear all other rows and add
//	this row back.

	var catalogTable = document.getElementById("vc_gasCatalog").getElementsByTagName("TBODY").item(0);
	var header = catalogTable.firstChild.cloneNode(true);	// Clone the header row, deeply

	mr.removeAllChildren(catalogTable);
	catalogTable.appendChild(header);
	

	document.getElementById("vc_gasCatalog_QOH").style.visibility = (displayQOH) ? 'visible' : 'hidden';

//	Loop through each product in the list.  Each will generate one or
//	more <tr> elements to add to the catalog table.

	var alternate = 0;
	for (var prod in jsonT)
	{
		if (typeof jsonT[prod] == "function")
			continue;

		this.addProduct(catalogTable, jsonT[prod], alternate, displayQOH);
		alternate = (alternate) ? 0 : 1;
	}
};

GasCatalogViewPageObject.prototype.addProduct = function(catalogTable, product, alternate, displayQOH)
{
//	A product consists of a description and an array of parts.  The
//	description is to span the rows of the several parts.  Thus the
//	row for the first part consists of the description followed by
//	the first part, while subsequent rows omit the product description.
//	This is effected by the use of rowspan.

//	alert('prod:'+product);

	var parts = product['Parts'];
//	alert('Desc:'+product['Description']+'  MSDS:'+product['MSDSNum']+ '  PartCt:'+parts.length);

//	Create a new row object.  Append the Description/MSDS as a cell element.
//	Here we have three elements in the cell; {Description}<br /><a href={MSDS}>MSDS {MSDS}</a>

	var row = document.createElement("tr");
	if (alternate) row.className = "alternateRow";
	var cell = document.createElement("td");
	cell.setAttribute("rowSpan", parts.length);
	cell.appendChild(document.createTextNode(product['Description']));
	cell.appendChild(document.createElement("br"));

	var msds = product['MSDSNum'];
	if (msds > '')
	{
		var link = document.createElement("a");
		link.setAttribute("href", "docs/MSDS/MSDS"+msds+".pdf");
		link.setAttribute("title", "display MSDS - requires PDF reader");
		link.appendChild(document.createTextNode('MSDS '+msds));
		cell.appendChild(link);
	}
	else
	{
		cell.appendChild(document.createTextNode('MSDS not available'));
	}

	row.appendChild(cell);

	for (var p = 0; p < parts.length; ++p)
	{
		if (typeof parts[p] == "function")
			continue;

//		If this not the first row, append the current row into the
//		table, and create a new one for adding the part detail.  This
//		has the effect of keeping the first part with the product detail.

		if (p > 0)
		{
			catalogTable.appendChild(row);
			row = document.createElement("tr");
			if (alternate) row.className = "alternateRow";
		}

		var part = parts[p];
//		alert('Cyl:'+part['Cylinder']+'  Part:'+part['PartNum']+'  QOH:'+part['QOH']);

		cell = document.createElement("td");
		cell.appendChild(document.createTextNode(part['Cylinder']));
		row.appendChild(cell);

		cell = document.createElement("td");
		cell.appendChild(document.createTextNode(part['PartNum']));
		row.appendChild(cell);

		if (displayQOH)
		{
			cell = document.createElement("td");
			cell.appendChild(document.createTextNode(part['QOH']));
			cell.setAttribute("align", "center");
			row.appendChild(cell);
		}
	}

//	Append the current (and final) row into the table.
	catalogTable.appendChild(row);
}

var GasCatalogView = new GasCatalogViewPageObject;
