<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../../opendb.php");
$rs=mysqli_query($db,"select max(brno) from bnkrec where brcode='$brcode'")or die(mysqli_error());
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
   $brno=1;
 else   
   $brno=$cr_row[0]+1;
?>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:c1d799}
.mnu{margin-left:5}
body{BACKGROUND-COLOR:fcfff1}
</style>
<html>
<head>
<script type="text/javascript">
var tq=0,tq2=0;
var cur=1;
var vv;
var drs=[];
var crs=[];
function setval(a)
{
 vv=a.name;
}
function insRow()
{
var x=document.getElementById('myTable').insertRow(cur);

if(cur>1)
 var nar=document.getElementById('dd1').value;
var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var z1=x.insertCell(3)	
var ag=x.insertCell(4)
var agval=x.insertCell(5)
var dr="dr";
a.innerHTML=cur
var value="<select name=dr[] onClick=setval(this)>";

for(k=0;k<drs.length;k++)
 value=value+"<option>"+drs[k]+"</option>";

value=value+"</select>";
y.innerHTML=value

z.innerHTML="<input type=text name=dr_amt[] id=dr"+cur+" size=10 style={text-align:right}>"
z1.innerHTML="<input type=text id='dd"+cur+"' name=dr_narr[] size=30 value="+nar+">"

ag.innerHTML="<select name='dragain[]' onChange=getda(this,cur,'dr')><option>CHEQ</option><option>DA</option></select>";
agval.innerHTML="<input type=text id='dragval"+cur+"' name='dragval[]'>";

cur++; 

}
function delRow(x1)
{
 var num=x1.name;
 if(cur>1)
  {
    var x=document.getElementById('myTable').deleteRow(cur-1)
    cur--;
  }
}

//************************************
function calcdr()
{
var tq=0;
for(i=1;i<cur;i++)
{
 var ele='dr'+i; //alert(ele);
 if(document.getElementById(ele).value>0)
  {
   amount=document.getElementById(ele).value;
   tq+=parseFloat(amount);
  }
 }
document.f1.tdamt.value=tq.toFixed(2);
}

function calccr()
{
var tq2=0;
for(i=1;i<cur2;i++)
{
 var ele2='cr'+i; //alert(ele);
 if(document.getElementById(ele2).value>0)
  {
   amount=document.getElementById(ele2).value;
   tq2+=parseFloat(amount);
  }
 }
document.f1.tcamt.value=tq2.toFixed(2);
}

//-------*888888888888888888888888*********************8888
var cur2=1;
var cr="cr";
function insRow2()
{
var x=document.getElementById('myTable2').insertRow(cur2)
var cr="cr";
if(cur2>1)
 var nar=document.getElementById('d2d1').value;
else
 {
  var nar=document.getElementById('dd1').value;
  var ag_val=document.getElementById('dragval1').value;
 }
var a=x.insertCell(0)
var y=x.insertCell(1)
var z=x.insertCell(2)	
var z1=x.insertCell(3)	

var crag=x.insertCell(4)
var cragval1=x.insertCell(5)

a.innerHTML=cur2
var value="<select name=cr[] onClick=setval(this)>";
for(k=0;k<crs.length;k++)
 value=value+"<option>"+crs[k]+"</option>";
value=value+"</select>";
y.innerHTML=value

z.innerHTML="<input type=text name=cr_amt[] id=cr"+cur2+" size=10>"
z1.innerHTML="<input type=text id='d2d"+cur2+"' name=cr_narr[] size=30 value='"+nar+"'>"

crag.innerHTML="<select name='cragain[]' onChange=getda(this,cur2,cr)><option>CHEQ</option><option>DA</option> </select>";
cragval1.innerHTML="<input type=text id='cragval"+cur2+"' name='cragval[]' value='"+ag_val+"'>";

cur2++;
}
function delRow2(x1)
{
 var num=x1.name;
 if(cur2>1)
 {
   var x=document.getElementById('myTable2').deleteRow(cur2-1)
   cur2--;
 }
}

function getda(x,y,drcr)
{
if(x.selectedIndex==1)
 window.open('getda.php?no='+y+'&drcr='+drcr);
}


function valid()
{
 calccr();
 calcdr();
var d1=parseFoloat(document.f1.d.value);
 var m1=parseFoloat(document.f1.m.value);
 var y1=parseFoloat(document.f1.y.value);

 if(d1<=0||m1<=0||y1<=0)
 {
  alert('Invalid Date');
  return false;
 }
 if(document.f1.tdamt.value==document.f1.tcamt.value)
   return true;
 else
   {alert('Dr, Cr Amount mis-match'); return false;}
}
</script>
</head>
<body>
<center><h3>Bank Receipt Number:<?print $brno?></h3></center>
<form name='f1' action='svbnk.php' method='post' onSubmit="return valid()">
Debit <input type="button" onclick="insRow()" value=" + ">
<input type='button' onclick='delRow(this)' value=' - '>
<input type='button' onclick='calcdr()' value=' CALCULATE DEBIT SUM  '>
<p align=center style={background-color:silver}>

Date:dd <input type=text name='d' size='2' value='<?print date("d",time());?>'>
mm <input type=text name='m' size='2' value='<?print date("m",time());?>'>
yy <input type=text name='y' size='4' value='<?print date("Y",time());?>'>
</p>
<table id="myTable" border="1">
<th>No.<th>Dr <th>Amt <th>Narr<th>Against <th>Cheq. No.
<tr>
<!--
<td><input type=text name=dr[] onClick=setval()></td>
<td><input type=text name=dr_amt[]></td>
-->
</tr>
</table><br>
Total Debit Amount <input type=text readonly name='tdamt'>
<hr>

Credit <input type="button" onclick="insRow2()" value=" + ">
<input type='button' onclick='delRow2(this)' value=' - '>
<input type='button' onclick='calccr()' value=' CALCULATE CREDIT SUM  '>

<br>&nbsp;

<table id="myTable2" border="1">
<th>No.<th>Cr <th>Amt <th>Narr<th>Against <th>Value
</table>

<br>
Total Credit Amount <input type=text readonly name='tcamt'>
<Hr><input type=submit value='    SAVE BANK RECEIPT    '>
</form>

</body>
<?

$re=mysqli_query($db,"select * from ledger where (grp='Bank accounts' or grp='Bank O/d')and brcode='$brcode' ") or die(mysqli_error());
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script>drs[$i]='$row[0]' </script>";
 $i++;
}

$re=mysqli_query($db,"select distinct led from ledger where brcode='$brcode' order by led") or die(mysqli_error());
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script>crs[$i]='$row[0]' </script>";
 $i++;
}

?>
</html>
