// iPosition is zero-based, like an array.
// example: 
//		iHaystack = "my-delimited-string"
//		iPosition = 1
//		iDelimiter = "-"
// returns: "delimited"
function GetDelimField( iHaystack, iPosition, iDelimiter )
{
	if( typeof(iHaystack) == "string" && iHaystack.length > 0 )
	{
		var fields = iHaystack.split(iDelimiter);
		if( fields.length > 0 )
			return fields[iPosition];
	}
	
	return "";
}


function ToggleTreeIcon()
{
	this.innerHTML = (this.innerHTML == "+")?"X":"+";
}


function ToggleWorkstationDisplay( iWorkStationDivId )
{
	var div = document.getElementById(iWorkStationDivId);
	if( div && div.tagName.toLowerCase() == "div" )
	{
		if( div.style.display == "none" || div.style.display == "" )
		{
			div.style.display = "block";
		}
		else
			div.style.display = "none";
	}
}


function ToggleWorkStationBoxes()
{
	var groupId = GetDelimField(this.id, 1, "-");
	var boxes = GetWorkStations(groupId);
	for( var x = 0; x < boxes.length; x++ )
		boxes[x].checked = this.checked;
}


function ToggleWorkGroupBox()
{
	var checked = true;	// default
	var workgroupId = GetDelimField(this.id, 0, "_");
	var wgBox = document.getElementById(workgroupId);
	var wsBoxes = GetWorkStations(GetDelimField(workgroupId, 1, "-"));
	for( var x = 0; x < wsBoxes.length; x++ )
	{
		if( !wsBoxes[x].checked )
		{
			checked = false;
			break;
		}
	}
	wgBox.checked = checked;
}


function GetWorkGroup( iWorkGroupId )
{
	return document.getElementById("wg-" + iWorkGroupId);
}


function GetWorkStations( iWorkGroupId )
{
	var workstations = new Array();
	var wg = GetWorkGroup(iWorkGroupId);
	if( wg && wg.type && wg.type == "checkbox" )
	{
		var wsDiv = document.getElementById("ws-" + iWorkGroupId);
		var nodes = wsDiv.getElementsByTagName("input");
		for( var x = 0; x < nodes.length; x++ )
		{
			if( nodes[x].name == "workstation" )
				workstations[workstations.length] = nodes[x];
		}
	}
	
	return workstations;
}
