<?php

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


include("config.inc.php");
include("Functions_Sales.php");

set_time_limit(0);

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");



//Read From Login Table
$querylog=" select ifNull(FC_CashRegisterDisc,0) As FC_CashRegisterDisc, ifNull(FC_DiscountPerLimit,0) As FC_DiscountPerLimit ".
		  " 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);
}

// read in case discount user is different

if($discount_user != ''){
	 $querylog=" select ifNull(FC_CashRegisterDisc,0) As FC_CashRegisterDisc, ifNull(FC_DiscountPerLimit,0) As FC_DiscountPerLimit ".
			  " from ".$_SESSION['dbLabel'].".login where user_account='".$discount_user."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}
}

//echo $querylog;
	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ifNull(ShowSlipNumber,0) As ShowSlipNumber, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage,ifnull(cashreg_colortheme,'default') as cashreg_colortheme from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_Pack,0) As Module_Pack, ".
             " ifNull(Module_Delivery,0) As Module_Delivery, ifNull(Module_PriceOffer,0) As Module_PriceOffer From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


//Read From CashRegister Table
$queryCashRegister=" Select Upper(CashRegister_Name) As CashRegister_Name, Currency_Code As StpCurrCode, CostCenter_Code As StpCostCentCode, Project_Code As StpProjectCode, ".
                   " CashRegister_SerialNo, CashRegister_Discount, CashRegisterTTC, Client_Account As StpClientAccount, Warehouse_Code As StpWarehouseCode, ifNull(CashReg_Vendor,0) As CashReg_Vendor,  ".
				   " ifnull(cashregister_service,0) as cashregister_service, ifnull(cashregister_servicePercentage, 0) as cashregister_servicePercentage, ".
				   " ifnull(cashregister_TaxConsumption,0) as cashregister_TaxConsumption, ifnull(cashregister_TaxConsumptionpPer,0) as cashregister_TaxConsumptionpPer ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

$PassDisc 	= $_GET["PassDisc"]; if ($PassDisc == ""){$PassDisc = $_POST["PassDisc"];}   

$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}   
$mainaction = $_POST["mainaction"]; if ($mainaction == ""){$mainaction = $_GET["mainaction"];}   
$message 	= $_POST["message"]; if ($message == ""){$message = $_GET["message"];}   
$PRetId 	= $_POST["PRetId"]; if ($PRetId == ""){$PRetId = $_GET["PRetId"];}   

$txtFocusField 		   = $_POST["txtFocusField"]; if ($txtFocusField == ""){$txtFocusField = $_GET["txtFocusField"];}   
$txtAllowDisc 		   = $_POST["txtAllowDisc"]; if ($txtAllowDisc == ""){$txtAllowDisc = $_GET["txtAllowDisc"];}   
$txtRetTaxable 		   = $_POST["txtRetTaxable"]; if ($txtRetTaxable == ""){$txtRetTaxable = $_GET["txtRetTaxable"];}   
$txtRetNoneTaxable 	   = $_POST["txtRetNoneTaxable"]; if ($txtRetNoneTaxable == ""){$txtRetNoneTaxable = $_GET["txtRetNoneTaxable"];}   
$txtRetGrossTotal 	   = $_POST["txtRetGrossTotal"]; if ($txtRetGrossTotal == ""){$txtRetGrossTotal = $_GET["txtRetGrossTotal"];}   
$txtRetGrossTotalOffer = $_POST["txtRetGrossTotalOffer"]; if ($txtRetGrossTotalOffer == ""){$txtRetGrossTotalOffer = $_GET["txtRetGrossTotalOffer"];}   
$txtRetVatAmt 		   = $_POST["txtRetVatAmt"]; if ($txtRetVatAmt == ""){$txtRetVatAmt = $_GET["txtRetVatAmt"];}   
$txtRetNetTotalDisp    = $_POST["txtRetNetTotalDisp"]; if ($txtRetNetTotalDisp == ""){$txtRetNetTotalDisp = $_GET["txtRetNetTotalDisp"];}   
$txtRetCurr 		   = $_POST["txtRetCurr"]; if ($txtRetCurr == ""){$txtRetCurr = $_GET["txtRetCurr"];}   
$txtRetCurr1 		   = $_POST["txtRetCurr1"]; if ($txtRetCurr1 == ""){$txtRetCurr1 = $_GET["txtRetCurr1"];}   
$txtRetCurr2 		   = $_POST["txtRetCurr2"]; if ($txtRetCurr2 == ""){$txtRetCurr2 = $_GET["txtRetCurr2"];}   
$txtRetDiscountPer     = $_POST["txtRetDiscountPer"]; if ($txtRetDiscountPer == ""){$txtRetDiscountPer = $_GET["txtRetDiscountPer"];}   
$txtRetDiscountAmt 	   = $_POST["txtRetDiscountAmt"]; if ($txtRetDiscountAmt == ""){$txtRetDiscountAmt = $_GET["txtRetDiscountAmt"];}   
$txtRetNetTotal 	   = $_POST["txtRetNetTotal"]; if ($txtRetNetTotal == ""){$txtRetNetTotal = $_GET["txtRetNetTotal"];}   




//If User has no access to add discount then password form will open
if ($FC_CashRegisterDisc == 0 && $PassDisc == 0 && $actionmode == '')
	header("location:Form_RetailDiscount_Password.php?PForm=1&mainaction=".$mainaction."&actionmode=&PRetId=".$PRetId);

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" />
HERE;

if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}

print <<<HERE
<script type="text/javascript" language="javascript">


function SubmitRecord(DecimalBase, SalesTTC, DiscountPerLimit, ModulePriceOffer) {
	x = document.retaildisc;
	
	var result = CheckFields(DiscountPerLimit);
	//var result = 1;
	var vRetId    = x.PRetId.value;
	
	var vMainMode = x.mainaction.value;
	var discount_user = x.discount_user.value;
	
	var vMode = 'adddiscount';	
	
	vDiscResult = CalcDiscount(3, DecimalBase, SalesTTC, ModulePriceOffer);
	   
	if (result == 1 && vDiscResult != 0) {
		x.action="Form_Retail_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SRetId="+vRetId+"&discount_user="+discount_user;
		x.submit();
		
	}	
}

function CloseForm() {
	ClearForm();
	
	self.close();
}

function CheckFields(DiscountPerLimit){
  
    x = document.retaildisc;
	
	var vRetDiscPer  = x.txtRetDiscountPer.value;
	var vRetDiscAmt  = x.txtRetDiscountAmt.value;
	
	if (vRetDiscPer == '') vRetDiscPer = 0;
	if (vRetDiscAmt == '') vRetDiscAmt = 0;
	
	vRetDiscPer   = parseFloat(vRetDiscPer);
	vDiscPerLimit = parseFloat(DiscountPerLimit);
	
	var vmode = x.actionmode.value;
	
	
	/*if (vRetDiscPer==0 && vRetDiscAmt==0)
	{
		alert("Discount is mandatory!");
		x.txtRetDiscountAmt.focus();
		return 0;
	}
	else */if (vRetDiscPer > 0 && vDiscPerLimit >= 0 && vRetDiscPer > vDiscPerLimit) {
		alert('Discount should be less than or equal to allowed limit ('+vDiscPerLimit+')');
		x.txtRetDiscountPer.focus();
		return 0;
	}
		
	
	else
	return 1 ;	
}


function ChangeLocation()
{
	x = document.retaildisc;
	var vMode = x.actionmode.value;
	
    if (vMode == 'discountadded') {
		//opener.document.retail.action='Form_Retail.php';
		opener.document.retail.submit();
		self.close();
	} 
}


function LoadForm() 
{
	x=document.retaildisc;
	x.txtRetDiscountPer.focus();
	x.txtRetDiscountPer.select();
	
	ChangeLocation();
}

function ClearForm(){
    x = document.retaildisc;
	var vmode = x.actionmode.value;
	
	x.txtRetCurr.value='';
	x.txtRetCurr1.value='';
	x.txtRetCurr2.value='';
	x.txtRetVatAmt.value='';
	x.txtRetGrossTotal.value='';
	x.txtRetDiscountPer.value='';
	x.txtRetDiscountAmt.value='';
	x.txtRetNetTotal.value='';
	x.txtRetNetTotalDisp.value='';
	x.txtRetTaxable.value='';
	x.txtRetNoneTaxable.value='';
	x.txtRetVatPerc.value='';
		
	x.message.value='';
	x.actionmode.value = '';
	

}


function CalcDiscount(option, dec, SalesTTC, ModulePriceOffer) {
	x = document.retaildisc;
	
	var vTaxableDisc, vNoneTaxableDisc, vTaxableAmt, vNoneTaxableAmt;
	
	var vRetDiscPer     	= x.txtRetDiscountPer.value;
	var vRetDiscAmt     	= x.txtRetDiscountAmt.value;
	var vRetTaxable     	= x.txtRetTaxable.value;
	var vRetNoneTaxable 	= x.txtRetNoneTaxable.value;
	var vRetVatPerc     	= x.txtRetVatPerc.value;
	var vRetGrossTotal  	= x.txtRetGrossTotal.value;
	var vRetGrossTotalOffer = x.txtRetGrossTotalOffer.value;
	var vRetVatAmt      	= x.txtRetVatAmt.value;
	var vRetNetTotal    	= x.txtRetNetTotal.value;
	var vRetServiceChecked	= x.serviceChecked.value;
	var vRetService	    	= x.cashregister_service.value;
	var vRetServicePercentage 	= x.cashregister_servicePercentage.value;
	var vRetTaxConsumption    	= x.cashregister_TaxConsumption.value;
	var vRetTaxConsumptionpPer 	= x.cashregister_TaxConsumptionpPer.value;
	var vRetServiceAmnt 	= x.serviceAmount.value;
	var vRetTaxConspAmnt 	= x.TaxConsumptAmount.value;
	
	
	
	if ((vRetDiscAmt*1 != vRetDiscAmt/1 ) ) {
		alert('Discount Amount should be a NUMERIC value.');
		x.txtRetDiscountAmt.focus();
		return 0;
	}
	else if ((vRetDiscPer*1 != vRetDiscPer/1 || vRetDiscPer < 0) ) {
		alert('Discount Percentage should be a NUMERIC value.');
		x.txtRetDiscountPer.focus();
		return 0;
	}
	else if ((vRetNetTotal*1 != vRetNetTotal/1 ) ) {
		alert('Net Total should be a NUMERIC value.');
		x.txtRetNetTotal.focus();
		return 0;
	}
	
	if (vRetDiscPer == '')     		vRetDiscPer = 0;
	if (vRetDiscAmt == '')     		vRetDiscAmt = 0;
	if (vRetTaxable == '')     		vRetTaxable = 0;
	if (vRetNoneTaxable == '') 		vRetNoneTaxable = 0;
	if (vRetVatPerc == '')     		vRetVatPerc = 0;
	if (vRetVatAmt == '')      		vRetVatAmt = 0;
	if (vRetGrossTotal == '')  		vRetGrossTotal = 0;
	if (vRetGrossTotalOffer == '')  vRetGrossTotalOffer = 0;
	if (vRetNetTotal == '')    		vRetNetTotal = 0;
	if (vRetServiceAmnt == '')		vRetServiceAmnt = 0;
	if (vRetTaxConspAmnt == '') 	vRetTaxConspAmnt = 0;
	
	
	vRetDiscPer     	= parseFloat(vRetDiscPer);
	vRetDiscAmt     	= parseFloat(vRetDiscAmt);
	vRetTaxable     	= parseFloat(vRetTaxable);
	vRetNoneTaxable 	= parseFloat(vRetNoneTaxable);
	vRetVatPerc     	= parseFloat(vRetVatPerc);
	vRetVatAmt      	= parseFloat(vRetVatAmt);
	vRetGrossTotal  	= parseFloat(vRetGrossTotal);
	vRetGrossTotalOffer = parseFloat(vRetGrossTotalOffer);
	vRetNetTotal    	= parseFloat(vRetNetTotal);
	vRetServiceAmnt    	= parseFloat(vRetServiceAmnt);
	vRetTaxConspAmnt    = parseFloat(vRetTaxConspAmnt);
	
	
	
	if (vRetGrossTotal != 0) {
		
		if (option == 1 ) {  //Option by Percentage Get The Amount
		
			vTaxableDisc     = 0; 
			vNoneTaxableDisc = 0; 
			vTaxableAmt      = 0; 
			vNoneTaxableAmt  = 0;
					
			vRetVatAmt   = 0;
			vRetDiscAmt  = 0;
			vRetNetTotal = 0;
			
			//vTaxableDisc     = vRetTaxable*vRetDiscPer/100;
			//vNoneTaxableDisc = vRetNoneTaxable*vRetDiscPer/100;
			//vTaxableAmt      = vRetTaxable     - vTaxableDisc;
			//vNoneTaxableAmt  = vRetNoneTaxable - vNoneTaxableDisc;
			
			if (ModulePriceOffer > 0)
				vRetDiscAmt  = vRetGrossTotalOffer*vRetDiscPer/100;
			else
				vRetDiscAmt  = vRetGrossTotal*vRetDiscPer/100;
			
			if (SalesTTC == 1) {	
				if (vRetTaxable > 0) {
					vRetVatAmt = roundNumber((((vRetTaxable - (vRetDiscAmt*vRetTaxable/vRetGrossTotal)) / (1 + vRetVatPerc/100)) * (vRetVatPerc/100)),dec);
				}
				
				if (vRetService == 1 && vRetServiceChecked == 1)
					vRetServiceAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetServicePercentage/100);
				if (vRetTaxConsumption == 1)
					vRetTaxConspAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetTaxConsumptionpPer/100);
					
				vRetNetTotal = 	roundNumber((vRetGrossTotal + vRetServiceAmnt + vRetTaxConspAmnt - vRetDiscAmt),dec);
				
				
			}
			else {
				if (vRetTaxable > 0) {
					vRetVatAmt = roundNumber(((vRetTaxable - (vRetDiscAmt*vRetTaxable/vRetGrossTotal))*vRetVatPerc/100),dec);
				} 
				
				if (vRetService == 1 && vRetServiceChecked == 1)
					vRetServiceAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetServicePercentage/100);
				if (vRetTaxConsumption == 1)
					vRetTaxConspAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetTaxConsumptionpPer/100);
				
				vRetNetTotal = 	roundNumber((vRetGrossTotal + vRetVatAmt + vRetServiceAmnt + vRetTaxConspAmnt - vRetDiscAmt),dec);
				
				//vRetVatAmt   = vTaxableAmt*vRetVatPerc/100;
				//vRetNetTotal = vRetGrossTotal + vRetVatAmt - vRetDiscAmt;
			}	
			
			x.serviceAmount.value     = Math.round((vRetServiceAmnt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.TaxConsumptAmount.value = Math.round((vRetTaxConspAmnt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetVatAmt.value      = Math.round((vRetVatAmt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetDiscountAmt.value = Math.round((vRetDiscAmt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetNetTotal.value    = Math.round((vRetNetTotal)*Math.pow(10,dec))/Math.pow(10,dec);
			
		}	
		else if (option == 2) {  //Option by Amount Get The Per
			x.txtRetDiscountPer.value = 0;
			
			vTaxableDisc     = 0; 
			vNoneTaxableDisc = 0; 
			vTaxableAmt      = 0; 
			vNoneTaxableAmt  = 0;
					
			vRetVatAmt   = 0;
			vRetDiscPer  = 0;
			vRetNetTotal = 0;
			
			vRetDiscPer  = (vRetDiscAmt/vRetGrossTotal)*100;
			
			//vTaxableDisc     = vRetTaxable*vRetDiscPer/100;
			//vNoneTaxableDisc = vRetNoneTaxable*vRetDiscPer/100;
			//vTaxableAmt      = vRetTaxable     - vTaxableDisc;
			//vNoneTaxableAmt  = vRetNoneTaxable - vNoneTaxableDisc;
			
			//vRetVatAmt   = vTaxableAmt*vRetVatPerc/100;
			//vRetNetTotal = vRetGrossTotal + vRetVatAmt - vRetDiscAmt;
			
			if (SalesTTC == 1) {		
				if (vRetTaxable > 0) {
					vRetVatAmt = roundNumber((((vRetTaxable - (vRetDiscAmt*vRetTaxable/vRetGrossTotal)) / (1 + vRetVatPerc/100)) * (vRetVatPerc/100)),dec);
				}
				
				if (vRetService == 1 && vRetServiceChecked == 1)
					vRetServiceAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetServicePercentage/100);
				if (vRetTaxConsumption == 1)
					vRetTaxConspAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetTaxConsumptionpPer/100);
				
				vRetNetTotal = 	roundNumber((vRetGrossTotal + vRetServiceAmnt + vRetTaxConspAmnt - vRetDiscAmt),dec);
			}
			else {
				if (vRetTaxable > 0) {
					vRetVatAmt = roundNumber(((vRetTaxable - (vRetDiscAmt*vRetTaxable/vRetGrossTotal))*vRetVatPerc/100),dec);
				} 
				
				if (vRetService == 1 && vRetServiceChecked == 1)
					vRetServiceAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetServicePercentage/100);
				if (vRetTaxConsumption == 1)
					vRetTaxConspAmnt = (((vRetGrossTotal+vRetVatAmt)-vRetDiscAmt)*vRetTaxConsumptionpPer/100);
				
				vRetNetTotal = 	roundNumber((vRetGrossTotal + vRetVatAmt + vRetServiceAmnt + vRetTaxConspAmnt - vRetDiscAmt),dec);
			}
			
			x.serviceAmount.value     = Math.round((vRetServiceAmnt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.TaxConsumptAmount.value = Math.round((vRetTaxConspAmnt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetVatAmt.value      = Math.round((vRetVatAmt)*Math.pow(10,dec))/Math.pow(10,dec);
		    x.txtRetDiscountPer.value = Math.round((vRetDiscPer)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetNetTotal.value    = Math.round((vRetNetTotal)*Math.pow(10,dec))/Math.pow(10,dec);
			
		}
		else if (option == 3 ) {  //Option by Net Amount Get The Disc Per and Amt
		
			vRetDiscAmt = 0;
			if (SalesTTC == 1) {
				
				vRetDiscAmt = (vRetGrossTotal + vRetServiceAmnt + vRetTaxConspAmnt - vRetNetTotal);
				vRetVatAmt  = roundNumber((((vRetTaxable - (vRetDiscAmt*vRetTaxable/vRetGrossTotal)) / (1 + vRetVatPerc/100)) * (vRetVatPerc/100)),dec);
			} else {
				
				vRetDiscAmt = (((vRetGrossTotal + vRetServiceAmnt + vRetTaxConspAmnt - vRetNetTotal + (vRetTaxable*vRetVatPerc/100))*vRetGrossTotal)/(vRetGrossTotal + (vRetTaxable*vRetVatPerc/100)));
				vRetVatAmt  = (vRetNetTotal - vRetGrossTotal  + vRetDiscAmt);
			}	
			//alert(vRetDiscAmt);
			//alert(vRetVatAmt);
		    x.txtRetDiscountAmt.value = Math.round((vRetDiscAmt)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetDiscountPer.value = Math.round(((vRetDiscAmt/vRetGrossTotal)*100)*Math.pow(10,dec))/Math.pow(10,dec);
			x.txtRetVatAmt.value      = Math.round((vRetVatAmt)*Math.pow(10,dec))/Math.pow(10,dec);
			
		}		

			
	}   
	

}

function CheckAllowDiscount(){
	x=document.retaildisc;
	
	x.txtAllowDisc.value = 0;
	if(x.chkAllowDisc.checked)
		x.txtAllowDisc.value = 1;

}

function roundNumber(num, dec) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  newnumber = parseFloat(newnumber); 
  
  return newnumber;
}

function kd(e)
{ 
	x=document.retaildisc;
	if (!e) { var e = window.event;}
    var intKey = (window.Event) ? e.which : e.keyCode;
	
	 
	// Block browser short cuts     
	//F1, F3, F4  key
	if (intKey == 112 || intKey == 114 || intKey == 115 ) {
		if(e.preventDefault) // non-IE browsers         
			e.preventDefault();     
		else  // IE Only  
		{
			e.keyCode=0;  
			e.returnValue = false; 
			
		}	
	}

	
    if (intKey == 113) { //F2 key
		x.btnOk.click();
		return false;
	}
	else if (intKey == 27) { //ESC key
		x.btnClose.click();
		return false;
	}
	
	
    return true;
}

function FillInput(PValue,DecimalBase,CashRegisterTTC,Module_PriceOffer){
	var vField=document.retaildisc.txtFocusField.value;
	
	if(isTextSelected(document.getElementById(vField))){
		document.retaildisc[vField].value=PValue;
	} else {
		document.retaildisc[vField].value=document.retaildisc[vField].value+PValue;
	}
	
	
	if(document.retaildisc.txtFocusField.value=='txtRetDiscountPer')
		CalcDiscount(1, DecimalBase,CashRegisterTTC,Module_PriceOffer);
	else if(document.retaildisc.txtFocusField.value=='txtRetDiscountAmt')	
		CalcDiscount(2, DecimalBase,CashRegisterTTC,Module_PriceOffer);
}

function isTextSelected(input) {
    if (typeof input.selectionStart == "number") {
        return input.selectionStart == 0 && input.selectionEnd == input.value.length;
    } else if (typeof document.selection != "undefined") {
        input.focus();
        return document.selection.createRange().text == input.value;
    }
}

function FillHidden(PField){
	document.retaildisc.txtFocusField.value=PField;
}

function EraseInput(PValue){
	var vField=document.retaildisc.txtFocusField.value;
	document.retaildisc[vField].value='';
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Discount</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;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 12px;
	color: #999999;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.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;}

 button {
  padding: 0 0 0 0;
  margin: 0 3px 0 3px;
  border: none;
  cursor: pointer;
  text-decoration: none;
  outline: none;
  white-space: nowrap;
  overflow: visible;
  height: 22px; 
  font-family: 11px Arial, sans-serif;
  font-size: 13px;
  color: #000000;
  background: none;
  font-weight: bold;

}
</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>

</head>
<body onLoad='LoadForm();' onkeydown='return kd(event);'>
<form name='retaildisc' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='PRetId' value='$PRetId'>
<input type='hidden' name='txtFocusField' value='$txtFocusField'>
		
HERE;
	


if($PRetId != ''){

	$sub_query = " Select Sale_ID, Date_Format(Sale_Date,'%d/%m/%Y') As Sale_Date, Currency_code, ".
				 " Round(ifNull(Sale_Taxable,0), ".$DecimalBase."), Round(ifNull(Sale_NoneTaxable,0), ".$DecimalBase."),".
				 " Round(ifNull(Sale_Taxable,0) + ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
				 " Round(ifNull(Sale_VAT,0), ".$DecimalBase.") As Sale_VAT, ".
				 " Round(ifNull(Sale_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Sale_DiscountAmount,0), ".$DecimalBase."),  ".
				 " Round((ifNull(Sale_Taxable,0) + ifNull(Sale_NoneTaxable,0) + IfNull(Sale_VAT,0) - IfNull(Sale_DiscountAmount,0) + (ifNull(Sale_Tax1,0) + ".
				 "        ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))), ".$DecimalBase.") As SaleNetTotal,  ".
				 " Round(ifNull(Sale_NetTotal,0), ".$DecimalBase."),  ".
				 " Round(ifNull(Sale_Total,0) + (ifNull(Sale_Taxable,0)*$Setup_VatPercentage/100), ".$DecimalBase."), ".
				 " Round((ifNull(Sale_Taxable,0)* (1 + $Setup_VatPercentage/100)) + ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As GrossTotalTTC, ".
				 " Round((((ifNull(Sale_Taxable,0)*(1+$Setup_VatPercentage/100)) + ifNull(Sale_NoneTaxable,0))*(ifNull(Sale_DiscountPer,0)/100)), ".$DecimalBase.") As DiscountTTC, ".
				 " Round((ifNull(Sale_Taxable,0)* (1 + $Setup_VatPercentage/100)), ".$DecimalBase.") As TaxableTTC,  ifnull(sale_serviceChecked,0),   ".
				 "  Round(ifNull(sale_service,0), ".$DecimalBase."),  Round(ifNull(sale_TaxConsumption,0), ".$DecimalBase.") ".
				 " From Temp_Retail As Retail ".
				 " Where Sale_ID = ".$PRetId;
				 
	//echo $sub_query;
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
    $sub_rows=mysql_fetch_array($sub_result);
	
	$txtRetCurr          = $sub_rows[2];
	
	if ($CashRegisterTTC == 1) {
		$txtRetTaxable       = $sub_rows[14];
		$txtRetGrossTotal    = $sub_rows[12];
		$txtRetDiscountAmt   = $sub_rows[13];
	}	
	else {
		$txtRetTaxable       = $sub_rows[3];
		$txtRetGrossTotal    = $sub_rows[5];
		$txtRetDiscountAmt   = $sub_rows[8];
	}	
		
	$txtRetNoneTaxable   = $sub_rows[4];
	$txtRetVatAmt        = $sub_rows[6];
	$txtRetDiscountPer   = $sub_rows[7];
	$txtRetNetTotal      = $sub_rows[10];
	$txtRetNetTotalDisp  = number_format($sub_rows[11], $DecimalBase);
	//$txtRetNetTotalDisp  = number_format($sub_rows[4], $DecimalBase);

	if($Module_PriceOffer > 0) {
		$sub_query1 = " Select Sum((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_Price,0))-((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_Price,0)*ifNull(SaleDt_DiscountPer,0))/100)) As Total, ".
					  " Sum((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceTTC,0))-((ifNull(SaleDt_Quantity,0)*ifNull(SaleDt_PriceTTC,0)*ifNull(SaleDt_DiscountPer,0))/100)) As TotalTTC ".
					  " From Temp_RetailDt ".
					  " Where Sale_ID = '".$PRetId."' ".
					  " And ifNull(Price_offer,0) = 0 ";
		$sub_result1 = selectData($connection, $sub_query1);
		
		if ($CashRegisterTTC == 1)
			$txtRetGrossTotalOffer = $sub_result1[1];
		else
			$txtRetGrossTotalOffer = $sub_result1[0];
		
	}

	$serviceChecked		= $sub_rows[15];	
	$serviceAmount		= $sub_rows[16];	
	$TaxConsumptAmount	= $sub_rows[17];	
		
	
}



//$style= "style=' width:70px; height:22px; 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='700px;'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px;' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Discount</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

	  
echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";  	  	  
echo"   
        <table border=0' cellpadding='1' style='table-layout:fixed' align='center' valign='top' >
          <col width=75>
		  <col width=200>
		  <col width=70>

		   <tr bgcolor='#F7F7F7'>	
			 <td colspan='5' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>

			 <tr bgcolor='#F7F7F7' height='25px'>	 <td colspan='4' align='left' bgcolor=#D8D8D8>
			 <button type='button' id='btnOk' name='btnOk'    onclick='SubmitRecord($DecimalBase,$CashRegisterTTC,$FC_DiscountPerLimit,$Module_PriceOffer);return false;'   title='Save Changes'><span><span>F2:Ok</span></span></button>
			 <button type='button' id='btnClose' name='btnClose'    onclick='CloseForm();return false;'   title='Close'><span><span>Esc:Close</span></span></button>
	
			 </td></tr>
			 
		  <tr bgcolor='#F7F7F7' height='50'>
             <td valign='center' align='left' ><span class='style4'>Gross Total</span></td> 
			 <td align='left' >
				 <input type='hidden' name='txtAllowDisc' value='$txtAllowDisc' >
				 <input type='hidden' name='txtRetTaxable' value='$txtRetTaxable' > 
				 <input type='hidden' name='txtRetNoneTaxable' value='$txtRetNoneTaxable' > 
				 <input type='hidden' name='txtRetGrossTotal' value='$txtRetGrossTotal' > 
				 <input type='hidden' name='txtRetGrossTotalOffer' value='$txtRetGrossTotalOffer' > 
				 <input type='hidden' name='txtRetVatAmt' value='$txtRetVatAmt' > 
				 <input type='hidden' name='serviceAmount' value='$serviceAmount' > 
				 <input type='hidden' name='TaxConsumptAmount' value='$TaxConsumptAmount' > 
				 <input type='hidden' name='serviceChecked' value='$serviceChecked' > 
				 <input type='hidden' name='cashregister_service' value='$cashregister_service' > 
				 <input type='hidden' name='cashregister_servicePercentage' value='$cashregister_servicePercentage' > 
				 <input type='hidden' name='cashregister_TaxConsumption' value='$cashregister_TaxConsumption' > 
				 <input type='hidden' name='cashregister_TaxConsumptionpPer' value='$cashregister_TaxConsumptionpPer' > 
			     <input type='hidden' name='txtRetVatPerc' value='$Setup_VatPercentage' > 
			     <input type='hidden' name='discount_user' value='$discount_user' > 
			     <input type='text' name='txtRetNetTotalDisp' value='$txtRetNetTotalDisp' maxlength='20' style='background-color: #000000; color:#00FF00; text-align:right; font-size:16px; font-weight:bold; width:195px; height:27px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' readonly ></td>
			 <td align='left' >	<input type='text' name='txtRetCurr' value='$txtRetCurr' maxlength='20' style='background-color: #000000; color:#00FF00; font-size:16px; font-weight:bold; width:50px; height:27px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' readonly ></td>
		<td rowspan='4'><table cellspacing=1 cellpadding=1 border='0' style='padding-top:5px;'>

<tr>
<td><input type='button' value='.' class='calculatorbutton' onclick=FillInput('.',$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);></td>
<td>&nbsp;</td>

<td><input type='button' value='1' class='calculatorbutton' onclick='FillInput(1,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
<td><input type='button' value='2' class='calculatorbutton' onclick='FillInput(2,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);' id='button2'></td>
<td><input type='button' value='3' class='calculatorbutton' onclick='FillInput(3,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
</tr>


<tr>
<td><input type='button' value='00' class='calculatorbutton' onclick=FillInput('00',$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);></td><td >&nbsp;</td>

<td><input type='button' value='4' class='calculatorbutton' onclick='FillInput(4,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>

<td><input type='button' value='5' class='calculatorbutton' onclick='FillInput(5,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
<td><input type='button' value='6' class='calculatorbutton' onclick='FillInput(6,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
</tr>

<tr><td><input type='button' value='0' class='calculatorbutton' onclick='FillInput(0,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
	<td><input type='button' value='Clr' class='calculatorbutton' onclick='EraseInput();'></td>
	
	<td><input type='button' value='7' class='calculatorbutton' onclick='FillInput(7,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
	<td><input type='button' value='8' class='calculatorbutton' onclick='FillInput(8,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
	<td><input type='button' value='9' class='calculatorbutton' onclick='FillInput(9,$DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'></td>
</tr>";
//echo"<tr><td><input type='button' value='<=' class='calculatorbutton' onclick='EraseFromInput();'></td></tr>";

echo"</table></td>  </tr>	

		  
		  <tr bgcolor='#F7F7F7' height='35'>
             <td valign='center' align='left' ><span class='style4'>Discount</span></td> 
			 <td align='left'>
			    <input type='text' name='txtRetDiscountPer' value='$txtRetDiscountPer' id='txtRetDiscountPer' maxlength='20' onfocus=FillHidden('txtRetDiscountPer'); onChange='CalcDiscount(1, $DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'  style='text-align:right; font-size:16px; font-weight:bold; width:60px; height:27px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' > %
			    <input type='text' name='txtRetDiscountAmt' value='$txtRetDiscountAmt' id='txtRetDiscountAmt' maxlength='20' onfocus=FillHidden('txtRetDiscountAmt'); onChange='CalcDiscount(2, $DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'  style='text-align:right; font-size:16px; font-weight:bold; width:115px; height:27px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' >
			 </td>
			 <td align='left' >	<input type='text' name='txtRetCurr1' value='$txtRetCurr' id='txtRetCurr' onfocus=FillHidden('txtRetCurr'); maxlength='20' style='font-size:14px; font-weight:bold; width:50px; height:22px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' readonly ></td>
		  </tr>	 	
		  
		  <tr bgcolor='#F7F7F7' height='35'>
             <td valign='center' align='left' ><span class='style4'>Net Total</span></td> 
			 <td align='left'><input type='text' name='txtRetNetTotal' value='$txtRetNetTotal' id='txtRetNetTotal' onfocus=FillHidden('txtRetNetTotal'); maxlength='20' onChange='CalcDiscount(3, $DecimalBase,$CashRegisterTTC,$Module_PriceOffer);'   style='text-align:right; font-size:16px; font-weight:bold; width:195px; height:27px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' ></td>
			 <td align='left' >	<input type='text' name='txtRetCurr2' value='$txtRetCurr' maxlength='20'  style='background-color: #000000; color:#00FF00; font-size:16px; font-weight:bold; width:50px; height:27px;-moz-border-radius: 0px 5px 5px 5px;
			-webkit-border-radius: 5px 5px 5px 5px;
			border-radius: 5px 5px 5px 5px;' readonly ></td>
		  </tr>	"; 	
		  
if ($Module_PriceOffer > 0) {
	echo" <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' colspan='3'><span class='style4'><input type='checkbox' name='chkAllowDisc' id='chkAllowDisc' onclick='CheckAllowDiscount()' value='1'>Allow discount even all Invoice Items are offer</span></td> 
		  </tr>	"; 	
}	  
			 
echo"	  <tr bgcolor='#F7F7F7' height='30'>

			 
		  </tr>
        </table>";

	 echo "</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></table>";	  

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

HERE;

?>