<?php
    session_start();
	if ( empty($_SESSION['login']) ) {
        header( 'Location: login.php' );
        die;
	}

    include 'include/database.php';
    $connection = connect($database);
    $canAdmin = canAdministrate($connection, $_SESSION['login']);
	$connection = null;
	if ( !$canAdmin ) {
		header( 'Location: /' );
		die;
	}
	
	include 'include/header.php';
?>

    <script>
        $(function(){
            load('getUserTable.php');
        });

        function load(site) {
            $.get(
                site,
                function(data, status) {
                    $('#admin_content').html(data);
                }
            );
        }
        
        function deleteUser(usrLogin) {
            $.post(
                'deleteUser.php', 
                { user: usrLogin },
                function(data) {
                    load('getUserTable.php');
                }
            );
        }
        
        function sendPermissions(myID, myUser) {
            var usrPrms = 0;
            var usrP = 0;
            usrP = (document.getElementById(myUser+' view').checked)?1:0;
            usrPrms = usrPrms | usrP;
            usrP = (document.getElementById(myUser+' reset').checked)?2:0;
            usrPrms = usrPrms | usrP;
            usrP = (document.getElementById(myUser+' edit').checked)?4:0;
            usrPrms = usrPrms | usrP;
            usrP = (document.getElementById(myUser+' delete').checked)?8:0;
            usrPrms = usrPrms | usrP;
            usrP = (document.getElementById(myUser+' links').checked)?16:0;
            usrPrms = usrPrms | usrP;
            usrP = (document.getElementById(myUser+' control').checked)?32:0;
            usrPrms = usrPrms | usrP;
            usrP = (document.getElementById(myUser+' admin').checked)?64:0;
            usrPrms = usrPrms | usrP;
            
            $.post(
                'setUserPermissions.php', 
                {
                    user: myUser,
                    permissions: usrPrms
                }
            );
        }
        
        /////////////
        function editExe(id, oldExe) {
            var newExe = window.prompt("Edit " + oldExe + "?", oldExe);
            if(newExe) {
                $.post(
                    'editCommandProcessor.php', 
                    {
                        id: id,
                        name: newExe
                    },
                    function(data, status) {
                        load('admin_cmdProcessors.php');
                    }
                );
            }
        }
        function deleteExe(id, oldExe) {
            var is_delete = confirm("Delete " + oldExe + "?");
            if (is_delete) {
                $.post(
                    'deleteExe.php', 
                    { id: id },
                    function(data) {
                        load('admin_cmdProcessors.php');
                    }
                );
            }
        }
        function resetExe() {
            $("#executable").val('');
        }
        function saveCmdProc() {
            var newExe = $("#executable").val();
            if (newExe) {
                $.post(
                    'addExe.php', 
                    { name: newExe },
                    function(data,status) {
                        load('admin_cmdProcessors.php');
                    }
                );
            }
        }
        
        ////////////////////
        var myCampus = '';
        var myBuilding = '';
        var myRoom = '';

        function setSelectedText(elementId, ndx) {
            var elt = document.getElementById(elementId);
            elt.selectedIndex = ndx;
        }
        
        function getSelectedText(elementId) {
            var elt = document.getElementById(elementId);

            if (elt.selectedIndex == -1)
                return null;

            return elt.options[elt.selectedIndex].text;
        }
        
        function setSelectDefault(selectedList) {
            var elt = document.getElementById(selectedList);
            var itemCount = elt.getElementsByTagName("option").length;
            if ( itemCount > 1 ) { 
                setSelectedText(selectedList, 1);
            }else{
                setSelectedText(selectedList, 0);
            }
        }
        
        function selectClick(selectedList, select_all) {
            select_all = typeof select_all !== 'undefined' ? select_all : false;
            
            var elt = document.getElementById(selectedList);
            if (elt.selectedIndex == -1)
                return null;
            var mySelection = elt.options[elt.selectedIndex].text;
            if ( selectedList == 'campusList' ) {
                myCampus = mySelection;
                getBuildingOptions(myCampus, select_all);
                //getRoomOptions(myBuilding);
                document.getElementById('buildingList').focus();
            }
            if ( selectedList == 'buildingList' ) {
                myBuilding = mySelection;
                getRoomOptions(myBuilding, select_all);
            }
            if ( selectedList == 'roomList' ) {
                myRoom = mySelection;
                try {getAssociatedTasks();}
                catch (e) {}
                try {getAssociatedButtons();}
                catch (e) {}
                try {getCmdProcOptions();}
                catch (e) {}
            }
            return mySelection;
        }
        
        function getCampusOptions(select_all){
            select_all = typeof select_all !== 'undefined' ? select_all : false;

            $.get(
                'getCampusOptions.php',
                {
                    'select_all': select_all,
                    'size': 1
                },
                function(data,status) {
                    $('#campus').html(data);
                    if (select_all) {
                        setSelectDefault('campusList');
                    }
                    myCampus = getSelectedText('campusList');
                    getBuildingOptions(myCampus, select_all);
                }
            );
        }
        
        function getBuildingOptions(sCampus, select_all){
            select_all = typeof select_all !== 'undefined' ? select_all : false;
            
            $.get(
                'getBuildingOptions.php', 
                {
                    campus: sCampus,
                    'select_all': select_all,
                    'size': 1
                },
                function(data,status) {
                    $('#building').html(data);
                    if (select_all) {
                        setSelectDefault('buildingList');
                    }
                    myBuilding = getSelectedText('buildingList');
                    getRoomOptions(myBuilding, select_all);
                }
            );
        }

        function getRoomOptions(sBuilding, select_all){
            select_all = typeof select_all !== 'undefined' ? select_all : false;
            
            $.get(
                'getRoomOptions.php', 
                {
                    campus: myCampus,
                    building: sBuilding,
                    'select_all': select_all,
                    'size': 1
                },
                function(data,status) {
                    $('#room').html(data);
                    if (select_all) {
                        setSelectDefault('roomList');
                    }
                    myRoom = getSelectedText('roomList');
                    selectClick('roomList', select_all);
                }
            );
        }
    </script>

    <div class="wrap">

<?php include('include/nav.php'); ?>

        <div id="admin_links" class="center">
            <div><a onclick="load('getUserTable.php')" href="javascript:void(0);">
                Users
            </a></div>

            <div><a onclick="load('admin_scheduler.php')" href="javascript:void(0);">
                Scheduler
            </a></div>

            <div><a onclick="load('admin_cmdProcessors.php')" href="javascript:void(0);">
                Command Processors
            </a></div>

            <div><a onclick="load('admin_buttonEditor.php')" href="javascript:void(0);">
                Button Editor
            </a></div>

            <div><a onclick="load('admin_CodeGenerator.php')" href="javascript:void(0);">
                AMX Code Generator
            </a></div>
        </div>

        <div id="admin_content" class="center pad-bottom"></div>

    </div>

<?php include('include/footer.php'); ?>