<?php
include("../include/session.php");  
include("applicationPermission.class.php");
require_once("../include/accessCheck.php");

if(!$session->logged_in || $session->userlevel!=1)     //Not logged in or not Super Admin
{
    echo accessDeniedMsg;
    exit;
}
function displayPermission(){

	global $adminID;
	

$organizationResult = applicationPermission::getOrganizationIDAppID($adminID);  //get Organization ID & Application ID
$num_rows = mysql_numrows($organizationResult);
if(!$organizationResult || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "This admin must have organization permissions before application permissions can be assigned. Please go to <a href='organizationPermission.php?id=" .$adminID. "'>Organization Permission</a>";
return;
}
for($i=0; $i<$num_rows; $i++){
	$organizationID[$i]  = mysql_result($organizationResult,$i,"organizationID");
	$applicationID[$i] = mysql_result($organizationResult,$i,"appID");
}


$organizationApp = applicationPermission::getOrganizationAndApp($organizationID);
$num_rows = mysql_numrows($organizationApp);

//get selected Application
$appPermission = applicationPermission::getAppPermission($adminID);
$appID_row= mysql_numrows($appPermission);

/* Display table contents */
echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" name="theForm">';
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Permission</b></th><th><b>Organization Name : Application Name</b></th></tr>\n";
?>
<tr><th><b>Select <a href="javascript:selectToggle(true, 'theForm');">All</a> | <a href="javascript:selectToggle(false, 'theForm');">None</a></b></th></tr>
<?php
for($i=0; $i<$num_rows; $i++){
$appName  = mysql_result($organizationApp,$i,"appName");
$organizationName  = mysql_result($organizationApp,$i,"organizationName");
$appID=mysql_result($organizationApp,$i,"appID");


$permission=false;
for($j=0; $j<$appID_row; $j++){
	$application= mysql_result($appPermission,$j,"appID");
	if($application==$appID){
		$permission=true;
	}
}
if($permission==true){
	echo '<tr><td class="'.($i%2? "c1":"c2").'" align="center"><input type="checkBox" name="application[]" value="' .$appID. '" checked></td>';
}else{
	echo '<tr><td class="'.($i%2? "c1":"c2").'" align="center"><input type="checkBox" name="application[]" value="' .$appID. '"></td>';
}
echo '<input type="hidden" name="id" value="' .$adminID. '"></input>';
echo '<td class="'.($i%2? "c1":"c2").'">' .$organizationName. ' : ' .$appName. '</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>";
}


?>

<!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 - Application</P>
	<?php
	if(isset($_POST['id'])){
		$adminID=$_POST['id'];
        $selectedID = NULL;
        if(isset($_POST['application']))
            $selectedID=$_POST['application'];
		
		if(applicationPermission::updateAppPermission($adminID, $selectedID)==1){
			echo "Update successful.<a href=permission.php> Click here return to Manage Permissions page </a>";
		}else{
			echo "Fail to update.<a href=permission.php> Click here return to Manage Permissions page </a>";
		}
		displayPermission();
		
	}else if(isset($_GET['id'])){
		$adminID=$_GET['id'];
		displayPermission();
	}else{
		echo "The user belongs to one or more organizations for which there are no defined applications.<a href='permission.php'> Click here to go back. </a>";
	}

	?>
	
	
<?php require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>

<script type="text/javascript">
function selectToggle(toggle, form) {
     var myForm = document.forms[form];
     for( var i=0; i < myForm.length; i++ ) { 
          if(toggle) {
               myForm.elements[i].checked = "checked";
          } 
          else {
               myForm.elements[i].checked = "";
          }
     }
}
function cancelButton() {
    window.location = "permission.php";
}
</script>

</body>
</html>
