<?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-title'];

  require_once "config.php";
  require_once "utils/top_otd.php";
?>
<table class="table" border="0" cellpadding="1" cellspacing="1" width="85%">
<?php
  if(strlen($_GET['org_id'])>20) puterror($TEXT['error-org']);
  if(strlen($_GET['upr_id'])>20) puterror($TEXT['search-err10']);
  $_GET['org_id'] = rep($_GET['org_id']);
  $_GET['upr_id'] = rep($_GET['upr_id']);

$query1 = "SELECT * FROM tbl_organizaciya WHERE org_id = '".$_GET['org_id']."'";
  $organizaciya = mysql_query($query1);
    if(!$organizaciya) puterror($TEXT['error-tbl7']);
       $org = mysql_fetch_array($organizaciya,MYSQL_ASSOC);
           echo "<tr><td><a class='texttableusrtitl2' href=upravlenie.php?org_id=".$_GET['org_id']." title='".$TEXT['menu-btn10']."'>".$org['organizaciya']."</a></td></tr>";

  $query2 = "SELECT * FROM tbl_upravlenie WHERE upr_id = '".$_GET['upr_id']."'";
    $upravlenie = mysql_query($query2);
    if(!$upravlenie) puterror($TEXT['error-tbl9']);
     while($upr = mysql_fetch_array($upravlenie,MYSQL_ASSOC))
      {
       echo "<tr><td class='texttableusrtit'>&nbsp;".$TEXT['top-upr'].$upr['upravlenie']."&nbsp;<a href=upravlenie_podr_prn.php?org_id=".$_GET['org_id']."&amp;upr_id=".$_GET['upr_id']." target=_blank title='".$TEXT['kat-edit-tit']."'><img alt='".$TEXT['tbl-edit']."' src='img/printer.png' height='16' width='16' align='bottom' /></a></td></tr>";

   $query3 = "SELECT * FROM tbl_otdel WHERE otdel = '-'";
     $otdel1 = mysql_query($query3);
     if(!$otdel1) puterror($TEXT['error-tbl8']);
      while($otd1 = mysql_fetch_array($otdel1,MYSQL_ASSOC))
        {
         $user_otd = $otd1['otd_id'];
         $query4 = "SELECT tbl_users.user_id, tbl_users.familiya, tbl_users.name, tbl_users.otchestvo, tbl_users.dol_id, tbl_users.phone_city, tbl_users.phone_int, tbl_dolzhnost.dolzhnost 
         FROM tbl_users 
         JOIN tbl_dolzhnost ON tbl_users.dol_id=tbl_dolzhnost.dol_id 
         WHERE tbl_users.otd_id = '$user_otd' AND tbl_users.org_id = '".$_GET['org_id']."' AND tbl_users.upr_id = '".$_GET['upr_id']."' AND tbl_users.visible = 'show' 
         ORDER BY tbl_users.position, tbl_users.familiya, tbl_users.name, tbl_users.otchestvo ";
         $users = mysql_query($query4);
          if(!$users) puterror($TEXT['error-tbl2']);
           while($user = mysql_fetch_array($users,MYSQL_ASSOC))
             {
              $res_dol = $user['dolzhnost'];
              ?>
              <tr>
                <td class="userpage">&nbsp;&nbsp;&nbsp;&nbsp;<a href=# onClick="window.open('user.php?user_id=<?php echo $user['user_id']; ?>','ee','top=<?php echo $TEXT['user-top']; ?>,left=<?php echo $TEXT['user-left']; ?>,width=<?php echo $TEXT['user-width']; ?>,height=<?php echo $TEXT['user-height']; ?>,status=no,scrollbars=no')"><span class="phoneusr"><?php echo $user['familiya']; ?> <?php echo $user['name']; ?> <?php echo $user['otchestvo']; ?></span></a><span class="phoneotd"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<?php echo $TEXT['search-dol']; ?> <?php echo $res_dol; ?> <?php echo $TEXT['msg-alf5']; ?> <?php echo $user['phone_city']; ?> <?php echo $TEXT['msg-alf6']; ?> <?php echo $user['phone_int']; ?>)</span></td>
              </tr>
              <?php
              }
        }
   $query6 = "SELECT DISTINCT
    tbl_users.otd_id,
    tbl_otdel.position AS otd_pos,
    tbl_otdel.otdel,
    tbl_otdel.phone
    FROM tbl_users
    JOIN tbl_otdel ON tbl_users.otd_id=tbl_otdel.otd_id
    WHERE tbl_users.org_id = '".$_GET['org_id']."' AND tbl_users.upr_id = '".$_GET['upr_id']."'  AND tbl_otdel.visible = 'show' ORDER BY otd_pos, tbl_otdel.otdel ";
    $tmp_otd = mysql_query($query6);
    if(!$tmp_otd) puterror($TEXT['error-tbl2']);
     while($otd_res = mysql_fetch_array($tmp_otd,MYSQL_ASSOC))
       {
       if($otd_res['phone'] !== "") $phone="<span class='phoneotd'>(".$TEXT['search-int-phone']." - ".$otd_res['phone'].")</span>";
       else $phone = "";
         echo "<tr>
               <td class='support4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='support4' href='otdel.php?otd_id=".$otd_res['otd_id']."&amp;org_id=".$_GET['org_id']."&amp;upr_id=".$_GET['upr_id']."'>".$TEXT['top-otd'].$otd_res['otdel']."&nbsp;".$phone."</a></td>
               </tr>";

         $query7 = "SELECT tbl_users.user_id, tbl_users.dol_id, tbl_users.org_id, tbl_users.upr_id, tbl_users.name, tbl_users.familiya,  tbl_users.otchestvo, tbl_users.phone_city, tbl_users.phone_int, tbl_dolzhnost.dolzhnost, tbl_organizaciya.organizaciya,  tbl_upravlenie.upravlenie 
         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 
         WHERE tbl_users.otd_id = '".$otd_res['otd_id']."' AND tbl_users.org_id = '".$_GET['org_id']."' AND tbl_users.upr_id = '".$_GET['upr_id']."' AND tbl_users.visible = 'show' ORDER BY tbl_users.position, tbl_users.familiya, tbl_users.name, tbl_users.otchestvo";
           $users2 = mysql_query($query7);
            if(!$users2) puterror($TEXT['error-tbl2']);
            if (mysql_num_rows($users2)==0) echo "<tr><td class='about'>".$TEXT['error-empty-otd']."</td></tr>";
            while($user2 = mysql_fetch_array($users2,MYSQL_ASSOC))
            {
             ?>
             <tr><td class='userpage'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=# onClick="window.open('user.php?user_id=<?php echo $user2['user_id']; ?>','popup','top=<?php echo $TEXT['user-top']; ?>,left=<?php echo $TEXT['user-left']; ?>,width=<?php echo $TEXT['user-width']; ?>,height=<?php echo $TEXT['user-height']; ?>,status=no,scrollbars=no,location=no,menubar=no,toolbar=no')" title = "<?php echo $TEXT['search-org']; ?> <?php echo $user2['organizaciya']; ?> / <?php echo $TEXT['search-upr']; ?> <?php echo $user2['upravlenie']; ?>"><span class="phoneusr"><?php echo $user2['familiya']; ?> <?php echo $user2['name']; ?> <?php echo $user2['otchestvo']; ?></span></a><span class="phoneotd"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<?php echo $TEXT['search-dol']; ?> <?php echo $user2['dolzhnost']; ?> <?php echo $TEXT['msg-alf5']; ?> <?php echo $user2['phone_city']; ?> <?php echo $TEXT['msg-alf6']; ?> <?php echo $user2['phone_int']; ?>)</span></td>
                   </tr>
             <?php
             }
        }
      }
?>
</table>
</div>
<?php
  require_once "utils/bottom.php";
?>