<?php

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


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


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


	
//Read From Setup Table							
$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);
}


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


function SubmitRecord() {
	x = document.productiondt;
	
	var result = CheckFields();
	var vPrdId    = x.PPrdId.value;
	var vPrdDtId  = x.PPrdDtId.value;
	var vItemType = x.PItemType.value;
	var vMainMode = x.mainaction.value;
	var vMode= x.actionmode.value;
	
	if (vPrdDtId == '')
		vMode = 'submitrecord';	
	else
		vMode = 'editdt';	
	   
	if (result == 1) {
		x.action="Form_ProductionReq_Actions.php?SItemType="+vItemType+"&mode="+vMode+"&mainmode="+vMainMode+"&SPrdId="+vPrdId+"&SPrdDtId="+vPrdDtId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.productiondt;
	var vMode = x.actionmode.value;
	
	if (vMode == 'submitrecord') {
		
		opener.document.production.actionmode.value = x.actionmode.value;
		opener.document.production.mainaction.value = x.mainaction.value;
		//opener.document.production.message.value = x.message.value;
		opener.document.production.txtPrdId.value = x.PPrdId.value;
		opener.document.production.submit();
		
	} 
	else if (vMode == 'submitrecord' || vMode == 'editdt') {
		opener.document.production.actionmode.value = x.actionmode.value;
		opener.document.production.mainaction.value = x.mainaction.value;
		opener.document.production.txtPrdId.value = x.PPrdId.value;
		opener.document.production.submit();
		
		if (x.message.value == "")
			self.close();
	
	}
	
	//x.txtItemCode.focus();
}

function CheckFields(){
  
    x = document.productiondt;
	
	var PrdDtQty = x.txtPrdDtQty.value;
	PrdDtQty = parseFloat(PrdDtQty);
	
	var vmode = x.actionmode.value;
	
	if ((PrdDtQty == '' || PrdDtQty*1 != PrdDtQty/1 || PrdDtQty < 0)) {
		alert('Quantity should be a NUMERIC and POSITIVE value ');
		x.txtPrdDtQty.focus();
		return 0;
	}
		
	
	else
	return 1 ;	
}


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

function showlistWarehouse(str){
	window.open('List_SorWarehouse.php?frm=7&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,top=50,left=100');
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Production Request Detail - Add Items</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: 9px;
	color: #999999;
	font-weight: bold;
}
.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;}

</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='ChangeLocation();' onkeydown='return kd(event)'>
<form name='productiondt' 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='PItemType' value='$PItemType'>
<input type='hidden' name='PPrdId' value='$PPrdId'>
<input type='hidden' name='PPrdDtId' value='$PPrdDtId'>

HERE;


//Edit Production Detail Mode
if($PPrdDtId != ''){
	if ($PItemType == 'RM') {
		$querydt = " Select ProductionReqRM_ID, ProductionReqRM.Item_CodePacked, ProductionReqRM.Item_Code, Item_Description, ".
				   " ProductionReqRM.Warehouse_Code, Warehouse.Warehouse_Name, ProductionReqRM_Quantity ".
				   " From Temp_ProductionReqRM As ProductionReqRM ".
				   " Inner Join Items on ProductionReqRM.Item_Code = Items.Item_Code ".
				   " Left Join Warehouse on ProductionReqRM.Warehouse_Code = Warehouse.Warehouse_Code ".
				   " Where ProductionReqRM.ProductionReqRM_ID = ".$PPrdDtId;
	}
	else if ($PItemType == 'FG') {
		$querydt = " Select ProductionReqFG_ID, ProductionReqFG.Item_CodePacked, ProductionReqFG.Item_Code, Item_Description, ".
				   " ProductionReqFG.Warehouse_Code, Warehouse.Warehouse_Name, ProductionReqFG_Quantity ".
				   " From Temp_ProductionReqFG As ProductionReqFG ".
				   " Inner Join Items on ProductionReqFG.Item_Code = Items.Item_Code ".
				   " Left Join Warehouse on ProductionReqFG.Warehouse_Code = Warehouse.Warehouse_Code ".
				   " Where ProductionReqFG.ProductionReqFG_ID = ".$PPrdDtId;
	}
	//echo $querydt;
	$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
    $rowsdt=mysql_fetch_array($resultdt);
 	
	$txtPrdDtId         = $rowsdt[0];
	$txtItemCode        = $rowsdt[2];
	$txtItemDesc        = $rowsdt[3];
	$txtWarehouseCode   = $rowsdt[4];
	$txtWarehouseName   = $rowsdt[5];
	$txtPrdDtQty        = $rowsdt[6];
	
}

$i_unitcoef = 1;

if($txtItemCode != ''){
	$query = " SELECT Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1) ".
			 " FROM items WHERE Item_Code = '".$txtItemCode."'";
		
	//echo "<br><br>".$query;
	$result = selectData($connection, $query);
	$i_unit1  	  = $result[0];
	$i_unit2 	  = $result[1]; 
	$i_unitcoef	  = $result[2];
	
	$txtItemUnit1 = $i_unit1;
	$txtItemUnit2 = $i_unit2;

}

//$style= "style=' 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='580px;'>";
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'>Add Items</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";


 echo"<tr  height='25'><td id='search-content1' style='height:34px; width:31px;'>&nbsp;</td>
				<td  align='left' id='search-content2'><table width='100%' align='left' border='0'>";
		
	echo"	<tr >";
echo"	  <td colspan='2' align='center'>

 <button type='button' id='btnSubmitRecord' name='btnSubmitRecord'  onclick='SubmitRecord();return false;' title='Submit Record'><span><span>Submit Record</span></span></button>";
echo"	  </td>";
echo"	</tr>";			
		echo"</table></td><td id='search-content3' style='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='center' valign='top'>
          <col width=100>
		  <col width=450>";

if($txtWhCode != ''){
	$querywh = "select warehouse_name from warehouse where warehouse_code='$txtWhCode'";
	$reswh = mysql_query($querywh,$connection);
	$rowwh = mysql_fetch_row($reswh);
	$txtWhDesc = $rowwh[0];
}		  
		  
if($txtWhCode == '') $txtWhCode=$txtWarehouseCode;
if($txtWhDesc == '') $txtWhDesc=$txtWarehouseName;		   
echo"	
		   <tr bgcolor='#F7F7F7' height='25'>	
			 <td colspan='2' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>			 
			 
		   <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Warehouse&nbsp;<span class='style26'>*</span></span></td>
			 <td>
			 <input type='text' name='txtWhCode' id='txtWhCode' value='$txtWhCode' style='width:120px;' maxlength='10'  />
           <input type='text' name='txtWhDesc' id='txtWhDesc' value='$txtWhDesc' style='width:270px;'  />
			 <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Warehouse' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistWarehouse(1); ' /> </span></label></td>
		   </tr>"; 
			
echo"     <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Item&nbsp;<span class='style26'>*</span></span></td>";
echo" 	     <td><b>$txtItemCode - $txtItemDesc</b></td>";
echo"     </tr>	";

echo"     <tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style12'>Quantity&nbsp;<span class='style26'>*</span></span></td>";
echo" 	     <td><input type='text' name='txtPrdDtQty' value='$txtPrdDtQty' maxlength='20' style='font-size:12px; width:80px; height:18px;' >&nbsp;&nbsp;<b>$txtItemUnit1</b></td>";
echo"     </tr>	";

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

?>