<?
$err = '';

$razdel_i=0;

for($i=1; $i<count($tree_cat); $i++)
	{
	if($tree_cat[$i])
		{
		$rzd = " AND razdel_".($i-1)."='".$tree_cat[$i]."' ";
		$razdel_i=$i;
		}
	}

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;



if ($_GET)
	{
	if($arr_field)  
		{
		foreach($arr_field as $ar_fi) 
			{
			if($ar_fi['spec']=='st' || $ar_fi['spec']=='sp')
				{
				eval ("\$data['".$ar_fi['field_field']."']=field_func_search_".$ar_fi['field_typ']." ('prov', \$ar_fi, \$_GET[\$ar_fi['field_field']] );");
				if($data[$ar_fi['field_field']][0])
				$err .= " ".$data[$ar_fi['field_field']][0]." ";
				}
			}
		}
	}


if($_GET['fy'])
$err .= " AND small !='' ";

if($_GET['arx'])
$err .= " AND activ = 1 ";


if($_GET['pols'])
	{
	if(intval($_GET['pols'])==3)
	$err .= " AND moder = 1  ";
	elseif(intval($_GET['pols'])==1)
	$err .= " AND pols = 1 ";
	elseif(intval($_GET['pols'])==2)
	$err .= " AND pols = 2 ";
	}

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


if(intval($_GET['id_akk']))
	{
	$id_akk=intval($_GET['id_akk']);
	$err .= " AND id_akk = '".$id_akk."' ";
	}


if(intval($_GET['number_ob']))
	{
	$number_ob=intval($_GET['number_ob']);
	$err .= " AND id = '".$number_ob."' ";
	}


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

	
$query = "SELECT * FROM board 
".$err." 
ORDER BY vip_sort DESC, data DESC";	


$query_count = "SELECT count(id) FROM board ".$err;	

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

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

<table cellpadding="0" cellspacing="0" border=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="arx" <? if($_GET['arx']) echo 'checked'; ?> > архивные
					<input type="checkbox" name="fy" <? if($_GET['fy']) echo 'checked'; ?> > с фото
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<?
					if(!$id_firm)
					{
					?>
					<input type="radio" name="pols" checked value="0"> Все
					<input type="radio" name="pols" value="1" <? if(intval($_GET['pols'])==1) echo 'checked'; ?>> Част.
					<input type="radio" name="pols" value="2" <? if(intval($_GET['pols'])==2)echo 'checked'; ?> > Комп.
					<input type="radio" name="pols" value="3" <? if(intval($_GET['pols'])==3) echo 'checked'; ?>> Модер.
					<?
					}
					?>
					</td>
					<td>
					N <input size="12" type="text" name="number_ob" value="<?=$number_ob?>">
					</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]]))
			{
			?>
			<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" style="padding:inherit ">
		<?
		if($arr_field)
			{
			foreach($arr_field as $ar_fi)
				{
				if(($ar_fi['spec']=='st' || $ar_fi['spec']=='sp') && $ar_fi['field_search'])
					{
					eval("field_func_search_".$ar_fi['field_typ']." ('add', \$ar_fi, \$data[\$ar_fi['field_field']], \$perehod);");
					}
				}
			}
			?>
		</td>
	</tr>
		
		
</table>
</fieldset>
<input type="hidden" name="id_akk" value="<?=$id_akk?>">
</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,
					success: function(html) {
					var v=w+1;
					for(var i=v; i<5; i++)
						{
						$("#cat_"+i).empty();
						}
					//$("#param").empty();
					$("#cat_"+v).html(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,
					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").html(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);
                	}
        		});
			}

    		
</script>