<?
	header("Content-Type: text/html; charset=utf-8");
	
	$g_path = '../../my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	
	include $g_path."u_headers.php";
	
	$mf = get_data('select * from meta_forma where rbr = '.$_GET['rbr'], 0);
	
	
	$l_css = '';
	$l_css .= '.'.$mf['klasa'].'{position:relative; display:block; width:980px; padding:0; margin:0 }'."\n";
	$l_css .= '.'.$mf['klasa'].' .allLabels{margin:20px 0 0 0 }'."\n";
	$l_css .= '.'.$mf['klasa'].' .formTop{width:960px; height:190px; background:#ffffe3; padding:10px 10px 10px 10px }'."\n";
	$l_css .= '.'.$mf['klasa'].' .formTop .label{color:black; font-size:10pt; width:160px; padding:7px 5px 0 0; font-weight:normal; text-align:left; float:left }'."\n";
	$l_css .= '.'.$mf['klasa'].' .inpWrap{padding:0 0 0 5px; display:block; float:left; position:relative }'."\n";
	$l_css .= '.'.$mf['klasa'].' input{border-style:outset; border-color:#dddddd; border-width:1px; padding:2px; background:#ffffc2 }'."\n";
	$l_css .= '.'.$mf['klasa'].' textarea{border-style:outset; border-color:#dddddd; border-width:1px; padding:2px; background:#ffffc2 }'."\n";
	$l_css .= '.'.$mf['klasa'].' select{border-style:solid; border-color:#dddddd; border-width:0px; padding:2px; background:#ffffc2 }'."\n";
	$l_css .= '.'.$mf['klasa'].'Label{width:960px; margin:5px 0 0 0; padding:0 0 5px 0; font-size:10.5pt; font-weight:bold; color:#886644; border-style:solid; border-color:#E9EFF5; border-width:0 0 1px 0 }'."\n";
	
	if($_GET['a'] == -1 && $_GET['b'] == -1){
		
		$x = get_data('select rbr from meta_tab where rbr_meta_forma = '.$_GET['rbr'], 1);
		for($i = 0; $i < sizeof($x['rbr']); $i++){
			$y = get_data('select rbr from meta_grupa where rbr_meta_tab = '.$x['rbr'][$i], 1);
			for($j = 0; $j < sizeof($y['rbr']); $j++){
				$l_css .= '.'.$mf['klasa'].' .formTop .grupa-'.$x['rbr'][$i].'-'.($j + 1).'{height:300px; position:relative; clear:left }'."\n";
				$z = get_data('select * from meta_item where rbr_meta_grupa = '.$y['rbr'][$j].' order by redoslijed, rbr, rbr_atribut ', 1);
				
				for($k = 0; $k < sizeof($z['rbr']); $k++){
					$l_css .= '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['klasa'][$k]).'{position:absolute; top:'.($k * 30).'px; left:'.($k * 30).'px; width:300px } '."\n";
				}
				
				for($k = 0; $k < sizeof($z['rbr']); $k++){
					$l_css .= '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['item_klasa'][$k]).'{width:120px; height:20px; font-size:10pt } '."\n";
				}
			}
		}
	}else{
		
		//$a = rawurlencode($_GET['a']);
		$a = $_GET['a'];
		//$b = rawurlencode($_GET['b']);
		$b = $_GET['b'];
		
		$la_a = explode('#', $a);
		$la_b = explode('{_#_}', $b);
		
		
		
		$ima_li = intval(get_item('select verzija from meta_css where rbr_meta_forma = '.$_GET['rbr'].' order by rbr desc limit 1 '));
		$l_path = $g_path_res.'css/klasa/css-5'.$_GET['rbr'].'-'.$ima_li.'.css';
		
		$la_line = array();
		
		$fp = @fopen($l_path, "r");
		while(!feof($fp)){
			$buffer = fgets($fp, 4096);
			$la_line []= $buffer;
		}
		fclose($fp);
		

		$x = get_data('select rbr from meta_tab where rbr_meta_forma = '.$_GET['rbr'], 1);
		for($i = 0; $i < sizeof($x['rbr']); $i++){
			$y = get_data('select rbr from meta_grupa where rbr_meta_tab = '.$x['rbr'][$i], 1);
			for($j = 0; $j < sizeof($y['rbr']); $j++){
				$z = get_data('select * from meta_item where rbr_meta_grupa = '.$y['rbr'][$j].' order by redoslijed, rbr, rbr_atribut ', 1);
				
				for($k = 0; $k < sizeof($z['rbr']); $k++){
					for($l = 0; $l < sizeof($la_line); $l++){
						
						if(strstr($la_line[$l], '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['klasa'][$k]).'{')){
							
							//print $la_line[$l];
							//print '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['klasa'][$k]).'{'."\n\n";
							
							for($m = 0; $m < sizeof($la_a) - 1; $m++){
								
								if(strstr($la_line[$l], $la_a[$m].'{')){
									//print $la_a[$m]." xxx \n";
									
									$la_b[$m] = str_replace('background-color: gray;', '', $la_b[$m]);
									$la_b[$m] = str_replace('background-color:gray;', '', $la_b[$m]);
									
									$la_line[$l] = '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['klasa'][$k]).'{'.$la_b[$m].'}'."\n";
								}
							}
						}
					}
				}
				
				for($k = 0; $k < sizeof($z['rbr']); $k++){
					for($l = 0; $l < sizeof($la_line); $l++){
						
						if(strstr($la_line[$l], '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['item_klasa'][$k]).'{')){
							
							//print $la_line[$l];
							//print '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['item_klasa'][$k]).'{'."\n\n";
							
							for($m = 0; $m < sizeof($la_a) - 1; $m++){
								
								if(strstr($la_line[$l], $la_a[$m].'{')){
									//print $la_a[$m]." xxx \n";
									
									$la_b[$m] = str_replace('background-color: gray;', '', $la_b[$m]);
									$la_b[$m] = str_replace('background-color:gray;', '', $la_b[$m]);
									
									$la_line[$l] = '.'.$mf['klasa'].' .'.str_replace($y['rbr'][0], $y['rbr'][$j], $z['item_klasa'][$k]).'{'.$la_b[$m].'}'."\n";
								}
							}
						}
					}
				}
				
			}
		}
		
		$l_css = '';
		for($i = 0; $i < sizeof($la_line); $i++){
			$l_css .= $la_line[$i];
		}
	}
	
	
	// subforme
	$la_subform_line = '';
	$x = get_data('select * from meta_subforma where rbr_meta_forma_child = '.$_GET['rbr'].' order by redoslijed ', 1);
	for($i = 0; $i < sizeof($x['rbr']); $i++){
		if($x['label_is_tab'][$i] == 1){
			$la_subform_line .= '.'.$x['klasa'][$i].'Label{ }'."\n";
			if($i == 0){
				$la_subform_line .= '#'.$x['klasa'][$i].'{display:block}'."\n";
			}else{
				$la_subform_line .= '#'.$x['klasa'][$i].'{display:none}'."\n";
			}
		}else{
			$la_subform_line .= '.'.$x['klasa'][$i].'Label{clear:left; margin:10px 0 0 0; font-size:10pt; font-weight:bold; color:#ffffaa; display:block; padding:5px 10px 5px 10px; text-decoration:none }'."\n";
			$la_subform_line .= '#'.$x['klasa'][$i].'{display:block}'."\n";
		}
		$la_subform_line .= '.'.$x['klasa'][$i].'{clear:left; position:relative; display:block; width:980px; padding:0; margin:0}'."\n";
		$la_subform_line .= '.'.$x['klasa'][$i].' .clickHere{font-weight:normal; width:980px; height:23px; background:#777777; color:white; text-align:center; font-size:11pt; display:block; padding:5px 0 0 0; overflow:hidden; margin:1px 0 1px 0; float:left }'."\n";
		$la_subform_line .= '.'.$x['klasa'][$i].' .headerField{background:#B8A6A0; width:980px; margin:0 0 1px 0 }'."\n";
		$la_subform_line .= '.'.$x['klasa'][$i].' .headerField .headerFieldLength{width:600px; display:block }'."\n";
		$la_subform_line .= '.'.$x['klasa'][$i].' .headerField p{font-weight:normal; color:#444444; float:left; overflow:hidden; padding-left:6px; padding-right:6px; font-size:9pt; margin:9px 0 8px 0 }'."\n";
		
		
		$l_rbr_mt = get_item('select rbr from meta_tab where rbr_meta_forma = '.$x['rbr_meta_forma'][$i]);
		$l_rbr_mg = get_item('select rbr from meta_grupa where rbr_meta_tab = '.$l_rbr_mt);
		$y = get_data('select * from meta_item where rbr_meta_grupa = '.$l_rbr_mg.' and rbr_atribut > 1 order by redoslijed, rbr, rbr_atribut ', 1);
		for($j = 0; $j < sizeof($y['rbr']); $j++){
			$la_subform_line .= '.'.$x['klasa'][$i].' .headerField .f'.$l_rbr_mg.'-'.($j + 2).'label{width:50px; margin-right:3px }'."\n";
		}
		$la_subform_line .= '.'.$x['klasa'][$i].' .subformNewLine{width:980px; color:#444444; font-size:11pt; display:block; padding:0 0 1px 0 }'."\n";
		$la_subform_line .= '.'.$x['klasa'][$i].' .subformNewLine .subFormRedak{padding:0 0 3px 0 }'."\n";
		$la_subform_line .= '.'.$x['klasa'][$i].' .subformNewLine .label{display:none }'."\n";
		for($j = 0; $j < sizeof($y['rbr']); $j++){
			$la_subform_line .= '.'.$x['klasa'][$i].' .subformNewLine .f'.$l_rbr_mg.'-'.($j + 2).'{width:65px }'."\n";
		}
		$la_subform_line .= '.'.$x['klasa'][$i].' .subformNewLine .wButton{width:15px; height:18px; font-size:13pt; color:#666666; background:#ffffc2; float:right; margin:0 0 0 1px; padding:4px 5px 2px 5px; text-align:center; text-decoration:none; font-weight:bold; border-style:solid; border-color:#cccccc; border-width:1px }'."\n";
		for($j = 0; $j < sizeof($y['rbr']); $j++){
			$la_subform_line .= '.'.$x['klasa'][$i].' .subformNewLine .i'.$l_rbr_mg.'-'.($j + 2).'{width:57px; height:20px; margin:3px 0 0 0 }'."\n";
		}
	}
	
	//print $l_css;
	
	//print_r($la_line);
	//print_r($la_subform_line);
	//exit;
	
	if($la_subform_line){
		$l_css .= $la_subform_line;
	}
	
	$ima_li = 1 + intval(get_item('select verzija from meta_css where rbr_meta_forma = '.$_GET['rbr']));
	$l_path = $g_path_res.'css/klasa/css-5'.$_GET['rbr'].'-'.$ima_li.'.css';
	
	$l_ret = -1;
	$fp = @fopen($l_path, "w");
	if(!($fp === false)){
		$l_ret = fwrite($fp, $l_css);
		fclose($fp);
	}
	
	if($ima_li > 1){
		$l_sql = 'update meta_css set verzija = '.$ima_li.', vrijeme = now() where rbr_meta_forma = '.$_GET['rbr'];
		$result = $ima_li;
	}else{
		$l_sql = 'insert into meta_css values(0, '.$_GET['rbr'].', 1, now()) ';
		$result = 1;
	}
	$l_res = sql_exe($l_sql);
	
	print $result;
?>
