<head>
<?php


$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$item_query = "SELECT store_item_id, quantity, authorised, delivered FROM $table_name WHERE quote_no = '".$_SESSION['job_no']."' ";
$item_result = mySQL_query($item_query);
$item_num = mysql_num_rows($item_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'); $mainwarning=0; ?>
<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_saleWiz6.php" method="get" name="form1" id="form1">
        <div class="scroll_frame_medium" style="height:340px; width:640px;">
          <table width="636" id="table_display">
               <tr>
                 <td width="242">Item</td>
                 <td width="56">Stock</td>
                 <td>Authorised</td>
                 <td align="center"><label class="label_std">To deliver</label></td>
                 <td>&nbsp;</td>
               </tr>
             <?php 
				  $i=0;
				  
				  while ($i<$item_num) { 
				       
						$store_item_id = mysql_result($item_result, $i, "store_item_id");
						//$quantity = mysql_result($item_result, $i, "quantity");
						$auth_quantity = mysql_result($item_result, $i, "authorised");
						//$delivered_quantity = mysql_result($item_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");
												
						$del_quantity = $auth_quantity;
						
						if ($del_quantity>$store_quantity) {
							  $del_quantity=$store_quantity;
							  $warning=1;
							  $mainwarning=1;
						}
						else {
							$warning=0;
						}
						
						//$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 height="27" valign="middle"><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 valign="middle"><?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="66" valign="middle">&nbsp;&nbsp;<?php echo number_format($auth_quantity, 0, '.', '')?></td>
               <td width="56" valign="middle">&nbsp;&nbsp;
               <input type="text" name="quantity<?php echo $i ?>" id="quantity<?php echo $i ?>" value="<?php echo number_format($del_quantity, 0, '.', '').' '.$unit ?>" style="width:40px; border:hidden;"></td>
               <td width="192" valign="middle"><?php if ($warning==1) { ?><img src="../images/exclamation.png" width="16" height="16">Not enough stock. <a href="javascript:void(0)" onMouseover="ddrivetip('There is not enough stock to deliver all authorised. The quantity to deliver is adjusted.', 150)"; onMouseout="hideddrivetip();"><img src="../images/Info.png" width="16" height="16" style=" cursor:help;"></a><?php } ?></td>
             </tr>
             <?php
			  $i++;
             }
             ?>
           </table>
          <input type="hidden" name="line2" id="line2" value="<?php echo $item_num ?>">
          <br>
<br>
        </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=1');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="2" />
            <input name="s" type="hidden" id="s" value="2" />
            </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  name="finish_button" type="submit" class="imagebutton" id="finish_button" value="Finish" onClick="needToConfirm = false;MM_showHideLayers('wait','','show'); "  /></td>
        </tr>
      </table>
</form>
<?php 
if ($mainwarning==1) { ?>
<script> alertMessage(2, '', 'For some items there is not enough stock.<br>Quantity to deliver has been adjusted.'); </script>
<?php } ?>
</div>
</body>      