#include "stdafx.h"

#define _GDI_CONST
#include "..\\inc\\gdiconst.h"
#include "CovertStr.h"
#include "..\\inc\\sdram.h"
#include "..\\inc\\p_strs.h"
#include "..\\inc\\p_apps.h"

CString AddString(CString cStr)
{
	CString strwrite;
	strwrite += "protocol = ";
	strwrite += cStr;
	strwrite += "\n";
	return strwrite;
}

CString _WriteFlagVal(char * p, CString strName, int iFlag)
{
	char cTemp[10];
	CString cWriteStr;

	itoa(((p[0] & iFlag) ? 1 : 0), cTemp, 10);

	strName = strName + " = ";
	cWriteStr += strName;
	cWriteStr += cTemp;
	cWriteStr += "\n";
	return cWriteStr;
}

void ConvertSettings(char * pSettings, CString& strwrite, int iMaxLen)
{
	char buffer[20];
	char * pFlag;
	int iTemp;
	int iDeviceAttr, iProtocol;


	pFlag = (char *)(pSettings + 256);
	iProtocol = (int)pFlag[FLAGS_PROTOCOL];
	iDeviceAttr = (int)pFlag[FLAGS_DEVICE_ATTR];

	strwrite = "[SETTING]\n";

	switch(iProtocol)
	{
	case PROTOCOL_H323:
		strwrite += AddString("h323");
		break;

	case PROTOCOL_SIP:
		strwrite += AddString("sip");
		break;

	case PROTOCOL_MGCP:
		strwrite += AddString("mgcp");
		break;

	case PROTOCOL_N2P:
		strwrite += AddString("n2p");
		break;

	case PROTOCOL_IAX2:
		strwrite += AddString("iax2");
		break;

	case PROTOCOL_EPN:
		strwrite += AddString("enp");
		break;

	case PROTOCOL_WP:
		strwrite += AddString("wp");
		break;
	}

	itoa(iDeviceAttr, buffer, 10);
	strwrite += "deviceattr = ";
	strwrite += buffer;
	strwrite += "\n";

	/*  Network Settings  */
	strwrite += _WriteString((char *)(pSettings + OPTIONS_PPP_ID), "pppid", PPP_ID_LEN);

	/*  Audio Settings  */
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_CODEC_TYPE), "codec1");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 1), "codec2");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 2), "codec3");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 3), "codec4");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 4), "codec5");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 5), "codec6");

	strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_DSP_OPTION), "6.3k", USE_HIGH_RATE_OR);
	strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_DSP_OPTION), "vad", USE_VAD_OR);
	strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_DSP_OPTION), "agc", USE_AGC_OR);
	strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_DSP_OPTION), "aec", USE_AEC_OR);

	strwrite += _WriteVal((char *)(pSettings + OPTIONS_ILBC_PTYPE), "ilbcpayload");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_AUDIO_FRAMES), "audioframes");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_JITTER_SIZE), "jittersize");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_HANDSET_INPUT), "handsetin");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_HANDSET_OUTPUT), "handsetout");

	if((iDeviceAttr & DEVICE_ATTR_SPEAKER))
	{
		/*  Only Phone  */
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_HANDFREE_INPUT), "speakerin");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_HANDFREE_OUTPUT), "speakerout");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_RING_TYPE), "ringtype");
	}
	else if((iDeviceAttr & DEVICE_ATTR_DUALMODE))
	{
		/*  Only _USE_FAKE_FXO  */
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_DUAL_MODE), "dualmode");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_DUALMODE_PREFIX), "dualmodeprefix", MAX_DUALMODE_PREFIX_LEN);
	}

	/*  Dialplan Settings  */
	if(iProtocol != PROTOCOL_MGCP)
	{
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_USE_DIALPLAN), "dialplan");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_DIAL_NUMBER), "dialnumber", DIAL_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_AREA_CODE), "dddcode", MAX_AREA_CODE_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_LONG_PREFIX), "dddprefix", MAX_LONG_PREFIX_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_COUNTRY_CODE), "iddcode", MAX_COUNTRY_CODE_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_INTL_PREFIX), "iddprefix", MAX_INTL_PREFIX_LEN);

		strwrite += _WriteVal((char *)(pSettings + OPTIONS_INNER_LINE), "innerline");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_INNERLINE_PREFIX), "innerlineprefix", MAX_INNERLINE_PREFIX_LEN);

		strwrite += _WriteVal((char *)(pSettings + OPTIONS_USE_DIGITMAP), "digitmap");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_FWD_NUMBER), "fwdnumber", FORWARD_NUMBER_LEN);

		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_FWD_TYPE), "fwdalways", FORWARD_ALWAYS_OR);
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_FWD_TYPE), "fwdbusy", FORWARD_BUSY_OR);
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_FWD_TYPE), "fwdnoanswer", FORWARD_NOANSWER_OR);
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_FWD_TYPE), "fwdpoweroff", FORWARD_POWEROFF_OR);
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_FWD_TYPE), "callwaiting", CALL_WAITING_OR);

		strwrite += _WriteVal((char *)(pSettings + OPTIONS_RING_NUMBER), "answer");
	}

	/*  Other Settings */
	strwrite += _WriteString((char *)(pSettings + OPTIONS_SUPER_PASSWORD), "superpassword", MAX_SUPERPASSWORD_LEN);
	strwrite += _WriteString((char *)(pSettings + OPTIONS_PASSWORD), "password", MAX_PASSWORD_LEN);
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_OUTPUT_DEBUG), "debug");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_UPGRADE_TYPE), "upgradetype");
	strwrite += _WriteString((char *)(pSettings + OPTIONS_UPGRADE_DNS), "upgradeaddr", MAX_DNS_ADDR_LEN);
	strwrite += _WriteIPAddr((unsigned char *)(pSettings + OPTIONS_NTS_ADDR), "sntpip");


	iTemp = (int)pSettings[OPTIONS_TIME_ZONE];
	itoa((iTemp & TIMEZONE_DAYLIGHT_AND), buffer, 10);
	strwrite += "timezone = ";
	strwrite += buffer;
	strwrite += "\n";
	strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_TIME_ZONE), "daylight", TIMEZONE_DAYLIGHT_OR);
	
	/*  Protocol Settings  */
	strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_REGISTER_TTL), "registerttl");
	strwrite += _WriteVal((char *)(pSettings + OPTIONS_DTMF_TYPE), "dtmf");

	switch (iProtocol)
	{
	case PROTOCOL_H323:
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_USE_GK), "service");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_SERVICE_TYPE), "servicetype");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "serviceaddr", MAX_DNS_ADDR_LEN);
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_NAT_TRAVERSAL), "nattraversal");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_NAT_DNS), "nataddr", MAX_DNS_ADDR_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_NAT_TTL), "natttl");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PHONE_NUMBER), "phonenumber", PHONE_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "account", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "pin", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RAS_PORT), "rasport");
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_Q931_PORT), "q931port");
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_H245_PORT), "h245port");
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RTP_PORT), "rtpport");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_RTP_TOS), "tos");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_LOCAL_ALIAS), "localtype");
		break;

	case PROTOCOL_SIP:
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_USE_GK), "service");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_SERVICE_TYPE), "servicetype");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "sipproxy", MAX_DNS_ADDR_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_ID), "domain", MAX_SERVICE_ID_LEN);
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_NAT_TRAVERSAL), "nattraversal");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_NAT_DNS), "nataddr", MAX_DNS_ADDR_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_NAT_TTL), "natttl");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PHONE_NUMBER), "phonenumber", PHONE_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "account", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "pin", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RAS_PORT), "registerport");
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RTP_PORT), "rtpport");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_RTP_TOS), "tos");
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_CALL_TYPE), "outboundproxy", FAST_START_OR);
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_CALL_TYPE), "prack", SIP_PRACK_OR);
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_DTMF_PTYPE), "dtmfpayload");
		break;
	
	case PROTOCOL_MGCP:
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_SERVICE_TYPE), "servicetype");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "caaddr", MAX_DNS_ADDR_LEN);
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_NAT_TRAVERSAL), "nattraversal");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_NAT_DNS), "nataddr", MAX_DNS_ADDR_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_NAT_TTL), "natttl");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PHONE_NUMBER), "phonenumber", PHONE_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "endpointid", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "domain", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RAS_PORT), "registerport");
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RTP_PORT), "rtpport");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_RTP_TOS), "tos");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_LOCAL_ALIAS), "localtype");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_DTMF_PTYPE), "dtmfpayload");
		break;
	
	case PROTOCOL_N2P:
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "serviceaddr", MAX_DNS_ADDR_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_ID), "serviceid", MAX_SERVICE_ID_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "account", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "pin", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RAS_PORT), "registerport");
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RTP_PORT), "voiceport");
		break;
	
	case PROTOCOL_IAX2:
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_USE_GK), "service");
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "serviceaddr", MAX_DNS_ADDR_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PHONE_NUMBER), "phonenumber", PHONE_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "account", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "pin", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RAS_PORT), "localport");
		strwrite += _WriteFlagVal((char *)(pSettings + OPTIONS_CALL_TYPE), "remotedialplan", REMOTE_DIALPLAN_OR);
		break;
	
	case PROTOCOL_WP:
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "serviceaddr", MAX_DNS_ADDR_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PHONE_NUMBER), "phonenumber", PHONE_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "account", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "pin", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RTP_PORT), "voiceport");
		break;

	case PROTOCOL_EPN:
		strwrite += _WriteString((char *)(pSettings + OPTIONS_SERVICE_DNS), "serviceaddr", MAX_DNS_ADDR_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PHONE_NUMBER), "phonenumber", PHONE_NUMBER_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_ACCOUNT), "account", MAX_ACCOUNT_LEN);
		strwrite += _WriteString((char *)(pSettings + OPTIONS_PIN), "pin", MAX_PIN_LEN);
		strwrite += _WritePort((unsigned char *)(pSettings + OPTIONS_RTP_PORT), "rtpport");
		strwrite += _WriteVal((char *)(pSettings + OPTIONS_RTP_TOS), "tos");
		break;
	}

	strwrite += "[END]\n";
}

BOOL CompareSettings(char * pSettings, CString str)
{
	int iVal, iProtocol;
	char * pFlag;

	pFlag = (char *)(pSettings + 256);
	iProtocol = (int)pFlag[FLAGS_PROTOCOL];

	if (_SetVal((char *)(pFlag + FLAGS_PROTOCOL), str, _cProtocolLabel, PROTOCOL_MAX))
		return TRUE;

	if (_SetVal((char *)(pFlag + FLAGS_DEVICE_ATTR), str, _cDeviceAttrLabel, 255))
		return TRUE;

	/*  Network Settings  */
	if (_SetString((char *)(pSettings + OPTIONS_PPP_ID), str, _cPppIDLabel, PPP_ID_LEN))
		return true;

	/*  Audio Settings  */
	if (_SetVal((char *)(pSettings + OPTIONS_CODEC_TYPE), str, _cCodec1Label, CODEC_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 1), str, _cCodec2Label, CODEC_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 2), str, _cCodec3Label, CODEC_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 3), str, _cCodec4Label, CODEC_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 4), str, _cCodec5Label, CODEC_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_CODEC_TYPE + 5), str, _cCodec6Label, CODEC_MAX))
		return TRUE;

	if (_GetFlag(str, _cHighrateLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_DSP_OPTION] |= USE_HIGH_RATE_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_DSP_OPTION] &= USE_HIGH_RATE_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cVadLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_DSP_OPTION] |= USE_VAD_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_DSP_OPTION] &= USE_VAD_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cAgcLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_DSP_OPTION] |= USE_AGC_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_DSP_OPTION] &= USE_AGC_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cAecLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_DSP_OPTION] |= USE_AEC_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_DSP_OPTION] &= USE_AEC_AND;
		return TRUE;
	}

	if (_SetVal((char *)(pSettings + OPTIONS_ILBC_PTYPE), str, _cIlbcPayLoadLabel, MAX_ILBC_PTYPE))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_AUDIO_FRAMES), str, _cAudioframeLabel, AUDIO_FRAMES_NUM))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_JITTER_SIZE), str, _cJitterSizeLabel, JITTER_SIZE_MAX))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_RING_TYPE), str, _cRingTypeLabel, RING_TYPE_MAX))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_HANDSET_INPUT), str, _cHandsetInputLabel, AC97_MIC_VOL_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_HANDSET_OUTPUT), str, _cHandsetOutputLabel, AC97_SPK_VOL_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_HANDFREE_INPUT), str, _cHandfreeInputLabel, AC97_MIC_VOL_MAX))
		return TRUE;
	if (_SetVal((char *)(pSettings + OPTIONS_HANDFREE_OUTPUT), str, _cHandfreeOutputLabel, AC97_SPK_VOL_MAX))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_DUAL_MODE), str, _cDualModeLabel, DUAL_MODE_MAX))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_DUALMODE_PREFIX), str, _cDualModePrefixLabel, MAX_DUALMODE_PREFIX_LEN))
		return TRUE;

	/*  Dialplan Settings  */
	if (_SetVal((char *)(pSettings + OPTIONS_USE_DIALPLAN), str, _cDialplanLabel, DIALPLAN_MAX))
		return TRUE;
	if (_SetString((char *)(pSettings + OPTIONS_DIAL_NUMBER), str, _cDialNumberLabel, DIAL_NUMBER_LEN))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_AREA_CODE), str, _cAreacodeLabel, MAX_AREA_CODE_LEN))
		return TRUE;
	if (_SetString((char *)(pSettings + OPTIONS_LONG_PREFIX), str, _cDDDprefixLabel, MAX_LONG_PREFIX_LEN))
		return TRUE;
	if (_SetString((char *)(pSettings + OPTIONS_COUNTRY_CODE), str, _cCountrycodeLabel, MAX_COUNTRY_CODE_LEN))
		return TRUE;
	if (_SetString((char *)(pSettings + OPTIONS_INTL_PREFIX), str, _cIDDprefixLabel, MAX_INTL_PREFIX_LEN))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_INNER_LINE), str, _cInnerLineLabel, INNER_LINE_MAX))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_INNERLINE_PREFIX), str, _cInnerLinePrefixLabel, MAX_INNERLINE_PREFIX_LEN))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_USE_DIGITMAP), str, _cDigitMapLabel, CHECKBOX_MAX))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_FWD_NUMBER), str, _cForwardNumberLabel, FORWARD_NUMBER_LEN))
		return TRUE;

	if (_GetFlag(str, _cFwdAlwaysLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_FWD_TYPE] |= FORWARD_ALWAYS_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_FWD_TYPE] &= FORWARD_ALWAYS_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cFwdBusyLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_FWD_TYPE] |= FORWARD_BUSY_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_FWD_TYPE] &= FORWARD_BUSY_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cFwdNoAnswerLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_FWD_TYPE] |= FORWARD_NOANSWER_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_FWD_TYPE] &= FORWARD_NOANSWER_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cFwdPowerOffLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_FWD_TYPE] |= FORWARD_POWEROFF_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_FWD_TYPE] &= FORWARD_POWEROFF_AND;
		return TRUE;
	}
	if (_GetFlag(str, _cCallWaitingLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_FWD_TYPE] |= CALL_WAITING_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_FWD_TYPE] &= CALL_WAITING_AND;
		return TRUE;
	}

	if (_SetVal((char *)(pSettings + OPTIONS_RING_NUMBER), str, _cRingNumberLabel, RING_NUMBER))
		return TRUE;

	/*  Other Settings  */
	if (_SetString((char *)(pSettings + OPTIONS_SUPER_PASSWORD), str, _cSuperPasswordLabel, MAX_SUPERPASSWORD_LEN))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_OUTPUT_DEBUG), str, _cDebugLabel, DEBUG_LEVEL_MAX))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_PASSWORD), str, _cPasswordLabel, MAX_PASSWORD_LEN))
		return TRUE;

	if (_SetVal((char *)(pSettings + OPTIONS_UPGRADE_TYPE), str, _cUpgradeTypeLabel, UPGRADE_TYPE_MAX))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_UPGRADE_DNS), str, _cUpgradeAddrLabel, MAX_DNS_ADDR_LEN))
		return TRUE;

	if (_SetIPAddr((char *)(pSettings + OPTIONS_NTS_ADDR), str, _cNtsIPLabel))
		return TRUE;

	if (_GetFlag(str, _cTimeZoneLabel, TIME_ZONE_MAX) != -1)
	{
		iVal = pSettings[OPTIONS_TIME_ZONE] & TIMEZONE_DAYLIGHT_OR;
		pSettings[OPTIONS_TIME_ZONE] = atoi(_GetStr(str)) + iVal;
		return TRUE;
	}
	if (_GetFlag(str, _cDaylightLabel, CHECKBOX_MAX) != -1)
	{
		iVal = atoi(_GetStr(str));
		if (iVal == 1)
			pSettings[OPTIONS_TIME_ZONE] |= TIMEZONE_DAYLIGHT_OR;
		else if (iVal == 0)
			pSettings[OPTIONS_TIME_ZONE] &= TIMEZONE_DAYLIGHT_AND;
		return TRUE;
	}

	/*  Protocol Settings  */
	if (_SetVal((char *)(pSettings + OPTIONS_USE_GK), str, _cUseGKLabel, CHECKBOX_MAX))
		return TRUE;

	if (_SetPort((char *)(pSettings + OPTIONS_REGISTER_TTL), str, _cRegisterTTLLabel))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_SERVICE_DNS), str, "serviceaddr", MAX_DNS_ADDR_LEN))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_NAT_DNS), str, _cNatAddrLabel, MAX_DNS_ADDR_LEN))
		return TRUE;

	if (_SetPort((char *)(pSettings + OPTIONS_NAT_TTL), str, _cNatTTLLabel))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_PHONE_NUMBER), str, _cPhoneNumberLabel, PHONE_NUMBER_LEN))
		return TRUE;

	if (_SetString((char *)(pSettings + OPTIONS_ACCOUNT), str, "account", MAX_ACCOUNT_LEN))
		return true;

	if (_SetString((char *)(pSettings + OPTIONS_PIN), str, "pin", MAX_PIN_LEN))
		return true;

	if (_SetPort((char *)(pSettings + OPTIONS_RAS_PORT), str, "registerport"))
		return TRUE;
	
	if (_SetVal((char *)(pSettings + OPTIONS_RTP_TOS), str, _cRtpTosLabel, MAX_RTP_TOS_LEN))
		return TRUE;

	switch (iProtocol)
	{
	case PROTOCOL_H323:
		if (_SetVal((char *)(pSettings + OPTIONS_SERVICE_TYPE), str, _cServicetypeLabel, H323_SERVICE_TYPE_MAX))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_NAT_TRAVERSAL), str, _cNatTraversalLabel, H323_NAT_TRAVERSAL_MAX))
			return TRUE;

		if (_SetPort((char *)(pSettings + OPTIONS_RAS_PORT), str, "rasport"))
			return TRUE;

		if (_SetPort((char *)(pSettings + OPTIONS_Q931_PORT), str, "q931port"))
			return TRUE;

		if (_SetPort((char *)(pSettings + OPTIONS_H245_PORT), str, "h245port"))
			return TRUE;

		if (_SetPort((char *)(pSettings + OPTIONS_RTP_PORT), str, "rtpport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_LOCAL_ALIAS), str, _cLocalAliasLabel, H323_LOCAL_TYPE_MAX))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, H323_DTMF_TYPE_MAX))
			return TRUE;
		break;

	case PROTOCOL_SIP:
		if (_SetVal((char *)(pSettings + OPTIONS_SERVICE_TYPE), str, _cServicetypeLabel, SIP_SERVICE_TYPE_MAX))
			return TRUE;

		if (_SetString((char *)(pSettings + OPTIONS_SERVICE_DNS), str, "sipproxy", MAX_DNS_ADDR_LEN))
			return TRUE;

		if (_SetString((char *)(pSettings + OPTIONS_SERVICE_ID), str, "domain", MAX_SERVICE_ID_LEN))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_NAT_TRAVERSAL), str, _cNatTraversalLabel, SIP_NAT_TRAVERSAL_MAX))
			return TRUE;

		if (_SetPort((char *)(pSettings + OPTIONS_RTP_PORT), str, "rtpport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, SIP_DTMF_TYPE_MAX))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_PTYPE), str, _cDtmfPayLoadLabel, MAX_DTMF_PTYPE))
			return TRUE;

		if (_GetFlag(str, "outboundproxy", CHECKBOX_MAX) != -1)
		{
			iVal = atoi(_GetStr(str));
			if (iVal == 1)
				pSettings[OPTIONS_CALL_TYPE] |= FAST_START_OR;
			else if (iVal == 0)
				pSettings[OPTIONS_CALL_TYPE] &= FAST_START_AND;
			return TRUE;
		}

		if (_GetFlag(str, "prack", CHECKBOX_MAX) != -1)
		{
			iVal = atoi(_GetStr(str));
			if (iVal == 1)
				pSettings[OPTIONS_CALL_TYPE] |= SIP_PRACK_OR;
			else if (iVal == 0)
				pSettings[OPTIONS_CALL_TYPE] &= SIP_PRACK_AND;
			return TRUE;
		}
		break;
	
	case PROTOCOL_MGCP:
		if (_SetVal((char *)(pSettings + OPTIONS_SERVICE_TYPE), str, _cServicetypeLabel, MGCP_SERVICE_TYPE_MAX))
			return TRUE;

		if (_SetString((char *)(pSettings + OPTIONS_SERVICE_DNS), str, "caaddr", MAX_DNS_ADDR_LEN))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_NAT_TRAVERSAL), str, _cNatTraversalLabel, MGCP_NAT_TRAVERSAL_MAX))
			return TRUE;

		if (_SetString((char *)(pSettings + OPTIONS_ACCOUNT), str, "endpointid", MAX_ACCOUNT_LEN))
			return true;

		if (_SetString((char *)(pSettings + OPTIONS_PIN), str, "domain", MAX_PIN_LEN))
			return true;

		if (_SetPort((char *)(pSettings + OPTIONS_RTP_PORT), str, "rtpport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_LOCAL_ALIAS), str, _cLocalAliasLabel, MGCP_LOCAL_TYPE_MAX))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, MGCP_DTMF_TYPE_MAX))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_PTYPE), str, _cDtmfPayLoadLabel, MAX_DTMF_PTYPE))
			return TRUE;
		break;
	
	case PROTOCOL_N2P:
		if (_SetString((char *)(pSettings + OPTIONS_SERVICE_ID), str, "serviceid", MAX_SERVICE_ID_LEN))
			return TRUE;

		if (_SetPort((char *)(pSettings + OPTIONS_RTP_PORT), str, "voiceport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, N2P_DTMF_TYPE_MAX))
			return TRUE;
		break;
	
	case PROTOCOL_IAX2:
		if (_SetPort((char *)(pSettings + OPTIONS_RAS_PORT), str, "localport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, DTMF_TYPE_MAX))
			return TRUE;

		if (_GetFlag(str, "remotedialplan", CHECKBOX_MAX) != -1)
		{
			iVal = atoi(_GetStr(str));
			if (iVal == 1)
				pSettings[OPTIONS_CALL_TYPE] |= REMOTE_DIALPLAN_OR;
			else if (iVal == 0)
				pSettings[OPTIONS_CALL_TYPE] &= REMOTE_DIALPLAN_AND;
			return TRUE;
		}
		break;
	
	case PROTOCOL_WP:
		if (_SetPort((char *)(pSettings + OPTIONS_RTP_PORT), str, "voiceport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, DTMF_TYPE_MAX))
			return TRUE;
		break;

	case PROTOCOL_EPN:
		if (_SetPort((char *)(pSettings + OPTIONS_RTP_PORT), str, "rtpport"))
			return TRUE;

		if (_SetVal((char *)(pSettings + OPTIONS_DTMF_TYPE), str, _cDTMFLabel, DTMF_TYPE_MAX))
			return TRUE;
		break;
	}

	return TRUE;
}
