<?php
# ID: 17
# Trunk_edit_filter_module ver.1.0
# last modification 26.3.2010
# Formulář pro filtraci a vyhledání trunků
#
?>
<script language="JavaScript">
 function check(theForm){
    if (document.getElementById("chck1").checked==false){
  document.getElementById('valid').disabled= false;
   }
 return true;
 }
 
 function RefreshForm(theForm){
      document.forms.form.submit();
  
  }
</script>


<?php
      include "build/db_connect.inc.php"  ;
      $query="SELECT T.idTRUNK,T.Name,T.Host,P.name
              FROM trunk T
              JOIN provider P ON T.idPROVIDER=P.idPROVIDER
              
              WHERE 
              ";
      if($_POST['TEF_VALIDITY']==1 || (IsSet($_GET['TEF_VALIDITY']) && $_GET['TEF_VALIDITY']==0)  )
      {
          $_SESSION['TEF_TEMP'][TEF_VALIDITY]=0;
      }else{
          $_SESSION['TEF_TEMP'][TEF_VALIDITY]= 1;
      }
      
      if($_SESSION['TEF_TEMP'][TEF_VALIDITY]==1)
      {
         $query=$query."T.validity=1 ";
      }else{
         $query=$query."1=1";
      }
      
      if(IsSet($_POST['TEF_ID']) )
      {
        $_SESSION['TEF_TEMP'][TEF_ID]= $_POST['TEF_ID'];
      }
      if ($_SESSION['TEF_TEMP'][TEF_ID]!=''){
          $query=$query." AND T.idTrunk=\"".$_SESSION['TEF_TEMP'][TEF_ID]."\""   ;
      }
      if(IsSet($_POST['TEF_NAME']) )
      {
        $_SESSION['TEF_TEMP'][TEF_NAME]= $_POST['TEF_NAME'];
      }
      if ($_SESSION['TEF_TEMP'][TEF_NAME]!=''){
          $query=$query." AND T.Name like \"".$_SESSION['TEF_TEMP'][TEF_NAME]."%\""   ;
      }
      if(IsSet($_POST['TEF_HOST']) )
      {
        $_SESSION['TEF_TEMP'][TEF_HOST]= $_POST['TEF_HOST'];
      }
      if ($_SESSION['TEF_TEMP'][TEF_HOST]!=''){
          $query=$query." AND T.HOST like \"".$_SESSION['TEF_TEMP'][TEF_HOST]."%\""   ;
      }
      if(IsSet($_POST['TEF_PROVIDER']) )
      {
        $_SESSION['TEF_TEMP'][TEF_PROVIDER]= $_POST['TEF_PROVIDER'];
      }
      if ($_SESSION['TEF_TEMP'][TEF_PROVIDER]!=''){
          $query=$query." AND P.name like \"".$_SESSION['TEF_TEMP'][TEF_PROVIDER]."%\""   ;
      }

##########################################################################
      #zjisteni poctu stran
        $itemperpage=15;
            $result = mysql_query($query);
        $remainder= (mysql_num_rows($result) % $itemperpage);
        $pages=(mysql_num_rows($result)-$remainder)/$itemperpage ;
        if(IsSet($_GET['PAGE']))
        {
          $query=$query." LIMIT ".($_GET['PAGE']*$itemperpage).",".$itemperpage.""   ;
        
        }
        else
        {
          $query=$query." LIMIT ".(0*$itemperpage).",".$itemperpage.""  ;
        }
      
        $result = mysql_query($query);
        if (!$result) {
            die('Could not query:' . mysql_error());
        }    
      #kod pro vypocet počtu stran
        if ($remainder!=0) $pages++; 
?>

<center>
<div class="forms">
<h3>Výběr trunku</h3>
<fieldset>
  <legend>Seznam trunků</legend>
    <div class="userlist">
    <form name="form" action="admin_page.php" onSubmit="return check(this)" method="post">
        <table align="center">
           <tr>
              <td class="tableculombhead" colspan=4 style="text-align:left">
                  <?php
                    for ($i=0 ;$i!=$pages;$i++) {
                  if ($i==$_POST['PAGE'])
                  {
                      $styl="<span class=\"pagelistselected\">";
                  }else{
                      $styl="<span class=\"pagelist\">";
                  }
                  echo $styl;
                  unset ($styl);
                      
                      echo ("<a href=\"".selfURL()."?TEF_VALIDITY=".$_SESSION['TEF_TEMP'][TEF_VALIDITY]."&PAGE=".$i."\">".($i+1)." </a>");
                      echo "</span>";   
                
            }
                  ?>
                
                </td>
                   <input type="hidden" name="TEF_VALIDITY" id="valid" value="1" >
                <td class="tableculombhead" colspan=1 style="text-align:right">Aktivní <input type="checkbox" name="TEF_VALIDITY" id="chck1" value="0" OnChange="RefreshForm(this)" <?php if ($_SESSION['TEF_TEMP'][TEF_VALIDITY]==1 ) echo "checked"; ?>></td>   
                   
           </tr><tr>
              <td class="tablehead"></td>
              <td class="tablehead">Id</td>
              <td class="tablehead">Název</td>
              <td class="tablehead">Host</td>
              <td class="tablehead">Provider</td>
           </tr><tr>
              <td class="tableculombhead"></td>
              <td class="tableculombhead"><input size="1" type="text" name="TEF_ID" value="<?php echo($_SESSION['TEF_TEMP'][TEF_ID]);?>" OnChange="RefreshForm(this)"></td>
              <td class="tableculombhead"><input size="8" type="text" name="TEF_NAME" value="<?php  echo($_SESSION['TEF_TEMP'][TEF_NAME]);?>" OnChange="RefreshForm(this)"></td>
              <td class="tableculombhead"><input size="8" type="text" name="TEF_HOST" value="<?php echo($_SESSION['TEF_TEMP'][TEF_HOST]);?>" OnChange="RefreshForm(this)"></td>
              <td class="tableculombhead"><input size="8" type="text" name="TEF_PROVIDER" value="<?php echo($_SESSION['TEF_TEMP'][TEF_PROVIDER]);?>" OnChange="RefreshForm(this)"></td>
           </tr>


<?php 
         $a=1;
         while (list($idTRUNK,$name,$host,$provider) = mysql_fetch_row($result)){
              #Stridani barev řádků tabulky
              if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
              #####################################################################
              echo "<tr bgcolor=$barva>";
              echo "<td><a href=\"".selfURL()."?NEXTFORM=23&idTrunk=".$idTRUNK."\"><img src=\"files/pictures/vyber.gif\"></a></td><td>".$idTRUNK."</td><td>".$name."</td><td>".$host."</td><td>".$provider."</td>";
              echo "</tr>";
          }
?>
        </table>
        <input type="hidden" name="TREF_SUBMIT">
      </form>
    </div>
  </fieldset>
</div>