<?
	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";
	
	$result = '';
	
	
	$l_table = get_item('select kljuc from meta_item where rbr =  '.$_POST['rbr'].' and rbr_atribut = '.$_POST['rbr_atribut']);
	
	$x1 = get_item('select rbr from meta_tablica_list where ime_tablice = "'.$l_table.'" ');
	$x2 = get_item('select rbr from meta_forma where rbr_meta_tablica_list = '.$x1);
	$x3 = get_item('select rbr from meta_tab where rbr_meta_forma = '.$x2);
	$x4 = get_item('select rbr from meta_grupa where rbr_meta_tab= '.$x3);
	
	$y = get_data('select * from meta_item where rbr_meta_grupa = '.$x4.' order by 1, 2 ', 1);
	
	//p($y['default_value']);
	
	$l_sql = 'insert into '.$l_table.' values (null ';
	
				
	$l_error = '';
	$l_has_error = 0;
	
	for($i = 1; $i < sizeof($y['rbr']); $i++){
		
		
		if($y['mandatory'][$i] == 1 && $_POST['item-'.$i] == ''){
			$l_error .= ','.$i;
			$l_has_error = 1;
		}else{
			$l_error .= ',';
		}
		
		
		if(isset($_POST['item-'.$i])){
			
			$l_sql .= ', "'.addslashes($_POST['item-'.$i]).'"';
			
		}else{
			
			if($y['default_value'][$i]){
				
				
				if($y['default_value'][$i] == 'vlasnik'){
					
					if($_COOKIE['subaplication']){
						$l_sql .= ', "'.addslashes(get_item('select username from users where md5(username) = "'.$_COOKIE['subaplication'].'" ')).'"';
					}else{
						$l_sql .= ', "'.addslashes($y['default_value'][$i]).'"';
					}
				}else{
					$l_sql .= ', "'.addslashes($y['default_value'][$i]).'"';
				}
				
			}else{
				
				if($y['aktivan'][$i] == '4'){
					$l_sql .= ', now()';
				}else{
					$l_sql .= ', ""';
				}
				
			}
			
		}
	}
	$l_sql .= ' )';
	
	
	$la_data = array();
	
	if($l_has_error == 0){
		$l_new_rbr = sql_exe($l_sql, true);
		
		$la_data['result'] = $l_new_rbr;
		$la_data['name'] = $_POST['item-1'];
		$la_data['rbr'] = $_POST['rbr_atribut'];
	}else{
		$la_data['result'] = 0;
		$la_data['error'] = $l_error;
	}
	
	
	$la_json = json_encode($la_data);
	print $la_json;
	exit;
?>