<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Invalid User'); parent.document.location='../../index.html' </script>";
 return;
}
include_once('../top.php'); 
header("Cache-Control: max-age=300, must-revalidate"); 
include("../opendb.php");
include("../test.php");
$jvno=$_REQUEST['jvno'];
$dt=$_REQUEST['dt'];
?>
<style>
 td{font-family:arial;font-size:14;color:black;}
 th{font-family:arial;font-size:14;color:black;}
 
 input
  {border-style:ridge;
   border-color:#22437f;
   
   height:25px;
   border-size=1;
   font-family:sans-serif;
   color:BLACK;
  }
</style>
<title>AMPL</title>  
    <script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style1.css" rel="stylesheet" />
	<link type="text/css" href="../css/form.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<body background="../back_ground.jpg">

<script type="text/javascript">
var tq=0,tq2=0;
var cur; //=1;
var vv;
var drs=[];
function setval(a)
{
 vv=a.name;
}
function insRow()
{ 
nar=''
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 yy=x.insertCell(6)

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>"
z1.innerHTML="<input type=text id='dd"+cur+"' name=dr_narr[] size=30 value="+nar+">"

ag.innerHTML="<select name='dragain[]'><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select>";
agval.innerHTML="<input type=text name='dragval[]'>";

var value="<select name=drbr[] onClick=setval(this)>";

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

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


//alert(cur);
cur++; 

}
function delRow(x1)
{ 
 var num=x1.name;
 if(cur>=1)
  {
    var x=document.getElementById('myTable').deleteRow(cur-1)
  //  var x=document.getElementById('myTable').deleteRow(cur)
    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;
}

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;
}

//-------*888888888888888888888888*********************8888
var cur2=0;
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)
var yy=x.insertCell(6)


a.innerHTML=(cur2+1)
var value="<select name=cr[] 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=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[]'><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select>";
cragval1.innerHTML="<input type=text name='cragval[]'>";

var value="<select name=crbr[] onClick=setval(this)>";
for(k=0;k<drs.length;k++)
 value=value+"<option>"+branches[k]+"</option>";
value=value+"</select>";

yy.innerHTML=value


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

function valid()
{
 calccr();
 calcdr();
 if(document.f1.tdamt.value==document.f1.tcamt.value)
   return true;
 else
   {alert('Dr, Cr Amount mis-match'); return false;}
}
</script>
<font color=red> Journal No: <?print $jvno?></font>
<form name=f1 action='updjour_new.php' method='post' onSubmit="return valid()">
<font color=red>Enter password <input type=password name='pswd'></font>
<br>
<input type=hidden name='jvno' value='<?print $jvno?>'>
<input type=hidden name='dt' value='<?print $dt?>'>

Debit <input type="button" onclick="insRow()" value=" + ">
<input type='button' onclick='delRow(this)' value=' - '>
<input type='button' onclick='calcdr()' value=' CALCULATE DEBIT SUM  '>
<input type=text name='tdamt'>
<br>&nbsp;
<table id="myTable" border="1">
<?
$req=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand' order by led");
$i=0;
while($rowq=mysqli_fetch_row($req))
{
 print "\n<script>drs[$i]='$rowq[0]' </script>";
 $i++;
}

$re=mysqli_query($db,"select brname from branch  order by brname");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script>branches[$i]='$row[0]' </script>";
 $i++;
}


$re2=mysqli_query($db,"select * from journal2 where jvno='$jvno' and type='Dr' and brcode='$brcode'") or die(mysqli_error($db));
print "<script> cur=".mysqli_num_rows($re2)."; </script>";
 print "<tr><td>Date </td><td colspan=5><input type=text name='dt' value='".datetohtml($dt)."'></td></tr>";
 print "<th>Type <th>Particulars <th> Amount <th>Narr<th>Ref <th>No<th>Branch";
while($row=mysqli_fetch_array($re2))
{
 print "<tr><td><select name='type[]'><option>Dr</option><option>Cr</option></select>";
 print "<td><select name='dr[]'><option>$row[partic]</option>";
$re=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand'  order by led");
$i=0;
while($row1=mysqli_fetch_array($re))
{
  print "\n<option>$row1[led]</option>";
}
 print "</select></td><td><input type=text name='dr_amt[]' value='$row[dramt]' size=12></td>";
 print "<td><input type=text name='dr_narr[]' value='$row[narr]' size=18></td>";
 print "<td><select name='dragain[]'><option>$row[against]</option><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select></td>";  
 print "<td><input type=text name='dragval[]' value='$row[no]' size=10></td>";
 
  print "<td><select name='drbr[]'><option>$row[tr_no]</option>";
$re=mysqli_query($db,"select brname from branch order by brname");
$i=0;
while($row1=mysqli_fetch_array($re))
{
  print "\n<option>$row1[brname]</option>";
}

}
print "</tr></table>";
?>
<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">
<?
$re2=mysqli_query($db,"select * from journal2 where jvno='$jvno' and type='Cr' and brcode='$brcode'") or die(mysqli_error($db));
while($row=mysqli_fetch_array($re2))
{
 print "<tr><td><select name='type[]'><option>Cr</option><option>Dr</option></select>";
 print "<td><select name='cr[]'><option>$row[partic]</option>";
$re=mysqli_query($db,"select * from ledger where brcode='$brcode' and grp!='Cash in hand'  order by led");
$i=0;
while($row1=mysqli_fetch_array($re))
{
  print "\n<option>$row1[led]</option>";
}
 print "</select></td>";
 print "\n<td><input type=text name='cr_amt[]' value='$row[cramt]' size=12></td>";
 print " <td><input type=text name='cr_narr[]' value='$row[narr]' size=18></td>";

 print "\n<td><select name='cragain[]'><option>$row[against]</option><option>OFNO</option><option>DA</option><option> ON Acc </option> <option>CHEQ</option></select></td>";  
 print "\n<td><input type=text name='cragval[]' value='$row[no]' size=10></td>";
 
   print "<td><select name='crbr[]'><option>$row[tr_no]</option>";
$re=mysqli_query($db,"select brname from branch order by brname");
$i=0;
while($row1=mysqli_fetch_array($re))
{
  print "\n<option>$row1[brname]</option>";
}
 
}
print "</tr></table>";
?>
<p style={background-color:silver}>
<input type=submit value='UPDATE'>


<?
echo "<a href=\"javascript:window.history.go(-1)\">GO BACK</a>";
?>

</P>


</form>

<script type="text/javascript">
function prvcbookpag()
{
var part=document.form1.partynm.value;
var df=document.form1.df.value;
var dtnw=document.form1.dt.value;
var sn=document.form1.sea.value;
document.location="../vmledger-test.php?cd="+part+"&&shvl="+sn+"&&df="+df+"&&dt="+dtnw,true;
}
</script>