<?
  include ("../../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
 	sajax_init();
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $entry=Array(
      "name" => $_POST["name"],
      //"trunk_order" => $_POST["trunk_order"],
      "port" => $_POST["listRight"], 
      "msns" => $_POST["phonenumber"], 
      "did"  => $_POST["did"],
     //"rule" => $_POST["rule"],
      "speeddial" => $_POST["speeddial"]
    
); 

    //add options to the entry
    foreach ($_POST as $k=>$v) {
      if (substr($k,0,7)=="status_") {
        $k=substr($k,7);
        if ($v=="1") {
          $o["key"]=$k; 
          $o["value"]=$_POST["option_".$k];
          $options[]=$o;
        }
      }
    }
    $entry["option"]=$options;
    $entry["oldname"]=$_POST["oldname"];
    $entry["groupno"]=$_POST["groupno"]; 

    //insert new entry or modify only??
    if (isset($_POST["id"])) $entry["id"]=$_POST["id"];

    //save the group informations
    //echo "<pre>";print_r($entry);die; 
    $ret=$asterisk->putZapataGroup($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["zapatagrp"]["alertModGood"]."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["zapatagrp"]["alertModBad"]."</div>";
    
    goToMainPage($alert,"../groups.php");
  }
  //--------------------------------------------------------------------------
  
  //if modify, read the group informations
  if (isset($_GET["id"])) {
    $ret=$asterisk->getZapataGroup($_GET["id"]);
    if ($err=$asterisk->getError()) die("Error: $err");
    $id=$ret[0]["id"];
    $name=$ret[0]["name"];
    $rule=$ret[0]["rule"];
    $speeddial=$ret[0]["speeddial"];
    $trunk_order=$ret[0]["trunk_order"];
    $groupChannel=$ret[0]["channel"];
    $did=$ret[0]['did'];
    $groupOption=$ret[0]["option"];
    $groupno=$ret[0]["group"];
    if (is_array($ret[0]["msns"]))
    foreach ($ret[0]["msns"] as $n) {
      $numbers.="<option value=\"$n\">$n</option>";
    }
  }  

  //get the PORTS informations
  $id=$_GET["id"];
  $ret=$asterisk->getZapChannel(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $old_card_id=0;
  $count=1;
  if (is_array($ret)) {
  //echo "<PRE>";print_r($ret);
    foreach($ret as $v) {
      $channelid=$v["id"];
      $groupname=$v[group];
       $portid=$v[port_id];
       //$port=$v["port"];
      if ($v[channelfrom]==$v[channelto]){
       $channel=$v[channelfrom];
      }else{
      $channel=$v['channelfrom']."-".$v['channelto'];}
      $available=$v["available"];
      $ree=$asterisk->getZapPort($portid);
//echo "<PRE>";print_r($ree);die();
     $card_id=$ree[0][card_id];
     $port=$ree[0][port];
      if ($name==$groupname and $name!='') $portsRight.="<option value=\"$channelid\" >Card.$card_id/Port.$port Ch.$channel - port ID($portid) </option>";   
      else if ($available==1) $portsLeft.="<option value=\"$channelid\" >Card.$card_id/Port.$port Ch.$channel - port ID($portid) </option>";    
      $old_card_id=$card_id;
      $count++;
    }  
  }

  //get the options
  $html=getOption(OPTION_ISDN_GROUPS,$groupOption);
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>

  <? $sel="groups"; include ("../navigation.php");?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["zapatagrp"]["title"]?></h1>
    <h3><a href="../groups.php" class="admin" ><?=$cwlang["zapatagrp"]["return"]?></a></h3>
  </div>
  
  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if (isset($_GET["id"])) echo "?id=".$_GET["id"];?>" method="post" onSubmit="selectAll(getElementById('listRight')); return selectAll(getElementById('phonenumber'))">
      
      <p class="blockintro"><?=$cwlang["zapatagrp"]["titleDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required"><label for="group[name]"><?=$cwlang["zapatagrp"]["groupName"]?>:</label></td>
            <td><input type="text" size="25" maxlength="30" id="group[name]" name="name" value="<?=$name?>" /></td>
          </tr>
          <tr>
            <td class="required"><label for="speeddial"><?=$cwlang["zapatagrp"][""]?>Speed Dial:</label></td>
            <td>
              <input type="text" name="speeddial" value="<?=$speeddial?>" size="3" maxlength="3"><big><strong>*</strong></big>
              <span class="help">Write a number to speed dial with this trunk. I.e. <strong>1</strong> or <strong>76</strong></span>
          <tr>
            <td class="required"><label for="groupno">Group No.:</label></td>
            <td>
              <input type="text" name="groupno" value="<?=$groupno?>" size="3" maxlength="3"><big><strong>#</strong></big>
              <span class="help">Zapata group number. Range: <strong>0</strong> to <strong>63</strong></span>
              <!-- 
              <input type="text" size="5" maxlength="2" id="group[trunk_order]" name="trunk_order" value="<?=$trunk_order?>" />
              -->
            </td>
          </tr>
          <tr>
            <td class="help" colspan="2">
              <!--
              <?=$cwlang["zapatagrp"]["enableOutcomingCallHelp"]?>
              -->
            </td>
          </tr>
        </table>
      </div>

    	<h2><?=$cwlang["zapatagrp"]["listeningNumber"]?></h2>
      <p class="blockintro"><?=$cwlang["zapatagrp"]["listeningNumberHelp"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required"><label for="number"><?=$cwlang["zapatagrp"]["phoneNumber"]?>:</label></td>
            <td class="required"><?=$cwlang["zapatagrp"]["phoneNumberAdded"]?></td>
          </tr>
          <tr>
            <td valign="top">
              <input type="text" size="25" maxlength="100" id="number" name="number" value="" />
              <input type="button" value="+" onclick="addOption(getElementById('phonenumber'),document.getElementById('number').value); document.getElementById('number').value='' " title="Add Length">
<div><strong>DID:</strong></div><div><?=showDid($did);?></div>
 
            </td>
            <td>
              <select name="phonenumber[]" size="4" onchange="saved = false" ondblclick="editOptions(this)" multiple style="float: left" id="phonenumber">
                <?=$numbers?>
              </select>
              <input type="button" value="edit" onclick="editOptions(getElementById('phonenumber'))" title="Edit Length(s)">
              <input type="button" value="X" onclick="deleteOptions(getElementById('phonenumber'))" title="Delete Length(s)">
            </td>
          </tr>
        </table>
      </div>
      
    	<h2><?=$cwlang["zapatagrp"]["ports"]?></h2>
      <p class="blockintro"><?=$cwlang["zapatagrp"]["portsDesc"]?></p>
      <div class="block">
        <table border="0">
        <tr>
          <td class="required">
            <div ><?=$cwlang["zapatagrp"]["availablePorts"]?>:</div>
            <select multiple size="5"  name="listLeft[]" id="listLeft"><?=$portsLeft?></select>
          </td>
          <td>   
            <div><input type="button" style="width:90" onclick="moveDualList( document.getElementById('listLeft'),  document.getElementById('listRight'), false )" name="Add     >>"  value="Aggiungi       >>" style="width:100px"></div>
            <div><input type="button" style="width:90" onclick="moveDualList( document.getElementById('listRight'), document.getElementById('listLeft'),  false )" name="Add     <<"  value="<<     Rimuovi" style="width:100px"></div>
            <div><input type="button" style="width:90" onclick="moveDualList( document.getElementById('listLeft'),  document.getElementById('listRight'), true  )" name="Add All >>"  value="Aggiungi tutte >>" style="width:100px"></div>
            <div><input type="button" style="width:90" onclick="moveDualList( document.getElementById('listRight'), document.getElementById('listLeft'),  true  )" name="Add All <<"  value="<< Rimuovi tutto " style="width:100px"></div>
          </td>
          <td class="required"><div ><?=$cwlang["zapatagrp"]["groupPorts"]?>:</div>
            <select multiple size="5" name="listRight[]" id="listRight"><?=$portsRight?></select>
          </td>
        </tr>
        </table>
      </div>
<?echo $js;?>
<?   // echo listRule($js, $rule, RULE_INCOMING);?>   
<?//    echo listOption($js, 5, $groupOption, false,false,RULE_INCOMING);?>   
<?    echo listOption($js, 5, $groupOption, false,false,RULE_OUTGOING);?>   
<!--      
      <a name="groupOptionTab"></a>
      <h2><?=$cwlang["zapatagrp"]["option"]?></h2>
      <div><a href="#groupOptionTab" onclick="document.getElementById('groupOption').style.display='block'" class="admin" ><?=$cwlang["zapatagrp"]["show"]?></a> <a href="#groupOptionTab" onclick="document.getElementById('groupOption').style.display='none'" class="admin" ><?=$cwlang["zapatagrp"]["hide"]?></a> </div>
      <p class="blockintro"><?=$cwlang["zapatagrp"]["optionDesc"]?></p>
      <div class="block" style="display: none" id="groupOption">
        <table>
          <?=$html?>      
        </table>
      </div>      
-->
      
    <?=$numHidden?>      
    <div class="action">
      <?if (isset($_GET["id"])) {?>
<input type="hidden" name="id" value="<?=$id?>" />
<input type="hidden" name="oldname" value="<?=$name?>" /><?}?>
      <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../groups.php'; return false;"/>
      <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
    </div>
  </form>
 </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <?$sel="zapata";include("../../sidebar.php");?>
          <h1><?=$cwlang["zapatagrp"]["sidebarTitle"]?></h1>
          <p><?=$cwlang["zapatagrp"]["sidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

  $smarty->assign("section", "zapata");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->assign("ajax", $js);
  $smarty->display('settings.tpl');
?>

