var TEXT = 0
var VALUE = 1

/**************************************************
* This function is called when a form is loaded. This function should be called by the 'onload' of the 
* body tag in HTML
*
* Parameter :
* - parentSelect ==> parent selection box object.
* - parentContent ==> an Array used to load the parent selection box object.
* - defaultParent ==> default selected item of the parent selection box.
* - childSelect ==> child selection box object.
* - childContent ==> an Array used to load the child selection box object.
* - defaultChild ==> default selected item of the child selection box.
* - pleaseSelectMsg ==> the message to be displayed if no item is selected.
*
* Return :
*
**************************************************/
function loadParent(parentSelect, parentContent, defaultParent, 
	childSelect, childContent, defaultChild, pleaseSelectMsg) {
	loadParent(parentSelect, parentContent, defaultParent, pleaseSelectMsg,
		childSelect, childContent, defaultChild, pleaseSelectMsg);
}

function loadParent(parentSelect, parentContent, defaultParent, pleaseSelectParentMsg,
	childSelect, childContent, defaultChild, pleaseSelectChildMsg)
{
	var optArr = new Array()

	parentSelect.options[0] = new Option(pleaseSelectParentMsg, "-1")

	for(var i=0; i<parentContent.length; i++)
	{
		parentSelect.options[i+1] = 
			new Option(parentContent[i][TEXT], parentContent[i][VALUE])
	}
	
	parentSelect.options.length = parentContent.length + 1
	
	if(defaultParent >= 0)
	{
		parentSelect.selectedIndex = defaultParent + 1
		loadChild(parentSelect, childSelect, childContent, pleaseSelectChildMsg)
		if(defaultChild >= 0)
			childSelect.selectedIndex = defaultChild + 1

		return
	}
	parentSelect.selectedIndex = 0
	childSelect.options[0] = new Option(pleaseSelectChildMsg, "-1")
	
	childSelect.options.length = 1
	childSelect.selectedIndex = 0
}



/**************************************************
* This function is called by the parent selection box object. When an item of the parent selection box is selected,
* the child selection box content will be refreshed.
*
* Parameter :
* - parentSelect ==> parent selection box object.
* - childSelect ==> child selection box object.
* - childContent ==> an Array used to load the child selection box object.
* - pleaseSelectMsg ==> the message to be displayed if no item is selected.
*
* Return :
*
**************************************************/
function loadChild(parentSelect, childSelect, childContent, pleaseSelectMsg)
{
	var selectedID = parentSelect.selectedIndex;
	
	// -1 is minus the parent Select box has an extra element that will not be counted.
	// The first element in the parent select box is a "Please select ..." message
	// which is not counted. Due to that, in order to match the Select box with the
	// array element, each element index should be -1.
	var selectedContent = childContent[selectedID-1];

	childSelect.options[0] = new Option(pleaseSelectMsg, "-1", true, false);
	if(selectedID == 0)
	{
		childSelect.options.length = 1;
		return;
	}
	
	for(var i=0; i<selectedContent.length; i++)
	{
		childSelect.options[i+1] = 
			new Option(selectedContent[i][TEXT], selectedContent[i][VALUE]);
	}
	
	childSelect.options.length = selectedContent.length + 1;
}
function loadSubChild(parentSelect, childSelect, childContent, pleaseSelectMsg)
{
	var selectedID = parentSelect.selectedIndex
	childSelect.options[0] = new Option(pleaseSelectMsg, "-1", true, false)
	
	for(var i=0; i<childContent.length; i++)
	{
		childSelect.options[i+1] = 
			new Option(childContent[i][TEXT], childContent[i][VALUE])
	}
	childSelect.options.length = childContent.length + 1
}

