--[[
  CDU6000.lua 
  $Id: CDU6000.lua,v 1.31 2006/07/12 04:42:40 mirage Exp $
]]
--------------------------------------------------------------------------------

dofile( "gauges/CDU.lua" );

--Groval vars

current_page = "page_stat";
size_x = 24;
size_y = 14;
color = {
	c0 = { r = 255;	g = 254;	b = 253; };
	c1 = { r = 255;	g = 255;	b = 255; };
	c2 = { r = 255;	g = 255;	b = 255; };
	c3 = { r = 255;	g = 255;	b = 255; };
	c4 = { r = 250;	g = 120;	b = 190; };
	c5 = { r = 86;	g = 250;	b = 190; };
	c6 = { r = 86;	g = 190;	b = 250; };
	c7 = { r = 255;	g = 255;	b = 255; };
};

--Groval vars
i = -1;
wait_Counter = 0;

F_KEY = {
	DEL = -1;
	EXEC = -2;
	MSG = -3;
	IDX = -4;
	DIR = -10;
	FPLN = -11;
	LEGS = -12;
	DEPAPR = -13;
	PERF = -14;
	MFDMENU = -15;
	MFDADV = -16;
	MFDDATA = -17;
	PREV = -18;
	NEXT = -19;


	LS_U = -30;
	LS_D = -35;
	RS_U = -50;
	RS_D = -55;
};

--------------------------------------------------------------------------------
-- init page
--------------------------------------------------------------------------------
function page_init( p )


	FMScore_setLine( p, 0, 2,	"        MIRAGE FMS      " );
	FMScore_setLine( p, 0, 4,	"       Version 0.01     " );
	FMScore_setLine( p, 0, 6,	"     (C)MIRAGE WORKS    " );
	FMScore_setLine( p, 0, 8,	" ABCDEFGHIJKLMNOPQRSTUV " );
	FMScore_setLine( p, 0, 9,	" WXYZ0123456789-_+][;:/ " );
	FMScore_setLine( p, 0, 10,	" ><\\?.,\'\"}{][|`~@#$% " );
	FMScore_setLine( p, 0, 11,	" %^&*()                 " );
	FMScore_setLine( p, 0, 12,	"------------------------" );
	FMScore_setLine( p, 0, 13,	"[                      ]" );

	for i = 0, 18, 1
	do
		FMScore_setText( p, 0, i, "<" );
		FMScore_setText( p, 23, i, ">" );
	end

	cnt = cnt + cntMult;

	if cnt > 100 then
		current_page = "page_legs";
	end

	contents_buffer:clear( 12 );

end


--------------------------------------------------------------------------------
-- test page
--------------------------------------------------------------------------------
function page_test( p )


	if cnt > 100 and cnt < 150 then
		FMScore_setLine( p, 0, 2, 	"      SELF TEST OK      " );
	elseif cnt < 180 then
		FMScore_setLine( p, 0, 2,	"       IRS ONLINE       " );
	elseif cnt < 240 then
		FMScore_setLine( p, 0, 2,	"      FMC DATA LOAD     " );
	elseif cnt < 400 then
		FMScore_setLine( p, 0, 2,	"      INITIALIZE OK     " );
	end

	FMScore_setLine( p, 0, 4,	"      Version  0.01     " );
	FMScore_setLine( p, 0, 6,	"       PLEASE WAIT      " );

	FMScore_setLine( p, 0, 12,	"------------------------" );
	
	modvar = math.mod( cnt, 4 );

	if modvar == 0 then
		FMScore_setLine( p, 12, 11, ".\0", 7, true );
	elseif modvar == 1 then
		FMScore_setLine( p, 11, 11, "..\0" );
	elseif modvar == 2 then
		FMScore_setLine( p, 12, 11, "...\0" );
	elseif modvar == 3 then
		FMScore_setLine( p, 10, 11, "....\0" );
	end

--	FMScore_setLine( p, 0, 0, string.format("%d\0", cnt ) );

	cnt = cnt + cntMult;
	if cnt > 280 then
		current_page = "page_stat";
	end

end


--------------------------------------------------------------------------------
-- index page
--------------------------------------------------------------------------------
function page_index( p )

	contents_buffer:clear( 13 );

	contents_buffer.LS_str[0] = " INDEX";
	contents_buffer.LS_color[0] = 6;

	contents_buffer.LS_str[2] = "<STATUS";
	contents_buffer.LS_str[4] = "<POS INIT";
	contents_buffer.LS_str[6] = "<VOR CTL";
	contents_buffer.LS_str[8] = "<GPS CTL";
	contents_buffer.LS_str[10] = "<FS CTL";

	contents_buffer.RS_str[2] = "ROUTE MENU>";
	contents_buffer.RS_str[4] = "DATA BASE>";
	contents_buffer.RS_str[6] = "DEFAULTS>";
	contents_buffer.RS_str[8] = "ARR DATA>";

	contents_buffer:draw( p );

	local key = keyProc( p );

	if key == F_KEY.LS_U then
		current_page = "page_stat";
	elseif key == F_KEY.LS_U - 1 then
		current_page = "page_pos";
	end

end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- status page
--------------------------------------------------------------------------------
stat_tbl = {
	cp = 1;
	page_max = 2;
	
	pages = {};
};

	function stat_tbl.pages:P1( p )

		contents_buffer:clear( 13 );

		contents_buffer.LS_str[0] = " STATUS";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", stat_tbl.cp, stat_tbl.page_max );
		contents_buffer.RS_color[0] = 6;

		contents_buffer.LS_str[1] = " NAV DATA";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;
		contents_buffer.LS_str[2] = " INOP";

		contents_buffer.LS_str[3] = " ACTIVE DATABASE";
		contents_buffer.LS_color[3] = 6;
		contents_buffer.LS_smallFont[3] = true;
		contents_buffer.LS_str[4] = " INOP";

		contents_buffer.LS_str[5] = " SEC DATABASE";
		contents_buffer.LS_color[5] = 6;
		contents_buffer.LS_smallFont[5] = true;
		contents_buffer.LS_str[6] = " INOP";

		contents_buffer.LS_str[7] = " UTC";
		contents_buffer.LS_color[7] = 6;
		contents_buffer.LS_smallFont[7] = true;
		contents_buffer.LS_str[8] = " INOP";

		contents_buffer.RS_str[7] = "DATE ";
		contents_buffer.RS_color[7] = 6;
		contents_buffer.RS_smallFont[7] = true;
		contents_buffer.RS_str[8] = " INOP";

		contents_buffer.LS_str[9] = " PROGRAM";
		contents_buffer.LS_color[9] = 6;
		contents_buffer.LS_smallFont[9] = true;
		contents_buffer.LS_str[10] = " INOP";
	end
	----------------------------

	function stat_tbl.pages:P2( p )

		contents_buffer:clear( 13 );

		contents_buffer.LS_str[0] = " STATUS";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", stat_tbl.cp, stat_tbl.page_max );
		contents_buffer.RS_color[0] = 6;

		contents_buffer.LS_str[1] = " MODEL";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;
		contents_buffer.LS_str[2] = " INOP";

		contents_buffer.LS_str[3] = " PERF DATABASE";
		contents_buffer.LS_color[3] = 6;
		contents_buffer.LS_smallFont[3] = true;
		contents_buffer.LS_str[4] = " INOP";

		contents_buffer.RS_str[1] = "VARIANT ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		contents_buffer.RS_str[2] = " INOP";

		contents_buffer.RS_str[3] = "ENGINES ";
		contents_buffer.RS_color[3] = 6;
		contents_buffer.RS_smallFont[3] = true;
		contents_buffer.RS_str[4] = " INOP";

	end
	----------------------------

	function stat_tbl:dispatchKey( p )

		local key = keyProc( p )

		if key ~= 0 then
			stat_tbl.needRedraw = true;

			if key == F_KEY.NEXT then
				if stat_tbl.cp < stat_tbl.page_max then
					stat_tbl.cp = stat_tbl.cp + 1;
				end
			elseif key == F_KEY.PREV then
				if stat_tbl.cp > 1 then
					stat_tbl.cp = stat_tbl.cp - 1;
				end
			elseif key == F_KEY.LS_D then
				current_page = "page_index";
			elseif key == F_KEY.RS_D then
				current_page = "page_pos";
			end
		end
		
	end
	----------------------------

function page_stat( p )
	stat_tbl:dispatchKey( p );

	if stat_tbl.cp == 1 then
		stat_tbl.pages:P1( p );
	else
		stat_tbl.pages:P2( p );
	end


	contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
	contents_buffer.LS_str[ 12 ] = string.format( "<INDEX" );
	contents_buffer.RS_str[ 12 ] = string.format( "POS INIT>" );

	contents_buffer:draw( p );


end
--------------------------------------------------------------------------------


--------------------------------------------------------------------------------
-- pos init page
--------------------------------------------------------------------------------
pos_tbl = {
	cp = 1;
	page_max = 2;

	pages = {};
};

	function pos_tbl.pages:P1( p )

		contents_buffer:clear( 13 );

		contents_buffer.LS_str[0] = " POS INIT";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", pos_tbl.cp, pos_tbl.page_max );
		contents_buffer.RS_color[0] = 6;

		lat, long = FMScore_getFMCpos( p );
		contents_buffer.LS_str[1] = " FMS POS";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;
		contents_buffer.LS_str[2] = string.format( " %s %s\0", latToStr( lat ), longToStr( long ) );

		contents_buffer.LS_str[3] = " AIRPORT";
		contents_buffer.LS_color[3] = 6;
		contents_buffer.LS_smallFont[3] = true;
		contents_buffer.LS_str[4] = " ----";

		contents_buffer.LS_str[5] = " PILOT/REF AIRPORT";
		contents_buffer.LS_color[5] = 6;
		contents_buffer.LS_smallFont[5] = true;
		contents_buffer.LS_str[6] = " ----";

		contents_buffer.RS_str[9] = " SET POS";
		contents_buffer.RS_color[9] = 6;
		contents_buffer.RS_smallFont[9] = true;
		contents_buffer.RS_str[10] = " ----";

		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.RS_str[ 12 ] = string.format( "FPLN>" );
		contents_buffer.LS_str[ 12 ] = string.format( "<INDEX" );

		local key = pos_tbl:dispatchKey( p );

		if key == F_KEY.LS_D then

		else
		end
	end
	----------------------------

	function pos_tbl.pages:P2( p )

		contents_buffer:clear( 13 );
		local key = pos_tbl:dispatchKey( p );

		contents_buffer.LS_str[0] = " POS INIT";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", pos_tbl.cp, pos_tbl.page_max );
		contents_buffer.RS_color[0] = 6;

		lat, long = FMScore_getFMCpos( p );
		contents_buffer.LS_str[1] = " FMS POS";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;
		contents_buffer.LS_str[2] = string.format( " %s %s\0", latToStr( lat ), longToStr( long ) );

		contents_buffer.RS_str[1] = "GS ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		contents_buffer.RS_str[2] = string.format( " %d\0", FMScore_getKGAS( p ) );

		lat, long = FMScore_getFMCpos( p );
		contents_buffer.LS_str[3] = " GPS POS";
		contents_buffer.LS_color[3] = 6;
		contents_buffer.LS_smallFont[3] = true;
		contents_buffer.LS_str[4] = string.format( " %s %s\0", latToStr( lat ), longToStr( long ) );

		contents_buffer.RS_str[3] = "GS ";
		contents_buffer.RS_color[3] = 6;
		contents_buffer.RS_smallFont[3] = true;
		contents_buffer.RS_str[4] = string.format( " %d\0", FMScore_getKGAS( p ) );

		contents_buffer.LS_str[9] = " UPDATE FROM";
		contents_buffer.LS_color[9] = 6;
		contents_buffer.LS_smallFont[9] = true;
		contents_buffer.LS_str[10] = "<NAVAID";

		contents_buffer.RS_str[9] = "NAVAID";
		contents_buffer.RS_color[9] = 6;
		contents_buffer.RS_smallFont[9] = true;
		contents_buffer.RS_str[10] = "----";


		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.RS_str[ 12 ] = string.format( "FPLN>" );
		contents_buffer.LS_str[ 12 ] = string.format( "<INDEX" );

	end
	----------------------------

	function pos_tbl:dispatchKey( p )

		local key = keyProc( p )

		if key ~= 0 then

			if key == F_KEY.NEXT then
				if pos_tbl.cp < pos_tbl.page_max then
					pos_tbl.cp = pos_tbl.cp + 1;
				end
			elseif key == F_KEY.PREV then
				if pos_tbl.cp > 1 then
					pos_tbl.cp = pos_tbl.cp - 1;
				end
			elseif key == F_KEY.RS_D then
				current_page = "page_flt";
			elseif key == F_KEY.LS_D then
				current_page = "page_index";
			end
		end
		return key;
	end
	----------------------------

function page_pos( p )

	if pos_tbl.cp == 1 then
		pos_tbl.pages:P1( p );
	else
		pos_tbl.pages:P2( p );
	end

	contents_buffer:draw( p );

end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- sel page
--------------------------------------------------------------------------------
function page_sel( p, sel_tbl, insertFunc )

	contents_buffer:clear( 13 );
	local key = keyProc( p );

	local i;
	local fixName;
	local lat;
	local long;
	local idxStart = 0;
	local idxEnd = 0;

	local pageMax = math.ceil ( sel_tbl.Size / 5 );

	if sel_tbl.cp > pageMax then
		sel_tbl.cp = pageMax;
	end

	local cp = sel_tbl.cp;

	if sel_tbl.Size > cp * 5 then
		idxEnd = 4;
	else
		idxEnd = sel_tbl.Size - ( cp - 1 ) * 5 - 1;
	end

	contents_buffer.LS_str[0] = " SELECT FIXS";
	contents_buffer.LS_color[0] = 6;
	contents_buffer.RS_str[0] = string.format( "%d/%d\0 ", sel_tbl.cp, pageMax );
	contents_buffer.RS_color[0] = 6;

	for i = 0, idxEnd, 1
	do
		fixName, lat, long = FMScore_getFixInfo( p, sel_tbl.Idx + i + ( ( cp - 1 ) * 5 ) );
		if string.len( fixName ) > 0 then
			contents_buffer.LS_str[i * 2 + 2] = fixName;
			contents_buffer.RS_str[i * 2 + 2] = latToStr( lat );
			contents_buffer.RS_str[i * 2 + 3] = longToStr( long );
		end
	end

	if key == F_KEY.NEXT then
		if sel_tbl.cp < pageMax then
			sel_tbl.cp = sel_tbl.cp + 1;
		end
	elseif key == F_KEY.PREV then
		if sel_tbl.cp > 1 then
			sel_tbl.cp = sel_tbl.cp - 1;
		end
	elseif key <= F_KEY.LS_U and key >= F_KEY.LS_D then
		local keyIdx = F_KEY.LS_U - key;
		if keyIdx <= idxEnd then
			local insertKey = ( cp - 1 ) * 5 + keyIdx + sel_tbl.Idx;
			insertFunc( p, insertKey, sel_tbl.InsertPos );
			sel_tbl.Idx = -1;
		end
	end

	contents_buffer:draw( p );

end
----------------------------

--------------------------------------------------------------------------------
-- legs page
--------------------------------------------------------------------------------

legs_tbl = {
	cp = 1;
	page_max = 1;
	waitExec = false;
	needRedraw = true;
	ls_blank = "-----";
	rs_blank = "---/-----";
	alt_blank = "-----";
	spd_blank = "---";
	
	LINE_MAX = 5;

	NavSel_tbl = {
		Idx = -1;
		Size = -1;
		InsertPos = -1;
		cp = 1;
	};

	pages = {};
	
};
	function legs_tbl.pages:ACT( p )

		contents_buffer:clear( 13 );
		local wpSize = FMScore_getActLegsSize( p );
		legs_tbl:buildLegs( p, wpSize, FMScore_getActLegsInfo, false );

		contents_buffer.LS_str[0] = " ACT LEGS";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", legs_tbl.cp, legs_tbl.page_max );
		contents_buffer.RS_color[0] = 6;
		contents_buffer.RS_str[1] = "SEQUENCE ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		if wpSize > 0 then
			contents_buffer.LS_smallFont[1] = true;
		else
			contents_buffer.LS_str[1] = "\0";
		end

		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.LS_smallFont[ 11 ] = false;
		contents_buffer.LS_color[11] = 6;
		contents_buffer.RS_str[ 12 ] = string.format( "LEG WIND>" );

		local key = legs_tbl:dispatchKey( p );
	end
	----------------------------

	function legs_tbl.pages:MOD( p )

		contents_buffer:clear( 13 );
		local wpSize = FMScore_getModLegsSize( p );
		legs_tbl:buildLegs( p, wpSize, FMScore_getModLegsInfo, false );

		contents_buffer.LS_str[0] = " MOD LEGS";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", legs_tbl.cp, legs_tbl.page_max );
		contents_buffer.RS_color[0] = 6;
		contents_buffer.RS_str[1] = "SEQUENCE ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		if wpSize > 0 then
			contents_buffer.LS_smallFont[1] = true;
		else
			contents_buffer.LS_str[1] = "\0";
		end

		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.LS_smallFont[ 11 ] = false;
		contents_buffer.LS_color[11] = 6;
		contents_buffer.RS_str[ 12 ] = string.format( "LEGS WIND>" );
		contents_buffer.LS_str[ 12 ] = string.format( "<CANCEL MOD" );
		contents_buffer.LS_SYS_MESSAGE = "EXEC LEGS MOD\0";
		contents_buffer.RS_SYS_MESSAGE = "EXEC\0";

		local key = legs_tbl:dispatchKey( p );

		if key == F_KEY.EXEC then
			FMScore_activateLegs( p );
			contents_buffer.LS_SYS_MESSAGE = nil;
			contents_buffer.RS_SYS_MESSAGE = nil;
		elseif key == F_KEY.LS_D then
			FMScore_eraseLegs( p );
			contents_buffer.LS_SYS_MESSAGE = nil;
			contents_buffer.RS_SYS_MESSAGE = nil;
		end

	end
	----------------------------

	function legs_tbl.pages:SEL( p )

		page_sel( p, legs_tbl.NavSel_tbl, FMScore_insertLegs );

	end
	----------------------------

	function legs_tbl:buildLegs( p, wpSize, wpGetFunc, LSfontSize )

		local wpStart = ( legs_tbl.LINE_MAX ) * ( legs_tbl.cp - 1 );
		local name, alt, spd, crs, dist, via

		if wpSize > 1 then
			legs_tbl.page_max = math.ceil( wpSize / ( legs_tbl.LINE_MAX - 1 ) );
		else
			legs_tbl.page_max = 1;
		end

		for i = 0, legs_tbl.LINE_MAX - 1, 1
		do
			local upIdx = i * 2 + 2;
			local dwIdx = i * 2 + 3;
			name, alt, spd, crs, dist, via = wpGetFunc( p, i + wpStart );
			if name ~= "" then
				contents_buffer.LS_str[ upIdx ] = string.format( "%s\0", name );
				contents_buffer.LS_smallFont[ upIdx ] = LSfontSize;
				if i == 0 and legs_tbl.cp == 1 then
					contents_buffer.LS_color[ upIdx ] = 4;
				elseif i == 1 and legs_tbl.cp == 1 then
					contents_buffer.LS_color[ upIdx ] = 5;
				end
			else
				contents_buffer.LS_str[ upIdx ] = string.format( "%s\0", legs_tbl.ls_blank );
			end

			if crs > -1 then
				contents_buffer.LS_str[ dwIdx ] = string.format( " %s`  %dNM\0", hdgToStr( crs ), dist );
			else
				contents_buffer.LS_str[ dwIdx ] = "\0";
			end
			contents_buffer.LS_smallFont[ dwIdx ] = true;

			local altStr = legs_tbl.alt_blank;
			local spdStr = legs_tbl.spd_blank;
			if alt >= 0 then
				if alt >= trantionAlt then
					local talt = alt / 100;
					altStr = string.format( "FL%d", talt );
				else
					altStr = string.format( "%d", alt );
				end
			end
			if spd >= 0 then
				spdStr = string.format( "%d", spd );
			end

			contents_buffer.RS_str[ upIdx ] = string.format( "%s/%s", spdStr, altStr );
			contents_buffer.RS_str[ dwIdx ] = "";
			contents_buffer.RS_color[ upIdx ] = 5;
			contents_buffer.RS_color[ dwIdx ] = 5;

		end

	end
	----------------------------

	function legs_tbl:dispatchKey( p )

		local key = keyProc( p )

		if key ~= 0 then
			legs_tbl.needRedraw = true;

			if key == F_KEY.NEXT then
				if legs_tbl.cp < legs_tbl.page_max then
					legs_tbl.cp = legs_tbl.cp + 1;
				end
			elseif key == F_KEY.PREV then
				if legs_tbl.cp > 1 then
					legs_tbl.cp = legs_tbl.cp - 1;
				end
			elseif key <= F_KEY.LS_U and key >= F_KEY.LS_D - 1 then
				if scratchPad.len > 0 then
					local idx = ( key * -1 ) - 30;
					local wpStart = ( legs_tbl.LINE_MAX ) * ( legs_tbl.cp - 1 );
					local sStr = scratchPad:bufToStr();

					if sStr == "DELETE\0" then
						FMScore_deleteLegs( p, idx + wpStart );
						scratchPad:popAll();
					else
						local firstIdx;
						local size;
						size, firstIdx = FMScore_queryFix( p, sStr );
						if size == 1 then
							FMScore_insertLegs( p, firstIdx, idx + wpStart );
							scratchPad:popAll();
						elseif size > 1 then
							legs_tbl.NavSel_tbl.Idx = firstIdx;
							legs_tbl.NavSel_tbl.Size = size;
							legs_tbl.NavSel_tbl.InsertPos = idx + wpStart;
							scratchPad:popAll();
						end
					end
				end
			elseif key <= F_KEY.RS_U and key >= F_KEY.RS_D - 1 then
				if scratchPad.len > 0 then
					local idx = ( key * -1 ) - 50;
					local wpStart = ( legs_tbl.LINE_MAX ) * ( legs_tbl.cp - 1 );
					local sStr = scratchPad:bufToStr();
					if sStr == "DELETE\0" then
						sStr = -10;
					end
					if FMScore_setLegsAlt( p, sStr, idx + wpStart ) ~= -1 then
						scratchPad:popAll();
					end
				end
			end
			legs_tbl.needRedraw = true;
		end

		return key;

	end
	----------------------------


function page_legs( p )

	local pageName = "ACT"
	if legs_tbl.NavSel_tbl.Idx ~= -1 then
		pageName = "SEL"
	elseif FMScore_isLegsModify( p ) then
		pageName = "MOD"
	end

	if legs_tbl.needRedraw then
		legs_tbl.pages[ pageName ]( legs_tbl, p );
		wait_Counter = 0;
	elseif wait_Counter > 5 then
		legs_tbl.needRedraw = true;
	else
		wait_Counter = wait_Counter + 1;
	end

	contents_buffer:draw( p );
	cnt = cnt + cntMult

end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- flightplan page
--------------------------------------------------------------------------------

flt_tbl = {
	cp = 1;
	page_max = 1;
	waitExec = false;
	needRedraw = true;
	ls_blank = "-----";
	rs_blank = "---/-----";
	alt_blank = "-----";
	spd_blank = "---";
	inputVia = nil;
	
	LINE_MAX = 5;

	NavSel_tbl = {
		Idx = -1;
		Size = -1;
		InsertPos = -1;
		cp = 1;
	};

	pages = {};
	
};

	function flt_tbl.pages:P1( p )

		contents_buffer:clear( 13 );
		local str;

		local getOrgFunc;
		local getDstFunc;
		local getModFunc;

		local wpSize = FMScore_getActFplnSize( p );

		if FMScore_isLegsModify( p ) then
			getOrgFunc = FMScore_getModOrg;
			getDstFunc = FMScore_getModDst;
			getPlnFunc = FMScore_getModFplnInfo;
			contents_buffer.LS_str[0] = " MOD FPLN";
			contents_buffer.LS_SYS_MESSAGE = "EXEC LEGS MOD\0";
			contents_buffer.RS_SYS_MESSAGE = "EXEC\0";
			wpSize = FMScore_getModFplnSize( p );
		else
			getOrgFunc = FMScore_getActOrg;
			getDstFunc = FMScore_getActDst;
			getPlnFunc = FMScore_getActFplnInfo;
			contents_buffer.LS_str[0] = " ACT FPLN";
		end

		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", flt_tbl.cp, flt_tbl.page_max );
		contents_buffer.RS_color[0] = 6;
		contents_buffer.LS_str[1] = " ORIGIN";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;
		contents_buffer.RS_str[1] = "DEST ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;

		str = getOrgFunc( p );
		if str == "" then
			contents_buffer.LS_str[2] = "||||";
		else
			contents_buffer.LS_str[2] = str;
		end

		str = getDstFunc( p );
		if str == "" then
			contents_buffer.RS_str[2] = "||||";
		else
			contents_buffer.RS_str[2] = str;
		end

		contents_buffer.LS_str[3] = " ROUTE";
		contents_buffer.LS_color[3] = 6;
		contents_buffer.LS_smallFont[3] = true;
		contents_buffer.LS_str[4] = "||||";

		contents_buffer.RS_str[3] = "ALTN ";
		contents_buffer.RS_color[3] = 6;
		contents_buffer.RS_smallFont[3] = true;
		contents_buffer.RS_str[4] = "||||";

		contents_buffer.RS_str[5] = "ORIG WAY ";
		contents_buffer.RS_color[5] = 6;
		contents_buffer.RS_smallFont[5] = true;
		contents_buffer.RS_str[6] = "||||";

		contents_buffer.LS_str[7] = " VIA";
		contents_buffer.LS_color[7] = 6;
		contents_buffer.LS_smallFont[7] = true;

		contents_buffer.RS_str[7] = "TO ";
		contents_buffer.RS_color[7] = 6;
		contents_buffer.RS_smallFont[7] = true;
		if wpSize > 0 then
			local name, alt, spd, crs, dist, via
			name, alt, spd, crs, dist, via = getPlnFunc( p, 0 );
			contents_buffer.RS_str[8] = name;
			contents_buffer.LS_str[8] = via;
		else
			contents_buffer.RS_str[8] = "----";
			contents_buffer.LS_str[8] = "----";
		end

		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.LS_color[11] = 6;

		if FMScore_isLegsModify( p ) then
			contents_buffer.LS_str[ 12 ] = string.format( "<CANCEL MOD" );
		else
			contents_buffer.LS_str[ 12 ] = string.format( "<INDEX" );
		end
		contents_buffer.RS_str[ 12 ] = string.format( "PERF INIT>" );

		local key = flt_tbl:dispatchKey( p );

		if key == F_KEY.EXEC then
			FMScore_activateLegs( p );
			contents_buffer.LS_SYS_MESSAGE = nil;
			contents_buffer.RS_SYS_MESSAGE = nil;
		elseif key <= F_KEY.LS_U and key >= F_KEY.LS_D - 1 then
			local sStr = scratchPad:bufToStr();
			if key == F_KEY.LS_U then
				if FMScore_setOrg( p, sStr ) ~= -1 then
					scratchPad:popAll();
				else
					contents_buffer.LS_ERR_MESSAGE = "INVALID ENTRY\0";
				end
			elseif key == F_KEY.LS_D then
				if FMScore_isLegsModify( p ) then
					FMScore_eraseFpln( p );
				else
					current_page = "page_index";
				end
			end
		elseif key <= F_KEY.RS_U and key >= F_KEY.RS_D - 1 then
			local sStr = scratchPad:bufToStr();
			if key == F_KEY.RS_U then
				if FMScore_setDst( p, sStr ) ~= -1 then
					scratchPad:popAll();
				else
					contents_buffer.LS_ERR_MESSAGE = "INVALID ENTRY\0";
				end

			elseif key == F_KEY.RS_U - 3 then
				local firstIdx;
				local size;
				size, firstIdx = FMScore_queryFix( p, sStr );
				if size == 1 then
					if FMScore_insertFpln( p, firstIdx, 0 ) == 0 then
						scratchPad:popAll();
					end
				elseif size > 1 then
					flt_tbl.NavSel_tbl.Idx = firstIdx;
					flt_tbl.NavSel_tbl.Size = size;
					flt_tbl.NavSel_tbl.InsertPos = 0;
					scratchPad:popAll();
				end
			end

		end

	end
	----------------------------

	function flt_tbl.pages:P2( p )

		contents_buffer:clear( 13 );
		local str;

		local getOrgFunc;
		local getDstFunc;
		local getModFunc;

		i = 0;

		local wpSize = FMScore_getActFplnSize( p );

		if FMScore_isLegsModify( p ) then
			getPlnFunc = FMScore_getModFplnInfo;
			contents_buffer.LS_str[0] = " MOD FPLN";
			contents_buffer.LS_SYS_MESSAGE = "EXEC LEGS MOD\0";
			contents_buffer.RS_SYS_MESSAGE = "EXEC\0";
			wpSize = FMScore_getModFplnSize( p );
		else
			getPlnFunc = FMScore_getActFplnInfo;
			contents_buffer.LS_str[0] = " ACT FPLN";
		end

		--HEADER DISPLAY
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", flt_tbl.cp, flt_tbl.page_max );
		contents_buffer.RS_color[0] = 6;

		contents_buffer.LS_str[1] = " VIA";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;

		contents_buffer.RS_str[1] = "TO ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;

		local wpStart = ( flt_tbl.cp - 2 ) * 5 + 1;
		local wpEnd = wpSize - wpStart;

		local lcnt = -1;

		--FPLN WAYPOINT DISPLAY
		for i = 0, wpEnd - 1, 1 do
			local name, alt, spd, crs, dist, via
			name, alt, spd, crs, dist, via = getPlnFunc( p, i + wpStart );
			contents_buffer.RS_str[i * 2 + 2] = name;
			contents_buffer.LS_str[i * 2 + 2] = via;

			lcnt = i
		end

		--VIA INPUT DISPLAY
		if flt_tbl.inputVia ~= nil then
			if lcnt ~= -1 then
				i = lcnt + 1;
			else
				i = 0;
			end

			local via = FMScore_getRteName( p, flt_tbl.inputVia );
			contents_buffer.LS_str[i * 2 + 2] = via;
		end

		--FOOTER DISPLAY
		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.LS_color[11] = 6;
		if FMScore_isLegsModify( p ) then
			contents_buffer.LS_str[ 12 ] = string.format( "<CANCEL MOD" );
		else
			contents_buffer.LS_str[ 12 ] = string.format( "<INDEX" );
		end
		contents_buffer.RS_str[ 12 ] = string.format( "PERF INIT>" );

		--KEY DISPATCH
		local key = flt_tbl:dispatchKey( p );

		if key == F_KEY.EXEC then
			FMScore_activateLegs( p );
			contents_buffer.LS_SYS_MESSAGE = nil;
			contents_buffer.RS_SYS_MESSAGE = nil;
		elseif key <= F_KEY.RS_U and key >= F_KEY.RS_U - wpEnd then
			local sStr = scratchPad:bufToStr();

			local firstIdx;
			local size;
			size, firstIdx = FMScore_queryFix( p, sStr );
			if size == 1 then
				if flt_tbl.inputVia ~= nil then
					if FMScore_insertRteFpln( p, firstIdx, flt_tbl.inputVia, wpStart + ( -key + F_KEY.RS_U ) ) == 0 then
						scratchPad:popAll();
						flt_tbl.inputVia = nil;
					end
				else
					if FMScore_insertFpln( p, firstIdx, wpStart + ( -key + F_KEY.RS_U ) ) == 0 then
						scratchPad:popAll();
					end
				end
			end
		elseif key <= F_KEY.LS_U and key >= F_KEY.LS_U - wpEnd then
			local sStr = scratchPad:bufToStr();

			local viaIdx;
			local preFixIdx = wpStart + ( -key + F_KEY.LS_U ) - 1;
			viaIdx = FMScore_queryRte( p, sStr, preFixIdx );
			if viaIdx >= 0 then
				flt_tbl.inputVia = viaIdx;
				scratchPad:popAll();
			end
		elseif key == F_KEY.LS_D then
			if FMScore_isLegsModify( p ) then
				FMScore_eraseFpln( p );
			else
				current_page = "page_index";
			end
		end

	end
	----------------------------
	function flt_tbl.pages:SEL( p )

		page_sel( p, flt_tbl.NavSel_tbl, FMScore_insertFpln );
	end
	----------------------------

	function flt_tbl:dispatchKey( p )

		local key = keyProc( p )

		if key ~= 0 then

			if key == F_KEY.NEXT then
				if flt_tbl.cp < flt_tbl.page_max then
					flt_tbl.cp = flt_tbl.cp + 1;
				end
			elseif key == F_KEY.PREV then
				if flt_tbl.cp > 1 then
					flt_tbl.cp = flt_tbl.cp - 1;
				end
			elseif key == F_KEY.RS_D then
				current_page = "page_perf";
			end
		end
		return key;
	end
	----------------------------

function page_flt( p )

	local wpSize = FMScore_getActFplnSize( p );
	if FMScore_isLegsModify( p ) then
		wpSize = FMScore_getModFplnSize( p );
	end

	if wpSize > 0 then
		flt_tbl.page_max = ( wpSize - 1 ) / flt_tbl.LINE_MAX + 2;
	else
		flt_tbl.page_max = 1;
	end

	if flt_tbl.NavSel_tbl.Idx ~= -1 then
		flt_tbl.pages:SEL( p );
	elseif flt_tbl.cp == 1 then
		flt_tbl.pages:P1( p );
	else
		flt_tbl.pages:P2( p );
	end

	scratchPad:draw( p );

	contents_buffer:draw( p );

end
--------------------------------------------------------------------------------


--------------------------------------------------------------------------------
-- performance init page
--------------------------------------------------------------------------------
perf_tbl = {

	pages = {};
};

	function perf_tbl.pages:P1( p )

		contents_buffer:clear( 13 );

		contents_buffer.LS_str[0] = " PERF INIT";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", stat_tbl.cp, stat_tbl.page_max );
		contents_buffer.RS_color[0] = 6;

		contents_buffer.LS_str[1] = " BOW";
		contents_buffer.LS_color[1] = 6;
		contents_buffer.LS_smallFont[1] = true;
		contents_buffer.LS_str[2] = " INOP";

		contents_buffer.LS_str[3] = " PASS/WT";
		contents_buffer.LS_color[3] = 6;
		contents_buffer.LS_smallFont[3] = true;
		contents_buffer.LS_str[4] = " INOP";

		contents_buffer.LS_str[5] = " CARGO";
		contents_buffer.LS_color[5] = 6;
		contents_buffer.LS_smallFont[5] = true;
		contents_buffer.LS_str[6] = " INOP";

		contents_buffer.LS_str[7] = " FUEL";
		contents_buffer.LS_color[7] = 6;
		contents_buffer.LS_smallFont[7] = true;
		contents_buffer.LS_str[8] = " INOP";

		contents_buffer.RS_str[7] = "CRZ ALT ";
		contents_buffer.RS_color[7] = 6;
		contents_buffer.RS_smallFont[7] = true;
		contents_buffer.RS_str[8] = " INOP";

		contents_buffer.RS_str[1] = "ALTN CRZ ALT ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		contents_buffer.RS_str[2] = " INOP";

		contents_buffer.RS_str[3] = "ZFW ";
		contents_buffer.RS_color[3] = 6;
		contents_buffer.RS_smallFont[3] = true;
		contents_buffer.RS_str[4] = " INOP";

		contents_buffer.RS_str[5] = "GW ";
		contents_buffer.RS_color[5] = 6;
		contents_buffer.RS_smallFont[5] = true;
		contents_buffer.RS_str[6] = " INOP";

	end
	----------------------------


function page_perf( p )

	perf_tbl.pages:P1( p );

	keyProc( p );
	scratchPad:draw( p );

	contents_buffer:draw( p );

end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- common key procedure
--------------------------------------------------------------------------------

function keyProc( p )

	local key = 0;

	if FMScore_getKeyBufferSize( p ) > 0 then

		local buf = FMScore_popKey( p );

		if buf > 0 then
			scratchPad:push_back( buf );
		elseif buf == F_KEY.DEL then
			if contents_buffer.LS_ERR_MESSAGE ~= nil then
				contents_buffer.LS_ERR_MESSAGE = nil;
			elseif scratchPad.len > 0 then
				if scratchPad:bufToStr() == "DELETE\0" then
					scratchPad:popAll();
				else
					scratchPad:erase_back();
				end
			else
				scratchPad:setDelete();
			end
		elseif buf == F_KEY.LEGS then
			current_page = "page_legs";
		elseif buf == F_KEY.FPLN then
			current_page = "page_flt";
		elseif buf == F_KEY.PERF then
			current_page = "page_perf";
		elseif buf == F_KEY.IDX then
			current_page = "page_index";
		end

		key =  buf;
	end

	return key;

end
--------------------------------------------------------------------------------









---
---
---DEBUG
---
---




--------------------------------------------------------------------------------
-- legsDebug page
--------------------------------------------------------------------------------

legsDebug_tbl = {
	cp = 1;
	page_max = 1;
	waitExec = false;
	needRedraw = true;
	ls_blank = "-----";
	rs_blank = "---/-----";
	alt_blank = "-----";
	spd_blank = "---";
	
	LINE_MAX = 5;

	pages = {};
	
};
	function legsDebug_tbl.pages:ACT( p )

		contents_buffer:clear( 13 );
		local wpSize = FMScore_DEgetActLegsSize( p );
		legsDebug_tbl:buildLegs( p, wpSize, FMScore_DEgetActLegsInfo, false );

		contents_buffer.LS_str[0] = " ACT LEGS";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", legsDebug_tbl.cp, legsDebug_tbl.page_max );
		contents_buffer.RS_color[0] = 6;
		contents_buffer.RS_str[1] = "SEQUENCE ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		if wpSize > 0 then
			contents_buffer.LS_str[1] = string.format( " %s`  %dNM\0", hdgToStr( next_dir ), next_dist );
			contents_buffer.LS_smallFont[1] = true;
		else
			contents_buffer.LS_str[1] = "\0";
		end

		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.RS_str[ 12 ] = string.format( "LEG WIND>" );

		local key = legsDebug_tbl:dispatchKey( p );
	end
	----------------------------

	function legsDebug_tbl.pages:MOD( p )

		contents_buffer:clear( 13 );
		local wpSize = FMScore_getModLegsSize( p );
		legsDebug_tbl:buildLegs( p, wpSize, FMScore_getModLegsInfo, false );

		contents_buffer.LS_str[0] = " MOD LEGS";
		contents_buffer.LS_color[0] = 6;
		contents_buffer.RS_str[0] = string.format( "%d/%d ", legsDebug_tbl.cp, legsDebug_tbl.page_max );
		contents_buffer.RS_color[0] = 6;
		contents_buffer.RS_str[1] = "SEQUENCE ";
		contents_buffer.RS_color[1] = 6;
		contents_buffer.RS_smallFont[1] = true;
		if wpSize > 0 then
			contents_buffer.LS_str[1] = string.format( " %s`  %dNM\0", hdgToStr( next_dir ), next_dist );
			contents_buffer.LS_smallFont[1] = true;
		else
			contents_buffer.LS_str[1] = "\0";
		end

		contents_buffer.LS_str[ 11 ] = string.format( "-----------------------------" );
		contents_buffer.RS_str[ 12 ] = string.format( "LEGS WIND>" );
		contents_buffer.LS_str[ 12 ] = string.format( "<CANCEL MOD" );
		contents_buffer.LS_SYS_MESSAGE = "EXEC LEGS MOD\0";
		contents_buffer.RS_SYS_MESSAGE = "EXEC\0";

		local key = legsDebug_tbl:dispatchKey( p );

		if key == F_KEY.EXEC then
			FMScore_activateLegs( p );
			contents_buffer.LS_SYS_MESSAGE = nil;
			contents_buffer.RS_SYS_MESSAGE = nil;
		elseif key == F_KEY.LS_D then
			FMScore_eraseLegs( p );
			contents_buffer.LS_SYS_MESSAGE = nil;
			contents_buffer.RS_SYS_MESSAGE = nil;
		end

	end
	----------------------------

	function legsDebug_tbl:buildLegs( p, wpSize, wpGetFunc, LSfontSize )

		local wpStart = ( legsDebug_tbl.LINE_MAX - 1 ) * ( legsDebug_tbl.cp - 1 );
		local name, alt, spd, crs, dist

		if wpSize > 1 then
			legsDebug_tbl.page_max = math.ceil( wpSize / ( legsDebug_tbl.LINE_MAX - 1 ) );
		else
			legsDebug_tbl.page_max = 1;
		end

		for i = 0, legsDebug_tbl.LINE_MAX - 1, 1
		do
			local upIdx = i * 2 + 2;
			local dwIdx = i * 2 + 3;
			name, alt, spd, crs, dist = wpGetFunc( p, i + wpStart );
			if name ~= "" then
				contents_buffer.LS_str[ upIdx ] = string.format( "%s\0", name );
				contents_buffer.LS_smallFont[ upIdx ] = LSfontSize;
			else
				contents_buffer.LS_str[ upIdx ] = string.format( "%s\0", legsDebug_tbl.ls_blank );
			end

			if crs > -1 then
				contents_buffer.LS_str[ dwIdx ] = string.format( " %s`  %dNM\0", hdgToStr( crs ), dist );
			else
				contents_buffer.LS_str[ dwIdx ] = "\0";
			end
			contents_buffer.LS_smallFont[ dwIdx ] = true;

			local altStr = legsDebug_tbl.alt_blank;
			local spdStr = legsDebug_tbl.spd_blank;
			if alt >= 0 then
				if alt >= trantionAlt then
					local talt = alt / 100;
					altStr = string.format( "FL%d", talt );
				else
					altStr = string.format( "%d", alt );
				end
			end
			if spd >= 0 then
				spdStr = string.format( "%d", spd );
			end

			contents_buffer.RS_str[ upIdx ] = string.format( "%s/%s", spdStr, altStr );
			contents_buffer.RS_str[ dwIdx ] = "";
			contents_buffer.RS_color[ upIdx ] = 5;
			contents_buffer.RS_color[ dwIdx ] = 5;

		end

	end
	----------------------------

	function legsDebug_tbl:dispatchKey( p )

		local key = keyProc( p )

		if key ~= 0 then
			legsDebug_tbl.needRedraw = true;

			if key == F_KEY.NEXT then
				if legsDebug_tbl.cp < legsDebug_tbl.page_max then
					legsDebug_tbl.cp = legsDebug_tbl.cp + 1;
				end
			elseif key == F_KEY.PREV then
				if legsDebug_tbl.cp > 1 then
					legsDebug_tbl.cp = legsDebug_tbl.cp - 1;
				end
			elseif key <= F_KEY.LS_U and key >= F_KEY.LS_D - 1 then
				if scratchPad.len > 0 then
					local idx = ( key * -1 ) - 30;
					local wpStart = ( legsDebug_tbl.LINE_MAX - 1 ) * ( legsDebug_tbl.cp - 1 );
					local sStr = scratchPad:bufToStr();
					if sStr == "DELETE\0" then
						FMScore_deleteLegs( p, idx + wpStart );
						scratchPad:popAll();
					elseif FMScore_insertLegs( p, sStr , idx + wpStart ) ~= -1 then
						scratchPad:popAll();
					end
				end
			elseif key <= F_KEY.RS_U and key >= F_KEY.RS_D - 1 then
				if scratchPad.len > 0 then
					local idx = ( key * -1 ) - 50;
					local wpStart = ( legsDebug_tbl.LINE_MAX - 1 ) * ( legsDebug_tbl.cp - 1 );
					local sStr = scratchPad:bufToStr();
					if sStr == "DELETE\0" then
						sStr = -10;
					end
					if FMScore_setLegsAlt( p, sStr, idx + wpStart ) ~= -1 then
						scratchPad:popAll();
					end
				end
			end
			legsDebug_tbl.needRedraw = true;
		end

		return key;

	end
	----------------------------


function page_legsDebug( p )

	local pageName = "ACT"
	if FMScore_isLegsModify( p ) then
		pageName = "MOD"
	end

	if legsDebug_tbl.needRedraw then
		legsDebug_tbl.pages[ pageName ]( legsDebug_tbl, p );
		wait_Counter = 0;
	elseif wait_Counter > 5 then
		legsDebug_tbl.needRedraw = true;
	else
		wait_Counter = wait_Counter + 1;
	end

	contents_buffer:draw( p );
	cnt = cnt + cntMult

end
--------------------------------------------------------------------------------

