<?
/**
 * VoiceOne Settings/IVR/ 
 *
 * Frontend to provide access to the ivr system in voiceone.
 * @package voiceone
 * @subpackage  settings_ivr
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang;
 	require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  sajax_init();

  //================ XAJAX ================
  function processFormData($formData){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Extract form's params
    $node=$formData["node"];
    $id=$node["id"];
    $name=$node["name"];
    $type=$node["type"];
    $key=$node["key"];
    $parent=$node["parent"];
    $macro=$formData["macro"];
    $link=$node["link"];
    $imgErr="<img src=\"".$relBasePath."/public/img/shutdown.png\" alt=\"error\">&nbsp;&nbsp;";   
    // Retrieve all nodes name
    $nodeArr=$asterisk->getIvrNode(0,0);
    if(is_array($nodeArr)){
      foreach($nodeArr as $n){
        if($id!=$n["id"]) $nNameArr[]=$n["name"];
      }
    }
    // Retrieve all keys on same level
    if($parent!=0){
      $nodeArr=$asterisk->getIvrNode(0,$parent);
      if(is_array($nodeArr)){
        foreach($nodeArr as $n){
          if($id!=$n["id"]) $nKeyArr[]=$n["key"];
        }
      }
    }
    // Check Name ((not null)&&(unique))
    $nameErr=false;
    if($name=="") { 
      //name cannot be null
      $errOut[]= T_("Name cannot be null");
      $objResponse->addAssign("nameErr","innerHTML",$imgErr);
      $nameErr=true;  
    }/* else if((is_array($nNameArr))&&(in_array($name,$nNameArr))){
      //name must be unique
      $errOut[]= T_("Name already exists. Please change it");
      $objResponse->addAssign("nameErr","innerHTML",$imgErr);
      $nameErr=true;
    }*/
    if($nameErr===false) $objResponse->addAssign("nameErr","innerHTML","");
    // Check Key ((not null)and(unique on level)and(numeric or * or #)) if parent!=0
    $keyErr=false;
    if($parent!=0){
      if($key==""){
        //key cannot be null
        $errOut[]=T_("Key cannot be null");
        $objResponse->addAssign("keyErr","innerHTML",$imgErr);
        $keyErr=true;
      }else if((is_array($nKeyArr))&&(in_array($key,$nKeyArr))){
        //key must be unique on level
        $errOut[]=T_("Key already exists in menu. Please change it");
        $objResponse->addAssign("keyErr","innerHTML",$imgErr);
        $keyErr=true;
      }else{
        //key must be (numeric or * or #)
        for($i=0; $i<strlen($key); $i++){
          $ch=ord($key[$i]);
          if($ch==32){ // " "
            $errOut[]=T_("Key doesn't allow spaces. Please change it");
            $objResponse->addAssign("keyErr","innerHTML",$imgErr);
            $keyErr=true;
            break;
          }                       
          if(!((($ch>47)&&($ch<58))||($ch==35)||($ch==42))){ // !(numeric or "#" or "*")
            $errOut[]=sprintf(T_("Key doesn't allow character \"%1\$s\" Please change it."),$key[$i] );
            $objResponse->addAssign("keyErr","innerHTML",$imgErr);
            $keyErr=true;
            break;
          }
        }
      }
    }
    if($keyErr===false) $objResponse->addAssign("keyErr","innerHTML","");
    // Check Macro (not null for leaf)
    $macroErr=false;
    if(($type==IVR_LEAF)&&($macro=="")){
      //macro cannot be null
      $errOut[]=T_("Macro cannot be null");
      $objResponse->addAssign("macroErr","innerHTML",$imgErr);
      $macroErr=true;
    }
    if($macroErr===false) $objResponse->addAssign("macroErr","innerHTML","");
    // Check link (not null for link)
    $linkErr=false;
    if(($type==IVR_LINK)&&($link=="")){
      //macro cannot be null
      $errOut[]=T_("Link cannot be null");
      $objResponse->addAssign("linkErr","innerHTML",$imgErr);
      $linkErr=true;
    }
    if($linkErr===false) $objResponse->addAssign("linkErr","innerHTML","");
    // Process Form
    if(isset($errOut)){
      $error="<div class=\"AlertBad\"><ul>";
      foreach($errOut as $err) $error.="<li>".$err."</li>";
      $error.="</ul></div>";
      $objResponse->addAssign("alert","innerHTML",$error);
      $objResponse->addAssign("loading","innerHTML","");
    }else{
      $objResponse->addAssign("loading","innerHTML",""); 
      $objResponse->addScript(" document.getElementById('mainform').submit(); ");
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  function showRootPanel(){
    global $relBasePath,$asterisk,$cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $script=" document.getElementById('div_macro').style.display = 'none'; ";
    $script.=" document.getElementById('div_audio').style.display = 'block'; ";
    $script.=" document.getElementById('div_beep').style.display = 'block'; ";
    $script.=" document.getElementById('div_menu').style.display = 'none'; ";
    $script.=" document.getElementById('div_timeout').style.display = 'block'; ";
    $script.=" document.getElementById('div_invalid').style.display = 'block'; ";
    // Return the xajaxResponse object
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function showNodePanel(){
    global $relBasePath,$asterisk,$cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $script=" document.getElementById('div_macro').style.display = 'none'; ";
    $script.=" document.getElementById('div_audio').style.display = 'block'; ";
    $script.=" document.getElementById('div_beep').style.display = 'block'; ";
    $script.=" document.getElementById('div_menu').style.display = 'block'; ";
    $script.=" document.getElementById('div_timeout').style.display = 'block'; ";
    $script.=" document.getElementById('div_invalid').style.display = 'block'; ";
    $script.=" document.getElementById('div_link').style.display = 'none'; ";
    // Return the xajaxResponse object
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function showLeafPanel(){
    global $relBasePath,$asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $script=" document.getElementById('div_macro').style.display = 'block'; ";
    $script.=" document.getElementById('div_audio').style.display = 'block'; ";
    $script.=" document.getElementById('div_beep').style.display = 'none'; ";
    $script.=" document.getElementById('div_menu').style.display = 'block'; ";
    $script.=" document.getElementById('div_timeout').style.display = 'none'; ";
    $script.=" document.getElementById('div_invalid').style.display = 'none'; ";
    $script.=" document.getElementById('div_link').style.display = 'none'; ";
    // Return the xajaxResponse object
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function showLinkPanel(){
    global $relBasePath,$asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $script=" document.getElementById('div_macro').style.display = 'none'; ";
    $script.=" document.getElementById('div_audio').style.display = 'block'; ";
    $script.=" document.getElementById('div_beep').style.display = 'none'; ";
    $script.=" document.getElementById('div_menu').style.display = 'block'; ";
    $script.=" document.getElementById('div_timeout').style.display = 'none'; ";
    $script.=" document.getElementById('div_invalid').style.display = 'none'; ";
    $script.=" document.getElementById('div_link').style.display = 'block'; ";
    // Return the xajaxResponse object
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function refreshAudioList($node_id, $selAudio){
    global $relBasePath,$asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    if(($node_id!="")&&($node_id!=0)){
      $ret=$asterisk->getIvrNode($node_id,0);
      if(is_array($ret)){
        $node=$ret[0];
        $maudio=$node["maudio"];
        $waudio=$node["waudio"];
        $taudio=$node["taudio"];
        $iaudio=$node["iaudio"];
      }
    }
    //reset audio list
    $script_a=" document.getElementById('audio').options.length = 0; ";
    $script_w=" document.getElementById('node[waudio]').options.length = 0; ";
    $script_m=" document.getElementById('node[maudio]').options.length = 0; ";
		$script_t=" document.getElementById('node[taudio]').options.length = 0; ";
		$script_i=" document.getElementById('node[iaudio]').options.length = 0; ";
    //set no-audio options
    $script_a.=" var optObj_a0=document.createElement('option'); optObj_a0.value=''; optObj_a0.innerText='".addslashes("[none]")."'; optObj_a0.text='".addslashes("[none]")."'; ";
    $script_a.=" document.getElementById('audio').appendChild(optObj_a0); ";
    $script_w.=" var optObj_w0=document.createElement('option'); optObj_w0.value=''; optObj_w0.innerText='".addslashes("[none]")."'; optObj_w0.text='".addslashes("[none]")."'; ";
		$script_w.=" document.getElementById('node[waudio]').appendChild(optObj_w0); ";
    $script_m.=" var optObj_m0=document.createElement('option'); optObj_m0.value=''; optObj_m0.innerText='".addslashes("[none]")."'; optObj_m0.text='".addslashes("[none]")."'; ";
    $script_m.=" document.getElementById('node[maudio]').appendChild(optObj_m0); ";
    $script_t.=" var optObj_t0=document.createElement('option'); optObj_t0.value=''; optObj_t0.innerText='".addslashes("[none]")."'; optObj_t0.text='".addslashes("[none]")."'; ";
    $script_t.=" document.getElementById('node[taudio]').appendChild(optObj_t0); ";
    $script_i.=" var optObj_i0=document.createElement('option'); optObj_i0.value=''; optObj_i0.innerText='".addslashes("[none]")."'; optObj_i0.text='".addslashes("[none]")."'; ";
    $script_i.=" document.getElementById('node[iaudio]').appendChild(optObj_i0); ";
    /*
    $audioList="<option value=\"\">[none]</option>\n";
    $wAudioList="<option value=\"\">[none]</option>\n";
    $mAudioList="<option value=\"\">[none]</option>\n";
    $tAudioList="<option value=\"\">[none]</option>\n";
    $iAudioList="<option value=\"\">[none]</option>\n";
  	*/
    //get audio lists
    $ret=$asterisk->getIvrAudioFile();
    if(is_array($ret)){
    	$i=0;
      foreach($ret as $v){
        $audio_id=$v["id"];
        $audio_name=$v["name"];
        $i++;
        //audio
        $script_a.=" var optObj_a".$i."=document.createElement('option'); optObj_a".$i.".value='".$audio_id."'; optObj_a".$i.".innerText='".addslashes($audio_name)."'; optObj_a".$i.".text='".addslashes($audio_name)."'; ";
        if($audio_id==$selAudio) $script_a.=" optObj_a".$i.".selected=true; ";
        $script_a.=" document.getElementById('audio').appendChild(optObj_a".$i."); ";
        //welcome
        $script_w.=" var optObj_w".$i."=document.createElement('option'); optObj_w".$i.".value='".$audio_id."'; optObj_w".$i.".innerText='".addslashes($audio_name)."'; optObj_w".$i.".text='".addslashes($audio_name)."'; ";
        if($audio_id==$waudio) $script_w.=" optObj_w".$i.".selected=true; ";
        $script_w.=" document.getElementById('node[waudio]').appendChild(optObj_w".$i."); ";
        //menu
				$script_m.=" var optObj_m".$i."=document.createElement('option'); optObj_m".$i.".value='".$audio_id."'; optObj_m".$i.".innerText='".addslashes($audio_name)."'; optObj_m".$i.".text='".addslashes($audio_name)."'; ";
        if($audio_id==$maudio) $script_m.=" optObj_m".$i.".selected=true; ";
       	$script_m.=" document.getElementById('node[maudio]').appendChild(optObj_m".$i."); ";
        //timeout
        $script_t.=" var optObj_t".$i."=document.createElement('option'); optObj_t".$i.".value='".$audio_id."'; optObj_t".$i.".innerText='".addslashes($audio_name)."'; optObj_t".$i.".text='".addslashes($audio_name)."'; ";
        if($audio_id==$taudio) $script_t.=" optObj_t".$i.".selected=true; ";
        $script_t.=" document.getElementById('node[taudio]').appendChild(optObj_t".$i."); ";
        //invalid
				$script_i.=" var optObj_i".$i."=document.createElement('option'); optObj_i".$i.".value='".$audio_id."'; optObj_i".$i.".innerText='".addslashes($audio_name)."'; optObj_i".$i.".text='".addslashes($audio_name)."'; ";
        if($audio_id==$iaudio) $script_i.=" optObj_i".$i.".selected=true; ";
        $script_i.=" document.getElementById('node[iaudio]').appendChild(optObj_i".$i."); ";
        /*
        if($audio_id==$selAudio) $audioList.="<option value=\"$audio_id\" selected=\"selected\">$audio_name</option>\n"; else $audioList.="<option value=\"$audio_id\">$audio_name</option>\n";
        if($audio_id==$waudio) $wAudioList.="<option value=\"$audio_id\" selected=\"selected\">$audio_name</option>\n"; else $wAudioList.="<option value=\"$audio_id\">$audio_name</option>\n"; 
        if($audio_id==$maudio) $mAudioList.="<option value=\"$audio_id\" selected=\"selected\">$audio_name</option>\n"; else $mAudioList.="<option value=\"$audio_id\">$audio_name</option>\n";
        if($audio_id==$taudio) $tAudioList.="<option value=\"$audio_id\" selected=\"selected\">$audio_name</option>\n"; else $tAudioList.="<option value=\"$audio_id\">$audio_name</option>\n";
        if($audio_id==$iaudio) $iAudioList.="<option value=\"$audio_id\" selected=\"selected\">$audio_name</option>\n"; else $iAudioList.="<option value=\"$audio_id\">$audio_name</option>\n";
        */ 
      }
    }
    // Return the xajaxResponse object
    /*
    $objResponse->addAssign("audio","innerHTML",$audioList);
    $objResponse->addAssign("node[waudio]","innerHTML",$wAudioList);
    $objResponse->addAssign("node[maudio]","innerHTML",$mAudioList);
    $objResponse->addAssign("node[taudio]","innerHTML",$tAudioList);
    $objResponse->addAssign("node[iaudio]","innerHTML",$iAudioList);
    */
    $objResponse->addScript($script_a);
    $objResponse->addScript($script_w);
    $objResponse->addScript($script_m);
    $objResponse->addScript($script_t);
    $objResponse->addScript($script_i);
    return $objResponse->getXML();  
  }
  function makeAudioPanel() {
    global $asterisk,$relBasePath,$cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret=$asterisk->getIvrAudioFile();
    $option="<option value=\"\">[none]</option>";
    if (is_array($ret)){
      foreach($ret as $v) {
        $id=$v["id"];
        $name=$v["name"];
        $filename=$v["filename"];
        if ($id==$selFile) $sel=" selected='selected' "; else $sel="";
        $option.="<option value=\"$id\" $sel>$name</option>";
      }
    }
    $audioList="<select name=\"$fieldName\" id=\"audio\">$option</select> ";
    $option="";
    $ret=$asterisk->getClient(0,0);
    if($err=$asterisk->getError()) die("Error: $err");
    if(is_array($ret)){
      foreach($ret as $v){
        $id=$v["id"];
        $name=$v["name"];
        $surname=$v["surname"];
        $extension=$v["extension"];
        $option.="<option value=\"$id\">$name $surname &lt;$extension&gt;</option>";
      }
      $clientList="<select name=\"clientToCall\" id=\"clientToCall\">$option</select>";
    }else $clientList="<input type=\"text\" size= \"30\" name=\"clientToCall\" id=\"clientToCall\" disabled=\"disabled\" value=\"[".T_("There are no defined extensions")."]\" />";
    ob_start()
    ?>
      <table cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td>
            <?=$audioList?>
            </td>
          <td align="left">
            <!-- rename an existing audiofile -->  
            <img style="cursor: pointer;" src="<?=$relBasePath?>/public/img/icon_edit.gif" alt="rename audio file" onclick=" var selectObj=document.getElementById('audio'); if(selectObj.options[selectObj.selectedIndex].value==''){alert('<?=T_("Select an audio file before rename it")?>'); return false;} if (newFilename=prompt('<?=T_("Enter file name:")?>', selectObj.options[selectObj.selectedIndex].innerHTML)) { document.getElementById('audioStatus').innerHTML='<img src=\'<?=$relBasePath?>/public/img/ico-progress.gif\' >'; xajax_renameAudio(selectObj.options[selectObj.selectedIndex].value, newFilename);}else return false;" />
            <!-- delete an existing audiofile -->     
            <img style="cursor: pointer;" src="<?=$relBasePath?>/public/img/icon_remove.gif" alt="delete audio file" onclick=" var selectObj=document.getElementById('audio'); if(selectObj.options[selectObj.selectedIndex].value==''){alert('<?=T_("Select an audio file before delete it")?>'); return false;} if (confirm('<?=T_("Are you sure to delete audio file")?> ' + selectObj.options[selectObj.selectedIndex].innerHTML)){ document.getElementById('audioStatus').innerHTML='<img src=\'<?=$relBasePath?>/public/img/ico-progress.gif\' >'; xajax_deleteAudio(selectObj.options[selectObj.selectedIndex].value);} else return false;" />
          </td>
        </tr>
        <tr>
          <td><?=$clientList?></td>
          <td align="left">
            <!-- play an existing audiofile -->  
            <img style="cursor: pointer;" src="<?=$relBasePath?>/public/img/play.gif" alt="play audio file" onclick=" var selectObj=document.getElementById('audio'); if(selectObj.options[selectObj.selectedIndex].value==''){alert('<?=T_("Select the audio file to reproduce")?>'); return false;} document.getElementById('audioStatus').innerHTML='<img src=\'<?=$relBasePath?>/public/img/ico-progress.gif\' >'; xajax_playAudio(selectObj.options[selectObj.selectedIndex].value);" />
            <!-- record a new audiofile -->
            <img style="cursor: pointer;" src="<?=$relBasePath?>/public/img/record.gif" alt="record audio file" onclick=" if(document.getElementById('clientToCall').selectedIndex == null){alert('<?=T_("There are no defined extensions")?>'); return false;} if (filename=prompt('<?=T_("Enter file name:")?>','audio'+Math.round(Math.random()*1000))) { document.getElementById('audioStatus').innerHTML='<img src=\'<?=$relBasePath?>/public/img/ico-progress.gif\' >'; var selectObj=document.getElementById('clientToCall'); xajax_recordAudio(selectObj.options[selectObj.selectedIndex].value, filename);} else return false;" /> 
            <!-- show status message -->      
          <span id="audioStatus"></span>
          </td>
        </tr>
      </table>
    <?
    $html=ob_get_contents();
    ob_end_clean();
    // Return the xajaxResponse object
    $objResponse->addAssign("audioRecorder","innerHTML", $html);
    return $objResponse->getXML();  
  }
  function renameAudio($id,$filename){
    global $relBasePath,$asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret=$asterisk->renameIvrAudioFile($id, $filename.".gsm", $filename);
    // Return the xajaxResponse object
    $script=" var selectObj=document.getElementById('audio'); "; 
    $script.=" var selAudio=selectObj.options[selectObj.selectedIndex].value; ";
    $script.=" xajax_refreshAudioList(document.getElementById('node[id]').value, selAudio); ";
    $script.=" document.getElementById('audioStatus').innerHTML=''; ";
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function deleteAudio($id){
    global $relBasePath,$asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret=$asterisk->deleteIvrAudioFile($id);
    // Return the xajaxResponse object
    $script=" var selectObj=document.getElementById('audio'); "; 
    $script.=" var selAudio=selectObj.options[selectObj.selectedIndex].value; ";
    $script.=" xajax_refreshAudioList(document.getElementById('node[id]').value, selAudio); ";
    $script.=" document.getElementById('audioStatus').innerHTML=''; ";
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function playAudio($id){
    global $relBasePath,$asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret=$asterisk->getSound($id);
    if(is_array($ret)){
      $sound=$ret[0];
      $script=" location.href='stream.php?filename=".addslashes($sound['name'])."&id=".$sound['category']."&lang=".$sound['language']."'; ";
    }
    // Return the xajaxResponse object
    $script.=" document.getElementById('audioStatus').innerHTML=''; ";
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function recordAudio($clientToCall, $filename){
    global $relBasePath,$asterisk,$cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $status="";
    $ret=getClient($clientToCall);
    if(is_array($ret)){
      $clientExtension=$ret[0]["extension"];
      $ret=$asterisk->getDirectory(0);
      if(is_array($ret)){
        foreach($ret as $v){
          if($v["extension"]==$clientExtension) $status=$v["status"];
        }
      } 
    }
    if(($status!="")&&(strpos($status,"OK")!==false)){
      $ret=$asterisk->recordIvrAudioFile($filename,$clientToCall);
      if($ret>0) $ret=$asterisk->putIvrAudioFile($filename,$filename.".gsm");
      $script=" var selectObj=document.getElementById('audio'); "; 
      $script.=" var selAudio=selectObj.options[selectObj.selectedIndex].value; ";
      $script.=" alert('".T_("Press ok when you finish recording audio message")."'); ";
      $script.=" xajax_refreshAudioList(document.getElementById('node[id]').value, selAudio); ";
    }else{
      $script=" alert('".T_("Unbale to record. Client is unreachable")."'); "; 
    }
    // Return the xajaxResponse object
    $script.=" document.getElementById('audioStatus').innerHTML=''; ";
    $objResponse->addScript($script);
    return $objResponse->getXML();  
  }
  function makeTree($id){
    global $cwlang, $asterisk, $relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    //retrieve ivr informations
    $ret=getIvrNode($id,0);
    if(is_array($ret)){
      $node=$ret[0];
      $node_name=$node["name"];
      $nodeTimesegment=$node["timesegment"];
      if($nodeTimesegment==0) $icon=$relBasePath."public/img/xtree/foldericon.png";
      else $icon=$relBasePath."public/img/xtree/foldericon_ts.png";
      // make tree
      $htmlscript="  webFXTreeConfig.deleteMessage='".T_("Are you sure to delete node")."';  \n";
      $htmlscript="  webFXTreeConfig.lMinusIcon='../../../admin/public/img/xtree/Lminus.png';  \n";
      $htmlscript.="  webFXTreeConfig.rootIcon='../../../admin/public/img/xtree/foldericon.png';  \n";
    	$htmlscript.="  webFXTreeConfig.openRootIcon ='../../../admin/public/img/xtree/openfoldericon.png';  \n";
    	$htmlscript.="  webFXTreeConfig.folderIcon='../../../admin/public/img/xtree/foldericon.png';  \n";
    	$htmlscript.="  webFXTreeConfig.openFolderIcon='../../../admin/public/img/xtree/openfoldericon.png';  \n";
    	$htmlscript.="  webFXTreeConfig.fileIcon='../../../admin/public/img/xtree/file.png';  \n";
    	$htmlscript.="  webFXTreeConfig.iIcon='../../../admin/public/img/xtree/I.png';  \n";
    	$htmlscript.="  webFXTreeConfig.lIcon='../../../admin/public/img/xtree/L.png';  \n";
    	$htmlscript.="  webFXTreeConfig.lMinusIcon='../../../admin/public/img/xtree/Lminus.png';  \n";
    	$htmlscript.="  webFXTreeConfig.lPlusIcon='../../../admin/public/img/xtree/Lplus.png';  \n";
    	$htmlscript.="  webFXTreeConfig.tIcon='../../../admin/public/img/xtree/T.png';  \n";
    	$htmlscript.="  webFXTreeConfig.tMinusIcon='../../../admin/public/img/xtree/Tminus.png';  \n";
    	$htmlscript.="  webFXTreeConfig.tPlusIcon='../../../admin/public/img/xtree/Tplus.png';  \n";
    	$htmlscript.="  webFXTreeConfig.blankIcon='../../../admin/public/img/xtree/blank.png';  \n";
      $htmlscript.="  var name='".$node_name."' + ' <a href=\"?ivr=".$id."&parentid=".$id."\"><img src=\"../../../admin/public/img/add.gif\" alt=\"Add a new node\"/></a>'; \n";
      $htmlscript.="  var tree = new WebFXTree(name); \n";
      $htmlscript.="  tree.setBehavior('classic');  \n";
      $htmlscript.="  tree.icon = '".$icon."';  \n";
      $htmlscript.="  tree.openIcon = '".$icon."';  \n";
      $htmlscript.="  tree.action = './?ivr=".$id."&id=".$id."';  \n";
      $htmlscript.= writeTree($id,$id,"tree",$selected);
      $htmlscript.="  document.getElementById('tree').innerHTML=tree;  \n"; 
      $htmlscript.="  tree.expandAll();  \n";         
    }else{
      $htmlscript.="  document.getElementById('tree').innerHTML='Tree error';  \n";
    }
    // Return the xajaxResponse object
    $objResponse->addScript($htmlscript);   
    return $objResponse->getXML();  
  } 
  //Private function
  function writeTree($ivr,$parentNode,$parentVar){
    global $asterisk, $relBasePath;
    $return="";
    $nodeArr=$asterisk->getIvrNode(0,$parentNode);
    if(is_array($nodeArr)){
      foreach($nodeArr as $n){
        $nodeId=$n["id"];
        $nodeName=$n["name"];
        $nodeType=$n["type"];
        $nodeTimesegment=$n["timesegment"];
        $nodeKey=$n["key"];
        $varName="node_".$nodeId;
        if($nodeKey!="") $key="$nodeKey &rarr; "; else $key="";
        $subTree=writeTree($ivr,$nodeId,$varName);
        // Node has no children
        if($subTree==""){
          //select icon  
          if(($nodeType==IVR_NODE)&&($nodeTimesegment==0)) $icon=$relBasePath."public/img/xtree/foldericon.png";
          else if(($nodeType==IVR_NODE)&&($nodeTimesegment!=0)) $icon=$relBasePath."public/img/xtree/foldericon_ts.png";
          else if(($nodeType==IVR_LEAF)&&($nodeTimesegment==0)) $icon=$relBasePath."public/img/xtree/file.png";
          else if(($nodeType==IVR_LEAF)&&($nodeTimesegment!=0)) $icon=$relBasePath."public/img/xtree/file_ts.png";
          else if(($nodeType==IVR_LINK)&&($nodeTimesegment==0)) $icon=$relBasePath."public/img/xtree/link.png";
          else if(($nodeType==IVR_LINK)&&($nodeTimesegment!=0)) $icon=$relBasePath."public/img/xtree/link_ts.png";
          $return.="  var ".$varName." = new WebFXTreeItem('".$key." ".$nodeName."');  \n";
          $return.="  ".$varName.".ivrId= '".$ivr."';  \n";
          $return.="  ".$varName.".nodeId= '".$nodeId."';  \n";
          $return.="  ".$varName.".nodeType= '".$nodeType."';  \n";
          $return.="  ".$varName.".icon = '".$icon."';  \n";
          $return.="  ".$varName.".openIcon = '".$icon."';  \n";
          $return.="  ".$varName.".action = \"javascript:location.href='./?ivr=".$ivr."&id=".$nodeId."';\";  \n";
          $return.="  ".$parentVar.".add(".$varName.");  \n"; 
        }
        // Node has children
        else{
          //select icon
          if(($nodeType==IVR_NODE)&&($nodeTimesegment==0)) $icon=$relBasePath."public/img/xtree/foldericon.png";
          else if(($nodeType==IVR_NODE)&&($nodeTimesegment!=0)) $icon=$relBasePath."public/img/xtree/foldericon_ts.png";
          else if(($nodeType==IVR_LEAF)&&($nodeTimesegment==0)) $icon=$relBasePath."public/img/xtree/file.png";
          else if(($nodeType==IVR_LEAF)&&($nodeTimesegment!=0)) $icon=$relBasePath."public/img/xtree/file_ts.png";
          else if(($nodeType==IVR_LINK)&&($nodeTimesegment==0)) $icon=$relBasePath."public/img/xtree/link.png";
          else if(($nodeType==IVR_LINK)&&($nodeTimesegment!=0)) $icon=$relBasePath."public/img/xtree/link_ts.png";
          $return.="  var ".$varName." = new WebFXTreeItem('".$key." ".$nodeName."');  \n";
          $return.="  ".$varName.".ivrId= '".$ivr."';  \n";
          $return.="  ".$varName.".nodeId= '".$nodeId."';  \n";
          $return.="  ".$varName.".nodeType= '".$nodeType."';  \n";
          $return.="  ".$varName.".icon = '".$icon."';  \n";
          $return.="  ".$varName.".openIcon = '".$icon."';  \n";
          $return.="  ".$varName.".action = \"javascript:location.href='./?ivr=".$ivr."&id=".$nodeId."';\";  \n";
          $return.="  ".$parentVar.".add(".$varName.");  \n"; 
          $return.=$subTree;
        }
      }
    }
    return $return;
  }
  $xajax = new xajax();
  $xajax->registerFunction("processFormData");
  $xajax->registerFunction("showRootPanel");
  $xajax->registerFunction("showNodePanel");
  $xajax->registerFunction("showLeafPanel");
  $xajax->registerFunction("showLinkPanel");
  $xajax->registerFunction("refreshAudioList");
  $xajax->registerFunction("makeAudioPanel");
  $xajax->registerFunction("renameAudio");
  $xajax->registerFunction("deleteAudio");
  $xajax->registerFunction("playAudio");
  $xajax->registerFunction("recordAudio");
  $xajax->registerFunction("makeTree");
  $xajax->processRequests();
  
  //==================== GET =====================
  if (count($_POST)>0) {
    $data=$_POST["node"];
    if ($data["beep"]=="")    $data["beep"]=0; 
    if ($data["irepeat"]=="") $data["irepeat"]=0; 
    if ($data["trepeat"]=="") $data["trepeat"]=0; 
    if ($data["itimes"]=="")  $data["itimes"]=0; 
    if ($data["ttimes"]=="")  $data["ttimes"]=0; 
    $entry=Array(
      "id"          => $data["id"], 
      "name"        => $data["name"], 
      "type"        => $data["type"],
      "parent"      => $data["parent"],
      "order"       => $data["order"],
      "key"         => $data["key"], 
      "maudio"      => $data["maudio"], 
      "waudio"      => $data["waudio"], 
      "beep"        => $data["beep"], 
      "macro"       => $_POST["macro"], 
      "link"        => $data["link"], 
      "timesegment" => $data["timesegment"], 
      "taudio"      => $data["taudio"], 
      "trepeat"     => $data["trepeat"], 
      "ttimes"      => $data["ttimes"], 
      "iaudio"      => $data["iaudio"],
      "irepeat"     => $data["irepeat"],
      "itimes"      => $data["itimes"],
      "tmacro"      => $_POST["macro_t"],
      "imacro"      => $_POST["macro_i"]
    );
    //merge the macro parameter
    $entry["parameter"]="";
    if(is_array($_POST["macro_param"])){
    	$p="";
      foreach ($_POST["macro_param"] as $k=>$v) $p.="$k=$v|";
      if(strlen($p)>1) $p=substr($p,0,-1);
      $entry["parameter"]=$p;
    }
    //merge the macro_t parameter
    $entry["tparameter"]="";
    if(is_array($_POST["macro_param_t"])){
    	$p="";
      foreach ($_POST["macro_param_t"] as $k=>$v) $p.="$k=$v|";
      if(strlen($p)>1) $p=substr($p,0,-1);
      $entry["tparameter"]=$p;
    }
    //merge the macro_i parameter
    $entry["iparameter"]="";
    if(is_array($_POST["macro_param_i"])){
    	$p="";
      foreach ($_POST["macro_param_i"] as $k=>$v) $p.="$k=$v|";
      if(strlen($p)>1) $p=substr($p,0,-1);
      $entry["iparameter"]=$p;
    }
    //submit node
    $node_id=$asterisk->putIvrNode($entry);
    if((isset($data["id"]))&&($data["id"]!="")){
      $goodMsg=T_("The node has been modified");
      $badMsg=T_("The node hasn't been modified");
    }else if((isset($_POST["ivr"]))&&($_POST["ivr"]!="")){
      $goodMsg=T_("The node has been created");
      $badMsg=T_("The node hasn't been created");
    }else{
      $goodMsg=T_("The IVR has been created");
      $badMsg=T_("The IVR hasn't been created");
    }
    if ($node_id>0) $alert="<div id=\"alert\" class=\"AlertGood\">".$goodMsg."</div>";
    else $alert="<div id=\"alert\" class=\"AlertBad\">".$badMsg."</div>";
    //redirect to right page
    $ivr=$_POST["ivr"];
    if($ivr!="") goToMainPage($alert,"./?ivr=".$_POST["ivr"]."&id=".$node_id);
    else goToMainPage($alert,"./?ivr=".$node_id."&id=".$node_id);
  }
  $alert=getAlert();
  
  //=================== GET ====================
  if((isset($_GET["ivr"]))&&($_GET["ivr"]!="")){
    if((isset($_GET["cmd"]))&&($_GET["cmd"]!="")){
      //delete node
      if(($_GET["cmd"]=="delNode")&&(isset($_GET["id"]))&&($_GET["id"]!="")){
        $ret=$asterisk->delIvrNode($_GET["id"]);
        if ($ret>0) $alert="<div id=\"alert\" class=\"AlertGood\">".T_("The node has been deleted")."</div>";
        else $alert="<div id=\"alert\" class=\"AlertBad\">".T_("The node hasn't deleted")."</div>";
        goToMainPage($alert,"./?ivr=".$_GET["ivr"]);
      }
    }
  }
  //================ PAGE LOGIC ================
  //NEW IVR
  if((!isset($_GET["ivr"]))||($_GET["ivr"]=="")){
    $title=T_("New IVR"); 
    $type=IVR_NODE;
    //Init script
    $initScript="<script> ";
    $initScript.=" xajax_showRootPanel(); "; 
    $initScript.=" xajax_refreshAudioList(document.getElementById('node[id]').value, ''); ";
    $initScript.=" xajax_makeAudioPanel(); ";
    $initScript.="</script>";
  }
  //EXISTING IVR
  else{
    $ivr=$_GET["ivr"];
    //EXISTING NODE
    if((!isset($_GET["parentid"]))||($_GET["parentid"]=="")){
      // If no node is selected, select root
      if((!isset($_GET["id"]))||($_GET["id"]=="")) $_GET["id"]=$_GET["ivr"];
      // Get selected node 
      $id=$_GET["id"];
      $ret=$asterisk->getIvrNode($id,0);
      if(is_array($ret)) $node=$ret[0]; else die("Error: Unknown Node $id");
      $name=$node["name"];
      $type=$node["type"];
      $parent=$node["parent"];
      $order=$node["order"];
      $key=$node["key"];
      $maudio=$node["maudio"];
      $waudio=$node["waudio"];
      $beep=$node["beep"];
      $macro=$node["macro"];
      $parameter=$node["parameter"];
      $link=$node["link"];
      $timesegment=$node["timesegment"];
      $taudio=$node["taudio"];
      $trepeat=$node["trepeat"];
      $ttimes=$node["ttimes"];
      $iaudio=$node["iaudio"];
      $irepeat=$node["irepeat"];
      $itimes=$node["itimes"];
      $tmacro=$node["tmacro"];
      $tparameter=$node["tparameter"];
      $imacro=$node["imacro"];
      $iparameter=$node["iparameter"];
      $ret=$asterisk->getIvrNode(0,$id);
      if((is_array($ret))&&(count($ret)>0))$hasChildren=true; else $hasChildren=false;
      $title=sprintf(T_("Edit node \"%1\$s\""),$name );
    }
    //NEW NODE
    else{
      $id=0;
      $parent=$_GET["parentid"];
      $type=IVR_LEAF;
      $title=T_("Create new node");
      //set some root settings
      $ret=$asterisk->getIvrNode($ivr,0);
      if(is_array($ret)){
        $node=$ret[0];
        $beep=$node["beep"];
        $trepeat=$node["trepeat"];
        $ttimes=$node["ttimes"];
        $irepeat=$node["irepeat"];
        $itimes=$node["itimes"];
      }
    }
    //Init script
    $initScript="<script> ";
    if(($type==IVR_NODE)&&($parent==0)) $initScript.=" xajax_showRootPanel(); ";
    else if(($type==IVR_NODE)&&($parent!=0)) $initScript.=" xajax_showNodePanel(); "; 
    else if($type==IVR_LINK) $initScript.=" xajax_showLinkPanel(); "; 
    else $initScript.=" xajax_showLeafPanel(); ";
    $initScript.=" xajax_makeTree('".$ivr."'); ";
    $initScript.=" xajax_refreshAudioList(document.getElementById('node[id]').value, ''); ";
    $initScript.=" xajax_makeAudioPanel(); ";
    $initScript.="</script>";
  }
  // Get the time segments available
  $tsOptList.="<option value=\"0\">[any time]</option>\n";
  $ret=$asterisk->getTimeSegment(0);
  if (is_array($ret)){
    foreach ($ret as $ts) {
      $tsid=$ts["id"];
      $tsname=$ts["name"];
      if ($tsid==$timesegment) $selected=" selected=\"selected\" "; else $selected="";
      $tsOptList.="<option value=\"$tsid\" $selected>$tsname</option>\n";
    }
  }
  //retrieve the IVR nodes for changing node position
  if($parent!=0){
    $nodeArray=$asterisk->getIvrNode(0,$parent);
    if(is_array($nodeArray)){
      foreach($nodeArray as $v){
        if ($v["id"]!=$id) $nodesOnSameLevel.="<option value=\"".$v["order"]."\">".T_("Replace")." ".$v["name"]."</option>\n";
        else $currentLevel.="<option value=\"".$v["order"]."\" selected=\"selected\">[".T_("current position")."]</option>\n";
      }
      if($currentLevel=="") $currentLevel="<option value=\"0\" selected=\"selected\">[".T_("last position")."]</option>\n"; 
      $nodesOnSameLevel=$currentLevel.$nodesOnSameLevel;
    }
  }
  //get Ivr nodes to jump to
  if($ivr!=0){
    $linkList="<option value=\"\">[none]</option>";
    $nodeArray=$asterisk->getChildNodes($ivr);
    if(is_array($nodeArray)){
      foreach($nodeArray as $v){
        if($v["type"]==IVR_NODE){
          if($v["id"]==$link) $select="selected=\"selected\""; else $select="";
          $linkList.="<option value=\"".$v["id"]."\" $select>".$v["name"]."</option>\n";
        }
      }
    }
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/");
  ?>
    <!-- Title -->
    <div class="SectionHeader">
      <h1><?=$title?></h1>
      <h3><a href="../" class="admin" ><?=T_("Return without save")?></a></h3>
    </div>
    <div id="alert">
      <?=$alert?>
    </div>
    <!-- Form -->
    <div id="basic_form">
      <form id="mainform" action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
        <!-- GENERAL INFORMATIONS -->
        <h2><?=T_("General information")?></h2>
        <p class="blockintro"><?=T_("Node selection properties")?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="node[name]"><?=T_("Name")?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="node[name]" name="node[name]" value="<?=$name?>" /> <span id="nameErr"></span><span class="help"><?=T_("Type a name")?></span></dd>
            <? if($parent!=0) { ?>
              <dt class="required"><label for="node[key]"><?=T_("Key")?>:</label></dt>
              <dd><input type="text" size="25" maxlength="11" id="node[key]" name="node[key]" value="<?=$key?>" /> <span id="keyErr"></span><span class="help"><?=T_("Insert one or more digits (i.e. 1, 99)")?></span></dd>
              <dt class="required"><label for="node[order]"><?=T_("Position")?>:</label></dt>
              <dd><select id="node[order]" name="node[order]"><?=$nodesOnSameLevel?></select></dd>
              <dt class="required"><label for="node[timesegment]"><?=T_("Time Segment")?>:</label></dt>
              <dd><select id="node[timesegment]" name="node[timesegment]"><?=$tsOptList?></select></dd>
            <? } ?>  
            <dt class="required"><label for="node[type]"><?=T_("Type")?>:</label></dt>
            <dd>
              <table style="width: 17em;" cellpadding="0" cellspacing="0">
                <tr>
                  <td style="align: right; width: 1%;"><input type="radio" id="node[type]" name="node[type]" value="<?=IVR_NODE?>" onclick=" xajax_showNodePanel();" <? if($type==IVR_NODE) echo "checked=\"checked\"" ?> /></td>
                  <td style="align: left;"><span style="color: #000000;"><?=T_("Menu")?></span></td>
                  <td style="align: right; width: 1%;"><input type="radio" id="node[type]" name="node[type]" value="<?=IVR_LEAF?>" onclick=" xajax_showLeafPanel();" <? if($type==IVR_LEAF) echo "checked=\"checked\"" ?> <? if(($parent==0)||($hasChildren==true)) echo "disabled=\"disabled\"" ?> /></td>
                  <td style="align: left;"><span style="color: #000000;"><?=T_("Action")?></span></td>
                  <td style="align: right; width: 1%;"><input type="radio" id="node[type]" name="node[type]" value="<?=IVR_LINK?>" onclick=" xajax_showLinkPanel();" <? if($type==IVR_LINK) echo "checked=\"checked\"" ?> <? if(($parent==0)||($hasChildren==true)) echo "disabled=\"disabled\"" ?> /></td>
                  <td style="align: left;"><span style="color: #000000;"><?=T_("Link")?></span></td>
                </tr>
              </table>
            </dd>
            </tr>
          </table>
        </div>
        <!-- AUDIO SECTION -->
        <h2><?=T_("Audio Message")?></h2>
        <p class="blockintro"><?=T_("Choose messages to play")?></p>
        <div class="block">
          <!-- Menu Message -->
          <div id="div_menu" style="display: none;">
            <dl>
              <dt class="required"><label for="node[maudio]"><?=T_("Menu")?>:</label></dt>
              <dd><select id="node[maudio]" name="node[maudio]"><?=$mAudioList?></select> <span class="help"><?=T_("Audio to play in the menu which includes this item")?></span></dd>
            </dl>    
          </div> 
          <!-- Wellcome Message -->
          <div id="div_audio" style="display: none;">
            <dl>
              <dt class="required"><label for="node[waudio]"><?=T_("Welcome")?>:</label></dt>
              <dd><select id="node[waudio]" name="node[waudio]"><?=$wAudioList?></select> <span class="help"><?=T_("Audio to play when entering this item")?></span></dd>
            </dl>
            <div id="div_beep" style="display: none;">
              <dl>     
                <dt class="required"><label for="node[irepeat]"></label></td>
                <dd>&nbsp;&nbsp;<input type="checkbox" id="node[beep]" name="node[beep]" value="1" <?if ($beep==1) echo "checked=\"checked\"" ?> />&nbsp;&nbsp;&nbsp;<?=T_("If checked, play a beep before user's choice")?></span></dd>
              </dl>
            </div>
          </div>
          <!-- Timeout Message -->
          <div id="div_timeout" style="display: none;">
          	<hr/>
            <dl>
              <dt class="required"><label for="node[taudio]"><?=T_("Timeout")?>:</label></dt>
              <dd><select id="node[taudio]" name="node[taudio]"><?=$tAudioList?></select> <span class="help"><?=T_("Audio to play on timeout")?></span></dd>
              <dt class="required"><label for="node[trepeat]"></label></td>
              <dd>&nbsp;&nbsp;<input type="checkbox" id="node[trepeat]" name="node[trepeat]" value="1" onclick="if(this.checked) document.getElementById('node[ttimes]').disabled=''; else document.getElementById('node[ttimes]').disabled='disabled';" <?if ($trepeat==1) echo "checked=\"checked\"" ?>/>&nbsp;&nbsp;&nbsp;<span class="help"><?=T_("If checked, the menu will be repeated on timeout")?></span></dd>
              <dt class="required"><label for="node[ttimes]"></label></td>
              <dd><input type="text" id="node[ttimes]" name="node[ttimes]" size="2" maxlength="2" value="<?=$ttimes?>" <?if ($trepeat!=1) echo "disabled=\"disabled\"" ?> /> <?=T_("Times to repeat menu on timeout (blank or 0 to loop endlessly)")?></span></dd>
            </dl>
            <div id="div_macro_t" style="margin: 0 0 20px 110px;">
          		<? echo listMacro($js, $tmacro, $tparameter, true, "_t"); ?>
        		</div>
          </div> 
          <!-- Invalid Message -->
          <div id="div_invalid" style="display: none;">
          	<hr/>
            <dl>
              <dt class="required"><label for="node[iaudio]"><?=T_("Invalid Choice")?>:</label></dt>
              <dd><select id="node[iaudio]" name="node[iaudio]"><?=$iAudioList?></select> <span class="help"><?=T_("Audio to play on invalid choice")?></span></dd>
              <dt class="required"><label for="node[irepeat]"></label></td>
              <dd>&nbsp;&nbsp;<input type="checkbox" id="node[irepeat]" name="node[irepeat]" value="1" onclick="if(this.checked) document.getElementById('node[itimes]').disabled=''; else document.getElementById('node[itimes]').disabled='disabled';" <?if ($irepeat==1) echo "checked=\"checked\"" ?> />&nbsp;&nbsp;&nbsp;<span class="help"><?=T_("If checked, the menu will be repeated on invalid choice")?></span></dd>
              <dt class="required"><label for="node[itimes]"></label></td>
              <dd><input type="text" id="node[itimes]" name="node[itimes]" size="2" maxlength="2" value="<?=$itimes?>" <?if ($irepeat!=1) echo "disabled=\"disabled\"" ?>  /> <?=T_("Times to repeat menu on invalid choice (blank or 0 to loop endlessly)")?></span></dd>
            </dl>
            <div id="div_macro_i" style="margin: 0 0 20px 110px;">
          		<? echo listMacro($js, $imacro, $iparameter, true, "_i"); ?>
        		</div>
          </div>    
        </div>
        <!-- MACRO -->
        <div id="div_macro" style="display: none;">
          <? echo listMacro($js, $macro, $parameter); ?>
        </div>
        <!-- LINK -->
        <div id="div_link" style="display: none;">
          <h2><?=T_("Link")?></h2>
          <p class="blockintro"><?=T_("Choose the node to jump to")?></p>
          <div class="block">
            <dt class="required"><label for="node[link]"><?=T_("Link to")?>:</label></dt>
            <dd><select id="node[link]" name="node[link]"><?=$linkList?></select>&nbsp;&nbsp;<span id="linkErr"></span><span class="help"><?=T_("Jump to the selected node")?></span></dd>
          </div>
        </div>
        <!-- ACTIONS -->
        <div class="action">
          <input type="hidden" name="ivr" value="<?=$ivr?>" />
          <input type="hidden" id="node[id]" name="node[id]" value="<?=$id?>" />
          <input type="hidden" name="node[parent]" value="<?=$parent?>" />
          <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/></a>
          <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick=" document.getElementById('alert').innerHTML=''; xajax_processFormData(xajax.getFormValues('mainform')); "/></a>
        <!--
          <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='index.php?choosedIvr=<?=$_REQUEST["choosedIvr"]?>'; return false;"/>
          <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
         -->
        </div>
      </form>
      <!-- Init Script -->
      <?=$initScript?>
    </div>  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page allows you to create a new IVR or to modify an existing one. Every IVR is a tree composed of nodes. Every node has a name, a type and a numeric key. It's possible to operate on nodes selecting one on the tree below or add new nodes useing the button on the right of the menu nodes. For each node it's possible to define parameters depending on the node's type. To save the node press save button on the end of the page.")?></p>
    <? if((!isset($_GET["ivr"]))||($_GET["ivr"]=="")){ ?>
      <!-- New IVR -->
    <? } ?>
    <? if((isset($_GET["ivr"]))&&($_GET["ivr"]!="")){ ?>
      <!-- Tree --> 
      <h1><?=T_("IVR Tree")?></h1><br />
      <div id="tree">
        <table>
          <tr>
            <td><img src="<?=$relBasePath?>public/img/ico-progress.gif" alt="loading" /></td>
            <td><p><?=T_(" loading... ")?></p></td>
          </tr>
        </table>
      </div>
      <br />
    <? } ?>
    <!-- Audio Recorder -->
      <h1><?=T_("Audio Recorder")?></h1>
      <div id="audioRecorder">
        <table>
          <tr>
            <td><img src="<?=$relBasePath?>public/img/ico-progress.gif" alt="loading" /></td>
            <td><p><?=T_(" loading... ")?></p></td>
          </tr>
        </table>
      </div>
      <p><?=T_("Rename, delete or listen to an audio file or record a new one")?></p>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();
  $smarty->assign("section", "ivr");
  $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');
?>

