function LiveSelect(select, dataset, valueFieldName, descriptionFieldName, initialValue, initialText) {
  this._select = select;
  this._dataset = dataset;
  this._valueFieldName = valueFieldName;
  this._descriptionFieldName = descriptionFieldName;
  this._initialValue = initialValue;
  this._initialText = initialText;
  this._retrievalNo = 0;
  this._extraData = new Array();
}

LiveSelect.prototype._select;
LiveSelect.prototype._dataset;
LiveSelect.prototype._valueFieldName;
LiveSelect.prototype._descriptionFieldName;
LiveSelect.prototype._initialValue;
LiveSelect.prototype._initialText;
LiveSelect.prototype._retrievalNo;
LiveSelect.prototype._extraData;

LiveSelect.prototype.getValue = function() {
  return this._select.options[this._select.selectedIndex].value
}

LiveSelect.prototype.clearSelect = function() {
  while (this._select.childNodes.length > 0) {
    this._select.removeChild(this._select.childNodes[this._select.childNodes.length - 1]);
  }
}

LiveSelect.prototype.addOption = function(value, text) { 
  var option = document.createElement("option");
  option.value = value;
  option.appendChild(document.createTextNode(text));
  this._select.appendChild(option);
  return option;
}

LiveSelect.prototype.setParameter = function(name, value) {
  this._extraData[name] = value;
}

// user defined arguments:
//   0 - live select
//   1 - retrieval number
//   2 - value to set as selected
function loadSelect(o) {
  // If this is the most recent data retrieval request for this select
  var select = o.argument[0]
  if (o.argument[1] == select._retrievalNo) {
    var results = JSON.parse(o.responseText);
    if (select._initialValue && select._initialText) {
      select.addOption(select._initialValue, select._initialText);
    }
    var newOption;
    for (var index = 0; index < results.row.length; index++) {
      newOption = select.addOption(results.row[index][select._valueFieldName], results.row[index][select._descriptionFieldName]);
      if (newOption.value == o.argument[2]) { 
        newOption.selected = true;
      }
    }
  }
}

function jsonFailure(o) {
  alert(o.statusText);
}

LiveSelect.prototype.retrieveSelect = function(selectedOption) {
  this._retrievalNo = this._retrievalNo + 1;

  var callback =
  {   
    success: loadSelect,   
    failure: jsonFailure,   
    argument: [this, this._retrievalNo, selectedOption]
  }

  var extraData = '';
  for (name in this._extraData) {
    extraData = extraData + name + '=' + this._extraData[name] + '&';
  }

  if (extraData != '') {
    extraData = extraData.substr(0, extraData.length - 1);
  }

  var transaction = YAHOO.util.Connect.asyncRequest('GET', '/servlet/Auction/JsonDataset.jsp?dataset=' + this._dataset + '&' + extraData, callback, null);
}
