<head>
<?php
$table_name = $_SESSION['db_prefix']."_captured_timber";
$laminated_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Laminated' AND cap_job = '".$_SESSION['job_no']."' ";
$laminated_result = mySQL_query($laminated_query);
$laminated_num = mysql_num_rows($laminated_result);
?>

<!--jscript-->
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script>  

<script>
function qty_check(p) {
	
	var reserved = parseFloat(document.getElementById('lam_reserved_text'+p).value);
	var authorised = parseFloat(document.getElementById('lam_auth_text'+p).value);
	
	
	if (authorised > reserved) {
	alertMessage(1, '', 'There is '+reserved+'mtr reserved for this job. <br>You cannot authorise more than reserved.<br>Please enter a new quantity.');
	document.getElementById('lam_auth_text'+p).value = '';
	}
	
}
</script>

<script>
function qty_check2(p) {
	
	var reserved = parseFloat(document.getElementById('lam_reserved_text'+p).value);
	var authorised = parseFloat(document.getElementById('lam_auth_text'+p).value);
	
	
	if (authorised < reserved) {
	alertMessage(1, '', 'There is '+reserved+'mtr reserved for this job. <br>If you do not use it, this will become redundant stock.');
	}
	
}
</script>
</head>
<?php include('../include/wait.php') ?>



<body onLoad="MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');">
<form id="form1" method="get" action="authorisationWizbase.php">
<div class="wizard_page_middle_frame">

         <div class="wizard_frame">
                <label style="font-size:18px; font-weight:700;">Bracing</label>
                <br>
                <br />
                <table width="650" style="border-collapse:collapse; border-bottom-style:solid; border-bottom-width:thin;" >
                  <tr>
                    <td width="275" align="left">&nbsp;&nbsp;Item</td>
                    <td width="76">Length</td>
                    <td width="95">Current Stock</td>
                    <td width="65">Quantity</td>
                    <td align="center">Length</td>
                  </tr>
                </table>
                
                <table id="table_resume_wizard3">
                <?php 
					  $n=0;	
					  $t=1;
					  while ($n<$_SESSION['list_5a']) {
                      $w=$_SESSION['list_5a']-1;
					  ?>
                  <?php
				        $c=0;
					    $m=0; 
						$temp_name='';  
						while ($m<$_SESSION['item_num'.$n]) {?>
                  <script>
    $(document).ready(function(){
        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".text60<?php echo $n+10?>").each(function() {
             $(this).keyup(function(){
                calculateSum<?php echo $n+10?>();
            });
        });

    });
 
    function calculateSum<?php echo $n+10?>() {
         var sum = 0;
        //iterate through each textboxes and add the values
        $(".text60<?php echo $n+10?>").each(function() {
             //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
         });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum<?php echo $n+10?>").html(sum.toFixed(2));
		issued_total<?php echo $n+10?> = (sum.toFixed(2));
		document.getElementById('issued_total<?php echo $n+10?>').value = issued_total<?php echo $n+10?> ;
		var  sum3 = sum1 + sum2;
    }
</script>
                  <?php
   			            
						if ($_SESSION['s']==3) {
						if(isset($_GET['check'.($n+10).($m+10)]) ) {
								$_SESSION['check'.($n+10).($m+10)] = 'yes';
						}
						else {
								$_SESSION['check'.($n+10).($m+10)] = 'no';
						}
						}
						 
						if ($_SESSION['check'.($n+10).($m+10)]=='yes') {
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$name_query = "SELECT store_item_id, width, depth, length, class, quantity, reserved FROM $table_name WHERE store_item_id = '".$_SESSION['item_id'.($n+10).($m+10)]."'";
						$name_result = mySQL_query($name_query);
						$name_num = mysql_num_rows($name_result);
					  
                  		$a1 = mysql_result($name_result, 0, "store_item_id");
						$a4 = mysql_result($name_result, 0, "reserved");
						$a5 = mysql_result($name_result, 0, "quantity");
						$a6 = mysql_result($name_result, 0, "length");
						
						
					?>
                  <tr>
                    <td width='264' align="left"><label class="label_std">
                        <?php if ($temp_name!=$_SESSION['bracing_name_'.$n]) {echo $_SESSION['bracing_name_'.$n];} ?>
                      </label>
                      <?php $temp_name=$_SESSION['bracing_name_'.$n]?>
                      &nbsp;&nbsp;&nbsp;</td>
                    <td width='76' align="left"><?php if (strlen($a6)==3) { echo '&nbsp;&nbsp;'.$a6; } else { echo $a6;} ?>
                      &nbsp;&nbsp;mm</td>
                    <td width='66' align="right"><?php echo number_format($a5, 0, '.', ',');?>&nbsp;&nbsp;no                  
                      &nbsp;</td>
                    <td width='38' align="left">
                      <input type="hidden" name="<?php echo "length".($n+10).($m+10) ?>" id="<?php echo "length".($n+10).($m+10) ?>"  value="<?php echo $a6 ?>"/>
                      <input type="hidden" name="<?php echo "item_no".($n+10).($m+10) ?>" id="<?php echo "item_no".($n+10).($m+10) ?>" value="<?php echo $a1 ?>">
                    </td>
                    <td width='66' align="left"><input name="<?php echo 'bracing_qty'.($n+10).($m+10)?>" type="text" autocomplete="off" class="text40" id="<?php echo 'bracing_qty'.($n+10).($m+10)?>" value="<?php echo $_SESSION['bracing_qty'.($n+10).($m+10)]?>" autofocus onKeyPress="return numbersonly(this, event)"style="text-align:right"  tabindex="<?php echo $t ?>" <?php $t=$t+1?>
 onkeyup=" 
   var textValue1 = document.getElementById('<?php echo "length".($n+10).($m+10) ?>').value;
   var textValue2 = document.getElementById('<?php echo "bracing_qty".($n+10).($m+10) ?>').value;
   var textValue3 = (((textValue1 * textValue2) / 1000).toFixed(2));
   document.getElementById('<?php echo 'sub'.($n+10).($m+10) ?>').innerHTML = textValue3;
   document.getElementById('<?php echo 'text'.($n+10).($m+10) ?>').value = textValue3;
   calculateSum<?php echo $n+10?>(); 
" 
onblur=" 
   var textValue1 = document.getElementById('<?php echo "length".($n+10).($m+10) ?>').value;
   var textValue2 = document.getElementById('<?php echo "bracing_qty".($n+10).($m+10) ?>').value;
   var textValue3 = (((textValue1 * textValue2) / 1000).toFixed(2));
   document.getElementById('<?php echo 'sub'.($n+10).($m+10) ?>').innerHTML = textValue3;
   document.getElementById('<?php echo 'text'.($n+10).($m+10) ?>').value = textValue3;
   calculateSum<?php echo $n+10?>(); 
"
 ></td>
                    <td width='76' align="right"><label id=<?php echo 'sub'.($n+10).($m+10) ?> ></label>
                      &nbsp;&nbsp;m</td>
                    <td width='32' align="right"><input type="text" name="<?php echo 'text'.($n+10).($m+10) ?>" id="<?php echo 'text'.($n+10).($m+10) ?>" class='text60<?php echo $n+10?>' style="width:20px;"  hidden="hidden" ></td>
                    <?php
					}
					else {
						$_SESSION['bracing_qty'.($n+10).($m+10)]='';
					}
					$m++;
					$_SESSION['list_5d'.$n]=$m;	
					}
					
					  ?>
                  </tr>
                  <?php if ($temp_name!='') { ?>
                  <tr>
                    <td colspan="2" align="left">&nbsp;Quoted Length: <?php echo $_SESSION['bracing_length_'.$n]  ?> m&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                    <td colspan="3" align="right">Total  Length:</td>
                    <td align="right"><label class="txt3"><b><span id="sum<?php echo $n+10?>"></span> m</b></label></td>
                    <td align="right">
                      <input name="issued_total<?php echo $n+10?>"  id="issued_total<?php echo $n+10?>" type="text" size="5" readonly hidden="hidden">
                    </td>
                  </tr>
                  <tr>
                    <td colspan="7" align="right"><hr></td>
                  </tr>
                  <?php
				  }
				  				  
				  $n++;
				  $_SESSION['list_5c']=$n;			 
					  }
				  ?>
                </table>
    </div>
              
              <div class="wizard_frame">
                <label style="font-size:18px; font-weight:700;">Sundries</label>
                <br>
                <br />
                <table width="650" style="border-collapse:collapse; border-bottom-style:solid; border-bottom-width:thin;" >
                  <tr>
                    <td width="275">&nbsp;&nbsp;Item</td>
                    <td width="76">Length</td>
                    <td width="95">Current Stock</td>
                    <td width="65">Quantity</td>
                    <td align="center">Length</td>
                  </tr>
                </table>
                
                <table id="table_resume_wizard3">
                
                <?php 
				
					  $n=0;	
					  while ($n<$_SESSION['list_5b']) {
                      $w=$_SESSION['list_5b']-1;
					  ?>
                  <?php
				        $c=0;
					    $m=0; 
						$temp_name2='';  
						while ($m<$_SESSION['item2_num'.$n]) {?>
                  <script>
    $(document).ready(function(){
        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".text60a<?php echo $n+10?>").each(function() {
             $(this).keyup(function(){
                calculateSum2<?php echo $n+10?>();
            });
        });

    });
 
    function calculateSum2<?php echo $n+10?>() {
         var sum2 = 0;
        //iterate through each textboxes and add the values
        $(".text60a<?php echo $n+10?>").each(function() {
             //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum2 += parseFloat(this.value);
            }
         });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum2<?php echo $n+10?>").html(sum2.toFixed(2));
		issued2_total<?php echo $n+10?> = (sum2.toFixed(2));
		document.getElementById('issued2_total<?php echo $n+10?>').value = issued2_total<?php echo $n+10?> ;
		
    }
    </script>
                  <?php
   			            
						if ($_SESSION['s']==3) {
						if(isset($_GET['check2'.($n+10).($m+10)]) ) {
								$_SESSION['check2_'.($n+10).($m+10)] = 'yes';
						}
						else {
								$_SESSION['check2_'.($n+10).($m+10)] = 'no';
						}
						}
						 
						if ($_SESSION['check2_'.($n+10).($m+10)]=='yes') {
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$name_query = "SELECT store_item_id, width, depth, length, class, quantity, reserved FROM $table_name WHERE store_item_id = '".$_SESSION['item2_id'.($n+10).($m+10)]."'";
						$name_result = mySQL_query($name_query);
						$name_num = mysql_num_rows($name_result);
					  ?>
                  <?php
                   
					  					 
						$a1 = mysql_result($name_result, 0, "store_item_id");
						$a4 = mysql_result($name_result, 0, "reserved");
						$a5 = mysql_result($name_result, 0, "quantity");
						$a6 = mysql_result($name_result, 0, "length");
					?>
                  <tr>
                    <td width='264' align="left"><label class="label_std">
                        <?php if ($temp_name2!=$_SESSION['sundries_name_'.$n]) {echo $_SESSION['sundries_name_'.$n];} ?>
                      </label>
                      <?php $temp_name2=$_SESSION['sundries_name_'.$n]?>
                      &nbsp;&nbsp;&nbsp;</td>
                    <td width='76' align="left"><?php if (strlen($a6)==3) { echo '&nbsp;&nbsp;'.$a6; } else { echo $a6;} ?>
                      &nbsp;&nbsp;mm</td>
                    <td width='66' align="right"><?php echo number_format($a5, 0, '.', ',');?>&nbsp;&nbsp;no
                      &nbsp;</td>
                    <td width='38' align="left">
                      <input type="hidden" name="<?php echo "length2".($n+10).($m+10) ?>" id="<?php echo "length2".($n+10).($m+10) ?>"  value="<?php echo $a6 ?>"/>
                      <input type="hidden" name="<?php echo "item2_no".($n+10).($m+10) ?>" id="<?php echo "item2_no".($n+10).($m+10) ?>" value="<?php echo $a1 ?>">
                    </td>
                    <td width='66' align="left"><input name="<?php echo 'sundries_qty'.($n+10).($m+10)?>" type="text" autocomplete="off" class="text40" id="<?php echo 'sundries_qty'.($n+10).($m+10)?>" value="<?php echo $_SESSION['sundries_qty'.($n+10).($m+10)]?>" autofocus onKeyPress="return numbersonly(this, event)"style="text-align:right"  tabindex="<?php echo $t ?>" <?php $t=$t+1?>
 onkeyup=" 
   var textValue1 = document.getElementById('<?php echo "length2".($n+10).($m+10) ?>').value;
   var textValue2 = document.getElementById('<?php echo "sundries_qty".($n+10).($m+10) ?>').value;
   var textValue3 = (((textValue1 * textValue2) / 1000).toFixed(2));
   document.getElementById('<?php echo 'sub2'.($n+10).($m+10) ?>').innerHTML = textValue3;
   document.getElementById('<?php echo 'text2'.($n+10).($m+10) ?>').value = textValue3;
   calculateSum2<?php echo $n+10?>(); 
" 
onblur=" 
   var textValue1 = document.getElementById('<?php echo "length2".($n+10).($m+10) ?>').value;
   var textValue2 = document.getElementById('<?php echo "sundries_qty".($n+10).($m+10) ?>').value;
   var textValue3 = (((textValue1 * textValue2) / 1000).toFixed(2));
   document.getElementById('<?php echo 'sub2'.($n+10).($m+10) ?>').innerHTML = textValue3;
   document.getElementById('<?php echo 'text2'.($n+10).($m+10) ?>').value = textValue3;
   calculateSum2<?php echo $n+10?>(); 
"
 ></td>
                    <td width='76' align="right"><label id=<?php echo 'sub2'.($n+10).($m+10) ?> > </label>
                      &nbsp;&nbsp;m</td>
                    <td width='32' align="right"><input type="text" name="<?php echo 'text2'.($n+10).($m+10) ?>" id="<?php echo 'text2'.($n+10).($m+10) ?>" class='text60a<?php echo $n+10?>' style="width:20px;" hidden="hidden"  ></td>
                    <?php
					}
					else {
						$_SESSION['sundries_qty'.($n+10).($m+10)]='';
					}
					$m++;
					$_SESSION['list_5d'.$n]=$m;	
					}
					
					  ?>
                  </tr>
                  <?php if ($temp_name2!='') { ?>
                  <tr>
                    <td colspan="2" align="left">&nbsp;Quoted Length: <?php echo $_SESSION['sundries_length_'.$n]  ?> m&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
                    <td colspan="3" align="right">Total Length:</td>
                    <td align="right"><label class="txt3"><b><span id="sum2<?php echo $n+10?>"></span> m</b></label></td>
                    <td align="right">
                      <input name="issued2_total<?php echo $n+10?>"  id="issued2_total<?php echo $n+10?>" type="text" size="5" readonly  hidden="hidden">
                    </td>
                  </tr>
                  <tr>
                    <td colspan="7" align="right"><hr></td>
                  </tr>
                  <?php
				  }
				  
				  
				  $n++;
				  $_SESSION['list_5c']=$n;			 
					  }
					  
				  ?>
                </table>
                
              </div>
              
              <div class="wizard_frame">
  <label style="font-size:18px; font-weight:700;">Laminated beams</label>
  - reserved for this job<br><br>
 <table width="650" id="table_list" >
    <thead>
      <th width="222" align="center">Laminated beam size</th>
      <th align="center">Captured</th>
      <th align="center">Reserved in stock<br>
        for this job.</th>
      <th align="center">Authorise</th>
    </thead>  
  <?php 
				 			 
					  $p=0;	
					  $_SESSION['list_5c'] = $laminated_num;
					  
					  while ($p<$_SESSION['list_5c']) {
						    
				 			$_SESSION['laminated_name_'.$p] = mysql_result($laminated_result, $p, "cap_timber_name");
				 			$_SESSION['laminated_line'.$p] = mysql_result($laminated_result, $p, "cap_timber_id");
				 			$_SESSION['laminated_depth'.$p] = mysql_result($laminated_result, $p, "cap_depth");
				 			$_SESSION['laminated_width'.$p] = mysql_result($laminated_result, $p, "cap_width");
				 			$_SESSION['laminated_class'.$p] = mysql_result($laminated_result, $p, "cap_class");
				 			$_SESSION['laminated_type_'.$p] = $_SESSION['laminated_depth'.$p]."x".$_SESSION['laminated_width'.$p]." (".$_SESSION['laminated_class'.$p].")"; 
						    $_SESSION['laminated_length_'.$p] = mysql_result($laminated_result, $p, "cap_timber_length");
							
						$table_name = $_SESSION['db_prefix']."_store_items";
						$name_query = "SELECT store_item_id, width, depth, length, class, price FROM $table_name WHERE subCategory='timber' AND width = '".$_SESSION['laminated_width'.$p]."' AND depth = '".$_SESSION['laminated_depth'.$p]."'  AND class='".$_SESSION['laminated_class'.$p]."'  ORDER BY length ASC";
						$name_result = mySQL_query($name_query);
						$store_item_id = mysql_result($name_result, 0, "store_item_id");
						$_SESSION['laminated_store_item_id_'.$p] = $store_item_id;	
				
			$table_name = $_SESSION['db_prefix']."_store_items_ref_lines";
			$res_length_query = "SELECT ref_2, ref_3 FROM $table_name WHERE store_item_id = '".$store_item_id."' AND ref_1 = '".$_SESSION['job_no']."'  ";
			$res_length_result = mySQL_query($res_length_query);
			$res_length_num = mysql_num_rows($res_length_result);
			if ($res_length_num!=0) {
			$res_length = mysql_result($res_length_result, 0, "ref_2");
			$del_length = mysql_result($res_length_result, 0, "ref_3");
			}
			else {
			$res_length = 0;
			$del_length = 0;
			}
			$res_stock = $res_length - $del_length;
					  ?>
 
 
    <tr>
      <td><?php echo $_SESSION['laminated_name_'.$p] ?></td>
      <td align="right"><?php echo number_format($_SESSION['laminated_length_'.$p], 0, '.', ',')  ?></td>
      <td align="right"><?php echo $res_stock ?><input name="lam_reserved_text<?php echo $p ?>" id="lam_reserved_text<?php echo $p ?>" type="hidden" value="<?php echo $res_stock ?>"></td>
      <td align="right"><input type="text" name="lam_auth_text<?php echo $p ?>" id="lam_auth_text<?php echo $p ?>" class="text60" onKeyPress="return isNumberKey(event)" onkeyup=" this.value = this.value.replace (/(\...).*/, '$1'); qty_check(<?php echo $p ?>);"  onBlur="qty_check2(<?php echo $p ?>);" value="<?php echo $_SESSION['lam_authorised_'.$p] ?>"></td>
    </tr>
  
  
 <?php
 $p++;
  }
   ?>
</table>    <br>
<br>

</div>
              <div class="wizard_frame" >
              
              <table width="660" height="50" align="left" cellpadding="20"  id="table_button_bar">
                <tr>
                  <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false;MM_showHideLayers('wait','','show');MM_goToURL('parent','authorisationWizbase.php?s=&amp;p=3');return document.MM_returnValue"  tabindex="<?php echo $t+2 ?>" /></td>
                  <td width="115">&nbsp;</td>
                  <td width="99"><input name="p" type="hidden" id="p" value="4" />
                    <input name="s" type="hidden" id="s" value="4" />
                    
                    <input name="screen" type="hidden" id="screen" value="0" /></td>
                  <td width="125" align="right"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="needToConfirm = false; cancel_confirmation(1, '../production/production_main.php');" value="Cancel"  tabindex="<?php echo $t+3 ?>" /></td>
                  <td width="110" align="right"><input type="submit" name="next_button" class="imagebutton" value="Next" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');"  tabindex="<?php echo $t ?>" /></td>
                </tr>
              </table>
              </div>
              </div>

</form>

<!--form validation--> 

<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
 
  <?php
   $n=0;
   while ($n<$_SESSION['list_5a']) { 
   $m=0;
   while ($m<$_SESSION['item_num'.$n]) { 
   ?>
   if(document.getElementById('<?php echo 'bracing_qty'.($n+10).($m+10) ?>'))
  frmvalidator.addValidation("<?php echo 'bracing_qty'.($n+10).($m+10) ?>","req","Not all quantities for bracing were entered.");
  <?php
  $m++;
	  }
	  $n++;
  }
  ?>
</script> 
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
   <?php
   $n=0;
   while ($n<$_SESSION['list_5b']) { 
   $m=0;
   while ($m<$_SESSION['item2_num'.$n]) { 
   ?>
   if(document.getElementById('<?php echo 'sundries_qty'.($n+10).($m+10) ?>'))
  frmvalidator.addValidation("<?php echo 'sundries_qty'.($n+10).($m+10) ?>","req","Not all quantities for sundries were entered.");
  <?php
  $m++;
	  }
	  $n++;
  }
  ?>
</script>
<?php
   $n=0;
   while ($n<$_SESSION['list_5a']) { 
   $m=0;
   while ($m<$_SESSION['item_num'.$n]) {   ?>
<script>
   var textValue1 = document.getElementById('<?php echo "length".($n+10).($m+10) ?>').value;
   var textValue2 = document.getElementById('<?php echo "bracing_qty".($n+10).($m+10) ?>').value;
   var textValue3 = (((textValue1 * textValue2) / 1000).toFixed(2));
   document.getElementById('<?php echo 'sub'.($n+10).($m+10) ?>').innerHTML = textValue3;
   document.getElementById('<?php echo 'text'.($n+10).($m+10) ?>').value = textValue3;
   calculateSum<?php echo $n+10?>(); 
   </script>
<?php
   $m++;
   }
   $n++;
   }
   ?>
<?php
   $n=0;
   while ($n<$_SESSION['list_5b']) {
   $m=0;
   while ($m<$_SESSION['item2_num'.$n]) {
	    ?>
<script>
   var textValue1 = document.getElementById('<?php echo "length2".($n+10).($m+10) ?>').value;
   var textValue2 = document.getElementById('<?php echo "sundries_qty".($n+10).($m+10) ?>').value;
   var textValue3 = (((textValue1 * textValue2) / 1000).toFixed(2));
   document.getElementById('<?php echo 'sub2'.($n+10).($m+10) ?>').innerHTML = textValue3;
   document.getElementById('<?php echo 'text2'.($n+10).($m+10) ?>').value = textValue3;
   calculateSum2<?php echo $n+10?>();
   </script>
<?php
   $m++;
   }
   $n++;
   }
   ?>
</body>
