<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}


//require_once ('mysql_connect.php');
$action          = $_GET["a"];
$delete			= $_GET["rem"];
$page			= $_GET["showaspage"];

//delete file if selected
if(!empty($delete)){
	$myFile = "../docs/". $delete;
	$myFile = urldecode($myFile);
	//die($myFile);
	unlink($myFile);
	//make sure file is removed from navdocs table
	$sql = "Delete from navdocs where docname='".mysql_real_escape_string($delete)."'";
	mysql_query($sql);
	echo '<div align="center"><font color="red">File "' . $delete . '" has been removed.</font></div>';
}

if(!empty($page)){
	//check if page listed in navdocs
	$pageqry="Select * from navdocs where docname = '" . $page ."'";
	$rt=mysql_query($pageqry); 
	if (mysql_num_rows($rt) == 0) {  //if not listed, add now
		//get highest orderNum
		$numqry="Select max(orderNum) as highnum from navdocs";
		$result=mysql_query($numqry);
		if (mysql_num_rows($result) > 0) {
			while($nt=mysql_fetch_array($result)){
				$highnum=$nt[highnum]+1;				
			}
		} else {
			$highnum=0;
		}	
		$actionqry = "insert into navdocs (docname, orderNum) values ('" . mysql_real_escape_string($page) ."', '$highnum')";
		mysql_query($actionqry); 
	} else {	//if listed, delete now
		$actionqry = "Delete from navdocs where  docname = '" . mysql_real_escape_string($page) ."'";
		mysql_query($actionqry); 
	}
	
	mysql_close;
	
}
	//echo 'Action = ' . $action; (for testing)
//require_once ('config.inc');
if($action=="upload"){
	$target = "../docs/"; 
	$target = $target . basename( $_FILES['uploaded']['name']) ; 
	//echo 'target=' . $target;
	$ok=1; 
	//check file size, allow if under 5MB
	if ($uploaded_size > 5000000)
	{
		echo "Your file is too large.<br>"; 
		$ok=0;
	} 
	//This is our limit file type condition 
	if ($uploaded_type =="text/php") 
	{ 
		echo "No PHP files<br>"; 
		$ok=0; 
	}
	//if (!($uploaded_type=="application/pdf")) {
	//	echo "You may only upload PDF files.<br>";
	//		$ok=0;
	//}	
	
	
	if(($ok==1)&&(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))) {
	//echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
		echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
	} else {
		echo "Sorry, there was a problem uploading your file.";
	}
}



	
	

echo '<div align="center"><i>Only PDF files supported at this time.</i><br>';
 echo '<form enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'?a=upload" method="POST">
 Please choose a PDF file to upload (5MB max):<br> <input name="uploaded" type="file" /><br />
 <input id="save-button" type="submit" value="Upload" />
 </form> ';

echo '<br><br><strong>Saved Documents:</strong><br />
<div id="docs-wrapper" align="center"><table width=70% cellspacing="0">
<tr style="background-color:#0f5f81;color:#fff;"><td style="padding:5px;-webkit-border-radius: 6px 0px 0px 0px; -moz-border-radius: 6px 0px 0px 0px; border-radius: 6px 0px 0px 0px;">
Document name</td><td><!--Show as page--></td><td style="text-align:right; padding:5px;-webkit-border-radius: 0px 6px 0px 0px; -moz-border-radius: 0px 6px 0px 0px; border-radius: 0px 6px 0px 0px;">Remove from library &nbsp; </td></tr>';

if ($handle = opendir('../docs')) {

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			echo "<tr><td><a href='../docs/".$file."' target='blank'><font color='blue'> " . $file . " </font></a> 
			<td>";
				
	//check if file listed in navdocs
	/*
	$pageqry="Select * from navdocs where docname = '" . $file ."'";
	$rt=mysql_query($pageqry); 
	if (mysql_num_rows($rt) == 0) {  //if not listed, add now
		echo '<font color="red">No</font>'; 
	} else {	//if listed, delete now
		echo '<font color="green">Yes</font>';
	}
	*/
	mysql_close;
	

			 
			echo"<!--<a href='".$_SERVER['PHP_SELF']."?showaspage=".$file."'><font color='blue'> - (Change)</font></a>-->
			<td style='text-align:right;'><span class='button'><a href='".$_SERVER['PHP_SELF']."?rem=".urlencode($file)."'>(remove)</a></span>" ;
		}
    }



    closedir($handle);
}

//cleanup...remove any files in navdocs table if they don't exist in docs folder
  

echo'</table></div>';


	

echo '<br>
		
		</div></table></div>';
/* testing...	
$docqry="Select * from navdocs";
$rt=mysql_query($docqry); 
echo"<ul id='menusort'>";
$x=0;
while($nt=mysql_fetch_array($rt)){
	$id="page".$x;
	echo'<li id="'.$id.'" name="'.$id.'">'.$nt[docname].'</li>';
	$x++;
}	
echo"</ul>";
*/
?>
<!--
<ul id="sortable">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>
 -->
<script>$("#menusort").sortable({
    axis: 'y',
    stop: function (event, ui) {
        var data = $(this).sortable("serialize").toString();
		var idsInOrder = $(this).sortable("toArray").toString();
        //var datastring = $("#sortable").serialize();
		//alert($(this).sortable('serialize'));
		alert(idsInOrder);
		//$( "#results" ).text( idsInOrder );
		var elem = document.getElementById("results").value = idsInOrder;
		//elem.value = idsInOrder;
        // POST to server using $.post or $.ajax
        $.ajax({
            data: data,
            type: 'POST',
            url: ''
        });
    }
});
</script>
<!--<p><input type="text" name="results" id="results" value=""></p>-->