// listutilities.js

/******************************************************************************
AddList (parent, nameid, OnChg)
	Helper to add an empty list.
******************************************************************************/

function AddList (parent, nameid, OnChg)
{
	var	s = document.createElement ("select");
	s.name = s.id = nameid;
	s.onchange = OnChg;
	parent.appendChild (s);
	return s;
}

/******************************************************************************
AddListItem (List, ID, Value, Desc, IsSelected)
	Add an item to a list.
******************************************************************************/

function AddListItem (List, ID, Value, Desc, IsSelected)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	var	n = List.length;
	var	opt = new Option (Value, ID, false, false);
	opt.title = Desc;
	opt.selected = IsSelected;
	List.options [n] = opt;
	if (IsSelected)
		List.selectedIndex = n;

	return opt;
}

/******************************************************************************
InsertListItemByText (List, ID, Value, Desc, IsSelected, nStart)
	Insert an item to a list by value.
******************************************************************************/

function InsertListItemByText (List, ID, Value, Desc, IsSelected, nStart)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	var	opt = new Option (Value, ID, false, false);
	opt.title = Desc;
	opt.selected = IsSelected;

	if (!nStart)
		nStart = 0;

	var	n;

	for (n = nStart; n < List.length && Value >= List.options [n].text; n++);

	List.options [n] = opt;
	if (IsSelected)
		List.selectedIndex = n;

	return opt;
}

/******************************************************************************
FindListItem (List, ID, Value)
	Find an item in a list.
******************************************************************************/

function FindListItem (List, ID, Value)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	var	nItem = -1;

	if (ID != null && ID != undefined) {
		for (var i = 0; i < List.length; i++) {
			if (List.options [i].value == ID) {
				nItem = i;
				break;
			}
		}
	} else if (Value != null && Value != undefined) {
		for (var i = 0; i < List.length; i++) {
			if (List.options [i].text == Value) {
				nItem = i;
				break;
			}
		}
	}

	return nItem;
}

/******************************************************************************
FindListItemByAttr (List, attr, Value)
	Find an item in a list.
******************************************************************************/

function FindListItemByAttr (List, attr, Value)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	var	nItem = -1;

	if (Value != null && Value != undefined) {
		for (var i = 0; i < List.length; i++) {
			if (List.options [i][attr] == Value) {
				nItem = i;
				break;
			}
		}
	}

	return nItem;
}

/******************************************************************************
GetListItemID (List, im)
	Helper to return the ID of a list item.
******************************************************************************/

function GetListItemID (List, im)
{
	var	ID = false;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		ID = List.options [im].value;

	return ID;
}

/******************************************************************************
GetListItemText (List, im)
	Helper to return the text of a list item.
******************************************************************************/

function GetListItemText (List, im)
{
	var	text = false;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		text = List.options [im].text;

	return text;
}

/******************************************************************************
GetListItemAttr (List, im, attr)
	Helper to set an attribute of a list item.
******************************************************************************/

function GetListItemAttr (List, im, attr)
{
	var	value = false;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		value = List.options [im][attr];

	return value;
}

/******************************************************************************
isListItemSelected (List, im)
	Helper to return the selection state of a list item.
******************************************************************************/

function isListItemSelected (List, im)
{
	var	bSelected = false;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		bSelected = List.options [im].selected;

	return bSelected;
}

/******************************************************************************
SetListItemID (List, im, ID)
	Helper to set the ID of a list item.
******************************************************************************/

function SetListItemID (List, im, ID)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		List.options [im].value = ID;
}

/******************************************************************************
SetListItemText (List, im, text)
	Helper to set the text of a list item.
******************************************************************************/

function SetListItemText (List, im, text)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		List.options [im].text = text;
}

/******************************************************************************
SetListItemAttr (List, im, attr, value)
	Helper to set an attribute of a list item.
******************************************************************************/

function SetListItemAttr (List, im, attr, value)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		List.options [im][attr] = value;
}

/******************************************************************************
selectListItem (List, im)
	Helper to select an item in a list.
******************************************************************************/

function selectListItem (List, im)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length) {
		List.selectedIndex = im;
		List.options [im].selected = true;
	}
}

/******************************************************************************
DeleteListItem (List, im)
	Helper to delete a list item.
******************************************************************************/

function DeleteListItem (List, im)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (im >= 0 && im < List.options.length)
		List.options [im] = null;
}

/******************************************************************************
ClearList (List)
	Helper to clear all entries in a list.
******************************************************************************/

function ClearList (List)
{
	if (typeof (List) == "string")
		List = document.getElementById (List);

	List.length = 0;
	List.selectedIndex = -1;
}

/******************************************************************************
GetSelOpt (List)
	Helper to return the selected entry in a list.
******************************************************************************/

function GetSelOpt (List)
{
	var opt = null;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	if (List.length > 0 && List.selectedIndex >= 0)
		opt = List.options [List.selectedIndex];

	return opt;
}

/******************************************************************************
GetSelID (List)
	Helper to return the value of the selected entry in a list.
******************************************************************************/

function GetSelID (List)
{
	var	opt = GetSelOpt (List);

	return opt ? opt.value : -1;
}

/******************************************************************************
GetSelText (List)
	Helper to return the text of the selected entry in a list.
******************************************************************************/

function GetSelText (List)
{
	var	opt = GetSelOpt (List);

	return opt ? opt.text : "";
}

/******************************************************************************
GetSelAttribute (List, attr)
	Helper to return an attribute of the selected entry in a list.
******************************************************************************/

function GetSelAttribute (List, attr)
{
	var	opt = GetSelOpt (List);

	return opt ? opt [attr] : "";
}

/******************************************************************************
SetSelID (List, ID)
	Helper to set selected entry in a list.
******************************************************************************/

function SetSelID (List, ID)
{
	var	nSelect = -1;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	for (var i = 0; nSelect < 0 && i < List.length; i++) {
		if (List.options [i].value == ID)
			nSelect = i;
	}

	List.selectedIndex = nSelect;

	if (nSelect >= 0 && nSelect < List.length)
		List.options [nSelect].selected = true;
}

/******************************************************************************
SetSelText (List, szText)
	Helper to set selected entry in a list.
******************************************************************************/

function SetSelText (List, szText)
{
	var	nSelect = -1;

	if (typeof (List) == "string")
		List = document.getElementById (List);

	for (var i = 0; nSelect < 0 && i < List.length; i++) {
		if (List.options [i].text == szText)
			nSelect = i;
	}

	List.selectedIndex = nSelect;

	if (nSelect >= 0 && nSelect < List.length)
		List.options [nSelect].selected = true;
}

