<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<?
session_start();

include("opendb.php");
include("test.php");

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 Visit 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($sv)
{

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

$phead=getphead($user,$model);
$dhead=getdhead($user,$model);




$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,dhead,phead,ussage,tonnage)"
." values('$inqno','$inqdate','$isource','$refby','$prospect','$model','$rate','$user', '$brcode','$atake','$user1','$ondate','$segment','$dhead','$phead','$ussage','$tonnage')";
mysqli_query($db,$sql) or die('inq:'.mysqli_error());


/* 	<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>    
*/
$sql="insert into followup (inqno, user, brcode, vobj, rmrk, platf, platcrite, nxtobj, nxtobjdt, discus,date,prospect,executive,atake,tfrom,tto,ondate,followdate,dhead,phead,model,rate,ussage,tonnage) values('$inqno', '$user','$brcode',  '$vobj', '$rmrk', '$plat','$refby', 'firstcall', '$nxtobjdt', '$discus','$inqdate','$prospect','$user1','$atake','$tf','$tt','$ondate','$inqdate','$dhead','$phead','$model','$rate','$ussage','$tonnage')";
mysqli_query($db,$sql) or die('Follow:'.mysqli_error());


$sql="update prospmaster set active='inactive' where pcode='$prospect'";
mysqli_query($db,$sql) or die('prosp'.mysqli_error());



$rr=mysqli_query($db,"select mob,pname from prospmaster where pcode='$prospect'");
$rs=mysqli_fetch_row($rr);
$cmobile=$rs[0];
$cus=$rs[1];


$rr=mysqli_query($db,"select ph from bruser where username='$refby' and branch='$brcode'");
$rs=mysqli_fetch_row($rr);
$emobile=$rs[0];


if ($sms=='sms')
{	$mob='0'.$emobile;
        $mesg= ' New Inquiry for '.$model.' Contact.' .$cmobile. ' of Mr. '.$cus.', Inquiry No.' .$inqno. ' updated on '.datetohtml($inqdate). ' meet him on ' .datetohtml($nxtobjdt). ' immediately.';
	$username="psnauto";
	$api_password="afc46op45nftmu1kk";
	$sender="PSNCEP";
	$domain="sms.psnindia.com";
	$priority="11";// 1-Normal,2-Priority,3-Marketing, 8-High Priority
	$method="POST";
	//$message="FUEL HIKE:-".$result1[1].",". $mloc." Date=".$mdate." ".$mtime. " Fuel=".$mfuel." Hike=".$mfuel_hike;
	$parameters="username=$username&api_password=$api_password&sender=$sender&to=$mob&message=$mesg&priority=$priority";
	$url="http://$domain/pushsms.php";
	$ch = curl_init($url);
	if($method=="POST")
	{
		curl_setopt($ch, CURLOPT_POST,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$parameters);
	}
	else
	{
		$get_url=$url."?".$parameters;
		curl_setopt($ch, CURLOPT_POST,0);
		curl_setopt($ch, CURLOPT_URL, $get_url);
	}
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
	curl_setopt($ch, CURLOPT_HEADER,0);  // DO NOT RETURN HTTP HEADERS 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  // RETURN THE CONTENTS OF THE CALL
	$return_val = curl_exec($ch);
	if($return_val=="")
		echo "Process Failed, Please check domain, username and password.";
	else
	{
		$return_val=1;
		echo "Message Sent to ".$parameters;
	}


}
}
}
?>
<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(document.f1.refby.value.length<=0)
 {
  alert("Invalid Executive");
  return false;
 }

 if(document.f1.d2.value.length<=0)
 {
  alert("Invalid date");
  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>
<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="90%">
    <tr>
      <td  align="center" bgcolor="steelblue" colspan=4>
<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>Inquiry No</td>
      <td>
<?
$rr=mysqli_query($db,"select count(*) from inquiry where user='$user' and brcode='$brcode'");
$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>
        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 >inquiry Source</td>
      <td >
<select name='isource'>
<?
$rr=mysqli_query($db,"select * from inqsource  order by source");
while($r=mysqli_fetch_row($rr))
{
 print "<option>$r[1]</option>";
}
?>
</select></td>
      <td  ><font color=red>*</font>Executive</td>
      <td  ><select name='refby'>
<?
$rr=mysqli_query($db,"select * from bruser where branch='$brcode' and level=0 and utype='active' order by username");
while($r=mysqli_fetch_row($rr))
{
 print "<option>$r[1]</option>";
}
?>
</select>
      </td>
    </tr>
    <tr>
      <td >Prospect</td>
      <td ><select name='prospect'> 
<?
$pres=mysqli_query($db,"select pcode,pname from prospmaster where brcode='$brcode' and exec='$user' and active='active' order by pname");
while($prow=mysqli_fetch_array($pres))
{
 print "<option value='$prow[0]'>$prow[1]</option>";
}
?>


      </td>
      <td >Attended By</td>
      <td >
<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  >Vehicle Model</td>
      <td  >
<select name='model'>
<?




$re= mysqli_query($db,"select * from premodel where model in (select maname from saletarget where exec='$user' and brcode='$brcode') order by model");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[model]</option>";
}
?>
</select>
      </td>

    </center>
      <td  >
        Rate</td>
    <center>

      <td  >
        <input type=text name='rate' size="15">
      </td>
    </tr>
<tr><td>Segment </td><td ><select name='segment'>
<?
 $segres=mysqli_query($db,"select * from seg_master order by sname");
 while($segrow=mysqli_fetch_row($segres))
  print "<option>$segrow[1]</option>";
?>
</select></td><td>Tonnage </td><td ><select name='tonnage'>
<?
 $segres=mysqli_query($db,"select * from tonnage order by source");
 while($segrow=mysqli_fetch_row($segres))
  print "<option>$segrow[1]</option>";
?>
</select></td></tr> <tr>       
<td>Usage </td><td ><select name='ussage'>
<?
 $segres=mysqli_query($db,"select * from ussage order by source");
 while($segrow=mysqli_fetch_row($segres))
  print "<option>$segrow[1]</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>Vehicle Description</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 Visit objective </td><td colspan=3>
<input type=text name='nxtobj' size=40>
</td>
</tr>
    <tr>
      <td height="41">Next Visit Date</td>
	<td colspan=2>  
	dd <input type=text name='d2' size="2" value=''>
        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'><select name='sms'>

<option>sms</option>
<option>no</option>
</select></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 * 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>

