<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if($save)
{
if(strlen($da)<=0||strlen($drv)<=0||strlen(stkm)<=0)
{
 print "<script> alert('Invalid Entry'); history.back(1); </script>";
 return;
}
 mysqli_query($db,"insert into tcr values('$da','$drv','$stkm','$toloc','$locfrom','$brcode')")or die(mysqli_error());

 mysqli_query($db,"update veh_purchase set loc='$toloc' where chasis='$da' and sale_invoice='-' and brcode='$brcode'") or die('Purchase Err:'.mysqli_error());

 mysqli_query($db,"update vehopstock set loc='$toloc' where chno='$da' and brcode='$brcode'") or die(mysqli_error());
log_entry($brcode,$user,$REMOTE_ADDR,"TCR Chasis: $da");
print "<font color=green>Data has been saved</font>";
}

?>
<script>
var loc=[];
function chk()
{
 var no=document.f1.da.selectedIndex;
  if(no>0)
   document.f1.locfrom.value=loc[no];
  else
   document.f1.locfrom.value='';

}
</script>

<link rel="stylesheet" href="../style.css" type="text/css" />
<center>
<form name='f1' action='<?print $PHP_SELF?>'>
<table>
<th>CHASIS <th>Driver <th>Strt KM. <th>To <th>From

<?
$i=1;
$res=mysqli_query($db,"select * from vehopstock where sold='N' and brcode='$brcode' order by pdate")or die(mysqli_error());
print "<tr><td><select name='da' onChange=chk()> <option</option>";
while($row=mysqli_fetch_array($res))
{
 print "\n<option>$row[chno]</option>";

  $sq="select lname from loc_master where code='$row[loc]'";  
 $res1=mysqli_query($db,$sq);
 $loc_row=mysqli_fetch_row($res1);
  print "\n<script> loc[$]='$loc_row[0]' </script>";
  $i++;
}

//**********************************************************
$res=mysqli_query($db,"select * from veh_purchase where sale_invoice='-' and type !='pur-ret' and brcode='$brcode' order by entrydate")or die(mysqli_error());
while($row=mysqli_fetch_array($res))
{
 print "\n<option>$row[chasis]</option>";
 if($row[type]=='trans-in')
  {
 $res2=mysqli_query($db,"select lname from loc_master where code='$row[loc]' and brcode='$brcode'")or die(mysqli_error());
 $loc_row=mysqli_fetch_row($res2);
  print "\n<script>loc[$i]='$loc_row[0]'; </script>";
 $i++;
}
else
 {
 $res2=mysqli_query($db,"select lname from loc_master where code='$row[loc]' and brcode='$brcode'");
 $loc_row=mysqli_fetch_row($res2);
  print "\n<script>loc[$i]='$loc_row[0]'; </script>";
 $i++;
}
}
?>
</select>
<td><select name='drv'>
<option></option>
<?
 $re=mysqli_query($db,"select * from drv_master where brcode='$brcode' order by name");
 while($row=mysqli_fetch_row($re))
  { 
    print "<option value='$row[0]'>$row[1]</option>";
 }
?>
</select></td>
<td><input type=text name='stkm' size=8></td><td>
<select name='toloc'>
<?
 $res2=mysqli_query($db,"select * from loc_master where brcode='$brcode'");
  while($loc=mysqli_fetch_row($res2))
   {
     print "<option value='$loc[0]'>$loc[1]</option>";
  }
?>
</select></td><td><input type=text name='locfrom' size=40></td></tr>
<tr><td colspan=10 align=center><input type=submit name='save' value='SAVE'> <input type=button value='Cancel' onClick=document.location='../blank.html'></td></tr>
</table>
</form>


