//dynamic form scripts
function addRowToTable()
{
  var tbl = document.getElementById('tblMembers');
  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);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(iteration);
  cellLeft.appendChild(textNode);
  
  // first name cell
  var cellfname = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'gmfname' + iteration;
  el.id = 'gmfname' + iteration;
  el.size = 15;
  
  el.onkeypress = keyPressTest;
  cellfname.appendChild(el);

  // last name cell
  var celllname = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'gmlname' + iteration;
  el.id = 'gmlname' + iteration;
  el.size = 15;
  
  el.onkeypress = keyPressTest;
  celllname.appendChild(el);
  
  // email cell
  var cellemail = row.insertCell(3);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'gmemail' + iteration;
  el.id = 'gmemail' + iteration;
  el.size = 15;
  
  el.onkeypress = keyPressTest;
  cellemail.appendChild(el);
  
  // phone cell
  var cellphone = row.insertCell(4);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'gmphone' + iteration;
  el.id = 'gmphone' + iteration;
  el.size = 15;
  
  el.onkeypress = keyPressTest;
  cellphone.appendChild(el);
  
  // college cell
  var cellcollege = row.insertCell(5);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'gmcollege' + iteration;
  el.id = 'gmcollege' + iteration;
  el.size = 15;
  
  el.onkeypress = keyPressTest;
  cellcollege.appendChild(el);
 
}
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('tblMembers');
  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('tblMembers');
    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);
}
