<style> 
#recpdetstab { 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('invno');
    for(var i=0;i<tp.length;i++){ 
        var avwc = tp[i].value;
     //   var av1 = avwc.replace(/[^0-9.]/g,'');   
        var aa = parseFloat(avwc);
        if(aa !== 0) { tot += aa;
				   paya += tp[i].value + ">";
				   ina += ip[i].value + ">";	
		 }  
     };   
     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['r'])){
    $db=new MySQLDatabase();
    $db->open_connection();
	$cname = $_GET['c'];
	$rtype = $_GET['r'];
	if( $rtype === "INSURANCE"){ $qrystr = "SELECT insinvoices.idate, insinvoices.invno, CONCAT(insrecs.insno,' - ',insrecs.regno,'-',insrecs.vehmod,' -',insrecs.insurer) as nar, IF(invoicecredit=1,insinvoices.amount,-1*insinvoices.amount) as amount FROM insinvoices, insrecs WHERE insinvoices.insno = insrecs.insno AND insinvoices.cname = '$cname' AND insinvoices.recpno=0
	UNION SELECT insinvoices.idate, insinvoices.invno, 'CREDIT' as nar, -1*insinvoices.amount FROM insinvoices WHERE insinvoices.invoicecredit=0 AND insinvoices.cname = '$cname' AND insinvoices.recpno=0;;"; 
	    }
	elseif( $rtype === "TRACKER") { $qrystr = "SELECT trackinvoices.idate, trackinvoices.invno, CONCAT(jcards.jcno,'- ',jcards.regno,'-',jcards.vehmod) as nar, trackinvoices.amount FROM trackinvoices, jcards WHERE trackinvoices.invno = jcards.invno AND trackinvoices.cname = '$cname' AND trackinvoices.recpno=0 
	UNION SELECT trackinvoices.idate, trackinvoices.invno, 'CREDIT' as nar, -1*trackinvoices.amount FROM trackinvoices WHERE trackinvoices.invoicecredit=0 AND trackinvoices.cname = '$cname' AND trackinvoices.recpno=0;"; 
		}
	elseif( $rtype === "REPOSESSION") { $qrystr = "SELECT repoinvoices.idate, repoinvoices.invno, repoinvoices.nar as nar, repoinvoices.amount FROM repoinvoices WHERE repoinvoices.cname = '$cname' AND repoinvoices.recpno=0 
	UNION SELECT repoinvoices.idate, repoinvoices.invno, 'CREDIT' as nar, repoinvoices.amount FROM repoinvoices WHERE repoinvoices.invoicecredit=0 AND repoinvoices.cname = '$cname' AND repoinvoices.recpno=0;"; 
		}	
	//echo $qrystr;	
    $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['invno']."</td> <td>".$nd." <input type='hidden' id='invno' name='invno' value='".$row['invno']."'/></td> <td align='left'>".$row['nar']."</td> <td>".$row['amount']."</td> <td><input class='pdamsjust' type='text' id='topay' name='topay' onchange='suminvams();' size='5' value='0'/></td> </tr>";
        }
    echo "</table>";
   // echo '</center></td></tr>';
    $db->close_connection(); 
}
else {echo'FAIL';}


?>

