﻿/******************
JavaScript Table representation loaded from an XHTML request

RajarStationsTable inherites from AjaxTable in AjaxTable.js
***********************************************************/

function RajarStationsTable(url,onload)
{
  //Inherit properties from AjaxTable
  AjaxTable.call(this,'tblStations',url,onload);
  this.showselect = true;
  this.rowid = 0; //Which column should be used as the row id?
  this.dataview.sort = "Name ASC";
  this.dataview.showcolumns = new Array(1); //Call wrong array constructor when only one entry
  //So need to do
  this.dataview.showcolumns[0] = 1;
  this.tablerendertarget = null;
  this.salesrendertarget = null;
  this.sortascimage = "images/triangasc.gif";
  this.sortdescimage = "images/triangdesc.gif";
  
  this.columnheadings = new Array();
  this.columnheadings[0] = "ID"
  this.columnheadings[1] = "Name"
  this.columnheadings[2] = "FirstLetter"
}
//inherit methods from AjaxTable
RajarStationsTable.prototype.inheritFrom(AjaxTable);

  RajarStationsTable.prototype.getStation = function(rownum)
  {
    //Get the raw station from the underlying datatable
    var station = this.getRow(rownum);
    //And cast this to a station object
    return new RajarStation(station);
  };
  
  RajarStationsTable.prototype.onRowCheck = function(table,rownum,checked)
  {
    AjaxTable.prototype.onRowCheck.call(this,table,rownum,checked);
    //Put the selection into the hidden form field so that it can be posted back
    var hiddenform = document.getElementById('selectedstations');
    hiddenform.value = stations.SelectedIDs();
  }
  
  //Override AjaxTable implementation of CreateTable to create a StationsTable
  RajarStationsTable.prototype.CreateTable = function()
  {
    //TODO - really want to make better use of inheritance
    var stations = new RajarStationsTable();
    stations.columnnames = this.columnnames;
    stations.tablerendertarget = this.tablerendertarget;
    return stations;
  }
   
  //Override AjaxTable implementation of onColSort to refresh display
  RajarStationsTable.prototype.onColClick = function(colnum)
  {
    //call base
    AjaxTable.prototype.onColClick.call(this,colnum);
    this.render();
  }
  
  RajarStationsTable.prototype.displayStations = function(tabletarget)
  {
    this.tablerendertarget = document.getElementById(tabletarget);
    this.render();
  }
  
  RajarStationsTable.prototype.render = function()
  {
    if(this.tablerendertarget != null)
    {
      this.tablerendertarget.innerHTML = ""; 
      var table = this.dataview.getDataTable(); 
      this.tablerendertarget.appendChild(table);
    }
  }
  
  RajarStationsTable.prototype.Ids = function()
  {
    //Build an Array of the IDs in this RajarStationsTable
    var scount = this.getCount();
    idArr = new Array(scount);
    for(var i=0;i<scount;i++)
    {
      var station = this.getStation(i);
      idArr.push(station.id);
    }
    return idArr.join(",");
  }
  
function RajarStation(stationrow)
{
  this.id              = stationrow[0];
  this.name            = stationrow[1];
  this.firstletter     = stationrow[2];
}


