// 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);
  
    
  // first cell
  var firstCell = row.insertCell(0);
  var el0 = document.createElement('input');
  el0.type = 'text';
  el0.name = 'sQty' + iteration;
  el0.id = 'sQty' + iteration;
  el0.size = 5;
  firstCell.appendChild(el0);
  
  // second cell
  var secondCell = row.insertCell(1);
  var el1 = document.createElement('input');
  el1.type = 'text';
  el1.name = 'sBrand' + iteration;
  el1.id = 'sBrand' + iteration;
  el1.size = 10; 
  //el1.value = 'sBrand' + iteration;
  //el.onkeypress = keyPressTest;
  secondCell.appendChild(el1);
  
  /*  
  // third cell
  var thirdCell = row.insertCell(2);
  var sel = document.createElement('select');
  sel.name = 'selRow' + iteration;
  sel.options[0] = new Option('text zero', 'value0');
  sel.options[1] = new Option('text one', 'value1');
  cellRightSel.appendChild(sel);
  */
  
   // third cell
  var thirdCell = row.insertCell(2);
  var el2 = document.createElement('input');
  el2.type = 'text';
  el2.name = 'sCode' + iteration;
  el2.id = 'sCode' + iteration;
  el2.size = 5;
  thirdCell.appendChild(el2);
  
  // fourth cell
  var fourthCell = row.insertCell(3);
  var el3 = document.createElement('input');
  el3.type = 'text';
  el3.name = 'sProduct' + iteration;
  el3.id = 'sProduct' + iteration;
  el3.size = 40;
  fourthCell.appendChild(el3);
  
  // qty cell
  //var qtyCell = row.insertCell(4);
  //var textNode = document.createTextNode(iteration);
  //qtyCell.appendChild(textNode);
  
  // fifth cell
  //var fifthCell = row.insertCell(4);
  //var el4 = document.createElement('input');
 // el4.type = 'hidden';
  //el4.name = 'sCount' + iteration;
  //el4.id = 'sCount' + iteration;
 // el4.value = iteration;
  //el4.size = 5;
 // fifthCell.appendChild(el4);   
  
  var counter = document.getElementById('sCount');
  counter.value = iteration;  
  
  //alert(counter.value);
}


/*
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);
}