// utility function for select drop down menus
jQuery.fn.emptySelect = function() {
  return this.each(function(){
    if (this.tagName=='SELECT') this.options.length = 0;
  });
}

// chain select menus
jQuery.fn.chainSelect = function( target, url, chain_empty, load_on_start ) 
{
  return this.each( function()
  {
	  $(this).change( function( ) 
	  {
      var firstValue = $(this).val();
      var dropdownSet = $(target);
      var tempValue = dropdownSet.val();
      if (chain_empty && firstValue.length == 0) {
        dropdownSet.attr("disabled",true);
        dropdownSet.emptySelect();
      }
      else {
        dropdownSet.attr("disabled",false);
        dropdownSet.load(
          url,
          { value: firstValue },
          function(){
            dropdownSet.val(tempValue);
          }
        );
      }
	  });
	  if (load_on_start) {
	  	$(this).change();
	  }
  });
};
