/******************************************************************************
MkTable (parent)
	Helper to create a table.
******************************************************************************/

function MkTable (parent)
{
	var	t = document.createElement ('table');
	parent.appendChild (t);

	return t;
}

/******************************************************************************
AddTableRow (t)
	Helper to add a row to a table.
******************************************************************************/

function AddTableRow (t)
{
	return t.insertRow (t.rows.length);
}

/******************************************************************************
MkTblHeading (row, itemText, itemClass, itemWidth, itemAlign)
	Helper to create a table heading.
******************************************************************************/

function MkTblHeading (row, itemText, itemClass, itemWidth, itemAlign)
{
	var	cell = document.createElement ("th");
	row.appendChild (cell);
	SetCellSelAttr (cell, itemText, itemClass, itemWidth, itemAlign);
	return cell;
}

/******************************************************************************
MkTblEntry (row, itemText, itemClass, itemWidth, itemAlign)
	Helper to create a table entry.
******************************************************************************/

function MkTblEntry (row, itemText, itemClass, itemWidth, itemAlign)
{
	var	cell = row.insertCell (row.cells.length);
	SetCellSelAttr (cell, itemText, itemClass, itemWidth, itemAlign);
	return cell;
}

/******************************************************************************
SetCellSelAttr (cell, itemText, itemClass, itemWidth, itemAlign)
	Helper to set certain attributes for a cell.
******************************************************************************/

function SetCellSelAttr (cell, itemText, itemClass, itemWidth, itemAlign)
{
	if (itemClass)
		SetClass (cell, itemClass);

	if (itemWidth)
		cell.setAttribute ("width", itemWidth);

	if (itemAlign)
		cell.setAttribute ("align", itemAlign);

	if (itemText != null && itemText != undefined)
		cell.innerHTML = itemText;

	return cell;
}

/******************************************************************************
GetCellElementTable (cellel)
	Helper to return a refrence to a cell.
******************************************************************************/

function GetCellElementTable (cellel)
{
	// Get the table containing the cell element.
	//	The parent of the element is the cell (the <td>).
	//	The parent of the cell is the row (the <tr>).
	//	The parent of the row is the table section.
	//	The parent of the table section is, finally, the table.

	return cellel.parentNode.parentNode.parentNode.parentNode;
}

/******************************************************************************
GetCellItem (t, nRow, nCol)
	Helper to return a refrence to a cell.
******************************************************************************/

function GetCellItem (t, nRow, nCol)
{
	return t.rows [nRow].cells [nCol];
}

/******************************************************************************
GetEntryItem (t, nRow, nCol)
	Helper to return a refrence to the entry item for a cell.
******************************************************************************/

function GetEntryItem (t, nRow, nCol)
{
	return GetCellItem (t, nRow, nCol).firstChild;
}

/******************************************************************************
GetEntryValue (t, i, j)
	Helper to return the value in a cell.
******************************************************************************/

function GetEntryValue (t, i, j)
{
	return GetEntryItem (t, i, j).value;
}

/******************************************************************************
SetEntryValue (t, i, j,  value)
	Helper to set the value in a cell.
******************************************************************************/

function SetEntryValue (t, i, j, value)
{
	GetEntryItem (t, i, j).value = value;
}

/******************************************************************************
SetEntryIDValue (t, i, j, id, value)
	Helper to set the value in a cell.
******************************************************************************/

function SetEntryIDValue (t, i, j, id, value)
{
	GetEntryItem (t, i, j).id = id;
	GetEntryItem (t, i, j).value = value;
}

/******************************************************************************
GetEntryText (t, i, j)
	Helper to get the text in a cell.
******************************************************************************/

function GetEntryText (t, i, j)
{
	return GetEntryItem (t, i, j).data;
}

/******************************************************************************
SetEntryText (t, i, j, text)
	Helper to set the text in a cell.
******************************************************************************/

function SetEntryText (t, i, j, text)
{
	GetEntryItem (t, i, j).data = text;
}

/******************************************************************************
GetEntryAttr (t, i, j, attr)
	Helper to return the value of the requested attribute for an entry.
******************************************************************************/

function GetCellAttr (t, i, j, attr)
{
	return GetEntryItem (t, i, j).getAttribute (attr);
}

/******************************************************************************
SetEntryAttr (t, i, j, attr, value)
	Helper to set the value of the requested attribute for a entry.
******************************************************************************/

function SetEntryAttr (t, i, j, attr, value)
{
	GetEntryItem (t, i, j).setAttribute (attr, value);
}

/******************************************************************************
GetCellAttr (t, i, j, attr)
	Helper to return the value of the requested attribute for a cell.
******************************************************************************/

function GetCellAttr (t, i, j, attr)
{
	return GetCellItem (t, i, j).getAttribute (attr);
}

/******************************************************************************
SetCellAttr (t, i, j, attr, value)
	Helper to set the value of the requested attribute for a cell.
******************************************************************************/

function SetCellAttr (t, i, j, attr, value)
{
	GetCellItem (t, i, j).setAttribute (attr, value);
}

/******************************************************************************
SetClass (elem, className)
	Helper to set the class of an element. Guess who gets this wrong.
	With thanks to http://alistapart.com/articles/jslogging (See end of article)
******************************************************************************/

function SetClass (elem, className)
{
	if (typeof (elem) == "string")
		elem = document.getElementById (elem);

	if (elem) {

		// if the node's class already exists
		// then replace its value
		if (elem.getAttributeNode ("class")) {
			for (var i = 0; i <elem.attributes.length; i++) {
				var attrName = elem.attributes[i].name.toUpperCase();
				if (attrName == 'CLASS') {
					elem.attributes[i].value = className;
				}
			}
		// otherwise create a new attribute
		} else {
			elem.setAttribute ("class", className);
		}
	}
}

/******************************************************************************
MkImageEntry (cell, src, alt, title, func)
	Helper to create an image.
******************************************************************************/

function MkImageEntry (cell, src, alt, func, i)
{
	var img = document.createElement ('img');
	img.setAttribute ('src', src);
	img.setAttribute ('alt', alt);
	img.setAttribute ('title', alt);
	
	if (func != null && func != undefined)
		img.onclick = func;
	
	if (i != null && i != undefined)
		img.setAttribute ('id', i);
	
	cell.appendChild (img);
	
	return img;
}

function mkPara (prnt, txt, clss)
{
	var para = document.createElement ('p');
	if (clss) handleClass ('add', para, clss);
	para.appendChild (document.createTextNode (txt));
	prnt.appendChild (para);
}

/******************************************************************************
AddRadioButton (parent, elname, elid, dbid, value, bSelected, title, 
				szClass, szPrompt, conttype, OnClick)
	Helper to add a radio button.
******************************************************************************/

function AddRadioButton (parent, elname, elid, dbid, value, bSelected, title, 
							szClass, szPrompt, conttype, OnClick)
{
	// Set up the definition as html so IE gets it right.

	var	htmldef = '<input type="radio"' + ' name="' + elname + '"' + 
					' value="' + value + '"';
	if (elid)
		htmldef += ' id="' + elid + '"';

	if (szClass)
		htmldef += ' class="' + szClass + '"';

	if (title)
		htmldef += ' title="' + title + '"';

	if (bSelected)
		htmldef += ' checked';

	htmldef += '/>';

	if (szPrompt)
		htmldef += ' ' + szPrompt;

	if (!conttype)
		conttype = "div";

	// Create the container, append to the parent and stuff the html in.

	var	but = document.createElement (conttype);

	parent.appendChild (but);

	but.innerHTML = htmldef;

	// Set the value on the container so it can be retieved via "this.value".

	but.value = value;

	// Add attributes.

	if (dbid)
		but.dbid = dbid;

	if (OnClick)
		but.onclick = OnClick;

	return but;
}

function genOptions(sel, text, val)
{
	var sel = document.getElementById(sel);
	sel.options[sel.options.length] = new Option(text, val);
}