<?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 Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, 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 CashRegister Table
$queryCashRegister=" Select Upper(CashRegister_Name) As CashRegister_Name, Currency_Code As StpCurrCode ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

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


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

if ($TransType == 'OUT') $Title = 'Cash Payout';
else if ($TransType == 'IN') $Title = 'Cash Receipt';

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" src="js/grid/jquery-1.9.1.js"></script>
<script type="text/javascript" language="javascript">


function SubmitRecord() {
	x = document.form1;
	var vMode = 'cashtrans';
	var vTransType = x.TransType.value;
	
	var result = CheckFields();
	
	   
	if (result == 1) {
		x.action="Form_Retail_Actions.php?mode="+vMode+"&TransType="+vTransType;
		x.submit();
	} 
}

function CheckFields(){
	
  
    x = document.form1;
	
	var vCurr	 = x.txtCashCurr.value;
	var vAmount  = x.txtCashAmount.value;
	var vDesc	 = x.txtCashDesc.value;
	
	
	var result = 1;
	
	if (vCurr=='' )
	{
		alert("Currency is mandatory!");
		x.txtCashCurr.focus();
		result = 0;
	}
	else if (vAmount=='')
	{
		alert("Amount is mandatory!.");
		x.txtCashAmount.focus();
		result = 0;
	}
	else if ((vAmount*1 != vAmount/1 ) || vAmount < 0) {
		alert('Amount should be a POSITIVE and NUMERIC value. ');
		x.txtCashAmount.focus();
		result = 0;
	}
	else if (vDesc=='' )
	{
		alert("Description is mandatory!");
		x.txtCashDesc.focus();
		result = 0;
	}

	
	return result;	
}


function LoadForm(){
	x=document.form1;
	x.txtCashAmount.focus();
}

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


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.form1;
	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.btnSubmit.click();
		return false;
	}
	else if (intKey == 27) { //ESC key
		x.btnClose.click();
		return false;
	}
	
	
    return true;
}


function FillInput(PValue){
	var vField=document.form1.FocusField.value;
	
	if (document.getElementById(vField)) {
		if(isTextSelected(document.getElementById(vField))){
			document.form1[vField].value=PValue;
		} else {
			document.form1[vField].value=document.form1[vField].value+PValue;
		}
	
		$(document.getElementById(vField)).trigger("change");
	}
}

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.form1.FocusField.value=PField;
}

function EraseInput(PValue){
	var vField=document.form1.FocusField.value;
	document.form1[vField].value='';
	
	if (document.getElementById(vField)) {
		$(document.getElementById(vField)).trigger("change");
	}
}

function PrintSummary() {
	var x = document.form1;
	
	var str = 'Form_RetailPayOuts.php?';
	window.open(str, 'Form_RetailPayOuts', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500,top=100');	

}
function PrintToday(){
	var str = 'Form_RetailPayOuts.php?printtoday=1';
	window.open(str, 'Form_RetailPayOuts', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500,top=100');	

}

</script>
<title>CashRegister- Cash Trans</title>
<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>


</head>
<body onload='LoadForm();' onkeydown='return kd(event);'>
<form name='form1' 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='TransType' value='$TransType'>
<input type='hidden' name='FocusField' value='$FocusField'>
<input type='hidden' name='usrac' value='$usrac'>

HERE;


echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' style='table-layout:fixed' width='680px;'>";
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'>$Title</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='0' style='table-layout:fixed' align='center' valign='top' >
          <col width=325>
		  <col width=300>

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

echo"	  	<tr bgcolor='#F7F7F7' height='50'>
				<td align='left' valign='top'><table cellspacing=1 cellpadding=1 border='0' style='padding-top:5px;'>";
echo"				<tr>
						<td width='100px;' align='left'><span class='style4'>Amount</span></td> 
						<td width='75px;' align='right'><select name='txtCashCurr' id='txtCashCurr' class='roundInputPos' style='width:60px; height:26px;'  >";
						echo"<option></option>";
						while($curf=mysql_fetch_row($resultcurrency)){
							echo"<option ";
							if ($base1==$curf[0]) echo " selected ";
							echo" value='$curf[0]'>$curf[0] </option>";
						}
						
echo"					</select></td>
						<td width='150px;' align='right'>
							<input type='text' name='txtCashAmount' id='txtCashAmount' value='' maxlength='20' onfocus=\"FillHidden('txtCashAmount')\";  class='roundInputPos' style='text-align:right; color:#FF0000; width:140px; height:27px;'  />
						</td>
					<tr>
					<tr><td colspan='3' align='left'><span class='style4'>Description</span></td></tr>
					<tr><td colspan='3' align='left'><textarea cols='36' rows='6' name='txtCashDesc' id='txtCashDesc'></textarea></td></tr>";	
				
echo"			</table></td>
				<td align='right' valign='top'><table cellspacing=1 cellpadding=1 border='0' style='padding-top:5px;'>
				<tr>
					<td rowspan='2'><input type='button' id='btnSubmit' name='btnSubmit' value='Enter' class='submitbutton' onclick=\"SubmitRecord();\"></td>
					<td><input type='button' value='.' class='calculatorbutton' onclick=FillInput('.');></td>
					<td><input type='button' value='1' class='calculatorbutton' onclick='FillInput(1);'></td>
					<td><input type='button' value='2' class='calculatorbutton' onclick='FillInput(2);' id='button2'></td>
					<td><input type='button' value='3' class='calculatorbutton' onclick='FillInput(3);'></td>
				</tr>
				<tr>
					<td><input type='button' value='00' class='calculatorbutton' onclick=FillInput('00');></td>
					<td><input type='button' value='4' class='calculatorbutton' onclick='FillInput(4);'></td>
					<td><input type='button' value='5' class='calculatorbutton' onclick='FillInput(5);'></td>
					<td><input type='button' value='6' class='calculatorbutton' onclick='FillInput(6);'></td>
				</tr>
				<tr>
					<td><input type='button' value='Clr' class='calculatorbutton' onclick='EraseInput();'></td>
					<td><input type='button' value='0' class='calculatorbutton' onclick='FillInput(0);'></td>
					<td><input type='button' value='7' class='calculatorbutton' onclick='FillInput(7);'></td>
					<td><input type='button' value='8' class='calculatorbutton' onclick='FillInput(8);'></td>
					<td><input type='button' value='9' class='calculatorbutton' onclick='FillInput(9);'></td>
				</tr>
				</table></td>
			</tr>	

			<tr bgcolor='#F7F7F7' height='40px'><td colspan='2' align='right'>&nbsp;</td></tr>
			<tr bgcolor='#F7F7F7' height='25px'>	 
				<td colspan='2' align='right' bgcolor=#D8D8D8>
					<button type='button' id='btnPrint' name='btnPrint' onclick='PrintToday();return false;'   title='Print Today'><span><span>Print Today</span></span></button>
					<button type='button' id='btnPrint' name='btnPrint' onclick='PrintSummary();return false;'   title='Print'><span><span>Print</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>";
			 
echo"	</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;

?>