<!--jscript-->
<script language="javascript" src="../jscript/chainedselects.js"></script>
<script language="javascript" src="../jscript/jquery.1.4.2.js"></script>
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script>  

<?php
	
$table_name = $_SESSION['db_prefix']."_store_items";
$store_item_query = "SELECT store_item_id, item_name, unit FROM $table_name WHERE subCategory = 'Minerals' ORDER BY item_name ASC";
	$store_item_result = $con->query($store_item_query);
	$store_item_num = mysqli_num_rows($store_item_result);
	$w = 0;
	$arrId3 = array ();
	$arrName3 = array ();
	while ($w < $store_item_num) {
		$row = mysqli_fetch_assoc($store_item_result);
		$arrId3[$w] = $row['store_item_id'];
		$arrName3[$w] = $row['item_name'].'  ('.$row['unit'];
		$w++;
	}
	
$table_name = $_SESSION['db_prefix']."_store_items";
$store_item_query = "SELECT store_item_id, item_name, unit FROM $table_name WHERE subCategory = 'Ingredients' ORDER BY item_name ASC";
	$store_item_result = $con->query($store_item_query);
	$store_item_num = mysqli_num_rows($store_item_result);
	$w = 0;
	$arrId5 = array ();
	$arrName5 = array ();
	while ($w < $store_item_num) {
		$row = mysqli_fetch_assoc($store_item_result);
		$arrId5[$w] = $row['store_item_id'];
		$arrName5[$w] = $row['item_name'].'  ('.$row['unit'];
		$w++;
	}
	
$table_name = $_SESSION['db_prefix']."_store_items";
$store_item_query = "SELECT store_item_id, item_name, unit FROM $table_name WHERE subCategory = 'Salt' ORDER BY item_name ASC";
	$store_item_result = $con->query($store_item_query);
	$store_item_num = mysqli_num_rows($store_item_result);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $store_item_num) {
		$row = mysqli_fetch_assoc($store_item_result);
		$arrId4[$w] = $row['store_item_id'];
		$arrName4[$w] = $row['item_name'].'  ('.$row['unit'];
		$w++;
	}
	
			
	
?>
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>

<script>
//var disable_empty_list=true; //uncomment this line to disable empty selection lists

<?php 
$k=0;
while ( $k<10 ) {
	$_SESSION['category'.$k]='';
$k++;
}
?>


<?php 
$k=0;
while ( $k<10 ) {
?>

addListGroup("Direct materials", "category<?php echo $k ?>", "store_item_id_select<?php echo $k ?>", "items");
//addOption("category0", "", "", "", 1); //Empty starter option
addList("category<?php echo $k ?>", "<?php echo $_SESSION['category'.$k]?>", "<?php echo $_SESSION['category'.$k]?>", "");
addList("category<?php echo $k ?>", "Salt", "Salt", "Salt");

addList("category<?php echo $k ?>", "Ingredients", "Ingredients", "Ingredients");
addList("category<?php echo $k ?>", "Minerals", "Minerals", "Minerals");
//addList("category<?php echo $k ?>", "Other", "Other", "Other");

<?php
$k++;
}
?>

		addList("Ingredients", "", "", "");
		<?php 
    	for ($w = 0; $w < count($arrId5); $w++) { ?>
			addList("Ingredients", "<?php echo $arrName5[$w]?> ", "<?php echo $arrId5[$w]?> ", "");
		<?php
	    }
     	?>
					
		addList("Salt", "", "", "");
		<?php 
    	for ($w = 0; $w < count($arrId4); $w++) { ?>
			addList("Salt", "<?php echo $arrName4[$w]?> ", "<?php echo $arrId4[$w]?> ", "");
		<?php
	    }
     	?>
		
		addList("Minerals", "", "", "");
		<?php 
    	for ($w = 0; $w < count($arrId3); $w++) { ?>
			addList("Minerals", "<?php echo $arrName3[$w]?> ", "<?php echo $arrId3[$w]?> ", "");
		<?php
	    }
     	?>
		
		
		
</script>


<script language="javascript">
function addRow(dataTable) {
 
            var table = document.getElementById(dataTable);
 
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
			var line_no = rowCount-1;
 
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name="line_checkbox"+line_no;
			element1.id="line_checkbox"+line_no;
			element1.onchange = function() {if (document.getElementById("line_checkbox"+line_no).checked) {document.getElementById("store_item_id"+line_no).value=''; } else {document.getElementById("store_item_id"+line_no).value='---';} }
			
			/*element1.checked="checked";*/
            cell1.appendChild(element1);
			
            var cell2 = row.insertCell(1);
            var element2 = document.createElement("select");
            element2.name = "category"+line_no;
			element2.id = "category"+line_no;
			element2.className = "select160";
			
			element2.onchange = function() {document.getElementById("index"+line_no).value = document.getElementById("category"+line_no).selectedIndex; }
						
			var element9 = document.createElement("input");
            element9.type = "hidden";
            element9.name = "index"+line_no;
			element9.id = "index"+line_no;
			
			var element10 = document.createElement("input");
            element10.type = "hidden";
            element10.name = "store_item_id"+line_no;
			element10.id = "store_item_id"+line_no;
											
            var element7 = document.createElement("select");
            element7.name = "store_item_id_select"+line_no;
			element7.id = "store_item_id_select"+line_no;
			element7.className = "select300";
			element7.style="margin-left:3px;";
			element7.selectedIndex= -1;
			element7.onchange = function(){
				document.getElementById("line_checkbox"+line_no).checked = "checked"; 
				document.getElementById("store_item_id"+line_no).value = 
				document.getElementById("store_item_id_select"+line_no).value; 
				units=document.getElementById("store_item_id_select"+line_no).options[document.getElementById("store_item_id_select"+line_no).selectedIndex].text;
				units2 = units.split("~"); 
				units3 = units2[1]; 
				if (units3=="no") {
					document.getElementById("quantity_text"+line_no).value = ""; 
					document.getElementById("quantity_text"+line_no).onkeypress = function num(event) { return numbersonly(this, event);}
					
					}
					else {
						document.getElementById("quantity_text"+line_no).value = ""; 
						document.getElementById("quantity_text"+line_no).onkeypress = function num(event) { return isNumberKey(event);}
						}
						document.getElementById("store_item_id"+line_no).value=document.getElementById("store_item_id_select0"+line_no).value; 
						document.getElementById("store_item_text"+line_no).value = document.getElementById("store_item_id_select0"+line_no).value; 
						};
			
			cell2.appendChild(element2);
			cell2.appendChild(element9);
			cell2.appendChild(element10);
			cell2.appendChild(element7);
			
			<?php
			$t=0;
			while ($t<50) {
			?>
			if (line_no==<?php echo $t ?>) {
			initListGroup("Direct materials", form1.category<?php echo $t ?>, form1.store_item_id_select<?php echo $t ?>);
			function initLists() { 
  			form1.category<?php echo $t ?>.selectedIndex = -1;
  			form1.category<?php echo $t ?>.onchange();
			}
			}
			<?php 
			$t++;
			}
			?>
		
			
            var cell4 = row.insertCell(2);
			cell4.align = "left";
            var element3 = document.createElement("input");
            element3.type = "text";
            element3.name = "quantity_text"+line_no;
			element3.id = "quantity_text"+line_no;
			element3.className = "text60";
			element3.value = "";
			element3.style = "text-align:right";
			element3.autocomplete="off";
			element3.onfocus = function() {this.select()};
			element3.onkeypress = function num(event) { return isNumberKey(event);}
			element3.onkeyup= function() { this.value = this.value.replace (/(\...).*/, '$1');}
			element3.onchange = function(){document.getElementById("line_checkbox"+line_no).checked = "checked";}
			element3.onchange = function(){document.getElementById("kg_text"+line_no).value = (document.getElementById("quantity_text"+line_no).value / 100) * 1000;}
			element3.onblur = function() {total_weight(); }
			
            var element4 = document.createElement("input");
            element4.type = "hidden";
            element4.name = "line_num";
			element4.value = rowCount;
            						
			cell4.appendChild(element3);
            cell4.appendChild(element4);
			
			var cell5 = row.insertCell(3);
			cell5.align = "left";
            var element9 = document.createElement("input");
            element9.type = "text";
            element9.name = "kg_text"+line_no;
			element9.id = "kg_text"+line_no;
			element9.className = "text60";
			element9.value = "";
			element9.style = "text-align:right";
			element9.setAttribute('readonly', true);
			  
			                     						
			cell5.appendChild(element9);
            
			
			var element8 = document.createElement("script");
			element8.setAttribute('type', 'text/javascript');
			element8.innerHTML="var frmvalidator  = new Validator('form1');frmvalidator.EnableMsgsTogether();for (i = 0; i < 10; i++) {frmvalidator.addValidation('store_item_id'+i,'req','- store_item on line '+(i+1)+'.'); frmvalidator.addValidation('quantity_text'+i,'req','- quantity on line '+(i+1)+'.');}"; 
			document.getElementsByTagName('head').item(0).appendChild(element8);



		
element1.onmouseup = function() {
				 if (document.getElementById('line_checkbox'+line_no).checked) {
                         element2.style.background = '#F0F0F0';
						 element3.style.background = '#F0F0F0';
						 element7.style.background = '#F0F0F0';
						 element2.selectedIndex=0;
						 element3.value = '-----';
						 element7.selectedIndex= -1;
						 element2.disabled = true;
						 element3.disabled = true;
						 element7.disabled = true;
								 
                } else {
                        element2.style.background = '#FFF';
						element3.style.background = '#FFF';
						element7.style.background = '#FFF';
						element2.selected_index=0;
						element3.value = '';
						element7.selectedIndex= -1;
						element2.disabled = false;
						element3.disabled = false;
						element7.disabled = false;
						
						 
                }
				};
				
			
}
</script>

<script language="javascript">
function initCS() {
<?php
$t=0;
while ($t<50) {
?>
initListGroup("Direct materials", form1.category<?php echo $t ?>, form1.store_item_id_select<?php echo $t ?>); 

function initLists() {
  form1.category<?php echo $t ?>.onchange();
  category<?php echo $t ?>.fireEvent("onchange");
}

<?php 
$t++;
}
?>
}

</script>