var recordings = new Array();

function Recording_writeTableRow()
{
  document.write("<TR><TD><A NAME=\"" + this.filename + "\"></A>" +
    this.dateTime.toLocaleDateString() + "</BR>" + this.dateTime.toLocaleTimeString() +
    "</TD>");

  document.write("<TD>" + (this.series == null ? "&nbsp;" : this.series) + "</TD><TD>" +
    this.title + "</TD><TD>" + this.presenter + "</TD>");

  if (this.size != null)
    document.write("<TD><A HREF=\"" + this.filename + ".mp3\"><IMG CLASS=\"Icon\" " +
      "SRC=\"images\/download.png\" TITLE=\"Download\"></A></TD><TD><A HREF=\"" + this.filename +
      ".m3u\" TYPE=\"audio/x-mpegurl\"><IMG CLASS=\"Icon\" SRC=\"images\/play.png\" " +
      "TITLE=\"Listen\"></A></TD>");
  else
    document.write("<TD>&nbsp;</TD><TD>&nbsp;</TD>");

  if (this.onTape)
    document.write("<TD><A HREF=\"javascript:showForm(&quot;" + this.filename + "&quot;,&quot;Tape&quot;);\">" +
      "<IMG CLASS=\"Icon\" SRC=\"images\/cassette.png\" TITLE=\"Borrow a Tape\"></A></TD>");
  else
    document.write("<TD>&nbsp;</TD>");

  if (this.digitized)
    document.write("<TD><A HREF=\"javascript:showForm(&quot;" + this.filename + "&quot;,&quot;CD&quot;);\">" +
      "<IMG CLASS=\"Icon\" SRC=\"images\/cd.png\" TITLE=\"Borrow a CD\"></TD>");
  else
    document.write("<TD>&nbsp;</TD>");

  if (this.size != null)
    document.write("<TD>" + this.sizeInBytes()) + "</TD>";
  else
    document.write("<TD>&nbsp;</TD>");

  if (this.length != null)
  {
    var seconds = this.length % 60;
    var minutes = Math.floor(this.length / 60) % 60;
    var hours = Math.floor(this.length / 3600);

    document.write("<TD>" + hours + ":" + numberToPaddedString(minutes, 2, 0) + ":" + numberToPaddedString(seconds, 2, 0) + "</TD>");
  }
  else
    document.write("<TD>&nbsp;</TD>");

  document.writeln("</TR>");

  return;
}

function Recording_sizeInBytes(numDecimals)
{
  var newSize   = this.size;
  var extension = "bytes"

  if (newSize > 1024)
  {
    newSize /= 1024
    extension = "KB";

    if (newSize > 1024)
    {
      newSize /= 1024
      extension = "MB";

      if (newSize > 1024)
      {
        newSize /= 1024
        extension = "GB";

        if (newSize > 1024)
        {
          newSize /= 1024
          extension = "TB";
        }
      }
    }
  }

  return newSize.toFixed(numDecimals) + "&nbsp;" + extension;
}

function Recording(filename, series, title, presenter, onTape, digitized, size, length)
{
  this.dateTime  = new Date(
                     parseInt(filename.substr(0, 4), 10),
                     parseInt(filename.substr(4, 2), 10) - 1,
                     parseInt(filename.substr(6, 2), 10),
                     parseInt(filename.substr(9, 2), 10),
                     parseInt(filename.substr(11, 2), 10),
                     0);

  this.series    = series;
  this.title     = title;
  this.presenter = presenter;
  this.onTape    = onTape;
  this.digitized = digitized;
  this.filename  = filename;
  this.size      = size;
  this.length    = length;

  this.writeTableRow = Recording_writeTableRow;
  this.sizeInBytes   = Recording_sizeInBytes;

  return;
}

function numberToPaddedString(source, length, numDecimals)
{
  var sourceNumber = new Number(source);
  var result = sourceNumber.toFixed(numDecimals);

  while (result.length < length)
    result = '0' + result;

  return result;
}

/*********************************************************************************************/

function showForm
(
  recordingID,
  medium
)

{
  {
    /*
    "width" and "height" are arbitrary values for the size (in pixels) of the window.  From
    these values, though, the window is created in the centre of the screen.
    */

    var width    = 720;
    var height   = 480;
    var leftPos  = screen.width ? (screen.width - width ) / 2 : 50;
    var topPos   = screen.height ? (screen.height - height) / 2 : 50;
    var settings = "width=" + width + ",height=" + height + ",top=" + topPos + ",left=" +
                   leftPos + ",scrollbars=1,location=0,directories=0,status=0,menubar=0," +
                   "toolbar=0,resizable=1";

    window.open("order.html?id=" + recordingID + "&medium=" + medium, "OrderForm", settings);

    return;
  }
}


