<?
	if($_POST){
		ob_start();
	}
	
	$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($_POST){
		$x = get_data('select rbr_users_outer_db as rbr from zaposlenik where login = "'.$_POST['LoginForm']['username'].'" and lozinka = "'.md5($_POST['LoginForm']['password']).'" ', 0);
		
		if($x['rbr']){
			$_SESSION['__id'] = $x['rbr'];
	
			if(isset($_POST['LoginForm[rememberMe]']) && $_POST['LoginForm[rememberMe]']){
				$l_stay = rand(1000, 9999).$x['rbr'].'http ';
				$_SESSION['rememberMe'] = $l_stay;
				setcookie("c_rememberMe", $l_stay, time() + 31536000, "/");
			}
			
			
			if($_POST['LoginForm']['username'] && $_POST['LoginForm']['password']){
				$rez = mysql_query('select rbr_users_outer_db from zaposlenik where login = "'.$_POST['LoginForm']['username'].'" and lozinka = "'.md5($_POST['LoginForm']['password']).'" ', $conn);
				$row = mysql_fetch_array ($rez);
				$result = $row['rbr_users_outer_db'];
				
				/*
				$rez2 = mysql_query('select DisplayName from zaposlenik where rbr_users_outer_db = '.$result, $conn);
				$row2 = mysql_fetch_array ($rez2);
				$l_name = $row2['DisplayName'];
				*/
				$l_name = '';
				
				
				$l_sql = 'delete from alarm where rbr_zaposlenik = '.$result;
				$rez2 = mysql_query($l_sql, $conn);
				
				$l_sql = 'select * from alarm_type order by rbr ';
				$rez2 = mysql_query($l_sql, $conn);
				while($row2 = mysql_fetch_array ($rez2)){
					//print $row2['ime']."<br>";
					
					$l_sql = 'select '.$row2['select_field'].' as guid '.
						' from '.$row2['db_table'].
						' where concat(", ", '.$row2['aims_persons_field'].', ", ") like "%, '.$l_name.',%" '.
						' and TIMESTAMPDIFF(SECOND, now(), '.$row2['source_date_field'].') < '.$row2['trigger_time'];
					$rez3 = mysql_query($l_sql, $conn);
					while($row3 = mysql_fetch_array ($rez3)){
						$l_sql = 'select rbr from alarm where rbr_alarm_type = '.$row2['rbr'].' and rbr_zaposlenik = '.$result.' and guid = "'.$row3['guid'].'" limit 1 ';
						$rez4 = mysql_query($l_sql, $conn);
						$row4 = mysql_fetch_array($rez4);
						if(!$row4['rbr']){
							$l_sql = 'insert into alarm values(0, '.$row2['rbr'].', '.$result.', "'.$row3['guid'].'", unix_timestamp(now()) - 5, 0, 0)';
							$rez4 = mysql_query($l_sql, $conn);
							//print $l_sql."<br><br>";
						}
					}
				}
			}
			
			
			
			
			
			header('location: ../../default.php?stat=ok');
		}else{
			header('location: ../../default.php?stat=err');
		}
		
		exit;
		
		ob_end_clean();
	}
?>