function on_type_change(form) {
  var src = form.elements['type'];
  var dest = form.elements['variety'];
  var change_handler = new TypeChangeHandler (form);
  
  if ((src == null) || (src.value == '')) {
    dest.options.length = 0;
    dest.options[0] = new Option ('Select a wine type first', '');
    return;
  }
  
  queue.request ('GET', 'ajax_get_varieties.php?type=' + src.value, change_handler);
}


function TypeChangeHandler (form) {
  this.form = form;
  
  this.process = function (top_node) {
    var dest = this.form.elements['variety'];
    var nodes = top_node.getElementsByTagName ('i');
    
    dest.options.length = 0;
    
    if (nodes.length == 0) {
      dest.options[0] = new Option ('n/a', '');
      return;
    }
    
    if (this.form.name == 'quicksearch') {
      dest.options[0] = new Option ('-- Any Grape Variety --', '');
    } else {
      dest.options[0] = new Option ('All varieties', '');
    }
    
    for (var x = 0; x < nodes.length; x++) {
      dest.options[x+1] = new Option (nodes[x].firstChild.nodeValue, nodes[x].getAttribute ('v'));
    }
  }
}
