<?
	$g_path = '../my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."html/c_time.php";
	include $g_path."u_spoj_na_bazu.php";
	
	include $g_path."u_headers.php";
	
	if(!$_SESSION['__id']){
		//exit;
	}
	
	
	if($_POST){
		if(strstr($_SERVER['HTTP_REFERER'], $g_path_sl.'res/document_upload')){
			$l_file = $_FILES['imgFile']['tmp_name'];
			$l_size = $_FILES['imgFile']['size'];
			$l_name = $_FILES['imgFile']['name'];
			
			$l_dest_name = "";
			
			
			if(strstr($l_name, '.')){
				$la_name = explode(".", $l_name);
				for($i = 0; $i < sizeof($la_name) - 1; $i++){
					$l_dest_name .= $la_name[$i];
				}
				$l_dest_name .= "-".u_datum(0, 1).u_sat().u_minuta().u_sekunda().rand(10000, 99999).".".$la_name[sizeof($la_name) - 1];
			}else{
				$l_dest_name .= $l_name."-".u_datum(0, 1).u_sat().u_minuta().u_sekunda().rand(10000, 99999).".jpg";
			}
			
			/*
			$sql = 'update zaposlenik set image = "'.$l_dest_name.'" where guid = "'.$_GET['id'].'"';
			$result = sql_exe($sql, true);
			*/
			
			$l_dest = "../res/documents/files/1/".$l_dest_name;
			
			/*
			print $l_file."<br>";
			print $l_dest."<br>";
			exit;
			*/
			
			$is_ok = move_uploaded_file($l_file, $l_dest);
			/*
			if($is_ok){
				kreiraj_malu_fotografiju_4($l_dest, $ga_slika_smanji_x, $ga_slika_smanji_y);
			}
			*/
	
			header("location:document_upload.php?upload=done&rbr=".$_GET['rbr']."&destname=".$l_dest_name."&name=".$l_name.
				"&activeDocument=".$_GET['activeDocument']."&redak=".$_GET['redak']);
			exit;
		}else{
			print "error 9875";
			exit;
		}
	}
	
	
	
	switch($_GET['mode']){
		case 'imageBox':
			$la_msg['insertImage'] = 'Select uploaded image';
			break;
			
		default:
			$la_msg['insertImage'] = 'Insert image as a background';
	}
	


	if($_GET['upload'] == 'done'){
?>
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
		<html>
		<head>
			<link rel='stylesheet' type='text/css' href="css/image_upload.css">
			<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-image-upload-2.js"></script>
		</head>
		<body>
			Upload is done.
			<a href="#" onclick="new_document_close('<?=$_GET['rbr']?>', '<?=$_GET['destname']?>', '<?=$_GET['name']
				?>', '<?=$_GET['activeDocument']?>', <?=$_GET['redak']?>); return false;">Potvrdi unos dokumenta i zatvori prozor</a>
			<br>
			<br>
		</body>
		</html>
<?
	}else{
		ob_start();
?>
			<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
			<html>
			<head>
				<link rel='stylesheet' type='text/css' href="css/image_upload.css">
				<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-image-upload-2.js"></script>
			</head>
			<body>
				<form name='fuForma' method='post' action='document_upload.php?id=<?=$_GET['id']?>&rbr=<?=$_GET['rbr']
					?>&activeDocument=<?=$_GET['activeDocument']?>&redak=<?=$_GET['redak']?>' enctype='multipart/form-data'>
					<input type="hidden" name="MAX_FILE_SIZE" value="4194304">
					Odaberite datoteku koju želite dodati<br>
					<br>
					<input type='file' name='imgFile' value=''><br>
					<br>
					<center><button title="Upload image" onclick="document.fuForma.submit()">Upload</button></center>
					<br><br>
					<a href="#" onclick="new_document_close('<?=$_GET['rbr']?>', '', '', '', ''); return false;">Zatvori prozor</a>
				</form>
			</body>
			</html>
<?
		$output = ob_get_contents();
		ob_end_clean();
		print  $output;
	}
?>