<?
	header("Content-Type: text/html; charset=utf-8");
	
	$g_path = '../my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.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/dist_list_edit.php')){
			
			$l_sql = 'delete from distribucijska_lista_primatelji where distribucijska_lista_guid = "'.$_GET['id'].'" ';
			sql_exe($l_sql);
					
			$x = get_data('select guid, DisplayName from zaposlenik order by DisplayName ', 1);
			for($i = 0; $i < sizeof($x['guid']); $i++){
				
				$l_cb = $_POST['cb_'.$x['guid'][$i]];
				if($l_cb){
					$uuid = get_item('SELECT UUID() as uuid');
					$l_sql = 'insert into distribucijska_lista_primatelji values("'.$uuid.'", "'.$_GET['id'].'", "'.$x['guid'][$i].'")';
					sql_exe($l_sql);
				}
			}
			header('location:dist_list_edit.php?edit=done&id='.$_GET['id'].'&rbr='.$_GET['rbr']);
			exit;
		}
	}
?>
	<!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/dist-list-edit-1.css">
		<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-dist-list-edit-2.js"></script>
	</head>
<?
		if($_GET['edit'] == 'done'){
?>
			<body onload="setTimeout('closeIFrame(<?=$_GET['rbr']?>, \'<?=$_GET['id']?>\')', 5000)">
<?
		}else{
?>
			<body>
<?
		}
		
		
		$result = '';
		
		if($_GET['edit'] == 'done'){
			$result .= 'Promjena je uspješno izvršena<br><br>';
		}
		
		$result .= '<form name="formaDistList" class="formaDistList" method="post" action="dist_list_edit.php?id='.$_GET['id'].'&rbr='.$_GET['rbr'].'"><ul>';
		
		$x = get_data('select guid, DisplayName from zaposlenik order by DisplayName ', 1);
		for($i = 0; $i < sizeof($x['guid']); $i++){
			
			if(get_item('select guid from distribucijska_lista_primatelji where distribucijska_lista_guid = "'.$_GET['id'].'" and primatelj_guid = "'.$x['guid'][$i].'" ')){
				$l_checked = ' checked';
			}else{
				$l_checked = '';
			}
			
			$result .= '<li><input type="checkbox" name="cb_'.$x['guid'][$i].'" value="1" class="cb"'.$l_checked.'><p>'.$x['DisplayName'][$i].'</p></li>';
		}
		
		$result .= '</ul>';
		
		
		if($_GET['edit'] == 'done'){
			$result .= '<a href="#" onclick="closeIFrame('.$_GET['rbr'].', \''.$_GET['id'].'\'); return false;" class="distListButtonCancel">Zatvori prozor</a>';
		}else{
			$result .= '<a href="#" onclick="closeIFrame('.$_GET['rbr'].', \''.$_GET['id'].'\'); return false;" class="distListButtonCancel">Odustani</a>';
			$result .= '<a href="#" onclick="document.formaDistList.submit();" class="distListButtonOK">Potvrdi promjene</a>';
		}
		
		$result .= '</form>';
		
		print $result;
?>
	</body>
	</html>
