<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/main.css" />

<?php
include('../include/session.php');	

$table_name = $_SESSION['db_prefix']."_captured_mitek";
$mitek_query = "SELECT * FROM $table_name WHERE cap_job = '".$_SESSION['job_no']."' ";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);

$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
$check_query = "SELECT supply_id FROM $table_name WHERE truss_type = 'Mitek' AND job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' "; 
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);

$i=0;
while ($i<$check_num) {
	$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
	$delete_query = "DELETE FROM $table_name WHERE truss_type = 'Mitek' AND job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' AND extra IS NULL ";
	execsql($delete_query);
	$i++;
}

$table_name = $_SESSION['db_prefix']."_supply_customer_temp";
$extra_query = "SELECT store_item_id, quantity, notes_text  FROM $table_name WHERE truss_type = 'Mitek' AND job_no = '".$_SESSION['job_no']."' AND user =  '".$_SESSION['username']."' AND extra = 'yes' ";
$extra_result = mySQL_query($extra_query);
$extra_num = mysql_num_rows($extra_result);


$table_name = $_SESSION['db_prefix']."_store_items";
$dropDownQuery = "SELECT store_item_id, item_name, unit FROM $table_name WHERE  subCategory = 'Mitek' AND class <> 'Plate'  ORDER BY item_name ASC";
$dropDownResult = execsql($dropDownQuery);
	$num = mysql_num_rows($dropDownResult);
	$a = 0;
	$arrId = array ();
	$arrName = array ();
	while ($a < $num) {
		$arrId[$a] = mysql_result($dropDownResult, $a, "store_item_id");
		$arrName[$a] = mysql_result($dropDownResult, $a, "item_name");
		$arrUnit[$a] = mysql_result($dropDownResult, $a, "unit");
		
		$a++;
	}
?>



<link rel="stylesheet" href="../window/dhtmlwindow.css" type="text/css" />
<link rel="stylesheet" href="../window/modal.css" type="text/css" />
<script type="text/javascript" src="../window/dhtmlwindow.js"></script>
<script type="text/javascript" src="../window/modal.js"></script>

<script language="JavaScript" src="../jscript/gen_validatorv4_2.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/go_to_url.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(2, 'variation_mitek.php', 'The quantity to de-authorise is higher than the outstanding authorised quantity.<br><br> Enter a quantity lower than the outstanding authorised quantity.');
	input.value = maxvalue;
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>
</head>
<body style="width:675px;" onload="alertMessage(1, '', 'Variations need to be applied before delivering. Any values for delivery entered already are cleared.');">
<form action="../store/deliverJobWizbase.php" method="get" name="form1" target="_parent">

<div class="scroll_frame_medium" style="height:250px; width:650px;">
<label class="label_std">Change authorised quantities.</label>
<table width="627" id="table_details_display" >
  <tr>
    <td align="left" valign="top"><label class="label_std">Mitek products:</label></td>
    <td align="left" valign="top"><label class="label_std">Stock:</label></td>
    <td colspan="2" align="center" valign="top"><label class="label_std">de-Authorise to</label>
      <input name="edit_job_no" type="hidden" value="<?php echo $_SESSION['job_no']?>" /></td>
    <td align="center" valign="top"><label class="label_std">
      <label class="label_std">Reason to de-Authorise </label>
      </label></td>
  </tr>
  <?php 
				  $i=0;
				  
				  while ($i<$mitek_num) { 
				        $reason_text = '-';
					    $store_item_id = mysql_result($mitek_result, $i, "cap_item_id");										
						$auth_quantity =( mysql_result($mitek_result, $i, "auth_quantity")-mysql_result($mitek_result, $i, "delivered")-mysql_result($mitek_result, $i, "de_authorised"));
						
						$table_name = $_SESSION['db_prefix']."_variations_temp";
						$variations_query = "SELECT de_auth_quantity, notes_text FROM $table_name WHERE job_no = '".$_SESSION['job_no']."' AND store_item_id = '".$store_item_id."' AND user =  '".$_SESSION['username']."'  ";
						$variations_result = mySQL_query($variations_query);
						$variations_num = mysql_num_rows($variations_result);
						if ($variations_num>0){
						$de_auth = mysql_result($variations_result, 0, "de_auth_quantity");	
						$reason_text = mysql_result($variations_result, 0, "notes_text");
						}
						else {
						$de_auth=0;
						$reason_text='-';
						}
						$to_auth_quantity = $auth_quantity-$de_auth;
						
						
						
						$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"); 
						$quantity = mysql_result($item_name_result, 0, "quantity"); 
						$_SESSION['auth_item_name'.$i] = $item_name;
				 ?>
  <tr>
    <td width="143"><?php echo $_SESSION['auth_item_name'.$i] ?>
      <input name="cap_item_id<?php echo $i?>" id="cap_item_id<?php echo $i?>" type="hidden" value="<?php echo $store_item_id ?>" /></td>
    <td width="98"><?php echo $quantity ?>&nbsp;</td>
    <td width="101" align="right"><input name="mitek_edit<?php echo $i?>" type="text" class="text60" id="mitek_edit<?php echo $i?>"  onfocus = "this.select();" style="text-align:right;" onkeyup="validate_required(mitek_edit<?php echo $i?>, 'The entered amount exceeds the outstanding authorised quantity, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $auth_quantity ?>)" onkeypress="return numbersonly(this, event)" autocomplete="off"  value="<?php echo $to_auth_quantity ?>" onchange=" document.getElementById('reason_text<?php echo $i?>').value = '';"/></td>
    <td width="56" align="center"><?php echo $unit?></td>
    <td width="205" align="center"><label for="reason_text"></label>
      <input name="reason_text<?php echo $i?>" type="text" class="text200" id="reason_text<?php echo $i?>" maxlength="50" value="<?php echo $reason_text?>" /></td>
  </tr>
  <?php
						
			  $i++;
             }
			 $list = $i;
			 ?>
  <input name="mitek_num" type="hidden" value="<?php echo $i;?>" />
</table>
</div>



<div class="scroll_frame_medium" style="height:200px; width:650px;">
<label class="label_std">Authorise new items.</label>
<table width="627" id="table_details_display" >
  <?php
  
  $k=0;
  while ($k<$extra_num) { 
    //$quantity='-';
//	$notes_text='-';
	$store_item_id =mysql_result($extra_result, $k, "store_item_id");
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$name_num = mysql_num_rows($name_result);
		if ($name_num!=0) {
		$item_name = mysql_result($name_result, 0, "item_name");
		}
		else {
			$item_name='';
		}
    $quantity =mysql_result($extra_result, $k, "quantity");
	$notes_text =mysql_result($extra_result, $k, "notes_text");
	?>
    <tr>
    <td><input name="store_item_text_<?php echo $k?>" type="text" class="text200" id="store_item_text_<?php echo $k?>"  value="<?php echo $item_name ?>"/><input name="store_item_select_<?php echo $k?>" type="hidden" value="<?php echo $store_item_id?>" /></td>
    <td><input name="new_qty_text_<?php echo $k?>" type="text" class="text50" id="new_qty_text_<?php echo $k?>" value="<?php echo $quantity ?>" onkeypress="return numbersonly(this, event)"/></td>
    <td><input name="new_reason_text_<?php echo $k?>" type="text" class="text200" id="new_reason_text_<?php echo $k?>"  value="<?php echo $notes_text ?>"/></td>
    </tr>
    <?php
	$k++;
  }?>
  <?php
  $store_item_id = '-';
  $quantity ='-';
  $notes_text ='-';
  while ($k<(2+$extra_num)) {
	  ?>
  <tr>
    <td>
      <select name="store_item_select_<?php echo $k?>" id="store_item_select_<?php echo $k?>" style="width:250px;" onchange=" document.getElementById('new_reason_text_<?php echo $k?>').value = ''; document.getElementById('new_qty_text_<?php echo $k?>').value = '';" >
        <option value=" " selected="selected"> </option>
        <?php
    	for ($v = 0; $v < count($arrId); $v++) {
		echo "<option value=" . "\"" . $arrId[$v] . "\"" . ">" . $arrName[$v] . "</option>";
		$_SESSION['extra_item_name'.$k] = $arrName[$v];
		//foreach($arrId as $key=>$value){
			//echo "<option value=\"delivery.php?store_item_id=".$arrId[$i]."&cat=$cat&formEnabled2=1&date=$date" . "\"" . ">" . $arrName[$i] . "</option>";	
	    }
     ?>
      </select>
    </td>
    <td><input name="new_qty_text_<?php echo $k?>" type="text" class="text50" id="new_qty_text_<?php echo $k?>" value="<?php echo $quantity ?>" onkeypress="return numbersonly(this, event)"/></td>
    <td><input name="new_reason_text_<?php echo $k?>" type="text" class="text200" id="new_reason_text_<?php echo $k?>"  value="<?php echo $notes_text ?>"/></td>
    </tr>
  <?php 
  $k++;
  $list2 = $k+$extra_num;
		}
		?>
   <input name="list2" type="hidden" value="<?php echo $k ?>" />     
</table>
<br />
<br />
<br />
</div>
<div class="page_inside_frame">
<input name="save_button" type="submit" class="imagebutton" value="Save &amp; close" />&nbsp;&nbsp;&nbsp;&nbsp;<input name="cancel_button" type="button" class="imagebutton" onclick="MM_goToURL('parent','../store/deliverJobWizbase.php?p=4&s=&all=');return document.MM_returnValue" value="Cancel" />
<input type="hidden" name="p" id="p" value="4"/>
<input type="hidden" name="s" id="s" value="10"/>
<input type="hidden" name="all" id="all" value=""/>
</div>
</form>
<!--form validation-->
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  </script>
  <?php 
  $j=0;
  while ($j<$list) { 
  ?> 
  <script language="JavaScript" type="text/javascript" xml:space="preserve">
  frmvalidator.addValidation("reason_text<?php echo $j?>","req","Please enter a reason for changing authorisation for <?php echo $_SESSION['auth_item_name'.$j]?>");
  </script>
  <?php 
  $j++;
  }
  ?>  
  <?php 
  $m=0;
  while ($m<$list2) { 
  ?> 
  <script language="JavaScript" type="text/javascript" xml:space="preserve">
  frmvalidator.addValidation("new_qty_text_<?php echo $m?>","req","Please enter a quantity for adding authorisation for <?php echo $_SESSION['extra_item_name'.$m]?>");
  frmvalidator.addValidation("new_reason_text_<?php echo $m?>","req","Please enter a reason for adding authorisation for <?php echo $_SESSION['extra_item_name'.$m]?>");
  </script>
  <?php 
  $m++;
  }
  ?>  

</body>
</html>