<?php

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


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($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


$querylog="SELECT FI_VehicleService as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['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");
	


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

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

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

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


if($mode=='edit' or $mode=='view'){

	$query_edit=" select service_id,service_description,service_frequency,service_repeattype,service_odometer ".
				"  from service where service_Id='".$service_id."'";
//echo $query_edit;exit;
	$resultedit = mysql_query($query_edit, $connection);
	while($row=mysql_fetch_array($resultedit)){

		$service_Id=$row[0];
		$service_description=$row[1];
		$service_frequency=$row[2];
		$service_repeattype=$row[3];
		$service_odometer=$row[4];
		
		
        
	}
}


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">
<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="tabs.css">
<script type="text/javascript" src="js/tabsClass/tabber.js"></script>
<link rel="stylesheet" href="js/tabsClass/tabber.css" type="text/css" />

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


function editmode () {

	document.service.action="Form_Services.php?mode=edit";
	document.service.submit();
}

function addservice () {
ClearForm('All');

			document.service.action="Form_Services.php?mode=insert";
				document.service.submit();
}



function Edit_Record(serviceid){

document.service.actionmode.value='edit';

document.service.service_id.value=serviceid;

service.action="Form_Services.php?mode=edit&service_id="+serviceid;

service.submit();
self.close();
}

function CancelRecord () {


if (confirm ( "Are you sure you want to Cancel Page?")){
	ClearForm('All');
	document.service.action="Form_Services.php?mode=";
	document.service.submit();
}
}






function SubmitRecord() {

		
if (document.service.service_description.value=='' )
		{alert('Unable to save : check missing field description');
		document.getElementById("service_description").focus();
		}		
		else {
		
		document.service.action="Form_Services_Actions.php";
		document.service.submit();
		}


}


function ClearPage(){
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		
		
		

		x.actionmode.value='';
		x.action='Form_Services.php';
		x.submit();
	}	
}

//All= All form; D= Detail Part
function ClearForm(Option){
    x = document.service;
	x.service_id.value='';
	x.service_description.value='';
	x.service_frequency.value='';
	x.service_repeattype.value='';
	x.service_odometer.value='';
		
	}



   function PrintPreview() {
	var str = '';
	
	str = 'Form_ServicesPreview.php';
	window.open(str);

}

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

}

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

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

function set_limit(){

	perpage=document.service.perpage.value;
	document.service.action='Form_Services.php?perpage='+perpage;
	document.service.submit();
}



function confirm_delete(Code, serviceid) {
	var strReplaceAll = Code;
	var intIndexOfMatch = strReplaceAll.indexOf( "%20" );

	while (intIndexOfMatch != -1){
		strReplaceAll = strReplaceAll.replace( "%20", " " );
		intIndexOfMatch = strReplaceAll.indexOf( "%20" );
	}
	Code= strReplaceAll;
				
	
	if (confirm ( "Are you sure you want to Delete the service " + Code + "?"))
	{
		document.service.action="Form_Services_Actions.php?actionmodedelete=delete&serviceid="+serviceid;
		document.service.submit();
	}
	
	
							
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Service</title>


<script type="text/javascript" src="dropdown.js"></script>

<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;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="tabs.css">
</head>
<body >
<form name='service' method='post'>
<input type=hidden name='actionmode' value='$mode'>
<input type=hidden name='message' value='$message'>
<input type=hidden name='service_id' value='$service_id'>
<input type=hidden name='message' value='$message'>
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'>Services</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

	 
	
echo"<tr>
		<td  height='18'  valign='top' id='content-top2'>";
			
			
echo"</td></tr>";

	echo"<tr><td align=center id='content-body'> ";


	echo"<table border=0 align=center width=900> <tr   height='35'>

			 ";
			
	
			echo"	<td align=left><button type='button'  value='Add Service'";  	
			
			if($mode=='insert' or $mode=='edit')
			echo "disabled ";
			
			echo"onclick='addservice()'   title='Add Service' $InsMode><span><span>Add Service</span></span></button>";
				
echo"           <button type='button' id='btnClear' name='BtnClear' value='Clear Page'   onclick='ClearPage();'  title='Clear Page'>
				<span><span>Clear Page</span></span></button>
				";

				
echo"           <button type='button' name='btnCancel' id='btnCancel' value='Cancel' ";//cancel
				if ($mode=='' )
					echo " disabled ";
				echo" onclick='CancelRecord();'   title='Cancel Changes'><span><span>Cancel</span></span></button>";
				
				
		if($mode=='view'){
echo"           <button type='button' name='btnEdit' id='btnEdit' value='Edit' ";//Edit
				
				
				echo" onclick='editmode();' 	title='Edit Service' /><span><span>Edit</span></span></button>";
	}
else	{
echo"           <button type='button' name='btnPost' id='btnPost' value='Save' ";//post
				if ($mode=='')
						echo " disabled ";
				
				echo" onclick='SubmitRecord();' 	title='Save Changes' /><span><span>Save</span></span></button>";}


				
echo" 			<button name='btnPrint' type='button' id='btnPrint' value='Print' onclick='PrintPreview();'   title='Print' ";
				if ($mode=='edit' or $mode=='insert') echo " disabled ";
						echo" onclick='PrintPreview();'  	title='Print' /><span><span>Print</span></span></button>";

						echo"</td>";
				

			 
			 
			 
		echo"  </tr></table>";
	 
    echo"</td>
      </tr>";
	   
echo"<tr><td colspan=2 id='content-body'>";
	  
echo"  
        <table border='0' align='center' valign='top' width=990>
      <col width=120>
		 ";

			



echo"<tr bgcolor='#F7F7F7'><td valign='center' align='left' ><span class='style1'>Description&nbsp;<span class='style26'>*</span></span></td>";
echo"<td><input type='text' name='service_description' value='".htmlspecialchars($service_description, ENT_QUOTES)."' style='font-size:12px; width:400px; height:18px;'  $status maxlength='40' /></td></tr>";

			  
echo"<tr bgcolor='#F7F7F7'><td valign='center' align='left' ><span class='style1'>Repeat every</span></td>";
echo"<td><input type='text' name='service_frequency' value='$service_frequency' style='width:35px'>&nbsp;&nbsp;";
echo"<select name='service_repeattype'>";
echo"<option>days</option>";
echo"<option>weeks</option>";
echo"<option>months</option>";
echo"<option>years</option>";
echo"</select >";
echo" or  <input type='text' name='service_odometer' value='$service_odometer' style='width:100px'> (Odometer Unit)</td></tr>";


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

		
			 
		  

print <<< HERE
   
	  <tr><td colspan=8 id='content-body'>
	  <div style='overflow-y:scroll;height:350px;width:990px;align:center' >
	  
HERE;

	echo"<table border='0' align=center width='900'>
	  <col width=50>
	  <col width=50>
	  <col width=290>
	  <col width=540>
	  
	  
		<tr><td></td><td>&nbsp;</td></tr>     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='5'><span class='style16'>List of Services</span></td></tr>
	  <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left' colspan='3'><span class='style5'>Search</span>
		<span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' 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 colspan=2 align='left' valign='center'><span class='style5'>Result per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
              


        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';

			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';	
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='5'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr  class='Search-Label-bg'>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style4'>Description </span></td>
		<td  height='25' align='center'><span class='style4'>Perform Service Every</span></td>
		
				
	  </tr>";
		

	

	


$querysearch="select service_Id,service_description,concat(service_frequency,' ',service_repeattype,' or ',service_odometer ,' (odom. unit)') 
from service ";
$querysearch=$querysearch." where (service_description like"."('%".$PRef."%') 
								or service_repeattype like"."('%".$PRef."%')) ".
                          " order by service_description ";
//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)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			

	echo'<tr '.$Color.'>';
	
	
print<<<HERE
	

HERE;
	
	
	
	$name=ereg_replace(" ","%20",$rows[2]);	
	$name=ereg_replace("'","%27",$name);
print<<<HERE
	
	
	
<td align=center width='3%'><img border='0' src='img/edit.png' width='20' height='20' title='Edit Detail' onmouseover="this.style.cursor='pointer'" onclick="Edit_Record('$rows[0]')"$ModMode ></td>


<td align=center width='3%'><img border='0' src='img/delete.png' width='20' height='20' title='Delete Detail' onmouseover="this.style.cursor='pointer'" OnClick="confirm_delete('$code', '$rows[0]')" $DelMode></td>

<td><font face='Arial' size='2' color='#186194'>

HERE;
	

print<<<HERE
<span class="style5">$rows[1]&nbsp;</span>

HERE;


echo"</font></td><td><font face='Arial' size='2' color='#186194'><span class='style5'>$rows[2]&nbsp;</span></font></td>";

echo"<td><font face='Arial' size='2' color='#186194'><span class='style5'>$rows[3]</font></span></td></tr>";


}
							

			
		
echo"   <tr><td colspan='5'>&nbsp;</td></tr>
		<tr><td colspan=5><table width='900' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";  
	  
	  

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

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


print <<<HERE
</form>
</body>

HERE;

?>