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

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

  $title=$titlepage = $TEXT['otd-title4'];
  $helppage = $TEXT['otd-help4'];

  require_once "../config.php";
  require_once "../util/top.php";

  if($result2['user'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(!isset($button)) $button = $TEXT['btn_filter'];
  if(!isset($action)) $action = "index.php";

  if(empty($_POST['data'])) {
      if(empty($_GET['data']))
          {
          $_POST['data'] = "";
          $data_trans = "";
          }
       else
           {
           $_POST['data'] = trim($_GET['data']);
           $data_trans = "&amp;data=".$_POST['data']."";
           }
  }
  else {
      $_POST['data'] = trim($_POST['data']);
      $data_trans = "&amp;data=".$_POST['data']."";
  }

  $data=$_POST['data'];

  if(strlen($_POST['data'])>250) puterror2($TEXT['error-otd3']);
  $_POST['data'] = rep($_POST['data']);

  if(strlen($_GET['k'])>8) puterror2($TEXT['error-paging1']);
  if(empty($_GET['k'])) $_GET['k']=0;
  $stop_r = $_GET['k'];
  if($stop_r == 0) $lim_ogr = "20";
   else $lim_ogr = $stop_r.",20";

  $ktblvis = $stop_r+20;
  $str = $ktblvis/20;
?>
 <img alt="<?php echo $TEXT['otd-add-new']; ?>" src= "../img/insert.png" height="15" align="top" /><a class="boldit" href="addform.php" title="<?php echo $TEXT['otd-add-new-tit']; ?>"><?php echo $TEXT['otd-add-new']; ?></a>
 <br />
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="30%">
<tr><td>
<fieldset class="filterr">
<legend align="left" title="<?php echo $TEXT['log-tit7']; ?>"><?php echo $TEXT['btn_filter']; ?></legend>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="zag2"><td><?php echo $TEXT['tbl-otd']; ?></td><td></td></tr>
<tr>
<td><input size=30 maxlength=125 type=text name=data title='<?php echo $TEXT['log-tit8']; ?>' value='<?php echo $data; ?>'></td>
<td><input class="button" type=submit value='<?php echo $button; ?>' /></td>
</tr>
</table>
</fieldset>
</td></tr>
</table>
</form>
 <span class="tabledata"><?php echo $TEXT['otd-text1']; ?></span>
<table class="table" border="1" cellpadding="5" cellspacing="0" width="70%">
<?php

   if(!empty($_POST['data'])) $data = "AND otdel LIKE '%".$_POST['data']."%' ";
    else $data = "";

     $query = "SELECT * FROM `tbl_otdel` WHERE otd_id!='' ".$data." ORDER BY `position` , `otdel` LIMIT $lim_ogr";
     $result = mysql_query($query);
     if(!$result) puterror2($TEXT['error-tbl8']);
       if(mysql_num_rows($result)>0);
         {
//           $href = "index.php?otd_id=".$result['otd_id'];
           ?>
           <tr class="tableheadercat">
             <td><?php echo $TEXT['tbl-otd']; ?></td>
             <td><?php echo $TEXT['tbl-position']; ?></td>
             <td><?php echo $TEXT['usr-field3']; ?></td>
             <td><?php echo $TEXT['tbl-visible']; ?></td>
             <td colspan=3><?php echo $TEXT['tbl-action']; ?></td>
           </tr>
           <?php
             while($result_otdel = mysql_fetch_array($result,MYSQL_ASSOC))
               {
              if($result_otdel['visible']=='show') $otd_vis="<a href=hide.php?otd_id=".$result_otdel['otd_id']." title='".$TEXT['tbl-hide-tit5']."'><img alt='".$TEXT['tbl-show']."' src='../img/show.png' height='16' width='16' align='top' /></a>";
                else $otd_vis="<a href=show.php?otd_id=".$result_otdel['otd_id']." title='".$TEXT['tbl-show-tit5']."'><img alt='".$TEXT['tbl-hide']."' src='../img/hide.png' height='16' width='16' align='top' /></a>";

              if($result_otdel['phone'] == "" || empty($result_otdel['phone'])) $phone="---";
                else $phone=$result_otdel['phone'];

              $edit = "<a href=editform.php?otd_id=".$result_otdel['otd_id']." title='".$TEXT['tbl-edit-tit-otd']."'>".$TEXT['tbl-edit']."</a>";
              $delete = "<a href=deleteform.php?otd_id=".$result_otdel['otd_id']." title='".$TEXT['tbl-delete-tit-otd']."'>".$TEXT['tbl-delete']."</a>";
              $move_up = "<a href=up-down.php?otd_id=".$result_otdel['otd_id']."&amp;position=".$result_otdel['position']."&amp;moving=up title='".$TEXT['global-up-down-tit']."'><img alt='up' src='../img/up.png' height='16' width='16' align='top' /></a>";
              $move_down = "<a href=up-down.php?otd_id=".$result_otdel['otd_id']."&amp;position=".$result_otdel['position']."&amp;moving=down title='".$TEXT['global-up-down-tit']."'><img alt='down' src='../img/down.png' height='16' width='16' align='top' /></a>";
           ?>
           <tr class="tabledata" onmouseover="this.style.backgroundColor='#cccccc';" onmouseout="this.style.backgroundColor='#ffffff';">
              <td><?php echo $result_otdel['otdel']; ?></td>
              <td><?php echo $result_otdel['position']; ?></td>
              <td><?php echo $phone; ?></td>
              <td><?php echo $otd_vis; ?></td>
              <td><?php echo $move_up; ?> <?php echo $move_down; ?></td>
              <td><?php echo $edit; ?></td>
              <td><?php echo $delete; ?></td>
           </tr>
           <?php
               }
         }
?>
</table>
<?php
       $query_count = mysql_query("SELECT COUNT(otd_id) FROM tbl_otdel WHERE otd_id!='' ".$data." ");
       $coun_rows = mysql_result($query_count, 0);
               $pag = $coun_rows/20;
               if($pag<=1) $pag = ceil($pag);
                else
                {
                 ?>
                  <table align="center" border="0" cellpadding="0" cellspacing="0" width="70%" >
                   <tr align="center">
                    <td>
                <?php
                $pag=ceil($pag);
                $strn = $str-5;
                if($strn<1) $strn=1;
                $strk = $str+5;
                for($i=$strn; $i<=$pag; $i++)
                 {
                  if($i<=$strk)
                  {
                    if($i==$str)
                    {
                      $k = $i*20-20;
                      $kvis=$k+20;
                      $nvis=$k;
                      echo $pg = " <a class='zagolovok' href='index.php?k=".$k."".$data_trans."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>".$i."</a>";
                    }
                    else
                    {
                    $k = $i*20-20;
                    $kvis=$k+20;
                    $nvis=$k;
                    echo $pg = " <a href='index.php?k=".$k."".$data_trans."' title='".$TEXT['zhurn-details2'].$nvis.$TEXT['zhurn-details1'].$kvis."'>[".$i."]</a>";
                    }
                  }
                 }
                 ?>
                    <select name='k' onchange=window.location.href=this.options[this.selectedIndex].value;>
                    <?php
                    for($j=1; $j<=$pag; $j++)
                    {
                     $k = $j*20-20;
                     if($k == $_GET['k']) echo "<option selected value=index.php?k=".$k."".$data_trans.">".$j."</option>";
                       else echo "<option value=index.php?k=".$k."".$data_trans.">".$j."</option>";
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
  require_once "../util/bottom.php";
?>