// 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) {
  var selectedArray = eval(DDB.name + selected + "Array");
   populateDDBAry(DDB, selectedArray);
}

function populateDDBAry(DDB,selectedArray) {  
	var curVal = getDropDownValue(DDB);
  while (selectedArray.length < DDB.options.length) {
    DDB.options[(DDB.options.length - 1)] = null;
  }
  for (var i=0; i < selectedArray.length; i++) {
    eval("DDB.options[i]=" + "new Option" + selectedArray[i]);
  }
	setDropDown(DDB, curVal);
}

function setDropDown(DDB, value) {

  if (DDB.selectedIndex == 0) {

	  DDB.selectedIndex = 0;
	  for (var i = 0; i < DDB.length; i++) {
	    if (DDB.options[i].value == value) {
	      DDB.selectedIndex = i;
	      break;
	    }
	  }
  }

}

function MsetDropDown(DDB, values) {
  values = '^'+values+'^';
  for (var i = 0; i < DDB.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 "";
  var theValue = "";
  if (DDB.selectedIndex != -1) {
    theValue = DDB.options[DDB.selectedIndex].value;  
  }
  return theValue;
}

function MgetDropDownValue (DDB) {
  var theValue = "";
  for (var i=0; i < DDB.length; i++){
    if (DDB.options[i].selected == true) {
      theValue += DDB.options[i].value + '^';
    }
  }
  return theValue;
}

function resetDropDown (DDB) {
  DDB.selectedIndex = 0;
  for (i = 0; i < DDB.length; i++) {
    if (DDB.options[i].defaultSelected == true) {
      DDB.selectedIndex = i;
    }
  }
}

function DDBgetDefault (DDB) {
  for (i = 0; i < DDB.length; i++) {
    if (DDB.options[i].defaultSelected == true) {
      return DDB.options[i].value;
    }
  }
}
