<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
if($save)
{
$dt=$y.'-'.$m.'-'.$d;

if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}
$re=mysqli_query($db,"select max(formno) from orderform where brcode='$brcode'")or die(mysqli_error());
$r=mysqli_fetch_row($re);
if($r[0]<=0)
 $form_no=1;
else
 $form_no=$r[0]+1;

$sql="insert into orderform  (formno,dt,cust,addr,phone,rname,model,config,seg,fine,exch,descr,exvalue,invalue,disamt,netamt,oinf,brcode,location) values('$form_no', '$dt','$cust', '$addr','$phone', '$rname', '$mcode','$config','$seg', '$fine','$exch','$descr', '$exvalue','$invalue', '$disamt','$netamt','$oinf', '$brcode','$loc')";
mysqli_query($db,$sql) or die (mysqli_error());
}
?>
<script>
var ar1=[];

function valid()
{
var p1=document.f1.mcode.selectedIndex; 
var c=document.f1.cust.value;
var inv=document.f1.invalue.value;
var net=document.f1.netamt.value;

 submit="true";
 if(p1<=0)
  {submit="false"; alert('Invalid Model Code');}
 if(c.length<=0)
  {submit="false"; alert('Invalid Customer Name');}
 if(inv.length<=0 || isNaN(inv)||net.length<=0 ||isNaN(net))
  {submit="false"; alert('Invalid invoice no.');}
 
 if(submit=="false")
  return false;
}


function fun1()
{
 var x=document.f1.config.selectedIndex;
 if(x<=0)
   document.f1.mcode.value='';
 else
 document.f1.mcode.value=ar1[document.f1.config.selectedIndex];
}
</script>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">


<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr><td bgcolor='green' align=center> <font color=ivory><b>ORDER FORM</td></tr>
<tr><td bgcolor='ivory' align=center>
<table border=0>
<tr><td>Order form number <td>
<?
$re=mysqli_query($db,"select max(formno) from orderform where brcode='$brcode'")or die(mysqli_error());
$r=mysqli_fetch_row($re);
if($r[0]<=0)
 $form_no=1;
else
 $form_no=$r[0]+1;
?>
<input type=text name='formno' readonly size=10 value='<?print $form_no?>'> 
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())?>'>
</td></tr>
<tr><td><font color='red'> Customer Name</td><td colspan=3><input type=text name='cust' size=50></td></tr>
<tr><td>Address</td><td colspan=3><textarea name='addr' cols=48></textarea></td></tr>
<tr><td>Phone</td><td colspan=3><input type=text name='phone' size=50></td></tr>

<tr><td>Rep.Name</td><td colspan=3><select name='rname'>
<?
 $result=mysqli_query($db,"select * from rep_master where branch='$brcode' order by repname");
 while($row=mysqli_fetch_row($result))
 {
   print "<option value='$row[0]'>".strtoupper($row[1])."</option>";
 }
?>
</select>
District
<select name='loc'>
<?
 $result=mysqli_query($db,"select * from dist_master  order by dist")or die(mysqli_error());
 while($row=mysqli_fetch_row($result))
 {
   print "<option value='$row[0]'>".strtoupper($row[1])."</option>";
 }
?>
</select>
</td></tr>
<tr><td><font color=red> Config</td>
<td colspan=3>

 <select name='config' onChange=fun1()> 
<option></option>
    <?
     $i=1;
     $res=mysqli_query($db,"select * from veh_master");
     while($row=mysqli_fetch_row($res))
     {
       print "\n<option>$row[2]</option>"; 
       print "\n<script>ar1[$i]='".strtoupper($row[1])."'</script>";$i++;
     }
    ?>
  </select>
</td></tr>
<tr><td>Model Code</td><td>
<input type=text name='mcode'>


 

</td>

</tr>
<tr><td>Segment</td><td><select name='seg'>
<?
 $result=mysqli_query($db,"select * from seg_master order by sname");
 while($row=mysqli_fetch_row($result))
 {
   print "<option value='$row[0]'>".strtoupper($row[1])."</option>";
 }
?>
</select>
</td></tr>


<tr><td>Financier/Bank</td><td colspan=3><select name='fine'>
<?
 $result=mysqli_query($db,"select * from fin_master order by finname");
 while($row=mysqli_fetch_row($result))
 {
   print "<option value='$row[0]'>".strtoupper($row[1])."</option>";
 }
?>
</select>
</td></tr>

<tr><td>Exchange (y/n)</td><td colspan=3><select name ='exch'><option>N</option><option>Y</option></select></td></tr>
<tr><td>Exchange Description </td><td colspan=3><textarea name='descr' cols=48></textarea></td></tr>
<tr><td>Exchange Value</td><td colspan=1><input type=text name='exvalue' size=10 style={text-align:right}>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color=red>Invoice Value <input type=text name='invalue' size=10 style={text-align:right}></td></tr>
<tr><td>Discount Amt.</td><td colspan=3><input type=text name='disamt' size=10 style={text-align:right}>
<font color=red>Net Amount to customer</font> <input type=text name='netamt' size=10 style={text-align:right}></td></tr>

<tr><td>Other Information</td><td colspan=3><textarea name='oinf' cols=50 rows=5></textarea></td></tr>
<tr><td colspan=2 align=center bgcolor=green> <input type=submit value='Save' name='save'>
<input type=submit value='Cancel' onClick=document.location='../blank.html'></td></tr>
</table>
</form>



