<?php
  ///////////////////////////////////////////////////
  //2007 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("lang/".$lang.".php")) include "lang/".$lang.".php";
     else include "lang/ru.php";

  $title=$titlepage=$TEXT['global-search-ext-res'];

  require_once "config.php";
  require_once "utils/top_search_ext_res.php";

  if(!empty($_GET['dom_id'])) {
  $_POST['dom_id'] = $_GET['dom_id'];
  $p1 = "&amp;dom_id=".$_GET['dom_id'];
  }
   else {
   $_POST['dom_id'] = $_POST['dom_id'];
   $p1 = "";
   }

  if(!empty($_GET['city_id'])) {
  $_POST['city_id'] = $_GET['city_id'];
  $p2 = "&amp;city_id=".$_GET['city_id'];
  }
   else {
   $_POST['city_id'] = $_POST['city_id'];
   $p2 = "";
   }

  if(!empty($_GET['str_id'])) {
  $_POST['str_id'] = $_GET['str_id'];
  $p3 = "&amp;str_id=".$_GET['str_id'];
  }
   else {
   $_POST['str_id'] = $_POST['str_id'];
   $p3 = "";
   }

  if(!empty($_GET['house_id'])) {
  $_POST['house_id'] = $_GET['house_id'];
  $p4 = "&amp;house_id=".$_GET['house_id'];
  }
   else {
   $_POST['house_id'] = $_POST['house_id'];
   $p4 = "";
   }

  if(!empty($_GET['org_id'])) {
  $_POST['org_id'] = $_GET['org_id'];
  $p5 = "&amp;org_id=".$_GET['org_id'];
  }
   else {
   $_POST['org_id'] = $_POST['org_id'];
   $p5 = "";
   }

  if(!empty($_GET['upr_id'])) {
  $_POST['upr_id'] = $_GET['upr_id'];
  $p6 = "&amp;upr_id=".$_GET['upr_id'];
  }
   else {
   $_POST['upr_id'] = $_POST['upr_id'];
   $p6 = "";
   }

  if(!empty($_GET['otd_id'])) {
  $_POST['otd_id'] = $_GET['otd_id'];
  $p7 = "&amp;otd_id=".$_GET['otd_id'];
  }
   else {
   $_POST['otd_id'] = $_POST['otd_id'];
   $p7 = "";
   }

  if(!empty($_GET['dol_id'])) {
  $_POST['dol_id'] = $_GET['dol_id'];
  $p8 = "&amp;dol_id=".$_GET['dol_id'];
  }
   else {
   $_POST['dol_id'] = $_POST['dol_id'];
   $p8 = "";
   }

  if(!empty($_GET['familiya'])) {
  $_POST['familiya'] = $_GET['familiya'];
  $p9 = "&amp;familiya=".$_GET['familiya'];
  }
   else {
   $_POST['familiya'] = $_POST['familiya'];
   $p9 = "";
   if(!empty($_POST['familiya'])) $p9 = "&amp;familiya=".$_POST['familiya'];
   }

  if(!empty($_GET['nam'])) {
  $_POST['nam'] = $_GET['nam'];
  $p10 = "&amp;nam=".$_GET['nam'];
  }
   else {
   $_POST['nam'] = $_POST['nam'];
   $p10 = "";
   if(!empty($_POST['nam'])) $p10 = "&amp;nam=".$_POST['nam'];
   }

  if(!empty($_GET['otch'])) {
  $_POST['otch'] = $_GET['otch'];
  $p11 = "&amp;otch=".$_GET['otch'];
  }
   else {
   $_POST['otch'] = $_POST['otch'];
   $p11 = "";
   if(!empty($_POST['otch'])) $p11 = "&amp;otch=".$_POST['otch'];
   }

  if(!empty($_GET['room'])) {
  $_POST['room'] = $_GET['room'];
  $p12 = "&amp;room=".$_GET['room'];
  }
  else {
   $_POST['room'] = $_POST['room'];
   $p12 = "";
   if(!empty($_POST['room'])) $p12 = "&amp;room=".$_POST['room'];
   }

  if(!empty($_GET['mobphone'])) {
  $_POST['mobphone'] = $_GET['mobphone'];
  $p13 = "&amp;mobphone=".$_GET['mobphone'];
  }
   else {
   $_POST['mobphone'] = $_POST['mobphone'];
   $p13 = "";
   if(!empty($_POST['mobphone'])) $p13 = "&amp;mobphone=".$_POST['mobphone'];
   }

  if(!empty($_GET['cityphone'])) {
  $_POST['cityphone'] = $_GET['cityphone'];
  $p14 = "&amp;cityphone=".$_GET['cityphone'];
  }
   else {
   $_POST['cityphone'] = $_POST['cityphone'];
   $p14 = "";
   if(!empty($_POST['cityphone'])) $p14 = "&amp;cityphone=".$_POST['cityphone'];
   }

  if(!empty($_GET['intphone'])) {
  $_POST['intphone'] = $_GET['intphone'];
  $p15 = "&amp;intphone=".$_GET['intphone'];
  }
   else {
   $_POST['intphone'] = $_POST['intphone'];
   $p15 = "";
   if(!empty($_POST['intphone'])) $p15 = "&amp;intphone=".$_POST['intphone'];
   }

  if(!empty($_GET['mail'])) {
  $_POST['mail'] = $_GET['mail'];
  $p16 = "&amp;mail=".$_GET['mail'];
  }
   else {
   $_POST['mail'] = $_POST['mail'];
   $p16 = "";
   if(!empty($_POST['mail'])) $p16 = "&amp;mail=".$_POST['mail'];
   }

  if(!empty($_GET['old_city'])) {
  $_POST['old_city'] = $_GET['old_city'];
  $p17 = "&amp;old_city=".$_GET['old_city'];
  }
   else {
   $_POST['old_city'] = $_POST['old_city'];
   $p17 = "";
   if(!empty($_POST['old_city'])) $p17 = "&amp;old_city=".$_POST['old_city'];
   }

  if(!empty($_GET['old_str'])) {
  $_POST['old_str'] = $_GET['old_str'];
  $p18 = "&amp;old_str=".$_GET['old_str'];
  }
   else {
   $_POST['old_str'] = $_POST['old_str'];
   $p18 = "";
   if(!empty($_POST['old_str'])) $p18 = "&amp;old_str=".$_POST['old_str'];
   }

  if(!empty($_GET['old_house'])) {
  $_POST['old_house'] = $_GET['old_house'];
  $p19 = "&amp;old_house=".$_GET['old_house'];
  }
   else {
   $_POST['old_house'] = $_POST['old_house'];
   $p19 = "";
   if(!empty($_POST['old_house'])) $p19 = "&amp;old_house=".$_POST['old_house'];
   }

  if(!empty($_GET['old_org'])) {
  $_POST['old_org'] = $_GET['old_org'];
  $p20 = "&amp;old_org=".$_GET['old_org'];
  }
   else {
   $_POST['old_org'] = $_POST['old_org'];
   $p20 = "";
   if(!empty($_POST['old_org'])) $p20 = "&amp;old_org=".$_POST['old_org'];
   }

  if(!empty($_GET['old_upr'])) {
  $_POST['old_upr'] = $_GET['old_upr'];
  $p21 = "&amp;old_upr=".$_GET['old_upr'];
  }
   else {
   $_POST['old_upr'] = $_POST['old_upr'];
   $p21 = "";
   if(!empty($_POST['old_upr'])) $p21 = "&amp;old_upr=".$_POST['old_upr'];
   }

  if(!empty($_GET['old_otd'])) {
  $_POST['old_otd'] = $_GET['old_otd'];
  $p22 = "&amp;old_otd=".$_GET['old_otd'];
  }
   else {
   $_POST['old_otd'] = $_POST['old_otd'];
   $p22 = "";
   if(!empty($_POST['old_otd'])) $p22 = "&amp;old_otd=".$_POST['old_otd'];
   }

  if(!empty($_GET['old_dol'])) {
 $_POST['old_dol'] = $_GET['old_dol'];
  $p23 = "&amp;old_dol=".$_GET['old_dol'];
  }
   else {
   $_POST['old_dol'] = $_POST['old_dol'];
   $p23 = "";
   if(!empty($_POST['old_dol'])) $p23 = "&amp;old_dol=".$_POST['old_dol'];
   }

  if(!empty($_GET['old_dom'])) {
  $_POST['old_dom'] = $_GET['old_dom'];
  $p24 = "&amp;old_dom=".$_GET['old_dom'];
  }
   else {
   $_POST['old_dom'] = $_POST['old_dom'];
   $p24 = "";
   if(!empty($_POST['old_dom'])) $p24 = "&amp;old_dom=".$_POST['old_dom'];
   }

  if(strlen($_POST['dom_id'])>11) puterror($TEXT['search-err5']);
  if(strlen($_POST['city_id'])>11) puterror($TEXT['search-err6']);
  if(strlen($_POST['str_id'])>11) puterror($TEXT['search-err7']);
  if(strlen($_POST['house_id'])>11) puterror($TEXT['search-err8']);
  if(strlen($_POST['org_id'])>11) puterror($TEXT['search-err9']);
  if(strlen($_POST['upr_id'])>11) puterror($TEXT['search-err10']);
  if(strlen($_POST['otd_id'])>11) puterror($TEXT['search-err11']);
  if(strlen($_POST['dol_id'])>11) puterror($TEXT['search-err12']);
  if(strlen($_POST['familiya'])>200) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['nam'])>200) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['otch'])>200) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['room'])>16) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['mobphone'])>30) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['cityphone'])>30) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['intphone'])>20) puterror($TEXT['error-tbl2']);
  if(strlen($_POST['mail'])>220) puterror($TEXT['error-tbl2']);
  $_POST['dom_id'] = rep($_POST['dom_id']);
  $_POST['city_id'] = rep($_POST['city_id']);
  $_POST['str_id'] = rep($_POST['str_id']);
  $_POST['house_id'] = rep($_POST['house_id']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['upr_id'] = rep($_POST['upr_id']);
  $_POST['otd_id'] = rep($_POST['otd_id']);
  $_POST['dol_id'] = rep($_POST['dol_id']);
  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['nam'] = rep($_POST['nam']);
  $_POST['otch'] = rep($_POST['otch']);
  $_POST['room'] = rep($_POST['room']);
  $_POST['mobphone'] = rep($_POST['mobphone']);
  $_POST['cityphone'] = rep($_POST['cityphone']);
  $_POST['intphone'] = rep($_POST['intphone']);
  $_POST['mail'] = rep($_POST['mail']);

  if(!empty($_POST['mail']))
 {
  $delimiter = "@";
  $srch_tmp = explode($delimiter, $_POST['mail']);
  $_POST['mail'] = $srch_tmp['0'];
 }

  if(!empty($_POST['familiya'])) $familiya = "AND LOWER(tbl_users.familiya) LIKE LOWER('%".$_POST['familiya']."%') ";
    else $familiya = "";

   if(!empty($_POST['nam'])) $name = "AND LOWER(tbl_users.name) LIKE LOWER('%".$_POST['nam']."%') ";
    else $name = "";

   if(!empty($_POST['otch'])) $otchestvo = "AND LOWER(tbl_users.otchestvo) LIKE LOWER('%".$_POST['otch']."%') ";
    else $otchestvo = "";

   if(!empty($_POST['mail'])) $mail = "AND LOWER(tbl_users.mail) LIKE LOWER('%".$_POST['mail']."%') ";
    else $mail = "";

   if(!empty($_POST['room'])) $room = "AND tbl_users.room='".$_POST['room']."' ";
    else $room = "";

   if(!empty($_POST['mobphone'])) $phonem = "AND tbl_users.phone_mob LIKE '%".$_POST['mobphone']."%' ";
    else $phonem = "";

   if(!empty($_POST['cityphone'])) $phonec = "AND tbl_users.phone_city LIKE '%".$_POST['cityphone']."%' ";
    else $phonec = "";

   if(!empty($_POST['intphone'])) $phonei = "AND tbl_users.phone_int='".$_POST['intphone']."' ";
    else $phonei = "";

  if(empty($_POST['old_city'])) $_POST['old_city'] = "";
  if(empty($_POST['old_str'])) $_POST['old_str'] = "";
  if(empty($_POST['old_house'])) $_POST['old_house'] = "";
  if(empty($_POST['old_org'])) $_POST['old_org'] = "";
  if(empty($_POST['old_upr'])) $_POST['old_upr'] = "";
  if(empty($_POST['old_otd'])) $_POST['old_otd'] = "";
  if(empty($_POST['old_dol'])) $_POST['old_dol'] = "";
  if(empty($_POST['old_dom'])) $_POST['old_dom'] = "";

  if($_POST['old_dom'] == "on") {
      $new_dom = "AND tbl_users.dom_id='".$_POST['dom_id']."' ";
      $p1 = "&amp;dom_id=".$_POST['dom_id'];
      }
    else $new_dom = "";

  if($_POST['old_city'] == "on") {
      $new_city = "AND tbl_users.city_id='".$_POST['city_id']."' ";
      $p2 = "&amp;city_id=".$_POST['city_id'];
      }
    else $new_city = "";

  if($_POST['old_str'] == "on") {
      $new_str = "AND tbl_users.str_id='".$_POST['str_id']."' ";
      $p3 = "&amp;str_id=".$_POST['str_id'];
      }
    else $new_str = "";

  if($_POST['old_house'] == "on") {
      $new_house = "AND tbl_users.house_id='".$_POST['house_id']."' ";
      $p4 = "&amp;house_id=".$_POST['house_id'];
      }
    else $new_house = "";

  if($_POST['old_org'] == "on") {
      $new_org = "AND tbl_users.org_id='".$_POST['org_id']."' ";
      $p5 = "&amp;org_id=".$_POST['org_id'];
      }
    else $new_org = "";

  if($_POST['old_upr'] == "on") {
      $new_upr = "AND tbl_users.upr_id='".$_POST['upr_id']."' ";
      $p6 = "&amp;upr_id=".$_POST['upr_id'];
      }
    else $new_upr = "";

  if($_POST['old_otd'] == "on") {
      $new_otd = "AND tbl_users.otd_id='".$_POST['otd_id']."' ";
      $p7 = "&amp;otd_id=".$_POST['otd_id'];
      }
    else $new_otd = "";

  if($_POST['old_dol'] == "on") {
      $new_dol = "AND tbl_users.dol_id='".$_POST['dol_id']."' ";
      $p8 = "&amp;dol_id=".$_POST['dol_id'];
      }
    else $new_dol = "";

 $n = 0;
?>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="87%">
<?php
  $query = "SELECT
  tbl_users.user_id,
  tbl_users.familiya,
  tbl_users.name,
  tbl_users.otchestvo,
  tbl_users.phone_city,
  tbl_users.phone_int,
  tbl_users.mail,
  tbl_users.phone_mob,
  tbl_users.room,
  tbl_users.photo,
  tbl_users.dol_id,
  tbl_users.org_id,
  tbl_users.upr_id,
  tbl_users.otd_id,
  tbl_domain.domain,
  tbl_city.city,
  tbl_street.street,
  tbl_house.house,
  tbl_dolzhnost.dolzhnost,
  tbl_organizaciya.organizaciya,
  tbl_upravlenie.upravlenie,
  tbl_otdel.otdel
  FROM tbl_users
  JOIN tbl_dolzhnost ON tbl_users.dol_id=tbl_dolzhnost.dol_id
  JOIN tbl_organizaciya ON tbl_users.org_id=tbl_organizaciya.org_id
  JOIN tbl_upravlenie ON tbl_users.upr_id=tbl_upravlenie.upr_id
  JOIN tbl_otdel ON tbl_users.otd_id=tbl_otdel.otd_id
  JOIN tbl_domain ON tbl_users.dom_id=tbl_domain.dom_id
  JOIN tbl_city ON tbl_users.city_id=tbl_city.city_id
  JOIN tbl_street ON tbl_users.str_id=tbl_street.str_id
  JOIN tbl_house ON tbl_users.house_id=tbl_house.house_id
  WHERE tbl_users.visible = 'show' ".$familiya." ".$name." ".$otchestvo." ".$phonei." ".$phonec." ".$phonem." ".$room." ".$mail." ".$new_dom." ".$new_city." ".$new_str." ".$new_house." ".$new_org." ".$new_upr." ".$new_otd." ".$new_dol." ORDER BY tbl_users.familiya ";
  $familiya = mysql_query($query);
    if(!$familiya) puterror($TEXT['error-tbl2']);
       $num_rows = mysql_num_rows($familiya);
       while($fam = mysql_fetch_array($familiya,MYSQL_ASSOC))
          {
           $res_dol = $fam['dolzhnost'];
           $res_org = $fam['organizaciya'];
           $res_upr = $fam['upravlenie'];
           $res_otd = $fam['otdel'];

           $n = $n+1;
           $start_anch = 8;
           $stop_anch = $num_rows - $start_anch;
           if($num_rows < $start_anch) {
             $anch_1 = "";
             $anch_2 = "";
             $anch_out = "";
             }
             else
             {
               if($n > $stop_anch) {
               $anch_1 = "<span id=".$fam['user_id'].">";
               $anch_2 = "</span>";
               $anch_out = "#".$fam['user_id']."";
                }
                else {
                $anch_1 = "<span id=".$fam['user_id']."><br /><br /><br /><br /><br />";
                $anch_2 = "</span>";
                $anch_out = "#".$fam['user_id']."";
                }
             }

            if($_GET['user_id'] == $fam['user_id'])
            {
            $dog = "@";
            $room1 = $fam['room'];
            $familiya1 = $fam['familiya'];
            $name1 = $fam['name'];
            $otchestvo1 = $fam['otchestvo'];
            $mail1 = $fam['mail'];
            $phone_mob1 = $fam['phone_mob'];
            $phone_int1 = $fam['phone_int'];
            $phone_city1 = $fam['phone_city'];
            $res_dol1 = $fam['dolzhnost'];
            $res_dom1 = $fam['domain'];
            $email1 = $mail1.$dog.$res_dom1;
            $res_cit1 = $fam['city'];
            $res_str1 = $fam['street'];
            $res_hou1 = $fam['house'];
            $res_org1 = $fam['organizaciya'];
            $res_upr1 = $fam['upravlenie'];
            $res_otd1 = $fam['otdel'];
            $sm = preg_replace("/\.\.+./","",$fam['photo']);
              if(!empty($user['photo']) && file_exists($sm)) $result_image = "<tr valign=top><td width='10%' rowspan='11'><img alt='picture' width='130' height='185' src='".$sm."' /></td></tr>";
                else $result_image = "";

               if(!empty($res_cit1) && $res_cit1!=="-"){$cit = "&nbsp;".$TEXT['user-city']."&nbsp;".$res_cit1.",&nbsp;";}
                else $cit = "";

               if(!empty($res_str1) && $res_str1!=="-"){$str = $TEXT['user-str']."&nbsp;".$res_str1.",&nbsp;";}
                else $str = "";

              if(!empty($res_hou1) && $res_hou1!=="-"){$hou = $TEXT['user-home']." ".$res_hou1;}
                else $hou = "";

              if(!empty($room1) && $room1!=="-"){$rum = ",&nbsp;".$TEXT['user-room']." ".$room1;}
                else $rum = "";

             echo "<tr><td>";
             echo $anch_1;
             echo "<fieldset class='zayavka2' >";
             echo " <legend align='left' class='about'>".$TEXT['global-usr']."</legend>
             <table align='center' border='0' width='100%' cellpadding='0' cellspacing='0'>";
             echo $result_image;
            if(!empty($familiya1)) echo "<tr valign=top><td width='80%' class='texttableusrtit'>$familiya1&nbsp;$name1&nbsp;$otchestvo1</td></tr>";
            if($cit!=="" || $str !== "" || $hou !== "" || $rum !== "")  echo "<tr valign=top><td width='80%' class='about'>".$TEXT['user-adrr']."<span class='texttableusr'>".$cit."".$str."".$hou."".$rum."</span></td></tr>";
            if(!empty($res_org1)) echo "<tr valign=top><td width='80%' class='about'>".$TEXT['search-org']."<span class='texttableusr'>&nbsp;$res_org1</span></td></tr>";
            if(!empty($res_upr1) && $res_upr1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['search-upr']."<span class='texttableusr'>&nbsp;$res_upr1</span></td></tr>";
            if(!empty($res_otd1) && $res_otd1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['search-otd']."<span class='texttableusr'>&nbsp;$res_otd1</span></td></tr>";
            if(!empty($res_dol1) && $res_dol1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['search-dol']."<span class='texttableusr'>&nbsp;$res_dol1</span></td></tr>";
            if(!empty($phone_mob1) && $phone_mob1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['user-tel-mob']."<span class='texttableusr'>&nbsp;$phone_mob1</span></td></tr>";
            if(!empty($phone_city1) && $phone_city1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['user-tel-city']."<span class='texttableusr'>&nbsp;<a href='callto://$phone_city1'>$phone_city1</a></span></td></tr>";
            if(!empty($phone_int1) && $phone_int1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['user-tel-int']."<span class='texttableusr'>&nbsp;<a href='callto://$phone_int1'>$phone_int1</a></span></td></tr>";
            if(!empty($mail1) && $res_dom1!=="-") echo "<tr valign=top><td width='80%' class='about'>".$TEXT['user-e-mail']."<span class='texttableusr'>&nbsp;<a href=mailto:$email1>$email1</a></span></td></tr>";
             echo "</table></fieldset><hr />";
             echo $anch_2."</td></tr>";
            }
            else
             {
             echo "<tr align=left>
             <td width='90%'><a href='search_ext_result.php?user_id=".$fam['user_id']."".$p1.$p2.$p3.$p4.$p5.$p6.$p7.$p8.$p9.$p10.$p11.$p12.$p13.$p14.$p15.$p16.$p17.$p18.$p19.$p20.$p21.$p22.$p23.$p24.$anch_out."' title = '".$TEXT['search-upr']." ".$res_upr." / ".$TEXT['search-otd']." ".$res_otd."'><span class='texttableusrtit'>".$fam['familiya']." ".$fam['name']." ".$fam['otchestvo']."</span><br /><span class='phoneotd'>(".$TEXT['msg-alf3']." ".$res_org." ".$TEXT['msg-alf4']." ".$res_dol." ".$TEXT['msg-alf5']." ".$fam['phone_city']." ".$TEXT['msg-alf6']." ".$fam['phone_int'].")</span><hr /></a></td>
             </tr>";
             }
           }
if($num_rows == 0) echo "<h1 class='userpage'>".$TEXT['search-err4']."</h1>";
?>
</table>
</div>
<?php
  require_once "utils/bottom_search.php";
?>