<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/upper_case.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>   


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

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$double_error = $_GET['double_error'];
$store_item_id = $_GET['store_item_id'];


if ($double_error==0) { 
$table_name = $_SESSION['db_prefix']."_store_register_temp";
$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
execsql($delete_query);
$line_num=0;
}

if ($double_error==2) {
$table_name = $_SESSION['db_prefix']."_store_register_temp";
$temp_query = "SELECT  * FROM $table_name WHERE user_name = '".$_SESSION['username']."' ";
$temp_result = mySQL_query($temp_query);
$temp_num = mysql_num_rows($temp_result);
}

$today = date("Y-m-d");
$receive_date = strtotime($today);
$d = date("d", $receive_date ) ;
$m = date("m", $receive_date ) ;
$y = date("Y", $receive_date ) ;

$table_name = $_SESSION['db_prefix']."_store_items";
$select_query = "SELECT store_item_id, item_name FROM $table_name WHERE register = 'yes' ORDER BY item_name";
	$arrId = array ();
	$arrName = array ();
	$select_result = execsql($select_query);
	$select_num = mysql_num_rows($select_result);
	$a = 0;
	while ($a < $select_num) {
		$arrId[$a] = mysql_result($select_result, $a, "store_item_id");
		$arrName[$a] = mysql_result($select_result, $a, "item_name");
		$a++;
	}
	

?>

<script language="javascript">
        function addRow(tableID) {
 
            var table = document.getElementById(tableID);
 
            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.checked="checked";*/
            cell1.appendChild(element1);
			 
            var cell2 = row.insertCell(1);
            var element5 = document.createElement("input");
            element5.type = "text";
			element5.value = "";
            element5.name = "serial_no_text"+line_no;
			element5.id = "serial_no_text"+line_no;
			element5.onchange = function(){document.getElementById("line_checkbox"+line_no).checked = "checked";}
			element5.className = "text200";
			element5.autocomplete="off";
			element5.onkeyup= function(){ ToUpper(this); }
			cell2.appendChild(element5);
			
			var element4 = document.createElement("input");
            element4.type = "hidden";
            element4.name = "line_num";
			element4.value = rowCount;					
 
            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "text";
            element2.name = "notes_text"+line_no;
			element2.className = "text300";
			element2.value = "";
			cell3.appendChild(element2);
			cell3.appendChild(element4);
					
			
			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('serial_no_text_text'+i,'req','- serial no 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';
						 element5.style.background = '#F0F0F0';
						 element2.value = '-----';
						 element5.value = '-----';
						 element2.disabled = true;
						 element5.disabled = true;
						
                } else {
                        element2.style.background = '#FFF';
						element5.style.background = '#FFF';
						element2.value = '';
						element5.value = '';
						element2.disabled = false;
						element5.disabled = false;
						 
                }
				};
			
}

</script>
</head>



<?php
include ("../navigation/header.php");
$page_name = "Stores-registration";
include ("../navigation/access.php");
if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
?>

<body onload="<?php if ($store_item_id=='') { ?> document.getElementById('hidden_frame').style.display = 'none'; <?php } else { ?> document.getElementById('hidden_frame').style.display = 'block'; <?php } ?> <?php if ($double_error==2) {?> alertMessage(1, '', 'There are duplicated serial numbers<br><br>orange = serial number duplicated in this list.<br>red = serial number already in register.'); <?php } ?>">


<div class="main_frame"  style="height:auto; min-height:570px;">

<div class="page_head_frame">
        <table width="809" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><label class="label_page_head">Store registration</label></td>
            </tr>
        </table>
      </div>
        <br /><br />
        <div class="page_middle_frame">
<div class="page_inside_frame" style=" <?php if ($store_item_id!='') { ?> display:none;   <?php } ?>">
<form name="form1" id="form1" action="store_registration.php" method="get"> <label class="label_std">Select store item:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
  <select name="store_item_select" class="select200" onchange="window.location.href= this.form.store_item_select.options[this.form.store_item_select.selectedIndex].value" >
    <option value=""></option>
    <?php
        	for ($i = 0; $i < count($arrId); $i++) {
    		echo "<option value=store_registration.php?double_error=1&store_item_id=".$arrId[$i].">".$arrName[$i]."</option>";
	}
?>
  </select>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</form>
</div>
<?php
$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) {
$store_item_name = mysql_result($name_result, 0, "item_name");
}
else {
	$store_item_name='';
}
?>
<div class="hidden_frame" id="hidden_frame">
<form action="store_registration_save.php" method="GET" name="form2" id="form2">
<div class="page_inside_frame">
  <p>Register <?php echo $store_item_name ?></p>
  <p>Date received:&nbsp; 
    <?php 
				  //instantiate class and set properties
		$myCalendar = new tc_calendar("receive_date", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($d, $m, $y);
		$myCalendar->setYearInterval(2014, 2020);
		//output the calendar
		$myCalendar->writeScript();
?>
    &nbsp;<br>
    <br>
  </p>
<?php 
if ($double_error==1) {
	?>  
  <table id="dataTable" width="654" border="1" style="border-collapse:collapse;">
    <thead>
      <tr>
        <td>&nbsp;</td>
        <td>Serial no</TD>
        <td>Notes</td>
        </tr>
    </thead>
    <script>
			 function check_it() {
				 if (document.getElementById('line_checkbox0').checked) {
                         document.getElementById('notes_text0').style.background = '#F0F0F0';
						 document.getElementById('serial_no_text0').style.background = '#F0F0F0';
						 document.getElementById('notes_text0').value = '-----';
						 document.getElementById('serial_no_text0').value = '-----';
						 document.getElementById('notes_text0').disabled = true;
						 document.getElementById('serial_no_text0').disabled = true;
						 
                } else {
                        document.getElementById('notes_text0').style.background = '#FFF';
						document.getElementById('serial_no_text0').style.background = '#FFF';
						document.getElementById('notes_text0').value = '';
						document.getElementById('serial_no_text0').value = '';
						document.getElementById('notes_text0').disabled = false;
						document.getElementById('serial_no_text0').disabled = false;
						 
                }
				}
		     </script>
    <tr>
      <td width="20"><input type="checkbox" name="line_checkbox0" id="line_checkbox0" onMouseUp="check_it()" /></td>
      <td width="202"><input class="text200" name="serial_no_text0" id="serial_no_text0" value="" onChange="document.getElementById('line_checkbox0').checked = 'checked';" onkeyup="ToUpper(this)" autocomplete="off"/>
        <label id="closeOnSelect">
          <!--<input type="checkbox" /> Close on selection-->
        </label>
        </td>
      <td width="410"><input name="notes_text0" type="text" class="text300" id="notes_text0"  value="" />
        <input type="hidden" name="line_num" id="line_num" value="1" ></td>
      <!--<input name="textfield0" type="text" id="textfield0" size="5" value="1"></td>-->
   </tr>
</table>

<?php
}
?>

<?php 
if ($double_error==2) {
	?>  
  <table id="dataTable" width="654" border="1" style="border-collapse:collapse;">
    <thead>
      <tr>
        <td>&nbsp;</td>
        <td>Serial no</TD>
        <td>Notes</td>
        </tr>
    </thead>
    <?php
	$i=0;
	while ($i<$temp_num) {
		$serial_number = mysql_result($temp_result, $i, "serial_number");
		$notes = mysql_result($temp_result, $i, "notes");
		$double_error = mysql_result($temp_result, $i, "double_error");
	?>
               
    <tr>
      <td width="20"><input type="checkbox" name="line_checkbox<?php echo $i ?>" id="line_checkbox<?php echo $i ?>" onMouseUp="
      if (document.getElementById('line_checkbox<?php echo $i ?>').checked) {
                         document.getElementById('notes_text<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('serial_no_text<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('notes_text<?php echo $i ?>').value = '-----';
						 document.getElementById('serial_no_text<?php echo $i ?>').value = '-----';
						 document.getElementById('notes_text<?php echo $i ?>').disabled = true;
						 document.getElementById('serial_no_text<?php echo $i ?>').disabled = true;
		          } 
                  else {
                        document.getElementById('notes_text<?php echo $i ?>').style.background = '#FFF';
						document.getElementById('serial_no_text<?php echo $i ?>').style.background = '#FFF';
						document.getElementById('notes_text<?php echo $i ?>').value = '';
						document.getElementById('serial_no_text<?php echo $i ?>').value = '';
						document.getElementById('notes_text<?php echo $i ?>').disabled = false;
						document.getElementById('serial_no_text<?php echo $i ?>').disabled = false;
				}
      " checked="checked"/></td>
      <td width="202"><input class="text200" name="serial_no_text<?php echo $i ?>" id="serial_no_text<?php echo $i ?>" onChange="document.getElementById('line_checkbox<?php echo $i ?>').checked = 'checked';" onkeyup="ToUpper(this)" value="<?php echo $serial_number ?>" <?php $color='FFF'; if ($double_error==1) {$color='#F5D5A7';} if ($double_error==2) {$color='#FEBCC6';} ?> style="background-color:<?php echo $color?>;" autocomplete="off"/>
        <label id="closeOnSelect">
          <!--<input type="checkbox" /> Close on selection-->
        </label>
        </td>
      <td width="410"><input name="notes_text<?php echo $i ?>" type="text" class="text300" id="notes_text<?php echo $i ?>"  value="<?php echo $notes ?>" />
        </td>
      
   </tr>
   <?php
   $i++;
	}
   ?>
   <input type="hidden" name="line_num" id="line_num" value="<?php echo $i ?>" >
</table>

<?php
}
?>
<div align="left">
<input type="button" class="imagebutton" onClick="addRow('dataTable')" value="Add line" />
</div>
  <div align="right">
    <label class="label_small">To delete a line: uncheck line box and click Temp Save.</label>
</div>
  <br><br>
</div>
<div class="page_inside_frame">
  <table width="645" border="0">
    <tr>
      <td width="20"></td>
      <td width="615" align="right"><input type="hidden" name="store_item_id_text" id="store_item_id_text" value="<?php echo $store_item_id ?>" />
        <input name="button2" type="button" class="imagebutton" id="button2" onclick="cancel_confirmation(3, '../store/store_main.php?date=0');" value="Close"/>
        <input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onclick="needToConfirm = false; confirmRefresh();" /></td>
    </tr>
  </table>
</div>
</form> 

</div>     
</div>
</div>





<?php } else {
    $_SESSION['denied_return'] = "store/store_main.php?date=";	
    include ("../navigation/store_sidebar.php");?>
	<body onLoad="accessdenied(); return false">	
	</body>
<?php }
?>


<?php include ('../navigation/side_bar.php'); ?>
</body>
</html>