// PhoneSettingsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "afxres.h"
#include "palmtool.h"
#include "UdpSocket.h"
#include "tcpsocket.h"
#include "PhoneSettingsDlg.h"
#include "..\\inc\\sdram.h"
#include "PhoneAddrDlg.h"
#include "H323SettingsDlg.h"
#include "SipSettingsDlg.h"
#include "MgcpSettingsDlg.h"
#include "N2PSettingsDlg.h"
#include "Iax2SettingsDlg.h"
#include "WPSettingsDlg.h"
#include "EPNSettingsDlg.h"
#include "GatewaySettingsDlg.h"
#include "AdvancedSettingsDlg.h"
#include "DialPlanSettingsDlg.h"
#include "..\\inc\\p_util.h"

char g_cSettings[TOTAL_SETTINGS_SIZE];
static	int		Task_iRingType;
int IntFromString(CString str);
int GetVal(int iOrig, int iMax);
BOOL GetBoolean(int bOrig);

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

/////////////////////////////////////////////////////////////////////////////
// CPhoneSettingsDlg dialog


CPhoneSettingsDlg::CPhoneSettingsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPhoneSettingsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPhoneSettingsDlg)
	m_strMac1 = _T("");
	m_strMac2 = _T("");
	m_strMac3 = _T("");
	m_strMac4 = _T("");
	m_strMac5 = _T("");
	m_strMac6 = _T("");
	m_iMicVol = 0;
	m_iSpkVol = 0;
	m_bUseE164 = FALSE;
	m_iG7231Frames = 0;
	m_strPassword = _T("");
	m_strPPPoEID = _T("");
	m_strPPPoEPin = _T("");
	m_strUpgradeAddr = _T("");
	m_iSubVersion = 0;
	m_bUseHighRate = FALSE;
	m_bUseVAD = FALSE;
	m_bUseAGC = FALSE;
	m_bUseAEC = FALSE;
	m_bUseDaylight = FALSE;
	m_iJitterSize = 0;
	m_iCodecType1 = -1;
	m_iCodecType2 = -1;
	m_iCodecType3 = -1;
	m_iCodecType4 = -1;
	m_iCodecType5 = -1;
	m_iCodecType6 = -1;
	m_iIlbcPType = 0;
	m_bVlanEnable = FALSE;
	m_iVlanID = 0;
	//}}AFX_DATA_INIT
}


void CPhoneSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPhoneSettingsDlg)
	DDX_Control(pDX, IDDIALPLAN, m_ctlDialPlanSettings);
	DDX_Control(pDX, IDATTRIBUTE, m_ctlAttribute);
	DDX_Control(pDX, IDC_CODEC_TYPE6, m_ctlCodecType6);
	DDX_Control(pDX, IDC_UPGRADE_TYPE, m_ctlUpgradeType);
	DDX_Control(pDX, IDC_CODEC_TYPE5, m_ctlCodecType5);
	DDX_Control(pDX, IDC_CODEC_TYPE4, m_ctlCodecType4);
	DDX_Control(pDX, IDC_CODEC_TYPE3, m_ctlCodecType3);
	DDX_Control(pDX, IDC_CODEC_TYPE2, m_ctlCodecType2);
	DDX_Control(pDX, IDC_CODEC_TYPE1, m_ctlCodecType1);
	DDX_Control(pDX, IDADDR, m_ctlAddr);
	DDX_Control(pDX, IDC_TIME_ZONE, m_ctlTimeZone);
	DDX_Control(pDX, IDC_SNTP_ADDR, m_ctlSntpIP);
	DDX_Control(pDX, IDC_IPTYPE, m_ctlIPType);
	DDX_Control(pDX, IDC_DNS_IP, m_ctlDnsIP);
	DDX_Control(pDX, IDC_DNS_IP2, m_ctlDnsIP2);
	DDX_Control(pDX, IDC_SPK_VOL, m_ctlSpkVol);
	DDX_Control(pDX, IDC_MIC_VOL, m_ctlMicVol);
	DDX_Control(pDX, IDC_SUBNET_MASK, m_ctlSubnetMask);
	DDX_Control(pDX, IDC_ROUTER_IP, m_ctlRouterIP);
	DDX_Control(pDX, IDC_LOCAL_IP, m_ctlLocalIP);
	DDX_Text(pDX, IDC_MAC1, m_strMac1);
	DDV_MaxChars(pDX, m_strMac1, 4);
	DDX_Text(pDX, IDC_MAC2, m_strMac2);
	DDV_MaxChars(pDX, m_strMac2, 4);
	DDX_Text(pDX, IDC_MAC3, m_strMac3);
	DDV_MaxChars(pDX, m_strMac3, 4);
	DDX_Text(pDX, IDC_MAC4, m_strMac4);
	DDV_MaxChars(pDX, m_strMac4, 4);
	DDX_Text(pDX, IDC_MAC5, m_strMac5);
	DDV_MaxChars(pDX, m_strMac5, 4);
	DDX_Text(pDX, IDC_MAC6, m_strMac6);
	DDV_MaxChars(pDX, m_strMac6, 4);
	DDX_Slider(pDX, IDC_MIC_VOL, m_iMicVol);
	DDX_Slider(pDX, IDC_SPK_VOL, m_iSpkVol);
	DDX_Text(pDX, IDC_G7231_FRAMES, m_iG7231Frames);
	DDV_MinMaxInt(pDX, m_iG7231Frames, 0, 8);
	DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
	DDV_MaxChars(pDX, m_strPassword, 16);
	DDX_Text(pDX, IDC_PPPOEID, m_strPPPoEID);
	DDV_MaxChars(pDX, m_strPPPoEID, 40);
	DDX_Text(pDX, IDC_PPPOEPIN, m_strPPPoEPin);
	DDV_MaxChars(pDX, m_strPPPoEPin, 24);
	DDX_Text(pDX, IDC_UPGRADE_ADDR, m_strUpgradeAddr);
	DDV_MaxChars(pDX, m_strUpgradeAddr, 32);
	DDX_Check(pDX, IDC_PHONE_USE_HIGH_RATE, m_bUseHighRate);
	DDX_Check(pDX, IDC_PHONE_USE_VAD, m_bUseVAD);
	DDX_Check(pDX, IDC_AGC, m_bUseAGC);
	DDX_Check(pDX, IDC_AEC, m_bUseAEC);
	DDX_Check(pDX, IDC_DAYLIGHT, m_bUseDaylight);
	DDX_Text(pDX, IDC_JITTER_SIZE, m_iJitterSize);
	DDV_MinMaxInt(pDX, m_iJitterSize, 0, 33);
	DDX_CBIndex(pDX, IDC_CODEC_TYPE1, m_iCodecType1);
	DDX_CBIndex(pDX, IDC_CODEC_TYPE2, m_iCodecType2);
	DDX_CBIndex(pDX, IDC_CODEC_TYPE3, m_iCodecType3);
	DDX_CBIndex(pDX, IDC_CODEC_TYPE4, m_iCodecType4);
	DDX_CBIndex(pDX, IDC_CODEC_TYPE5, m_iCodecType5);
	DDX_CBIndex(pDX, IDC_CODEC_TYPE6, m_iCodecType6);
	DDX_Text(pDX, IDC_ILBC_PAYLOAD, m_iIlbcPType);
	DDV_MinMaxInt(pDX, m_iIlbcPType, 0, 255);
	DDX_Check(pDX, IDC_VLAN_ENABLE, m_bVlanEnable);
	DDX_Text(pDX, IDC_VLAN_ID, m_iVlanID);
	DDV_MinMaxInt(pDX, m_iVlanID, 0, 4095);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPhoneSettingsDlg, CDialog)
	//{{AFX_MSG_MAP(CPhoneSettingsDlg)
	ON_CBN_SELCHANGE(IDC_IPTYPE, OnSelchangeIptype)
	ON_BN_CLICKED(IDADDR, OnLoadAddr)
	ON_BN_CLICKED(IDPROTOCOL, OnProtocol)
	ON_BN_CLICKED(IDATTRIBUTE, OnAttribute)
	ON_BN_CLICKED(IDDIALPLAN, OnDialplan)
	ON_BN_CLICKED(IDC_VLAN_ENABLE, OnVlanEnable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPhoneSettingsDlg message handlers

void CPhoneSettingsDlg::OnOK() 
{
	ULONG uIP;

	UpdateData();

	/* 0x000000--0x000005: Local MAC address 0x00, 0x09, 0x45, 0x00, 0x00, 0x00 */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac1"), m_strMac1);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac2"), m_strMac2);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac3"), m_strMac3);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac4"), m_strMac4);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac5"), m_strMac5);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac6"), m_strMac6);

	/* 0x000006--0x000009: Local IP address 192, 168, 1, 100 */
	m_ctlLocalIP.GetAddress(uIP);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("LocalIP"), uIP);

	/* 0x000020--0x000023: Subnet mask */
	m_ctlSubnetMask.GetAddress(uIP);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SubnetMask"), uIP);

	/* 0x000024--0x000027: Router IP address */
	m_ctlRouterIP.GetAddress(uIP);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RouterIP"), uIP);

	/* 0x000028:           1 to use DHCP, 2 to use PPPOE, 0 to use IP address in 06-09 */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("IPType"), m_ctlIPType.GetCurSel());

	/* 0x00000a:           1 to enable VLAN support, 0 to disable VLAN support */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("VlanEnable"), (int)m_bVlanEnable);
	/* 0x00000b--0x00000c:           VLAN ID, range from 0-4095 */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("VlanID"), m_iVlanID);

	/* 0x000029:           Initial mic vol */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("MicVol"), m_iMicVol);

	/* 0x00002a:           Initial speaker vol */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SpkVol"), m_iSpkVol);

	/* 0x00002c--0x00002f: DNS server IP address */
	m_ctlDnsIP.GetAddress(uIP);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DnsIP"), uIP);

	/* 0x000035:           Default G.723.1 audio frames in a send-out packet (1-8) */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("G7231Frames"), m_iG7231Frames);

	/* 0x00003b--0x00003e: Alternate Dns server IP address */
	m_ctlDnsIP2.GetAddress(uIP);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DnsIP2"), uIP);

	/* 0x000048--0x000057: Password */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Password"), m_strPassword);

	/* 0x0000b0--0x0000d7: PPP id */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("PPPoEID"), m_strPPPoEID);

	/* 0x0000d8--0x0000ef: PPP password */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("PPPoEPin"), m_strPPPoEPin);

	/* 0x000161:           Upgrade Type, 0 to disable, 1 - all, 2 - pppid, 3 - account, 4 - phonenumber */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UpgradeType"), m_ctlUpgradeType.GetCurSel());
	
	/* 0x000120:           Codec type, 0 - g729, 1 - g7231, 2 - g711u, 3 - g711a, 4 - gsm, 5 - null */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec1"), (int)m_ctlCodecType1.GetCurSel());
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec2"), (int)m_ctlCodecType2.GetCurSel());
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec3"), (int)m_ctlCodecType3.GetCurSel());
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec4"), (int)m_ctlCodecType4.GetCurSel());
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec5"), (int)m_ctlCodecType5.GetCurSel());
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec6"), (int)m_ctlCodecType6.GetCurSel());

	/* 0x00012f:           iLBC RTP payload type */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("IlbcPayload"), m_iIlbcPType);

	/* 0x00002b:           Dsp options */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseHighRate"), (int)m_bUseHighRate);
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseVAD"), (int)m_bUseVAD);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseAGC"), (int)m_bUseAGC);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseAEC"), (int)m_bUseAEC);

	/* 0x000030:           Jitter size */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("JitterSize"), m_iJitterSize);

	/* 0x000162:           Time zone */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("TimeZone"), (int)m_ctlTimeZone.GetCurSel());
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseDaylight"), (int)m_bUseDaylight);

	/* 0x000163--0x000166: Time Server IP address 210, 59, 157, 10 */
	m_ctlSntpIP.GetAddress(uIP);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SntpIP"), uIP);

	/* 0x0001d0-0x0001ff: Ftp DNS */
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("UpgradeAddr"), m_strUpgradeAddr);

	/* 0x0000fb:		    SubVersion Number */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SubVersion"), m_iSubVersion);

	CDialog::OnOK();
}

BOOL GetBoolean(int bOrig)
{
	if (bOrig == 0)
	{
		return FALSE;
	}
	else if (bOrig == 1)
	{
		return TRUE;
	}
	return FALSE;
}

int GetVal(int iOrig, int iMax)
{
	if (iOrig < iMax)
	{
		return iOrig;
	}
	return iMax-1;
}

void CPhoneSettingsDlg::SetSettings(BYTE * pSettings)
{
	CString str;
	ULONG uIP;
	static	char cStr[41];

	memcpy(g_cSettings, pSettings, TOTAL_SETTINGS_SIZE);

	char * pFlag;
	pFlag = g_cSettings + 256;
	Sys_iProtocol = GetVal((int)pFlag[FLAGS_PROTOCOL], PROTOCOL_MAX);

	/* 0x000000--0x000005: Local MAC address 0x00, 0x09, 0x45, 0x00, 0x00, 0x00 */
	str.Format("0x%02x", pSettings[OPTIONS_MAC_ADDR]);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac1"), str);
	str.Format("0x%02x", pSettings[OPTIONS_MAC_ADDR + 1]);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac2"), str);
	str.Format("0x%02x", pSettings[OPTIONS_MAC_ADDR + 2]);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac3"), str);
	str.Format("0x%02x", pSettings[OPTIONS_MAC_ADDR + 3]);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac4"), str);
	str.Format("0x%02x", pSettings[OPTIONS_MAC_ADDR + 4]);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac5"), str);
	str.Format("0x%02x", pSettings[OPTIONS_MAC_ADDR + 5]);
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Mac6"), str);

	/* 0x000006--0x000009: Local IP address 192, 168, 1, 100 */
	uIP = (pSettings[OPTIONS_IP_ADDR]<<24) + (pSettings[OPTIONS_IP_ADDR + 1]<<16) + (pSettings[OPTIONS_IP_ADDR + 2]<<8) + pSettings[OPTIONS_IP_ADDR + 3];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("LocalIP"), uIP);

	/* 0x000163--0x000166: Time server IP address 210, 59, 157, 10 */
	uIP = (pSettings[OPTIONS_NTS_ADDR]<<24) + (pSettings[OPTIONS_NTS_ADDR + 1]<<16) + (pSettings[OPTIONS_NTS_ADDR + 2]<<8) + pSettings[OPTIONS_NTS_ADDR + 3];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SntpIP"), uIP);

	/* 0x00000a:           1 to enable VLAN support, 0 to disable VLAN support */
	VLAN_bEnable = GetBoolean((int)pSettings[OPTIONS_VLAN_ENABLE]);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("VlanEnable"), (int)VLAN_bEnable);
	/* 0x00000b--0x00000c:           VLAN ID, range from 0-4095 */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("VlanID"), (int)((pSettings[OPTIONS_VLAN_ID] << 8) + pSettings[OPTIONS_VLAN_ID+1]));

	/* 0x00000e:           1 to use gatekeeper, 0 to dial IP address directly 1,*/
	Sys_bUseService = GetBoolean((int)pSettings[OPTIONS_USE_GK]);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseService"), (int)Sys_bUseService);

	/* 0x00000f:           ring type */
	Task_iRingType = GetVal((int)pSettings[OPTIONS_RING_TYPE], RING_TYPE_MAX);
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RingType"), pSettings[OPTIONS_RING_TYPE]);

	/* 0x000010--0x00001f: My phone number to use with a gatekeeper */
	memcpy(cStr, (char *)(pSettings+OPTIONS_PHONE_NUMBER), PHONE_NUMBER_LEN);
	cStr[PHONE_NUMBER_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("PhoneNumber"), cStr);

	/* 0x000170--0x00017f: dial number */
	memcpy(cStr, (char *)(pSettings+OPTIONS_DIAL_NUMBER), DIAL_NUMBER_LEN);
	cStr[DIAL_NUMBER_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DialNumber"), cStr);

	/* 0x000020--0x000023: Subnet mask */
	uIP = (pSettings[OPTIONS_SUBNET_MASK]<<24) + (pSettings[OPTIONS_SUBNET_MASK + 1]<<16) + (pSettings[OPTIONS_SUBNET_MASK + 2]<<8) + pSettings[OPTIONS_SUBNET_MASK + 3];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SubnetMask"), uIP);

	/* 0x000024--0x000027: Router IP address */
	uIP = (pSettings[OPTIONS_ROUTER_ADDR]<<24) + (pSettings[OPTIONS_ROUTER_ADDR + 1]<<16) + (pSettings[OPTIONS_ROUTER_ADDR + 2]<<8) + pSettings[OPTIONS_ROUTER_ADDR + 3];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RouterIP"), uIP);

	/* 0x000028:           1 to use DHCP, 0 to use IP address in 06-09 */
	Sys_iIPType = GetVal((int)pSettings[OPTIONS_IP_TYPE], IP_TYPE_MAX);
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("IPType"), pSettings[OPTIONS_IP_TYPE]);

	/* 0x000029:           Initial mic vol */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("MicVol"), (int)pSettings[OPTIONS_HANDSET_INPUT]);

	/* 0x00002a:           Initial speaker vol */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SpkVol"), (int)pSettings[OPTIONS_HANDSET_OUTPUT]);

	/* 0x00002c--0x00002f: Dns IP address */
	uIP = (pSettings[OPTIONS_DNS_ADDR]<<24) + (pSettings[OPTIONS_DNS_ADDR + 1]<<16) + (pSettings[OPTIONS_DNS_ADDR + 2]<<8) + pSettings[OPTIONS_DNS_ADDR + 3];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DnsIP"), uIP);

	/* 0x00003b--0x00003e: Alternate Dns server IP address */
	uIP = (pSettings[OPTIONS_DNS_ADDR2]<<24) + (pSettings[OPTIONS_DNS_ADDR2 + 1]<<16) + (pSettings[OPTIONS_DNS_ADDR2 + 2]<<8) + pSettings[OPTIONS_DNS_ADDR2 + 3];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DnsIP2"), uIP);

	/* 0x000031:           Number to ring before auto answer */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RingTimeout"), (int)pSettings[OPTIONS_RING_NUMBER]);

	/* 0x00015f:           Type of service */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Tos"), (int)pSettings[OPTIONS_RTP_TOS]);

	/* 0x00003a:           Dtmf Payload Type */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DtmfPayloadType"), (int)pSettings[OPTIONS_DTMF_PTYPE]);

	/* 0x00012f:           iLBC RTP payload type */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("IlbcPayload"), (int)pSettings[OPTIONS_ILBC_PTYPE]);

	/* 0x0000f6:           Register TTL  high*/
	/* 0x0000f7:           Register TTL  low*/
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RegisterTTL"), (int)((pSettings[OPTIONS_REGISTER_TTL] << 8) + pSettings[OPTIONS_REGISTER_TTL+1]));

	/* 0x000030:           Jitter size */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("JitterSize"), (int)pSettings[OPTIONS_JITTER_SIZE]);

	/* 0x0000f8:           NAT hint TTL high*/
	/* 0x0000f9:           NAT hint TTL low*/
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NatTTL"), (int)((pSettings[OPTIONS_NAT_TTL] << 8) + pSettings[OPTIONS_NAT_TTL+1]));

	/* 0x000033:           Initial ring vol */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RingVol"), (int)pSettings[OPTIONS_HANDFREE_OUTPUT]);

	/* 0x000034:           Initial Handfree input vol */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SpeakerIn"), (int)pSettings[OPTIONS_HANDFREE_INPUT]);

	/* 0x000120:           Codec type, 0 - g729, 1 - g7231, 2 - g711u, 3 - g711a, 4 - gsm, 5 - null */
	Sys_pCodecType[0] = GetVal((int)pSettings[OPTIONS_CODEC_TYPE], CODEC_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec1"), Sys_pCodecType[0]);
	Sys_pCodecType[1] = GetVal((int)pSettings[OPTIONS_CODEC_TYPE+1], CODEC_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec2"), Sys_pCodecType[1]);
	Sys_pCodecType[2] = GetVal((int)pSettings[OPTIONS_CODEC_TYPE+2], CODEC_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec3"), Sys_pCodecType[2]);
	Sys_pCodecType[3] = GetVal((int)pSettings[OPTIONS_CODEC_TYPE+3], CODEC_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec4"), Sys_pCodecType[3]);
	Sys_pCodecType[4] = GetVal((int)pSettings[OPTIONS_CODEC_TYPE+4], CODEC_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec5"), Sys_pCodecType[4]);
	Sys_pCodecType[5] = GetVal((int)pSettings[OPTIONS_CODEC_TYPE+5], CODEC_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Codec6"), Sys_pCodecType[5]);

	/* 0x000035:           Default G.723.1 audio frames in a send-out packet (1-8) */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("G7231Frames"), (int)pSettings[OPTIONS_AUDIO_FRAMES]);

	/* 0x000036:           Q931 source port high
	   0x000037:           Q931 source port low */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Q931Port"), (int)((pSettings[OPTIONS_Q931_PORT] << 8) + pSettings[OPTIONS_Q931_PORT+1]));

	/* 0x000150:		   Forward type, 0 - none, 1 - always, 2 - busy, 3 - no answer */	 
	Sys_iForwardType = (int)pSettings[OPTIONS_FWD_TYPE];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdAlways"), ((Sys_iForwardType & FORWARD_ALWAYS_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdBusy"), ((Sys_iForwardType & FORWARD_BUSY_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdNoanswer"), ((Sys_iForwardType & FORWARD_NOANSWER_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("FwdPoweroff"), ((Sys_iForwardType & FORWARD_POWEROFF_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("CallWaiting"), ((Sys_iForwardType & CALL_WAITING_OR) ? 1 : 0));

	/* 0x00002b:           Dsp Options  */
	Dsp_iOptions = (int)pSettings[OPTIONS_DSP_OPTION];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseHighRate"), ((Dsp_iOptions & USE_HIGH_RATE_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseVAD"), ((Dsp_iOptions & USE_VAD_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseAGC"), ((Dsp_iOptions & USE_AGC_OR) ? 1 : 0));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseAEC"), ((Dsp_iOptions & USE_AEC_OR) ? 1 : 0));

	/* 0x000162:		   Time zone and use daylight */	 
	Sys_iTimeZone = (int)pSettings[OPTIONS_TIME_ZONE];
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("TimeZone"), (Sys_iTimeZone & TIMEZONE_DAYLIGHT_AND));
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseDaylight"), ((Sys_iTimeZone & TIMEZONE_DAYLIGHT_OR) ? 1 : 0));

	/* 0x000140--0x00014f: forward number */
	memcpy(cStr, (char *)(pSettings+OPTIONS_FWD_NUMBER), FORWARD_NUMBER_LEN);
	cStr[FORWARD_NUMBER_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("ForwardNumber"), cStr);

	/* 0x000048--0x00004f: Password */
	memcpy(cStr, (char *)(pSettings+OPTIONS_PASSWORD), MAX_PASSWORD_LEN);
	cStr[MAX_PASSWORD_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Password"), cStr);

	/* 0x000130--0x00013f: Supervisor Password */
	memcpy(cStr, (char *)(pSettings+OPTIONS_SUPER_PASSWORD), MAX_SUPERPASSWORD_LEN);
	cStr[MAX_SUPERPASSWORD_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("SuperPassword"), cStr);

	/* 0x000058--0x00005b: Country Code; maximum length 4 digits */
	memcpy(cStr, (char *)(pSettings+OPTIONS_COUNTRY_CODE), MAX_COUNTRY_CODE_LEN);
	cStr[MAX_COUNTRY_CODE_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("IDDCode"), cStr);
	
	/* 0x00005c--0x00005e: International Prefix: maximum length 3 digits */
	memcpy(cStr, (char *)(pSettings+OPTIONS_INTL_PREFIX), MAX_INTL_PREFIX_LEN);
	cStr[MAX_INTL_PREFIX_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("IDDPrefix"), cStr);

	/* 0x00005f:		   1 to use dial plan, 0 to ignore */
	Sys_iUseDialPlan = GetVal((int)pSettings[OPTIONS_USE_DIALPLAN], DIALPLAN_MAX);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseDialPlan"), Sys_iUseDialPlan);

	/* 0x000060--0x000063: Area Code: maximum length 4 digits */
	memcpy(cStr, (char *)(pSettings+OPTIONS_AREA_CODE), MAX_AREA_CODE_LEN);
	cStr[MAX_AREA_CODE_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DDDCode"), cStr);

	/* 0x000064--0x000066: Longprefix: maximum length 3 digits */
	memcpy(cStr, (char *)(pSettings+OPTIONS_LONG_PREFIX), MAX_LONG_PREFIX_LEN);
	cStr[MAX_LONG_PREFIX_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DDDPrefix"), cStr);

	/* 0x000068:		   1 - inner line, 0 - direct dial, default 0 */
	Sys_iInnerLine = GetVal((int)pSettings[OPTIONS_INNER_LINE], INNER_LINE_MAX);
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("InnerLine"), Sys_iInnerLine);

	/* 0x00015f:		   Dual mode, 0 to disable, 1 pstn first, 2 ip first */
	Sys_iDualMode = GetVal((int)pSettings[OPTIONS_DUAL_MODE], DUAL_MODE_MAX);
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DualMode"), Sys_iDualMode);

	/* 0x000069--0x00006a: to access local outer line */ 
	memcpy(cStr, (char *)(pSettings+OPTIONS_DUALMODE_PREFIX), MAX_DUALMODE_PREFIX_LEN);
	cStr[MAX_DUALMODE_PREFIX_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("DualModePrefix"), cStr);

	/* 0x00006b--0x00006c: to access long-distance outer line */
	memcpy(cStr, (char *)(pSettings+OPTIONS_INNERLINE_PREFIX), MAX_INNERLINE_PREFIX_LEN);
	cStr[MAX_INNERLINE_PREFIX_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("InnerLinePrefix"), cStr);

	/* 0x00006e--0x00007d: calling card account */
	memcpy(cStr, (char *)(pSettings+OPTIONS_ACCOUNT), MAX_ACCOUNT_LEN);
	cStr[MAX_ACCOUNT_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Account"), cStr);

	/* 0x00007e--0x00008d: calling card PIN */
	memcpy(cStr, (char *)(pSettings+OPTIONS_PIN), MAX_PIN_LEN);
	cStr[MAX_PIN_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("Pin"), cStr);

	/* 0x0001a0--0x0001cf: Service DNS */
	memcpy(cStr, (char *)(pSettings+OPTIONS_SERVICE_DNS), MAX_DNS_ADDR_LEN);
	cStr[MAX_DNS_ADDR_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("ServiceDns"), cStr);

	/* 0x0001e0-0x0001ff: NAT Server Address */
	memcpy(cStr, (char *)(pSettings+OPTIONS_NAT_DNS), MAX_DNS_ADDR_LEN);
	cStr[MAX_DNS_ADDR_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("NatAddr"), cStr);

	/* 0x0001d0--0x0001ff: ftp DNS */
	memcpy(cStr, (char *)(pSettings+OPTIONS_UPGRADE_DNS), MAX_DNS_ADDR_LEN);
	cStr[MAX_DNS_ADDR_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("UpgradeAddr"), cStr);

	/* 0x0000b0--0x0000df: PPP id */
	memcpy(cStr, (char *)(pSettings+OPTIONS_PPP_ID), PPP_ID_LEN);
	cStr[PPP_ID_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("PPPoEID"), cStr);

	/* 0x0000c8--0x0000d7: PPP password */
	memcpy(cStr, (char *)(pSettings+OPTIONS_PPP_PASSWORD), PPP_PASSWORD_LEN);
	cStr[PPP_PASSWORD_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("PPPoEPin"), cStr);

	/* 0x0000d8--0x0000ef: GKID */
	memcpy(cStr, (char *)(pSettings+OPTIONS_SERVICE_ID), MAX_SERVICE_ID_LEN);
	cStr[MAX_SERVICE_ID_LEN] = 0;
	AfxGetApp()->WriteProfileString(_T("PhoneSettings"), _T("ServiceID"), cStr);

	/* 0x0000f0:           Ras source port high
	   0x0000f1:           Ras source port low */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RasPort"), (int)((pSettings[OPTIONS_RAS_PORT] << 8)+pSettings[OPTIONS_RAS_PORT+1]));

	/* 0x0000f2:           H245 source port high
	   0x0000f3:           H245 source port low */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("H245Port"), (int)((pSettings[OPTIONS_H245_PORT] << 8)+pSettings[OPTIONS_H245_PORT+1]));

	/* 0x0000f4:           Rtp source port high 
	   0x0000f5:           Rtp source port low */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RtpPort"), (int)((pSettings[OPTIONS_RTP_PORT] << 8)+pSettings[OPTIONS_RTP_PORT+1]));

	/* 0x0000fa:			Dhcp or PPPoE retries */
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NetworkRetry"), pSettings[OPTIONS_NETWORK_RETRY]);

	/* 0x0000fe:           1 to output debug information by udp, 0 to output nothing */
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("Output"), (int)pSettings[OPTIONS_OUTPUT_DEBUG]);

	/* 0x000161:           Upgrade Type, 0 to disable, 1 - all, 2 - pppid, 3 - account, 4 - phonenumber */
	Sys_iUpgradeType = GetVal((int)pSettings[OPTIONS_UPGRADE_TYPE], UPGRADE_TYPE_MAX);
    AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UpgradeType"), Sys_iUpgradeType);

	/* 0x0000ff:		   1 to use digit map */
	Sys_bUseDigitMap = GetBoolean((int)pSettings[OPTIONS_USE_DIGITMAP]);
	AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseDigitMap"), (int)Sys_bUseDigitMap);

	switch(Sys_iProtocol)
	{
	case PROTOCOL_H323:
		/* 0x000032:           1 to use e.164 log on with gatekeeper, 0 to use h323-id */
		Sys_iLocalType = GetVal((int)pSettings[OPTIONS_LOCAL_ALIAS], H323_LOCAL_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("LocalAlias"), Sys_iLocalType);

		/* 0x00003f:           Use built in H.323 proxy support */
		Sys_iNatTraversal = GetVal((int)pSettings[OPTIONS_NAT_TRAVERSAL], H323_NAT_TRAVERSAL_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NatTraversal"), Sys_iNatTraversal);

		/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */
		Sys_iDtmfType = GetVal((int)pSettings[OPTIONS_DTMF_TYPE], H323_DTMF_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DTMFType"), Sys_iDtmfType);

		/* 0x00006d:		   1 to use calling card, 0 to direct dial */
		Sys_iServiceType = GetVal((int)pSettings[OPTIONS_SERVICE_TYPE], H323_SERVICE_TYPE_MAX); 
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("ServiceType"), Sys_iServiceType);
		break;

	case PROTOCOL_SIP:
		/* 0x00003f:           Use built in H.323 proxy support */
		Sys_iNatTraversal = GetVal((int)pSettings[OPTIONS_NAT_TRAVERSAL], SIP_NAT_TRAVERSAL_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NatTraversal"), Sys_iNatTraversal);

		/* 0x000046:		   1 to use fast start, 0 to bring normal h245 */
		Sys_iCallType = (int)pSettings[OPTIONS_CALL_TYPE];
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("UseFaststart"), ((Sys_iCallType & FAST_START_OR) ? TRUE : FALSE));
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("SipPrack"), ((Sys_iCallType & SIP_PRACK_OR) ? TRUE : FALSE));

		/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */
		Sys_iDtmfType = GetVal((int)pSettings[OPTIONS_DTMF_TYPE], SIP_DTMF_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DTMFType"), Sys_iDtmfType);

		/* 0x00006d:		   1 to use calling card, 0 to direct dial */
		Sys_iServiceType = GetVal((int)pSettings[OPTIONS_SERVICE_TYPE], SIP_SERVICE_TYPE_MAX); 
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("ServiceType"), Sys_iServiceType);
		break;

	case PROTOCOL_IAX2:
		Sys_iCallType = (int)pSettings[OPTIONS_CALL_TYPE];
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("RemoteDialplan"), ((Sys_iCallType & REMOTE_DIALPLAN_OR) ? TRUE : FALSE));
		break;

	case PROTOCOL_MGCP:
		/* 0x000032:           1 to use e.164 log on with gatekeeper, 0 to use h323-id */
		Sys_iLocalType = GetVal((int)pSettings[OPTIONS_LOCAL_ALIAS], MGCP_LOCAL_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("LocalAlias"), Sys_iLocalType);

		/* 0x00003f:           Use built in H.323 proxy support */
		Sys_iNatTraversal = GetVal((int)pSettings[OPTIONS_NAT_TRAVERSAL], MGCP_NAT_TRAVERSAL_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("NatTraversal"), Sys_iNatTraversal);

		/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */
		Sys_iDtmfType = GetVal((int)pSettings[OPTIONS_DTMF_TYPE], MGCP_DTMF_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DTMFType"), Sys_iDtmfType);

		/* 0x00006d:		   1 to use calling card, 0 to direct dial */
		Sys_iServiceType = GetVal((int)pSettings[OPTIONS_SERVICE_TYPE], MGCP_SERVICE_TYPE_MAX); 
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("ServiceType"), Sys_iServiceType);
		break;

	case PROTOCOL_N2P:
		/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */
		Sys_iDtmfType = GetVal((int)pSettings[OPTIONS_DTMF_TYPE], N2P_DTMF_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DTMFType"), Sys_iDtmfType);
		break;

	default:
		/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */
		Sys_iDtmfType = GetVal((int)pSettings[OPTIONS_DTMF_TYPE], DTMF_TYPE_MAX);
		AfxGetApp()->WriteProfileInt(_T("PhoneSettings"), _T("DTMFType"), Sys_iDtmfType);
		break;
	}
}

BOOL CPhoneSettingsDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	ULONG uIP;
	int i;
	CString strText;

	char * pFlag;
	pFlag = g_cSettings + 256;

	Sys_iDeviceAttr = (int)pFlag[FLAGS_DEVICE_ATTR];

	if((Sys_iDeviceAttr | DEVICE_ATTR_NONE))
	{
		m_ctlAttribute.EnableWindow(TRUE);
	}
	else
	{
		m_ctlAttribute.EnableWindow(FALSE);
	}

	if(Sys_iProtocol != PROTOCOL_MGCP)
	{
		m_ctlDialPlanSettings.EnableWindow(TRUE);
	}
	else
	{
		m_ctlDialPlanSettings.EnableWindow(FALSE);
	}

	for(i = 0; i < CODEC_MAX; i++)
		m_ctlCodecType1.AddString((char *)(g_cAudioTypeDisplay[0] + i * AUDIO_TYPE_DISPLAY_LEN));

	for(i = 0; i < CODEC_MAX; i++)
		m_ctlCodecType2.AddString((char *)(g_cAudioTypeDisplay[0] + i * AUDIO_TYPE_DISPLAY_LEN));

	for(i = 0; i < CODEC_MAX; i++)
		m_ctlCodecType3.AddString((char *)(g_cAudioTypeDisplay[0] + i * AUDIO_TYPE_DISPLAY_LEN));

	for(i = 0; i < CODEC_MAX; i++)
		m_ctlCodecType4.AddString((char *)(g_cAudioTypeDisplay[0] + i * AUDIO_TYPE_DISPLAY_LEN));

	for(i = 0; i < CODEC_MAX; i++)
		m_ctlCodecType5.AddString((char *)(g_cAudioTypeDisplay[0] + i * AUDIO_TYPE_DISPLAY_LEN));

	for(i = 0; i < CODEC_MAX; i++)
		m_ctlCodecType6.AddString((char *)(g_cAudioTypeDisplay[0] + i * AUDIO_TYPE_DISPLAY_LEN));

	GetDlgItemText(IDADDR, strText);

	if( !strcmp(strText, "µç»°±¡"))
	{
		for(i = 0; i < IP_TYPE_MAX; i++)
			m_ctlIPType.AddString((char *)(g_cIpTypeDisplay_GB[0] + i * IPTYPE_DISPLAY_LEN));

		for(i = 0; i < UPGRADE_TYPE_MAX; i++)
			m_ctlUpgradeType.AddString((char *)(g_cUpgradeTypeDisplay_GB[0] + i * UPGRADE_TYPE_GB_DISPLAY_LEN));

		for(i = 0; i < TIME_ZONE_MAX; i++)
			m_ctlTimeZone.AddString((char *)(g_cTimezoneDisplay_GB[0] + i * TIMEZONE_GB_DISPLAY_LEN));
	}
	else if (!strcmp(strText, "Phone Book"))
	{
		for(i = 0; i < IP_TYPE_MAX; i++)
			m_ctlIPType.AddString((char *)(g_cIpTypeDisplay[0] + i * IPTYPE_DISPLAY_LEN));

		for(i = 0; i < UPGRADE_TYPE_MAX; i++)
			m_ctlUpgradeType.AddString((char *)(g_cUpgradeTypeDisplay[0] + i * UPGRADE_TYPE_DISPLAY_LEN));

		for(i = 0; i < TIME_ZONE_MAX; i++)
			m_ctlTimeZone.AddString((char *)(g_cTimezoneDisplay[0] + i * TIMEZONE_DISPLAY_LEN));
	}

	/* 0x000000--0x000005: Local MAC address 0x00, 0x09, 0x45, 0x00, 0x00, 0x00 */
	m_strMac1 = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac1"), _T("0x00"));
	m_strMac2 = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac2"), _T("0x09"));
	m_strMac3 = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac3"), _T("0x45"));
	m_strMac4 = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac4"), _T("0x00"));
	m_strMac5 = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac5"), _T("0x00"));
	m_strMac6 = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac6"), _T("0x00"));

	/* 0x000006--0x000009: Local IP address 192, 168, 1, 100 */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("LocalIP"), 0xc0a80164);
	m_ctlLocalIP.SetAddress(uIP);

	/* 0x000162:           Time zone */
    m_ctlTimeZone.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("TimeZone"), 20));
	m_bUseDaylight = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseDaylight"), 0);

	/* 0x000163--0x000166: Time server IP address 210, 59, 157, 10 */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SntpIP"), 0xd23b9d0a);
	m_ctlSntpIP.SetAddress(uIP);

	/* 0x00002b:           Dsp options */
    m_bUseHighRate = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseHighRate"), 1);
    m_bUseVAD = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseVAD"), 1);
	m_bUseAGC = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseAGC"), 0);
	m_bUseAEC = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseAEC"), 1);

	/* 0x00000a:           1 to enable VLAN support, 0 to disable VLAN support */
	m_bVlanEnable = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("VlanEnable"), 0);
	/* 0x00000b--0x00000c:           VLAN ID, range from 0-4095 */
    m_iVlanID = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("VlanID"), 60);

	/* 0x000020--0x000023: Subnet mask */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SubnetMask"), 0xffffff00);
	m_ctlSubnetMask.SetAddress(uIP);

	/* 0x000024--0x000027: Router IP address */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RouterIP"), 0xc0a80101);
	m_ctlRouterIP.SetAddress(uIP);

	/* 0x000028:           1 to use DHCP, 0 to use IP address in 06-09 */
	m_ctlIPType.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("IPType"), 0));

	/* 0x000029:           Initial mic vol */
	m_iMicVol = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("MicVol"), 10);
	m_ctlMicVol.SetRange(0, 15);
	m_ctlMicVol.SetTic(1);

	/* 0x00002a:           Initial speaker vol */
	m_iSpkVol = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SpkVol"), 20);
	m_ctlSpkVol.SetRange(0, 31);
	m_ctlSpkVol.SetTic(1);

	/* 0x00002c--0x00002f: DNS server IP address */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DnsIP"), 0xc0a80101);
	m_ctlDnsIP.SetAddress(uIP);

	/* 0x00003b--0x00003e: Alternate Dns server IP address */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DnsIP2"), 0);
	m_ctlDnsIP2.SetAddress(uIP);

	/* 0x000030:           Jitter size */
    m_iJitterSize = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("JitterSize"), 0);
	
	/* 0x000120:           Codec type, 0 - g729, 1 - g7231, 2 - g711u, 3 - g711a, 4 - gsm, 5 - null */
    m_iCodecType1 = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec1"), 5);
	m_ctlCodecType1.SetCurSel(m_iCodecType1);
    m_iCodecType2 = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec2"), 5);
	m_ctlCodecType2.SetCurSel(m_iCodecType2);
    m_iCodecType3 = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec3"), 5);
	m_ctlCodecType3.SetCurSel(m_iCodecType3);
    m_iCodecType4 = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec4"), 5);
	m_ctlCodecType4.SetCurSel(m_iCodecType4);
    m_iCodecType5 = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec5"), 5);
	m_ctlCodecType5.SetCurSel(m_iCodecType5);
    m_iCodecType6 = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec6"), 5);
	m_ctlCodecType6.SetCurSel(m_iCodecType6);
	
	/* 0x00012f:           iLBC RTP payload type */
    m_iIlbcPType = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("IlbcPayload"), 0);

	/* 0x000035:           Default G.723.1 audio frames in a send-out packet (1-8) */
    m_iG7231Frames = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("G7231Frames"), 1);

	/* 0x000048--0x00004f: Password */
	m_strPassword = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Password"), "1234");

	/* 0x0001d0-0x0001ff: Ftp DNS */
	m_strUpgradeAddr = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("UpgradeAddr"), "");

	/* 0x0000b0--0x0000c7: PPP id */
	m_strPPPoEID = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("PPPoEID"), _T(""));

	/* 0x0000c8--0x0000d7: PPP password */
	m_strPPPoEPin = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("PPPoEPin"), _T(""));

	/* 0x0000f7--0x0000f9: TimeOut0-2 */
	/* 0x0000fb:		    SubVersion Number */
	m_iSubVersion = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SubVersion"), 10);

	/* 0x000161:           Upgrade Type, 0 to disable, 1 - all, 2 - pppid, 3 - account, 4 - phonenumber */
	m_ctlUpgradeType.SetCurSel(AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UpgradeType"), 0));

	UpdateData(FALSE);
	OnSelchangeIptype();
	OnVlanEnable();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPhoneSettingsDlg::GetSettings(BYTE * pSettings)
{
	CString str;
	ULONG uIP;
	int iPort;
	int iVal;

	memcpy(pSettings, g_cSettings, TOTAL_SETTINGS_SIZE);

	/* 0x000000--0x000005: Local MAC address 0x00, 0x09, 0x45, 0x00, 0x00, 0x00 */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac1"), _T("0x00"));
	pSettings[OPTIONS_MAC_ADDR] = (BYTE)IntFromString(str);
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac2"), _T("0x09"));
	pSettings[OPTIONS_MAC_ADDR + 1] = (BYTE)IntFromString(str);
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac3"), _T("0x45"));
	pSettings[OPTIONS_MAC_ADDR + 2] = (BYTE)IntFromString(str);
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac4"), _T("0x00"));
	pSettings[OPTIONS_MAC_ADDR + 3] = (BYTE)IntFromString(str);
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac5"), _T("0x00"));
	pSettings[OPTIONS_MAC_ADDR + 4] = (BYTE)IntFromString(str);
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Mac6"), _T("0x00"));
	pSettings[OPTIONS_MAC_ADDR + 5] = (BYTE)IntFromString(str);

	/* 0x000006--0x000009: Local IP address 192, 168, 1, 100 */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("LocalIP"), 0xc0a80164);
	pSettings[OPTIONS_IP_ADDR] = (BYTE)((uIP>>24)&0xff);
	pSettings[OPTIONS_IP_ADDR + 1] = (BYTE)((uIP>>16)&0xff);
	pSettings[OPTIONS_IP_ADDR + 2] = (BYTE)((uIP>>8)&0xff);
	pSettings[OPTIONS_IP_ADDR + 3] = (BYTE)(uIP&0xff);

	/* 0x000163--0x000166: Time server IP address 210, 59, 157, 10 */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SntpIP"), 0xd23b9d0a);
	pSettings[OPTIONS_NTS_ADDR] = (BYTE)((uIP>>24)&0xff);
	pSettings[OPTIONS_NTS_ADDR + 1] = (BYTE)((uIP>>16)&0xff);
	pSettings[OPTIONS_NTS_ADDR + 2] = (BYTE)((uIP>>8)&0xff);
	pSettings[OPTIONS_NTS_ADDR + 3] = (BYTE)(uIP&0xff);

	/* 0x00000a:           1 to enable VLAN support, 0 to disable VLAN support */
	pSettings[OPTIONS_VLAN_ENABLE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("VlanEnable"), 0);
	/* 0x00000b--0x00000c:           VLAN ID, range from 0-4095 */
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("VlanID"), 0);
	pSettings[OPTIONS_VLAN_ID] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_VLAN_ID+1] = (BYTE)(iPort&0xff);

	/* 0x00000e:           1 to use gatekeeper, 0 to dial IP address directly 1,*/
	pSettings[OPTIONS_USE_GK] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseService"), 0);

	/* 0x00000f:           ring type */
    pSettings[OPTIONS_RING_TYPE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RingType"), 0);

	/* 0x000010--0x00001f: My phone number to use with a gatekeeper */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("PhoneNumber"), _T("100"));
	memcpy((char *)(pSettings+OPTIONS_PHONE_NUMBER), str, PHONE_NUMBER_LEN);

	/* 0x000170--0x00017f: dial number */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DialNumber"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_DIAL_NUMBER), str, DIAL_NUMBER_LEN);

	/* 0x000020--0x000023: Subnet mask */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SubnetMask"), 0xffffff00);
	pSettings[OPTIONS_SUBNET_MASK] = (BYTE)((uIP>>24)&0xff);
	pSettings[OPTIONS_SUBNET_MASK + 1] = (BYTE)((uIP>>16)&0xff);
	pSettings[OPTIONS_SUBNET_MASK + 2] = (BYTE)((uIP>>8)&0xff);
	pSettings[OPTIONS_SUBNET_MASK + 3] = (BYTE)(uIP&0xff);

	/* 0x000024--0x000027: Router IP address */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RouterIP"), 0xc0a80101);
	pSettings[OPTIONS_ROUTER_ADDR] = (BYTE)((uIP>>24)&0xff);
	pSettings[OPTIONS_ROUTER_ADDR + 1] = (BYTE)((uIP>>16)&0xff);
	pSettings[OPTIONS_ROUTER_ADDR + 2] = (BYTE)((uIP>>8)&0xff);
	pSettings[OPTIONS_ROUTER_ADDR + 3] = (BYTE)(uIP&0xff);

	/* 0x000028:           1 to use DHCP, 0 to use IP address in 06-09 */
    pSettings[OPTIONS_IP_TYPE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("IPType"), 0);

	/* 0x000029:           Initial mic vol */
	pSettings[OPTIONS_HANDSET_INPUT] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("MicVol"), 10);

	/* 0x00002a:           Initial speaker vol */
	pSettings[OPTIONS_HANDSET_OUTPUT] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SpkVol"), 20);

	/* 0x00002c--0x00002f: Dns IP address */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DnsIP"), 0xc0a80101);
	pSettings[OPTIONS_DNS_ADDR] = (BYTE)((uIP>>24)&0xff);
	pSettings[OPTIONS_DNS_ADDR + 1] = (BYTE)((uIP>>16)&0xff);
	pSettings[OPTIONS_DNS_ADDR + 2] = (BYTE)((uIP>>8)&0xff);
	pSettings[OPTIONS_DNS_ADDR + 3] = (BYTE)(uIP&0xff);

	/* 0x00003b--0x00003e: Alternate Dns server IP address */
	uIP = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DnsIP2"), 0);
	pSettings[OPTIONS_DNS_ADDR2] = (BYTE)((uIP>>24)&0xff);
	pSettings[OPTIONS_DNS_ADDR2 + 1] = (BYTE)((uIP>>16)&0xff);
	pSettings[OPTIONS_DNS_ADDR2 + 2] = (BYTE)((uIP>>8)&0xff);
	pSettings[OPTIONS_DNS_ADDR2 + 3] = (BYTE)(uIP&0xff);

	/* 0x000031:           Number to ring before auto answer */
    pSettings[OPTIONS_RING_NUMBER] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RingTimeout"), 5);

	/* 0x00015f:           Type of service */
    pSettings[OPTIONS_RTP_TOS] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Tos"), 0);

	/* 0x00003a:           Dtmf Payload Type */
    pSettings[OPTIONS_DTMF_PTYPE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DtmfPayloadType"), 0);

	/* 0x00012f:           iLBC RTP payload type */
    pSettings[OPTIONS_ILBC_PTYPE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("IlbcPayload"), 0);

	/* 0x000030:           Jitter size */
    pSettings[OPTIONS_JITTER_SIZE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("JitterSize"), 0);

	/* 0x0000f6:           Register TTL high*/
	/* 0x0000f7:           Register TTL low*/
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RegisterTTL"), 0);
	pSettings[OPTIONS_REGISTER_TTL] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_REGISTER_TTL+1] = (BYTE)(iPort&0xff);

	/* 0x0000f8:           NAT hint TTL high*/
	/* 0x0000f9:           NAT hint TTL low*/
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("NatTTL"), 0);
	pSettings[OPTIONS_NAT_TTL] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_NAT_TTL+1] = (BYTE)(iPort&0xff);

	/* 0x000032:           1 to use e.164 log on with gatekeeper, 0 to use h323-id */
    pSettings[OPTIONS_LOCAL_ALIAS] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("LocalAlias"), 1);

	/* 0x000033:           Initial ring vol */
	pSettings[OPTIONS_HANDFREE_OUTPUT] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RingVol"), 31);

	/* 0x000033:           Initial ring vol */
	pSettings[OPTIONS_HANDFREE_INPUT] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SpeakerIn"), 15);

	/* 0x000120:           Codec type, 0 - g729, 1 - g7231, 2 - g711u, 3 - g711a, 4 - gsm, 5 - null */
    pSettings[OPTIONS_CODEC_TYPE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec1"), 0);
    pSettings[OPTIONS_CODEC_TYPE+1] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec2"), 1);
    pSettings[OPTIONS_CODEC_TYPE+2] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec3"), 2);
    pSettings[OPTIONS_CODEC_TYPE+3] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec4"), 3);
    pSettings[OPTIONS_CODEC_TYPE+4] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec5"), 4);
    pSettings[OPTIONS_CODEC_TYPE+5] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Codec6"), 5);

	/* 0x000035:           Default G.723.1 audio frames in a send-out packet (1-8) */
	pSettings[OPTIONS_AUDIO_FRAMES] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("G7231Frames"), 1);

	/* 0x000036:           Q931 source port high
	   0x000037:           Q931 source port low */
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Q931Port"), 1720);
	pSettings[OPTIONS_Q931_PORT] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_Q931_PORT+1] = (BYTE)(iPort&0xff);

	/* 0x00003f:           Use built in H.323 proxy support */
    pSettings[OPTIONS_NAT_TRAVERSAL] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("NatTraversal"), 0);

	/* 0x000046:		   1 to use fast start, 0 to bring normal h245 */
	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseFaststart"), 0);
	if (iVal == 1)
	{
		Sys_iCallType |= FAST_START_OR;
	}
	else if (iVal == 0)
	{
		Sys_iCallType &= FAST_START_AND;
	}
	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("SipPrack"), 0);
	if (iVal == 1)
	{
		Sys_iCallType |= SIP_PRACK_OR;
	}
	else if (iVal == 0)
	{
		Sys_iCallType &= SIP_PRACK_AND;
	}
	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RemoteDialplan"), 0);
	if (iVal == 1)
	{
		Sys_iCallType |= REMOTE_DIALPLAN_OR;
	}
	else if (iVal == 0)
	{
		Sys_iCallType &= REMOTE_DIALPLAN_AND;
	}
	pSettings[OPTIONS_CALL_TYPE] = Sys_iCallType;

	/* 0x000047:		   DTMF method, 0 - h245 string, 1 - h245 signal, 2 - Q931 Keypad, 3 - RFC2833 */	
    pSettings[OPTIONS_DTMF_TYPE] = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DTMFType"), 0);
	
	/* 0x00002b:           Dsp options */
	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseHighRate"), 0);
	if (iVal == 1)
	{
		Dsp_iOptions |= USE_HIGH_RATE_OR;
	}
	else if (iVal == 0)
	{
		Dsp_iOptions &= USE_HIGH_RATE_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseVAD"), 0);
	if (iVal == 1)
	{
		Dsp_iOptions |= USE_VAD_OR;
	}
	else if (iVal == 0)
	{
		Dsp_iOptions &= USE_VAD_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseAGC"), 0);
	if (iVal == 1)
	{
		Dsp_iOptions |= USE_AGC_OR;
	}
	else if (iVal == 0)
	{
		Dsp_iOptions &= USE_AGC_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseAEC"), 0);
	if (iVal == 1)
	{
		Dsp_iOptions |= USE_AEC_OR;
	}
	else if (iVal == 0)
	{
		Dsp_iOptions &= USE_AEC_AND;
	}
	pSettings[OPTIONS_DSP_OPTION] = Dsp_iOptions;

	/* 0x000150:		   Forward type, 0 - none, 1 - always, 2 - busy, 3 - no answer */	 
	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdAlways"), 0);
	if (iVal == 1)
	{
		Sys_iForwardType |= FORWARD_ALWAYS_OR;
	}
	else if (iVal == 0)
	{
		Sys_iForwardType &= FORWARD_ALWAYS_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdBusy"), 0);
	if (iVal == 1)
	{
		Sys_iForwardType |= FORWARD_BUSY_OR;
	}
	else if (iVal == 0)
	{
		Sys_iForwardType &= FORWARD_BUSY_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdNoanswer"), 0);
	if (iVal == 1)
	{
		Sys_iForwardType |= FORWARD_NOANSWER_OR;
	}
	else if (iVal == 0)
	{
		Sys_iForwardType &= FORWARD_NOANSWER_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("FwdPoweroff"), 0);
	if (iVal == 1)
	{
		Sys_iForwardType |= FORWARD_POWEROFF_OR;
	}
	else if (iVal == 0)
	{
		Sys_iForwardType &= FORWARD_POWEROFF_AND;
	}

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("CallWaiting"), 0);
	if (iVal == 1)
	{
		Sys_iForwardType |= CALL_WAITING_OR;
	}
	else if (iVal == 0)
	{
		Sys_iForwardType &= CALL_WAITING_AND;
	}
	pSettings[OPTIONS_FWD_TYPE] = Sys_iForwardType;

	/* 0x000162:		   Time zone and use daylight */
	Sys_iTimeZone = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("TimeZone"), 0);

	iVal = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseDaylight"), 0);
	if (iVal == 1)
	{
		Sys_iTimeZone |= TIMEZONE_DAYLIGHT_OR;
	}
	else if (iVal == 0)
	{
		Sys_iTimeZone &= TIMEZONE_DAYLIGHT_AND;
	}
	pSettings[OPTIONS_TIME_ZONE] = Sys_iTimeZone;

	/* 0x000140--0x00014f: forward number */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("ForwardNumber"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_FWD_NUMBER), str, FORWARD_NUMBER_LEN);

	/* 0x000048--0x00004f: Password */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Password"), "1234");
	memcpy((char *)(pSettings+OPTIONS_PASSWORD), str, MAX_PASSWORD_LEN);

	/* 0x000130--0x00013f: Supervisor Password */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("SuperPassword"), "12345678");
	memcpy((char *)(pSettings+OPTIONS_SUPER_PASSWORD), str, MAX_SUPERPASSWORD_LEN);

	/* 0x0001a0--0x0001cf: Service DNS */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("ServiceDns"), "");
	memcpy((char *)(pSettings+OPTIONS_SERVICE_DNS), str, MAX_DNS_ADDR_LEN);

	/* 0x0001e0-0x0001ff: NAT Server Address */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("NatAddr"), "");
	memcpy((char *)(pSettings+OPTIONS_NAT_DNS), str, MAX_DNS_ADDR_LEN);

	/* 0x0001d0--0x0001ff: ftp DNS */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("UpgradeAddr"), "");
	memcpy((char *)(pSettings+OPTIONS_UPGRADE_DNS), str, MAX_DNS_ADDR_LEN);

	/* 0x000058--0x00005b: Country Code; maximum length 4 digits */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("IDDCode"), _T("00"));
	memcpy((char *)(pSettings+OPTIONS_COUNTRY_CODE), str, MAX_COUNTRY_CODE_LEN);
	
	/* 0x00005c--0x00005e: International Prefix: maximum length 3 digits */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("IDDPrefix"), _T("86"));
	memcpy((char *)(pSettings+OPTIONS_INTL_PREFIX), str, MAX_INTL_PREFIX_LEN);

	/* 0x00005f:		   1 to use dial plan, 0 to ignore */
	pSettings[OPTIONS_USE_DIALPLAN] = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseDialPlan"), 0);

	/* 0x000060--0x000063: Area Code: maximum length 4 digits */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DDDCode"), _T("0"));
	memcpy((char *)(pSettings+OPTIONS_AREA_CODE), str, MAX_AREA_CODE_LEN);

	/* 0x000064--0x000066: Longprefix: maximum length 3 digits */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DDDPrefix"), _T("10"));
	memcpy((char *)(pSettings+OPTIONS_LONG_PREFIX), str, MAX_LONG_PREFIX_LEN);

	/* 0x000068:		   1 - inner line, 0 - direct dial, default 0 */ 
    pSettings[OPTIONS_INNER_LINE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("InnerLine"), 0);

	/* 0x00015f:		   Dual mode, 0 to disable, 1 pstn first, 2 ip first */ 
    pSettings[OPTIONS_DUAL_MODE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("DualMode"), 0);

	/* 0x000069--0x00006a: to access local outer line */ 
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("DualModePrefix"), _T("0"));
	memcpy((char *)(pSettings+OPTIONS_DUALMODE_PREFIX), str, MAX_DUALMODE_PREFIX_LEN);

	/* 0x00006b--0x00006c: to access long-distance outer line */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("InnerLinePrefix"), _T("0"));
	memcpy((char *)(pSettings+OPTIONS_INNERLINE_PREFIX), str, MAX_INNERLINE_PREFIX_LEN);

	/* 0x00006d:		   1 to use calling card, 0 to direct dial */	 
	pSettings[OPTIONS_SERVICE_TYPE] = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("ServiceType"), 0);

	/* 0x00006e--0x00007d: calling card account */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Account"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_ACCOUNT), str, MAX_ACCOUNT_LEN);
	/* 0x00007e--0x00008d: calling card PIN */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("Pin"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_PIN), str, MAX_PIN_LEN);

	/* 0x0000b0--0x0000c7: PPP id */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("PPPoEID"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_PPP_ID), str, PPP_ID_LEN);

	/* 0x0000c8--0x0000d7: PPP password */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("PPPoEPin"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_PPP_PASSWORD), str, PPP_PASSWORD_LEN);

	/* 0x0000d8--0x0000ef: GKID */
	str = AfxGetApp()->GetProfileString(_T("PhoneSettings"), _T("ServiceID"), _T(""));
	memcpy((char *)(pSettings+OPTIONS_SERVICE_ID), str, MAX_SERVICE_ID_LEN);

	/* 0x0000f0:           Ras source port high
	   0x0000f1:           Ras source port low */
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RasPort"), 1720);
	pSettings[OPTIONS_RAS_PORT] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_RAS_PORT+1] = (BYTE)(iPort&0xff);

	/* 0x0000f2:           H245 source port high
	   0x0000f3:           H245 source port low */
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("H245Port"), 1722);
	pSettings[OPTIONS_H245_PORT] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_H245_PORT+1] = (BYTE)(iPort&0xff);

	/* 0x0000f4:           Rtp source port high 
	   0x0000f5:           Rtp source port low */
	iPort = AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("RtpPort"), 1722);
	pSettings[OPTIONS_RTP_PORT] = (BYTE)((iPort>>8)&0xff);
	pSettings[OPTIONS_RTP_PORT+1] = (BYTE)(iPort&0xff);

	/* 0x0000fa:			Dhcp or PPPoE retries */
	pSettings[OPTIONS_NETWORK_RETRY] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("NetworkRetry"), 60);

	/* 0x0000fe:           1 to output debug information by udp, 0 to output nothing */
    pSettings[OPTIONS_OUTPUT_DEBUG] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("Output"), 0);

	/* 0x000161:           Upgrade Type, 0 to disable, 1 - all, 2 - pppid, 3 - account, 4 - phonenumber */
    pSettings[OPTIONS_UPGRADE_TYPE] = (BYTE)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UpgradeType"), 0);

	/* 0x0000ff:		   1 to use digit map */
	pSettings[OPTIONS_USE_DIGITMAP] = (BOOL)AfxGetApp()->GetProfileInt(_T("PhoneSettings"), _T("UseDigitMap"), 1);
}

void CPhoneSettingsDlg::OnSelchangeIptype() 
{
	BOOL m_bUseDHCP, m_bUsePPPoE;

	switch(m_ctlIPType.GetCurSel())
	{
	case IP_TYPE_STATIC:
		m_bUseDHCP = FALSE;
		m_bUsePPPoE = FALSE;
		break;
	case IP_TYPE_DHCP:
		m_bUseDHCP = TRUE;
		m_bUsePPPoE = FALSE;
		break;
	case IP_TYPE_PPPOE:
		m_bUseDHCP = TRUE;
		m_bUsePPPoE = TRUE;
		break;
	}

	UpdateData();
	m_ctlLocalIP.EnableWindow(!m_bUseDHCP);
	m_ctlRouterIP.EnableWindow(!m_bUseDHCP);
	m_ctlSubnetMask.EnableWindow(!m_bUseDHCP);	
	if(m_ctlIPType.GetCurSel() == IP_TYPE_PPPOE)
	{
		m_ctlDnsIP.EnableWindow(m_bUseDHCP);
		m_ctlDnsIP2.EnableWindow(m_bUseDHCP);
	}
	else
	{
		m_ctlDnsIP.EnableWindow(!m_bUseDHCP);
		m_ctlDnsIP2.EnableWindow(!m_bUseDHCP);
	}

	GetDlgItem(IDC_PPPOEID)->EnableWindow(m_bUsePPPoE);
	GetDlgItem(IDC_PPPOEPIN)->EnableWindow(m_bUsePPPoE);
}

void CPhoneSettingsDlg::OnLoadAddr() 
{
	CPhoneAddrDlg dlg;

//	SaveSettings();
	memcpy(dlg.m_pInfo, g_cSettings, TOTAL_SETTINGS_SIZE);

	if (dlg.DoModal() == IDOK)
	{
		memcpy(g_cSettings, dlg.m_pInfo, TOTAL_SETTINGS_SIZE);
	}	
}

void CPhoneSettingsDlg::OnProtocol() 
{
	CH323SettingsDlg	H323dlg;
	CSipSettingsDlg		Sipdlg;
	CMgcpSettingsDlg	Mgcpdlg;
	CIax2SettingsDlg	Iax2dlg;
	CN2PSettingsDlg		N2pdlg;
	CWPSettingsDlg		Wpdlg;
	CEPNSettingsDlg		Epndlg;

	char * pFlag;
	pFlag = g_cSettings + 256;

	switch(Sys_iProtocol)
	{
	case PROTOCOL_H323:
		H323dlg.DoModal();
		break;

	case PROTOCOL_SIP:
		Sipdlg.DoModal();
		break;

	case PROTOCOL_MGCP:
		Mgcpdlg.DoModal();
		break;

	case PROTOCOL_N2P:
		N2pdlg.DoModal();
		break;

	case PROTOCOL_IAX2:
		Iax2dlg.DoModal();
		break;

	case PROTOCOL_WP:
		Wpdlg.DoModal();
		break;

	case PROTOCOL_EPN:
		Epndlg.DoModal();
		break;
	}
	pFlag[FLAGS_PROTOCOL] = Sys_iProtocol;
}

void CPhoneSettingsDlg::OnAttribute() 
{
	CGatewaySettingsDlg		Gatewaydlg;	
	CAdvancedSettingsDlg	Advanceddlg;	

	char * pFlag;
	pFlag = g_cSettings + 256;

	Sys_iDeviceAttr = (int)pFlag[FLAGS_DEVICE_ATTR];

	if((Sys_iDeviceAttr & DEVICE_ATTR_DUALMODE))
	{
		Gatewaydlg.DoModal();
	}
	else if((Sys_iDeviceAttr & DEVICE_ATTR_SPEAKER))
	{
		Advanceddlg.DoModal();
	}
}

void CPhoneSettingsDlg::OnDialplan() 
{
	CDialPlanSettingsDlg	DialPlandlg;

	if(Sys_iProtocol != PROTOCOL_MGCP)
	{
		DialPlandlg.DoModal();
	}
}

void CPhoneSettingsDlg::OnVlanEnable() 
{
	BOOL m_bID;

	UpdateData();

	if (m_bVlanEnable == 0)	
		m_bID = FALSE;
	else
		m_bID = TRUE;

	GetDlgItem(IDC_VLAN_ID)->EnableWindow(m_bID);
}
