<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("Functions_Synchro.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);
}

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

//Read From Module Table
$querymodule=" Select ifNull(Module_Synchro,0) As Module_Synchro From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$query_setup=" Select base1 As base1, base2 as base2, Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

//$Container_size = ereg_replace("'", "\'", $Container_size);

echo $containerdt_id;
$containerdt_id 		= $_POST["containerdt_id"]; if ($containerdt_id == ""){$containerdt_id = $_GET["containerdt_id"];} 

$message="";


$received_date 			= formatdate($received_date);

if($mode=='insert'){
		
	$query="Insert into containerdt( 
	 container_sysdate, container_BOL,  received_date, departure_date,
	Shipper, consigne, vessel_name,  container_code,container_freeday) values(now(),'$billlading','$received_date',
	'DATE_ADD('$received_date', INTERVAL $freeday DAY)','$vessel_name','$container_code''$shipper','$consignee','$vessel_name','$container_code','$freeday')";
			
	echo $query;
	$result  = mysql_query($query, $connection);
	
	

	
}

if($mode=='delete')
{	
	$query="delete from containerdt where containerdt_id= '$containerdt_id'";
	
	$result  = mysql_query($query, $connection);
	echo $query;
	if (!$result) {

	$message ='Delete Not Allowed ';	
	}
}


if($mode=='edit'){
	
	$query="update Containerdt set
container_BOL='$billlading',  received_date='$received_date', departure_date=DATE_ADD('$received_date', INTERVAL $freeday DAY),
	Shipper='$shipper', consigne='$consignee', vessel_name='$vessel_name',  container_code='$container_code',container_freeday='$freeday'
	where containerdt_id='$containerdt_id'";
	$result = mysql_query($query, $connection);
	$message    = mysql_error();
	echo $query;
	
	
	
}

$message = ereg_replace(' ','%20',$message);
echo $mode;
if(($mode=='delete')||($mode=='insert'))
	header ("Location: ./Form_Containerdt.php?mode=insert&message=".$message."&perpage=".$perpage);
else 
header ("Location: ./Form_Containerdt.php?containerdt_id=$containerdt_id&mode=editmode&message=".$message."&perpage=".$perpage);

mysql_close($connection);

function formatdate($dateval){
	if($dateval != ''){
	$ndate = explode("-", $dateval);
	$dateval = $ndate[2]."-".$ndate[1]."-".$ndate[0];
}
	return $dateval;
}


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

HERE;


?>
		           