// 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, baseName) {
	populateDDBAry(DDB, getSelectedArray(DDB, selected, baseName), validationFunction);
}

function getSelectedArray(DDB, selected, baseName) {
	if (DDB == null) return null;
  if (baseName == null) baseName = DDB.name;

	var aryName = baseName + selected + "Array";
	var exists = eval("typeof " + aryName);

	if ((document.location.host.indexOf("10.21.100.2") != -1) && (exists == "undefined") && (selected != '')) {
		window.status = aryName + " not found using " + baseName + "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 findOptionWithText(DDB, textValue) {
  if (DDB == null) return null;	
  if ((DDB.type != "select-one") && (DDB.type != "select-multiple")) return;

	for (var i = 0; i < DDB.options.length; i++) {
		if (DDB.options[i].text == textValue) {
			return DDB.options[i];
		}
	}
}

function findOptionWithValue(DDB, value) {
  if (DDB == null) return null;	
  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) {
			return DDB.options[i];
		}
	}
}
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)>=0)
		return DDB.value;
	var theValue = "";
	if (DDB.selectedIndex !=  - 1){
		theValue = DDB.options[DDB.selectedIndex].value;
	}
	return theValue;
}


function getDropDownText(DDB){
	if (DDB == null)
		return null;
	if ("text|hidden" .indexOf(DDB.type)>=0)
		return DDB.value;
	var theValue = "";
	if (DDB.selectedIndex !=  - 1){
		theValue = DDB.options[DDB.selectedIndex].text;
	}
	return theValue;
}


function getSelected(DDB){
	if (DDB == null)
		return null;
	if ("text|hidden" .indexOf(DDB.type)>=0)
		return DDB.value;
	var theValue = "";
	if (DDB.selectedIndex !=  - 1){
		theValue = DDB.options[DDB.selectedIndex];
	}
	return theValue;
}

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;
}


function sortDropDown(DDB) {
	if (DDB == null) return ;
	if ((DDB.type != "select-one") && (DDB.type != "select-multiple"))return ;
  
  var curValue = MgetDropDownValue(DDB);
  var sorted = new Array();
  for (var i = 0; i < DDB.options.length; i++) {
    var insertPt = sorted.length;
    for (var j = 0; j < sorted.length; j++) {
      if (DDB.options[i].text < sorted[j].text) {
        insertPt = j;
        break;
      }
    }
    var opt = new Object();
    opt.value = DDB.options[i].value;
    opt.text = DDB.options[i].text;
    sorted.splice(insertPt, 0, opt);    
  }
  for (var j = 0; j < sorted.length; j++) {
    DDB.options[j] = new Option(sorted[j].text, sorted[j].value);  
  }  
  MsetDropDown(DDB, curValue);
}


