<?xml version="1.0"?>
<vxml version="2.0" application="../vm_vxml/root.vxml">
  <!-- Auto-Attendant Template -->
  <!-- This template is used by Configure Server to generate
       a run-time auto-attendant menu -->

  <!-- Autoattendant main menu -->

  <!-- This variable is common to both the forms.
       It is filled when the user selects the auto attendant 
       option that should result in a transfer. This variable is
       filled with the destination extension.
       This is used by the form 'transferform'. -->
  <var name="transferextn"/>

  <form id="aa">
#set( $dtmf = $attendant.Settings.getSetting('dtmf'))
    <property name="interdigittimeout" value="${dtmf.getSetting('interDigitTimeout').Value}s" />
    <property name="timeout" value="${dtmf.getSetting('overallDigitTimeout').Value}s" />

    <!-- Parameters passed in from the calling subroutine -->
    <var name="from" />
    <var name="mediaserverurl" />
    <var name="securemediaserverurl" />
    <var name="action" />
    <var name="extension" />
    <var name="mailbox" />
    <var name="aa_menu_name" />
    <var name="systemgreetingurl" />
    <var name="autoattendantprompturl" />
    <var name="dial_attempts" expr="0" />
#set( $onfail = $attendant.Settings.getSetting('onfail'))
    <var name="failedoption" expr="${onfail.getSetting('transfer').Value}" />

    <!-- Play the autoattendant main menu -->
    <field name="menu" type="digits?minlength=1;maxlength=${dtmf.getSetting('maxDigits').Value}">
      <prompt>
	<audio expr="securemediaserverurl + aa_promptsalias + '${attendant.Prompt}'" />
      </prompt>
      <filled>
#foreach( $dialPadKey in ${attendant.MenuItems.keySet()} )
#if ( $velocityCount == 1 )
	<if cond="menu=='${dialPadKey.Name}'">
#else	
	<elseif cond="menu=='${dialPadKey.Name}'"/>
#end	
#set( $menuItem = ${attendant.MenuItems.get($dialPadKey)} )
	  <assign name="operation" expr="'${menuItem.Action.Name}'" />
	  <assign name="extension" expr="'$menuItem.Action.vxmlParameter($menuItem.Parameter)'" />
	  <goto nextitem="menuaction" />
#end	  
	<else />
	  <!-- Here is the transfer to a given extension -->
	  <assign name="operation" expr="'transfer_to_extension'" />
	  <assign name="extension" expr="menu" />
	  <goto nextitem="menuaction"/>
	</if>
      </filled>
      <noinput count="1">
	<reprompt/>
      </noinput>
      <noinput count="2">
	<reprompt/>
      </noinput>
      <noinput count="3">
        <goto nextitem="giveup" />
      </noinput>
    </field>

    <block name="menuaction">
      <if cond="operation == 'dial_by_name'">
	<goto nextitem="dialbyname"/>
      <elseif cond="operation == 'transfer_to_another_aa_menu'" />
	<assign name="aa_menu_name" expr="extension" />
	<goto nextitem="another_aa_menu" />
      <elseif cond="operation == 'voicemail_access'" />
	<assign name="action" expr="'retrieve'" />
	<goto nextitem="retrieve" />
      <elseif cond="operation == 'voicemail_deposit'" />
	<assign name="action" expr="'deposit'" />
	<assign name="mailbox" expr="extension" />
	<goto nextitem="deposit" />
      <elseif cond="operation == 'transfer_to_extension'" />
	<assign name="action" expr="'transfer'" />
	<prompt bargein="false">
	  <audio expr="mediaserverurl + promptsalias + 'please_hold.wav'" />
	</prompt>
	<goto nextitem="transfertoextn" />
      <elseif cond="operation == 'operator'" />
	<prompt bargein="false">
	  <audio expr="mediaserverurl + promptsalias + 'please_hold.wav'" />
	</prompt>
	<goto next="root.vxml#operator" />
      <elseif cond="operation == 'disconnect'" />
	<prompt bargein="false">
	  <audio expr="mediaserverurl + promptsalias + 'thankyou_goodbye.wav'" />
	</prompt>
	<disconnect />
      <elseif cond="operation == 'repeat_prompt'" />
	<clear namelist="menu" />
	<goto nextitem="menu" />
      <else/>
	<prompt>
	  <audio expr="mediaserverurl + promptsalias + 'invalid_entry_try_again.wav'" />
	</prompt>
	<clear namelist="menu" />
	<goto nextitem="menu" />
      </if>
    </block>

    <subdialog name="transfertoextn" cond="operation == 'transfer_to_extension'" srcexpr="securemediaserverurl + cgiurl" namelist="action extension">
      <filled>
	<if cond="transfertoextn.result=='invalidextn'">
	  <if cond="failedoption == '2'">
	    <!-- We were transfering due to failure, and that failed!  -->
	    <!-- Probably the provisioned "transfer-extension" is not real -->
	    <goto nextitem="giveup" />
	  </if>
	  <prompt>
	    <audio expr="mediaserverurl + promptsalias + 'extn_invalid.wav'" />
	  </prompt>
	  <goto nextitem="invalidoptions" />
	<elseif cond="transfertoextn.result=='failed'"/>
	  <if cond="failedoption == '2'">
	    <!-- We were transfering due to failure, and that failed!  -->
	    <goto nextitem="giveup" />
	  </if>
	  <prompt>
	    <audio expr="mediaserverurl + promptsalias + 'system_error.wav'" />
	  </prompt>
	  <goto nextitem="invalidoptions" />
	<else />
	  <return />
	</if>
      </filled>
    </subdialog>

    <subdialog name="dialbyname" cond="operation == 'dial_by_name'" srcexpr="securemediaserverurl + dialbynameurl">
      <param name="from" expr="from"/>
      <param name="mediaserverurl" expr="mediaserverurl"/>
      <param name="securemediaserverurl" expr="securemediaserverurl" />
      <param name="fromdeposit" expr="'no'" />
      <filled>
	<if cond="dialbyname.result=='success'">
	  <clear namelist="menu" />
	  <goto nextitem="menu" />
	<elseif cond="dialbyname.result=='failed'"/>
	  <goto nextitem="dialbyname"/>
	<elseif cond="dialbyname.result=='quit'"/>
	  <clear namelist="menu" />
	  <goto nextitem="menu"/>
	</if>
      </filled>
    </subdialog>

    <subdialog name="another_aa_menu" cond="operation == 'transfer_to_another_aa_menu'" srcexpr="securemediaserverurl + aa_alias + 'autoattendant-' + aa_menu_name + '.vxml'">
      <param name="from" expr="from"/>
      <param name="mediaserverurl" expr="mediaserverurl"/>
      <param name="securemediaserverurl" expr="securemediaserverurl" />
      <filled>
	<if cond="another_aa_menu.result=='success'">
	  <clear namelist="menu" />
	  <goto nextitem="menu" />
	<elseif cond="another_aa_menu.result=='failed'"/>
	  <goto nextitem="another_aa_menu"/>
	<elseif cond="another_aa_menu.result=='quit'"/>
	  <clear namelist="menu" />
	  <goto nextitem="menu"/>
	</if>
      </filled>
    </subdialog>

    <subdialog name="retrieve" cond="operation == 'voicemail_access'" srcexpr="securemediaserverurl + cgiurl" namelist="action from">
      <filled>
	<return />
      </filled>
    </subdialog>

    <subdialog name="deposit" cond="operation == 'voicemail_deposit'" srcexpr="securemediaserverurl + cgiurl" namelist="action from mailbox">
      <filled>
	<return />
      </filled>
    </subdialog>

    <block name="invalidoptions">
      <if cond="dial_attempts == '2'">
        <goto nextitem="giveup" />
      <else/>
        <assign name="dial_attempts" expr="dial_attempts + 1" />
        <clear namelist="menu" />
        <goto nextitem="menu" />
      </if>
    </block>

    <block name="giveup">
      <!-- Time to give up on this call.  Behavior depends on value of 
           failedoption.
             0 means say goodbye and exit.
             1 means try to transfer to the assigned extension
             2 means we did #1, and that failed!                         -->
      <if cond="failedoption == '0'">
        <prompt bargein="false">
          <audio expr="mediaserverurl + promptsalias + 'thankyou_goodbye.wav'" />
        </prompt>
        <disconnect />
      </if>

      <if cond="failedoption == '1'">
        <prompt bargein="false">
          <audio expr="mediaserverurl + promptsalias + 'please_hold.wav'" />
        </prompt>
        <!-- Flag we are transfering due to giving up, so we don't get 
              back here on failure! -->
        <assign name="failedoption" expr="'2'" />
        <assign name="operation" expr="'transfer_to_extension'" />
        <assign name="action" expr="'transfer'" />
        <assign name="extension" expr="'$!{onfail.getSetting('transfer-extension').Value}'" />
        <goto nextitem="transfertoextn" />
      </if>

      <if cond="failedoption == '2'">
        <!-- We were transfering due to failure, and that failed!  -->
        <prompt bargein="false">           
          <audio expr="mediaserverurl + promptsalias + 'system_error_short.wav'" />
          <audio expr="mediaserverurl + promptsalias + 'thankyou_goodbye.wav'" />
        </prompt>
        <disconnect/>
      </if>
    
    </block>
    
  </form>

  <form id="transferform">
    <transfer name="extntranfer" destexpr="transferextn" />
  </form>
</vxml>
