<?php
include("../include/session.php");  
include("organizationPermission.class.php");
require_once("../include/accessCheck.php");
if(!$session->logged_in || $session->userlevel!=1)     //Not logged in or not Super Admin
{
    echo accessDeniedMsg;
    exit;
}
if (isset($_GET['page'])){ //find out what the sub page is
	$page = $_GET['page'];
	}
else{$page = 'display';}

global $pageNum;
if(isset($_GET['pageNum']))
        $pageNum=$_GET['pageNum']; //Get the current page number
    else
        $pageNum=1;

		


function displayPermission($adminID){

	global $pageNum;
	global $adminID;
$result=organiztionPermission::showAllOrganization();
$nrResults = mysql_numrows($result);
//paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     
$limit=10000; // Limit of result per page 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  

$result = organiztionPermission::showAllOrganization($pageNum,$limit);
$num_rows = mysql_numrows($result);
//
$organizationResult = organiztionPermission::getAdminOrganization($adminID);
$organizationRow = mysql_numrows($organizationResult);
if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "Database table empty";
return;
}

/* Display table contents */
echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Permission</b></th><th><b>Organization Name</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$organizationID  = mysql_result($result,$i,"organizationID");
$organizationName  = mysql_result($result,$i,"organizationName");

$permission=false;
for($j=0; $j<$organizationRow; $j++){
$admin_OrganizationID=mysql_result($organizationResult,$j,"organizationID");
	if($admin_OrganizationID==$organizationID){
		$permission=true;
	}
}
if($permission==true){
	echo '<tr><td class="'.($i%2? "c1":"c2").'" align="center"><input type="checkBox" name="organizationID[]" value="' .$organizationID. '" checked></td>';
}else{
	echo '<tr><td class="'.($i%2? "c1":"c2").'" align="center"><input type="checkBox" name="organizationID[]" value="' .$organizationID. '"></td>';
}
echo '<input type="hidden" name="id" value="' .$adminID. '"></input>';
//echo $selectedID;
echo '<td class="'.($i%2? "c1":"c2").'">' .$organizationName. '</td>';    
echo "</tr>\n";
}
echo '<tr><td class="'.($i%2? "c1":"c2").'" align="center" colspan="2"><input type="submit"><input type="button" value="Cancel" onClick="cancelButton()"></input></td>';
echo "</table><br>";
echo "</form>";
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    

organiztionPermission::displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF']."?id=$adminID");

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="../style.css"/>       
</head>
<body >

<div id="layout">
	<?php require('../include/header.php');   ?>
	 <P class=veryLargeBlue>Manage Permission - Organization</P>
	<?php
if(isset($_POST['id'])){
		$adminID=$_POST['id'];
        $selectedID = NULL;
        if(isset($_POST['organizationID']))
		    $selectedID=$_POST['organizationID'];
		if(organiztionPermission::updateOrganizationPermission($adminID, $selectedID)==1){
			echo "Update successful.<br><a href=permission.php>Return to Manage Permissions page </a> || 
            <a href=applicationPermission.php?id=".$adminID."> Update Application Permissions</a>";
		}else{
		echo "Fail to update.<a href=permission.php> Click here return to Manage Permissions page </a>";
		}
		displayPermission($_POST['id']);
		
}else if(isset($_GET['id'])){
		$adminID=$_GET['id'];
		displayPermission($_GET['id']);
}else{
		echo "Wrong Page";
}

	?>
	
	
<?php require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
<script type="text/javascript">
function cancelButton() {
    window.location = "permission.php";
}
</script>