<?php
# ID: 15
# Tarif_group_create_module ver.1.0
# last modification 7.3.2010
# Formulář pro vytvážení nových tarifů
# POST
# 

if (!IsSet($_SESSION['buffer_item'])){
      $_SESSION['buffer_item']=array();
    }



if(IsSet($_POST['TGED_ADD']) && !in_array(array($_POST['TGE_idTRUNK'],$_POST['TGE_idNUMBER'],$_POST['TGE_idTARIF']),$_SESSION['buffer_item'])){
#Uložení vybrané hodnoty do bufferu, neni li už v bufferu obsažena                
    $_SESSION['buffer_item'][]=array($_POST['TGE_idTRUNK'],$_POST['TGE_idNUMBER'],$_POST['TGE_idTARIF'])   ;
    
}
/*
for ($i=0;$i<count($_SESSION['buffer_item']);$i++){
   echo  $_SESSION['buffer_item'][$i][0]."-".$_SESSION['buffer_item'][$i][1]."-".$_SESSION['buffer_item'][$i][2].",";
}
*/

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 id=\"trrol\" 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>";
        }
    }
    echo "</select>";
}
################################################################################
################################################################################
function PrefixRoletka(){
   if($_POST['TGE_idTRUNK']!=''){  
    require "build/db_connect.inc.php"  ;
    $query="SELECT DN.idNumber,DN.number
            FROM dest_number DN
            WHERE DN.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)){
        $dupl=0;
        for ($i=0;$i<count($_SESSION['buffer_item']);$i++){
          if ($_SESSION['buffer_item'][$i][0]==$_POST['TGE_idTRUNK'] && $_SESSION['buffer_item'][$i][1]==$idNUMBER){
                $dupl=1;
             }
        }
        if ($_POST['TGE_idNUMBER']==$idNUMBER && $dupl==0){                                 
            echo "<option value=".$idNUMBER." selected>".$name."</option>";
        }else if($dupl==0){
            echo "<option value=".$idNUMBER.">".$name."</option>";
        }
        
    }
    echo "</select>";
    }
}
################################################################################
################################################################################
function MapaRoletka(){
   if($_POST['TGE_idNUMBER']!='' && $_POST['TGE_idTRUNK']!='' ){  
    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['TGE_idTARIF']!=''){ 
    echo "</form>";
    echo "<form action=\"admin_page.php?\" method=\"post\">";
    echo "<input type=\"hidden\" name=\"TGE_idTRUNK\" value=\"".$_POST['TGE_idTRUNK']."\">";
    echo "<input type=\"hidden\" name=\"TGE_idNUMBER\" value=\"".$_POST['TGE_idNUMBER']."\">";
    echo "<input type=\"hidden\" name=\"TGE_idTARIF\" value=\"".$_POST['TGE_idTARIF']."\">";
    echo "<input type=\"hidden\" name=\"TGED_NAME\" value=\"".$_POST['TGED_NAME']."\">";
    echo "<input type=\"hidden\" name=\"TGED_STARTDATE\" value=\"".$_POST['TGED_STARTDATE']."\">";
    echo "<input type=\"hidden\" name=\"TGED_DESCRIPTION\" value=\"".$_POST['TGED_DESCRIPTION']."\">";
    echo "<input type=\"hidden\" name=\"TGED_PAY\" value=\"".$_POST['TGED_PAY']."\">"; 
    echo "<input type=\"hidden\" name=\"TGED_ADD\" value=\"TRUE\">";
    echo "<input type=\"submit\"  value=\"Přidat\">";
    echo "</form>";
    }       
}
  
if (IsSet($_POST['TGED_SUBMIT']) && count($_SESSION['buffer_item'])>0) {      
    require "build/db_connect.inc.php"  ;
    $query="START TRANSACTION";
    $result = mysql_query($query);
    $query="INSERT INTO tarif_mix (Name,start_date,description) VALUES (\"".$_POST['TGED_NAME']."\",\"".$_POST['TGED_STARTDATE']."\",\"".$_POST['TGED_DESCRIPTION']."\")";
    
    $result = mysql_query($query);
    if (!$result) {
        die('Nelze uložit obecné parametry:' . mysql_error());
    }
    $query="SELECT last_insert_id()";
    $result = mysql_query($query);
    if (!$result) {
        die('Nelze uložit obecné parametry:' . mysql_error());
    }
    list($last_id)=mysql_fetch_row($result);
    $query="INSERT INTO spec_pay_type (idTARIFMIX,idPAYTYPE) VALUES(".$last_id.",".$_POST['TGED_PAY'].")"; 
    $result = mysql_query($query);
    if (!$result) {
        die('Způsob platby nelze yapsat:' . mysql_error());
    }
    for ($i=0;$i<count($_SESSION['buffer_item']);$i++){
        $query="INSERT INTO spec_tarif_mix (idTRUNK,idNUMBER,idTARIF,idTARIFMIX)
                      VALUES (".$_SESSION['buffer_item'][$i][0].",".$_SESSION['buffer_item'][$i][1].",".$_SESSION['buffer_item'][$i][2].",".$last_id.")";
        $result = mysql_query($query);
        if (!$result) {
            die('Data nelze vlozit do tabulky spec_tarif_mix:' . mysql_error());
        }
   } 
    
    
    $query="COMMIT";
    $result = mysql_query($query);
    unset ($_SESSION['buffer_item']);
    echo "Změny byly uloženy.<a href=\"admin_page.php?NEXTFORM=15\">Zpět</a>";
  }else{ 
   


?>
<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;
}
 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";
      if (Validate(document.forms.form1)==false){
          document.forms.form1.reset();
      }else{
          document.forms.form1.submit();
      }
     
  
}
</script> 

<div class="forms">

<h3>Vytvoření nové tarifní skupiny</h3><span class="backtoform"><a href="admin_page.php?NEXTFORM=10">Zpět</a> </span>
    <div class="userlist">
    </fieldset>
   <fieldset>
    <legend>Obecné údaje</legend>
    <form name="form1" action="admin_page.php" method="post" onSubmit="return Validate(this);">
    <table align="center">
      <tr>
        <td class="tableculombhead" style="text-align:right" colspan=3></td>
      </tr><tr>
        <td class="tablehead">Název</td>
        <td class="tablehead">Platnost od</td>
        <td class="tablehead"></td>
      </tr><tr>
        <td class="tableculombhead"><input type="text" name="TGED_NAME" value="<?php echo $_POST[TGED_NAME]; ?>"> </td> 
        <td class="tableculombhead" colspan=2><input id="f_rangeStart" type="text" <?php if($_POST['TGED_STARTDATE']=='') echo "disabled";?> name="TGED_STARTDATE" value="<?php echo $_POST[TGED_STARTDATE]; ?>"/> 
                                      <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($_POST['TGED_PAY']!=2) echo "checked"; ?>>Předplacený<input type="radio" name="TGED_PAY" value="2" <?php if($_POST['TGED_PAY']==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 $_POST['TGED_DESCRIPTION']; ?></textarea>   
             <div id="TGED_DESCRIPTION_PBar" style="width:0px;height:10px;border:0;font-size:8px;overflow:hidden;background-color: #3399FF;"></div>
         <td>
      
    </table>
 
  


   
       
    
    
    <table align="center" >
        <tr>
        <td colspan=5 class="tableculombhead" height="16pt"> </td>
        </tr><tr>
            <td class="tablehead"></td>
            <td class="tablehead">Trunk</td>
            <td class="tablehead">Prefix</td>
            <td class="tablehead" colspan=1>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=1> <?php MapaRoletka();?> </td>
            <td class="tableculombhead"> <?php OkTlacitko();?> </td>
        </tr>
            <?php 
            $a=0;
            for ($i=0;$i<count($_SESSION['buffer_item']);$i++){    
              $query="SELECT Name
                      FROM trunk  
                      WHERE idTrunk=".$_SESSION['buffer_item'][$i][0]."
                      AND validity=1";
              $result = mysql_query($query);
              if (!$result) {
                  die('Info trunku nelze načíst:' . mysql_error());
              }
              list($show_trunk)=mysql_fetch_row($result);         
              $query="SELECT number
                      FROM dest_number
                      WHERE idNUMBER=".$_SESSION['buffer_item'][$i][1]."
                      AND validity=1";
              $result = mysql_query($query);
              if (!$result) {
                  die('Info prefix nelze načíst:' . mysql_error());
              }
              list($show_number)=mysql_fetch_row($result);
              $query="SELECT Name
                      FROM tarif
                      WHERE idTARIF=".$_SESSION['buffer_item'][$i][2]."
                      AND validity=1";
              $result = mysql_query($query);
              if (!$result) {
                  die('Info cenová mapa nelze načíst:' . mysql_error());
              }
              list($show_tarif)=mysql_fetch_row($result);
              if($a==1){$a=0; $barva="#ffb67a";}else{$a=1;$barva="#ffdfc5"; }
              echo "<tr bgcolor=$barva>";
              echo "<td></td><td>".$show_trunk."</td><td>".$show_number."</td><td>".$show_tarif."</td><td></td>";
              echo "</tr>";                     
            }
              unset($show_trunk);
              unset($show_number);
              unset($show_tarif);
              unset($a);
            ?>
            </tr><tr>
        <!--  <input type="hidden" name="TGED_SUBMIT"> -->
          <td class="tablehead" colspan=6><input type="reset" value="Zrušit změny"><input name="TGED_SUBMIT" type="submit" value="Ulož"></td>
      </tr>
    </table>
    </fieldset> 
    </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 }?>