<?php
header("Content-type: text/html; charset=windows-1256");
session_start();


if($_SESSION['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($_SESSION['dbName'],$connection) or die("Could not find DB");

if($config['log_file'] == 1){
$connection_history = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($config['dbName_history'],$connection_history) or die("Could not find DB");
}
$query_user_name = "select User_Name from ".$_SESSION['dbLabel'].".users where user_account='".$_SESSION['useraccount']."'";
$result_user_name = mysql_query($query_user_name, $connection);
$rows_user_name = mysql_fetch_array($result_user_name);
extract($rows_user_name);

$actionmode 		= $_GET["actionmode"]; if ($actionmode == ""){$actionmode = $_POST["actionmode"];}   
$measurement_type 	= $_POST["measurement_type"]; if ($measurement_type == ""){$measurement_type = $_GET["measurement_type"];}   
$measurement_number = $_POST["measurement_number"]; if ($measurement_number == ""){$measurement_number = $_GET["measurement_number"];}   
$measurement_id 	= $_POST["measurement_id"]; if ($measurement_id == ""){$measurement_id = $_GET["measurement_id"];}   

if($actionmode == 'insert'){
	$queryinc = "select ifnull(max(Measurement_id),0)+1 from measurementsetup";
	$resinc = mysql_query($queryinc,$connection) or die(mysql_error());
	$rowinc = mysql_fetch_row($resinc);
	$measurement_id = $rowinc[0];
	$query = "insert into measurementsetup ( Measurement_id, Measurement_Type, Measurement_number) ".
			" values ( $measurement_id, ".strformat($measurement_type).", ".strformat($measurement_number).")";
	$res = mysql_query($query,$connection);

if($config['log_file'] == 1){
							
							$query_history="insert into measurementsetup
							( Measurement_id, Measurement_Type, Measurement_number
							,measurementsetup_date, measurementsetup_operator_id, measurementsetup_schema, measurementsetup_status)
							 values ( $measurement_id, ".strformat($measurement_type).", ".strformat($measurement_number).",
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
						$result_history  = mysql_query($query_history, $connection_history);
						$message    = mysql_error();
						
						//echo $query_history;exit;
						}
	
	
	if(!$res) $message  = mysql_error($connection); 	
} else if($actionmode == 'update'){
	$query = "update measurementsetup set Measurement_Type=".strformat($measurement_type).", 
			  Measurement_number = ".strformat($measurement_number)." 
			  where Measurement_id = $measurement_id";
			
	$res = mysql_query($query,$connection) or die(mysql_error());
$message    = mysql_error();	
	
	if($config['log_file'] == 1){
							
							$query_history="insert into measurementsetup
							( Measurement_id,Measurement_Type, Measurement_number
							,measurementsetup_date, measurementsetup_operator_id, measurementsetup_schema, measurementsetup_status)
							 values ($measurement_id ," .strformat($measurement_type)."," .strformat($measurement_number).",
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
						$resulot_query_history  = mysql_query($query_history, $connection_history);
						$message    = mysql_error();
						
						//echo $query_history;
						//exit;
						}
	
	
	if(!$res)  $message  = mysql_error($connection); 	
} else if($actionmode == 'deletemeasure'){
	
	
	
	$query = "select * from measurementsetup where Measurement_id = $measurement_id";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$Measurement_id=$rows[0];
	$Measurement_Type=$rows[1];
	$Measurement_number=$rows[2];
	
	
	$query = "delete from measurementsetup where Measurement_id = $measurement_id";
	$res = mysql_query($query,$connection) or die(mysql_error());	
	
	
	if($config['log_file'] == 1){
							
							$query_history="insert into measurementsetup
							( Measurement_id,Measurement_Type, Measurement_number
							,measurementsetup_date, measurementsetup_operator_id, measurementsetup_schema, measurementsetup_status)
							 values ($Measurement_id ," .strformat($Measurement_Type)."," .strformat($Measurement_number).",
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
						$resulot_query_history  = mysql_query($query_history, $connection_history);
						$message    = mysql_error();
						
					
						}
	if(!$res)  $message  = mysql_error($connection); 
	else  $message  = "Measurement was Deleted!"; 	
}

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

header("location:./Form_MeasurmentSetup.php?message=$message");
?>
		        



