<?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['top-search'];

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

  if(!empty($_GET['search'])) $_POST['search'] = $_GET['search'];
   else $_POST['search'] = $_POST['search'];

  if(!empty($_GET['typepar'])) $_POST['typepar'] = $_GET['typepar'];
   else $_POST['typepar'] = $_POST['typepar'];

  if(empty($_POST['search'])) puterror($TEXT['search-err1']);
  if(strlen($_POST['search'])>200) puterror($TEXT['search-err2']);
  if(strlen($_POST['typepar'])>10) puterror($TEXT['search-err3']);
  if(strlen($_POST['org_id'])>11) puterror($TEXT['error-org']);
  $_POST['search'] = rep($_POST['search']);
  $_POST['typepar'] = rep($_POST['typepar']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_GET['user_id'] = rep($_GET['user_id']);

  $action = "tbl_users.familiya";
    switch($_POST['typepar'])
      {
      case 1:
      $action = "tbl_users.familiya";
      break;
      case 2:
      $action = "tbl_users.name";
      break;
      case 3:
      $action = "tbl_users.room";
      break;
      case 4:
      $action = "tbl_users.phone_int";
      break;
      case 5:
      $action = "tbl_users.mail";
      break;
      }
  ?>
<table class="table" border="0" cellpadding="0" cellspacing="0" width=87%>
<?php
 $n = 0;

if($action == "tbl_users.mail")
 {
  $delimiter = "@";
  $srch_tmp = explode($delimiter, $_POST['search']);
  $_POST['search'] = $srch_tmp['0'];
 }

  $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' AND tbl_users.org_id='".$_POST['org_id']."' AND LOWER(".$action.") LIKE LOWER('%".$_POST['search']."%') 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_org.php?user_id=".$fam['user_id']."&amp;search=".$_POST['search']."&amp;typepar=".$_POST['typepar']."&amp;org_id=".$_POST['org_id'].$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'><br />&nbsp;&nbsp; ".$TEXT['search-err4']."</h1>";
?>
</table>
</div>
<?php
  require_once "utils/bottom_search.php";
?>