<?xml version="1.0" encoding="UTF-8"?>

<ccxml xmlns="http://www.w3.org/2002/09/ccxml" version="1.0">
	<var name="legAID"/>
	<var name="outboundID"/>
	<var name="welcomeDlg"/>
	<var name="DestURI"/>
	<var name="CallingURI"/>
	<var name="Balance_dollars_legA"/>
	<var name="Balance_cents_legA" />
	<var name="Balance_dollars_legB"/>
	<var name="Balance_cents_legB" />
	<var name="UserPin" />
	<var name="CallTimeout_legA" />
	<var name="UnitTimeout_legA" expr="'0'"/>
	<var name="CallTimeout_legB" />
	<var name="UnitTimeout_legB" expr="'0'"/> 
	<var name="CallTimeout_Mins_legA" />
	<var name="CallTimeout_Secs_legA" />
	<var name="CallTimeout_Mins_legB" />
	<var name="CallTimeout_Secs_legB" />
	<var name="talkTime" expr="0"/>
	<var name="initState" expr="'start'" />
	<var name="AuthenType" expr="'ANI'" />
	<var name="isSessionActive" expr="'0'" />
	<var name="isSessionActive_legA" expr="0" />
	<var name="isSessionActive_legB" expr="0" />
	<var name="callattempts" expr="0" />


<eventprocessor statevariable="initState">
    <transition state="start" event="connection.alerting" name="event$">
        <assign name="legAID" expr="event$.connectionid" />
        <assign name="initState" expr="'alerting'"/>
        <assign name="CallingURI" expr="event$.remote"/>
        <assign name="AccessNumber" expr="event$.local"/>
        <assign name="SIP_CallID" expr="event$.sipcallid"/>
        <reject connectionid="legAID" />
    </transition>

    <transition state="alerting" event="connection.disconnected" name="event$">
        <assign name="initState" expr="'authenticate_leg_a'"/>
	    <if cond="CallingURI == ''">
            <assign name="initState" expr="'exit'"/>
            <exit/>
        <else/>
	<translatenum fromnumber="CallingURI" tonumber="CallingURI" numbertobemodified="CallingURI" modifiednumber="CallingURI" translatefor="authentication"/>
            <authenticate authtype="ANI" UserName="CallingURI" CallingStationId="CallingURI" ServiceID="Audio" AccessNumber="AccessNumber" CallID="SIP_CallID"/>
        </if>
    </transition>

    <transition state="authenticate_leg_a" event="authenticate.error" name="event$">
        <assign name="initState" expr="'exit'"/> 
        <exit/>
    </transition>

    <transition state="authenticate_leg_a" event="authenticate.timeout" name="event$">
        <assign name="initState" expr="'exit'"/> 
        <exit/>
    </transition>

    <transition state="authenticate_leg_a" event="authenticate.success" name="event$">

        <assign name="initState" expr="'authorize_leg_a'"/>
        <assign name="Balance_dollars_legA" expr="event$.creditamount_dollars" />
        <assign name="Balance_cents_legA" expr="event$.creditamount_cents" />
        <authorize SessionID="legAAuthID" authtype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="CallingURI" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" UnitReservation="yes"/>

    </transition>

    <transition state="authorize_leg_a" event="authorize.success" name="event$">	
        <assign name="initState" expr="'leg_a_inprogress'"/>
	<assign name="isSessionActive_legA" expr="1"/>
	<assign name="CallTimeout_legA" expr="event$.auth_totaltimeout"/>
	<assign name="UnitTimeout_legA" expr="event$.auth_UnitTime"/>
	<assign name="CallTimeout_Mins_legA" expr="event$.auth_timeout_minutes"/>
	<assign name="CallTimeout_Secs_legA" expr="event$.auth_timeout_seconds"/>
	<assign name="TempCLI" expr="'900'"/>
	<createcall dest="CallingURI" callerid="AccessNumber" connectionid="legAID" AuthSessionID="legAAuthID" callidsuffix="legAAuthID" timeout="'30s'" connectedtimelimit="UnitTimeout_legA" />v
    </transition>


    <transition state="leg_a_inprogress" event="connection.connected" name="event$">
	  <assign name="initState" expr="'leg_a_connected'"/>
	  <accountstart SessionID="legAAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="CallingURI" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" />
	  <assign name="callattempts" expr="callattempts+1" />
	  <dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition>

    <transition state="leg_a_inprogress" event="connection.failed" name="event$">
	    <assign name="initState" expr="'leg_a_failed'" />

	    <if cond="CallingURI == ''">
		    <exit/>
	   <else/>
	   <accountstop SessionID="legAAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="AccessNumber" AcctSessionTime="talkTime" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" />
	    </if>

    </transition>
    
    <transition state="leg_a_failed" event="account.stop.success" name="event$">
	    <assign name="initState" expr="'exit'" />
	    <exit/>
    </transition>

    <transition state="leg_a_connected" event="dialog.exit" name="event$">
        <assign name="DestURI" expr="event$.digitscollected"/>
        <if cond="DestURI == ''">
	    <if cond="callattempts == 3">
		<disconnect connectionid="legAID"/>
            <else/>
            	<assign name="initState" expr="'dest_no_error'"/>
            	<dialogstart src="'NoDigitsError.mscml'" connectionid="legAID" />
	    </if>
        <else/>
            <assign name="initState" expr="'authorize_leg_b'"/>
	     <translatenum fromnumber="CallingURI" tonumber="DestURI" numbertobemodified="DestURI" modifiednumber="DestURI" translatefor="authorization"/>
            <authorize SessionID="legBAuthID" authtype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="DestURI" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" UnitReservation="yes"/>
        </if>
    </transition>

    <transition state="dest_no_error" event="dialog.exit" name="event$">
        <assign name="initState" expr="'leg_a_connected'"/>
	<assign name="callattempts" expr="callattempts+1" />
        <dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition>

    <transition state="authorize_leg_b" event="authorize.success" name="event$">	
	<assign name="initState" expr="'leg_b_inprogress'"/>
	<assign name="isSessionActive_legB" expr="1"/>
	<assign name="CallTimeout_legB" expr="event$.auth_totaltimeout"/>
	<assign name="UnitTimeout_legB" expr="event$.auth_UnitTime"/>
	<assign name="CallTimeout_Mins_legB" expr="event$.auth_timeout_minutes"/>
	<assign name="CallTimeout_Secs_legB" expr="event$.auth_timeout_seconds"/>
	<createcall dest="DestURI" callerid="AccessNumber" connectionid="outboundID" AuthSessionID="legBAuthID" callidsuffix="legBAuthID" timeout="'30s'" connectedtimelimit="UnitTimeout_legB" />
	
    </transition>

    <transition state="authorize_leg_b" event="authorize.timeout" name="event$">
        <assign name="initState" expr="'leg_a_connected'"/>
	<assign name="callattempts" expr="0" />
        <dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition> 

    <transition state="authorize_leg_b" event="authorize.error" name="event$">
        <assign name="initState" expr="'leg_a_connected'"/>
	<assign name="callattempts" expr="0" />
        <dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition>


    <transition state="authorization_leg_b_failed" event="dialog.exit" name="event$">
	<assign name="initState" expr="'leg_a_connected'"/>
	<assign name="callattempts" expr="0" />
	<dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition>

    <transition state="leg_a_connected" event="error.dialog" name="event$">
        <disconnect connectionid="legAID"/>
    </transition>

    <transition state="leg_b_inprogress" event="connection.connected" name="event$">
	    <assign name="initState" expr="'join'"/>
	    <join id1="legAID" id2="outboundID" duplex="'full'"/>
    </transition>
    
    <transition state="leg_b_inprogress" event="connection.failed" name="event$">
	<assign name="initState" expr="'leg_b_failed'"/>
        <assign name="talkTime" expr="0"/>
        <accountstop SessionID="legBAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="DestURI" AcctSessionTime="talkTime" AccessNumber="AccessNumber" ServiceID="'Audio'" CallID="SIP_CallID" Reason="event$.reason"/>
        <assign name="isSessionActive_legB" expr="0"/>
        <dialogstart src="'outgoing_call_failure.mscml'" connectionid="legAID" />
    </transition>


    <transition state="leg_b_failed" event="dialog.exit" name="event$">
	    <assign name="initState" expr="'leg_a_connected'"/>
	    <assign name="callattempts" expr="0" />
	    <dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition>

    <transition state="join" event="conference.joined"  name="event$">
        <assign name="initState" expr="'join_success'"/>
	<accountstart SessionID="legBAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="DestURI" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" />

    </transition>

    <transition state="join_success" event="account.start.error" name="event$">

    </transition>

    <transition state="join" event="error.conference.joined"  name="event$">

	<assign name="initState" expr="'join_error'" />
        <disconnect connectionid="outboundID"/>
    </transition>


    <transition state="join_success" event="connection.disconnected" name="event$">
        <assign name="disconnectedID" expr="event$.connectionid" />
        <assign name="connectedTime" expr="event$.connectedtime" />
        <assign name="initState" expr="'legDisconnected'" />
    </transition>

    <transition state="legDisconnected" event="conference.unjoined" name="event$">
        <assign name="initState" expr="'unjoined'" />
        <assign name="talkTime" expr="event$.jointime" />
        <if cond="disconnectedID == outboundID">
            <accountstop SessionID="legBAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="DestURI" AcctSessionTime="talkTime" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" Reason="Sucess"/>
            <assign name="isSessionActive_legB" expr="0"/>

        <else/>
            <disconnect connectionid="outboundID"/>
        </if>

    </transition>

    <transition state="unjoined" event="connection.disconnected" name="event$">
        <assign name="initState" expr="'legBAccounting'" />
        <if cond="isSessionActive_legB == 1">
            <accountstop SessionID="legBAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="CallingURI" AcctSessionTime="talkTime" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" Reason="'Success'"/>
            <assign name="isSessionActive_legB" expr="0"/>
        </if>


    </transition>

    <transition state="legBAccounting" event="account.stop.success" name="event$">
        <assign name="initState" expr="'exit'" />
        <if cond="isSessionActive_legA == 1">
            <accountstop SessionID="legAAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="CallingURI" AcctSessionTime="connectedTime" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" Reason="'Success'"/>
            <assign name="isSessionActive_legA" expr="0"/>
        </if>
        <exit/>
    </transition>

    <transition state="authorize_leg_b  leg_a_connected leg_b_inprogress dest_no_error leg_b_failed authorize_leg_b_failed" event="connection.disconnected" name="event$">
        <assign name="initState" expr="'exit'" />
        <assign name="connectedTime" expr="event$.connectedtime" />
        <if cond="isSessionActive_legA == 1">
            <accountstop SessionID="legAAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="CallingURI" AcctSessionTime="connectedTime" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" Reason="'Success'"/>
            <assign name="isSessionActive_legA" expr="0"/>
        <else/>
            <exit/>
        </if>
    </transition>

    <transition state="exit" event="account.stop.success" name="event$">
        <exit/>
    </transition>

    <transition state="unjoined" event="account.stop.success" name="event$">
	<assign name="initState" expr="'leg_a_connected'"/>
        <assign name="talkTime" expr="0"/>
	<assign name="callattempts" expr="0" />
        <dialogstart src="'get_dest.mscml'" connectionid="legAID" />
    </transition>

    <transition state="join" event="connection.disconnected" name="event$">

        <assign name="initState" expr="'unjoined'" />
       	<assign name="talkTime" expr="0" />
        <assign name="disconnectedID" expr="event$.connectionid" />
        <if cond="disconnectedID == outboundID">
            <accountstop SessionID="legBAuthID" accttype="ANI" UserName="CallingURI" CallingStationId="CallingURI" CalledStationId="DestURI" AcctSessionTime="talkTime" AccessNumber="AccessNumber" ServiceID="Audio" CallID="SIP_CallID" Reason="Sucess"/>
            <assign name="isSessionActive_legB" expr="0"/>
       <else/>
            <disconnect connectionid="outboundID"/>
        </if>
    </transition>

  </eventprocessor>
</ccxml> 

