function PSched(id, form)
{
  if (id)
  {
    this.id = id;
    this.form = form;
    PSched.idx[this.id] = this;
    PSched.arr[PSched.arr.length] = this;
    this.rows = new Array();
    this.rowsIdx = new Array();

    this.rowHdrs = new Array();

    this.colSelected = -1;
    this.rowSelected = -1;
    
    this.total = 0;
    this.qty = 0;
    this.label = '';
    this.orderEst = null
  }
}
new PSched(null);


PSched.arr = new Array();
PSched.idx = new Array();
PSched.OK = document.getElementById;

PSched.colors = new Object();
PSched.colors.selectedCell = 'white';
PSched.colors.selectedRowCol = '#B0B0B0';
PSched.colors.noSelect = '#888888';

function PSched_get(id)
{
  return PSched.schedsIdx[id];
}
PSched.get = PSched_get;


function PSched_prototype_addRowHdr(qty, tdElID, divElID)
{
  var idx = this.rowHdrs.length;
  this.rowHdrs[idx] = new Object();
  this.rowHdrs[idx].qty = qty;
  if (PSched.OK)
  {
    this.rowHdrs[idx].tdEl = document.getElementById(tdElID);
    this.rowHdrs[idx].divEl = document.getElementById(divElID);
  }
  else
  {
    this.rowHdrs[idx].tdEl = null;
    this.rowHdrs[idx].divEl = null;
  }
}
PSched.prototype.addRowHdr = PSched_prototype_addRowHdr;


function PSched_prototype_addRow(id, label, tdElID, divElID)
{
  this.rowsIdx[id] = new PSchedRow(id, label, tdElID, divElID);
  this.rowsIdx[id].sched = this;
  this.rowsIdx[id].rowNo = this.rows.length;
  this.rows[this.rows.length] = this.rowsIdx[id];
}
PSched.prototype.addRow = PSched_prototype_addRow;


function PSched_prototype_addCell(row_id, id, inputValue, qty, amt, tdElID, divElID, inputEl)
{
  var row;
  if (row = this.rowsIdx[row_id])
  {
    row.addCell(id, inputValue, qty, amt, tdElID, divElID, inputEl);
  }
}
PSched.prototype.addCell = PSched_prototype_addCell;


function PSched_prototype_onchange()
{
  var row, colSelected, foundSelected;
  
  for (i = 0; i < this.rowHdrs.length; i++)
  {
    if (i == this.colSelected)
    {
      //PSched.setStyleAtt(this.rowHdrs[i].tdEl, 'backgroundColor', PSched.colors.selectedRowCol);
    }
    else
    {
      //PSched.setStyleAtt(this.rowHdrs[i].tdEl, 'backgroundColor', PSched.colors.noSelect);
    }
  }
  
  for (i = 0, foundSelected = false; i < this.rows.length; i++)
  {
    row = this.rows[i];
    
    if (row.selected)
    {
      //PSched.setStyleAtt(row.tdEl, 'backgroundColor', PSched.colors.selectedRowCol);
    }
    else
    {
      //PSched.setStyleAtt(row.tdEl, 'backgroundColor', PSched.colors.noSelect);
    }
    
    for (j = 0; j < row.cells.length; j++)
    {
      if (row.cells[j].selected)
      {
        PSched.setStyleAtt(row.cells[j].tdEl, 'backgroundColor', PSched.colors.selectedCell);
        PSched.setStyleAtt(row.cells[j].tdEl, 'fontWeight', 'bold');
        this.total = row.cells[j].amt;
        this.qty = row.cells[j].qty;
        this.label = row.label;
        foundSelected = true;
      }
      else if ((row.selected) && (!foundSelected))
      {
        //PSched.setStyleAtt(row.cells[j].tdEl, 'backgroundColor', PSched.colors.selectedRowCol);
        PSched.setStyleAtt(row.cells[j].tdEl, 'fontWeight', 'normal');
      }
      else if ((j == this.colSelected) && (!foundSelected))
      {
        //PSched.setStyleAtt(row.cells[j].tdEl, 'backgroundColor', PSched.colors.selectedRowCol);
        PSched.setStyleAtt(row.cells[j].tdEl, 'fontWeight', 'normal');
      }
      else
      {
        //PSched.setStyleAtt(row.cells[j].tdEl, 'backgroundColor', PSched.colors.noSelect);
        PSched.setStyleAtt(row.cells[j].tdEl, 'fontWeight', 'normal');
      }
    }
  }
  
  if (this.orderEst)
  {
    this.orderEst.notifyPSchedChg(this);
  }
}
PSched.prototype.onchange = PSched_prototype_onchange;


function PSched_prototype_selectCell(cell)
{
  for (i = 0; i < this.rows.length; i++)
  {
    this.rows[i].selected = false; 
    for (j = 0; j < this.rows[i].cells.length; j++)
    {
      if (this.rows[i].cells[j].id == cell.id)
      {
        this.rows[i].cells[j].selected = true;
        this.colSelected = j;
        this.rowSelected = i;
        this.rows[i].selected = true; 
      }
      else
      {
        this.rows[i].cells[j].selected = false;
      }
    }
  }
}
PSched.prototype.selectCell = PSched_prototype_selectCell;


function PSched_prototype_getCell(row, col)
{
  if (this.rows[row])
  {
    return this.rows[row].cells[col]
  }
  return null;
}
PSched.prototype.getCell = PSched_prototype_getCell;


function PSched_prototype_init()
{
  if (this.rows[0])
  {
    if (this.rows[0].cells[0])
    {
      this.selectCell(this.rows[0].cells[0]);
      this.rows[0].cells[0].onclick();
    }
  }
}
PSched.prototype.init = PSched_prototype_init;


function PSched_init()
{
  for (i = 0; i < PSched.arr.length; i++)
  {
    PSched.arr[i].init();
  }
}
PSched.init = PSched_init;


function PSched_setStyleAtt(obj, att, val)
{
  if (obj && obj.style)
  {
    switch (att)
    {
      case 'backgroundColor':
        obj.style.backgroundColor = val;
        break;
      case 'fontWeight':
        obj.style.fontWeight = val;
        break;
      default:
        alert('No def for '+att);
    }
  }
}
PSched.setStyleAtt = PSched_setStyleAtt;






function PSchedRow(id, label, tdElID, divElID)
{
  if (id)
  {
    this.id = id;
    this.label = label;
    this.cells = new Array();
    this.cellsIdx = new Array();
    this.sched = null;
    this.selected = false;
    this.rowNo = 0;
    
    if (PSched.OK)
    {
      this.tdEl = document.getElementById(tdElID);
      this.divEl = document.getElementById(divElID);
    }
    else
    {
      this.tdEl = null;
      this.divEl = null;
    }
  }
}


function PSchedRow_prototype_addCell(id, inputValue, qty, amt, tdElID, divElID, inputEl)
{
  this.cellsIdx[id] = new PSchedCell(id, inputValue, qty, amt, tdElID, divElID, inputEl);
  this.cellsIdx[id].row = this;
  this.cellsIdx[id].colNo = this.cells.length;
  this.cells[this.cells.length] = this.cellsIdx[id];
}
PSchedRow.prototype.addCell = PSchedRow_prototype_addCell;






function PSchedCell(id, inputValue, qty, amt, tdElID, divElID, inputEl)
{
  this.id = id;
  this.inputValue = inputValue;
  this.qty = qty;
  this.amt = parseInt(amt);
  this.row = null;
  this.colNo = 0;
  this.selected = false;
  
  this.inputEl = inputEl;
  
  if (PSched.OK)
  {
    this.tdEl = document.getElementById(tdElID);
    this.tdEl.com_littletype_PSchedCell = this;
    if (this.amt)
    {      
      this.tdEl.onmouseover = PSchedCell.onmouseover;
      this.tdEl.onmouseout = PSchedCell.onmouseout;
      this.tdEl.onclick = PSchedCell.onclick;
    }
    else
    {
      this.tdEl.onmouseover = PSchedCell.nullHandler;
      this.tdEl.onmouseout = PSchedCell.nullHandler;
      this.tdEl.onclick = PSchedCell.nullHandler;
    }

    this.divEl = document.getElementById(divElID);
  }
  else
  {
    this.tdEl = null;
    this.divEl = null;
    this.inputEl.com_littletype_PSchedCell = this;
    this.inputEl.onclick = PSchedCell.onclick();
  }
}


function PSchedCell_onmouseover()
{
  //this.style.backgroundColor = '#C0C0C0';
}
PSchedCell.onmouseover = PSchedCell_onmouseover;


function PSchedCell_onmouseout()
{
  //this.style.backgroundColor = 'white';
}
PSchedCell.onmouseout = PSchedCell_onmouseout;


function PSchedCell_nullHandler()
{
  //this.style.backgroundColor = '#C0C0C0';
}
PSchedCell.nullHandler = PSchedCell_nullHandler;


function PSchedCell_prototype_onclick()
{
  this.row.sched.selectCell(this);
  
  if (this.inputEl.length)
  {
    for (i = 0; i < this.inputEl.length; i++)
    {
      if (this.inputEl[i].value == this.inputValue)
      {
        this.inputEl[i].checked = true;
        this.row.sched.onchange();
        return;
      }
    }
  }
  else
  {
    if (this.inputEl.value == this.inputValue)
    {
      this.inputEl.checked = true;
      this.row.sched.onchange();
      return;
    }
  }
}
PSchedCell.prototype.onclick = PSchedCell_prototype_onclick;

function PSchedCell_onclick()
{
  var cell = this.com_littletype_PSchedCell;
  cell.onclick();
}
PSchedCell.onclick = PSchedCell_onclick;
