<?
$err = '';
$err2 = '';

$razdel_i=0;

for($i=1; $i<count($tree_cat); $i++)
	{
	if($tree_cat[$i])
		{
		$razdel_i=$i;
		}
	}

if(!$razdel_i)
	{
	$order_g = 'razdel_m';
	}
elseif($razdel_i==1)
	{
	$rzd = " AND id IN (SELECT magazin_r FROM razdel WHERE razdel_m='".$tree_cat[1]."') ";
	$order_v = 'razdel_m';
	$order_g = 'razdel_r';
	}
elseif($razdel_i==2)
	{
	$rzd = " AND id IN (SELECT magazin_r FROM razdel WHERE razdel_r='".$tree_cat[2]."') ";
	$order_v = 'razdel_r';
	}


if($rzd)
$err .= $rzd;

for($i=1; $i<count($tree_reg); $i++)
	{
	if($tree_reg[$i])
	$rgn = " AND region_".($i-1)."='".$tree_reg[$i]."' ";
	}

if($rgn)
	{
	$err .= $rgn;
	$err2 .= $rgn;
	}


if($_GET['text_search'])
	{
	if(search2(urldecode($_GET['text_search']), 'zag'))
		{
		if($_GET['sz'])
		$err .= " AND ".search2(urldecode($_GET['text_search']), 'zag')." ";
		else
		$err .= " AND ".search2(urldecode($_GET['text_search']), 'opis')." ";
		}
	else $_GET['text_search']='';
	}

/*
if($err)
$err=" WHERE ".mb_substr($err, 4, mb_strlen($err)-4); 

if($err2)
$err2=" WHERE ".mb_substr($err2, 4, mb_strlen($err2)-4); 
*/

$err = " WHERE activ !=1 ".$err; 
$err2 = " WHERE activ !=1 ".$err2; 


$query = "SELECT * FROM magazin 
".$err." 
ORDER BY data DESC, id DESC";	

if($order_g)
	{
	if($order_v)
		{
		$query_count = "SELECT COUNT(DISTINCT(magazin_r)) as cou, ".$order_g." FROM razdel WHERE ".$order_v."='".$id_catalog."' AND magazin_r IN (SELECT id FROM magazin ".$err2.")  GROUP BY ".$order_g;	
		$query_count_all = "SELECT COUNT(DISTINCT(magazin_r)) as cou, ".$order_g." FROM razdel WHERE ".$order_v."='".$id_catalog."' AND magazin_r IN (SELECT id FROM magazin ".$err2.")";	
		}
	else
		{
		$query_count = "SELECT COUNT(DISTINCT(magazin_r)) as cou, ".$order_g." FROM razdel WHERE magazin_r IN (SELECT id FROM magazin ".$err2.")  GROUP BY ".$order_g;	
		$query_count_all = "SELECT COUNT(DISTINCT(magazin_r)) as cou, ".$order_g." FROM razdel WHERE magazin_r IN (SELECT id FROM magazin ".$err2.")";	
		}
	$query_group=1;
	}
else
	{
	$query_count_all = "SELECT count(id) FROM magazin ".$err;	
	$query_group=0;
	}

//echo '<br>'.$query.'<br>';
//echo '<br>'.$query_count.'<br>';
?>

<form action="<?=$bp.'searchmagazin.php'?>" method="post" name="form_edit" id="form_edit" >
<fieldset style="background-color:#CFE6FE">

<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td>
			<table cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td width=200 id="text_search"><input style="width: 200px" type="text" name="text_search" value="<?=urldecode($_GET['text_search'])?>"></td>
					<td>&nbsp;
					<input type="checkbox" name="sz" <? if($_GET['sz']) echo 'checked'; ?> > только в заголовках
					</td>
					<td width="50">
					<input type="image" name="submit" border="0" src="<?=$bp.'icon/poisk.png';?>">
					</td>
				</tr>
			</table>
		</td>
	</tr>
	
	<tr><td><table cellpadding="0" cellspacing="0">
	<tr>
		<?
		for ($i=0; $i<4; $i++)
		{
		?>
		<td id="reg_<?=$i?>">
			<?
			if(count($arr_regions[$tree_reg[$i]]) && !$arr_regions[$tree_reg[$i]] [$arr_rayon[0]])
			{
			?>
			<select style="width: 150px" name="region_<?=$i?>" 
				<?
				if(!$i)
					{
					?>
					onChange="search_reg(<?=$i?>, this.value, this.value, 0); return false">
					<?
					}
				else
					{
					?>
					onChange="search_reg(<?=$i?>, this.value, <?=$glob_region?>, <?=$tree_reg[$i]?>); return false">
					<?
					}
				?>
			<option value="0">любое расположение</option>
			<?
			foreach($arr_regions[$tree_reg[$i]] as $index=>$arr_region)
				{
				?>
				<option value="<?=$index?>" <? if($tree_reg[($i+1)]==$index) echo 'selected'; ?> ><?=$arr_region?></option>
				<?
				}
				?>
			</select>
			<input type="hidden" name="name_reg_<?=$i?>" id="name_reg_<?=$i?>" value="<?=$arr_regions_url[$tree_reg[$i]][$tree_reg[($i+1)]]?>">
			<?
			
			}
			?>
		</td>
		<?
		}
		?>
		
		<td  id="reg_4"></td>
		<td ></td>
	</tr>
	</table></td></tr>
	
	
	<tr><td><table cellpadding="0" cellspacing="0" >
	<tr>
		<?
		for ($i=0; $i<4; $i++)
		{
		?>
		<td id="cat_<?=$i?>">
			<?
			if(count($arr_razdel[$tree_cat[$i]]))
			{
			?>
			<select style="width: 150px" name="razdel_<?=$i?>" onChange="search_cat(<?=$i?>, this.value); return false">
			<option value="0">все разделы</option>
			<?
			foreach($arr_razdel[$tree_cat[$i]] as $index=>$raz)
				{
				?>
				<option value="<?=$index?>" <? if($tree_cat[($i+1)]==$index) echo 'selected'; ?> ><?=$raz?></option>
				<?
				}
				?>
			</select>
			<input type="hidden" name="name_cat_<?=$i?>" id="name_cat_<?=$i?>" value="<?=$arr_razdel_url[$tree_cat[$i]][$tree_cat[($i+1)]]?>">
			<?
			}
			?>
		</td>
		<?
		}
		?>
		
		<td  id="cat_4"></td>
		<td ></td>
	</tr>
	</table></td></tr>
		
		
		
	<tr>
		<td id="param" >
		</td>
	</tr>
		
		
</table>
</fieldset>
<input type="hidden" name="id_firm" value="<?=$id_firm?>">
</form>


<script language="javascript">
			function search_cat(w, id_cat)
			{
			$.ajax({
					type: "GET",
					url: "<?=$bp.'blok/'?>search_cat_two.php?w="+w+"&id_cat="+id_cat+"&magaz=1",
					success: function(html) {
					var v=w+1;
					for(var i=v; i<5; i++)
						{
						$("#cat_"+i).empty();
						}
					$("#param").empty();
					$("#cat_"+v).append(html);
                	}
        		});
			}
			
			function search_reg(w, id_reg, glob, m)
			{
			$.ajax({
					type: "GET",
					url: "<?=$bp.'blok/'?>search_reg_two.php?w="+w+"&id_reg="+id_reg+"&glob="+glob+"&m="+m+"&magaz=1",
					success: function(html) {
					var v=w+1;
					for(var i=v; i<5; i++)
						{
						$("#reg_"+i).empty();
						}
					$("#reg_"+v).append(html);
                	}
        		});
			}
			
			/*
			function search_param(w, id_catalog)
			{
			$.ajax({
					type: "GET",
					url: "<?=$bp.'blok/'?>search_param.php?w="+w+"&id_catalog="+id_catalog,
					success: function(html) {
					$("#param").empty();
					$("#param").append(html);
                	}
        		});
			}
			
			
			function send_element_two(car, id, two, field, viv)
			{
			$.ajax({
					type: "GET",
					url: "<?=$bp.'blok/'?>el_two.php?car="+car+"&id="+id+"&two="+two+"&field="+field+"&viv="+viv,
					success: function(html) {
						$("#www"+id).empty();
						$("#www"+id).append(html);
                	}
        		});
			}
			
			*/
    		
			function showValues(a, pn) {
      		var str = $("#form_edit").serialize();
			$.ajax({
					type: "POST",
					url: "<?=$bp.'blok/'?>"+a+"?pn="+pn,
					data: str,
					success: function(html) {
						$("#len_tab").empty();
						$("#len_tab").append(html);
                	}
        		});
				scrollTo("#len_tab");
    		}
			
			function sort_tab(id_catalog, srt, srt2, pn)
			{
			$.ajax({
					type: "GET",
					url: "<?=$bp.'blok/'?>sort_tab_glav.php?id_catalog="+id_catalog+"&srt="+srt+"&srt2="+srt2+"&pn="+pn,
					success: function(html) {
						$("#len_tab").empty();
						$("#len_tab").append(html);
                	}
        		});
			scrollTo ("#len_tab");				
			}
			
			function send_cart(car)
			{
			$.ajax({
					type: "GET",
					url: "<?=$bp.'blok/'?>cart_glav.php?car="+car,
					success: function(html) {
						$("#car"+car).empty();
						$("#car"+car).append(html);
                	}
        		});
			}

			
</script>