<?php
$x=getsecurity();
$endtext='';
$uselinktext='';
// Safely read optional submenu param
$initialsubmenu=safetext2(isset($_GET['submenu']) ? $_GET['submenu'] : '');
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $hlink = "https"; 
else
    $hlink = "http";
$hlink .= "://"; 
$hlink .= $_SERVER['HTTP_HOST']; 
$hlink .= $_SERVER['REQUEST_URI']; 
$menuallowed=1;
?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
    var now = new Date(<?php echo time() * 1000 ?>);
    function startInterval(){  
        setInterval('updateTime();', 1000);  
    }
    startInterval();//start it right away
    function updateTime(){
        var nowMS = now.getTime();
        nowMS += 1000;
        now.setTime(nowMS);
        var clock = document.getElementById('qwe');
        if(clock){
            clock.innerHTML = now.toTimeString();//adjust to suit
        }
    } 
</script>	

<script type="text/javascript">
function showwhoinandout( epochdate ) {
 var link = "../functions/showwhoinandout.php<?php print "$x";?>&epochdate="+epochdate;
 //document.getElementById("showwhoinandout").innerHTML=link;
	if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.open("GET",link,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    	
    document.getElementById("whosinandabout").innerHTML=xmlhttp.responseText;
    }
  }
}
</script>

<script type="text/javascript">
function updatemystatus() {

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","/intranet/databases/menu/staffstatus.php<?php print "$x";?>",true);
xmlhttp.send();
document.getElementById('whosinandabout').innerHTML="Lets See Whos About..";

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	document.getElementById('whosinandabout').innerHTML=xmlhttp.responseText;
    }
  }
}
</script>

<script type="text/javascript">
function updateprototype() {

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","/intranet/databases/menu/updateprototype.php<?php print "$x";?>",true);
xmlhttp.send();
document.getElementById('menuprototype').innerHTML="Updating";

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	document.getElementById('menuprototype').innerHTML=xmlhttp.responseText;
    }
  }
}
</script>

<script type="text/javascript">
function favme(userid , menuid) {

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","/intranet/databases/menu/setfav.php?userid="+userid+"&menuid="+menuid,true);
xmlhttp.send();
document.getElementById(menuid).innerHTML="W";

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	document.getElementById(menuid).innerHTML=xmlhttp.responseText;
    }
  }
}
</script>


<script type="text/javascript">
function requesttraining( menuid , userid) {
var link="TT"+menuid;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","/intranet/databases/menu/requesttraining.php?userid="+userid+"&menuid="+menuid,true);
xmlhttp.send();
document.getElementById(link).innerHTML='&#10004;';
}
</script>


 
<script type="text/javascript">

function ismenusearch(sel,newwindow) {


	var searchtype = document.getElementById('atlassearch').value;
	var searchbox = document.getElementById('atlassearchbox').value;
	var link = "/intranet/databases/functions/ismenusearch.php<?php print "$x";?>&searchid="+searchtype+"&searchtext="+searchbox;
	var responsecode='';
	var access=0;
	//document.getElementById('dltest').innerHTML='Searching '+searchtype+" FOR "+searchbox+link ;
	
	
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET",link,true);
xmlhttp.send();


xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	//document.getElementById('dltest').innerHTML=xmlhttp.responseText;
	responsecode=xmlhttp.responseText;
	access=responsecode.split(",")[1];
	if (access==1) {
		
		if (newwindow==1) {
		window.open(responsecode.split(",")[2],'<?php print "$nowis";?>');
		} else {
		window.location=responsecode.split(",")[2];
		}
	} else {
		<?php
		if ($vui==2) {
			print "document.getElementById('dltest').innerHTML=\"Failed \"+responsecode;";
		} else {
			print "document.getElementById('dltest').innerHTML=\"Failed \";";
		}
		?>
		
	}
    }
  }
	
	
	
	
}

</script>

<script type="text/javascript">

function searchhistory() {
	var searchtype = document.getElementById('atlassearch').value;
	window.location='/intranet/databases/search/searchhistory.php<?php print "$x";?>&searchid='+searchtype;
}
</script>

<script type="text/javascript">
// AI_enter_key_search_01
document.addEventListener("DOMContentLoaded", function () {
    var searchInput = document.getElementById("atlassearchbox");

    if (searchInput) {
        searchInput.addEventListener("keydown", function (e) {
            if (e.keyCode === 13 || e.which === 13) {
                e.preventDefault(); // Prevent default Enter behavior
                ismenusearch('A', '0'); // Trigger the search
                return false;
            }
        });
    }
});
</script>



<script type="text/javascript">
function showoutoffice() {
    document.getElementById("holiday222").innerHTML='<?php print "<br>$endtext $uselinktext";?>';
  }
 function hideoutoffice() {
    document.getElementById("holiday222").innerHTML=" ";
  } 
  
</script>

<script type="text/javascript">
// AI_submenu_click_handler_AJAX_02
document.addEventListener("DOMContentLoaded", function () {
    var links = document.getElementsByClassName("submenu-link");

    for (var i = 0; i < links.length; i++) {
        links[i].addEventListener("click", function (e) {
            // If middle-click or Ctrl/Cmd click, let browser handle it
            if (e.ctrlKey || e.metaKey || e.button === 1) {
                return; // allow default behavior
            }

            e.preventDefault(); // stop default link follow

            var submenu = this.getAttribute("data-submenu");
            if (submenu !== "") {
                loadSubmenu(submenu); // trigger AJAX submenu loader
            }
        });
    }
});

// AI_load_submenu_AJAX_01
function loadSubmenu(submenu) {
    var xmlhttp;
    var url = "/intranet/databases/menu/get_submenu.php<?php echo $x; ?>&submenu=" + encodeURIComponent(submenu);
    //document.getElementById("submenuarea").innerHTML = url;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("submenuarea").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

// AI_load_submenu_AJAX_01
function loadstaticlinks() {
    var xmlhttp;
    var url = "/intranet/databases/menu/get_static_links.php<?php echo $x; ?>&hlink=<?php echo $hlink;?>";
    document.getElementById("staticlinks").innerHTML = url;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("staticlinks").innerHTML = xmlhttp.responseText;
            loadSubmenu('<?php echo $initialsubmenu;?>');
        }
    }

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
</script>

<script type="text/javascript">
// AI_toggle_topmenu_01
function toggleTopMenu() {
    var icons = document.getElementById("topmenuicons");
    var toggle = document.getElementById("togglebutton");

    if (icons.style.display === "none") {
        icons.style.display = "block";
        toggle.innerHTML = "&laquo;"; // show <<
    } else {
        icons.style.display = "none";
        toggle.innerHTML = "&raquo;"; // show >>
    }
}

// Optional: start collapsed
document.addEventListener("DOMContentLoaded", function () {
    var icons = document.getElementById("topmenuicons");
    var iconhol = document.getElementById("topmenuhols");
    var toggle = document.getElementById("togglebutton");
    icons.style.display = "none";
    iconhol.style.display = "none";
    //toggle.innerHTML = "&raquo;";
    
});
</script>

<script type="text/javascript">
function toggleTopHolidays() {
var icons = document.getElementById("topmenuhols");
    if (icons.style.display === "none") {
        icons.style.display = "block";
    } else {
        icons.style.display = "none";
    }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","/intranet/databases/menu/get_holidays.php<?php echo "$x";?>",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	 icons.innerHTML=xmlhttp.responseText;
    }
  }
}
</script>
<?php
// Safely read search parameters to avoid undefined constant errors
$menusearchtype=safetext2(isset($_GET['menusearchtype']) ? $_GET['menusearchtype'] : '');
$menusearchbox=safetext2(isset($_GET['menusearchbox']) ? $_GET['menusearchbox'] : '');



	
$s="SELECT * FROM intrastat_access where userid='$vui'";
//print "$s<br>";
$result=sqlquery($s);
while ($row=$result->fetch_assoc())
		{
		$menuid=$row['menuid'];
		$accessgranted[$menuid]=1;
		$fav[$menuid]=$row['fav'];
		}
//print "$mybirthday";
$birthday=date("dm");


	

$s="SELECT * FROM building_temperatures where locationid='150394' order by  id desc limit 0,1";
$result=sqlquery($s);
	while ($row=$result->fetch_assoc())
	{
		$curtemp="$row[temperature] &#176;"."C";
	}


$s="SELECT ISO_quality_manual_user_read.docid as docid FROM 	 intrastatmenu , ISO_quality_manual_user_read
where ISO_quality_manual_user_read.userid='$vui' and
ISO_quality_manual_user_read.docid = intrastatmenu.docid and ISO_quality_manual_user_read.docid<>'0'
";
$result=sqlquery($s);
$num=$result->num_rows;
while ($row=$result->fetch_assoc())
	{
	$docid=$row['docid'];
	$userread[$docid]=1;
	//print "$docid <br>";
	}

	if ( $myfluff==1 and $mybirthday==$birthday)
	{
	$usebackgroundimage="birthday.jpg";
  print "<body background=\"/intranet/images/$usebackgroundimage\" > ";
	} 
	
	
if ($usebackgroundimage=='')
	{
	$usebackgroundimage="background.jpg";
  //print "<body background=\"/intranet/images/$usebackgroundimage\" > ";
  print '<body style="margin:0; background:linear-gradient(180deg,#EEF3FF 0%,#F7F9FF 45%,#F4F6FA 100%); background-attachment:fixed;">';

	}
	

$submenu=safetext2(isset($_GET['submenu']) ? $_GET['submenu'] : '');
if ($localname<>'Blah' and $usersecurity<>''){
	
$cata='o';
$r= "SELECT * FROM viamedagendahistory WHERE investigateby='$localname' and completed='' and investigatedcomplete='' and (hide_until<'$nowis' or hide_until='' ) order by createdate ";
$result = sqlquery($r);
$iout=$result->num_rows;
$nowis=date('U');
$r= "SELECT * FROM viamedagendahistory WHERE investigateby='$localname' and completed='' and investigatedcomplete=''  and viewed ='' and (hide_until<'$nowis' or hide_until='' ) order by createdate ";
$result = sqlquery($r);
$ivout=$result->num_rows;

$r= "SELECT * FROM viamedagendahistory WHERE investigateby='$localname' and completed='' and investigatedcomplete='' and  autoissueid>'0'  order by createdate ";
$result = sqlquery($r);
$isotaskout=$result->num_rows;

$s="SELECT * FROM tellog where currentuser='$vui' and completedon='0'";
$result=sqlquery($s);
$calls=$result->num_rows;
$x=getsecurity();
if ($calls >0)
	{
	
	$callinfo .= "<a href=\"/intranet/databases/telephone/index.php$x\""."&submenu=&showme=me\">";
	$callinfo .= "<i><b><font  color=#ff7878>";
	$callinfo .= "$calls  Calls</font></b></i></a>"; 
	} else
	{
	$callinfo="";
	}
	
	
if ($vui==2)
	{
	$s="SELECT * FROM intrastat_access_failed where reviewed='0'";
	$result=sqlquery($s);
	$num=$result->num_rows;

	if ($num>0)
		{
		$callinfo .= " (<a href=\"/intranet/databases/intrastats_checkers/failedlogins.php$x\">$num</a>) Failed Login Attempts ";
		}
	
	}

$s="SELECT * FROM user_groups_linked where userid='$vui'";
$result=sqlquery($s);
while ($row=$result->fetch_assoc())
		{
		$usergroupid=$row['groupid'];
		$user_groups[$usergroupid]=1;
		}

// AI_load_active_users_AI003 – updated to mysqli/sqlquery compatible
$s = "SELECT * FROM pw WHERE active = '1'";
$result = sqlquery($s);
while ($row = $result->fetch_assoc()) {
    $usenamed = ucwords($row['username']);
    $useractive[$usenamed] = 1;
    $showhideholidaybar[$usenamed] = $row['showhideholiday'];

    if ($row['id'] == $vui) {
        $myhelper = $row['helper'];
    }
}





print "<FONT FACE=\"Arial\">";
// TEST LINE GOING IN HERE


	$usetxt="";
	$usetxt .= $callinfo;
	if ($iout>0)
		{
		$usetxt="$usetxt $iout Issues";
		}

	if ($ivout>0)
		{
		$usetxt="$usetxt <font color=#ff7878 ><b>$ivout</b></font> <font color=#ff7878><b><i>Unread </i></b></font> $isotaskout ISO Tasks";
		}

	

	
$s="SELECT * FROM holvmemphollog_new where 	userinformed='0' and userid='$vui' limit 0,1";
$result = sqlquery($s);
$numhol=$result->num_rows;
if ($numhol > 0 ) {
  		$usetxt="$usetxt | <font color=#ff7878><b><a href=\"/intranet/databases/holidays/my_holidays_review.php$x\">My Requests</a></b></font>";
  		}

	
	
	if ($localname=='Derek Lamb' OR $localname=='Steve Nixon' OR $localname=='steve nixon' OR $localname=='Helen Lamb') {
	
	
	$useraccess=0;
	$s="SELECT * FROM pw_companys where vui='$vui'";
	$result=sqlquery($s);
	$num=$result->num_rows;
	while ($row=$result->fetch_assoc())
		{
		 if ($row['updatestaff']==1) {
		 	$useraccess=1;
		 }
		}

	
  if ($useraccess==1) {
  	//print "h";
  	$s="SELECT * FROM holvmemphollog_new where authorisedon='0' limit 0,1";
  	$result = sqlquery($s);
  	$numhol=$result->num_rows;
  	if ($numhol > 0 ) {
  		$usetxt="$usetxt | <font color=#ff7878><b><a href=\"/intranet/databases/holidays/authorise.php$x\">New Holiday</a></b></font>";
  		}
  	}
	

	}
	
	

// END OF TEST LINE
print "<div id='titlerowmenu' class='titlerowmenu'>";
// main external links should go here



// end of main external links


echo "<div id='staticlinks' style='padding-left:20px;'>";

include ("../../databases/menu/get_static_links.php");

echo "</div>";


echo "<div id='topmenuicons' style='padding-left:20px;'>";
?>
</div>


</div>
<?php
echo "<div id='topmenuhols' style='padding-left:20px;'></div>";
?>
<?php
print "<div id='titledetails' class='titlerowmenu'>";


$ref_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
$ref_url .= "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// Get just the filename (e.g. something.php)
$ref_file = basename($_SERVER['PHP_SELF']);

// Build safe query string (urlencode for reliability)
$update_link = "../intrastats_md/update_page.php".$x."&ref_url=" . urlencode($ref_url) . "&ref_file=" . urlencode($ref_file);

if ($vui==2) {
	echo "<a href=\"$update_link\" target=\"_blank\"><img src=\"/intranet/images/admin.png\" width=16 height=16 title=\"Add Notes for This Page\"></a>";

}


print "<a href=\"/intranet/databases/menu/index.php$x&submenu=\"><img src=\"/intranet/images/menu/staron.png\" width=16 height=16 title=\"Favorites\"></a>";
?>

<a href="/intranet/databases/timeclock/time.php<?php echo $x;?>&submenu="><img src="/intranet/images/clock.png" width=16 height=16 title="Viamed Time" ></a>
<?php
print "<b>".getcurrentname($vui)."</b>";
if ($docstoread=='1')
	{
	?>
	<a class=important href="/intranet/databases/training/training_documents_to_read.php<?php include ("linkget.php");?>&submenu=">Required Reading</a>
	<?php
	} else
	{
	print " ";
	}
print "$usetxt";	
	
	


//Extra Whos active, and the temperature

print "<a href=\"/intranet/databases/temperature/view.php$x\"> $curtemp</a>";


//print"<br>";

// END OF TITLE BAR


//// New Search Box
$searchoptions = "<option value='0'></option>";
$s="SELECT * FROM ATLAS_searchoptions where usersecurity < '$usersecurity' order by description";
$result=sqlquery($s);
while ($row=$result->fetch_assoc())
	{
		if ($row['id']==$menusearchtype) {
			$searchoptions .= "<option value='$row[id]' selected>".ucwords($row['description'])."</option>";
		} else {
			$searchoptions .= "<option value='$row[id]'>".ucwords($row['description'])."</option>";
		}
	}
if ($menuallowed==1) {
?>
<div id='dltest' style="display:inline-block;align: right;float: right;"></div>
<div id='searchbar' style="display:inline-block;align: right;float: right; vertical-align: top;" width=100%> 
<select id='atlassearch'>
<?php
print "$searchoptions";
?>
</select>
<div id='searchhistory' style="display:inline-block">
<input type=text id='atlassearchbox' <?php print "$focustext";?> value='<?php print "$menusearchbox";?>'>
</div>
<img src="/intranet/images/mglass.png" width=20 height=20 onclick="ismenusearch('A','0')" title="Search in this window" alt="Search in this window">
<img src="/intranet/images/openinnewwindow.png" width=20 height=20 onclick="ismenusearch('A','1')" title="Search in new tab" alt="Search in new tab">


 
</div>
<?php
}


echo "<div id='toggleholidays' style='position:absolute; right: 32px; left: auto; z-index:1001; cursor:pointer; background-color:#003366; color:white; padding:4px 8px; font-weight:bold; border-radius:6px; box-shadow:0 0 4px rgba(0,0,0,0.4); font-family:Arial; font-size:14px;' onclick='toggleTopHolidays()' title='Show/Hide Menu Bar'>&raquo;</div>";
print "</div><center>";


	
echo "</center><div id=\"submenuarea\"></div>";	
	


}
Else
{
print "Incorrect Login";
}
?>
<script>
loadSubmenu('<?php echo $initialsubmenu;?>');

</script>
