// Last updated 2006-02-21
function addRowToTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  

  // Series Name
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode("Series "+(iteration-3));
  cellLeft.appendChild(textNode);
  
  // Shares
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'shares[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
  
  // dollar
  var cellRight = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'amount[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);


  // Month
  var cellRightSel = row.insertCell(3);
  var sel = document.createElement('select');
  sel.name = 'month[]';
  sel.options[0] = new Option('January', '1');
  sel.options[1] = new Option('February', '2');
  sel.options[2] = new Option('March', '3');
  sel.options[3] = new Option('April', '4');
  sel.options[4] = new Option('May', '5');
  sel.options[5] = new Option('June', '6');
  sel.options[6] = new Option('July', '7');
  sel.options[7] = new Option('August', '8');
  sel.options[8] = new Option('September', '9');
  sel.options[9] = new Option('October', '10');
  sel.options[10] = new Option('November', '11');
  sel.options[11] = new Option('December', '12');
  cellRightSel.appendChild(sel);

  // Year
  var cellRight = row.insertCell(4);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'year[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
  
  // Preference x
  var cellRight = row.insertCell(5);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'preference[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);

  // Preference Type
  var cellRightSel = row.insertCell(6);
  var sel = document.createElement('select');
  sel.name = 'preferenceType[]';
  sel.options[0] = new Option('Non-Participating', '0');
  sel.options[1] = new Option('Participating', '1');
  sel.options[2] = new Option('Participating  with Cap: ', '2');
  cellRightSel.appendChild(sel);

  // Cap X
   var cellRight = row.insertCell(7);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'preferenceCap[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);

  //Waterfall
  var cellRight = row.insertCell(8);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'waterfall[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);

   // Dividend Rate
  var cellRight = row.insertCell(9);
  var el = document.createElement('input');
  el.text="0.0";
  el.type = 'text';
  el.name = 'divRate[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
  
  // Dividends
  var cellRightSel = row.insertCell(10);
  var sel = document.createElement('select');
  sel.name = 'selRow' + iteration;
  sel.options[0] = new Option('No Dividends', '0');
  sel.options[1] = new Option('Cumulative', '1');
  sel.options[2] = new Option('Non-Cumulative', '2');
  cellRightSel.appendChild(sel);
}


function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}
function removeRowFromTable()
{
  var tbl = document.getElementById('tblSample');
  var lastRow = tbl.rows.length;
  if (lastRow > 5) tbl.deleteRow(lastRow - 1);
}
function openInNewWindow(frm)
{
  // open a blank window
  var aWindow = window.open('', 'TableAddRowNewWindow',
   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
   
  // set the target to the blank window
  frm.target = 'TableAddRowNewWindow';
  
  // submit
  frm.submit();
}
function validateRow(frm)
{
  var chkb = document.getElementById('chkValidate');
  if (chkb.checked) {
    var tbl = document.getElementById('tblSample');
    var lastRow = tbl.rows.length - 1;
    var i;
    for (i=1; i<=lastRow; i++) {
      var aRow = document.getElementById('txtRow' + i);
      if (aRow.value.length <= 0) {
        alert('Row ' + i + ' is empty');
        return;
      }
    }
  }
  openInNewWindow(frm);
}
