<?php
session_start();
 header("Content-type: text/html; charset=windows-1256");
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("Functions_UserAccess.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");




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

$frm   = $_GET['frm'];if ($frm == ""){$frm = $_POST["frm"];}
$txtId = $_GET['txtId'];if ($txtId == ""){$txtId = $_POST["txtId"];}

if ($PCustomerCode == '') {
		if ($txtCode != '') {
			$sub_query = " Select Count(Customer_Code) From Customer ".
						 " Where Customer_Code = '".$txtCode."'";
			$sub_result = selectData($connection, $sub_query);
			if ($sub_result[0] != 0) $ErrorExists = 1;
		}
		
		if ($ErrorExists == 1) {
			$PActionMode = 'Add';
			$message = 'Insert not allowed. Duplicate Code: '.$txtCode;
		}	
			
		
		
		if ($txtCode != "" && $ErrorExists == 0){
		
			fbegin();
			$query = " INSERT INTO Customer (Customer_Code, Customer_Name, Customer_Telephone1, Customer_Telephone2, Customer_Email, Customer_Address ) ".
					 " VALUES ( '".$txtCode."', '".ereg_replace("'", "\'", trim($txtName))."', '".$txtPhone1."', '".$txtPhone2."', ".
					 "          '".ereg_replace("'", "\'", trim($txtEmail))."', '".ereg_replace("'", "\'", trim($txtAddress))."' )";
						 
				 
			$result=mysql_query($query,$connection);
			//echo $query."<br>";exit;	
			
			if ($result){
				$PActionMode = '';
				$PLedgerNo   = $txtCode;
				fcommit();
				
				header ("Location: ./List_Customers.php?frm=".$frm."&item=".$item."&txtId=".$txtId."&PLedgerNo=".$PLedgerNo."&message=".$message);	
			}else{
				$PActionMode = 'Add';
				$message  = mysql_error(); 
				frollback();
			}
	
		}
	} else {
		fbegin();
		$query = " UPDATE Customer SET ".
		         " Customer_Name = '".ereg_replace("'", "\'", trim($txtName))."', Customer_Telephone1 = '".$txtPhone1."', Customer_Telephone2 = '".$txtPhone2."', ".
				 " Customer_Email = '".ereg_replace("'", "\'", trim($txtEmail))."', Customer_Address = '".ereg_replace("'", "\'", trim($txtAddress))."' ".
				
				 " WHERE Customer_Code = '".$PCustomerCode."'";
						 
				 
		$result=mysql_query($query,$connection);
		//echo $query."<br>";exit;	
			
		if ($result){
			$PActionMode = '';
			$PCustomerCode = '';
			$PLedgerNo   = $txtCode;
			fcommit();
				
			header ("Location: ./List_Customers.php?frm=".$frm."&item=".$item."&txtId=".$txtId."&PLedgerNo=".$PLedgerNo."&message=".$message);	
		}else{
			$PActionMode = 'Edit';
			$message  = mysql_error(); 
			frollback();
		}
	}	

	function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}

function selectData($conn, $query){
	$result = mysql_query($query, $conn);
	$cur = mysql_fetch_row($result);		
	return $cur;
}
//header ("Location: ./List_Customers.php");
?>