<!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" />
<title>Tocotex main menu</title>


<?php
include ("login_include/dbset.php");

$con = mysqli_connect("localhost",$dbu,$dbp);
if (!$con)
  {
  die('Could not connect to database: ' . mysqli_error());
  }
   
mysqli_select_db($con, $dbs);

//function execsql($qry){
//	$result = $con->query($login_query);
//	return $result;
//}

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

$log = $_GET['log'];
$e = $_GET['e'];

if ($log==0) {
	
$ip_address=$_POST['ip_address'];
$username_text = $_POST['user_name_text'];
$password_text = $_POST['password_text'];




$login_query = "SELECT username, password, access_id, tlh_gab, tlh_ftw, rad_gab, rad_nam, rad_rmc, vertex, pecus, admin, reporting FROM system_users WHERE BINARY username LIKE  '".$username_text."' and password = '".md5($password_text)."' ";
$login_result = $con->query($login_query);
$login_num = mysqli_num_rows($login_result);
if ($login_num == 1) {
	
$check_query = "SELECT username, password, access_limit FROM system_users WHERE BINARY username LIKE  '".$username_text."' and password = '".md5($password_text)."' ";
$check_result = $con->query($check_query);
$check_num = mysqli_num_rows($check_result);
if ($check_result->num_rows > 0) {
     while($row = $check_result->fetch_assoc()) {
		$access_limit = $row["access_limit"];
    }
} else {
    $access_limit = 'yes';
}

$table_name = "test_tracking_log";
$track_check_query = "INSERT INTO $table_name (entry_date, user, company, page, location) VALUES ('".$entry_date."' ,'".$username_text."', 'Main', 'mainmenu.php', '".$ip_address."' )";
$track_check_query = $con->query($track_check_query);


$access_perm = 'yes';
if ($access_limit=='yes') {
	   	$ip_query = "SELECT address_no FROM ip_address WHERE ip_address = '".$ip_address."' ";
		$ip_result = $con->query($ip_query);
		$ip_num = mysqli_num_rows($ip_result);
		if ($ip_result->num_rows > 0) {
			  $access_perm = 'yes';
		}
		else {
			$access_perm = 'no';
		}
}

if ($access_perm=='yes') {
while($row = $login_result->fetch_assoc()) {	
	
$username = $row["username"];
$access_id = $row["access_id"];
$tlh_gab = $row["tlh_gab"];
$tlh_ftw = $row["tlh_ftw"];
$rad_gab = $row["rad_gab"];
$rad_nam = $row["rad_nam"];
$rad_rmc = $row["rad_rmc"];
$vertex = $row["vertex"];
$pecus = $row["pecus"];
$admin = $row["admin"];
$reporting = $row["reporting"];
}


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

$delete_query = "DELETE FROM active_users WHERE  username = '".$username."' "; 
$delete_result = $con->query($delete_query);

$active_query = "INSERT INTO active_users (username, log_in, last_active, logged_into) VALUES ('".$username."', '".$entry_date."', '".$entry_date."', 'Log in' ) ";
$active_result = $con->query($active_query);
}
else {
   ?>
    <script>
	window.location='main.php?e=3';
	</script>
    <?php
}
}
else {
	?>
    <script>
	window.location='main.php?e=1';
	</script>
    <?php	
}
}


if ($log==1) {
$access_id = $_GET['cx'];
$login_query = "SELECT username, password, tlh_gab, tlh_ftw, rad_gab, rad_nam, rad_rmc, vertex, pecus, admin, reporting FROM system_users WHERE access_id = '".$access_id."'  ";
echo '<br>*****'.$login_query;
$login_result = $con->query($login_query);
$login_num = mysqli_num_rows($login_result);
if ($login_num == 1) {
$row = mysqli_fetch_assoc($login_result);	
$username = $row["username"];
$tlh_gab = $row["tlh_gab"];
$tlh_ftw = $row["tlh_ftw"];
$rad_gab = $row["rad_gab"];
$rad_nam = $row["rad_nam"];
$rad_rmc = $row["rad_rmc"];
$vertex = $row["vertex"];
$pecus = $row["pecus"];
$admin = $row["admin"];
$reporting = $row["reporting"];
}
}

if ($log==2) {
$access_id = $_GET['cx'];
$login_query = "SELECT username, password, tlh_gab, tlh_ftw, rad_gab, rad_nam, rad_rmc, vertex, pecus, admin, reporting FROM system_users WHERE access_id = '".$access_id."'  ";
$login_result = $con->query($login_query);
$login_num = mysqli_num_rows($login_result);
if ($login_num == 1) {
$row = mysqli_fetch_assoc($login_result);
$username = $row["username"];
$tlh_gab = $row["tlh_gab"];
$tlh_ftw = $row["tlh_ftw"];
$rad_gab = $row["rad_gab"];
$rad_nam = $row["rad_nam"];
$rad_rmc = $row["rad_rmc"];
$vertex = $row["vertex"];
$pecus = $row["pecus"];
$admin = $row["admin"];
$reporting = $row["reporting"];

$update_query = "UPDATE active_users SET logged_into = 'Log in' WHERE username = '".$username."' ";
$update_result = $con->query($update_query);
}
}


$access_text = '';
if ($tlh_gab==1) {$access_text=$access_text.'- Tlhomeso Gaborone<br>';}
if ($tlh_ftw==1) {$access_text=$access_text.'- Tlhomeso Francistown<br>';}
if ($rad_gab==1) {$access_text=$access_text.'- Radstone Gaborone<br>';}
if ($rad_nam==1) {$access_text=$access_text.'- Radstone Namibia<br>';}
if ($rad_rmc==1) {$access_text=$access_text.'- Radstone ready mix<br>';}
if ($vertex==1) {$access_text=$access_text.'- Vertex<br>';}
if ($pecus==1) {$access_text=$access_text.'- Pecus<br>';}





$table_name = "tlh_alert_message";
$tlh_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$tlh_alert_result = $con->query($tlh_alert_query);
$tlh_alert_num = mysqli_num_rows($tlh_alert_result);


$table_name = "ftw_alert_message";
$ftw_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$ftw_alert_result = $con->query($ftw_alert_query);
$ftw_alert_num = mysqli_num_rows($ftw_alert_result);

$rds_alert_num = 0;
$table_name = "rds_alerts_hr";
$rds_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rds_alert_result = $con->query($rds_alert_query);
$rds_hr_num = mysqli_num_rows($rds_alert_result);

$table_name = "rds_alerts_production";
$rds_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rds_alert_result = $con->query($rds_alert_query);
$rds_production_num = mysqli_num_rows($rds_alert_result);

$table_name = "rds_alerts_store";
$rds_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rds_alert_result = $con->query($rds_alert_query);
$rds_store_num = mysqli_num_rows($rds_alert_result);


$table_name = "rds_alerts_quality";
$rds_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rds_alert_result = $con->query($rds_alert_query);
$rds_quality_num = mysqli_num_rows($rds_alert_result);

$rds_alert_num = $rds_hr_num + $rds_production_num + $rds_store_num + $rds_quality_num;

$nmb_alert_num = 0;
$table_name = "nmb_alerts_hr";
$nmb_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$nmb_alert_result = $con->query($nmb_alert_query);
$nmb_hr_num = mysqli_num_rows($nmb_alert_result);

$table_name = "nmb_alerts_production";
$nmb_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$nmb_alert_result = $con->query($nmb_alert_query);
$nmb_production_num = mysqli_num_rows($nmb_alert_result);

$table_name = "nmb_alerts_store";
$nmb_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$nmb_alert_result = $con->query($nmb_alert_query);
$nmb_store_num = mysqli_num_rows($nmb_alert_result);

$table_name = "nmb_alerts_quality";
$nmb_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$nmb_alert_result = $con->query($nmb_alert_query);
$nmb_quality_num = mysqli_num_rows($nmb_alert_result);

$nmb_alert_num = $nmb_hr_num + $nmb_production_num + $nmb_store_num + $nmb_quality_num;

$table_name = "ver_alert_message";
$ver_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$ver_alert_result = $con->query($ver_alert_query);
$ver_alert_num = mysqli_num_rows($ver_alert_result);

$pec_alert_num = 0;
$table_name = "pec_alerts_hr";
$pec_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$pec_alert_result = $con->query($pec_alert_query);
$pec_hr_num = mysqli_num_rows($pec_alert_result);

$table_name = "pec_alerts_production";
$pec_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$pec_alert_result = $con->query($pec_alert_query);
$pec_production_num = mysqli_num_rows($pec_alert_result);

$table_name = "pec_alerts_store";
$pec_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$pec_alert_result = $con->query($pec_alert_query);
$pec_store_num = mysqli_num_rows($pec_alert_result);

$pec_alert_num = $pec_hr_num + $pec_production_num + $pec_store_num ;


$rmc_alert_num = 0;
$table_name = "rmc_alerts_hr";
$rmc_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rmc_alert_result = $con->query($rmc_alert_query);
$rmc_hr_num = mysqli_num_rows($rmc_alert_result);

$table_name = "rmc_alerts_production";
$rmc_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rmc_alert_result = $con->query($rmc_alert_query);
$rmc_production_num = mysqli_num_rows($rmc_alert_result);

$table_name = "rmc_alerts_store";
$rmc_alert_query = "SELECT message_id FROM $table_name WHERE user_name = '".$username."' AND read_done IS NULL AND no_view IS NULL";
$rmc_alert_result = $con->query($rmc_alert_query);
$rmc_store_num = mysqli_num_rows($rmc_alert_result);

$rmc_alert_num = $rmc_hr_num + $rmc_production_num + $rmc_store_num ;



?>
<style>
label.label_std{
	margin-left:5px;
    line-height:20px;
	padding-top:6px;
    color:#46599D;
    text-shadow:1px 1px 1px #fff;
    font:bold 14px Arial;
}

A:link {
	text-decoration:none;
	color:#FF0;
	}
A:visited {
	text-decoration: none;
	color:#FF0;
	}
A:active {
	text-decoration: none;
	color:#FF0;
	}
A:hover {
	text-decoration:none;
	color:#FF0;
	}
</style>

<!--jscript-->
<script language="JavaScript" src="system_admin/jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="system_admin/jscript/show_hide_layers.js"  type="text/javascript"></script>  
<script language="JavaScript" src="system_admin/jscript/tooltip.js"  type="text/javascript"></script> 
<link rel="stylesheet" type="text/css" href="system_admin/css/tooltip.css" />

<link rel="stylesheet" href="system_admin/window/dhtmlwindow.css" type="text/css" />
<link rel="stylesheet" href="system_admin/window/modal.css" type="text/css" />

<script type="text/javascript" src="system_admin/window/dhtmlwindow.js"></script>
<script type="text/javascript" src="system_admin/window/modal.js"></script>

</head>

<body onload="<?php if ($e=='2') { ?>alertMessage(1, '', 'You have access to this company, but your permissions are not yet set. Contact the companies administrator to set your homepage and permissions.'); return false;<?php } ?>">
<br />
<br />
<table width="752" height="467" border="0" background="main_images/tocotex_main_menu.jpg" align="center">
  <tr>
    <td width="262" height="132" align="center"><?php  if ($tlh_gab==1) {?><a href="Tlhomeso/include/loginproc.php?user_name_text=<?php echo $username ?>"><img src="main_images/tlhomeso_logo_white.png" width="180" height="80" /></a><?php } else { ?> <a href="#" onMouseover="ddrivetip('<b>NO ACCESS RIGHTS.</b><br><br>You cannot log in to the Tlhomeso program.<br><br>Contact the system manager to get access rights.', 250)"; onMouseout="hideddrivetip()"><img src="main_images/tlhomeso_logo_white.png" width="180" height="80" /></a><?php } ?><br />
   <label class="label_std" style="color:#FFF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gaborone</label>
   <?php if ($tlh_alert_num>0) { ?><div style="background-color:#F00; float:right; color:#FFF; width:20px; margin-right:50px; border-radius:10px;"><?php echo $tlh_alert_num ?><?php } else { ?> <div style="background-color:transparent; float:right; width:20px; margin-right:50px;">&nbsp;</div> <?php } ?></div>
</td>
    <td width="242" align="center"><?php  if ($tlh_ftw==1) {?><a href="Tlhomeso_FTW/include/loginproc.php?user_name_text=<?php echo $username ?>"><img src="main_images/tlhomeso_logo_white.png" width="180" height="80" /></a><?php } else { ?> <a href="#" onMouseover="ddrivetip('<b>NO ACCESS RIGHTS.</b><br><br>You cannot log in to the Tlhomeso Ftw program.<br><br>Contact the system manager to get access rights.', 250)"; onMouseout="hideddrivetip()"><img src="main_images/tlhomeso_logo_white.png" width="180" height="80" /></a><?php } ?><br />
  <label class="label_std" style="color:#FFF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Francistown</label>
   <?php if ($ftw_alert_num>0) { ?><div style="background-color:#F00; float:right; color:#FFF; width:20px; margin-right:35px; border-radius:10px;"><?php echo $ftw_alert_num ?><?php } else { ?> <div style="background-color:transparent; float:right; width:20px; margin-right:50px;">&nbsp;</div> <?php } ?></div>  
    </td>
    <td width="242" align="center"><?php  if ($pecus==1) {?><a href="Pecus/include/loginproc.php?user_name_text=<?php echo $username ?>"><img src="main_images/Pecus_logo.png" width="200" height="93" /></a><?php } else { ?> <a href="#" onMouseover="ddrivetip('<b>NO ACCESS RIGHTS.</b><br><br>You cannot log in to the Pecus program.<br><br>Contact the system manager to get access rights.', 250)"; onMouseout="hideddrivetip()"><img src="main_images/Pecus_logo.png" width="200" height="93" /></a><?php } ?><br />
  
   <?php if ($pec_alert_num>0) { ?><div style="background-color:#F00; float:right; color:#FFF; width:20px; margin-right:35px; border-radius:10px;"><?php echo $pec_alert_num ?><?php } else { ?> <div style="background-color:transparent; float:right; width:20px; margin-right:50px;">&nbsp;</div> <?php } ?></div>  
    </td>
  </tr>
  <tr>
    <td height="158" align="center"><label class="label_std" style="color:#FFF">Welcome <a href="#" onMouseover="ddrivetip('You have access to:<br><?php echo $access_text ?>', 150)"; onMouseout="hideddrivetip()">   <?php echo $username ?></a> to<br />
the Toxotex Management Program.<br /><br />
Click on the company logo to enter.</label></td>
    <td height="158" colspan="2" align="center"><?php if ($reporting==1) { ?><a href="system_admin/comp_reporting.php?s=1&cx=<?php echo $access_id ?> "><img src="main_images/tocotex_logo_main.fw.png" width="260" height="61" /></a><?php } else { ?><img src="main_images/tocotex_logo_main.fw.png" width="260" height="61" /><?php } ?></td>
  </tr>
  <tr>
    <td align="center" valign="middle" height="138"><?php  if ($rad_gab==1) {?><a href="Radstone/include/loginproc.php?user_name_text=<?php echo $username ?>"><img src="main_images/radstone_logo_white.png" width="180" height="67" /></a><?php } else { ?> <a href="#" onMouseover="ddrivetip('<b>NO ACCESS RIGHTS.</b><br><br>You cannot log in to the Radstone program.<br><br>Contact the system manager to get access rights.', 250)"; onMouseout="hideddrivetip()"><img src="main_images/radstone_logo_white.png" width="180" height="67" /></a><?php } ?><br />
<label class="label_std" style="color:#FFF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phakalane</label>
   <?php if ($rds_alert_num>0) { ?><div style="background-color:#F00; float:right; color:#FFF; width:20px; margin-right:50px; border-radius:10px;"><?php echo $rds_alert_num ?><?php } else { ?> <div style="background-color:transparent; float:right; width:20px; margin-right:50px;">&nbsp;</div> <?php } ?></div>
    </td>
    <td align="center"><?php echo '#'.$rad_rmc ?> <?php  if ($rad_rmc==1) {?><a href="Radstone_rmc/include/loginproc.php?user_name_text=<?php echo $username ?>"><img src="main_images/radstone_rmc_logo_white.png" width="180" height="67" /></a><?php } else { ?> <a href="#" onMouseover="ddrivetip('<b>NO ACCESS RIGHTS.</b><br><br>You cannot log in to the Radstone program.<br><br>Contact the system manager to get access rights.', 250)"; onMouseout="hideddrivetip()"><img src="main_images/radstone_rmc_logo_white.png" width="180" height="67" /></a><?php } ?><br />
    <label class="label_std" style="color:#FFF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gaborone</label>
   <?php if ($rmc_alert_num>0) { ?><div style="background-color:#F00; float:right; color:#FFF; width:20px; margin-right:35px;  border-radius:10px;"><?php echo $nmb_alert_num ?><?php } else { ?> <div style="background-color:transparent; float:right; width:20px; margin-right:35px;">&nbsp;</div> <?php } ?></div>
    </td>
    <td align="center">
	
	<?php  if ($vertex==1) {?>
    <a href="Vertex/include/loginproc.php?user_name_text=<?php echo $username ?>"><img src="main_images/logo_vertex.png" width="200" height="93" /></a>
	<?php } else { ?>
    <a href="#" onMouseover="ddrivetip('<b>NO ACCESS RIGHTS.</b><br><br>You cannot log in to the Vertex program.<br><br>Contact the system manager to get access rights.', 250)"; onMouseout="hideddrivetip()"><img src="main_images/logo_vertex.png" width="200" height="93" /></a>
	<?php } ?>
    <br />
<label class="label_std" style="color:#FFF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp</label>
<?php if ($ver_alert_num>0) { ?><div style="background-color:#F00; float:right; color:#FFF; width:20px; margin-right:35px;  border-radius:10px;"><?php echo $ver_alert_num ?><?php } else { ?> <div style="background-color:transparent; float:right; width:20px; margin-right:35px;">&nbsp;</div> <?php } ?></div>
    </td>
  </tr>
  <tr>
    <td height="27" align="left" valign="middle"><a href="main.php?e=" onmouseover="ddrivetip('Back to log-in page.', 150)"; onmouseout="hideddrivetip()">Exit</a></td>
    <td height="27" colspan="2" align="right" valign="middle"><?php if ($admin==1) { ?>
      <label class="label_std" style="color:#FFF">You are a system manager. Click <a href="system_admin/admin_settings.php?s=1&cx=<?php echo $access_id ?>">here</a> for admin</label>
      <?php } ?>
&nbsp;&nbsp;</td>
  </tr>
</table>

<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
$prev_page = $_SERVER['HTTP_REFERER'];
$table_name = "test_tracking_log";
$track_check_query = "INSERT INTO $table_name (INSERT INTO $table_name (entry_date, user, company, page, location, prev_page) VALUES ('".$entry_date."' ,'".$username."','Main', 'mainmenu.php', '".$ip_address."', '".$prev_page."' )"; 
$track_check_result = $con->query($track_check_query);
?>

</body>
</html>