<?php
include("../include/session.php");  
include("organization.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;
        
/**
* displayOrganizations - Displays the organization database table in
* a nicely formatted html table.
*/
function displayOrganizations(){
    global $pageNum;
//paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
$result=organization::showAllOrganizations();
$nrResults = mysql_numrows($result);

$limit=20; // Limit of result per page 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
$result = organization::showAllOrganizations($pageNum,$limit);
$num_rows = mysql_numrows($result);

echo "<p><img  SRC=\"../icons/add.png\" >&nbsp;&nbsp;&nbsp;<a href='organization.php?page=add'>Add New Organization</a></p>"; 
if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "No record.";
return;
}

/* Display table contents */
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Organization Name</b></th><th><b>Organization Location</b></th><th><b>Action</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$organizationID  = mysql_result($result,$i,"organizationID");
$organizationName  = mysql_result($result,$i,"organizationName");
$organizationLocation  = mysql_result($result,$i,"organizationLocation"); 

echo '<tr><td class="'.($i%2? "c1":"c2").'">'.$organizationName.'</td>';

echo '<td class="'.($i%2? "c1":"c2").'">'.$organizationLocation.'</td>';    
?> 
<script type="text/javascript">

function submitForm(form)
{
    document.getElementById(form).submit();
    return true;  
}
</script>                   
<td align="center" class="<?php echo ($i%2? "c1":"c2") ?>">
    <div style="float: left; width:30px;"> 
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=edit&subeditorganizationID=' . $organizationID?>"><img SRC="../icons/Edit.png" title="Edit Organization" ALT="Edit Organization" border="0"/></a>
    </div>
    <!--    ||||||||||||||||||   --> 
    <div style="float: left; width:30px;">
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&subdelorganizationID=' . $organizationID?>" onClick="javascript:return confirm('Are you sure you want to delete this organization?');"><img border="0" SRC="../icons/delete.gif" title="Delete Organization" ALT="Delete Organization"  ></a>
    </div>
</td>
<?php
echo "</tr>\n";
}
echo "</table><br>";
//Paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
organization::displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF']);

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

 //----------------------------------------------------------------------------------
 if(isset($_POST['subaddorganization']))
 {  
    global $form;

    $field = "organization_name";  
    if(!isset($_POST['organization_name']) || strlen(trim($_POST['organization_name'])) == 0){
    $form->setError($field, "* Please enter a valid organization name.");
    }

    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    }
    /* No errors, add the new organization to the database table*/
    else
    {
       
       if(organization::organizationDuplicateCheck($_POST['organization_name']))
       {
             $process_result="<div class='alert_msg'>An organization with this name already exists in the system.</div>";
       }
       else
       { 
           if(organization::addOrganization($_POST['organization_name'],$_POST['organization_location'],$_POST['organization_jurisdiction'],$_POST['organization_comment']))
           {
                $process_result="<div class='success_msg'>Organization added to the database successfully.</div>";
                unset($_POST);
                $page="display";
           }    
            else
                $process_result="<div class='alert_msg'>Failed to add the new organization to the database.</div>";
       }

    }
 }

 //----------------------------------------------------------------------------------
 if(isset($_GET['subdelorganizationID']))
 {  
    if(organization::queryOrganization($_GET['subdelorganizationID'])!=false)
    { 
        $deleteResult=organization::deleteOrganization($_GET['subdelorganizationID']);
        
        if($deleteResult)
            $process_result="<div class='success_msg'>The selected organization has been removed successfully.</div>";
        else
            $process_result="<div class='alert_msg'>Failed to remove the organization from the database.</div>";
    }

 }
  //----------------------------------------------------------------------------------
if(isset($_POST['subeditorganization']) && isset($_POST['subeditorganizationID']))
 {  
    global $form;
   
    $field = "organization_name";  
    if(!isset($_POST['organization_name']) || strlen(trim($_POST['organization_name'])) == 0){
    $form->setError($field, "* Please enter a valid organization name.");
    }
    
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    $page="edit"; 
    }
    /* No errors, update organization information in the database*/
    else
    {
 
       if(strcasecmp(trim($_POST['organization_name']),$_POST['original_organization_name'])!=0 && organization::organizationDuplicateCheck($_POST['organization_name']))
       {
             $process_result="<div class='alert_msg'>An organization with this name already exists in the system.</div>";
             $page="edit";
       }
       else
       { 
       
           if(organization::updateOrganization($_POST['subeditorganizationID'],$_POST['organization_name'],$_POST['organization_location'],$_POST['organization_jurisdiction'],$_POST['organization_comment']))
           {
                $process_result="<div class='success_msg'>".$_POST['organization_name'].": organization updated successfully. <a href='organization.php'>Go Back</a>...</div><br>";
                $page="edit";
           }    
            else
                $process_result="<div class='alert_msg'>Failed to update.".$_POST['organization_name']."</div>";
       }

    }
 }
//print_r($_POST)  ;

?>
<!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');   ?>
        
<?php    switch ($page){ //what page to load
		case 'display':  //begin display page?>
			<div id="displayOrganizations" class="in"> 
            <P class=veryLargeBlue>Manage Organizations</P>
				<?php if(isset($process_result)) {echo $process_result;}
				displayOrganizations();
				?>
			</div>
		<?php
		break; //end display page
		
		case 'edit': //begin edit page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Update Organization</P> 
                <?php
                if(isset($_REQUEST['subeditorganizationID']))
                {
                $organizationInfo=organization::queryOrganization($_REQUEST['subeditorganizationID']);
                ?>
                <div id="editOrganization">
                    <?php if(isset($process_result)) {echo $process_result;}?>
                    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                        <table align="left" border="0" cellspacing="0" cellpadding="3">

                        <tr>
                            <td class="c1">Organization Name:*</td>
                            <td class="c1"><input type="text"  name="organization_name" maxlength="100" size="40" value="<?php if(isset($_POST['organization_name'])) echo $_POST['organization_name']; else echo $organizationInfo->organizationName; ?>">
                            <?php  echo $form->error("organization_name"); ?></td>
                            <input type="hidden" name="original_organization_name" value="<?php echo $organizationInfo->organizationName; ?>">
                          
                        </tr>
                
                         <tr>
                            <td class="c2">Organization Location:</td>
                            <td class="c2"><input type="text"  name="organization_location" maxlength="100" size="40" value="<?php if(isset($_POST['organization_location'])) echo $_POST['organization_location']; else echo $organizationInfo->organizationLocation; ?>">
                            <?php // echo $form->error("organization_name"); ?></td>
                        </tr>
                        <tr>
                            <td class="c1">Organization Jurisdiction:</td>
                            <td class="c1"><input type="text"  name="organization_jurisdiction" maxlength="100" size="40" value="<?php if(isset($_POST['organization_jurisdiction'])) echo $_POST['organization_jurisdiction']; else echo $organizationInfo->organizationJurisdiction; ?>">
                            <?php // echo $form->error("organization_name"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Comment:</td>
                            <td class="c2"><textarea  name="organization_comment" cols="70" rows="6" ><?php if(isset($_POST['organization_comment'])) echo $_POST['organization_comment']; else echo $organizationInfo->organizationComment; ?></textarea>
                            <?php // echo $form->error("organization_comment"); ?></td>
                        </tr>
                       
                        <input type="hidden" name="subeditorganization" value="1"> 
                        <input type="hidden" name="subeditorganizationID" value="<?php echo $organizationInfo->organizationID; ?>">
                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="editOrganization" type="submit" >Update Organization</button></div> 
                               
                                <div style="float:left;"><button type="button" onclick="window.location='organization.php'">Cancel</button>
                            </td>
                        </tr>
   
                        </table>
                        </form>
           <?php } ?>
                </div>
            </div>
		<?php break; //end edit page
		case 'add':  //begin add page ?>
                <!--Add New Organization form START-->
                <div id="panel-left" class="box panel">
                <P class=veryLargeBlue>Add Organization</P> 
                <div enctype="multipart/form-data" id="addOrganization">
                <?php if(isset($process_result)) {echo $process_result;} ?>
                
                
                    <form name="addForm" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
                    <table align="left" border="0" > 
                        <tr>
                            <td class="c1" >Organization Name:*</td>
                            <td class="c1" ><input type="text"  name="organization_name" maxlength="100" size="40" value="<?php if(isset($_POST['organization_name'])) echo $_POST['organization_name']; ?>">
                            <?php  echo $form->error("organization_name"); ?></td>
                        </tr>
                        
                         <tr>
                            <td class="c2">Organization Location:</td>
                            <td class="c2"><input type="text"  name="organization_location" maxlength="100" size="40" value="<?php if(isset($_POST['organization_location'])) echo $_POST['organization_location']; ?>">
                            <?php  echo $form->error("organization_location"); ?></td>
                        </tr>
                        <tr>
                            <td class="c1">Organization Jurisdiction:</td>
                            <td class="c1"><input type="text"  name="organization_jurisdiction" maxlength="100" size="40" value="<?php if(isset($_POST['organization_jurisdiction'])) echo $_POST['organization_jurisdiction']; ?>">
                            <?php  echo $form->error("organization_jurisdiction"); ?></td>
                        </tr>
                        <tr>
                            <td class="c2">Comment:</td>
                            <td class="c2"><textarea  name="organization_comment" cols="70" rows="6" ><?php if(isset($_POST['organization_comment'])) echo $_POST['organization_comment'];?></textarea>
                           
                        </tr>
                        
                        <input type="hidden" name="subaddorganization" value="1"> 
                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="addOrganization" type="submit" >Add Organization</button></div> 
                                
                                <div style="float:left;"><button type="button" onclick="window.location='organization.php'">Cancel</button></div>
                            </td>
                        </tr>
                        </table> 
                       </form>  
            </div>
        </div>
        <!--Add New Organization form END-->
	<?php break; } //end add page and end switch page?> 
<?php require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
