// JavaScript Document
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);
  
  // cell 1
  /*var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration);
  cellLeft.appendChild(textNode);*/
  
  // cell 2
  var cellRight = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'txtRowNum' + iteration;
  el.id = 'txtRowNum' + iteration;
  el.size = 5;
 // el.class = 'bodytext';
  
  el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
  
  // cell 3
  var cellRight2 = row.insertCell(1);
  var el2 = document.createElement('input');
  el2.type = 'text';
  el2.name = 'txtRowName' + iteration;
  el2.id = 'txtRowName' + iteration;
  el2.size = 40;
  //el.class = 'bodytext';
  
  el2.onkeypress = keyPressTest;
  cellRight2.appendChild(el2);
  
  // select cell
  /*var cellRightSel = row.insertCell(3);
  var sel = document.createElement('select');
  sel.name = 'txtRow' + iteration;
  sel.options[0] = new Option('Nike', 'Nike');
  sel.options[1] = new Option('Prostar', 'Prostar');
  sel.options[2] = new Option('Adidas', 'Adidas');
  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 > 2) 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);
}
