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

$type_code = ereg_replace("'", "\'", $type_code);
$type_name = ereg_replace("'", "\'", $type_name);
$tipo_type = ereg_replace("'", "\'", $tipo_type);
$tipo_measure = ereg_replace("'", "\'", $tipo_measure);
$tipo_height = ereg_replace("'", "\'", $tipo_height);
$tipo_decsription = ereg_replace("'", "\'", $tipo_decsription);
$tipo_remark = ereg_replace("'", "\'", $tipo_remark);
$tipo_tar = ereg_replace("'", "\'", $tipo_tar);


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

$message="";



if($mode=='insert'){
		$query="select type_code  from containertype where type_code='".$type_code."'";
	
	$result = mysql_query($query, $connection);
	
	$records=mysql_num_rows($result);

	
	if ($records==0){
	$query="Insert into containertype(type_code,type_name,tipo_type,tipo_measure,
	tipo_height,tipo_decsription,tipo_remark,tipo_tar) values('$type_code','$type_name','$tipo_type',
	'$tipo_measure','$tipo_height','$tipo_decsription','$tipo_remark','$tipo_tar')";
	$result  = mysql_query($query, $connection);}
		else $message ="this code already exist ";		
	
	
	


	if (!$result) {

	$message ="this type already exist ";	
	}
}

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

	$message ='Delete Not Allowed ';	
	}
}


if($mode=='edit'){
	
	$query="select type_code  from containertype where type_code='".$type_code."'and Type_Auto!='$Type_Auto'";
	
	$result = mysql_query($query, $connection);
	
	$records=mysql_num_rows($result);
echo $records;
	
	if ($records==0){
	$query="update containertype set type_code='$type_code',type_name='$type_name' ,
	tipo_type='$tipo_type',tipo_measure='$tipo_measure',
	tipo_height='$tipo_height',tipo_decsription='$tipo_decsription',tipo_remark='$tipo_remark',tipo_tar='$tipo_tar'
	where Type_Auto='$Type_Auto'";
	echo $query;
	$result = mysql_query($query, $connection);
	$message    = mysql_error();
	//echo $query;
	}

	else $message ="this code already exist ";		
	if (!$result) {
		$message='this Port already exist ';
	}	

}

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

if(($mode=='delete')||($mode=='insert'))
	header ("Location: ./Form_ContainerType.php?mode=insert&message=".$message."&perpage=".$perpage);
else 
header ("Location: ./Form_ContainerType.php?Type_Auto=$Type_Auto&mode=editmode&message=".$message."&perpage=".$perpage);

mysql_close($connection);



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

HERE;


?>
		           