// Populate Dependent List Box
//
// Common function for populating a dependent list box.  The arrays that are referenced should have
// been created using WDDX functions.
//
// Author:  Brian Bailey (The Seva Group)
// Date:    September 21, 1999
//
// Revisions:
//
//		Author			Date		Description of Changes
//		===================================================
//		B.Bailey		09/21/1999	Original creation.
//		BBailey			10/24/2001	Added blnSelectAll
//
// Input parameters:
//		ParentValue       - The value from the parent list box that will impact what values get set
//                          for the dependent list box
//      ChildArray		  - Array containing values and text that should be used to populate list box
//      MatchColName	  - Column in array whose values should be matched to ParentValue.
//      ValueColName      - Column in array that contains values that should be put in list box
//      TextColName       - Column in array that contains text that should appear in list box
//      objListBox        - List box that is to be populated.
//      blnPadFront       - Flag indicating whether a row should be inserted at beginning of list box
//                          prior to any data retrieved from arrays
//      numPadValue       - Value that padding entry should have
//      strPadText        - Text that padding entry should have
//		numToSelect		  - Value in target list box that should be selected (in case you want to pre-populate)
//		blnSelectCol	  - Flag indicating whether the SelectColName and SelectColValue fields should be evaluated or not.  Allows for selection of multiple items in the list box but has to be user determined in advance.
//      SelectColName     - Name of the column (if any) that provides the flag/indicator as to whether a given row should be selected or not.
//      SelectColValue    - Value in the SelectColName that should be looked for in order to make a value become selected.
//		blnSelectAll	  - Override flag that forces synchronization to select all values whether they match the ParentValue or not.
//
function DependentListBox(ParentValue, ChildArray, MatchColName, ValueColName, TextColName, objListBox, blnPadFront, numPadValue, strPadText, numToSelect, blnSelectCol, SelectColName, SelectColValue, blnSelectAll) {

	aryColNames = new Array();
	qtyChildRows = ChildArray.getRowCount();
	idxMatchCol = 0;
	idxValueCol = 0;
	idxTextCol = 0;
	idxSelectCol = 0;
	i = 0;
	idxOption = 0;
	objListBox.options.length = 0;

	if (blnPadFront) {
		objListBox.options.length += 1
		objListBox.options[idxOption].value = numPadValue
		objListBox.options[idxOption].text = strPadText;	
		idxOption += 1
	}
	
	// Find matching column, where value is located, and where text is located in array	
    for (col in ChildArray)    
	{
		if (typeof(ChildArray[col]) == "object")  {	
		
			aryColNames[i++] = col.toUpperCase()
			if (MatchColName.toUpperCase() == col.toUpperCase()) {idxMatchCol = i}
			if (ValueColName.toUpperCase() == col.toUpperCase()) {idxValueCol = i}
			if (TextColName.toUpperCase()  == col.toUpperCase()) {idxTextCol = i}			
			if (blnSelectCol) {
				if (SelectColName.toUpperCase()  == col.toUpperCase()) {idxSelectCol = i}			
			}
		}    
	}

	// Check to make sure that matches found on column names
	if (idxMatchCol == 0) {return false}
	if (idxValueCol == 0) {return false}
	if (idxTextCol == 0)  {return false}

	flSelected = "N";
	
  	for (row = 0; row < qtyChildRows; ++row)
	{
		if (ChildArray.getField(row, aryColNames[idxMatchCol - 1]) == ParentValue || (blnSelectAll == true)) {
			tempvalue = ChildArray.getField(row, aryColNames[idxValueCol - 1])
			temptext  = ChildArray.getField(row, aryColNames[idxTextCol - 1])
			objListBox.options.length += 1
			objListBox.options[idxOption].value = tempvalue
			objListBox.options[idxOption].text = temptext
			idxOption = idxOption + 1
			
			if (numToSelect != 0 && tempvalue == numToSelect && flSelected == "N") {
				objListBox.selectedIndex = idxOption - 1
				flSelected = "Y"
			}

			if (blnSelectCol) {
				tempSelect  = ChildArray.getField(row, aryColNames[idxSelectCol - 1])			
				if (tempSelect == SelectColValue) {
					objListBox.options[idxOption - 1].selected = true
					flSelected = "Y"
				}
			}
		}
	}

	if (objListBox.options.length == 0) {EmptyListBox(objListBox)}

	if (flSelected == "N") {objListBox.selectedIndex = 0}
		
	return true;
}

function EmptyListBox(objListBox) {
	objListBox.options.length = 0;
	objListBox.options.length += 1;	 	 
	objListBox.options[0].value = 0;
	objListBox.options[0].text = "No Values";	
	objListBox.selectedIndex = 0;
		
	return true;
}
