// This function accepts a drop down(select) box to populate and
// the selected value of the logically preceding select to which
// the contents are related, the various arrays are defined elsewhere

function populateDDB (DDB, selected, validationFunction) {
	populateDDBAry(DDB, getSelectedArray(DDB, selected), validationFunction);
}

function getSelectedArray(DDB, selected) {
	if (DDB == null) return null;

	var aryName = DDB.name + selected + "Array";
	var exists = eval("typeof " + aryName);

	if ((document.location.host == "10.21.100.2") && (exists == "undefined") && (selected != '')) {
		window.status = aryName + " not found using " + DDB.name + "Array";
		return getSelectedArray(DDB, '');
	} else {
		return eval(aryName);
	}
}

function populateDDBAry (DDB, selectedArray, validationFunction) {
	if (DDB == null) return;
	if (selectedArray == null) return;

	if ((DDB.type != "select-one") && (DDB.type != "select-multiple")) return;

	var curSel = getDropDownValue(DDB);

	while (0 < DDB.options.length) {
		DDB.options[(DDB.options.length - 1)] = null;
	}

	for (var i = 0, j = 0; i < selectedArray.length; i++) {
		var curItem = eval("new Array "+ selectedArray[i]);
		if ((typeof validationFunction != "function") || (validationFunction(curItem[1]))) {
			eval("DDB.options[j]=" + "new Option" + selectedArray[i]);
			j++;
		}
	}

	setDropDown(DDB, curSel);
}

function setDropDown (DDB, value) {
	if (DDB == null) return;

	if ((DDB.type != "select-one") && (DDB.type != "select-multiple")) return;

	for (var i = 0; i < DDB.options.length; i++) {
		if (DDB.options[i].value == value) {
			DDB.options[i].selected = true;
		} else {
			DDB.options[i].selected = false;
		}
	}
}

function MsetDropDown (DDB, values) {
	if (DDB == null) return;

	if ((DDB.type != "select-one") && (DDB.type != "select-multiple")) return;

	values = '^' + values + '^';

	for (var i = 0; i < DDB.options.length; i++) {
		if (values.indexOf('^' + DDB.options[i].value + '^') > -1) {
			DDB.options[i].selected = true;
		} else {
			DDB.options[i].selected = false;
		}
	}
}


/*
function getDropDownValue(DDB){
	if (DDB == null)
		return null;
	if ("text|hidden" .indexOf(DDB.type))
		return DDB.value;
	var theValue = "";
	if (DDB.selectedIndex !=  - 1){
		theValue = DDB.options[DDB.selectedIndex].value;
	}
	return theValue;
}
*/

function getDropDownValue (DDB)
{
	var returnVal 	= '';
	
	if (DDB == '' || DDB == 'undefined' || DDB == null)
	{
		returnVal = null;
	}
	else if (DDB.type == "text" || DDB.type == "hidden")
	{
		returnVal = DDB.value;
	}
	else
	{
		if (DDB.options)
		{
			var index = DDB.options.selectedIndex;
			if (index != -1)
			{
				returnVal = DDB.options[index].value;
			}
		}
	}
	
	return returnVal;
}

function MgetDropDownValue(DDB){
	if (DDB == null)
		return null;
	if ("text|hidden" .indexOf(DDB.type))
		return DDB.value;
	var theValue = "";
	for (var i = 0; i < DDB.options.length; i++){
		if (DDB.options[i].selected == true){
			theValue += DDB.options[i].value + '^';
		}
	}
	return theValue;
}

function resetDropDown(DDB){
	if (DDB == null)
		return ;
	if ((DDB.type != "select-one") && (DDB.type != "select-multiple"))
		return ;
	DDB.selectedIndex = 0;
	for (i = 0; i < DDB.length; i++){
		if (DDB.options[i].defaultSelected == true){
			DDB.selectedIndex = i;
		}
	}
}

function DDBgetDefault(DDB){
	if (DDB == null)
		return "";
	if ((DDB.type != "select-one") && (DDB.type != "select-multiple"))
		return "";
	for (i = 0; i < DDB.length; i++){
		if (DDB.options[i].defaultSelected == true){
			return DDB.options[i].value;
		}
	}
}

function clearDDB(DDB){
	if (DDB == null)
		return ;
	if ((DDB.type != "select-one") && (DDB.type != "select-multiple"))
		return ;
	while (DDB.options.length > 0)
		DDB.options[0] = null;
}
