<?php 
if ($_SESSION['branch']=='') {
?>
<script language="javascript" type="text/javascript">
          needToConfirm = false;
             window.location="store_no_return.php";
</script>
<?php
}
else {


 
    $table_name = $_SESSION['db_prefix']."_tile_orders";
		$quote_query = "SELECT *  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."'";
		$quote_result = mySQL_query($quote_query);
		$branch=mysql_result($quote_result, 0, "branch");	
		$direct=mysql_result($quote_result, 0, "direct");	
		$pallets=mysql_result($quote_result, 0, "pallets");
		$pallets_price=mysql_result($quote_result, 0, "pallets_price"); 
		$del_pallets=mysql_result($quote_result, 0, "del_pallets");
		
		$available_pallets = $pallets - $del_pallets;
				
		$table_name = $_SESSION['db_prefix']."_tile_order_lines";
		$line_query = "SELECT *  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."'";
		$line_result = mySQL_query($line_query);
		$line_num = mysql_num_rows($line_result);
		

?>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>  
<script type="text/javascript">

function validate_required(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, 'tile_receive.php', 'The quantity to deliver exceeds the quantity received,<br>The entry will be cleared.<br><br>Please enter a valid quantity.');
	input.value = "";
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<script type="text/javascript">
function validate_pallets(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, 'tile_receive.php', 'You are to supply more <br> pallets as deposit was paid for!');
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<script>
function del_all() {
	
	for (var i=0; i < 25; i++) {
		    document.getElementById('quantity_text'+i).value=document.getElementById('balance_text'+i).value;
			document.getElementById('quantity_text'+i).onchange;
	}
}
</script>

<script>
function pallets_all() {
	    document.getElementById('pallets_text').value=document.getElementById('balance_pallets').value;
}
</script>

</head>

<?php include('../include/wait.php') ?>

<body onLoad="MM_showHideLayers('wait','','hide');  " onFocus="MM_showHideLayers('wait','','hide');" >

<form id="form1" name="form1" method="get" action="deliver_tilesWizbase.php">
<div class="wizard_page_middle_frame">
<div class="wizard_frame" align="right">
  <input name="button" type="button" class="imagebutton" id="button" onClick="del_all(); " onMouseUp="pallets_all();" value="Deliver all" checked="checked" >
   
</div>
<div class="wizard_frame" >
<table width="650" id="table_display" >
           <tr>
             <td height="26" colspan="2" align="left" valign="top">Item</td>
             <td width="66" align="center" valign="top">Group</td>
             <td width="36" align="center" valign="top">Unit</td>
             <td width="54" align="center" valign="top">Ordered</td>
             <td width="56" align="center" valign="top">&nbsp;Delivered</td>
             <td width="58" align="center" valign="top">Balance</td>
             <td width="56" align="center" valign="top">Deliver</td>
           </tr>
           <?php
				$i=0;
				
				 while ($i<$line_num){
					 $a0 = mysql_result($line_result, $i, "order_line_id");
					 $a1 = mysql_result($line_result, $i, "store_item_id");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, unit, subCategory FROM $table_name WHERE store_item_id = '".$a1."' ";
						$item_result = mySQL_query($item_query);
						$a1a = mysql_result($item_result, 0, "item_name");
						$a3a = mysql_result($item_result, 0, "unit");
						$a4a = mysql_result($item_result, 0, "subCategory");
					if ($direct=='yes') {	
					       $a2 = mysql_result($line_result, $i, "quantity") - mysql_result($line_result, $i, "del_quantity");
					}
					else {
						$a2 = mysql_result($line_result, $i, "rec_quantity") - mysql_result($line_result, $i, "del_quantity");
					}
					 $a7 = mysql_result($line_result, $i, "rec_quantity");
					 $a6 = mysql_result($line_result, $i, "quantity");
					 $a5 = mysql_result($line_result, $i, "del_quantity");
					 $a10 = mysql_result($line_result, $i, "price");
					 $a11 = mysql_result($line_result, $i, "purchase_price");
					 
					$table_name = $_SESSION['db_prefix']."_tile_deliver_lines_temp";
					$quantity_query = "SELECT quantity  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."' AND store_item_id = '".$a1."' AND user_name = '".$_SESSION['username']."'"; 
					$quantity_result = mySQL_query($quantity_query);
					$quantity_num = mysql_num_rows($quantity_result);
					if ( $quantity_num> 0) {
					        $a8 = mysql_result($quantity_result, 0, "quantity");
					}
					else { 
					$a8 = '';
					}
				
					 
				 ?>
           <tr>
             <td colspan="2" valign="middle"><?php echo $a1a ?>
             <input type="hidden" name="store_item_id<?php echo $i?>" id="store_item_id<?php echo $i?>" value="<?php echo $a1 ?>">
             <input type="hidden" name="price_text<?php echo $i?>" id="price_text<?php echo $i?>" value="<?php echo $a10 ?>">
             <input type="hidden" name="purchase_price_text<?php echo $i?>" id="purchase_price_text<?php echo $i?>" value="<?php echo $a11 ?>"></td>
             <td valign="middle"><?php echo $a4a ?></td>
             <td valign="middle"><?php echo $a3a ?></td>
             <td align="right" valign="middle"><?php echo $a6 ?></td>
             <td align="right" valign="middle"><?php echo $a5 ?></td>
             <td align="right" valign="middle"><?php $a7=$a6-$a5;  echo $a7; ?>
             <input name="balance_text<?php echo $i ?>" id="balance_text<?php echo $i?>" type="hidden" value="<?php echo $a7 ?>"></td>
             <td align="right" valign="middle"><input name="quantity_text<?php echo $i ?>" type="text" class="text60" id="quantity_text<?php echo $i ?>"  onkeypress="return numbersonly(this, event)" onKeyUp="validate_required(quantity_text<?php echo $i?>, 'The delivery amount exceeds the balance, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $a7 ?>);"  value="<?php echo $a8 ?>" autocomplete="off" autofocus onfocus = "this.select();" style="text-align:right" ></td>
           </tr>
           <?php 
			   // $a6_total=$a6_total+$a6;
				$i++;
				 }
				 ?>
           <?php 
					$table_name = $_SESSION['db_prefix']."_tile_deliver_lines_temp";
					$quantity_query = "SELECT pallets  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."' AND pallets > 0 AND user_name = '".$_SESSION['username']."'"; 
					$quantity_result = mySQL_query($quantity_query);
					$quantity_num = mysql_num_rows($quantity_result);
					if ( $quantity_num> 0) {
					        $a9 = mysql_result($quantity_result, 0, "pallets");
					}
					else { 
					$a9 = '';
					}
	?>          
           <tr>
             <td colspan="2" align="left" valign="middle">
             Pallets</td>
             <td align="left" valign="middle">&nbsp;</td>
             <td align="left" valign="middle">&nbsp;</td>
             <td align="right" valign="middle"><?php echo $pallets ?></td>
             <td align="right" valign="middle"><?php echo $del_pallets ?></td>
             <td align="right" valign="middle"><?php if ($pallets!=0) { $bal_pallets = $pallets - $del_pallets;} else {$bal_pallets=0;} echo $bal_pallets ?>
             <input name="balance_pallets" id="balance_pallets" type="hidden" value="<?php echo $bal_pallets ?>"></td>
             <td align="right" valign="middle"><input type="text" name="pallets_text" id="pallets_text"  class="text50" value="<?php echo $a9 ?>" onfocus = "this.select();" onKeyPress="return numbersonly(this, event)" onKeyUp="validate_pallets(pallets_text, '', <?php echo $bal_pallets ?>)" style="width:50px; text-align:right" autocomplete="off"></td>
           </tr>
         </table>
</div>
<div class="wizard_frame">
				<input name="s" type="hidden" id="s" value="2" />
              	<input name="p" type="hidden" id="p" value="2" />
                <input name="line_num"  id="line_num" type="hidden" value="<?php echo $line_num ?>">
      <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_tilesWizbase.php?s=&p=1');return document.MM_returnValue" /></td>
            <td width="115">&nbsp;</td>
            <td width="99">&nbsp;</td>
            <td width="125"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="javascript:cancel_confirmation(1, '../store/store_main.php?date=0&branch=&subCatSelect=Tiles'); needToConfirm = false;" value="Cancel" /></td>
            <td width="110"><input type="submit" name="next_button" id="next_button" class="imagebutton" value="Next" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');" /></td>
          </tr>
    </table>
  </div>

</form>

</body>

<?php 
}
?>