<?
	//include $g_path."html/c_time.php";
	include "c_time.php";
	
	function _convert($str){
		//return iconv('utf-8', 'iso-8859-2//TRANSLIT', utf8_decode($str));
		return iconv('utf-8', 'iso-8859-2//TRANSLIT', $str);
	}
	
	
	function mailer_get_date_content(){
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		$handle = @fopen($g_path.'app/webroot/iso/extra-info-'.$l_tip_prog.'.txt', 'r');
		$contents = '';
		if($handle){
			while (!feof($handle)) {
				$contents .= fread($handle, 8192);
			}
			fclose($handle);
		}
		
		return $contents;
	}
	
	function mailer_get_outer_date(){
		
		$l_oib = get_item('select value from settings where name = "general.oib" ');
		
		$handle = @fopen(get_item('select value from settings where name = "licence.synchro.url.1" ').'&oib_num='.$l_oib.'&_='.time(), 'rb');
		
		if(empty($handle) || $handle == ''){
			return '';
			
			/*
			$handle = @fopen(get_item('select value from settings where name = "licence.synchro.url.2" ').'&oib_num='.$l_oib.'&_='.time(), 'rb');
			
			if(empty($handle) || $handle == ''){
				return '';
			}
			*/
			
		}
		
		$contents = '';
		while (!feof($handle)) {
			$contents .= fread($handle, 8192);
		}
		fclose($handle);
		
		return $contents;
	}
	
	function mailer_write_date($diff, $skip_contents = 0, $data = ''){
		if($diff > 3600){
			
			if($skip_contents == 0){
				$contents = mailer_get_outer_date();
			}else{
				$contents = $data;
			}
			
			if($contents == ''){
				$contents = date("Y-m-d").'{}';
			}
			$g_path = get_item('select value from settings where name = "putanja.abs" ');
			$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
			$handle2 = @fopen($g_path.'app/webroot/iso/extra-info-'.$l_tip_prog.'.txt', 'w');
			fwrite($handle2, $contents.date("Y-m-d H:i:s"));
			fclose($handle2);
			
			$la_tmp = explode('{}', $contents);
			$result = $la_tmp[0];
		}else{
			$result = -1;
		}
		
		return $result;
	}
	
	function mailer_check_date(){
		$result = 0;
		
		$contents = mailer_get_date_content();
		
		if($contents == ''){
			$contents = mailer_get_outer_date();
		}
		
		if($contents != ''){
			$la_tmp = explode('{}', $contents);
			
			if(isset($la_tmp[1]) && $la_tmp[1] != ''){
				$l_new_date = mailer_write_date(strtotime('now') - strtotime($la_tmp[1]));
				if($l_new_date != -1){
					$la_tmp[0] = $l_new_date;
				}
			}
			if(isset($la_tmp[1]) && $la_tmp[1] == ''){
				$l_new_date = mailer_write_date(10000000000);
				if($l_new_date != -1){
					$la_tmp[0] = $l_new_date;
				}
			}
			
			if(strtotime($la_tmp[0]) - strtotime('now') >= 7 * 24 * 60 * 60){
				$result = 1;
			}
		}
		
		return $result;
	}
	
	
	function get_mailer_time_data(){
		
		$l_month = u_mjesec(0, 1);
		$l_year = u_godina();
		
		if(intval($l_month) == 1){
			$l_last_month = 12;
			$l_last_year = $l_year - 1;
		}else{
			$l_last_month = intval($l_month) - 1;
			$l_last_year = $l_year;
		}
		
		$l_date  = u_datum(0, 1);
		
		switch($l_last_month){
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				$l_last_date  = 31;
				break;
			
			case 4:
			case 6:
			case 9:
			case 11:
				$l_last_date  = 30;
				break;
				
			case 2:
				if($l_last_year % 4 == 0){
					$l_last_date  = 29;
				}else{
					$l_last_date  = 28;
				}
				break;
		}
		
		$result = array('hr' => array(), 'en' => array());
		$result['hr']['today'] = $l_date.'-'.$l_month.'-'.$l_year;
		$result['hr']['first'] = '01-'.sprintf('%02d', $l_last_month).'-'.$l_last_year;
		$result['hr']['last'] = $l_last_date.'-'.sprintf('%02d', $l_last_month).'-'.$l_last_year;
		$result['en']['today'] = $l_year.'-'.$l_month.'-'.$l_date;
		$result['en']['first'] = $l_last_year.'-'.sprintf('%02d', $l_last_month).'-01';
		$result['en']['last'] = $l_last_year.'-'.sprintf('%02d', $l_last_month).'-'.$l_last_date;
		
		return $result;
	}
	
	
	
	function create_owner_report_and_send($rbr, $cnt, $x, $y, $a_time, $dont_create_get_only_info = 0){
		$result = 0;
		$l_owner_auto = get_item('select value from settings where name = "mailer.owner.auto" ');
		
		if($l_owner_auto == 'no'){
			return $result;
		}
		
		$la_owner_time = array();
		$la_owner_time[0] = get_item('select value from settings where name = "mailer.owner.time.1" ');
		$la_owner_time[1] = get_item('select value from settings where name = "mailer.owner.time.2" ');
		$la_owner_time[2] = get_item('select value from settings where name = "mailer.owner.time.3" ');
		$la_owner_time[3] = get_item('select value from settings where name = "mailer.owner.time.4" ');
		
		for($i = 0; $i < sizeof($la_owner_time); $i++){
			if(strlen($la_owner_time[$i]) != 5 && substr($la_owner_time[$i], 2, 1) != ':'){
				$la_owner_time[$i] = '';
			}
		}
		
		$l_now = u_sat().':'.u_minuta();
		
		
		$l_make_report = 0;
		
		if($rbr == 0){
			// prvo generiranje nekog izvjesca, za gazdu, nekog dana
			for($i = sizeof($la_owner_time) - 1; $i >= 0; $i--){
				if($la_owner_time[$i] != ''){
					if($l_now >= $la_owner_time[$i]){
						$l_time_upis = $la_owner_time[$i];
						$l_make_report = 1;
						break;
					}
				}
			}
			
			
			/*
			if($la_owner_time[0] != ''){
				if($l_now >= $la_owner_time[0]){
					$l_time_upis = $la_owner_time[0];
					$l_make_report = 1;
				}
			}
			*/
			
		}else{
			// drugo, trece, cetvrto i ostala generiranja nekog izvjesca, za gazdu, nekog dana
			for($i = sizeof($la_owner_time) - 1; $i >= 1; $i--){
				if($la_owner_time[$i] != ''){
					if($l_now >= $la_owner_time[$i] && $la_owner_time[$i] > $y['creation_time']){
						$l_time_upis = $la_owner_time[$i];
						$l_make_report = 1;
						break;
					}
				}
			}
			
			
			/*
			for($i = 1; $i < sizeof($la_owner_time); $i++){
				if($la_owner_time[$i] != ''){
					if($l_now >= $la_owner_time[$i] && $la_owner_time[$i] > $y['creation_time']){
						$l_time_upis = $la_owner_time[$i];
						$l_make_report = 1;
						break;
					}
				}
			}
			*/
		}
		
		if($l_make_report == 1){
			
			if($dont_create_get_only_info == 1){
				// vrati samo informaciju treba li ili ne generirati izvjesce
				switch($x['id'][$cnt]){
					case 1:
					case 2:
					case 8:
					case 9:
						$result = 1;
						break;
						
					default:
						$result = 0;
				}
				
				// ovdje obavezno iskoci van funkcije
				return $result;
			
			}else{
				
				$l_do_it = mailer_check_date();
				
				// a ovdje idi u generiranje izvjesca
				switch($x['id'][$cnt]){
					case 1:
					case 8:
						if($l_do_it){
							$la_file = all_dnevni_promet($a_time['hr']['today'], '', '');
						}else{
							$la_file = all_dnevni_promet('01-01-2000', '', '');
						}
						break;
						
					case 2:
						if($l_do_it){
							$la_file = all_pregled_prodaje_po_normativu($a_time['hr']['today'], $a_time['hr']['today'], '');
						}else{
							$la_file = all_pregled_prodaje_po_normativu('01-01-2000', '01-01-2000', '');
						}
						break;
						
					case 9:
						if($l_do_it){
							$la_file = all_prodani_artikli($a_time['hr']['today'], $a_time['hr']['today'], 1, 0, 0);
						}else{
							$la_file = all_prodani_artikli('01-01-2000', '01-01-2000', 0, 0, 0);
						}
						break;
						
					default:
						// ova izvjesca ne treba slati vlasniku
						break;
				}
			}
			
			
			
			
			switch($x['id'][$cnt]){
				case 1:
				case 8:
					// dnevni promet
					if($l_do_it){
						$l_period = 'na dan '.$a_time['hr']['today'];
					}else{
						$l_period = 'na dan 01-01-2000';
					}
					break;
					
				case 2:
					// pregled prodaje po normativu
					if($l_do_it){
						$l_period = 'za period od '.$a_time['hr']['today'].' do '.$a_time['hr']['today'];
					}else{
						$l_period = 'za period od 01-01-2000 do 01-01-2000';
					}
					break;
					
				case 9:
					// prodani artikli
					if($l_do_it){
						$l_period = 'za period od '.$a_time['hr']['today'].' do '.$a_time['hr']['today'].' - svi artikli';
					}else{
						$l_period = 'za period od 01-01-2000 do 01-01-2000';
					}
					break;
				
				default:
					// ova izvjesca ne treba slati vlasniku
					$l_period = '';
			}
			
			
			if(isset($la_file) && is_file($la_file['path'].$la_file['name'])){
				
				if($l_do_it){
					$l_hr_today = $a_time['hr']['today'];
				}else{
					$l_hr_today = '01-01-2000';
				}
				
				$l_sql = 'insert into mailer values(0, '.$x['id'][$cnt].', 1, "'.$a_time['en']['today'].'", "'.$l_time_upis.'", 1, 0, 0, now(), null, null, null, null)';
				$l_rbr = sql_exe($l_sql, true);
				
				if($l_rbr > 0){
					
					//$l_email_sender = get_item('select value from settings where name = "mailer.sender" ');
					$l_email_sender = 'prometi@itr.hr';
					$l_email_name = get_item('select value from settings where name = "mailer.name" ');
					$l_email_owner = get_item('select value from settings where name = "mailer.owner" ');
					
					$is_sent = mail_attachment($la_file['name'], $la_file['path'],
						$l_email_owner, $l_email_sender, $l_email_name, $l_email_sender, $x['name'][$cnt].' - '.$l_hr_today,
						'Dobar dan,<br><br>u privitku se nalazi izvjestaj "'.$x['name'][$cnt].'" '.$l_period.
						'<br><br>Srdacan pozdrav,<br><br>'.$l_email_name.'<br><br>');
					
					if($is_sent){
						$l_sql = 'update mailer '.
							' set sent_to_owner = 1, email_sent_to_owner = now(), email_owner = "'.$l_email_owner.'" '.
							' where id = '.$l_rbr;
						sql_exe($l_sql);
						$result = 1;
					}
				}
			}
		}
		
		return $result;
	}
	
	
	
	
	function create_accounting_report_and_send($rbr, $cnt, $x, $y, $a_time, $dont_create_get_only_info = 0){
		$result = 0;
		
		$l_accounting_auto = get_item('select value from settings where name = "mailer.accounting.auto" ');
		
		if($l_accounting_auto == 'no'){
			return $result;
		}
		
		if($rbr == 0){
			if(empty($y['sent_to_accounting']) || $y['sent_to_accounting'] == 0){
				if($l_accounting_auto == 'yes'){
					
					if($dont_create_get_only_info == 1){
						// vrati samo informaciju treba li ili ne generirati izvjesce
						switch($x['id'][$cnt]){
							case 2:
							case 3:
							case 4:
							case 5:
							case 6:
							case 7:
							case 9:
							case 10:
							case 11:
							case 24:
							case 25:
								$result = 1;
								break;
								
							default:
								$result = 0;
						}
						
						// ovdje obavezno iskoci van funkcije
						return $result;
						
					}else{
						
						$l_do_it = mailer_check_date();
						
						// a ovdje idi u generiranje izvjesca
						switch($x['id'][$cnt]){
							case 2:
								if($l_do_it){
									$la_file = all_pregled_prodaje_po_normativu($a_time['hr']['first'], $a_time['hr']['last'], '');
								}else{
									$la_file = all_pregled_prodaje_po_normativu('01-01-2000', '31-01-2000', '');
								}
								break;
								
							case 3:
								if($l_do_it){
									$la_file = all_pregled_prodaje_po_nacinu_placanja($a_time['hr']['first'], $a_time['hr']['last'], '');
								}else{
									$la_file = all_pregled_prodaje_po_nacinu_placanja('01-01-2000', '31-01-2000', '');
								}
								break;
								
							case 4:
							case 11:
								if($l_do_it){
									$la_file = all_rekapitulacija_prometa($a_time['hr']['first'], $a_time['hr']['last']);
								}else{
									$la_file = all_rekapitulacija_prometa('01-01-2000', '31-01-2000');
								}
								break;
								
							case 5:
								if($l_do_it){
									$la_file = all_pregled_prodaje_po_artiklu($a_time['hr']['first'], $a_time['hr']['last'], 'DA', 0);
								}else{
									$la_file = all_pregled_prodaje_po_artiklu('01-01-2000', '31-01-2000', 'DA', 0);
								}
								break;
								
							case 6:
								if($l_do_it){
									$la_file = all_porez_na_potrosnju($a_time['hr']['first'], $a_time['hr']['last']);
								}else{
									$la_file = all_porez_na_potrosnju('01-01-2000', '31-01-2000');
								}
								break;
								
							case 7:
							case 10:
								if($l_do_it){
									$la_file = all_stanje_skladista($a_time['hr']['last'], '23:59', '');
								}else{
									$la_file = all_stanje_skladista('31-01-2000', '23:59', '');
								}
								break;
								
							case 9:
								if($l_do_it){
									$la_file = all_prodani_artikli($a_time['hr']['first'], $a_time['hr']['last'], 1, 0, 0);
								}else{
									$la_file = all_prodani_artikli('01-01-2000', '31-01-2000', 0, 0, 0);
								}
								break;
								
							case 23:
								if($l_do_it){
									$la_file = all_pregled_nabave_materijalno($a_time['hr']['first'], $a_time['hr']['last']);
								}else{
									$la_file = all_pregled_nabave_materijalno('01-01-2000', '31-01-2000');
								}
								break;
								
							case 24:
							case 25:
								if($l_do_it){
									$la_file = all_stanje_skladista_iz_artikala($a_time['hr']['last'], '23:59');
								}else{
									$la_file = all_stanje_skladista_iz_artikala('31-01-2000', '23:59');
								}
								break;
								
							default:
								// ova izvjesca ne treba slati vlasniku
								break;
						}
					}
					
					
					switch($x['id'][$cnt]){
						case 1:
						case 8:
							// dnevni promet
						case 12:
						case 13:
							// kartica artikla
						case 14:
							// kolicina_materijala_po_stolovima
							// ova izvjesca ne treba slati u knjigovodstvo
							$l_period = '';
							break;
							
						case 7:
						case 10:
						case 24:
						case 25:
							// stanje skladista
							if($l_do_it){
								$l_period = 'na dan '.$a_time['hr']['last'].' u 23:59h';
							}else{
								$l_period = 'na dan 31-01-2000 u 23:59h';
							}
							break;
							
						case 9:
							if($l_do_it){
								$l_period = 'za period od '.$a_time['hr']['first'].' do '.$a_time['hr']['last'].' - svi artikli';
							}else{
								$l_period = 'za period od 01-01-2000 do 31-01-2000';
							}
							break;
						
						default:
							if($l_do_it){
								$l_period = 'za period od '.$a_time['hr']['first'].' do '.$a_time['hr']['last'];
							}else{
								$l_period = 'za period od 01-01-2000 do 31-01-2000';
							}
					}
					
					
					if(isset($la_file) && is_file($la_file['path'].$la_file['name'])){
						if($l_do_it){
							$l_hr_last = $a_time['hr']['last'];
						}else{
							$l_hr_last = '31-01-2000';
						}
						
						$l_sql = 'insert into mailer values(0, '.$x['id'][$cnt].', 2, "'.$a_time['en']['last'].'", null, 1, 0, 0, now(), null, null, null, null)';
						$l_rbr = sql_exe($l_sql, true);
						
						if($l_rbr > 0){
							//$l_email_sender = get_item('select value from settings where name = "mailer.sender" ');
							$l_email_sender = 'prometi@itr.hr';
							$l_email_name = get_item('select value from settings where name = "mailer.name" ');
							$l_email_accounting = get_item('select value from settings where name = "mailer.accounting" ');
							
							$is_sent = mail_attachment($la_file['name'], $la_file['path'],
								$l_email_accounting, $l_email_sender, $l_email_name, $l_email_sender, $x['name'][$cnt].' - '.$l_hr_last,
								'Dobar dan,<br><br>u privitku se nalazi izvjestaj "'.$x['name'][$cnt].'" '.$l_period.
								'<br><br>Srdacan pozdrav,<br><br>'.$l_email_name.'<br><br>');
							
							if($is_sent){
								$l_sql = 'update mailer '.
									' set sent_to_accounting = 1, email_sent_to_accounting = now(), email_accounting = "'.$l_email_accounting.'" '.
									' where id = '.$l_rbr;
								sql_exe($l_sql);
								$result = 1;
							}
						}
					}
				}
			}
		}
		
		return $result;
	}
	
	
	
	function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $bcc = ''){
		
		//return 1;
		
		$file = $path.$filename;
		$file_size = filesize($file); 
		$handle = @fopen($file, "r"); 
		$content = fread($handle, $file_size); 
		fclose($handle); 
		$content = chunk_split(base64_encode($content)); 
		$uid = md5(uniqid(time())); 
		$name = basename($file); 
		$header = "From: ".$from_name." <".$from_mail.">\n"; 
		if($bcc){
			$header .= "Bcc: ".$bcc."\n";
		}
		$header .= "Reply-To: ".$replyto."\n"; 
		$header .= "MIME-Version: 1.0\n"; 
		$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n"; 
		$header .= "This is a multi-part message in MIME format.\n"; 
		$header .= "--".$uid."\n"; 
		$header .= "Content-type:text/html; charset=windows-1250\n"; 
		$header .= "Content-Transfer-Encoding: 7bit\n\n"; 
		$header .= $message."\n\n"; 
		$header .= "--".$uid."\n"; 
		$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use diff. tyoes here 
		$header .= "Content-Transfer-Encoding: base64\n"; 
		$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n"; 
		$header .= $content."\n\n"; 
		$header .= "--".$uid."--"; 
		
		if(mail($mailto, $subject, "", $header)){
			$result = 1;
		}else{
			$result = 0;
		}
		
		return $result;
	}
	
	
	function presijeci_tekst($tekst, $limit){
		if(strlen($tekst) == 0){
			return $tekst;
		}
		$l_space_pos = 0;
		if((strlen($tekst) > $limit) && $limit > 0){
			$l_space_pos = strpos($tekst, " ", $limit);
		}
		if($l_space_pos == 0 || (!$l_space_pos)){
			$l_space_pos = strlen($tekst);
		}
		
		$l_left = substr($tekst, 0, $l_space_pos);
		$l_right = substr($tekst, $l_space_pos + 1);
		
		return array($l_left, $l_right);
	}
	
	function skrati_tekst($tekst, $limit){
		if(strlen($tekst) == 0){
			return $tekst;
		}
		$l_space_pos = 0;
		if((strlen($tekst) > $limit) && $limit > 0){
			$l_space_pos = strpos($tekst, " ", $limit);
			$l_end = "...";
		}
		if($l_space_pos == 0 || (!$l_space_pos)){
			$l_space_pos = strlen($tekst);
			$l_end = "";
		}
		return substr($tekst, 0, $l_space_pos).$l_end;
	}
	
		
	function mk_f_name($id){
		global $g_path_res;
		
		$l_folder_name = 'fold-'.(1763 * (1 + round($id / 11)) + 29 * zbroji($id)).zbroji($id);
		
		$l_dest = $g_path_res.'files/'.$l_folder_name.'/';
		
		if(!is_dir($l_dest)){
			mkdir($l_dest, 0755);
		}
		
		return $l_folder_name;
	}
	
	function map_element_value($tip, $value){
		switch($tip){
			case 'datetimebig':
				$la_tmp_0 = explode(' ', $value);
				
				$la_tmp = explode('-', $la_tmp_0[0]);
				$result = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				if($result == '00-00-0000' || $result == '--'){
					$result = '';
				}else{
					$result .= ' '.$la_tmp_0[1];
				}
				break;
			
			case 'datetime':
				$la_tmp = explode('-', substr($value, 0, 10));
				$result = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				if($result == '00-00-0000' || $result == '--'){
					$result = '';
				}
				break;
			
			default:
				$result = $value;
		}
		
		return $result;
	}
	
	function check_element_disabled($rules, $rbr_atribut, $a_ime_polja, $z, $pk_rbr){
		$result = 0;
		
		if($rules){
			$la_tmp = explode(':', $rules);
			
			if($la_tmp[0] == 'disabled'){
				$result = 1;
				if($la_tmp[1] == 'except_current_user_is_in_field'){
					$l_index = array_search($la_tmp[2], $rbr_atribut);
					
					if($_SESSION['__id']){
						$l_guid = get_item('select id from zaposlenik where rbr_users_outer_db = '.$_SESSION['__id']);
						
						if($l_guid == $z[$a_ime_polja[$l_index]]){
							// enabled, jer je vrijednost iz polja $la_tmp[2] jednaka ulogiranom korisniku
							$result = 0;
						}
					}
				}
				if(!$pk_rbr && $la_tmp[3] == 'orItIsNewRecord'){
					$result = 0;
				}
				if($la_tmp[1] == 'except_value_yes_is_in_field'){
					$l_index = array_search($la_tmp[2], $rbr_atribut);
					
					if($z[$a_ime_polja[$l_index]] == 'DA'){
						// enabled, jer je vrijednost iz polja $la_tmp[2] jednaka 'Da'
						$result = 0;
					}
				}
			}
		}
		
		return $result;
	}
	

	function gen_prikaz($prikaz, $table, $uvjet, $vrijednost, $ajax_style, $column, $reverse){
		$result = '';
		
		global $g_path_sl_res;
		global $g_path_sl_outer;
		
		$x = get_data('select rbr, ime, polje_order, klasa, show_label, label, show_header, header_klasa '.
			' from meta_prikaz where ime = "'.$prikaz.'" ', 0);
			
		// kontrola prikaza indexa modula
		//$z = get_item('select rbr from access '.
			//' where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = 1 and rbr_modul = '.$x['rbr'].' and rbr_access_type = 1 limit 1 ');
		$z = 0;
		if($z > 0){
			$result = '<div class="accessLimit">Nemate ovlasti za pregled podataka.</div>';
			return $result;
		}
		
		
		$y = get_data('select meta_prikaz_item.* '.
			' from meta_prikaz, meta_prikaz_item '.
			' where meta_prikaz.rbr = meta_prikaz_item.rbr_meta_prikaz '.
			' and meta_prikaz.ime = "'.$prikaz.'" '.
			' order by meta_prikaz_item.redoslijed, meta_prikaz_item.rbr_atribut ', 1);
			
		global $g_language;
		
		if($g_language > 1){
			$y_lang = get_data('select * '.
				' from meta_prikaz_item_lang '.
				' where rbr = '.$y['rbr_meta_prikaz'][0].
				' and rbr_jezik = '.$g_language.
				' order by rbr, rbr_atribut ', 1);
		}
		
		
		//print_r($_GET);
		//print_r($y['ime_polja']);
		
		/*
		$l_cnt = 0;
		$l_uvjet_get = '';
		$la_tip = array();
		foreach($_GET as $key => $value){
			if(substr($key, 0, 2) == 'sf'){
				
				if($l_cnt == 0){
					
					//print $prikaz." xxxxx<br><br>";
					
					// samo prvi put se poziva
					$y2 = get_data('select meta_prikaz_item.ime_polja, meta_prikaz_item.tip_polja, meta_prikaz_item.tip_polja_maska, meta_prikaz_item.kljuc '.
						' from meta_prikaz, meta_prikaz_item '.
						' where meta_prikaz.rbr = meta_prikaz_item.rbr_meta_prikaz '.
						' and meta_prikaz.ime = "'.$prikaz.'" '.
						' order by meta_prikaz_item.rbr_atribut ', 1);
				}
				
				if(!$value){
					continue;
				}
				
				$l_key = str_replace('sf', '', $key);
				
				if($y2['tip_polja_maska'][$l_key]){
					$y2['tip_polja'][$l_key] = $y2['tip_polja_maska'][$l_key];
				}
				$la_tip[$l_key] = explode('-', $y2['tip_polja'][$l_key]);
				$la_tip[$l_key][0] = strtolower(trim($la_tip[$l_key][0]));
				
				//print $la_tip[$l_key][0]."<br><br>\n\n";
				
				if($la_tip[$l_key][0] == 'smallint' || ($y2['kljuc'][$l_key] && $y2['kljuc'][$l_key] != 'PRI')){
					$l_uvjet_get .= 'and '.$y2['ime_polja'][$l_key].' = "'.$value.'" ';
				}else{
					$l_uvjet_get .= 'and '.$y2['ime_polja'][$l_key].' like "%'.$value.'%" ';
				}
				
				$l_cnt++;
			}
		}
		print $l_uvjet_get."<br>";
		*/
		
		//print $g_pretraga_string." aaaaa <br>";
			
			
		if($column){
			$y['kljuc'][$column - 1] ?
				$l_order = ' order by '.$y['kljuc'][$column - 1].'.'.$y['polje_select'][$column - 1]
				:
				$l_order = 'order by '.$y['ime_polja'][$column - 1];
			
			if($reverse){
				$l_order .= ' desc ';
			}
		}else{
			$x['polje_order'] == '' ? $l_order = '' : $l_order = ' order by '.$x['polje_order'];
		}
		
		
		//$uvjet == '' ? $l_uvjet = '' : $l_uvjet = ' where 1 = 1 and '.$uvjet;
		
		global $g_pretraga_string;
		
		if($uvjet == '' ){
			$l_uvjet = '';
			if($g_pretraga_string){
				$l_uvjet = ' where 1 = 1 '.$g_pretraga_string;
			}
		}else{
			$l_uvjet = ' where 1 = 1 and '.$uvjet;
			if($g_pretraga_string){
				$l_uvjet .= $g_pretraga_string;
			}
		}
		
		if($y['kljuc'][$column - 1]){
			$l_sql = 'select '.$table.'.* from '.$table.
				' left outer join '.$y['kljuc'][$column - 1].
				' on '.$table.'.'.$y['ime_polja'][$column - 1].' = '.$y['kljuc'][$column - 1].'.'.$y['polje_where'][$column - 1].' '.
				$l_uvjet.' '.$l_order;
		}else{
			
			if(!strstr($l_uvjet, 'where ')){
				$l_uvjet = ' where 1 = 1 ';
			}
			
			if(isset($_GET['addition'])){
				switch($_GET['addition']){
					case 'Annulments':
						$l_uvjet_2 = ' and type = 1 ';
						break;
						
					case 'Transfers':
						$l_uvjet_2 = ' and type = 3 ';
						break;
						
					case 'Returns':
						$l_uvjet_2 = ' and type = 2 ';
						break;
						
					case 'Inventura':
						$l_uvjet_2 = ' and type = 4 ';
						break;
						
					case 'Primka':
						$l_uvjet_2 = ' and type = 0 ';
						break;
						
					case 'Skladiste':
						$l_uvjet_2 = ' and type = 6 ';
						// DODANO 07.03.2026 - paging za Skladiste (20 po stranici)
						$l_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
						if($l_page < 1) $l_page = 1;
						$l_limit_skladiste = ' LIMIT ' . (($l_page - 1) * 20) . ', 20';
						break;
						
					default:
						$l_uvjet_2 = '';
				}
			}else{
				$l_uvjet_2 = '';
			}
			
			$l_sql = 'select '.$table.'.* from '.$table.' '.$l_uvjet.' '.$l_uvjet_2.' '.$l_order;
			// DODANO 07.03.2026 - paging za Skladiste
			if(isset($l_limit_skladiste)){
				$l_sql .= $l_limit_skladiste;
			}
		}
		
		
		//print $l_sql."<br><br>";
		
		$z = get_data($l_sql, 1);
		//p($z);
		
		$x['klasa'] == '' ? $l_klasa = '' : $l_klasa = ' class="'.$x['klasa'].'"';
		$x['header_klasa'] == '' ? $l_header_klasa = '' : $l_header_klasa = ' class="'.$x['header_klasa'].'"';
		$x['show_label'] == 1 ? $l_label = '<h3>'.$x['label'].'</h3>' : $l_label = '';
		
		/*
		$l_pretraga = '<a href="#" onclick="return false;" class="pretraga">Detaljna pretraga</a>'."\n".
			'<div class="pretragaDiv" id="pretragaDiv">'."\n".
			'<p class="label">Prezime i ime</p><input type="text" id="searchField1" name="searchField1" value="" onkeyup="checkSearch(this.value, 11, 3, 1)" >'."\n".
			'<p class="label">MB</p><input type="text" id="searchField2" name="searchField2" value="" onkeyup="checkSearch(this.value, 11, 29, 2)">'."\n".
			'<input type="submit" value="Pretraži" class="sbm">'.
			'<div id="searchResults"></div></div>';
		*/
		
		
		$la_type = array();
		$la_tip_polja = array();
		$la_sort_class = array();
		
		
		if($ajax_style == ''){
			$result .= $l_label."\n";
			//$result .= $l_pretraga."\n";
		}
			
		
		$l_zapamti_stupac = -1;
		
		$la_skip = array();
		$l_nc_pos = -1;
		$l_pc_pos = -1;
		$l_nc_sum = 0;
		$l_pc_sum = 0;
		
		$result .= '<table cellspacing="0" cellpadding="0" border="0"'.$l_klasa.' style="width:1050px;">'."\n";
		$l_header = '<tr'.$l_header_klasa.'>'."\n";
		if(isset($_GET['addition'])){
			$l_header_2 = '<tr'.$l_header_klasa.'>'."\n";
		}
		for($i = 0; $i < sizeof($y['label']); $i++){
			if($y['aktivan'][$i] == 0){
				continue;
			}
			
			
			if($column > 0 && $reverse != 1){
				$l_reverse = '1';
				$la_sort_class[$column - 1] = ' class="sort"';
			}else{
				$l_reverse = '';
			}
				
			if($reverse == 1){
				$la_sort_class[$column - 1] = ' class="sortReverse"';
			}
			
			if(($i + 1) != $column){
				$l_reverse = '';
			}
			
			if($ajax_style == ''){
				$l_href = ' href="#" onclick="display_layer(\''.$vrijednost.'\', \''.$ajax_style.'\', '.zbroji($i).', '.intval($l_reverse).'); return false;"';
			}else{
				$la_old = array('&sort='.$column, '&reverse='.$reverse, '&added=1', '&error=1', '&edited=1');
				$la_new = array('', '', '', '', '');
				
				$l_qstr = str_replace($la_old, $la_new, $_SERVER['QUERY_STRING']);
				
				if($l_reverse){
					$l_reverse = '&reverse=1';
				}
				$l_href = ' href="'.$_SERVER['PHP_SELF'].'?'.$l_qstr.'&sort='.zbroji($i).$l_reverse.'"';
			}
		
			//print $l_href." --- <br>";
			
			if($g_language > 1 && $y_lang['label'][$y['rbr_atribut'][$i] - 1]){
				$l_label_lang = $y_lang['label'][$y['rbr_atribut'][$i] - 1];
			}else{
				$l_label_lang = $y['label'][$i];
			}
			
			
			$l_skip = -1;
			if(isset($_GET['addition'])){
				switch($_GET['addition']){
					case 'Annulments':
						if($l_label_lang == 'Broj primke' || $l_label_lang == 'Broj povratnice' || $l_label_lang == 'Broj medjuskladisnice'){
							$l_skip = $i;
							//array_push($la_skip, 'Broj otpisa');
							$la_skip = array('Broj primke', 'Broj povratnice', 'Broj medjuskladisnice');
						}
						break;
						
					case 'Transfers':
						if($l_label_lang == 'Broj primke' || $l_label_lang == 'Broj otpisa' || $l_label_lang == 'Broj povratnice'){
							$l_skip = $i;
							//array_push($la_skip, 'Broj medjuskladisnice');
							$la_skip = array('Broj primke', 'Broj otpisa', 'Broj povratnice');
						}
						if($l_label_lang == 'Dobavljač'){
							$l_label_lang = 'Korisniku';
						}
						break;
						
					case 'Returns':
						if($l_label_lang == 'Broj primke' || $l_label_lang == 'Broj otpisa' || $l_label_lang == 'Broj medjuskladisnice'){
							$l_skip = $i;
							//array_push($la_skip, 'Broj povratnice');
							$la_skip = array('Broj primke', 'Broj otpisa', 'Broj medjuskladisnice');
						}
						break;
						
					case 'Primka':
						if($l_label_lang == 'Broj otpisa' || $l_label_lang == 'Broj povratnice' || $l_label_lang == 'Broj medjuskladisnice'){
							$l_skip = $i;
							//array_push($la_skip, 'Broj primke');
							$la_skip = array('Broj otpisa', 'Broj povratnice', 'Broj medjuskladisnice');
						}
						break;
						
					case 'Skladiste':
						// DODANO 07.03.2026 - sakrivamo nepotrebne stupce za Skladiste
						if($l_label_lang == 'Broj primke' || $l_label_lang == 'Broj otpisa' || $l_label_lang == 'Broj medjuskladisnice' || $l_label_lang == 'Broj dokumenta' || $l_label_lang == 'Datum dokumenta'){
							$l_skip = $i;
							$la_skip = array('Broj primke', 'Broj otpisa', 'Broj medjuskladisnice', 'Broj dokumenta', 'Datum dokumenta');
						}
						if($l_label_lang == 'Broj povratnice'){
							$l_label_lang = 'Broj e računa - skladište';
						}
						break;
				}
				if($l_skip >= 0){
					continue;
				}
			}
			
			
			$l_header .= '<td'.$la_sort_class[$i].'><a'.$l_href.'>'.$l_label_lang.'</a></td>'."\n";
			if(isset($_GET['addition'])){
				switch($l_label_lang){
					case 'NC':
						$l_header_2 .= '<td>(#-#NCSUM#-#)</td>'."\n";
						$l_nc_pos = $i;
						break;
						
					case 'PC':
						$l_header_2 .= '<td>(#-#PCSUM#-#)</td>'."\n";
						$l_pc_pos = $i;
						break;
						
					default:
						$l_header_2 .= '<td></td>'."\n";
				}
			}
			
			
			// iznimka - za prikaz tipa dokumenta (jer je sifrarnik izvan definicije kljuc->tablica)
			if(($y['rbr'][$i] == 89 || $y['rbr'][$i] == 90) && $y['rbr_atribut'][$i] == 14 && $l_label_lang == 'Vrsta zapisa'){
				$l_zapamti_stupac = $i;
			}
			
			
			if($y['tip_polja_maska'][$i]){
				$y['tip_polja'][$i] = $y['tip_polja_maska'][$i];
			}
			
			$la_type[$i] = explode('-', $y['tip_polja'][$i]);
			$la_type[$i][0] = strtolower(trim($la_type[$i][0]));
			
			//print $y['tip_polja'][$i]." === <br>";
			//print $la_type[$i][0]." === <br>";
			
			if($la_type[$i][0] == 'char' && !$y['kljuc'][$i]){
				$la_tip_polja[$i] = 'txt';
			}
			if($la_type[$i][0] == 'text'){
				$la_tip_polja[$i] = 'area';
			}
			if($la_type[$i][0] == 'file'){
				$la_tip_polja[$i] = 'file';
			}
			if($la_type[$i][0] == 'date'){
				$la_tip_polja[$i] = 'date';
			}
			if($la_type[$i][0] == 'datetimebig'){
				$la_tip_polja[$i] = 'datetimebig';
			}
			if($la_type[$i][0] == 'smallint' || ($y['kljuc'][$i] && $y['kljuc'][$i] != 'PRI')){
				$la_tip_polja[$i] = 'select';
			}
			if($la_type[$i][0] == 'int'){
				$la_tip_polja[$i] = 'txt';
			}
			if($la_type[$i][0] == 'label'){
				$la_tip_polja[$i] = 'label';
			}
		}
		$l_header .= '<td></td></tr>'."\n";
		if(isset($_GET['addition'])){
			$l_header_2 .= '<td></td></tr>'."\n";
		}
		
		if($x['show_header'] == 1){
			$result .= $l_header;
			if(isset($_GET['addition'])){
				$result .= $l_header_2;
			}
		}
			
		$la_class_par_nepar = array(' class="par"', ' class="nepar"', ' class="parGreen"', ' class="neparGreen"', ' class="parRed"', ' class="neparRed"');
		
		$l_check_date = get_item('select ime_polja from meta_prikaz_item where rbr = '.$y['rbr'][0].' and check_date = 1 order by rbr_atribut limit 1 ');
		$l_check_finished = get_item('select ime_polja from meta_prikaz_item where rbr = '.$y['rbr'][0].' and check_finished = 1 order by rbr_atribut limit 1 ');
		
		
		$la_through = array();
		
		for($i = 0; $i < sizeof($y['label']); $i++){
			$la_polje_select = explode(':', $y['polje_select'][$i]);
			if($la_polje_select[0] == '_{through}_'){
				$la_through[$i][0] = $la_polje_select[2];
				$la_through[$i][1] = $la_polje_select[3];
				
				$y['polje_select'][$i] = $la_polje_select[1];
			}
		}
		
		for($i = 0; $i < sizeof($z[$y['ime_polja'][0]]); $i++){
			
			if($l_check_date && $l_check_finished){
				$l_check_date_add = 0;
				
				if($z[$l_check_finished][$i] == 0){
					// nezavrseni - prikazi ih u zelenim nijansama
					$l_check_date_add = 2;
				}
				if($z[$l_check_finished][$i] == 0 && u_godina().'-'.u_mjesec(0, 1).'-'.u_datum(0, 1) > $z[$l_check_date][$i]){
					// ako je nezavrsen i danasnji dan je veci od datuma koji se provjerava - prikazi ih u crvenim nijansama
					$l_check_date_add = 4;
				}
			}
			$result .= '<tr'.$la_class_par_nepar[$i % 2 + $l_check_date_add].'>'."\n";
			
			for($j = 0; $j < sizeof($y['label']); $j++){
				if($y['aktivan'][$j] == 0){
					continue;
				}
				
				if(sizeof($la_skip) > 0 && in_array($y['label'][$j], $la_skip)){
					continue;
				}
				
				//print $z[$y['ime_polja'][$j]][$i]." ++++ <br><br>";
				
				$l_element = '';
				$l_connect = '';
				
				if($y['connect_fields'][$j]){
					$la_all = explode(':', $y['connect_fields'][$j]);
					$la_tmp = explode(';', $la_all[0]);
					
					$la_polje = array();
					
					for($k = 0; $k < sizeof($la_tmp); $k++){
						$la2_tmp = explode('-', $la_tmp[$k]);
						$x_conn = get_data('select ime_polja, label, tip_polja, tip_polja_maska from meta_prikaz_item where rbr = '.$la2_tmp[0].' and rbr_atribut = '.$la2_tmp[1]);
						$la_polje []= $x_conn;
					}
					
					$l_table = get_item('select meta_tablica_list.ime_tablice '.
						' from meta_prikaz_item, meta_prikaz, meta_tablica_list '.
						' where meta_prikaz_item.rbr_meta_prikaz = meta_prikaz.rbr '.
						' and meta_prikaz.rbr_meta_tablica_list = meta_tablica_list.rbr '.
						' and meta_prikaz_item.rbr = '.$la2_tmp[0].
						' and meta_prikaz_item.rbr_atribut = '.$la2_tmp[1]);
					
					$la2_tmp = explode('-', $la_all[1]);
					$l_field = get_item('select ime_polja from meta_prikaz_item where rbr = '.$la2_tmp[0].' and rbr_atribut = '.$la2_tmp[1]);
					
					$l_polje_txt = '';
					for($k = 0; $k < sizeof($la_polje); $k++){
						$l_polje_txt .= ', '.$la_polje[$k]['ime_polja'];
					}
					$l_polje_txt = substr($l_polje_txt, 2);
					
					$y_connect = get_data('select '.$l_polje_txt.' from '.$l_table.' where '.$l_field.' = "'.$z['id'][$i].'"', 1);
					
					if(sizeof($y_connect[$la_polje[0]['ime_polja']]) > 0){
						$l_connect = '<a href="#" onclick="opcl_connectFields(this, \''.$i.'\', \''.$z['id'][$i].'\', \''.$y['connect_fields'][$j].'\'); return false;" '.
							'class="cfButton" title="Pregled vezanih popravnih radnji">+</a>'.
							'<div id="connectFields_'.$i.'" class="connectFields"></div>';
					}
				}
				
				if($l_nc_pos == $j){
					$l_nc_sum += $z[$y['ime_polja'][$j]][$i];
				}
				if($l_pc_pos == $j){
					$l_pc_sum += $z[$y['ime_polja'][$j]][$i];
				}
				
				//p($y['ime_polja'][$j]);
				//print "yyy ".$la_tip_polja[$j]." xxx <br>";
				switch($la_tip_polja[$j]){
					case 'file':
						$z[$y['ime_polja'][$j]][$i] == '' ?
							$l_element = ''
							:
							$l_element = '<a href="'.$z[$y['ime_polja'][$j]][$i].'" target="_blank"><img src="'.$g_path_sl_res.'images/document.jpg" border="0" '.
								'title="'.$z[$y['ime_polja'][$j]][$i].'"></a>';
						break;
						
					case 'select':
						if($la_through[$j][0]){
							$l_element = get_item('select '.str_replace('f_', '', $y['polje_select'][$j]).' from '.$y['kljuc'][$j].' where '.$y['polje_where'][$j].' = "'.$z[$y['ime_polja'][$j]][$i].'" ');
							$l_element = get_item('select '.$la_through[$j][1].' from '.$la_through[$j][0].' where '.$y['polje_where'][$j].' = "'.$l_element.'" ');
						}else{
							$l_element = get_item('select '.str_replace('f_', '', $y['polje_select'][$j]).' from '.$y['kljuc'][$j].' where '.$y['polje_where'][$j].' = "'.$z[$y['ime_polja'][$j]][$i].'" ');
						}
						break;
						
					case 'datetimebig':
						$la_tmp_0 = explode(' ', $z[$y['ime_polja'][$j]][$i]);
						
						$la_tmp = explode('-', $la_tmp_0[0]);
						$l_element = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
						if($l_element == '00-00-0000' || $l_element == '--'){
							$l_element = '';
						}else{
							$l_element .= ' '.$la_tmp_0[1];
						}
						break;
						
					case 'label':
						//print $la_type[$j][1].$z[$y['ime_polja'][$j]][$i].' ===<br>';
						if($la_type[$j][1] == 'rbr_fiskal'){
							$la_tmp_fiskal = explode('/', $z[$y['ime_polja'][$j]][$i]);
							$l_element = intval($la_tmp_fiskal[1]).'/'.$la_tmp_fiskal[2].'/'.$la_tmp_fiskal[3];
							//$l_element = $z[$y['ime_polja'][$j]][$i];
						}
						break;
						
					case 'date':
						$la_tmp = explode('-', substr($z[$y['ime_polja'][$j]][$i], 0, 10));
						$l_element = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
						break;
						
					default:
						$l_element = $z[$y['ime_polja'][$j]][$i].$l_connect;
						break;
				}
				
				if($l_zapamti_stupac > -1 && $l_zapamti_stupac == $j){
					$result .= '<td>'.get_item('select name from receiving_types where id = '.$l_element).' ('.$l_element.')</td>'."\n";
				}else{
					$result .= '<td>'.$l_element.'</td>'."\n";
				}
			}
			
			$result .= '<td width="140" align="right" style="position:relative;">';
			$result .= '<span class="primka-email-send" id="email-send-'.$z['id'][$i].'"></span>';
			
			if($prikaz == 'tablica_dokumenata' && $table == 'document'){
				$result .= '<a href="#" onclick="open_layer(\'clanoviDistListDo_'.$i.'\'); return false;"><img src="'.$g_path_sl_res.'images/b-distribucijske-liste-m.gif" border="0" '.
					'title="Distribuiraj dokument" class="imgSmall"></a>'.
					'<div id="clanoviDistListDo_'.$i.'" class="clanoviDistListDo"><iframe id="distListDoFrame_'.$i.'" src="res/dist_list_do.php?rbr='.$i.'&id='.$z['id'][$i].'" '.
					'name="distListDoFrame_'.$i.'" class="distListDoFrame" frameborder="0" border="0" scrolling="yes" scrollbar="yes"></iframe>';
				$result .= '</div>'.
					''."\n";
			}
			
			if($_GET['a'] == 3 && $_GET['b'] == 3 && $_GET['c'] != 2){
				$result .= '<a href="#" onclick="open_layer(\'copyDistList_'.$i.'\'); return false;"><img src="'.$g_path_sl_res.'images/b-copy-m.gif" border="0" '.
					'title="Kopiraj distribucijsku listu" class="imgSmall"></a>'.
					'<div id="copyDistList_'.$i.'" class="copyDistList"><iframe id="copyDistListFrame_'.$i.'" src="res/dist_list_copy.php?rbr='.$i.'&id='.$z['id'][$i].'" '.
					'name="copyDistListFrame_'.$i.'" class="copyDistListFrame" frameborder="0" border="0" scrolling="yes" scrollbar="yes"></iframe>';
				$result .= '</div>'.
					''."\n";
					
				$result .= '<a href="#" onclick="open_layer(\'clanoviDistList_'.$i.'\'); return false;"><img src="'.$g_path_sl_res.'images/b-distribucijske-liste-m.gif" border="0" '.
					'title="Uredi članove distribucijske liste" class="imgSmall"></a>'.
					'<div id="clanoviDistList_'.$i.'" class="clanoviDistList"><iframe id="distListFrame_'.$i.'" src="res/dist_list_edit.php?rbr='.$i.'&id='.$z['id'][$i].'" '.
					'name="distListFrame_'.$i.'" class="distListFrame" frameborder="0" border="0" scrolling="yes" scrollbar="yes"></iframe>';
				$result .= '</div>'.
					''."\n";
			}
			
			
			if(!($_GET['a'] == 3 && $_GET['b'] == 4)){
				
				
				$l_js_spremi = 'return forma_del(\''.$x['ime'].'\', \''.$z['id'][$i].'\');';
				$l_img_spremi = 'b-del-m.gif';
					
				// kontrola prikaza indexa modula
				//$zz = get_item('select rbr from access '.
					//' where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = 1 and rbr_modul = '.$x['rbr'].' and rbr_access_type = 2 limit 1 ');
				$zz = 0;
				if($zz > 0){
					$l_img_spremi = 'b-del-m-disabled.gif';
					$l_js_spremi = 'alert(\'Nemate ovlasti za brisanje podataka.\'); return false;';
				}
				
				
				if($_GET['a'] == 6 && ($_GET['b'] == 7 || $_GET['b'] == 8 || $_GET['b'] == 9)){
				}else{
					$result .= '<a href="#" onclick="'.$l_js_spremi.'"><img src="'.$g_path_sl_res.'images/'.$l_img_spremi.'" border="0" title="Obriši podatak" class="imgSmall"></a>'."\n";
				}
				
				if($ajax_style == ''){
					$l_href_edit = ' href="#" onclick="document_get_ch(\''.$z['id'][$i].'\', \'adddata\'); return false;"';
				}else{
					$_GET['c'] ? $l_more_param = $_GET['c'].':' : $l_more_param = '';
					$_GET['d'] ? $l_more_param .= $_GET['d'].':' : $l_more_param = $l_more_param;
					$l_href_edit = ' href="'.make_link('sifrarnik-editiraj-podatak-'.$_GET['d'], $_GET['a'].':'.$_GET['b'].':'.$l_more_param.$z['id'][$i]).'" ';
				}
				
				if($_GET['a'] == 6 && ($_GET['b'] == 7 || $_GET['b'] == 8)){
					$l_rec_data = get_data('select type, posted from receivings where id = '.$z['id'][$i], 0);
					
					$result .= '<a href="#" onclick="primka_email_send(this, '.$l_rec_data['type'].', '.$z['id'][$i].'); return false;"><img src="'.
						$g_path_sl_res.'images/b-email-2.gif" border="0" title="Pošalji izvještaj na e-mail" class="imgSmall"></a>'."\n";
								
					switch($l_rec_data['type']){
						case '0':
							$result .= '<a href="'.$g_path_sl_outer.'reports/print_receiving/'.$z['id'][$i].'" target="_blank"><img src="'.
								$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
							break;
							
						case '1':
							$result .= '<a href="'.$g_path_sl_outer.'reports/print_writeOff/'.$z['id'][$i].'" target="_blank"><img src="'.
								$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
							break;
							
						case '2':
							$result .= '<a href="'.$g_path_sl_outer.'reports/print_returnee/'.$z['id'][$i].'" target="_blank"><img src="'.
								$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
							break;
							
						case '3':
							$result .= '<a href="'.$g_path_sl_outer.'reports/print_transfer_note/'.$z['id'][$i].'" target="_blank"><img src="'.
								$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
							break;
							
						case '4':
							$result .= '<a href="'.$g_path_sl_outer.'reports/print_inventura/'.$z['id'][$i].'" target="_blank"><img src="'.
								$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
							break;
							
						case '6':
							$result .= '<a href="'.$g_path_sl_outer.'reports/print_skladiste/'.$z['id'][$i].'" target="_blank"><img src="'.
								$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
							break;
					}
						
					if($l_rec_data['posted'] == 1){
						$result .= '<a href="#" onclick="stavka_promijeni_status('.$z['id'][$i].', 1); return false;"><img src="'.
							$g_path_sl_res.'images/b-otknjizi-m.gif" border="0" title="Otknjiži" alt="Otknjiži" class="imgSmall"></a>'."\n";
					}else{
						$result .= '<a href="#" onclick="stavka_promijeni_status('.$z['id'][$i].', 2); return false;"><img src="'.
							$g_path_sl_res.'images/b-proknjizi-m.gif" border="0" title="Proknjiži" alt="Proknjiži" class="imgSmall"></a>'."\n";
					}
				}
				
				
				if($_GET['a'] == 6 && $_GET['b'] == 9){
					$l_rec_data = get_data('select id, posted from medjus where id = '.$z['id'][$i], 0);
					
					$result .= '<a href="#" onclick="primka_email_send(this, 1001, '.$z['id'][$i].'); return false;"><img src="'.
						$g_path_sl_res.'images/b-email-2.gif" border="0" title="Pošalji izvještaj na e-mail" class="imgSmall"></a>'."\n";
						
					$result .= '<a href="'.$g_path_sl_outer.'reports/print_medjus/'.$z['id'][$i].'" target="_blank"><img src="'.
						$g_path_sl_res.'images/b-print-m.gif" border="0" title="Ispiši" class="imgSmall"></a>'."\n";
						
					if($l_rec_data['posted'] == 1){
						$result .= '<a href="#" onclick="stavka_promijeni_status_medjus('.$z['id'][$i].', 1); return false;"><img src="'.
							$g_path_sl_res.'images/b-otknjizi-m.gif" border="0" title="Otknjiži" alt="Otknjiži" class="imgSmall"></a>'."\n";
					}else{
						$result .= '<a href="#" onclick="stavka_promijeni_status_medjus('.$z['id'][$i].', 2); return false;"><img src="'.
							$g_path_sl_res.'images/b-proknjizi-m.gif" border="0" title="Proknjiži" alt="Proknjiži" class="imgSmall"></a>'."\n";
					}
				}
				
				
				
				if(isset($_GET['addition'])){
					if(strstr($l_href_edit, '?a=') && strstr($l_href_edit, '&b=') && !strstr($l_href_edit, '&addition=')){
						$l_href_edit = str_replace('&b=', '&addition='.$_GET['addition'].'&b=', $l_href_edit);
					}
				}
				
				$result .= '<a '.$l_href_edit.'><img src="'.$g_path_sl_res.'images/b-edit-m.gif" border="0" title="Promijeni podatak" class="imgSmall" id="clickmeedit-'.$z['id'][$i].'"></a>'."\n";
			}
			
			
			$result .= '</td>'."\n";
			
			$result .= '</tr>'."\n";
		}
		
		$result .= '</table>'."\n";
		
		// DODANO 07.03.2026 - paging navigacija za Skladiste
		if(isset($_GET['addition']) && $_GET['addition'] == 'Skladiste'){
			$l_total = get_item('select count(*) from '.$table.' where type = 6');
			$l_total_pages = ceil($l_total / 20);
			if($l_total_pages < 1) $l_total_pages = 1;
			$l_current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			if($l_current_page < 1) $l_current_page = 1;
			
			$l_base_url = $_SERVER['PHP_SELF'].'?a='.$_GET['a'].'&b='.$_GET['b'].'&addition='.$_GET['addition'];
			
			$l_paging = '<div style="padding:10px; text-align:center;">';
			if($l_current_page > 1){
				$l_paging .= '<a href="'.$l_base_url.'&page=1" style="margin:0 5px;">&laquo; Prva</a>';
				$l_paging .= '<a href="'.$l_base_url.'&page='.($l_current_page - 1).'" style="margin:0 5px;">&lt; Prethodna</a>';
			}
			$l_paging .= ' Stranica '.$l_current_page.' od '.$l_total_pages.' ';
			if($l_current_page < $l_total_pages){
				$l_paging .= '<a href="'.$l_base_url.'&page='.($l_current_page + 1).'" style="margin:0 5px;">Sljedeća &gt;</a>';
				$l_paging .= '<a href="'.$l_base_url.'&page='.$l_total_pages.'" style="margin:0 5px;">Zadnja &raquo;</a>';
			}
			$l_paging .= '</div>';
			$result .= $l_paging;
		}
		// KRAJ DODANO 07.03.2026
		
		$result = '<div class="list">'.$result.'</div>';
		
		//p($result);
		
		$result = str_replace('#-#NCSUM#-#', sprintf("%01.2f", $l_nc_sum), $result);
		$result = str_replace('#-#PCSUM#-#', sprintf("%01.2f", $l_pc_sum), $result);
		
		return $result;
	}

	
	
	function get_ime_polja($rbr){
			// ako ima vise grupa, mora se vidjeti za svaku grupu u koji tab pripada... pa se iz tog skupa tabova dobiju sve grupe koje se koriste za dohvat imena polja za dohvat "$z = ..."
			// jer grupe podijele polja na vise skupova, a samo jedan skup ima "PRI" primarni kljuc
			// ovo sam kasnije prosirio i na tabove jer moze biti i vise tabova koji imaju vise grupa...
			
			$l_str = '(0';
			$x = get_data('select meta_tab.rbr from meta_tab where meta_tab.rbr_meta_forma ='.$rbr, 1);
			for($i = 0; $i < sizeof($x['rbr']); $i++){
				$l_str .= ', '.$x['rbr'][$i];
			}
			$l_str .= ')';
			
			//print $l_str."<br>";
			
			$l_str_2 = '(0';
			$x = get_data('select meta_grupa.rbr from meta_grupa where meta_grupa.rbr_meta_tab in '.$l_str, 1);
			for($i = 0; $i < sizeof($x['rbr']); $i++){
				$l_str_2 .= ', '.$x['rbr'][$i];
			}
			$l_str_2 .= ')';
			//print $l_str.' select rbr from meta_grupa where rbr_meta_tab = '.$l_rbr." xxxxx <br><br>";
				
			//print $l_str_2."<br><br>";
			
			//$l_pk_name = get_item('select ime_polja from meta_item where rbr_meta_grupa = '.$rbr.' and kljuc = "PRI" ');
			$l_pk_name = get_item('select ime_polja from meta_item where rbr_meta_grupa in '.$l_str_2.' and kljuc = "PRI" ');
			
			//print "zzz<br><br>";
			
			//print 'select ime_polja from meta_item where rbr_meta_grupa in '.$l_str_2.' and kljuc = "PRI" <br>';
			
			return $l_pk_name;
	}
	
	
	function gen_items($rbr, $pk_rbr = 0, $table = '', $redak = 0, $has_delete = 0, $subform_tab = 0, $default_value = ''){
		global $g_path_sl;
		global $g_path_sl_res;
		
		//global $ga_js_execute;
		
		$l_style_vrsta_zapisa_add = '';
		//print $pk_rbr."--- <br>";
		
		$result = '';
		
		$redak == 0 ? $l_redak_str = '' : $l_redak_str = '_'.$redak;
		$subform_tab == 0 ? $l_sf_tab_str = '' : $l_sf_tab_str = '_'.$subform_tab;
		
		
		$l_rbr = get_item('select meta_grupa.rbr_meta_tab '.
			' from meta_grupa '.
			' where meta_grupa.rbr = '.$rbr);
		
		$l_rbr2 = get_item('select meta_tab.rbr_meta_forma '.
			' from meta_forma, meta_tab '.
			' where meta_forma.rbr = meta_tab.rbr_meta_forma '.
			' and meta_tab.rbr = '.$l_rbr, 0);
			
		$l_nad_forma = get_item('select rbr_meta_forma_child from meta_subforma where rbr_meta_forma = '.$l_rbr2);
		if(!$l_nad_forma){
			$l_nad_forma = $l_rbr2;
		}
		$l_nad_forma_ime = get_item('select ime from meta_forma where rbr = '.$l_nad_forma);
		
		//print "\n\n";
		//print $l_nad_forma_ime."\n\n";
		
		if($pk_rbr){
			
			$l_pk_name = get_ime_polja($l_rbr2);
			
			$z = get_data('select * from '.$table.' where '.$l_pk_name.' = "'.$pk_rbr.'" ', 0);
			//print "zzzzzzztttttt<br><br>";
			//print 'select ime_polja from meta_item where rbr_meta_grupa in '.$l_str.' and kljuc = "PRI" '."<br><br>";
			
			//print 'select * from '.$table.' where '.$l_pk_name.' = "'.$pk_rbr.'" <br><br>';
			
		}
		
		$la_old = array(
			'd_current_year',
			'd_current_date',
			'd_current_user',
			'd_this_year_end_date',
			'd_this_year_period',
			//'d_pbr_default',
		);
		$la_new = array(
			u_godina(), 
			u_godina().'-'.u_mjesec(0, 1).'-'.u_datum(0, 1).' '.u_sat().':'.u_minuta().':'.u_sekunda(),
			1, //get_item('select id from zaposlenik where rbr_users_outer_db = '.$_SESSION['__id']),
			u_godina().'-12-31',
			'01.01.'.u_godina().'. - 31.12.'.u_godina().'.',
			//(1 + intval(get_item('select max(substring(Broj, 6, 4)) as zbr from izlazni_racuni where substring(Broj, 1, 4) = '.u_godina()))).'-'.substr(u_godina(), 2, 2),
		);
		
		//generate_meta_item('zastitne_radnje_files', 65);
		//generate_meta_prikaz_  item('popravne_radnje', 18, 18);
		                         
		$x = get_data('select rbr, rbr_atribut, ime_polja, tip_polja, tip_polja_maska, kljuc, polje_select, '.
			' polje_order, rbr_meta_grupa, klasa, label, label_klasa, item_klasa, redoslijed, aktivan, aktivan_edit, default_value, rules '.
			' from meta_item '.
			' where rbr_meta_grupa = '.$rbr.
			' order by redoslijed, rbr_atribut ', 1);
		
		/*
		print 'select rbr, rbr_atribut, ime_polja, tip_polja, tip_polja_maska, kljuc, polje_select, '.
			' polje_order, rbr_meta_grupa, klasa, label, label_klasa, item_klasa, redoslijed, aktivan, aktivan_edit, default_value, rules '.
			' from meta_item '.
			' where rbr_meta_grupa = '.$rbr.
			' order by redoslijed, rbr_atribut <br><br>';
		*/
		
		global $g_language;
		
		if($g_language > 1){
			
			$l_rbr_mi = get_item('select rbr from meta_item where rbr_meta_grupa = '.$rbr.' limit 1 ');
			
			//print 'select * from meta_item_lang where rbr = '.$l_rbr_mi.' order by rbr, rbr_atribut <br>';
			
			$x_lang = get_data('select * '.
				' from meta_item_lang '.
				' where rbr = '.$l_rbr_mi.
				' and rbr_jezik = '.$g_language.
				' order by rbr, rbr_atribut ', 1);
				
			print 'select * '.
				' from meta_item_lang '.
				' where rbr = '.$l_rbr_mi.
				' and rbr_jezik = '.$g_language.
				' order by rbr, rbr_atribut <br>';
		}
			
			
			
		$pk_rbr ? $l_akt_str = 'aktivan_edit' : $l_akt_str = 'aktivan';
			
		$l_delete_undelete_str = '';
		
		
		$la_cf_default = array();
		if($_GET['cf']){
			$la_tmp_0 = explode(':', $_GET['cf']);
			for($i = 1; $i < sizeof($la_tmp_0); $i++){
				//print $_GET['cf']."+++".$la_tmp_0[$i]."<br>";
				$la_tmp = explode('-', $la_tmp_0[$i]);
				$la_cf_default[$la_tmp[1]] = $_GET['cf_data'.($i - 1)];
			}
		}else{
			unset($la_cf_default);
		}
		
		
		$l_hidden_value = '';
		for($i = 0; $i < sizeof($x['ime_polja']); $i++){
			
			


			$l_style_vrsta_zapisa_add = '';
			$l_style_vrsta_zapisa_label_add = '';

			
			
			
			if(!$pk_rbr){
				
				//print $rbr." x ".$i." - ".$x['ime_polja'][$i]." ::: ".$default_value." ------ ".$la_cf_default[$x['rbr_atribut'][$i]]."<br>";
				//print $x['rbr'][$i]." = ".$x['rbr_atribut'][$i]." -- ".$la_cf_default[$x['rbr_atribut'][$i]]."<br>";
				
				if($default_value){
					$l_default_value = '';
					
					$la_tmp = explode(';', $default_value);
					for($j = 0; $j < sizeof($la_tmp); $j++){
						$la_tmp_2 = explode(':', $la_tmp[$j]);
						
						if($x['rbr_atribut'][$i] == $la_tmp_2[0]){
							$l_default_value = $la_tmp_2[1];
							break;
						}
					}
				}else{
					$l_default_value = $x['default_value'][$i];
				}
				//print $l_default_value." ------ "."<br>";
				
						
				if($_GET['cf'] && $la_cf_default[$x['rbr_atribut'][$i]]){
					if($la_tmp_0[0] == $x['rbr'][$i]){
						//print $la_tmp_0[0]." => ".$x['rbr'][$i]."xxxx ".$x['rbr_atribut'][$i]." ".$la_cf_default[$x['rbr_atribut'][$i]]."<br>";
						$l_default_value = $la_cf_default[$x['rbr_atribut'][$i]];
						$l_hidden_value = '1';
					}
				}
				
				if($_GET['sid'] && ($_GET['sidg'] == $x['rbr_meta_grupa'][$i])){
					$l_pk_name = get_ime_polja($l_rbr2);
					
					$z = get_data('select * from '.$table.' where '.$l_pk_name.' = "'.$_GET['sid'].'" ', 0);
					$l_default_value = $z[$x['ime_polja'][$i]];
					
					//print $x['rbr_meta_grupa'][$i].' => '.$l_default_value." aa - <br>";
				}
				
				if(strstr($l_default_value, 'd_current_date+')){
					$la_day_num = explode('+', $l_default_value);
					$l_date = mktime (0, 0, 0, date("m"), date("d") + $la_day_num[1], date("Y"));
					$l_default_value = date('Y-m-d', $l_date);
				}else{
					if(strstr($l_default_value, '::g::')){
						$la_tmp_def = explode(':', str_replace('::g::', '', $l_default_value));
						//print_r('select '.$la_tmp_def[1].' from '.$la_tmp_def[0].' where '.$la_tmp_def[2].' limit 1 ');
						$l_default_value = get_item('select '.$la_tmp_def[1].' from '.$la_tmp_def[0].' where '.$la_tmp_def[2].' limit 1 ');
					}else{
						$l_default_value = str_replace($la_old, $la_new, $l_default_value);
					}
				}
				
				$z[$x['ime_polja'][$i]] = $l_default_value;
			}
			
			if($x[$l_akt_str][$i] == 0){
				if($l_hidden_value == '1'){
					$result .= '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" id="formfield_'.$x['rbr_atribut'][$i].
						$l_redak_str.$l_sf_tab_str.'" value="'.$l_default_value.'">'."\n";
				}
				
				continue;
				
				//print "=>>>".$x['ime_polja'][$i]." == ".$x['tip_polja_maska'][$i]."<br>";
				//print " -- =>>>".$l_default_value."<br>";
			}
			
			if($x[$l_akt_str][$i] == 2){
				
				if(!$pk_rbr && $z[$x['ime_polja'][$i]]){
					$l_value = $z[$x['ime_polja'][$i]];
				}else{
					$l_value = '$'.'var_value_'.$x['ime_polja'][$i];
					eval("global $l_value;");
					eval("\$l_value = \"$l_value\";");
				}
				
				//print $l_value."----<br>";
				
				$result .= '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" id="formfield_'.$x['rbr_atribut'][$i].
					$l_redak_str.$l_sf_tab_str.'" value="'.$l_value.'">'."\n";
				continue;
			}
			if($x[$l_akt_str][$i] == 3){
				$result .= '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="db_guid()">'."\n";
				continue;
			}
			if($x[$l_akt_str][$i] == 4){
				$result .= '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="now()">'."\n";
				continue;
			}
			
			if($x['tip_polja_maska'][$i]){
				$x['tip_polja'][$i] = $x['tip_polja_maska'][$i];
			}
			
			//print $x['ime_polja'][$i]." ---- ".$x['tip_polja'][$i]." ---- ".$x['tip_polja_maska'][$i]."<br>";
			
			$la_type = explode('-', $x['tip_polja'][$i]);
			$la_type[0] = strtolower(trim($la_type[0]));
			
			$l_tip_polja = '';
			
			if($la_type[0] == 'char' && !$x['kljuc'][$i]){
				$l_tip_polja = 'txt';
			}
			if($la_type[0] == 'text'){
				$l_tip_polja = 'area';
			}
			if($la_type[0] == 'smallint' || ($x['kljuc'][$i] && $x['kljuc'][$i] != 'PRI')){
				$l_tip_polja = 'select';
			}
			if($la_type[0] == 'int'){
				$l_tip_polja = 'txt';
			}
			if($la_type[0] == 'pass'){
				$l_tip_polja = 'pass';
			}
			if($la_type[0] == 'login'){
				$l_tip_polja = 'login';
			}
			if($la_type[0] == 'maxouterdb'){
				$l_tip_polja = 'maxouterdb';
			}
			if($la_type[0] == 'number'){
				$l_tip_polja = 'number';
			}
			if($la_type[0] == 'datetime'){
				$l_tip_polja = 'datetime';
			}
			if($la_type[0] == 'datetimebig'){
				$l_tip_polja = 'datetimebig';
			}
			if($la_type[0] == 'file'){
				$l_tip_polja = 'file';
			}
			if($la_type[0] == 'multidd'){
				$l_tip_polja = 'multidd';
			}
			if($la_type[0] == 'mydropdown'){
				$l_tip_polja = 'mydropdown';
			}
			if($la_type[0] == 'importtable'){
				$l_tip_polja = 'importtable';
			}
			if($la_type[0] == 'memo'){
				$l_tip_polja = 'memo';
			}
			if($la_type[0] == 'textclick'){
				$l_tip_polja = 'textclick';
			}
			if($la_type[0] == 'texttiny'){
				$l_tip_polja = 'texttiny';
			}
			if($la_type[0] == 'lova'){
				$l_tip_polja = 'lova';
			}
			if($la_type[0] == 'radioizvnesukladnosti'){
				$l_tip_polja = 'radioizvnesukladnosti';
			}
			if($la_type[0] == 'selectyesno'){
				$l_tip_polja = 'selectyesno';
			}
			if($la_type[0] == 'selectio'){
				$l_tip_polja = 'selectio';
			}
			if($la_type[0] == 'label'){
				$l_tip_polja = 'label';
			}
			if($la_type[0] == 'date'){
				$l_tip_polja = 'datetime';
			}
			if($la_type[0] == 'checkbox'){
				$l_tip_polja = 'checkbox';
			}
			if($la_type[0] == 'img'){
				$l_tip_polja = 'img';
			}
			
			//print $la_type[0]." = ".$l_tip_polja." - <br>";
			
			
			
			$l_has_add_items = 0;
			if(strstr($x['label_klasa'][$i], '-addItems')){
				$x['label_klasa'][$i] = str_replace('-addItems', '', $x['label_klasa'][$i]);
				$l_has_add_items = 1;
			}
			
			
			
			
			
			$x['klasa'][$i] == '' ? $l_klasa = '' : $l_klasa = ' class="'.$x['klasa'][$i].'"';
			$x['label_klasa'][$i] == '' ? $l_label_klasa = '' : $l_label_klasa = ' class="'.$x['label_klasa'][$i].'"';
			$x['item_klasa'][$i] == '' ? $l_item_klasa = '' : $l_item_klasa = ' class="'.$x['item_klasa'][$i].'"';
			
			$l_element = '';
			
			$l_element_value = map_element_value($l_tip_polja, $z[$x['ime_polja'][$i]]);
			
			//print $l_element_value." -- <br>";
			
			
			$l_elem_disabled = check_element_disabled($x['rules'][$i], $x['rbr_atribut'], $x['ime_polja'], $z, $pk_rbr);
			
			
			// iznimka - za prikaz tipa dokumenta (jer je sifrarnik izvan definicije kljuc->tablica)
			// vrsta zapisa se moze birati kod kreiranja novog zapisa
			//if(($x['rbr'][$i] == 108 || $x['rbr'][$i] == 110) && $x['rbr_atribut'][$i] == 14 && $x['label'][$i] == 'Vrsta zapisa' && $_GET['c'] == '-1'){
			if(($x['rbr'][$i] == 108) && $x['rbr_atribut'][$i] == 14 && $x['label'][$i] == 'Vrsta zapisa' && $_GET['c'] == '-1'){
				//$l_elem_disabled = 0;
			}
			
			
			if(($x['rbr'][$i] == 112) && ($x['rbr_atribut'][$i] == 1 || $x['rbr_atribut'][$i] == 2) && ($x['label'][$i] == 'Artikl' || $x['label'][$i] == 'Datum') && $_GET['c'] != '-1'){
				$l_disabled = ' disabled="true"';
				$l_disabled_hidden = '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" '.
					'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="'.$l_element_value.'">';
				$l_id_disabled = '_dis';
			}else{
				if($l_elem_disabled == 1){
					if($_GET['c'] == '-1'){
						if(($x['rbr'][$i] == 108 || $x['rbr'][$i] == 110) && $x['rbr_atribut'][$i] == 14 && $x['label'][$i] == 'Vrsta zapisa' && isset($_GET['addition'])){
							switch($_GET['addition']){
								case 'Annulments':
									$l_element_value = '1';
									break;
									
								case 'Transfers':
									$l_element_value = '3';
									break;
									
								case 'Returns':
									$l_element_value = '2';
									break;
									
								case 'Inventura':
									$l_element_value = '4';
									break;
									
								case 'Primka':
									$l_element_value = '0';
									break;
									
								case 'Skladiste':
									$l_element_value = '6';
									break;
									
								default:
									$l_element_value = '0';
							}
						}else{
							$l_element_value = '';
						}
					}
					
					
					$l_disabled = ' disabled="true"';
					$l_disabled_hidden = '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="'.$l_element_value.'">';
					$l_id_disabled = '_dis';
				}else{
					$l_disabled = '';
					$l_disabled_hidden = '';
					$l_id_disabled = '';
				}
			}
			
			
			
			//print $l_tip_polja." - <br><br>";
			
			$l_rules_txt = '';
			
			//print $l_tip_polja."<br>";
			
			switch($l_tip_polja){
				case 'login':
					$l_rules_txt = ' onblur="checkfieldsLogin(this, \''.$_GET['c'].'\')" onkeyup="checkfieldsLogin(this, \''.$_GET['c'].'\')" ';
					
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'value="'.$l_element_value.'"'.$l_item_klasa.$l_disabled.$l_rules_txt.'>'.$l_disabled_hidden;
					break;
					
				case 'pass':
					$l_element = '<input type="password" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'value=""'.$l_item_klasa.$l_disabled.$l_rules_txt.'>'.$l_disabled_hidden;
					break;
					
				case 'txt':
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						
						switch($la_rules[0]){
							case 'get-marza':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 13; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_marza(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', \''.$la_tmp_2[13][1].'\', 0) ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-iznos-primka':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 12; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_iznos_primka(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', 0) ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-proizv-medjuskl-data-kolicina':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 10; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_proizv_medjuskl_data_kolicina(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-zopc-data-new':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 5; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_zopc_data_new(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-razlika-inventura':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 9; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_razlika_inventura(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'racunaj_rabat2':
								if($la_rules[0]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 14; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'racunaj_rabat2(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', '.
										'\''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', '.
										'\''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', \''.$la_tmp_2[13][1].'\', \''.$la_tmp_2[14][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-iznos-ponuda':
								if($la_rules[0]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 14; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_iznos_ponuda(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', '.
										'\''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', '.
										'\''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', \''.$la_tmp_2[13][1].'\', \''.$la_tmp_2[14][1].'\') ';
									
									$l_delete_undelete_str = $l_change.'; ';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-iznos-kn':
								if($la_rules[0]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 5; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_iznos_kn(\''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'concat-fields':
								$la_tmp = explode(';', $la_rules[4]);
								$l_attr_str = '';
								for($j = 0; $j < sizeof($la_tmp); $j++){
									$la_tmp_2 = explode('-', $la_tmp[$j]);
									$l_attr_str .= ':'.$la_tmp_2[1];
								}
								$l_attr_str = substr($l_attr_str, 1);
								$la_tmp_3 = explode('-', $la_rules[2]);
								
								if($la_rules[2]){
									$l_change = 'concat_fields('.$la_tmp_3[1].', \''.$la_rules[3].'\', \''.$l_nad_forma_ime.'\', \''.$l_attr_str.'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
								
							case 'subtract-fields':
								$la_tmp = explode('-', $la_rules[1]);
								$la_tmp_2 = explode('-', $la_rules[2]);
								
								/*
								print_r($la_tmp);
								print "<br><br>";
								print_r($la_tmp_2);
								print "<br><br>";
								*/
								
								if($la_rules[1] && $la_rules[2]){
									$l_change = 'sf_subtracts('.$la_tmp[1].', '.$redak.', '.$la_tmp_2[1].', '.$x['rbr_atribut'][$i].', '.
										'\''.$la_rules[3].'\', \''.$l_nad_forma_ime.'\', \''.$la_rules[4].'\', \''.$la_rules[5].'\')';
									
									$l_delete_undelete_str .= $l_change.'; ';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								
								break;
						}
						
					}
					
					// iznimka - za prikaz tipa dokumenta (jer je sifrarnik izvan definicije kljuc->tablica)
					if(($x['rbr'][$i] == 108 || $x['rbr'][$i] == 110) && $x['rbr_atribut'][$i] == 14 && $x['label'][$i] == 'Vrsta zapisa'){
						$l_iznimka .= ' - '.get_item('select name from receiving_types where id = '.$l_element_value);
						$l_pamti_type_value_rbr = $l_element_value;
					}else{
						$l_iznimka = '';
						$l_pamti_type_value_rbr = '';
					}
					
					/*
					// NAJNOVIJI KOMENTAR: nema iznimke - svi mogu mijenjati cijenu
					// NOVI KOMENTAR: iznimka - pogasi polje za promjenu cijene u Inventuri... Jer inventura ne moze mijenjati cijenu, a ostali mogu.
					// STARI KOMENTAR: iznimka - pogasi polje za promjenu cijene bilo gdje osim u primki... Jer primka moze mijenjati cijenu, a ostali ne mogu.
					if($x['rbr'][$i] == 109 && $x['rbr_atribut'][$i] == 6 && $x['label'][$i] == 'PC'){
						if(isset($_GET['addition']) && 
							//($_GET['addition'] == 'Annulments' || $_GET['addition'] == 'Transfers' || $_GET['addition'] == 'Returns' || $_GET['addition'] == 'Inventura')
							($_GET['addition'] == 'Inventura')
						){
							
							$l_disabled = ' disabled="true"';
							$l_disabled_hidden = '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" '.
								'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="'.$l_element_value.'">';
							$l_id_disabled = '_dis';
						}
					}
					*/
					
					if(($x['rbr'][$i] == 108 || $x['rbr'][$i] == 110) && $x['rbr_atribut'][$i] == 14 && $x['label'][$i] == 'Vrsta zapisa' && $_GET['c'] == '-1'){
						$y = get_data('select id as rbr, name as ime from receiving_types order by id', 1);
						
						$l_element = '<div class="selectWrap"><select name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							$l_item_klasa.$l_disabled.' onchange="select_zapis(this.value, \'0:16_0;1:17_1;2:18_2;3:19_3;4:20_4\')">'."\n";
						$l_selected = '';
						for($j = 0; $j < sizeof($y['rbr']); $j++){
							$y['rbr'][$j] == $l_element_value ?
								$l_selected = ' selected'
								:
								$l_selected = '';
							$l_element .= '<option value="'.$y['rbr'][$j].'"'.$l_selected.'>'.$y['ime'][$j].'</option>'."\n";
						}
						$l_element .= '</select></div>'.$l_disabled_hidden."\n";
					}else{
						$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							'value="'.$l_element_value.$l_iznimka.'"'.$l_item_klasa.$l_disabled.$l_rules_txt.'>'.$l_disabled_hidden;
					}
					
					break;
					
				case 'importtable':
					
					$l_element = '<iframe id="nesuklFrame" name="nesuklFrame" frameborder="0" marginwidth="0" marginheight="0" border="0" '.
						' scrolling="auto" scrollbar="no"'.$l_item_klasa.' src="'.$g_path_sl_res.'if_nesukl.php?rbr='.$_GET['c'].'"></iframe>';
					break;
					
				case 'mydropdown':
					/*
					$redak == 0 ? $l_multidd_add = '' : $l_multidd_add = 'Line';
					if($l_element_value){
						$l_my_val = get_data('select '.$x['polje_select'][$i].' from '.$x['kljuc'][$i].' where id = '.$l_element_value, 0);
					}else{
						$l_my_val = array('rbr', 'ime');
					}
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'_dis" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'_dis" '.
						'value="'.$l_my_val['ime'].'"'.$l_item_klasa.' disabled="true">'.
						'<a href="#" onclick="opcl_mydropdown(\''.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'\', \''.$x['rules'][$i].'\');return false;" class="wButton"'.
						'><img src="'.$g_path_sl_res.'images/arr.png" border="0"></a>'.
						'<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="'.$l_element_value.'">'.
						'<div id="multimydropdown_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" class="multimydropdown'.$l_multidd_add.'"></div>';
						*/
						
					if($l_id_disabled == '_dis'){
						$l_on_change_more = 'onchange_more(this.id); ';
					}else{
						$l_on_change_more = '';
					}
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						switch($la_rules[0]){
							case 'get-proizv-medjuskl-data':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 10; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_proizv_medjuskl_data(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-inventura-data':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 10; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_inventura_data(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-artikl-nc-pc-popust':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 13; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_artikl_nc_pc_popust(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', \''.$la_tmp_2[13][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-artikl-nc-pc-last-primka':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 13; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_artikl_nc_pc_last_primka(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', \''.$la_tmp_2[13][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-cijena-fields':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 7; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_cijena_fields(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', '.
										'\''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-kupac-data':
								if($la_rules[1]){
									for($j = 1; $j < sizeof($la_rules); $j++){
										$la_rules[$j] = str_replace('field-', '', $la_rules[$j]);
									}
									$l_change = 'get_kupac_data(this, \''.$la_rules[1].'\', \''.$la_rules[2].'\', \''.$la_rules[3].'\', \''.$la_rules[4].'\', \''.$la_rules[5].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							case 'get-tecaj-value':
								if($la_rules[1]){
									$la_rules[1] = str_replace('field-', '', $la_rules[1]);
									$l_change = 'get_tecaj_value(this, \''.$la_rules[1].'\', \''.$la_rules[2].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							case 'get-form-value':
								if($la_rules[4]){
									$l_change = 'get_value_from_form(this, \''.$la_rules[3].'\', \''.$la_rules[4].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							case 'get-form-value-max':
								if($la_rules[4]){
									$l_change = 'get_value_from_form_max(this, \''.$la_rules[3].'\', \''.$la_rules[4].'\', \''.$la_rules[5].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							default:
								if($l_on_change_more){
									$l_rules_txt = ' onchange="'.$l_on_change_more.'"';
								}
						}
					}else{
						if($l_on_change_more){
							$l_rules_txt = ' onchange="'.$l_on_change_more.'"';
						}
					}
					$l_filter = '$'.'var_filter_'.$x['ime_polja'][$i];
					eval("global $l_filter;");
					eval("\$l_filter = \"$l_filter\";");
					
					//print 'select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where 1 = 1 '.$l_filter.' order by '.$x['polje_order'][$i]."<br>";
					
					if($l_element_value){
						$l_filter .= ' and id = '.$l_element_value;
					}
					
					//p('select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where 1 = 1 '.$l_filter.' order by '.$x['polje_order'][$i]);
					
					
					if($x['rbr'][$i] == 109 && ($_GET['addition'] == 'Primka' || $_GET['addition'] == 'Annulments' || $_GET['addition'] == 'Transfers' || $_GET['addition'] == 'Returns' || $_GET['addition'] == 'Skladiste')){
						$l_addition = 1;
					}else{
						$l_addition = 0;
					}
					
					$y = get_data('select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where 1 = 1 '.$l_filter.' order by '.$x['polje_order'][$i], 1);
					$l_element = '<div class="selectWrap"><select name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" ';
					$l_element .= $l_item_klasa.$l_disabled.$l_rules_txt.' onfocus="select_ajax_fill(this, '.$rbr.', '.$x['rbr_atribut'][$i].', '.$l_addition.'); return false;">'."\n";
					
					for($j = 0; $j < 30; $j++){
						$l_element .= '<option value=""></option>'."\n";
					}
					$l_selected = '';
					for($j = 0; $j < sizeof($y['rbr']); $j++){
						$y['rbr'][$j] == $l_element_value ?
							$l_selected = ' selected'
							:
							$l_selected = '';
						$l_element .= '<option value="'.$y['rbr'][$j].'"'.$l_selected.'>'.$y['ime'][$j].'</option>'."\n";
					}
					$l_element .= '</select></div>'.$l_disabled_hidden."\n";
					
					break;
					
				case 'multidd':
					$redak == 0 ? $l_multidd_add = '' : $l_multidd_add = 'Line';
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'_dis" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'_dis" '.
						'value="'.$l_element_value.'"'.$l_item_klasa.' disabled="true">'.
						'<a href="#" onclick="opcl_multidd(\''.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'\', \''.$x['rules'][$i].'\');return false;" class="wButton">..</a>'.
						'<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="'.$l_element_value.'">'.
						'<div id="multidd_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" class="multidd'.$l_multidd_add.'"></div>';
					break;
					
				case 'memo':
					$redak == 0 ? $l_memo_add = '' : $l_memo_add = 'Line';
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'_dis" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'_dis" '.
						'value="'.$l_element_value.'"'.$l_item_klasa.' disabled="true">'.
						'<a href="#" onclick="opcl_memo(\''.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'\');return false;" class="wButton">..</a>'.
						'<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" value="'.$l_element_value.'">'.
						'<div id="memo_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'" class="memo'.$l_memo_add.'"></div>';
					break;
					
				case 'number':
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						switch($la_rules[0]){
							case 'max-lines':
								$la_tmp = explode('-', $la_rules[1]);
								$la_tmp_2 = explode('-', $la_rules[2]);
								break;
						}
						if($la_rules[1] && $la_rules[2]){
							$l_change = 'sf_max('.$la_tmp[1].', '.$la_tmp_2[1].', '.$x['rbr_atribut'][$i].', \''.$la_rules[3].'\', \''.$l_nad_forma_ime.'\') ';
							
							$l_delete_undelete_str .= $l_change.'; ';
							
							$l_rules_txt = ' onchange="'.$l_change.'"';
						}
					}
					
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'value="'.$l_element_value.'"'.$l_item_klasa.$l_disabled.$l_rules_txt.'>'.$l_disabled_hidden;
					break;
					
				case 'lova':
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						switch($la_rules[0]){
							case 'sum-lines':
								$la_tmp = explode('-', $la_rules[1]);
								$la_tmp_2 = explode('-', $la_rules[2]);
								break;
						}
						if($la_rules[1] && $la_rules[2]){
							$l_change = 'sf_sums('.$la_tmp[1].', '.$la_tmp_2[1].', '.$x['rbr_atribut'][$i].', \''.$la_rules[3].'\', \''.$l_nad_forma_ime.'\')';
							
							$l_delete_undelete_str .= $l_change.'; ';
							
							$l_rules_txt = ' onchange="'.$l_change.'"';
						}
					}
					
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'value="'.$l_element_value.'"'.$l_item_klasa.$l_disabled.$l_rules_txt.'>'.$l_disabled_hidden;
					break;
					
				case 'label':
					$l_veza_sufix = '';
					
					switch($la_type[1]){
						case 'rbr':
							$l_id = '';
							break;
						
						case 'db':
							$l_id = '';
							break;
							
						default:
							$la_tmp_rules = explode(':', $x['rules'][$i]);
							if($redak > 0){
								$x['rules'][$i] ? $l_id = ' id="'.$la_tmp_rules[0].'-'.$redak.'"' : $l_id = '';
							}else{
								$x['rules'][$i] ? $l_id = ' id="'.$la_tmp_rules[0].'"' : $l_id = '';
							}
					}
					
					switch($la_type[1]){
						case 'money':
							$l_label_value = lova($l_element_value).' Kn';
							break;
							
						case 'number':
							$l_label_value = $l_element_value;
							break;
							
						case 'select':
							$l_filter = '$'.'var_filter_'.$x['ime_polja'][$i];
							eval("global $l_filter;");
							eval("\$l_filter = \"$l_filter\";");
					
							$l_label_value = '';
							$y = get_data('select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where 1 = 1 '.$l_filter.' order by '.$x['polje_order'][$i], 1);
							for($j = 0; $j < sizeof($y['rbr']); $j++){
								if($y['rbr'][$j] == $l_element_value){
									$l_label_value = $y['ime'][$j];
									break;
								}
							}
							
							if($l_element_value == ''){
								$l_element_value = 'NULL';
							}
							//print $l_element_value." ++ <br>";
					
							break;
							
						case 'txt':
							$l_label_value = $l_element_value;
							break;
							
						case 'db':
							if($pk_rbr){
								$l_label_value = $l_element_value;
							}else{
								if($x['rules'][$i]){
									$la_tmp = explode(':', $x['rules'][$i]);
									if($la_tmp[0] == 'getValueFrom'){
										$l_label_value = get_item($la_tmp[1]);
									}
								}
							}
							
							$l_element_value = $l_label_value;
							break;
							
						case 'rbr':
						
							$la_replace_1 = [
								'{_u_godina()_}',
								'{_u_godina_current()_}',
							];
							$la_replace_2 = [
								u_godina(),
								u_godina_current(),
							];
							
							if($pk_rbr){
								if($x['rules'][$i]){
									$la_tmp = explode(':', $x['rules'][$i]);
									//p($la_tmp);
									if($la_tmp[0] == 'sql_rbr_fiskal'){
										$la_tmp_fiskal = explode('/', $l_element_value);
										$l_label_value = intval($la_tmp_fiskal[1]).'/'.$la_tmp_fiskal[2].'/'.$la_tmp_fiskal[3];
									}
									if($la_tmp[0] == 'sql_rbr'){
										
										if(($x['rbr'][$i] == 112 && $x['rbr_atribut'][$i] == 1 && $x['label'][$i] == 'Broj')){
											$l_pamti_type_value_rbr = 0;
										}
										
										if(isset($la_tmp[2]) && isset($l_pamti_type_value_rbr)){
											if($la_tmp[2] == $l_pamti_type_value_rbr){
												
												$l_label_value = $l_element_value;
												$l_element_value = $l_element_value;
												$l_style_vrsta_zapisa_add = ' style="background:#e2d2c2; color:#222222"';
												$l_style_vrsta_zapisa_label_add = '';
											}else{
												//$l_label_value = '';
												//$l_element_value = '';
												//$l_style_vrsta_zapisa_add = ' style="background:#f1f1f1; color:#999999"';
												
												
												$l_label_value = '';
												$l_element_value = '';
												$l_style_vrsta_zapisa_add = ' style="display:none; "';
												$l_style_vrsta_zapisa_label_add = ' style="display:none; "';
												
											}
										}else{
											$l_cnt = intval(get_item(str_replace($la_replace_1, $la_replace_2, $la_tmp[1])));
											$l_label_value = u_godina_current().'/'.sprintf("%06d", $l_cnt);
											$l_element_value = $l_label_value;
											$l_veza_sufix = '_'.$la_tmp[2];
										}
										
									}
								}else{
									$l_label_value = $l_element_value;
								}
							}else{
								if($x['rules'][$i]){
									$la_tmp = explode(':', $x['rules'][$i]);
									if($la_tmp[0] == 'sql_rbr_fiskal'){
										
										//print $la_tmp[1]."<br>";
										//print str_replace('{_u_godina()_}', u_godina(), $la_tmp[1])."<br>";
										
										$l_cnt = 1 + intval(get_item(str_replace($la_replace_1, $la_replace_2, $la_tmp[1])));
										
										$la_txt = get_data('select txt1, txt2 from opcija_format_racuna where rbr = 1 ', 0);
										$l_label_value = $l_cnt.'/'.$la_txt['txt1'].'/'.$la_txt['txt2'];
										$l_element_value = u_godina_current().'/'.sprintf("%04d", $l_cnt).'/'.$la_txt['txt1'].'/'.$la_txt['txt2'];
									}else{
										if($la_tmp[0] == 'sql_rbr'){
											
											$l_cnt = 1 + intval(get_item(str_replace($la_replace_1, $la_replace_2, $la_tmp[1])));
											
											if(isset($la_tmp[2]) && isset($l_pamti_type_value_rbr)){
												if($la_tmp[2] == $l_pamti_type_value_rbr){
													//p($l_pamti_type_value_rbr);
													//p($la_tmp[2]);
													$l_style_vrsta_zapisa_add = ' style="background:#e2d2c2; color:#222222"';
												}else{
													$l_style_vrsta_zapisa_add = '';
													$l_element_value = '--#--skip--#--';
												}
											}
											
										}
										// ne znam vise zasto je ovo ostalo izvan gornjeg if uvjeta i smijem li to ubaciti u taj if uvjet...
										
										$l_veza_sufix = '_'.$la_tmp[2];
										
										if($l_element_value == '--#--skip--#--'){
											$l_label_value = '';
											$l_element_value = '';
											$l_style_vrsta_zapisa_add = ' style="display:none; "';
											$l_style_vrsta_zapisa_label_add = ' style="display:none; "';
										}else{
											$l_label_value = u_godina_current().'/'.sprintf("%06d", $l_cnt);
											$l_element_value = $l_label_value;
											$l_style_vrsta_zapisa_label_add = '';
										}
									}
								}else{
									$l_label_value = u_godina_current().'/'.sprintf("%04d", $l_cnt);
									$l_element_value = $l_label_value;
								}
							}
							break;
						
						case 'file':
							$l_element_value ?
								$l_label_value = '<a href="'.$l_element_value.'" target="_blank"><img src="'.$g_path_sl_res.'images/document.jpg" border="0" title="'.$l_element_value.'"></a>'
								:
								$l_label_value = '';
							break;
							
						default:
							$l_label_value = '';
					}
					
					//print 'formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'<br>';
				
					$l_element = '<div'.$l_id.$l_item_klasa.'id="div_formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_veza_sufix.'"'.$l_style_vrsta_zapisa_add.'>'.$l_label_value.'</div>'.
						'<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_veza_sufix.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_veza_sufix.'" value="'.$l_element_value.'">';
					break;
					
				case 'file':
					$l_file_flag = '0';
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						switch($la_rules[0]){
							case 'get-file-max':
								if($la_rules[4]){
									$l_change = 'get_file_max(this, \''.$la_rules[3].'\', \''.$la_rules[4].'\', \''.$la_rules[5].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
									
									$l_file_flag = '1';
								}
								break;
						}
					}
				
					$l_element_value == '' ?
						$l_file = ''
						:
						$l_file = '<a href="'.$l_element_value.'" target="_blank"><img src="'.$g_path_sl_res.'images/document.jpg" border="0" title="'.$l_element_value.'"></a>';
					
					$pk_rbr ?
						$l_element = '<div id="lnkDodajDatoteku'.$l_redak_str.$l_sf_tab_str.'"><a href="#" '.
							'onclick="parent.a_redak[parent.active_tab - 1] = '.$redak.'; new_file('.$x['rbr_atribut'][$i].', \''.$l_redak_str.$l_sf_tab_str.'\', 1, \'\', '.$l_file_flag.'); return false;" '.
							'class="upload" title="Dodaj datoteku"><img src="res/images/b-add-m.gif" border="0"></a></div>'
						:
						$l_element = '<div id="lnkDodajDatoteku'.$l_redak_str.$l_sf_tab_str.'"><a href="#" '.
							'onclick="new_file('.$x['rbr_atribut'][$i].', \''.$l_redak_str.$l_sf_tab_str.'\', 1, \'\', '.$l_file_flag.'); return false;" '.
							'class="upload" title="Dodaj datoteku"><img src="res/images/b-add-m.gif" border="0"></a></div>';
							
					$l_element .= '<div id="dodajDatotekuImg'.$l_redak_str.$l_sf_tab_str.'" class="dodajDatotekuImgClass">'.$l_file.'</div>'.
						'<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" value="'.$l_element_value.'"'.$l_item_klasa.$l_rules_txt.'>';
					
					break;
					
				case 'calendar':
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						' value="'.$l_element_value.'"'.$l_item_klasa.'>';
					break;
					
				case 'datetime':
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" value="'.$l_element_value.'"'.$l_item_klasa.$l_disabled.'>'.$l_disabled_hidden;
					if($l_elem_disabled == 1){
						$l_element .= '<img src="'.$g_path_sl_res.'js/datetimepicker_css/cal-disabled.gif" class="datetimepickerdisabled" '.
							'onclick="return false;">';
					}else{
						$l_element .= '<img src="'.$g_path_sl_res.'js/datetimepicker_css/cal.gif" class="datetimepicker" '.
							'onclick="NewCssCal(\'formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'\', \'ddMMyyyy\', \'arrow\'); return false;">';
					}
					break;
					
				case 'maxouterdb':
					$l_element .= 'aaaaa';
					break;
					
				case 'datetimebig':
					if($x['rules'][$i] == 'strip-seconds'){
						$l_element_value = substr($l_element_value, 0, strlen($l_element_value) - 3);
					}
					$l_element = '<input type="text" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" value="'.$l_element_value.'"'.$l_item_klasa.$l_disabled.'>'.$l_disabled_hidden;
					if($l_elem_disabled == 1){
						$l_element .= '<img src="'.$g_path_sl_res.'js/datetimepicker_css/cal-disabled.gif" class="datetimepickerdisabled" '.
							'onclick="return false;">';
					}else{
						$l_element .= '<img src="'.$g_path_sl_res.'js/datetimepicker_css/cal.gif" class="datetimepicker" '.
							'onclick="NewCssCal(\'formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'\', \'ddMMyyyy\', \'arrow\', \'yes\', 24); return false;">';
					}
					break;
					
				case 'checkbox':
					$l_on_click = '';
					
					if($x['rules'][$i]){
						$la_more_rules = explode(';', $x['rules'][$i]);
						for($k = 0; $k < sizeof($la_more_rules); $k++){
							$la_rules = explode(':', $la_more_rules[$k]);
							switch($la_rules[0]){
								case 'xdisable':
									if($la_rules[0]){
										$l_ima_li = get_item('select rbr from '.$la_rules[1].' where '.$la_rules[2].' limit 1 ');
										if($l_ima_li){
											$l_disabled = ' disabled="true" ';
											$l_element_value = '';
											//print 'select rbr from '.$la_rules[1].' where '.$la_rules[2].' limit 1 '.$l_checked;
										}else{
											$l_disabled = '';
										}
									}
									break;
									
								case 'racunaj-pdv':
									if($la_rules[0]){
										
										$la_tmp_2 = array();
										for($j = 1; $j <= 14; $j++){
											$la_tmp_2[$j] = explode('-', $la_rules[$j]);
										}
										
										$l_change = 'racunaj_pdv(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', '.
											'\''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', '.
											'\''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\', \''.$la_tmp_2[11][1].'\', \''.$la_tmp_2[12][1].'\', \''.$la_tmp_2[13][1].'\', \''.$la_tmp_2[14][1].'\') ';
										$l_on_click = ' onclick="'.$l_change.'"';
										
										//$l_delete_undelete_str .= '';
										
										//$l_rules_txt = ' onchange="'.$l_change.'"';
									}
									break;
							}
							
							
						}
					}
					
					switch($la_type[1]){
						case 'click':
							if($x['rules'][$i]){
								$la_rules = explode(':', $x['rules'][$i]);
								switch($la_rules[0]){
									case 'click-label':
										$la_tmp = explode('-', $la_rules[1]);
										
										$l_on_click = ' onclick="cb_click_label(this, \''.$la_tmp[1].'\', \''.$x['label'][$i].'\')" ';
										
										$l_item_klasa = str_replace(' class="', ' class="checkbox-click ', $l_item_klasa);
										
										if($la_rules[4]){
											$l_change = 'get_value_from_form(this, \''.$la_rules[3].'\', \''.$la_rules[4].'\') ';
											
											$l_delete_undelete_str .= '';
											
											$l_rules_txt = ' onchange="'.$l_change.'"';
										}
										break;
								}
							}
							
							break;
					}
					
					$l_element_value == 1 ?
						$l_checked = ' checked'
						:
						$l_checked = '';
						
					$l_element = '<input type="checkbox" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" value="1"'.
						' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						$l_item_klasa.$l_checked.$l_disabled.$l_on_click.'>'.$l_disabled_hidden;
					break;
					
				case 'radioizvnesukladnosti':
					$y = array(
						'rbr' => array('IA', 'Vanjski izvor', 'Proces'),
						'ime' => array('IA', 'Vanjski izvor', 'Proces')
					);
					$l_element = '';
					for($j = 0; $j < sizeof($y['rbr']); $j++){
						$y['rbr'][$j] == $l_element_value ?
							$l_checked = ' checked'
							:
							$l_checked = '';
						$l_element .= '<input type="radio" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						$l_item_klasa.$l_disabled.' value="'.$y['rbr'][$j].'"'.$l_checked.'><span class="radiolabel">'.$y['rbr'][$j].'</span>'."\n";
					}
					$l_element .= $l_disabled_hidden."\n";
					break;
					
				case 'selectyesno':
					$y = array(
						'rbr' => array('DA', 'NE'),
						'ime' => array('DA', 'NE'),
					);
					$l_element = '<div class="selectWrap"><select name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						$l_item_klasa.$l_disabled.'>'."\n";
					$l_element .= '<option value="NULL">--</option>'."\n";
					$l_selected = '';
					for($j = 0; $j < sizeof($y['rbr']); $j++){
						$y['rbr'][$j] == $l_element_value ?
							$l_selected = ' selected'
							:
							$l_selected = '';
						$l_element .= '<option value="'.$y['rbr'][$j].'"'.$l_selected.'>'.$y['ime'][$j].'</option>'."\n";
					}
					$l_element .= '</select></div>'.$l_disabled_hidden."\n";
					break;
					
				case 'selectio':
					$l_element = '';
					$l_rules_txt = '';
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						switch($la_rules[0]){
							case 'switch-field':
								$l_js_str = '';
								for($j = 1; $j < sizeof($la_rules); $j++){
									$l_js_str .= ','.str_replace('field-', '', $la_rules[$j]);
								}
								$l_change = 'switch_field(this.value, \''.substr($l_js_str, 1).'\');';
								
								//print $l_change;
								
								$l_rules_txt = ' onchange="'.$l_change.'"';
								
								if($l_element_value){
									$l_element = '<script type="text/javascript">window.onload = function() {switch_field(\''.$l_element_value.'\', \''.substr($l_js_str, 1).'\');}</script>';
								}
								
								break;
						}
					}
					
					$y = array(
						'rbr' => array('U', 'I'),
						'ime' => array('U', 'I'),
					);
					$l_element .= '<div class="selectWrap"><select name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						$l_item_klasa.$l_disabled.$l_rules_txt.'>'."\n";
					$l_element .= '<option value="NULL">--</option>'."\n";
					$l_selected = '';
					for($j = 0; $j < sizeof($y['rbr']); $j++){
						$y['rbr'][$j] == $l_element_value ?
							$l_selected = ' selected'
							:
							$l_selected = '';
						$l_element .= '<option value="'.$y['rbr'][$j].'"'.$l_selected.'>'.$y['ime'][$j].'</option>'."\n";
					}
					$l_element .= '</select></div>'.$l_disabled_hidden."\n";
					break;
					
				case 'select':
					if($l_id_disabled == '_dis'){
						$l_on_change_more = 'onchange_more(this.id); ';
					}else{
						$l_on_change_more = '';
					}
					if($x['rules'][$i]){
						$la_rules = explode(':', $x['rules'][$i]);
						switch($la_rules[0]){
							case 'get-zopc-data':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 8; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_zopc_data(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
							
							case 'get-proizv-medjuskl-data':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 10; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_proizv_medjuskl_data(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-inventura-data':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 6; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_inventura_data(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-artikl-nc-pc-popust':
								if($la_rules[1]){
									$la_tmp_2 = array();
									//for($j = 1; $j <= 10; $j++){
									for($j = 1; $j <= 5; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									//$l_change = 'get_artikl_nc_pc_popust(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\', \''.$la_tmp_2[8][1].'\', \''.$la_tmp_2[9][1].'\', \''.$la_tmp_2[10][1].'\') ';
									$l_change = 'get_artikl_nc_pc_popust(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', \''.$la_tmp_2[5][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-cijena-fields':
								if($la_rules[1]){
									$la_tmp_2 = array();
									for($j = 1; $j <= 7; $j++){
										$la_tmp_2[$j] = explode('-', $la_rules[$j]);
									}
									$l_change = 'get_cijena_fields(this, \''.$la_tmp_2[1][1].'\', \''.$la_tmp_2[2][1].'\', \''.$la_tmp_2[3][1].'\', \''.$la_tmp_2[4][1].'\', '.
										'\''.$la_tmp_2[5][1].'\', \''.$la_tmp_2[6][1].'\', \''.$la_tmp_2[7][1].'\') ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.'"';
								}
								break;
								
							case 'get-kupac-data':
								if($la_rules[1]){
									for($j = 1; $j < sizeof($la_rules); $j++){
										$la_rules[$j] = str_replace('field-', '', $la_rules[$j]);
									}
									$l_change = 'get_kupac_data(this, \''.$la_rules[1].'\', \''.$la_rules[2].'\', \''.$la_rules[3].'\', \''.$la_rules[4].'\', \''.$la_rules[5].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							case 'get-tecaj-value':
								if($la_rules[1]){
									$la_rules[1] = str_replace('field-', '', $la_rules[1]);
									$l_change = 'get_tecaj_value(this, \''.$la_rules[1].'\', \''.$la_rules[2].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							case 'get-form-value':
								if($la_rules[4]){
									$l_change = 'get_value_from_form(this, \''.$la_rules[3].'\', \''.$la_rules[4].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							case 'get-form-value-max':
								if($la_rules[4]){
									$l_change = 'get_value_from_form_max(this, \''.$la_rules[3].'\', \''.$la_rules[4].'\', \''.$la_rules[5].'\'); ';
									
									$l_delete_undelete_str .= '';
									
									$l_rules_txt = ' onchange="'.$l_change.$l_on_change_more.'"';
								}
								break;
								
							default:
								if($l_on_change_more){
									$l_rules_txt = ' onchange="'.$l_on_change_more.'"';
								}
						}
					}else{
						if($l_on_change_more){
							$l_rules_txt = ' onchange="'.$l_on_change_more.'"';
						}
					}
					$l_filter = '$'.'var_filter_'.$x['ime_polja'][$i];
					eval("global $l_filter;");
					eval("\$l_filter = \"$l_filter\";");
					
					//print 'select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where 1 = 1 '.$l_filter.' order by '.$x['polje_order'][$i]."<br>";
					
					
					if(($x['rbr'][$i] == 112) && $x['rbr_atribut'][$i] == 2 && $x['label'][$i] == 'Artikl' && $_GET['c'] != '-1'){
					
						$y = get_data('select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where id = '.$l_element_value.' order by '.$x['polje_order'][$i], 0);
						$l_element = '<div class="inputWrap"><input name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							$l_item_klasa.$l_disabled.$l_rules_txt.' value="'.$y['ime'].'">'."\n";
						$l_selected = '';
						$l_element .= '</div>'.$l_disabled_hidden."\n";
						
					}else{
						$y = get_data('select '.str_replace('f_', '', $x['polje_select'][$i]).' from '.$x['kljuc'][$i].' where 1 = 1 '.$l_filter.' order by '.$x['polje_order'][$i], 1);
						$l_element = '<div class="selectWrap"><select name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
							$l_item_klasa.$l_disabled.$l_rules_txt.'>'."\n";
						$l_element .= '<option value="NULL">--</option>'."\n";
						$l_selected = '';
						for($j = 0; $j < sizeof($y['rbr']); $j++){
							$y['rbr'][$j] == $l_element_value ?
								$l_selected = ' selected'
								:
								$l_selected = '';
							$l_element .= '<option value="'.$y['rbr'][$j].'"'.$l_selected.'>'.$y['ime'][$j].'</option>'."\n";
						}
						
						
						if($l_has_add_items == 1){
							$l_add_items = '<a href="#" onclick="add_new_item_in_dd(this, '.$x['rbr'][$i].', '.$x['rbr_atribut'][$i].'); return false;" class="add-items-button">+</a>';
						}else{
							$l_add_items = '';
						}
						
						$l_element .= '</select></div>'.$l_add_items.$l_disabled_hidden."\n";
					}
					
					break;
					
				case 'area':
					$l_element = '<textarea name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'"'.
						$l_item_klasa.$l_disabled.'>'.$l_element_value.'</textarea>'.$l_disabled_hidden;
					break;
					
				case 'textclick':
					$l_element = '<textarea name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'"'.
						$l_item_klasa.$l_disabled.' onkeyup="check_click_label(this)">'.$l_element_value.'</textarea>'.$l_disabled_hidden;
					break;
					
				case 'texttiny':
					$l_element = '<textarea name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						' id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'"'.
						$l_item_klasa.$l_disabled.'>'.$l_element_value.'</textarea>'.$l_disabled_hidden;
					break;
					
				case 'img':
					//$l_element_value == '' ? $l_image = 'person.jpg' : $l_image = 'person/1/'.$l_element_value;
					$l_element_value == '' ? $l_image = 'res/images/person.jpg' : $l_image = $l_element_value;
					$l_element = '<input type="hidden" name="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" '.
						'id="formfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.$l_id_disabled.'" value="'.$l_element_value.'"'.$l_item_klasa.'>'."\n";
					$l_element .= '<img src="'.$g_path_sl.$l_image.'" border="0"'.$l_item_klasa.' id="imgfield_'.$x['rbr_atribut'][$i].$l_redak_str.$l_sf_tab_str.'">';
					break;
					
				default:
			}
			switch($l_tip_polja){
				case 'img':
					$result .= '<span'.$l_klasa.'>';
					//$result .= '<div id="imgdiv_'.$x['rbr_atribut'][$i].'" class="hiddenImgWindow"><iframe id="upload_frame" '.
					//	'name="upload_frame" frameborder="0" border="0" src="" scrolling="no" scrollbar="no"></iframe></div>'."\n";
					$result .= $l_element."\n";
					//$result .= '<b'.$l_label_klasa.'><a href="#" onclick="new_image(\''.$_GET['c'].'\', '.$x['rbr_atribut'][$i].'); return false;">'.$x['label'][$i].'</a></b>'."\n";
					$result .= '<b'.$l_label_klasa.'><a href="#" onclick="new_file('.$x['rbr_atribut'][$i].', \'\', 2, \''.$_GET['c'].'\', 0); return false;">'.
						$x['label'][$i].'</a></b>'."\n";
					$result .= '</span>'."\n";
					break;
					
				default:
					$result .= '<div'.$l_klasa.'>';
					
					if($_GET['editmove'] == 1234 && $_SESSION['__id'] == 1){
						$l_div_podrucje = ' &nbsp; <a href="#" onclick="selectP(this); return false;">X</a>';
					}else{
						$l_div_podrucje = '';
					}
					
					
					
					if($g_language > 1 && $x_lang['label'][$x['rbr_atribut'][$i] - 1]){
						$l_label_lang = $x_lang['label'][$x['rbr_atribut'][$i] - 1];
					}else{
						$l_label_lang = $x['label'][$i];
					}
					
					
					if(isset($_GET['addition']) && $_GET['addition'] == 'Transfers'){
						if($l_label_lang == 'Dobavljač'){
							$l_label_lang = 'Korisniku';
						}
					}
					
					
					$result .= '<b'.$l_label_klasa.$l_style_vrsta_zapisa_label_add.'>'.$l_label_lang.$l_div_podrucje.' </b>'."\n";
					
					
					$result .= '<div class="inpWrap">'.$l_element.'</div>'."\n";
					$result .= '</div>'."\n";
			}
			
		}
		if($has_delete){
			$result .= '<input type="hidden" name="delete_row'.$l_redak_str.$l_sf_tab_str.'" id="delete_row'.$l_redak_str.$l_sf_tab_str.'" value="0">'."\n";
			$result .= '<div class="grupaDelete"><a href="#" onclick="subform_delete_row(this, '.$redak.', '.$subform_tab.
				'); '.$l_delete_undelete_str.' return false;" title="Delete row" id="formfield_DELETE_'.$redak.'_'.$subform_tab.'">'.
				'<img src="'.$g_path_sl_res.'images/b-del-m.gif" border="0" title="Obriši podatak" class="imgSmallSubForm"></a></div>';
		}
		
		return $result;
	}
	
	function gen_groups($rbr, $pk_rbr = 0, $table = '', $redak = 0, $has_delete = 0, $subform_tab = 0, $default_value = ''){
		$x = get_data('select rbr, klasa, show_label, label, label_klasa, title, aktivan '.
			' from meta_grupa '.
			' where rbr_meta_tab = '.$rbr.
			' and aktivan = 1 '.
			' order by redoslijed ', 1);
		if(sizeof($x['rbr']) == 1){
			if($x['show_label'][0]){
				$x['klasa'][0] == '' ? $l_klasa = '' : $l_klasa = ' class="'.$x['klasa'][0].'"';
				$x['label_klasa'][0] == '' ? $l_label_klasa = '' : $l_label_klasa = ' class="'.$x['label_klasa'][0].'"';
				$result .= '<div id="group_'.$redak.'_'.$subform_tab.'"'.$l_klasa.'><p'.$l_label_klasa.'>'.$x['label'][0].'</p>'.
					gen_items($x['rbr'][0], $pk_rbr, $table, $redak, $has_delete, $subform_tab, $default_value).'</div>';
			}else{
				$x['klasa'][0] == '' ? $l_klasa = '' : $l_klasa = ' class="'.$x['klasa'][0].'"';
				$result .= '<div id="group_'.$redak.'_'.$subform_tab.'"'.$l_klasa.'>'.
					gen_items($x['rbr'][0], $pk_rbr, $table, $redak, $has_delete, $subform_tab, $default_value).'</div>';
			}
		}else{
			for($i = 0; $i < sizeof($x['rbr']); $i++){
				$x['klasa'][$i] == '' ? $l_klasa = '' : $l_klasa = ' class="'.$x['klasa'][$i].'"';
				if($x['show_label'][$i]){
					$x['label_klasa'][$i] == '' ? $l_label_klasa = '' : $l_label_klasa = ' class="'.$x['label_klasa'][$i].'"';
					$result .= '<div id="group_'.$redak.'_'.$subform_tab.'"'.$l_klasa.'><p'.$l_label_klasa.'>'.$x['label'][$i].'</p>'.
						gen_items($x['rbr'][$i], $pk_rbr, $table, $redak, $has_delete, $subform_tab).'</div>';
				}else{
					$result .= '<div id="group_'.$redak.'_'.$subform_tab.'"'.$l_klasa.'>'.
						gen_items($x['rbr'][$i], $pk_rbr, $table, $redak, $has_delete, $subform_tab).'</div>';
				}
			}
		}
		
		return $result;
	}
	
	function gen_tabs($rbr, $default_tab, $pk_rbr = 0, $table = '', $redak = 0, $has_delete = 0, $subform_tab = 0, $default_value = ''){
		
		
		$y = get_data('select rbr, tab_related from meta_subforma where rbr_meta_forma_child = '.$rbr.' order by redoslijed limit 1 ', 0);
		
		//print_r($y);
		
		$x = get_data('select rbr, klasa, show_label, label, label_klasa, title, aktivan, is_tabular '.
			' from meta_tab '.
			' where rbr_meta_forma = '.$rbr.
			' and aktivan = 1 '.
			' order by redoslijed ', 1);
			
		if(sizeof($x['rbr']) == 1){
			if(!$x['show_label'][0]){
				$result .= gen_groups($x['rbr'][0], $pk_rbr, $table, $redak, $has_delete, $subform_tab, $default_value);
			}
		}else{
			$la_akt_tab = array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
			$tab_result = '';
			$l_labels = '';
			$l_first_tabular = -1;
			for($i = 0; $i < sizeof($x['rbr']); $i++){
				if($x['is_tabular'][$i] == 1){
					if($l_first_tabular == -1){
						$l_first_tabular = $i;
						$la_akt_tab[$i] = 'Akt';
					}
					$x['label_klasa'][$i] == '' ? $l_label_klasa = '' : $l_label_klasa = ' class="'.$x['label_klasa'][$i].' sfTab'.$la_akt_tab[$i].'"';
					
					if($y['tab_related']){
						$l_js_funkcija = 'open_close_all_labels_tab_related(this, \''.$y['tab_related'].'\', '.$i.')';
					}else{
						$l_js_funkcija = 'open_close_all_labels(this)';
					}
					$l_labels .= '<p'.$l_label_klasa.'><a href="#" onclick="'.$l_js_funkcija.'; return false;">'.$x['label'][$i].'</a></p>';
					
					$tab_result .= "\n".'<div id="'.$x['klasa'][$i].'" class="'.$x['klasa'][$i].'">';
					$tab_result .= gen_groups($x['rbr'][$i], $pk_rbr, $table, $redak, $has_delete, $subform_tab);
					$tab_result .= '</div>';
				}else{
					$result .= gen_groups($x['rbr'][$i], $pk_rbr, $table, $redak, $has_delete, $subform_tab);
				}
			}
			$result = $result.'<div id="allLabels" class="allLabels">'.$l_labels.'</div>'.$tab_result;
		}
		
		return $result;
	}
	
	function gen_checkfieldslogin($ime){
		$l_table = get_item('select meta_tablica_list.ime_tablice '.
			' from meta_forma, meta_tablica_list '.
			' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_forma.ime = "'.$ime.'" '.
			' order by meta_forma.rbr limit 1 ');
		
		$x = get_data('select meta_item.rbr_atribut '.
			' from meta_forma, meta_tablica_list, meta_tab, meta_grupa, meta_item '.
			' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_tab.rbr_meta_forma = meta_forma.rbr '.
			' and meta_grupa.rbr_meta_tab = meta_tab.rbr '.
			' and meta_item.rbr_meta_grupa = meta_grupa.rbr '.
			' and meta_tablica_list.ime_tablice = "'.$l_table.'" '.
			' and meta_forma.ime = "'.$ime.'" '.
			' and meta_item.tip_polja_maska = "login" '.
			' order by meta_item.redoslijed ', 0);
			
		$result = $x['rbr_atribut'];
		
		return $result;
	}
	
	
	function gen_checkfields($ime){
		
		$l_table = get_item('select meta_tablica_list.ime_tablice '.
			' from meta_forma, meta_tablica_list '.
			' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_forma.ime = "'.$ime.'" '.
			' order by meta_forma.rbr limit 1 ');
		
		$x = get_data('select meta_item.rbr_atribut, meta_item.tip_polja, meta_item.tip_polja_maska, meta_item.kljuc, meta_item.mandatory '.
			' from meta_forma, meta_tablica_list, meta_tab, meta_grupa, meta_item '.
			' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_tab.rbr_meta_forma = meta_forma.rbr '.
			' and meta_grupa.rbr_meta_tab = meta_tab.rbr '.
			' and meta_item.rbr_meta_grupa = meta_grupa.rbr '.
			' and meta_tablica_list.ime_tablice = "'.$l_table.'" '.
			' and meta_forma.ime = "'.$ime.'" '.
			' and meta_item.mandatory = 1'.
			' order by meta_item.redoslijed ', 1);
			
		$result = '';
		for($i = 0; $i < sizeof($x['rbr_atribut']); $i++){
			$result .= ':'.$x['rbr_atribut'][$i];
		}
		
		return substr($result, 1);
		
		/*
		for($i = 0; $i < sizeof($x['rbr_atribut']); $i++){
			$result .= 
				'var obj = document.forma_'.$ime.'.formfield_'.$x['rbr_atribut'][$i].';'."\n".
				'var bs = obj.parentNode.parentNode.getElementsByTagName("B");'."\n".
				'for(var i = 0; i < bs.length; i++){'."\n".
				'if(obj.value == ""){'."\n".
				'bs[i].style.color = "red"; '."\n".
				'bs[i].style.fontWeight = "bold"; '."\n".
				'if(bs[i].innerHTML.indexOf("* ") == -1){'."\n".
				'bs[i].innerHTML = "* " + bs[i].innerHTML + "<img src=\'res/images/b-stop.gif\' border=\'0\' class=\'stopImage\'>"; '."\n".
				'}'."\n".
				'is_false = 1;'."\n".
				'}else{'."\n".
				'bs[i].style.color = "black"; '."\n".
				'bs[i].style.fontWeight = "normal"; '."\n".
				'bs[i].innerHTML = bs[i].innerHTML.replace("* ", ""); '."\n".
				'bs[i].innerHTML = bs[i].innerHTML.replace("<img src=\'res/images/b-stop.gif\' border=\'0\' class=\'stopImage\'>", ""); '."\n".
				'bs[i].innerHTML = bs[i].innerHTML.replace("<img src=\"res/images/b-stop.gif\" border=\"0\" class=\"stopImage\">", ""); '."\n".
				'bs[i].innerHTML = bs[i].innerHTML.replace("<img src=\'res/images/b-stop.gif\' class=\'stopImage\' border=\'0\'>", ""); '."\n".
				'bs[i].innerHTML = bs[i].innerHTML.replace("<img src=\"res/images/b-stop.gif\" class=\"stopImage\" border=\"0\">", ""); '."\n".
				'}'."\n".
				'}'."\n".
				'if(is_false == 1){'."\n".
				'return false;'."\n".
				'}'."\n".
				'return false;'."\n".
				''."\n".
				''."\n".
				''."\n".
				''."\n";
		}
		*/
	}
	
	function gen_form($table, $ime, $pk_rbr = 0){
		global $g_path_sl;
		
		$x = get_data('select meta_forma.rbr, klasa, show_label, label, default_tab '.
			' from meta_forma, meta_tablica_list '.
			' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_tablica_list.ime_tablice = "'.$table.'" '.
			' and meta_forma.ime = "'.$ime.'" ', 0);
			
		
		//print strval($pk_rbr)." -- <br>";
		if(strval($pk_rbr) == '0'){
			$l_access_type = 2;
			$l_msg = 'Nemate ovlasti za dodavanje podataka.';
		}else{
			$l_access_type = 1;
			$l_msg = 'Nemate ovlasti za pregled ovog podatka.';
		}
		
		//print $l_access_type." -- <br>";
		
		// kontrola prikaza indexa modula
		//$z = get_item('select rbr from access '.
			//' where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = 2 and rbr_modul = '.$x['rbr'].' and rbr_access_type = '.$l_access_type.' limit 1 ');
		$z = 0;
		if($z > 0){
			$result = '<div class="accessLimit">'.$l_msg.'</div>';
			return $result;
		}
		
		
		$x['klasa'] == '' ? $l_klasa = '' : $l_klasa = ' class="'.$x['klasa'].'"';
		
		$_GET['editmove'] == 1234 ? $l_css_id = ' id="formcss'.$x['klasa'].'" ' : $l_css_id = '';
		
		$result = '<form name="forma_'.$ime.'" method="post" action="'.$g_path_sl.'default.php"'.$l_klasa.' onsubmit="return checkfields(\'forma_'.$ime.'\');"'.$l_css_id.'>'.
			'<div class="formTop">'."\n";
		$result .= '<input type="hidden" name="checkfieldsvalue" id="checkfieldsvalue" value="'.gen_checkfields($ime).'">'."\n";
		$result .= '<input type="hidden" name="checkfieldsvaluelogin" id="checkfieldsvaluelogin" value="'.gen_checkfieldslogin($ime).'">'."\n";
		$result .= '<input type="hidden" name="formname" value="forma_'.$ime.'">'."\n";
		$result .= '<input type="hidden" name="spremi_i_dodaj" value="">'."\n";
		$result .= '<input type="hidden" name="spremi_i_dodaj_grupa" value="">'."\n";
		$result .= '<input type="hidden" name="spremi_i_dodaj_back_rbr" value="">'."\n";
		if($pk_rbr){
			$result .= '<input type="hidden" name="get" value="'.$pk_rbr.'">'."\n";
		}
		
		$result .= '<div id="dodajDatotekuLayer"><iframe id="upload_frame" '.
			'name="upload_frame" frameborder="0" border="0" scrolling="no" scrollbar="no"></iframe></div>'."\n";
			
		if($x['show_label']){
			$result .= '<h3>'.$x['label'].'</h3>'."\n";
		}
		$result .= gen_tabs($x['rbr'], $x['default_tab'], $pk_rbr, $table);
		
		$result .= '</div>'."\n";
		$result .= gen_subform($x['rbr'], $pk_rbr)."\n";
		
		$result .= gen_subform_tabs_from_table($x['rbr'], $pk_rbr)."\n";
		
		
			/*
		$l_skripta = 
			'function checkfields11(){'."\n".
			'alert("assssaa");var is_false = 0;'."\n".
			gen_js_check($table, $ime)."\n".
			'if(is_false == 1){'."\n".
			'return false;'."\n".
			'}'."\n".
			'}'."\n".
			''."\n".
			''."\n".
			'function sf_sums(tab, line, field, layerId){'."\n".
				'var l_sum = 0;'."\n".
				'var l_obj_str;'."\n".
				'var obj;'."\n".
				'for(var i = 0; i < document.getElementById("subform_tabs").value; i++){'."\n".
					'if(i + 1 != tab){'."\n".
						'continue;'."\n".
					'}'."\n".
					'for(var j = 0; j < document.getElementById("subform_lines_" + i).value; j++){'."\n".
						'if(document.getElementById("delete_row_" + (j + 1) + "_" + (i + 1)).value == "1"){'."\n".
							'continue;'."\n".
						'}'."\n".
						'l_obj_str = "document.forma_'.$ime.'.formfield_" + field + "_" + (j + 1) + "_" + (i + 1);'."\n".
						'obj = eval(l_obj_str);'."\n".
						'if(obj.value != ""){'."\n".
							'l_sum += parseFloat(obj.value);'."\n".
						'}'."\n".
					'}'."\n".
				'}'."\n".
				'document.getElementById(layerId).innerHTML = roundNumber(l_sum, 2) + " Kn";'."\n".
				'document.getElementById("formfield_" + line).value = roundNumber(l_sum, 2);'."\n".
				''."\n".
				''."\n".
			'}'."\n".
			''."\n".
			''."\n".
			'function sf_max(tab, line, field, layerId){'."\n".
				'var l_sum = 0;'."\n".
				'var l_obj_str;'."\n".
				'var obj;'."\n".
				'for(var i = 0; i < document.getElementById("subform_tabs").value; i++){'."\n".
					'if(i + 1 != tab){'."\n".
						'continue;'."\n".
					'}'."\n".
					'for(var j = 0; j < document.getElementById("subform_lines_" + i).value; j++){'."\n".
						'if(document.getElementById("delete_row_" + (j + 1) + "_" + (i + 1)).value == "1"){'."\n".
							'continue;'."\n".
						'}'."\n".
						'l_obj_str = "document.forma_'.$ime.'.formfield_" + field + "_" + (j + 1) + "_" + (i + 1);'."\n".
						'alert(l_obj_str);'."\n".
						'obj = eval(l_obj_str);'."\n".
						'if(obj.value != ""){'."\n".
							'l_sum += parseFloat(obj.value);'."\n".
						'}'."\n".
					'}'."\n".
				'}'."\n".
				'document.getElementById(layerId).innerHTML = roundNumber(l_sum, 2) + " Kn";'."\n".
				'document.getElementById("formfield_" + line).value = roundNumber(l_sum, 2);'."\n".
				''."\n".
				''."\n".
			'}'."\n".
			''."\n".
			''."\n";
			
		$result .= '<script type="text/javascript">'."\n".$l_skripta;
		$result .= '</script>'."\n";
			*/
		
		/*
		global $ga_js_execute;
		$result .= '<script type="text/javascript">'."\n";
		for($i = 0; $i < sizeof($ga_js_execute); $i++){
			$result .= $ga_js_execute[$i].';'."\n";
		}
		$result .= '</script>'."\n";
		*/
		
		$result .= '<div class="submit" id="submitButtonInvisible"><input type="submit" value="Spremi"></div>'."\n";
		
		$result .= '<input type="hidden" name="action_plus" id="action_plus" value="0">'."\n";
		$result .= '<input type="hidden" name="action_id" id="action_id" value="0">'."\n";
		
		$result .= '</form>'."\n";
		
		return $result;
	}
	
	function gen_subform_data($rbr_meta_forma, $pk_rbr, $tab_rbr, $default_value = ''){
		$result = array();
		
		$l_table = get_item('select meta_tablica_list.ime_tablice '.
			' from meta_forma, meta_tablica_list '.
			' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_forma.rbr = '.$rbr_meta_forma);
			
		$l_data = '';
		$l_tab_str = '_'.$tab_rbr;
		
		if($pk_rbr == -1){
			$l_data = '<div class="subFormRedak">'.gen_tabs($rbr_meta_forma, 0, 0, $l_table, 1, 1, zbroji($tab_rbr), $default_value).'</div>'.
				'<div id="subformRedak2'.$l_tab_str.'"></div>';
			
			$result[0] = 1;
			$result[1] = $l_data;
		}else{
			/*
			// ovdje --- treba napraviti da se poziva tablica na osnovu meta_item tablice, meta_form i meta_subforma tablica, a ne uvijek samo document_version tablica
			// EVO GA... napravljeno upravo dolje
			$x = get_data('select guid from document_version where document_guid = "'.$pk_rbr.'" ', 1);
			$x = get_data('select guid from zadatak_aktivnosti where zadatak_guid = "'.$pk_rbr.'" ', 1);
			*/
			
			$y = get_data('select rbr_meta_forma_child, polje_order from meta_subforma where rbr_meta_forma = '.$rbr_meta_forma, 0);
			if($y['rbr_meta_forma_child']){
				$l_table_child = get_item('select meta_tablica_list.ime_tablice '.
					' from meta_forma, meta_tablica_list '.
					' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
					' and meta_forma.rbr = '.$y['rbr_meta_forma_child']);
				if($l_table_child){
					
					if($l_table_child == 'receivings'){
						$l_table_child = 'receiving';
					}
					
					if($l_table_child == 'medjus'){
						$l_table_child = 'medju';
					}
					
					$x = get_data('select id from '.$l_table.' where '.$l_table_child.'_id = "'.$pk_rbr.'" '.$y['polje_order'], 1);
				}else{
					return "";
				}
			}
			
			for($i = 1; $i <= sizeof($x['id']); $i++){
				if($pk_rbr){
					$l_data .= '<input type="hidden" name="get_'.$i.$l_tab_str.'" value="'.$x['id'][$i - 1].'">'."\n";
				}
				$l_data .= '<div class="subFormRedak">'.gen_tabs($rbr_meta_forma, 0, $x['id'][$i - 1], $l_table, $i, 1, zbroji($tab_rbr)).'</div>'.
					'<div id="subformRedak'.zbroji($i).$l_tab_str.'"></div>';
			}
			
			$result[0] = sizeof($x['id']);
			$result[1] = $l_data;
		}
		
		return $result;
	}
	
	function gen_subform_header($rbr_meta_forma){
		$result = '';
		
		global $g_language;
		
		if($g_language > 1){
			$x_lang = get_data('select * '.
				' from meta_item_lang '.
				' where rbr = '.$rbr_meta_forma.
				' order by rbr, rbr_atribut ', 1);
		}
		
		$x = get_data('select meta_item.rbr_atribut, meta_item.label, meta_item.klasa '.
			' from meta_tab, meta_grupa, meta_item '.
			' where meta_tab.rbr = meta_grupa.rbr_meta_tab '.
			' and meta_grupa.rbr = meta_item.rbr_meta_grupa '.
			' and meta_tab.rbr_meta_forma = '.$rbr_meta_forma.
			' and meta_item.aktivan = 1'.
			' order by meta_item.redoslijed, meta_item.rbr_atribut ', 1);
		for($i = 0; $i < sizeof($x['label']); $i++){
			$x['klasa'][$i] == '' ? $l_class = '' : $l_class = ' class="'.$x['klasa'][$i].'label"';
			
			if($g_language > 1 && $x_lang['label'][$x['rbr_atribut'][$i] - 1]){
				$l_label_lang = $x_lang['label'][$x['rbr_atribut'][$i] - 1];
			}else{
				$l_label_lang = $x['label'][$i];
			}
			
			$result .= '<p'.$l_class.'><span class="headerFieldLength" title="'.$l_label_lang.'">'.$l_label_lang.'</span></p>'."\n";
		}
		
		return $result;
	}
	
	
	function gen_subform_tabs_from_table($rbr_meta_forma_child, $pk_rbr = 0){
		
		// ovdje - veci dio funkcije je napravljen za rad samo s tablicama upravina_ocjena i upravina_ocjena_detalji,
		// tako da treba napraviti poopcenje funkcije da radi sa svim mogucim tablicama
		
		
		$x = get_data('select * from meta_subforma_table where rbr_meta_forma_child = '.$rbr_meta_forma_child.' order by redoslijed limit 1 ');
		
		//print_r($x);
		
		$l_size = sizeof($x['rbr_meta_forma']);
		
		if($l_size > 0){
			
			$l_table = get_item('select meta_tablica_list.ime_tablice '.
				' from meta_forma, meta_tablica_list '.
				' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
				' and meta_forma.rbr = '.$x['rbr_meta_forma']);
				
			
			$l_child = get_item('select rbr_meta_forma_child from meta_subforma_table where rbr_meta_forma = '.$x['rbr_meta_forma']);
			if($l_child){
				$l_table_child = get_item('select meta_tablica_list.ime_tablice '.
					' from meta_forma, meta_tablica_list '.
					' where meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
					' and meta_forma.rbr = '.$l_child);
				if($l_table_child){
					
					//$x_guid = get_data('select guid, upravina_ocjena_tabovi_guid as key2 from '.$l_table.' where '.$l_table_child.'_guid = "'.$pk_rbr.'" order by key2 ', 1);
					
					$x_guid = get_data('select upravina_ocjena_detalji.id, upravina_ocjena_tabovi_id as key2, upravina_ocjena_tabovi.Poredak '.
						' from upravina_ocjena_detalji, upravina_ocjena_tabovi '.
						' where upravina_ocjena_detalji.upravina_ocjena_tabovi_id = upravina_ocjena_tabovi.id '.
						' and upravina_ocjena_id = "'.$pk_rbr.'"  '.
						' order by upravina_ocjena_tabovi.Poredak ', 1);
					
					
					$x_all_tabs = get_data('select id from upravina_ocjena_tabovi order by Poredak ', 1);
					
					//print 'select upravina_ocjena_tabovi_guid as key3 from '.$l_table.' order by Poredak <br>';
					
				}else{
					return "";
				}
			}
				
			//print_r($x_guid['key2']);
			
			$l_result_tab = '';
			$l_result_content = '';
			
			
			$l_replace_old_str = '_{db}_:upravina_ocjena_tabovi:id:NazivPolja';
			
			$y = get_data('select '.$x['table_rbr'].' as rbr, '.$x['table_ime'].' as ime, NazivPolja as replaceStr from '.$x['db_table'].' order by '.$x['table_order'], 1);
			
			$l_size_2 = sizeof($y['rbr']);
			
			
			//print_r($x_guid['key2']);
			
			
			$l_cnt = 0;
			$la_akt_tab = array('Akt', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
			for($i = 0; $i < $l_size_2; $i++){
				$l_replace_new_str = $y['replaceStr'][$i];
				
				$l_result_tab .= '<p class="sfTabTableTab'.$la_akt_tab[$i].'" id="sfTabTableTab_'.$i.'">'.
					'<a href="#" onclick="active_table_tab = '.zbroji($i).'; open_close_all_tabs(this, '.$x['rbr_meta_forma'].', '.$i.'); return false;">'.
					$y['ime'][$i].'</a></p>'."\n";
				
				
				// ovdje... treba stavit varijablu da nije samo za "upravina_ocjena_tabovi_guid"
				global $var_value_upravina_ocjena_tabovi_id;
				//$var_value_upravina_ocjena_tabovi_guid = zbroji($i);
				$var_value_upravina_ocjena_tabovi_id = $y['rbr'][$i];
				
				global $var_value_upravina_ocjena_id;
				$var_value_upravina_ocjena_id = '_{get_last_inserted_id()}_';
				// end of: ovdje... treba stavit varijablu da nije samo za "upravina_ocjena_tabovi_guid"
				
				
				
				//print $x_all_tabs['guid'][$i]."<br>\n";
				
				if(is_array($x_guid['key2']) && in_array($x_all_tabs['id'][$i], $x_guid['key2'])){
					$l_pk_rbr = $x_guid['id'][$l_cnt++];
				}else{
					$l_pk_rbr = 0;
				}
				
				$l_tab_html = gen_tabs($x['rbr_meta_forma'], 0, $l_pk_rbr, $l_table, zbroji($i));
				
				
				if($l_pk_rbr){
					$l_result_content .= '<input type="hidden" name="get_'.zbroji($i).'" value="'.$l_pk_rbr.'">'."\n";
				}
				
				$l_result_content .= '<div class="sfTabTable-'.$x['rbr_meta_forma'].'" id="sfTabTable-'.$x['rbr_meta_forma'].'-'.$i.'">'."\n";
				
				$l_result_content .= str_replace($l_replace_old_str, $l_replace_new_str, $l_tab_html);
				
				$l_result_content .= '</div>'."\n";
			}
			
			$result = '<div class="sfTabTable">'."\n";
			$result .= '<div class="sfTabTableWrap" id="allTabs">'."\n";
			$result .= '<div id="allTabsArrows"><a href="#" onclick="move_left_right(1, '.$l_size_2.'); return false;"><img src="res/images/arr-l.jpg"></a>'.
				'<a href="#" onclick="move_left_right(2, '.$l_size_2.'); return false;"><img src="res/images/arr-r.jpg"></a></div>'."\n";
			$result .= $l_result_tab;
			$result .= '</div>'."\n";
			$result .= $l_result_content;
			$result .= '</div>'."\n";
			
			$result .= '<input type="hidden" name="subform_table_tabs" id="subform_table_tabs" value="'.$l_size_2.'">'."\n";
		}else{
			$result = '<input type="hidden" name="subform_table_tabs" id="subform_table_tabs" value="0">'."\n";
		}
		
		
		return $result;
	}
	
	
	function gen_subform($rbr_meta_forma_child, $pk_rbr = 0){
		
		//print 'select rbr_meta_forma, klasa, show_label, label, label_klasa from meta_subforma where rbr_meta_forma_child = '.$rbr_meta_forma_child;
		
		$x = get_data('select * from meta_subforma where rbr_meta_forma_child = '.$rbr_meta_forma_child.' order by redoslijed ', 1);
		
		$l_size = sizeof($x['rbr_meta_forma']);
		
		if($l_size > 0){
			$result = '';
			$l_labels = '';
			$la_akt_tab = array('Akt', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
			for($i = 0; $i < $l_size; $i++){
				$l_tab_str = '_'.$i;
				$x['klasa'][$i] == '' ? $l_class = '' : $l_class = ' class="'.$x['klasa'][$i].'"';
				$x['klasa'][$i] == '' ? $l_id = '' : $l_id = ' id="'.$x['klasa'][$i].'"';
				
				$result .= '<div'.$l_class.$l_id.'>'."\n";
				$x['label_klasa'][$i] == '' ? $l_label_klasa = '' : $l_label_klasa = ' class="'.$x['label_klasa'][$i].' sfTab'.$la_akt_tab[$i].'"';
				
				if($x['show_label'][$i] == 1){
					if($x['label_is_tab'][$i] == 1){
						$l_label = '<p'.$l_label_klasa.'><a href="#" onclick="active_tab = '.zbroji($i).'; open_close_all_labels(this); return false;">'.$x['label'][$i].'</a></p>';
					}else{
						$l_label = '<p'.$l_label_klasa.'>'.$x['label'][$i].'</p>';
					}
				}else{
					$l_label = '';
				}
				
				$l_labels .= $l_label."\n";
				
				//print $pk_rbr."<br>";
				
				if($pk_rbr){
					$la_subform_data = gen_subform_data($x['rbr_meta_forma'][$i], $pk_rbr, $i);
					
					$result .= '<script type="text/javascript">'."\n";
					$result .= 'set_redak('.$la_subform_data[0].', '.$i.')'."\n";
					$result .= '</script>'."\n";
				
				}else{
					//print $x['rbr_meta_forma'][$i]." -- <br>";
						
					// OVDJE Ovdje ovdje: HARDKODIRANO
					if($x['rbr_meta_forma'][$i] == 22 && $i == 2){
						// Zadaci -> Zadatak (default.php?a=1&b=3&c=-1)
						
						//global $var_value_task_guid;
						//$var_value_task_guid = '_{get_last_inserted_id()}_';
						
						$la_subform_data = gen_subform_data($x['rbr_meta_forma'][$i], -1, $i, '1:_{get_last_inserted_id()}_;2:d_current_user');
						
					}else{
						if($x['rbr_meta_forma'][$i] == 93 && $i == 0){
							// Upravljanje resursima -> Putni nalozi (default.php?a=2&b=10&c=-1)
							
							//global $var_value_task_guid;
							//$var_value_task_guid = '_{get_last_inserted_id()}_';
							$la_subform_data = gen_subform_data($x['rbr_meta_forma'][$i], -1, $i, '1:_{get_last_inserted_id()}_');
							
							$result .= '<script type="text/javascript">'."\n";
							$result .= 'set_redak('.$la_subform_data[0].', '.$i.')'."\n";
							$result .= '</script>'."\n";
							
						}else{
							
							$la_subform_data = array(0, '');
						}
					}
					
				}
				
				if($x['rbr_meta_forma'][$i] == 93 && $i == 0){
					$result .= '<p class="clickHere">&nbsp;</p>'."\n";
				}else{
					$result .= '<div class="clickHereWrap"><a href="#" onclick="set_redak('.$la_subform_data[0].', '.$i.'); '.
						' subform_new_row('.$x['rbr_meta_forma'][$i].', 0, 0, \''.$pk_rbr.'\', '.$i.', \'\', \''.$_GET['addition'].'\'); return false;" '.
						'class="clickHere" id="clickHereIDButton">.DODAJ ARTIKL.</a></div>'."\n";
					
					if($_GET['addition'] == 'Inventura'){
						$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
						
						if(strtolower($l_tip_prog) == 'u'){
							$result .= '<a href="#" onclick="unesi_inventuru(); return false;" class="clickHereInv" id="clickHereIDInv">UNESI INVENTURU</a>'."\n";
							$result .= '<div id="dodajDatotekuLayer"><iframe id="upload_frame" '.
								'name="upload_frame" frameborder="0" border="0" scrolling="no" scrollbar="no"></iframe></div>'."\n";
							$result .= '<script type="text/javascript">'."\n";
							$result .= 'setTimeout(function(){if(document.getElementById("subformRedak1_0").innerHTML != ""){document.getElementById("clickHereIDInv").style.display = "none";}}, 500);'."\n";
							$result .= '</script>'."\n";
						}
						
					}
				}
				
				$result .= '<div class="headerField">'."\n";
				$result .= gen_subform_header($x['rbr_meta_forma'][$i])."\n";
				$result .= '</div>'."\n";
				
				//$result .= '<div id="bodyField">'."\n";
				$result .= '<div class="subformNewLine" id="subformNewLineId"><div id="subformRedak1'.$l_tab_str.'">'.$la_subform_data[1].'</div></div>'."\n";
				//$result .= '</div>'."\n";
				$result .= '</div>'."\n";
				$result .= '<input type="hidden" name="subform_lines'.$l_tab_str.'" id="subform_lines'.$l_tab_str.'" '.
					'value="'.$la_subform_data[0].'">'."\n";
				
				$l_ime_sf = get_item('select ime from meta_forma where rbr = '.$x['rbr_meta_forma'][$i]);
				//$result .= '<input type="hidden" name="checkfieldsform_'.$i.'" id="checkfieldsform_'.$i.'" value="forma_'.$l_ime_sf.'">'."\n";
				$result .= '<input type="hidden" name="checkfieldsvalue_'.$i.'" id="checkfieldsvalue_'.$i.'" value="'.gen_checkfields($l_ime_sf).'">'."\n";
			}
			$result .= '<input type="hidden" name="subform_tabs" id="subform_tabs" value="'.$l_size.'">'."\n";
			
			$result = '<div id="allLabels">'.$l_labels.'</div>'.$result;
			
			if($rbr_meta_forma_child == 126 && $_GET['c'] > 0){
				$result .= '<script type="text/javascript">'."\n";
				$result .= 'get_pdv();'."\n";
				$result .= '</script>'."\n";
			}
		}else{
			$result = '<input type="hidden" name="subform_lines" id="subform_lines" value="0">'."\n";
		}
		
							
		return $result;
		
	}
	
	function get_children($id, $sql){
		
		$x = get_data(str_replace('{_REPLACE_VALUE_}', $id, $sql), 1);
		
		//$x = get_data("select guid, Naziv from organizacijska_shema_drustva where parent_guid = '".$id."' ", 1);
		
		$l_parents = "";
		for($i = 0; $i < sizeof($x['id']); $i++){
			$l_parents .= get_children($x['id'][$i], $sql);
		}
		return $l_parents.",'".$id."'";
	}
	
	
	function make_link($ulaz, $rbr){
		global $g_path_sl;
		
		if(1 == 1 || $_SERVER['HTTP_HOST'] == '127.0.0.1'){
			$la_var = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
			if($rbr){
				$la_rbr = explode(':', $rbr);
				
				$l_q = '';
				if(sizeof($la_rbr) > 0){
					$l_q = '?a='.$la_rbr[0];
					
					for($i = 1; $i < sizeof($la_rbr); $i++){
						$l_q .= '&'.$la_var[$i].'='.$la_rbr[$i];
					}
				}
				
				$result = 'default.php'.$l_q;
			}else{
				$result = $g_path_sl.'default.php';
			}
		}else{
			/*
			if($ulaz == ''){
				$ulaz = $g_path_sl.'default.php';
			}
			$result = $ulaz;
			*/
		}
		return $result;
	}
	
	
	function get_tag_value($tag, $xml){
		$l_tag_end = str_replace("<", "</", $tag);
		
		$l_pos_1 = strpos($xml, $tag);
		$l_pos_1_end = strpos($xml, $l_tag_end);
		
		if(!($l_pos_1 === false) && !($l_pos_1_end === false)){
			$result = substr($xml, $l_pos_1, $l_pos_1_end - $l_pos_1 + strlen($l_tag_end));
			$result = str_replace($tag, "", $result);
			$result = str_replace($l_tag_end, "", $result);
		}else{
			$result = "";
		}
		
		return $result;
	}
	
	
	function zbroji($a, $b = 1){
		return $a + $b;
	}
	

	function pe($input){
		p($input);
		exit;
	}
	
	function p($input){
		print_r($input);
		print "<br><br>\n\n";
	}
	
	function get_item($sql, $db_res = "", $test_mode = 0){
		
		if($db_res){
			$rez = mysql_query($sql, $db_res);
		}else{
			$rez = mysql_query($sql);
		}
		//p($sql);
		
		// ova funkcija uvijek mora vratiti samo nula ili jedan redak
		$l_br_redaka = mysql_num_rows($rez);
		switch($l_br_redaka){
			case 0:
				$result = "";
				break;
			case 1:
				$row = mysql_fetch_array ($rez);
				$result = trim($row[0]);
				break;
			default:
				if($test_mode == 1){
					die("Funkcija <i>get_item()</i> vratila je više od jednog retka iako je dozvoljeno vraćanje samo jednog".
						"<br><br> &nbsp; :::=>> <b>".$sql."</b>");
				}
		}
		
		return $result;
	}
	
	
	function get_data($sql, $force_array = 0, $db_res = ""){
		$la_polje = array();
		
		if($db_res){
			$rez = mysql_query($sql, $db_res);
		}else{
			$rez = mysql_query($sql);
		}
		//echo $sql."\n<br><br>";
		//echo mysql_errno() . ": " . mysql_error() . "\n<br><br>";
		

		$l_br_polja = mysql_num_fields($rez);
		for($i = 0; $i < $l_br_polja; $i++){
			array_push($la_polje, mysql_field_name($rez, $i));
		}
		
		$l_br_redaka = mysql_num_rows($rez);
		if($force_array && $l_br_redaka == 1){
			// uvijek vrati array
			$l_br_redaka = -1;
		}
		switch($l_br_redaka){
			case 0:
				$result = array("");
				break;
			case 1:
				$row = mysql_fetch_array ($rez);
				for($i = 0; $i < $l_br_polja; $i++){
					$result[$la_polje[$i]] = $row[$i];
				}
				break;
			default:
				for($i = 0; $i < $l_br_polja; $i++){
					$result[$la_polje[$i]] = array();
				}
				while ($row = mysql_fetch_array ($rez)){
					for($i = 0; $i < $l_br_polja; $i++){
						array_push($result[$la_polje[$i]], $row[$i]);
					}
				}
		}
		return $result;
	}
	
	
	function sql_exe($sql, $last_id = false, $db_res = ""){
		$db_res == "" ? $result = mysql_query($sql) : $result = mysql_query($sql, $db_res);
		if($last_id && $result){
			$result = mysql_insert_id();
		}
		return $result;
	}
	
	
	function lova($ulaz){
		if(intval($ulaz) < 0){
			$negativan = 1;
			$ulaz = abs($ulaz);
		}
		$ulaz = sprintf("%01.2f", $ulaz);
		$decimalni = ",".substr($ulaz, strlen($ulaz) - 2);
		$cijeli = substr($ulaz, 0, strlen($ulaz) - 3);
//		print $cijeli."<br>";
		
		$result = "";
		while(strlen($cijeli) > 0){
//			print $cijeli." ".$l_duzina." x<br>";
			if(strlen($cijeli) - 3 < 0){
				$l_duzina = 0;
			}else{
				$l_duzina = strlen($cijeli) - 3;
			}
			$result = substr($cijeli, $l_duzina).$result;
			$cijeli = substr($cijeli, 0, $l_duzina);
//			print $cijeli." x<br>";
			if(strlen($cijeli) > 0){
				$result = ".".$result;
			}
		}
		
		if($negativan){
			$result = "-".$result.$decimalni;
		}else{
			$result = $result.$decimalni;
		}
		return $result;
	}




	///// pomocne funkcije....
	function generate_meta_prikaz_item($table, $rbr_prikaz, $rbr_redak = -1){
		$x = get_data('describe '.$table);
		$l_max = zbroji(get_item('select max(rbr) from meta_prikaz_item '));
		for($i = 0; $i < sizeof($x['Field']); $i++){
			if($rbr_redak >= 0){
				if($rbr_redak != $i){
					continue;
				}
			}
			
			$l_key_table = $x['Key'][$i];
			if($l_key_table == 'MUL'){
				$y = get_data('show index from '.$table);
				for($j = 0; $j < sizeof($y['Table']); $j++){
					if($y['Column_name'][$j] == $x['Field'][$i]){
						$l_key_table = str_replace('fk_'.$table.'_', '', $y['Key_name'][$j]);
						break;
					}
				}
			}
			
			$la_old = array('char(', 'int(', 'smallint(', ')');
			$la_new = array('char-', 'int-', 'smallint-', '');
			$l_type = str_replace($la_old, $la_new, $x['Type'][$i]);
			
			$sql = "insert into meta_prikaz_item values(".$l_max.", ".$i.", '".$x['Field'][$i]."', '".$l_type."', '', ".
				"'".$l_key_table."', '', '', '".$x['Field'][$i]."', ".$rbr_prikaz.", '', '', '', 0, '', 1000, 0, 0, '', '')";
			print $sql.";<br>";
			sql_exe($sql);
		}
	}
	
	function generate_meta_item($table, $rbr_grupa, $rbr_redak = -1){
		$x = get_data('describe '.$table);
		
		$l_max = zbroji(get_item('select max(rbr) from meta_item '));
		for($i = 0; $i < sizeof($x['Field']); $i++){
			if($rbr_redak >= 0){
				if($rbr_redak != $i){
					continue;
				}
			}
			
			$l_key_table = $x['Key'][$i];
			if($l_key_table == 'MUL'){
				$y = get_data('show index from '.$table);
				for($j = 0; $j < sizeof($y['Table']); $j++){
					if($y['Column_name'][$j] == $x['Field'][$i]){
						$l_key_table = str_replace('fk_'.$table.'_', '', $y['Key_name'][$j]);
						break;
					}
				}
			}
			
			$la_old = array('char(', 'int(', 'smallint(', ')');
			$la_new = array('char-', 'int-', 'smallint-', '');
			$l_type = str_replace($la_old, $la_new, $x['Type'][$i]);
			
			//$sql = "insert into meta_item values(".$l_max.", ".$i.", '".$x['Field'][$i]."', '".$l_type."', '', ".
			//	"'".$l_key_table."', '', '', ".$rbr_grupa.", 'itemWrap', '".$x['Field'][$i]."', 'label', 'item', '', 1, 1, '', 1000, 0, '')";
				
			if($i == 0){
				$sql = "insert into meta_item values(".$l_max.", ".$i.", '".$x['Field'][$i]."', '".$l_type."', '', ".
					"'".$l_key_table."', '', '', ".$rbr_grupa.", 'itemWrap', '".$x['Field'][$i]."', 'label', 'item', '', 0, 0, '', 1000, 0, '')";
			}else{
				$sql = "insert into meta_item values(".$l_max.", ".$i.", '".$x['Field'][$i]."', '".$l_type."', '', ".
					"'".$l_key_table."', '', '', ".$rbr_grupa.", 'f".$rbr_grupa."-".$i."', '".$x['Field'][$i]."', 'label', 'i".$rbr_grupa."-".$i."', '', 1, 1, '', 1000, 0, '')";
			}
			
			sql_exe($sql);
			print $sql.";<br><br>\n\n";
			
		}
	}
	///// end of: pomocne funkcije....


?>