// Code for the disabled SELECT boxes
// some taken from 
// http://www.lattimore.id.au/2005/07/01/select-option-disabled-and-the-javascript-solution/

// this Map[ selectElementId ] --> index of option
// stores the current selected option, so we can 
// revert to it in case a 'disabled' option is selected
var previousSelectedIndex = new Array();

// call from Select.onfocus: storeCurrentSelection(this)
//
// store the selected choice for option element
function storeCurrentSelection(element) {
  previousSelectedIndex[element.id] = element.selectedIndex; 
}
     
// call from SELECT.onchange: checkSelected(this)
//
// this method checks if the newly selected option is not disabled
// If it is, then revert to the previous selection.
// Also update the current selection
function checkSelected(element) {
  if ( element.options[element.selectedIndex].disabled ) {
    if ( Math.abs( element.selectedIndex - previousSelectedIndex[element.id] ) == 1 ) {
      element.selectedIndex += (element.selectedIndex - previousSelectedIndex[element.id]);
    } else {
      element.selectedIndex = previousSelectedIndex[element.id];
    }
  }
  element.onfocus();
}
  
// call from init code, or whenever you change 
// the disabled status from any option in a select.
// 
// process all options in the select box, and grey-out the disabled ones
function updateDisableds(select) {
  for (var i=0, option; option = select.options[i]; i++) {
    if (option.disabled) {
      option.style.color = "graytext";
    } else {
      option.style.color = "menutext";
    }
  }
}

// Select element:
// onfocus="storeCurrentSelection(this);"
// onchange="checkSelected(this);"

// after
//  updateDisableds(this);


// demo method, throw away
function updateDivisibleBy( nr, selected, selectId ) {
  var select = document.getElementById(selectId);
  for (var i=0, option; option = select.options[i]; i++) {
    if ( ((i+1) % nr) == 0 ) {
      option.disabled = selected;
    }
    updateDisableds(select);
  }
}

