<?php require_once('../Connections/conn_ama.php');
include("../../../../../../functions/functions.php");

if (!isset($_SESSION)) {
  session_start();
}

	//$curr_season = $_SESSION['curr_season'];
	//$curr_season = '2018-2019';

/*//Category Select
$query_rsCategory = "SELECT categoryID, categoryTitle FROM categories";
$rsCategory = mysqli_query($conn_ama, $query_rsCategory ) or die(mysqli_error());
$row_rsCategory = mysqli_fetch_assoc($rsCategory);
$totalRows_rsCategory = mysqli_num_rows($rsCategory);*/

// //$editFormAction = $_SERVER['PHP_SELF'];
// if (isset($_SERVER['QUERY_STRING'])) {
//   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
// }

//echo "<pre>".print_r($_FILES)."</pre>";

	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "save-add-document")) {


		define('MAX_FILE_SIZE',901212000);

			$documentcount = count($_FILES['document']['name']);

			//echo $documentcount;

			//echo $_FILES['document']['name'];

			//$newseason = trim(str_replace("/", "-", $curr_season), "'");

			//define('UPLOAD_DIR', '../documents/');
			define('UPLOAD_DIR', '../../../../../../documents/');

			// define constants
			//define('SOURCE_DIR', '../documents/');
			define('SOURCE_DIR', '../../../../../../documents/');

			/* define('THUMBS_DIR', '../documents/');
			define('MAX_WIDTH', 320);
			define('MAX_HEIGHT', 278); */
			/* define('MAX_WIDTH', 320);
			define('MAX_HEIGHT', 320); */

			for($i=0; $i<$documentcount; $i++)
			{
				$file = trim(str_replace(" ","_",$_FILES['document']['name'][$i]));

				$kilobytes = MAX_FILE_SIZE/1024;
				$formatted = number_format($kilobytes, 1);
				$max = $formatted."KB";
				$permitted = array('application/pdf');
				$sizeok = false;
				$typeok = false;
				if($_FILES['document']['size'][$i] > 0 && $_FILES['document']['size'][$i] <= MAX_FILE_SIZE)
					{
						$sizeok = true;
					}
					foreach($permitted as $type){
						if($type==$_FILES['document']['type'][$i])
						{
							$typeok = true;
							break;
						}
					}
				if($sizeok && $typeok)
				{
				switch($_FILES['document']['error'][$i]){
				case 0:
					include('_insert-document.inc.php');
					break;
					case 3:
					$result = "Error uploading $file. Please try again";
					default:
					$result = "System error uploading $file. Please contact webmaster";
					}
				}
				elseif($_FILES['document']['error'][$i]==4)
				{
				$result = "No file selected";
				}
				else{ $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file type:
				gif, jpeg, png, tiff";}
			}

			if(isset($result)){ echo $result;}
	}

?>
