<?php
# ID: 16
# Tarif_Group_edit_detail_module ver.1.0
# last modification 9.3.2010
# Formulář pro úpravu vybrané tarifní skupiny
# 
if (IsSet($_GET['DELETE'])){
  $query="UPDATE spec_tarif_mix SET validity=0
          WHERE idTARIFMIX=".$_GET['idTARIFMIX']."
          AND idNUMBER=".$_GET['idNUMBER']."";
  $result = mysql_query($query);
    if (!$result) {
         die('Záznam nebylo možné odstranit: ' . mysql_error());
    }        

}



if(IsSet($_POST['TGED_ADD'])){
     $query="SELECT 1 FROM spec_tarif_mix 
            WHERE idTARIFMIX=".$_POST['idTARIFMIX']."
            AND idNUMBER=".$_POST['TGE_idNUMBER']."
          ";
     $result = mysql_query($query);
      if (!$result) {
       echo $query;
         die('Záznamy spec_tarif_mix nelze prohledat:' . mysql_error());
        
      }
      if (mysql_num_rows($result)!=0){
          $query="UPDATE spec_tarif_mix SET validity=1,idTARIF=".$_POST['TGE_idTARIF']." WHERE idTARIFMIX=".$_POST['idTARIFMIX']."
                  AND idNUMBER=".$_POST['TGE_idNUMBER']."";
          $result = mysql_query($query);
          if (!$result) {
              die('Záznam nelze obnovit:' . mysql_error());
          }        
      }else{
          $query="INSERT INTO spec_tarif_mix (idTRUNK,idNUMBER,idTARIF,idTARIFMIX)
                   VALUES(".$_POST['TGE_idTRUNK'].",".$_POST['TGE_idNUMBER'].",".$_POST['TGE_idTARIF'].",".$_POST['idTARIFMIX'].")";
          $result = mysql_query($query);
          if (!$result) {
              die('Záznam nelze přidat:' . mysql_error());
          }
     }
}




function TrunkRoletka(){
    require "build/db_connect.inc.php"  ;
    $query="SELECT idTrunk,Name
            FROM trunk
            WHERE validity=1
            ";
    $result = mysql_query($query);
    if (!$result) {
         die('Roletku trunk; nelze načíst:' . mysql_error());
    }
    echo "<select name=\"TGE_idTRUNK\" size=1 OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\" >vyberte...</option>";
    while(list($idTRUNK,$name)=mysql_fetch_row($result)){
        if ($_POST['TGE_idTRUNK']==$idTRUNK){
            echo "<option value=".$idTRUNK." selected>".$name."</option>";
        }else{
            echo "<option value=".$idTRUNK.">".$name."</option>";
        }
    }
}
################################################################################
################################################################################
function PrefixRoletka(){
   if($_POST['TGE_idTRUNK']!=0 && $_POST['idTARIFMIX']!=0){  
    require "build/db_connect.inc.php"  ;
    $query="SELECT DN.idNumber,DN.number
            FROM dest_number DN
            WHERE DN.validity=1
            AND NOT idNUMBER IN(SELECT idNUMBER FROM spec_tarif_mix 
                            WHERE idTARIFMIX=".$_POST['idTARIFMIX']."
                            AND validity=1)
            ";
    $result = mysql_query($query);
        if (!$result) {
            die('Roletku prefixu; nelze načíst:' . mysql_error());
        }
    echo "<select name=\"TGE_idNUMBER\" size=1 OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\">vyberte...</option>";
    while(list($idNUMBER,$name)=mysql_fetch_row($result)){
        if ($_POST['TGE_idNUMBER']==$idNUMBER){                                 
            echo "<option value=".$idNUMBER." selected>".$name."</option>";
        }else{
            echo "<option value=".$idNUMBER.">".$name."</option>";
        }
    }
    echo "</select>";
    }
}
################################################################################
################################################################################
function MapaRoletka(){
   if($_POST['TGE_idNUMBER']!=0 && $_POST['TGE_idTRUNK']!=0 && $_POST['idTARIFMIX']!=0){  
    require "build/db_connect.inc.php"  ;
    $query="SELECT idTarif,name
            FROM tarif
            WHERE validity=1";
    $result = mysql_query($query);
        if (!$result) {
            die('Roletku prefixu; nelze načíst:' . mysql_error());
        }
    echo "<select name=\"TGE_idTARIF\" size=1 OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\"  >vyberte...</option>";
    while(list($idTARIF,$name)=mysql_fetch_row($result) ){
        if ($_POST['TGE_idTARIF']==$idTARIF){                                 
            echo "<option value=".$idTARIF." selected>".$name."</option>";
        }else{
            echo "<option value=".$idTARIF.">".$name."</option>";
        }
    }
    echo "</select>";
    }
}
################################################################################
################################################################################
function OkTlacitko(){
   if($_POST['TGE_idNUMBER']!='' && $_POST['TGE_idTRUNK']!='' && $_POST['idTARIFMIX']!='' && $_POST['TGE_idTARIF']!=''){  
    echo "<input type=\"submit\" id=\"sub\" OnChange=\"Unlock()\" name=\"TGED_ADD\" value=\"Přidat\">";
    }
}

if (IsSet($_POST['TGED_SUBMIT'])){      
    require "build/db_connect.inc.php"  ;
    $query="START TRANSACTION";
    $result = mysql_query($query);
    $query="UPDATE tarif_mix SET Name=\"".$_POST['TGED_NAME']."\",start_date=\"".$_POST['TGED_STARTDATE']."\",description=\"".$_POST['TGED_DESCRIPTION']."\",";
             
    if (IsSet($_POST['TGED_VALIDITY'])){
      $query.=" validity=1 ";
    }else{
      $query.=" validity=0 ";
    }
    $query.=" WHERE idTARIFMIX=".$_POST['idTARIFMIX']." ";
    $result = mysql_query($query);
    if (!$result) {
        die('Nelze uložit obecné parametry:' . mysql_error());
    }
    $query="UPDATE spec_pay_type SET idPAYTYPE=".$_POST['TGED_PAY']." WHERE idTARIFMIX=".$_POST['idTARIFMIX'].""; 
    $result = mysql_query($query);
    if (!$result) {
        die('Způsob platby nelze změnit:' . mysql_error());
    }
    $query="COMMIT";
    $result = mysql_query($query);
    echo "Změny byly uloženy.<a href=\"admin_page.php?NEXTFORM=16&idTARIFMIX=".$_POST['idTARIFMIX']."\">Zpět</a>";
  }else{ 
   
################################################################################
  require "build/db_connect.inc.php"  ;
    if (IsSet($_GET['idTARIFMIX'])){$idtarifmix=$_GET['idTARIFMIX'];}else{$idtarifmix=$_POST['idTARIFMIX'];}
  $query="SELECT TM.idTARIFMIX,TM.Name,TM.start_date,TM.validity,SPT.idPAYTYPE,TM.description
          FROM tarif_mix TM
          JOIN spec_pay_type SPT ON SPT.idTARIFMIX=TM.idTARIFMIX AND SPT.validity=1
          WHERE TM.idTARIFMIX=".$idtarifmix."";
  $result = mysql_query($query);
  if (!$result) {
        die('1Obecné info nelze načíst:' . mysql_error());
  }
  list($idtarifmix,$name,$start_date,$validity,$paytype,$description)=mysql_fetch_row($result);



?>
<script language="JavaScript">
// soubor funkcí pro kontrolu hodnot formuláře na straně klienta
  function HideIt() {
        document.getElementById('required').style.visibility= "hidden";
        document.getElementById('requiredtext').style.visibility= "hidden";
  }
  function CheckEmptyField( theField, theMessage ) {
   if( theField.value=="" ) {
      theField.focus();
      if(document.getElementById) {
        document.getElementById('required').style.visibility= "visible";
        document.getElementById('requiredtext').style.visibility= "visible";
        document.getElementById('requiredtext').innerHTML= theMessage;
        setTimeout( "HideIt()",1500);
      } else {
          alert(theMessage);
      }
      return false;
   } else {
      return true;
   }
  }
  function Validate(theForm) {
      if( !CheckEmptyField(theForm.TGED_NAME,"Jméno musí být vyplněno!") ) return false;
      
  // uspech
  return true;
}
  function Validate1(theForm) {
      if( !CheckEmptyField(theForm.TGE_idNUMBER,"Prefix musí být vybrán!") ) return false;
      
  // uspech
  return true;
}
 var MaxLengthLock = false;
      function MaxLengthCount(fieldObj,fieldMaxLength)
      {
        if (!MaxLengthLock)
        {
          MaxLengthLock = true;
          if (fieldObj.value.length > fieldMaxLength)
          {
            alert("Text je delší než " + fieldMaxLength + " znaků!");
            fieldObj.value = fieldObj.value.substring(0,fieldMaxLength);
          }
          var percentage = parseInt(100 - (( fieldMaxLength - fieldObj.value.length) * 100)/fieldMaxLength);
          document.getElementById(fieldObj.id + "_PBar").style.width = parseInt((parseInt(fieldObj.offsetWidth)*percentage)/100)+"px";
          MaxLengthLock = false;
        }
      }
function RefreshForm(theForm){
  //    document.form1.TGE_SUBMIT.value="FALSE";
      document.forms.form1.submit();
      }

</script> 
<div class="forms">
<h3>Editace tarifní skupiny</h3><span class="backtoform"><a href="admin_page.php?NEXTFORM=10">Zpět</a> </span>
    </fieldset>
   <fieldset>
    <legend>Obecné údaje</legend>
    <form action="admin_page.php" method="post" onSubmit="return Validate(this);">
    <table width="500px">
      <tr>
        <td class="tableculombhead" style="text-align:right" colspan=3>Aktivní:  <input type="checkbox" name="TGED_VALIDITY" <?php if($validity==1){echo "checked";} ?>  value="1"></td>
      </tr><tr>
        <td class="tablehead">ID</td>
        <td class="tablehead">Název</td>
        <td class="tablehead">Platnost od</td>
      </tr><tr>
        <td class="tableculombhead"><input type="text" size=1 name="_idTARIFMIX" value="<?php echo $idtarifmix; ?>" disabled ></td>
        <input type="hidden" name="idTARIFMIX" value="<?php echo $_GET['idTARIFMIX']; ?>">
        <td class="tableculombhead"><input type="text" name="TGED_NAME" value="<?php echo $name;  ?>"> </td> 
        <td class="tableculombhead"><input id="f_rangeStart" type="text" name="TGED_STARTDATE" value="<?php echo $start_date;  ?>">
                   <button type="button" id="f_rangeStart_trigger"  >Kalendář</button>
                             <script type="text/javascript">
                                        new Calendar.setup({
                                                showTime : 24,
                                                inputField: "f_rangeStart",
                                                dateFormat: "%Y-%m-%d %H:%M:%S",
                                                trigger: "f_rangeStart_trigger",
                                                bottomBar: false,
                                                onSelect: function() {
                                                document.getElementById("f_rangeStart").disabled=false;
                                                    this.hide();
                                                }
                                        });
                  
                                      </script>
        
        
         </td> 
      </tr><tr>        
        <td class="tablehead">Způsob platby :</td><td class="tableculombhead" colspan=2>Fakturou<input type="radio" name="TGED_PAY" value="1" <?php if($paytype==1) echo "checked"  ?>>Předplacený<input type="radio" name="TGED_PAY" value="2" <?php if($paytype==2) echo "checked"  ?>></td>
      </tr><tr>
        <td class="tablehead" colspan=3>Popis: </td>
      </tr><tr>
         <td colspan=3 class="tableculombhead"><textarea cols="60" id="TGED_DESCRIPTION" name="TGED_DESCRIPTION"  style="max-width:500px" wrap="virtual
            onpropertychange="MaxLengthCount(this,160);"
            onclick="MaxLengthCount(this,160);"
            onmousemove="MaxLengthCount(this,160);"
            onblur="MaxLengthCount(this,160);"
            onkeyup="MaxLengthCount(this,160);"
            onfocus="MaxLengthCount(this,160);"
            onchange="MaxLengthCount(this,160);"
            onafterupdate="MaxLengthCount(this,160);"><?php echo $description; ?></textarea>   
             <div id="TGED_DESCRIPTION_PBar" style="width:0px;height:10px;border:0;font-size:8px;overflow:hidden;background-color: #3399FF;"></div>
         <td>
      </tr><tr>
          <input type="hidden" name="TGED_SUBMIT">
          <td class="tablehead" colspan=3><input type="reset" value="Zrušit změny"><input type="submit" value="Ulož"></td>
      </tr>
    </table>
  </form>      
  

  </fieldset>
  <fieldset>
    <legend>Položky skupiny</legend>
    
    <div class="userlist">
    <form name="form1" action="admin_page.php" OnSubmit="return Validate1(this);" method="post">
    <table align="center" width="500px">
        <tr><td colspan=3 class="tableculombhead" colspan=3></td>
            <td colspan=3 class="tableculombhead" colspan=2>Aktivní </td>
        </tr><tr>
            <td class="tablehead"></td>
            <td class="tablehead">Trunk</td>
            <td class="tablehead">Prefix</td>
            <td class="tablehead" colspan=2>Cenová mapa</td>
            <td class="tablehead"></td>
        </tr><tr>
            <td class="tablehead">Nová položka:</td>
            <td class="tableculombhead"> <?php TrunkRoletka();?> </td>
            <td class="tableculombhead"> <?php PrefixRoletka();?> </td>
            <td class="tableculombhead" colspan=2> <?php MapaRoletka();?> </td>
            <td class="tableculombhead"> <?php OkTlacitko();?> </td>
            <input type="hidden" name="idTARIFMIX" value="<?php if(IsSet($_GET['idTARIFMIX'])){echo $_GET['idTARIFMIX'];}else{echo $_POST['idTARIFMIX'];} ?>">
        </tr>
            <?php
             $query="SELECT T.idTrunk,T.Name,DN.idNUMBER,DN.number,TAR.idTARIF,TAR.name
                      FROM spec_tarif_mix  STM
                      JOIN trunk T ON T.idTrunk=STM.idTrunk AND T.validity=1
                      JOIN dest_number DN ON DN.idNUMBER=STM.idNUMBER AND DN.validity=1
                      JOIN tarif TAR ON TAR.idTARIF=STM.idTARIF AND TAR.validity=1
                      WHERE STM.idTARIFMIX=".$idtarifmix."
                      AND STM.validity=1";
              $result = mysql_query($query);
            if (!$result) {
                die('1Obecné info nelze načíst:' . mysql_error());
            }
            $a=1;
            while(list($idtrunk,$tname,$idnumber,$number,$idtarif,$tarname)=mysql_fetch_row($result)){
                if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
                echo "<tr bgcolor=$barva>";
                echo "<td><a href=\"".selfURL()."?NEXTFORM=16&DELETE=1&idTARIFMIX=".$idtarifmix."&idNUMBER=".$idnumber."\"><img src=\"files/pictures/no1.gif\"></a></td><td>".$tname."</td><td>".$number."</td><td colspan=2>".$tarname."</td><td></td>";
                echo "</tr>";
            
            
            }
            
            ?>
    </table>
    </form>
    </div>
    
</div>


<script language="JavaScript"><!-------------
// Tuto funkci je mozno vlozit na konec hlavního dokumentu a ušetři její opakování
if(document.getElementById) {
  document.write('<img ID="required" STYLE="visibility:hidden; position:absolute; left:300; top:120" src="required.gif">');
  document.write('<p ID="requiredtext" STYLE="visibility:hidden; position:absolute; left:365; top:155" >abc</p>');
}

//-----------></script>

<?php }?>