<style> 
#paydetstab { margin-left:10px; color:darkslategrey; border: 1px solid #a5a5a5; max-height:100px; max-width:600px; overflow-y:scroll;}
.pdamsjust {float:right; text-align: right; width:50px;}
</style>
<script type="text/javascript">

function suminvams() {                      // on selecting
    var tot = 0; 	var ina = "";  	var paya = "";
    var tp = document.getElementsByName('topay');
	var ip = document.getElementsByName('billno');
    for(var i=0;i<tp.length;i++){ 
        var avwc = tp[i].value;
        var av1 = avwc.replace(/[+-][^0-9.]/g,'');   
        var aa = parseFloat(av1);
        if(aa>0) { tot += aa;
				   paya += tp[i].value + ">";
				   ina += ip[i].value + ">";	
		 }  
        else { tp[i].value = 0;}
     };   
     document.getElementById('am').value = tot;
	 document.getElementById('payarr').value = paya;
	 document.getElementById('invarr').value = ina;
 };
    

</script>
<?php
include 'includes/database.php';
include 'includes/funcs.php';
if (isset($_GET['s'])){
    $db=new MySQLDatabase();
    $db->open_connection();
	$supp = $_GET['s'];
	$rtype = $_GET['r'];
	$qrystr = "SELECT insbills.idate, insbills.billno, CONCAT('BK. No.',insrecs.insno,'- ',insrecs.regno,'-',insrecs.vehmod,' -',insrecs.cname) as nar, insbills.amount FROM insbills, insrecs WHERE insbills.insno = insrecs.insno AND insbills.supp = '$supp' AND insbills.payno=0 ORDER BY insbills.billno"; 
    $res = mysqli_query($db->connection, $qrystr) or die (mysqli_error($db->connection) );
    echo "<table id='paydetstab' cellspacing='4px' align='center'>";
    echo "<th>Bill No.</th> <th>Date</th> <th>Naration</th> <th>Amount</th> <th>To Pay</th>";
	echo '<tr><td></td></tr>';
    while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)){
        $sd = $row['idate']; $nd = substr($sd,8,2)."-".substr($sd,5,2)."-".substr($sd,0,4);
        echo "<tr><td>".$row['billno']."</td> <td>".$nd." <input type='hidden' id='billno' name='billno' value='".$row['billno']."'/></td> <td align='left'>".$row['nar']."</td> <td>".number_format($row['amount'],2)."</td> <td><input class='pdamsjust' type='text' id='topay' name='topay' onchange='suminvams();' value='0'/></td> </tr>";
        }
    echo "</table>";
   // echo '</center></td></tr>';
    $db->close_connection(); 
}
else if (isset($_GET['ro'])){
	$db=new MySQLDatabase();
    $db->open_connection();
	$ro = $_GET['ro'];
	$rtype = $_GET['r'];
	$qrystr = "SELECT repo.idate, insbills.billno, CONCAT(insrecs.insno,'- ',insrecs.regno,'-',insrecs.vehmod,' -',insrecs.cname) as nar, insbills.amount FROM insbills, insrecs WHERE insbills.insno = insrecs.insno AND insbills.supp = '$supp' AND insbills.payno=0 ORDER BY insbills.billno"; 
    $res = mysqli_query($db->connection, $qrystr) or die (mysqli_error($db->connection) );
    echo "<table id='paydetstab' cellspacing='4px' align='center'>";
    echo "<th>Invoice No.</th> <th>Date</th> <th>Naration</th> <th>Amount</th> <th>To Pay</th>";
	echo '<tr><td></td></tr>';
    while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)){
        $sd = $row['idate']; $nd = substr($sd,8,2)."-".substr($sd,5,2)."-".substr($sd,0,4);
        echo "<tr><td>".$row['billno']."</td> <td>".$nd." <input type='hidden' id='billno' name='billno' value='".$row['billno']."'/></td> <td align='left'>".$row['nar']."</td> <td>".$row['amount']."</td> <td><input class='pdamsjust' type='text' id='topay' name='topay' onchange='suminvams();' value='0'/></td> </tr>";
        }
    echo "</table>";
   // echo '</center></td></tr>';
    $db->close_connection(); 
}
else {echo'FAIL';}


?>

