<?php

session_start();

if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.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");


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


//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_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter, ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject ".
			 " From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
						
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<script type="text/javascript" language="javascript">


</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>
<form name='inserttrans' method=''>
<input type='text' name='mode' value='$mode'>
<input type='text' name='mainmode' value='$mainmode'>
<input type='text' name='SPrePayMId' value='$SPrePayMId'>
<input type='text' name='SPrePayDId' value='$SPrePayDId'>
<input type='text' name='SGenDate' value='$SGenDate'>

HERE;

$message='';

 
if ($mode == "submitrecord" || $mode == "editdt"){
	
	$AllowInsert = 1;
	$ErrorExists = 0;
	$message  = '';
	
	//Insert Master Table
	if ($SPrePayMId == ""){
		
		if ($AllowInsert == 1) {
			//Check Ledger No If Already Exists
			$query = " Select Count(*) ".
					 " From PrepaymentM ".
					 " Where Ledger_Number = '".$txtLedgerNo."' ";

			$result = selectData($connection, $query);
			$LedgerExists = $result[0];
			
			if ($LedgerExists > 0 ){
				$message  = 'Insert not allowed, Duplicate ledger number.';
				$AllowInsert = 0;
			}
		}	
				
		if ($AllowInsert == 1) {
		
			fbegin();	
			$vPrePayMId = get_nextsequence('PrePaymentM_Id','',$connection);	
			fcommit();
			
			 
			fbegin();
			$query = " INSERT INTO Temp_PrepaymentM (PrePaymentM_Id, Ledger_Number, PrePayment_dbcd, PrePayment_TotalB1, PrePayment_TotalB2, ".
					 " PrePayment_RemainB1, PrePayment_RemainB2, PrePayment_Edit, Synchronized, Logged_Code ) ".
					 " VALUES ( ".$vPrePayMId.", '".$txtLedgerNo."', '".$txtJourType."', 0, 0, 0, 0, 0, 0, '".$HTTP_SESSION_VARS['useraccount']."' )";
						 
				 
			$result=mysql_query($query,$connection);
			//echo $query."<br>";exit;	
			
			if ($result){
				$SPrePayMId = $vPrePayMId;
				fcommit();
			}else{
				$SPrePayMId = "";
				$message  = mysql_error(); 
				frollback();
			}
		}
	}
	
	//Insert Detail Tables
	if ($SPrePayMId != ""){
	
		if (empty($txtAmountD))   $txtAmountD = 0;
		if (empty($txtGenerate))  $txtGenerate = 0;
		if (!isset($txtCurrD))    $txtCurrD = $base1;
			
		if (empty($txtCostCentCode))	$CostCentCode = 'Null';
		else		$CostCentCode = "'".$txtCostCentCode."'";
		if (empty($txtProjectCode))		$ProjectCode = 'Null';
		else		$ProjectCode = "'".$txtProjectCode."'";
		
		if (empty($txtCostCentCodeD))	$CostCentCodeD = 'Null';
		else		$CostCentCodeD = "'".$txtCostCentCodeD."'";
		if (empty($txtProjectCodeD))	$ProjectCodeD = 'Null';
		else		$ProjectCodeD = "'".$txtProjectCodeD."'";

    		
		$vLedgerNo     = '';	
		$vLedgerNoD    = '';
		$vDescriptionD = '';
		$vDateD 	   = '';
		$vCurrD 	   = '';
		$vAmountD    = 0;
		$vAmountB1D  = 0;
		$vAmountB2D  = 0;
			
		$vLedgerNo     = $txtLedgerNo;
		$vLedgerNoD    = $txtLedgerNoD;
		$vDescriptionD = $txtDescriptionD;
		$vDateD 	   = $txtDate;
		$vCurrD        = $txtCurrD;
		$vAmountD      = $txtAmountD;
		$vGenerate     = $txtGenerate;
		$vDivisionD    = $txtDivisionD;
		
		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$vCurrD."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$vCurrD."' )";
		$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];	
					
		
		$vAmountB1D    = ($vAmountD*$rate_dt)/$rate_dt1;
		$vAmountB2D    = ($vAmountD*$rate_dt)/$rate_dt2;
					
		if ($SPrePayDId == "" ){			
			fbegin();
			$vPrePayDId = get_nextsequence('PrePaymentD_Id','',$connection);	
			fcommit();

				
			fbegin();
			$query = " INSERT INTO Temp_PrepaymentD (PrePaymentD_Id, PrePaymentM_Id, Ledger_Number, Ledger_NumberM, Description, Date1, DateGenerate, Division, ". 
					 " DivisionGenerate, Project_Code, Costcent_Code, Project_CodeD, Costcent_CodeD, Currency_Code, Amount, AmountGenerate, AmountBase1, ".
					 " AmountGenerateBase1, AmountBase2, AmountGenerateBase2, Generate, Synchronized, Logged_Code )  ".
					 " VALUES ( ".$vPrePayDId.", ".$SPrePayMId.", '".$vLedgerNoD."', '".$vLedgerNo."', '".ereg_replace("'", "\'", trim($vDescriptionD))."', ".
					 "          '".insertMySQLDate($vDateD)."', Null, '".ereg_replace("'", "\'", trim($vDivisionD))."', Null, $ProjectCode, $CostCentCode, ".
					 "          $ProjectCodeD, $CostCentCodeD, '".$vCurrD."', $vAmountD, 0, $vAmountB1D, 0, $vAmountB2D, 0, $vGenerate, 0, '".$HTTP_SESSION_VARS['useraccount']."'	) ";
		
		} else if ($SPrePayDId != "") {
			fbegin();
			$query = " UPDATE Temp_PrepaymentD SET ".
					 " Currency_Code = '".$vCurrD."', Ledger_Number = '".$vLedgerNoD."', Description = '".ereg_replace("'", "\'", trim($vDescriptionD))."', ". 
					 " Date1 = '".insertMySQLDate($vDateD)."', Division = '".ereg_replace("'", "\'", trim($vDivisionD))."', Amount = $vAmountD, ".
					 " AmountBase1 = $vAmountB1D, AmountBase2 = $vAmountB2D,  Generate = $vGenerate, Project_Code = $ProjectCode, Costcent_Code = $CostCentCode, ".
					 " Project_CodeD = $ProjectCodeD, Costcent_CodeD = $CostCentCodeD  ".
					 " WHERE PrePaymentD_Id = '".$SPrePayDId."'";
		
		}
		//echo $query."<br>";exit;				
		$result=mysql_query($query,$connection);
		
		if ($result){
		
			$vTotalAmountB1 = 0;
			$vTotalAmountB2 = 0;
			$vTotalRemainB1 = 0;
			$vTotalRemainB2 = 0;
			
			$sub_query = " Select ifNull(Sum(ifNull(AmountBase1,0)),0) As TotalAmountBase1, ifNull(Sum(ifNull(AmountBase2,0)),0) As TotalAmountBase2, ".
						 " ifNull(Sum(ifNull(AmountBase1,0) - ifNull(AmountGenerateBase1,0)),0) As TotalRemainBase1, ifNull(Sum(ifNull(AmountBase2,0) - ifNull(AmountGenerateBase1,0)),0) As TotalRemainBase2 ".
						 " From Temp_PrepaymentD ".
						 " Where PrePaymentM_Id = ".$SPrePayMId;
			$sub_result = selectData($connection, $sub_query);
			
			$vTotalAmountB1 = $sub_result[0];
			$vTotalAmountB2 = $sub_result[1];
			$vTotalRemainB1 = $sub_result[2];
			$vTotalRemainB2 = $sub_result[3];
			
				 
			$sub_query1 = " UPDATE Temp_PrepaymentM SET ".
						  " PrePayment_TotalB1 = $vTotalAmountB1, PrePayment_TotalB2 = $vTotalAmountB2, ".
						  " PrePayment_RemainB1 = $vTotalRemainB1, PrePayment_RemainB2 = $vTotalRemainB2 ".
						  " WHERE PrePaymentM_Id = ".$SPrePayMId;
				 
			$sub_result1=mysql_query($sub_query1,$connection);
			//echo $sub_query1."<br>";exit;

				
						 
			fcommit();
		} else{
			$message  = mysql_error(); 
			frollback();
		}	
		
		
	} // End Case Insert In Detail Tables
	

} 


	
//Delete Detail Mode
else if ($mode == "deletedt" && $SPrePayDId != "" && $SPrePayMId != ""){

	$AllowDelete = 1;
	$ErrorExists = 0;
	$message  = '';
	
	$vTotalAmountB1 = 0;
	$vTotalAmountB2 = 0;
	$vTotalRemainB1 = 0;
	$vTotalRemainB2 = 0;
	
	
	if ($AllowDelete == 1) {
		//Check If Record Has Generated Amount > 0 
		$query = " Select Count(*) ".
				 " From Temp_PrepaymentD As PrepaymentD ".
				 " Where PrepaymentD.PrePaymentD_Id = '".$SPrePayDId."' ".
				 " And ifNull(PrepaymentD.AmountGenerateBase1,0) > 0  ";

		$result = selectData($connection, $query);
		$RecordIsGenerated = $result[0];
		
		if ($RecordIsGenerated > 0 ){
			$message  = 'Delete not allowed, Ledger has related JV.';
			$AllowDelete = 0;
		}
	}	
	

		
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {
		
		fbegin();
		
		// Delete From TempPrepaymentD For Selected Record
		$query = " DELETE FROM Temp_PrepaymentD ".
				 " WHERE PrePaymentD_Id = ".$SPrePayDId;
				 //" And Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
					 
		if (!updateData($connection, $query, true)) {
			$message  = mysql_error();
			$ErrorExists = 1;
			frollback();
		}  
		else {
			fcommit();
		}
			
			
	
			
		if ( $ErrorExists == 0 ){ 
			
			$sub_query = " Select ifNull(Sum(ifNull(AmountBase1,0)),0) As TotalAmountBase1, ifNull(Sum(ifNull(AmountBase2,0)),0) As TotalAmountBase2, ".
						 " ifNull(Sum(ifNull(AmountBase1,0) - ifNull(AmountGenerateBase1,0)),0) As TotalRemainBase1, ifNull(Sum(ifNull(AmountBase2,0) - ifNull(AmountGenerateBase1,0)),0) As TotalRemainBase2 ".
						 " From Temp_PrepaymentD ".
						 " Where PrePaymentM_Id = ".$SPrePayMId;
			$sub_result = selectData($connection, $sub_query);
			//echo $sub_query;exit;
			$vTotalAmountB1 = $sub_result[0];
			$vTotalAmountB2 = $sub_result[1];
			$vTotalRemainB1 = $sub_result[2];
			$vTotalRemainB2 = $sub_result[3];
			
				 
			$sub_query1 = " UPDATE Temp_PrepaymentM SET ".
						  " PrePayment_TotalB1 = $vTotalAmountB1, PrePayment_TotalB2 = $vTotalAmountB2, ".
						  " PrePayment_RemainB1 = $vTotalRemainB1, PrePayment_RemainB2 = $vTotalRemainB2 ".
						  " WHERE PrePaymentM_Id = ".$SPrePayMId;
				 
			$sub_result1=mysql_query($sub_query1,$connection);
			//echo $sub_query1."<br>";exit;
				
			if ( $sub_result1 ){ 
				fcommit();
			} else
			{
			   $message  = mysql_error(); 
			   frollback();
			} 
		} // End If No Error Exists 
			
			
	
	} // End If No Constraints Found On Delete
	
	
}


//Delete Record Mode From Main Tables
else if ($mode == "" && $mainmode == "delete" && $SPrePayMId != ''){

	$AllowDelete = 1;
	$message  = '';
	
	
	if ($AllowDelete == 1) {
		//Check Each Record If Has Generated Amount > 0 
		$query = " Select Count(*) ".
				 " From PrepaymentD ".
				 " Where PrepaymentD.PrePaymentM_Id = '".$SPrePayMId."' ".
				 " And ifNull(PrepaymentD.AmountGenerateBase1,0) > 0  ";

		$result = selectData($connection, $query);
		$RecordIsGenerated = $result[0];
		
		if ($RecordIsGenerated > 0 ){
			$message  = 'Delete not allowed, Ledger(s) has related JV.';
			$AllowDelete = 0;
		}
	}	
	

	
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {
	
		$query = " DELETE FROM PrePaymentD ".
				 " WHERE PrePaymentM_Id  = '".$SPrePayMId."' ";
					 
		if (updateData($connection, $query, true)) {
			// Delete From PrePaymentM For Selected Record
			$query = " DELETE FROM PrePaymentM ".
					 " WHERE PrePaymentM_Id = '".$SPrePayMId."' ";
				
			if (!updateData($connection, $query, true))		 
				$message  = mysql_error(); 
				

		}
		else
			$message  = mysql_error(); 	
		
	}
	
	
}

//Delete Record Mode From Temp Tables
else if ($mode == "" && $mainmode == "deletepending" && $SPrePayMId != ''){

	// Delete From Temp Tables Already Filled By Search
	$query = " DELETE FROM Temp_PrePaymentD ".
			 " WHERE PrePaymentM_Id  = '".$SPrePayMId."' ";

	if (!updateData($connection, $query, true))		 
      $message  = mysql_error(); 
	  
	  
	$query = " DELETE FROM Temp_PrePaymentM ".
			 " WHERE PrePaymentM_Id = '".$SPrePayMId."' ";

	if (!updateData($connection, $query, true))
      $message  = mysql_error();   
	  

}

//Search Mode	
else if($mainmode == "search" && $SPrePayMId != ''){

	$ErrorExists = 0;
	//Copy Data From Main Tables To Temp Tables
	$query = " INSERT INTO Temp_PrepaymentM (PrePaymentM_Id, Ledger_Number, PrePayment_dbcd, PrePayment_TotalB1, PrePayment_TotalB2, ".
	         " PrePayment_RemainB1, PrePayment_RemainB2, PrePayment_Edit, Logged_Code ) ".
			 " SELECT PrePaymentM_Id, Ledger_Number, PrePayment_dbcd, PrePayment_TotalB1, PrePayment_TotalB2, ".
	         " PrePayment_RemainB1, PrePayment_RemainB2, PrePayment_Edit, '".$HTTP_SESSION_VARS['useraccount']."' ".
			 " FROM PrepaymentM ".
			 " WHERE PrePaymentM_Id = '".$SPrePayMId."'";
	
	if (!updateData($connection, $query, true)) {
	  $ErrorExists = 1;
      $message  = mysql_error(); 	
	}  

	if ($ErrorExists == 0) {
		//echo $message.$query; exit;				 
		$query = " INSERT INTO Temp_PrepaymentD (PrePaymentD_Id, PrePaymentM_Id, Ledger_Number, Ledger_NumberM, Description, Date1, DateGenerate, Division, DivisionGenerate, ". 
				 " Project_Code, Costcent_Code, Project_CodeD, Costcent_CodeD, Currency_Code, Amount, AmountGenerate, AmountBase1, ".
				 " AmountGenerateBase1, AmountBase2, AmountGenerateBase2, Generate, Synchronized, Logged_Code )  ".
				 " SELECT PrePaymentD_Id, PrePaymentM_Id, Ledger_Number, Ledger_NumberM, Description, Date1, DateGenerate, Division, DivisionGenerate, ". 
				 " Project_Code, Costcent_Code, Project_CodeD, Costcent_CodeD, Currency_Code, Amount, AmountGenerate, AmountBase1, ".
				 " AmountGenerateBase1, AmountBase2, AmountGenerateBase2, Generate, Synchronized, '".$HTTP_SESSION_VARS['useraccount']."'  ".
				 " FROM PrepaymentD ".
				 " WHERE PrepaymentD.PrePaymentM_Id = '".$SPrePayMId."'";
		
		if (!updateData($connection, $query, true))
		  $message  = mysql_error(); 			


	}
}

else if($mode == "generate" && $SGenDate != ''){  

	// Delete From Temp Tables Before Generate
	$query = " DELETE FROM Temp_PrePaymentD ";

	if (!updateData($connection, $query, true))		 
		$message  = mysql_error(); 
	  
	$query = " DELETE FROM Temp_PrePaymentM ";

	if (!updateData($connection, $query, true))
		$message  = mysql_error();   

		
	$result = array();
	$result = Generate_Chargings(insertMySQLDate($SGenDate), $HTTP_SESSION_VARS['dbName'], $HTTP_SESSION_VARS['dbLabel'], $HTTP_SESSION_VARS['useraccount'], $HTTP_SESSION_VARS['username']); 
	
	$PJournalId = $result['id'];
	$message    = $result['error'];
}

//Post Mode	
else if($mode == "post" && $SPrePayMId != ''){  

	$AllowSave = 1;
	$ErrorExists = 0;
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$txtCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$txtCurr."' )";
	$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];
	
	
	if ($txtLedgerNo != '') {
		$sub_query = " Select Count(Ledger_Number) From Ledger ".
					 " Where Ledger_Number = '".$txtLedgerNo."'";
		$sub_result = selectData($connection, $sub_query);
		if ($sub_result[0] == 0) $ErrorExists = 1;
		
		if ($ErrorExists == 1)
			$message = 'Save not allowed. Please check for Ledger Number.';
	}
	
	//Check User Access For Date Modification/ Lock Period
	$MainCompanyName=explode('_',$HTTP_SESSION_VARS['dbName']);

	$sub_query= " Select ifNull(AllowLockedPeriod,0) As AllowLockedPeriod, LockedPeriod As LockedPeriodDate, LockedPeriodDays As LockedPeriodDays ".
				" From ".$HTTP_SESSION_VARS['dbLabel'].".ListUserDir  ".
				" Where Usercode='".$HTTP_SESSION_VARS['useraccount']."' ".
				" And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
				" And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
				  
	$sub_result = mysql_query($sub_query,$connection) or die(mysql_error());
	$rowsperiod = mysql_num_rows($sub_result);

	while($sub_rows=mysql_fetch_array($sub_result)){
		extract($sub_rows);
	}	
	
	if ($rowsperiod > 0 && $AllowLockedPeriod == 1) { 
		$ExceedLockPeriod = 0;
		
		$sub_query2 = " Select PrepaymentD.PrePaymentD_Id, PrepaymentD.Date1 ".
					  " From Temp_PrepaymentD As PrepaymentD ".
					  " Where PrepaymentD.PrePaymentM_Id = '".$SPrePayMId."' "; 
							  
		//echo $sub_query2;
		$sub_result2=mysql_query($sub_query2,$connection);
		while($sub_row2=mysql_fetch_row($sub_result2)){
			$vPrePayDId = $sub_row2[0];
			$vDateD     = $sub_row2[1];
			
			if (!(empty($LockedPeriodDate)) && $LockedPeriodDate != '' && insertMySQLDate($vDateD) < $LockedPeriodDate) $ExceedLockPeriod = 1;
			if ($LockedPeriodDays != '' && dateDiff(insertMySQLDate(displayToday()),insertMySQLDate($vDateD)) > $LockedPeriodDays) $ExceedLockPeriod = 1;
			
			if ($ExceedLockPeriod == 1) {
				$AllowSave = 0;
				$ErrorRecId = $vPrePayDId;
				$message='Save not allowed. Exceed Lock Period.';
				break;
			}
		}
	}
				
			

						
	if ($AllowSave == 1 && $ErrorExists == 0) {	
			 
		$sub_query3 = " UPDATE Temp_PrepaymentM SET ".
					  " Ledger_Number = '".ereg_replace("'", "\'", trim($txtLedgerNo))."', ".
					  " PrePayment_dbcd = '".ereg_replace("'", "\'", trim($txtJourType))."'  ".
					  " WHERE PrePaymentM_Id = ".$SPrePayMId;
				 
		$sub_result3=mysql_query($sub_query3,$connection);
		//echo $sub_query5."<br>"; exit;
	
	
		if ($sub_result3){
			
			if ($mainmode == 'insert') $SMode = 'I';
			if ($mainmode == 'edit') $SMode = 'U';
			//echo $SMode;exit;
			
			$query = "Select Count(*) From Temp_PrepaymentM where PrePaymentM_Id = '".$SPrePayMId."'";
			//echo $query; exit;
			$result = selectData($connection, $query);
			$ExistInTemp = $result[0];	
			
			//Check If Record Exists In Temp Tables
			if ($ExistInTemp == 1) {
				fbegin();		
				
				$query = " SELECT CopyData_TempPrepayment('".$SMode."', ".$SPrePayMId.", '".$HTTP_SESSION_VARS['useraccount']."') FROM Dual ";
				$result = selectData($connection, $query);
				$result = $result[0];
				//echo $query; exit;
				//echo 'error='.$result; exit;
				if ($result != 0){ 
					fcommit();
					 
				} else{
					//$message  = mysql_error(); 
					$message  = 'Error In CopyData_TempPrepayment Function.'; 
					frollback();
				}
			}
			
		} else{
			$message  = mysql_error(); 
			frollback();
		}
		
	}
}


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

//echo $mode.$message; exit;

if($mode == "post") {
	if ($AllowSave == 1)
		header ("Location: ./Form_Prepayment.php?actionmode=".$mode."&mainaction=".$mainmode."&PrPrePayMId=".$SPrePayMId."&PrLedgerNo=".$txtLedgerNo."&message=".$message);
	else
		header ("Location: ./Form_Prepayment.php?actionmode=submitrecord&mainaction=".$mainmode."&ErrorRecId=".$ErrorRecId."&txtPrePayMId=".$SPrePayMId."&message=".$message);
}
else if ($mode == "generate")	{
	header ("Location: ./Form_PrepaymentGenerate.php?txtJournalId=".$PJournalId."&message=".$message);
}	
else if ($mode == "" && $mainmode == "delete" )	{
	header ("Location: ./Form_PrepaymentSearch.php?message=".$message);
}	
else if ($mode == "" && $mainmode == "deletepending")	
	header ("Location: ./Form_PrepaymentSearchPending.php?message=".$message);
else if ($mode == "" && $mainmode == "search" )		
	header ("Location: ./Form_PrepaymentSearch.php?PMode=1&message=".$message);	
else if ($mode == "submitrecord" || $mode == "editdt" || $mode == "deletedt"){
	if ($AllowInsert == 0 && ($mode == "submitrecord" || $mode == "editdt"))
		header ("Location: ./Form_Prepayment.php?actionmode=".$mode."&mainaction=".$mainmode."&txtPrePayMId=".$SPrePayMId.
				"&txtLedgerNo=".$txtLedgerNo."&txtLedgerName=".$txtLedgerName."&txtJourType=".$txtJourType."&txtLedgerNoD=".$txtLedgerNoD.
				"&txtLedgerNameD=".$txtLedgerNameD."&txtDate=".$txtDate."&txtDescriptionD=".$txtDescriptionD."&txtDivisionD=".$txtDivisionD.
				"&txtProjectCode=".$txtProjectCode."&txtCostCentCode=".$txtCostCentCode."&txtCurrD=".$txtCurrD."&txtAmountD=".$txtAmountD.
				"&txtGenerate=".$txtGenerate."&message=".$message);
	else
		header ("Location: ./Form_Prepayment.php?actionmode=".$mode."&mainaction=".$mainmode."&txtPrePayMId=".$SPrePayMId."&message=".$message);
}	
		

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

HERE;


?>