<?php
include("../include/session.php");  
include("appSubLocation.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;
        
/**
* displayApps - Displays the app database table in
* a nicely formatted html table.
*/
function displayAppSubLocations($appID){
    global $pageNum;
    if(trim($appID)=='')          //Make sure appID isn't blank
    {
        echo "Incorrect application ID";
        exit;
    }
    
//paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
$result=appSubLocation::showAppSubLocations($appID);
$nrResults = mysql_numrows($result);

$limit=20; // Limit of result per page 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
$result = appSubLocation::showAppSubLocations($appID,$pageNum,$limit);
$num_rows = mysql_numrows($result);

echo "<p><img  SRC=\"../icons/add.png\" >&nbsp;&nbsp;&nbsp;<a href='appSubLocation.php?page=add&appID=".$appID."'>Add New Application Sub Location</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>Application Name</b></th><th><b>Sub Location ID</b></th><th><b>Sub Location Description</b></th><th><b>Suggestion URL</b></th><th width='100px'><b>Action</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$appSubLocationID  = mysql_result($result,$i,"appSubLocationID");
$appName  = mysql_result($result,$i,"appName");
$appSubLocationDesc  = mysql_result($result,$i,"appSubLocationDescription");
$appCode= mysql_result($result,$i,"appCode");  

echo '<tr><td class="'.($i%2? "c1":"c2").'">'.$appName.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$appSubLocationID.'</td>'; 
echo '<td class="'.($i%2? "c1":"c2").'">'.$appSubLocationDesc.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.webBaseURL.'suggestion.php?appid='.$appCode.'&appsubid='.$appSubLocationID.'</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&subeditappsublocationID=' . $appSubLocationID.'&appID='.$appID; ?>"><img SRC="../icons/Edit.png" title="Edit App Sub Location" ALT="Edit App Sub Location" border="0"/></a>
    </div>
    <!--    ||||||||||||||||||   --> 
    <div style="float: left; width:30px;">
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&subdelappsublocationID=' . $appSubLocationID.'&appID='.$appID;?>" onClick="javascript:return confirm('Are you sure you want to delete this sub location?');"><img border="0" SRC="../icons/delete.gif" title="Delete App Sub Location" ALT="Delete App Sub Location"  ></a>
    </div>
</td>
<?php
echo "</tr>\n";
}
echo "</table><br>";
//Paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
appSubLocation::displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF']);

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

 //----------------------------------------------------------------------------------
 if(isset($_POST['subaddappsublocation']) && isset($_POST['appID']) && $_POST['appID']!='')
 {  
    global $form;

    $field = "description";  
    if(!isset($_POST['description']) || strlen(trim($_POST['description'])) == 0){
    $form->setError($field, "* Please enter some description for this sub location.");
    }

    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    $page="add";
    }
    /* No errors, add the new application sub location to the database table*/
    else
    {
       
       if(!appSubLocation::isValidAppID($_POST['appID']))
       {
             $process_result="We were unable to verify the application for which you are adding this sub location.";
       }
       else
       { 
           if(appSubLocation::addAppSubLocation($_POST['appID'],$_POST['description'],$_POST['comment'],$_POST['address']))
           {
                $process_result="Application sub location added to the database successfully.";
                unset($_POST);
                $page="display";
           }    
            else
                $process_result="Failed to add the new application sub location to the database.";
                $page="add";
       }

    }
 }

 //----------------------------------------------------------------------------------
 if(isset($_GET['subdelappsublocationID']))
 {  
    if(appSubLocation::queryAppSubLocation($_GET['subdelappsublocationID'])!=false)
    { 
        $deleteResult=appSubLocation::deleteAppSubLocation($_GET['subdelappsublocationID']);
        
        if($deleteResult)
            $process_result="The selected application sub location has been removed successfully.";
        else
            $process_result="Failed to remove the application sub location from the database.";
    }

 }
  //----------------------------------------------------------------------------------
if(isset($_POST['subeditappsublocation']) && isset($_POST['subeditappsublocationID']))
 {  
    global $form;
   
    $field = "description";  
    if(!isset($_POST['description']) || strlen(trim($_POST['description'])) == 0){
    $form->setError($field, "* Please enter some description for this sub location.");
    }
    
    /* 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 application information in the database*/
    else
    { 
       
           if(appSubLocation::updateAppSubLocation($_POST['subeditappsublocationID'],$_POST['description'],$_POST['comment'],$_POST['address']))
           {
                $process_result= "Application sub location updated successfully. <a href='appSubLocation.php?appID=".$_POST['appID']."'>Go Back</a>...";
                $page="edit";
           }    
            else
                $process_result="Failed to update.";
    }

 }

?>
<!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="displayApp" class="in"> 
            <P class=veryLargeBlue>Manage Application Sub Locations</P>
				<?php if(isset($process_result)) {echo $process_result;}
				if(isset($_GET['appID'])) {
                        displayAppSubLocations($_GET['appID']);
                }
                else  {
                        echo "Unable to identify the application for which you want to display subLocation table.";
                        exit;
                }
				?>
			</div>
		<?php
		break; //end display page
		
		case 'edit': //begin edit page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Update Application Sub Location</P> 
                <?php
                if(isset($_REQUEST['subeditappsublocationID']))
                {
                $appSubLocationInfo=appSubLocation::queryAppSubLocation($_REQUEST['subeditappsublocationID']);
                ?>
                <div id="editApp">
                    <?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">Application Name:</td>
                            <td class="c1"><?php echo $appSubLocationInfo->appName; ?></td>
                           
                            <input type="hidden" name="subeditappsublocationID" value="<?php echo $_REQUEST['subeditappsublocationID']; ?>"> 
                            <input type="hidden" name="appID" value="<?php echo $appSubLocationInfo->appID; ?>" >   
                        </tr>

                         <tr>
                            <td class="c2">App Sub Location Description:*</td>
                            <td class="c2"><textarea  name="description" cols="70" rows="6" ><?php if(isset($_POST['description'])) echo $_POST['description']; else echo $appSubLocationInfo->appSubLocationDescription; ?></textarea>
                            <?php  echo $form->error("description"); ?></td>
                        </tr>
                            <tr>
                            <td class="c1">App Sub Location Http Address:</td>
                            <td class="c1"><textarea  name="address" cols="70" rows="3" ><?php if(isset($_POST['address'])) echo $_POST['address']; else echo $appSubLocationInfo->appSubLocationHttp; ?></textarea>
                           
                        </tr>
                        <tr>
                            <td class="c2">Comment:</td>
                            <td class="c2"><textarea  name="comment" cols="70" rows="6" ><?php if(isset($_POST['comment'])) echo $_POST['comment']; else echo $appSubLocationInfo->appSubLocationComment; ?></textarea>
                            <?php // echo $form->error("comment"); ?></td>
                        </tr>
                       
                        <input type="hidden" name="subeditappsublocation" value="1"> 

                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="editAppSubLocation" type="submit" >Update Application Sub Location</button></div> 
                               
                                <div style="float:left;"><button type="button" onclick="window.location='appSubLocation.php?appID=<?php echo $appSubLocationInfo->appID; ?>'">Cancel</button>
                            </td>
                        </tr>
   
                        </table>
                        </form>
           <?php } ?>
                </div>
            </div>
		<?php break; //end edit page
		case 'add':  //begin add page ?>
                <!--Add New App Sub Location form START-->
                <div id="panel-left" class="box panel">
                <P class=veryLargeBlue>Add Application Sub Location</P> 
                <div enctype="multipart/form-data" id="addApp">
                <?php if(isset($process_result)) {echo $process_result;} ?>
                
                
                    <form name="addForm" action="<?php $_SERVER['PHP_SELF'];?>" method="POST">
                    <table align="left" border="0" > 
                    <?php
                        if(isset($_GET['appID']))
                        {
                            $appDetail=appSubLocation::queryApp($_GET['appID']); 
                        ?>
                          <input type="hidden" name="appID" value="<?php echo $_GET['appID'] ?>" > 
                        <tr>
                            <td class="c2">Organization Name>>App Name:</td>
                            <td class="c2"><?php echo $appDetail->organizationName." >> ".$appDetail->appName; ?></td>
                           
                        </tr>
                        <?php
                  
                        }
                        ?>
                        <tr>
                            <td class="c1" >App Sub Location Description:*</td>
                            <td class="c1"><textarea  name="description" cols="70" rows="6" ><?php if(isset($_POST['description'])) echo $_POST['description'];?></textarea>
                            <?php  echo $form->error("description"); ?></td>
                        </tr>
                   
                        <tr>
                            <td class="c2">App Sub Location Http Address:</td>
                            <td class="c2"><textarea  name="address" cols="70" rows="3" ><?php if(isset($_POST['address'])) echo $_POST['address'];?></textarea>
                           
                        </tr>
                        
                        <tr>
                            <td class="c1">Comment:</td>
                            <td class="c1"><textarea  name="comment" cols="70" rows="6" ><?php if(isset($_POST['comment'])) echo $_POST['comment'];?></textarea>
                           
                        </tr>
                        
                        <input type="hidden" name="subaddappsublocation" value="1"> 
                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="addApp" type="submit" >Add App Sub Location</button></div> 
                                
                                <div style="float:left;"><button type="button" onclick="window.location='appSubLocation.php?appID=<?php echo $_GET['appID'];?>'">Cancel</button></div>
                            </td>
                        </tr>
                        </table> 
                       </form>  
            </div>
        </div>
        <!--Add New App form END-->
	<?php break; } //end add page and end switch page?> 
<?php require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
