/**
 * Script file containing common features of both the Currencies and Interbank
 * quotes.
 *
 * Things that you cannot fing in the cs_quotes_core.js file or the
 * interbank_quotes_core.js file are mostly to be found in this one.
 *
 * @author Elie Zedeck RANDRIAMIANDRIRAY <rez@eliezedeck.com>
 */

var currentCumulatedConnections = 0;
var currentCumulatedInterbankConnections = 0;
var connected = false;

/**
 * Determines the status of the Ajax request
 */
function callInProgress(xmlhttp) {
  switch (xmlhttp.readyState) {
    case 1: case 2: case 3:
      return true; break;
    default:
      return false; break;
  }
}

/**
 * Returns another version of the same floating point number, but with 5 decimal
 * places.
 */
function displayFloatNicely(float_number)
{
  return float_number.toFixed(5);
}

// register time-out handling functions for all Ajax requests
Ajax.Responders.register({
  onCreate: function(request) {
    request['timeoutId'] = window.setTimeout(
      function() {
        // If we have hit the timeout and the AJAX request is active, abort it and let the user know
        if (callInProgress(request.transport)) {
          request.transport.abort();
          // Run the onFailure method
          if (request.options['onFailure']) {
            request.options['onFailure'](request.transport, request.json);
          }
        }
      },
      timoutBeforeDisconnected * 1000 // Computed timeout
    );
  },
  onComplete: function(request) {
    // Clear the timeout, the request completed ok
    window.clearTimeout(request['timeoutId']);
  }
});

function moveTableColumn(clickedTableHead, direction)
{
  // Ensure that it's the Table Head tag (TH)
  if (clickedTableHead.tagName != 'TH') return;

  // Find the Index of the Column
  var columnIndex = null;
  var theRow = clickedTableHead.up();
  var numberOfColumns = theRow.children.length;

  for (var i = 0; i < numberOfColumns; i ++)
  {
    if (clickedTableHead === theRow.children[i])
    {
      columnIndex = i;
      break;
    }
  }

  if (columnIndex === null) return;

  var allRows = $$('#LiveQuotesTable tr');
  var amountOfRows = allRows.length;

  // Ensure that there is going to be rows of data, not just the header and footer
  if (amountOfRows == 2) return;

  for (var i = 0; i < amountOfRows; i ++)
  {
    row = allRows[i];
    cells = row.children;

    if (cells.length != numberOfColumns) continue;

    if (direction == 'left')
    {
      if (columnIndex == 0)
        row.insertBefore(cells[columnIndex], cells[numberOfColumns]);
      else
        row.insertBefore(cells[columnIndex], cells[columnIndex - 1]);
    }
    else if (direction == 'right')
    {
      if (columnIndex == numberOfColumns - 1)
        row.insertBefore(cells[columnIndex], cells[0]);
      else
        row.insertBefore(cells[columnIndex + 1], cells[columnIndex]);
    }
  }

  // Compute the new Order of the Columns
  var newOrder = '';
  $$('th._head').each(function(elem) {
    newOrder = newOrder + elem.id + ',';
  });

  return newOrder.substr(0, newOrder.length - 1);
}