// Title: Dependent Dropdowns
// Description: 
// Author: Stephen Fensom

// populates a child dd from the selected value in the parent. Also calls any onChange for the child
function dcmnr_changeDependentDD(formId, parentSelectId, childSelectId, dependentListArray, valueListArray, optionalSelectedValueId) {
  //alert('change: ' + formId + ' ' + parentSelectId + ' ' + childSelectId + ' ' + dependentListArray + ' ' + valueListArray + ' ' + optionalSelectedValueId);
  var formObj = document.getElementById(formId);
  var parentSelect = formObj[parentSelectId];
  var childSelect = formObj[childSelectId];
  // find which value is selected
  var selectedValue = optionalSelectedValueId==''?parentSelect.options[parentSelect.selectedIndex].value:optionalSelectedValueId;
  // find the value in the dependent list (index 0)
  //alert('Selected id='+selectedValue);
  // remove any current options
  if (childSelect.options) {
   childSelect.options.length = 0;
   if (childSelect.getAttribute('mandatory')!='true') {
     childSelect.options[childSelect.options.length] = new Option('','');
   }
  }
   
  dcmnr_privatePopulateChildDD(selectedValue, dependentListArray, valueListArray, childSelect);
  //if (childSelect.parentChangeFiresChildAction && childSelect.onchange) {
  if (childSelect.getAttribute('parentChangeFiresChildAction') && childSelect.onchange) {
    childSelect.onchange();
  }
}

// populates a child dd from the selected value in the Auto Complete parent
function dcmnr_changeACDependentDD(formId, parentACId, childSelectId, dependentListArray, valueListArray, optionalSelectedValueId) {
  //alert('dcmnr_changeACDependentDD');
  //alert('change: ' + formId + ' ' + parentSelectId + ' ' + childSelectId + ' ' + dependentListArray + ' ' + valueListArray + ' ' + optionalSelectedValueId);
  var formObj = document.getElementById(formId);
  var parentSelect = formObj[parentACId];
  var childSelect = formObj[childSelectId];
  // find which value is selected
  var selectedValue = optionalSelectedValueId==''?parentSelect.value:optionalSelectedValueId;
  //alert(selectedValue);
  // find the value in the dependent list (index 0)
  //alert('Selected id='+selectedValue);
  // remove any current options
  if (childSelect.options) {
   childSelect.options.length = 0;
   if (childSelect.getAttribute('mandatory')!='true') {
     childSelect.options[childSelect.options.length] = new Option('','');
   }
  }
   
  dcmnr_privatePopulateChildDD(selectedValue, dependentListArray, valueListArray, childSelect); 
}

// Populates a child drop down using the given parents chosen element id.
//
// This is the public function. It handles locating the childSelect using the given id.
//
// Author: Tim McAuley
// Date: 24/08/2005
function dcmnr_populateChildDD(formId, selectedValue, childSelectId, dependentListArray, valueListArray) {
  var formObj = document.getElementById(formId);
  var childSelect = formObj[childSelectId];
  dcmnr_privatePopulateChildDD(selectedValue, dependentListArray, valueListArray, childSelect);
}

// Populates a child drop down using the given parents chosen element id.
//
// Author: Tim McAuley
// Date: 24/08/2005
function dcmnr_privatePopulateChildDD(selectedValue, dependentListArray, valueListArray, childSelect) {

  for (var i = 0; i < dependentListArray.length; i++) {
    //alert(dependentListArray[i][0] + ' == ' + selectedValue);
    if (dependentListArray[i][0] == selectedValue) {
      for (var ii = 1; ii < dependentListArray[i].length; ii++) {
        var valueId = dependentListArray[i][ii];
        dcmnr_privateDependentValueOption(valueId, valueListArray, childSelect);
      }
      break;
    }
  }
}

// appends a new option representing the value to the DD.
// If the value is not found,  then attempts to append all belonging to the group.
function dcmnr_privateDependentValueOption(valueId, valueListArray, dd) {
  //alert('dcmnr_privateDependentValueOption');
  var newOpt = null;
  for (var i = 0; i < valueListArray.length; i++) {
    if (valueListArray[i][0] == valueId) {
      newOpt = new Option(valueListArray[i][1], valueListArray[i][0]);
      break;
    }
  }
  if (newOpt != null) {
    dd.options[dd.options.length] = newOpt;
  } else {
      // try to match the value as the group id, adding all in the group.
      // while this could have also been done as part of the above loop,
      // we specifically only want to do this should the value not match.
    for (var i = 0; i < valueListArray.length; i++) {
      if (valueListArray[i][2] == valueId) {
        dd.options[dd.options.length] = new Option(valueListArray[i][1], valueListArray[i][0]);
      }
    }
  }
}

// return the label for the valueId specified
function dcmnr_privateDependentValueLookup(valueId, valueListArray) {
  for (var i = 0; i < valueListArray.length; i++) {
    if (valueListArray[i][0] == valueId) {
      return valueListArray[i][1];
    }
  }
  return null;
}

// loads the values in the parent DD only - at page load, optionally selecting a value
function dcmnr_loadDependentParent(formId, parentId, dependentListArray, valueListArray, optionalSelectedValueId) {
  //alert('load: ' + formId + ' ' + parentId + ' ' + dependentListArray + ' ' + valueListArray + ' ' + optionalSelectedValueId);
  var formObj = document.getElementById(formId);
  var parentSelect = formObj[parentId];
  // populate the values from the dependent list (index 0)
  //alert(formObj + ' ' + parentSelect + ' ' + dependentListArray.length);
  //alert(parentSelect.options.length + ' ' + parentSelect.mandatory);
  if (parentSelect.getAttribute('mandatory')=='true') parentSelect.options.length = 0;
  for (var i = 0; i < dependentListArray.length; i++) {
    var valueId = dependentListArray[i][0];
    var newOpt = new Option(dcmnr_privateDependentValueLookup(valueId, valueListArray), valueId);
    if (optionalSelectedValueId == valueId) {
      newOpt.selected=true;
    }
    parentSelect.options[parentSelect.options.length] = newOpt;
  }
} 

// selects the specified value in the child dd
function dcmnr_selectDependentDD(formId, ddId, selectedValue) {
  //alert('select: ' + formId + ' ' + ddId + ' ' + selectedValue);
  var formObj = document.getElementById(formId);
  var dd = formObj[ddId];
  for (var i = 0; i < dd.options.length; i++) {
    var opt = dd.options[i];
    //alert('Comparing:'+opt.value+' to '+selectedValue);
    if (opt.value == selectedValue) {
      opt.selected=true;
      break;
    }
  }
}

