<?php

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


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

							
$querysetup="select 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);
}

$SRItemCode	= $_POST["SRItemCode"]; if ($SRItemCode == ""){$SRItemCode = $_GET["SRItemCode"];}   
$SRWhCode 	= $_POST["SRWhCode"]; if ($SRWhCode == ""){$SRWhCode = $_GET["SRWhCode"];}   
$SRItemQty 	= $_POST["SRItemQty"]; if ($SRItemQty == ""){$SRItemQty = $_GET["SRItemQty"];}   
$SRRetDtId 	= $_POST["SRRetDtId"]; if ($SRRetDtId == ""){$SRRetDtId = $_GET["SRRetDtId"];}   
$SRRetId 	= $_POST["SRRetId"]; if ($SRRetId == ""){$SRRetId = $_GET["SRRetId"];}   
$SRCount 	= $_POST["SRCount"]; if ($SRCount == ""){$SRCount = $_GET["SRCount"];}   
$SRHideSerialNo	= $_POST["SRHideSerialNo"]; if ($SRHideSerialNo == ""){$SRHideSerialNo = $_GET["SRHideSerialNo"];}   
$SRItemSerialNo	= $_POST["SRItemSerialNo"]; if ($SRItemSerialNo == ""){$SRItemSerialNo = $_GET["SRItemSerialNo"];}   


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>Item Serial No</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
  x=document.itemserials;
  x.SRItemSerialNo.value = '';
  x.action='Form_RetailSerialNo.php';
  x.submit();

}

function HideShowSerialNo()
{
	x=document.itemserials;
	
	if (x.ChkQuery.checked)
		x.SRHideSerialNo.value = 1;
	else
		x.SRHideSerialNo.value = '';
		
	x.submit();
}

function ConfirmForm()
{
	
	opener.document.retail.submit();
	self.close();
	
}

function LoadForm() 
{ 
	x=document.itemserials;
	
	var SrQty   = x.SRItemQty.value;
	var SrCount = x.SRCount.value;
	
	if( SrCount != SrQty && SrQty != '')
		opener.document.retail.QtySerNo.value = 1;
	else
		opener.document.retail.QtySerNo.value = 0;
	
	x.SRItemSerialNo.disabled = false;
	document.getElementById('SRItemSerialNo').focus();
	
}

function ChangeLocation()
{
	opener.document.retail.action='Form_Retail.php';
	opener.document.retail.submit();
}

function SubmitForm() {
	x=document.itemserials;
	
	var SrQty      = x.SRItemQty.value;
	var SrItemCode = x.SRItemCode.value;
	var SrRetDtId  = x.SRRetDtId.value;
	var SrRetId    = x.SRRetId.value;
	var SrTotal    = x.SRCount.value;
	var SrSerial   = x.SRItemSerialNo.value;
	var SrWhCode   = x.SRWhCode.value;
	
	var SrPerPage = x.perpage.value;
	var SrHide = x.SRHideSerialNo.value;
	
	SrQty = parseInt(SrQty);
	SrTotal = parseInt(SrTotal);
	
	SrWhCode = SrWhCode.replace('&','%26');
	
	if (SrWhCode == '') 
		alert("Can't add Serial No "+SrSerial+" warehouse is empty.");
		
	if (SrSerial != '' && SrWhCode != '')
	{
		x.SRItemSerialNo.disabled = true;
		
		x.action='Form_RetailSerialNo_Actions.php?Mode=insert&ItemSerialNo='+SrSerial+'&RetDtId='+SrRetDtId+'&RetId='+SrRetId+'&PerPage='+SrPerPage+'&HideSerialNo='+SrHide;
		x.submit();
	}	
	
	//x.SRItemSerialNo.focus();
	//document.getElementById('SRItemSerialNo').focus();
	
}

function DeleteSerialNo(SerialId, SerialNo) {
	x=document.itemserials;
	var SrQty      = x.SRItemQty.value;
	var Sritemcode = x.SRItemCode.value;
	var SrRetDtId  = x.SRRetDtId.value;
	var SrRetId    = x.SRRetId.value;
	var SrWhCode   = x.SRWhCode.value;
	
	var SrPerPage = x.perpage.value;
	var SrHide = x.SRHideSerialNo.value;
	
	SrWhCode = SrWhCode.replace('&','%26');
	
	if (confirm ( "Are you sure you want to Delete this record ?") )
	{
		x.action='Form_RetailSerialNo_Actions.php?Mode=delete&ItemSerialNo='+SerialNo+'&SerialId='+SerialId+'&RetDtId='+SrRetDtId+'&RetId='+SrRetId+'&PerPage='+SrPerPage+'&HideSerialNo='+SrHide;
		x.submit();
	}
	
	//x.SRItemSerialNo.focus();
	document.getElementById('SRItemSerialNo').focus();
}


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

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

function kd(e)
{
	x=document.itemserials;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		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;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style5 {
   font-size: 11px;
   color: #000000;
   font-family: Verdana, Arial, Helvetica, sans-serif;   }
   
.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>
<body onLoad='LoadForm();'  class='bodybackground'>
<form name='itemserials' method='post' action=''>
		    
HERE;
//onunLoad='ChangeLocation();'

$sub_query = " Select Item_code, Warehouse_Code, SaleDt_Quantity ".
			 " From Temp_RetailDt As RetailDt ".
		     " Where SaleDt_ID = '".$SRRetDtId."' ".
			 " And Sale_ID = '".$SRRetId."' ";
$sub_result = selectData($connection, $sub_query);
$SRItemCode = $sub_result[0];
$SRWhCode   = $sub_result[1]; 
$SRItemQty  = $sub_result[2];

$sub_query1 = " SELECT Serial_ID, Items.Item_Code As ItemCode, Items.Item_Description As ItemDesc, SerialNo_Code, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity  ".
			  " FROM temp_purchasesserialno ".
			  " Inner Join Items on temp_purchasesserialno.Item_Code = Items.Item_Code ".
			  " WHERE RetailDt_ID  = '".$SRRetDtId."' ".
			  " AND Items.Item_Code = '".$SRItemCode."'  ".
			  " AND Journal_Type In ('DI','DR') ".
			  //" AND Logged_Code = '".$_SESSION['useraccount']."' ".
			  " ORDER BY Serial_ID Desc ";

			 
			 
//echo $sub_query1;

$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($sub_result1);

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

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='530px'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:530px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Serial Numbers for Item $SRItemCode
			</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'>
	  <col width=20>
	  <col width=80>
	  <col width=250>
	  <col width=170>

	  <tr class='Search-header-bg'>
	    <td align='left' valign='center' colspan='2'><span class='style4'>Enter Serial No</span></td>
		<td align='left' valign='top' ><span class='style4'>
			<input type='text' name='SRItemSerialNo' id='SRItemSerialNo' value='$SRItemSerialNo' size='29' maxlength='40' onkeydown='return kd(event)'/>
			<input type='hidden' name='SRItemCode' id='SRItemCode' value='$SRItemCode' size='20' maxlength='40'/>
			<input type='hidden' name='SRWhCode' id='SRWhCode' value='$SRWhCode' size='20' maxlength='40'/>
			<input type='hidden' name='SRItemQty' id='SRItemQty' value='$SRItemQty' size='10' maxlength='40'/>
			<input type='hidden' name='SRRetDtId' id='SRRetDtId' value='$SRRetDtId' size='10' maxlength='40'/>
			<input type='hidden' name='SRRetId' id='SRRetId' value='$SRRetId' size='10' maxlength='40'/>
			<input type='hidden' name='SRCount' id='SRCount' value='$SRCount' size='10' maxlength='40'/>
			<input type='hidden' name='SRHideSerialNo' id='SRHideSerialNo' value='$SRHideSerialNo' size='10' maxlength='40'/>
			<br>
			<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='save'><span><span>Submit</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='ClearForm();return false;' title='Cancel'><span><span>Cancel</span></span></button>
			<button type='button' id='btnConfirm' name='btnConfirm' onclick='ConfirmForm();return false;' title='Confirm'><span><span>Confirm</span></span></button>
			</td>
		<td align='left' valign='center'><span class='style4'>Result per page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange='ClearForm();'>";
              
//<input type='text' name='SRMode' id='SRMode' value='$SRMode' size='10' maxlength='40'/>
//<input type='text' name='SRSerialId' id='SRSerialId' value='$SRSerialId' size='10' maxlength='40'/>
			
        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>';	
		echo"</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:250px;width:530px;'>
	<table border='0'  cellpadding='0' cellspacing='1' >
	 <col width=20>
	  <col width=80>
	  <col width=250>
	  <col width=170>";
	
	 echo" <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='4'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <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 No </span></td>
		<td  height='25' align='left'><span class='style4'>
		<input name='ChkQuery' type='checkbox' id='ChkQuery' value='1' onClick='HideShowSerialNo();'   "; 
	          if($SRHideSerialNo==1) echo "checked />"; else echo " /> "; 
echo" 	Hide SerialNo</span></td>
	  </tr>";
		
	
	
if ($SRHideSerialNo == '') {

	$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());		
	while($sub_rows1=mysql_fetch_array($sub_result1)){
			
		extract($sub_rows1);
		$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 height="20" align="center"><span class="style5"><img src="img/c_delete.png" onclick="DeleteSerialNo('.$Serial_ID.',\''.$SerialNo_Code.'\');" onMouseOver="style.cursor=\'hand\'"  title="Delete Record"  ></span></td>
				<td height="20" align="left"><span class="style5">'.$CurrentRow.'</span></td>
				<td height="20" align="left"><span class="style5">'.$SerialNo_Code.'</span></td>
				<td height="20" align="center"><span class="style5">&nbsp;</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:920px;' valign='top'><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table>";

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

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

}


echo"</table>";
	


          

		  

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

HERE;



?>