function addRowToTable()
{
// alert(document.getElementById('tblSample').getElementsByTagName('tr').length);
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's noheader row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var iteration1 = parseInt(lastRow+1);
alert(iteration1);
//Validate for empty check
// var iteration = parseFloat(iteration-1);
for(var i=3;i<=iteration1;i++)
{
if($('debit'+i)!=null)
{
if($('debit'+i).value.trim() == '' && $('credit'+i).value.trim() != '')
{
$('debit'+i).readOnly = true;
$('credit'+i).readOnly = true;
$('dledger'+i).disabled = true;
}
else if($('credit'+i).value.trim() == '' && $('debit'+i).value.trim() != '')
{
$('credit'+i).readOnly = true;
$('debit'+i).readOnly = true;
$('dledger'+i).disabled = true;
}
}
}
var debitTotal = 0;
var creditTotal = 0;
if($('debit'+iteration).value.trim() == '' && $('credit'+iteration).value.trim() == '')
{
alert('Please enter debit/credit value');
return false;
}
if($('debit'+iteration).value != '' && $('credit'+iteration).value != '')
{
alert('Please enter only debit/credit value');
return false;
}
for(var i=3;i<=iteration1;i++)
{
if($('debit'+i).value != ''){
debitTotal = parseFloat(debitTotal) + parseFloat($('debit'+i).value);
}
if($('credit'+i).value != ''){
creditTotal = parseFloat(creditTotal) + parseFloat($('credit'+i).value);
}
}
$('tdebit').value = debitTotal;
$('tcredit').value = creditTotal;
document.getElementById('counter').value=iteration-3;
var row = tbl.insertRow(lastRow);
row.id = 'tr_'+iteration1;
// left cell
// cellRightSel.appendChild(sel);
// right cell
var cellRightSel = row.insertCell(0);
var sel = document.createElement('select');
sel.id ='dledger' + iteration1;
sel.name ='dledger' + iteration1;
cellRightSel.align = 'center';
sel.style.width = 125;
//sel.size = 100;
<?
$userid = $_SESSION['userid'];
$sql="SELECT * from 0_ledger where group_id not in(1,7) and user_id = $userid";
$result=mysql_query($sql)or die('Query error:'.''.mysql_errno());
$counter=0;
while($row=mysql_fetch_array($result))
{?>
sel.options[<?= $counter; ?>] = new Option('<?= $row['ledgerName']?>', '<?=$row['ledger_id']?>');
<? $counter = $counter + 1;
}?>
cellRightSel.appendChild(sel);
var cellRight = row.insertCell(1);
cellRight.align = 'center';
var el = document.createElement('input');
el.type = 'text';
el.name = 'debit' + iteration1;
el.id = 'debit' + iteration1;
el.size = 20;
el.maxLength = 8;
cellRight.appendChild(el);
/* var cellRightSel = 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); */
var cellRight2 = row.insertCell(2);
cellRight2.align='center';
var el2 = document.createElement('input');
el2.type = 'text';
el2.name = 'credit' + iteration1;
el2.id = 'credit' + iteration1;
el2.size = 20;
el2.maxLength = 8;
cellRight2.appendChild(el2);
var cellLeft = row.insertCell(3);
cellLeft.align = 'center';
cellLeft.id = 'td'+iteration1;
var chbk1 = document.createElement('span');
chbk1.id = 'change'+iteration1;
var chbk = document.createElement('input');
chbk.type = 'button';
chbk.name = 'additem'+iteration1;
chbk.id = 'additem'+iteration1;
chbk.className = 'ajaxsubmit';
chbk.value = 'Add Item';
//chbk.disabled = true;
//.onClick = function(){addRowToTable();};
chbk.onclick = function(){addRowToTable1();change();};
//chbk.setAttribute('onClick','addRowToTable1();change()');
cellLeft.appendChild(chbk1);
cellLeft.appendChild(chbk);
}