<?
  include ("../admin/config/cfg.php");
  global $cwBasePath,$cwlang;
  require_once("$absBasePath/lib/xajax/xajax.inc.php");
  
  //=================== XAJAX ==================
  function showApplet(){
    global $cwUserInfo, $asterisk, $cwlang;
    $objResponse = new xajaxResponse();
    $html=" <APPLET name=\"phoneBox\" code=\"PhoneBox/Applet.class\" width=\"298\" height=\"475\"> \n"; 
    //retrive user extensions and password
    if ($cwUserInfo["entity_id"]!=0) {
      $client=$asterisk->getClient($cwUserInfo["entity_id"],0);
      $ext=$client[0]["extension"];
      $pass=$client[0]["password"];
      $html.="  <param name=\"username\" value=\"$ext\" /> \n";
      $html.="	<param name=\"password\" value=\"$pass\" /> \n";
    } 
    //retrieve sip extern IP
    $sipOption=$asterisk->getOptionByName("externip","sip");
    $externIp=$sipOption[0]["value"];
    $html.="	 <param name=\"serverIp\" value=\"$externIp\" /> \n";
    $html.="	 <param name=\"archive\" value=\"PhoneBox.jar\" /> \n";
    $html.="	 <param name=\"serverPort\" value=\"5060\" /> \n";
    $html.="	 <param name=\"localPort\" value=\"5070\" /> \n";
    $html.="	 <param name=\"localRtpStartPort\" value=\"\" /> \n";
    $html.="	 <param name=\"localRtpEndPort\" value=\"\" /> \n";
    $html.="	 <param name=\"jmfDownloadPage\" value=\"http://java.sun.com/products/java-media/jmf/2.1.1/download.html\" /> \n";
    $html.=" </APPLET> \n";
    $objResponse->addAssign("main","innerHTML", $html);
    return $objResponse->getXML();
  }
  $xajax = new xajax();
  $xajax->registerFunction("showApplet");
  $xajax->processRequests();
  
  //================= PAGE LOGIC ==================
  //create the list of users
  $ret=$asterisk->getDirectory(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) {
    foreach($ret as $v) {
      $name=$v["name"];
      $username=$v["username"];
      $status=$v["status"];
      $domain=$soapHostname;
      if (strpos($status,"OK")!==false) $class="online";
      if (strpos($status,"UNKNOWN")!==false) $class="offline";
      if (strpos($status,"Unmonitored")!==false) $class="unknown";
      $txt.="<tr><td class=\"$class\"><img src=\"../admin/public/img/$class.gif\" /> <a href=\"#\" onclick=\"document.phoneBox.setPhoneNumber('$username'); return false;\" >$name</a></td><td class=\"$class\">$class</td></tr>";
    }
    $html="<table id=\"directory\">";
    $html.="<th>".$cwlang["phonebook"]["name"]."</th><th>".$cwlang["phonebook"]["status"]."</th>";
    $html.="$txt</table>";
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../admin/lib/xajax/"); 
  ?>
  <div class="SectionHeader">
    <h1><?=$cwlang["phone"]["title"]?>SIP Phone</h1>
  </div>
  
  <h2><?=$cwlang["phone"]["title2"]?></h2>
  <p class="blockintro">
    <?=$cwlang["phone"][""]?>
    You can dial using your user account. The below applet is a full working sip
    client that is registered with your asterisk account.
  </p>
    
  <div id="main" style="text-align: center;">
    <div style="width: 298px; height: 475px; background: red url(../admin/public/img/applet_bg.gif) no-repeat; margin: 0 auto; ">
      <input type="button" value="Carica Client" onclick="javascript:xajax_showApplet();" style="margin-top: 360px;"/>
    </div>
  </div>
  
  <!--<input type="button" value="chiama" onclick='document.phoneBox.performActionDial();' />-->

<?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang[""]["information"]?>Contacts</h1>
          <p><?=$cwlang[""]["sidebarText"]?></p>
          <?=$html?>
          <div style="text-align: center"><img src="../admin/public/img/book.jpg" alt="Rubrica" align="center"/></div>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  $smarty->assign("section", "phone");
  $smarty->assign("subsection", "");
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('main.tpl');
?>

