<!DOCTYPE html PUBLIC "-//W3C//Dth XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/Dth/xhtml1-transitional.dth">

<head> 

<title>Suggestion</title>
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="style.css"/>   
<script type="text/javascript" src="animatedcollapse.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript">

    
animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
//$: Access to jQuery
//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
//state: "block" or "none", depending on state
}
    var num=0; //count object
    animatedcollapse.init()

</script>
<style>
td {
   text-align: left;
}
.c1 {
    background-color:#F8F8F8;
}
</style>
<?php
require_once("include/db.class.php"); 
require_once("include/constants.php");
 /* if(!session_id()) session_start();

  header('Cache-control: private, no-cache, must-revalidate');
  header('Expires: 0'); */
  
function getCategory(){
    $db = new DB();
    $mysqlString= $db->query("SELECT * FROM SuggestionCategory");
    return $mysqlString;
}
function decryptAppID($appid){
    $db = new DB();
    $mysqlString= $db->query("SELECT * FROM Application WHERE appCode='".$appid."'");
    if(mysql_numrows($mysqlString)>0){
        $appid=mysql_result($mysqlString, 0, "appID");
        return $appid;
    }else{
        return false;
    }
}
function defaultSuggestion(){
    $db = new DB();
    $mysqlString= $db->query("SELECT * FROM ConfigTable WHERE settingName='defaultPublicOrPrivate'");
    if(mysql_numrows($mysqlString)>0){
        $default_setting=mysql_result($mysqlString, 0, "settingValue");
        return $default_setting;
    }
}
function displayPageNav($totalRows,$limit,$pageNum,$pageAddr, $appid, $appsubid)
{

        if (($totalRows%$limit)<>0) {
            $pmax=floor($totalRows/$limit)+1;  // Divide to total result by the number of query you want
        // to display per page($limit) and create a Max page
        } else {
            $pmax=floor($totalRows/$limit);
        }
        if($pmax==1)  //if there is only 1 page, no need to display page number
            return;
            
        echo "<div class='navpage'>"; 

        // We need to create a Previous page, so we need $pageNum to be bigger than 1, otherwise at the first page we would get a 0.
        //For the previous to show up we need at least to be at the second page.
        if($pageNum > 1) {
            $prevp="<a href='".$pageAddr."?pageNum=".($pageNum-1)."&appid=".$appid."&appsubid=".$appsubid."&viewSuggestion=true' title='Previous Page'>Previous Page</a>&nbsp;&nbsp;"; 
            echo $prevp;
        } else {echo "";}   

        // Here We want create a page from the results we got by dividing the total by the the limit. So let say you got
        //45 results and you want 5 results per page; these simple lines will create you 8 pages.
        $pid=1;
        while ($pid<=$pmax) {
            $paging= "<a href='".$pageAddr."?pageNum=$pid&appid=".$appid."&appsubid=".$appsubid."&viewSuggestion=true' title='Page $pid of $pmax'>$pid</a>&nbsp;&nbsp;";
        //So here, let say we are at the 3rd page and we want the 3 to be blank so the user can know where is he now.
        //this will act as Current Page! We need to replace the url by a text
            if($pid==$pageNum)
                echo "<span>$pageNum</span>&nbsp;&nbsp;";
            else 
                echo $paging; 
            $pid++;  // create pages until reach the result
        } 
        //We want to create a next page and a last page, $pageNum have to be less than $pmax.
        if($pageNum < $pmax) {
            $nextp="<a href='".$pageAddr."?pageNum=".($pageNum+1)."&appid=".$appid."&appsubid=".$appsubid."&viewSuggestion=true' title='Next Page'>Next Page</a>&nbsp;&nbsp;"; 
            echo $nextp;
        } else {echo "";}
        if($pageNum != $pmax) {      
            echo  "<a href='".$pageAddr."?pageNum=$pmax&appid=".$appid."&appsubid=".$appsubid."&viewSuggestion=true' title='Last Page'>Last Page</a>"; }
        
        echo "</div>";
}
function viewSuggestion($appid, $subappid, $pageNum=NULL, $limit=NULL){
    
     $db = new DB();

    if(!is_null($pageNum) && !is_null($limit)){
        $mysqlString="SELECT * FROM Suggestion WHERE appID='".$appid."' and appsubLocationID='" .$subappid."' and enablePublicView='1' ORDER BY submissionDateTime ASC LIMIT ".(($pageNum-1)*$limit).", $limit";
        $suggestion=$db->query($mysqlString);
    }else{
        $mysqlString="SELECT * FROM Suggestion WHERE appID='".$appid."' and appsubLocationID='" .$subappid."' and enablePublicView='1' ORDER BY submissionDateTime ASC";
        $suggestion= $db->query($mysqlString);
    }
    
    return $suggestion;
}

//---------------------------------------------------------------------------------------------------------
$state_list = array('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 
                    'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 
                    'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 
                    'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 
                    'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 
                    'Wyoming');

$user="";
$email="";
$suggestion="";
$descript="";
$codeCheck="";
$error = array();
$emailCheck="";
$city="";
$state="";
$zipcode="";
$appid="";
$appsubid="";
$statusNofitied="";
$categoryError="";

if(isset($_GET['appid'])){
	$appid=$_GET['appid'];
}else{
	$errorid="<font color='red'>Unknown Application...</font>";
}
if(isset($_GET['appsubid'])){
	$appsubid=$_GET['appsubid'];
}else{
	$errorid= "<font color='red'>Unknown Application Location...</font>";
}

if(isset($_GET['error'])){
	if(isset($_GET['userName']))$user=$_GET['userName'];
	if(isset($_GET['email']))$email=$_GET['email'];
	if(isset($_GET['suggestion']))$suggestion=$_GET['suggestion'];
	if(isset($_GET['description']))$descript=$_GET['description'];
	if(isset($_GET['emailCheck']))$emailCheck=$_GET['emailCheck'];
	if(isset($_GET['city']))$city=$_GET['city'];
	if(isset($_GET['state']))$state=$_GET['state'];
	if(isset($_GET['zipcode']))$zipcode=$_GET['zipcode'];
    if(isset($_GET['category']))$categoryError=$_GET['category'];
	if(isset($_GET['statusNofitied']))$statusNofitied=$_GET['statusNofitied'];
    $user=replace_quotes($user);
    $email=replace_quotes($email);
    $suggestion=replace_quotes($suggestion);
    $descript=replace_quotes($descript);
    $emailCheck=replace_quotes($emailCheck);
    $city=replace_quotes($city);
    $state=replace_quotes($state);
	$GET_error=$_GET['error'];
	$error=explode(",", $GET_error);
	for($i=0;$i<count($error);$i++){
		if($error[$i]=="ErrorAppID"){
			break;
		}
	}
}
function replace_quotes($str){
    $str=stripslashes($str);
    return $str=str_replace("\'", "'",$str);
}

?>

</head>
<BODY>
<center>

<?php 

if(!isset($errorid) && !isset($_GET['viewSuggestion']))
{
    $defaultSetting=defaultSuggestion();
    ?>

<TABLE border=0 cellSpacing=0 cellPadding=10 width="100%" bgColor=#000000>
    <TR>
        <th align="right" height=20 background=images/black_dot.gif width="5%" colSpan=3 ><FONT color="#cccccc">
            <A class=charcoalHeader  href="suggestion.php?appid=<?php echo $appid ?>&appsubid=<?php echo $appsubid?>">NEW SUGGESTION</A></FONT></th> 
        <th align="left" height=20 background=images/black_dot.gif width="5%" colSpan=3><FONT color=#cccccc>
        <?php
            if($defaultSetting=="Private"){
                ?>
                <A class=deadLinkHeader href="#" title="Not available now">VIEW SUGGESTIONS</A></FONT></th> 
                <?php
            }else{
                ?>
                <A class=charcoalHeader href="suggestion.php?appid=<?php echo $appid ?>&appsubid=<?php echo $appsubid?>&viewSuggestion=true">VIEW SUGGESTIONS</A></FONT></th> 
                <?php
            }
        ?>
            
   </TR>
</TABLE>

<form action="suggestion_class.php" method="post">
	<p class="veryLargeBlue">New Suggestion</p>
    
	<table cellspacing="0" cellpadding="3" border="3" >
		<tr>
         <td class="c2">
         </td>
			<td class="c2"  align="center">
				<p class="blue" >Personal Information</p>
				
				<?php
					if(isset($_GET['error'])){
						print "<font color='red'>Submission failed. Please try again!</font>";
					}
				?>
			</td>
        
		</tr>
		<tr>
			<td class="c1">
			<?php  in_array("name",$error) ? print "<font color='red'> Name* </font>" : print "Name*"; ?>
			</td>
			<td class="c1">
				<input type="text" id="nameText" name="nameText" size="60" value="<?php echo $user; ?>" />
			</td>
		</tr>
		<tr>
			<td  class="c1">
            <?php  in_array("email",$error) ? print "<font color='red'> E-Mail* </font>" : print "E-Mail*"; ?>
			</td>
			
				<td  class="c1">
				<input type="text" id="emtilText" name="emailText" size="60" value= "<?php echo $email; ?>" />
				</td>
			</tr>
			<tr>
				<td class="c1">
                 <?php  in_array("email",$error) ? print "<font color='red'>Repeat E-Mail* </font>" : print " Confirm e-mail*"; ?>  
	
				</td>

			<td  class="c1">
				<input type="text" id="emailCheckText" name="emailCheckText" size="60" value="<?php echo $emailCheck; ?>" />
			</td>
		</tr>
		
		<tr>
			<td class="c1">
				City
			</td>
            <td class="c1">
                <input type="text" name="cityText" id="cityText" value="<?php echo $city; ?>" />
            </td>
        </tr>
        <tr>
			<td class="c1">
				State
			</td>
            <td class="c1">
                <select name="state" >
                    <option>--SELECT--</option>
                <?php
                
                    for($i=0;$i<count($state_list);$i++){
                        if($state==$state_list[$i]){
                        ?>
                            <option value="<?php echo $state_list[$i]; ?>" selected > <?php echo $state_list[$i]; ?></option>
                        <?php
                        }else{
                        ?>
                            <option value="<?php echo $state_list[$i]; ?>" > <?php echo $state_list[$i]; ?></option>
                        <?php
                        }

                    }
                ?>
                </select>
            </td>
        </tr>
        <tr>
			<td  class="c1">
             <?php  in_array("zip",$error) ? print "<font color='red'>Zip Code </font>" : print "Zip Code"; ?>    
	
			</td>
			
			<td class="c1">
				<input type="text" name="zipText" id="zipText" size="5" maxlength="5" value="<?php echo $zipcode; ?>" />
			</td>
		</tr>
		<tr>
			 <td class="c2">
            </td>
            <td  class="c2" align="center">
                <p class="blue">Suggestion</p>
					
			</td>
		</tr>
	    
					<tr>
						<td class="c1" >
                        <?php  in_array("category",$error) ? print "<font color='red'> Category* </font>" : print "Category*"; ?> 
            
                        </td>
                        <td class="c1">
                        	<select name="category">
                            <option value="-1">--SELECT--</option>
                            <?php
                                $category=getCategory();
                                for($i=0;$i<mysql_numrows($category);$i++){
                                    $categoryName  = mysql_result($category,$i,"categoryDescription");
                                    $categoryid=mysql_result($category,$i,"categoryID");
                                    if($categoryError==$categoryid){
                                        echo "<option value=".$categoryid." selected>".$categoryName."</option>";
                                    }else{
                                        echo "<option value=".$categoryid.">".$categoryName."</option>";
                                    }
                                }
                            ?>
							</select>
                       
						</td>
					</tr>
				
                    <tr>
           
            <td class="c1">
            <?php  in_array("subject",$error) ? print "<font color='red'> Subject* </font>" : print "Subject*"; ?> 

                        </td>
            
                        <td  class="c1">
                            <input type="text" name="suggestionText" id="suggestionText" size="60" value="<?php echo $suggestion; ?>" />
                        </td>
                    </tr>
					<tr>
					<td class="c1">
                    <?php  in_array("description",$error) ? print "<font color='red'> Description* </font>" : print "Description*"; ?> 
			</td>
						<td  class="c1">
							<textArea cols="44" rows="5" name="description" id="description"><?php echo $descript?></textArea>
						</td>
			</tr>
                <tr>
                        <td class="c1">
                        Notification Request:
                        </td>
                        <td class="c1">
                            <?php
                                if($statusNofitied=="1"){
                                    print "<input type='checkbox' name='statusNotice' value='1' checked >I want to get notified of Suggestion Status";
                                }else{
                                    print "<input type='checkbox' name='statusNotice' value='1' >I want to get notified of Suggestion Status";
                                }
                            ?>
                        </td>
                    </tr>
			<tr>
			<td class="c1">
            <?php  in_array("code",$error) ? print "<font color='red'> Security Code* </font>" : print "Security Code*"; ?> 

						
                        <img src="SecurityCode/seccode.php" width="71" height="21" align="absmiddle">
                </td>
				<td class="c1">
							<input type="text" name="security" size="5" maxlength="6" />
				</td>
				</tr>
				<tr>
						 <td class="c2"></td>
                        <td  class="c2" align="center">
							<input type="hidden" name="appid" value="<?php echo $appid ?>" />
							<input type="hidden" name="appsubid" value="<?php echo $appsubid ?>"  />
							<input type="submit" name="submit" value="Submit" />
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>

</form>
<?php
}else if(isset($_GET['viewSuggestion'])==true && defaultSuggestion()=="Public"){
?>

<TABLE border=0 cellSpacing=0 cellPadding=10 width="100%" bgColor=#000000>
    <TR>
        <th align="right" height=20 background=images/black_dot.gif width="5%" colSpan=3 ><FONT color="#cccccc">
            <A class=charcoalHeader  href="suggestion.php?appid=<?php echo $appid ?>&appsubid=<?php echo $appsubid?>">NEW SUGGESTION</A></FONT></th> 
        <th align="left" height=20 background=images/black_dot.gif width="5%" colSpan=3><FONT color=#cccccc>
            <A class=charcoalHeader href="suggestion.php?appid=<?php echo $appid ?>&appsubid=<?php echo $appsubid?>&viewSuggestion=true">VIEW SUGGESTIONS</A></FONT></th> 
   </TR>
</TABLE>
<?php
    
    $appid=$_GET['appid'];
    $subappid=$_GET['appsubid'];
    $decryptappid=decryptAppID($appid);
    if($decryptappid!=false){
        $suggestion=viewSuggestion($decryptappid, $subappid);
        $nrResults = mysql_numrows($suggestion);

        echo "<p class='veryLargeBlue'>Suggestion List</p>";
        
        
        if($nrResults>0){
            
            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;
    
            $limit=20; // Limit of result per page 
            
            $suggestion=viewSuggestion($decryptappid, $subappid, $pageNum, $limit);
            $num_rows = mysql_numrows($suggestion);
            
            echo "<table border=0 width=800>";
            echo "<tr><td></td><td>Subject</td><td>Submitter Name</td><td>Submission Date</td><td width=30>Solution</td></tr>";
                            ?>

                <?php
            for($i=0;$i<$num_rows;$i++){
                ?>
                <script type="text/javascript">
                    
                    animatedcollapse.addDiv('jason'+num, 'fade=1,height=270dpx')
                    num+=1;
                </script>
                <?php
                
                echo '<tr><td width=30 class="'.($i%2? "c1":"c2").'"><a href="javascript:animatedcollapse.show('."'jason".$i."'".')"><img src=images/arrowdown.png></a></img></td>';
                echo '<td class="'.($i%2? "c1":"c2").'">'.mysql_result($suggestion, $i, "suggestionSubject")."</td>";
                echo '<td class="'.($i%2? "c1":"c2").'">'.mysql_result($suggestion, $i, "submitterName")."</td>";
                echo '<td class="'.($i%2? "c1":"c2").'">'.mysql_result($suggestion, $i, "submissionDateTime")."</td>";
                if(is_null(mysql_result($suggestion, $i, "suggestionDisposition")) or trim(mysql_result($suggestion, $i, "suggestionDisposition"))==''){
                    echo '<td align=center class="'.($i%2? "c1":"c2").'"><img src="images/NoSolution.png"></img></td></tr>';
                
                }else{
                    echo '<td align=center class="'.($i%2? "c1":"c2").'"><img src="images/Solution.png"></img></td></tr>';
                }
                
                echo '<tr><td></td><td colspan="4">';
                //*************description & Solution display
                echo '<div id="jason'.$i.'" style="width:540px; display:none">';
                echo '<table border="1"><tr><td align="center">Description</td><td align="center">Solution</td></tr>';
                echo '<tr><td class="'.($i%2? "c1":"c2").'"><div style="height: 200px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">'.mysql_result($suggestion, $i, "suggestionDescription")."</div></td>";
                
                if(is_null(mysql_result($suggestion, $i, "suggestionDisposition")) or trim(mysql_result($suggestion, $i, "suggestionDisposition"))==''){
                    echo '<td class="'.($i%2? "c1":"c2").'"><div style="height: 200px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;"><p>This subject has not solved yet</p>';
                }else{
                    
                    echo '<td class="'.($i%2? "c1":"c2").'"><div style="height: 200px;width:350px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">'.mysql_result($suggestion, $i, "suggestionDisposition")."</div></td></tr>";
                }
                echo '<tr><td colspan="2" class="'.($i%2? "c1":"c2").'"><a href="javascript:animatedcollapse.hide('."'jason".$i."'".')"><img src="images/arrowup.png"></img></a></td></tr>';
                echo '</table>';
                echo '</div>';
                //*********************
                echo '</td></tr>';
                //echo '<td class="'.($i%2? "c1":"c2").'">'.mysql_result($suggestion, $i, "suggestionDescription")."</td></tr>";
            }
            echo "</table>";
            //Paging code below
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
            displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF'], $appid, $subappid);

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
        }else{
            echo "Suggestions are not available for Public View at this time. <a href='suggestion.php?appid=".$appid."&appsubid=".$appsubid."'>Click here to go back</a>";
        }
    }else{
        echo "<font color='red' size='15'>" .$errorid. "</font>";
    }
    
}else{
    $errorid="<font color='red'>Viewing suggestion is Not available now</font>";
    echo "<font color='red' size='15'>" .$errorid. "</font>";
}

?>
</center>

</BODY>
</html>
