// XApic.cpp : implementation file
//

#include "stdafx.h"
#include "XApi.h"
#include "XApic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// XApic dialog

XApic::XApic(CWnd* pParent /*=NULL*/)
	: CDialog(XApic::IDD, pParent)
{
    //{{AFX_DATA_INIT(XApic)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT

    handle = 0;
    memset (&LastState, 0, sizeof (LastState));
    BdpAccessCode = 0xffffffff;
    BdpStatus = 0;
}

void XApic::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(XApic)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    DDX_Control(pDX, IDC_LIST_DIGITS, m_digits);
    DDX_Control(pDX, IDC_EDIT_CHARGE_PULSES, m_charge_pulses);
    DDX_Control(pDX, IDC_BUTTON_CALL_MAINTAIN, m_call_maintain);
    DDX_Control(pDX, IDC_BUTTON_KEY1, m_key1);
    DDX_Control(pDX, IDC_BUTTON_KEY0, m_key0);
    DDX_Control(pDX, IDC_BUTTON_KEY2, m_key2);
    DDX_Control(pDX, IDC_BUTTON_KEY3, m_key3);
    DDX_Control(pDX, IDC_BUTTON_KEY4, m_key4);
    DDX_Control(pDX, IDC_BUTTON_KEY5, m_key5);
    DDX_Control(pDX, IDC_BUTTON_KEY6, m_key6);
    DDX_Control(pDX, IDC_BUTTON_KEY7, m_key7);
    DDX_Control(pDX, IDC_BUTTON_KEY8, m_key8);
    DDX_Control(pDX, IDC_BUTTON_KEY9, m_key9);
    DDX_Control(pDX, IDC_BUTTON_KEYSQUARE, m_keysquare);
    DDX_Control(pDX, IDC_BUTTON_KEYSTAR, m_keystar);
    DDX_Control(pDX, IDC_LIST_CALLERID, m_callerid);
    DDX_Control(pDX, IDC_EDIT_NOTIFICATION, m_notification);
    DDX_Control(pDX, IDC_EDIT_PENDING_CALLS, m_pending_calls);
    DDX_Control(pDX, IDC_LIST_TRANSFERRED_CALLS, m_transferred_calls);
    DDX_Control(pDX, IDC_BUTTON_REGISTER_BDP, m_register_bdp);
    DDX_Control(pDX, IDC_EDIT_BDP, m_bdp);
    DDX_Control(pDX, IDC_EDIT_BDP_STATUS, m_bdp_status);
    DDX_Control(pDX, IDC_LIST_SUSPENDED_CALLS, m_suspended_calls);
    DDX_Control(pDX, IDC_BUTTON_DEFLECT, m_deflect);
    DDX_Control(pDX, IDC_EDIT_DEFLECT, m_edit_deflect);
    DDX_Control(pDX, IDC_BUTTON_INTRUDE_BDP, m_intrude_bdp);
    DDX_Control(pDX, IDC_BUTTON_ANSWER_BDP, m_answer_bdp);
    DDX_Control(pDX, IDC_BUTTON_HOLD_RETRIEVE, m_hold_etrieve);
    DDX_Control(pDX, IDC_BUTTON_SUSPEND, m_suspend);
    DDX_Control(pDX, IDC_BUTTON_CONFERENCE, m_conference);
    DDX_Control(pDX, IDC_BUTTON_TRANSFER, m_transfer);
    DDX_Control(pDX, IDC_BUTTON_RECORD, m_record);
    DDX_Control(pDX, IDC_BUTTON_MAILBOX, m_mailbox);
    DDX_Control(pDX, IDC_BUTTON_CALLB, m_callb);
}

BEGIN_MESSAGE_MAP(XApic, CDialog)
    //{{AFX_MSG_MAP(XApic)
    ON_WM_CLOSE()
    //}}AFX_MSG_MAP
    ON_WM_SHOWWINDOW()
    ON_BN_CLICKED(IDC_BUTTON_CALL_MAINTAIN, OnClickedButtonCallMaintain)
    ON_BN_CLICKED(IDC_BUTTON_KEY0, OnClickedButtonKey0)
    ON_BN_CLICKED(IDC_BUTTON_KEY1, OnClickedButtonKey1)
    ON_BN_CLICKED(IDC_BUTTON_KEY2, OnClickedButtonKey2)
    ON_BN_CLICKED(IDC_BUTTON_KEY3, OnClickedButtonKey3)
    ON_BN_CLICKED(IDC_BUTTON_KEY4, OnClickedButtonKey4)
    ON_BN_CLICKED(IDC_BUTTON_KEY5, OnClickedButtonKey5)
    ON_BN_CLICKED(IDC_BUTTON_KEY6, OnClickedButtonKey6)
    ON_BN_CLICKED(IDC_BUTTON_KEY7, OnClickedButtonKey7)
    ON_BN_CLICKED(IDC_BUTTON_KEY8, OnClickedButtonKey8)
    ON_BN_CLICKED(IDC_BUTTON_KEY9, OnClickedButtonKey9)
    ON_BN_CLICKED(IDC_BUTTON_KEYSQUARE, OnClickedButtonKeysquare)
    ON_BN_CLICKED(IDC_BUTTON_KEYSTAR, OnClickedButtonKeystar)
    ON_LBN_DBLCLK(IDC_LIST_CALLERID, OnDblclkListCallerid)
    ON_LBN_DBLCLK(IDC_LIST_TRANSFERRED_CALLS, OnDblclkListTransferredCalls)
    ON_EN_CHANGE(IDC_EDIT_BDP, OnChangeEditBdp)
    ON_BN_CLICKED(IDC_BUTTON_REGISTER_BDP, OnClickedButtonRegisterBdp)
    ON_LBN_DBLCLK(IDC_LIST_SUSPENDED_CALLS, OnDblclkListSuspendedCalls)
    ON_EN_CHANGE(IDC_EDIT_DEFLECT, OnChangeEditDeflect)
    ON_BN_CLICKED(IDC_BUTTON_DEFLECT, OnClickedButtonDeflect)
    ON_BN_CLICKED(IDC_BUTTON_INTRUDE_BDP, OnClickedButtonIntrudeBdp)
    ON_BN_CLICKED(IDC_BUTTON_ANSWER_BDP, OnClickedButtonAnswerBdp)
    ON_BN_CLICKED(IDC_BUTTON_HOLD_RETRIEVE, OnClickedButtonHoldRetrieve)
    ON_BN_CLICKED(IDC_BUTTON_SUSPEND, OnClickedButtonSuspend)
    ON_BN_CLICKED(IDC_BUTTON_CONFERENCE, OnClickedButtonConference)
    ON_BN_CLICKED(IDC_BUTTON_TRANSFER, OnClickedButtonTransfer)
    ON_BN_CLICKED(IDC_BUTTON_RECORD, OnClickedButtonRecord)
    ON_BN_CLICKED(IDC_BUTTON_MAILBOX, OnClickedButtonMailbox)
    ON_BN_CLICKED(IDC_BUTTON_CALLB, OnClickedButtonCallb)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// XApic message handlers

void XApic::PostNcDestroy()
{
    if (handle)
    {
        XAPI_Client_stop (handle);
        handle = 0;
    }

    CDialog::PostNcDestroy();
    delete this;
}

void XApic::OnClose() 
{
    CDialog::OnClose();
    CDialog::DestroyWindow ();
}

void XApic::UpdateChargePulses (PXAPI pxapi)
{
    CString mStr;

    if (pxapi->ChargePulses)
    {
        mStr.Format ("Charge pulses: %i", pxapi->ChargePulses);
    }
    else
    {
        mStr.Format ("Port control state: %i", pxapi->PortControlState);
    }

    m_charge_pulses.SetWindowText (mStr);
}


void XApic::UpdateCallMaintainButton (PXAPI pxapi)
{
    if (pxapi->NewcallEnabled)
    {
        m_call_maintain.SetWindowText ("New Call");
        m_call_maintain.EnableWindow (TRUE);
    }
    else
    {
        if (pxapi->AnswerEnabled)
        {
            m_call_maintain.SetWindowText ("Answer");
            m_call_maintain.EnableWindow (TRUE);
        }
        else
        {
            if (pxapi->ClearEnabled)
            {
                m_call_maintain.SetWindowText ("Clear");
                m_call_maintain.EnableWindow (TRUE);
            }
            else
            {
                m_call_maintain.SetWindowText ("");
                m_call_maintain.EnableWindow (FALSE);
            }
        }
    }
}

void XApic::UpdateHoldRetrieveButton (PXAPI pxapi)
{
    if (pxapi->HoldEnabled)
    {
        m_hold_etrieve.SetWindowText ("Hold");
        m_hold_etrieve.EnableWindow (TRUE);
    }
    else
    {
        if (pxapi->RetrieveEnabled)
        {
            m_hold_etrieve.SetWindowText ("Retrieve");
            m_hold_etrieve.EnableWindow (TRUE);
        }
        else
        {
            m_hold_etrieve.SetWindowText ("");
            m_hold_etrieve.EnableWindow (FALSE);
        }
    }
}

void XApic::UpdateSuspendButton (PXAPI pxapi)
{
    m_suspend.EnableWindow (pxapi->SuspendEnabled);
}

void XApic::UpdateConferenceButton (PXAPI pxapi)
{
    m_conference.EnableWindow (pxapi->ConferenceEnabled);
}

void XApic::UpdateCallbButton (PXAPI pxapi)
{
    m_callb.EnableWindow (pxapi->CallbEnabled);
}

void XApic::UpdateTransferButton (PXAPI pxapi)
{
    m_transfer.EnableWindow (pxapi->TransferEnabled);
}

void XApic::UpdateRecordButton (PXAPI pxapi)
{
    if (pxapi->RecordStartEnabled)
    {
        m_record.SetWindowText ("Record start");
        m_record.EnableWindow (TRUE);
    }
    else
    {
        if (pxapi->RecordStopEnabled)
        {
            m_record.SetWindowText ("Record stop");
            m_record.EnableWindow (TRUE);
        }
        else
        {
            m_record.SetWindowText ("Record");
            m_record.EnableWindow (FALSE);
        }
    }
}

void XApic::UpdateMailboxButton (PXAPI pxapi)
{
    m_mailbox.EnableWindow (pxapi->MailboxEnabled);
}

void XApic::UpdateKeypad (BOOL enable)
{
    m_key1.EnableWindow (enable);
    m_key0.EnableWindow (enable);
    m_key2.EnableWindow (enable);
    m_key3.EnableWindow (enable);
    m_key4.EnableWindow (enable);
    m_key5.EnableWindow (enable);
    m_key6.EnableWindow (enable);
    m_key7.EnableWindow (enable);
    m_key8.EnableWindow (enable);
    m_key9.EnableWindow (enable);
    m_keysquare.EnableWindow (enable);
    m_keystar.EnableWindow (enable);
}

void XApic::SetDigitsDisplay (CString mStr)
{
    m_digits.ResetContent ();
    m_digits.AddString (mStr);
}

void XApic::UpdateNotification (PXAPI pxapi)
{
    if (pxapi->Suspended)
    {
        m_notification.SetWindowText ("Suspended");
    }
    else
    {
        if (pxapi->RemoteConf)
        {
            m_notification.SetWindowText ("Remote conf");
        }
        else
        {
            if (pxapi->RemoteHold)
            {
                m_notification.SetWindowText ("Remote Hold");
            }
            else
            {
                m_notification.SetWindowText ("");
            }
        }
    }
}

void XApic::UpdatePendingCalls (PXAPI pxapi)
{
    CString mStr;
    if (pxapi->PendingCalls)
    {
        mStr.Format ("Pending calls: %i", pxapi->PendingCalls);
    }
    else
    {
        mStr = "";
    }
    m_pending_calls.SetWindowText (mStr);
}

void XApic::UpdateTransferredCalls (PXAPI pxapi)
{
    if (pxapi->FunctionEnabled)
    {
        m_transferred_calls.EnableWindow (TRUE);
    }
    else
    {
        m_transferred_calls.EnableWindow (FALSE);
    }
}

void XApic::UpdateSuspendedCalls (PXAPI pxapi)
{
    if (pxapi->FunctionEnabled)
    {
        m_suspended_calls.EnableWindow (TRUE);
    }
    else
    {
        m_suspended_calls.EnableWindow (FALSE);
    }
}

void XApic::UpdateDeflect (PXAPI pxapi)
{
    if (pxapi->DeflectEnabled)
    {
        m_edit_deflect.EnableWindow (TRUE);
    }
    else
    {
        m_edit_deflect.EnableWindow (FALSE);
        m_edit_deflect.SetWindowText ("");
        m_deflect.EnableWindow (FALSE);
    }
}

void XApic::UpdateBdpButtons (PXAPI pxapi)
{
    m_intrude_bdp.EnableWindow (FALSE);
    m_answer_bdp.EnableWindow (FALSE);

    if ((BdpAccessCode != 0xffffffff)\
     && (pxapi->FunctionEnabled))
    {
        switch (BdpStatus)
        {
        case Bdp_status_slow_flash:
            {
                m_answer_bdp.EnableWindow (TRUE);
                break;
            }
        case Bdp_status_on:
            {
                m_intrude_bdp.EnableWindow (TRUE);
                break;
            }
        }
    }
}

void XApic::XapiStateChange (PXAPI pxapi)
{
    if (pxapi->RegistrationState != LastState.RegistrationState)
    {
        m_digits.ResetContent ();
    
        switch (pxapi->RegistrationState)
        {
        case RegistrationState_registering:
            {
                SetDigitsDisplay ("Registering");
                break;
            }
        case RegistrationState_registered:
            {
                SetDigitsDisplay ("");
                break;
            }
        case RegistrationState_registration_failed:
            {
                SetDigitsDisplay ("Registration failed");
                if (pxapi->registration_failed_cause)
                {
                    m_digits.AddString (pxapi->registration_failed_cause);
                }
                break;
            }
        }
    }

    if (pxapi->RegistrationState == RegistrationState_registered)
    {
        if ((LastState.PortControlState == PortControlState_s00_null_state)
         && (pxapi->PortControlState))
        { // transition from idle state
            SetDigitsDisplay ("");
        }
        if ((LastState.PortControlState)
         && (pxapi->PortControlState == PortControlState_s00_null_state))
        { // transition to idle state
            SetDigitsDisplay ("");
        }
        if ((LastState.PortControlState != pxapi->PortControlState)
         && (pxapi->PortControlState == PortControlState_s06_call_present))
        { // new incoming call
            SetDigitsDisplay ("Incoming call");
        }

        UpdateChargePulses (pxapi);
        UpdateNotification (pxapi);
        UpdatePendingCalls (pxapi);
        UpdateCallMaintainButton (pxapi);
        UpdateHoldRetrieveButton (pxapi);
        UpdateSuspendButton (pxapi);
        UpdateConferenceButton (pxapi);
        UpdateCallbButton (pxapi);
        UpdateTransferButton (pxapi);
        UpdateRecordButton (pxapi);
        UpdateMailboxButton (pxapi);
        UpdateKeypad (pxapi->DigitsEnabled);
        UpdateTransferredCalls (pxapi);
        UpdateSuspendedCalls (pxapi);
        UpdateBdpButtons (pxapi);
        m_callerid.EnableWindow (pxapi->FunctionEnabled);

        UpdateDeflect (pxapi);
    }

    LastState = (*pxapi);
}

void XApic::XapiDigitFromCallcontrol (unsigned char digit)
{
}

void XApic::XapiDigitToCallcontrol (unsigned char digit)
{
}

void XApic::XapiCallerId (const char * pCallerId)
{
    m_callerid.InsertString (0, pCallerId);
}

void XApic::XapiDisplay (const char * pDisplay)
{
    SetDigitsDisplay (pDisplay);
}

void XApic::XapiTransferredCalls (const char **pTransferredCalls)
{
    m_transferred_calls.ResetContent ();
    while (*pTransferredCalls)
    {
        m_transferred_calls.AddString (*pTransferredCalls++);
    }    
}

void XApic::XapiSuspendedCalls (const char **pSuspendedCalls)
{
    m_suspended_calls.ResetContent ();
    while (*pSuspendedCalls)
    {
        m_suspended_calls.AddString (*pSuspendedCalls++);
    }    
}

void XApic::XapiBdpStatus (BdpStatusS * pBdpStatus)
{
    CString mStr;

    if ((pBdpStatus->access_code == BdpAccessCode)\
     && (pBdpStatus->access_code != 0xffffffff))
    {
        BdpStatus = pBdpStatus->Status;

        mStr = AccToStrNoSpace (pBdpStatus->access_code);

        switch (BdpStatus)
        {
        case Bdp_status_fast_flash:
            {
                mStr += " Fast flash";
                break;
            }
        case Bdp_status_slow_flash:
            {
                if (LastState.FunctionEnabled)
                {
                    m_answer_bdp.EnableWindow (TRUE);
                }
                mStr += " Slow flash";
                break;
            }
        case Bdp_status_on:
            {
                if (LastState.FunctionEnabled)
                {
                    m_intrude_bdp.EnableWindow (TRUE);
                }
                mStr += " On";
                break;
            }
        }
        m_bdp_status.SetWindowText (mStr);
        UpdateBdpButtons (&LastState);
    }
}

BOOL XApic::PreTranslateMessage(MSG* pMsg)
{
    switch (pMsg->message)
    {
    case WM_KEYFIRST:
        {
            if ((pMsg->wParam == 0xd)\
             || (pMsg->wParam == 0x1b))
            { // enter or escape terminates program, disable it
                return TRUE;
            }
            break;
        }
    case XAPILNK_WM:
        {
            switch (pMsg->wParam)
            {
            case XAPILNK_WPARAM_STATE_CHANGE:
                {
                    XapiStateChange ((PXAPI)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_DIGIT_FROM_CALLCONTROL:
                {
                    XapiDigitFromCallcontrol ((unsigned char)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_DIGIT_TO_CALLCONTROL:
                {
                    XapiDigitToCallcontrol ((unsigned char)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_CALLING_LINE_ID:
                {
                    XapiCallerId ((const char *)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_DISPLAY:
                {
                    XapiDisplay ((const char *)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_BDP_STATUS:
                {
                    XapiBdpStatus ((BdpStatusS *)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_TRANSFERRED_CALLS:
                {
                    XapiTransferredCalls ((const char **)pMsg->lParam);
                    break;
                }
            case XAPILNK_WPARAM_SUSPENDED_CALLS:
                {
                    XapiSuspendedCalls ((const char **)pMsg->lParam);
                    break;
                }
            }
            return TRUE;
        }
    }
    return CDialog::PreTranslateMessage(pMsg);
}

void XApic::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialog::OnShowWindow(bShow, nStatus);
    SetDigitsDisplay ("Registering");
}

void XApic::OnClickedButtonKey0()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "0");
    }
}

void XApic::OnClickedButtonKey1()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "1");
    }
}

void XApic::OnClickedButtonKey2()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "2");
    }
}

void XApic::OnClickedButtonKey3()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "3");
    }
}

void XApic::OnClickedButtonKey4()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "4");
    }
}

void XApic::OnClickedButtonKey5()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "5");
    }
}

void XApic::OnClickedButtonKey6()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "6");
    }
}

void XApic::OnClickedButtonKey7()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "7");
    }
}

void XApic::OnClickedButtonKey8()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "8");
    }
}

void XApic::OnClickedButtonKey9()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "9");

    }
}

void XApic::OnClickedButtonKeysquare()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "#");
    }
}

void XApic::OnClickedButtonKeystar()
{
    if (LastState.DigitsEnabled)
    {
        XAPI_Client_digits (handle, "*");
    }
}

void XApic::OnClickedButtonCallMaintain()
{
    if (LastState.NewcallEnabled)
    {
        XAPI_Client_newcall (handle, NULL);
        return;
    }
    if (LastState.AnswerEnabled)
    {
        XAPI_Client_answer (handle);
        return;
    }
    if (LastState.ClearEnabled)
    {
        XAPI_Client_clear (handle);
        return;
    }
}

void XApic::OnClickedButtonHoldRetrieve()
{
    if (LastState.HoldEnabled)
    {
        XAPI_Client_hold (handle);
        return;
    }
    if (LastState.RetrieveEnabled)
    {
        XAPI_Client_retrieve (handle);
        return;
    }
}

void XApic::OnClickedButtonSuspend()
{
    if (LastState.SuspendEnabled)
    {
        XAPI_Client_suspend (handle);
        return;
    }
}

void XApic::OnClickedButtonConference()
{
    if (LastState.ConferenceEnabled)
    {
        XAPI_Client_conference (handle);
        return;
    }
}

void XApic::OnClickedButtonCallb()
{
    if (LastState.CallbEnabled)
    {
        XAPI_Client_callb (handle);
        return;
    }
}

void XApic::OnClickedButtonTransfer()
{
    if (LastState.TransferEnabled)
    {
        XAPI_Client_transfer (handle);
        return;
    }
}

void XApic::OnClickedButtonRecord()
{
    if (LastState.RecordStartEnabled)
    {
        XAPI_Client_record_start (handle);
        return;
    }
    if (LastState.RecordStopEnabled)
    {
        XAPI_Client_record_stop (handle);
        return;
    }
}

void XApic::OnClickedButtonMailbox()
{
    if (LastState.MailboxEnabled)
    {
        XAPI_Client_mailbox (handle);
        return;
    }
}

void XApic::OnDblclkListCallerid()
{
    CString mStr;

    if (LastState.DigitsEnabled)
    {
        int strpos = m_callerid.GetCurSel ();
        if (strpos >= 0)
        {
            m_callerid.GetText (strpos, mStr);
            XAPI_Client_digits (handle, (LPCSTR)mStr);
        }
    }
}

void XApic::OnDblclkListTransferredCalls()
{
    CString mStr;

    if (LastState.FunctionEnabled)
    {
        int strpos = m_transferred_calls.GetCurSel ();
        if (strpos >= 0)
        {
            m_transferred_calls.GetText (strpos, mStr);

            // send transfer_index to undo
            mStr = mStr.Mid (0, 4);
            XAPI_Client_transfer_undo (handle, (LPCSTR)mStr);
        }
    }
}

void XApic::OnChangeEditBdp()
{
    CString mStr;
    unsigned acc;

    m_bdp.GetWindowText (mStr);
    acc = StrToAcc (mStr);

    if ((acc != 0xffffffff)\
     && (acc != BdpAccessCode))
    {
        m_register_bdp.EnableWindow (TRUE);
    }
    else
    {
        m_register_bdp.EnableWindow (FALSE);
    }
}

void XApic::OnClickedButtonRegisterBdp()
{
    CString mStr;
    unsigned acc;

    m_bdp.GetWindowText (mStr);
    acc = StrToAcc (mStr);

    if ((acc != 0xffffffff)\
     && (acc != BdpAccessCode))
    {
        if (BdpAccessCode != 0xffffffff)
        { // unregister user that is already registered
            XAPI_Client_bdp_register (handle, BdpAccessCode, 0);
        }
        XAPI_Client_bdp_register (handle, acc, 1);
          // register new user

        BdpAccessCode = acc;
        BdpStatus = 0;

        m_bdp_status.SetWindowText ("");
        m_register_bdp.EnableWindow (FALSE);
    }
}

void XApic::OnClickedButtonIntrudeBdp()
{
    if ((LastState.FunctionEnabled)\
     && (BdpAccessCode != 0xffffffff)\
     && (BdpStatus == Bdp_status_on))
    {
        XAPI_Client_bdp_intrude (handle, BdpAccessCode);
    }
}

void XApic::OnClickedButtonAnswerBdp()
{
    if ((LastState.FunctionEnabled)\
     && (BdpAccessCode != 0xffffffff)\
     && (BdpStatus == Bdp_status_slow_flash))
    {
        XAPI_Client_bdp_answer (handle, BdpAccessCode);
    }
}

void XApic::OnDblclkListSuspendedCalls()
{
    CString mStr;
    unsigned acc;

    if (LastState.FunctionEnabled)
    {
        int strpos = m_suspended_calls.GetCurSel ();
        if (strpos >= 0)
        {
            m_suspended_calls.GetText (strpos, mStr);

            acc = StrToAcc (mStr);
            // fetch access code

            if (acc != 0xffffffff)
            {
                XAPI_Client_resume (handle, acc);
            }
        }
    }
}

void XApic::OnChangeEditDeflect()
{
    CString mStr;
    unsigned digit;

    m_edit_deflect.GetWindowText (mStr);
    if (mStr.GetLength ())
    {
        digit = mStr.GetAt (0);
        if ((digit >= '0') && (digit <= '9'))
        {
            m_deflect.EnableWindow (TRUE);
            return;
        }
    }
    m_deflect.EnableWindow (FALSE);
}

void XApic::OnClickedButtonDeflect()
{
    CString mStr;
    unsigned digit;

    m_edit_deflect.GetWindowText (mStr);
    if (mStr.GetLength ())
    {
        digit = mStr.GetAt (0);
        if ((digit >= '0') && (digit <= '9'))
        {
            XAPI_Client_deflect (handle, (LPCSTR)mStr);
        }
    }
}
