<?php
{

function VlastniUcetRoletka(){
require "build/db_connect.inc.php"  ;
    $query="SELECT UA.id,SB.name
            FROM user_account UA
            JOIN sip_buddies SB ON SB.id=UA.id AND UA.validity=1
            WHERE UA.idTECHNOLOGY=1
            AND idUSER=".$_SESSION["USERID"]."
                ";
    $result = mysql_query($query);
    if (!$result) {
         die('Roletku uživatelů nelze načíst:' . mysql_error());
    }
    echo "<select name=\"MYSELF\" size=1 id=\"acrol\" OnChange=\"RefreshForm(this);\">";
    while(list($idSELF,$name)=mysql_fetch_row($result)){
        if ($_POST['MYSELF']==$name){
            echo "<option value=".$name." selected>".$name."</option>";
        }else{
            echo "<option value=".$name.">".$name."</option>";
        }
    }
    echo "</select>";


}



function UserRoletka(){
require "build/db_connect.inc.php"  ;
    $query="SELECT idUSER,jmeno,prijmeni
            FROM user U
            WHERE U.validity=1
            AND idUSER<>1
            ORDER BY prijmeni,jmeno ASC
          ";
    $result = mysql_query($query);
    if (!$result) {
         die('Roletku uživatelů nelze načíst:' . mysql_error());
    }
    echo "<select name=\"USERS\" size=1 id=\"acrol\" OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\" >vyberte...</option>";
    while(list($idUSER,$jmeno,$prijmeni)=mysql_fetch_row($result)){
        if ($_POST['USERS']==$idUSER){
            echo "<option value=".$idUSER." selected>".$prijmeni." ".$jmeno."</option>";
        }else{
            echo "<option value=".$idUSER.">".$prijmeni." ".$jmeno."</option>";
        }
    }
    echo "</select>";


}



function AccountRoletka(){
require "build/db_connect.inc.php"  ;
    $query="SELECT UA.id,SB.name
            FROM user_account UA
            JOIN sip_buddies SB ON SB.id=UA.id AND UA.validity=1
            WHERE UA.idTECHNOLOGY=1
            AND idUSER=".$_POST['USERS']."
                ";
    $result = mysql_query($query);
    if (!$result) {
         die('Roletku uživatelů nelze načíst:' . mysql_error());
    }
    echo "<select name=\"ACCOUNT\" size=1 id=\"acrol\" id=\"account\" OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\" >vyberte...</option>";
    while(list($idACC,$name)=mysql_fetch_row($result)){
        if ($_POST['ACCOUNT']==$name){
            echo "<option value=".$name." selected>".$name."</option>";
        }else{
            echo "<option value=".$name.">".$name."</option>";
        }
    }
    echo "</select>";


}



###############################################################################

?>
<script language="JavaScript">
function RefreshForm(theForm){
  //    document.form1.TGE_SUBMIT.value="FALSE";
      
          document.forms.form1.submit();
      }

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.ACCOUNT,"Vyplňte prosím hodnotu!") ) return false;
      
  // uspech
  return true;
}
</script>
<center>
<div class="forms">
<h3>Vytočit účet</h3>
    <fieldset>
        <legend>Parametry vytáčení</legend>
    <form name="form1" action="index.php" method="post" >
      <table align="center">
        <tr>
              <td class="tablehead">Můj účet</td>
              <td class="tablehead">Volaný uživatel</td>
              <td class="tablehead">Účet volaného</td>
              
        
              <td class="tablehead"></td>
        </tr><tr>
            <td class="tableculombhead"> <?php VlastniUcetRoletka();?> </td> 
             <td class="tableculombhead"> <?php UserRoletka();?> </td>  
             <td class="tableculombhead"> <?php if($_POST['USERS']!=''){AccountRoletka();}else{echo "<input type=\"text\" name=\"ACCOUNT\" id=\"account\" size=\"3\" >";}?> </td>
          
            
            <td class="tableculombhead"><input type="submit" name="CHARGE" value="Vytočit" OnClick="return Validate(this);"> </td>
        </tr>
    
      </table>
    </form>
  
<?php
 ###############################################################################
if(IsSet($_POST['CHARGE']) && $_POST['ACCOUNT']){
    if (IsSet($_POST['ACCOUNT'])){
      
      $out_extension = $_POST['ACCOUNT'];
    }
 //  if (IsSet($_POST['USERS1'])){
  //    $out_extension = $_POST['USERS1'];
  //  }
    
//$out_extension = '607';
$login ='billing';
$password = 'billing';
$IP ='127.0.0.1';
$protocol = 'sip';
$caller = $_POST['MYSELF'];
//$out_extension = '605';
$out_context =  'default';

$socket = fsockopen($IP,"5038", $errno, $errstr, $timeout);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: ".$login."\r\n");
fputs($socket, "Secret: ".$password."\r\n\r\n");
$ret=fgets($socket,1024); 
echo "<center>";
echo $ret;
echo "login end\r\n";
echo "</center>";
/*login end*/

fputs($socket, "Action: Originate\r\n");
fputs($socket, "Channel: ".$protocol."/".$caller."\r\n");
fputs($socket, "Exten: ".$out_extension."\r\n");
fputs($socket, "Context: ".$out_context."\r\n");
fputs($socket, "Priority: 1\r\n");
fputs($socket, "Timeout: 10000\r\n");
fputs($socket, "Async: yes\r\n\r\n");
$ret=fgets($socket,10000);
echo "<center>".$ret."</center>";
echo "<center><br> Vytáčeno : <b>".$out_extension."</b></center>";

fputs($socket, "Action: Logoff\r\n\r\n");
fclose($socket);

}





?>

</fieldset>  
</div>

</center>

<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
}
?>