<?php

session_start();

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

include("config.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");

$mode 			= $_GET['mode'];
$amntfeet_id	= $_POST['amntfeet_id']; if ($amntfeet_id == '') $amntfeet_id = $_GET['amntfeet_id'];

$lineop 		= $_POST['lineop'];
$location 		= $_POST['txtlocation'];
$bltype 		= $_POST['bltype'];
$cnttype 		= $_POST['cnttype'];
$currencycode 	= $_POST['currencycode'];
$amnt 			= $_POST['txtamnt'];
$commission 	= $_POST['txtcommission'];
$blfreight 		= $_POST['blfreight'];
$cntrlfees 		= $_POST['cntrlfees'];
$fees2 			= $_POST['txtfees2'];
$fees3 			= $_POST['txtfees3'];

if ($amnt == '') 		$amnt 		= 0;
if ($commission == '') 	$commission = 0;
if ($cntrlfees == '') 	$cntrlfees 	= 0;
if ($fees2 == '') 		$fees2 		= 0;
if ($fees3 == '') 		$fees3 		= 0;


if ($mode == 'insert'){
	$querychk = "select 1 from Manifest_FeetAmount
				 where location = ".strformat($location)." and bl_type = ".strformat($bltype)." and container_type = ".strformat($cnttype)." 
				 and  op_line = ".strformat($lineop)." and bl_freight = ".strformat($blfreight)." ";
	$resultchk = mysql_query($querychk, $connection);
	$nmr = mysql_num_rows($resultchk);
	
	if ($nmr == 0){	
		$queryins = "insert into Manifest_FeetAmount
					(location, bl_type, container_type, op_line, amount, currency_code, commission, bl_freight, fees1, fees2, fees3)
					values 
					(".strformat($location).", ".strformat($bltype).", ".strformat($cnttype).", ".strformat($lineop).", $amnt, ".strformat($currencycode).", $commission, 
					 ".strformat($blfreight).", $cntrlfees, $fees2, $fees3 ) ";
		$resultins = mysql_query($queryins, $connection);			 
		if ($resultins)
			$message = "Record Submitted Successfully!";
		else
			$message = mysql_error($connection);
	} else 	
			$message = "Record Already Exists!";	 
} 


if ($mode == 'update' && $amntfeet_id != ''){
	$querychk = "select 1 from Manifest_FeetAmount
				 where location = ".strformat($location)." and bl_type = ".strformat($bltype)." and container_type = ".strformat($cnttype)." 
				 and  op_line = ".strformat($lineop)." and bl_freight = ".strformat($blfreight)." and feetAmount_id <> $amntfeet_id ";
	$resultchk = mysql_query($querychk, $connection);
	$nmr = mysql_num_rows($resultchk);
	
	if ($nmr == 0){	
		$queryup = "update Manifest_FeetAmount set
					location = ".strformat($location).", 
					bl_type = ".strformat($bltype).", 
					container_type = ".strformat($cnttype).", 
					op_line = ".strformat($lineop).", 
					amount = $amnt, 
					currency_code = ".strformat($currencycode).", 
					commission = $commission,
					bl_freight = ".strformat($blfreight).", 
					fees1 = $cntrlfees, 
					fees2 = $fees2, 
					fees3 = $fees3
					where feetAmount_id = $amntfeet_id  ";
		$resultup = mysql_query($queryup, $connection);			 
		if ($resultup)
			$message = "Record Submitted Successfully!";
		else
			$message = mysql_error($connection);
	} else 	
			$message = "Record Already Exists!";	
} 


if ($mode == 'delete' && $amntfeet_id != ''){
	$querydel = "delete from Manifest_FeetAmount where feetAmount_id = $amntfeet_id  ";
	$resdel = mysql_query($querydel, $connection);
	if ($resdel)
			$message = "Record Deleted Successfully!";
		else
			$message = mysql_error($connection);
} 


function strformat($str){
	if($str == '') $str = "NULL";
	else $str = "'".ereg_replace("'", "\'", trim($str))."'";
	return $str;
}

echo "<form name='feetamntaction' action='Form_AmountPerFeet.php'>
		<input type='hidden' name='message' value='$message'>
		<input type='hidden' name='searchOn' value=0>
	 </form>";
echo "<script>
		document.feetamntaction.submit();;
	 </script>";	
exit;	 
?>