#include "settings.h"
//#include "PalmChip.h"
#include "Ac97.h"
#include "p_dsp.h"
#include "gdiconst.h"
#ifdef VER_IPH_E00
#include "..\main\At_modem.h"
#endif

// save as in lcd.c
#define DISPLAY_ROLL_MAX 25
#define DISPLAY_STRING_MAX	48

static XCHAR _pDisplay[DISPLAY_MAX_CHAR+1];

static XCHAR _pDisplayBackup[DISPLAY_STRING_MAX+1];
static BOOLEAN _bDisplayRoll = FALSE;
static XCHAR _iDisplayRollCount;
static XCHAR _iDisplayRollPos;
static XCHAR _iDisplayRollTotal;


#define MAIN_MENU			0
#define SUB_MENU			1
#define STORE_MENU			2
#define LOAD_MENU			3

#define MENU_NETWORK		0
#define MENU_AUDIO			1
#ifndef CALL_MGCP
#define MENU_DIALPLAN		2
#define MENU_PROTOCOL		3
#define MENU_OTHER			4
#define MENU_ADVANCED		5
#define MENU_SPEEDDIAL		6
#define MENU_DEFAULT		7
#define MENU_STORE			8
#define MENU_LOAD			9
#define MENU_SAVE			10
#define MENU_EXIT			11
#define MAIN_MENU_MAX		12
#else
#define MENU_PROTOCOL		2
#define MENU_OTHER			3
#define MENU_ADVANCED		4
#define MENU_SPEEDDIAL		5
#define MENU_DEFAULT		6
#define MENU_STORE			7
#define MENU_LOAD			8
#define MENU_SAVE			9
#define MENU_EXIT			10
#define MAIN_MENU_MAX		11
#endif

#define STORE_MENU_MAX		4
#define LOAD_MENU_MAX		4

extern BOOLEAN Key_bLetter;
BOOLEAN Key_bInputMode = FALSE;
BOOLEAN Menu_bPasswordOK = FALSE;

static BOOLEAN _bConfirmMode = FALSE;
static BOOLEAN _bSpeedDialName;
static DCHAR _iMenuType;
static DCHAR _iMainMenuItem;
static DCHAR _iStoreMenuItem;
static DCHAR _iLoadMenuItem;
static DSHORT _iSubMenuItem;

//*****VIDA CHANGED*****//
#ifdef NEWUI
static CCHAR _NewUIMenu0[]="0.STATIC 1.DHCP";
static CCHAR _NewUIMenu1[]="2.PPPoE 3.MODEM ";
static CCHAR _NewUIMenu1_NoModem[]="2.PPPoE ";
static CCHAR _ALL_OK[]="SETTING FINISH !!";
BOOLEAN NewUI_bSetup= FALSE;
#define MAX_PASS_INVALID	3
static XCHAR _iPassInvalidCount = 0;
#endif

#define MAIN_MENU_LEN	11
#ifndef CALL_MGCP
static CCHAR _cMainMenu[MAIN_MENU_MAX][MAIN_MENU_LEN] = {
"network", 
"audio",
"dial plan",
"protocol",
"other",
"advanced",
"speed dial",
"default",
"store",
"load",
"save",
"exit"
};
#else
static CCHAR _cMainMenu[MAIN_MENU_MAX][MAIN_MENU_LEN] = {
"network", 
"audio",
"protocol",
"other",
"advanced",
"speed dial",
"default",
"store",
"load",
"save",
"exit"
};
#endif

#define STORE_MENU_LEN	13	
static CCHAR _cStoreMenu[STORE_MENU_MAX][STORE_MENU_LEN] = {
"Store First",
"Store Second",
"Store Third",
"Store Fourth"
};

#define LOAD_MENU_LEN	12	
static CCHAR _cLoadMenu[LOAD_MENU_MAX][LOAD_MENU_LEN] = {
"Load First",
"Load Second",
"Load Third",
"Load Fourth"
};

void OnHandset()
{
}

void OnHookFlash()
{
}

void OnLogoff()
{
}

void DebugString(PXCHAR pOutput)
{
	if (pOutput[0])
	{
	}
}

void DebugCString(PCCHAR pOutput)
{
	if (pOutput[0])
	{
	}
}

void OutputString(USHORT iID)
{
	if (iID)
	{
	}
}

void OutputVal(USHORT iVal, USHORT iPrefixID)
{
	if (iVal)	OutputString(iPrefixID);
}

BOOLEAN IsUpgrading()
{
	return FALSE;
}

BOOLEAN CheckFileVersion(UCHAR iPage)
{
	return (iPage) ? TRUE : FALSE;
}

void _MainMenu()
{
	if (Sys_bLoadDefault && _iMainMenuItem == MENU_DEFAULT)
	{
		DisplayString((PCCHAR)"Undo Default", 0);
	}
	else
	{
		DisplayString(_cMainMenu[_iMainMenuItem], 0);
	}
	DisplayString(g_cSettings, 1);
}

void _StoreMenu()
{
	DisplayString(_cStoreMenu[_iStoreMenuItem], 0);
	DisplayString(g_cSettings, 1);
}

void _LoadMenu()
{
	DisplayString(_cLoadMenu[_iLoadMenuItem], 0);
	DisplayString(g_cSettings, 1);
}

void _Display_IP(PXCHAR pIP, PCCHAR cName)
{
	DisplayString(cName, 0);
	p_IP2String(pIP, _pDisplay);
	p_strcpy_x2x(Key_pBuf, _pDisplay);
	DisplayLine(_pDisplay, 1);
}

void _DisplayBoolean(PCCHAR cName, BOOLEAN bEnable)
{
	DisplayString(cName, 0);
	if (bEnable)
	{
		p_strcpy_c2x(Key_pBuf, "1");
		DisplayString(g_cEnableLabel, 1);
	}
	else
	{
		p_strcpy_c2x(Key_pBuf, "0");
		DisplayString(g_cDisableLabel, 1);
	}
}

void _Display_Val(UCHAR iVal, PCCHAR cName)
{
	DisplayString(cName, 0);
	p_itoa(iVal, _pDisplay, 10);			
	p_strcpy_x2x(Key_pBuf, _pDisplay);
	DisplayLine(_pDisplay, 1);
}

void _DisplayShort(USHORT iVal, PCCHAR cName)
{
	DisplayString(cName, 0);
	p_itoa(iVal, _pDisplay, 10);			
	p_strcpy_x2x(Key_pBuf, _pDisplay);
	DisplayLine(_pDisplay, 1);
}

void _DisplayValRemark(UCHAR iVal, PCCHAR cName, PCCHAR cRemark)
{
	DisplayString(cName, 0);
	p_itoa(iVal, _pDisplay, 10);
	p_strcpy_x2x(Key_pBuf, _pDisplay);
	p_strcat_char2x(_pDisplay, '[');
	p_strcat_c2x(_pDisplay, cRemark);
	p_strcat_char2x(_pDisplay, ']');
	SettingsDisplayRoll(_pDisplay);
	DisplayLine(_pDisplay, 1);
}

void _Display_String(PXCHAR pString, PCCHAR cName)
{
	DisplayString(cName, 0);
	SettingsDisplayRoll(pString);
	DisplayLine(pString, 1);
	p_strcpy_x2x(Key_pBuf, pString);
}

void _Display_Mac(PXCHAR pMac, PCCHAR cName)
{
	DisplayString(cName, 0);
	p_Mac2String(pMac, _pDisplay);
	p_strcpy_x2x(Key_pBuf, _pDisplay);
	SettingsDisplayRoll(_pDisplay);
	DisplayLine(_pDisplay, 1);
}

void SettingsDisplayRoll(PXCHAR pString)
{
	DCHAR iLen;

	iLen = p_strlen_x(pString);
	if (iLen > DISPLAY_MAX_CHAR)
	{
		p_strcpy_x2x(_pDisplayBackup, pString);
		_bDisplayRoll = TRUE;
		_iDisplayRollCount = 0;
		_iDisplayRollPos = 0;
		_iDisplayRollTotal = iLen - DISPLAY_MAX_CHAR;
	}
}

void SettingsStopRoll()
{
	_bDisplayRoll = FALSE;
}

void SettingsHandleTimer()
{
	if (_bDisplayRoll)
	{
		_iDisplayRollCount ++;
		
		if (_iDisplayRollCount == DISPLAY_ROLL_MAX)
		{
			if(_iDisplayRollPos == 0)
				SysSleep(2000000);

			_iDisplayRollCount = 0;
			_iDisplayRollPos ++;
			DisplayLine((PXCHAR)(_pDisplayBackup+_iDisplayRollPos), 1);
			if (_iDisplayRollPos == _iDisplayRollTotal)
				_iDisplayRollPos = 255;
		}
	}
}

UCHAR _SpeedDialPos2Index(USHORT iPos)
{
	DSHORT iVal;

	iVal = iPos;
	iVal -= SPEED_DIAL_BEGIN;
	iVal >>= 5;	// iVal /= SPEED_DIAL_LEN
	return (UCHAR)iVal;
}

void _SubMenu()
{
	DCHAR iIndex;

	if (_iSubMenuItem >= SPEED_DIAL_BEGIN)
	{
		iIndex = _SpeedDialPos2Index(_iSubMenuItem);
		if (_bSpeedDialName)
		{
			DisplayLine(ReadSpeedDial(iIndex*2+1), 1);
			p_strcpy_c2x(_pDisplay, (PCCHAR)"memory");
		}
		else
		{
			DisplayLine(ReadSpeedDial(iIndex*2), 1);
			p_strcpy_c2x(_pDisplay, (PCCHAR)"number");
		}
		p_strcat_char2x(_pDisplay, ':');
		p_itoa(iIndex + 1, (PXCHAR)(_pDisplay + p_strlen_x(_pDisplay)), 10);
		DisplayLine(_pDisplay, 0);
	}
	else
	{
		switch (_iSubMenuItem)
		{
		//Network Settings
		case OPTIONS_IP_TYPE:
			_DisplayValRemark(Sys_iIPType, _cIPTypeLabel, g_cIpTypeDisplay[Sys_iIPType]);
			break;
#ifdef NEWUI	
		case OPTIONS_PPP_ID:	
			_Display_String(PPP_pID, "Dial-Up Account ");
			break;
		case OPTIONS_PPP_PASSWORD:	
			_Display_String(PPP_pPassword, "Dial-Up PIN ");
			break;
		case OPTIONS_IP_ADDR:
			_Display_IP(Local_pIPAddress, "IP Address ");
			break;
		case OPTIONS_SUBNET_MASK:		
			_Display_IP(Local_pSubnetMask, "Subnet Mask ");
			break;
		case OPTIONS_ROUTER_ADDR:		
			_Display_IP(Local_pRouteIP, "Router IP ");
			break;
		case OPTIONS_DNS_ADDR:		
			_Display_IP(Dns_pServerIP, "DNS IP ");
			break;
		case OPTIONS_DNS_ADDR2:
			_Display_IP(Dns_pServerIP2, "DNS2 IP ");
			break;
#else
		case OPTIONS_PPP_ID:	
			_Display_String(PPP_pID, _cPppIDLabel);
			break;
		case OPTIONS_PPP_PASSWORD:	
			_Display_String(PPP_pPassword, _cPppPinLabel);
			break;
		case OPTIONS_IP_ADDR:
			_Display_IP(Local_pIPAddress, _cIPLabel);
			break;
		case OPTIONS_SUBNET_MASK:		
			_Display_IP(Local_pSubnetMask, _cMaskLabel);
			break;
		case OPTIONS_ROUTER_ADDR:		
			_Display_IP(Local_pRouteIP, _cRouterLabel);
			break;
		case OPTIONS_DNS_ADDR:		
			_Display_IP(Dns_pServerIP, _cDNSLabel);
			break;
		case OPTIONS_DNS_ADDR2:
			_Display_IP(Dns_pServerIP2, _cDNSLabel2);
			break;
#endif
		case OPTIONS_MAC_ADDR:
			_Display_Mac(Local_pMacAddress, _cMacLabel);
//			DisplayString(_cMacLabel, 0);
//			p_Mac2String(Local_pMacAddress, _pDisplay);
//			SettingsDisplayRoll(_pDisplay);
//			DisplayLine(_pDisplay, 1);
			break;

		case OPTIONS_VLAN_ENABLE:
			_DisplayBoolean(_cVlan, VLAN_bEnable);
			break;

		case OPTIONS_VLAN_ID:
			_DisplayShort(VLAN_iID, _cVlanID);
			break;

		//Audio Settings
		case OPTIONS_CODEC_TYPE:
			_DisplayValRemark(Sys_pCodecType[0], _cCodec1Label, g_cAudioTypeDisplay[Sys_pCodecType[0]]);
			break;
		case OPTIONS_CODEC_TYPE+1:
			_DisplayValRemark(Sys_pCodecType[1], _cCodec2Label, g_cAudioTypeDisplay[Sys_pCodecType[1]]);
			break;
		case OPTIONS_CODEC_TYPE+2:
			_DisplayValRemark(Sys_pCodecType[2], _cCodec3Label, g_cAudioTypeDisplay[Sys_pCodecType[2]]);
			break;
		case OPTIONS_CODEC_TYPE+3:
			_DisplayValRemark(Sys_pCodecType[3], _cCodec4Label, g_cAudioTypeDisplay[Sys_pCodecType[3]]);
			break;
		case OPTIONS_CODEC_TYPE+4:
			_DisplayValRemark(Sys_pCodecType[4], _cCodec5Label, g_cAudioTypeDisplay[Sys_pCodecType[4]]);
			break;
		case OPTIONS_CODEC_TYPE+5:
			_DisplayValRemark(Sys_pCodecType[5], _cCodec6Label, g_cAudioTypeDisplay[Sys_pCodecType[5]]);
			break;
		case OPTIONS_USE_VAD:
			_DisplayBoolean(_cVadLabel, (Dsp_iOptions & USE_VAD_OR) ? 1 : 0);
			break;
		case OPTIONS_USE_AGC:
			_DisplayBoolean(_cAgcLabel, (Dsp_iOptions & USE_AGC_OR) ? 1 : 0);
			break;
		case OPTIONS_USE_AEC:			
			_DisplayBoolean(_cAecLabel, (Dsp_iOptions & USE_AEC_OR) ? 1 : 0);
			break;
		case OPTIONS_USE_HIGH_RATE:
			_DisplayBoolean(_cHighrateLabel, (Dsp_iOptions & USE_HIGH_RATE_OR) ? 1 : 0);
			break;
		case OPTIONS_ILBC_PTYPE:	
			_Display_Val(Sys_iIlbcPType, _cIlbcPayLoadLabel);
			break;
		case OPTIONS_AUDIO_FRAMES:	
			_Display_Val(Sys_iAudioFrames, _cAudioframeLabel);
			break;
		case OPTIONS_JITTER_SIZE:
			_Display_Val(Sys_iJitterSize, _cJitterSizeLabel);
			break;
		case OPTIONS_HANDFREE_OUTPUT:
			_Display_Val(Ac97_iHandfreeOutputVol, _cHandfreeOutputLabel);
			break;
		case OPTIONS_HANDSET_INPUT:
			_Display_Val(Ac97_iHandsetInputVol, _cHandsetInputLabel);
			break;
		case OPTIONS_RING_TYPE:
			_DisplayValRemark(Task_iRingType, _cRingTypeLabel, g_cRingTypeDisplay[Task_iRingType]);
			break;
		case OPTIONS_HANDFREE_INPUT:
			_Display_Val(Ac97_iHandfreeInputVol, _cHandfreeInputLabel);
			break;
		case OPTIONS_HANDSET_OUTPUT:	
			_Display_Val(Ac97_iHandsetOutputVol, _cHandsetOutputLabel);
			break;
		case OPTIONS_DUAL_MODE:		
			_DisplayValRemark(Sys_iDualMode, _cDualModeLabel, g_cDualModeDisplay[Sys_iDualMode]);
			break;
		case OPTIONS_DUALMODE_PREFIX:		
			_Display_String(Sys_pDualModePrefix, _cDualModePrefixLabel);
			break;

		//Phone Settings
#ifndef CALL_MGCP
		case OPTIONS_USE_DIALPLAN:
			_DisplayValRemark(Sys_iUseDialPlan, _cDialplanLabel, g_cDialplanDisplay[Sys_iUseDialPlan]);
			break;
		case OPTIONS_COUNTRY_CODE:
			_Display_String(Sys_pCountryCode, _cCountrycodeLabel);
			break;
		case OPTIONS_AREA_CODE:	
			_Display_String(Sys_pAreaCode, _cAreacodeLabel);
			break;
		case OPTIONS_INTL_PREFIX:		
			_Display_String(Sys_pIntlPrefix, _cIDDprefixLabel);
			break;
		case OPTIONS_LONG_PREFIX:		
			_Display_String(Sys_pLongPrefix, _cDDDprefixLabel);
			break;
		case OPTIONS_DIAL_NUMBER:	
			_Display_String(Sys_pDialNumber, _cDialNumberLabel);
			break;
		case OPTIONS_INNER_LINE:		
			_DisplayValRemark(Sys_iInnerLine, _cInnerLineLabel, g_cInnerLineDisplay[Sys_iInnerLine]);
			break;
		case OPTIONS_INNERLINE_PREFIX:	
			_Display_String(Sys_pInnerLinePrefix, _cInnerLinePrefixLabel);
			break;
		case OPTIONS_CALL_WAITING:
			_DisplayBoolean(_cCallWaitingLabel, (Sys_iForwardType & CALL_WAITING_OR) ? 1 : 0);
			break;
		case OPTIONS_FWD_NUMBER:
#ifndef NEWUI
			_Display_String(Sys_pForwardNumber, _cForwardNumberLabel);
#else
			_Display_String(Sys_pForwardNumber, "Dial-Up Number");
#endif
			break;
		case OPTIONS_FWD_POWEROFF:
			_DisplayBoolean(_cFwdPowerOffLabel, (Sys_iForwardType & FORWARD_POWEROFF_OR) ? 1 : 0);
			break;
		case OPTIONS_FWD_ALWAYS:
			_DisplayBoolean(_cFwdAlwaysLabel, (Sys_iForwardType & FORWARD_ALWAYS_OR) ? 1 : 0);
			break;
		case OPTIONS_FWD_BUSY:
			_DisplayBoolean(_cFwdBusyLabel, (Sys_iForwardType & FORWARD_BUSY_OR) ? 1 : 0);
			break;
		case OPTIONS_FWD_NOANSWER:
			_DisplayBoolean(_cFwdNoAnswerLabel, (Sys_iForwardType & FORWARD_NOANSWER_OR) ? 1 : 0);
			break;
		case OPTIONS_RING_NUMBER:		
			_Display_Val(Task_iRingNumber, _cRingNumberLabel);
			break;
		case OPTIONS_USE_DIGITMAP:
			_DisplayBoolean(_cDigitMapLabel, Sys_bUseDigitMap);
			break;
#endif

		//Protocol Settings
		case OPTIONS_USE_GK:	
			_DisplayBoolean(_cUseGKLabel, Sys_bUseService);
			break;
		case OPTIONS_REGISTER_TTL:
			_DisplayShort(Sys_iRegisterTTL, _cRegisterTTLLabel);
			break;
		case OPTIONS_SERVICE_TYPE:
#ifdef CALL_H323
			_DisplayValRemark(Sys_iServiceType, _cServicetypeLabel, g_cH323ServiceTypeDisplay[Sys_iServiceType]);
#elif defined CALL_SIP
			_DisplayValRemark(Sys_iServiceType, _cServicetypeLabel, g_cSipServiceTypeDisplay[Sys_iServiceType]);
#elif defined CALL_MGCP
			_DisplayValRemark(Sys_iServiceType, _cServicetypeLabel, g_cMgcpServiceTypeDisplay[Sys_iServiceType]);
#endif
			break;
		case OPTIONS_SERVICE_DNS:	
			_Display_String(Sys_pServiceAddr, _cServiceAddrLabel);
			break;
		case OPTIONS_SERVICE_ID:	
#if defined CALL_SIP || defined CALL_N2P
			_Display_String(Sys_pServiceID, _cGKIDLabel);
#endif
			break;
		case OPTIONS_NAT_TRAVERSAL:	
#ifdef CALL_H323
			_DisplayValRemark(Sys_iNatTraversal, _cNatTraversalLabel, g_cH323NatTraversalDisplay[Sys_iNatTraversal]);
#elif defined CALL_SIP
			_DisplayValRemark(Sys_iNatTraversal, _cNatTraversalLabel, g_cSipNatTraversalDisplay[Sys_iNatTraversal]);
#elif defined CALL_MGCP
			_DisplayValRemark(Sys_iNatTraversal, _cNatTraversalLabel, g_cMgcpNatTraversalDisplay[Sys_iNatTraversal]);
#endif
			break;
		case OPTIONS_NAT_DNS:
			_Display_String(Sys_pNatAddr, _cNatAddrLabel);
			break;
		case OPTIONS_NAT_TTL:
			_DisplayShort(Sys_iNatTTL, _cNatTTLLabel);
			break;
		case OPTIONS_PHONE_NUMBER:
			_Display_String(Sys_pLocalNumber, _cPhoneNumberLabel);
			break;
		case OPTIONS_ACCOUNT:		
			_Display_String(Sys_pAccount, _cCCIDLabel);
			break;
		case OPTIONS_PIN:			
			_Display_String(Sys_pPin, _cCCPINLabel);
			break;
		case OPTIONS_RAS_PORT:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP || defined CALL_N2P || defined CALL_IAX2
			_DisplayShort(Sys_iRegisterPort, _cRasLabel);
#endif
			break;
		case OPTIONS_Q931_PORT:	
#ifdef CALL_H323
			_DisplayShort(Sys_iSignalPort, _cQ931Label);
#endif
			break;
		case OPTIONS_H245_PORT:	
#ifdef CALL_H323
			_DisplayShort(Sys_iControlPort, _cH245Label);
#endif
			break;
		case OPTIONS_RTP_PORT:	
#ifndef CALL_IAX2
			_DisplayShort(Sys_iRtpPort, _cRtpLabel);
#endif
			break;
		case OPTIONS_RTP_TOS:	
			_Display_Val(Rtp_iTos, _cRtpTosLabel);
			break;
		case OPTIONS_LOCAL_ALIAS:		
#ifdef CALL_H323
			_DisplayValRemark(Sys_iLocalType, _cLocalAliasLabel, g_cH323LocalTypeDisplay[Sys_iLocalType]);
#elif defined CALL_MGCP
			_DisplayValRemark(Sys_iLocalType, _cLocalAliasLabel, g_cMgcpLocalTypeDisplay[Sys_iLocalType]);
#endif
			break;
		case OPTIONS_FAST_START:
#ifdef CALL_SIP
			_DisplayBoolean(_cFaststartLabel, (Sys_iCallType & FAST_START_OR) ? 1 : 0);
#endif
			break;
		case OPTIONS_SIP_PRACK:
#ifdef CALL_SIP
			_DisplayBoolean(_cSipPrackLabel, (Sys_iCallType & SIP_PRACK_OR) ? 1 : 0);
#endif
			break;
		case OPTIONS_REMOTE_DIALPLAN:
#ifdef CALL_IAX2
			_DisplayBoolean(_cRemoteDialplanLabel, (Sys_iCallType & REMOTE_DIALPLAN_OR) ? 1 : 0);
#endif
			break;
		case OPTIONS_DTMF_TYPE:	
#ifdef CALL_H323
			_DisplayValRemark(Sys_iDtmfType, _cDTMFLabel, g_cH323DtmfDisplay[Sys_iDtmfType]);
#elif defined CALL_SIP
			_DisplayValRemark(Sys_iDtmfType, _cDTMFLabel, g_cSipDtmfDisplay[Sys_iDtmfType]);
#elif defined CALL_MGCP
			_DisplayValRemark(Sys_iDtmfType, _cDTMFLabel, g_cMgcpDtmfDisplay[Sys_iDtmfType]);
#elif defined CALL_N2P
			_DisplayValRemark(Sys_iDtmfType, _cDTMFLabel, g_cN2pDtmfDisplay[Sys_iDtmfType]);
#elif defined CALL_IAX2 || defined CALL_WP || defined CALL_EPN
			_DisplayValRemark(Sys_iDtmfType, _cDTMFLabel, g_cDtmfDisplay[Sys_iDtmfType]);
#endif
			break;
		case OPTIONS_DTMF_PTYPE:	
			_Display_Val(Sys_iDtmfPayloadType, _cDtmfPayLoadLabel);
			break;

		//Other Settings
		case OPTIONS_PASSWORD:		
			_Display_String(Sys_pPassword, _cPasswordLabel);
			break;
		case OPTIONS_UPGRADE_TYPE:		
			_DisplayValRemark(Sys_iUpgradeType, _cUpgradeTypeLabel, g_cUpgradeTypeDisplay[Sys_iUpgradeType]);
			break;
		case OPTIONS_UPGRADE_DNS:
			_Display_String(Sys_pUpgradeUrl, _cUpgradeAddrLabel);
			break;
		case OPTIONS_NTS_ADDR:
			_Display_IP(Sys_pNtsIP, _cNtsIPLabel);
			break;
		case OPTIONS_TIME_ZONE:
			_DisplayValRemark((Sys_iTimeZone & TIMEZONE_DAYLIGHT_AND), _cTimeZoneLabel, g_cTimezoneDisplay[(Sys_iTimeZone & TIMEZONE_DAYLIGHT_AND)]);
			break;
		case OPTIONS_DAYLIGHT:
			_DisplayBoolean(_cDaylightLabel, (Sys_iTimeZone & TIMEZONE_DAYLIGHT_OR) ? 1 : 0);
			break;

		//Advanced Settings
		case OPTIONS_SUPER_PASSWORD:	
			_Display_String(Sys_pSuperPassword, _cSuperPasswordLabel);
			break;
		case OPTIONS_OUTPUT_DEBUG:	
			_DisplayValRemark(Udp_iOutputDebug, _cDebugLabel, g_cDebugDisplay[Udp_iOutputDebug]);
			break;
		}
	}
}

BOOLEAN _GetBoolean(BOOLEAN bOrig)
{
	DCHAR iVal;

	if(p_strlen_x(Key_pBuf) == 0) return bOrig;

	if (p_strlen_x(Key_pBuf) != 1)	return bOrig;
	iVal = Key_pBuf[0];
	if (iVal == '0')
	{
		return FALSE;
	}
	else if (iVal == '1')
	{
		return TRUE;
	}
	return bOrig;
}

void _GetIP(PXCHAR pIP)
{
	if(p_strlen_x(Key_pBuf) == 0) return;
	p_String2IP(Key_pBuf, pIP);
}

UCHAR _GetVal(UCHAR iOrig, UCHAR iMax)
{
	DCHAR iVal;

	if(p_strlen_x(Key_pBuf) == 0) return iOrig;

	iVal = p_atoi(Key_pBuf, 10);
	if (iVal < iMax)
	{
		return iVal;
	}
	return iOrig;
}

USHORT _GetShort(USHORT iOrig)
{
	DSHORT iPort;

	if(p_strlen_x(Key_pBuf) == 0) return iOrig;

	iPort = iOrig;
	iPort = p_atoi(Key_pBuf, 10);
	return iPort;
}

void _GetString(PXCHAR pString, UCHAR iMax)
{
	if (p_strlen_x(Key_pBuf) > iMax)	return;
	p_strcpy_x2x(pString, Key_pBuf);
}

void _GetMac(PXCHAR pMac)
{
#if defined OEM_MEDIARING || defined OEM_RINGCHAT
	PXCHAR_D pCur;

	pCur = pMac;
	return;
#else
	if (p_strlen_x(Key_pBuf) == 0) return;
	p_String2Mac(Key_pBuf, pMac);
#endif
}

void _SubMenuOK()
{
	DCHAR iIndex, iVal;

//	if(p_strlen_x(Key_pBuf) == 0) return;

	if (_iSubMenuItem >= SPEED_DIAL_BEGIN)
	{
		iIndex = _SpeedDialPos2Index(_iSubMenuItem);
		if (_bSpeedDialName)
		{
			WriteSpeedDial(Key_pBuf, iIndex*2+1);
		}
		else
		{
			WriteSpeedDial(Key_pBuf, iIndex*2);
		}
	}
	else
	{
		switch (_iSubMenuItem)
		{
		//Network Settings
		case OPTIONS_IP_TYPE:
			Sys_iIPType = _GetVal(Sys_iIPType, IP_TYPE_MAX);
			break;
		case OPTIONS_PPP_ID:
			_GetString(PPP_pID, PPP_ID_LEN);
			break;
		case OPTIONS_PPP_PASSWORD:
			_GetString(PPP_pPassword, PPP_PASSWORD_LEN);
			break;
		case OPTIONS_IP_ADDR:
			_GetIP(Local_pIPAddress);
			break;
		case OPTIONS_SUBNET_MASK:
			_GetIP(Local_pSubnetMask);
			break;
		case OPTIONS_ROUTER_ADDR:
			_GetIP(Local_pRouteIP);
			break;
		case OPTIONS_DNS_ADDR:
			_GetIP(Dns_pServerIP);
			break;
		case OPTIONS_DNS_ADDR2:
			_GetIP(Dns_pServerIP2);
			break;
		case OPTIONS_MAC_ADDR:
/*#if !defined(OEM_MEDIARING) && !defined(OEM_RINGCHAT)
			p_String2Mac(Key_pBuf, Local_pMacAddress);
#endif
*/
			_GetMac(Local_pMacAddress);
			break;
		
		case OPTIONS_VLAN_ENABLE:
			VLAN_bEnable = _GetBoolean(VLAN_bEnable);
			break;

		case OPTIONS_VLAN_ID:
			VLAN_iID = _GetShort(VLAN_iID);
			break;

		//Audio Settings			
		case OPTIONS_CODEC_TYPE:
			Sys_pCodecType[0] = _GetVal(Sys_pCodecType[0], CODEC_MAX);
			break;
		case OPTIONS_CODEC_TYPE+1:
			Sys_pCodecType[1] = _GetVal(Sys_pCodecType[1], CODEC_MAX);
			break;
		case OPTIONS_CODEC_TYPE+2:
			Sys_pCodecType[2] = _GetVal(Sys_pCodecType[2], CODEC_MAX);
			break;
		case OPTIONS_CODEC_TYPE+3:
			Sys_pCodecType[3] = _GetVal(Sys_pCodecType[3], CODEC_MAX);
			break;
		case OPTIONS_CODEC_TYPE+4:
			Sys_pCodecType[4] = _GetVal(Sys_pCodecType[4], CODEC_MAX);
			break;
		case OPTIONS_CODEC_TYPE+5:
			Sys_pCodecType[5] = _GetVal(Sys_pCodecType[5], CODEC_MAX);
			break;
		case OPTIONS_USE_VAD:
			iIndex = _GetVal(Dsp_iOptions, 2);
			if (iIndex == 1)
			{
				Dsp_iOptions |= USE_VAD_OR;
			}
			else if (iIndex == 0)
			{
				Dsp_iOptions &= USE_VAD_AND;
			}
			break;
		case OPTIONS_USE_AGC:
			iIndex = _GetVal(Dsp_iOptions, 2);
			if (iIndex == 1)
			{
				Dsp_iOptions |= USE_AGC_OR;
			}
			else if (iIndex == 0)
			{
				Dsp_iOptions &= USE_AGC_AND;
			}
			break;
		case OPTIONS_USE_AEC:
			iIndex = _GetVal(Dsp_iOptions, 2);
			if (iIndex == 1)
			{
				Dsp_iOptions |= USE_AEC_OR;
			}
			else if (iIndex == 0)
			{
				Dsp_iOptions &= USE_AEC_AND;
			}
			break;
		case OPTIONS_USE_HIGH_RATE:
			iIndex = _GetVal(Dsp_iOptions, 2);
			if (iIndex == 1)
			{
				Dsp_iOptions |= USE_HIGH_RATE_OR;
			}
			else if (iIndex == 0)
			{
				Dsp_iOptions &= USE_HIGH_RATE_AND;
			}
			break;
		case OPTIONS_ILBC_PTYPE:
			Sys_iIlbcPType = _GetVal(Sys_iIlbcPType, MAX_DTMF_PTYPE);
			break;
		case OPTIONS_AUDIO_FRAMES:
			Sys_iAudioFrames = _GetVal(Sys_iAudioFrames, AUDIO_FRAMES_NUM);
			break;
		case OPTIONS_JITTER_SIZE:
			Sys_iJitterSize = _GetVal(Sys_iJitterSize, JITTER_SIZE_MAX);
			break;
		case OPTIONS_HANDSET_INPUT:
			Ac97_iHandsetInputVol = _GetVal(Ac97_iHandsetInputVol, AC97_MIC_VOL_MAX+1);
			break;
		case OPTIONS_HANDFREE_INPUT:
			Ac97_iHandfreeInputVol = _GetVal(Ac97_iHandfreeInputVol, AC97_MIC_VOL_MAX+1);
			break;
		case OPTIONS_RING_TYPE:
			Task_iRingType = _GetVal(Task_iRingType, RING_TYPE_MAX);
			break;
		case OPTIONS_HANDSET_OUTPUT:
			Ac97_iHandsetOutputVol = _GetVal(Ac97_iHandsetOutputVol, AC97_SPK_VOL_MAX+1);
			break;
		case OPTIONS_HANDFREE_OUTPUT:
			Ac97_iHandfreeOutputVol = _GetVal(Ac97_iHandfreeOutputVol, AC97_SPK_VOL_MAX+1);
			break;
		case OPTIONS_DUAL_MODE:
			Sys_iDualMode = _GetVal(Sys_iDualMode, DUAL_MODE_MAX);
			break;
		case OPTIONS_DUALMODE_PREFIX:
			_GetString(Sys_pDualModePrefix, MAX_DUALMODE_PREFIX_LEN);
			break;

		//Phone Settings
#ifndef CALL_MGCP
		case OPTIONS_USE_DIALPLAN:
			Sys_iUseDialPlan = _GetVal(Sys_iUseDialPlan, DIALPLAN_MAX);
			break;
		case OPTIONS_COUNTRY_CODE:
			_GetString(Sys_pCountryCode, MAX_COUNTRY_CODE_LEN);
			break;
		case OPTIONS_AREA_CODE:
			_GetString(Sys_pAreaCode, MAX_AREA_CODE_LEN);
			break;
		case OPTIONS_INTL_PREFIX:
			_GetString(Sys_pIntlPrefix, MAX_INTL_PREFIX_LEN);
			break;
		case OPTIONS_LONG_PREFIX:
			_GetString(Sys_pLongPrefix, MAX_LONG_PREFIX_LEN);
			break;
		case OPTIONS_DIAL_NUMBER:	
			_GetString(Sys_pDialNumber, PHONE_NUMBER_LEN-1);
			break;
		case OPTIONS_INNER_LINE:
			Sys_iInnerLine = _GetVal(Sys_iInnerLine, INNER_LINE_MAX);
			break;
		case OPTIONS_INNERLINE_PREFIX:
			_GetString(Sys_pInnerLinePrefix, MAX_INNERLINE_PREFIX_LEN);
			break;
		case OPTIONS_CALL_WAITING:
			iIndex = _GetVal(Sys_iForwardType, 2);
			if (iIndex == 1)
			{
				Sys_iForwardType |= CALL_WAITING_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iForwardType &= CALL_WAITING_AND;
			}
			break;
		case OPTIONS_FWD_NUMBER:
			_GetString(Sys_pForwardNumber, FORWARD_NUMBER_LEN);
			break;
		case OPTIONS_FWD_ALWAYS:
			iIndex = _GetVal(Sys_iForwardType, 2);
			if (iIndex == 1)
			{
				Sys_iForwardType |= FORWARD_ALWAYS_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iForwardType &= FORWARD_ALWAYS_AND;
			}
			break;
		case OPTIONS_FWD_BUSY:
			iIndex = _GetVal(Sys_iForwardType, 2);
			if (iIndex == 1)
			{
				Sys_iForwardType |= FORWARD_BUSY_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iForwardType &= FORWARD_BUSY_AND;
			}
			break;
		case OPTIONS_FWD_POWEROFF:
			iIndex = _GetVal(Sys_iForwardType, 2);
			if (iIndex == 1)
			{
				Sys_iForwardType |= FORWARD_POWEROFF_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iForwardType &= FORWARD_POWEROFF_AND;
			}
			break;
		case OPTIONS_FWD_NOANSWER:
			iIndex = _GetVal(Sys_iForwardType, 2);
			if (iIndex == 1)
			{
				Sys_iForwardType |= FORWARD_NOANSWER_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iForwardType &= FORWARD_NOANSWER_AND;
			}
			break;
		case OPTIONS_RING_NUMBER:
			Task_iRingNumber = _GetVal(Task_iRingNumber, RING_NUM);
			break;
		case OPTIONS_USE_DIGITMAP:
			Sys_bUseDigitMap = _GetBoolean(Sys_bUseDigitMap);
			break;
#endif

		//Protocol Settings
		case OPTIONS_USE_GK:
			Sys_bUseService = _GetBoolean(Sys_bUseService);
			break;
		case OPTIONS_REGISTER_TTL:
			Sys_iRegisterTTL = _GetShort(Sys_iRegisterTTL);
			break;
		case OPTIONS_SERVICE_TYPE:
#ifdef CALL_H323
			Sys_iServiceType = _GetVal(Sys_iServiceType, H323_SERVICE_TYPE_MAX);
#elif defined CALL_SIP
			Sys_iServiceType = _GetVal(Sys_iServiceType, SIP_SERVICE_TYPE_MAX);
#elif defined CALL_MGCP
			Sys_iServiceType = _GetVal(Sys_iServiceType, MGCP_SERVICE_TYPE_MAX);
#endif
			break;
		case OPTIONS_SERVICE_DNS:	
			_GetString(Sys_pServiceAddr, MAX_DNS_ADDR_LEN);
			break;
		case OPTIONS_SERVICE_ID:
			_GetString(Sys_pServiceID, MAX_SERVICE_ID_LEN);
			break;
		case OPTIONS_NAT_TRAVERSAL:
#ifdef CALL_H323
			Sys_iNatTraversal = _GetVal(Sys_iNatTraversal, H323_NAT_TRAVERSAL_MAX);
#elif defined CALL_SIP
			Sys_iNatTraversal = _GetVal(Sys_iNatTraversal, SIP_NAT_TRAVERSAL_MAX);
#elif defined CALL_MGCP
			Sys_iNatTraversal = _GetVal(Sys_iNatTraversal, MGCP_NAT_TRAVERSAL_MAX);
#endif
			break;
		case OPTIONS_NAT_DNS:	
			_GetString(Sys_pNatAddr, MAX_DNS_ADDR_LEN);
			break;
		case OPTIONS_NAT_TTL:
			Sys_iNatTTL = _GetShort(Sys_iNatTTL);
			break;
		case OPTIONS_PHONE_NUMBER:
			_GetString(Sys_pLocalNumber, PHONE_NUMBER_LEN);
			break;
		case OPTIONS_ACCOUNT:
			_GetString(Sys_pAccount, MAX_ACCOUNT_LEN);
			break;
		case OPTIONS_PIN:
			_GetString(Sys_pPin, MAX_PIN_LEN);
			break;
		case OPTIONS_Q931_PORT:
			Sys_iSignalPort = _GetShort(Sys_iSignalPort);
			break;
		case OPTIONS_RAS_PORT:
			Sys_iRegisterPort = _GetShort(Sys_iRegisterPort);
			break;
		case OPTIONS_H245_PORT:
			Sys_iControlPort = _GetShort(Sys_iControlPort);
			break;
		case OPTIONS_RTP_PORT:
			Sys_iRtpPort = _GetShort(Sys_iRtpPort);
			break;
		case OPTIONS_RTP_TOS:
			Rtp_iTos = _GetVal(Rtp_iTos, MAX_RTP_TOS_LEN);
			break;
		case OPTIONS_LOCAL_ALIAS:
#ifdef CALL_H323
			Sys_iLocalType = _GetVal(Sys_iLocalType, H323_LOCAL_TYPE_MAX);
#elif defined CALL_MGCP
			Sys_iLocalType = _GetVal(Sys_iLocalType, MGCP_LOCAL_TYPE_MAX);
#endif
			break;
		case OPTIONS_FAST_START:
#ifdef CALL_SIP
			iIndex = _GetVal(Sys_iCallType, 2);
			if (iIndex == 1)
			{
				Sys_iCallType |= FAST_START_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iCallType &= FAST_START_AND;
			}
#endif
			break;
		case OPTIONS_SIP_PRACK:
			iIndex = _GetVal(Sys_iCallType, 2);
			if (iIndex == 1)
			{
				Sys_iCallType |= SIP_PRACK_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iCallType &= SIP_PRACK_AND;
			}
			break;
		case OPTIONS_REMOTE_DIALPLAN:
#ifdef CALL_IAX2
			iIndex = _GetVal(Sys_iCallType, 2);
			if (iIndex == 1)
			{
				Sys_iCallType |= REMOTE_DIALPLAN_OR;
			}
			else if (iIndex == 0)
			{
				Sys_iCallType &= REMOTE_DIALPLAN_AND;
			}
#endif
			break;
		case OPTIONS_DTMF_TYPE:
#ifdef CALL_H323
			Sys_iDtmfType = _GetVal(Sys_iDtmfType, H323_DTMF_TYPE_MAX);
#elif defined CALL_SIP
			Sys_iDtmfType = _GetVal(Sys_iDtmfType, SIP_DTMF_TYPE_MAX);
#elif defined CALL_MGCP
			Sys_iDtmfType = _GetVal(Sys_iDtmfType, MGCP_DTMF_TYPE_MAX);
#elif defined CALL_N2P
			Sys_iDtmfType = _GetVal(Sys_iDtmfType, N2P_DTMF_TYPE_MAX);
#elif defined CALL_IAX2 || defined CALL_WP || defined CALL_EPN
			Sys_iDtmfType = _GetVal(Sys_iDtmfType, DTMF_TYPE_MAX);
#endif
			break;
		case OPTIONS_DTMF_PTYPE:
			Sys_iDtmfPayloadType = _GetVal(Sys_iDtmfPayloadType, MAX_DTMF_PTYPE);
			break;

		//Other Settings
		case OPTIONS_PASSWORD:
			_GetString(Sys_pPassword, MAX_PASSWORD_LEN);
			break;
		case OPTIONS_UPGRADE_TYPE:
			Sys_iUpgradeType = _GetVal(Sys_iUpgradeType, UPGRADE_TYPE_MAX);
			break;
		case OPTIONS_UPGRADE_DNS:
			_GetString(Sys_pUpgradeUrl, MAX_DNS_ADDR_LEN);
			break;
		case OPTIONS_NTS_ADDR:
			_GetIP(Sys_pNtsIP);
			break;
		case OPTIONS_TIME_ZONE:
			iVal = Sys_iTimeZone & TIMEZONE_DAYLIGHT_OR;
			Sys_iTimeZone = _GetVal(Sys_iTimeZone, TIME_ZONE_MAX);
			Sys_iTimeZone += iVal;
			break;
		case OPTIONS_DAYLIGHT:
			iIndex = _GetVal(Sys_iTimeZone, 2);
			if (iIndex == 1)
			{
				Sys_iTimeZone |= TIMEZONE_DAYLIGHT_OR; 
			}
			else if(iIndex == 0)
			{
				Sys_iTimeZone &= TIMEZONE_DAYLIGHT_AND; 
			}
			break;

		//Advanced Settings
		case OPTIONS_OUTPUT_DEBUG:
			Udp_iOutputDebug = _GetVal(Udp_iOutputDebug, DEBUG_LEVEL_MAX);
			break;
		case OPTIONS_SUPER_PASSWORD:	
			_GetString(Sys_pSuperPassword, MAX_SUPERPASSWORD_LEN);
			break;
		}
	}
}

void _SubMenuUp()
{
	if (_iSubMenuItem >= SPEED_DIAL_BEGIN)
	{
		if (_bSpeedDialName)
		{
			_iSubMenuItem -= SPEED_DIAL_LEN;
			if (_iSubMenuItem < SPEED_DIAL_BEGIN)
			{
				_iSubMenuItem = SPEED_DIAL_BEGIN + SPEED_DIAL_LEN*(SPEED_DIAL_NUM-1);
			}
			_bSpeedDialName = FALSE;
		}
		else
		{
			_bSpeedDialName = TRUE;
		}
	}
	else
	{
		switch (_iSubMenuItem)
		{
		//Network Settings
		case OPTIONS_IP_TYPE:
			if (VLAN_bEnable)
			{
				_iSubMenuItem = OPTIONS_VLAN_ID;
			}
			else
			{
				_iSubMenuItem = OPTIONS_VLAN_ENABLE;
			}
			break;
		case OPTIONS_PPP_ID:	
			switch(Sys_iIPType)
			{
			case IP_TYPE_PPPOE:
#ifdef NEWUI
				break;
#endif
			case IP_TYPE_MODEM:
#ifdef NEWUI
				_iSubMenuItem = OPTIONS_FWD_NUMBER;
#else
				_iSubMenuItem = OPTIONS_IP_TYPE;
#endif
				break;

			default:
				break;
			}
			break;
		case OPTIONS_PPP_PASSWORD:	
			_iSubMenuItem = OPTIONS_PPP_ID;
			break;
		case OPTIONS_IP_ADDR:
			switch(Sys_iIPType)
			{
			case IP_TYPE_PPPOE:
			case IP_TYPE_MODEM:
				_iSubMenuItem = OPTIONS_PPP_PASSWORD;
				break;

			default:
#ifndef NEWUI
				_iSubMenuItem = OPTIONS_IP_TYPE;
#endif
				break;
			}
			break;
		case OPTIONS_SUBNET_MASK:		
			_iSubMenuItem = OPTIONS_IP_ADDR;
			break;
		case OPTIONS_ROUTER_ADDR:		
			_iSubMenuItem = OPTIONS_SUBNET_MASK;
			break;
		case OPTIONS_DNS_ADDR:		
			switch(Sys_iIPType)
			{
			case IP_TYPE_STATIC:
				_iSubMenuItem = OPTIONS_ROUTER_ADDR;
				break;
			case IP_TYPE_PPPOE:
				_iSubMenuItem = OPTIONS_PPP_PASSWORD;
				break;
			}
			break;
		case OPTIONS_DNS_ADDR2:		
			_iSubMenuItem = OPTIONS_DNS_ADDR;
			break;
		case OPTIONS_MAC_ADDR:
			switch(Sys_iIPType)
			{
			case IP_TYPE_DHCP:
				_iSubMenuItem = OPTIONS_IP_TYPE;
				break;
			case IP_TYPE_STATIC:
			case IP_TYPE_PPPOE:
				_iSubMenuItem = OPTIONS_DNS_ADDR2;
				break;
			case IP_TYPE_MODEM:
				_iSubMenuItem = OPTIONS_PPP_PASSWORD;
				break;
			}
			break;

		case OPTIONS_VLAN_ENABLE:
			_iSubMenuItem = OPTIONS_MAC_ADDR;
			break;

		case OPTIONS_VLAN_ID:
			_iSubMenuItem = OPTIONS_VLAN_ENABLE;
			break;

		//Audio Settings
		case OPTIONS_CODEC_TYPE:
#ifdef _USE_FXS
			_iSubMenuItem = OPTIONS_HANDSET_OUTPUT;
#else
			_iSubMenuItem = OPTIONS_HANDFREE_OUTPUT;
#endif
#ifdef _USE_FAKE_FXO
			if(Sys_iDualMode == DUAL_MODE_DISABLE)
				_iSubMenuItem = OPTIONS_DUAL_MODE;
			else
				_iSubMenuItem = OPTIONS_DUALMODE_PREFIX;
#endif
			break;
		case OPTIONS_CODEC_TYPE+1:
			_iSubMenuItem = OPTIONS_CODEC_TYPE;
			break;
		case OPTIONS_CODEC_TYPE+2:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+1;
			break;
		case OPTIONS_CODEC_TYPE+3:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+2;
			break;
		case OPTIONS_CODEC_TYPE+4:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+3;
			break;
		case OPTIONS_CODEC_TYPE+5:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+4;
			break;
		case OPTIONS_USE_VAD:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+5;
			break;
		case OPTIONS_USE_AGC:
			_iSubMenuItem = OPTIONS_USE_VAD;
			break;
		case OPTIONS_USE_AEC:			
			_iSubMenuItem = OPTIONS_USE_AGC;
			break;
		case OPTIONS_USE_HIGH_RATE:
			_iSubMenuItem = OPTIONS_USE_AEC;
			break;
		case OPTIONS_ILBC_PTYPE:
			_iSubMenuItem = OPTIONS_USE_HIGH_RATE;
			break;
		case OPTIONS_AUDIO_FRAMES:	
			_iSubMenuItem = OPTIONS_ILBC_PTYPE;
			break;
		case OPTIONS_JITTER_SIZE:
			_iSubMenuItem = OPTIONS_AUDIO_FRAMES;
			break;
		case OPTIONS_HANDSET_INPUT:
			_iSubMenuItem = OPTIONS_JITTER_SIZE;
			break;
		case OPTIONS_HANDSET_OUTPUT:
			_iSubMenuItem = OPTIONS_HANDSET_INPUT;
			break;
		//DEVICE_ATTR_SPEAKER
		case OPTIONS_RING_TYPE:	
			_iSubMenuItem = OPTIONS_HANDSET_OUTPUT;
			break;
		case OPTIONS_HANDFREE_INPUT:
			_iSubMenuItem = OPTIONS_RING_TYPE;
			break;
		case OPTIONS_HANDFREE_OUTPUT:
			_iSubMenuItem = OPTIONS_HANDFREE_INPUT;
			break;
		//DEVICE_ATTR_DUALMODE
		case OPTIONS_DUAL_MODE:	
			_iSubMenuItem = OPTIONS_HANDSET_OUTPUT;
			break;
		case OPTIONS_DUALMODE_PREFIX:		
			_iSubMenuItem = OPTIONS_DUAL_MODE;
			break;

#ifndef CALL_MGCP
		//Phone Settings
		case OPTIONS_USE_DIALPLAN:
			_iSubMenuItem = OPTIONS_USE_DIGITMAP;
			break;
		case OPTIONS_DIAL_NUMBER:
			_iSubMenuItem = OPTIONS_USE_DIALPLAN;
			break;
		case OPTIONS_COUNTRY_CODE:
			switch(Sys_iUseDialPlan)
			{
			case DIALPLAN_DISABLE:
			case DIALPLAN_ENABLE:
				_iSubMenuItem = OPTIONS_USE_DIALPLAN;
				break;

			case DIALPLAN_DIALNUM:
			case DIALPLAN_PREFIX:
			case DIALPLAN_HOTLINE:
				_iSubMenuItem = OPTIONS_DIAL_NUMBER;
				break;
			}
			break;
		case OPTIONS_AREA_CODE:	
			_iSubMenuItem = OPTIONS_COUNTRY_CODE;
			break;
		case OPTIONS_INTL_PREFIX:		
			_iSubMenuItem = OPTIONS_AREA_CODE;
			break;
		case OPTIONS_LONG_PREFIX:		
			_iSubMenuItem = OPTIONS_INTL_PREFIX;
			break;
		case OPTIONS_INNER_LINE:	
			switch(Sys_iUseDialPlan)
			{
			case DIALPLAN_DISABLE:
				_iSubMenuItem = OPTIONS_USE_DIALPLAN;
				break;

			case DIALPLAN_ENABLE:
			case DIALPLAN_DIALNUM:
			case DIALPLAN_PREFIX:
				_iSubMenuItem = OPTIONS_LONG_PREFIX;
				break;

			case DIALPLAN_HOTLINE:
				_iSubMenuItem = OPTIONS_DIAL_NUMBER;
				break;
			}
			break;
		case OPTIONS_INNERLINE_PREFIX:	
			_iSubMenuItem = OPTIONS_INNER_LINE;
			break;
		case OPTIONS_CALL_WAITING:
			if(Sys_iInnerLine == INNER_LINE_DISABLE)
				_iSubMenuItem = OPTIONS_INNER_LINE;
			else
				_iSubMenuItem = OPTIONS_INNERLINE_PREFIX;
			break;
		case OPTIONS_FWD_NUMBER:
#ifndef NEWUI
			_iSubMenuItem = OPTIONS_CALL_WAITING;
#endif
			break;
		case OPTIONS_FWD_ALWAYS:
			if((Sys_iForwardType & FORWARD_ALWAYS_OR) || (Sys_iForwardType & FORWARD_BUSY_OR) || (Sys_iForwardType & FORWARD_NOANSWER_OR) || (Sys_iForwardType & FORWARD_POWEROFF_OR))
				_iSubMenuItem = OPTIONS_FWD_NUMBER;
			else
				_iSubMenuItem = OPTIONS_CALL_WAITING;
			break;
		case OPTIONS_FWD_BUSY:
			_iSubMenuItem = OPTIONS_FWD_ALWAYS;
			break;
		case OPTIONS_FWD_POWEROFF:
			_iSubMenuItem = OPTIONS_FWD_BUSY;
			break;
		case OPTIONS_FWD_NOANSWER:
			_iSubMenuItem = OPTIONS_FWD_POWEROFF;
			break;
		case OPTIONS_RING_NUMBER:		
			_iSubMenuItem = OPTIONS_FWD_NOANSWER;
			break;
		case OPTIONS_USE_DIGITMAP:
			if(Sys_iForwardType & FORWARD_NOANSWER_OR)
				_iSubMenuItem = OPTIONS_RING_NUMBER;
			else
				_iSubMenuItem = OPTIONS_FWD_NOANSWER;
			break;
#endif

		//Protocol Settings
		case OPTIONS_USE_GK:
#if defined CALL_H323 || defined CALL_IAX2
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
#elif defined CALL_SIP
			_iSubMenuItem = OPTIONS_SIP_PRACK;
#endif
			break;
		case OPTIONS_REGISTER_TTL:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_IAX2
			_iSubMenuItem = OPTIONS_USE_GK;
#elif defined CALL_MGCP
			_iSubMenuItem = OPTIONS_DTMF_PTYPE;
#elif defined CALL_N2P || defined CALL_WP || defined CALL_EPN
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
#endif
			break;
		case OPTIONS_SERVICE_TYPE:
			_iSubMenuItem = OPTIONS_REGISTER_TTL;
			break;
		case OPTIONS_SERVICE_DNS:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_SERVICE_TYPE;
#else
			_iSubMenuItem = OPTIONS_REGISTER_TTL;
#endif
			break;
		case OPTIONS_SERVICE_ID:	
			_iSubMenuItem = OPTIONS_SERVICE_DNS;
			break;
		case OPTIONS_NAT_TRAVERSAL:
#if defined CALL_SIP || defined CALL_N2P
			_iSubMenuItem = OPTIONS_SERVICE_ID;
#else
			_iSubMenuItem = OPTIONS_SERVICE_DNS;
#endif
			break;
		case OPTIONS_NAT_DNS:	
			_iSubMenuItem = OPTIONS_NAT_TRAVERSAL;
			break;
		case OPTIONS_NAT_TTL:
#ifdef CALL_H323
			switch(Sys_iNatTraversal)
			{
			case NAT_TRAVERSAL_DISABLE:
			case H323_NAT_TRAVERSAL_CITRON:
				_iSubMenuItem = OPTIONS_NAT_TRAVERSAL;
				break;
			case NAT_TRAVERSAL_ENABLE:
			case H323_NAT_TRAVERSAL_AIVGR:
			case H323_NAT_TRAVERSAL_EPROXY:
				_iSubMenuItem = OPTIONS_NAT_DNS;
				break;
			}
#elif defined CALL_SIP
			switch(Sys_iNatTraversal)
			{
			case NAT_TRAVERSAL_DISABLE:
				_iSubMenuItem = OPTIONS_NAT_TRAVERSAL;
				break;

			case NAT_TRAVERSAL_ENABLE:
			case SIP_NAT_TRAVERSAL_STUN:
				_iSubMenuItem = OPTIONS_NAT_DNS;
				break;
			}
#elif defined CALL_MGCP
			switch(Sys_iNatTraversal)
			{
			case NAT_TRAVERSAL_DISABLE:
				_iSubMenuItem = OPTIONS_NAT_TRAVERSAL;
				break;

			case NAT_TRAVERSAL_ENABLE:
				_iSubMenuItem = OPTIONS_NAT_DNS;
				break;
			}
#endif
			break;
		case OPTIONS_PHONE_NUMBER:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_NAT_TTL;
#else
			_iSubMenuItem = OPTIONS_SERVICE_DNS;
#endif
			break;
		case OPTIONS_ACCOUNT:
#ifdef CALL_N2P
			_iSubMenuItem = OPTIONS_SERVICE_ID;
#else
			_iSubMenuItem = OPTIONS_PHONE_NUMBER;
#endif
			break;
		case OPTIONS_PIN:			
			_iSubMenuItem = OPTIONS_ACCOUNT;
			break;
		case OPTIONS_RAS_PORT:
			_iSubMenuItem = OPTIONS_PIN;
			break;
		case OPTIONS_Q931_PORT:
			_iSubMenuItem = OPTIONS_RAS_PORT;
			break;
		case OPTIONS_H245_PORT:	
			_iSubMenuItem = OPTIONS_Q931_PORT;
			break;
		case OPTIONS_RTP_PORT:
#ifdef CALL_H323
			_iSubMenuItem = OPTIONS_H245_PORT;
#elif defined CALL_SIP || defined CALL_MGCP || defined CALL_N2P
			_iSubMenuItem = OPTIONS_RAS_PORT;
#else
			_iSubMenuItem = OPTIONS_PIN;
#endif
			break;
		case OPTIONS_RTP_TOS:
#ifdef CALL_IAX2
			_iSubMenuItem = OPTIONS_RAS_PORT;
#else
			_iSubMenuItem = OPTIONS_RTP_PORT;
#endif
			break;
		case OPTIONS_REMOTE_DIALPLAN:
#ifdef CALL_IAX2
			_iSubMenuItem = OPTIONS_RTP_TOS;
#endif
			break;
		case OPTIONS_LOCAL_ALIAS:
#if defined CALL_H323 || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_RTP_TOS;
#endif
			break;
		case OPTIONS_FAST_START:
#ifdef CALL_SIP
			_iSubMenuItem = OPTIONS_RTP_TOS;
#endif
			break;
		case OPTIONS_DTMF_TYPE:	
#ifdef CALL_SIP
			_iSubMenuItem = OPTIONS_FAST_START;
#elif defined CALL_MGCP || defined CALL_H323
			_iSubMenuItem = OPTIONS_LOCAL_ALIAS;
#elif defined CALL_N2P || defined CALL_WP
			_iSubMenuItem = OPTIONS_RTP_PORT;
#elif defined CALL_IAX2
			_iSubMenuItem = OPTIONS_REMOTE_DIALPLAN;
#elif defined CALL_EPN
			_iSubMenuItem = OPTIONS_RTP_TOS;
#endif
			break;
		case OPTIONS_DTMF_PTYPE:
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
			break;
		case OPTIONS_SIP_PRACK:
			_iSubMenuItem = OPTIONS_DTMF_PTYPE;
			break;

		//Other Settings
		case OPTIONS_PASSWORD:
			_iSubMenuItem = OPTIONS_DAYLIGHT;
			break;
		case OPTIONS_UPGRADE_TYPE:
			_iSubMenuItem = OPTIONS_PASSWORD;
			break;
		case OPTIONS_UPGRADE_DNS:
			_iSubMenuItem = OPTIONS_UPGRADE_TYPE;
			break;
		case OPTIONS_NTS_ADDR:
			_iSubMenuItem = OPTIONS_UPGRADE_DNS;
			break;
		case OPTIONS_TIME_ZONE:
			_iSubMenuItem = OPTIONS_NTS_ADDR;
			break;
		case OPTIONS_DAYLIGHT:
			_iSubMenuItem = OPTIONS_TIME_ZONE;
			break;

		//Advanced Settings
		case OPTIONS_SUPER_PASSWORD:	
			_iSubMenuItem = OPTIONS_OUTPUT_DEBUG;
			break;
		case OPTIONS_OUTPUT_DEBUG:	
			_iSubMenuItem = OPTIONS_SUPER_PASSWORD;
				break;
		}
	}
}

//*****VIDA CHANGED*****//
#ifdef NEWUI
BOOLEAN ModemDetect()
{
	XBYTE[0x811C] = 0xff;	// Clear io edge irq
	XBYTE[0x811B] &= 0xfd;			// disable ethernet irq, PIO 9
//	XBYTE[0x811B] |= 0x08;
	XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE & 0x7f;                /* Clear DLAB */
	XBYTE[MODEM_BASE+MODEM_FCR] = FCR_VALUE;                       /* Set FIFO control */
	XBYTE[MODEM_BASE+MODEM_FCR] = FCR_VALUE | 0x06;                /* Reset all FIFO */
	XBYTE[MODEM_BASE+MODEM_MCR] = MCR_VALUE;                       /* Set Modem control */
	XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE;                       /* Set Line control */
	XBYTE[MODEM_BASE+MODEM_IER] = IER_VALUE;		                /* Set interrupt control */
	XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE | 0x80;                /* Set DLAB */
	XBYTE[MODEM_BASE+MODEM_DLL] = DLL_VALUE;                       /* Set Divisor LSB Value */
	XBYTE[MODEM_BASE+MODEM_DLM] = DLM_VALUE;                       /* Set Divisor MSB Value */
	if (XBYTE[MODEM_BASE+MODEM_DLL] != DLL_VALUE)
		return FALSE;			
	else	
		return TRUE;
}

void _OnFinish()
{
	DisplayString((PCCHAR) _ALL_OK,0);
	DisplayString(g_cBlank, 1);
	SysSleep(1000000);
	SysWriteOptions();
	OnUpdateOptions();
}
#endif

void _SubMenuDown()
{
	if (_iSubMenuItem >= SPEED_DIAL_BEGIN)
	{
		if (_bSpeedDialName)
		{
			_bSpeedDialName = FALSE;
		}
		else
		{
			_iSubMenuItem += SPEED_DIAL_LEN;
			if (_SpeedDialPos2Index(_iSubMenuItem) == SPEED_DIAL_NUM)
			{
				_iSubMenuItem = SPEED_DIAL_BEGIN;
			}
			_bSpeedDialName = TRUE;
		}
	}
	else
	{
		switch (_iSubMenuItem)
		{
		//Network Settings
		case OPTIONS_IP_TYPE:
			switch(Sys_iIPType)
			{
			case IP_TYPE_PPPOE:
#ifdef NEWUI
				_iSubMenuItem = OPTIONS_PPP_ID;
				break;
#endif
			case IP_TYPE_MODEM:
#ifdef NEWUI
				_iSubMenuItem = OPTIONS_FWD_NUMBER;
#else
				_iSubMenuItem = OPTIONS_PPP_ID;
#endif
				break;
			case IP_TYPE_DHCP:
				_iSubMenuItem = OPTIONS_MAC_ADDR;
				break;
			case IP_TYPE_STATIC:
				_iSubMenuItem = OPTIONS_IP_ADDR;
				break;
			}
			break;
		case OPTIONS_PPP_ID:
			_iSubMenuItem = OPTIONS_PPP_PASSWORD;
			break;
		case OPTIONS_PPP_PASSWORD:	
#ifndef NEWUI
			switch(Sys_iIPType)
			{
			case IP_TYPE_PPPOE:
				_iSubMenuItem = OPTIONS_DNS_ADDR;
				break;
			case IP_TYPE_MODEM:
				_iSubMenuItem = OPTIONS_MAC_ADDR;
				break;
			}
#else			
				_OnFinish();
#endif
			break;
		case OPTIONS_IP_ADDR:
			_iSubMenuItem = OPTIONS_SUBNET_MASK;
			break;
		case OPTIONS_SUBNET_MASK:		
			_iSubMenuItem = OPTIONS_ROUTER_ADDR;
			break;
		case OPTIONS_ROUTER_ADDR:		
			_iSubMenuItem = OPTIONS_DNS_ADDR;
			break;
		case OPTIONS_DNS_ADDR:		
			_iSubMenuItem = OPTIONS_DNS_ADDR2;
			break;
		case OPTIONS_DNS_ADDR2:		
#ifndef NEWUI
			_iSubMenuItem = OPTIONS_MAC_ADDR;
#else 
				_OnFinish();
#endif
			break;
		case OPTIONS_MAC_ADDR:
			_iSubMenuItem = OPTIONS_VLAN_ENABLE;
			break;

		case OPTIONS_VLAN_ENABLE:
			if (VLAN_bEnable)
			{
				_iSubMenuItem = OPTIONS_VLAN_ID;
			}
			else
			{
				_iSubMenuItem = OPTIONS_IP_TYPE;
			}
			break;

		case OPTIONS_VLAN_ID:
			_iSubMenuItem = OPTIONS_IP_TYPE;
			break;

		//Audio Settings
		case OPTIONS_CODEC_TYPE:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+1;
			break;
		case OPTIONS_CODEC_TYPE+1:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+2;
			break;
		case OPTIONS_CODEC_TYPE+2:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+3;
			break;
		case OPTIONS_CODEC_TYPE+3:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+4;
			break;
		case OPTIONS_CODEC_TYPE+4:
			_iSubMenuItem = OPTIONS_CODEC_TYPE+5;
			break;
		case OPTIONS_CODEC_TYPE+5:
			_iSubMenuItem = OPTIONS_USE_VAD;
			break;
		case OPTIONS_USE_VAD:
			_iSubMenuItem = OPTIONS_USE_AGC;
			break;
		case OPTIONS_USE_AGC:
			_iSubMenuItem = OPTIONS_USE_AEC;
			break;
		case OPTIONS_USE_AEC:	
			_iSubMenuItem = OPTIONS_USE_HIGH_RATE;
			break;
		case OPTIONS_USE_HIGH_RATE:
			_iSubMenuItem = OPTIONS_ILBC_PTYPE;
			break;
		case OPTIONS_ILBC_PTYPE:
			_iSubMenuItem = OPTIONS_AUDIO_FRAMES;
			break;
		case OPTIONS_AUDIO_FRAMES:	
			_iSubMenuItem = OPTIONS_JITTER_SIZE;
			break;
		case OPTIONS_JITTER_SIZE:
			_iSubMenuItem = OPTIONS_HANDSET_INPUT;
			break;
		case OPTIONS_HANDSET_INPUT:
			_iSubMenuItem = OPTIONS_HANDSET_OUTPUT;
			break;
		case OPTIONS_HANDSET_OUTPUT:
#ifdef _USE_FXS
			_iSubMenuItem = OPTIONS_CODEC_TYPE;
#else
			_iSubMenuItem = OPTIONS_RING_TYPE;
#endif
#ifdef _USE_FAKE_FXO
			_iSubMenuItem = OPTIONS_DUAL_MODE;
#endif
			break;
		//DEVICE_ATTR_SPEAKER
		case OPTIONS_RING_TYPE:
			_iSubMenuItem = OPTIONS_HANDFREE_INPUT;
			break;
		case OPTIONS_HANDFREE_INPUT:
			_iSubMenuItem = OPTIONS_HANDFREE_OUTPUT;
			break;
		case OPTIONS_HANDFREE_OUTPUT:
			_iSubMenuItem = OPTIONS_CODEC_TYPE;
			break;
		//DEVICE_ATTR_DUALMODE
		case OPTIONS_DUAL_MODE:
			if(Sys_iDualMode == DUAL_MODE_DISABLE)
				_iSubMenuItem = OPTIONS_CODEC_TYPE;
			else
				_iSubMenuItem = OPTIONS_DUALMODE_PREFIX;
			break;
		case OPTIONS_DUALMODE_PREFIX:		
			_iSubMenuItem = OPTIONS_CODEC_TYPE;
			break;

		//Phone Settings
#ifndef CALL_MGCP
		case OPTIONS_USE_DIALPLAN:	
			switch(Sys_iUseDialPlan)
			{
			case DIALPLAN_DISABLE:
				_iSubMenuItem = OPTIONS_INNER_LINE;
				break;

			case DIALPLAN_ENABLE:
				_iSubMenuItem = OPTIONS_COUNTRY_CODE;
				break;

			case DIALPLAN_DIALNUM:
			case DIALPLAN_PREFIX:
			case DIALPLAN_HOTLINE:
				_iSubMenuItem = OPTIONS_DIAL_NUMBER;
				break;
			}
			break;
		case OPTIONS_DIAL_NUMBER:	
			switch(Sys_iUseDialPlan)
			{
			case DIALPLAN_HOTLINE:
				_iSubMenuItem = OPTIONS_INNER_LINE;
				break;

			default:
				_iSubMenuItem = OPTIONS_COUNTRY_CODE;
				break;
			}
			break;
		case OPTIONS_COUNTRY_CODE:
			_iSubMenuItem = OPTIONS_AREA_CODE;
			break;
		case OPTIONS_AREA_CODE:	
			_iSubMenuItem = OPTIONS_INTL_PREFIX;
			break;
		case OPTIONS_INTL_PREFIX:		
			_iSubMenuItem = OPTIONS_LONG_PREFIX;
			break;
		case OPTIONS_LONG_PREFIX:		
			_iSubMenuItem = OPTIONS_INNER_LINE;
			break;
		case OPTIONS_INNER_LINE:
			if(Sys_iInnerLine == INNER_LINE_DISABLE)
				_iSubMenuItem = OPTIONS_CALL_WAITING;
			else
				_iSubMenuItem = OPTIONS_INNERLINE_PREFIX;
			break;
		case OPTIONS_INNERLINE_PREFIX:	
			_iSubMenuItem = OPTIONS_CALL_WAITING;
			break;
		case OPTIONS_CALL_WAITING:
			if((Sys_iForwardType & FORWARD_ALWAYS_OR) || (Sys_iForwardType & FORWARD_BUSY_OR) || (Sys_iForwardType & FORWARD_NOANSWER_OR) || (Sys_iForwardType & FORWARD_POWEROFF_OR))
				_iSubMenuItem = OPTIONS_FWD_NUMBER;
			else
				_iSubMenuItem = OPTIONS_FWD_ALWAYS;
			break;
		case OPTIONS_FWD_NUMBER:
#ifdef NEWUI
			_iSubMenuItem = OPTIONS_PPP_ID;
#else
			_iSubMenuItem = OPTIONS_FWD_ALWAYS;
#endif
			break;
		case OPTIONS_FWD_ALWAYS:
			_iSubMenuItem = OPTIONS_FWD_BUSY;
			break;
		case OPTIONS_FWD_BUSY:
			_iSubMenuItem = OPTIONS_FWD_POWEROFF;
			break;
		case OPTIONS_FWD_POWEROFF:
			_iSubMenuItem = OPTIONS_FWD_NOANSWER;
			break;
		case OPTIONS_FWD_NOANSWER:
			if(Sys_iForwardType & FORWARD_NOANSWER_OR)
				_iSubMenuItem = OPTIONS_RING_NUMBER;
			else
				_iSubMenuItem = OPTIONS_USE_DIGITMAP;
			break;
		case OPTIONS_RING_NUMBER:		
			_iSubMenuItem = OPTIONS_USE_DIGITMAP;
			break;
		case OPTIONS_USE_DIGITMAP:
			_iSubMenuItem = OPTIONS_USE_DIALPLAN;
			break;
#endif
		//Protocol Settings
		case OPTIONS_USE_GK:
			_iSubMenuItem = OPTIONS_REGISTER_TTL;
			break;

		case OPTIONS_REGISTER_TTL:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_SERVICE_TYPE;
#else
			_iSubMenuItem = OPTIONS_SERVICE_DNS;
#endif
			break;

		case OPTIONS_SERVICE_TYPE:
			_iSubMenuItem = OPTIONS_SERVICE_DNS;
			break;

		case OPTIONS_SERVICE_DNS:
#if defined CALL_SIP || defined CALL_N2P
			_iSubMenuItem = OPTIONS_SERVICE_ID;
#elif defined CALL_H323 || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_NAT_TRAVERSAL;
#else
			_iSubMenuItem = OPTIONS_PHONE_NUMBER;
#endif
			break;

		case OPTIONS_SERVICE_ID:
#ifdef CALL_SIP
			_iSubMenuItem = OPTIONS_NAT_TRAVERSAL;
#elif defined CALL_N2P
			_iSubMenuItem = OPTIONS_PIN;
#endif
			break;

		case OPTIONS_NAT_TRAVERSAL:
#ifdef CALL_H323
			switch(Sys_iNatTraversal)
			{
			case NAT_TRAVERSAL_DISABLE:
			case H323_NAT_TRAVERSAL_CITRON:
				_iSubMenuItem = OPTIONS_NAT_TTL;
				break;

			case NAT_TRAVERSAL_ENABLE:
			case H323_NAT_TRAVERSAL_AIVGR:
			case H323_NAT_TRAVERSAL_EPROXY:
				_iSubMenuItem = OPTIONS_NAT_DNS;
				break;
			}
#elif defined CALL_SIP
			switch(Sys_iNatTraversal)
			{
			case NAT_TRAVERSAL_DISABLE:
				_iSubMenuItem = OPTIONS_NAT_TTL;
				break;

			case NAT_TRAVERSAL_ENABLE:
			case SIP_NAT_TRAVERSAL_STUN:
				_iSubMenuItem = OPTIONS_NAT_DNS;
				break;
			}
#elif defined CALL_MGCP
			switch(Sys_iNatTraversal)
			{
			case NAT_TRAVERSAL_DISABLE:
				_iSubMenuItem = OPTIONS_NAT_TTL;
				break;

			case NAT_TRAVERSAL_ENABLE:
				_iSubMenuItem = OPTIONS_NAT_DNS;
				break;
			}
#endif
			break;

		case OPTIONS_NAT_DNS:	
			_iSubMenuItem = OPTIONS_NAT_TTL;
			break;
		
		case OPTIONS_NAT_TTL:
#ifdef CALL_N2P
			_iSubMenuItem = OPTIONS_ACCOUNT;
#else
			_iSubMenuItem = OPTIONS_PHONE_NUMBER;
#endif
			break;

		case OPTIONS_PHONE_NUMBER:
			_iSubMenuItem = OPTIONS_ACCOUNT;
			break;
		case OPTIONS_ACCOUNT:
			_iSubMenuItem = OPTIONS_PIN;
			break;

		case OPTIONS_PIN:
#if defined CALL_EPN || defined CALL_WP
			_iSubMenuItem = OPTIONS_RTP_PORT;
#else
			_iSubMenuItem = OPTIONS_RAS_PORT;
#endif
			break;

		case OPTIONS_RAS_PORT:
#ifdef CALL_H323
			_iSubMenuItem = OPTIONS_Q931_PORT;
#elif defined CALL_SIP || defined CALL_MGCP || defined CALL_N2P
			_iSubMenuItem = OPTIONS_RTP_PORT;
#elif defined CALL_IAX2
			_iSubMenuItem = OPTIONS_RTP_TOS;
#endif
			break;

		case OPTIONS_Q931_PORT:
			_iSubMenuItem = OPTIONS_H245_PORT;
			break;
		case OPTIONS_H245_PORT:	
			_iSubMenuItem = OPTIONS_RTP_PORT;
			break;

		case OPTIONS_RTP_PORT:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_MGCP || defined CALL_EPN
			_iSubMenuItem = OPTIONS_RTP_TOS;
#elif defined CALL_N2P || defined CALL_WP
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
#endif
			break;

		case OPTIONS_RTP_TOS:
#if defined CALL_H323 || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_LOCAL_ALIAS;
#elif defined CALL_SIP
			_iSubMenuItem = OPTIONS_FAST_START;
#elif defined CALL_EPN
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
#elif defined CALL_IAX2
			_iSubMenuItem = OPTIONS_REMOTE_DIALPLAN;
#endif
			break;

		case OPTIONS_REMOTE_DIALPLAN:
#ifdef CALL_IAX2
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
#endif
			break;
		
		case OPTIONS_LOCAL_ALIAS:
#if defined CALL_H323 || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
#endif
			break;

#ifdef CALL_SIP
		case OPTIONS_FAST_START:
			_iSubMenuItem = OPTIONS_DTMF_TYPE;
			break;
#endif

		case OPTIONS_DTMF_TYPE:	
#if defined CALL_SIP || defined CALL_MGCP
			_iSubMenuItem = OPTIONS_DTMF_PTYPE;
#elif defined CALL_H323 || defined CALL_IAX2
			_iSubMenuItem = OPTIONS_USE_GK;
#else
			_iSubMenuItem = OPTIONS_REGISTER_TTL;
#endif
			break;

		case OPTIONS_DTMF_PTYPE:
#ifdef CALL_SIP
			_iSubMenuItem = OPTIONS_SIP_PRACK;
#elif defined CALL_MGCP
			_iSubMenuItem = OPTIONS_REGISTER_TTL;
#endif
			break;
		case OPTIONS_SIP_PRACK:
			_iSubMenuItem = OPTIONS_USE_GK;
			break;

		//Other Settings
		case OPTIONS_PASSWORD:		
			_iSubMenuItem = OPTIONS_UPGRADE_TYPE;
			break;
		case OPTIONS_UPGRADE_TYPE:
			_iSubMenuItem = OPTIONS_UPGRADE_DNS;
			break;
		case OPTIONS_UPGRADE_DNS:
			_iSubMenuItem = OPTIONS_NTS_ADDR;
			break;
		case OPTIONS_NTS_ADDR:
			_iSubMenuItem = OPTIONS_TIME_ZONE;
			break;
		case OPTIONS_TIME_ZONE:
			_iSubMenuItem = OPTIONS_DAYLIGHT;
			break;
		case OPTIONS_DAYLIGHT:
			_iSubMenuItem = OPTIONS_PASSWORD;
			break;

		//Advanced Settings
		case OPTIONS_SUPER_PASSWORD:	
			_iSubMenuItem = OPTIONS_OUTPUT_DEBUG;
			break;
		case OPTIONS_OUTPUT_DEBUG:	
			_iSubMenuItem = OPTIONS_SUPER_PASSWORD;
			break;
		}
	}
}

void OnBackSpace()
{
	KeypadBackspace();
	Key_bPressed = FALSE;
	KeypadDisplay();
}

void OnSet()
{
	if (_bConfirmMode)	return;

	if (Menu_bPasswordOK)
	{
		if (_iMenuType == SUB_MENU)
		{
			if(_iSubMenuItem >= SPEED_DIAL_BEGIN)
			{
				if(_bSpeedDialName)
					Key_bLetter = TRUE;
				else
					Key_bLetter = FALSE;
			}
			else
			{
				switch (_iSubMenuItem)
				{
				case OPTIONS_MAC_ADDR:
				case OPTIONS_PASSWORD:		
				case OPTIONS_ACCOUNT:		
				case OPTIONS_PIN:			
				case OPTIONS_PPP_ID:
				case OPTIONS_PPP_PASSWORD:	
				case OPTIONS_SERVICE_ID:	
				case OPTIONS_SUPER_PASSWORD:	
				case OPTIONS_SERVICE_DNS:	
					Key_bLetter = TRUE;
					break;

				default:
					Key_bLetter = FALSE;
					break;
				}
			}
//			KeypadClearAll();

			Key_bInputMode = TRUE;
#if defined VER_KINGTEL || defined VER_YODAO
			DisplayCursor(FALSE);
#else
			DisplayCursor(TRUE);
#endif
			Key_bPressed = FALSE;
			KeypadDisplay();

#ifdef VER_KINGTEL
			DisplayCursor(FALSE);
#endif
		}
	}
}

void OnMenuDown()
{
	if (_bConfirmMode)	return;

	if (Key_bInputMode)
	{
		Key_bPressed = FALSE;
		KeypadDisplay();
		return;
	}

	if (Menu_bPasswordOK)
	{
		if (_iMenuType == MAIN_MENU)
		{
			_iMainMenuItem ++;
			if( !Task_bSupervisor && (_iMainMenuItem == MENU_PROTOCOL || _iMainMenuItem == MENU_ADVANCED) )
			{
				_iMainMenuItem ++;
			}

#ifdef VER_IPH_E00
			if( !Task_bSupervisor && _iMainMenuItem == MENU_PROTOCOL)
			{
				_iMainMenuItem =MENU_SAVE;
			}
#endif
			if (_iMainMenuItem == MAIN_MENU_MAX)
			{
				_iMainMenuItem = 0;
			}
			_MainMenu();
		}
		else if (_iMenuType == STORE_MENU)
		{
			_iStoreMenuItem ++;
			if (_iStoreMenuItem == STORE_MENU_MAX)
			{
				_iStoreMenuItem = 0;
			}
			_StoreMenu();
		}
		else if (_iMenuType == LOAD_MENU)
		{
			_iLoadMenuItem ++;
			if (_iLoadMenuItem == LOAD_MENU_MAX)
			{
				_iLoadMenuItem = 0;
			}
			_LoadMenu();
		}
		else
		{
			_SubMenuDown();
			_SubMenu();
		}
	}
}

void OnMenuUp()
{
	if (_bConfirmMode)	return;

	if (Key_bInputMode)
	{
		OnBackSpace();
		return;
	}

	if (Menu_bPasswordOK)
	{
		if (_iMenuType == MAIN_MENU)
		{
			if (_iMainMenuItem)
			{
				_iMainMenuItem --;
				if( !Task_bSupervisor && (_iMainMenuItem == MENU_PROTOCOL || _iMainMenuItem == MENU_ADVANCED) )
				{
					_iMainMenuItem --;
				}
#ifdef VER_IPH_E00
				if( !Task_bSupervisor && _iMainMenuItem == MENU_LOAD)
				{
					_iMainMenuItem =MENU_NETWORK;
				}
#endif
			}
			else
			{
				_iMainMenuItem = MAIN_MENU_MAX - 1;
			}
			_MainMenu();
		}
		else if (_iMenuType == STORE_MENU)
		{
			if (_iStoreMenuItem)
			{
				_iStoreMenuItem --;
			}
			else
			{
				_iStoreMenuItem = STORE_MENU_MAX - 1;
			}
			_StoreMenu();
		}
		else if (_iMenuType == LOAD_MENU)
		{
			if (_iLoadMenuItem)
			{
				_iLoadMenuItem --;
			}
			else
			{
				_iLoadMenuItem = LOAD_MENU_MAX - 1;
			}
			_LoadMenu();
		}
		else
		{
			_SubMenuUp();
			_SubMenu();
		}
	}
}

void OnCancel()
{
#ifdef NEWUI
if(!Task_bSupervisor) return;
#endif
	if (Menu_bPasswordOK)
	{
		if (_iMenuType == MAIN_MENU)
		{
			if (_bConfirmMode)
			{
				_bConfirmMode = FALSE;
				_MainMenu();
			}
			else
			{
#ifndef VER_IPH_E00
				OnMenuUp();
#else
				_iMainMenuItem = MENU_SAVE;
				_MainMenu();
#endif
			}
		}
		else if (_iMenuType == STORE_MENU || _iMenuType == LOAD_MENU)
		{
			_iMenuType = MAIN_MENU;
			_MainMenu();
		}
		else
		{
			if (Key_bInputMode)
			{
				Key_bInputMode = FALSE;
				DisplayCursor(FALSE);
				_SubMenu();
			}
			else
			{
				_iMenuType = MAIN_MENU;
				_MainMenu();
			}
		}
	}
	else
	{
		KeypadClearAll();
	}
}

void _ConfirmMode()
{
	_bConfirmMode = TRUE;
	_iMenuType = MAIN_MENU; // still in the main menu
	DisplayString((PCCHAR)"Are you sure?", 1);
}
//*****VIDA CHANGED*****//
#ifdef NEWUI
void NewUI_Menu()
{
	switch(Key_iCurrentID)
	{
	case '1':
		Sys_iIPType=IP_TYPE_DHCP;
		_OnFinish();
		break;
	case '0':
		KeypadClear();
		Key_bInputMode = FALSE;
		NewUI_bSetup=FALSE;
		Sys_iIPType=IP_TYPE_STATIC;
		_iSubMenuItem = OPTIONS_IP_ADDR;
		_SubMenu();
		break;
	case '2':
		KeypadClear();
		Key_bInputMode = FALSE;
		NewUI_bSetup=FALSE;
		Sys_iIPType=IP_TYPE_PPPOE;
		_iSubMenuItem = OPTIONS_PPP_ID;
		_SubMenu();
		break;
	case '3':
		KeypadClear();
		Key_bInputMode = FALSE;
		NewUI_bSetup=FALSE;
		if(!ModemDetect())
		{
			_OnFinish();
			break;
		}
	
		Sys_iIPType=IP_TYPE_MODEM;
		_iSubMenuItem = OPTIONS_FWD_NUMBER;
		_SubMenu();
		break;
	case '9':
		OnReboot();
		break;
	}
}
#endif
void OnOK()
{
	if (Menu_bPasswordOK)
	{
//*****VIDA CHANGED*****//
#ifdef NEWUI
	if (NewUI_bSetup)  
		NewUI_Menu();
	else 
#endif
		if (_iMenuType == MAIN_MENU)
		{
			_iMenuType = SUB_MENU;
			switch (_iMainMenuItem)
			{
			case MENU_NETWORK:
				_iSubMenuItem = OPTIONS_IP_TYPE;
				_SubMenu();
				break;
			case MENU_PROTOCOL:
#if defined CALL_H323 || defined CALL_SIP || defined CALL_IAX2
				_iSubMenuItem = OPTIONS_USE_GK;
#else
				_iSubMenuItem = OPTIONS_REGISTER_TTL;
#endif
				_SubMenu();
				break;
#ifndef CALL_MGCP
			case MENU_DIALPLAN:
				_iSubMenuItem = OPTIONS_USE_DIALPLAN;
				_SubMenu();
				break;
#endif
			case MENU_AUDIO:	
				_iSubMenuItem = OPTIONS_CODEC_TYPE;
				_SubMenu();
				break;
			case MENU_OTHER:	
				_iSubMenuItem = OPTIONS_PASSWORD;
				_SubMenu();
				break;
			case MENU_ADVANCED:
				_iSubMenuItem = OPTIONS_SUPER_PASSWORD;
				_SubMenu();
				break;
			case MENU_SPEEDDIAL:
				_iSubMenuItem = SPEED_DIAL_BEGIN;
				_bSpeedDialName = TRUE;
				_SubMenu();
				break;
			case MENU_DEFAULT:
				if (_bConfirmMode)
				{
					if (Sys_bLoadDefault)
					{
						Sys_bLoadDefault = FALSE;
						SysLoadOptions(0);
					}
					else
					{
						DefaultSettings();
						SysReadOptions();
					}
					_bConfirmMode = FALSE;
					DisplayString(_cOKLabel, 1);
					SysSleep(2000000);
					_iMenuType = MAIN_MENU; // still in the main menu
					_MainMenu();
				}
				else
				{
					_ConfirmMode();
				}
				break;
			case MENU_STORE:
				_iStoreMenuItem = 0;
				_iMenuType = STORE_MENU;
				_StoreMenu();
				break;
			case MENU_LOAD:
				_iLoadMenuItem = 0;
				_iMenuType = LOAD_MENU;
				_LoadMenu();
				break;
/*			case MENU_AUTOTEST:
				SwitchPage(9);
				break;
*/
			case MENU_SAVE:
				if (_bConfirmMode)
				{
					SysWriteOptions();
					OnUpdateOptions();
				}
				else
				{
					_ConfirmMode();
				}
				break;
			case MENU_EXIT:
				SysReadOptions();
				SysWriteOptions();
				OnUpdateOptions();
//				OnReboot();
				break;
			}
		}
		else if (_iMenuType == STORE_MENU)
		{
			SysStoreOptions(_iStoreMenuItem+1);
			_iMenuType = MAIN_MENU;
			_MainMenu();
		}
		else if (_iMenuType == LOAD_MENU)
		{
			SysLoadOptions(_iLoadMenuItem+1);
			_iMenuType = MAIN_MENU;
			_MainMenu();
		}
		else
		{
			if (Key_bInputMode)
			{
				Key_bInputMode = FALSE;
				DisplayCursor(FALSE);
				_SubMenuOK();
				_SubMenu();
			}
			else
			{
#ifndef VER_IPH_E00
				OnMenuDown();
#else
				OnSet();
#endif
			}
		}
	}
	else
	{
		if (IsPassword(Key_pBuf))
		{
			Menu_bPasswordOK = TRUE;
//*****VIDA CHANGED*****//
#ifdef NEWUI	
			if(!Task_bSupervisor)
			{
				NewUI_bSetup=TRUE;
				DisplayString((PCCHAR) _NewUIMenu0,0);
				if(!ModemDetect())
				{
					DisplayString((PCCHAR) _NewUIMenu1_NoModem,1);
				}
				else
				{
					DisplayString((PCCHAR) _NewUIMenu1,1);
				}
				_iMenuType = SUB_MENU;
				return;
			}
#endif
			Key_bInputMode = FALSE;
#ifdef VER_KINGTEL
			DisplayCursor(FALSE);		// if have not this line, Kingtel's LCD will display strange code
#endif
			_iMenuType = MAIN_MENU;
			_iMainMenuItem = 0;
			_MainMenu();
		}
		else
		{
			KeypadClearAll();
#ifdef NEWUI	
				_iPassInvalidCount++;
			if(_iPassInvalidCount >= MAX_PASS_INVALID)
			{
				OnReboot();
			}
#endif		
		}
	}
}

