<?php
session_start();

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

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

/// Color Grids    Base1 Base2     Decimal for Amounts

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



	$query_chkjourtotal="select currency_code,ledger_number,Journaldt_amount,Journaldt_amount1,Journaldt_amount2 from temp_journaldt where journaldt_id=".$journaldt_id;
	$result_chkjour=mysql_query($query_chkjourtotal,$connection) or die(mysql_error());
	$row_ledger=mysql_fetch_row($result_chkjour);
	$ledger_number=$row_ledger[1];
	$chk_currency=$row_ledger[0];
	$Amount_total=$row_ledger[2];
	$Amount_total1=$row_ledger[3];
	$Amount_total2=$row_ledger[4];
	
	//echo$Amount_total.''.$Amount_total1.''.$Amount_total2."<br>";


if($chkmode!='editmode' && $checkvalidity!='1' && $checkvalidity!='0'){

	$query_amount="select sum(Chk_Amount),sum(Chk_Amount1),sum(Chk_Amount2) from temp_chkjour where
					Journaldt_id=".$journaldt_id." group by ledger_number";

	$result_chkjour_left=mysql_query($query_amount,$connection) or die(mysql_error());
	$row_ledger=mysql_fetch_row($result_chkjour_left);
	$Amount=$row_ledger[0];
	$Amount1=$row_ledger[1];
	$Amount2=$row_ledger[2];



	$Chk_Amount=$Amount_total-$Amount;
	$Chk_Amount=round($Chk_Amount,$DecimalBase);
	
	$Chk_Amount1=$Amount_total1-$Amount1;
	$Chk_Amount1=round($Chk_Amount1,$DecimalBase1);
	
	$Chk_Amount2=$Amount_total2-$Amount2;
	$Chk_Amount2=round($Chk_Amount2,$DecimalBase2);
	
	$Chkjour_amountnotrounded=$Amount_total-$Amount;
	$Chkjour_amountnotrounded1=$Amount_total1-$Amount1;
	$Chkjour_amountnotrounded2=$Amount_total2-$Amount2;
	


}


if($chkmode=='editmode'){

	$query_edit="select Chk_Number,Chk_Bank,Currency_code,Chk_Amount,date_format(Chk_ValueDate,'%d-%m-%Y'),Chk_Increment,
				Chk_Amount1,Chk_Amount2 from temp_chkjour where chk_increment=".$chk_increment;
	
	$result_edit=mysql_query($query_edit,$connection);
	$row_edit=mysql_fetch_row($result_edit);

	$chk_number=$row_edit[0];
	$chk_bank=$row_edit[1];
	$chk_valuedate=$row_edit[4];
	$Chk_Amount=$row_edit[3];
	$chk_currency=$row_edit[2];
	$Chk_Amount1=$row_edit[6];
	$Chk_Amount2=$row_edit[7];
	
	// filling hidden fields
	$Chkjour_amountnotrounded=$Chk_Amount;
	$Chkjour_amountnotrounded1=$Chk_Amount1;
	$Chkjour_amountnotrounded2=$Chk_Amount2;
	
	
	// filling form fields
	$Chk_Amount=round($Chk_Amount,$DecimalBase);
	$Chk_Amount1=round($Chk_Amount1,$DecimalBase1);
	$Chk_Amount2=round($Chk_Amount2,$DecimalBase2);
	
	$chkmode='edit';
}

if(empty($chkmode))
	$chkmode='insert';

if(empty($chk_valuedate)) {
	$today = date('d-m-Y');
	$chk_valuedate=$today;
}
// Activity
$query_ledger="select if(Ledger_activity=1,'Checks for Collections','Checks for Payments'),ifnull(ledger_activity,1) from ledger where ledger_number=".$ledger_number;
$result_ledger=mysql_query($query_ledger,$connection);
$row_activity=mysql_fetch_row($result_ledger);
$activity=$row_activity[0];
$activity_num=$row_activity[1];


// Dbcd

$query_dbcd="select Journaldt_dbcd from temp_journaldt where journaldt_id=".$journaldt_id;
$result_dbcd=mysql_query($query_dbcd,$connection);
$row_dbcd=mysql_fetch_row($result_dbcd);
$type=$row_dbcd[0];

/// Currency Rates

	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$chk_currency."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$chk_currency."' )";
	$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 ($activity_num=='1'){
	
	if($type=='D')
		$showlist=0;
	else $showlist=1;
	
}

if ($activity_num=='2'){
	
	if($type=='C')
		$showlist=0;
	else $showlist=1;
	
}



if($showlist=='1')
	$status='readonly';
	
$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";


print <<<HERE
<html>
<head>
<SCRIPT type="text/javascript" src="shortcut.js"></script>
<script type="text/javascript" language="javascript">


function set_amount(){

	x=document.chk;
	var checkOK = ",0123456789.";
	var checkStr = chk.Chk_Amount.value;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{				
		alert("Please enter only digit characters in the \"Amount\" field.");
		x.Chk_Amount.focus();			
		
		return (false);
	}

	var dec1=x.DecimalBase1.value;
	var dec2=x.DecimalBase2.value;
	
	dec1=parseInt(dec1);
	dec2=parseInt(dec2);
	
	var amount=x.Chk_Amount.value;
	amount=parseFloat(amount);
	
	var rate=x.rate_dt.value;
	rate=parseFloat(rate);
	
	var rate1=x.rate_dt1.value;
	rate1=parseFloat(rate1);
	
	var rate2=x.rate_dt2.value;
	rate2=parseFloat(rate2);


	if (amount >= 0) {
	
		x.Chkjour_amountnotrounded.value=amount;
	
		var amount1hidden=((amount*rate)/rate1);
		amount1hidden=parseFloat(amount1hidden);	
		x.Chkjour_amountnotrounded1.value=amount1hidden;
		
		var amount1=Math.round(((amount*rate)/rate1)*Math.pow(10,dec1))/Math.pow(10,dec1);
		amount1=parseFloat(amount1);
		x.Chk_Amount1.value= amount1;
		

		var amount2hidden=((amount*rate)/rate2);
		amount2hidden=parseFloat(amount2hidden);	
		x.Chkjour_amountnotrounded2.value=amount2hidden;
		
		var amount2=Math.round(((amount*rate)/rate2)*Math.pow(10,dec2))/Math.pow(10,dec2);
		amount2=parseFloat(amount2);
		x.Chk_Amount2.value=amount2;
	}	
}



function change_amount1() {

	x=document.chk;
	var checkOK = ",0123456789.";
	var checkStr = chk.Chk_Amount1.value;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{				
		alert("Please enter only digit characters in the \"Amount\" field.");
		x.Chk_Amount1.focus();			
		return (false);
	}

	var base1=x.base1.value;
	var cur=x.chk_currency.value;
	
	if(base1==cur){
	
		var amount1=x.Chk_Amount1.value;
		x.Chk_Amount.value=amount1;
		x.Chkjour_amountnotrounded.value=amount1;
		x.Chkjour_amountnotrounded1.value=amount1;
	}	
}

function change_amount2() {

	x=document.chk;
	var checkOK = ",0123456789.";
	var checkStr = chk.Chk_Amount2.value;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{				
		alert("Please enter only digit characters in the \"Amount\" field.");
		x.Chk_Amount2.focus();			
		return (false);
	}

	var base2=x.base2.value;
	var cur=x.chk_currency.value;
	
	if(base2==cur){

		var amount2=x.Chk_Amount2.value;
		x.Chk_Amount.value=amount2;
		x.Chkjour_amountnotrounded.value=amount2;
		x.Chkjour_amountnotrounded2.value=amount2;
	}	
}

function confirm_delete(Code) {

	x=document.chk;
	window.event.returnValue = false;
				
	if (confirm ( "Are you sure you want to Delete this Record " + "?")) {
		window.event.returnValue = true;
					
		x.chk_increment.value=Code;			
		x.chkmode.value='deletechk';			
		x.action='Form_Journal_Actions.php';
		x.submit();
	}
}

function EditChk(code){

	x=document.chk;
	x.chk_increment.value=code;
	x.chkmode.value='editmode';
	x.action='Form_Chkjour.php';
	x.submit();
}


function AddDetail(){

	x=document.chk;
	var checkOK = ",0123456789.";
	var checkStr = x.Chk_Amount.value;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
	if (j == checkOK.length)
	{
		allValid = false;
		break;
	}
	if (ch != ",")
	   allNum += ch;
	}
	if (!allValid)
	{				
		alert("Please enter only digit characters in the \"Amount\" field.");
		x.Chk_Amount.focus();			
		
		return (false);
	}


	if(x.chk_number.value==''){
		alert('Check No is Mandatory');
		x.chk_number.focus();
		}

	else if(x.chk_bank.value==''){
			alert('Bank is Mandatory');
			x.chk_bank.focus();
		} 

	else if(x.chk_valuedate.value==''){
			alert('Value Date is Mandatory');
			x.chk_valuedate.focus();
		} 

	else if(x.Chk_Amount.value==''){
			alert('Ammount is Mandatory');
			x.Chk_Amount.focus();
		} 

	else if(x.chk_currency.value==''){
			alert('Currency is Mandatory');
			x.chk_currency.focus();
		} 

	else if ((x.Chk_Amount.value=='') || (x.Chk_Amount.value=='0')){
	
		alert('Amount Can not be Zero');
		x.Chk_Amount.focus();
	}
	
	else if(((x.activity_num.value=='2') && (x.type.value=='C')) || ((x.activity_num.value=='1') && (x.type.value=='D'))){
		x.checkvalidity.value=1;
		x.checkvaliditycheck.value=0;	
		x.action='Form_Chkjour.php';
		x.submit();
	}

	else {
	
		x.checkvalidity.value=1;
		x.checkvaliditycheck.value=1;
		x.action='Form_Chkjour.php';
		x.submit();
	}
}

function cleardetail() {
	x=document.chk;
	x.chk_number.value='';
	x.chk_bank.value='';
	x.chk_valuedate.value='';
	x.Chk_Amount.value='';
	x.chk_increment.value='';
	x.message.value='';
	x.checkvalidity.value='';
	x.Chk_Amount1.value='';	
	x.Chk_Amount2.value='';	
	x.chkmode.value='';
	x.action='Form_Chkjour.php';
	x.submit();
}

function showlistchecks(){

	x=document.chk;
	chk_increment=x.chk_increment.value;
	currency=x.chk_currency.value;
	activity=x.activity_num.value;
	ledger_number=x.ledger_number.value;
	journalid=x.journal_id.value;
	journaldtid=x.journaldt_id.value;
	mode=x.chkmode.value;
	
	window.open('List_Checks.php?chk_increment='+chk_increment+'&journaldt_id='+journaldtid+'&journal_id='+journalid+'&ledger_number='+ledger_number+'&activity_num='+activity+'&chk_currency='+currency+'&chkmode='+mode,'checks','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

</script>
<link rel="stylesheet" href="default.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}

.style11 {color: #999999; font-weight: bold; }
.style27 {color: #999999 ;font-size: 10px; font-weight:bold;}
.style15 {color: #5151FF; font-size: 11px;}
.selectstyle {font-size: 11px; font-family:arial,verdana,geneva; color:#000000; width=52px}
.selectstyle1 {font-size: 11px; font-family:arial,verdana,geneva; color:#000000; width=35px}
</style>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>



<title>Journal</title>
</head>
<body>
<form name='chk' method='post'>
<input type='hidden'  name='journaldt_id' value='$journaldt_id'>
<input type='hidden'  name='journal_id' value='$journal_id'>
<input type='hidden'  name='ledger_number' value='$ledger_number'>
<input type='hidden'  name='chkmode' value='$chkmode'>
<input type='hidden'  name='chk_increment' value='$chk_increment'>
<input type='hidden'  name='message' value='$message'>
<input type='hidden'  name='activity_num' value='$activity_num'>
<input type='hidden'  name='checkvalidity' value='$checkvalidity'>
<input type='hidden'  name='checkvaliditycheck' value='$checkvaliditycheck'>
<input type='hidden'  name='type' value='$type'>
<input type='hidden' name='base2'  value='$base2'>
<input type='hidden' name='base1'  value='$base1'>
<input type='hidden' name='DecimalBase1'  value='$DecimalBase1'>
<input type='hidden' name='DecimalBase2'  value='$DecimalBase2'>
<input type='hidden' name='rate_dt'  value='$rate_dt'>
<input type='hidden' name='rate_dt1'  value='$rate_dt1'>
<input type='hidden' name='rate_dt2'  value='$rate_dt2'>
<input type='hidden'  name='Chkjour_amountnotrounded' value='$Chkjour_amountnotrounded'>
<input type='hidden'  name='Chkjour_amountnotrounded1' value='$Chkjour_amountnotrounded1'>
<input type='hidden'  name='Chkjour_amountnotrounded2' value='$Chkjour_amountnotrounded2'>

HERE;


echo "<table  border=0><tr><td colspan=6 valign='top'>";
echo "<table style='table-layout:fixed' border=0>
			<col width=40>
			<col width=40>
			<col width=130>
			<col width=155>
			<col width=100>
			<col width=60>
			<col width=120>
			<col width=120>
			<col width=120>";

echo "<tr bgcolor='#F7F7F7' height=25px><td height='20' colspan='9' align='center'><font size='1' color='#FF0000' face='Verdana'><b>$message</b></font></td></tr>";

echo"	<tr bgcolor='#F7F7F7' height='25'>";
echo"	<td colspan=2><input type='button'  name='adddetail' value='Submit'";  	
echo"	onclick='AddDetail();'  title='Add Detail' '".$AddMode."' $style></td>";
echo"	<td><span class='style11'>Check No.</span></td>";

echo"	<td><span class='style11'>Bank</span></td>";
echo"	<td><span class='style11'>Value Date</span>";

if($showlist!='1')
print<<<HERE
		<A HREF="#" onClick="displayCalendar(document.chk.chk_valuedate,'dd-mm-yyyy',this);  return false; "><img src='./img/cal.gif' border=0 align=center></A></td>		
HERE;
echo"	</td>
		<td><span class='style11'>Currency</span></td>
		<td><span class='style11'>Amount</span></td>
		<td><span class='style11'>Amount $base1</span></td>
		<td><span class='style11'>Amount $base2</span></td></tr>";

echo"	<tr bgcolor='#F7F7F7'><td colspan=2><input type='button'  name='clearbutton' value='Clear'";  	
echo"	onclick='cleardetail();'  title='Clear Detail'  $style></td>";
echo"	<td><input type='text' name='chk_number' value='$chk_number' maxlength='20' style='font-size:11px; width:100px; height:22px; text-align:right;' $status>";

if($showlist=='1')
	echo"<label>&nbsp;<span class='style11'><img src='img/btnopen.jpg' title=' Select Checks ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistchecks(); ' /></span></label>";

echo"	<td><input type='text' name='chk_bank' value='$chk_bank' maxlength='30' style='font-size:11px; width:150px; height:22px; text-align:left;' $status></td>
		<td><input type='text' name='chk_valuedate' value='$chk_valuedate' readonly style='font-size:11px; width:90px; height:22px; text-align:left;' ></td>
		<td><input type=text name='chk_currency'  value='$chk_currency' readonly style='font-size:11px; width:50px; height:22px; text-align:left;' $status>";
						
echo"<td><input type='text' name='Chk_Amount' value='$Chk_Amount' style='font-size:11px; width:110px; height:22px; text-align:left;' onchange='set_amount()'; $status></td>
		<td><input type='text' name='Chk_Amount1' value='$Chk_Amount1' style='font-size:11px; width:110px; height:22px; text-align:left;' onchange='change_amount1();' $status></td>
		<td><input type='text' name='Chk_Amount2' value='$Chk_Amount2' style='font-size:11px; width:110px; height:22px; text-align:left;' onchange='change_amount2();' $status></td></tr>";
		
$query_chkjour="select Chk_Number,Chk_Bank,Currency_code,Chk_Amount,date_format(Chk_ValueDate,'%d-%m-%Y'),Chk_Increment,
						Chk_Amount1,Chk_Amount2 from temp_chkjour where journaldt_id=".$journaldt_id;

$result_chk=mysql_query($query_chkjour,$connection);
$i=1;
while($row_chk=mysql_fetch_row($result_chk)){	

	$chk_amountdisplay=round($row_chk[3],$DecimalBase);
	$chk_amountdisplay1=round($row_chk[6],$DecimalBase1);
	$chk_amountdisplay2=round($row_chk[7],$DecimalBase2);
		
	if($i%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
		
		echo'<tr '.$Color.'>';

	$query_journaldt2="select journaldt_id2,ledger_activity from checks where Chk_Number='".$row_chk[0]."' and ledger_number=".$ledger_number;
	$result_journaldt2=mysql_query($query_journaldt2,$connection);
	$row_journaldt=mysql_fetch_row($result_journaldt2);
	
	
		
	print<<<HERE
		<td align=center width='3%'><img border='0' src='images/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover="this.style.cursor='pointer'"
HERE;
if($row_journaldt[0]!='' && (($type=='D' && $activity_num=='1') or ($type=='C' && $activity_num=='2')))
	echo"disabled";
else echo"
		OnClick=confirm_delete('$row_chk[5]');";
		echo"></td>";
print<<<HERE
		<td align=center width='3%'><img border='0' src='images/button_edit.png' width='12' height='13' title='Edit Detail' onmouseover="this.style.cursor='pointer'" 
HERE;
if($row_journaldt[0]!='' && (($type=='D' && $activity_num=='1') or ($type=='C' && $activity_num=='2')))
	echo"disabled";

else echo"	onclick=EditChk('$row_chk[5]');";
	echo"></td>	";
echo"	<td><span class='style15'>$row_chk[0]</span></td>
		<td><span class='style15'>$row_chk[1]</span></td>
		<td><span class='style15'>$row_chk[4]</span></td>
		<td><span class='style15'>$row_chk[2]</span></td>
		<td align='right'><span class='style15'>$chk_amountdisplay</span></td>
		<td align='right'><span class='style15'>$chk_amountdisplay1</span></td>
		<td align='right'><span class='style15'>$chk_amountdisplay2</span></td></tr>";

			
	$i=$i+1;		
}

echo"</table>";	


if($checkvalidity=='1'){

	if($checkvaliditycheck!='1' && $chkmode!='edit'){
		$query_checks="	select count(*) from checks where ledger_number=".$ledger_number." and chk_number='".$chk_number."'
		 and journaldt_id1<>".$journaldt_id;
	
		$result_checks=mysql_query($query_checks,$connection);
		$row_check=mysql_fetch_row($result_checks);
		$count_check=$row_check[0];
		

	}
	
	if($chkmode=='edit')
		$query_chk="select count(*) from temp_chkjour where ledger_number=".$ledger_number." and chk_number='".$chk_number."'
		and chk_increment<>".$chk_increment;
	else
		$query_chk="select count(*) from temp_chkjour where ledger_number=".$ledger_number." and chk_number='".$chk_number."'";

	//echo$query_chk;
	$result_checkjour=mysql_query($query_chk,$connection);
	$row_chkjour=mysql_fetch_row($result_checkjour);
	$count_checkjour=$row_chkjour[0];
	
	
	
	if($chkmode!='edit' && $checkvaliditycheck!='1') {
		$query_checknumber="select count(*) from checks where ledger_number=".$ledger_number." and chk_number='".$chk_number."'
							and chk_number not in (select Chk_number from chkjour,journaldt where chkjour.journaldt_id=journaldt.journaldt_id
							and journaldt.journal_id=".$journal_id." ) and (Journaldt_id2 is null or Journaldt_id2=0)";
	
		$result_checknumber=mysql_query($query_checknumber,$connection);					
		$row_checknumber=mysql_fetch_row($result_checknumber);
		$count_checknumber=$row_checknumber[0];
	}
	
	if($chkmode=='edit')
		$query_checkdiff="select sum(Chk_Amount),sum(Chk_Amount1),
					sum(Chk_Amount2) from temp_chkjour where journaldt_id=".$journaldt_id." and Chk_Increment<>".$chk_increment;
	else
		$query_checkdiff="select sum(Chk_Amount),sum(Chk_Amount1),
					sum(Chk_Amount2) from temp_chkjour where journaldt_id=".$journaldt_id;
	
	
	$result_checkdiff=mysql_query($query_checkdiff,$connection);
	$row_checkdiff=mysql_fetch_row($result_checkdiff);
	
	$amount_diff=$row_checkdiff[0];
	$amount_diff1=$row_checkdiff[1];
	$amount_diff2=$row_checkdiff[2];
	
	$amount_diff=$amount_diff+$Chk_Amount;
	$amount_diff1=$amount_diff1+$Chk_Amount1;
	$amount_diff2=$amount_diff2+$Chk_Amount2;
	
	$amount_diff=round($amount_diff,$DecimalBase);
	$amount_diff1=round($amount_diff1,$DecimalBase1);
	$amount_diff2=round($amount_diff2,$DecimalBase2);
	

	if($count_check>0 or $count_checkjour>0 or $count_checknumber>0){
	
		echo"<script type='text/javascript'>";
		echo"	document.chk.message.value='Chk Number Already Exists';";
		echo"	document.chk.checkvalidity.value='0';";
		echo"	document.chk.submit();";	
		echo"</script>";
	}
	
	else if($amount_diff>$Amount_total or $amount_diff1>$Amount_total1 or $amount_diff2>$Amount_total2){
	
		echo"<script type='text/javascript'>";
		echo"	document.chk.message.value='Total Check Amount Greater than Main Amount';";
		echo"	document.chk.checkvalidity.value='0';";
		echo"	document.chk.submit();";	
		echo"</script>";
	}
	
	else{
		echo"<script type='text/javascript'>";
		echo"	document.chk.action='Form_Journal_Actions.php';";
		echo"	document.chk.submit();";
		echo"</script>";
	}
	
}

if($refresh==1){

	echo"<script type='text/javascript'>";
	echo"	opener.document.journal.action='Form_Journal.php';";
	echo"	opener.document.journal.submit();";
	echo"</script>";
}
	
echo"</form></body></html>";
?>


