<!doctype html public "-//W3C//DTD HTML 4.0//EN">
<?php 
// CORPORATE ADDRESS BOOK v 1.3.0.1 (c) Shamarinov V.V. 2010
include("config.php");
if (isset($_POST['find'])) $find = $_POST['find'];
if (isset($_GET['find'])) $find = $_GET['find'];
if (isset($_GET['findval'])) $findval = $_GET['findval'];
if (isset($_POST['findval'])) $findval = $_POST['findval'];
if (isset($_GET['sort'])) $sort = $_GET['sort'];
if (isset($_POST['sort'])) $sort = $_POST['sort'];
if (isset($_GET['fselect'])) $fselect = $_GET['fselect'];
if (isset($_POST['fselect'])) $fselect = $_POST['fselect'];

//настройки уменьшенных изображений по умолчанию
if (!isset($th_show))     $th_show = 1;
if (!isset($th_img_w))    $th_img_w = 16;
if (!isset($th_img_h))    $th_img_h = 20;
if (!isset($th_img_zoom)) $th_img_zoom = 2;
//настройки подписей столбцов по умолчанию
if (!isset($header_num))  $header_num="№";
if (!isset($header_name)) $header_name="Фамилия Имя Отчество";
if (!isset($header_rank)) $header_rank="Должность";
if (!isset($header_tel))  $header_tel="№ тел.";
if (!isset($header_mail)) $header_mail="E-mail";
if (!isset($header_room)) $header_room="№ ком.";
if (!isset($header_dep))  $header_dep="Отдел";
//для поиска по умолчанию
if (!isset($fheader_all))  $fheader_all="везде";
if (!isset($fheader_name)) $fheader_name="по Ф.И.О.";
if (!isset($fheader_rank)) $fheader_rank="по должности";
if (!isset($fheader_tel))  $fheader_tel= "по телефону";
if (!isset($fheader_mail)) $fheader_mail="по комнате";
if (!isset($fheader_room)) $fheader_room="по отделу";
if (!isset($fheader_dep))  $fheader_dep= "по e-mail";

?>
<html>
<head>
  <title><?php echo $maintitle?></title>
  <meta http-equiv='Content-Type' content='text/html; charset=windows-1251' />
</head>

 <link rel="stylesheet" href="css/rustylex.css" type="text/css" />
 <link rel='stylesheet' href='css/dialog2.css' type='text/css' />

 <!--
 <script type="text/javascript" src="js/prototype.js"></script>
 <script type="text/javascript" src="js/scriptaculous.js?load=effects"></script>
 <script type="text/javascript" src="js/lightbox.js"></script>
 <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />

 <script language="javascript" type="text/javascript" src="./js/actb.js"></script>
 <script language="javascript" type="text/javascript" src="./js/common.js"></script>
 -->

 <script type="text/javascript" src="js/jquery.js"></script>
 <script type='text/javascript' src='js/jquery.bgiframe.min.js'></script>
 <script type='text/javascript' src='js/jquery.ajaxQueue.js'></script>
 <script type='text/javascript' src='js/thickbox-compressed.js'></script>
 <script type='text/javascript' src='js/jquery.autocomplete.js'></script>
<!-- <script type='text/javascript' src='js/localdata.js'></script>  -->

 <!--<link rel="stylesheet" type="text/css" href="css/main.css" />-->
 <link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
 <!--<link rel="stylesheet" type="text/css" href="css/thickbox.css" /> -->

 <script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script>
 <link rel="stylesheet" type="text/css" href="css/jquery.lightbox-0.5.css" media="screen" />



<script type="text/javascript">

$(function() {
  $('a.lightbox').lightBox();
  //$('a[@rel*=lightbox]').lightBox();
  });

$().ready(function() {
  $("#findval").autocomplete(cities, {
    scroll: true,
    selectFirst: false,
    matchContains: true,
    minChars: 1
    });
 $("#findval").result(function(event, data, formatted) {
    $("#sendbtn").click();//alert(data);//$(this).prev().search();
    });

});


function log(event, data, formatted) {
    alert( !data ? "No match!" : "Selected: " + formatted);
  }



$(document).ready(function(){
//      $(".accordion h3:first").addClass("active");
//      $(".accordion p:not(:first)").hide();

      $(".hidebtn").click(function(){
         var id = $(this).attr("id");
         if ($(this).attr("name")=="hidebtn")
          {
          $(".datarow"+id).hide();
          $(this).attr("src","images/show.png");
          $(this).attr("name","showbtn");
          }
         else
          {
          $(".datarow"+id).show();
          $(this).attr("src","images/hide.png");
          $(this).attr("name","hidebtn");
          }
       });

      $("#hideallbtn").click(function(){
          $(".hidebtn").attr("name","hidebtn");
          $(".hidebtn").click();
       });
      $("#showallbtn").click(function(){
          $(".hidebtn").attr("name","showbtn");
          $(".hidebtn").click();
       });
      $("#showphotobtn").click(function(){
          if ($("#showphoto").val()=="0") $("#showphoto").val("1");
          else $("#showphoto").val("0");
          $("#sendbtn").click();
       });

      $(".fs").click(function(){
          //alert(1);
          $(".fs").css("color","#000000");
          var fs = $(this).attr("id");
          fs = fs.substr(2);
          if (fs!="all") $(this).css("color","red");
          $("#fselect").val(fs);
          $("#sendbtn").click();
          /*
          switch (fs)
           {
           case "name": $("#fcaption").html("Поиск по Ф.И.О.:");break;
           case "tel": $("#fcaption").html("Поиск по телефону:");break;
           case "rank": $("#fcaption").html("Поиск по должности:");break;
           case "mail": $("#fcaption").html("Поиск по E-mail:");break;
           case "room": $("#fcaption").html("Поиск по комнате:");break;
           case "dep": $("#fcaption").html("Поиск по отделу:");break;
           default: $("#fcaption").html("Поиск:");break;
           }
          */
         });
   });

</script>



<body>
<center>


<table cellpadding=0 cellspacing=0 >
<tr><th colspan=2 class=thCornerL1 nowrap=nowrap>
 <h4 style="border-bottom: solid 1px #DAE2E8;"><?php echo $maintitle?></h4>
</th></tr>
<tr><td align="center">
<?php 
//include("config.php");
mysql_connect ($dbhost, $username, $password);
mysql_select_db ($maindb);

if (!isset($_POST['showphoto'])) $_POST['showphoto']=$th_show;

if ($findval=="") $find=0;
if (!isset($print))
 {
 $result = mysql_query("SELECT name FROM t1 WHERE perm <> 'hidden' AND perm <> 'hiddent' ORDER BY name;");
 unset($d);
 while ($value = mysql_fetch_array($result))
  $d[]=$value[0];
  //echo "<option value='".$value[0]."'>".$value[0]."</option>\n";
 sort($d);
 mysql_free_result($result);
 $ta = "'".implode("','",$d)."'";

 echo "
        <script>
        var cities = [$ta];
         </script>
         ";
 ?>

 <form action='index.php' method=post autocomplete="off">
 <input type="hidden" name="find" value="1" />
 <table align="center"><tr valign="middle"><td><center>
 <span id="fcaption">Поиск:</span> <input type=text name=findval id=findval value='<?php echo $findval?>' size=60>
 <!-- <input type="hidden" name="fselect" id="fselect" value="<?php  /*if (!isset($fselect))*/ echo "all"; /*else echo $fselect;*/ ?>">-->
 <select name="fselect" id="fselect">
  <option value='all'><?php echo $fheader_all?></option>
  <option <?php  if ($fselect=="name") echo "selected" ?> value='name'><?php echo $fheader_name?></option>
  <option <?php  if ($fselect=="rank") echo "selected" ?> value='rank'><?php echo $fheader_rank?></option>
  <option <?php  if ($fselect=="tel") echo "selected" ?> value='tel'  ><?php echo $fheader_tel ?></option>
  <option <?php  if ($fselect=="room") echo "selected" ?> value='room'><?php echo $fheader_room?></option>
  <option <?php  if ($fselect=="dep") echo "selected" ?> value='dep'  ><?php echo $fheader_dep ?></option>
  <option <?php  if ($fselect=="mail") echo "selected" ?> value='mail'><?php echo $fheader_mail?></option>
 </select>

 <!--<button type="submit">Найти</button>-->
<!-- <input type="submit" value="Найти" id="sendbtn" style="cursor: pointer" />-->
 <button type="submit" id="sendbtn" style="cursor: pointer" >Найти</button>
 <button type="button" title="Очистить форму поиска" onclick="document.location.href='index.php'" style="cursor: pointer"><img src="images/delete.png" width="10" height="11" /></button> &nbsp;
 <button type="button" title="Экспорт в Excel" onclick="document.location.href='xls.php'" style="cursor: pointer"><img src="images/excel.png" width="11" height="11" /></button>
 <?php  if ($ep==1) { ?>
  <button type="button" title="Рассылка по e-mail" onclick="document.location.href='mail.php'" style="cursor: pointer"><img src="images/letter.png"  /></button>
 <?php  } ?>
 <button type="button" title="Свернуть все" id="hideallbtn" style="cursor: pointer"><img src="images/hideall.png" /></button>
 <button type="button" title="Развернуть все" id="showallbtn" style="cursor: pointer"><img src="images/showall.png" /></button>

 <button type="button" title="Скрыть или показать фото" id="showphotobtn" style="cursor: pointer"><img src="images/photo<?php  if ($_POST['showphoto']==1) echo "x"; ?>.png" height="12px" width="10px" /></button>
 <input type="hidden" id="showphoto" name="showphoto" value="<?php echo $_POST['showphoto']?>" />
 <!-- <form action='index.php' method=post> !-->
 <input type="hidden" name="sort" id="sort" value="<?php  if (!isset($sort)) echo "6"; else echo $sort; ?>">
 </center>
 </td></tr></table>
 <!--<br>-->
 <?php 
 }

unset($sia);
for ($i=0;$i<7;$i++)
 if ($sort==$i)
  $sia[$i]="images/rdown.gif";
 else
  $sia[$i]="images/down.gif";
?>
<table class="formTable" width=100% cellspacing="0" cellpadding="3" bgcolor="#000000" style="margin-bottom: 5px;font-family:tahoma;font-size:11px;border: 1px solid #DAE2E8"  align="center" border="0">
 <tr style="background-color:#C1D6FC;">
  <th><span class="fs" id="fsall"><?php echo $header_num?></span></th>
  <th><span class="fs" id="fsname" style="cursor: crosshair"><?php echo $header_name?></span>&nbsp;<img src="<?php echo $sia[1] ?>" height='5px' width='7px' onclick="getElementById('sort').value='1';getElementById('sendbtn').click();" style="cursor:pointer" title="Упорядочить по Ф.И.О."/></th>
  <th><span class="fs" id="fsrank" style="cursor: crosshair"><?php echo $header_rank?></span>&nbsp;<img src="<?php echo $sia[2] ?>" height='5px' width='7px' onclick="getElementById('sort').value='2';getElementById('sendbtn').click();" style="cursor:pointer" title="Упорядочить по должности"/></th>
  <th><span class="fs" id="fstel" style="cursor: crosshair"><?php echo $header_tel?></span>&nbsp;<img src="<?php echo $sia[3] ?>" height='5px' width='7px' onclick="getElementById('sort').value='3';getElementById('sendbtn').click();" style="cursor:pointer" title="Упорядочить по номеру телефона"/></th>
  <th><span class="fs" id="fsmail" style="cursor: crosshair"><?php echo $header_mail?></span>&nbsp;<img src="<?php echo $sia[4] ?>" height='5px' width='7px' onclick="getElementById('sort').value='4';getElementById('sendbtn').click();" style="cursor:pointer" title="Упорядочить по E-mail"/></th>
  <th><span class="fs" id="fsroom" style="cursor: crosshair"><?php echo $header_room?></span>&nbsp;<img src="<?php echo $sia[5] ?>" height='5px' width='7px' onclick="getElementById('sort').value='5';getElementById('sendbtn').click();" style="cursor:pointer" title="Упорядочить по номеру комнаты"/></th>
  <th><span class="fs" id="fsdep" style="cursor: crosshair"><?php echo $header_dep?></span>&nbsp;<img src="<?php echo $sia[6] ?>" height='5px' width='7px' onclick="getElementById('sort').value='6';getElementById('sendbtn').click();" style="cursor:pointer" title="Упорядочить по названию отдела"/></th>
 </tr>

<?php 
//вывод записей
$cols = "id,name,rank,tel,mail,room,dep,perm";
$hname=array("id","name","rank","tel", "mail","room","dep");
if (!isset($sort)) $sort=6;

$findval2 = $findval; // сохраняем первоначальное значение findval

$findval=strtolower(translit($findval));
$findval2 = strtolower(translit(encodestring($findval2)));

$sortname=$hname[$sort];
unset($srt);
$i=0;
$result = mysql_query("SELECT $cols FROM t1 ORDER BY id;");
while($value = mysql_fetch_array($result))
 {
 $ok=0;
 if ($find==1)
  {
  if ($fselect=="all")
   {
   if (
      InStr(strtolower(translit($value['name'])),$findval)==-1 && InStr(strtolower(translit($value['name'])),$findval2)==-1 &&
      InStr(strtolower(translit($value['rank'])),$findval)==-1 && InStr(strtolower(translit($value['name'])),$findval2)==-1 &&
      InStr(strtolower(translit($value['tel'])),$findval)==-1 &&
      InStr(strtolower(translit($value['mail'])),$findval)==-1 &&
      InStr(strtolower(translit($value['room'])),$findval)==-1 &&
      InStr(strtolower(translit($value['dep'])),$findval)==-1 && InStr(strtolower(translit($value['name'])),$findval2)==-1
      ) $ok=0; else $ok=1;
   }
  else
   {
   if (InStr(strtolower(translit($value[$fselect])),$findval)==-1 && InStr(strtolower(translit($value[$fselect])),$findval2)==-1)
   $ok=0; else $ok=1;
   }
  }
 else $ok=1;
 if (substr($value['perm'],0,6)=="hidden") $ok=0;
 if ($ok)
  {
  $srt[$i][0]=$value['id'];
  $srt[$i][1]=$value[$sortname];
  //echo $srt[$i][0]." ".$srt[$i][1]."<br />";
  $i++;
  }
 }
@mysql_free_result($result);

$srtc=$i;
//sort($srt);
//array_multisort_key($srt, 1, $type = SORT_ASC, $cmp_func = 'strcmp');
if ($sort!=6)
 $srt=array_order($srt,1,SORT_ASC);

$trow="trow1";

for ($i=0; $i<$srtc; $i++)
 {
 $id=$srt[$i][0];
 $result=mysql_query("SELECT * FROM t1 WHERE id='$id';");
 $name = mysql_result ($result, 0, 1);
 $rank = mysql_result ($result, 0, 2);
 $tel =  mysql_result ($result, 0, 3);
 $mail = mysql_result ($result, 0, 4);
 $room = mysql_result ($result, 0, 5);
 $dep =  mysql_result ($result, 0, 6);

 $cdate=Trim(mysql_result ($result, 0, $sort*1));
 $c=$i+1;
 if (strtolower(translit(Trim($cdate))) != strtolower(translit(Trim($olddate))) && $sort!=1 && $sort!=4)
  {
  ?>
  <tr class="seprow <?php echo strtolower(translit2en($cdate)); ?>"><td colspan=7><center><i>
  <?php 
  if (isset($print)) echo $cdate;
  else
   {
   ?>
   <table cellpadding="0" cellspacing="0" width="100%"><tr>
    <td width="10%" >&nbsp;</td>
    <td width="80%" ><center>
    <?php 
    echo "<a href='index.php?find=1&fselect=$sortname&findval=$cdate&sort=$sort' style='text-decoration: none; color: #000000'> $cdate </a>&nbsp;";
    if ($ep!=1)
     {
     unset($ma);
     $mr=mysql_query("SELECT DISTINCT mail FROM t1 WHERE $sortname='$cdate' AND mail<>'';");
     while ($mv=mysql_fetch_array($mr)) $ma[]=Trim($mv[0]);
     if (count($ma)>0)
      {
      echo "<img src='images/mail.png' title='Сделать рассылку по E-Mail' width=14px heigth=10px
             onclick='document.location=\"mailto:".implode(",",$ma)."\"' style='cursor:pointer'>";
      }
     }
    else
     {
     echo "<img class='hidebtn' id='".md5($cdate)."' name='hidebtn' src='images/hide.png' style='cursor:pointer'>";
     //$cdate=Trim($cdate);
     $mr=mysql_query("SELECT grpmail FROM mgrp WHERE grpname='$cdate';");
     if ($mr!=NULL)
      {
      @$grpmail = mysql_result($mr,0,0);
      if ($grpmail!=NULL)
       {
       //echo "<img src='images/mail.png' title='mailto:$grpmail' width=14px heigth=10px
       //      onclick='document.location=\"mailto:".$grpmail."\"' style='cursor:pointer'>";
       }
      }
     }

    ?>
    </center></td>
    <td width="10%"><div align="right">
    <?php  echo "<img class='hidebtn' id='".md5($cdate)."' name='hidebtn' src='images/hide.png' style='cursor:pointer'>&nbsp;"; ?>
    </div></td>
   </tr></table>
   <?php 

   }
  ?></i></center></td></tr>
  <?php 
  $olddate = $cdate;
  }
 $translitname = strtolower(translit(Trim($name)));
 //echo $translitname.".jpg";
 $pfn = "photos/".$translitname.".jpg";
 $photo=0;
 if (file_exists($site_dir.$pfn)) $photo=1;
 else $pfn="photos/nophoto.gif";
 $a1="";
 $a2="";
 if ($photo==1 && $print!=1)
  {
  $a1="<a href='$pfn' id='id$c' class='lightbox' title='$name,<br />$rank<br />($dep)' style='text-decoration: none; color: #000000'>";
  $a2="</a>";
  }
 ?>
 <tr class="datarow<?php echo md5($cdate);?>" style='background-color:#<?php  if ($trow=="trow1") echo "FFFFFF"; else echo "EEF6FE";?>;' >
  <td><center> <?php echo $c?> </center></td>
  <td nowrap="nowrap"><center>
              <?php  if ($_POST['showphoto']==1 && $photo==1) {   ?>
              <table cellpadding="0" cellspacing="0" width="100%"><tr>
              <td>
               <img src="<?php echo $pfn?>" width="<?php echo $th_img_w?>" height="<?php echo $th_img_h?>"
                     onclick="document.getElementById('id<?php echo $c?>').click()"
                     onmousemove="this.width=<?php echo $th_img_w*$th_img_zoom?>;this.height=<?php echo $th_img_h*$th_img_zoom?>;"
                     onmouseout="this.width=<?php echo $th_img_w?>;this.height=<?php echo $th_img_h?>;"
                    style="cursor:pointer"
                    />&nbsp;</td>
              <td align="center">
              <?php   }
              echo $a1.$name.$a2;
              if ($_POST['showphoto']==1 && $photo==1) {
              ?>
              </td>
              </tr>
              </table>  <?php  } ?>
              </center></td>
  <td><center><?php echo $rank?></center></td>
  <td><center><?php  if (!isset($print)) { ?><img src="images/phone.png" height="9"  width="12"><?php  } ?> <?php echo $tel?></center></td>
  <td><center><a href='mailto:<?php echo $mail?>' target='_parent'><?php echo $mail?></a></center></td>
  <td><center><a href='index.php?find=1&fselect=room&findval=<?php echo $room?>' style='text-decoration: none; color: #888888'><?php echo $room?></a></center></td>
  <td><center><?php echo $dep?></center></td>
 </tr>
 <?php 
 if ($trow=="trow1") $trow="trow2";
 else $trow="trow1";
 }
@mysql_free_result($result);
mysql_close();


if ($srtc==0) // ничего не найдено
 {
 echo "<tr style='background-color:#F0F0F0;'><td colspan=7><center> По вашему запросу ничего не найдено! </center></td></tr>";
 }


// функция превода текста с английской раскладки в русскую
function encodestring($st)
    {
    // Сначала заменяем "односимвольные" фонемы.
    $st=strtr($st,"f,dult`;pbqrkvyjghcnea[wxio]sm'.z",
                  "абвгдеёжзийклмнопрстуфхцчшщъыьэюя");
    $st=strtr($st,"F,DULT`;PBQRKVYJGHCNEA[WXIO]SM'.Z",
                  "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ");
    // Возвращаем результат.
    return $st;
    }

function translit($st)
    {
    // Сначала заменяем "односимвольные" фонемы.
    $st=strtr($st,"абвгдеёзийклмнопрстуфхъыэ. ",
    "abvgdeeziyklmnoprstufh'ie__");
    $st=strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ ",
    "ABVGDEEZIYKLMNOPRSTUFH'IE_");
    // Затем - "многосимвольные".
    $st=strtr($st,
                    array(
                        "ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
                        "щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
                        "Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
                        "Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
                        "ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye",
                        "№"=>"N"
                        )
             );
    // Возвращаем результат.
    return $st;
    }

function translit2en($st)
    {
    // Сначала заменяем "односимвольные" фонемы.
    $st=strtr($st,"абвгдеёзийклмнопрстуфхъыэ. ",
    "abvgdeeziyklmnoprstufh'ie__");
    $st=strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ ",
    "ABVGDEEZIYKLMNOPRSTUFH'IE_");
    // Затем - "многосимвольные".
    $st=strtr($st,
                    array(
                        "ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh",
                        "щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
                        "Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH",
                        "Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
                        "ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye",
                        "№"=>"N"
                        )
             );
    // Возвращаем результат.
    return $st;
    }

function array_order($array,$key,$type=SORT_ASC)
{
    if(!is_array($array)) return;
    $ASC = ( $type === SORT_ASC );
    $elements = count($array)-1;
    for($i = 0; $i < $elements; $i++) {
        for($k = $elements; $k > $i ; $k--) {
            if($ASC ? $array[$k][$key] < $array[$k-1][$key] : $array[$k][$key] > $array[$k-1][$key]) {
                $buf = $array[$k-1]; $array[$k-1] = $array[$k]; $array[$k] = $buf;
            }
        }
    }
    return $array;
}

function InStr($haystack, $needle)
{
    $pos=strpos($haystack, $needle);
    if ($pos !== false)
    {
        return $pos;
    }
    else
    {
        return -1;
    }
}
?>
</table>

</form>


<!--
<a href=admin.php>&gt;&gt; Администрирование &lt;&lt;</a>
!-->
</td></tr></table>
</center>
</body>
</html>

