<?php

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


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

$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(ShowSlipNumber,0) As ShowSlipNumber 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, 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  ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

$querylog=" select ifNull(FC_CashRegisterQtyRet,0) As FC_CashRegisterQtyRet ".
		  " 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);
}


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

$PRetId 	= $_GET["PRetId"]; if ($PRetId == ""){$PRetId = $_POST["PRetId"];}   
$PRetDate 	= $_GET["PRetDate"]; if ($PRetDate == ""){$PRetDate = $_POST["PRetDate"];}   
$PRetCurr 	= $_GET["PRetCurr"]; if ($PRetCurr == ""){$PRetCurr = $_POST["PRetCurr"];}   
$PRetCashRegister 	= $_GET["PRetCashRegister"]; if ($PRetCashRegister == ""){$PRetCashRegister = $_POST["PRetCashRegister"];}   
$PRetClientCode 	= $_GET["PRetClientCode"]; if ($PRetClientCode == ""){$PRetClientCode = $_POST["PRetClientCode"];}   
$PRetProjectCode 	= $_GET["PRetProjectCode"]; if ($PRetProjectCode == ""){$PRetProjectCode = $_POST["PRetProjectCode"];}   
$PRetCostCentCode 	= $_GET["PRetCostCentCode"]; if ($PRetCostCentCode == ""){$PRetCostCentCode = $_POST["PRetCostCentCode"];}   

$txtVendorCode 	 = $_POST["txtVendorCode"]; if ($txtVendorCode == ""){$txtVendorCode = $_GET["txtVendorCode"];}   
$txtSlipNo 	  	 = $_POST["txtSlipNo"]; if ($txtSlipNo == ""){$txtSlipNo = $_GET["txtSlipNo"];}   
$txtItemSerialNo = $_POST["txtItemSerialNo"]; if ($txtItemSerialNo == ""){$txtItemSerialNo = $_GET["txtItemSerialNo"];}   
$txtItemReturn 	 = $_POST["txtItemReturn"]; if ($txtItemReturn == ""){$txtItemReturn = $_GET["txtItemReturn"];}   



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">
<title>Add Serials</title>
<script type="text/javascript" language="javascript">

function RefreshPage()
{
  x=document.itemserials;
  x.txtItemSerialNo.value = '';

  x.submit();

}


function ConfirmForm()
{
	ChangeLocation();
}

function LoadForm() 
{
	x=document.itemserials;
	
	x.txtItemSerialNo.disabled = false;
	document.getElementById('txtItemSerialNo').focus();
	
}

function ChangeLocation()
{
	x=document.itemserials;
	var vRetId = x.PRetId.value;
	opener.document.retail.txtId.value = vRetId;
	opener.document.retail.actionmode.value = 'submitrecord';
	opener.document.retail.action='Form_Retail.php';
	opener.document.retail.submit();
	
	self.close();
}

function SubmitForm() {
	x=document.itemserials;
	
	var SrRetId   = x.PRetId.value;
	var SrSerial  = x.txtItemSerialNo.value;
	var SrPerPage = x.perpage.value;
	
	
	var SrReturn = 0;
	if (x.txtItemReturn.checked)
		SrReturn = 1;
	
	if (SrSerial != '')
	{
		x.txtItemSerialNo.disabled = true;
		
		x.action='Form_RetailAddSerial_Actions.php?Mode=insert&ItemSerialNo='+SrSerial+'&ItemReturn='+SrReturn+'&RetId='+SrRetId+'&PerPage='+SrPerPage;
		x.submit();
	}	
	
	
}

function DeleteSerialNo(SerialId, SerialNo) {
	x=document.itemserials;
	
	var SrRetId   = x.PRetId.value;
	var SrSerial  = x.txtItemSerialNo.value;
	var SrPerPage = x.perpage.value;
	
	if (confirm ( "Are you sure you want to Delete this record ?") )
	{
		x.action='Form_RetailAddSerial_Actions.php?Mode=delete&SerialId='+SerialId+'&RetId='+SrRetId+'&PerPage='+SrPerPage;
		x.submit();
	}
	
	//x.txtItemSerialNo.focus();
	document.getElementById('txtItemSerialNo').focus();
}


function SetNextPage(startrange, perpage){
	x=document.itemserials;
	
	vRetId = x.PRetId.value;
	
	x.action='Form_RetailAddSerial.php?PRetId='+vRetId+'&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function SetPrvPage(startrange, perpage){
	x=document.itemserials;
	
	vRetId = x.PRetId.value;
	
	x.action='Form_RetailAddSerial.php?PRetId='+vRetId+'&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function showlistVendors(str){
  var vVendorCode;
  x = document.itemserials;
  
  vVendorCode  = x.txtVendorCode.value;
  vVendorCode = vVendorCode.replace('&','%26');
  
  window.open('List_Vendors.php?frm=2&PVendorCode='+vVendorCode,'mywindow','width=750,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function SelectReturnOption(){
	x=document.itemserials;

	if (x.txtItemReturn.checked) x.txtItemReturn.checked = false;
	else x.txtItemReturn.checked = true;	
	
}

function SerianNokd(e)
{
	x=document.itemserials;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
    return true;
}
function OnHelp()
{
	return false;
}
function kd(e)
{ 
	x=document.itemserials;
	if (!e) { var e = window.event;}
    var intKey = (window.Event) ? e.which : e.keyCode;
	
	 
	// Block browser short cuts     
	//F3, F4, F6, F11 key
	if (intKey == 114 || intKey == 115 || intKey == 117 || intKey == 122) {
		if(e.preventDefault) // non-IE browsers         
			e.preventDefault();     
		else  // IE Only  
		{
			e.keyCode=0;  
			e.returnValue = false; 
			
		}	
	}

	
    if (intKey == 112) { //F1 key
		if ('onhelp' in window) {   // Internet Explorer
	       window.onhelp = OnHelp;
		}
		
		return false;
	}
	else if (intKey == 113) { //F2 key
		x.btnConfirm.click();
		return false;
	}
	else if (intKey == 114) { //F3 key
		if (document.getElementById('txtVendorCode').type == 'text')
			showlistVendors(1);
		
		return false;
	}
	else if (intKey == 115) { //F4 key
		if (document.getElementById('txtSlipNo').type == 'text')
			x.txtSlipNo.focus();
		
		return false;
	}
	else if (intKey == 117) { //F6 key
		SelectReturnOption();
				
		return false;
	}
	else if (intKey == 118) { //F7 key
		x.txtItemSerialNo.focus();
			
		return false;
	}
	else if (intKey == 27) { //ESC key
	
		self.close();
		
		return false;
	}
	
	
    return true;
}



</script>

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

   
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;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 class='bodybackground'>
<body onLoad='LoadForm();'  onkeydown='return kd(event);'>
<form name='itemserials' 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='PRetDate' value='$PRetDate'>
<input type='hidden' name='PRetCurr' value='$PRetCurr'>
<input type='hidden' name='PRetId' value='$PRetId'>
<input type='hidden' name='PRetCashRegister' value='$PRetCashRegister'>
<input type='hidden' name='PRetClientCode' value='$PRetClientCode'>
<input type='hidden' name='PRetProjectCode' value='$PRetProjectCode'>
<input type='hidden' name='PRetCostCentCode' value='$PRetCostCentCode'>
			
HERE;

$sub_query = " SELECT Serial_ID, Items.Item_Code As ItemCode, Items.Item_Description As ItemDesc, ".
			 " Temp_Purchasesserialno.SerialNo_Code As SerialNo_Code, ".
			 " Temp_Purchasesserialno.Warehouse_Code As WarehouseCode, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity  ".
			 " FROM Temp_Purchasesserialno ".
			 " Inner Join Items on Temp_Purchasesserialno.Item_Code = Items.Item_Code ".
			 " Inner Join Temp_RetailDt on Temp_Purchasesserialno.RetailDt_ID = Temp_RetailDt.SaleDt_ID ".
			 " WHERE Temp_RetailDt.Sale_ID  = '".$PRetId."' ".
			 " AND Temp_RetailDt.Journal_Type In ('RI','RR') ".
			 " AND Temp_Purchasesserialno.Journal_Type In ('DI','DR') ".
			 " ORDER BY Serial_ID Desc ";

			 
			 
//echo $sub_query;

$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($sub_result);


	
if ($startrange=='')
	$startrange = 0;
	
	
if ($perpage=='' && $config['DftResultPerPage'] != '')
	$perpage = $config['DftResultPerPage'];	
if ($perpage=='')
	$perpage = 5;
		
$sub_query = $sub_query." limit ".$startrange.",".$perpage;

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='820px'>";
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'>Items and Serials
			</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' style='table-layout:fixed' align='left' cellspacing=1>
	  <col width=50>
	  <col width=50>
	  <col width=150>
	  <col width=140>
	  <col width=190>
	  <col width=60>
	  <col width=100>
	
	  <tr bgcolor='#DDDDDD'>
		<td align='center' colspan='7'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>
	  <tr class='Search-header-bg'>";

if ($CashReg_Vendor == 1){
    echo "   <td align='left' valign='center' colspan='2'><span class='style4'>F3:Vendor</span></td>
	         <td ><input type='text' name='txtVendorCode' value='$txtVendorCode'  maxlength='10' style='width:110px;' readonly>
						<label><span class='style4'><img src='img/btnopen.jpg' title=' Select Vendor '  width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistVendors(1); ' /></span></label></td>";
} else {
	echo "   <td align='left' colspan='3' ><input type='hidden' name='txtVendorCode' value='$txtVendorCode' >&nbsp;</td>";
}

if ($ShowSlipNumber == 1){
    echo "   <td align='right' valign='center' ><span class='style4'>F4:Slip No.</span></td>
	         <td><input type='text' name='txtSlipNo'  value='$txtSlipNo'  maxlength='20' style='width:110px;'></td>";
} else {
	echo "   <td align='left' colspan='2' ><input type='hidden' name='txtSlipNo' value='$txtSlipNo' >&nbsp;</td>";
}
echo "   <td align='left' valign='center' colspan='2'>";

if ($txtItemReturn == 1)
	echo"<span class='style4'><input type='checkbox' name='txtItemReturn' id='txtItemReturn' value='1' checked>F6:Return </span>";
else 	
	echo"<span class='style4'><input type='checkbox' name='txtItemReturn' id='txtItemReturn' value='1' >F6:Return </span>";

echo"	 </td>
      </tr>
	  <tr class='Search-header-bg'>
	    <td align='left' valign='center' colspan='2'><span class='style4'>F7:Serial No</span></td>
		<td align='left' valign='top' colspan='3'><span class='style4'>
			<input type='text' name='txtItemSerialNo' id='txtItemSerialNo' value='$txtItemSerialNo' size='29' maxlength='40' onkeydown='return SerianNokd(event)' >
			
			<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Submit'><span><span>Ent:Submit</span></span></button>
			<button type='button' id='btnConfirm' name='btnConfirm' onclick='ConfirmForm();return false;' title='Confirm'><span><span>F2:Confirm</span></span></button>
			</td>
		<td align='right' valign='center' colspan='2'>
		</td>";	

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

  echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:1040px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage();\">";
              
			 if ($perpage==5)
			$s5 = 'selected';
		else if ($perpage==10)
			$s10 = 'selected';	
		else if ($perpage==15)
			$s15 = 'selected';	
		else if ($perpage==20)
			$s20 = 'selected';	
		else if ($perpage==30)
			$s30 = 'selected';	
		else if ($perpage==60)
			$s60 = 'selected';
		else if ($perpage==90)
			$s90 = 'selected';
		else if ($perpage==120)
			$s120 = 'selected';

			
				
        echo'<option '.$s5.' value="5">5</option>
			 <option '.$s10.' value="10">10</option>
			 <option '.$s15.' value="15">15</option>
			 <option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s60.' value="60">60</option>
			 <option '.$s90.' value="90">90</option>
			 <option '.$s120.' value="120">120</option>
				 </select></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>";
	echo"<div style='overflow:scroll;height:400px;width:750px;'>";
	
	echo"<table border='0'  cellpadding='0' cellspacing='1' >
	<col width=30>
	  <col width=70>
	  <col width=150>
	  <col width=140>
	  <col width=190>
	  <col width=60>
	  <col width=150>
       <tr class='Search-Label-bg'>
	    <td  height='25' align='left'><span class='style4'>&nbsp; </span></td>
		<td  height='25' align='left'><span class='style4'>S/N </span></td>
		<td  height='25' align='center'><span class='style4'>Serial Code </span></td>
		<td  height='25' align='center'><span class='style4'>Item Code </span></td>
		<td  height='25' align='center' colspan='2'><span class='style4'>Item Description </span></td>
		<td  height='25' align='center'><span class='style4'>Warehouse Code </span></td>
	  </tr>";
		

	

	


	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());		
	while($sub_rows=mysql_fetch_array($sub_result)){
			
		extract($sub_rows);
		$ct+=1;

		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
				

			
		$CurrentRow = $totalrows - $startrange - $ct + 1;
			
				
		echo'<tr '.$Color.'>
				<td align="center"><span class="style5"><img src="img/delete.png" onclick="DeleteSerialNo('.$Serial_ID.',\''.$SerialNo_Code.'\');" onMouseOver="style.cursor=\'hand\'"  title="Delete Record"  ></span></td>
				<td align="left"><span class="style5">'.$CurrentRow.'</span></td>
				<td align="left"><span class="style5">'.$SerialNo_Code.'</span></td>
				<td align="left"><span class="style5">'.$ItemCode.'</span></td>
				<td align="left" colspan="2"><span class="style5">'.$ItemDesc.'</span></td>
				<td align="center"><span class="style5">'.$WarehouseCode.'</span></td>';
			
		echo'</tr>';		
		
		
		
	}
			
			
echo"</table></div></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:1040px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='74px' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='380px'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr>";
echo"</td></tr>";




echo"</table>";
	


          

		  

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

HERE;



?>