<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
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(bpno) from bnkpay where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
   $bpno=1;
 else   
   $bpno=$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)
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[] onKeyDown=chkey(this) 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[]'>";
//alert(cur);
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)

if(cur2>1)
 var nar=document.getElementById('d2d1').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

var nar=document.f1.dd1.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 onKeyDown=chkey(this) 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='"+document.f1.dragval1.value+"'>";

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

function valid()
{
 calccr();
 calcdr();
submitOK="true";

 if(document.f1.tdamt.value!=document.f1.tcamt.value)
{  
 submitOK="false";
  alert('Dr, Cr Amount mis-match');
}

   
var day1=document.f1.d.value;
var mon1=document.f1.m.value;
var year1=document.f1.y.value;
var chdt=day1+"/"+mon1+"/"+year1;

var tempdt=year1+"-"+mon1+"-"+day1;
var tempact=document.f1.act.value;
var tempacf=document.f1.acf.value;


function ValidateDate() 
{
 
    var msg = '';
 
    this.isDate = function(date) 
     {
 
        var explodeData = date.explode("/");
        var day = explodeData[0];
        var month = explodeData[1];
        var year = explodeData[2];
 
        var thisMonth = new Date(year, month, 0);
        var daysInMonth = thisMonth.getDate();
        var pattern = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
 
        if (!pattern.test(date)) {
            msg = "invalid date format";
        }
        else if (day > daysInMonth) {
            msg = "invalid day";
        }
        else if (month > 12) {
            msg = "invalid month";
        }
 
        if (msg) {
            return false;
        } else {
            return true;
        }
    }
 
    this.getMsg = function() 
    {
 
        return msg;
    }
}

 
 vDate = new ValidateDate();
 
if (vDate.isDate(chdt))

 {

if(tempdt>=tempacf&&tempdt<=tempact)
{

//alert('hii');

}
else
{
submitOK="false";

document.f1.m.focus();

alert('date should be in between current Financial Year');

}

 //alert("Date is valid");
 }
else 

{
 submitOK="false";

document.f1.d.focus();
alert("Date is not valid: " + vDate.getMsg()); 
}


if(submitOK=="true")
{
 x=confirm("Do you want to save?");
 if(x)
   submitOK="true";
 else
    submitOK="false";
}
  if(submitOK=="true")
   return true;
  else
   return false;
}



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

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }
 if(yy.indexOf('"')>-1) 
  {
    alert("\" is not allowed");
    abc.value='';
  }
 if(yy.indexOf('&')>-1) 
  {
    alert("\& is not allowed");
    abc.value='';
  }
}
</script>
<link type="text/css" href="../css/form.css" rel="stylesheet">
</head>
<body>
<center><h3>Bank Payment Number:<?print $bpno?></h3></center>
<form name=f1 action='svbnk_pay.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
<?
//new
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error());
$acrow=mysqli_fetch_row($acpd);


?>
<tr><td><input type=hidden name='acf' value='<?print $acrow[0]?>'></td>
<td><input type=hidden name='act' value='<?print $acrow[1]?>'></td>
</tr>

</table>

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

</body>
<?

$re=mysqli_query($db,"select * from ledger where brcode='$brcode' order by led") 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 * from ledger where brcode='$brcode' and (grp='Bank accounts' or grp='Bank O/d') 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>
