<?php 
$table_name = $_SESSION['db_prefix']."_mworks_order_job_lines";
$items_query = "SELECT *  FROM $table_name WHERE job_card_no = '".$_SESSION['job_card_no']."' AND misc IS NULL";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);
?>

<script type="text/javascript">
function validate_required(input,alerttxt,maxvalue)
{
	
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, '', 'The delivery amount 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)
{
	
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, '', 'There is not enough stock, the entry will be cleared.<br><br>Please enter a valid amount.');
	input.value = "";
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<script>
function del_all() {
	alertMessage(1, '', 'Supply quantities are set to balance quantities,<br>you can still edit the supply quantities');
	i=0;
	while (i < 50) {
		document.getElementById('supply_text'+i ).value=document.getElementById('balance_text'+i).value;
	i++;
	}
	
}
</script>

<script>
function check_nul() {
i=0;	
while (i < document.getElementById('line_num').value) {
 if (document.getElementById('supply_text'+i).value > 0) {
        document.getElementById('min_del_text').value='yes';
  }
i++;
}

}
</script>
</head>

<?php include('../include/wait.php') ?>
<link href="../css/wait.css" rel="stylesheet" type="text/css">
<body onLoad="MM_showHideLayers('wait','','hide'); <?php if($_SESSION['stock_error']==1) { ?> alertMessage(1, '', 'Stock for the indicated boxes is not sufficient and is reduced to the available stock quantity.')<?php } ?>;" onFocus="MM_showHideLayers('wait','','hide');"  onBlur="MM_showHideLayers('wait','','hide');">

<div class="wizard_page_middle_frame">
<form action="m_supplyJobWizbase.php?p=3&s=2" method="post" name="form1" id="form1" >
<div class="wizard_frame">
<input name="button" type="button" class="imagebutton" id="button" onClick="del_all();" value="Deliver all" checked="checked" >
</div>

         <div class="wizard_frame">
         <table width="600" id="table_details_display">
  <tr>
    <td width="266">Item</td>
    <td width="46">Stock</td>
    <td width="59" align="center">Ordered</td>
    <td width="62" align="center">Delivered</td>
    <td width="52" align="center">Balance</td>
    <td width="87" align="center">Supply</td>
  </tr>
  <?php
  $i=0;
  while ($i<$items_num) {
	  $store_item_id = mysql_result($items_result, $i, "store_item_id");
	  $spec_name = mysql_result($items_result, $i, "item_name");
	  $spec_price = mysql_result($items_result, $i, "price");
	  $order_line_id = mysql_result($items_result, $i, "order_line_id"); 
	  $price='';
	  		if  (($store_item_id>0) AND ($store_item_id<100000)) {
	  			$table_name = $_SESSION['db_prefix']."_store_items";
				$name_query = "SELECT item_name, quantity, price, unit FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
				$name_result = mySQL_query($name_query);
				$item_name = mysql_result($name_result, 0, "item_name");
				$stock =  mysql_result($name_result, 0, "quantity");
				$price =  mysql_result($name_result, 0, "price");
				$unit =  mysql_result($name_result, 0, "unit");
			}
			if ($store_item_id>100000) {
	  			$table_name = $_SESSION['db_prefix']."_store_items_special";
				$name_query = "SELECT item_name, quantity, price, unit FROM $table_name WHERE special_store_item_id = '".$store_item_id."' AND ordered_for = '".$_SESSION['job_card_no']."' ";
				$name_result = mySQL_query($name_query);
				$item_name = mysql_result($name_result, 0, "item_name");
				$stock =  mysql_result($name_result, 0, "quantity");
				$price =  mysql_result($name_result, 0, "price");
				$unit =  mysql_result($name_result, 0, "unit");
			}
			
			 if ($store_item_id==0) {
					   $item_name = $spec_name;
					   $price = $spec_price;
					   $quantity = 0;
					   $stock = 0;
					 }
			
	  $quantity = mysql_result($items_result, $i, "quantity");
	  $delivered = mysql_result($items_result, $i, "delivered");
	  $balance = $quantity-$delivered;
	  
  
	  $table_name = $_SESSION['db_prefix']."_mworks_order_job_lines_supply_temp";
	  $supply_query = "SELECT quantity, error FROM $table_name WHERE  store_item_id = '".$store_item_id."' AND job_card_no = '".$_SESSION['job_card_no']."' AND user_name = '".$_SESSION['username']."' ";
	  $supply_result = mySQL_query($supply_query);
	  $supply_num = mysql_num_rows($supply_result);
	  if ($supply_num>0) {
	  $supply_qty = mysql_result($supply_result, 0, "quantity");
	  $error = mysql_result($supply_result, 0, "error");
	  }
	  else {
		$supply_qty = '';
		$error = 0;
	  }
	  
  ?>
  <tr>
    <td><?php echo $item_name ?>
      <input type="hidden" name="store_item_id_text<?php echo $i?>" id="store_item_id_text<?php echo $i?>" value="<?php echo $store_item_id ?>">
      <input type="hidden" name="store_item_name_text<?php echo $i?>" id="store_item_name_text<?php echo $i?>" value="<?php echo $item_name ?>"> <input type="hidden" name="price_text<?php echo $i?>" id="price_text<?php echo $i?>" value="<?php echo $price ?>"></td>
    <td align="center"><?php if ($unit!='no') {echo number_format($stock, 2, '.', ',');} else { echo number_format($stock, 0, '.', ','); } ?></td>
    <td align="center"><?php if ($unit!='no') {echo number_format($quantity, 2, '.', ',');} else { echo number_format($quantity, 0, '.', ','); } ?>      <input type="hidden" name="quantity_text<?php echo $i?>" id="quantity_text<?php echo $i?>" value="<?php echo $quantity ?>"><input name="order_line_id<?php echo $i?>" type="hidden" value="<?php echo $order_line_id ?>"></td>
    <td align="center"><?php if ($unit!='no') {echo number_format($delivered, 2, '.', ',');} else { echo number_format($delivered, 0, '.', ','); } ?>      <input type="hidden" name="delivered_text<?php echo $i?>" id="delivered_text<?php echo $i?>" value="<?php echo $delivered ?>"></td>
    <td align="center"><?php if ($unit!='no') {echo number_format($balance, 2, '.', ',');} else { echo number_format($balance, 0, '.', ','); } ?>      <input type="hidden" name="balance_text<?php echo $i?>" id="balance_text<?php echo $i?>" value="<?php echo $balance ?>"></td>
    <td align="left"><input type="text" name="supply_text<?php echo $i?>" id="supply_text<?php echo $i?>"  width="50px;" class="text40" style="text-align:right" onKeyUp="validate_required(supply_text<?php echo $i?>, 'The supply amount exceeds the balance, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $balance ?>)"  onBlur="validate_stock(supply_text<?php echo $i?>, 'There is not enough stock, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $stock ?>)" onKeyPress="<?php if ($unit!='no'){ ?>return isNumberKey(event)<?php } else { ?>return numbersonly(this, event)<?php } ?>" onfocus = "this.select();" autocomplete="off"  value="<?php  if ($supply_qty!='') {if ($unit!='no') {echo number_format($supply_qty, 2, '.', ',');} else { echo number_format($supply_qty, 0, '.', ','); } } ?>">
      <?php if ($error==1) { ?> <img src="../images/exclamation_mark_red_small.png" width="10" height="16"><?php } ?></td>
  </tr>
  <?php
  $i++;
  }
  ?>
  <input name="line_num" id="line_num" type="hidden" value="<?php echo $i ?>">
</table>

         
         </div>
        
        
        <div class="wizard_frame">
      <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','m_supplyJobWizbase.php?s=&amp;p=1');return document.MM_returnValue" /></td>
          <td width="115"><input type="hidden" name="min_del_text" id="min_del_text" value=""></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, '../store/store_main.php');" value="Cancel" /></td>
          <td width="110" align="right"><input type="submit" name="next_button" id="next_button" class="imagebutton" value="Next" onMouseDown="check_nul();"  onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); " /></td>
        </tr>
      </table>
      </div>
</form>
</div>
<!--form validation-->
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("min_del_text","req","at least quantity for one item.");
</script>
</body>      