<?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");

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp 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);
}

/*
echo $actionmode."<br>";
echo $modedt."<br>";
echo $modedtaccidents."<br>";
echo "ads".$vehicle_idaccidents."<br>";
echo $employeeaccident_id;
exit;
*/
if (!empty($employee_birthdate))	{
	$employee_birthdate  = explodeword1($employee_birthdate );
	$employee_birthdate   = "'$employee_birthdate'";
}	
else {$employee_birthdate = 'null';}

if (!empty($employee_licenseexpirydate))	{
	$employee_licenseexpirydate  = explodeword1($employee_licenseexpirydate );
	$employee_licenseexpirydate   = "'$employee_licenseexpirydate'";
}	
else {$employee_licenseexpirydate = 'null';}




if($actionmode=='insert'){
	
	// Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);	
	  
	  
	$query = "Insert into employee (
				  employee_firstname, employee_lastname, employee_birthdate
				,  employee_licensenumber, employee_licenseexpirydate, employee_note, employee_country
				, employee_city, employee_address, employee_telephone1, employee_telephone2, employee_picture
				)
			values (
			 '".$employee_firstname."', '".$employee_lastname."', ".$employee_birthdate."
				, '".$employee_licensenumber."', ".$employee_licenseexpirydate.", '".$employee_note."'
				, '".$employee_country."', '".$employee_city."', '".$employee_address."', '".$employee_telephone1."'
				, '".$employee_telephone2."'	, '".$data."'	)";
	
	$result  = mysql_query($query, $connection);
	$employee_id= mysql_insert_id($connection);
	$message = mysql_error();

			   

}


if($actionmodedelete=='delete')
{	
	$query="delete from employee where employee_id='".$employeeid."'";
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	
}


if (($actionmode=='edit') and ($employeevehicle_id=='') and ($vehicle_id=='') and ($employeeaccident_id=='') and ($vehicle_idaccidents=='')){


if ($_FILES['image']['tmp_name']!='') {
// Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  
       
      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);	
	  }
	

	$query_update=" update employee set 
					employee_firstname = '".$employee_firstname."'
					,employee_lastname = '".$employee_lastname."'
					,employee_birthdate = ".$employee_birthdate."
					,employee_licensenumber =  '".$employee_licensenumber."'
					,employee_licenseexpirydate =".$employee_licenseexpirydate."
					,employee_note = '".$employee_note."'
					,employee_country = '".$employee_country."'
					,employee_city= '".$employee_city."'
					,employee_address= '".$employee_address."'
					,employee_telephone1 = '".$employee_telephone1."'
					,employee_telephone2 = '".$employee_telephone2."'";
					
				  if (!empty($data))
					$query_update=$query_update.",employee_picture =  '".$data."'";
				  
				  $query_update=$query_update." where employee_id='".$employee_id."'";


	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();


}
else
if (($actionmode=='edit') and ($modedt=='add_employeevehicle') and ($vehicle_id!='') ){


if (!empty($employeevehicle_startdate))	{
	$employeevehicle_startdate  = explodeword1($employeevehicle_startdate );
	$employeevehicle_startdate   = "'$employeevehicle_startdate'";
}	
else {$employeevehicle_startdate = 'null';}

if (!empty($employeevehicle_enddate))	{
	$employeevehicle_enddate  = explodeword1($employeevehicle_enddate );
	$employeevehicle_enddate   = "'$employeevehicle_enddate'";
}	
else {$employeevehicle_enddate = 'null';}

$query="Insert into employeevehicle (
				 employee_id
				 , vehicle_id
				 , employeevehicle_startdate
				 , employeevehicle_enddate
				 , employeevehicle_note
				)
			values (
			'".$employee_id."', '".$vehicle_id."', ".$employeevehicle_startdate.", ".$employeevehicle_enddate."
			, '".$employeevehicle_note."'
					
				)";
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	header ("Location: ./Form_Employee.php?employee_id=$employee_id&vTabFocus=Vehicle%20Driven&actionmode=edit&message=".$message);
	exit;
}
else
if (($actionmode=='edit') and ($modedt=='edit_employeevehicle') ){


if (!empty($employeevehicle_startdate))	{
	$employeevehicle_startdate  = explodeword1($employeevehicle_startdate );
	$employeevehicle_startdate   = "'$employeevehicle_startdate'";
}	
else {$employeevehicle_startdate = 'null';}

if (!empty($employeevehicle_enddate))	{
	$employeevehicle_enddate  = explodeword1($employeevehicle_enddate );
	$employeevehicle_enddate   = "'$employeevehicle_enddate'";
}	
else {$employeevehicle_enddate = 'null';}

$query="update employeevehicle 
				 set employee_id = '".$employee_id."'
				 , vehicle_id = '".$vehicle_id."'
				 , employeevehicle_startdate = ".$employeevehicle_startdate."
				 , employeevehicle_enddate = ".$employeevehicle_enddate."
				 , employeevehicle_note = '".$employeevehicle_note."'
				where employeevehicle_id =	$employeevehicle_id	
			";
			
					
			
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	header ("Location: ./Form_Employee.php?employee_id=$employee_id&vTabFocus=Vehicle%20Driven&actionmode=edit&message=".$message);
	exit;
}
else
if (($actionmode=='edit') and ($modedt=='delete_employeevehicle') and ($employeevehicle_id!='') )
{	
	$query="delete from employeevehicle where employeevehicle_id='".$employeevehicle_id."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
header ("Location: ./Form_Employee.php?employee_id=$employee_id&vTabFocus=Vehicle%20Driven&actionmode=edit&message=".$message);
exit;
	
}

else
if ( ($actionmode=='edit') and ($modedtaccidents=='add_employeeaccidents') and ($vehicle_idaccidents!='')){


if (!empty($employeeaccident_startdate))	{
	$employeeaccident_startdate  = explodeword1($employeeaccident_startdate );
	$employeeaccident_startdate   = "'$employeeaccident_startdate'";
}	
else 
$employeeaccident_startdate = 'null';

$query="Insert into employeeaccident (
				 employee_id
				 , vehicle_id
				 , employeeaccident_startdate
				 , employeeaccident_reference
				 , employeeaccident_note
				)
			values (
			'".$employee_id."', '".$vehicle_idaccidents."', ".$employeeaccident_startdate.", '".$employeeaccident_reference."'
			, '".$employeeaccident_note."'
					
				)";
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	header ("Location: ./Form_Employee.php?employee_id=$employee_id&vTabFocus=Accidents&actionmode=edit&message=".$message);
	exit;
}
else
if ( ($actionmode=='edit') and ($modedtaccidents=='edit_employeeaccidents') and ($employeeaccident_id!='')){

if (!empty($employeeaccident_startdate))	{
	$employeeaccident_startdate  = explodeword1($employeeaccident_startdate );
	$employeeaccident_startdate   = "'$employeeaccident_startdate'";
}	

$query="update employeeaccident
				 set employee_id = '".$employee_id."'
				 , vehicle_id = '".$vehicle_idaccidents."'
				 , employeeaccident_startdate = ".$employeeaccident_startdate."
				 , employeeaccident_reference = ".$employeeaccident_reference."
				 , employeeaccident_note = '".$employeeaccident_note."'
				where employeeaccident_id =	$employeeaccident_id	
			";
			
					
			
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	header ("Location: ./Form_Employee.php?employee_id=$employee_id&vTabFocus=Accidents&actionmode=edit&message=".$message);
	exit;
}
else
if ( ($actionmode=='edit') and ($modedtaccidents=='delete_employeeaccidents') and ($employeeaccident_id!=''))
{	
	$query="delete from employeeaccident where employeeaccident_id='".$employeeaccident_id."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
header ("Location: ./Form_Employee.php?employee_id=$employee_id&vTabFocus=Accidents&actionmode=edit&message=".$message);
exit;
	
}

$message = ereg_replace(' ','%20',$message);

if($actionmode=='delete')			   
	header ("Location: ./Form_EmployeeSearch.php?employee_id=$employee_id&actionmode=view&message=".$message);
else
	header ("Location: ./Form_Employee.php?employee_id=$employee_id&actionmode=view&message=".$message);

mysql_close($connection);



function explodeword1($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}
print <<<HERE
</form>
</body>

HERE;


?>
		           