//
// AJAX based Table functions. These functions talk to the server
// and reload table pages in-place.
//

var XMLTableRequest = null;
var XMLTableInfo = {};

/**
  This method is called when the form is created, and sets a fixed height to
  the <div> section that surrounds the <table>. Our javaScript allows the <div>
  to grow, but not shrink, so that the buttons don't move around too much.
*/
function tableInit(formId, scrollId, tableId, tableName, instanceId, currentPage, pageCount)
{
  if (pageCount == 1)
    return;
    
  var table = document.getElementById(tableId);
  tableSetSize(table);
  
  table.pageCount = pageCount;
  table.parentNode.oldHeight = 0;
  tableSetPageNumber(table, currentPage);
}

/**
  Adjust the size of the surrounding <div> section.
*/
function tableSetSize(table)
{
  // In IE, add an offset of 5 to cover the borders.
  // This just leaves a slight margin in other browsers,
  // and that's fine.
  var tableHeight = table.offsetHeight + 5;
  var parentDiv = table.parentNode;
  if (tableHeight >= parentDiv.offsetHeight)
    parentDiv.style.height = tableHeight + "px";
}

function tableEnableDecoration(decorationId, enabled)
{
  var decoration = document.getElementById(decorationId);
  if (enabled)
    decoration.className = "tableDecorationEnabled";
  else
    decoration.className = "tableDecorationDisabled";
}

/**
  Set the class of the "next" and "previous" buttons.
*/
function tableSetPageNumber(table, pageNumber)
{
  tableEnableDecoration("N" + table.id, pageNumber < table.pageCount);
  tableEnableDecoration("P" + table.id, pageNumber > 1);
}

function tableDisableDecorations(table)
{
  tableEnableDecoration("N" + table.id, false);
  tableEnableDecoration("P" + table.id, false);
}

function tableLoadPage(formId, rootInstanceId, instanceId, tableName, tableBody, pageNumber)
{
  if (XMLTableRequest != null)
    return;
    
  XMLTableRequest = BrowserGetXMLHttpRequest();
  XMLTableInfo.tableBody = tableBody;
  XMLTableInfo.pageNumber = pageNumber;
  
  tableDisableDecorations(tableBody.parentNode);

  // FIXME: this should be asynchronous (ie, false should be changed to true)
  XMLTableRequest.open("GET", getContext(formId) +
    "/tang.table" +
    "?rid=" + rootInstanceId +
    "&iid=" + instanceId +
    "&tname=" + tableName +
    "&page=" + pageNumber +
    "&formId=" + formId, true);
    
  // This needs to be done first or the server might respond
  // before we start the timer. This means that the timer is
  // stopped before it is started, which looks silly.
  startWaitTimer();
  
  XMLTableRequest.onreadystatechange = tablePageLoaded;
  XMLTableRequest.send(null);
}

function tablePageLoaded()
{
  var tableRequest = XMLTableRequest;
  // only if req shows "loaded"
  if (tableRequest.readyState != 4)
    return;
  
  // After this, the request is finished regardless
  // of what happened.
  stopWaitTimer();
  XMLTableRequest = null;
  
  if (tableRequest.status != 200)
  {
    alert("Received invalid response: " + xmlRequest.statusText);
    return;
  }
  
//  alert(tableRequest.responseText);
  
  var xml = tableRequest.responseXML;

  var tableBodyElement = xml.documentElement;	// Get to the <tbody> element
  
  var newTableBody = BrowserImportNode(document, tableBodyElement);
  
  var table = XMLTableInfo.tableBody.parentNode;
  table.removeChild(XMLTableInfo.tableBody);
  table.appendChild(newTableBody);
  
  tableSetSize(table);
  tableSetPageNumber(table, XMLTableInfo.pageNumber);
  BrowserFixImages();
  growArea("main", "menubar");
  return;  
}

function tablePaged(formId, rootInstanceId, instanceId, tableName, bodyId, controlNodeName, direction)
{
  var controlElement = getForm(formId).elements[controlNodeName];
  var tableBody = document.getElementById(bodyId);
  var table = tableBody.parentNode;

  var newPageNumber = controlElement.value;
  newPageNumber = Number(newPageNumber) + direction;
  
  if ((newPageNumber < 1) || (newPageNumber > table.pageCount))
    return;
  
  controlElement.value = newPageNumber;
  tableLoadPage(formId, rootInstanceId, instanceId, tableName, tableBody, newPageNumber);
}
