#include "Page1.h"

#include "p_dsp.h"
#include "gdiconst.h"

#include "v_task.h"

#ifdef VER_IPH_E00

#ifdef VER_IPH_E00_2G
#define	Menu_Confirm		0
#define SCROLL_CALLREC_MENU	1
#endif

extern	XLONG vida_iDurationTime;

XCHAR Modem_iConnectIndicate;
static XCHAR _iSpeedDialIndex = 0;
static XCHAR _iSpeedDialScrool = 1;
#define _BalanceonSpeedDial 99;

XCHAR Task_iSpecKey;
XCHAR iConfigIndex;
//*****VIDA CHANGED*****//
XCHAR iIVRIndex;

//Add iPH-E00-2G Board support, by Louis 20040628
XCHAR Menu_iScrollItem;		
XCHAR Menu_iScrollType;		

//for gk led indicate
XCHAR _iIndicatorFlashing;
XCHAR Sys_iLedFlag = 0;
BOOLEAN	LED_bBlink=FALSE;

static CCHAR _cCallDisplayPos[3] = {15, 13, 13};

#ifndef VER_IPH_E00_2G
static CCHAR _CdrRemain[] =	"Remain Time     99:00:00";
static CCHAR _CallDuration[] =	"Last Call Dur.  00:00:00";
#else
static CCHAR _CdrRemain[] =	"Remain  99:00:00";
static CCHAR _CallDuration[] =	"Dur.    00:00:00";
#endif

/* CDR Remain Time */
#ifndef VER_IPH_E00_2G
#define	DURATION_HOUR_LOC			(16)
#define	DURATION_MINUTE_LOC			(19)
#define	DURATION_SECOND_LOC			(22)
#else
#define	DURATION_HOUR_LOC			(8)
#define	DURATION_MINUTE_LOC			(11)
#define	DURATION_SECOND_LOC			(14)
#endif

//*****VIDA CHANGED*****//
//IVR Index
#define	IVR_STATIC				0
#define	IVR_DHCP				1
#define	IVR_PPPOE				2
#define	IVR_MODEM				3
#define	IVR_PREPAID_ACCOUNT	4
#define	IVR_PREPAID_PASSWORD	5
#define	IVR_IP_ADDR			6
#define	IVR_ROUTE_ADDR		7
#define	IVR_SUBNET_MASK		8
#define	IVR_DNS_ADDR			9
#define	IVR_ISP_NUMBER		10
#define	IVR_PPP_ID				11
#define	IVR_PPP_PASSWORD		12
#define	IVR_FINISH				13


// Capital MAP
#define	MAP_ROW			10
#define	MAP_COL			10
static CCHAR _cIVRLetterMap[MAP_ROW][MAP_COL] =
{
	{'0', ' ', ':', ';', '/', '-', '_', '$', '%', 0},
	{'1', '.', ',', '?', '~', '!', '@', '&', '#', 0},
	{'2', 'a', 'b', 'c', 'A', 'B', 'C', 0, 0, 0},
	{'3', 'd', 'e', 'f', 'D', 'E', 'F', 0, 0, 0},
	{'4', 'g', 'h', 'i', 'G', 'H', 'I', 0, 0, 0},
	{'5', 'j', 'k', 'l', 'J', 'K', 'L', 0, 0, 0},
	{'6', 'm', 'n', 'o', 'M', 'N', 'O', 0, 0, 0},
	{'7', 'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S', 0},
	{'8', 't', 'u', 'v', 'T', 'U', 'V', 0, 0, 0},
	{'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z', 0}
};

void VidaShowDuration(ULONG duration)
{
	DCHAR		i;
	DCHAR		durationLoc = DURATION_MINUTE_LOC;
	XCHAR		dTime[3];		// [0] for hour, [1] for minute, [2] for second
	PXCHAR_D	pCdrDuration;

	SdramRead(PROTOCOL_SDRAM_HI, VIDASYS_DATA_SDRAM_LO, CDR_DURATION_LEN/4);
	pCdrDuration = (PXCHAR)(Sdram_pDataCache+CDR_DURATION_POS);

	if(Task_bConnected)
		for (i=0;i<DISPLAY_MAX_CHAR;i++)
			pCdrDuration[i] = _CdrRemain[i];
	else
		for (i=0;i<DISPLAY_MAX_CHAR;i++)
			pCdrDuration[i] = _CallDuration[i];

	if (duration < SECONDS_PER_99HRS)
	{
		dTime[0] = (UCHAR)(duration/SECONDS_PER_HOUR);
		dTime[1] = (UCHAR)((duration%SECONDS_PER_HOUR)/SECONDS_PER_MINUTE);
		dTime[2] = (UCHAR)(duration%SECONDS_PER_MINUTE);

		pCdrDuration[DURATION_HOUR_LOC] = dTime[0]/10 + '0';		// first value
		pCdrDuration[DURATION_HOUR_LOC+1] = dTime[0]%10 + '0';		// second value
		pCdrDuration[DURATION_HOUR_LOC+2] = ':';
		for (i=1; i<3; i++)
		{
			pCdrDuration[durationLoc++] = dTime[i]/10 + '0';			// first value
			pCdrDuration[durationLoc] = dTime[i]%10 + '0';				// second value
			durationLoc +=2;
		}
	}
	else//set max display value
	{
		for (i=DURATION_HOUR_LOC;i<DISPLAY_MAX_CHAR;i++)
			pCdrDuration[i] = _CdrRemain[i];
	}

	SdramWrite(PROTOCOL_SDRAM_HI, VIDASYS_DATA_SDRAM_LO, CDR_DURATION_LEN/4);
	DisplayLine((PXCHAR)(Sdram_pDataCache+CDR_DURATION_POS), 1);	// display on second line
}


void VidaCallRecDisplay(UCHAR iPos)
{
	CallRecDisplay(Sys_iScrollType);
	if (!iPos)
	{
		DisplayString(g_cEmpty, 1);
	}
	else
	{
		p_itoa(iPos, (PXCHAR)Sdram_pDataCache, 10);
		Display((PXCHAR)Sdram_pDataCache, 0, _cCallDisplayPos[Sys_iScrollType], TRUE);
		DisplayLine(Key_pBuf, 1);
		SdramSpeedDial(iPos);
	}
}

void _OnKeyFunctions()
{
	DCHAR iVal;

	if(!p_strlen_x(Key_pBuf)&&!IsModemConnect())
	{
		#ifdef VER_IPH_E00_2G
		L3ClockOn();
		#endif
		// Switch to Menu Configuration
		OnLogoff();
		//OnLogoff();
		SdramUpdateFlagByte(FLAGS_UPGRADE_MODE, '#');	// Use Upgrade mode Flag to indicate # has been pressed
		SwitchPage(15);
 	}
 	else
	{
	iVal = (UCHAR)p_atoi(Key_pBuf, 10);
	switch (iVal)
	{
	case 0:
//		OnUpdateOptions();
		break;
	case 1:			// log off and PPPoE, Modem Disconnect
		OnLogoff();
		break;
	case 2:			// Firmware Upgrade
		DisplayString((PCCHAR)"Firmware Upgrade", 0);
		break;
	case 5:			// Display Local IP addr
		// stop remote debug
		UdpDebugRemoteStop();
		DisplayString(g_cLocalIP[Sys_iIPType], 0);
		if (!IsCallMode())
		{
			SdramPlayIP(IVR_MSG_LOCAL, IVR_MSG_IP, Local_pIPAddress);
		}
		break;
	case 6:			
#ifdef HW_MODEM
 if(Sys_iIPType==IP_TYPE_MODEM)
   {
		ModemInitialize();
		ModemRedial();
		TcpipInitialize();
 	}
#endif
		break;
	case 7:			
		OnUpdateOptions();
		break;
	default:
		DtmfBusy();
		break;
	}
}
}

#ifdef E00_NEW_HW
void L3Led_Mute_On()
{
	L3ClockOff();
}	
void L3Led_Mute_Off()
{
	L3ClockOn();
}	
void L3Led_Spk_On()
{
	L3DataOff();
}	
void L3Led_Spk_Off()
{
	L3DataOn();
}
#endif	// E00_NEW_HW

void OnPhoneNumber()
{
	if(!IsCallMode())
	{
		Task_iTimer = 0;
		if (Task_iSpecKey == KEY_IP_TELNUM)
		{
			// prevent to play IVR while at ringing or ringback state, Andy-190503
			if (Task_bConnecting)		return;
#ifdef CALL_H323
			if (Sys_iServiceType == H323_SERVICE_TYPE_ETALK)
			{
				DisplayString((PCCHAR)"ETALK PIN:",0);
				SdramPlayString(Sys_pPin);
				if (!IsCallMode())
				{
					SdramPhoneNumber();
				}
			}
			else
#endif
			{
				DisplayString(g_cPhoneNumber, 0);
				DisplayLine(Sys_pLocalNumber, 1);
				if (!IsCallMode())
				{
					SdramPhoneNumber();
				}
			}
			Task_iSpecKey = KEY_TYPE_NULL;
		}
		else
		{
			UdpDebugRemoteStop();
			DisplayString(g_cLocalIP[Sys_iIPType], 0);
			if (!IsCallMode())
			{
				SdramPlayIP(IVR_MSG_LOCAL, IVR_MSG_IP, Local_pIPAddress);
			}
			Task_iSpecKey = KEY_IP_TELNUM;
		}
	}
}

BOOLEAN _GetSpeedDial()
{
	PXCHAR_D pBuf;
	DCHAR iIndex;

	if (p_strlen_x(Key_pBuf))
	{
		_iSpeedDialIndex = (UCHAR)p_atoi(Key_pBuf, 10) - 1;
		if (_iSpeedDialIndex >= SPEED_DIAL_NUM)
		{
			_iSpeedDialIndex = 0;
			return FALSE;	
		}
	}

	iIndex = _iSpeedDialIndex;
	iIndex <<= 1;
	pBuf = ReadSpeedDial(iIndex);

	while (!CheckSpeedDial(pBuf))
	{
		if (_iSpeedDialScrool)
			_iSpeedDialIndex ++;
		else
			_iSpeedDialIndex --;
		if (_iSpeedDialIndex >= SPEED_DIAL_NUM)
		{
			return FALSE;
		}
		iIndex = _iSpeedDialIndex;
		iIndex <<= 1;
		pBuf = ReadSpeedDial(iIndex);
	}

	Key_bCopied = TRUE;
	p_strcpy_x2x(Key_pBuf, pBuf);
	iIndex ++;
	pBuf = ReadSpeedDial(iIndex);
	DisplayLine(pBuf, 1);	// display name here
	return TRUE;
}

void OnBalanceIVRcall()
{	
	PXCHAR pBuf;
	XCHAR iIndex;
	
	//p_memcpy_x2x_0((PXCHAR)(Key_pBuf), "100", 3);
	//Key_pBuf="100";
	_iSpeedDialIndex =_BalanceonSpeedDial;
	iIndex = _iSpeedDialIndex;
	iIndex <<= 1;
	if (!IsCallMode() && IsFunctionMode())
	{	
		pBuf = ReadSpeedDial(iIndex);
		DisplayString(g_cMemory, 0);
		
		if (CheckSpeedDial(pBuf))
		{
					Key_bCopied = TRUE;
		p_strcpy_x2x(Key_pBuf, pBuf);
		DisplayLine(Key_pBuf, 1);

		iIndex ++;
		pBuf = ReadSpeedDial(iIndex);
		DisplayLine(pBuf, 0);
		DisplayLine(Key_pBuf, 0);
		Sys_iScrollType = SCROLL_SPEED_DIAL;
		OnCall();
			}
			
	else
		{
			KeypadClear();
			DisplayString(g_cEmpty, 1);
		}
		
	}

	
}


void OnSaveSpeedDial(BOOLEAN bSpeedDial)
{
	if (bSpeedDial)
	{
		_iSpeedDialIndex = 0;
		_iSpeedDialScrool = 1;
		KeypadClear();
	}

	if (!IsCallMode() && IsFunctionMode())
	{
//		DisplayString(g_cMemory, 0);
		if (!_GetSpeedDial())
		{
			KeypadClear();
			DisplayString((PCCHAR)"PhoneBook Empty", 1);
		}
		else
		{
			SdramSpeedDial((UCHAR)(_iSpeedDialIndex+1));
			Sys_iScrollType = SCROLL_SPEED_DIAL;
		}
	}
}

#ifdef VER_IPH_E00_2G
void _CallRecMenuDisplay()
{
	switch(Menu_iScrollItem)
	{
	case 0:
		DisplayString((PCCHAR)"->Missed Calls", 1);
		break;
	case 1:
		DisplayString((PCCHAR)"->Dialled Calls", 1);
		break;
	case 2:
		DisplayString((PCCHAR)"->Answered Calls", 1);
		break;
	}	
}
#endif

//*****VIDA CHANGED*****//
BOOLEAN IsSetupMode()
{
	if (!IsFunctionMode())		return FALSE;
	return Task_bSetupMode;
}

void OnVolHigher()
{
	DCHAR	iPos;

//*****VIDA CHANGED*****//
	if (IsCallMode() && !IsSetupMode())
	{
		DisplayString(g_cVolPlus, 0);
		Ac97OutputVolHigher();
		DisplayOutputVol();
	}
	else
	{
		if (Sys_iScrollType == SCROLL_SPEED_DIAL)
		{
			_iSpeedDialIndex = OnScroll(_iSpeedDialIndex, SPEED_DIAL_NUM, TRUE);
			_iSpeedDialScrool = 1;
			OnSaveSpeedDial(FALSE);
		}
#ifndef VER_IPH_E00_2G
		else if (Sys_iScrollType < SCROLL_SPEED_DIAL)
		{
			iPos = CallRecReadScroll(TRUE);
			VidaCallRecDisplay(iPos);
		}
#else
		else if (Sys_iScrollType < SCROLL_SPEED_DIAL && Menu_iScrollType== Menu_Confirm)
		{
			iPos = CallRecReadScroll(TRUE);
			VidaCallRecDisplay(iPos);
		}
		else if (Menu_iScrollType == SCROLL_CALLREC_MENU && Task_iSpecKey == KEY_CALL_RECORD)
		{
			if( ++Menu_iScrollItem > 2)
				Menu_iScrollItem = 0;
			_CallRecMenuDisplay();
		}
#endif		
	}
}

void OnVolLower()
{
	DCHAR	iPos;

//*****VIDA CHANGED*****//
	if (IsCallMode() && !IsSetupMode())
	{
		DisplayString(g_cVolMinus, 0);
		Ac97OutputVolLower();
		DisplayOutputVol();
	}
	else
	{
		if (Sys_iScrollType == SCROLL_SPEED_DIAL)
		{
			_iSpeedDialIndex = OnScroll(_iSpeedDialIndex, SPEED_DIAL_NUM, FALSE);
			_iSpeedDialScrool = 0;
			OnSaveSpeedDial(FALSE);
		}
#ifndef VER_IPH_E00_2G
		else if (Sys_iScrollType < SCROLL_SPEED_DIAL)
		{
			iPos = CallRecReadScroll(FALSE);
			VidaCallRecDisplay(iPos);
		}
#else
		else if (Sys_iScrollType < SCROLL_SPEED_DIAL && Menu_iScrollType== Menu_Confirm)
		{
			iPos = CallRecReadScroll(FALSE);
			VidaCallRecDisplay(iPos);
		}
		else if (Menu_iScrollType == SCROLL_CALLREC_MENU && Task_iSpecKey == KEY_CALL_RECORD)
		{

			if( Menu_iScrollItem-- == 0)
				Menu_iScrollItem = 2;
			_CallRecMenuDisplay();			
		}
#endif
		else if (Sys_iScrollType == SCROLL_NULL && IsFunctionMode())
		{
			OnBackSpace();
		}
	}
}

void OnCall()
{
	DCHAR iLen;

#if defined CALL_H323 || defined CALL_MGCP || defined CALL_N2P || defined CALL_EPN || defined CALL_WP
	if (Task_bConnected)	return;
#endif	

	iLen = p_strlen_x(Key_pBuf);

	DisplayString(g_cCall, 0);
	Task_iSpecKey = KEY_TYPE_NULL;

	if (!iLen)
	{
		DtmfBusy();
		return;
	}

	if (iLen > 1 && Key_pBuf[0] == '#')
	{
		CopySpeedDialKeypad();
		if (!_GetSpeedDial())
		{
			UndoSpeedDialKeypad();
//			DtmfBusy();
//			return;
		}
	}

	if (!IsCallMode())
	{
#ifndef CALL_MGCP
		OnPrepareCall();
#endif
		PIO_bHandFree = TRUE;
		Key_bCopied = TRUE;
#ifdef E00_NEW_HW
		L3Led_Spk_On();
#else
		Led_On(SPK_LIGHT);
#endif // E00_NEW_HW
		OnMute();
	}

#if defined CALL_H323 || defined CALL_MGCP || defined CALL_N2P || defined CALL_EPN || defined CALL_WP
	if (IsFunctionMode())
#endif		
	{
		DialPlanBuildNumber();

		// To prevent self-number dialing, Andy-190503
		if (!p_strcmp_x2x(Sys_pRemoteNumber, Sys_pLocalNumber))
		{
			Ac97Stop();
			OnCheckHandset();
			return;
		}

		UpdateRedialBuf();
		CallRecWriteStart(Key_pBuf, CALL_TYPE_DIALED);
		if (Key_bCopied)
		{
			DtmfPlayKeys();
		}
		else
		{
			TaskCall();
		}
		p_memcpy_x2x_0(Sys_pPeerDisplay, Key_pBuf, PHONE_NUMBER_LEN);
		DisplayLine(Sys_pPeerDisplay, 1);
	}
}

void LedMute()
{
	if (Ac97_bMuteInput)
	{
#ifdef E00_NEW_HW
		L3Led_Mute_On();
#else
		Led_On(MUTE_LIGHT);
#endif	// E00_NEW_HW
	}
	else
	{
#ifdef E00_NEW_HW
		L3Led_Mute_Off();
#else
		Led_Off(MUTE_LIGHT);
#endif	// E00_NEW_HW
	}
}

void _LoadNumToDialOut()
{
	if (Sys_iScrollType <= SCROLL_SPEED_DIAL || p_strlen_x(Key_pBuf))
	{
		Key_bCopied = TRUE;
		OnCall();
	}
}

//Add store config feature support, by Anfernee 20040810
void OnStoreSettings(UCHAR iOptions)
{
	if (!IsFunctionMode())		return;

	if (Task_iSpecKey == KEY_CONFIG_STORE)
	{
		Task_iSpecKey = KEY_TYPE_NULL;
		DisplayString("Save Completed", 1);
		SysStoreOptions(iConfigIndex);
		TaskRestart(0);
	}
	else
	{
		Task_iTimer = 0;
		Task_iSpecKey =	KEY_CONFIG_STORE;
		iConfigIndex = iOptions;
		DisplayString((PCCHAR)"Store Settings",0);
		DisplayString((PCCHAR)"Are you sure?",1);
	}
}

//Add load config feature support, by Anfernee 20040810
void OnLoadSettings(UCHAR iOptions)
{
	if (!IsFunctionMode())		return;

	if (Task_iSpecKey == KEY_CONFIG_LOAD)
	{
		Task_iSpecKey = KEY_TYPE_NULL;
		SysLoadOptions(iConfigIndex);
		SysWriteOptions();
		OnUpdateOptions();
	}
	else
	{
		Task_iTimer = 0;
		Task_iSpecKey =	KEY_CONFIG_LOAD;
		iConfigIndex = iOptions;
		DisplayString((PCCHAR)"Load Settings",0);
		DisplayString((PCCHAR)"Are you sure?",1);
	}
}

void OnHandFree()
{
#ifndef VER_IPH_E00_2G
//Add store config feature support, by Anfernee 20040810
	if(Task_iSpecKey ==	KEY_CONFIG_STORE)
	{
		OnStoreSettings(0);
	}
//Add load config feature support, by Anfernee 20040810
	else if(Task_iSpecKey ==	KEY_CONFIG_LOAD)
	{
		OnLoadSettings(0);
	}	
	else
	{
#endif
// Original Handfree function
		if (PIO_bHandFree)
		{
			Ac97_bMuteInput = FALSE;
			PIO_bHandFree = FALSE;
			if (!PIO_bHandsetUp)
			{
				HangupPhone();
			}
			// switch to handset operation
#ifdef E00_NEW_HW
			L3Led_Spk_Off();
#else
			Led_Off(SPK_LIGHT);
#endif	// E00_NEW_HW
			LedMute();
		}
		else
		{
			PIO_bHandFree = TRUE;
			OnPrepareCall();
			OnHotLine();
			// Switch to Hand-free operation
			Ac97_bMuteInput = TRUE;
#ifdef E00_NEW_HW
			L3Led_Spk_On();
#else
			Led_On(SPK_LIGHT);
#endif	// E00_NEW_HW
			OnMute();
#ifdef E00_NEW_HW
		L3Led_Mute_Off();
#else
		Led_Off(MUTE_LIGHT);
#endif	// E00_NEW_HW
                        if (IsFunctionMode())
			{
				Task_iSpecKey = KEY_TYPE_NULL;
				DisplayNormalState();
				_LoadNumToDialOut();
			}
		}
		Ac97OnHandsetChange();
		KeypadClear();
	}
#ifndef VER_IPH_E00_2G
}
#endif


void OnHandset()
{
	Task_iTimer = 0;

	Ac97_bMuteInput = FALSE;

	if (PIO_bHandsetUp)
	{
		OutputString(IDS_HANDSET_UP);
		if (PIO_bHandFree)
		{
			PIO_bHandFree = FALSE;
#ifdef E00_NEW_HW
			L3Led_Spk_Off();
#else
			Led_Off(SPK_LIGHT);
#endif	// E00_NEW_HW
			LedMute();
		}
		else
		{
			OnPrepareCall();
			OnHotLine();
			if (IsFunctionMode())
			{
				Task_iSpecKey = KEY_TYPE_NULL;
				DisplayNormalState();
				_LoadNumToDialOut();
			}
		}
	}
	else
	{
		OutputString(IDS_HANDSET_DOWN);
		if (!PIO_bHandFree)
		{
			HangupPhone();
			LedMute();
		}
	}
	Ac97OnHandsetChange();
	KeypadClear();
}

void SetSpkOutput(UCHAR iVol)
{
	DCHAR iTmpVol;

	iTmpVol = Ac97_iHandfreeOutputVol;
	Ac97_iHandfreeOutputVol = iVol;
	Ac97SetOutputVol();
	Ac97_iHandfreeOutputVol = iTmpVol;
}

void OnGetBalance()
{
}

void OnCallRecord()
{
	if (!IsCallMode() && IsFunctionMode())
	{
		Task_iTimer = 0;
		if (Task_iSpecKey == KEY_CALL_RECORD)
		{
			DisplayNormalState();
			Task_iSpecKey = KEY_TYPE_NULL;
		}
		else
		{
			DisplayString((PCCHAR)"Call Record:", 0);
#ifndef VER_IPH_E00_2G
			DisplayString((PCCHAR)"  ANS  MISS  DIAL", 1);
#else
			Menu_iScrollItem = 0;
			_CallRecMenuDisplay();			
			Menu_iScrollType = SCROLL_CALLREC_MENU;
#endif
			Task_iSpecKey = KEY_CALL_RECORD;
		}
	}
}

void UI_CallRecRead(UCHAR iReadType)
{
	DCHAR iType, iIndex;

	iType = iReadType;
	CallRecDisplay(iType);
	iIndex = CallRecRead(iType);
	if (iIndex)
	{
		p_itoa(iIndex, (PXCHAR)Sdram_pDataCache, 10);
		Display((PXCHAR)Sdram_pDataCache, 0, _cCallDisplayPos[iType], TRUE);
		CallRecGetStartTime(iType, iIndex);	// prevent DCHAR consumption
		DisplayLine(Key_pBuf, 1);
		SdramSpeedDial(iIndex);
	}
	else
	{
		DisplayString(g_cEmpty, 1);
		OutputString(IDS_CALL_REC_EMPTY);
	}
}

void UIMissedCallIndicate()
{
	Sys_iLedFlag |= LED_MISSCALL_OR_MASK;
}

#ifdef VER_IPH_E00_2G
void OnSoftKeys()
{
	if (Task_iSpecKey == KEY_CALL_RECORD && Menu_iScrollType== SCROLL_CALLREC_MENU)
	{
		Menu_iScrollType = Menu_Confirm;				
		switch(Menu_iScrollItem)
		{
		case 0:
			OnSoftKey2();
			break;
		case 1:
			OnSoftKey3();
			break;
		case 2:
			OnSoftKey1();
			break;	
		}
	}
}			
#endif

void OnSoftKey1()
{
	if (!IsFunctionMode())		return;

	if (Task_iSpecKey == KEY_CALL_RECORD)
	{
		// Display Answered Calls
		UI_CallRecRead(CALL_TYPE_ANSWERED);
	}
	Task_iSpecKey = KEY_TYPE_NULL;
}

void OnSoftKey2()
{
	if (!IsFunctionMode())		return;

	if (Task_iSpecKey == KEY_CALL_RECORD)
	{
		// Display Missed Calls
		Task_iSpecKey = KEY_TYPE_NULL;
		UI_CallRecRead(CALL_TYPE_MISSED);
		Sys_iLedFlag &= LED_MISSCALL_AND_MASK;
		Led_Off(NORMAL_LIGHT);
	}
}

void OnSoftKey3()
{
	if (!IsFunctionMode())		return;

	if (Task_iSpecKey == KEY_CALL_RECORD)
	{
		// Display Dialed Numbers
		UI_CallRecRead(CALL_TYPE_DIALED);
	}
	Task_iSpecKey = KEY_TYPE_NULL;
}

//*****VIDA CHANGED*****//

void OnFuncKey()
{
		if (IsFunctionMode())
		{
			if(!IsModemConnect())
			{
				DisplayString((PCCHAR)" ", 0);
			}
			_OnKeyFunctions();
			KeypadClear();
		}
}
/*
void OnDelete()
{
	if (Sys_iScrollType < SCROLL_SPEED_DIAL)		// For Voice Message
	{
		DisplayString((PCCHAR)"Call REC deleted", 0);
		Ac97StopIVR();
		if (!CallRecDelete((UCHAR)Sys_iScrollType, FALSE))
		{
			DisplayString(g_cEmpty, 1);
		}
		Task_iTimer = FUNCTION_TIME_OUT - 3;
		Task_iSpecKey = KEY_CALLREC_DELETE;
	}
}
*/
void UIHandleTimer()
{
	if (Sys_iLedFlag&LED_MISSCALL_OR_MASK)
	{
		Led_Blink(NORMAL_LIGHT);
	}

	if (Task_iTimer == FUNCTION_TIME_OUT)
	{
		if (Ac97_bIVR || IsUpgrading())
		{
			Task_iTimer = 0;
		}
		else
		{
			if (Task_iSpecKey == KEY_TYPE_NULL)
			{
				Sys_iScrollType = SCROLL_NULL;
				DisplayNormalState();
			}
			if (Task_iSpecKey == KEY_CALLREC_DELETE)
			{
				// Automatically play the next call record
				UI_CallRecRead((UCHAR)Sys_iScrollType);
				Task_iSpecKey = KEY_TYPE_NULL;
			}
		}
	}
	else if (Task_iTimer == MENU_TMR)
	{
		if (SoloFileIsRunning())
		{
			Task_iTimer = 0;
		}
		else
		{
			Task_iSpecKey = KEY_TYPE_NULL;
			Sys_iScrollType = SCROLL_NULL;
			if (IsFunctionMode())
			{
				KeypadClear();
				DisplayNormalState();
			}
			else if (Task_bConnected)
			{
				DisplayString(g_cConnected, 0);
				DisplayLine(Sys_pPeerDisplay, 1);
			}
		}
	}
	if (Task_bConnected)
	{
		vida_iDurationTime ++;
	}
	else
	{
	if (!(Sys_lCurrentTime % 40))
      { 
        if(Sys_iIPType==IP_TYPE_MODEM)
      	if((Modem_iConnectIndicate&SET_BIT0_2_TO_1)==SET_BIT0_2_TO_1) //bit2 and bit0
      	{
       //if((Modem_iConnectIndicate&0x02) != 0x02)  //bit 1 use Task_bConnecting
				if(!Task_bConnecting)
				{
					Modem_iConnectIndicate+=COUNT_BIT4; //bit4 to count
			  		if (Modem_iConnectIndicate>(13))
			  		{
			  			OnUpdateOptions();
			  		}
		      }
				else
				Modem_iConnectIndicate&=CLEAR_BIT4;  //clear bit4
	         }
         }
		CommonUIHandleTimer();
	}
}

void UIStartup()
{
}

void UIInitialize()
{
	Ac97_bMuteInput = FALSE;
	Task_iSpecKey = KEY_TYPE_NULL;
	Task_bSetupMode = FALSE;
}

void UIRingIndicate(BOOLEAN bRinging)
{
	if (bRinging)
	{
		Led_Blink(SECOND_LIGHT);
	}
	else
	{
		Led_Off(SECOND_LIGHT);
	}
}

//*****VIDA CHANGED*****//

BOOLEAN UIHandleSpecialKeys()
{
	DCHAR iLen;

	iLen = p_strlen_x(Key_pBuf);
//*****VIDA CHANGED*****//
/*
#ifdef _USE_FAKE_FXO
	if (Sys_iDualMode == DUAL_MODE_IP_FIRST)
	{
		if (!p_memcmp_x2x(Key_pBuf, Sys_pDualModePrefix, iLen))
		{
//			OnSwitchToPSTN();
			SwitchToFXO(TRUE);
		}
	}
#endif
*/

	if ((Key_iCurrentID == '*' || Key_iCurrentID == '#') && Task_bSetupMode)
	{
		if (!iLen)
	{
			if (Key_iCurrentID == '#')
			{
#ifdef VER_IPH_E00_2G
				L3ClockOn();
#endif
				// Switch to Menu Configuration
				OnLogoff();
				SwitchPage(15);
			}
			else
			{
				OnReboot();
			}
		}
	}

/*	if (Key_iCurrentID == '*')
	{
		if (!iLen)
		{
			OnDelete();
			return TRUE;
		}
	}
	else if (Key_iCurrentID == '#')
*/
	if (Key_iCurrentID == '#' && !Task_bSetupMode)
	{
		if (iLen && !Sys_bUseDigitMap)
		{
			if (Sys_iScrollType <= SCROLL_SPEED_DIAL)
			{
//				OnPrepareCall();						// need?
				Key_bCopied = TRUE;
			}
			OnCall();
			return TRUE;
		}
	}

	return FALSE;
}

void DisplayKeys()
{
	Display(Key_pBuf, 0, 0, FALSE);

	if (p_strlen_x(Key_pBuf) > DISPLAY_MAX_CHAR)
	{
		Display((PXCHAR)(Key_pBuf+DISPLAY_MAX_CHAR), 1, 0, FALSE);
	}
}

void OnBackSpace()
{
	KeypadBackspace();
	if (p_strlen_x(Key_pBuf))
	{
		DisplayKeys();
	}
	else
	{
		DisplayNormalState();
	}
}

void GKIndicate()
{
	if (Sys_bUseService&&(!Task_bLogoned))
	{
		_iIndicatorFlashing++;
		if(_iIndicatorFlashing > 2 ) 
		{
			_iIndicatorFlashing = 0;
#ifdef VER_IPH_E00_2G
			L3ClockBlink();
#else
			Led_Blink(SECOND_LIGHT);
#endif
			LED_bBlink= TRUE;	

		}
	}
	else
	{
		if (((Modem_iConnectIndicate &SET_BIT2_TO_1)!=SET_BIT2_TO_1)&&Sys_iIPType==IP_TYPE_MODEM)
			{
				Modem_iConnectIndicate |=SET_BIT2_TO_1;		//set bit 2 to 1 
			}
		if(LED_bBlink == TRUE) 
			{
				LED_bBlink= FALSE;	
			}					

		if(!Task_bConnecting) 
		{
#ifdef VER_IPH_E00_2G
		L3ClockOff();
#else
		Led_On(SECOND_LIGHT);
#endif
		}
	}
}

/*
void ModemChangeStatus()
{
	Modem_iConnectIndicate|=SET_BIT0_TO_1;	//set bit0 to 1 Simon 
}
*/

void ModemInitializeStatus()
{
	Modem_iConnectIndicate=0;	//set all bit0 to 0 Simon 
}
BOOLEAN IsModemConnect()
{ 
	if ((Modem_iConnectIndicate&SET_BIT0_TO_1)==SET_BIT0_TO_1)
		{
		return TRUE;
		}
		return FALSE;
}

#ifdef HW_MODEM
void _UpdateVal(UCHAR iOffset, UCHAR iVal)
{
	SdramReadOptions();
	X_VAL(Sdram_pDataCache, iOffset) = iVal;
	SdramWriteOptions();
//	OnUpdateOptions();
}

void OnModemConnect()
{
	_UpdateVal(OPTIONS_IP_TYPE, IP_TYPE_MODEM);
}

void OnModemDisconnect()
{
	_UpdateVal(OPTIONS_IP_TYPE, IP_TYPE_DHCP);
}
#endif

#endif
