<?php

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


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

$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());


print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" language="javascript">


function SubmitRecord() {
	x = document.chargesdt;
	
	var result = CheckFields();
	var vChgId    = x.PChgId.value;
	var vChgDtId  = x.txtChgDtId.value;
	var vMainMode = x.mainaction.value;
	var vMode     = x.actionmode.value;
	
	if (vChgDtId == '')
		vMode = 'submitrecord';	
	else
		vMode = 'editdetail';	
	   
	if (result == 1) {
		x.action="Form_Charges_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SChgId="+vChgId+"&SChgDtId="+vChgDtId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.chargesdt;
	var vMode = x.actionmode.value;
	
	if (vMode == 'submitrecord') {
		
		opener.document.charges.actionmode.value = x.actionmode.value;
		opener.document.charges.mainaction.value = x.mainaction.value;
		//opener.document.charges.message.value = x.message.value;
		opener.document.charges.txtChgId.value   = x.PChgId.value;
		opener.document.charges.submit();
		
	} 
	else if (vMode == 'submitrecord' || vMode == 'editdetail') {
		opener.document.charges.actionmode.value = x.actionmode.value;
		opener.document.charges.mainaction.value = x.mainaction.value;
		opener.document.charges.txtChgId.value   = x.PChgId.value;
		opener.document.charges.submit();
		
		self.close();
	
	}
	
	
}

function CheckFields(){
  
    x = document.chargesdt;
	
	var vmode 	   = x.actionmode.value;
	var vDebitAcc  = x.txtDebitAcc.value;
	var vCreditAcc = x.txtCreditAcc.value;
	var vCurrency  = x.txtCurr.value;
	var vAmount    = x.txtAmount.value;
	
	vAmount = parseFloat(vAmount);
	if (vAmount == '') vAmount = 0;
	
		
	if (vDebitAcc=='')
	{
		alert("Debit Column is mandatory!");
		x.txtDebitAcc.focus();
		return 0;
	}
	else if (vCreditAcc=='' )
	{
		alert("Credit Column is mandatory!");
		x.txtCreditAcc.focus();
		return 0;
	}
	else if (vCurrency=='' )
	{
		alert("Currency Column is mandatory!");
		x.txtCurr.focus();
		return 0;
	}
	else if ((vAmount*1 != vAmount/1 || vAmount < 0)) {
		alert('Amount should be a NUMERIC and POSITIVE value ');
		x.txtAmount.focus();
		return 0;
	}
	
	
	else
	return 1 ;	
}

function SelectGenDebit() {
	x = document.chargesdt;
	
	x.txtGenDebit.value = 0;
	
	if (x.chkGenDebit.checked)
		x.txtGenDebit.value = 1;

}

function SelectIsVat() {
	x = document.chargesdt;
	
	x.txtIsVat.value = 0;
	
	if (x.chkIsVat.checked)
		x.txtIsVat.value = 1;
}

function showlistLedgers(str){
	
	window.open('Form_Adjustment_ListLedgers.php?item='+str+'&frm=2','mywindow','width=650,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function kd(e)
{
	x=document.chargesdt;
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { //enter key
        //x.btnSubmitRecord.click();
		return false;
    }
    return true;
}

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


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style12 {
	font-size: 10px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>


</head>
<body onLoad='ChangeLocation();' onkeydown='return kd(event)'>
<form name='chargesdt' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PChgId' value='$PChgId'>
<input type='hidden' name='PChgDtId' value='$PChgDtId'>

HERE;

//Edit Charges Detail Mode
if($PChgDtId != ''){
	$query = " Select ChargesDt_ID, ifNull(ChargesDt_DistOption,1), ChargesDt_DebitAccount, LedgerDb.Ledger_Name As LedgerDbName, ".
			 " ChargesDt_CreditAccount, LedgerCr.Ledger_Name As LedgerCrName, ChargesDT.Currency_Code, ".
			 " Round(ChargesDt_Total, ".$DecimalBase.") As ChargesDt_Total, ".
			 " Round(ChargesDt_TotalBase1, ".$DecimalBase.") As ChargesDt_TotalBase1, ".
			 " Round(ChargesDt_TotalBase2, ".$DecimalBase.") As ChargesDt_TotalBase2, ".
			 " ifNull(ChargesDt_GenerateDebit, 0), ifNull(ChargesDt_isVat, 0) ".
			 " From Temp_ChargesDT As ChargesDT ".
			 " Left Join Ledger as LedgerDb on ChargesDT.ChargesDt_DebitAccount = LedgerDb.Ledger_Number ".
			 " Left Join Ledger as LedgerCr on ChargesDT.ChargesDt_CreditAccount = LedgerCr.Ledger_Number ".
	         " Where ChargesDt_ID = '".$PChgDtId."'";
			 
			 
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtChgDtId       = $rows[0];
	$txtOptions       = $rows[1];
	$txtDebitAcc      = $rows[2];
	$txtDebitAccName  = $rows[3];
	$txtCreditAcc     = $rows[4];
	$txtCreditAccName = $rows[5];
	$txtCurr          = $rows[6];
	$txtAmount        = $rows[7];
	$txtAmountBase1   = $rows[8];
	$txtAmountBase2   = $rows[9];
	$txtGenDebit      = $rows[10];
	$txtIsVat         = $rows[11];
	
}


$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' style='table-layout:fixed' width='560px;'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:560px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Edit Charges</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";


 echo"<tr  height='25'><td id='search-content1' style='height:34px; width:31px;'>&nbsp;</td>
				<td  align='center' id='search-content2'>
				<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitRecord(); return false;' title='Submit Record'><span><span>Submit</span></span></button>
		
		</td><td id='search-content3' style='width:31px;'>&nbsp;</td>
		      </tr>";
			  
echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";  	  	  
echo"   <table border='0' style='table-layout:fixed' align='center' valign='top'>
          <col width=90>
		  <col width=500>";

		  
echo"	
		   <tr bgcolor='#F7F7F7' height='20'>	
			 <td colspan='2' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>";
			 
		     
echo"   <tr bgcolor='#F7F7F7' height='25'>
		  <td align='left' ><span class='style12'>Options</span></td><td> <span class='style12'>";
echo"		<input type='radio' name='txtOptions' id='txtOptions' value='1' "; if($txtOptions == 1 || $txtOptions == '') echo" checked "; echo" >Amount ";
echo"		<input type='radio' name='txtOptions' id='txtOptions' value='2' "; if($txtOptions == 2) echo" checked "; echo" >Quantity ";
echo"		<input type='radio' name='txtOptions' id='txtOptions' value='3' "; if($txtOptions == 3) echo" checked "; echo" >Percentage ";
echo"		<input type='radio' name='txtOptions' id='txtOptions' value='4' "; if($txtOptions == 4) echo" checked "; echo" >Volume ";
echo"		<input type='radio' name='txtOptions' id='txtOptions' value='5' "; if($txtOptions == 5) echo" checked "; echo" >Detail Amount ";
echo"	  </span></td>";
echo"   </tr>";	

echo"   <tr bgcolor='#F7F7F7'><td><span class='style12'>Debit Account </span></td><td><span class='style12'><label>";
echo"       <input type='hidden' name='txtChgDtId' id='txtChgDtId' value='$txtChgDtId'>
            <input type='text'   name='txtDebitAcc' id='txtDebitAcc' value='$txtDebitAcc' style='width:100px;' maxlength='10'  readonly />
			<input type='text'   name='txtDebitAccName' id='txtDebitAccName' value='$txtDebitAccName' style='width:280px;'  readonly />
			</label></span>
			<label><span class='style12'><img src='img/btnopen.jpg' title=' Select Ledger ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistLedgers(1); ' /> </span></label>
		  </td>";
echo"   </tr>";		  
	
echo"   <tr bgcolor='#F7F7F7'><td><span class='style12'>Credit Account</span></td><td><span class='style12'><label>";
echo"       <input type='text'   name='txtCreditAcc' id='txtCreditAcc' value='$txtCreditAcc' style='width:100px;' maxlength='10' readonly />
			<input type='text'   name='txtCreditAccName' id='txtCreditAccName' value='$txtCreditAccName' style='width:280px;' readonly />
			</label></span>
			<label><span class='style12'><img src='img/btnopen.jpg' title=' Select Ledger ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistLedgers(2); ' /> </span></label>
		  </td>";
echo"   </tr>";
			
			 
echo"     <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Currency</span></td>";
echo"     	 <td ><span class='style12'><select name='txtCurr' id='txtCurr' style='width:90px;'>";
			echo"<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
			echo"<option ";
				if ($txtCurr==$curf[0])	echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"</select></span></td>";					 
echo"	  </tr>";
echo"	  <tr bgcolor='#F7F7F7' height='25'>";
echo"		<td valign='center' align='left' ><span class='style12'>Amount</span></td>";
echo"		<td align='left'><input type='text' name='txtAmount' value='$txtAmount' maxlength='20' style='text-align:right; font-size:13px; width:90px; ' >";
echo"        <input type='hidden' name='txtGenDebit' id='txtGenDebit' value='$txtGenDebit'> ";
echo"        <input type='hidden' name='txtIsVat' id='txtIsVat' value='$txtIsVat'> ";
echo"        <span class='style12' style='margin-left:30px;'><input type='checkbox' name='chkGenDebit' id='chkGenDebit' value='1' onclick='SelectGenDebit();'"; if($txtGenDebit == 1) echo " checked ";  echo" >&nbsp;Generate Debit</span>";
echo"        <span class='style12' style='margin-left:30px;'><input type='checkbox' name='chkIsVat' id='chkIsVat' value='1' onclick='SelectIsVat();'"; if($txtIsVat == 1) echo " checked ";  echo" >&nbsp;isVat</span></td>";
echo"	  </tr>";

		  
		 echo "</table></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";

echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:560px;' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;' valign='top'></td>";
echo" </tr>";	  
echo"</table>";

	

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

HERE;

?>