<!--
 * this file is to be used in combination with store_item_select_header_no_price.php
 * the temp table name needs to be declared in the base file
 * for error reporting.
 *
-->
<script>
function enable_button() {
	document.getElementById('add_line_button').hidden=false;
	document.getElementById('dis_add_line_button').hidden=true;
}
</script>

<div class="wizard_frame" style="height:auto;" ><br>
    <table id="dataTable" width="670" border="1" style="border-collapse:collapse;">
    <?php
	
	   if ($line_num==0) {
	   	   $a1='';
		   $a1a='-';
		   $a1b='';
		   $a2='-';
		   $a3='-';
		   $error='';
	   }
	   else {
		        $a1 = mysql_result($line_result, 0, "store_item_id");
		   }
	
	if ($a1!='') {
	  $table_name = $_SESSION['db_prefix']."_store_items";
	  $name_query = "SELECT item_name, subCategory FROM $table_name WHERE store_item_id = '".$a1."'  ";
	  echo '<br>*****'.$name_query;
	  $name_result = mySQL_query($name_query);
	  $a1a = mysql_result($name_result, 0, "item_name");
	  $subCategory = mysql_result($name_result, 0, "subCategory"); 
	  if ($subCategory=='Tiles') {$a1b = 'no';}
	  if ($subCategory=='Accessories') {$a1b = 'no';}
	  if ($subCategory=='Consumables') {$a1b = 'kg';}	
		
	  $table_name = $temp_table_name;
	  $supply_query = "SELECT quantity, error FROM $table_name WHERE  store_item_id = '".$a1."' AND user_name = '".$_SESSION['username']."' ";
	  $supply_result = mySQL_query($supply_query);
	  $supply_num = mysql_num_rows($supply_result);
	  if ($supply_num>0) {
	  $a2 = mysql_result($supply_result, 0, "quantity");
	  $error = mysql_result($supply_result, 0, "error");
	  }
	  else {
		$a2 = '0';
		$error = 0;
	  }	   
	}	   
		   
		   
		?>
    <thead> 
        <tr>
          <td>&nbsp;</td>
          <td width="549">Store item</td>
          <td>Quantity</td>
          </tr>
        </thead>
        <script>
		
			 function check_it() {
				 if (document.getElementById('line_checkbox0').checked) {
                         document.getElementById('store_item_id_select0').style.background = '#F0F0F0';
						 document.getElementById('store_item_text0').style.background = '#F0F0F0';
						 document.getElementById('quantity_text0').style.background = '#F0F0F0';
						 document.getElementById('store_item_id_select0').selectedIndex=1;
						 document.getElementById('store_item_text0').value = '.';
						 document.getElementById('quantity_text0').value = '.';
						 document.getElementById('store_item_id_select0').disabled = true;
						 document.getElementById('store_item_text0').disabled = true;
						 document.getElementById('quantity_text0').disabled = true;
						 document.getElementById('add_icon0').hidden = true;
						 
                } else {
                         document.getElementById('store_item_id_select0').style.background = '#FFF';
						 document.getElementById('quantity_text0').style.background = '#FFF';
						 document.getElementById('store_item_id_select0').selectedIndex=0;
						 document.getElementById('store_item_text0').value = '';
						 document.getElementById('quantity_text0').value = '';
						 document.getElementById('store_item_id_select0').disabled = false;
						 document.getElementById('quantity_text0').disabled = false;
						 document.getElementById('add_icon0').hidden = false;
                }
				}
		     </script>
        <tr>
            <td width="24"><input type="checkbox" name="line_checkbox0" id="line_checkbox0" onMouseUp="check_it()" <?php if ($a1!='') { ?> checked="checked" <?php } ?> /></td>
          <td>
          
          
          <div id=store_item_divA0>
          <img src=<?php if ($a1a!='') { echo "../images/line_edit.png width=16 height=12 "; } else { echo "../images/line_edit_text.png width=135 height=12 "; } ?> 
          title="Click on line to select and enter first line."   
          id="add_icon0" 
          onClick="
          
          document.getElementById('add_line_button').hidden=false;
          document.getElementById('dis_add_line_button').hidden=true;
          document.getElementById('store_item_divA0').hidden=true; 
          document.getElementById('store_item_divB0').hidden=false; 
          document.getElementById('quantity_text0').value=''; 
          document.getElementById('store_item_text0').value ='';
          document.getElementById('store_item_id0').value ='';
          "
          >
          
<input name="store_item_text0" id="store_item_text0" type="text" class="text300" value="<?php echo $a1a ?>" readonly style="border:hidden; background-color:inherit; ">
          </div>
          <div id=store_item_divB0 hidden="hidden">
            <select name="category0" id="category0"  class="select160"  onChange="document.getElementById('store_item_divA0').hidden=true; document.getElementById('store_item_divB0').hidden=false; document.getElementById('store_item_id_select0').selectedIndex=-1; document.getElementById('index0').value = document.getElementById('category0').selectedIndex; ">
            </select>
            <input type="hidden" name="index0" id="index0">
            <select name="store_item_id_select0" id="store_item_id_select0" class="select250" onChange="
              document.getElementById('line_checkbox0').checked=true;
              unit = document.getElementById('store_item_id_select0').options[document.getElementById('store_item_id_select0').selectedIndex].text; 
              unit2 = unit.split('~'); 
              unit3 = unit2[1];
              if (unit3=='no') {
              document.getElementById('quantity_text0').value = '';
              document.getElementById('quantity_text0').onkeypress = function num(event) { return numbersonly(this, event);}
              }
              else {
              document.getElementById('quantity_text0').value = '';
              document.getElementById('quantity_text0').onkeypress = function decnum(event) { return isNumberKey(event);}  
              }            
              document.getElementById('store_item_id0').value=document.getElementById('store_item_id_select0').value; 
              document.getElementById('store_item_text0').value = document.getElementById('store_item_id_select0').value; 
              "
              >
            </select>
            <input type="hidden" name="line_num" id="line_num" value="1">
            <input name="store_item_id0" id="store_item_id0" type="hidden" value="<?php echo $a1 ?>" />
          </div></td>
          <td width="75" align="left"><input  name="quantity_text0" type="text" class="text60" id="quantity_text0" style="text-align:right" onKeyPress="<?php if ($a1b!='no'){ ?>return isNumberKey(event)<?php } else { ?>return numbersonly(this, event)<?php } ?>" value="<?php if (($a2!='0') AND ($a2!='-'))  { if ($a1b!='no') {echo number_format($a2, 2, '.', '');} else { echo number_format($a2, 0, '.', ''); }}  else { if ($a2=='-') { echo'-'; }}?>" size="10" autocomplete="off" onfocus = "this.select();"/>
            <?php if ($error==1) { ?> <img src="../images/exclamation_mark_red_small.png" width="10" height="16"><?php } ?>  
            </td>
            <!--<input name="textfield0" type="text" id="textfield0" size="5" value="1"></td>-->
        </tr>
        <?php
		$i=1;
		while ($i<$line_num) {
		   $a1 = mysql_result($line_result, $i, "store_item_id");
		   		
		   				$table_name = $_SESSION['db_prefix']."_store_items";
						$name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$a1."'  ";
						$name_result = mySQL_query($name_query);
						$a1a = mysql_result($name_result, 0, "item_name");
						$a1b = mysql_result($name_result, 0, "unit");
						$a2 = mysql_result($line_result, 0, "quantity");
		   				$a3 = mysql_result($line_result, 0, "price");
				
		   
	  $table_name = $temp_table_name;
	  $supply_query = "SELECT quantity, error FROM $table_name WHERE  store_item_id = '".$a1."' AND user_name = '".$_SESSION['username']."' ";
	  $supply_result = mySQL_query($supply_query);
	  $supply_num = mysql_num_rows($supply_result);
	  if ($supply_num>0) {
	  $a2 = mysql_result($supply_result, 0, "quantity");
	  $error = mysql_result($supply_result, 0, "error");
	  }
	  else {
		$a1 = '';  
		$a2 = '';
		$error = 0;
	  }	   
		   
		   
		?>
        <tr>
            <td width="24"><input type="checkbox" name="line_checkbox<?php echo $i ?>" id="line_checkbox<?php echo $i ?>"  <?php if ($a1!='') { ?> checked="checked" <?php } ?> onMouseUp="{
				 if (document.getElementById('line_checkbox<?php echo $i ?>').checked) {
                         document.getElementById('store_item_id_select<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('quantity_text<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('store_item_id_select<?php echo $i ?>').selectedIndex=1;
                         document.getElementById('store_item_text<?php echo $i ?>').value = '.';
						 document.getElementById('quantity_text<?php echo $i ?>').value = '.';
						 document.getElementById('store_item_id_select<?php echo $i ?>').disabled = true;
                         document.getElementById('store_item_text<?php echo $i ?>').disabled = true;
						 document.getElementById('quantity_text<?php echo $i ?>').disabled = true;
						 document.getElementById('add_icon<?php echo $i ?>').hidden = true;
					} else {
                        document.getElementById('store_item_id_select<?php echo $i ?>').style.background = '#FFF';
						 document.getElementById('quantity_text<?php echo $i ?>').style.background = '#FFF';
						 document.getElementById('store_item_id_select<?php echo $i ?>').selectedIndex=1;
                         document.getElementById('store_item_text<?php echo $i ?>').value = '';
						 document.getElementById('quantity_text<?php echo $i ?>').value = '';
						 document.getElementById('store_item_id_select<?php echo $i ?>').disabled = false;
						 document.getElementById('quantity_text<?php echo $i ?>').disabled = false;
						 document.getElementById('add_icon<?php echo $i ?>').hidden = false;
                }
				}
                "/></td>
          <td>
          
          
          <div id=store_item_divA<?php echo $i ?>>
              <img src="../images/line_edit.png" width="16" height="12" id="add_icon<?php echo $i ?>" title="Click to edit line" 
              onClick="
              document.getElementById('store_item_divA<?php echo $i ?>').hidden=true; 
              document.getElementById('store_item_divB<?php echo $i ?>').hidden=false; 
              document.getElementById('quantity_text<?php echo $i ?>').value=''; 
              document.getElementById('store_item_text<?php echo $i ?>').value =''; 
              ">
              
<input name="store_item_text<?php echo $i ?>" id="store_item_text<?php echo $i ?>" type="text" class="text300" value="<?php echo $a1a ?>" readonly style="border:hidden; background-color:inherit"> <input name="store_item_id<?php echo $i ?>" id="store_item_id<?php echo $i ?>" type="hidden" value="<?php echo $a1 ?>">
          </div>
          <div id=store_item_divB<?php echo $i ?> hidden="hidden">
            <select name="category<?php echo $i ?>" id="category<?php echo $i ?>" class="select160"  onChange="document.getElementById('store_item_divA<?php echo $i ?>').hidden=true; document.getElementById('store_item_divB<?php echo $i ?>').hidden=false; document.getElementById('store_item_id_select<?php echo $i ?>').selectedIndex=-1; document.getElementById('index<?php echo $i ?>').value = document.getElementById('category<?php echo $i ?>').selectedIndex; ">
            </select>
            <input type="hidden" name="index<?php echo $i ?>" id="index<?php echo $i ?>">
            <select name="store_item_id_select<?php echo $i ?>" id="store_item_id_select<?php echo $i ?>" class="select250" onChange="
              document.getElementById('line_checkbox<?php echo $i ?>').checked=true;
              unit = document.getElementById('store_item_id_select<?php echo $i ?>').options[document.getElementById('store_item_id_select<?php echo $i ?>').selectedIndex].text; 
              unit2 = unit.split('~'); 
              unit3 = unit2[1];
              if (unit3=='no') {
              document.getElementById('quantity_text<?php echo $i ?>').value = '';
              document.getElementById('quantity_text<?php echo $i ?>').onkeypress = function num(event) { return numbersonly(this, event);}
              }
              else {
              document.getElementById('quantity_text<?php echo $i ?>').value = '';
              document.getElementById('quantity_text<?php echo $i ?>').onkeypress = function decnum(event) { return isNumberKey(event);}  
              }            
              document.getElementById('store_item_id<?php echo $i ?>').value=document.getElementById('store_item_id_select<?php echo $i ?>').value; 
              document.getElementById('store_item_text<?php echo $i ?>').value = document.getElementById('store_item_id_select<?php echo $i ?>').value; 
              "
              >
            </select>
            <input type="hidden" name="line_num" id="line_num" value="<?php echo $line_num?>">
          </div></td>
          <td width="75" align="left"><input  name="quantity_text<?php echo $i ?>" type="text" class="text60" id="quantity_text<?php echo $i ?>" style="text-align:right" onKeyPress="<?php if ($a1b!='no'){ ?>return isNumberKey(event)<?php } else { ?>return numbersonly(this, event)<?php } ?>" value="<?php if ($a2!='0') { if ($a1b!='no') {echo number_format($a2, 2, '.', '');} else { echo number_format($a2, 0, '.', ''); } }?>" size="10" autocomplete="off" onfocus = "this.select();"/>
          <?php if ($error==1) { ?> <img src="../images/exclamation_mark_red_small.png" width="10" height="16"><?php } ?></td>
        </tr>
        <?php
		$i++;
		}
		?>
        
  </table>
   <br />
                  
               
    <input type="button" class="imagebutton" onClick="addRow('dataTable')" value="Add line" name="add_line_button" id="add_line_button" hidden="true" />
    <input type="button" class="dis_imagebutton" value="Add line" name="dis_add_line_button" id="dis_add_line_button"  />
    <div align="right"><label class="label_small">To delete a line: uncheck line box</label></div>
	<?php
	if ($line_num!=0) {
		?>
	<script>
		enable_button();
	</script>
    <?php
	}
	?>
  </div>