#include "Page1.h"
#include "p_dsp.h"
#include "v_task.h"
#ifdef CALL_H323
#include "p_h323.h"
#endif
#ifdef CALL_SIP
#include "p_sip.h"
#endif
#ifdef CALL_MGCP
#include "p_mgcp.h"
#endif
#include "p_http.h"
#include "p_rtp.h"
#include "p_sdp.h"

#ifdef VER_IPH_E00
extern XCHAR Task_iSpecKey;
#endif

#ifdef OEM_SKYCOM
#include "skycom.h"
#endif

#if defined OEM_MEDIARING || defined OEM_RINGCHAT
#include "mediaring.h"
#endif
                                       
BOOLEAN Key_bCopied = FALSE;

void UpdateRedialBuf()
{
	SdramSaveData(OPTIONS_SDRAM_POS+KEY_PREVBUF_SDRAM_LO, Key_pBuf, p_strlen_x(Key_pBuf));
}

void _SaveKey()
{
	DCHAR iLen;

	Sys_iScrollType = SCROLL_NULL;		// Andy
	iLen = p_strlen_x(Key_pBuf);
	if (iLen == MAX_KEYBUF_LEN)
	{
		iLen = 0;
	}
	Key_pBuf[iLen] = Key_iCurrentID;
	iLen ++;
	Key_pBuf[iLen] = 0;
	Key_bCopied = FALSE;
	DisplayKeys();
}

void HandleNumberKeys()
{
	PIO_bIndicated = FALSE;
	if (Ac97_bIVR)
	{
		if (Key_iCurrentID == '#' || Key_iCurrentID == '*')
		{
			Ac97StopIVR();
		}
		else
		{
#ifdef VER_IPH_E00
			Ac97StopIVR();
#else
			return;
#endif
		}
	}
#ifndef GUI_STANDARD
#ifndef VER_IPH_E00
	if (Ac97_iDtmfCode == DTMF_SIGNAL_BUSY)	return;
#endif
#endif

#ifndef CALL_MGCP
	if ((IsInCall()) && !Task_bCallHold)
	{
#ifdef _USE_FXS
		if (Sys_iDtmfType != DTMF_INBAND_AUDIO)
		{
			OnOutgoingKey(Key_iCurrentID);
		}
#else
		OnOutgoingKey(Key_iCurrentID);
#endif
		_SaveKey();
		return;
	}
#endif

	if (IsCallMode())
	{
#ifdef _USE_FXS
		Ac97_iDtmfCode = DTMF_SIGNAL_QUIET;
#else
		DtmfPlayKey(Key_iCurrentID);
#endif
	}

#ifdef CALL_IAX2
	if (Sys_iCallType & REMOTE_DIALPLAN_OR)
	{
		if (IsCallMode())
		{
			_SaveKey();
			Iax2DPReq(Key_pBuf);
			return;
		}
	}
#endif
	if (UIHandleSpecialKeys())	return;

	_SaveKey();

#ifdef _USE_FAKE_FXO
	CheckDualModePrefix();
#endif
	
	if (!IsInCall())
	{
#if defined GUI_STANDARD 
		if (IsPassword(Key_pBuf) && !Task_bSetupMode && IsCallMode())
		{
			Task_bSetupMode = TRUE;
			KeypadClear();
		}
#elif defined VER_IPH_E00
		if (IsPassword(Key_pBuf) && !Task_bSetupMode && IsCallMode()&&!IsModemConnect())
		{
			Task_bSetupMode = TRUE;
			KeypadClear();
		}
#endif
		if (Task_bSetupMode)	return;
	}

	if (IsCallMode())
	{
#ifdef CALL_MGCP
		if (Task_bConnecting)
		{
			UpdateRedialBuf();
			p_memcpy_x2x_0(Sys_pPeerDisplay, Key_pBuf, PHONE_NUMBER_LEN);
			CallRecWriteStart(Sys_pPeerDisplay, CALL_TYPE_DIALED);
		}
		MgcpNotifyKey(Key_iCurrentID);
#else
		if (Sys_bUseDigitMap)
		{
//			if (Key_pBuf[0] != '#')
//			{
				if (OnMapMatch(FALSE))
				{
					OnCall();
				}
//			}
		}
		else
		{
			Map_iTimeOut = NONE_MAP_TIME_OUT;
		}
#endif
	}
}

void _OnImpossibleKey()
{
#if defined OEM_NONE_IVR || defined OEM_JR168
	Ac97StartIVR();
#endif

	DisplayCursor(TRUE);
	DisplayInitialize();

	OnDirectSpeedDial(0);
	OnHookFlash();
	WriteSpeedDial((PXCHAR)0, 0);

#ifdef _USE_FXS
	DtmfRing();
	L3ClockBlink();
	L3DataOff();
	L3DataOn();
	L3ModeOff();
	L3ModeOn();
#endif

#ifdef _USE_FXS_AM79R70
	SlicActiveMode();
#endif

	DebugCString((PCCHAR)"");
	GetSoftwareVersion();
	SoloFileSetLimit(0);

#ifdef CALL_H323
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
#elif defined CALL_SIP
	p_line_set_len(0);
	RtcpHandleTimer();
	SdramCopyData(0, (PXCHAR)0);
	TcbConnected(0);
	TcpOpen((PXCHAR)0, 0, 0);
#elif defined CALL_MGCP
	DtmfStopRing();
	p_count_char((PXCHAR)0, 0);
	p_line_inc_len(0);
	SdpAddDtmfFmtp(0);
	SdramCopyData(0, (PXCHAR)0);
	TaskUserData(0, (PXCHAR)0);
//	TaskHandleData(0, (PXCHAR)0, 0);
	TaskIncomingKeys(0, (PXCHAR)0);
	TaskHangup();
	TcbConnected(0);
	TcpOpen((PXCHAR)0, 0, 0);
#elif defined CALL_N2P
	Ac97StartRing();
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
	ProxyGetLocalIP((PXCHAR)0);
	SdramCopyData(0, (PXCHAR)0);
	SdramWriteString(0);
	TaskHandleData(0, (PXCHAR)0, 0);
	TaskStartRtp();
	TaskUserData(0, (PXCHAR)0);
	UdpSetTos(0, 0);
#elif defined CALL_WP
	OutputPXCHAR((PXCHAR)1, 0);
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
	ProxyGetLocalIP((PXCHAR)0);
	SdramCopyData(0, (PXCHAR)0);
	TaskIncomingKeys(0, (PXCHAR)0);
	TaskStartRtp();
	TaskUserData(0, (PXCHAR)0);
	UdpSetTos(0, 0);
#elif defined CALL_IAX2
	OutputPXCHAR((PXCHAR)1, 0);
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
	p_rand_char();
	ProxyGetLocalIP((PXCHAR)0);
	SdramCopyData(0, (PXCHAR)0);
	TaskHandleData(0, (PXCHAR)0, 0);
	TaskUserData(0, (PXCHAR)0);
	TcbConnected(0);
	TcpOpen((PXCHAR)0, 0, 0);
	UdpSetTos(0, 0);
#elif defined CALL_EPN
	OutputPXCHAR((PXCHAR)1, 0);
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
	ProxyGetLocalIP((PXCHAR)0);
	SdramCopyData(0, (PXCHAR)0);
	TaskHandleData(0, (PXCHAR)0, 0);
	TaskUserData(0, (PXCHAR)0);
#endif

#ifdef HW_MODEM
	OnModemDisconnect();
	OnModemConnect();
#endif

#if defined VER_IPH_E00
	L3ClockBlink();
	L3ModeOn();
	L3ModeOff();
	p_ltoa(0, (PXCHAR)0, 0);
	OnCallHold();
	OnCallTransfer();
#elif defined GUI_5111
	OnRouterAddress();
	OnGKAddress();
	OnSubnetMask();
	OnMute();
	OnLoadSpeedDial();
#elif defined VER_MAXLINK
	OnBackSpace();
	OnRouterAddress();
	OnGKAddress();
	OnSubnetMask();
	OnMute();
#elif defined VER_YWH10
	OnCall();
	OnGKAddress();
//	OnLoadSpeedDial();
	OnMute();
#elif defined VER_YWH100
	OnRouterAddress();
	OnSubnetMask();
	OnMute();
#elif defined VER_YWH200
	OnRouterAddress();
	OnSubnetMask();
	OnLoadSpeedDial();
	OnMute();
#elif defined VER_YWH300
	OnRouterAddress();
	OnSubnetMask();
	OnLoadSpeedDial();
	OnGKAddress();
	OnMute();
#elif defined VER_YWH500
	OnRouterAddress();
	OnSubnetMask();
	OnGKAddress();
	OnMute();
#elif defined VER_EP668
	OnRouterAddress();
	OnGKAddress();
	OnMute();
#elif defined VER_WLINK
	OnGKAddress();
	OnRouterAddress();
	OnSubnetMask();
	OnMute();
#elif defined VER_KINGTEL
	OnRouterAddress();
	OnSubnetMask();
	OnGKAddress();
	OnMute();
#endif

#if defined OEM_MEDIARING || defined OEM_RINGCHAT
	OnGKAddress();
	OnMute();
	OnPhoneNumber();
	p_String2Mac((PXCHAR)0, (PXCHAR)0);
#endif

#if defined OEM_SOYO_GROUP
	OnMute();
#endif
}

#if defined VER_IPH_E00

void HandleFunctionKeys()
{
#ifndef VER_IPH_E00_2G
	if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P')	// VOL- & VOL+
	{
		if (Key_iCurrentID == 'N')						// SPK
		{
			if (Sys_iScrollType > SCROLL_SPEED_DIAL)
			{
				Sys_iScrollType = SCROLL_NULL;
			}
		}
		else
		{
			Sys_iScrollType = SCROLL_NULL;
		}
	}

	if((Task_iSpecKey == KEY_CONFIG_STORE) ||(Task_iSpecKey == KEY_CONFIG_LOAD))
	{
		if (Key_iCurrentID != 'N')						// SPK
		{
			TaskRestart(0);
		}
	}	

	switch (Key_iCurrentID)
	{
	case 'A':	
		OnRedial();
		break;
	case 'B':	
		OnSaveSpeedDial(TRUE);
		break;
	case 'C':
		OnGetBalance();
		break;
	case 'L':			// Ethernet LED place
		OnPhoneNumber();
		break;
	case 'Q':
		OnSoftKey1();
		break;
	case 'R':
		OnSoftKey2();
		break;
	case 'S':
		OnSoftKey3();
		break;
	case 'I':			// Power LED place
		OnFuncKey();
		break;
	case 'J':
		OnCallRecord();
		break;
	case 'M':	
		OnMute();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':	
		OnVolLower();
		break;
	case 'P':	
		OnVolHigher();
		break;
//Support TeleSynergy TelePCX flask key by seding out-band dtmf 'a' when connecting, by Anfernee 20040525
	case 'W':		//FUN1
		OnBalanceIVRcall();
		break;
	case 'V':		//FUN2
		OnCallHold();
		break;
	case 'U':		//FUN3
		OnCallTransfer();
		break;
//Add Reboot(modem Reconnect function), by Anfernee 20040621
	case 'H':			//FUN4
#ifdef HW_MODEM
    if(Sys_iIPType==IP_TYPE_MODEM)
    {
		ModemInitialize();
		ModemRedial();
		TcpipInitialize();
    }
#endif
		break;
//Add modem disconnect function, by Anfernee 20040621
	case 'G':			//FUN5
#ifdef HW_MODEM
		OnUpdateOptions();
#endif
		break;
//Add store config feature support, by Anfernee 20040810
	case 'F':			//FUN6
		if (((UCHAR)p_atoi(Key_pBuf, 10) > 0) && ((UCHAR)p_atoi(Key_pBuf, 10) <= ALTERNATE_SETTING_NUM))
		{
			OnStoreSettings((UCHAR)p_atoi(Key_pBuf, 10));
		}
		break;
//Add load config feature support, by Anfernee 20040810
	case 'E':			//FUN7
		if (((UCHAR)p_atoi(Key_pBuf, 10) > 0) && ((UCHAR)p_atoi(Key_pBuf, 10) <= ALTERNATE_SETTING_NUM))
		{
			OnLoadSettings((UCHAR)p_atoi(Key_pBuf, 10));
		}
		break;

	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	default:
		break;
	}
#else				//2G
	if (Key_iCurrentID != 'J' && Key_iCurrentID != 'I')	// VOL- & VOL+
	{
		if (Key_iCurrentID == 'M')						// SPK
		{
			if (Sys_iScrollType > SCROLL_SPEED_DIAL)
			{
				Sys_iScrollType = SCROLL_NULL;
			}
		}
		else
		{
			Sys_iScrollType = SCROLL_NULL;
		}
	}

	switch (Key_iCurrentID)
	{
	case 'F':	
		OnRedial();
		break;
	case 'L':	
		OnSaveSpeedDial(TRUE);
		break;
	case 'G':
		OnGetBalance();
		break;
	case 'E':
		OnFuncKey();
		break;
	case 'K':			//Enter
		OnSoftKeys();
		break;
	case 'O':			//Flash
		OnCallTransfer();
		break;
	case 'P':			// IP/TELNUM	
		OnPhoneNumber();
		break;
	case 'H':
		OnCallRecord();
		break;
	case 'B':	
		OnCallHold();
		break;
	case 'M':	
		OnHandFree();
		break;
	case 'J':	
		OnVolLower();
		break;
	case 'I':	
		OnVolHigher();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	default:
		break;
	}
#endif	
}

#elif defined GUI_5111

void HandleFunctionKeys()
{
	if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P')	// vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
#ifndef VER_IPLINK
		OnRedial();
#else
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnRedial();
		}
#endif
		break;
	case 'B':	
		OnDialedCall();
		break;
	case 'C':
#ifndef VER_IPLINK
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnIPAddress();
		}
#else
		OnIPAddress();
#endif
		break;
	case 'D':	// vol key
#ifndef VER_IPLINK
		if (!PIO_bHandsetUp && !PIO_bHandFree)
		{
			OnHandFree();
			OnVolHigher();
			OnVolLower();
		}
#else
		OnSaveSpeedDial(TRUE);
#endif
		break;
	case 'E':	
		OnMissedCall();
		break;
	case 'F':	
		OnPhoneNumber();
		break;
	case 'G':
#ifndef VER_IPLINK
		OnCallHold();
#endif
		break;
	case 'H':	
		OnAnsweredCall();
		break;
	case 'I':	
		if (IsCallMode())
		{
			OnLoadSpeedDial();
		}
		else
		{
			OnSaveSpeedDial(TRUE);
		}
		break;
	case 'J':
#ifndef VER_IPLINK
		OnBackSpace();
#else
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnBackSpace();
		}
#endif
		break;
	case 'K':	
		OnCallTransfer();
		break;
	case 'L':	
		break;
	case 'M':
		OnHookFlash();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':	
		OnVolLower();
		break;
	case 'P':	
		OnVolHigher();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_MAXLINK

void HandleFunctionKeys()
{
	// Andy       vol-             vol+
	if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P')
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':	
		OnRedial();
		break;
	case 'C':	
		OnAnsweredCall();
		break;
	case 'F':	
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			DisplayString((PCCHAR)"Mute", 0);
		}
		break;
	case 'G':	 
		OnDialedCall();
		break;
	case 'H':	
		OnMissedCall();
		break;
	case 'I':	
		OnLoadSpeedDial();
		break;
	case 'K':	
		OnPhoneNumber();
		break;
	case 'L':	
		OnIPAddress();
		break;
	case 'M':
#ifdef CALL_MGCP
		MgcpNotify(MGCP_EVENT_LINE_HF);
#else
		OnCallTransfer();
#endif
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':	
		OnVolHigher();
		break;
	case 'P':	
		OnVolLower();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_YWH10

void HandleFunctionKeys()
{
	if (Key_iCurrentID != 'P' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnLoadSpeedDial();
//		OnHookFlash();
		break;
	case 'B':
		OnSaveSpeedDial(TRUE);
		break;
	case 'C':	
		OnPhoneNumber();
		break;
	case 'D':
		OnBackSpace();
		break;
	case 'E':
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnDialedCall();
		}
		break;
	case 'F':
		OnIPAddress();
		break;
	case 'G':	 
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnRouterAddress();
		}
		break;
	case 'H':	
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnSubnetMask();
		}
		break;
	case 'I':
		OnMissedCall();
		break;
	case 'J':
		if (Task_bConnected)              //yan
		{
			OnCallTransfer();
		}
		else
		{
			OnAnsweredCall();
		}
		break;
	case 'K':	
		OnDialedCall();
		break;
//	case 'L':	
//		OnCall();
//		break;
	case 'M':
		OnVolHigher();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnRedial();
		break;
	case 'P':
		OnVolLower();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_YWH500
void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'M')	// CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'O' && Key_iCurrentID != 'G' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnIPAddress();//OnRedial();
		break;
	case 'B':
		OnCall();//OnMissedCall();
		break;
	case 'C':	
		OnMissedCall();//OnIPAddress();
		break;
	case 'D':
		OnCallTransfer();//OnBackSpace();
		break;
	case 'E':
		OnPhoneNumber();
		break;
	case 'F':
		OnAnsweredCall();//OnGKAddress();
		break;
	case 'G':	 
		OnVolHigher();
		break;
	case 'H':	
		OnCallHold();
		break;
	case 'M':
		OnGKAddress();//OnHookFlash();
		break;
	case 'N':	
		OnDialedCall();//OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'P':
		OnHookFlash();//OnVolHigher();
		break;

	case 'h':
		OnLoadSpeedDial();  
		break;
	case 'k':	
		OnBackSpace();                 
		break;
	case 'l':	
		OnHandFree();                 
		break;
	case 'm':	
		OnRedial();
		break;
	case 'n':	
		OnSaveSpeedDial(TRUE);              
		break;

	case 'X': // impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_YWH300

void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'O') // CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'A' && Key_iCurrentID != 'E' && Key_iCurrentID != 'O') // Andy       vol-            
	{
		Sys_iScrollType = SCROLL_NULL;
	}
	
	switch (Key_iCurrentID)
	{
	case 'A':
		OnVolHigher();//OnRedial();
		break;
	case 'B':
		OnPhoneNumber();//OnMissedCall();
		break;
	case 'C': 
		OnIPAddress();
		break;
	case 'D':
		OnBackSpace();
		break;
	case 'E':
		OnVolLower();//OnPhoneNumber();
		break;
	case 'F':
		OnAnsweredCall();//OnGKAddress();
		break;
	case 'H': 
		OnRedial();
		break;
	case 'J':
		OnCallTransfer();//OnSaveSpeedDial();
		break;
	case 'K': 
		OnHookFlash();//OnDialedCall();
		break;
	case 'L': 
		OnCallHold();//OnAnsweredCall();
		break;
	case 'M':
		OnDialedCall();//OnHookFlash();
		break;
	case 'N': 
		OnMissedCall();//OnHandFree();
		break;
	case 'O':
		OnHandFree();//OnVolLower();
		break;
	case 'P':
		OnSaveSpeedDial();//OnVolHigher();
		break;
	case 'X': // impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_YWH200

void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'N') // CALL
	{
		Ac97StopIVR();
	}
	else if(Key_iCurrentID != 'A' && Key_iCurrentID != 'C' && Key_iCurrentID != 'N') // Andy       vol-       vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}
	
	switch (Key_iCurrentID)
	{
	case 'A':
		OnVolLower();//OnRedial();
		break;
	case 'B':
		OnPhoneNumber();//OnMissedCall();
		break;
	case 'C': 
		OnVolHigher();//OnIPAddress();
		break;
	case 'D':
		OnIPAddress();//OnBackSpace();
		break;
	case 'F':
		OnBackSpace();//OnGKAddress();
		break;
	case 'G':
		OnCallTransfer();
		break;
	case 'H':
		OnAnsweredCall();
		break;
	case 'I':
		OnGKAddress();//OnLoadSpeedDial();
		break;
	case 'J':
		OnCallHold();//OnSaveSpeedDial();
		break;
	case 'K': 
		OnHookFlash();//OnDialedCall();
		break;
	case 'L': 
		OnDialedCall();//OnAnsweredCall();
		break;
	case 'M':
		//  OnCall();
		OnSaveSpeedDial(TRUE);//OnHookFlash();
		break;
	case 'N': 
		OnHandFree();
		break;
	case 'O':
		OnRedial();//OnVolLower();
		break;
	case 'P':
		OnMissedCall();//OnVolHigher();
		break;
	case 'X': // impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_YWH100

void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'M')	// CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnRedial();
		break;
	case 'B':
		OnMissedCall();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':
		OnBackSpace();
		break;
	case 'E':
		OnPhoneNumber();
		break;
	case 'F':
		OnGKAddress();
		break;
	case 'G':	 
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnAnsweredCall();
		}
		break;
	case 'H':	
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnDialedCall();
		}
		break;
	case 'I':
		OnLoadSpeedDial();
		break;
	case 'J':
		OnSaveSpeedDial(TRUE);
		break;
//	case 'K':	
//		OnDialedCall();
//		break;
//	case 'L':	
//		OnAnsweredCall();
//		break;
	case 'M':
//		OnCall();
		OnHookFlash();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'P':
		OnVolHigher();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_COSUN
void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'M')	// CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':	
		OnRedial();
		break;
	case 'B':	
		OnMissedCall();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':	// not used, back space key in the future
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnBackSpace();
		}
		break;
	case 'E':	
		OnPhoneNumber();
		break;
	case 'F':	
		OnGKAddress();
		break;
	case 'G':	 
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnRouterAddress();
		}
		break;
	case 'H':	
		OnSubnetMask();
		break;
	case 'I':	
		OnLoadSpeedDial();
		break;
	case 'J':	
		OnSaveSpeedDial(TRUE);
		break;
	case 'K':	
		OnDialedCall();
		break;
	case 'L':	
		OnAnsweredCall();
		break;
	case 'M':
		OnCall();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':	
		OnVolLower();
		break;
	case 'P':	
		OnVolHigher();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_KINGTEL

void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'M')	// CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnRedial();
		break;
	case 'B':
		OnMissedCall();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':
		OnBackSpace();
		break;
	case 'E':
		OnPhoneNumber();
		break;
	case 'F':				// Menu/Enter
		OnLogoff();
		SdramUpdateFlagByte(FLAGS_UPGRADE_MODE, '#');	// Use Upgrade mode Flag to indicate # has been pressed
		SwitchPage(15);
		break;
	case 'G':	 
		OnCallTransfer();
		break;
	case 'H':	
		OnCallHold();
		break;
	case 'I':
		OnLoadSpeedDial();
		break;
	case 'J':
		OnHookFlash();		// Flash
		break;
	case 'K':	
		OnDialedCall();
		break;
	case 'L':	
		OnAnsweredCall();
		break;
	case 'M':
		OnCall();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'P':
		OnVolHigher();
		break;
	case 'b':
		OnDirectSpeedDial(4);
		break;
	case 'c':
		OnDirectSpeedDial(3);
		break;
	case 'd':
		OnDirectSpeedDial(2);
		break;
	case 'e':
		OnDirectSpeedDial(1);
		break;
	case 'f':
		OnDirectSpeedDial(0);
		break;
	case 'g':
		DebugCString((PCCHAR)"Set Pressed");
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined OEM_SKYCOM
void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'M')	// CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}
	
	DebugCString((PCCHAR)"HandleFunctionKeys..... in main..... ");
	DebugCString((PCCHAR)"in PalmH323 ");	
	DebugChar(Key_iCurrentID);
	DebugNewLine();
 
	SpecificKeyProcess();  //control pass key and delete memory key
	
	switch (Key_iCurrentID)
	{
	case 'A':
		OnRedial();
		break;
	case 'B':
		OnMissedCall();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':
		OnBackSpace();
		break;
	case 'E':
		OnPhoneNumber();
		break;
	case 'F':
		OnGKAddress();
		break;
	case 'G':	 
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnRouterAddress();
		}
		break;
	case 'H':	
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnSubnetMask();
		}
		break;
	case 'I':
		OnLoadSpeedDial();
		break;
	case 'J':
		OnSaveSpeedDial(TRUE);
		break;
	case 'K':	
		OnDialedCall();
		break;
	case 'L':	
		OnAnsweredCall();
		break;
	case 'M':
		OnCall();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'P':
		OnVolHigher();
		break;
	case 'g':
		//silence();
		break;
	case 'f':
	case 'e':
	case 'd':		
	case 'c':
	case 'b':
	case 'a':		
	case 'n':
	case 'm':		
	case 'l':		
	case 'k':
	case 'j':
	case 'i':		
		OnMemoryKey(Key_iCurrentID);
		break;
	case 'h':				
		Money_QueryType = 0;
		OnMoneyTcpRun();
		break;

	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_EP668
void HandleFunctionKeys()
{
	if (Key_iCurrentID != 'O' && Key_iCurrentID != 'F')	//  vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnRedial();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':
		break;
	case 'E':
		OnDialedCall();
		break;
	case 'F':
		OnVolHigher();
		break;
	case 'G':	 
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnPhoneNumber();
		}
		break;
	case 'H':	
			OnSubnetMask();
		break;
	case 'I':
		OnLoadSpeedDial();
		break;
	case 'J':
		OnAnsweredCall();
		break;
	case 'L':	
		break;
	case 'M':
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnBackSpace();
		}
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'P':
		OnMissedCall();
		break;

	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#elif defined VER_WLINK
void HandleFunctionKeys()
{
	if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P')	//  vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnRedial();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':
		if (!PIO_bHandsetUp && !PIO_bHandFree)
		{
			OnHandFree();
			OnVolHigher();
			OnVolLower();
		}
		break;
	case 'B':
		OnDialedCall();
		break;
	case 'P':
		OnVolHigher();
		break;
	case 'F':
		OnPhoneNumber();
		break;
	case 'I':
		OnLoadSpeedDial();
		break;
	case 'H':
		OnAnsweredCall();
		break;
	case 'L':	
		break;
	case 'K':
		OnCallTransfer();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'E':
		OnMissedCall();
		break;
	case 'G':		//FUN2
		OnCallHold();
		break;
	case 'M':		//FUN2
		OnHookFlash();
		break;
	case 'J':
		OnBackSpace();
		break;
	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#else	// PA168X

void HandleFunctionKeys()
{
	if (Key_iCurrentID == 'M')	// CALL
	{
		Ac97StopIVR();
	}
	else if (Key_iCurrentID != 'O' && Key_iCurrentID != 'P' && Key_iCurrentID != 'M')	// Andy       vol-             vol+
	{
		Sys_iScrollType = SCROLL_NULL;
	}

	switch (Key_iCurrentID)
	{
	case 'A':
		OnRedial();
		break;
	case 'B':
#ifdef HW_MODEM
		if (Modem_bInUse)
		{
			OnModemConnect();
		}
#endif
		OnMissedCall();
		break;
	case 'C':	
		OnIPAddress();
		break;
	case 'D':
		OnBackSpace();
		break;
	case 'E':
#if defined OEM_MEDIARING || defined OEM_RINGCHAT
		OnHookFlash();
#else
		OnPhoneNumber();
#endif
		break;
	case 'F':
		if (Task_bConnected)
		{
#if defined OEM_MEDIARING || defined OEM_RINGCHAT
			OnCallTransfer();
#else
			OnMute();
#endif
		}
		else
		{
			OnGKAddress();
		}
		break;
	case 'G':	 
		if (Task_bConnected)
		{
			OnCallTransfer();
		}
		else
		{
			OnRouterAddress();
		}
		break;
	case 'H':	
#ifdef	OEM_JR168
		OnDirectSpeedDial(9);	// MemoryDial10
#else
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnSubnetMask();
		}
#endif
		break;
	case 'I':
		OnLoadSpeedDial();
		break;
	case 'J':
#ifdef OEM_JR168
		if (IsInCall())
		{
			OnHookFlash();
		}
		else
		{
			OnSaveSpeedDial(TRUE);
		}
#else
#if defined OEM_MEDIARING || defined OEM_RINGCHAT
		OnCallHold();		
#else
		OnSaveSpeedDial(TRUE);
#endif
#endif
		break;
	case 'K':	
#ifdef HW_MODEM
		if (Modem_bInUse)
		{
			OnModemDisconnect();
		}
#endif
		OnDialedCall();
		break;
	case 'L':	
		OnAnsweredCall();
		break;
	case 'M':
		OnCall();
		break;
	case 'N':	
		OnHandFree();
		break;
	case 'O':
		OnVolLower();
		break;
	case 'P':
		OnVolHigher();
		break;

	case 'a':	// Set Key, only used in setting page.
#ifdef OEM_JR168
		OnIPAddress();
#endif
		break;

#ifdef	OEM_JR168
	case 'b':	// MemoryDial4
		OnDirectSpeedDial(3);
		break;
	case 'c':	// MemoryDial3
		OnDirectSpeedDial(2);
		break;
	case 'd':	// MemoryDial2		
		OnDirectSpeedDial(1);
		break;
	case 'e':	// MemoryDial1
		OnDirectSpeedDial(0);
		break;
	case 'f':
		if (IsInCall())
		{
			OnCallHold();
		}
		else
		{
			OnSubnetMask();
		}
		break;
#else
	case 'b':	// MemoryDial5
		OnDirectSpeedDial(4);
		break;
	case 'c':	// MemoryDial4
		OnDirectSpeedDial(3);
		break;
	case 'd':	// MemoryDial3		
		OnDirectSpeedDial(2);
		break;
	case 'e':	// MemoryDial2
		OnDirectSpeedDial(1);
		break;
	case 'f':	// MemoryDial1
		OnDirectSpeedDial(0);
		break;
#endif

	case 'g':	// Mute Key	
		OnMute();
		break;
	case 'h':	// Menu/Enter Key
		OnLogoff();
		SdramUpdateFlagByte(FLAGS_UPGRADE_MODE, '#');	// Use Upgrade mode Flag to indicate # has been pressed
		SwitchPage(15);
		break;

#ifdef	OEM_JR168
	case 'i':	// MemoryDial9		
		OnDirectSpeedDial(8);
		break;
	case 'j':	// MemoryDial8
		OnDirectSpeedDial(7);
		break;
	case 'k':	// MemoryDial7
		OnDirectSpeedDial(6);
		break;
	case 'l':	// MemoryDial6
		OnDirectSpeedDial(5);
		break;
	case 'm':	// MemoryDial5		
		OnDirectSpeedDial(4);
		break;
#else
	case 'i':	// MemoryDial10		
		OnDirectSpeedDial(9);
		break;
	case 'j':	// MemoryDial9
		OnDirectSpeedDial(8);
		break;
	case 'k':	// MemoryDial8
		OnDirectSpeedDial(7);
		break;
	case 'l':	// MemoryDial7
		OnDirectSpeedDial(6);
		break;
	case 'm':	// MemoryDial6		
		OnDirectSpeedDial(5);
		break;
#endif

	case 'n':	// HookFlash Key
		OnHookFlash();
		break;

	case 'X':	// impossible key
		_OnImpossibleKey();
		break;
	}
}

#endif


