<?php

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
include("config.inc.php");
include("function.inc.php");



$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

//User Access
$querylog=" select ifNull(FI_PlaceApp,0) As FI_PlaceApp, ifNull(FI_PlaceDel,0) As FI_PlaceDel,".
          "ifNull(FI_PlaceMod,0) As FI_PlaceMod from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//echo $querylog;
//Read From Setup Table							
$querysetup= " select grid_color1, grid_color2,maxperpage,base1,base2,DecimalBase,DecimalBase1,DecimalBase2,maxperpage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}



$querylog="SELECT FI_Place as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}
	
	
if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	
	

//Access Mode On Insert/Edit/Delete
$InsMode = '';
if ($FI_PlaceApp == 0)
	$InsMode = ' disabled ';


if(!isset($actionmode))
$actionmode='';


if($mode=='editmode'){
	
$query_edit="select Place_id,fromPlace_name ,toplace_name,place_millage,place_millagetype, place_tripAllowance, place_tripTypeId, place_usageLtrs, place_estimationDays,
			place_ConsumptionPerDist, place_Consumption, place_ChargesRate 
			from Place
			where Place_Id='".$Place_Id."'";
						  
//echo $query_edit;exit;
	$resultedit = mysql_query($query_edit, $connection);
	while($row=mysql_fetch_array($resultedit)){
		$Place_Id			= $row[0];
		$fromplace_name		= $row[1];
		$toplace_name		= $row[2];
		$place_millage		= $row[3];
		$place_millagetype	= $row[4];
		$tripAllowance		= $row[5];
		$tripType			= $row[6];
		$usageLtrs			= $row[7];
		$estimationDays		= $row[8];
		$consumptionPerDist	= $row[9];
		$consumption		= $row[10];
		$chargeRate			= $row[11];
		
	}
	$mode='edit';
}

 


if($mode==''){
$status='disabled';
$dis='disabled';

}

if($mode=='view'){

$status='readonly';
$dis='disabled';

}

if($mode=='insert')
$status='';


print <<<HERE

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">

<script type="text/javascript" language="javascript">

function AddPlace () {
	ClearForm('All');
	document.Place.mode.value='insert';
	document.Place.action="Form_Place.php";
	document.Place.submit();
}

function SubmitRecord() {
	if (document.Place.fromplace_name.value=='' )
		{
			alert('Unable to save : check missing field Name ');
			document.getElementById("fromplace_name").focus();
		}		
		else {
			if(document.Place.Place_Id.value == '')
				document.Place.mode.value = 'insert';
			else 
				document.Place.mode.value = 'edit';	

			document.Place.action="Form_Place_Actions.php";
			document.Place.submit();
		
		}


}


function ClearPage(){
	let x = document.Place;
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		x.actionmode.value='';
		x.message.value='';
		x.mode.value='';
		x.action='Form_Place.php';
		x.submit();
	}	
}

//All= All form; D= Detail Part
function ClearForm(Option){
    let x = document.Place;
	x.Place_Id.value='';
	x.fromplace_name.value='';
	x.toplace_name.value='';
	x.place_millage.value='';
	x.place_millagetype.value='';
	x.tripAllowance.value='';
	x.tripType.value='';
	x.usageLtrs.value='';
	x.estimationDays.value='';
	x.consumptionPerDist.value='';
	x.consumption.value='';
	x.chargeRate.value='';	
}


function clearform()
{
  document.Place.PRef.value='';
  document.Place.action='Form_Place.php';
  document.Place.submit();
}

function LoadForm() 
{
	x=document.Place;
	x.PRef.focus();
	

}

function SetNextPage(startrange,perpage){
	document.Place.action='Form_Place.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.Place.submit();
}

function SetPrvPage(startrange,perpage){
	document.Place.action='Form_Place.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.Place.submit();
}

function set_limit(){

	perpage=document.Place.perpage.value;
	document.Place.action='Form_Place.php?perpage='+perpage;
	document.Place.submit();
}



function confirm_delete(PlaceId) {
	if (confirm ( "Are you sure you want to Delete this Record ?"))
	{
		document.Place.Place_Id.value=PlaceId;
		document.Place.mode.value='delete';
		document.Place.action="Form_Place_Actions.php";
		document.Place.submit();
	}
	
	
							
}


function SetWh(whId){
	
	document.Place.Place_Id.value=whId;
	document.Place.message.value='';
	document.Place.actionmode.value='';
	
	document.Place.mode.value='editmode';
	document.Place.submit();
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Place</title>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style15 {color: #5151FF; font-size: 10px;}
.style26 {color: #FF0000}
.style28 {color: #FFFFFF ;font-size: 10px;}

/* Hide HTML5 Up and Down arrows. */
input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"] {
-moz-appearance: textfield;
}

</style>
</head>
<body >
<form name='Place' method='post'>
<input type=hidden name='mode' value='$mode'>
<input type=hidden name='actionmode' value='$mode'>
<input type=hidden name='message' value='$message'>
<input type=hidden name='Place_Id' value='$Place_Id'>

HERE;


if($mode==''){
$status='disabled';
$dis='disabled';

}

if($mode=='view'){

$status='readonly';
$dis='disabled';

}

if($mode=='insert')
$status='';

//$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Place</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

	 
	
echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px'>";
echo"		<button type='button' id='btnClear' name='btnClear' onclick='ClearPage(); return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";


echo"<button type='button' id='btnPost' name='btnPost' onclick='AddPlace(); return false;' ";
if($mode=='insert' or $mode=='edit')	echo "disabled ";
echo" title='Add Place' $Insmode><span><span>Add Place</span></span></button>";
	
echo"<button type='button' name='btnPost' id='btnPost'  ".($mode==''?'disabled':'')." onclick='SubmitRecord();' $style	title='Save Changes' ><span><span>Save</span></span></button>";


				
echo"   </td><td align='center' width='260px'>
	
	<input type='text' name='PRef' id='PRef' value='$PRef' size='13' maxlength='30'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
	<button type='submit' name='Submit' value='Find' /><span><span>Find</span></span></button>
	<button type='button' name='btnCancel' value='Cancel' onclick='clearform();'/>
	<span><span>Cancel</span></span></button>
	</td>
	
	</td><td align='left'><span class='style1'><b><font size=1>Results/page</b></span>
	 <span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
	 
	if ($startrange=='')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
		
	for($i=10;$i<=$maxperpage;$i=$i+10){
		if($i==$perpage)
			echo "<option value='$i'  selected>$i</option>";
		else echo "<option value='$i'>$i</option>";
		
	} 
	 echo"</td></tr>";
echo"	</table>";
echo"</td></tr>";

echo"<tr><td id='content-body'>";
echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";

echo"   <tr bgcolor='#F7F7F7'><td valign='top'>";	 

	  
echo"<table border='0' style='table-layout:fixed' align='left' valign='top'>
      <col width=120px><col width=220px><col width=120px><col width=220px><col width=120px><col width=220px>";
echo"<tr bgcolor='#F7F7F7'>
		<td valign='center' align='left' ><span class='style1'>From Place&nbsp;<span class='style26'>*</span></span></td>
		<td><input type='text' name='fromplace_name' value='".htmlspecialchars($fromplace_name, ENT_QUOTES)."' style='font-size:12px; width:200px; height:18px;'  $status maxlength='100' /></td>
		<td valign='center' align='left' ><span class='style1'>To Place&nbsp;<span class='style26'></span></span></td>
		<td><input type='text' name='toplace_name' value='".htmlspecialchars($toplace_name, ENT_QUOTES)."' style='font-size:12px; width:200px; height:18px;'  $status maxlength='100' /></td>
	    <td valign='center' align='left' ><span class='style1'>Trip Distance<span class='style26'></span></span></td>";

if ($place_millagetype=='Hrs')
	$s1 = ' selected ';
else
if ($place_millagetype=='Km')
	$s2 = ' selected ';
else
if ($place_millagetype=='Miles')
	$s3 = ' selected ';

	
echo"<td><input type='number' name='place_millage' id='place_millage' value='".htmlspecialchars($place_millage, ENT_QUOTES)."' style='font-size:12px; width:140px; height:18px;'  $status maxlength='40' />
	<select name='place_millagetype'  $status >
		<option></option>
		<option $s1>Hrs</option>
		<option $s2>Km</option>
		<option $s3>Miles</option>
	</select>
	</td>
	</tr>";

echo "<tr bgcolor='#F7F7F7'>	
	    <td><span class='style1'>Trip Allowance</span></td>
	   <td><input type='number' name='tripAllowance' id='tripAllowance' value='$tripAllowance' style='font-size:12px; width:150px; height:18px;'  $status> <span class='style1'>$base1</span></td>	
    ";

$queryTrip = "select fueltype_id, fueltype_description from fueltype ";
$resTrip = mysql_query($queryTrip,$connection);

echo"   <td><span class='style1'>Trip Type</span></td>
		<td><select name='tripType' id='tripType' style='font-size:12px; width:200px; height:18px;' $status>";
		echo "<option value='' ".($tripType==''?'selected':'')."></option>";
	while($rowTrip = mysql_fetch_array($resTrip)){
		echo "<option value='$rowTrip[0]' ".($tripType==$rowTrip[0]?'selected':'').">$rowTrip[1]</option>";
	}
echo "	</select></td>
		<td><span class='style1'>Usage in LTRS</span></td>
		<td><input type='number' name='usageLtrs' id='usageLtrs' value='$usageLtrs' style='font-size:12px; width:140px; height:18px;'  $status></td>	
	</tr>";

echo "<tr bgcolor='#F7F7F7'>
		<td><span class='style1'>Estimation Nbr of days</span></td>
		<td><input type='number' name='estimationDays' id='estimationDays' value='$estimationDays' style='font-size:12px; width:150px; height:18px;'  $status></td>	
		<td><span class='style1'>Consumption Per distance</span></td>
		<td><input type='number' name='consumptionPerDist' id='consumptionPerDist' value='$consumptionPerDist' readonly style='font-size:12px; width:200px; height:18px;'  $status></td>	
		<td><span class='style1'>Consumption</span></td>
		<td><input type='number' name='consumption' id='consumption' value='$consumption' style='font-size:12px; width:140px; height:18px;' readonly $status></td>	
	 </tr>";

echo "<tr bgcolor='#F7F7F7'>
		<td><span class='style1'>Charges Rate</span></td>
		<td><input type='number' name='chargeRate' id='chargeRate' value='$chargeRate' style='font-size:12px; width:150px; height:18px;'  $status></td>	
		<td></td>
		<td></td>	
		<td></td>
		<td></td>	
	 </tr>";	
echo "<tr height='30px' bgcolor='#F7F7F7'><td colspan='6'></td></tr>";
echo"</table></td></tr>";

echo"<tr><td  valign='top'>";

echo"<table style='width:1080px;' border='0' cellspacing=0><col width=1080>
	<tr><td style='width:1080px;height:15px;' ></td></tr>

	<tr><td  valign='top' style='width:1080px;'><table style='width:1080px;' border='0' cellspacing=0>
	 <col width=20px><col width=20px><col width=120px><col width=120px><col width=100px><col width=100px><col width=100px><col width=100px>
	 <col width=100px><col width=100px><col width=100px><col width=100px>
	<tr height='30px'><td colspan='2'>&nbsp;</td>
		<td align='center'><span class='style1'><b>From</b></span></td>
		<td align='center'><span class='style1'><b>To</b></span></td>
		<td align='center'><span class='style1'><b>Trip Distance</b></span></td>
		<td align='center'><span class='style1'><b>Trip Allowance</b></span></td>
		<td align='center'><span class='style1'><b>Trip Type</b></span></td>
		<td align='center'><span class='style1'><b>Usage in LTRS</b></span></td>
		<td align='center'><span class='style1'><b>Estimation Nbr of days</b></span></td>
		<td align='center'><span class='style1'><b>Consumption Per distance</b></span></td>
		<td align='center'><span class='style1'><b>Consumption</b></span></td>
		<td align='center'><span class='style1'><b>Charges Rate</b></span></td>
	</tr></table></td></tr>";
	 
	echo"<tr><td ><div style='overflow-y:scroll;height:300px;width:1080px;border:0px solid #000000;padding-left:5px;'><table  border='0' width='1080px'>
	<col width=20px><col width=20px><col width=120px><col width=120px><col width=100px><col width=100px><col width=100px><col width=100px>
	<col width=100px><col width=100px><col width=100px><col width=100px>";
	


$querysearch="select Place_Id,fromplace_name,toplace_name,place_millage,place_millagetype,place_tripAllowance, fueltype_description, place_usageLtrs, place_estimationDays,
			place_ConsumptionPerDist, place_Consumption, place_ChargesRate 
			from Place 
			left join fueltype on fueltype.fueltype_id = place.place_tripTypeId ";
$querysearch .= " where (fromplace_name like"."('%".$PRef."%') or toplace_name like"."('%".$PRef."%') or place_millage like"."('%".$PRef."%')) ".
                          " order by fromplace_name ";
//echo$querysearch;

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
	
	if ($startrange=='')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
	
	$querysearch = $querysearch." limit ".$startrange.",".$perpage;
	

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	
	while($rows=mysql_fetch_array($resultsearch)){
		$ct+=1;
		$stylefont='style15';
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
				
		if($mode=='edit' && $Place_Id==$rows[0]){
			
			$Color="style='background-color:#1e87a5'";
			$stylefont='style28';
	}	
		
	echo'<tr '.$Color.' height="20px" >';
	
	if($FI_PlaceDel==1)	
print<<<HERE

<td align=center><img border='0' src='img/c_delete.png' width='12' height='12' title='Delete Record' onmouseover="this.style.cursor='pointer'" OnClick="confirm_delete('$rows[0]')"></td>

HERE;
else print<<<HERE

<td align=center >&nbsp;</td>

HERE;

if($FI_PlaceMod==1)
	echo "<td align='center'><img border='0' src='img/button_edit.png' width='12' height='12' title='Edit Record' onmouseover=\"this.style.cursor='pointer'\" onclick=\"SetWh('$rows[0]');\" ></td>";
else
	echo "<td></td>";
	 
echo"<td><span class='$stylefont' >$rows[1]</span></td>
	<td><span class='$stylefont' >$rows[2]</span></td>
	<td><span class='$stylefont' >$rows[3] $rows[4]</span></td>
	<td><span class='$stylefont' >$rows[5]</span></td>
	<td><span class='$stylefont' >$rows[6]</span></td>
	<td><span class='$stylefont' >$rows[7]</span></td>
	<td><span class='$stylefont' >$rows[8]</span></td>
	<td><span class='$stylefont' >$rows[9]</span></td>
	<td><span class='$stylefont' >$rows[10]</span></td>
	<td><span class='$stylefont' >$rows[11]</span></td>
	";
echo"</tr>";
}		
	
echo"</table></div></td></tr>";	

echo"<tr height='50px'><td ><table width=100% border='0'><tr><td align='center' width=50%>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td></tr><tr>	<td  style='width:380px;height:20px;'></td>	</tr></table></td></tr>";
			


echo"</tr></table></td></tr>";	
echo"</tr></table></td></tr>";	
	
	 
			 
		  

print <<< HERE
       </td>
      </tr>  <tr height='30'><td align='center' id='content-bottom' ></td></tr>
	  </table>
HERE;


print <<<HERE
</form>
<script>
	document.getElementById('usageLtrs').addEventListener("keyup", setConsumption);
	document.getElementById('usageLtrs').addEventListener("keyup", setConsumptionDistance);
	document.getElementById('place_millage').addEventListener("keyup", setConsumptionDistance);
	document.getElementById('tripType').addEventListener("change", setConsumption);

	function setConsumption()
	{ 
		let usageLtrs = document.getElementById('usageLtrs').value;
		let tripType = document.getElementById('tripType').value; 

		usageLtrs = parseFloat(usageLtrs);
		
		if (tripType != ''){
			if (window.XMLHttpRequest)
			{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
			}
			else
			{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange=function()
			{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					let data = xmlhttp.responseText.split("[BRK]");
					let amountPerLtr = data[1];
					if (amountPerLtr == '') amountPerLtr = 0;

					amountPerLtr = parseFloat(amountPerLtr);
					document.getElementById('consumption').value = parseFloat(usageLtrs*amountPerLtr);
				}
			}
			xmlhttp.open("GET","Form_Place_Actions.php?getmode=setConsumption&Typeid="+tripType,true);
			xmlhttp.send();  
		}
	}
	function setConsumptionDistance()
	{ 
		let usageLtrs = document.getElementById('usageLtrs').value;
		let place_millage = document.getElementById('place_millage').value; 

		usageLtrs 		= parseFloat(usageLtrs);
		place_millage   = parseFloat(place_millage);
		
		if (usageLtrs != '')
		document.getElementById('consumptionPerDist').value = place_millage/usageLtrs;
	}
</script>	
</body>

HERE;

?>