#include <lib/std.mi>

Function sendQuery();

Global Group pgroup;
Global Edit editfield;

Global string lastquery;

System.onScriptLoaded() {
  // we only run in groups!
  pgroup = getScriptGroup();
  if (pgroup == NULL) {
    messageBox("queryedit.maki can only run within a group", "Error", 0, "");
    return;
  }

  String id = getToken(getParam(), ";", 0);
  editfield = pgroup.findObject(id);
  if (editfield == NULL) {
    messageBox("queryedit.maki cannot find the edit field (param 0 = " + id + ")", "Error", 0, "");
  }
}

editfield.onEnter() {
  sendQuery();
}

editfield.onIdleEditUpdate() {
  if (lastquery == editfield.getText()) return;
  sendQuery();
}

sendQuery() {
  String id = getToken(getParam(), ";", 1);
  GuiObject queryline = getScriptGroup().findObject(id);
  lastquery = editfield.getText();
  if (queryline != NULL) {
    queryline.setXmlParam("auto", "1");
    queryline.setXmlParam("query", lastquery);
  } else
    messageBox("queryedit.maki cannot find the queryline field (param 1 = " + id + ")", "Error", 0, "");
}

