QueryForm = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newQueryControl, newQueryControlId, newQueryIndex)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newQueryControl, newQueryControlId, newQueryIndex);
};

QueryForm.prototype = new GuiWidget();
QueryForm.constructor = QueryForm;
QueryForm.superclass = GuiWidget.prototype;

QueryForm.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newQueryControl, newQueryControlId, newQueryIndex)
{
  QueryForm.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "");
  this.id = newId;
  this.queryIndex = newQueryIndex;
  if (newQueryControlId!=null)
  {
    this.queryControl = OBJECT_MANAGER.getControl(newQueryControlId);
    this.queryControlId = newQueryControlId;
  }
  else
  {
    this.queryControl = newQueryControl;
    this.queryControlId = newQueryControl.id;
  }
  this.queryConfig = this.queryControl.queryList[this.queryIndex];

  this.formWidgets = new Array;
  this.inputElements = new Array;
  this.captionElement = new GuiWidget(this.id+"captionElement", this.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
  this.captionElement.element.cbe.innerHtml(this.queryConfig.description);
  this.formContainer = new GuiWidget (this.id+"formContainer", this.element, 0, this.captionElement.height(), 0, this.width(), 0, true, "queryObjectBottom");
  var totalHeight = 20;
  for (var currentField in this.queryConfig.fieldList)
  {
    this.formWidgets[currentField] = new GuiWidget(this.id+"."+currentField+"FormElement", this.formContainer.element, 0, this.formContainer.height(), 0, this.width(), 25, true, "queryObjectForm");
    switch (this.queryConfig.fieldList[currentField].fieldType)
    {
      case 'labelField':
        this.formWidgets[currentField].element.cbe.innerHtml(this.queryConfig.fieldList[currentField].value);
        break;
      case 'textField':
        this.formWidgets[currentField].element.cbe.innerHtml(this.queryConfig.fieldList[currentField].caption
             +'<input type="text" id="'+this.id+'['+currentField+'].inputElement" value = "'
             +this.queryConfig.fieldList[currentField].initialValue
             +'" size="'+this.queryConfig.fieldList[currentField].width
             +'" onKeyDown="document.getWidgetById(\''+this.id+'\').onKeyPress(this,event);">');
        this.inputElements[currentField] = document.getElementById(this.id+"["+currentField+"].inputElement");
        break;
      case 'selectField':
        selectBoxHTML = '<select id="'+this.id+'['+currentField+'].inputElement">';
        for (var optionIndex=0; optionIndex< this.queryConfig.fieldList[currentField].optionList.length; optionIndex++)
        {
          selectBoxHTML += this.buildSelectOptionString(this.queryConfig.fieldList[currentField].optionList[optionIndex], currentField);
        }
        selectBoxHTML +='</select>';
        this.formWidgets[currentField].element.cbe.innerHtml(this.queryConfig.fieldList[currentField].caption+selectBoxHTML);
        this.inputElements[currentField] = document.getElementById(this.id+"["+currentField+"].inputElement");
        break;
      default:
        alert('unknown field type in query '+this.id+':'+this.queryConfig.fieldList[currentField].fieldType);
        break;
    }
    this.formContainer.height(this.formContainer.height()+25);
    totalHeight += 25;
  }
  this.submitButton = new ImageButton(this.id+".submitButton", this.formContainer.element, 10, this.formContainer.height(), 0, 50,20,true,'searchButton','');
  this.submitButton.queryForm = this;
  this.submitButton.clickEvent =  function(e)
  {
    this.queryForm.runQuery();
  };
  var XOffset = 70;
  if ((OBJECT_MANAGER.getGuiValueById('extensions')["savedQueries"]) && (OBJECT_MANAGER.getGuiValueById('extensions')["savedQueries"].enabled))
  {
    this.saveSearchButton = new ImageButton(this.id+'.saveSearchButton',this.formContainer.element, XOffset, this.formContainer.height()-3, 0, 25, 25, true,'saveSearchButton','Save Search');
    this.saveSearchButton.queryForm = this;
    this.saveSearchButton.clickEvent = function(e)
    {
      this.queryForm.saveQuery();
    };
    XOffset += 30;
  }
  if ((OBJECT_MANAGER.getGuiValueById('extensions')["exportQueryToCSV"]) && (OBJECT_MANAGER.getGuiValueById('extensions')["exportQueryToCSV"].enabled))
  {
    if ((this.queryConfig["export"]) && (this.queryConfig["export"].csv) && (this.queryConfig["export"].csv.enabled==true))
    {
      this.exportQueryToCSVButton = new ImageButton(this.id+'.exportQueryToCSVButton',this.formContainer.element, XOffset, this.formContainer.height()-3, 0, 25, 25, true,'exportQueryToCSV','Export Search To CSV');
      this.exportQueryToCSVButton.queryForm = this;
      this.exportQueryToCSVButton.clickEvent = function(e)
      {
        this.queryForm.exportQuery('csv');
      };
      XOffset += 30;
    }
  }
  this.formContainer.height(this.formContainer.height()+25);
  totalHeight+=25;
  this.height(totalHeight);
};

QueryForm.prototype.buildSelectOptionString = function(optionObject, currentField)
{
  if (optionObject.type == 'optgroup')
  {
    var optionHTML = '<optgroup label='+optionObject.description+'>';
    for (var optionIndex=0; optionIndex < optionObject.value.length; optionIndex++)
    {
      optionHTML += this.buildSelectOptionString(optionObject.value[optionIndex],currentField);
    }
    optionHTML += '</optgroup>';    
  }
  else
    var optionHTML = ('<option value="'+optionObject.value+'"'
      +((this.queryConfig.fieldList[currentField].defaultValue == optionObject.value)?' selected ':'')
      +' >'
      +optionObject.description
      +'</option>'
    );
  return optionHTML;
};

QueryForm.prototype.runQuery = function()
{
  var queryValues = new Object;
  for (var currentField in this.inputElements)
  {
    queryValues[currentField] = ((this.inputElements[currentField].value == '')?(this.queryConfig.fieldList[currentField].defaultValue):(this.inputElements[currentField].value));
  }
  this.queryControl.runQuery(this.queryConfig.id,queryValues,1,true);
};

QueryForm.prototype.onKeyPress = function(textField,evt)
{
  //check for enter key and run query if pressed.
  if (!evt)
    evt = window.evt; 
  else
    if (!evt.keyCode)
      evt.keyCode = evt.which;
  if (evt.keyCode == 13)
    this.runQuery();
};

QueryForm.prototype.saveQuery = function()
{
  var saveName = prompt('Save Query Name',"");
  if (saveName == null)
    return;
  var queryValues = new Object;
  for (var currentField in this.inputElements)
    queryValues[currentField] = ((this.inputElements[currentField].value == '')?(this.queryConfig.fieldList[currentField].defaultValue):(this.inputElements[currentField].value));
  this.queryControl.saveQuery(escapeHTML(stripQuotes(saveName)),this.queryConfig.id,queryValues);
};

QueryForm.prototype.exportQuery = function(outputType)
{
  var queryValues = new Object;
  for (var currentField in this.inputElements)
    queryValues[currentField] = ((this.inputElements[currentField].value == '')?(this.queryConfig.fieldList[currentField].defaultValue):(this.inputElements[currentField].value));
  this.queryControl.exportQuery(outputType,this.queryConfig.id,queryValues);
};

