<head>
<?php


$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$mitek_query = "SELECT store_item_id, quantity, authorised, delivered FROM $table_name WHERE quote_no = '".$_SESSION['job_no']."' AND truss_item = 'Mitek' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);

?>
<!--style sheets-->

<!--jscript-->
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script>  
<script type="text/javascript">
function validate_required(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, 'deliver_cash_saleWizbase.php?p=2&s=', 'The amount to deliver exceeds the balance, the entry will be cleared.<br><br>Please enter a valid amount.');
	input.value = "";
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<script type="text/javascript">
function validate_stock(input,alerttxt,maxvalue, box_no)
{

with (input)
  {
  if (value>maxvalue)
    {
	alertMessage(1, 'deliver_cash_saleWizbase.php?p=2&s=', 'The amount exceeds the stock quantity, the entry will be cleared.<br><br>Please enter a valid amount.');
	input.value = "";
	return false;
	}
  else
    {
    return true;
	}
}

}
</script>

<script>
function del_all() {
	
	for (var i=0; i < 25; i++) {
		  if ( Number(document.getElementById('del_quantity'+i).value) > Number(document.getElementById('store_quantity_text'+i).value) ) {
		            document.getElementById('quantity_text_'+i).value=document.getElementById('store_quantity_text'+i).value;
					document.getElementById('quantity_text_'+i).style="background-color:#F96; text-align:right;"
		 }
		 else {
			        document.getElementById('quantity_text_'+i).value=document.getElementById('del_quantity'+i).value;		
		   }
		   
	}
}
		  
</script>

</head>
<?php include('../include/wait.php') ?>
<link href="../css/wait.css" rel="stylesheet" type="text/css">
<body onLoad="MM_showHideLayers('wait','','hide');" onFocus="MM_showHideLayers('wait','','hide')"; document.getElementById('quantity_text_0').disabled=false;>
<div class="page_middle_frame">
<form action="deliver_cash_saleWizbase.php" method="get" name="form1" id="form1">
<table id="tableMain" width="595">
    <!--begin of tableMain-->
    <tr>
      <td><!--center of tableMain-->

        <div class="scroll_frame_medium" style="height:340px; width:640px;">
          <table width="626">
               <tr>
               <td width="222"><label class="label_std" style="font-size:14px;">Timber:</label></td>
               <td width="46">&nbsp;</td>
               <td>&nbsp;</td>
               <td>&nbsp;</td>
               <td width="58">&nbsp;</td>
               <td>&nbsp;</td>
               <td align="right"><input name="button" type="button" class="imagebutton" id="button" onClick="del_all();" value="Deliver all" checked="checked" ></td>
              </tr>
               <tr>
                 <td>&nbsp;</td>
                 <td>Stock</td>
                 <td><label class="label_std">On order</label></td>
                 <td>Authorised</td>
                 <td>Delivered</td>
                 <td>Balance</td>
                 <td><label class="label_std">To deliver</label></td>
               </tr>
             <?php 
				  $i=0;
				  while ($i<$mitek_num) { 
				       
						$store_item_id = mysql_result($mitek_result, $i, "store_item_id");
						$quantity = mysql_result($mitek_result, $i, "quantity");
						$auth_quantity = mysql_result($mitek_result, $i, "authorised");
						$delivered_quantity = mysql_result($mitek_result, $i, "delivered");
						$balance_quantity = $auth_quantity - $delivered_quantity; 					
						
						$table_name = $_SESSION['db_prefix']."_store_items";
						$item_name_query = "SELECT item_name, unit, quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
						$item_name_result = mySQL_query($item_name_query);
						$item_name = mysql_result($item_name_result, 0, "item_name"); 
						$unit = mysql_result($item_name_result, 0, "unit"); 
						$store_quantity = mysql_result($item_name_result, 0, "quantity");
												
						
						
						$table_name = $_SESSION['db_prefix']."_cash_deliver_lines_temp";
						$del_query = "SELECT quantity FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND user_name = '".$_SESSION['username']."' ";
						$del_result = mySQL_query($del_query);
						$del_num = mysql_num_rows($del_result);
						if ($del_num > 0) {
						      $del_quantity = mysql_result($del_result, 0, "quantity");
						}
						else {
							  $del_quantity = '';
						}
								
				 ?>
             <tr>
               <td><label class="label_std" ><?php echo $item_name ?></label>
                 <input type="hidden" name="store_item_name<?php echo $i?>" id="store_item_name<?php echo $i?>" value="<?php echo $item_name ?>">
                 <input type="hidden" name="store_item_id<?php echo $i?>" id="store_item_id<?php echo $i?>" value="<?php echo $store_item_id ?>">
               </td>
               <td><?php echo number_format($store_quantity, 0, '.', '')?><input name="store_quantity_text<?php echo $i?>" id="store_quantity_text<?php echo $i?>" type="hidden" value="<?php echo number_format($store_quantity, 0, '.', '') ?> ">
                </td>
               <td width="56" align="left"><label for="textfield"><?php echo number_format($quantity, 0, '.', '')?></label></td>
               <td width="74">&nbsp;&nbsp;<?php echo number_format($auth_quantity, 0, '.', '')?></td>
               <td><?php echo number_format($delivered_quantity, 0, '.', '')?></td>
               <td width="56"><input type="hidden" name="del_quantity<?php echo $i?>" id="del_quantity<?php echo $i?>" value="<?php echo $balance_quantity ?> ">                 <?php echo number_format($balance_quantity, 0, '.', '')?></td>
               <td width="82"><input name="quantity_text_<?php echo $i?>" type="text" class="text60" id="quantity_text_<?php echo $i?>" style="text-align:right" onKeyPress="return numbersonly(this, event)"  value="<?php if ($del_quantity!='') { echo number_format($del_quantity, 0, '.', ''); } ?>" autocomplete="off" autofocus tabindex="<?php echo $i+1 ?>" onKeyUp="validate_required(quantity_text_<?php echo $i?>, 'The amount to deliver exceeds the balance, the entry will be cleared.' +'\n'+ 'Please enter a valid amount.', <?php echo $balance_quantity ?>); validate_stock(quantity_text_<?php echo $i?>, 'The amount exceeds the stock quantity, the entry will be cleared.' +'\n'+ 'Please enter a valid amount.', <?php echo $store_quantity ?>, <?php echo $i?> ); " onfocus = "this.select();" width="20px;" >
  &nbsp;&nbsp;<?php echo $unit ?></td>
             </tr>
             <?php
			  $i++;
             }
             ?>
           </table>
          <input type="hidden" name="line3" id="line3" value="<?php echo $mitek_num ?>">
          <br>
<br>
<?php if ($mitek_num==0) { ?><img src="../images/exclamation.png" width="24" height="24">There are no Eco products on this order.<?php } ?>
          </div>
      </td>
      </tr>
      <tr>
      <td><table width="590" height="50" align="center" 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','deliver_cash_saleWizbase.php?s=&amp;p=2');return document.MM_returnValue"  tabindex="<?php echo $i+3 ?>"/></td>
          <td width="115">&nbsp;</td>
          <td width="99"><input name="p" type="hidden" id="p" value="3" />
            <input name="s" type="hidden" id="s" value="3" />
            </span></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 $i+4 ?>" /></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 $i+2 ?>"/></td>
        </tr>
      </table></td>
      </tr>
      </table>
</form>
</div>
</body>      