<?php

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


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

set_time_limit(0);

$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");

$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
		     " ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent ".
             " From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


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

$MainCompanyName=explode('_',$HTTP_SESSION_VARS['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$HTTP_SESSION_VARS['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	
			  
//echo $queryperiod;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);	


$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$querygroup= " select distinct group_code, group_name ".
			 " from grouping where group_code in (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
			 " order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist  = mysql_num_rows($resultgroup);


$querywarehouse=" select distinct warehouse.warehouse_code, warehouse_name ".
				" from warehouse where warehouse_code in (select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
				" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


//Get Default Project
$querydflproject="select project_code as defaultproject from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 2;

if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;


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

function ClearForm()
{
  document.search.keyword.value='';
  document.search.PProjectCode.value='';
  document.search.PCostCentCode.value='';

  document.search.action='Form_Assemble_LoadPreprod.php';
  document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.keyword.focus();
	
	ChangeLocation();
}

function SetNextPage(startrange,perpage){
	
	document.search.action='Form_Assemble_LoadPreprod.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){

	document.search.action='Form_Assemble_LoadPreprod.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}




function SelectRecord(RecordId){
	
	document.search.PMode.value=1;	
	document.search.action="Form_Assemble_Actions.php?mainmode=loadpreprod&SSelectdId="+RecordId;
	document.search.submit();
	
}

function ChangeLocation()
{
    if (document.search.PMode.value == 1) {
		var GenId = document.search.GeneratedId.value;
		
		opener.document.production.txtPrdId.value=GenId;
	    opener.document.production.mainaction.value='';
		opener.document.production.actionmode.value='submitrecord';
		opener.document.production.action='Form_Assemble.php';
		opener.document.production.submit();
		self.close();
	}	
}



function SelectOrderBy()
{
  document.search.keyword.value='';

  document.search.action='Form_Assemble_LoadPreprod.php';
  document.search.submit();

}

function SelectSearchBy()
{	

  //document.search.keyword.value='';
  //document.search.action='Form_Assemble_LoadPreprod.php';
  document.search.submit();

}

function ShowHideDetails(PrePrdId, Option) {
	if (Option == 'S') {
		document.search.SelectedID.value = PrePrdId;
	}
	else if (Option == 'H') {
		document.search.SelectedID.value = '';
	}	
	document.search.action='Form_Assemble_LoadPreprod.php?SelectedID='+PrePrdId;
	document.search.submit();
}


function SubmitForm()
{	

  //document.search.keyword.value='';
  document.search.action='Form_Assemble_LoadPreprod.php?startrange=0';
  document.search.submit();

}

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

function showlistProject(str){

  window.open('List_SorProject.php?frm=6&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(str){

  window.open('List_SorCostcenter.php?frm=6&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_Assemble_LoadPreprod.php?startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_Assemble_LoadPreprod.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}
</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 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {color: #5151FF; text-decoration:underline}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='GeneratedId' value='$GeneratedId'>
<input type='hidden' name='SelectedID' id='SelectedID' value='$SelectedID'>
HERE;


	
$PRef = GetSQLValueString($keyword, 'text');

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
			 
$querysearch=" Select Distinct PreProduction.Production_ID, PreProduction.Production_Reference, Date_Format(PreProduction.Production_Date,'%d-%m-%Y') As Production_Date, ".
             " PreProduction.Item_code As ItemCode, Items.Item_Description As ItemDesc, PreProduction.Warehouse_Code, Warehouse_Name, PreProduction.Production_Quantity, ".
			 " PreProduction.Production_IdealQty, Project.Project_Name As ProjectName, Costcent.Costcent_name As CostcentName, PreProduction.Currency_code, ".
			 " Round(PreProduction.Production_Total, ".$DecimalBase.") As ProductionTotal, Round(PreProduction.Production_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal ".
			 " From PreProduction ".
			 " Left Join Items on PreProduction.Item_code = Items.Item_code ".
			 " Left Join Warehouse on PreProduction.Warehouse_Code = Warehouse.Warehouse_Code ".
			 " Left Join Project on PreProduction.Project_Code = Project.Project_Code ".
			 " Left Join Costcent on PreProduction.Costcenter_code = Costcent.Costcent_code ";
			 
if ($projectexist >	0)
  $querysearch= $querysearch." Inner join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and PreProduction.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and PreProduction.project_code = Q1.project_code )) ";
							 
if ($costcentexist > 0)
  $querysearch= $querysearch." Inner join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and PreProduction.Costcenter_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and PreProduction.Costcenter_code = Q2.costcent_code )) ";

							 
$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and PreProduction.Production_Reference Like Concat(ifNull($PRef, PreProduction.Production_Reference),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(PreProduction.Production_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(PreProduction.Production_Date,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 3 and (PreProduction.Item_code Like Concat(ifNull($PRef, PreProduction.Item_code),'%') or Items.Item_Description Like Concat('%', ifNull($PRef, Items.Item_Description),'%')) ) Or  ".
			 "        ($searchby = 4 and (PreProduction.Warehouse_Code Like Concat(ifNull($PRef, PreProduction.Warehouse_Code),'%') or Warehouse.Warehouse_Name Like Concat('%', ifNull($PRef, Warehouse.Warehouse_Name),'%'))) ) ".
			 " And ifNull(PreProduction.PreProduction_used,0) = 0 ".
			 " And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(PreProduction.Production_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',PreProduction.Production_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(PreProduction.Production_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ".
			 " And ifNull(PreProduction.Production_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
			 
if ($groupexist > 0)
    $querysearch = $querysearch. " And Items.group_code In (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."')  ";			 
	
if ($whexist > 0)
    $querysearch = $querysearch. " And PreProduction.warehouse_code In (select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ";			 	
	
			 
if ($PProjectCode != '')
	$querysearch= $querysearch." And PreProduction.project_code like concat('".$PProjectCode."','%') ";
if ($PCostCentCode != '')
	$querysearch= $querysearch." And PreProduction.costcenter_code like concat('".$PCostCentCode."','%') ";				 
			 

if ($orderby == 1)
	$querysearch= $querysearch." Order By PreProduction.Production_Date Desc, PreProduction.Production_Reference Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By PreProduction.Production_Reference Desc ";	
	
			 
//echo $querysearch;			 

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);

		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		



echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed'width='940px'>";
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'>List of Preproductions
			</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' cellspacing='1'  style='table-layout:fixed'>
	  <col width=25>
	  <col width=25>
	  <col width=70>
	  <col width=80>
	  <col width=100>
	  <col width=200>
	  <col width=80>
	  <col width=100>
	  <col width=90>
	  <col width=100>
	
	  <tr class='Search-header-bg'>";
	  
if ($Module_Project == 1) {
	echo" <td align='left' colspan='2'><span class='style4'>Project</span></td>
	      <td align='left' colspan='2'><span class='style4'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' size='15' maxlength='15' /></span>
		    <label><span class='style4'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
} else {
	echo" <td align='left' colspan='4'><input type='hidden' name='PProjectCode' id='PProjectCode' value='$PProjectCode' /></td>";

}			

if ($Module_CostCenter == 1) {		  
	echo" <td align='right'><span class='style4'>Costcenter</span></td>		
		  <td align='left' ><span class='style4'>
		    <input type='text' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' size='17' maxlength='15' /></span>
		    <label><span class='style4'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
			
} else {
	echo" <td align='left' colspan='2'><input type='hidden' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' /></td>";

}	  

echo" <td align='right' colspan='2'>&nbsp;</td>";
echo" <td align='right' colspan='2'>&nbsp;</td>	";
  
echo" </tr>
	  <tr bgcolor='#DDDDDD'>	  
	    <td align='left' colspan='2'><span class='style4'>Search By</span></td>
		<td align='left' colspan='4'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30' onkeydown='return kd(event)'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
			
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
			if ($searchby==1)
				$s21 = 'selected';
			else if ($searchby==2)
				$s22 = 'selected';
			else if ($searchby==3)
				$s23 = 'selected';
			else if ($searchby==4)
				$s24 = 'selected';		
				
			echo"<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>Reference</option>
				 <option ".$s23." value='3'>Item</option>
				 <option ".$s24." value='4'>Warehouse</option>";
			echo"</select>";	
				 
echo"	    <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='ClearForm();return false;' title='Cancel'><span><span>Cancel</span></span></button></td>";
echo" <td align='right' ><span class='style4'>Order By</span></td>		
		  <td align='left' colspan='3'><span class='style4'><select name='orderby' id='orderby' value='$orderby' onchange='SelectOrderBy();'>";
		
		if ($orderby==1)
			$s11 = 'selected';
		else if ($orderby==2)
			$s12 = 'selected';
			
		echo"<option ".$s11." value='1'>Date</option>
			 <option ".$s12." value='2'>Reference</option>
			 </select></span></td>";		
			 
echo"</tr>
</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:1025px;'>
		<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==20)
				$s20 = 'selected';
			else if ($perpage==30)
				$s30 = 'selected';
			else if ($perpage==40)
				$s40 = 'selected';
			else if ($perpage==50)
				$s50 = 'selected';

						
							
			echo'<option '.$s20.' value="20">20</option>
				 <option '.$s30.' value="30">30</option>
				 <option '.$s40.' value="40">40</option>
				 <option '.$s50.' value="50">50</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"&nbsp;&nbsp;<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"&nbsp;&nbsp;<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>";			
if($message!=''){
		echo"	  <tr class='Search-header-bg'>
		<td height='15' align='center' colspan='8' class='searchmessage'>".$message."</td>
		</tr>";
	}
	
	
	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:870px;'>
			<table border='0'  cellpadding='0' cellspacing='1' >
			<col width=25>
			  <col width=25>
			  <col width=70>
			  <col width=80>
			  <col width=100>
			  <col width=200>
			  <col width=80>
			  <col width=100>
			  <col width=90>
			  <col width=70>
	";
echo" </tr>
	 
	  <tr class='Search-Label-bg'>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style4'>Reference </span></td>
		<td  height='25' align='center'><span class='style4'>Date </span></td>
		<td  height='25' align='center'><span class='style4'>Item</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>Warehouse</span></td>
		<td  height='25' align='center'><span class='style4'>Project</span></td>
		<td  height='25' align='center'><span class='style4'>CostCenter</span></td>
		<td  height='25' align='center'><span class='style4'>Actual Qty</span></td>
	  </tr>";
		

	

while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			
	
	echo'<tr '.$Color.'>
			<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="SelectRecord('.$Production_ID.');" title="Load Record"  ></span></td>';
			
			
	if ($SelectedID != "" && $Production_ID == $SelectedID)		
		echo'<td align="center"><span class="style5"><img src="img/hide.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$Production_ID.', \''.'H'.'\');" title="Hide Details" height="15px" width="15px"></span></td>';
	else	
		echo'<td align="center"><span class="style5"><img src="img/show.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$Production_ID.', \''.'S'.'\');" title="Show Details" height="15px" width="15px"></span></td>';
		
		
	echo'	<td align="left"><span class="style5">'.$Production_Reference.'</span></td>
			<td align="center"><span class="style5">'.$Production_Date.'</span></td>
			<td align="left"><span class="style5">'.$ItemCode.'</span></td>
			<td align="left"><span class="style5">'.$ItemDesc.'</span></td>
			<td align="left"><span class="style5">'.$Warehouse_Code.'</span></td>
			<td align="left"><span class="style5">'.$ProjectName.'</span></td>
			<td align="left"><span class="style5">'.$CostcentName.'</span></td>
			<td align="center"><span class="style5">'.$Production_Quantity.'</span></td>
		</tr>';
		
	if ($SelectedID != "" && $Production_ID == $SelectedID) {
		$sub_query = " Select PreProductionDt.Item_Code As ItemCodeRM, Item_Description as ItemDescriptionRM, ".
					 " PreProductionDt.Warehouse_Code As WarehouseCodeRM, ifNull(ProductionDt_Quantity,0) As ProductionDt_Quantity".
					 " From PreProductionDt Left Join Items on PreProductionDt.Item_Code = Items.Item_code ".
					 " Where PreProductionDt.Production_ID = '".$Production_ID."' ";
		$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
		$sub_totalrows = mysql_num_rows($sub_result);
		
		if ($sub_totalrows > 0) {
			echo "<tr ".$Color.">";
			echo "<td colspan='2' valign='top' align='left'>&nbsp;</td>";
			echo "<td colspan='8' valign='top' align='left'><div style='border:1px solid #33CCFF; z-index:9999; background-color:#CCFFFF; padding-left:5px; padding-bottom:5px;'>";
			echo "<table width='95%' border='0' cellpading='0' cellspacing='1'>";
			echo "<tr><td colspan='4' align='center'><span class='style17'>Raw Materials</span></td></tr>";
			echo "<tr><td width='20%'><span class='style17'>Item Code</span></td><td width='50%'><span class='style17'>Description</span></td> <td width='15%' align='center'><span class='style17'>Warehouse</span></td><td width='15%' align='center'><span class='style17'>Quantity</span></td>";
			while($sub_rows=mysql_fetch_array($sub_result)){
				echo "<tr><td>".$sub_rows[0]."</td><td>".$sub_rows[1]."</td><td>".$sub_rows[2]."</td><td align='right'>".number_format($sub_rows[3],$DecimalBase)."</td></tr>";
			
			}
			
			echo "</tr></table></div></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 height=28px>
		<tr><td style='width:280px;'>&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='150' 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"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr>";

echo"</table>";
	


          

		  

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

HERE;

?>