﻿
function FilterChild(e, cboTarget, aOptions)
{
	iSelected = e.options[e.selectedIndex].value;
	PopulateTarget(iSelected,cboTarget,aOptions);	
}

function PopulateTarget(iParent,sTarget,aOptions)
{
	var aList = GetArrayWithOptions(iParent,aOptions);	
	if (aList == null)
	{
		var oTargetID = GetObjectName(sTarget);
		var cboTarget = eval('document.forms[0].'+ oTargetID);
		cboTarget.options.length = 0;
	}

	FillCombo(aList,sTarget)		
}
function FillCombo(aList, sTarget)
{
	var sText;
	var sValue;
//	var oTargetID = GetObjectName(sTarget);
//	oTargetID.options.length = 0;
	var cboTarget = eval('document.forms[0].'+ sTarget);
	
	// Clear the list first
	cboTarget.options.length = 0;
	
	cboTarget.options[cboTarget.options.length] = new Option("-Please Select-",0);	
	// Make sure there is an array to work with.
	if (aList != null)
	{
		for(var i=0; i<aList.length;i++)
		{
			sText = aList[i][1];
			sValue = aList[i][0];
			cboTarget.options[cboTarget.options.length] = new Option(sText,sValue);
		}
	}
}
function GetObjectName(name)
{	
	for(var x=0;x< document.forms[0].elements.length;x++)
	{
		var myString = new String(document.forms[0].elements[x].id)		
		if (myString.indexOf(name) != -1)
		{
			//we have found the full name of the control
			return myString.toString();
		}
	}
	return "";
}

function GetArrayWithOptions(iParent, aOptions)
{
	var a = new Array();
	var aSize = 0;
	for (var i = 0;i<aOptions.length;i++)
	{
		// Add items to the arrary that have the same parent ID
		if (aOptions[i][2] == iParent)
		{
			a[aSize] = new Array(aOptions[i][0],aOptions[i][1]);
			aSize++;
		}
	}
	if(a.length >=1)
		return a;
	else
		return null;
}
