<?php
session_start();

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

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


if($checkall!='1' && $checkall!='0'){
	$query_journaldt="	select journaldt_id,journal_id,Journaldt_desc,Ledger_number,Journaldt_dbcd,Journaldt_amount,Journaldt_amount1,".
						"Journaldt_amount2,date_format(Journaldt_ValueDate,'%d-%m-%Y'),currency_code,manual_no".
						" from temp_journaldt where journaldt_id=".$journaldt_id;
			
	$result_journaldetail=mysql_query($query_journaldt,$connection);
	while($row_journaldetail=mysql_fetch_row($result_journaldetail)){
		
		$Cstjour_amountnotrounded=$row_journaldetail[5];
		$Cstjour_amounttemp=$Cstjour_amountnotrounded;
		
		$Cstjour_amountnotrounded1=$row_journaldetail[6];	
		$Cstjour_amount1temp=$Cstjour_amountnotrounded1;
		
		$Cstjour_amountnotrounded2=$row_journaldetail[7];
		$Cstjour_amount2temp=$Cstjour_amountnotrounded2;
	}
}

if($cstmode=='editmode'){

	$query_edit="select Journaldt_id,Costcent_code,Project_Code,Cstjour_prcnt,Cstjour_amount,Cstjour_amount1,Cstjour_amount2,cst_increment from temp_cstjour
				where cst_increment=".$cst_increment;
	$result_edit=mysql_query($query_edit,$connection);
	$row_edit=mysql_fetch_row($result_edit);
	
	$project_code=$row_edit[2];
	$costcent_code=$row_edit[1];
	$Cstjour_prcnt=$row_edit[3];
	$Cstjour_amount=$row_edit[4];
	$Cstjour_amount1=$row_edit[5];
	$Cstjour_amount2=$row_edit[6];
	
	$Cstjour_amountnotrounded=$Cstjour_amount;
	$Cstjour_amountnotrounded1=$Cstjour_amount1;
	$Cstjour_amountnotrounded2=$Cstjour_amount2;
	
	
	$Cstjour_amount=round($Cstjour_amount,$DecimalBase);
	$Cstjour_amount1=round($Cstjour_amount1,$DecimalBase1);
	$Cstjour_amount2=round($Cstjour_amount2,$DecimalBase2);
	
	$cstmode='edit';
	$message='';
}


if($cstmode!='edit' && $checkall!='1' && $checkall!='0'){
	$query_cstdiff="select sum(Cstjour_prcnt),sum(Cstjour_amount),sum(Cstjour_amount1),sum(Cstjour_amount2) from temp_cstjour where journaldt_id=".$journaldt_id."
					group by journaldt_id";
	
	$result_cstdiff=mysql_query($query_cstdiff,$connection);
	$row_cstdiff=mysql_fetch_row($result_cstdiff);
	$Cstjour_prcnt=100-$row_cstdiff[0];

	$Cstjour_amount=$Cstjour_amounttemp-$row_cstdiff[1];
	$Cstjour_amountnotrounded=$Cstjour_amount;
	$Cstjour_amount=round($Cstjour_amount,$DecimalBase);

	$Cstjour_amount1=$Cstjour_amount1temp-$row_cstdiff[2];
	$Cstjour_amountnotrounded1=$Cstjour_amount1;
	$Cstjour_amount1=round($Cstjour_amount1,$DecimalBase1);

	$Cstjour_amount2=$Cstjour_amount2temp-$row_cstdiff[3];
	$Cstjour_amountnotrounded2=$Cstjour_amount2;
	$Cstjour_amount2=round($Cstjour_amount2,$DecimalBase2);

	if($Cstjour_amount=='0')
		$Cstjour_amount='';
	if($Cstjour_amount1=='0')
		$Cstjour_amount1='';
	if($Cstjour_amount2=='0')
		$Cstjour_amount2='';
	if($Cstjour_prcnt=='0')
		$Cstjour_prcnt='';
}

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

$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 confirm_delete(Code) {

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

function cleardetail(){

	x=document.cst;
	x.Cstjour_amount.value='';
	x.Cstjour_amount1.value='';
	x.Cstjour_amount2.value='';
	x.Cstjour_prcnt.value='';
	x.project_code.value='';
	x.costcent_code.value='';
	x.cst_increment.value='';
	x.message.value='';
	x.cstmode.value='';
	x.checkall.value='';
	x.action='Form_CSTjour.php';
	x.submit();
}


function EditCst(code){

	x=document.cst;
	x.cst_increment.value=code;
	x.cstmode.value='editmode';
	x.action='Form_CSTjour.php';
	x.submit();
}

function set_percentage(){

/// Not rounded for hidden fields not rounded 
x=document.cst;
var checkOK = ",0123456789.";
var checkStr = x.Cstjour_prcnt.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 \"Percentage\" field.");
	x.Cstjour_prcnt.focus();			
	
    return (false);
}

var checkOK = ",0123456789.";
var checkStr = x.Cstjour_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.Cstjour_amount.focus();			
	
return (false);
}


var checkStr = x.Cstjour_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 1\" field.");
	x.Cstjour_amount.focus();			
	
return (false);
}

var checkStr = x.Cstjour_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 2\" field.");
	x.Cstjour_amount2.focus();			
	
return (false);
}

	x=document.cst;
	var amount=x.Cstjour_amounttemp.value;
	amount=parseFloat(amount);
	
	var amount1=x.Cstjour_amount1temp.value;
	amount1=parseFloat(amount1);

	var amount2=x.Cstjour_amount2temp.value;
	amount2=parseFloat(amount2);

	var dec1=x.DecimalBase1.value;
	var dec2=x.DecimalBase2.value;
	var dec=x.DecimalBase.value;
	
	dec1=parseInt(dec1);
	dec2=parseInt(dec2);
	dec=parseInt(dec);
	
	var percent=x.Cstjour_prcnt.value;
		percent=parseInt(percent);

var Cstjour_amount=x.Cstjour_amount.value;

	if(	Cstjour_amount>=0){
		cst_amountnotrounded=(amount*percent)/100;
		cst_amount=Math.round(((amount*percent)/100)*Math.pow(10,dec))/Math.pow(10,dec);
		x.Cstjour_amount.value=cst_amount;
		x.Cstjour_amountnotrounded.value=cst_amountnotrounded;

		cst_amountnotrounded1=(amount1*percent)/100;
		cst_amount1=Math.round(((amount1*percent)/100)*Math.pow(10,dec1))/Math.pow(10,dec1);
		x.Cstjour_amount1.value=cst_amount1;
		x.Cstjour_amountnotrounded1.value=cst_amountnotrounded1;
		
		cst_amountnotrounded2=(amount2*percent)/100;
		cst_amount2=Math.round((cst_amountnotrounded2)*Math.pow(10,dec2))/Math.pow(10,dec2);
		x.Cstjour_amount2.value=cst_amount2;
		x.Cstjour_amountnotrounded2.value=cst_amountnotrounded2;
	}
}

function set_amount(){

x=document.cst;
var checkOK = ",0123456789.";
var checkStr = x.Cstjour_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.Cstjour_amount.focus();			
	
    return (false);
}

	x=document.cst;
	var amount=x.Cstjour_amounttemp.value;
	amount=parseFloat(amount);
	
	var amount1=x.Cstjour_amount1temp.value;
	amount1=parseFloat(amount1);

	var amount2=x.Cstjour_amount2temp.value;
	amount2=parseFloat(amount2);
	
	var dec1=x.DecimalBase1.value;
	var dec2=x.DecimalBase2.value;
	var dec=x.DecimalBase.value;
	
	dec1=parseInt(dec1);
	dec2=parseInt(dec2);
	dec=parseInt(dec);
	
	var cst_amount=x.Cstjour_amount.value;
	x.Cstjour_amountnotrounded.value=cst_amount;
	var percentage=(cst_amount*100)/amount;
	x.Cstjour_prcnt.value=percentage;

	cst_amount1=Math.round(((amount1*percentage)/100)*Math.pow(10,dec))/Math.pow(10,dec);
	cst_amountnotrounded1=(amount1*percentage)/100;
	x.Cstjour_amount1.value=cst_amount1;
	x.Cstjour_amountnotrounded1.value=cst_amountnotrounded1;

	cst_amount2=Math.round(((amount2*percentage)/100)*Math.pow(10,dec))/Math.pow(10,dec);
	cst_amountnotrounded2=(amount2*percentage)/100;
	x.Cstjour_amount2.value=cst_amount2;
	x.Cstjour_amountnotrounded2.value=cst_amountnotrounded2;
}


function set_amountbase(num){

	x=document.cst;
	var checkOK = ",0123456789.";

	if(num==1)
		var checkStr = x.Cstjour_amount1.value;
	else var checkStr = x.Cstjour_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.");
		
		if(num==1)
			x.Cstjour_amount1.focus();			
		else x.Cstjour_amount2.focus();		
		return (false);
	}

		x=document.cst;
		var base1=x.base1.value;
		var base2=x.base2.value;
		var cur=x.currency_code.value;

		var amount1=x.Cstjour_amount1temp.value;
		amount1=parseFloat(amount1);
		
		var amount2=x.Cstjour_amount2temp.value;
		amount2=parseFloat(amount2);
		
		var dec1=x.DecimalBase1.value;
		var dec2=x.DecimalBase2.value;
		var dec=x.DecimalBase.value;
		
		dec1=parseInt(dec1);
		dec2=parseInt(dec2);
		dec=parseInt(dec);
		
		var cst_amount1=x.Cstjour_amount1.value;
		
		if(num==1){
				if(cur==base1 && cst_amount1>=0) {
				
					var percentage=(cst_amount1*100)/amount1;
					x.Cstjour_prcnt.value=percentage;
		
					x.Cstjour_amount.value=cst_amount1;
					x.Cstjour_amountnotrounded.value=cst_amount1;
				}
			x.Cstjour_amountnotrounded1.value=cst_amount1;
		}
		else if(num==2){
				var cst_amount2=x.Cstjour_amount2.value;
				
				if(cur==base2 && cst_amount2>=0) {
					var cst_amount2=x.Cstjour_amount2.value;
					var percentage=(cst_amount2*100)/amount2;
					x.Cstjour_prcnt.value=Math.round(((percentage)*Math.pow(10,dec))/Math.pow(10,dec));
					
					x.Cstjour_amount.value=cst_amount2;
					x.Cstjour_amountnotrounded.value=cst_amount2;
			}
			x.Cstjour_amountnotrounded2.value=cst_amount2;
		}
}

function AddDetail(){

	x=document.cst;
	var checkOK = ",0123456789.";
	var checkStr = x.Cstjour_prcnt.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 \"Percentage\" field.");
		x.Cstjour_prcnt.focus();			
		
		return (false);
	}

		
	var checkOK = ",0123456789.";
	var checkStr = x.Cstjour_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.Cstjour_amount.focus();			
		
	return (false);
	}


	var checkStr = x.Cstjour_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 1\" field.");
		x.Cstjour_amount.focus();			
		
	return (false);
	}

	var checkStr = x.Cstjour_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 2\" field.");
		x.Cstjour_amount2.focus();			
		
	return (false);
	}


	if(x.project_code.value=='' && x.costcent_code.value==''){
		alert('Project Code or Cost Cent are Mandatory');
		x.project_code.focus();
	}

	else if(((x.Cstjour_amount1.value=='') && (x.Cstjour_amount2.value=='0')) || ((x.Cstjour_amount1.value=='0') && (x.Cstjour_amount2.value==''))){
		
		alert('Amounts Can Not be Equal to Zero');
	
	if(x.Cstjour_amount1.value=='0')
		x.Cstjour_amount1.focus();
	else x.Cstjour_amount2.focus();
	}

	else if((x.Cstjour_amount1.value=='0') && (x.Cstjour_amount2.value=='0')) {
		alert('Amounts Can not be Zero');
		x.Cstjour_amount1.focus();
	}

	else{	
		x.checkall.value=1;
		x.action='Form_CSTjour.php';
		x.submit();
	}
}


function showlistdistper(){

	x=document.cst;
	journalid=x.journaldt_id.value;
	window.open('List_distper.php?journaldt_id='+journalid,'mywindow1','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');


}

function showlistprojects(str){

  window.open('List_Project_orderbycode.php?str='+str,'mywindow1','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistcostcentercode(str){

  window.open('List_CostCenter_orderbycode.php?str='+str,'mywindow2','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>Cost Centers</title>
</head>
<body>
<form name='cst' method='post'>
<input type='hidden'  name='journaldt_id' value='$journaldt_id'>
<input type='hidden'  name='Cstjour_amounttemp' value='$Cstjour_amounttemp'>
<input type='hidden'  name='Cstjour_amount1temp' value='$Cstjour_amount1temp'>
<input type='hidden'  name='Cstjour_amount2temp' value='$Cstjour_amount2temp'>
<input type='hidden'  name='Cstjour_amountnotrounded' value='$Cstjour_amountnotrounded'>
<input type='hidden'  name='Cstjour_amountnotrounded1' value='$Cstjour_amountnotrounded1'>
<input type='hidden'  name='Cstjour_amountnotrounded2' value='$Cstjour_amountnotrounded2'>
<input type='hidden'  name='DecimalBase' value='$DecimalBase'>
<input type='hidden'  name='DecimalBase1' value='$DecimalBase1'>
<input type='hidden'  name='DecimalBase2' value='$DecimalBase2'>
<input type='hidden'  name='base1' value='$base1'>
<input type='hidden'  name='base2' value='$base2'>
<input type='hidden'  name='cstmode' value='$cstmode'>
<input type='hidden'  name='currency_code' value='$currency_code'>
<input type='hidden'  name='cst_increment' value='$cst_increment'>
<input type='hidden'  name='checkall' value='$checkall'>
<input type='hidden'  name='message' value='$message'>

HERE;


echo "<table  border=0><tr><td colspan=6 valign='top'>";
echo "<table style='table-layout:fixed' border='0'>
			<col width=40>
			<col width=50>
			<col width=180>
			<col width=150>
			<col width=80>
			<col width=110>
			<col width=110>
			<col width=110>";

echo "<tr bgcolor='#F7F7F7' height=25px><td height='20' colspan='8' 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>
		&nbsp;<label><span class='style12'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistdistper(); '  /></span></label></td>";
echo"	<td><span class='style11'>Project</span></td>";

echo"	<td><span class='style11'>CostCenter</span></td>";
echo"	<td><span class='style11'>Percentage</span></td><td><span class='style11'>Amount $currency_code</span></td>
		<td><span class='style11'>$base1</span></td><td><span class='style11'>$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='project_code' value='$project_code' readonly size='15'>
		<label><span class='style11'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistprojects(7); '  /></span></label>
		
</td>";
echo"	<td><input type='text' name='costcent_code' value='$costcent_code' readonly size='15'>
		<label><span class='style11'><img src='img/btnopen.jpg' title=' Select CostCenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistcostcentercode(5); '  /></span></label>
</td>";

echo"	<td align='center'><input type='text'  name='Cstjour_prcnt' value='$Cstjour_prcnt' onchange='set_percentage();' style='font-size:11px; width:30px; height:22px; text-align:right;' maxlength=4></td>
		<td align='center'><input type='text'  name='Cstjour_amount' value='$Cstjour_amount'  onchange='set_amount();' size='7' style='font-size:11px; width:100px; height:22px; text-align:right;'></td>
		<td align='center'><input type='text'  name='Cstjour_amount1' value='$Cstjour_amount1' onblur='set_amountbase(1);' size='7' style='font-size:11px; width:100px; height:22px; text-align:right;'></td>
		<td align='center'><input type='text'  name='Cstjour_amount2' value='$Cstjour_amount2' onblur='set_amountbase(2);' size='7' style='font-size:11px; width:100px; height:22px; text-align:right;'></td></tr>";		

$query_cst="select Journaldt_id,Costcent_code,Project_Code,Cstjour_prcnt,Cstjour_amount,Cstjour_amount1,Cstjour_amount2,cst_increment from temp_cstjour
			where journaldt_id=".$journaldt_id;
			
$result_cst=mysql_query($query_cst,$connection);
$i=1;
while($row_cst=mysql_fetch_row($result_cst)){

	$Cstjour_amountdisplay=round($row_cst[4],$DecimalBase);
	$Cstjour_amount1display=round($row_cst[5],$DecimalBase1);
	$Cstjour_amount2display=round($row_cst[6],$DecimalBase2);
	
	
	if($i%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
	
	echo'<tr '.$Color.'>';

print<<<HERE
			<td align=center width='3%'><img border='0' src='img/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover="this.style.cursor='pointer'" OnClick=confirm_delete('$row_cst[7]');></td>
			<td align=center width='3%'><img border='0' src='img/button_edit.png' width='12' height='13' title='Edit Detail' onmouseover="this.style.cursor='pointer'" onclick="EditCst('$row_cst[7]')"; ></td>
			<td><span class='style15'>$row_cst[2]</span></td>
			<td><span class='style15'>$row_cst[1]</span></td>
			<td align='right'><span class='style15'>$row_cst[3]</span></td>
			<td  align='right'><span class='style15'>$Cstjour_amountdisplay</span></td>
			<td  align='right'><span class='style15'>$Cstjour_amount1display</span></td>
			<td  align='right'><span class='style15'>$Cstjour_amount2display</span></td></tr>
HERE;

//echo$totalamount;
$i=$i+1;
}		
echo"</table></table>";

if($checkall=='1'){

	if($cstmode=='edit')
		$query_percent="select sum(Cstjour_prcnt),sum(Cstjour_amount),sum(Cstjour_amount1),sum(Cstjour_amount2) from temp_cstjour where journaldt_id=".$journaldt_id."
						and cst_increment<>".$cst_increment;
						
	else $query_percent="select sum(Cstjour_prcnt),sum(Cstjour_amount),sum(Cstjour_amount1),sum(Cstjour_amount2) from temp_cstjour where journaldt_id=".$journaldt_id;
	
	$result_percent=mysql_query($query_percent,$connection);
	$row_percent=mysql_fetch_row($result_percent);
	$totalpercent=$row_percent[0];
	$totalamount=$row_percent[1];
	$totalamount1=$row_percent[2];
	$totalamount2=$row_percent[3];
	

	$totalpercent=$totalpercent+$Cstjour_prcnt;
	$totalamount=$totalamount+$Cstjour_amountnotrounded;
	$totalamount1=$totalamount1+$Cstjour_amountnotrounded1;
	$totalamount2=$totalamount2+$Cstjour_amountnotrounded2;
	
//echo$totalamount.' '.$totalamount1.' '.$totalamount2."<br>";
//echo$Cstjour_amounttemp.' '.$Cstjour_amount1temp.' '.$Cstjour_amount2temp;


if($totalpercent>100 or $totalamount>$Cstjour_amounttemp or $totalamount1>$Cstjour_amount1temp or $totalamount2>$Cstjour_amount2temp)
	$alertmessage=1;



if($alertmessage==1) {

	echo"<script type='text/javascript'>";
	
if($totalpercent>100)	
	echo"document.cst.message.value='Save not allowed . Percentage over 100';";
else if ($totalamount>$Cstjour_amounttemp)
	echo"document.cst.message.value='Save not allowed . Total Amount Different ';";
	
else if ($totalamount1>$Cstjour_amount1temp)
	echo"document.cst.message.value='Save not allowed . Total Amount1 Different ';";

else if ($totalamount2>$Cstjour_amount2temp)
	echo"document.cst.message.value='Save not allowed . Total Amount2 Different ';";
	
	echo"document.cst.checkall.value='0';";
	echo"document.cst.action='Form_CSTjour.php';";
	echo"document.cst.submit();";
	echo"</script>";
}
else {

	echo"<script type='text/javascript'>";
	echo"document.cst.action='Form_Journal_Actions.php';";
	echo"document.cst.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>";
?>
