<?
include('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];
include("opendb.php");
$inqno=$_REQUEST['inqno'];
$inqdate=$_REQUEST['inqdate'];
$isource=$_REQUEST['isource'];
$refby=$_REQUEST['refby'];
$prospect=$_REQUEST['prospect'];
$model=$_REQUEST['model'];
$rate=$_REQUEST['rate'];
$atake=$_REQUEST['atake'];
$user1=$_REQUEST['user1'];

$segment=$_REQUEST['segment'];
$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];
$d2=$_REQUEST['d2'];
$m2=$_REQUEST['m2'];
$y2=$_REQUEST['y2'];
$plat=$_REQUEST['platf'];
$platcrite=$_REQUEST['platcrite'];
$nxtobj=$_REQUEST['nxtobj'];
$nxtobjdt=$_REQUEST['nxtobjdt'];
$discus=$_REQUEST['discus'];
$tfh=$_REQUEST['tfh'];
$tfm=$_REQUEST['tfm'];
$tth=$_REQUEST['tth'];
$ttm=$_REQUEST['ttm'];

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

?>
<script>
function validdate()
{
 if(parseInt(document.f1.d2.value)< parseInt(document.f1.cdate.value))
  {
    alert('Invalid next obj date');
    document.f1.d2.value='<?print date('d')?>';
  }
}

 function validtime(x)
 {
  var na=document.getElementById(x).value;
  if(na.length<2 && na<6||parseInt(na)>24)
   {
     alert("Use 24 hour time format");
    document.getElementById(x).value='00';
    document.getElementById(x).focus();

   }
 }
 function validmin(x)
 {
  var na=document.getElementById(x).value;
  if(na.length<2 ||na>=60)
   {
     alert("Invalid Minute, Use 24 hour time format");
    document.getElementById(x).value='00';
    document.getElementById(x).focus();
    return false;
   }

 }

 
</script>
<?

if(isset($_POST['sv']))
{
$dt=date('d')-2;
if($d1<=$dt)
{
print "<script>alert('Invalid Inq. Date'); history.back(1);</script>";
 return;
}
$rr=mysqli_query($db,"select count(*) from inquiry where user='$user'");
$rs=mysqli_fetch_row($rr);
if($rs[0]<=0)
 $inqno=1;
else
 $inqno=($rs[0]+1);
$inqdate=$y1.'-'.$m1.'-'.$d1;
if(!checkdate($m1,$d1,$y1))
{
 print "<script>alert('Invalid Inq. Date'); history.back(1);</script>";
 return;
}

if(!checkdate($m2,$d2,$y2))
{
 print "<script>alert('Invalid Nxt Obj. Date'); history.back(1);</script>";
 return;
}

if(!empty($prospect))
{
/*$tf=$tfh.":".$tfm;
$tt=$tth.":".$ttm;
$sq="select * from followup where date='$inqdate'  and user='$user' and '$tf'>=tfrom and '$tt'<=tto or(tfrom>='$tt')";

$chre=mysqli_query($db,$sq) or die(mysqli_error($db));
if(mysqli_num_rows($chre)>0)
{
 print "<script> alert('Time has already been fixed on the day'); </script>";
 return;
}
*/
$nxtobjdt=$y2.'-'.$m2.'-'.$d2;
$sq="select to_days('$inqdate')-to_days('$nxtobjdt')";
$dtre1=mysqli_query($db,$sq)or die(mysqli_error($db));
$dtrow=mysqli_fetch_row($dtre1);
if($dtrow[0]>=0)
{
 print "<script> alert('Invalid Next followup Date'); history.back(1); </script>";
 return;
}

$tf=$tfh.':'.$tfm;
$tt=$tth.':'.$ttm;
include("presale/validtime.php");
$vd=validate($inqdate,$tf,$tt,$user);
if($vd=='y')
{
 print "<script> alert('Time has already been used'); history.back(1)</script>";
 return;
}

$ondate=date("Y-m-d H:i:s");
$sql="insert into inquiry (inqno,inqdate,isource,refby,prospect,model,rate,user,brcode,atake,exec,ondate,segment)"
." values('$inqno','$inqdate','$isource','$refby','$prospect','$model','$rate','$user', '$brcode','$atake','$user1','$ondate','$segment')";

mysqli_query($db,$sql) or die('inq:'.mysqli_error($db));


/* 	<td>From Hr <input type=text id='tfh' name='tfh' size=2 value='00' onChange='validtime(this.id)'> Min <input type=text name='tfm' size=2 value='00'><br>
            To&nbsp;&nbsp;&nbsp; Hr <input type=text name='tth' id='tth' size=2 value='00' onChange='validtime(this.id)'> Min <input type=text name='ttm' size=2 value='00'>
     </tr>    
*/
$sql1="insert into followup (inqno, user, brcode, vobj, rmrk, platf, platcrite, nxtobj, nxtobjdt, discus,date,prospect,executive,atake,tfrom,tto,ondate,followdate) values('$inqno', '$user','$brcode',  '$vobj', '$rmrk', '$plat','$platcrite', '$nxtobj', '$nxtobjdt', '$discus','$inqdate','$prospect','$user1','$atake','$tf','$tt','$ondate','$inqdate')";


mysqli_query($db,$sql1) or die('Follow:'.mysqli_error($db));

}

}
?>
<html>
<head>
<title>Inquiry Details</title>
<script>
function valid()
{
 if(parseFloat(document.f1.tfh.value)==0)
 {
  alert("Invalid Time");
  return false;
 }
 if(parseFloat(document.f1.tth.value)==0)
 {
  alert("Invalid Time");
  return false;
 }
 if(document.f1.plat.value.length<=1)
 {
  alert("Invalid Platform");
  return false;
 }
 if(document.f1.prospect.value.length<=0)
 {
  alert("Invalid Prospect");
  return false;
 }
  if(confirm('Date and time is very important, check date and time \n Other wise data will be lost \nPress OK to save'))
    return true;
  else
    return false;

}
</script>
<style>
tr{font-family:arial;font-size:13;background-color:white}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<script>
/*
var s1=Array();
var s2=Array();
var s3=Array();
var s4=Array();
var s5=Array();

function getcri()
{
 if(document.f1.platf.selectedIndex==1)
 { 
   document.f1.platcrite.options.length=s1.length;
   for(i=0;i<=s1.length;i++)
   {
	document.f1.platcrite.options[i].text=s1[i];	
	document.f1.platcrite.options[i].value=s1[i];	

   }
 }
 if(document.f1.platf.selectedIndex==2)
 { 
   document.f1.platcrite.options.length=s2.length;
   for(i=0;i<=s2.length;i++)
   {
	document.f1.platcrite.options[i].text=s2[i];	
	document.f1.platcrite.options[i].value=s2[i];	

   }
 }

 if(document.f1.platf.selectedIndex==1)
 { 
   document.f1.platcrite.options.length=s1.length;
   for(i=0;i<=s1.length;i++)
   {
	document.f1.platcrite.options[i].text=s1[i];	
	document.f1.platcrite.options[i].value=s1[i];	

   }
 }

 if(document.f1.platf.selectedIndex==1)
 { 
   document.f1.platcrite.options.length=s1.length;
   for(i=0;i<=s1.length;i++)
   {
	document.f1.platcrite.options[i].text=s1[i];	
	document.f1.platcrite.options[i].value=s1[i];	

   }
 }

}
*/
</script>
<?
/*$re=mysqli_query($db,"select * from pcmaster where plat='s1'");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script> s1[$i]='$row[2]' </script>";
 $i++;
}

$re=mysqli_query($db,"select * from pcmaster where plat='s2'");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script> s2[$i]='$row[2]' </script>";
 $i++;
}

$re=mysqli_query($db,"select * from pcmaster where plat='s3'");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script> s3[$i]='$row[2]' </script>";
 $i++;
}
$re=mysqli_query($db,"select * from pcmaster where plat='s4'");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script> s4[$i]='$row[2]' </script>";
 $i++;
}
$re=mysqli_query($db,"select * from pcmaster where plat='s5'");
$i=0;
while($row=mysqli_fetch_row($re))
{
 print "\n<script> s5[$i]='$row[2]' </script>";
 $i++;
}
*/
?>
<body bgcolor='steelblue' topmargin=0 bottommargin=0>

<form name='f1' method='post' onSubmit='return valid()'>
<input type=hidden name='cdate' value='<?print date('d')?>'>
<table width=90% height=100% align=center cellpadding=2>
<tr><td bgcolor='white'>
  <div align="center">
    <center>
  <table border="0" cellpadding="2" cellspacing="1" bgcolor='#B0C4DE' width="100%">
    <tr>
      <td colspan="9" align="center" width="643" height="37" bgcolor="steelblue">
<font color=white><b>I N Q U I R Y&nbsp;&nbsp; D E&nbsp; T A I L S&nbsp;</td>
    </tr>
    <tr>
      <td height="27" width="120" colspan="2">Inquiry No</td>
      <td height="27" width="161" align="left" colspan="3">
<?
$rr=mysqli_query($db,"select count(*) from inquiry where user='$user'");
$rs=mysqli_fetch_row($rr);
if($rs[0]<=0)
 $inqno=1;
else
 $inqno=$rs[0]+1;
?>
<input type=text name='inqno' size="5" value='<?print $inqno?>'>
      </td>

      <td height="27" width="129" colspan="2">
        <p align="left">Inquiry Date</td>

      <td height="27" >
        dd <input type=text name='d1' size="2" value='<?print date('d')?>'>
        mm <input type=text name='m1' size="2" value='<?print date('m')?>'>
        yy <input type=text name='y1' size="4" value='<?print date('Y')?>'>
      </td>
    </tr>

    <tr>
      <td height="25" width="120" colspan="2">inquiry Source</td>
      <td height="25" width="161" align="left" colspan="3">
<select name='isource'>
<?
$rr1=mysqli_query($db,"select * from inqsource order by source");
while($r=mysqli_fetch_row($rr1))
{
 print "<option>$r[1]</option>";
}
?>
</select></td>
      <td height="25" width="147" colspan="2">Referred By</td>
      <td height="25" width="128" align="left" colspan="2"><input type=text name='refby' size="15">
      </td>
    </tr>
    <tr>
      <td height="27" width="120" colspan="2">Prospect</td>
      <td height="27" width="161" align="left" colspan="3"><select name='prospect'> 
<?
$pres=mysqli_query($db,"select pcode,pname from prospmaster where brcode='$brcode' and exec='$user' and pcode not in(select prospect from inquiry where brcode='$brcode' and exec='$user') order by pname");
while($prow=mysqli_fetch_array($pres))
{
 print "<option value='$prow[0]'>$prow[1]</option>";
}
?>


      </td>
      <td height="27" width="147" colspan="2">Attended By</td>
      <td height="27" width="128" align="left" colspan="2">
<select name='user1'>
<option><?print $user?></option>
<?
/*
$empres=mysqli_query($db,"select empcode,empname from empmaster where design='SALES EXE' and brcode='$brcode' order by empname");
 while($emprow=mysqli_fetch_row($empres))
 {
 print "<option value='$emprow[0]'>$emprow[1]</option>";
 }
*/
?>
</select>
      </td>
    </tr>
    <tr>
      <td height="27" width="120" colspan="2">Vehicle Model</td>
      <td height="27" width="161" align="left" colspan="3">
<select name='model'>
<?
$re= mysqli_query($db,"select model from premodel order by model");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[model]</option>";
}
?>
</select>
      </td>

    </center>
      <td height="27" width="129" colspan="2">
        <p align="left">Rate</td>
    <center>

      <td height="27" width="146" colspan="2">
        <input type=text name='rate' size="15">
      </td>
    </tr>
<tr><td>Segment </td><td colspan=12><select name='segment'>
<?
 $segres=mysqli_query($db,"select segment from seg_master order by sname");
 while($segrow=mysqli_fetch_row($segres))
  print "<option>$segrow[segment]</option>";
?>
</select></td></tr>    
</table>
<br>
<table border=0 width=100%>
<tr><td valign=top><center>
<table border="0" cellpadding="2" cellspacing="1" bgcolor='#B0C4DE'>
    <tr>
      <td  height=25  colspan="9" bgcolor="steelblue">
        <p align="center"><font color=white>O T H E R &nbsp; D E T A I L S</td>
    </tr>
    <tr bgcolor='lightsteelblue'>
      <td  align=center>Visit objective</td>
      <td   align=center>Remark
      </td> </TR>

<tr>
<td align=center>
<input type=text name='vobj'>
</td><td colspan=2><center><input type=text name='rmrk'></td>
</tr>
 <tr>
      <td  align=center>Time Spent 
      </td>
 	<td>From Hr <select name='tfh'>
        <option>00</option>
	<option>6</option>
	<option>7</option>
	<option>8</option>
	<option>9</option>
	<option>10</option>
	<option>11</option>
	<option>12</option>
	<option>13</option>
	<option>14</option>
	<option>15</option>
	<option>16</option>
	<option>17</option>
	<option>18</option>
	<option>19</option>
	<option>20</option>
	<option>21</option>
	<option>22</option>
	<option>23</option>
   </select>

 Min <select name='tfm'>
<option>00</option>
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
<option>35</option>
<option>40</option>
<option>45</option>
<option>50</option>
<option>55</option>
</select>

<br>
            To&nbsp;&nbsp;&nbsp; Hr <select name='tth'>
        <option>00</option>
	<option>6</option>
	<option>7</option>
	<option>8</option>
	<option>9</option>
	<option>10</option>
	<option>11</option>
	<option>12</option>
	<option>13</option>
	<option>14</option>
	<option>15</option>
	<option>16</option>
	<option>17</option>
	<option>18</option>
	<option>19</option>
	<option>20</option>
	<option>21</option>
	<option>22</option>
	<option>23</option>
   </select>

 Min <select name='ttm'>
<option>00</option>
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
<option>35</option>
<option>40</option>
<option>45</option>
<option>50</option>
<option>55</option>
</select>

     </tr>    
    <tr>
      <td height="41"> Discussion</td>
	<td colspan=2><textarea name='discus' cols=39></textarea></td>
    </tr>
    <tr>
      <td height="41"> Action to take</td>
	<td colspan=2><textarea name='atake' cols=39></textarea></td>
    </tr>


<tr>
<td>
Next objective </td><td colspan=3>
<input type=text name='nxtobj' size=40>
</td>
</tr>
    <tr>
      <td height="41">Next Obj Date</td>
	<td colspan=2>  
	dd <input type=text name='d2' size="2" value='<?print date('d')?>'>
        mm <input type=text name='m2' size="2" value='<?print date('m')?>'>
        yy <input type=text name='y2' size="4" value='<?print date('Y')?>'></td>
    </tr>
    
<tr><td colspan=4 bgcolor='steelblue' align=center><input type=submit value='Save' name='sv'></td></tr>
  </table>
</td>
<td valign=top>
 <table border=1>
 <tr><td colspan=2 align=center BGCOLOR='LIGHTSTEELBLUE'>ACTION TAKEN</td></tr>
 <?
  $platres=mysqli_query($db,"select val,criteria from pcmaster where plat='s0' or plat='s1' order by val");
  while($platrow=mysqli_fetch_array($platres))
  {
   print "\n<tr><td><input type=checkbox name=ch[] value='$platrow[val]' onChange='getplat(this,this.value)'></td>";
   print "<td>$platrow[criteria]</td></tr>";
  }
 ?>
 </table>
</td></tr>
</table>
Platform Value: <input type=text name='platf' value=0 readonly>
Platform  <input type=text name='plat' value='S0' readonly>

    </center>
  </div>
</td></tr></table>

</form>

</body>

</html>
<script>
var s=[];
</script>
<?
 $sumres=mysqli_query($db,"select distinct plat,sum(val) from pcmaster  where plat='s0' or plat='s1' group by plat order by val");
   $k=0;
while($sumrow=mysqli_fetch_row($sumres))
{
 print "\n<script>s[$k]=$sumrow[1]</script>";
 $k++;
}
?>
<script>
 function getplat(y,x)
 {
  if(y.checked)
   document.f1.platf.value=parseInt(document.f1.platf.value)+parseInt(x);
  else
   document.f1.platf.value=parseInt(document.f1.platf.value)-parseInt(x);
 var val=parseInt(document.f1.platf.value);

 if(val>=(s[4]+s[3]+s[2]+s[0]+s[1]))
  document.f1.plat.value='S5';
 else if(val>=(s[3]+s[2]+s[0]+s[1]))
  document.f1.plat.value='S4';
 else if(val>=(s[2]+s[0]+s[1]))
  document.f1.plat.value='S2';
 else if(val>=(s[1]+s[0]))
  document.f1.plat.value='S1';
 else if(val>=s[0])
  document.f1.plat.value='S0';
// else
//   document.f1.plat.value='S0'
 }
</script>

