<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("Functions_Purchase.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536);
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);
}


//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);
}
$querycostcent=" select costcent_applyvat ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
 			   "";
			   
$costcentres = mysql_query($querycostcent,$connection) or die(mysql_error());
$rows=mysql_fetch_array($costcentres);
$costcent_applyvat = $rows[0];
if($costcent_applyvat=='')	$costcent_applyvat = 1;



//Read From SalesSetup Table
$querySalesSetup="Select SalesTTC, TipsAccount, CashCur,ifNull(show_discountAmount,0) As show_discountAmount From SalesSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}

$querymodule=" Select Module_AdditionalTaxes, ifNull(Module_NegativeQuantity,0) As Module_NegativeQuantity, ifNull(Module_rawmaterial,0) As Module_RawMaterial, ".
			 " ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ifNull(module_paymentterms,0) As Module_PaymentTerms, ".
             " ifNull(module_creditlimit,0) As Module_CreditLimit, ifNull(Module_Delivery,0) As Module_Delivery, ".
			 " ifNull(module_DeliveryWithSorder,0) As Module_DeliveryWithSorder, ifNull(Module_InvoiceCheckLimit,0) As Module_InvoiceCheckLimit, ".
			 " ifNull(Module_PostToJournal,0) As Module_PostToJournal, ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter, ".
			 " ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject,ifNull(Module_CreditLimitByProj,0) As Module_CreditLimitByProj,  ".
			 " ifNull(Module_SerialNo,0) As Module_SerialNo, ifNull(Module_Unit1Method,0) As Module_Unit1Method, ifNull(Module_ChangeInvoiceItems,0) As Module_ChangeInvoiceItems, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt, ".
			 " ifNull(Module_ExpireDate,0) As Module_ExpireDate, ifnull(module_serialnoout,0) as module_serialnoout ".
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

		
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,maxperpage from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$mode = $_GET['mode'];

$crfttype 		= $_POST['crfttype'];
$BL_number 		= $_POST['BL_number'];
$voy_number 	= $_POST['voy_number'];
$voy_year 		= $_POST['voy_year'];
$extra_info		= $_POST['extra_info'];
$manifest_auto	= $_POST['manifest_auto'];
$crftdate		= $_POST['crftdate'];

$item_code		= $_POST['item_code'];
$amount			= $_POST['amount'];
$currency_code	= $_POST['currency_code'];
$client_code 	= $_POST['client_code'];
$crft_id		= $_POST['crft_id'];

$temp_crftdate = $crftdate;
$temp_extrinfo = $extra_info;

if($crftdate == '') $crftdate = "NULL";
else {
	$ndate = explode('-', $crftdate);
	$crftdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$crftdate = "'".$crftdate."'";
}

if($extra_info == '') $extra_info = "NULL";
else $extra_info = "'".$extra_info."'";

$ErrorExist = 0;
$message;
// check if any container was checked
if($mode == 'submitRecord' || $mode == 'editRecord'){
	$count_checked = 0;
	$querydt = "select auto, crft_id  from manifestdt
				where manifest_manifestauto = $manifest_auto and (ifnull(ctrflag,'') <> '*' or ifnull(detailtype,'') = 1) order by detailsequence; ";
	$resdt = mysql_query($querydt, $connection);
	while($rwdt = mysql_fetch_array($resdt)){
		$chk_id = 'chk_id'.$rwdt[0];
		if ($$chk_id == '') $$chk_id = 0;
		
		if ($$chk_id == 1)
			$count_checked += 1;
	}
	if ($count_checked == 0) {
		$ErrorExist = 1;
		$message = "No Container was Selected!";	
	}		
}

if($mode == 'submitRecord' && $ErrorExist == 0){
	
	
	fbegin();	
	if($crfttype == 'CRF')
		$reference = get_nextsequence('crf_reference','',$connection);	
	else if($crfttype == 'CT')
		$reference = get_nextsequence('crt_reference','',$connection);	
	
	fcommit();
	
	fbegin();
	$queryins = " insert into CRFT_Header (cr_type, reference, BL_number, voy_number, voy_year, extra_info, manifest_auto, crft_date) 
				  values 
				  ('$crfttype', '$reference', '$BL_number', '$voy_number', '$voy_year', $extra_info, '$manifest_auto', $crftdate);";
	$result = mysql_query($queryins, $connection);
	//$result=1;
	if($result){
		$crft_id = mysql_insert_id();
		fcommit();		
		
		$querydt = "select auto, crft_id  from manifestdt where manifest_manifestauto = $manifest_auto and (ifnull(ctrflag,'') <> '*' or ifnull(detailtype,'') = 1) order by detailsequence; ";
		$resdt = mysql_query($querydt, $connection);
		while($rwdt = mysql_fetch_array($resdt)){
			$chk_id = 'chk_id'.$rwdt[0];
			if ($$chk_id == '') $$chk_id = 0;
			
			if ($$chk_id == '1'){
				if($crfttype == 'CRF')
					$update1 = "update manifestdt set crft_id = $crft_id where auto = '$rwdt[0]' ";
				else if($crfttype == 'CT')
					$update1 = "update manifestdt set crft_id2 = $crft_id where auto = '$rwdt[0]' ";
				$res1 = mysql_query($update1, $connection);
				
				if($crfttype == 'CRF')
					$update2 = "update containertrace set crf_date = $crftdate, Phase_id = 2 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				else if($crfttype == 'CT')
					$update2 = "update containertrace set crt_date = $crftdate, Phase_id = 3 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				
				$resultup = mysql_query($update2, $connection);
				//echo $update1;echo"<br>";echo$update2;echo"<br>";
			}
			if ($$chk_id == '0'){
				if($crfttype == 'CRF')
					$update1 = "update manifestdt set crft_id = NULL where auto = '$rwdt[0]' ";
				else if($crfttype == 'CT')
					$update1 = "update manifestdt set crft_id2 = NULL where auto = '$rwdt[0]' ";
				$res1 = mysql_query($update1, $connection);
				
				if($crfttype == 'CRF')
					$update2 = "update containertrace set crf_date = NULL, Phase_id = 1 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				else if($crfttype == 'CT')
					$update2 = "update containertrace set crt_date = NULL, Phase_id = 2 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				
				$resultup = mysql_query($update2, $connection);
				//echo $update1;echo"<br>";echo$update2;echo"<br>";
			}
			
		}
		//exit;
		
		
	if($amount != '' && $amount > 0){	
		if ($currency_code == '')	   
		$currency_code = $base1;

		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$currency_code."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$currency_code."' )";
		$result = selectData($connection, $query);
		$rate_dt = $result[0];
		
		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
		$result = selectData($connection, $query);
		$rate_dt1 = $result[0];
		
		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
		$result = selectData($connection, $query);
		$rate_dt2 = $result[0];
		
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$client_code."'";			 
		$sub_result = selectData($connection, $sub_query);
		$PSaVatChkbx = $sub_result[0];
		if($costcent_applyvat==0) $PSaVatChkbx=0;
		$PSaId 		= get_nextsequence('delivery_id','',$connection);
		$vSaRef 	= get_nextsequence('delivery_reference','',$connection);
		
		$manifestpr = "select vessel_id, bol_type from manifest where manifest_auto=$manifest_auto";

		$resultprct  = mysql_query($manifestpr, $connection);
		$ctpr = mysql_fetch_row($resultprct);
		$vessel_id = $ctpr[0];
		$bol_type  = $ctpr[1];
		
		$project_name = $vessel_id.$voy_number.$voy_year;
		
		if ($bol_type=='I') $blt = 'Import';
		if ($bol_type=='E') $blt = 'Export';
		
		$Acc_Query = " SELECT Sale_Acctax1, Sale_Acctax2, Sale_Acctax3, Sale_Acctax4, Sale_Acctax5, if( ifNull(CreditCurdefault,0)=1, CreditCur, '".$currency_code."') As CreditCur, ".
					 " CreditAccount, TipsAccount, VatAccount, CreditAccountVAT, PromotionAccount, TransportAccount, BonusAccount ".
					 " FROM salessetup ";
		$Acc_Result = selectData($connection, $Acc_Query);
		
		$vSaVatAcc = $Acc_Result[8];
		$sub_queryVat = " SELECT GetVATAccount('".$client_code."','".$vSaVatAcc."') from dual ";
		$sub_resultVat = selectData($connection, $sub_queryVat);
		if ($sub_resultVat[0] != '') $vSaVatAcc = $sub_resultVat[0];
		
		$query = " INSERT INTO deliveries (Delivery_ID, Delivery_Reference, Delivery_Date,Client_code,  Currency_Code, Journaldt_valuedate, ".
			" Project_Code, Costcent_code, Delivery_Posted,Delivery_Edit,Delivery_Taxable, Delivery_TaxableBase1, Delivery_TaxableBase2, Delivery_NoneTaxable, Delivery_NoneTaxableBase1, Delivery_NoneTaxableBase2, 
			Delivery_DiscountAmount, Delivery_DiscountBase1, Delivery_DiscountBase2, Delivery_DiscountPer, Delivery_VAT,Delivery_VATBase1, Delivery_VATBase2, Delivery_Total, Delivery_TotalBase1, Delivery_TotalBase2,
			Delivery_Confirmed, Delivery_Mode, Journal_Type, Journal_Id, Delivery_BelongsToGroup, Delivery_Acctax1, Delivery_Acctax2, Delivery_Acctax3, Delivery_Acctax4, Delivery_Acctax5, Delivery_CreditCur, Delivery_CreditAccount, Delivery_TipsAccount, ".
			" Delivery_VatAccount, Delivery_CreditAccountVat, Delivery_PromotionAcc,Delivery_TransportAccount, Delivery_CREDITACCcur, Delivery_CREDITACC, Delivery_VatChkbx, Delivery_BonusAccount,
			User_Account, Delivery_ClientDeliveryDate ) ".
			" VALUES ( ".$PSaId.", '$vSaRef', ".$crftdate.","."  '".$client_code."', "."'".$currency_code."',".$crftdate.",
			'$project_name', '$blt', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0,
			'DI', Null, $FI_BelongsToGrp, '".$Acc_Result[0]."', '".$Acc_Result[1]."', '".$Acc_Result[2]."', '".$Acc_Result[3]."', '".$Acc_Result[4]."', '".$Acc_Result[5]."', '".$Acc_Result[6]."', '".$Acc_Result[7]."', ".
			"          '".$vSaVatAcc."', '".$Acc_Result[9]."', '".$Acc_Result[10]."', '".$Acc_Result[11]."', '".$Acc_Result[5]."', '".$Acc_Result[6]."', '$PSaVatChkbx', '".$Acc_Result[12]."',".
			"  '".$_SESSION['useraccount']."' , ".$crftdate.")";
		
		$result  = mysql_query($query, $connection);
		
		$query = "SELECT Item_Vat, ITEM_MAXQTY, Group_code, ifNull(Item_Production, 0), ifNull(Item_TTCInclude, 0), ifNull(Item_Unit, 0), ifNull(Item_Promotion, 0), ifNull(Item_NonStock,0) ".
				" FROM Items WHERE item_Code = '".$item_code."'";	
		$result = selectData($connection, $query);
		//echo $query;
		$item_vat 	   = $result[0];
		$Item_MaxQty   = $result[1];
		$ItemGroupCode = $result[2];
		$ItemProd      = $result[3];
		$ItemTTCInc    = $result[4];
		$ItemUnit      = $result[5];
		$ItemPromo     = $result[6];
		$ItemNonStock  = $result[7];
		
		//$item_vat = $item_vat/100;
		$tax_flag = 0;
		$notax_flag = 1;
		if ($item_vat > 0){
			$tax_flag = 1;
			$notax_flag = 0;
		}	
				
		$vItemUnitCoef = 0;
		$vItemUnit2Qty = 0;
		
		if ($SalesTTC == 1) {
			$vSaDtPriceTTC      = $amount;
			$vSaDtPriceTTCBase1 = ($vSaDtPriceTTC*$rate_dt)/$rate_dt1;
			$vSaDtPriceTTCBase2 = ($vSaDtPriceTTC*$rate_dt)/$rate_dt2;
			
			if ($PSaVatChkbx == 1)	$vSaDtPrice = ($vSaDtPriceTTC / (1 + $item_vat/100));
			else $vSaDtPrice = $amount;			
							
			//$vSaDtPrice      = ($vSaDtPriceTTC / (1 + $item_vat/100));
			$vSaDtPriceBase1 = ($vSaDtPrice*$rate_dt)/$rate_dt1;
			$vSaDtPriceBase2 = ($vSaDtPrice*$rate_dt)/$rate_dt2;
					
		} else	{
			$vSaDtPrice      = $amount;
			$vSaDtPriceBase1 = ($vSaDtPrice*$rate_dt)/$rate_dt1;
			$vSaDtPriceBase2 = ($vSaDtPrice*$rate_dt)/$rate_dt2;
			
			$vSaDtPriceTTC      = 0;
			$vSaDtPriceTTCBase1 = 0;
			$vSaDtPriceTTCBase2 = 0;
					
		}	
		if ($Module_AdditionalTaxes == 1) {
		
			// Calculation of Item Tax Amounts
			$query = " SELECT ifNull(Client_Tax1,0), ifNull(Client_Tax2,0), ifNull(Client_Tax3,0), ifNull(Client_Tax4,0), ifNull(Client_Tax5,0) ".
					 " FROM Clients ".
					 " WHERE Ledger_number = '".$client_code."'";
								 
			$result = selectData($connection, $query);
			$vCltTax1 = $result[0];
			$vCltTax2 = $result[1];
			$vCltTax3 = $result[2];
			$vCltTax4 = $result[3];
			$vCltTax5 = $result[4];
						
			$query = " SELECT ifNull(Item_Tax1,0), ifNull(Item_Tax2,0), ifNull(Item_Tax3,0), ifNull(Item_Tax4,0), ifNull(Item_Tax5,0) ".
					 " FROM Items ".
					 " WHERE Item_Code = '".$item_code."'";
							 
			$result = selectData($connection, $query);

			
			if ($vCltTax1 != 0)	$vSaDtTax1 = $result[0];
			if ($vCltTax2 != 0)	$vSaDtTax2 = $result[1];
			if ($vCltTax3 != 0)	$vSaDtTax3 = $result[2];
			if ($vCltTax4 != 0)	$vSaDtTax4 = $result[3];
			if ($vCltTax5 != 0)	$vSaDtTax5 = $result[4];
				
		}
		
		// Get DirectStock Accounts
		$query = " SELECT Directstockdebit, Directstockcredit, Directstockcurrency_code, DiscountAccount, CreditAccountVAT, CreditAccount, PromotionAccount, TipsAccount,  ".
				 " CreditAccountNonStock, CreditAccountVatNonStock, VatAccount, VATAccountNonStock, VatAccountPOS, VATAccountNonStockPOS ".
				 " FROM salessetup ";
						 
		$result = selectData($connection, $query);
				
		$vSaStpDirectStkDbt = $result[0];
		$vSaStpDirectStkCrd = $result[1];
		$vSaStpDirectStkCur = $result[2];	
		$vSaStpDiscAcc      = $result[3];	
		$vSaStpCreditVatAcc = $result[4];	
		$vSaStpCreditAcc    = $result[5];	
		$vSaStpPromoAcc     = $result[6];
		$vSaStpTipsAcc      = $result[7];
		
		$vSaStpCreditNonStockAcc    = $result[8];
		$vSaStpCreditVatNonStockAcc = $result[9];
		$vSaStpVatAcc 		  	  	= $result[10];
		$vSaStpVatNonStockAcc 	  	= $result[11];
		$vSaStpVatAccCash	  	  	= $result[12];
		$vSaStpVatNonStockAccCash 	= $result[13];
				
		$queryexp = "select ifnull(client_export,0) from clients where Ledger_number =  '".$client_code."' ";	
		$resexp = mysql_query($queryexp,$connection);
		$rwexp = mysql_fetch_row($resexp);
		$clientexp = $rwexp[0];
		
		$query = " SELECT Trans_DirectStkDbt, Trans_DirectStkCrd, Trans_DirectStkCur, Trans_VATAccount, Trans_NonRMVatAcc, ".
				 " Trans_RMVatAcc, Trans_NonRMTTCIncAcc, Trans_RMTTCIncAcc, Trans_NonRMAcc, Trans_RMAcc, Trans_discountacc, Trans_PromotionAcc, Trans_TipsAcc, trans_export ".
				 " FROM transactionsetup ".
				 " WHERE Journal_type = 'SI' ".
				 " AND Group_code = '".$ItemGroupCode."'  ";
							
		$result = selectData($connection, $query);
		$vTranStpDirectStkDbt = $result[0];
		$vTranStpDirectStkCrd = $result[1];
		$vTranStpDirectStkCur = $result[2];
		$vTranStpVatAcc       = $result[3];
		$vTranStpNonRmVatAcc  = $result[4];
		$vTranStpRmVatAcc     = $result[5];
		$vTranStpNonRmTTCIncAcc  = $result[6];
		$vTranStpRmTTCIncAcc     = $result[7];
		$vTranStpNonRmAcc        = $result[8];
		$vTranStpRmAcc           = $result[9];
		$vTranStpDiscAcc         = $result[10];
		$vTranStpPromoAcc        = $result[11]; 
		$vTranStpTipsAcc         = $result[12]; 
		
		if ($Module_RawMaterial == 1) {
			$vSaDtDirectStkDbt = $vTranStpDirectStkDbt;
			$vSaDtDirectStkCrd = $vTranStpDirectStkCrd;
			$vSaDtDirectStkCur = $vTranStpDirectStkCur;
			$vSaDtVatAcc       = $vTranStpVatAcc;
			$vSaDtDiscAcc      = $vTranStpDiscAcc;
			$vSaDtPromoAcc     = $vTranStpPromoAcc;
			$vSaDtTipsAcc      = $vTranStpTipsAcc;
					
			if ($item_vat > 0 && $PSaVatChkbx == 1) {
				if ($ItemProd == 1)	$vSaDtItemAcc = $vTranStpNonRmVatAcc; else $vSaDtItemAcc = $vTranStpRmVatAcc;
			}
			else {
				if ($ItemProd == 1) {
					if ($ItemTTCInc == 1) $vSaDtItemAcc = $vTranStpNonRmTTCIncAcc; else $vSaDtItemAcc = $vTranStpNonRmAcc;
				}	
				else {
					if ($ItemTTCInc == 1) $vSaDtItemAcc = $vTranStpRmTTCIncAcc; else $vSaDtItemAcc = $vTranStpRmAcc;
				}
			}	
		} 
		else {
			$vSaDtDirectStkDbt = $vSaStpDirectStkDbt;
			$vSaDtDirectStkCrd = $vSaStpDirectStkCrd;
			$vSaDtDirectStkCur = $vSaStpDirectStkCur;		
			//$vSaDtVatAcc       = $vSaStpVatAcc;
			$vSaDtDiscAcc      = $vSaStpDiscAcc;
			$vSaDtPromoAcc     = $vSaStpPromoAcc;
			$vSaDtTipsAcc      = $vSaStpTipsAcc;
					
			if ($item_vat > 0 && $PSaVatChkbx == 1) {
				if ($ItemNonStock == 1)
					$vSaDtItemAcc = $vSaStpCreditVatNonStockAcc;
				else
					$vSaDtItemAcc = $vSaStpCreditVatAcc;
			} else {
				if ($ItemNonStock == 1)
					$vSaDtItemAcc = $vSaStpCreditNonStockAcc;
				else	
					$vSaDtItemAcc = $vSaStpCreditAcc;
			}
				if ($ItemNonStock == 1)
					$vSaDtVatAcc = $vSaStpVatNonStockAcc;
				else	
					$vSaDtVatAcc = $vSaStpVatAcc;
			//}
			
							
		} 
		
		if($clientexp == 1) $vSaDtItemAcc = $result[13]; 
					
		$sub_queryVat = " SELECT GetVATAccount('".$client_code."','".$vSaDtVatAcc."') from dual ";//echo $sub_queryVat;exit;
		$sub_resultVat = selectData($connection, $sub_queryVat);
		if ($sub_resultVat[0] != '') $vSaDtVatAcc = $sub_resultVat[0];
		
		$query_cost = " select ifNull(MeanCostCur('".$item_code."', ".$crftdate.", 1, '', ''),0) MeanCostAmountB1 ,  ".
					  " ifNull(MeanCostCur('".$item_code."', ".$crftdate.", 2, '', ''),0) MeanCostAmountB2 from dual  ";//echo $query_cost;exit;
		$result_cost = selectData($connection, $query_cost);
		
		
		$vSaDtCostBase1  = $result_cost[0];
		$vSaDtCostBase2  = $result_cost[1];
		$vSaDtCost=ConvertCurrency($base1, $currency_code, $crftdate, $vSaDtCostBase1, $connection);
		//$message = $vSaDtCost;
		if (empty($vSaDtCostBase1)) $vSaDtCostBase1 = 0;
		if (empty($vSaDtCostBase2)) $vSaDtCostBase2 = 0;
		
	
		$deliverydt_clientdiscount = 0;
		$deliverydt_itemdiscount = 0;

		// Calculation For Detail Discount Amount
		$vSaDtDiscAmt       = 0;
		$vSaDtDiscAmtBase1  = 0;
		$vSaDtDiscAmtBase2  = 0;
		
		fbegin();
		$txtSaDtId = get_nextsequence('deliverydt_id','',$connection);	
		fcommit();
		$query = " INSERT INTO deliverydt (Project_code, Costcent_code,DeliveryDt_id, Delivery_id, Currency_Code,Delivery_Date, Journal_Type, Client_Code, Warehouse_Code, ".
				 " Item_code, Item_CodePacked, DeliveryDt_Quantity, DeliveryDt_Price, DeliveryDt_PriceBase1, DeliveryDt_PriceBase2, DeliveryDt_DiscountPer, ".
				 " DeliveryDt_DiscountAmount, DeliveryDt_DiscountBase1, DeliveryDt_DiscountBase2, Item_Vat, DeliveryDt_PriceTTC, ".
				 " Deliverydt_priceTTCBase1, Deliverydt_priceTTCBase2, Deliverydt_DirectStkDbt, Deliverydt_DirectStkCrd, Deliverydt_DirectStkCur, ".
				 " Deliverydt_discountacc, Deliverydt_ItemAccount, DeliveryDt_PromoAcc, ".
				 " Deliverydt_BelongsToGroup, Item_Tax1, Item_Tax2, Item_Tax3, Item_Tax4, Item_Tax5, Deliverydt_posted, Deliverydt_OldData, Item_UnitCoef, Item_Unit2Qty, ".
				 " Deliverydt_promotion, SaleDt_Price, SaleDt_PriceBase1, SaleDt_PriceBase2,  Deliverydt_cost, Deliverydt_costbase1, Deliverydt_costbase2, ".
				 " DeliveryDt_TipsAccount, DeliveryDt_VatAccount, User_Account, deliverydt_clientdiscount,deliverydt_itemdiscount) ".
				 " VALUES ( '$project_name','$blt',".$txtSaDtId.", ".$PSaId.", '".$currency_code."', ".$crftdate.", 'DI', '".$client_code."', ".
				 " '".$lstWarehouse."', '".$item_code."', Null, '1', $vSaDtPrice , $vSaDtPriceBase1, $vSaDtPriceBase2, ".
				 " '$itemdiscountper', '$vSaDtDiscAmt', '$vSaDtDiscAmtBase1', '$vSaDtDiscAmtBase2', '$item_vat', ".
				 " '$vSaDtPriceTTC', '$vSaDtPriceTTCBase1', '$vSaDtPriceTTCBase2', '".$vSaDtDirectStkDbt."', ".
				 " '".$vSaDtDirectStkCrd."', '".$vSaDtDirectStkCur."', '".$vSaDtDiscAcc."', '".$vSaDtItemAcc."', '".$vSaDtPromoAcc."', ".
				 " '$FI_BelongsToGrp',  '$vSaDtTax1', '$vSaDtTax2', '$vSaDtTax3', '$vSaDtTax4','$vSaDtTax5', 0, 0, '$vItemUnitCoef', '$vItemUnit2Qty', '$chkSaDtPromo', ".
				 " '$vSaDtPrice', '$vSaDtPriceBase1',	 '$vSaDtPriceBase2' ,'$vSaDtCost', '$vSaDtCostBase2', ".
				 " '$vSaDtCostBase1'    ,  '".$vSaDtTipsAcc."', '".$vSaDtVatAcc."' , '".$_SESSION['useraccount']."','$deliverydt_clientdiscount', '$deliverydt_itemdiscount' ) ";
		
		$result=mysql_query($query,$connection);
		$CalculateResult = Calculate_MasterAmounts($PSaId, $crftdate, $PSaVatChkbx, 0, 0, 0, 0, $rate_dt, $rate_dt1, $rate_dt2, $Setup_VatStartDate, $Setup_VatPercentage, $Module_AdditionalTaxes, $SalesTTC, 'I', $connection);
			
		$message = Create_DeliveryJv($PSaId, $_SESSION['dbName'], $_SESSION['dbLabel'], $_SESSION['useraccount'], $_SESSION['username']); 
		
		$querydelivery="update CRFT_Header set delivery_id = '$PSaId' where crft_id='$crft_id';";
		$result=mysql_query($querydelivery,$connection);
	}
		
		
	} else {
		$message  = mysql_error($connection); 
		frollback();
	}	
	 
}


if($mode == 'editRecord' && $crft_id != '' && $ErrorExist == 0){
	fbegin();
	$queryup = " update CRFT_Header set BL_number = '$BL_number', voy_number = '$voy_number', voy_year = '$voy_year', extra_info = $extra_info, 
					crft_date = $crftdate where crft_id = '$crft_id' ";
	$result = mysql_query($queryup, $connection);
	//$result=1;
	if($result){
		fcommit();
		$querydt = "select auto, crft_id  from manifestdt where manifest_manifestauto = $manifest_auto and (ifnull(ctrflag,'') <> '*' or ifnull(detailtype,'') = 1) order by detailsequence; ";
		$resdt = mysql_query($querydt, $connection);
		while($rwdt = mysql_fetch_array($resdt)){
			$chk_id = 'chk_id'.$rwdt[0];
			if ($$chk_id == '') $$chk_id = 0;
			
			if ($$chk_id == '1'){
				if($crfttype == 'CRF')
					$update1 = "update manifestdt set crft_id = $crft_id where auto = '$rwdt[0]' ";
				else if($crfttype == 'CT')	
					$update1 = "update manifestdt set crft_id2 = $crft_id where auto = '$rwdt[0]' ";
				$res1 = mysql_query($update1, $connection);
				
				if($crfttype == 'CRF')
					$update2 = "update containertrace set crf_date = $crftdate, Phase_id = 2 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				else if($crfttype == 'CT')
					$update2 = "update containertrace set crt_date = $crftdate, Phase_id = 3 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				
				$resultup = mysql_query($update2, $connection);
				//echo $update1;echo"<br>";echo$update2;echo"<br>";
			}
			if ($$chk_id == '0'){
				if($crfttype == 'CRF')
					$update1 = "update manifestdt set crft_id = NULL where auto = '$rwdt[0]' ";
				else if($crfttype == 'CT')
					$update1 = "update manifestdt set crft_id2 = NULL where auto = '$rwdt[0]' ";
				$res1 = mysql_query($update1, $connection);
				
				if($crfttype == 'CRF')
					$update2 = "update containertrace set crf_date = NULL, Phase_id = 1 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				else if($crfttype == 'CT')
					$update2 = "update containertrace set crt_date = NULL, Phase_id = 2 where manifest_manifestauto = $manifest_auto and manifestdt_auto = '$rwdt[0]';";
				
				$resultup = mysql_query($update2, $connection);
				//echo $update1;echo"<br>";echo$update2;echo"<br>";
			}
			
		}
		
	} else 
		$message = mysql_error();
}

if ($mode == 'deletecrft'){
	$crfttype = $_GET['crfttype'];
	$crft_id  = $_GET['crft_id'];
	
	$ErrorExist = 0;
	$manifest = "select manifest_auto from crft_header where crft_id = $crft_id ";
	$resman = mysql_query($manifest, $connection);
	$rowman = mysql_fetch_row($resman);
	$manifest_auto = $rowman[0];
	
	if($crfttype == 'CRF')
		$update2 = "update containertrace set crf_date = NULL, Phase_id = 1 
					where manifest_manifestauto = $manifest_auto and manifestdt_auto in (select auto from manifestdt where manifest_manifestauto = $manifest_auto and crft_id = $crft_id ) ";
	else if($crfttype == 'CT')
		$update2 = "update containertrace set crt_date = NULL, Phase_id = 2 
					where manifest_manifestauto = $manifest_auto and manifestdt_auto in (select auto from manifestdt where manifest_manifestauto = $manifest_auto and crft_id2 = $crft_id ) ";
	
	$resultup = mysql_query($update2, $connection);
	if (!$resultup){
			$message = mysql_error();
			$ErrorExist = 1;
		}		
	
	if ($ErrorExist == 0){
		if($crfttype == 'CRF')
			$update1 = "update manifestdt set crft_id = NULL where crft_id = $crft_id ";
		else if($crfttype == 'CT')
			$update1 = "update manifestdt set crft_id2 = NULL where crft_id2 = $crft_id ";
		$res1 = mysql_query($update1, $connection);
		if (!$res1){
			$message = mysql_error();
			$ErrorExist = 1;
		}	
	}
	if ($ErrorExist == 0){
		$del = "delete from crft_header where crft_id = $crft_id ";
		$res = mysql_query($del, $connection);
	}
	
}
function Create_DeliveryJv($PSaId, $DbName, $DbLabel, $UserAccount, $UserName) {

	include("config.inc.php");
	
	$conn = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
	mysql_select_db($DbName, $conn) or die("Could not find DB");

	fbegin();
	$resultvars = mysql_query("set @id :=0 ", $conn);

	$query = " call Create_DeliveryAutoJv(".$PSaId.", 0, '".$DbName."', '".$DbLabel."', '".$UserAccount."', '".$UserName."', 0, @id) ";
//echo $query; 
//exit;
	$result = mysql_query($query,$conn);
    $error = mysql_error();
	
	$query1 = " select @id from dual ";
	$result1 = mysql_query($query1,$conn) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	

	fcommit();
	
	
    if ($cur1[0] == -2)
	   $error = 'Error In Procedure Create_DeliveryAutoJv: '.$error;
	else if  ($cur1[0] == -1)  
	   $error = 'Error In Procedure Create_Jv: '.$error;
	else if  ($error != '')  
		$error = 'Error In Function Create_DeliveryJv: '.$error;

	Return $error;
	

}
function Calculate_MasterAmounts($PId, $PDate, $PVatChk, $PDiscPer, $PDiscAmt, $PTransportAmt, $PBonusAmt, $PRate, $PRate1, $PRate2, $PSetupVatStartDate, $PSetupVatPercentage, $PModuleAdditionalTaxes, $PSalesTTC, $PMode, $connection) {

	$vSaDtTotal = 0;
	$vItemVat    = 0;
	
	$vSaTaxable        = 0;
	$vSaTaxableTTC     = 0;
	$vSaNoneTaxable    = 0;
	$vSaGrossTotal     = 0;
	$vSaDiscountAmt    = 0;
	$vSaDiscountPer    = 0;
	$vSaDiscountAmtTTC = 0;
	$vSaTransportAmt   = 0;
	$vSaTransportAmtB1 = 0;
	$vSaTransportAmtB2 = 0;
	$vSaTransportAmtTTC = 0;
	$vSaBonusAmt   	    = 0;
	$vSaBonusAmtB1      = 0;
	$vSaBonusAmtB2      = 0;
	$vSaBonusAmtTTC     = 0;
	$vSaVat      = 0;
	$vSaVatBase1 = 0;
	$vSaVatBase2 = 0;
		
	$vSaTax1 = 0;
	$vSaTax2 = 0;
	$vSaTax3 = 0;
	$vSaTax4 = 0;
	$vSaTax5 = 0;
	$vSaTax1B1 = 0;
	$vSaTax2B1 = 0;
	$vSaTax3B1 = 0;
	$vSaTax4B1 = 0;
	$vSaTax5B1 = 0;
	$vSaTax1B2 = 0;
	$vSaTax2B2 = 0;
	$vSaTax3B2 = 0;
	$vSaTax4B2 = 0;
	$vSaTax5B2 = 0;
	
	$vSaTotalWeight = 0;

	$vSaDiscountPer = $PDiscPer;
	
	// In Case Not Save, Then Get The Vat CheckBox From Table Sorder
	if ($PMode != 'P') {
		$sub_query = "Select ifNull(Delivery_VatChkbx, 0) From Deliveries Where Delivery_ID = '".$PId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		$PVatChk    = $sub_result[0];
	
	}

	if ($PSalesTTC ==1 && $PVatChk == 0 && $PMode == 'P') {

		$query = " UPDATE DeliveryDt SET  ".
				 " DeliveryDt_Price = DeliveryDt_PriceTTC, ".
				 " DeliveryDt_PriceBase1 = DeliveryDt_PriceTTCBase1, ".
				 " DeliveryDt_PriceBase2 = DeliveryDt_PriceTTCBase2 ".
				 " Where Delivery_ID = ".$PId;

		//echo $query; exit;
		$result=mysql_query($query,$connection);
	}
	else if ($PSalesTTC ==1 && $PVatChk == 1 && $PMode == 'P') {

		$query = " UPDATE DeliveryDt SET  ".
				 " DeliveryDt_Price      = (DeliveryDt_PriceTTC / (1 + Item_Vat/100)), ".
				 " DeliveryDt_PriceBase1 = (DeliveryDt_PriceTTCBase1 / (1 + Item_Vat/100)), ".
				 " DeliveryDt_PriceBase2 = (DeliveryDt_PriceTTCBase2 / (1 + Item_Vat/100)) ".
				 " Where Delivery_ID = ".$PId;

		//echo $query; exit;
		$result=mysql_query($query,$connection);
	}	
	
	
	//Calculating Master Amounts
	$sub_query = " Select DeliveryDt.Item_Code, ((ifNull(DeliveryDt_Quantity,0)*ifNull(DeliveryDt_Price,0))-((ifNull(DeliveryDt_Quantity,0)*ifNull(DeliveryDt_Price,0)*ifNull(DeliveryDt_DiscountPer,0))/100)) As Total, ifNull(DeliveryDt.Item_Vat,0),
				   deliveryDt_Quantity, (ifNull(DeliveryDt_DiscountAmount,0) - ifNull(DeliveryDt_TransportAmount,0) - ifNull(DeliveryDt_BonusAmount,0)), ifNull(DeliveryDt.Item_Tax1,0), ifNull(DeliveryDt.Item_Tax2,0), ifNull(DeliveryDt.Item_Tax3,0), ifNull(DeliveryDt.Item_Tax4,0), ifNull(DeliveryDt.Item_Tax5,0),
				   ifNull(item_weight,0),ifNull(DeliveryDt_Quantity,0) ".
				 " From DeliveryDt ".
				 " left join items on items.item_code = DeliveryDt.Item_Code ".
				 " Where Delivery_ID = ".$PId;
										 
	//echo $sub_query;
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
	while($sub_rows=mysql_fetch_array($sub_result)){

		$vSaDtTotal = $sub_rows[1];
		$vItemVat   = $sub_rows[2];
						
		$tax_flag = 0;
		$notax_flag = 1;
		if ($vItemVat > 0 && $PVatChk == 1){
			$tax_flag = 1;
			$notax_flag = 0;
		}	
						
							
		$vSaTaxable     = $vSaTaxable     + ($vSaDtTotal*$tax_flag);
		$vSaNoneTaxable = $vSaNoneTaxable + ($vSaDtTotal*$notax_flag);
						
		
		// Calculation of Vat Amount
		if ((($PDate > $PSetupVatStartDate && $vItemVat > 0) || ($PModuleAdditionalTaxes == 1))  && $PVatChk == 1 && $vSaDtTotal > 0)
			$vSaVat = $vSaVat + (($vSaDtTotal - $sub_rows[4])*$vItemVat/100);
						
		if ($PModuleAdditionalTaxes == 1 && $vSaDtTotal > 0) {
			if ($vItemVat > 0 && $sub_rows[5] != 0)
				$vSaTax1 = $vSaTax1 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[5]/100);
			if ($vItemVat > 0 && $sub_rows[6] != 0)
				$vSaTax2 = $vSaTax2 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[6]/100);
			if ($vItemVat > 0 && $sub_rows[7] != 0)
				$vSaTax3 = $vSaTax3 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[7]/100);
			if ($vItemVat > 0 && $sub_rows[8] != 0)
				$vSaTax4 = $vSaTax4 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[8]/100);
			if ($vItemVat > 0 && $sub_rows[9] != 0)
				$vSaTax5 = $vSaTax5 + (($vSaDtTotal - $sub_rows[4])*$sub_rows[9]/100);	
		}
		$vSaTotalWeight = $vSaTotalWeight + ($sub_rows[10]*$sub_rows[11]);
					
	}			
					
				
	$vSaGrossTotal = $vSaTaxable + $vSaNoneTaxable;
					
	// Calculation of Discount Amount	
	if ($PSalesTTC == 1) {
		if ($PMode == 'P') {
			$vSaDiscountAmtTTC  = $PDiscAmt;					
			$vSaDiscountAmt     = ($vSaDiscountAmtTTC / (1 + $PSetupVatPercentage/100));
		} else {
			if ($vSaDiscountPer > 0) {
				$vSaDiscountAmt     = (($vSaGrossTotal * $vSaDiscountPer) / 100);
				$vSaDiscountAmtTTC  = ($vSaDiscountAmt * (1 + $PSetupVatPercentage/100));
			}	
		}
		
		$vSaTransportAmtTTC = $PTransportAmt;					
		$vSaTransportAmt    = ($vSaTransportAmtTTC / (1 + $PSetupVatPercentage/100));
		$vSaBonusAmtTTC 	= $PBonusAmt;					
		$vSaBonusAmt    	= ($vSaBonusAmtTTC / (1 + $PSetupVatPercentage/100));
		
		if ($PVatChk == 1)	$vSaTaxableTTC = ($vSaTaxable * (1 + $PSetupVatPercentage/100));
		else $vSaTaxableTTC = $vSaTaxable;
	} else	{
		if ($PMode == 'P') {
			$vSaDiscountAmt      = $PDiscAmt;					
		} else {
			if ($vSaDiscountPer > 0) {
				$vSaDiscountAmt     = (($vSaGrossTotal * $vSaDiscountPer) / 100);
			}	
		}
		
		$vSaTransportAmt    = $PTransportAmt;
		$vSaTransportAmtTTC = 0;
		$vSaBonusAmt    	= $PBonusAmt;
		$vSaBonusAmtTTC 	= 0;
		$vSaTaxableTTC 		= 0;
		$vSaDiscountAmtTTC  = 0;
	}
	
			
					
	$vSaNetTotal         = $vSaGrossTotal + $vSaVat - $vSaDiscountAmt;
	$vSaTaxableBase1     = ($vSaTaxable*$PRate)/$PRate1;
	$vSaTaxableBase2     = ($vSaTaxable*$PRate)/$PRate2;
	$vSaNoneTaxableBase1 = ($vSaNoneTaxable*$PRate)/$PRate1;
	$vSaNoneTaxableBase2 = ($vSaNoneTaxable*$PRate)/$PRate2;
	$vSaDiscountAmtBase1 = ($vSaDiscountAmt*$PRate)/$PRate1;
	$vSaDiscountAmtBase2 = ($vSaDiscountAmt*$PRate)/$PRate2;
	$vSaGrossTotalBase1  = ($vSaGrossTotal*$PRate)/$PRate1;
	$vSaGrossTotalBase2  = ($vSaGrossTotal*$PRate)/$PRate2;
	$vSaVatBase1 		 = ($vSaVat*$PRate)/$PRate1;
	$vSaVatBase2 		 = ($vSaVat*$PRate)/$PRate2;
	$vSaTransportAmtB1   = ($vSaTransportAmt*$PRate)/$PRate1;
	$vSaTransportAmtB2   = ($vSaTransportAmt*$PRate)/$PRate2;	
	$vSaBonusAmtB1 	     = ($vSaBonusAmt*$PRate)/$PRate1;
	$vSaBonusAmtB2	     = ($vSaBonusAmt*$PRate)/$PRate2;	
					

	$vSaTax1B1  = ($vSaTax1*$PRate)/$PRate1;
	$vSaTax1B2  = ($vSaTax1*$PRate)/$PRate2;		
	$vSaTax2B1  = ($vSaTax2*$PRate)/$PRate1;
	$vSaTax2B2  = ($vSaTax2*$PRate)/$PRate2;		
	$vSaTax3B1  = ($vSaTax3*$PRate)/$PRate1;
	$vSaTax3B2  = ($vSaTax3*$PRate)/$PRate2;		
	$vSaTax4B1  = ($vSaTax4*$PRate)/$PRate1;
	$vSaTax4B2  = ($vSaTax4*$PRate)/$PRate2;		
	$vSaTax5B1  = ($vSaTax5*$PRate)/$PRate1;
	$vSaTax5B2  = ($vSaTax5*$PRate)/$PRate2;	
	
	fbegin();
		
	//echo $delivery_date;exit;
	$sub_query1 = " UPDATE deliveries SET ".
				  " Delivery_Taxable = $vSaTaxable, Delivery_TaxableBase1 = $vSaTaxableBase1, Delivery_TaxableBase2 = $vSaTaxableBase2, ".
				  " Delivery_NoneTaxable = $vSaNoneTaxable, Delivery_NoneTaxableBase1 = $vSaNoneTaxableBase1, Delivery_NoneTaxableBase2 = $vSaNoneTaxableBase2, ".
				  " Delivery_DiscountAmount = $vSaDiscountAmt, Delivery_DiscountBase1 = $vSaDiscountAmtBase1, Delivery_DiscountBase2 = $vSaDiscountAmtBase2, ".
				  " Delivery_DiscountPer = $vSaDiscountPer, Delivery_VAT = $vSaVat, Delivery_VATBase1 = $vSaVatBase1, Delivery_VATBase2 = $vSaVatBase2, ".
				  " Delivery_VatChkbx = $PVatChk, ".
				  " Delivery_Total = $vSaGrossTotal, Delivery_TotalBase1 = $vSaGrossTotalBase1, Delivery_TotalBase2 = $vSaGrossTotalBase2, ".
				  " Delivery_TransportAmount = $vSaTransportAmt, Delivery_TransportAmountBase1 = $vSaTransportAmtB1, Delivery_TransportAmountBase2 = $vSaTransportAmtB2, Delivery_TransportTTC = $vSaTransportAmtTTC, ".
				  " Delivery_BonusAmount = $vSaBonusAmt, Delivery_BonusBase1 = $vSaBonusAmtB1, Delivery_BonusBase2 = $vSaBonusAmtB2, Delivery_BonusTTC = $vSaBonusAmtTTC, ".
				  " Delivery_DiscountTTC = $vSaDiscountAmtTTC, Delivery_TaxableTTC = $vSaTaxableTTC,  ".
				  " Delivery_Tax1 = $vSaTax1, Delivery_Tax1Base1 = $vSaTax1B1, Delivery_Tax1Base2 = $vSaTax1B2,  ".
				  " Delivery_Tax2 = $vSaTax2, Delivery_Tax2Base1 = $vSaTax2B1, Delivery_Tax2Base2 = $vSaTax2B2,  ".
				  " Delivery_Tax3 = $vSaTax3, Delivery_Tax3Base1 = $vSaTax3B1, Delivery_Tax3Base2 = $vSaTax3B2,  ".
				  " Delivery_Tax4 = $vSaTax4, Delivery_Tax4Base1 = $vSaTax4B1, Delivery_Tax4Base2 = $vSaTax4B2,  ".
				  " Delivery_Tax5 = $vSaTax5, Delivery_Tax5Base1 = $vSaTax5B1, Delivery_Tax5Base2 = $vSaTax5B2,  ".
				  " totalWeight = $vSaTotalWeight ".
				  " WHERE Delivery_ID = ".$PId;
		//echo $sub_query1."<br>";exit;
	$sub_result1=mysql_query($sub_query1,$connection);
	
	if ($sub_result1){
		fcommit();
						
	}else{
		$message  = mysql_error(); 
		$error = 'Error In Function Calculate_MasterAmounts: '.$message;
		frollback();
	}
			
	Return $error;		

}


echo "<form name='crftaction' method='POST' action=''>
	<input type='hidden' name='message' value='$message'>";
if($message != '' && ($mode == 'submitRecord' || $mode == 'editRecord'))
echo "<input type='hidden' name='showinfo' value='1'>
	<input type='hidden' name='manifest_auto' value='$manifest_auto'>
	<input type='hidden' name='extra_info' value='".htmlspecialchars($temp_extrinfo, ENT_QUOTES)."'>
	<input type='hidden' name='crftdate' value='$temp_crftdate'>
	<input type='hidden' name='voy_year' value='$voy_year'>";
echo "</form>";

echo "<script> ";

if($mode == 'submitRecord' || $mode == 'editRecord')
		echo "document.crftaction.action = 'Form_CRFT.php?type=$crfttype';";
else if ($mode == 'deletecrft')
	echo "document.crftaction.action = 'Form_CRFTSearch.php?type=$crfttype';";
echo "	document.crftaction.submit();
	</script>";
exit;
?>














