<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

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

$querylog="SELECT FI_Production as loginaccess, ifNull(Login_FastPrint,0) As Login_FastPrint from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['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);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");

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

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_RestrictProjCost,0) As Module_RestrictProjCost, ifNull(Module_RestrictCost,0) As Module_RestrictCost, ".
			 " ifNull(Module_SerialNo,0) As Module_SerialNo, ifNull(Module_PostToJournal,0) As Module_PostToJournal, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt, ".
			 " ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ifNull(Module_ExpireDate,0) As Module_ExpireDate, ".
			 " ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter, ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject, ".
			 " ifNull(Module_Pack,0) As Module_Pack, ifNull(Module_DirectStockPD,0) As Module_DirectStockPD ".
			 " ,ifNull(Module_Heatno,0) As Module_Heatno,ifNull(Module_Sorderproduction,0) As Module_Sorderproduction ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Read From ProductionSetup Table
$queryProductionSetup=" Select ifNull(DisableIdeal,0) As DisableIdeal, Currency_code As SetupDftCurrency, Warehouse_code As SetupDftWareHouse, ".
					  " ifNull(ShowCostCentLayout,0) As ShowCostCentLayout, ifNull(Actual_Zero,0) As Actual_Zero, ifNull(Production_extraInfo,0) As Production_extraInfo ".
					  " ,ifNull(ShowStage,0) As ShowStage, ifNull(ShowAdditions,0) As ShowAdditions, ifnull(show_preproductionCost,0) as show_preproductionCost ".
					  " From ProductionSetup";
$resultProductionSetup=mysql_query($queryProductionSetup,$connection) or die(mysql_error());
while($rowProductionSetup=mysql_fetch_array($resultProductionSetup)){
	extract($rowProductionSetup);
}

//Read From SorderSetup Table
$querySorderSetup=" Select ifnull(sorderproduction,0) as sorderproduction From SorderSetup ";
$resultSorderSetup=mysql_query($querySorderSetup,$connection) or die(mysql_error());
while($rowSorderSetup=mysql_fetch_array($resultSorderSetup)){
	extract($rowSorderSetup);
}

$querydflwh="select default_warehouse_code as defaultwarehouse from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_warehouse_code != '' ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}

$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"];}   


if (empty($mainaction))
	$mainaction = 'insert';

$message = ereg_replace("'"," ",$message);



print <<<HERE
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">

<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>


<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>

<link type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>

<script type="text/javascript" language="javascript">
	$(document).ready(function() { 
		
		$(document).on('focus', '.date', function() {
			$('.date').datepick({showTrigger: '#calImg'});
		});
		
		$(document).on('focusin', '.table-focus-g1', function() {
			AutoSaveRecordGrid(this.id,'SelectedRec','ModRec_G1','imgSubmit');
			selRecordColor(this,'SelectedRec','SelectedRecColor');
		});
		
		//Set Scroll Positions on Page Load
		scrollToPage();
		
		$(window).scroll(function(){
			scrollPage();
		});
		
		//Set Cursor Position After Page Reload
		var NextElement = readCookie('ass-nextcell');
		MoveNextRecord(NextElement);
		eraseCookie('ass-nextcell');
		
		
		
	});
	
	
</script>

<script type="text/javascript" language="javascript">


function checkduplicate(PItem) {


x = document.production;
	
		var xmlhttp = getXMLHttpRequest();	
		var SaId  = x.txtPrdId.value;
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = xmlhttp.responseText;
					 
					if(data==1)
						alert('Duplicate Item');
				
			}
		}
	    
		xmlhttp.open("GET","Form_PreAssemble_Actions.php?mode=checkduplicate&PItem="+PItem+"&SSaId="+SaId);
		xmlhttp.send();


}
 
function showlistHeatNo(PIndex) {
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	var vHeatNoCode = encodeURIComponent(document.getElementById('productiondt_heatno['+PIndex+']').value);
	
	window.open('List_HeatNo.php?frm=8&index='+PIndex+'&PHeatNoCode='+vHeatNoCode,'List_HeatNo','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}
function ShowSlidingDiv(PDivID) {

	$(PDivID).slideToggle();
}


function CheckFields(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate){
  
    x = document.production;
	
	var PrdDate     = x.txtDate.value;
	var PrdItemCode = x.txtItemCodeFG.value;
	var PrdWhCode   = x.txtWhCode.value;
	var PrdCurr     = x.txtCurr.value;
	var PrdProject  = x.txtProjectCode.value;
	var PrdCostCenter  = x.txtCostCentCode.value;
	var PrdQty         = x.txtPrdQty.value;
	var ItemWithSerial = x.txtItemWithSerialNo.value;
	
	var PrdExpireDate = x.txtPrdExpireDate.value;
	var PrdBatchNo    = x.txtPrdBatchNo.value;
	
	if (PrdQty == '') PrdQty = 0;
	if (ItemWithSerial == '') ItemWithSerial = 0;
	
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	
	if (PrdDate=='' )
	{
		alert("Date Column is mandatory!");
		x.txtDate.focus();
		return 0;
	}
	else if (PrdItemCode=='' )
	{
		alert("Item Column is mandatory!");
		x.txtItemCodeFG.focus();
		return 0;
	}
	else if (PrdWhCode=='' )
	{
		alert("Warehouse Column is mandatory!");
		x.txtWhCode.focus();
		return 0;
	}
	else if (PrdCurr=='' )
	{
		alert("Currency Column is mandatory!");
		x.txtCurr.focus();
		return 0;
	}
	else if (PrdProject=='' && ModuleRestProj==1 && ModuleProj==1)
	{
		alert("Project is mandatory!");
		x.txtProjectCode.focus();
		return 0;
	}
	else if (PrdCostCenter=='' && ModuleRestCost==1 && ModuleCost==1 && document.getElementById('txtCostCentCode').type == 'text')
	{
		alert("CostCenter is mandatory!");
		x.txtCostCentCode.focus();
		return 0;
	}
	else if (PrdQty == 0 && ItemWithSerial == 0){
		alert('Item Actual Quantity is mandatory! ');
		x.txtPrdQty.focus();
		return 0;
	}
	else if (PrdQty*1 != PrdQty/1 || PrdQty < 0) {
		alert('Quantity should be a NUMERIC and POSITIVE value! ');
		x.txtPrdQty.focus();
		return 0;
	}
	else if (ItemExpire == 1 && ModuleExpireDate==1 && PrdExpireDate == '' ) {
		alert('Item Expire Date is mandatory.');
		x.txtPrdExpireDate.focus();
		return 0;
	}
	else if (ItemExpire == 1 && ModuleExpireDate==1 && PrdBatchNo == '' ) {
		alert('Item Batch No is mandatory.');
		x.txtPrdBatchNo.focus();
		return 0;
	}
	
			
	
	else
	return 1 ;	
}


function CheckFieldsDt(PIndex,ItemExpire,ModuleExpireDate,ItemWithSerial,ModuleSerialNo){
  
    x = document.production;
	
	var PrdDtItem 	   = document.getElementById('itemcode['+PIndex+']').value;
	var PrdDtWarehouse = document.getElementById('warehousecode['+PIndex+']').value;
	var PrdDtQty       = document.getElementById('prddtquantity['+PIndex+']').value;
	var PrdDtPrice     = 0;
	if(document.getElementById('costprice['+PIndex+']'))
		PrdDtPrice = document.getElementById('costprice['+PIndex+']').value;
	
	var PrdDtExpireDate = document.getElementById('expiredate['+PIndex+']').value;
	var PrdDtBatchNo    = document.getElementById('batchno['+PIndex+']').value;
	
	
	var vmode = x.actionmode.value;
	
	if (PrdDtPrice == '')   PrdDtPrice = 0;
	if (PrdDtQty == '')     PrdDtQty = 0;
	
	PrdDtQty     = parseFloat(PrdDtQty);	
	
	
	if (PrdDtItem=='')
	{
		alert("Item is mandatory!");
		document.getElementById('itemcode['+PIndex+']').focus();
		return 0;
	}	
	else if (PrdDtWarehouse=='')
	{
		alert("Warehouse is mandatory!");
		document.getElementById('warehousecode['+PIndex+']').focus();
		return 0;
	}
	/*else if (PrdDtQty*1 != PrdDtQty/1 || PrdDtQty <= 0  ) {
		alert('Quantity ('+PrdDtQty+') should be NUMERIC and POSITIVE and GREATER Than Zero. ');
		document.getElementById('prddtquantity['+PIndex+']').focus();
		return 0;
	}*/
	else if (PrdDtQty == 0  ) {
		alert('Item Quantity is mandatory. ');
		document.getElementById('prddtquantity['+PIndex+']').focus();
		return 0;
	}
	else if ((PrdDtPrice*1 != PrdDtPrice/1 || PrdDtPrice < 0) ) {
		alert('Cost Price should be a NUMERIC and POSITIVE value');
		document.getElementById('costprice['+PIndex+']').focus();
		return 0;
	}
	else if (ItemExpire == 1 && ModuleExpireDate==1 && PrdDtExpireDate == '' ) {
		alert('Item Expire Date is mandatory.');
		document.getElementById('expiredate['+PIndex+']').focus();
		return 0;
	}
	else if (ItemExpire == 1 && ModuleExpireDate==1 && PrdDtBatchNo == '' ) {
		alert('Item Batch No is mandatory.');
		document.getElementById('batchno['+PIndex+']').focus();
		return 0;
	}
	
		
	
	else
	return 1 ;	
}


function ClearPage(){
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		x = document.production;
		x.mainaction.value='insert';
		x.actionmode.value='';
		x.action='Form_PreAssemble.php';
		x.submit();
	}	
}

function ClearPage1(){
	x = document.production;
	var msg = x.message.value;
	
	ClearForm('All');
	
	x.mainaction.value='insert';
	x.actionmode.value='';
	x.message.value=msg;
	x.action='Form_PreAssemble.php';
	x.submit();
}


function CancelRecord(){
	var x = document.production;
	var vmode   = x.mainaction.value;
	var vmode1  = x.actionmode.value;
	var PrdId   = x.txtPrdId.value;
		
	if (confirm ( "Are you sure you want to Delete current pending production permenantly?"))
	{
		x.action="Form_PreAssemble_Actions.php?mode=cancel&mainmode=&SPrdId="+PrdId;
		x.submit();
	}
    
}

function CheckExtraInfo(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate){
	var x = document.production;
	var PrdId  = x.txtPrdId.value;
	var vMainMode = x.mainaction.value;
	var xmlhttp = getXMLHttpRequest();	
	
	if(xmlhttp) {
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = $.parseJSON(xmlhttp.responseText);
				
				var msg = data[0].message;
				
				if (msg != "") {
				alert(decodeURIComponent(msg));
				AddInfo(PrdId);
				} else {	
					PostChanges(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate) 
					
				}
			}
		}
		
		xmlhttp.open("GET","Form_PreAssemble_Actions.php?mode=CheckExtraInfo&SPrdId="+PrdId);
		xmlhttp.send();
		
	}
}
function PostChanges(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate) {
	var x = document.production;
	var PrdId  = x.txtPrdId.value;
	var vMainMode = x.mainaction.value;
	
	var result = CheckFields(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate);
	var CountSelectRec = CheckIfSelected();
	
	if (x.txtPrdCountDetail.value == 0 || x.txtPrdCountDetail.value == '') {
		alert('Save not allowed. Please add at least one item.');
	}
	
	else if (CountSelectRec == 0) {
		alert('Save not allowed. Please select at least one item.');
	}

	else
	{
		if (result == 1) {
			if (confirm ( "Are you sure you want to Save current preproduction?"))
			{
				//Save Grid Before Posting	
				var vModRec  = document.getElementById('ModRec_G1').value;
				if (vModRec != "") {
					var Obj = document.getElementById('CurRec'+vModRec);
					Obj.style.backgroundColor='#F13A3A';
					alert("Please Submit changes before.");
					
				} else {
					if (document.getElementById('btnPost'))
						x.btnPost.disabled = true;
					if (document.getElementById('btnCancel'))
						x.btnCancel.disabled = true;	
					if (document.getElementById('btnClear'))
						x.btnClear.disabled = true;
					if (document.getElementById('btnAddItem'))
						x.btnAddItem.disabled = true;
					if (document.getElementById('btnAddSerial'))
						x.btnAddSerial.disabled = true;		
					
						
					x.actionmode.value='post';
					x.action="Form_PreAssemble_Actions.php?mode=post&mainmode="+vMainMode+"&SPrdId="+PrdId;
					x.submit();
				}	
			}
		}	
	}
}
	
function DeletePrdDt(PrdDtId, ItemPackCode, ItemCode) {
	x = document.production;
	var PrdId  = x.txtPrdId.value;
	var vMainMode = x.mainaction.value;
	var vDelMsg = '';
	
	
	if (ItemPackCode != '')
	   vDelMsg = "Are you sure you want to Delete Pack Item "+ItemPackCode+" ?";
	else  
	   vDelMsg = "Are you sure you want to Delete Item "+ItemCode+" ?";
	   
	if (confirm ( vDelMsg ))
	{
		x.action="Form_PreAssemble_Actions.php?mode=deletedt&mainmode="+vMainMode+"&SPrdId="+PrdId+"&SPrdDtId="+PrdDtId;
		x.submit();
	}

}

function UpdatePrdDt(PrdDtId) {
    x = document.production;
	var vPrdId   = x.txtPrdId.value;
	var vPrdDate = x.txtDate.value;
	var vPrdCurr = x.txtCurr.value;
		
	var vMainMode = x.mainaction.value;
	
	win = window.open('Form_PreAssembleDetail.php?mainaction='+vMainMode+'&actionmode=&PPrdId='+vPrdId+'&PPrdDate='+vPrdDate+'&PPrdCurr='+vPrdCurr+'&PPrdDtId='+PrdDtId, 'Form_AssembleDetail', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=720,height=340,top=100');		
	win.creator = self;
	win.focus();		

}


function AddSerial() {
	x = document.production;
	var PrdId = x.txtPrdId.value;
	
	
	XPos = (screen.availHeight - 450) / 2;
	YPos = (screen.availWidth - 550) / 2;
    win = window.open('Form_PreAssembleSerialNo.php?SRPrdId='+PrdId, 'Form_AssembleSerialNo', 'titlebar=no,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,directories=no, width=550,height=450,left='+YPos+',top='+XPos);
	//win.creator = self;
	//win.focus();		
	
}



//All= All form; D= Detail Part
function ClearForm(Option){
    x = document.production;
	var vmode = x.actionmode.value;
	
	if (Option == 'All') {
		
		x.txtPrdId.value='';
		x.txtRef.value='';
		x.txtDate.value='';
		x.txtJvRef.value='';
		x.txtJvFolio.value='';
		x.txtPrdExpireDate.value='';
		x.txtPrdBatchNo.value='';
		x.txtPrdUnit.value='';
		x.txtPrdMixer.value='';
		x.txtPrdSize.value='';
		x.txtItemCodeFG.value='';
		x.txtItemDescFG.value='';
		x.txtWhCode.value='';
		x.txtWhDesc.value='';
		x.txtRemark.value='';
		x.txtPrdBatchNo.value='';
		x.txtCurr.value='';
		x.txtProjectCode.value='';
		x.txtCostCentCode.value='';
		if(x.txtClientCode){
			x.txtClientCode.value='';
		}
		x.txtPrdQty.value='';
		if(x.txtPrdQtyIdeal)
			x.txtPrdQtyIdeal.value='';
		
		if (x.txtPrdTotal)
			x.txtPrdTotal.value='';
		if (x.txtPrdTotalIdeal)
			x.txtPrdTotalIdeal.value='';
		if(x.txtPrdCostPerUnit)
			x.txtPrdCostPerUnit.value='';
		if(x.txtPrdCostPerUnitIdeal)
			x.txtPrdCostPerUnitIdeal.value='';
		
		x.txtqtyill.value='';
		x.message.value='';
		x.txtPrdCountDetail.value='';
		
		x.actionmode.value = '';
		x.mainaction.value = '';
	} 
}

function AddItems(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate) {
	x = document.production;
	var result = CheckFields(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate);

	var vPrdId         = x.txtPrdId.value;
	var vPrdDate       = x.txtDate.value;
	var vPrdExpireDate = x.txtPrdExpireDate.value;
	var vPrdBatchNo    = x.txtPrdBatchNo.value;
	
	var vPrdUnit    = x.txtPrdUnit.value;
	var vPrdMixer    = x.txtPrdMixer.value;
	var vPrdSize    = x.txtPrdSize.value;
	
	var vPrdItemCode   = x.txtItemCodeFG.value;
	var vPrdWhCode     = x.txtWhCode.value;
	var vPrdRemark     = x.txtRemark.value;
	var vPrdCurr       = x.txtCurr.value;
	var vPrdProjectCode  = x.txtProjectCode.value;
	var vPrdCostCentCode = x.txtCostCentCode.value;
	var vPrdQty          = x.txtPrdQty.value;
	var vPrdQtyIdeal          = x.txtPrdQtyIdeal.value;
		
	
	var vMainMode = x.mainaction.value;
	
	vPrdRemark = vPrdRemark.replace('&','%26');
	vPrdWhCode = vPrdWhCode.replace('&','%26');
	
	vPrdRemark = vPrdRemark.replace('#','%23');
	vPrdWhCode = vPrdWhCode.replace('#','%23');
	
	if (result == 1) {
		win = window.open('Form_PreAssembleDetail.php?mainaction='+vMainMode+'&actionmode=&PPrdId='+vPrdId+'&PPrdDate='+vPrdDate+'&PPrdExpireDate='+vPrdExpireDate+
		                  '&PPrdItemCode='+vPrdItemCode+'&PPrdWhCode='+vPrdWhCode+'&PPrdCurr='+vPrdCurr+'&PPrdProjectCode='+vPrdProjectCode+'&PPrdCostCentCode='+vPrdCostCentCode+
						  '&PPrdQty='+vPrdQty+'&PPrdQtyIdeal='+vPrdQtyIdeal+'&PPrdBatchNo='+vPrdBatchNo+'&PPrdRemark='+vPrdRemark	, 
						  'Form_AssembleDetail', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=720,height=340,top=100');		
		win.creator = self;
		win.focus();		
	}	
	
    
}

function LoadMaterials(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate,Module_Sorderproduction) {
	x = document.production;
	var result = CheckFields(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost, ItemExpire, ModuleExpireDate);

	var vPrdId    = x.txtPrdId.value;
	var vMainMode = x.mainaction.value;
	var chooseClient=1;
	if(Module_Sorderproduction==1){
		if(document.getElementById("txtClientCode").value==''){
		if(confirm('Choose a Client!')){
			chooseClient=0;
			return 0;
		}
		else chooseClient=1;
		}
	}
	if (result == 1 && chooseClient==1) {
		x.action="Form_PreAssemble_Actions.php?mode=loadmaterials&mainmode="+vMainMode+"&SPrdId="+vPrdId;
		x.submit();		
	}	
	
    
}


function DeleteRecordGrid(filepath,formname,transid,PrdDtId,index,dftorderby,orderby,morelines,startrange,perpage,direction) {
	x = document.production;
	var vPrdId    = x.txtPrdId.value;
	var vPrdCurr  = x.txtCurr.value;
	var vPrdDate  = x.txtDate.value;
	var vMainMode = x.mainaction.value;
	var vDelMsg = 'Are you sure you want to delete this record?';

	
	if (confirm ( vDelMsg ))
	{
		var xmlhttp = getXMLHttpRequest();	
		$("#Loader").addClass("ajaxsaving");
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = $.parseJSON(xmlhttp.responseText);
					
				var msg = data[0].message;
					
				if (msg != "") {
					$("#Loader").removeClass("ajaxsaving");
					alert(decodeURIComponent(msg));
				} else { 
					$("#Loader").removeClass("ajaxsaving");
					ReloadGrid(filepath,formname,transid,'datagrid',index,dftorderby,'DEL',orderby,morelines,startrange,perpage,0,direction);			
					ReloadGridDt(filepath,'PreASS-TOT','TOTALS','totals',transid,-1);
				}
			}
		}

		xmlhttp.open("GET","Form_PreAssemble_Actions.php?mode=deletedt&mainmode="+vMainMode+"&SPrdId="+vPrdId+"&SPrdDtId="+PrdDtId+"&txtCurr="+vPrdCurr+"&txtDate="+vPrdDate);
		xmlhttp.send();
	}	

}


function SubmitRecordGrid(PIndex,filepath,formname,transid,dftorderby,orderby,morelines,startrange,perpage,direction,ModuleRestProj,ModuleRestCost,ModuleProj,ModuleCost,ModuleExpireDate,ModuleSerialNo) {
	
	var x = document.production;
	
	var ItemExpire     = '';
	if(document.getElementById('itemexpire['+PIndex+']'))
		ItemExpire = document.getElementById('itemexpire['+PIndex+']').value;
	var ItemWithSerial = '';
	if(document.getElementById('itemwithserial['+PIndex+']'))
		ItemWithSerial = document.getElementById('itemwithserial['+PIndex+']').value;
	
	
	var result = 1;
	result = CheckFields(ModuleRestProj, ModuleRestCost, ModuleProj, ModuleCost);
	
	if (result == 1) {
		result = CheckFieldsDt(PIndex,ItemExpire,ModuleExpireDate,ItemWithSerial,ModuleSerialNo);
	}
	
	var PrdDtId   = document.getElementById('prddtid['+PIndex+']').value;
	if (PrdDtId == '') vMode = 'submitrecord-ajax';	
	else vMode = 'editdt-ajax';	
	
	if(document.getElementById('prddtinitialquantity['+PIndex+']'))
	var SaDtiniQty = document.getElementById('prddtinitialquantity['+PIndex+']').value;
	else SaDtiniQty = 0;
	
	if(document.getElementById('prddtaddquantity['+PIndex+']'))
	var SaDtaddQty = document.getElementById('prddtaddquantity['+PIndex+']').value;
	else SaDtaddQty = 0;
	
	if(document.getElementById('item_stage['+PIndex+']'))
	var SaDtitem_stage = document.getElementById('item_stage['+PIndex+']').value;
	else SaDtitem_stage = 0;
	
	var vClientCode = '';
	if(x.txtClientCode)
		vClientCode = x.txtClientCode.value;	
	
	var txtItemCodeFG = '';
	if(x.txtItemCodeFG) txtItemCodeFG = x.txtItemCodeFG.value;
	var txtPrdExpireDate = '';
	if(x.txtPrdExpireDate) txtPrdExpireDate = x.txtPrdExpireDate.value;
	var txtPrdBatchNo = '';
	if(x.txtPrdBatchNo) txtPrdBatchNo = x.txtPrdBatchNo.value;
	
	var txtPrdUnit = '';
	if(x.txtPrdUnit) txtPrdUnit = x.txtPrdUnit.value;

	var txtPrdMixer = '';
	if(x.txtPrdMixer) txtPrdMixer = x.txtPrdMixer.value;

	var txtPrdSize = '';
	if(x.txtPrdSize) txtPrdSize = x.txtPrdSize.value;

	var prddtidealqty = '';
	if(document.getElementById('prddtidealqty['+PIndex+']'))
		prddtidealqty = document.getElementById('prddtidealqty['+PIndex+']').value;

	var prddtwaste = '';
	if(document.getElementById('prddtwaste['+PIndex+']'))
		prddtwaste = document.getElementById('prddtwaste['+PIndex+']').value;

	var costprice = '';	
	if(document.getElementById('costprice['+PIndex+']')) 
		costprice = document.getElementById('costprice['+PIndex+']').value;
			
	var batchno = '';
	if(document.getElementById('batchno['+PIndex+']'))
		batchno	= document.getElementById('batchno['+PIndex+']').value;
				
	var expiredate = '';
	if(document.getElementById('expiredate['+PIndex+']'))
		expiredate	= document.getElementById('expiredate['+PIndex+']').value;
			
	var projectcodedt = '';
	if(document.getElementById('projectcodedt['+PIndex+']'))
		projectcodedt = document.getElementById('projectcodedt['+PIndex+']').value;

	var productiondt_heatno = '';
	if(document.getElementById('productiondt_heatno['+PIndex+']'))
		productiondt_heatno = document.getElementById('productiondt_heatno['+PIndex+']').value;
		
	var costcentcodedt = '';
	if(document.getElementById('costcentcodedt['+PIndex+']'))
		costcentcodedt = document.getElementById('costcentcodedt['+PIndex+']').value;

					
	var  the_data = 'mode='+vMode+
					'&mainmode='+x.mainaction.value+
					'&SPrdId='+x.txtPrdId.value+
					'&PPrdId='+x.txtPrdId.value+
					'&PPrdDate='+x.txtDate.value+
					'&PPrdCurr='+x.txtCurr.value+
					'&PPrdItemCode='+x.txtItemCodeFG.value+
					'&PPrdWhCode='+x.txtWhCode.value+
					'&PPrdQty='+x.txtPrdQty.value+
					'&PPrdExpireDate='+x.txtPrdExpireDate.value+
					'&PPrdBatchNo='+x.txtPrdBatchNo.value+
					'&PPrdUnit='+x.txtPrdUnit.value+
					'&PPrdMixer='+x.txtPrdMixer.value+
					'&PPrdSize='+x.txtPrdSize.value+
					'&PPrdProjectCode='+x.txtProjectCode.value+
					'&PPrdCostCentCode='+x.txtCostCentCode.value+
					'&PPrdRemark='+encodeURIComponent(x.txtRemark.value)+
					'&SPrdDtId='+document.getElementById('prddtid['+PIndex+']').value+
					'&lstWarehouse='+document.getElementById('warehousecode['+PIndex+']').value+
					'&txtItemCode='+document.getElementById('itemcode['+PIndex+']').value+
					'&txtPrdDtRemark='+document.getElementById('remarkdt['+PIndex+']').value+
					'&txtPrdDtQty='+document.getElementById('prddtquantity['+PIndex+']').value+
					'&txtPrdDtQtyIdeal='+prddtidealqty+
					'&txtPrdDtWaste='+prddtwaste+
					'&txtPrdDtCostPrice='+costprice+
					'&txtPrdDtBatchNo='+batchno+
					'&txtPrdDtExpireDate='+expiredate+
					'&txtProjectCodeDt='+projectcodedt+
					'&productiondt_heatno='+productiondt_heatno+
					'&txtCostCentCodeDt='+costcentcodedt+
					'&SaDtiniQty='+SaDtiniQty+'&SaDtaddQty='+SaDtaddQty+'&clientcode='+vClientCode+
					'&SaDtitem_stage='+SaDtitem_stage;
		

	if (result == 1) {
		var xmlhttp = getXMLHttpRequest();	
		var getdate = new Date();  //Used to prevent caching during ajax call
  
		$("#Loader").addClass("ajaxsaving");
		//$('#datagrid :input').attr('disabled', true);
		
		var SubBtnID = 'imgSubmit['+PIndex+']';
		if (document.getElementById(SubBtnID))	document.getElementById(SubBtnID).disabled = true;
		
		if(xmlhttp) {
			//xmlhttp.open("POST", "Form_PreAssemble_Actions.php", true);
		
			xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					var data = $.parseJSON(xmlhttp.responseText);
					
					var msg = data[0].message;
					
					if(document.getElementById("txttotalQty")){
					totQty = data[0].totalQty; 
					x.txttotalQty.value = totQty;
					}
					if (msg != "") {
						$("#Loader").removeClass("ajaxsaving");
						if (document.getElementById(SubBtnID))	document.getElementById(SubBtnID).disabled = false;
						alert(decodeURIComponent(msg));
					} else {	
						if (transid == ""){ 
							transid = data[0].transid;
							x.txtPrdId.value = transid;
								
							x.submit();
						} 
							
						var index = parseInt(PIndex) + 1;
						var nextcell = 'itemcode['+index+']';
						
						createCookie('preass-nextcell',nextcell);
						
						ReloadGrid(filepath,formname,transid,'datagrid',PIndex,dftorderby,'ADD',orderby,morelines,startrange,perpage,0,direction,function() {
							MoveNextRecord(nextcell);
							ReloadGridDt(filepath,'PreASS-TOT','TOTALS','totals',transid,-1);
							document.getElementById('ModRec_G1').value = "";
						});
						
						
					} 
				}
			}
			
			// adds  a header to tell the PHP script to recognize the data as is sent via POST
			//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			//xmlhttp.send(the_data);
			
			xmlhttp.open("GET","Form_PreAssemble_Actions.php?"+the_data);
			xmlhttp.send();
			
		}
	}
}


function SearchForRef(){
    //ClearForm('All');
    x = document.production;
    win = window.open('Form_PreAssembleSearch.php', 'Form_PreAssembleSearch', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=500,top=100,left=10');		
	win.creator = self;
	win.focus();		
			
}

function SearchForPending(){
    //ClearForm('All');
    x = document.production;
    win = window.open('Form_PreAssembleSearchPending.php', 'Form_PreAssembleSearchPending', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=960,height=580,top=100,left=10');		
	win.creator = self;
	win.focus();		
			
}


function CalcQuantities(tagID, DisableIdeal) {

	x = document.production;
	
	var vPrdQty      = x.txtPrdQty.value;
	var vPrdQtyIdeal = x.txtPrdQtyIdeal.value;
	var vPrdWaste    = x.txtPrdWasteCalc.value;
	
	if (vPrdQty == '')      vPrdQty = 0;
	if (vPrdQtyIdeal == '') vPrdQtyIdeal = 0;
	if (vPrdWaste == '')    vPrdWaste = 0;
	
	
	
	if (vPrdQty != 0 && (vPrdQty*1 != vPrdQty/1 || vPrdQty < 0)) {
		alert('Quantity should be a NUMERIC and POSITIVE value ');
		x.txtPrdQty.focus();
		return false;
	}
	else 
	if (vPrdQtyIdeal != 0 && (vPrdQtyIdeal*1 != vPrdQtyIdeal/1 || vPrdQtyIdeal < 0)) {
		alert('Quantity Ideal should be a NUMERIC and POSITIVE value ');
		x.txtPrdQtyIdeal.focus();
		return false;
	}
	else if (DisableIdeal == 0)
	{
		vPrdQty      = parseFloat(vPrdQty);
		vPrdQtyIdeal = parseFloat(vPrdQtyIdeal);
	
		vPrdWaste = vPrdQtyIdeal - vPrdQty;
		x.txtPrdWasteCalc.value = vPrdWaste;
	
	}


}

function UnSelectRecord() {
	var CheckName, vUnSelectedRows;
	var x, y;
	
	x = document.production;
	
	vUnSelectedRows = '';
	for (var i=1; i <= x.ChkCounter.value; i++)
	{
		CheckName='R'+i;
		y = document.getElementById(CheckName);
		
		if (y.checked==false)
			vUnSelectedRows = vUnSelectedRows + ',' + y.value;
		
	}
	
	vUnSelectedRows = vUnSelectedRows.substr(1);
	x.UnSelectedRows.value = vUnSelectedRows;
}

function CheckIfSelected() {
	var CheckName, vSelectedRows;
	var x, y;
	
	x = document.production;
	
	vSelectedRows = 0;
	for (var i=1; i <= x.ChkCounter.value; i++)
	{
		CheckName='R'+i;
		y = document.getElementById(CheckName);
		
		if (y.checked)
			vSelectedRows = vSelectedRows + 1;
		
	}
	
	return vSelectedRows;
	
}

function PrintProduction(PrdId, PrdRef) {
	var str = '';
	
	
	if (confirm ( "Do you want to Print current PreProduction with reference "+PrdRef+" ?"))
	{
		str = 'Form_PreAssemblePreview2.php?ProductionId='+PrdId;
		window.open(str);
	}	
 
	

	
}

function CalculateQty() {
	var x = document.production;
	
	var PrdId  = x.txtPrdId.value;
	var PrdQty = x.txtPrdQty.value;
	if(x.txtPrdQtyIdeal)
		var PrdQtyIdeal = x.txtPrdQtyIdeal.value;
	else
		var PrdQtyIdeal ='';
	
	if (PrdId != '') {
		x.action="Form_PreAssemble_Actions.php?mode=changeprodqty&SPrdId="+PrdId+"&PPrdQty="+PrdQty+"&PPrdQtyIdeal="+PrdQtyIdeal;
		x.submit();
	} 
}
function CalculateQtyIdeal() {
	x = document.production;
	
	var PrdId  = x.txtPrdId.value;
	
	var PrdQtyIdeal = x.txtPrdQtyIdeal.value;
	var PrdQty = x.txtPrdQty.value;
	
	if (PrdId != '') {
		x.action="Form_PreAssemble_Actions.php?mode=changeprodqtyideal&SPrdId="+PrdId+"&PPrdQtyIdeal="+PrdQtyIdeal+"&PPrdQty="+PrdQty;
		x.submit();
	} 
}

function showlistItemsH(str){
	var vDate, vItemCode;
	x = document.production;
	
	XPos = (screen.availHeight - 400) / 2;
	YPos = (screen.availWidth - 850) / 2;
  
	vDate 	   = x.txtDate.value;
	vItemCode  = encodeURIComponent(document.getElementById('txtItemCodeFG').value);
  
	window.open('List_SorItems.php?frm=3&PItem_Code='+vItemCode+'&ItemType=Assemble&TransDate='+vDate,'List_SorItems','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}




function showlistItemsDescH(str){
	var vDate, vItemDesc;
	x = document.production;
	
	XPos = (screen.availHeight - 400) / 2;
	YPos = (screen.availWidth - 850) / 2;
  
	vDate 	   = x.txtDate.value;
	vItemDesc  = encodeURIComponent(document.getElementById('txtItemDescFG').value);
	
	window.open('List_SorItems.php?frm=3&PItem_Description='+vItemDesc+'&ItemType=Assemble&TransDate='+vDate,'List_SorItems','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}


function showlistWarehouseH(str) {
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	var vWarehouseCode = encodeURIComponent(document.getElementById('txtWhCode').value);
	
	window.open('List_SorWarehouse.php?frm=1&item='+str+'&PWarehouse_Code='+vWarehouseCode,'List_SorWarehouse','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}

function showlistWarehouseDescH(str) {
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	var vWarehouseDesc = encodeURIComponent(document.getElementById('txtWhDesc').value);
	
	window.open('List_SorWarehouse.php?frm=1&item='+str+'&PWarehouse_Name='+vWarehouseDesc,'List_SorWarehouse','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}

function showlistProject(str){

	vProjectCode = encodeURIComponent(document.getElementById('txtProjectCode').value);
	window.open('List_SorProject.php?frm=7&PProject_Code='+vProjectCode,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(str){

	vCostcentCode = encodeURIComponent(document.getElementById('txtCostCentCode').value);
	window.open('List_SorCostcenter.php?frm=7&PCostcent_Code='+vCostcentCode,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function showlistProjectDt(str,PIndex){
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	vProjectCode = encodeURIComponent(document.getElementById('projectcodedt['+PIndex+']').value);
	
	window.open('List_SorProject.php?frm=25&index='+PIndex+'&item='+str+'&PProject_Code='+vProjectCode,'List_SorProject','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,left='+YPos+',top='+XPos);
}

function showlistCostcenterDt(str,PIndex){
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	vCostcentCode = encodeURIComponent(document.getElementById('costcentcodedt['+PIndex+']').value);
	
	window.open('List_SorCostcenter.php?frm=25&index='+PIndex+'&item='+str+'&PCostcent_Code='+vCostcentCode,'List_SorCostcenter','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,left='+YPos+',top='+XPos);
}

function showlistItems(str,PIndex){
	var vDate, vItemCode;
	x = document.production;
	
	XPos = (screen.availHeight - 400) / 2;
	YPos = (screen.availWidth - 850) / 2;
  
	vDate 	   = x.txtDate.value;
	vItemCode  = encodeURIComponent(document.getElementById('itemcode['+PIndex+']').value);
  
	window.open('List_SorItems.php?frm=7&index='+PIndex+'&PItem_Code='+vItemCode+'&ItemType=All&TransDate='+vDate,'List_SorItems','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}


function showlistItemsOpt(str, index){
	var vItem;
	
	XPos = (screen.availHeight - 400) / 2;
	YPos = (screen.availWidth - 650) / 2;
	vItem = encodeURIComponent(document.getElementById('itemcode['+index+']').value);
	
	window.open('List_itemOption.php?frm=1&vIndex='+index+'&itemCode='+vItem,'listItemsOpt','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}

function getQtyExists(index, Prdt_id){
	vItemCode 		= document.getElementById('itemcode['+index+']').value;
	vWarehouseCode 	= document.getElementById('warehousecode['+index+']').value;
	vDate 		  	= document.production.txtDate.value;
	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var data = xmlhttp.responseText.split("[BRK]");
			document.getElementById('prddtqtyexists['+index+']').value = data[1];
		}
	  }
	 
	xmlhttp.open("GET","Form_PreAssemble_Actions.php?mode=CheckQtyExists&vItemCode="+vItemCode+"&vWarehouseCode="+vWarehouseCode+"&vDate="+vDate+"&Prdt_id="+Prdt_id,true);
	xmlhttp.send(); 
}
function showlistItemsDesc(str,PIndex){
	var vDate, vItemCode;
	x = document.production;
	
	XPos = (screen.availHeight - 400) / 2;
	YPos = (screen.availWidth - 850) / 2;
  
	vDate 	   = x.txtDate.value;
	vItemDesc  = encodeURIComponent(document.getElementById('itemdescription['+PIndex+']').value);
	
	window.open('List_SorItems.php?frm=7&index='+PIndex+'&PItem_Description='+vItemDesc+'&ItemType=All&TransDate='+vDate,'List_SorItems','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}

function showlistWarehouse(PIndex) {
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	var vWarehouseCode = encodeURIComponent(document.getElementById('warehousecode['+PIndex+']').value);
	
	window.open('List_SorWarehouse.php?frm=2&index='+PIndex+'&PWarehouse_Code='+vWarehouseCode,'List_SorWarehouse','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}

function showlistBatchNo(str,PIndex) {
	x = document.production;
	
	XPos = (screen.availHeight - 400) / 2;
	YPos = (screen.availWidth - 580) / 2;
	
	vItemCode = encodeURIComponent(document.getElementById('itemcode['+PIndex+']').value);
	vWarehouseCode = encodeURIComponent(document.getElementById('warehousecode['+PIndex+']').value);
	
	window.open('List_BatchNo.php?frm=10&index='+PIndex+'&PItem_Code='+vItemCode+'&PWarehouseCode='+vWarehouseCode,'List_BatchNo','width=580,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,left='+YPos+',top='+XPos);
}

function CalcPrdDtTotal(dec,PIndex) {
	x = document.production;
	var vmode = x.actionmode.value;
	var Qty      = document.getElementById('prddtquantity['+PIndex+']').value;
	var Price    = 0;
	if(document.getElementById('costprice['+PIndex+']'))
		Price = document.getElementById('costprice['+PIndex+']').value;
	
	if (Qty == '') 		Qty = 0;
	if (Price == '') 	Price = 0;
	
	Qty      = parseFloat(Qty);
	Price    = parseFloat(Price);
	
	if (Qty > 0 && Price >= 0 && document.getElementById('prddttotal['+PIndex+']')) {
	   document.getElementById('prddttotal['+PIndex+']').value = roundNumber((Qty*Price),dec);
	}  

}

function ChangeCurrency(){
	x = document.production;
	
	var PrdId  = x.txtPrdId.value;
	
	if (PrdId != '') {
		x.action="Form_PreAssemble_Actions.php?mode=changecurrency&SPrdId="+PrdId;
		x.submit();
	} else {
		x.submit();
	}
}

function PrintPreview(PrdId)
{
	var str = '';
	str = 'Form_PreAssemblePreview.php?ProductionId='+PrdId;
	window.open(str);

}

function PrintPreview2(PrdId)
{
	var str = '';
	str = 'Form_PreAssemblepending.php?ProductionId='+PrdId;
	window.open(str);

}

function kdItem(choice,e)
{   
	 x = document.production;
	
	 if(choice==0)
		var itemcode = x.txtItemCodeFG.value;
	else
		var itemcode = x.txtItemDescFG.value;
		
    var intKey = (window.Event) ? e.which : e.keyCode;
	
		
    if (intKey == 13) { //enter key
	
	
		var xmlhttp = getXMLHttpRequest();	
		
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = xmlhttp.responseText.split("[BRK]");
				  
					if(data[1]!=''){
							 
							if(choice==0)
								x.txtItemDescFG.value = data[2];
							else
								x.txtItemCodeFG.value = data[1];
								
						
						if(x.txtItemDescFG.value!='')
							{
								x.submit();	
							} 
							
						
						
					}else {
					 
					document.getElementById('lstItemCodeH').click();
				}
			}
		}
	    
		xmlhttp.open("GET","Form_PreAssemble_Actions.php?mode=getitemname&itemcode="+itemcode+"&choice="+choice);
		xmlhttp.send();
	
      
			
			
    }
}

function LoadSorder() {
	var x = document.production;
	
	var vPrdId    = x.txtPrdId.value;
	var vMainMode = x.mainaction.value;
	
	win = window.open('Form_PreProduction_LoadSorder.php', 'Form_PreProduction_LoadSorder', 
					  'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=960,height=600,top=100,left=10');
	win.creator = self;
	win.focus();		
	
}

function LoadSorderdt() {
	win = window.open('Form_PreProduction_LoadSorderdt.php', 'Form_PreProduction_LoadSorderdt', 
					  'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=960,height=600,top=100,left=10');
	win.creator = self;
	win.focus();		
	
}

function AddInfo(txtPrdId){
	var x = document.production;
	
	var vPrdId    = x.txtPrdId.value;
	var vMainMode = x.mainaction.value;
	var newbatchno = document.getElementById("txtPrdBatchNo").value;
	var newRemark = document.getElementById("txtRemark").value;
	win = window.open('Form_Production_info.php?txtPrdId='+txtPrdId+'&newbatchno='+newbatchno+'&newRemark='+newRemark, 'Form_Production_info', 
					  'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=600,height=600,top=100,left=10');
	
	
	win.creator = self;
	win.focus();	
}

function adjustQty(){
	var x = document.production;
	if(document.getElementById("txtPrdQtyIdeal"))
	x.txtPrdQtyIdeal.value=x.txtPrdQty.value;
	
}

function CalcAddition(dec,PIndex){
	var x = document.production;
	
	var addQty      = document.getElementById('prddtaddquantity['+PIndex+']').value;
	var iniQty    = document.getElementById('prddtinitialquantity['+PIndex+']').value;
	
	
	if (addQty == '') 		addQty = 0;
	if (iniQty == '') 	iniQty = 0;
	
	
	addQty      = parseFloat(addQty);
	iniQty    = parseFloat(iniQty);
	
	

	   document.getElementById('prddtquantity['+PIndex+']').value = (addQty+iniQty);
	 
}

function changePrice(){
	var Qtyill = document.getElementById("txtqtyill").value;
	var totCost = document.getElementById("totalactual").value;
	
	if(Qtyill > 0){
	var newVal = roundNumber(parseFloat(totCost/Qtyill),2);
	document.getElementById("unitcostactual").value=newVal;
	}
}

function showlistClient(str){
	var vdate, vcode;
	vdate=document.production.txtDate.value;
	vcode=encodeURIComponent(document.production.txtClientCode.value);
	vname=encodeURIComponent(document.production.txtclientname.value);
  
	window.open('List_SorClient.php?frm=8&item='+str+'&PLedger_name='+vname+'&PLedger_number='+vcode+'&TransDate='+vdate,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function OpenMaterialProj(){
	$('#matbox').slideToggle();
}

function closeMatWin(){
	document.getElementById('matbox').style.display = "none";
}

function setTrans(transtype){
	if(transtype == 'TR'){
		if(document.getElementById('chk_tr').checked){	
			document.getElementById('chk_tr').value=1;
			document.getElementById('chk_tr').checked=true;
		}
		else {
			document.getElementById('chk_tr').value=0;
			document.getElementById('chk_tr').checked=false;
		}
	}
}

function showMatProject(){
	vProjectCode = encodeURIComponent(document.getElementById('mat_project').value);
	window.open('List_SorProject.php?frm=39&PProject_Code='+vProjectCode,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showMatWarehouse(){
	XPos = (screen.availHeight - 550) / 2;
	YPos = (screen.availWidth - 850) / 2;
	
	var vWarehouseCode = encodeURIComponent(document.getElementById('mat_warehouse').value);
	
	window.open('List_SorWarehouse.php?frm=12&item=1&PWarehouse_Code='+vWarehouseCode,'List_SorWarehouse','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);

}
function LoadMaterialProj(ModuleProj, ModuleRestProj, ModuleCost, ModuleRestCost){
	var x = document.production;
	var chk_tr = 0;
	if(x.chk_tr.checked)
		chk_tr = 1;
	
	if (x.txtDate.value=='' )
	{
		alert("Date Column is mandatory!");
		x.txtDate.focus();
		return 0;
	}
	else if (x.txtItemCodeFG.value=='' )
	{
		alert("Item Column is mandatory!");
		x.txtItemCodeFG.focus();
		return 0;
	}
	else if (x.txtWhCode.value=='' )
	{
		alert("Warehouse Column is mandatory!");
		x.txtWhCode.focus();
		return 0;
	}
	else if (x.txtCurr.value=='' )
	{
		alert("Currency Column is mandatory!");
		x.txtCurr.focus();
		return 0;
	}
	else if (x.txtProjectCode.value =='' && ModuleRestProj==1 && ModuleProj==1)
	{
		alert("Project is mandatory!");
		x.txtProjectCode.focus();
		return 0;
	}
	else if (x.txtCostCentCode.value =='' && ModuleRestCost==1 && ModuleCost==1 )
	{
		alert("CostCenter is mandatory!");
		x.txtCostCentCode.focus();
		return 0;
	} else if(x.mat_project.value == '' && ModuleRestProj==1 && ModuleProj==1){
		alert("Project in Load Material is mandatory!");
		x.mat_project.focus();
		return 0;
	} else if( chk_tr == 0){
		alert("At Least 1 Transaction should be included!");
		return 0;
	} else if( x.mat_warehouse.value == ''){
		alert("Warehouse is Mandatory!");
		x.mat_warehouse.focus();
		return 0;
	} 
	else {
		var vPrdId    = x.txtPrdId.value;
		var vMainMode = x.mainaction.value;
		x.action="Form_PreAssemble_Actions.php?mode=loadmaterialsproj&mainmode="+vMainMode+"&SPrdId="+vPrdId;
		x.submit();	
	}
}

function loadFromSorder(sorderid){

	document.production.action='Form_PreAssemble_Actions.php?mode=loadPendingSorder&SSorId='+sorderid;
	document.production.submit();
}
</script>

<title>Pre-Assemble</title>


<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>
<style>
#matbox {
	width: 600px;
    height: 200px;
    z-index: 99999999;
    position: absolute;
    background-color: rgb(220, 213, 213);
    right: 50%;
    left: 30%;
    top: 30%;
    display: none;
	border: 2px solid darkgray;
}
</style>
</head>
<body>
<form name='production' method='post'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>
<input type='hidden' name='UnSelectedRows' value='$UnSelectedRows'>

HERE;


	
//Get Default Project
$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['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 ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

	
if ((empty($txtProjectCode)) && $Module_Project == 1)
	$txtProjectCode = $defaultproject;
if ((empty($txtCostCentCode)) && $Module_CostCenter == 1)
	$txtCostCentCode = $defaultcostcent;	
if (empty($txtCurr)) {
	if ($SetupDftCurrency != '')
		$txtCurr = $SetupDftCurrency;
	else
		$txtCurr = $base1;
}

if (empty($txtWhCode)) {
	if (!empty($SetupDftWareHouse))
		$txtWhCode = $SetupDftWareHouse;
	else	
		$txtWhCode = $defaultwarehouse;
		
	$querywarehouse="select warehouse_name as SetupDftWareHouseName from warehouse where warehouse_code = '".$txtWhCode."'";
	$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultwarehouse)){
		extract($rows);
	}	
	
	$txtWhDesc  = $SetupDftWareHouseName;
}
			
if (empty($txtDate)) $txtDate = displayToday();
//if (empty($txtPrdExpireDate))	
	//$txtPrdExpireDate = displayToday();
	
$i_expire   = 0;
if ($txtPrdId == '' && $txtItemCodeFG != '') {
	$sub_query = " Select ifnull(Item_QtyToBeProduced, 1) As Item_QtyToBeProduced, ifNull(item_withserialno,0) As ItemWithSerialNo, ifNull(Item_Expire,1), Project_Code, CostCent_Code From Items ".
	             " Where Item_Code = '".$txtItemCodeFG."' ";
	$sub_result = selectData($connection, $sub_query);	
	
	$sub_query1  = " Select Warehouse_Name From Warehouse ".
	               " Where Warehouse_Code = '".$txtWhCode."' ";
	$sub_result1 = selectData($connection, $sub_query1);	
	
	$txtWhDesc    = $sub_result1[0];
	$txtPrdQty    = $sub_result[0];
	$txtItemWithSerialNo = $sub_result[1];
	$i_expire	  = $sub_result[2];
	//$txtPrdQtyIdeal  = $sub_result[0];
	//$txtPrdWasteCalc = 0;
	
	if($Module_ReferenceByProject != 1)    $txtProjectCode  = $sub_result[3];
	if($Module_ReferenceByCostcenter != 1) $txtCostCentCode = $sub_result[4];
}
	
		
if ($actionmode != "cancel" && $txtPrdId != '') {
	// Read From Temp Tables For Selected Record				 
	$query = " Select Production_ID, Production_Reference, Date_Format(Production_Date,'%d/%m/%Y') As Production_Date, ".
	         " Date_Format(Production_ExpireDate,'%d/%m/%Y') As ExpireDate, Production.Currency_Code, ".
			 " Production.Item_code, Item_Description, Production.Warehouse_Code, Warehouse_Name, Round(Production_Quantity, ".$DecimalBase.") As Production_Quantity,  ".
			 " Round(Production_IdealQty, ".$DecimalBase.") As Production_IdealQty,  ifNull(Production_IdealQty,0) - ifNull(Production_Quantity,0) As ProductionWasteCalc, ".
			 " Production_Waste, Production_Remark, Production_Type, Production.Project_Code, Production.Costcenter_code,  ".
			 " Round(Production_Total, ".$DecimalBase.") As ProductionTotal, ".
			 " Round(Production_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal, ".
			 " if('$Production_extraInfo'=1,Round(Production_Total/if(Quantity_Ill is null or Quantity_Ill=0,1,Quantity_Ill), ".$DecimalBase."),Round(Production_Total/ifNull(Production_Quantity,1), ".$DecimalBase.")) As CostPricePerUnit, ".
			 " Round(Production_IdealTotal/ifNull(Production_IdealQty,1), ".$DecimalBase.") As CostPricePerUnitIdeal, ".
			 " null,null, ifNull(item_withserialno,0) As ItemWithSerialNo, ".
			 " Production.Production_BatchNo, ledger_name, Round(Quantity_Ill, ".$DecimalBase.") As Quantity_Ill, Client_Code,Production_unit,Production_mixer,Production_size ".
			 " From Temp_PreProduction As Production ".
			 " Left Join Items on Production.Item_code = Items.Item_code ".
			 " Left Join Warehouse on Production.Warehouse_Code = Warehouse.Warehouse_Code ".
			 " left join clients on clients.ledger_number = Client_Code ".
	         " Where Production_ID = ".$txtPrdId;
			 //" And Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
			 
			 
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtPrdId      = $rows[0];
	$txtRef        = $rows[1];
	$txtDate       = $rows[2];
	$txtCurr       = $rows[4];
	if($txtItemCodeFG == '')
	$txtItemCodeFG = $rows[5];
	if($txtItemDescFG == '')
	$txtItemDescFG = $rows[6];
	$txtWhCode     = $rows[7];
	$txtWhDesc     = $rows[8];
	$txtPrdQty     = $rows[9];
	$txtPrdQtyIdeal  = $rows[10];
	//$txtPrdWasteCalc = $rows[11];
	//$txtPrdWaste     = $rows[12];
	$txtRemark       = $rows[13];
	$txtProjectCode  = $rows[15];
	$txtCostCentCode = $rows[16];
	
	$txtPrdExpireDate = $rows[3];
	$txtPrdBatchNo    = $rows[24];
	
	$txtPrdUnit    = $rows[28];
	$txtPrdMixer    = $rows[29];
	$txtPrdSize    = $rows[30];
	
	$txtPrdTotal            = $rows[17];
	$txtPrdTotalIdeal       = $rows[18];
	$txtPrdCostPerUnit      = $rows[19];
	$txtPrdCostPerUnitIdeal = $rows[20];
	
	$txtJvFolio	    = $rows[21];
	$txtJvRef 	    = $rows[22];
	
	$txtItemWithSerialNo = $rows[23];
	
	if($txtClientCode == ''){
	$txtClientCode = $rows[27];	
	$txtclientname = $rows[25];
	}
	$txtqtyill = $rows[26];
	
	$query = " Select Count(*) FROM Temp_ProductionDt ".
			 " WHERE Production_id = ".$txtPrdId;
	$result = selectData($connection, $query);
	
	$txtPrdCountDetail = $result[0];
	//echo "xxxxxxxxxxxxxxxxxttt".$txtPrdCountDetail;
	
	if($txtPrdCountDetail == '') $txtPrdCountDetail = 1;
	 $txtPrdCountDetail = 1;
}



//$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' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>PRE-ASSEMBLE</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";

//if ($FI_ProductionApp == 1)
//	echo"<input type='button' id='btnAddItem' name='btnAddItem' value='Add Item'  onclick='AddItems($Module_RestrictProjCost, $Module_RestrictCost, $Module_Project, $Module_CostCenter, $i_expire, $Module_ExpireDate)' $style title='Add Item'>";
				

echo" <button type='button' id='btnClear' name='btnClear' onclick='ClearPage();return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";
	
if ($txtPrdId != '')
	echo" <button type='button' id='btnCancel' name='btnCancel' onclick='CancelRecord();return false;' title='Cancel Changes'><span><span>Cancel</span></span></button>";
	
if ($txtPrdId != ''){
	echo" <button type='button' id='btnPost' name='btnPost' onclick='";
	if($Production_extraInfo == 1)
	echo "CheckExtraInfo($Module_RestrictProjCost, $Module_RestrictCost, $Module_Project, $Module_CostCenter, $i_expire, $Module_ExpireDate);return false;' ";
	else
	echo "PostChanges($Module_RestrictProjCost, $Module_RestrictCost, $Module_Project, $Module_CostCenter, $i_expire, $Module_ExpireDate);return false;' ";
	
	echo "title='Save Changes'><span><span>Save</span></span></button>";
}


if ($txtPrdId == '')
	echo" <button type='button' id='btnSearch' name='btnSearch' onclick='SearchForRef();return false;' title='Search By Reference'><span><span>Search</span></span></button>";

if ($txtPrdId == '')
	echo" <button type='button' id='btnPending' name='btnPending' onclick='SearchForPending();return false;' title='Pending Productions'><span><span>Pending</span></span></button>";


	echo" <button type='button' id='btnPrint' name='btnPrint' onclick='PrintPreview($txtPrdId);return false;' title='Print RM Request'><span><span>Print RM Request</span></span></button>";
    echo" <button type='button' id='btnPrint2' name='btnPrint2' onclick='PrintPreview2($txtPrdId);return false;' title='Print Pending Sales Order'><span><span>Print Pending Sales Order</span></span></button>";

//if($Module_Sorderproduction == 1 && $txtPrdId == "")
//echo" <button type='button' id='btnLoadSorder' name='btnLoadSorder' onclick='LoadSorder();return false;' title='Load Sales Order'><span><span>Load Sales Order</span></span></button>";
if($sorderproduction == 1 && $txtPrdId == "")
echo" <button type='button' id='btnLoadSorderdt' name='btnLoadSorderdt' onclick='LoadSorderdt();return false;' title='Load Pending Sales Order'><span><span>Load Pending Sales Order</span></span></button>";
		
echo"	</td></tr></table>";
echo"</td></tr>";				

		
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";	

$strt = $_SESSION['StartFinancial_Date'];
$end = $_SESSION['EndFinancial_Date'];
print <<<HERE

		<table border="0" style='table-layout:fixed'>
		  <col width=80>
		  <col width=70>
		  <col width=50>
		  <col width=110>
		  <col width=40>
		  <col width=65>
		  <col width=60>
		  <col width=85>
		  <col width=110>
		  <col width=130>
		  <col width=100>
		  <col width=170>

		<tr bgcolor="#F7F7F7" >
          <td valign="center" align='left' ><span class="style1">Reference&nbsp;<span class="style16">*</span></span></td>
		  <td><font face='Verdana' size='2'><b>&nbsp;$txtRef</b></font>
			<input type='hidden' name='txtPrdCountDetail' value='$txtPrdCountDetail' >
			<input type='hidden' name='txtItemWithSerialNo' value='$txtItemWithSerialNo' >
			<input type='hidden' name='txtPrdId' value='$txtPrdId' >
			<input type="hidden" name="txtRef" value="$txtRef" ></td>
		  <td align="left"><span class="style1">Date&nbsp;<span class="style16">*</span></span></td>
		  <td><input type='text' name='txtDate' value='$txtDate' maxlength='10'  onBlur=checkDateFormat(this,$strt,$end)  style='width:80px;' >
		                   <A HREF="#" onClick="displayCalendar(document.production.txtDate,'dd/mm/yyyy',this); return false;" NAME="anchor1" ID="anchor1"><img src='./img/cal.gif' border=0 align=center></A>
			  <input type="hidden" name="txtJvFolio" value="$txtJvFolio" >
			  <input type="hidden" name="txtJvRef" value="$txtJvRef" >
		  </td>
						   
HERE;

echo" <td align='left' colspan='4'>&nbsp;</td>";
		  
		  
echo"     <td align='left' ><span class='style1'>Currency&nbsp;<span class='style16'>*</span></span></td>";
echo"     <td ><span class='style1'><select name='txtCurr' id='txtCurr' style='width:80px;' onchange='ChangeCurrency();'>";
		  echo"<option></option>";
		  while($curf=mysql_fetch_row($resultcurrency)){
		  echo"<option ";
			if ($txtCurr==$curf[0])
				echo " selected ";
		    echo" value='$curf[0]'>$curf[0] </option>";
		  }
		  echo"</select></span></td>";	
		  
 

$Parameters = array();
$Parameters['Module_Pack'] = $Module_Pack;
$Parameters['Module_DoubleUnit'] = $Module_DoubleUnit;
$Parameters['Module_Project'] = $Module_Project;
$Parameters['Module_CostCenter'] = $Module_CostCenter;
$Parameters['Module_ProjectDt'] = $Module_ProjectDt;
$Parameters['Module_CostCenterDt'] = $Module_CostCenterDt;
$Parameters['Module_RestrictProjCost'] = $Module_RestrictProjCost;
$Parameters['Module_RestrictCost'] = $Module_RestrictCost;
$Parameters['ShowCostCentLayout'] = $ShowCostCentLayout;
$Parameters['Module_SerialNo'] = $Module_SerialNo;
$Parameters['Module_ExpireDate'] = $Module_ExpireDate;
$Parameters['DecimalBase'] = $DecimalBase;
$Parameters['Base1'] = $base1;
$Parameters['Base2'] = $base2;
$Parameters['DisableIdeal'] = $DisableIdeal;
$Parameters['Module_Heatno'] = $Module_Heatno;
$Parameters['Actual_Zero'] = $Actual_Zero;

$Parameters['SetupDftWareHouse'] = $SetupDftWareHouse;
$Parameters['defaultwarehouse']  = $defaultwarehouse;
$Parameters['FI_ShowCostPrice']  = $FI_ShowCostPrice;

$Parameters['txtDate'] 		   = $txtDate;
$Parameters['txtCurrCode']     = $txtCurr;
$Parameters['actionmode']     = $actionmode;
$Parameters['ShowStage']     = $ShowStage;
$Parameters['ShowAdditions']     = $ShowAdditions;
$Parameters['Production_extraInfo']     = $Production_extraInfo;
$Parameters['show_preproductionCost']	= $show_preproductionCost;
//session_unregister("gass_parameters");
//$_SESSION['gass_parameters'] = '';
$_SESSION['gass_parameters'] = $Parameters;
 

echo"	  <td rowspan='5' colspan='2' valign='top'>";

echo"	<div id='totals' style='width:270px; margin-top:3px; float:right;'>";
	ShowPreAssembleTotals($connection,$txtPrdId,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess']);
echo"	</div>"; 

echo"	  </td>";

echo "</tr>";
		
/*
echo"   <td rowspan='5' colspan='2' valign='top'><table border='0' style='table-layout:fixed'>
			<col width=110>
			<col width=110>
			
			<tr bgcolor='#D8D8D8' height='30'>
			 <td valign='center' align='right' ><span class='style1'>Total Cost: <br>Actual</span></td> 
			 <td align='center'><input type='text' name='txtPrdTotal' value='$txtPrdTotal' maxlength='20' readonly style='text-align:right; width:100px; height=23px;'></td>
		  </tr>";
		  
	echo" <tr bgcolor='#D8D8D8' height='30'>
			 <td valign='center' align='right' ><span class='style1'>Cost Price/Unit: <br>Actual</span></td> 
			 <td align='center'><input type='text' name='txtPrdCostPerUnit' value='$txtPrdCostPerUnit' maxlength='20' readonly style='text-align:right; width:100px; height=23px;'>
			 <input type='hidden' name='txtPrdTotalIdeal' value='$txtPrdTotalIdeal'>
			 <input type='hidden' name='txtPrdCostPerUnitIdeal' value='$txtPrdCostPerUnitIdeal'>
			 </td>
		  </tr>";
		  
		  
echo"	  </table></td>";		
echo"   </tr>";*/


echo"   <tr bgcolor='#F7F7F7'>
		  <td align='left' ><span class='style1'>Item&nbsp;<span class='style16'>*</span></span></td>";
echo"     <td colspan='7' >";

//if ($txtPrdId == '') {
	echo"  <span class='style1'><label>
           <input type='text' name='txtItemCodeFG' id='txtItemCodeFG' value='$txtItemCodeFG' onkeyup=\"AutoCompleteData(event,this.id,'txtItemCodeFG','txtItemDescFG','items');\" onkeydown=\"return kdItem(0,event);\" style='width:100px;text-transform:uppercase' maxlength='20' onchange=\"CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeH'); GetItemProjCostCent(this.id,this.value,-1);\" />
           <input type='text' name='txtItemDescFG' id='txtItemDescFG' value='$txtItemDescFG' style='width:260px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescH');\" onkeydown=\"return kdItem(1,event);\" />
		   <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Item ' id='lstItemCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistItemsH(1); ' /> </span></label>
		   <label><img src='img/btnopen.jpg' title='Select Item' id='lstItemDescH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistItemsDescH(1);' style='display:none;' /></label>
          </label>
          </span>
          </td>";
/*} else {
	echo " <input type='hidden' name='txtItemCodeFG' value='$txtItemCodeFG' />
	       <input type='hidden' name='txtItemDescFG' id='txtItemDescFG' value='$txtItemDescFG' />
		   <b>$txtItemCodeFG &nbsp;&nbsp; $txtItemDescFG</b></td>";
}*/		  

echo"     <td align='left' valign=top rowspan=2><span class='style1'>Actual Qty ";

if($DisableIdeal == 0)
	echo"<br><br>Ideal Qty</span>";

if($DisableIdeal == 0)
		echo"<br><br><span class='style1'></span>";
		
echo"	  </td>";


echo"     <td  valign=top rowspan=2>
			<span class='style1'  ><input type='text'  name='txtPrdQty' id='txtPrdQty' value='$txtPrdQty' style='width:80px; text-align:right;' onchange=\"adjustQty();CalculateQty();\"/></span>
			<button type='button' id='btnCalculateQty' name='btnCalculateQty' onclick='CalculateQty();return false;' title='Recalculate Details'><span><span>C</span></span></button>";
if($DisableIdeal == 0){



if($txtPrdQtyIdeal=='')
	$txtPrdQtyIdeal = $txtPrdQty;


$txtWstQty = $txtPrdQtyIdeal - $txtPrdQty;
	
echo" 	  <br><span class='style1'><input type='text' name='txtPrdQtyIdeal' id='txtPrdQtyIdeal' value='$txtPrdQtyIdeal' style='width:80px;text-align:right;' onchange=\"CalculateQtyIdeal();\" /></span>
		  <button type='button' id='btnCalculateQtyIdeal' name='btnCalculateQtyIdeal' onclick='CalculateQtyIdeal();return false;' title='Recalculate Details Ideal'><span><span>C</span></span></button>
		<br><span class='style1'><input type='hidden' name='txtWstQty' id='txtWstQty' value='$txtWstQty' style='width:80px' readonly /></span>		  
		  </td>";
}		  
echo"   </tr>";
		


echo"   <tr bgcolor='#F7F7F7'>
		  <td align='left' ><span class='style1'>Warehouse&nbsp;<span class='style16'>*</span></span></td>";
echo"     <td colspan='7' >";

//if ($txtPrdId == '') {
	echo"  <span class='style1'><label>
           <input type='text' name='txtWhCode' id='txtWhCode' value='$txtWhCode' style='width:80px;' maxlength='10' onchange=\"CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeH');\" />
           <input type='text' name='txtWhDesc' id='txtWhDesc' value='$txtWhDesc' style='width:270px;' onchange=\"CheckLookup('chkcodeexists','warehouse','warehouse_name',this.value,'lstWarehouseDescH');\" />
		   <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Warehouse ' id='lstWarehouseCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistWarehouseH(1); ' /> </span></label>
		   <label><img src='img/btnopen.jpg' title='Select Item' id='lstWarehouseDescH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistWarehouseDescH(1);' style='display:none;' /></label>
          </label>
          </span>
          </td>";
/*} else {
	echo " <input type='hidden' name='txtWhCode' value='$txtWhCode' />
	       <input type='hidden' name='txtWhDesc' id='txtWhDesc' value='$txtWhDesc' />
		   <b>$txtWhCode &nbsp;&nbsp; $txtWhDesc</b></td>";
}*/		  

		  
echo"   </tr>";

//echo" <td align='left' colspan='2'><input type='hidden' name='txtPrdQtyIdeal' id='txtPrdQtyIdeal'  value='$txtPrdQtyIdeal' /></td>";
//echo" <td align='left' colspan='2'><input type='hidden' name='txtPrdWasteCalc' id='txtPrdWasteCalc'  value='$txtPrdWasteCalc' /></td>";
//echo" <td align='left' colspan='2'><input type='hidden' name='txtPrdWaste' id='txtPrdWaste'  value='$txtPrdWaste' /></td>";


echo"   <tr bgcolor='#F7F7F7' >
		  <td align='left' ><span class='style1'>Remark</span></td><td colspan='7' > <span class='style1'>
		  <input type='text' name='txtRemark' id='txtRemark' value='".htmlspecialchars($txtRemark, ENT_QUOTES)."'  style='width:385px;' maxlength='100' ></td>";

echo"  <td align='left' ><span class='style1'>Yield Qty</span></td><td colspan='7' > <span class='style1'>
		  <input type='text' name='txtqtyill' id='txtqtyill' value='".htmlspecialchars($txtqtyill, ENT_QUOTES)."' ";
if($Production_extraInfo==1) echo " onkeyup=\"changePrice();\" "; 
echo " style='width:80px;text-align:right;' ></td>";

if($Production_extraInfo == 1){
$queryCalcQty = "select round(sum(Productiondt_Quantity),".$DecimalBase.") from temp_productiondt B inner join items A on A.item_code=B.Item_Code 
				where A.Group_code != 'PKG' and Production_ID = $txtPrdId;";
$resCalcQty = mysql_query($queryCalcQty,$connection);
$rowCalcQty = mysql_fetch_row($resCalcQty);		
$txttotalQty = 	$rowCalcQty[0];
echo"   </tr>";	
echo"   <tr bgcolor='#F7F7F7' >
		  <td align='left' ></td><td colspan='7' ></td>";

echo"  <td align='left' ><span class='style1'>Total Qty</span></td><td colspan='7' > <span class='style1'>
		  <input type='text' name='txttotalQty' id='txttotalQty'  readonly value='".htmlspecialchars($txttotalQty, ENT_QUOTES)."'  style='width:80px;text-align:right;' ></td>";
echo "</tr>";		  
}


echo"   </tr>";		  
	  

if ($Module_ExpireDate == 1 || $Module_Sorderproduction == 1) {

echo"   <tr bgcolor='#F7F7F7'>";
	echo"	<td align='left' ><span class='style1'>Expire Date</span></td>
			<td colspan=7><input type='text' name='txtPrdExpireDate' value='$txtPrdExpireDate' maxlength='10' style='width:80px;' >
			  <A HREF='#' onClick=\"displayCalendar(document.production.txtPrdExpireDate,'dd/mm/yyyy',this); return false;\" NAME='anchor1' ID='anchor1'><img src='./img/cal.gif' border=0 align=center></A></td>";	
echo"   	<td valign='center' align='left' ><span class='style1'>Batch No</span></td>
			<td><input type='text' name='txtPrdBatchNo' id='txtPrdBatchNo' value='".htmlspecialchars($txtPrdBatchNo, ENT_QUOTES)."'  maxlength='40' style='width:80px;'  ></td>";			  

			
echo"   <tr bgcolor='#F7F7F7'>";
	echo"	<td align='left' ><span class='style1'>Prod.Unit</span></td>
			<td><input type='text' name='txtPrdUnit' id='txtPrdUnit' value='".htmlspecialchars($txtPrdUnit, ENT_QUOTES)."'  maxlength='40' style='width:70px;'  ></td>";			  
echo"   	<td valign='center' align='left' ><span class='style1'>Mixer</span></td>
			<td><input type='text' name='txtPrdMixer' id='txtPrdMixer' value='".htmlspecialchars($txtPrdMixer, ENT_QUOTES)."'  maxlength='40' style='width:70px;'  ></td>";			  

echo"   	<td valign='center' align='left' ><span class='style1'>Size</span></td>
			<td><input type='text' name='txtPrdSize' id='txtPrdSize' value='".htmlspecialchars($txtPrdSize, ENT_QUOTES)."'  maxlength='40' style='width:70px;'  ></td>";				
			
echo"</tr>";
} else {
    
	echo" <input type='hidden' name='txtPrdExpireDate' value='$txtPrdExpireDate'>";
	echo" <input type='hidden' name='txtPrdBatchNo' value='$txtPrdBatchNo'>";
	echo" <input type='hidden' name='txtPrdUnit' value='$txtPrdUnit'>";
	echo" <input type='hidden' name='txtPrdMixer' value='$txtPrdMixer'>";
	echo" <input type='hidden' name='txtPrdSize' value='$txtPrdSize'>";
	
}	
		
echo"   <tr bgcolor='#F7F7F7'>";

if ($Module_Project == 1) {
	echo" <td align='left' ><span class='style1'>Project&nbsp;<span class='style16'>*</span></span></td>
	      <td align='left' colspan='2'> <span class='style1'>
		    <input type='text' name='txtProjectCode' id='txtProjectCode' value='$txtProjectCode' style='width:99px;' maxlength='20' onchange=\"CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCodeH'); \" /></span>
		    <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Project ' id='lstProjectCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
} else {
	echo" <td align='left' colspan='3'><input type='hidden' name='txtProjectCode' id='txtProjectCode' value='$txtProjectCode' /></td>";

}			

if ($Module_CostCenter == 1 && $ShowCostCentLayout == 1) {		  
	echo" <td align='right' ><span class='style1'>Costcenter&nbsp;<span class='style16'>*</span></span></td>		
		  <td align='left' colspan='4'><span class='style1'>
		    <input type='text' name='txtCostCentCode' id='txtCostCentCode' value='$txtCostCentCode' style='width:149px;' maxlength='20' onchange=\"CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCodeH');\" /></span>
		    <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Costcenter ' id='lstCostCentCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
} else {
	echo" <td align='left' colspan='5'><input type='hidden' name='txtCostCentCode' id='txtCostCentCode' value='$txtCostCentCode' /></td>";

}
if ($Module_Sorderproduction == 1) {	
	echo"     <td align='left'  ><span class='style1'>Client</span></td><td colspan ='3'>
	<input type='text'   name='txtClientCode' id='txtClientCode' value='$txtClientCode'   style='width:80px;' maxlength='10'  /> 
	<input type='text' name='txtclientname' id='txtclientname' value='$txtclientname' style='width:200px;'>
	 <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Client ' id='lstClientCodeH' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistClient(1); ' /> </span></label>
	</td>";
} else			
echo"     <td align='left' colspan='2'><input type='hidden'   name='txtClientCode' id='txtClientCode' value='' maxlength='10'  /> </td>";
echo"   </tr>";

echo"   <tr bgcolor='#D8D8D8' height='30'>
			<td colspan='10' align='center'>
		 <button type='button' id='btnLoad' name='btnLoad' ";
		if ($txtPrdId != '' )
			echo " disabled ";
		echo" onclick='LoadMaterials($Module_RestrictProjCost, $Module_RestrictCost, $Module_Project, $Module_CostCenter, $i_expire, $Module_ExpireDate,$Module_Sorderproduction);return false;'
		 
		 title='Load Raw Materials'><span><span>Load Materials</span></span></button>";
			
			
echo " <button type='button' id='btnLoad' name='btnLoad' title='Load Materials By Project' onclick=\"OpenMaterialProj();return false;\" ><span><span>Load Materials By Project</span></span></button>";				
				
if ($FI_ProductionApp == 1 && $Module_SerialNo == 1 && $txtItemWithSerialNo == 1 && $txtPrdId != '')	
	echo"  <button type='button' id='btnAddSerial' name='btnAddSerial' onclick='AddSerial(); return false;' title='Add Serial No'><span><span>Add Serials</span></span></button>";				
if($Production_extraInfo == 1 && $txtPrdId != '')
	echo"  <button type='button' id='btnXtraInfo' name='btnXtraInfo' onclick='AddInfo($txtPrdId); return false;' title='More Information'><span><span>Quality Assurance</span></span></button>";
echo"       </td>";
echo"<td bgcolor='#F7F7F7' align='left' colspan='2'>&nbsp;</td>
		</tr>";



//start material by project box
echo "<tr><td colspan='10'><div id='matbox'>";
echo "<table width='600px' cellpadding='0' cellspacing='0'><col width='150px'><col width='450px'>";
echo "<tr><td colspan='2' align='right' valign='top'><img src='img/close-button.png' height='20px' width='20px' onclick=\"closeMatWin();\" onmouseover=\"style.cursor='hand'\"></td></tr>";

echo "<tr height='30px'>
		<td  style='padding-left: 10px;'><span class='style1'>Project</span></td>
		<td><input type='text' name='mat_project' id='mat_project' value='$mat_project' style='width:150px;'>
		<img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick=\"showMatProject();\" />
		<input type='text' name='mat_projectdesc' id='mat_projectdesc' value='$mat_projectdesc' style='width:250px;'>
		</td>
	 </tr>";

echo "<tr height='30px'>
		<td  style='padding-left: 10px;'><span class='style1'>Warehouse</span></td>
		<td><input type='text' name='mat_warehouse' id='mat_warehouse' value='$mat_warehouse' style='width:150px;'>
		<img src='img/btnopen.jpg' title=' Select Warehouse ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick=\"showMatWarehouse(1);\" />
		<input type='text' name='mat_warehousedesc' id='mat_warehousedesc' value='$mat_warehousedesc' style='width:250px;'>
		</td>
	</tr>";


echo "<tr height='30px'>
			<td  style='padding-left: 10px;'><span class='style1'>Transaction Included</span></td>
			<td><input type='checkbox' name='chk_tr' id='chk_tr' value='1' checked onclick=\"setTrans('TR');\"><span class='style1'>Transfer</span></td>
	 </tr>";
echo "<tr height='30px'><td colspan='2' align='center'></td></tr>";
echo "<tr height='30px'><td colspan='2' align='center'>
			<button type='button' id='btnLoad' name='btnLoad' title='Load Materials By Project' onclick=\"LoadMaterialProj('$Module_Project', '$Module_RestrictProjCost', '$Module_CostCenter', '$Module_RestrictCost');return false;\" ><span><span>Load Materials By Project</span></span></button>
		  </td>
	  </tr> ";				

echo "</table>";
echo "</div></tr>";		  
		  
		
echo"   <tr bgcolor='#ffffff'><td colspan='12' valign='top'>";
echo"	<div id='datagrid' class='grid' style='width:1090px;' >";

//Show Items Grid
ShowPreAssembleDtGrid($connection,'PreASS',$txtPrdId,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess'],'ProductionDt.ProductionDt_ID');

echo"	  </div>"; 
echo"   </td></tr>"; 


echo"   </table></td></tr>"; 
 echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo" </table>  ";//end of last table menu
	
if ($actionmode == "post" && $PrPrdId != '') {
	echo '<script type="text/javascript" language="javascript">';
	
	if ($Login_FastPrint == 1) 
		echo ' PrintProduction(\''.$PrPrdId.'\', \''.$PrPrdRef.'\'); ';
		
	echo ' ClearPage1(); ';
	echo '</script>';

}

print <<<HERE
</form>
</body>
</html>
HERE;

?>