var Suggest = function(rootEl, q, formEl, textBoxEl, idEl, uri, param, successHandler, instructions, networkType, placeholderText, defaultOptions, showNoMatches, override_resize) {
  this.onInputChange = function() {
    var currentInputValue = oThis.typeAheadObj.currentInputValue;
    var cache = oThis.getCache(currentInputValue);
    if (cache) {
      oThis.onSuggestRequestDone(currentInputValue, cache[0], cache[1], cache[2]);
    } else {
      var typeStr = "";

      var data = {};
      data[oThis.suggestParam] = currentInputValue;
      if (oThis.networkType) {
        data['t'] = oThis.networkType;
      }

      var asyncRequestGet = new AsyncRequest()
        .setURI(oThis.suggestURI)
        .setData(data)
        .setHandler(function(response) {
          var payload = response.payload;
          oThis.onSuggestRequestDone(currentInputValue, payload.suggestNames, payload.suggestIDs, payload.suggestLocs, oThis.typeAheadObj.pEvent);
        })
        .setErrorHandler(function(response) {
          new Dialog()
            .setTitle(tx('sh:error-occurred'))
            .setBody(tx('su01'))
            .setButtons(Dialog.OK)
            .show();
        })
        .setMethod('GET')
        .setReadOnly(true)
        .send();
    }
  }


  this.onSuggestRequestDone = function(key, names, ids, locs, pEvent) {
    this.setCache(key, names, ids, locs);
    if (this.typeAheadObj.displaySuggestList(names, ids, locs)) {
      this.typeAheadObj.pEvent = pEvent;
      this.typeAheadObj.onListChange();
    }
  }

  this.getCache = function(key) {
    return this.suggestCache[key.toUpperCase()];
  }

  this.setCache = function(key, names, ids, locs) {
    this.suggestCache[key.toUpperCase()] = new Array(names, ids, locs);
  }

  this.init = function() {
    this.suggestURI = uri;
    this.suggestParam = param;
    this.suggestCache = [];
    this.networkType = networkType;
    if (!instructions) {
      instructions = tx('su02');
    }

    textBoxEl.value = q;
    this.typeAheadObj = new TypeAhead(rootEl, formEl, textBoxEl, idEl, defaultOptions, instructions, 0, successHandler, this.onInputChange, null, null, null, placeholderText, showNoMatches, override_resize);
  }

  var oThis = this;
  this.init();
}

function debug(str) {
  document.getElementById("debug").innerHTML += str + "<BR>";
}
