<?php

session_start();


if($HTTP_SESSION_VARS['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($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);
}

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

$Area_Code = ereg_replace("'", "\'", $Area_Code);
$Area_Name = ereg_replace("'", "\'", $Area_Name);
$cartype = ereg_replace("'", "\'", $cartype);

if (!empty($Area_CreationDate))	{
	$Area_CreationDate  = explodeword($Area_CreationDate );
	$Area_CreationDate   = "'$Area_CreationDate '";

}	
else {$Area_CreationDate = 'null';}

$Area_Code=trim($Area_Code);
echo $Auto;



if($mainmode=='insert'){
		
	$query="Insert into Location ( code, Description, Stamp1, Stamp2, Stamp3, Stamp4, Stamp5, Stamp6)
			values ('".$Location_Code."', '".$Location_Name."','$stamp1','$stamp2' ,'$stamp3' ,'$stamp4' ,'$stamp5' ,'$stamp6')";

	echo$query;				
	
	$result  = mysql_query($query, $connection);
	$Auto= mysql_insert_id($connection);
	$message = mysql_error();

	
}

if($mainmode=='delete')
{	
	$Auto =$_GET['Auto'];
	
	$query="delete from Location  where Auto='".$Auto."'";
	echo$query;
	//exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	$errornum= mysql_errno();
						
	if($errornum=='1451') {
		$message="Delete Not Allowed . Location  is used .";
		//echo $message;
	}
	
	
	
}


if($mainmode=='edit'){
	$query_update=" update location  set code='".$Location_Code."', ".
				  " Description='".$Location_Name."', ".
				  " Stamp1='".$stamp1."', ".
				  " Stamp2='".$stamp2."', ".
				  " Stamp3='".$stamp3."', ".
				  " Stamp4='".$stamp4."', ".
				  " Stamp5='".$stamp5."', ".
				  "	Stamp6='". $stamp6  ."'".
				  "	where Auto='".$Auto."'";
	echo $query_update;
	//exit;		

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

	
}


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

if($mainmode=='delete')
	header ("Location: ./Form_Location.php?mainmode=insert&message=".$message);
else 
header ("Location: ./Form_Location.php?Auto=$Auto&mainmode=editmode&message=".$message);

mysql_close($connection);



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

HERE;


?>
		           