SavedQueryIndexPanel = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility);
};

SavedQueryIndexPanel.prototype   = new GuiWidget();
SavedQueryIndexPanel.constructor = SavedQueryIndexPanel;
SavedQueryIndexPanel.superclass  = GuiWidget.prototype;
 
SavedQueryIndexPanel.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility)
{
  SavedQueryIndexPanel.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "LabelPanel");
  this.id = newId;
  this.savedQueryList = Array();
  this.queryControl = null;
  this.captionElement = new GuiWidget(this.id+"captionElement", this.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
    this.captionElement.element.cbe.innerHtml("Query Index");
  this.listForm = new GuiWidget(this.id+"listForm", this.element, 0, 20, 4, this.width(), 100, true, "");  
  this.height(200);
};

SavedQueryIndexPanel.prototype.initialize = function()
{
  this.getList();
};

SavedQueryIndexPanel.prototype.getList = function()
{
  var requestInfo = new Object();
  var request = 'SQL.storedDefinedQuery.getList';
  requestInfo.mode = 'getList';
  var sessionID = document.getSessionID();
  if (sessionID!=null)
    makeASyncPostRequest(this,requestInfo, XMLRPC_URL,request,document.getSessionID());
  else
    this.updateListData(Array());
};

SavedQueryIndexPanel.prototype.callback = function(serverReplyDoc, requestInfo)
{
  var pendingOperation = requestInfo.mode;
  if(serverReplyDoc == null) 
    return(null);
  if(serverReplyDoc.xml == '') 
    return(null);
  if(serverReplyDoc.documentElement.nodeName == 'parsererror')
  {
    this.setWaiting(false);
    return(null);
  }
    
  var clientReply = new XMLRPCResponse();
  //clientReply.setResponseByStr(serverReplyDoc.xml);
  clientReply.setResponseByDoc(serverReplyDoc);
  if(clientReply.isFault())
  {
    var data = null;
    alert(this.id+'.callback:  clientReply fault:\n\n'+clientReply.getFaultCode()+'\n'+clientReply.getFaultString());
  }
  else
    var data = clientReply.getObject();

  if(data != null)
  {
    switch (pendingOperation)
    {
      case 'getList':
        if (data)
          this.updateListData(data);
        else
          this.updateListData(Array());
        break;
      case 'deleteFromList':
        this.getList();
        break;
      //default:
      //  alert('unknown pO: '+pendingOperation+' with data = '+data);
    }
  }
};

SavedQueryIndexPanel.prototype.updateListData = function(data)
{
  /* categorize by query */
  var newList = Array();
  for(var lcv in data)
  {
    var queryName = data[lcv][1];
    if (!newList[queryName])
      newList[queryName] = Array();
    newList[queryName][newList[queryName].length] = data[lcv];
  }
  delete(this.savedQueryList);
  this.savedQueryList = newList;
  this.draw();
};

SavedQueryIndexPanel.prototype.draw = function()
{
  var htmlString = '<table id="'+this.id+'savedQueryindexTable" class="labelList" cellpadding="0" cellspacing="0" width="100%" style="margin-right: 10px">';
  var allEmpty = true;
  var listHeight = 0;

  for (var queryName in this.savedQueryList)  //loop through query lists
  {
    queryListEmpty = true;
    queryListString = '';
    if (this.savedQueryList[queryName].length > 0)
    {
      var queryDescription = this.queryControl.queryList[queryName]["description"];
      queryListString = '<tr><td colspan = "2" class="labelListThemeHeader">'+queryDescription+'</td></tr>';
      listHeight += 50;
      for (var j=0; j<this.savedQueryList[queryName].length; j++)  //loop through query list for a given query name
      {
          allEmpty = false;
          queryListEmpty = false;
          listHeight += 100;
          var savedName = this.savedQueryList[queryName][j][0];
          queryListString = queryListString 
            + '<tr>'
            +'<td class="bookmarkKillButton"><span class="kill" onclick="document.getWidgetById(\'savedQueryIndexPanel\').deleteSavedQuery(\''+savedName+'\');"><img src="'+GuiWidget.THEME_PATH+GuiWidget.THEME+'/images/deleteBookmark.png"></span></td>'
            +'<td class="bookmarkTitle"><span class="pseudolink" onclick="'
            +'document.getWidgetById(\'savedQueryIndexPanel\').runSavedQuery(\''+queryName+'\',\''+savedName+'\','+j+');">'
            +savedName
            +'</span></td>'
            +'</tr>';
        }
    }
    if (!queryListEmpty)
      htmlString = htmlString + queryListString; 
  }
  htmlString = allEmpty?('<table id="'+this.id+'savedQueryIndexTable" class="labelList" cellpadding="0" cellspacing="0" width="100%" style="margin-right: 10px"><tr><td>No Saved Queries.</td></tr></table>'):(htmlString + '</table>');
  this.listForm.height(allEmpty?100:listHeight);
  this.listForm.element.cbe.innerHtml(htmlString);
  this.height(listHeight+120);
};

SavedQueryIndexPanel.prototype.deleteSavedQuery = function(savedName)
{
  var requestInfo = new Object();
  var request = 'SQL.storedDefinedQuery.remove';
  requestInfo.mode = 'deleteFromList';
  makeASyncPostRequest(this,requestInfo, XMLRPC_URL,request,document.getSessionID(),savedName);
};

SavedQueryIndexPanel.prototype.runSavedQuery = function(queryName,savedName,savedQueryNum)
{
  var thisSavedQuery = this.savedQueryList[queryName][savedQueryNum];
  /* put into QueryControl.runQuery style format for code reuse */
  var queryParams = thisSavedQuery[2];
  var newParams = Array();
  for(var lcv in queryParams)
  {
    var placeholder = queryParams[lcv][0];
    var value = queryParams[lcv][1];
    newParams[placeholder] = value;
  }
  this.queryControl.runQuery(queryName,newParams,1,true);
};

SavedQueryIndexPanel.prototype.resize = function(newWidth, newHeight)
{
  this.element.cbe.resizeTo(newWidth, this.height());
  this.captionElement.resize(newWidth, this.captionElement.height());
  this.listForm.resize(newWidth, this.listForm.height());
};

