--[[
*****************************************************************************************
* Program Script Name	:	B738.trim
* Author Name			:	Jim Gregory
*
*   Revisions:
*   -- DATE --	--- REV NO ---		--- DESCRIPTION ---
*   2016-04-26	0.01a				Start of Dev
*
*
*
*
*****************************************************************************************
*        COPYRIGHT � 2016 JIM GREGORY / LAMINAR RESEARCH - ALL RIGHTS RESERVED
*****************************************************************************************
--]]


--*************************************************************************************--
--** 					              XLUA GLOBALS              				     **--
--*************************************************************************************--

--[[

SIM_PERIOD - this contains the duration of the current frame in seconds (so it is alway a
fraction).  Use this to normalize rates,  e.g. to add 3 units of fuel per second in a
per-frame callback you’d do fuel = fuel + 3 * SIM_PERIOD.

IN_REPLAY - evaluates to 0 if replay is off, 1 if replay mode is on

--]]


--*************************************************************************************--
--** 					               CONSTANTS                    				 **--
--*************************************************************************************--

--- Arms are in meters -------
DEFAULT_ARM = 16.8783		-- 664.5 in
MAIN_TANK_ARM = 16.9164		-- 666.0 in
CENTER_TANK_ARM = 15.3924	-- 606.0 in
------------------------------
FMS_SCREEN_LAG = 0.21

FMC_MAX_WPT = 256

raw_table('vnav_vvi')
vnav_vvi = 
	{
	[2000] = 1500, [3000] = 1700, [4000] = 1900, [5000] = 2200, [6000] = 2600, [7000] = 2800, [8000] = 2700, [9000] = 2700, [10000] = 2800,
	[11000] = 2800, [12000] = 2900, [13000] = 3000, [14000] = 3050, [15000] = 3050, [16000] = 3050, [17000] = 2950, [18000] = 2850, [19000] = 2750, [20000] = 2650,
	[21000] = 2550, [22000] = 2500, [23000] = 2400, [24000] = 2200, [25000] = 2150, [26000] = 2050, [27000] = 2750, [28000] = 2450, [29000] = 2200, [30000] = 2050,
	[31000] = 1850, [32000] = 1700, [33000] = 1600, [34000] = 1450, [35000] = 1350, [36000] = 1200, [37000] = 1000, [38000] =  850, [39000] =  650, [40000] =  500,
	[41000] = 350, [42000] = 330
	}

raw_table('vnav_vvi1')
vnav_vvi1 = 
	{
	[2000] = 3500, [3000] = 1700, [4000] = 3500, [5000] = 3600, [6000] = 3900, [7000] = 4600, [8000] = 4750, [9000] = 4700, [10000] = 4600,
	[11000] = 3000, [12000] = 3750, [13000] = 3850, [14000] = 3950, [15000] = 3850, [16000] = 3700, [17000] = 3600, [18000] = 3450, [19000] = 3300, [20000] = 3200,
	[21000] = 3050, [22000] = 2900, [23000] = 2800, [24000] = 2650, [25000] = 2500, [26000] = 2350, [27000] = 2250, [28000] = 2150, [29000] = 2450, [30000] = 2550,
	[31000] = 2550, [32000] = 2500, [33000] = 2400, [34000] = 2350, [35000] = 2250, [36000] = 2150, [37000] = 1950, [38000] = 1850, [39000] = 1700, [40000] = 1600,
	[41000] = 1500, [42000] = 1400
	}

raw_table('vnav_vvi2')
vnav_vvi2 = 
	{
	[2000] = 2500, [3000] = 1450, [4000] = 3200, [5000] = 3400, [6000] = 3400, [7000] = 3350, [8000] = 3250, [9000] = 3150, [10000] = 3050,
	[11000] = 2000, [12000] = 2700, [13000] = 2750, [14000] = 2650, [15000] = 2600, [16000] = 2500, [17000] = 2400, [18000] = 2300, [19000] = 2200, [20000] = 2100,
	[21000] = 2000, [22000] = 1900, [23000] = 1800, [24000] = 1700, [25000] = 1600, [26000] = 1500, [27000] = 1400, [28000] = 1300, [29000] = 1200, [30000] = 1100,
	[31000] = 1400, [32000] = 1400, [33000] = 1350, [34000] = 1250, [35000] = 1200, [36000] = 1100, [37000] = 1000, [38000] =  950, [39000] =  850, [40000] =  700,
	[41000] = 550, [42000] = 400
	}

raw_table('month_table')
month_table =
	{	 [1] = { [1] = "JAN", [2] = "01"},
		 [2] = { [1] = "FEB", [2] = "02"},
		 [3] = { [1] = "MAR", [2] = "03"},
		 [4] = { [1] = "APR", [2] = "04"},
		 [5] = { [1] = "MAY", [2] = "05"},
		 [6] = { [1] = "JUN", [2] = "06"},
		 [7] = { [1] = "JUL", [2] = "07"},
		 [8] = { [1] = "AUG", [2] = "08"},
		 [9] = { [1] = "SEP", [2] = "09"},
		[10] = { [1] = "OCT", [2] = "10"},
		[11] = { [1] = "NOV", [2] = "11"},
		[12] = { [1] = "DEC", [2] = "12"}
	}


DES_SPEED_COEF = 0.128

-- 37 idx
raw_table('vnav_des_alt')
vnav_des_alt = { 0, 2000, 4000, 6000, 8000, 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000,
				 26000, 27000, 28000, 29000, 30000, 31000, 32000, 33000, 34000, 35000, 36000, 37000, 38000, 39000, 41000, 42000}

raw_table('vnav_des_dist_diff_100x')
vnav_des_dist_diff_100x = { 12.7576, 12.7576, 12.7576, 12.8263, 12.8439, 12.8313, 6.4007, 6.3857, 6.3802, 6.3593, 6.3401, 6.3173, 6.2990, 6.2780, 6.2559, 6.2386,
							6.2051, 6.1899, 6.1725, 6.1461, 6.1299, 6.1104, 6.0911, 6.0819, 6.0640, 6.0488,
							6.0449, 6.0307, 6.0217, 6.0195, 6.0203, 6.0128, 6.2759, 6.2807, 6.2807, 6.2807, 6.2807}
							
raw_table('vnav_des_dist_diff_150x')
vnav_des_dist_diff_150x = { 10.1251, 10.1251, 10.1251, 10.1795, 10.1935, 10.1835, 5.0799, 5.0680, 5.0636, 5.0470, 5.0318, 5.0138, 4.9992, 4.9825, 4.9650, 4.9513,
							4.9247, 4.9126, 4.8988, 4.8778, 4.8650, 4.8495, 4.8342, 4.8269, 4.8127, 4.8006,
							4.7976, 4.7863, 4.7791, 4.7773, 4.7780, 4.7721, 4.9809, 4.9847, 4.9847, 4.9847, 4.9847}
							
raw_table('vnav_des_dist_diff_200x')
vnav_des_dist_diff_200x = { 7.9102, 7.9102, 7.9102, 7.9528, 7.9637, 7.9559, 3.9687, 3.9594, 3.9560, 3.9430, 3.9311, 3.9170, 3.9056, 3.8926, 3.8789, 3.8682,
							3.8474, 3.8380, 3.8272, 3.8108, 3.8008, 3.7887, 3.7767, 3.7710, 3.7599, 3.7505,
							3.7481, 3.7393, 3.7337, 3.7323, 3.7328, 3.7282, 3.8913, 3.8943, 3.8943, 3.8943, 3.8943}
							
raw_table('vnav_des_dist_diff_250x')
vnav_des_dist_diff_250x = { 6.0613, 6.0613, 6.0613, 6.1340, 6.2022, 6.2522, 3.1430, 3.1552, 3.1622, 3.1684, 3.1785, 3.1868, 3.1945, 3.2060, 3.2081, 3.2126,
							3.2225, 3.2334, 3.2401, 3.2448, 3.2512, 3.2669, 3.2719, 3.2774, 3.2952, 3.3071,
							3.3228, 3.3383, 3.3590, 3.3771, 3.3965, 3.4113, 3.6514, 3.6839, 3.6839, 3.6839, 3.6839}
							
raw_table('vnav_des_dist_diff_290x')
vnav_des_dist_diff_290x = { 4.5968, 4.5968, 4.5968, 4.6764, 4.7460, 4.8151, 2.4306, 2.4521, 2.4664, 2.4795, 2.4970, 2.5186, 2.5306, 2.5504, 2.5696, 2.5821,
							2.6048, 2.6276, 2.6489, 2.6629, 2.6912, 2.7048, 2.7322, 2.7592, 2.7801, 2.8059,
							2.8346, 2.8670, 2.8880, 2.8880, 2.8880, 2.8880, 2.8880, 2.8880, 2.8880, 2.8880, 2.8880}
							
raw_table('vnav_des_dist_diff_350x')
vnav_des_dist_diff_350x = { 3.7607, 3.7607, 3.7607, 3.9014, 4.0360, 4.1644, 2.1263, 2.1008, 2.0767, 2.1050, 2.1349, 2.1699, 2.0547, 2.0696, 2.0945, 2.1232,
							2.1513, 2.1768, 2.2022, 2.2345, 2.2582, 2.2888, 2.3174, 2.3445, 2.3744, 2.4051,
							2.4425, 2.4685, 2.5084, 2.5440, 2.5777, 2.6070, 2.8166, 2.8676, 2.8676, 2.8676, 2.8676}
							
raw_table('vnav_des_dist_diff_400x')
vnav_des_dist_diff_400x = { 2.6494, 2.6494, 2.6494, 2.6638, 2.8124, 2.7260, 1.3624, 1.3964, 1.4268, 1.4606, 1.4915, 1.5234, 1.5580, 1.5893, 1.6179, 1.6526,
							1.6845, 1.7167, 1.7527, 1.7813, 1.7878, 1.8755, 1.8835, 1.9139, 1.9509, 1.9860,
							2.0258, 2.0611, 2.1033, 2.1423, 2.1801, 2.2217, 2.4070, 2.4637, 2.4637, 2.4637, 2.4637}
							
raw_table('vnav_des_dist_diff_107x')
vnav_des_dist_diff_107x = { 2.2400, 2.2400, 2.2400, 2.2400, 2.2400, 2.2400, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200,
							1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1200, 1.1300, 1.1500, 1.1800, 1.2200,
							1.2700, 1.3300, 1.4000, 1.4800, 1.5625, 1.7096, 1.7559, 1.7817, 1.7817, 1.7817, 1.7817}
							
raw_table('vnav_des_dist_diff_082x')
vnav_des_dist_diff_082x = { 3.0896, 3.0896, 3.0896, 3.0896, 3.0896, 3.0896, 1.5448, 1.5488, 1.5488, 1.5488, 1.5488, 1.5488, 1.5488, 1.5488, 1.5488, 1.5488,
							1.5448, 1.5448, 1.5448, 1.5448, 1.5448, 1.6004, 1.6205, 1.6895, 1.7649, 1.8374,
							1.8973, 1.9767, 2.0444, 2.1117, 2.1763, 2.3080, 2.5069, 2.5761, 2.6377, 2.6377, 2.6377}
							
raw_table('vnav_des_dist_diff_079x')
vnav_des_dist_diff_079x = { 2.9726, 2.9726, 2.9726, 2.9726, 2.9726, 2.9726, 1.4863, 1.4863, 1.4863, 1.4863, 1.4863, 1.4863, 1.4863, 1.4863, 1.4863, 1.4863,
							1.4863, 1.4863, 1.4863, 1.5535, 1.6224, 1.6936, 1.7457, 1.8168, 1.8973, 1.9630,
							2.0412, 2.1073, 2.1729, 2.2392, 2.3137, 2.4234, 2.6393, 2.6831, 2.6797, 2.6797, 2.6797}
							
raw_table('vnav_des_dist_diff_076x')
vnav_des_dist_diff_076x = { 2.9294, 2.9294, 2.9294, 2.9294, 2.9294, 2.9294, 1.4647, 1.4647, 1.4647, 1.4647, 1.4647, 1.4647, 1.4647, 1.4647, 1.4647, 1.4647,
							1.4647, 1.5394, 1.5993, 1.6684, 1.7483, 1.8188, 1.8793, 1.9617, 2.0270, 2.1062,
							2.1708, 2.2424, 2.3072, 2.3790, 2.4380, 2.5420, 2.7598, 2.8170, 2.8706, 2.8706, 2.8706}
							
raw_table('vnav_des_dist_diff_073x')
vnav_des_dist_diff_073x = { 2.8912, 2.8912, 2.8912, 2.8912, 2.8912, 2.8912, 1.4456, 1.4456, 1.4456, 1.4456, 1.4456, 1.4456, 1.4456, 1.4456, 1.4456, 1.4456,
							1.5101, 1.5797, 1.6544, 1.7188, 1.7933, 1.8711, 1.9396, 2.0123, 2.0890, 2.1612,
							2.2240, 2.2991, 2.3697, 2.4313, 2.4947, 2.5401, 2.6472, 2.8623, 2.9476, 2.9476, 2.9476}
							
raw_table('vnav_des_dist_diff_070x')
vnav_des_dist_diff_070x = { 2.9164, 2.9164, 2.9164, 2.9164, 2.9164, 2.9164, 1.4582, 1.4582, 1.4582, 1.4582, 1.4582, 1.4582, 1.4582, 1.5329, 1.5896, 1.6684,
							1.7361, 1.8131, 1.8843, 1.9655, 2.0414, 2.1204, 2.1901, 2.2553, 2.3325, 2.3938,
							2.4620, 2.5250, 2.5841, 2.6432, 2.6892, 2.7747, 2.9599, 2.9950, 3.0308, 3.0308, 3.0308}

raw_table('vnav_des_dist_diff_066x')
vnav_des_dist_diff_066x = { 2.6758, 2.6758, 2.6758, 2.6758, 2.6758, 2.6758, 1.3379, 1.3379, 1.3379, 1.3379, 1.3289, 1.3858, 1.4473, 1.5084, 1.5792, 1.6513,
							1.7198, 1.8011, 1.8781, 1.9619, 2.0556, 2.1639, 2.2136, 2.3002, 2.3873, 2.4815,
							2.5712, 2.6660, 2.7574, 2.8564, 2.9370, 3.0316, 3.2697, 3.3396, 3.3995, 6.5926, 3.5163}

raw_table('vnav_des_dist_diff_062x')
vnav_des_dist_diff_062x = { 2.6052, 2.6052, 2.6052, 2.6052, 2.6052, 2.6052, 1.3026, 1.3484, 1.4050, 1.4666, 1.5235, 1.6087, 1.6729, 1.7431, 1.8257, 1.9022,
							1.9895, 2.0747, 2.1595, 2.2512, 2.3436, 2.4442, 2.5202, 2.6112, 2.7049, 2.8025,
							2.8846, 2.9823, 3.0587, 3.1516, 3.2056, 3.2641, 3.4859, 3.5375, 3.5577, 7.0952, 3.5568}

raw_table('vnav_des_dist_diff_058x')
vnav_des_dist_diff_058x = { 2.6559, 2.6559, 2.6559, 2.6559, 2.6559, 2.7769, 1.4805, 1.5506, 1.6135, 1.6879, 1.7593, 1.8393, 1.9206, 2.0024, 2.0958, 2.1853,
							2.2749, 2.3661, 2.4580, 2.5529, 2.6471, 2.7612, 2.8239, 2.9163, 3.0102, 3.0979,
							3.1824, 3.2514, 3.3240, 3.3563, 3.4105, 3.4405, 3.6117, 3.6246, 3.6080, 7.0595, 3.5467}

raw_table('vnav_des_dist_diff_054x')
vnav_des_dist_diff_054x = { 2.6208, 2.6208, 2.6208, 2.7441, 2.5846, 3.6659, 1.7353, 1.8161, 1.8982, 1.9779, 2.0651, 2.1564, 2.2487, 2.3440, 2.4423, 2.5356,
							2.6269, 2.7292, 2.8203, 2.9107, 3.0070, 3.1076, 3.1858, 3.2734, 3.3412, 3.4139,
							3.4719, 3.4931, 3.5119, 3.5333, 3.5443, 3.5452, 3.6501, 3.6113, 3.5381, 6.7806, 3.3045}

raw_table('vnav_des_dist_diff_050x')
vnav_des_dist_diff_050x = { 3.0646, 3.0646, 3.0646, 3.3410, 3.6405, 3.9692, 2.1173, 2.2082, 2.3063, 2.4060, 2.5048, 2.6011, 2.7071, 2.8030, 2.9056, 2.9961,
							3.0903, 3.1837, 3.2751, 3.4816, 3.5140, 3.5338, 3.5550, 3.5967, 3.6254, 3.6391,
							3.6521, 3.6446, 3.6287, 3.6062, 3.5531, 3.4899, 3.4952, 3.3960, 3.5836, 3.5836, 3.5836}

raw_table('vnav_des_dist_diff_045x')
vnav_des_dist_diff_045x = { 3.9437, 3.9437, 3.9437, 4.3019, 4.6846, 5.0998, 2.7056, 2.8164, 2.9177, 3.0274, 3.1277, 3.2303, 3.3204, 3.4073, 3.4995, 3.5821,
							3.6533, 3.7220, 3.7669, 3.8001, 3.8190, 3.8304, 3.8014, 3.7996, 3.7593, 3.7187,
							3.6451, 3.5483, 3.4375, 3.4375, 3.4375, 3.4375, 3.4375, 3.4375, 3.4375, 3.4375, 3.4375}

raw_table('vnav_des_dist_diff_040x')
vnav_des_dist_diff_040x = { 4.9475, 4.9475, 4.9475, 5.3863, 5.8358, 6.2783, 3.3015, 3.4035, 3.5025, 3.5916, 3.6718, 3.7539, 3.8234, 3.8819, 3.9260, 3.9567,
							3.9709, 3.9684, 3.9477, 3.9155, 3.6365, 3.8875, 3.7284, 3.5951, 3.5350, 3.5350,
							3.5350, 3.5350, 3.5350, 3.5350, 3.5350, 3.5350, 3.5350, 3.5350, 3.5350, 3.5350, 3.5350}

raw_table('vnav_des_dist_diff_100xf1')
vnav_des_dist_diff_100xf1 = { 6.5623, 6.5623, 6.5623, 6.5158, 6.4594, 6.3927, 3.1704, 3.1534, 3.1337, 3.1123, 3.0944, 3.0753, 3.0550, 3.0388, 3.0171, 2.9961,
							2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961,
							2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961, 2.9961}
							

raw_table('vnav_des_dist_diff_155xf1')
vnav_des_dist_diff_155xf1 = { 5.1268, 5.1268, 5.1268, 5.0905, 5.0464, 4.9943, 2.4769, 2.4636, 2.4482, 2.4315, 2.4175, 2.4026, 2.3867, 2.3741, 2.3571, 2.3407,
							2.3047, 2.3047, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407,
							2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407, 2.3407}
							

raw_table('vnav_des_dist_diff_220xf1')
vnav_des_dist_diff_220xf1 = { 3.4716, 3.4716, 3.4716, 3.4757, 3.4789, 3.4760, 1.7379, 1.7336, 1.7337, 1.7355, 1.7361, 1.7294, 1.7346, 1.7300, 1.7322, 1.7298,
							1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298,
							1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298, 1.7298}
							

raw_table('vnav_des_dist_diff_100xf5')
vnav_des_dist_diff_100xf5 = { 5.1409, 5.1409, 5.1409, 5.1110, 5.0783, 5.0333, 2.5030, 2.4873, 2.4790, 2.4681, 2.4544, 2.4383, 2.4305, 2.4169, 2.4070, 2.3964,
							2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964,
							2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964, 2.3964}
							

raw_table('vnav_des_dist_diff_155xf5')
vnav_des_dist_diff_155xf5 = { 4.0163, 4.0163, 4.0163, 3.9930, 3.9674, 3.9323, 1.9555, 1.9432, 1.9367, 1.9282, 1.9175, 1.9049, 1.8988, 1.8882, 1.8805, 1.8722,
							1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722,
							1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722, 1.8722}
							

raw_table('vnav_des_dist_diff_220xf5')
vnav_des_dist_diff_220xf5 = { 2.4860, 2.4860, 2.4860, 2.4881, 2.4944, 2.4954, 1.2486, 1.2449, 1.2476, 1.2519, 1.2457, 1.2505, 1.2500, 1.2499, 1.2504, 1.2504,
							1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504,
							1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504, 1.2504}
							

raw_table('vnav_des_dist_diff_150xf10')
vnav_des_dist_diff_150xf10 = { 3.8263, 3.8263, 3.8263, 3.8090, 3.7812, 3.7494, 1.8629, 1.8550, 1.8492, 1.8349, 1.8298, 1.8206, 1.8084, 1.8045, 1.7911, 1.7829,
							1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829,
							1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829, 1.7829}
							

raw_table('vnav_des_dist_diff_200xf10')
vnav_des_dist_diff_200xf10 = { 2.7085, 2.7085, 2.7085, 2.7012, 2.7028, 2.6953, 1.3461, 1.3435, 1.3431, 1.3426, 1.3385, 1.3379, 1.3393, 1.3344, 1.3368, 1.3338,
							1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338,
							1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338, 1.3338}
							

ci_clb_tab_n = 10
raw_table('ci_clb_tab')
ci_clb_tab = {0, 20, 40, 60, 80, 100, 120, 140, 160, 180}
raw_table('ci_clb_ias1')
ci_clb_ias1 = {252, 261, 270, 279, 288, 297, 306, 315, 324, 335}
raw_table('ci_clb_ias2')
ci_clb_ias2 = {303, 312, 321, 330, 340, 340, 340, 340, 340, 340}
raw_table('ci_clb_gw1')
ci_clb_gw1 = {70, 70, 70, 70, 70, 70, 70, 70, 70, 70}
raw_table('ci_clb_gw2')
ci_clb_gw2 = {180, 180, 180, 180, 172, 152, 133, 113, 94, 70}

ci_crs_tab_n = 8
raw_table('ci_crs_tab')
ci_crs_tab = {0, 20, 40, 60, 80, 100, 150, 200}
raw_table('ci_crs_41_m1')
ci_crs_41_m1 = {0.655, 0.748, 0.782, 0.797, 0.804, 0.805, 0.805, 0.805}
raw_table('ci_crs_41_m2')
ci_crs_41_m2 = {0.740, 0.776, 0.790, 0.796, 0.802, 0.805, 0.805, 0.805}
raw_table('ci_crs_41_m3')
ci_crs_41_m3 = {0.760, 0.782, 0.791, 0.794, 0.798, 0.802, 0.805, 0.805}
raw_table('ci_crs_41_m4')
ci_crs_41_m4 = {0.772, 0.784, 0.790, 0.792, 0.794, 0.796, 0.805, 0.805}
raw_table('ci_crs_41_m5')
ci_crs_41_m5 = {0.772, 0.778, 0.782, 0.784, 0.786, 0.788, 0.792, 0.796}
raw_table('ci_crs_41_gw1')
ci_crs_41_gw1 = {80, 80, 80, 80, 80, 80, 80, 80}
raw_table('ci_crs_41_gw2')
ci_crs_41_gw2 = {105, 105, 105, 105, 105, 105, 105, 105}
raw_table('ci_crs_41_gw3')
ci_crs_41_gw3 = {115, 115, 115, 115, 115, 115, 115, 115}
raw_table('ci_crs_41_gw4')
ci_crs_41_gw4 = {125, 125, 125, 125, 125, 125, 120, 125}
raw_table('ci_crs_41_gw5')
ci_crs_41_gw5 = {140, 140, 140, 140, 140, 140, 140, 140}

raw_table('ci_crs_35_m1')
ci_crs_35_m1 = {0.580, 0.688, 0.754, 0.782, 0.795, 0.803, 0.808, 0.808}
raw_table('ci_crs_35_m2')
ci_crs_35_m2 = {0.718, 0.762, 0.782, 0.791, 0.798, 0.802, 0.808, 0.808}
raw_table('ci_crs_35_m3')
ci_crs_35_m3 = {0.756, 0.778, 0.787, 0.792, 0.794, 0.799, 0.806, 0.808}
raw_table('ci_crs_35_m4')
ci_crs_35_m4 = {0.792, 0.780, 0.786, 0.790, 0.790, 0.792, 0.796, 0.800}
raw_table('ci_crs_35_m5')
ci_crs_35_m5 = {0.792, 0.778, 0.784, 0.786, 0.787, 0.788, 0.794, 0.796}
raw_table('ci_crs_35_gw1')
ci_crs_35_gw1 = {80, 80, 80, 80, 80, 80, 80, 80}
raw_table('ci_crs_35_gw2')
ci_crs_35_gw2 = {130, 130, 130, 130, 130, 130, 140, 130}
raw_table('ci_crs_35_gw3')
ci_crs_35_gw3 = {150, 150, 150, 150, 150, 150, 150, 155}
raw_table('ci_crs_35_gw4')
ci_crs_35_gw4 = {170, 170, 170, 170, 170, 170, 170, 170}
raw_table('ci_crs_35_gw5')
ci_crs_35_gw5 = {180, 180, 180, 180, 180, 180, 180, 180}

raw_table('ci_crs_29_m1')
ci_crs_29_m1 = {0.510, 0.615, 0.690, 0.745, 0.775, 0.790, 0.805, 0.805}
raw_table('ci_crs_29_m2')
ci_crs_29_m2 = {0.585, 0.660, 0.725, 0.760, 0.785, 0.790, 0.805, 0.805}
raw_table('ci_crs_29_m3')
ci_crs_29_m3 = {0.630, 0.695, 0.745, 0.770, 0.785, 0.790, 0.800, 0.805}
raw_table('ci_crs_29_m4')
ci_crs_29_m4 = {0.675, 0.725, 0.755, 0.780, 0.785, 0.790, 0.798, 0.805}
raw_table('ci_crs_29_m5')
ci_crs_29_m5 = {0.730, 0.760, 0.775, 0.785, 0.786, 0.790, 0.796, 0.805}
raw_table('ci_crs_29_gw1')
ci_crs_29_gw1 = {80, 80, 80, 80, 80, 80, 80, 80}
raw_table('ci_crs_29_gw2')
ci_crs_29_gw2 = {110, 110, 110, 110, 110, 110, 110, 110}
raw_table('ci_crs_29_gw3')
ci_crs_29_gw3 = {130, 130, 130, 130, 130, 130, 130, 130}
raw_table('ci_crs_29_gw4')
ci_crs_29_gw4 = {150, 150, 150, 150, 150, 150, 150, 150}
raw_table('ci_crs_29_gw5')
ci_crs_29_gw5 = {180, 180, 180, 180, 180, 180, 180, 180}

raw_table('ci_crs_20_m1')
ci_crs_20_m1 = {0.420, 0.530, 0.600, 0.650, 0.700, 0.725, 0.725, 0.725}
raw_table('ci_crs_20_m2')
ci_crs_20_m2 = {0.495, 0.575, 0.635, 0.680, 0.725, 0.725, 0.725, 0.725}
raw_table('ci_crs_20_m3')
ci_crs_20_m3 = {0.535, 0.595, 0.655, 0.695, 0.725, 0.725, 0.725, 0.725}
raw_table('ci_crs_20_m4')
ci_crs_20_m4 = {0.590, 0.620, 0.675, 0.710, 0.725, 0.725, 0.725, 0.725}
raw_table('ci_crs_20_m5')
ci_crs_20_m5 = {0.610, 0.650, 0.705, 0.725, 0.725, 0.725, 0.725, 0.725}
raw_table('ci_crs_20_gw1')
ci_crs_20_gw1 = {80, 80, 80, 80, 80, 80, 80, 80}
raw_table('ci_crs_20_gw2')
ci_crs_20_gw2 = {110, 110, 110, 110, 115, 110, 110, 110}
raw_table('ci_crs_20_gw3')
ci_crs_20_gw3 = {130, 130, 130, 130, 130, 130, 130, 130}
raw_table('ci_crs_20_gw4')
ci_crs_20_gw4 = {150, 150, 150, 150, 150, 150, 150, 150}
raw_table('ci_crs_20_gw5')
ci_crs_20_gw5 = {180, 180, 180, 165, 165, 180, 180, 180}
---*****
raw_table('ci_crs_10_m1')
ci_crs_10_m1 = {0.350, 0.435, 0.510, 0.560, 0.600, 0.600, 0.600, 0.600}
raw_table('ci_crs_10_m2')
ci_crs_10_m2 = {0.405, 0.475, 0.540, 0.585, 0.600, 0.600, 0.600, 0.600}
raw_table('ci_crs_10_m3')
ci_crs_10_m3 = {0.440, 0.510, 0.560, 0.600, 0.600, 0.600, 0.600, 0.600}
raw_table('ci_crs_10_m4')
ci_crs_10_m4 = {0.470, 0.530, 0.575, 0.600, 0.600, 0.600, 0.600, 0.600}
raw_table('ci_crs_10_m5')
ci_crs_10_m5 = {0.515, 0.560, 0.600, 0.600, 0.600, 0.600, 0.600, 0.600}
raw_table('ci_crs_10_gw1')
ci_crs_10_gw1 = {80, 80, 80, 80, 80, 80, 80, 80}
raw_table('ci_crs_10_gw2')
ci_crs_10_gw2 = {110, 110, 110, 110, 110, 110, 110, 110}
raw_table('ci_crs_10_gw3')
ci_crs_10_gw3 = {130, 130, 130, 130, 130, 130, 130, 130}
raw_table('ci_crs_10_gw4')
ci_crs_10_gw4 = {150, 150, 150, 150, 150, 150, 150, 150}
raw_table('ci_crs_10_gw5')
ci_crs_10_gw5 = {180, 180, 180, 180, 180, 180, 180, 180}

PLANE_NAME = ""
FILE_NAME_HW_CFG = ""
FILE_NAME_CFG = ""
FILE_NAME_STATUS = ""
FILE_NAME_APT_DAT = ""
FILE_NAME_RNW_DAT = ""
FILE_NAME_GATE_DAT = ""
FILE_NAME_CUST_DAT = ""
FILE_NAME_FMOD_CFG = ""
FILE_NAME_ACF_CFG = ""
FILE_NAME_FMOD = ""
ACF_TYPE = 0
FILE_NAME_STATE = ""
PLANE_NAME_2 = 67

MAX_NUM_SCRATCH = 24
MAX_LEGS_DATA = 54	--44

-- MESSAGES --
INVALID_INPUT 				= "INVALID ENTRY"
INVALID_DELETE 				= ">INVALID DELETE"
ENTER_IRS_POS 				= "ENTER IRS POSITION"
ENTER_IRS_HDG 				= "ENTER IRS HEADING"
USING_RSV_FUEL 				= "USING RSV FUEL"		--if the estimate is less than fuel reserve
INSUFICIENT_FUEL 			= "INSUFFICIENT FUEL" --if predicted fuel at destin will be 2000 lb or less
RESET_MCP_ALT 				= "RESET MCP ALT"	-- during VNAV before 5 NM of TOD without selecting lower alt
VERIFY_TO_SPEEDS 			= "VERIFY TAKEOFF SPEEDS"
IRS_MOTION 					= "IRS MOTION"	-- if IRS restart align
GPS_L_INVALID 				= "GPS-L INVALID"	-- if GPS-L fail
GPS_R_INVALID 				= "GPS-R INVALID"	-- if GPS-R fail
GPS_LR_INVALID 				= "GPS-L-R INVALID"	-- if GPS-L-R fail
TO_SPEEDS_DELETED 			= "TAKEOFF SPEEDS DELETED"
DRAG_REQUIRED 				= "DRAG REQUIRED"	-- if airspeed > 10 kts above FMC target
NOT_IN_DATABASE 			= "NOT IN DATABASE"
NAV_DATA_OF_DATE 			= "NAV DATA OUT OF DATE"
UNABLE_CRUISE_ALT 			= "UNABLE CRZ ALT"
CONFIG_SAVED 				= "CONFIG SAVED"
PRESET_SAVED				= "PRESET SAVED"
PRESET_LOADED				= "PRESET LOADED"
CHECK_ALT_TGT 				= "CHECK ALT TGT"
TAI_ON_ABOVE_10C 			= "TAI ON ABOVE 10`C"	-- TAI operated above temp 10 C
DISCON 						= "DISCONTINUITY"
ABOVE_MAX_CERT_ALT 			= "ABOVE MAX CERT ALT"
APPRCH_VREF_NOT_SELECTED 	= "APPRCH VREF NOT SELECTED"
LNAV_DISCON 				= "LNAV DISCONNECT"
END_OF_ROUTE 				= "END OF ROUTE"
UNABLE_NEXT_ALTITUDE 		= "UNABLE NEXT ALTITUDE"
ALT_CONSTRAINT 				= "ALT CONSTRAINT "
UNABLE_REQ_NAV_PERF			= "UNABLE REQD NAV PERF-RNP"
NOT_INTCPT_HDG				= "NOT ON INTERCEPT HEADING"
MAX_ALT_FLXXX				= "MAX ALT FL"
STEEP_DESC					= "STEEP DESCENT AFTER "
V_SPEEDS_UNAV				= "V SPEEDS UNAVAILABLE"
CORRUPTED_NAVDATA			= "CORRUPTED NAVDATA"
ACARS_VOICE					= "ACARS VOICE"
REF_ICAO_MSG				= ""
RWY_NA_FOR_SID				= "RUNWAY N/A FOR SID"
HOLD_AT						= "HOLD AT "
UNABLE_OFFSET				= "UNABLE TO OFFSET"
OFFSET_DELETED				= "OFFSET DELETED"
NO_OFFSET_AT				= "NO OFFSET AT LEG "
RW_APP_TUNE					= "RW/APP TUNE DISAGREE"
RW_APP_CRS					= "RW/APP CRS ERROR"

KGS_LBS = 2.204622622

--*************************************************************************************--
--** 					            GLOBAL VARIABLES                				 **--
--*************************************************************************************--

plane_already_loaded = 0
rec_mode = 0
rec_mode_sample = 0
file_rec = 0

rte_header_str = "RTE  "
rte_option_str = "RTE  "

rte_header_str_fo = "RTE  "
rte_option_str_fo = "RTE  "

rte12_copy_done = 0
rte21_copy_done = 0
rte12_act = -1
rte12_act_old = -1
rte12_sel = 0
rte12_sel_fo = 0

hold_data_num4 = 0
raw_table('hold_data4')
hold_data4 = {}
act_page_buf = 0
max_page_buf = 0
legs_num2_buf = 0
raw_table('legs_data2_buf')
legs_data2_buf = {}
fpln_num_buf = 0
raw_table('fpln_data_buf')
fpln_data_buf = {}
hold_data_num_buf = 0
raw_table('hold_data_buf')
hold_data_buf = {}

ref_icao7 = "----"
ref_icao8 = "----"
ref_icao7_x = "----"
des_icao7 = "****"
des_icao7_x = "****"
co_route7 = "------------"
co_route7_x = "------------"
ref_rwy7 = "----"
ref_rwy8 = "----"
ref_rwy9 = "----"
des_rwy7 = "----"

ref_tns_alt7_x = 0
ref_tns_lvl7_x = 0
ref_icao_alt7_x = 0
trans_alt_mod2 = "-----"
ref_gate7_x = "-----"
des_tns_alt7_x = 0
des_tns_lvl7_x = 0
des_icao_alt7_x = 0
trans_lvl_mod2 = "-----"
ref_sid8 = "------"
ref_sid_tns8 = "------"
des_star8 = "------"
des_star_trans8 = "------"
des_app8 = "------"
des_app_tns8 = "------"
des_star8_old = des_star8
des_star_trans8_old = des_star_trans8
des_app8_old = des_app8
des_app_tns8_old = des_app_tns8
ref_icao_lat7_x = 0
ref_icao_lon7_x = 0
des_icao_lat7_x = 0
des_icao_lon7_x = 0
des_app_rw_only7 = 0
rw_ext_dist7 = "--.-"
rw_ext_fpa7 = "-.--"
des_app_rw_only7_x = 0
rw_ext_dist7_x = "--.-"
rw_ext_fpa7_x = "-.--"

ref_exec2 = 0
des_exec2 = 0
rte_exec2 = 0
exec_activate_fpln2 = 0

-- legs_intdir2 = 0
-- legs_intdir_idx_mod2 = 0
-- legs_intdir_crs_mod2 = 0
arr_data7_x = 0
des_app_from_apt7 = 0

last_sid_idx_mod2 = 0
first_star_idx_mod2 = 0
first_app_idx_mod2 = 0
-- id_ed_mod = ""
idx_ed_mod = 0
idx_ed_mod2 = 0
-- idx_iaf_mod = 0
rnav_idx_first_mod = 0
rnav_idx_first_mod2 = 0
-- rnav_idx_last_mod = 0
rnav_idx_last_mod2 = 0

first_miss_app_idx_mod2 = 0
-- last_miss_app_idx_mod2 = 0
missed_app_via2 = ""
missed_app_distance2 = 0

ref_icao_lat7 = 0
ref_icao_lon7 = 0
ref_tns_alt7 = 0
ref_tns_lvl7 = 0
ref_icao_alt7 = 0
trans_alt7 = "-----"

ref_gate7 = "-----"
co_route7 = "------------"

ref_sid7 = "------"
ref_sid_tns7 = "------"
des_star7 = "------"
des_star_trans7 = "------"
des_app7 = "------"
des_app_tns7 = "------"

des_app_from_apt7 = 0


des_rnw7 = "----"
des_rnw8 = "----"
des_icao_lat7 = 0
des_icao_lon7 = 0
des_tns_alt7 = 0
des_tns_lvl7 = 0
des_icao_alt7 = 0
trans_lvl7 = "-----"

des_app7 = des_app8
des_app_tns7 = des_app_tns8
des_star7 = des_star8
des_star_trans7 = des_star_trans8

arr_data7 = 0

sel_ref_rwy8 = "-----"
sel_ref_sid8 = "------"
sel_ref_sid_tns8 = "------"

sel_des_star8 = "------"
sel_des_star_trans8 = "------"
sel_des_app8 = "------"
sel_des_app_tns8 = "------"


first_sid_idx_mod2 = 0
last_sid_idx_mod2 = 0
first_app_idx_mod2 = 0
first_star_idx_mod2 = 0
iaf_app_idx_mod = 0

offset_mod_x8 = 0
last_offset8 = 0
legs_num8mm = 0
raw_table('legs_data8mm')
legs_data8mm = {}
legs_num8m = 0
raw_table('legs_data8m')
legs_data8m = {}
offset_mod2 = 0



first_sid_idx_mod_buf = 0
last_sid_idx_mod_buf = 0
first_star_idx_mod_buf = 0
first_app_idx_mod_buf = 0
idx_ed_mod_buf = 0
rnav_alt_mod_buf = 0
rnav_vpa_mod_buf = 0
rnav_idx_first_mod_buf = 0
rnav_idx_last_mod_buf = 0

version = ""
fmod_version = "NOT FOUND"
file_navdata = 0
file_dump = 0

fmc_enable = 0
reset_fmc = 0
reverse_rte1 = 0
reverse_rte2 = 0
save_rte1 = 0
save_rte2 = 0
co_route_save1 = "************"
co_route_save2 = "************"

clr_repeat = 0
clr_repeat_time = 0

line0_l = ""
line1_l = ""
line2_l = ""
line3_l = ""
line4_l = ""
line5_l = ""
line6_l = ""
line0_s = ""
line1_s = ""
line2_s = ""
line3_s = ""
line4_s = ""
line5_s = ""
line6_s = ""
line1_x = ""
line2_x = ""
line3_x = ""
line4_x = ""
line5_x = ""
line6_x = ""
line0_inv = ""
line1_inv = ""
line2_inv = ""
line3_inv = ""
line4_inv = ""
line5_inv = ""
line6_inv = ""
line0_m = ""
line1_m = ""
line2_m = ""
line3_m = ""
line4_m = ""
line5_m = ""
line6_m = ""
line0_g = ""
line1_g = ""
line2_g = ""
line3_g = ""
line4_g = ""
line5_g = ""
line6_g = ""
line0_c = ""
line4_si = ""
line1_lx = ""
line6_lx = ""

max_page = 0
act_page = 0
act_page_old = 0

act_page2 = 0
max_page2 = 0
act_page_old2 = 0

page_menu = 0
page_ident = 0
page_init = 0
page_takeoff = 0
display_update = 1
page_approach = 0
page_perf = 0
page_n1_limit = 0
page_pos_init = 0
page_route = 0
page_dep_arr = 0
page_dep = 0
page_arr = 0
page_descent = 0
page_descent_forecast = 0
page_legs = 0
page_rte_init = 0
page_climb = 0
page_cruise = 0
page_progress = 0
page_hold = 0
page_sel_wpt = 0
page_sel_wpt2 = 0
page_sel_wpt3 = 0
page_sel_wpt4 = 0
page_sel_hold_wpt = 0
fix_idx1 = 0
fix_idx2 = 0
page_offset = 0
page_legs_step = 0
page_fix = 0
page_rte_legs = 0
page_ac = 0
page_ac_atis = 0
page_ac_msg = 0
page_nearest_apt = 0
page_altern_apt = 0
page_nav_status = 0
page_atc = 0
page_fmc_comm = 0
page_dl_menu = 0
page_dl_acars = 0
page_dl_requests = 0
page_dl_wxr_req = 0
page_dl_wxr_info = 0
page_dl_fltlog = 0
page_dl_sensors = 0
page_eo_climb = 0
page_eo_cruise = 0

page_menu2 = 0
page_ident2 = 0
page_init2 = 0
page_takeoff2 = 0
page_approach2 = 0
page_perf2 = 0
page_n1_limit2 = 0
page_pos_init2 = 0
page_route2 = 0
page_dep_arr2 = 0
page_dep2 = 0
page_arr2 = 0
page_descent2 = 0
page_descent_forecast2 = 0
page_legs2 = 0
page_rte_init2 = 0
page_climb2 = 0
page_cruise2 = 0
page_progress2 = 0
page_hold2 = 0
page_sel_wpt_2 = 0
page_sel_wpt2_2 = 0
page_sel_wpt3_2 = 0
page_sel_wpt4_2 = 0
page_sel_hold_wpt_2 = 0
page_offset2 = 0
page_legs_step2 = 0
page_ref_nav_data2 = 0
page_ref_nav_data_wpt2 = 0
page_ref_nav_data_navaid2 = 0
page_ref_nav_data_apt2 = 0
page_ref_sel2 = 0
page_fix2 = 0
page_rte_legs2 = 0
page_ac2 = 0
page_ac_atis2 = 0
page_ac_msg2 = 0
page_nearest_apt2 = 0
page_altern_apt2 = 0
page_nav_status2 = 0
page_atc2 = 0
page_fmc_comm2 = 0
page_dl_menu2 = 0
page_dl_acars2 = 0
page_dl_requests2 = 0
page_dl_wxr_req2 = 0
page_dl_wxr_info2 = 0
page_dl_fltlog2 = 0
page_dl_sensors2 = 0
page_eo_climb2 = 0
page_eo_cruise2 = 0

legs_step = 0
legs_step2 = 0
map_mode = 2
map_mode_old = 2
map_mode_old2 = 2
map_mode2 = 2

-- DATALINK status:
local datalink_status = 0
raw_table('datalink_status_txt')
local datalink_status_txt = {true, true, true, true, true}
datalink_status_txt[1] = "    NO COMM"
datalink_status_txt[2] = "      READY"
datalink_status_txt[3] = "      VOICE"
datalink_status_txt[4] = "       FAIL"
datalink_status_txt[5] = "NO ATC COMM"

dl_ref = "----"
dl_des = "****"
dl_flt_num = "--------"
dl_day = 0
dl_out_hrs = 0
dl_out_min = 0
dl_out_fuel = -1
dl_in_hrs = 0
dl_in_min = 0
dl_in_fuel = -1
dl_on_hrs = 0
dl_on_min = 0
dl_on_fuel = -1
dl_off_hrs = 0
dl_off_min = 0
dl_off_fuel = -1
dl_flight_time = -1
dl_block_time = -1
dl_out_of_gate = 0

raw_table('dl_flights_ref')
local dl_flights_ref = {true, true, true, true}
raw_table('dl_flights_des')
local dl_flights_des = {true, true, true, true}
raw_table('dl_flights_flt_num')
local dl_flights_flt_num = {true, true, true, true}
raw_table('dl_flights_day')
local dl_flights_day = {true, true, true, true}
raw_table('dl_flights_out_hrs')
local dl_flights_out_hrs = {true, true, true, true}
raw_table('dl_flights_out_min')
local dl_flights_out_min = {true, true, true, true}
raw_table('dl_flights_out_fuel')
local dl_flights_out_fuel = {true, true, true, true}
raw_table('dl_flights_in_hrs')
local dl_flights_in_hrs = {true, true, true, true}
raw_table('dl_flights_in_min')
local dl_flights_in_min = {true, true, true, true}
raw_table('dl_flights_in_fuel')
local dl_flights_in_fuel = {true, true, true, true}
raw_table('dl_flights_on_hrs')
local dl_flights_on_hrs = {true, true, true, true}
raw_table('dl_flights_on_min')
local dl_flights_on_min = {true, true, true, true}
raw_table('dl_flights_on_fuel')
local dl_flights_on_fuel = {true, true, true, true}
raw_table('dl_flights_off_hrs')
local dl_flights_off_hrs = {true, true, true, true}
raw_table('dl_flights_off_min')
local dl_flights_off_min = {true, true, true, true}
raw_table('dl_flights_off_fuel')
local dl_flights_off_fuel = {true, true, true, true}
raw_table('dl_flights_flight_time')
local dl_flights_flight_time = {true, true, true, true}
raw_table('dl_flights_block_time')
local dl_flights_block_time = {true, true, true, true}
-- for iii = 1, 4 do
	-- dl_flights_ref[iii] = "----"
	-- dl_flights_des[iii] = "****"
	-- dl_flights_flt_num[iii] = "--------"
	-- dl_flights_day[iii] = 0
	-- dl_flights_out_hrs[iii] = 0
	-- dl_flights_out_min[iii] = 0
	-- dl_flights_out_fuel[iii] = -1
	-- dl_flights_in_hrs[iii] = 0
	-- dl_flights_in_min[iii] = 0
	-- dl_flights_in_fuel[iii] = -1
	-- dl_flights_on_hrs[iii] = 0
	-- dl_flights_on_min[iii] = 0
	-- dl_flights_on_fuel[iii] = -1
	-- dl_flights_off_hrs[iii] = 0
	-- dl_flights_off_min[iii] = 0
	-- dl_flights_off_fuel[iii] = -1
	-- dl_flights_flight_time[iii] = -1
	-- dl_flights_block_time[iii] = -1
-- end

entry = ""
entry2 = ""

entry_wind_dir = ""
entry_wind_spd = ""

v1 = "---"
vr = "---"
v2 = "---"
flaps = "**"
vref_15 = "   "
vref_30 = "   "
vref_40 = "   "
flaps_app = "  "

gw_act = "    "
v1_set = "---"
vr_set = "---"
v2_set = "---"
irs_pos_set = "*****.*******.*"
irs2_pos_set = "*****.*******.*"
gps_right = "-----.-------.-"
gps_left = "-----.-------.-"
last_pos = "-----.-------.-"
fmc_pos = "-----.-------.-"
last_pos_enable = 1

ref_icao = "----"
ref_gate = "-----"
des_icao = "****"
co_route = "------------"
flt_num = "--------"
flt_num_x = "--------"
ref_rwy = "-----"
des_rwy = "----"
ref_icao2 = "----"

vrnp_num = 400
vrnp = ""
app_rnp = ""
app_rnp_num = 0.30

gw = "***.*"
gw_calc = "***.*"
gw_lbs = "***.*"
gw_kgs = "***.*"
gw_app = "***.*"
gw_app_lbs = "***.*"
gw_app_kgs = "***.*"
gw_app_nul = 0

zfw_calc = "---.-"
zfw_calc_lbs = "---.-"
zfw_calc_kgs = "---.-"

init_fuel = 0

fuel_weight = "--.-"
fuel_weight_lbs = "--.-"
fuel_weight_kgs = "--.-"

plan_weight = "---.-"
plan_weight_kgs = "---.-"
plan_weight_lbs = "---.-"

plan_weight_mod = "---.-"
plan_weight_kgs_mod = "---.-"
plan_weight_lbs_mod = "---.-"

zfw = "***.*"
zfw_kgs = "***.*"
zfw_lbs = "***.*"

gw_mod = "***.*"
gw_lbs_mod = "***.*"
gw_kgs_mod = "***.*"
zfw_mod = "***.*"
zfw_kgs_mod = "***.*"
zfw_lbs_mod = "***.*"

reserves = "**.*"
reserves_kgs = "**.*"
reserves_lbs = "**.*"

cost_index = "***"

reserves_mod = "**.*"
reserves_kgs_mod = "**.*"
reserves_lbs_mod = "**.*"
cost_index_mod = "***"

econ_clb_spd = 0
econ_clb_spd_mach = 0
econ_crz_spd = 0
econ_crz_spd_mach = 0
econ_des_spd = 0
econ_des_spd_mach = 0
econ_des_vpa = 0.0
lrc_crz_spd = 280
lrc_crz_spd_mach = 0.760

crz_alt = "*****"
crz_alt_num = 0
crz_alt_num2 = 0
crz_spd = "---"
crz_spd_mach = ".---"
crz_alt_old = "*****"
crz_spd_old = "---"
crz_spd_mach_old = ".---"
clb_alt = "----"
cut_alt = "----"

clb_alt_num = 1500
cut_alt_num = 800
accel_alt_num = 1000
eo_accel_alt_num = 1000

accel_alt = "----"
eo_accel_alt = "----"

crz_wind_dir = "---"
crz_wind_spd = "---"
rw_wind_dir = "---"
rw_wind_spd = "---"
rw_slope = "--.-"
rw_hdg = "---"
trans_alt = "-----"
trans_alt_mod = "-----"
trans_lvl = "-----"
isa_dev_f = "----"
isa_dev_c = "---"
tc_oat_f = "----"
tc_oat_c = "---"
forec_alt_1 = "-----"
forec_alt_1_num = 0
forec_dir_1 = "---"
forec_spd_1 = "---"
forec_alt_2 = "-----"
forec_alt_2_num = 0
forec_dir_2 = "---"
forec_spd_2 = "---"
forec_alt_3 = "-----"
forec_alt_3_num = 0
forec_dir_3 = "---"
forec_spd_3 = "---"
cabin_rate = "---"
forec_isa_dev = "---"
forec_qnh = "------"

trans_lvl_mod = "-----"
forec_alt_1_mod = "-----"
forec_alt_1_num_mod = 0
forec_dir_1_mod = "---"
forec_spd_1_mod = "---"
forec_alt_2_mod = "-----"
forec_alt_2_num_mod = 0
forec_dir_2_mod = "---"
forec_spd_2_mod = "---"
forec_alt_3_mod = "-----"
forec_alt_3_num_mod = 0
forec_dir_3_mod = "---"
forec_spd_3_mod = "---"
cabin_rate_mod = "---"
forec_isa_dev_mod = "---"
forec_qnh_mod = "------"

forecast_exec = 0
descent_exec = 0
descent_r_alt1_mod = 0
descent_r_speed1_mod = 0
fmc_descent_r_alt1_mod = ""

climb_exec = 0
climb_r_alt1_mod = 0
climb_r_speed1_mod = 0
fmc_climb_r_alt1_mod = ""

to = "<ACT>"
to_1 = "     "
to_2 = "     "
clb = "<SEL>"
clb_1 = "     "
clb_2 = "     "
sel_clb_thr = 0
rw_cond = 0
cg = "--.-"
trim = "    "
time_err = "  "
units = 0
units_recalc = 0
weight_min = 90
weight_max = 180

clb_min_kts = "   "
clb_min_mach = "   "
clb_max_kts = "   "
clb_max_mach = "   "
crz_min_kts = "   "
crz_min_mach = "   "
crz_max_kts = "   "
crz_max_mach = "   "
des_min_kts = "   "
des_min_mach = "   "
des_max_kts = "   "
des_max_mach = "   "


latitude_deg = ""
latitude_min = ""
longitude_deg = ""
longitude_min = ""
irs_hdg = "---`"
irs_pos = "*****.*******.*"
msg_irs_pos = 0
msg_irs_hdg = 0
zulu_time = "             "
ground_air = 0
fmc_gs = ""
irs_gs = ""
irs2_gs = ""
oat_sim = "    "
oat = "****"
sel_temp = "----"
sel_temp_f = "----"
oat_f = "****"
oat_sim_f = "    "
oat_unit = "`C"
oat_n1_calc = 0
oat_old = ""
oat_f_old = ""

wind_corr = "--"
app_flap = "--"
app_spd = "---"

qrh = "OFF"

raw_table('fmc_message')
fmc_message = {}
raw_table('fmc_message_warn')
fmc_message_warn = {}
fmc_message_num = 0
raw_table('fmc_message2')
fmc_message2 = {}
raw_table('fmc_message_warn2')
fmc_message_warn2 = {}
fmc_message_num2 = 0

msg_mcp_alt = 0
msg_gps_l_fail = 0
msg_gps_r_fail = 0
msg_gps_lr_fail = 0
msg_irs_motion = 0
msg_drag_req = 0
msg_nav_data = 0
msg_unavaible_crz_alt = 0
msg_next_alt = 0
msg_chk_alt_tgt = 0
msg_tai_above_10 = 0
msg_above_max = 0
msg_vref_not_sel = 0
msg_chk_alt_constr = 0
msg_rnp_alert = 0
rnp_alert = 0
msg_using_rsv_fuel = 0
msg_using_ins_fuel = 0
msg_com3_data_alert = 0
msg_ref_icao_alert = 0

auto_act = "<ACT>"
ga_act = "     "
con_act = "     "
clb_act = "     "
crz_act = "     "
flight_phase_old2 = -1
fms_auto_n1_active = 1

tai_on_alt = "-----"
tai_off_alt = "-----"
tai_on_alt_num = 0
tai_off_alt_num = 0

tai_on_alt_mod = "-----"
tai_off_alt_mod = "-----"
tai_on_alt_num_mod = 0
tai_off_alt_num_mod = 0

eng_out_prompt = 0

was_on_air = 0
takeoff_enable = 0
climb_enable = 1
descent_enable = 0
goaround_enable = 0
fmc_climb_mode = 0
fmc_cruise_mode = 0
fmc_cont_mode = 0
fmc_takeoff_mode = 0
fmc_goaround_mode = 0
fms_N1_mode = 0
fms_N1_to_mode_sel = 0
fms_N1_clb_mode_sel = 0
in_flight_mode = 0

disable_POS_2L = 0
disable_POS_3L = 0
disable_POS_4R = 0
disable_POS_5R = 0
disable_PERF_3R = 0
disable_PERF_4R = 0
disable_N1_6L = 0
disable_N1_6R = 0


fmc_full_thrust = 0.984
fmc_dto_thrust = 0.984
fmc_sel_thrust = 0.984
fmc_clb_thrust = 0.984
fmc_crz_thrust = 0.984
fmc_con_thrust = 0.984
fmc_ga_thrust = 0.984
fmc_auto_thrust = 0.984

fmc_full_thrust2 = 0.984
fmc_dto_thrust2 = 0.984
fmc_sel_thrust2 = 0.984
fmc_clb_thrust2 = 0.984
fmc_crz_thrust2 = 0.984
fmc_con_thrust2 = 0.984
fmc_ga_thrust2 = 0.984
fmc_auto_thrust2 = 0.984

fmc_full_thrust_ind = 0.984
fmc_full_thrust_ind2 = 0984
fmc_dto_thrust_ind = 0.984
fmc_dto_thrust_ind2 = 0.984
fmc_ga_thrust_ind = 0.984
fmc_ga_thrust_ind2 = 0.984

next_enable = 1
prev_enable = 1
exec1_light = 0

des_now_enable = 0
drag_timeout = 0

file_name = ""
file_name_path = ""
file_name_path_nav = ""
file_name_path_fix = ""
file_name_path_awy = ""

raw_table('ref_data_rwy')
ref_data_rwy = {}
ref_rwy_num = 0
raw_table('ref_data_sid')
ref_data_sid = {}
sid_num = 0
raw_table('ref_data_sid_tns')
ref_data_sid_tns = {}
sid_tns_num = 0
raw_table('ref_data_star')
ref_data_star = {}
star_num = 0
raw_table('ref_data_star_tns')
ref_data_star_tns = {}
ref_data_star_tns_n = 0
raw_table('ref_data_app')
ref_data_app = {}
ref_data_app_n = 0
raw_table('ref_data_app_tns')
ref_data_app_tns = {}
ref_data_app_tns_n = 0
raw_table('ref_ed_app')
ref_ed_app = {}
ref_ed_app_n = 0

raw_table('des_data_rwy')
des_data_rwy = {}
des_rwy_num = 0
raw_table('data_des_star')
data_des_star = {}
data_des_star_n = 0
raw_table('data_des_star_tns')
data_des_star_tns = {}
data_des_star_tns_n = 0
raw_table('data_des_app')
data_des_app = {}
data_des_app_n = 0
raw_table('data_des_app_tns')
data_des_app_tns = {}
data_des_app_tns_n = 0
-- don't used data
ed_app_num = 0
raw_table('ed_app')
ed_app = {}

raw_table('rte_sid')
rte_sid = {}
rte_sid_num = 0
raw_table('rte_star')
rte_star = {}
rte_star_num = 0
raw_table('rte_app')
rte_app = {}
rte_app_num = 0

--RTE1
raw_table('ref_data_rwy_1')
ref_data_rwy_1 = {}
ref_rwy_num_1 = 0
raw_table('ref_data_sid_1')
ref_data_sid_1 = {}
sid_num_1 = 0
raw_table('ref_data_sid_tns_1')
ref_data_sid_tns_1 = {}
sid_tns_num_1 = 0
raw_table('ref_data_star_1')
ref_data_star_1 = {}
star_num_1 = 0
raw_table('ref_data_star_tns_1')
ref_data_star_tns_1 = {}
ref_data_star_tns_n_1 = 0
raw_table('ref_data_app_1')
ref_data_app_1 = {}
ref_data_app_n_1 = 0
raw_table('ref_data_app_tns_1')
ref_data_app_tns_1 = {}
ref_data_app_tns_n_1 = 0
raw_table('ref_ed_app_1')
ref_ed_app_1 = {}
ref_ed_app_n_1 = 0
raw_table('rte_sid_1')
rte_sid_1 = {}
rte_sid_num_1 = 0

raw_table('des_data_rwy_1')
des_data_rwy_1 = {}
des_rwy_num_1 = 0
raw_table('data_des_star_1')
data_des_star_1 = {}
data_des_star_n_1 = 0
raw_table('data_des_star_tns_1')
data_des_star_tns_1 = {}
data_des_star_tns_n_1 = 0
raw_table('data_des_app_1')
data_des_app_1 = {}
data_des_app_n_1 = 0
raw_table('data_des_app_tns_1')
data_des_app_tns_1 = {}
data_des_app_tns_n_1 = 0
-- don't used data
ed_app_num_1 = 0
raw_table('ed_app_1')
ed_app_1 = {}
raw_table('rte_star_1')
rte_star_1 = {}
rte_star_num_1 = 0
raw_table('rte_app_1')
rte_app_1 = {}
rte_app_num_1 = 0

--RTE2
raw_table('ref_data_rwy_2')
ref_data_rwy_2 = {}
ref_rwy_num_2 = 0
raw_table('ref_data_sid_2')
ref_data_sid_2 = {}
sid_num_2 = 0
raw_table('ref_data_sid_tns_2')
ref_data_sid_tns_2 = {}
sid_tns_num_2 = 0
raw_table('ref_data_star_2')
ref_data_star_2 = {}
star_num_2 = 0
raw_table('ref_data_star_tns_2')
ref_data_star_tns_2 = {}
ref_data_star_tns_n_2 = 0
raw_table('ref_data_app_2')
ref_data_app_2 = {}
ref_data_app_n_2 = 0
raw_table('ref_data_app_tns_2')
ref_data_app_tns_2 = {}
ref_data_app_tns_n_2 = 0
raw_table('ref_ed_app_2')
ref_ed_app_2 = {}
ref_ed_app_n_2 = 0
raw_table('rte_sid_2')
rte_sid_2 = {}
rte_sid_num_2 = 0

raw_table('des_data_rwy_2')
des_data_rwy_2 = {}
des_rwy_num_2 = 0
raw_table('data_des_star_2')
data_des_star_2 = {}
data_des_star_n_2 = 0
raw_table('data_des_star_tns_2')
data_des_star_tns_2 = {}
data_des_star_tns_n_2 = 0
raw_table('data_des_app_2')
data_des_app_2 = {}
data_des_app_n_2 = 0
raw_table('data_des_app_tns_2')
data_des_app_tns_2 = {}
data_des_app_tns_n_2 = 0
-- don't used data
ed_app_num_2 = 0
raw_table('ed_app_2')
ed_app_2 = {}
raw_table('rte_star_2')
rte_star_2 = {}
rte_star_num_2 = 0
raw_table('rte_app_2')
rte_app_2 = {}
rte_app_num_2 = 0


ref_sid = "------"
ref_sid_tns = "------"
des_star = "------"
des_star_trans = "------"
des_app = "------"
des_app_tns = "------"

ref_data_icao = "----"
des_data_icao = "****"

ref_rwy_exec = 0
ref_sid_exec = 0
ref_tns_exec = 0
ref_app_tns_exec = 0
des_star_exec = 0
des_star_tns_exec = 0
des_app_exec = 0
des_app_tns_exec = 0

raw_table('fpln_data2')
fpln_data2 = {}
fpln_num2 = 0

raw_table('legs_data')
legs_data = {}
legs_num = 0

raw_table('ref_rwy_sel')
local ref_rwy_sel = {true, true, true, true, true}
-- ref_rwy_sel[1] = ""
-- ref_rwy_sel[2] = ""
-- ref_rwy_sel[3] = ""
-- ref_rwy_sel[4] = ""
-- ref_rwy_sel[5] = ""

raw_table('ref_sid_sel')
local ref_sid_sel = {true, true, true, true, true}
-- ref_sid_sel[1] = ""
-- ref_sid_sel[2] = ""
-- ref_sid_sel[3] = ""
-- ref_sid_sel[4] = ""
-- ref_sid_sel[5] = ""

raw_table('ref_tns_sel')
local ref_tns_sel = {true, true, true, true, true}
-- ref_tns_sel[1] = ""
-- ref_tns_sel[2] = ""
-- ref_tns_sel[3] = ""
-- ref_tns_sel[4] = ""
-- ref_tns_sel[5] = ""

raw_table('des_star_sel')
local des_star_sel = {true, true, true, true, true}
-- des_star_sel[1] = ""
-- des_star_sel[2] = ""
-- des_star_sel[3] = ""
-- des_star_sel[4] = ""
-- des_star_sel[5] = ""

raw_table('des_star_tns_sel')
local des_star_tns_sel = {true, true, true, true, true}
-- des_star_tns_sel[1] = ""
-- des_star_tns_sel[2] = ""
-- des_star_tns_sel[3] = ""
-- des_star_tns_sel[4] = ""
-- des_star_tns_sel[5] = ""

raw_table('des_app_sel')
local des_app_sel = {true, true, true, true, true}
-- des_app_sel[1] = ""
-- des_app_sel[2] = ""
-- des_app_sel[3] = ""
-- des_app_sel[4] = ""
-- des_app_sel[5] = ""

raw_table('des_tns_sel')
local des_tns_sel = {true, true, true, true, true}
-- des_tns_sel[1] = ""
-- des_tns_sel[2] = ""
-- des_tns_sel[3] = ""
-- des_tns_sel[4] = ""
-- des_tns_sel[5] = ""

raw_table('ref_rwy_sel_fo')
local ref_rwy_sel_fo = {true, true, true, true, true}
-- ref_rwy_sel_fo[1] = ""
-- ref_rwy_sel_fo[2] = ""
-- ref_rwy_sel_fo[3] = ""
-- ref_rwy_sel_fo[4] = ""
-- ref_rwy_sel_fo[5] = ""

raw_table('ref_sid_sel_fo')
local ref_sid_sel_fo = {true, true, true, true, true}
-- ref_sid_sel_fo[1] = ""
-- ref_sid_sel_fo[2] = ""
-- ref_sid_sel_fo[3] = ""
-- ref_sid_sel_fo[4] = ""
-- ref_sid_sel_fo[5] = ""

raw_table('ref_tns_sel_fo')
local ref_tns_sel_fo = {true, true, true, true, true}
-- ref_tns_sel_fo[1] = ""
-- ref_tns_sel_fo[2] = ""
-- ref_tns_sel_fo[3] = ""
-- ref_tns_sel_fo[4] = ""
-- ref_tns_sel_fo[5] = ""

raw_table('des_star_sel_fo')
local des_star_sel_fo = {true, true, true, true, true}
-- des_star_sel_fo[1] = ""
-- des_star_sel_fo[2] = ""
-- des_star_sel_fo[3] = ""
-- des_star_sel_fo[4] = ""
-- des_star_sel_fo[5] = ""

raw_table('des_star_tns_sel_fo')
local des_star_tns_sel_fo = {true, true, true, true, true}
-- des_star_tns_sel_fo[1] = ""
-- des_star_tns_sel_fo[2] = ""
-- des_star_tns_sel_fo[3] = ""
-- des_star_tns_sel_fo[4] = ""
-- des_star_tns_sel_fo[5] = ""

raw_table('des_app_sel_fo')
local des_app_sel_fo = {true, true, true, true, true}
-- des_app_sel_fo[1] = ""
-- des_app_sel_fo[2] = ""
-- des_app_sel_fo[3] = ""
-- des_app_sel_fo[4] = ""
-- des_app_sel_fo[5] = ""

raw_table('des_tns_sel_fo')
local des_tns_sel_fo = {true, true, true, true, true}
-- des_tns_sel_fo[1] = ""
-- des_tns_sel_fo[2] = ""
-- des_tns_sel_fo[3] = ""
-- des_tns_sel_fo[4] = ""
-- des_tns_sel_fo[5] = ""

set_ils = 0
gs_prompt = 0
legs_offset = 0
legs_select = 0
legs_delete = 0
legs_delete_old = 0
legs_delete_old2 = 0
legs_delete_item = 0
legs_delete_key = 0
legs_page = 0
legs_button = 0
direct_to = 0
direct_to_offset = 0
legs_dir = 0

raw_table('legs_restr_spd')
legs_restr_spd = {}
legs_restr_spd_n = 0

raw_table('legs_restr_alt')
legs_restr_alt = {}
legs_restr_alt_n = 0

tc_lat = 0
tc_lon = 0

td_lat = 0
td_lon = 0

decel_lat = 0
decel_lon = 0
was_decel = 0

vnav_update = 0

offset_old = 0

temp_ils3 = ""
temp_ils4 = ""

raw_table('ed_fix_found2')
ed_fix_found2 = {}
raw_table('ed_fix_alt2')
ed_fix_alt2 = {}
raw_table('ed_fix_vpa2')
ed_fix_vpa2 = {}
raw_table('ed_fix_type')
ed_fix_type = {}
raw_table('ed_fix_table')
ed_fix_table = {}
raw_table('ed_fix_distance')
ed_fix_distance = {}
raw_table('ed_fix_tl')
ed_fix_tl = {}

dist_tc = 0
time_tc = 0
dist_td = 0
time_td = 0
time_ed = 0

dist_dest_mod = 0
time_dest_mod = ""
dist_tc_mod = 0
time_tc_mod = 0
dist_td_mod = 0
time_td_mod = 0
dist_ed_mod = 0
time_ed_mod = 0
fuel_ed_mod = 0
fuel_tc_mod = 0
fuel_td_mod = 0

fms_msg_sound = 0

lock_bank = 0
file_path_cfg = ""

chock_pos_x = 0
chock_pos_y = 0
chock_pos_z = 0

pause_td_disable = 0

pre_flt_pos_init = 0
pre_flt_perf_init = 0
pre_flt_rte = 0
pre_flt_dep = 0

last_lat = 0
last_lon = 0
last_offset = 0

last_offset2 = 0
offset_mod_x2 = 0

offset_mod = 0

rw_dist = 0
rw_fac_crs2 = 0

head_wind = 0

set_chock = 0
chock_timer = 0

apt_data_num = 0
raw_table('apt_data')
apt_data = {}
rnw_data_num = 0
raw_table('rnw_data')
rnw_data = {}

ref_runway_lenght = 0
ref_runway_lat = 0
ref_runway_lon = 0
ref_runway_crs = 0
des_runway_lenght = 0
des_runway_lat = 0
des_runway_lon = 0
des_runway_crs = 0
ref_runway_old_name = ""
des_runway_old_name = ""

ref_runway_lenght_mod = 0
ref_runway_lat_mod = 0
ref_runway_lon_mod = 0
ref_runway_crs_mod = 0
des_runway_lenght_mod = 0
des_runway_lat_mod = 0
des_runway_lon_mod = 0
des_runway_crs_mod = 0
ref_runway_old_name_mod = ""
des_runway_old_name_mod = ""
des_runway_end_lat_mod = 0
des_runway_end_lon_mod = 0

ref_runway_lenght_mod2 = 0
ref_runway_lat_mod2 = 0
ref_runway_lon_mod2 = 0
ref_runway_crs_mod2 = 0
des_runway_lenght_mod2 = 0
des_runway_lat_mod2 = 0
des_runway_lon_mod2 = 0
des_runway_crs_mod2 = 0
ref_runway_old_name_mod2 = ""
des_runway_old_name_mod2 = ""
des_runway_end_lat_mod2 = 0
des_runway_end_lon_mod2 = 0

des_rnw_nd = ""
des_rnw_nd_mod = ""
des_rnw_nd_mod2 = ""

des_rnw = ""
des_rnw2 = ""
log_line = ""
log_line2 = ""
log_line3 = ""
scenery_num = 0
raw_table('custom_scenery')
custom_scenery = {}

xfirst_time3 = 0
xfirst_5sec = 0

raw_table('nearest_apt_icao')
nearest_apt_icao = {true, true, true, true, true}
raw_table('nearest_apt_dist')
nearest_apt_dist = {true, true, true, true, true}
raw_table('nearest_apt_lat')
nearest_apt_lat = {true, true, true, true, true}
raw_table('nearest_apt_lon')
nearest_apt_lon = {true, true, true, true, true}
raw_table('nearest_apt_icao2')
nearest_apt_icao2 = {true, true, true, true, true}
raw_table('nearest_apt_dist2')
nearest_apt_dist2 = {true, true, true, true, true}
raw_table('nearest_apt_lat2')
nearest_apt_lat2 = {true, true, true, true, true}
raw_table('nearest_apt_lon2')
nearest_apt_lon2 = {true, true, true, true, true}

nearest_apt_list_num = 0
raw_table('nearest_apt_list_icao')
nearest_apt_list_icao = {true, true, true, true, true}
raw_table('nearest_apt_list_dist')
nearest_apt_list_dist = {true, true, true, true, true}
raw_table('nearest_apt_list_dist2')
nearest_apt_list_dist2 = {true, true, true, true, true}
raw_table('nearest_apt_list_via')
nearest_apt_list_via = {true, true, true, true, true}
raw_table('nearest_apt_list_lat')
nearest_apt_list_lat = {true, true, true, true, true}
raw_table('nearest_apt_list_lon')
nearest_apt_list_lon = {true, true, true, true, true}
raw_table('nearest_apt_list_wind')
nearest_apt_list_wind = {true, true, true, true, true}
raw_table('nearest_apt_list_alt')
nearest_apt_list_alt = {true, true, true, true, true}
raw_table('nearest_apt_list_dir_dist')
nearest_apt_list_dir_dist = {true, true, true, true, true}

altern_apt_list_num = 0
raw_table('altern_apt_list_icao')
altern_apt_list_icao = {true, true, true, true, true}
raw_table('altern_apt_list_dist')
altern_apt_list_dist = {true, true, true, true, true}
raw_table('altern_apt_list_via')
altern_apt_list_via = {true, true, true, true, true}
raw_table('altern_apt_list_lat')
altern_apt_list_lat = {true, true, true, true, true}
raw_table('altern_apt_list_lon')
altern_apt_list_lon = {true, true, true, true, true}
raw_table('altern_apt_list_alt')
altern_apt_list_alt = {true, true, true, true, true}
raw_table('altern_apt_list_wind')
altern_apt_list_wind = {true, true, true, true, true}
raw_table('altern_apt_list_dist2')
altern_apt_list_dist2 = {true, true, true, true, true}

first_time_gls = 0
gls_from = 0
gls_to = 0
gls_page = 0
gls_page1_num = 0
gls_page2_num = 0
raw_table('gls_page1')
gls_page1 = {}
raw_table('gls_page2')
gls_page2 = {}
gls_teak = 0


near_apt1_dis = 0
near_apt2_dis = 0
near_apt1_icao = ""
near_apt2_icao = ""



-- *******************************************************************************--


after_load_flight_TMR = 0
des_app_from_apt = 0
altitude_last = 0
goaround_altitude_last = 41000
ref_icao_pos = "               "
ref_icao_pos2 = "               "
icao_latitude = 0
icao_longitude = 0
icao_tns_alt = 0
icao_tns_lvl = 0

awy_data_num = 0
raw_table('awy_data')
awy_data = {}

raw_table('awy_path')
awy_path = {}
awy_path_num = 0

awy_temp_num2 = 0
raw_table('awy_temp2')
awy_temp2 = {}

via_via_entry = ""
via_via_ok = 0

custom_wpt_num = 0
raw_table('custom_wpt')
custom_wpt  = {}

wpt_id_paste = 0
wpt_id_nav_tmp = ""
wpt_id_brg_tmp = ""
wpt_id_dist_tmp = ""
wpt_id_nav_tmp02 = ""
wpt_id_brg_tmp02 = ""

calc_lat = 0
calc_lon = 0
calc_lat_fix = 0
calc_lon_fix = 0

inter1_x = 0
inter1_y = 0
inter2_x = 0
inter2_y = 0

rte_copy_idx = 0
rte_copy_idxb = 0

new_hold = 0
new_hold_inbd = "---"
new_hold_turn = -1
new_hold_time = "-.-"
new_hold_dist = "--.-"
new_hold_idx = 0

new_hold2 = 0
new_hold_inbd2 = "---"
new_hold_turn2 = -1
new_hold_time2 = "-.-"
new_hold_dist2 = "--.-"
new_hold_idx2 = 0

hold_data_num2 = 0
raw_table('hold_data2')
hold_data2 = {}
new_hold_act_page = 0
new_hold_act_page2 = 0

hold_offset = 0
hold_offset_idx = 0
hold_offset_idx2 = 0
hold_circuit = 0
hold_time_set = 0
hold_dist_set = 0
hold_timer = 0
pi_phase = 0

ils_id = ""
ils_freq = 0
ils_course = 0
lda_id = ""
lda_freq = 0
lda_course = 0
gls_id = ""
gls_channel = 0
gls_course = 0

add_star = 0
add_star2 = 0
add_app = 0
del_execute = 0

output_str = ""
output_str1 = ""
output_str2 = ""
output_num1 = 0
output_num2 = 0

radii_dist = 0
radii_dist_ctr = 0
radii_lat = 0
radii_lon = 0
radii_turn_dir = 0
radii_inbound = 0
radii_outbound = 0
radii_brg = 0
radii_add_dist = 0
radii_add_lat = 0
radii_add_lon = 0
radii_arc_dist = 0
radii_full_dist = 0
radii_begin_crs = 0
radii_end_crs = 0
radii_end_angle = 0
radii_remain_dist = 0
turn_radii_used = 0

vnav_status_old = 0
mcp_alt_dial_old = 0
fmc1_msg_alt = 0
fmc2_msg_alt = 0
fmc1_msg_light = 0
fmc2_msg_light = 0
fmc_message_warn_dis = 0

hold_crs1 = 0
hold_crs2 = 0
hold_lenght = 0
hold_radius = 0
pi_radius = 0
hold_lat1 = 0
hold_lon1 = 0
hold_lat2 = 0
hold_lon2 = 0
hold_lat3 = 0
hold_lon3 = 0
hold_opposite_lat = 0
hold_opposite_lon = 0

fpln_import_ok = 0

fix_data_num = 0
raw_table('fix_data')
fix_data = {}


-- *******************************************************************************--

raw_table('fix_abm_act')
local fix_abm_act = {true, true, true, true, true}
raw_table('fix_abm_wpt_idx')
local fix_abm_wpt_idx = {true, true, true, true, true}
raw_table('fix_abm_wpt_dist')
local fix_abm_wpt_dist = {true, true, true, true, true}
raw_table('fix_abm_lat')
local fix_abm_lat = {true, true, true, true, true}
raw_table('fix_abm_lon')
local fix_abm_lon = {true, true, true, true, true}
raw_table('fix_abm_brg')
local fix_abm_brg = {true, true, true, true, true}
raw_table('fix_abm_dist')
local fix_abm_dist = {true, true, true, true, true}
raw_table('fix_abm_eta')
local fix_abm_eta = {true, true, true, true, true}
raw_table('fix_abm_alt')
local fix_abm_alt = {true, true, true, true, true}
-- for eee = 1, 5 do
	-- fix_abm_act[eee] = 0
	-- fix_abm_wpt_idx[eee] = -1
	-- fix_abm_wpt_dist[eee] = -1
	-- fix_abm_lat[eee] = -1
	-- fix_abm_lon[eee] = -1
	-- fix_abm_brg[eee] = -1
	-- fix_abm_dist[eee] = -1
	-- fix_abm_eta[eee] = -1
	-- fix_abm_alt[eee] = -1
-- end

raw_table('fmod_preset')
local fmod_preset = {true, true, true, true, true}
fmod_preset[1] = {}
fmod_preset[2] = {}
fmod_preset[3] = {}
fmod_preset[4] = {}
fmod_preset[5] = {}

raw_table('cfg_preset')
local cfg_preset = {true, true, true, true, true}
cfg_preset[1] = {}
cfg_preset[2] = {}
cfg_preset[3] = {}
cfg_preset[4] = {}
cfg_preset[5] = {}


app_str = ""

ff_approx = 0
ff_total_old = 0
ff_sample = 0

offset_corrected = 0
msg_unavaible_crz_alt_del = 0

turn_off_turn = 0

first_desc_rest = 0
ed_found_old = 0
crz_alt_changed = 0
last_dta = 0
legs_executed = 0
legs_delete_old = 0

turn_prev_crs = 0
turn_next_crs = 0

desc_wind_tab_num = 0
raw_table('desc_wind_alt_tab')
local desc_wind_alt_tab = {}
raw_table('desc_wind_spd_tab')
local desc_wind_spd_tab = {}
raw_table('desc_wind_dir_tab')
local desc_wind_dir_tab = {}


-- *******************************************************************************--


--*************************************************************************************--
--** 					            LOCAL VARIABLES                 				 **--
--*************************************************************************************--
	
	raw_table('ref_rnw_list')
	ref_rnw_list = {}
	ref_rnw_list_num = 0
	raw_table('ref_rnw_list2')
	ref_rnw_list2 = {}
	ref_rnw_list_num2 = 0
	raw_table('des_rnw_list')
	des_rnw_list = {}
	des_rnw_list_num = 0

	raw_table('sid_list')
	sid_list = {}
	sid_list_num = 0
	raw_table('sid_tns_list')
	sid_tns_list = {}
	sid_tns_list_num = 0

	raw_table('star_list')
	star_list = {}
	star_list_num = 0
	raw_table('star_tns_list')
	star_tns_list = {}
	star_tns_list_num = 0

	raw_table('app_list')
	app_list = {}
	app_list_num = 0
	raw_table('app_tns_list')
	app_tns_list = {}
	app_tns_list_num = 0

	--RTE1
	raw_table('ref_rnw_list_1')
	ref_rnw_list_1 = {}
	ref_rnw_list_num_1 = 0
	raw_table('ref_rnw_list2_1')
	ref_rnw_list2_1 = {}
	ref_rnw_list_num2_1 = 0
	raw_table('des_rnw_list_1')
	des_rnw_list_1 = {}
	des_rnw_list_num_1 = 0

	raw_table('sid_list_1')
	sid_list_1 = {}
	sid_list_num_1 = 0
	raw_table('sid_tns_list_1')
	sid_tns_list_1 = {}
	sid_tns_list_num_1 = 0

	raw_table('star_list_1')
	star_list_1 = {}
	star_list_num_1 = 0
	raw_table('star_tns_list_1')
	star_tns_list_1 = {}
	star_tns_list_num_1 = 0

	raw_table('app_list_1')
	app_list_1 = {}
	app_list_num_1 = 0
	raw_table('app_tns_list_1')
	app_tns_list_1 = {}
	app_tns_list_num_1 = 0

	--RTE2
	raw_table('ref_rnw_list_2')
	ref_rnw_list_2 = {}
	ref_rnw_list_num_2 = 0
	raw_table('ref_rnw_list2_2')
	ref_rnw_list2_2 = {}
	ref_rnw_list_num2_2 = 0
	raw_table('des_rnw_list_2')
	des_rnw_list_2 = {}
	des_rnw_list_num_2 = 0

	raw_table('sid_list_2')
	sid_list_2 = {}
	sid_list_num_2 = 0
	raw_table('sid_tns_list_2')
	sid_tns_list_2 = {}
	sid_tns_list_num_2 = 0

	raw_table('star_list_2')
	star_list_2 = {}
	star_list_num_2 = 0
	raw_table('star_tns_list_2')
	star_tns_list_2 = {}
	star_tns_list_num_2 = 0

	raw_table('app_list_2')
	app_list_2 = {}
	app_list_num_2 = 0
	raw_table('app_tns_list_2')
	app_tns_list_2 = {}
	app_tns_list_num_2 = 0

	raw_table('temp_list')
	temp_list = {}
	temp_list_num = 0

	
-- *******************************************************************************--

	
	ref_rwy2 = "-----"
	ref_rwy3 = "-----"
	ref_sid2 = "------"
	ref_sid_tns2 = "------"
	des_app2 = "------"
	des_app_tns2 = "------"
	des_star2 = "------"
	des_star_trans2 = "------"
	
	des_star2_old = "------"
	des_star_trans2_old = "------"
	des_app2_old = "------"
	des_app_tns2_old = "------"
	
	sel_ref_rwy2 = "-----"
	sel_ref_sid2 = "------"
	sel_ref_sid_tns2 = "------"
	
	sel_des_star2 = "------"
	sel_des_star_trans2 = "------"
	sel_des_app2 = "------"
	sel_des_app_tns2 = "------"
	
	ref_exec = 0
	des_exec = 0
	ref_icao_x = "----"
	des_icao_x = "****"
	arr_data = 0
	arr_data_x = 0
	
	legs_data_tmp_n = 0
	raw_table('legs_data_tmp')
	legs_data_tmp = {}
	legs_data2_tmp_n = 0
	raw_table('legs_data2_tmp')
	legs_data2_tmp = {}
	
	legs_num2 = 0
	raw_table('legs_data2')
	legs_data2 = {}
	
	legs_num2b = 0
	raw_table('legs_data2b')
	legs_data2b = {}
	rte_add_sid_act = 0
	rte_add_star_act = 0
	rte_add_app_act = 0
	add_sid_star_app_act = 0
	sid_cnt = 0
	rnw_added = 0
	add_ok = 0
	wrong_rw = 0
	offset_sid_mode = 0
	offset_sid_idx = 0
	offset_sid_idx2 = 0
	added_sid_idx = 0
	added_sid_tns_idx = 0
	app_via_mised = 0
	legs_data2_tmp_nb = 0
	raw_table('legs_data2_tmpb')
	legs_data2_tmpb = {}
	first_add_star_idx = 0
	first_add_app_idx = 0
	first_add_app_idx2 = 0
	
	legs_num2m = 0
	raw_table('legs_data2m')
	legs_data2m = {}
	
	legs_num2mm = 0
	raw_table('legs_data2mm')
	legs_data2mm = {}
	
	rte_data_num = 0
	raw_table('rte_data')
	rte_data = {}
	
	local legs_abeam_data_n = 0
	raw_table('legs_abeam_data')
	local legs_abeam_data = {}
	
	legs_num3 = 0
	raw_table('legs_data3')
	legs_data3 = {}
	
	-- offset_act = 0
	offset_start = 0
	offset_end = 0
	offset_dist = 0
	offset_side = 0
	offset_start_def = 0
	offset_end_def = 0
	offset_start_def_mod = 0
	offset_end_def_mod = 0
	offset_act_mod = 0
	offset_start_mod = 0
	offset_end_mod = 0
	offset_dist_mod = 0
	offset_side_mod = 0
	offset_act_old = 0
	
	item_sel = 0
	item_sel2 = 0
	item_sel_act = 0
	item_sel_len = 0
	item_sel_act2 = 0
	item_sel_len2 = 0
	
	
	item_sel_via = 0
	item_sel_via2 = 0
	
	legs_num8 = 0
	raw_table('legs_data8')
	legs_data8 = {}
	fpln_num4 = 0
	raw_table('fpln_data4')
	fpln_data4 = {}
	hold_data_num4 = 0
	raw_table('hold_data4')
	hold_data4 = {}
	hold_offset2 = 0
	hold_offset_idx7 = 0
	hold_offset_idx8 = 0
	
	earth_nav_num = 0  --number navid
	raw_table('earth_nav')
	earth_nav = {}
	ils_nav_num = 0
	raw_table('ils_nav')
	ils_nav = {}
	gls_nav_num = 0
	raw_table('gls_nav')
	gls_nav = {}
	raw_table('idx_gls')
	idx_gls = {}
	raw_table('idx_gls_icao')
	idx_gls_icao = {}
	gls1_idx = 0
	gls2_idx = 0
	gls1_rnw_lat = 0
	gls1_rnw_lon = 0
	gls1_rnw_height = 0
	gls2_rnw_lat = 0
	gls2_rnw_lon = 0
	gls2_rnw_height = 0
	
	hold_nav_num = 0
	raw_table('hold_nav')
	hold_nav = {}
	raw_table('idx_hold')
	idx_hold = {}

	airac_navdata = ""
	airac_navdata_active = ""
	airac_cycle = ""
	
	raw_table('navaid_list')
	local navaid_list = {}
	local navaid_list_n = 0
	
	rte_lat = 0
	rte_lon = 0

	calc_rte_enable = 0
	calc_rte_act = 0
	rte_calc_lat = 0
	rte_calc_lon = 0
	calc_rte_enable_old = -1
	
	calc_rte_enable2 = 0
	calc_rte_act2 = 0
	rte_calc_lat2 = 0
	rte_calc_lon2 = 0
	calc_rte_act2_init = 0
	
	calc_rte_act7 = 0
	calc_rte_act8 = 0
	calc_rte_act8_init = 0
	calc_rte_enable8 = 0
	
	
	ref_icao_lat = 0
	ref_icao_lon = 0
	des_icao_lat = 0
	des_icao_lon = 0
	ref_tns_alt = 0
	ref_tns_lvl = 0
	des_tns_alt = 0
	des_tns_lvl = 0
	
	------
	ref_gate_x = "-----"
	co_route_x = "------------"
	exec_activate_fpln = 0
	ref_icao_lat_x = 0
	ref_icao_lon_x = 0
	ref_tns_alt_x = 0
	ref_tns_lvl_x = 0
	ref_icao_alt_x = 0
	des_icao_lat_x = 0
	des_icao_lon_x = 0
	des_tns_alt_x = 0
	des_tns_lvl_x = 0
	des_icao_alt_x = 0
	--------
	
	add_disco = 0
	raw_table('temp_nav_sort')
	temp_nav_sort = {}
	
	rte_exec = 0
	dir_change = 0
	dir_idx = 0
	dir_disco = 0
	
	find_lat = 0
	find_lon = 0
	
	last_sid_idx = 0
	first_sid_idx = 0
	first_star_idx = 0
	
	af_lat = 0
	af_lon = 0
	af_dist = 0
	af_turn = 0
	
	legs_num_old = 0


-- *******************************************************************************--


	gp_available = 0
	-- MOD variable
	vnav_update_mod = 0
	last_sid_idx_mod = 0
	first_sid_idx_mod = 0
	first_app_idx_mod = 0
	first_star_idx_mod = 0
	iaf_app_idx_mod = 0
	-- id_ed_mod = ""
	idx_ed_mod = 0
	idx_ed_mod2 = 0
	rnav_idx_first_mod = 0
	first_miss_app_idx_mod = 0
	-- last_miss_app_idx_mod = 0
	missed_app_via = ""
	missed_app_distance = 0
	rnav_alt_mod = 0
	rnav_alt_mod2 = 0
	rnav_vpa_mod = 0
	rnav_vpa_mod2 = 0
	-- alt_ed_mod = 0
	-- alt_type_ed_mod = 0
	td_fix_dist_mod = 0		-- distance before td_fix_idx
	td_fix_idx_mod = 0		-- td_fix_idx
	ed_fix_found_mod = 0	-- ed fix idx
	ed_fix_alt_mod = 0		-- ed alt fix idx
	raw_table('ed_fix_found2_mod')
	ed_fix_found2_mod = {}
	raw_table('ed_fix_alt2_mod')
	ed_fix_alt2_mod = {}
	raw_table('ed_fix_vpa2_mod')
	ed_fix_vpa2_mod = {}
	raw_table('ed_fix_type_mod')
	ed_fix_type_mod = {}
	raw_table('ed_fix_table_mod')
	ed_fix_table_mod = {}
	raw_table('ed_fix_tl_mod')
	ed_fix_tl_mod = {}
	ed_fix_num_mod = 0
	ed_vpa_mod = 0
	ed_found_mod = 0
	idx_ed_mod = 0
	idx_ed_mod2 = 0
	ed_alt_mod = 0
	ed_dist_mod = 0
	td_idx2_mod = 0
	td_idx_break = 0
	ed_tl = 0
	distance_tl = 0
	ed_type_mod = 0
	ed_tl_mod = 0
	
	legs_intdir_crs_mod = 0
	
	idx_iaf = 0
	-- idx_iaf_mod = 0
	
	del_all_rest_intv = 0
	
	reset_fmc_act = 0
	reset_fmc_act2 = 0
	transf_lat = 0
	transf_lon = 0
	
	raw_table('legs_restr_spd_mod')
	legs_restr_spd_mod = {}
	legs_restr_spd_n_mod = 0
	raw_table('legs_restr_alt_mod')
	legs_restr_alt_mod = {}
	legs_restr_alt_n_mod = 0

	menu_timer = 0
	perf_exec = 0
	
	page_ref_nav_data = 0
	page_ref_nav_data_wpt = 0
	page_ref_nav_data_navaid = 0
	page_ref_nav_data_apt = 0
	page_ref_sel = 0
	ref_nav_wpt = "-----"
	ref_nav_navaid = "----"
	ref_nav_apt = "----"
	ref_nav_new = "*****"
	ref_nav_lat = 0
	ref_nav_lon = 0
	ref_nav_mag_var = ""
	ref_nav_id = "-----"
	ref_nav_rc = ""
	ref_nav_brg = "---"
	ref_nav_dist = "---"
	ref_nav_exec = 0
	ref_nav_id_lat = 0
	ref_nav_id_lon = 0
	ref_nav_freq = ""
	ref_nav_rnw = "-----"
	ref_nav_rnw_length = 0
	ref_elevation = 0
	nav_data_type = 0
	nav_data_idx = 0
	nav_data_idx2 = 0
	nav_data_entry = ""
	
	page_flt_plns = 0
	page_flt_plns2 = 0
	
	local flt_plns_data_num = 0
	raw_table('flt_plns_data')
	local flt_plns_data = {}
	local sel_flt_plns = {true, true, true, true, true}
	-- sel_flt_plns[1] = ""
	-- sel_flt_plns[2] = ""
	-- sel_flt_plns[3] = ""
	-- sel_flt_plns[4] = ""
	-- sel_flt_plns[5] = ""
	raw_table('sel_flt_plns2')
	local sel_flt_plns2 = {true, true, true, true, true}
	-- sel_flt_plns2[1] = ""
	-- sel_flt_plns2[2] = ""
	-- sel_flt_plns2[3] = ""
	-- sel_flt_plns2[4] = ""
	-- sel_flt_plns2[5] = ""
	
	fmc_align = 0
	fmc_align_old = 0
	
	rnp_lock_time = 0

	wptx_n = 0
	raw_table('wptx')
	wptx = {}
	
	cg_set_m = 0
	cg_set_in = 0
	mac = 0
	mac_zfw = 0
	
	gps_ok = 0
	menu_tick = 0
	
	align_anp = 0
	align_anp_min = 0
	align_anp_max = 0
	align_anp_time = 0
	align_source_old = 0
	
	ndx_lat = 0
	ndx_lon = 0
	ndx_mag_hdg = 0
	ndx_ahars_mag_hdg = 0
	
	dec_find_type = 0
	cust_def_data = 0
	update_apt_dat = 0
	
	raw_table('idx_earth_nav')
	idx_earth_nav = {}
	raw_table('idx_apt')
	idx_apt = {}
	raw_table('idx_ils')
	idx_ils = {}
	raw_table('idx_ils_icao')
	idx_ils_icao = {}
	raw_table('idx_rnw')
	idx_rnw = {}
	raw_table('idx_awy')
	idx_awy = {}
	
	fmc_descent_r_alt1 = ""
	fmc_climb_r_alt1 = ""
	
	flight_phase_old = 0
	
	raw_table('vnav_des_table_dist')
	vnav_des_table_dist = {}
	raw_table('vnav_des_table_alt')
	vnav_des_table_alt = {}
	raw_table('vnav_des_table_spd')
	vnav_des_table_spd = {}
	
	raw_table('vnav_des_table_flap')
	vnav_des_table_flap = {}
	raw_table('vnav_des_table_act_spd')
	vnav_des_table_act_spd = {}
	raw_table('vnav_des_table_type')
	vnav_des_table_type = {}
	vnav_des_table_num = 0
	raw_table('vnav_des_table_dist2')
	vnav_des_table_dist2 = {}
	raw_table('vnav_des_table_alt2')
	vnav_des_table_alt2 = {}
	raw_table('vnav_des_table_spd2')
	vnav_des_table_spd2 = {}
	raw_table('vnav_des_table_flap2')
	vnav_des_table_flap2 = {}
	raw_table('vnav_des_table_act_spd2')
	vnav_des_table_act_spd2 = {}
	raw_table('vnav_des_table_type2')
	vnav_des_table_type2 = {}
	vnav_des_table_num2 = 0

	vnav_alt_err_ratio_old = 0
	gp_alt_err_ratio_old = 0
	vnav_alt_err_ratio2_old = 0
	gp_alt_err_ratio2_old = 0
	timer_pth_err = 0
	
	fuel_ed = 0
	fuel_tc = 0
	fuel_td = 0
	
	first_alt_restrict = 0
	
	file_atis = 0
	ac_atis_icao = "----"
	acars_atis_status = 0
	atis_time = ""
	raw_table('atis_message')
	atis_message = {}
	atis_msg_status = 0
	atis_tick = 0
	atis_validtime = 0
	vhf_in_prog = "            "
	atis_send_time = "     "
	receive_msg = 0
	
	x_delay = 0
	
	des_app_rw_only = 0
	rw_ext_dist = "--.-"
	rw_ext_fpa = "-.--"
	des_app_rw_only_x = 0
	rw_ext_dist_x = "--.-"
	rw_ext_fpa_x = "-.--"
	change_desc_to_path = 0
	fms_recalc = 0
	rf_dist = 0
	leg_dist = 0
	block_df = 0
	skip_hold = 0
	
	livery = ""
	livery_idx = 0
	livery_path = ""

	max_cruise_alt = 0
	max_cruise_alt_idx = 0

  td_idx_last = 0
  td_spd_rest = 0

  vnav_pth_alt_old = 0
  gp_pth_alt_old = 0
  vnav_vvi_cur = 0
  vnav_vvi_cur99 = 0
  vnav_pth_alt99_old = 0
  gp_vvi_cur = 0

crz_spd_exec = 0
fmc_cruise_speed_mod = 0
fmc_cruise_speed_mach_mod = 0
des_spd_exec = 0
fmc_descent_speed_mod = 0
fmc_descent_speed_mach_mod = 0
clb_spd_exec = 0
fmc_climb_speed_mod = 0
fmc_climb_speed_mach_mod = 0
fmc_cruise_mode_mod = 0
fmc_descent_mode_mod = 0
fmc_climb_mode_mod = 0

crz_wind_dir_mod = "---"
crz_wind_spd_mod = "---"
isa_dev_f_mod = "----"
isa_dev_c_mod = "---"
tc_oat_f_mod = "----"
tc_oat_c_mod = "---"

elevation_ft = 0
vnav_mode = 0
gp_mode = 0

fix_rnw_lat = 0
fix_rnw_lon = 0
fix_rnw_crs = 0
fix_rnw_num = ""

nav1_nav_id_old = ""
nav2_nav_id_old = ""
nav1_nav_id_fo_old = ""
nav2_nav_id_fo_old = ""

cruise_alt_changed = 0
cruise_alt_changed2 = 0

autogate_connect_status = 0
autogate_arrival = 0

raw_table('vor_cpt_stby_value')
vor_cpt_stby_value = {true, true, true, true, true}
raw_table('ils_cpt_stby_value')
ils_cpt_stby_value = {true, true, true, true, true}
raw_table('gls_cpt_stby_value')
gls_cpt_stby_value = {true, true, true, true, true}
raw_table('vor_fo_stby_value')
vor_fo_stby_value = {true, true, true, true, true}
raw_table('ils_fo_stby_value')
ils_fo_stby_value = {true, true, true, true, true}
raw_table('gls_fo_stby_value')
gls_fo_stby_value = {true, true, true, true, true}

raw_table('vor_cpt_stby_value2')
vor_cpt_stby_value2 = {true, true, true, true, true}
raw_table('ils_cpt_stby_value2')
ils_cpt_stby_value2 = {true, true, true, true, true}
raw_table('vor_fo_stby_value2')
vor_fo_stby_value2 = {true, true, true, true, true}
raw_table('ils_fo_stby_value2')
ils_fo_stby_value2 = {true, true, true, true, true}


-- for iii = 0, 4 do
	-- vor_cpt_stby_value[iii] = 0
	-- ils_cpt_stby_value[iii] = 0
	-- gls_cpt_stby_value[iii] = 0
	-- vor_fo_stby_value[iii] = 0
	-- ils_fo_stby_value[iii] = 0
	-- gls_fo_stby_value[iii] = 0
	
	-- vor_cpt_stby_value2[iii] = 0
	-- ils_cpt_stby_value2[iii] = 0
	-- vor_fo_stby_value2[iii] = 0
	-- ils_fo_stby_value2[iii] = 0
-- end

tuned_nav1_id = ""
tuned_nav2_id = ""

reseted_mmr = 0
reseted_mmr2 = 0
first_time_mmr = 0
mmr_old = 0
mmr_cpt_act_mode_old = 0
mmr_fo_act_mode_old = 0

ffirst_time = 0
ffirst_time2 = 0
fuel_tank_last_left = 0
fuel_tank_last_center = 0
fuel_tank_last_right = 0

tat_degc = 0

ils_cpt_crs_disagree = 0
ils_cpt_id_disagree = 0
ils_fo_crs_disagree = 0
ils_fo_id_disagree = 0
gls_cpt_crs_disagree = 0
gls_cpt_id_disagree = 0
gls_fo_crs_disagree = 0
gls_fo_id_disagree = 0

fmc1_input_lag = 0
fmc2_input_lag = 0
fmc1_screen_lag = 0
fmc2_screen_lag = 0
save_fuel_timer = 0
autosave_timer = 0
autosave_old = 0

vnav_desc_spd_disable = 0
last_ed_alt = 0
hold_lat_tmp = 0
hold_lon_tmp = 0
clb_del_spd_idx = 0
clb_del_spd_idx2 = 0
clb_del_spd_idx_fo = 0
clb_del_spd_idx2_fo = 0
des_del_spd_idx = 0
des_del_spd_idx2 = 0
des_del_spd_idx_fo = 0
des_del_spd_idx2_fo = 0

full_crew_weight = 0	--1587 -- kg
time_calc_enable = 0
fuel_calculated = 0
time_calc_enable2 = 0
fuel_calculated2 = 0
zfw_real = 0
B738DR_dest_runway_id = ""
on_ground_1_old = 0

fmc_data_num = 0
raw_table('fmc_data')
fmc_data = {}
raw_table('buf_word')
buf_word = {}
fmc_fix_data_num = 0
raw_table('fmc_fix_data')
fmc_fix_data = {}

vb_calc_id = ""
vb_calc_alt = 0
vb_calc_lat = 0
vb_calc_lon = 0
after_lift_2s = 0

ident_str = ""
engine_1k = ""
engine_2k = ""
engine_3k = ""

sb_servo = 0
land_alt = 0
standby_altimeter_baro_old = 0
verify_to_spd = 0
missed_app_act_idx = 0
missed_app_alt_change = 0

engout_clb = 0
engout_crz = 0
sfp_old = 0

fmc_lock = 0
fmc2_lock = 0

nav1_stdby_Mhz = 0
nav1_stdby_khz = 0
nav2_stdby_Mhz = 0
nav2_stdby_khz = 0

start_eng1_timer = 0
start_eng2_timer = 0

wind_data_clb_num = 0
raw_table('wind_data_clb_alt')
wind_data_clb_alt = {}
raw_table('wind_data_clb_hdg')
wind_data_clb_hdg = {}
raw_table('wind_data_clb_spd')
wind_data_clb_spd = {}
wind_data_toc_num = 0
raw_table('wind_data_toc_alt')
wind_data_toc_alt = {}
raw_table('wind_data_toc_hdg')
wind_data_toc_hdg = {}
raw_table('wind_data_toc_spd')
wind_data_toc_spd = {}
wind_data_tod_num = 0
raw_table('wind_data_tod_alt')
wind_data_tod_alt = {}
raw_table('wind_data_tod_hdg')
wind_data_tod_hdg = {}
raw_table('wind_data_tod_spd')
wind_data_tod_spd = {}
wind_data_des_num = 0
raw_table('wind_data_des_alt')
wind_data_des_alt = {}
raw_table('wind_data_des_hdg')
wind_data_des_hdg = {}
raw_table('wind_data_des_spd')
wind_data_des_spd = {}
qnh_data_dest = ""
avg_isa = ""

wind_data_clb_num7 = 0
raw_table('wind_data_clb_alt7')
wind_data_clb_alt7 = {}
raw_table('wind_data_clb_hdg7')
wind_data_clb_hdg7 = {}
raw_table('wind_data_clb_spd7')
wind_data_clb_spd7 = {}
wind_data_toc_num7 = 0
raw_table('wind_data_toc_alt7')
wind_data_toc_alt7 = {}
raw_table('wind_data_toc_hdg7')
wind_data_toc_hdg7 = {}
raw_table('wind_data_toc_spd7')
wind_data_toc_spd7 = {}
wind_data_tod_num7 = 0
raw_table('wind_data_tod_alt7')
wind_data_tod_alt7 = {}
raw_table('wind_data_tod_hdg7')
wind_data_tod_hdg7 = {}
raw_table('wind_data_tod_spd7')
wind_data_tod_spd7 = {}
wind_data_des_num7 = 0
raw_table('wind_data_des_alt7')
wind_data_des_alt7 = {}
raw_table('wind_data_des_hdg7')
wind_data_des_hdg7 = {}
raw_table('wind_data_des_spd7')
wind_data_des_spd7 = {}
qnh_data_dest7 = ""
avg_isa7 = ""

nd_vert_path_enable = false
ga_data_deleted = false

irs_enable = 0

--*************************************************************************************--
--** 				             FIND X-PLANE DATAREFS            			    	 **--
--*************************************************************************************--
start_rec = 0
file_rec_path = ""
rec_alt = 0
rec_lat = 0
rec_lon = 0
rec_timer = 0

simDR_faxil_aero 			= find_dataref("sim/flightmodel/forces/faxil_aero")
simDR_faxil_prop 			= find_dataref("sim/flightmodel/forces/faxil_prop")
simDR_faxil_gear 			= find_dataref("sim/flightmodel/forces/faxil_gear")
simDR_faxil_total 			= find_dataref("sim/flightmodel/forces/faxil_total")
simDR_faxil_plugin			= find_dataref("sim/flightmodel/forces/faxil_plug_acf")

simDR_startup_running			= find_dataref("sim/operation/prefs/startup_running")
simDR_livery 					= find_dataref("sim/aircraft/view/acf_livery_path")
simDR_livery_idx				= find_dataref("sim/aircraft/view/acf_livery_index")

simDR_total_weight			= find_dataref("sim/flightmodel/weight/m_total")
simDR_payload_weight		= find_dataref("sim/flightmodel/weight/m_fixed")
simDR_fuel_weight			= find_dataref("sim/flightmodel/weight/m_fuel_total")

simDR_latitude				= find_dataref("sim/flightmodel/position/latitude")
simDR_longitude				= find_dataref("sim/flightmodel/position/longitude")

simDR_zulu_hours			= find_dataref("sim/cockpit2/clock_timer/zulu_time_hours")
simDR_zulu_minutes			= find_dataref("sim/cockpit2/clock_timer/zulu_time_minutes")
simDR_zulu_seconds			= find_dataref("sim/cockpit2/clock_timer/zulu_time_seconds")
simDR_time_month			= find_dataref("sim/cockpit2/clock_timer/current_month")
simDR_time_day				= find_dataref("sim/cockpit2/clock_timer/current_day")

simDR_gps_fail					= find_dataref("sim/operation/failures/rel_gps")
simDR_gps2_fail					= find_dataref("sim/operation/failures/rel_gps2")

simDR_OAT				= find_dataref("sim/cockpit2/temperature/outside_air_temp_degc")

simDR_on_ground					= find_dataref("sim/flightmodel2/gear/on_ground")
simDR_radio_height_pilot_ft		= find_dataref("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot")
simDR_altitude_pilot			= find_dataref("sim/cockpit2/gauges/indicators/altitude_ft_pilot")
simDR_flaps_ratio				= find_dataref("sim/cockpit2/controls/flap_handle_deploy_ratio")
simDR_flaps_handle_ratio		= find_dataref("sim/cockpit2/controls/flap_ratio")
simDR_flaps_handle_ratio_xp12	= find_dataref("sim/cockpit2/controls/flap_handle_request_ratio")

simDR_gear_deploy				= find_dataref("sim/aircraft/parts/acf_gear_deploy")

B738DR_batbus_status		= find_dataref("laminar/B738/electric/batbus_status")

simDR_ap_altitude_dial_ft		= find_dataref("sim/cockpit2/autopilot/altitude_dial_ft")
simDR_ap_vvi_dial				= find_dataref("sim/cockpit2/autopilot/vvi_dial_fpm")
simDR_airspeed_dial				= find_dataref("sim/cockpit2/autopilot/airspeed_dial_kts_mach")
B738DR_ap_vvi_dial				= create_dataref("laminar/autopilot/ap_vvi_dial", "number")

simDR_airspeed_is_mach			= find_dataref("sim/cockpit2/autopilot/airspeed_is_mach")
simDR_vvi_fpm_pilot				= find_dataref("sim/cockpit2/gauges/indicators/vvi_fpm_pilot")

simDR_autopilot_altitude_mode	= find_dataref("sim/cockpit2/autopilot/altitude_mode")
-- simDR_ap_alt_hold_ft 			= find_dataref("sim/cockpit2/autopilot/altitude_hold_ft")
B738DR_preselected_alt			= create_dataref("laminar/B738/autopilot/preselected_alt", "number")

simDR_wind_hdg					= find_dataref("sim/cockpit2/gauges/indicators/wind_heading_deg_mag")
simDR_wind_spd					= find_dataref("sim/cockpit2/gauges/indicators/wind_speed_kts")
simDR_position_mag_psi 			= find_dataref("sim/flightmodel/position/mag_psi")


simDR_ground_spd					= find_dataref("sim/flightmodel/position/groundspeed")


simDR_fms_exec_light1			= find_dataref("sim/cockpit2/radios/indicators/fms_exec_light_pilot")
simDR_airspeed_pilot			= find_dataref("sim/cockpit2/gauges/indicators/airspeed_kts_pilot")
simDR_airspeed_copilot			= find_dataref("sim/cockpit2/gauges/indicators/airspeed_kts_copilot")

simDR_throttle_use				= find_dataref("sim/flightmodel/engine/ENGN_thro_use")

simDR_elevator_trim				= find_dataref("sim/cockpit2/controls/elevator_trim")

simDR_mag_variation		= find_dataref("sim/flightmodel/position/magnetic_variation")

simDR_mag_hdg			= find_dataref("sim/cockpit2/gauges/indicators/ground_track_mag_pilot")
simDR_ahars_mag_hdg		= find_dataref("sim/cockpit2/gauges/indicators/heading_AHARS_deg_mag_pilot")
simDR_efis_sub_mode		= find_dataref("sim/cockpit/switches/EFIS_map_submode")
simDR_efis_map_range	= find_dataref("sim/cockpit2/EFIS/map_range")

simDR_ext_pwr_1_on 		= find_dataref("sim/cockpit/electrical/gpu_on")

-- PILOT
B738DR_nav_flag_gs			= create_dataref("laminar/radios/pilot/nav_flag_gs", "number")
B738DR_nav_vert_dsp			= create_dataref("laminar/radios/pilot/nav_vert_dsp", "number")
B738DR_nav_horz_dsp			= create_dataref("laminar/radios/pilot/nav_horz_dsp", "number")
B738DR_nav_flag_ft			= create_dataref("laminar/radios/pilot/nav_flag_ft", "number")
B738DR_nav_nav_id			= create_dataref("laminar/radios/pilot/nav_nav_id", "string")
B738DR_nav_dme				= create_dataref("laminar/radios/pilot/nav_dme", "number")
B738DR_nav_has_dme			= create_dataref("laminar/radios/pilot/nav_has_dme", "number")
B738DR_nav_obs				= create_dataref("laminar/radios/pilot/nav_obs", "number")
B738DR_nav_bearing			= create_dataref("laminar/radios/pilot/nav_bearing", "number")
B738DR_nav_course			= create_dataref("laminar/radios/pilot/nav_course", "number")
B738DR_nav_type				= create_dataref("laminar/radios/pilot/nav_type", "number")
B738DR_nav_tuned_type		= create_dataref("laminar/radios/pilot/nav_tuned_type", "number")
B738DR_nav_tuned_crs		= create_dataref("laminar/radios/pilot/nav_tuned_crs", "number")
B738DR_nav_freq				= create_dataref("laminar/radios/pilot/nav_freq", "number")
B738DR_nav_id_decode		= create_dataref("laminar/radios/pilot/nav_id_decode", "number")
B738DR_nav_hdef				= create_dataref("laminar/radios/pilot/nav_hdef", "number")
B738DR_nav_vdef				= create_dataref("laminar/radios/pilot/nav_vdef", "number")

-- COPILOT
B738DR_nav_flag_gs_fo		= create_dataref("laminar/radios/copilot/nav_flag_gs", "number")
B738DR_nav_vert_dsp_fo		= create_dataref("laminar/radios/copilot/nav_vert_dsp", "number")
B738DR_nav_horz_dsp_fo		= create_dataref("laminar/radios/copilot/nav_horz_dsp", "number")
B738DR_nav_flag_ft_fo		= create_dataref("laminar/radios/copilot/nav_flag_ft", "number")
B738DR_nav_nav_id_fo		= create_dataref("laminar/radios/copilot/nav_nav_id", "string")
B738DR_nav_dme_fo			= create_dataref("laminar/radios/copilot/nav_dme", "number")
B738DR_nav_has_dme_fo		= create_dataref("laminar/radios/copilot/nav_has_dme", "number")
B738DR_nav_obs_fo			= create_dataref("laminar/radios/copilot/nav_obs", "number")
B738DR_nav_bearing_fo		= create_dataref("laminar/radios/copilot/nav_bearing", "number")
B738DR_nav_course_fo		= create_dataref("laminar/radios/copilot/nav_course", "number")
B738DR_nav_type_fo			= create_dataref("laminar/radios/copilot/nav_type", "number")
B738DR_nav_tuned_type_fo	= create_dataref("laminar/radios/copilot/nav_tuned_type_fo", "number")
B738DR_nav_tuned_crs_fo		= create_dataref("laminar/radios/copilot/nav_tuned_crs_fo", "number")
B738DR_nav_freq_fo			= create_dataref("laminar/radios/copilot/nav_freq_fo", "number")
B738DR_nav_id_decode_fo		= create_dataref("laminar/radios/copilot/nav_id_decode_fo", "number")
B738DR_nav_hdef_fo			= create_dataref("laminar/radios/copilot/nav_hdef_fo", "number")
B738DR_nav_vdef_fo			= create_dataref("laminar/radios/copilot/nav_vdef_fo", "number")


simDR_nav1_course_pilot		= find_dataref("sim/cockpit2/radios/actuators/nav1_course_deg_mag_pilot")
simDR_nav1_course_copilot	= find_dataref("sim/cockpit2/radios/actuators/nav1_course_deg_mag_copilot")
simDR_nav2_course_pilot		= find_dataref("sim/cockpit2/radios/actuators/nav2_course_deg_mag_pilot")
simDR_nav2_course_copilot	= find_dataref("sim/cockpit2/radios/actuators/nav2_course_deg_mag_copilot")

simDR_approach_status		= find_dataref("sim/cockpit2/autopilot/approach_status")
simDR_glideslope_status		= find_dataref("sim/cockpit2/autopilot/glideslope_status")
simDR_nav_status			= find_dataref("sim/cockpit2/autopilot/nav_status")

simDR_elevation_m			= find_dataref("sim/flightmodel/position/elevation")

simDR_cg					= find_dataref("sim/flightmodel/misc/cgz_ref_to_default")

simDR_cowl_ice_on			= find_dataref("sim/cockpit2/ice/ice_inlet_heat_on_per_engine")
B738DR_wing_l_heat_on		= find_dataref("laminar/B738/ice/wing_l_heat_on")
B738DR_wing_r_heat_on		= find_dataref("laminar/B738/ice/wing_r_heat_on")
B738DR_wing_heat_pos		= find_dataref("laminar/B738/ice/wing_heat_pos")

-- simDR_TAT					= find_dataref("sim/cockpit2/temperature/outside_air_LE_temp_degc")
simDR_TAT					= find_dataref("laminar/B738/indicated/TAT")
simDR_stat_pitot_capt		= find_dataref("sim/cockpit2/ice/ice_static_heat_on_pilot")

simDR_engine_on 			= find_dataref("sim/flightmodel2/engines/engine_is_burning_fuel")
simDR_eng_N1					= find_dataref("sim/flightmodel2/engines/N1_percent")

simDR_hide_yoke				= find_dataref("sim/graphics/view/hide_yoke")
simDR_roll					= find_dataref("sim/cockpit2/gauges/indicators/roll_AHARS_deg_pilot")

simDR_pause					= find_dataref("sim/time/paused")

simDR_nav1_relative_hdg		= find_dataref("sim/cockpit2/radios/indicators/nav1_relative_heading_AHARS_deg_pilot")

-- simDR_TAS					= find_dataref("sim/flightmodel/position/true_airspeed")
simDR_TAS					= find_dataref("laminar/B738/indicated/TAS")

simDR_kill_map_fms			= find_dataref("sim/graphics/misc/kill_map_fms_line")
simDR_fuel_tank_weight_kg 	= find_dataref("sim/flightmodel/weight/m_fuel")
B738DR_fuel_temp_real			= find_dataref("laminar/B738/engine/fuel_temp_real")

B738DR_gpu_available	= create_dataref("laminar/B738/gpu_available2", "number")

B738DR_jetway_power			= find_dataref("laminar/B738/jetway_power")

function autogate_gpu_handl() end
function autogate_nearest_handl() end
function autogate_nearest_name_handl() end
function de_ice_truck_available_handl() end
B738DR_autogate_gpu			= create_dataref("laminar/B738/autogate_gpu", "number", autogate_gpu_handl)
B738DR_autogate_nearest		= create_dataref("laminar/B738/autogate_nearest", "number", autogate_nearest_handl)
B738DR_autogate_nearest_name	= create_dataref("laminar/B738/autogate_nearest_name", "string", autogate_nearest_name_handl)
B738DR_de_ice_truck_available = create_dataref("laminar/B738/de_ice_truck_available", "number", de_ice_truck_available_handl)

simDR_pitch_nz 				= find_dataref("sim/joystick/joystick_pitch_nullzone")
simDR_roll_nz 				= find_dataref("sim/joystick/joystick_roll_nullzone")
simDR_yaw_nz 				= find_dataref("sim/joystick/joystick_heading_nullzone")

simDR_ff 					= find_dataref("sim/flightmodel/engine/ENGN_FF_")
simDR_press_set_vvi			= find_dataref("sim/cockpit/pressure/cabin_vvi_set_m_msec")

simDR_brake					= find_dataref("sim/cockpit2/controls/parking_brake_ratio")
B738DR_ac_tnsbus1_status	= find_dataref("laminar/B738/electric/ac_tnsbus1_status")

simDR_axis = find_dataref("sim/joystick/joystick_axis_values")
simDR_axis_min = find_dataref("sim/joystick/joystick_axis_minimum")
simDR_axis_max = find_dataref("sim/joystick/joystick_axis_maximum")

find_lat_mag_var			= find_dataref("zibomod/lat_mag_var")
find_lon_mag_var			= find_dataref("zibomod/lon_mag_var")
find_calc_mag_var			= find_dataref("zibomod/calc_mag_var")
find_mag_var				= find_dataref("zibomod/mag_var")

simDR_sea_level_inhg_xp11		= find_dataref("sim/weather/barometer_sealevel_inhg")
simDR_sea_level_inhg_xp12		= find_dataref("sim/weather/region/sealevel_pressure_pas")
simDR_sea_level_temp_xp12		= find_dataref("sim/weather/region/sealevel_temperature_c")
simDR_sea_level_inhg = 0
simDR_sea_level_temp = 15
simDR_barometer_setting_capt 	= find_dataref("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_pilot")
simDR_barometer_setting_fo		= find_dataref("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_copilot")
simDR_autopilot_side			= find_dataref("sim/cockpit2/autopilot/autopilot_source")

simDR_instrument_brightness	= find_dataref("sim/cockpit2/switches/instrument_brightness_ratio")

simDR_eng_N2					= find_dataref("sim/flightmodel2/engines/N2_percent")

--*************************************************************************************--
--** 				               FIND X-PLANE COMMANDS                   	    	 **--
--*************************************************************************************--


simCMD_nosmoking_toggle		= find_command("sim/systems/no_smoking_toggle")

simCMD_autopilot_lvl_chg	= find_command("sim/autopilot/level_change")
simCMD_autopilot_vs_sel		= find_command("sim/autopilot/vertical_speed_pre_sel")

simCMD_pause				= find_command("sim/operation/pause_toggle")

simCMD_autopilot_alt_hold	= find_command("sim/autopilot/altitude_hold")

--*************************************************************************************--
--** 				              FIND CUSTOM DATAREFS             			    	 **--
--*************************************************************************************--

B738DR_fmc_descent_speed_l_act	= create_dataref("laminar/B738/autopilot/fmc_descent_speed_l_act", "number")

function B738DR_fmc_climb_speed_DRhandler()end
function B738DR_fmc_climb_speed_l_DRhandler()end
function B738DR_fmc_climb_speed_mach_DRhandler()end
function B738DR_fmc_climb_r_speed1_DRhandler()end
function B738DR_fmc_climb_r_alt1_DRhandler()end
function B738DR_fmc_climb_r_speed2_DRhandler()end
function B738DR_fmc_climb_r_alt2_DRhandler()end

function B738DR_fmc_cruise_speed_DRhandler()end
function B738DR_fmc_cruise_speed_mach_DRhandler()end
function B738DR_fmc_cruise_alt_DRhandler()end

function B738DR_fmc_descent_speed_DRhandler()end
function B738DR_fmc_descent_speed_mach_DRhandler()end
function B738DR_fmc_descent_alt_DRhandler()end
function B738DR_fmc_descent_r_speed1_DRhandler()end
function B738DR_fmc_descent_r_alt1_DRhandler()end
function B738DR_fmc_descent_r_speed2_DRhandler()end
function B738DR_fmc_descent_r_alt2_DRhandler()end
function B738DR_fmc_approach_alt_DRhandler()end

-- CLIMB
B738DR_fmc_climb_speed			= create_dataref("laminar/B738/autopilot/fmc_climb_speed", "number", B738DR_fmc_climb_speed_DRhandler)
B738DR_fmc_climb_speed_l		= create_dataref("laminar/B738/autopilot/fmc_climb_speed_l", "number", B738DR_fmc_climb_speed_l_DRhandler)
B738DR_fmc_climb_speed_mach		= create_dataref("laminar/B738/autopilot/fmc_climb_speed_mach", "number", B738DR_fmc_climb_speed_mach_DRhandler)
B738DR_fmc_climb_r_speed1		= create_dataref("laminar/B738/autopilot/fmc_climb_r_speed1", "number", B738DR_fmc_climb_r_speed1_DRhandler)
B738DR_fmc_climb_r_alt1			= create_dataref("laminar/B738/autopilot/fmc_climb_r_alt1", "number", B738DR_fmc_climb_r_alt1_DRhandler)
B738DR_fmc_climb_r_speed2		= create_dataref("laminar/B738/autopilot/fmc_climb_r_speed2", "number", B738DR_fmc_climb_r_speed2_DRhandler)
B738DR_fmc_climb_r_alt2			= create_dataref("laminar/B738/autopilot/fmc_climb_r_alt2", "number", B738DR_fmc_climb_r_alt2_DRhandler)

-- CRUISE
B738DR_fmc_cruise_speed			= create_dataref("laminar/B738/autopilot/fmc_cruise_speed", "number", B738DR_fmc_cruise_speed_DRhandler)
B738DR_fmc_cruise_speed_mach	= create_dataref("laminar/B738/autopilot/fmc_cruise_speed_mach", "number", B738DR_fmc_cruise_speed_mach_DRhandler)
B738DR_fmc_cruise_alt			= create_dataref("laminar/B738/autopilot/fmc_cruise_alt", "number", B738DR_fmc_cruise_alt_DRhandler)

-- DESCENT
B738DR_fmc_descent_speed		= create_dataref("laminar/B738/autopilot/fmc_descent_speed", "number", B738DR_fmc_descent_speed_DRhandler)
B738DR_fmc_descent_speed_mach	= create_dataref("laminar/B738/autopilot/fmc_descent_speed_mach", "number", B738DR_fmc_descent_speed_mach_DRhandler)
B738DR_fmc_descent_alt			= create_dataref("laminar/B738/autopilot/fmc_descent_alt", "number", B738DR_fmc_descent_alt_DRhandler)
B738DR_fmc_descent_r_speed1		= create_dataref("laminar/B738/autopilot/fmc_descent_r_speed1", "number", B738DR_fmc_descent_r_speed1_DRhandler)
B738DR_fmc_descent_r_alt1		= create_dataref("laminar/B738/autopilot/fmc_descent_r_alt1", "number", B738DR_fmc_descent_r_alt1_DRhandler)
B738DR_fmc_descent_r_speed2		= create_dataref("laminar/B738/autopilot/fmc_descent_r_speed2", "number", B738DR_fmc_descent_r_speed2_DRhandler)
B738DR_fmc_descent_r_alt2		= create_dataref("laminar/B738/autopilot/fmc_descent_r_alt2", "number", B738DR_fmc_descent_r_alt2_DRhandler)

B738DR_fmc_vnav_v2_20			= create_dataref("laminar/B738/autopilot/fmc_vnav_v2_20", "number")

B738DR_fmc_descent_speed_mach_init 	= create_dataref("laminar/B738/autopilot/fmc_descent_speed_mach_init", "number")
B738DR_fmc_descent_speed_init 		= create_dataref("laminar/B738/autopilot/fmc_descent_speed_init", "number")


-- APPROACH
B738DR_fmc_approach_alt			= create_dataref("laminar/B738/autopilot/fmc_approach_alt", "number", B738DR_fmc_approach_alt_DRhandler)

B738DR_fms_vref			= create_dataref("laminar/B738/FMS/vref", "number")
B738DR_fms_vref_15		= create_dataref("laminar/B738/FMS/vref_15", "number")
B738DR_fms_vref_25		= create_dataref("laminar/B738/FMS/vref_25", "number")
B738DR_fms_vref_30		= create_dataref("laminar/B738/FMS/vref_30", "number")
B738DR_fms_vref_40		= create_dataref("laminar/B738/FMS/vref_40", "number")

B738DR_fms_v1_calc		= create_dataref("laminar/B738/FMS/v1_calc", "number")
B738DR_fms_vr_calc		= create_dataref("laminar/B738/FMS/vr_calc", "number")
B738DR_fms_v2_calc		= create_dataref("laminar/B738/FMS/v2_calc", "number")
B738DR_trim_calc		= create_dataref("laminar/B738/FMS/trim_calc", "number")

B738DR_latitude_deg			= create_dataref("laminar/B738/latitude_deg", "number")
B738DR_latitude_min			= create_dataref("laminar/B738/latitude_min", "number")
B738DR_latitude_NS			= create_dataref("laminar/B738/latitude_NS", "number")

B738DR_longitude_deg		= create_dataref("laminar/B738/longitude_deg", "number")
B738DR_longitude_min		= create_dataref("laminar/B738/longitude_min", "number")
B738DR_longitude_EW			= create_dataref("laminar/B738/longitude_EW", "number")


B738DR_gps_pos					= create_dataref("laminar/B738/irs/gps_pos", "string")
B738DR_gps2_pos					= create_dataref("laminar/B738/irs/gps2_pos", "string")
B738DR_irs_pos					= create_dataref("laminar/B738/irs/irs_pos", "string")
B738DR_irs2_pos					= create_dataref("laminar/B738/irs/irs2_pos", "string")
B738DR_irs_status				= create_dataref("laminar/B738/irs/irs_status", "number")
B738DR_irs2_status				= create_dataref("laminar/B738/irs/irs2_status", "number")

B738DR_irs_left_mode			= create_dataref("laminar/B738/irs/irs_mode", "number")
B738DR_irs_right_mode			= create_dataref("laminar/B738/irs/irs2_mode", "number")

function B738DR_irs_source_DRhandler() end
function B738DR_irs_left_DRhandler() end
function B738DR_irs_right_DRhandler() end
B738DR_irs_source = 			create_dataref("laminar/B738/toggle_switch/irs_source", "number", B738DR_irs_source_DRhandler)
B738DR_irs_left = 				create_dataref("laminar/B738/toggle_switch/irs_left", "number", B738DR_irs_left_DRhandler)
B738DR_irs_right = 				create_dataref("laminar/B738/toggle_switch/irs_right", "number", B738DR_irs_right_DRhandler)

B738DR_alignment_left_remain	= create_dataref("laminar/B738/irs/alignment_left_remain", "number")
B738DR_alignment_right_remain	= create_dataref("laminar/B738/irs/alignment_right_remain", "number")

B738DR_irs_align_left2			= create_dataref("laminar/B738/annunciator/irs_align_left2", "number")
B738DR_irs_align_right2			= create_dataref("laminar/B738/annunciator/irs_align_right2", "number")

B738DR_irs_was_full_allign 	= create_dataref("laminar/B738/irs/irs_was_full_allign", "number")
B738DR_irs_was_full_allign2 = create_dataref("laminar/B738/irs/irs_was_full_allign2", "number")

B738DR_n1_set_source 		= find_dataref("laminar/B738/toggle_switch/n1_set_source")

B738DR_altitude_mode					= create_dataref("laminar/B738/autopilot/altitude_mode", "number")
B738DR_heading_mode						= create_dataref("laminar/B738/autopilot/heading_mode", "number")
B738DR_speed_mode						= create_dataref("laminar/B738/autopilot/speed_mode", "number")

B738DR_lvl_chg_mode						= create_dataref("laminar/B738/autopilot/lvl_chg_mode", "number")

function B738DR_irs1_restart_DRhandler() end
function B738DR_irs2_restart_DRhandler() end
B738DR_irs1_restart			= create_dataref("laminar/B738/toggle_switch/irs_restart", "number", B738DR_irs1_restart_DRhandler)
B738DR_irs2_restart			= create_dataref("laminar/B738/toggle_switch/irs2_restart", "number", B738DR_irs2_restart_DRhandler)

B738DR_thr_takeoff_full_N1	= find_dataref("laminar/B738/engine/calc/thr_takeoff_full_N1")

B738DR_thr_takeoff_N1		= find_dataref("laminar/B738/engine/calc/thr_takeoff_N1")
B738DR_thr_climb_N1			= find_dataref("laminar/B738/engine/calc/thr_climb_N1")
B738DR_thr_cruise_N1		= find_dataref("laminar/B738/engine/calc/thr_cruise_N1")
B738DR_thr_cont_N1			= find_dataref("laminar/B738/engine/calc/thr_cont_N1")
B738DR_thr_goaround_N1		= find_dataref("laminar/B738/engine/calc/thr_goaround_N1")
B738DR_thr_cutback_N1		= find_dataref("laminar/B738/engine/calc/thr_cutback_N1")
B738DR_cruise_opt_alt		= find_dataref("laminar/B738/engine/calc/cruise_opt_alt")
B738DR_cruise_max_alt		= find_dataref("laminar/B738/engine/calc/cruise_max_alt")
B738DR_cont_spd				= find_dataref("laminar/B738/engine/calc/cont_spd")
B738DR_cont_lvl				= find_dataref("laminar/B738/engine/calc/cont_lvl")
B738DR_engout_crz_alt 		= find_dataref("laminar/B738/engine/calc/engout_crz_alt")

B738DR_mcp_alt_dial			= find_dataref("laminar/B738/autopilot/mcp_alt_dial")

B738DR_baro_set_std_pilot		= find_dataref("laminar/B738/EFIS/baro_set_std_pilot")
B738DR_baro_sel_in_hg_pilot		= find_dataref("laminar/B738/EFIS/baro_sel_in_hg_pilot")
B738DR_baro_set_std_copilot		= find_dataref("laminar/B738/EFIS/baro_set_std_copilot")
B738DR_baro_sel_in_hg_copilot	= find_dataref("laminar/B738/EFIS/baro_sel_in_hg_copilot")

B738DR_efis_map_range_capt 		= find_dataref("laminar/B738/EFIS/capt/map_range")
B738DR_efis_map_range_fo 		= find_dataref("laminar/B738/EFIS/fo/map_range")
B738DR_capt_map_mode		= find_dataref("laminar/B738/EFIS_control/capt/map_mode_pos")
B738DR_fo_map_mode			= find_dataref("laminar/B738/EFIS_control/fo/map_mode_pos")
B738DR_capt_exp_map_mode	= find_dataref("laminar/B738/EFIS_control/capt/exp_map")
B738DR_fo_exp_map_mode		= find_dataref("laminar/B738/EFIS_control/fo/exp_map")

function B738DR_idx_corr_DRhandler() end
function B738DR_idx_corr2_DRhandler() end
function B738DR_idx_corr3_DRhandler() end
function B738DR_idx_corr4_DRhandler() end
B738DR_idx_corr				= create_dataref("laminar/B738/fms/idx_corr", "number", B738DR_idx_corr_DRhandler)
B738DR_idx_corr2			= create_dataref("laminar/B738/fms/idx_corr2", "number", B738DR_idx_corr2_DRhandler)
B738DR_idx_corr3			= create_dataref("laminar/B738/fms/idx_corr3", "number", B738DR_idx_corr3_DRhandler)
B738DR_idx_corr4			= create_dataref("laminar/B738/fms/idx_corr4", "number", B738DR_idx_corr4_DRhandler)


B738DR_ap_spd_interv_status	= find_dataref("laminar/B738/autopilot/spd_interv_status")
B738DR_speed_ratio			= find_dataref("laminar/B738/FMS/speed_ratio")

flaps_speed						= find_dataref("laminar/B738/FMS/flaps_speed")
vnav_speed						= find_dataref("laminar/B738/FMS/vnav_speed")
B738DR_vnav_speed_trg			= find_dataref("laminar/B738/autopilot/vnav_speed_trg")
B738DR_autopilot_vnav_status	= find_dataref("laminar/B738/autopilot/vnav_status1")
B738DR_autopilot_cmd_a_status	= find_dataref("laminar/B738/autopilot/cmd_a_status")
B738DR_autopilot_cmd_b_status	= find_dataref("laminar/B738/autopilot/cmd_b_status")


B738DR_mcp_speed_dial		= find_dataref("laminar/B738/autopilot/mcp_speed_dial_kts_mach")

B738DR_efis_data_capt_status	= find_dataref("laminar/B738/EFIS/capt/data_status")
B738DR_efis_data_fo_status		= find_dataref("laminar/B738/EFIS/fo/data_status")

B738DR_autopilot_bank_angle_pos		= find_dataref("laminar/B738/autopilot/bank_angle_pos")

vnav_alt_mode				= find_dataref("laminar/B738/autopilot/vnav_alt_mode")
B738DR_lnav_engaged			= find_dataref("laminar/B738/autopilot/lnav_engaged")
B738DR_vnav_engaged			= find_dataref("laminar/B738/autopilot/vnav_engaged")
B738DR_gls_nav_status		= find_dataref("laminar/B738/nav/gls_nav_status")
B738DR_gls_app_status		= find_dataref("laminar/B738/nav/gls_app_status")

B738DR_spd_ref 				= find_dataref("laminar/B738/toggle_switch/spd_ref")

B738DR_efis_vor_on			= find_dataref("laminar/B738/EFIS/EFIS_vor_on")
B738DR_efis_apt_on			= find_dataref("laminar/B738/EFIS/EFIS_airport_on")
B738DR_efis_fix_on			= find_dataref("laminar/B738/EFIS/EFIS_fix_on")

B738DR_efis_fo_vor_on		= find_dataref("laminar/B738/EFIS/fo/EFIS_vor_on")
B738DR_efis_fo_apt_on		= find_dataref("laminar/B738/EFIS/fo/EFIS_airport_on")
B738DR_efis_fo_fix_on		= find_dataref("laminar/B738/EFIS/fo/EFIS_fix_on")

B738DR_track_up_active		= find_dataref("laminar/B738/fms/track_up_active")
fac_gp_engaged				= find_dataref("laminar/B738/ap/fac_gp_engaged")
loc_gp_engaged				= find_dataref("laminar/B738/ap/loc_gp_engaged")
B738DR_fac_status			= find_dataref("laminar/B738/ap/fac_status")
B738DR_gp_status			= find_dataref("laminar/B738/pfd/gp_status")

B738DR_ac_tnsbus2_status	= find_dataref("laminar/B738/electric/ac_tnsbus2_status")

B738DR_calc_vspd			= find_dataref("laminar/B738/FMS/calc_vspd")
B738DR_calc_trim 			= find_dataref("laminar/B738/FMS/calc_trim")
B738DR_eng_out				= find_dataref("laminar/B738/FMS/eng_out")
B738DR_eng_out_disable		= create_dataref("laminar/B738/FMS/eng_out_disable", "number")

B738DR_pfd_flaps_up			= find_dataref("laminar/B738/pfd/flaps_up")
B738DR_pfd_flaps_1			= find_dataref("laminar/B738/pfd/flaps_1")
B738DR_pfd_flaps_5			= find_dataref("laminar/B738/pfd/flaps_5")
B738DR_pfd_flaps_10			= find_dataref("laminar/B738/pfd/flaps_10")
B738DR_pfd_flaps_15			= find_dataref("laminar/B738/pfd/flaps_15")
B738DR_pfd_min_maneuver_speed	= find_dataref("laminar/B738/pfd/min_maneuver_speed")

B738DR_alt_hold_mem 		= find_dataref("laminar/autopilot/alt_hold_mem")

B738DR_capt_vsd_map_mode	= find_dataref("laminar/B738/EFIS_control/capt/vsd_map")
B738DR_fo_vsd_map_mode		= find_dataref("laminar/B738/EFIS_control/fo/vsd_map")

AP_airspeed 				= find_dataref("laminar/B738/autopilot/airspeed")
AP_airspeed_mach 			= find_dataref("laminar/B738/autopilot/airspeed_mach")
AP_altitude 				= find_dataref("laminar/B738/autopilot/altitude")

B738DR_com3_data 			= find_dataref("laminar/B738/comm/com3/act_freq_data")

B738DR_standby_altimeter_baro 	= find_dataref("laminar/B738/gauges/standby_altimeter_baro")

B738DR_lights_test			= find_dataref("laminar/B738/annunciator/test")

----------------
function B738DR_kill_effect_DRhandler() end
function B738DR_kill_windshield_DRhandler() end
B738DR_kill_effect	= create_dataref("laminar/B738/perf/kill_effect", "number", B738DR_kill_effect_DRhandler)
B738DR_kill_windshield	= create_dataref("laminar/B738/perf/kill_windshield", "number", B738DR_kill_windshield_DRhandler)

function hide_glass_DRhandler() end
B738DR_hide_glass 				= create_dataref("laminar/B738/effect/hide_glass", "number", hide_glass_DRhandler)
function tire_blown_DRhandler() end
B738DR_tire_blown				= create_dataref("laminar/B738/effect/tire_blown", "number", tire_blown_DRhandler)
function nd_rings_DRhandler() end
B738DR_nd_rings					= create_dataref("laminar/B738/effect/nd_rings", "number", nd_rings_DRhandler)
function hide_gauges_reflection_DRhandler() end
B738DR_hide_gauges_reflection	= create_dataref("laminar/B738/effect/gauges_reflection", "number", hide_gauges_reflection_DRhandler)
function flight_ctrl_DRhandler() end
B738DR_flight_ctrl				= create_dataref("laminar/B738/effects/flight_control", "number", flight_ctrl_DRhandler)
function brake_temp_DRhandler() end
B738DR_brake_temp				= create_dataref("laminar/B738/effects/brake_temp", "number", brake_temp_DRhandler)
function backlight_color_DRhandler() end
B738DR_backlight_color			= create_dataref("laminar/B738/effects/backlight_color", "number", backlight_color_DRhandler)
function hardware_thr_lever_DRhandler() end
B738DR_hardware_thr_lever		= create_dataref("laminar/B738/effects/hardware_thr_lever", "number", hardware_thr_lever_DRhandler)
function sync_pilot_DRhandler() end
B738DR_sync_pilot				= create_dataref("laminar/B738/effects/sync_pilot", "number", sync_pilot_DRhandler)
function cust_nav_data_DRhandler() end
B738DR_cust_nav_data			= create_dataref("laminar/B738/effects/cust_nav_data", "number", cust_nav_data_DRhandler)
function prior_tx_com_DRhandler() end
B738DR_prior_tx_com				= create_dataref("laminar/B738/effects/prior_tx_com", "number", prior_tx_com_DRhandler)
function irs_ding_DRhandler() end
B738DR_irs_ding					= create_dataref("laminar/B738/effects/irs_ding", "number", irs_ding_DRhandler)
function long_led_strobes_DRhandler() end
B738DR_long_led_strobes			= create_dataref("laminar/B738/effects/long_led_strobes", "number", long_led_strobes_DRhandler)
function pulse_lite_DRhandler() end
B738DR_pulse_lite				= create_dataref("laminar/B738/effects/pulse_lite", "number", pulse_lite_DRhandler)
function led_lights_DRhandler() end
B738DR_led_lights				= create_dataref("laminar/B738/led_lights", "number", led_lights_DRhandler)
function aoa_ra_DRhandler() end
B738DR_aoa_ra					= create_dataref("laminar/B738/aoa_ra", "number", aoa_ra_DRhandler)
function strobe_cockpit_DRhandler() end
B738DR_strobe_cockpit			= create_dataref("laminar/B738/strobe_cockpit", "number", strobe_cockpit_DRhandler)
function particle_rain_DRhandler() end
B738DR_particle_rain			= create_dataref("laminar/B738/particle_rain", "number", particle_rain_DRhandler)
function fps_enable_DRhandler() end
B738DR_fps_enable				= create_dataref("laminar/B738/fps_enable", "number", fps_enable_DRhandler)
function ap_colour_DRhandler() end
B738DR_ap_colour				= create_dataref("laminar/B738/ap_colour", "number", ap_colour_DRhandler)
function white_bug_DRhandler() end
B738DR_white_bug				= create_dataref("laminar/B738/white_bug", "number", white_bug_DRhandler)
function gp_cable_connect_DRhandler() end
B738DR_gp_cable_connect			= create_dataref("laminar/B738/gp_cable_connect", "number", gp_cable_connect_DRhandler)
function chrono_variant_DRhandler() end
B738DR_chrono_variant			= create_dataref("laminar/B738/chrono_variant", "number", chrono_variant_DRhandler)
function pfd_fmc_flap_spd_DRhandler() end
B738DR_pfd_fmc_flap_spd			= create_dataref("laminar/B738/pfd_fmc_flap_spd", "number", pfd_fmc_flap_spd_DRhandler)
function std_pax_weight_DRhandler() end
B738DR_std_pax_weight			= create_dataref("laminar/B738/std_pax_weight", "array[3]", std_pax_weight_DRhandler)
function std_pax_num_DRhandler() end
B738DR_std_pax_num				= create_dataref("laminar/B738/std_pax_num", "array[5]", std_pax_num_DRhandler)
function zone_arm_DRhandler() end
B738DR_zone_arm					= create_dataref("laminar/B738/zone_arm", "array[5]", zone_arm_DRhandler)
function pax_layout_DRhandler() end
B738DR_pax_layout				= create_dataref("laminar/B738/pax_layout", "number", pax_layout_DRhandler)
function oew_kg_DRhandler() end
B738DR_oew_kg					= create_dataref("laminar/B738/oew_kg", "number", oew_kg_DRhandler)
function fa_fwd_kg_DRhandler() end
B738DR_fa_fwd_kg				= create_dataref("laminar/B738/fa_fwd_kg", "number", fa_fwd_kg_DRhandler)
function fa_aft_kg_DRhandler() end
B738DR_fa_aft_kg				= create_dataref("laminar/B738/fa_aft_kg", "number", fa_aft_kg_DRhandler)
function galley_fwd_kg_DRhandler() end
B738DR_galley_fwd_kg			= create_dataref("laminar/B738/galley_fwd_kg", "number", galley_fwd_kg_DRhandler)
function galley_aft_kg_DRhandler() end
B738DR_galley_aft_kg			= create_dataref("laminar/B738/galley_aft_kg", "number", galley_aft_kg_DRhandler)
function fd_helper_DRhandler() end
B738DR_fd_helper				= create_dataref("laminar/B738/fd_helper", "number", fd_helper_DRhandler)
function nd_tcas_range_DRhandler() end
B738DR_nd_tcas_range			= create_dataref("laminar/B738/nd_tcas_range", "number", nd_tcas_range_DRhandler)
function takeoff_hdg_DRhandler() end
B738DR_takeoff_hdg				= create_dataref("laminar/B738/takeoff_hdg", "number", takeoff_hdg_DRhandler)
function init_ref_smart_DRhandler() end
B738DR_init_ref_smart			= create_dataref("laminar/B738/init_ref_smart", "number", init_ref_smart_DRhandler)
function sync_pilot_isdf_DRhandler() end
B738DR_sync_pilot_isdf			= create_dataref("laminar/B738/effects/sync_pilot_isdf", "number", sync_pilot_isdf_DRhandler)
function hide_seats_DRhandler() end
B738DR_hide_seats				= create_dataref("laminar/B738/effects/hide_seats", "number", hide_seats_DRhandler)

function geodes() end
B738DR_geo_des			= create_dataref("laminar/B738/fms/geo_des", "number", geodes)

function pause_td_DRhandler() end
B738DR_pause_td			= create_dataref("laminar/B738/fms/pause_td", "number", pause_td_DRhandler)

function lock_idle_thrust_DRhandler() end
B738DR_lock_idle_thrust	= create_dataref("laminar/B738/fms/lock_idle_thrust", "number", lock_idle_thrust_DRhandler)

function lock_thrust_DRhandler() end
B738DR_lock_thrust		= create_dataref("laminar/B738/fms/lock_thrust", "number", lock_thrust_DRhandler)

function engine_no_running_state_DRhandler() end
B738DR_engine_no_running_state = create_dataref("laminar/B738/fms/engine_no_running_state", "number", engine_no_running_state_DRhandler)

function parkbrake_remove_chock_DRhandler() end
B738DR_parkbrake_remove_chock = create_dataref("laminar/B738/fms/parkbrake_remove_chock", "number", parkbrake_remove_chock_DRhandler)

function chock_on_startup_DRhandler() end
B738DR_chock_on_startup = create_dataref("laminar/B738/fms/chock_on_startup", "number", chock_on_startup_DRhandler)

function toe_brakes_ovr_DRhandler() end
B738DR_toe_brakes_ovr	= create_dataref("laminar/B738/fms/toe_brakes_ovr", "number", toe_brakes_ovr_DRhandler)

function throttle_noise_DRhandler() end
B738DR_throttle_noise	= create_dataref("laminar/B738/fms/throttle_noise", "number", throttle_noise_DRhandler)

function fuelgauge_DRhandler() end
B738DR_fuelgauge		= create_dataref("laminar/B738/effects/fuelgauge", "number", fuelgauge_DRhandler)

function nosewheel_DRhandler() end
B738DR_nosewheel		= create_dataref("laminar/B738/effects/nosewheel", "number", nosewheel_DRhandler)

function fpln_format_DRhandler() end
B738DR_fpln_format		= create_dataref("laminar/B738/fms/fpln_format", "number", fpln_format_DRhandler)

function track_up_DRhandler() end
B738DR_track_up			= create_dataref("laminar/B738/fms/track_up", "number", track_up_DRhandler)

function baro_in_hpa_DRhandler() end
B738DR_baro_in_hpa		= create_dataref("laminar/B738/fms/baro_in_hpa", "number", baro_in_hpa_DRhandler)

function min_baro_radio_DRhandler() end
B738DR_min_baro_radio	= create_dataref("laminar/B738/fms/min_baro_radio", "number", min_baro_radio_DRhandler)

function vor_radial_DRhandler() end
B738DR_vor_radial		= create_dataref("laminar/B738/fms/vor_radial", "number", vor_radial_DRhandler)

function flap_detents_DRhandler() end
B738DR_flap_detents		= create_dataref("laminar/B738/fms/flap_detents", "array[9]", flap_detents_DRhandler)

function flap_detents_thr_DRhandler() end
B738DR_flap_detents_thr	= create_dataref("laminar/B738/fms/flap_detents_thr", "array[9]", flap_detents_thr_DRhandler)

function passengers_DRhandler() end
B738DR_passengers		= create_dataref("laminar/B738/effects/passengers", "number", passengers_DRhandler)

function wings_variant_DRhandler() end
B738DR_wings_variant	= create_dataref("laminar/B738/wings_variant", "number", wings_variant_DRhandler)

function satcom_variant_DRhandler() end
B738DR_satcom_variant	= create_dataref("laminar/B738/satcom_variant", "number", satcom_variant_DRhandler)

function hud_disable_DRhandler() end
B738DR_hud_disable		= create_dataref("laminar/B738/hud_disable", "number", hud_disable_DRhandler)

function kill_led_lights_DRhandler() end
B738DR_kill_led_lights	= create_dataref("laminar/B738/kill_led_lights", "number", kill_led_lights_DRhandler)

function yoke_disco_ap_DRhandler() end
B738DR_yoke_disco_ap	= create_dataref("laminar/B738/yoke_disco_ap", "number", yoke_disco_ap_DRhandler)

function yoke_to_center_DRhandler() end
B738DR_yoke_to_center	= create_dataref("laminar/B738/yoke_to_center", "number", yoke_to_center_DRhandler)

function parkbrake_set_DRhandler() end
B738DR_parkbrake_set	= create_dataref("laminar/B738/parkbrake_set", "number", parkbrake_set_DRhandler)

function brunner_enable_DRhandler() end
B738DR_brunner_enable	= create_dataref("laminar/B738/brunner_enable", "number", brunner_enable_DRhandler)

function xhsi_enable_DRhandler() end
B738DR_xhsi_enable		= create_dataref("laminar/B738/xhsi_enable", "number", xhsi_enable_DRhandler)

function com_decimal_DRhandler() end
B738DR_com_decimal		= create_dataref("laminar/B738/effects/com_decimal", "number", com_decimal_DRhandler)

function smart_ap_knob_DRhandler() end
B738DR_smart_ap_knob	= create_dataref("laminar/B738/effects/smart_ap_knob", "number", smart_ap_knob_DRhandler)

function restrict_fms_DRhandler() end
B738DR_restrict_fms		= create_dataref("laminar/B738/fms/restrict_fms", "number", restrict_fms_DRhandler)

function cfy_enable_DRhandler() end
B738DR_cfy_enable		= create_dataref("laminar/B738/cfy_enable", "number", cfy_enable_DRhandler)

function cfy_trim_control_DRhandler() end
B738DR_cfy_trim_control		= create_dataref("laminar/B738/cfy_trim_control", "number", cfy_trim_control_DRhandler)

function sam_enable_DRhandler() end
B738DR_sam_enable		= create_dataref("laminar/B738/sam_enable", "number", sam_enable_DRhandler)

function ext_payload_DRhandler() end
B738DR_ext_payload		= create_dataref("laminar/B738/ext_payload", "number", ext_payload_DRhandler)

function eicas_full_n1_DRhandler() end
B738DR_eicas_full_n1		= create_dataref("laminar/B738/eicas_full_n1", "number", eicas_full_n1_DRhandler)

function eicas_n1_num_DRhandler() end
B738DR_eicas_n1_num		= create_dataref("laminar/B738/eicas_n1_num", "number", eicas_n1_num_DRhandler)

function single_cue_fd_DRhandler() end
B738DR_single_cue_fd		= create_dataref("laminar/B738/single_cue_fd", "number", single_cue_fd_DRhandler)

function mmr_DRhandler() end
B738DR_mmr				= create_dataref("laminar/B738/fms/mmr", "number", mmr_DRhandler)

function mmr_colour_DRhandler() end
B738DR_mmr_colour		= create_dataref("laminar/B738/fms/mmr_colour", "number", mmr_colour_DRhandler)

function ils_gls_annun_DRhandler() end
B738DR_ils_gls_annun	= create_dataref("laminar/B738/fms/ils_gls_annun", "number", ils_gls_annun_DRhandler)

function no_smoking_DRhandler() end
B738DR_no_smoking	 			= create_dataref("laminar/B738/fms/no_smoking", "number", no_smoking_DRhandler)

function no_vcr_switch_DRhandler() end
B738DR_no_vcr_switch			= create_dataref("laminar/B738/fms/no_vcr_switch", "number", no_vcr_switch_DRhandler)

function gw_entry_DRhandler() end
B738DR_gw_entry					= create_dataref("laminar/B738/fms/gw_entry", "number", gw_entry_DRhandler)

function aspirated_probe_DRhandler() end
B738DR_aspirated_probe	= create_dataref("laminar/B738/fms/aspirated_probe", "number", aspirated_probe_DRhandler)

function ra_callout_disable_DRhandler() end
B738DR_ra_callout_disable	= create_dataref("laminar/B738/fms/ra_callout_disable", "array[15]", ra_callout_disable_DRhandler)

function passengers_hide_DRhandler() end
B738DR_passengers_hide	= create_dataref("laminar/B738/effects/passengers_hide", "number", passengers_hide_DRhandler)

function B738DR_gpu_test_enable_DRhandler() end
B738DR_gpu_test_enable			= create_dataref("laminar/B738/fms/gpu_test_enable", "number", B738DR_gpu_test_enable_DRhandler)

B738DR_disable_fo_callout 		= find_dataref("laminar/b738/fmodpack/disable_fo_callout")

function B738DR_fo_callout_var_DRhandler() end
B738DR_fo_callout_var	= create_dataref("laminar/b738/fmodpack/fo_callout_var", "number", B738DR_fo_callout_var_DRhandler)

function B738DR_vnav_alt_disable_DRhandler() end
B738DR_vnav_alt_disable			= create_dataref("laminar/B738/fms/vnav_alt_disable", "number", B738DR_vnav_alt_disable_DRhandler)

function B738DR_vnavalt_althld_DRhandler() end
B738DR_vnavalt_althld			= create_dataref("laminar/B738/fms/vnavalt_althld", "number", B738DR_vnavalt_althld_DRhandler)

function B738DR_fd_cam_disable_DRhandl() end
B738DR_fd_cam_disable		= create_dataref("laminar/B738/fd_cam_disable", "number", B738DR_fd_cam_disable_DRhandl)

function B738DR_selcal_disable_DRhandl() end
B738DR_selcal_disable		= create_dataref("laminar/B738/selcal_disable", "number", B738DR_selcal_disable_DRhandl)

function B738DR_isfd_rmi_DRhandl() end
B738DR_isfd_rmi		= create_dataref("laminar/B738/isfd_rmi", "number", B738DR_isfd_rmi_DRhandl)

function B738DR_new_fuel_cutoff_DRhandl() end
B738DR_new_fuel_cutoff		= create_dataref("laminar/B738/new_fuel_cutoff", "number", B738DR_new_fuel_cutoff_DRhandl)

function B738DR_nps_DRhandl() end
B738DR_nps		= create_dataref("laminar/B738/nps", "number", B738DR_nps_DRhandl)

function B738DR_yaw_damper_DRhandl() end
B738DR_yaw_damper		= create_dataref("laminar/B738/yaw_damper", "number", B738DR_yaw_damper_DRhandl)

function B738DR_spd_intv_but_DRhandl() end
B738DR_spd_intv_but		= create_dataref("laminar/B738/spd_intv_but", "number", B738DR_spd_intv_but_DRhandl)

function B738DR_alt_intv_but_DRhandl() end
B738DR_alt_intv_but		= create_dataref("laminar/B738/alt_intv_but", "number", B738DR_alt_intv_but_DRhandl)

function B738DR_rmi_DRhandl() end
B738DR_rmi		= create_dataref("laminar/B738/rmi", "number", B738DR_rmi_DRhandl)

function B738DR_sfp_DRhandl() end
B738DR_sfp		= create_dataref("laminar/B738/sfp", "number", B738DR_sfp_DRhandl)

function B738DR_at_wakeup_DRhandl() end
B738DR_at_wakeup		= create_dataref("laminar/B738/pfd/at_wakeup", "number", B738DR_at_wakeup_DRhandl)

function B738DR_plan_fuel_handl() end
B738DR_plan_fuel		= create_dataref("laminar/B738/plan_fuel", "number", B738DR_plan_fuel_handl)

function B738DR_oil_qty_handl() end
B738DR_oil_qty_pct_qts		= create_dataref("laminar/B738/oil_qty_pct_qts", "number", B738DR_oil_qty_handl)

function B738DR_vnav_wakeup_idle_handl() end
B738DR_vnav_wakeup_idle		= create_dataref("laminar/B738/vnav_wakeup_idle", "number", B738DR_vnav_wakeup_idle_handl)

function B738DR_nd_arpt_rw_lenght_handl() end
B738DR_nd_arpt_rw_lenght	= create_dataref("laminar/B738/nd_arpt_rw_lenght", "number", B738DR_nd_arpt_rw_lenght_handl)

function B738DR_guarded_covers_handl() end
B738DR_guarded_covers	= create_dataref("laminar/B738/guarded_covers", "number", B738DR_guarded_covers_handl)

function B738DR_acp_DRhandl() end
B738DR_acp		= create_dataref("laminar/B738/acp", "number", B738DR_acp_DRhandl)

function B738DR_vanish_y_DRhandl() end
B738DR_vanish_y		= create_dataref("laminar/B738/vanish_y", "number", B738DR_vanish_y_DRhandl)

function B738DR_mute_switches_DRhandl() end
B738DR_mute_switches		= create_dataref("laminar/B738/mute_switches", "number", B738DR_mute_switches_DRhandl)

function B738DR_clock_variant_DRhandl() end
B738DR_clock_variant		= create_dataref("laminar/B738/clock_variant", "number", B738DR_clock_variant_DRhandl)

function B738DR_old_mcp_DRhandl() end
B738DR_old_mcp		= create_dataref("laminar/B738/old_mcp", "number", B738DR_old_mcp_DRhandl)

function B738DR_def_clb_alt_DRhandl() end
B738DR_def_clb_alt		= create_dataref("laminar/B738/def_clb_alt", "number", B738DR_def_clb_alt_DRhandl)

function B738DR_def_clb_alt2_DRhandl() end
B738DR_def_clb_alt2		= create_dataref("laminar/B738/def_clb_alt2", "number", B738DR_def_clb_alt2_DRhandl)

function B738DR_def_cut_alt_DRhandl() end
B738DR_def_cut_alt		= create_dataref("laminar/B738/def_cut_alt", "number", B738DR_def_cut_alt_DRhandl)

function B738DR_def_accel_alt_DRhandl() end
B738DR_def_accel_alt		= create_dataref("laminar/B738/def_accel_alt", "number", B738DR_def_accel_alt_DRhandl)

function B738DR_def_eo_accel_alt_DRhandl() end
B738DR_def_eo_accel_alt		= create_dataref("laminar/B738/def_eo_accel_alt", "number", B738DR_def_eo_accel_alt_DRhandl)

function B738DR_plus_hundred_DRhandl() end
B738DR_plus_hundred			= create_dataref("laminar/B738/plus_hundred", "number", B738DR_plus_hundred_DRhandl)

function B738DR_ra_radio_altimeter_DRhandl() end
B738DR_ra_radio_altimeter			= create_dataref("laminar/B738/ra_radio_altimeter", "number", B738DR_ra_radio_altimeter_DRhandl)

function B738DR_ra_decision_height_DRhandl() end
B738DR_ra_decision_height			= create_dataref("laminar/B738/ra_decision_height", "number", B738DR_ra_decision_height_DRhandl)

function B738DR_ra_minimums_DRhandl() end
B738DR_ra_minimums			= create_dataref("laminar/B738/ra_minimums", "number", B738DR_ra_minimums_DRhandl)

function B738DR_ra_500_1000_baro_DRhandl() end
B738DR_ra_500_1000_baro			= create_dataref("laminar/B738/ra_500_1000_baro", "number", B738DR_ra_500_1000_baro_DRhandl)

function B738DR_missed_app_colour_DRhandl() end
B738DR_missed_app_colour		= create_dataref("laminar/B738/missed_app_colour", "number", B738DR_missed_app_colour_DRhandl)

function B738DR_high_alt_ldg_DRhandl() end
B738DR_high_alt_ldg		= create_dataref("laminar/B738/high_alt_ldg", "number", B738DR_high_alt_ldg_DRhandl)

function B738DR_vorloc_lnav_DRhandl() end
B738DR_vorloc_lnav			= create_dataref("laminar/B738/vorloc_lnav", "number", B738DR_vorloc_lnav_DRhandl)

function B738DR_fmc_type_DRhandl()
	if B738DR_fmc_type ~= 0 then
		if page_atc ~= 0 or page_fmc_comm ~= 0 then
			page_atc = 0
			page_fmc_comm = 0
			page_menu = 1
		end
	end
end
B738DR_fmc_type				= create_dataref("laminar/B738/fmc_type", "number", B738DR_fmc_type_DRhandl)

function B738DR_raas_DRhandl() end
B738DR_raas					= create_dataref("laminar/B738/raas", "number", B738DR_raas_DRhandl)

function B738DR_not_airstairs_DRhandl() end
B738DR_not_airstairs					= create_dataref("laminar/B738/not_airstairs", "number", B738DR_not_airstairs_DRhandl)

function B738DR_engine_start_auto_DRhandl() end
B738DR_engine_start_auto					= create_dataref("laminar/B738/engine_start_auto", "number", B738DR_engine_start_auto_DRhandl)

function B738DR_probe_heat_auto_DRhandl() end
B738DR_probe_heat_auto					= create_dataref("laminar/B738/probe_heat_auto", "number", B738DR_probe_heat_auto_DRhandl)

function B738DR_autoland_install_DRhandl() end
B738DR_autoland_install				= create_dataref("laminar/B738/autoland_install", "number", B738DR_autoland_install_DRhandl)

function B738DR_nps_disable_DRhandl() end
B738DR_nps_disable				= create_dataref("laminar/B738/nps_disable", "number", B738DR_nps_disable_DRhandl)

function B738DR_ian_not_install_DRhandl() end
B738DR_ian_not_install				= create_dataref("laminar/B738/ian_install", "number", B738DR_ian_not_install_DRhandl)

function B738DR_fuel_pumps_off_placard_DRhandl() end
B738DR_fuel_pumps_off_placard		= create_dataref("laminar/B738/fuel_pumps_off_placard", "number", B738DR_fuel_pumps_off_placard_DRhandl)

function B738DR_cat3_placard_DRhandl() end
B738DR_cat3_placard		= create_dataref("laminar/B738/cat3_placard", "number", B738DR_cat3_placard_DRhandl)

function B738DR_brakes_placard_DRhandl() end
B738DR_brakes_placard		= create_dataref("laminar/B738/brakes_placard", "number", B738DR_brakes_placard_DRhandl)


function B738DR_yd_installed_DRhandl() end
B738DR_yd_installed			= create_dataref("laminar/B738/yd_installed", "number", B738DR_yd_installed_DRhandl)

B738DR_vorloc_lnav_enable	= create_dataref("laminar/B738/vorloc_lnav_enable", "number")

function B738DR_manip_yoke_DRhandl() end
B738DR_manip_yoke		= create_dataref("laminar/B738/manip_yoke", "number", B738DR_manip_yoke_DRhandl)

function B738DR_manip_throttle_DRhandl() end
B738DR_manip_throttle		= create_dataref("laminar/B738/manip_throttle", "number", B738DR_manip_throttle_DRhandl)

function B738DR_manip_flaps_DRhandl() end
B738DR_manip_flaps		= create_dataref("laminar/B738/manip_flaps", "number", B738DR_manip_flaps_DRhandl)

function B738DR_manip_speedbrake_DRhandl() end
B738DR_manip_speedbrake		= create_dataref("laminar/B738/manip_speedbrake", "number", B738DR_manip_speedbrake_DRhandl)

function B738DR_manip_trimwheel_DRhandl() end
B738DR_manip_trimwheel		= create_dataref("laminar/B738/manip_trimwheel", "number", B738DR_manip_trimwheel_DRhandl)

function B738DR_manip_park_brake_DRhandl() end
B738DR_manip_park_brake		= create_dataref("laminar/B738/manip_park_brake", "number", B738DR_manip_park_brake_DRhandl)

function B738DR_manip_stab_trim_DRhandl() end
B738DR_manip_stab_trim		= create_dataref("laminar/B738/manip_stab_trim", "number", B738DR_manip_stab_trim_DRhandl)

function B738DR_manip_fuelcutout_DRhandl() end
B738DR_manip_fuelcutout		= create_dataref("laminar/B738/manip_fuelcutout", "number", B738DR_manip_fuelcutout_DRhandl)

function B738DR_manip_horn_cutout_DRhandl() end
B738DR_manip_horn_cutout		= create_dataref("laminar/B738/manip_horn_cutout", "number", B738DR_manip_horn_cutout_DRhandl)

function B738DR_autosave_DRhandl() end
B738DR_autosave			= create_dataref("laminar/B738/autosave", "number", B738DR_autosave_DRhandl)

function B738DR_ff_eicas_DRhandl() end
B738DR_ff_eicas			= create_dataref("laminar/B738/ff_eicas", "number", B738DR_ff_eicas_DRhandl)

function B738DR_digital_press_panel_DRhandl() end
B738DR_digital_press_panel		= create_dataref("laminar/B738/digital_press_panel", "number", B738DR_digital_press_panel_DRhandl)

function B738DR_no_rte2_DRhandl() end
B738DR_no_rte2					= create_dataref("laminar/B738/no_rte2", "number", B738DR_no_rte2_DRhandl)

function B738DR_gs_on_pfd_DRhandl() end
B738DR_gs_on_pfd				= create_dataref("laminar/B738/gs_on_pfd", "number", B738DR_gs_on_pfd_DRhandl)

function B738DR_autoload_on_DRhandl() end
B738DR_autoload_on		= create_dataref("laminar/B738/autoload_on", "number", B738DR_autoload_on_DRhandl)

function B738DR_eyebrow_kill_handl() end
function B738DR_no_eyebrow_kill_handl() end
B738DR_eyebrow_kill		= create_dataref("laminar/B738/tab/eyebrow_kill", "number", B738DR_eyebrow_kill_handl)
B738DR_no_eyebrow_kill		= create_dataref("laminar/B738/tab/no_eyebrow_kill", "number", B738DR_no_eyebrow_kill_handl)

function B738DR_aft_exit_kill_handl() end
function B738DR_no_aft_exit_kill_handl() end
B738DR_aft_exit_kill		= create_dataref("laminar/B738/tab/aft_exit_kill", "number", B738DR_aft_exit_kill_handl)
B738DR_no_aft_exit_kill		= create_dataref("laminar/B738/tab/no_aft_exit_kill", "number", B738DR_no_aft_exit_kill_handl)

function B738DR_pip_kill_handl() end
function B738DR_no_pip_kill_handl() end
B738DR_pip_kill		= create_dataref("laminar/B738/tab/pip_kill", "number", B738DR_pip_kill_handl)
B738DR_no_pip_kill	= create_dataref("laminar/B738/tab/no_pip_kill", "number", B738DR_no_pip_kill_handl)

function B738DR_fuel_cutoff_sw_handl() end
B738DR_fuel_cutoff_sw		= create_dataref("laminar/B738/fuel_cutoff_switches", "number", B738DR_fuel_cutoff_sw_handl)

function B738DR_efb_colorize_handl() end
B738DR_efb_colorize		= create_dataref("laminar/B738/tab/efb_colorize", "number", B738DR_efb_colorize_handl)

function B738DR_hyd_compensator_handl() end
B738DR_hyd_compensator		= create_dataref("laminar/B738/hyd_compensator_cartidge", "number", B738DR_hyd_compensator_handl)

function B738DR_ctr_fuel_pump_auto_handl() end
B738DR_ctr_fuel_pump_auto		= create_dataref("laminar/B738/ctr_fuel_pump_auto", "number", B738DR_ctr_fuel_pump_auto_handl)

function B738DR_vnav_common_handl() end
B738DR_vnav_common		= create_dataref("laminar/B738/vnav_common", "number", B738DR_vnav_common_handl)

-- FMOD by AudioBird XP
B738DR_enable_pax_boarding	= find_dataref("laminar/b738/fmodpack/fmod_pax_boarding_on")
B738DR_enable_gyro			= find_dataref("laminar/b738/fmodpack/fmod_woodpecker_on")
B738DR_enable_crew			= find_dataref("laminar/b738/fmodpack/fmod_crew_on")
B738DR_enable_chatter		= find_dataref("laminar/b738/fmodpack/fmod_chatter_on")
B738DR_airport_set 			= find_dataref("laminar/b738/fmodpack/fmod_airport_set")
B738DR_vol_int_ducker 		= find_dataref("laminar/b738/fmodpack/fmod_vol_int_ducker")
B738DR_vol_int_eng 			= find_dataref("laminar/b738/fmodpack/fmod_vol_int_eng")
B738DR_vol_int_start 		= find_dataref("laminar/b738/fmodpack/fmod_vol_int_start")
B738DR_vol_int_ac 			= find_dataref("laminar/b738/fmodpack/fmod_vol_int_ac")
B738DR_vol_int_gyro 		= find_dataref("laminar/b738/fmodpack/fmod_vol_int_gyro")
B738DR_vol_int_roll 		= find_dataref("laminar/b738/fmodpack/fmod_vol_int_roll")
B738DR_vol_int_bump 		= find_dataref("laminar/b738/fmodpack/fmod_vol_int_bump")
B738DR_vol_int_pax 			= find_dataref("laminar/b738/fmodpack/fmod_vol_int_pax")
B738DR_vol_int_pax_applause = find_dataref("laminar/b738/fmodpack/fmod_pax_applause_on")
B738DR_enable_mutetrim		= find_dataref("laminar/b738/fmodpack/fmod_mutetrim_on")
B738DR_vol_airport			= find_dataref("laminar/b738/fmodpack/fmod_vol_airport")
B738DR_xp_int_vol			= create_dataref("laminar/b738/fmodpack/fmod_xp_int_vol", "number")
B738DR_vol_int_XP 			= find_dataref("sim/operation/sound/interior_volume_ratio")

B738DR_enable_fmc_mute_on 	= find_dataref("laminar/b738/fmodpack/fmod_enable_fmc_mute_on")
B738DR_vol_PM 				= find_dataref("laminar/b738/fmodpack/fmod_vol_PM")
B738DR_vol_int_wind_vol 	= find_dataref("laminar/b738/fmodpack/fmod_vol_int_wind")
B738DR_vol_crew 			= find_dataref("laminar/b738/fmodpack/fmod_vol_crew")
B738DR_announcement_set 	= find_dataref("laminar/b738/fmodpack/fmod_announcement_set")
play_welcome_msg 			= find_dataref("laminar/b738/fmodpack/play_welcome_msg") 
play_cruise_msg 			= find_dataref("laminar/b738/fmodpack/play_cruise_msg") 
play_descent_msg 			= find_dataref("laminar/b738/fmodpack/play_descent_msg")
play_preland_msg 			= find_dataref("laminar/b738/fmodpack/play_preland_msg")
play_turbulence_msg 		= find_dataref("laminar/b738/fmodpack/play_turbulence_msg")
eq_high 					= find_dataref("laminar/b738/fmodpack/fmod_eq_high")
eq_mid 						= find_dataref("laminar/b738/fmodpack/fmod_eq_mid")
eq_low 						= find_dataref("laminar/b738/fmodpack/fmod_eq_low")
start_leg 					= find_dataref("laminar/b738/fmodpack/fmod_start_leg")
end_leg 					= find_dataref("laminar/b738/fmodpack/fmod_end_leg")
play_cargo 					= find_dataref("laminar/b738/fmodpack/fmod_play_cargo")
B738DR_vol_computer 		= find_dataref("laminar/b738/fmodpack/fmod_vol_computer")

B738DR_vol_FAC 				= find_dataref("laminar/b738/fmodpack/fmod_vol_FAC")
B738DR_vol_weather 			= find_dataref("laminar/b738/fmodpack/fmod_vol_weather")
B738DR_TO_trigger 			= find_dataref("laminar/b738/fmodpack/TO_trigger")
B738DR_vol_int_trim 		= find_dataref("laminar/b738/fmodpack/fmod_vol_int_trim")

B738DR_pa_announc_set		= create_dataref("laminar/b738/fmodpack/pa_announ_set", "number")

custom_ils_status			= create_dataref("laminar/b738/custom_navdata_status", "number")
vanish_y_status				= create_dataref("laminar/b738/vanish_y_status", "number")
-------
B738DR_ap_goaround 						= create_dataref("laminar/B738/autopilot/ap_goaround", "number")
B738DR_fd_goaround 						= create_dataref("laminar/B738/autopilot/fd_goaround", "number")

B738DR_ap_on				= create_dataref("laminar/autopilot/ap_on", "number")

B738DR_joy_axis_flap_lever	= find_dataref("laminar/B738/axis/flap_lever")
B738DR_joy_axis_heading 	= find_dataref("laminar/B738/axis/heading")
B738DR_joy_axis_pitch 		= find_dataref("laminar/B738/axis/pitch")
B738DR_joy_axis_roll 		= find_dataref("laminar/B738/axis/roll")

B738DR_apu_power_bus1		= find_dataref("laminar/B738/electrical/apu_power_bus1")
B738DR_apu_power_bus2		= find_dataref("laminar/B738/electrical/apu_power_bus2")

function B738DR_course_pilot_handl()
	local course = B738DR_course_pilot
	while course < 0 do
		course = course + 360
	end
	course = (course + 360) % 360
	course = math.floor(course + 0.5)
	course = math.min(course, 360)
	B738DR_course_pilot = course
end

function B738DR_course_copilot_handl()
	local course = B738DR_course_copilot
	while course < 0 do
		course = course + 360
	end
	course = (course + 360) % 360
	course = math.floor(course + 0.5)
	course = math.min(course, 360)
	B738DR_course_copilot = course
end

B738DR_course_pilot			= create_dataref("laminar/B738/autopilot/course_pilot", "number", B738DR_course_pilot_handl)
B738DR_course_copilot		= create_dataref("laminar/B738/autopilot/course_copilot", "number", B738DR_course_copilot_handl)

B738DR_eicas_full_n1_show	= create_dataref("laminar/B738/eicas_full_n1_show", "array[2]")

simDR_reverse_deploy		= find_dataref("sim/flightmodel2/engines/thrust_reverser_deploy_ratio")


B738DR_filename_list_cmd	= find_dataref("laminar/B738/zibomod/filename_list_cmd")
B738DR_filename_list_n		= find_dataref("laminar/B738/zibomod/filename_list_n")
B738DR_filename_list		= find_dataref("laminar/B738/zibomod/filename_list")

--*************************************************************************************--
--** 				               FIND CUSTOM COMMANDS              			     **--
--*************************************************************************************--

B738CMD_fmc_to_irs_pos		= find_command("laminar/B738/push_button/fmc_to_irs_pos")
B738CMD_fmc_to_irs_hdg		= find_command("laminar/B738/push_button/fmc_to_irs_hdg")

-- FMOD by AudioBird XP
B738CMD_enable_pax_boarding = find_command("laminar/b738/fmodpack/pax_board")
B738CMD_enable_gyro 		= find_command("laminar/b738/fmodpack/fmod_woodpecker_on")
B738CMD_enable_crew			= find_command("laminar/b738/fmodpack/fmod_crew_on")
B738CMD_enable_chatter 		= find_command("laminar/b738/fmodpack/pax_talk")
B738CMD_airport_set 		= find_command("laminar/b738/fmodpack/fmod_airport_set")
B738CMD_vol_int_ducker 		= find_command("laminar/b738/fmodpack/fmod_vol_int_ducker")
B738CMD_vol_int_eng 		= find_command("laminar/b738/fmodpack/fmod_vol_int_eng")
B738CMD_vol_int_start 		= find_command("laminar/b738/fmodpack/fmod_vol_int_start")
B738CMD_vol_int_ac 			= find_command("laminar/b738/fmodpack/fmod_vol_int_ac")
B738CMD_vol_int_gyro 		= find_command("laminar/b738/fmodpack/fmod_vol_int_gyro")
B738CMD_vol_int_roll		= find_command("laminar/b738/fmodpack/fmod_vol_int_roll")
B738CMD_vol_int_bump		= find_command("laminar/b738/fmodpack/fmod_vol_int_bump")
B738CMD_vol_int_pax				= find_command("laminar/b738/fmodpack/fmod_vol_int_pax")
B738CMD_vol_int_pax_applause	= find_command("laminar/b738/fmodpack/fmod_pax_applause_on")
B738CMD_vol_int_wind			= find_command("laminar/b738/fmodpack/fmod_vol_int_wind")
B738CMD_enable_mutetrim 		= find_command("laminar/b738/fmodpack/fmod_mutetrim_on")
B738CMD_vol_int_XP			= find_command("laminar/b738/fmodpack/fmod_vol_int_XP")
B738CMD_vol_airport			= find_command("laminar/b738/fmodpack/fmod_vol_airport")
B738CMD_announcement_set 	= find_command("laminar/b738/fmodpack/fmod_announcement_set")

B738_eq_high 				= find_command("laminar/b738/fmodpack/fmod_eq_high")
B738_eq_mid 				= find_command("laminar/b738/fmodpack/fmod_eq_mid")
B738_eq_low 				= find_command("laminar/b738/fmodpack/fmod_eq_low")
B738_end_leg 				= find_command("laminar/b738/fmodpack/fmod_end_leg")
B738_start_leg 				= find_command("laminar/b738/fmodpack/fmod_start_leg")
B738CMD_play_cargo 			= find_command("laminar/b738/fmodpack/fmod_play_cargo")
B738CMD_vol_crew			= find_command("laminar/b738/fmodpack/fmod_vol_crew")	
B738CMD_vol_PM				= find_command("laminar/b738/fmodpack/fmod_vol_PM")	
B738CMD_play_welcome		= find_command("laminar/b738/fmodpack/play_welcome_msg")
B738CMD_play_cruise			= find_command("laminar/b738/fmodpack/play_cruise_msg")
B738CMD_play_descent		= find_command("laminar/b738/fmodpack/play_descent_msg")
B738CMD_play_preland		= find_command("laminar/b738/fmodpack/play_preland_msg")
B738CMD_play_turbulence		= find_command("laminar/b738/fmodpack/play_turbulence_msg")
B738CMD_enable_fmc_mute_on 	= find_command("laminar/b738/fmodpack/fmod_enable_fmc_mute_on")
B738CMD_vol_computer		= find_command("laminar/b738/fmodpack/fmod_vol_computer")

B738CMD_vol_FAC				= find_command("laminar/b738/fmodpack/fmod_vol_FAC")
B738CMD_vol_weather 		= find_command("laminar/b378/fmodpack/fmod_vol_weather")
B738CMD_switch_TO_trigger 	= find_command("laminar/b738/fmodpack/CMD_switch_TO_trigger")
B738CMD_vol_int_trim		= find_command("laminar/b738/fmodpack/fmod_vol_int_trim")



--*************************************************************************************--
--** 				                X-PLANE DATAREFS            			    	 **--
--*************************************************************************************--
function B738DR_yoke_roll_DRhandler() end
function B738DR_yoke_pitch_DRhandler() end

B738DR_yoke_roll			= create_dataref("laminar/yoke/roll", "number", B738DR_yoke_roll_DRhandler)
B738DR_yoke_pitch			= create_dataref("laminar/yoke/pitch", "number", B738DR_yoke_pitch_DRhandler)
simDR_roll_AHARS_deg 			= find_dataref("sim/cockpit2/gauges/indicators/roll_AHARS_deg_pilot")
simDR_ahars_pitch_deg_pilot		= find_dataref("sim/cockpit2/gauges/indicators/pitch_AHARS_deg_pilot")
simDR_fdir_pitch				= find_dataref("sim/cockpit/autopilot/flight_director_pitch")
simDR_fdir_roll					= find_dataref("sim/cockpit/autopilot/flight_director_roll")

simDR_throttle					= find_dataref("sim/cockpit2/engine/actuators/throttle_ratio")
simDR_reverse_act				= find_dataref("sim/cockpit2/engine/actuators/prop_mode")



--*************************************************************************************--
--** 				        CREATE READ-ONLY CUSTOM DATAREFS               	         **--
--*************************************************************************************--
B738DR_freeze						= create_dataref("laminar/B738/fmc_freeze", "number")

-- CAPTAIN FMC
B738DR_fmc1_Line00_L                = create_dataref("laminar/B738/fmc1/Line00_L", "string")
B738DR_fmc1_Line01_L                = create_dataref("laminar/B738/fmc1/Line01_L", "string")
B738DR_fmc1_Line02_L                = create_dataref("laminar/B738/fmc1/Line02_L", "string")
B738DR_fmc1_Line03_L                = create_dataref("laminar/B738/fmc1/Line03_L", "string")
B738DR_fmc1_Line04_L                = create_dataref("laminar/B738/fmc1/Line04_L", "string")
B738DR_fmc1_Line05_L                = create_dataref("laminar/B738/fmc1/Line05_L", "string")
B738DR_fmc1_Line06_L                = create_dataref("laminar/B738/fmc1/Line06_L", "string")

B738DR_fmc1_Line00_S                = create_dataref("laminar/B738/fmc1/Line00_S", "string")
B738DR_fmc1_Line01_S                = create_dataref("laminar/B738/fmc1/Line01_S", "string")
B738DR_fmc1_Line02_S                = create_dataref("laminar/B738/fmc1/Line02_S", "string")
B738DR_fmc1_Line03_S                = create_dataref("laminar/B738/fmc1/Line03_S", "string")
B738DR_fmc1_Line04_S                = create_dataref("laminar/B738/fmc1/Line04_S", "string")
B738DR_fmc1_Line05_S                = create_dataref("laminar/B738/fmc1/Line05_S", "string")
B738DR_fmc1_Line06_S                = create_dataref("laminar/B738/fmc1/Line06_S", "string")

B738DR_fmc1_Line01_X                = create_dataref("laminar/B738/fmc1/Line01_X", "string")
B738DR_fmc1_Line02_X                = create_dataref("laminar/B738/fmc1/Line02_X", "string")
B738DR_fmc1_Line03_X                = create_dataref("laminar/B738/fmc1/Line03_X", "string")
B738DR_fmc1_Line04_X                = create_dataref("laminar/B738/fmc1/Line04_X", "string")
B738DR_fmc1_Line05_X                = create_dataref("laminar/B738/fmc1/Line05_X", "string")
B738DR_fmc1_Line06_X                = create_dataref("laminar/B738/fmc1/Line06_X", "string")

B738DR_fmc1_Line00_I                = create_dataref("laminar/B738/fmc1/Line00_I", "string")
B738DR_fmc1_Line01_I                = create_dataref("laminar/B738/fmc1/Line01_I", "string")
B738DR_fmc1_Line02_I                = create_dataref("laminar/B738/fmc1/Line02_I", "string")
B738DR_fmc1_Line03_I                = create_dataref("laminar/B738/fmc1/Line03_I", "string")
B738DR_fmc1_Line04_I                = create_dataref("laminar/B738/fmc1/Line04_I", "string")
B738DR_fmc1_Line05_I                = create_dataref("laminar/B738/fmc1/Line05_I", "string")
B738DR_fmc1_Line06_I                = create_dataref("laminar/B738/fmc1/Line06_I", "string")

B738DR_fmc1_Line00_M                = create_dataref("laminar/B738/fmc1/Line00_M", "string")
B738DR_fmc1_Line01_M                = create_dataref("laminar/B738/fmc1/Line01_M", "string")
B738DR_fmc1_Line02_M                = create_dataref("laminar/B738/fmc1/Line02_M", "string")
B738DR_fmc1_Line03_M                = create_dataref("laminar/B738/fmc1/Line03_M", "string")
B738DR_fmc1_Line04_M                = create_dataref("laminar/B738/fmc1/Line04_M", "string")
B738DR_fmc1_Line05_M                = create_dataref("laminar/B738/fmc1/Line05_M", "string")
B738DR_fmc1_Line06_M                = create_dataref("laminar/B738/fmc1/Line06_M", "string")

B738DR_fmc1_Line00_G                = create_dataref("laminar/B738/fmc1/Line00_G", "string")
B738DR_fmc1_Line01_G                = create_dataref("laminar/B738/fmc1/Line01_G", "string")
B738DR_fmc1_Line02_G                = create_dataref("laminar/B738/fmc1/Line02_G", "string")
B738DR_fmc1_Line03_G                = create_dataref("laminar/B738/fmc1/Line03_G", "string")
B738DR_fmc1_Line04_G                = create_dataref("laminar/B738/fmc1/Line04_G", "string")
B738DR_fmc1_Line05_G                = create_dataref("laminar/B738/fmc1/Line05_G", "string")
B738DR_fmc1_Line06_G                = create_dataref("laminar/B738/fmc1/Line06_G", "string")

B738DR_fmc1_Line00_C                = create_dataref("laminar/B738/fmc1/Line00_C", "string")

B738DR_fmc1_Line04_SI               = create_dataref("laminar/B738/fmc1/Line04_SI", "string")

B738DR_fmc1_Line01_LX               = create_dataref("laminar/B738/fmc1/Line01_LX", "string")
B738DR_fmc1_Line06_LX               = create_dataref("laminar/B738/fmc1/Line06_LX", "string")

B738DR_fmc1_Line_entry              = create_dataref("laminar/B738/fmc1/Line_entry", "string")
B738DR_fmc1_Line_entry_I            = create_dataref("laminar/B738/fmc1/Line_entry_I", "string")

-- FIRST OFFICER FMC
B738DR_fmc2_Line00_L                = create_dataref("laminar/B738/fmc2/Line00_L", "string")
B738DR_fmc2_Line01_L                = create_dataref("laminar/B738/fmc2/Line01_L", "string")
B738DR_fmc2_Line02_L                = create_dataref("laminar/B738/fmc2/Line02_L", "string")
B738DR_fmc2_Line03_L                = create_dataref("laminar/B738/fmc2/Line03_L", "string")
B738DR_fmc2_Line04_L                = create_dataref("laminar/B738/fmc2/Line04_L", "string")
B738DR_fmc2_Line05_L                = create_dataref("laminar/B738/fmc2/Line05_L", "string")
B738DR_fmc2_Line06_L                = create_dataref("laminar/B738/fmc2/Line06_L", "string")

B738DR_fmc2_Line00_S                = create_dataref("laminar/B738/fmc2/Line00_S", "string")
B738DR_fmc2_Line01_S                = create_dataref("laminar/B738/fmc2/Line01_S", "string")
B738DR_fmc2_Line02_S                = create_dataref("laminar/B738/fmc2/Line02_S", "string")
B738DR_fmc2_Line03_S                = create_dataref("laminar/B738/fmc2/Line03_S", "string")
B738DR_fmc2_Line04_S                = create_dataref("laminar/B738/fmc2/Line04_S", "string")
B738DR_fmc2_Line05_S                = create_dataref("laminar/B738/fmc2/Line05_S", "string")
B738DR_fmc2_Line06_S                = create_dataref("laminar/B738/fmc2/Line06_S", "string")

B738DR_fmc2_Line01_X                = create_dataref("laminar/B738/fmc2/Line01_X", "string")
B738DR_fmc2_Line02_X                = create_dataref("laminar/B738/fmc2/Line02_X", "string")
B738DR_fmc2_Line03_X                = create_dataref("laminar/B738/fmc2/Line03_X", "string")
B738DR_fmc2_Line04_X                = create_dataref("laminar/B738/fmc2/Line04_X", "string")
B738DR_fmc2_Line05_X                = create_dataref("laminar/B738/fmc2/Line05_X", "string")
B738DR_fmc2_Line06_X                = create_dataref("laminar/B738/fmc2/Line06_X", "string")

B738DR_fmc2_Line00_I                = create_dataref("laminar/B738/fmc2/Line00_I", "string")
B738DR_fmc2_Line01_I                = create_dataref("laminar/B738/fmc2/Line01_I", "string")
B738DR_fmc2_Line02_I                = create_dataref("laminar/B738/fmc2/Line02_I", "string")
B738DR_fmc2_Line03_I                = create_dataref("laminar/B738/fmc2/Line03_I", "string")
B738DR_fmc2_Line04_I                = create_dataref("laminar/B738/fmc2/Line04_I", "string")
B738DR_fmc2_Line05_I                = create_dataref("laminar/B738/fmc2/Line05_I", "string")
B738DR_fmc2_Line06_I                = create_dataref("laminar/B738/fmc2/Line06_I", "string")

B738DR_fmc2_Line00_M                = create_dataref("laminar/B738/fmc2/Line00_M", "string")
B738DR_fmc2_Line01_M                = create_dataref("laminar/B738/fmc2/Line01_M", "string")
B738DR_fmc2_Line02_M                = create_dataref("laminar/B738/fmc2/Line02_M", "string")
B738DR_fmc2_Line03_M                = create_dataref("laminar/B738/fmc2/Line03_M", "string")
B738DR_fmc2_Line04_M                = create_dataref("laminar/B738/fmc2/Line04_M", "string")
B738DR_fmc2_Line05_M                = create_dataref("laminar/B738/fmc2/Line05_M", "string")
B738DR_fmc2_Line06_M                = create_dataref("laminar/B738/fmc2/Line06_M", "string")

B738DR_fmc2_Line00_G                = create_dataref("laminar/B738/fmc2/Line00_G", "string")
B738DR_fmc2_Line01_G                = create_dataref("laminar/B738/fmc2/Line01_G", "string")
B738DR_fmc2_Line02_G                = create_dataref("laminar/B738/fmc2/Line02_G", "string")
B738DR_fmc2_Line03_G                = create_dataref("laminar/B738/fmc2/Line03_G", "string")
B738DR_fmc2_Line04_G                = create_dataref("laminar/B738/fmc2/Line04_G", "string")
B738DR_fmc2_Line05_G                = create_dataref("laminar/B738/fmc2/Line05_G", "string")
B738DR_fmc2_Line06_G                = create_dataref("laminar/B738/fmc2/Line06_G", "string")

B738DR_fmc2_Line00_C                = create_dataref("laminar/B738/fmc2/Line00_C", "string")

B738DR_fmc2_Line04_SI               = create_dataref("laminar/B738/fmc2/Line04_SI", "string")

B738DR_fmc2_Line01_LX               = create_dataref("laminar/B738/fmc2/Line01_LX", "string")
B738DR_fmc2_Line06_LX               = create_dataref("laminar/B738/fmc2/Line06_LX", "string")

B738DR_fmc2_Line_entry              = create_dataref("laminar/B738/fmc2/Line_entry", "string")
B738DR_fmc2_Line_entry_I            = create_dataref("laminar/B738/fmc2/Line_entry_I", "string")

B738DR_rel 					= create_dataref("laminar/B738/release", "string")
B738DR_fmod_rel 			= create_dataref("laminar/B738/fmod_release", "string")
B738DR_fm_rel 				= create_dataref("laminar/B738/fm_release", "string")

function near_apt_icao_handl() end
B738DR_near_apt_icao 		= create_dataref("laminar/B738/near_apt_icao", "string", near_apt_icao_handl)

function nearest_apt_icao_handl() end
function nearest_apt_dist_handl() end
function nearest_apt_lat_handl() end
function nearest_apt_lon_handl() end
B738DR_nearest_apt_icao 	= create_dataref("laminar/B738/nearest_apt_icao", "string", nearest_apt_icao_handl)
B738DR_nearest_apt_dist		= create_dataref("laminar/B738/nearest_apt_dist", "array[5]", nearest_apt_dist_handl)
B738DR_nearest_apt_lat		= create_dataref("laminar/B738/nearest_apt_lat", "array[5]", nearest_apt_lat_handl)
B738DR_nearest_apt_lon		= create_dataref("laminar/B738/nearest_apt_lon", "array[5]", nearest_apt_lon_handl)

B738DR_fms_v1_set			= create_dataref("laminar/B738/FMS/v1_set", "number")
B738DR_fms_vr_set			= create_dataref("laminar/B738/FMS/vr_set", "number")
B738DR_fms_v2_set			= create_dataref("laminar/B738/FMS/v2_set", "number")

B738DR_calc_spd_enable		= create_dataref("laminar/B738/FMS/calc_spd_enable", "number")
B738DR_fmc_gw				= create_dataref("laminar/B738/FMS/fmc_gw", "number")
B738DR_fmc_gw_app			= create_dataref("laminar/B738/FMS/fmc_gw_app", "number")
B738DR_irs_pos_fmc			= create_dataref("laminar/B738/FMS/irs_pos_fmc", "number")

B738DR_irs_hdg_fmc			= create_dataref("laminar/B738/FMS/irs_hdg_fmc", "number")
B738DR_irs_hdg_fmc_set		= create_dataref("laminar/B738/FMS/irs_hdg_fmc_set", "string")
B738DR_irs2_hdg_fmc_set		= create_dataref("laminar/B738/FMS/irs2_hdg_fmc_set", "string")
B738DR_irs_pos_fmc_set		= create_dataref("laminar/B738/FMS/irs_pos_fmc_set", "string")
B738DR_irs2_pos_fmc_set		= create_dataref("laminar/B738/FMS/irs2_pos_fmc_set", "string")
B738DR_last_pos_str			= create_dataref("laminar/B738/FMS/last_pos_str", "string")

function fmc_units_handl() end
B738DR_fmc_units			= create_dataref("laminar/B738/FMS/fmc_units", "number", fmc_units_handl)

B738DR_fmc_cg				= create_dataref("laminar/B738/FMS/fmc_cg", "number")
B738DR_fmc_sel_temp			= create_dataref("laminar/B738/FMS/fmc_sel_temp", "number")
B738DR_fmc_oat_temp			= create_dataref("laminar/B738/FMS/fmc_oat_temp", "number")

B738DR_fmc_rw_cond			= create_dataref("laminar/B738/FMS/fmc_rw_cond", "number")

function fms_N1_thrust_handle() end
B738DR_fms_N1_thrust		= create_dataref("laminar/B738/FMS/N1_mode_thrust", "array[2]", fms_N1_thrust_handle)
B738DR_fms_N1_full_thrust	= create_dataref("laminar/B738/FMS/N1_mode_full_thrust", "array[2]")
B738DR_fms_N1_mode			= create_dataref("laminar/B738/FMS/N1_mode", "number")
B738DR_fms_N1_to_sel		= create_dataref("laminar/B738/FMS/N1_mode_to_sel", "number")

function B738DR_flight_phase_DRhandler() end
B738DR_flight_phase			= create_dataref("laminar/B738/FMS/flight_phase", "number", B738DR_flight_phase_DRhandler)

B738DR_aerodrom_altitude	= create_dataref("laminar/B738/FMS/aerodrom_altitude", "number")
B738DR_lift_timer			= create_dataref("laminar/B738/FMS/lift_timer", "number")

B738DR_climb_mode			= create_dataref("laminar/B738/FMS/climb_mode", "number")
B738DR_cruise_mode			= create_dataref("laminar/B738/FMS/cruise_mode", "number")
B738DR_descent_mode			= create_dataref("laminar/B738/FMS/descent_mode", "number")

B738DR_fms_exec_light_pilot		= create_dataref("laminar/B738/indicators/fms_exec_light_pilot", "number")
B738DR_fms_exec_light_copilot	= create_dataref("laminar/B738/indicators/fms_exec_light_copilot", "number")
-- new --
B738DR_fmc_exec_lights 			= create_dataref("laminar/B738/fmod/fmc_exec_lights", "number")
B738DR_fmc_exec_lights_fo 		= create_dataref("laminar/B738/fmod/fmc_exec_lights_fo", "number")
B738DR_fmc_exec_lights2			= create_dataref("laminar/B738/indicators/fmc_exec_lights", "number")
B738DR_fmc_exec_lights2_fo		= create_dataref("laminar/B738/indicators/fmc_exec_lights_fo", "number")

B738DR_autopilot_alt_interv_pos	= create_dataref("laminar/B738/autopilot/alt_interv_pos", "number")


B738DR_trans_alt			= create_dataref("laminar/B738/FMS/fmc_trans_alt", "number")
B738DR_trans_alt_mod = 0
B738DR_trans_lvl			= create_dataref("laminar/B738/FMS/fmc_trans_lvl", "number")
B738DR_forecast_in_hg		= create_dataref("laminar/B738/FMS/fmc_forecast_in_hg", "number")
B738DR_trans_lvl_mod = 0
B738DR_forecast_in_hg_mod = 0
B738DR_forecast_isa_dev		= create_dataref("laminar/B738/FMS/fmc_forecast_isa_dev", "number")
B738DR_forecast_isa_dev_mod = 0

B738DR_isa_dev_c			= create_dataref("laminar/B738/FMS/fmc_isa_dev_c", "number")
B738DR_fmc_cabin_rate		= create_dataref("laminar/B738/FMS/fmc_cabin_rate", "number")

B738DR_takeoff_flaps_set	= create_dataref("laminar/B738/FMS/takeoff_flaps_set", "number")
B738DR_approach_flaps_set	= create_dataref("laminar/B738/FMS/approach_flaps_set", "number")
B738DR_trim_set				= create_dataref("laminar/B738/FMS/trim_set", "number")

B738DR_thr_red_alt			= create_dataref("laminar/B738/FMS/throttle_red_alt", "number")
B738DR_accel_alt			= create_dataref("laminar/B738/FMS/accel_height", "number")
B738DR_eo_accel_alt			= create_dataref("laminar/B738/FMS/eo_accel_height", "number")
B738DR_thr_cut_alt			= create_dataref("laminar/B738/FMS/cutback_height", "number")
B738DR_thr_cutback			= create_dataref("laminar/B738/FMS/cutback", "number")

B738DR_pfd_vert_path		= create_dataref("laminar/B738/pfd/pfd_vert_path", "number")
B738DR_pfd_vert_path2		= create_dataref("laminar/B738/pfd/pfd_vert_path2", "number")
B738DR_pfd_vert_path_fo		= create_dataref("laminar/B738/pfd/pfd_vert_path_fo", "number")
B738DR_pfd_vert_path2_fo	= create_dataref("laminar/B738/pfd/pfd_vert_path2_fo", "number")
B738DR_pfd_trk_path			= create_dataref("laminar/B738/pfd/pfd_trk_path", "number")
B738DR_pfd_trk_path_fo		= create_dataref("laminar/B738/pfd/pfd_trk_path_fo", "number")
B738DR_nd_vert_path			= create_dataref("laminar/B738/pfd/nd_vert_path", "number")

B738DR_pfd_loc_ghost 		= create_dataref("laminar/B738/pfd/pfd_loc_ghost", "number")
B738DR_pfd_gs_ghost 		= create_dataref("laminar/B738/pfd/pfd_gs_ghost", "number")
B738DR_pfd_loc_ghost_fo		= create_dataref("laminar/B738/pfd/pfd_loc_ghost_fo", "number")
B738DR_pfd_gs_ghost_fo 		= create_dataref("laminar/B738/pfd/pfd_gs_ghost_fo", "number")
B738DR_pfd_fac_ghost 		= create_dataref("laminar/B738/pfd/pfd_fac_ghost", "number")
B738DR_pfd_gp_ghost 		= create_dataref("laminar/B738/pfd/pfd_gp_ghost", "number")
B738DR_pfd_fac_ghost_fo		= create_dataref("laminar/B738/pfd/pfd_fac_ghost_fo", "number")
B738DR_pfd_gp_ghost_fo 		= create_dataref("laminar/B738/pfd/pfd_gp_ghost_fo", "number")
B738DR_pfd_gls_loc_ghost 	= create_dataref("laminar/B738/pfd/pfd_gls_loc_ghost", "number")
B738DR_pfd_gls_gs_ghost 	= create_dataref("laminar/B738/pfd/pfd_gls_gs_ghost", "number")
B738DR_pfd_gls_loc_ghost_fo	= create_dataref("laminar/B738/pfd/pfd_gls_loc_ghost_fo", "number")
B738DR_pfd_gls_gs_ghost_fo 	= create_dataref("laminar/B738/pfd/pfd_gls_gs_ghost_fo", "number")

B738DR_nd_fac_horizontal	= create_dataref("laminar/B738/fms/pfd_fac_horizontal", "number")
B738DR_nd_fac_horizontal_fo	= create_dataref("laminar/B738/fms/pfd_fac_horizontal_fo", "number")
B738DR_ian_info				= create_dataref("laminar/B738/fms/ian_info", "number")
B738DR_ian_info_fo			= create_dataref("laminar/B738/fms/ian_info_fo", "number")

rw_fac_id2				= create_dataref("laminar/B738/fms/rw_fac_id2", "string")
rw_dist2				= create_dataref("laminar/B738/fms/rw_dist2", "number")

B738DR_autopilot_pfd_mode		= create_dataref("laminar/B738/autopilot/pfd_mode", "number")
B738DR_autopilot_pfd_mode_fo	= create_dataref("laminar/B738/autopilot/pfd_mode_fo", "number")
B738DR_scale_pfd_mode			= create_dataref("laminar/B738/autopilot/scale_pfd_mode", "number")
B738DR_scale_pfd_mode_fo		= create_dataref("laminar/B738/autopilot/scale_pfd_mode_fo", "number")

B738DR_fms_ils_disable		= create_dataref("laminar/B738/FMS/ils_disable", "number")
B738DR_gls_gs_status		= create_dataref("laminar/B738/nav/gls_gs_status", "number")

function align_time_handl() end
function refuel_time_handl() end
function detail_cl_handl() end
function fmc_input_lag_handl() end
function ap_yoke_tolerance_handl() end
B738DR_align_time			= create_dataref("laminar/B738/FMS/align_time", "number", align_time_handl)
B738DR_refuel_time			= create_dataref("laminar/B738/FMS/refuel_time", "number", refuel_time_handl)
B738DR_detail_cl			= create_dataref("laminar/B738/FMS/detail_cl", "number", detail_cl_handl)
B738DR_fmc_input_lag		= create_dataref("laminar/B738/FMS/fmc_input_lag", "number", fmc_input_lag_handl)
B738DR_ap_yoke_tolerance	= create_dataref("laminar/B738/FMS/ap_yoke_tolerance", "number", ap_yoke_tolerance_handl)

-- FMOD SOUNDS DATAREFS
B738DR_fms_msg_sound		= create_dataref("laminar/B738/fmod/fms_message", "number")
B738DR_fms_key				= create_dataref("laminar/B738/fmod/fms_key", "number")


-- FIX
B738DR_fix_show			= create_dataref("laminar/B738/nd/fix_show", "array[5]")
B738DR_fix_x			= create_dataref("laminar/B738/nd/fix_x", "array[5]")
B738DR_fix_y			= create_dataref("laminar/B738/nd/fix_y", "array[5]")
B738DR_fix_type			= create_dataref("laminar/B738/nd/fix_type", "array[5]")
B738DR_fix_rot_0		= create_dataref("laminar/B738/nd/fix_rot_0", "array[5]")
B738DR_fix_rot_1		= create_dataref("laminar/B738/nd/fix_rot_1", "array[5]")
B738DR_fix_rot_2		= create_dataref("laminar/B738/nd/fix_rot_2", "array[5]")
B738DR_fix_dist_0		= create_dataref("laminar/B738/nd/fix_dist_0", "array[5]")
B738DR_fix_dist_1		= create_dataref("laminar/B738/nd/fix_dist_1", "array[5]")
B738DR_fix_dist_2		= create_dataref("laminar/B738/nd/fix_dist_2", "array[5]")
B738DR_fix_dist_0_nm	= create_dataref("laminar/B738/nd/fix_dist_0_nm", "array[5]")
B738DR_fix_dist_1_nm	= create_dataref("laminar/B738/nd/fix_dist_1_nm", "array[5]")
B738DR_fix_dist_2_nm	= create_dataref("laminar/B738/nd/fix_dist_2_nm", "array[5]")
B738DR_fix_id00			= create_dataref("laminar/B738/nd/fix_id00", "string")
B738DR_fix_id01			= create_dataref("laminar/B738/nd/fix_id01", "string")
B738DR_fix_id02			= create_dataref("laminar/B738/nd/fix_id02", "string")
B738DR_fix_id03			= create_dataref("laminar/B738/nd/fix_id03", "string")
B738DR_fix_id04			= create_dataref("laminar/B738/nd/fix_id04", "string")
B738DR_fix_rad_dist_0	= create_dataref("laminar/B738/nd/fix_rad_dist_0", "array[5]")
B738DR_fix_rad_dist_1	= create_dataref("laminar/B738/nd/fix_rad_dist_1", "array[5]")
B738DR_fix_rad_dist_2	= create_dataref("laminar/B738/nd/fix_rad_dist_2", "array[5]")
B738DR_fix_rad_dist_0a	= create_dataref("laminar/B738/nd/fix_rad_dist_0a", "array[5]")
B738DR_fix_rad_dist_1a	= create_dataref("laminar/B738/nd/fix_rad_dist_1a", "array[5]")
B738DR_fix_rad_dist_2a	= create_dataref("laminar/B738/nd/fix_rad_dist_2a", "array[5]")
B738DR_fix_rad00_0		= create_dataref("laminar/B738/nd/fix_rad00_0", "string")
B738DR_fix_rad00_1		= create_dataref("laminar/B738/nd/fix_rad00_1", "string")
B738DR_fix_rad00_2		= create_dataref("laminar/B738/nd/fix_rad00_2", "string")
B738DR_fix_rad01_0		= create_dataref("laminar/B738/nd/fix_rad01_0", "string")
B738DR_fix_rad01_1		= create_dataref("laminar/B738/nd/fix_rad01_1", "string")
B738DR_fix_rad01_2		= create_dataref("laminar/B738/nd/fix_rad01_2", "string")
B738DR_fix_rad02_0		= create_dataref("laminar/B738/nd/fix_rad02_0", "string")
B738DR_fix_rad02_1		= create_dataref("laminar/B738/nd/fix_rad02_1", "string")
B738DR_fix_rad02_2		= create_dataref("laminar/B738/nd/fix_rad02_2", "string")
B738DR_fix_rad03_0		= create_dataref("laminar/B738/nd/fix_rad03_0", "string")
B738DR_fix_rad03_1		= create_dataref("laminar/B738/nd/fix_rad03_1", "string")
B738DR_fix_rad03_2		= create_dataref("laminar/B738/nd/fix_rad03_2", "string")
B738DR_fix_rad04_0		= create_dataref("laminar/B738/nd/fix_rad04_0", "string")
B738DR_fix_rad04_1		= create_dataref("laminar/B738/nd/fix_rad04_1", "string")
B738DR_fix_rad04_2		= create_dataref("laminar/B738/nd/fix_rad04_2", "string")
B738DR_fix_lat			= create_dataref("laminar/B738/nd/fix_lat", "array[5]")
B738DR_fix_lon			= create_dataref("laminar/B738/nd/fix_lon", "array[5]")
B738DR_fix_en			= create_dataref("laminar/B738/nd/fix_en", "array[5]")
B738DR_fix_abm_act		= create_dataref("laminar/B738/nd/fix_abm_act", "array[5]")
B738DR_fix_abm_brg		= create_dataref("laminar/B738/nd/fix_abm_brg", "array[5]")
B738DR_fix_abm_dist		= create_dataref("laminar/B738/nd/fix_abm_dist", "array[5]")


--/////
function fix_dist_x0_handl() end
function fix_dist_y0_handl() end
function fix_dist_x1_handl() end
function fix_dist_y1_handl() end
function fix_dist_x2_handl() end
function fix_dist_y2_handl() end

function fix_fo_dist_x0_handl() end
function fix_fo_dist_y0_handl() end
function fix_fo_dist_x1_handl() end
function fix_fo_dist_y1_handl() end
function fix_fo_dist_x2_handl() end
function fix_fo_dist_y2_handl() end

function fix_dist_00_00_handl() end
function fix_dist_00_01_handl() end
function fix_dist_00_02_handl() end
function fix_dist_01_00_handl() end
function fix_dist_01_01_handl() end
function fix_dist_01_02_handl() end
function fix_dist_02_00_handl() end
function fix_dist_02_01_handl() end
function fix_dist_02_02_handl() end
function fix_dist_03_00_handl() end
function fix_dist_03_01_handl() end
function fix_dist_03_02_handl() end
function fix_dist_04_00_handl() end
function fix_dist_04_01_handl() end
function fix_dist_04_02_handl() end

B738DR_fix_dist_x0		= create_dataref("laminar/B738/nd/fix_dist_x_0", "array[5]", fix_dist_x0_handl)
B738DR_fix_dist_y0		= create_dataref("laminar/B738/nd/fix_dist_y_0", "array[5]", fix_dist_y0_handl)
B738DR_fix_dist_x1		= create_dataref("laminar/B738/nd/fix_dist_x_1", "array[5]", fix_dist_x1_handl)
B738DR_fix_dist_y1		= create_dataref("laminar/B738/nd/fix_dist_y_1", "array[5]", fix_dist_y1_handl)
B738DR_fix_dist_x2		= create_dataref("laminar/B738/nd/fix_dist_x_2", "array[5]", fix_dist_x2_handl)
B738DR_fix_dist_y2		= create_dataref("laminar/B738/nd/fix_dist_y_2", "array[5]", fix_dist_y2_handl)

B738DR_fix_dist_00_00		= create_dataref("laminar/B738/nd/fix_dist_00_00", "string", fix_dist_00_00_handl)
B738DR_fix_dist_00_01		= create_dataref("laminar/B738/nd/fix_dist_00_01", "string", fix_dist_00_01_handl)
B738DR_fix_dist_00_02		= create_dataref("laminar/B738/nd/fix_dist_00_02", "string", fix_dist_00_02_handl)
B738DR_fix_dist_01_00		= create_dataref("laminar/B738/nd/fix_dist_01_00", "string", fix_dist_01_00_handl)
B738DR_fix_dist_01_01		= create_dataref("laminar/B738/nd/fix_dist_01_01", "string", fix_dist_01_01_handl)
B738DR_fix_dist_01_02		= create_dataref("laminar/B738/nd/fix_dist_01_02", "string", fix_dist_01_02_handl)
B738DR_fix_dist_02_00		= create_dataref("laminar/B738/nd/fix_dist_02_00", "string", fix_dist_02_00_handl)
B738DR_fix_dist_02_01		= create_dataref("laminar/B738/nd/fix_dist_02_01", "string", fix_dist_02_01_handl)
B738DR_fix_dist_02_02		= create_dataref("laminar/B738/nd/fix_dist_02_02", "string", fix_dist_02_02_handl)
B738DR_fix_dist_03_00		= create_dataref("laminar/B738/nd/fix_dist_03_00", "string", fix_dist_03_00_handl)
B738DR_fix_dist_03_01		= create_dataref("laminar/B738/nd/fix_dist_03_01", "string", fix_dist_03_01_handl)
B738DR_fix_dist_03_02		= create_dataref("laminar/B738/nd/fix_dist_03_02", "string", fix_dist_03_02_handl)
B738DR_fix_dist_04_00		= create_dataref("laminar/B738/nd/fix_dist_04_00", "string", fix_dist_04_00_handl)
B738DR_fix_dist_04_01		= create_dataref("laminar/B738/nd/fix_dist_04_01", "string", fix_dist_04_01_handl)
B738DR_fix_dist_04_02		= create_dataref("laminar/B738/nd/fix_dist_04_02", "string", fix_dist_04_02_handl)


B738DR_fix_fo_dist_x0		= create_dataref("laminar/B738/nd/fix_fo_dist_x_0", "array[5]", fix_fo_dist_x0_handl)
B738DR_fix_fo_dist_y0		= create_dataref("laminar/B738/nd/fix_fo_dist_y_0", "array[5]", fix_fo_dist_y0_handl)
B738DR_fix_fo_dist_x1		= create_dataref("laminar/B738/nd/fix_fo_dist_x_1", "array[5]", fix_fo_dist_x1_handl)
B738DR_fix_fo_dist_y1		= create_dataref("laminar/B738/nd/fix_fo_dist_y_1", "array[5]", fix_fo_dist_y1_handl)
B738DR_fix_fo_dist_x2		= create_dataref("laminar/B738/nd/fix_fo_dist_x_2", "array[5]", fix_fo_dist_x2_handl)
B738DR_fix_fo_dist_y2		= create_dataref("laminar/B738/nd/fix_fo_dist_y_2", "array[5]", fix_fo_dist_y2_handl)

--/////


B738DR_ref_list_rwy_icao	= create_dataref("laminar/B738/fms/ref_list_rwy_icao", "string")
B738DR_ref_list_rwy			= create_dataref("laminar/B738/fms/ref_list_rwy", "string")
B738DR_ref_list_rwy_len		= create_dataref("laminar/B738/fms/ref_list_rwy_len", "array[16]")
B738DR_ref_list_rwy_hdg		= create_dataref("laminar/B738/fms/ref_list_rwy_hdg", "array[16]")
B738DR_des_list_rwy_icao	= create_dataref("laminar/B738/fms/des_list_rwy_icao", "string")
B738DR_des_list_rwy			= create_dataref("laminar/B738/fms/des_list_rwy", "string")
B738DR_des_list_rwy_len		= create_dataref("laminar/B738/fms/des_list_rwy_len", "array[16]")
B738DR_des_list_rwy_hdg		= create_dataref("laminar/B738/fms/des_list_rwy_hdg", "array[16]")

-- FIX First Officer
B738DR_fix_fo_show			= create_dataref("laminar/B738/nd/fix_fo_show", "array[5]")
B738DR_fix_fo_x				= create_dataref("laminar/B738/nd/fix_fo_x", "array[5]")
B738DR_fix_fo_y				= create_dataref("laminar/B738/nd/fix_fo_y", "array[5]")
B738DR_fix_fo_type			= create_dataref("laminar/B738/nd/fix_fo_type", "array[5]")
B738DR_fix_fo_rot_0			= create_dataref("laminar/B738/nd/fix_fo_rot_0", "array[5]")
B738DR_fix_fo_rot_1			= create_dataref("laminar/B738/nd/fix_fo_rot_1", "array[5]")
B738DR_fix_fo_rot_2			= create_dataref("laminar/B738/nd/fix_fo_rot_2", "array[5]")
B738DR_fix_fo_dist_0		= create_dataref("laminar/B738/nd/fix_fo_dist_0", "array[5]")
B738DR_fix_fo_dist_1		= create_dataref("laminar/B738/nd/fix_fo_dist_1", "array[5]")
B738DR_fix_fo_dist_2		= create_dataref("laminar/B738/nd/fix_fo_dist_2", "array[5]")
B738DR_fix_fo_dist_0_nm		= create_dataref("laminar/B738/nd/fix_fo_dist_0_nm", "array[5]")
B738DR_fix_fo_dist_1_nm		= create_dataref("laminar/B738/nd/fix_fo_dist_1_nm", "array[5]")
B738DR_fix_fo_dist_2_nm		= create_dataref("laminar/B738/nd/fix_fo_dist_2_nm", "array[5]")
B738DR_fix_fo_id00			= create_dataref("laminar/B738/nd/fix_fo_id00", "string")
B738DR_fix_fo_id01			= create_dataref("laminar/B738/nd/fix_fo_id01", "string")
B738DR_fix_fo_id02			= create_dataref("laminar/B738/nd/fix_fo_id02", "string")
B738DR_fix_fo_id03			= create_dataref("laminar/B738/nd/fix_fo_id03", "string")
B738DR_fix_fo_id04			= create_dataref("laminar/B738/nd/fix_fo_id04", "string")
B738DR_fix_fo_rad_dist_0	= create_dataref("laminar/B738/nd/fix_fo_rad_dist_0", "array[5]")
B738DR_fix_fo_rad_dist_1	= create_dataref("laminar/B738/nd/fix_fo_rad_dist_1", "array[5]")
B738DR_fix_fo_rad_dist_2	= create_dataref("laminar/B738/nd/fix_fo_rad_dist_2", "array[5]")
B738DR_fix_fo_rad_dist_0a	= create_dataref("laminar/B738/nd/fix_fo_rad_dist_0a", "array[5]")
B738DR_fix_fo_rad_dist_1a	= create_dataref("laminar/B738/nd/fix_fo_rad_dist_1a", "array[5]")
B738DR_fix_fo_rad_dist_2a	= create_dataref("laminar/B738/nd/fix_fo_rad_dist_2a", "array[5]")
B738DR_fix_fo_rad00_0		= create_dataref("laminar/B738/nd/fix_fo_rad00_0", "string")
B738DR_fix_fo_rad00_1		= create_dataref("laminar/B738/nd/fix_fo_rad00_1", "string")
B738DR_fix_fo_rad00_2		= create_dataref("laminar/B738/nd/fix_fo_rad00_2", "string")
B738DR_fix_fo_rad01_0		= create_dataref("laminar/B738/nd/fix_fo_rad01_0", "string")
B738DR_fix_fo_rad01_1		= create_dataref("laminar/B738/nd/fix_fo_rad01_1", "string")
B738DR_fix_fo_rad01_2		= create_dataref("laminar/B738/nd/fix_fo_rad01_2", "string")
B738DR_fix_fo_rad02_0		= create_dataref("laminar/B738/nd/fix_fo_rad02_0", "string")
B738DR_fix_fo_rad02_1		= create_dataref("laminar/B738/nd/fix_fo_rad02_1", "string")
B738DR_fix_fo_rad02_2		= create_dataref("laminar/B738/nd/fix_fo_rad02_2", "string")
B738DR_fix_fo_rad03_0		= create_dataref("laminar/B738/nd/fix_fo_rad03_0", "string")
B738DR_fix_fo_rad03_1		= create_dataref("laminar/B738/nd/fix_fo_rad03_1", "string")
B738DR_fix_fo_rad03_2		= create_dataref("laminar/B738/nd/fix_fo_rad03_2", "string")
B738DR_fix_fo_rad04_0		= create_dataref("laminar/B738/nd/fix_fo_rad04_0", "string")
B738DR_fix_fo_rad04_1		= create_dataref("laminar/B738/nd/fix_fo_rad04_1", "string")
B738DR_fix_fo_rad04_2		= create_dataref("laminar/B738/nd/fix_fo_rad04_2", "string")
B738DR_fix_fo_lat			 = create_dataref("laminar/B738/nd/fix_fo_lat", "array[5]")
B738DR_fix_fo_lon			 = create_dataref("laminar/B738/nd/fix_fo_lon", "array[5]")
B738DR_fix_fo_en			 = create_dataref("laminar/B738/nd/fix_fo_en", "array[5]")


B738DR_acf_show 			= create_dataref("laminar/B738/nd/acf_show", "number")
B738DR_acf_x 				= create_dataref("laminar/B738/nd/acf_x", "number")
B738DR_acf_y 				= create_dataref("laminar/B738/nd/acf_y", "number")
B738DR_acf_rot 				= create_dataref("laminar/B738/nd/acf_rot", "number")

B738DR_acf_fo_show			= create_dataref("laminar/B738/nd/acf_fo_show", "number")
B738DR_acf_x_fo 			= create_dataref("laminar/B738/nd/acf_x_fo", "number")
B738DR_acf_y_fo 			= create_dataref("laminar/B738/nd/acf_y_fo", "number")
B738DR_acf_rot_fo			= create_dataref("laminar/B738/nd/acf_rot_fo", "number")

B738DR_rest_wpt_spd_id 		= create_dataref("laminar/B738/fms/rest_wpt_spd_id", "string")
B738DR_rest_wpt_spd 		= create_dataref("laminar/B738/fms/rest_wpt_spd", "number")
B738DR_rest_wpt_spd_idx		= create_dataref("laminar/B738/fms/rest_wpt_spd_idx", "number")
B738DR_rest_wpt_spd2		= create_dataref("laminar/B738/fms/rest_wpt_spd2", "number")
B738DR_rest_wpt_spd_tgt		= create_dataref("laminar/B738/fms/rest_wpt_spd_tgt", "number")
B738DR_rest_wpt_spd_type	= create_dataref("laminar/B738/fms/rest_wpt_spd_type", "number")

B738DR_rest_wpt_alt_id 		= create_dataref("laminar/B738/fms/rest_wpt_alt_id", "string")
B738DR_rest_wpt_alt 		= create_dataref("laminar/B738/fms/rest_wpt_alt", "number")
B738DR_rest_wpt_alt_t 		= create_dataref("laminar/B738/fms/rest_wpt_alt_t", "number")
B738DR_rest_wpt_alt_idx		= create_dataref("laminar/B738/fms/rest_wpt_alt_idx", "number")
B738DR_rest_wpt_alt_dist	= create_dataref("laminar/B738/fms/rest_wpt_alt_dist", "number")

B738DR_rest_wpt_alt_id2 		= create_dataref("laminar/B738/fms/rest_wpt_alt_id2", "string")
B738DR_rest_wpt_alt2 		= create_dataref("laminar/B738/fms/rest_wpt_alt2", "number")
B738DR_rest_wpt_alt_idx2		= create_dataref("laminar/B738/fms/rest_wpt_alt_idx2", "number")

B738DR_calc_wpt_spd 		= create_dataref("laminar/B738/fms/calc_wpt_spd", "number")
B738DR_calc_wpt_alt 		= create_dataref("laminar/B738/fms/calc_wpt_alt", "number")

id_ed						= create_dataref("laminar/B738/fms/id_ed", "string")
idx_ed						= create_dataref("laminar/B738/fms/idx_ed", "number")
alt_ed 						= create_dataref("laminar/B738/fms/alt_ed", "number")
alt_type_ed 				= create_dataref("laminar/B738/fms/alt_type_ed", "number")
ed_dist						= create_dataref("laminar/B738/fms/ed_dist", "number")
td_dist						= create_dataref("laminar/B738/fms/td_dist", "number")
td_dist99 					= create_dataref("laminar/B738/fms/td_dist99", "number")
dist_ed						= create_dataref("laminar/B738/fms/dist_to_ed", "number")

ed_alt						= create_dataref("laminar/B738/fms/ed_alt", "number")
ed_found					= create_dataref("laminar/B738/fms/ed_idx", "number")
ed_to_dist					= create_dataref("laminar/B738/fms/ed_to_dist", "number")
ed_vpa						= create_dataref("laminar/B738/fms/ed_vpa", "number")
ed_type						= create_dataref("laminar/B738/fms/ed_type", "number")

-- AUTOPILOT
function offset_handl() end
offset						= create_dataref("laminar/B738/fms/vnav_idx", "number", offset_handl)
B738DR_offset_mod			= create_dataref("laminar/B738/fms/vnav_idx_mod", "number")
B738DR_offset_mod3			= create_dataref("laminar/B738/fms/vnav_idx_mod3", "number")

function offset_seq_handl() end
offset_seq					= create_dataref("laminar/B738/fms/vnav_idx_seq", "number", offset_seq_handl)
function offset_seq_prev_handl() end
offset_seq_prev				= create_dataref("laminar/B738/fms/vnav_idx_seq_prev", "number", offset_seq_prev_handl)

B738DR_fmc_dist				= create_dataref("laminar/B738/fms/lnav_dist_next", "number")
B738DR_fmc_dist2			= create_dataref("laminar/B738/fms/lnav_dist2_next", "number")
B738DR_fmc_crs				= create_dataref("laminar/B738/fms/gps_course_degtm", "number")
B738DR_fmc_trk				= create_dataref("laminar/B738/fms/gps_track_degtm", "number")
B738DR_fmc_trk2				= create_dataref("laminar/B738/fms/gps_track2_degtm", "number")
B738DR_fmc_trk3				= create_dataref("laminar/B738/fms/gps_track3_degtm", "number")
B738DR_fmc_trk_turn			= create_dataref("laminar/B738/fms/gps_track_turn", "number")
B738DR_fmc_trk_turn2		= create_dataref("laminar/B738/fms/gps_track_turn2", "number")
B738DR_fmc_dist_mod			= create_dataref("laminar/B738/fms/lnav_dist_next_mod", "number")
B738DR_fmc_crs_mod			= create_dataref("laminar/B738/fms/gps_course_degtm_mod", "number")

B738DR_fmc_dist_to_dest		= create_dataref("laminar/B738/fms/dist_to_dest", "number")
B738DR_fmc_dist_to_dest2	= create_dataref("laminar/B738/fms/dist_to_dest2", "number")

B738DR_fpln_active			= create_dataref("laminar/B738/fms/fpln_acive", "number")
B738DR_fpln_active_fo		= create_dataref("laminar/B738/fms/fpln_acive_fo", "number")
B738DR_fpln_nav_id			= create_dataref("laminar/B738/fms/fpln_nav_id", "string")
B738DR_xtrack				= create_dataref("laminar/B738/fms/xtrack", "number")
B738DR_xtrack2				= create_dataref("laminar/B738/fms/xtrack2", "number")
B738DR_wpt_path				= create_dataref("laminar/B738/fms/gps_wpt_path", "string")
B738DR_rnp					= create_dataref("laminar/B738/fms/rnp", "number")
B738DR_rnp_cifp				= create_dataref("laminar/B738/fms/rnp_cifp", "number")
B738DR_anp					= create_dataref("laminar/B738/fms/anp", "number")
B738DR_anp_rnp_colour		= create_dataref("laminar/B738/fms/anp_rnp_colour", "number")
B738DR_xtrack_nd			= create_dataref("laminar/B738/fms/xtrack_nd", "number")
B738DR_xtrack_show			= create_dataref("laminar/B738/fms/xtrack_show", "number")

function rt_abeam_dist_handl() end
function rt_abeam_dist_bp_handl() end
function rt_xtrack_handl() end
function rt_region_handl() end
function rt_abeam_dist_seg_handl() end
B738DR_rt_abeam_dist		= create_dataref("laminar/B738/fms/rt_abeam_dist", "number", rt_abeam_dist_handl)
B738DR_rt_abeam_dist_bp		= create_dataref("laminar/B738/fms/rt_abeam_dist_bp", "number", rt_abeam_dist_bp_handl)
B738DR_rt_xtrack			= create_dataref("laminar/B738/fms/rt_xtrack", "number", rt_xtrack_handl)
B738DR_rt_region			= create_dataref("laminar/B738/fms/rt_region", "number", rt_region_handl)
B738DR_rt_abeam_dist_seg	= create_dataref("laminar/B738/fms/rt_abeam_dist_seg", "number", rt_abeam_dist_seg_handl)

B738DR_irs_nd_allign		= create_dataref("laminar/B738/irs/nd_allign", "number")
B738DR_irs_nd_allign2		= create_dataref("laminar/B738/irs/nd_allign2", "number")
B738DR_irs_nd_allign_12		= create_dataref("laminar/B738/irs/nd_allign12", "number")

B738DR_irs_pos_set 			= create_dataref("laminar/B738/irs/irs_pos_set", "string")

B738DR_vrnp_enable			= create_dataref("laminar/B738/fms/vrnp_enable", "number")
B738DR_vrnp					= create_dataref("laminar/B738/fms/vrnp", "number")
B738DR_vanp					= create_dataref("laminar/B738/fms/vanp", "number")
B738DR_fmc_bank_angle		= create_dataref("laminar/B738/fms/bank_angle", "number")
B738DR_radii_turn_act		= create_dataref("laminar/B738/fms/radii_turn_act", "number")
B738DR_radii_turn			= create_dataref("laminar/B738/fms/radii_turn", "number")
B738DR_radii_final_crs		= create_dataref("laminar/B738/fms/radii_final_crs", "number")

function legs_intdir_act_hadl() end
legs_intdir_act				= create_dataref("laminar/B738/fms/intdir_act", "number", legs_intdir_act_hadl)
function legs_intdir_idx_hadl() end
legs_intdir_idx				= create_dataref("laminar/B738/fms/intdir_idx", "number", legs_intdir_idx_hadl)
function legs_intdir_idx_mod_hadl() end
legs_intdir_idx_mod				= create_dataref("laminar/B738/fms/intdir_idx_mod", "number", legs_intdir_idx_mod_hadl)
function legs_intdir_crs_hadl() end
legs_intdir_crs				= create_dataref("laminar/B738/fms/intdir_crs", "number", legs_intdir_crs_hadl)
legs_intdir_crs2			= create_dataref("laminar/B738/fms/intdir_crs2", "number")
legs_intdir					= create_dataref("laminar/B738/fms/intdir", "number")
legs_intdir_lat 			= create_dataref("laminar/B738/fms/intdir_lat", "number")
legs_intdir_lon 			= create_dataref("laminar/B738/fms/intdir_lon", "number")
legs_intdir_trk 			= create_dataref("laminar/B738/fms/intdir_trk", "number")
legs_intdir_rte				= create_dataref("laminar/B738/fms/intdir_rte", "number")

function legs_intdir_trk2_handl() end
legs_intdir_trk2			= create_dataref("laminar/B738/fms/intdir_trk2", "number", legs_intdir_trk2_handl)
local legs_intdir_trk2_mem = -1
local legs_intdir_radius_mem = 0
local legs_intdir_rad_lat_mem = 0
local legs_intdir_rad_lon_mem = 0
local legs_intdir_turn_mem = 0
local legs_intdir_end_lat_mem = 0
local legs_intdir_end_lon_mem = 0

legs_intdir_radius			= create_dataref("laminar/B738/fms/intdir_radius", "number")
legs_intdir_rad_lat			= create_dataref("laminar/B738/fms/intdir_rad_lat", "number")
legs_intdir_rad_lon			= create_dataref("laminar/B738/fms/intdir_rad_lon", "number")
legs_intdir_turn			= create_dataref("laminar/B738/fms/intdir_turn", "number")
legs_intdir_end_lat			= create_dataref("laminar/B738/fms/intdir_end_lat", "number")
legs_intdir_end_lon			= create_dataref("laminar/B738/fms/intdir_end_lon", "number")
legs_intdir_init_trk		= create_dataref("laminar/B738/fms/intdir_init_trk", "number")

function legs_intdir_radius_mod_handl() end
function legs_intdir_rad_lat_mod_handl() end
function legs_intdir_rad_lon_mod_handl() end
function legs_intdir_turn_mod_handl() end
function legs_intdir_end_lat_mod_handl() end
function legs_intdir_end_lon_mod_handl() end
legs_intdir_radius_mod			= create_dataref("laminar/B738/fms/intdir_radius_mod", "number", legs_intdir_radius_mod_handl)
legs_intdir_rad_lat_mod			= create_dataref("laminar/B738/fms/intdir_rad_lat_mod", "number", legs_intdir_rad_lat_mod_handl)
legs_intdir_rad_lon_mod			= create_dataref("laminar/B738/fms/intdir_rad_lon_mod", "number", legs_intdir_rad_lon_mod_handl)
legs_intdir_turn_mod			= create_dataref("laminar/B738/fms/intdir_turn_mod", "number", legs_intdir_turn_mod_handl)
legs_intdir_end_lat_mod			= create_dataref("laminar/B738/fms/intdir_end_lat_mod", "number", legs_intdir_end_lat_mod_handl)
legs_intdir_end_lon_mod			= create_dataref("laminar/B738/fms/intdir_end_lon_mod", "number", legs_intdir_end_lon_mod_handl)

function nav_mode_hadl() end
nav_mode					= create_dataref("laminar/B738/fms/nav_mode", "number", nav_mode_hadl)

B738DR_tc_lat			= create_dataref("laminar/B738/nd/tc_lat", "number")
B738DR_tc_lon			= create_dataref("laminar/B738/nd/tc_lon", "number")
B738DR_td_lat			= create_dataref("laminar/B738/nd/td_lat", "number")
B738DR_td_lon			= create_dataref("laminar/B738/nd/td_lon", "number")
B738DR_decel_lat		= create_dataref("laminar/B738/nd/decel_lat", "number")
B738DR_decel_lon		= create_dataref("laminar/B738/nd/decel_lon", "number")
B738DR_decel_def_lat	= create_dataref("laminar/B738/nd/decel_def_lat", "number")
B738DR_decel_def_lon	= create_dataref("laminar/B738/nd/decel_def_lon", "number")

td_idx 						= create_dataref("laminar/B738/fms/vnav_td_idx", "number")
tc_idx						= create_dataref("laminar/B738/fms/vnav_tc_idx", "number")
tc_disco 					= create_dataref("laminar/B738/fms/vnav_tc_disco", "number")
decel_idx					= create_dataref("laminar/B738/fms/vnav_decel_idx", "number")
decel_before_idx			= create_dataref("laminar/B738/fms/vnav_decel_before_idx", "number")
decel_dist					= create_dataref("laminar/B738/fms/vnav_decel_dist", "number")
decel_dist2					= create_dataref("laminar/B738/fms/vnav_decel_dist2", "number")
decel_wpt_idx				= create_dataref("laminar/B738/fms/vnav_decel_wpt_idx", "number")
decel_wpt_dist				= create_dataref("laminar/B738/fms/vnav_decel_wpt_dist", "number")
decel_wpt_spd				= create_dataref("laminar/B738/fms/vnav_decel_wpt_spd", "number")
tc_dist						= create_dataref("laminar/B738/fms/vnav_tc_dist", "number")
B738DR_FL100_desc_dist		= create_dataref("laminar/B738/fms/vnav_fl100_desc_dist", "number")
B738DR_FL100_idx 			= create_dataref("laminar/B738/fms/vnav_fl100_idx", "number")
B738DR_FL100_dist 			= create_dataref("laminar/B738/fms/vnav_fl100_dist", "number")
B738DR_FL100_idx2 			= create_dataref("laminar/B738/fms/vnav_fl100_idx2", "number")
B738DR_FL100_dist2 			= create_dataref("laminar/B738/fms/vnav_fl100_dist2", "number")
B738DR_FL100_dist_dest 		= create_dataref("laminar/B738/fms/vnav_fl100_dist_dest", "number")
B738DR_FL100_dist_dest2 	= create_dataref("laminar/B738/fms/vnav_fl100_dist_dest2", "number")
decel_hold_idx 				= create_dataref("laminar/B738/fms/vnav_decel_hold_idx", "number")
decel_hold_dist 			= create_dataref("laminar/B738/fms/vnav_decel_hold_dist", "number")
decel_hold_spd				= create_dataref("laminar/B738/fms/vnav_decel_hold_spd", "number")

tc_idx_mod					= create_dataref("laminar/B738/fms/vnav_tc_idx_mod", "number")
tc_dist_mod					= create_dataref("laminar/B738/fms/vnav_tc_dist_mod", "number")
tc_disco_mod				= create_dataref("laminar/B738/fms/vnav_tc_disco_mod", "number")
td_idx_mod 					= create_dataref("laminar/B738/fms/vnav_td_idx_mod", "number")
td_dist_mod					= create_dataref("laminar/B738/fms/td_dist_mod", "number")
td_dist99_mod				= create_dataref("laminar/B738/fms/td_dist99_mod", "number")
decel_idx_mod				= create_dataref("laminar/B738/fms/vnav_decel_idx_mod", "number")
decel_dist_mod				= create_dataref("laminar/B738/fms/vnav_decel_dist_mod", "number")
decel_dist2_mod				= create_dataref("laminar/B738/fms/vnav_decel_dist2_mod", "number")
decel_wpt_idx_mod			= create_dataref("laminar/B738/fms/vnav_decel_wpt_idx_mod", "number")
decel_wpt_dist_mod			= create_dataref("laminar/B738/fms/vnav_decel_wpt_dist_mod", "number")
B738DR_FL100_desc_dist_mod	= create_dataref("laminar/B738/fms/vnav_fl100_desc_dist_mod", "number")
B738DR_FL100_idx_mod 		= create_dataref("laminar/B738/fms/vnav_fl100_idx_mod", "number")
B738DR_FL100_dist_mod 		= create_dataref("laminar/B738/fms/vnav_fl100_dist_mod", "number")
B738DR_FL100_idx2_mod 		= create_dataref("laminar/B738/fms/vnav_fl100_idx2_mod", "number")
B738DR_FL100_dist2_mod 		= create_dataref("laminar/B738/fms/vnav_fl100_dist2_mod", "number")
B738DR_decel_mod_lat		= create_dataref("laminar/B738/nd/decel_mod_lat", "number")
B738DR_decel_mod_lon		= create_dataref("laminar/B738/nd/decel_mod_lon", "number")
B738DR_decel_def_mod_lat	= create_dataref("laminar/B738/nd/decel_def_mod_lat", "number")
B738DR_decel_def_mod_lon	= create_dataref("laminar/B738/nd/decel_def_mod_lon", "number")

td_fix_dist 				= create_dataref("laminar/B738/fms/vnav_td_fix_dist", "number")
td_fix_idx 					= create_dataref("laminar/B738/fms/vnav_td_fix_idx", "number")
ed_fix_found				= create_dataref("laminar/B738/fms/vnav_td_fix_ed", "number")
ed_fix_alt					= create_dataref("laminar/B738/fms/vnav_td_fix_alt", "number")
ed_fix_num					= create_dataref("laminar/B738/fms/vnav_ed_fix_num", "number")
ed_found_type				= create_dataref("laminar/B738/fms/vnav_ed_found_type", "number")

td_idx2						= create_dataref("laminar/B738/fms/vnav_td_idx2", "number")
td_dist2					= create_dataref("laminar/B738/fms/vnav_td_dist2", "number")
td_alt2						= create_dataref("laminar/B738/fms/vnav_td_alt2", "number")
td_alt2_hold				= create_dataref("laminar/B738/fms/vnav_td_alt2_hold", "number")
ed_alt2_hold				= create_dataref("laminar/B738/fms/vnav_ed_alt2_hold", "number")

function ignore_td_idx2_handl() end
B738DR_ignore_td_idx2		= create_dataref("laminar/B738/fms/vnav_ignore_td_idx2", "number", ignore_td_idx2_handl)

B738DR_vnav_td_dist 		= create_dataref("laminar/B738/fms/vnav_td_dist", "number")
B738DR_vnav_td_dist_mod		= create_dataref("laminar/B738/fms/vnav_td_dist_mod", "number")
B738DR_vnav_td_dist_fix		= create_dataref("laminar/B738/fms/vnav_td_dist_fix", "number")
B738DR_vnav_td_dist_fix_mod	= create_dataref("laminar/B738/fms/vnav_td_dist_fix_mod", "number")
B738DR_vnav_pth_alt			= create_dataref("laminar/B738/fms/vnav_pth_alt", "number")
B738DR_vnav_alt_err			= create_dataref("laminar/B738/fms/vnav_alt_err", "number")
B738DR_vnav_alt_err2 		= create_dataref("laminar/B738/fms/vnav_alt_err2", "number")
B738DR_vnav_pth_fpm			= create_dataref("laminar/B738/fms/vnav_pth_fpm", "number")
B738DR_vnav_vvi				= create_dataref("laminar/B738/fms/vnav_vvi", "number")
B738DR_vnav_vvi_corr		= create_dataref("laminar/B738/fms/vnav_vvi_corr", "number")
B738DR_vnav_err_pfd			= create_dataref("laminar/B738/fms/vnav_err_pfd", "number")
B738DR_vnav_pth_show		= create_dataref("laminar/B738/fms/vnav_pth_show", "number")

B738DR_vnav_pth_alt99		= create_dataref("laminar/B738/fms/vnav_pth_alt99", "number")
B738DR_vnav_alt_err99		= create_dataref("laminar/B738/fms/vnav_alt_err99", "number")
B738DR_vnav_vvi99			= create_dataref("laminar/B738/fms/vnav_vvi99", "number")

B738DR_gp_vvi				= create_dataref("laminar/B738/fms/gp_vvi", "number")
B738DR_gp_vvi_corr			= create_dataref("laminar/B738/fms/gp_vvi_corr", "number")
B738DR_gp_err_pfd			= create_dataref("laminar/B738/fms/gp_err_pfd", "number")
B738DR_gp_alt_err 			= create_dataref("laminar/B738/fms/gp_alt_err", "number")
B738DR_gp_alt_err2			= create_dataref("laminar/B738/fms/gp_alt_err2", "number")
B738DR_gp_pth_alt 			= create_dataref("laminar/B738/fms/gp_pth_alt", "number")
B738DR_fac_xtrack			= create_dataref("laminar/B738/fms/fac_xtrack", "number")
B738DR_fac_trk				= create_dataref("laminar/B738/fms/fac_trk", "number")
B738DR_fac_crs 				= create_dataref("laminar/B738/fms/fac_crs", "number")
B738DR_fac_brg				= create_dataref("laminar/B738/fms/fac_brg", "number")
B738DR_pfd_gp_path			= create_dataref("laminar/B738/fms/pfd_gp_path", "number")
B738DR_pfd_gp_path_fo		= create_dataref("laminar/B738/fms/pfd_gp_path_fo", "number")


calc_to_time 				= create_dataref("laminar/B738/fms/calc_to_time", "number")

B738DR_rnav_enable			= create_dataref("laminar/B738/fms/rnav_enable", "number")
rnav_idx_first				= create_dataref("laminar/B738/fms/rnav_idx_first", "number")
rnav_idx_last				= create_dataref("laminar/B738/fms/rnav_idx_last", "number")
rnav_alt					= create_dataref("laminar/B738/fms/rnav_alt", "number")
rnav_vpa					= create_dataref("laminar/B738/fms/rnav_vpa", "number")
B738DR_glidepath_type		= create_dataref("laminar/B738/fms/glidepath_type", "number")

rnav_idx_last_mod			= create_dataref("laminar/B738/fms/rnav_idx_last_mod", "number")

B738DR_gp_active			= create_dataref("laminar/B738/fms/vnav_gp_active", "number")
B738DR_vnav_app_active		= create_dataref("laminar/B738/fms/vnav_app_active", "number")

B738DR_missed_app_alt		= create_dataref("laminar/B738/fms/missed_app_alt", "number")
B738DR_missed_app_act		= create_dataref("laminar/B738/fms/missed_app_act", "number")
first_miss_app_idx 			= create_dataref("laminar/B738/fms/missed_app_wpt_idx", "number")
last_miss_app_idx 			= create_dataref("laminar/B738/fms/missed_app_wpt_idx2", "number")
first_app_idx 				= create_dataref("laminar/B738/fms/first_app_idx", "number")
iaf_app_idx 				= create_dataref("laminar/B738/fms/iaf_app_idx", "number")

B738DR_fms_id_eta		= create_dataref("laminar/B738/fms/id_eta", "string")
B738DR_fms_id_dist		= create_dataref("laminar/B738/fms/id_dist", "number")
B738DR_end_route		= create_dataref("laminar/B738/fms/end_route", "number")
B738DR_no_perf			= create_dataref("laminar/B738/fms/no_perf", "number")

function DR_TAT_hadl() end
B738DR_TAT				= create_dataref("laminar/B738/systems/temperature/tat_degc", "number", DR_TAT_hadl)

-- MMR --------
B738DR_nav1_hz			= find_dataref("sim/cockpit2/radios/actuators/nav1_frequency_hz")
B738DR_nav2_hz			= find_dataref("sim/cockpit2/radios/actuators/nav2_frequency_hz")
B738DR_nav1_stby_hz		= find_dataref("sim/cockpit2/radios/actuators/nav1_standby_frequency_hz")
B738DR_nav2_stby_hz		= find_dataref("sim/cockpit2/radios/actuators/nav2_standby_frequency_hz")

B738DR_nav_tuned_no_id		= create_dataref("laminar/radios/pilot/nav_tuned_no_id", "number")
B738DR_nav_tuned_no_id_fo	= create_dataref("laminar/radios/copilot/nav_tuned_no_id_fo", "number")



B738DR_tuned_nav1_type		= create_dataref("laminar/B738/nav/nav1_type", "number")
B738DR_tuned_nav2_type		= create_dataref("laminar/B738/nav/nav2_type", "number")
B738DR_tuned_nav1_no_id		= create_dataref("laminar/B738/nav/nav1_no_id", "number")
B738DR_tuned_nav2_no_id		= create_dataref("laminar/B738/nav/nav2_no_id", "number")
B738DR_tuned_nav1_crs		= create_dataref("laminar/B738/nav/nav1_crs", "number")
B738DR_tuned_nav2_crs		= create_dataref("laminar/B738/nav/nav2_crs", "number")
B738DR_tuned_nav1_decode	= create_dataref("laminar/B738/nav/nav1_decode", "number")
B738DR_tuned_nav2_decode	= create_dataref("laminar/B738/nav/nav2_decode", "number")

B738DR_hide_mmr			= create_dataref("laminar/B738/fms/hide_mmr", "number")

B738DR_mmr_cpt_act_mode		= create_dataref("laminar/B738/mmr/cpt/act_mode", "number")
B738DR_mmr_cpt_act_value	= create_dataref("laminar/B738/mmr/cpt/act_value", "number")
B738DR_mmr_cpt_stby_mode	= create_dataref("laminar/B738/mmr/cpt/stby_mode", "number")
B738DR_mmr_cpt_stby_value	= create_dataref("laminar/B738/mmr/cpt/stby_value", "array[5]")
B738DR_mmr_cpt_stby_cursor	= create_dataref("laminar/B738/mmr/cpt/stby_cursor", "number")
B738DR_mmr_cpt_stby_cur		= create_dataref("laminar/B738/mmr/cpt/stby_cur", "array[3]")
B738DR_mmr_cpt_stby_gls		= create_dataref("laminar/B738/mmr/cpt/stby_gls", "number")
B738DR_mmr_cpt_err			= create_dataref("laminar/B738/mmr/cpt/err", "number")

B738DR_mmr_fo_act_mode		= create_dataref("laminar/B738/mmr/fo/act_mode", "number")
B738DR_mmr_fo_act_value		= create_dataref("laminar/B738/mmr/fo/act_value", "number")
B738DR_mmr_fo_stby_mode		= create_dataref("laminar/B738/mmr/fo/stby_mode", "number")
B738DR_mmr_fo_stby_value	= create_dataref("laminar/B738/mmr/fo/stby_value", "array[5]")
B738DR_mmr_fo_stby_cursor	= create_dataref("laminar/B738/mmr/fo/stby_cursor", "number")
B738DR_mmr_fo_stby_cur		= create_dataref("laminar/B738/mmr/fo/stby_cur", "array[3]")
B738DR_mmr_fo_stby_gls		= create_dataref("laminar/B738/mmr/fo/stby_gls", "number")
B738DR_mmr_fo_err			= create_dataref("laminar/B738/mmr/fo/err", "number")

-- GLS 1
B738DR_gls1_active 			= create_dataref("laminar/B738/nav/gls1_active", "number")
B738DR_gls1_id 				= create_dataref("laminar/B738/nav/gls1_id", "string")
B738DR_gls1_idx				= create_dataref("laminar/B738/nav/gls1_idx", "number")
B738DR_gls1_dme 			= create_dataref("laminar/B738/nav/gls1_dme", "number")
B738DR_gls1_alt 			= create_dataref("laminar/B738/nav/gls1_alt", "number")
B738DR_gls1_rnw 			= create_dataref("laminar/B738/nav/gls1_rnw", "string")
B738DR_gls1_crs 			= create_dataref("laminar/B738/nav/gls1_crs", "number")
B738DR_gls1_crs_mag			= create_dataref("laminar/B738/nav/gls1_crs_mag", "number")
B738DR_gls1_horz_dev 		= create_dataref("laminar/B738/nav/gls1_horz_dev", "number")
B738DR_gls1_vert_dev 		= create_dataref("laminar/B738/nav/gls1_vert_dev", "number")
B738DR_gls1_xtrack			= create_dataref("laminar/B738/nav/gls1_xtrack", "number")
B738DR_gls1_type			= create_dataref("laminar/B738/nav/gls1_type", "number")

-- GLS 2
B738DR_gls2_active 			= create_dataref("laminar/B738/nav/gls2_active", "number")
B738DR_gls2_id 				= create_dataref("laminar/B738/nav/gls2_id", "string")
B738DR_gls2_idx				= create_dataref("laminar/B738/nav/gls2_idx", "number")
B738DR_gls2_dme 			= create_dataref("laminar/B738/nav/gls2_dme", "number")
B738DR_gls2_alt 			= create_dataref("laminar/B738/nav/gls2_alt", "number")
B738DR_gls2_rnw 			= create_dataref("laminar/B738/nav/gls2_rnw", "string")
B738DR_gls2_crs 			= create_dataref("laminar/B738/nav/gls2_crs", "number")
B738DR_gls2_crs_mag			= create_dataref("laminar/B738/nav/gls2_crs_mag", "number")
B738DR_gls2_horz_dev 		= create_dataref("laminar/B738/nav/gls2_horz_dev", "number")
B738DR_gls2_vert_dev 		= create_dataref("laminar/B738/nav/gls2_vert_dev", "number")
B738DR_gls2_xtrack			= create_dataref("laminar/B738/nav/gls2_xtrack", "number")
B738DR_gls2_type			= create_dataref("laminar/B738/nav/gls2_type", "number")

B738DR_lpv_app_status		= create_dataref("laminar/B738/nav/lpv_app_status", "number")
B738DR_lpv_gs_status		= create_dataref("laminar/B738/nav/lpv_gs_status", "number")

B738DR_mmr_act_mode 		= create_dataref("laminar/B738/nav/mmr_act_mode", "number")
B738DR_mmr_active 			= create_dataref("laminar/B738/nav/mmr_active", "number")
B738DR_mmr_channel			= create_dataref("laminar/B738/nav/mmr_channel", "number")
B738DR_mmr_id 				= create_dataref("laminar/B738/nav/mmr_id", "string")
B738DR_mmr_dme 				= create_dataref("laminar/B738/nav/mmr_dme", "number")
B738DR_mmr_rnw 				= create_dataref("laminar/B738/nav/mmr_rnw", "string")
B738DR_mmr_crs 				= create_dataref("laminar/B738/nav/mmr_crs", "number")
B738DR_mmr_crs_mag 			= create_dataref("laminar/B738/nav/mmr_crs_mag", "number")
B738DR_mmr_type 			= create_dataref("laminar/B738/nav/mmr_type", "number")
B738DR_mmr_vdef				= create_dataref("laminar/B738/nav/mmr_vdef", "number")
B738DR_mmr_hdef				= create_dataref("laminar/B738/nav/mmr_hdef", "number")
B738DR_mmr_no_id			= create_dataref("laminar/B738/nav/mmr_no_id", "number")
B738DR_mmr_act_mode_fo 		= create_dataref("laminar/B738/nav/mmr_act_mode_fo", "number")
B738DR_mmr_active_fo 		= create_dataref("laminar/B738/nav/mmr_active_fo", "number")
B738DR_mmr_channel_fo		= create_dataref("laminar/B738/nav/mmr_channel_fo", "number")
B738DR_mmr_id_fo 			= create_dataref("laminar/B738/nav/mmr_id_fo", "string")
B738DR_mmr_dme_fo 			= create_dataref("laminar/B738/nav/mmr_dme_fo", "number")
B738DR_mmr_rnw_fo 			= create_dataref("laminar/B738/nav/mmr_rnw_fo", "string")
B738DR_mmr_crs_fo 			= create_dataref("laminar/B738/nav/mmr_crs_fo", "number")
B738DR_mmr_crs_mag_fo 		= create_dataref("laminar/B738/nav/mmr_crs_mag_fo", "number")
B738DR_mmr_type_fo 			= create_dataref("laminar/B738/nav/mmr_type_fo", "number")
B738DR_mmr_vdef_fo 			= create_dataref("laminar/B738/nav/mmr_vdef_fo", "number")
B738DR_mmr_hdef_fo 			= create_dataref("laminar/B738/nav/mmr_hdef_fo", "number")
B738DR_mmr_no_id_fo			= create_dataref("laminar/B738/nav/mmr_no_id_fo", "number")

B738DR_autopilot_vhf_source_pos			= create_dataref("laminar/B738/toggle_switch/vhf_nav_source", "number")

-----------------

B738_legs_num			= create_dataref("laminar/B738/vnav/legs_num", "number")
B738_legs_num_before	= create_dataref("laminar/B738/vnav/legs_num_before", "number")
B738_legs_num_first		= create_dataref("laminar/B738/vnav/legs_num_first", "number")
B738_num_of_wpts		= create_dataref("laminar/B738/fms/num_of_wpts", "number")

B738DR_fpln_dist		= create_dataref("laminar/B738/FMS/fpln_dist", "number")
B738DR_fpln_dist2		= create_dataref("laminar/B738/FMS/fpln_dist2", "number")
dist_dest				= create_dataref("laminar/B738/FMS/dist_dest", "number")
time_dest				= create_dataref("laminar/B738/FMS/time_dest", "string")

B738DR_vnav_desc_spd_disable = create_dataref("laminar/B738/fms/vnav_desc_spd_disable", "number")
B738DR_act_wpt_gp			 = create_dataref("laminar/B738/fms/act_wpt_gp", "number")

B738DR_fmc_message 		= create_dataref("laminar/B738/fmc/fmc_message", "number")
B738DR_fmc_message2		= create_dataref("laminar/B738/fmc/fmc_message2", "number")
B738DR_fmc_message_warn = create_dataref("laminar/B738/fmc/fmc_message_warn", "number")

B738DR_fmc_offset 		= create_dataref("laminar/B738/fmc/fmc_offset", "number")
B738DR_fmc_dspy 		= create_dataref("laminar/B738/fmc/fmc_dspy", "number")
B738DR_fmc_fail 		= create_dataref("laminar/B738/fmc/fmc_fail", "number")
B738DR_fmc_offset2 		= create_dataref("laminar/B738/fmc/fmc_offset2", "number")
B738DR_fmc_dspy2 		= create_dataref("laminar/B738/fmc/fmc_dspy2", "number")
B738DR_fmc_fail2 		= create_dataref("laminar/B738/fmc/fmc_fail2", "number")

pfd_cpt_nav_txt1		= create_dataref("laminar/B738/pfd/cpt_nav_txt1", "string")
pfd_cpt_nav_txt2		= create_dataref("laminar/B738/pfd/cpt_nav_txt2", "string")
pfd_cpt_nav_txt1a		= create_dataref("laminar/B738/pfd/cpt_nav_txt1a", "string")
pfd_fo_nav_txt1			= create_dataref("laminar/B738/pfd/fo_nav_txt1", "string")
pfd_fo_nav_txt2			= create_dataref("laminar/B738/pfd/fo_nav_txt2", "string")
pfd_fo_nav_txt1a			= create_dataref("laminar/B738/pfd/fo_nav_txt1a", "string")

B738DR_rw_wind_dir		= create_dataref("laminar/B738/fms/rw_wind_dir", "number")
B738DR_rw_wind_spd		= create_dataref("laminar/B738/fms/rw_wind_spd", "number")
B738DR_rw_slope			= create_dataref("laminar/B738/fms/rw_slope", "number")
B738DR_rw_hdg			= create_dataref("laminar/B738/fms/rw_hdg", "number")

-- RUNWAYS
B738DR_ils_rotate	 	= create_dataref("laminar/B738/pfd/ils_rotate", "number")
B738DR_ils_x	 		= create_dataref("laminar/B738/pfd/ils_x", "number")
B738DR_ils_y	 		= create_dataref("laminar/B738/pfd/ils_y", "number")
B738DR_ils_runway 		= create_dataref("laminar/B738/pfd/ils_runway", "string")
B738DR_ils_show	 		= create_dataref("laminar/B738/pfd/ils_show", "number")
B738DR_ils_copilot_show	= create_dataref("laminar/B738/pfd/ils_copilot_show", "number")

B738DR_ils_rotate0	 		= create_dataref("laminar/B738/pfd/ils_rotate0", "number")
B738DR_ils_x0	 			= create_dataref("laminar/B738/pfd/ils_x0", "number")
B738DR_ils_y0	 			= create_dataref("laminar/B738/pfd/ils_y0", "number")
B738DR_ils_runway0 			= create_dataref("laminar/B738/pfd/ils_runway0", "string")
B738DR_ils_show0	 		= create_dataref("laminar/B738/pfd/ils_show0", "number")
B738DR_ils_copilot_show0	= create_dataref("laminar/B738/pfd/ils_copilot_show0", "number")

B738DR_ils_fo_rotate	 	= create_dataref("laminar/B738/pfd/ils_fo_rotate", "number")
B738DR_ils_fo_x	 		= create_dataref("laminar/B738/pfd/ils_fo_x", "number")
B738DR_ils_fo_y	 		= create_dataref("laminar/B738/pfd/ils_fo_y", "number")
B738DR_ils_fo_runway 		= create_dataref("laminar/B738/pfd/ils_fo_runway", "string")
B738DR_ils_fo_show	 		= create_dataref("laminar/B738/pfd/ils_fo_show", "number")
B738DR_ils_fo_copilot_show	= create_dataref("laminar/B738/pfd/ils_fo_copilot_show", "number")

B738DR_ils_fo_rotate0	 		= create_dataref("laminar/B738/pfd/ils_fo_rotate0", "number")
B738DR_ils_fo_x0	 			= create_dataref("laminar/B738/pfd/ils_fo_x0", "number")
B738DR_ils_fo_y0	 			= create_dataref("laminar/B738/pfd/ils_fo_y0", "number")
B738DR_ils_fo_runway0 			= create_dataref("laminar/B738/pfd/ils_fo_runway0", "string")
B738DR_ils_fo_show0	 		= create_dataref("laminar/B738/pfd/ils_fo_show0", "number")
B738DR_ils_fo_copilot_show0	= create_dataref("laminar/B738/pfd/ils_fo_copilot_show0", "number")

-- RUNWAYS MOD
B738DR_ils_rotate_mod	 	= create_dataref("laminar/B738/pfd/ils_rotate_mod", "number")
B738DR_ils_x_mod	 		= create_dataref("laminar/B738/pfd/ils_x_mod", "number")
B738DR_ils_y_mod	 		= create_dataref("laminar/B738/pfd/ils_y_mod", "number")
B738DR_ils_runway_mod 		= create_dataref("laminar/B738/pfd/ils_runway_mod", "string")
B738DR_ils_show_mod	 		= create_dataref("laminar/B738/pfd/ils_show_mod", "number")
B738DR_ils_copilot_show_mod	= create_dataref("laminar/B738/pfd/ils_copilot_show_mod", "number")

B738DR_ils_rotate0_mod	 		= create_dataref("laminar/B738/pfd/ils_rotate0_mod", "number")
B738DR_ils_x0_mod	 			= create_dataref("laminar/B738/pfd/ils_x0_mod", "number")
B738DR_ils_y0_mod	 			= create_dataref("laminar/B738/pfd/ils_y0_mod", "number")
B738DR_ils_runway0_mod 			= create_dataref("laminar/B738/pfd/ils_runway0_mod", "string")
B738DR_ils_show0_mod	 		= create_dataref("laminar/B738/pfd/ils_show0_mod", "number")
B738DR_ils_copilot_show0_mod	= create_dataref("laminar/B738/pfd/ils_copilot_show0_mod", "number")


B738DR_ils_fo_rotate_mod	 	= create_dataref("laminar/B738/pfd/ils_fo_rotate_mod", "number")
B738DR_ils_fo_x_mod	 		= create_dataref("laminar/B738/pfd/ils_fo_x_mod", "number")
B738DR_ils_fo_y_mod	 		= create_dataref("laminar/B738/pfd/ils_fo_y_mod", "number")
B738DR_ils_fo_runway_mod 		= create_dataref("laminar/B738/pfd/ils_fo_runway_mod", "string")
B738DR_ils_fo_show_mod	 		= create_dataref("laminar/B738/pfd/ils_fo_show_mod", "number")
B738DR_ils_fo_copilot_show_mod	= create_dataref("laminar/B738/pfd/ils_fo_copilot_show_mod", "number")

B738DR_ils_fo_rotate0_mod	 		= create_dataref("laminar/B738/pfd/ils_fo_rotate0_mod", "number")
B738DR_ils_fo_x0_mod	 			= create_dataref("laminar/B738/pfd/ils_fo_x0_mod", "number")
B738DR_ils_fo_y0_mod	 			= create_dataref("laminar/B738/pfd/ils_fo_y0_mod", "number")
B738DR_ils_fo_runway0_mod 			= create_dataref("laminar/B738/pfd/ils_fo_runway0_mod", "string")
B738DR_ils_fo_show0_mod	 		= create_dataref("laminar/B738/pfd/ils_fo_show0_mod", "number")
B738DR_ils_fo_copilot_show0_mod	= create_dataref("laminar/B738/pfd/ils_fo_copilot_show0_mod", "number")



-- HOLDs
B738DR_hold_x	 		= create_dataref("laminar/B738/nd/hold_x", "array[5]")
B738DR_hold_y	 		= create_dataref("laminar/B738/nd/hold_y", "array[5]")
B738DR_hold_crs	 		= create_dataref("laminar/B738/nd/hold_crs", "array[5]")
B738DR_hold_dist 		= create_dataref("laminar/B738/nd/hold_dist", "array[5]")
B738DR_hold_type 		= create_dataref("laminar/B738/nd/hold_type", "array[5]")

B738DR_hold_fo_x	 		= create_dataref("laminar/B738/nd/hold_fo_x", "array[5]")
B738DR_hold_fo_y	 		= create_dataref("laminar/B738/nd/hold_fo_y", "array[5]")
B738DR_hold_fo_crs	 		= create_dataref("laminar/B738/nd/hold_fo_crs", "array[5]")
B738DR_hold_fo_dist 		= create_dataref("laminar/B738/nd/hold_fo_dist", "array[5]")
B738DR_hold_fo_type 		= create_dataref("laminar/B738/nd/hold_fo_type", "array[5]")

function center_line_handl() end
B738DR_center_line			= create_dataref("laminar/B738/fms/center_line", "number", center_line_handl)
-- temporary
B7378DR_ctr_line_dist	= create_dataref("laminar/B738/fms/ctr_line_min_delta", "number")

B738DR_fms_light_pilot		= create_dataref("laminar/B738/push_button/fms_light_pilot", "number")
B738DR_fms_light_fo			= create_dataref("laminar/B738/push_button/fms_light_fo", "number")

-- LEGS as string
function B738DR_fms_legs_change_handler() end
function B738DR_fms_legs_change2_handler() end
function B738DR_fms_legs_change3_handler() end
B738DR_fms_legs_change			= create_dataref("laminar/B738/fms/legs_change", "number", B738DR_fms_legs_change_handler)
B738DR_fms_legs_change_2		= create_dataref("laminar/B738/fms/legs_change2", "number", B738DR_fms_legs_change2_handler)
B738DR_fms_legs_change_3		= create_dataref("laminar/B738/fms/legs_change3", "number", B738DR_fms_legs_change3_handler)

B738DR_fms_legs					= create_dataref("laminar/B738/fms/legs", "string")
B738DR_fms_legs_ext				= create_dataref("laminar/B738/fms/legs_ext", "string")
B738DR_fms_legs_lat 			= find_dataref("laminar/B738/fms/legs_lat")
B738DR_fms_legs_lon 			= find_dataref("laminar/B738/fms/legs_lon")
B738DR_fms_legs_type 			= find_dataref("laminar/B738/fms/legs_type")
B738DR_fms_legs_rte_type		= create_dataref("laminar/B738/fms/fms_legs_rte_type", "array[256]")
B738DR_fms_legs_alt_rest1 		= find_dataref("laminar/B738/fms/legs_alt_rest1")
B738DR_fms_legs_alt_rest2 		= find_dataref("laminar/B738/fms/legs_alt_rest2")
B738DR_fms_legs_alt_rest_type 	= find_dataref("laminar/B738/fms/legs_alt_rest_type")
B738DR_fms_legs_alt_calc		= find_dataref("laminar/B738/fms/legs_alt_calc")
B738DR_fms_legs_spd_calc		= find_dataref("laminar/B738/fms/legs_spd_calc")
B738DR_fms_legs_crs_mag			= find_dataref("laminar/B738/fms/legs_crs_mag")
B738DR_fms_legs_eta				= find_dataref("laminar/B738/fms/legs_eta")
B738DR_fms_legs_rnp				= find_dataref("laminar/B738/fms/legs_rnp")
B738DR_fms_legs_rnp_data		= find_dataref("laminar/B738/fms/legs_rnp_data")
B738DR_fms_legs_radii_dta		= find_dataref("laminar/B738/fms/legs_radii_dta")
B738DR_fms_legs_radii_radius	= find_dataref("laminar/B738/fms/legs_radii_radius")
B738DR_fms_legs_radii_ctr_lat	= find_dataref("laminar/B738/fms/legs_radii_ctr_lat")
B738DR_fms_legs_radii_ctr_lon	= find_dataref("laminar/B738/fms/legs_radii_ctr_lon")
B738DR_fms_legs_radii_lat2		= find_dataref("laminar/B738/fms/legs_radii_lat2")
B738DR_fms_legs_radii_lon2		= find_dataref("laminar/B738/fms/legs_radii_lon2")
B738DR_fms_legs_turn			= find_dataref("laminar/B738/fms/legs_turn")
B738DR_fms_legs_df_crs			= find_dataref("laminar/B738/fms/legs_df_crs")
B738DR_fms_legs_df_crs_fin		= find_dataref("laminar/B738/fms/legs_df_crs_fin")
B738DR_fms_legs_bypass			= find_dataref("laminar/B738/fms/legs_bypass")
B738DR_fms_legs_af_beg			= find_dataref("laminar/B738/fms/legs_af_beg")
B738DR_fms_legs_af_end			= find_dataref("laminar/B738/fms/legs_af_end")


B738DR_fms_legs_seg1_ctr_lat		= find_dataref("laminar/B738/fms/legs_seg1_ctr_lat")
B738DR_fms_legs_seg1_ctr_lon		= find_dataref("laminar/B738/fms/legs_seg1_ctr_lon")
B738DR_fms_legs_seg1_radius			= find_dataref("laminar/B738/fms/legs_seg1_radius")
B738DR_fms_legs_seg1_turn			= find_dataref("laminar/B738/fms/legs_seg1_turn")
B738DR_fms_legs_seg1_start_angle	= find_dataref("laminar/B738/fms/legs_seg1_start_angle")
B738DR_fms_legs_seg1_end_angle		= find_dataref("laminar/B738/fms/legs_seg1_end_angle")

B738DR_fms_legs_seg3_ctr_lat		= find_dataref("laminar/B738/fms/legs_seg3_ctr_lat")
B738DR_fms_legs_seg3_ctr_lon		= find_dataref("laminar/B738/fms/legs_seg3_ctr_lon")
B738DR_fms_legs_seg3_radius			= find_dataref("laminar/B738/fms/legs_seg3_radius")
B738DR_fms_legs_seg3_turn			= find_dataref("laminar/B738/fms/legs_seg3_turn")
B738DR_fms_legs_seg3_start_angle	= find_dataref("laminar/B738/fms/legs_seg3_start_angle")
B738DR_fms_legs_seg3_end_angle		= find_dataref("laminar/B738/fms/legs_seg3_end_angle")

B738DR_fms_legs_seg2_crs_true		= find_dataref("laminar/B738/fms/legs_seg2_crs_true")

B738DR_fms_legs_seg1_start_lat		= find_dataref("laminar/B738/fms/legs_seg1_start_lat")
B738DR_fms_legs_seg1_start_lon		= find_dataref("laminar/B738/fms/legs_seg1_start_lon")
B738DR_fms_legs_seg1_end_lat		= find_dataref("laminar/B738/fms/legs_seg1_end_lat")
B738DR_fms_legs_seg1_end_lon		= find_dataref("laminar/B738/fms/legs_seg1_end_lon")
B738DR_fms_legs_seg2_end_lat		= find_dataref("laminar/B738/fms/legs_seg2_end_lat")
B738DR_fms_legs_seg2_end_lon		= find_dataref("laminar/B738/fms/legs_seg2_end_lon")
B738DR_fms_legs_seg3_end_lat		= find_dataref("laminar/B738/fms/legs_seg3_end_lat")
B738DR_fms_legs_seg3_end_lon		= find_dataref("laminar/B738/fms/legs_seg3_end_lon")





B738DR_fms_dir_seg1_ctr_lat		= find_dataref("laminar/B738/fms/dir_seg1_ctr_lat")
B738DR_fms_dir_seg1_ctr_lon		= find_dataref("laminar/B738/fms/dir_seg1_ctr_lon")
B738DR_fms_dir_seg1_radius		= find_dataref("laminar/B738/fms/dir_seg1_radius")
B738DR_fms_dir_seg1_turn		= find_dataref("laminar/B738/fms/dir_seg1_turn")
B738DR_fms_dir_seg1_start_angle	= find_dataref("laminar/B738/fms/dir_seg1_start_angle")
B738DR_fms_dir_seg1_end_angle	= find_dataref("laminar/B738/fms/dir_seg1_end_angle")

B738DR_fms_dir_seg3_ctr_lat		= find_dataref("laminar/B738/fms/dir_seg3_ctr_lat")
B738DR_fms_dir_seg3_ctr_lon		= find_dataref("laminar/B738/fms/dir_seg3_ctr_lon")
B738DR_fms_dir_seg3_radius		= find_dataref("laminar/B738/fms/dir_seg3_radius")
B738DR_fms_dir_seg3_turn		= find_dataref("laminar/B738/fms/dir_seg3_turn")
B738DR_fms_dir_seg3_start_angle	= find_dataref("laminar/B738/fms/dir_seg3_start_angle")
B738DR_fms_dir_seg3_end_angle	= find_dataref("laminar/B738/fms/dir_seg3_end_angle")

B738DR_fms_dir_seg2_crs_true	= find_dataref("laminar/B738/fms/dir_seg2_crs_true")

B738DR_fms_dir_seg1_start_lat	= find_dataref("laminar/B738/fms/dir_seg1_start_lat")
B738DR_fms_dir_seg1_start_lon	= find_dataref("laminar/B738/fms/dir_seg1_start_lon")
B738DR_fms_dir_seg1_end_lat		= find_dataref("laminar/B738/fms/dir_seg1_end_lat")
B738DR_fms_dir_seg1_end_lon		= find_dataref("laminar/B738/fms/dir_seg1_end_lon")
B738DR_fms_dir_seg2_end_lat		= find_dataref("laminar/B738/fms/dir_seg2_end_lat")
B738DR_fms_dir_seg2_end_lon		= find_dataref("laminar/B738/fms/dir_seg2_end_lon")
B738DR_fms_dir_seg3_end_lat		= find_dataref("laminar/B738/fms/dir_seg3_end_lat")
B738DR_fms_dir_seg3_end_lon		= find_dataref("laminar/B738/fms/dir_seg3_end_lon")



B738DR_fms_legs_offset			= find_dataref("laminar/B738/fms/legs_offset")
B738DR_fms_legs_offset_lat 		= find_dataref("laminar/B738/fms/legs_offset_lat")
B738DR_fms_legs_offset_lon 		= find_dataref("laminar/B738/fms/legs_offset_lon")

function offset_act_handl() end
offset_act		= create_dataref("laminar/B738/fms/legs_offset_act", "number", offset_act_handl)
-- B738DR_fms_legs_offset_act		= create_dataref("laminar/B738/fms/legs_offset_act", "number", offset_act_handl)
B738DR_fms_legs_offset_dist		= create_dataref("laminar/B738/fms/legs_offset_dist", "number")
B738DR_fms_legs_offset_engage	= create_dataref("laminar/B738/fms/legs_offset_engage", "number")
B738DR_fms_legs_offset_start	= create_dataref("laminar/B738/fms/legs_offset_start", "number")
B738DR_fms_legs_offset_end		= create_dataref("laminar/B738/fms/legs_offset_end", "number")
B738DR_fms_legs_offset_start_t	= create_dataref("laminar/B738/fms/legs_offset_start_t", "number")
B738DR_fms_legs_offset_act_mod		= create_dataref("laminar/B738/fms/legs_offset_act_mod", "number")
B738DR_fms_legs_offset_start_mod	= create_dataref("laminar/B738/fms/legs_offset_start_mod", "number")
B738DR_fms_legs_offset_end_mod		= create_dataref("laminar/B738/fms/legs_offset_end_mod", "number")
B738DR_fms_legs_offset_start_t_mod	= create_dataref("laminar/B738/fms/legs_offset_start_t_mod", "number")
B738DR_fms_legs_offset_latx_1	= create_dataref("laminar/B738/fms/legs_offset_latx_1", "number")
B738DR_fms_legs_offset_lonx_1	= create_dataref("laminar/B738/fms/legs_offset_lonx_1", "number")
B738DR_fms_legs_offset_latx_2	= create_dataref("laminar/B738/fms/legs_offset_latx_2", "number")
B738DR_fms_legs_offset_lonx_2	= create_dataref("laminar/B738/fms/legs_offset_lonx_2", "number")
B738DR_fms_legs_offset_latx_3	= create_dataref("laminar/B738/fms/legs_offset_latx_3", "number")
B738DR_fms_legs_offset_lonx_3	= create_dataref("laminar/B738/fms/legs_offset_lonx_3", "number")
B738DR_fms_legs_offset_latx_4	= create_dataref("laminar/B738/fms/legs_offset_latx_4", "number")
B738DR_fms_legs_offset_lonx_4	= create_dataref("laminar/B738/fms/legs_offset_lonx_4", "number")
B738DR_fms_legs_offset_latx_1_mod	= create_dataref("laminar/B738/fms/legs_offset_latx_1_mod", "number")
B738DR_fms_legs_offset_lonx_1_mod	= create_dataref("laminar/B738/fms/legs_offset_lonx_1_mod", "number")
B738DR_fms_legs_offset_latx_2_mod	= create_dataref("laminar/B738/fms/legs_offset_latx_2_mod", "number")
B738DR_fms_legs_offset_lonx_2_mod	= create_dataref("laminar/B738/fms/legs_offset_lonx_2_mod", "number")
B738DR_fms_legs_offset_latx_3_mod	= create_dataref("laminar/B738/fms/legs_offset_latx_3_mod", "number")
B738DR_fms_legs_offset_lonx_3_mod	= create_dataref("laminar/B738/fms/legs_offset_lonx_3_mod", "number")
B738DR_fms_legs_offset_latx_4_mod	= create_dataref("laminar/B738/fms/legs_offset_latx_4_mod", "number")
B738DR_fms_legs_offset_lonx_4_mod	= create_dataref("laminar/B738/fms/legs_offset_lonx_4_mod", "number")

function B738DR_fms_next_predict_handler() end
B738DR_next_predict				= create_dataref("laminar/B738/fms/next_predict", "number", B738DR_fms_next_predict_handler)

B738DR_fms_legs_pth				= create_dataref("laminar/B738/fms/legs_pth", "string")
B738DR_fms_legs_pth_before		= create_dataref("laminar/B738/fms/legs_pth_before", "string")
B738DR_fms_legs_dist			= find_dataref("laminar/B738/fms/legs_dist")
B738DR_fms_legs_brg_true		= find_dataref("laminar/B738/fms/legs_brg_true")
B738DR_fms_legs_brg_mag			= find_dataref("laminar/B738/fms/legs_brg_mag")
B738DR_fms_legs_brg_fin_true	= find_dataref("laminar/B738/fms/legs_brg_fin_true")
B738DR_fms_legs_radius			= find_dataref("laminar/B738/fms/legs_radius")
B738DR_fms_legs_rad_lat			= find_dataref("laminar/B738/fms/legs_rad_lat")
B738DR_fms_legs_rad_lon			= find_dataref("laminar/B738/fms/legs_rad_lon")
B738DR_fms_legs_rad_turn		= find_dataref("laminar/B738/fms/legs_rad_turn")
B738DR_fms_legs_spd				= find_dataref("laminar/B738/fms/legs_spd")
B738DR_fms_legs_spd_type		= find_dataref("laminar/B738/fms/legs_spd_type")
B738DR_fms_legs_hold_time		= find_dataref("laminar/B738/fms/legs_hold_time")
B738DR_fms_legs_hold_dist		= find_dataref("laminar/B738/fms/legs_hold_dist")
B738DR_fms_legs_before			= find_dataref("laminar/B738/fms/legs_before")
B738DR_fms_legs_max_spd		 	= find_dataref("laminar/B738/fms/legs_max_spd")
B738DR_fms_legs_min_spd 		= find_dataref("laminar/B738/fms/legs_min_spd")

B738DR_fms_legs_num2				= create_dataref("laminar/B738/fms/legs_num2", "number")
B738_num_of_wpts_2					= create_dataref("laminar/B738/fms/num_of_wpts_2", "number")
B738DR_fms_legs_2					= create_dataref("laminar/B738/fms/legs_2", "string")
B738DR_fms_legs_ext_2				= create_dataref("laminar/B738/fms/legs_ext_2", "string")
B738DR_fms_legs_lat_2 				= create_dataref("laminar/B738/fms/legs_lat_2", "array[256]")
B738DR_fms_legs_lon_2 				= create_dataref("laminar/B738/fms/legs_lon_2", "array[256]")
B738DR_fms_legs_type_2 				= create_dataref("laminar/B738/fms/legs_type_2", "array[256]")
B738DR_fms_legs_rte_type_2			= create_dataref("laminar/B738/fms/fms_legs_rte_type_2", "array[256]")
B738DR_fms_legs_alt_rest1_2 		= create_dataref("laminar/B738/fms/legs_alt_rest1_2", "array[256]")
B738DR_fms_legs_alt_rest2_2 		= create_dataref("laminar/B738/fms/legs_alt_rest2_2", "array[256]")
B738DR_fms_legs_alt_rest_type_2 	= create_dataref("laminar/B738/fms/legs_alt_rest_type_2", "array[256]")
B738DR_fms_legs_alt_calc_2			= create_dataref("laminar/B738/fms/legs_alt_calc_2", "array[256]")
B738DR_fms_legs_crs_mag_2			= create_dataref("laminar/B738/fms/legs_crs_mag_2", "array[256]")

B738DR_fms_legs_pth_2			= create_dataref("laminar/B738/fms/legs_pth_2", "string")
B738DR_fms_legs_pth_before_2	= create_dataref("laminar/B738/fms/legs_pth_before_2", "string")
B738DR_fms_legs_dist_2			= create_dataref("laminar/B738/fms/legs_dist_2", "array[256]")
B738DR_fms_legs_brg_true_2		= create_dataref("laminar/B738/fms/legs_brg_true_2", "array[256]")
B738DR_fms_legs_brg_mag_2		= create_dataref("laminar/B738/fms/legs_brg_mag_2", "array[256]")
B738DR_fms_legs_brg_fin_true_2	= create_dataref("laminar/B738/fms/legs_brg_fin_true_2", "array[256]")
B738DR_fms_legs_radius_2		= create_dataref("laminar/B738/fms/legs_radius_2", "array[256]")
B738DR_fms_legs_rad_lat_2		= create_dataref("laminar/B738/fms/legs_rad_lat_2", "array[256]")
B738DR_fms_legs_rad_lon_2		= create_dataref("laminar/B738/fms/legs_rad_lon_2", "array[256]")
B738DR_fms_legs_rad_turn_2		= create_dataref("laminar/B738/fms/legs_rad_turn_2", "array[256]")
B738DR_fms_legs_wpt_type_2		= create_dataref("laminar/B738/fms/legs_wpt_type_2", "array[256]")
B738DR_fms_legs_spd_2			= create_dataref("laminar/B738/fms/legs_spd_2", "array[256]")
B738DR_fms_legs_hold_time_2		= create_dataref("laminar/B738/fms/legs_hold_time_2", "array[256]")
B738DR_fms_legs_hold_dist_2		= create_dataref("laminar/B738/fms/legs_hold_dist_2", "array[256]")
B738DR_fms_legs_before_2		= create_dataref("laminar/B738/fms/legs_before_2", "array[256]")
B738DR_fms_legs_idx_2			= create_dataref("laminar/B738/fms/legs_idx_2", "array[256]")

B738DR_fms_legs_offset_2 		= create_dataref("laminar/B738/fms/legs_offset_2", "array[256]")
B738DR_fms_legs_offset_lat_2 	= create_dataref("laminar/B738/fms/legs_offset_lat_2", "array[256]")
B738DR_fms_legs_offset_lon_2 	= create_dataref("laminar/B738/fms/legs_offset_lon_2", "array[256]")

B738DR_fms_legs_num3				= create_dataref("laminar/B738/fms/legs_num3", "number")
B738_num_of_wpts_3					= create_dataref("laminar/B738/fms/num_of_wpts_3", "number")
B738DR_fms_legs_3					= create_dataref("laminar/B738/fms/legs_3", "string")
B738DR_fms_legs_ext_3				= create_dataref("laminar/B738/fms/legs_ext_3", "string")
B738DR_fms_legs_lat_3 				= create_dataref("laminar/B738/fms/legs_lat_3", "array[256]")
B738DR_fms_legs_lon_3 				= create_dataref("laminar/B738/fms/legs_lon_3", "array[256]")
B738DR_fms_legs_type_3 				= create_dataref("laminar/B738/fms/legs_type_3", "array[256]")
B738DR_fms_legs_rte_type_3			= create_dataref("laminar/B738/fms/fms_legs_rte_type_3", "array[256]")
B738DR_fms_legs_alt_rest1_3 		= create_dataref("laminar/B738/fms/legs_alt_rest1_3", "array[256]")
B738DR_fms_legs_alt_rest2_3 		= create_dataref("laminar/B738/fms/legs_alt_rest2_3", "array[256]")
B738DR_fms_legs_alt_rest_type_3 	= create_dataref("laminar/B738/fms/legs_alt_rest_type_3", "array[256]")
B738DR_fms_legs_alt_calc_3			= create_dataref("laminar/B738/fms/legs_alt_calc_3", "array[256]")
B738DR_fms_legs_crs_mag_3			= create_dataref("laminar/B738/fms/legs_crs_mag_3", "array[256]")

B738DR_fms_legs_pth_3			= create_dataref("laminar/B738/fms/legs_pth_3", "string")
B738DR_fms_legs_pth_before_3	= create_dataref("laminar/B738/fms/legs_pth_before_3", "string")
B738DR_fms_legs_dist_3			= create_dataref("laminar/B738/fms/legs_dist_3", "array[256]")
B738DR_fms_legs_brg_true_3		= create_dataref("laminar/B738/fms/legs_brg_true_3", "array[256]")
B738DR_fms_legs_brg_mag_3		= create_dataref("laminar/B738/fms/legs_brg_mag_3", "array[256]")
B738DR_fms_legs_brg_fin_true_3	= create_dataref("laminar/B738/fms/legs_brg_fin_true_3", "array[256]")
B738DR_fms_legs_radius_3		= create_dataref("laminar/B738/fms/legs_radius_3", "array[256]")
B738DR_fms_legs_rad_lat_3		= create_dataref("laminar/B738/fms/legs_rad_lat_3", "array[256]")
B738DR_fms_legs_rad_lon_3		= create_dataref("laminar/B738/fms/legs_rad_lon_3", "array[256]")
B738DR_fms_legs_rad_turn_3		= create_dataref("laminar/B738/fms/legs_rad_turn_3", "array[256]")
B738DR_fms_legs_wpt_type_3		= create_dataref("laminar/B738/fms/legs_wpt_type_3", "array[256]")
B738DR_fms_legs_spd_3			= create_dataref("laminar/B738/fms/legs_spd_3", "array[256]")
B738DR_fms_legs_hold_time_3		= create_dataref("laminar/B738/fms/legs_hold_time_3", "array[256]")
B738DR_fms_legs_hold_dist_3		= create_dataref("laminar/B738/fms/legs_hold_dist_3", "array[256]")
B738DR_fms_legs_before_3		= create_dataref("laminar/B738/fms/legs_before_3", "array[256]")
B738DR_fms_legs_idx_3			= create_dataref("laminar/B738/fms/legs_idx_3", "array[256]")

B738DR_fms_legs_offset_3 		= create_dataref("laminar/B738/fms/legs_offset_3", "array[256]")
B738DR_fms_legs_offset_lat_3 	= create_dataref("laminar/B738/fms/legs_offset_lat_3", "array[256]")
B738DR_fms_legs_offset_lon_3 	= create_dataref("laminar/B738/fms/legs_offset_lon_3", "array[256]")

B738DR_fms_rte12_act			= create_dataref("laminar/B738/fms/rte12_act", "number")
B738DR_fms_rte12_sel			= create_dataref("laminar/B738/fms/rte12_sel", "number")
B738DR_fms_rte12_sel_fo			= create_dataref("laminar/B738/fms/rte12_sel_fo", "number")


function B738DR_fms_legs_df_crs_2_handler() end
B738DR_fms_legs_df_crs_2		= create_dataref("laminar/B738/fms/legs_df_crs_2", "array[256]", B738DR_fms_legs_df_crs_2_handler)

B738DR_fms_legs_dist_t			= find_dataref("laminar/B738/fms/legs_dist_true")
B738DR_fms_legs_dist_dest_t		= find_dataref("laminar/B738/fms/legs_dist_dest_true")
B738DR_fms_legs_dist_dest_t_bp	= find_dataref("laminar/B738/fms/legs_dist_dest_true_bp")


function fms_add_cmd_handler() end
function fms_add_data1_handler() end
function fms_add_data2_handler() end
function fms_add_data3_handler() end
function fms_add_data4_handler() end
function fms_add_data5_handler() end
function fms_add_data6_handler() end
function fms_add_data7_handler() end
function fms_add_data8_handler() end
function fms_add_data9_handler() end
function fms_add_data10_handler() end
B738DR_fms_add_cmd				= create_dataref("laminar/B738/fms/fms_add_cmd", "number", fms_add_cmd_handler)
B738DR_fms_add_data_num			= create_dataref("laminar/B738/fms/fms_add_data_num", "number", fms_add_cmd_handler)
B738DR_fms_add_data_1			= create_dataref("laminar/B738/fms/fms_add_data_1", "array[128]", fms_add_data1_handler)
B738DR_fms_add_data_2			= create_dataref("laminar/B738/fms/fms_add_data_2", "array[128]", fms_add_data2_handler)
B738DR_fms_add_data_3			= create_dataref("laminar/B738/fms/fms_add_data_3", "array[128]", fms_add_data3_handler)
B738DR_fms_add_data_4			= create_dataref("laminar/B738/fms/fms_add_data_4", "array[128]", fms_add_data4_handler)
B738DR_fms_add_data_5			= create_dataref("laminar/B738/fms/fms_add_data_5", "array[128]", fms_add_data5_handler)
B738DR_fms_add_data_6			= create_dataref("laminar/B738/fms/fms_add_data_6", "array[128]", fms_add_data6_handler)
B738DR_fms_add_data_7			= create_dataref("laminar/B738/fms/fms_add_data_7", "array[128]", fms_add_data7_handler)
B738DR_fms_add_data_8			= create_dataref("laminar/B738/fms/fms_add_data_8", "array[128]", fms_add_data8_handler)
B738DR_fms_add_data_9			= create_dataref("laminar/B738/fms/fms_add_data_9", "array[128]", fms_add_data9_handler)
B738DR_fms_add_data_10			= create_dataref("laminar/B738/fms/fms_add_data_10", "array[128]", fms_add_data10_handler)

B738DR_legs_step					= create_dataref("laminar/B738/fms/legs_step_ctr", "number")
B738DR_legs_step2					= create_dataref("laminar/B738/fms/legs_step_ctr_fo", "number")

B738DR_legs_abeam_lat		= create_dataref("laminar/B738/fms/legs_abeam_lat", "array[256]")
B738DR_legs_abeam_lon		= create_dataref("laminar/B738/fms/legs_abeam_lon", "array[256]")
B738DR_legs_abeam_new_id	= create_dataref("laminar/B738/fms/legs_abeam_new_id", "string")
B738DR_legs_abeam_num		= create_dataref("laminar/B738/fms/legs_abeam_num", "number")
legs_abeam_selected			= create_dataref("laminar/B738/fms/legs_abeam_selected", "number")
function B738DR_legs_abeam_lat2_handler() end
function B738DR_legs_abeam_lon2_handler() end
function B738DR_legs_abeam_dist_handler() end
function B738DR_legs_abeam_avail_handler() end
B738DR_legs_abeam_lat2		= create_dataref("laminar/B738/fms/legs_abeam_lat2", "array[256]", B738DR_legs_abeam_lat2_handler)
B738DR_legs_abeam_lon2		= create_dataref("laminar/B738/fms/legs_abeam_lon2", "array[256]", B738DR_legs_abeam_lon2_handler)
B738DR_legs_abeam_dist		= create_dataref("laminar/B738/fms/legs_abeam_dist", "array[256]", B738DR_legs_abeam_dist_handler)
B738DR_legs_abeam_avail		= create_dataref("laminar/B738/fms/legs_abeam_avail", "number", B738DR_legs_abeam_avail_handler)

ref_icao_alt				= create_dataref("laminar/B738/fms/ref_icao_alt", "number")
des_icao_alt				= create_dataref("laminar/B738/fms/des_icao_alt", "number")

B738DR_wind_show			= create_dataref("laminar/B738/nd/wind_show", "number")

B738DR_nd_navaid			= create_dataref("laminar/B738/nd/disable_navaid", "number")
B738DR_nd_apt				= create_dataref("laminar/B738/nd/disable_apt", "number")

B738DR_last_pos_lat			= create_dataref("laminar/B738/irs/last_pos_lat", "number")
B738DR_last_pos_lon			= create_dataref("laminar/B738/irs/last_pos_lon", "number")

vnav_alt_err_ratio			= create_dataref("laminar/B738/fms/vnav_alt_err_ratio", "number")
gp_alt_err_ratio			= create_dataref("laminar/B738/fms/gp_alt_err_ratio", "number")
vnav_alt_err_ratio2			= create_dataref("laminar/B738/fms/vnav_alt_err_ratio2", "number")
gp_alt_err_ratio2			= create_dataref("laminar/B738/fms/gp_alt_err_ratio2", "number")

cross_wind					= create_dataref("laminar/B738/fms/cross_wind", "number")

B738DR_ref_runway_len 			= create_dataref("laminar/B738/fms/ref_runway_len", "number")
B738DR_ref_runway_crs 			= create_dataref("laminar/B738/fms/ref_runway_crs", "number")
B738DR_ref_runway_start_lat 	= create_dataref("laminar/B738/fms/ref_runway_start_lat", "number")
B738DR_ref_runway_start_lon 	= create_dataref("laminar/B738/fms/ref_runway_start_lon", "number")
B738DR_ref_runway_end_lat 		= create_dataref("laminar/B738/fms/ref_runway_end_lat", "number")
B738DR_ref_runway_end_lon 		= create_dataref("laminar/B738/fms/ref_runway_end_lon", "number")

B738DR_dest_runway_alt			= create_dataref("laminar/B738/fms/dest_runway_alt", "number")
B738DR_dest_runway_len 			= create_dataref("laminar/B738/fms/dest_runway_len", "number")
B738DR_dest_runway_crs 			= create_dataref("laminar/B738/fms/dest_runway_crs", "number")
B738DR_dest_runway_start_lat 	= create_dataref("laminar/B738/fms/dest_runway_start_lat", "number")
B738DR_dest_runway_start_lon 	= create_dataref("laminar/B738/fms/dest_runway_start_lon", "number")
B738DR_dest_runway_end_lat 		= create_dataref("laminar/B738/fms/dest_runway_end_lat", "number")
B738DR_dest_runway_end_lon 		= create_dataref("laminar/B738/fms/dest_runway_end_lon", "number")

B738DR_ref_runway_len_mod 			= create_dataref("laminar/B738/fms/ref_runway_len_mod", "number")
B738DR_ref_runway_crs_mod 			= create_dataref("laminar/B738/fms/ref_runway_crs_mod", "number")
B738DR_ref_runway_start_lat_mod 	= create_dataref("laminar/B738/fms/ref_runway_start_lat_mod", "number")
B738DR_ref_runway_start_lon_mod 	= create_dataref("laminar/B738/fms/ref_runway_start_lon_mod", "number")
B738DR_ref_runway_end_lat_mod 		= create_dataref("laminar/B738/fms/ref_runway_end_lat_mod", "number")
B738DR_ref_runway_end_lon_mod 		= create_dataref("laminar/B738/fms/ref_runway_end_lon_mod", "number")

B738DR_dest_runway_alt_mod			= create_dataref("laminar/B738/fms/dest_runway_alt_mod", "number")
B738DR_dest_runway_len_mod 			= create_dataref("laminar/B738/fms/dest_runway_len_mod", "number")
B738DR_dest_runway_crs_mod 			= create_dataref("laminar/B738/fms/dest_runway_crs_mod", "number")
B738DR_dest_runway_start_lat_mod 	= create_dataref("laminar/B738/fms/dest_runway_start_lat_mod", "number")
B738DR_dest_runway_start_lon_mod 	= create_dataref("laminar/B738/fms/dest_runway_start_lon_mod", "number")
B738DR_dest_runway_end_lat_mod 		= create_dataref("laminar/B738/fms/dest_runway_end_lat_mod", "number")
B738DR_dest_runway_end_lon_mod 		= create_dataref("laminar/B738/fms/dest_runway_end_lon_mod", "number")

B738DR_ref_runway_len_mod2 			= create_dataref("laminar/B738/fms/ref_runway_len_mod2", "number")
B738DR_ref_runway_crs_mod2 			= create_dataref("laminar/B738/fms/ref_runway_crs_mod2", "number")
B738DR_ref_runway_start_lat_mod2 	= create_dataref("laminar/B738/fms/ref_runway_start_lat_mod2", "number")
B738DR_ref_runway_start_lon_mod2 	= create_dataref("laminar/B738/fms/ref_runway_start_lon_mod2", "number")
B738DR_ref_runway_end_lat_mod2 		= create_dataref("laminar/B738/fms/ref_runway_end_lat_mod2", "number")
B738DR_ref_runway_end_lon_mod2 		= create_dataref("laminar/B738/fms/ref_runway_end_lon_mod2", "number")

B738DR_dest_runway_alt_mod2			= create_dataref("laminar/B738/fms/dest_runway_alt_mod2", "number")
B738DR_dest_runway_len_mod2 		= create_dataref("laminar/B738/fms/dest_runway_len_mod2", "number")
B738DR_dest_runway_crs_mod2 		= create_dataref("laminar/B738/fms/dest_runway_crs_mod2", "number")
B738DR_dest_runway_start_lat_mod2 	= create_dataref("laminar/B738/fms/dest_runway_start_lat_mod2", "number")
B738DR_dest_runway_start_lon_mod2 	= create_dataref("laminar/B738/fms/dest_runway_start_lon_mod2", "number")
B738DR_dest_runway_end_lat_mod2 	= create_dataref("laminar/B738/fms/dest_runway_end_lat_mod2", "number")
B738DR_dest_runway_end_lon_mod2 	= create_dataref("laminar/B738/fms/dest_runway_end_lon_mod2", "number")

des_runway_lat_nd 				= create_dataref("laminar/B738/fms/des_runway_start_lat", "number")
des_runway_lon_nd 				= create_dataref("laminar/B738/fms/des_runway_start_lon", "number")
des_runway_crs_nd 				= create_dataref("laminar/B738/fms/des_runway_crs", "number")
des_runway_lenght_nd 			= create_dataref("laminar/B738/fms/des_runway_len", "number")

des_runway_lat_nd_mod  			= create_dataref("laminar/B738/fms/des_runway_start_lat_mod", "number")
des_runway_lon_nd_mod  			= create_dataref("laminar/B738/fms/des_runway_start_lon_mod", "number")
des_runway_crs_nd_mod  			= create_dataref("laminar/B738/fms/des_runway_crs_mod", "number")
des_runway_lenght_nd_mod  		= create_dataref("laminar/B738/fms/des_runway_len_mod", "number")

des_runway_lat_nd_mod2  		= create_dataref("laminar/B738/fms/des_runway_start_lat_mod2", "number")
des_runway_lon_nd_mod2  		= create_dataref("laminar/B738/fms/des_runway_start_lon_mod2", "number")
des_runway_crs_nd_mod2  		= create_dataref("laminar/B738/fms/des_runway_crs_mod2", "number")
des_runway_lenght_nd_mod2  		= create_dataref("laminar/B738/fms/des_runway_len_mod2", "number")


B738DR_lnav_prior_arm		= create_dataref("laminar/B738/fms/lnav_prior_arm", "number")

B738DR_ref_rwy_altitude 	= create_dataref("laminar/B738/pfd/ref_rwy_altitude", "number")
B738DR_des_rwy_altitude 	= create_dataref("laminar/B738/pfd/des_rwy_altitude", "number")
B738DR_pfd_rwy_altitude 	= create_dataref("laminar/B738/pfd/rwy_altitude", "number")
B738DR_pfd_rwy_show		 	= create_dataref("laminar/B738/pfd/rwy_show", "number")

B738DR_dest_icao			= create_dataref("laminar/B738/fms/dest_icao", "string")
B738DR_dest_fuel			= create_dataref("laminar/B738/fms/dest_fuel", "number")
B738DR_tocw					= create_dataref("laminar/B738/fms/tocw", "number")

B738DR_legs_mod_active		= create_dataref("laminar/B738/fms/legs_mod_active", "number")
new_hold_at					= create_dataref("laminar/B738/fms/new_hold_at", "number")
new_hold_at_id				= create_dataref("laminar/B738/fms/new_hold_at_id", "string")
new_hold_at_lat				= create_dataref("laminar/B738/fms/new_hold_at_lat", "number")
new_hold_at_lon				= create_dataref("laminar/B738/fms/new_hold_at_lon", "number")
new_hold_at_crs_mag			= create_dataref("laminar/B738/fms/new_hold_at_crs_mag", "number")
new_hold_at_turn			= create_dataref("laminar/B738/fms/new_hold_at_turn", "number")
new_hold_at_speed			= create_dataref("laminar/B738/fms/new_hold_at_speed", "number")
new_hold_at_altitude		= create_dataref("laminar/B738/fms/new_hold_at_altitude", "number")
new_hold_at_time			= create_dataref("laminar/B738/fms/new_hold_at_time", "number")
new_hold_at_distance		= create_dataref("laminar/B738/fms/new_hold_at_distance", "number")

new_hold_at2				= create_dataref("laminar/B738/fms/new_hold_at2", "number")
new_hold_at_id2				= create_dataref("laminar/B738/fms/new_hold_at_id2", "string")
new_hold_at_lat2			= create_dataref("laminar/B738/fms/new_hold_at_lat2", "number")
new_hold_at_lon2			= create_dataref("laminar/B738/fms/new_hold_at_lon2", "number")
new_hold_at_crs_mag2		= create_dataref("laminar/B738/fms/new_hold_at_crs_mag2", "number")
new_hold_at_turn2			= create_dataref("laminar/B738/fms/new_hold_at_turn2", "number")
new_hold_at_speed2			= create_dataref("laminar/B738/fms/new_hold_at_speed2", "number")
new_hold_at_altitude2		= create_dataref("laminar/B738/fms/new_hold_at_altitude2", "number")
new_hold_at_time2			= create_dataref("laminar/B738/fms/new_hold_at_time2", "number")
new_hold_at_distance2		= create_dataref("laminar/B738/fms/new_hold_at_distance2", "number")

function hide_gpu_handl() end
B738DR_hide_gpu 			= create_dataref("laminar/B738/gpu_hide", "number", hide_gpu_handl)

function gpu_test_handl() end
function asu_test_handl() end
function fueltruck_test_handl() end
function cones_test_handl() end
function deicetruck_test_handl() end
B738DR_gpu_test 			= create_dataref("laminar/B738/gpu_test", "number", gpu_test_handl)
B738DR_asu_test 			= create_dataref("laminar/B738/asu_test", "number", asu_test_handl)
B738DR_fueltruck_test		= create_dataref("laminar/B738/fueltruck_test", "number", fueltruck_test_handl)
B738DR_cones_test 			= create_dataref("laminar/B738/cones_test", "number", cones_test_handl)
B738DR_deicetruck_test		= create_dataref("laminar/B738/deicetruck_test", "number", deicetruck_test_handl)


plane_2k_4k				= create_dataref("laminar/B738/plane_2k_4k", "number")

xplane_file_path2			= find_dataref("zibomod/Xplane_Path")
file_path2					= find_dataref("zibomod/Aircraft_Path")

function B738DR_state_cmd_handl() end
function B738DR_state_cmd2_handl() end
function B738DR_state_cmd3_handl() end
B738DR_state_cmd 			= create_dataref("laminar/B738/state_cmd", "string", B738DR_state_cmd_handl)
B738DR_state_cmd2 			= create_dataref("laminar/B738/state_cmd2", "number", B738DR_state_cmd2_handl)
B738DR_state_cmd3 			= create_dataref("laminar/B738/state_cmd3", "number", B738DR_state_cmd3_handl)

simDR_hsi_crs1					= find_dataref("sim/cockpit2/radios/actuators/hsi_obs_deg_mag_pilot")
simDR_hsi_crs2					= find_dataref("sim/cockpit2/radios/actuators/hsi_obs_deg_mag_copilot")

simDR_nav1_nav_id				= find_dataref("sim/cockpit2/radios/indicators/nav1_nav_id")
simDR_nav1_type					= find_dataref("sim/cockpit2/radios/indicators/nav1_type")
simDR_nav1_dme					= find_dataref("sim/cockpit2/radios/indicators/nav1_dme_distance_nm")
simDR_nav1_has_dme				= find_dataref("sim/cockpit2/radios/indicators/nav1_has_dme")
simDR_nav1_obs_pilot			= find_dataref("sim/cockpit2/radios/actuators/nav1_obs_deg_mag_pilot")
simDR_nav1_obs_copilot			= find_dataref("sim/cockpit2/radios/actuators/nav1_obs_deg_mag_copilot")
simDR_nav1_bearing				= find_dataref("sim/cockpit2/radios/indicators/nav1_bearing_deg_mag")
simDR_nav1_horz_dsp				= find_dataref("sim/cockpit2/radios/indicators/nav1_display_horizontal")

	
simDR_vor1_capt					= find_dataref("sim/cockpit2/EFIS/EFIS_1_selection_pilot")
simDR_vor2_capt					= find_dataref("sim/cockpit2/EFIS/EFIS_2_selection_pilot")
simDR_vor1_fo					= find_dataref("sim/cockpit2/EFIS/EFIS_1_selection_copilot")
simDR_vor2_fo					= find_dataref("sim/cockpit2/EFIS/EFIS_2_selection_copilot")

simDR_nav2_nav_id				= find_dataref("sim/cockpit2/radios/indicators/nav2_nav_id")
simDR_nav2_type					= find_dataref("sim/cockpit2/radios/indicators/nav2_type")
simDR_nav2_dme					= find_dataref("sim/cockpit2/radios/indicators/nav2_dme_distance_nm")
simDR_nav2_has_dme				= find_dataref("sim/cockpit2/radios/indicators/nav2_has_dme")
simDR_nav2_obs_pilot			= find_dataref("sim/cockpit2/radios/actuators/nav2_obs_deg_mag_pilot")
simDR_nav2_obs_copilot			= find_dataref("sim/cockpit2/radios/actuators/nav2_obs_deg_mag_copilot")
simDR_nav2_bearing				= find_dataref("sim/cockpit2/radios/indicators/nav2_bearing_deg_mag")
simDR_nav2_horz_dsp				= find_dataref("sim/cockpit2/radios/indicators/nav2_display_horizontal")

simDR_adf1_bearing				= find_dataref("sim/cockpit2/radios/indicators/adf1_bearing_deg_mag")
simDR_adf2_bearing				= find_dataref("sim/cockpit2/radios/indicators/adf2_bearing_deg_mag")

B738DR_vor1_sel_rotate	 		= create_dataref("laminar/B738/pfd/vor1_sel_rotate", "number")
B738DR_vor1_sel_x	 			= create_dataref("laminar/B738/pfd/vor1_sel_x", "number")
B738DR_vor1_sel_y	 			= create_dataref("laminar/B738/pfd/vor1_sel_y", "number")
B738DR_vor1_sel_id	 			= create_dataref("laminar/B738/pfd/vor1_sel_id", "string")
B738DR_vor1_sel_crs	 			= create_dataref("laminar/B738/pfd/vor1_sel_crs", "string")
B738DR_vor1_sel_bcrs 			= create_dataref("laminar/B738/pfd/vor1_sel_bcrs", "string")
B738DR_vor1_show	 			= create_dataref("laminar/B738/pfd/vor1_show", "number")
B738DR_vor1_line_show			= create_dataref("laminar/B738/pfd/vor1_line_show", "number")

nav1_lat 						= create_dataref("laminar/B738/pfd/vor1_sel_lat", "number")
nav1_lon 						= create_dataref("laminar/B738/pfd/vor1_sel_lon", "number")
nav2_lat 						= create_dataref("laminar/B738/pfd/vor2_sel_lat", "number")
nav2_lon 						= create_dataref("laminar/B738/pfd/vor2_sel_lon", "number")

nav1_lat_fo 					= create_dataref("laminar/B738/pfd/vor1_sel_lat_fo", "number")
nav1_lon_fo 					= create_dataref("laminar/B738/pfd/vor1_sel_lon_fo", "number")
nav2_lat_fo 					= create_dataref("laminar/B738/pfd/vor2_sel_lat_fo", "number")
nav2_lon_fo 					= create_dataref("laminar/B738/pfd/vor2_sel_lon_fo", "number")

B738DR_vor2_sel_rotate	 		= create_dataref("laminar/B738/pfd/vor2_sel_rotate", "number")
B738DR_vor2_sel_x	 			= create_dataref("laminar/B738/pfd/vor2_sel_x", "number")
B738DR_vor2_sel_y	 			= create_dataref("laminar/B738/pfd/vor2_sel_y", "number")
B738DR_vor2_sel_id	 			= create_dataref("laminar/B738/pfd/vor2_sel_id", "string")
B738DR_vor2_sel_crs	 			= create_dataref("laminar/B738/pfd/vor2_sel_crs", "string")
B738DR_vor2_sel_bcrs 			= create_dataref("laminar/B738/pfd/vor2_sel_bcrs", "string")
B738DR_vor2_show	 			= create_dataref("laminar/B738/pfd/vor2_show", "number")
B738DR_vor2_line_show			= create_dataref("laminar/B738/pfd/vor2_line_show", "number")

B738DR_vor1_sel_rotate_fo 		= create_dataref("laminar/B738/pfd/vor1_sel_rotate_fo", "number")
B738DR_vor1_sel_x_fo 			= create_dataref("laminar/B738/pfd/vor1_sel_x_fo", "number")
B738DR_vor1_sel_y_fo 			= create_dataref("laminar/B738/pfd/vor1_sel_y_fo", "number")
B738DR_vor1_sel_id_fo 			= create_dataref("laminar/B738/pfd/vor1_sel_id_fo", "string")
B738DR_vor1_sel_crs_fo 			= create_dataref("laminar/B738/pfd/vor1_sel_crs_fo", "string")
B738DR_vor1_sel_bcrs_fo			= create_dataref("laminar/B738/pfd/vor1_sel_bcrs_fo", "string")
B738DR_vor1_copilot_show		= create_dataref("laminar/B738/pfd/vor1_copilot_show", "number")
B738DR_vor1_line_copilot_show	= create_dataref("laminar/B738/pfd/vor1_line_copilot_show", "number")

B738DR_vor2_sel_rotate_fo		= create_dataref("laminar/B738/pfd/vor2_sel_rotate_fo", "number")
B738DR_vor2_sel_x_fo	 		= create_dataref("laminar/B738/pfd/vor2_sel_x_fo", "number")
B738DR_vor2_sel_y_fo	 		= create_dataref("laminar/B738/pfd/vor2_sel_y_fo", "number")
B738DR_vor2_sel_id_fo	 		= create_dataref("laminar/B738/pfd/vor2_sel_id_fo", "string")
B738DR_vor2_sel_crs_fo	 		= create_dataref("laminar/B738/pfd/vor2_sel_crs_fo", "string")
B738DR_vor2_sel_bcrs_fo 		= create_dataref("laminar/B738/pfd/vor2_sel_bcrs_fo", "string")
B738DR_vor2_copilot_show		= create_dataref("laminar/B738/pfd/vor2_copilot_show", "number")
B738DR_vor2_line_copilot_show 	= create_dataref("laminar/B738/pfd/vor2_line_copilot_show", "number")

B738DR_vor1_arrow 				= create_dataref("laminar/B738/pfd/vor1_arrow", "number")
B738DR_vor2_arrow 				= create_dataref("laminar/B738/pfd/vor2_arrow", "number")
B738DR_vor1_arrow_fo 			= create_dataref("laminar/B738/pfd/vor1_arrow_fo", "number")
B738DR_vor2_arrow_fo 			= create_dataref("laminar/B738/pfd/vor2_arrow_fo", "number")

B738DR_adf1_arrow 				= create_dataref("laminar/B738/pfd/adf1_arrow", "number")
B738DR_adf2_arrow 				= create_dataref("laminar/B738/pfd/adf2_arrow", "number")
B738DR_wind_arrow				= create_dataref("laminar/B738/pfd/wind_arrow", "number")
B738DR_adf1_arrow_fo 			= create_dataref("laminar/B738/pfd/adf1_arrow_fo", "number")
B738DR_adf2_arrow_fo 			= create_dataref("laminar/B738/pfd/adf2_arrow_fo", "number")
B738DR_wind_arrow_fo			= create_dataref("laminar/B738/pfd/wind_arrow_fo", "number")

B738DR_vor_dme1					= create_dataref("laminar/B738/pfd/vor_dme1", "number")
B738DR_vor_dme2					= create_dataref("laminar/B738/pfd/vor_dme2", "number")
B738DR_vor_dme_id1				= create_dataref("laminar/B738/pfd/vor_dme_id1", "number")
B738DR_vor_dme_id2				= create_dataref("laminar/B738/pfd/vor_dme_id2", "number")

simDR_nav1_freq					= find_dataref("sim/cockpit2/radios/actuators/nav1_frequency_hz")
simDR_nav2_freq					= find_dataref("sim/cockpit2/radios/actuators/nav2_frequency_hz")
simDR_adf1_freq					= find_dataref("sim/cockpit2/radios/actuators/adf1_frequency_hz")
simDR_adf2_freq					= find_dataref("sim/cockpit2/radios/actuators/adf2_frequency_hz")
simDR_adf1_nav_id				= find_dataref("sim/cockpit2/radios/indicators/adf1_nav_id")
simDR_adf2_nav_id				= find_dataref("sim/cockpit2/radios/indicators/adf2_nav_id")

B738DR_vor_freq1				= create_dataref("laminar/B738/pfd/vor_freq1", "string")
B738DR_vor_freq2				= create_dataref("laminar/B738/pfd/vor_freq2", "string")
B738DR_adf_freq1				= create_dataref("laminar/B738/pfd/adf_freq1", "string")
B738DR_adf_freq2				= create_dataref("laminar/B738/pfd/adf_freq2", "string")
B738DR_vor1_id_show				= create_dataref("laminar/B738/pfd/vor1_id_show", "number")
B738DR_vor2_id_show				= create_dataref("laminar/B738/pfd/vor2_id_show", "number")

B738DR_fmod_mmr 				= create_dataref("laminar/B738/fmod/mmr_button", "number")
B738DR_fmod_nav_rotary			= create_dataref("laminar/B738/fmod/nav_rotary", "number")

simDR_nav1_stdby_Mhz 			= find_dataref("sim/cockpit2/radios/actuators/nav1_standby_frequency_Mhz")
simDR_nav1_stdby_khz 			= find_dataref("sim/cockpit2/radios/actuators/nav1_standby_frequency_khz")
simDR_nav2_stdby_Mhz 			= find_dataref("sim/cockpit2/radios/actuators/nav2_standby_frequency_Mhz")
simDR_nav2_stdby_khz 			= find_dataref("sim/cockpit2/radios/actuators/nav2_standby_frequency_khz")

crz_exec						= create_dataref("laminar/B738/fms/crz_exec", "number")

B738DR_calc_to_cg				= create_dataref("laminar/B738/fms/calc_to_cg", "number")

B738DR_efis_vor1_capt_pos	= create_dataref("laminar/B738/EFIS_control/capt/vor1_off_pos", "number")
B738DR_efis_vor2_capt_pos	= create_dataref("laminar/B738/EFIS_control/capt/vor2_off_pos", "number")
B738DR_efis_vor1_fo_pos		= create_dataref("laminar/B738/EFIS_control/fo/vor1_off_pos", "number")
B738DR_efis_vor2_fo_pos		= create_dataref("laminar/B738/EFIS_control/fo/vor2_off_pos", "number")

B738DR_clock_year				= create_dataref("laminar/B738/clock/year", "number")

function ls_hand() end
function ls_hand2() end
B738DR_load_save				= create_dataref("laminar/B738/load_save", "number", ls_hand)
B738DR_load_save2				= create_dataref("laminar/B738/load_save2", "number", ls_hand2)

B738DR_fms_flight01				= create_dataref("laminar/B738/fms/flight01", "string")
B738DR_fms_flight02				= create_dataref("laminar/B738/fms/flight02", "string")
B738DR_fms_flight03				= create_dataref("laminar/B738/fms/flight03", "string")
B738DR_fms_flight04				= create_dataref("laminar/B738/fms/flight04", "string")
B738DR_fms_flight05				= create_dataref("laminar/B738/fms/flight05", "string")
B738DR_fms_flight06				= create_dataref("laminar/B738/fms/flight06", "string")
B738DR_fms_flight07				= create_dataref("laminar/B738/fms/flight07", "string")
B738DR_fms_flight08				= create_dataref("laminar/B738/fms/flight08", "string")
B738DR_fms_flight09				= create_dataref("laminar/B738/fms/flight09", "string")
B738DR_fms_flight01b			= create_dataref("laminar/B738/fms/flight01b", "string")
B738DR_fms_flight02b			= create_dataref("laminar/B738/fms/flight02b", "string")
B738DR_fms_flight03b			= create_dataref("laminar/B738/fms/flight03b", "string")
B738DR_fms_flight04b			= create_dataref("laminar/B738/fms/flight04b", "string")
B738DR_fms_flight05b			= create_dataref("laminar/B738/fms/flight05b", "string")
B738DR_fms_flight06b			= create_dataref("laminar/B738/fms/flight06b", "string")
B738DR_fms_flight07b			= create_dataref("laminar/B738/fms/flight07b", "string")
B738DR_fms_flight08b			= create_dataref("laminar/B738/fms/flight08b", "string")
B738DR_fms_flight09b			= create_dataref("laminar/B738/fms/flight09b", "string")
B738DR_fms_flight_saved			= create_dataref("laminar/B738/fms/flight_saved", "array[8]")

function DR_glare_handl() end
function DR_glare_sw_handl() end
function DR_latlon_handl() end
function DR_latlon_sw_handl() end
function DR_system_handl() end
function DR_system_sw_handl() end
function DR_efb_handl() end
function DR_antiice_handl() end
function DR_antiice_sw_handl() end
function DR_lights_handl() end
function DR_lights_sw_handl() end
function DR_cover_handl() end
function DR_cover_trg_handl() end
function DR_fire_handl() end
function DR_fire_sw_handl() end
function DR_global_handl() end
B738DR_glare					= create_dataref("laminar/B738/glare", "array[250]", DR_glare_handl)
B738DR_glare_sw					= create_dataref("laminar/B738/glare_sw", "array[250]", DR_glare_sw_handl)
B738DR_latlon					= create_dataref("laminar/B738/latlon", "array[250]", DR_latlon_handl)
B738DR_latlon_sw				= create_dataref("laminar/B738/latlon_sw", "array[250]", DR_latlon_sw_handl)
B738DR_system					= create_dataref("laminar/B738/system", "array[250]", DR_system_handl)
B738DR_system_sw				= create_dataref("laminar/B738/system_sw", "array[250]", DR_system_sw_handl)
B738DR_efb						= create_dataref("laminar/B738/efb", "array[250]", DR_efb_handl)
B738DR_antiice					= create_dataref("laminar/B738/antiice", "array[250]", DR_antiice_handl)
B738DR_antiice_sw				= create_dataref("laminar/B738/antiice_sw", "array[250]", DR_antiice_sw_handl)
B738DR_lights					= create_dataref("laminar/B738/lights", "array[250]", DR_lights_handl)
B738DR_lights_sw				= create_dataref("laminar/B738/lights_sw", "array[250]", DR_lights_sw_handl)
B738DR_cover					= create_dataref("laminar/B738/cover", "array[250]", DR_cover_handl)
B738DR_cover_tgt				= create_dataref("laminar/B738/cover_tgt", "array[250]", DR_cover_trg_handl)
B738DR_fire						= create_dataref("laminar/B738/fire", "array[250]", DR_fire_handl)
B738DR_fire_sw					= create_dataref("laminar/B738/fire_sw", "array[250]", DR_fire_sw_handl)
B738DR_global					= create_dataref("laminar/B738/global", "array[250]", DR_global_handl)

simDR_press_max_alt		= find_dataref("sim/cockpit/pressure/max_allowable_altitude")
B738DR_land_alt_knob 	= find_dataref("laminar/B738/pressurization/knobs/landing_alt")
simDR_squawk			= find_dataref("sim/cockpit2/radios/actuators/transponder_code")

B738DR_zibomod_debug			= find_dataref("zibomod/Debug")

B738DR_73x						= find_dataref("laminar/B738/73x")
B738DR_b737_variant				= find_dataref("zibomod/b737_variant")

function el_failure_handler() end
function hyd_failure_handler() end
function sys_failure_handler() end
function pressurisation_failure_handler() end
function engine_failure_handler() end
function rst_failure_handler() end
B738DR_el_failure	= create_dataref("laminar/B738/failure/electric", "array[70]", el_failure_handler)
B738DR_hyd_failure	= create_dataref("laminar/B738/failure/hydraulic", "array[70]", hyd_failure_handler)
B738DR_sys_failure	= create_dataref("laminar/B738/failure/system", "array[70]", sys_failure_handler)
B738DR_pressurisation_failure = create_dataref("laminar/B738/failure/pressurisation", "array[70]", pressurisation_failure_handler)
B738DR_engine_failure = create_dataref("laminar/B738/failure/engine", "array[70]", engine_failure_handler)
B738DR_rst_failure	= create_dataref("laminar/B738/failure/rst", "number", rst_failure_handler)


--*************************************************************************************--
--** 				       READ-WRITE CUSTOM DATAREF HANDLERS     	        	     **--
--*************************************************************************************--


function DRindex_DRhandler()end
function decode_value_DRhandler()end
function index_pos_DRhandler()end

function DR_test_DRhandler()end
function DR_test2_DRhandler()end

function B738DR_fms_takeoff_flaps_DRhandler()end
function B738DR_fms_approach_flaps_DRhandler()end
function B738DR_fms_approach_speed_DRhandler()end
function B738DR_fms_approach_wind_corr_DRhandler()end

function B738DR_fms_descent_now_DRhandler()end

function B738DR_vvi_const_DRhandler()end

function B738DR_vnav_disconnect_DRhandler() end
function B738DR_lnav_disconnect_DRhandler() end

function B738DR_chock_status_DRhandler() end

function B738DR_hold_phase_DRhandler() end
function B738DR_hold_term_DRhandler() end
function B738DR_pi_phase_DRhandler() end

function B738DR_test_DRhandler()end
function B738DR_test1_DRhandler()end
function B738DR_test3_DRhandler()end
function B738DR_test4_DRhandler()end
function B738DR_test5_DRhandler()end
function B738DR_test6_DRhandler()end
function B738DR_test7_DRhandler()end
function B738DR_test8_DRhandler()end

function B738DR_kill_fms_DRhandler()end
function B738DR_kill_fms_navaid_DRhandler()end
function B738DR_kill_fms_navaid2_DRhandler()end
function B738DR_kill_fms_navaid3_DRhandler()end

function B738DR_idle_path_DRhandler()end
function B738DR_idle_path_kts_DRhandler()end
function B738DR_idle_path_mach_DRhandler()end

function B738DR_metar_cmd_DRhandler()end
function B738DR_metar_data_DRhandler()end

function B738DR_find_icao_lat_DRhandler()end
function B738DR_find_icao_lon_DRhandler()end
function B738DR_find_icao_height_DRhandler()end

function B738_cust_axis_speedbrake_DRhandler()end
function B738_cust_axis_flaps_DRhandler()end
function B738_cust_axis_speedbrake_arm_DRhandler()end
function B738_cust_axis_speedbrake_flight_DRhandler()end
function B738_cust_axis_speedbrake_noise_DRhandler()end
function B738_cust_axis_flaps_mapped_DRhandler()end
function B738_cust_axis_speedbrake_mapped_DRhandler()end
function B738_cust_axis_flaps_reverse_DRhandler()end
function B738_cust_axis_speedbrake_reverse_DRhandler()end
function B738_cust_axis_landgear_mapped_DRhandler()end
function B738_cust_axis_landgear_reverse_DRhandler()end
function B738_cust_axis_landgear_off_DRhandler()end
function B738_cust_axis_mixture1_mapped_DRhandler()end
function B738_cust_axis_mixture2_mapped_DRhandler()end
function B738_cust_axis_mixture1_reverse_DRhandler()end
function B738_cust_axis_mixture2_reverse_DRhandler()end
function B738_cust_axis_landgear_DRhandler()end
function B738_cust_axis_mixture1_DRhandler()end
function B738_cust_axis_mixture2_DRhandler()end

--*************************************************************************************--
--** 				       CREATE READ-WRITE CUSTOM DATAREFS                         **--
--*************************************************************************************--

B738DR_cust_axis_speedbrake			= create_dataref("laminar/B738/flt_ctrls/speedbrake", "number", B738_cust_axis_speedbrake_DRhandler)
B738DR_cust_axis_flaps				= create_dataref("laminar/B738/flt_ctrls/flaps", "number", B738_cust_axis_flaps_DRhandler)
B738DR_cust_axis_speedbrake_arm		= create_dataref("laminar/B738/flt_ctrls/speedbrake_arm", "number", B738_cust_axis_speedbrake_arm_DRhandler)
B738DR_cust_axis_speedbrake_flight	= create_dataref("laminar/B738/flt_ctrls/speedbrake_flight", "number", B738_cust_axis_speedbrake_flight_DRhandler)
B738DR_cust_axis_speedbrake_noise	= create_dataref("laminar/B738/flt_ctrls/speedbrake_noise", "number", B738_cust_axis_speedbrake_noise_DRhandler)
B738DR_cust_axis_flaps_mapped		= create_dataref("laminar/B738/flt_ctrls/flaps_mapped", "number", B738_cust_axis_flaps_mapped_DRhandler)
B738DR_cust_axis_speedbrake_mapped	= create_dataref("laminar/B738/flt_ctrls/speedbrake_mapped", "number", B738_cust_axis_speedbrake_mapped_DRhandler)
B738DR_cust_axis_flaps_reverse		= create_dataref("laminar/B738/flt_ctrls/flaps_reverse", "number", B738_cust_axis_flaps_reverse_DRhandler)
B738DR_cust_axis_speedbrake_reverse	= create_dataref("laminar/B738/flt_ctrls/speedbrake_reverse", "number", B738_cust_axis_speedbrake_reverse_DRhandler)
B738DR_cust_axis_landgear			= create_dataref("laminar/B738/flt_ctrls/landgear", "number", B738_cust_axis_landgear_DRhandler)
B738DR_cust_axis_landgear_mapped	= create_dataref("laminar/B738/flt_ctrls/landgear_mapped", "number", B738_cust_axis_landgear_mapped_DRhandler)
B738DR_cust_axis_landgear_off		= create_dataref("laminar/B738/flt_ctrls/landgear_off", "number", B738_cust_axis_landgear_off_DRhandler)
B738DR_cust_axis_landgear_reverse	= create_dataref("laminar/B738/flt_ctrls/landgear_reverse", "number", B738_cust_axis_landgear_reverse_DRhandler)
B738DR_cust_axis_mixture1			= create_dataref("laminar/B738/flt_ctrls/mixture1", "number", B738_cust_axis_mixture1_DRhandler)
B738DR_cust_axis_mixture2			= create_dataref("laminar/B738/flt_ctrls/mixture2", "number", B738_cust_axis_mixture2_DRhandler)
B738DR_cust_axis_mixture1_mapped	= create_dataref("laminar/B738/flt_ctrls/mixture1_mapped", "number", B738_cust_axis_mixture1_mapped_DRhandler)
B738DR_cust_axis_mixture2_mapped	= create_dataref("laminar/B738/flt_ctrls/mixture2_mapped", "number", B738_cust_axis_mixture2_mapped_DRhandler)
B738DR_cust_axis_mixture1_reverse	= create_dataref("laminar/B738/flt_ctrls/mixture1_reverse", "number", B738_cust_axis_mixture1_reverse_DRhandler)
B738DR_cust_axis_mixture2_reverse	= create_dataref("laminar/B738/flt_ctrls/mixture2_reverse", "number", B738_cust_axis_mixture2_reverse_DRhandler)

function B738_cust_axis_flaps_raw_handl() end
function B738_cust_axis_speedbrake_raw_handl() end
B738DR_cust_axis_flaps_raw			= create_dataref("laminar/B738/flt_ctrls/flaps_raw", "number", B738_cust_axis_flaps_raw_handl)
B738DR_cust_axis_speedbrake_raw		= create_dataref("laminar/B738/flt_ctrls/speedbrake_raw", "number", B738_cust_axis_speedbrake_raw_handl)

idle_alt = 40000
file_idle_path = 0
B738DR_idle_path = create_dataref("laminar/B738/fms/idle_path", "number", B738DR_idle_path_DRhandler)
B738DR_idle_path_kts = create_dataref("laminar/B738/fms/idle_path_kts", "number", B738DR_idle_path_kts_DRhandler)
B738DR_idle_path_mach = create_dataref("laminar/B738/fms/idle_path_mach", "number", B738DR_idle_path_mach_DRhandler)

B738DR_metar_icao 			= create_dataref("laminar/B738/metar_icao", "string")
B738DR_metar_cmd 			= create_dataref("laminar/B738/metar_cmd", "number", B738DR_metar_cmd_DRhandler)
B738DR_metar_data 			= create_dataref("laminar/B738/metar_data", "string", B738DR_metar_data_DRhandler)

B738DR_find_icao 			= create_dataref("laminar/B738/find_icao", "string")
B738DR_find_icao_cmd 		= find_dataref("zibomod/find_icao_cmd")
B738DR_find_icao_lat		= create_dataref("laminar/B738/find_icao_lat", "number", B738DR_find_icao_lat_DRhandler)
B738DR_find_icao_lon		= create_dataref("laminar/B738/find_icao_lon", "number", B738DR_find_icao_lon_DRhandler)
B738DR_find_icao_height		= create_dataref("laminar/B738/find_icao_height", "number", B738DR_find_icao_height_DRhandler)

B738DR_legs_intdir_dist 	= create_dataref("laminar/B738/fms/legs_intdir_dist", "number")

-- simDR_mach_no					= find_dataref("sim/flightmodel/misc/machno")
simDR_mach_no					= find_dataref("laminar/B738/indicated/mach_no")
simCMD_autopilot_co			= find_command("sim/autopilot/knots_mach_toggle")


B738DR_kill_fms	= create_dataref("laminar/B738/perf/kill_fms", "number", B738DR_kill_fms_DRhandler)
B738DR_kill_fms_navaid = create_dataref("laminar/B738/perf/kill_fms_navaid", "number", B738DR_kill_fms_navaid_DRhandler)
B738DR_kill_fms_navaid2 = create_dataref("laminar/B738/perf/kill_fms_navaid2", "number", B738DR_kill_fms_navaid2_DRhandler)
B738DR_kill_fms_navaid3 = create_dataref("laminar/B738/perf/kill_fms_navaid3", "number", B738DR_kill_fms_navaid3_DRhandler)

B738DR_fms_test			= create_dataref("laminar/B738/fms/test", "number", B738DR_test_DRhandler)
B738DR_fms_test1		= create_dataref("laminar/B738/fms/test1", "number", B738DR_test1_DRhandler)
B738DR_fms_test3		= create_dataref("laminar/B738/fms/test3", "number", B738DR_test3_DRhandler)
B738DR_fms_test4		= create_dataref("laminar/B738/fms/test4", "number", B738DR_test4_DRhandler)
B738DR_fms_test5		= create_dataref("laminar/B738/fms/test5", "number", B738DR_test5_DRhandler)
B738DR_fms_test6		= create_dataref("laminar/B738/fms/test6", "number", B738DR_test6_DRhandler)
B738DR_fms_test7		= create_dataref("laminar/B738/fms/test7", "number", B738DR_test7_DRhandler)
B738DR_fms_test8		= create_dataref("laminar/B738/fms/test8", "number", B738DR_test8_DRhandler)
B738DR_fms_test_x		= create_dataref("laminar/B738/fms/test_x", "string")
B738DR_fms_test_next	= create_dataref("laminar/B738/fms/test_next", "number")

function B738DR_arrow_3d_override_DRhandler() end
function B738DR_arrow_3d_kill_DRhandler() end
function B738DR_arrow_3d_x_DRhandler() end
function B738DR_arrow_3d_y_DRhandler() end
function B738DR_arrow_3d_z_DRhandler() end
function B738DR_arrow_3d_rot_DRhandler() end
function B738DR_arrow_3d_item_DRhandler() end
function B738DR_arrow_3d_kill2_DRhandler() end
function B738DR_arrow_3d_x2_DRhandler() end
function B738DR_arrow_3d_y2_DRhandler() end
function B738DR_arrow_3d_z2_DRhandler() end
function B738DR_arrow_3d_rot2_DRhandler() end
function B738DR_arrow_3d_item2_DRhandler() end
function B738DR_arrow_3d_helper_DRhandler() end
B738DR_arrow_3d_override	= create_dataref("laminar/B738/arrow_3d/override", "number", B738DR_arrow_3d_override_DRhandler)
B738DR_arrow_3d_kill		= create_dataref("laminar/B738/arrow_3d/kill", "number", B738DR_arrow_3d_kill_DRhandler)
B738DR_arrow_3d_x			= create_dataref("laminar/B738/arrow_3d/x", "number", B738DR_arrow_3d_x_DRhandler)
B738DR_arrow_3d_y			= create_dataref("laminar/B738/arrow_3d/y", "number", B738DR_arrow_3d_y_DRhandler)
B738DR_arrow_3d_z			= create_dataref("laminar/B738/arrow_3d/z", "number", B738DR_arrow_3d_z_DRhandler)
B738DR_arrow_3d_rotate		= create_dataref("laminar/B738/arrow_3d/rotate", "number", B738DR_arrow_3d_rot_DRhandler)
B738DR_arrow_3d_item		= create_dataref("laminar/B738/arrow_3d/item", "number", B738DR_arrow_3d_item_DRhandler)
B738DR_arrow_3d_kill2		= create_dataref("laminar/B738/arrow_3d/kill2", "number", B738DR_arrow_3d_kill2_DRhandler)
B738DR_arrow_3d_x2			= create_dataref("laminar/B738/arrow_3d/x2", "number", B738DR_arrow_3d_x2_DRhandler)
B738DR_arrow_3d_y2			= create_dataref("laminar/B738/arrow_3d/y2", "number", B738DR_arrow_3d_y2_DRhandler)
B738DR_arrow_3d_z2			= create_dataref("laminar/B738/arrow_3d/z2", "number", B738DR_arrow_3d_z2_DRhandler)
B738DR_arrow_3d_rotate2		= create_dataref("laminar/B738/arrow_3d/rotate2", "number", B738DR_arrow_3d_rot2_DRhandler)
B738DR_arrow_3d_item2		= create_dataref("laminar/B738/arrow_3d/item2", "number", B738DR_arrow_3d_item2_DRhandler)
B738DR_arrow_3d_helper		= create_dataref("laminar/B738/arrow_3d/arrow_3d_helper", "number", B738DR_arrow_3d_helper_DRhandler)

B738DR_hold_phase		= create_dataref("laminar/B738/fms/hold_phase", "number", B738DR_hold_phase_DRhandler)
B738DR_hold_term		= create_dataref("laminar/B738/fms/hold_term", "number", B738DR_hold_term_DRhandler)
hold_exec				= create_dataref("laminar/B738/fms/hold_exec", "number")
hold_exec2				= create_dataref("laminar/B738/fms/hold_exec2", "number")
B738DR_pi_phase			= create_dataref("laminar/B738/fms/pi_phase", "number", B738DR_pi_phase_DRhandler)

B738DR_chock_status		= create_dataref("laminar/B738/fms/chock_status", "number", B738DR_chock_status_DRhandler)
B738DR_cones_hide		= create_dataref("laminar/B738/fms/cones_hide", "number")

B738DR_vvi_const	= create_dataref("laminar/B738/fms/vnav_vvi_const", "number", B738DR_vvi_const_DRhandler)

DRindex	= create_dataref("laminar/B738/fmc/vnav/DRindex", "number", DRindex_DRhandler)

decode_value		= create_dataref("laminar/B738/fmc/decode_value", "number", decode_value_DRhandler)
index_pos			= create_dataref("laminar/B738/fmc/index_pos", "number", index_pos_DRhandler)

DR_test				= create_dataref("laminar/B738/fmc/DR_test", "number", DR_test_DRhandler)
DR_test2				= create_dataref("laminar/B738/fmc/DR_test2", "number", DR_test2_DRhandler)


B738DR_fms_takeoff_flaps	= create_dataref("laminar/B738/FMS/takeoff_flaps", "number", B738DR_fms_takeoff_flaps_DRhandler)
B738DR_fms_approach_flaps	= create_dataref("laminar/B738/FMS/approach_flaps", "number", B738DR_fms_approach_flaps_DRhandler)
B738DR_fms_approach_speed		= create_dataref("laminar/B738/FMS/approach_speed", "number", B738DR_fms_approach_speed_DRhandler)
B738DR_fms_approach_wind_corr	= create_dataref("laminar/B738/FMS/approach_wind_corr", "number", B738DR_fms_approach_wind_corr_DRhandler)

B738DR_fms_descent_now		= create_dataref("laminar/B738/FMS/descent_now", "number", B738DR_fms_descent_now_DRhandler)

B738DR_vnav_disconnect		= create_dataref("laminar/B738/fms/vnav_disconnect", "number", B738DR_vnav_disconnect_DRhandler)
B738DR_lnav_disconnect		= create_dataref("laminar/B738/fms/lnav_disconnect", "number", B738DR_lnav_disconnect_DRhandler)

B738DR_activate_pitch_mode	= create_dataref("laminar/B738/FMS/activate_pitch_mode", "number")
B738DR_alt_hold_vnav		= create_dataref("laminar/B738/autopilot/alt_hold_vnav", "number")

simDR_pos_x			= find_dataref("sim/flightmodel/position/local_x")
simDR_pos_y			= find_dataref("sim/flightmodel/position/local_y")
simDR_pos_z			= find_dataref("sim/flightmodel/position/local_z")
simDR_pos_vx		= find_dataref("sim/flightmodel/position/local_vx")
simDR_pos_vy		= find_dataref("sim/flightmodel/position/local_vy")
simDR_pos_vz		= find_dataref("sim/flightmodel/position/local_vz")
simDR_pos_ax		= find_dataref("sim/flightmodel/position/local_ax")
simDR_pos_ay		= find_dataref("sim/flightmodel/position/local_ay")
simDR_pos_az		= find_dataref("sim/flightmodel/position/local_az")

xfirst_time2		= create_dataref("laminar/B738/fms/xfirst_time2", "number")

simDR_int_ver			= find_dataref("sim/version/xplane_internal_version")

function JAR_copilot_handl() end
B738DR_JAR_copilot = create_dataref("laminar/b738/jar_copilot", "number", JAR_copilot_handl)


function nd_white_r_handl() end
function nd_white_g_handl() end
function nd_white_b_handl() end
B738DR_nd_white_r  = create_dataref("laminar/b738/nd_white_r", "number", nd_white_r_handl)
B738DR_nd_white_g  = create_dataref("laminar/b738/nd_white_g", "number", nd_white_g_handl)
B738DR_nd_white_b  = create_dataref("laminar/b738/nd_white_b", "number", nd_white_b_handl)

function nd_magenta_r_handl() end
function nd_magenta_g_handl() end
function nd_magenta_b_handl() end
B738DR_nd_magenta_r  = create_dataref("laminar/b738/nd_magenta_r", "number", nd_magenta_r_handl)
B738DR_nd_magenta_g  = create_dataref("laminar/b738/nd_magenta_g", "number", nd_magenta_g_handl)
B738DR_nd_magenta_b  = create_dataref("laminar/b738/nd_magenta_b", "number", nd_magenta_b_handl)

function nd_blue_r_handl() end
function nd_blue_g_handl() end
function nd_blue_b_handl() end
B738DR_nd_blue_r  = create_dataref("laminar/b738/nd_blue_r", "number", nd_blue_r_handl)
B738DR_nd_blue_g  = create_dataref("laminar/b738/nd_blue_g", "number", nd_blue_g_handl)
B738DR_nd_blue_b  = create_dataref("laminar/b738/nd_blue_b", "number", nd_blue_b_handl)

function nd_green_r_handl() end
function nd_green_g_handl() end
function nd_green_b_handl() end
B738DR_nd_green_r  = create_dataref("laminar/b738/nd_green_r", "number", nd_green_r_handl)
B738DR_nd_green_g  = create_dataref("laminar/b738/nd_green_g", "number", nd_green_g_handl)
B738DR_nd_green_b  = create_dataref("laminar/b738/nd_green_b", "number", nd_green_b_handl)

function sb_servo_deploy_handl() end
function sb_servo_retract_handl() end
B738DR_sb_servo_deploy  = create_dataref("laminar/b738/fmod/sb_servo_deploy", "number", sb_servo_deploy_handl)
B738DR_sb_servo_retract  = create_dataref("laminar/b738/fmod/sb_servo_retract", "number", sb_servo_retract_handl)


B738DR_stdby_baro_inhg_1 		= create_dataref("laminar/B738/gauges/standby_altitude_inhg_1", "number")
B738DR_stdby_baro_inhg_10 		= create_dataref("laminar/B738/gauges/standby_altitude_inhg_10", "number")
B738DR_stdby_baro_inhg_100 		= create_dataref("laminar/B738/gauges/standby_altitude_inhg_100", "number")
B738DR_stdby_baro_inhg_1000 	= create_dataref("laminar/B738/gauges/standby_altitude_inhg_1000", "number")
B738DR_stdby_baro_mb_1 			= create_dataref("laminar/B738/gauges/standby_altitude_mb_1", "number")
B738DR_stdby_baro_mb_10 		= create_dataref("laminar/B738/gauges/standby_altitude_mb_10", "number")
B738DR_stdby_baro_mb_100 		= create_dataref("laminar/B738/gauges/standby_altitude_mb_100", "number")
B738DR_stdby_baro_mb_1000 		= create_dataref("laminar/B738/gauges/standby_altitude_mb_1000", "number")

simDR_beta						= find_dataref("sim/flightmodel/position/beta")
B738DR_yd_indicator		 		= create_dataref("laminar/B738/gauges/yd_indicator", "number")
B738DR_parking_brake_pos		= find_dataref("laminar/B738/parking_brake_pos")

door_ratio_fwd_L 		= create_dataref("737u/doors/L1", "number")
door_ratio_fwd_R 		= create_dataref("737u/doors/R1", "number")
door_ratio_aft_L 		= create_dataref("737u/doors/L2", "number")
door_ratio_aft_R 		= create_dataref("737u/doors/R2", "number")
door_ratio_fwd_cargo 	= create_dataref("737u/doors/Fwd_Cargo", "number")
door_ratio_aft_cargo 	= create_dataref("737u/doors/aft_Cargo", "number")
B738DR_air_ground_sensor		= find_dataref("laminar/B738/air_ground_sensor")

--*************************************************************************************--
--** 				              CUSTOM COMMAND HANDLERS            			     **--
--*************************************************************************************--

function B738CMD_chock_toggle_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_chock_status == 0 then
			if simDR_on_ground[0] == 1 and simDR_on_ground[1] == 1 and simDR_on_ground[2] == 1 then
				B738DR_chock_status = 1
			end
		else
			B738DR_chock_status = 0
		end
	end
end

function B738CMD_pause_td_toggle_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_pause_td == 0 then
			B738DR_pause_td = 1
		else
			B738DR_pause_td = 0
		end
	end
end

function B738_fms_light_pilot_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fms_light_pilot = 1
		B738DR_fmc_message_warn = 0
		fmc_message_warn_dis = 1
	elseif phase == 2 then
		B738DR_fms_light_pilot = 0
	end
end

function B738_fms_light_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fms_light_fo = 1
		B738DR_fmc_message_warn = 0
		fmc_message_warn_dis = 1
	elseif phase == 2 then
		B738DR_fms_light_fo = 0
	end
end

-- --*************************************************************************************--
-- --** 				              CREATE CUSTOM COMMANDS              			     **--
-- --*************************************************************************************--


B738CMD_chock_toggle	= create_command("laminar/B738/toggle_switch/chock", "Chock toggle", B738CMD_chock_toggle_CMDhandler)
B738CMD_pause_td_toggle		= create_command("laminar/B738/toggle_switch/pause_td", "Pause at T/D toggle", B738CMD_pause_td_toggle_CMDhandler)

B738CMD_fms_light_pilot		 	= create_command("laminar/B738/push_button/fms_light_pilot", "FMS light captain button", B738_fms_light_pilot_CMDhandler)
B738CMD_fms_light_fo		 	= create_command("laminar/B738/push_button/fms_light_fo", "FMS light captain button", B738_fms_light_fo_CMDhandler)

--*************************************************************************************--
--** 				             X-PLANE COMMAND HANDLERS               	    	 **--
--*************************************************************************************--


-- ROUNDING ---
function roundUpToIncrement(number, increment)

    local y = number / increment
    local q = math.ceil(y)
    local z = q * increment

    return z

end

function roundDownToIncrement(number, increment)

    local y = number / increment
    local q = math.floor(y)
    local z = q * increment

    return z

end

----- ANIMATION UTILITY -----------------------------------------------------------------

function B738_set_anim_value(current_value, target, min, max, speed)

    if target >= (max - 0.001) and current_value >= (max - 0.01) then
        return max
    elseif target <= (min + 0.001) and current_value <= (min + 0.01) then
        return min
    else
        return current_value + ((target - current_value) * (speed * SIM_PERIOD))
    end

end


function B738_set_anim_value2(current_value, target, min, max, speed, limit)

    local mmm = 0
	if target >= (max - 0.001) and current_value >= (max - 0.01) then
        return max
    elseif target <= (min + 0.001) and current_value <= (min + 0.01) then
        return min
    else
        mmm = math.abs((target - current_value) * (speed * SIM_PERIOD))
		if mmm > limit then
			if target > current_value then
				return current_value + limit
			else
				return current_value - limit
			end
		else
			return current_value + ((target - current_value) * (speed * SIM_PERIOD))
		end
    end

end

function B738_set_animation_rate(current_value, target, min, max, speed)
	local calc_result = 0
	if target >= (max - 0.001) and current_value >= (max - 0.01) then
        return max
    elseif target <= (min + 0.001) and current_value <= (min + 0.01) then
        return min
    elseif current_value > max then
		return max
    elseif current_value < min then
		return min
	elseif current_value < target then
		calc_result = current_value + (SIM_PERIOD / speed)
		if calc_result > target then
			calc_result = target
		end
		return calc_result
	else
		calc_result = current_value - (SIM_PERIOD / speed)
		if calc_result < target then
			calc_result = target
		end
        return calc_result
    end
end


----- RESCALE FLOAT AND CLAMP TO OUTER LIMITS -------------------------------------------

function B738_rescale(res1_in, res1_out, res2_in, res2_out, res_x)
	local result = 0
	if res1_in <= res2_in then
		if (res_x < res1_in) then
			result = res1_out
		elseif (res_x > res2_in) then
			result = res2_out
		elseif (res2_in == res1_in) then
			result = res1_out
		else
			result = res1_out + (res2_out - res1_out) * (res_x - res1_in) / (res2_in - res1_in)
		end
	else
		if (res_x > res1_in) then
			result = res1_out
		elseif (res_x < res2_in) then
			result = res2_out
		elseif (res2_in == res1_in) then
			result = res1_out
		else
			result = res1_out + (res2_out - res1_out) * (res_x - res1_in) / (res2_in - res1_in)
		end
	end
	
	return result
end

function displ_crs(in_crs)
	local result = (in_crs + 360) % 360
	result = math.floor(result + 0.5)
	if result >= 359.5 then
		result = 0
	end
	
	return result
end

function Angle180(angle)
    return (angle + 180) % 360
end

function spaces_before(strings, max_num)
	local temp_strings = ""
	local num_strings = string.len(strings)
	local num1 = 0
	
	if num_strings > max_num then
		return string.sub(strings, 1, max_num)
	else
		if num_strings == max_num then
			return strings
		else
			temp_strings = strings
			for num1 = num_strings, max_num - 1 do
				temp_strings = " " .. temp_strings
			end
			return temp_strings
		end
	
	end
end

function spaces_after(strings, max_num)
	local temp_strings = ""
	local num_strings = string.len(strings)
	local num1 = 0
	
	if num_strings > max_num then
		return string.sub(strings, 1, max_num)
	else
		if num_strings == max_num then
			return strings
		else
			temp_strings = strings
			for num1 = num_strings, max_num - 1 do
				temp_strings = temp_strings .. " "
			end
			return temp_strings
		end
	
	end
end

function wind_alt_order()

	local swap_alt = ""
	local swap_alt_num = 0
	local swap_dir = ""
	local swap_spd = ""
	
	if forec_alt_2_num_mod > forec_alt_1_num_mod then
		-- swap 2 - 1
		swap_alt = forec_alt_2_mod
		swap_alt_num = forec_alt_2_num_mod
		swap_dir = forec_dir_2_mod
		swap_spd = forec_spd_2_mod
		forec_alt_2_mod = forec_alt_1_mod
		forec_alt_2_num_mod = forec_alt_1_num_mod
		forec_dir_2_mod = forec_dir_1_mod
		forec_spd_2_mod = forec_spd_1_mod
		forec_alt_1_mod = swap_alt
		forec_alt_1_num_mod = swap_alt_num
		forec_dir_1_mod = swap_dir
		forec_spd_1_mod = swap_spd
	end
	if forec_alt_3_num_mod > forec_alt_2_num_mod then
		if forec_alt_3_num_mod  > forec_alt_1_num_mod then
			-- swap 3 - 1
			swap_alt = forec_alt_3_mod
			swap_alt_num = forec_alt_3_num_mod
			swap_dir = forec_dir_3_mod
			swap_spd = forec_spd_3_mod
			forec_alt_3_mod = forec_alt_1_mod
			forec_alt_3_num_mod = forec_alt_1_num_mod
			forec_dir_3_mod = forec_dir_1_mod
			forec_spd_3_mod = forec_spd_1_mod
			forec_alt_1_mod = swap_alt
			forec_alt_1_num_mod = swap_alt_num
			forec_dir_1_mod = swap_dir
			forec_spd_1_mod = swap_spd
			-- swap 3 - 2
			swap_alt = forec_alt_3_mod
			swap_alt_num = forec_alt_3_num_mod
			swap_dir = forec_dir_3_mod
			swap_spd = forec_spd_3_mod
			forec_alt_3_mod = forec_alt_2_mod
			forec_alt_3_num_mod = forec_alt_2_num_mod
			forec_dir_3_mod = forec_dir_2_mod
			forec_spd_3_mod = forec_spd_2_mod
			forec_alt_2_mod = swap_alt
			forec_alt_2_num_mod = swap_alt_num
			forec_dir_2_mod = swap_dir
			forec_spd_2_mod = swap_spd
		else
		-- swap 3 - 2
			swap_alt = forec_alt_3_mod
			swap_alt_num = forec_alt_3_num_mod
			swap_dir = forec_dir_3_mod
			swap_spd = forec_spd_3_mod
			forec_alt_3_mod = forec_alt_2_mod
			forec_alt_3_num_mod = forec_alt_2_num_mod
			forec_dir_3_mod = forec_dir_2_mod
			forec_spd_3_mod = forec_spd_2_mod
			forec_alt_2_mod = swap_alt
			forec_alt_2_num_mod = swap_alt_num
			forec_dir_2_mod = swap_dir
			forec_spd_2_mod = swap_spd
		end
	end

end

function check_navdata()
	
	local navdata_status1 = 0
	local navdata_status2 = 0
	
	local line = ""
	local line_cnt = 0
	local custom_ils1 = ""
	local custom_ils2 = ""
	local custom_nav1 = ""
	local custom_nav2 = ""
	
	cust_def_data = 0
	custom_ils_status = 0
	airac_navdata = ""
	airac_navdata_active = ""
	
	-- check Custom NavData
	file_name_path = "Custom Data/"
	
	if B738DR_cust_nav_data ~= 0 then
		custom_ils_status = 1	-- we want use UserData
	end
	
	file_name = "Custom Data/user_nav.dat"
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		-- read 2 lines
		line_cnt = 0
		line = file_navdata:read()
		while line do
			line_cnt = line_cnt + 1
			if line_cnt == 1 then
				custom_ils1 = line
			elseif line_cnt == 2 then
				custom_ils2 = line
			else
				break
			end
			line = file_navdata:read()
		end
		file_navdata:close()
	end
	
	file_name = file_name_path .. "earth_nav.dat"
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		navdata_status1 = navdata_status1 + 1
		if custom_ils_status == 1 then
			-- read 2 lines
			line_cnt = 0
			line = file_navdata:read()
			while line do
				line_cnt = line_cnt + 1
				if line_cnt == 1 then
					custom_nav1 = line
				elseif line_cnt == 2 then
					custom_nav2 = line
				else
					break
				end
				line = file_navdata:read()
			end
			if update_apt_dat == 0 and custom_nav1 == custom_ils1 and custom_nav2 == custom_ils2 then
				custom_ils_status = 0
			end
		end
		file_navdata:close()
	end
	file_name = file_name_path .. "earth_fix.dat"
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		navdata_status1 = navdata_status1 + 1
		file_navdata:close()
	end
	file_name = file_name_path .. "earth_awy.dat"
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		navdata_status1 = navdata_status1 + 1
		file_navdata:close()
	end
	
	if navdata_status1 > 2 then
		file_name_path_nav = file_name_path
		file_name_path_fix = file_name_path
		file_name_path_awy = file_name_path
		cust_def_data = 1
		read_navdata2()
	else
		custom_ils_status = 0
		
		-- check Default NavData
		file_name_path = "Resources/default data/"
		file_name_path_nav = file_name_path
		file_name = file_name_path_nav .. "earth_nav.dat"
		file_navdata = io.open(file_name, "r")
		if file_navdata ~= nil then
			navdata_status2 = navdata_status2 + 1
			file_navdata:close()
		end
		
		file_name_path_fix = file_name_path
		file_name = file_name_path_fix .. "earth_fix.dat"
		file_navdata = io.open(file_name, "r")
		if file_navdata ~= nil then
			navdata_status2 = navdata_status2 + 1
			file_navdata:close()
		end
		
		file_name_path_awy = file_name_path
		file_name = file_name_path_awy .. "earth_awy.dat"
		file_navdata = io.open(file_name, "r")
		if file_navdata ~= nil then
			navdata_status2 = navdata_status2 + 1
			file_navdata:close()
		end
		
		if navdata_status2 > 2 then
			cust_def_data = 2
			file_name_path = "Resources/default data/"
			read_navdata2()
		end
	end
	
end

function shift_pos(in_shift)
	local file_name_input = ""
	local file_name_output = ""
	file_name_input = "Aircraft/B737-800X_FM40/fmod/b738_4k.snd"
	local file_name_input0 = io.open(file_name_input, "r")
	
	file_name_output = "Aircraft/B737-800X_FM40/fmod/b738.txt"
	local file_name_output0 = io.open(file_name_output, "w")
	if file_name_input0 ~= nil and file_name_output0 ~= nil then
		local line = "" 
		local i = 0
		local j = 0
		local k = 0
		local line_trim = ""
		local line_char = ""
		local line_lenght = 0
		raw_table('words')
		local words = {}
		local output_buf = ""
		
		line = file_name_input0:read()
		while line do
			i = 0
			j = 0
			line_trim = ""
			line_char = ""
			words = {}
			line_lenght = string.len(line)
			if line_lenght > 0 then
				for k = 1, line_lenght do
					line_char = string.sub(line, k, k)
					if string.byte(line_char) == 32 or string.byte(line_char) == 9 then		-- space or tab
						if i == 1 then
							j = j + 1
							words[j] = line_trim
							i = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						i = 1
					end
				end
				if string.len(line_trim) > 0 then
					j = j + 1
					words[j] = line_trim
				end
				
				if j > 0 then
					if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
						words[j] = string.sub(words[j], 1, -2)
					end
				end
				
				i = 0
				if j > 3 then
					if words[1] == "VEH_XYZ" then
						k = tonumber(words[3])
						if k ~= nil then
							k = k + in_shift
							words[3] = string.format("%6.3f", k)
							output_buf = "\t" .. words[1]
							for k = 2, j do
								output_buf = output_buf .. " " .. words[k]
							end
							i = 1
						end
					end
				end
				if i == 0 then
					output_buf = line
				end
				output_buf = output_buf .. "\n"
				file_name_output0:write(output_buf)
			end
			line = file_name_input0:read()
		end
		file_name_input0:close()
		file_name_output0:close()
	end
end


function read_navdata2()
	
	local line = "" 
	local i = 0
	local j = 0
	local k = 0
	local build = ""
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local old_lat = ""
	local old_lon = ""
	local old_id = ""
	local old_freq = ""
	local old_icao = ""
	local old_type = 0
	local skip = 0
	raw_table('words')
	local words = {}
	
	file_name = file_name_path_nav .. "earth_nav.dat"
	file_navdata = io.open(file_name, "r")
	
	local file_name_cd = ""
	local file_navdata_cd = 0
	local line_cnt = 0
	local line_cd = ""
	
	local line_ga = ""
	local line_ga_write = ""
	local line_ga_cnt = 0
	raw_table('navdata_ga')
	local navdata_ga = {}
	raw_table('navdata_ga_num')
	local navdata_ga_num = {}
	local word_number = 0
	local word_number2 = 0
	local line_user_cnt = 0
	raw_table('navdata_user')
	local navdata_user = {}
	raw_table('navdata_user_num')
	local navdata_user_num = {}
	local ga_runway_found = 0
	local ga_runway_number = 0
	local ga_runway_ext = ""
	local rnw_data_number = 0
	local rnw_data_ext = ""
	local num_same_runway = 0
	local idx_same_runway = 0
	
	local file_name_rw = "Custom Data/runways.dat"
	local navdata_icao_num = 0
	raw_table('navdata_icao')
	local navdata_icao = {}
	raw_table('navdata_icao_mv')
	local navdata_icao_mv = {}
	raw_table('navdata_icao_lat')
	local navdata_icao_lat = {}
	raw_table('navdata_icao_lon')
	local navdata_icao_lon = {}
	raw_table('navdata_icao_ta')
	local navdata_icao_ta = {}
	raw_table('navdata_icao_tl')
	local navdata_icao_tl = {}
	
	local compens = 0
	local compens_lat = 0
	local compens_lon = 0
	local compens_crs = 0
	
	
	local file_name_ga = "Custom Scenery/Global Airports/Earth nav data/earth_nav.dat"
	local file_navdata_ga = io.open(file_name_ga, "r")
	if file_navdata_ga ~= nil then
		line_ga = file_navdata_ga:read()
		while line_ga do
			i = 0
			j = 0
			line_trim = ""
			line_char = ""
			words = {}
			line_lenght = string.len(line_ga)
			if line_lenght > 0 then
				for k = 1, line_lenght do
					line_char = string.sub(line_ga, k, k)
					if line_char == " " then
						if i == 1 then
							j = j + 1
							words[j] = line_trim
							i = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						i = 1
					end
				end
				if string.len(line_trim) > 0 then
					j = j + 1
					words[j] = line_trim
				end
				
				if j > 0 then
					if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
						words[j] = string.sub(words[j], 1, -2)
					end
					
					word_number = tonumber(words[1])
					if word_number ~= nil then
						if word_number < 1000 then
							line_ga_cnt = line_ga_cnt + 1
							navdata_ga[line_ga_cnt] = {}
							navdata_ga_num[line_ga_cnt] = j
							if j > 10 then
								ga_runway_number = tonumber((words[11]))
								if ga_runway_number == nil then
									if string.len(words[11]) < 1 then
										words[11] = "00"
									elseif string.len(words[11]) < 2 then
										words[11] = "00" .. words[11]
									elseif string.len(words[11]) < 3 then
										words[11] = "0" .. words[11]
									end
								else
									if string.len(words[11]) < 1 then
										words[11] = "00"
									elseif string.len(words[11]) < 2 then
										words[11] = "0" .. words[11]
									end
								end
							end
							for k = 1, j do
								navdata_ga[line_ga_cnt][k] = words[k]
							end
						end
					end
				end
			end
			line_ga = file_navdata_ga:read()
		end
		file_navdata_ga:close()
	else
		custom_ils_status = 0
	end
	
	file_name_cd = "Custom Data/user_nav.dat"
	if custom_ils_status == 1 then
		file_navdata_cd = io.open(file_name_cd, "w")
		if file_navdata_cd == nil then
			custom_ils_status = 0
		end
	else
		file_navdata_cd = io.open(file_name_cd, "r")
		if file_navdata_cd ~= nil then
			line_ga = file_navdata_cd:read()
			while line_ga do
				i = 0
				j = 0
				line_trim = ""
				line_char = ""
				words = {}
				line_lenght = string.len(line_ga)
				if line_lenght > 0 then
					for k = 1, line_lenght do
						line_char = string.sub(line_ga, k, k)
						if line_char == " " then
							if i == 1 then
								j = j + 1
								words[j] = line_trim
								i = 0
								line_trim = ""
							end
						else
							line_trim = line_trim .. line_char
							i = 1
						end
					end
					if string.len(line_trim) > 0 then
						j = j + 1
						words[j] = line_trim
					end
					
					if j > 0 then
						if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
							words[j] = string.sub(words[j], 1, -2)
						end
						
						word_number = tonumber(words[1])
						if word_number ~= nil then
							if word_number < 1000 then
								line_user_cnt = line_user_cnt + 1
								navdata_user[line_user_cnt] = {}
								navdata_user_num[line_user_cnt] = j
								for k = 1, j do
									navdata_user[line_user_cnt][k] = words[k]
								end
							end
						end
					end
				end
				line_ga = file_navdata_cd:read()
			end
			file_navdata_cd:close()
		end
	end
	
	-- CIFP airports
	file_name_rw = "Custom Data/airports.dat"
	local file_navdata_rw = io.open(file_name_rw, "r")
	if file_navdata_rw ~= nil then
		line_ga = file_navdata_rw:read()
		while line_ga do
			i = 0
			j = 0
			line_trim = ""
			line_char = ""
			words = {}
			line_lenght = string.len(line_ga)
			if line_lenght > 0 then
				for k = 1, line_lenght do
					line_char = string.sub(line_ga, k, k)
					if line_char == "," then
						j = j + 1
						words[j] = line_trim
						line_trim = ""
					else
						line_trim = line_trim .. line_char
					end
				end
				if string.len(line_trim) > 0 then
					j = j + 1
					words[j] = line_trim
				end
				
				if j > 5 then
					if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
						words[j] = string.sub(words[j], 1, -2)
					end
					
					navdata_icao_num = navdata_icao_num + 1
					navdata_icao[navdata_icao_num] = words[1]
					if tonumber(words[2]) == nil then
						navdata_icao_lat[navdata_icao_num] = 0
					else
						navdata_icao_lat[navdata_icao_num] = tonumber(words[2])
					end
					if tonumber(words[3]) == nil then
						navdata_icao_lon[navdata_icao_num] = 0
					else
						navdata_icao_lon[navdata_icao_num] = tonumber(words[3])
					end
					if tonumber(words[4]) == nil then
						navdata_icao_ta[navdata_icao_num] = 0
					else
						navdata_icao_ta[navdata_icao_num] = tonumber(words[4])
					end
					if tonumber(words[5]) == nil then
						navdata_icao_tl[navdata_icao_num] = 0
					else
						navdata_icao_tl[navdata_icao_num] = tonumber(words[5])
					end
					if tonumber(words[6]) == nil then
						navdata_icao_mv[navdata_icao_num] = 0
					else
						navdata_icao_mv[navdata_icao_num] = tonumber(words[6])
					end
				end
			end
			line_ga = file_navdata_rw:read()
		end
		file_navdata_rw:close()
	end
	
	-- looking for AIRAC
	line = file_navdata:read()
	if custom_ils_status == 1 then
		file_navdata_cd:write(line)
		file_navdata_cd:write("\n")
	end
	while line do
		i,j = string.find(line, "cycle ")
		if i ~= nil then
			break
		end
		line = file_navdata:read()
	end
	if i ~= nil then
		airac_cycle = string.sub(line, (j+1), (j+4))
		i,j = string.find(line, "build ")
		build = string.sub(line, (j+1), (j+8))
		airac_navdata = string.sub(build, 3, 8) .. airac_cycle
		if custom_ils_status == 1 then
			file_navdata_cd:write(line)
			file_navdata_cd:write("\n")
			file_navdata_cd:write("\n")
		end
	end
	
	local ils_tmp = 0
	-- looking for NAV
	line = file_navdata:read()
	while line do
		i = 0
		j = 0
		line_trim = ""
		line_char = ""
		words = {}
		line_lenght = string.len(line)
		if line_lenght > 0 then
			for k = 1, line_lenght do
				line_char = string.sub(line, k, k)
				if line_char == " " then
					if i == 1 then
						j = j + 1
						words[j] = line_trim
						i = 0
						line_trim = ""
					end
				else
					line_trim = line_trim .. line_char
					i = 1
				end
			end
			if string.len(line_trim) > 0 then
				j = j + 1
				words[j] = line_trim
			end
			
			if j > 0 then
				if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
					words[j] = string.sub(words[j], 1, -2)
				end
			end
			
			if custom_ils_status == 1 then
				if j > 11 and line_ga_cnt > 0 then
					word_number = tonumber(words[1])
					if word_number ~= nil then
						if (word_number >= 4 and word_number <= 9) or word_number == 12 then
							for k = 1, line_ga_cnt do
								if navdata_ga_num[k] > 11 then
									if navdata_ga[k][1] == words[1] then
										if navdata_ga[k][8] == words[8] then
											if navdata_ga[k][9] == words[9] then
												if navdata_ga[k][10] == words[10] then
													
													-- ** runway exist ??? **
													ga_runway_found = 0
													if rnw_data_num > 0 then
														for rrr = 1, rnw_data_num do
															if rnw_data[rrr][1] == navdata_ga[k][9] and rnw_data[rrr][2] == navdata_ga[k][11] then
																ga_runway_found = 1
																break
															end
														end
														if ga_runway_found == 0 then
															if string.len(navdata_ga[k][11]) > 2 then
																ga_runway_number = tonumber(string.sub(navdata_ga[k][11], 1, 2))
																ga_runway_ext = string.sub(navdata_ga[k][11], 3, 3)
															else
																ga_runway_number = tonumber(navdata_ga[k][11])
																ga_runway_ext = ""
															end
															for rrr = 1, rnw_data_num do
																if rnw_data[rrr][1] == navdata_ga[k][9] then
																	if string.len(rnw_data[rrr][2]) > 2 then
																		rnw_data_number = tonumber(string.sub(rnw_data[rrr][2], 1, 2))
																		rnw_data_ext = string.sub(rnw_data[rrr][2], 3, 3)
																	else
																		rnw_data_number = tonumber(rnw_data[rrr][2])
																		rnw_data_ext = ""
																	end
																	if rnw_data_number ~= nil and ga_runway_number ~= nil then
																		rnw_data_number = (rnw_data_number - ga_runway_number + 36) % 36
																		if rnw_data_number > 18 then
																			rnw_data_number = rnw_data_number - 36
																		end
																		if math.abs(rnw_data_number) < 2 and rnw_data_ext == ga_runway_ext then
																			navdata_ga[k][11] = rnw_data[rrr][2]
																			ga_runway_found = 1
																			break
																		end
																	end
																end
															end
														end
														if ga_runway_found == 0 then
															num_same_runway = 0
															for rrr = 1, rnw_data_num do
																if rnw_data[rrr][1] == navdata_ga[k][9] then
																	if string.sub(rnw_data[rrr][2], 1, 2) == string.sub(navdata_ga[k][11], 1, 2) then
																		num_same_runway = num_same_runway + 1
																		idx_same_runway = rrr
																	end
																end
															end
															if num_same_runway == 1 then
																navdata_ga[k][11] = rnw_data[idx_same_runway][2]
															end
														end
													end
													-- ********************
													
													line_ga_write = navdata_ga[k][1] .. " " .. navdata_ga[k][2] .. " " .. navdata_ga[k][3] .. " " .. navdata_ga[k][4]
													line_ga_write = line_ga_write .. " " .. words[5].. " " .. words[6].. " " .. words[7]
													
													for i = 8, 11 do
														line_ga_write = line_ga_write .. " " .. navdata_ga[k][i]
													end
													for i = 12, j do
														line_ga_write = line_ga_write .. " " .. words[i]
													end
													
													line_ga_write = line_ga_write .. "\n"
													file_navdata_cd:write(line_ga_write)
												end
												break
											end
										end
									end
								end
							end
						end
					end
				end
			else
				if j > 11 then
					if line_ga_cnt > 0 then
						word_number = tonumber(words[1])
						if word_number ~= nil then
							if (word_number >= 4 and word_number <= 9) or word_number == 12 then
								for k = 1, line_ga_cnt do
									if navdata_ga_num[k] > 11 then
										if navdata_ga[k][1] == words[1] then
											if navdata_ga[k][8] == words[8] then
												if navdata_ga[k][9] == words[9] then
													if navdata_ga[k][10] == words[10] then
														for i = 1, navdata_ga_num[k] do
															words[i] = navdata_ga[k][i]
														end
														j = navdata_ga_num[k]
													end
													break
												end
											end
										end
									end
								end
							end
						end
					end
					if line_user_cnt > 0 then
						word_number = tonumber(words[1])
						if word_number ~= nil then
							if (word_number >= 4 and word_number <= 9) or word_number == 12 then
								for k = 1, line_user_cnt do
									if navdata_user_num[k] > 11 then
										if navdata_user[k][1] == words[1] then
											if navdata_user[k][8] == words[8] then
												if navdata_user[k][9] == words[9] then
													if navdata_user[k][10] == words[10] then
														for i = 1, navdata_user_num[k] do
															words[i] = navdata_user[k][i]
														end
														j = navdata_user_num[k]
													end
													break
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
			
			if j > 11 then
				
				-- GLS navaids
				if words[1] == "15" or words[1] == "16" then
					gls_nav_num = gls_nav_num + 1
					gls_nav[gls_nav_num] = {true, true, true, true, true, true, true, true, true, true, true}
					gls_nav[gls_nav_num][1] = words[8]				--id
					gls_nav[gls_nav_num][2] = tonumber(words[2]) 	--lat
					gls_nav[gls_nav_num][3] = tonumber(words[3]) 	--lon
					gls_nav[gls_nav_num][4] = words[11] 			--runway
					gls_nav[gls_nav_num][5] = tonumber(string.sub(words[7], 4, -1))	--course
					gls_nav[gls_nav_num][6] = tonumber(words[5]) 	--channel
					gls_nav[gls_nav_num][7] = words[9] 				--ICAO
					gls_nav[gls_nav_num][8] = words[10]				-- reg code
					gls_nav[gls_nav_num][9] = tonumber(string.sub(words[7], 1, 3)) / 100	-- GS degree
					gls_nav[gls_nav_num][10] = tonumber(words[4])	-- height
					if words[1] == "15" then
						gls_nav[gls_nav_num][11] = 0	-- type GLS
					else
						gls_nav[gls_nav_num][11] = 1	-- type LPV
					end
					old_type = 0
				-- end
				
				-- ILS navaids
				elseif words[1] == "4" and (string.sub(words[12], 1, 3) == "ILS" or string.sub(words[12], 1, 3) == "LOC" 
				or string.sub(words[12], 1, 3) == "LDA" or string.sub(words[12], 1, 3) == "IGS") then
					ils_nav_num = ils_nav_num + 1
					ils_nav[ils_nav_num] = {true, true, true, true, true, true, true, true, true, true}
					ils_nav[ils_nav_num][1] = words[8]				--id
					ils_nav[ils_nav_num][2] = tonumber(words[2]) 	--lat
					ils_nav[ils_nav_num][3] = tonumber(words[3]) 	--lon
					ils_nav[ils_nav_num][4] = words[11] 			--runway
					ils_tmp = tonumber(words[7])
					if ils_tmp > 360 then
						ils_nav[ils_nav_num][10] = math.floor(ils_tmp / 360)	--mag course
						ils_nav[ils_nav_num][5] = ils_tmp - ils_nav[ils_nav_num][10]	--true course
					else
						ils_nav[ils_nav_num][10] = -1	--mag course
						ils_nav[ils_nav_num][5] = ils_tmp	--true course
					end
					ils_nav[ils_nav_num][6] = tonumber(words[5]) 	--frequency
					ils_nav[ils_nav_num][7] = words[9] 				--ICAO
					ils_nav[ils_nav_num][8] = words[10]				-- reg code
					ils_nav[ils_nav_num][9] = 0						-- type LOC/GS (ILS), IGS
					if string.sub(words[12], 1, 3) == "LOC" then
						ils_nav[ils_nav_num][9] = 2						-- type LOC/GS
					elseif string.sub(words[12], 1, 3) == "LDA" then
						ils_nav[ils_nav_num][9] = 3						-- type LOC/GS
					end
					old_type = 0
				elseif words[1] == "5" then
					ils_nav_num = ils_nav_num + 1
					ils_nav[ils_nav_num] = {true, true, true, true, true, true, true, true, true, true}
					ils_nav[ils_nav_num][1] = words[8]				--id
					ils_nav[ils_nav_num][2] = tonumber(words[2]) 	--lat
					ils_nav[ils_nav_num][3] = tonumber(words[3]) 	--lon
					ils_nav[ils_nav_num][4] = words[11] 			--runway
					ils_tmp = tonumber(words[7])
					if ils_tmp > 360 then
						ils_nav[ils_nav_num][10] = math.floor(ils_tmp / 360)	--mag course
						ils_nav[ils_nav_num][5] = ils_tmp - ils_nav[ils_nav_num][10]	--true course
					else
						ils_nav[ils_nav_num][10] = -1	--mag course
						ils_nav[ils_nav_num][5] = ils_tmp	--true course
					end
					ils_nav[ils_nav_num][6] = tonumber(words[5]) 	--frequency
					ils_nav[ils_nav_num][7] = words[9] 				--ICAO
					ils_nav[ils_nav_num][8] = words[10]				-- reg code
					ils_nav[ils_nav_num][9] = 1						-- only LOC
					old_type = 0
				end
				
				if old_id == words[8] and old_freq == words[5] then
					if old_type == 1 and words[1] == "12" then -- VOR-DME, VORTAG
						earth_nav[earth_nav_num][1] = 2
						old_type = 2
					end
				end
				if old_type == 5 and words[1] == "12" and old_icao == words[9] then -- ILS -> APT
					skip = 1
				elseif old_type == 5 and words[1] == "6" and old_icao == words[9] then -- ILS -> APT
					skip = 1
				elseif old_type == 5 and words[1] == "4" and old_icao == words[9] then -- ILS -> APT
					skip = 1
				end
				if skip == 1 then
					skip = 0
					old_id = words[8]
					old_freq = words[5]
					old_icao = words[9]
				else
					if words[1] == "3" then	-- VOR
						earth_nav_num = earth_nav_num + 1
						earth_nav[earth_nav_num] = {true, true, true, true, true, true, true, true, true}  		--new row
						earth_nav[earth_nav_num][1] = 1			--type 1-VOR
						earth_nav[earth_nav_num][2] = tonumber(words[2])  --lat
						earth_nav[earth_nav_num][3] = tonumber(words[3])  --lon
						earth_nav[earth_nav_num][4] = words[8]  --id
						earth_nav[earth_nav_num][5] = words[9]  --ICAO
						earth_nav[earth_nav_num][6] = words[5] --freq
						earth_nav[earth_nav_num][7] = words[11] --name
						earth_nav[earth_nav_num][8] = words[10] --reg_code
						earth_nav[earth_nav_num][9] = tonumber(words[7]) --mag variation
						old_type = 1
						old_freq = words[5]
						old_icao = "" --words[9]
						old_id = words[8]
					elseif words[1] == "2" then	-- NDB
						earth_nav_num = earth_nav_num + 1
						earth_nav[earth_nav_num] = {true, true, true, true, true, true, true, true, true}  		--new row
						earth_nav[earth_nav_num][1] = 3			--type 3-NDB
						earth_nav[earth_nav_num][2] = tonumber(words[2])  --lat
						earth_nav[earth_nav_num][3] = tonumber(words[3])  --lon
						earth_nav[earth_nav_num][4] = words[8]  --id
						earth_nav[earth_nav_num][5] = words[9]  --ICAO
						earth_nav[earth_nav_num][6] = words[5] --freq
						earth_nav[earth_nav_num][7] = words[11] --name
						earth_nav[earth_nav_num][8] = words[10] --reg_code
						earth_nav[earth_nav_num][9] = 0
						old_type = 3
						old_freq = words[5]
						old_icao = "" --words[9]
						old_id = words[8]
					elseif words[1] == "13" then	-- DME
						earth_nav_num = earth_nav_num + 1
						earth_nav[earth_nav_num] = {true, true, true, true, true, true, true, true, true}  		--new row
						earth_nav[earth_nav_num][1] = 5			--type 5-only DME
						earth_nav[earth_nav_num][2] = tonumber(words[2])  --lat
						earth_nav[earth_nav_num][3] = tonumber(words[3])  --lon
						earth_nav[earth_nav_num][4] = words[8]  --id
						earth_nav[earth_nav_num][5] = words[9]  --ICAO
						earth_nav[earth_nav_num][6] = words[5] --freq
						earth_nav[earth_nav_num][7] = words[11] --name
						earth_nav[earth_nav_num][8] = words[10] --reg_code
						earth_nav[earth_nav_num][9] = 0
						old_type = 5
						old_freq = words[5]
						old_icao = "" --words[9]
						old_id = words[8]
					else
						old_type = 0
					end
				end
			end
		end
		line = file_navdata:read()
	end
	file_navdata:close()
	
	if custom_ils_status == 1 then
		file_navdata_cd:write("99\n")
		file_navdata_cd:close()
		custom_ils_status = 2
	end
	
	-- fixes
	file_name = file_name_path_fix .. "earth_fix.dat"
	file_navdata = io.open(file_name, "r")
	-- looking for fix
	line = file_navdata:read()
	line = file_navdata:read()
	line = file_navdata:read()
	while line do
		i = 0
		j = 0
		line_trim = ""
		line_char = ""
		line_lenght = string.len(line)
		if line_lenght > 0 then
			for k = 1, line_lenght do
				line_char = string.sub(line, k, k)
				if line_char == " " then
					if i == 1 then
						j = j + 1
						words[j] = line_trim
						i = 0
						line_trim = ""
					end
				else
					line_trim = line_trim .. line_char
					i = 1
				end
			end
			if string.len(line_trim) > 0 then
				j = j + 1
				words[j] = line_trim
			end
			
			if j > 0 then
				if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
					words[j] = string.sub(words[j], 1, -2)
				end
			end
			
			
			if j > 4 then
				earth_nav_num = earth_nav_num + 1
				earth_nav[earth_nav_num] = {true, true, true, true, true, true, true, true, true}  		--new row
				earth_nav[earth_nav_num][1] = 4			--type 4-FIX
				earth_nav[earth_nav_num][2] = tonumber(words[1])  --lat
				earth_nav[earth_nav_num][3] = tonumber(words[2])  --lon
				earth_nav[earth_nav_num][4] = words[3]  --id
				earth_nav[earth_nav_num][5] = words[4]  --ICAO
				earth_nav[earth_nav_num][6] = "" --freq
				earth_nav[earth_nav_num][7] = "" --name
				earth_nav[earth_nav_num][8] = words[5] --reg_code
				earth_nav[earth_nav_num][9] = 0
			end
		end
		line = file_navdata:read()
	end
	file_navdata:close()
	
	find_airac_cycle2(airac_cycle)
	
	-- georef fixes
	file_name = file_name_path .. "user_fix_georef.dat"
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		-- looking for fix
		line = file_navdata:read()
		line = file_navdata:read()
		line = file_navdata:read()
		while line do
			i = 0
			j = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(line)
			if line_lenght > 0 then
				for k = 1, line_lenght do
					line_char = string.sub(line, k, k)
					if line_char == " " then
						if i == 1 then
							j = j + 1
							words[j] = line_trim
							i = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						i = 1
					end
				end
				if string.len(line_trim) > 0 then
					j = j + 1
					words[j] = line_trim
				end
				
				if j > 0 then
					if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
						words[j] = string.sub(words[j], 1, -2)
					end
				end
				
				
				if j > 4 then
					earth_nav_num = earth_nav_num + 1
					earth_nav[earth_nav_num] = {true, true, true, true, true, true, true, true, true}  		--new row
					earth_nav[earth_nav_num][1] = 4			--type 4-FIX
					earth_nav[earth_nav_num][2] = tonumber(words[1])  --lat
					earth_nav[earth_nav_num][3] = tonumber(words[2])  --lon
					earth_nav[earth_nav_num][4] = words[3]  --id
					earth_nav[earth_nav_num][5] = words[4]  --ICAO
					earth_nav[earth_nav_num][6] = "" --freq
					earth_nav[earth_nav_num][7] = "" --name
					earth_nav[earth_nav_num][8] = words[5] --reg_code
					earth_nav[earth_nav_num][9] = 0
				end
			end
			line = file_navdata:read()
		end
		file_navdata:close()
	end
	
	-- Holdings
	hold_nav_num = 0
	hold_nav = {}
	file_name = file_name_path .. "earth_hold.dat"
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		-- looking for hold
		line = file_navdata:read()
		line = file_navdata:read()
		line = file_navdata:read()
		while line do
			i = 0
			j = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(line)
			if line_lenght > 0 then
				for k = 1, line_lenght do
					line_char = string.sub(line, k, k)
					if line_char == " " then
						if i == 1 then
							j = j + 1
							words[j] = line_trim
							i = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						i = 1
					end
				end
				if string.len(line_trim) > 0 then
					j = j + 1
					words[j] = line_trim
				end
				
				if j > 0 then
					if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
						words[j] = string.sub(words[j], 1, -2)
					end
				end
				
				
				if j > 10 then
					hold_nav_num = hold_nav_num + 1
					hold_nav[hold_nav_num] = {true, true, true, true, true, true, true, true, true}  		--new row
					hold_nav[hold_nav_num][1] = words[1]			--id
					hold_nav[hold_nav_num][2] = words[2] 			--reg code
					hold_nav[hold_nav_num][3] = tonumber(words[5])	--inbound crs
					hold_nav[hold_nav_num][4] = tonumber(words[6])	--time
					hold_nav[hold_nav_num][5] = tonumber(words[7])	--distance
					if words[8] == "L" then
						hold_nav[hold_nav_num][6] = 0				--turn L
					else
						hold_nav[hold_nav_num][6] = 1				--turn R
					end
					hold_nav[hold_nav_num][7] = tonumber(words[9])	--altitude min
					hold_nav[hold_nav_num][8] = tonumber(words[10])	--altitude max
					hold_nav[hold_nav_num][9] = tonumber(words[11])	--speed
				end
			end
			line = file_navdata:read()
		end
		file_navdata:close()
	end
	--dump_ils_table()
end


function dump_ils_table()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if ils_nav_num > 0 then
		for vvv = 1, ils_nav_num do
			fms_line = ils_nav[vvv][1] .. " " .. tostring(ils_nav[vvv][2]) .. " " .. tostring(ils_nav[vvv][3]) .. " " .. ils_nav[vvv][4] .. " "
			fms_line = fms_line .. tostring(ils_nav[vvv][5]) .. " " .. tostring(ils_nav[vvv][6]) .. " " .. ils_nav[vvv][7] .. " " .. ils_nav[vvv][8] .. " "
			fms_line = fms_line .. tostring(ils_nav[vvv][10]) .. "\n"
			file_navdata2:write(fms_line)
		end
	end
	file_navdata2:close()
end

function create_idx_table()
	
	local ii = 0
	local tmp_byte = 0
	local tmp_idx = 0
	
	-- clear idx tables
	idx_earth_nav = {}
	idx_apt = {}
	idx_ils = {}
	idx_ils_icao = {}
	idx_gls = {}
	idx_gls_icao = {}
	idx_rnw = {}
	idx_awy = {}
	idx_hold = {}
	
	for ii = 48, 57 do
		idx_earth_nav[ii] = {}
		idx_earth_nav[ii][99999] = 0
		idx_apt[ii] = {}
		idx_apt[ii][99999] = 0
		idx_ils[ii] = {}
		idx_ils[ii][99999] = 0
		idx_ils_icao[ii] = {}
		idx_ils_icao[ii][99999] = 0
		idx_gls[ii] = {}
		idx_gls[ii][99999] = 0
		idx_gls_icao[ii] = {}
		idx_gls_icao[ii][99999] = 0
		idx_rnw[ii] = {}
		idx_rnw[ii][99999] = 0
		idx_awy[ii] = {}
		idx_awy[ii][99999] = 0
		idx_hold[ii] = {}
		idx_hold[ii][99999] = 0
	end
	for ii = 65, 90 do
		idx_earth_nav[ii] = {}
		idx_earth_nav[ii][99999] = 0
		idx_apt[ii] = {}
		idx_apt[ii][99999] = 0
		idx_ils[ii] = {}
		idx_ils[ii][99999] = 0
		idx_ils_icao[ii] = {}
		idx_ils_icao[ii][99999] = 0
		idx_gls[ii] = {}
		idx_gls[ii][99999] = 0
		idx_gls_icao[ii] = {}
		idx_gls_icao[ii][99999] = 0
		idx_rnw[ii] = {}
		idx_rnw[ii][99999] = 0
		idx_awy[ii] = {}
		idx_awy[ii][99999] = 0
		idx_hold[ii] = {}
		idx_hold[ii][99999] = 0
	end
	
	if earth_nav_num > 0 then
		for ii = 1, earth_nav_num do
			if string.len(earth_nav[ii][4]) > 0 then
				tmp_byte = string.byte(string.sub(earth_nav[ii][4], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_earth_nav[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_earth_nav[tmp_byte][99999] = tmp_idx
						idx_earth_nav[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	if apt_data_num > 0 then
		for ii = 1, apt_data_num do
			if string.len(apt_data[ii][1]) > 0 then
				tmp_byte = string.byte(string.sub(apt_data[ii][1], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_apt[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_apt[tmp_byte][99999] = tmp_idx
						idx_apt[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	if ils_nav_num > 0 then
		for ii = 1, ils_nav_num do
			if string.len(ils_nav[ii][1]) > 0 then
				tmp_byte = string.byte(string.sub(ils_nav[ii][1], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_ils[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_ils[tmp_byte][99999] = tmp_idx
						idx_ils[tmp_byte][tmp_idx] = ii
					end
				end
			end
			if string.len(ils_nav[ii][7]) > 0 then
				tmp_byte = string.byte(string.sub(ils_nav[ii][7], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_ils_icao[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_ils_icao[tmp_byte][99999] = tmp_idx
						idx_ils_icao[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	if gls_nav_num > 0 then
		for ii = 1, gls_nav_num do
			if string.len(gls_nav[ii][1]) > 0 then
				tmp_byte = string.byte(string.sub(gls_nav[ii][1], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_gls[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_gls[tmp_byte][99999] = tmp_idx
						idx_gls[tmp_byte][tmp_idx] = ii
					end
				end
			end
			if string.len(gls_nav[ii][7]) > 0 then
				tmp_byte = string.byte(string.sub(gls_nav[ii][7], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_gls_icao[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_gls_icao[tmp_byte][99999] = tmp_idx
						idx_gls_icao[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	if rnw_data_num > 0 then
		for ii = 1, rnw_data_num do
			if string.len(rnw_data[ii][1]) > 0 then
				tmp_byte = string.byte(string.sub(rnw_data[ii][1], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_rnw[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_rnw[tmp_byte][99999] = tmp_idx
						idx_rnw[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	if awy_data_num > 0 then
		for ii = 1, awy_data_num do
			if string.len(awy_data[ii][1]) > 0 then
				tmp_byte = string.byte(string.sub(awy_data[ii][1], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_awy[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_awy[tmp_byte][99999] = tmp_idx
						idx_awy[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	if hold_nav_num > 0 then
		for ii = 1, hold_nav_num do
			if string.len(hold_nav[ii][1]) > 0 then
				tmp_byte = string.byte(string.sub(hold_nav[ii][1], 1, 1))
				if (tmp_byte >= 48 and tmp_byte <= 57) or (tmp_byte >= 65 and tmp_byte <= 90) then
					tmp_idx = idx_hold[tmp_byte][99999]
					if tmp_idx < 99998 then
						tmp_idx = tmp_idx + 1
						idx_hold[tmp_byte][99999] = tmp_idx
						idx_hold[tmp_byte][tmp_idx] = ii
					end
				end
			end
		end
	end
	
	
	--dump_idx_table()
end


function dump_idx_table()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	for vvv = 48, 57 do
		fms_line = tostring(vvv) .. ">" .. tostring(idx_awy[vvv][99999]) .. "\n"
		file_navdata2:write(fms_line)
	end
	for vvv = 65, 90 do
		fms_line = tostring(vvv) .. ">" .. tostring(idx_awy[vvv][99999]) .. "\n"
		file_navdata2:write(fms_line)
	end
	file_navdata2:close()
end

function month_to_text(month_num)
	local ii = 0
	local result = ""
	
	for ii = 1, 12 do
		if month_table[ii][2] == month_num then
			result = month_table[ii][1]
			break
		end
	end
	return result
end

function find_airac_cycle2(a_c)

	local ii = 0
	local first_airac_num = 0
	local second_airac_num = 0
	local fix_date = 1483574400 	-- fix date 05.01.2017 00:00:00 GMT
	
	local temp_month = 1
	local temp_year = 17
	local temp_year2 = 0
	
	airac_navdata_active = ""
	
	if string.len(a_c) == 4 then
		first_airac_num = tonumber(string.sub(a_c, 1, 2))
		second_airac_num = tonumber(string.sub(a_c, 3, 4))
		if first_airac_num ~= nil and second_airac_num ~= nil then
			if first_airac_num >= 17 then
				ii = fix_date
				while temp_year <= first_airac_num do
					if temp_year < first_airac_num or (temp_year == first_airac_num and temp_month < second_airac_num) then
						-- generate second date (+ 28 days)
						ii = ii + 2419200
						temp_year2 = tonumber(os.date("!%y", ii))
						if temp_year2 > temp_year then
							temp_year = temp_year + 1
							temp_month = 1
						else
							temp_month = temp_month + 1
						end
					else
						break
					end
				end
				
				airac_navdata_active = month_to_text(os.date("!%m", ii))	-- month
				airac_navdata_active = airac_navdata_active .. os.date("!%d", ii)	-- day
				-- generate second date (+ 27 days)
				ii = ii + 2332800
				airac_navdata_active = airac_navdata_active .. month_to_text(os.date("!%m", ii))	-- month
				airac_navdata_active = airac_navdata_active .. os.date("!%d", ii)	-- day
				airac_navdata_active = airac_navdata_active .. "/" .. os.date("!%y", ii)	-- year
			end
			
		end
	end
end

function detect_apt_dat()

	local navdata_ok = 1
	local apt_line = ""
	
	raw_table('apt_word')
	local apt_word = {}
	local ii = 0
	local line_lenght = 0
	local stamp = 0
	local temp_scenery = ""
	local file_scenery = 0
	
	local file_navdata_log = io.open("Log.txt", "r")
	if file_navdata_log ~= nil then
		log_line = file_navdata_log:read()
		file_navdata_log:close()
	end
	
	scenery_num = 0
	custom_scenery = {}
	
	log_line2 = ""
	log_line3 = ""
	
	file_navdata_log = io.open("Custom Scenery/scenery_packs.ini", "r")
	if file_navdata_log ~= nil then
		-- create timestamp
		apt_line = file_navdata_log:read()
		while apt_line do
			line_lenght = string.len(apt_line)
			if line_lenght > 15 then
				if string.sub(apt_line, 1, 12) == "SCENERY_PACK" then
					local global_apt = false
					if line_lenght > 29 and simDR_int_ver >= 120000 then
						if string.sub(apt_line, 1, 30) == "SCENERY_PACK *GLOBAL_AIRPORTS*" then
							global_apt = true
						end
					end
					if global_apt == true then
						temp_scenery = "Global Scenery/Global Airports/Earth nav data/apt.dat"
					elseif string.byte(apt_line, -1) == 13 then
						temp_scenery = string.sub(apt_line, 14, -2) .. "Earth nav data/apt.dat"
					else
						temp_scenery = string.sub(apt_line, 14, -1) .. "Earth nav data/apt.dat"
					end
					file_scenery = io.open(temp_scenery, "r")
					if file_scenery ~= nil then
						scenery_num = scenery_num + 1
						custom_scenery[scenery_num] = temp_scenery
						for ii = 14, line_lenght do
							stamp = stamp + string.byte(apt_line, ii)
						end
						file_scenery:close()
					end
				end
			end
			apt_line = file_navdata_log:read()
		end
		log_line2 = string.format("%d", scenery_num)
		if stamp < 0 then
			stamp = -stamp
		end
		log_line3 = string.format("%d", stamp)
		file_navdata_log:close()
	end
	
	local file_navdata2 = io.open(file_path2 .. FILE_NAME_APT_DAT, "r")
	if file_navdata2 == nil then
		navdata_ok = 0
	else
		apt_line = file_navdata2:read()
		if apt_line ~= log_line then
			navdata_ok = 0
		else
			apt_line = file_navdata2:read()
			if apt_line ~= log_line2 then
				navdata_ok = 0
			else
				apt_line = file_navdata2:read()
				if apt_line ~= log_line3 then
					navdata_ok = 0
				end
			end
		end
		file_navdata2:close()
	end
	
	local file_navdata3 = io.open(file_path2 .. FILE_NAME_RNW_DAT, "r")
	if file_navdata3 == nil then
		navdata_ok = 0
	else
		apt_line = file_navdata3:read()
		if apt_line ~= log_line then
			navdata_ok = 0
		else
			apt_line = file_navdata3:read()
			if apt_line ~= log_line2 then
				navdata_ok = 0
			else
				apt_line = file_navdata3:read()
				if apt_line ~= log_line3 then
					navdata_ok = 0
				end
			end
		end
		file_navdata3:close()
	end
	
	local file_navdata4 = io.open(file_path2 .. FILE_NAME_GATE_DAT, "r")
	if file_navdata4 == nil then
		navdata_ok = 0
	else
		apt_line = file_navdata4:read()
		if apt_line ~= log_line then
			navdata_ok = 0
		else
			apt_line = file_navdata4:read()
			if apt_line ~= log_line2 then
				navdata_ok = 0
			else
				apt_line = file_navdata4:read()
				if apt_line ~= log_line3 then
					navdata_ok = 0
				end
			end
		end
		file_navdata4:close()
	end
	
	if navdata_ok == 0 then
		create_apt_rnw_dat2()
		update_apt_dat = 1
	end
	
end


function create_apt_rnw_dat2()

	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local token = ""
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	
	local apt_icao = ""
	local apt_lat = "0"
	local apt_lon = "0"
	local apt_tns_alt = "0"
	local apt_tns_lvl = "0"
	local apt_rnw0 = ""
	local apt_rnw_lat0 = ""
	local apt_rnw_lon0 = ""
	local apt_rnw1 = ""
	local apt_rnw_lat1 = ""
	local apt_rnw_lon1 = ""
	local apt_rnw_lenght = "0"
	local num_rnw = 0
	raw_table('apt_rnw')
	local apt_rnw = {}
	raw_table('apt_rnw_lat_start')
	local apt_rnw_lat_start = {}
	raw_table('apt_rnw_lon_start')
	local apt_rnw_lon_start = {}
	raw_table('apt_rnw_lat_end')
	local apt_rnw_lat_end = {}
	raw_table('apt_rnw_lon_end')
	local apt_rnw_lon_end = {}
	raw_table('apt_len')
	local apt_len = {}
	raw_table('apt_rnw_crs')
	local apt_rnw_crs = {}
	raw_table('apt_idx')
	local apt_idx = {}
	
	local num_gate = 0
	raw_table('apt_gate')
	local apt_gate = {}
	raw_table('apt_gate_lat')
	local apt_gate_lat = {}
	raw_table('apt_gate_lon')
	local apt_gate_lon = {}
	
	local sort_rnw = ""
	local sort_rnw_lat_start = ""
	local sort_rnw_lon_start = ""
	local sort_rnw_lat_end = ""
	local sort_rnw_lon_end = ""
	local sort_len = ""
	local sort_rnw_crs = ""
	local sort_idx = 0
	local temp_idx = 0
	
	local lat_temp = "0"
	local lon_temp = "0"
	local longest_rnw = 0
	
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_x = 0
	local nd_y = 0
	local nd_dis = 0
	local nd_hdg = 0

	local fms_line = ""
	local database = 0
	local checked_header = 0
	local custom_data_ok = 0
	local skip_apt_icao = 0
	
	local scenery_idx = 1
	local scenery_ok = 0
	
	local apt_icao_temp_num = 0
	raw_table('apt_icao_temp')
	local apt_icao_temp = {}
	raw_table('apt_lat_temp')
	local apt_lat_temp = {}
	raw_table('apt_lon_temp')
	local apt_lon_temp = {}
	raw_table('apt_tns_alt_temp')
	local apt_tns_alt_temp = {}
	raw_table('apt_tns_lvl_temp')
	local apt_tns_lvl_temp = {}
	raw_table('longest_rnw_temp')
	local longest_rnw_temp = {}
	local apt_icao_idx = 0
	
	local aaa_number = 0
	
	local apt_file_name = ""
	local file_aptdata = 0
	
	while scenery_idx <= scenery_num do
		apt_file_name = custom_scenery[scenery_idx]
		file_aptdata = io.open(apt_file_name, "r")
		scenery_idx = scenery_idx + 1
		if file_aptdata ~= nil then
			custom_data_ok = 1
			break
		end
	end
	
	if custom_data_ok == 0 then
		database = 1
	end
	
	if file_aptdata ~= nil or database ~= 0 then
		
		local file_navdata2 = io.open(file_path2 .. FILE_NAME_APT_DAT, "w")
		if file_navdata2 == nil then
			file_aptdata:close()
			return
		end
		
		local file_navdata3 = io.open(file_path2 .. FILE_NAME_RNW_DAT, "w")
		if file_navdata3 == nil then
			file_navdata2:close()
			file_aptdata:close()
			return
		end
		
		local file_navdata4 = io.open(file_path2 .. FILE_NAME_GATE_DAT, "w")
		if file_navdata3 == nil then
			file_navdata2:close()
			file_navdata2:close()
			file_aptdata:close()
			return
		end
		
		scenery_idx = 1
		
		while database < 3 do
			if database == 0 then
				scenery_ok = 0
				while scenery_idx <= scenery_num do
					apt_file_name = custom_scenery[scenery_idx]
					file_aptdata = io.open(apt_file_name, "r")
					scenery_idx = scenery_idx + 1
					if file_aptdata ~= nil then
						if simDR_int_ver < 120000 then
							if apt_file_name ~= "Custom Scenery/Global Airports/Earth nav data/apt.dat" then
								scenery_ok = 1
								break
							end
						else
							if apt_file_name ~= "Global Scenery/Global Airports/Earth nav data/apt.dat" then
								scenery_ok = 1
								break
							end
						end
					end
				end
				if scenery_ok == 0 then
					database = 1
				end
			end
			if database == 1 then
				if simDR_int_ver < 120000 then
					apt_file_name = "Custom Scenery/Global Airports/Earth nav data/apt.dat"
				else
					apt_file_name = "Global Scenery/Global Airports/Earth nav data/apt.dat"
				end
				file_aptdata = io.open(apt_file_name, "r")
				if file_aptdata == nil then
					database = 2
				else
					if custom_data_ok == 0 then
						custom_data_ok = 1
					end
				end
			end
			if database == 2 then
				if simDR_int_ver < 120000 then
					apt_file_name = "Resources/default scenery/default apt dat/Earth nav data/apt.dat"
					file_aptdata = io.open(apt_file_name, "r")
				else
					database = 3
				end
			end
			
			if log_line ~= nil and checked_header == 0 then
				fms_line = log_line .. "\n"
				file_navdata2:write(fms_line)
				file_navdata3:write(fms_line)
				file_navdata4:write(fms_line)
				
				fms_line = log_line2 .. "\n"
				file_navdata2:write(fms_line)
				file_navdata3:write(fms_line)
				file_navdata4:write(fms_line)
				
				fms_line = log_line3 .. "\n"
				file_navdata2:write(fms_line)
				file_navdata3:write(fms_line)
				file_navdata4:write(fms_line)
				
				fms_line = ""
			end
			checked_header = 1
			
			if file_aptdata ~= nil and database < 3 then
			
				apt_line = file_aptdata:read()
				while apt_line do
					ii = 0
					jj = 0
					line_trim = ""
					line_char = ""
					line_lenght = string.len(apt_line)
					if line_lenght > 0 then
						for kk = 1, line_lenght do
							line_char = string.sub(apt_line, kk, kk)
							if line_char == " " then
								if ii == 1 then
									jj = jj + 1
									apt_word[jj] = line_trim
									ii = 0
									line_trim = ""
								end
							else
								line_trim = line_trim .. line_char
								ii = 1
							end
						end
						if string.len(line_trim) > 0 then
							jj = jj + 1
							apt_word[jj] = line_trim
						end
						
						if jj > 0 then
							if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
								apt_word[jj] = string.sub(apt_word[jj], 1, -2)
							end
						end
						
						-- Read airport datum_lat, datum_lon
						if jj > 5 then		--new airport
							
							if apt_word[1] == "1" or apt_word[1] == "16" or apt_word[1] == "17" then
								if apt_icao ~= "" then
									
									skip_apt_icao = 0
									apt_icao_idx = 0
									if apt_icao_temp_num > 0 then
										for ddd = 1, apt_icao_temp_num do
											if apt_icao == apt_icao_temp[ddd] then
												skip_apt_icao = 1
												apt_icao_idx = ddd
												break
											end
										end
									end
									
									if skip_apt_icao == 0 then
										-- Prepare data for write B738X apt.dat
										apt_icao_temp_num = apt_icao_temp_num + 1
										apt_icao_temp[apt_icao_temp_num] = apt_icao
										apt_lat_temp[apt_icao_temp_num] = apt_lat
										apt_lon_temp[apt_icao_temp_num] = apt_lon
										apt_tns_alt_temp[apt_icao_temp_num] = apt_tns_alt
										apt_tns_lvl_temp[apt_icao_temp_num] = apt_tns_lvl
										longest_rnw_temp[apt_icao_temp_num] = longest_rnw
										
										-- Write B738X_rnw.dat
										if num_rnw > 1 then
											-- Sort runways
											for kk = 1, num_rnw - 1 do
												mm = kk + 1 
												for ll = mm, num_rnw do
													if apt_idx[kk] > apt_idx[ll] then
														sort_rnw = apt_rnw[kk]
														sort_rnw_lat_start = apt_rnw_lat_start[kk]
														sort_rnw_lon_start = apt_rnw_lon_start[kk]
														sort_rnw_lat_end = apt_rnw_lat_end[kk]
														sort_rnw_lon_end = apt_rnw_lon_end[kk]
														sort_len = apt_len[kk]
														sort_idx = apt_idx[kk]
														sort_rnw_crs = apt_rnw_crs[kk]
														apt_rnw[kk] = apt_rnw[ll]
														apt_rnw_lat_start[kk] = apt_rnw_lat_start[ll]
														apt_rnw_lon_start[kk] = apt_rnw_lon_start[ll]
														apt_rnw_lat_end[kk] = apt_rnw_lat_end[ll]
														apt_rnw_lon_end[kk] = apt_rnw_lon_end[ll]
														apt_len[kk] = apt_len[ll]
														apt_idx[kk] = apt_idx[ll]
														apt_rnw_crs[kk] = apt_rnw_crs[ll]
														apt_rnw[ll] = sort_rnw
														apt_rnw_lat_start[ll] = sort_rnw_lat_start
														apt_rnw_lon_start[ll] = sort_rnw_lon_start
														apt_rnw_lat_end[ll] = sort_rnw_lat_end
														apt_rnw_lon_end[ll] = sort_rnw_lon_end
														apt_len[ll] = sort_len
														apt_rnw_crs[ll] = sort_rnw_crs
														apt_idx[ll] = sort_idx
													end
												end
											end
											for kk = 1, num_rnw do
												fms_line = apt_icao .. " " .. apt_rnw[kk] .. " " .. apt_rnw_lat_start[kk] .. " " .. apt_rnw_lon_start[kk] .. " "
												fms_line = fms_line .. apt_rnw_lat_end[kk] .. " " .. apt_rnw_lon_end[kk] .. " " .. apt_len[kk] .. " "
												fms_line = fms_line .. apt_rnw_crs[kk] .. "\n"
												file_navdata3:write(fms_line)
											end
										end
										
										-- Write B738X_gate.dat
										if num_gate > 0 then
											for kk = 1, num_gate do
												fms_line = apt_icao .. " " .. apt_gate_lat[kk] .. " " .. apt_gate_lon[kk] .. " " .. apt_gate[kk] .. "\n"
												file_navdata4:write(fms_line)
											end
										end
										
									else
										if apt_tns_alt_temp[apt_icao_idx] == "0" and apt_tns_lvl_temp[apt_icao_idx] == "0" then
											apt_tns_alt_temp[apt_icao_idx] = apt_tns_alt
											apt_tns_lvl_temp[apt_icao_idx] = apt_tns_lvl
										end
									end
								end
								
								apt_icao = ""
								apt_lat = "0"
								apt_lon = "0"
								apt_tns_alt = "0"
								apt_tns_lvl = "0"
								apt_rnw0 = ""
								apt_rnw1 = ""
								apt_rnw_lat0 = ""
								apt_rnw_lon0 = ""
								apt_rnw_lat1 = ""
								apt_rnw_lon1 = ""
								apt_rnw_lenght = "0"
								num_rnw = 0
								apt_rnw = {}
								apt_rnw_lat_start = {}
								apt_rnw_lon_start = {}
								apt_rnw_lat_end = {}
								apt_rnw_lon_end = {}
								apt_len = {}
								apt_rnw_crs = {}
								apt_idx = {}
								longest_rnw = 0
								
								num_gate = 0
								apt_gate_lat = {}
								apt_gate_lon = {}
								apt_gate = {}
							end
						end
						
						if jj == 3 then
							if apt_word[1] == "1302" and apt_word[2] == "datum_lat" then
								if apt_lat == "0" then
									apt_lat = apt_word[3]
								end
							end
							if apt_word[1] == "1302" and apt_word[2] == "datum_lon" then
								if apt_lon == "0" then
									apt_lon = apt_word[3]
								end
							end
							if apt_word[1] == "1302" and apt_word[2] == "transition_alt" then
								aaa_number = tonumber(apt_word[3])
								if aaa_number ~= nil then
									apt_tns_alt = apt_word[3]
								end
							end
							if apt_word[1] == "1302" and apt_word[2] == "transition_level" then
								aaa_number = tonumber(apt_word[3])
								if aaa_number == nil then
									if string.len(apt_word[3]) > 2 then
										if string.sub(apt_word[3], 1, 2) == "FL" then
											aaa_number = tonumber(string.sub(apt_word[3], 3, -1))
											if aaa_number ~= nil then
												apt_tns_lvl = tostring(aaa_number) .. "00"
											end
										end
									end
								elseif aaa_number < 1000 then
									apt_tns_lvl = apt_word[3] .. "00"
								else
									apt_tns_lvl = apt_word[3]
								end
							end
						end
						
						-- Read runway data
						if jj > 19 then
							if apt_word[1] == "100" then
								apt_rnw0 = apt_word[9]
								apt_rnw_lat0 = apt_word[10]
								apt_rnw_lon0 = apt_word[11]
								apt_rnw1 = apt_word[18]
								apt_rnw_lat1 = apt_word[19]
								apt_rnw_lon1 = apt_word[20]
								-- calc runway lenght
								
								nd_lat = tonumber(apt_rnw_lat0)
								nd_lon = tonumber(apt_rnw_lon0)
								nd_lat2 = tonumber(apt_rnw_lat1)
								nd_lon2 = tonumber(apt_rnw_lon1)
								if nd_lat == nil or nd_lon == nil or nd_lat2 == nil or nd_lon2 == nil then
									-- bad data
								else
								
									nd_dis = nd_calc_dist(nd_lat, nd_lon, nd_lat2, nd_lon2)
									nd_dis = nd_dis * 1852	-- in m
									nd_dis = math.floor(nd_dis + 0.5)
									nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
									
									apt_rnw_lenght = tostring(nd_dis)
									num_rnw = num_rnw + 1
									if string.len(apt_rnw0) < 2 then
										apt_rnw0 = "0" .. apt_rnw0
									elseif string.len(apt_rnw0) == 2 then
										if tonumber(apt_rnw0) == nil then
											apt_rnw0 = "0" .. apt_rnw0
										end
									end
									apt_rnw[num_rnw] = apt_rnw0
									apt_rnw_lat_start[num_rnw] = apt_rnw_lat0
									apt_rnw_lon_start[num_rnw] = apt_rnw_lon0
									apt_rnw_lat_end[num_rnw] = apt_rnw_lat1
									apt_rnw_lon_end[num_rnw] = apt_rnw_lon1
									apt_len[num_rnw] = apt_rnw_lenght
									apt_rnw_crs[num_rnw] = tostring(nd_hdg)
									
									temp_idx = tonumber(apt_rnw0)
									if temp_idx == nil then
										temp_idx = tonumber(string.sub(apt_rnw0, 1, -2))
										if temp_idx == nil then
											apt_idx[num_rnw] = 0
										else
											apt_idx[num_rnw] = temp_idx
										end
										if string.sub(apt_rnw0, -1, -1) == "L" then
											apt_idx[num_rnw] = apt_idx[num_rnw] + 0.3
										elseif string.sub(apt_rnw0, -1, -1) == "R" then
											apt_idx[num_rnw] = apt_idx[num_rnw] + 0.7
										end
									else
										apt_idx[num_rnw] = temp_idx
									end
									
									num_rnw = num_rnw + 1
									nd_hdg = (nd_hdg + 180) % 360
									if string.len(apt_rnw1) < 2 then
										apt_rnw1 = "0" .. apt_rnw1
									elseif string.len(apt_rnw1) == 2 then
										if tonumber(apt_rnw1) == nil then
											apt_rnw1 = "0" .. apt_rnw1
										end
									end
									apt_rnw[num_rnw] = apt_rnw1
									apt_rnw_lat_start[num_rnw] = apt_rnw_lat1
									apt_rnw_lon_start[num_rnw] = apt_rnw_lon1
									apt_rnw_lat_end[num_rnw] = apt_rnw_lat0
									apt_rnw_lon_end[num_rnw] = apt_rnw_lon0
									apt_len[num_rnw] = apt_rnw_lenght
									apt_rnw_crs[num_rnw] = tostring(nd_hdg)
									temp_idx = tonumber(apt_rnw1)
									if temp_idx == nil then
										temp_idx = tonumber(string.sub(apt_rnw1, 1, -2))
										if temp_idx == nil then
											apt_idx[num_rnw] = 0
										else
											apt_idx[num_rnw] = temp_idx
										end
										if string.sub(apt_rnw1, -1, -1) == "L" then
											apt_idx[num_rnw] = apt_idx[num_rnw] + 0.3
										elseif string.sub(apt_rnw1, -1, -1) == "R" then
											apt_idx[num_rnw] = apt_idx[num_rnw] + 0.7
										end
									else
										apt_idx[num_rnw] = temp_idx
									end
									if longest_rnw < nd_dis then
										longest_rnw = nd_dis
									end
									
									-- add protect against missing datas
									if apt_lat == "0" and apt_lon == "0" then
										apt_lat = apt_rnw_lat1
										apt_lon = apt_rnw_lon1
									end
								end
							end
						end
						
						-- startup position
						if jj >= 5 then
							if apt_word[1] == "15" or apt_word[1] == "1300" then
								num_gate = num_gate + 1
								apt_gate_lat[num_gate] = apt_word[2]
								apt_gate_lon[num_gate] = apt_word[3]
								apt_gate[num_gate] = apt_word[5]
								local word_gate = 6
								while word_gate <= jj do
									apt_gate[num_gate] = apt_gate[num_gate] .. " " .. apt_word[word_gate]
									word_gate = word_gate + 1
								end
							end
						end
						
						if jj > 5 then
							if apt_word[1] == "1" and string.len(apt_word[5]) == 4 then
								apt_icao = apt_word[5]
								apt_lat = "0"	--lat_temp
								apt_lon = "0"	--lon_temp
								lat_temp = "0"
								lon_temp = "0"
							end
						end
						
						-- END OF FILE
						if jj > 0 then
							if apt_word[1] == "99" then
									if apt_icao ~= "" then
										skip_apt_icao = 0
										apt_icao_idx = 0
										if apt_icao_temp_num > 0 then
											for ddd = 1, apt_icao_temp_num do
												if apt_icao == apt_icao_temp[ddd] then
													skip_apt_icao = 1
													apt_icao_idx = ddd
													break
												end
											end
										end
										
										if skip_apt_icao == 0 then
											-- Prepare data for write B738X apt.dat
											apt_icao_temp_num = apt_icao_temp_num + 1
											apt_icao_temp[apt_icao_temp_num] = apt_icao
											apt_lat_temp[apt_icao_temp_num] = apt_lat
											apt_lon_temp[apt_icao_temp_num] = apt_lon
											apt_tns_alt_temp[apt_icao_temp_num] = apt_tns_alt
											apt_tns_lvl_temp[apt_icao_temp_num] = apt_tns_lvl
											longest_rnw_temp[apt_icao_temp_num] = longest_rnw
											
											-- Write B738X_rnw.dat
											if num_rnw > 1 then
												-- Sort runways
												for kk = 1, num_rnw - 1 do
													mm = kk + 1 
													for ll = mm, num_rnw do
														if apt_idx[kk] > apt_idx[ll] then
															sort_rnw = apt_rnw[kk]
															sort_rnw_lat_start = apt_rnw_lat_start[kk]
															sort_rnw_lon_start = apt_rnw_lon_start[kk]
															sort_rnw_lat_end = apt_rnw_lat_end[kk]
															sort_rnw_lon_end = apt_rnw_lon_end[kk]
															sort_len = apt_len[kk]
															sort_idx = apt_idx[kk]
															sort_rnw_crs = apt_rnw_crs[kk]
															apt_rnw[kk] = apt_rnw[ll]
															apt_rnw_lat_start[kk] = apt_rnw_lat_start[ll]
															apt_rnw_lon_start[kk] = apt_rnw_lon_start[ll]
															apt_rnw_lat_end[kk] = apt_rnw_lat_end[ll]
															apt_rnw_lon_end[kk] = apt_rnw_lon_end[ll]
															apt_len[kk] = apt_len[ll]
															apt_idx[kk] = apt_idx[ll]
															apt_rnw_crs[kk] = apt_rnw_crs[ll]
															apt_rnw[ll] = sort_rnw
															apt_rnw_lat_start[ll] = sort_rnw_lat_start
															apt_rnw_lon_start[ll] = sort_rnw_lon_start
															apt_rnw_lat_end[ll] = sort_rnw_lat_end
															apt_rnw_lon_end[ll] = sort_rnw_lon_end
															apt_len[ll] = sort_len
															apt_rnw_crs[ll] = sort_rnw_crs
															apt_idx[ll] = sort_idx
														end
													end
												end
												for kk = 1, num_rnw do
													fms_line = apt_icao .. " " .. apt_rnw[kk] .. " " .. apt_rnw_lat_start[kk] .. " " .. apt_rnw_lon_start[kk] .. " "
													fms_line = fms_line .. apt_rnw_lat_end[kk] .. " " .. apt_rnw_lon_end[kk] .. " " .. apt_len[kk] .. " "
													fms_line = fms_line .. apt_rnw_crs[kk] .. "\n"
													file_navdata3:write(fms_line)
												end
											end
											
											-- Write B738X_gate.dat
											if num_gate > 0 then
												for kk = 1, num_gate do
													fms_line = apt_icao .. " " .. apt_gate_lat[kk] .. " " .. apt_gate_lon[kk] .. " " .. apt_gate[kk] .. "\n"
													file_navdata4:write(fms_line)
												end
											end
											
										else
											if apt_tns_alt_temp[apt_icao_idx] == "0" and apt_tns_alt ~= "0" then
												apt_tns_alt_temp[apt_icao_idx] = apt_tns_alt
											end
											if apt_tns_lvl_temp[apt_icao_idx] == "0" and apt_tns_lvl ~= "0" then
												apt_tns_lvl_temp[apt_icao_idx] = apt_tns_lvl
											end
										end
									end
									------------
									apt_icao = ""
									apt_lat = "0"
									apt_lon = "0"
									apt_tns_alt = "0"
									apt_tns_lvl = "0"
									apt_rnw0 = ""
									apt_rnw1 = ""
									apt_rnw_lat0 = ""
									apt_rnw_lon0 = ""
									apt_rnw_lat1 = ""
									apt_rnw_lon1 = ""
									apt_rnw_lenght = "0"
									num_rnw = 0
									apt_rnw = {}
									apt_rnw_lat_start = {}
									apt_rnw_lon_start = {}
									apt_rnw_lat_end = {}
									apt_rnw_lon_end = {}
									apt_len = {}
									apt_rnw_crs = {}
									apt_idx = {}
									longest_rnw = 0
									
									num_gate = 0
									apt_gate_lat = {}
									apt_gate_lon = {}
									apt_gate = {}
							end
						end
					end
					apt_line = file_aptdata:read()
				end
				file_aptdata:close()
			end
			
			if database > 0 then
				database = database + 1
			end
		end
		
		if apt_icao_temp_num > 0 then
			-- Write B738X_apt.dat
			for ddd = 1, apt_icao_temp_num do
				fms_line = apt_icao_temp[ddd] .. " " .. apt_lat_temp[ddd] .. " " .. apt_lon_temp[ddd] .. " " .. apt_tns_alt_temp[ddd] .. " " .. apt_tns_lvl_temp[ddd]
				fms_line = fms_line .. " " .. tostring(longest_rnw_temp[ddd]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		
		file_navdata2:close()
		file_navdata3:close()
		file_navdata4:close()
	end

end

function read_apt_dat()

	local ii = 0
	local jj = 0
	local kk = 0
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	local mm = 0
	
	local apt_first = 0
	
	apt_data_num = 0
	apt_data = {}
	
	local file_navdata2 = io.open(file_path2 .. FILE_NAME_APT_DAT, "r")
	if file_navdata2 ~= nil then
		apt_line = file_navdata2:read()
		if string.sub(apt_line, 1, 7) == "log.txt" then
			apt_line = file_navdata2:read()
		end
		while apt_line do
			if apt_first ~= 0 then
				ii = 0
				jj = 0
				line_trim = ""
				line_char = ""
				line_lenght = string.len(apt_line)
				if line_lenght > 0 then
					for kk = 1, line_lenght do
						line_char = string.sub(apt_line, kk, kk)
						if line_char == " " then
							if ii == 1 then
								jj = jj + 1
								apt_word[jj] = line_trim
								ii = 0
								line_trim = ""
							end
						else
							line_trim = line_trim .. line_char
							ii = 1
						end
					end
					if string.len(line_trim) > 0 then
						jj = jj + 1
						apt_word[jj] = line_trim
					end
					
					if jj > 0 then
						if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
							apt_word[jj] = string.sub(apt_word[jj], 1, -2)
						end
					end
					
					
					if jj == 6 then
						apt_data_num = apt_data_num + 1
						apt_data[apt_data_num] = {true, true, true, true, true, true}
						apt_data[apt_data_num][1] = apt_word[1]		-- ICAO
						apt_data[apt_data_num][2] = tonumber(apt_word[2])		-- lat
						apt_data[apt_data_num][3] = tonumber(apt_word[3])		-- lon
						mm = tonumber(apt_word[4])		-- trans alt
						if mm == nil then
							apt_data[apt_data_num][4] = 0		-- trans alt
						else
							apt_data[apt_data_num][4] = mm		-- trans alt
						end
						mm = tonumber(apt_word[5])		-- trans lvl
						if mm == nil then
							mm = tonumber(string.sub(apt_word[5], 3, -1))
							if mm == nil then
								apt_data[apt_data_num][5] = 0		-- trans lvl
							else
								apt_data[apt_data_num][5] = mm * 100		-- trans lvl
							end
						else
							if mm < 1000 then
								apt_data[apt_data_num][5] = mm * 100		-- trans lvl
							else
								apt_data[apt_data_num][5] = mm		-- trans lvl
							end
						end
						apt_data[apt_data_num][6] = tonumber(apt_word[6])		-- longest rnw
					end
				end
			end
			apt_first = 1
			apt_line = file_navdata2:read()
		end
		file_navdata2:close()
	end

end

function read_rnw_dat()

	local ii = 0
	local jj = 0
	local kk = 0
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	
	local apt_first = 0
	
	local file_navdata2 = io.open(file_path2 .. FILE_NAME_RNW_DAT, "r")
	
	if file_navdata2 ~= nil then
		apt_line = file_navdata2:read()
		if string.sub(apt_line, 1, 7) == "log.txt" then
			apt_line = file_navdata2:read()
		end
		while apt_line do
			
			if apt_first ~= 0 then
				ii = 0
				jj = 0
				line_trim = ""
				line_char = ""
				line_lenght = string.len(apt_line)
				if line_lenght > 0 then
					for kk = 1, line_lenght do
						line_char = string.sub(apt_line, kk, kk)
						if line_char == " " then
							if ii == 1 then
								jj = jj + 1
								apt_word[jj] = line_trim
								ii = 0
								line_trim = ""
							end
						else
							line_trim = line_trim .. line_char
							ii = 1
						end
					end
					if string.len(line_trim) > 0 then
						jj = jj + 1
						apt_word[jj] = line_trim
					end
					
					if jj > 0 then
						if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
							apt_word[jj] = string.sub(apt_word[jj], 1, -2)
						end
					end
					
					if jj == 8 then
						rnw_data_num = rnw_data_num + 1
						rnw_data[rnw_data_num] = {true, true, true, true, true, true, true, true, true, true}
						rnw_data[rnw_data_num][1] = apt_word[1]		-- ICAO
						rnw_data[rnw_data_num][2] = apt_word[2]		-- runway
						rnw_data[rnw_data_num][3] = tonumber(apt_word[3])		-- lat start
						rnw_data[rnw_data_num][4] = tonumber(apt_word[4])		-- lon start
						rnw_data[rnw_data_num][5] = tonumber(apt_word[5])		-- lat end
						rnw_data[rnw_data_num][6] = tonumber(apt_word[6])		-- lon end
						rnw_data[rnw_data_num][7] = tonumber(apt_word[7])		-- lenght
						rnw_data[rnw_data_num][8] = tonumber(apt_word[8])		-- course
						rnw_data[rnw_data_num][9] = 0	-- reserved
						rnw_data[rnw_data_num][10] = apt_word[2]	-- old runway
					end
				end
			end
			apt_first = 1
			apt_line = file_navdata2:read()
		end
		file_navdata2:close()
		
	end
	--dump_rnw_data()
	
end

function read_awy_dat2()

	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	
	local apt_first = 0
	local awy_temp = ""
	local awy_tempx = ""
	local awy_skip = 0
	local awy_skip_txt = ""
	local awy_skip_txt2 = ""
	
	raw_table('awy_swap')
	local awy_swap = {}
	
	awy_data_num = 0
	awy_data = {}
	
	local file_name_2 = file_name_path_awy .. "earth_awy.dat"
	local file_navdata2 = io.open(file_name_2, "r")
	
	
	-- read data
	apt_line = file_navdata2:read()
	while apt_line do
		--space_first = 0
		if apt_first ~= 0 then
			ii = 0
			jj = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(apt_line)
			if line_lenght > 0 then
				for kk = 1, line_lenght do
					line_char = string.sub(apt_line, kk, kk)
					if line_char == " " then
						if ii == 1 then
							jj = jj + 1
							apt_word[jj] = line_trim
							ii = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						ii = 1
					end
				end
				if string.len(line_trim) > 0 then
					jj = jj + 1
					apt_word[jj] = line_trim
				end
				
				if jj == 11 then
					if string.len(apt_word[11]) > 1 and string.byte(apt_word[11], -1) == 13 then	-- CR
						apt_word[11] = string.sub(apt_word[11], 1, -2)
					end
					awy_skip_txt = apt_word[11] .. apt_word[1] .. apt_word[4]
					awy_temp = apt_word[11]
					jj, kk = string.find(awy_temp, "-")
					if jj == nil then
						awy_data_num = awy_data_num + 1
						awy_data[awy_data_num] = {true, true, true, true, true, true}
						awy_data[awy_data_num][1] = apt_word[11]	-- airway
						awy_data[awy_data_num][2] = apt_word[1]		-- navaid from
						awy_data[awy_data_num][3] = apt_word[4]		-- navaid to
						awy_data[awy_data_num][4] = apt_word[2]		-- region code from
						awy_data[awy_data_num][5] = apt_word[5]		-- region code to
						awy_data[awy_data_num][6] = apt_word[7]		-- direction restrict: Forward, Back, None
					else
						while jj ~= nil do 
							awy_tempx = string.sub(awy_temp, 1, jj - 1)
							awy_data_num = awy_data_num + 1
							awy_data[awy_data_num] = {true, true, true, true, true, true}
							awy_data[awy_data_num][1] = awy_tempx	-- airway
							awy_data[awy_data_num][2] = apt_word[1]		-- navaid
							awy_data[awy_data_num][3] = apt_word[4]		-- navaid
							awy_data[awy_data_num][4] = apt_word[2]		-- region code from
							awy_data[awy_data_num][5] = apt_word[5]		-- region code to
							awy_data[awy_data_num][6] = apt_word[7]		-- direction restrict: Forward, Back, None
							awy_temp = string.sub(awy_temp, jj + 1, -1)
							jj, kk = string.find(awy_temp, "-")
						end
						awy_data_num = awy_data_num + 1
						awy_data[awy_data_num] = {true, true, true, true, true, true}
						awy_data[awy_data_num][1] = awy_temp	-- airway
						awy_data[awy_data_num][2] = apt_word[1]		-- navaid
						awy_data[awy_data_num][3] = apt_word[4]		-- navaid
						awy_data[awy_data_num][4] = apt_word[2]		-- region code from
						awy_data[awy_data_num][5] = apt_word[5]		-- region code to
						awy_data[awy_data_num][6] = apt_word[7]		-- direction restrict: Forward, Back, None
					end
				end
			end
		end
		apt_first = 1
		apt_line = file_navdata2:read()
	end
	file_navdata2:close()
	
end

function find_awy(awy_from, awy_from_rc, awy)
	
	local ii = 0
	local jj = 0
	local from_find = 0
	local kk = 0
	local mm = 0
	local compare1 = ""
	local compare2 = ""
	local compare1_1 = ""
	local compare2_1 = ""
	local nd_x = 0
	local idx_rec = 0
	
	raw_table('awy_temp')
	local awy_temp = {}
	local awy_temp_num = 0
	
	local compare_result = 0
	
	awy_temp2 = {}
	awy_temp_num2 = 0
	
	if string.len(awy_from_rc) > 0 then
		kk = 1
	end
	
	if awy_data_num > 0 and awy ~= "" and string.len(awy) > 0 then
		nd_x = string.byte(string.sub(awy, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_awy[nd_x][99999] > 0 then
				for ii = 1, idx_awy[nd_x][99999] do
					idx_rec = idx_awy[nd_x][ii]
					if awy_data[idx_rec][1] == awy then
						awy_temp_num = awy_temp_num + 1
						awy_temp[awy_temp_num] = {true, true, true, true, true, true, true, true}
						awy_temp[awy_temp_num][1] = awy_data[idx_rec][1]
						awy_temp[awy_temp_num][2] = awy_data[idx_rec][2]
						awy_temp[awy_temp_num][3] = awy_data[idx_rec][3]
						awy_temp[awy_temp_num][4] = awy_data[idx_rec][4]
						awy_temp[awy_temp_num][5] = awy_data[idx_rec][5]
						awy_temp[awy_temp_num][6] = awy_data[idx_rec][6]
						awy_temp[awy_temp_num][7] = 0
						awy_temp[awy_temp_num][8] = 0
						if kk == 0 then
							if awy_data[idx_rec][6] == "N" or awy_data[idx_rec][6] == "F" then
								if awy_data[idx_rec][2] == awy_from then --or awy_data[ii][3] == awy_from then
									from_find = 1
								end
							end
							if awy_data[idx_rec][6] == "N" or awy_data[idx_rec][6] == "B" then
								if awy_data[idx_rec][3] == awy_from then
									from_find = 1
								end
							end
						else
							if awy_data[idx_rec][6] == "N" or awy_data[idx_rec][6] == "F" then
								if awy_data[idx_rec][2] == awy_from and awy_data[idx_rec][4] == awy_from_rc then
									from_find = 1
								end
							end
							if awy_data[idx_rec][6] == "N" or awy_data[idx_rec][6] == "B" then
								if awy_data[idx_rec][3] == awy_from and awy_data[idx_rec][5] == awy_from_rc then
									from_find = 1
								end
							end
						end
					end
				end
			end
		end
	end
	
	--delete the same airways
	if awy_temp_num == 1 then
		awy_temp_num2 = awy_temp_num2 + 1
		awy_temp2[awy_temp_num2] = {true, true, true, true, true, true, true, true}
		for mm = 1, 8 do
			awy_temp2[awy_temp_num2][mm] = awy_temp[1][mm]
		end
	elseif awy_temp_num > 1 then
		awy_temp_num2 = awy_temp_num2 + 1
		awy_temp2[awy_temp_num2] = {true, true, true, true, true, true, true, true}
		for mm = 1, 8 do
			awy_temp2[awy_temp_num2][mm] = awy_temp[1][mm]
		end
		for ii = 2, awy_temp_num do
			if kk == 0 then
				compare2 = awy_temp[ii][1] .. awy_temp[ii][2]
				compare2_1 = awy_temp[ii][1] .. awy_temp[ii][3]
			else
				compare2 = awy_temp[ii][1] .. awy_temp[ii][2] .. awy_temp[ii][4]
				compare2_1 = awy_temp[ii][1] .. awy_temp[ii][3] .. awy_temp[ii][5]
			end
			
			compare_result = 0
			for jj = 1 , awy_temp_num2 do
				if kk == 0 then
					compare1 = awy_temp2[jj][1] .. awy_temp2[jj][2]
					compare1_1 = awy_temp2[jj][1] .. awy_temp2[jj][3]
				else
					compare1 = awy_temp2[jj][1] .. awy_temp2[jj][2] .. awy_temp2[jj][4]
					compare1_1 = awy_temp2[jj][1] .. awy_temp2[jj][3] .. awy_temp2[jj][5]
				end
				if (compare1 == compare2 and compare1_1 == compare2_1) or (compare1 == compare2_1 and compare1_1 == compare2) then
					compare_result = jj
					break
				end
			end
			if compare_result == 0 then
				awy_temp_num2 = awy_temp_num2 + 1
				awy_temp2[awy_temp_num2] = {true, true, true, true, true, true, true, true}
				for mm = 1, 8 do
					awy_temp2[awy_temp_num2][mm] = awy_temp[ii][mm]
				end
			else
				if awy_temp2[compare_result][6] == "F" then
					if awy_temp[ii][6] == "N" or awy_temp[ii][6] == "B" then
						awy_temp2[compare_result][6] = "N"
					end
				elseif awy_temp2[compare_result][6] == "B" then
					if awy_temp[ii][6] == "N" or awy_temp[ii][6] == "F" then
						awy_temp2[compare_result][6] = "N"
					end
				end
			end
		end
	end
	
	if from_find == 0 then
		awy_temp_num2 = 0
	end
	
	--dump_awy()
	
	return from_find

end



function find_awy_path(awy_from, awy_from_rc, awy_to, awy_to_rc, awy)

	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local awy_idx = 0
	local awy_idx2 = 0
	local from_tmp = awy_from
	local from_tmp_rc = awy_from_rc
	local to_find = 0
	local awy_repeat_n = 0
	local awy_repeat = 0
	local aw_used = 0
	local awy_enable = 0
	
	awy_path = {}
	awy_path_num = 0
	
	if string.len(awy_from_rc) > 0 then
		kk = 1
	end
	
	if string.len(awy_to_rc) > 0 then
		ll = 1
	end
	
	if awy_temp_num2 > 0 then
		
		awy_repeat_n = 2
		for awy_repeat = 1, awy_repeat_n do
			
			awy_path = {}
			awy_path_num = 0
			
			for jj = 1, awy_temp_num2 do 
				for ii = 1, awy_temp_num2 do
					
					awy_enable = 0
					if awy_repeat == 1 then
						if awy_temp2[ii][7] == 0 and awy_temp2[ii][8] == 0 then
							awy_enable = 1
						end
					else
						awy_enable = 1
						if awy_temp2[ii][7] == 1 and awy_temp2[ii][8] == 1 then
							awy_enable = 0
						end
					end
					
					if awy_enable == 1 then
						if kk == 0 then
							aw_used = 0
							if awy_temp2[ii][7] == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "F") then
								if awy_temp2[ii][2] == from_tmp then
									awy_path_num = awy_path_num + 1
									awy_path[awy_path_num] = {true, true}
									awy_path[awy_path_num][1] = awy_temp2[ii][3]
									awy_path[awy_path_num][2] = awy_temp2[ii][5]
									awy_temp2[ii][7] = 1	-- used
									from_tmp = awy_temp2[ii][3]
									from_tmp_rc = awy_temp2[ii][5]
									aw_used = 1
								end
							end
							if awy_temp2[ii][8] == 0 and aw_used == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "B") then
								if awy_temp2[ii][3] == from_tmp then
									awy_path_num = awy_path_num + 1
									awy_path[awy_path_num] = {true, true}
									awy_path[awy_path_num][1] = awy_temp2[ii][2]
									awy_path[awy_path_num][2] = awy_temp2[ii][4]
									awy_temp2[ii][8] = 1	-- used
									from_tmp = awy_temp2[ii][2]
									from_tmp_rc = awy_temp2[ii][4]
									aw_used = 1
								end
							end
						else
							aw_used = 0
							if awy_temp2[ii][7] == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "F") then
								if awy_temp2[ii][2] == from_tmp and awy_temp2[ii][4] == from_tmp_rc then
									awy_path_num = awy_path_num + 1
									awy_path[awy_path_num] = {true, true}
									awy_path[awy_path_num][1] = awy_temp2[ii][3]
									awy_path[awy_path_num][2] = awy_temp2[ii][5]
									awy_temp2[ii][7] = 1	-- used
									from_tmp = awy_temp2[ii][3]
									from_tmp_rc = awy_temp2[ii][5]
									aw_used = 1
								end
							end
							if awy_temp2[ii][8] == 0 and aw_used == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "B") then
								if awy_temp2[ii][3] == from_tmp and awy_temp2[ii][5] == from_tmp_rc then
									awy_path_num = awy_path_num + 1
									awy_path[awy_path_num] = {true, true}
									awy_path[awy_path_num][1] = awy_temp2[ii][2]
									awy_path[awy_path_num][2] = awy_temp2[ii][4]
									awy_temp2[ii][8] = 1	-- used
									from_tmp = awy_temp2[ii][2]
									from_tmp_rc = awy_temp2[ii][4]
									aw_used = 1
								end
							end
						end
						
						if ll == 0 then
							if awy_to == from_tmp then
								to_find = 1
								break
							end
						else
							if awy_to == from_tmp and awy_to_rc == from_tmp_rc then
								to_find = 1
								break
							end
						end
					end
				end
				if to_find == 1 then
					break
				end
			end
			if to_find == 1 then
				break
			end
			
			from_tmp = awy_from
			from_tmp_rc = awy_from_rc
		end
	
	end
	
	--dump_awy()
	--dump_awy2()
	
	return to_find

end


function via_via_check()
	
	if via_via_ok == 1 then
		if calc_rte_enable2 == 0 then
			-- if via_via_ok == 1 then
				fpln_num2 = fpln_num2 + 1
				fpln_data2[fpln_num2] = {}
				fpln_data2[fpln_num2][1] = ""
				fpln_data2[fpln_num2][2] = via_via_entry
				fpln_data2[fpln_num2][3] = ""
				fpln_data2[fpln_num2][4] = 1	--legs_data_idx
				fpln_data2[fpln_num2][5] = 0	-- num_legs_data
				fpln_data2[fpln_num2][6] = 1	-- fpln new
				via_via_entry = ""
				via_via_ok = 0
				
				if legs_num > 1 and rte12_act == 0 then
					rte_exec = 1
				end
				
				find_awy(fpln_data2[fpln_num2-1][1], fpln_data2[fpln_num2-1][3], fpln_data2[fpln_num2][2])
			-- end
		end
	elseif via_via_ok == 2 then
		if calc_rte_enable8 == 0 then
			-- if via_via_ok == 1 then
				fpln_num4 = fpln_num4 + 1
				fpln_data4[fpln_num4] = {}
				fpln_data4[fpln_num4][1] = ""
				fpln_data4[fpln_num4][2] = via_via_entry
				fpln_data4[fpln_num4][3] = ""
				fpln_data4[fpln_num4][4] = 1	--legs_data_idx
				fpln_data4[fpln_num4][5] = 0	-- num_legs_data
				fpln_data4[fpln_num4][6] = 1	-- fpln new
				via_via_entry = ""
				via_via_ok = 0
				
				if legs_num > 1 and rte12_act == 1 then
					rte_exec2 = 1
				end
				
				find_awy(fpln_data4[fpln_num4-1][1], fpln_data4[fpln_num4-1][3], fpln_data4[fpln_num4][2])
			-- end
		end
	end
end

function via_via_add(entry_in, mode_in, in_rte)
	
	local idx_tmp = 0
	local idx_tmp2 = 0
	
	via_via_ok = 0
	
	if in_rte == 0 then
		if fpln_num2 > 2 then
			if find_via_via(fpln_data2[fpln_num2-1][1], fpln_data2[fpln_num2-1][3], fpln_data2[fpln_num2][2], entry_in) == 0 then
				if mode_in == 0 then
					entry = ">INVALID ENTRY"
				else
					entry2 = ">INVALID ENTRY"
				end
			else
				fpln_data2[fpln_num2][4] = fpln_data2[fpln_num2-1][4] + fpln_data2[fpln_num2-1][5]	--legs_data_idx
				idx_tmp = fpln_data2[fpln_num2][4] + fpln_data2[fpln_num2][5]
				fpln_data2[fpln_num2][5] = awy_path_num
				
				-- add airways waypoints
				idx_tmp2 = fpln_data2[fpln_num2][4]
				fpln_add_leg_dir2(idx_tmp, idx_tmp2, fpln_data2[fpln_num2][2], in_rte)
				
				via_via_entry = entry_in
				via_via_ok = in_rte + 1
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec = 1
				end
			
			end
		end
	elseif in_rte == 1 then
		if fpln_num4 > 2 then
			if find_via_via(fpln_data4[fpln_num4-1][1], fpln_data4[fpln_num4-1][3], fpln_data4[fpln_num4][2], entry_in) == 0 then
				if mode_in == 0 then
					entry = ">INVALID ENTRY"
				else
					entry2 = ">INVALID ENTRY"
				end
			else
				fpln_data4[fpln_num4][4] = fpln_data4[fpln_num4-1][4] + fpln_data4[fpln_num4-1][5]	--legs_data_idx
				idx_tmp = fpln_data4[fpln_num4][4] + fpln_data4[fpln_num4][5]
				fpln_data4[fpln_num4][5] = awy_path_num
				
				-- add airways waypoints
				idx_tmp2 = fpln_data4[fpln_num4][4]
				fpln_add_leg_dir2(idx_tmp, idx_tmp2, fpln_data4[fpln_num4][2], in_rte)
				
				via_via_entry = entry_in
				via_via_ok = in_rte + 1
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec2 = 1
				end
			
			end
		end
	end
end

function find_via_via(awy_from, awy_from_rc, awy1, awy2)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local awy_idx = 0
	local awy_idx2 = 0
	local from_tmp = awy_from
	local from_tmp_rc = awy_from_rc
	local to_find = 0
	
	local check_awy = 0
	local awy_repeat_n = 0
	local awy_repeat = 0
	local aw_used = 0
	local awy_enable = 0
	
	local nd_x = 0
	local idx_rec = 0
	
	local awy_temp_next_num = 0
	raw_table('awy_temp_next')
	local awy_temp_next = {}
	
	awy_path = {}
	awy_path_num = 0
	
	if string.len(awy_from_rc) > 0 then
		kk = 1
	end
	
	
	-- find awy2
	if awy_data_num > 0 and awy2 ~= "" and string.len(awy2) > 0 then
		nd_x = string.byte(string.sub(awy2, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_awy[nd_x][99999] > 0 then
				for ll = 1, idx_awy[nd_x][99999] do
					idx_rec = idx_awy[nd_x][ll]
					if awy_data[idx_rec][1] == awy2 then
						awy_temp_next_num = awy_temp_next_num + 1
						awy_temp_next[awy_temp_next_num] = {}
						for ii = 1, 6 do
							awy_temp_next[awy_temp_next_num][ii] = awy_data[idx_rec][ii]
						end
					end
				end
			end
		end
	end
	
	if awy_temp_next_num > 0 then
	
		if awy_temp_num2 > 0 then
			
			awy_repeat_n = 2
			for awy_repeat = 1, awy_repeat_n do
				
				awy_path = {}
				awy_path_num = 0
				
				for jj = 1, awy_temp_num2 do 
					for ii = 1, awy_temp_num2 do
				
						awy_enable = 0
						if awy_repeat == 1 then
							if awy_temp2[ii][7] == 0 and awy_temp2[ii][8] == 0 then
								awy_enable = 1
							end
						else
							awy_enable = 1
							if awy_temp2[ii][7] == 1 and awy_temp2[ii][8] == 1 then
								awy_enable = 0
							end
						end
				
						if awy_enable == 1 then
							if kk == 0 then
								aw_used = 0
								if awy_temp2[ii][7] == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "F") then
									if awy_temp2[ii][2] == from_tmp then
										awy_path_num = awy_path_num + 1
										awy_path[awy_path_num] = {true, true}
										awy_path[awy_path_num][1] = awy_temp2[ii][3]
										awy_path[awy_path_num][2] = awy_temp2[ii][5]
										awy_temp2[ii][7] = 1	-- used
										from_tmp = awy_temp2[ii][3]
										from_tmp_rc = awy_temp2[ii][5]
										check_awy = 1
										aw_used = 1
									end
								end
								if awy_temp2[ii][8] == 0 and aw_used == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "B") then
									if awy_temp2[ii][3] == from_tmp then
										awy_path_num = awy_path_num + 1
										awy_path[awy_path_num] = {true, true}
										awy_path[awy_path_num][1] = awy_temp2[ii][2]
										awy_path[awy_path_num][2] = awy_temp2[ii][4]
										awy_temp2[ii][8] = 1	-- used
										from_tmp = awy_temp2[ii][2]
										from_tmp_rc = awy_temp2[ii][4]
										check_awy = 1
									end
								end
							else
								aw_used = 0
								if awy_temp2[ii][7] == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "F") then
									if awy_temp2[ii][2] == from_tmp and awy_temp2[ii][4] == from_tmp_rc then
										awy_path_num = awy_path_num + 1
										awy_path[awy_path_num] = {true, true}
										awy_path[awy_path_num][1] = awy_temp2[ii][3]
										awy_path[awy_path_num][2] = awy_temp2[ii][5]
										awy_temp2[ii][7] = 1	-- used
										from_tmp = awy_temp2[ii][3]
										from_tmp_rc = awy_temp2[ii][5]
										check_awy = 1
										aw_used = 1
									end
								end
								if awy_temp2[ii][8] == 0 and aw_used == 0 and (awy_temp2[ii][6] == "N" or awy_temp2[ii][6] == "B") then
									if awy_temp2[ii][3] == from_tmp and awy_temp2[ii][5] == from_tmp_rc then
										awy_path_num = awy_path_num + 1
										awy_path[awy_path_num] = {true, true}
										awy_path[awy_path_num][1] = awy_temp2[ii][2]
										awy_path[awy_path_num][2] = awy_temp2[ii][4]
										awy_temp2[ii][8] = 1	-- used
										from_tmp = awy_temp2[ii][2]
										from_tmp_rc = awy_temp2[ii][4]
										check_awy = 1
									end
								end
							end
							
							if check_awy == 1 then
									
								for ll = 1, awy_temp_next_num do
									if awy_temp_next[ll][2] == from_tmp and awy_temp_next[ll][4] == from_tmp_rc then
										to_find = 1
									end
									if awy_temp_next[ll][3] == from_tmp and awy_temp_next[ll][5] == from_tmp_rc then
										to_find = 1
									end
									if to_find == 1 then
										if awy_path_num == 1 and awy_temp_next[ll][3] == "N" then
											awy_path_num = 0
										end
										break
									end
								end
							end
							
							check_awy = 0
							
							if to_find == 1 then
								break
							end
						end
					end
					if to_find == 1 then
						break
					end
				end
				if to_find == 1 then
					break
				end
				
				from_tmp = awy_from
				from_tmp_rc = awy_from_rc
			end
		
		end
		--dump_awy2()
		if awy_path_num == 0 then
			to_find = 0
		end
	
	end
	
	return to_find

end

function via_add(awy_from2, awy_from_rc2, entry_in, mode_in, in_rte)
	
	-- check via
	if find_awy(awy_from2, awy_from_rc2, entry_in) == 0 then
		entry_in = ">INVALID ENTRY"
	else
		if in_rte == 0 then
			-- add fpln -> via
			fpln_num2 = fpln_num2 + 1
			fpln_data2[fpln_num2] = {}
			fpln_data2[fpln_num2][1] = ""
			fpln_data2[fpln_num2][2] = entry_in
			fpln_data2[fpln_num2][3] = ""
			fpln_data2[fpln_num2][4] = 1	--legs_data_idx
			fpln_data2[fpln_num2][5] = 0	-- num_legs_data
			fpln_data2[fpln_num2][6] = 1	-- fpln new
			if mode_in == 0 then
				entry = ""
			else
				entry2 = ""
			end
			
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec = 1
			end
		elseif in_rte == 1 then
			-- add fpln -> via
			fpln_num4 = fpln_num4 + 1
			fpln_data4[fpln_num4] = {}
			fpln_data4[fpln_num4][1] = ""
			fpln_data4[fpln_num4][2] = entry_in
			fpln_data4[fpln_num4][3] = ""
			fpln_data4[fpln_num4][4] = 1	--legs_data_idx
			fpln_data4[fpln_num4][5] = 0	-- num_legs_data
			fpln_data4[fpln_num4][6] = 1	-- fpln new
			if mode_in == 0 then
				entry = ""
			else
				entry2 = ""
			end
			
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec2 = 1
			end
		end
	end
	
end

function via_chg(awy_from2, awy_from_rc2, via_id, entry_in, mode_in, in_rte)
	
	local idx_tmp = 0
	local idx_tmp2 = 0
	
	if in_rte == 0 then
		-- check via
		if find_awy(awy_from2, awy_from_rc2, entry_in) == 0 then
			if mode_in == 0 then
				entry = ">INVALID ENTRY"
			else
				entry2 = ">INVALID ENTRY"
			end
		else
			if fpln_data2[via_id][1] == "" then
				fpln_data2[via_id][2] = entry_in
				idx_tmp = fpln_data2[via_id-1][4] + fpln_data2[via_id-1][5]
				legs_data2[idx_tmp][9] = entry_in
			else
				if find_awy_path(awy_from2, awy_from_rc2, fpln_data2[via_id][1], fpln_data2[via_id][3], entry_in) == 0 then
					if mode_in == 0 then
						entry = ">INVALID ENTRY"
					else
						entry2 = ">INVALID ENTRY"
					end
				else
					if via_id == 1 then
						fpln_data2[via_id][4] = 2	--legs_data_idx
						idx_tmp = fpln_data2[via_id][4] + fpln_data2[via_id][5]
						fpln_data2[via_id][5] = awy_path_num
					else
						fpln_data2[via_id][4] = fpln_data2[via_id-1][4] + fpln_data2[via_id-1][5]	--legs_data_idx
						idx_tmp = fpln_data2[via_id][4] + fpln_data2[via_id][5]
						fpln_data2[via_id][5] = awy_path_num
					end
					
					-- add airways waypoints
					idx_tmp2 = fpln_data2[via_id][4]
					fpln_add_leg_dir2(idx_tmp, idx_tmp2, entry_in, in_rte)
				end
			end
			--dump_leg()
			if mode_in == 0 then
				entry = ""
			else
				entry2 = ""
			end
			
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec = 1
			end
		end
	elseif in_rte == 1 then
		-- check via
		if find_awy(awy_from2, awy_from_rc2, entry_in) == 0 then
			if mode_in == 0 then
				entry = ">INVALID ENTRY"
			else
				entry2 = ">INVALID ENTRY"
			end
		else
			if fpln_data4[via_id][1] == "" then
				fpln_data4[via_id][2] = entry_in
				idx_tmp = fpln_data4[via_id-1][4] + fpln_data4[via_id-1][5]
				legs_data8[idx_tmp][9] = entry_in
			else
				if find_awy_path(awy_from2, awy_from_rc2, fpln_data4[via_id][1], fpln_data4[via_id][3], entry_in) == 0 then
					if mode_in == 0 then
						entry = ">INVALID ENTRY"
					else
						entry2 = ">INVALID ENTRY"
					end
				else
					if via_id == 1 then
						fpln_data4[via_id][4] = 2	--legs_data_idx
						idx_tmp = fpln_data4[via_id][4] + fpln_data4[via_id][5]
						fpln_data4[via_id][5] = awy_path_num
					else
						fpln_data4[via_id][4] = fpln_data4[via_id-1][4] + fpln_data4[via_id-1][5]	--legs_data_idx
						idx_tmp = fpln_data4[via_id][4] + fpln_data4[via_id][5]
						fpln_data4[via_id][5] = awy_path_num
					end
					
					-- add airways waypoints
					idx_tmp2 = fpln_data4[via_id][4]
					fpln_add_leg_dir2(idx_tmp, idx_tmp2, entry_in, in_rte)
				end
			end
			--dump_leg()
			if mode_in == 0 then
				entry = ""
			else
				entry2 = ""
			end
			
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec2 = 1
			end
		end
	end
end


function dir_via_add(awy_from2, awy_from_rc2, awy_to2, awy_to_rc2, via2, via_idx, entry_in, mode_in, in_rte)
	
	local idx_tmp = 0
	local idx_tmp2 = 0
	
	dir_change = 0
	dir_idx = via_idx
	dir_disco = 0
	
	if in_rte == 0 then
		if via2 == "" then
			-- check navaid
			-- nav mode => distance from 0-PPOS, 1-REF ICAO, 2-DES ICAO
			find_navaid(entry_in, "", 0, "", in_rte)
			if navaid_list_n == 0 then
				add_fmc_msg(NOT_IN_DATABASE, 1)
			elseif navaid_list_n == 1 then
				-- add fpln -> direct navaid
				if via_idx > fpln_num2 then
					fpln_num2 = fpln_num2 + 1
					fpln_data2[fpln_num2] = {}
					fpln_data2[via_idx][1] = navaid_list[1][4]	--entry
					fpln_data2[via_idx][2] = "DIRECT"
					fpln_data2[via_idx][3] = navaid_list[1][8]	--reg_code
					if via_idx == 1 then
						if legs_num2 == 1 then
							fpln_data2[via_idx][4] = 2	--legs_data_idx
						else
							fpln_data2[via_idx][4] = 3	--legs_data_idx
						end
						fpln_data2[via_idx][5] = 1
					else
						fpln_data2[via_idx][4] = fpln_data2[via_idx-1][4] + fpln_data2[via_idx-1][5]	--legs_data_idx
						fpln_data2[via_idx][5] = 1
					end
					fpln_data2[via_idx][6] = 1	-- fpln new
					idx_tmp = fpln_data2[via_idx][4]
					idx_tmp2 = idx_tmp
				else
					fpln_data2[via_idx][1] = navaid_list[1][4]	--entry
					fpln_data2[via_idx][2] = "DIRECT"
					fpln_data2[via_idx][3] = navaid_list[1][8]	--reg_code
					
					idx_tmp = fpln_data2[via_idx][4] + fpln_data2[via_idx][5]
					idx_tmp2 = fpln_data2[via_idx][4]
					fpln_data2[via_idx][5] = 1
					
					-- add DISCONTINUITY
					local item_sel_via_x = item_sel_via
					if mode_in ~= 0 then
						item_sel_via_x = item_sel_via2
					end
					local via_idx_tmp = 0
					if item_sel_via_x ~= 0 then
						if item_sel_via_x < fpln_num2 then
							via_idx_tmp = fpln_data2[item_sel_via_x][4] + fpln_data2[item_sel_via_x][5]
							if via_idx_tmp < legs_num2 then
								if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
									dir_disco = 1
								end
							end
						end
					elseif via_idx < fpln_num2 then
						if via_idx > 1 then
							via_idx_tmp = fpln_data2[via_idx-1][4] + fpln_data2[via_idx-1][5]
							if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
								dir_disco = 1
							end
						end
					end
				end
				
				-- add legs direct waypoint
				fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", 1, in_rte)
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec = 1
				end
				create_fpln(in_rte)
			else
				-- more navaids
				if mode_in == 0 then
					entry = ""
					page_sel_wpt2 = 1
					page_rte_init = 0
					act_page_old = act_page
					act_page = 1
				else
					entry2 = ""
					page_sel_wpt2_2 = 1
					page_rte_init2 = 0
					act_page_old2 = act_page2
					act_page2 = 1
				end
			end
		elseif via2 == "DIRECT" then
			find_navaid(entry_in, "", 0, "", in_rte, in_rte)
			if navaid_list_n == 0 then
				add_fmc_msg(NOT_IN_DATABASE, 1)
			elseif navaid_list_n == 1 then
				fpln_data2[via_idx][1] = navaid_list[1][4]	--entry
				fpln_data2[via_idx][2] = "DIRECT"
				fpln_data2[via_idx][3] = navaid_list[1][8]	--reg_code
				
				idx_tmp = fpln_data2[via_idx][4] + fpln_data2[via_idx][5]
				idx_tmp2 = fpln_data2[via_idx][4]
				fpln_data2[via_idx][5] = 1
				
				-- add DISCONTINUITY
				local item_sel_via_x = item_sel_via
				if mode_in ~= 0 then
					item_sel_via_x = item_sel_via2
				end
				local via_idx_tmp = 0
				if item_sel_via_x ~= 0 then
					if item_sel_via_x < fpln_num2 then
						via_idx_tmp = fpln_data2[item_sel_via_x][4] + fpln_data2[item_sel_via_x][5]
						if via_idx_tmp < legs_num2 then
							if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
								dir_disco = 1
							end
						end
					end
				elseif via_idx < fpln_num2 then
					if via_idx > 1 then
						via_idx_tmp = fpln_data2[via_idx-1][4] + fpln_data2[via_idx-1][5]
						if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
							dir_disco = 1
						end
					end
				end
				
				-- change legs direct waypoint
				fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", 1, in_rte)
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec = 1
				end
				create_fpln(in_rte)
			else
				-- more navaids
				if mode_in == 0 then
					entry = ""
					page_sel_wpt2 = 1
					page_rte_init = 0
					act_page_old = act_page
					act_page = 1
				else
					entry2 = ""
					page_sel_wpt2_2 = 1
					page_rte_init2 = 0
					act_page_old2 = act_page2
					act_page2 = 1
				end
				dir_change = 1
			end
		else
			-- add fpln -> via navaid
			find_awy(awy_from2, awy_from_rc2, via2)
			if find_awy_path(awy_from2, awy_from_rc2, awy_to2, awy_to_rc2, via2) == 0 then
				if mode_in == 0 then
					entry = ">INVALID ENTRY"
				else
					entry2 = ">INVALID ENTRY"
				end
			else
				fpln_data2[via_idx][1] = awy_path[awy_path_num][1]	--entry
				fpln_data2[via_idx][3] = awy_path[awy_path_num][2]	--reg_code
				
				if via_idx == 1 then
					fpln_data2[via_idx][4] = 2	--legs_data_idx
					idx_tmp = fpln_data2[via_idx][4] + fpln_data2[via_idx][5]
					fpln_data2[via_idx][5] = awy_path_num
				else
					fpln_data2[via_idx][4] = fpln_data2[via_idx-1][4] + fpln_data2[via_idx-1][5]	--legs_data_idx
					idx_tmp = fpln_data2[via_idx][4] + fpln_data2[via_idx][5]
					fpln_data2[via_idx][5] = awy_path_num
				end
				
				-- add airways waypoints
				idx_tmp2 = fpln_data2[via_idx][4]
				
				-- add DISCONTINUITY
				local item_sel_via_x = item_sel_via
				if mode_in ~= 0 then
					item_sel_via_x = item_sel_via2
				end
				local via_idx_tmp = 0
				if item_sel_via_x ~= 0 then
					if item_sel_via_x < fpln_num2 then
						via_idx_tmp = fpln_data2[item_sel_via_x][4] + fpln_data2[item_sel_via_x][5]
						idx_tmp = idx_tmp + 1
						if via_idx_tmp < legs_num2 then
							if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
								dir_disco = 1
							end
						end
					end
				elseif via_idx < fpln_num2 then
					if via_idx > 1 then
						via_idx_tmp = fpln_data2[via_idx-1][4] + fpln_data2[via_idx-1][5]
						if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
							dir_disco = 1
						end
					end
				end
				
				fpln_add_leg_dir3(idx_tmp, idx_tmp2, via2, in_rte)
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec = 1
				end
			end
		end
	elseif in_rte == 1 then
		if via2 == "" then
			-- check navaid
			-- nav mode => distance from 0-PPOS, 1-REF ICAO, 2-DES ICAO
			find_navaid(entry_in, "", 0, "", in_rte, in_rte)
			if navaid_list_n == 0 then
				add_fmc_msg(NOT_IN_DATABASE, 1)
			elseif navaid_list_n == 1 then
				-- add fpln -> direct navaid
				if via_idx > fpln_num4 then
					fpln_num4 = fpln_num4 + 1
					fpln_data4[fpln_num4] = {}
					fpln_data4[via_idx][1] = navaid_list[1][4]	--entry
					fpln_data4[via_idx][2] = "DIRECT"
					fpln_data4[via_idx][3] = navaid_list[1][8]	--reg_code
					if via_idx == 1 then
						if legs_num8 == 1 then
							fpln_data4[via_idx][4] = 2	--legs_data_idx
						else
							fpln_data4[via_idx][4] = 3	--legs_data_idx
						end
						fpln_data4[via_idx][5] = 1
					else
						fpln_data4[via_idx][4] = fpln_data4[via_idx-1][4] + fpln_data4[via_idx-1][5]	--legs_data_idx
						fpln_data4[via_idx][5] = 1
					end
					fpln_data4[via_idx][6] = 1	-- fpln new
					idx_tmp = fpln_data4[via_idx][4]
					idx_tmp2 = idx_tmp
				else
					fpln_data4[via_idx][1] = navaid_list[1][4]	--entry
					fpln_data4[via_idx][2] = "DIRECT"
					fpln_data4[via_idx][3] = navaid_list[1][8]	--reg_code
					
					idx_tmp = fpln_data4[via_idx][4] + fpln_data4[via_idx][5]
					idx_tmp2 = fpln_data4[via_idx][4]
					fpln_data4[via_idx][5] = 1
					fpln_data4[via_idx][6] = 1	-- fpln new
					
					-- add DISCONTINUITY
					local item_sel_via_x = item_sel_via
					if mode_in ~= 0 then
						item_sel_via_x = item_sel_via2
					end
					local via_idx_tmp = 0
					if item_sel_via_x ~= 0 then
						if item_sel_via_x < fpln_num4 then
							via_idx_tmp = fpln_data4[item_sel_via_x][4] + fpln_data4[item_sel_via_x][5]
							if via_idx_tmp < legs_num8 then
								if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
									dir_disco = 1
								end
							end
						end
					elseif via_idx < fpln_num4 then
						if via_idx > 1 then
							via_idx_tmp = fpln_data4[via_idx-1][4] + fpln_data4[via_idx-1][5]
							if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
								dir_disco = 1
							end
						end
					end
				end
				
				-- add legs direct waypoint
				fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", 1, in_rte)
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec2 = 1
				end
				create_fpln(in_rte)
			else
				-- more navaids
				if mode_in == 0 then
					entry = ""
					page_sel_wpt2 = 1
					page_rte_init = 0
					act_page_old = act_page
					act_page = 1
				else
					entry2 = ""
					page_sel_wpt2_2 = 1
					page_rte_init2 = 0
					act_page_old2 = act_page2
					act_page2 = 1
				end
			end
		elseif via2 == "DIRECT" then
			find_navaid(entry_in, "", 0, "", in_rte, in_rte)
			if navaid_list_n == 0 then
				add_fmc_msg(NOT_IN_DATABASE, 1)
			elseif navaid_list_n == 1 then
				fpln_data4[via_idx][1] = navaid_list[1][4]	--entry
				fpln_data4[via_idx][2] = "DIRECT"
				fpln_data4[via_idx][3] = navaid_list[1][8]	--reg_code
				
				idx_tmp = fpln_data4[via_idx][4] + fpln_data4[via_idx][5]
				idx_tmp2 = fpln_data4[via_idx][4]
				fpln_data4[via_idx][5] = 1
				
				-- add DISCONTINUITY
				local item_sel_via_x = item_sel_via
				if mode_in ~= 0 then
					item_sel_via_x = item_sel_via2
				end
				local via_idx_tmp = 0
				if item_sel_via_x ~= 0 then
					if item_sel_via_x < fpln_num4 then
						via_idx_tmp = fpln_data4[item_sel_via_x][4] + fpln_data4[item_sel_via_x][5]
						if via_idx_tmp < legs_num8 then
							if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
								dir_disco = 1
							end
						end
					end
				elseif via_idx < fpln_num4 then
					if via_idx > 1 then
						via_idx_tmp = fpln_data4[via_idx-1][4] + fpln_data4[via_idx-1][5]
						if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
							dir_disco = 1
						end
					end
				end
				
				-- change legs direct waypoint
				fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", 1, in_rte)
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec2 = 1
				end
				create_fpln(in_rte)
			else
				-- more navaids
				if mode_in == 0 then
					entry = ""
					page_sel_wpt2 = 1
					page_rte_init = 0
					act_page_old = act_page
					act_page = 1
				else
					entry2 = ""
					page_sel_wpt2_2 = 1
					page_rte_init2 = 0
					act_page_old2 = act_page2
					act_page2 = 1
				end
				dir_change = 1
			end
		else
			-- add fpln -> via navaid
			find_awy(awy_from2, awy_from_rc2, via2)
			if find_awy_path(awy_from2, awy_from_rc2, awy_to2, awy_to_rc2, via2) == 0 then
				if mode_in == 0 then
					entry = ">INVALID ENTRY"
				else
					entry2 = ">INVALID ENTRY"
				end
			else
				fpln_data4[via_idx][1] = awy_path[awy_path_num][1]	--entry
				fpln_data4[via_idx][3] = awy_path[awy_path_num][2]	--reg_code
				
				if via_idx == 1 then
					fpln_data4[via_idx][4] = 2
					idx_tmp = fpln_data4[via_idx][4] + fpln_data4[via_idx][5]
					fpln_data4[via_idx][5] = awy_path_num
				else
					fpln_data4[via_idx][4] = fpln_data4[via_idx-1][4] + fpln_data4[via_idx-1][5]	--legs_data_idx
					idx_tmp = fpln_data4[via_idx][4] + fpln_data4[via_idx][5]
					fpln_data4[via_idx][5] = awy_path_num
				end
				
				-- add airways waypoints
				idx_tmp2 = fpln_data4[via_idx][4]
				
				-- add DISCONTINUITY
				local item_sel_via_x = item_sel_via
				if mode_in ~= 0 then
					item_sel_via_x = item_sel_via2
				end
				local via_idx_tmp = 0
				if item_sel_via_x ~= 0 then
					if item_sel_via_x < fpln_num4 then
						via_idx_tmp = fpln_data4[item_sel_via_x][4] + fpln_data4[item_sel_via_x][5]
						idx_tmp = idx_tmp + 1
						if via_idx_tmp < legs_num8 then
							if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
								dir_disco = 1
							end
						end
					end
				elseif via_idx < fpln_num4 then
					if via_idx > 1 then
						via_idx_tmp = fpln_data4[via_idx-1][4] + fpln_data4[via_idx-1][5]
						if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
							dir_disco = 1
						end
					end
				end
				
				fpln_add_leg_dir3(idx_tmp, idx_tmp2, via2, in_rte)
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
				if legs_num > 1 and rte12_act == in_rte then
					rte_exec2 = 1
				end
			end
		end
	end
end

function dir_add(awy_idx, entry_in, mode_in, in_rte)
	
	local idx_tmp = 0
	local idx_tmp2 = 0
	
	if in_rte == 0 then
		if dir_change == 0 then
			-- add navaid
			if dir_idx > fpln_num2 then
				fpln_num2 = fpln_num2 + 1
				fpln_data2[fpln_num2] = {}
				fpln_data2[dir_idx][1] = navaid_list[awy_idx][4]
				fpln_data2[dir_idx][2] = "DIRECT"
				fpln_data2[dir_idx][3] = navaid_list[awy_idx][8]
				
				if dir_idx == 1 then
					if legs_num2 == 1 then
						fpln_data2[dir_idx][4] = 2	--legs_data_idx
					else
						fpln_data2[dir_idx][4] = 3	--legs_data_idx
					end
					fpln_data2[dir_idx][5] = 1
				else
					fpln_data2[dir_idx][4] = fpln_data2[dir_idx-1][4] + fpln_data2[dir_idx-1][5]	--legs_data_idx
					fpln_data2[dir_idx][5] = 1
				end
				fpln_data2[dir_idx][6] = 1	-- fpln new
				
				idx_tmp = fpln_data2[dir_idx][4]
				idx_tmp2 = idx_tmp
			else
				fpln_data2[dir_idx][1] = navaid_list[awy_idx][4]
				fpln_data2[dir_idx][2] = "DIRECT"
				fpln_data2[dir_idx][3] = navaid_list[awy_idx][8]
				idx_tmp = fpln_data2[dir_idx][4] + fpln_data2[dir_idx][5]
				idx_tmp2 = fpln_data2[dir_idx][4]
				fpln_data2[dir_idx][5] = 1
				
				-- add DISCONTINUITY
				local item_sel_via_x = item_sel_via
				if mode_in ~= 0 then
					item_sel_via_x = item_sel_via2
				end
				local via_idx_tmp = 0
				if dir_idx < legs_num2 then
					if dir_idx > 1 then
						via_idx_tmp = fpln_data2[dir_idx-1][4] + fpln_data2[dir_idx-1][5]
						if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
							dir_disco = 1
						end
					end
				end
			end
			
			-- add legs direct waypoint
			fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", awy_idx, in_rte)
			
		else
			-- change navaid
			fpln_data2[dir_idx][1] = navaid_list[awy_idx][4]
			fpln_data2[dir_idx][2] = "DIRECT"
			fpln_data2[dir_idx][3] = navaid_list[awy_idx][8]
			idx_tmp = fpln_data2[dir_idx][4] + fpln_data2[dir_idx][5]
			idx_tmp2 = fpln_data2[dir_idx][4]
			fpln_data2[dir_idx][5] = 1
			
			-- add DISCONTINUITY
			local item_sel_via_x = item_sel_via
			if mode_in ~= 0 then
				item_sel_via_x = item_sel_via2
			end
			local via_idx_tmp = 0
			if dir_idx < legs_num2 then
				if dir_idx > 1 then
					via_idx_tmp = fpln_data2[dir_idx-1][4] + fpln_data2[dir_idx-1][5]
					if legs_data2[via_idx_tmp][1] == "DISCONTINUITY" then
						dir_disco = 1
					end
				end
			end
			
			-- change legs direct waypoint
			fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", awy_idx, in_rte)
		end
		
		if mode_in == 0 then
			entry = ""
			page_sel_wpt2 = 0
			page_rte_init = 1
			act_page = act_page_old
			max_page = math.max(max_page, act_page)
		else
			entry2 = ""
			page_sel_wpt2_2 = 0
			page_rte_init2 = 1
			act_page2 = act_page_old2
			max_page2 = math.max(max_page2, act_page2)
		end
		if legs_num > 1 and rte12_act == in_rte then
			rte_exec = 1
		end
	elseif in_rte == 1 then
		if dir_change == 0 then
			-- add navaid
			if dir_idx > fpln_num4 then
				fpln_num4 = fpln_num4 + 1
				fpln_data4[fpln_num4] = {}
				fpln_data4[dir_idx][1] = navaid_list[awy_idx][4]
				fpln_data4[dir_idx][2] = "DIRECT"
				fpln_data4[dir_idx][3] = navaid_list[awy_idx][8]
				
				if dir_idx == 1 then
					if legs_num8 == 1 then
						fpln_data4[dir_idx][4] = 2	--legs_data_idx
					else
						fpln_data4[dir_idx][4] = 3	--legs_data_idx
					end
					fpln_data4[dir_idx][5] = 1
				else
					fpln_data4[dir_idx][4] = fpln_data4[dir_idx-1][4] + fpln_data4[dir_idx-1][5]	--legs_data_idx
					fpln_data4[dir_idx][5] = 1
				end
				fpln_data4[dir_idx][6] = 1	-- fpln new
				
				idx_tmp = fpln_data4[dir_idx][4]
				idx_tmp2 = idx_tmp
			else
				fpln_data4[dir_idx][1] = navaid_list[awy_idx][4]
				fpln_data4[dir_idx][2] = "DIRECT"
				fpln_data4[dir_idx][3] = navaid_list[awy_idx][8]
				idx_tmp = fpln_data4[dir_idx][4] + fpln_data4[dir_idx][5]
				idx_tmp2 = fpln_data4[dir_idx][4]
				fpln_data4[dir_idx][5] = 1
				
				-- add DISCONTINUITY
				local item_sel_via_x = item_sel_via
				if mode_in ~= 0 then
					item_sel_via_x = item_sel_via2
				end
				local via_idx_tmp = 0
				if dir_idx < legs_num8 then
					if dir_idx > 1 then
						via_idx_tmp = fpln_data4[dir_idx-1][4] + fpln_data4[dir_idx-1][5]
						if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
							dir_disco = 1
						end
					end
				end
			end
			
			-- add legs direct waypoint
			fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", awy_idx, in_rte)
			
		else
			-- change navaid
			fpln_data4[dir_idx][1] = navaid_list[awy_idx][4]
			fpln_data4[dir_idx][2] = "DIRECT"
			fpln_data4[dir_idx][3] = navaid_list[awy_idx][8]
			idx_tmp = fpln_data4[dir_idx][4] + fpln_data4[dir_idx][5]
			idx_tmp2 = fpln_data4[dir_idx][4]
			fpln_data4[dir_idx][5] = 1
			
			-- add DISCONTINUITY
			local item_sel_via_x = item_sel_via
			if mode_in ~= 0 then
				item_sel_via_x = item_sel_via2
			end
			local via_idx_tmp = 0
			if dir_idx < legs_num8 then
				if dir_idx > 1 then
					via_idx_tmp = fpln_data4[dir_idx-1][4] + fpln_data4[dir_idx-1][5]
					if legs_data8[via_idx_tmp][1] == "DISCONTINUITY" then
						dir_disco = 1
					end
				end
			end
			
			-- change legs direct waypoint
			fpln_add_leg_dir(idx_tmp, idx_tmp2, "DIRECT", awy_idx, in_rte)
		end
		
		if mode_in == 0 then
			entry = ""
			page_sel_wpt2 = 0
			page_rte_init = 1
			act_page = act_page_old
			max_page = math.max(max_page, act_page)
		else
			entry2 = ""
			page_sel_wpt2_2 = 0
			page_rte_init2 = 1
			act_page2 = act_page_old2
			max_page2 = math.max(max_page2, act_page2)
		end
		if legs_num > 1 and rte12_act == in_rte then
			rte_exec2 = 1
		end
	end
end


function del_via(via_id, mode_in, in_rte)
	
	local idx_tmp = 0
	local idx_tmp2 = 0
	
	if in_rte == 0 then
		if fpln_data2[via_id][2] ~= "DIRECT" and fpln_data2[via_id][1] ~= "" then
			
			fpln_data2[via_id][2] = "DIRECT"
			idx_tmp = fpln_data2[via_id][4] + fpln_data2[via_id][5] - 1
			idx_tmp2 = fpln_data2[via_id][4]
			fpln_data2[via_id][5] = 1
			
			fpln_del_leg(idx_tmp, idx_tmp2, in_rte)
			
			--entry = ""
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec = 1
			end
		elseif fpln_data2[via_id][2] ~= "DIRECT" and fpln_data2[via_id][1] == "" then
			fpln_data2[via_id][2] = ""
			idx_tmp = fpln_data2[via_id][4] + fpln_data2[via_id][5]
			legs_data2[idx_tmp][9] = ""
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec = 1
			end
		end
		
		if mode_in == 0 then
			entry = ""
		else
			entry2 = ""
		end
	elseif in_rte == 1 then
		if fpln_data4[via_id][2] ~= "DIRECT" and fpln_data4[via_id][1] ~= "" then
			
			fpln_data4[via_id][2] = "DIRECT"
			idx_tmp = fpln_data4[via_id][4] + fpln_data4[via_id][5] - 1
			idx_tmp2 = fpln_data4[via_id][4]
			fpln_data4[via_id][5] = 1
			
			fpln_del_leg(idx_tmp, idx_tmp2, in_rte)
			
			--entry = ""
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec2 = 1
			end
		elseif fpln_data4[via_id][2] ~= "DIRECT" and fpln_data4[via_id][1] == "" then
			fpln_data4[via_id][2] = ""
			idx_tmp = fpln_data4[via_id][4] + fpln_data4[via_id][5]
			legs_data8[idx_tmp][9] = ""
			if legs_num > 1 and rte12_act == in_rte then
				rte_exec2 = 1
			end
		end
		
		if mode_in == 0 then
			entry = ""
		else
			entry2 = ""
		end
	end
end


function fpln_del_leg(idx_copy, idx_first, in_rte)
	
	if in_rte == 0 then
		if idx_first > 1 and idx_first <= legs_num2 then
			legs_data2[idx_copy][9] = "DIRECT"
			rte_copy(idx_copy, in_rte)
			rte_paste(idx_first, in_rte)
			if rte12_act == in_rte then
				legs_delete = 1
			end
			fms_recalc = 1
			msg_unavaible_crz_alt = 0
			calc_rte_enable2 = 1
		end
	elseif in_rte == 1 then
		if idx_first > 1 and idx_first <= legs_num8 then
			legs_data8[idx_copy][9] = "DIRECT"
			rte_copy(idx_copy, in_rte)
			rte_paste(idx_first, in_rte)
			if rte12_act == in_rte then
				legs_delete = 1
			end
			fms_recalc = 1
			msg_unavaible_crz_alt = 0
			calc_rte_enable8 = 1
		end
	end
end

function add_empty_leg_wpt(idx_empty)
	-- create EMPTY WPT
	legs_num2 = idx_empty
	legs_data2[legs_num2] = {}
	legs_data2[legs_num2][1] = ""
	legs_data2[legs_num2][2] = 0		-- brg
	legs_data2[legs_num2][3] = 0		-- distance
	legs_data2[legs_num2][4] = 0		-- speed
	legs_data2[legs_num2][5] = 0		-- altitude
	legs_data2[legs_num2][6] = 0	-- altitude type
	legs_data2[legs_num2][7] = 0		-- latitude
	legs_data2[legs_num2][8] = 0		-- longitude
	legs_data2[legs_num2][9] = ""			-- via id
	legs_data2[legs_num2][10] = 0		-- calc speed
	legs_data2[legs_num2][11] = 0		-- calc altitude
	legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
	legs_data2[legs_num2][13] = 0
	legs_data2[legs_num2][14] = 0		-- rest alt
	legs_data2[legs_num2][15] = 0		-- last fuel
	legs_data2[legs_num2][16] = ""
	legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
	legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
	legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
	legs_data2[legs_num2][20] = 0
	legs_data2[legs_num2][21] = -1
	legs_data2[legs_num2][22] = ""
	legs_data2[legs_num2][23] = 0
	legs_data2[legs_num2][24] = 0
	legs_data2[legs_num2][25] = 0
	legs_data2[legs_num2][26] = 0
	legs_data2[legs_num2][27] = ""
	legs_data2[legs_num2][28] = ""
	legs_data2[legs_num2][29] = ""
	legs_data2[legs_num2][30] = ""
	legs_data2[legs_num2][31] = ""
	legs_data2[legs_num2][32] = 0
	legs_data2[legs_num2][33] = ""
	legs_data2[legs_num2][34] = ""
	legs_data2[legs_num2][35] = ""
	legs_data2[legs_num2][36] = 0
	legs_data2[legs_num2][37] = 0
	legs_data2[legs_num2][38] = ""
	legs_data2[legs_num2][39] = ""
	legs_data2[legs_num2][40] = 0
	legs_data2[legs_num2][41] = 0
	legs_data2[legs_num2][42] = 0
	legs_data2[legs_num2][43] = ""
	legs_data2[legs_num2][44] = 0
	legs_data2[legs_num2][45] = 0
	legs_data2[legs_num2][46] = 0
	legs_data2[legs_num2][47] = 0
	legs_data2[legs_num2][48] = ""
	legs_data2[legs_num2][49] = 0
	legs_data2[legs_num2][50] = 0
	legs_data2[legs_num2][51] = 0
	legs_data2[legs_num2][52] = 0
	legs_data2[legs_num2][53] = 0
	legs_data2[legs_num2][54] = 0
end

function fpln_add_leg_disco(idx_disco, in_rte)
	
	if in_rte == 0 then
		-- create DISCONTINUITY
		legs_num2 = idx_disco
		legs_data2[legs_num2] = {}
		legs_data2[legs_num2][1] = "DISCONTINUITY"
		legs_data2[legs_num2][2] = 0		-- brg
		legs_data2[legs_num2][3] = 0		-- distance
		legs_data2[legs_num2][4] = 0		-- speed
		legs_data2[legs_num2][5] = 0		-- altitude
		legs_data2[legs_num2][6] = 0	-- altitude type
		legs_data2[legs_num2][7] = 0		-- latitude
		legs_data2[legs_num2][8] = 0		-- longitude
		legs_data2[legs_num2][9] = ""			-- via id
		legs_data2[legs_num2][10] = 0		-- calc speed
		legs_data2[legs_num2][11] = 0		-- calc altitude
		legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
		legs_data2[legs_num2][13] = 0
		legs_data2[legs_num2][14] = 0		-- rest alt
		legs_data2[legs_num2][15] = 0		-- last fuel
		legs_data2[legs_num2][16] = ""
		legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data2[legs_num2][20] = 0
		legs_data2[legs_num2][21] = -1
		legs_data2[legs_num2][22] = ""
		legs_data2[legs_num2][23] = 0
		legs_data2[legs_num2][24] = 0
		legs_data2[legs_num2][25] = 0
		legs_data2[legs_num2][26] = 0
		legs_data2[legs_num2][27] = ""
		legs_data2[legs_num2][28] = ""
		legs_data2[legs_num2][29] = ""
		legs_data2[legs_num2][30] = ""
		legs_data2[legs_num2][31] = ""
		legs_data2[legs_num2][32] = 0
		legs_data2[legs_num2][33] = ""
		legs_data2[legs_num2][34] = ""
		legs_data2[legs_num2][35] = ""
		legs_data2[legs_num2][36] = 0
		legs_data2[legs_num2][37] = 0
		legs_data2[legs_num2][38] = ""
		legs_data2[legs_num2][39] = ""
		legs_data2[legs_num2][40] = 0
		legs_data2[legs_num2][41] = 0
		legs_data2[legs_num2][42] = 0
		legs_data2[legs_num2][43] = ""
		legs_data2[legs_num2][44] = 0
		legs_data2[legs_num2][45] = 0
		legs_data2[legs_num2][46] = 0
		legs_data2[legs_num2][47] = 0
		legs_data2[legs_num2][48] = ""
		legs_data2[legs_num2][49] = 0
		legs_data2[legs_num2][50] = 0
		legs_data2[legs_num2][51] = 0
		legs_data2[legs_num2][52] = 0
		legs_data2[legs_num2][53] = 0
		legs_data2[legs_num2][54] = 0
	elseif in_rte == 1 then
		-- create DISCONTINUITY
		legs_num8 = idx_disco
		legs_data8[legs_num8] = {}
		legs_data8[legs_num8][1] = "DISCONTINUITY"
		legs_data8[legs_num8][2] = 0		-- brg
		legs_data8[legs_num8][3] = 0		-- distance
		legs_data8[legs_num8][4] = 0		-- speed
		legs_data8[legs_num8][5] = 0		-- altitude
		legs_data8[legs_num8][6] = 0	-- altitude type
		legs_data8[legs_num8][7] = 0		-- latitude
		legs_data8[legs_num8][8] = 0		-- longitude
		legs_data8[legs_num8][9] = ""			-- via id
		legs_data8[legs_num8][10] = 0		-- calc speed
		legs_data8[legs_num8][11] = 0		-- calc altitude
		legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
		legs_data8[legs_num8][13] = 0
		legs_data8[legs_num8][14] = 0		-- rest alt
		legs_data8[legs_num8][15] = 0		-- last fuel
		legs_data8[legs_num8][16] = ""
		legs_data8[legs_num8][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data8[legs_num8][20] = 0
		legs_data8[legs_num8][21] = -1
		legs_data8[legs_num8][22] = ""
		legs_data8[legs_num8][23] = 0
		legs_data8[legs_num8][24] = 0
		legs_data8[legs_num8][25] = 0
		legs_data8[legs_num8][26] = 0
		legs_data8[legs_num8][27] = ""
		legs_data8[legs_num8][28] = ""
		legs_data8[legs_num8][29] = ""
		legs_data8[legs_num8][30] = ""
		legs_data8[legs_num8][31] = ""
		legs_data8[legs_num8][32] = 0
		legs_data8[legs_num8][33] = ""
		legs_data8[legs_num8][34] = ""
		legs_data8[legs_num8][35] = ""
		legs_data8[legs_num8][36] = 0
		legs_data8[legs_num8][37] = 0
		legs_data8[legs_num8][38] = ""
		legs_data8[legs_num8][39] = ""
		legs_data8[legs_num8][40] = 0
		legs_data8[legs_num8][41] = 0
		legs_data8[legs_num8][42] = 0
		legs_data8[legs_num8][43] = ""
		legs_data8[legs_num8][44] = 0
		legs_data8[legs_num8][45] = 0
		legs_data8[legs_num8][46] = 0
		legs_data8[legs_num8][47] = 0
		legs_data8[legs_num8][48] = ""
		legs_data8[legs_num8][49] = 0
		legs_data8[legs_num8][50] = 0
		legs_data8[legs_num8][51] = 0
		legs_data8[legs_num8][52] = 0
		legs_data8[legs_num8][53] = 0
		legs_data8[legs_num8][54] = 0
	end
	
end

function fpln_add_leg_dir2(idx_copy, idx_paste, idx_via, in_rte)
	
	local leg_wpt_id = ""
	local leg_wpt_rg = ""
	local legs_num_tmp = 0
	
	if in_rte == 0 then
		legs_num_tmp = legs_num2
		rte_copy(idx_copy, in_rte)
		legs_num2 = idx_paste - 1
		
		if awy_path_num > 0 then
			
			-- dump_leg_append("fpln_add_leg_dir2 - legs:\n")
			
			for ii = 1, awy_path_num do
				
				
				leg_wpt_id = awy_path[ii][1]
				leg_wpt_rg = awy_path[ii][2]
				find_navaid(leg_wpt_id, "", 0, leg_wpt_rg, in_rte)
				
				legs_num2 = legs_num2 + 1
				if navaid_list_n ~= 0 then
					legs_data2[legs_num2] = {}
					legs_data2[legs_num2][1] = navaid_list[1][4]	--entry
					legs_data2[legs_num2][2] = 0		-- brg
					legs_data2[legs_num2][3] = 0		-- distance
					legs_data2[legs_num2][4] = 0		-- speed
					legs_data2[legs_num2][5] = 0		-- altitude
					legs_data2[legs_num2][6] = 0	-- altitude type
					legs_data2[legs_num2][7] = navaid_list[1][2]		-- latitude
					legs_data2[legs_num2][8] = navaid_list[1][3]		-- longitude
					legs_data2[legs_num2][9] = idx_via			-- via id
					legs_data2[legs_num2][10] = 0		-- calc speed
					legs_data2[legs_num2][11] = 0		-- calc altitude
					legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
					legs_data2[legs_num2][13] = 0
					legs_data2[legs_num2][14] = 0		-- rest alt
					legs_data2[legs_num2][15] = 0		-- last fuel
					legs_data2[legs_num2][16] = navaid_list[1][8]		-- reg code
					legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data2[legs_num2][20] = 0
					legs_data2[legs_num2][21] = -1
					legs_data2[legs_num2][22] = ""
					legs_data2[legs_num2][23] = 0
					legs_data2[legs_num2][24] = 0
					legs_data2[legs_num2][25] = 0
					legs_data2[legs_num2][26] = 0
					legs_data2[legs_num2][27] = ""
					legs_data2[legs_num2][28] = ""
					legs_data2[legs_num2][29] = ""
					legs_data2[legs_num2][30] = ""
					legs_data2[legs_num2][31] = ""
					legs_data2[legs_num2][32] = 0
					legs_data2[legs_num2][33] = ""
					legs_data2[legs_num2][34] = ""
					legs_data2[legs_num2][35] = ""
					legs_data2[legs_num2][36] = navaid_list[1][1]
					legs_data2[legs_num2][37] = 0
					legs_data2[legs_num2][38] = ""
					legs_data2[legs_num2][39] = ""
					legs_data2[legs_num2][40] = 0
					legs_data2[legs_num2][41] = 0
					legs_data2[legs_num2][42] = 0
					legs_data2[legs_num2][43] = ""
					legs_data2[legs_num2][44] = 0
					legs_data2[legs_num2][45] = 0
					legs_data2[legs_num2][46] = 0
					legs_data2[legs_num2][47] = 0
					legs_data2[legs_num2][48] = ""
					legs_data2[legs_num2][49] = 0
					legs_data2[legs_num2][50] = 0
					legs_data2[legs_num2][51] = 0
					legs_data2[legs_num2][52] = 0
					legs_data2[legs_num2][53] = 0
					legs_data2[legs_num2][54] = 0
				else
					legs_data2[legs_num2] = {}
					legs_data2[legs_num2][1] = leg_wpt_id	--entry
					legs_data2[legs_num2][2] = 0		-- brg
					legs_data2[legs_num2][3] = 0		-- distance
					legs_data2[legs_num2][4] = 0		-- speed
					legs_data2[legs_num2][5] = 0		-- altitude
					legs_data2[legs_num2][6] = 0	-- altitude type
					legs_data2[legs_num2][7] = 0		-- latitude
					legs_data2[legs_num2][8] = 0		-- longitude
					legs_data2[legs_num2][9] = idx_via			-- via id
					legs_data2[legs_num2][10] = 0		-- calc speed
					legs_data2[legs_num2][11] = 0		-- calc altitude
					legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
					legs_data2[legs_num2][13] = 0
					legs_data2[legs_num2][14] = 0		-- rest alt
					legs_data2[legs_num2][15] = 0		-- last fuel
					legs_data2[legs_num2][16] = leg_wpt_rg		-- reg code
					legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data2[legs_num2][20] = 0
					legs_data2[legs_num2][21] = -1
					legs_data2[legs_num2][22] = ""
					legs_data2[legs_num2][23] = 0
					legs_data2[legs_num2][24] = 0
					legs_data2[legs_num2][25] = 0
					legs_data2[legs_num2][26] = 0
					legs_data2[legs_num2][27] = ""
					legs_data2[legs_num2][28] = ""
					legs_data2[legs_num2][29] = ""
					legs_data2[legs_num2][30] = ""
					legs_data2[legs_num2][31] = ""
					legs_data2[legs_num2][32] = 0
					legs_data2[legs_num2][33] = ""
					legs_data2[legs_num2][34] = ""
					legs_data2[legs_num2][35] = ""
					legs_data2[legs_num2][36] = 0
					legs_data2[legs_num2][37] = 0
					legs_data2[legs_num2][38] = ""
					legs_data2[legs_num2][39] = ""
					legs_data2[legs_num2][40] = 0
					legs_data2[legs_num2][41] = 0
					legs_data2[legs_num2][42] = 0
					legs_data2[legs_num2][43] = ""
					legs_data2[legs_num2][44] = 0
					legs_data2[legs_num2][45] = 0
					legs_data2[legs_num2][46] = 0
					legs_data2[legs_num2][47] = 0
					legs_data2[legs_num2][48] = ""
					legs_data2[legs_num2][49] = 0
					legs_data2[legs_num2][50] = 0
					legs_data2[legs_num2][51] = 0
					legs_data2[legs_num2][52] = 0
					legs_data2[legs_num2][53] = 0
					legs_data2[legs_num2][54] = 0
				end
				-- dump_leg_append(tostring(legs_num2) .. ": " .. legs_data2[legs_num2][1] .. "\n")
			end
			
			if dir_disco == 1 then
				legs_num2 = legs_num2 + 1
				legs_data2[legs_num2] = {}
				fpln_add_leg_disco(legs_num2, in_rte)
			else
				if legs_data2_tmp_n > 1 then
					legs_num2 = legs_num2 - 1
					-- change last WAYPOINT to DIRECT
					legs_data2_tmp[1][9] = "DIRECT"
					legs_data2_tmp[1][19] = 4
				end
			end
			
		else
			legs_num2 = legs_num_tmp
		end
		
		dir_disco = 0
		rte_paste(legs_num2 + 1, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable2 = 1
	elseif in_rte == 1 then
		legs_num_tmp = legs_num8
		rte_copy(idx_copy, in_rte)
		legs_num8 = idx_paste - 1
		
		if awy_path_num > 0 then
			
			-- dump_leg_append("fpln_add_leg_dir2 - legs:\n")
			
			for ii = 1, awy_path_num do
				
				
				leg_wpt_id = awy_path[ii][1]
				leg_wpt_rg = awy_path[ii][2]
				find_navaid(leg_wpt_id, "", 0, leg_wpt_rg, in_rte)
				
				legs_num8 = legs_num8 + 1
				if navaid_list_n ~= 0 then
					legs_data8[legs_num8] = {}
					legs_data8[legs_num8][1] = navaid_list[1][4]	--entry
					legs_data8[legs_num8][2] = 0		-- brg
					legs_data8[legs_num8][3] = 0		-- distance
					legs_data8[legs_num8][4] = 0		-- speed
					legs_data8[legs_num8][5] = 0		-- altitude
					legs_data8[legs_num8][6] = 0	-- altitude type
					legs_data8[legs_num8][7] = navaid_list[1][2]		-- latitude
					legs_data8[legs_num8][8] = navaid_list[1][3]		-- longitude
					legs_data8[legs_num8][9] = idx_via			-- via id
					legs_data8[legs_num8][10] = 0		-- calc speed
					legs_data8[legs_num8][11] = 0		-- calc altitude
					legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
					legs_data8[legs_num8][13] = 0
					legs_data8[legs_num8][14] = 0		-- rest alt
					legs_data8[legs_num8][15] = 0		-- last fuel
					legs_data8[legs_num8][16] = navaid_list[1][8]		-- reg code
					legs_data8[legs_num8][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data8[legs_num8][20] = 0
					legs_data8[legs_num8][21] = -1
					legs_data8[legs_num8][22] = ""
					legs_data8[legs_num8][23] = 0
					legs_data8[legs_num8][24] = 0
					legs_data8[legs_num8][25] = 0
					legs_data8[legs_num8][26] = 0
					legs_data8[legs_num8][27] = ""
					legs_data8[legs_num8][28] = ""
					legs_data8[legs_num8][29] = ""
					legs_data8[legs_num8][30] = ""
					legs_data8[legs_num8][31] = ""
					legs_data8[legs_num8][32] = 0
					legs_data8[legs_num8][33] = ""
					legs_data8[legs_num8][34] = ""
					legs_data8[legs_num8][35] = ""
					legs_data8[legs_num8][36] = navaid_list[1][1]
					legs_data8[legs_num8][37] = 0
					legs_data8[legs_num8][38] = ""
					legs_data8[legs_num8][39] = ""
					legs_data8[legs_num8][40] = 0
					legs_data8[legs_num8][41] = 0
					legs_data8[legs_num8][42] = 0
					legs_data8[legs_num8][43] = ""
					legs_data8[legs_num8][44] = 0
					legs_data8[legs_num8][45] = 0
					legs_data8[legs_num8][46] = 0
					legs_data8[legs_num8][47] = 0
					legs_data8[legs_num8][48] = ""
					legs_data8[legs_num8][49] = 0
					legs_data8[legs_num8][50] = 0
					legs_data8[legs_num8][51] = 0
					legs_data8[legs_num8][52] = 0
					legs_data8[legs_num8][53] = 0
					legs_data8[legs_num8][54] = 0
				else
					legs_data8[legs_num8] = {}
					legs_data8[legs_num8][1] = leg_wpt_id	--entry
					legs_data8[legs_num8][2] = 0		-- brg
					legs_data8[legs_num8][3] = 0		-- distance
					legs_data8[legs_num8][4] = 0		-- speed
					legs_data8[legs_num8][5] = 0		-- altitude
					legs_data8[legs_num8][6] = 0	-- altitude type
					legs_data8[legs_num8][7] = 0		-- latitude
					legs_data8[legs_num8][8] = 0		-- longitude
					legs_data8[legs_num8][9] = idx_via			-- via id
					legs_data8[legs_num8][10] = 0		-- calc speed
					legs_data8[legs_num8][11] = 0		-- calc altitude
					legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
					legs_data8[legs_num8][13] = 0
					legs_data8[legs_num8][14] = 0		-- rest alt
					legs_data8[legs_num8][15] = 0		-- last fuel
					legs_data8[legs_num8][16] = leg_wpt_rg		-- reg code
					legs_data8[legs_num8][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data8[legs_num8][20] = 0
					legs_data8[legs_num8][21] = -1
					legs_data8[legs_num8][22] = ""
					legs_data8[legs_num8][23] = 0
					legs_data8[legs_num8][24] = 0
					legs_data8[legs_num8][25] = 0
					legs_data8[legs_num8][26] = 0
					legs_data8[legs_num8][27] = ""
					legs_data8[legs_num8][28] = ""
					legs_data8[legs_num8][29] = ""
					legs_data8[legs_num8][30] = ""
					legs_data8[legs_num8][31] = ""
					legs_data8[legs_num8][32] = 0
					legs_data8[legs_num8][33] = ""
					legs_data8[legs_num8][34] = ""
					legs_data8[legs_num8][35] = ""
					legs_data8[legs_num8][36] = 0
					legs_data8[legs_num8][37] = 0
					legs_data8[legs_num8][38] = ""
					legs_data8[legs_num8][39] = ""
					legs_data8[legs_num8][40] = 0
					legs_data8[legs_num8][41] = 0
					legs_data8[legs_num8][42] = 0
					legs_data8[legs_num8][43] = ""
					legs_data8[legs_num8][44] = 0
					legs_data8[legs_num8][45] = 0
					legs_data8[legs_num8][46] = 0
					legs_data8[legs_num8][47] = 0
					legs_data8[legs_num8][48] = ""
					legs_data8[legs_num8][49] = 0
					legs_data8[legs_num8][50] = 0
					legs_data8[legs_num8][51] = 0
					legs_data8[legs_num8][52] = 0
					legs_data8[legs_num8][53] = 0
					legs_data8[legs_num8][54] = 0
				end
			end
			
			if dir_disco == 1 then
				legs_num8 = legs_num8 + 1
				legs_data8[legs_num8] = {}
				fpln_add_leg_disco(legs_num8, in_rte)
			else
				if legs_data2_tmp_n > 1 then
					legs_num8 = legs_num8 - 1
					-- change last WAYPOINT to DIRECT
					legs_data2_tmp[1][9] = "DIRECT"
					legs_data2_tmp[1][19] = 4
				end
			end
			
		else
			legs_num8 = legs_num_tmp
		end
		
		dir_disco = 0
		rte_paste(legs_num8 + 1, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable8 = 1
	end
end

function fpln_add_leg_dir3(idx_copy, idx_paste, idx_via, in_rte)
	
	local leg_wpt_id = ""
	local leg_wpt_rg = ""
	local legs_num_tmp = 0
	
	if in_rte == 0 then
		legs_num_tmp = legs_num2
		rte_copy(idx_copy, in_rte)
		legs_num2 = idx_paste - 1
		
		if awy_path_num > 0 then
			
			for ii = 1, awy_path_num do
				
				
				leg_wpt_id = awy_path[ii][1]
				leg_wpt_rg = awy_path[ii][2]
				find_navaid(leg_wpt_id, "", 0, leg_wpt_rg, in_rte)
				
				legs_num2 = legs_num2 + 1
				if navaid_list_n ~= 0 then
					legs_data2[legs_num2] = {}
					legs_data2[legs_num2][1] = navaid_list[1][4]	--entry
					legs_data2[legs_num2][2] = 0		-- brg
					legs_data2[legs_num2][3] = 0		-- distance
					legs_data2[legs_num2][4] = 0		-- speed
					legs_data2[legs_num2][5] = 0		-- altitude
					legs_data2[legs_num2][6] = 0	-- altitude type
					legs_data2[legs_num2][7] = navaid_list[1][2]		-- latitude
					legs_data2[legs_num2][8] = navaid_list[1][3]		-- longitude
					legs_data2[legs_num2][9] = idx_via			-- via id
					legs_data2[legs_num2][10] = 0		-- calc speed
					legs_data2[legs_num2][11] = 0		-- calc altitude
					legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
					legs_data2[legs_num2][13] = 0
					legs_data2[legs_num2][14] = 0		-- rest alt
					legs_data2[legs_num2][15] = 0		-- last fuel
					legs_data2[legs_num2][16] = navaid_list[1][8]		-- reg code
					legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data2[legs_num2][20] = 0
					legs_data2[legs_num2][21] = -1
					legs_data2[legs_num2][22] = ""
					legs_data2[legs_num2][23] = 0
					legs_data2[legs_num2][24] = 0
					legs_data2[legs_num2][25] = 0
					legs_data2[legs_num2][26] = 0
					legs_data2[legs_num2][27] = ""
					legs_data2[legs_num2][28] = ""
					legs_data2[legs_num2][29] = ""
					legs_data2[legs_num2][30] = ""
					legs_data2[legs_num2][31] = ""
					legs_data2[legs_num2][32] = 0
					legs_data2[legs_num2][33] = ""
					legs_data2[legs_num2][34] = ""
					legs_data2[legs_num2][35] = ""
					legs_data2[legs_num2][36] = navaid_list[1][1]
					legs_data2[legs_num2][37] = 0
					legs_data2[legs_num2][38] = ""
					legs_data2[legs_num2][39] = ""
					legs_data2[legs_num2][40] = 0
					legs_data2[legs_num2][41] = 0
					legs_data2[legs_num2][42] = 0
					legs_data2[legs_num2][43] = ""
					legs_data2[legs_num2][44] = 0
					legs_data2[legs_num2][45] = 0
					legs_data2[legs_num2][46] = 0
					legs_data2[legs_num2][47] = 0
					legs_data2[legs_num2][48] = ""
					legs_data2[legs_num2][49] = 0
					legs_data2[legs_num2][50] = 0
					legs_data2[legs_num2][51] = 0
					legs_data2[legs_num2][52] = 0
					legs_data2[legs_num2][53] = 0
					legs_data2[legs_num2][54] = 0
				else
					legs_data2[legs_num2] = {}
					legs_data2[legs_num2][1] = leg_wpt_id	--entry
					legs_data2[legs_num2][2] = 0		-- brg
					legs_data2[legs_num2][3] = 0		-- distance
					legs_data2[legs_num2][4] = 0		-- speed
					legs_data2[legs_num2][5] = 0		-- altitude
					legs_data2[legs_num2][6] = 0	-- altitude type
					legs_data2[legs_num2][7] = 0		-- latitude
					legs_data2[legs_num2][8] = 0		-- longitude
					legs_data2[legs_num2][9] = idx_via			-- via id
					legs_data2[legs_num2][10] = 0		-- calc speed
					legs_data2[legs_num2][11] = 0		-- calc altitude
					legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
					legs_data2[legs_num2][13] = 0
					legs_data2[legs_num2][14] = 0		-- rest alt
					legs_data2[legs_num2][15] = 0		-- last fuel
					legs_data2[legs_num2][16] = leg_wpt_rg		-- reg code
					legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data2[legs_num2][20] = 0
					legs_data2[legs_num2][21] = -1
					legs_data2[legs_num2][22] = ""
					legs_data2[legs_num2][23] = 0
					legs_data2[legs_num2][24] = 0
					legs_data2[legs_num2][25] = 0
					legs_data2[legs_num2][26] = 0
					legs_data2[legs_num2][27] = ""
					legs_data2[legs_num2][28] = ""
					legs_data2[legs_num2][29] = ""
					legs_data2[legs_num2][30] = ""
					legs_data2[legs_num2][31] = ""
					legs_data2[legs_num2][32] = 0
					legs_data2[legs_num2][33] = ""
					legs_data2[legs_num2][34] = ""
					legs_data2[legs_num2][35] = ""
					legs_data2[legs_num2][36] = 0
					legs_data2[legs_num2][37] = 0
					legs_data2[legs_num2][38] = ""
					legs_data2[legs_num2][39] = ""
					legs_data2[legs_num2][40] = 0
					legs_data2[legs_num2][41] = 0
					legs_data2[legs_num2][42] = 0
					legs_data2[legs_num2][43] = ""
					legs_data2[legs_num2][44] = 0
					legs_data2[legs_num2][45] = 0
					legs_data2[legs_num2][46] = 0
					legs_data2[legs_num2][47] = 0
					legs_data2[legs_num2][48] = ""
					legs_data2[legs_num2][49] = 0
					legs_data2[legs_num2][50] = 0
					legs_data2[legs_num2][51] = 0
					legs_data2[legs_num2][52] = 0
					legs_data2[legs_num2][53] = 0
					legs_data2[legs_num2][54] = 0
				end
			end
			
			if dir_disco == 1 then
				legs_num2 = legs_num2 + 1
				legs_data2[legs_num2] = {}
				fpln_add_leg_disco(legs_num2, in_rte)
			end
			
		else
			legs_num2 = legs_num_tmp
		end
		
		dir_disco = 0
		rte_paste(legs_num2 + 1, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable2 = 1
	elseif in_rte == 1 then
		legs_num_tmp = legs_num8
		rte_copy(idx_copy, in_rte)
		legs_num8 = idx_paste - 1
		
		if awy_path_num > 0 then
			
			for ii = 1, awy_path_num do
				
				
				leg_wpt_id = awy_path[ii][1]
				leg_wpt_rg = awy_path[ii][2]
				find_navaid(leg_wpt_id, "", 0, leg_wpt_rg, in_rte)
				
				legs_num8 = legs_num8 + 1
				if navaid_list_n ~= 0 then
					legs_data8[legs_num8] = {}
					legs_data8[legs_num8][1] = navaid_list[1][4]	--entry
					legs_data8[legs_num8][2] = 0		-- brg
					legs_data8[legs_num8][3] = 0		-- distance
					legs_data8[legs_num8][4] = 0		-- speed
					legs_data8[legs_num8][5] = 0		-- altitude
					legs_data8[legs_num8][6] = 0	-- altitude type
					legs_data8[legs_num8][7] = navaid_list[1][2]		-- latitude
					legs_data8[legs_num8][8] = navaid_list[1][3]		-- longitude
					legs_data8[legs_num8][9] = idx_via			-- via id
					legs_data8[legs_num8][10] = 0		-- calc speed
					legs_data8[legs_num8][11] = 0		-- calc altitude
					legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
					legs_data8[legs_num8][13] = 0
					legs_data8[legs_num8][14] = 0		-- rest alt
					legs_data8[legs_num8][15] = 0		-- last fuel
					legs_data8[legs_num8][16] = navaid_list[1][8]		-- reg code
					legs_data8[legs_num8][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data8[legs_num8][20] = 0
					legs_data8[legs_num8][21] = -1
					legs_data8[legs_num8][22] = ""
					legs_data8[legs_num8][23] = 0
					legs_data8[legs_num8][24] = 0
					legs_data8[legs_num8][25] = 0
					legs_data8[legs_num8][26] = 0
					legs_data8[legs_num8][27] = ""
					legs_data8[legs_num8][28] = ""
					legs_data8[legs_num8][29] = ""
					legs_data8[legs_num8][30] = ""
					legs_data8[legs_num8][31] = ""
					legs_data8[legs_num8][32] = 0
					legs_data8[legs_num8][33] = ""
					legs_data8[legs_num8][34] = ""
					legs_data8[legs_num8][35] = ""
					legs_data8[legs_num8][36] = navaid_list[1][1]
					legs_data8[legs_num8][37] = 0
					legs_data8[legs_num8][38] = ""
					legs_data8[legs_num8][39] = ""
					legs_data8[legs_num8][40] = 0
					legs_data8[legs_num8][41] = 0
					legs_data8[legs_num8][42] = 0
					legs_data8[legs_num8][43] = ""
					legs_data8[legs_num8][44] = 0
					legs_data8[legs_num8][45] = 0
					legs_data8[legs_num8][46] = 0
					legs_data8[legs_num8][47] = 0
					legs_data8[legs_num8][48] = ""
					legs_data8[legs_num8][49] = 0
					legs_data8[legs_num8][50] = 0
					legs_data8[legs_num8][51] = 0
					legs_data8[legs_num8][52] = 0
					legs_data8[legs_num8][53] = 0
					legs_data8[legs_num8][54] = 0
				else
					legs_data8[legs_num8] = {}
					legs_data8[legs_num8][1] = leg_wpt_id	--entry
					legs_data8[legs_num8][2] = 0		-- brg
					legs_data8[legs_num8][3] = 0		-- distance
					legs_data8[legs_num8][4] = 0		-- speed
					legs_data8[legs_num8][5] = 0		-- altitude
					legs_data8[legs_num8][6] = 0	-- altitude type
					legs_data8[legs_num8][7] = 0		-- latitude
					legs_data8[legs_num8][8] = 0		-- longitude
					legs_data8[legs_num8][9] = idx_via			-- via id
					legs_data8[legs_num8][10] = 0		-- calc speed
					legs_data8[legs_num8][11] = 0		-- calc altitude
					legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
					legs_data8[legs_num8][13] = 0
					legs_data8[legs_num8][14] = 0		-- rest alt
					legs_data8[legs_num8][15] = 0		-- last fuel
					legs_data8[legs_num8][16] = leg_wpt_rg		-- reg code
					legs_data8[legs_num8][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data8[legs_num8][20] = 0
					legs_data8[legs_num8][21] = -1
					legs_data8[legs_num8][22] = ""
					legs_data8[legs_num8][23] = 0
					legs_data8[legs_num8][24] = 0
					legs_data8[legs_num8][25] = 0
					legs_data8[legs_num8][26] = 0
					legs_data8[legs_num8][27] = ""
					legs_data8[legs_num8][28] = ""
					legs_data8[legs_num8][29] = ""
					legs_data8[legs_num8][30] = ""
					legs_data8[legs_num8][31] = ""
					legs_data8[legs_num8][32] = 0
					legs_data8[legs_num8][33] = ""
					legs_data8[legs_num8][34] = ""
					legs_data8[legs_num8][35] = ""
					legs_data8[legs_num8][36] = 0
					legs_data8[legs_num8][37] = 0
					legs_data8[legs_num8][38] = ""
					legs_data8[legs_num8][39] = ""
					legs_data8[legs_num8][40] = 0
					legs_data8[legs_num8][41] = 0
					legs_data8[legs_num8][42] = 0
					legs_data8[legs_num8][43] = ""
					legs_data8[legs_num8][44] = 0
					legs_data8[legs_num8][45] = 0
					legs_data8[legs_num8][46] = 0
					legs_data8[legs_num8][47] = 0
					legs_data8[legs_num8][48] = ""
					legs_data8[legs_num8][49] = 0
					legs_data8[legs_num8][50] = 0
					legs_data8[legs_num8][51] = 0
					legs_data8[legs_num8][52] = 0
					legs_data8[legs_num8][53] = 0
					legs_data8[legs_num8][54] = 0
				end
			end
			
			if dir_disco == 1 then
				legs_num8 = legs_num8 + 1
				legs_data8[legs_num8] = {}
				fpln_add_leg_disco(legs_num8, in_rte)
			end
			
		else
			legs_num8 = legs_num_tmp
		end
		
		dir_disco = 0
		rte_paste(legs_num8 + 1, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable8 = 1
	end
end

function fpln_add_leg_dir(idx_copy, idx_paste, idx_via, wpt_idx, in_rte)
	
	if in_rte == 0 then
		rte_copy(idx_copy, in_rte)
		legs_num2 = idx_paste
		
		legs_data2[legs_num2] = {}
		legs_data2[legs_num2][1] = navaid_list[wpt_idx][4]	--entry
		legs_data2[legs_num2][2] = 0		-- brg
		legs_data2[legs_num2][3] = 0		-- distance
		legs_data2[legs_num2][4] = 0		-- speed
		legs_data2[legs_num2][5] = 0		-- altitude
		legs_data2[legs_num2][6] = 0	-- altitude type
		legs_data2[legs_num2][7] = navaid_list[wpt_idx][2]		-- latitude
		legs_data2[legs_num2][8] = navaid_list[wpt_idx][3]		-- longitude
		legs_data2[legs_num2][9] = idx_via			-- via id
		legs_data2[legs_num2][10] = 0		-- calc speed
		legs_data2[legs_num2][11] = 0		-- calc altitude
		legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
		legs_data2[legs_num2][13] = 0
		legs_data2[legs_num2][14] = 0		-- rest alt
		legs_data2[legs_num2][15] = 0		-- last fuel
		legs_data2[legs_num2][16] = navaid_list[wpt_idx][8]		-- reg code
		legs_data2[legs_num2][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data2[legs_num2][20] = 0
		legs_data2[legs_num2][21] = -1
		legs_data2[legs_num2][22] = ""
		legs_data2[legs_num2][23] = 0
		legs_data2[legs_num2][24] = 0
		legs_data2[legs_num2][25] = 0
		legs_data2[legs_num2][26] = 0
		legs_data2[legs_num2][27] = ""
		legs_data2[legs_num2][28] = ""
		legs_data2[legs_num2][29] = ""
		legs_data2[legs_num2][30] = ""
		legs_data2[legs_num2][31] = ""
		legs_data2[legs_num2][32] = 0
		legs_data2[legs_num2][33] = ""
		legs_data2[legs_num2][34] = ""
		legs_data2[legs_num2][35] = ""
		legs_data2[legs_num2][36] = navaid_list[wpt_idx][1]
		legs_data2[legs_num2][37] = 0
		legs_data2[legs_num2][38] = ""
		legs_data2[legs_num2][39] = ""
		legs_data2[legs_num2][40] = 0
		legs_data2[legs_num2][41] = 0
		legs_data2[legs_num2][42] = 0
		legs_data2[legs_num2][43] = ""
		legs_data2[legs_num2][44] = 0
		legs_data2[legs_num2][45] = 0
		legs_data2[legs_num2][46] = 0
		legs_data2[legs_num2][47] = 0
		legs_data2[legs_num2][48] = ""
		legs_data2[legs_num2][49] = 0
		legs_data2[legs_num2][50] = 0
		legs_data2[legs_num2][51] = 0
		legs_data2[legs_num2][52] = 0
		legs_data2[legs_num2][53] = 0
		legs_data2[legs_num2][54] = 0
			
		if dir_disco == 1 then
			-- add DISCO
			legs_num2 = legs_num2 + 1
			legs_data2[legs_num2] = {}
			fpln_add_leg_disco(legs_num2, in_rte)
		else
			if legs_data2_tmp_n > 1 then
				legs_num2 = legs_num2 - 1
			-- change last WAYPOINT to DIRECT
				legs_data2_tmp[1][9] = "DIRECT"
				legs_data2_tmp[1][19] = 4
			end
		end
		dir_disco = 0
		
		rte_paste(legs_num2 + 1, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable2 = 1
	elseif in_rte == 1 then
		rte_copy(idx_copy, in_rte)
		legs_num8 = idx_paste
		
		legs_data8[legs_num8] = {}
		legs_data8[legs_num8][1] = navaid_list[wpt_idx][4]	--entry
		legs_data8[legs_num8][2] = 0		-- brg
		legs_data8[legs_num8][3] = 0		-- distance
		legs_data8[legs_num8][4] = 0		-- speed
		legs_data8[legs_num8][5] = 0		-- altitude
		legs_data8[legs_num8][6] = 0	-- altitude type
		legs_data8[legs_num8][7] = navaid_list[wpt_idx][2]		-- latitude
		legs_data8[legs_num8][8] = navaid_list[wpt_idx][3]		-- longitude
		legs_data8[legs_num8][9] = idx_via			-- via id
		legs_data8[legs_num8][10] = 0		-- calc speed
		legs_data8[legs_num8][11] = 0		-- calc altitude
		legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
		legs_data8[legs_num8][13] = 0
		legs_data8[legs_num8][14] = 0		-- rest alt
		legs_data8[legs_num8][15] = 0		-- last fuel
		legs_data8[legs_num8][16] = navaid_list[wpt_idx][8]		-- reg code
		legs_data8[legs_num8][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data8[legs_num8][20] = 0
		legs_data8[legs_num8][21] = -1
		legs_data8[legs_num8][22] = ""
		legs_data8[legs_num8][23] = 0
		legs_data8[legs_num8][24] = 0
		legs_data8[legs_num8][25] = 0
		legs_data8[legs_num8][26] = 0
		legs_data8[legs_num8][27] = ""
		legs_data8[legs_num8][28] = ""
		legs_data8[legs_num8][29] = ""
		legs_data8[legs_num8][30] = ""
		legs_data8[legs_num8][31] = ""
		legs_data8[legs_num8][32] = 0
		legs_data8[legs_num8][33] = ""
		legs_data8[legs_num8][34] = ""
		legs_data8[legs_num8][35] = ""
		legs_data8[legs_num8][36] = navaid_list[wpt_idx][1]
		legs_data8[legs_num8][37] = 0
		legs_data8[legs_num8][38] = ""
		legs_data8[legs_num8][39] = ""
		legs_data8[legs_num8][40] = 0
		legs_data8[legs_num8][41] = 0
		legs_data8[legs_num8][42] = 0
		legs_data8[legs_num8][43] = ""
		legs_data8[legs_num8][44] = 0
		legs_data8[legs_num8][45] = 0
		legs_data8[legs_num8][46] = 0
		legs_data8[legs_num8][47] = 0
		legs_data8[legs_num8][48] = ""
		legs_data8[legs_num8][49] = 0
		legs_data8[legs_num8][50] = 0
		legs_data8[legs_num8][51] = 0
		legs_data8[legs_num8][52] = 0
		legs_data8[legs_num8][53] = 0
		legs_data8[legs_num8][54] = 0
			
		if dir_disco == 1 then
			-- add DISCO
			legs_num8 = legs_num8 + 1
			legs_data8[legs_num8] = {}
			fpln_add_leg_disco(legs_num8, in_rte)
		else
			if legs_data2_tmp_n > 1 then
				legs_num8 = legs_num8 - 1
			-- change last WAYPOINT to DIRECT
				legs_data2_tmp[1][9] = "DIRECT"
				legs_data2_tmp[1][19] = 4
			end
		end
		dir_disco = 0
		
		rte_paste(legs_num8 + 1, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable8 = 1
	end
end


function create_fpln_old(in_rte)

	local ii = 0
	local compare_txt = ""
	local rw_disable = 0
	
	if in_rte == 0 then
		fpln_num2 = 0
		fpln_data2 = {}
		
		hold_data_num2 = 0
		hold_data2 = {}
		
		if legs_num2 == 2 then
			
			if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
				rw_disable = 1
			end
			
			if rw_disable == 0 then
				fpln_num2 = fpln_num2 + 1
				fpln_data2[fpln_num2] = {true, true, true, true, true}
				if legs_data2[2][1] == "DISCONTINUITY" then
					fpln_data2[fpln_num2][1] = ""
					fpln_data2[fpln_num2][2] = legs_data2[2][9]
					fpln_data2[fpln_num2][3] = ""
					fpln_data2[fpln_num2][4] = 2
					fpln_data2[fpln_num2][5] = 1
				else
					if legs_data2[2][19] == 3 then
						legs_data2[2][9] = "DIRECT"
						legs_data2[2][19] = 0
					end
					fpln_data2[fpln_num2][1] = legs_data2[2][1]
					fpln_data2[fpln_num2][2] = legs_data2[2][9]
					fpln_data2[fpln_num2][3] = legs_data2[2][16]
					fpln_data2[fpln_num2][4] = 2
					fpln_data2[fpln_num2][5] = 1
				end
			end
		elseif legs_num2 > 1 then
			
			if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
				rw_disable = 1
			end
			
			if rw_disable == 0 then
				fpln_num2 = fpln_num2 + 1
				fpln_data2[fpln_num2] = {true, true, true, true, true}
				if legs_data2[2][1] == "DISCONTINUITY" then
					fpln_data2[fpln_num2][1] = ""
					fpln_data2[fpln_num2][2] = legs_data2[2][9]
					fpln_data2[fpln_num2][3] = ""
					fpln_data2[fpln_num2][4] = 2
					fpln_data2[fpln_num2][5] = 1
				else
					if legs_data2[2][19] == 3 then
						legs_data2[2][9] = "DIRECT"
						legs_data2[2][19] = 0
					end
					fpln_data2[fpln_num2][1] = legs_data2[2][1]
					fpln_data2[fpln_num2][2] = legs_data2[2][9]
					fpln_data2[fpln_num2][3] = legs_data2[2][16]
					fpln_data2[fpln_num2][4] = 2
					if legs_num2 > 2 then
						-- if legs_data2[3][31] == "HA" or legs_data2[3][31] == "HF" or legs_data2[3][31] == "HM" then
							-- fpln_data2[fpln_num2][5] = 2
						-- else
							fpln_data2[fpln_num2][5] = 1
						-- end
					else
						fpln_data2[fpln_num2][5] = 1
					end
				end
				compare_txt = legs_data2[2][9]
				if compare_txt == "DIRECT" then
					compare_txt = compare_txt .. "ZZZZZZZ"
				elseif compare_txt == "" then
					compare_txt = "ZZZZZZZ"
				end
				for ii = 3, legs_num2 do
					
					if legs_data2[ii][9] ~= compare_txt then
						-- if legs_data2[ii][31] ~= "HA" and legs_data2[ii][31] ~= "HF" and legs_data2[ii][31] ~= "HM" then
							fpln_num2 = fpln_num2 + 1
							fpln_data2[fpln_num2] = {true, true, true, true, true}
							if legs_data2[ii][1] == "DISCONTINUITY" then
								fpln_data2[fpln_num2][1] = ""
								fpln_data2[fpln_num2][2] = legs_data2[ii][9]
								fpln_data2[fpln_num2][3] = ""
								fpln_data2[fpln_num2][4] = ii
								fpln_data2[fpln_num2][5] = 1
							else
								if legs_data2[ii][19] == 3 or legs_data2[ii-1][1] == "DISCONTINUITY" then
									legs_data2[ii][9] = "DIRECT"
									if legs_data2[ii][19] == 3 then
										legs_data2[ii][19] = 0
									end
								end
								fpln_data2[fpln_num2][1] = legs_data2[ii][1]
								fpln_data2[fpln_num2][2] = legs_data2[ii][9]
								fpln_data2[fpln_num2][3] = legs_data2[ii][16]
								fpln_data2[fpln_num2][4] = ii
								if ii < legs_num2 then
									-- if legs_data2[ii+1][31] == "HA" or legs_data2[ii+1][31] == "HF" or legs_data2[ii][31+1] == "HM" then
										-- fpln_data2[fpln_num2][5] = 2
									-- else
										fpln_data2[fpln_num2][5] = 1
									-- end
								else
									fpln_data2[fpln_num2][5] = 1
								end
							end
						-- end
					else
						if legs_data2[ii][1] == "DISCONTINUITY" then
							fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
						else
							fpln_data2[fpln_num2][1] = legs_data2[ii][1]
							fpln_data2[fpln_num2][3] = legs_data2[ii][16]
							fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
						end
					end
					compare_txt = legs_data2[ii][9]
					if compare_txt == "DIRECT" then
						compare_txt = compare_txt .. "ZZZZZZZ"
					elseif compare_txt == "" then
						compare_txt = "ZZZZZZZ"
					end
					-- create hold data
					if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
						hold_data_num2 = hold_data_num2 + 1
						hold_data2[hold_data_num2] = ii
					end
				end
			else
				if legs_num2 > 3 then
					fpln_num2 = fpln_num2 + 1
					fpln_data2[fpln_num2] = {true, true, true, true, true}
					if legs_data2[3][1] == "DISCONTINUITY" then
						fpln_data2[fpln_num2][1] = ""
						fpln_data2[fpln_num2][2] = legs_data2[3][9]
						fpln_data2[fpln_num2][3] = ""
						fpln_data2[fpln_num2][4] = 3
						fpln_data2[fpln_num2][5] = 1
					else
						if legs_data2[3][19] == 3 then
							legs_data2[3][9] = "DIRECT"
							legs_data2[3][19] = 0
						end
						fpln_data2[fpln_num2][1] = legs_data2[3][1]
						fpln_data2[fpln_num2][2] = legs_data2[3][9]
						fpln_data2[fpln_num2][3] = legs_data2[3][16]
						fpln_data2[fpln_num2][4] = 3
						if legs_num2 > 3 then
							-- if legs_data2[4][31] == "HA" or legs_data2[4][31] == "HF" or legs_data2[4][31] == "HM" then
								-- fpln_data2[fpln_num2][5] = 2
							-- else
								fpln_data2[fpln_num2][5] = 1
							-- end
						else
							fpln_data2[fpln_num2][5] = 1
						end
					end
					compare_txt = legs_data2[3][9]
					if compare_txt == "DIRECT" then
						compare_txt = compare_txt .. "ZZZZZZZ"
					elseif compare_txt == "" then
						compare_txt = "ZZZZZZZ"
					end
					for ii = 4, legs_num2 do
						if legs_data2[ii][9] ~= compare_txt then
							-- if legs_data2[ii][31] ~= "HA" and legs_data2[ii][31] ~= "HF" and legs_data2[ii][31] ~= "HM" then
								fpln_num2 = fpln_num2 + 1
								fpln_data2[fpln_num2] = {true, true, true, true, true}
								if legs_data2[ii][1] == "DISCONTINUITY" then
									fpln_data2[fpln_num2][1] = ""
									fpln_data2[fpln_num2][2] = legs_data2[ii][9]
									fpln_data2[fpln_num2][3] = ""
									fpln_data2[fpln_num2][4] = ii
									fpln_data2[fpln_num2][5] = 1
								else
									if legs_data2[ii][19] == 3 or legs_data2[ii-1][1] == "DISCONTINUITY" then
										legs_data2[ii][9] = "DIRECT"
										if legs_data2[ii][19] == 3 then
											legs_data2[ii][19] = 0
										end
									end
									fpln_data2[fpln_num2][1] = legs_data2[ii][1]
									fpln_data2[fpln_num2][2] = legs_data2[ii][9]
									fpln_data2[fpln_num2][3] = legs_data2[ii][16]
									fpln_data2[fpln_num2][4] = ii
									if ii < legs_num2 then
										-- if legs_data2[ii+1][31] == "HA" or legs_data2[ii+1][31] == "HF" or legs_data2[ii+1][31] == "HM" then
											-- fpln_data2[fpln_num2][5] = 2
										-- else
											fpln_data2[fpln_num2][5] = 1
										-- end
									else
										fpln_data2[fpln_num2][5] = 1
									end
								end
							-- end
						else
							if legs_data2[ii][1] == "DISCONTINUITY" then
								fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
							else
								fpln_data2[fpln_num2][1] = legs_data2[ii][1]
								fpln_data2[fpln_num2][3] = legs_data2[ii][16]
								fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
							end
						end
						compare_txt = legs_data2[ii][9]
						if compare_txt == "DIRECT" then
							compare_txt = compare_txt .. "ZZZZZZZ"
						elseif compare_txt == "" then
							compare_txt = "ZZZZZZZ"
						end
						-- create hold data
						if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
							hold_data_num2 = hold_data_num2 + 1
							hold_data2[hold_data_num2] = ii
						end
					end
				else
					fpln_num2 = fpln_num2 + 1
					fpln_data2[fpln_num2] = {true, true, true, true, true}
					if legs_data2[3][1] == "DISCONTINUITY" then
						fpln_data2[fpln_num2][1] = ""
						fpln_data2[fpln_num2][2] = legs_data2[3][9]
						fpln_data2[fpln_num2][3] = ""
						fpln_data2[fpln_num2][4] = 3
						fpln_data2[fpln_num2][5] = 1
					else
						if legs_data2[3][19] == 3 then
							legs_data2[3][9] = "DIRECT"
							legs_data2[3][19] = 0
						end
						fpln_data2[fpln_num2][1] = legs_data2[3][1]
						fpln_data2[fpln_num2][2] = legs_data2[3][9]
						fpln_data2[fpln_num2][3] = legs_data2[3][16]
						fpln_data2[fpln_num2][4] = 3
						if ii < legs_num2 then
							-- if legs_data2[ii+1][31] == "HA" or legs_data2[ii+1][31] == "HF" or legs_data2[ii+1][31] == "HM" then
								-- fpln_data2[fpln_num2][5] = 2
							-- else
								fpln_data2[fpln_num2][5] = 1
							-- end
						else
							fpln_data2[fpln_num2][5] = 1
						end
					end
				end
			end
		end
	elseif in_rte == 1 then
		fpln_num4 = 0
		fpln_data4 = {}
		
		hold_data_num4 = 0
		hold_data4 = {}
		
		if legs_num8 == 2 then
			
			if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
				rw_disable = 1
			end
			
			if rw_disable == 0 then
				fpln_num4 = fpln_num4 + 1
				fpln_data4[fpln_num4] = {true, true, true, true, true}
				if legs_data8[2][1] == "DISCONTINUITY" then
					fpln_data4[fpln_num4][1] = ""
					fpln_data4[fpln_num4][2] = legs_data8[2][9]
					fpln_data4[fpln_num4][3] = ""
					fpln_data4[fpln_num4][4] = 2
					fpln_data4[fpln_num4][5] = 1
				else
					if legs_data8[2][19] == 3 then
						legs_data8[2][9] = "DIRECT"
						legs_data8[2][19] = 0
					end
					fpln_data4[fpln_num4][1] = legs_data8[2][1]
					fpln_data4[fpln_num4][2] = legs_data8[2][9]
					fpln_data4[fpln_num4][3] = legs_data8[2][16]
					fpln_data4[fpln_num4][4] = 2
					fpln_data4[fpln_num4][5] = 1
				end
			end
		elseif legs_num8 > 1 then
			
			if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
				rw_disable = 1
			end
			
			if rw_disable == 0 then
				fpln_num4 = fpln_num4 + 1
				fpln_data4[fpln_num4] = {true, true, true, true, true}
				if legs_data8[2][1] == "DISCONTINUITY" then
					fpln_data4[fpln_num4][1] = ""
					fpln_data4[fpln_num4][2] = legs_data8[2][9]
					fpln_data4[fpln_num4][3] = ""
					fpln_data4[fpln_num4][4] = 2
					fpln_data4[fpln_num4][5] = 1
				else
					if legs_data8[2][19] == 3 then
						legs_data8[2][9] = "DIRECT"
						legs_data8[2][19] = 0
					end
					fpln_data4[fpln_num4][1] = legs_data8[2][1]
					fpln_data4[fpln_num4][2] = legs_data8[2][9]
					fpln_data4[fpln_num4][3] = legs_data8[2][16]
					fpln_data4[fpln_num4][4] = 2
					if legs_num8 > 2 then
						-- if legs_data8[3][31] == "HA" or legs_data8[3][31] == "HF" or legs_data8[3][31] == "HM" then
							-- fpln_data4[fpln_num4][5] = 2
						-- else
							fpln_data4[fpln_num4][5] = 1
						-- end
					else
						fpln_data4[fpln_num4][5] = 1
					end
				end
				compare_txt = legs_data8[2][9]
				if compare_txt == "DIRECT" then
					compare_txt = compare_txt .. "ZZZZZZZ"
				elseif compare_txt == "" then
					compare_txt = "ZZZZZZZ"
				end
				for ii = 3, legs_num8 do
					if legs_data8[ii][9] ~= compare_txt then
						-- if legs_data8[ii][31] ~= "HA" and legs_data8[ii][31] ~= "HF" and legs_data8[ii][31] ~= "HM" then
							fpln_num4 = fpln_num4 + 1
							fpln_data4[fpln_num4] = {true, true, true, true, true}
							if legs_data8[ii][1] == "DISCONTINUITY" then
								fpln_data4[fpln_num4][1] = ""
								fpln_data4[fpln_num4][2] = legs_data8[ii][9]
								fpln_data4[fpln_num4][3] = ""
								fpln_data4[fpln_num4][4] = ii
								fpln_data4[fpln_num4][5] = 1
							else
								if legs_data8[ii][19] == 3 or legs_data8[ii-1][1] == "DISCONTINUITY" then
									legs_data8[ii][9] = "DIRECT"
									if legs_data8[ii][19] == 3 then
										legs_data8[ii][19] = 0
									end
								end
								fpln_data4[fpln_num4][1] = legs_data8[ii][1]
								fpln_data4[fpln_num4][2] = legs_data8[ii][9]
								fpln_data4[fpln_num4][3] = legs_data8[ii][16]
								fpln_data4[fpln_num4][4] = ii
								if ii < legs_num8 then
									-- if legs_data8[ii+1][31] == "HA" or legs_data8[ii+1][31] == "HF" or legs_data8[ii][31+1] == "HM" then
										-- fpln_data4[fpln_num4][5] = 2
									-- else
										fpln_data4[fpln_num4][5] = 1
									-- end
								else
									fpln_data4[fpln_num4][5] = 1
								end
							end
						-- end
					else
						if legs_data8[ii][1] == "DISCONTINUITY" then
							fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
						else
							fpln_data4[fpln_num4][1] = legs_data8[ii][1]
							fpln_data4[fpln_num4][3] = legs_data8[ii][16]
							fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
						end
					end
					compare_txt = legs_data8[ii][9]
					if compare_txt == "DIRECT" then
						compare_txt = compare_txt .. "ZZZZZZZ"
					elseif compare_txt == "" then
						compare_txt = "ZZZZZZZ"
					end
					-- create hold data
					if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
						hold_data_num4 = hold_data_num4 + 1
						hold_data4[hold_data_num4] = ii
					end
				end
			else
				if legs_num8 > 3 then
					fpln_num4 = fpln_num4 + 1
					fpln_data4[fpln_num4] = {true, true, true, true, true}
					if legs_data8[3][1] == "DISCONTINUITY" then
						fpln_data4[fpln_num4][1] = ""
						fpln_data4[fpln_num4][2] = legs_data8[3][9]
						fpln_data4[fpln_num4][3] = ""
						fpln_data4[fpln_num4][4] = 3
						fpln_data4[fpln_num4][5] = 1
					else
						if legs_data8[3][19] == 3 then
							legs_data8[3][9] = "DIRECT"
							legs_data8[3][19] = 0
						end
						fpln_data4[fpln_num4][1] = legs_data8[3][1]
						fpln_data4[fpln_num4][2] = legs_data8[3][9]
						fpln_data4[fpln_num4][3] = legs_data8[3][16]
						fpln_data4[fpln_num4][4] = 3
						if legs_num8 > 3 then
							-- if legs_data8[4][31] == "HA" or legs_data8[4][31] == "HF" or legs_data8[4][31] == "HM" then
								-- fpln_data4[fpln_num4][5] = 2
							-- else
								fpln_data4[fpln_num4][5] = 1
							-- end
						else
							fpln_data4[fpln_num4][5] = 1
						end
					end
					compare_txt = legs_data8[3][9]
					if compare_txt == "DIRECT" then
						compare_txt = compare_txt .. "ZZZZZZZ"
					elseif compare_txt == "" then
						compare_txt = "ZZZZZZZ"
					end
					for ii = 4, legs_num8 do
						if legs_data8[ii][9] ~= compare_txt then
							-- if legs_data8[ii][31] ~= "HA" and legs_data8[ii][31] ~= "HF" and legs_data8[ii][31] ~= "HM" then
								fpln_num4 = fpln_num4 + 1
								fpln_data4[fpln_num4] = {true, true, true, true, true}
								if legs_data8[ii][1] == "DISCONTINUITY" then
									fpln_data4[fpln_num4][1] = ""
									fpln_data4[fpln_num4][2] = legs_data8[ii][9]
									fpln_data4[fpln_num4][3] = ""
									fpln_data4[fpln_num4][4] = ii
									fpln_data4[fpln_num4][5] = 1
								else
									if legs_data8[ii][19] == 3 or legs_data8[ii-1][1] == "DISCONTINUITY" then
										legs_data8[ii][9] = "DIRECT"
										if legs_data8[ii][19] == 3 then
											legs_data8[ii][19] = 0
										end
									end
									fpln_data4[fpln_num4][1] = legs_data8[ii][1]
									fpln_data4[fpln_num4][2] = legs_data8[ii][9]
									fpln_data4[fpln_num4][3] = legs_data8[ii][16]
									fpln_data4[fpln_num4][4] = ii
									if ii < legs_num8 then
										-- if legs_data8[ii+1][31] == "HA" or legs_data8[ii+1][31] == "HF" or legs_data8[ii+1][31] == "HM" then
											-- fpln_data4[fpln_num4][5] = 2
										-- else
											fpln_data4[fpln_num4][5] = 1
										-- end
									else
										fpln_data4[fpln_num4][5] = 1
									end
								end
							-- end
						else
							if legs_data8[ii][1] == "DISCONTINUITY" then
								fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
							else
								fpln_data4[fpln_num4][1] = legs_data8[ii][1]
								fpln_data4[fpln_num4][3] = legs_data8[ii][16]
								fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
							end
						end
						compare_txt = legs_data8[ii][9]
						if compare_txt == "DIRECT" then
							compare_txt = compare_txt .. "ZZZZZZZ"
						elseif compare_txt == "" then
							compare_txt = "ZZZZZZZ"
						end
						-- create hold data
						if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
							hold_data_num4 = hold_data_num4 + 1
							hold_data4[hold_data_num4] = ii
						end
					end
				else
					fpln_num4 = fpln_num4 + 1
					fpln_data4[fpln_num4] = {true, true, true, true, true}
					if legs_data8[3][1] == "DISCONTINUITY" then
						fpln_data4[fpln_num4][1] = ""
						fpln_data4[fpln_num4][2] = legs_data8[3][9]
						fpln_data4[fpln_num4][3] = ""
						fpln_data4[fpln_num4][4] = 3
						fpln_data4[fpln_num4][5] = 1
					else
						if legs_data8[3][19] == 3 then
							legs_data8[3][9] = "DIRECT"
							legs_data8[3][19] = 0
						end
						fpln_data4[fpln_num4][1] = legs_data8[3][1]
						fpln_data4[fpln_num4][2] = legs_data8[3][9]
						fpln_data4[fpln_num4][3] = legs_data8[3][16]
						fpln_data4[fpln_num4][4] = 3
						if ii < legs_num8 then
							-- if legs_data8[ii+1][31] == "HA" or legs_data8[ii+1][31] == "HF" or legs_data8[ii+1][31] == "HM" then
								-- fpln_data4[fpln_num4][5] = 2
							-- else
								fpln_data4[fpln_num4][5] = 1
							-- end
						else
							fpln_data4[fpln_num4][5] = 1
						end
					end
				end
			end
		end
	end
end


function create_fpln(in_rte)

	local ii = 0
	local compare_txt = ""
	local rw_disable = 0
	local fpln_from = 0
	
	if in_rte == 0 then
		fpln_num2 = 0
		fpln_data2 = {}
		
		hold_data_num2 = 0
		hold_data2 = {}
		
		fpln_from = 2	--offset_mod
		
		if fpln_from > 0 and fpln_from <= legs_num2 and legs_num2 > 1 then
			
			if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 and fpln_from == 2 then
				fpln_from = fpln_from + 1
			end
			
			if fpln_from <= legs_num2 then
				fpln_num2 = fpln_num2 + 1
				fpln_data2[fpln_num2] = {true, true, true, true, true, true}
				if legs_data2[fpln_from][1] == "DISCONTINUITY" then
					fpln_data2[fpln_num2][1] = ""
					fpln_data2[fpln_num2][2] = legs_data2[fpln_from][9]
					fpln_data2[fpln_num2][3] = ""
					fpln_data2[fpln_num2][4] = fpln_from
					fpln_data2[fpln_num2][5] = 1
					fpln_data2[fpln_num2][6] = 0
				else
					if legs_data2[fpln_from][19] == 3 then
						legs_data2[fpln_from][9] = "DIRECT"
						legs_data2[fpln_from][19] = 0
					end
					fpln_data2[fpln_num2][1] = legs_data2[fpln_from][1]
					fpln_data2[fpln_num2][2] = legs_data2[fpln_from][9]
					fpln_data2[fpln_num2][3] = legs_data2[fpln_from][16]
					fpln_data2[fpln_num2][4] = fpln_from
					fpln_data2[fpln_num2][5] = 1
					if legs_data2[fpln_from][52] <= 0 and rte12_act == in_rte then
						fpln_data2[fpln_num2][6] = 1
					else
						fpln_data2[fpln_num2][6] = 0
					end
				end
				
				-- create hold data
				if legs_data2[fpln_from][31] == "HA" or legs_data2[fpln_from][31] == "HF" or legs_data2[fpln_from][31] == "HM" then
					hold_data_num2 = hold_data_num2 + 1
					hold_data2[hold_data_num2] = fpln_from
				end
				
				if fpln_from < legs_num2 then
					compare_txt = legs_data2[fpln_from][9]
					if compare_txt == "DIRECT" then
						compare_txt = compare_txt .. "ZZZZZZZ"
					elseif compare_txt == "" then
						compare_txt = "ZZZZZZZ"
					end
					for ii = fpln_from + 1, legs_num2 do
						
						if legs_data2[ii][9] ~= compare_txt then
							if legs_data2[ii][31] ~= "HA" and legs_data2[ii][31] ~= "HF" and legs_data2[ii][31] ~= "HM" then
								fpln_num2 = fpln_num2 + 1
								fpln_data2[fpln_num2] = {true, true, true, true, true, true}
								if legs_data2[ii][1] == "DISCONTINUITY" then
									fpln_data2[fpln_num2][1] = ""
									fpln_data2[fpln_num2][2] = legs_data2[ii][9]
									fpln_data2[fpln_num2][3] = ""
									fpln_data2[fpln_num2][4] = ii
									fpln_data2[fpln_num2][5] = 1
									fpln_data2[fpln_num2][6] = 0
								else
									if legs_data2[ii][19] == 3 or legs_data2[ii-1][1] == "DISCONTINUITY" then
										legs_data2[ii][9] = "DIRECT"
										if legs_data2[ii][19] == 3 then
											legs_data2[ii][19] = 0
										end
									end
									fpln_data2[fpln_num2][1] = legs_data2[ii][1]
									fpln_data2[fpln_num2][2] = legs_data2[ii][9]
									fpln_data2[fpln_num2][3] = legs_data2[ii][16]
									fpln_data2[fpln_num2][4] = ii
									fpln_data2[fpln_num2][5] = 1
									if legs_data2[ii][52] <= 0 and rte12_act == in_rte then
										fpln_data2[fpln_num2][6] = 1
									else
										fpln_data2[fpln_num2][6] = 0
									end
								end
							else
								fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
							end
						else
							if legs_data2[ii][1] == "DISCONTINUITY" then
								fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
							else
								fpln_data2[fpln_num2][1] = legs_data2[ii][1]
								fpln_data2[fpln_num2][3] = legs_data2[ii][16]
								fpln_data2[fpln_num2][5] = fpln_data2[fpln_num2][5] + 1
								if legs_data2[ii][52] <= 0 and rte12_act == in_rte then
									fpln_data2[fpln_num2][6] = 1
								end
							end
						end
						-- create hold data
						if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
							hold_data_num2 = hold_data_num2 + 1
							hold_data2[hold_data_num2] = ii
						else
							compare_txt = legs_data2[ii][9]
							if compare_txt == "DIRECT" then
								compare_txt = compare_txt .. "ZZZZZZZ"
							elseif compare_txt == "" then
								compare_txt = "ZZZZZZZ"
							end
						end
					end
				end
			end
			
		end
	else
		-- RTE2
		fpln_num4 = 0
		fpln_data4 = {}
		
		hold_data_num4 = 0
		hold_data4 = {}
		
		fpln_from = 2	--offset_mod2
		
		if fpln_from > 0 and fpln_from <= legs_num8 and legs_num8 > 1 then
			
			if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 and fpln_from == 2 then
				fpln_from = fpln_from + 1
			end
			
			if fpln_from <= legs_num8 then
				fpln_num4 = fpln_num4 + 1
				fpln_data4[fpln_num4] = {true, true, true, true, true, true}
				if legs_data8[fpln_from][1] == "DISCONTINUITY" then
					fpln_data4[fpln_num4][1] = ""
					fpln_data4[fpln_num4][2] = legs_data8[fpln_from][9]
					fpln_data4[fpln_num4][3] = ""
					fpln_data4[fpln_num4][4] = fpln_from
					fpln_data4[fpln_num4][5] = 1
					fpln_data4[fpln_num4][6] = 0
				else
					if legs_data8[fpln_from][19] == 3 then
						legs_data8[fpln_from][9] = "DIRECT"
						legs_data8[fpln_from][19] = 0
					end
					fpln_data4[fpln_num4][1] = legs_data8[fpln_from][1]
					fpln_data4[fpln_num4][2] = legs_data8[fpln_from][9]
					fpln_data4[fpln_num4][3] = legs_data8[fpln_from][16]
					fpln_data4[fpln_num4][4] = fpln_from
					fpln_data4[fpln_num4][5] = 1
					if legs_data8[fpln_from][52] <= 0 and rte12_act == in_rte then
						fpln_data4[fpln_num4][6] = 1
					else
						fpln_data4[fpln_num4][6] = 0
					end
				end
				
				-- create hold data
				if legs_data8[fpln_from][31] == "HA" or legs_data8[fpln_from][31] == "HF" or legs_data8[fpln_from][31] == "HM" then
					hold_data_num4 = hold_data_num4 + 1
					hold_data4[hold_data_num4] = fpln_from
				end
				
				if fpln_from < legs_num8 then
					compare_txt = legs_data8[fpln_from][9]
					if compare_txt == "DIRECT" then
						compare_txt = compare_txt .. "ZZZZZZZ"
					elseif compare_txt == "" then
						compare_txt = "ZZZZZZZ"
					end
					for ii = fpln_from + 1, legs_num8 do
						
						if legs_data8[ii][9] ~= compare_txt then
							if legs_data8[ii][31] ~= "HA" and legs_data8[ii][31] ~= "HF" and legs_data8[ii][31] ~= "HM" then
								fpln_num4 = fpln_num4 + 1
								fpln_data4[fpln_num4] = {true, true, true, true, true, true}
								if legs_data8[ii][1] == "DISCONTINUITY" then
									fpln_data4[fpln_num4][1] = ""
									fpln_data4[fpln_num4][2] = legs_data8[ii][9]
									fpln_data4[fpln_num4][3] = ""
									fpln_data4[fpln_num4][4] = ii
									fpln_data4[fpln_num4][5] = 1
									fpln_data4[fpln_num4][6] = 0
								else
									if legs_data8[ii][19] == 3 or legs_data8[ii-1][1] == "DISCONTINUITY" then
										legs_data8[ii][9] = "DIRECT"
										if legs_data8[ii][19] == 3 then
											legs_data8[ii][19] = 0
										end
									end
									fpln_data4[fpln_num4][1] = legs_data8[ii][1]
									fpln_data4[fpln_num4][2] = legs_data8[ii][9]
									fpln_data4[fpln_num4][3] = legs_data8[ii][16]
									fpln_data4[fpln_num4][4] = ii
									fpln_data4[fpln_num4][5] = 1
									if legs_data8[ii][52] <= 0 and rte12_act == in_rte then
										fpln_data4[fpln_num4][6] = 1
									else
										fpln_data4[fpln_num4][6] = 0
									end
								end
							else
								fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
								if legs_data8[ii][52] <= 0 and rte12_act == in_rte then
									fpln_data4[fpln_num4][6] = 1
								end
							end
						else
							if legs_data8[ii][1] == "DISCONTINUITY" then
								fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
							else
								fpln_data4[fpln_num4][1] = legs_data8[ii][1]
								fpln_data4[fpln_num4][3] = legs_data8[ii][16]
								fpln_data4[fpln_num4][5] = fpln_data4[fpln_num4][5] + 1
								if legs_data8[ii][52] <= 0 and rte12_act == in_rte then
									fpln_data4[fpln_num4][6] = 1
								end
							end
						end
						-- create hold data
						if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
							hold_data_num4 = hold_data_num4 + 1
							hold_data4[hold_data_num4] = ii
						else
							compare_txt = legs_data8[ii][9]
							if compare_txt == "DIRECT" then
								compare_txt = compare_txt .. "ZZZZZZZ"
							elseif compare_txt == "" then
								compare_txt = "ZZZZZZZ"
							end
						end
					end
				end
			end
			
		end
	end
end



function dump_fpln2()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		file_navdata2:write("FPLN:\n")
		if fpln_num2 > 0 then
			for vvv = 1, fpln_num2  do
--				if awy_temp2[vvv][1] == "R232" then
					fms_line = fpln_data2[vvv][1] .. "," .. fpln_data2[vvv][2] .. "," .. fpln_data2[vvv][3] .. "," 
					fms_line = fms_line .. tostring(fpln_data2[vvv][4]) .. "," .. tostring(fpln_data2[vvv][5]) .. "\n"
					file_navdata2:write(fms_line)
--				end
			end
		else
			file_navdata2:write("none data\n")
		end
		file_navdata2:write("HOLD:\n")
		if hold_data_num2 > 0 then
			for vvv = 1, hold_data_num2  do
				fms_line = ">" .. tostring(hold_data2[vvv]) .. "\n"
				file_navdata2:write(fms_line)
			end
		else
			file_navdata2:write("none data\n")
		end
		file_navdata2:close()
	end
end


function dump_awy()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if awy_temp_num2 > 0 then
			fms_line = "awy_temps2:\n"
			file_navdata2:write(fms_line)
			for vvv = 1, awy_temp_num2  do
--				if awy_temp2[vvv][1] == "R232" then
					fms_line = awy_temp2[vvv][1] .. "," .. awy_temp2[vvv][2] .. "," .. awy_temp2[vvv][4] .. "," .. awy_temp2[vvv][3] .. "," .. awy_temp2[vvv][5] .. "," .. awy_temp2[vvv][6] .. "\n"
					file_navdata2:write(fms_line)
--				end
			end
		end
		file_navdata2:close()
	end
end

function dump_awy2()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if awy_path_num > 0 then
			fms_line = "awy_path:\n"
			file_navdata2:write(fms_line)
			for vvv = 1, awy_path_num  do
				fms_line = awy_path[vvv][1] .. "/" .. awy_path[vvv][2] .. "\n"
				--fms_line = awy_path[vvv] ..  "\n"
				file_navdata2:write(fms_line)
			end
		else
			fms_line = "NOT FOUND" .. "\n"
			file_navdata2:write(fms_line)
		end
		file_navdata2:close()
	end
end

function find_ref_rnw_data(in_mod, in_rte)

	local ii = 0
	local jj = 0
	local kk = 0
	local rnw_temp0 = ""
	local nd_x = 0
	local idx_rec = 0
	
	local ref_icao_tmp = ""
	local ref_rwy2_tmp = ""
	
	local ref_runway_lenght_tmp = 0
	local ref_runway_lat_tmp = 0
	local ref_runway_lon_tmp = 0
	local ref_runway_crs_tmp = 0
	local ref_runway_old_name_tmp = ""
	
	if in_mod == 0 then
		ref_runway_lenght = 0
		ref_runway_lat = 0
		ref_runway_lon = 0
		ref_runway_crs = 0
		ref_runway_old_name = ""
		
		-- if in_rte == 0 then
		if rte12_act == 0 then
			ref_icao_tmp = ref_icao
			ref_rwy2_tmp = ref_rwy
		elseif rte12_act == 1 then
			ref_icao_tmp = ref_icao7
			ref_rwy2_tmp = ref_rwy7
		end
	else
		
		if in_rte == 0 then
			ref_rwy2_tmp = ref_rwy2
			ref_icao_tmp = ref_icao_x
			ref_runway_lenght_mod = 0
			ref_runway_lat_mod = 0
			ref_runway_lon_mod = 0
			ref_runway_crs_mod = 0
			ref_runway_old_name_mod = ""
		elseif in_rte == 1 then
			ref_rwy2_tmp = ref_rwy8
			ref_icao_tmp = ref_icao7_x
			ref_runway_lenght_mod2 = 0
			ref_runway_lat_mod2 = 0
			ref_runway_lon_mod2 = 0
			ref_runway_crs_mod2 = 0
			ref_runway_old_name_mod2 = ""
		end
	end
	
	if string.sub(ref_rwy2_tmp, -1, -1) == " " then
		rnw_temp0 = string.sub(ref_rwy2_tmp, 1, -2)
	else
		rnw_temp0 = ref_rwy2_tmp
	end
	
	if rnw_data_num > 0 and string.len(ref_icao_tmp) > 0 then
		nd_x = string.byte(string.sub(ref_icao_tmp, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if ref_icao_tmp == rnw_data[idx_rec][1] and rnw_temp0 == rnw_data[idx_rec][2] then
						ref_runway_lenght_tmp = rnw_data[idx_rec][7]
						ref_runway_lat_tmp = rnw_data[idx_rec][3]
						ref_runway_lon_tmp = rnw_data[idx_rec][4]
						ref_runway_crs_tmp = rnw_data[idx_rec][8]
						ref_runway_old_name_tmp = rnw_data[idx_rec][10]
						break
					end
				end
			end
		end
	end
	
	if in_mod == 0 then
		ref_runway_lenght = ref_runway_lenght_tmp
		ref_runway_lat = ref_runway_lat_tmp
		ref_runway_lon = ref_runway_lon_tmp
		ref_runway_crs = ref_runway_crs_tmp
		ref_runway_old_name = ref_runway_old_name_tmp
	else
		if in_rte == 0 then
			ref_runway_lenght_mod = ref_runway_lenght_tmp
			ref_runway_lat_mod = ref_runway_lat_tmp
			ref_runway_lon_mod = ref_runway_lon_tmp
			ref_runway_crs_mod = ref_runway_crs_tmp
			ref_runway_old_name_mod = ref_runway_old_name_tmp
		else
			ref_runway_lenght_mod2 = ref_runway_lenght_tmp
			ref_runway_lat_mod2 = ref_runway_lat_tmp
			ref_runway_lon_mod2 = ref_runway_lon_tmp
			ref_runway_crs_mod2 = ref_runway_crs_tmp
			ref_runway_old_name_mod2 = ref_runway_old_name_tmp
		end
	end
end

function find_des_rnw_data(in_mod, in_rte)

	local ii = 0
	local jj = 0
	local kk = 0
	local rnw_temp0 = ""
	local nd_x = 0
	local idx_rec = 0
	
	local des_icao_tmp = ""
	local des_rnw_tmp = ""
	
	local des_runway_lenght_tmp = 0
	local des_runway_lat_tmp = 0
	local des_runway_lon_tmp = 0
	local des_runway_crs_tmp = 0
	local des_runway_old_name_tmp = ""
	local des_runway_end_lat_tmp = 0
	local des_runway_end_lon_tmp = 0
	
	if in_mod == 0 then
		-- if in_rte == 0 then
		if rte12_act == 0 then
			des_rnw = ""
			des_runway_lenght = 0
			des_runway_lat = 0
			des_runway_lon = 0
			des_runway_crs = 0
			des_runway_old_name = ""
			
			if arr_data == 0 then
				des_icao_tmp = des_icao
			else
				des_icao_tmp = ref_icao
			end
		
			if string.sub(des_app, 1, 2) == "RW" then
				des_rnw = string.sub(des_app, 3, -1)
			else
				if string.len(des_app) > 4 then
					jj, kk = string.find(des_app, "-")
					if jj == nil then
						des_rnw = string.sub(des_app, 2, -2)
					else
						des_rnw = string.sub(des_app, 2, jj-1)
					end
				else
					des_rnw = string.sub(des_app, 2, -1)
				end
			end
			des_rnw_tmp = des_rnw
		elseif rte12_act == 1 then
			des_rnw7 = ""
			des_runway_lenght = 0
			des_runway_lat = 0
			des_runway_lon = 0
			des_runway_crs = 0
			des_runway_old_name = ""
			
			if arr_data7 == 0 then
				des_icao_tmp = des_icao7
			else
				des_icao_tmp = ref_icao7
			end
		
			if string.sub(des_app7, 1, 2) == "RW" then
				des_rnw7 = string.sub(des_app7, 3, -1)
			else
				if string.len(des_app7) > 4 then
					jj, kk = string.find(des_app7, "-")
					if jj == nil then
						des_rnw7 = string.sub(des_app7, 2, -2)
					else
						des_rnw7 = string.sub(des_app7, 2, jj-1)
					end
				else
					des_rnw7 = string.sub(des_app7, 2, -1)
				end
			end
			des_rnw_tmp = des_rnw7
		end
	else
		if in_rte == 0 then
			des_rnw2 = ""
			des_runway_lenght_mod = 0
			des_runway_lat_mod = 0
			des_runway_lon_mod = 0
			des_runway_crs_mod = 0
			des_runway_old_name_mod = ""
			des_runway_end_lat_mod = 0
			des_runway_end_lon_mod = 0
			
			if arr_data_x == 0 then
				des_icao_tmp = des_icao_x
			else
				des_icao_tmp = ref_icao_x
			end
			
			if string.sub(des_app2, 1, 2) == "RW" then
				des_rnw2 = string.sub(des_app2, 3, -1)
			else
				if string.len(des_app2) > 4 then
					jj, kk = string.find(des_app2, "-")
					if jj == nil then
						des_rnw2 = string.sub(des_app2, 2, -2)
					else
						des_rnw2 = string.sub(des_app2, 2, jj-1)
					end
				else
					des_rnw2 = string.sub(des_app2, 2, -1)
				end
			end
			des_rnw_tmp = des_rnw2
		elseif in_rte == 1 then
			des_rnw8 = ""
			des_runway_lenght_mod2 = 0
			des_runway_lat_mod2 = 0
			des_runway_lon_mod2 = 0
			des_runway_crs_mod2 = 0
			des_runway_old_name_mod2 = ""
			des_runway_end_lat_mod2 = 0
			des_runway_end_lon_mod2 = 0
			
			if arr_data7_x == 0 then
				des_icao_tmp = des_icao7_x
			else
				des_icao_tmp = ref_icao7_x
			end
			
			if string.sub(des_app8, 1, 2) == "RW" then
				des_rnw8 = string.sub(des_app8, 3, -1)
			else
				if string.len(des_app8) > 4 then
					jj, kk = string.find(des_app8, "-")
					if jj == nil then
						des_rnw8 = string.sub(des_app8, 2, -2)
					else
						des_rnw8 = string.sub(des_app8, 2, jj-1)
					end
				else
					des_rnw8 = string.sub(des_app8, 2, -1)
				end
			end
			des_rnw_tmp = des_rnw8
		end
	end
	
	
	if rnw_data_num > 0 and string.len(des_icao_tmp) > 0 then
		nd_x = string.byte(string.sub(des_icao_tmp, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if des_icao_tmp == rnw_data[idx_rec][1] and des_rnw_tmp == rnw_data[idx_rec][2] then
						des_runway_lenght_tmp = rnw_data[idx_rec][7]
						des_runway_lat_tmp = rnw_data[idx_rec][3]
						des_runway_lon_tmp = rnw_data[idx_rec][4]
						des_runway_crs_tmp = rnw_data[idx_rec][8]
						des_runway_old_name_tmp = rnw_data[idx_rec][10]
						des_runway_end_lat_tmp = rnw_data[idx_rec][5]
						des_runway_end_lon_tmp = rnw_data[idx_rec][6]
						break
					end
				end
			end
		end
	end
	
	if in_mod == 0 then
		des_runway_lenght = des_runway_lenght_tmp
		des_runway_lat = des_runway_lat_tmp
		des_runway_lon = des_runway_lon_tmp
		des_runway_crs = des_runway_crs_tmp
		des_runway_old_name = des_runway_old_name_tmp
		
		des_runway_lat_nd = des_runway_lat
		des_runway_lon_nd = des_runway_lon
		des_runway_crs_nd = (des_runway_crs + mag_variation_deg(des_runway_lat, des_runway_lon) + 360) % 360
		des_runway_lenght_nd = des_runway_lenght
		-- if in_rte == 0 then
		if rte12_act == 0 then
			des_rnw_nd = des_rnw
		elseif rte12_act == 1 then
			des_rnw_nd = des_rnw7
		end
	else
		if in_rte == 0 then
			des_rnw_nd_mod = des_rnw2
			des_runway_lenght_mod = des_runway_lenght_tmp
			des_runway_lat_mod = des_runway_lat_tmp
			des_runway_lon_mod = des_runway_lon_tmp
			des_runway_crs_mod = des_runway_crs_tmp
			des_runway_old_name_mod = des_runway_old_name_tmp
			des_runway_end_lat_mod = des_runway_end_lat_tmp
			des_runway_end_lon_mod = des_runway_end_lon_tmp
			
			des_runway_lat_nd_mod = des_runway_lat_mod
			des_runway_lon_nd_mod = des_runway_lon_mod
			des_runway_crs_nd_mod = (des_runway_crs_mod + mag_variation_deg(des_runway_lat_mod, des_runway_lon_mod) + 360) % 360
			des_runway_lenght_nd_mod = des_runway_lenght_mod
		elseif in_rte == 1 then
			des_rnw_nd_mod2 = des_rnw8
			des_runway_lenght_mod2 = des_runway_lenght_tmp
			des_runway_lat_mod2 = des_runway_lat_tmp
			des_runway_lon_mod2 = des_runway_lon_tmp
			des_runway_crs_mod2 = des_runway_crs_tmp
			des_runway_old_name_mod2 = des_runway_old_name_tmp
			des_runway_end_lat_mod2 = des_runway_end_lat_tmp
			des_runway_end_lon_mod2 = des_runway_end_lon_tmp
			
			des_runway_lat_nd_mod2 = des_runway_lat_mod2
			des_runway_lon_nd_mod2 = des_runway_lon_mod2
			des_runway_crs_nd_mod2 = (des_runway_crs_mod2 + mag_variation_deg(des_runway_lat_mod2, des_runway_lon_mod2) + 360) % 360
			des_runway_lenght_nd_mod2 = des_runway_lenght_mod2
		end
	end
end


function find_id_app_data(in_rte)
	local ii = 0
	local jj = 0
	local kk = 0
	local nd_x = 0
	local idx_rec = 0
	local find_ok = 0
	local skip_id = 0
	local app_id = ""
	local app_id_tmp = ""
	
	-- ILS
	ils_id = ""
	ils_freq = 0
	ils_course = 0
	
	lda_id = ""
	lda_freq = 0
	lda_course = 0
	
	local ils_find_runway = ""
	local des_icao_tmp = ""
	local ils_lda = 0
	
	if in_rte == 0 then
		ils_find_runway = des_runway_old_name
		if ils_find_runway == "" then
			ils_find_runway = des_rnw
		end
		des_icao_tmp = des_icao
		if arr_data ~= 0 then
			des_icao_tmp = ref_icao
		end
		if string.sub(des_app, 1, 1) == "I" or string.sub(des_app, 1, 1) == "L" then 	-- I/L = ILS
			ils_lda = 1
		elseif string.sub(des_app, 1, 1) == "X" then 	-- X = LDA
			ils_lda = 2
		end
	elseif in_rte == 1 then
		ils_find_runway = des_runway_old_name
		if ils_find_runway == "" then
			ils_find_runway = des_rnw7
		end
		des_icao_tmp = des_icao7
		if arr_data7 ~= 0 then
			des_icao_tmp = ref_icao7
		end
		if string.sub(des_app7, 1, 1) == "I" or string.sub(des_app7, 1, 1) == "L" then 	-- I/L = ILS
			ils_lda = 1
		elseif string.sub(des_app7, 1, 1) == "X" then 	-- X = LDA
			ils_lda = 2
		end
	end
	
	if ils_lda ~= 0 then
		if ils_nav_num > 0 and string.len(des_icao_tmp) > 0 then
			
			-- find approach ils/loc/lda id
			if legs_num > 1 and offset <= legs_num and offset > 0 then
				for ii = offset, legs_num do
					app_id_tmp = string.sub(legs_data[ii][9], 1, 3)
					if app_id_tmp == "ILS" or app_id_tmp == "LOC" or app_id_tmp == "LDA" then
						if string.sub(legs_data[ii][1], 1, 2) == "RW" then
							app_id = legs_data[ii][27]
						end
					end
				end
			end
			
			nd_x = string.byte(string.sub(des_icao_tmp, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_ils_icao[nd_x][99999] > 0 then
					for ii = 1, idx_ils_icao[nd_x][99999] do
						idx_rec = idx_ils_icao[nd_x][ii]
						if des_icao_tmp == ils_nav[idx_rec][7] and ils_find_runway == ils_nav[idx_rec][4] then
							if ils_nav[idx_rec][9] < 3 and ils_lda == 1 then
								ils_id = ils_nav[idx_rec][1]
								ils_freq = ils_nav[idx_rec][6]
								if ils_nav[idx_rec][10] < 0 then
									ils_course = (ils_nav[idx_rec][5] + mag_variation_deg(ils_nav[idx_rec][2], ils_nav[idx_rec][3]) + 360) % 360
								else
									ils_course = ils_nav[idx_rec][10]
								end
								if app_id == "" or app_id == ils_id then
									break
								end
							elseif ils_nav[idx_rec][9] == 3 and ils_lda == 2 then
								lda_id = ils_nav[idx_rec][1]
								lda_freq = ils_nav[idx_rec][6]
								if ils_nav[idx_rec][10] < 0 then
									lda_course = (ils_nav[idx_rec][5] + mag_variation_deg(ils_nav[idx_rec][2], ils_nav[idx_rec][3]) + 360) % 360
								else
									lda_course = ils_nav[idx_rec][10]
								end
								if app_id == "" or app_id == lda_id then
									break
								end
							end
							-- break
						end
					end
				end
			end
		end
	end
	
	-- GLS
	local app_id_name = ""
	local des_rnw_tgt = ""
	if in_rte == 0 then
		if string.sub(des_app, 1, 1) == "J" then 	-- GLS
			if rnav_idx_last ~= 0 then
				app_id_name = legs_data[rnav_idx_last][27]
			end
		elseif string.sub(des_app, 1, 1) == "R" then 	-- GLS
			-- convert approach to id approach (WAAS, EGNOS,MSAS)
			app_id_name = des_app
		else
			skip_id = 1
		end
		des_rnw_tgt = des_rnw
	elseif in_rte == 1 then
		if string.sub(des_app7, 1, 1) == "J" then 	-- GLS
			if rnav_idx_last ~= 0 then
				app_id_name = legs_data[rnav_idx_last][27]
			end
		elseif string.sub(des_app7, 1, 1) == "R" then 	-- GLS
			-- convert approach to id approach (WAAS, EGNOS,MSAS)
			app_id_name = des_app7
		else
			skip_id = 1
		end
		des_rnw_tgt = des_rnw7
	end
	gls_id = ""
	gls_channel = 0
	gls_course = 0
	
	if skip_id == 0 then
		if gls_nav_num > 0 then
			nd_x = string.byte(string.sub(des_icao_tmp, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_gls_icao[nd_x][99999] > 0 then
					for ii = 1, idx_gls_icao[nd_x][99999] do
						idx_rec = idx_gls_icao[nd_x][ii]
						if app_id_name == "" or rnav_idx_last == 0 then
							if des_icao_tmp == gls_nav[idx_rec][7] and des_rnw_tgt == gls_nav[idx_rec][4] then
								find_ok = 1
							end
						else
							if des_icao_tmp == gls_nav[idx_rec][7] and des_rnw_tgt == gls_nav[idx_rec][4] and app_id_name == gls_nav[idx_rec][1] then
								find_ok = 1
							end
						end
						if find_ok ~= 0 then
							gls_id = gls_nav[idx_rec][1]
							gls_channel = gls_nav[idx_rec][6]
							gls_course = (gls_nav[idx_rec][5] + mag_variation_deg(gls_nav[idx_rec][2], gls_nav[idx_rec][3]) + 360) % 360
							break
						end
					end
				end
			end
		end
	end
end

function find_hold_data(in_id, in_rc)
	local ii = 0
	local nd_x = 0
	local idx_rec = 0
	
	local hold_record = -1
	
	if hold_nav_num > 0 then
		nd_x = string.byte(string.sub(in_id, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_hold[nd_x][99999] > 0 then
				for ii = 1, idx_hold[nd_x][99999] do
					idx_rec = idx_hold[nd_x][ii]
					
					if in_id == hold_nav[idx_rec][1] and in_rc == hold_nav[idx_rec][2] then
						hold_record = idx_rec
						break
					end
				end
			end
		end
	end
	
	return hold_record
end

function ref_rnw_length_crs(in_rte)
	
	local res_lenght = 0
	local res_course = 0
	local res_start_lat = 0
	local res_start_lon = 0
	local res_end_lat = 0
	local res_end_lon = 0
	
	local ii = 0
	local jj = 0
	local kk = 0
	local nd_x = 0
	local idx_rec = 0
	
	local ref_icao_tmp = ""
	
	local rnw_txt_tmp = ""
	local ref_rwy2_tmp = ""
	
	if in_rte == 0 then
		ref_icao_tmp = ref_icao
		ref_rwy2_tmp = ref_rwy
	elseif in_rte == 1 then
		ref_icao_tmp = ref_icao7
		ref_rwy2_tmp = ref_rwy7
	end
	
	if string.sub(ref_rwy2_tmp, -1, -1) == " " then
		rnw_txt_tmp = string.sub(ref_rwy2_tmp, 1, -2)
	else
		rnw_txt_tmp = ref_rwy2_tmp
	end
	
	if rnw_data_num > 0 and string.len(ref_icao_tmp) > 0 then
		nd_x = string.byte(string.sub(ref_icao_tmp, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if ref_icao_tmp == rnw_data[idx_rec][1] and rnw_txt_tmp == rnw_data[idx_rec][2] then
						res_lenght = rnw_data[idx_rec][7]
						res_course = (rnw_data[idx_rec][8] + mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4]) + 360) % 360
						res_start_lat = rnw_data[idx_rec][3]
						res_start_lon = rnw_data[idx_rec][4]
						res_end_lat = rnw_data[idx_rec][5]
						res_end_lon = rnw_data[idx_rec][6]
						break
					end
				end
			end
		end
	end
	
	B738DR_ref_runway_len = res_lenght
	B738DR_ref_runway_crs = res_course
	B738DR_ref_runway_start_lat = res_start_lat
	B738DR_ref_runway_start_lon = res_start_lon
	B738DR_ref_runway_end_lat = res_end_lat
	B738DR_ref_runway_end_lon = res_end_lon
end

function ref_rnw_length_crs2(in_rte)
	
	local res_lenght = 0
	local res_course = 0
	local res_start_lat = 0
	local res_start_lon = 0
	local res_end_lat = 0
	local res_end_lon = 0
	
	local ii = 0
	local jj = 0
	local kk = 0
	local nd_x = 0
	local idx_rec = 0
	
	local ref_icao_tmp = ""
	
	local rnw_txt_tmp = ""
	local ref_rwy2_tmp = ""
	
	if in_rte == 0 then
		ref_icao_tmp = ref_icao_x
		ref_rwy2_tmp = ref_rwy2
	elseif in_rte == 1 then
		ref_icao_tmp = ref_icao7_x
		ref_rwy2_tmp = ref_rwy8
	end
	
		if ref_rwy2 == "-----" then
			ref_rwy2_tmp = ref_rwy3
		end
	if string.sub(ref_rwy2_tmp, -1, -1) == " " then
		rnw_txt_tmp = string.sub(ref_rwy2_tmp, 1, -2)
	else
		rnw_txt_tmp = ref_rwy2_tmp
	end
	
	if rnw_data_num > 0 and string.len(ref_icao_tmp) > 0 then
		nd_x = string.byte(string.sub(ref_icao_tmp, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if ref_icao_tmp == rnw_data[idx_rec][1] and rnw_txt_tmp == rnw_data[idx_rec][2] then
						res_lenght = rnw_data[idx_rec][7]
						res_course = (rnw_data[idx_rec][8] + mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4]) + 360) % 360
						res_start_lat = rnw_data[idx_rec][3]
						res_start_lon = rnw_data[idx_rec][4]
						res_end_lat = rnw_data[idx_rec][5]
						res_end_lon = rnw_data[idx_rec][6]
						break
					end
				end
			end
		end
	end
	
	if in_rte == 0 then
		B738DR_ref_runway_len_mod = res_lenght
		B738DR_ref_runway_crs_mod = res_course
		B738DR_ref_runway_start_lat_mod = res_start_lat
		B738DR_ref_runway_start_lon_mod = res_start_lon
		B738DR_ref_runway_end_lat_mod = res_end_lat
		B738DR_ref_runway_end_lon_mod = res_end_lon
	else
		B738DR_ref_runway_len_mod2 = res_lenght
		B738DR_ref_runway_crs_mod2 = res_course
		B738DR_ref_runway_start_lat_mod2 = res_start_lat
		B738DR_ref_runway_start_lon_mod2 = res_start_lon
		B738DR_ref_runway_end_lat_mod2 = res_end_lat
		B738DR_ref_runway_end_lon_mod2 = res_end_lon
	end
end

function des_rnw_length_crs(in_rte)
	
	local rnw_txt_tmp = ""
	local res_lenght = 0
	local res_course = 0
	local res_start_lat = 0
	local res_start_lon = 0
	local res_end_lat = 0
	local res_end_lon = 0
	
	local ii = 0
	local jj = 0
	local kk = 0
	local nd_x = 0
	local idx_rec = 0
	
	local des_icao_tmp = ""
	local des_app_tmp = ""
	
	if in_rte == 0 then
		des_icao_tmp = des_icao
		if arr_data ~= 0 then
			des_icao_tmp = ref_icao
		end
		des_app_tmp = des_app
	elseif in_rte == 1 then
		des_icao_tmp = des_icao7
		if arr_data7 ~= 0 then
			des_icao_tmp = ref_icao7
		end
		des_app_tmp = des_app7
	end
	
	if string.sub(des_app_tmp, 1, 2) == "RW" then
		rnw_txt_tmp = string.sub(des_app_tmp, 3, -1)
	else
		if string.len(des_app_tmp) > 4 then
			jj, kk = string.find(des_app_tmp, "-")
			if jj == nil then
				rnw_txt_tmp = string.sub(des_app_tmp, 2, -2)
			else
				rnw_txt_tmp = string.sub(des_app_tmp, 2, jj-1)
			end
		else
			rnw_txt_tmp = string.sub(des_app_tmp, 2, -1)
		end
	end
	
	if rnw_data_num > 0 and string.len(des_icao_tmp) > 0 then
		nd_x = string.byte(string.sub(des_icao_tmp, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if des_icao_tmp == rnw_data[idx_rec][1] and rnw_txt_tmp == rnw_data[idx_rec][2] then
						res_lenght = rnw_data[idx_rec][7]
						res_course = (rnw_data[idx_rec][8] + mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4]) + 360) % 360
						res_start_lat = rnw_data[idx_rec][3]
						res_start_lon = rnw_data[idx_rec][4]
						res_end_lat = rnw_data[idx_rec][5]
						res_end_lon = rnw_data[idx_rec][6]
						break
					end
				end
			end
		end
	end
	
	B738DR_dest_runway_len = res_lenght
	B738DR_dest_runway_crs = res_course
	B738DR_dest_runway_start_lat = res_start_lat
	B738DR_dest_runway_start_lon = res_start_lon
	B738DR_dest_runway_end_lat = res_end_lat
	B738DR_dest_runway_end_lon = res_end_lon
	if res_lenght ~= 0 then
		if string.len(rnw_txt_tmp) > 2 then
			if string.sub(rnw_txt_tmp, -1, -1) == " " then
				B738DR_dest_runway_id = string.sub(rnw_txt_tmp, 1, -2)
			else
				B738DR_dest_runway_id = rnw_txt_tmp
			end
		else
			B738DR_dest_runway_id = rnw_txt_tmp
		end
	else
		B738DR_dest_runway_id = ""
	end
	
end

function des_rnw_length_crs2(in_rte)
	
	local rnw_txt_tmp = ""
	local res_lenght = 0
	local res_course = 0
	local res_start_lat = 0
	local res_start_lon = 0
	local res_end_lat = 0
	local res_end_lon = 0
	
	local ii = 0
	local jj = 0
	local kk = 0
	local nd_x = 0
	local idx_rec = 0
	
	local des_icao_tmp = ""
	local des_app_tmp = ""
	
	if in_rte == 0 then
		des_icao_tmp = des_icao_x
		if arr_data_x ~= 0 then
			des_icao_tmp = ref_icao_x
		end
		des_app_tmp = des_app2
	elseif in_rte == 1 then
		des_icao_tmp = des_icao7_x
		if arr_data7_x ~= 0 then
			des_icao_tmp = ref_icao7_x
		end
		des_app_tmp = des_app8
	end
	
	if string.sub(des_app_tmp, 1, 2) == "RW" then
		rnw_txt_tmp = string.sub(des_app_tmp, 3, -1)
	else
		if string.len(des_app_tmp) > 4 then
			jj, kk = string.find(des_app_tmp, "-")
			if jj == nil then
				rnw_txt_tmp = string.sub(des_app_tmp, 2, -2)
			else
				rnw_txt_tmp = string.sub(des_app_tmp, 2, jj-1)
			end
		else
			rnw_txt_tmp = string.sub(des_app_tmp, 2, -1)
		end
	end
	
	if rnw_data_num > 0 and string.len(des_icao_tmp) > 0 then
		nd_x = string.byte(string.sub(des_icao_tmp, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if des_icao_tmp == rnw_data[idx_rec][1] and rnw_txt_tmp == rnw_data[idx_rec][2] then
						res_lenght = rnw_data[idx_rec][7]
						res_course = (rnw_data[idx_rec][8] + mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4]) + 360) % 360
						res_start_lat = rnw_data[idx_rec][3]
						res_start_lon = rnw_data[idx_rec][4]
						res_end_lat = rnw_data[idx_rec][5]
						res_end_lon = rnw_data[idx_rec][6]
						break
					end
				end
			end
		end
	end
	
	if in_rte == 0 then
		B738DR_dest_runway_len_mod = res_lenght
		B738DR_dest_runway_crs_mod = res_course
		B738DR_dest_runway_start_lat_mod = res_start_lat
		B738DR_dest_runway_start_lon_mod = res_start_lon
		B738DR_dest_runway_end_lat_mod = res_end_lat
		B738DR_dest_runway_end_lon_mod = res_end_lon
	else
		B738DR_dest_runway_len_mod2 = res_lenght
		B738DR_dest_runway_crs_mod2 = res_course
		B738DR_dest_runway_start_lat_mod2 = res_start_lat
		B738DR_dest_runway_start_lon_mod2 = res_start_lon
		B738DR_dest_runway_end_lat_mod2 = res_end_lat
		B738DR_dest_runway_end_lon_mod2 = res_end_lon
	end
end


function find_des_runway(entry_in, in_rte)
	
	local ii = 0
	local nd_x = 0
	local idx_rec = 0
	local result_lat = 0
	local result_lon = 0
	local fix_rnw = ""
	local result_crs = 0
	local result_num = ""
	local find_icao = ""
	
	if in_rte == 0 then
		find_icao = des_icao_x
	elseif in_rte == 1 then
		find_icao = des_icao7_x
	end
	
	if string.len(entry_in) > 3 then
		fix_rnw = string.sub(entry_in, 3, -1)
	end
	
	if rnw_data_num > 0 and string.len(find_icao) > 0 then
		nd_x = string.byte(string.sub(find_icao, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if find_icao == rnw_data[idx_rec][1] and fix_rnw == rnw_data[idx_rec][2] then
						result_lat = rnw_data[idx_rec][3]
						result_lon = rnw_data[idx_rec][4]
						result_crs = rnw_data[idx_rec][8]
						result_num = rnw_data[idx_rec][2]
						break
					end
				end
			end
		end
	end
	
	return result_lat, result_lon, result_crs, result_num
end

function find_ref_runway(entry_in, in_rte)
	
	local ii = 0
	local nd_x = 0
	local idx_rec = 0
	local result_lat = 0
	local result_lon = 0
	local fix_rnw = ""
	local result_crs = 0
	local result_num = ""
	local find_icao = ""
	
	if in_rte == 0 then
		find_icao = ref_icao_x
	elseif in_rte == 1 then
		find_icao = ref_icao7_x
	end
	
	if string.len(entry_in) > 3 then
		fix_rnw = string.sub(entry_in, 3, -1)
	end
	
	if rnw_data_num > 0 and string.len(find_icao) > 0 then
		nd_x = string.byte(string.sub(find_icao, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if find_icao == rnw_data[idx_rec][1] and fix_rnw == rnw_data[idx_rec][2] then
						result_lat = rnw_data[idx_rec][3]
						result_lon = rnw_data[idx_rec][4]
						result_crs = rnw_data[idx_rec][8]
						result_num = rnw_data[idx_rec][2]
						break
					end
				end
			end
		end
	end
	
	return result_lat, result_lon, result_crs, result_num
end


function dump_rnw(rrr_icao)
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if rnw_data_num > 0 then
			for vvv = 1, rnw_data_num  do
				if rnw_data[vvv][1] == rrr_icao then
					fms_line = rnw_data[vvv][1] .. "," .. rnw_data[vvv][2] .. "\n"
					file_navdata2:write(fms_line)
				end
			end
		end
		file_navdata2:close()
	end
end

function upload_winds(name_of_file, in_rte)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	local id_nav = ""
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	local apt_ok = 0
	local iii = 0
	local jjj = 0
	local wx_find = 0
	local qnh_find = 0
	local avg_isa_find = 0
	local fms_line = ""
	
	local column_name_num = 0
	raw_table('column_name')
	local column_name = {}
	local wind_column_num = 0
	raw_table('wind_column')
	local wind_column = {}
	
	if in_rte == 0 then
		wind_data_clb_num = 0
		wind_data_clb_alt = {}
		wind_data_clb_hdg = {}
		wind_data_clb_spd = {}
		wind_data_toc_num = 0
		wind_data_toc_alt = {}
		wind_data_toc_hdg = {}
		wind_data_toc_spd = {}
		wind_data_tod_num = 0
		wind_data_tod_alt = {}
		wind_data_tod_hdg = {}
		wind_data_tod_spd = {}
		wind_data_des_num = 0
		wind_data_des_alt = {}
		wind_data_des_hdg = {}
		wind_data_des_spd = {}
		qnh_data_dest = ""
		avg_isa = ""
	else
		wind_data_clb_num7 = 0
		wind_data_clb_alt7 = {}
		wind_data_clb_hdg7 = {}
		wind_data_clb_spd7 = {}
		wind_data_toc_num7 = 0
		wind_data_toc_alt7 = {}
		wind_data_toc_hdg7 = {}
		wind_data_toc_spd7 = {}
		wind_data_tod_num7 = 0
		wind_data_tod_alt7 = {}
		wind_data_tod_hdg7 = {}
		wind_data_tod_spd7 = {}
		wind_data_des_num7 = 0
		wind_data_des_alt7 = {}
		wind_data_des_hdg7 = {}
		wind_data_des_spd7 = {}
		qnh_data_dest7 = ""
		avg_isa7 = ""
	end
	
	
	local file_name_imp = "Output/FMS plans/" .. name_of_file .. ".xml"
	local file_navdata2 = io.open(file_name_imp, "r")
	if file_navdata2 ~= nil then
		apt_line = file_navdata2:read()
		while apt_line do
			ii = 0
			jj = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(apt_line)
			if line_lenght > 0 then
				if avg_isa_find == 0 and line_lenght > 4 then
					if string.sub(apt_line, 1, 4) == "OFP " then
						avg_isa_find = 1
					end
				elseif avg_isa_find == 1 then
					for kk = 1, line_lenght do
						line_char = string.sub(apt_line, kk, kk)
						if line_char == " " then
							if ii == 1 then
								jj = jj + 1
								apt_word[jj] = line_trim
								ii = 0
								line_trim = ""
							end
						else
							line_trim = line_trim .. line_char
							ii = 1
						end
					end
					if string.len(line_trim) > 0 then
						jj = jj + 1
						apt_word[jj] = line_trim
					end
					
					if jj > 0 then
						if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
							apt_word[jj] = string.sub(apt_word[jj], 1, -2)
						end
					end
					
					if jj > 2 then
						for kk = 1, jj-2 do
							if apt_word[kk] == "AVG" and apt_word[kk+1] == "ISA" then
								if (string.sub(apt_word[kk+2], 1, 1) == "P" or string.sub(apt_word[kk+2], 1, 1) == "M") and string.len(apt_word[kk+2]) > 1 then
									fms_line = string.sub(apt_word[kk+2], 2, -1)
									mm = tonumber(fms_line)
									if mm == nil then
										avg_isa_find = 2
									else
										if in_rte == 0 then
											if string.sub(apt_word[kk+2], 1, 1) == "P" then
												avg_isa = string.format("%3d", mm)
											else
												avg_isa = string.format("%3d", -mm)
											end
										else
											if string.sub(apt_word[kk+2], 1, 1) == "P" then
												avg_isa7 = string.format("%3d", mm)
											else
												avg_isa7 = string.format("%3d", -mm)
											end
										end
										avg_isa_find = 2
									end
								else
									avg_isa_find = 2
								end
							end
						end
					end
					
					ii = 0
					jj = 0
					line_trim = ""
					line_char = ""
					
				end
				iii,jjj = string.find(apt_line, " WIND INFORMATION ")
				if iii ~= nil then
					wx_find = 1
					avg_isa_find = 2
				else
					if wx_find ~= 0 then
						if string.sub(apt_line, 1, 1) == "-" then
							wx_find = 3
							
							if qnh_find ~= 0 then
								break
							end
						end
						
						for kk = 1, line_lenght do
							line_char = string.sub(apt_line, kk, kk)
							if line_char == " " then
								if ii == 1 then
									jj = jj + 1
									apt_word[jj] = line_trim
									ii = 0
									line_trim = ""
								end
							else
								line_trim = line_trim .. line_char
								ii = 1
							end
						end
						if string.len(line_trim) > 0 then
							jj = jj + 1
							apt_word[jj] = line_trim
						end
						
						if jj > 0 then
							if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
								apt_word[jj] = string.sub(apt_word[jj], 1, -2)
							end
						end
						
						if qnh_find == 0 then
							if jj > 0 then
								if apt_word[1] == "Destination:" then
									qnh_find = 1
								end
							end
						elseif qnh_find == 1 then
							qnh_find = 2
						elseif qnh_find == 2 then
							qnh_find = 3
							
							if jj > 0 then
								for kk = 1, jj do
									if in_rte == 0 then
										if string.sub(apt_word[kk], 1, 1) == "Q" and string.len(apt_word[kk]) == 5 then
											qnh_data_dest = string.sub(apt_word[kk], 2, -1)
											break
										elseif string.sub(apt_word[kk], 1, 1) == "A" and string.len(apt_word[kk]) == 5 then
											qnh_data_dest = string.sub(apt_word[kk], 2, 3) .. "." .. string.sub(apt_word[kk], 4, 5)
											break
										end
									else
										if string.sub(apt_word[kk], 1, 1) == "Q" and string.len(apt_word[kk]) == 5 then
											qnh_data_dest7 = string.sub(apt_word[kk], 2, -1)
											break
										elseif string.sub(apt_word[kk], 1, 1) == "A" and string.len(apt_word[kk]) == 5 then
											qnh_data_dest7 = string.sub(apt_word[kk], 2, 3) .. "." .. string.sub(apt_word[kk], 4, 5)
											break
										end
									end
								end
							end
							
							if wx_find ~= 0 then
								break
							end
						end
						
						if wx_find == 1 then
							if jj > 0 then
								kk = 1
								while kk <= jj do
									if kk <= jj - 2 then
										if apt_word[kk] == "T" and apt_word[kk+1] == "O" and apt_word[kk+2] == "C" then
											column_name_num = column_name_num + 1
											column_name[column_name_num] = "TOC"
											kk = kk + 3
										elseif apt_word[kk] == "T" and apt_word[kk+1] == "O" and apt_word[kk+2] == "D" then
											column_name_num = column_name_num + 1
											column_name[column_name_num] = "TOD"
											kk = kk + 3
										else
											column_name_num = column_name_num + 1
											column_name[column_name_num] = apt_word[kk]
											kk = kk + 1
										end
									else
										column_name_num = column_name_num + 1
										column_name[column_name_num] = apt_word[kk]
										kk = kk + 1
									end
								end
								
								if column_name_num > 0 then
									for kk = 1, column_name_num do
										wind_column_num = wind_column_num + 1
										wind_column[wind_column_num] = column_name[kk]
									end
									wx_find = 2
								end
							end
						elseif wx_find == 2 then
							kk = (wind_column_num * 3) - 1
							
							if jj > kk and wind_column_num > 0 then
								for ll = 1, wind_column_num do
									mm = ((ll - 1) * 3) + 1
									if in_rte == 0 then
										if wind_column[ll] == "CLIMB" then
											wind_data_clb_num = wind_data_clb_num + 1
											wind_data_clb_alt[wind_data_clb_num] = tonumber(apt_word[mm]) * 100
											wind_data_clb_hdg[wind_data_clb_num] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_clb_spd[wind_data_clb_num] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										elseif wind_column[ll] == "TOC" then
											wind_data_toc_num = wind_data_toc_num + 1
											wind_data_toc_alt[wind_data_toc_num] = tonumber(apt_word[mm]) * 100
											wind_data_toc_hdg[wind_data_toc_num] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_toc_spd[wind_data_toc_num] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										elseif wind_column[ll] == "TOD" then
											wind_data_tod_num = wind_data_tod_num + 1
											wind_data_tod_alt[wind_data_tod_num] = tonumber(apt_word[mm]) * 100
											wind_data_tod_hdg[wind_data_tod_num] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_tod_spd[wind_data_tod_num] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										elseif wind_column[ll] == "DESCENT" then
											wind_data_des_num = wind_data_des_num + 1
											wind_data_des_alt[wind_data_des_num] = tonumber(apt_word[mm]) * 100
											wind_data_des_hdg[wind_data_des_num] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_des_spd[wind_data_des_num] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										end
									else
										if wind_column[ll] == "CLIMB" then
											wind_data_clb_num7 = wind_data_clb_num7 + 1
											wind_data_clb_alt7[wind_data_clb_num7] = tonumber(apt_word[mm]) * 100
											wind_data_clb_hdg7[wind_data_clb_num7] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_clb_spd7[wind_data_clb_num7] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										elseif wind_column[ll] == "TOC" then
											wind_data_toc_num7 = wind_data_toc_num7 + 1
											wind_data_toc_alt7[wind_data_toc_num7] = tonumber(apt_word[mm]) * 100
											wind_data_toc_hdg7[wind_data_toc_num7] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_toc_spd7[wind_data_toc_num7] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										elseif wind_column[ll] == "TOD" then
											wind_data_tod_num7 = wind_data_tod_num7 + 1
											wind_data_tod_alt7[wind_data_tod_num7] = tonumber(apt_word[mm]) * 100
											wind_data_tod_hdg7[wind_data_tod_num7] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_tod_spd7[wind_data_tod_num7] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										elseif wind_column[ll] == "DESCENT" then
											wind_data_des_num7 = wind_data_des_num7 + 1
											wind_data_des_alt7[wind_data_des_num7] = tonumber(apt_word[mm]) * 100
											wind_data_des_hdg7[wind_data_des_num7] = tonumber(string.sub(apt_word[mm+1], 1, 3))
											wind_data_des_spd7[wind_data_des_num7] = tonumber(string.sub(apt_word[mm+1], 5, 7))
										end
									end
								end
							end
						end
					end
				end
			else
				if wx_find == 2 and wind_column_num ~= 0 then
					column_name_num = 0
					column_name = {}
					wind_column_num = 0
					wind_column = {}
					wx_find = 1
				end
			end
			apt_line = file_navdata2:read()
		end
		file_navdata2:close()
	end
	
end

function uplink_wind_des(in_rte)
	
	local nn = 0
	local kk = 0
	local ll = 0
	local forecast_uplinked = 0
	
	local use_wind_data_clb_num = 0
	local use_wind_data_toc_num = 0
	local use_wind_data_tod_num = 0
	local use_wind_data_des_num = 0
	local use_avg_isa = ""
	local use_qnh_data_dest = ""
	
	if in_rte == 0 then
		use_wind_data_clb_num = wind_data_clb_num
		use_wind_data_toc_num = wind_data_toc_num
		use_wind_data_tod_num = wind_data_tod_num
		use_wind_data_des_num = wind_data_des_num
		use_avg_isa = avg_isa
		use_qnh_data_dest = qnh_data_dest
	else
		use_wind_data_clb_num = wind_data_clb_num7
		use_wind_data_toc_num = wind_data_toc_num7
		use_wind_data_tod_num = wind_data_tod_num7
		use_wind_data_des_num = wind_data_des_num7
		use_avg_isa = avg_isa7
		use_qnh_data_dest = qnh_data_dest7
	end
	
	if use_wind_data_des_num > 0 then
		for kk = 1, use_wind_data_des_num do
			if in_rte == 0 then
				if wind_data_des_alt[kk] < crz_alt_num then
					ll = kk
					break
				end
			else
				if wind_data_des_alt7[kk] < crz_alt_num then
					ll = kk
					break
				end
			end
		end
		
		if ll ~= 0 then
			forecast_uplinked = 1
			
			-- Wind Layer 1
			if in_rte == 0 then
				forec_alt_1_num_mod = wind_data_des_alt[ll]
			else
				forec_alt_1_num_mod = wind_data_des_alt7[ll]
			end
			if forec_alt_1_num_mod >= B738DR_trans_lvl_mod then
				nn = forec_alt_1_num_mod / 100
				forec_alt_1_mod = "FL" .. string.format("%03d", nn)
			else
				nn = forec_alt_1_num_mod
				forec_alt_1_mod = string.format("%5d", nn)
			end
			
			if in_rte == 0 then
				forec_dir_1_mod = string.format("%03d", wind_data_des_hdg[ll])
				forec_spd_1_mod = string.format("%3d", wind_data_des_spd[ll])
			else
				forec_dir_1_mod = string.format("%03d", wind_data_des_hdg7[ll])
				forec_spd_1_mod = string.format("%3d", wind_data_des_spd7[ll])
			end
			
			if ll == use_wind_data_des_num - 2 then
				ll = use_wind_data_des_num - 1
				-- Wind Layer 2
				if in_rte == 0 then
					forec_alt_2_num_mod = wind_data_des_alt[ll]
				else
					forec_alt_2_num_mod = wind_data_des_alt7[ll]
				end
				if forec_alt_2_num_mod >= B738DR_trans_lvl_mod then
					nn = forec_alt_2_num_mod / 100
					forec_alt_2_mod = "FL" .. string.format("%03d", nn)
				else
					nn = forec_alt_2_num_mod
					forec_alt_2_mod = string.format("%5d", nn)
				end
				if in_rte == 0 then
					forec_dir_2_mod = string.format("%03d", wind_data_des_hdg[ll])
					forec_spd_2_mod = string.format("%3d", wind_data_des_spd[ll])
				else
					forec_dir_2_mod = string.format("%03d", wind_data_des_hdg7[ll])
					forec_spd_2_mod = string.format("%3d", wind_data_des_spd7[ll])
				end
				
				ll = use_wind_data_des_num
				-- Wind Layer 3
				if in_rte == 0 then
					forec_alt_3_num_mod = wind_data_des_alt[ll]
				else
					forec_alt_3_num_mod = wind_data_des_alt7[ll]
				end
				if forec_alt_3_num_mod >= B738DR_trans_lvl_mod then
					nn = forec_alt_3_num_mod / 100
					forec_alt_3_mod = "FL" .. string.format("%03d", nn)
				else
					nn = forec_alt_3_num_mod
					forec_alt_3_mod = string.format("%5d", nn)
				end
				if in_rte == 0 then
					forec_dir_3_mod = string.format("%03d", wind_data_des_hdg[ll])
					forec_spd_3_mod = string.format("%3d", wind_data_des_spd[ll])
				else
					forec_dir_3_mod = string.format("%03d", wind_data_des_hdg7[ll])
					forec_spd_3_mod = string.format("%3d", wind_data_des_spd7[ll])
				end
			elseif ll < use_wind_data_des_num - 1 then
				ll = math.floor((ll + 1 + use_wind_data_des_num) / 2)
				-- Wind Layer 2
				if in_rte == 0 then
					forec_alt_2_num_mod = wind_data_des_alt[ll]
				else
					forec_alt_2_num_mod = wind_data_des_alt7[ll]
				end
				if forec_alt_2_num_mod >= B738DR_trans_lvl_mod then
					nn = forec_alt_2_num_mod / 100
					forec_alt_2_mod = "FL" .. string.format("%03d", nn)
				else
					nn = forec_alt_2_num_mod
					forec_alt_2_mod = string.format("%5d", nn)
				end
				if in_rte == 0 then
					forec_dir_2_mod = string.format("%03d", wind_data_des_hdg[ll])
					forec_spd_2_mod = string.format("%3d", wind_data_des_spd[ll])
				else
					forec_dir_2_mod = string.format("%03d", wind_data_des_hdg7[ll])
					forec_spd_2_mod = string.format("%3d", wind_data_des_spd7[ll])
				end
				
				if ll < use_wind_data_des_num then
					ll = use_wind_data_des_num
					-- Wind Layer 3
					if in_rte == 0 then
						forec_alt_3_num_mod = wind_data_des_alt[ll]
					else
						forec_alt_3_num_mod = wind_data_des_alt7[ll]
					end
					if forec_alt_3_num_mod >= B738DR_trans_lvl_mod then
						nn = forec_alt_3_num_mod / 100
						forec_alt_3_mod = "FL" .. string.format("%03d", nn)
					else
						nn = forec_alt_3_num_mod
						forec_alt_3_mod = string.format("%5d", nn)
					end
					if in_rte == 0 then
						forec_dir_3_mod = string.format("%03d", wind_data_des_hdg[ll])
						forec_spd_3_mod = string.format("%3d", wind_data_des_spd[ll])
					else
						forec_dir_3_mod = string.format("%03d", wind_data_des_hdg7[ll])
						forec_spd_3_mod = string.format("%3d", wind_data_des_spd7[ll])
					end
				else
					-- Wind Layer 3
					forec_alt_3_mod = "-----"
					forec_alt_3_num_mod = 0
					forec_dir_3_mod = "---"
					forec_spd_3_mod = "---"
				end
			elseif ll < use_wind_data_des_num then
				ll = use_wind_data_des_num
				-- Wind Layer 2
				if in_rte == 0 then
					forec_alt_2_num_mod = wind_data_des_alt[ll]
				else
					forec_alt_2_num_mod = wind_data_des_alt7[ll]
				end
				if forec_alt_2_num_mod >= B738DR_trans_lvl_mod then
					nn = forec_alt_2_num_mod / 100
					forec_alt_2_mod = "FL" .. string.format("%03d", nn)
				else
					nn = forec_alt_2_num_mod
					forec_alt_2_mod = string.format("%5d", nn)
				end
				if in_rte == 0 then
					forec_dir_2_mod = string.format("%03d", wind_data_des_hdg[ll])
					forec_spd_2_mod = string.format("%3d", wind_data_des_spd[ll])
				else
					forec_dir_2_mod = string.format("%03d", wind_data_des_hdg7[ll])
					forec_spd_2_mod = string.format("%3d", wind_data_des_spd7[ll])
				end
				
				-- Wind Layer 3
				forec_alt_3_mod = "-----"
				forec_alt_3_num_mod = 0
				forec_dir_3_mod = "---"
				forec_spd_3_mod = "---"
			else
				-- Wind Layer 2
				forec_alt_2_mod = "-----"
				forec_alt_2_num_mod = 0
				forec_dir_2_mod = "---"
				forec_spd_2_mod = "---"
				
				-- Wind Layer 3
				forec_alt_3_mod = "-----"
				forec_alt_3_num_mod = 0
				forec_dir_3_mod = "---"
				forec_spd_3_mod = "---"
			end
		end
	end
	
	kk = string.len(avg_isa)
	if kk == 0 then
		if forecast_uplinked ~= 0 then
			forec_isa_dev_mod = "---"
		end
	else
		if forecast_uplinked == 0 then
			-- Wind Layer 1
			forec_alt_1_mod = "-----"
			forec_alt_1_num_mod = 0
			forec_dir_1_mod = "---"
			forec_spd_1_mod = "---"
			
			-- Wind Layer 2
			forec_alt_2_mod = "-----"
			forec_alt_2_num_mod = 0
			forec_dir_2_mod = "---"
			forec_spd_2_mod = "---"
			
			-- Wind Layer 3
			forec_alt_3_mod = "-----"
			forec_alt_3_num_mod = 0
			forec_dir_3_mod = "---"
			forec_spd_3_mod = "---"
		end
		forecast_uplinked = 1
		forec_isa_dev_mod = avg_isa
	end
	
	kk = string.len(qnh_data_dest)
	if kk == 0 then
		if forecast_uplinked ~= 0 then
			forec_qnh_mod = "------"
		end
	elseif kk == 4 then
		if forecast_uplinked == 0 then
			-- Wind Layer 1
			forec_alt_1_mod = "-----"
			forec_alt_1_num_mod = 0
			forec_dir_1_mod = "---"
			forec_spd_1_mod = "---"
			
			-- Wind Layer 2
			forec_alt_2_mod = "-----"
			forec_alt_2_num_mod = 0
			forec_dir_2_mod = "---"
			forec_spd_2_mod = "---"
			
			-- Wind Layer 3
			forec_alt_3_mod = "-----"
			forec_alt_3_num_mod = 0
			forec_dir_3_mod = "---"
			forec_spd_3_mod = "---"
		end
		forecast_uplinked = 1
		forec_qnh_mod = "  " .. qnh_data_dest
	elseif kk == 5 then
		if forecast_uplinked == 0 then
			-- Wind Layer 1
			forec_alt_1_mod = "-----"
			forec_alt_1_num_mod = 0
			forec_dir_1_mod = "---"
			forec_spd_1_mod = "---"
			
			-- Wind Layer 2
			forec_alt_2_mod = "-----"
			forec_alt_2_num_mod = 0
			forec_dir_2_mod = "---"
			forec_spd_2_mod = "---"
			
			-- Wind Layer 3
			forec_alt_3_mod = "-----"
			forec_alt_3_num_mod = 0
			forec_dir_3_mod = "---"
			forec_spd_3_mod = "---"
		end
		forecast_uplinked = 1
		forec_qnh_mod = " " .. qnh_data_dest
	else
		if forecast_uplinked ~= 0 then
			forec_qnh_mod = "------"
		end
	end
	
	if forecast_uplinked ~= 0 then
		forecast_exec = 1
	end
end

function import_fms(name_of_file, in_rte)
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	local id_nav = ""
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	local apt_ok = 0
	
	rte_data_num = 0
	rte_data = {}
	
	local file_name_imp = "Output/FMS plans/" .. name_of_file .. ".fms"
	local file_navdata2 = io.open(file_name_imp, "r")
	if file_navdata2 ~= nil then
		apt_line = file_navdata2:read()
		while apt_line do
			ii = 0
			jj = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(apt_line)
			if line_lenght > 0 then
				for kk = 1, line_lenght do
					line_char = string.sub(apt_line, kk, kk)
					if line_char == " " then
						if ii == 1 then
							jj = jj + 1
							apt_word[jj] = line_trim
							ii = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						ii = 1
					end
				end
				if string.len(line_trim) > 0 then
					jj = jj + 1
					apt_word[jj] = line_trim
				end
				
				if jj > 0 then
					if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
						apt_word[jj] = string.sub(apt_word[jj], 1, -2)
					end
				end
				
				if jj > 4 then
					if apt_word[1] == "1" or apt_word[1] == "2" or apt_word[1] == "3" or apt_word[1] == "11" or
					apt_word[1] == "28" then --or apt_word[1] == "28" then
						
						if apt_word[1] == "28" or apt_word[1] == "PPOS" then
							
							id_nav = "WPT"
							
							mm = 1
							if custom_wpt_num > 0 then
								for kk = 1, custom_wpt_num do
									if string.sub(custom_wpt[kk][1], 1, 3) == id_nav then
										nn = tonumber(string.sub(custom_wpt[kk][1], 4, -1))
										if nn ~= nil then
											if nn >= mm then
												mm = nn + 1
											end
										end
									end
								end
							end
							apt_word[2] = id_nav .. string.format("%02d", mm)
							
							-- add custom wpt
							custom_wpt_num = custom_wpt_num + 1
							custom_wpt[custom_wpt_num] = {}
							custom_wpt[custom_wpt_num][1] = apt_word[2]
							custom_wpt[custom_wpt_num][2] = tonumber(apt_word[4])
							custom_wpt[custom_wpt_num][3] = tonumber(apt_word[5])
							custom_wpt[custom_wpt_num][4] = ""
							custom_wpt[custom_wpt_num][5] = ""
							custom_wpt[custom_wpt_num][6] = ""
							
						elseif string.len(apt_word[2]) > 7 then
							apt_word[2] = string.sub(apt_word[2], 1, 7)
						end
						
						rte_data_num = rte_data_num + 1
						rte_data[rte_data_num] = {}
						rte_data[rte_data_num][1] = tonumber(apt_word[1])		-- type
						rte_data[rte_data_num][2] = apt_word[2]		-- id name
						rte_data[rte_data_num][3] = apt_word[3]		-- alt
						rte_data[rte_data_num][4] = tonumber(apt_word[4])		-- lat
						rte_data[rte_data_num][5] = tonumber(apt_word[5])		-- lon
						rte_data[rte_data_num][6] = "DIRECT"		-- via
						rte_data[rte_data_num][7] = ""		-- reg code
					end
				end
			end
			apt_line = file_navdata2:read()
		end
		file_navdata2:close()
		use_import_data(name_of_file, in_rte)
	end
	
end

function import_fms_v11(name_of_file, in_rte)
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	local id_nav = ""
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	local apt_ok = 0
	
	local rwy_tmp = ""
	local sid_tmp = ""
	local sid_tns_tmp = ""
	local star_tmp = ""
	local star_tns_tmp = ""
	local app_tmp = ""
	local app_tns_tmp = ""
	
	local tmp_txt_via = ""
	
	rte_data_num = 0
	rte_data = {}
	
	local file_name_imp = "Output/FMS plans/" .. name_of_file .. ".fms"
	local file_navdata2 = io.open(file_name_imp, "r")
	if file_navdata2 ~= nil then
		apt_line = file_navdata2:read()
		while apt_line do
			ii = 0
			jj = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(apt_line)
			if line_lenght > 0 then
				for kk = 1, line_lenght do
					line_char = string.sub(apt_line, kk, kk)
					if line_char == " " or string.byte(line_char) == 9 then		-- SP or TAB
						if ii == 1 then
							jj = jj + 1
							apt_word[jj] = line_trim
							ii = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						ii = 1
					end
				end
				if string.len(line_trim) > 0 then
					jj = jj + 1
					apt_word[jj] = line_trim
				end
				
				if jj > 0 then
					if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
						apt_word[jj] = string.sub(apt_word[jj], 1, -2)
					end
				end
				
				if jj > 5 then
					if apt_word[1] == "1" or apt_word[1] == "2" or apt_word[1] == "3" or apt_word[1] == "11" or
					apt_word[1] == "28" then
						
						if apt_word[1] == "28" or apt_word[1] == "PPOS" then
							
							id_nav = "WPT"
							
							mm = 1
							if custom_wpt_num > 0 then
								for kk = 1, custom_wpt_num do
									if string.sub(custom_wpt[kk][1], 1, 3) == id_nav then
										nn = tonumber(string.sub(custom_wpt[kk][1], 4, -1))
										if nn ~= nil then
											if nn >= mm then
												mm = nn + 1
											end
										end
									end
								end
							end
							apt_word[2] = id_nav .. string.format("%02d", mm)
							
							-- add custom wpt
							custom_wpt_num = custom_wpt_num + 1
							custom_wpt[custom_wpt_num] = {}
							custom_wpt[custom_wpt_num][1] = apt_word[2]
							custom_wpt[custom_wpt_num][2] = tonumber(apt_word[5])
							custom_wpt[custom_wpt_num][3] = tonumber(apt_word[6])
							custom_wpt[custom_wpt_num][4] = ""
							custom_wpt[custom_wpt_num][5] = ""
							custom_wpt[custom_wpt_num][6] = ""
							
						elseif string.len(apt_word[2]) > 7 then
							apt_word[2] = string.sub(apt_word[2], 1, 7)
						end
						
						rte_data_num = rte_data_num + 1
						rte_data[rte_data_num] = {}
						rte_data[rte_data_num][1] = tonumber(apt_word[1])		-- type
						rte_data[rte_data_num][2] = apt_word[2]		-- id name
						rte_data[rte_data_num][3] = apt_word[4]		-- alt
						rte_data[rte_data_num][4] = tonumber(apt_word[5])		-- lat
						rte_data[rte_data_num][5] = tonumber(apt_word[6])		-- lon
						tmp_txt_via = apt_word[3]
						if apt_word[3] == "ADEP" then
							tmp_txt_via = ""
						elseif apt_word[3] == "ADES" then
							tmp_txt_via = ""
						elseif apt_word[3] == "DRCT" then
							tmp_txt_via = "DIRECT"
						end
						rte_data[rte_data_num][6] = tmp_txt_via		-- via
						rte_data[rte_data_num][7] = ""		-- reg code
					end
				elseif jj > 1 then
					if apt_word[1] == "DEPRWY" then
						rwy_tmp = apt_word[2]
						if string.len(rwy_tmp) > 3 then
							rwy_tmp = string.sub(rwy_tmp, 3, -1)
						end
					elseif apt_word[1] == "SID" then
						sid_tmp = apt_word[2]
					elseif apt_word[1] == "SIDTRANS" then
						sid_tns_tmp = apt_word[2]
					elseif apt_word[1] == "STAR" then
						star_tmp = apt_word[2]
					elseif apt_word[1] == "STARTRANS" then
						star_tns_tmp = apt_word[2]
					elseif apt_word[1] == "APP" then
						app_tmp = apt_word[2]
					elseif apt_word[1] == "APPTRANS" then
						app_tns_tmp = apt_word[2]
					end
				end
			end
			apt_line = file_navdata2:read()
		end
		file_navdata2:close()
		
		-- dump_rte_data()
		
		use_import_data(name_of_file, in_rte)
		
		-- add sid-star-app
		if fpln_import_ok == 1 then
			
			if in_rte == 0 then
				reverse_rte1 = 1
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. ref_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. ref_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_sid(rwy_tmp, sid_tmp, sid_tns_tmp, in_rte)
				end
				
				apt_ok = 0
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. des_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. des_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_star(star_tmp, star_tns_tmp, in_rte)
					import_app(app_tmp, app_tns_tmp, in_rte)
				end
			else
				reverse_rte2 = 1
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. ref_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao7_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. ref_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao7_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_sid(rwy_tmp, sid_tmp, sid_tns_tmp, in_rte)
				end
				
				apt_ok = 0
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. des_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao7_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. des_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao7_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_star(star_tmp, star_tns_tmp, in_rte)
					import_app(app_tmp, app_tns_tmp, in_rte)
				end
			end
		end
	end
	
end

function import_sid(i_rwy, i_sid, i_sid_tns, in_rte)
	if in_rte == 0 then
		if i_rwy ~= "" then
			ref_rwy2 = i_rwy
		end
		if i_sid ~= "" then
			ref_sid2 = i_sid
		end
		if i_sid_tns ~= "" then
			ref_sid_tns2 = i_sid_tns
		end
	else
		if i_rwy ~= "" then
			ref_rwy8 = i_rwy
		end
		if i_sid ~= "" then
			ref_sid8 = i_sid
		end
		if i_sid_tns ~= "" then
			ref_sid_tns8 = i_sid_tns
		end
	end
	rte_add_sid_act = 1
	add_sid_star_app_act = in_rte
end

function import_star(i_star, i_star_tns, in_rte)
	if in_rte == 0 then
		if i_star ~= "" then
			des_star2 = i_star
		end
		if i_star_tns ~= "" then
			des_star_trans2 = i_star_tns
		end
		des_star2_old = des_star2
		des_star_trans2_old = des_star_trans2
	else
		if i_star ~= "" then
			des_star8 = i_star
		end
		if i_star_tns ~= "" then
			des_star_trans8 = i_star_tns
		end
		des_star8_old = des_star8
		des_star_trans8_old = des_star_trans8
	end
	rte_add_star_act = 1
	rte_add_app_act = 1
	add_sid_star_app_act = in_rte
end

function import_app(i_app, i_app_tns, in_rte)
	if in_rte == 0 then
		if i_app ~= "" then
			des_app2 = i_app
		end
		if i_app_tns ~= "" then
			des_app_tns2 = i_app_tns
		end
		des_app2_old = des_app2
		des_app_tns2_old = des_app_tns2
	else
		if i_app ~= "" then
			des_app8 = i_app
		end
		if i_app_tns ~= "" then
			des_app_tns8 = i_app_tns
		end
		des_app8_old = des_app8
		des_app_tns8_old = des_app_tns8
	end
	rte_add_app_act = 1
	add_sid_star_app_act = in_rte
end

function use_import_data(name_of_file, in_rte)

	local ii = 0
	local jj = 0
	local kk = 0
	local apt_ok = 0
	local xy = 0
	local iii = 0
	local jjj = 0
	local import_alt = ""
	local import_speed = ""
	
	fpln_import_ok = 0
	
	-- USE DATA
	if rte_data_num > 1 then
		if rte_data[1][1] == 1 and rte_data[rte_data_num][1] == 1 then
			
			kk = 0		-- no error
			legs_num2_buf = 0
			legs_data2_buf = {}
			
			-- REF ICAO
			apt_ok = 0	-- no error
			if in_rte == 0 then
				ref_icao_x = rte_data[1][2]
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. ref_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. ref_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
			else
				ref_icao7_x = rte_data[1][2]
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. ref_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao7_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. ref_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao7_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
			end
			
			if apt_ok == 1 then
				if in_rte == 0 then
					des_icao_x = "****"
					ref_rwy2 = "-----"
					ref_rwy3 = "-----"
					ref_sid2 = "------"
					ref_sid_tns2 = "------"
					des_app2 = "------"
					des_app_tns2 = "------"
					des_star2 = "------"
					des_star_trans2 = "------"
					
					if apt_exist(ref_icao_x) == true then
						ref_icao_lat_x = icao_latitude
						ref_icao_lon_x = icao_longitude
						ref_tns_alt_x = icao_tns_alt
						ref_tns_lvl_x = icao_tns_lvl
						ref_icao_alt_x = 0
						B738DR_find_icao = ref_icao_x
						B738DR_find_icao_cmd = 1
						if B738DR_find_icao_cmd == 2 then
							ref_icao_alt_x = B738DR_find_icao_height * 3.280839895
						end
					else
						ref_tns_alt_x = 0
						ref_tns_lvl_x = 0
					end
					if ref_tns_alt_x == 0 then
						trans_alt_mod = "-----"
					else
						trans_alt_mod = string.format("%5d", ref_tns_alt_x)
					end
				else
					des_icao7_x = "****"
					ref_rwy8 = "-----"
					ref_rwy9 = "-----"
					ref_sid8 = "------"
					ref_sid_tns8 = "------"
					des_app8 = "------"
					des_app_tns8 = "------"
					des_star8 = "------"
					des_star_trans8 = "------"
					
					if apt_exist(ref_icao7_x) == true then
						ref_icao_lat7_x = icao_latitude
						ref_icao_lon7_x = icao_longitude
						ref_tns_alt7_x = icao_tns_alt
						ref_tns_lvl7_x = icao_tns_lvl
						ref_icao_alt7_x = 0
						B738DR_find_icao = ref_icao7_x
						B738DR_find_icao_cmd = 1
						if B738DR_find_icao_cmd == 2 then
							ref_icao_alt7_x = B738DR_find_icao_height * 3.280839895
						end
					else
						ref_tns_alt7_x = 0
						ref_tns_lvl7_x = 0
					end
					if ref_tns_alt7_x == 0 then
						trans_alt_mod2 = "-----"
					else
						trans_alt_mod2 = string.format("%5d", ref_tns_alt7_x)
					end
				end
				
				legs_num2_buf = legs_num2_buf + 1
				legs_data2_buf[legs_num2_buf] = {}
				if in_rte == 0 then
					legs_data2_buf[legs_num2_buf][1] = ref_icao_x	--entry
				else
					legs_data2_buf[legs_num2_buf][1] = ref_icao7_x	--entry
				end
				legs_data2_buf[legs_num2_buf][2] = 0		-- brg
				legs_data2_buf[legs_num2_buf][3] = 0		-- distance
				legs_data2_buf[legs_num2_buf][4] = 0		-- speed
				legs_data2_buf[legs_num2_buf][5] = 0		-- altitude
				legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
				if in_rte == 0 then
					legs_data2_buf[legs_num2_buf][7] = ref_icao_lat_x		-- latitude
					legs_data2_buf[legs_num2_buf][8] = ref_icao_lon_x		-- longitude
				else
					legs_data2_buf[legs_num2_buf][7] = ref_icao_lat7_x		-- latitude
					legs_data2_buf[legs_num2_buf][8] = ref_icao_lon7_x		-- longitude
				end
				legs_data2_buf[legs_num2_buf][9] = ""			-- via id
				legs_data2_buf[legs_num2_buf][10] = 0		-- calc speed
				legs_data2_buf[legs_num2_buf][11] = 0		-- calc altitude
				legs_data2_buf[legs_num2_buf][12] = 0		-- calc altitude vnav pth
				legs_data2_buf[legs_num2_buf][13] = 0
				legs_data2_buf[legs_num2_buf][14] = 0		-- rest alt
				legs_data2_buf[legs_num2_buf][15] = 0		-- last fuel
				legs_data2_buf[legs_num2_buf][16] = ""
				legs_data2_buf[legs_num2_buf][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
				legs_data2_buf[legs_num2_buf][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data2_buf[legs_num2_buf][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data2_buf[legs_num2_buf][20] = 0
				legs_data2_buf[legs_num2_buf][21] = -1		-- none HOLD
				legs_data2_buf[legs_num2_buf][22] = ""
				legs_data2_buf[legs_num2_buf][23] = 0
				legs_data2_buf[legs_num2_buf][24] = 0
				legs_data2_buf[legs_num2_buf][25] = 0
				legs_data2_buf[legs_num2_buf][26] = 0
				legs_data2_buf[legs_num2_buf][27] = ""
				legs_data2_buf[legs_num2_buf][28] = ""
				legs_data2_buf[legs_num2_buf][29] = ""
				legs_data2_buf[legs_num2_buf][30] = ""
				legs_data2_buf[legs_num2_buf][31] = ""
				legs_data2_buf[legs_num2_buf][32] = 0
				legs_data2_buf[legs_num2_buf][33] = ""
				legs_data2_buf[legs_num2_buf][34] = ""
				legs_data2_buf[legs_num2_buf][35] = ""
				legs_data2_buf[legs_num2_buf][36] = 9
				legs_data2_buf[legs_num2_buf][37] = 0
				legs_data2_buf[legs_num2_buf][38] = ""
				legs_data2_buf[legs_num2_buf][39] = ""
				legs_data2_buf[legs_num2_buf][40] = 0
				legs_data2_buf[legs_num2_buf][41] = 0
				legs_data2_buf[legs_num2_buf][42] = 0
				legs_data2_buf[legs_num2_buf][43] = ""
				legs_data2_buf[legs_num2_buf][44] = 0
				legs_data2_buf[legs_num2_buf][45] = 0
				legs_data2_buf[legs_num2_buf][46] = 0
				legs_data2_buf[legs_num2_buf][47] = 0
				legs_data2_buf[legs_num2_buf][48] = ""
				legs_data2_buf[legs_num2_buf][49] = 0
				legs_data2_buf[legs_num2_buf][50] = 0
				legs_data2_buf[legs_num2_buf][51] = 0
				legs_data2_buf[legs_num2_buf][52] = 0
				legs_data2_buf[legs_num2_buf][53] = 0
				legs_data2_buf[legs_num2_buf][54] = 0
			else
				kk = 1
			end
			
			-- ROUTE
			jj = rte_data_num - 1
			for ii = 2, jj do
				legs_num2_buf = legs_num2_buf + 1
				legs_data2_buf[legs_num2_buf] = {}
				legs_data2_buf[legs_num2_buf][1] = rte_data[ii][2]	--entry
				legs_data2_buf[legs_num2_buf][2] = 0		-- brg
				legs_data2_buf[legs_num2_buf][3] = 0		-- distance
				
				if B738DR_restrict_fms == 0 then
					legs_data2_buf[legs_num2_buf][4] = 0		-- speed
					legs_data2_buf[legs_num2_buf][5] = 0		-- altitude
					legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
					legs_data2_buf[legs_num2_buf][42] = 0
				else
					if tonumber(rte_data[ii][3]) == nil then
						iii,jjj = string.find(rte_data[ii][3], "/")
						if iii == nil then
							-- only altitude
							import_speed = ""
							import_alt = rte_data[ii][3]
						else
							if string.sub(rte_data[ii][3], -1, -1) == "/" then
								-- only speed
								import_speed = string.sub(rte_data[ii][3], 1, -2)
								import_alt = ""
							else
								-- speed and altitude
								import_speed = string.sub(rte_data[ii][3], 1, iii-1)
								import_alt = string.sub(rte_data[ii][3], iii+1, -1)
							end
						end
						
						-- convert speed
						if tonumber(import_speed) == nil then
							if tonumber(string.sub(import_speed, 1, -2)) == nil then
								legs_data2_buf[legs_num2_buf][4] = 0		-- speed
								legs_data2_buf[legs_num2_buf][42] = 0
							elseif string.sub(import_speed, -1, -1) == "B" then
								legs_data2_buf[legs_num2_buf][4] = tonumber(string.sub(import_speed, 1, -2))		-- speed
								legs_data2_buf[legs_num2_buf][42] = 1
							else
								legs_data2_buf[legs_num2_buf][4] = 0		-- speed
								legs_data2_buf[legs_num2_buf][42] = 0
							end
						else
							legs_data2_buf[legs_num2_buf][4] = tonumber(import_speed)		-- speed
							legs_data2_buf[legs_num2_buf][42] = 0
						end
						
						-- convert alt
						if tonumber(import_alt) == nil then
							if tonumber(string.sub(import_alt, 1, -2)) == nil then
								legs_data2_buf[legs_num2_buf][5] = 0	-- altitude
								legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
							elseif string.sub(import_alt, -1, -1) == "A" then
								legs_data2_buf[legs_num2_buf][5] = tonumber(string.sub(import_alt, 1, -2))	-- altitude
								legs_data2_buf[legs_num2_buf][6] = 43	-- altitude type
							elseif string.sub(import_alt, -1, -1) == "B" then
								legs_data2_buf[legs_num2_buf][5] = tonumber(string.sub(import_alt, 1, -2))	-- altitude
								legs_data2_buf[legs_num2_buf][6] = 45	-- altitude type
							else
								legs_data2_buf[legs_num2_buf][5] = 0	-- altitude
								legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
							end
						else
							legs_data2_buf[legs_num2_buf][5] = tonumber(import_alt)	-- altitude
							legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
						end
					else
						-- only altitude
						legs_data2_buf[legs_num2_buf][4] = 0		-- speed
						legs_data2_buf[legs_num2_buf][5] = tonumber(rte_data[ii][3])		-- altitude
						legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
						legs_data2_buf[legs_num2_buf][42] = 0
					end
					
					if legs_data2_buf[legs_num2_buf][4] < 100 then
						legs_data2_buf[legs_num2_buf][4] = 0		-- speed
						legs_data2_buf[legs_num2_buf][42] = 0
					end
				end
				
				legs_data2_buf[legs_num2_buf][7] = rte_data[ii][4]		-- latitude
				legs_data2_buf[legs_num2_buf][8] = rte_data[ii][5]		-- longitude
				legs_data2_buf[legs_num2_buf][9] = rte_data[ii][6]			-- via id
				legs_data2_buf[legs_num2_buf][10] = 0		-- calc speed
				legs_data2_buf[legs_num2_buf][11] = 0		-- calc altitude
				legs_data2_buf[legs_num2_buf][12] = 0		-- calc altitude vnav pth
				legs_data2_buf[legs_num2_buf][13] = 0
				legs_data2_buf[legs_num2_buf][14] = 0		-- rest alt
				legs_data2_buf[legs_num2_buf][15] = 0		-- last fuel
				
				if rte_data[ii][7] == "" then
					find_lat = rte_data[ii][4]
					find_lon = rte_data[ii][5]
					find_navaid(rte_data[ii][2], "", 3, "", in_rte)
					if navaid_list_n > 0 then
						rte_data[ii][7] = navaid_list[1][8]
					end
				end
				
				legs_data2_buf[legs_num2_buf][16] = rte_data[ii][7]
				legs_data2_buf[legs_num2_buf][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
				legs_data2_buf[legs_num2_buf][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data2_buf[legs_num2_buf][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data2_buf[legs_num2_buf][20] = 0
				legs_data2_buf[legs_num2_buf][21] = -1
				legs_data2_buf[legs_num2_buf][22] = ""
				legs_data2_buf[legs_num2_buf][23] = 0
				legs_data2_buf[legs_num2_buf][24] = 0
				legs_data2_buf[legs_num2_buf][25] = 0
				legs_data2_buf[legs_num2_buf][26] = 0
				legs_data2_buf[legs_num2_buf][27] = ""
				legs_data2_buf[legs_num2_buf][28] = ""
				legs_data2_buf[legs_num2_buf][29] = ""
				legs_data2_buf[legs_num2_buf][30] = ""
				legs_data2_buf[legs_num2_buf][31] = "TF"
				legs_data2_buf[legs_num2_buf][32] = 0
				legs_data2_buf[legs_num2_buf][33] = ""
				legs_data2_buf[legs_num2_buf][34] = ""
				legs_data2_buf[legs_num2_buf][35] = ""
				if rte_data[ii][1] == 1 then
					legs_data2_buf[legs_num2_buf][36] = 9
				elseif rte_data[ii][1] == 2 then
					legs_data2_buf[legs_num2_buf][36] = 2
				elseif rte_data[ii][1] == 3 then
					legs_data2_buf[legs_num2_buf][36] = 1
				elseif rte_data[ii][1] == 11 then
					legs_data2_buf[legs_num2_buf][36] = 4
				elseif rte_data[ii][1] == 28 then
					legs_data2_buf[legs_num2_buf][36] = 4
				else
					legs_data2_buf[legs_num2_buf][36] = 0
				end
				legs_data2_buf[legs_num2_buf][37] = 0
				legs_data2_buf[legs_num2_buf][38] = ""
				legs_data2_buf[legs_num2_buf][39] = ""
				legs_data2_buf[legs_num2_buf][40] = 0
				legs_data2_buf[legs_num2_buf][41] = 0
				legs_data2_buf[legs_num2_buf][43] = ""
				legs_data2_buf[legs_num2_buf][44] = 0
				legs_data2_buf[legs_num2_buf][45] = 0
				legs_data2_buf[legs_num2_buf][46] = 0
				legs_data2_buf[legs_num2_buf][47] = 0
				legs_data2_buf[legs_num2_buf][48] = ""
				legs_data2_buf[legs_num2_buf][49] = 0
				legs_data2_buf[legs_num2_buf][50] = 0
				legs_data2_buf[legs_num2_buf][51] = 0
				legs_data2_buf[legs_num2_buf][52] = 0
				legs_data2_buf[legs_num2_buf][53] = 0
				legs_data2_buf[legs_num2_buf][54] = 0
			end
			
			-- DES ICAO
			apt_ok = 0	-- no error
			if in_rte == 0 then
				des_icao_x = rte_data[rte_data_num][2]
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. des_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 2
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. des_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 2
					end
				end
			else
				des_icao7_x = rte_data[rte_data_num][2]
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. des_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao7_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 2
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. des_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao7_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 2
					end
				end
			end
			
			if apt_ok > 0 then
				
				if in_rte == 0 then
					des_app2 = "------"
					des_app_tns2 = "------"
					des_star2 = "------"
					des_star_trans2 = "------"
					
					if apt_exist(des_icao_x) == true then
						des_icao_lat_x = icao_latitude
						des_icao_lon_x = icao_longitude
						des_tns_alt_x = icao_tns_alt
						des_tns_lvl_x = icao_tns_lvl
						des_icao_alt_x = 2500
						B738DR_find_icao = des_icao_x
						B738DR_find_icao_cmd = 1
						if B738DR_find_icao_cmd == 2 then
							des_icao_alt_x = B738DR_find_icao_height * 3.280839895
						end
					else
						des_icao_alt = 2500
						B738DR_find_icao = des_icao_x
						B738DR_find_icao_cmd = 1
						if B738DR_find_icao_cmd == 2 then
							des_icao_alt_x = B738DR_find_icao_height * 3.280839895
						end
						des_tns_alt_x = 0
						des_tns_lvl_x = 0
					end
					if des_tns_lvl_x == 0 then
						des_tns_lvl_x = des_tns_alt_x
					end
					if des_tns_lvl_x == 0 then
						trans_lvl_mod = "-----"
					else
						apt_ok = des_tns_lvl_x / 100
						trans_lvl_mod = "FL" .. string.format("%03d", apt_ok)
					end
				else
					des_app8 = "------"
					des_app_tns8 = "------"
					des_star8 = "------"
					des_star_trans8 = "------"
					
					if apt_exist(des_icao7_x) == true then
						des_icao_lat7_x = icao_latitude
						des_icao_lon7_x = icao_longitude
						des_tns_alt7_x = icao_tns_alt
						des_tns_lvl7_x = icao_tns_lvl
						des_icao_alt7_x = 2500
						B738DR_find_icao = des_icao_x
						B738DR_find_icao_cmd = 1
						if B738DR_find_icao_cmd == 2 then
							des_icao_alt7_x = B738DR_find_icao_height * 3.280839895
						end
					else
						des_icao_alt7 = 2500
						B738DR_find_icao = des_icao_x
						B738DR_find_icao_cmd = 1
						if B738DR_find_icao_cmd == 2 then
							des_icao_alt7_x = B738DR_find_icao_height * 3.280839895
						end
						des_tns_alt7_x = 0
						des_tns_lvl7_x = 0
					end
					if des_tns_lvl7_x == 0 then
						des_tns_lvl7_x = des_tns_alt7_x
					end
					if des_tns_lvl7_x == 0 then
						trans_lvl_mod2 = "-----"
					else
						apt_ok = des_tns_lvl7_x / 100
						trans_lvl_mod2 = "FL" .. string.format("%03d", apt_ok)
					end
				end
				
				legs_num2_buf = legs_num2_buf + 1
				legs_data2_buf[legs_num2_buf] = {}
				if in_rte == 0 then
					legs_data2_buf[legs_num2_buf][1] = des_icao_x	--entry
				else
					legs_data2_buf[legs_num2_buf][1] = des_icao7_x	--entry
				end
				legs_data2_buf[legs_num2_buf][2] = 0		-- brg
				legs_data2_buf[legs_num2_buf][3] = 0		-- distance
				legs_data2_buf[legs_num2_buf][4] = 0		-- speed
				legs_data2_buf[legs_num2_buf][5] = 0		-- altitude
				legs_data2_buf[legs_num2_buf][6] = 0	-- altitude type
				if in_rte == 0 then
					legs_data2_buf[legs_num2_buf][7] = des_icao_lat_x		-- latitude
					legs_data2_buf[legs_num2_buf][8] = des_icao_lon_x		-- longitude
				else
					legs_data2_buf[legs_num2_buf][7] = des_icao_lat7_x		-- latitude
					legs_data2_buf[legs_num2_buf][8] = des_icao_lon7_x		-- longitude
				end
				legs_data2_buf[legs_num2_buf][9] = ""			-- via id
				legs_data2_buf[legs_num2_buf][10] = 0		-- calc speed
				legs_data2_buf[legs_num2_buf][11] = 0		-- calc altitude
				legs_data2_buf[legs_num2_buf][12] = 0		-- calc altitude vnav pth
				legs_data2_buf[legs_num2_buf][13] = 0
				legs_data2_buf[legs_num2_buf][14] = 0		-- rest alt
				legs_data2_buf[legs_num2_buf][15] = 0		-- last fuel
				legs_data2_buf[legs_num2_buf][16] = ""
				legs_data2_buf[legs_num2_buf][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
				legs_data2_buf[legs_num2_buf][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data2_buf[legs_num2_buf][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data2_buf[legs_num2_buf][20] = 0
				legs_data2_buf[legs_num2_buf][21] = -1		-- none HOLD
				legs_data2_buf[legs_num2_buf][22] = ""
				legs_data2_buf[legs_num2_buf][23] = 0
				legs_data2_buf[legs_num2_buf][24] = 0
				legs_data2_buf[legs_num2_buf][25] = 0
				legs_data2_buf[legs_num2_buf][26] = 0
				legs_data2_buf[legs_num2_buf][27] = ""
				legs_data2_buf[legs_num2_buf][28] = ""
				legs_data2_buf[legs_num2_buf][29] = ""
				legs_data2_buf[legs_num2_buf][30] = ""
				legs_data2_buf[legs_num2_buf][31] = ""
				legs_data2_buf[legs_num2_buf][32] = 0
				legs_data2_buf[legs_num2_buf][33] = ""
				legs_data2_buf[legs_num2_buf][34] = "DEST"
				legs_data2_buf[legs_num2_buf][35] = ""
				legs_data2_buf[legs_num2_buf][36] = 9
				legs_data2_buf[legs_num2_buf][37] = 0
				legs_data2_buf[legs_num2_buf][38] = ""
				legs_data2_buf[legs_num2_buf][39] = ""
				legs_data2_buf[legs_num2_buf][40] = 0
				legs_data2_buf[legs_num2_buf][41] = 0
				legs_data2_buf[legs_num2_buf][42] = 0
				legs_data2_buf[legs_num2_buf][43] = ""
				legs_data2_buf[legs_num2_buf][44] = 0
				legs_data2_buf[legs_num2_buf][45] = 0
				legs_data2_buf[legs_num2_buf][46] = 0
				legs_data2_buf[legs_num2_buf][47] = 0
				legs_data2_buf[legs_num2_buf][48] = ""
				legs_data2_buf[legs_num2_buf][49] = 0
				legs_data2_buf[legs_num2_buf][50] = 0
				legs_data2_buf[legs_num2_buf][51] = 0
				legs_data2_buf[legs_num2_buf][52] = 0
				legs_data2_buf[legs_num2_buf][53] = 0
				legs_data2_buf[legs_num2_buf][54] = 0
			else
				kk = 1
			end
			
			legs_num2_buf = legs_num2_buf - 1
			
			if kk == 0 then
				if in_rte == 0 then
					co_route_x = name_of_file
					ref_gate_x = "-----"
					calc_rte_enable2 = 1
					rte_calc_lat2 = 0
					rte_calc_lon2 = 0
					if legs_num ~= 0 then
						if rte12_act == 0 then
							legs_delete = 1
						-- end
							ref_exec = 1
							des_exec = 1
						end
					end
				else
					co_route7_x = name_of_file
					ref_gate7_x = "-----"
					calc_rte_enable8 = 1
					rte_calc_lat2 = 0
					rte_calc_lon2 = 0
					if legs_num ~= 0 then
						if rte12_act == 1 then
							legs_delete = 1
						-- end
							ref_exec2 = 1
							des_exec2 = 1
						end
					end
				end
				
				copy_from_legs_data2_buf(in_rte)
				fpln_import_ok = 1
				
				if in_rte == rte12_act or rte12_act < 0 then
					upload_winds(name_of_file, in_rte)
				end
			else
				if in_rte == 0 then
					legs_delete = 0
					copy_to_legsdata2(in_rte)
					
					ref_icao_x = ref_icao
					des_icao_x = des_icao
					
					ref_gate_x = "-----"
					co_route_x = co_route
					trans_alt_mod = trans_alt
					trans_lvl_mod = trans_lvl
					
					ref_tns_alt_x = ref_tns_alt
					ref_tns_lvl_x = ref_tns_lvl
					des_tns_alt_x = ref_tns_alt
					des_tns_lvl_x = ref_tns_lvl
					
					ref_rwy2 = ref_rwy
					ref_sid2 = ref_sid
					ref_sid_tns2 = ref_sid_tns
					des_app2 = des_app
					des_app_tns2 = des_app_tns
					des_star2 = des_star
					des_star_trans2 = des_star_trans
				else
					legs_delete = 0
					copy_to_legsdata2(in_rte)
					
					ref_icao7_x = ref_icao7
					des_icao7_x = des_icao7
					
					ref_gate7_x = "-----"
					co_route7_x = co_route7
					trans_alt_mod2 = trans_alt
					trans_lvl_mod2 = trans_lvl
					
					ref_tns_alt7_x = ref_tns_alt7
					ref_tns_lvl7_x = ref_tns_lvl7
					des_tns_alt7_x = ref_tns_alt7
					des_tns_lvl7_x = ref_tns_lvl7
					
					ref_rwy8 = ref_rwy7
					ref_sid8 = ref_sid7
					ref_sid_tns8 = ref_sid_tns7
					des_app8 = des_app7
					des_app_tns8 = des_app_tns7
					des_star8 = des_star7
					des_star_trans8 = des_star_trans7
				end
				add_fmc_msg("LOAD ROUTE ERR", 1)
			end
		else
			add_fmc_msg("LOAD ROUTE ERR", 1)
		end
	else
		add_fmc_msg("LOAD ROUTE ERR", 1)
	end
	
	
end


function export_fms(file_of_name, in_rte)
	
	local vvv = 0
	local www = 0
	local fms_line = ""
	local file_name2 = ""
	
	if in_rte == 0 then
		if fpln_num2 > 0 then
			file_name2 = "Output/FMS plans/" .. file_of_name .. ".fms"
			local file_navdata2 = io.open(file_name2, "w")
			
			if file_navdata2 ~= nil then
				
				-- header
				fms_line = "I\n"
				file_navdata2:write(fms_line)
				fms_line = "3 version\n"
				file_navdata2:write(fms_line)
				fms_line = "0\n"
				file_navdata2:write(fms_line)
				
				-- num waypoints
				www = fpln_num2 + 1
				fms_line = tostring(www) .. "\n"
				file_navdata2:write(fms_line)
				
				-- save REF airport
				fms_line = "1 " .. ref_icao2 .. " 0 " .. tostring(legs_data2[1][7]) .. " " .. tostring(legs_data2[1][8]) .. "\n"
				file_navdata2:write(fms_line)
				
				-- save waypoints
				for vvv = 1, fpln_num2 do
					www = fpln_data2[vvv][4] + fpln_data2[vvv][5] - 1
					if www <= legs_num2 and www > 0 then
						fms_line = "11 " .. fpln_data2[vvv][1] .. " 0 " .. tostring(legs_data2[www][7]) .. " " .. tostring(legs_data2[www][8]) .. "\n"
					end
					file_navdata2:write(fms_line)
				end
				
				www = legs_num2 + 1
				-- save DES airport
				fms_line = "1 " .. des_icao_x .. " 0 " .. tostring(legs_data2[www][7]) .. " " .. tostring(legs_data2[www][8]) .. "\n"
				file_navdata2:write(fms_line)
				
				-- empty line
				fms_line = "\n"
				file_navdata2:write(fms_line)
				
				file_navdata2:close()
			end
		end
	else
		if fpln_num4 > 0 then
			file_name2 = "Output/FMS plans/" .. file_of_name .. ".fms"
			local file_navdata2 = io.open(file_name2, "w")
			
			if file_navdata2 ~= nil then
				
				-- header
				fms_line = "I\n"
				file_navdata2:write(fms_line)
				fms_line = "3 version\n"
				file_navdata2:write(fms_line)
				fms_line = "0\n"
				file_navdata2:write(fms_line)
				
				-- num waypoints
				www = fpln_num4 + 1
				fms_line = tostring(www) .. "\n"
				file_navdata2:write(fms_line)
				
				-- save REF airport
				fms_line = "1 " .. ref_icao8 .. " 0 " .. tostring(legs_data8[1][7]) .. " " .. tostring(legs_data8[1][8]) .. "\n"
				file_navdata2:write(fms_line)
				
				-- save waypoints
				for vvv = 1, fpln_num4 do
					www = fpln_data4[vvv][4] + fpln_data4[vvv][5] - 1
					if www <= legs_num8 and www > 0 then
						fms_line = "11 " .. fpln_data4[vvv][1] .. " 0 " .. tostring(legs_data8[www][7]) .. " " .. tostring(legs_data8[www][8]) .. "\n"
					end
					file_navdata2:write(fms_line)
				end
				
				www = legs_num8 + 1
				-- save DES airport
				fms_line = "1 " .. des_icao7_x .. " 0 " .. tostring(legs_data8[www][7]) .. " " .. tostring(legs_data8[www][8]) .. "\n"
				file_navdata2:write(fms_line)
				
				-- empty line
				fms_line = "\n"
				file_navdata2:write(fms_line)
				
				file_navdata2:close()
			end
		end
	end
	
end


function import_fmx(name_of_file, in_rte)
	
	local fms_line = ""
	raw_table('fms_word')
	local fms_word = {}
	local ii = 0
	local token = ""
	
	local apt_ok = 0
	
	local rwy_tmp = ""
	local sid_tmp = ""
	local sid_tns_tmp = ""
	local star_tmp = ""
	local star_tns_tmp = ""
	local app_tmp = ""
	local app_tns_tmp = ""
	
	local flight_num_tmp = ""
	
	rte_data_num = 0
	rte_data = {}

	local file_name2 = "Output/FMS plans/" .. name_of_file .. ".fmx"
	local file_navdata2 = io.open(file_name2, "r")
	
	if file_navdata2 ~= nil then
		fms_line = file_navdata2:read()
		while fms_line do
			-- read Flight number
			if string.len(fms_line) > 11 then
				if string.sub(fms_line, 1, 11) == "FLIGHT_NUM:" then
					flight_num_tmp = string.sub(fms_line, 12, -1)
				end
			end
			
			-- split DATA
			fms_word = {}
			ii = 0
			for token in string.gmatch(fms_line, "[^,]+") do
				ii = ii + 1
				fms_word[ii] = token
			end
			
			if ii > 0 then
				if string.len(fms_word[ii]) > 1 and string.byte(fms_word[ii], -1) == 13 then	-- CR
					fms_word[ii] = string.sub(fms_word[ii], 1, -2)
				end
			end
			
			if ii == 5 then
				rte_data_num = rte_data_num + 1
				rte_data[rte_data_num] = {}
				rte_data[rte_data_num][1] = 0		-- type
				rte_data[rte_data_num][2] = fms_word[1]		-- id name
				rte_data[rte_data_num][3] = 0		-- alt
				rte_data[rte_data_num][4] = tonumber(fms_word[4])		-- lat
				rte_data[rte_data_num][5] = tonumber(fms_word[5])		-- lon
				if string.len(fms_word[3]) > 0 then
					if string.len(fms_word[3]) > 2 and string.sub(fms_word[1], 1, 2) == "RW" then
						rte_data[rte_data_num][6] = ""
					else
						rte_data[rte_data_num][6] = fms_word[3]		-- via
					end
				else
					rte_data[rte_data_num][6] = ""
				end
				rte_data[rte_data_num][7] = fms_word[2]		-- reg_code
			
			elseif ii > 1 then
				if fms_word[1] == "DEPRWY" then
					rwy_tmp = fms_word[2]
					if string.len(rwy_tmp) > 3 then
						rwy_tmp = string.sub(rwy_tmp, 3, -1)
					end
				elseif fms_word[1] == "SID" then
					sid_tmp = fms_word[2]
				elseif fms_word[1] == "SIDTRANS" then
					sid_tns_tmp = fms_word[2]
				elseif fms_word[1] == "STAR" then
					star_tmp = fms_word[2]
				elseif fms_word[1] == "STARTRANS" then
					star_tns_tmp = fms_word[2]
				elseif fms_word[1] == "APP" then
					app_tmp = fms_word[2]
				elseif fms_word[1] == "APPTRANS" then
					app_tns_tmp = fms_word[2]
				end
			end
			fms_line = file_navdata2:read()
		end
		if rte_data_num > 1 then
			rte_data[1][1] = 1
			rte_data[rte_data_num][1] = 1
		end
		file_navdata2:close()
		
		use_import_data(name_of_file, in_rte)
		
		-- add sid-star-app
		if fpln_import_ok == 1 then
			
			if in_rte == 0 then
				reverse_rte1 = 1
				
				if string.len(flight_num_tmp) > 8 then
					flt_num_x = string.sub(flight_num_tmp, 1, 8)
				else
					flt_num_x = flight_num_tmp
				end
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. ref_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. ref_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_sid(rwy_tmp, sid_tmp, sid_tns_tmp, in_rte)
				end
				
				apt_ok = 0
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. des_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. des_icao_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao_x, 0)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_star(star_tmp, star_tns_tmp, in_rte)
					import_app(app_tmp, app_tns_tmp, in_rte)
				end
			else
				reverse_rte2 = 1
				
				if string.len(flight_num_tmp) > 8 then
					flt_num_x = string.sub(flight_num_tmp, 1, 8)
				else
					flt_num_x = flight_num_tmp
				end
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. ref_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao7_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. ref_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(ref_icao7_x) == true then
							apt_ok = 1
							sid_num = 0
							sid_tns_num = 0
							star_num = 0
							ref_data_star_tns_n = 0
							ref_data_app_n = 0
							ref_data_app_tns_n = 0
							ref_ed_app_n = 0
						end
					else
						read_ref_data(ref_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_sid(rwy_tmp, sid_tmp, sid_tns_tmp, in_rte)
				end
				
				apt_ok = 0
				
				if cust_def_data == 1 then
					file_name = "Custom Data/CIFP/" .. des_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao7_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				elseif cust_def_data == 2 then
					file_name = "Resources/default data/CIFP/" .. des_icao7_x
					file_name = file_name .. ".dat"
					file_navdata = io.open(file_name, "r")
					if file_navdata == nil then
						if apt_exist(des_icao7_x) == true then
							apt_ok = 1
							data_des_star_n = 0
							data_des_star_tns_n = 0
							data_des_app_n = 0
							data_des_app_tns_n = 0
							ed_app_num = 0
						end
					else
						read_des_data(des_icao7_x, 1)		-- read reference airport data
						file_navdata:close()
						apt_ok = 1
					end
				end
				
				if apt_ok == 1 then
					import_star(star_tmp, star_tns_tmp, in_rte)
					import_app(app_tmp, app_tns_tmp, in_rte)
				end
			end
		end
	end
	
end


function import_fml(name_of_file, in_rte)
	local fms_line = ""
	raw_table('fms_word')
	local fms_word = {}
	local ii = 0
	local jj = 0
	local kk = 0
	local token = ""
	
	local fpln_active = 0
	local ref_icao_active = 0
	local ref_icao_found = 0
	local des_icao_active = 0
	local des_icao_found = 0
	local ref_rwy_found = 0
	local des_rwy_found = 0
	local trans_alt_found = 0
	local ref_rwy_map_active = 0
	local ref_rwy_map_found = 0
	
	local leg_enable = 0
	local leg_entry = 0
	local leg_id = ""
	local leg_brg = 0
	local leg_spd = 0
	local leg_alt = 0
	local leg_alt_type = 0
	local leg_dis = 0
	local leg_lat = 0
	local leg_lon = 0
	local via_id = ""
	local leg_vpa = 0
	local leg_hld_trk = -1
	local leg_hld_out_trk = 0
	local leg_hld_lft = 0
	local leg_hld_len_nm = 0
	local leg_hld_len_min = 0
	local leg_hld_spd_kts = 0
	local lat_lon_disable = 0
	local last_leg_id = ""
	local next_waypoint = 0
	local ref_idx = 0
	
	local www = des_app
	
	msg_chk_alt_constr = 0
	
	
	
	local ref_sid_imp = "-"
	local ref_sid_tns_imp = "-"
	local des_star_imp = "-"
	local des_star_tns_imp = "-"
	local des_app_imp = "-"
	local des_app_tns_imp = "-"
	
	rte_data_num = 0
	rte_data = {}
	
	raw_table('rte_data_temp')
	local rte_data_temp = {}
	
	local file_name2 = "Output/FMS plans/" .. name_of_file .. ".fml"
	local file_navdata2 = io.open(file_name2, "r")
	if file_navdata2 ~= nil then
		fms_line = file_navdata2:read()
		while fms_line do
			
			-- Reference ICAO
			if ref_icao_found == 0 then
				ii,jj = string.find(fms_line, 'class_id="37"')
				if ii ~= nil then
					ref_icao_active = 1
				end
			end
			if ref_icao_active == 1 then
				-- lat and lon
				ii,jj = string.find(fms_line, "<rad_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</rad_>")
					jj = ii - 1
					if kk > jj then
						leg_lat = 0
						leg_lon = 0
					else
						if ref_idx == 0 then
							leg_lat = string.sub(fms_line, kk, jj)
						elseif ref_idx == 1 then
							leg_lon = string.sub(fms_line, kk, jj)
						end
					end
					ref_idx = ref_idx + 1
				end
				
				ii,jj = string.find(fms_line, "<id_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</id_>")
					jj = ii - 1
					if kk > jj then
						ref_icao = "----"
					else
						rte_data_num = rte_data_num + 1
						rte_data[rte_data_num] = {}
						rte_data[rte_data_num][1] = 1		-- type
						rte_data[rte_data_num][2] = string.sub(fms_line, kk, jj)		-- id name
						rte_data[rte_data_num][3] = 0	-- alt
						rte_data[rte_data_num][4] = math.deg(tonumber(leg_lat))		-- lat
						rte_data[rte_data_num][5] = math.deg(tonumber(leg_lon))		-- lon
						rte_data[rte_data_num][6] = ""		-- via
						rte_data[rte_data_num][7] = ""		-- reg code
						
					end
					ref_icao_found = 1
					ref_icao_active = 0
				end
			end
			
			-- SID
			ii,jj = string.find(fms_line, "<sid_id_>")
			if ii ~= nil then
				kk = jj + 1
				ii,jj = string.find(fms_line, "</sid_id_>")
				jj = ii - 1
				if kk > jj then
					ref_sid_imp = "-"
				else
					ref_sid_imp = string.sub(fms_line, kk, jj)
				end
			end
			
			-- SID TRANSITION
			ii,jj = string.find(fms_line, "<sid_trans_id_>")
			if ii ~= nil then
				kk = jj + 1
				ii,jj = string.find(fms_line, "</sid_trans_id_>")
				jj = ii - 1
				if kk > jj then
					ref_sid_tns_imp = "-"
				else
					ref_sid_tns_imp = string.sub(fms_line, kk, jj)
				end
			end
			
			-- STAR
			ii,jj = string.find(fms_line, "<star_id_>")
			if ii ~= nil then
				kk = jj + 1
				ii,jj = string.find(fms_line, "</star_id_>")
				jj = ii - 1
				if kk > jj then
					des_star_imp = "-"
				else
					des_star_imp = string.sub(fms_line, kk, jj)
				end
			end
			
			-- STAR TRANSITION
			ii,jj = string.find(fms_line, "<star_trans_id_>")
			if ii ~= nil then
				kk = jj + 1
				ii,jj = string.find(fms_line, "</star_trans_id_>")
				jj = ii - 1
				if kk > jj then
					des_star_tns_imp = "-"
				else
					des_star_tns_imp = string.sub(fms_line, kk, jj)
				end
			end
			
			-- APP
			ii,jj = string.find(fms_line, "<approach_id_>")
			if ii ~= nil then
				kk = jj + 1
				ii,jj = string.find(fms_line, "</approach_id_>")
				jj = ii - 1
				if kk > jj then
					des_app_imp = "-"
				else
					des_app_imp = string.sub(fms_line, kk, jj)
				end
			end
			
			-- APP TRANSITION
			ii,jj = string.find(fms_line, "<approach_trans_id_>")
			if ii ~= nil then
				kk = jj + 1
				ii,jj = string.find(fms_line, "</approach_trans_id_>")
				jj = ii - 1
				if kk > jj then
					des_app_tns_imp = "-"
				else
					des_app_tns_imp = string.sub(fms_line, kk, jj)
				end
			end
			
			-- LEGS
			if des_icao_found == 0 then
			
			ii,jj = string.find(fms_line, "<trkToWpt>")
			if ii ~= nil then
				leg_enable = 1
				leg_entry = 4
			end
			if leg_enable == 1 then
				
				-- brg
				if leg_entry == 4 then
					ii,jj = string.find(fms_line, "<rad_>")
					if ii ~= nil then
						kk = jj + 1
						ii,jj = string.find(fms_line, "</rad_>")
						jj = ii - 1
						if kk < jj then
							leg_brg = tonumber(string.sub(fms_line, kk, jj))
						end
						leg_entry = 0
					end
				end
				
				-- distance, speed, altitude
				if leg_entry > 0 then
					ii,jj = string.find(fms_line, "<m_rep>")
					if ii ~= nil then
						kk = jj + 1
						ii,jj = string.find(fms_line, "</m_rep>")
						jj = ii - 1
						if kk < jj then
							if leg_entry == 1 then
								leg_dis = tonumber(string.sub(fms_line, kk, jj))
							elseif leg_entry == 2 then
								leg_spd = tonumber(string.sub(fms_line, kk, jj))
							elseif leg_entry == 3 then
								leg_alt = tonumber(string.sub(fms_line, kk, jj))
							end
						end
						leg_entry = 5	--0
					end
				end
				--lat, lon
				if leg_entry > 4 then
					ii,jj = string.find(fms_line, "<rad_>")
					if ii ~= nil then
						kk = jj + 1
						ii,jj = string.find(fms_line, "</rad_>")
						jj = ii - 1
						if kk < jj then
							if leg_entry == 5 then
								if lat_lon_disable == 0 then
									leg_lat = tonumber(string.sub(fms_line, kk, jj))
								end
							else
								if lat_lon_disable == 0 then
									leg_lon = tonumber(string.sub(fms_line, kk, jj))
									lat_lon_disable = 1
								end
							end
						end
						leg_entry = 0
					end
				end
				ii,jj = string.find(fms_line, "<distToWptNM>")
				if ii == nil then
					ii,jj = string.find(fms_line, "<speed_restriction_kts_>")
					if ii == nil then
						ii,jj = string.find(fms_line, "<altitude_restriction1_ft_>")
						if ii == nil then
							ii,jj = string.find(fms_line, "<lat_>")
							if ii == nil then
								ii,jj = string.find(fms_line, "<lon_>")
								if ii ~= nil then
									leg_entry = 6	-- longitude
								end
							else
								leg_entry = 5	-- latitude
							end
						else
							leg_entry = 3	-- alt restriction
						end
					else
						leg_entry = 2	-- speed restriction
					end
				else
					leg_entry = 1	-- distance
				end
				
				-- altitude type
				ii,jj = string.find(fms_line, "<altitude_restriction_type_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</altitude_restriction_type_>")
					jj = ii - 1
					if kk < jj then
						leg_alt_type = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				
				-- id
				ii,jj = string.find(fms_line, "<id_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</id_>")
					jj = ii - 1
					if kk < jj then
						if leg_id == "" then
							leg_id = string.sub(fms_line, kk, jj)
						end
					end
				end
				
				-- via id
				ii,jj = string.find(fms_line, "<via_id_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</via_id_>")
					jj = ii - 1
					if kk < jj then
						via_id = string.sub(fms_line, kk, jj)
					else
						via_id = ""
					end
					if leg_id == "" then
						leg_id = "-----"
					end
				end
				-- vpa
				ii,jj = string.find(fms_line, "<vpa_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</vpa_>")
					jj = ii - 1
					if kk < jj then
						leg_vpa = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				-- holding track
				ii,jj = string.find(fms_line, "<holding_track_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</holding_track_>")
					jj = ii - 1
					if kk < jj then
						leg_hld_trk = tonumber(string.sub(fms_line, kk, jj))
						if via_id == "" or leg_id == "-----" then
							leg_id = last_leg_id
						end
						last_leg_id = ""
					end
				end
				-- holding out track
				ii,jj = string.find(fms_line, "<holding_out_track_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</holding_out_track_>")
					jj = ii - 1
					if kk < jj then
						leg_hld_out_trk = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				-- holding left or right
				ii,jj = string.find(fms_line, "<is_left_holding_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</is_left_holding_>")
					jj = ii - 1
					if kk <= jj then
						leg_hld_lft = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				-- holding lenght nm
				ii,jj = string.find(fms_line, "<hold_leg_length_nm_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</hold_leg_length_nm_>")
					jj = ii - 1
					if kk < jj then
						leg_hld_len_nm = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				-- holding lenght min
				ii,jj = string.find(fms_line, "<hold_leg_length_min_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</hold_leg_length_min_>")
					jj = ii - 1
					if kk < jj then
						leg_hld_len_min = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				-- holding speed limit kts
				ii,jj = string.find(fms_line, "<speed_limit_kts_>")
				if ii ~= nil then
					kk = jj + 1
					ii,jj = string.find(fms_line, "</speed_limit_kts_>")
					jj = ii - 1
					if kk < jj then
						leg_hld_spd_kts = tonumber(string.sub(fms_line, kk, jj))
					end
				end
				-- save waypoint data
				next_waypoint = 0
				ii,jj = string.find(fms_line, "</item>")
				if ii ~= nil then
					next_waypoint = 1
				end
				ii,jj = string.find(fms_line, "<item>")
				if ii ~= nil then
					next_waypoint = 1
				end
				
				if next_waypoint == 1 then
					rte_data_num = rte_data_num + 1
					rte_data[rte_data_num] = {}
					rte_data[rte_data_num][1] = 11		-- type
					rte_data[rte_data_num][2] = leg_id		-- id name
					rte_data[rte_data_num][3] = 0		-- alt
					rte_data[rte_data_num][4] = math.deg(tonumber(leg_lat))		-- lat
					rte_data[rte_data_num][5] = math.deg(tonumber(leg_lon))		-- lon
					rte_data[rte_data_num][6] = via_id		-- via
					rte_data[rte_data_num][7] = ""		-- reg code
					
					last_leg_id = leg_id
					leg_id = ""
					leg_brg = 0
					leg_dis = 0
					leg_spd = 0
					leg_alt = 0
					leg_alt_type = 0
					leg_lat = 0
					leg_lon = 0
					via_id = ""
					leg_vpa = 0
					leg_hld_trk = -1
					leg_hld_out_trk = 0
					leg_hld_lft = 0
					leg_hld_len_nm = 0
					leg_hld_len_min = 0
					leg_hld_spd_kts = 0
					leg_entry = 0
					leg_enable = 0
					lat_lon_disable = 0
				end
			end
			
			end
			
			fms_line = file_navdata2:read()
		end
		file_navdata2:close()
		
		if rte_data_num > 0 then
			
			if ref_sid_tns_imp == "-" then
				-- delete SID waypoints
				if ref_sid_imp ~= "-" then
					leg_brg = 0
					leg_spd = 0
					for ii = 1, rte_data_num do
						if rte_data[ii][6] == ref_sid_imp then
							if leg_brg == 0 then
								leg_brg = ii
							end
							leg_spd = ii
						end
					end
					if leg_brg > 0 then
						jj = leg_spd - leg_brg
						rte_data[leg_spd][6] = ""
						if jj > 0 then
							jj = leg_brg - 1
							for ii = leg_spd, rte_data_num do
								jj = jj + 1
								for kk = 1, 7 do
									rte_data[jj][kk] = rte_data[ii][kk]
								end
								
							end
							rte_data_num = jj
						end
					end
				end
			else
				-- delete SID waypoints
				if ref_sid_imp ~= "-" then
					ii = 1
					while ii <= rte_data_num do
						if rte_data[ii][6] == ref_sid_imp then
							for kk = 1, 7 do
								rte_data_temp[kk] = rte_data[ii][kk]
							end
							if ii < rte_data_num then
								for jj = ii + 1, rte_data_num do
									for kk = 1, 7 do
										rte_data[jj-1][kk] = rte_data[jj][kk] 
									end
								end
							end
							rte_data_num = rte_data_num - 1
						else
							ii = ii + 1
						end
					end
				end
				
				-- delete SID TNS waypoints
				if ref_sid_tns_imp ~= "-" then
					leg_brg = 0
					leg_spd = 0
					for ii = 1, rte_data_num do
						if rte_data[ii][6] == ref_sid_tns_imp then
							if leg_brg == 0 then
								leg_brg = ii
							end
							leg_spd = ii
						end
					end
					if leg_brg > 0 then
						jj = leg_spd - leg_brg
						rte_data[leg_spd][6] = ""
						if jj > 0 then
							jj = leg_brg - 1
							for ii = leg_spd, rte_data_num do
								jj = jj + 1
								for kk = 1, 7 do
									rte_data[jj][kk] = rte_data[ii][kk]
								end
								
							end
							rte_data_num = jj
						end
					end
				end
			end
			
			-- delete STAR TNS waypoints
			if des_star_tns_imp ~= "-" then
				ii = 1
				while ii <= rte_data_num do
					if rte_data[ii][6] == des_star_tns_imp then
						for kk = 1, 7 do
							rte_data_temp[kk] = rte_data[ii][kk]
						end
						if ii < rte_data_num then
							for jj = ii + 1, rte_data_num do
								for kk = 1, 7 do
									rte_data[jj-1][kk] = rte_data[jj][kk] 
								end
							end
						end
						rte_data_num = rte_data_num - 1
					else
						ii = ii + 1
					end
				end
			end
			
			-- delete STAR waypoints
			if des_star_imp ~= "-" then
				ii = 1
				while ii <= rte_data_num do
					if rte_data[ii][6] == des_star_imp then
						for kk = 1, 7 do
							rte_data_temp[kk] = rte_data[ii][kk]
						end
						if ii < rte_data_num then
							for jj = ii + 1, rte_data_num do
								for kk = 1, 7 do
									rte_data[jj-1][kk] = rte_data[jj][kk] 
								end
							end
						end
						rte_data_num = rte_data_num - 1
					else
						ii = ii + 1
					end
				end
			end
			
			-- delete APP TNS waypoints
			if des_app_tns_imp ~= "-" then
				ii = 1
				while ii <= rte_data_num do
					if rte_data[ii][6] == des_app_tns_imp then
						for kk = 1, 7 do
							rte_data_temp[kk] = rte_data[ii][kk]
						end
						if ii < rte_data_num then
							for jj = ii + 1, rte_data_num do
								for kk = 1, 7 do
									rte_data[jj-1][kk] = rte_data[jj][kk] 
								end
							end
						end
						rte_data_num = rte_data_num - 1
					else
						ii = ii + 1
					end
				end
			end
			
			-- delete APP waypoints
			if des_app_imp ~= "-" then
				ii = 1
				while ii <= rte_data_num do
					if rte_data[ii][6] == des_app_imp then
						for kk = 1, 7 do
							rte_data_temp[kk] = rte_data[ii][kk]
						end
						if ii < rte_data_num then
							for jj = ii + 1, rte_data_num do
								for kk = 1, 7 do
									rte_data[jj-1][kk] = rte_data[jj][kk] 
								end
							end
						end
						rte_data_num = rte_data_num - 1
					else
						ii = ii + 1
					end
				end
			end
			
			if string.len(rte_data[rte_data_num][2]) == 4 then
				rte_data[rte_data_num][1] = 1
			end
		end
		
		--dump_rte_data()
		
		use_import_data(name_of_file, in_rte)
		
	end
	
end


function load_cust_wpt()
	
	local fms_line = ""
	raw_table('fms_word')
	local fms_word = {}
	local ii = 0
	local token = ""
	
	-- wptx_n = 0
	-- wptx = {}

	local file_name2 = FILE_NAME_CUST_DAT	--"Custom Data/B738X_wptx.dat"
	local file_navdata2 = io.open(file_name2, "r")
	
	if file_navdata2 ~= nil then
		fms_line = file_navdata2:read()
		while fms_line do
			
			-- split DATA
			fms_word = {}
			ii = 0
			for token in string.gmatch(fms_line, "[^,]+") do
				ii = ii + 1
				fms_word[ii] = token
			end
			
			if ii > 0 then
				if string.len(fms_word[ii]) > 1 and string.byte(fms_word[ii], -1) == 13 then	-- CR
					fms_word[ii] = string.sub(fms_word[ii], 1, -2)
				end
			end
			
			if ii == 8 then
				wptx_n = wptx_n + 1
				wptx[wptx_n] = {true, true, true, true, true, true, true, true}
				wptx[wptx_n][1] = fms_word[1]		-- id name
				wptx[wptx_n][2] = tonumber(fms_word[2])		-- lat
				wptx[wptx_n][3] = tonumber(fms_word[3])		-- lon
				wptx[wptx_n][4] = fms_word[4]		-- rec navaid
				wptx[wptx_n][5] = fms_word[5]		-- rec navaid rc
				wptx[wptx_n][6] = fms_word[6]		-- brg
				wptx[wptx_n][7] = fms_word[7]		-- distance
				wptx[wptx_n][8] = fms_word[8]		-- mag var
			end
			
			fms_line = file_navdata2:read()
		end
		
		file_navdata2:close()
	
	end
end

function save_cust_wpt()
	
	local vvv = 0
	local fms_line = ""
	local file_name2 = ""
	
	if wptx_n > 0 then
		file_name2 = FILE_NAME_CUST_DAT	--"Custom Data/B738X_wptx.dat"
		local file_navdata2 = io.open(file_name2, "w")
		
		if file_navdata2 ~= nil then
			for vvv = 1, wptx_n do
				fms_line = wptx[vvv][1] .. "," .. tostring(wptx[vvv][2]) .. "," .. tostring(wptx[vvv][3]) .. ","
				fms_line = fms_line .. wptx[vvv][4] .. "," .. wptx[vvv][5] .. "," .. wptx[vvv][6] .. "," .. wptx[vvv][7] .. ","
				fms_line = fms_line .. wptx[vvv][8] .. "\n"
				file_navdata2:write(fms_line)
			end
			file_navdata2:close()
		end
	end
	
end


function dump_rte_data()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if rte_data_num > 0 then
			for vvv = 1, rte_data_num  do
--				if awy_temp2[vvv][1] == "R232" then
					fms_line = tostring(rte_data[vvv][1]) .. "," .. rte_data[vvv][2] .. "," .. rte_data[vvv][3] .. "," .. tostring(rte_data[vvv][4]) 
					fms_line = fms_line .. "," .. tostring(rte_data[vvv][5]) .. "," .. rte_data[vvv][6] .. "\n"
					file_navdata2:write(fms_line)
--				end
			end
		end
		file_navdata2:close()
	end
end


function load_fpln(name_of_file, in_rte)
	
	local file_name2 = "Output/FMS plans/" .. name_of_file .. ".fmx"
	local file_navdata2 = io.open(file_name2, "r")
	local fms_line = ""
	local fms_ver = 0
	local result = 1
	
	if file_navdata2 == nil then
		
		file_name2 = "Output/FMS plans/" .. name_of_file .. ".fml"
		file_navdata2 = io.open(file_name2, "r")
		if file_navdata2 == nil then
			
			file_name2 = "Output/FMS plans/" .. name_of_file .. ".fms"
			file_navdata2 = io.open(file_name2, "r")
			if file_navdata2 == nil then 
				add_fmc_msg(INVALID_INPUT, 1)
				result = 0
			else
				-- check v3 or v11
				fms_line = file_navdata2:read()
				while fms_line do
					if string.sub(fms_line, 1, 4) == "1100" then
						fms_ver = 1
						break
					end
					fms_line = file_navdata2:read()
				end
				file_navdata2:close()
				if fms_ver == 0 then
					import_fms(name_of_file, in_rte)
				else
					import_fms_v11(name_of_file, in_rte)
				end
			end
		else
			file_navdata2:close()
			import_fml(name_of_file, in_rte)
		end
	else
		file_navdata2:close()
		import_fmx(name_of_file, in_rte)
	end
	
	return result
end


function export_fmx(file_of_name, in_rte)
	
	local vvv = 0
	local www = 0
	local fms_line = ""
	local file_name2 = ""
	
	if in_rte == 0 then
		if legs_num2 > 1 then
			file_name2 = "Output/FMS plans/" .. file_of_name .. ".fmx"
			local file_navdata2 = io.open(file_name2, "w")
			
			if file_navdata2 ~= nil then
				www = legs_num2 + 1
				for vvv = 1, www do
					if legs_data2[vvv][19] == 0 or legs_data2[vvv][19] == 3 or legs_data2[vvv][19] == 4 then
						if ref_rwy2 == "-----" or vvv ~= 2 then
							fms_line = legs_data2[vvv][1] .. "," 
							if legs_data2[vvv][16] == "" then
								fms_line = fms_line .. " " .. ","
							else
								fms_line = fms_line .. legs_data2[vvv][16] .. ","
							end
							if legs_data2[vvv][9] == "" then
								fms_line = fms_line .. " " .. ","
							else
								fms_line = fms_line .. legs_data2[vvv][9] .. ","
							end
							fms_line = fms_line .. tostring(legs_data2[vvv][7]) .. "," .. tostring(legs_data2[vvv][8]) .. "\n"
							file_navdata2:write(fms_line)
						end
					end
				end
				
				-- save data
				if ref_rwy2 ~= "-----" then
					fms_line = "DEPRWY," .. ref_rwy2 .. "\n"
					file_navdata2:write(fms_line)
				end
				if ref_sid2 ~= "------" then
					fms_line = "SID," .. ref_sid2 .. "\n"
					file_navdata2:write(fms_line)
				end
				if ref_sid_tns2 ~= "------" then
					fms_line = "SIDTRANS," .. ref_sid_tns2 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_star2 ~= "------" then
					fms_line = "STAR," .. des_star2 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_star_trans2 ~= "------" then
					fms_line = "STARTRANS," .. des_star_trans2 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_app2 ~= "------" then
					fms_line = "APP," .. des_app2 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_app_tns2 ~= "------" then
					fms_line = "APPTRANS," .. des_app_tns2 .. "\n"
					file_navdata2:write(fms_line)
				end
				
				fms_line = "FLIGHT_NUM:" .. flt_num_x .. "\n"
				file_navdata2:write(fms_line)
				
				file_navdata2:close()
			end
		end
	elseif in_rte == 1 then
		if legs_num8 > 1 then
			file_name2 = "Output/FMS plans/" .. file_of_name .. ".fmx"
			local file_navdata2 = io.open(file_name2, "w")
			
			if file_navdata2 ~= nil then
				www = legs_num8 + 1
				for vvv = 1, www do
					if legs_data8[vvv][19] == 0 or legs_data8[vvv][19] == 3 or legs_data8[vvv][19] == 4 then
						if ref_rwy8 == "-----" or vvv ~= 2 then
							fms_line = legs_data8[vvv][1] .. "," 
							if legs_data8[vvv][16] == "" then
								fms_line = fms_line .. " " .. ","
							else
								fms_line = fms_line .. legs_data8[vvv][16] .. ","
							end
							if legs_data8[vvv][9] == "" then
								fms_line = fms_line .. " " .. ","
							else
								fms_line = fms_line .. legs_data8[vvv][9] .. ","
							end
							fms_line = fms_line .. tostring(legs_data8[vvv][7]) .. "," .. tostring(legs_data8[vvv][8]) .. "\n"
							file_navdata2:write(fms_line)
						end
					end
				end
				
				-- save data
				if ref_rwy8 ~= "-----" then
					fms_line = "DEPRWY," .. ref_rwy8 .. "\n"
					file_navdata2:write(fms_line)
				end
				if ref_sid8 ~= "------" then
					fms_line = "SID," .. ref_sid8 .. "\n"
					file_navdata2:write(fms_line)
				end
				if ref_sid_tns8 ~= "------" then
					fms_line = "SIDTRANS," .. ref_sid_tns8 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_star8 ~= "------" then
					fms_line = "STAR," .. des_star8 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_star_trans8 ~= "------" then
					fms_line = "STARTRANS," .. des_star_trans8 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_app8 ~= "------" then
					fms_line = "APP," .. des_app8 .. "\n"
					file_navdata2:write(fms_line)
				end
				if des_app_tns8 ~= "------" then
					fms_line = "APPTRANS," .. des_app_tns8 .. "\n"
					file_navdata2:write(fms_line)
				end
				
				fms_line = "FLIGHT_NUM:" .. flt_num_x .. "\n"
				file_navdata2:write(fms_line)
				
				file_navdata2:close()
			end
		end
	end
end

function save_fpln(file_of_name, in_rte)
	
	if B738DR_fpln_format == 0 then
		export_fmx(file_of_name, in_rte)
	else
		export_fms(file_of_name, in_rte)
	end
	
end


function mag_variation_deg(lat_mag, lon_mag)
	local result = 0
	find_lat_mag_var = lat_mag
	find_lon_mag_var = lon_mag
	find_calc_mag_var = 1
	result = find_mag_var
	return result
end

function mag_variation_rad(lat_mag, lon_mag)
	local result = 0
	find_lat_mag_var = math.deg(lat_mag)
	find_lon_mag_var = math.deg(lon_mag)
	find_calc_mag_var = 1
	result = find_mag_var
	return math.rad(result)
end

function update_offset_idx()
	local offset_start_tmp = 0
	local offset_end_tmp = 0
	local in_offset = offset_start
	if in_offset == 0 then
		in_offset = offset
	end
	
	if legs_num > 1 then
		-- for ii = 2, legs_num do
		for ii = in_offset, legs_num do
			if legs_data[ii][45] ~= 0 then
				if offset_start_tmp == 0 then
					offset_start_tmp = ii
				end
				offset_end_tmp = ii
			end
		end
	end
	offset_start = offset_start_tmp
	offset_end = offset_end_tmp
end

function update_offset_idx2(in_rte)
	local offset_start_tmp = 0
	local offset_end_tmp = 0
	local offset_start_def_mod_tmp = 0
	local offset_end_def_mod_tmp = 0
	local in_offset = offset_start_mod
	if in_offset == 0 then
		in_offset = offset
	end
	
	local wpt_available = 0
	local wpt_break = 0
	local wpt_rel_brg = 0
	
	if in_rte == 0 then
		if legs_num2 > 1 and in_offset > 0 and in_offset <= legs_num2 then
			for ii = in_offset, legs_num2 do
				if legs_data2[ii][45] ~= 0 then
					if offset_start_tmp == 0 then
						offset_start_tmp = ii
					end
					offset_end_tmp = ii
				end
			end
		end
		offset_start_def_mod = offset_start_tmp
		offset_end_def_mod = offset_end_tmp
		if offset_start_mod ~= 0 then
			offset_start_mod = offset_start_tmp
		end
		if offset_end_mod ~= 0 then
			offset_end_mod = offset_end_tmp
		end
	elseif in_rte == 1 then
		if legs_num8 > 1 and in_offset > 0 and in_offset <= legs_num8 then
			for ii = in_offset, legs_num8 do
				if legs_data8[ii][45] ~= 0 then
					if offset_start_tmp == 0 then
						offset_start_tmp = ii
					end
					offset_end_tmp = ii
				end
			end
		end
		offset_start_def_mod = offset_start_tmp
		offset_end_def_mod = offset_end_tmp
		if offset_start_mod ~= 0 then
			offset_start_mod = offset_start_tmp
		end
		if offset_end_mod ~= 0 then
			offset_end_mod = offset_end_tmp
		end
	end
end

function offset_check_act()
	if offset_act < 5 then
		offset_start_def = 0
	end
	offset_end_def_mod = 0
	local wpt_available = 0
	local end_wpt_available = 0
	local wpt_rel_brg = 0
	local in_offset = offset_start
	if in_offset == 0 then
		in_offset = offset
	end
	local jj = 0
	local kk = 0
	local offset_pass = false
	
	if legs_num > 2 and offset < legs_num and offset > 0 and in_offset > 0 and in_offset < legs_num then
		for ii = in_offset, legs_num do
			wpt_available = 1
			end_wpt_available = 1
			if ii < 2 then
				wpt_available = 0
				end_wpt_available = 0
			elseif legs_data[ii][1] == "(VECTOR)" or legs_data[ii][1] == "DISCONTINUITY" then
				wpt_available = 0
				end_wpt_available = 0
			elseif legs_data[ii][31] ~= "" and legs_data[ii][31] ~= "TF" and legs_data[ii][31] ~= "IF" then
				wpt_available = 0
				end_wpt_available = 0
			else
				if legs_data[ii][19] ~= 0 and legs_data[ii][19] ~= 4 and legs_data[ii][19] ~= 2 then
					wpt_available = 0
				end
				if legs_data[ii-1][19] ~= 0 and legs_data[ii-1][19] ~= 4 and legs_data[ii-1][19] ~= 2 then
					end_wpt_available = 0
				end
			end
			if wpt_available ~= 0 or end_wpt_available ~= 0 then
				wpt_rel_brg = (math.deg(legs_data[ii-1][2]) - math.deg(legs_data[ii][2]) + 360) % 360
				if wpt_rel_brg > 180 then
					wpt_rel_brg = wpt_rel_brg - 360
				end
				if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
					wpt_available = 0
					end_wpt_available = 0
				end
			end
			
			if wpt_available ~= 0 then
				if offset_start_def == 0 then
					offset_start_def = ii - 1
				end
				offset_end_def = ii
				offset_pass = true
			elseif end_wpt_available ~= 0 and offset_start_def ~= 0 then
				offset_end_def = ii
				offset_pass = true
			end
			
			if offset_pass == true and wpt_available == 0 and end_wpt_available == 0 then
				break
			end
		end
	end
end

function offset_check(in_rte)
	if offset_act < 5 then
		offset_start_def_mod = 0
	end
	offset_end_def_mod = 0
	local wpt_available = 0
	local end_wpt_available = 0
	local wpt_rel_brg = 0
	local jj = 0
	local kk = 0
	local offset_pass = false
	local in_offset = offset_start_mod
	if in_offset == 0 then
		in_offset = offset
	end
	
	if in_rte == 0 then
		if legs_num2 > 2 and offset < legs_num2 and offset > 0 and in_offset > 0 and in_offset < legs_num2 then
			for ii = in_offset, legs_num2 do
				wpt_available = 1
				end_wpt_available = 1
				if ii < 2 then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data2[ii][1] == "(VECTOR)" or legs_data2[ii][1] == "DISCONTINUITY" then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data2[ii][31] ~= "" and legs_data2[ii][31] ~= "TF" and legs_data2[ii][31] ~= "IF" then
					wpt_available = 0
					end_wpt_available = 0
				else
					if legs_data2[ii][19] ~= 0 and legs_data2[ii][19] ~= 4 and legs_data2[ii][19] ~= 2 then
						wpt_available = 0
					end
					if legs_data2[ii-1][19] ~= 0 and legs_data2[ii-1][19] ~= 4 and legs_data2[ii-1][19] ~= 2 then
						end_wpt_available = 0
					end
				end
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					wpt_rel_brg = (math.deg(legs_data2[ii-1][2]) - math.deg(legs_data2[ii][2]) + 360) % 360
					if wpt_rel_brg > 180 then
						wpt_rel_brg = wpt_rel_brg - 360
					end
					if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
						wpt_available = 0
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 then
					if offset_start_def_mod == 0 then
						offset_start_def_mod = ii - 1
					end
					offset_end_def_mod = ii
					offset_pass = true
				elseif end_wpt_available ~= 0 and offset_start_def_mod ~= 0 then
					offset_end_def_mod = ii
					offset_pass = true
				end
				
				if offset_pass == true and wpt_available == 0 and end_wpt_available == 0 then
					break
				end
			end
		end
		if offset_start_def_mod ~= 0 then
			offset_pass = false
			if offset_start_mod == 0 then
				--check distance
				jj = offset_start_def_mod
				kk = 0
				while jj <= offset_end_def_mod do
					if jj == offset and jj == offset_start_def_mod then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num2 then
						kk = kk + legs_data2[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			elseif offset_start_def_mod < offset_end_def_mod then
				--check distance
				jj = offset_start_def_mod + 1
				kk = 0
				while jj <= offset_end_def_mod do
					if jj == offset and jj == offset_start_def_mod + 1 then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num2 then
						kk = kk + legs_data2[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			end
			if offset_pass == false then
				offset_start_def_mod = 0
				offset_end_def_mod = 0
			end
		end
	elseif in_rte == 1 then
		if legs_num8 > 2 and offset < legs_num8 and offset > 0 and in_offset > 0 and in_offset < legs_num8 then
			for ii = in_offset, legs_num8 do
				wpt_available = 1
				end_wpt_available = 1
				if ii < 2 then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data8[ii][1] == "(VECTOR)" or legs_data8[ii][1] == "DISCONTINUITY" then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data8[ii][31] ~= "" and legs_data8[ii][31] ~= "TF" and legs_data8[ii][31] ~= "IF" then
					wpt_available = 0
					end_wpt_available = 0
				else
					if legs_data8[ii][19] ~= 0 and legs_data8[ii][19] ~= 4 and legs_data8[ii][19] ~= 2 then
						wpt_available = 0
					end
					if legs_data8[ii-1][19] ~= 0 and legs_data8[ii-1][19] ~= 4 and legs_data8[ii-1][19] ~= 2 then
						end_wpt_available = 0
					end
				end
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					wpt_rel_brg = (math.deg(legs_data8[ii-1][2]) - math.deg(legs_data8[ii][2]) + 360) % 360
					if wpt_rel_brg > 180 then
						wpt_rel_brg = wpt_rel_brg - 360
					end
					if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
						wpt_available = 0
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 then
					if offset_start_def_mod == 0 then
						offset_start_def_mod = ii - 1
					end
					offset_end_def_mod = ii
					offset_pass = true
				elseif end_wpt_available ~= 0 and offset_start_def_mod ~= 0 then
					offset_end_def_mod = ii
					offset_pass = true
				end
				
				if offset_pass == true and wpt_available == 0 and end_wpt_available == 0 then
					break
				end
			end
		end
		if offset_start_def_mod ~= 0 then
			offset_pass = false
			if offset_start_mod == 0 then
				--check distance
				jj = offset_start_def_mod
				kk = 0
				while jj <= offset_end_def_mod do
					if jj == offset and jj == offset_start_def_mod then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num8 then
						kk = kk + legs_data8[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			elseif offset_start_def_mod < offset_end_def_mod then
				--check distance
				jj = offset_start_def_mod + 1
				kk = 0
				while jj <= offset_end_def_mod do
					if jj == offset and jj == offset_start_def_mod + 1 then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num8 then
						kk = kk + legs_data8[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			end
			if offset_pass == false then
				offset_start_def_mod = 0
				offset_end_def_mod = 0
			end
		end
	end
end

function offset_check1(off_leg_id, in_rte)
	
	local ii = 0
	local in_end2 = offset_end_mod
	if in_end2 == 0 then
		in_end2 = offset_end_def_mod
	end
	
	offset_start_mod = 0
	
	local wpt_available = 0
	local end_wpt_available = 0
	local wpt_rel_brg = 0
	local jj = 0
	local kk = 0
	local offset_pass = false
	
	if in_rte == 0 then
		if legs_num2 > 2 and offset < legs_num2 and offset > 1 then
			for ii = offset, legs_num2 do
				wpt_available = 1
				end_wpt_available = 1
				if ii < 2 then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data2[ii][1] == "(VECTOR)" or legs_data2[ii][1] == "DISCONTINUITY" then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data2[ii][31] ~= "" and legs_data2[ii][31] ~= "TF" and legs_data2[ii][31] ~= "IF" then
					wpt_available = 0
					end_wpt_available = 0
				else
					if legs_data2[ii][19] ~= 0 and legs_data2[ii][19] ~= 4 and legs_data2[ii][19] ~= 2 then
						wpt_available = 0
					end
					if legs_data2[ii-1][19] ~= 0 and legs_data2[ii-1][19] ~= 4 and legs_data2[ii-1][19] ~= 2 then
						end_wpt_available = 0
					end
				end
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					wpt_rel_brg = (math.deg(legs_data2[ii-1][2]) - math.deg(legs_data2[ii][2]) + 360) % 360
					if wpt_rel_brg > 180 then
						wpt_rel_brg = wpt_rel_brg - 360
					end
					if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
						wpt_available = 0
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 then
					if legs_data2[ii][1] == off_leg_id then
						offset_start_mod = ii
						break
					elseif legs_data2[ii-1][1] == off_leg_id and offset == ii - 1 then
						offset_start_mod = ii - 1
						break
					end
				end
			end
		end
		-- if offset_start_mod > in_end2 then
			-- offset_start_mod = 0
		-- end
		if offset_start_def_mod ~= 0 then
			offset_pass = false
			if offset_start_mod == 0 then
				--check distance
				jj = offset_start_def_mod
				kk = 0
				while jj <= in_end2 do
					if jj == offset and jj == offset_start_def_mod then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num2 then
						kk = kk + legs_data2[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			elseif offset_start_mod < in_end2 then
				--check distance
				jj = offset_start_mod + 1
				kk = 0
				while jj <= in_end2 do
					if jj == offset and jj == offset_start_def_mod + 1 then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num2 then
						kk = kk + legs_data2[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			end
			if offset_pass == false then
				offset_start_mod = 0
			end
		else
			offset_start_mod = 0
		end
	elseif in_rte == 1 then
		if legs_num8 > 2 and offset < legs_num8 and offset > 1 then
			for ii = offset, legs_num8 do
				wpt_available = 1
				end_wpt_available = 1
				if ii < 2 then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data8[ii][1] == "(VECTOR)" or legs_data8[ii][1] == "DISCONTINUITY" then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data8[ii][31] ~= "" and legs_data8[ii][31] ~= "TF" and legs_data8[ii][31] ~= "IF" then
					wpt_available = 0
					end_wpt_available = 0
				else
					if legs_data8[ii][19] ~= 0 and legs_data8[ii][19] ~= 4 and legs_data8[ii][19] ~= 2 then
						wpt_available = 0
					end
					if legs_data8[ii-1][19] ~= 0 and legs_data8[ii-1][19] ~= 4 and legs_data8[ii-1][19] ~= 2 then
						end_wpt_available = 0
					end
				end
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					wpt_rel_brg = (math.deg(legs_data8[ii-1][2]) - math.deg(legs_data8[ii][2]) + 360) % 360
					if wpt_rel_brg > 180 then
						wpt_rel_brg = wpt_rel_brg - 360
					end
					if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
						wpt_available = 0
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 then
					if legs_data8[ii][1] == off_leg_id then
						offset_start_mod = ii
						break
					elseif legs_data8[ii-1][1] == off_leg_id and offset == ii - 1 then
						offset_start_mod = ii - 1
						break
					end
				end
			end
		end
		-- if offset_start_mod > in_end2 then
			-- offset_start_mod = 0
		-- end
		if offset_start_def_mod ~= 0 then
			offset_pass = false
			if offset_start_mod == 0 then
				--check distance
				jj = offset_start_def_mod
				kk = 0
				while jj <= in_end2 do
					if jj == offset and jj == offset_start_def_mod then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num8 then
						kk = kk + legs_data8[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			elseif offset_start_mod < in_end2 then
				--check distance
				jj = offset_start_mod + 1
				kk = 0
				while jj <= in_end2 do
					if jj == offset and jj == offset_start_def_mod + 1 then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num8 then
						kk = kk + legs_data8[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			end
			if offset_pass == false then
				offset_start_mod = 0
			end
		else
			offset_start_mod = 0
		end
	end
end

function offset_check2(off_leg_id, in_rte)
	
	local ii = 0
	local in_start2 = offset_start_mod
	if in_start2 == 0 then
		in_start2 = offset_start_def_mod
	end
	
	offset_end_mod = 0
	local wpt_available = 0
	local end_wpt_available = 0
	local wpt_rel_brg = 0
	local jj = 0
	local kk = 0
	local offset_pass = false
	
	if in_rte == 0 then
		if legs_num2 > 2 and in_start2 <= legs_num2 and in_start2 > 1 then
			for ii = in_start2, legs_num2 do
				wpt_available = 1
				end_wpt_available = 1
				if ii < 2 then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data2[ii][1] == "(VECTOR)" or legs_data2[ii][1] == "DISCONTINUITY" then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data2[ii][31] ~= "" and legs_data2[ii][31] ~= "TF" and legs_data2[ii][31] ~= "IF" then
					wpt_available = 0
					end_wpt_available = 0
				else
					if legs_data2[ii][19] ~= 0 and legs_data2[ii][19] ~= 4 and legs_data2[ii][19] ~= 2 then
						wpt_available = 0
					end
					if legs_data2[ii-1][19] ~= 0 and legs_data2[ii-1][19] ~= 4 and legs_data2[ii-1][19] ~= 2 then
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					wpt_rel_brg = (math.deg(legs_data2[ii-1][2]) - math.deg(legs_data2[ii][2]) + 360) % 360
					if wpt_rel_brg > 180 then
						wpt_rel_brg = wpt_rel_brg - 360
					end
					if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
						wpt_available = 0
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					if off_leg_id == legs_data2[ii][1] then
						offset_end_mod = ii
						break
					end
				else
					break
				end
			end
		end
		-- if offset_end_mod < in_start2 then
			-- offset_end_mod = 0
		-- end
		
		if offset_start_def_mod ~= 0 then
			offset_pass = false
			if offset_start_mod == 0 then
				--check distance
				jj = offset_start_def_mod
				kk = 0
				while jj <= offset_end_mod do
					if jj == offset and jj == offset_start_def_mod then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num2 then
						kk = kk + legs_data2[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			elseif in_start2 < offset_end_mod then
				--check distance
				jj = in_start2 + 1
				kk = 0
				while jj <= offset_end_mod do
					if jj == offset and jj == in_start2 + 1 then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num2 then
						kk = kk + legs_data2[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			end
			if offset_pass == false then
				offset_end_mod = 0
			end
		else
			offset_end_mod = 0
		end
	elseif in_rte == 1 then
		if legs_num8 > 2 and in_start2 <= legs_num8 and in_start2 > 1 then
			for ii = in_start2, legs_num8 do
				wpt_available = 1
				end_wpt_available = 1
				if ii < 2 then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data8[ii][1] == "(VECTOR)" or legs_data8[ii][1] == "DISCONTINUITY" then
					wpt_available = 0
					end_wpt_available = 0
				elseif legs_data8[ii][31] ~= "" and legs_data8[ii][31] ~= "TF" and legs_data8[ii][31] ~= "IF" then
					wpt_available = 0
					end_wpt_available = 0
				else
					if legs_data8[ii][19] ~= 0 and legs_data8[ii][19] ~= 4 and legs_data8[ii][19] ~= 2 then
						wpt_available = 0
					end
					if legs_data8[ii-1][19] ~= 0 and legs_data8[ii-1][19] ~= 4 and legs_data8[ii-1][19] ~= 2 then
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					wpt_rel_brg = (math.deg(legs_data8[ii-1][2]) - math.deg(legs_data8[ii][2]) + 360) % 360
					if wpt_rel_brg > 180 then
						wpt_rel_brg = wpt_rel_brg - 360
					end
					if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
						wpt_available = 0
						end_wpt_available = 0
					end
				end
				
				if wpt_available ~= 0 or end_wpt_available ~= 0 then
					if off_leg_id == legs_data8[ii][1] then
						offset_end_mod = ii
						break
					end
				else
					break
				end
			end
		end
		-- if offset_end_mod < in_start2 then
			-- offset_end_mod = 0
		-- end
		if offset_start_def_mod ~= 0 then
			offset_pass = false
			if offset_start_mod == 0 then
				--check distance
				jj = offset_start_def_mod
				kk = 0
				while jj <= offset_end_mod do
					if jj == offset and jj == offset_start_def_mod then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num8 then
						kk = kk + legs_data8[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			elseif in_start2 < offset_end_mod then
				--check distance
				jj = in_start2 + 1
				kk = 0
				while jj <= offset_end_mod do
					if jj == offset and jj == in_start2 + 1 then
						kk = kk + B738DR_fmc_dist
					elseif jj <= legs_num8 then
						kk = kk + legs_data8[jj][3]
					end
					if kk > (2 * offset_dist_mod) + 1 then
						offset_pass = true
						break
					end
					jj = jj + 1
				end
			end
			if offset_pass == false then
				offset_end_mod = 0
			end
		else
			offset_end_mod = 0
		end
	end
end

function lat_lon_legs(in_idx)
	local out_lat = 0
	local out_lon = 0
	if legs_num > 0 then
		if in_idx <= legs_num + 1 then
			out_lat = legs_data[in_idx][7]
			out_lon = legs_data[in_idx][8]
		end
	end
	return out_lat, out_lon
end

function lat_lon_legs2(in_idx)
	local out_lat = 0
	local out_lon = 0
	if legs_num2 > 0 then
		if in_idx <= legs_num2 + 1 then
			out_lat = legs_data2[in_idx][7]
			out_lon = legs_data2[in_idx][8]
		end
	end
	return out_lat, out_lon
end

function lat_lon_legs8(in_idx)
	local out_lat = 0
	local out_lon = 0
	if legs_num8 > 0 then
		if in_idx <= legs_num8 + 1 then
			out_lat = legs_data8[in_idx][7]
			out_lon = legs_data8[in_idx][8]
		end
	end
	return out_lat, out_lon
end

function check_lat_offset()
	local use_calc_rte_enable = 0
	if rte12_act == 0 then
		use_calc_rte_enable = calc_rte_enable2
	elseif rte12_act == 1 then
		use_calc_rte_enable = calc_rte_enable8
	end
	if legs_intdir == 1 and offset_dist_mod ~= 0 and use_calc_rte_enable == 0 then
		offset_start_def_mod = 0
		offset_end_def_mod = 0
		offset_act_mod = 0
		offset_start_mod = 0
		offset_end_mod = 0
		offset_dist_mod = 0
		offset_side_mod = 0
	end
	
	local offset_end_mod_in = offset_end_mod
	if offset_end_mod_in == 0 then
		offset_end_mod_in = offset_end_def_mod
	end
	
	if offset_act ~= offset_act_mod and offset_act_mod >= 2 and use_calc_rte_enable == 0 and legs_num > 1 then
		
		local crs1 = 0
		local crs2 = 0
		local crs3 = 0
		local dist1 = 0
		local dist2 = 0
		
		if offset_act_mod == 3 and offset_end_mod_in ~= 0 then
			-- PPOS entry
			B738DR_fms_legs_offset_latx_1_mod = simDR_latitude
			B738DR_fms_legs_offset_lonx_1_mod = simDR_longitude
			B738DR_fms_legs_offset_latx_2_mod = 0
			B738DR_fms_legs_offset_lonx_2_mod = 0
			
			if rte12_act == 0 then
				crs1 = nd_calc_brg(legs_data2[offset][46], legs_data2[offset][47], simDR_latitude, simDR_longitude)
				crs1 = (crs1 - math.deg(legs_data2[offset][37]) + 180) % 360
				if crs1 > 180 then
					crs1 = crs1 - 360
				end
				if crs1 >= 0 then
					crs1 = (math.deg(legs_data2[offset][2]) + 45) % 360
				else
					crs1 = (math.deg(legs_data2[offset][2]) + 315) % 360
				end
				calc_brg_brg(simDR_latitude, simDR_longitude, math.rad(crs1), legs_data2[offset][46], legs_data2[offset][47], math.rad((math.deg(legs_data2[offset][37]) + 180) % 360))
				if calc_lat == 0 and calc_lon == 0 then
					if offset_end_mod_in >= offset + 1 and offset < legs_num2 then
						crs1 = nd_calc_brg(legs_data2[offset+1][46], legs_data2[offset+1][47], simDR_latitude, simDR_longitude)
						crs1 = (crs1 - math.deg(legs_data2[offset+1][37]) + 180) % 360
						if crs1 > 180 then
							crs1 = crs1 - 360
						end
						if crs1 >= 0 then
							crs1 = (math.deg(legs_data2[offset+1][2]) + 45) % 360
						else
							crs1 = (math.deg(legs_data2[offset+1][2]) + 315) % 360
						end
						calc_brg_brg(simDR_latitude, simDR_longitude, math.rad(crs1), legs_data2[offset+1][46], legs_data2[offset+1][47], math.rad((math.deg(legs_data2[offset+1][37]) + 180) % 360))
						if calc_lat == 0 and calc_lon == 0 then
							B738DR_fms_legs_offset_latx_2_mod = legs_data2[offset][7]
							B738DR_fms_legs_offset_lonx_2_mod = legs_data2[offset][8]
						else
							B738DR_fms_legs_offset_latx_2_mod = calc_lat
							B738DR_fms_legs_offset_lonx_2_mod = calc_lon
						end
					else
						B738DR_fms_legs_offset_latx_2_mod = legs_data2[offset][7]
						B738DR_fms_legs_offset_lonx_2_mod = legs_data2[offset][8]
					end
				else
					B738DR_fms_legs_offset_latx_2_mod = calc_lat
					B738DR_fms_legs_offset_lonx_2_mod = calc_lon
				end
			elseif rte12_act == 1 then
				crs1 = nd_calc_brg(legs_data8[offset][46], legs_data8[offset][47], simDR_latitude, simDR_longitude)
				crs1 = (crs1 - math.deg(legs_data8[offset][37]) + 180) % 360
				if crs1 > 180 then
					crs1 = crs1 - 360
				end
				if crs1 >= 0 then
					crs1 = (math.deg(legs_data8[offset][2]) + 45) % 360
				else
					crs1 = (math.deg(legs_data8[offset][2]) + 315) % 360
				end
				calc_brg_brg(simDR_latitude, simDR_longitude, math.rad(crs1), legs_data8[offset][46], legs_data8[offset][47], math.rad((math.deg(legs_data8[offset][37]) + 180) % 360))
				if calc_lat == 0 and calc_lon == 0 then
					if offset_end_mod_in >= offset + 1 and offset < legs_num8 then
						crs1 = nd_calc_brg(legs_data8[offset+1][46], legs_data8[offset+1][47], simDR_latitude, simDR_longitude)
						crs1 = (crs1 - math.deg(legs_data8[offset+1][37]) + 180) % 360
						if crs1 > 180 then
							crs1 = crs1 - 360
						end
						if crs1 >= 0 then
							crs1 = (math.deg(legs_data8[offset+1][2]) + 45) % 360
						else
							crs1 = (math.deg(legs_data8[offset+1][2]) + 315) % 360
						end
						calc_brg_brg(simDR_latitude, simDR_longitude, math.rad(crs1), legs_data8[offset+1][46], legs_data8[offset+1][47], math.rad((math.deg(legs_data8[offset+1][37]) + 180) % 360))
						if calc_lat == 0 and calc_lon == 0 then
							B738DR_fms_legs_offset_latx_2_mod = legs_data8[offset][7]
							B738DR_fms_legs_offset_lonx_2_mod = legs_data8[offset][8]
						else
							B738DR_fms_legs_offset_latx_2_mod = calc_lat
							B738DR_fms_legs_offset_lonx_2_mod = calc_lon
						end
					else
						B738DR_fms_legs_offset_latx_2_mod = legs_data8[offset][7]
						B738DR_fms_legs_offset_lonx_2_mod = legs_data8[offset][8]
					end
				else
					B738DR_fms_legs_offset_latx_2_mod = calc_lat
					B738DR_fms_legs_offset_lonx_2_mod = calc_lon
				end
			end
		end
		if offset_act_mod == 4 then
			-- PPOS exit
			B738DR_fms_legs_offset_latx_3_mod = simDR_latitude
			B738DR_fms_legs_offset_lonx_3_mod = simDR_longitude
			B738DR_fms_legs_offset_latx_4_mod = 0
			B738DR_fms_legs_offset_lonx_4_mod = 0
			
			if rte12_act == 0 then
				crs1 = nd_calc_brg(legs_data2[offset][7], legs_data2[offset][8], simDR_latitude, simDR_longitude)
				crs1 = (crs1 - math.deg(legs_data2[offset][37]) + 180) % 360
				if crs1 > 180 then
					crs1 = crs1 - 360
				end
				if crs1 >= 0 then
					crs1 = (math.deg(legs_data2[offset][2]) + 45) % 360
				else
					crs1 = (math.deg(legs_data2[offset][2]) + 315) % 360
				end
				calc_brg_brg(simDR_latitude, simDR_longitude, math.rad(crs1), legs_data2[offset-1][7], legs_data2[offset-1][8], legs_data2[offset][2])
				if calc_lat == 0 and calc_lon == 0 then
					B738DR_fms_legs_offset_latx_4_mod = legs_data2[offset][7]
					B738DR_fms_legs_offset_lonx_4_mod = legs_data2[offset][8]
				else
					B738DR_fms_legs_offset_latx_4_mod = calc_lat
					B738DR_fms_legs_offset_lonx_4_mod = calc_lon
				end
			elseif rte12_act == 1 then
				crs1 = nd_calc_brg(legs_data8[offset][7], legs_data8[offset][8], simDR_latitude, simDR_longitude)
				crs1 = (crs1 - math.deg(legs_data8[offset][37]) + 180) % 360
				if crs1 > 180 then
					crs1 = crs1 - 360
				end
				if crs1 >= 0 then
					crs1 = (math.deg(legs_data8[offset][2]) + 45) % 360
				else
					crs1 = (math.deg(legs_data8[offset][2]) + 315) % 360
				end
				calc_brg_brg(simDR_latitude, simDR_longitude, math.rad(crs1), legs_data8[offset-1][7], legs_data8[offset-1][8], legs_data8[offset][2])
				if calc_lat == 0 and calc_lon == 0 then
					B738DR_fms_legs_offset_latx_4_mod = legs_data8[offset][7]
					B738DR_fms_legs_offset_lonx_4_mod = legs_data8[offset][8]
				else
					B738DR_fms_legs_offset_latx_4_mod = calc_lat
					B738DR_fms_legs_offset_lonx_4_mod = calc_lon
				end
			end
		elseif offset_end_mod_in ~= 0 then
			-- calculate normal exit wpt
			B738DR_fms_legs_offset_latx_3_mod = 0
			B738DR_fms_legs_offset_lonx_3_mod = 0
			B738DR_fms_legs_offset_latx_4_mod = 0
			B738DR_fms_legs_offset_lonx_4_mod = 0
			
			if rte12_act == 0 then
				if offset_side_mod == 1 then
					crs1 = (math.deg(legs_data2[offset_end_mod_in][37]) + 225) % 360
				else
					crs1 = (math.deg(legs_data2[offset_end_mod_in][37]) + 135) % 360
				end
				calc_brg_brg(legs_data2[offset_end_mod_in][7], legs_data2[offset_end_mod_in][8], math.rad(crs1), legs_data2[offset_end_mod_in-1][46], legs_data2[offset_end_mod_in-1][47], legs_data2[offset_end_mod_in][2])
				if calc_lat == 0 and calc_lon == 0 then
					B738DR_fms_legs_offset_latx_3_mod = legs_data2[offset_end_mod_in][7]
					B738DR_fms_legs_offset_lonx_3_mod = legs_data2[offset_end_mod_in][8]
				else
					B738DR_fms_legs_offset_latx_3_mod = calc_lat
					B738DR_fms_legs_offset_lonx_3_mod = calc_lon
				end
				B738DR_fms_legs_offset_latx_4_mod = legs_data2[offset_end_mod_in][7]
				B738DR_fms_legs_offset_lonx_4_mod = legs_data2[offset_end_mod_in][8]
			elseif rte12_act == 1 then
				if offset_side_mod == 1 then
					crs1 = (math.deg(legs_data8[offset_end_mod_in][37]) + 225) % 360
				else
					crs1 = (math.deg(legs_data8[offset_end_mod_in][37]) + 135) % 360
				end
				calc_brg_brg(legs_data8[offset_end_mod_in][7], legs_data8[offset_end_mod_in][8], math.rad(crs1), legs_data8[offset_end_mod_in-1][46], legs_data8[offset_end_mod_in-1][47], legs_data8[offset_end_mod_in][2])
				if calc_lat == 0 and calc_lon == 0 then
					B738DR_fms_legs_offset_latx_3_mod = legs_data8[offset_end_mod_in][7]
					B738DR_fms_legs_offset_lonx_3_mod = legs_data8[offset_end_mod_in][8]
				else
					B738DR_fms_legs_offset_latx_3_mod = calc_lat
					B738DR_fms_legs_offset_lonx_3_mod = calc_lon
				end
				B738DR_fms_legs_offset_latx_4_mod = legs_data8[offset_end_mod_in][7]
				B738DR_fms_legs_offset_lonx_4_mod = legs_data8[offset_end_mod_in][8]
			end
		end
	else
		B738DR_fms_legs_offset_latx_1_mod = 0
		B738DR_fms_legs_offset_lonx_1_mod = 0
		B738DR_fms_legs_offset_latx_2_mod = 0
		B738DR_fms_legs_offset_lonx_2_mod = 0
		B738DR_fms_legs_offset_latx_3_mod = 0
		B738DR_fms_legs_offset_lonx_3_mod = 0
		B738DR_fms_legs_offset_latx_4_mod = 0
		B738DR_fms_legs_offset_lonx_4_mod = 0
	end
end

function offset_create(in_rte)
	
	local ii = 0
	local jj = 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local temp_brg = 0
	local kk = 0
	local mm = 0
	local id_nav_txt = ""
	local id_nav_txt2 = ""
	local lat_tmp = 0
	local lon_tmp = 0
	local lat_tmp2 = 0
	local lon_tmp2 = 0
	
	-- legs_off_tmp_n = 0
	-- legs_off_tmp = {}
	
	local offset_start_in = offset_start_mod
	local offset_end_in = offset_end_mod
	if offset_start_in == 0 then
		offset_start_in = offset_start_def_mod
	end
	if offset_end_in == 0 then
		offset_end_in = offset_end_def_mod
	end
	
	local create_offset_enable = true
	local offset_ok = false
	local wpt_available = 0
	local first_wpt = false
	local wpt_rel_brg = 0
	
	if in_rte == 0 then
		-- create lateral offset route
		-- if offset_act >= 5 then
			-- if offset_start_in > legs_num2 or offset_end_in > legs_num2 then
				-- create_offset_enable = false
			-- elseif offset_start_mod == 0 and offset_start_def_mod == 0 then
				-- create_offset_enable = false
			-- end
		-- else
			-- if offset_start_in < offset or offset_start_in > legs_num2 or offset_end_in > legs_num2 then
			-- if (offset_start_def_mod < offset and offset_start_mod == 0) or offset_start_in > legs_num2 or offset_end_in > legs_num2 then
			if offset_start_in == 0 or offset_start_in > legs_num2 or offset_end_in > legs_num2 then
				create_offset_enable = false
			elseif offset_start_mod == 0 and (offset_start_def_mod == 0 or offset_start_def_mod > offset) then
				create_offset_enable = false
			end
		-- end
		
		-- if offset_start_in >= offset and offset_start_in < legs_num2 and offset_end_in < legs_num2 then
			for ii = 1, legs_num2 do
				-- if ii >= offset_start_in and ii <= offset_end_in and create_offset_enable == true then
				if ii > offset_start_in and ii <= offset_end_in and create_offset_enable == true then
					-- -- if first_wpt == false and ii > 2 then
						-- -- wpt_available = 1
						-- -- if legs_data2[ii][19] == 1 or legs_data2[ii][19] == 3 then
							-- -- wpt_available = 0
						-- -- else
							-- -- if legs_data2[ii-1][1] == "(VECTOR)" or legs_data2[ii-1][1] == "DISCONTINUITY" then
								-- -- wpt_available = 0
							-- -- elseif legs_data2[ii-1][31] ~= "" and legs_data2[ii-1][31] ~= "TF" and legs_data2[ii-1][31] ~= "IF" then
								-- -- wpt_available = 0
							-- -- -- elseif legs_data2[ii-1][19] ~= 0 and legs_data2[ii-1][19] ~= 4 and legs_data2[ii-1][19] ~= 2 then
								-- -- -- wpt_available = 0
							-- -- end
							-- -- if wpt_available ~= 0 then
								-- -- wpt_rel_brg = (math.deg(legs_data2[ii-2][2]) - math.deg(legs_data2[ii-1][2]) + 360) % 360
								-- -- if wpt_rel_brg > 180 then
									-- -- wpt_rel_brg = wpt_rel_brg - 360
								-- -- end
								-- -- if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
									-- -- wpt_available = 0
								-- -- end
							-- -- end
						-- -- end
					-- -- end
					
					-- -- if first_wpt == true or wpt_available == 1 or (offset_start_mod == 0 and offset_act_mode == 3) then
						if offset_side_mod == 1 then
							-- left
							legs_data2[ii][45] = -offset_dist_mod
						else
							-- right
							legs_data2[ii][45] = offset_dist_mod
						end
						
						-- -- temp_brg = math.deg(legs_data2[ii][2])
						
						local rel_off_brg2 = math.deg(legs_data2[ii][2])
						-- local rel_off_brg2 = math.deg(legs_data2[ii][37])
						local rel_off_brg = (math.deg(legs_data2[ii+1][2]) - rel_off_brg2 + 360) % 360
						if rel_off_brg > 180 then
							rel_off_brg = rel_off_brg - 360
						end
						
						local rel_off_brg3 = 0
						if rel_off_brg < 0 then
							rel_off_brg = -rel_off_brg
							rel_off_brg3 = (180 - rel_off_brg) / 2
							if offset_side_mod == 1 then
								temp_brg = rel_off_brg + rel_off_brg3
								temp_brg = (rel_off_brg2 - temp_brg + 360) % 360
							else
								temp_brg = (rel_off_brg2 + rel_off_brg3) % 360
							end
						else
							rel_off_brg3 = (180 - rel_off_brg) / 2
							if offset_side_mod == 1 then
								temp_brg = (rel_off_brg2 - rel_off_brg3 + 360) % 360
							else
								temp_brg = rel_off_brg + rel_off_brg3
								temp_brg = (rel_off_brg2 + temp_brg) % 360
							end
						end
						
						local calc_off_dist = 0
						if rel_off_brg3 == 0 then
							calc_off_dist = offset_dist_mod
						else
							calc_off_dist = offset_dist_mod / math.sin(math.rad(rel_off_brg3))
						end
						
						temp_brg = math.rad(temp_brg)
						-- offsetsk
						calc_brg_dist(legs_data2[ii][7], legs_data2[ii][8], temp_brg, calc_off_dist)
						legs_data2[ii][46] = calc_lat
						legs_data2[ii][47] = calc_lon
						offset_ok = true
					-- end
					-- first_wpt = true
				else
					if offset_act < 5 then
						legs_data2[ii][45] = 0
						-- legs_data2[ii][46] = 0
						-- legs_data2[ii][47] = 0
						legs_data2[ii][48] = ""
					end
				end
			end
			
			if rte12_act == in_rte then
				legs_delete = 1
			end
			calc_rte_enable2 = 1
		-- end
	elseif in_rte == 1 then
		-- create lateral offset route
		-- if offset_act >= 5 then
			-- if offset_start_in > legs_num8 or offset_end_in > legs_num8 then
				-- create_offset_enable = false
			-- elseif offset_start_mod == 0 and offset_start_def_mod == 0 then
				-- create_offset_enable = false
			-- end
		-- else
			-- if offset_start_in < offset or offset_start_in > legs_num8 or offset_end_in > legs_num8 then
			-- if (offset_start_def_mod < offset and offset_start_mod == 0) or offset_start_in > legs_num8 or offset_end_in > legs_num8 then
			if offset_start_in == 0 or offset_start_in > legs_num8 or offset_end_in > legs_num8 then
				create_offset_enable = false
			elseif offset_start_mod == 0 and (offset_start_def_mod == 0 or offset_start_def_mod > offset) then
				create_offset_enable = false
			end
		-- end
		-- if offset_start_in < offset or offset_start_in >= legs_num8 or offset_end_in >= legs_num8 then
			-- create_offset_enable = false
		-- elseif offset_start_mod == 0 and (offset_start_def_mod == 0 or offset_start_def_mod > offset) then
			-- create_offset_enable = false
		-- end
		
		-- if offset_start_in >= offset and offset_start_in < legs_num8 and offset_end_in < legs_num8 then
			for ii = 1, legs_num8 do
				-- if ii >= offset_start_in and ii <= offset_end_in and create_offset_enable == true then
				if ii > offset_start_in and ii <= offset_end_in and create_offset_enable == true then
					-- if first_wpt == false and ii > 2 then
						-- wpt_available = 1
						-- if legs_data8[ii][19] == 1 or legs_data8[ii][19] == 3 then
							-- wpt_available = 0
						-- else
							-- if legs_data8[ii-1][1] == "(VECTOR)" or legs_data8[ii-1][1] == "DISCONTINUITY" then
								-- wpt_available = 0
							-- elseif legs_data8[ii-1][31] ~= "" and legs_data8[ii-1][31] ~= "TF" and legs_data8[ii-1][31] ~= "IF" then
								-- wpt_available = 0
							-- -- elseif legs_data8[ii-1][19] ~= 0 and legs_data8[ii-1][19] ~= 4 and legs_data8[ii-1][19] ~= 2 then
								-- -- wpt_available = 0
							-- end
							-- if wpt_available ~= 0 then
								-- wpt_rel_brg = (math.deg(legs_data8[ii-2][2]) - math.deg(legs_data8[ii-1][2]) + 360) % 360
								-- if wpt_rel_brg > 180 then
									-- wpt_rel_brg = wpt_rel_brg - 360
								-- end
								-- if wpt_rel_brg > 135 or wpt_rel_brg < -135 then
									-- wpt_available = 0
								-- end
							-- end
						-- end
					-- end
					
					-- if first_wpt == true or wpt_available == 1 or (offset_start_mod == 0 and offset_act_mode == 3) then
						if offset_side_mod == 1 then
							-- left
							legs_data8[ii][45] = -offset_dist_mod
						else
							-- right
							legs_data8[ii][45] = offset_dist_mod
						end
						
						-- temp_brg = math.deg(legs_data8[ii][2])
						
						local rel_off_brg2 = math.deg(legs_data8[ii][2])
						-- local rel_off_brg2 = math.deg(legs_data8[ii][37])
						local rel_off_brg = (math.deg(legs_data8[ii+1][2]) - rel_off_brg2 + 360) % 360
						if rel_off_brg > 180 then
							rel_off_brg = rel_off_brg - 360
						end
						
						local rel_off_brg3 = 0
						if rel_off_brg < 0 then
							rel_off_brg = -rel_off_brg
							rel_off_brg3 = (180 - rel_off_brg) / 2
							if offset_side_mod == 1 then
								temp_brg = rel_off_brg + rel_off_brg3
								temp_brg = (rel_off_brg2 - temp_brg + 360) % 360
							else
								temp_brg = (rel_off_brg2 + rel_off_brg3) % 360
							end
						else
							rel_off_brg3 = (180 - rel_off_brg) / 2
							if offset_side_mod == 1 then
								temp_brg = (rel_off_brg2 - rel_off_brg3 + 360) % 360
							else
								temp_brg = rel_off_brg + rel_off_brg3
								temp_brg = (rel_off_brg2 + temp_brg) % 360
							end
						end
						
						local calc_off_dist = 0
						if rel_off_brg3 == 0 then
							calc_off_dist = offset_dist_mod
						else
							calc_off_dist = offset_dist_mod / math.sin(math.rad(rel_off_brg3))
						end
						
						temp_brg = math.rad(temp_brg)
						-- offsetsk
						calc_brg_dist(legs_data8[ii][7], legs_data8[ii][8], temp_brg, calc_off_dist)
						legs_data8[ii][46] = calc_lat
						legs_data8[ii][47] = calc_lon
						offset_ok = true
					-- end
					-- first_wpt = true
				else
					if offset_act < 5 then
						legs_data8[ii][45] = 0
						-- legs_data8[ii][46] = 0
						-- legs_data8[ii][47] = 0
						legs_data8[ii][48] = ""
					end
				end
			end
			
			if rte12_act == in_rte then
				legs_delete = 1
			end
			calc_rte_enable8 = 1
		-- end
	end
	
	return offset_ok
	--dump_leg()
end

function delete_offset_legs()
	for ii = 1, legs_num do
		legs_data[ii][45] = 0
		-- legs_data[ii][46] = 0
		-- legs_data[ii][47] = 0
		legs_data[ii][48] = ""
		legs_data[ii][49] = 0
	end
end

function delete_offset_legs2(in_rte)
	if in_rte == 0 then
		if legs_num2 > 1 then
			for ii = 1, legs_num2 do
				legs_data2[ii][45] = 0
				-- legs_data2[ii][46] = 0
				-- legs_data2[ii][47] = 0
				legs_data2[ii][48] = ""
				legs_data2[ii][49] = 0
			end
			if rte12_act == in_rte then
				legs_delete = 1
			end
			calc_rte_enable2 = 1
		end
	elseif in_rte == 1 then
		if legs_num8 > 1 then
			for ii = 1, legs_num8 do
				legs_data8[ii][45] = 0
				-- legs_data8[ii][46] = 0
				-- legs_data8[ii][47] = 0
				legs_data8[ii][48] = ""
				legs_data8[ii][49] = 0
			end
			if rte12_act == in_rte then
				legs_delete = 1
			end
			calc_rte_enable8 = 1
		end
	end
end

function calc_brg_brg(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_brg2)
	
	-- first waypoint
	local nd_lat1 = math.rad(req_lat)
	local nd_lon1 = math.rad(req_lon)
	local nd_brg1 = req_brg --+ mag_variation_rad(nd_lat1, nd_lon1)
	
	-- second waypoint
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	local nd_brg2 = req_brg2 --+ mag_variation_rad(nd_lat2, nd_lon2)
	
	local nd_lat = 0
	local nd_lon = 0
	local nd_x = 0
	local nd_y = 0
	local nd_a = 0
	local nd_b = 0
	local nd_c = 0
	local nd_xy = 0
	local nd_brg12 = 0
	local nd_brg21 = 0
	local pi = math.pi	--math.rad(180) --3.141592653589 --math.pi()
	
	-- calculate intercept two waypoints
	nd_xy = 2 * math.asin(math.sqrt((math.sin((nd_lat1-nd_lat2)/2))^2+math.cos(nd_lat1)*math.cos(nd_lat2)*math.sin((nd_lon1-nd_lon2)/2)^2))
	
	
	nd_x = math.sin(nd_lon2 - nd_lon1)
	if nd_x > 0 then
		nd_brg12 = math.acos((math.sin(nd_lat2)-math.sin(nd_lat1)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat1)))
		nd_brg21 = 2 * pi-math.acos((math.sin(nd_lat1)-math.sin(nd_lat2)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat2)))
	else
		nd_brg12 = 2 * pi-math.acos((math.sin(nd_lat2)-math.sin(nd_lat1)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat1)))
		nd_brg21 = math.acos((math.sin(nd_lat1)-math.sin(nd_lat2)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat2)))
	end
	
	nd_a = math.rad(((math.deg(nd_brg1) - math.deg(nd_brg12)) + 360) % 360)
	nd_b = math.rad(((math.deg(nd_brg21) - math.deg(nd_brg2)) + 360) % 360)
	
	if (math.sin(nd_a) == 0 and math.sin(nd_b) == 0) then
		-- infinity of intersections
		calc_lat = rte_calc_lat
		calc_lon = rte_calc_lon
	elseif (math.sin(nd_a) * math.sin(nd_b)) < 0 then
		-- intersection ambiguous
		calc_lat = rte_calc_lat
		calc_lon = rte_calc_lon
	else
		nd_a = math.abs(nd_a)
		nd_b = math.abs(nd_b)
		nd_c = math.acos(-math.cos(nd_a)*math.cos(nd_b)+math.sin(nd_a)*math.sin(nd_b)*math.cos(nd_xy))
		
		nd_x = math.atan2(math.sin(nd_xy)*math.sin(nd_a)*math.sin(nd_b),math.cos(nd_b)+math.cos(nd_a)*math.cos(nd_c))
		nd_lat = math.asin(math.sin(nd_lat1)*math.cos(nd_x)+math.cos(nd_lat1)*math.sin(nd_x)*math.cos(nd_brg1))
		nd_lon = math.atan2(math.sin(nd_brg1)*math.sin(nd_x)*math.cos(nd_lat1),math.cos(nd_x)-math.sin(nd_lat1)*math.sin(nd_lat))
		nd_lon = ((nd_lon1+nd_lon+pi) % (2*pi)) - pi
		
		calc_lat = math.deg(nd_lat)		-- latitude
		calc_lon = math.deg(nd_lon)		-- longitude
	end

end

-- behind navaid
function calc_brg_dist3(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_dist2)
		
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	local temp_brg = req_brg --+ mag_variation_rad(nd_lat, nd_lon)
	
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	local kk = 0
	
	local nd_lat3 = math.rad(req_lat2)
	local nd_lon3 = math.rad(req_lon2)
	
	local relative_brg = 0
	local brg_to_nav = 0
	local brg_to = math.deg(req_brg)
	
	for kk = 0.1, 99.9 do
		nd_dis = kk / 3440.064795					-- distance NM
		nd_lat2 = math.asin(math.sin(nd_lat)*math.cos(nd_dis) + math.cos(nd_lat)*math.sin(nd_dis)*math.cos(temp_brg))
		nd_lon2 = nd_lon + math.atan2(math.sin(temp_brg)*math.sin(nd_dis)*math.cos(nd_lat), math.cos(nd_dis)-math.sin(nd_lat)*math.sin(nd_lat2))
		
		brg_to_nav = nd_calc_brg(math.deg(nd_lat2), math.deg(nd_lon2), req_lat2, req_lon2)
		relative_brg = (brg_to_nav - brg_to + 360) % 360
		if relative_brg > 180 then
			relative_brg = relative_brg - 360
		end
		
		if relative_brg > 90 or relative_brg < -90 then
			-- distance nd3 to nd2
			nd_x = (nd_lon2 - nd_lon3) * math.cos((nd_lat3 + nd_lat2)/2)
			nd_y = nd_lat2 - nd_lat3
			nd_dis = math.sqrt(nd_x*nd_x + nd_y*nd_y) * 3440.064795	--nm
			if nd_dis >= req_dist2 then
				break
			end
		end
		
	end
	
	calc_lat = math.deg(nd_lat2)
	calc_lon = math.deg(nd_lon2)
	
end


function calc_brg_dist2(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_dist2)
		
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	local temp_brg = req_brg --+ mag_variation_rad(nd_lat, nd_lon)
	
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	local kk = 0
	
	local nd_lat3 = math.rad(req_lat2)
	local nd_lon3 = math.rad(req_lon2)
	
	for kk = 0.1, 99.9, 0.5 do
		nd_dis = kk / 3440.064795					-- distance NM
		nd_lat2 = math.asin(math.sin(nd_lat)*math.cos(nd_dis) + math.cos(nd_lat)*math.sin(nd_dis)*math.cos(temp_brg))
		nd_lon2 = nd_lon + math.atan2(math.sin(temp_brg)*math.sin(nd_dis)*math.cos(nd_lat), math.cos(nd_dis)-math.sin(nd_lat)*math.sin(nd_lat2))
		
		-- distance nd3 to nd2
		nd_x = (nd_lon2 - nd_lon3) * math.cos((nd_lat3 + nd_lat2)/2)
		nd_y = nd_lat2 - nd_lat3
		nd_dis = math.sqrt(nd_x*nd_x + nd_y*nd_y) * 3440.064795	--nm
		if nd_dis >= req_dist2 then
			break
		end
	end
	calc_lat = math.deg(nd_lat2)
	calc_lon = math.deg(nd_lon2)
	
end

function calc_brg_dist20(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_dist2)
		
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	local temp_brg = req_brg --+ mag_variation_rad(nd_lat, nd_lon)
	
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	local kk = 0
	
	local nd_lat3 = math.rad(req_lat2)
	local nd_lon3 = math.rad(req_lon2)
	
	for kk = 0.1, 99.9, 0.5 do
		nd_dis = kk / 3440.064795					-- distance NM
		nd_lat2 = math.asin(math.sin(nd_lat)*math.cos(nd_dis) + math.cos(nd_lat)*math.sin(nd_dis)*math.cos(temp_brg))
		nd_lon2 = nd_lon + math.atan2(math.sin(temp_brg)*math.sin(nd_dis)*math.cos(nd_lat), math.cos(nd_dis)-math.sin(nd_lat)*math.sin(nd_lat2))
		
		-- distance nd3 to nd2
		nd_x = (nd_lon2 - nd_lon3) * math.cos((nd_lat3 + nd_lat2)/2)
		nd_y = nd_lat2 - nd_lat3
		nd_dis = math.sqrt(nd_x*nd_x + nd_y*nd_y) * 3440.064795	--nm
		if nd_dis <= req_dist2 then
			break
		end
	end
	calc_lat = math.deg(nd_lat2)
	calc_lon = math.deg(nd_lon2)
	
end


function calc_brg_dist(req_lat, req_lon, req_brg, req_dist)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local temp_brg = 0
	
	temp_brg = req_brg --+ mag_variation_rad(nd_lat, nd_lon)
	nd_dis = req_dist / 3440.064795					-- distance NM
	nd_lat2 = math.asin(math.sin(nd_lat)*math.cos(nd_dis) + math.cos(nd_lat)*math.sin(nd_dis)*math.cos(temp_brg))
	nd_lon2 = nd_lon + math.atan2(math.sin(temp_brg)*math.sin(nd_dis)*math.cos(nd_lat), math.cos(nd_dis)-math.sin(nd_lat)*math.sin(nd_lat2))
	
	calc_lat = math.deg(nd_lat2)
	calc_lon = math.deg(nd_lon2)
	
end

function nd_calc_dist(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	
	local nd_x = (nd_lon2 - nd_lon) * math.cos((nd_lat + nd_lat2)/2)
	local nd_y = nd_lat2 - nd_lat
	local nd_dis = math.sqrt(nd_x*nd_x + nd_y*nd_y) * 3440.064795	--nm
	
	return nd_dis
	
end

function nd_calc_dist2(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lat2 = math.rad(req_lat2)
	
	local nd_dlat = math.rad(req_lat2-req_lat)
	local nd_dlon = math.rad(req_lon2-req_lon)
	
	local nd_a = math.sin(nd_dlat/2)*math.sin(nd_dlat/2)+math.cos(nd_lat)*math.cos(nd_lat2)*math.sin(nd_dlon/2)*math.sin(nd_dlon/2)
	local nd_b = 2 * math.atan2(math.sqrt(nd_a), math.sqrt(1-nd_a))
	local nd_dis = nd_b * 3440.064795	--nm
	
	return nd_dis
end

function nd_calc_dist9(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_dlat = math.abs(req_lat2 - req_lat) / 180
	local nd_dlon = math.abs(req_lon2 - req_lon) / 360
	
	local nd_a = 12430 * nd_dlat
	local nd_b = 24901 * nd_dlon * math.cos(math.rad((req_lat + req_lat2) / 2))
	local nd_dis = math.sqrt(nd_a * nd_a + nd_b * nd_b)
	
	return nd_dis
end

function nd_calc_brg(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	
	local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
	local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
	local nd_hdg = math.atan2(nd_y, nd_x)
	nd_hdg = math.deg(nd_hdg)
	nd_hdg = (nd_hdg + 360) % 360
	
	return nd_hdg
	
end

function nd_calc_brg_rad(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	
	local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
	local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
	local nd_hdg = math.atan2(nd_y, nd_x)
	if nd_hdg < 0 then
		nd_hdg = nd_hdg + (2 * math.pi)
	end
	
	return nd_hdg
	
end

function nd_calc_dist_d(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	
	local delta_lat = nd_lat2 - nd_lat
	local delta_lon = nd_lon2 - nd_lon
	
	local nd_y = math.log(math.tan(math.pi / 4 + nd_lat2 / 2) / math.tan(math.pi / 4 + nd_lat / 2))
	local nd_x = 0
	if math.abs(nd_y) > 10e-6 then		--10e-12
		nd_x = delta_lat / nd_y
	else
		nd_x = math.cos(nd_lat)
	end
	if math.abs(delta_lon) > math.pi then
		if delta_lon > 0 then
			delta_lon = 2 * math.pi - delta_lon
			delta_lon = -delta_lon
		else
			delta_lon = 2 * math.pi + delta_lon
		end
	end
	
	local nd_hdg = math.sqrt(delta_lat * delta_lat + nd_x * nd_x * delta_lon * delta_lon) * 3440.064795	--nm
	
	return nd_hdg
	
end

function nd_calc_brg_d(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	
	local delta_lon = nd_lon2 - nd_lon
	
	local nd_y = math.log(math.tan(math.pi / 4 + nd_lat2 / 2)/math.tan(math.pi / 4 + nd_lat / 2))
	if math.abs(delta_lon) > math.pi then
		if delta_lon > 0 then
			delta_lon = 2 * math.pi - delta_lon
			delta_lon = -delta_lon
		else
			delta_lon = 2 * math.pi + delta_lon
		end
	end
	
	local nd_hdg = math.atan2(delta_lon, nd_y)
	nd_hdg = math.deg(nd_hdg)
	
	nd_hdg = (nd_hdg + 360) % 360
	
	return nd_hdg
	
end

function calc_brg_dist_d(req_lat, req_lon, req_brg, req_dist)
	
	local nd_lat = math.rad(req_lat)
	local nd_lon = math.rad(req_lon)
	local nd_dis = req_dist / 3440.064795	-- distance NM
	local temp_brg = req_brg
	
	local delta_lat = nd_dis * math.cos(temp_brg)
	local nd_lat2 = nd_lat + delta_lat
	
	local nd_y = math.log(math.tan(math.pi / 4 + nd_lat2 / 2)/math.tan(math.pi / 4 + nd_lat / 2))
	local nd_x = 0
	if math.abs(nd_y) > 10e-6 then		--10e-12
		nd_x = delta_lat / nd_y
	else
		nd_x = math.cos(nd_lat)
	end
	
	local delta_lon = nd_dis * math.sin(temp_brg) / nd_x
	local nd_lon2 = nd_lon + delta_lon
	
	if math.abs(nd_lat2) > math.pi/2 then
		if nd_lat2 > 0 then
			nd_lat2 = math.pi - nd_lat2
		else
			nd_lat2 = math.pi - nd_lat2
			nd_lat2 = -nd_lat2
		end
	end
	
	nd_lon2 = (nd_lon2 + 540) % 360 - 180
	
	calc_lat = math.deg(nd_lat2)
	calc_lon = math.deg(nd_lon2)
	
end

function calc_brg_dist_d2(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_dist2)
	
	local nd_dis = 0
	local kk = 0
	
	calc_lat = 0
	calc_lon = 0
	
	for kk = 0.1, 99.9, 0.2 do
		-- create wpt
		calc_brg_dist(req_lat, req_lon, req_brg, kk)
		
		nd_dis = nd_calc_dist(req_lat2, req_lon2, calc_lat, calc_lon)
		
		if nd_dis >= req_dist2 then
			break
		end
	end
	
end

function calc_brg_dist_d20(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_dist2)
	
	local nd_dis = 0
	local kk = 0
	
	calc_lat = 0
	calc_lon = 0
	
	for kk = 0.1, 99.9, 0.2 do
		-- create wpt
		calc_brg_dist(req_lat, req_lon, req_brg, kk)
		
		nd_dis = nd_calc_dist(req_lat2, req_lon2, calc_lat, calc_lon)
		
		if nd_dis <= req_dist2 then
			break
		end
	end
	
end


function find_sid_app_idx()

	local ii = 0
	
	last_sid_idx = 0
	first_sid_idx = 0
	first_app_idx = 0
	first_star_idx = 0
	iaf_app_idx = 0
	
	if legs_num > 1 then
		for ii = 1, legs_num do
			if legs_data[ii][19] == 1 or legs_data[ii][19] == 3 then
				if first_sid_idx == 0 then
					first_sid_idx = ii
				end
				last_sid_idx = ii
			elseif legs_data[ii][19] == 10 then
				last_sid_idx = ii
			end
			if first_star_idx == 0 then
				if legs_data[ii][19] == 2 or legs_data[ii][19] == 4 then
					first_star_idx = ii
				end
			end
			if first_app_idx == 0 then
				if legs_data[ii][19] == 7 or legs_data[ii][19] == 8 or legs_data[ii][19] == 9 then
					first_app_idx = ii
				end
			end
			if iaf_app_idx == 0 then
				if legs_data[ii][44] >= 50000 then
					iaf_app_idx = ii
				end
			end
		end
		if first_sid_idx == 0 and last_sid_idx ~= 0 then
			first_sid_idx = last_sid_idx
		end
	end
	if first_star_idx == 0 and first_app_idx ~= 0 then
		first_star_idx = first_app_idx
	end
end


function find_sid_app_idx_mod(in_rte)

	local ii = 0
	local valid_begin = false
	
	if in_rte == 0 then
		last_sid_idx_mod = 0
		first_sid_idx_mod = 0
		first_app_idx_mod = 0
		first_star_idx_mod = 0
		iaf_app_idx_mod = 0
		
		if legs_num2 > 1 then
			for ii = 1, legs_num2 do
				if legs_data2[ii][17] == 4 then
					valid_begin = true
				elseif valid_begin == true then
					if legs_data2[ii][19] == 1 or legs_data2[ii][19] == 3 then
						if first_sid_idx_mod == 0 then
							first_sid_idx_mod = ii
						end
						last_sid_idx_mod = ii
					elseif legs_data2[ii][19] == 10 then
						last_sid_idx_mod = ii
					end
					if first_star_idx_mod == 0 then
						if legs_data2[ii][19] == 2 or legs_data2[ii][19] == 4 then
							first_star_idx_mod = ii
						end
					end
					if first_app_idx_mod == 0 then
						if legs_data2[ii][19] == 7 or legs_data2[ii][19] == 8 or legs_data2[ii][19] == 9 then
							first_app_idx_mod = ii
						end
					end
					if iaf_app_idx_mod == 0 then
						if legs_data2[ii][44] >= 50000 then
							iaf_app_idx_mod = ii
						end
					end
				end
			end
			if first_sid_idx_mod == 0 and last_sid_idx_mod ~= 0 then
				first_sid_idx_mod = last_sid_idx_mod
			end
		end
		if first_star_idx_mod == 0 and first_app_idx_mod ~= 0 then
			first_star_idx_mod = first_app_idx_mod
		end
	elseif in_rte == 1 then
		last_sid_idx_mod2 = 0
		first_sid_idx_mod2 = 0
		first_app_idx_mod2 = 0
		first_star_idx_mod2 = 0
		iaf_app_idx_mod = 0
		
		if legs_num8 > 1 then
			for ii = 1, legs_num8 do
				if legs_data8[ii][17] == 4 then
					valid_begin = true
				elseif valid_begin == true then
					if legs_data8[ii][19] == 1 or legs_data8[ii][19] == 3 then
						if first_sid_idx_mod2 == 0 then
							first_sid_idx_mod2 = ii
						end
						last_sid_idx_mod2 = ii
					elseif legs_data8[ii][19] == 10 then
						last_sid_idx_mod2 = ii
					end
					if first_star_idx_mod2 == 0 then
						if legs_data8[ii][19] == 2 or legs_data8[ii][19] == 4 then
							first_star_idx_mod2 = ii
						end
					end
					if first_app_idx_mod2 == 0 then
						if legs_data8[ii][19] == 7 or legs_data8[ii][19] == 8 or legs_data8[ii][19] == 9 then
							first_app_idx_mod2 = ii
						end
					end
					if iaf_app_idx_mod == 0 then
						if legs_data8[ii][44] >= 50000 then
							iaf_app_idx_mod = ii
						end
					end
				end
				if first_sid_idx_mod2 == 0 and last_sid_idx_mod2 ~= 0 then
					first_sid_idx_mod2 = last_sid_idx_mod2
				end
			end
		end
		if first_star_idx_mod2 == 0 and first_app_idx_mod2 ~= 0 then
			first_star_idx_mod2 = first_app_idx_mod2
		end
	end
end


function rte_copy1(aaa, in_rte)

	local tt1 = 0
	local tt2 = 0
	local tt3 = 0
	
	legs_data_tmp_n = 0
	legs_data_tmp = {}
	
	tt3 = legs_num + 1
	if aaa <= tt3 and aaa > 0 then
		for tt1 = aaa, tt3 do
			legs_data_tmp_n = legs_data_tmp_n + 1
			legs_data_tmp[legs_data_tmp_n] = {}
			for tt2 = 1, MAX_LEGS_DATA do
				legs_data_tmp[legs_data_tmp_n][tt2] = legs_data[tt1][tt2]
			end
		end
	end
	
end

function rte_paste1(aaa, in_rte)

	local tt1 = 0
	local tt2 = 0
	local cnt_leg = aaa - 1
	
	if legs_data_tmp_n > 0 then
	
		for tt1 = 1, legs_data_tmp_n do
			cnt_leg = cnt_leg + 1
			legs_data[cnt_leg] = {}
			for tt2 = 1, MAX_LEGS_DATA do
				legs_data[cnt_leg][tt2] = legs_data_tmp[tt1][tt2]
			end
		end
		legs_num = cnt_leg - 1
	end
end

-- mode: 0-legs_data,1-legs_data2
function del_if(del_mode, in_rte)
	
	local ii = 0
	del_execute = 0
	
	if in_rte == 0 then
		if del_mode == 0 then
			for ii = 1, legs_num do
				if legs_data[ii][32] > 19 then
					legs_data[ii][32] = legs_data[ii][32] - 20
				elseif legs_data[ii][32] > 9 then
					legs_data[ii][32] = legs_data[ii][32] - 10
				end
			end
			ii = 1
			while ii <= legs_num do
				if legs_data[ii][19] == 99 then
					rte_copy1(ii+1, in_rte)
					rte_paste1(ii, in_rte)
					del_execute = 1
				else
					ii = ii + 1
				end
			end
		else
			for ii = 1, legs_num2 do
				if legs_data2[ii][32] > 19 then
					legs_data2[ii][32] = legs_data2[ii][32] - 20
				elseif legs_data2[ii][32] > 9 then
					legs_data2[ii][32] = legs_data2[ii][32] - 10
				end
			end
			ii = 1
			while ii <= legs_num2 do
				if legs_data2[ii][19] == 99 then
					rte_copy(ii+1, in_rte)
					rte_paste(ii, in_rte)
					del_execute = 1
				else
					ii = ii + 1
				end
			end
		end
	elseif in_rte == 1 then
		if del_mode == 0 then
			for ii = 1, legs_num do
				if legs_data[ii][32] > 19 then
					legs_data[ii][32] = legs_data[ii][32] - 20
				elseif legs_data[ii][32] > 9 then
					legs_data[ii][32] = legs_data[ii][32] - 10
				end
			end
			ii = 1
			while ii <= legs_num do
				if legs_data[ii][19] == 99 then
					rte_copy1(ii+1, in_rte)
					rte_paste1(ii, in_rte)
					del_execute = 1
				else
					ii = ii + 1
				end
			end
		else
			for ii = 1, legs_num8 do
				if legs_data8[ii][32] > 19 then
					legs_data8[ii][32] = legs_data8[ii][32] - 20
				elseif legs_data8[ii][32] > 9 then
					legs_data8[ii][32] = legs_data8[ii][32] - 10
				end
			end
			ii = 1
			while ii <= legs_num8 do
				if legs_data8[ii][19] == 99 then
					rte_copy(ii+1, in_rte)
					rte_paste(ii, in_rte)
					del_execute = 1
				else
					ii = ii + 1
				end
			end
		end
	end
end

function dec_lat_lon2(dec_idx, in_rte)
	
	local dec_calc_lat = 0
	local dec_calc_lon = 0
	local dec_calc_brg = 0
	local dec_calc_brg_true = 0
	local dec_calc_dist = 0
	
	local dec_navaid = ""
	local dec_navaid_rc = ""
	local dec_path = ""
	local dec_crs = 0
	local dec_brg = ""
	local dec_alt = 0
	local dec_dist = ""
	local dec_rnavaid = ""
	local dec_rnavaid_rc = ""
	
	local dec_prev_lat = 0
	local dec_prev_lon = 0
	local dec_prev_crs = 0
	local dec_prev_navaid = ""
	
	local dec_next_brg = 0
	
	local crs1 = 0
	local crs2 = 0
	local crs4 = 0
	local radius = 0
	local dist = 0
	local dist2 = 0
	local idx1 = 0
	local lat1 = 0
	local lon1 = 0
	local lat2 = 0
	local lon2 = 0
	local lat3 = 0
	local lon3 = 0
	local nd_dis = 0
	
	local ctr_lat = 0
	local ctr_lon = 0
	local turn_dir = 0
	local ndb_navaid = 0
	local find_from = 0
	local calc_lat1 = 0
	local calc_lon1 = 0
	
	-- legs_data2_buf
	
	copy_to_legs_data2_buf(in_rte)
	
	-- previous wpt
	local use_ref_icao_alt_x = ""
	local use_des_icao_alt_x = ""
	if in_rte == 0 then
		dec_prev_lat, dec_prev_lon = lat_lon_legs2(dec_idx-1)
		use_ref_icao_alt_x = ref_icao_alt_x
		use_des_icao_alt_x = des_icao_alt_x
	elseif in_rte == 1 then
		dec_prev_lat, dec_prev_lon = lat_lon_legs8(dec_idx-1)
		use_ref_icao_alt_x = ref_icao_alt7_x
		use_des_icao_alt_x = des_icao_alt7_x
	end
	dec_prev_crs = legs_data2_buf[dec_idx-1][37]
	dec_prev_navaid = legs_data2_buf[dec_idx-1][1]
	
	dec_calc_lat = dec_prev_lat
	dec_calc_lon = dec_prev_lon
	dec_calc_brg = legs_data2_buf[dec_idx-1][18]
	dec_calc_dist = 0
	
	-- actual wpt
	dec_navaid = legs_data2_buf[dec_idx][1]
	dec_crs = legs_data2_buf[dec_idx][2]
	dec_alt = legs_data2_buf[dec_idx][5]
	dec_navaid_rc = legs_data2_buf[dec_idx][16]
	dec_rnavaid = legs_data2_buf[dec_idx][27]
	dec_rnavaid_rc = legs_data2_buf[dec_idx][28]
	dec_brg = legs_data2_buf[dec_idx][29]
	dec_dist = legs_data2_buf[dec_idx][30]
	dec_path = legs_data2_buf[dec_idx][31]
	
	-- IF Initial Fix
	if dec_path == "IF" then
		ndb_navaid = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		else
			find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		end
		
		if navaid_list_n > 0 then
			dec_calc_lat = navaid_list[1][2]
			dec_calc_lon = navaid_list[1][3]
			dec_find_type = navaid_list[1][1]
			dec_calc_brg = nd_calc_brg(dec_prev_lat, dec_prev_lon, dec_calc_lat, dec_calc_lon)
			dec_calc_brg = (dec_calc_brg + mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		end
		
	-- TF/DF Track/Direct to Fix
	elseif dec_path == "TF" or dec_path == "DF" or dec_path == "" then
		ndb_navaid = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		else
			find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		end
		if navaid_list_n > 0 then
			dec_calc_lat = navaid_list[1][2]
			dec_calc_lon = navaid_list[1][3]
			dec_find_type = navaid_list[1][1]
			dec_calc_brg = nd_calc_brg(dec_prev_lat, dec_prev_lon, dec_calc_lat, dec_calc_lon)
			dec_calc_brg = (dec_calc_brg + mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		end
		
	-- (INTC) Course to Fix -> CF
	elseif dec_path == "CF" then
		ndb_navaid = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		else
			find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		end
		if navaid_list_n > 0 then
			dec_calc_lat = navaid_list[1][2]
			dec_calc_lon = navaid_list[1][3]
			dec_find_type = navaid_list[1][1]
			crs1 = tonumber(dec_brg)
			if crs1 == nil then
				crs1 = 0
			else
				crs1 = crs1 / 10
			end
			dec_calc_brg = crs1
		end
		
	-- (INTC) Course to Intercept -> CI/VI
	elseif dec_path == "CI" or dec_path == "VI" then
		-- calc next course
		idx1 = 0
		
		ndb_navaid = 0
		if (dec_idx + 1) <= legs_num2 then
			if legs_data2_buf[dec_idx+1][31] == "IF" then
				if (dec_idx + 2) <= legs_num2 then
					if string.sub(legs_data2_buf[dec_idx+2][31], 1, 1) == "C" or string.sub(legs_data2_buf[dec_idx+2][31], 1, 1) == "V" then
						if legs_data2_buf[dec_idx+2][32] > 19 then
							ndb_navaid = 2
						elseif legs_data2_buf[dec_idx+2][32] > 9 then
							ndb_navaid = 1
						end
						if legs_data2_buf[dec_idx+2][19] == 1 or legs_data2_buf[dec_idx+2][19] == 3 then
							find_navaid2(legs_data2_buf[dec_idx+2][34], "", 11, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
						elseif legs_data2_buf[dec_idx+2][19] == 2 or legs_data2_buf[dec_idx+2][19] == 4 then
							find_navaid2(legs_data2_buf[dec_idx+2][34], "", 12, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
						elseif legs_data2_buf[dec_idx+2][19] == 7 or legs_data2_buf[dec_idx+2][19] == 8 or legs_data2_buf[dec_idx+2][19] == 9 then
							find_navaid2(legs_data2_buf[dec_idx+2][34], "", 12, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
						else
							find_navaid2(legs_data2_buf[dec_idx+2][34], "", 0, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
						end
						if navaid_list_n > 0 then
							lat1 = navaid_list[1][2]
							lon1 = navaid_list[1][3]
						end
						crs2 = tonumber(legs_data2_buf[dec_idx+2][29])
						if crs2 == nil then
							crs2 = 0
						else
							crs2 = crs2 / 10
							if dec_path == "VI" then
								idx1 = 2
							else
								idx1 = 3
							end
						end
						crs2 = (crs2 + 180) % 360
						dec_next_brg = (crs2 - mag_variation_deg(lat1, lon1) + 360) % 360
					else
						if legs_data2_buf[dec_idx+1][32] > 19 then
							ndb_navaid = 2
						elseif legs_data2_buf[dec_idx+1][32] > 9 then
							ndb_navaid = 1
						end
						if legs_data2_buf[dec_idx+1][19] == 1 or legs_data2_buf[dec_idx+1][19] == 3 then
							find_navaid2(legs_data2_buf[dec_idx+1][34], "", 11, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
						elseif legs_data2_buf[dec_idx+1][19] == 2 or legs_data2_buf[dec_idx+1][19] == 4 then
							find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
						elseif legs_data2_buf[dec_idx+1][19] == 7 or legs_data2_buf[dec_idx+1][19] == 8 or legs_data2_buf[dec_idx+1][19] == 9 then
							find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
						else
							find_navaid2(legs_data2_buf[dec_idx+1][34], "", 0, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
						end
						if navaid_list_n > 0 then
							lat1 = navaid_list[1][2]
							lon1 = navaid_list[1][3]
							legs_data2_buf[dec_idx+2][7] = lat1
							legs_data2_buf[dec_idx+2][8] = lon1
							ndb_navaid = 0
							if legs_data2_buf[dec_idx+2][32] > 19 then
								ndb_navaid = 2
							elseif legs_data2_buf[dec_idx+2][32] > 9 then
								ndb_navaid = 1
							end
							if legs_data2_buf[dec_idx+2][19] == 1 or legs_data2_buf[dec_idx+2][19] == 3 then
								find_navaid2(legs_data2_buf[dec_idx+2][34], "", 11, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
							elseif legs_data2_buf[dec_idx+2][19] == 2 or legs_data2_buf[dec_idx+2][19] == 4 then
								find_navaid2(legs_data2_buf[dec_idx+2][34], "", 12, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
							elseif legs_data2_buf[dec_idx+2][19] == 7 or legs_data2_buf[dec_idx+2][19] == 8 or legs_data2_buf[dec_idx+2][19] == 9 then
								find_navaid2(legs_data2_buf[dec_idx+2][34], "", 12, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
							else
								find_navaid2(legs_data2_buf[dec_idx+2][34], "", 0, legs_data2_buf[dec_idx+2][16], ndb_navaid, in_rte)
							end
							if navaid_list_n > 0 then
								lat2 = navaid_list[1][2]
								lon2 = navaid_list[1][3]
								dec_next_brg = nd_calc_brg(lat1, lon1, lat2, lon2)
								legs_data2_buf[dec_idx+2][7] = lat2
								legs_data2_buf[dec_idx+2][8] = lon2
								idx1 = 1
							end
						end
						legs_data2_buf[dec_idx][23] = legs_data2_buf[dec_idx+1][7]
						legs_data2_buf[dec_idx][24] = legs_data2_buf[dec_idx+1][8]
					end
					legs_data2_buf[dec_idx+1][19] = 99	-- delete wpt
				end
			elseif string.sub(legs_data2_buf[dec_idx+1][31], 1, 1) == "C" or string.sub(legs_data2_buf[dec_idx+1][31], 1, 1) == "V" then
				if legs_data2_buf[dec_idx+1][32] > 19 then
					ndb_navaid = 2
				elseif legs_data2_buf[dec_idx+1][32] > 9 then
					ndb_navaid = 1
				end
				if legs_data2_buf[dec_idx+1][19] == 1 or legs_data2_buf[dec_idx+1][19] == 3 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 11, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				elseif legs_data2_buf[dec_idx+1][19] == 2 or legs_data2_buf[dec_idx+1][19] == 4 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				elseif legs_data2_buf[dec_idx+1][19] == 7 or legs_data2_buf[dec_idx+1][19] == 8 or legs_data2_buf[dec_idx+1][19] == 9 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				else
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 0, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				end
				if navaid_list_n > 0 then
					lat1 = navaid_list[1][2]
					lon1 = navaid_list[1][3]
				end
				crs2 = tonumber(legs_data2_buf[dec_idx+1][29])
				if crs2 == nil then
					crs2 = 0
				else
					crs2 = crs2 / 10
					if dec_path == "VI" then
						idx1 = 2
					else
						idx1 = 3
					end
				end
				crs2 = (crs2 + 180) % 360
				dec_next_brg = (crs2 - mag_variation_deg(lat1, lon1) + 360) % 360
			elseif legs_data2_buf[dec_idx+1][31] == "AF" then
				find_navaid(legs_data2_buf[dec_idx+1][27], "", 0, legs_data2_buf[dec_idx+1][28], in_rte)
				if navaid_list_n > 0 then
					lat3 = navaid_list[1][2]
					lon3 = navaid_list[1][3]
					dist = tonumber(legs_data2_buf[dec_idx+1][22])
					if dist == nil then
						dist = 0
					else
						dist = dist / 10
					end
					-- calc first wpt
					crs1 = (math.deg(legs_data2_buf[dec_idx+1][2]) + mag_variation_deg(lat3, lon3) + 360) % 360
					calc_brg_dist(lat3, lon3, math.rad(crs1), dist)
					lat1 = calc_lat
					lon1 = calc_lon
					-- calc second wpt
					crs1 = tonumber(legs_data2_buf[dec_idx+1][29])
					if crs1 == nil then
						crs1 = 0
					else
						crs1 = crs1 / 10
					end
					crs1 = (crs1 - mag_variation_deg(lat3, lon3) + 360) % 360
					calc_brg_dist(lat3, lon3, math.rad(crs1), dist)
					lat2 = calc_lat
					lon2 = calc_lon
					dec_next_brg = nd_calc_brg(lat1, lon1, lat2, lon2)
					idx1 = 2
				end
				
			-- else	-- AF, TF,...
			elseif legs_data2_buf[dec_idx+1][31] == "FM" or legs_data2_buf[dec_idx+1][31] == "VM" then
				if legs_data2_buf[dec_idx+1][32] > 19 then
					ndb_navaid = 2
				elseif legs_data2_buf[dec_idx+1][32] > 9 then
					ndb_navaid = 1
				end
				if legs_data2_buf[dec_idx+1][19] == 1 or legs_data2_buf[dec_idx+1][19] == 3 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 11, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				elseif legs_data2_buf[dec_idx+1][19] == 2 or legs_data2_buf[dec_idx+1][19] == 4 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				elseif legs_data2_buf[dec_idx+1][19] == 7 or legs_data2_buf[dec_idx+1][19] == 8 or legs_data2_buf[dec_idx+1][19] == 9 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				else
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 0, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				end
				if navaid_list_n > 0 then
					lat1 = navaid_list[1][2]
					lon1 = navaid_list[1][3]
					legs_data2_buf[dec_idx+1][23] = navaid_list[1][2]
					legs_data2_buf[dec_idx+1][24] = navaid_list[1][3]
				end
				crs1 = tonumber(legs_data2_buf[dec_idx+1][29])
				if crs1 == nil then
					crs1 = 0
				else
					crs1 = crs1 / 10
				end
				dec_next_brg = (crs1 - mag_variation_deg(lat1, lon1) + 360) % 360
				idx1 = 4
			elseif legs_data2_buf[dec_idx+1][31] == "FA" or legs_data2_buf[dec_idx+1][31] == "FC" or legs_data2_buf[dec_idx+1][31] == "FD" then
				if legs_data2_buf[dec_idx+1][32] > 19 then
					ndb_navaid = 2
				elseif legs_data2_buf[dec_idx+1][32] > 9 then
					ndb_navaid = 1
				end
				if legs_data2_buf[dec_idx+1][19] == 1 or legs_data2_buf[dec_idx+1][19] == 3 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 11, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				elseif legs_data2_buf[dec_idx+1][19] == 2 or legs_data2_buf[dec_idx+1][19] == 4 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				elseif legs_data2_buf[dec_idx+1][19] == 7 or legs_data2_buf[dec_idx+1][19] == 8 or legs_data2_buf[dec_idx+1][19] == 9 then
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				else
					find_navaid2(legs_data2_buf[dec_idx+1][34], "", 0, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
				end
				if navaid_list_n > 0 then
					lat1 = navaid_list[1][2]
					lon1 = navaid_list[1][3]
					legs_data2_buf[dec_idx+1][23] = navaid_list[1][2]
					legs_data2_buf[dec_idx+1][24] = navaid_list[1][3]
				end
				crs1 = tonumber(legs_data2_buf[dec_idx+1][29])
				if crs1 == nil then
					crs1 = 0
				else
					crs1 = crs1 / 10
					idx1 = 5
				end
				dec_next_brg = (crs1 - mag_variation_deg(lat1, lon1) + 360) % 360
			end
		end
		
		if idx1 > 0 then
			crs1 = tonumber(dec_brg)
			if crs1 == nil then
				crs1 = 0
			else
				crs1 = crs1 / 10
			end
			dec_calc_brg = crs1
			crs1 = (crs1 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
			
			local crs3 = 0
			
			--idx1 = 0
			if string.sub(legs_data2_buf[dec_idx-1][31], 1, 1) == "C" or string.sub(legs_data2_buf[dec_idx-1][31], 1, 1) == "V" 
			or legs_data2_buf[dec_idx-1][31] == "FC" or legs_data2_buf[dec_idx-1][31] == "AF" or legs_data2_buf[dec_idx-1][31] == "RF"  then	--or legs_data2_buf[dec_idx-1][31] == "FD" then
				crs3 = tonumber(legs_data2_buf[dec_idx-1][29])
				if crs3 == nil then
					crs3 = 0
				else
					crs3 = crs3 / 10
				end
				dec_prev_crs = math.rad((crs3 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360)
			elseif string.sub(legs_data2_buf[dec_idx-1][1], 1, 2) == "RW" and legs_data2_buf[dec_idx-1][19] == 1 then
				crs3 = tonumber(legs_data2_buf[dec_idx-1][29])
				if crs3 == nil then
					crs3 = 0
				else
					crs3 = crs3 / 10
				end
				dec_prev_crs = math.rad((crs3 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360)
			end
			
			-- crs2 = relative crs
			crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
			if crs2 > 180 then
				crs2 = crs2 - 360
			end
			
			turn_dir = legs_data2_buf[dec_idx][21]
			
			if turn_dir == -1 then
				if crs2 < 0 then
					turn_dir = 2	--left
				else
					turn_dir = 3	--right
				end
			end
			
			dist = 0
			if (crs2 < -100) or (crs2 > 100) then
				dist = 1
			elseif (crs2 < 0) and (turn_dir == 2) then
				dist = 1
			elseif (crs2 > 0) and (turn_dir == 3) then
				dist = 1
			end
			
			if dist == 0 then
				
				local ca_va = 0
				if dec_idx > 1 then
					if legs_data2_buf[dec_idx-1][31] == "CA" or legs_data2_buf[dec_idx-1][31] == "VA" then
						ca_va = 1
					end
				end
				
				if ca_va == 0 then
					calc_brg_brg(dec_prev_lat, dec_prev_lon, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
				else
					crs2 = math.deg(dec_prev_crs)
					crs2 = (crs2 - crs1 + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					
					if crs2 > -20 and crs2 < 20 then
						calc_brg_brg(dec_prev_lat, dec_prev_lon, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
					else
					
						dist = 0
						
						if turn_dir == 2 then
							--left
							-- calc CTR lat,lon
							crs2 = (math.deg(dec_prev_crs) + 270) % 360
							calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
							ctr_lat = calc_lat
							ctr_lon = calc_lon
							
							crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
							if crs2 > 180 then
								crs2 = crs2 - 360
							end
							if crs2 < 0 then
								crs2 = crs2 + 90
							else
								crs2 = crs2 - 270
							end
							crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
							calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
							
							calc_brg_brg(calc_lat, calc_lon, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
							
							dist = 1
						elseif turn_dir == 3 then
							--right
							crs2 = (math.deg(dec_prev_crs) + 90) % 360
							calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
							ctr_lat = calc_lat
							ctr_lon = calc_lon
							
							crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
							if crs2 > 180 then
								crs2 = crs2 - 360
							end
							if crs2 > 0 then
								crs2 = crs2 - 90
							else
								crs2 = crs2 + 270
							end
							crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
							calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
							
							calc_brg_brg(calc_lat, calc_lon, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
							
							dist = 1
						end
						
						if dist == 0 then
							calc_lat = dec_prev_lat
							calc_lon = dec_prev_lon
						end
						
					end
				end
				
			else
				
				local calc_radius = 1.5	--2.05
				local calc_repeat = 1
				local calc_position = 0
				
				while calc_repeat == 1 do
					if turn_dir == 2 then
						--left
						crs2 = (math.deg(dec_prev_crs) + 270) % 360
						calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), calc_radius)	-- radius 2.05 NM
						ctr_lat = calc_lat
						ctr_lon = calc_lon
						
						crs2 = (crs1 + 90) % 360
						calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), calc_radius)	-- radius 2NM
						
						--dist = 1
					elseif turn_dir == 3 then
						--right
						crs2 = (math.deg(dec_prev_crs) + 90) % 360
						calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), calc_radius)	-- radius 2.05 NM
						ctr_lat = calc_lat
						ctr_lon = calc_lon
						
						crs2 = (crs1 + 270) % 360
						calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), calc_radius)	-- radius 2NM
					end
					
					calc_lat1 = calc_lat
					calc_lon1 = calc_lon
					calc_brg_brg(calc_lat1, calc_lon1, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
					
					calc_position = (nd_calc_brg(calc_lat1, calc_lon1, calc_lat, calc_lon) + mag_variation_deg(calc_lat1, calc_lon1) + 360) % 360
					calc_position = (calc_position - crs1 + 360) % 360
					if calc_position > 180 then
						calc_position = calc_position - 360
					end
					if calc_position > -90 and calc_position < 90 then
						-- correct position
						calc_repeat = 0
					else
						if calc_radius < 1.5 then
							calc_repeat = 0
						else
							calc_radius = calc_radius - 0.1
						end
					end
				end
				
				dist = nd_calc_dist2(calc_lat1, calc_lon1, calc_lat, calc_lon)
				
				if dist > 500 then
					if string.sub(legs_data2_buf[dec_idx+1][31], 1, 1) == "C" or string.sub(legs_data2_buf[dec_idx+1][31], 1, 1) == "V" 
					or legs_data2_buf[dec_idx-1][31] == "FC" or legs_data2_buf[dec_idx-1][31] == "AF" or legs_data2_buf[dec_idx-1][31] == "RF" then
						dec_next_brg = (dec_next_brg + 180) % 360
						calc_brg_brg(calc_lat1, calc_lon1, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
						dist = nd_calc_dist2(calc_lat1, calc_lon1, calc_lat, calc_lon)
						if dist > 500 then
							calc_brg_brg(calc_lat1, calc_lon1, math.rad((crs1+180)%360), lat1, lon1, math.rad(dec_next_brg))
						end
					else
						calc_brg_brg(calc_lat1, calc_lon1, math.rad((crs1+180)%360), lat1, lon1, math.rad(dec_next_brg))
					end
					
				end
			end
			
			dec_calc_lat = calc_lat
			dec_calc_lon = calc_lon
		end
		dec_find_type = 4
	
	-- (1000) -> CA/VA/FA
	elseif dec_path == "CA" or dec_path == "VA" or dec_path == "FA" then
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
		crs1 = (crs1 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		
		local disk_ok = 0
		
		local fo_wpt = 0
		if legs_data2_buf[dec_idx][32] > 9 then
			fo_wpt = legs_data2_buf[dec_idx][32]
			if fo_wpt > 19 then
				fo_wpt = fo_wpt - 20
			elseif fo_wpt > 9 then
				fo_wpt = fo_wpt - 10
			end
			if fo_wpt == 0 then
				fo_wpt = legs_data2_buf[dec_idx][32] + 1
			end
		else
			fo_wpt = 1
		end
		legs_data2_buf[dec_idx][32] = fo_wpt
		if legs_data2_buf[dec_idx][19] == 1 then
			disk_ok = 0
			dist = 0
			if dec_idx > 1 then
				for hhh = dec_idx - 1, 1, -1 do
					if hhh == 1 then
						dist = (((dec_alt - use_ref_icao_alt_x) / 900) + 0.9) - dist
						disk_ok = 1
						break
					elseif string.sub(legs_data2_buf[hhh][1], 1, 2) == "RW" then
						dist = (((dec_alt - use_ref_icao_alt_x) / 900) + (legs_data2_buf[hhh][26] * 0.00054)) - dist
						disk_ok = 1
						break
					elseif legs_data2_buf[hhh][5] ~= 0 and (legs_data2_buf[hhh][6] == 0 or legs_data2_buf[hhh][6] == 45 or legs_data2_buf[hhh][6] == 32) then
						dist = ((dec_alt - legs_data2_buf[hhh][5]) / 900) - dist
						disk_ok = 1
						break
					end
					dist = dist + wpt_distance2(hhh, in_rte)
				end
				if disk_ok == 0 then
					dist = 0
				end
			else
				dist = ((dec_alt - use_ref_icao_alt_x) / 900) + 0.9
			end
		else
			disk_ok = 0
			dist = 0
			if dec_idx > 4 then
				for hhh = dec_idx - 1, dec_idx - 3, -1 do
					if hhh == 1 then
						dist = (((dec_alt - use_des_icao_alt_x) / 900) + 0.9) - dist
						disk_ok = 1
						break
					elseif string.sub(legs_data2_buf[hhh][1], 1, 2) == "RW" then
						if legs_data2_buf[hhh][5] ~= 0 then
							dist = (((dec_alt - legs_data2_buf[hhh][5]) / 900) + 0.9) - dist
						else
							dist = (((dec_alt - use_des_icao_alt_x) / 900) + 0.9) - dist
						end
						disk_ok = 1
						break
					elseif legs_data2_buf[hhh][5] ~= 0 and (legs_data2_buf[hhh][6] == 0 or legs_data2_buf[hhh][6] == 45 or legs_data2_buf[hhh][6] == 32) then
						dist = ((dec_alt - legs_data2_buf[hhh][5]) / 900) - dist
						disk_ok = 1
						break
					end
					dist = dist + wpt_distance2(hhh, in_rte)
				end
				if disk_ok == 0 then
					dist = 0
				end
			else
				dist = ((dec_alt - use_des_icao_alt_x) / 900) + 0.9
			end
		end
		
		
		-- calc turn
		crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
		if crs2 > 180 then
			crs2 = crs2 - 360
		end
		
		if dec_path == "FA" then
			if crs2 > -5 and crs2 < 5 then
				-- straight
				if dist <= 0 then
					dec_calc_lat = dec_prev_lat
					dec_calc_lon = dec_prev_lon
				else
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs1), dist)
					dec_calc_lat = calc_lat
					dec_calc_lon = calc_lon
				end
			else
				if dist < 0.9 then
					dist = 0.9
				end
				calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs1), dist)
				dec_calc_lat = calc_lat
				dec_calc_lon = calc_lon
			end
		else
			if crs2 > -5 and crs2 < 5 then
				-- straight
				if dist <= 0 then
					dec_calc_lat = dec_prev_lat
					dec_calc_lon = dec_prev_lon
				else
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs1), dist)
					dec_calc_lat = calc_lat
					dec_calc_lon = calc_lon
				end
			else
				turn_dir = legs_data2_buf[dec_idx][21]
				
				if turn_dir == -1 then
					if crs2 < 0 then
						turn_dir = 2	--left
					else
						turn_dir = 3	--right
					end
				end
				
				if turn_dir == 2 then
					--left
					-- calc CTR lat,lon
					crs2 = (math.deg(dec_prev_crs) + 270) % 360
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
					ctr_lat = calc_lat
					ctr_lon = calc_lon
					
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 < 0 then
						crs2 = crs2 + 90
					else
						crs2 = crs2 - 270
					end
					crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
					calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
				elseif turn_dir == 3 then
					--right
					crs2 = (math.deg(dec_prev_crs) + 90) % 360
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
					ctr_lat = calc_lat
					ctr_lon = calc_lon
					
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 > 0 then
						crs2 = crs2 - 90
					else
						crs2 = crs2 + 270
					end
					crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
					calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
				end
				
				if dist <= 0 then
					dec_calc_lat = calc_lat
					dec_calc_lon = calc_lon
				else
					calc_brg_dist(calc_lat, calc_lon, math.rad(crs1), dist)
					dec_calc_lat = calc_lat
					dec_calc_lon = calc_lon
				end
			end
		end
		dec_find_type = 4

	-- FD Track/Course to distance
	elseif dec_path == "FD" then
		
		local tmp_lat = dec_prev_lat
		local tmp_lon = dec_prev_lon
		
		ndb_navaid = 0
		find_from = 0
		if string.sub(dec_prev_navaid, 1, 2) ~= "RW" then
			if legs_data2_buf[dec_idx][32] > 19 then
				ndb_navaid = 2
			elseif legs_data2_buf[dec_idx][32] > 9 then
				ndb_navaid = 1
			end
			dec_navaid = legs_data2_buf[dec_idx][34]
			if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
				find_navaid2(dec_navaid, "", 11, dec_navaid_rc, ndb_navaid, in_rte)
				find_from = 11
			elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
				find_navaid2(dec_navaid, "", 12, dec_navaid_rc, ndb_navaid, in_rte)
				find_from = 12
			elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
				find_navaid2(dec_navaid, "", 12, dec_navaid_rc, ndb_navaid, in_rte)
				find_from = 12
			else
				find_navaid2(dec_navaid, "", 0, dec_navaid_rc, ndb_navaid, in_rte)
			end
			
			if navaid_list_n > 0 then
				dec_prev_lat = navaid_list[1][2]
				dec_prev_lon = navaid_list[1][3]
			end
		end
		
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		-- dec_calc_brg = crs1
		crs1 = (crs1 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		dist = tonumber(dec_dist)
		if dist == nil then
			dist = 0
		else
			dist = dist / 10
		end
		
		--find_navaid(dec_rnavaid, "", 0, dec_rnavaid_rc, in_rte)
		find_navaid2(dec_rnavaid, "", find_from, dec_rnavaid_rc, ndb_navaid, in_rte)
		if navaid_list_n == 0 then
			if ndb_navaid == 1 then
				find_navaid2(dec_rnavaid, "", find_from, dec_rnavaid_rc, 2, in_rte)
			end
		end
		
		if dist == 0 then
			calc_lat = dec_prev_lat
			calc_lon = dec_prev_lon
		elseif navaid_list_n > 0 then
			
			calc_lat1 = dec_prev_lat
			calc_lon1 = dec_prev_lon
			
			calc_brg_dist(calc_lat1, calc_lon1, math.rad(crs1), 10)
			ctr_lat = calc_lat
			ctr_lon = calc_lon
			
			local fix_tmp = inter(navaid_list[1][2], navaid_list[1][3], dist, calc_lat1, calc_lon1, ctr_lat, ctr_lon)
			
			if fix_tmp == 1 then
				crs2 = nd_calc_brg(calc_lat1, calc_lon1, inter1_x, inter1_y)
				
				crs2 = (crs2 - crs1 + 360) % 360
				if crs2 > 180 then
					crs2 = crs2 - 360
				end
				if crs2 > -5 and crs2 < 5 then
					calc_lat = inter1_x		-- latitude
					calc_lon = inter1_y		-- longitude
				else
					calc_lat = dec_prev_lat
					calc_lon = dec_prev_lon
				end
			elseif fix_tmp == 2 then
				crs2 = nd_calc_brg(calc_lat1, calc_lon1, inter1_x, inter1_y)
				
				crs2 = (crs2 - crs1 + 360) % 360
				if crs2 > 180 then
					crs2 = crs2 - 360
				end
				
				if crs2 > -5 and crs2 < 5 then
					dist = nd_calc_dist2(calc_lat1, calc_lon1, inter1_x, inter1_y)
				else
					dist = 10000
				end
				
				crs4 = nd_calc_brg(calc_lat1, calc_lon1, inter2_x, inter2_y)
				
				crs4 = (crs4 - crs1 + 360) % 360
				if crs4 > 180 then
					crs4 = crs4 - 360
				end
				
				if crs4 > -5 and crs4 < 5 then
					dist2 = nd_calc_dist2(calc_lat1, calc_lon1, inter2_x, inter2_y)
				else
					dist2 = 10000
				end
				
				if dist == 10000 and dist2 == 10000 then
					calc_lat = dec_prev_lat
					calc_lon = dec_prev_lon
				elseif dist < dist2 then
					calc_lat = inter1_x		-- latitude
					calc_lon = inter1_y		-- longitude
				else
					calc_lat = inter2_x		-- latitude
					calc_lon = inter2_y		-- longitude
				end
			else
				calc_lat = dec_prev_lat
				calc_lon = dec_prev_lon
			end
			
			dec_calc_lat = calc_lat		-- latitude
			dec_calc_lon = calc_lon		-- longitude
			
		end
		
		dec_find_type = 4
		
		-- Fly over wpt
		local fo_wpt = 0
		if legs_data2_buf[dec_idx][32] > 9 then
			fo_wpt = legs_data2_buf[dec_idx][32]
			if fo_wpt > 19 then
				fo_wpt = fo_wpt - 20
			elseif fo_wpt > 9 then
				fo_wpt = fo_wpt - 10
			end
			if fo_wpt == 0 then
				fo_wpt = legs_data2_buf[dec_idx][32] + 1
			end
		else
			fo_wpt = 1
		end
		legs_data2_buf[dec_idx][32] = fo_wpt
		
	-- CD/VD Track/Course to distance
	elseif dec_path == "CD" or dec_path == "VD" then
		
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
		crs1 = (crs1 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		dist = tonumber(dec_dist)
		if dist == nil then
			dist = 0
		else
			dist = dist / 10
		end
		
		find_from = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(dec_rnavaid, "", 11, dec_rnavaid_rc, ndb_navaid, in_rte)
			find_from = 11
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(dec_rnavaid, "", 12, dec_rnavaid_rc, ndb_navaid, in_rte)
			find_from = 12
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(dec_rnavaid, "", 12, dec_rnavaid_rc, ndb_navaid, in_rte)
			find_from = 12
		else
			find_navaid2(dec_rnavaid, "", 0, dec_rnavaid_rc, ndb_navaid, in_rte)
		end
		
		if dist == 0 then
			calc_lat = dec_prev_lat
			calc_lon = dec_prev_lon
		elseif navaid_list_n > 0 then
			
			legs_data2_buf[dec_idx][23] = navaid_list[1][2]
			legs_data2_buf[dec_idx][24] = navaid_list[1][3]
			
			crs2 = math.deg(dec_prev_crs)
			crs2 = (crs2 - crs1 + 360) % 360
			if crs2 > 180 then
				crs2 = crs2 - 360
			end
			
			if crs2 > -5 and crs2 < 5 then
				--direct
				calc_lat1 = dec_prev_lat
				calc_lon1 = dec_prev_lon
			else
				--turn
				turn_dir = legs_data2_buf[dec_idx][21]
				
				if turn_dir == -1 then
					-- calc turn dir
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 < 0 then
						turn_dir = 2	--left
					else
						turn_dir = 3	--right
					end
				end
				
				if turn_dir == 2 then
					--left
					-- calc CTR lat,lon
					crs2 = (math.deg(dec_prev_crs) + 270) % 360
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
					ctr_lat = calc_lat
					ctr_lon = calc_lon
					
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 < 0 then
						crs2 = crs2 + 90
					else
						crs2 = crs2 - 270
					end
					crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
					calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
					
					calc_lat1 = calc_lat
					calc_lon1 = calc_lon
				elseif turn_dir == 3 then
					--right
					crs2 = (math.deg(dec_prev_crs) + 90) % 360
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
					ctr_lat = calc_lat
					ctr_lon = calc_lon
					
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 > 0 then
						crs2 = crs2 - 90
					else
						crs2 = crs2 + 270
					end
					crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
					calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
					
					calc_lat1 = calc_lat
					calc_lon1 = calc_lon
				end
				
			end
			
			calc_brg_dist(calc_lat1, calc_lon1, math.rad(crs1), 10)
			ctr_lat = calc_lat
			ctr_lon = calc_lon
			
			local fix_tmp = inter(navaid_list[1][2], navaid_list[1][3], dist, calc_lat1, calc_lon1, ctr_lat, ctr_lon)
			
			if fix_tmp == 1 then
				crs2 = nd_calc_brg(calc_lat1, calc_lon1, inter1_x, inter1_y)
				
				crs2 = (crs2 - crs1 + 360) % 360
				if crs2 > 180 then
					crs2 = crs2 - 360
				end
				if crs2 > -5 and crs2 < 5 then
					calc_lat = inter1_x		-- latitude
					calc_lon = inter1_y		-- longitude
				else
					calc_lat = dec_prev_lat
					calc_lon = dec_prev_lon
				end
			elseif fix_tmp == 2 then
				crs2 = nd_calc_brg(calc_lat1, calc_lon1, inter1_x, inter1_y)
				
				crs2 = (crs2 - crs1 + 360) % 360
				if crs2 > 180 then
					crs2 = crs2 - 360
				end
				
				if crs2 > -5 and crs2 < 5 then
					dist = nd_calc_dist2(calc_lat1, calc_lon1, inter1_x, inter1_y)
				else
					dist = 10000
				end
				
				crs4 = nd_calc_brg(calc_lat1, calc_lon1, inter2_x, inter2_y)
				
				crs4 = (crs4 - crs1 + 360) % 360
				if crs4 > 180 then
					crs4 = crs4 - 360
				end
				
				if crs4 > -5 and crs4 < 5 then
					dist2 = nd_calc_dist2(calc_lat1, calc_lon1, inter2_x, inter2_y)
				else
					dist2 = 10000
				end
				
				if dist == 10000 and dist2 == 10000 then
					calc_lat = dec_prev_lat
					calc_lon = dec_prev_lon
				elseif dist < dist2 then
					calc_lat = inter1_x		-- latitude
					calc_lon = inter1_y		-- longitude
				else
					calc_lat = inter2_x		-- latitude
					calc_lon = inter2_y		-- longitude
				end
			else
				calc_lat = dec_prev_lat
				calc_lon = dec_prev_lon
			end
			
			dec_calc_lat = calc_lat		-- latitude
			dec_calc_lon = calc_lon		-- longitude
			
		end
		dec_find_type = 4
	
	-- FC Track to Fix
	elseif dec_path == "FC" then
		
		if string.sub(dec_prev_navaid, 1, 2) ~= "RW" then
			ndb_navaid = 0
			if legs_data2_buf[dec_idx][32] > 19 then
				ndb_navaid = 2
			elseif legs_data2_buf[dec_idx][32] > 9 then
				ndb_navaid = 1
			end
			dec_navaid = legs_data2_buf[dec_idx][34]
			if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
				find_navaid2(dec_navaid, "", 11, dec_navaid_rc, ndb_navaid, in_rte)
			elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
				find_navaid2(dec_navaid, "", 12, dec_navaid_rc, ndb_navaid, in_rte)
			elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
				find_navaid2(dec_navaid, "", 12, dec_navaid_rc, ndb_navaid, in_rte)
			else
				find_navaid2(dec_navaid, "", 0, dec_navaid_rc, ndb_navaid, in_rte)
			end
			
			if navaid_list_n > 0 then
				dec_prev_lat = navaid_list[1][2]
				dec_prev_lon = navaid_list[1][3]
			end
		end
		
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
		crs1 = (crs1 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		dist = tonumber(dec_dist)
		if dist == nil then
			dist = 0
		else
			dist = dist / 10
		end
		calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs1), dist)
		dec_calc_lat = calc_lat
		dec_calc_lon = calc_lon
		dec_find_type = 4
		
	-- VECTOR -> FM
	elseif dec_path == "FM" then
		
		if string.sub(dec_prev_navaid, 1, 2) ~= "RW" then
			ndb_navaid = 0
			if legs_data2_buf[dec_idx][32] > 19 then
				ndb_navaid = 2
			elseif legs_data2_buf[dec_idx][32] > 9 then
				ndb_navaid = 1
			end
			if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
				find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
				find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
				find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			else
				find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			end
			
			if navaid_list_n > 0 then
				dec_prev_lat = navaid_list[1][2]
				dec_prev_lon = navaid_list[1][3]
				dec_find_type = navaid_list[1][1]
			else
				dec_find_type = 0
			end
		end
		
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
	
	-- VECTOR -> VM
	elseif dec_path == "VM" then
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
		dec_find_type = 0

	-- CR/VR Course to Radial
	elseif dec_path == "CR" or dec_path == "VR" then
		
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
		crs1 = (crs1 - mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		
		find_from = 0
		ndb_navaid = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(dec_rnavaid, "", 11, dec_rnavaid_rc, ndb_navaid, in_rte)
			find_from = 1
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(dec_rnavaid, "", 12, dec_rnavaid_rc, ndb_navaid, in_rte)
			find_from = 2
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(dec_rnavaid, "", 12, dec_rnavaid_rc, ndb_navaid, in_rte)
			find_from = 2
		else
			find_navaid2(dec_rnavaid, "", 0, dec_rnavaid_rc, ndb_navaid, in_rte)
		end
		
		if navaid_list_n > 0 then
			
			legs_data2_buf[dec_idx][23] = navaid_list[1][2]
			legs_data2_buf[dec_idx][24] = navaid_list[1][3]
			legs_data2_buf[dec_idx][30] = math.deg(dec_crs) * 10
			
			lat1 = navaid_list[1][2]
			lon1 = navaid_list[1][3]
			dec_next_brg = (math.deg(dec_crs) - mag_variation_deg(lat1, lon1) + 360) % 360
			
			crs2 = math.deg(dec_prev_crs)
			crs2 = (crs2 - crs1 + 360) % 360
			if crs2 > 180 then
				crs2 = crs2 - 360
			end
			
			if crs2 > -45 and crs2 < 45 then
				
				calc_brg_brg(dec_prev_lat, dec_prev_lon, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
				
			else
			
				dist = 0
				turn_dir = legs_data2_buf[dec_idx][21]
				
				if turn_dir == -1 then
					-- calc turn dir
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 < 0 then
						turn_dir = 2	--left
					else
						turn_dir = 3	--right
					end
				end
				
				if turn_dir == 2 then
					--left
					-- calc CTR lat,lon
					crs2 = (math.deg(dec_prev_crs) + 270) % 360
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
					ctr_lat = calc_lat
					ctr_lon = calc_lon
					
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 < 0 then
						crs2 = crs2 + 90
					else
						crs2 = crs2 - 270
					end
					crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
					calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
					
					dist = 1
				elseif turn_dir == 3 then
					--right
					crs2 = (math.deg(dec_prev_crs) + 90) % 360
					calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs2), 1.6)	-- radius 2NM
					ctr_lat = calc_lat
					ctr_lon = calc_lon
					
					crs2 = (crs1 - math.deg(dec_prev_crs) + 360) % 360
					if crs2 > 180 then
						crs2 = crs2 - 360
					end
					if crs2 > 0 then
						crs2 = crs2 - 90
					else
						crs2 = crs2 + 270
					end
					crs2 = (math.deg(dec_prev_crs) + crs2 + 360) % 360
					calc_brg_dist(ctr_lat, ctr_lon, math.rad(crs2), 1.6)	-- radius 2NM
					
					dist = 1
				end
				
				if dist == 0 then --or idx1 == 2 then
					calc_lat = dec_prev_lat
					calc_lon = dec_prev_lon
				end
				
				calc_brg_brg(calc_lat, calc_lon, math.rad(crs1), lat1, lon1, math.rad(dec_next_brg))
			end
			
			dec_calc_lat = calc_lat
			dec_calc_lon = calc_lon
			dec_next_brg = (crs1 + mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		end
		dec_find_type = 4
	
	-- RF Radius to Fix
	elseif dec_path == "RF" then
		dec_find_type = 0
		ndb_navaid = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		else
			find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		end
		
		if navaid_list_n > 0 then
			dec_calc_lat = navaid_list[1][2]
			dec_calc_lon = navaid_list[1][3]
			dec_find_type = navaid_list[1][1]
			crs1 = nd_calc_brg(dec_prev_lat, dec_prev_lon, dec_calc_lat, dec_calc_lon)
			dec_calc_brg = (crs1 + mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
			
			if legs_data2_buf[dec_idx][21] == 2 then
				-- left
				crs2 = (crs1 + 270) % 360
				radius = tonumber(legs_data2_buf[dec_idx][33])
				if radius == nil then
					radius = 0
				else
					radius = radius / 1000
				end
			else
				-- right
				crs2 = (crs1 + 90) % 360
				radius = tonumber(legs_data2_buf[dec_idx][33])
				if radius == nil then
					radius = 0
				else
					radius = radius / 1000
				end
			end
			
			-- [30]sk
			
			-- calc midpoint
			dist = (nd_calc_dist2(dec_prev_lat, dec_prev_lon, dec_calc_lat, dec_calc_lon)) / 2
			calc_brg_dist(dec_prev_lat, dec_prev_lon, math.rad(crs1), dist)
			lat1 = calc_lat
			lon1 = calc_lon
			
			-- calc Center of Radius
			if dist >= radius then
				dist2 = 0
				radius = dist
			else
				dist2 = math.sqrt((radius * radius) - (dist * dist))
				calc_brg_dist(lat1, lon1, math.rad(crs2), dist2)
			end
			
			local next_crs = 0
			local begin_crs = 0
			local end_crs = 0
			local end_angle = 0
			local rf_dist_calc1 = 0
			local rf_dist_calc2 = 0
			local rf_dist_tt = 0
			local second_lat = 0
			local second_lon = 0
			
			legs_data2_buf[dec_idx][23] = calc_lat
			legs_data2_buf[dec_idx][24] = calc_lon
			
			-- final course
			next_crs = nd_calc_brg(legs_data2_buf[dec_idx][23], legs_data2_buf[dec_idx][24], dec_calc_lat, dec_calc_lon)
			end_crs = next_crs
			if legs_data2_buf[dec_idx][21] == 2 then
				-- left
				next_crs = (next_crs + 270) % 360
			else
				-- right
				next_crs = (next_crs + 90) % 360
			end
			next_crs = (next_crs + mag_variation_deg(dec_calc_lat, dec_calc_lon) + 360) % 360
			begin_crs = nd_calc_brg(legs_data2_buf[dec_idx][23], legs_data2_buf[dec_idx][24], dec_prev_lat, dec_prev_lon)
			legs_data2_buf[dec_idx][29] = string.format("%04d", next_crs * 10)
			
			rf_dist_tt = tonumber(legs_data2_buf[dec_idx][30])
			if rf_dist_tt ~= nil then
				rf_dist_tt = rf_dist_tt / 10
				
				-- calc lenght
				end_angle = rel_crs_turn(begin_crs, end_crs, legs_data2_buf[dec_idx][21])
				end_angle = math.abs(end_angle)
				rf_dist_calc1 = ((math.pi * radius / 180) * end_angle) --+ radii_remain_dist
				
				-- recalc center point
				crs2 = (crs2 + 180) % 360
				calc_brg_dist(lat1, lon1, math.rad(crs2), dist2)
				
				second_lat = calc_lat
				second_lon = calc_lon
				
				-- final course
				next_crs = nd_calc_brg(second_lat, second_lon, dec_calc_lat, dec_calc_lon)
				end_crs = next_crs
				if legs_data2_buf[dec_idx][21] == 2 then
					-- left
					next_crs = (next_crs + 270) % 360
				else
					-- right
					next_crs = (next_crs + 90) % 360
				end
				next_crs = (next_crs + mag_variation_deg(dec_calc_lat, dec_calc_lon) + 360) % 360
				begin_crs = nd_calc_brg(second_lat, second_lon, dec_prev_lat, dec_prev_lon)
				
				-- calc lenght
				end_angle = rel_crs_turn(begin_crs, end_crs, legs_data2_buf[dec_idx][21])
				end_angle = math.abs(end_angle)
				rf_dist_calc2 = ((math.pi * radius / 180) * end_angle) --+ radii_remain_dist
				
				-- check lenght
				if math.abs(rf_dist_tt - rf_dist_calc1) > math.abs(rf_dist_tt - rf_dist_calc2) then
					legs_data2_buf[dec_idx][23] = second_lat
					legs_data2_buf[dec_idx][24] = second_lon
					legs_data2_buf[dec_idx][29] = string.format("%04d", next_crs * 10)
				end
			end
			
		else
			legs_data2_buf[dec_idx][23] = 0
			legs_data2_buf[dec_idx][24] = 0
		end

	-- AF Arc to Fix
	elseif dec_path == "AF" then
		dec_find_type = 0
		ndb_navaid = 0
		find_from = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			find_from = 11
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			find_from = 12
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
			find_from = 12
		else
			find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		end
		
		if navaid_list_n > 0 then
			dec_calc_lat = navaid_list[1][2]
			dec_calc_lon = navaid_list[1][3]
			dec_find_type = navaid_list[1][1]
			dec_calc_brg = nd_calc_brg(dec_prev_lat, dec_prev_lon, dec_calc_lat, dec_calc_lon)
			dec_calc_brg = (dec_calc_brg + mag_variation_deg(dec_prev_lat, dec_prev_lon) + 360) % 360
		end
		-- add Arc Center Lat, Lon
		find_navaid2(dec_rnavaid, "", find_from, dec_rnavaid_rc, ndb_navaid, in_rte)
		if navaid_list_n == 0 then
			if ndb_navaid == 1 then
				find_navaid2(dec_rnavaid, "", find_from, dec_rnavaid_rc, 2, in_rte)
			end
		end
		
		--afsk
		local nd_hdg = 0
		local next_crs = 0
		local nd_y = 0
		local relative_brg3 = 0
		
		local nnn = 0
		if navaid_list_n > 0 then
			if navaid_list_n == 1 then
				legs_data2_buf[dec_idx][23] = navaid_list[1][2]
				legs_data2_buf[dec_idx][24] = navaid_list[1][3]
				-- final course
				next_crs = nd_calc_brg(legs_data2_buf[dec_idx][23], legs_data2_buf[dec_idx][24], dec_calc_lat, dec_calc_lon)
				if legs_data2_buf[dec_idx][23] ~= 0 or legs_data2_buf[dec_idx][24] ~= 0 then
					-- from Center Arc
					if legs_data2_buf[dec_idx][21] == 2 then
						-- left
						next_crs = (next_crs + 270) % 360
					else
						-- right
						next_crs = (next_crs + 90) % 360
					end
					next_crs = (next_crs + mag_variation_deg(dec_calc_lat, dec_calc_lon) + 360) % 360
					legs_data2_buf[dec_idx][29] = string.format("%04d", next_crs * 10)
				end
			else
				for nnn = 1, navaid_list_n do
					if navaid_list[nnn][1] == 1 or navaid_list[nnn][1] == 2 then	-- VOR / VORTAG / VORDME
						legs_data2_buf[dec_idx][23] = navaid_list[nnn][2]
						legs_data2_buf[dec_idx][24] = navaid_list[nnn][3]
						-- final course
						next_crs = nd_calc_brg(legs_data2_buf[dec_idx][23], legs_data2_buf[dec_idx][24], dec_calc_lat, dec_calc_lon)
						if legs_data2_buf[dec_idx][23] ~= 0 or legs_data2_buf[dec_idx][24] ~= 0 then
							-- from Center Arc
							if legs_data2_buf[dec_idx][21] == 2 then
								-- right
								next_crs = (next_crs + 270) % 360
							else
								-- left
								next_crs = (next_crs + 90) % 360
							end
							next_crs = (next_crs + mag_variation_deg(dec_calc_lat, dec_calc_lon) + 360) % 360
							legs_data2_buf[dec_idx][29] = string.format("%04d", next_crs * 10)
						end
					end
				end
			end
			
		end

	-- PI Procedure turn
	elseif dec_path == "PI" then
		
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		dec_calc_brg = crs1
		
		dec_find_type = 0
		ndb_navaid = 0
		if legs_data2_buf[dec_idx][32] > 19 then
			ndb_navaid = 2
		elseif legs_data2_buf[dec_idx][32] > 9 then
			ndb_navaid = 1
		end
		if legs_data2_buf[dec_idx][19] == 1 or legs_data2_buf[dec_idx][19] == 3 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 11, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 2 or legs_data2_buf[dec_idx][19] == 4 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		elseif legs_data2_buf[dec_idx][19] == 7 or legs_data2_buf[dec_idx][19] == 8 or legs_data2_buf[dec_idx][19] == 9 then
			find_navaid2(legs_data2_buf[dec_idx][34], "", 12, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		else
			find_navaid2(legs_data2_buf[dec_idx][34], "", 0, legs_data2_buf[dec_idx][16], ndb_navaid, in_rte)
		end
		if navaid_list_n > 0 then
			dec_calc_lat = navaid_list[1][2]
			dec_calc_lon = navaid_list[1][3]
			dec_find_type = navaid_list[1][1]
		end
		if legs_data2_buf[dec_idx+1][31] == "IF" then
			ndb_navaid = 0
			if legs_data2_buf[dec_idx+1][32] > 19 then
				ndb_navaid = 2
			elseif legs_data2_buf[dec_idx+1][32] > 9 then
				ndb_navaid = 1
			end
			if legs_data2_buf[dec_idx+1][19] == 1 or legs_data2_buf[dec_idx+1][19] == 3 then
				find_navaid2(legs_data2_buf[dec_idx+1][34], "", 11, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
			elseif legs_data2_buf[dec_idx+1][19] == 2 or legs_data2_buf[dec_idx+1][19] == 4 then
				find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
			elseif legs_data2_buf[dec_idx+1][19] == 7 or legs_data2_buf[dec_idx+1][19] == 8 or legs_data2_buf[dec_idx+1][19] == 9 then
				find_navaid2(legs_data2_buf[dec_idx+1][34], "", 12, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
			else
				find_navaid2(legs_data2_buf[dec_idx+1][34], "", 0, legs_data2_buf[dec_idx+1][16], ndb_navaid, in_rte)
			end
			if navaid_list_n > 0 then
				if legs_data2_buf[dec_idx][21] == 2 then
					crs1 = (crs1 + 135) % 360
				else
					crs1 = (crs1 + 225) % 360
				end
				crs1 = (crs1 + mag_variation_deg(dec_calc_lat, dec_calc_lon) + 360) % 360
				crs2 = nd_calc_brg(navaid_list[1][2], navaid_list[1][3], dec_calc_lat, dec_calc_lon)
				crs1 = (crs1 - crs2 + 360) % 360
				if crs1 > 180 then
					crs1 = crs1 - 360
				end
				crs1 = math.abs(crs1)
				if crs1 <= 90 then
					legs_data2_buf[dec_idx+1][19] = 99	-- delete wpt
				end
			end
		end
	
	-- HOLD -> HA/HF/HM
	elseif dec_path == "HA" or dec_path == "HF" or dec_path == "HM" then
		crs1 = tonumber(dec_brg)
		if crs1 == nil then
			crs1 = 0
		else
			crs1 = crs1 / 10
		end
		if crs1 <= 360 then
			dec_calc_brg = crs1
		else
			dec_calc_brg = math.deg(dec_calc_brg)
			crs1 = crs1 * 10
			legs_data2_buf[dec_idx][29] = string.format("%04d", crs1)
		end
		dec_find_type = 4
	end
	
	-- save decoded data
	legs_data2_buf[dec_idx][7] = dec_calc_lat
	legs_data2_buf[dec_idx][8] = dec_calc_lon
	legs_data2_buf[dec_idx][18] = math.rad(dec_calc_brg)
	
	copy_from_legs_data2_buf(in_rte)
end




function B738_calc_rte()

	local ii = 0
	local jj = 0
	local kk = 0
	local find_txt = ""
	local delta_crs = 0
	local rnw_temp = ""
	local rnw_alt_found = 0
	local lat_tmp = 0
	local lon_tmp = 0
	local lat_tmp2 = 0
	local lon_tmp2 = 0
	
	if calc_rte_enable == 1 then
		calc_rte_enable = 2
		calc_rte_act = 0
		rte_calc_lat = 0
		rte_calc_lon = 0
		if calc_rte_enable2 ~= 0 then
			calc_rte_enable2 = 0
		end
		if calc_rte_enable8 ~= 0 then
			calc_rte_enable8 = 0
		end
		if rte12_act == 0 then
			if legs_num2 > 1 then
				for kkk = 1, legs_num2 + 1 do
					legs_data2[kkk][17] = 0
				end
			end
		elseif rte12_act == 1 then
			if legs_num8 > 1 then
				for kkk = 1, legs_num8 + 1 do
					legs_data8[kkk][17] = 0
				end
			end
		end
		if legs_num == 0 then
			calc_rte_enable = 0
			vnav_update = 1
		end
		B738DR_dest_runway_alt = -999
	end
	
	if calc_rte_enable == 2 then
		
		find_ref_rnw_data(0, rte12_act)
		find_des_rnw_data(0, rte12_act)
		if rte12_act == 0 then
			des_rnw = des_rnw2
		elseif rte12_act == 1 then
			des_rnw7 = des_rnw8
		end
		
		find_sid_app_idx()
		
		copy_to_legsdata2(rte12_act)
		
		B738_find_rnav()
		calc_rte_enable = 0
		B738DR_dest_runway_len = 0
		B738DR_dest_runway_crs = 0
		B738DR_dest_runway_start_lat = 0
		B738DR_dest_runway_start_lon = 0
		B738DR_dest_runway_end_lat = 0
		B738DR_dest_runway_end_lon = 0
		B738DR_dest_runway_id = ""
		rnw_alt_found = 0
		
		if rte12_act == 0 then
			if ref_rwy ~= "-----" then
				ref_rnw_length_crs(rte12_act)
				-- find ref runway alt
				if ref_rwy_num > 0 then
					if string.len(ref_rwy) == 2 then
						rnw_temp = ref_rwy .. " "
					else
						rnw_temp = ref_rwy
					end
					for jj = 1, ref_rwy_num do
						if ref_data_rwy[jj][1] == rnw_temp then
							kk = tonumber(ref_data_rwy[jj][2])
							if kk ~= nil then
								B738DR_ref_rwy_altitude = kk
								rnw_alt_found = 1
							end
							break
						end
					end
				end
			end
			if rnw_alt_found == 0 then
				B738DR_ref_rwy_altitude = ref_icao_alt
			end
			rnw_alt_found = 0
			B738DR_missed_app_alt = 0
			if des_app ~= "------" then
				if B738DR_dest_runway_alt == -999 then
					B738DR_dest_runway_alt = des_icao_alt
				end
				des_rnw_length_crs(rte12_act)
				-- find des runway alt
				if des_rwy_num > 0 then
					if string.sub(des_app, 1, 2) == "RW" then
						rnw_temp = des_app
					else
						if string.len(des_app) > 4 then
							jj, kk = string.find(des_app, "-")
							if jj == nil then
								rnw_temp = "RW" .. string.sub(des_app, 2, -2)
							else
								rnw_temp = "RW" .. string.sub(des_app, 2, jj-1)
							end
						else
							rnw_temp = "RW" .. string.sub(des_app, 2, -1)
						end
					end
					if string.len(rnw_temp) == 4 then
						rnw_temp = rnw_temp .. " "
					end
					for jj = 1, des_rwy_num do
						if des_data_rwy[jj][1] == rnw_temp then
							kk = tonumber(des_data_rwy[jj][5])
							if kk ~= nil then
								B738DR_des_rwy_altitude = kk
								rnw_alt_found = 1
							end
							break
						end
					end
				end
				B738DR_missed_app_alt = legs_data[legs_num][5]
			end
			if rnw_alt_found == 0 then
				B738DR_des_rwy_altitude = des_icao_alt
			end
		elseif rte12_act == 1 then
			if ref_rwy7 ~= "-----" then
				ref_rnw_length_crs(rte12_act)
				-- find ref runway alt
				if ref_rwy_num > 0 then
					if string.len(ref_rwy7) == 2 then
						rnw_temp = ref_rwy7 .. " "
					else
						rnw_temp = ref_rwy7
					end
					for jj = 1, ref_rwy_num do
						if ref_data_rwy[jj][1] == rnw_temp then
							kk = tonumber(ref_data_rwy[jj][2])
							if kk ~= nil then
								B738DR_ref_rwy_altitude = kk
								rnw_alt_found = 1
							end
							break
						end
					end
				end
			end
			if rnw_alt_found == 0 then
				B738DR_ref_rwy_altitude = ref_icao_alt7
			end
			rnw_alt_found = 0
			B738DR_missed_app_alt = 0
			if des_app7 ~= "------" then
				if B738DR_dest_runway_alt == -999 then
					B738DR_dest_runway_alt = des_icao_alt7
				end
				des_rnw_length_crs(rte12_act)
				-- find des runway alt
				if des_rwy_num > 0 then
					if string.sub(des_app7, 1, 2) == "RW" then
						rnw_temp = des_app7
					else
						if string.len(des_app7) > 4 then
							jj, kk = string.find(des_app7, "-")
							if jj == nil then
								rnw_temp = "RW" .. string.sub(des_app7, 2, -2)
							else
								rnw_temp = "RW" .. string.sub(des_app7, 2, jj-1)
							end
						else
							rnw_temp = "RW" .. string.sub(des_app7, 2, -1)
						end
					end
					if string.len(rnw_temp) == 4 then
						rnw_temp = rnw_temp .. " "
					end
					for jj = 1, des_rwy_num do
						if des_data_rwy[jj][1] == rnw_temp then
							kk = tonumber(des_data_rwy[jj][5])
							if kk ~= nil then
								B738DR_des_rwy_altitude = kk
								rnw_alt_found = 1
							end
							break
						end
					end
				end
				B738DR_missed_app_alt = legs_data[legs_num][5]
			end
			if rnw_alt_found == 0 then
				B738DR_des_rwy_altitude = des_icao_alt7
			end
		end
		find_id_app_data(rte12_act)
		
		local dist_tmp = 0
		for iii = legs_num, 1, -1 do
			dist_tmp = dist_tmp + wpt_distance(iii+1)
			legs_data[iii][50] = dist_tmp
			legs_data[iii][25] = ii	--waypoint idx before
			legs_data[iii][52] = ii	--waypoint idx before
		end
		
		vnav_update = 1
	end

end



function B738_read_calc_data()
	
	if ffirst_time2 ~= 0 then
		if calc_rte_enable == 0 then
			if (calc_rte_enable2 ~= 0 and rte12_act == 0) or (calc_rte_enable8 ~= 0 and rte12_act == 1) then
				B738DR_fms_add_data_num = 0
				B738DR_fms_add_cmd = 0
			elseif legs_num > 0 then
				for ii = 1, legs_num + 1 do
					legs_data[ii][50] = B738DR_fms_legs_dist_dest_t[ii-1]
					legs_data[ii][51] = B738DR_fms_legs_dist_t[ii-1]
				end
				if B738DR_fms_add_cmd == 1 then
					if B738DR_fms_add_data_num > 0 then
						for ii = 1, B738DR_fms_add_data_num do
							if B738DR_fms_add_data_1[ii-1] > 0 and B738DR_fms_add_data_1[ii-1] <= legs_num then
								legs_data[B738DR_fms_add_data_1[ii-1]][7] = B738DR_fms_add_data_2[ii-1]		-- Lat
								legs_data[B738DR_fms_add_data_1[ii-1]][8] = B738DR_fms_add_data_3[ii-1]		-- Lon
								legs_data[B738DR_fms_add_data_1[ii-1]][2] = B738DR_fms_add_data_4[ii-1]		-- Brg
								legs_data[B738DR_fms_add_data_1[ii-1]][3] = B738DR_fms_add_data_5[ii-1]		-- Dist
								legs_data[B738DR_fms_add_data_1[ii-1]][18] = B738DR_fms_add_data_6[ii-1]	-- Crs
								legs_data[B738DR_fms_add_data_1[ii-1]][37] = B738DR_fms_add_data_4[ii-1]	--B738DR_fms_add_data_7[ii-1]	-- Final Brg
							end
						end
					end
					
					if rte12_act == 0 then
						if calc_rte_enable2 == 0 and legs_delete == 0 and legs_intdir == 0 and hold_exec == 0 and rte_exec == 0 then
							copy_to_legsdata2(rte12_act)
						end
					elseif rte12_act == 1 then
						if calc_rte_enable8 == 0 and legs_delete == 0 and legs_intdir == 0 and hold_exec2 == 0 and rte_exec2 == 0 then
							copy_to_legsdata2(rte12_act)
						end
					end
				end
				B738DR_fms_add_data_num = 0
				B738DR_fms_add_cmd = 0
			end
		else
			B738DR_fms_add_data_num = 0
			B738DR_fms_add_cmd = 0
		end
	end
end

function inter(cx, cy, cr, p1x, p1y, p2x, p2y)

	local dx = 0
	local dy = 0
	local a = 0
	local b = 0
	local c = 0
	local det = 0
	local t = 0
	local result = 0

	local c_const_lat = 60
	local c_const_lon = math.cos(math.rad(cx)) * c_const_lat
	local clat = cx * c_const_lat
	local clon = cy * c_const_lon

	local p1lat = p1x * c_const_lat
	local p1lon = p1y * c_const_lon
	local p2lat = p2x * c_const_lat
	local p2lon = p2y * c_const_lon

	inter1_x = 0
	inter1_y = 0
	inter2_x = 0
	inter2_y = 0

	dx = p2lat - p1lat
	dy = p2lon - p1lon

	a = dx * dx + dy * dy
	b = 2 * (dx * (p1lat - clat) + dy * (p1lon - clon))
	c = (p1lat - clat) * (p1lat - clat) + (p1lon - clon) * (p1lon - clon) - cr * cr

	det = b * b - 4 * a * c
	if (a <= 0.0000001) or (det < 0) then
		result = 0;
	elseif det == 0 then
		result = 1
		t = -b / (2 * a)
		inter1_x = p1lat + t * dx
		inter1_y = p1lon + t * dy
		inter1_x = inter1_x / c_const_lat
		inter1_y = inter1_y / c_const_lon
		inter2_x = inter1_x
		inter2_y = inter1_y
	else
		result = 2
		t = (-b + math.sqrt(det)) / (2 * a)
		inter1_x = p1lat + t * dx
		inter1_y = p1lon + t * dy
		inter1_x = inter1_x / c_const_lat
		inter1_y = inter1_y / c_const_lon
		t = (-b - math.sqrt(det)) / (2 * a)
		inter2_x = p1lat + t * dx
		inter2_y = p1lon + t * dy
		inter2_x = inter2_x / c_const_lat
		inter2_y = inter2_y / c_const_lon
	end
	
	-- calc_lat = inter1_x
	-- calc_lon = inter1_y
	-- calc_lat2 = inter2_x
	-- calc_lon2 = inter2_y
	
	return result
end

function B738_calc_rte2()

	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local find_txt = ""
	local delta_crs = 0
	local lat_tmp = 0
	local lon_tmp = 0
	local lat_tmp2 = 0
	local lon_tmp2 = 0
	local max_wpt_calc = 5
	local brg_tmp = 0
	local dist_tmp = 0
	
	local rel_crs_tmp = 0
	local pom_crs = 0
	
	local calc_enable = 0
	if rte_add_sid_act == 0 and rte_add_star_act == 0 and rte_add_app_act == 0 then	--and mod_offset == 0 then
		calc_enable = 1
	end
	if calc_rte_enable ~= 0 or (calc_rte_enable8 ~= 0 and rte12_act == 1) then
		calc_enable = 0
	end
	
	if calc_enable == 1 then
		if calc_rte_enable2 == 1 then
			calc_rte_enable2 = 2
			rte_calc_lat2 = 0
			rte_calc_lon2 = 0
			if legs_num2 == 0 then
				calc_rte_enable2 = 0
				calc_rte_act2 = 0
			else
				calc_rte_act2 = 1
				-- dump_leg_append("calc_rte_act2 = " .. tostring(calc_rte_act2) .. "\n")
				-- dump_leg()
			end
		end
		if calc_rte_enable2 == 2 then
			calc_rte_act2_init = calc_rte_act2
			while calc_rte_act2 - calc_rte_act2_init < max_wpt_calc do
				if calc_rte_act2 > legs_num2 + 1 then
					calc_rte_enable2 = 3
					-- dump_leg_append("calc_rte_act2 = " .. tostring(calc_rte_act2) .. "\n")
					-- dump_leg()
					break
				elseif calc_rte_act2 == 1 then
					-- dump_leg_append("calc_rte_act2 = " .. tostring(calc_rte_act2) .. "\n")
					-- dump_leg()
					B738DR_fms_test1 = 71
					find_ref_rnw_data(1, 0)
					find_des_rnw_data(1, 0)
				else
					-- dump_leg_append("calc_rte_act2 = " .. tostring(calc_rte_act2) .. "\n")
					-- dump_leg()
					B738DR_fms_test1 = 72
					-- B738DR_freeze = 7
					if legs_data2[calc_rte_act2][7] == 0 and legs_data2[calc_rte_act2][8] == 0 then
						if legs_data2[calc_rte_act2][1] == "DISCONTINUITY" then
							legs_data2[calc_rte_act2][7] = legs_data2[calc_rte_act2-1][7]
							legs_data2[calc_rte_act2][8] = legs_data2[calc_rte_act2-1][8]
							legs_data2[calc_rte_act2][2] = legs_data2[calc_rte_act2-1][2]		-- brg
							legs_data2[calc_rte_act2][18] = 0
							legs_data2[calc_rte_act2][3] = 0		-- distance
						elseif legs_data2[calc_rte_act2][1] == "PPOS" then
							legs_data2[calc_rte_act2][2] = 0
							legs_data2[calc_rte_act2][18] = 0
							legs_data2[calc_rte_act2][3] = 0		-- distance
						else
							find_txt = legs_data2[calc_rte_act2][1]
							if string.len(find_txt) == 0 then
								find_txt = "ZZZZZZZZ"
							end
							if string.sub(find_txt, 1, 2) == "RW" then
								if legs_data2[calc_rte_act2][19] == 7 or legs_data2[calc_rte_act2][19] == 8 
								or legs_data2[calc_rte_act2][19] == 2 or legs_data2[calc_rte_act2][19] == 9 then
									
									brg_tmp = tonumber(legs_data2[calc_rte_act2][29])
									if brg_tmp == nil then
										brg_tmp = -1
									else
										brg_tmp = brg_tmp / 10
									end
									dist_tmp = tonumber(legs_data2[calc_rte_act2][30])
									if dist_tmp == nil then
										dist_tmp = -1
									else
										dist_tmp = dist_tmp / 10
									end
									
									if legs_data2[calc_rte_act2-1][44] == legs_data2[calc_rte_act2][44] - 10 
									and brg_tmp >= 0 and dist_tmp >= 0 then
										
										calc_brg_dist(des_runway_lat_mod, des_runway_lon_mod, math.rad(des_runway_crs_mod), 5)
										jj = inter(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8], dist_tmp, des_runway_lat_mod, des_runway_lon_mod, calc_lat, calc_lon)
										
										if jj == 1 then
											brg_tmp = (brg_tmp - mag_variation_deg(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8]) + 360) % 360
											ll = nd_calc_brg(des_runway_lat_mod, des_runway_lon_mod, inter1_x, inter1_y)
											ll = (ll - des_runway_crs_mod + 360) % 360
											if ll > 180 then
												ll = ll - 360
											end
											if math.abs(ll) <= 90 then
												kk = nd_calc_brg(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8], inter1_x, inter1_y)
												kk = (kk - brg_tmp + 360) % 360
												if kk > 180 then
													kk = kk - 360
												end
												if math.abs(kk) <= 10 then
													legs_data2[calc_rte_act2][7] = inter1_x
													legs_data2[calc_rte_act2][8] = inter1_y
												else
													jj = 0
												end
											else
												jj = 0
											end
										elseif jj == 2 then
											brg_tmp = (brg_tmp - mag_variation_deg(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8]) + 360) % 360
											ll = nd_calc_brg(des_runway_lat_mod, des_runway_lon_mod, inter1_x, inter1_y)
											ll = (ll - des_runway_crs_mod + 360) % 360
											if ll > 180 then
												ll = ll - 360
											end
											kk = nd_calc_brg(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8], inter1_x, inter1_y)
											kk = (kk - brg_tmp + 360) % 360
											if kk > 180 then
												kk = kk - 360
											end
											if math.abs(kk) <= 10 and math.abs(ll) <= 90 then
												legs_data2[calc_rte_act2][7] = inter1_x
												legs_data2[calc_rte_act2][8] = inter1_y
											else
												ll = nd_calc_brg(des_runway_lat_mod, des_runway_lon_mod, inter2_x, inter2_y)
												ll = (ll - des_runway_crs_mod + 360) % 360
												if ll > 180 then
													ll = ll - 360
												end
												if math.abs(ll) <= 90 then
													kk = nd_calc_brg(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8], inter2_x, inter2_y)
													kk = (kk - brg_tmp + 360) % 360
													if kk > 180 then
														kk = kk - 360
													end
													if math.abs(kk) <= 10 then
														legs_data2[calc_rte_act2][7] = inter2_x
														legs_data2[calc_rte_act2][8] = inter2_y
													else
														jj = 0
													end
												else
													jj = 0
												end
											end
										else
											jj = 0
										end
										
										if jj == 0 then
											legs_data2[calc_rte_act2][7] = des_runway_lat_mod
											legs_data2[calc_rte_act2][8] = des_runway_lon_mod
										end
										
									else
										legs_data2[calc_rte_act2][7] = des_runway_lat_mod
										legs_data2[calc_rte_act2][8] = des_runway_lon_mod
									end
									
									legs_data2[calc_rte_act2][46] = des_runway_end_lat_mod
									legs_data2[calc_rte_act2][47] = des_runway_end_lon_mod
									
									legs_data2[calc_rte_act2][2] = nd_calc_brg_rad(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8], legs_data2[calc_rte_act2][7], legs_data2[calc_rte_act2][8])
									legs_data2[calc_rte_act2][18] = legs_data2[calc_rte_act2][2] + mag_variation_rad(math.rad(legs_data2[calc_rte_act2][7]), math.rad(legs_data2[calc_rte_act2][8]))
									if legs_data2[calc_rte_act2][18] < 0 then
										legs_data2[calc_rte_act2][18] = legs_data2[calc_rte_act2][18] + (2 * math.pi)
									end
									legs_data2[calc_rte_act2][3] = nd_calc_dist2(legs_data2[calc_rte_act2-1][7], legs_data2[calc_rte_act2-1][8], legs_data2[calc_rte_act2][7], legs_data2[calc_rte_act2][8])
								else
									dec_lat_lon2(calc_rte_act2, 0)
									legs_data2[calc_rte_act2][36] = dec_find_type
								end
							else
								dec_lat_lon2(calc_rte_act2, 0)
								legs_data2[calc_rte_act2][36] = dec_find_type
							end
						end
					else
						if max_wpt_calc < 10 then
							max_wpt_calc = max_wpt_calc + 1
						end
					end
					
					if legs_data2[calc_rte_act2][31] == "" then
						legs_data2[calc_rte_act2][31] = "TF"
					end
					
					lat_tmp, lon_tmp = lat_lon_legs2(calc_rte_act2)
					lat_tmp2, lon_tmp2 = lat_lon_legs2(calc_rte_act2-1)
					
					B738DR_fms_test1 = 721
					
					legs_data2[calc_rte_act2][3] = nd_calc_dist2(lat_tmp, lon_tmp, lat_tmp2, lon_tmp2)
					
					if string.sub(legs_data2[calc_rte_act2][31], -1, -1) ~= "F" and legs_data2[calc_rte_act2][31] ~= "" and legs_data2[calc_rte_act2][31] ~= "PI" 
					and legs_data2[calc_rte_act2][3] > 1000 and calc_rte_act2 <= legs_num2 then
						legs_data2[calc_rte_act2][3] = 0
						legs_data2[calc_rte_act2][7] = lat_tmp2
						legs_data2[calc_rte_act2][8] = lon_tmp2
					end
					if legs_data2[calc_rte_act2][31] == "FM" or legs_data2[calc_rte_act2][31] == "VM" then
						legs_data2[calc_rte_act2][2] = legs_data2[calc_rte_act2][18] - mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))
						if legs_data2[calc_rte_act2][2] < 0 then
							legs_data2[calc_rte_act2][2] = legs_data2[calc_rte_act2][2] + (2 * math.pi)
						end
					elseif legs_data2[calc_rte_act2][31] == "HM" or legs_data2[calc_rte_act2][31] == "HA" or legs_data2[calc_rte_act2][31] == "HF" then
						legs_data2[calc_rte_act2][2] = legs_data2[calc_rte_act2][18] - mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))
						if legs_data2[calc_rte_act2][2] < 0 then
							legs_data2[calc_rte_act2][2] = legs_data2[calc_rte_act2][2] + (2 * math.pi)
						end
					elseif string.sub(legs_data2[calc_rte_act2][1], 1, 2) ~= "RW" or calc_rte_act2 ~= 2 then
						if legs_data2[calc_rte_act2][3] == 0 and calc_rte_act2 > 1 then
							legs_data2[calc_rte_act2][2] = legs_data2[calc_rte_act2-1][2]
						else
							if legs_data2[calc_rte_act2][31] == "TF" or legs_data2[calc_rte_act2][31] == "IF" or legs_data2[calc_rte_act2][31] == "" then
								legs_data2[calc_rte_act2][2] = nd_calc_brg_rad(lat_tmp2, lon_tmp2, lat_tmp, lon_tmp)
							else
								legs_data2[calc_rte_act2][2] = math.rad(nd_calc_brg(lat_tmp2, lon_tmp2, lat_tmp, lon_tmp))
							end
							
							
						end
						if legs_data2[calc_rte_act2][31] == "TF" or legs_data2[calc_rte_act2][31] == "" or legs_data2[calc_rte_act2][31] == "IF" then
							legs_data2[calc_rte_act2][18] = legs_data2[calc_rte_act2][2] + mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))
							if legs_data2[calc_rte_act2][18] < 0 then
								legs_data2[calc_rte_act2][18] = legs_data2[calc_rte_act2][18] + (2 * math.pi)
							end
						end
					end
					if legs_data2[calc_rte_act2][31] == "TF" or legs_data2[calc_rte_act2][31] == "IF" or legs_data2[calc_rte_act2][31] == "" then
						if string.sub(legs_data2[calc_rte_act2][1], 1, 2) == "RW" then
							legs_data2[calc_rte_act2][37] = legs_data2[calc_rte_act2][18] - mag_variation_rad(math.rad(legs_data2[calc_rte_act2][7]), math.rad(legs_data2[calc_rte_act2][8]))
						else
							delta_crs = (nd_calc_brg(lat_tmp, lon_tmp, lat_tmp2, lon_tmp2) + 180) % 360
							legs_data2[calc_rte_act2][37] = math.rad(delta_crs)
						end
					else
						legs_data2[calc_rte_act2][37] = legs_data2[calc_rte_act2][2]
					end
					
					B738DR_fms_test1 = 722
					
					
				end
				
				if calc_rte_act2 > 1 then
					if legs_data2[calc_rte_act2-1][1] == "DISCONTINUITY" then
						legs_data2[calc_rte_act2][9] = ""
					end
				end
				
				calc_rte_act2 = calc_rte_act2 + 1
				if calc_rte_act2 > legs_num2 + 1 then
					calc_rte_enable2 = 3
					break
				end
			end
			--B738DR_freeze = 8
		elseif calc_rte_enable2 == 3 then
				B738DR_fms_test1 = 73
				del_if(1, 0)
				if del_execute == 1 then
					-- recalc
					calc_rte_enable2 = 1
				else
					calc_rte_enable2 = 4
				end
		elseif calc_rte_enable2 == 4 then
				B738DR_fms_test1 = 74
				find_sid_app_idx_mod(0)
				calc_rte_enable2 = 5
		elseif calc_rte_enable2 == 5 then
				B738DR_fms_test1 = 75
				create_fpln(0)
				calc_rte_enable2 = 6
		elseif calc_rte_enable2 == 6 then
				B738DR_fms_test1 = 76
				B738_find_rnav_mod(0)
				calc_rte_enable2 = 0
				B738DR_dest_runway_len_mod = 0
				B738DR_dest_runway_crs_mod = 0
				B738DR_dest_runway_start_lat_mod = 0
				B738DR_dest_runway_start_lon_mod = 0
				B738DR_dest_runway_end_lat_mod = 0
				B738DR_dest_runway_end_lon_mod = 0
				if ref_rwy2 ~= "-----" then
					ref_rnw_length_crs2(0)
				else
					B738DR_ref_runway_len_mod = 0
					B738DR_ref_runway_crs_mod = 0
					B738DR_ref_runway_start_lat_mod = 0
					B738DR_ref_runway_start_lon_mod = 0
					B738DR_ref_runway_end_lat_mod = 0
					B738DR_ref_runway_end_lon_mod = 0
				end
				
				des_rnw_length_crs2(0)
			local dist_tmp = 0
			for iii = legs_num2, 1, -1 do
				dist_tmp = dist_tmp + wpt_distance2(iii+1, 0)
				legs_data2[iii][50] = dist_tmp
			end
		end
	end

end

function B738_calc_rte8()

	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local find_txt = ""
	local delta_crs = 0
	local lat_tmp = 0
	local lon_tmp = 0
	local lat_tmp2 = 0
	local lon_tmp2 = 0
	local max_wpt_calc = 5
	local brg_tmp = 0
	local dist_tmp = 0
	
	local rel_crs_tmp = 0
	local pom_crs = 0
	
	local calc_enable = 0
	if rte_add_sid_act == 0 and rte_add_star_act == 0 and rte_add_app_act == 0 then		---and mod_offset == 0 then
		calc_enable = 1
	end
	-- if calc_rte_enable7 ~= 0 or (calc_rte_enable2 ~= 0 and rte12_act == 0) then
	if calc_rte_enable ~= 0 or (calc_rte_enable2 ~= 0 and rte12_act == 0) then
		calc_enable = 0
	end
	
	if calc_enable == 1 then
		if calc_rte_enable8 == 1 then
			calc_rte_enable8 = 2
			rte_calc_lat2 = 0
			rte_calc_lon2 = 0
			if legs_num8 == 0 then
				calc_rte_enable8 = 0
				calc_rte_act8 = 0
			else
				calc_rte_act8 = 1
			end
		end
		if calc_rte_enable8 == 2 then
			calc_rte_act8_init = calc_rte_act8
			while calc_rte_act8 - calc_rte_act8_init < max_wpt_calc do
				if calc_rte_act8 > legs_num8 + 1 then
					calc_rte_enable8 = 3
					break
				elseif calc_rte_act8 == 1 then
					B738DR_fms_test1 = 71
					find_ref_rnw_data(1, 1)
					find_des_rnw_data(1, 1)
				else
					B738DR_fms_test1 = 72
					-- B738DR_freeze = 7
					if legs_data8[calc_rte_act8][7] == 0 and legs_data8[calc_rte_act8][8] == 0 then
						if legs_data8[calc_rte_act8][1] == "DISCONTINUITY" then
							legs_data8[calc_rte_act8][7] = legs_data8[calc_rte_act8-1][7]
							legs_data8[calc_rte_act8][8] = legs_data8[calc_rte_act8-1][8]
							legs_data8[calc_rte_act8][2] = legs_data8[calc_rte_act8-1][2]		-- brg
							legs_data8[calc_rte_act8][18] = 0
							legs_data8[calc_rte_act8][3] = 0		-- distance
						elseif legs_data8[calc_rte_act8][1] == "PPOS" then
							legs_data8[calc_rte_act8][2] = 0
							legs_data8[calc_rte_act8][18] = 0
							legs_data8[calc_rte_act8][3] = 0		-- distance
						else
							find_txt = legs_data8[calc_rte_act8][1]
							if string.len(find_txt) == 0 then
								find_txt = "ZZZZZZZZ"
							end
							if string.sub(find_txt, 1, 2) == "RW" then
								if legs_data8[calc_rte_act8][19] == 7 or legs_data8[calc_rte_act8][19] == 8 
								or legs_data8[calc_rte_act8][19] == 2 or legs_data8[calc_rte_act8][19] == 9 then
									
									brg_tmp = tonumber(legs_data8[calc_rte_act8][29])
									if brg_tmp == nil then
										brg_tmp = -1
									else
										brg_tmp = brg_tmp / 10
									end
									dist_tmp = tonumber(legs_data8[calc_rte_act8][30])
									if dist_tmp == nil then
										dist_tmp = -1
									else
										dist_tmp = dist_tmp / 10
									end
									
									if legs_data8[calc_rte_act8-1][44] == legs_data8[calc_rte_act8][44] - 10 
									and brg_tmp >= 0 and dist_tmp >= 0 then
										
										calc_brg_dist(des_runway_lat_mod2, des_runway_lon_mod2, math.rad(des_runway_crs_mod2), 5)
										jj = inter(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8], dist_tmp, des_runway_lat_mod2, des_runway_lon_mod2, calc_lat, calc_lon)
										
										if jj == 1 then
											brg_tmp = (brg_tmp - mag_variation_deg(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8]) + 360) % 360
											ll = nd_calc_brg(des_runway_lat_mod2, des_runway_lon_mod2, inter1_x, inter1_y)
											ll = (ll - des_runway_crs_mod2 + 360) % 360
											if ll > 180 then
												ll = ll - 360
											end
											if math.abs(ll) <= 90 then
												kk = nd_calc_brg(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8], inter1_x, inter1_y)
												kk = (kk - brg_tmp + 360) % 360
												if kk > 180 then
													kk = kk - 360
												end
												if math.abs(kk) <= 10 then
													legs_data8[calc_rte_act8][7] = inter1_x
													legs_data8[calc_rte_act8][8] = inter1_y
												else
													jj = 0
												end
											else
												jj = 0
											end
										elseif jj == 2 then
											brg_tmp = (brg_tmp - mag_variation_deg(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8]) + 360) % 360
											ll = nd_calc_brg(des_runway_lat_mod2, des_runway_lon_mod2, inter1_x, inter1_y)
											ll = (ll - des_runway_crs_mod2 + 360) % 360
											if ll > 180 then
												ll = ll - 360
											end
											kk = nd_calc_brg(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8], inter1_x, inter1_y)
											kk = (kk - brg_tmp + 360) % 360
											if kk > 180 then
												kk = kk - 360
											end
											if math.abs(kk) <= 10 and math.abs(ll) <= 90 then
												legs_data8[calc_rte_act8][7] = inter1_x
												legs_data8[calc_rte_act8][8] = inter1_y
											else
												ll = nd_calc_brg(des_runway_lat_mod2, des_runway_lon_mod2, inter2_x, inter2_y)
												ll = (ll - des_runway_crs_mod2 + 360) % 360
												if ll > 180 then
													ll = ll - 360
												end
												if math.abs(ll) <= 90 then
													kk = nd_calc_brg(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8], inter2_x, inter2_y)
													kk = (kk - brg_tmp + 360) % 360
													if kk > 180 then
														kk = kk - 360
													end
													if math.abs(kk) <= 10 then
														legs_data8[calc_rte_act8][7] = inter2_x
														legs_data8[calc_rte_act8][8] = inter2_y
													else
														jj = 0
													end
												else
													jj = 0
												end
											end
										else
											jj = 0
										end
										
										if jj == 0 then
											legs_data8[calc_rte_act8][7] = des_runway_lat_mod2
											legs_data8[calc_rte_act8][8] = des_runway_lon_mod2
										end
										
									else
										legs_data8[calc_rte_act8][7] = des_runway_lat_mod2
										legs_data8[calc_rte_act8][8] = des_runway_lon_mod2
									end
									
									legs_data8[calc_rte_act8][46] = des_runway_end_lat_mod2
									legs_data8[calc_rte_act8][47] = des_runway_end_lon_mod2
									
									legs_data8[calc_rte_act8][2] = nd_calc_brg_rad(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8], legs_data8[calc_rte_act8][7], legs_data8[calc_rte_act8][8])
									legs_data8[calc_rte_act8][18] = legs_data8[calc_rte_act8][2] + mag_variation_rad(math.rad(legs_data8[calc_rte_act8][7]), math.rad(legs_data8[calc_rte_act8][8]))
									if legs_data8[calc_rte_act8][18] < 0 then
										legs_data8[calc_rte_act8][18] = legs_data8[calc_rte_act8][18] + (2 * math.pi)
									end
									legs_data8[calc_rte_act8][3] = nd_calc_dist2(legs_data8[calc_rte_act8-1][7], legs_data8[calc_rte_act8-1][8], legs_data8[calc_rte_act8][7], legs_data8[calc_rte_act8][8])
								else
									dec_lat_lon2(calc_rte_act8, 1)
									legs_data8[calc_rte_act8][36] = dec_find_type
								end
							else
								dec_lat_lon2(calc_rte_act8, 1)
								legs_data8[calc_rte_act8][36] = dec_find_type
							end
						end
					else
						if max_wpt_calc < 10 then
							max_wpt_calc = max_wpt_calc + 1
						end
					end
					
					if legs_data8[calc_rte_act8][31] == "" then
						legs_data8[calc_rte_act8][31] = "TF"
					end
					
					lat_tmp, lon_tmp = lat_lon_legs8(calc_rte_act8)
					lat_tmp2, lon_tmp2 = lat_lon_legs8(calc_rte_act8-1)
					
					B738DR_fms_test1 = 721
					
					legs_data8[calc_rte_act8][3] = nd_calc_dist2(lat_tmp, lon_tmp, lat_tmp2, lon_tmp2)
					
					if string.sub(legs_data8[calc_rte_act8][31], -1, -1) ~= "F" and legs_data8[calc_rte_act8][31] ~= "" and legs_data8[calc_rte_act8][31] ~= "PI" 
					and legs_data8[calc_rte_act8][3] > 1000 and calc_rte_act8 <= legs_num8 then
						legs_data8[calc_rte_act8][3] = 0
						legs_data8[calc_rte_act8][7] = lat_tmp2
						legs_data8[calc_rte_act8][8] = lon_tmp2
					end
					if legs_data8[calc_rte_act8][31] == "FM" or legs_data8[calc_rte_act8][31] == "VM" then
						legs_data8[calc_rte_act8][2] = legs_data8[calc_rte_act8][18] - mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))
						if legs_data8[calc_rte_act8][2] < 0 then
							legs_data8[calc_rte_act8][2] = legs_data8[calc_rte_act8][2] + (2 * math.pi)
						end
					elseif legs_data8[calc_rte_act8][31] == "HM" or legs_data8[calc_rte_act8][31] == "HA" or legs_data8[calc_rte_act8][31] == "HF" then
						legs_data8[calc_rte_act8][2] = legs_data8[calc_rte_act8][18] - mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))
						if legs_data8[calc_rte_act8][2] < 0 then
							legs_data8[calc_rte_act8][2] = legs_data8[calc_rte_act8][2] + (2 * math.pi)
						end
					elseif string.sub(legs_data8[calc_rte_act8][1], 1, 2) ~= "RW" or calc_rte_act8 ~= 2 then
						if legs_data8[calc_rte_act8][3] == 0 and calc_rte_act8 > 1 then
							legs_data8[calc_rte_act8][2] = legs_data8[calc_rte_act8-1][2]
						else
							if legs_data8[calc_rte_act8][31] == "TF" or legs_data8[calc_rte_act8][31] == "IF" or legs_data8[calc_rte_act8][31] == "" then
								legs_data8[calc_rte_act8][2] = nd_calc_brg_rad(lat_tmp2, lon_tmp2, lat_tmp, lon_tmp)
							else
								legs_data8[calc_rte_act8][2] = math.rad(nd_calc_brg(lat_tmp2, lon_tmp2, lat_tmp, lon_tmp))
							end
							
							
						end
						if legs_data8[calc_rte_act8][31] == "TF" or legs_data8[calc_rte_act8][31] == "" or legs_data8[calc_rte_act8][31] == "IF" then
							legs_data8[calc_rte_act8][18] = legs_data8[calc_rte_act8][2] + mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))
							if legs_data8[calc_rte_act8][18] < 0 then
								legs_data8[calc_rte_act8][18] = legs_data8[calc_rte_act8][18] + (2 * math.pi)
							end
						end
					end
					if legs_data8[calc_rte_act8][31] == "TF" or legs_data8[calc_rte_act8][31] == "IF" or legs_data8[calc_rte_act8][31] == "" then
						if string.sub(legs_data8[calc_rte_act8][1], 1, 2) == "RW" then
							legs_data8[calc_rte_act8][37] = legs_data8[calc_rte_act8][18] - mag_variation_rad(math.rad(legs_data8[calc_rte_act8][7]), math.rad(legs_data8[calc_rte_act8][8]))
						else
							delta_crs = (nd_calc_brg(lat_tmp, lon_tmp, lat_tmp2, lon_tmp2) + 180) % 360
							legs_data8[calc_rte_act8][37] = math.rad(delta_crs)
						end
					else
						legs_data8[calc_rte_act8][37] = legs_data8[calc_rte_act8][2]
					end
					
					B738DR_fms_test1 = 722
					
				end
				
				if calc_rte_act8 > 1 then
					if legs_data8[calc_rte_act8-1][1] == "DISCONTINUITY" then
						legs_data8[calc_rte_act8][9] = ""
					end
				end
				
				calc_rte_act8 = calc_rte_act8 + 1
				if calc_rte_act8 > legs_num8 + 1 then
					calc_rte_enable8 = 3
					break
				end
			end
			--B738DR_freeze = 8
		elseif calc_rte_enable8 == 3 then
				B738DR_fms_test1 = 73
				del_if(1, 1)
				if del_execute == 1 then
					-- recalc
					calc_rte_enable8 = 1
				else
					calc_rte_enable8 = 4
				end
		elseif calc_rte_enable8 == 4 then
				B738DR_fms_test1 = 74
				find_sid_app_idx_mod(1)
				calc_rte_enable8 = 5
		elseif calc_rte_enable8 == 5 then
				B738DR_fms_test1 = 75
				create_fpln(1)
				calc_rte_enable8 = 6
		elseif calc_rte_enable8 == 6 then
				B738DR_fms_test1 = 76
				B738_find_rnav_mod(1)
				calc_rte_enable8 = 0
				B738DR_dest_runway_len_mod2 = 0
				B738DR_dest_runway_crs_mod2 = 0
				B738DR_dest_runway_start_lat_mod2 = 0
				B738DR_dest_runway_start_lon_mod2 = 0
				B738DR_dest_runway_end_lat_mod2 = 0
				B738DR_dest_runway_end_lon_mod2 = 0
				if ref_rwy8 ~= "-----" then
					ref_rnw_length_crs2(1)
				else
					B738DR_ref_runway_len_mod2 = 0
					B738DR_ref_runway_crs_mod2 = 0
					B738DR_ref_runway_start_lat_mod2 = 0
					B738DR_ref_runway_start_lon_mod2 = 0
					B738DR_ref_runway_end_lat_mod2 = 0
					B738DR_ref_runway_end_lon_mod2 = 0
				end
				
				des_rnw_length_crs2(1)
			local dist_tmp = 0
			for iii = legs_num8, 1, -1 do
				dist_tmp = dist_tmp + wpt_distance2(iii+1, 1)
				legs_data8[iii][50] = dist_tmp
			end
		end
	end

end

--findnavaidsk
-- nav mode => distance from 0-PPOS, 1-REF ICAO, 2-DES ICAO, 3-By lat,lon (find_lat, find_lon)
function find_navaid(nav_id, nav_icao, nav_mode0, nav_reg_code, in_rte)
	
	-- B738DR_freeze = 3

	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nav_id_ok = 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	
	local idx_rec = 0
	
	if string.len(nav_icao) > 0 then
		kk = 1
	end
	if string.len(nav_reg_code) > 0 then
		ll = 1
	end
	
	navaid_list = {}
	navaid_list_n = 0
	
	if earth_nav_num > 0 and string.len(nav_id) > 0 then
		nd_x = string.byte(string.sub(nav_id, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_earth_nav[nd_x][99999] > 0 then
				for ii = 1, idx_earth_nav[nd_x][99999] do
					idx_rec = idx_earth_nav[nd_x][ii]
					
					nav_id_ok = 0
					if kk == 0 then
						if ll == 0 then
							if nav_id == earth_nav[idx_rec][4] then
								nav_id_ok = 1
							end
						else
							if nav_id == earth_nav[idx_rec][4] and nav_reg_code == earth_nav[idx_rec][8] then
								nav_id_ok = 1
							end
						end
					else
						if ll == 0 then
							if nav_id == earth_nav[idx_rec][4] and nav_icao == earth_nav[idx_rec][5] then
								nav_id_ok = 1
							end
						else
							if nav_id == earth_nav[idx_rec][4] and nav_icao == earth_nav[idx_rec][5] and nav_reg_code == earth_nav[idx_rec][8] then
								nav_id_ok = 1
							end
						end
					end
					if nav_id_ok == 1 then
						navaid_list_n = navaid_list_n + 1
						navaid_list[navaid_list_n] = {}
						for jj = 1, 8 do
							navaid_list[navaid_list_n][jj] = earth_nav[idx_rec][jj]
						end
						navaid_list[navaid_list_n][20] = ">"
						navaid_list[navaid_list_n][21] = ""
						navaid_list[navaid_list_n][22] = ""
						navaid_list[navaid_list_n][23] = ""
						navaid_list[navaid_list_n][24] = 0
					end
				end
			end
		end
		
	end
	
	if kk == 0 then 
		if apt_data_num > 0 and string.len(nav_id) > 0 then
			nd_x = string.byte(string.sub(nav_id, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_apt[nd_x][99999] > 0 then
					for ii = 1, idx_apt[nd_x][99999] do
						idx_rec = idx_apt[nd_x][ii]
						
						if nav_id == apt_data[idx_rec][1] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							navaid_list[navaid_list_n][1] = 9	-- APT
							navaid_list[navaid_list_n][2] = apt_data[idx_rec][2]
							navaid_list[navaid_list_n][3] = apt_data[idx_rec][3]
							navaid_list[navaid_list_n][4] = apt_data[idx_rec][1]
							navaid_list[navaid_list_n][5] = ""
							navaid_list[navaid_list_n][6] = ""
							navaid_list[navaid_list_n][7] = ""
							navaid_list[navaid_list_n][8] = ""
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				end
			end
			
		end
	end
	
	-- custom wpt
	if wptx_n > 0 and string.len(nav_id) > 0 then
		for ii = 1, wptx_n do
			if nav_id == wptx[ii][1] then
				navaid_list_n = navaid_list_n + 1
				navaid_list[navaid_list_n] = {}
				navaid_list[navaid_list_n][1] = 4	-- WPT
				navaid_list[navaid_list_n][2] = wptx[ii][2]
				navaid_list[navaid_list_n][3] = wptx[ii][3]
				navaid_list[navaid_list_n][4] = wptx[ii][1]
				navaid_list[navaid_list_n][5] = ""
				navaid_list[navaid_list_n][6] = ""
				navaid_list[navaid_list_n][7] = ""
				navaid_list[navaid_list_n][8] = ""
				navaid_list[navaid_list_n][20] = wptx[ii][8]
				navaid_list[navaid_list_n][21] = wptx[ii][4]
				navaid_list[navaid_list_n][22] = wptx[ii][6]
				navaid_list[navaid_list_n][23] = wptx[ii][7]
				navaid_list[navaid_list_n][24] = ii
			end
		end
	end
	
	-- custom wpt internal FMS memory
	if custom_wpt_num > 0 and string.len(nav_id) > 0 then
		for ii = 1, custom_wpt_num do
			if nav_id == custom_wpt[ii][1] then
				navaid_list_n = navaid_list_n + 1
				navaid_list[navaid_list_n] = {}
				navaid_list[navaid_list_n][1] = 4	-- WPT
				navaid_list[navaid_list_n][2] = custom_wpt[ii][2]
				navaid_list[navaid_list_n][3] = custom_wpt[ii][3]
				navaid_list[navaid_list_n][4] = custom_wpt[ii][1]
				navaid_list[navaid_list_n][5] = ""
				navaid_list[navaid_list_n][6] = ""
				navaid_list[navaid_list_n][7] = ""
				navaid_list[navaid_list_n][8] = ""
				navaid_list[navaid_list_n][20] = ">"
				navaid_list[navaid_list_n][21] = custom_wpt[ii][4]
				navaid_list[navaid_list_n][22] = custom_wpt[ii][5]
				navaid_list[navaid_list_n][23] = custom_wpt[ii][6]
				navaid_list[navaid_list_n][24] = 0
			end
		end
	end
	
	-- find legs RW wpts
	if legs_num > 1 and offset > 0 and string.len(nav_id) > 3 then
		if string.sub(nav_id, 1, 2) == "RW" then
			if offset <= legs_num then
				for zzz = offset, legs_num do
					if nav_id == legs_data[zzz][1] then
						navaid_list_n = navaid_list_n + 1
						navaid_list[navaid_list_n] = {}
						navaid_list[navaid_list_n][1] = 4	-- WPT
						navaid_list[navaid_list_n][2] = legs_data[zzz][7]
						navaid_list[navaid_list_n][3] = legs_data[zzz][8]
						navaid_list[navaid_list_n][4] = legs_data[zzz][1]
						navaid_list[navaid_list_n][5] = ""
						navaid_list[navaid_list_n][6] = ""
						navaid_list[navaid_list_n][7] = ""
						navaid_list[navaid_list_n][8] = ""
						navaid_list[navaid_list_n][20] = ">"
						navaid_list[navaid_list_n][21] = ""
						navaid_list[navaid_list_n][22] = ""
						navaid_list[navaid_list_n][23] = ""
						navaid_list[navaid_list_n][24] = 0
						break
					end
				end
			end
		end
	end
	
	-- calc distance
	if navaid_list_n > 1 then
		
		if nav_mode0 == 0 then
			nd_lat = simDR_latitude
			nd_lon = simDR_longitude
		elseif nav_mode0 == 1 then
			if in_rte == 0 then
				nd_lat = ref_icao_lat
				nd_lon = ref_icao_lon
			else
				nd_lat = ref_icao_lat7
				nd_lon = ref_icao_lon7
			end
		elseif nav_mode0 == 2 then
			if in_rte == 0 then
				nd_lat = des_icao_lat
				nd_lon = des_icao_lon
			else
				nd_lat = des_icao_lat7
				nd_lon = des_icao_lon7
			end
		elseif nav_mode0 == 3 then
			nd_lat = find_lat
			nd_lon = find_lon
		elseif nav_mode0 == 4 then
			nd_lat = simDR_latitude
			nd_lon = simDR_longitude
		end
		
		for ii = 1, navaid_list_n do
			nd_dis = nd_calc_dist2(nd_lat, nd_lon,  navaid_list[ii][2], navaid_list[ii][3])
			navaid_list[ii][9] = nd_dis
		end
	end
	
	-- sort by nearest
	if navaid_list_n > 1 then
		for ii = 1, navaid_list_n - 1 do
			nd_x = ii + 1
			for jj = nd_x, navaid_list_n do
				if navaid_list[ii][9] > navaid_list[jj][9] then
					temp_nav_sort = {}
					for mm = 1, 9 do
						temp_nav_sort[mm] = navaid_list[ii][mm]
						navaid_list[ii][mm] = navaid_list[jj][mm]
						navaid_list[jj][mm] = temp_nav_sort[mm]
					end
					temp_nav_sort[20] = navaid_list[ii][20]
					temp_nav_sort[21] = navaid_list[ii][21]
					temp_nav_sort[22] = navaid_list[ii][22]
					temp_nav_sort[23] = navaid_list[ii][23]
					temp_nav_sort[24] = navaid_list[ii][24]
					navaid_list[ii][20] = navaid_list[jj][20]
					navaid_list[ii][21] = navaid_list[jj][21]
					navaid_list[ii][22] = navaid_list[jj][22]
					navaid_list[ii][23] = navaid_list[jj][23]
					navaid_list[ii][24] = navaid_list[jj][24]
					navaid_list[jj][20] = temp_nav_sort[20]
					navaid_list[jj][21] = temp_nav_sort[21]
					navaid_list[jj][22] = temp_nav_sort[22]
					navaid_list[jj][23] = temp_nav_sort[23]
					navaid_list[jj][24] = temp_nav_sort[24]
				end
			end
		end
	end
	
	-- if navaid_list_n == 0 then
	if navaid_list_n == 0 or nav_mode0 == 4 then
		if ils_nav_num > 0 and string.len(nav_id) > 0 then
			for ii = 1, ils_nav_num do
				if ll == 0 then
					if kk == 0 then
						if nav_id == ils_nav[ii][1] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == ils_nav[ii][1] and nav_icao == ils_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				else
					if kk == 0 then
						if nav_id == ils_nav[ii][1] and nav_reg_code == ils_nav[ii][8] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == ils_nav[ii][1] and nav_reg_code == ils_nav[ii][8] and nav_icao == ils_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				end
			end
		end
		if gls_nav_num > 0 and string.len(nav_id) > 0 then
			for ii = 1, gls_nav_num do
				if ll == 0 then
					if kk == 0 then
						if nav_id == gls_nav[ii][1] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == gls_nav[ii][1] and nav_icao == gls_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				else
					if kk == 0 then
						if nav_id == gls_nav[ii][1] and nav_reg_code == gls_nav[ii][8] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == gls_nav[ii][1] and nav_reg_code == gls_nav[ii][8] and nav_icao == gls_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				end
			end
		end
	end
	-- B738DR_freeze = 4

end

-- nav mode => distance from 0-PPOS, 1-REF ICAO, 2-DES ICAO, 3-By lat,lon (find_lat, find_lon)
-- nav_ndb = 0 - no, 1 - yes
function find_navaid2(nav_id, nav_icao, nav_mode0, nav_reg_code, nav_ndb, in_rte)
	
	-- B738DR_freeze = 1
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nav_id_ok = 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	
	local idx_rec = 0
	
	if string.len(nav_icao) > 0 then
		kk = 1
	end
	if string.len(nav_reg_code) > 0 then
		ll = 1
	end
	
	navaid_list = {}
	navaid_list_n = 0
	
	if earth_nav_num > 0 and string.len(nav_id) > 0 then
		nd_x = string.byte(string.sub(nav_id, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_earth_nav[nd_x][99999] > 0 then
				for ii = 1, idx_earth_nav[nd_x][99999] do
					idx_rec = idx_earth_nav[nd_x][ii]
					
					nav_id_ok = 0
					if kk == 0 then
						if ll == 0 then
							if nav_id == earth_nav[idx_rec][4] then
								nav_id_ok = 1
							end
						else
							if nav_id == earth_nav[idx_rec][4] and nav_reg_code == earth_nav[idx_rec][8] then
								nav_id_ok = 1
							end
						end
					else
						if ll == 0 then
							if nav_id == earth_nav[idx_rec][4] and nav_icao == earth_nav[idx_rec][5] then
								nav_id_ok = 1
							end
						else
							if nav_id == earth_nav[idx_rec][4] and nav_icao == earth_nav[idx_rec][5] and nav_reg_code == earth_nav[idx_rec][8] then
								nav_id_ok = 1
							end
						end
					end
					if nav_id_ok == 1 and nav_ndb > 0 then
						if nav_ndb == 1 then
							if earth_nav[idx_rec][1] ~= 3 then
								nav_id_ok = 0
							end
						else
							if earth_nav[idx_rec][1] == 3 then
								nav_id_ok = 0
							end
						end
					end
					if nav_id_ok == 1 then
						navaid_list_n = navaid_list_n + 1
						navaid_list[navaid_list_n] = {}
						for jj = 1, 8 do
							navaid_list[navaid_list_n][jj] = earth_nav[idx_rec][jj]
						end
						navaid_list[navaid_list_n][20] = ">"
						navaid_list[navaid_list_n][21] = ""
						navaid_list[navaid_list_n][22] = ""
						navaid_list[navaid_list_n][23] = ""
						navaid_list[navaid_list_n][24] = 0
					end
					
				end
			end
			
		end
		
	end
	
	
	if kk == 0 then 
		if apt_data_num > 0 and string.len(nav_id) > 0 then
			nd_x = string.byte(string.sub(nav_id, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_apt[nd_x][99999] > 0 then
					for ii = 1, idx_apt[nd_x][99999] do
						idx_rec = idx_apt[nd_x][ii]
					
						if nav_id == apt_data[idx_rec][1] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							navaid_list[navaid_list_n][1] = 9	-- APT
							navaid_list[navaid_list_n][2] = apt_data[idx_rec][2]
							navaid_list[navaid_list_n][3] = apt_data[idx_rec][3]
							navaid_list[navaid_list_n][4] = apt_data[idx_rec][1]
							navaid_list[navaid_list_n][5] = ""
							navaid_list[navaid_list_n][6] = ""
							navaid_list[navaid_list_n][7] = ""
							navaid_list[navaid_list_n][8] = ""
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				end
			end
		end
	end
	
	-- custom wpt
	if wptx_n > 0 and string.len(nav_id) > 0 then
		for ii = 1, wptx_n do
			if nav_id == wptx[ii][1] then
				navaid_list_n = navaid_list_n + 1
				navaid_list[navaid_list_n] = {}
				navaid_list[navaid_list_n][1] = 4	-- WPT
				navaid_list[navaid_list_n][2] = wptx[ii][2]
				navaid_list[navaid_list_n][3] = wptx[ii][3]
				navaid_list[navaid_list_n][4] = wptx[ii][1]
				navaid_list[navaid_list_n][5] = ""
				navaid_list[navaid_list_n][6] = ""
				navaid_list[navaid_list_n][7] = ""
				navaid_list[navaid_list_n][8] = ""
				navaid_list[navaid_list_n][20] = wptx[ii][8]
				navaid_list[navaid_list_n][21] = wptx[ii][4]
				navaid_list[navaid_list_n][22] = wptx[ii][6]
				navaid_list[navaid_list_n][23] = wptx[ii][7]
				navaid_list[navaid_list_n][24] = ii
			end
		end
	end
	
	-- custom wpt internal FMS memory
	if custom_wpt_num > 0 and string.len(nav_id) > 0 then
		for ii = 1, custom_wpt_num do
			if nav_id == custom_wpt[ii][1] then
				navaid_list_n = navaid_list_n + 1
				navaid_list[navaid_list_n] = {}
				navaid_list[navaid_list_n][1] = 4	-- WPT
				navaid_list[navaid_list_n][2] = custom_wpt[ii][2]
				navaid_list[navaid_list_n][3] = custom_wpt[ii][3]
				navaid_list[navaid_list_n][4] = custom_wpt[ii][1]
				navaid_list[navaid_list_n][5] = ""
				navaid_list[navaid_list_n][6] = ""
				navaid_list[navaid_list_n][7] = ""
				navaid_list[navaid_list_n][8] = ""
				navaid_list[navaid_list_n][20] = ">"
				navaid_list[navaid_list_n][21] = custom_wpt[ii][4]
				navaid_list[navaid_list_n][22] = custom_wpt[ii][5]
				navaid_list[navaid_list_n][23] = custom_wpt[ii][6]
				navaid_list[navaid_list_n][24] = 0
			end
		end
	end
	
	-- find legs RW wpts
	if legs_num > 1 and offset > 0 and string.len(nav_id) > 3 then
		if string.sub(nav_id, 1, 2) == "RW" then
			if offset <= legs_num then
				for zzz = offset, legs_num do
					if nav_id == legs_data[zzz][1] then
						navaid_list_n = navaid_list_n + 1
						navaid_list[navaid_list_n] = {}
						navaid_list[navaid_list_n][1] = 4	-- WPT
						navaid_list[navaid_list_n][2] = legs_data[zzz][7]
						navaid_list[navaid_list_n][3] = legs_data[zzz][8]
						navaid_list[navaid_list_n][4] = legs_data[zzz][1]
						navaid_list[navaid_list_n][5] = ""
						navaid_list[navaid_list_n][6] = ""
						navaid_list[navaid_list_n][7] = ""
						navaid_list[navaid_list_n][8] = ""
						navaid_list[navaid_list_n][20] = ">"
						navaid_list[navaid_list_n][21] = ""
						navaid_list[navaid_list_n][22] = ""
						navaid_list[navaid_list_n][23] = ""
						navaid_list[navaid_list_n][24] = 0
						break
					end
				end
			end
		end
	end
	
	-- calc distance
	if navaid_list_n > 1 then
		
		if nav_mode0 == 0 then
			nd_lat = simDR_latitude
			nd_lon = simDR_longitude
		elseif nav_mode0 == 1 then
			if in_rte == 0 then
				nd_lat = ref_icao_lat
				nd_lon = ref_icao_lon
			else
				nd_lat = ref_icao_lat7
				nd_lon = ref_icao_lon7
			end
		elseif nav_mode0 == 2 then
			if in_rte == 0 then
				nd_lat = des_icao_lat
				nd_lon = des_icao_lon
			else
				nd_lat = des_icao_lat7
				nd_lon = des_icao_lon7
			end
		elseif nav_mode0 == 3 then
			nd_lat = find_lat
			nd_lon = find_lon
		elseif nav_mode0 == 4 then
			nd_lat = simDR_latitude
			nd_lon = simDR_longitude
		elseif nav_mode0 == 11 then
			if in_rte == 0 then
				nd_lat = ref_icao_lat_x
				nd_lon = ref_icao_lon_x
			else
				nd_lat = ref_icao_lat7_x
				nd_lon = ref_icao_lon7_x
			end
		elseif nav_mode0 == 12 then
			if in_rte == 0 then
				nd_lat = des_icao_lat_x
				nd_lon = des_icao_lon_x
			else
				nd_lat = des_icao_lat7_x
				nd_lon = des_icao_lon7_x
			end
		end
		
		for ii = 1, navaid_list_n do
			nd_dis = nd_calc_dist2(nd_lat, nd_lon,  navaid_list[ii][2], navaid_list[ii][3])
			navaid_list[ii][9] = nd_dis
		end
	end
	
	-- sort by nearest
	if navaid_list_n > 1 then
		for ii = 1, navaid_list_n - 1 do
			nd_x = ii + 1
			for jj = nd_x, navaid_list_n do
				if navaid_list[ii][9] > navaid_list[jj][9] then
					temp_nav_sort = {}
					for mm = 1, 9 do
						temp_nav_sort[mm] = navaid_list[ii][mm]
						navaid_list[ii][mm] = navaid_list[jj][mm]
						navaid_list[jj][mm] = temp_nav_sort[mm]
					end
					temp_nav_sort[20] = navaid_list[ii][20]
					temp_nav_sort[21] = navaid_list[ii][21]
					temp_nav_sort[22] = navaid_list[ii][22]
					temp_nav_sort[23] = navaid_list[ii][23]
					temp_nav_sort[24] = navaid_list[ii][24]
					navaid_list[ii][20] = navaid_list[jj][20]
					navaid_list[ii][21] = navaid_list[jj][21]
					navaid_list[ii][22] = navaid_list[jj][22]
					navaid_list[ii][23] = navaid_list[jj][23]
					navaid_list[ii][24] = navaid_list[jj][24]
					navaid_list[jj][20] = temp_nav_sort[20]
					navaid_list[jj][21] = temp_nav_sort[21]
					navaid_list[jj][22] = temp_nav_sort[22]
					navaid_list[jj][23] = temp_nav_sort[23]
					navaid_list[jj][24] = temp_nav_sort[24]
				end
			end
		end
	end
	
	-- if navaid_list_n == 0 then
	if navaid_list_n == 0 or nav_mode0 == 4 then
		if ils_nav_num > 0 and string.len(nav_id) > 0 then
			for ii = 1, ils_nav_num do
				if ll == 0 then
					if kk == 0 then
						if nav_id == ils_nav[ii][1] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == ils_nav[ii][1] and nav_icao == ils_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				else
					if kk == 0 then
						if nav_id == ils_nav[ii][1] and nav_reg_code == ils_nav[ii][8] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == ils_nav[ii][1] and nav_reg_code == ils_nav[ii][8] and nav_icao == ils_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if ils_nav[ii][9] == 0 then
								navaid_list[navaid_list_n][1] = 8	-- ILS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " ILS " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 1 then
								navaid_list[navaid_list_n][1] = 7	-- LOC
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC " .. ils_nav[ii][4]
							elseif ils_nav[ii][9] == 2 then
								navaid_list[navaid_list_n][1] = 6	-- LOC-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LOC-GS " .. ils_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 10	-- LDA-GS
								navaid_list[navaid_list_n][7] = ils_nav[ii][7] .. " LDA-GS " .. ils_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = ils_nav[ii][2]
							navaid_list[navaid_list_n][3] = ils_nav[ii][3]
							navaid_list[navaid_list_n][4] = ils_nav[ii][1]
							navaid_list[navaid_list_n][5] = ils_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(ils_nav[ii][6])
							navaid_list[navaid_list_n][8] = ils_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				end
			end
		end
		if gls_nav_num > 0 and string.len(nav_id) > 0 then
			for ii = 1, gls_nav_num do
				if ll == 0 then
					if kk == 0 then
						if nav_id == gls_nav[ii][1] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == gls_nav[ii][1] and nav_icao == gls_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				else
					if kk == 0 then
						if nav_id == gls_nav[ii][1] and nav_reg_code == gls_nav[ii][8] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					else
						if nav_id == gls_nav[ii][1] and nav_reg_code == gls_nav[ii][8] and nav_icao == gls_nav[ii][7] then
							navaid_list_n = navaid_list_n + 1
							navaid_list[navaid_list_n] = {}
							if gls_nav[ii][11] == 0 then
								navaid_list[navaid_list_n][1] = 11	-- GLS
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " GLS " .. gls_nav[ii][4]
							else
								navaid_list[navaid_list_n][1] = 12	-- LPV
								navaid_list[navaid_list_n][7] = gls_nav[ii][7] .. " LPV " .. gls_nav[ii][4]
							end
							navaid_list[navaid_list_n][2] = gls_nav[ii][2]
							navaid_list[navaid_list_n][3] = gls_nav[ii][3]
							navaid_list[navaid_list_n][4] = gls_nav[ii][1]
							navaid_list[navaid_list_n][5] = gls_nav[ii][7]
							navaid_list[navaid_list_n][6] = tostring(gls_nav[ii][6])
							navaid_list[navaid_list_n][8] = gls_nav[ii][8]
							navaid_list[navaid_list_n][20] = ">"
							navaid_list[navaid_list_n][21] = ""
							navaid_list[navaid_list_n][22] = ""
							navaid_list[navaid_list_n][23] = ""
							navaid_list[navaid_list_n][24] = 0
						end
					end
				end
			end
		end
	end
	-- B738DR_freeze = 2

end



function rte_copyb(aaa)

	local tt1 = 0
	local tt2 = 0
	local tt3 = 0
	
	legs_data2_tmp_nb = 0
	legs_data2_tmpb = {}
	
	tt3 = legs_num2b + 1
	if aaa <= tt3 and aaa > 0 then
		for tt1 = aaa, tt3 do
			legs_data2_tmp_nb = legs_data2_tmp_nb + 1
			legs_data2_tmpb[legs_data2_tmp_nb] = {}
			for tt2 = 1, MAX_LEGS_DATA do
				legs_data2_tmpb[legs_data2_tmp_nb][tt2] = legs_data2b[tt1][tt2]
			end
		end
	end
	
	
end

function rte_pasteb(aaa)

	local tt1 = 0
	local tt2 = 0
	local cnt_leg = aaa - 1
	
	if legs_data2_tmp_nb > 0 then
	
		for tt1 = 1, legs_data2_tmp_nb do
			cnt_leg = cnt_leg + 1
			legs_data2b[cnt_leg] = {}
			for tt2 = 1, MAX_LEGS_DATA do
				legs_data2b[cnt_leg][tt2] = legs_data2_tmpb[tt1][tt2]
			end
		end
		legs_num2b = cnt_leg - 1
	end
	
end


function rte_del_specb(bbb, mode, in_offset)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local sid_leg_idx = 0
	
	rte_copy_idxb = 0
	
	if legs_num2b > 0 then
		if bbb == 1 then	-- SID
			kk = in_offset		--1
			for ii = legs_num2b, kk, -1 do
				if legs_data2b[ii][19] ~= nil then
					if ref_sid2 == "------" and legs_data2b[ii][19] == 3 then
						legs_data2b[ii][19] = 0 -- route
						legs_data2b[ii][9] = "DIRECT"
						legs_data2b[ii][31] = "TF"
						legs_data2b[ii][21] = -1
						legs_data2b[ii][32] = 0
					end
					if legs_data2b[ii][19] == bbb then
						sid_leg_idx = ii
						break
					end
				end
			end
			if sid_leg_idx ~= 0 then
				sid_leg_idx = sid_leg_idx + 1
				if sid_leg_idx <= legs_num2b then
					if legs_data2b[sid_leg_idx][1] == "DISCONTINUITY" then
						sid_leg_idx = sid_leg_idx + 1
					end
				end
				rte_copyb(sid_leg_idx)
				-- rte_pasteb(2)
				rte_pasteb(in_offset)
			end
		elseif bbb == 2 then	-- STAR
			kk = 1
			if mode == 1 then
				if in_offset > 0 then
					kk = in_offset + 1
					if kk > legs_num2b then
						kk = legs_num2b
					end
				end
			end
			for ii = kk, legs_num2b do
				if legs_data2b[ii][19] ~= nil then
					if des_star2 == "------" and legs_data2b[ii][19] == 4 then
						legs_data2b[ii][19] = 0 -- route
					end
					if des_app2 == "------" and legs_data2b[ii][19] == 8 then
						legs_data2b[ii][19] = 2 -- STAR
					end
					if legs_data2b[ii][19] == bbb then
						if sid_leg_idx == 0 then
							sid_leg_idx = ii
						end
						rte_copy_idxb = ii
					end
				end
			end
			if sid_leg_idx ~= 0 then
				rte_copyb(rte_copy_idxb + 1)
				-- find DISCONTINUITY
				jj = sid_leg_idx - 1
				if jj > 1 then
					if legs_data2b[jj][1] == "DISCONTINUITY" then
						sid_leg_idx = jj
					end
				end
				rte_pasteb(sid_leg_idx)
			end
		elseif bbb == 7 then	-- APP
			kk = 1
			if mode == 1 then
				if in_offset > 0 then
					kk = in_offset + 1
					if kk > legs_num2b then
						kk = legs_num2b
					end
				end
			end
			for ii = kk, legs_num2b do
				if legs_data2b[ii][19] ~= nil then
					if des_star2 == "------" and legs_data2b[ii][19] == 8 then
						legs_data2b[ii][19] = 7 -- APP
					end
					if legs_data2b[ii][19] == bbb then
						if sid_leg_idx == 0 then
							sid_leg_idx = ii
						end
						rte_copy_idxb = ii
					end
				end
			end
			if sid_leg_idx ~= 0 then
				rte_copyb(rte_copy_idxb + 1)
				-- find DISCONTINUITY
				jj = sid_leg_idx - 1
				if jj > 1 then
					if legs_data2b[jj][1] == "DISCONTINUITY" then
						sid_leg_idx = jj
					end
				end
				rte_pasteb(sid_leg_idx)
			end
		elseif bbb == 99 then	-- STAR, APP
			kk = 1
			if mode == 1 then
				if in_offset > 0 then
					kk = in_offset + 1
					if kk > legs_num2b then
						kk = legs_num2b
					end
				end
			end
			for ii = kk, legs_num2b do
				if legs_data2b[ii][19] ~= nil then
					
					if legs_data2b[ii][19] == 4 then	-- before RTE
						legs_data2b[ii][19] = 0 -- route
					end
					if legs_data2b[ii][19] == 9 then	-- before RTE
						legs_data2b[ii][19] = 0 -- route
					end
					
					if legs_data2b[ii][19] == 2 or legs_data2b[ii][19] == 7 or legs_data2b[ii][19] == 8 then
						if sid_leg_idx == 0 then
							sid_leg_idx = ii
						end
						rte_copy_idxb = ii
					end
				end
			end
			if sid_leg_idx ~= 0 then
				rte_copyb(rte_copy_idxb + 1)
				-- find DISCONTINUITY
				jj = sid_leg_idx - 1
				if jj > 1 then
					if legs_data2b[jj][1] == "DISCONTINUITY" then
						sid_leg_idx = jj
					end
				end
				rte_pasteb(sid_leg_idx)
			end
		end
	end

end


function copy_to_legsdata(in_rte)

	local ii = 0
	local jj = 0
	local kk = 0
	local fms_line = ""
	local fms_pth = ""
	local radius_num = 0
	
	if in_rte == 0 then
		legs_num = 0
		legs_data = {}
		
		B738_num_of_wpts = 0
		
		if legs_num2 > 0 then
			for ii = 1, legs_num2 + 1 do
				legs_data2[ii][25] = ii	--waypoint idx before
				legs_data[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data[ii][jj] = legs_data2[ii][jj]
				end
			end
			legs_num = legs_num2
		end
		calc_rte_enable = 1
		-- update_offset_idx()
		offset_check_act()
	elseif in_rte == 1 then
		legs_num = 0
		legs_data = {}
		
		B738_num_of_wpts = 0
		
		if legs_num8 > 0 then
			for ii = 1, legs_num8 + 1 do
				legs_data8[ii][25] = ii	--waypoint idx before
				legs_data[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data[ii][jj] = legs_data8[ii][jj]
				end
			end
			legs_num = legs_num8
		end
		calc_rte_enable = 1
		-- update_offset_idx()
		offset_check_act()
	end
end

function copy_to_legs_data2_buf(in_rte)
	legs_num2_buf = 0
	legs_data2_buf = {}
	
	if in_rte == 0 then
		
		if legs_num2 > 0 then
			for ii = 1, legs_num2 + 1 do
				legs_data2_buf[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data2_buf[ii][jj] = legs_data2[ii][jj]
				end
			end
			legs_num2_buf = legs_num2
		end
	elseif in_rte == 1 then
		
		if legs_num8 > 0 then
			for ii = 1, legs_num8 + 1 do
				legs_data2_buf[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data2_buf[ii][jj] = legs_data8[ii][jj]
				end
			end
			legs_num2_buf = legs_num8
		end
	end
	copy_to_idx_mod_buf(in_rte)
end

function copy_from_legs_data2_buf(in_rte)
	if in_rte == 0 then
		legs_num2 = 0
		legs_data2 = {}
		
		if legs_num2_buf > 0 then
			for ii = 1, legs_num2_buf + 1 do
				legs_data2[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data2[ii][jj] = legs_data2_buf[ii][jj]
				end
			end
			legs_num2 = legs_num2_buf
		end
	elseif in_rte == 1 then
		legs_num8 = 0
		legs_data8 = {}
		
		if legs_num2_buf > 0 then
			for ii = 1, legs_num2_buf + 1 do
				legs_data8[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data8[ii][jj] = legs_data2_buf[ii][jj]
				end
			end
			legs_num8 = legs_num2_buf
		end
	end
	copy_from_idx_mod_buf(in_rte)
end

function copy_to_idx_mod_buf(in_rte)
	if in_rte == 0 then
		last_sid_idx_mod_buf = last_sid_idx_mod
		first_sid_idx_mod_buf = first_sid_idx_mod
		first_app_idx_mod_buf = first_app_idx_mod
		first_star_idx_mod_buf = first_star_idx_mod
		
		-- iaf_app_idx_mod = iaf_app_idx_mod
		-- id_ed_mod = id_ed_mod
		-- idx_ed_mod = idx_ed_mod
		-- idx_iaf_mod = idx_iaf_mod
		-- rnav_idx_first_mod = rnav_idx_first_mod
		-- rnav_idx_last_mod = rnav_idx_last_mod
		
		
		idx_ed_mod_buf = idx_ed_mod
		rnav_alt_mod_buf = rnav_alt_mod
		rnav_vpa_mod_buf = rnav_vpa_mod
		rnav_idx_first_mod_buf = rnav_idx_first_mod
		rnav_idx_last_mod_buf = rnav_idx_last_mod
		
		-- rnav_idx_first_mod = 0
		-- rnav_idx_last_mod = 0
		-- first_miss_app_idx_mod = 0
		-- last_miss_app_idx_mod = 0
		-- rnav_alt_mod = 0
		-- rnav_vpa_mod = 0
		-- alt_ed_mod = 0
		-- alt_type_ed_mod = 0
		-- idx_iaf_mod = 0
		
	elseif in_rte == 1 then
		last_sid_idx_mod_buf = last_sid_idx_mod2
		first_sid_idx_mod_buf = first_sid_idx_mod2
		first_app_idx_mod_buf = first_app_idx_mod2
		first_star_idx_mod_buf = first_star_idx_mod2
		
		idx_ed_mod_buf = idx_ed_mod2
		rnav_alt_mod_buf = rnav_alt_mod2
		rnav_vpa_mod_buf = rnav_vpa_mod2
		rnav_idx_first_mod_buf = rnav_idx_first_mod2
		rnav_idx_last_mod_buf = rnav_idx_last_mod2
		
		-- iaf_app_idx_mod = iaf_app_idx_mod
		-- id_ed_mod = id_ed_mod
		-- idx_ed_mod = idx_ed_mod
		-- idx_iaf_mod = idx_iaf_mod
		-- rnav_idx_first_mod = rnav_idx_first_mod
		-- rnav_idx_last_mod = rnav_idx_last_mod
	else
		last_sid_idx_mod_buf = 0
		first_sid_idx_mod_buf = 0
		first_app_idx_mod_buf = 0
		first_star_idx_mod_buf = 0
		
		idx_ed_mod_buf = 0
		rnav_alt_mod_buf = 0
		rnav_vpa_mod_buf = 0
		rnav_idx_first_mod_buf = 0
		rnav_idx_last_mod_buf = 0
		
		-- iaf_app_idx_mod = 0
		-- id_ed_mod = ""
		-- idx_ed_mod = 0
		-- idx_iaf_mod = 0
		-- rnav_idx_first_mod = 0
		-- rnav_idx_last_mod = 0
	end
end

function copy_from_idx_mod_buf(in_rte)
	if in_rte == 0 then
		last_sid_idx_mod = last_sid_idx_mod_buf
		first_sid_idx_mod = first_sid_idx_mod_buf
		first_app_idx_mod = first_app_idx_mod_buf
		first_star_idx_mod = first_star_idx_mod_buf
		
		idx_ed_mod = idx_ed_mod_buf
		rnav_alt_mod = rnav_alt_mod_buf
		rnav_vpa_mod = rnav_vpa_mod_buf
		rnav_idx_first_mod = rnav_idx_first_mod_buf
		rnav_idx_last_mod = rnav_idx_last_mod_buf
		
		-- iaf_app_idx_mod = iaf_app_idx_mod
		-- id_ed_mod = id_ed_mod
		-- idx_ed_mod = idx_ed_mod
		-- idx_iaf_mod = idx_iaf_mod
		-- rnav_idx_first_mod = rnav_idx_first_mod
		-- rnav_idx_last_mod = rnav_idx_last_mod
	elseif in_rte == 1 then
		last_sid_idx_mod2 = last_sid_idx_mod_buf
		first_sid_idx_mod2 = first_sid_idx_mod_buf
		first_app_idx_mod2 = first_app_idx_mod_buf
		first_star_idx_mod2 = first_star_idx_mod_buf
		
		idx_ed_mod2 = idx_ed_mod_buf
		rnav_alt_mod2 = rnav_alt_mod_buf
		rnav_vpa_mod2 = rnav_vpa_mod_buf
		rnav_idx_first_mod2 = rnav_idx_first_mod_buf
		rnav_idx_last_mod2 = rnav_idx_last_mod_buf
		
		-- iaf_app_idx_mod = iaf_app_idx_mod
		-- id_ed_mod = id_ed_mod
		-- idx_ed_mod = idx_ed_mod
		-- idx_iaf_mod = idx_iaf_mod
		-- rnav_idx_first_mod = rnav_idx_first_mod
		-- rnav_idx_last_mod = rnav_idx_last_mod
	end
end

function copy_to_fpln_buf(in_rte)
	fpln_num_buf = 0
	fpln_data_buf = {}
	
	if in_rte == 0 then
		if fpln_num2 > 0 then
			for ii = 1, fpln_num2 do
				fpln_data_buf[ii] = {}
				for jj = 1, 6 do
					fpln_data_buf[ii][jj] = fpln_data2[ii][jj]
				end
			end
			fpln_num_buf = fpln_num2
		end
	elseif in_rte == 1 then
		if fpln_num4 > 0 then
			for ii = 1, fpln_num4 do
				fpln_data_buf[ii] = {}
				for jj = 1, 6 do
					fpln_data_buf[ii][jj] = fpln_data4[ii][jj]
				end
			end
			fpln_num_buf = fpln_num4
		end
	end
	
end

function copy_from_fpln_buf(in_rte)
	if in_rte == 0 then
		fpln_num2 = 0
		fpln_data2 = {}
		
		if fpln_num_buf > 0 then
			for ii = 1, fpln_num_buf do
				fpln_data2[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					fpln_data2[ii][jj] = fpln_data_buf[ii][jj]
				end
			end
			fpln_num2 = fpln_num_buf
		end
	elseif in_rte == 1 then
		fpln_num4 = 0
		fpln_data4 = {}
		
		if fpln_num_buf > 0 then
			for ii = 1, fpln_num_buf do
				fpln_data4[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					fpln_data4[ii][jj] = fpln_data_buf[ii][jj]
				end
			end
			fpln_num4 = fpln_num_buf
		end
	end
	
end

function copy_to_hold_data2_buf(in_rte)
	hold_data_num_buf = 0
	hold_data_buf = {}
	
	if in_rte == 0 then
		if hold_data_num2 > 0 then
			for ii = 1, hold_data_num2 do
				hold_data_buf[ii] = hold_data2[ii]
			end
			hold_data_num_buf = hold_data_num2
		end
	elseif in_rte == 1 then
		if hold_data_num4 > 0 then
			for ii = 1, hold_data_num4 do
				hold_data_buf[ii] = hold_data4[ii]
			end
			hold_data_num_buf = hold_data_num4
		end
	end
	
end

function copy_from_hold_data2_buf(in_rte)
	if in_rte == 0 then
		hold_data_num2 = 0
		hold_data2 = {}
		
		if hold_data_num_buf > 0 then
			for ii = 1, hold_data_num_buf do
				hold_data2[ii] = hold_data_buf[ii]
			end
			hold_data_num2 = hold_data_num_buf
		end
	elseif in_rte == 1 then
		hold_data_num4 = 0
		hold_data4 = {}
		
		if hold_data_num_buf > 0 then
			for ii = 1, hold_data_num_buf do
				hold_data4[ii] = hold_data_buf[ii]
			end
			hold_data_num4 = hold_data_num_buf
		end
	end
	
end

function B738_activate()
	if exec_activate_fpln ~= 0 and legs_num2 <= 1 then
		exec_activate_fpln = 0
	end
	if exec_activate_fpln2 ~= 0 and legs_num8 <= 1 then
		exec_activate_fpln2 = 0
	end
	
	if in_flight_mode ~= 0 then
		if legs_intdir == 0 and (exec_activate_fpln ~= 0 or exec_activate_fpln2 ~= 0) then
			local disable_direct = false
			if exec_activate_fpln ~= 0 then
				if offset_mod > 0 and offset_mod <= legs_num2 then
					if legs_data2[offset_mod][1] == "DISCONTINUITY" then
						disable_direct = true
					end
				end
			else
				if offset_mod2 > 0 and offset_mod2 <= legs_num8 then
					if legs_data8[offset_mod2][1] == "DISCONTINUITY" then
						disable_direct = true
					end
				end
			end
			if disable_direct == false then
				legs_intdir = 1
				-- compute >legs_intdir_crs
				local nd_lat = math.rad(simDR_latitude)
				local nd_lon = math.rad(simDR_longitude)
				
				local nd_lat2 = 0
				local nd_lon2 = 0
				if exec_activate_fpln ~= 0 then
					legs_intdir_rte = 0
					legs_intdir_idx_mod = offset_mod
					legs_data2[legs_intdir_idx_mod][31] = "DF"
					legs_data2[legs_intdir_idx_mod][9] = ""
					nd_lat2, nd_lon2 = lat_lon_legs2(legs_intdir_idx_mod)
				else
					legs_intdir_rte = 1
					legs_intdir_idx_mod = offset_mod2
					legs_data8[legs_intdir_idx_mod][31] = "DF"
					legs_data8[legs_intdir_idx_mod][9] = ""
					nd_lat2, nd_lon2 = lat_lon_legs8(legs_intdir_idx_mod)
				end
				nd_lat2 = math.rad(nd_lat2)
				nd_lon2 = math.rad(nd_lon2)
				
				local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
				local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
				local nd_hdg = math.atan2(nd_y, nd_x)
				nd_hdg = math.deg(nd_hdg)
				nd_hdg = (nd_hdg + 360) % 360
				legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
			end
		end
	end
end

function true_legs_num(in_rte)
	local result = -1
	
	if in_rte == 1 then
		if legs_num > 0 then
			if legs_data[legs_num+1][34] == "DEST" and legs_num == 1 then
				result = 0
			else
				result = legs_num
			end
		end
	elseif in_rte == 2 then
		if legs_num2 > 0 then
			if legs_data2[legs_num2+1][34] == "DEST" and legs_num2 == 1 then
				result = 0
			else
				result = legs_num2
			end
		end
	elseif in_rte == 8 then
		if legs_num8 > 0 then
			if legs_data8[legs_num8+1][34] == "DEST" and legs_num8 == 1 then
				result = 0
			else
				result = legs_num8
			end
		end
	end
	
	return result
end

function legs_fill()
	local ii = 0
	local jj = 0
	local kk = 0
	local fms_line = ""
	local fms_pth = ""
	local fms_pth_before = ""
	local radius_num = 0
	local num_of_wpts = 0
	
	local fms_line2 = ""
	
	local x_temp = 0
	local y_temp = 0
	local legs_num_tmp = true_legs_num(1)
	
	local maxx = legs_num + 1
	if legs_num_tmp == 0 then
		maxx = legs_num
	end
	
	if legs_num_tmp >= 0 then
		
		for ii = 1, maxx do
			if ii < FMC_MAX_WPT then
				
				B738DR_fms_test1 = 7000
				
				num_of_wpts = num_of_wpts + 1
				-- legs data + lat,lon
				if ii == 1 then
					fms_line = legs_data[1][1]
				elseif ii <= 128 then
					fms_line = fms_line .. " " .. legs_data[ii][1]
				elseif ii == 129 then
					fms_line2 = legs_data[ii][1]
				elseif ii > 129 then
					fms_line2 = fms_line2 .. " " .. legs_data[ii][1]
				end
				B738DR_fms_test1 = 7001
				
				B738DR_fms_legs_lat[ii-1] = legs_data[ii][7]
				
				B738DR_fms_test1 = 70011
				
				B738DR_fms_legs_lon[ii-1] = legs_data[ii][8]
				
				B738DR_fms_test1 = 70012
				
				B738DR_fms_legs_dist[ii-1] = legs_data[ii][3]
				
				B738DR_fms_test1 = 70013
				
				B738DR_fms_legs_brg_true[ii-1] = legs_data[ii][2]
				
				B738DR_fms_test1 = 70014
				
				B738DR_fms_legs_brg_mag[ii-1] = legs_data[ii][18]
				
				B738DR_fms_test1 = 70015
				
				B738DR_fms_legs_brg_fin_true[ii-1] = legs_data[ii][37]
				
				B738DR_fms_test1 = 70016
				
				B738DR_fms_legs_rad_lat[ii-1] = legs_data[ii][23]
				
				B738DR_fms_test1 = 70017
				
				B738DR_fms_legs_rad_lon[ii-1] = legs_data[ii][24]
				
				B738DR_fms_test1 = 70018
				
				B738DR_fms_legs_rad_turn[ii-1] = legs_data[ii][21]
				
				B738DR_fms_test1 = 70019
				
				B738DR_fms_legs_spd[ii-1] = legs_data[ii][4]
				
				B738DR_fms_test1 = 70020
				
				B738DR_fms_legs_spd_type[ii-1] = legs_data[ii][42]
				
				B738DR_fms_test1 = 70021
				
				B738DR_fms_legs_eta[ii-1] = legs_data[ii][13]
				
				B738DR_fms_test1 = 7002
				
				radius_num = tonumber(string.sub(legs_data[ii][35], 1, 2))
				if radius_num == nil then
					if ii >= first_app_idx and first_app_idx ~= 0 then
						-- approach
						if string.sub(des_app, 1, 1) == "R" then
							B738DR_fms_legs_rnp[ii-1] = 0.3
						else
							B738DR_fms_legs_rnp[ii-1] = 1
						end
					elseif ii >= first_star_idx and first_star_idx ~= 0 then
						-- arrival
						B738DR_fms_legs_rnp[ii-1] = 1
					elseif ii <= last_sid_idx and last_sid_idx ~= 0 then
						-- departure
						B738DR_fms_legs_rnp[ii-1] = 1
					else
						B738DR_fms_legs_rnp[ii-1] = 2
					end
					B738DR_fms_legs_rnp_data[ii-1] = 0
				else
					if string.sub(legs_data[ii][35], 3, 3) == "1" then
						B738DR_fms_legs_rnp[ii-1] = radius_num / 10
						B738DR_fms_legs_rnp_data[ii-1] = B738DR_fms_legs_rnp[ii-1]
					elseif string.sub(legs_data[ii][35], 3, 3) == "2" then
						B738DR_fms_legs_rnp[ii-1] = radius_num / 100
						B738DR_fms_legs_rnp_data[ii-1] = B738DR_fms_legs_rnp[ii-1]
					elseif string.sub(legs_data[ii][35], 3, 3) == "3" then
						B738DR_fms_legs_rnp[ii-1] = radius_num / 1000
						B738DR_fms_legs_rnp_data[ii-1] = B738DR_fms_legs_rnp[ii-1]
					else
						B738DR_fms_legs_rnp[ii-1] = 2
						B738DR_fms_legs_rnp_data[ii-1] = 0
					end
				end
				
				B738DR_fms_test1 = 7003
				
				radius_num = tonumber(legs_data[ii][29])
				if radius_num == nil then
					radius_num = 0
				else
					radius_num = radius_num / 10
				end
				B738DR_fms_legs_crs_mag[ii-1] = radius_num
				
				x_temp = 0
				y_temp = 0
				radius_num = tonumber(legs_data[ii][30])
				if radius_num == nil then
					B738DR_fms_legs_hold_time[ii-1] = 0
					if string.len(legs_data[ii][30]) == 4 then
						if string.sub(legs_data[ii][30], 1, 1) == "T" then
							-- Time
							x_temp = tonumber(string.sub(legs_data[ii][30], 2, 3))
							y_temp = (tonumber(string.sub(legs_data[ii][30], 4, 4))) / 10
							x_temp = x_temp + y_temp
							B738DR_fms_legs_hold_time[ii-1] = x_temp * 60	-- in seconds
						end
					end
					B738DR_fms_legs_hold_dist[ii-1] = 0
				else
					B738DR_fms_legs_hold_time[ii-1] = 0
					B738DR_fms_legs_hold_dist[ii-1] = radius_num / 10
				end
				
				B738DR_fms_test1 = 7004
				
				if legs_data[ii][31] == "AF" then
					radius_num = tonumber(legs_data[ii][22])
					if radius_num == nil then
						B738DR_fms_legs_radius[ii-1] = 0
					else
						B738DR_fms_legs_radius[ii-1] = radius_num / 10
					end
				else
					-- RF
					radius_num = tonumber(legs_data[ii][33])
					if radius_num == nil then
						B738DR_fms_legs_radius[ii-1] = 0
					else
						B738DR_fms_legs_radius[ii-1] = radius_num / 1000
					end
				end
				if legs_data[ii][31] == "" then
					fms_pth = fms_pth .. "TF"
				else
					fms_pth = fms_pth .. legs_data[ii][31]
				end
				
				B738DR_fms_test1 = 7005
				
				if legs_data[ii][43] == "" then
					fms_pth_before = fms_pth_before .. "  "
				else
					fms_pth_before = fms_pth_before .. legs_data[ii][43]
				end
				B738DR_fms_legs_alt_rest1[ii-1] = legs_data[ii][5]
				if string.sub(legs_data[ii][1], 1, 2) == "RW" then
					B738DR_fms_legs_alt_rest2[ii-1] = 0
					if legs_data[ii][19] == 7 or legs_data[ii][19] == 8 
					or legs_data[ii][19] == 9 then
						-- destination runway
						B738DR_fms_legs_alt_rest_type[ii-1] = 4 -- destination runway
					else
						B738DR_fms_legs_alt_rest_type[ii-1] = 0
					end
				elseif legs_data[ii][6] == 45 then	-- Below
					B738DR_fms_legs_alt_rest_type[ii-1] = 1
					B738DR_fms_legs_alt_rest2[ii-1] = 0
				elseif legs_data[ii][6] == 43 then	-- Above
					B738DR_fms_legs_alt_rest_type[ii-1] = 2
					B738DR_fms_legs_alt_rest2[ii-1] = 0
				elseif legs_data[ii][6] == 41 then	-- Between
					B738DR_fms_legs_alt_rest_type[ii-1] = 3
					B738DR_fms_legs_alt_rest2[ii-1] = legs_data[ii][41]
				else	-- At
					B738DR_fms_legs_alt_rest_type[ii-1] = 0
					B738DR_fms_legs_alt_rest2[ii-1] = 0
				end
				B738DR_fms_legs_alt_calc[ii-1] = legs_data[ii][11]
				B738DR_fms_legs_spd_calc[ii-1] = legs_data[ii][10]
				
				if legs_data[ii][31] == "CA" or legs_data[ii][31] == "VA" or legs_data[ii][31] == "FA" then
					B738DR_fms_legs_type[ii-1] = 6	-- Conditional wpt
				elseif legs_data[ii][31] == "CD" or legs_data[ii][31] == "VD" then
					B738DR_fms_legs_type[ii-1] = 6	-- Conditional wpt
				elseif legs_data[ii][31] == "CR" or legs_data[ii][31] == "VR" then
					B738DR_fms_legs_type[ii-1] = 6	-- Conditional wpt
				elseif fly_over_wpt(ii) then
					B738DR_fms_legs_type[ii-1] = 5	-- Fly over wpt
				else
					B738DR_fms_legs_type[ii-1] = 0
				end
				
				B738DR_fms_test1 = 7006
				
				B738DR_fms_legs_rte_type[ii-1] = legs_data[ii][19]
				
				B738DR_fms_legs_offset[ii-1] = legs_data[ii][45]
				B738DR_fms_legs_offset_lat[ii-1] = legs_data[ii][46]
				B738DR_fms_legs_offset_lon[ii-1] = legs_data[ii][47]
				B738DR_fms_legs_before[ii-1] = legs_data[ii][25]
				
				B738DR_fms_legs_max_spd[ii-1] = legs_data[ii][53]
				B738DR_fms_legs_min_spd[ii-1] = legs_data[ii][54]
				
				B738DR_fms_test1 = 7007
			end
		end
		
	end
	
	B738_num_of_wpts = num_of_wpts	--ii
	
	B738DR_fms_legs = fms_line .. " "
	B738DR_fms_legs_pth = fms_pth
	B738DR_fms_legs_pth_before = fms_pth_before
	
	B738DR_fms_legs_ext = fms_line2 .. " "
	--B738DR_fms_legs_change = 1
end


function copy_to_legsdata2(in_rte)

	local ii = 0
	local jj = 0
	local fms_line = ""
	local fms_pth = ""
	local radius_num = 0
	
	if in_rte == 0 then
		legs_num2 = 0
		legs_data2 = {}
		
		B738_num_of_wpts_2 = 0
		
		if legs_num > 0 then
			for ii = 1, legs_num + 1 do
				legs_data[ii][25] = ii	--waypoint idx before
				legs_data[ii][52] = ii	--waypoint idx before
				legs_data2[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data2[ii][jj] = legs_data[ii][jj]
				end
			end
			legs_num2 = legs_num
		end
		B738_find_rnav_mod(in_rte)
		calc_rte_enable2 = 0
		if rte12_act == in_rte then
			-- update_offset_idx2(rte12_act)
			offset_check(rte12_act)
		end
		create_fpln(in_rte)
	elseif in_rte == 1 then
		
		legs_num8 = 0
		legs_data8 = {}
		
		B738_num_of_wpts_2 = 0
		
		if legs_num > 0 then
			for ii = 1, legs_num + 1 do
				legs_data[ii][25] = ii	--waypoint idx before
				legs_data[ii][52] = ii	--waypoint idx before
				legs_data8[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data8[ii][jj] = legs_data[ii][jj]
				end
			end
			legs_num8 = legs_num
		end
		B738_find_rnav_mod(in_rte)
		calc_rte_enable8 = 0
		if rte12_act == in_rte then
			-- update_offset_idx2(rte12_act)
			offset_check(rte12_act)
		end
		create_fpln(in_rte)
	end
end

function legs_fill2(in_rte)
	local ii = 0
	local jj = 0
	local kk = 0
	local fms_line = ""
	local fms_pth = ""
	local fms_pth_before = ""
	local radius_num = 0
	local num_of_wpts_2 = 0
	
	local fms_line2 = ""
	
	local x_temp = 0
	local y_temp = 0
	
	local legs_num_tmp = 0
	
	
	local maxx = 0
	
	if in_rte == 0 then
		
		legs_num_tmp = true_legs_num(2)
		
		maxx = legs_num2 + 1
		
		if legs_num_tmp < 0 then
			maxx = 0
		end
		
		if legs_num_tmp >= 0 then
			for ii = 1, maxx do
				if ii < FMC_MAX_WPT then
					num_of_wpts_2 = num_of_wpts_2 + 1
					
					if ii == 1 then
						fms_line = legs_data2[1][1]
					elseif ii <= 128 then
						fms_line = fms_line .. " " .. legs_data2[ii][1]
					elseif ii == 129 then
						fms_line2 = legs_data2[ii][1]
					elseif ii > 129 then
						fms_line2 = fms_line2 .. " " .. legs_data2[ii][1]
					end
					
					B738DR_fms_legs_lat_2[ii-1] = legs_data2[ii][7]
					B738DR_fms_legs_lon_2[ii-1] = legs_data2[ii][8]
					B738DR_fms_legs_dist_2[ii-1] = legs_data2[ii][3]
					B738DR_fms_legs_brg_true_2[ii-1] = legs_data2[ii][2]
					B738DR_fms_legs_brg_mag_2[ii-1] = legs_data2[ii][18]
					B738DR_fms_legs_brg_fin_true_2[ii-1] = legs_data2[ii][37]
					B738DR_fms_legs_rad_lat_2[ii-1] = legs_data2[ii][23]
					B738DR_fms_legs_rad_lon_2[ii-1] = legs_data2[ii][24]
					B738DR_fms_legs_rad_turn_2[ii-1] = legs_data2[ii][21]
					B738DR_fms_legs_wpt_type_2[ii-1] = legs_data2[ii][17]
					B738DR_fms_legs_spd_2[ii-1] = legs_data2[ii][4]
					
					radius_num = tonumber(legs_data2[ii][29])
					if radius_num == nil then
						radius_num = 0
					else
						radius_num = radius_num / 10
					end
					B738DR_fms_legs_crs_mag_2[ii-1] = radius_num
					
					x_temp = 0
					y_temp = 0
					radius_num = tonumber(legs_data2[ii][30])
					if radius_num == nil then
						B738DR_fms_legs_hold_time_2[ii-1] = 0
						if string.len(legs_data2[ii][30]) == 4 then
							if string.sub(legs_data2[ii][30], 1, 1) == "T" then
								-- Time
								x_temp = tonumber(string.sub(legs_data2[ii][30], 2, 3))
								y_temp = (tonumber(string.sub(legs_data2[ii][30], 4, 4))) / 10
								x_temp = x_temp + y_temp
								B738DR_fms_legs_hold_time_2[ii-1] = x_temp * 60	-- in seconds
							end
						end
						B738DR_fms_legs_hold_dist_2[ii-1] = 0
					else
						B738DR_fms_legs_hold_time_2[ii-1] = 0
						B738DR_fms_legs_hold_dist_2[ii-1] = radius_num / 10
					end
					
					if legs_data2[ii][31] == "AF" then
						radius_num = tonumber(legs_data2[ii][22])
						if radius_num == nil then
							B738DR_fms_legs_radius_2[ii-1] = 0
						else
							B738DR_fms_legs_radius_2[ii-1] = radius_num / 10
						end
					else
						-- RF
						radius_num = tonumber(legs_data2[ii][33])
						if radius_num == nil then
							B738DR_fms_legs_radius_2[ii-1] = 0
						else
							B738DR_fms_legs_radius_2[ii-1] = radius_num / 1000
						end
					end
					if legs_data2[ii][31] == "" then
						fms_pth = fms_pth .. "TF"
					else
						fms_pth = fms_pth .. legs_data2[ii][31]
					end
					if legs_data2[ii][43] == "" then
						fms_pth_before = fms_pth_before .. "  "
					else
						fms_pth_before = fms_pth_before .. legs_data2[ii][43]
					end
					B738DR_fms_legs_alt_rest1_2[ii-1] = legs_data2[ii][5]
					if string.sub(legs_data2[ii][1], 1, 2) == "RW" then
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
						if legs_data2[ii][19] == 7 or legs_data2[ii][19] == 8 
						or legs_data2[ii][19] == 9 then
							-- destination runway
							B738DR_fms_legs_alt_rest_type_2[ii-1] = 4 -- destination runway
						else
							B738DR_fms_legs_alt_rest_type_2[ii-1] = 0
						end
					elseif legs_data2[ii][6] == 45 then	-- Below
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 1
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
					elseif legs_data2[ii][6] == 43 then	-- Above
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 2
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
					elseif legs_data2[ii][6] == 41 then	-- Between
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 3
						B738DR_fms_legs_alt_rest2_2[ii-1] = legs_data2[ii][41]
					else	-- At
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 0
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
					end
					B738DR_fms_legs_alt_calc_2[ii-1] = legs_data2[ii][11]
					
					if legs_data2[ii][31] == "CA" or legs_data2[ii][31] == "VA" or legs_data2[ii][31] == "FA" then
						B738DR_fms_legs_type_2[ii-1] = 6	-- Conditional wpt
					elseif legs_data2[ii][31] == "CD" or legs_data2[ii][31] == "VD" then
						B738DR_fms_legs_type_2[ii-1] = 6	-- Conditional wpt
					elseif fly_over_wpt2(ii, 0) then
						B738DR_fms_legs_type_2[ii-1] = 5	-- Fly over wpt
					else
						B738DR_fms_legs_type_2[ii-1] = 0
					end
					B738DR_fms_legs_rte_type_2[ii-1] = legs_data2[ii][19]
					B738DR_fms_legs_offset_2[ii-1] = legs_data2[ii][45]
					B738DR_fms_legs_offset_lat_2[ii-1] = legs_data2[ii][46]
					B738DR_fms_legs_offset_lon_2[ii-1] = legs_data2[ii][47]
					B738DR_fms_legs_before_2[ii-1] = legs_data2[ii][25]
					B738DR_fms_legs_idx_2[ii-1] = legs_data2[ii][52]
					--fms_line = fms_line .. " "
				end
				
			end
		end
		B738_num_of_wpts_2 = num_of_wpts_2
		B738DR_fms_legs_2 = fms_line .. " "
		B738DR_fms_legs_pth_2 = fms_pth
		B738DR_fms_legs_pth_before_2 = fms_pth_before
		
		B738DR_fms_legs_ext_2 = fms_line2 .. " "
	elseif in_rte == 1 then
		legs_num_tmp = true_legs_num(8)
		
		maxx = legs_num8 + 1
		
		if legs_num_tmp < 0 then
			maxx = 0
		end
		
		if legs_num_tmp >= 0 then
			for ii = 1, maxx do
				
				if ii < FMC_MAX_WPT then
					num_of_wpts_2 = num_of_wpts_2 + 1
					
					if ii == 1 then
						fms_line = legs_data8[1][1]
					elseif ii <= 128 then
						fms_line = fms_line .. " " .. legs_data8[ii][1]
					elseif ii == 129 then
						fms_line2 = legs_data8[ii][1]
					elseif ii > 129 then
						fms_line2 = fms_line2 .. " " .. legs_data8[ii][1]
					end
					
					B738DR_fms_legs_lat_2[ii-1] = legs_data8[ii][7]
					B738DR_fms_legs_lon_2[ii-1] = legs_data8[ii][8]
					B738DR_fms_legs_dist_2[ii-1] = legs_data8[ii][3]
					B738DR_fms_legs_brg_true_2[ii-1] = legs_data8[ii][2]
					B738DR_fms_legs_brg_mag_2[ii-1] = legs_data8[ii][18]
					B738DR_fms_legs_brg_fin_true_2[ii-1] = legs_data8[ii][37]
					B738DR_fms_legs_rad_lat_2[ii-1] = legs_data8[ii][23]
					B738DR_fms_legs_rad_lon_2[ii-1] = legs_data8[ii][24]
					B738DR_fms_legs_rad_turn_2[ii-1] = legs_data8[ii][21]
					B738DR_fms_legs_wpt_type_2[ii-1] = legs_data8[ii][17]
					B738DR_fms_legs_spd_2[ii-1] = legs_data8[ii][4]
					
					radius_num = tonumber(legs_data8[ii][29])
					if radius_num == nil then
						radius_num = 0
					else
						radius_num = radius_num / 10
					end
					B738DR_fms_legs_crs_mag_2[ii-1] = radius_num
					
					x_temp = 0
					y_temp = 0
					radius_num = tonumber(legs_data8[ii][30])
					if radius_num == nil then
						B738DR_fms_legs_hold_time_2[ii-1] = 0
						if string.len(legs_data8[ii][30]) == 4 then
							if string.sub(legs_data8[ii][30], 1, 1) == "T" then
								-- Time
								x_temp = tonumber(string.sub(legs_data8[ii][30], 2, 3))
								y_temp = (tonumber(string.sub(legs_data8[ii][30], 4, 4))) / 10
								x_temp = x_temp + y_temp
								B738DR_fms_legs_hold_time_2[ii-1] = x_temp * 60	-- in seconds
							end
						end
						B738DR_fms_legs_hold_dist_2[ii-1] = 0
					else
						B738DR_fms_legs_hold_time_2[ii-1] = 0
						B738DR_fms_legs_hold_dist_2[ii-1] = radius_num / 10
					end
					
					if legs_data8[ii][31] == "AF" then
						radius_num = tonumber(legs_data8[ii][22])
						if radius_num == nil then
							B738DR_fms_legs_radius_2[ii-1] = 0
						else
							B738DR_fms_legs_radius_2[ii-1] = radius_num / 10
						end
					else
						-- RF
						radius_num = tonumber(legs_data8[ii][33])
						if radius_num == nil then
							B738DR_fms_legs_radius_2[ii-1] = 0
						else
							B738DR_fms_legs_radius_2[ii-1] = radius_num / 1000
						end
					end
					if legs_data8[ii][31] == "" then
						fms_pth = fms_pth .. "TF"
					else
						fms_pth = fms_pth .. legs_data8[ii][31]
					end
					if legs_data8[ii][43] == "" then
						fms_pth_before = fms_pth_before .. "  "
					else
						fms_pth_before = fms_pth_before .. legs_data8[ii][43]
					end
					
					B738DR_fms_legs_alt_rest1_2[ii-1] = legs_data8[ii][5]
					if string.sub(legs_data8[ii][1], 1, 2) == "RW" then
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
						if legs_data8[ii][19] == 7 or legs_data8[ii][19] == 8 
						or legs_data8[ii][19] == 9 then
							-- destination runway
							B738DR_fms_legs_alt_rest_type_2[ii-1] = 4 -- destination runway
						else
							B738DR_fms_legs_alt_rest_type_2[ii-1] = 0
						end
					elseif legs_data8[ii][6] == 45 then	-- Below
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 1
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
					elseif legs_data8[ii][6] == 43 then	-- Above
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 2
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
					elseif legs_data8[ii][6] == 41 then	-- Between
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 3
						B738DR_fms_legs_alt_rest2_2[ii-1] = legs_data8[ii][41]
					else	-- At
						B738DR_fms_legs_alt_rest_type_2[ii-1] = 0
						B738DR_fms_legs_alt_rest2_2[ii-1] = 0
					end
					B738DR_fms_legs_alt_calc_2[ii-1] = legs_data8[ii][11]
					
					if legs_data8[ii][31] == "CA" or legs_data8[ii][31] == "VA" or legs_data8[ii][31] == "FA" then
						B738DR_fms_legs_type_2[ii-1] = 6	-- Conditional wpt
					elseif legs_data8[ii][31] == "CD" or legs_data8[ii][31] == "VD" then
						B738DR_fms_legs_type_2[ii-1] = 6	-- Conditional wpt
					elseif fly_over_wpt2(ii, 1) then
						B738DR_fms_legs_type_2[ii-1] = 5	-- Fly over wpt
					else
						B738DR_fms_legs_type_2[ii-1] = 0
					end
					B738DR_fms_legs_rte_type_2[ii-1] = legs_data8[ii][19]
					B738DR_fms_legs_offset_2[ii-1] = legs_data8[ii][45]
					B738DR_fms_legs_offset_lat_2[ii-1] = legs_data8[ii][46]
					B738DR_fms_legs_offset_lon_2[ii-1] = legs_data8[ii][47]
					B738DR_fms_legs_before_2[ii-1] = legs_data8[ii][25]
					B738DR_fms_legs_idx_2[ii-1] = legs_data8[ii][52]
				end
				
			end
		end
		B738_num_of_wpts_2 = num_of_wpts_2
		B738DR_fms_legs_2 = fms_line .. " "
		B738DR_fms_legs_pth_2 = fms_pth
		B738DR_fms_legs_pth_before_2 = fms_pth_before
		
		B738DR_fms_legs_ext_2 = fms_line2 .. " "
	end
end

function legs_fill3(in_rte)
	local ii = 0
	local jj = 0
	local kk = 0
	local fms_line = ""
	local fms_pth = ""
	local fms_pth_before = ""
	local radius_num = 0
	local num_of_wpts_2 = 0
	
	local fms_line2 = ""
	
	local x_temp = 0
	local y_temp = 0
	
	local legs_num_tmp = 0
	
	
	local maxx = 0
	
	if in_rte == 0 then
		
		legs_num_tmp = true_legs_num(2)
		
		maxx = legs_num2 + 1
		
		if legs_num_tmp < 0 then
			maxx = 0
		end
		
		if legs_num_tmp >= 0 then
			for ii = 1, maxx do
				if ii < FMC_MAX_WPT then
					num_of_wpts_2 = num_of_wpts_2 + 1
					
					if ii == 1 then
						fms_line = legs_data2[1][1]
					elseif ii <= 128 then
						fms_line = fms_line .. " " .. legs_data2[ii][1]
					elseif ii == 129 then
						fms_line2 = legs_data2[ii][1]
					elseif ii > 129 then
						fms_line2 = fms_line2 .. " " .. legs_data2[ii][1]
					end
					
					B738DR_fms_legs_lat_3[ii-1] = legs_data2[ii][7]
					B738DR_fms_legs_lon_3[ii-1] = legs_data2[ii][8]
					B738DR_fms_legs_dist_3[ii-1] = legs_data2[ii][3]
					B738DR_fms_legs_brg_true_3[ii-1] = legs_data2[ii][2]
					B738DR_fms_legs_brg_mag_3[ii-1] = legs_data2[ii][18]
					B738DR_fms_legs_brg_fin_true_3[ii-1] = legs_data2[ii][37]
					B738DR_fms_legs_rad_lat_3[ii-1] = legs_data2[ii][23]
					B738DR_fms_legs_rad_lon_3[ii-1] = legs_data2[ii][24]
					B738DR_fms_legs_rad_turn_3[ii-1] = legs_data2[ii][21]
					B738DR_fms_legs_wpt_type_3[ii-1] = legs_data2[ii][17]
					B738DR_fms_legs_spd_3[ii-1] = legs_data2[ii][4]
					
					radius_num = tonumber(legs_data2[ii][29])
					if radius_num == nil then
						radius_num = 0
					else
						radius_num = radius_num / 10
					end
					B738DR_fms_legs_crs_mag_3[ii-1] = radius_num
					
					x_temp = 0
					y_temp = 0
					radius_num = tonumber(legs_data2[ii][30])
					if radius_num == nil then
						B738DR_fms_legs_hold_time_3[ii-1] = 0
						if string.len(legs_data2[ii][30]) == 4 then
							if string.sub(legs_data2[ii][30], 1, 1) == "T" then
								-- Time
								x_temp = tonumber(string.sub(legs_data2[ii][30], 2, 3))
								y_temp = (tonumber(string.sub(legs_data2[ii][30], 4, 4))) / 10
								x_temp = x_temp + y_temp
								B738DR_fms_legs_hold_time_3[ii-1] = x_temp * 60	-- in seconds
							end
						end
						B738DR_fms_legs_hold_dist_3[ii-1] = 0
					else
						B738DR_fms_legs_hold_time_3[ii-1] = 0
						B738DR_fms_legs_hold_dist_3[ii-1] = radius_num / 10
					end
					
					if legs_data2[ii][31] == "AF" then
						radius_num = tonumber(legs_data2[ii][22])
						if radius_num == nil then
							B738DR_fms_legs_radius_3[ii-1] = 0
						else
							B738DR_fms_legs_radius_3[ii-1] = radius_num / 10
						end
					else
						-- RF
						radius_num = tonumber(legs_data2[ii][33])
						if radius_num == nil then
							B738DR_fms_legs_radius_3[ii-1] = 0
						else
							B738DR_fms_legs_radius_3[ii-1] = radius_num / 1000
						end
					end
					if legs_data2[ii][31] == "" then
						fms_pth = fms_pth .. "TF"
					else
						fms_pth = fms_pth .. legs_data2[ii][31]
					end
					if legs_data2[ii][43] == "" then
						fms_pth_before = fms_pth_before .. "  "
					else
						fms_pth_before = fms_pth_before .. legs_data2[ii][43]
					end
					B738DR_fms_legs_alt_rest1_3[ii-1] = legs_data2[ii][5]
					if string.sub(legs_data2[ii][1], 1, 2) == "RW" then
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
						if legs_data2[ii][19] == 7 or legs_data2[ii][19] == 8 
						or legs_data2[ii][19] == 9 then
							-- destination runway
							B738DR_fms_legs_alt_rest_type_3[ii-1] = 4 -- destination runway
						else
							B738DR_fms_legs_alt_rest_type_3[ii-1] = 0
						end
					elseif legs_data2[ii][6] == 45 then	-- Below
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 1
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
					elseif legs_data2[ii][6] == 43 then	-- Above
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 2
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
					elseif legs_data2[ii][6] == 41 then	-- Between
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 3
						B738DR_fms_legs_alt_rest2_3[ii-1] = legs_data2[ii][41]
					else	-- At
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 0
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
					end
					B738DR_fms_legs_alt_calc_3[ii-1] = legs_data2[ii][11]
					
					if legs_data2[ii][31] == "CA" or legs_data2[ii][31] == "VA" or legs_data2[ii][31] == "FA" then
						B738DR_fms_legs_type_3[ii-1] = 6	-- Conditional wpt
					elseif legs_data2[ii][31] == "CD" or legs_data2[ii][31] == "VD" then
						B738DR_fms_legs_type_3[ii-1] = 6	-- Conditional wpt
					elseif fly_over_wpt2(ii, 0) then
						B738DR_fms_legs_type_3[ii-1] = 5	-- Fly over wpt
					else
						B738DR_fms_legs_type_3[ii-1] = 0
					end
					B738DR_fms_legs_rte_type_3[ii-1] = legs_data2[ii][19]
					B738DR_fms_legs_offset_3[ii-1] = legs_data2[ii][45]
					B738DR_fms_legs_offset_lat_3[ii-1] = legs_data2[ii][46]
					B738DR_fms_legs_offset_lon_3[ii-1] = legs_data2[ii][47]
					B738DR_fms_legs_before_3[ii-1] = legs_data2[ii][25]
					B738DR_fms_legs_idx_3[ii-1] = legs_data2[ii][52]
					--fms_line = fms_line .. " "
				end
				
			end
		end
		B738_num_of_wpts_3 = num_of_wpts_2
		B738DR_fms_legs_3 = fms_line .. " "
		B738DR_fms_legs_pth_3 = fms_pth
		B738DR_fms_legs_pth_before_3 = fms_pth_before
		
		B738DR_fms_legs_ext_3 = fms_line2 .. " "
	elseif in_rte == 1 then
		legs_num_tmp = true_legs_num(8)
		
		maxx = legs_num8 + 1
		
		if legs_num_tmp < 0 then
			maxx = 0
		end
		
		if legs_num_tmp >= 0 then
			for ii = 1, maxx do
				
				if ii < FMC_MAX_WPT then
					num_of_wpts_2 = num_of_wpts_2 + 1
					
					if ii == 1 then
						fms_line = legs_data8[1][1]
					elseif ii <= 128 then
						fms_line = fms_line .. " " .. legs_data8[ii][1]
					elseif ii == 129 then
						fms_line2 = legs_data8[ii][1]
					elseif ii > 129 then
						fms_line2 = fms_line2 .. " " .. legs_data8[ii][1]
					end
					
					B738DR_fms_legs_lat_3[ii-1] = legs_data8[ii][7]
					B738DR_fms_legs_lon_3[ii-1] = legs_data8[ii][8]
					B738DR_fms_legs_dist_3[ii-1] = legs_data8[ii][3]
					B738DR_fms_legs_brg_true_3[ii-1] = legs_data8[ii][2]
					B738DR_fms_legs_brg_mag_3[ii-1] = legs_data8[ii][18]
					B738DR_fms_legs_brg_fin_true_3[ii-1] = legs_data8[ii][37]
					B738DR_fms_legs_rad_lat_3[ii-1] = legs_data8[ii][23]
					B738DR_fms_legs_rad_lon_3[ii-1] = legs_data8[ii][24]
					B738DR_fms_legs_rad_turn_3[ii-1] = legs_data8[ii][21]
					B738DR_fms_legs_wpt_type_3[ii-1] = legs_data8[ii][17]
					B738DR_fms_legs_spd_3[ii-1] = legs_data8[ii][4]
					
					radius_num = tonumber(legs_data8[ii][29])
					if radius_num == nil then
						radius_num = 0
					else
						radius_num = radius_num / 10
					end
					B738DR_fms_legs_crs_mag_3[ii-1] = radius_num
					
					x_temp = 0
					y_temp = 0
					radius_num = tonumber(legs_data8[ii][30])
					if radius_num == nil then
						B738DR_fms_legs_hold_time_3[ii-1] = 0
						if string.len(legs_data8[ii][30]) == 4 then
							if string.sub(legs_data8[ii][30], 1, 1) == "T" then
								-- Time
								x_temp = tonumber(string.sub(legs_data8[ii][30], 2, 3))
								y_temp = (tonumber(string.sub(legs_data8[ii][30], 4, 4))) / 10
								x_temp = x_temp + y_temp
								B738DR_fms_legs_hold_time_3[ii-1] = x_temp * 60	-- in seconds
							end
						end
						B738DR_fms_legs_hold_dist_3[ii-1] = 0
					else
						B738DR_fms_legs_hold_time_3[ii-1] = 0
						B738DR_fms_legs_hold_dist_3[ii-1] = radius_num / 10
					end
					
					if legs_data8[ii][31] == "AF" then
						radius_num = tonumber(legs_data8[ii][22])
						if radius_num == nil then
							B738DR_fms_legs_radius_3[ii-1] = 0
						else
							B738DR_fms_legs_radius_3[ii-1] = radius_num / 10
						end
					else
						-- RF
						radius_num = tonumber(legs_data8[ii][33])
						if radius_num == nil then
							B738DR_fms_legs_radius_3[ii-1] = 0
						else
							B738DR_fms_legs_radius_3[ii-1] = radius_num / 1000
						end
					end
					if legs_data8[ii][31] == "" then
						fms_pth = fms_pth .. "TF"
					else
						fms_pth = fms_pth .. legs_data8[ii][31]
					end
					if legs_data8[ii][43] == "" then
						fms_pth_before = fms_pth_before .. "  "
					else
						fms_pth_before = fms_pth_before .. legs_data8[ii][43]
					end
					
					B738DR_fms_legs_alt_rest1_3[ii-1] = legs_data8[ii][5]
					if string.sub(legs_data8[ii][1], 1, 2) == "RW" then
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
						if legs_data8[ii][19] == 7 or legs_data8[ii][19] == 8 
						or legs_data8[ii][19] == 9 then
							-- destination runway
							B738DR_fms_legs_alt_rest_type_3[ii-1] = 4 -- destination runway
						else
							B738DR_fms_legs_alt_rest_type_3[ii-1] = 0
						end
					elseif legs_data8[ii][6] == 45 then	-- Below
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 1
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
					elseif legs_data8[ii][6] == 43 then	-- Above
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 2
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
					elseif legs_data8[ii][6] == 41 then	-- Between
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 3
						B738DR_fms_legs_alt_rest2_3[ii-1] = legs_data8[ii][41]
					else	-- At
						B738DR_fms_legs_alt_rest_type_3[ii-1] = 0
						B738DR_fms_legs_alt_rest2_3[ii-1] = 0
					end
					B738DR_fms_legs_alt_calc_3[ii-1] = legs_data8[ii][11]
					
					if legs_data8[ii][31] == "CA" or legs_data8[ii][31] == "VA" or legs_data8[ii][31] == "FA" then
						B738DR_fms_legs_type_3[ii-1] = 6	-- Conditional wpt
					elseif legs_data8[ii][31] == "CD" or legs_data8[ii][31] == "VD" then
						B738DR_fms_legs_type_3[ii-1] = 6	-- Conditional wpt
					elseif fly_over_wpt2(ii, 1) then
						B738DR_fms_legs_type_3[ii-1] = 5	-- Fly over wpt
					else
						B738DR_fms_legs_type_3[ii-1] = 0
					end
					B738DR_fms_legs_rte_type_3[ii-1] = legs_data8[ii][19]
					B738DR_fms_legs_offset_3[ii-1] = legs_data8[ii][45]
					B738DR_fms_legs_offset_lat_3[ii-1] = legs_data8[ii][46]
					B738DR_fms_legs_offset_lon_3[ii-1] = legs_data8[ii][47]
					B738DR_fms_legs_before_3[ii-1] = legs_data8[ii][25]
					B738DR_fms_legs_idx_3[ii-1] = legs_data8[ii][52]
				end
				
			end
		end
		B738_num_of_wpts_3 = num_of_wpts_2
		B738DR_fms_legs_3 = fms_line .. " "
		B738DR_fms_legs_pth_3 = fms_pth
		B738DR_fms_legs_pth_before_3 = fms_pth_before
		
		B738DR_fms_legs_ext_3 = fms_line2 .. " "
	end
end

function rte_copy(aaa, in_rte)

	local tt1 = 0
	local tt2 = 0
	local tt3 = 0
	
	legs_data2_tmp_n = 0
	legs_data2_tmp = {}
	
	if in_rte == 0 then
		tt3 = legs_num2 + 1
		if aaa <= tt3 and aaa > 0 then
			for tt1 = aaa, tt3 do
				legs_data2_tmp_n = legs_data2_tmp_n + 1
				legs_data2_tmp[legs_data2_tmp_n] = {}
				for tt2 = 1, MAX_LEGS_DATA do
					legs_data2_tmp[legs_data2_tmp_n][tt2] = legs_data2[tt1][tt2]
				end
			end
		end
	elseif in_rte == 1 then
		tt3 = legs_num8 + 1
		if aaa <= tt3 and aaa > 0 then
			for tt1 = aaa, tt3 do
				legs_data2_tmp_n = legs_data2_tmp_n + 1
				legs_data2_tmp[legs_data2_tmp_n] = {}
				for tt2 = 1, MAX_LEGS_DATA do
					legs_data2_tmp[legs_data2_tmp_n][tt2] = legs_data8[tt1][tt2]
				end
			end
		end
	end
end

function rte_paste(aaa, in_rte)

	local tt1 = 0
	local tt2 = 0
	local cnt_leg = aaa - 1
	
	if in_rte == 0 then
		if legs_data2_tmp_n > 0 then
			for tt1 = 1, legs_data2_tmp_n do
				cnt_leg = cnt_leg + 1
				legs_data2[cnt_leg] = {}
				for tt2 = 1, MAX_LEGS_DATA do
					legs_data2[cnt_leg][tt2] = legs_data2_tmp[tt1][tt2]
				end
			end
			legs_num2 = cnt_leg - 1
		end
	elseif in_rte == 1 then
		if legs_data2_tmp_n > 0 then
			for tt1 = 1, legs_data2_tmp_n do
				cnt_leg = cnt_leg + 1
				legs_data8[cnt_leg] = {}
				for tt2 = 1, MAX_LEGS_DATA do
					legs_data8[cnt_leg][tt2] = legs_data2_tmp[tt1][tt2]
				end
			end
			legs_num8 = cnt_leg - 1
		end
	end
	
end

function rte_add_wpt2(wpt_idx, mode_in, in_rte)
	
	if in_rte == 0 then
		if wpt_idx <= navaid_list_n and wpt_idx ~= 0 then
				legs_num2 = wpt_id_paste
				legs_data2[legs_num2] = {}
				legs_data2[legs_num2][1] = navaid_list[wpt_idx][4]	--entry
				legs_data2[legs_num2][2] = 0		-- brg
				legs_data2[legs_num2][3] = 0		-- distance
				legs_data2[legs_num2][4] = 0		-- speed
				legs_data2[legs_num2][5] = 0		-- altitude
				legs_data2[legs_num2][6] = 0	-- altitude type
				legs_data2[legs_num2][7] = navaid_list[wpt_idx][2]		-- latitude
				legs_data2[legs_num2][8] = navaid_list[wpt_idx][3]		-- longitude
				legs_data2[legs_num2][9] = "DIRECT"			-- via id
				legs_data2[legs_num2][10] = 0		-- calc speed
				legs_data2[legs_num2][11] = 0		-- calc altitude
				legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
				legs_data2[legs_num2][13] = 0
				legs_data2[legs_num2][14] = 0		-- rest alt
				legs_data2[legs_num2][15] = 0		-- last fuel
				legs_data2[legs_num2][16] = navaid_list[wpt_idx][8]		-- reg code
				legs_data2[legs_num2][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
				legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data2[legs_num2][20] = 0
				legs_data2[legs_num2][21] = -1
				legs_data2[legs_num2][22] = ""
				legs_data2[legs_num2][23] = 0
				legs_data2[legs_num2][24] = 0
				legs_data2[legs_num2][25] = 0
				legs_data2[legs_num2][26] = 0
				legs_data2[legs_num2][27] = ""
				legs_data2[legs_num2][28] = ""
				legs_data2[legs_num2][29] = ""
				legs_data2[legs_num2][30] = ""
				legs_data2[legs_num2][31] = ""
				legs_data2[legs_num2][32] = 0
				legs_data2[legs_num2][33] = ""
				legs_data2[legs_num2][34] = ""
				legs_data2[legs_num2][35] = ""
				legs_data2[legs_num2][36] = navaid_list[wpt_idx][1]
				legs_data2[legs_num2][37] = 0
				legs_data2[legs_num2][38] = ""
				legs_data2[legs_num2][39] = ""
				legs_data2[legs_num2][40] = 0
				legs_data2[legs_num2][41] = 0
				legs_data2[legs_num2][42] = 0
				legs_data2[legs_num2][43] = ""
				legs_data2[legs_num2][44] = 0
				legs_data2[legs_num2][45] = 0
				legs_data2[legs_num2][46] = 0
				legs_data2[legs_num2][47] = 0
				legs_data2[legs_num2][48] = ""
				legs_data2[legs_num2][49] = 0
				legs_data2[legs_num2][50] = 0
				legs_data2[legs_num2][51] = 0
				legs_data2[legs_num2][52] = 0
				legs_data2[legs_num2][53] = 0
				legs_data2[legs_num2][54] = 0
				rte_lat = legs_data2[legs_num2][7]
				rte_lon = legs_data2[legs_num2][8]
				
				if add_disco == 1 then
					-- create discontinuity
					legs_num2 = legs_num2 + 1
					rte_add_disco(legs_num2, in_rte)
				end
				
				rte_paste(legs_num2 + 1, in_rte)
				calc_rte_enable2 = 1
		end
		if mode_in == 0 then
			page_sel_wpt = 0
			page_legs = 1
			act_page = act_page_old
			max_page = math.max(max_page, act_page)
		else
			page_sel_wpt_2 = 0
			page_legs2 = 1
			act_page2 = act_page_old2
			max_page2 = math.max(max_page2, act_page2)
		end
	elseif in_rte == 1 then
		if wpt_idx <= navaid_list_n and wpt_idx ~= 0 then
				legs_num8 = wpt_id_paste
				legs_data8[legs_num8] = {}
				legs_data8[legs_num8][1] = navaid_list[wpt_idx][4]	--entry
				legs_data8[legs_num8][2] = 0		-- brg
				legs_data8[legs_num8][3] = 0		-- distance
				legs_data8[legs_num8][4] = 0		-- speed
				legs_data8[legs_num8][5] = 0		-- altitude
				legs_data8[legs_num8][6] = 0	-- altitude type
				legs_data8[legs_num8][7] = navaid_list[wpt_idx][2]		-- latitude
				legs_data8[legs_num8][8] = navaid_list[wpt_idx][3]		-- longitude
				legs_data8[legs_num8][9] = "DIRECT"			-- via id
				legs_data8[legs_num8][10] = 0		-- calc speed
				legs_data8[legs_num8][11] = 0		-- calc altitude
				legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
				legs_data8[legs_num8][13] = 0
				legs_data8[legs_num8][14] = 0		-- rest alt
				legs_data8[legs_num8][15] = 0		-- last fuel
				legs_data8[legs_num8][16] = navaid_list[wpt_idx][8]		-- reg code
				legs_data8[legs_num8][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
				legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data8[legs_num8][20] = 0
				legs_data8[legs_num8][21] = -1
				legs_data8[legs_num8][22] = ""
				legs_data8[legs_num8][23] = 0
				legs_data8[legs_num8][24] = 0
				legs_data8[legs_num8][25] = 0
				legs_data8[legs_num8][26] = 0
				legs_data8[legs_num8][27] = ""
				legs_data8[legs_num8][28] = ""
				legs_data8[legs_num8][29] = ""
				legs_data8[legs_num8][30] = ""
				legs_data8[legs_num8][31] = ""
				legs_data8[legs_num8][32] = 0
				legs_data8[legs_num8][33] = ""
				legs_data8[legs_num8][34] = ""
				legs_data8[legs_num8][35] = ""
				legs_data8[legs_num8][36] = navaid_list[wpt_idx][1]
				legs_data8[legs_num8][37] = 0
				legs_data8[legs_num8][38] = ""
				legs_data8[legs_num8][39] = ""
				legs_data8[legs_num8][40] = 0
				legs_data8[legs_num8][41] = 0
				legs_data8[legs_num8][42] = 0
				legs_data8[legs_num8][43] = ""
				legs_data8[legs_num8][44] = 0
				legs_data8[legs_num8][45] = 0
				legs_data8[legs_num8][46] = 0
				legs_data8[legs_num8][47] = 0
				legs_data8[legs_num8][48] = ""
				legs_data8[legs_num8][49] = 0
				legs_data8[legs_num8][50] = 0
				legs_data8[legs_num8][51] = 0
				legs_data8[legs_num8][52] = 0
				legs_data8[legs_num8][53] = 0
				legs_data8[legs_num8][54] = 0
				rte_lat = legs_data8[legs_num8][7]
				rte_lon = legs_data8[legs_num8][8]
				
				if add_disco == 1 then
					-- create discontinuity
					legs_num8 = legs_num8 + 1
					rte_add_disco(legs_num8, in_rte)
				end
				
				rte_paste(legs_num8 + 1, in_rte)
				calc_rte_enable8 = 1
		end
		if mode_in == 0 then
			page_sel_wpt = 0
			page_legs = 1
			act_page = act_page_old
			max_page = math.max(max_page, act_page)
		else
			page_sel_wpt_2 = 0
			page_legs2 = 1
			act_page2 = act_page_old2
			max_page2 = math.max(max_page2, act_page2)
		end
	end
end

function rte_add_hold_wpt(entry_in, mode_in, in_rte)
	
	-- legs_num3 = 0
	
	-- add WPT
	find_navaid(entry_in, "", 0, "", in_rte)
	
	if navaid_list_n > 0 then
		
		if navaid_list_n == 1 then
			if mode_in == 0 then
				act_page_old = act_page
			else
				act_page_old2 = act_page2
			end
			sel_add_hold_wpt(1, mode_in, in_rte)
		else
			if mode_in == 0 then
				page_sel_hold_wpt = 1
				page_legs = 0
				act_page_old = act_page
				act_page = 1
			else
				page_sel_hold_wpt_2 = 1
				page_legs2 = 0
				act_page_old2 = act_page2
				act_page2 = 1
			end
		end
	else
		add_fmc_msg(NOT_IN_DATABASE, 1)
	end
	
	if mode_in == 0 then
		entry = ""
	else
		entry2 = ""
	end
end

function sel_add_hold_wpt(in_item, mode_in, in_rte)
	legs_num3 = in_rte + 1
	legs_data3[legs_num3] = {}
	legs_data3[legs_num3][1] = navaid_list[in_item][4]	--entry
	legs_data3[legs_num3][2] = 0		-- brg
	legs_data3[legs_num3][3] = 0		-- distance
	legs_data3[legs_num3][4] = 0		-- speed
	legs_data3[legs_num3][5] = 0		-- altitude
	legs_data3[legs_num3][6] = 0	-- altitude type
	legs_data3[legs_num3][7] = navaid_list[in_item][2]		-- latitude
	legs_data3[legs_num3][8] = navaid_list[in_item][3]		-- longitude
	legs_data3[legs_num3][9] = "DIRECT"			-- via id
	legs_data3[legs_num3][10] = 0		-- calc speed
	legs_data3[legs_num3][11] = 0		-- calc altitude
	legs_data3[legs_num3][12] = 0		-- calc altitude vnav pth
	legs_data3[legs_num3][13] = 0
	legs_data3[legs_num3][14] = 0		-- rest alt
	legs_data3[legs_num3][15] = 0		-- last fuel
	legs_data3[legs_num3][16] = navaid_list[in_item][8]		-- reg code
	legs_data3[legs_num3][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
	legs_data3[legs_num3][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
	legs_data3[legs_num3][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
	legs_data3[legs_num3][20] = 0
	legs_data3[legs_num3][21] = -1
	legs_data3[legs_num3][22] = ""
	legs_data3[legs_num3][23] = 0
	legs_data3[legs_num3][24] = 0
	legs_data3[legs_num3][25] = 0
	legs_data3[legs_num3][26] = 0
	legs_data3[legs_num3][27] = ""
	legs_data3[legs_num3][28] = ""
	legs_data3[legs_num3][29] = ""
	legs_data3[legs_num3][30] = ""
	legs_data3[legs_num3][31] = ""
	legs_data3[legs_num3][32] = 0
	legs_data3[legs_num3][33] = ""
	legs_data3[legs_num3][34] = ""
	legs_data3[legs_num3][35] = ""
	legs_data3[legs_num3][36] = navaid_list[in_item][1]
	legs_data3[legs_num3][37] = 0
	legs_data3[legs_num3][38] = ""
	legs_data3[legs_num3][39] = ""
	legs_data3[legs_num3][40] = 0
	legs_data3[legs_num3][41] = 0
	legs_data3[legs_num3][42] = 0
	legs_data3[legs_num3][43] = ""
	legs_data3[legs_num3][44] = 0
	legs_data3[legs_num3][45] = 0
	legs_data3[legs_num3][46] = 0
	legs_data3[legs_num3][47] = 0
	legs_data3[legs_num3][48] = ""
	legs_data3[legs_num3][49] = 0
	legs_data3[legs_num3][50] = 0
	legs_data3[legs_num3][51] = 0
	legs_data3[legs_num3][52] = 0
	legs_data3[legs_num3][53] = 0
	legs_data3[legs_num3][54] = 0
	
	if mode_in == 0 then
		page_sel_hold_wpt = 0
		page_legs = 1
		act_page = act_page_old
		max_page = math.max(max_page, act_page)
		
		fmc_message_num = fmc_message_num + 1
		fmc_message[fmc_message_num] = HOLD_AT .. legs_data3[legs_num3][1]
		fmc_message_warn[fmc_message_num] = 1
		fms_msg_sound = 1
		entry = ""
	else
		page_sel_hold_wpt_2 = 0
		page_legs2 = 1
		act_page2 = act_page_old2
		max_page2 = math.max(max_page2, act_page2)
		
		fmc_message_num = fmc_message_num + 1
		fmc_message[fmc_message_num] = HOLD_AT .. legs_data3[legs_num3][1]
		fmc_message_warn[fmc_message_num] = 1
		fms_msg_sound = 1
		entry2 = ""
	end
	
	if in_rte == 0 then
		new_hold_at = 1
	else
		new_hold_at2 = 1
	end
	
	local hold_record = find_hold_data(legs_data3[1][1], legs_data3[legs_num3][16])
	
	local def_altitude = simDR_altitude_pilot
	local def_altitude2 = 0
	local def_altitude_type = 0
	local def_speed = simDR_ground_spd * 1.94384449244
	local def_turn = 1
	local def_time = 0
	local def_dist = 0
	local def_crs = 0
	
	if in_rte == 0 then
		def_crs = nd_calc_brg(legs_data2[offset_mod][7], legs_data2[offset_mod][8], legs_data3[legs_num3][7], legs_data3[legs_num3][8])
	else
		def_crs = nd_calc_brg(legs_data8[offset_mod2][7], legs_data8[offset_mod2][8], legs_data3[legs_num3][7], legs_data3[legs_num3][8])
	end
	def_crs = (def_crs + simDR_mag_variation + 360) % 360
	
	if hold_record > 0 then
		-- altitude
		if hold_nav[hold_record][7] == 0 then
			--below
			def_altitude = hold_nav[hold_record][8]
			def_altitude_type = 45
		elseif hold_nav[hold_record][8] == 0 then
			--above
			def_altitude = hold_nav[hold_record][7]
			def_altitude_type = 43
		else
			if hold_nav[hold_record][7] == hold_nav[hold_record][8] == 0 then
				--at
				def_altitude = hold_nav[hold_record][7]
				def_altitude_type = 0
			else
				--between
				def_altitude = hold_nav[hold_record][7]
				def_altitude2 = hold_nav[hold_record][8]
				def_altitude_type = 41
			end
		end
		-- speed
		def_speed = hold_nav[hold_record][9]
		
		-- time/distance
		def_time = hold_nav[hold_record][4] * 60
		def_dist = hold_nav[hold_record][5]
		
		-- inbound crs (magnetic)
		def_crs = hold_nav[hold_record][3]
		
		-- turn
		def_turn = hold_nav[hold_record][6]
	end
	
	if mode_in == 0 then
		new_hold_at_id = legs_data3[legs_num3][1]
		new_hold_at_lat = legs_data3[legs_num3][7]
		new_hold_at_lon = legs_data3[legs_num3][8]
		new_hold_at_crs_mag = def_crs
		new_hold_at_turn = def_turn
		new_hold_at_speed = def_speed
		new_hold_at_altitude = def_altitude
		new_hold_at_time = def_time
		new_hold_at_distance = def_dist
	else
		new_hold_at_id2 = legs_data3[legs_num3][1]
		new_hold_at_lat2 = legs_data3[legs_num3][7]
		new_hold_at_lon2 = legs_data3[legs_num3][8]
		new_hold_at_crs_mag2 = def_crs
		new_hold_at_turn2 = def_turn
		new_hold_at_speed2 = def_speed
		new_hold_at_altitude2 = def_altitude
		new_hold_at_time2 = def_time
		new_hold_at_distance2 = def_dist
	end
end

function rte_add_hold_fix(in_item, mode_in, in_rte)
	
	local ww = 0
	
	if in_rte == 0 then
		ww = in_item
		if legs_data2[in_item][1] == "(VECTOR)" and in_item < legs_num then
			while legs_data2[ww][1] == "(VECTOR)" or legs_data2[ww][1] == "DISCONTINUITY" do
				ww = ww + 1
				if ww > legs_num2 then
					break
				end
			end
			rte_copy(ww, in_rte)
		else
			rte_copy(in_item, in_rte)
		end
		
		for ww = 1, MAX_LEGS_DATA do
			legs_data2[in_item][ww] = legs_data3[in_rte + 1][ww]
		end
		
		-- add Disco
		rte_add_disco(in_item + 1, in_rte)
		
		--paste
		rte_paste(in_item + 2, in_rte)
		
		if in_rte == 0 then
			new_hold_idx = in_item
		else
			new_hold_idx2 = in_item
		end
		
		-- add new hold
		rte_add_hold(in_item, mode_in, in_rte)
		
		if mode_in == 0 then
			page_hold = 1
			page_legs = 0
			entry = ""
			act_page = 1
			item_sel = 0
			item_sel_act = 0
			
		else
			page_hold2 = 1
			page_legs2 = 0
			entry2 = ""
			act_page2 = 1
			item_sel2 = 0
			item_sel_act2 = 0
		end
		
		new_hold = 0
		new_hold_at = 0
		
		if rte12_act == in_rte then
			hold_exec = 1
		end
		
		calc_rte_act2 = 1
	elseif in_rte == 1 then
		ww = in_item
		if legs_data8[in_item][1] == "(VECTOR)" and in_item < legs_num then
			while legs_data8[ww][1] == "(VECTOR)" or legs_data8[ww][1] == "DISCONTINUITY" do
				ww = ww + 1
				if ww > legs_num8 then
					break
				end
			end
			rte_copy(ww, in_rte)
		else
			rte_copy(in_item, in_rte)
		end
		
		for ww = 1, MAX_LEGS_DATA do
			legs_data8[in_item][ww] = legs_data3[in_rte + 1][ww]
		end
		
		-- add Disco
		rte_add_disco(in_item + 1, in_rte)
		
		--paste
		rte_paste(in_item + 2, in_rte)
		
		if in_rte == 0 then
			new_hold_idx = in_item
		else
			new_hold_idx2 = in_item
		end
		
		-- add new hold
		rte_add_hold(in_item, mode_in, in_rte)
		
		if mode_in == 0 then
			page_hold = 1
			page_legs = 0
			entry = ""
			-- hold_exec = 1
			act_page = 1
			new_hold = 0
			item_sel = 0
			item_sel_act = 0
			new_hold_at = 0
		else
			page_hold2 = 1
			page_legs2 = 0
			entry2 = ""
			-- hold_exec2 = 1
			act_page2 = 1
			new_hold2 = 0
			item_sel2 = 0
			item_sel_act2 = 0
			new_hold_at2 = 0
		end
		
		new_hold2 = 0
		new_hold_at2 = 0
		if rte12_act == in_rte then
			hold_exec2 = 1
		end
		
		calc_rte_act8 = 1
	end
end

function rte_add_wpt(aaa, entry_in, mode_in, in_rte)

	local ii = aaa + 1
	local jj = 0
	
	if in_rte == 0 then
		jj = legs_num2 + 1
		-- add WPT
		if legs_data2[aaa][1] ~= entry_in and legs_data2[aaa-1][1] ~= entry_in then
		
			find_navaid(entry_in, "", 0, "", in_rte)
			if navaid_list_n > 0 then
			
				
				if legs_data2[aaa][1] == "DISCONTINUITY" then
					-- rte_copy(ii)
					rte_copy(aaa, in_rte)
					add_disco = 0
				else
					legs_data2[aaa][31] = "TF"
					rte_copy(aaa, in_rte)
					if aaa == jj then
						add_disco = 0
					else
						add_disco = 1
					end
				end
				
				if navaid_list_n == 1 then
				
					legs_num2 = aaa
					legs_data2[legs_num2] = {}
					legs_data2[legs_num2][1] = navaid_list[1][4]	--entry
					legs_data2[legs_num2][2] = 0		-- brg
					legs_data2[legs_num2][3] = 0		-- distance
					legs_data2[legs_num2][4] = 0		-- speed
					legs_data2[legs_num2][5] = 0		-- altitude
					legs_data2[legs_num2][6] = 0	-- altitude type
					legs_data2[legs_num2][7] = navaid_list[1][2]		-- latitude
					legs_data2[legs_num2][8] = navaid_list[1][3]		-- longitude
					legs_data2[legs_num2][9] = "DIRECT"			-- via id
					legs_data2[legs_num2][10] = 0		-- calc speed
					legs_data2[legs_num2][11] = 0		-- calc altitude
					legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
					legs_data2[legs_num2][13] = 0
					legs_data2[legs_num2][14] = 0		-- rest alt
					legs_data2[legs_num2][15] = 0		-- last fuel
					legs_data2[legs_num2][16] = navaid_list[1][8]		-- reg code
					legs_data2[legs_num2][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data2[legs_num2][20] = 0
					legs_data2[legs_num2][21] = -1
					legs_data2[legs_num2][22] = ""
					legs_data2[legs_num2][23] = 0
					legs_data2[legs_num2][24] = 0
					legs_data2[legs_num2][25] = 0
					legs_data2[legs_num2][26] = 0
					legs_data2[legs_num2][27] = ""
					legs_data2[legs_num2][28] = ""
					legs_data2[legs_num2][29] = ""
					legs_data2[legs_num2][30] = ""
					legs_data2[legs_num2][31] = ""
					legs_data2[legs_num2][32] = 0
					legs_data2[legs_num2][33] = ""
					legs_data2[legs_num2][34] = ""
					legs_data2[legs_num2][35] = ""
					legs_data2[legs_num2][36] = navaid_list[1][1]
					legs_data2[legs_num2][37] = 0
					legs_data2[legs_num2][38] = ""
					legs_data2[legs_num2][39] = ""
					legs_data2[legs_num2][40] = 0
					legs_data2[legs_num2][41] = 0
					legs_data2[legs_num2][42] = 0
					legs_data2[legs_num2][43] = ""
					legs_data2[legs_num2][44] = 0
					legs_data2[legs_num2][45] = 0
					legs_data2[legs_num2][46] = 0
					legs_data2[legs_num2][47] = 0
					legs_data2[legs_num2][48] = ""
					legs_data2[legs_num2][49] = 0
					legs_data2[legs_num2][50] = 0
					legs_data2[legs_num2][51] = 0
					legs_data2[legs_num2][52] = 0
					legs_data2[legs_num2][53] = 0
					legs_data2[legs_num2][54] = 0
					rte_lat = legs_data2[legs_num2][7]
					rte_lon = legs_data2[legs_num2][8]
					
					if add_disco == 1 then
						-- create discontinuity
						legs_num2 = legs_num2 + 1
						rte_add_disco(legs_num2, in_rte)
					end
					
					ii = legs_num2 + 1
					rte_paste(ii, in_rte)
					if rte12_act == in_rte then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
					calc_rte_enable2 = 1
				
				else
					if mode_in == 0 then
						page_sel_wpt = 1
						page_legs = 0
						act_page_old = act_page
						act_page = 1
					else
						page_sel_wpt_2 = 1
						page_legs2 = 0
						act_page_old2 = act_page2
						act_page2 = 1
					end
					wpt_id_paste = aaa
				end
				
			else
				add_fmc_msg(NOT_IN_DATABASE, 1)
			end
		end
		if mode_in == 0 then
			entry = ""
		else
			entry2 = ""
		end
	elseif in_rte == 1 then
		jj = legs_num8 + 1
		-- add WPT
		if legs_data8[aaa][1] ~= entry_in and legs_data8[aaa-1][1] ~= entry_in then
		
			find_navaid(entry_in, "", 0, "", in_rte)
			if navaid_list_n > 0 then
			
				
				if legs_data8[aaa][1] == "DISCONTINUITY" then
					-- rte_copy(ii)
					rte_copy(aaa, in_rte)
					add_disco = 0
				else
					legs_data8[aaa][31] = "TF"
					rte_copy(aaa, in_rte)
					if aaa == jj then
						add_disco = 0
					else
						add_disco = 1
					end
				end
				
				if navaid_list_n == 1 then
				
					legs_num8 = aaa
					legs_data8[legs_num8] = {}
					legs_data8[legs_num8][1] = navaid_list[1][4]	--entry
					legs_data8[legs_num8][2] = 0		-- brg
					legs_data8[legs_num8][3] = 0		-- distance
					legs_data8[legs_num8][4] = 0		-- speed
					legs_data8[legs_num8][5] = 0		-- altitude
					legs_data8[legs_num8][6] = 0	-- altitude type
					legs_data8[legs_num8][7] = navaid_list[1][2]		-- latitude
					legs_data8[legs_num8][8] = navaid_list[1][3]		-- longitude
					legs_data8[legs_num8][9] = "DIRECT"			-- via id
					legs_data8[legs_num8][10] = 0		-- calc speed
					legs_data8[legs_num8][11] = 0		-- calc altitude
					legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
					legs_data8[legs_num8][13] = 0
					legs_data8[legs_num8][14] = 0		-- rest alt
					legs_data8[legs_num8][15] = 0		-- last fuel
					legs_data8[legs_num8][16] = navaid_list[1][8]		-- reg code
					legs_data8[legs_num8][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
					legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
					legs_data8[legs_num8][20] = 0
					legs_data8[legs_num8][21] = -1
					legs_data8[legs_num8][22] = ""
					legs_data8[legs_num8][23] = 0
					legs_data8[legs_num8][24] = 0
					legs_data8[legs_num8][25] = 0
					legs_data8[legs_num8][26] = 0
					legs_data8[legs_num8][27] = ""
					legs_data8[legs_num8][28] = ""
					legs_data8[legs_num8][29] = ""
					legs_data8[legs_num8][30] = ""
					legs_data8[legs_num8][31] = ""
					legs_data8[legs_num8][32] = 0
					legs_data8[legs_num8][33] = ""
					legs_data8[legs_num8][34] = ""
					legs_data8[legs_num8][35] = ""
					legs_data8[legs_num8][36] = navaid_list[1][1]
					legs_data8[legs_num8][37] = 0
					legs_data8[legs_num8][38] = ""
					legs_data8[legs_num8][39] = ""
					legs_data8[legs_num8][40] = 0
					legs_data8[legs_num8][41] = 0
					legs_data8[legs_num8][42] = 0
					legs_data8[legs_num8][43] = ""
					legs_data8[legs_num8][44] = 0
					legs_data8[legs_num8][45] = 0
					legs_data8[legs_num8][46] = 0
					legs_data8[legs_num8][47] = 0
					legs_data8[legs_num8][48] = ""
					legs_data8[legs_num8][49] = 0
					legs_data8[legs_num8][50] = 0
					legs_data8[legs_num8][51] = 0
					legs_data8[legs_num8][52] = 0
					legs_data8[legs_num8][53] = 0
					legs_data8[legs_num8][54] = 0
					rte_lat = legs_data8[legs_num8][7]
					rte_lon = legs_data8[legs_num8][8]
					
					if add_disco == 1 then
						-- create discontinuity
						legs_num8 = legs_num8 + 1
						rte_add_disco(legs_num8, in_rte)
					end
					
					ii = legs_num8 + 1
					rte_paste(ii, in_rte)
					if rte12_act == in_rte then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
					calc_rte_enable8 = 1
				
				else
					if mode_in == 0 then
						page_sel_wpt = 1
						page_legs = 0
						act_page_old = act_page
						act_page = 1
					else
						page_sel_wpt_2 = 1
						page_legs2 = 0
						act_page_old2 = act_page2
						act_page2 = 1
					end
					wpt_id_paste = aaa
				end
				
			else
				add_fmc_msg(NOT_IN_DATABASE, 1)
			end
		end
		if mode_in == 0 then
			entry = ""
		else
			entry2 = ""
		end
	end
end

-- add SEA330/10, SEA/5
function rte_add_wpt3(aaa, entry_in, mode_in, in_rte)
	
	local ii = aaa + 1
	local jj = legs_num2 + 1
	local entry_len = string.len(entry_in)
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	local oo = -1
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local temp_brg = 0
	local nd_dis = 0
	local id_nav_txt = ""
	
	local along_wpt = 0
	local cross_wpt = 0
	local aa = 0
	local bb = 0
	local cc = 0
	local dd = 0
	local along_dist = 0
	local wpt_ok = 0
	local item_select = 0
	
	local id_brg2 = 0
	local id_dist2 = 0
	
	local id_nav = ""
	local id_brg = 0
	local id_dist = 0
	local id_nav02 = ""
	local id_brg02 = 0
	
	local zzz = 0
	local byte_str = ""
	
	local id_nav_mem = ""
	local lat_mem = 0
	local lon_mem = 0
	
	if mode_in == 0 then
		item_select = item_sel
	else
		item_select = item_sel2
	end
	
	if in_rte == 0 then
		jj = legs_num2 + 1
		aa,bb = string.find(entry_in, "/")
		if aa == nil then
			-- without "/"
			ll = 1
		elseif aa < 3 then
			-- min "XX/"
			ll = 1
		elseif aa == entry_len then
			-- last char "/"
			ll = 1
		elseif item_select == 0 then
			-- find id_nav
			id_nav = string.sub(entry_in, 1, aa-1)
			find_navaid(id_nav, "", 0, "", in_rte)
			if navaid_list_n > 0 then
				-- SEA/9, NEMUS/9 => allong waypoint
				along_dist = tonumber(string.sub(entry_in, aa+1, -1))	-- distance
				if along_dist == nil then
					ll = 1
				else
					along_dist = tonumber(string.format("%5.1f", along_dist))
					along_wpt = 1
				end
			else
				if aa > 5 then
					-- brg
					if string.sub(entry_in, aa-2, aa-2) == "." then
						id_brg = tonumber(string.sub(entry_in, aa-5, aa-1))
						if aa > 6 then
							id_nav = string.sub(entry_in, 1, aa-6)
						else
							id_nav = string.sub(entry_in, 1, aa-5)
						end
					else
						id_brg = tonumber(string.sub(entry_in, aa-3, aa-1))
						id_nav = string.sub(entry_in, 1, aa-4)
					end
					if id_brg == nil then
						ll = 1
					else
						--999/9 or 999.9/9
						if id_brg < 0 or id_brg > 360 then
							ll = 1
						else
							find_navaid(id_nav, "", 0, "", in_rte)
							if navaid_list_n > 0 then
								id_brg = tonumber(string.format("%5.1f", id_brg))
								--dist
								id_dist = tonumber(string.sub(entry_in, aa+1, -1))
								if id_dist == nil then
									-- id_brg02
									if string.sub(entry_in, -2, -2) == "." then
										id_brg02 = tonumber(string.sub(entry_in, -5, -1))
										id_nav02 = string.sub(entry_in, aa+1, -6)
									else
										id_brg02 = tonumber(string.sub(entry_in, -3, -1))
										id_nav02 = string.sub(entry_in, aa+1, -4)
									end
									if id_brg02 == nil then
										ll = 1
									else
										-- /brg xxx
										if id_brg02 < 0 or id_brg02 > 360 then
											ll = 1
										else
											id_brg02 = tonumber(string.format("%5.1f", id_brg02))
											find_navaid(id_nav02, "", 0, "", in_rte)
											if navaid_list_n > 0 then
												cross_wpt = 1
											else
												ll = 1
											end
										end
									end
								else
									-- /dist x
									if id_dist < 0 or id_dist > 999 then
										ll = 1
									else
										id_dist = tonumber(string.format("%5.1f", id_dist))
									end
								end
							else
								ll = 1
							end
						end
					end
				else
					ll = 1
				end
			end
		else
			id_nav = legs_data2[item_select][1]
			kk = string.len(id_nav)
			if aa == kk + 1 then
				-- SEA/9 => allong waypoint
				along_dist = tonumber(string.sub(entry_in, aa+1, -1))	-- distance
				if along_dist == nil then
					ll = 1
				else
					along_dist = tonumber(string.format("%5.1f", along_dist))
					along_wpt = 1
				end
			elseif aa > 5 then
				-- SEA999/9 or SEA 999.9/9
				-- brg
				id_brg = tonumber(string.sub(entry_in, kk+1, aa-1))
				if id_brg == nil then
					ll = 1
				else
					if id_brg < 0 or id_brg > 360 then
						ll = 1
					else
						id_brg = tonumber(string.format("%5.1f", id_brg))
						--dist
						id_dist = tonumber(string.sub(entry_in, aa+1, -1))
						if id_dist == nil then
							-- id_brg02
							if string.sub(entry_in, -2, -2) == "." then
								id_brg02 = tonumber(string.sub(entry_in, -5, -1))
								id_nav02 = string.sub(entry_in, aa+1, -6)
							else
								id_brg02 = tonumber(string.sub(entry_in, -3, -1))
								id_nav02 = string.sub(entry_in, aa+1, -4)
							end
							if id_brg02 == nil then
								ll = 1
							else
								if id_brg02 < 0 or id_brg02 > 360 then
									ll = 1
								else
									id_brg02 = tonumber(string.format("%5.1f", id_brg02))
									find_navaid(id_nav02, "", 0, "", in_rte)
									if navaid_list_n > 0 then
										cross_wpt = 1
									else
										ll = 1
									end
								end
							end
						else
							if id_dist < 0 or id_dist > 999.9 then
								ll = 1
							else
								id_dist = tonumber(string.format("%5.1f", id_dist))
							end
						end
					end
				end
			end
		end
		
		if ll == 0 then
			-- add WPT
			if item_select == 0 then
				if along_wpt == 0 then
					find_navaid(id_nav, "", 0, "", in_rte)
				else
					if along_dist == 0 then
						navaid_list_n = -1
					elseif legs_data2[aaa][31] == "RF" or legs_data2[aaa][31] == "AF" then
						navaid_list_n = -1
					elseif along_dist < 0 and legs_data2[aaa][1] == id_nav then
						item_select = aaa
						navaid_list_n = 1
					elseif along_dist > 0 and legs_data2[aaa-1][1] == id_nav then
						item_select = aaa-1
						navaid_list_n = 1
					else
						navaid_list_n = -1
					end
				end
			else
				if along_wpt == 0 then
					navaid_list_n = 1
				else
					if along_dist == 0 then
						navaid_list_n = -1
					elseif legs_data2[item_select][31] == "RF" or legs_data2[item_select][31] == "AF" then
						navaid_list_n = -1
					elseif along_dist < 0 and legs_data2[item_select][1] == legs_data2[aaa][1] then
						navaid_list_n = 1
					elseif along_dist > 0 and legs_data2[item_select][1] == legs_data2[aaa-1][1] then
						navaid_list_n = 1
					else
						navaid_list_n = -1
					end
				end
			end
			
			if navaid_list_n == -1 then
				add_fmc_msg(INVALID_INPUT, 1)
				navaid_list_n = 0
			elseif navaid_list_n > 0 then
			
				if along_wpt ~= 0 then
					rte_copy(aaa, in_rte)
					add_disco = 0
				elseif legs_data2[aaa][1] == "DISCONTINUITY" then
					rte_copy(ii, in_rte)
					add_disco = 0
				else
					legs_data2[aaa][31] = "TF"
					rte_copy(aaa, in_rte)
					if aaa == jj then
						add_disco = 0
					else
						add_disco = 1
					end
				end
				
				if navaid_list_n == 1 then
				
					legs_num2 = aaa
					
					if item_select == 0 then
						nd_lat = navaid_list[1][2]
						nd_lon = navaid_list[1][3]
						wpt_ok = 1
					else
						nd_lat, nd_lon = lat_lon_legs2(item_select)
						if along_wpt == 0 then
							wpt_ok = 1
						end
					end
					
					lat_mem = nd_lat
					lon_mem = nd_lon
					
					if along_wpt == 1 then
						if along_dist > 0 then
							cc = legs_data2[item_select + 1][3]
							if cc > along_dist then
								wpt_ok = 1
								
								if item_select == offset then
									id_dist2 = along_dist
									if string.sub(legs_data2[item_select][31], 1, 1) == "C" or string.sub(legs_data2[item_select][31], 1, 1) == "V" then
										id_brg2 = (math.deg(legs_data2[item_select][18]) - simDR_mag_variation + 360) % 360
										nd_lat = legs_data2[item_select][7]
										nd_lon = legs_data2[item_select][8]
									else
										id_brg2 = math.deg(legs_data2[item_select][2])
									end
								else
									nd_lat, nd_lon = lat_lon_legs2(item_select)
									id_brg2 = math.deg(legs_data2[item_select + 1][2])
									id_dist2 = along_dist
								end
							end
						elseif along_dist < 0 then
							along_dist = -along_dist
							aa = item_select
							
							if aa >= offset then
								if aa == offset then
									nd_lat = simDR_latitude
									nd_lon = simDR_longitude
									dd = nd_calc_dist2(nd_lat, nd_lon, legs_data2[item_select][7], legs_data2[item_select][8])
								else
									nd_lat = legs_data2[item_select - 1][7]
									nd_lon = legs_data2[item_select - 1][8]
									dd = legs_data2[item_select][3]
								end
								if dd > along_dist then
									id_dist2 = dd - along_dist
									if aa == offset then
										if legs_intdir_act == 0 then
											if string.sub(legs_data2[item_select][31], 1, 1) == "C" or string.sub(legs_data2[item_select][31], 1, 1) == "V" then
												id_brg2 = (math.deg(legs_data2[item_select][18]) - simDR_mag_variation + 360) % 360
												id_brg2 = (id_brg2 + 180) % 360
												nd_lat = legs_data2[item_select][7]
												nd_lon = legs_data2[item_select][8]
												id_dist2 = along_dist
											else
												id_brg2 = math.deg(legs_data2[item_select][2])
											end
										else
											if legs_intdir_crs == -1 then
												id_brg2 = nd_calc_brg(nd_lat, nd_lon, legs_data2[item_select][7], legs_data2[item_select][8])
											else
												id_brg2 = legs_intdir_crs
												id_brg2 = (id_brg2 - simDR_mag_variation + 360) % 360
											end
											
										end
									else
										if string.sub(legs_data2[item_select][31], 1, 1) == "C" or string.sub(legs_data2[item_select][31], 1, 1) == "V" then
											id_brg2 = (math.deg(legs_data2[item_select][18]) - simDR_mag_variation + 360) % 360
											id_brg2 = (id_brg2 + 180) % 360
											nd_lat = legs_data2[item_select][7]
											nd_lon = legs_data2[item_select][8]
											id_dist2 = along_dist
										else
											id_brg2 = math.deg(legs_data2[item_select][2])
										end
									end
									wpt_ok = 1
								end
							end
							
						end
					end
					
					if wpt_ok == 1 then
						if cross_wpt == 0 then
							if along_wpt == 0 then
								temp_brg = (id_brg - mag_variation_deg(nd_lat, nd_lon) + 360) % 360
								nd_dis = id_dist --/ 3440.064795					-- distance NM
							else
								temp_brg = id_brg2
								nd_dis = id_dist2 --/ 3440.064795					-- distance NM
							end
							calc_brg_dist(nd_lat, nd_lon, math.rad(temp_brg), nd_dis)
							nd_lat2 = calc_lat
							nd_lon2 = calc_lon
						else
							find_navaid(id_nav02, "", 0, "", in_rte)
							if navaid_list_n > 0 then
								nd_lat2 = navaid_list[1][2]
								nd_lon2 = navaid_list[1][3]
								--brg_brg
								calc_brg_brg(nd_lat, nd_lon, math.rad(id_brg) - mag_variation_rad(math.rad(nd_lat), math.rad(nd_lon)), nd_lat2, nd_lon2, math.rad(id_brg02) - mag_variation_rad(math.rad(nd_lat2), math.rad(nd_lon2)))
								nd_lat2 = calc_lat
								nd_lon2 = calc_lon
							else
								nd_lat2 = nd_lat
								nd_lon2 = nd_lon
							end
						end
						
						id_nav_mem = id_nav
						
						if string.len(id_nav) > 2 then
							if string.sub(id_nav, 1, 2) == "RW" then
								id_nav = "WPT"
							else
								id_nav = string.sub(id_nav, 1, 3)
							end
						elseif string.len(id_nav) > 1 then
							id_nav = string.sub(id_nav, 1, 2)
						end
						
						nd_lat = string.len(id_nav)
						mm = 1
						if custom_wpt_num > 0 then
							for kk = 1, custom_wpt_num do
								if string.sub(custom_wpt[kk][1], 1, nd_lat) == id_nav then
									nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
									if nn ~= nil then
										if nn >= mm then
											mm = nn + 1
										end
									end
								end
							end
						end
						id_nav_txt = id_nav .. string.format("%02d", mm)
						
						-- add custom wpt
						custom_wpt_num = custom_wpt_num + 1
						custom_wpt[custom_wpt_num] = {}
						custom_wpt[custom_wpt_num][1] = id_nav_txt
						custom_wpt[custom_wpt_num][2] = nd_lat2
						custom_wpt[custom_wpt_num][3] = nd_lon2
						custom_wpt[custom_wpt_num][4] = id_nav_mem
						mm = (nd_calc_brg(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3]) + mag_variation_deg(lat_mem, lon_mem) + 360) % 360
						custom_wpt[custom_wpt_num][5] = string.format("%03d", mm)
						mm = nd_calc_dist(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3])
						custom_wpt[custom_wpt_num][6] = string.format("%3d", mm)
						
						legs_data2[legs_num2] = {}
						legs_data2[legs_num2][1] = id_nav_txt	--entry
						legs_data2[legs_num2][2] = 0		-- brg
						legs_data2[legs_num2][3] = 0		-- distance
						legs_data2[legs_num2][4] = 0		-- speed
						legs_data2[legs_num2][5] = 0		-- altitude
						legs_data2[legs_num2][6] = 0	-- altitude type
						legs_data2[legs_num2][7] = nd_lat2	--math.deg(nd_lat2)		-- latitude
						legs_data2[legs_num2][8] = nd_lon2	--math.deg(nd_lon2)		-- longitude
						legs_data2[legs_num2][9] = "DIRECT"			-- via id
						legs_data2[legs_num2][10] = 0		-- calc speed
						legs_data2[legs_num2][11] = 0		-- calc altitude
						legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
						legs_data2[legs_num2][13] = 0
						legs_data2[legs_num2][14] = 0		-- rest alt
						legs_data2[legs_num2][15] = 0		-- last fuel
						legs_data2[legs_num2][16] = "" --navaid_list[1][8]		-- reg code
						legs_data2[legs_num2][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
						legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
						legs_data2[legs_num2][20] = 0
						legs_data2[legs_num2][21] = -1
						legs_data2[legs_num2][22] = ""
						legs_data2[legs_num2][23] = 0
						legs_data2[legs_num2][24] = 0
						legs_data2[legs_num2][25] = 0
						legs_data2[legs_num2][26] = 0
						legs_data2[legs_num2][27] = ""
						legs_data2[legs_num2][28] = ""
						legs_data2[legs_num2][29] = ""
						legs_data2[legs_num2][30] = ""
						legs_data2[legs_num2][31] = ""
						legs_data2[legs_num2][32] = 0
						legs_data2[legs_num2][33] = ""
						legs_data2[legs_num2][34] = ""
						legs_data2[legs_num2][35] = ""
						legs_data2[legs_num2][36] = 4
						legs_data2[legs_num2][37] = 0
						legs_data2[legs_num2][38] = ""
						legs_data2[legs_num2][39] = ""
						legs_data2[legs_num2][40] = 0
						legs_data2[legs_num2][41] = 0
						legs_data2[legs_num2][42] = 0
						legs_data2[legs_num2][43] = ""
						legs_data2[legs_num2][44] = 0
						legs_data2[legs_num2][45] = 0
						legs_data2[legs_num2][46] = 0
						legs_data2[legs_num2][47] = 0
						legs_data2[legs_num2][48] = ""
						legs_data2[legs_num2][49] = 0
						legs_data2[legs_num2][50] = 0
						legs_data2[legs_num2][51] = 0
						legs_data2[legs_num2][52] = 0
						legs_data2[legs_num2][53] = 0
						legs_data2[legs_num2][54] = 0
						rte_lat = legs_data2[legs_num2][7]
						rte_lon = legs_data2[legs_num2][8]
						
						if add_disco == 1 then
							-- create discontinuity
							legs_num2 = legs_num2 + 1
							rte_add_disco(legs_num2, in_rte)
						end
						
						ii = legs_num2 + 1
						rte_paste(ii, in_rte)
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						calc_rte_enable2 = 1
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					if mode_in == 0 then
						page_sel_wpt3 = 1
						page_legs = 0
						act_page_old = act_page
						act_page = 1
					else
						page_sel_wpt3_2 = 1
						page_legs2 = 0
						act_page_old2 = act_page2
						act_page2 = 1
					end
					wpt_id_paste = aaa
					wpt_id_nav_tmp = id_nav
					if id_brg == nil then
						wpt_id_brg_tmp = ""
					else
						wpt_id_brg_tmp = string.format("%5.1f", id_brg)
					end
					if id_dist == nil then
						wpt_id_dist_tmp = ""
					else
						wpt_id_dist_tmp = string.format("%5.1f", id_dist)
					end
					wpt_id_nav_tmp02 = id_nav02
					if id_brg02 == nil then
						wpt_id_brg_tmp02 = ""
					else
						wpt_id_brg_tmp02 = string.format("%5.1f", id_brg02)
					end
				end
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
			else
				add_fmc_msg(NOT_IN_DATABASE, 1)
			end
		else
			add_fmc_msg(INVALID_INPUT, 1)
		end
	elseif in_rte == 1 then
		jj = legs_num8 + 1
		aa,bb = string.find(entry_in, "/")
		if aa == nil then
			-- without "/"
			ll = 1
		elseif aa < 3 then
			-- min "XX/"
			ll = 1
		elseif aa == entry_len then
			-- last char "/"
			ll = 1
		elseif item_select == 0 then
			-- find id_nav
			id_nav = string.sub(entry_in, 1, aa-1)
			find_navaid(id_nav, "", 0, "", in_rte)
			if navaid_list_n > 0 then
				-- SEA/9, NEMUS/9 => allong waypoint
				along_dist = tonumber(string.sub(entry_in, aa+1, -1))	-- distance
				if along_dist == nil then
					ll = 1
				else
					along_dist = tonumber(string.format("%5.1f", along_dist))
					along_wpt = 1
				end
			else
				if aa > 5 then
					-- brg
					if string.sub(entry_in, aa-2, aa-2) == "." then
						id_brg = tonumber(string.sub(entry_in, aa-5, aa-1))
						if aa > 6 then
							id_nav = string.sub(entry_in, 1, aa-6)
						else
							id_nav = string.sub(entry_in, 1, aa-5)
						end
					else
						id_brg = tonumber(string.sub(entry_in, aa-3, aa-1))
						id_nav = string.sub(entry_in, 1, aa-4)
					end
					if id_brg == nil then
						ll = 1
					else
						--999/9 or 999.9/9
						if id_brg < 0 or id_brg > 360 then
							ll = 1
						else
							find_navaid(id_nav, "", 0, "", in_rte)
							if navaid_list_n > 0 then
								id_brg = tonumber(string.format("%5.1f", id_brg))
								--dist
								id_dist = tonumber(string.sub(entry_in, aa+1, -1))
								if id_dist == nil then
									-- id_brg02
									if string.sub(entry_in, -2, -2) == "." then
										id_brg02 = tonumber(string.sub(entry_in, -5, -1))
										id_nav02 = string.sub(entry_in, aa+1, -6)
									else
										id_brg02 = tonumber(string.sub(entry_in, -3, -1))
										id_nav02 = string.sub(entry_in, aa+1, -4)
									end
									if id_brg02 == nil then
										ll = 1
									else
										-- /brg xxx
										if id_brg02 < 0 or id_brg02 > 360 then
											ll = 1
										else
											id_brg02 = tonumber(string.format("%5.1f", id_brg02))
											find_navaid(id_nav02, "", 0, "", in_rte)
											if navaid_list_n > 0 then
												cross_wpt = 1
											else
												ll = 1
											end
										end
									end
								else
									-- /dist x
									if id_dist < 0 or id_dist > 999 then
										ll = 1
									else
										id_dist = tonumber(string.format("%5.1f", id_dist))
									end
								end
							else
								ll = 1
							end
						end
					end
				else
					ll = 1
				end
			end
		else
			id_nav = legs_data8[item_select][1]
			kk = string.len(id_nav)
			if aa == kk + 1 then
				-- SEA/9 => allong waypoint
				along_dist = tonumber(string.sub(entry_in, aa+1, -1))	-- distance
				if along_dist == nil then
					ll = 1
				else
					along_dist = tonumber(string.format("%5.1f", along_dist))
					along_wpt = 1
				end
			elseif aa > 5 then
				-- SEA999/9 or SEA 999.9/9
				-- brg
				id_brg = tonumber(string.sub(entry_in, kk+1, aa-1))
				if id_brg == nil then
					ll = 1
				else
					if id_brg < 0 or id_brg > 360 then
						ll = 1
					else
						id_brg = tonumber(string.format("%5.1f", id_brg))
						--dist
						id_dist = tonumber(string.sub(entry_in, aa+1, -1))
						if id_dist == nil then
							-- id_brg02
							if string.sub(entry_in, -2, -2) == "." then
								id_brg02 = tonumber(string.sub(entry_in, -5, -1))
								id_nav02 = string.sub(entry_in, aa+1, -6)
							else
								id_brg02 = tonumber(string.sub(entry_in, -3, -1))
								id_nav02 = string.sub(entry_in, aa+1, -4)
							end
							if id_brg02 == nil then
								ll = 1
							else
								if id_brg02 < 0 or id_brg02 > 360 then
									ll = 1
								else
									id_brg02 = tonumber(string.format("%5.1f", id_brg02))
									find_navaid(id_nav02, "", 0, "", in_rte)
									if navaid_list_n > 0 then
										cross_wpt = 1
									else
										ll = 1
									end
								end
							end
						else
							if id_dist < 0 or id_dist > 999.9 then
								ll = 1
							else
								id_dist = tonumber(string.format("%5.1f", id_dist))
							end
						end
					end
				end
			end
		end
		
		if ll == 0 then
			-- add WPT
			if item_select == 0 then
				if along_wpt == 0 then
					find_navaid(id_nav, "", 0, "", in_rte)
				else
					if along_dist == 0 then
						navaid_list_n = -1
					elseif legs_data8[aaa][31] == "RF" or legs_data8[aaa][31] == "AF" then
						navaid_list_n = -1
					elseif along_dist < 0 and legs_data8[aaa][1] == id_nav then
						item_select = aaa
						navaid_list_n = 1
					elseif along_dist > 0 and legs_data8[aaa-1][1] == id_nav then
						item_select = aaa-1
						navaid_list_n = 1
					else
						navaid_list_n = -1
					end
				end
			else
				if along_wpt == 0 then
					navaid_list_n = 1
				else
					if along_dist == 0 then
						navaid_list_n = -1
					elseif legs_data8[item_select][31] == "RF" or legs_data8[item_select][31] == "AF" then
						navaid_list_n = -1
					elseif along_dist < 0 and legs_data8[item_select][1] == legs_data8[aaa][1] then
						navaid_list_n = 1
					elseif along_dist > 0 and legs_data8[item_select][1] == legs_data8[aaa-1][1] then
						navaid_list_n = 1
					else
						navaid_list_n = -1
					end
				end
			end
			
			if navaid_list_n == -1 then
				add_fmc_msg(INVALID_INPUT, 1)
				navaid_list_n = 0
			elseif navaid_list_n > 0 then
			
				if along_wpt ~= 0 then
					rte_copy(aaa, in_rte)
					add_disco = 0
				elseif legs_data8[aaa][1] == "DISCONTINUITY" then
					rte_copy(ii, in_rte)
					add_disco = 0
				else
					legs_data8[aaa][31] = "TF"
					rte_copy(aaa, in_rte)
					if aaa == jj then
						add_disco = 0
					else
						add_disco = 1
					end
				end
				
				if navaid_list_n == 1 then
				
					legs_num8 = aaa
					
					if item_select == 0 then
						nd_lat = navaid_list[1][2]
						nd_lon = navaid_list[1][3]
						wpt_ok = 1
					else
						nd_lat, nd_lon = lat_lon_legs8(item_select)
						if along_wpt == 0 then
							wpt_ok = 1
						end
					end
					
					lat_mem = nd_lat
					lon_mem = nd_lon
					
					if along_wpt == 1 then
						if along_dist > 0 then
							cc = legs_data8[item_select + 1][3]
							if cc > along_dist then
								wpt_ok = 1
								
								if item_select == offset then
									id_dist2 = along_dist
									if string.sub(legs_data8[item_select][31], 1, 1) == "C" or string.sub(legs_data8[item_select][31], 1, 1) == "V" then
										id_brg2 = (math.deg(legs_data8[item_select][18]) - simDR_mag_variation + 360) % 360
										nd_lat = legs_data8[item_select][7]
										nd_lon = legs_data8[item_select][8]
									else
										id_brg2 = math.deg(legs_data8[item_select][2])
									end
								else
									nd_lat, nd_lon = lat_lon_legs8(item_select)
									id_brg2 = math.deg(legs_data8[item_select + 1][2])
									id_dist2 = along_dist
								end
							end
						elseif along_dist < 0 then
							along_dist = -along_dist
							aa = item_select
							
							if aa >= offset then
								if aa == offset then
									nd_lat = simDR_latitude
									nd_lon = simDR_longitude
									dd = nd_calc_dist2(nd_lat, nd_lon, legs_data8[item_select][7], legs_data8[item_select][8])
								else
									nd_lat = legs_data8[item_select - 1][7]
									nd_lon = legs_data8[item_select - 1][8]
									dd = legs_data8[item_select][3]
								end
								if dd > along_dist then
									id_dist2 = dd - along_dist
									if aa == offset then
										if legs_intdir_act == 0 then
											if string.sub(legs_data8[item_select][31], 1, 1) == "C" or string.sub(legs_data8[item_select][31], 1, 1) == "V" then
												id_brg2 = (math.deg(legs_data8[item_select][18]) - simDR_mag_variation + 360) % 360
												id_brg2 = (id_brg2 + 180) % 360
												nd_lat = legs_data8[item_select][7]
												nd_lon = legs_data8[item_select][8]
												id_dist2 = along_dist
											else
												id_brg2 = math.deg(legs_data8[item_select][2])
											end
										else
											if legs_intdir_crs == -1 then
												id_brg2 = nd_calc_brg(nd_lat, nd_lon, legs_data8[item_select][7], legs_data8[item_select][8])
											else
												id_brg2 = legs_intdir_crs
												id_brg2 = (id_brg2 - simDR_mag_variation + 360) % 360
											end
											
										end
									else
										if string.sub(legs_data8[item_select][31], 1, 1) == "C" or string.sub(legs_data8[item_select][31], 1, 1) == "V" then
											id_brg2 = (math.deg(legs_data8[item_select][18]) - simDR_mag_variation + 360) % 360
											id_brg2 = (id_brg2 + 180) % 360
											nd_lat = legs_data8[item_select][7]
											nd_lon = legs_data8[item_select][8]
											id_dist2 = along_dist
										else
											id_brg2 = math.deg(legs_data8[item_select][2])
										end
									end
									wpt_ok = 1
								end
							end
							
						end
					end
					
					if wpt_ok == 1 then
						if cross_wpt == 0 then
							if along_wpt == 0 then
								temp_brg = (id_brg - mag_variation_deg(nd_lat, nd_lon) + 360) % 360
								nd_dis = id_dist --/ 3440.064795					-- distance NM
							else
								temp_brg = id_brg2
								nd_dis = id_dist2 --/ 3440.064795					-- distance NM
							end
							calc_brg_dist(nd_lat, nd_lon, math.rad(temp_brg), nd_dis)
							nd_lat2 = calc_lat
							nd_lon2 = calc_lon
						else
							find_navaid(id_nav02, "", 0, "", in_rte)
							if navaid_list_n > 0 then
								nd_lat2 = navaid_list[1][2]
								nd_lon2 = navaid_list[1][3]
								--brg_brg
								calc_brg_brg(nd_lat, nd_lon, math.rad(id_brg) - mag_variation_rad(math.rad(nd_lat), math.rad(nd_lon)), nd_lat2, nd_lon2, math.rad(id_brg02) - mag_variation_rad(math.rad(nd_lat2), math.rad(nd_lon2)))
								nd_lat2 = calc_lat
								nd_lon2 = calc_lon
							else
								nd_lat2 = nd_lat
								nd_lon2 = nd_lon
							end
						end
						
						id_nav_mem = id_nav
						
						if string.len(id_nav) > 2 then
							if string.sub(id_nav, 1, 2) == "RW" then
								id_nav = "WPT"
							else
								id_nav = string.sub(id_nav, 1, 3)
							end
						elseif string.len(id_nav) > 1 then
							id_nav = string.sub(id_nav, 1, 2)
						end
						
						nd_lat = string.len(id_nav)
						mm = 1
						if custom_wpt_num > 0 then
							for kk = 1, custom_wpt_num do
								if string.sub(custom_wpt[kk][1], 1, nd_lat) == id_nav then
									nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
									if nn ~= nil then
										if nn >= mm then
											mm = nn + 1
										end
									end
								end
							end
						end
						id_nav_txt = id_nav .. string.format("%02d", mm)
						
						-- add custom wpt
						custom_wpt_num = custom_wpt_num + 1
						custom_wpt[custom_wpt_num] = {}
						custom_wpt[custom_wpt_num][1] = id_nav_txt
						custom_wpt[custom_wpt_num][2] = nd_lat2
						custom_wpt[custom_wpt_num][3] = nd_lon2
						custom_wpt[custom_wpt_num][4] = id_nav_mem
						mm = (nd_calc_brg(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3]) + mag_variation_deg(lat_mem, lon_mem) + 360) % 360
						custom_wpt[custom_wpt_num][5] = string.format("%03d", mm)
						mm = nd_calc_dist(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3])
						custom_wpt[custom_wpt_num][6] = string.format("%3d", mm)
						
						legs_data8[legs_num8] = {}
						legs_data8[legs_num8][1] = id_nav_txt	--entry
						legs_data8[legs_num8][2] = 0		-- brg
						legs_data8[legs_num8][3] = 0		-- distance
						legs_data8[legs_num8][4] = 0		-- speed
						legs_data8[legs_num8][5] = 0		-- altitude
						legs_data8[legs_num8][6] = 0	-- altitude type
						legs_data8[legs_num8][7] = nd_lat2	--math.deg(nd_lat2)		-- latitude
						legs_data8[legs_num8][8] = nd_lon2	--math.deg(nd_lon2)		-- longitude
						legs_data8[legs_num8][9] = "DIRECT"			-- via id
						legs_data8[legs_num8][10] = 0		-- calc speed
						legs_data8[legs_num8][11] = 0		-- calc altitude
						legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
						legs_data8[legs_num8][13] = 0
						legs_data8[legs_num8][14] = 0		-- rest alt
						legs_data8[legs_num8][15] = 0		-- last fuel
						legs_data8[legs_num8][16] = "" --navaid_list[1][8]		-- reg code
						legs_data8[legs_num8][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
						legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
						legs_data8[legs_num8][20] = 0
						legs_data8[legs_num8][21] = -1
						legs_data8[legs_num8][22] = ""
						legs_data8[legs_num8][23] = 0
						legs_data8[legs_num8][24] = 0
						legs_data8[legs_num8][25] = 0
						legs_data8[legs_num8][26] = 0
						legs_data8[legs_num8][27] = ""
						legs_data8[legs_num8][28] = ""
						legs_data8[legs_num8][29] = ""
						legs_data8[legs_num8][30] = ""
						legs_data8[legs_num8][31] = ""
						legs_data8[legs_num8][32] = 0
						legs_data8[legs_num8][33] = ""
						legs_data8[legs_num8][34] = ""
						legs_data8[legs_num8][35] = ""
						legs_data8[legs_num8][36] = 4
						legs_data8[legs_num8][37] = 0
						legs_data8[legs_num8][38] = ""
						legs_data8[legs_num8][39] = ""
						legs_data8[legs_num8][40] = 0
						legs_data8[legs_num8][41] = 0
						legs_data8[legs_num8][42] = 0
						legs_data8[legs_num8][43] = ""
						legs_data8[legs_num8][44] = 0
						legs_data8[legs_num8][45] = 0
						legs_data8[legs_num8][46] = 0
						legs_data8[legs_num8][47] = 0
						legs_data8[legs_num8][48] = ""
						legs_data8[legs_num8][49] = 0
						legs_data8[legs_num8][50] = 0
						legs_data8[legs_num8][51] = 0
						legs_data8[legs_num8][52] = 0
						legs_data8[legs_num8][53] = 0
						legs_data8[legs_num8][54] = 0
						rte_lat = legs_data8[legs_num8][7]
						rte_lon = legs_data8[legs_num8][8]
						
						if add_disco == 1 then
							-- create discontinuity
							legs_num8 = legs_num8 + 1
							rte_add_disco(legs_num8, in_rte)
						end
						
						ii = legs_num8 + 1
						rte_paste(ii, in_rte)
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						calc_rte_enable8 = 1
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					if mode_in == 0 then
						page_sel_wpt3 = 1
						page_legs = 0
						act_page_old = act_page
						act_page = 1
					else
						page_sel_wpt3_2 = 1
						page_legs2 = 0
						act_page_old2 = act_page2
						act_page2 = 1
					end
					wpt_id_paste = aaa
					wpt_id_nav_tmp = id_nav
					if id_brg == nil then
						wpt_id_brg_tmp = ""
					else
						wpt_id_brg_tmp = string.format("%5.1f", id_brg)
					end
					if id_dist == nil then
						wpt_id_dist_tmp = ""
					else
						wpt_id_dist_tmp = string.format("%5.1f", id_dist)
					end
					wpt_id_nav_tmp02 = id_nav02
					if id_brg02 == nil then
						wpt_id_brg_tmp02 = ""
					else
						wpt_id_brg_tmp02 = string.format("%5.1f", id_brg02)
					end
				end
				
				if mode_in == 0 then
					entry = ""
				else
					entry2 = ""
				end
			else
				add_fmc_msg(NOT_IN_DATABASE, 1)
			end
		else
			add_fmc_msg(INVALID_INPUT, 1)
		end
	end
end

function rte_add_wpt4(wpt_idx, mode_in, in_rte)
	
	local kk = 0
	local mm = 0
	local nn = 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local temp_brg = 0
	local nd_dis = 0
	local id_nav_txt = ""
	
	local brg_tmp = 0
	local brg_tmp02 = 0
	
	local nav_list01 = ""
	local nav_list08 = ""
	
	local id_nav_mem = ""
	local lat_mem = 0
	local lon_mem = 0
	
	if in_rte == 0 then
		if wpt_idx <= navaid_list_n and wpt_idx ~= 0 then
				
				nd_lat = navaid_list[wpt_idx][2]
				nd_lon = navaid_list[wpt_idx][3]
				nav_list01 = navaid_list[wpt_idx][1]
				nav_list08 = navaid_list[wpt_idx][8]
				if wpt_id_nav_tmp02 == "" then
					temp_brg = (tonumber(wpt_id_brg_tmp) - mag_variation_deg(nd_lat, nd_lon) + 360) % 360
					nd_dis = tonumber(wpt_id_dist_tmp) --/ 3440.064795					-- distance NM
					calc_brg_dist(nd_lat, nd_lon, math.rad(temp_brg), nd_dis)
					nd_lat2 = calc_lat
					nd_lon2 = calc_lon
				else
					find_navaid(wpt_id_nav_tmp02, "", 0, "", in_rte)
					if navaid_list_n > 0 then
						nd_lat2 = navaid_list[1][2]
						nd_lon2 = navaid_list[1][3]
						--brg_brg
						brg_tmp = math.rad(tonumber(wpt_id_brg_tmp)) - mag_variation_rad(math.rad(nd_lat), math.rad(nd_lon))
						brg_tmp02 = math.rad(tonumber(wpt_id_brg_tmp02)) - mag_variation_rad(math.rad(nd_lat2), math.rad(nd_lon2))
						calc_brg_brg(nd_lat, nd_lon, brg_tmp, nd_lat2, nd_lon2, brg_tmp02)
						nd_lat2 = calc_lat
						nd_lon2 = calc_lon
					else
						nd_lat2 = nd_lat
						nd_lon2 = nd_lon
					end
				end
				
				id_nav_mem = wpt_id_nav_tmp
				lat_mem = nd_lat
				lon_mem = nd_lon
				
				if string.len(wpt_id_nav_tmp) > 2 then
					if string.sub(wpt_id_nav_tmp, 1, 2) == "RW" then
						wpt_id_nav_tmp = "WPT"
					else
						wpt_id_nav_tmp = string.sub(wpt_id_nav_tmp, 1, 3)
					end
				elseif string.len(wpt_id_nav_tmp) > 1 then
					wpt_id_nav_tmp = string.sub(wpt_id_nav_tmp, 1, 2)
				end
				
				nd_lat = string.len(wpt_id_nav_tmp)
				mm = 1
				if custom_wpt_num > 0 then
					for kk = 1, custom_wpt_num do
						if string.sub(custom_wpt[kk][1], 1, nd_lat) == wpt_id_nav_tmp then
							nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
							if nn ~= nil then
								if nn >= mm then
									mm = nn + 1
								end
							end
						end
					end
				end
				id_nav_txt = wpt_id_nav_tmp .. string.format("%02d", mm)
				
				-- add custom wpt
				custom_wpt_num = custom_wpt_num + 1
				custom_wpt[custom_wpt_num] = {}
				custom_wpt[custom_wpt_num][1] = id_nav_txt
				custom_wpt[custom_wpt_num][2] = nd_lat2
				custom_wpt[custom_wpt_num][3] = nd_lon2
				custom_wpt[custom_wpt_num][4] = id_nav_mem
				mm = (nd_calc_brg(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3]) + mag_variation_deg(lat_mem, lon_mem) + 360) % 360
				custom_wpt[custom_wpt_num][5] = string.format("%03d", mm)
				mm = nd_calc_dist(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3])
				custom_wpt[custom_wpt_num][6] = string.format("%3d", mm)
				
				legs_num2 = wpt_id_paste
				
				legs_data2[legs_num2] = {}
				legs_data2[legs_num2][1] = id_nav_txt	--entry
				legs_data2[legs_num2][2] = 0		-- brg
				legs_data2[legs_num2][3] = 0		-- distance
				legs_data2[legs_num2][4] = 0		-- speed
				legs_data2[legs_num2][5] = 0		-- altitude
				legs_data2[legs_num2][6] = 0	-- altitude type
				legs_data2[legs_num2][7] = nd_lat2	--math.deg(nd_lat2)		-- latitude
				legs_data2[legs_num2][8] = nd_lon2	--math.deg(nd_lon2)		-- longitude
				legs_data2[legs_num2][9] = "DIRECT"			-- via id
				legs_data2[legs_num2][10] = 0		-- calc speed
				legs_data2[legs_num2][11] = 0		-- calc altitude
				legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
				legs_data2[legs_num2][13] = 0
				legs_data2[legs_num2][14] = 0		-- rest alt
				legs_data2[legs_num2][15] = 0		-- last fuel
				legs_data2[legs_num2][16] = nav_list08		-- reg code
				legs_data2[legs_num2][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
				legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data2[legs_num2][20] = 0
				legs_data2[legs_num2][21] = -1
				legs_data2[legs_num2][22] = ""
				legs_data2[legs_num2][23] = 0
				legs_data2[legs_num2][24] = 0
				legs_data2[legs_num2][25] = 0
				legs_data2[legs_num2][26] = 0
				legs_data2[legs_num2][27] = ""
				legs_data2[legs_num2][28] = ""
				legs_data2[legs_num2][29] = ""
				legs_data2[legs_num2][30] = ""
				legs_data2[legs_num2][31] = ""
				legs_data2[legs_num2][32] = 0
				legs_data2[legs_num2][33] = ""
				legs_data2[legs_num2][34] = ""
				legs_data2[legs_num2][35] = ""
				legs_data2[legs_num2][36] = nav_list01
				legs_data2[legs_num2][37] = 0
				legs_data2[legs_num2][38] = ""
				legs_data2[legs_num2][39] = ""
				legs_data2[legs_num2][40] = 0
				legs_data2[legs_num2][41] = 0
				legs_data2[legs_num2][42] = 0
				legs_data2[legs_num2][43] = ""
				legs_data2[legs_num2][44] = 0
				legs_data2[legs_num2][45] = 0
				legs_data2[legs_num2][46] = 0
				legs_data2[legs_num2][47] = 0
				legs_data2[legs_num2][48] = ""
				legs_data2[legs_num2][49] = 0
				legs_data2[legs_num2][50] = 0
				legs_data2[legs_num2][51] = 0
				legs_data2[legs_num2][52] = 0
				legs_data2[legs_num2][53] = 0
				legs_data2[legs_num2][54] = 0
				rte_lat = legs_data2[legs_num2][7]
				rte_lon = legs_data2[legs_num2][8]
				
				if add_disco == 1 then
					-- create discontinuity
					legs_num2 = legs_num2 + 1
					rte_add_disco(legs_num2, in_rte)
				end
				
				rte_paste(legs_num2 + 1, in_rte)
				calc_rte_enable2 = 1
		end
		if mode_in == 0 then
			page_sel_wpt3 = 0
			page_legs = 1
			act_page = act_page_old
			max_page = math.max(max_page, act_page)
		else
			page_sel_wpt3_2 = 0
			page_legs2 = 1
			act_page2 = act_page_old2
			max_page2 = math.max(max_page2, act_page2)
		end
	elseif in_rte == 1 then
		if wpt_idx <= navaid_list_n and wpt_idx ~= 0 then
				
				nd_lat = navaid_list[wpt_idx][2]
				nd_lon = navaid_list[wpt_idx][3]
				nav_list01 = navaid_list[wpt_idx][1]
				nav_list08 = navaid_list[wpt_idx][8]
				if wpt_id_nav_tmp02 == "" then
					temp_brg = (tonumber(wpt_id_brg_tmp) - mag_variation_deg(nd_lat, nd_lon) + 360) % 360
					nd_dis = tonumber(wpt_id_dist_tmp) --/ 3440.064795					-- distance NM
					calc_brg_dist(nd_lat, nd_lon, math.rad(temp_brg), nd_dis)
					nd_lat2 = calc_lat
					nd_lon2 = calc_lon
				else
					find_navaid(wpt_id_nav_tmp02, "", 0, "", in_rte)
					if navaid_list_n > 0 then
						nd_lat2 = navaid_list[1][2]
						nd_lon2 = navaid_list[1][3]
						--brg_brg
						brg_tmp = math.rad(tonumber(wpt_id_brg_tmp)) - mag_variation_rad(math.rad(nd_lat), math.rad(nd_lon))
						brg_tmp02 = math.rad(tonumber(wpt_id_brg_tmp02)) - mag_variation_rad(math.rad(nd_lat2), math.rad(nd_lon2))
						calc_brg_brg(nd_lat, nd_lon, brg_tmp, nd_lat2, nd_lon2, brg_tmp02)
						nd_lat2 = calc_lat
						nd_lon2 = calc_lon
					else
						nd_lat2 = nd_lat
						nd_lon2 = nd_lon
					end
				end
				
				id_nav_mem = wpt_id_nav_tmp
				lat_mem = nd_lat
				lon_mem = nd_lon
				
				if string.len(wpt_id_nav_tmp) > 2 then
					if string.sub(wpt_id_nav_tmp, 1, 2) == "RW" then
						wpt_id_nav_tmp = "WPT"
					else
						wpt_id_nav_tmp = string.sub(wpt_id_nav_tmp, 1, 3)
					end
				elseif string.len(wpt_id_nav_tmp) > 1 then
					wpt_id_nav_tmp = string.sub(wpt_id_nav_tmp, 1, 2)
				end
				
				nd_lat = string.len(wpt_id_nav_tmp)
				mm = 1
				if custom_wpt_num > 0 then
					for kk = 1, custom_wpt_num do
						if string.sub(custom_wpt[kk][1], 1, nd_lat) == wpt_id_nav_tmp then
							nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
							if nn ~= nil then
								if nn >= mm then
									mm = nn + 1
								end
							end
						end
					end
				end
				id_nav_txt = wpt_id_nav_tmp .. string.format("%02d", mm)
				
				-- add custom wpt
				custom_wpt_num = custom_wpt_num + 1
				custom_wpt[custom_wpt_num] = {}
				custom_wpt[custom_wpt_num][1] = id_nav_txt
				custom_wpt[custom_wpt_num][2] = nd_lat2
				custom_wpt[custom_wpt_num][3] = nd_lon2
				custom_wpt[custom_wpt_num][4] = id_nav_mem
				mm = (nd_calc_brg(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3]) + mag_variation_deg(lat_mem, lon_mem) + 360) % 360
				custom_wpt[custom_wpt_num][5] = string.format("%03d", mm)
				mm = nd_calc_dist(lat_mem, lon_mem, custom_wpt[custom_wpt_num][2], custom_wpt[custom_wpt_num][3])
				custom_wpt[custom_wpt_num][6] = string.format("%3d", mm)
				
				legs_num8 = wpt_id_paste
				
				legs_data8[legs_num8] = {}
				legs_data8[legs_num8][1] = id_nav_txt	--entry
				legs_data8[legs_num8][2] = 0		-- brg
				legs_data8[legs_num8][3] = 0		-- distance
				legs_data8[legs_num8][4] = 0		-- speed
				legs_data8[legs_num8][5] = 0		-- altitude
				legs_data8[legs_num8][6] = 0	-- altitude type
				legs_data8[legs_num8][7] = nd_lat2	--math.deg(nd_lat2)		-- latitude
				legs_data8[legs_num8][8] = nd_lon2	--math.deg(nd_lon2)		-- longitude
				legs_data8[legs_num8][9] = "DIRECT"			-- via id
				legs_data8[legs_num8][10] = 0		-- calc speed
				legs_data8[legs_num8][11] = 0		-- calc altitude
				legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
				legs_data8[legs_num8][13] = 0
				legs_data8[legs_num8][14] = 0		-- rest alt
				legs_data8[legs_num8][15] = 0		-- last fuel
				legs_data8[legs_num8][16] = nav_list08		-- reg code
				legs_data8[legs_num8][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
				legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
				legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
				legs_data8[legs_num8][20] = 0
				legs_data8[legs_num8][21] = -1
				legs_data8[legs_num8][22] = ""
				legs_data8[legs_num8][23] = 0
				legs_data8[legs_num8][24] = 0
				legs_data8[legs_num8][25] = 0
				legs_data8[legs_num8][26] = 0
				legs_data8[legs_num8][27] = ""
				legs_data8[legs_num8][28] = ""
				legs_data8[legs_num8][29] = ""
				legs_data8[legs_num8][30] = ""
				legs_data8[legs_num8][31] = ""
				legs_data8[legs_num8][32] = 0
				legs_data8[legs_num8][33] = ""
				legs_data8[legs_num8][34] = ""
				legs_data8[legs_num8][35] = ""
				legs_data8[legs_num8][36] = nav_list01
				legs_data8[legs_num8][37] = 0
				legs_data8[legs_num8][38] = ""
				legs_data8[legs_num8][39] = ""
				legs_data8[legs_num8][40] = 0
				legs_data8[legs_num8][41] = 0
				legs_data8[legs_num8][42] = 0
				legs_data8[legs_num8][43] = ""
				legs_data8[legs_num8][44] = 0
				legs_data8[legs_num8][45] = 0
				legs_data8[legs_num8][46] = 0
				legs_data8[legs_num8][47] = 0
				legs_data8[legs_num8][48] = ""
				legs_data8[legs_num8][49] = 0
				legs_data8[legs_num8][50] = 0
				legs_data8[legs_num8][51] = 0
				legs_data8[legs_num8][52] = 0
				legs_data8[legs_num8][53] = 0
				legs_data8[legs_num8][54] = 0
				rte_lat = legs_data8[legs_num8][7]
				rte_lon = legs_data8[legs_num8][8]
				
				if add_disco == 1 then
					-- create discontinuity
					legs_num8 = legs_num8 + 1
					rte_add_disco(legs_num8, in_rte)
				end
				
				rte_paste(legs_num8 + 1, in_rte)
				calc_rte_enable8 = 1
		end
		if mode_in == 0 then
			page_sel_wpt3 = 0
			page_legs = 1
			act_page = act_page_old
			max_page = math.max(max_page, act_page)
		else
			page_sel_wpt3_2 = 0
			page_legs2 = 1
			act_page2 = act_page_old2
			max_page2 = math.max(max_page2, act_page2)
		end
	end
end

function rte_del_wpt(in_item, in_mode, in_rte)
	
	local tmp_tmp_2 = 0
	local invalid_del_msg = false
	
	if in_rte == 0 then
		if in_item == legs_num2 then
			rte_copy(legs_num2 + 1, in_rte)
			rte_paste(legs_num2, in_rte)
		elseif in_item < legs_num2 then
			if legs_data2[in_item][1] == "DISCONTINUITY" then
				invalid_del_msg = true
			elseif string.sub(legs_data2[in_item][1], 1, 1) == "(" then
				invalid_del_msg = true
			elseif string.sub(legs_data2[in_item - 1][1], 1, 1) == "(" then
				invalid_del_msg = true
			elseif legs_data2[in_item+1][1] == "DISCONTINUITY" then
				if in_item > 0 then
					if legs_data2[in_item-1][1] == "DISCONTINUITY" then
						rte_copy(in_item + 2, in_rte)
						rte_paste(in_item, in_rte)
						legs_data2[in_item+2][31] = "TF"
					else
						rte_copy(in_item + 1, in_rte)
						rte_paste(in_item, in_rte)
						legs_data2[in_item+1][31] = "TF"
					end
				else
					rte_copy(in_item + 1, in_rte)
					rte_paste(in_item, in_rte)
				end
			elseif legs_data2[in_item+1][31] == "HM" or legs_data2[in_item+1][31] == "HA" or legs_data2[in_item+1][31] == "HF" then
				rte_copy(in_item + 2, in_rte)
				rte_paste(in_item, in_rte)
			else
				legs_data2[in_item+1][31] = "TF"
				if in_item > 0 then
					if legs_data2[in_item-1][1] == "DISCONTINUITY" then
						rte_copy(in_item + 1, in_rte)
						rte_paste(in_item, in_rte)
					else
						tmp_tmp_2 = legs_num2
						rte_add_disco(in_item, in_rte)
						legs_num2 = tmp_tmp_2
					end
				else
					tmp_tmp_2 = legs_num2
					rte_add_disco(in_item, in_rte)
					legs_num2 = tmp_tmp_2
				end
			end
		end
		
		if invalid_del_msg == true then
			if in_mode == 0 then
				entry = INVALID_DELETE
			else
				entry2 = INVALID_DELETE
			end
		else
			calc_rte_enable2 = 1
			if rte12_act == in_rte then
				legs_delete = 1
			end
			fms_recalc = 1
			msg_unavaible_crz_alt = 0
		end
	elseif in_rte == 1 then
		if in_item == legs_num8 then
			rte_copy(legs_num8 + 1, in_rte)
			rte_paste(legs_num8, in_rte)
		elseif in_item < legs_num8 then
			if legs_data8[in_item][1] == "DISCONTINUITY" then
				invalid_del_msg = true
			elseif string.sub(legs_data8[in_item][1], 1, 1) == "(" then
				invalid_del_msg = true
			elseif string.sub(legs_data8[in_item - 1][1], 1, 1) == "(" then
				invalid_del_msg = true
			elseif legs_data8[in_item+1][1] == "DISCONTINUITY" then
				if in_item > 0 then
					if legs_data8[in_item-1][1] == "DISCONTINUITY" then
						rte_copy(in_item + 2, in_rte)
						rte_paste(in_item, in_rte)
						legs_data8[in_item+2][31] = "TF"
					else
						rte_copy(in_item + 1, in_rte)
						rte_paste(in_item, in_rte)
						legs_data8[in_item+1][31] = "TF"
					end
				else
					rte_copy(in_item + 1, in_rte)
					rte_paste(in_item, in_rte)
				end
			elseif legs_data8[in_item+1][31] == "HM" or legs_data8[in_item+1][31] == "HA" or legs_data8[in_item+1][31] == "HF" then
				rte_copy(in_item + 2, in_rte)
				rte_paste(in_item, in_rte)
			else
				legs_data8[in_item+1][31] = "TF"
				if in_item > 0 then
					if legs_data8[in_item-1][1] == "DISCONTINUITY" then
						rte_copy(in_item + 1, in_rte)
						rte_paste(in_item, in_rte)
					else
						tmp_tmp_2 = legs_num8
						rte_add_disco(in_item, in_rte)
						legs_num8 = tmp_tmp_2
					end
				else
					tmp_tmp_2 = legs_num8
					rte_add_disco(in_item, in_rte)
					legs_num8 = tmp_tmp_2
				end
			end
		end
		
		if invalid_del_msg == true then
			if in_mode == 0 then
				entry = INVALID_DELETE
			else
				entry2 = INVALID_DELETE
			end
		else
			calc_rte_enable8 = 1
			if rte12_act == in_rte then
				legs_delete = 1
			end
			fms_recalc = 1
			msg_unavaible_crz_alt = 0
		end
	end
end

function find_act_route_wpt2(in_entry, in_pos, in_rte)
	local result = -1
	if in_rte == 0 then
		if in_pos > 0 and legs_num2 > 1 and in_pos <= legs_num2 and in_pos >= offset then
			for zzz = in_pos, legs_num2 do
				if zzz == first_miss_app_idx_mod and in_pos < first_miss_app_idx_mod then
					break
				else
					if legs_data2[zzz][1] == in_entry then
						result = zzz
						break
					end
				end
			end
		end
	elseif in_rte == 1 then
		if in_pos > 0 and legs_num8 > 1 and in_pos <= legs_num8 and in_pos >= offset then
			for zzz = in_pos, legs_num8 do
				if zzz == first_miss_app_idx_mod2 and in_pos < first_miss_app_idx_mod2 then
					break
				else
					if legs_data8[zzz][1] == in_entry then
						result = zzz
						break
					end
				end
			end
		end
	end
	return result
end

function create_legs_abeam_list(in_from, in_to, in_rte)
	
	local id_cust = ""
	local id_nav_txt = ""
	local kk = 0
	local mm = 0
	local nn = 0
	local nd_lat = 0
	local nd_distance = 0
	
	if in_rte == 0 and legs_num > 0 then
		if in_from > 1 and in_from < legs_num2 and in_to > 1 and in_to <= legs_num2 and in_from <= in_to then
			for ab = in_from, in_to do
				if string.sub(legs_data2[ab][1], 1, 1) ~= "(" and legs_data2[ab][1] ~= "DISCONTINUITY" then
					legs_abeam_data_n = legs_abeam_data_n + 1
					legs_abeam_data[legs_abeam_data_n] = {}
					legs_abeam_data[legs_abeam_data_n][1] = legs_data2[ab][1]	--ID Name
					legs_abeam_data[legs_abeam_data_n][2] = legs_data2[ab][7]	-- Lat
					legs_abeam_data[legs_abeam_data_n][3] = legs_data2[ab][8]	-- Lon
					
					if string.len(legs_data2[ab][1]) > 2 then
						if string.sub(legs_data2[ab][1], 1, 2) == "RW" then
							id_cust = "WPT"
						else
							id_cust = string.sub(legs_data2[ab][1], 1, 3)
						end
					elseif string.len(legs_data2[ab][1]) > 1 then
						id_cust = string.sub(legs_data2[ab][1], 1, 2)
					end
					
					nd_lat = string.len(id_cust)
					mm = 1
					if custom_wpt_num > 0 then
						for kk = 1, custom_wpt_num do
							if string.sub(custom_wpt[kk][1], 1, nd_lat) == id_cust then
								nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
								if nn ~= nil then
									if nn >= mm then
										mm = nn + 1
									end
								end
							end
						end
					end
					id_nav_txt = id_cust .. string.format("%02d", mm)
					
					-- add custom wpt
					custom_wpt_num = custom_wpt_num + 1
					custom_wpt[custom_wpt_num] = {}
					custom_wpt[custom_wpt_num][1] = id_nav_txt
					custom_wpt[custom_wpt_num][2] = legs_data2[ab][7]
					custom_wpt[custom_wpt_num][3] = legs_data2[ab][8]
					custom_wpt[custom_wpt_num][4] = ""
					custom_wpt[custom_wpt_num][5] = ""
					custom_wpt[custom_wpt_num][6] = ""
					
					legs_abeam_data[legs_abeam_data_n][4] = id_nav_txt	-- NEW ID Name
					legs_abeam_data[legs_abeam_data_n][5] = legs_data2[ab][39]	-- Wind data
				end
			end
		end
	elseif in_rte ~= 0 and legs_num > 0 then
		if in_from > 1 and in_from < legs_num8 and in_to > 1 and in_to <= legs_num8 and in_from <= in_to then
			for ab = in_from, in_to do
				if string.sub(legs_data8[ab][1], 1, 1) ~= "(" and legs_data8[ab][1] ~= "DISCONTINUITY" then
					legs_abeam_data_n = legs_abeam_data_n + 1
					legs_abeam_data[legs_abeam_data_n] = {}
					legs_abeam_data[legs_abeam_data_n][1] = legs_data8[ab][1]	--ID Name
					legs_abeam_data[legs_abeam_data_n][2] = legs_data8[ab][7]	-- Lat
					legs_abeam_data[legs_abeam_data_n][3] = legs_data8[ab][8]	-- Lon
					
					if string.len(legs_data8[ab][1]) > 2 then
						if string.sub(legs_data8[ab][1], 1, 2) == "RW" then
							id_cust = "WPT"
						else
							id_cust = string.sub(legs_data8[ab][1], 1, 3)
						end
					elseif string.len(legs_data8[ab][1]) > 1 then
						id_cust = string.sub(legs_data8[ab][1], 1, 2)
					end
					
					nd_lat = string.len(id_cust)
					mm = 1
					if custom_wpt_num > 0 then
						for kk = 1, custom_wpt_num do
							if string.sub(custom_wpt[kk][1], 1, nd_lat) == id_cust then
								nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
								if nn ~= nil then
									if nn >= mm then
										mm = nn + 1
									end
								end
							end
						end
					end
					id_nav_txt = id_cust .. string.format("%02d", mm)
					
					-- add custom wpt
					custom_wpt_num = custom_wpt_num + 1
					custom_wpt[custom_wpt_num] = {}
					custom_wpt[custom_wpt_num][1] = id_nav_txt
					custom_wpt[custom_wpt_num][2] = legs_data8[ab][7]
					custom_wpt[custom_wpt_num][3] = legs_data8[ab][8]
					custom_wpt[custom_wpt_num][4] = ""
					custom_wpt[custom_wpt_num][5] = ""
					custom_wpt[custom_wpt_num][6] = ""
					
					legs_abeam_data[legs_abeam_data_n][4] = id_nav_txt	-- NEW ID Name
					legs_abeam_data[legs_abeam_data_n][5] = legs_data8[ab][39]	-- Wind data
				end
			end
		end
	end
end

function rte_add_wpt_cust(aaa, id_cust, lat_cust, lon_cust, entry_in, mode_in, in_rte)

	local ii = aaa + 1
	local jj = legs_num2 + 1
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local temp_brg = 0
	local nd_dis = 0
	local id_nav_txt = ""
	
	if in_rte == 0 then
		if legs_data2[aaa][1] == "DISCONTINUITY" then
			rte_copy(ii, in_rte)
			add_disco = 0
		else
			legs_data2[aaa][31] = "TF"
			rte_copy(aaa, in_rte)
			if aaa == jj then
				add_disco = 0
			else
				add_disco = 1
			end
		end
		
		legs_num2 = aaa
		
		if string.len(id_cust) > 2 then
			if string.sub(id_cust, 1, 2) == "RW" then
				id_cust = "WPT"
			else
				id_cust = string.sub(id_cust, 1, 3)
			end
		elseif string.len(id_cust) > 1 then
			id_cust = string.sub(id_cust, 1, 3)
		end
		
		nd_lat = string.len(id_cust)
		mm = 1
		if custom_wpt_num > 0 then
			for kk = 1, custom_wpt_num do
				if string.sub(custom_wpt[kk][1], 1, nd_lat) == id_cust then
					nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
					if nn ~= nil then
						if nn >= mm then
							mm = nn + 1
						end
					end
				end
			end
		end
		id_nav_txt = id_cust .. string.format("%02d", mm)
		
		-- add custom wpt
		custom_wpt_num = custom_wpt_num + 1
		custom_wpt[custom_wpt_num] = {}
		custom_wpt[custom_wpt_num][1] = id_nav_txt
		custom_wpt[custom_wpt_num][2] = lat_cust
		custom_wpt[custom_wpt_num][3] = lon_cust
		custom_wpt[custom_wpt_num][4] = ""
		custom_wpt[custom_wpt_num][5] = ""
		custom_wpt[custom_wpt_num][6] = ""
		
		legs_data2[legs_num2] = {}
		legs_data2[legs_num2][1] = id_nav_txt	--entry
		legs_data2[legs_num2][2] = 0		-- brg
		legs_data2[legs_num2][3] = 0		-- distance
		legs_data2[legs_num2][4] = 0		-- speed
		legs_data2[legs_num2][5] = 0		-- altitude
		legs_data2[legs_num2][6] = 0	-- altitude type
		legs_data2[legs_num2][7] = lat_cust		-- latitude
		legs_data2[legs_num2][8] = lon_cust		-- longitude
		legs_data2[legs_num2][9] = "DIRECT"			-- via id
		legs_data2[legs_num2][10] = 0		-- calc speed
		legs_data2[legs_num2][11] = 0		-- calc altitude
		legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
		legs_data2[legs_num2][13] = 0
		legs_data2[legs_num2][14] = 0		-- rest alt
		legs_data2[legs_num2][15] = 0		-- last fuel
		legs_data2[legs_num2][16] = ""		-- reg code
		legs_data2[legs_num2][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data2[legs_num2][20] = 0
		legs_data2[legs_num2][21] = -1
		legs_data2[legs_num2][22] = ""
		legs_data2[legs_num2][23] = 0
		legs_data2[legs_num2][24] = 0
		legs_data2[legs_num2][25] = 0
		legs_data2[legs_num2][26] = 0
		legs_data2[legs_num2][27] = ""
		legs_data2[legs_num2][28] = ""
		legs_data2[legs_num2][29] = ""
		legs_data2[legs_num2][30] = ""
		legs_data2[legs_num2][31] = ""
		legs_data2[legs_num2][32] = 0
		legs_data2[legs_num2][33] = ""
		legs_data2[legs_num2][34] = ""
		legs_data2[legs_num2][35] = ""
		legs_data2[legs_num2][36] = 4
		legs_data2[legs_num2][37] = 0
		legs_data2[legs_num2][38] = ""
		legs_data2[legs_num2][39] = ""
		legs_data2[legs_num2][40] = 0
		legs_data2[legs_num2][41] = 0
		legs_data2[legs_num2][42] = 0
		legs_data2[legs_num2][43] = ""
		legs_data2[legs_num2][44] = 0
		legs_data2[legs_num2][45] = 0
		legs_data2[legs_num2][46] = 0
		legs_data2[legs_num2][47] = 0
		legs_data2[legs_num2][48] = ""
		legs_data2[legs_num2][49] = 0
		legs_data2[legs_num2][50] = 0
		legs_data2[legs_num2][51] = 0
		legs_data2[legs_num2][52] = 0
		legs_data2[legs_num2][53] = 0
		legs_data2[legs_num2][54] = 0
		rte_lat = legs_data2[legs_num2][7]
		rte_lon = legs_data2[legs_num2][8]
		
		if add_disco == 1 then
			-- create discontinuity
			legs_num2 = legs_num2 + 1
			rte_add_disco(legs_num2, in_rte)
		end
		
		ii = legs_num2 + 1
		rte_paste(ii, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable2 = 1
		
		if mode_in == 0 then
			entry = ""
		else
			entry2 = ""
		end
	elseif in_rte == 1 then
		if legs_data8[aaa][1] == "DISCONTINUITY" then
			rte_copy(ii, in_rte)
			add_disco = 0
		else
			legs_data8[aaa][31] = "TF"
			rte_copy(aaa, in_rte)
			if aaa == jj then
				add_disco = 0
			else
				add_disco = 1
			end
		end
		
		legs_num8 = aaa
		
		if string.len(id_cust) > 2 then
			if string.sub(id_cust, 1, 2) == "RW" then
				id_cust = "WPT"
			else
				id_cust = string.sub(id_cust, 1, 3)
			end
		elseif string.len(id_cust) > 1 then
			id_cust = string.sub(id_cust, 1, 3)
		end
		
		nd_lat = string.len(id_cust)
		mm = 1
		if custom_wpt_num > 0 then
			for kk = 1, custom_wpt_num do
				if string.sub(custom_wpt[kk][1], 1, nd_lat) == id_cust then
					nn = tonumber(string.sub(custom_wpt[kk][1], nd_lat + 1, -1))
					if nn ~= nil then
						if nn >= mm then
							mm = nn + 1
						end
					end
				end
			end
		end
		id_nav_txt = id_cust .. string.format("%02d", mm)
		
		-- add custom wpt
		custom_wpt_num = custom_wpt_num + 1
		custom_wpt[custom_wpt_num] = {}
		custom_wpt[custom_wpt_num][1] = id_nav_txt
		custom_wpt[custom_wpt_num][2] = lat_cust
		custom_wpt[custom_wpt_num][3] = lon_cust
		custom_wpt[custom_wpt_num][4] = ""
		custom_wpt[custom_wpt_num][5] = ""
		custom_wpt[custom_wpt_num][6] = ""
		
		legs_data8[legs_num8] = {}
		legs_data8[legs_num8][1] = id_nav_txt	--entry
		legs_data8[legs_num8][2] = 0		-- brg
		legs_data8[legs_num8][3] = 0		-- distance
		legs_data8[legs_num8][4] = 0		-- speed
		legs_data8[legs_num8][5] = 0		-- altitude
		legs_data8[legs_num8][6] = 0	-- altitude type
		legs_data8[legs_num8][7] = lat_cust		-- latitude
		legs_data8[legs_num8][8] = lon_cust		-- longitude
		legs_data8[legs_num8][9] = "DIRECT"			-- via id
		legs_data8[legs_num8][10] = 0		-- calc speed
		legs_data8[legs_num8][11] = 0		-- calc altitude
		legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
		legs_data8[legs_num8][13] = 0
		legs_data8[legs_num8][14] = 0		-- rest alt
		legs_data8[legs_num8][15] = 0		-- last fuel
		legs_data8[legs_num8][16] = ""		-- reg code
		legs_data8[legs_num8][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data8[legs_num8][20] = 0
		legs_data8[legs_num8][21] = -1
		legs_data8[legs_num8][22] = ""
		legs_data8[legs_num8][23] = 0
		legs_data8[legs_num8][24] = 0
		legs_data8[legs_num8][25] = 0
		legs_data8[legs_num8][26] = 0
		legs_data8[legs_num8][27] = ""
		legs_data8[legs_num8][28] = ""
		legs_data8[legs_num8][29] = ""
		legs_data8[legs_num8][30] = ""
		legs_data8[legs_num8][31] = ""
		legs_data8[legs_num8][32] = 0
		legs_data8[legs_num8][33] = ""
		legs_data8[legs_num8][34] = ""
		legs_data8[legs_num8][35] = ""
		legs_data8[legs_num8][36] = 4
		legs_data8[legs_num8][37] = 0
		legs_data8[legs_num8][38] = ""
		legs_data8[legs_num8][39] = ""
		legs_data8[legs_num8][40] = 0
		legs_data8[legs_num8][41] = 0
		legs_data8[legs_num8][42] = 0
		legs_data8[legs_num8][43] = ""
		legs_data8[legs_num8][44] = 0
		legs_data8[legs_num8][45] = 0
		legs_data8[legs_num8][46] = 0
		legs_data8[legs_num8][47] = 0
		legs_data8[legs_num8][48] = ""
		legs_data8[legs_num8][49] = 0
		legs_data8[legs_num8][50] = 0
		legs_data8[legs_num8][51] = 0
		legs_data8[legs_num8][52] = 0
		legs_data8[legs_num8][53] = 0
		legs_data8[legs_num8][54] = 0
		rte_lat = legs_data8[legs_num8][7]
		rte_lon = legs_data8[legs_num8][8]
		
		if add_disco == 1 then
			-- create discontinuity
			legs_num8 = legs_num8 + 1
			rte_add_disco(legs_num8, in_rte)
		end
		
		ii = legs_num8 + 1
		rte_paste(ii, in_rte)
		if rte12_act == in_rte then
			legs_delete = 1
		end
		fms_recalc = 1
		msg_unavaible_crz_alt = 0
		calc_rte_enable8 = 1
		
		if mode_in == 0 then
			entry = ""
		else
			entry2 = ""
		end
	end
end

function hold_speed_calc(in_def_spd, in_def_alt)
	
	local result = 0
	local spd_m = 0
	
	if in_def_spd > 0 then
		result = in_def_spd
	else
		if in_def_alt <= 14000 then
			if B738DR_pfd_flaps_up > 0 and B738DR_pfd_flaps_up < 230 then
				result = B738DR_pfd_flaps_up		-- speed
			else
				result = 230		-- speed
			end
		elseif in_def_alt <= 20000 then
			if B738DR_pfd_flaps_up > 0 and B738DR_pfd_flaps_up < 240 then
				result = B738DR_pfd_flaps_up		-- speed
			else
				result = 240		-- speed
			end
		elseif in_def_alt <= 34000 then
			if B738DR_pfd_flaps_up > 0 and B738DR_pfd_flaps_up < 265 then
				result = B738DR_pfd_flaps_up		-- speed
			else
				result = 265		-- speed
			end
		else
			result = 0.76		-- speed
		end
	end
	
	return result
end

function rte_add_hold(aaa, mode_in, in_rte)

	local ii = aaa + 1
	local kk = 0
	local nn = 0
	
	local hold_record = 0
	
	local def_altitude = 0
	local def_altitude2 = 0
	local def_altitude_type = 0
	local def_speed = 0
	local def_turn = 1
	local def_time_dist = ""
	local def_crs = ""
	
	if in_rte == 0 then
		nn = (math.deg(legs_data2[aaa][2]) + mag_variation_deg(legs_data2[aaa][7], legs_data2[aaa][8]) + 360) % 360
		def_crs = string.format("%04d", nn * 10)
		
		hold_record = find_hold_data(legs_data2[aaa][1], legs_data2[aaa][16])
		
		if hold_record > 0 then
			-- altitude
			if hold_nav[hold_record][7] == 0 then
				--below
				def_altitude = hold_nav[hold_record][8]
				def_altitude_type = 45
			elseif hold_nav[hold_record][8] == 0 then
				--above
				def_altitude = hold_nav[hold_record][7]
				def_altitude_type = 43
			else
				if hold_nav[hold_record][7] == hold_nav[hold_record][8] == 0 then
					--at
					def_altitude = hold_nav[hold_record][7]
					def_altitude_type = 0
				else
					--between
					def_altitude = hold_nav[hold_record][7]
					def_altitude2 = hold_nav[hold_record][8]
					def_altitude_type = 41
				end
			end
			-- speed
			def_speed = hold_nav[hold_record][9]
			
			-- time/distance
			if hold_nav[hold_record][4] > 0 then
				def_time_dist = "T" .. string.format("%03d", hold_nav[hold_record][4] * 10)
			else
				def_time_dist = string.format("%04d", hold_nav[hold_record][5] * 10)
			end
			
			-- inbound crs (magnetic)
			def_crs = string.format("%04d", hold_nav[hold_record][3] * 10)
			
			-- turn
			def_turn = hold_nav[hold_record][6]
		end
		
		rte_copy(ii, in_rte)
		legs_num2 = ii
		
		legs_data2[legs_num2] = {}
		
		legs_data2[legs_num2][1] = legs_data2[aaa][1]	--entry
		legs_data2[legs_num2][2] = 0		-- brg
		legs_data2[legs_num2][3] = 0		-- distance
		
		legs_data2[legs_num2][5] = def_altitude		-- altitude
		legs_data2[legs_num2][41] = def_altitude2
		legs_data2[legs_num2][6] = def_altitude_type		-- altitude type
		
		if def_altitude == 0 then
			def_altitude = legs_data2[aaa][11]
		end
		
		legs_data2[legs_num2][4] = def_speed
		legs_data2[legs_num2][42] = 0
		
		legs_data2[legs_num2][10] = hold_speed_calc(def_speed, def_altitude)
		
		legs_data2[legs_num2][7] = legs_data2[aaa][7]		-- latitude
		legs_data2[legs_num2][8] = legs_data2[aaa][8]		-- longitude
		legs_data2[legs_num2][9] = legs_data2[aaa][9]			-- via id
		legs_data2[legs_num2][11] = legs_data2[aaa][11]		-- calc altitude
		legs_data2[legs_num2][12] = 0		-- calc altitude vnav pth
		legs_data2[legs_num2][13] = 0
		legs_data2[legs_num2][14] = 0		-- rest alt
		legs_data2[legs_num2][15] = 0		-- last fuel
		legs_data2[legs_num2][16] = legs_data2[aaa][16]		-- reg code
		legs_data2[legs_num2][17] = 0	--100		-- spd flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data2[legs_num2][19] = legs_data2[aaa][19]		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data2[legs_num2][20] = 0
		legs_data2[legs_num2][21] = def_turn	--1		-- 0-L, 1-R, default turn R
		legs_data2[legs_num2][22] = ""
		legs_data2[legs_num2][23] = 0
		legs_data2[legs_num2][24] = 0
		legs_data2[legs_num2][25] = 0		-- hold lenght mins
		legs_data2[legs_num2][26] = 0		-- hold lenght NM
		
		legs_data2[legs_num2][27] = ""
		legs_data2[legs_num2][28] = ""
		
		legs_data2[legs_num2][29] = def_crs 	-- inbound crs (magnetic)
		legs_data2[legs_num2][30] = def_time_dist	--""
		
		legs_data2[legs_num2][31] = "HM"	-- HOLD manual termination
		legs_data2[legs_num2][32] = 0
		legs_data2[legs_num2][33] = ""
		legs_data2[legs_num2][34] = ""
		legs_data2[legs_num2][35] = ""
		legs_data2[legs_num2][36] = 0
		legs_data2[legs_num2][37] = 0
		legs_data2[legs_num2][38] = ""
		legs_data2[legs_num2][39] = ""
		legs_data2[legs_num2][40] = 0
		legs_data2[legs_num2][43] = ""
		legs_data2[legs_num2][44] = 0
		legs_data2[legs_num2][45] = 0
		legs_data2[legs_num2][46] = 0
		legs_data2[legs_num2][47] = 0
		legs_data2[legs_num2][48] = ""
		legs_data2[legs_num2][49] = 0
		legs_data2[legs_num2][50] = 0
		legs_data2[legs_num2][51] = 0
		legs_data2[legs_num2][52] = 0
		legs_data2[legs_num2][53] = 0
		legs_data2[legs_num2][54] = 0
		
		rte_lat = legs_data2[legs_num2][7]
		rte_lon = legs_data2[legs_num2][8]
		
		
		ii = legs_num2 + 1
		rte_paste(ii, in_rte)
		
		if rte12_act == in_rte then
			hold_exec = 1
		end
		
		-- create hold data
		hold_data_num2 = 0
		hold_data2 = {}
		for ii = 1, legs_num2 do
			if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
				hold_data_num2 = hold_data_num2 + 1
				hold_data2[hold_data_num2] = ii
				if ii == new_hold_idx + 1 then
					new_hold_act_page = hold_data_num2
				end
			end
		end
		
		calc_rte_act2 = 1
	elseif in_rte == 1 then
		nn = (math.deg(legs_data8[aaa][2]) + mag_variation_deg(legs_data8[aaa][7], legs_data8[aaa][8]) + 360) % 360
		def_crs = string.format("%04d", nn * 10)
		
		hold_record = find_hold_data(legs_data8[aaa][1], legs_data8[aaa][16])
		
		if hold_record > 0 then
			-- altitude
			if hold_nav[hold_record][7] == 0 then
				--below
				def_altitude = hold_nav[hold_record][8]
				def_altitude_type = 45
			elseif hold_nav[hold_record][8] == 0 then
				--above
				def_altitude = hold_nav[hold_record][7]
				def_altitude_type = 43
			else
				if hold_nav[hold_record][7] == hold_nav[hold_record][8] == 0 then
					--at
					def_altitude = hold_nav[hold_record][7]
					def_altitude_type = 0
				else
					--between
					def_altitude = hold_nav[hold_record][7]
					def_altitude2 = hold_nav[hold_record][8]
					def_altitude_type = 41
				end
			end
			-- speed
			def_speed = hold_nav[hold_record][9]
			
			-- time/distance
			if hold_nav[hold_record][4] > 0 then
				def_time_dist = "T" .. string.format("%03d", hold_nav[hold_record][4] * 10)
			else
				def_time_dist = string.format("%04d", hold_nav[hold_record][5] * 10)
			end
			
			-- inbound crs (magnetic)
			def_crs = string.format("%04d", hold_nav[hold_record][3] * 10)
			
			-- turn
			def_turn = hold_nav[hold_record][6]
		end
		
		rte_copy(ii, in_rte)
		legs_num8 = ii
		
		legs_data8[legs_num8] = {}
		
		legs_data8[legs_num8][1] = legs_data8[aaa][1]	--entry
		legs_data8[legs_num8][2] = 0		-- brg
		legs_data8[legs_num8][3] = 0		-- distance
		
		legs_data8[legs_num8][5] = def_altitude		-- altitude
		legs_data8[legs_num8][41] = def_altitude2
		legs_data8[legs_num8][6] = def_altitude_type		-- altitude type
		
		if def_altitude == 0 then
			def_altitude = legs_data8[aaa][11]
		end
		
		legs_data8[legs_num8][4] = def_speed
		legs_data8[legs_num8][42] = 0
		
		legs_data8[legs_num8][10] = hold_speed_calc(def_speed, def_altitude)
		
		legs_data8[legs_num8][7] = legs_data8[aaa][7]		-- latitude
		legs_data8[legs_num8][8] = legs_data8[aaa][8]		-- longitude
		legs_data8[legs_num8][9] = legs_data8[aaa][9]			-- via id
		legs_data8[legs_num8][11] = legs_data8[aaa][11]		-- calc altitude
		legs_data8[legs_num8][12] = 0		-- calc altitude vnav pth
		legs_data8[legs_num8][13] = 0
		legs_data8[legs_num8][14] = 0		-- rest alt
		legs_data8[legs_num8][15] = 0		-- last fuel
		legs_data8[legs_num8][16] = legs_data8[aaa][16]		-- reg code
		legs_data8[legs_num8][17] = 0	--100		-- spd flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data8[legs_num8][19] = legs_data8[aaa][19]		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data8[legs_num8][20] = 0
		legs_data8[legs_num8][21] = def_turn	--1		-- 0-L, 1-R, default turn R
		legs_data8[legs_num8][22] = ""
		legs_data8[legs_num8][23] = 0
		legs_data8[legs_num8][24] = 0
		legs_data8[legs_num8][25] = 0		-- hold lenght mins
		legs_data8[legs_num8][26] = 0		-- hold lenght NM
		
		legs_data8[legs_num8][27] = ""
		legs_data8[legs_num8][28] = ""
		
		legs_data8[legs_num8][29] = def_crs 	-- inbound crs (magnetic)
		legs_data8[legs_num8][30] = def_time_dist	--""
		
		legs_data8[legs_num8][31] = "HM"	-- HOLD manual termination
		legs_data8[legs_num8][32] = 0
		legs_data8[legs_num8][33] = ""
		legs_data8[legs_num8][34] = ""
		legs_data8[legs_num8][35] = ""
		legs_data8[legs_num8][36] = 0
		legs_data8[legs_num8][37] = 0
		legs_data8[legs_num8][38] = ""
		legs_data8[legs_num8][39] = ""
		legs_data8[legs_num8][40] = 0
		legs_data8[legs_num8][43] = ""
		legs_data8[legs_num8][44] = 0
		legs_data8[legs_num8][45] = 0
		legs_data8[legs_num8][46] = 0
		legs_data8[legs_num8][47] = 0
		legs_data8[legs_num8][48] = ""
		legs_data8[legs_num8][49] = 0
		legs_data8[legs_num8][50] = 0
		legs_data8[legs_num8][51] = 0
		legs_data8[legs_num8][52] = 0
		legs_data8[legs_num8][53] = 0
		legs_data8[legs_num8][54] = 0
		
		rte_lat = legs_data8[legs_num8][7]
		rte_lon = legs_data8[legs_num8][8]
		
		
		ii = legs_num8 + 1
		rte_paste(ii, in_rte)
		
		if rte12_act == in_rte then
			hold_exec2 = 1
		end
		
		-- create hold data
		hold_data_num4 = 0
		hold_data4 = {}
		for ii = 1, legs_num8 do
			if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
				hold_data_num4 = hold_data_num4 + 1
				hold_data4[hold_data_num4] = ii
				
				if ii == new_hold_idx2 + 1 then
					new_hold_act_page2 = hold_data_num4
				end
			end
		end
		
		calc_rte_act8 = 1
	end
end

function rte_add_disco(aaa, in_rte)
	
	if in_rte == 0 then
		-- create DISCONTINUITY
		legs_num2 = aaa
		legs_data2[aaa] = {}
		legs_data2[aaa][1] = "DISCONTINUITY"
		legs_data2[aaa][2] = 0		-- brg
		legs_data2[aaa][3] = 0		-- distance
		legs_data2[aaa][4] = 0		-- speed
		legs_data2[aaa][5] = 0		-- altitude
		legs_data2[aaa][6] = 0	-- altitude type
		legs_data2[aaa][7] = rte_lat		-- latitude
		legs_data2[aaa][8] = rte_lon		-- longitude
		legs_data2[aaa][9] = ""			-- via id
		legs_data2[aaa][10] = 0		-- calc speed
		legs_data2[aaa][11] = 0		-- calc altitude
		legs_data2[aaa][12] = 0		-- calc altitude vnav pth
		legs_data2[aaa][13] = 0
		legs_data2[aaa][14] = 0		-- rest alt
		legs_data2[aaa][15] = 0		-- last fuel
		legs_data2[aaa][16] = ""
		legs_data2[aaa][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
		legs_data2[aaa][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data2[aaa][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data2[aaa][20] = 0
		legs_data2[aaa][21] = -1
		legs_data2[aaa][22] = ""
		legs_data2[aaa][23] = 0
		legs_data2[aaa][24] = 0
		legs_data2[aaa][25] = 0
		legs_data2[aaa][26] = 0
		legs_data2[aaa][27] = ""
		legs_data2[aaa][28] = ""
		legs_data2[aaa][29] = ""
		legs_data2[aaa][30] = ""
		legs_data2[aaa][31] = ""
		legs_data2[aaa][32] = 0
		legs_data2[aaa][33] = ""
		legs_data2[aaa][34] = ""
		legs_data2[aaa][35] = ""
		legs_data2[aaa][36] = 0
		legs_data2[aaa][37] = 0
		legs_data2[aaa][38] = ""
		legs_data2[aaa][39] = ""
		legs_data2[aaa][40] = 0
		legs_data2[aaa][41] = 0
		legs_data2[aaa][42] = 0
		legs_data2[aaa][43] = ""
		legs_data2[aaa][44] = 0
		legs_data2[aaa][45] = 0
		legs_data2[aaa][46] = 0
		legs_data2[aaa][47] = 0
		legs_data2[aaa][48] = ""
		legs_data2[aaa][49] = 0
		legs_data2[aaa][50] = 0
		legs_data2[aaa][51] = 0
		legs_data2[aaa][52] = 0
		legs_data2[aaa][53] = 0
		legs_data2[aaa][54] = 0
	elseif in_rte == 1 then
		-- create DISCONTINUITY
		legs_num8 = aaa
		legs_data8[aaa] = {}
		legs_data8[aaa][1] = "DISCONTINUITY"
		legs_data8[aaa][2] = 0		-- brg
		legs_data8[aaa][3] = 0		-- distance
		legs_data8[aaa][4] = 0		-- speed
		legs_data8[aaa][5] = 0		-- altitude
		legs_data8[aaa][6] = 0	-- altitude type
		legs_data8[aaa][7] = rte_lat		-- latitude
		legs_data8[aaa][8] = rte_lon		-- longitude
		legs_data8[aaa][9] = ""			-- via id
		legs_data8[aaa][10] = 0		-- calc speed
		legs_data8[aaa][11] = 0		-- calc altitude
		legs_data8[aaa][12] = 0		-- calc altitude vnav pth
		legs_data8[aaa][13] = 0
		legs_data8[aaa][14] = 0		-- rest alt
		legs_data8[aaa][15] = 0		-- last fuel
		legs_data8[aaa][16] = ""
		legs_data8[aaa][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
		legs_data8[aaa][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
		legs_data8[aaa][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
		legs_data8[aaa][20] = 0
		legs_data8[aaa][21] = -1
		legs_data8[aaa][22] = ""
		legs_data8[aaa][23] = 0
		legs_data8[aaa][24] = 0
		legs_data8[aaa][25] = 0
		legs_data8[aaa][26] = 0
		legs_data8[aaa][27] = ""
		legs_data8[aaa][28] = ""
		legs_data8[aaa][29] = ""
		legs_data8[aaa][30] = ""
		legs_data8[aaa][31] = ""
		legs_data8[aaa][32] = 0
		legs_data8[aaa][33] = ""
		legs_data8[aaa][34] = ""
		legs_data8[aaa][35] = ""
		legs_data8[aaa][36] = 0
		legs_data8[aaa][37] = 0
		legs_data8[aaa][38] = ""
		legs_data8[aaa][39] = ""
		legs_data8[aaa][40] = 0
		legs_data8[aaa][41] = 0
		legs_data8[aaa][42] = 0
		legs_data8[aaa][43] = ""
		legs_data8[aaa][44] = 0
		legs_data8[aaa][45] = 0
		legs_data8[aaa][46] = 0
		legs_data8[aaa][47] = 0
		legs_data8[aaa][48] = ""
		legs_data8[aaa][49] = 0
		legs_data8[aaa][50] = 0
		legs_data8[aaa][51] = 0
		legs_data8[aaa][52] = 0
		legs_data8[aaa][53] = 0
		legs_data8[aaa][54] = 0
	end
end

function rte_add_discob(aaa)
	
	-- create DISCONTINUITY
	legs_num2b = aaa
	legs_data2b[aaa] = {}
	legs_data2b[aaa][1] = "DISCONTINUITY"
	legs_data2b[aaa][2] = 0		-- brg
	legs_data2b[aaa][3] = 0		-- distance
	legs_data2b[aaa][4] = 0		-- speed
	legs_data2b[aaa][5] = 0		-- altitude
	legs_data2b[aaa][6] = 0	-- altitude type
	legs_data2b[aaa][7] = rte_lat		-- latitude
	legs_data2b[aaa][8] = rte_lon		-- longitude
	legs_data2b[aaa][9] = ""			-- via id
	legs_data2b[aaa][10] = 0		-- calc speed
	legs_data2b[aaa][11] = 0		-- calc altitude
	legs_data2b[aaa][12] = 0		-- calc altitude vnav pth
	legs_data2b[aaa][13] = 0
	legs_data2b[aaa][14] = 0		-- rest alt
	legs_data2b[aaa][15] = 0		-- last fuel
	legs_data2b[aaa][16] = ""
	legs_data2b[aaa][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
	legs_data2b[aaa][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
	legs_data2b[aaa][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
	legs_data2b[aaa][20] = 0
	legs_data2b[aaa][21] = -1
	legs_data2b[aaa][22] = ""
	legs_data2b[aaa][23] = 0
	legs_data2b[aaa][24] = 0
	legs_data2b[aaa][25] = 0
	legs_data2b[aaa][26] = 0
	legs_data2b[aaa][27] = ""
	legs_data2b[aaa][28] = ""
	legs_data2b[aaa][29] = ""
	legs_data2b[aaa][30] = ""
	legs_data2b[aaa][31] = ""
	legs_data2b[aaa][32] = 0
	legs_data2b[aaa][33] = ""
	legs_data2b[aaa][34] = ""
	legs_data2b[aaa][35] = ""
	legs_data2b[aaa][36] = 0
	legs_data2b[aaa][37] = 0
	legs_data2b[aaa][38] = ""
	legs_data2b[aaa][39] = ""
	legs_data2b[aaa][40] = 0
	legs_data2b[aaa][41] = 0
	legs_data2b[aaa][42] = 0
	legs_data2b[aaa][43] = ""
	legs_data2b[aaa][44] = 0
	legs_data2b[aaa][45] = 0
	legs_data2b[aaa][46] = 0
	legs_data2b[aaa][47] = 0
	legs_data2b[aaa][48] = ""
	legs_data2b[aaa][49] = 0
	legs_data2b[aaa][50] = 0
	legs_data2b[aaa][51] = 0
	legs_data2b[aaa][52] = 0
	legs_data2b[aaa][53] = 0
	legs_data2b[aaa][54] = 0
	
end

function rte_add_sid()
	
	local ii = 0
	local jj = 0
	local kk = 0
	local mm = 0
	local nn = 0
	
	if add_sid_star_app_act == 0 then
		if rte_add_sid_act == 1  then
			copy_to_rte_sid(0)
			add_star = 0
			offset_sid_idx = 0
			offset_sid_idx2 = 0
			
			-- if legs_num2 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 1 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			-- if legs_num2 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			if legs_num2 > 0 then
				
				if add_star == 0 then
					
					if legs_data2[1][17] == 4 then
						offset_sid_mode = 2
						rnw_added = 0
						nn = 1
					else
						offset_sid_mode = 3
						nn = offset_mod
						if nn <= 0 then
							nn = 1
						end
						if nn > legs_num2 then
							nn = legs_num2
						end
						rnw_added = 1
					end
					
					-- for ii = 1, legs_num2 do
						-- if legs_data2[ii][17] == 4 then
							-- if string.sub(legs_data2[ii][1], 1, 2) == "RW" then
								-- rnw_added = 1
							-- end
							-- nn = ii
							-- break
						-- end
					-- end
					
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, legs_num2 + 1 do
						legs_data2b[ii] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[ii][jj] = legs_data2[ii][jj]
						end
					end
					legs_num2b = legs_num2
					
					--delete previous SID
					rte_del_specb(1, 0, nn + 1)
					
					rte_copyb(nn + 1)
					
					offset_sid_idx = nn
					offset_sid_idx2 = offset_mod
					
					sid_cnt = 0
					rte_add_sid_act = 2
					add_ok = 0
				elseif in_flight_mode == 0 and legs_data2[1][17] == 4 then
					-- dump_leg_append("Before SID\n")
					-- -- dump_leg2a()
					-- copy to legs_data2b
					offset_sid_mode = 0
					legs_data2b = {}
					for ii = 1, legs_num2 + 1 do
						legs_data2b[ii] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[ii][jj] = legs_data2[ii][jj]
						end
					end
					legs_num2b = legs_num2
					-- dump_leg_append(tostring(legs_num2))
					
					sid_cnt = 0
					rte_add_sid_act = 2
					rnw_added = 0
					add_ok = 0
					
					--delete previous SID
					-- if rte12_act == 0 then
						-- rte_del_specb(1, 0, offset)
					-- else
						-- rte_del_specb(1, 0, offset_mod)
					-- end
					rte_del_specb(1, 0, 2)
					
					-- dump_leg_append("After SID in_flight=0\n")
					-- dump_leg2ba()
				elseif legs_data2[1][17] == 0 then
					offset_sid_mode = 1
					kk = offset_mod
					if kk <= 0 then
						kk = 1
					end
					if kk > legs_num2 then
						kk = legs_num2
					end
					nn = kk
					rnw_added = 0
					for ii = kk, legs_num2 do
						if string.sub(legs_data2[ii][1], 1, 2) == "RW" then
							nn = ii
							rnw_added = 1
							break
						end
					end
					
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, nn do
						mm = mm + 1
						if legs_data2[ii][19] == 1 or legs_data2[ii][19] == 3 then
							legs_data2[ii][19] = 0
							legs_data2[ii][9] = ""
						elseif legs_data2[ii][19] == 2 or legs_data2[ii][19] == 4 or legs_data2[ii][19] == 7 or legs_data2[ii][19] == 8 or legs_data2[ii][19] == 9 then
							legs_data2[ii][19] = 0
							legs_data2[ii][9] = ""
						end
						legs_data2b[mm] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[mm][jj] = legs_data2[ii][jj]
						end
					end
					legs_data2b[mm + 1] = {}
					for jj = 1, MAX_LEGS_DATA do
						legs_data2b[mm + 1][jj] = legs_data2[legs_num2 + 1][jj]
					end
					
					legs_num2b = mm
					
					if rnw_added == 1 then
						rte_copyb(legs_num2b + 1)
					end
					
					sid_cnt = 0
					rte_add_sid_act = 2
					-- rnw_added = 0
					add_ok = 0
				else
					rte_add_sid_act = 0
				end
			else
				rte_add_sid_act = 0
			end
			wrong_rw = 0
			added_sid_idx = 0
			added_sid_tns_idx = 0
		elseif rte_add_sid_act == 2 then
			rte_add_sid2()
			--dump_leg2b()
		elseif rte_add_sid_act == 3 then
			for ii = 1, 20 do
				sid_cnt = sid_cnt + 1
				if sid_cnt > rte_sid_num then
					rte_add_sid_act = 4
					rte_add_sid2()
					break
				end
				rte_add_sid2()
			end
			--dump_leg2b()
		elseif rte_add_sid_act == 5 then
			-- copy to legs_data2
			--dump_leg2b()
			
			legs_data2 = {}
			for ii = 1, legs_num2b + 1 do
				legs_data2[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data2[ii][jj] = legs_data2b[ii][jj]
				end
			end
			legs_num2 = legs_num2b
			rte_add_sid_act = 0
			--dump_leg()
			--dump_leg2b()
			
		end
	else
		--RTE2
		if rte_add_sid_act == 1  then
			copy_to_rte_sid(1)
			add_star = 0
			offset_sid_idx = 0
			offset_sid_idx2 = 0
			
			-- if legs_num8 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 1 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			-- if legs_num8 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			if legs_num8 > 0 then
				
				if add_star == 0 then
					
					if legs_data8[1][17] == 4 then
						offset_sid_mode = 2
						rnw_added = 0
						nn = 1
					else
						offset_sid_mode = 3
						nn = offset_mod2
						if nn <= 0 then
							nn = 1
						end
						if nn > legs_num2 then
							nn = legs_num2
						end
						rnw_added = 1
					end
					
					-- for ii = 1, legs_num8 do
						-- if legs_data8[ii][17] == 4 then
							-- if string.sub(legs_data8[ii][1], 1, 2) == "RW" then
								-- rnw_added = 1
							-- end
							-- nn = ii
							-- break
						-- end
					-- end
					
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, legs_num8 + 1 do
						legs_data2b[ii] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[ii][jj] = legs_data8[ii][jj]
						end
					end
					legs_num2b = legs_num8
					
					--delete previous SID
					rte_del_specb(1, 0, nn + 1)
					
					rte_copyb(nn + 1)
					
					offset_sid_idx = nn
					offset_sid_idx2 = offset_mod2
					
					sid_cnt = 0
					rte_add_sid_act = 2
					add_ok = 0
					
				elseif in_flight_mode == 0 and legs_data8[1][17] == 4 then
					offset_sid_mode = 0
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, legs_num8 + 1 do
						legs_data2b[ii] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[ii][jj] = legs_data8[ii][jj]
						end
					end
					legs_num2b = legs_num8
					
					sid_cnt = 0
					rte_add_sid_act = 2
					rnw_added = 0
					add_ok = 0
					--delete previous SID
					if rte12_act == 1 then
						rte_del_specb(1, 0, offset)
					else
						rte_del_specb(1, 0, offset_mod2)
					end
				elseif legs_data8[1][17] == 0 then
					offset_sid_mode = 1
					kk = offset_mod2
					if kk > legs_num8 then
						kk = legs_num8
					end
					nn = kk
					rnw_added = 0
					for ii = kk, legs_num8 do
						if string.sub(legs_data8[ii][1], 1, 2) == "RW" then
							nn = ii
							rnw_added = 1
							break
						end
					end
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, nn do
						mm = mm + 1
						if legs_data8[ii][19] == 1 or legs_data8[ii][19] == 3 then
							legs_data8[ii][19] = 0
							legs_data8[ii][9] = ""
						elseif legs_data8[ii][19] == 2 or legs_data8[ii][19] == 4 or legs_data8[ii][19] == 7 or legs_data8[ii][19] == 8 or legs_data8[ii][19] == 9 then
							legs_data8[ii][19] = 0
							legs_data8[ii][9] = ""
						end
						legs_data2b[mm] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[mm][jj] = legs_data8[ii][jj]
						end
					end
					legs_data2b[mm + 1] = {}
					for jj = 1, MAX_LEGS_DATA do
						legs_data2b[mm + 1][jj] = legs_data8[legs_num8 + 1][jj]
					end
					
					legs_num2b = mm
					
					if rnw_added == 1 then
						rte_copyb(legs_num2b + 1)
					end
					
					sid_cnt = 0
					rte_add_sid_act = 2
					add_ok = 0
				else
					rte_add_sid_act = 0
				end
			else
				rte_add_sid_act = 0
			end
			wrong_rw = 0
			added_sid_idx = 0
			added_sid_tns_idx = 0
		elseif rte_add_sid_act == 2 then
			rte_add_sid2()
		elseif rte_add_sid_act == 3 then
			for ii = 1, 20 do
				sid_cnt = sid_cnt + 1
				if sid_cnt > rte_sid_num then
					rte_add_sid_act = 4
					rte_add_sid2()
					break
				end
				rte_add_sid2()
			end
		elseif rte_add_sid_act == 5 then
			-- copy to legs_data8
			legs_data8 = {}
			for ii = 1, legs_num2b + 1 do
				legs_data8[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data8[ii][jj] = legs_data2b[ii][jj]
				end
			end
			legs_num8 = legs_num2b
			rte_add_sid_act = 0
		end
	end
end


function rte_add_star()
	
	local ii = 0
	local jj = 0
	
	if add_sid_star_app_act == 0 then
		if rte_add_sid_act == 0 then
			if rte_add_star_act == 1 then
				if legs_num2 > 0 then
					copy_to_rte_star(0)
					add_star = 0
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, legs_num2 + 1 do
						legs_data2b[ii] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[ii][jj] = legs_data2[ii][jj]
						end
					end
					legs_num2b = legs_num2
					sid_cnt = 0
					rte_add_star_act = 2
					add_ok = 0
					--delete previous STAR, APP
					if rte12_act == 0 then
						rte_del_specb(99, 1, offset)
					else
						rte_del_specb(99, 1, offset_mod)
					end
					add_star = 1	-- info for APP
					add_star2 = 0
					first_add_star_idx = legs_num2b
				else
					rte_add_star_act = 0
				end
			elseif rte_add_star_act > 1 then
				for ii = 1, 20 do
					sid_cnt = sid_cnt + 1
					if sid_cnt > rte_star_num then
						rte_add_star_act = 0
						first_add_star_idx = 0
						break
					end
					rte_add_star2()
				end
			end
		end
	else
		--RTE2
		if rte_add_sid_act == 0 then
			if rte_add_star_act == 1 then
				if legs_num8 > 0 then
					copy_to_rte_star(1)
					add_star = 0
					-- copy to legs_data2b
					legs_data2b = {}
					for ii = 1, legs_num8 + 1 do
						legs_data2b[ii] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2b[ii][jj] = legs_data8[ii][jj]
						end
					end
					legs_num2b = legs_num8
					sid_cnt = 0
					rte_add_star_act = 2
					add_ok = 0
					--delete previous STAR, APP
					if rte12_act == 1 then
						rte_del_specb(99, 1, offset)
					else
						rte_del_specb(99, 1, offset_mod2)
					end
					add_star = 1	-- info for APP
					add_star2 = 0
					first_add_star_idx = legs_num2b
				else
					rte_add_star_act = 0
				end
			elseif rte_add_star_act > 1 then
				for ii = 1, 20 do
					sid_cnt = sid_cnt + 1
					if sid_cnt > rte_star_num then
						rte_add_star_act = 0
						first_add_star_idx = 0
						break
					end
					rte_add_star2()
				end
			end
		end
	end
end

function rte_add_app()
	
	local ii = 0
	local jj = 0
	local temp_str = ""
	
	if add_sid_star_app_act == 0 then
		if rte_add_sid_act == 0 and rte_add_star_act == 0 then
			if rte_add_app_act == 1 then
				if legs_num2 > 0 then
					copy_to_rte_app(0)
					sid_cnt = 0
					rte_add_app_act = 2
					add_ok = 0
					add_app = 0
					if add_star == 0 then
						--copy to legs_data2b
						legs_data2b = {}
						for ii = 1, legs_num2 + 1 do
							legs_data2b[ii] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2b[ii][jj] = legs_data2[ii][jj]
							end
						end
						legs_num2b = legs_num2
						--delete previous APP
						if rte12_act == 0 then
							rte_del_specb(7, 1, offset)
						else
							rte_del_specb(7, 1, offset_mod)
						end
						
						B738DR_fms_test1 = 3001
					else
						if add_star == 2 then
							add_ok = 1
						end
					end
					if first_add_star_idx ~= 0 then
						first_add_app_idx = first_add_star_idx
					else
						first_add_app_idx = legs_num2b
					end
					first_add_app_idx2 = legs_num2b
					
					-- approach name
					temp_str = string.sub(des_app2, 1, 1)
					if temp_str == "I" then
						app_str = "ILS "
					elseif temp_str == "R" then
						app_str = "RNV "
					elseif temp_str == "D" then
						app_str = "VDM "
					elseif temp_str == "S" then
						app_str = "VDM "
					elseif temp_str == "L" then
						app_str = "LOC "
					elseif temp_str == "Q" then
						app_str = "NDB "
					elseif temp_str == "N" then
						app_str = "NDB "
					elseif temp_str == "J" then
						app_str = "GLS "
					elseif temp_str == "V" then
						app_str = "VOR "
					elseif temp_str == "X" then
						app_str = "LDA "
					else
						app_str = temp_str
					end
					app_str = app_str .. string.sub(des_app2, 2, -1)
					app_via_mised = 0
				else
					rte_add_app_act = 0
				end
			elseif rte_add_app_act == 2 then
				for ii = 1, 20 do
					sid_cnt = sid_cnt + 1
					if sid_cnt > rte_app_num then
						rte_add_app_act = 3
						rte_add_app2()
						break
					end
					B738DR_fms_test1 = 3002
					rte_add_app2()
					B738DR_fms_test1 = 3003
				end
			elseif rte_add_app_act == 4 then
				legs_data2 = {}
				for ii = 1, legs_num2b + 1 do
					legs_data2[ii] = {}
					if add_star == 0 then
						B738DR_fms_test1 = 5000 + ii
					elseif add_star == 1 then
						B738DR_fms_test1 = 6000 + ii
					else
						B738DR_fms_test1 = 8000 + ii
					end
					
					for jj = 1, MAX_LEGS_DATA do
						legs_data2[ii][jj] = legs_data2b[ii][jj]
					end
				end
				legs_num2 = legs_num2b
				rte_add_app_act = 0
				first_add_app_idx = 0
				first_add_app_idx2 = 0
				first_add_star_idx = 0
				B738DR_fms_test1 = 3005
				
				-- Temporary shifted
				add_star = 0
				add_star2 = 0
			end
		end
	else
		--RTE2
		if rte_add_sid_act == 0 and rte_add_star_act == 0 then
			if rte_add_app_act == 1 then
				if legs_num8 > 0 then
					copy_to_rte_app(1)
					sid_cnt = 0
					rte_add_app_act = 2
					add_ok = 0
					add_app = 0
					if add_star == 0 then
						--copy to legs_data2b
						legs_data2b = {}
						for ii = 1, legs_num8 + 1 do
							legs_data2b[ii] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2b[ii][jj] = legs_data8[ii][jj]
							end
						end
						legs_num2b = legs_num8
						--delete previous APP
						if rte12_act == 1 then
							rte_del_specb(7, 1, offset)
						else
							rte_del_specb(7, 1, offset_mod2)
						end
						
						B738DR_fms_test1 = 3001
					else
						if add_star == 2 then
							add_ok = 1
						end
					end
					if first_add_star_idx ~= 0 then
						first_add_app_idx = first_add_star_idx
					else
						first_add_app_idx = legs_num2b
					end
					first_add_app_idx2 = legs_num2b
					
					-- approach name
					temp_str = string.sub(des_app8, 1, 1)
					if temp_str == "I" then
						app_str = "ILS "
					elseif temp_str == "R" then
						app_str = "RNV "
					elseif temp_str == "D" then
						app_str = "VDM "
					elseif temp_str == "S" then
						app_str = "VDM "
					elseif temp_str == "L" then
						app_str = "LOC "
					elseif temp_str == "Q" then
						app_str = "NDB "
					elseif temp_str == "N" then
						app_str = "NDB "
					elseif temp_str == "J" then
						app_str = "GLS "
					elseif temp_str == "V" then
						app_str = "VOR "
					elseif temp_str == "X" then
						app_str = "LDA "
					else
						app_str = temp_str
					end
					app_str = app_str .. string.sub(des_app8, 2, -1)
					app_via_mised = 0
				else
					rte_add_app_act = 0
				end
			elseif rte_add_app_act == 2 then
				for ii = 1, 20 do
					sid_cnt = sid_cnt + 1
					if sid_cnt > rte_app_num then
						rte_add_app_act = 3
						rte_add_app2()
						break
					end
					B738DR_fms_test1 = 3002
					rte_add_app2()
					B738DR_fms_test1 = 3003
				end
			elseif rte_add_app_act == 4 then
				legs_data8 = {}
				for ii = 1, legs_num2b + 1 do
					legs_data8[ii] = {}
					if add_star == 0 then
						B738DR_fms_test1 = 5000 + ii
					elseif add_star == 1 then
						B738DR_fms_test1 = 6000 + ii
					else
						B738DR_fms_test1 = 8000 + ii
					end
					
					for jj = 1, MAX_LEGS_DATA do
						legs_data8[ii][jj] = legs_data2b[ii][jj]
					end
				end
				legs_num8 = legs_num2b
				rte_add_app_act = 0
				first_add_app_idx = 0
				first_add_app_idx2 = 0
				first_add_star_idx = 0
				B738DR_fms_test1 = 3005
				
				-- Temporary shifted
				add_star = 0
				add_star2 = 0
			end
		end
	end
end



function rte_add_sid2()
	
	local ii = 0
	local gg = 0
	local gg2 = 0
	local gg3 = 0
	local xx_temp = 0
	local rnw_ok  = 0
	local rnw_ok2  = 0
	local wpt_sid_idx = 0
	local add_sid_rte_wpt = false
	local rnw_txt = ""
	local rnw_txt2 = ""
	local tns_ok = 0
	local disco_status = 0
	
	local temp_str = ""
	local jj = 0
	local add_wpt = 0
	local prev_ok = 0
	
	local nd_x = 0
	local idx_rec = 0
	
	local find_icao = ""
	local find_rwy = ""
	local find_alt = 0
	local find_sid = ""
	local find_sid_tns = ""
	local sid_via = ""
	
	if add_sid_star_app_act == 0 then
		find_icao = ref_icao_x
		find_rwy = ref_rwy2
		find_alt = ref_icao_alt
		find_sid = ref_sid2
		find_sid_tns = ref_sid_tns2
	else
		find_icao = ref_icao7_x
		find_rwy = ref_rwy8
		find_alt = ref_icao_alt7
		find_sid = ref_sid8
		find_sid_tns = ref_sid_tns8
	end
	if find_sid_tns == "------" then
		sid_via = find_sid
	else
		sid_via = find_sid .. "." .. find_sid_tns
		if string.len(sid_via) > 15 then
			sid_via = string.sub(sid_via, 1, 15)
		end
	end
	
	local wrong_sid_runway = 1
	
	-- SIDs by runway
	local rnw_temp = "RW" .. find_rwy
	local temp_str1 = ""
	local temp_str2 = ""
	
	-- if offset_sid_mode == 2 or offset_sid_mode == 3 then
		-- if rnw_added == 1 and offset_sid_idx > 0 and offset_sid_idx <= legs_num2b and offset_sid_mode == 3 then
			-- if string.sub(legs_data2b[offset_sid_idx][1], 1, 2) == "RW" then
				-- rnw_temp = legs_data2b[offset_sid_idx][1]
			-- end
		-- end
	-- end
	
	-- copy_to_ref_data(add_sid_star_app_act)
	if offset_sid_mode ~= 3 then
		if add_sid_star_app_act == 0 then
			if sid_num_1 > 0 then
				for ii = 1, sid_num_1 do
					if string.sub(ref_data_sid_1[ii][2],-1,-1) == "B" then
						temp_str1 = string.sub(ref_data_sid_1[ii][2],1,-2)
						temp_str2 = string.sub(rnw_temp,1,-2)
					else
						temp_str1 = "A"
						temp_str2 = "B"
					end
					if rnw_temp == ref_data_sid_1[ii][2] or ref_data_sid_1[ii][2] == "ALL" or temp_str1 == temp_str2 then
						if find_sid == ref_data_sid_1[ii][1] then
							wrong_sid_runway = 0
							break
						end
						
					end
				end
			end
		else
			if sid_num_2 > 0 then
				for ii = 1, sid_num_2 do
					if string.sub(ref_data_sid_2[ii][2],-1,-1) == "B" then
						temp_str1 = string.sub(ref_data_sid_2[ii][2],1,-2)
						temp_str2 = string.sub(rnw_temp,1,-2)
					else
						temp_str1 = "A"
						temp_str2 = "B"
					end
					if rnw_temp == ref_data_sid_2[ii][2] or ref_data_sid_2[ii][2] == "ALL" or temp_str1 == temp_str2 then
						if find_sid == ref_data_sid_2[ii][1] then
							wrong_sid_runway = 0
							break
						end
						
					end
				end
			end
		end
	end
	
	-- B738DR_fms_test7 = wrong_sid_runway
	-- B738DR_fms_test8 = wrong_rw
	
	rte_lat = 0
	rte_lon = 0
	
	--add RWxx
	if rnw_data_num > 0 and rte_add_sid_act == 2 and string.len(find_icao) > 0 then
		nd_x = string.byte(string.sub(find_icao, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if find_icao == rnw_data[idx_rec][1] and find_rwy == rnw_data[idx_rec][2] then
						if add_ok == 0 and rnw_added == 0 then
							-- if in_flight_mode == 0 or rte12_act ~= add_sid_star_app_act then
							if offset_sid_mode == 2 or offset_sid_mode == 3 then
								if (offset_sid_idx2 > offset_sid_idx + 1) and offset_sid_mode == 3 then
									legs_num2b = offset_sid_idx2
								else
									legs_num2b = offset_sid_idx
								end
								gg3 = 0
								legs_num2b = legs_num2b + 1
							elseif offset_sid_mode == 0 then
								if legs_data2b[2][1] == "DISCONTINUITY" then
									rte_copyb(3)
								else
									rte_copyb(2)
								end
								legs_num2b = 2
							else
								rte_copyb(legs_num2b + 1)
								if string.sub(legs_data2b[legs_num2b][1], 1, 2) == "RW" then
									gg3 = 1
								else
									legs_num2b = legs_num2b + 1
								end
							end
							
							if gg3 == 0 then
								legs_data2b[legs_num2b] = {}
								legs_data2b[legs_num2b][1] = "RW" .. find_rwy
								--legs_data2b[legs_num2b][2] = math.rad((rnw_data[idx_rec][8]+ mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4])) % 360)		-- brg
								legs_data2b[legs_num2b][2] = math.rad(rnw_data[idx_rec][8])
								legs_data2b[legs_num2b][3] = 0		-- distance
								legs_data2b[legs_num2b][4] = 0		-- speed
								legs_data2b[legs_num2b][5] = 0		-- altitude
								legs_data2b[legs_num2b][6] = 0	-- altitude type
								calc_brg_dist(rnw_data[idx_rec][3], rnw_data[idx_rec][4], math.rad(rnw_data[idx_rec][8]), rnw_data[idx_rec][7] / 1852 * 0.7);
								legs_data2b[legs_num2b][7] = calc_lat	--rnw_data[idx_rec][3]	--rnw_data[idx_rec][3]		-- latitude
								legs_data2b[legs_num2b][8] = calc_lon	--rnw_data[idx_rec][4]	--rnw_data[idx_rec][4]		-- longitude
								legs_data2b[legs_num2b][9] = find_icao			-- via id
								legs_data2b[legs_num2b][10] = 0		-- calc speed
								if in_flight_mode == 0 or rte12_act ~= add_sid_star_app_act then
									legs_data2b[legs_num2b][11] = find_alt		-- calc altitude
								else
									legs_data2b[legs_num2b][11] = find_alt + 50		-- calc altitude
								end
								legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
								legs_data2b[legs_num2b][13] = 0
								legs_data2b[legs_num2b][14] = 0		-- rest alt
								legs_data2b[legs_num2b][15] = 0		-- last fuel
								legs_data2b[legs_num2b][16] = ""
								legs_data2b[legs_num2b][17] = 0	--200		-- 0-none, 1-FAF, 2-FMA, 3-IAF, 4-start leg
								--legs_data2b[legs_num2b][18] = math.rad(rnw_data[idx_rec][8])
								legs_data2b[legs_num2b][18] = math.rad((rnw_data[idx_rec][8] + mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4]) + 360) % 360)		-- brg
								legs_data2b[legs_num2b][19] = 1		-- 0-none, 1-SID, 2-STAR, 3-APP
								legs_data2b[legs_num2b][20] = 0
								legs_data2b[legs_num2b][21] = -1
								legs_data2b[legs_num2b][22] = ""
								legs_data2b[legs_num2b][23] = 0
								legs_data2b[legs_num2b][24] = 0
								legs_data2b[legs_num2b][25] = 0
								legs_data2b[legs_num2b][26] = rnw_data[idx_rec][7]	-- runway lenght for runway
								legs_data2b[legs_num2b][27] = ""
								legs_data2b[legs_num2b][28] = ""
								legs_data2b[legs_num2b][29] = string.format("%04d", math.deg(legs_data2b[legs_num2b][18]) * 10)
								legs_data2b[legs_num2b][30] = ""
								legs_data2b[legs_num2b][31] = "TF"
								legs_data2b[legs_num2b][32] = 1		--flyover wpt
								legs_data2b[legs_num2b][33] = ""
								legs_data2b[legs_num2b][34] = ""
								legs_data2b[legs_num2b][35] = ""
								legs_data2b[legs_num2b][36] = 4
								legs_data2b[legs_num2b][37] = 0
								legs_data2b[legs_num2b][38] = ""
								legs_data2b[legs_num2b][39] = ""
								legs_data2b[legs_num2b][40] = 0
								legs_data2b[legs_num2b][41] = 0
								legs_data2b[legs_num2b][42] = 0
								legs_data2b[legs_num2b][43] = ""
								legs_data2b[legs_num2b][44] = 0
								legs_data2b[legs_num2b][45] = 0
								legs_data2b[legs_num2b][46] = calc_lat	--rnw_data[idx_rec][5]		-- latitude end
								legs_data2b[legs_num2b][47] = calc_lon	--rnw_data[idx_rec][6]		-- longitude end
								legs_data2b[legs_num2b][48] = ""
								legs_data2b[legs_num2b][49] = 0
								legs_data2b[legs_num2b][50] = 0
								legs_data2b[legs_num2b][51] = 0
								legs_data2b[legs_num2b][52] = 0
								legs_data2b[legs_num2b][53] = 0
								legs_data2b[legs_num2b][54] = 0
							end
							rnw_added = 1
							break
						end
					end
				end
			end
		end
		if find_sid == "------" then
			rte_add_sid_act = 4
		end
	end
	
	-- add SID
	if rte_add_sid_act == 3 then
		if rte_sid_num > 0 then
			
			rte_lat = 0
			rte_lon = 0
			if find_sid ~= "------" then
				gg = sid_cnt
				if gg <= rte_sid_num then
					rnw_ok = 0
					rnw_txt = "RW" .. find_rwy
					rnw_txt2 = "RW"
					if rte_sid[gg][2] == "ALL" or rte_sid[gg][2] == "" then
						rnw_ok = 1
					end
					if rte_sid[gg][2] == rnw_txt then
						rnw_ok = 1
					end
					
					-- RWxxB -> L, R, C
					if string.sub(rte_sid[gg][2], -1, -1) == "B" then
						if string.len(rnw_txt) > 4 then
							rnw_txt2 = string.sub(rnw_txt, 1, -2)
						end
						if string.sub(rte_sid[gg][2], 1, -2) == rnw_txt2 then
							rnw_ok = 1
						end
					end
					
					tns_ok = 0
					if rte_sid[gg][2] == find_sid_tns then
						tns_ok = 1
					end
					
					rnw_ok2 = 0
					if string.sub(rte_sid[gg][2], 1, 2) == "RW" then
						rnw_ok2 = 1
					end
					
					if rte_sid[gg][1] == find_sid then --and string.sub(rte_sid[gg][3], 1, 2) ~= "RW" then
						
						if wrong_sid_runway ~= 0 and wrong_rw == 0 then
							
							if add_ok == 0 then
								if offset_sid_mode == 2 or offset_sid_mode == 3 then
									if (offset_sid_idx2 > offset_sid_idx + 1) and offset_sid_mode == 3 then
										legs_num2b = offset_sid_idx2
									else
										legs_num2b = offset_sid_idx + 1
									end
									if legs_data2b[2][1] ~= "DISCONTINUITY" then
										-- create disco
										if rnw_added == 0 and offset_sid_mode ~= 3 then
											rte_add_discob(legs_num2b)
											add_ok = 1
										else
											legs_num2b = legs_num2b + 1
											rte_add_discob(legs_num2b)
										end
									end
								elseif offset_sid_mode == 0 then
									if legs_data2b[2][1] ~= "DISCONTINUITY" then
										if rnw_added == 0 then
											rte_copyb(2)
											-- create disco
											legs_num2b = 2
											rte_add_discob(2)
											add_ok = 1
										else
											-- create disco
											legs_num2b = legs_num2b + 1
											rte_add_discob(legs_num2b)
										end
									end
								else
									if legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
										if rnw_added == 0 then
											rte_copyb(legs_num2b + 1)
											add_ok = 1
										end
										-- create disco
										legs_num2b = legs_num2b + 1
										rte_add_discob(legs_num2b)
									end
								end
							end
							wrong_rw = 1
						end
						
						add_sid_rte_wpt = false
						if rnw_ok == 0 and rnw_ok2 == 1 then
							if rnw_added == 0 or wrong_sid_runway ~= 0 then
								if rte_sid[gg][2] == " " then
									wpt_sid_idx = rte_sid[gg][25] + 10000
								else
									wpt_sid_idx = rte_sid[gg][25] + 13000
								end
								if wpt_sid_idx > added_sid_idx then
									add_sid_rte_wpt = true
								end
							end
						elseif rnw_ok == 1 then
							add_sid_rte_wpt = true
						elseif tns_ok == 1 then
							add_sid_rte_wpt = true
						end
						
						-- if (rnw_ok == 1 or rnw_ok2 == 1) or tns_ok == 1 then
						if add_sid_rte_wpt == true then
							
							add_wpt = 0
							
							ii = 0
							if gg > 1 and legs_num2b > 1 then
								
								if legs_data2b[legs_num2b-1][31] == "CI" or legs_data2b[legs_num2b-1][31] == "VI" then
									ii = 1
								end
							end
							
							if rte_sid[gg][15] == "IF" and ii == 0 then
								
								disco_status = 1
								if legs_data2b[legs_num2b][19] == 1 or legs_data2b[legs_num2b][19] == 3 or legs_data2b[legs_num2b][19] == 10 then
									disco_status = 0
								end
								
								if disco_status == 0 then
									
									prev_ok = 0
									if legs_num2b > 1 then
										if rte_sid[gg][3] == legs_data2b[legs_num2b-1][1] then
											prev_ok = 1
										end
									end
									
									if rte_sid[gg][3] == legs_data2b[legs_num2b][1] and rte_sid[gg][3] ~= "" then
										
										if legs_data2b[legs_num2b][1] == "(VECTOR)" and prev_ok ~= 0 then
											-- skip VECTOR
											legs_num2b = legs_num2b - 1
										end
										xx_temp = tonumber(rte_sid[gg][4])
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][4] = xx_temp		-- speed
										if rte_sid[gg][24] == "-" then
											legs_data2b[legs_num2b][42] = 1
										elseif rte_sid[gg][24] == "+" then
											legs_data2b[legs_num2b][42] = 2
										else
											legs_data2b[legs_num2b][42] = 0
										end
										
										if string.sub(rte_sid[gg][5], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_sid[gg][5], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_sid[gg][5])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][5] = xx_temp		-- altitude
										if string.sub(rte_sid[gg][23], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_sid[gg][23], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_sid[gg][23])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
										xx_temp = 0
										if rte_sid[gg][6] == "+" then
											xx_temp = 43
										elseif rte_sid[gg][6] == "-" then
											xx_temp = 45
										elseif rte_sid[gg][6] == "B" then
											xx_temp = 41
										end
										legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
										
										if tns_ok == 0 then
											if rte_sid[gg][2] == " " then
												added_sid_idx = rte_sid[gg][25] + 10000
											else
												added_sid_idx = rte_sid[gg][25] + 13000
											end
											legs_data2b[legs_num2b][44] = added_sid_idx
										else
											added_sid_tns_idx = rte_sid[gg][25] + 16000
											legs_data2b[legs_num2b][44] = added_sid_tns_idx
										end
										
									else
										add_wpt = 1
									end
								else
									
									if add_ok == 0 and rnw_added == 0 then
										-- if in_flight_mode == 0 or rte12_act ~= add_sid_star_app_act then
										if offset_sid_mode == 2 or offset_sid_mode == 3 then
											if (offset_sid_idx2 > offset_sid_idx + 1) and offset_sid_mode == 3 then
												legs_num2b = offset_sid_idx2
											else
												legs_num2b = offset_sid_idx
											end
										elseif offset_sid_mode == 0 then
											if legs_data2b[2][1] == "DISCONTINUITY" then
												if legs_data2b[2][1] == find_icao then
													rte_copyb(2)
												else
													rte_copyb(3)
												end
											else
												rte_copyb(2)
											end
											legs_num2b = 1
										else
											rte_copyb(legs_num2b + 1)
										end
									end
									
									-- if in_flight_mode == 0 or rte12_act ~= add_sid_star_app_act then
									if offset_sid_mode == 0 then
										if legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
											legs_num2b = legs_num2b + 1
											rte_add_discob(legs_num2b)
											-- -- add waypoint
											-- add_wpt = 1
										end
									-- else
										-- -- add waypoint
										-- add_wpt = 1
									end
									add_wpt = 1
								end
							else
								
								if add_ok == 0 and rnw_added == 0 then
									
									if offset_sid_mode == 2 or offset_sid_mode == 3 then
										if (offset_sid_idx2 > offset_sid_idx + 1) and offset_sid_mode == 3 then
											legs_num2b = offset_sid_idx2
										else
											legs_num2b = offset_sid_idx
										end
									elseif in_flight_mode == 0 or rte12_act ~= add_sid_star_app_act then
										if legs_data2b[2][1] == "DISCONTINUITY" then
											rte_copyb(3)
										else
											rte_copyb(2)
										end
										legs_num2b = 1
									else
										rte_copyb(legs_num2b + 1)
									end
								end
								add_wpt = 1
								
							end
							
							if add_wpt == 1 then
								
								add_ok = 1
								legs_num2b = legs_num2b + 1
								legs_data2b[legs_num2b] = {}
								
								if rte_sid[gg][15] == "HA" or rte_sid[gg][15] == "HF" or rte_sid[gg][15] == "HM" then
									-- HOLD
									
									if legs_num2b > 1 then
										if legs_data2b[legs_num2b-1][1] ~= rte_sid[gg][3] then
											-- add HOLD WPT
											legs_data2b[legs_num2b][1] = rte_sid[gg][3]	--entry
											legs_data2b[legs_num2b][2] = 0		-- brg
											legs_data2b[legs_num2b][3] = 0		-- distance
											legs_data2b[legs_num2b][4] = 0		-- speed
											legs_data2b[legs_num2b][5] = 0		-- altitude fpa
											legs_data2b[legs_num2b][6] = 0	-- altitude type
											legs_data2b[legs_num2b][7] = 0	-- latitude
											legs_data2b[legs_num2b][8] = 0	-- longitude
											legs_data2b[legs_num2b][9] = sid_via		-- via id
											legs_data2b[legs_num2b][10] = 0		-- calc speed
											legs_data2b[legs_num2b][11] = 0		-- calc altitude
											legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
											legs_data2b[legs_num2b][13] = 0
											legs_data2b[legs_num2b][14] = 0		-- rest alt
											legs_data2b[legs_num2b][15] = 0		-- last fuel
											legs_data2b[legs_num2b][16] = rte_sid[gg][12] --navaid_list[1][8]		-- reg code
											legs_data2b[legs_num2b][17] = rte_sid[gg][17]	--201		-- FAF
											legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
											legs_data2b[legs_num2b][19] = 1		-- 0-none, 1-SID, 2-STAR/APP, 7 - APP
											legs_data2b[legs_num2b][20] = 0
											legs_data2b[legs_num2b][21] = -1	--rte_sid[gg][9]
											legs_data2b[legs_num2b][22] = rte_sid[gg][18]
											legs_data2b[legs_num2b][23] = 0
											legs_data2b[legs_num2b][24] = 0
											legs_data2b[legs_num2b][25] = 0
											legs_data2b[legs_num2b][26] = 0
											legs_data2b[legs_num2b][27] = rte_sid[gg][13]
											legs_data2b[legs_num2b][28] = rte_sid[gg][14]
											legs_data2b[legs_num2b][30] = rte_sid[gg][10]
											if rte_sid[gg][3] == legs_data2b[legs_num2b-1][1] and legs_data2b[legs_num2b-1][31] == "CF" and rte_sid[gg][15] == "IF" then
												legs_data2b[legs_num2b][31] = legs_data2b[legs_num2b-1][31]
												legs_data2b[legs_num2b][29] = legs_data2b[legs_num2b-1][29]
											else
												legs_data2b[legs_num2b][31] = "TF"		--rte_sid[gg][15]
												legs_data2b[legs_num2b][29] = rte_sid[gg][11]
											end
											legs_data2b[legs_num2b][32] = rte_sid[gg][16]
											if rte_sid[gg][22] == 1 then
												legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 10
											elseif rte_sid[gg][22] == 2 then
												legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 20
											end
											legs_data2b[legs_num2b][33] = rte_sid[gg][19]
											legs_data2b[legs_num2b][34] = rte_sid[gg][20]
											legs_data2b[legs_num2b][35] = rte_sid[gg][21]
											legs_data2b[legs_num2b][36] = 0
											legs_data2b[legs_num2b][37] = 0
											legs_data2b[legs_num2b][38] = ""
											legs_data2b[legs_num2b][39] = ""
											legs_data2b[legs_num2b][40] = 0
											legs_data2b[legs_num2b][41] = 0
											legs_data2b[legs_num2b][42] = 0
											legs_data2b[legs_num2b][43] = ""
											legs_data2b[legs_num2b][44] = 0
											legs_data2b[legs_num2b][45] = 0
											legs_data2b[legs_num2b][46] = 0
											legs_data2b[legs_num2b][47] = 0
											legs_data2b[legs_num2b][48] = ""
											legs_data2b[legs_num2b][49] = 0
											legs_data2b[legs_num2b][50] = 0
											legs_data2b[legs_num2b][51] = 0
											legs_data2b[legs_num2b][52] = 0
											legs_data2b[legs_num2b][53] = 0
											legs_data2b[legs_num2b][54] = 0
											
											xx_temp = tonumber(rte_sid[gg][4])
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][4] = xx_temp		-- speed
											if rte_sid[gg][24] == "-" then
												legs_data2b[legs_num2b][42] = 1
											elseif rte_sid[gg][24] == "+" then
												legs_data2b[legs_num2b][42] = 2
											else
												legs_data2b[legs_num2b][42] = 0
											end
											-- xx_temp = tonumber(rte_sid[gg][5])
											-- if xx_temp == nil then
												-- xx_temp = 0
											-- end
											if string.sub(rte_sid[gg][5], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_sid[gg][5], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_sid[gg][5])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][5] = xx_temp		-- altitude
											if string.sub(rte_sid[gg][23], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_sid[gg][23], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_sid[gg][23])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
											xx_temp = 0
											if rte_sid[gg][6] == "+" then
												xx_temp = 43
											elseif rte_sid[gg][6] == "-" then
												xx_temp = 45
											elseif rte_sid[gg][6] == "B" then
												xx_temp = 41
											end
											legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
											
											if tns_ok == 0 then
												if rte_sid[gg][2] == " " then
													added_sid_idx = rte_sid[gg][25] + 10000
												else
													added_sid_idx = rte_sid[gg][25] + 13000
												end
												legs_data2b[legs_num2b][44] = added_sid_idx
											else
												added_sid_tns_idx = rte_sid[gg][25] + 16000
												legs_data2b[legs_num2b][44] = added_sid_tns_idx
											end
											
											legs_num2b = legs_num2b + 1
											legs_data2b[legs_num2b] = {}
										end
									end
								end
								
								if rte_sid[gg][3] == legs_data2b[legs_num2b-1][1] then
									if rte_sid[gg][15] == "VM" or rte_sid[gg][15] == "FM" then		-- VECTOR
										-- VECTOR
										legs_data2b[legs_num2b][1] = "(VECTOR)"	-- id WPT
									elseif rte_sid[gg][15] == "HA" or rte_sid[gg][15] == "HF" or rte_sid[gg][15] == "HM" then
										legs_data2b[legs_num2b][1] = rte_sid[gg][3]
									elseif string.sub(rte_sid[gg][15], 2, 2) == "A" then
										-- (1000)
										jj = tonumber(rte_sid[gg][5])
										if jj == nil then
											legs_data2b[legs_num2b][1] = "(----)"	-- id WPT
										else
											legs_data2b[legs_num2b][1] = "(" .. tostring(jj) .. ")"
										end
									elseif rte_sid[gg][15] == "PI" then
										-- Proc Turn
										legs_data2b[legs_num2b][1] = rte_sid[gg][3]	-- id WPT
									elseif string.sub(rte_sid[gg][15], 2, 2) == "I" then
										-- (INTC)
										legs_data2b[legs_num2b][1] = "(INTC)"	-- id WPT
									elseif rte_sid[gg][15] == "CF" then
										-- (INTC)
										legs_data2b[legs_num2b][1] = rte_sid[gg][3]	--"(INTC)"	-- id WPT
									elseif string.sub(rte_sid[gg][15], 2, 2) == "R" then
										--(ABC180)
										jj = tonumber(rte_sid[gg][11])
										temp_str = ""
										if jj ~= nil then
											jj = jj / 10
											temp_str = string.format("%03d", jj)
										else
											temp_str = "000"
										end
										legs_data2b[legs_num2b][1] = "(" .. rte_sid[gg][13] .. temp_str .. ")"
									elseif string.sub(rte_sid[gg][15], 2, 2) == "D" then
										--(ABC-10)
										jj = tonumber(rte_sid[gg][10])
										temp_str = ""
										if jj ~= nil then
											jj = jj / 10
											temp_str = string.format("%02d", jj)
										else
											temp_str = "00"
										end
										legs_data2b[legs_num2b][1] = "(" .. rte_sid[gg][13] .. "-" .. temp_str .. ")"
									elseif rte_sid[gg][15] == "FC" then
										--(ABC)
										legs_data2b[legs_num2b][1] = "(" .. rte_sid[gg][3]  .. ")"
									else
										legs_data2b[legs_num2b][1] = rte_sid[gg][3]
									end
									
									if tns_ok == 0 then
										if rte_sid[gg][2] == " " then
											added_sid_idx = rte_sid[gg][25] + 10000
										else
											added_sid_idx = rte_sid[gg][25] + 13000
										end
										legs_data2b[legs_num2b][44] = added_sid_idx
									else
										added_sid_tns_idx = rte_sid[gg][25] + 16000
										legs_data2b[legs_num2b][44] = added_sid_tns_idx
									end
								else
									legs_data2b[legs_num2b][1] = rte_sid[gg][3]	-- id WPT
								end
								xx_temp = tonumber(rte_sid[gg][7])
								if xx_temp == nil then
									xx_temp = 0
								end
								xx_temp = xx_temp / 10
								legs_data2b[legs_num2b][2] = math.rad(xx_temp)		-- brg
								legs_data2b[legs_num2b][3] = 0		-- distance
								
								if rte_sid[gg][15] == "HA" or rte_sid[gg][15] == "HF" or rte_sid[gg][15] == "HM" then
									-- HOLD
									xx_temp = tonumber(rte_sid[gg][4])
									if xx_temp == nil then
										xx_temp = legs_data2b[legs_num2b-1][4]
									end
									legs_data2b[legs_num2b][4] = xx_temp		-- speed
									if rte_sid[gg][24] == "-" then
										legs_data2b[legs_num2b][42] = 1
									elseif rte_sid[gg][24] == "+" then
										legs_data2b[legs_num2b][42] = 2
									else
										legs_data2b[legs_num2b][42] = 0
									end
									
									if string.sub(rte_sid[gg][5], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_sid[gg][5], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_sid[gg][5])
									end
									if xx_temp == nil then
										legs_data2b[legs_num2b][5] = legs_data2b[legs_num2b-1][5]
										legs_data2b[legs_num2b][6] = legs_data2b[legs_num2b-1][6]
										legs_data2b[legs_num2b][41] = legs_data2b[legs_num2b-1][41]
									else
										legs_data2b[legs_num2b][5] = xx_temp		-- altitude
										if string.sub(rte_sid[gg][23], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_sid[gg][23], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_sid[gg][23])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
										xx_temp = 0
										if rte_sid[gg][6] == "+" then
											xx_temp = 43
										elseif rte_sid[gg][6] == "-" then
											xx_temp = 45
										elseif rte_sid[gg][6] == "B" then
											xx_temp = 41
										end
										legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
									end
								else
									xx_temp = tonumber(rte_sid[gg][4])
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][4] = xx_temp		-- speed
									if rte_sid[gg][24] == "-" then
										legs_data2b[legs_num2b][42] = 1
									elseif rte_sid[gg][24] == "+" then
										legs_data2b[legs_num2b][42] = 2
									else
										legs_data2b[legs_num2b][42] = 0
									end
									
									xx_temp = tonumber(rte_sid[gg][5])
									-- if xx_temp == nil then
										-- xx_temp = 0
									-- end
									if string.sub(rte_sid[gg][5], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_sid[gg][5], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_sid[gg][5])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][5] = xx_temp		-- altitude
									if string.sub(rte_sid[gg][23], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_sid[gg][23], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_sid[gg][23])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
									xx_temp = 0
									if rte_sid[gg][6] == "+" then
										xx_temp = 43
									elseif rte_sid[gg][6] == "-" then
										xx_temp = 45
									elseif rte_sid[gg][6] == "B" then
										xx_temp = 41
									end
									legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
								end
								legs_data2b[legs_num2b][7] = 0		-- latitude
								legs_data2b[legs_num2b][8] = 0		-- longitude
								legs_data2b[legs_num2b][9] = sid_via			-- via id
								legs_data2b[legs_num2b][10] = 0		-- calc speed
								legs_data2b[legs_num2b][11] = 0		-- calc altitude
								legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
								legs_data2b[legs_num2b][13] = 0
								legs_data2b[legs_num2b][14] = 0		-- rest alt
								legs_data2b[legs_num2b][15] = 0		-- last fuel
								legs_data2b[legs_num2b][16] = rte_sid[gg][12]
								legs_data2b[legs_num2b][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
								legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
								legs_data2b[legs_num2b][19] = 1		-- 0-none, 1-SID, 2-STAR, 3-APP
								xx_temp = tonumber(rte_sid[gg][8])
								if xx_temp == nil then
									legs_data2b[legs_num2b][20] = 0	-- vpa
								else
									legs_data2b[legs_num2b][20] = xx_temp / 100	-- vpa
								end
								legs_data2b[legs_num2b][21] = rte_sid[gg][9]
								legs_data2b[legs_num2b][22] = rte_sid[gg][18]
								legs_data2b[legs_num2b][23] = 0
								legs_data2b[legs_num2b][24] = 0
								legs_data2b[legs_num2b][25] = 0
								legs_data2b[legs_num2b][26] = 0
								legs_data2b[legs_num2b][27] = rte_sid[gg][13]
								legs_data2b[legs_num2b][28] = rte_sid[gg][14]
								-- legs_data2b[legs_num2b][29] = rte_sid[gg][11]
								legs_data2b[legs_num2b][30] = rte_sid[gg][10]
								if rte_sid[gg][3] == legs_data2b[legs_num2b-1][1] and legs_data2b[legs_num2b-1][31] == "CF" and rte_sid[gg][15] == "IF" then
									legs_data2b[legs_num2b][31] = legs_data2b[legs_num2b-1][31]
									legs_data2b[legs_num2b][29] = legs_data2b[legs_num2b-1][29]
								else
									legs_data2b[legs_num2b][31] = rte_sid[gg][15]
									legs_data2b[legs_num2b][29] = rte_sid[gg][11]
								end
								legs_data2b[legs_num2b][32] = rte_sid[gg][16]
								if rte_sid[gg][22] == 1 then
									legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 10
								elseif rte_sid[gg][22] == 2 then
									legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 20
								end
								legs_data2b[legs_num2b][33] = rte_sid[gg][19]
								legs_data2b[legs_num2b][34] = rte_sid[gg][20]
								legs_data2b[legs_num2b][35] = rte_sid[gg][21]
								legs_data2b[legs_num2b][36] = 0
								legs_data2b[legs_num2b][37] = 0
								legs_data2b[legs_num2b][38] = ""
								legs_data2b[legs_num2b][39] = ""
								legs_data2b[legs_num2b][40] = 0
								--legs_data2b[legs_num2b][42] = 0
								--legs_data2b[legs_num2b][41] = 0
								legs_data2b[legs_num2b][43] = ""
								legs_data2b[legs_num2b][44] = 0
								legs_data2b[legs_num2b][45] = 0
								legs_data2b[legs_num2b][46] = 0
								legs_data2b[legs_num2b][47] = 0
								legs_data2b[legs_num2b][48] = ""
								legs_data2b[legs_num2b][49] = 0
								legs_data2b[legs_num2b][50] = 0
								legs_data2b[legs_num2b][51] = 0
								legs_data2b[legs_num2b][52] = 0
								legs_data2b[legs_num2b][53] = 0
								legs_data2b[legs_num2b][54] = 0
								
								if tns_ok == 0 then
									if rte_sid[gg][2] == " " then
										added_sid_idx = rte_sid[gg][25] + 10000
									else
										added_sid_idx = rte_sid[gg][25] + 13000
									end
									legs_data2b[legs_num2b][44] = added_sid_idx
								else
									added_sid_tns_idx = rte_sid[gg][25] + 16000
									legs_data2b[legs_num2b][44] = added_sid_tns_idx
								end
							end
						end
					end
				else 
					--rte_add_sid_act = 4
				end
			else
				--rte_add_sid_act = 4
			end
		else
			--rte_add_sid_act = 4
		end
	end
	
	if rte_add_sid_act == 4 then
		
		
		if add_ok == 1 or rnw_added == 1 then
			if legs_data2_tmpb[1][1] ~= legs_data2b[legs_num2b][1] and legs_data2_tmp_nb > 1 
			and legs_data2_tmpb[1][1] ~= "DISCONTINUITY" then --and add_ok ~= 1 then
				-- if legs_data2_tmpb[1][9] ~= "DIRECT" then
					-- create DISCONTINUITY
					legs_num2b = legs_num2b + 1
					rte_add_discob(legs_num2b)
					rte_pasteb(legs_num2b + 1)
				-- else
					-- rte_pasteb(legs_num2b + 1)
				-- end
			else
				
				if legs_data2_tmpb[1][1] == legs_data2b[legs_num2b][1] and legs_data2_tmpb[1][16] == legs_data2b[legs_num2b][16] then
					
					-- change to SID
					legs_data2_tmpb[1][9] = sid_via
					legs_data2_tmpb[1][19] = 3
					-- lat/lon
					legs_data2_tmpb[1][7] = legs_data2b[legs_num2b][7]
					legs_data2_tmpb[1][8] = legs_data2b[legs_num2b][8]
					-- path terminator
					legs_data2_tmpb[1][31] = legs_data2b[legs_num2b][31]
					legs_data2_tmpb[1][18] = legs_data2b[legs_num2b][18]
					legs_data2_tmpb[1][21] = legs_data2b[legs_num2b][21]
					legs_data2_tmpb[1][29] = legs_data2b[legs_num2b][29]
					legs_data2_tmpb[1][32] = legs_data2b[legs_num2b][32]
					legs_data2_tmpb[1][33] = legs_data2b[legs_num2b][33]
					legs_data2_tmpb[1][23] = legs_data2b[legs_num2b][23]
					legs_data2_tmpb[1][24] = legs_data2b[legs_num2b][24]
					legs_data2_tmpb[1][27] = legs_data2b[legs_num2b][27]
					legs_data2_tmpb[1][28] = legs_data2b[legs_num2b][28]
					legs_data2_tmpb[1][30] = legs_data2b[legs_num2b][30]
					legs_data2_tmpb[1][34] = legs_data2b[legs_num2b][34]
					legs_data2_tmpb[1][35] = legs_data2b[legs_num2b][35]
					legs_data2_tmpb[1][22] = legs_data2b[legs_num2b][22]
					legs_data2_tmpb[1][43] = legs_data2b[legs_num2b][43]
					legs_data2_tmpb[1][44] = legs_data2b[legs_num2b][44]
					legs_data2_tmpb[1][45] = legs_data2b[legs_num2b][45]
					legs_data2_tmpb[1][46] = legs_data2b[legs_num2b][46]
					legs_data2_tmpb[1][47] = legs_data2b[legs_num2b][47]
					legs_data2_tmpb[1][48] = legs_data2b[legs_num2b][48]
					legs_data2_tmpb[1][49] = legs_data2b[legs_num2b][49]
					legs_data2_tmpb[1][50] = legs_data2b[legs_num2b][50]
					legs_data2_tmpb[1][51] = legs_data2b[legs_num2b][51]
					legs_data2_tmpb[1][52] = legs_data2b[legs_num2b][52]
					legs_data2_tmpb[1][53] = legs_data2b[legs_num2b][53]
					legs_data2_tmpb[1][54] = legs_data2b[legs_num2b][54]
					-- speed restrict
					if legs_data2b[legs_num2b][4] ~= 0 then
						legs_data2_tmpb[1][4] = legs_data2b[legs_num2b][4]
						legs_data2_tmpb[1][42] = legs_data2b[legs_num2b][42]
					end
					-- altitude restrict
					if legs_data2b[legs_num2b][5] ~= 0 then
						legs_data2_tmpb[1][5] = legs_data2b[legs_num2b][5]
						legs_data2_tmpb[1][6] = legs_data2b[legs_num2b][6]
						legs_data2_tmpb[1][41] = legs_data2b[legs_num2b][41]
					end
					---
					rte_pasteb(legs_num2b)
				else
					rte_pasteb(legs_num2b + 1)
				end
			end
		end
	end
	if rte_add_sid_act == 2 then
		rte_add_sid_act = 3
	elseif rte_add_sid_act == 4 then
		rte_add_sid_act = 5
	end
	
	
end

--function rte_add_star_app()

function rte_add_star2()
	
	local ii = 0
	local jj = 0
	local kk = 0
	local gg = 0
	local gg2 = 0
	local xx_temp = 0
	local rnw_ok  = 0
	local rnw_txt = ""
	local rnw_txt2 = ""
	local tns_ok = 0
	local disco_status = 0
	local only_one_star = 0
	local add_wpt = 0
	local skip_wpt = 0
	local wpt_idx_cifp = 0
	local prev_ok = 0
	
	local temp_str = ""
	
	local find_star = ""
	local find_star_tns = ""
	local find_star_tns2 = ""
	local find_app = ""
	local star_via = ""
	
	if add_sid_star_app_act == 0 then
		find_star = des_star2
		find_star_tns = des_star_trans
		find_star_tns2 = des_star_trans2
		find_app = des_app2
	else
		find_star = des_star8
		find_star_tns = des_star_trans7
		find_star_tns2 = des_star_trans8
		find_app = des_app8
	end
	if find_star_tns2 == "------" then
		star_via = find_star
	else
		star_via = find_star .. "." .. find_star_tns2
		if string.len(star_via) > 15 then
			star_via = string.sub(star_via, 1, 15)
		end
	end
	
	rte_lat = 0
	rte_lon = 0
	
	-- add STAR and TRANS STAR
	if rte_star_num > 0 then
		
		if find_star ~= "------" then
			
			if rte_star_num > 1 then
				for gg = 1, rte_star_num do
					if rte_star[gg][1] == find_star then
						if rnw_ok == 0 then
							rnw_txt = rte_star[gg][2]
							rnw_ok = 1
							only_one_star = 1
						else
							if rte_star[gg][2] ~= " " then
								if rte_star[gg][2] ~= rnw_txt then
									only_one_star = 0
								end
							end
						end
					end
				end
			end
			
			rnw_ok = 0
			
			gg = sid_cnt
			if gg <= rte_star_num then
				rnw_ok = 0
				rnw_txt = "RW"
				rnw_txt2 = "RW"
				if find_app ~= "------" then
					if string.sub(find_app, 1, 2) == "RW" then
						rnw_txt = find_app
					elseif string.len(find_app) > 4 then
						jj, kk = string.find(find_app, "-")
						if jj == nil then
							rnw_txt = rnw_txt .. string.sub(find_app, 2, -2)
						else
							rnw_txt = rnw_txt .. string.sub(find_app, 2, jj-1)
						end
					else
						rnw_txt = rnw_txt .. string.sub(find_app, 2, -1)
					end
				end
				-- RWxx
				if rte_star[gg][2] == rnw_txt then
					rnw_ok = 1
				end
				
				-- RWxxB -> L, R, C
				if string.sub(rte_star[gg][2], -1, -1) == "B" then
					if string.len(rnw_txt) > 4 then
						rnw_txt2 = string.sub(rnw_txt, 1, -2)
					end
					if string.sub(rte_star[gg][2], 1, -2) == rnw_txt2 then
						rnw_ok = 1
					end
				end
				
				-- ALL
				if rte_star[gg][2] == "ALL" or rte_star[gg][2] == " " then
					rnw_ok = 1
				end
				
				tns_ok = 0
				if rte_star[gg][2] == find_star_tns2 then
					tns_ok = 1
				end
				
				if rte_star[gg][1] == find_star then
					wpt_idx_cifp = rte_star[gg][25]
					if wpt_idx_cifp ~= 0 then
						if rte_star[gg][2] == find_star_tns2 then
							wpt_idx_cifp = wpt_idx_cifp + 20000		--20000 + idx
						elseif rte_star[gg][2] == " " then
							wpt_idx_cifp = wpt_idx_cifp + 30000		--30000 + idx
						else
							wpt_idx_cifp = wpt_idx_cifp + 31000		--31000 + idx
						end
					end
					
					skip_wpt = 0
					if offset > 0 and offset <= legs_num and legs_num > 1 then
						if find_star == des_star and legs_data[offset][44] ~= 0 then
							if legs_data[offset][44] > 40000 then
								skip_wpt = 1
							elseif wpt_idx_cifp ~= 0 then
								if legs_data[offset][44] > 30000 then
									if wpt_idx_cifp <= legs_data[offset][44] then
										skip_wpt = 1
									end
								else
									if find_star_tns2 == find_star_tns then
										if wpt_idx_cifp <= legs_data[offset][44] then
											skip_wpt = 1
										end
									end
								end
							end
						end
					end
					if (rnw_ok == 1 or tns_ok == 1 or only_one_star == 1) and skip_wpt == 0 then
						
						add_wpt = 0
						
						ii = 0
						if gg > 1 and legs_num2b > 1 then
							if (legs_data2b[legs_num2b][19] == 0 or legs_data2b[legs_num2b][19] == 1 or legs_data2b[legs_num2b][19] == 3 or legs_data2b[legs_num2b][19] == 10) and add_star2 == 0 then
								ii = 0
							elseif legs_data2b[legs_num2b-1][31] == "CI" or legs_data2b[legs_num2b-1][31] == "VI" then
								ii = 1
							end
						end
						
						if rte_star[gg][15] == "IF" and ii == 0 then
							disco_status = 1
							if legs_data2b[legs_num2b][19] == 2 or legs_data2b[legs_num2b][19] == 4 then
								disco_status = 0
							-- elseif legs_data2b[legs_num2b][19] == 10 then
								-- disco_status = 0
							else
								if legs_data2b[legs_num2b][1] == rte_star[gg][3] then
									disco_status = 0
								end
							end
							
							if disco_status == 0 then
								prev_ok = 0
								if legs_num2b > 1 then
									if rte_star[gg][3] == legs_data2b[legs_num2b-1][1] then
										prev_ok = 1
									end
								end
								
								if rte_star[gg][3] == legs_data2b[legs_num2b][1] and rte_star[gg][3] ~= "" and rte_star[gg][12] == legs_data2b[legs_num2b][16] then
									if legs_data2b[legs_num2b][1] == "(VECTOR)" and prev_ok ~= 0 then
										-- skip VECTOR
										legs_num2b = legs_num2b - 1
									end
									if legs_data2b[legs_num2b][19] ~= 2 and legs_data2b[legs_num2b][19] ~= 10 then
										legs_data2b[legs_num2b][19] = 4	-- STAR APP before RTE
										first_add_star_idx = first_add_star_idx - 1
									end
									if legs_data2b[legs_num2b][19] == 1 then
										first_add_star_idx = first_add_star_idx - 1
									end
									-- change ALT, SPD restrict,..
									xx_temp = tonumber(rte_star[gg][4])
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][4] = xx_temp		-- speed
									if rte_star[gg][24] == "-" then
										legs_data2b[legs_num2b][42] = 1
									elseif rte_star[gg][24] == "+" then
										legs_data2b[legs_num2b][42] = 2
									else
										legs_data2b[legs_num2b][42] = 0
									end
									
									if string.sub(rte_star[gg][5], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_star[gg][5], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_star[gg][5])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][5] = xx_temp		-- altitude
									if string.sub(rte_star[gg][23], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_star[gg][23], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_star[gg][23])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
									xx_temp = 0
									if rte_star[gg][6] == "+" then
										xx_temp = 43
									elseif rte_star[gg][6] == "-" then
										xx_temp = 45
									elseif rte_star[gg][6] == "B" then
										xx_temp = 41
									end
									legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
								else
									if legs_data2b[legs_num2b][1] == "(VECTOR)" and prev_ok ~= 0 then
										-- skip VECTOR
										legs_num2b = legs_num2b - 1
										if legs_data2b[legs_num2b][19] ~= 2 and legs_data2b[legs_num2b][19] ~= 10 then
											legs_data2b[legs_num2b][19] = 4	-- STAR APP before RTE
										end
										-- change ALT, SPD restrict,..
										xx_temp = tonumber(rte_star[gg][4])
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][4] = xx_temp		-- speed
										if rte_star[gg][24] == "-" then
											legs_data2b[legs_num2b][42] = 1
										elseif rte_star[gg][24] == "+" then
											legs_data2b[legs_num2b][42] = 2
										else
											legs_data2b[legs_num2b][42] = 0
										end
										
										if string.sub(rte_star[gg][5], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_star[gg][5], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_star[gg][5])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][5] = xx_temp		-- altitude
										if string.sub(rte_star[gg][23], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_star[gg][23], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_star[gg][23])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
										xx_temp = 0
										if rte_star[gg][6] == "+" then
											xx_temp = 43
										elseif rte_star[gg][6] == "-" then
											xx_temp = 45
										elseif rte_star[gg][6] == "B" then
											xx_temp = 41
										end
										legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
									else
										if add_ok == 0 then
											rte_copyb(legs_num2b + 1)
										end
										add_wpt = 1
									end
								end
							else
								if legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
									if add_ok == 0 then
										rte_copyb(legs_num2b + 1)
									end
									-- create DISCONTINUITY
									legs_num2b = legs_num2b + 1
									rte_add_discob(legs_num2b)
									-- add waypoint
									add_wpt = 1
								end
							end
						else
							if add_ok == 0 then
								rte_copyb(legs_num2b + 1)
							end
							-- add waypoint
							add_wpt = 1
						end
						
						if add_wpt == 1 then
							add_star2 = 1
							add_ok = 1
							legs_num2b = legs_num2b + 1
							legs_data2b[legs_num2b] = {}
							
							if rte_star[gg][15] == "HA" or rte_star[gg][15] == "HF" or rte_star[gg][15] == "HM" then
								-- HOLD
								
								if legs_num2b > 1 then
									if legs_data2b[legs_num2b-1][1] ~= rte_star[gg][3] then
										-- add HOLD WPT
										legs_data2b[legs_num2b][1] = rte_star[gg][3]	--entry
										legs_data2b[legs_num2b][2] = 0		-- brg
										legs_data2b[legs_num2b][3] = 0		-- distance
										legs_data2b[legs_num2b][4] = 0		-- speed
										legs_data2b[legs_num2b][5] = 0		-- altitude fpa
										legs_data2b[legs_num2b][6] = 0	-- altitude type
										legs_data2b[legs_num2b][7] = 0	-- latitude
										legs_data2b[legs_num2b][8] = 0	-- longitude
										legs_data2b[legs_num2b][9] = ""		--"DIRECT" --find_app	--"DIRECT"			-- via id
										legs_data2b[legs_num2b][10] = 0		-- calc speed
										legs_data2b[legs_num2b][11] = 0		-- calc altitude
										legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
										legs_data2b[legs_num2b][13] = 0
										legs_data2b[legs_num2b][14] = 0		-- rest alt
										legs_data2b[legs_num2b][15] = 0		-- last fuel
										legs_data2b[legs_num2b][16] = rte_star[gg][12] --navaid_list[1][8]		-- reg code
										legs_data2b[legs_num2b][17] = rte_star[gg][17]	--201		-- FAF
										legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
										legs_data2b[legs_num2b][19] = 7		-- 0-none, 1-SID, 2-STAR/APP, 7 - APP
										legs_data2b[legs_num2b][20] = 0
										legs_data2b[legs_num2b][21] = -1		--rte_star[gg][9]
										legs_data2b[legs_num2b][22] = rte_star[gg][18]
										legs_data2b[legs_num2b][23] = 0
										legs_data2b[legs_num2b][24] = 0
										legs_data2b[legs_num2b][25] = 0
										legs_data2b[legs_num2b][26] = 0
										legs_data2b[legs_num2b][27] = rte_star[gg][13]
										legs_data2b[legs_num2b][28] = rte_star[gg][14]
										legs_data2b[legs_num2b][30] = rte_star[gg][10]
										if rte_star[gg][3] == legs_data2b[legs_num2b-1][1] and legs_data2b[legs_num2b-1][31] == "CF" and rte_star[gg][15] == "IF" then
											legs_data2b[legs_num2b][31] = legs_data2b[legs_num2b-1][31]
											legs_data2b[legs_num2b][29] = legs_data2b[legs_num2b-1][29]
										else
											legs_data2b[legs_num2b][31] = "TF"		--rte_star[gg][15]
											legs_data2b[legs_num2b][29] = rte_star[gg][11]
										end
										legs_data2b[legs_num2b][32] = rte_star[gg][16]
										if rte_star[gg][22] == 1 then
											legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 10
										elseif rte_star[gg][22] == 2 then
											legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 20
										end
										legs_data2b[legs_num2b][33] = rte_star[gg][19]
										legs_data2b[legs_num2b][34] = rte_star[gg][20]
										legs_data2b[legs_num2b][35] = rte_star[gg][21]
										legs_data2b[legs_num2b][36] = 0
										legs_data2b[legs_num2b][37] = 0
										legs_data2b[legs_num2b][38] = ""
										legs_data2b[legs_num2b][39] = ""
										legs_data2b[legs_num2b][40] = 0
										legs_data2b[legs_num2b][41] = 0
										legs_data2b[legs_num2b][42] = 0
										legs_data2b[legs_num2b][43] = ""
										legs_data2b[legs_num2b][44] = 0
										legs_data2b[legs_num2b][45] = 0
										legs_data2b[legs_num2b][46] = 0
										legs_data2b[legs_num2b][47] = 0
										legs_data2b[legs_num2b][48] = ""
										legs_data2b[legs_num2b][49] = 0
										legs_data2b[legs_num2b][50] = 0
										legs_data2b[legs_num2b][51] = 0
										legs_data2b[legs_num2b][52] = 0
										legs_data2b[legs_num2b][53] = 0
										legs_data2b[legs_num2b][54] = 0
										
										wpt_idx_cifp = rte_star[gg][25]
										if wpt_idx_cifp ~= 0 then
											if rte_star[gg][2] == find_star_tns2 then
												wpt_idx_cifp = wpt_idx_cifp + 20000		--20000 + idx
											elseif rte_star[gg][2] == " " then
												wpt_idx_cifp = wpt_idx_cifp + 30000		--30000 + idx
											else
												wpt_idx_cifp = wpt_idx_cifp + 31000		--31000 + idx
											end
										end
										legs_data2b[legs_num2b][44] = wpt_idx_cifp
										
										
										xx_temp = tonumber(rte_star[gg][4])
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][4] = xx_temp		-- speed
										if rte_star[gg][24] == "-" then
											legs_data2b[legs_num2b][42] = 1
										elseif rte_star[gg][24] == "+" then
											legs_data2b[legs_num2b][42] = 2
										else
											legs_data2b[legs_num2b][42] = 0
										end
										-- xx_temp = tonumber(rte_star[gg][5])
										-- if xx_temp == nil then
											-- xx_temp = 0
										-- end
										if string.sub(rte_star[gg][5], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_star[gg][5], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_star[gg][5])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][5] = xx_temp		-- altitude
										if string.sub(rte_star[gg][23], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_star[gg][23], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_star[gg][23])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
										xx_temp = 0
										if rte_star[gg][6] == "+" then
											xx_temp = 43
										elseif rte_star[gg][6] == "-" then
											xx_temp = 45
										elseif rte_star[gg][6] == "B" then
											xx_temp = 41
										end
										legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
										
										if legs_num2b == first_add_star_idx + 1 and legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
											legs_data2b[legs_num2b][9] = ""
										elseif legs_num2b == first_add_star_idx + 2 and legs_data2b[legs_num2b-1][1] == "DISCONTINUITY" then
											legs_data2b[legs_num2b][9] = ""
										else
											legs_data2b[legs_num2b][9] = star_via			-- via id
										end
										
										legs_num2b = legs_num2b + 1
										legs_data2b[legs_num2b] = {}
									end
								end
							end
							
							if rte_star[gg][3] == legs_data2b[legs_num2b-1][1] and rte_star[gg][12] == legs_data2b[legs_num2b-1][16] then
								if rte_star[gg][15] == "VM" or rte_star[gg][15] == "FM" then		-- VECTOR
									-- VECTOR
									legs_data2b[legs_num2b][1] = "(VECTOR)"	-- id WPT
								elseif rte_star[gg][15] == "HA" or rte_star[gg][15] == "HF" or rte_star[gg][15] == "HM" then
									legs_data2b[legs_num2b][1] = rte_star[gg][3]
								elseif string.sub(rte_star[gg][15], 2, 2) == "A" then
									-- (1000)
									jj = tonumber(rte_star[gg][5])
									if jj == nil then
										legs_data2b[legs_num2b][1] = "(----)"	-- id WPT
									else
										legs_data2b[legs_num2b][1] = "(" .. tostring(jj) .. ")"
									end
								elseif rte_star[gg][15] == "PI" then
									-- Proc Turn
									legs_data2b[legs_num2b][1] = rte_star[gg][3]	-- id WPT
								elseif string.sub(rte_star[gg][15], 2, 2) == "I" then
									-- (INTC)
									legs_data2b[legs_num2b][1] = "(INTC)"	-- id WPT
								elseif rte_star[gg][15] == "CF" then
									-- (INTC)
									legs_data2b[legs_num2b][1] = rte_star[gg][3]	--"(INTC)"	-- id WPT
								elseif string.sub(rte_star[gg][15], 2, 2) == "R" then
									--(ABC180)
									jj = tonumber(rte_star[gg][11])
									temp_str = ""
									if jj ~= nil then
										jj = jj / 10
										temp_str = string.format("%03d", jj)
									else
										temp_str = "000"
									end
									legs_data2b[legs_num2b][1] = "(" .. rte_star[gg][13] .. temp_str .. ")"
								elseif string.sub(rte_star[gg][15], 2, 2) == "D" then
									--(ABC-10)
									jj = tonumber(rte_star[gg][10])
									temp_str = ""
									if jj ~= nil then
										jj = jj / 10
										temp_str = string.format("%02d", jj)
									else
										temp_str = "00"
									end
									legs_data2b[legs_num2b][1] = "(" .. rte_star[gg][13] .. "-" .. temp_str .. ")"
								elseif rte_star[gg][15] == "FC" then
									--(ABC)
									legs_data2b[legs_num2b][1] = "(" .. rte_star[gg][3]  .. ")"
								else
									-- if (rte_star[gg][22] == 0 and legs_data2b[legs_num2b-1][32] < 10) 
									-- or (rte_star[gg][22] == 1 and legs_data2b[legs_num2b-1][32] > 9 and legs_data2b[legs_num2b-1][32] < 20) 
									-- or (rte_star[gg][22] == 2 and legs_data2b[legs_num2b-1][32] > 19) then
										-- legs_data2b[legs_num2b][1] = "(----)"	-- id WPT
									-- else
										legs_data2b[legs_num2b][1] = rte_star[gg][3]
									-- end
									--legs_data2b[legs_num2b][1] = "(----)"	-- id WPT
								end
							else
								legs_data2b[legs_num2b][1] = rte_star[gg][3]	-- id WPT
							end
							xx_temp = tonumber(rte_star[gg][7])
							if xx_temp == nil then
								xx_temp = 0
							end
							xx_temp = xx_temp / 10
							legs_data2b[legs_num2b][2] = math.rad(xx_temp)		-- brg
							legs_data2b[legs_num2b][3] = 0		-- distance
							
							if rte_star[gg][15] == "HA" or rte_star[gg][15] == "HF" or rte_star[gg][15] == "HM" then
								-- HOLD
								xx_temp = tonumber(rte_star[gg][4])
								if xx_temp == nil then
									xx_temp = legs_data2b[legs_num2b-1][4]
								end
								legs_data2b[legs_num2b][4] = xx_temp		-- speed
								if rte_star[gg][24] == "-" then
									legs_data2b[legs_num2b][42] = 1
								elseif rte_star[gg][24] == "+" then
									legs_data2b[legs_num2b][42] = 2
								else
									legs_data2b[legs_num2b][42] = 0
								end
								
								if string.sub(rte_star[gg][5], 1, 2) == "FL" then
									xx_temp = tonumber(string.sub(rte_star[gg][5], 3, -1)) * 100
								else
									xx_temp = tonumber(rte_star[gg][5])
								end
								if xx_temp == nil then
									legs_data2b[legs_num2b][5] = legs_data2b[legs_num2b-1][5]
									legs_data2b[legs_num2b][6] = legs_data2b[legs_num2b-1][6]
									legs_data2b[legs_num2b][41] = legs_data2b[legs_num2b-1][41]
								else
									legs_data2b[legs_num2b][5] = xx_temp		-- altitude
									if string.sub(rte_star[gg][23], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_star[gg][23], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_star[gg][23])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
									xx_temp = 0
									if rte_star[gg][6] == "+" then
										xx_temp = 43
									elseif rte_star[gg][6] == "-" then
										xx_temp = 45
									elseif rte_star[gg][6] == "B" then
										xx_temp = 41
									end
									legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
								end
							else
								xx_temp = tonumber(rte_star[gg][4])
								if xx_temp == nil then
									xx_temp = 0
								end
								legs_data2b[legs_num2b][4] = xx_temp		-- speed
								if rte_star[gg][24] == "-" then
									legs_data2b[legs_num2b][42] = 1
								elseif rte_star[gg][24] == "+" then
									legs_data2b[legs_num2b][42] = 2
								else
									legs_data2b[legs_num2b][42] = 0
								end
								-- xx_temp = tonumber(rte_star[gg][5])
								-- if xx_temp == nil then
									-- xx_temp = 0
								-- end
								if string.sub(rte_star[gg][5], 1, 2) == "FL" then
									xx_temp = tonumber(string.sub(rte_star[gg][5], 3, -1)) * 100
								else
									xx_temp = tonumber(rte_star[gg][5])
								end
								if xx_temp == nil then
									xx_temp = 0
								end
								legs_data2b[legs_num2b][5] = xx_temp		-- altitude
								if string.sub(rte_star[gg][23], 1, 2) == "FL" then
									xx_temp = tonumber(string.sub(rte_star[gg][23], 3, -1)) * 100
								else
									xx_temp = tonumber(rte_star[gg][23])
								end
								if xx_temp == nil then
									xx_temp = 0
								end
								legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
								xx_temp = 0
								if rte_star[gg][6] == "+" then
									xx_temp = 43
								elseif rte_star[gg][6] == "-" then
									xx_temp = 45
								elseif rte_star[gg][6] == "B" then
									xx_temp = 41
								end
								legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
							end
							
							legs_data2b[legs_num2b][7] = 0		-- latitude
							legs_data2b[legs_num2b][8] = 0		-- longitude
							if legs_num2b == first_add_star_idx + 1 and legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
								legs_data2b[legs_num2b][9] = ""
							elseif legs_num2b == first_add_star_idx + 2 and legs_data2b[legs_num2b-1][1] == "DISCONTINUITY" then
								legs_data2b[legs_num2b][9] = ""
							else
								legs_data2b[legs_num2b][9] = star_via			-- via id
							end
							legs_data2b[legs_num2b][10] = 0		-- calc speed
							legs_data2b[legs_num2b][11] = 0		-- calc altitude
							legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
							legs_data2b[legs_num2b][13] = 0
							legs_data2b[legs_num2b][14] = 0		-- rest alt
							legs_data2b[legs_num2b][15] = 0		-- last fuel
							legs_data2b[legs_num2b][16] = rte_star[gg][12]
							legs_data2b[legs_num2b][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
							legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
							legs_data2b[legs_num2b][19] = 2		-- 0-none, 1-SID, 2-STAR, 3-APP
							xx_temp = tonumber(rte_star[gg][8])
							if xx_temp == nil then
								legs_data2b[legs_num2b][20] = 0	-- vpa
							else
								legs_data2b[legs_num2b][20] = xx_temp / 100	-- vpa
							end
							legs_data2b[legs_num2b][21] = rte_star[gg][9]
							legs_data2b[legs_num2b][22] = rte_star[gg][18]
							legs_data2b[legs_num2b][23] = 0
							legs_data2b[legs_num2b][24] = 0
							legs_data2b[legs_num2b][25] = 0
							legs_data2b[legs_num2b][26] = 0
							legs_data2b[legs_num2b][27] = rte_star[gg][13]
							legs_data2b[legs_num2b][28] = rte_star[gg][14]
							-- legs_data2b[legs_num2b][29] = rte_star[gg][11]
							legs_data2b[legs_num2b][30] = rte_star[gg][10]
							if rte_star[gg][3] == legs_data2b[legs_num2b-1][1] and legs_data2b[legs_num2b-1][31] == "CF" and rte_star[gg][15] == "IF" then
								legs_data2b[legs_num2b][31] = legs_data2b[legs_num2b-1][31]
								legs_data2b[legs_num2b][29] = legs_data2b[legs_num2b-1][29]
							else
								legs_data2b[legs_num2b][31] = rte_star[gg][15]
								legs_data2b[legs_num2b][29] = rte_star[gg][11]
							end
							legs_data2b[legs_num2b][32] = rte_star[gg][16]
							if rte_star[gg][22] == 1 then
								legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 10
							elseif rte_star[gg][22] == 2 then
								legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 20
							end
							legs_data2b[legs_num2b][33] = rte_star[gg][19]
							legs_data2b[legs_num2b][34] = rte_star[gg][20]
							legs_data2b[legs_num2b][35] = rte_star[gg][21]
							legs_data2b[legs_num2b][36] = 0
							legs_data2b[legs_num2b][37] = 0
							legs_data2b[legs_num2b][38] = ""
							legs_data2b[legs_num2b][39] = ""
							legs_data2b[legs_num2b][40] = 0
							--legs_data2b[legs_num2b][42] = 0
							--legs_data2b[legs_num2b][41] = 0
							legs_data2b[legs_num2b][43] = ""
							legs_data2b[legs_num2b][44] = 0
							legs_data2b[legs_num2b][45] = 0
							legs_data2b[legs_num2b][46] = 0
							legs_data2b[legs_num2b][47] = 0
							legs_data2b[legs_num2b][48] = ""
							legs_data2b[legs_num2b][49] = 0
							legs_data2b[legs_num2b][50] = 0
							legs_data2b[legs_num2b][51] = 0
							legs_data2b[legs_num2b][52] = 0
							legs_data2b[legs_num2b][53] = 0
							legs_data2b[legs_num2b][54] = 0
							
							wpt_idx_cifp = rte_star[gg][25]
							if wpt_idx_cifp ~= 0 then
								if rte_star[gg][2] == find_star_tns2 then
									wpt_idx_cifp = wpt_idx_cifp + 20000		--20000 + idx
								elseif rte_star[gg][2] == " " then
									wpt_idx_cifp = wpt_idx_cifp + 30000		--30000 + idx
								else
									wpt_idx_cifp = wpt_idx_cifp + 31000		--31000 + idx
								end
							end
							legs_data2b[legs_num2b][44] = wpt_idx_cifp
							
						end
						
					end
				end
			else
				rte_add_star_act = 0
			end
		else
			rte_add_star_act = 0
		end
		
	else
		rte_add_star_act = 0
	end
	
	if add_ok == 1 then
		add_star = 2
	end
	
	--rte_add_app()
	
end

function rte_add_app2()
	
	local ii = 0
	local jj = 0
	local kk = 0
	local gg = 0
	local gg2 = 0
	local xx_temp = 0
	local rnw_ok  = 0
	local rnw_txt = ""
	local rnw_txt2 = ""
	local tns_ok = 0
	local disco_status = 0
	local only_one_star = 0
	local temp_str = ""
	local add_wpt = 0
	local skip_wpt = 0
	
	local nd_x = 0
	local idx_rec = 0
	local wpt_idx_cifp = 0
	local prev_ok = 0
	
	local find_icao = ""
	local find_app = ""
	local find_app_tns = ""
	local find_app_tns2 = ""
	local find_app_rw_only = 0
	local find_alt = 0
	local find_rw_ext_dist = ""
	local app_via = ""
	
	if add_sid_star_app_act == 0 then
		find_icao = des_icao_x
		find_app = des_app2
		find_app_tns = des_app_tns
		find_app_tns2 = des_app_tns2
		find_app_rw_only = des_app_rw_only_x
		find_alt = des_icao_alt
		find_rw_ext_dist = rw_ext_dist_x
	else
		find_icao = des_icao7_x
		find_app = des_app8
		find_app_tns = des_app_tns7
		find_app_tns2 = des_app_tns8
		find_app_rw_only = des_app_rw_only7_x
		find_alt = des_icao_alt7
		find_rw_ext_dist = rw_ext_dist7_x
	end
	if app_via_mised ~= 0 then
		app_via = "MISSED APPRCH"
	elseif find_app_tns2 == "------" then
		app_via = app_str
	else
		app_via = app_str .. "." .. find_app_tns2
		if string.len(app_via) > 15 then
			app_via = string.sub(app_via, 1, 15)
		end
	end
	
	rte_lat = 0
	rte_lon = 0
	-- add APP and TRANS APP
	disco_status = 0
	if rte_add_app_act == 2 then
		if rte_app_num > 0 and find_app_rw_only == 0 then
			if find_app ~= "------" then
				gg = sid_cnt
				if gg <= rte_app_num then
					
					tns_ok = 0
					if rte_app[gg][2] == find_app_tns2 then
						tns_ok = 1
					end
					if rte_app[gg][2] == " " then
						tns_ok = 1
					end
					
					if rte_app[gg][1] == find_app then
						wpt_idx_cifp = rte_app[gg][25]
						if wpt_idx_cifp ~= 0 then
							if rte_app[gg][2] == find_app_tns2 then
								wpt_idx_cifp = wpt_idx_cifp + 40000
							elseif app_via_mised == 0 then
								wpt_idx_cifp = wpt_idx_cifp + 50000
							else
								wpt_idx_cifp = wpt_idx_cifp + 60000
							end
						end
						skip_wpt = 0
						if offset > 0 and offset <= legs_num and legs_num > 1 then
							if find_app == des_app and legs_data[offset][44] ~= 0 and wpt_idx_cifp ~= 0 then
								if legs_data[offset][44] > 50000 then
									if wpt_idx_cifp <= legs_data[offset][44] then
										skip_wpt = 1
									end
								else
									if find_app_tns2 == find_app_tns then
										if wpt_idx_cifp <= legs_data[offset][44] then
											skip_wpt = 1
										end
									end
								end
							end
						end
						
						if tns_ok == 1 and skip_wpt == 0 then
							
							add_wpt = 0
							
							ii = 0
							if gg > 1 and legs_num2b > 1 then
								if (legs_data2b[legs_num2b][19] == 0 or legs_data2b[legs_num2b][19] == 1 or legs_data2b[legs_num2b][19] == 3) and add_app == 0 then
									ii = 0
								elseif legs_data2b[legs_num2b-1][31] == "CI" or legs_data2b[legs_num2b-1][31] == "VI" then
									ii = 1
								end
							end
							
							if rte_app[gg][15] == "IF" and ii == 0 then
								disco_status = 1
								if legs_data2b[legs_num2b][19] == 2 or legs_data2b[legs_num2b][19] == 4 then
									disco_status = 0
								elseif legs_data2b[legs_num2b][19] == 7 or legs_data2b[legs_num2b][19] == 8 or legs_data2b[legs_num2b][19] == 9 then
									disco_status = 0
								-- elseif legs_data2b[legs_num2b][19] == 10 then
									-- disco_status = 0
								else
									if legs_data2b[legs_num2b][1] == rte_app[gg][3] then
										disco_status = 0
									end
								end
								
								if disco_status == 0 then
									prev_ok = 0
									if legs_num2b > 1 then
										if rte_app[gg][3] == legs_data2b[legs_num2b-1][1] then
											prev_ok = 1
										end
									end
									
									if rte_app[gg][3] == legs_data2b[legs_num2b][1] and rte_app[gg][3] ~= "" and rte_app[gg][12] == legs_data2b[legs_num2b][16] then
										if legs_data2b[legs_num2b][1] == "(VECTOR)" and prev_ok ~= 0 then
											-- skip VECTOR
											legs_num2b = legs_num2b - 1
										end
										if legs_data2b[legs_num2b][19] == 2 then
											legs_data2b[legs_num2b][19] = 8	-- APP before STAR
											first_add_app_idx = first_add_app_idx - 1
										elseif legs_data2b[legs_num2b][19] ~= 7 and legs_data2b[legs_num2b][19] ~= 10 then
											legs_data2b[legs_num2b][19] = 9	-- APP before RTE
											first_add_app_idx = first_add_app_idx - 1
										end
										if legs_data2b[legs_num2b][19] == 1 then
											first_add_app_idx = first_add_app_idx - 1
										end
										legs_data2b[legs_num2b][17] = rte_app[gg][17] --+ 200		-- 0-none, 1-FAF
										-- change ALT, SPD restrict,.. if not HOLD
										if legs_data2b[legs_num2b][21] ~= 0 and legs_data2b[legs_num2b][21] ~= 1 then
											xx_temp = tonumber(rte_app[gg][4])
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][4] = xx_temp		-- speed
											if rte_app[gg][24] == "-" then
												legs_data2b[legs_num2b][42] = 1
											elseif rte_app[gg][24] == "+" then
												legs_data2b[legs_num2b][42] = 2
											else
												legs_data2b[legs_num2b][42] = 0
											end
											
											if string.sub(rte_app[gg][5], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][5], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][5])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][5] = xx_temp		-- altitude
											if string.sub(rte_app[gg][23], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][23], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][23])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
											xx_temp = 0
											if rte_app[gg][6] == "+" then
												xx_temp = 43
											elseif rte_app[gg][6] == "-" then
												xx_temp = 45
											elseif rte_app[gg][6] == "B" then
												xx_temp = 41
											end
											legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
											
											-- app 50000
											wpt_idx_cifp = rte_app[gg][25]
											if wpt_idx_cifp ~= 0 then
												if rte_app[gg][2] == find_app_tns2 then
													wpt_idx_cifp = wpt_idx_cifp + 40000
												elseif app_via_mised == 0 then
													wpt_idx_cifp = wpt_idx_cifp + 50000
												else
													wpt_idx_cifp = wpt_idx_cifp + 60000
												end
											end
											legs_data2b[legs_num2b][44] = wpt_idx_cifp
										end
									else
										if legs_data2b[legs_num2b][1] == "(VECTOR)" and prev_ok ~= 0 then
											-- skip VECTOR
											legs_num2b = legs_num2b - 1
											-- change ALT, SPD restrict,..
											if legs_data2b[legs_num2b][19] == 2 then
												legs_data2b[legs_num2b][19] = 8	-- APP before STAR
											elseif legs_data2b[legs_num2b][19] ~= 7 and legs_data2b[legs_num2b][19] ~= 10 then
												legs_data2b[legs_num2b][19] = 9	-- APP before RTE
											end
											legs_data2b[legs_num2b][17] = rte_app[gg][17] --+ 200		-- 0-none, 1-FAF
											xx_temp = tonumber(rte_app[gg][4])
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][4] = xx_temp		-- speed
											if rte_app[gg][24] == "-" then
												legs_data2b[legs_num2b][42] = 1
											elseif rte_app[gg][24] == "+" then
												legs_data2b[legs_num2b][42] = 2
											else
												legs_data2b[legs_num2b][42] = 0
											end
											
											if string.sub(rte_app[gg][5], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][5], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][5])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][5] = xx_temp		-- altitude
											if string.sub(rte_app[gg][23], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][23], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][23])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
											xx_temp = 0
											if rte_app[gg][6] == "+" then
												xx_temp = 43
											elseif rte_app[gg][6] == "-" then
												xx_temp = 45
											elseif rte_app[gg][6] == "B" then
												xx_temp = 41
											end
											legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
										else
											if add_ok == 0 then
												rte_copyb(legs_num2b + 1)
											end
											add_wpt = 1
										end
									end
								else
									if legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
										if add_ok == 0 then
											rte_copyb(legs_num2b + 1)
										end
										-- create DISCONTINUITY
										legs_num2b = legs_num2b + 1
										rte_add_discob(legs_num2b)
										-- add waypoint
										add_wpt = 1
									end
								end
							else
								ii = 0
								if gg > 1 and legs_num2b > 1 then
									if legs_data2b[legs_num2b-1][31] == "PI" and legs_data2b[legs_num2b][31] == "IF" then
										ii = 1
									end
								end
								
								if ii == 1 then
									-- previous PI leg
									if rte_app[gg][3] == legs_data2b[legs_num2b-1][1] and rte_app[gg][3] ~= "" and rte_app[gg][12] == legs_data2b[legs_num2b-1][16] then
										xx_temp = tonumber(rte_app[gg][4])
										if xx_temp ~= nil then
											legs_data2b[legs_num2b-1][4] = xx_temp		-- speed
											if rte_app[gg][24] == "-" then
												legs_data2b[legs_num2b-1][42] = 1
											elseif rte_app[gg][24] == "+" then
												legs_data2b[legs_num2b-1][42] = 2
											else
												legs_data2b[legs_num2b-1][42] = 0
											end
										end
										
										if string.sub(rte_app[gg][5], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_app[gg][5], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_app[gg][5])
										end
										if xx_temp ~= nil then
											legs_data2b[legs_num2b-1][5] = xx_temp		-- altitude
											if string.sub(rte_app[gg][23], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][23], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][23])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b-1][41] = xx_temp		-- altitude2
											xx_temp = 0
											if rte_app[gg][6] == "+" then
												xx_temp = 43
											elseif rte_app[gg][6] == "-" then
												xx_temp = 45
											elseif rte_app[gg][6] == "B" then
												xx_temp = 41
											end
											legs_data2b[legs_num2b-1][6] = xx_temp	-- altitude type
										end
									end
								end
								
								if add_ok == 0 then
									rte_copyb(legs_num2b + 1)
								end
								-- add waypoint
								add_wpt = 1
							end
							
							if add_wpt == 1 then
								add_app = 1
								add_ok = 1
								
								if rte_app[gg][17] == 2 then
									-- app_str = "MISSED APPRCH"
									app_via = "MISSED APPRCH"
									app_via_mised = 1
								end
								
								legs_num2b = legs_num2b + 1
								legs_data2b[legs_num2b] = {}
								
								
								if rte_app[gg][15] == "HA" or rte_app[gg][15] == "HF" or rte_app[gg][15] == "HM" then
									-- HOLD
									
									if legs_num2b > 1 then
										if legs_data2b[legs_num2b-1][1] ~= rte_app[gg][3] then
											
											-- add HOLD WPT
											legs_data2b[legs_num2b][1] = rte_app[gg][3]	--entry
											legs_data2b[legs_num2b][2] = 0		-- brg
											legs_data2b[legs_num2b][3] = 0		-- distance
											legs_data2b[legs_num2b][4] = 0		-- speed
											legs_data2b[legs_num2b][5] = 0		-- altitude fpa
											legs_data2b[legs_num2b][6] = 0	-- altitude type
											legs_data2b[legs_num2b][7] = 0	-- latitude
											legs_data2b[legs_num2b][8] = 0	-- longitude
											legs_data2b[legs_num2b][9] = ""		--"DIRECT" --find_app	--"DIRECT"			-- via id
											legs_data2b[legs_num2b][10] = 0		-- calc speed
											legs_data2b[legs_num2b][11] = 0		-- calc altitude
											legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
											legs_data2b[legs_num2b][13] = 0
											legs_data2b[legs_num2b][14] = 0		-- rest alt
											legs_data2b[legs_num2b][15] = 0		-- last fuel
											legs_data2b[legs_num2b][16] = rte_app[gg][12] --navaid_list[1][8]		-- reg code
											legs_data2b[legs_num2b][17] = rte_app[gg][17]	--201		-- FAF
											legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
											legs_data2b[legs_num2b][19] = 7		-- 0-none, 1-SID, 2-STAR/APP, 7 - APP
											legs_data2b[legs_num2b][20] = 0
											legs_data2b[legs_num2b][21] = -1		--rte_app[gg][9]
											legs_data2b[legs_num2b][22] = rte_app[gg][18]
											legs_data2b[legs_num2b][23] = 0
											legs_data2b[legs_num2b][24] = 0
											legs_data2b[legs_num2b][25] = 0
											legs_data2b[legs_num2b][26] = 0
											legs_data2b[legs_num2b][27] = rte_app[gg][13]
											legs_data2b[legs_num2b][28] = rte_app[gg][14]
											legs_data2b[legs_num2b][30] = rte_app[gg][10]
											if rte_app[gg][3] == legs_data2b[legs_num2b-1][1] and legs_data2b[legs_num2b-1][31] == "CF" and rte_app[gg][15] == "IF" then
												legs_data2b[legs_num2b][31] = legs_data2b[legs_num2b-1][31]
												legs_data2b[legs_num2b][29] = legs_data2b[legs_num2b-1][29]
											else
												legs_data2b[legs_num2b][31] = "TF"		--rte_app[gg][15]
												legs_data2b[legs_num2b][29] = rte_app[gg][11]
											end
											legs_data2b[legs_num2b][32] = rte_app[gg][16]
											if rte_app[gg][22] == 1 then
												legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 10
											elseif rte_app[gg][22] == 2 then
												legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 20
											end
											legs_data2b[legs_num2b][33] = rte_app[gg][19]
											legs_data2b[legs_num2b][34] = rte_app[gg][20]
											legs_data2b[legs_num2b][35] = rte_app[gg][21]
											legs_data2b[legs_num2b][36] = 0
											legs_data2b[legs_num2b][37] = 0
											legs_data2b[legs_num2b][38] = ""
											legs_data2b[legs_num2b][39] = ""
											legs_data2b[legs_num2b][40] = 0
											legs_data2b[legs_num2b][41] = 0
											legs_data2b[legs_num2b][42] = 0
											legs_data2b[legs_num2b][43] = ""
											legs_data2b[legs_num2b][44] = 0
											legs_data2b[legs_num2b][45] = 0
											legs_data2b[legs_num2b][46] = 0
											legs_data2b[legs_num2b][47] = 0
											legs_data2b[legs_num2b][48] = ""
											legs_data2b[legs_num2b][49] = 0
											legs_data2b[legs_num2b][50] = 0
											legs_data2b[legs_num2b][51] = 0
											legs_data2b[legs_num2b][52] = 0
											legs_data2b[legs_num2b][53] = 0
											legs_data2b[legs_num2b][54] = 0
											
											wpt_idx_cifp = rte_app[gg][25]
											if wpt_idx_cifp ~= 0 then
												if rte_app[gg][2] == find_app_tns2 then
													wpt_idx_cifp = wpt_idx_cifp + 40000
												elseif app_via_mised == 0 then
													wpt_idx_cifp = wpt_idx_cifp + 50000
												else
													wpt_idx_cifp = wpt_idx_cifp + 60000
												end
											end
											legs_data2b[legs_num2b][44] = wpt_idx_cifp
											
											xx_temp = tonumber(rte_app[gg][4])
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][4] = xx_temp		-- speed
											if rte_app[gg][24] == "-" then
												legs_data2b[legs_num2b][42] = 1
											elseif rte_app[gg][24] == "+" then
												legs_data2b[legs_num2b][42] = 2
											else
												legs_data2b[legs_num2b][42] = 0
											end
											-- xx_temp = tonumber(rte_app[gg][5])
											-- if xx_temp == nil then
												-- xx_temp = 0
											-- end
											if string.sub(rte_app[gg][5], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][5], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][5])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][5] = xx_temp		-- altitude
											if string.sub(rte_app[gg][23], 1, 2) == "FL" then
												xx_temp = tonumber(string.sub(rte_app[gg][23], 3, -1)) * 100
											else
												xx_temp = tonumber(rte_app[gg][23])
											end
											if xx_temp == nil then
												xx_temp = 0
											end
											legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
											xx_temp = 0
											if rte_app[gg][6] == "+" then
												xx_temp = 43
											elseif rte_app[gg][6] == "-" then
												xx_temp = 45
											elseif rte_app[gg][6] == "B" then
												xx_temp = 41
											end
											legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
											
											if add_star2 == 0 then
												if legs_num2b == first_add_app_idx + 1 and legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
													legs_data2b[legs_num2b][9] = ""
												elseif legs_num2b == first_add_app_idx2 + 2 and legs_data2b[legs_num2b-1][1] == "DISCONTINUITY" then
													legs_data2b[legs_num2b][9] = ""
												else
													legs_data2b[legs_num2b][9] = app_via	-- via
												end
											else
												if legs_num2b == first_add_app_idx + 1 and legs_data2b[legs_num2b-1][1] == "DISCONTINUITY" then
													legs_data2b[legs_num2b][9] = ""
												else
													legs_data2b[legs_num2b][9] = app_via	-- via
												end
											end
											
											legs_num2b = legs_num2b + 1
											legs_data2b[legs_num2b] = {}
										end
									end
								end
								
								if rte_app[gg][3] == legs_data2b[legs_num2b-1][1] and rte_app[gg][12] == legs_data2b[legs_num2b-1][16] then
									if rte_app[gg][15] == "VM" or rte_app[gg][15] == "FM" then		-- VECTOR
										-- VECTOR
										legs_data2b[legs_num2b][1] = "(VECTOR)"	-- id WPT
									elseif rte_app[gg][15] == "HA" or rte_app[gg][15] == "HF" or rte_app[gg][15] == "HM" then
										legs_data2b[legs_num2b][1] = rte_app[gg][3]
									elseif string.sub(rte_app[gg][15], 2, 2) == "A" then
										-- (1000)
										jj = tonumber(rte_app[gg][5])
										if jj == nil then
											legs_data2b[legs_num2b][1] = "(----)"	-- id WPT
										else
											legs_data2b[legs_num2b][1] = "(" .. tostring(jj) .. ")"
										end
									elseif rte_app[gg][15] == "PI" then
										-- Proc Turn
										legs_data2b[legs_num2b][1] = rte_app[gg][3]	-- id WPT
									elseif string.sub(rte_app[gg][15], 2, 2) == "I" then
										-- (INTC)
										legs_data2b[legs_num2b][1] = "(INTC)"	-- id WPT
									elseif rte_app[gg][15] == "CF" then
										-- (INTC)
										legs_data2b[legs_num2b][1] = rte_app[gg][3]	--"(INTC)"	-- id WPT
									elseif string.sub(rte_app[gg][15], 2, 2) == "R" then
										--(ABC180)
										jj = tonumber(rte_app[gg][11])
										temp_str = ""
										if jj ~= nil then
											jj = jj / 10
											temp_str = string.format("%03d", jj)
										else
											temp_str = "000"
										end
										legs_data2b[legs_num2b][1] = "(" .. rte_app[gg][13] .. temp_str .. ")"
									elseif string.sub(rte_app[gg][15], 2, 2) == "D" then
										--(ABC-10)
										jj = tonumber(rte_app[gg][10])
										temp_str = ""
										if jj ~= nil then
											jj = jj / 10
											temp_str = string.format("%02d", jj)
										else
											temp_str = "00"
										end
										legs_data2b[legs_num2b][1] = "(" .. rte_app[gg][13] .. "-" .. temp_str .. ")"
									elseif rte_app[gg][15] == "FC" then
										--(ABC)
										legs_data2b[legs_num2b][1] = "(" .. rte_app[gg][3]  .. ")"
									else
										-- if (rte_app[gg][22] == 0 and legs_data2b[legs_num2b-1][32] < 10) 
										-- or (rte_app[gg][22] == 1 and legs_data2b[legs_num2b-1][32] > 9 and legs_data2b[legs_num2b-1][32] < 20) 
										-- or (rte_app[gg][22] == 2 and legs_data2b[legs_num2b-1][32] > 19) then
											-- legs_data2b[legs_num2b][1] = "(----)"	-- id WPT
										-- else
											legs_data2b[legs_num2b][1] = rte_app[gg][3]
										-- end
									end
								else
									legs_data2b[legs_num2b][1] = rte_app[gg][3]
								end
								
								xx_temp = tonumber(rte_app[gg][7])
								if xx_temp == nil then
									xx_temp = 0
								end
								xx_temp = xx_temp / 10
								legs_data2b[legs_num2b][2] = math.rad(xx_temp)		-- brg
								legs_data2b[legs_num2b][3] = 0		-- distance
								
								if rte_app[gg][15] == "HA" or rte_app[gg][15] == "HF" or rte_app[gg][15] == "HM" then
									-- HOLD
									xx_temp = tonumber(rte_app[gg][4])
									if xx_temp == nil then
										xx_temp = legs_data2b[legs_num2b-1][4]
									end
									legs_data2b[legs_num2b][4] = xx_temp		-- speed
									if rte_app[gg][24] == "-" then
										legs_data2b[legs_num2b][42] = 1
									elseif rte_app[gg][24] == "+" then
										legs_data2b[legs_num2b][42] = 2
									else
										legs_data2b[legs_num2b][42] = 0
									end
									
									if string.sub(rte_app[gg][5], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_app[gg][5], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_app[gg][5])
									end
									if xx_temp == nil then
										legs_data2b[legs_num2b][5] = legs_data2b[legs_num2b-1][5]
										legs_data2b[legs_num2b][6] = legs_data2b[legs_num2b-1][6]
										legs_data2b[legs_num2b][41] = legs_data2b[legs_num2b-1][41]
									else
										legs_data2b[legs_num2b][5] = xx_temp		-- altitude
										if string.sub(rte_app[gg][23], 1, 2) == "FL" then
											xx_temp = tonumber(string.sub(rte_app[gg][23], 3, -1)) * 100
										else
											xx_temp = tonumber(rte_app[gg][23])
										end
										if xx_temp == nil then
											xx_temp = 0
										end
										legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
										xx_temp = 0
										if rte_app[gg][6] == "+" then
											xx_temp = 43
										elseif rte_app[gg][6] == "-" then
											xx_temp = 45
										elseif rte_app[gg][6] == "B" then
											xx_temp = 41
										end
										legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
									end
								else
									xx_temp = tonumber(rte_app[gg][4])
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][4] = xx_temp		-- speed
									if rte_app[gg][24] == "-" then
										legs_data2b[legs_num2b][42] = 1
									elseif rte_app[gg][24] == "+" then
										legs_data2b[legs_num2b][42] = 2
									else
										legs_data2b[legs_num2b][42] = 0
									end
									-- xx_temp = tonumber(rte_app[gg][5])
									-- if xx_temp == nil then
										-- xx_temp = 0
									-- end
									if string.sub(rte_app[gg][5], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_app[gg][5], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_app[gg][5])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][5] = xx_temp		-- altitude
									if string.sub(rte_app[gg][23], 1, 2) == "FL" then
										xx_temp = tonumber(string.sub(rte_app[gg][23], 3, -1)) * 100
									else
										xx_temp = tonumber(rte_app[gg][23])
									end
									if xx_temp == nil then
										xx_temp = 0
									end
									legs_data2b[legs_num2b][41] = xx_temp		-- altitude2
									xx_temp = 0
									if rte_app[gg][6] == "+" then
										xx_temp = 43
									elseif rte_app[gg][6] == "-" then
										xx_temp = 45
									elseif rte_app[gg][6] == "B" then
										xx_temp = 41
									end
									legs_data2b[legs_num2b][6] = xx_temp	-- altitude type
								end
								legs_data2b[legs_num2b][7] = 0		-- latitude
								legs_data2b[legs_num2b][8] = 0		-- longitude
								if add_star2 == 0 then
									if legs_num2b == first_add_app_idx + 1 and legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
										legs_data2b[legs_num2b][9] = ""
									elseif legs_num2b == first_add_app_idx2 + 2 and legs_data2b[legs_num2b-1][1] == "DISCONTINUITY" then
										legs_data2b[legs_num2b][9] = ""
									else
										legs_data2b[legs_num2b][9] = app_via	-- via
									end
								else
									if legs_num2b == first_add_app_idx + 1 and legs_data2b[legs_num2b-1][1] == "DISCONTINUITY" then
										legs_data2b[legs_num2b][9] = ""
									else
										legs_data2b[legs_num2b][9] = app_via	-- via
									end
								end
								
								legs_data2b[legs_num2b][10] = 0		-- calc speed
								legs_data2b[legs_num2b][11] = 0		-- calc altitude
								legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
								legs_data2b[legs_num2b][13] = 0
								legs_data2b[legs_num2b][14] = 0		-- rest alt
								legs_data2b[legs_num2b][15] = 0		-- last fuel
								legs_data2b[legs_num2b][16] = rte_app[gg][12]
								legs_data2b[legs_num2b][17] = rte_app[gg][17] --+ 200 		-- 0 - none, 1 - FAF, 2 - Missed wpt
								legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
								legs_data2b[legs_num2b][19] = 7		-- 0-none, 1-SID, 2-STAR, 3-APP
								xx_temp = tonumber(rte_app[gg][8])
								if xx_temp == nil then
									legs_data2b[legs_num2b][20] = 0	-- vpa
								else
									legs_data2b[legs_num2b][20] = xx_temp / 100	-- vpa
								end
								legs_data2b[legs_num2b][21] = rte_app[gg][9]
								legs_data2b[legs_num2b][22] = rte_app[gg][18]
								legs_data2b[legs_num2b][23] = 0
								legs_data2b[legs_num2b][24] = 0
								legs_data2b[legs_num2b][25] = 0
								legs_data2b[legs_num2b][26] = 0
								legs_data2b[legs_num2b][27] = rte_app[gg][13]
								legs_data2b[legs_num2b][28] = rte_app[gg][14]
								legs_data2b[legs_num2b][30] = rte_app[gg][10]
								if rte_app[gg][3] == legs_data2b[legs_num2b-1][1] and legs_data2b[legs_num2b-1][31] == "CF" and rte_app[gg][15] == "IF" then
									legs_data2b[legs_num2b][31] = legs_data2b[legs_num2b-1][31]
									legs_data2b[legs_num2b][29] = legs_data2b[legs_num2b-1][29]
								else
									legs_data2b[legs_num2b][31] = rte_app[gg][15]
									legs_data2b[legs_num2b][29] = rte_app[gg][11]
								end
								legs_data2b[legs_num2b][32] = rte_app[gg][16]
								if rte_app[gg][22] == 1 then
									legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 10
								elseif rte_app[gg][22] == 2 then
									legs_data2b[legs_num2b][32] = legs_data2b[legs_num2b][32] + 20
								end
								legs_data2b[legs_num2b][33] = rte_app[gg][19]
								legs_data2b[legs_num2b][34] = rte_app[gg][20]
								legs_data2b[legs_num2b][35] = rte_app[gg][21]
								legs_data2b[legs_num2b][36] = 0
								legs_data2b[legs_num2b][37] = 0
								legs_data2b[legs_num2b][38] = ""
								legs_data2b[legs_num2b][39] = ""
								legs_data2b[legs_num2b][40] = 0
								--legs_data2b[legs_num2b][42] = 0
								--legs_data2b[legs_num2b][41] = 0
								legs_data2b[legs_num2b][43] = ""
								legs_data2b[legs_num2b][44] = 0
								legs_data2b[legs_num2b][45] = 0
								legs_data2b[legs_num2b][46] = 0
								legs_data2b[legs_num2b][47] = 0
								legs_data2b[legs_num2b][48] = ""
								legs_data2b[legs_num2b][49] = 0
								legs_data2b[legs_num2b][50] = 0
								legs_data2b[legs_num2b][51] = 0
								legs_data2b[legs_num2b][52] = 0
								legs_data2b[legs_num2b][53] = 0
								legs_data2b[legs_num2b][54] = 0
								
								wpt_idx_cifp = rte_app[gg][25]
								if wpt_idx_cifp ~= 0 then
									if rte_app[gg][2] == find_app_tns2 then
										wpt_idx_cifp = wpt_idx_cifp + 40000
									elseif app_via_mised == 0 then
										wpt_idx_cifp = wpt_idx_cifp + 50000
									else
										wpt_idx_cifp = wpt_idx_cifp + 60000
									end
								end
								legs_data2b[legs_num2b][44] = wpt_idx_cifp
							end
							
						end
					end
				
				else
					rte_add_app_act = 3
				end
			else
				rte_add_app_act = 3
			end
		else
			rte_add_app_act = 3
		end
	end
	
	local nd_fpa = 3
	if add_sid_star_app_act == 0 then
		nd_fpa = tonumber(rw_ext_fpa_x)
	else
		nd_fpa = tonumber(rw_ext_fpa7_x)
	end
	if nd_fpa == nil then
		nd_fpa = 3.00
	end
	
	if rte_add_app_act == 3 and find_app_rw_only ~= 0 then
		-- RW only
		if rnw_data_num > 0 and string.len(find_icao) > 0 then
			nd_x = string.byte(string.sub(find_icao, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_rnw[nd_x][99999] > 0 then
					for ii = 1, idx_rnw[nd_x][99999] do
						idx_rec = idx_rnw[nd_x][ii]
						if find_icao == rnw_data[idx_rec][1] and string.sub(find_app, 3, -1) == rnw_data[idx_rec][2] then
							
							if add_ok == 0 then
								rte_copyb(legs_num2b + 1)
							end
							if legs_data2b[legs_num2b][1] ~= "DISCONTINUITY" then
								-- create DISCONTINUITY
								legs_num2b = legs_num2b + 1
								rte_add_discob(legs_num2b)
							end
							
							-- add RX
							if tonumber(find_rw_ext_dist) ~= nil then
								add_ok = math.rad((rnw_data[idx_rec][8] + 180) % 360)
								rte_add_rw_ext(rnw_data[idx_rec][3], rnw_data[idx_rec][4], add_ok, find_alt + 50)
								nd_fpa = -nd_fpa
								-- create DISCONTINUITY
								legs_num2b = legs_num2b + 1
								rte_add_discob(legs_num2b)
							else
								nd_fpa = 0
							end
							
							add_ok = 1
							legs_num2b = legs_num2b + 1
							legs_data2b[legs_num2b] = {}
							legs_data2b[legs_num2b][1] = find_app
							--legs_data2b[legs_num2b][2] = math.rad((rnw_data[idx_rec][8]+ mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4])) % 360)		-- brg
							legs_data2b[legs_num2b][2] = rnw_data[idx_rec][8]
							legs_data2b[legs_num2b][3] = 0		-- distance
							legs_data2b[legs_num2b][4] = 0		-- speed
							legs_data2b[legs_num2b][5] = find_alt + 50		-- altitude
							legs_data2b[legs_num2b][6] = 0	-- altitude type
							legs_data2b[legs_num2b][7] = rnw_data[idx_rec][3]		-- latitude
							legs_data2b[legs_num2b][8] = rnw_data[idx_rec][4]		-- longitude
							legs_data2b[legs_num2b][9] = ""		--"DIRECT" -- RUNWAY only --find_app	--find_icao			-- via id
							legs_data2b[legs_num2b][10] = 0		-- calc speed
							legs_data2b[legs_num2b][11] = 0		-- calc altitude
							legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
							legs_data2b[legs_num2b][13] = 0
							legs_data2b[legs_num2b][14] = 0		-- rest alt
							legs_data2b[legs_num2b][15] = 0		-- last fuel
							legs_data2b[legs_num2b][16] = ""
							legs_data2b[legs_num2b][17] = 1	--201		-- 1 - FAF
							--legs_data2b[legs_num2b][18] = math.rad(rnw_data[idx_rec][8])
							--legs_data2b[legs_num2b][18] = math.rad((rnw_data[idx_rec][8] - mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4])) % 360)		-- brg
							legs_data2b[legs_num2b][18] = math.rad((rnw_data[idx_rec][8] + mag_variation_deg(rnw_data[idx_rec][3], rnw_data[idx_rec][4]) + 360) % 360)		-- brg
							legs_data2b[legs_num2b][19] = 7		-- 0-none, 1-SID, 2-STAR, 3-APP
							legs_data2b[legs_num2b][20] = nd_fpa
							legs_data2b[legs_num2b][21] = -1
							legs_data2b[legs_num2b][22] = ""
							legs_data2b[legs_num2b][23] = 0
							legs_data2b[legs_num2b][24] = 0
							legs_data2b[legs_num2b][25] = 0
							legs_data2b[legs_num2b][26] = 0
							legs_data2b[legs_num2b][27] = ""
							legs_data2b[legs_num2b][28] = ""
							legs_data2b[legs_num2b][29] = ""
							legs_data2b[legs_num2b][30] = ""
							legs_data2b[legs_num2b][31] = ""
							legs_data2b[legs_num2b][32] = 0
							legs_data2b[legs_num2b][33] = ""
							legs_data2b[legs_num2b][34] = ""
							legs_data2b[legs_num2b][35] = ""
							legs_data2b[legs_num2b][36] = 4
							legs_data2b[legs_num2b][37] = 0
							legs_data2b[legs_num2b][38] = ""
							legs_data2b[legs_num2b][39] = ""
							legs_data2b[legs_num2b][40] = 0
							legs_data2b[legs_num2b][41] = 0
							legs_data2b[legs_num2b][42] = 0
							legs_data2b[legs_num2b][43] = ""
							legs_data2b[legs_num2b][44] = 0
							legs_data2b[legs_num2b][45] = 0
							legs_data2b[legs_num2b][46] = rnw_data[idx_rec][5]		-- latitude end
							legs_data2b[legs_num2b][47] = rnw_data[idx_rec][6]		-- longitude end
							legs_data2b[legs_num2b][48] = ""
							legs_data2b[legs_num2b][49] = 0
							legs_data2b[legs_num2b][50] = 0
							legs_data2b[legs_num2b][51] = 0
							legs_data2b[legs_num2b][52] = 0
							legs_data2b[legs_num2b][53] = 0
							legs_data2b[legs_num2b][54] = 0
							break
						end
					end
				end
			end
		end
	end
	
	if add_ok == 1 and rte_add_app_act == 3 then
		-- dump_leg_append("Temp data for copy APP\n")
		-- dump_leg2ba_tmp()
		-- dump_leg_append("Before paste APP\n")
		-- dump_leg2ba()
		rte_pasteb(legs_num2b + 1)
		-- dump_leg_append("After paste APP\n")
		-- dump_leg2ba()
	end
	if rte_add_app_act == 3 then
		rte_add_app_act = 4
	end
	
end


-- add ruwnway extension wpt RX...
function rte_add_rw_ext(rx_lat, rx_lon, rx_brg, rx_alt)
	
	local nd_dis = 0
	
	local find_app = ""
	local find_rw_ext_fpa = 3
	local find_rw_ext_dist = ""
	
	if add_sid_star_app_act == 0 then
		find_app = des_app2
		find_rw_ext_fpa = rw_ext_fpa_x
		find_rw_ext_dist = rw_ext_dist_x
	else
		find_app = des_app8
		find_rw_ext_fpa = rw_ext_fpa7_x
		find_rw_ext_dist = rw_ext_dist7_x
	end
	
	
	local nd_fpa = tonumber(find_rw_ext_fpa)
	
	if nd_fpa == nil then
		nd_fpa = 3.00
	end
	
	nd_dis = tonumber(find_rw_ext_dist)	-- / 3440.064795					-- distance NM
	
	calc_brg_dist(rx_lat, rx_lon, rx_brg, nd_dis)
	
	legs_num2b = legs_num2b + 1
	legs_data2b[legs_num2b] = {}
	legs_data2b[legs_num2b][1] = "RX" .. string.sub(find_app, 3, -1)	--entry
	legs_data2b[legs_num2b][2] = 0		-- brg
	legs_data2b[legs_num2b][3] = 0		-- distance
	legs_data2b[legs_num2b][4] = 0		-- speed
	legs_data2b[legs_num2b][5] = 0		-- altitude fpa
	--legs_data2b[legs_num2b][5] = math.floor(rx_alt + ((nd_dis * (math.tan(math.rad(nd_fpa)))) * 6076.11549))	-- in ft
	legs_data2b[legs_num2b][5] = rx_alt + ((nd_dis * (math.tan(math.rad(nd_fpa)))) * 6076.11549)	-- in ft
	legs_data2b[legs_num2b][5] = roundDownToIncrement(legs_data2b[legs_num2b][5], 10)
	if legs_data2b[legs_num2b][5] < rx_alt then
		legs_data2b[legs_num2b][5] = rx_alt
	end
	legs_data2b[legs_num2b][6] = 0	-- altitude type
	legs_data2b[legs_num2b][7] = calc_lat	-- latitude
	legs_data2b[legs_num2b][8] = calc_lon	-- longitude
	legs_data2b[legs_num2b][9] = ""		--"DIRECT" --find_app	--"DIRECT"			-- via id
	legs_data2b[legs_num2b][10] = 0		-- calc speed
	legs_data2b[legs_num2b][11] = 0		-- calc altitude
	legs_data2b[legs_num2b][12] = 0		-- calc altitude vnav pth
	legs_data2b[legs_num2b][13] = 0
	legs_data2b[legs_num2b][14] = 0		-- rest alt
	legs_data2b[legs_num2b][15] = 0		-- last fuel
	legs_data2b[legs_num2b][16] = "" --navaid_list[1][8]		-- reg code
	legs_data2b[legs_num2b][17] = 1	--201		-- FAF
	legs_data2b[legs_num2b][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
	legs_data2b[legs_num2b][19] = 7		-- 0-none, 1-SID, 2-STAR/APP, 7 - APP
	legs_data2b[legs_num2b][20] = -nd_fpa
	legs_data2b[legs_num2b][21] = -1
	legs_data2b[legs_num2b][22] = ""
	legs_data2b[legs_num2b][23] = 0
	legs_data2b[legs_num2b][24] = 0
	legs_data2b[legs_num2b][25] = 0
	legs_data2b[legs_num2b][26] = 0
	legs_data2b[legs_num2b][27] = ""
	legs_data2b[legs_num2b][28] = ""
	legs_data2b[legs_num2b][29] = ""
	legs_data2b[legs_num2b][30] = ""
	legs_data2b[legs_num2b][31] = ""
	legs_data2b[legs_num2b][32] = 0
	legs_data2b[legs_num2b][33] = ""
	legs_data2b[legs_num2b][34] = ""
	legs_data2b[legs_num2b][35] = ""
	legs_data2b[legs_num2b][36] = 4
	legs_data2b[legs_num2b][37] = 0
	legs_data2b[legs_num2b][38] = ""
	legs_data2b[legs_num2b][39] = ""
	legs_data2b[legs_num2b][40] = 0
	legs_data2b[legs_num2b][41] = 0
	legs_data2b[legs_num2b][42] = 0
	legs_data2b[legs_num2b][43] = ""
	legs_data2b[legs_num2b][44] = 0
	legs_data2b[legs_num2b][45] = 0
	legs_data2b[legs_num2b][46] = 0
	legs_data2b[legs_num2b][47] = 0
	legs_data2b[legs_num2b][48] = ""
	legs_data2b[legs_num2b][49] = 0
	legs_data2b[legs_num2b][50] = 0
	legs_data2b[legs_num2b][51] = 0
	legs_data2b[legs_num2b][52] = 0
	legs_data2b[legs_num2b][53] = 0
	legs_data2b[legs_num2b][54] = 0

end

--rte_add_sid

function dump_leg2b()
	local vvv = 0
	local fms_line = ""
	
	
	if rte_add_sid_act == 1 then
		file_dump = io.open("legs.txt", "a")
	end
	
	if file_dump ~= nil then
		fms_line = "LEGS DATA 2B\n"
		file_dump:write(fms_line)
		fms_line = "ADD " .. tostring(rte_add_sid_act) .. ":\n"
		file_dump:write(fms_line)
		if legs_num2b > 0 then
			for vvv = 1, legs_num2b  do
				fms_line = legs_data2b[vvv][1] .. "\n"
				file_dump:write(fms_line)
			end
		end
		fms_line = "<EOF>\n"
		file_dump:write(fms_line)
		
		if legs_data2_tmp_nb > 0 then
			
			fms_line = "LEGS DATA TMPB\n"
			file_dump:write(fms_line)
			for vvv = 1, legs_data2_tmp_nb do
				fms_line = legs_data2_tmpb[vvv][1] .. "\n"
				file_dump:write(fms_line)
			end
			fms_line = "<EOF>\n"
			file_dump:write(fms_line)
		end

	end
	
	if rte_add_sid_act == 5 then
		file_dump:close()
	end
end

function dump_leg2ba_tmp()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		fms_line = "legs_data2_tmp_nb = " .. tostring(legs_data2_tmp_nb) .. "\n"
		file_navdata2:write(fms_line)
		if legs_data2_tmp_nb > 0 then
			for vvv = 1, legs_data2_tmp_nb do
				fms_line = legs_data2_tmpb[vvv][1] .. ", " .. tostring(legs_data2_tmpb[vvv][19]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end


function dump_leg2()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if earth_nav_num > 0 then
			for vvv = 1, earth_nav_num  do
				fms_line = tostring(earth_nav[vvv][2]) .. "," .. tostring(earth_nav[vvv][3]) .. "," .. earth_nav[vvv][4] .. "," .. earth_nav[vvv][5] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function dump_leg2ba()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		fms_line = "Legs_num2b = " .. tostring(legs_num2b) .. "\n"
		file_navdata2:write(fms_line)
		if legs_num2b > 0 then
			for vvv = 1, legs_num2b + 1 do
				fms_line = legs_data2b[vvv][1] .. ", " .. tostring(legs_data2b[vvv][19]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function dump_leg2a()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		if legs_num2 > 0 then
			for vvv = 1, legs_num2 + 1 do
				fms_line = legs_data2[vvv][1] .. ", " .. tostring(legs_data2[vvv][19]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function page_to_var()
	local result = 0
	if page_dep_arr ~= 0 then
		result = 1
	elseif page_climb ~= 0 then
		result = 2
	elseif page_init ~= 0 then
		result = 3
	elseif page_ident ~= 0 then
		result = 4
	elseif page_takeoff ~= 0 then
		result = 5
	elseif page_approach ~= 0 then
		result = 6
	elseif page_perf ~= 0 then
		result = 7
	elseif page_n1_limit ~= 0 then
		result = 8
	elseif page_pos_init ~= 0 then
		result = 9
	elseif page_cruise ~= 0 then
		result = 10
	elseif page_descent ~= 0 then
		result = 11
	elseif page_route ~= 0 then
		result = 12
	elseif page_legs ~= 0 then
		result = 13
	elseif page_descent_forecast ~= 0 then
		result = 14
	elseif page_rte_init ~= 0 then
		result = 15
	elseif page_dep ~= 0 then
		result = 16
	elseif page_arr ~= 0 then
		result = 17
	elseif page_progress ~= 0 then
		result = 18
	elseif page_hold ~= 0 then
		result = 19
	elseif page_fix ~= 0 then
		result = 20
	elseif page_rte_legs ~= 0 then
		result = 21
	end
	return result
end

function var_to_page(in_page)
	if in_page == 1 then
		reset_fmc_pages()
		page_dep_arr = 1
	elseif in_page == 2 then
		reset_fmc_pages()
		page_climb = 1
	elseif in_page == 3 then
		reset_fmc_pages()
		page_init = 1
	elseif in_page == 4 then
		reset_fmc_pages()
		page_ident = 1
	elseif in_page == 5 then
		reset_fmc_pages()
		page_takeoff = 1
	elseif in_page == 6 then
		reset_fmc_pages()
		page_approach = 1
	elseif in_page == 7 then
		reset_fmc_pages()
		page_perf = 1
	elseif in_page == 8 then
		reset_fmc_pages()
		page_n1_limit = 1
	elseif in_page == 9 then
		reset_fmc_pages()
		page_pos_init = 1
	elseif in_page == 10 then
		reset_fmc_pages()
		page_cruise = 1
	elseif in_page == 11 then
		reset_fmc_pages()
		page_descent = 1
	elseif in_page == 12 then
		reset_fmc_pages()
		page_route = 1
	elseif in_page == 13 then
		reset_fmc_pages()
		page_legs = 1
	elseif in_page == 14 then
		reset_fmc_pages()
		page_descent_forecast = 1
	elseif in_page == 15 then
		reset_fmc_pages()
		page_rte_init = 1
	elseif in_page == 16 then
		reset_fmc_pages()
		page_dep = 1
	elseif in_page == 17 then
		reset_fmc_pages()
		page_arr = 1
	elseif in_page == 18 then
		reset_fmc_pages()
		page_progress = 1
	elseif in_page == 19 then
		reset_fmc_pages()
		page_hold = 1
	elseif in_page == 20 then
		reset_fmc_pages()
		page_fix = 1
	elseif in_page == 21 then
		reset_fmc_pages()
		page_rte_legs = 1
	end
end

function page_to_var2()
	local result = 0
	if page_dep_arr2 ~= 0 then
		result = 1
	elseif page_climb2 ~= 0 then
		result = 2
	elseif page_init2 ~= 0 then
		result = 3
	elseif page_ident2 ~= 0 then
		result = 4
	elseif page_takeoff2 ~= 0 then
		result = 5
	elseif page_approach2 ~= 0 then
		result = 6
	elseif page_perf2 ~= 0 then
		result = 7
	elseif page_n1_limit2 ~= 0 then
		result = 8
	elseif page_pos_init2 ~= 0 then
		result = 9
	elseif page_cruise2 ~= 0 then
		result = 10
	elseif page_descent2 ~= 0 then
		result = 11
	elseif page_route2 ~= 0 then
		result = 12
	elseif page_legs ~= 0 then
		result = 13
	elseif page_descent_forecast2 ~= 0 then
		result = 14
	elseif page_rte_init2 ~= 0 then
		result = 15
	elseif page_dep2 ~= 0 then
		result = 16
	elseif page_arr2 ~= 0 then
		result = 17
	elseif page_progress2 ~= 0 then
		result = 18
	elseif page_hold2 ~= 0 then
		result = 19
	elseif page_fix2 ~= 0 then
		result = 20
	elseif page_rte_legs2 ~= 0 then
		result = 21
	end
	return result
end

function var_to_page2(in_page)
	if in_page == 1 then
		reset_fmc_pages_fo()
		page_dep_arr2 = 1
	elseif in_page == 2 then
		reset_fmc_pages_fo()
		page_climb2 = 1
	elseif in_page == 3 then
		reset_fmc_pages_fo()
		page_init2 = 1
	elseif in_page == 4 then
		reset_fmc_pages_fo()
		page_ident2 = 1
	elseif in_page == 5 then
		reset_fmc_pages_fo()
		page_takeoff2 = 1
	elseif in_page == 6 then
		reset_fmc_pages_fo()
		page_approach2 = 1
	elseif in_page == 7 then
		reset_fmc_pages_fo()
		page_perf2 = 1
	elseif in_page == 8 then
		reset_fmc_pages_fo()
		page_n1_limit2 = 1
	elseif in_page == 9 then
		reset_fmc_pages_fo()
		page_pos_init2 = 1
	elseif in_page == 10 then
		reset_fmc_pages_fo()
		page_cruise2 = 1
	elseif in_page == 11 then
		reset_fmc_pages_fo()
		page_descent2 = 1
	elseif in_page == 12 then
		reset_fmc_pages_fo()
		page_route2 = 1
	elseif in_page == 13 then
		reset_fmc_pages_fo()
		page_legs2 = 1
	elseif in_page == 14 then
		reset_fmc_pages_fo()
		page_descent_forecast2 = 1
	elseif in_page == 15 then
		reset_fmc_pages_fo()
		page_rte_init2 = 1
	elseif in_page == 16 then
		reset_fmc_pages_fo()
		page_dep2 = 1
	elseif in_page == 17 then
		reset_fmc_pages_fo()
		page_arr2 = 1
	elseif in_page == 18 then
		reset_fmc_pages_fo()
		page_progress2 = 1
	elseif in_page == 19 then
		reset_fmc_pages_fo()
		page_hold2 = 1
	elseif in_page == 20 then
		reset_fmc_pages_fo()
		page_fix2 = 1
	elseif in_page == 21 then
		reset_fmc_pages_fo()
		page_rte_legs2 = 1
	end
end

function save_fmc(in_mode)
	
	local in_mode_ok = 0
	
	local file_fmcdata_name = "Output/situations/"
	if in_mode == 1 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_01.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_01.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_01.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_01.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_01.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_01.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_01.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_01.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 2 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_02.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_02.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_02.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_02.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_02.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_02.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_02.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_02.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 3 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_03.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_03.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_03.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_03.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_03.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_03.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_03.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_03.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 4 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_04.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_04.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_04.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_04.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_04.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_04.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_04.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_04.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 5 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_05.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_05.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_05.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_05.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_05.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_05.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_05.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_05.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 6 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_06.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_06.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_06.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_06.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_06.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_06.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_06.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_06.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 7 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_07.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_07.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_07.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_07.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_07.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_07.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_07.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_07.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 8 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_08.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_08.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_08.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_08.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_08.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_08.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_08.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_08.dat"
				in_mode_ok = 1
			end
		end
	elseif in_mode == 9 then
		if B738DR_b737_variant < 0 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_00.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_00.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_00.dat"
				in_mode_ok = 1
			end
		else
			if B738DR_b737_variant == 0 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_00.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_00.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_00.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_00.dat"
				in_mode_ok = 1
			elseif B738DR_b737_variant == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_00.dat"
				in_mode_ok = 1
			end
		end
	end
	
	if in_mode_ok ~= 0 then
	
	local file_fmcdata = io.open(file_fmcdata_name, "w")
	local vvv = 0
	local fms_line = ""
	local legs_num_save = 0
	
	raw_table('legs_data_type')
	local legs_data_type = {1,0,0,0,0,0,0,0,1,0,
							0,0,0,0,0,1,0,0,0,0,
							0,1,0,0,0,0,1,1,1,1,
							1,0,1,1,1,0,0,1,1,0,
							0,0,1,0,0,0,0,1,0,0,
							0,0,0,0}
	
	if file_fmcdata ~= nil then
		fms_line = "V1.1" .. "\n"
		file_fmcdata:write(fms_line)
		
		--if legs_num > 1 then
		-- if file_fmcdata ~= nil then
		fms_line = "FMC" .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(rte12_act) .. "," .. tostring(offset) .. "," .. tostring(offset_mod) .. "," .. tostring(offset_mod2) .. "," .. tostring(rte12_sel) .. "," .. tostring(rte12_sel_fo) ..  "\n"
		file_fmcdata:write(fms_line)
		
		if offset > 1 and offset <= legs_num then
			fms_line = legs_data[offset][1] .. "\n"
		else
			fms_line = ">NONE<\n"
		end
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(simDR_altitude_pilot) .. "\n"
		file_fmcdata:write(fms_line)
		
		local ref_rwy_out = " "
		
		if ref_rwy ~= "" then
			ref_rwy_out = ref_rwy
		end
		fms_line = ref_icao .. "," .. ref_gate .. "," .. ref_rwy_out .. "," .. ref_sid .. "," .. ref_sid_tns .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = des_icao .. "," .. des_star .. "," .. des_star_trans .. "," .. des_app .. "," .. des_app_tns .. ","
		fms_line = fms_line .. tostring(des_app_rw_only) .. "," .. rw_ext_dist .. "," .. rw_ext_fpa .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(ref_icao_lat) .. "," .. tostring(ref_icao_lon) .. "," .. tostring(ref_tns_alt) .. "," .. tostring(ref_tns_lvl) .. "," .. tostring(ref_icao_alt) .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(des_icao_lat) .. "," .. tostring(des_icao_lon) .. "," .. tostring(des_tns_alt) .. "," .. tostring(des_tns_lvl) .. "," .. tostring(des_icao_alt)  .. "\n"
		file_fmcdata:write(fms_line)
		
		ref_rwy_out = " "
		if ref_rwy7 ~= "" then
			ref_rwy_out = ref_rwy7
		end
		fms_line = ref_icao7 .. "," .. ref_gate7 .. "," .. ref_rwy_out .. "," .. ref_sid7 .. "," .. ref_sid_tns7 .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = des_icao7 .. "," .. des_star7 .. "," .. des_star_trans7 .. "," .. des_app7 .. "," .. des_app_tns7 .. ","
		fms_line = fms_line .. tostring(des_app_rw_only7) .. "," .. rw_ext_dist7 .. "," .. rw_ext_fpa7 .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(ref_icao_lat7) .. "," .. tostring(ref_icao_lon7) .. "," .. tostring(ref_tns_alt7) .. "," .. tostring(ref_tns_lvl7) .. "," .. tostring(ref_icao_alt7) .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(des_icao_lat7) .. "," .. tostring(des_icao_lon7) .. "," .. tostring(des_tns_alt7) .. "," .. tostring(des_tns_lvl7) .. "," .. tostring(des_icao_alt7)  .. "\n"
		file_fmcdata:write(fms_line)
		
		--MOD
		ref_rwy_out = " "
		
		if ref_rwy2 ~= "" then
			ref_rwy_out = ref_rwy2
		end
		fms_line = ref_icao_x .. "," .. des_icao_x .. "," .. ref_rwy_out .. "," .. ref_sid2 .. "," .. ref_sid_tns2 .. "," .. des_star2 .. "," .. des_star_trans2 .. "," .. des_app2 .. "," .. des_app_tns2 .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = sel_ref_rwy2 .. "," .. sel_ref_sid2 .. "," .. sel_ref_sid_tns2 .. "," .. sel_des_star2 .. "," .. sel_des_star_trans2 .. "," .. sel_des_app2 .. "," .. sel_des_app_tns2 .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = trans_alt_mod .. "," .. tostring(ref_tns_alt_x) .. "," .. trans_lvl_mod .. "," .. tostring(des_tns_lvl_x) .. "," .. co_route_x .. "," .. flt_num_x .. "\n"
		file_fmcdata:write(fms_line)
		
		ref_rwy_out = " "
		
		if ref_rwy8 ~= "" then
			ref_rwy_out = ref_rwy8
		end
		fms_line = ref_icao7_x .. "," .. des_icao7_x .. "," .. ref_rwy_out .. "," .. ref_sid8 .. "," .. ref_sid_tns8 .. "," .. des_star8 .. "," .. des_star_trans8 .. "," .. des_app8 .. "," .. des_app_tns8 .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = sel_ref_rwy8 .. "," .. sel_ref_sid8 .. "," .. sel_ref_sid_tns8 .. "," .. sel_des_star8 .. "," .. sel_des_star_trans8 .. "," .. sel_des_app8 .. "," .. sel_des_app_tns8 .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = trans_alt_mod2 .. "," .. tostring(ref_tns_alt7_x) .. "," .. trans_lvl_mod2 .. "," .. tostring(des_tns_lvl7_x) .. "," .. co_route7_x .. "," .. flt_num_x .. "\n"
		file_fmcdata:write(fms_line)
		
		-- perf
		fms_line = gw .. "," .. gw_lbs .. "," .. gw_kgs .. "," .. zfw .. "," .. zfw_lbs .. "," .. zfw_kgs .. "," .. isa_dev_f .. "," .. isa_dev_c .. ","
		fms_line = fms_line .. crz_wind_dir .. "," .. crz_wind_spd .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = co_route .. "," .. flt_num .. "," .. crz_alt .. "," .. tostring(crz_alt_num) .. "," .. reserves .. "," .. cost_index .. "," .. tc_oat_f .. "," .. tc_oat_c .. ","
		fms_line = fms_line .. tostring(perf_exec) .. "," .. tostring(page_to_var()) .. "," .. tostring(page_to_var2()) .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = gw_mod .. "," .. gw_lbs_mod .. "," .. gw_kgs_mod .. "," .. zfw_mod .. "," .. zfw_lbs_mod .. "," .. zfw_kgs_mod .. "," .. reserves_mod .. "," .. reserves_lbs_mod .. ","
		fms_line = fms_line .. reserves_kgs_mod .. "," .. cost_index_mod .. "\n"
		file_fmcdata:write(fms_line)
		
		
		
		-- thrust setting
		fms_line = sel_temp .. "," .. sel_temp_f .. "," .. oat .. "," .. oat_f .. ","
		if to_1 == "<ACT>" then
			fms_line = fms_line .. "1" .. ","
		elseif to_2 == "<ACT>" then
			fms_line = fms_line .. "2" .. ","
		else
			fms_line = fms_line .. "0" .. ","
		end
		if clb_1 == "<SEL>" then
			fms_line = fms_line .. "1" .. ","
		elseif clb_2 == "<SEL>" then
			fms_line = fms_line .. "2" .. ","
		else
			fms_line = fms_line .. "0" .. ","
		end
		if ga_act == "<ACT>" then
			fms_line = fms_line .. "1"
		elseif con_act == "<ACT>" then
			fms_line = fms_line .. "2"
		elseif clb_act == "<ACT>" then
			fms_line = fms_line .. "3"
		elseif crz_act == "<ACT>" then
			fms_line = fms_line .. "4"
		else
			fms_line = fms_line .. "0"
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- takeoff 01
		fms_line = flaps .. "," .. v1_set .. "," .. vr_set .. "," .. v2_set .. "," .. cg .. "," .. trim .. "," .. rw_wind_dir .. "," .. rw_wind_spd .. ","
		fms_line = fms_line .. rw_slope .. "," .. rw_hdg .. "," .. tostring(rw_cond) .. "\n"
		file_fmcdata:write(fms_line)
		-- takeoff 02
		fms_line = accel_alt .. "," .. eo_accel_alt .. "," .. clb_alt .. "," .. tostring(B738DR_thr_cutback) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- approach
		fms_line = gw_app .. "," .. gw_app_lbs .. "," .. gw_app_kgs .. "," .. flaps_app .. "," .. app_flap .. "," .. app_spd .. ","
		fms_line = fms_line .. wind_corr .. "," .. tostring(B738DR_fms_ils_disable) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- descent forecast 01
		fms_line = trans_lvl .. "," .. cabin_rate .. "," .. forec_isa_dev .. "," .. forec_qnh .. "," .. tai_on_alt .. "," .. tai_off_alt .. ","
		fms_line = fms_line .. tostring(tai_on_alt_num) .. "," .. tostring(tai_off_alt_num) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- descent forecast 02
		fms_line = forec_alt_1 .. "," .. tostring(forec_alt_1_num) .. "," .. forec_dir_1 .. "," .. forec_spd_1 .. "\n"
		file_fmcdata:write(fms_line)
		
		-- descent forecast 03
		fms_line = forec_alt_2 .. "," .. tostring(forec_alt_2_num) .. "," .. forec_dir_2 .. "," .. forec_spd_2 .. "\n"
		file_fmcdata:write(fms_line)
		
		-- descent forecast 04
		fms_line = forec_alt_3 .. "," .. tostring(forec_alt_3_num) .. "," .. forec_dir_3 .. "," .. forec_spd_3 .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 01
		fms_line = tostring(B738DR_flight_phase) .. "," .. tostring(B738DR_fms_descent_now) .. "," .. tostring(B738DR_missed_app_act) .. ","
		fms_line = fms_line .. tostring(altitude_last) .. "," .. tostring(takeoff_enable) .. "," .. tostring(climb_enable) .. ","
		if ga_data_deleted == false then
			fms_line = fms_line .. "0\n"
		else
			fms_line = fms_line .. "1\n"
		end
		file_fmcdata:write(fms_line)
		
		-- state fms 02
		fms_line = tostring(nav_mode) .. "," .. tostring(B738DR_hold_phase) .. "," .. tostring(vnav_alt_mode) .. "," .. tostring(B738DR_idx_corr) .. ","
		fms_line = fms_line .. tostring(B738DR_idx_corr2) .. "," .. tostring(B738DR_idx_corr3) .. "," .. tostring(B738DR_idx_corr4) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 03
		fms_line = tostring(af_lat) .. "," .. tostring(af_lon) .. "," .. tostring(af_dist) .. "," .. tostring(af_turn) .. ","
		fms_line = fms_line .. tostring(radii_lat) .. "," .. tostring(radii_lon) .. "," .. tostring(radii_dist) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 04
		fms_line = tostring(radii_turn_dir) .. "," .. tostring(radii_begin_crs) .. "," .. tostring(radii_end_crs) .. "," .. tostring(radii_end_angle) .. ","
		fms_line = fms_line .. tostring(radii_remain_dist) .. "," .. tostring(turn_radii_used) .. "," .. tostring(last_dta) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 05
		fms_line = tostring(legs_intdir_act) .. "," .. tostring(legs_intdir_crs) .. "," .. tostring(legs_intdir_trk) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 06 CLB
		fms_line = tostring(B738DR_fmc_climb_speed) .. "," .. tostring(B738DR_fmc_climb_speed_mach) .. "," .. tostring(B738DR_fmc_climb_r_speed1) .. ","
		fms_line = fms_line .. tostring(B738DR_fmc_climb_r_alt1) .. "," .. tostring(B738DR_fmc_climb_r_speed2) .. "," .. tostring(B738DR_fmc_climb_r_alt2) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 07 CRS
		fms_line = tostring(B738DR_fmc_cruise_speed) .. "," .. tostring(B738DR_fmc_cruise_speed_mach) .. "," .. tostring(B738DR_fmc_cruise_alt) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 08 DES
		fms_line = tostring(B738DR_fmc_descent_speed) .. "," .. tostring(B738DR_fmc_descent_speed_mach) .. "," .. tostring(B738DR_fmc_descent_alt) .. ","
		fms_line = fms_line .. tostring(B738DR_fmc_descent_r_speed1) .. "," .. tostring(B738DR_fmc_descent_r_alt1) .. "," .. tostring(B738DR_fmc_descent_r_speed2) .. ","
		fms_line = fms_line .. tostring(B738DR_fmc_descent_r_alt2) .. "," .. tostring(B738DR_fmc_approach_alt) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 09 ECON
		fms_line = tostring(econ_clb_spd) .. "," .. tostring(econ_clb_spd_mach) .. "," .. tostring(econ_crz_spd) .. ","
		fms_line = fms_line .. tostring(econ_crz_spd_mach) .. "," .. tostring(econ_des_spd) .. "," .. tostring(econ_des_spd_mach) .. "," .. tostring(lrc_crz_spd) .. "," .. tostring(lrc_crz_spd_mach) .. ","
		fms_line = fms_line .. tostring(econ_des_vpa) .. "," .. tostring(B738DR_climb_mode) .. "," .. tostring(B738DR_cruise_mode) .. "," .. tostring(B738DR_descent_mode) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- state fms 10 DLINK
		fms_line = dl_ref .. "," .. dl_des .. "," .. dl_flt_num .. "," .. tostring(dl_day) .. ","
		fms_line = fms_line .. tostring(dl_out_hrs) .. "," .. tostring(dl_out_min) .. "," .. tostring(dl_out_fuel) .. ","
		fms_line = fms_line .. tostring(dl_in_hrs) .. "," .. tostring(dl_in_min) .. "," .. tostring(dl_in_fuel) .. ","
		fms_line = fms_line .. tostring(dl_on_hrs) .. "," .. tostring(dl_on_min) .. "," .. tostring(dl_on_fuel) .. ","
		fms_line = fms_line .. tostring(dl_off_hrs) .. "," .. tostring(dl_off_min) .. "," .. tostring(dl_off_fuel) .. ","
		fms_line = fms_line .. tostring(dl_flight_time) .. "," .. tostring(dl_block_time) .. "\n"
		file_fmcdata:write(fms_line)
		
		-- fixes max. 5
		fms_line = tostring(fix_data_num) .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = ""
		if fix_data_num > 0 then
			for vvv = 1, fix_data_num do
				for vvvv = 1, 38 do
					if vvvv == 1 or vvvv == 5 then
						if (string.len(fix_data[vvv][vvvv]) == 0) then
							fms_line = fms_line .. " "
						else
							fms_line = fms_line .. fix_data[vvv][vvvv]
						end
					else
						fms_line = fms_line .. tostring(fix_data[vvv][vvvv])
					end
					if vvvv == 10 or vvvv == 20 or vvvv == 30 then
						fms_line = fms_line .. "\n"
						file_fmcdata:write(fms_line)
						fms_line = ""
					elseif vvvv < 38 then
						fms_line = fms_line .. ","
					end
				end
				fms_line = fms_line .. "\n"
				file_fmcdata:write(fms_line)
				fms_line = ""
			end
		end
		
		-- legs
		if rte12_act == 0 then
			if legs_num > 1 then
				legs_num_save = legs_num + 1
			elseif ref_icao ~= "----" and des_icao ~= "****" then
				legs_num_save = 2
			end
		else
			if legs_num > 1 then
				legs_num_save = legs_num + 1
			elseif ref_icao7 ~= "----" and des_icao7 ~= "****" then
				legs_num_save = 2
			end
		end
		fms_line = tostring(legs_num_save) .. "\n"
		file_fmcdata:write(fms_line)
		
		if legs_num_save > 0 then
			fms_line = tostring(legs_num) .. "\n"
			file_fmcdata:write(fms_line)
			
			fms_line = ""
			if legs_num_save > 0 then
				for vvv = 1, legs_num_save do
					for vvvv = 1, MAX_LEGS_DATA do
						if legs_data_type[vvvv] == 0 then
							fms_line = fms_line .. tostring(legs_data[vvv][vvvv])
						else
							if (string.len(legs_data[vvv][vvvv]) == 0) then
								fms_line = fms_line .. " "
							else
								fms_line = fms_line .. legs_data[vvv][vvvv]
							end
						end
						if vvvv == 10 or vvvv == 20 or vvvv == 30 or vvvv == 40 or vvvv == 50 or vvvv == 60 then
							fms_line = fms_line .. "\n"
							file_fmcdata:write(fms_line)
							fms_line = ""
						elseif vvvv < MAX_LEGS_DATA then
							fms_line = fms_line .. ","
						end
					end
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				end
			end
		end
		
		legs_num_save = 0
		if legs_num2 > 1 then
			legs_num_save = legs_num2 + 1
		elseif ref_icao_x ~= "----" and des_icao_x ~= "****" then
			legs_num_save = 2
		end
		fms_line = tostring(legs_num_save) .. "\n"
		file_fmcdata:write(fms_line)
		
		if legs_num_save > 0 then
			fms_line = tostring(legs_num2) .. "\n"
			file_fmcdata:write(fms_line)
			
			fms_line = ""
			if legs_num_save > 0 then
				for vvv = 1, legs_num_save do
					for vvvv = 1, MAX_LEGS_DATA do
						if legs_data_type[vvvv] == 0 then
							fms_line = fms_line .. tostring(legs_data2[vvv][vvvv])
						else
							if (string.len(legs_data2[vvv][vvvv]) == 0) then
								fms_line = fms_line .. " "
							else
								fms_line = fms_line .. legs_data2[vvv][vvvv]
							end
						end
						if vvvv == 10 or vvvv == 20 or vvvv == 30 or vvvv == 40 or vvvv == 50 or vvvv == 60 then
							fms_line = fms_line .. "\n"
							file_fmcdata:write(fms_line)
							fms_line = ""
						elseif vvvv < MAX_LEGS_DATA then
							fms_line = fms_line .. ","
						end
					end
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				end
			end
		end
		
		legs_num_save = 0
		if legs_num8 > 1 then
			legs_num_save = legs_num8 + 1
		elseif ref_icao7_x ~= "----" and des_icao7_x ~= "****" then
			legs_num_save = 2
		end
		
		fms_line = tostring(legs_num_save) .. "\n"
		file_fmcdata:write(fms_line)
		
		if legs_num_save > 0 then
			fms_line = tostring(legs_num8) .. "\n"
			file_fmcdata:write(fms_line)
			
			fms_line = ""
			if legs_num_save > 0 then
				for vvv = 1, legs_num_save do
					for vvvv = 1, MAX_LEGS_DATA do
						if legs_data_type[vvvv] == 0 then
							fms_line = fms_line .. tostring(legs_data8[vvv][vvvv])
						else
							if (string.len(legs_data8[vvv][vvvv]) == 0) then
								fms_line = fms_line .. " "
							else
								fms_line = fms_line .. legs_data8[vvv][vvvv]
							end
						end
						if vvvv == 10 or vvvv == 20 or vvvv == 30 or vvvv == 40 or vvvv == 50 or vvvv == 60 then
							fms_line = fms_line .. "\n"
							file_fmcdata:write(fms_line)
							fms_line = ""
						elseif vvvv < MAX_LEGS_DATA then
							fms_line = fms_line .. ","
						end
					end
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				end
			end
		end
		
		
		fms_line = "SYS" .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Global
		fms_line = tostring(simDR_airspeed_pilot) .. "," .. tostring(simDR_airspeed_copilot) .. ",0,0,0,0,0,0,0,0" .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Fuel
		fms_line = tostring(simDR_fuel_tank_weight_kg[0]) .. "," .. tostring(simDR_fuel_tank_weight_kg[1]) .. "," .. tostring(simDR_fuel_tank_weight_kg[2]) .. "\n"
		file_fmcdata:write(fms_line)
		
		
		-- Payload
		fms_line = tostring(B738DR_fa_fwd_kg) .. "," .. tostring(B738DR_fa_aft_kg) .. "," .. tostring(B738DR_galley_fwd_kg) .. "," .. tostring(B738DR_galley_aft_kg) .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = ""
		for vvv = 1, 50 do
			fms_line = fms_line .. tostring(B738DR_efb[vvv-1])
			if vvv < 50 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- MMR
		fms_line = tostring(B738DR_mmr_cpt_act_mode) .. "," .. tostring(B738DR_mmr_cpt_act_value) .. "," .. tostring(B738DR_mmr_cpt_stby_mode) .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = ""
		for vvv = 0, 4 do
			fms_line = fms_line .. tostring(B738DR_mmr_cpt_stby_value[vvv])
			if vvv < 4 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = tostring(B738DR_mmr_cpt_stby_cursor) .. "," .. tostring(B738DR_mmr_cpt_stby_gls) .. "," .. tostring(B738DR_mmr_cpt_err) .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = ""
		for vvv = 0, 3 do
			fms_line = fms_line .. tostring(B738DR_mmr_cpt_stby_cur[vvv])
			if vvv < 3 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = tostring(B738DR_mmr_fo_act_mode) .. "," .. tostring(B738DR_mmr_fo_act_value) .. "," .. tostring(B738DR_mmr_fo_stby_mode) .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = ""
		for vvv = 0, 4 do
			fms_line = fms_line .. tostring(B738DR_mmr_fo_stby_value[vvv])
			if vvv < 4 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = tostring(B738DR_mmr_fo_stby_cursor) .. "," .. tostring(B738DR_mmr_fo_stby_gls) .. "," .. tostring(B738DR_mmr_fo_err) .. "\n"
		file_fmcdata:write(fms_line)
		fms_line = ""
		for vvv = 0, 3 do
			fms_line = fms_line .. tostring(B738DR_mmr_fo_stby_cur[vvv])
			if vvv < 3 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- El Failure
		fms_line = ""
		for vvv = 1, 70 do
			fms_line = fms_line .. tostring(B738DR_el_failure[vvv-1])
			if vvv < 70 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Hyd Failure
		fms_line = ""
		for vvv = 1, 70 do
			fms_line = fms_line .. tostring(B738DR_hyd_failure[vvv-1])
			if vvv < 70 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Sys Failure
		fms_line = ""
		for vvv = 1, 70 do
			fms_line = fms_line .. tostring(B738DR_sys_failure[vvv-1])
			if vvv < 70 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Press Failure
		fms_line = ""
		for vvv = 1, 70 do
			fms_line = fms_line .. tostring(B738DR_pressurisation_failure[vvv-1])
			if vvv < 70 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Glare
		fms_line = ""
		for vvv = 1, 250 do
			fms_line = fms_line .. tostring(B738DR_glare[vvv-1])
			if vvv < 250 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- B738DR_fms_test4 = 1
		-- B738DR_fms_test5 = B738DR_glare[226]
		-- B738DR_fms_test6 = B738DR_glare[138]
		
		-- Glare sw
		fms_line = ""
		for vvv = 1, 250 do
			fms_line = fms_line .. tostring(B738DR_glare_sw[vvv-1])
			if vvv < 250 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Latlon
		fms_line = ""
		for vvv = 1, 50 do
			fms_line = fms_line .. tostring(B738DR_latlon[vvv-1])
			if vvv < 50 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Latlon SW
		fms_line = ""
		for vvv = 1, 50 do
			fms_line = fms_line .. tostring(B738DR_latlon_sw[vvv-1])
			if vvv < 50 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- System
		fms_line = ""
		for vvv = 1, 250 do
			fms_line = fms_line .. tostring(B738DR_system[vvv-1])
			if vvv < 250 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- System SW
		fms_line = ""
		for vvv = 1, 250 do
			fms_line = fms_line .. tostring(B738DR_system_sw[vvv-1])
			if vvv < 250 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Antiice
		fms_line = ""
		for vvv = 1, 30 do
			fms_line = fms_line .. tostring(B738DR_antiice[vvv-1])
			if vvv < 30 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Antiice SW
		fms_line = ""
		for vvv = 1, 30 do
			fms_line = fms_line .. tostring(B738DR_antiice_sw[vvv-1])
			if vvv < 30 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Lights
		fms_line = ""
		for vvv = 1, 20 do
			fms_line = fms_line .. tostring(B738DR_lights[vvv-1])
			if vvv < 20 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Lights SW
		fms_line = ""
		for vvv = 1, 20 do
			fms_line = fms_line .. tostring(B738DR_lights_sw[vvv-1])
			if vvv < 20 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Covers
		fms_line = ""
		for vvv = 1, 11 do
			fms_line = fms_line .. tostring(B738DR_cover[vvv-1])
			if vvv < 11 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		fms_line = ""
		for vvv = 1, 11 do
			fms_line = fms_line .. tostring(B738DR_cover_tgt[vvv-1])
			if vvv < 11 then
				fms_line = fms_line .. ","
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Fire
		fms_line = ""
		for vvv = 1, 20 do
			fms_line = fms_line .. tostring(B738DR_fire[vvv-1])
			if vvv < 20 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		-- Fire SW
		fms_line = ""
		for vvv = 1, 20 do
			fms_line = fms_line .. tostring(B738DR_fire_sw[vvv-1])
			if vvv < 20 then
				if vvv % 10 == 0 then
					fms_line = fms_line .. "\n"
					file_fmcdata:write(fms_line)
					fms_line = ""
				else
					fms_line = fms_line .. ","
				end
			end
		end
		fms_line = fms_line .. "\n"
		file_fmcdata:write(fms_line)
		
		file_fmcdata:close()
	end
	
	end
	
	update_header()
end


function split_buffer(in_buf_line)
	
	local token = ""
	local iii = 0
	
	buf_word = {}
	
	for token in string.gmatch(in_buf_line, "[^,]+") do
		iii = iii + 1
		buf_word[iii] = token
	end
	
	if iii > 0 then
		if string.len(buf_word[iii]) > 1 and string.byte(buf_word[iii], -1) == 13 then	-- CR
			buf_word[iii] = string.sub(buf_word[iii], 1, -2)
		end
	end
	
	return iii
end


function update_header()
	local ii = 0
	for ii = 1, 9 do
		load_header(ii)
	end
end

function load_header(in_mode)
	local in_mode_ok = 0
	local error_loading = 0
	local flight_info = ""
	local act_wpt_info = ""
	
	local file_fmcdata_name = "Output/situations/"
	if B738DR_b737_variant < 0 then
		if in_mode == 1 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_01.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_01.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_01.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 2 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_02.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_02.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_02.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 3 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_03.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_03.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_03.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 4 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_04.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_04.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_04.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 5 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_05.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_05.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_05.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 6 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_06.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_06.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_06.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 7 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_07.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_07.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_07.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 8 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_08.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_08.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_08.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 9 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_00.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_00.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_00.dat"
				in_mode_ok = 1
			end
		end
	else
		if B738DR_b737_variant == 0 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 1 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 2 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 3 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 4 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 5 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 6 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 7 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 8 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 9 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 5 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_05.dat"
				in_mode_ok = 1
			elseif in_mode == 6 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_06.dat"
				in_mode_ok = 1
			elseif in_mode == 7 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_07.dat"
				in_mode_ok = 1
			elseif in_mode == 8 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_08.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_00.dat"
				in_mode_ok = 1
			end
		end
	end
	
	
	if in_mode_ok ~= 0 then
	
		local file_fmcdata = io.open(file_fmcdata_name, "r")
		local fms_line = ""
		local ii = 0
		local token = ""
		local fmc_data_idx = 0
		local fmc_data_legs_num = 0
		local fmc_data_fix_num = 0
		local num_tmp = 0
		local data_ok = 0
		local include_fmc = 0
		local legs_num_save = 0
		
		local from_icao = ""
		local to_icao = ""
		local act_wpt = ""
		local act_alt = ""
		local rte12_act_hdr = -1
		
		if file_fmcdata ~= nil then
			
			-- version
			fms_line = file_fmcdata:read()
			if fms_line then
				error_loading = 0
			else
				error_loading = 1
			end
			-- B738DR_fms_test8 = 1
			
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if fms_line == "FMC" then
						include_fmc = 1
					end
				else
					error_loading = 1
				end
			end
			
			if include_fmc ~= 0 then
				-- offset
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						if split_buffer(fms_line) > 5 then
							num_tmp = tonumber(buf_word[1])
							if num_tmp == nil then
								error_loading = 1
							else
								rte12_act_hdr = num_tmp
							end
						else
							error_loading = 1
						end
					else
						error_loading = 1
					end
				end
				
				-- act wpt
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						if fms_line ~= ">NONE<" then
							if string.len(fms_line) > 5 then
								act_wpt = string.sub(fms_line, 1, 5)
							else
								act_wpt = spaces_after(fms_line, 5)
							end
						end
					else
						error_loading = 1
					end
				end
				
				-- act alt
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						num_tmp = tonumber(fms_line)
						if num_tmp == nil then
							error_loading = 1
						else
							num_tmp = math.floor((num_tmp / 100) + 0.5) * 100
							act_alt = string.format("%5d", num_tmp)
						end
					else
						error_loading = 1
					end
				end
				
				-- ref 01
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						if split_buffer(fms_line) > 4 then
							if rte12_act_hdr == 0 then
								from_icao = buf_word[1]
							end
						else
							error_loading = 1
						end
					end
				end
				
				-- des 01
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						if split_buffer(fms_line) > 4 then	--7
							if rte12_act_hdr == 0 then
								to_icao = buf_word[1]
							end
						else
							error_loading = 1
						end
					end
				end
				fms_line = file_fmcdata:read()
				fms_line = file_fmcdata:read()
				
				-- ref 01/2
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						if split_buffer(fms_line) > 4 then
							if rte12_act_hdr == 1 then
								from_icao = buf_word[1]
							end
						else
							error_loading = 1
						end
					end
				end
				
				-- des 01/2
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						if split_buffer(fms_line) > 4 then		--7
							if rte12_act_hdr == 1 then
								to_icao = buf_word[1]
							end
						else
							error_loading = 1
						end
					end
				end
			end
		else
			error_loading = 1
		end
		
		if error_loading == 0 then
			if from_icao ~= "" and to_icao ~= "" then
				flight_info = from_icao .. " - " .. to_icao
			end
			act_wpt_info = "TO " .. act_wpt .. " " .. act_alt .. "FT"
		end
	
	end
	
	-- if flight_info == "" and act_wpt_info == "" and error_loading ~= 0 then
	if error_loading ~= 0 then
		B738DR_fms_flight_saved[in_mode - 1] = 0
	else
		B738DR_fms_flight_saved[in_mode - 1] = 1
	end
	
	if in_mode == 1 then
		B738DR_fms_flight01 = flight_info
		B738DR_fms_flight01b = act_wpt_info
	elseif in_mode == 2 then
		B738DR_fms_flight02 = flight_info
		B738DR_fms_flight02b = act_wpt_info
	elseif in_mode == 3 then
		B738DR_fms_flight03 = flight_info
		B738DR_fms_flight03b = act_wpt_info
	elseif in_mode == 4 then
		B738DR_fms_flight04 = flight_info
		B738DR_fms_flight04b = act_wpt_info
	elseif in_mode == 5 then
		B738DR_fms_flight05 = flight_info
		B738DR_fms_flight05b = act_wpt_info
	elseif in_mode == 6 then
		B738DR_fms_flight06 = flight_info
		B738DR_fms_flight06b = act_wpt_info
	elseif in_mode == 7 then
		B738DR_fms_flight07 = flight_info
		B738DR_fms_flight07b = act_wpt_info
	elseif in_mode == 8 then
		B738DR_fms_flight08 = flight_info
		B738DR_fms_flight08b = act_wpt_info
	elseif in_mode == 9 then
		B738DR_fms_flight09 = flight_info
		B738DR_fms_flight09b = act_wpt_info
	end
	
end

function load_fmc(in_mode)
	
	local in_mode_ok = 0
	local error_loading = 0
	
	local file_fmcdata_name = "Output/situations/"
	if B738DR_b737_variant < 0 then
		if in_mode == 1 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_01.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_01.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_01.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 2 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_02.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_02.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_02.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 3 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_03.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_03.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_03.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 4 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_04.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_04.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_04.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 5 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_05.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_05.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_05.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 6 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_06.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_06.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_06.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 7 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_07.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_07.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_07.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 8 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_08.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_08.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_08.dat"
				in_mode_ok = 1
			end
		elseif in_mode == 9 then
			if B738DR_73x == 0 then
				file_fmcdata_name = file_fmcdata_name .. "B738X_00.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 1 then
				file_fmcdata_name = file_fmcdata_name .. "B739U_00.dat"
				in_mode_ok = 1
			elseif B738DR_73x == 2 then
				file_fmcdata_name = file_fmcdata_name .. "B737U_00.dat"
				in_mode_ok = 1
			end
		end
	else
		if B738DR_b737_variant == 0 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_80NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 1 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_90NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 2 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_70NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 3 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_60NG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 4 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_9ENG_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 5 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_70MX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 6 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_2HMX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 7 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_80MX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 8 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_90MX_00.dat"
				in_mode_ok = 1
			end
		elseif B738DR_b737_variant == 9 then
			if in_mode == 1 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_01.dat"
				in_mode_ok = 1
			elseif in_mode == 2 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_02.dat"
				in_mode_ok = 1
			elseif in_mode == 3 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_03.dat"
				in_mode_ok = 1
			elseif in_mode == 4 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_04.dat"
				in_mode_ok = 1
			elseif in_mode == 9 then
				file_fmcdata_name = file_fmcdata_name .. "737_1KMX_00.dat"
				in_mode_ok = 1
			end
		end
	end
	
	if in_mode_ok ~= 0 then
	
	-- local file_fmcdata_name = "fms.txt"
	
	local file_fmcdata = io.open(file_fmcdata_name, "r")
	local fms_line = ""
	local ii = 0
	local token = ""
	local fmc_data_idx = 0
	local fmc_data_legs_num = 0
	local fmc_data_fix_num = 0
	local num_tmp = 0
	local vvv = 0
	local vvvv = 0
	local data_ok = 0
	local include_fmc = 0
	local legs_num_save = 0
	
	raw_table('legs_data_type')
	local legs_data_type = {1,0,0,0,0,0,0,0,1,0,
							0,0,0,0,0,1,0,0,0,0,
							0,1,0,0,0,0,1,1,1,1,
							1,0,1,1,1,0,0,1,1,0,
							0,0,1,0,0,0,0,1,0,0,
							0,0,0,0}
	
	fmc_data_num = 0
	fmc_data = {}
	
	fmc_fix_data_num = 0
	fmc_fix_data = {}
	
	-- B738DR_fms_test7 = 0
	local flight_save_ver = 0
	
	if file_fmcdata ~= nil then
		
		-- initialise
		B738_init2()
		B738_init3()
		B738_init4()
		B738_init_vspd()
		
		-- version
		fms_line = file_fmcdata:read()
		if fms_line then
			-- B738DR_fms_test7 = 1
			if string.len(fms_line) > 3 then
				if string.sub(fms_line, 1, 4) == "V1.0" then
					flight_save_ver = 10
				elseif string.sub(fms_line, 1, 4) == "V1.1" then
					flight_save_ver = 11
				else
					error_loading = 1
				end
			end
		else
			error_loading = 2
		end
		
		if error_loading == 0 then
			fms_line = file_fmcdata:read()
			if fms_line then
				if fms_line == "FMC" then
					include_fmc = 1
				end
			else
				error_loading = 3
			end
		end
		
		if include_fmc ~= 0 then
			-- offset
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 5 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp == nil then
							error_loading = 4
						else
							rte12_act = num_tmp
							num_tmp = tonumber(buf_word[2])
							if num_tmp == nil then
								error_loading = 4
							else
								offset = num_tmp
								offset_seq = offset
								last_offset = offset
								offset_mod = offset
								last_offset  = offset
							end
							if error_loading == 4 then
								num_tmp = tonumber(buf_word[3])
								if num_tmp == nil then
									error_loading = 4
								else
									last_offset2  = num_tmp
									offset_mod2 = num_tmp
								end
								num_tmp = tonumber(buf_word[4])
								if num_tmp == nil then
									error_loading = 4
								else
									last_offset8  = num_tmp
									offset_mod2 = num_tmp
								end
							end
							num_tmp = tonumber(buf_word[5])
							if num_tmp == nil then
								error_loading = 5
							else
								rte12_sel = num_tmp
							end
							num_tmp = tonumber(buf_word[6])
							if num_tmp == nil then
								error_loading = 5
							else
								rte12_sel_fo = num_tmp
							end
						end
						if error_loading == 4 then
							rte12_act = -1
							offset = 0
							offset_seq = offset
							last_offset = offset
							offset_mod = offset
							last_offset  = offset
							last_offset2  = offset
							offset_mod2 = offset
							last_offset8  = offset
							offset_mod2 = offset
						end
					else
						error_loading = 5
					end
				else
					error_loading = 5
				end
			end
			
			if flight_save_ver == 11 then
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					-- active wpt
				end
				
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					-- active altitude
				end
			end
			
			-- ref 01
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 4 then
						ref_icao = buf_word[1]
						-- ref_icao_x = ref_icao
						ref_gate = buf_word[2]
						if buf_word[3] == " " then
							ref_rwy = ""
						else
							ref_rwy = buf_word[3]
						end
						ref_sid = buf_word[4]
						ref_sid_tns = buf_word[5]
					else
						error_loading = 6
					end
				end
			end
			
			-- des 01
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 7 then
						des_icao = buf_word[1]
						des_star = buf_word[2]
						des_star_trans = buf_word[3]
						des_app = buf_word[4]
						des_app_tns = buf_word[5]
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							des_app_rw_only = num_tmp
						else
							des_app_rw_only = 0
						end
						rw_ext_dist = buf_word[7]
						rw_ext_fpa = buf_word[8]
						des_app_rw_only_x = des_app_rw_only
						rw_ext_dist_x = rw_ext_dist
						rw_ext_fpa_x = rw_ext_fpa
					else
						error_loading = 7
					end
				end
			end
			
			-- ref 02
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 4 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							ref_icao_lat = num_tmp
						else
							ref_icao_lat = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							ref_icao_lon = num_tmp
						else
							ref_icao_lon = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							ref_tns_alt = num_tmp
						else
							ref_tns_alt = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							ref_tns_lvl = num_tmp
						else
							ref_tns_lvl = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							ref_icao_alt = num_tmp
						else
							ref_icao_alt = 0
						end
					else
						error_loading = 8
					end
				end
			end
			
			-- des 02
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 4 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							des_icao_lat = num_tmp
						else
							des_icao_lat = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							des_icao_lon = num_tmp
						else
							des_icao_lon = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							des_tns_alt = num_tmp
						else
							des_tns_alt = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							des_tns_lvl = num_tmp
						else
							des_tns_lvl = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							des_icao_alt = num_tmp
						else
							des_icao_alt = 0
						end
					else
						error_loading = 9
					end
				end
			end
			
			-- ref01/2
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 4 then
						ref_icao7 = buf_word[1]
						-- ref_icao7_x = ref_icao
						ref_gate7 = buf_word[2]
						if buf_word[3] == " " then
							ref_rwy7 = ""
						else
							ref_rwy7 = buf_word[3]
						end
						ref_sid7 = buf_word[4]
						ref_sid_tns7 = buf_word[5]
					else
						error_loading = 6
					end
				end
			end
			
			-- des01/2
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 7 then
						des_icao7 = buf_word[1]
						des_star7 = buf_word[2]
						des_star_trans7 = buf_word[3]
						des_app7 = buf_word[4]
						des_app_tns7 = buf_word[5]
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							des_app_rw_only7 = num_tmp
						else
							des_app_rw_only7 = 0
						end
						rw_ext_dist7 = buf_word[7]
						rw_ext_fpa7 = buf_word[8]
						des_app_rw_only7_x = des_app_rw_only
						rw_ext_dist7_x = rw_ext_dist
						rw_ext_fpa7_x = rw_ext_fpa
					else
						error_loading = 7
					end
				end
			end
			
			-- ref02/2
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 4 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							ref_icao_lat7 = num_tmp
						else
							ref_icao_lat7 = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							ref_icao_lon7 = num_tmp
						else
							ref_icao_lon7 = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							ref_tns_alt7 = num_tmp
						else
							ref_tns_alt7 = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							ref_tns_lvl7 = num_tmp
						else
							ref_tns_lvl7 = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							ref_icao_alt7 = num_tmp
						else
							ref_icao_alt7 = 0
						end
					else
						error_loading = 8
					end
				end
			end
			
			-- des02/2
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 4 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							des_icao_lat7 = num_tmp
						else
							des_icao_lat7 = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							des_icao_lon7 = num_tmp
						else
							des_icao_lon7 = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							des_tns_alt7 = num_tmp
						else
							des_tns_alt7 = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							des_tns_lvl7 = num_tmp
						else
							des_tns_lvl7 = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							des_icao_alt7 = num_tmp
						else
							des_icao_alt7 = 0
						end
					else
						error_loading = 9
					end
				end
			end
			
			-- mod
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 8 then
						ref_icao_x = buf_word[1]
						des_icao_x = buf_word[2]
						ref_rwy2 = buf_word[3]
						ref_sid2 = buf_word[4]
						ref_sid_tns2 = buf_word[5]
						des_star2 = buf_word[6]
						des_star_trans2 = buf_word[7]
						des_app2 = buf_word[8]
						des_app_tns2 = buf_word[9]
						
						des_star2_old = des_star2
						des_star_trans2_old = des_star_trans2
						des_app2_old = des_app2
						des_app_tns2_old = des_app_tns2
					else
						error_loading = 9
					end
				end
			end
			
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						sel_ref_rwy2 = buf_word[1]
						sel_ref_sid2 = buf_word[2]
						sel_ref_sid_tns2 = buf_word[3]
						sel_des_star2 = buf_word[4]
						sel_des_star_trans2 = buf_word[5]
						sel_des_app2 = buf_word[6]
						sel_des_app_tns2 = buf_word[7]
					else
						error_loading = 9
					end
				end
			end
			
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 5 then
						trans_alt_mod = buf_word[1]
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							ref_tns_alt_x = num_tmp
						else
							ref_tns_alt_x = 18000
						end
						trans_lvl_mod = buf_word[3]
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							des_tns_lvl_x = num_tmp
						else
							des_tns_lvl_x = 18000
						end
						co_route_x = buf_word[5]
						flt_num_x = buf_word[6]
					else
						error_loading = 9
					end
				end
			end
			
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 8 then
						ref_icao7_x = buf_word[1]
						des_icao7_x = buf_word[2]
						ref_rwy8 = buf_word[3]
						ref_sid8 = buf_word[4]
						ref_sid_tns8 = buf_word[5]
						des_star8 = buf_word[6]
						des_star_trans8 = buf_word[7]
						des_app8 = buf_word[8]
						des_app_tns8 = buf_word[9]
						
						des_star8_old = des_star8
						des_star_trans8_old = des_star_trans8
						des_app8_old = des_app8
						des_app_tns8_old = des_app_tns8
					else
						error_loading = 9
					end
				end
			end
			
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						sel_ref_rwy8 = buf_word[1]
						sel_ref_sid8 = buf_word[2]
						sel_ref_sid_tns8 = buf_word[3]
						sel_des_star8 = buf_word[4]
						sel_des_star_trans8 = buf_word[5]
						sel_des_app8 = buf_word[6]
						sel_des_app_tns8 = buf_word[7]
					else
						error_loading = 9
					end
				end
			end
			
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 5 then
						trans_alt_mod2 = buf_word[1]
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							ref_tns_alt7_x = num_tmp
						else
							ref_tns_alt7_x = 18000
						end
						trans_lvl_mod2 = buf_word[3]
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							des_tns_lvl7_x = num_tmp
						else
							des_tns_lvl7_x = 18000
						end
						co_route7_x = buf_word[5]
						flt_num_x = buf_word[6]
					else
						error_loading = 9
					end
				end
			end
			
			-- perf
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 9 then
						-- B738DR_fms_test7 = 7
						gw = buf_word[1]
						gw_lbs = buf_word[2]
						gw_kgs = buf_word[3]
						zfw = buf_word[4]
						zfw_lbs = buf_word[5]
						zfw_kgs = buf_word[6]
						isa_dev_f = buf_word[7]
						isa_dev_c = buf_word[8]
						crz_wind_dir = buf_word[9]
						crz_wind_spd = buf_word[10]
						
						gw_mod = gw
						gw_lbs_mod = gw_lbs
						gw_kgs_mod = gw_kgs
						zfw_mod = zfw
						zfw_kgs_mod = zfw_lbs
						zfw_lbs_mod = zfw_kgs
						crz_wind_dir_mod = crz_wind_dir
						crz_wind_spd_mod = crz_wind_spd
						isa_dev_f_mod = isa_dev_f
						isa_dev_c_mod = isa_dev_c
						
					else
						error_loading = 10
					end
				end
			end
			
			-- coroute
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 10 then
						co_route = buf_word[1]
						flt_num = buf_word[2]
						crz_alt = buf_word[3]
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							crz_alt_num = num_tmp
						else
							crz_alt_num = 0
						end
						reserves = buf_word[5]
						cost_index = buf_word[6]
						tc_oat_f = buf_word[7]
						tc_oat_c = buf_word[8]
						tc_oat_c_mod = tc_oat_c
						tc_oat_f_mod = tc_oat_f
						num_tmp = tonumber(buf_word[9])
						if num_tmp ~= nil then
							perf_exec = num_tmp
						else
							perf_exec = 0
						end
						num_tmp = tonumber(buf_word[10])
						if num_tmp ~= nil then
							var_to_page(num_tmp)
							-- B738DR_fms_test8 = page_takeoff
						end
						num_tmp = tonumber(buf_word[11])
						if num_tmp ~= nil then
							var_to_page2(num_tmp)
						end
					else
						error_loading = 11
					end
				end
			end
			
			-- perf mod
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 9 then
						gw_mod = buf_word[1]
						gw_lbs_mod = buf_word[2]
						gw_kgs_mod = buf_word[3]
						zfw_mod = buf_word[4]
						zfw_lbs_mod = buf_word[5]
						zfw_kgs_mod = buf_word[6]
						reserves_mod = buf_word[7]
						reserves_lbs_mod = buf_word[8]
						reserves_kgs_mod = buf_word[9]
						cost_index_mod = buf_word[10]
					else
						error_loading = 12
					end
				end
			end
			
			
			-- thrust setting
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						-- B738DR_fms_test7 = 9
						sel_temp = buf_word[1]
						sel_temp_f = buf_word[2]
						oat = buf_word[3]
						oat_f = buf_word[4]
						if buf_word[5] == "1" then
							to_1 = "<ACT>"
							to = "     "
							to_2 = "     "
						elseif buf_word[5] == "2" then
							to_2 = "<ACT>"
							to = "     "
							to_1 = "     "
						else
							to = "<ACT>"
							to_1 = "     "
							to_2 = "     "
						end
						if buf_word[6] == "1" then
							clb_1 = "<SEL>"
							clb = "     "
							clb_2 = "     "
						elseif buf_word[6] == "2" then
							clb_2 = "<SEL>"
							clb = "     "
							clb_1 = "     "
						else
							clb = "<SEL>"
							clb_1 = "     "
							clb_2 = "     "
						end
						if buf_word[7] == "1" then
							ga_act = "<ACT>"
							con_act = "     "
							clb_act = "     "
							crz_act = "     "
							auto_act = "     "
						elseif buf_word[7] == "2" then
							con_act = "<ACT>"
							ga_act = "     "
							clb_act = "     "
							crz_act = "     "
							auto_act = "     "
						elseif buf_word[7] == "3" then
							clb_act = "<ACT>"
							ga_act = "     "
							con_act = "     "
							crz_act = "     "
							auto_act = "     "
						elseif buf_word[7] == "4" then
							crz_act = "<ACT>"
							ga_act = "     "
							con_act = "     "
							clb_act = "     "
							auto_act = "     "
						else
							auto_act = "<ACT>"
							ga_act = "     "
							con_act = "     "
							clb_act = "     "
							crz_act = "     "
							crz_act = "     "
						end
					else
						error_loading = 13
					end
				end
			else
				error_loading = 14
			end
			
			-- takeoff 01
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 10 then
						-- B738DR_fms_test7 = 10
						flaps = buf_word[1]
						v1_set = buf_word[2]
						vr_set = buf_word[3]
						v2_set = buf_word[4]
						cg = buf_word[5]
						trim = buf_word[6]
						rw_wind_dir = buf_word[7]
						rw_wind_spd = buf_word[8]
						rw_slope = buf_word[9]
						rw_hdg = buf_word[10]
						num_tmp = tonumber(buf_word[11])
						if num_tmp ~= nil then
							rw_cond = num_tmp
						else
							rw_cond = 0
						end
					else
						error_loading = 15
					end
				end
			end
			
			-- takeoff 02
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 3 then
						-- B738DR_fms_test7 = 11
						accel_alt = buf_word[1]
						eo_accel_alt = buf_word[2]
						clb_alt = buf_word[3]
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							B738DR_thr_cutback = num_tmp
						else
							B738DR_thr_cutback = 0
						end
					else
						error_loading = 16
					end
				end
			end
			
			-- approach
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 7 then
						-- B738DR_fms_test7 = 12
						gw_app = buf_word[1]
						gw_app_lbs = buf_word[2]
						gw_app_kgs = buf_word[3]
						flaps_app = buf_word[4]
						app_flap = buf_word[5]
						app_spd = buf_word[6]
						wind_corr = buf_word[7]
						num_tmp = tonumber(buf_word[8])
						if num_tmp ~= nil then
							B738DR_fms_ils_disable = num_tmp
						else
							B738DR_fms_ils_disable = 0
						end
					else
						error_loading = 17
					end
				end
			end
			
			-- descent forecast 01
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 7 then
						-- B738DR_fms_test7 = 13
						trans_lvl = buf_word[1]
						cabin_rate = buf_word[2]
						forec_isa_dev = buf_word[3]
						forec_qnh = buf_word[4]
						tai_on_alt = buf_word[5]
						tai_off_alt = buf_word[6]
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							tai_on_alt_num = num_tmp
						else
							tai_on_alt_num = 0
						end
						num_tmp = tonumber(buf_word[8])
						if num_tmp ~= nil then
							tai_off_alt_num = num_tmp
						else
							tai_off_alt_num = 0
						end
						
						trans_lvl_mod = trans_lvl
						cabin_rate_mod = cabin_rate
						forec_isa_dev_mod = forec_isa_dev
						forec_qnh_mod = forec_qnh
						tai_on_alt_mod = tai_on_alt
						tai_off_alt_mod = tai_off_alt
						tai_on_alt_num_mod = tai_on_alt_num
						tai_off_alt_num_mod = tai_off_alt_num
					else
						error_loading = 18
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 12
			-- end
			
			-- descent forecast 02
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 3 then
						-- B738DR_fms_test7 = 14
						forec_alt_1 = buf_word[1]
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							forec_alt_1_num = num_tmp
						else
							forec_alt_1_num = 0
						end
						forec_dir_1 = buf_word[3]
						forec_spd_1 = buf_word[4]
						
						forec_alt_1_mod = forec_alt_1
						forec_alt_1_num_mod = forec_alt_1_num
						forec_dir_1_mod = forec_dir_1
						forec_spd_1_mod = forec_spd_1
					else
						error_loading = 19
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 13
			-- end
			
			-- descent forecast 03
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 3 then
						-- B738DR_fms_test7 = 15
						forec_alt_2 = buf_word[1]
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							forec_alt_2_num = num_tmp
						else
							forec_alt_2_num = 0
						end
						forec_dir_2 = buf_word[3]
						forec_spd_2 = buf_word[4]
						
						forec_alt_2_mod = forec_alt_2
						forec_alt_2_num_mod = forec_alt_2_num
						forec_dir_2_mod = forec_dir_2
						forec_spd_2_mod = forec_spd_2
					else
						error_loading = 20
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 14
			-- end
			
			-- descent forecast 04
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 3 then
						-- B738DR_fms_test7 = 16
						forec_alt_3 = buf_word[1]
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							forec_alt_3_num = num_tmp
						else
							forec_alt_3_num = 0
						end
						forec_dir_3 = buf_word[3]
						forec_spd_3 = buf_word[4]
						
						forec_alt_3_mod = forec_alt_3
						forec_alt_3_num_mod = forec_alt_3_num
						forec_dir_3_mod = forec_dir_3
						forec_spd_3_mod = forec_spd_3
					else
						error_loading = 21
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 15
			-- end
			
			-- state fms 01
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						-- B738DR_fms_test7 = 17
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							B738DR_flight_phase = num_tmp
						else
							B738DR_flight_phase = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							B738DR_fms_descent_now = num_tmp
						else
							B738DR_fms_descent_now = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							B738DR_missed_app_act = num_tmp
						else
							B738DR_missed_app_act = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							altitude_last = num_tmp
						else
							altitude_last = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							takeoff_enable = num_tmp
						else
							takeoff_enable = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							climb_enable = num_tmp
						else
							climb_enable = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							if num_tmp == 0 then
								ga_data_deleted = false
							else
								ga_data_deleted = true
							end
						else
							ga_data_deleted = false
						end
					else
						error_loading = 22
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 16
			-- end
			
			-- state fms 02
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						-- B738DR_fms_test7 = 18
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							nav_mode = num_tmp
						else
							nav_mode = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							B738DR_hold_phase = num_tmp
						else
							B738DR_hold_phase = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							vnav_alt_mode = num_tmp
						else
							vnav_alt_mode = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							B738DR_idx_corr = num_tmp
						else
							B738DR_idx_corr = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							B738DR_idx_corr2 = num_tmp
						else
							B738DR_idx_corr2 = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							B738DR_idx_corr3 = num_tmp
						else
							B738DR_idx_corr3 = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							B738DR_idx_corr4 = num_tmp
						else
							B738DR_idx_corr4 = 0
						end
					else
						error_loading = 23
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 17
			-- end
			
			-- state fms 03
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						-- B738DR_fms_test7 = 18
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							af_lat = num_tmp
						else
							af_lat = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							af_lon = num_tmp
						else
							af_lon = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							af_dist = num_tmp
						else
							af_dist = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							af_turn = num_tmp
						else
							af_turn = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							radii_lat = num_tmp
						else
							radii_lat = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							radii_lon = num_tmp
						else
							radii_lon = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							radii_dist = num_tmp
						else
							radii_dist = 0
						end
					else
						error_loading = 24
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 18
			-- end
			
			-- state fms 04
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 6 then
						-- B738DR_fms_test7 = 18
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							radii_turn_dir = num_tmp
						else
							radii_turn_dir = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							radii_begin_crs = num_tmp
						else
							radii_begin_crs = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							radii_end_crs = num_tmp
						else
							radii_end_crs = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							radii_end_angle = num_tmp
						else
							radii_end_angle = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							radii_remain_dist = num_tmp
						else
							radii_remain_dist = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							turn_radii_used = num_tmp
						else
							turn_radii_used = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							last_dta = num_tmp
						else
							last_dta = 0
						end
					else
						error_loading = 25
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 19
			-- end
			
			-- state fms 05
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 2 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							legs_intdir_act = num_tmp
						else
							legs_intdir_act = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							legs_intdir_crs = num_tmp
						else
							legs_intdir_crs = -1
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							legs_intdir_trk = num_tmp
						else
							legs_intdir_trk = 0
						end
						if legs_intdir_act == 0 then
							legs_intdir_idx = 0
						else
							legs_intdir_idx = offset
						end
						-- if legs_intdir_act == 1 and legs_intdir_crs == -1 then
							-- legs_intdir_act = 3
						-- end
					else
						error_loading = 26
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 20
			-- end
			
			-- state fms 06 CLB
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 5 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							B738DR_fmc_climb_speed = num_tmp
						else
							B738DR_fmc_climb_speed = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							B738DR_fmc_climb_speed_mach = num_tmp
						else
							B738DR_fmc_climb_speed_mach = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							B738DR_fmc_climb_r_speed1 = num_tmp
						else
							B738DR_fmc_climb_r_speed1 = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							B738DR_fmc_climb_r_alt1 = num_tmp
						else
							B738DR_fmc_climb_r_alt1 = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							B738DR_fmc_climb_r_speed2 = num_tmp
						else
							B738DR_fmc_climb_r_speed2 = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							B738DR_fmc_climb_r_alt2 = num_tmp
						else
							B738DR_fmc_climb_r_alt2 = 0
						end
						
						fmc_climb_r_alt1 = string.format("%5d", B738DR_fmc_climb_r_alt1)
						climb_r_alt1_mod = B738DR_fmc_climb_r_alt1
						climb_r_speed1_mod = B738DR_fmc_climb_r_speed1
						fmc_climb_r_alt1_mod = fmc_climb_r_alt1
						
					else
						error_loading = 27
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 21
			-- end
			
			-- state fms 07 CRS
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 2 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							B738DR_fmc_cruise_speed = num_tmp
						else
							B738DR_fmc_cruise_speed = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							B738DR_fmc_cruise_speed_mach = num_tmp
						else
							B738DR_fmc_cruise_speed_mach = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							B738DR_fmc_cruise_alt = num_tmp
						else
							B738DR_fmc_cruise_alt = 0
						end
					else
						error_loading = 28
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 22
			-- end
			
			-- state fms 08 DES
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 7 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							B738DR_fmc_descent_speed = num_tmp
						else
							B738DR_fmc_descent_speed = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							B738DR_fmc_descent_speed_mach = num_tmp
						else
							B738DR_fmc_descent_speed_mach = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							B738DR_fmc_descent_alt = num_tmp
						else
							B738DR_fmc_descent_alt = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							B738DR_fmc_descent_r_speed1 = num_tmp
						else
							B738DR_fmc_descent_r_speed1 = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							B738DR_fmc_descent_r_alt1 = num_tmp
						else
							B738DR_fmc_descent_r_alt1 = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							B738DR_fmc_descent_r_speed2 = num_tmp
						else
							B738DR_fmc_descent_r_speed2 = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							B738DR_fmc_descent_r_alt2 = num_tmp
						else
							B738DR_fmc_descent_r_alt2 = 0
						end
						num_tmp = tonumber(buf_word[8])
						if num_tmp ~= nil then
							B738DR_fmc_approach_alt = num_tmp
						else
							B738DR_fmc_approach_alt = 0
						end
						
						fmc_descent_r_alt1 = string.format("%5d", B738DR_fmc_descent_r_alt1)
						descent_r_alt1_mod = B738DR_fmc_descent_r_alt1
						descent_r_speed1_mod = B738DR_fmc_descent_r_speed1
						fmc_descent_r_alt1_mod = fmc_descent_r_alt1
						
					else
						error_loading = 29
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 23
			-- end
			
			-- state fms 09 ECON
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 11 then
						num_tmp = tonumber(buf_word[1])
						if num_tmp ~= nil then
							econ_clb_spd = num_tmp
						else
							econ_clb_spd = 0
						end
						num_tmp = tonumber(buf_word[2])
						if num_tmp ~= nil then
							econ_clb_spd_mach = num_tmp
						else
							econ_clb_spd_mach = 0
						end
						num_tmp = tonumber(buf_word[3])
						if num_tmp ~= nil then
							econ_crz_spd = num_tmp
						else
							econ_crz_spd = 0
						end
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							econ_crz_spd_mach = num_tmp
						else
							econ_crz_spd_mach = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							econ_des_spd = num_tmp
						else
							econ_des_spd = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							econ_des_spd_mach = num_tmp
						else
							econ_des_spd_mach = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							lrc_crz_spd = num_tmp
						else
							lrc_crz_spd = 0
						end
						num_tmp = tonumber(buf_word[8])
						if num_tmp ~= nil then
							lrc_crz_spd_mach = num_tmp
						else
							lrc_crz_spd_mach = 0
						end
						num_tmp = tonumber(buf_word[9])
						if num_tmp ~= nil then
							econ_des_vpa = num_tmp
						else
							econ_des_vpa = 0
						end
						num_tmp = tonumber(buf_word[10])
						if num_tmp ~= nil then
							B738DR_climb_mode = num_tmp
						else
							B738DR_climb_mode = 0
						end
						fmc_climb_mode_mod = B738DR_climb_mode
						num_tmp = tonumber(buf_word[11])
						if num_tmp ~= nil then
							B738DR_cruise_mode = num_tmp
						else
							B738DR_cruise_mode = 0
						end
						fmc_cruise_mode_mod = B738DR_cruise_mode
						num_tmp = tonumber(buf_word[12])
						if num_tmp ~= nil then
							B738DR_descent_mode = num_tmp
						else
							B738DR_descent_mode = 0
						end
						fmc_descent_mode_mod = B738DR_descent_mode
					else
						error_loading = 30
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 24
			-- end
			
			-- state fms 09 ECON
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					if split_buffer(fms_line) > 17 then
						dl_ref = spaces_before(buf_word[1], 4)
						dl_des = spaces_before(buf_word[2], 4)
						dl_flt_num = buf_word[3]
						num_tmp = tonumber(buf_word[4])
						if num_tmp ~= nil then
							dl_day = num_tmp
						else
							dl_day = 0
						end
						num_tmp = tonumber(buf_word[5])
						if num_tmp ~= nil then
							dl_out_hrs = num_tmp
						else
							dl_out_hrs = 0
						end
						num_tmp = tonumber(buf_word[6])
						if num_tmp ~= nil then
							dl_out_min = num_tmp
						else
							dl_out_min = 0
						end
						num_tmp = tonumber(buf_word[7])
						if num_tmp ~= nil then
							dl_out_fuel = num_tmp
						else
							dl_out_fuel = -1
						end
						num_tmp = tonumber(buf_word[8])
						if num_tmp ~= nil then
							dl_in_hrs = num_tmp
						else
							dl_in_hrs = 0
						end
						num_tmp = tonumber(buf_word[9])
						if num_tmp ~= nil then
							dl_in_min = num_tmp
						else
							dl_in_min = 0
						end
						num_tmp = tonumber(buf_word[10])
						if num_tmp ~= nil then
							dl_in_fuel = num_tmp
						else
							dl_in_fuel = -1
						end
						num_tmp = tonumber(buf_word[11])
						if num_tmp ~= nil then
							dl_on_hrs = num_tmp
						else
							dl_on_hrs = 0
						end
						num_tmp = tonumber(buf_word[12])
						if num_tmp ~= nil then
							dl_on_min = num_tmp
						else
							dl_on_min = 0
						end
						num_tmp = tonumber(buf_word[13])
						if num_tmp ~= nil then
							dl_on_fuel = num_tmp
						else
							dl_on_fuel = -1
						end
						num_tmp = tonumber(buf_word[14])
						if num_tmp ~= nil then
							dl_off_hrs = num_tmp
						else
							dl_off_hrs = 0
						end
						num_tmp = tonumber(buf_word[15])
						if num_tmp ~= nil then
							dl_off_min = num_tmp
						else
							dl_off_min = 0
						end
						num_tmp = tonumber(buf_word[16])
						if num_tmp ~= nil then
							dl_off_fuel = num_tmp
						else
							dl_off_fuel = -1
						end
						num_tmp = tonumber(buf_word[17])
						if num_tmp ~= nil then
							dl_flight_time = num_tmp
						else
							dl_flight_time = -1
						end
						num_tmp = tonumber(buf_word[18])
						if num_tmp ~= nil then
							dl_block_time = num_tmp
						else
							dl_block_time = -1
						end
						
					else
						error_loading = 31
					end
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 25
			-- end
			
			-- fix_num
			fmc_data_fix_num = 0
			if error_loading == 0 then
				fms_line = file_fmcdata:read()
				if fms_line then
					-- B738DR_fms_test7 = 19
					num_tmp = tonumber(fms_line)
					if num_tmp == nil then
						error_loading = 32
					else
						fmc_data_fix_num = num_tmp
					end
				else
					error_loading = 33
				end
			end
			
			-- B738DR_fms_test6 = fmc_data_fix_num
			
			if error_loading == 0 and fmc_data_fix_num > 0 then
				fmc_data_idx = 0
				fms_line = file_fmcdata:read()
				while fms_line do
					ii = split_buffer(fms_line)
					data_ok = 0
					if fmc_data_idx < 3 then
						if ii == 10 then
							data_ok = 1
						end
					else
						if ii == 8 then
							data_ok = 1
						end
					end
					if data_ok ~= 0 then
						if fmc_data_idx == 0 then
							fmc_fix_data_num = fmc_fix_data_num + 1
							fmc_fix_data[fmc_fix_data_num] = {}
							for vvvv = 1, 38 do
								if vvvv == 1 or vvvv == 5 then
									fmc_fix_data[fmc_fix_data_num][vvvv] = ""
								elseif vvvv >= 6 and vvvv <= 11 then
									fmc_fix_data[fmc_fix_data_num][vvvv] = -1
								else
									fmc_fix_data[fmc_fix_data_num][vvvv] = 0
								end
							end
						end
						for vvv = 1, ii do
							vvvv = fmc_data_idx * 10 + vvv
							if vvvv == 1 or vvvv == 5 then
								if buf_word[vvv] == " " then
									fmc_fix_data[fmc_fix_data_num][vvvv] = ""
								else
									fmc_fix_data[fmc_fix_data_num][vvvv] = buf_word[vvv]
								end
							else
								num_tmp = tonumber(buf_word[vvv])
								if num_tmp == nil then
									fmc_fix_data[fmc_fix_data_num][vvvv] = 0
								else
									fmc_fix_data[fmc_fix_data_num][vvvv] = num_tmp
								end
							end
						end
						fmc_data_idx = fmc_data_idx + 1
						if fmc_data_idx > 3 then
							fmc_data_idx = 0
							if fmc_fix_data_num == fmc_data_fix_num then
								break
							end
						end
					end
					fms_line = file_fmcdata:read()
				end
			end
			
			-- B738DR_fms_test6 = fmc_data_fix_num
			
			if fmc_fix_data_num == fmc_data_fix_num and fmc_fix_data_num > 0 then
				fix_data = {}
				for vvv = 1, fmc_fix_data_num do
					fix_data[vvv] = {}
					for vvvv = 1, 38 do
						fix_data[vvv][vvvv] = fmc_fix_data[vvv][vvvv]
					end
				end
				fix_data_num = fmc_fix_data_num
				
				fix_abm_act = {}
				fix_abm_wpt_idx = {}
				fix_abm_wpt_dist = {}
				fix_abm_lat = {}
				fix_abm_lon = {}
				fix_abm_brg = {}
				fix_abm_dist = {}
				fix_abm_eta = {}
				fix_abm_alt = {}
				for eee = 1, 5 do
					fix_abm_act[eee] = 0
					fix_abm_wpt_idx[eee] = -1
					fix_abm_wpt_dist[eee] = -1
					fix_abm_lat[eee] = -1
					fix_abm_lon[eee] = -1
					fix_abm_brg[eee] = -1
					fix_abm_dist[eee] = -1
					fix_abm_eta[eee] = -1
					fix_abm_alt[eee] = -1
				end
			end
			
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 26
			-- end
			
			-- legs_num
			fmc_data_fix_num = 0
			legs_num_save = 0
			fmc_data_num = 0
			if error_loading == 0 then
				error_loading = 34
				fms_line = file_fmcdata:read()
				if fms_line then
					num_tmp = tonumber(fms_line)
					if num_tmp ~= nil then
						legs_num_save = num_tmp
						error_loading = 0
					end
				end
			end
			
			if legs_num_save > 0 then
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						-- B738DR_fms_test7 = 20
						num_tmp = tonumber(fms_line)
						if num_tmp == nil then
							error_loading = 35
						else
							fmc_data_legs_num = num_tmp
						end
					else
						error_loading = 36
					end
				end
				
				if error_loading == 0 and legs_num_save > 0 then
					fmc_data_idx = 0
					fms_line = file_fmcdata:read()
					while fms_line do
						ii = split_buffer(fms_line)
						data_ok = 0
						if fmc_data_idx < 5 then
							if ii == 10 then
								data_ok = 1
							end
						else
							-- 54 max legs[][54]
							if ii <= 4 then
								data_ok = 1
							end
						end
						if data_ok ~= 0 then
							if fmc_data_idx == 0 then
								fmc_data_num = fmc_data_num + 1
								add_empty_record(fmc_data_num)
							end
							for vvv = 1, ii do
								vvvv = fmc_data_idx * 10 + vvv
								if legs_data_type[vvvv] == 0 then
									num_tmp = tonumber(buf_word[vvv])
									if num_tmp == nil then
										fmc_data[fmc_data_num][vvvv] = 0
									else
										fmc_data[fmc_data_num][vvvv] = num_tmp
									end
								else
									if buf_word[vvv] == " " then
										fmc_data[fmc_data_num][vvvv] = ""
									else
										fmc_data[fmc_data_num][vvvv] = buf_word[vvv]
									end
								end
							end
							fmc_data_idx = fmc_data_idx + 1
							if fmc_data_idx > 5 then
								fmc_data_idx = 0
								if fmc_data_num == legs_num_save then
									break
								end
							end
						end
						fms_line = file_fmcdata:read()
					end
				end
				
				if (fmc_data_num == legs_num_save) and (legs_num_save > 0) and rte12_act >= 0 then
					
					legs_data = {}
					for vvv = 1, fmc_data_num do
						legs_data[vvv] = {}
						for vvvv = 1, MAX_LEGS_DATA do
							legs_data[vvv][vvvv] = fmc_data[vvv][vvvv]
						end
					end
					legs_num = fmc_data_legs_num
					
					if rte12_act == 0 then
						copy_to_legsdata2(rte12_act)
						
						ref_icao_x = ref_icao
						ref_icao2 = ref_icao
						des_icao_x = des_icao
						
						des_app_from_apt = 0
						
						ref_rwy2 = ref_rwy
						ref_sid2 = ref_sid
						ref_sid_tns2 = ref_sid_tns
						des_app2 = des_app
						des_app_tns2 = des_app_tns
						des_star2 = des_star
						des_star_trans2 = des_star_trans
						co_route_x = co_route
						
						sel_ref_rwy2 = ref_rwy2
						sel_ref_sid2 = ref_sid2
						sel_ref_sid_tns2 = ref_sid_tns2
						sel_des_star2 = des_star2
						sel_des_star_trans2 = des_star_trans2
						sel_des_app2 = des_app2
						sel_des_app_tns2 = des_app_tns2
						
						des_star2_old = des_star2
						des_star_trans2_old = des_star_trans2
						des_app2_old = des_app2
						des_app_tns2_old = des_app_tns2
						
						-- offset leg
						offset_act = 0
						offset_start = 0
						offset_end = 0
						offset_dist = 0
						offset_side = 0
						offset_act_mod = 0
						offset_start_mod = 0
						offset_end_mod = 0
						offset_dist_mod = 0
						offset_side_mod = 0
						
						if ref_tns_alt == 0 then
							trans_alt = "-----"
						else
							trans_alt = string.format("%5d", ref_tns_alt)
						end
						trans_alt_mod = trans_alt
						ref_tns_alt_x = ref_tns_alt
						
						if des_tns_lvl == 0 then
							trans_lvl = "-----"
						else
							num_tmp = des_tns_lvl / 100
							trans_lvl = "FL" .. string.format("%03d", num_tmp)
						end
						trans_lvl_mod = trans_lvl
						des_tns_lvl_x = des_tns_lvl
						
						-- lists of SID/STAR/APP
						arr_data = 0
						arr_data_x = 0
						airport_pos(rte12_act)
						
						local apt_ok = 0
						if cust_def_data == 1 then
							file_name = "Custom Data/CIFP/" .. ref_icao_x
							file_name = file_name .. ".dat"
							file_navdata = io.open(file_name, "r")
							if file_navdata == nil then
								if apt_exist(ref_icao_x) == true then
									apt_ok = 1
									sid_num = 0
									sid_tns_num = 0
									star_num = 0
									ref_data_star_tns_n = 0
									ref_data_app_n = 0
									ref_data_app_tns_n = 0
									ref_ed_app_n = 0
								end
							else
								read_ref_data(ref_icao_x, 0)		-- read reference airport data
								file_navdata:close()
								apt_ok = 1
							end
						elseif cust_def_data == 2 then
							file_name = "Resources/default data/CIFP/" .. ref_icao_x
							file_name = file_name .. ".dat"
							file_navdata = io.open(file_name, "r")
							if file_navdata == nil then
								if apt_exist(ref_icao_x) == true then
									apt_ok = 1
									sid_num = 0
									sid_tns_num = 0
									star_num = 0
									ref_data_star_tns_n = 0
									ref_data_app_n = 0
									ref_data_app_tns_n = 0
									ref_ed_app_n = 0
								end
							else
								read_ref_data(ref_icao_x, 0)		-- read reference airport data
								file_navdata:close()
								apt_ok = 1
							end
						end
						
						apt_ok = 0
						if apt_exist(des_icao_x) == true then
							if cust_def_data == 1 then
								file_name = "Custom Data/CIFP/" .. des_icao_x
								file_name = file_name .. ".dat"
								file_navdata = io.open(file_name, "r")
								if file_navdata == nil then
									if apt_exist(des_icao_x) == true then
										apt_ok = 1
										data_des_star_n = 0
										data_des_star_tns_n = 0
										data_des_app_n = 0
										data_des_app_tns_n = 0
										ed_app_num = 0
									end
								else
									read_des_data(des_icao_x, 0)		-- read destination airport data
									file_navdata:close()
									apt_ok = 2
								end
							elseif cust_def_data == 2 then
								file_name = "Resources/default data/CIFP/" .. des_icao_x
								file_name = file_name .. ".dat"
								file_navdata = io.open(file_name, "r")
								if file_navdata == nil then
									if apt_exist(des_icao_x) == true then
										apt_ok = 1
										data_des_star_n = 0
										data_des_star_tns_n = 0
										data_des_app_n = 0
										data_des_app_tns_n = 0
										ed_app_num = 0
									end
								else
									read_des_data(des_icao_x, 0)		-- read destination airport data
									file_navdata:close()
									apt_ok = 2
								end
							end
						end
						
						create_rnw_list(rte12_act)
						create_sid_list(rte12_act)
						create_sid_tns_list(rte12_act)
						create_star_list(rte12_act)
						create_star_tns_list(rte12_act)
						create_app_list(rte12_act)
						create_app_tns_list(rte12_act)
						
						
						-- find rnav,...
						-- find_rnw_data()
						find_ref_rnw_data(0, 0)
						find_des_rnw_data(0, 0)
						
						B738_find_rnav()
						B738DR_dest_runway_len = 0
						B738DR_dest_runway_crs = 0
						B738DR_dest_runway_start_lat = 0
						B738DR_dest_runway_start_lon = 0
						B738DR_dest_runway_end_lat = 0
						B738DR_dest_runway_end_lon = 0
						B738DR_dest_runway_id = ""
						
						B738DR_dest_runway_len_mod = 0
						B738DR_dest_runway_crs_mod = 0
						B738DR_dest_runway_start_lat_mod = 0
						B738DR_dest_runway_start_lon_mod = 0
						B738DR_dest_runway_end_lat_mod = 0
						B738DR_dest_runway_end_lon_mod = 0
						
						local rnw_alt_found = 0
						local jj = 0
						local kk = 0
						local rnw_temp = ""
						
						if ref_rwy ~= "-----" then
							ref_rnw_length_crs(rte12_act)
							if ref_rwy_num > 0 then
								if string.len(ref_rwy) == 2 then
									rnw_temp = ref_rwy .. " "
								else
									rnw_temp = ref_rwy
								end
								for jj = 1, ref_rwy_num do
									if ref_data_rwy[jj][1] == rnw_temp then
										kk = tonumber(ref_data_rwy[jj][2])
										if kk ~= nil then
											B738DR_ref_rwy_altitude = kk
											rnw_alt_found = 1
										end
										break
									end
								end
							end
						end
						if rnw_alt_found == 0 then
							B738DR_ref_rwy_altitude = ref_icao_alt
						end
						
						rnw_alt_found = 0
						B738DR_missed_app_alt = 0
						if des_app ~= "------" then
							if B738DR_dest_runway_alt == -999 then
								B738DR_dest_runway_alt = des_icao_alt
							end
							des_rnw_length_crs(rte12_act)
							-- find des runway alt
							if des_rwy_num > 0 then
								if string.sub(des_app, 1, 2) == "RW" then
									rnw_temp = des_app
								else
									if string.len(des_app) > 4 then
										jj, kk = string.find(des_app, "-")
										if jj == nil then
											rnw_temp = "RW" .. string.sub(des_app, 2, -2)
										else
											rnw_temp = "RW" .. string.sub(des_app, 2, jj-1)
										end
									else
										rnw_temp = "RW" .. string.sub(des_app, 2, -1)
									end
								end
								if string.len(rnw_temp) == 4 then
									rnw_temp = rnw_temp .. " "
								end
								for jj = 1, des_rwy_num do
									if des_data_rwy[jj][1] == rnw_temp then
										kk = tonumber(des_data_rwy[jj][5])
										if kk ~= nil then
											B738DR_des_rwy_altitude = kk
											rnw_alt_found = 1
										end
										break
									end
								end
							end
							B738DR_missed_app_alt = legs_data[legs_num][5]
						end
						if rnw_alt_found == 0 then
							B738DR_des_rwy_altitude = des_icao_alt
						end
						find_id_app_data(rte12_act)
					elseif rte12_act == 1 then
						copy_to_legsdata2(rte12_act)
						
						ref_icao7_x = ref_icao7
						ref_icao8 = ref_icao7
						des_icao7_x = des_icao7
						
						des_app_from_apt7 = 0
						
						ref_rwy8 = ref_rwy7
						ref_sid8 = ref_sid7
						ref_sid_tns8 = ref_sid_tns7
						des_app8 = des_app7
						des_app_tns8 = des_app_tns7
						des_star8 = des_star7
						des_star_trans8 = des_star_trans7
						co_route7_x = co_route7
						
						sel_ref_rwy8 = ref_rwy8
						sel_ref_sid8 = ref_sid8
						sel_ref_sid_tns8 = ref_sid_tns8
						sel_des_star8 = des_star8
						sel_des_star_trans8 = des_star_trans8
						sel_des_app8 = des_app8
						sel_des_app_tns8 = des_app_tns8
						
						des_star8_old = des_star8
						des_star_trans8_old = des_star_trans8
						des_app8_old = des_app8
						des_app_tns8_old = des_app_tns8
						
						-- offset leg
						offset_act = 0
						offset_start = 0
						offset_end = 0
						offset_dist = 0
						offset_side = 0
						offset_act_mod = 0
						offset_start_mod = 0
						offset_end_mod = 0
						offset_dist_mod = 0
						offset_side_mod = 0
						
						if ref_tns_alt7 == 0 then
							trans_alt7 = "-----"
						else
							trans_alt7 = string.format("%5d", ref_tns_alt7)
						end
						trans_alt_mod2 = trans_alt7
						ref_tns_alt7_x = ref_tns_alt7
						
						if des_tns_lvl == 0 then
							trans_lvl7 = "-----"
						else
							num_tmp = des_tns_lvl / 100
							trans_lvl7 = "FL" .. string.format("%03d", num_tmp)
						end
						trans_lvl_mod2 = trans_lvl7
						des_tns_lvl7_x = des_tns_lvl7
						
						-- lists of SID/STAR/APP
						arr_data7 = 0
						arr_data7_x = 0
						airport_pos(rte12_act)
						
						local apt_ok = 0
						if cust_def_data == 1 then
							file_name = "Custom Data/CIFP/" .. ref_icao7_x
							file_name = file_name .. ".dat"
							file_navdata = io.open(file_name, "r")
							if file_navdata == nil then
								if apt_exist(ref_icao7_x) == true then
									apt_ok = 1
									sid_num = 0
									sid_tns_num = 0
									star_num = 0
									ref_data_star_tns_n = 0
									ref_data_app_n = 0
									ref_data_app_tns_n = 0
									ref_ed_app_n = 0
								end
							else
								read_ref_data(ref_icao7_x, 1)		-- read reference airport data
								file_navdata:close()
								apt_ok = 1
							end
						elseif cust_def_data == 2 then
							file_name = "Resources/default data/CIFP/" .. ref_icao7_x
							file_name = file_name .. ".dat"
							file_navdata = io.open(file_name, "r")
							if file_navdata == nil then
								if apt_exist(ref_icao_x) == true then
									apt_ok = 1
									sid_num = 0
									sid_tns_num = 0
									star_num = 0
									ref_data_star_tns_n = 0
									ref_data_app_n = 0
									ref_data_app_tns_n = 0
									ref_ed_app_n = 0
								end
							else
								read_ref_data(ref_icao7_x, 1)		-- read reference airport data
								file_navdata:close()
								apt_ok = 1
							end
						end
						
						apt_ok = 0
						if apt_exist(des_icao7_x) == true then
							if cust_def_data == 1 then
								file_name = "Custom Data/CIFP/" .. des_icao7_x
								file_name = file_name .. ".dat"
								file_navdata = io.open(file_name, "r")
								if file_navdata == nil then
									if apt_exist(des_icao_x) == true then
										apt_ok = 1
										data_des_star_n = 0
										data_des_star_tns_n = 0
										data_des_app_n = 0
										data_des_app_tns_n = 0
										ed_app_num = 0
									end
								else
									read_des_data(des_icao7_x, 1)		-- read destination airport data
									file_navdata:close()
									apt_ok = 2
								end
							elseif cust_def_data == 2 then
								file_name = "Resources/default data/CIFP/" .. des_icao7_x
								file_name = file_name .. ".dat"
								file_navdata = io.open(file_name, "r")
								if file_navdata == nil then
									if apt_exist(des_icao7_x) == true then
										apt_ok = 1
										data_des_star_n = 0
										data_des_star_tns_n = 0
										data_des_app_n = 0
										data_des_app_tns_n = 0
										ed_app_num = 0
									end
								else
									read_des_data(des_icao7_x, 1)		-- read destination airport data
									file_navdata:close()
									apt_ok = 2
								end
							end
						end
						
						create_rnw_list(rte12_act)
						create_sid_list(rte12_act)
						create_sid_tns_list(rte12_act)
						create_star_list(rte12_act)
						create_star_tns_list(rte12_act)
						create_app_list(rte12_act)
						create_app_tns_list(rte12_act)
						
						
						-- find rnav,...
						-- find_rnw_data()
						find_ref_rnw_data(0, 1)
						find_des_rnw_data(0, 1)
						
						B738_find_rnav()
						B738DR_dest_runway_len = 0
						B738DR_dest_runway_crs = 0
						B738DR_dest_runway_start_lat = 0
						B738DR_dest_runway_start_lon = 0
						B738DR_dest_runway_end_lat = 0
						B738DR_dest_runway_end_lon = 0
						B738DR_dest_runway_id = ""
						
						B738DR_dest_runway_len_mod = 0
						B738DR_dest_runway_crs_mod = 0
						B738DR_dest_runway_start_lat_mod = 0
						B738DR_dest_runway_start_lon_mod = 0
						B738DR_dest_runway_end_lat_mod = 0
						B738DR_dest_runway_end_lon_mod = 0
						
						local rnw_alt_found = 0
						local jj = 0
						local kk = 0
						local rnw_temp = ""
						
						if ref_rwy7 ~= "-----" then
							ref_rnw_length_crs(rte12_act)
							if ref_rwy_num > 0 then
								if string.len(ref_rwy7) == 2 then
									rnw_temp = ref_rwy7 .. " "
								else
									rnw_temp = ref_rwy7
								end
								for jj = 1, ref_rwy_num do
									if ref_data_rwy[jj][1] == rnw_temp then
										kk = tonumber(ref_data_rwy[jj][2])
										if kk ~= nil then
											B738DR_ref_rwy_altitude = kk
											rnw_alt_found = 1
										end
										break
									end
								end
							end
						end
						if rnw_alt_found == 0 then
							B738DR_ref_rwy_altitude = ref_icao_alt7
						end
						
						rnw_alt_found = 0
						B738DR_missed_app_alt = 0
						if des_app7 ~= "------" then
							if B738DR_dest_runway_alt == -999 then
								B738DR_dest_runway_alt = des_icao_alt7
							end
							des_rnw_length_crs(rte12_act)
							-- find des runway alt
							if des_rwy_num > 0 then
								if string.sub(des_app7, 1, 2) == "RW" then
									rnw_temp = des_app7
								else
									if string.len(des_app7) > 4 then
										jj, kk = string.find(des_app7, "-")
										if jj == nil then
											rnw_temp = "RW" .. string.sub(des_app7, 2, -2)
										else
											rnw_temp = "RW" .. string.sub(des_app7, 2, jj-1)
										end
									else
										rnw_temp = "RW" .. string.sub(des_app7, 2, -1)
									end
								end
								if string.len(rnw_temp) == 4 then
									rnw_temp = rnw_temp .. " "
								end
								for jj = 1, des_rwy_num do
									if des_data_rwy[jj][1] == rnw_temp then
										kk = tonumber(des_data_rwy[jj][5])
										if kk ~= nil then
											B738DR_des_rwy_altitude = kk
											rnw_alt_found = 1
										end
										break
									end
								end
							end
							B738DR_missed_app_alt = legs_data[legs_num][5]
						end
						if rnw_alt_found == 0 then
							B738DR_des_rwy_altitude = des_icao_alt7
						end
						find_id_app_data(rte12_act)
					end
					
				else
					if (fmc_data_num ~= legs_num_save) or (legs_num_save == 0) then
						error_loading = 37
					end
				end
			end
		end
		
		
		-- legs_num2
		legs_num_save = 0
		fmc_data_legs_num = 0
		fmc_data_num = 0
		if error_loading == 0 then
			if error_loading == 0 then
				error_loading = 34
				fms_line = file_fmcdata:read()
				if fms_line then
					num_tmp = tonumber(fms_line)
					if num_tmp ~= nil then
						legs_num_save = num_tmp
						error_loading = 0
					end
				end
			end
			
			if legs_num_save > 0 then
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						-- B738DR_fms_test7 = 20
						num_tmp = tonumber(fms_line)
						if num_tmp == nil then
							error_loading = 35
						else
							fmc_data_legs_num = num_tmp
						end
					else
						error_loading = 36
					end
				end
				
				if error_loading == 0 and legs_num_save > 0 then
					fmc_data_idx = 0
					fms_line = file_fmcdata:read()
					while fms_line do
						ii = split_buffer(fms_line)
						data_ok = 0
						if fmc_data_idx < 5 then
							if ii == 10 then
								data_ok = 1
							end
						else
							-- 54 max legs[][54]
							if ii <= 4 then
								data_ok = 1
							end
						end
						if data_ok ~= 0 then
							if fmc_data_idx == 0 then
								fmc_data_num = fmc_data_num + 1
								add_empty_record(fmc_data_num)
							end
							for vvv = 1, ii do
								vvvv = fmc_data_idx * 10 + vvv
								if legs_data_type[vvvv] == 0 then
									num_tmp = tonumber(buf_word[vvv])
									if num_tmp == nil then
										fmc_data[fmc_data_num][vvvv] = 0
									else
										fmc_data[fmc_data_num][vvvv] = num_tmp
									end
								else
									if buf_word[vvv] == " " then
										fmc_data[fmc_data_num][vvvv] = ""
									else
										fmc_data[fmc_data_num][vvvv] = buf_word[vvv]
									end
								end
							end
							fmc_data_idx = fmc_data_idx + 1
							if fmc_data_idx > 5 then
								fmc_data_idx = 0
								if fmc_data_num == legs_num_save then
									break
								end
							end
						end
						fms_line = file_fmcdata:read()
					end
				end
				
				if (fmc_data_num == legs_num_save) and (legs_num_save > 0) then
					if string.len(co_route_x) > 0 then
						upload_winds(co_route_x, 0)
					end
					if rte12_act ~= 0 then
						legs_data2 = {}
						for vvv = 1, fmc_data_num do
							legs_data2[vvv] = {}
							for vvvv = 1, MAX_LEGS_DATA do
								legs_data2[vvv][vvvv] = fmc_data[vvv][vvvv]
							end
						end
						legs_num2 = fmc_data_legs_num
					end
				end
			end
		else
			error_loading = 37
		end
		
		-- legs_num8
		legs_num_save = 0
		fmc_data_legs_num = 0
		fmc_data_num = 0
		if error_loading == 0 then
			if error_loading == 0 then
				error_loading = 34
				fms_line = file_fmcdata:read()
				if fms_line then
					num_tmp = tonumber(fms_line)
					if num_tmp ~= nil then
						legs_num_save = num_tmp
						error_loading = 0
					end
				end
			end
			
			if legs_num_save > 0 then
				if error_loading == 0 then
					fms_line = file_fmcdata:read()
					if fms_line then
						-- B738DR_fms_test7 = 20
						num_tmp = tonumber(fms_line)
						if num_tmp == nil then
							error_loading = 35
						else
							fmc_data_legs_num = num_tmp
						end
					else
						error_loading = 36
					end
				end
				
				if error_loading == 0 and legs_num_save > 0 then
					fmc_data_idx = 0
					fms_line = file_fmcdata:read()
					while fms_line do
						ii = split_buffer(fms_line)
						data_ok = 0
						if fmc_data_idx < 5 then
							if ii == 10 then
								data_ok = 1
							end
						else
							-- 54 max legs[][54]
							if ii <= 4 then
								data_ok = 1
							end
						end
						if data_ok ~= 0 then
							if fmc_data_idx == 0 then
								fmc_data_num = fmc_data_num + 1
								add_empty_record(fmc_data_num)
							end
							for vvv = 1, ii do
								vvvv = fmc_data_idx * 10 + vvv
								if legs_data_type[vvvv] == 0 then
									num_tmp = tonumber(buf_word[vvv])
									if num_tmp == nil then
										fmc_data[fmc_data_num][vvvv] = 0
									else
										fmc_data[fmc_data_num][vvvv] = num_tmp
									end
								else
									if buf_word[vvv] == " " then
										fmc_data[fmc_data_num][vvvv] = ""
									else
										fmc_data[fmc_data_num][vvvv] = buf_word[vvv]
									end
								end
							end
							fmc_data_idx = fmc_data_idx + 1
							if fmc_data_idx > 5 then
								fmc_data_idx = 0
								if fmc_data_num == legs_num_save then
									break
								end
							end
						end
						fms_line = file_fmcdata:read()
					end
				end
				
				if (fmc_data_num == legs_num_save) and (legs_num_save > 0) then
					if string.len(co_route7_x) > 0 then
						upload_winds(co_route7_x, 1)
					end
					if rte12_act ~= 1 then
						legs_data8 = {}
						for vvv = 1, fmc_data_num do
							legs_data8[vvv] = {}
							for vvvv = 1, MAX_LEGS_DATA do
								legs_data8[vvv][vvvv] = fmc_data[vvv][vvvv]
							end
						end
						legs_num8 = fmc_data_legs_num
					end
				end
			end
		else
			error_loading = 37
		end
		
		if error_loading == 0 then
			error_loading = 38
			-- if ii == 1 then
				fms_line = file_fmcdata:read()
			-- end
			if fms_line then
				if fms_line == "SYS" then
					error_loading = 0
				end
			end
		end
		
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 28
			-- end
			
		if error_loading == 0 then
			error_loading = 39
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_global[vvvv-1] = 0
						else
							B738DR_global[vvvv-1] = num_tmp
						end
					end
					error_loading = 0
				end
			end
		end
		
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 29
			-- end
			
		if error_loading == 0 then
			reset_fmc = 0
			-- Fuel
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				if ii == 3 then
					num_tmp = tonumber(buf_word[1])
					if num_tmp == nil then
						simDR_fuel_tank_weight_kg[0] = 0
					else
						simDR_fuel_tank_weight_kg[0] = num_tmp
					end
					num_tmp = tonumber(buf_word[2])
					if num_tmp == nil then
						simDR_fuel_tank_weight_kg[1] = 0
					else
						simDR_fuel_tank_weight_kg[1] = num_tmp
					end
					num_tmp = tonumber(buf_word[3])
					if num_tmp == nil then
						simDR_fuel_tank_weight_kg[2] = 0
					else
						simDR_fuel_tank_weight_kg[2] = num_tmp
					end
					-- B738DR_fms_test7 = 40
				else
					error_loading = 40
				end
			else
				error_loading = 41
			end
		end
		
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 30
			-- end
			
		if error_loading == 0 then
			error_loading = 42
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				if ii == 4 then
					num_tmp = tonumber(buf_word[1])
					if num_tmp == nil then
						B738DR_fa_fwd_kg = 0
					else
						B738DR_fa_fwd_kg = num_tmp
					end
					num_tmp = tonumber(buf_word[2])
					if num_tmp == nil then
						B738DR_fa_aft_kg = 0
					else
						B738DR_fa_aft_kg = num_tmp
					end
					num_tmp = tonumber(buf_word[3])
					if num_tmp == nil then
						B738DR_galley_fwd_kg = 0
					else
						B738DR_galley_fwd_kg = num_tmp
					end
					num_tmp = tonumber(buf_word[4])
					if num_tmp == nil then
						B738DR_galley_aft_kg = 0
					else
						B738DR_galley_aft_kg = num_tmp
					end
					-- B738DR_fms_test7 = 41
					fms_line = file_fmcdata:read()
					if fms_line then
						ii = split_buffer(fms_line)
						if ii == 50 then
							for vvv = 1, ii do
								num_tmp = tonumber(buf_word[vvv])
								if num_tmp == nil then
									B738DR_efb[vvv-1] = 0
								else
									B738DR_efb[vvv-1] = num_tmp
								end
								error_loading = 0
								-- B738DR_fms_test7 = 42
							end
						end
					end
				end
			end
		end
		
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 31
			-- end
			
		-- MMR CPT
		if error_loading == 0 then
			error_loading = 43
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				if ii == 3 then
					num_tmp = tonumber(buf_word[1])
					if num_tmp == nil then
						B738DR_mmr_cpt_act_mode = 0
					else
						B738DR_mmr_cpt_act_mode = num_tmp
					end
					num_tmp = tonumber(buf_word[2])
					if num_tmp == nil then
						B738DR_mmr_cpt_act_value = 0
					else
						B738DR_mmr_cpt_act_value = num_tmp
					end
					num_tmp = tonumber(buf_word[3])
					if num_tmp == nil then
						B738DR_mmr_cpt_stby_mode = 0
					else
						B738DR_mmr_cpt_stby_mode = num_tmp
					end
					fms_line = file_fmcdata:read()
					if fms_line then
						ii = split_buffer(fms_line)
						if ii == 5 then
							for vvv = 1, 5 do
								num_tmp = tonumber(buf_word[vvv])
								if num_tmp == nil then
									B738DR_mmr_cpt_stby_value[vvv-1] = 0
								else
									B738DR_mmr_cpt_stby_value[vvv-1] = num_tmp
								end
							end
							fms_line = file_fmcdata:read()
							if fms_line then
								ii = split_buffer(fms_line)
								if ii == 3 then
									num_tmp = tonumber(buf_word[1])
									if num_tmp == nil then
										B738DR_mmr_cpt_stby_cursor = 0
									else
										B738DR_mmr_cpt_stby_cursor = num_tmp
									end
									num_tmp = tonumber(buf_word[2])
									if num_tmp == nil then
										B738DR_mmr_cpt_stby_gls = 0
									else
										B738DR_mmr_cpt_stby_gls = num_tmp
									end
									num_tmp = tonumber(buf_word[3])
									if num_tmp == nil then
										B738DR_mmr_cpt_err = 0
									else
										B738DR_mmr_cpt_err = num_tmp
									end
									fms_line = file_fmcdata:read()
									if fms_line then
										ii = split_buffer(fms_line)
										if ii == 4 then
											for vvv = 1, 4 do
												num_tmp = tonumber(buf_word[vvv])
												if num_tmp == nil then
													B738DR_mmr_cpt_stby_cur[vvv-1] = 0
												else
													B738DR_mmr_cpt_stby_cur[vvv-1] = num_tmp
												end
											end
											error_loading = 0
											-- B738DR_fms_test7 = 43
										end
									end
								end
							end
						end
					end
				end
			end
		end
		
		-- MMR FO
		if error_loading == 0 then
			error_loading = 44
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				if ii == 3 then
					num_tmp = tonumber(buf_word[1])
					if num_tmp == nil then
						B738DR_mmr_fo_act_mode = 0
					else
						B738DR_mmr_fo_act_mode = num_tmp
					end
					num_tmp = tonumber(buf_word[2])
					if num_tmp == nil then
						B738DR_mmr_fo_act_value = 0
					else
						B738DR_mmr_fo_act_value = num_tmp
					end
					num_tmp = tonumber(buf_word[3])
					if num_tmp == nil then
						B738DR_mmr_fo_stby_mode = 0
					else
						B738DR_mmr_fo_stby_mode = num_tmp
					end
					fms_line = file_fmcdata:read()
					if fms_line then
						ii = split_buffer(fms_line)
						if ii == 5 then
							for vvv = 1, 5 do
								num_tmp = tonumber(buf_word[vvv])
								if num_tmp == nil then
									B738DR_mmr_fo_stby_value[vvv-1] = 0
								else
									B738DR_mmr_fo_stby_value[vvv-1] = num_tmp
								end
							end
							fms_line = file_fmcdata:read()
							if fms_line then
								ii = split_buffer(fms_line)
								if ii == 3 then
									num_tmp = tonumber(buf_word[1])
									if num_tmp == nil then
										B738DR_mmr_fo_stby_cursor = 0
									else
										B738DR_mmr_fo_stby_cursor = num_tmp
									end
									num_tmp = tonumber(buf_word[2])
									if num_tmp == nil then
										B738DR_mmr_fo_stby_gls = 0
									else
										B738DR_mmr_fo_stby_gls = num_tmp
									end
									num_tmp = tonumber(buf_word[3])
									if num_tmp == nil then
										B738DR_mmr_fo_err = 0
									else
										B738DR_mmr_fo_err = num_tmp
									end
									fms_line = file_fmcdata:read()
									if fms_line then
										ii = split_buffer(fms_line)
										if ii == 4 then
											for vvv = 1, 4 do
												num_tmp = tonumber(buf_word[vvv])
												if num_tmp == nil then
													B738DR_mmr_fo_stby_cur[vvv-1] = 0
												else
													B738DR_mmr_fo_stby_cur[vvv-1] = num_tmp
												end
											end
											error_loading = 0
											-- B738DR_fms_test7 = 44
										end
									end
								end
							end
						end
					end
				end
			end
		end
		
			-- if error_loading ~= 0 and B738DR_fms_test7 == 0 then
				-- B738DR_fms_test7 = 32
			-- end
			
		-- El failure
		if error_loading == 0 then
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_el_failure[vvvv-1] = 0
						else
							B738DR_el_failure[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 6 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		-- Hyd failure
		if error_loading == 0 then
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_hyd_failure[vvvv-1] = 0
						else
							B738DR_hyd_failure[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 6 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		-- Sys failure
		if error_loading == 0 then
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_sys_failure[vvvv-1] = 0
						else
							B738DR_sys_failure[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 6 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		-- Press failure
		if error_loading == 0 then
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_pressurisation_failure[vvvv-1] = 0
						else
							B738DR_pressurisation_failure[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 6 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		-- Glare
		if error_loading == 0 then
			-- B738DR_fms_test7 = 45
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_glare[vvvv-1] = 0
						else
							B738DR_glare[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 24 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
			-- B738DR_fms_test4 = 2
			-- B738DR_fms_test5 = B738DR_glare[226]
			-- B738DR_fms_test6 = B738DR_glare[138]
		end
		
		if error_loading == 0 then
			-- Glare SW
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_glare_sw[vvvv-1] = 0
						else
							B738DR_glare_sw[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 24 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Latlon
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_latlon[vvvv-1] = 0
						else
							B738DR_latlon[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 4 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Latlon SW
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_latlon_sw[vvvv-1] = 0
						else
							B738DR_latlon_sw[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 4 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- System
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_system[vvvv-1] = 0
						else
							B738DR_system[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 24 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- System SW
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_system_sw[vvvv-1] = 0
						else
							B738DR_system_sw[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 24 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Antiice
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_antiice[vvvv-1] = 0
						else
							B738DR_antiice[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 2 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Antiice SW
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_antiice_sw[vvvv-1] = 0
						else
							B738DR_antiice_sw[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 2 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Lights
			-- B738DR_fms_test7 = 46
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_lights[vvvv-1] = 0
						else
							B738DR_lights[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 1 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Lights SW
			fmc_data_idx = 0
			-- B738DR_fms_test7 = 47
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_lights_sw[vvvv-1] = 0
						else
							B738DR_lights_sw[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 1 then
						fmc_data_idx = 0
						break
					end
					-- B738DR_fms_test6 = vvvv
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		-- Covers
		if error_loading == 0 then
			error_loading = 45
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				if ii == 11 then
					for vvv = 1, 11 do
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_cover[vvv-1] = 0
						else
							B738DR_cover[vvv-1] = num_tmp
						end
					end
					error_loading = 0
				end
			end
		end
		if error_loading == 0 then
			error_loading = 46
			fms_line = file_fmcdata:read()
			if fms_line then
				ii = split_buffer(fms_line)
				if ii == 11 then
					for vvv = 1, 11 do
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_cover_tgt[vvv-1] = 0
						else
							B738DR_cover_tgt[vvv-1] = num_tmp
						end
					end
					error_loading = 0
				end
			end
		end
		
		if error_loading == 0 then
			-- Fire
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_fire[vvvv-1] = 0
						else
							B738DR_fire[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 1 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		
		if error_loading == 0 then
			-- Fire SW
			fmc_data_idx = 0
			fms_line = file_fmcdata:read()
			while fms_line do
				ii = split_buffer(fms_line)
				data_ok = 0
				if ii == 10 then
					data_ok = 1
				end
				if data_ok ~= 0 then
					for vvv = 1, ii do
						vvvv = fmc_data_idx * 10 + vvv
						num_tmp = tonumber(buf_word[vvv])
						if num_tmp == nil then
							B738DR_fire_sw[vvvv-1] = 0
						else
							B738DR_fire_sw[vvvv-1] = num_tmp
						end
					end
					fmc_data_idx = fmc_data_idx + 1
					if fmc_data_idx > 1 then
						fmc_data_idx = 0
						break
					end
				end
				fms_line = file_fmcdata:read()
			end
		end
		

		
		
		file_fmcdata:close()
		
		 -- B738DR_fms_test6 = fmc_data_num
		 -- B738DR_fms_test7 = fmc_data_legs_num
		
		
	else
		error_loading = 47
	end
	
	else
		error_loading = 48
	end
	
	return error_loading
end

function dump_leg_fmc(in_legs_num)
	local vvv = 0
	local fms_line = ""
	raw_table('legs_data_type')
	local legs_data_type = {1,0,0,0,0,0,0,0,1,0,
							0,0,0,0,0,1,0,0,0,0,
							0,1,0,0,0,0,1,1,1,1,
							1,0,1,1,1,0,0,1,1,0,
							0,0,1,0,0,0,0,1,0,0,
							0,0,0,0}
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if in_legs_num > 0 then
			for vvv = 1, in_legs_num + 1 do
				fms_line = ""
				for www = 1, MAX_LEGS_DATA do
					if legs_data_type[www] == 0 then
						fms_line = fms_line .. tostring(fmc_data[vvv][www]) .. ","
					else
						fms_line = fms_line .. fmc_data[vvv][www] .. ","
					end
				end
				fms_line = fms_line .. "\n"
				file_navdata2:write(fms_line)
				-- fms_line = fmc_data[vvv][1] .. "," .. fmc_data[vvv][16] .. "," .. fmc_data[vvv][31] .. ","
				-- fms_line = fms_line .. tostring(fmc_data[vvv][7]) .. "," .. tostring(fmc_data[vvv][8]) .. ","
				-- fms_line = fms_line .. tostring(fmc_data[vvv][5]) .. "," .. tostring(fmc_data[vvv][21]) .. "\n"
				-- file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function add_empty_record(idx_empty)
	-- create EMPTY WPT
	fmc_data[idx_empty] = {}
	fmc_data[idx_empty][1] = ""
	fmc_data[idx_empty][2] = 0		-- brg
	fmc_data[idx_empty][3] = 0		-- distance
	fmc_data[idx_empty][4] = 0		-- speed
	fmc_data[idx_empty][5] = 0		-- altitude
	fmc_data[idx_empty][6] = 0	-- altitude type
	fmc_data[idx_empty][7] = 0		-- latitude
	fmc_data[idx_empty][8] = 0		-- longitude
	fmc_data[idx_empty][9] = ""			-- via id
	fmc_data[idx_empty][10] = 0		-- calc speed
	fmc_data[idx_empty][11] = 0		-- calc altitude
	fmc_data[idx_empty][12] = 0		-- calc altitude vnav pth
	fmc_data[idx_empty][13] = 0
	fmc_data[idx_empty][14] = 0		-- rest alt
	fmc_data[idx_empty][15] = 0		-- last fuel
	fmc_data[idx_empty][16] = ""
	fmc_data[idx_empty][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
	fmc_data[idx_empty][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
	fmc_data[idx_empty][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
	fmc_data[idx_empty][20] = 0
	fmc_data[idx_empty][21] = -1
	fmc_data[idx_empty][22] = ""
	fmc_data[idx_empty][23] = 0
	fmc_data[idx_empty][24] = 0
	fmc_data[idx_empty][25] = 0
	fmc_data[idx_empty][26] = 0
	fmc_data[idx_empty][27] = ""
	fmc_data[idx_empty][28] = ""
	fmc_data[idx_empty][29] = ""
	fmc_data[idx_empty][30] = ""
	fmc_data[idx_empty][31] = ""
	fmc_data[idx_empty][32] = 0
	fmc_data[idx_empty][33] = ""
	fmc_data[idx_empty][34] = ""
	fmc_data[idx_empty][35] = ""
	fmc_data[idx_empty][36] = 0
	fmc_data[idx_empty][37] = 0
	fmc_data[idx_empty][38] = ""
	fmc_data[idx_empty][39] = ""
	fmc_data[idx_empty][40] = 0
	fmc_data[idx_empty][41] = 0
	fmc_data[idx_empty][42] = 0
	fmc_data[idx_empty][43] = ""
	fmc_data[idx_empty][44] = 0
	fmc_data[idx_empty][45] = 0
	fmc_data[idx_empty][46] = 0
	fmc_data[idx_empty][47] = 0
	fmc_data[idx_empty][48] = ""
	fmc_data[idx_empty][49] = 0
	fmc_data[idx_empty][50] = 0
	fmc_data[idx_empty][51] = 0
	fmc_data[idx_empty][52] = 0
	fmc_data[idx_empty][53] = 0
	fmc_data[idx_empty][54] = 0
end



-- 5, 12, 14, 19, 22, 24
function dec_name_cifs(dec_inp, dec_path, dec_navid, dec_brg, dec_dist, dec_alt)
	
	local dec_input = string.gsub(dec_inp, "%s+", "")
	local dec_output = dec_inp
  local jj = 0
  local temp_str = ""
	
	if dec_input == "" then
		if dec_path == "VM" or dec_path == "FM" then		-- VECTORS
			-- VECTORS
			dec_output = "(VECTOR)"	-- id WPT
		elseif string.sub(dec_path, 2, 2) == "A" then
			-- (1000)
			jj = tonumber(dec_alt)
			if jj == nil then
				dec_output = "(----)"	-- id WPT
			else
				dec_output = "(" .. tostring(jj) .. ")"
			end
		elseif dec_path == "PI" then
			-- Proc Turn
			dec_output = dec_inp	-- id WPT
		elseif string.sub(dec_path, 2, 2) == "I" then
			-- (INTC)
			dec_output = "(INTC)"	-- id WPT
		elseif dec_path == "CF" then
			-- (INTC)
			dec_output = "(INTC)"	-- id WPT
		elseif string.sub(dec_path, 2, 2) == "R" then
			--(ABC180)
			jj = tonumber(dec_brg)
			temp_str = ""
			if jj ~= nil then
				jj = jj / 10
				temp_str = string.format("%03d", jj)
			else
				temp_str = "000"
			end
			-- dec_output = "(" .. dec_navid .. temp_str .. ")"
			dec_output = dec_navid .. temp_str
		elseif string.sub(dec_path, 2, 2) == "D" then
			--(ABC-10)
			jj = tonumber(dec_dist)
			temp_str = ""
			if jj ~= nil then
				jj = jj / 10
				temp_str = string.format("%02d", jj)
			else
				temp_str = "00"
			end
			-- dec_output = "(" .. dec_navid .. "-" .. temp_str .. ")"
			dec_output = dec_navid .. "-" .. temp_str
		else
			dec_output = "---"	-- id WPT
		end
	
	else
		if dec_path == "VM" or dec_path == "FM" then		-- VECTORS
			-- VECTORS -> VM/FM
			dec_output = "(VECTOR)"	-- id WPT
		elseif dec_path == "HM" or dec_path == "HA" or dec_path == "HF" then		-- HOLD
			-- HOLD -> HM/HF/HA
			dec_output = dec_inp	-- id WPT
		elseif string.sub(dec_path, 2, 2) == "A" then
			-- (1000) -> FA
			jj = tonumber(dec_alt)
			if jj == nil then
				dec_output = "(----)"	-- id WPT
			else
				dec_output = "(" .. tostring(jj) .. ")"
			end
		elseif string.sub(dec_path, 2, 2) == "D" then
			--(ABC-10) -> FD
			jj = tonumber(dec_dist)
			temp_str = ""
			if jj ~= nil then
				jj = jj / 10
				temp_str = string.format("%02d", jj)
			else
				temp_str = "00"
			end
			-- dec_output = "(" .. dec_navid .. "-" .. temp_str .. ")"
			dec_output = dec_navid .. "-" .. temp_str
		elseif dec_path == "FC" then
			--(ABC-10) -> FC
			jj = tonumber(dec_dist)
			temp_str = ""
			if jj ~= nil then
				jj = jj / 10
				temp_str = string.format("%02d", jj)
			else
				temp_str = "00"
			end
			-- dec_output = "(" .. dec_inp .. "-" .. temp_str .. ")"
			dec_output = dec_inp .. "-" .. temp_str
		elseif dec_path == "PI" then
			-- Proc Turn
			dec_output = dec_inp	-- id WPT
		elseif string.sub(dec_path, 2, 2) == "I" then
			-- (INTC) -> PI CI/VI
			dec_output = "(INTC)"	-- id WPT
		else
			dec_output = dec_inp	-- id WPT
		end
	end
	
	return dec_output

end


function read_ref_data(ddd_ref_icao, in_rte)
	
	local fms_line = ""
	raw_table('fms_word')
	local fms_word = {}
	local ii = 0
	local jj = 0
	local kk = 0
	local token = ""
	local old_sid = ""
	local temp_sid = ""
	local old_sid_tns = ""
	local old_star = ""
	local old_star_tns = ""
	local old_app = ""
	local old_app_tns = ""
	local temp_str = ""
	local temp_num = 0
	local hold_iaf_enable = 0
	
	local nd_x = 0
	local idx_rec = 0
	
	ref_data_icao = ddd_ref_icao
	
	ref_data_rwy = {}
	ref_rwy_num = 0
	ref_data_sid = {}
	sid_num = 0
	ref_data_sid_tns = {}
	sid_tns_num = 0
	ref_data_star = {}
	star_num = 0
	ref_data_star_tns = {}
	ref_data_star_tns_n = 0
	ref_data_app = {}
	ref_data_app_n = 0
	ref_data_app_tns = {}
	ref_data_app_tns_n = 0
	ref_ed_app = {}
	ref_ed_app_n = 0
	
	rte_sid = {}
	rte_sid_num = 0
	
	local compare = 0
	local fd_compare = false
	
	fms_line = file_navdata:read()
	while fms_line do
		
		-- split DATA
		fms_word = {}
		ii = 0
		for token in string.gmatch(fms_line, "[^,]+") do	--(fms_line, "([^,]+),%s*") do
			ii = ii + 1
			fms_word[ii] = token
		end
		
		-- read RUNWAYs
		if string.sub(fms_line, 1, 4) == "RWY:" then
			ref_rwy_num = ref_rwy_num + 1
			ref_data_rwy[ref_rwy_num] = {true, true, true, true, true, true}
			ref_data_rwy[ref_rwy_num][1] = string.sub(fms_line, 7, 9)		-- RWY
			ref_data_rwy[ref_rwy_num][2] = fms_word[4]		-- altitude
			ref_data_rwy[ref_rwy_num][3] = 0	-- used rwy
			ref_data_rwy[ref_rwy_num][4] = 0	-- lat
			ref_data_rwy[ref_rwy_num][5] = 0	-- lon
			ref_data_rwy[ref_rwy_num][6] = 0	-- course
		end
		
		-- read SIDs and TRANSs
		if string.sub(fms_word[1], 1, 4) == "SID:" then
			idx_rec = tonumber(string.sub(fms_word[1], 5, -1))
			if idx_rec == nil then
				idx_rec = 0
			end
			-- read SIDs
			if string.sub(fms_word[4], 1, 2) == "RW" or fms_word[4] == "ALL" then
				temp_sid = fms_word[3] .. fms_word[4]
				if old_sid ~= temp_sid then 
					sid_num = sid_num + 1
					ref_data_sid[sid_num] = {true, true}
					ref_data_sid[sid_num][1] = fms_word[3]		-- id SID
					ref_data_sid[sid_num][2] = fms_word[4]		-- id runway
					old_sid = fms_word[3] .. fms_word[4]
				end
			else
				-- read TRANSs
				fms_word[4] = string.gsub(fms_word[4], "%s+", "")
				if fms_word[4] ~= "" then
					temp_sid = fms_word[3] .. fms_word[4]
					if old_sid_tns ~= temp_sid then
						sid_tns_num = sid_tns_num + 1
						ref_data_sid_tns[sid_tns_num] = {true, true}
						ref_data_sid_tns[sid_tns_num][1] = fms_word[3]		-- id SID
						ref_data_sid_tns[sid_tns_num][2] = fms_word[4]		-- id trans
						old_sid_tns = fms_word[3] .. fms_word[4]
					end
				end
			end
			
			-- read Route SID
			fd_compare = false
			if rte_sid_num > 0 then
				if fms_word[12] == "FD" and rte_sid[rte_sid_num][3] ~= fms_word[5] then
					fd_compare = true
				end
			elseif fms_word[12] == "FD" then
				fd_compare = true
			end
			
			rte_sid_num = rte_sid_num + 1
			rte_sid[rte_sid_num] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
			
			-- if fms_word[12] == "FD" then
			if fd_compare == true then
				-- create base wpt
				rte_sid[rte_sid_num][25] = idx_rec		-- Record No
				
				rte_sid[rte_sid_num][1] = fms_word[3]	-- id SID
				rte_sid[rte_sid_num][2] = fms_word[4]	-- id RW / trans
				if fms_word[10] == "L" then
					rte_sid[rte_sid_num][9] = 2	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				elseif fms_word[10] == "R" or fms_word[10] == "E" then
					rte_sid[rte_sid_num][9] = 3	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				else
					rte_sid[rte_sid_num][9] = -1	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				end
				if string.sub(fms_word[12], 1, 1) == "H" then
					-- HOLD
					rte_sid[rte_sid_num][3] = fms_word[5]	-- id WPT
					if fms_word[10] == "L" then
						rte_sid[rte_sid_num][9] = 0		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
					else
						rte_sid[rte_sid_num][9] = 1		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
					end
				end
				
				-- 5, 12, 14, 19, 22, 24
				rte_sid[rte_sid_num][3] = dec_name_cifs(fms_word[5], "IF", fms_word[14], fms_word[19], fms_word[22], fms_word[24])
				rte_sid[rte_sid_num][20] = fms_word[5]	-- navaid_id_cifs
				rte_sid[rte_sid_num][4] = fms_word[28]	-- restrict speed
				rte_sid[rte_sid_num][24] = fms_word[27]	-- restrict speed type
				rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude
				
				
				if fms_word[23] == "B" then
					rte_sid[rte_sid_num][5] = fms_word[25]	-- restrict altitude1
					rte_sid[rte_sid_num][23] = fms_word[24]	-- restrict altitude2
					rte_sid[rte_sid_num][6] = fms_word[23]	-- restrict altitude +,-,B
				elseif fms_word[23] == "G" or fms_word[23] == "H" or fms_word[23] == "X" or fms_word[23] == "Y" or fms_word[23] == "V" then
					rte_sid[rte_sid_num][5] = fms_word[25]	-- restrict altitude2
					rte_sid[rte_sid_num][23] = 0	--fms_word[24]	-- restrict altitude1
					rte_sid[rte_sid_num][6] = " "	-- restrict altitude +,-,B
				elseif fms_word[23] == "I" then
					rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude1
					rte_sid[rte_sid_num][23] = 0	--fms_word[25]	-- restrict altitude2
					rte_sid[rte_sid_num][6] = " "	-- restrict altitude +,-,B
				elseif fms_word[23] == "J" then
					rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude1
					rte_sid[rte_sid_num][23] = 0	--fms_word[25]	-- restrict altitude2
					rte_sid[rte_sid_num][6] = "+"	-- restrict altitude +,-,B
				else
					rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude1
					rte_sid[rte_sid_num][23] = fms_word[25]	-- restrict altitude2
					rte_sid[rte_sid_num][6] = fms_word[23]	-- restrict altitude +,-,B
				end
				
				
				rte_sid[rte_sid_num][7] = fms_word[19]	-- course
				rte_sid[rte_sid_num][8] = fms_word[29]	-- vpa
				rte_sid[rte_sid_num][10] = fms_word[22]	-- distance
				rte_sid[rte_sid_num][11] = fms_word[21]	-- course mag
				rte_sid[rte_sid_num][12] = fms_word[6]	-- id wpt reg code
				rte_sid[rte_sid_num][13] = fms_word[14]	-- rec navaid
				rte_sid[rte_sid_num][14] = fms_word[15]	-- rec navaid reg code
				rte_sid[rte_sid_num][15] = "IF"	--fms_word[12]	-- path termination
				rte_sid[rte_sid_num][18] = fms_word[20]	-- distance frrom rec navaid
				rte_sid[rte_sid_num][19] = fms_word[18]	-- Radius for turn
				rte_sid[rte_sid_num][21] = fms_word[11]	-- RNP
				rte_sid[rte_sid_num][16] = 0	-- fly over wpt 0/1
				rte_sid[rte_sid_num][17] = 0
				rte_sid[rte_sid_num][22] = 0	-- NDB navaid 0/1
				if string.len(fms_word[9]) > 1 then
					if string.sub(fms_word[9], 2, 2) == "Y" then
						rte_sid[rte_sid_num][16] = 1	-- fly over wpt 0/1
					end
					if string.sub(fms_word[9], 1, 1) == "N" then
						rte_sid[rte_sid_num][22] = 1	-- NDB navaid 0/1
					elseif string.sub(fms_word[9], 1, 1) == "V" then
						rte_sid[rte_sid_num][22] = 2	-- NDB navaid 0/1
					end
					hold_iaf_enable = 1
					if fms_word[12] == "IF" and fms_word[3] == rte_sid[rte_sid_num][1] then
						hold_iaf_enable = 0
					end
				end
				
				rte_sid_num = rte_sid_num + 1
				rte_sid[rte_sid_num] = {}
				
			end
			
			rte_sid[rte_sid_num][25] = idx_rec		-- Record No
			
			rte_sid[rte_sid_num][1] = fms_word[3]	-- id SID
			rte_sid[rte_sid_num][2] = fms_word[4]	-- id RW / trans
			if fms_word[10] == "L" then
				rte_sid[rte_sid_num][9] = 2	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			elseif fms_word[10] == "R" or fms_word[10] == "E" then
				rte_sid[rte_sid_num][9] = 3	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			else
				rte_sid[rte_sid_num][9] = -1	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			end
			if string.sub(fms_word[12], 1, 1) == "H" then
				-- HOLD
				rte_sid[rte_sid_num][3] = fms_word[5]	-- id WPT
				if fms_word[10] == "L" then
					rte_sid[rte_sid_num][9] = 0		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
				else
					rte_sid[rte_sid_num][9] = 1		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
				end
			end
			
			-- 5, 12, 14, 19, 22, 24
			rte_sid[rte_sid_num][3] = dec_name_cifs(fms_word[5], fms_word[12], fms_word[14], fms_word[19], fms_word[22], fms_word[24])
			rte_sid[rte_sid_num][20] = fms_word[5]	-- navaid_id_cifs
			rte_sid[rte_sid_num][4] = fms_word[28]	-- restrict speed
			rte_sid[rte_sid_num][24] = fms_word[27]	-- restrict speed type
			rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude
			
			
			if fms_word[23] == "B" then
				rte_sid[rte_sid_num][5] = fms_word[25]	-- restrict altitude1
				rte_sid[rte_sid_num][23] = fms_word[24]	-- restrict altitude2
				rte_sid[rte_sid_num][6] = fms_word[23]	-- restrict altitude +,-,B
			elseif fms_word[23] == "G" or fms_word[23] == "H" or fms_word[23] == "X" or fms_word[23] == "Y" or fms_word[23] == "V" then
				rte_sid[rte_sid_num][5] = fms_word[25]	-- restrict altitude2
				rte_sid[rte_sid_num][23] = 0	--fms_word[24]	-- restrict altitude1
				rte_sid[rte_sid_num][6] = " "	-- restrict altitude +,-,B
			elseif fms_word[23] == "I" then
				rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude1
				rte_sid[rte_sid_num][23] = 0	--fms_word[25]	-- restrict altitude2
				rte_sid[rte_sid_num][6] = " "	-- restrict altitude +,-,B
			elseif fms_word[23] == "J" then
				rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude1
				rte_sid[rte_sid_num][23] = 0	--fms_word[25]	-- restrict altitude2
				rte_sid[rte_sid_num][6] = "+"	-- restrict altitude +,-,B
			else
				rte_sid[rte_sid_num][5] = fms_word[24]	-- restrict altitude1
				rte_sid[rte_sid_num][23] = fms_word[25]	-- restrict altitude2
				rte_sid[rte_sid_num][6] = fms_word[23]	-- restrict altitude +,-,B
			end
			
			
			rte_sid[rte_sid_num][7] = fms_word[19]	-- course
			rte_sid[rte_sid_num][8] = fms_word[29]	-- vpa
			rte_sid[rte_sid_num][10] = fms_word[22]	-- distance
			rte_sid[rte_sid_num][11] = fms_word[21]	-- course mag
			rte_sid[rte_sid_num][12] = fms_word[6]	-- id wpt reg code
			rte_sid[rte_sid_num][13] = fms_word[14]	-- rec navaid
			rte_sid[rte_sid_num][14] = fms_word[15]	-- rec navaid reg code
			rte_sid[rte_sid_num][15] = fms_word[12]	-- path termination
			rte_sid[rte_sid_num][18] = fms_word[20]	-- distance frrom rec navaid
			rte_sid[rte_sid_num][19] = fms_word[18]	-- Radius for turn
			rte_sid[rte_sid_num][21] = fms_word[11]	-- RNP
			if fms_word[12] == "CA" or fms_word[12] == "VA" or fms_word[12] == "FA" or fms_word[12] == "FD" 
			or fms_word[12] == "VM" then	--or fms_word[12] == "HM" or fms_word[12] == "HF" or fms_word[12] == "HA" then
				rte_sid[rte_sid_num][16] = 1	-- fly over wpt 0/1
			else
				rte_sid[rte_sid_num][16] = 0	-- fly over wpt 0/1
			end
			rte_sid[rte_sid_num][17] = 0
			rte_sid[rte_sid_num][22] = 0	-- NDB navaid 0/1
			if string.len(fms_word[9]) > 1 then
				if string.sub(fms_word[9], 2, 2) == "Y" then
					rte_sid[rte_sid_num][16] = 1	-- fly over wpt 0/1
				end
				if string.sub(fms_word[9], 1, 1) == "N" then
					rte_sid[rte_sid_num][22] = 1	-- NDB navaid 0/1
				elseif string.sub(fms_word[9], 1, 1) == "V" then
					rte_sid[rte_sid_num][22] = 2	-- NDB navaid 0/1
				end
				hold_iaf_enable = 1
				if fms_word[12] == "IF" and fms_word[3] == rte_sid[rte_sid_num][1] then
					hold_iaf_enable = 0
				end
			end
		end
		
		-- read STARs
		if string.sub(fms_word[1], 1, 5) == "STAR:" then
			if string.sub(fms_word[4], 1, 2) == "RW" or fms_word[4] == "ALL" then
				temp_sid = fms_word[3] .. fms_word[4]
				if old_star ~= temp_sid then
					star_num = star_num + 1
					ref_data_star[star_num] = {true, true}
					ref_data_star[star_num][1] = fms_word[3]		-- id STAR
					ref_data_star[star_num][2] = fms_word[4]		-- id runway
					old_star = fms_word[3] .. fms_word[4]
				end
			else
				temp_sid = fms_word[3] .. fms_word[4]
				if old_star_tns ~= temp_sid then
					ref_data_star_tns_n = ref_data_star_tns_n + 1
					ref_data_star_tns[ref_data_star_tns_n] = {true, true}
					ref_data_star_tns[ref_data_star_tns_n][1] = fms_word[3]		-- id STAR
					ref_data_star_tns[ref_data_star_tns_n][2] = fms_word[4]		-- id trans
					old_star_tns = fms_word[3] .. fms_word[4]
				end
			end
		end
		
		-- read APPROACHs
		if string.sub(fms_word[1], 1, 6) == "APPCH:" then
			if fms_word[2] == "A" then
				temp_sid = fms_word[3] .. fms_word[4]
				if old_app_tns ~= temp_sid then
					ref_data_app_tns_n = ref_data_app_tns_n + 1
					ref_data_app_tns[ref_data_app_tns_n] = {true, true, true}
					ref_data_app_tns[ref_data_app_tns_n][1] = fms_word[3]		-- id APP
					ref_data_app_tns[ref_data_app_tns_n][2] = fms_word[4]		-- id trans
					ref_data_app_tns[ref_data_app_tns_n][3] = fms_word[5]		-- id E/D
					old_app_tns = fms_word[3] .. fms_word[4]
				end
			else
				if old_app ~= fms_word[3] then
					ref_data_app_n = ref_data_app_n + 1
					ref_data_app[ref_data_app_n] = fms_word[3]		-- id APP
					old_app = fms_word[3]
				end
				if fms_word[9] == "E  F" then	-- E/D
					ref_ed_app_n = ref_ed_app_n + 1
					ref_ed_app[ref_ed_app_n] = {true, true}
					ref_ed_app[ref_ed_app_n][1] = fms_word[3]	-- id APP
					ref_ed_app[ref_ed_app_n][2] = fms_word[5]	-- id E/D
				end
			end
		end
		
		fms_line = file_navdata:read()
	end
	
	--*******************************************************************************************
	-- create idx list of runways
	idx_rec = 0
	local idx_rwy_tmp_num = 0
	raw_table('idx_rwy_tmp')
	local idx_rwy_tmp = {}
	if rnw_data_num > 0 and string.len(ddd_ref_icao) > 0 then
		nd_x = string.byte(string.sub(ddd_ref_icao, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for jj = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][jj]
					if rnw_data[idx_rec][1] == ddd_ref_icao then
						idx_rwy_tmp_num = idx_rwy_tmp_num + 1
						idx_rwy_tmp[idx_rwy_tmp_num] = idx_rec
					end
				end
			end
		end
	end
	
	-- find unknown runways
	idx_rec = 0
	if ref_rwy_num > 0 and idx_rwy_tmp_num > 0 then
		for jj = 1, idx_rwy_tmp_num do
			idx_rec = idx_rwy_tmp[jj]
			rnw_data[idx_rec][9] = 0
					
			if string.sub(rnw_data[idx_rec][2], -1, -1) == "L" or string.sub(rnw_data[idx_rec][2], -1, -1) == "R"
			or string.sub(rnw_data[idx_rec][2], -1, -1) == "C" then
				temp_num = tonumber(string.sub(rnw_data[idx_rec][2], 1, -2))
				if temp_num == nil then
					temp_str = ""
				else
					temp_str = string.format("%02d", temp_num) .. string.sub(rnw_data[idx_rec][2], -1, -1)
				end
			else
				temp_num = tonumber(rnw_data[idx_rec][2])
				if temp_num == nil then
					temp_str = ""
				else
					temp_str = string.format("%02d", temp_num) .. " "
				end
			end
			
			for ii = 1, ref_rwy_num do
				if ref_data_rwy[ii][1] == temp_str then
					rnw_data[idx_rec][9] = 1
					ref_data_rwy[ii][3] = 1
					ref_data_rwy[ii][4] = rnw_data[idx_rec][3]
					ref_data_rwy[ii][5] = rnw_data[idx_rec][4]
					ref_data_rwy[ii][6] = rnw_data[idx_rec][8]
				end
			end
		end
		
		-- compare runways and change to CIFP data
		for ii = 1, ref_rwy_num do
			if ref_data_rwy[ii][3] == 0 then
				for jj = 1, idx_rwy_tmp_num do
					idx_rec = idx_rwy_tmp[jj]
					if rnw_data[idx_rec][9] == 0 then
						compare = 0
						if string.sub(rnw_data[idx_rec][2], -1, -1) == "L" or string.sub(rnw_data[idx_rec][2], -1, -1) == "R"
						or string.sub(rnw_data[idx_rec][2], -1, -1) == "C" then
							temp_num = tonumber(string.sub(rnw_data[idx_rec][2], 1, -2))
							if temp_num == nil then
								temp_str = ""
							else
								temp_str = string.format("%02d", temp_num) .. string.sub(rnw_data[idx_rec][2], -1, -1)
							end
						else
							temp_num = tonumber(rnw_data[idx_rec][2])
							if temp_num == nil then
								temp_str = ""
							else
								temp_str = string.format("%02d", temp_num) .. " "
							end
						end
						
						if string.sub(ref_data_rwy[ii][1], -1, -1) == string.sub(temp_str, -1, -1) then
							compare = 1
						end
						
						if compare == 1 then
							if string.sub(ref_data_rwy[ii][1], -1, -1) == " " then
								temp_num =  tonumber(ref_data_rwy[ii][1])
							else
								temp_num =  tonumber(string.sub(ref_data_rwy[ii][1], 1, -2))
							end
							if temp_num ~= nil then
								kk = temp_num * 10
								if string.sub(temp_str, -1, -1) == " " then
									temp_num =  tonumber(temp_str)
								else
									temp_num =  tonumber(string.sub(temp_str, 1, -2))
								end
								if temp_num ~= nil then
									temp_num = temp_num * 10
									kk = ((kk - temp_num) + 360) % 360
									if kk > 180 then
										kk = kk - 360
									end
									if kk < 0 then
										kk = -kk
									end
									if kk < 30 then
										if string.sub(ref_data_rwy[ii][1], -1, -1) == " " then
											temp_num = tonumber(ref_data_rwy[ii][1])
											token = string.format("%02d", temp_num)
										else
											temp_num = tonumber(string.sub(ref_data_rwy[ii][1], 1, -2))
											token = string.format("%02d", temp_num) .. string.sub(ref_data_rwy[ii][1], -1, -1)
										end
										rnw_data[idx_rec][2] = token		-- runway
										rnw_data[idx_rec][9] = 1
										
										ref_data_rwy[ii][4] = rnw_data[idx_rec][3]
										ref_data_rwy[ii][5] = rnw_data[idx_rec][4]
										ref_data_rwy[ii][6] = rnw_data[idx_rec][8]
										ref_data_rwy[ii][3] = 1
									end
								end
							end
						end
					end
				end
				if ref_data_rwy[ii][3] == 0 then
					compare = 0
					if string.sub(ref_data_rwy[ii][1], -1, -1) == " " then
						temp_num =  tonumber(ref_data_rwy[ii][1])
					else
						temp_num =  tonumber(string.sub(ref_data_rwy[ii][1], 1, -2))
					end
					if temp_num ~= nil then
						kk = temp_num
						for jj = 1, idx_rwy_tmp_num do
							idx_rec = idx_rwy_tmp[jj]
							if rnw_data[idx_rec][9] == 0 then
								if string.sub(rnw_data[idx_rec][2], -1, -1) == "L" or string.sub(rnw_data[idx_rec][2], -1, -1) == "R"
								or string.sub(rnw_data[idx_rec][2], -1, -1) == "C" then
									temp_num = tonumber(string.sub(rnw_data[idx_rec][2], 1, -2))
									if temp_num == nil then
										temp_str = ""
									else
										temp_str = string.format("%02d", temp_num) .. string.sub(rnw_data[idx_rec][2], -1, -1)
									end
								else
									temp_num = tonumber(rnw_data[idx_rec][2])
									if temp_num == nil then
										temp_str = ""
									else
										temp_str = string.format("%02d", temp_num) .. " "
									end
								end
								if string.sub(temp_str, -1, -1) == " " then
									temp_num =  tonumber(temp_str)
								else
									temp_num =  tonumber(string.sub(temp_str, 1, -2))
								end
								if temp_num ~= nil then
									if temp_num == kk then
										compare = compare + 1
										nd_x = idx_rec
									end
								end
							end
						end
						if compare == 1 then
							if string.sub(ref_data_rwy[ii][1], -1, -1) == " " then
								temp_num = tonumber(ref_data_rwy[ii][1])
								token = string.format("%02d", temp_num)
							else
								temp_num = tonumber(string.sub(ref_data_rwy[ii][1], 1, -2))
								token = string.format("%02d", temp_num) .. string.sub(ref_data_rwy[ii][1], -1, -1)
							end
							
							idx_rec = nd_x
							rnw_data[idx_rec][2] = token		-- runway
							rnw_data[idx_rec][9] = 1
							
							ref_data_rwy[ii][4] = rnw_data[idx_rec][3]
							ref_data_rwy[ii][5] = rnw_data[idx_rec][4]
							ref_data_rwy[ii][6] = rnw_data[idx_rec][8]
							ref_data_rwy[ii][3] = 1
						end
					end
				end
			end
		end
	end
	
	copy_from_ref_data(in_rte)
	copy_from_rte_sid(in_rte)
end

function copy_from_ref_data(in_rte)
	if in_rte == 0 then
		ref_data_rwy_1 = {}
		ref_rwy_num_1 = 0
		ref_data_sid_1 = {}
		sid_num_1 = 0
		ref_data_sid_tns_1 = {}
		sid_tns_num_1 = 0
		ref_data_star_1 = {}
		star_num_1 = 0
		ref_data_star_tns_1 = {}
		ref_data_star_tns_n_1 = 0
		ref_data_app_1 = {}
		ref_data_app_n_1 = 0
		ref_data_app_tns_1 = {}
		ref_data_app_tns_n_1 = 0
		ref_ed_app_1 = {}
		ref_ed_app_n_1 = 0
		
		if ref_rwy_num > 0 then
			ref_rwy_num_1 = ref_rwy_num
			for ii = 1, ref_rwy_num do
				ref_data_rwy_1[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					ref_data_rwy_1[ii][jj] = ref_data_rwy[ii][jj]
				end
			end
		end
		
		if sid_tns_num > 0 then
			sid_tns_num_1 = sid_tns_num
			for ii = 1, sid_tns_num do
				ref_data_sid_tns_1[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid_tns_1[ii][jj] = ref_data_sid_tns[ii][jj]
				end
			end
		end
		
		if sid_num > 0 then
			sid_num_1 = sid_num
			for ii = 1, sid_num do
				ref_data_sid_1[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid_1[ii][jj] = ref_data_sid[ii][jj]
				end
			end
		end
		
		if star_num > 0 then
			star_num_1 = star_num
			for ii = 1, star_num do
				ref_data_star_1[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star_1[ii][jj] = ref_data_star[ii][jj]
				end
			end
		end
		
		if ref_data_star_tns_n > 0 then
			ref_data_star_tns_n_1 = ref_data_star_tns_n
			for ii = 1, ref_data_star_tns_n do
				ref_data_star_tns_1[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star_tns_1[ii][jj] = ref_data_star_tns[ii][jj]
				end
			end
		end
		
		if ref_data_app_n > 0 then
			ref_data_app_n_1 = ref_data_app_n
			for ii = 1, ref_data_app_n do
				ref_data_app_1[ii] = ref_data_app[ii]
			end
		end
		
		if ref_data_app_tns_n > 0 then
			ref_data_app_tns_n_1 = ref_data_app_tns_n
			for ii = 1, ref_data_app_tns_n do
				ref_data_app_tns_1[ii] = {true, true, true}
				for jj = 1, 3 do
					ref_data_app_tns_1[ii][jj] = ref_data_app_tns[ii][jj]
				end
			end
		end
		
	elseif in_rte == 1 then
		ref_data_rwy_2 = {}
		ref_rwy_num_2 = 0
		ref_data_sid_2 = {}
		sid_num_2 = 0
		ref_data_sid_tns_2 = {}
		sid_tns_num_2 = 0
		ref_data_star_2 = {}
		star_num_2 = 0
		ref_data_star_tns_2 = {}
		ref_data_star_tns_n_2 = 0
		ref_data_app_2 = {}
		ref_data_app_n_2 = 0
		ref_data_app_tns_2 = {}
		ref_data_app_tns_n_2 = 0
		ref_ed_app_2 = {}
		ref_ed_app_n_2 = 0
		
		if ref_rwy_num > 0 then
			ref_rwy_num_2 = ref_rwy_num
			for ii = 1, ref_rwy_num do
				ref_data_rwy_2[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					ref_data_rwy_2[ii][jj] = ref_data_rwy[ii][jj]
				end
			end
		end
		
		if sid_tns_num > 0 then
			sid_tns_num_2 = sid_tns_num
			for ii = 1, sid_tns_num do
				ref_data_sid_tns_2[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid_tns_2[ii][jj] = ref_data_sid_tns[ii][jj]
				end
			end
		end
		
		if sid_num > 0 then
			sid_num_2 = sid_num
			for ii = 1, sid_num do
				ref_data_sid_2[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid_2[ii][jj] = ref_data_sid[ii][jj]
				end
			end
		end
		
		if star_num > 0 then
			star_num_2 = star_num
			for ii = 1, star_num do
				ref_data_star_2[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star_2[ii][jj] = ref_data_star[ii][jj]
				end
			end
		end
		
		if ref_data_star_tns_n > 0 then
			ref_data_star_tns_n_2 = ref_data_star_tns_n
			for ii = 1, ref_data_star_tns_n do
				ref_data_star_tns_2[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star_tns_2[ii][jj] = ref_data_star_tns[ii][jj]
				end
			end
		end
		
		if ref_data_app_n > 0 then
			ref_data_app_n_2 = ref_data_app_n
			for ii = 1, ref_data_app_n do
				ref_data_app_2[ii] = ref_data_app[ii]
			end
		end
		
		if ref_data_app_tns_n > 0 then
			ref_data_app_tns_n_2 = ref_data_app_tns_n
			for ii = 1, ref_data_app_tns_n do
				ref_data_app_tns_2[ii] = {true, true, true}
				for jj = 1, 3 do
					ref_data_app_tns_2[ii][jj] = ref_data_app_tns[ii][jj]
				end
			end
		end
	end
end

function copy_to_ref_data(in_rte)
	if in_rte == 0 then
		ref_data_rwy = {}
		ref_rwy_num = 0
		ref_data_sid = {}
		sid_num = 0
		ref_data_sid_tns = {}
		sid_tns_num = 0
		ref_data_star = {}
		star_num = 0
		ref_data_star_tns = {}
		ref_data_star_tns_n = 0
		ref_data_app = {}
		ref_data_app_n = 0
		ref_data_app_tns = {}
		ref_data_app_tns_n = 0
		ref_ed_app = {}
		ref_ed_app_n = 0
		
		if ref_rwy_num_1 > 0 then
			ref_rwy_num = ref_rwy_num_1
			for ii = 1, ref_rwy_num do
				ref_data_rwy[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					ref_data_rwy[ii][jj] = ref_data_rwy_1[ii][jj]
				end
			end
		end
		
		if sid_tns_num_1 > 0 then
			sid_tns_num = sid_tns_num_1
			for ii = 1, sid_tns_num do
				ref_data_sid_tns[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid_tns[ii][jj] = ref_data_sid_tns_1[ii][jj]
				end
			end
		end
		
		if sid_num_1 > 0 then
			sid_num = sid_num_1
			for ii = 1, sid_num do
				ref_data_sid[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid[ii][jj] = ref_data_sid_1[ii][jj]
				end
			end
		end
		
		if star_num_1 > 0 then
			star_num = star_num_1
			for ii = 1, star_num do
				ref_data_star[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star[ii][jj] = ref_data_star_1[ii][jj]
				end
			end
		end
		
		if ref_data_star_tns_n_1 > 0 then
			ref_data_star_tns_n = ref_data_star_tns_n_1
			for ii = 1, ref_data_star_tns_n do
				ref_data_star_tns[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star_tns[ii][jj] = ref_data_star_tns_1[ii][jj]
				end
			end
		end
		
		if ref_data_app_n_1 > 0 then
			ref_data_app_n = ref_data_app_n_1
			for ii = 1, ref_data_app_n do
				ref_data_app[ii] = ref_data_app_1[ii]
			end
		end
		
		if ref_data_app_tns_n_1 > 0 then
			ref_data_app_tns_n = ref_data_app_tns_n_1
			for ii = 1, ref_data_app_tns_n do
				ref_data_app_tns[ii] = {true, true, true}
				for jj = 1, 3 do
					ref_data_app_tns[ii][jj] = ref_data_app_tns_1[ii][jj]
				end
			end
		end
		
	elseif in_rte == 1 then
		ref_data_rwy = {}
		ref_rwy_num = 0
		ref_data_sid = {}
		sid_num = 0
		ref_data_sid_tns = {}
		sid_tns_num = 0
		ref_data_star = {}
		star_num = 0
		ref_data_star_tns = {}
		ref_data_star_tns_n = 0
		ref_data_app = {}
		ref_data_app_n = 0
		ref_data_app_tns = {}
		ref_data_app_tns_n = 0
		ref_ed_app = {}
		ref_ed_app_n = 0
		
		if ref_rwy_num_2 > 0 then
			ref_rwy_num = ref_rwy_num_2
			for ii = 1, ref_rwy_num do
				ref_data_rwy[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					ref_data_rwy[ii][jj] = ref_data_rwy_2[ii][jj]
				end
			end
		end
		
		if sid_tns_num_2 > 0 then
			sid_tns_num = sid_tns_num_2
			for ii = 1, sid_tns_num do
				ref_data_sid_tns[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid_tns[ii][jj] = ref_data_sid_tns_2[ii][jj]
				end
			end
		end
		
		if sid_num_2 > 0 then
			sid_num = sid_num_2
			for ii = 1, sid_num do
				ref_data_sid[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_sid[ii][jj] = ref_data_sid_2[ii][jj]
				end
			end
		end
		
		if star_num_2 > 0 then
			star_num = star_num_2
			for ii = 1, star_num do
				ref_data_star[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star[ii][jj] = ref_data_star_2[ii][jj]
				end
			end
		end
		
		if ref_data_star_tns_n_2 > 0 then
			ref_data_star_tns_n = ref_data_star_tns_n_2
			for ii = 1, ref_data_star_tns_n do
				ref_data_star_tns[ii] = {true, true}
				for jj = 1, 2 do
					ref_data_star_tns[ii][jj] = ref_data_star_tns_2[ii][jj]
				end
			end
		end
		
		if ref_data_app_n_2 > 0 then
			ref_data_app_n = ref_data_app_n_2
			for ii = 1, ref_data_app_n do
				ref_data_app[ii] = ref_data_app_2[ii]
			end
		end
		
		if ref_data_app_tns_n_2 > 0 then
			ref_data_app_tns_n = ref_data_app_tns_n_2
			for ii = 1, ref_data_app_tns_n do
				ref_data_app_tns[ii] = {true, true, true}
				for jj = 1, 3 do
					ref_data_app_tns[ii][jj] = ref_data_app_tns_2[ii][jj]
				end
			end
		end
	end
end

function copy_from_rte_sid(in_rte)
	if in_rte == 0 then
		rte_sid_1 = {}
		rte_sid_num_1 = 0
		
		if rte_sid_num > 0 then
			rte_sid_num_1 = rte_sid_num
			for ii = 1, rte_sid_num do
				rte_sid_1[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_sid_1[ii][jj] = rte_sid[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		rte_sid_2 = {}
		rte_sid_num_2 = 0
		
		if rte_sid_num > 0 then
			rte_sid_num_2 = rte_sid_num
			for ii = 1, rte_sid_num do
				rte_sid_2[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_sid_2[ii][jj] = rte_sid[ii][jj]
				end
			end
		end
	end
end

function copy_to_rte_sid(in_rte)
	if in_rte == 0 then
		rte_sid = {}
		rte_sid_num = 0
		
		if rte_sid_num_1 > 0 then
			rte_sid_num = rte_sid_num_1
			for ii = 1, rte_sid_num do
				rte_sid[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_sid[ii][jj] = rte_sid_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		rte_sid = {}
		rte_sid_num = 0
		
		if rte_sid_num_2 > 0 then
			rte_sid_num = rte_sid_num_2
			for ii = 1, rte_sid_num do
				rte_sid[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_sid[ii][jj] = rte_sid_2[ii][jj]
				end
			end
		end
	end
end

function dump_sid()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if rte_sid_num > 0 then
			for vvv = 1, rte_sid_num  do
				fms_line = rte_sid[vvv][1] .. "," .. rte_sid[vvv][2] .. "," .. rte_sid[vvv][10] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:write("SID:\n")
		if sid_num > 0 then
			for vvv = 1, sid_num  do
				fms_line = ref_data_sid[vvv][1] .. "," .. ref_data_sid[vvv][2] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:write("SID TRANS:\n")
		if sid_tns_num > 0 then
			for vvv = 1, sid_tns_num  do
				fms_line = ref_data_sid_tns[vvv][1] .. "," .. ref_data_sid_tns[vvv][2] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function dump_sid_list()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if sid_list_num > 0 then
			for vvv = 1, sid_list_num  do
				fms_line = sid_list[vvv][1] .. "," .. sid_list[vvv][2] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function dump_rnw_data(rrr_des_icao)
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if rnw_data_num > 0 then
			for vvv = 1, rnw_data_num  do
				if rnw_data[vvv][1] == rrr_des_icao then
					fms_line = rnw_data[vvv][1] .. "," .. rnw_data[vvv][2] .. "/" .. tostring(rnw_data[vvv][9]) .. "\n"
					file_navdata2:write(fms_line)
				end
			end
		end
		file_navdata2:close()
	end
end


function read_des_data(ddd_des_icao, in_rte)
	
	local fms_line = ""
	raw_table('fms_word')
	local fms_word = {}
	local ii = 0
	local jj = 0
	local kk = 0
	local token = ""
	local temp_sid = ""
	local old_star = ""
	local old_star_tns = ""
	local old_app = ""
	local old_app_tns = ""
	local temp_str = ""
	local temp_num = 0
	local compare = 0
	local hold_iaf_enable = 0
	local pi_active = 0
	local pi_crs = 0
	local pi_path = ""
	local nd_x = 0
	local idx_rec = 0
	local fd_compare = false
	
	des_data_icao = ddd_des_icao
	
	des_data_rwy = {}
	des_rwy_num = 0
	data_des_star = {}
	data_des_star_n = 0
	data_des_star_tns = {}
	data_des_star_tns_n = 0
	data_des_app = {}
	data_des_app_n = 0
	data_des_app_tns = {}
	data_des_app_tns_n = 0
	-- don't used
	ed_app_num = 0
	ed_app = {}
	
	rte_star = {}
	rte_star_num = 0
	rte_app = {}
	rte_app_num = 0
	
	fms_line = file_navdata:read()
	while fms_line do
		
		-- split DATA
		fms_word = {}
		ii = 0
		for token in string.gmatch(fms_line, "[^,]+") do 		--%s*") do
			ii = ii + 1
			fms_word[ii] = token
		end
		
		-- read RUNWAYs
		if string.sub(fms_line, 1, 4) == "RWY:" then
			des_rwy_num = des_rwy_num + 1
			des_data_rwy[des_rwy_num] = {true, true, true, true, true, true}
			des_data_rwy[des_rwy_num][1] = string.sub(fms_line, 5, 9)		-- RWY
			des_data_rwy[des_rwy_num][2] = "" --string.sub(fms_line, -26, -18)	-- lat
			des_data_rwy[des_rwy_num][3] = "" --string.sub(fms_line, -16, -7)	-- lon
			des_data_rwy[des_rwy_num][4] = fms_word[6]						-- id ILS
			des_data_rwy[des_rwy_num][5] = fms_word[4]						-- altitude
			des_data_rwy[des_rwy_num][6] = 0 
		end
		
		-- read STARs
		if string.sub(fms_word[1], 1, 5) == "STAR:" then
			idx_rec = tonumber(string.sub(fms_word[1], 6, -1))
			if idx_rec == nil then
				idx_rec = 0
			end
			if string.sub(fms_word[4], 1, 2) == "RW" or fms_word[4] == "ALL" then
				temp_sid = fms_word[3] .. fms_word[4]
				if old_star ~= temp_sid then
					data_des_star_n = data_des_star_n + 1
					data_des_star[data_des_star_n] = {true, true}
					data_des_star[data_des_star_n][1] = fms_word[3]		-- id STAR
					data_des_star[data_des_star_n][2] = fms_word[4]		-- id runway
					old_star = fms_word[3] .. fms_word[4]
				end
			else
				temp_sid = fms_word[3] .. fms_word[4]
				if old_star_tns ~= temp_sid then
					data_des_star_tns_n = data_des_star_tns_n + 1
					data_des_star_tns[data_des_star_tns_n] = {true, true}
					data_des_star_tns[data_des_star_tns_n][1] = fms_word[3]		-- id STAR
					data_des_star_tns[data_des_star_tns_n][2] = fms_word[4]		-- id trans
					old_star_tns = fms_word[3] .. fms_word[4]
				end
			end
			-- read Route STAR
			fd_compare = false
			if rte_star_num > 0 then
				if fms_word[12] == "FD" and rte_star[rte_star_num][3] ~= fms_word[5] then
					fd_compare = true
				end
			elseif fms_word[12] == "FD" then
				fd_compare = true
			end
			
			rte_star_num = rte_star_num + 1
			rte_star[rte_star_num] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
			
			-- if fms_word[12] == "FD" then
			if fd_compare == true then
				
				rte_star[rte_star_num][25] = idx_rec		-- Record No
				
				rte_star[rte_star_num][1] = fms_word[3]	-- id STAR
				rte_star[rte_star_num][2] = fms_word[4]	-- id RW / trans
				
				if fms_word[10] == "L" then
					rte_star[rte_star_num][9] = 2	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				elseif fms_word[10] == "R" or fms_word[10] == "E" then
					rte_star[rte_star_num][9] = 3	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				else
					rte_star[rte_star_num][9] = -1	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				end
				if string.sub(fms_word[12], 1, 1) == "H" then
					-- HOLD
					rte_star[rte_star_num][3] = fms_word[5]	-- id WPT
					if fms_word[10] == "L" then
						rte_star[rte_star_num][9] = 0		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
					else
						rte_star[rte_star_num][9] = 1		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
					end
				end
				
				-- 5, 12, 14, 19, 22, 24
				rte_star[rte_star_num][3] = dec_name_cifs(fms_word[5], "IF", fms_word[14], fms_word[19], fms_word[22], fms_word[24])
				rte_star[rte_star_num][20] = fms_word[5]	-- navaid_id_cifs
				rte_star[rte_star_num][4] = fms_word[28]	-- restrict speed
				rte_star[rte_star_num][24] = fms_word[27]		-- restrict speed type
				rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude
				
				if fms_word[23] == "B" then
					rte_star[rte_star_num][5] = fms_word[25]	-- restrict altitude1
					rte_star[rte_star_num][23] = fms_word[24]	-- restrict altitude2
					rte_star[rte_star_num][6] = fms_word[23]	-- restrict altitude +,-,B
				elseif fms_word[23] == "G" or fms_word[23] == "H" or fms_word[23] == "X" or fms_word[23] == "Y" or fms_word[23] == "V" then
					rte_star[rte_star_num][5] = fms_word[25]	-- restrict altitude2
					rte_star[rte_star_num][23] = 0	--fms_word[24]	-- restrict altitude1
					rte_star[rte_star_num][6] = " "	-- restrict altitude +,-,B
				elseif fms_word[23] == "I" then
					rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude1
					rte_star[rte_star_num][23] = 0	--fms_word[25]	-- restrict altitude2
					rte_star[rte_star_num][6] = " "	-- restrict altitude +,-,B
				elseif fms_word[23] == "J" then
					rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude1
					rte_star[rte_star_num][23] = 0	--fms_word[25]	-- restrict altitude2
					rte_star[rte_star_num][6] = "+"	-- restrict altitude +,-,B
				else
					rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude1
					rte_star[rte_star_num][23] = fms_word[25]	-- restrict altitude2
					rte_star[rte_star_num][6] = fms_word[23]	-- restrict altitude +,-,B
				end
				
				rte_star[rte_star_num][7] = fms_word[19]	-- course
				rte_star[rte_star_num][8] = fms_word[29]	-- vpa
				rte_star[rte_star_num][10] = fms_word[22]	-- distance
				rte_star[rte_star_num][11] = fms_word[21]	-- course mag
				rte_star[rte_star_num][12] = fms_word[6]	-- id wpt reg code
				rte_star[rte_star_num][13] = fms_word[14]	-- rec navaid
				rte_star[rte_star_num][14] = fms_word[15]	-- rec navaid reg code
				rte_star[rte_star_num][15] = "IF"	--fms_word[12]	-- path termination
				rte_star[rte_star_num][18] = fms_word[20]	-- distance frrom rec navaid
				rte_star[rte_star_num][19] = fms_word[18]	-- Radius for turn
				rte_star[rte_star_num][21] = fms_word[11]	-- RNP
				rte_star[rte_star_num][16] = 0	-- fly over wpt 0/1
				rte_star[rte_star_num][17] = 0
				rte_star[rte_star_num][22] = 0	-- NDB navaid 0/1
				if string.len(fms_word[9]) > 1 then
					if string.sub(fms_word[9], 2, 2) == "Y" then
						rte_star[rte_star_num][16] = 1	-- fly over wpt 0/1
					end
					if string.sub(fms_word[9], 1, 1) == "N" then
						rte_star[rte_star_num][22] = 1	-- NDB navaid 0/1
					elseif string.sub(fms_word[9], 1, 1) == "V" then
						rte_star[rte_star_num][22] = 2	-- NDB navaid 0/1
					end
					hold_iaf_enable = 1
					if fms_word[12] == "IF" and fms_word[3] == rte_star[rte_star_num][1] then
						hold_iaf_enable = 0
					end
				end
				
				idx_rec = idx_rec + 1
				
				rte_star_num = rte_star_num + 1
				rte_star[rte_star_num] = {}
				
				
				
			end
			
			rte_star[rte_star_num][25] = idx_rec		-- Record No
			
			rte_star[rte_star_num][1] = fms_word[3]	-- id STAR
			rte_star[rte_star_num][2] = fms_word[4]	-- id RW / trans
			
			if fms_word[10] == "L" then
				rte_star[rte_star_num][9] = 2	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			elseif fms_word[10] == "R" or fms_word[10] == "E" then
				rte_star[rte_star_num][9] = 3	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			else
				rte_star[rte_star_num][9] = -1	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			end
			if string.sub(fms_word[12], 1, 1) == "H" then
				-- HOLD
				rte_star[rte_star_num][3] = fms_word[5]	-- id WPT
				if fms_word[10] == "L" then
					rte_star[rte_star_num][9] = 0		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
				else
					rte_star[rte_star_num][9] = 1		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
				end
			end
			
			-- 5, 12, 14, 19, 22, 24
			rte_star[rte_star_num][3] = dec_name_cifs(fms_word[5], fms_word[12], fms_word[14], fms_word[19], fms_word[22], fms_word[24])
			rte_star[rte_star_num][20] = fms_word[5]	-- navaid_id_cifs
			rte_star[rte_star_num][4] = fms_word[28]	-- restrict speed
			rte_star[rte_star_num][24] = fms_word[27]		-- restrict speed type
			rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude
			
			if fms_word[23] == "B" then
				rte_star[rte_star_num][5] = fms_word[25]	-- restrict altitude1
				rte_star[rte_star_num][23] = fms_word[24]	-- restrict altitude2
				rte_star[rte_star_num][6] = fms_word[23]	-- restrict altitude +,-,B
			elseif fms_word[23] == "G" or fms_word[23] == "H" or fms_word[23] == "X" or fms_word[23] == "Y" or fms_word[23] == "V" then
				rte_star[rte_star_num][5] = fms_word[25]	-- restrict altitude2
				rte_star[rte_star_num][23] = 0	--fms_word[24]	-- restrict altitude1
				rte_star[rte_star_num][6] = " "	-- restrict altitude +,-,B
			elseif fms_word[23] == "I" then
				rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude1
				rte_star[rte_star_num][23] = 0	--fms_word[25]	-- restrict altitude2
				rte_star[rte_star_num][6] = " "	-- restrict altitude +,-,B
			elseif fms_word[23] == "J" then
				rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude1
				rte_star[rte_star_num][23] = 0	--fms_word[25]	-- restrict altitude2
				rte_star[rte_star_num][6] = "+"	-- restrict altitude +,-,B
			else
				rte_star[rte_star_num][5] = fms_word[24]	-- restrict altitude1
				rte_star[rte_star_num][23] = fms_word[25]	-- restrict altitude2
				rte_star[rte_star_num][6] = fms_word[23]	-- restrict altitude +,-,B
			end
			
			rte_star[rte_star_num][7] = fms_word[19]	-- course
			rte_star[rte_star_num][8] = fms_word[29]	-- vpa
			rte_star[rte_star_num][10] = fms_word[22]	-- distance
			rte_star[rte_star_num][11] = fms_word[21]	-- course mag
			rte_star[rte_star_num][12] = fms_word[6]	-- id wpt reg code
			rte_star[rte_star_num][13] = fms_word[14]	-- rec navaid
			rte_star[rte_star_num][14] = fms_word[15]	-- rec navaid reg code
			rte_star[rte_star_num][15] = fms_word[12]	-- path termination
			rte_star[rte_star_num][18] = fms_word[20]	-- distance frrom rec navaid
			rte_star[rte_star_num][19] = fms_word[18]	-- Radius for turn
			rte_star[rte_star_num][21] = fms_word[11]	-- RNP
			if fms_word[12] == "CA" or fms_word[12] == "VA" or fms_word[12] == "FA" or fms_word[12] == "FD" 
			or fms_word[12] == "VM" then	--or fms_word[12] == "HM" or fms_word[12] == "HF" or fms_word[12] == "HA" then
				rte_star[rte_star_num][16] = 1	-- fly over wpt 0/1
			else
				rte_star[rte_star_num][16] = 0	-- fly over wpt 0/1
			end
			rte_star[rte_star_num][17] = 0
			rte_star[rte_star_num][22] = 0	-- NDB navaid 0/1
			if string.len(fms_word[9]) > 1 then
				if string.sub(fms_word[9], 2, 2) == "Y" then
					rte_star[rte_star_num][16] = 1	-- fly over wpt 0/1
				end
				if string.sub(fms_word[9], 1, 1) == "N" then
					rte_star[rte_star_num][22] = 1	-- NDB navaid 0/1
				elseif string.sub(fms_word[9], 1, 1) == "V" then
					rte_star[rte_star_num][22] = 2	-- NDB navaid 0/1
				end
				hold_iaf_enable = 1
				if fms_word[12] == "IF" and fms_word[3] == rte_star[rte_star_num][1] then
					hold_iaf_enable = 0
				end
			end
		end
		
		-- read APPROACHs
		if string.sub(fms_word[1], 1, 6) == "APPCH:" then
			idx_rec = tonumber(string.sub(fms_word[1], 7, -1))
			if idx_rec == nil then
				idx_rec = 0
			end
			if fms_word[2] == "A" then
				temp_sid = fms_word[3] .. fms_word[4]
				if old_app_tns ~= temp_sid then
					data_des_app_tns_n = data_des_app_tns_n + 1
					data_des_app_tns[data_des_app_tns_n] = {true, true, true}
					data_des_app_tns[data_des_app_tns_n][1] = fms_word[3]		-- id APP
					data_des_app_tns[data_des_app_tns_n][2] = fms_word[4]		-- id trans
					data_des_app_tns[data_des_app_tns_n][3] = fms_word[5]		-- id E/D
					old_app_tns = fms_word[3] .. fms_word[4]
				end
			else
				if old_app ~= fms_word[3] then
					data_des_app_n = data_des_app_n + 1
					data_des_app[data_des_app_n] = fms_word[3]		-- id APP
					old_app = fms_word[3]
				end
				if string.len(fms_word[9]) > 3 then
					if string.sub(fms_word[9], -1, -1) == "F" then	-- FAF wpt E/D
						ed_app_num = ed_app_num + 1
						ed_app[ed_app_num] = {true, true}
						ed_app[ed_app_num][1] = fms_word[3]	-- id APP
						ed_app[ed_app_num][2] = fms_word[5]	-- id E/D
					end
				end
			end
			-- read Route APP
			fd_compare = false
			if rte_app_num > 0 then
				if fms_word[12] == "FD" and rte_app[rte_app_num][3] ~= fms_word[5] then
					fd_compare = true
				end
			elseif fms_word[12] == "FD" then
				fd_compare = true
			end
			
			rte_app_num = rte_app_num + 1
			rte_app[rte_app_num] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
			
			-- if fms_word[12] == "FD" then
			if fd_compare == true then
				
				rte_app[rte_app_num][25] = idx_rec		-- Record No
				
				rte_app[rte_app_num][1] = fms_word[3]	-- id SID
				rte_app[rte_app_num][2] = fms_word[4]	-- id RW / trans
				
				if fms_word[10] == "L" then
					rte_app[rte_app_num][9] = 2	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				elseif fms_word[10] == "R" or fms_word[10] == "E" then
					rte_app[rte_app_num][9] = 3	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				else
					rte_app[rte_app_num][9] = -1	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
				end
				if string.sub(fms_word[12], 1, 1) == "H" then
					-- HOLD
					rte_app[rte_app_num][3] = fms_word[5]	-- id WPT
					if fms_word[10] == "L" then
						rte_app[rte_app_num][9] = 0		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
					else
						rte_app[rte_app_num][9] = 1		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
					end
				end
				
				-- 5, 12, 14, 19, 22, 24
				rte_app[rte_app_num][3] = dec_name_cifs(fms_word[5], "IF", fms_word[14], fms_word[19], fms_word[22], fms_word[24])
				rte_app[rte_app_num][20] = fms_word[5]	-- navaid_id_cifs
				rte_app[rte_app_num][4] = fms_word[28]	-- restrict speed
				rte_app[rte_app_num][24] = fms_word[27]	-- restrict speed type
				
				if fms_word[23] == "B" then
					rte_app[rte_app_num][5] = fms_word[25]	-- restrict altitude1
					rte_app[rte_app_num][23] = fms_word[24]	-- restrict altitude2
					rte_app[rte_app_num][6] = fms_word[23]	-- restrict altitude +,-,B
				elseif fms_word[23] == "G" or fms_word[23] == "H" or fms_word[23] == "X" or fms_word[23] == "Y" or fms_word[23] == "V" then
					rte_app[rte_app_num][5] = fms_word[25]	-- restrict altitude2
					rte_app[rte_app_num][23] = 0	--fms_word[24]	-- restrict altitude1
					rte_app[rte_app_num][6] = " "	-- restrict altitude +,-,B
				elseif fms_word[23] == "I" then
					rte_app[rte_app_num][5] = fms_word[24]	-- restrict altitude1
					rte_app[rte_app_num][23] = 0	--fms_word[25]	-- restrict altitude2
					rte_app[rte_app_num][6] = " "	-- restrict altitude +,-,B
				elseif fms_word[23] == "J" then
					rte_app[rte_app_num][5] = fms_word[24]	-- restrict altitude1
					rte_app[rte_app_num][23] = 0	--fms_word[25]	-- restrict altitude2
					rte_app[rte_app_num][6] = "+"	-- restrict altitude +,-,B
				else
					rte_app[rte_app_num][5] = fms_word[24]	-- restrict altitude1
					rte_app[rte_app_num][23] = fms_word[25]	-- restrict altitude2
					rte_app[rte_app_num][6] = fms_word[23]	-- restrict altitude +,-,B
				end
				
				rte_app[rte_app_num][7] = fms_word[19]	-- course
				rte_app[rte_app_num][8] = fms_word[29]	-- vpa
				rte_app[rte_app_num][10] = fms_word[22]	-- distance
				rte_app[rte_app_num][11] = fms_word[21]	-- course mag
				rte_app[rte_app_num][12] = fms_word[6]	-- id wpt reg code
				rte_app[rte_app_num][13] = fms_word[14]	-- rec navaid
				rte_app[rte_app_num][14] = fms_word[15]	-- rec navaid reg code
				rte_app[rte_app_num][15] = "IF"	--fms_word[12]	-- path termination
				rte_app[rte_app_num][18] = fms_word[20]	-- distance frrom rec navaid
				rte_app[rte_app_num][19] = fms_word[18]	-- Radius for turn
				rte_app[rte_app_num][21] = fms_word[11]	-- RNP
				rte_app[rte_app_num][16] = 0	-- fly over wpt 0/1
				rte_app[rte_app_num][22] = 0	-- NDB navaid 0/1
				if string.len(fms_word[9]) > 1 then
					if string.sub(fms_word[9], 2, 2) == "Y" then
						rte_app[rte_app_num][16] = 1	-- fly over wpt 0/1
					end
					if string.sub(fms_word[9], 1, 1) == "N" then
						rte_app[rte_app_num][22] = 1	-- NDB navaid 0/1
					elseif string.sub(fms_word[9], 1, 1) == "V" then
						rte_app[rte_app_num][22] = 2	-- NDB navaid 0/1
					end
				end
				rte_app[rte_app_num][17] = 0
				if string.len(fms_word[9]) > 3 then
					if string.sub(fms_word[9], -1, -1) == "F" then	-- FAF wpt E/D
						rte_app[rte_app_num][17] = 1	-- FAF
					elseif string.sub(fms_word[9], 3, 3) == "M" then	-- First missed approach wpt E/D
						rte_app[rte_app_num][17] = 2	-- FMA
					elseif string.sub(fms_word[9], 3, 3) == "A" then	-- Initial Approach Fix
						rte_app[rte_app_num][17] = 3	-- IAF
					elseif string.sub(fms_word[9], 3, 3) == "B" then	-- Itermediate Approach Fix
						rte_app[rte_app_num][17] = 3	-- IAF
					elseif string.sub(fms_word[9], 3, 3) == "C" then	-- Initial Approach Fix with Holding
						rte_app[rte_app_num][17] = 3	-- IAF
					elseif string.sub(fms_word[9], 3, 3) == "D" then	-- Initial Approach Fix with FAC fix
						rte_app[rte_app_num][17] = 3	-- IAF
					elseif string.sub(fms_word[9], -1, -1) == "I" then	-- Initial Approach Fix ILS
						rte_app[rte_app_num][17] = 5	-- IAF
					end
					hold_iaf_enable = 1
					if fms_word[12] == "IF" and fms_word[3] == rte_app[rte_app_num][1] then
						hold_iaf_enable = 0
					end
					if rte_star_num > 0 then
						if fms_word[12] == "IF" and fms_word[3] == rte_star[rte_star_num][1] then
							hold_iaf_enable = 0
						end
					end
					rte_app[rte_app_num][25] = idx_rec
				end
				
				idx_rec = idx_rec + 1
				
				rte_app_num = rte_app_num + 1
				rte_app[rte_app_num] = {}
				
				
			end
			
			rte_app[rte_app_num][25] = idx_rec		-- Record No
			
			rte_app[rte_app_num][1] = fms_word[3]	-- id SID
			rte_app[rte_app_num][2] = fms_word[4]	-- id RW / trans
			
			if fms_word[10] == "L" then
				rte_app[rte_app_num][9] = 2	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			elseif fms_word[10] == "R" or fms_word[10] == "E" then
				rte_app[rte_app_num][9] = 3	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			else
				rte_app[rte_app_num][9] = -1	-- -1 -> no turn, 0 -> HOLD L, 1 -> HOLD R, 2-> TURN L, 3-> TURN R
			end
			if string.sub(fms_word[12], 1, 1) == "H" then
				-- HOLD
				rte_app[rte_app_num][3] = fms_word[5]	-- id WPT
				if fms_word[10] == "L" then
					rte_app[rte_app_num][9] = 0		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
				else
					rte_app[rte_app_num][9] = 1		-- HOLD: -1 -> no hold, 0 -> L, 1 -> R
				end
			end
			
			-- 5, 12, 14, 19, 22, 24
			rte_app[rte_app_num][3] = dec_name_cifs(fms_word[5], fms_word[12], fms_word[14], fms_word[19], fms_word[22], fms_word[24])
			rte_app[rte_app_num][20] = fms_word[5]	-- navaid_id_cifs
			rte_app[rte_app_num][4] = fms_word[28]	-- restrict speed
			rte_app[rte_app_num][24] = fms_word[27]	-- restrict speed type
			
			if fms_word[23] == "B" then
				rte_app[rte_app_num][5] = fms_word[25]	-- restrict altitude1
				rte_app[rte_app_num][23] = fms_word[24]	-- restrict altitude2
				rte_app[rte_app_num][6] = fms_word[23]	-- restrict altitude +,-,B
			elseif fms_word[23] == "G" or fms_word[23] == "H" or fms_word[23] == "X" or fms_word[23] == "Y" or fms_word[23] == "V" then
				rte_app[rte_app_num][5] = fms_word[25]	-- restrict altitude2
				rte_app[rte_app_num][23] = 0	--fms_word[24]	-- restrict altitude1
				rte_app[rte_app_num][6] = " "	-- restrict altitude +,-,B
			elseif fms_word[23] == "I" then
				rte_app[rte_app_num][5] = fms_word[24]	-- restrict altitude1
				rte_app[rte_app_num][23] = 0	--fms_word[25]	-- restrict altitude2
				rte_app[rte_app_num][6] = " "	-- restrict altitude +,-,B
			elseif fms_word[23] == "J" then
				rte_app[rte_app_num][5] = fms_word[24]	-- restrict altitude1
				rte_app[rte_app_num][23] = 0	--fms_word[25]	-- restrict altitude2
				rte_app[rte_app_num][6] = "+"	-- restrict altitude +,-,B
			else
				rte_app[rte_app_num][5] = fms_word[24]	-- restrict altitude1
				rte_app[rte_app_num][23] = fms_word[25]	-- restrict altitude2
				rte_app[rte_app_num][6] = fms_word[23]	-- restrict altitude +,-,B
			end
			
			rte_app[rte_app_num][7] = fms_word[19]	-- course
			rte_app[rte_app_num][8] = fms_word[29]	-- vpa
			rte_app[rte_app_num][10] = fms_word[22]	-- distance
			rte_app[rte_app_num][11] = fms_word[21]	-- course mag
			rte_app[rte_app_num][12] = fms_word[6]	-- id wpt reg code
			rte_app[rte_app_num][13] = fms_word[14]	-- rec navaid
			rte_app[rte_app_num][14] = fms_word[15]	-- rec navaid reg code
			rte_app[rte_app_num][15] = fms_word[12]	-- path termination
			rte_app[rte_app_num][18] = fms_word[20]	-- distance frrom rec navaid
			rte_app[rte_app_num][19] = fms_word[18]	-- Radius for turn
			rte_app[rte_app_num][21] = fms_word[11]	-- RNP
			if fms_word[12] == "CA" or fms_word[12] == "VA" or fms_word[12] == "FA" or fms_word[12] == "FD" 
			or fms_word[12] == "VM" then	--or fms_word[12] == "HM" or fms_word[12] == "HF" or fms_word[12] == "HA" then
				rte_app[rte_app_num][16] = 1	-- fly over wpt 0/1
			else
				rte_app[rte_app_num][16] = 0	-- fly over wpt 0/1
			end
			rte_app[rte_app_num][22] = 0	-- NDB navaid 0/1
			if string.len(fms_word[9]) > 1 then
				if string.sub(fms_word[9], 2, 2) == "Y" then
					rte_app[rte_app_num][16] = 1	-- fly over wpt 0/1
				end
				if string.sub(fms_word[9], 1, 1) == "N" then
					rte_app[rte_app_num][22] = 1	-- NDB navaid 0/1
				elseif string.sub(fms_word[9], 1, 1) == "V" then
					rte_app[rte_app_num][22] = 2	-- NDB navaid 0/1
				end
			end
			rte_app[rte_app_num][17] = 0
			if string.len(fms_word[9]) > 3 then
				if string.sub(fms_word[9], -1, -1) == "F" then	-- FAF wpt E/D
					rte_app[rte_app_num][17] = 1	-- FAF
				elseif string.sub(fms_word[9], 3, 3) == "M" then	-- First missed approach wpt E/D
					rte_app[rte_app_num][17] = 2	-- FMA
				elseif string.sub(fms_word[9], 3, 3) == "A" then	-- Initial Approach Fix
					rte_app[rte_app_num][17] = 3	-- IAF
				elseif string.sub(fms_word[9], 3, 3) == "B" then	-- Itermediate Approach Fix
					rte_app[rte_app_num][17] = 3	-- IAF
				elseif string.sub(fms_word[9], 3, 3) == "C" then	-- Initial Approach Fix with Holding
					rte_app[rte_app_num][17] = 3	-- IAF
				elseif string.sub(fms_word[9], 3, 3) == "D" then	-- Initial Approach Fix with FAC fix
					rte_app[rte_app_num][17] = 3	-- IAF
				elseif string.sub(fms_word[9], -1, -1) == "I" then	-- Initial Approach Fix ILS
					rte_app[rte_app_num][17] = 5	-- IAF
				end
				hold_iaf_enable = 1
				if fms_word[12] == "IF" and fms_word[3] == rte_app[rte_app_num][1] then
					hold_iaf_enable = 0
				end
				if rte_star_num > 0 then
					if fms_word[12] == "IF" and fms_word[3] == rte_star[rte_star_num][1] then
						hold_iaf_enable = 0
					end
				end
				rte_app[rte_app_num][25] = idx_rec
			end
		end
		
		fms_line = file_navdata:read()
	end
	
	--*******************************************************************************************
	-- create idx list of runways
	idx_rec = 0
	local idx_rwy_tmp_num = 0
	raw_table('idx_rwy_tmp')
	local idx_rwy_tmp = {}
	if rnw_data_num > 0 and string.len(ddd_des_icao) > 0 then
		nd_x = string.byte(string.sub(ddd_des_icao, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for jj = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][jj]
					if rnw_data[idx_rec][1] == ddd_des_icao then
						idx_rwy_tmp_num = idx_rwy_tmp_num + 1
						idx_rwy_tmp[idx_rwy_tmp_num] = idx_rec
					end
				end
			end
		end
	end
	
	-- find unknown runways
	idx_rec = 0
	if des_rwy_num > 0 and idx_rwy_tmp_num > 0 then
		for jj = 1, idx_rwy_tmp_num do
			idx_rec = idx_rwy_tmp[jj]
			
			rnw_data[idx_rec][9] = 0
					
			if string.sub(rnw_data[idx_rec][2], -1, -1) == "L" or string.sub(rnw_data[idx_rec][2], -1, -1) == "R"
			or string.sub(rnw_data[idx_rec][2], -1, -1) == "C" then
				temp_num = tonumber(string.sub(rnw_data[idx_rec][2], 1, -2))
				if temp_num == nil then
					temp_str = ""
				else
					temp_str = "RW" .. string.format("%02d", temp_num) .. string.sub(rnw_data[idx_rec][2], -1, -1)
				end
			else
				temp_num = tonumber(rnw_data[idx_rec][2])
				if temp_num == nil then
					temp_str = ""
				else
					temp_str = "RW" .. string.format("%02d", temp_num) .. " "
				end
			end
			
			for ii = 1, des_rwy_num do
				if des_data_rwy[ii][1] == temp_str then
					rnw_data[idx_rec][9] = 1
					des_data_rwy[ii][6] = 1
				end
			end
		end
		
		--dump_rnw_data(ddd_des_icao)
		
		-- compare runways and change to CIFP data
		for ii = 1, des_rwy_num do
			if des_data_rwy[ii][6] == 0 then
				for jj = 1, idx_rwy_tmp_num do
					idx_rec = idx_rwy_tmp[jj]
					
					if rnw_data[idx_rec][9] == 0 then
						compare = 0
						
						if string.sub(rnw_data[idx_rec][2], -1, -1) == "L" or string.sub(rnw_data[idx_rec][2], -1, -1) == "R"
						or string.sub(rnw_data[idx_rec][2], -1, -1) == "C" then
							temp_num = tonumber(string.sub(rnw_data[idx_rec][2], 1, -2))
							if temp_num == nil then
								temp_str = ""
							else
								temp_str = "RW" .. string.format("%02d", temp_num) .. string.sub(rnw_data[idx_rec][2], -1, -1)
							end
						else
							temp_num = tonumber(rnw_data[idx_rec][2])
							if temp_num == nil then
								temp_str = ""
							else
								temp_str = "RW" .. string.format("%02d", temp_num) .. " "
							end
						end
						
						if string.sub(des_data_rwy[ii][1], -1, -1) == string.sub(temp_str, -1, -1) then
							compare = 1
						end
						
						if compare == 1 then
							if string.sub(des_data_rwy[ii][1], -1, -1) == " " then
								temp_num =  tonumber(string.sub(des_data_rwy[ii][1], 3, -1))
							else
								temp_num =  tonumber(string.sub(des_data_rwy[ii][1], 3, -2))
							end
							if temp_num ~= nil then
								kk = temp_num * 10
								if string.sub(temp_str, -1, -1) == " " then
									temp_num =  tonumber(string.sub(temp_str, 3, -1))
								else
									temp_num =  tonumber(string.sub(temp_str, 3, -2))
								end
								if temp_num ~= nil then
									temp_num = temp_num * 10
									kk = (kk - temp_num + 360) % 360
									if kk > 180 then
										kk = kk - 360
									end
									if kk < 0 then
										kk = -kk
									end
									if kk < 30 then
										if string.sub(des_data_rwy[ii][1], -1, -1) == " " then
											temp_num = tonumber(string.sub(des_data_rwy[ii][1], 3, -1))
											token = string.format("%02d", temp_num)
										else
											temp_num = tonumber(string.sub(des_data_rwy[ii][1], 3, -2))
											token = string.format("%02d", temp_num) .. string.sub(des_data_rwy[ii][1], -1, -1)
										end
										rnw_data[idx_rec][2] = token		-- runway
										rnw_data[idx_rec][9] = 1
										
										des_data_rwy[ii][6] = 1
									end
								end
							end
						end
					end
				end
				
				if des_data_rwy[ii][6] == 0 then
					compare = 0
					if string.sub(des_data_rwy[ii][1], -1, -1) == " " then
						temp_num =  tonumber(string.sub(des_data_rwy[ii][1], 3, -1))
					else
						temp_num =  tonumber(string.sub(des_data_rwy[ii][1], 3, -2))
					end
					if temp_num ~= nil then
						kk = temp_num
						for jj = 1, idx_rwy_tmp_num do
							idx_rec = idx_rwy_tmp[jj]
							if rnw_data[idx_rec][9] == 0 then
								if string.sub(rnw_data[idx_rec][2], -1, -1) == "L" or string.sub(rnw_data[idx_rec][2], -1, -1) == "R"
								or string.sub(rnw_data[idx_rec][2], -1, -1) == "C" then
									temp_num = tonumber(string.sub(rnw_data[idx_rec][2], 1, -2))
									if temp_num == nil then
										temp_str = ""
									else
										temp_str = "RW" .. string.format("%02d", temp_num) .. string.sub(rnw_data[idx_rec][2], -1, -1)
									end
								else
									temp_num = tonumber(rnw_data[idx_rec][2])
									if temp_num == nil then
										temp_str = ""
									else
										temp_str = "RW" .. string.format("%02d", temp_num) .. " "
									end
								end
								if string.sub(temp_str, -1, -1) == " " then
									temp_num =  tonumber(string.sub(temp_str, 3, -1))
								else
									temp_num =  tonumber(string.sub(temp_str, 3, -2))
								end
								if temp_num ~= nil then
									if temp_num == kk then
										compare = compare + 1
										nd_x = idx_rec
									end
								end
							end
						end
						if compare == 1 then
							if string.sub(des_data_rwy[ii][1], -1, -1) == " " then
								temp_num =  tonumber(string.sub(des_data_rwy[ii][1], 3, -1))
								token = string.format("%02d", temp_num)
							else
								temp_num = tonumber(string.sub(des_data_rwy[ii][1], 3, -2))
								token = string.format("%02d", temp_num) .. string.sub(des_data_rwy[ii][1], -1, -1)
							end
							idx_rec = nd_x
							rnw_data[idx_rec][2] = token		-- runway
							rnw_data[idx_rec][9] = 1
							
							des_data_rwy[ii][6] = 1
						end
					end
				end
			end
		end
	end
	
	
	copy_from_des_data(in_rte)
	copy_from_rte_star(in_rte)
	copy_from_rte_app(in_rte)
	
end

function copy_from_des_data(in_rte)
	if in_rte == 0 then
		des_data_rwy_1 = {}
		des_rwy_num_1 = 0
		data_des_star_1 = {}
		data_des_star_n_1 = 0
		data_des_star_tns_1 = {}
		data_des_star_tns_n_1 = 0
		data_des_app_1 = {}
		data_des_app_n_1 = 0
		data_des_app_tns_1 = {}
		data_des_app_tns_n_1 = 0
		-- don't used data
		-- ed_app_num_1 = 0
		-- ed_app_1 = {}
		
		if des_rwy_num > 0 then
			des_rwy_num_1 = des_rwy_num
			for ii = 1, des_rwy_num do
				des_data_rwy_1[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					des_data_rwy_1[ii][jj] = des_data_rwy[ii][jj]
				end
			end
		end
		
		if data_des_star_n > 0 then
			data_des_star_n_1 = data_des_star_n
			for ii = 1, data_des_star_n do
				data_des_star_1[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star_1[ii][jj] = data_des_star[ii][jj]
				end
			end
		end
		
		if data_des_star_tns_n > 0 then
			data_des_star_tns_n_1 = data_des_star_tns_n
			for ii = 1, data_des_star_tns_n do
				data_des_star_tns_1[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star_tns_1[ii][jj] = data_des_star_tns[ii][jj]
				end
			end
		end
		
		if data_des_app_n > 0 then
			data_des_app_n_1 = data_des_app_n
			for ii = 1, data_des_app_n do
				data_des_app_1[ii] = data_des_app[ii]
			end
		end
		
		if data_des_app_tns_n > 0 then
			data_des_app_tns_n_1 = data_des_app_tns_n
			for ii = 1, data_des_app_tns_n do
				data_des_app_tns_1[ii] = {true, true, true}
				for jj = 1, 3 do
					data_des_app_tns_1[ii][jj] = data_des_app_tns[ii][jj]
				end
			end
		end
		
	elseif in_rte == 1 then
		des_data_rwy_2 = {}
		des_rwy_num_2 = 0
		data_des_star_2 = {}
		data_des_star_n_2 = 0
		data_des_star_tns_2 = {}
		data_des_star_tns_n_2 = 0
		data_des_app_2 = {}
		data_des_app_n_2 = 0
		data_des_app_tns_2 = {}
		data_des_app_tns_n_2 = 0
		-- don't used data
		-- ed_app_num_2 = 0
		-- ed_app_2 = {}
		
		if des_rwy_num > 0 then
			des_rwy_num_2 = des_rwy_num
			for ii = 1, des_rwy_num do
				des_data_rwy_2[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					des_data_rwy_2[ii][jj] = des_data_rwy[ii][jj]
				end
			end
		end
		
		if data_des_star_n > 0 then
			data_des_star_n_2 = data_des_star_n
			for ii = 1, data_des_star_n do
				data_des_star_2[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star_2[ii][jj] = data_des_star[ii][jj]
				end
			end
		end
		
		if data_des_star_tns_n > 0 then
			data_des_star_tns_n_2 = data_des_star_tns_n
			for ii = 1, data_des_star_tns_n do
				data_des_star_tns_2[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star_tns_2[ii][jj] = data_des_star_tns[ii][jj]
				end
			end
		end
		
		if data_des_app_n > 0 then
			data_des_app_n_2 = data_des_app_n
			for ii = 1, data_des_app_n do
				data_des_app_2[ii] = data_des_app[ii]
			end
		end
		
		if data_des_app_tns_n > 0 then
			data_des_app_tns_n_2 = data_des_app_tns_n
			for ii = 1, data_des_app_tns_n do
				data_des_app_tns_2[ii] = {true, true, true}
				for jj = 1, 3 do
					data_des_app_tns_2[ii][jj] = data_des_app_tns[ii][jj]
				end
			end
		end
	end
end

function copy_to_des_data(in_rte)
	if in_rte == 0 then
		des_data_rwy = {}
		des_rwy_num = 0
		data_des_star = {}
		data_des_star_n = 0
		data_des_star_tns = {}
		data_des_star_tns_n = 0
		data_des_app = {}
		data_des_app_n = 0
		data_des_app_tns = {}
		data_des_app_tns_n = 0
		-- don't used
		-- ed_app_num = 0
		-- ed_app = {}
		
		if des_rwy_num_1 > 0 then
			des_rwy_num = des_rwy_num_1
			for ii = 1, des_rwy_num do
				des_data_rwy[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					des_data_rwy[ii][jj] = des_data_rwy_1[ii][jj]
				end
			end
		end
		
		if data_des_star_n_1 > 0 then
			data_des_star_n = data_des_star_n_1
			for ii = 1, data_des_star_n do
				data_des_star[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star[ii][jj] = data_des_star_1[ii][jj]
				end
			end
		end
		
		if data_des_star_tns_n_1 > 0 then
			data_des_star_tns_n = data_des_star_tns_n_1
			for ii = 1, data_des_star_tns_n do
				data_des_star_tns[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star_tns[ii][jj] = data_des_star_tns_1[ii][jj]
				end
			end
		end
		
		if data_des_app_n_1 > 0 then
			data_des_app_n = data_des_app_n_1
			for ii = 1, data_des_app_n do
				data_des_app[ii] = data_des_app_1[ii]
			end
		end
		
		if data_des_app_tns_n_1 > 0 then
			data_des_app_tns_n = data_des_app_tns_n_1
			for ii = 1, data_des_app_tns_n do
				data_des_app_tns[ii] = {true, true, true}
				for jj = 1, 3 do
					data_des_app_tns[ii][jj] = data_des_app_tns_1[ii][jj]
				end
			end
		end
		
	elseif in_rte == 1 then
		des_data_rwy = {}
		des_rwy_num = 0
		data_des_star = {}
		data_des_star_n = 0
		data_des_star_tns = {}
		data_des_star_tns_n = 0
		data_des_app = {}
		data_des_app_n = 0
		data_des_app_tns = {}
		data_des_app_tns_n = 0
		-- don't used
		-- ed_app_num = 0
		-- ed_app = {}
		
		if des_rwy_num_2 > 0 then
			des_rwy_num = des_rwy_num_2
			for ii = 1, des_rwy_num do
				des_data_rwy[ii] = {true, true, true, true, true, true}
				for jj = 1, 6 do
					des_data_rwy[ii][jj] = des_data_rwy_2[ii][jj]
				end
			end
		end
		
		if data_des_star_n_2 > 0 then
			data_des_star_n = data_des_star_n_2
			for ii = 1, data_des_star_n do
				data_des_star[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star[ii][jj] = data_des_star_2[ii][jj]
				end
			end
		end
		
		if data_des_star_tns_n_2 > 0 then
			data_des_star_tns_n = data_des_star_tns_n_2
			for ii = 1, data_des_star_tns_n do
				data_des_star_tns[ii] = {true, true}
				for jj = 1, 2 do
					data_des_star_tns[ii][jj] = data_des_star_tns_2[ii][jj]
				end
			end
		end
		
		if data_des_app_n_2 > 0 then
			data_des_app_n = data_des_app_n_2
			for ii = 1, data_des_app_n do
				data_des_app[ii] = data_des_app_2[ii]
			end
		end
		
		if data_des_app_tns_n_2 > 0 then
			data_des_app_tns_n = data_des_app_tns_n_2
			for ii = 1, data_des_app_tns_n do
				data_des_app_tns[ii] = {true, true, true}
				for jj = 1, 3 do
					data_des_app_tns[ii][jj] = data_des_app_tns_2[ii][jj]
				end
			end
		end
	end
end

function copy_from_rte_star(in_rte)
	if in_rte == 0 then
		rte_star_1 = {}
		rte_star_num_1 = 0
		
		if rte_star_num > 0 then
			rte_star_num_1 = rte_star_num
			for ii = 1, rte_star_num do
				rte_star_1[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_star_1[ii][jj] = rte_star[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		rte_star_2 = {}
		rte_star_num_2 = 0
		
		if rte_star_num > 0 then
			rte_star_num_2 = rte_star_num
			for ii = 1, rte_star_num do
				rte_star_2[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_star_2[ii][jj] = rte_star[ii][jj]
				end
			end
		end
	end
end

function copy_to_rte_star(in_rte)
	if in_rte == 0 then
		rte_star = {}
		rte_star_num = 0
		
		if rte_star_num_1 > 0 then
			rte_star_num = rte_star_num_1
			for ii = 1, rte_star_num do
				rte_star[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_star[ii][jj] = rte_star_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		rte_star = {}
		rte_star_num = 0
		
		if rte_star_num_2 > 0 then
			rte_star_num = rte_star_num_2
			for ii = 1, rte_star_num do
				rte_star[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_star[ii][jj] = rte_star_2[ii][jj]
				end
			end
		end
	end
end

function copy_from_rte_app(in_rte)
	if in_rte == 0 then
		rte_app_1 = {}
		rte_app_num_1 = 0
		
		if rte_app_num > 0 then
			rte_app_num_1 = rte_app_num
			for ii = 1, rte_app_num do
				rte_app_1[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_app_1[ii][jj] = rte_app[ii][jj]
				end
			end
		end
		
	elseif in_rte == 1 then
		rte_app_2 = {}
		rte_app_num_2 = 0
		
		if rte_app_num > 0 then
			rte_app_num_2 = rte_app_num
			for ii = 1, rte_app_num do
				rte_app_2[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_app_2[ii][jj] = rte_app[ii][jj]
				end
			end
		end
	end
end

function copy_to_rte_app(in_rte)
	if in_rte == 0 then
		rte_app = {}
		rte_app_num = 0
		
		if rte_app_num_1 > 0 then
			rte_app_num = rte_app_num_1
			for ii = 1, rte_app_num do
				rte_app[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_app[ii][jj] = rte_app_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		rte_app = {}
		rte_app_num = 0
		
		if rte_app_num_2 > 0 then
			rte_app_num = rte_app_num_2
			for ii = 1, rte_app_num do
				rte_app[ii] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true}
				for jj = 1, 25 do
					rte_app[ii][jj] = rte_app_2[ii][jj]
				end
			end
		end
	end
end

function dump_star()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if rte_star_num > 0 then
			for vvv = 1, rte_star_num  do
				fms_line = rte_star[vvv][1] .. "," .. rte_star[vvv][2] .. "," .. rte_star[vvv][3] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end


function B738_detect_fmod()
	
	local fms_line = ""
	local temp_fmod = 0
	
	--file_name = "Aircraft/B737-800X/fmod/b738.snd"
	file_name = file_path2 .. FILE_NAME_FMOD		--"fmod/b738.snd"
	file_navdata = io.open(file_name, "r")
	if file_navdata == nil then
		fmod_version = ">NOT FOUND<"
	else
		fmod_version = ">UNKNOWN<"
		fms_line = file_navdata:read()
		while fms_line do
			if string.sub(fms_line, 1, 9) == "#version=" then
				temp_fmod = string.len(fms_line)
				if temp_fmod > 9 then
					fmod_version = string.sub(fms_line, 10, -1)
				end
				break
			end
			fms_line = file_navdata:read()
		end
		file_navdata:close()
	end
	
end

function find_last_str(f_str1, f_str2)
	local result = 0
	local len_pom = 0
	local res_pom2 = string.len(f_str1)
	
	for len_pom = 1, res_pom2 do
		if string.sub(f_str1, len_pom, len_pom) == f_str2 then
			result = len_pom
		end
	end
	
	return result
end


function B738_default_fmod_config()

	-- DEFAULT value
	B738DR_enable_pax_boarding = 1
	B738DR_enable_gyro = 0
	B738DR_enable_crew = 1
	B738DR_enable_chatter = 1
	B738DR_airport_set = 1
	B738DR_vol_int_ducker = 0
	B738DR_vol_int_eng = 5
	B738DR_vol_int_start = 5
	B738DR_vol_int_ac = 5
	B738DR_vol_int_gyro = 5
	B738DR_vol_int_roll = 5
	B738DR_vol_int_bump = 5
	B738DR_vol_int_pax = 5
	B738DR_vol_int_pax_applause = 0
	--B738DR_vol_int_wind = 5
	B738DR_enable_mutetrim = 0
	B738DR_vol_airport = 5
	--B738DR_ann_set = 0
	--B738DR_announcement_set = 0

	-- new
	B738DR_enable_chatter = 1
	B738DR_enable_fmc_mute_on = 0
	B738DR_vol_PM = 5
	B738DR_vol_int_wind_vol = 5
	B738DR_vol_crew = 5
	B738DR_announcement_set = 0
	B738DR_pa_announc_set = 0
	eq_low = 0
	eq_mid = 0
	eq_high = 0
	play_cargo = 0
	B738DR_vol_computer = 5
	B738DR_vol_FAC = 5
	B738DR_vol_weather = 5
	B738DR_TO_trigger = 0
	B738DR_vol_int_trim  = 5
	
end

function B738_default_others_config()
	
	B738DR_align_time = 0
	B738DR_refuel_time = 0
	simDR_hide_yoke = 1
	B738DR_toe_brakes_ovr = 0
	B738DR_chock_on_startup = 0
	B738DR_pause_td = 0
	B738DR_lock_idle_thrust = 0
	B738DR_engine_no_running_state = 0
	B738DR_parkbrake_remove_chock = 1
	B738DR_throttle_noise = 0
	B738DR_fuelgauge = 0
	B738DR_com_decimal = 0
	B738DR_mmr = 0
	B738DR_mmr_colour = 0
	B738DR_aspirated_probe = 1
	B738DR_smart_ap_knob = 0
	B738DR_nosewheel = 0
	B738DR_fpln_format = 0
	units = 0
	B738DR_track_up = 1
	B738DR_baro_in_hpa = 0
	B738DR_min_baro_radio = 0
	B738DR_vor_radial = 1
	if simDR_int_ver < 120000 then
		B738DR_kill_windshield = 0
	else
		B738DR_kill_windshield = 3
	end
	B738DR_hide_glass = 0
	B738DR_tire_blown = 1
	B738DR_nd_rings = 1
	B738DR_white_bug = 0
	B738DR_gp_cable_connect = 0
	B738DR_chrono_variant = 0
	B738DR_pfd_fmc_flap_spd = 1
	B738DR_nd_tcas_range = 1
	B738DR_takeoff_hdg = 0
	B738DR_init_ref_smart = 0
	B738DR_hide_gauges_reflection = 0
	B738DR_flight_ctrl = 1
	B738DR_brake_temp = 1
	B738DR_hardware_thr_lever = 1
	B738DR_detail_cl = 0
	B738DR_fmc_input_lag = 0
	B738DR_sync_pilot = 0
	B738DR_cust_nav_data = 0
	B738DR_prior_tx_com = 0
	B738DR_passengers = 1
	simDR_pitch_nz = 0
	simDR_roll_nz = 0
	simDR_yaw_nz = 0
	B738DR_flap_detents_thr[0] = 0.0
	B738DR_flap_detents_thr[1] = 0.0625
	B738DR_flap_detents_thr[2] = 0.1875
	B738DR_flap_detents_thr[3] = 0.3125
	B738DR_flap_detents_thr[4] = 0.4375
	B738DR_flap_detents_thr[5] = 0.5625
	B738DR_flap_detents_thr[6] = 0.6875
	B738DR_flap_detents_thr[7] = 0.8125
	B738DR_flap_detents_thr[8] = 0.9375
	-- B738DR_flap_detents_thr[0] = 0.0
	-- B738DR_flap_detents_thr[1] = 0.03125
	-- B738DR_flap_detents_thr[2] = 0.03125
	-- B738DR_flap_detents_thr[3] = 0.03125
	-- B738DR_flap_detents_thr[4] = 0.03125
	-- B738DR_flap_detents_thr[5] = 0.03125
	-- B738DR_flap_detents_thr[6] = 0.03125
	-- B738DR_flap_detents_thr[7] = 0.03125
	-- B738DR_flap_detents_thr[8] = 0.03125
	B738DR_flap_detents[0] = 0.0
	B738DR_flap_detents[1] = 0.125
	B738DR_flap_detents[2] = 0.25
	B738DR_flap_detents[3] = 0.375
	B738DR_flap_detents[4] = 0.5
	B738DR_flap_detents[5] = 0.625
	B738DR_flap_detents[6] = 0.75
	B738DR_flap_detents[7] = 0.875
	B738DR_flap_detents[8] = 1.0
	B738DR_irs_ding = 0
	B738DR_long_led_strobes = 0
	B738DR_wings_variant = 0
	B738DR_satcom_variant = 0
	B738DR_pulse_lite = 0
	B738DR_led_lights = 0
	B738DR_hud_disable = 1
	B738DR_yoke_disco_ap = 2
	B738DR_lock_thrust = 0
	B738DR_aoa_ra = 0
	B738DR_yoke_to_center = 1
	B738DR_parkbrake_set = 0
	B738DR_strobe_cockpit = 1
	B738DR_particle_rain = 0
	B738DR_fps_enable = 0
	B738DR_fd_helper = 0
	B738DR_brunner_enable = 0
	B738DR_xhsi_enable = 0
	B738DR_cfy_enable = 0
	B738DR_cfy_trim_control = 0
	B738DR_sam_enable = 0
	B738DR_ext_payload = 0
	B738DR_eicas_full_n1 = 1
	B738DR_eicas_n1_num = 1
	B738DR_single_cue_fd = 0
	--B738DR_kill_retract_lights = 0
	B738DR_cust_axis_speedbrake_mapped = -1
	B738DR_cust_axis_speedbrake_reverse = 0
	B738DR_cust_axis_flaps_mapped = -1
	B738DR_cust_axis_flaps_reverse = 0
	B738DR_cust_axis_speedbrake_noise = 0
	B738DR_cust_axis_speedbrake_arm = 0
	B738DR_cust_axis_speedbrake_flight = 0
	B738DR_cust_axis_landgear_mapped = -1
	B738DR_cust_axis_landgear_reverse = 0
	B738DR_cust_axis_landgear_off = 0
	B738DR_cust_axis_mixture1_mapped = -1
	B738DR_cust_axis_mixture1_reverse = 0
	B738DR_cust_axis_mixture2_mapped = -1
	B738DR_cust_axis_mixture2_reverse = 0
	
	B738DR_manip_yoke = 0
	B738DR_manip_throttle = 0
	B738DR_manip_flaps = 0
	B738DR_manip_speedbrake = 0
	B738DR_manip_trimwheel = 0
	B738DR_manip_park_brake = 0
	B738DR_manip_stab_trim = 0
	B738DR_manip_fuelcutout = 0
	B738DR_manip_horn_cutout = 0
	
	B738DR_restrict_fms = 0
	
	for uuu = 0, 14 do
		B738DR_ra_callout_disable[uuu] = 0
	end
	B738DR_ra_callout_disable[0] = 1
	B738DR_ra_callout_disable[6] = 1
	B738DR_std_pax_weight[0] = 87
	B738DR_std_pax_weight[1] = 76
	B738DR_std_pax_weight[2] = 35
	if B738DR_73x == 2 then
		-- Layout 4
		B738DR_pax_layout = 4
		B738DR_std_pax_num[0] = 24
		B738DR_std_pax_num[1] = 30
		B738DR_std_pax_num[2] = 24
		B738DR_std_pax_num[3] = 30
		B738DR_std_pax_num[4] = 30
		B738DR_zone_arm[0] = 9.3218	-- 367 in
		B738DR_zone_arm[1] = 12.827	-- 505 in
		B738DR_zone_arm[2] = 16.4592	-- 648 in
		B738DR_zone_arm[3] = 19.939	-- 785 in
		B738DR_zone_arm[4] = 24.003	-- 945 in
		B738DR_fa_fwd_kg = 190.5	-- kg
		B738DR_fa_aft_kg = 190.5	-- kg
		B738DR_galley_fwd_kg = 286	-- kg
		B738DR_galley_aft_kg = 410	-- kg
		B738DR_oew_kg = 38172 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
	else
		B738DR_pax_layout = 0
		B738DR_std_pax_num[0] = 16
		B738DR_std_pax_num[1] = 36
		B738DR_std_pax_num[2] = 36
		B738DR_std_pax_num[3] = 36
		B738DR_std_pax_num[4] = 36
		B738DR_zone_arm[0] = 6.6548	-- 262 in
		B738DR_zone_arm[1] = 11.43	-- 450 in
		B738DR_zone_arm[2] = 16.764	-- 660 in
		B738DR_zone_arm[3] = 21.463	-- 845 in
		B738DR_zone_arm[4] = 26.289	-- 1035 in
		B738DR_fa_fwd_kg = 190.5	-- kg
		B738DR_fa_aft_kg = 190.5	-- kg
		B738DR_galley_fwd_kg = 286	-- kg
		B738DR_galley_aft_kg = 410	-- kg
		B738DR_oew_kg = 42034 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
	end
	B738DR_ap_yoke_tolerance = 0
	B738DR_sync_pilot_isdf = 0
	B738DR_hide_seats = 0
	B738DR_disable_fo_callout = 0
	B738DR_vnav_alt_disable = 0
	B738DR_vnavalt_althld = 0
	B738DR_fd_cam_disable = 0
	B738DR_selcal_disable = 0
	B738DR_isfd_rmi = 0
	B738DR_new_fuel_cutoff = 0
	B738DR_nps = 0
	B738DR_yaw_damper = 0
	B738DR_spd_intv_but = 1
	B738DR_alt_intv_but = 1
	B738DR_mute_switches = 0
	B738DR_clock_variant = 0
	B738DR_old_mcp = 0
	B738DR_rmi = 1
	B738DR_sfp = 0
	B738DR_at_wakeup = 0
	B738DR_plan_fuel = 1
	B738DR_oil_qty_pct_qts = 1
	B738DR_acp = 0 --always
	B738DR_vnav_wakeup_idle = 5
	B738DR_nd_arpt_rw_lenght = 0
	B738DR_guarded_covers = 0
	
	B738DR_def_clb_alt = 1500
	B738DR_def_clb_alt2 = 3000
	B738DR_def_cut_alt = 800
	B738DR_def_accel_alt = 1000
	B738DR_def_eo_accel_alt = 1000
	
	B738DR_plus_hundred = 0
	B738DR_vorloc_lnav = 0
	B738DR_fmc_type = 0
	B738DR_raas = 0
	B738DR_not_airstairs = 0
	B738DR_engine_start_auto = 0
	B738DR_probe_heat_auto = 0
	B738DR_autoland_install = 0
	B738DR_nps_disable = 0
	B738DR_ian_not_install = 0
	B738DR_autosave = 0
	B738DR_fuel_pumps_off_placard = 0
	B738DR_cat3_placard = 0
	B738DR_brakes_placard = 0
	B738DR_ff_eicas = 0
	B738DR_digital_press_panel = 0
	B738DR_no_rte2 = 0
	B738DR_gs_on_pfd = 0
	B738DR_ils_gls_annun = 0
	B738DR_no_smoking = 0
	B738DR_no_vcr_switch = 0
	B738DR_gw_entry = 0
	B738DR_vanish_y = 0
	B738DR_ra_radio_altimeter = 0
	B738DR_ra_decision_height = 0
	B738DR_ra_500_1000_baro = 0
	B738DR_missed_app_colour = 0
	B738DR_high_alt_ldg = 0
	B738DR_ra_minimums = 0
	
	B738DR_no_eyebrow_kill = 0
	B738DR_eyebrow_kill = 1
	B738DR_no_aft_exit_kill = 0
	B738DR_aft_exit_kill = 1
	B738DR_no_pip_kill = 0
	B738DR_pip_kill = 1
	B738DR_fo_callout_var = 0
	B738DR_fuel_cutoff_sw = 0
	B738DR_arrow_3d_helper = 1
	B738DR_efb_colorize = 1
	B738DR_hyd_compensator = 0
	B738DR_ctr_fuel_pump_auto = 1
	B738DR_vnav_common = 1
	
	if B738DR_b737_variant == 0 then
		--800
		B738DR_wings_variant = 1		--scimi winglets
		B738DR_led_lights = 1			--LED
		B738DR_satcom_variant = 3		--rear2
		B738DR_no_eyebrow_kill = 0		--no Eyebrow
		B738DR_eyebrow_kill = 1
		B738DR_no_pip_kill = 1			-- PiP
		B738DR_pip_kill = 0
		B738DR_nosewheel = 2
	elseif B738DR_b737_variant == 1 then
		--900
		B738DR_wings_variant = 2		--no winglets
		B738DR_led_lights = 0			--Halogen
		B738DR_satcom_variant = 3		--both
		B738DR_no_eyebrow_kill = 1		--Eyebrow
		B738DR_eyebrow_kill = 0
		B738DR_no_pip_kill = 0			--no PiP
		B738DR_pip_kill = 1
		B738DR_nosewheel = 2
		B738DR_hud_disable = 1
	elseif B738DR_b737_variant == 2 then
		--700
		B738DR_wings_variant = 0		--blended winglets
		B738DR_led_lights = 1			--LED
		B738DR_satcom_variant = 3		--rear2
		B738DR_no_eyebrow_kill = 0		--no Eyebrow
		B738DR_eyebrow_kill = 1
		B738DR_no_pip_kill = 1			-- PiP
		B738DR_pip_kill = 0
		B738DR_nosewheel = 2
	elseif B738DR_b737_variant == 3 then
		--600
		B738DR_wings_variant = 2		--no winglets
		B738DR_led_lights = 0			--Halogen
		B738DR_satcom_variant = 2		--rear
		B738DR_no_eyebrow_kill = 1		--Eyebrow
		B738DR_eyebrow_kill = 0
		B738DR_no_pip_kill = 0			--no PiP
		B738DR_pip_kill = 1
		B738DR_nosewheel = 2
		B738DR_hud_disable = 1
	elseif B738DR_b737_variant == 4 then
		--900ER
		B738DR_wings_variant = 1		--scimi winglets
		B738DR_led_lights = 1			--LED
		B738DR_satcom_variant = 1		--both
		B738DR_no_eyebrow_kill = 0		--no Eyebrow
		B738DR_eyebrow_kill = 1
		B738DR_no_pip_kill = 1			--PiP
		B738DR_pip_kill = 0
		B738DR_nosewheel = 2
		B738DR_no_aft_exit_kill = 1
		B738DR_aft_exit_kill = 0
		B738DR_isfd_rmi = 0
	end
	
	if B738DR_b737_variant >= 0 then
		if B738DR_b737_variant == 4 then
			if B738DR_wings_variant > 1 then
				B738DR_wings_variant = 0
			end
		end
		if B738DR_wings_variant > 1 then
			if B738DR_no_pip_kill ~= 0 then
				B738DR_no_pip_kill = 0
				B738DR_pip_kill = 1
			end
		end
		if simDR_int_ver < 120000 then
			if B738DR_kill_windshield < 3 then
				B738DR_kill_windshield = 2
			else
				B738DR_kill_windshield = 3
			end
		else
			B738DR_kill_windshield = 3
		end
		B738DR_particle_rain = 0
		
		if (B738DR_b737_variant == 0 or B738DR_b737_variant == 2 or B738DR_b737_variant == 4) and B738DR_wings_variant < 2 then
			if B738DR_no_pip_kill ~= 0 then
				B738DR_no_eyebrow_kill = 0
				B738DR_eyebrow_kill = 1
				
			end
		end
		
		if B738DR_no_eyebrow_kill ~= 0 then
			B738DR_hud_disable = 1
		end
		
		if B738DR_b737_variant ~= 4 then
			B738DR_no_aft_exit_kill = 0
			B738DR_aft_exit_kill = 1
			
		end
	end
	
end

function rtrim(txt_trim)
	local trim_lenght = string.len(txt_trim)
	local ll = 0
	local result_pom = txt_trim
	local result = ""
	
	if trim_lenght ~= 0 then
		for ll = 1, trim_lenght do
			if string.sub(txt_trim, ll, ll) ~= " " then
				result_pom = string.sub(result_pom, ll, -1)
				break
			end
		end
		trim_lenght = string.len(result_pom)
		if trim_lenght ~= 0 then
			for ll = trim_lenght, 1, -1 do
				if string.sub(result_pom, ll, ll) ~= " " then
					result = string.sub(result_pom, 1, ll)
					break
				end
			end
		end
	end
	return result
end

function B738_load_hw_config()
	
	local fms_line = ""
	local fms_txt = ""
	local fms_val = ""
	local temp_fmod = 0
	local jj = 0
	local kk = 0
	local ignore_line = 0
	
	file_name = "Output/preferences/" .. FILE_NAME_HW_CFG
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		fms_line = file_navdata:read()
		while fms_line do
			if string.len(fms_line) > 1 and string.byte(fms_line, -1) == 13 then	-- CR
				fms_line = string.sub(fms_line, 1, -2)
			end
			
			ignore_line = 0
			jj,kk = string.find(fms_line, "=")
			if jj == nil then
				ignore_line = 1
			else
				if jj > 1 and jj < string.len(fms_line) then
					fms_txt = string.sub(fms_line, 1, jj-1)
					fms_txt = rtrim(fms_txt)
					fms_txt = string.upper(fms_txt)
					fms_val = string.sub(fms_line, jj+1, -1)
					temp_fmod = tonumber(fms_val)
				else
					ignore_line = 1
				end
			end
			
			if ignore_line == 0 then
				if fms_txt == "TOE BRAKE AXIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_toe_brakes_ovr = 1
						else
							B738DR_toe_brakes_ovr = 0
						end
					end
				elseif fms_txt == "LOCK IDLE THR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_lock_idle_thrust = 1
						else
							B738DR_lock_idle_thrust = 0
						end
					end
				elseif fms_txt == "LOCK THROTTLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_lock_thrust = 1
						else
							B738DR_lock_thrust = 0
						end
					end
				elseif fms_txt == "BRAKE REM CHOCK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_parkbrake_remove_chock = 1
						else
							B738DR_parkbrake_remove_chock = 0
						end
					end
				elseif fms_txt == "THROTTLE NOISE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and temp_fmod <= 10 then
							B738DR_throttle_noise = temp_fmod
						else
							B738DR_throttle_noise = 10
						end
					end
				elseif fms_txt == "SMART AP KNOB" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_smart_ap_knob = 1
						else
							B738DR_smart_ap_knob = 0
						end
					end
				elseif fms_txt == "NOSEWHEEL AXIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_nosewheel = 1
						elseif temp_fmod == 2 then
							B738DR_nosewheel = 2
						elseif temp_fmod == 3 then
							B738DR_nosewheel = 3
						else
							B738DR_nosewheel = 0
						end
					end
				elseif fms_txt == "FPLN SAVE FMT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fpln_format = 1
						else
							B738DR_fpln_format = 0
						end
					end
				elseif fms_txt == "PITCH 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							simDR_pitch_nz = temp_fmod / 100
						else
							simDR_pitch_nz = 0
						end
					end
				elseif fms_txt == "YAW 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							simDR_yaw_nz = temp_fmod / 100
						else
							simDR_yaw_nz = 0
						end
					end
				elseif fms_txt == "ROLL 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							simDR_roll_nz = temp_fmod / 100
						else
							simDR_roll_nz = 0
						end
					end
				elseif fms_txt == "FLAP DETENT UP" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[0] = temp_fmod / 1000000
						else
							B738DR_flap_detents[0] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 1" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[1] = temp_fmod / 1000000
						else
							B738DR_flap_detents[1] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 2" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[2] = temp_fmod / 1000000
						else
							B738DR_flap_detents[2] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 5" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[3] = temp_fmod / 1000000
						else
							B738DR_flap_detents[3] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 10" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[4] = temp_fmod / 1000000
						else
							B738DR_flap_detents[4] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 15" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[5] = temp_fmod / 1000000
						else
							B738DR_flap_detents[5] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 25" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[6] = temp_fmod / 1000000
						else
							B738DR_flap_detents[6] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 30" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[7] = temp_fmod / 1000000
						else
							B738DR_flap_detents[7] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 40" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[8] = temp_fmod / 1000000
						else
							B738DR_flap_detents[8] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR UP" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[0] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[0] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 1" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[1] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[1] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 2" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[2] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[2] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 5" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[3] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[3] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 10" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[4] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[4] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 15" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[5] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[5] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 25" then
						if temp_fmod ~= nil then
							if temp_fmod >= 0 and  temp_fmod <= 1000000 then
								B738DR_flap_detents_thr[6] = temp_fmod / 1000000
							else
								B738DR_flap_detents_thr[6] = 0
							end
						end
				elseif fms_txt == "FLAP DT THR 30" then
						if temp_fmod ~= nil then
							if temp_fmod >= 0 and  temp_fmod <= 1000000 then
								B738DR_flap_detents_thr[7] = temp_fmod / 1000000
							else
								B738DR_flap_detents_thr[7] = 0
							end
						end
				elseif fms_txt == "FLAP DT THR 40" then
						if temp_fmod ~= nil then
							if temp_fmod >= 0 and  temp_fmod <= 1000000 then
								B738DR_flap_detents_thr[8] = temp_fmod / 1000000
							else
								B738DR_flap_detents_thr[8] = 0
							end
						end
				elseif fms_txt == "SPEEDBRAKE MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_speedbrake_mapped = temp_fmod
					else
						B738DR_cust_axis_speedbrake_mapped = -1
					end
				elseif fms_txt == "FLAP MAPPED" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_flaps_mapped = temp_fmod
					else
						B738DR_cust_axis_flaps_mapped = -1
					end
				elseif fms_txt == "SPEEDBRAKE REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_speedbrake_reverse = 0
						else
							B738DR_cust_axis_speedbrake_reverse = 1
						end
					end
				elseif fms_txt == "FLAP REVERSE" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_flaps_reverse = 0
						else
							B738DR_cust_axis_flaps_reverse = 1
						end
					end
				elseif fms_txt == "SPEEDBRAKE NOI" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_cust_axis_speedbrake_noise = 1
						elseif temp_fmod == 2 then
							B738DR_cust_axis_speedbrake_noise = 2
						elseif temp_fmod == 3 then
							B738DR_cust_axis_speedbrake_noise = 3
						elseif temp_fmod == 4 then
							B738DR_cust_axis_speedbrake_noise = 4
						elseif temp_fmod == 5 then
							B738DR_cust_axis_speedbrake_noise = 5
						elseif temp_fmod == 6 then
							B738DR_cust_axis_speedbrake_noise = 6
						elseif temp_fmod == 7 then
							B738DR_cust_axis_speedbrake_noise = 7
						elseif temp_fmod == 8 then
							B738DR_cust_axis_speedbrake_noise = 8
						elseif temp_fmod == 9 then
							B738DR_cust_axis_speedbrake_noise = 9
						else
							B738DR_cust_axis_speedbrake_noise = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE ARM" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_cust_axis_speedbrake_arm = temp_fmod / 1000000
						else
							B738DR_cust_axis_speedbrake_arm = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE FLT" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_cust_axis_speedbrake_flight = temp_fmod / 1000000
						else
							B738DR_cust_axis_speedbrake_flight = 0
						end
					end
				elseif fms_txt == "LANDGEAR MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_landgear_mapped = temp_fmod
					else
						B738DR_cust_axis_landgear_mapped = -1
					end
				elseif fms_txt == "LANDGEAR REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_landgear_reverse = 0
						else
							B738DR_cust_axis_landgear_reverse = 1
						end
					end
				elseif fms_txt == "LANDGEAR OFF" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_cust_axis_landgear_off = temp_fmod / 1000000
						else
							B738DR_cust_axis_landgear_off = 0
						end
					end
				elseif fms_txt == "MIXTURE1 MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_mixture1_mapped = temp_fmod
					else
						B738DR_cust_axis_mixture1_mapped = -1
					end
				elseif fms_txt == "MIXTURE1 REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_mixture1_reverse = 0
						else
							B738DR_cust_axis_mixture1_reverse = 1
						end
					end
				elseif fms_txt == "MIXTURE2 MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_mixture2_mapped = temp_fmod
					else
						B738DR_cust_axis_mixture2_mapped = -1
					end
				elseif fms_txt == "MIXTURE2 REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_mixture2_reverse = 0
						else
							B738DR_cust_axis_mixture2_reverse = 1
						end
					end
				end
			end
			fms_line = file_navdata:read()
		end
		file_navdata:close()
	end
end

function read_state_cmd(state_str)
	local fms_line = ""
	local fms_txt = ""
	local fms_val = ""
	local ignore_line = 0
	local jj = 0
	local kk = 0
	local result = ""
	
	-- file_name = FILE_NAME_STATE
	file_name = "Output/preferences/" .. FILE_NAME_STATE
	
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		fms_line = file_navdata:read()
		while fms_line do
			if string.len(fms_line) > 1 and string.byte(fms_line, -1) == 13 then	-- CR
				fms_line = string.sub(fms_line, 1, -2)
			end
			
			ignore_line = 0
			jj,kk = string.find(fms_line, "=")
			if jj == nil then
				ignore_line = 1
			else
				if jj > 1 and jj < string.len(fms_line) then
					fms_txt = string.sub(fms_line, 1, jj-1)
					fms_txt = rtrim(fms_txt)
					fms_txt = string.upper(fms_txt)
					fms_val = string.sub(fms_line, jj+1, -1)
				else
					ignore_line = 1
				end
			end
			if ignore_line == 0 then
				if fms_txt == state_str then
					result = fms_val
				end
			end
			if result ~= "" then
				break
			end
			fms_line = file_navdata:read()
		end
		file_navdata:close()
	end
	
	local result_len = string.len(result)
	while result_len ~= 0 do
		if string.sub(result, 1, 1) == " " and result_len > 1 then
			result = string.sub (result, 2, -1)
			result_len = result_len - 1
		else
			break
		end
	end
	
	return result
end

function save_state_cmd(state_cd, state_ta, state_fl1, state_fl2)
	local fms_line = ""
	
	-- file_name = FILE_NAME_STATE
	file_name = "Output/preferences/" .. FILE_NAME_STATE
	
	file_navdata = io.open(file_name, "w")
	if file_navdata ~= nil then
		if string.len(state_cd) ~= 0 then
			fms_line = "CD = " .. state_cd .. "\n"
			file_navdata:write(fms_line)
		end
		if string.len(state_ta) ~= 0 then
			fms_line = "TA = " .. state_ta .. "\n"
			file_navdata:write(fms_line)
		end
		if string.len(state_fl1) ~= 0 then
			fms_line = "SW1 = " .. state_fl1 .. "\n"
			file_navdata:write(fms_line)
		end
		if string.len(state_fl2) ~= 0 then
			fms_line = "SW2 = " .. state_fl2 .. "\n"
			file_navdata:write(fms_line)
		end
		file_navdata:close()
	end
	
	--return result
end

function B738_load_config(xfile_path_cfg)
	
	local fms_line = ""
	local fms_txt = ""
	local fms_val = ""
	local temp_fmod = 0
	local jj = 0
	local kk = 0
	local ignore_line = 0
	
	if xfile_path_cfg == "" then
		file_name = "Output/preferences/" .. FILE_NAME_CFG	--"b738x.cfg"
	else
		file_name = xfile_path_cfg .. FILE_NAME_CFG
	end
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		fms_line = file_navdata:read()
		while fms_line do
			if string.len(fms_line) > 1 and string.byte(fms_line, -1) == 13 then	-- CR
				fms_line = string.sub(fms_line, 1, -2)
			end
			
			ignore_line = 0
			jj,kk = string.find(fms_line, "=")
			if jj == nil then
				ignore_line = 1
			else
				if jj > 1 and jj < string.len(fms_line) then
					fms_txt = string.sub(fms_line, 1, jj-1)
					fms_txt = rtrim(fms_txt)
					fms_txt = string.upper(fms_txt)
					fms_val = string.sub(fms_line, jj+1, -1)
					temp_fmod = tonumber(fms_val)
				else
					ignore_line = 1
				end
			end
			
			if ignore_line == 0 then
				-- FMOD
				if fms_txt == "PAX BOARDING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_enable_pax_boarding = 1
						else
							B738DR_enable_pax_boarding = 0
						end
					end
				-- elseif fms_txt == "GYRO VIBRATORS" then
					-- if temp_fmod ~= nil then
						-- if temp_fmod == 1 then
							-- B738DR_enable_gyro = 1
						-- else
							-- B738DR_enable_gyro = 0
						-- end
					-- end
				elseif fms_txt == "CREW ANNOUN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_enable_crew = 1
						else
							B738DR_enable_crew = 0
						end
					end
				elseif fms_txt == "CHATTER PASS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_enable_chatter = 1
						else
							B738DR_enable_chatter = 0
						end
					end
				elseif fms_txt == "AIRPORT SET" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_airport_set = 1
						elseif temp_fmod == 2 then
							B738DR_airport_set = 2
						else
							B738DR_airport_set = 3
						end
					end
				elseif fms_txt == "INTERNAL VOL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vol_int_ducker = 1
						else
							B738DR_vol_int_ducker = 0
						end
					end
				elseif fms_txt == "INT ENG SND" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_eng = temp_fmod
						else
							B738DR_vol_int_eng = 10
						end
					end
				elseif fms_txt == "INT ENG START" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_start = temp_fmod
						else
							B738DR_vol_int_start = 10
						end
					end
				elseif fms_txt == "AC FANS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_ac = temp_fmod
						else
							B738DR_vol_int_ac = 10
						end
					end
				elseif fms_txt == "GYRO VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_gyro = temp_fmod
						else
							B738DR_vol_int_gyro = 10
						end
					end
				elseif fms_txt == "ROLL VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_roll = temp_fmod
						else
							B738DR_vol_int_roll = 10
						end
					end
				elseif fms_txt == "BUMP INTENS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_bump = temp_fmod
						else
							B738DR_vol_int_bump = 10
						end
					end
				elseif fms_txt == "PAX VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and temp_fmod <= 10 then
							B738DR_vol_int_pax = temp_fmod
						else
							B738DR_vol_int_pax = 5
						end
					end
				elseif fms_txt == "PAX APPLAUSE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vol_int_pax_applause = 1
						else
							B738DR_vol_int_pax_applause = 0
						end
					end
				elseif fms_txt == "INT WIND" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_wind_vol = temp_fmod
						else
							B738DR_vol_int_wind_vol = 10
						end
					end
				elseif fms_txt == "MUTE TRIM WH" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_enable_mutetrim = 1
						else
							B738DR_enable_mutetrim = 0
						end
					end
				elseif fms_txt == "AIRPORT VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_airport = temp_fmod
						else
							B738DR_vol_airport = 10
						end
					end
				-- elseif fms_txt == "ANNOUNCEM SET" then
					-- if temp_fmod ~= nil then
						 -- temp_fmod = math.floor(temp_fmod + 0.5)
						-- if temp_fmod >= 0 and  temp_fmod <= 3 then
							-- --B738DR_ann_set = temp_fmod
							-- B738DR_announcement_set = temp_fmod
						-- else
							-- --B738DR_ann_set = 0
							-- B738DR_announcement_set = 0
						-- end
					-- end
				elseif fms_txt == "MUTE FMC" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_enable_fmc_mute_on = 1
						else
							B738DR_enable_fmc_mute_on = 0
						end
					end
				elseif fms_txt == "TO TRIGGER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_TO_trigger = 1
						else
							B738DR_TO_trigger = 0
						end
					end
				elseif fms_txt == "PM VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_PM = temp_fmod
						else
							B738DR_vol_PM = 10
						end
					end
				elseif fms_txt == "ANNOUNC.VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_crew = temp_fmod
						else
							B738DR_vol_crew = 10
						end
					end
				elseif fms_txt == "ANNOUNC.SET NR" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and temp_fmod <= 23 then		--15
							--B738DR_announcement_set = temp_fmod
							B738DR_pa_announc_set = temp_fmod
						else
							--B738DR_announcement_set = 0
							B738DR_pa_announc_set = 0
						end
					end
				elseif fms_txt == "ADD LOWS" then
						if temp_fmod ~= nil then
							 temp_fmod = math.floor(temp_fmod + 0.5)
							if temp_fmod >= 0 and  temp_fmod <= 10 then
								eq_low = temp_fmod
							else
								eq_low = 0
							end
						end
				elseif fms_txt == "ADD MIDS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							eq_mid = temp_fmod
						else
							eq_mid = 0
						end
					end
				elseif fms_txt == "ADD HIGHS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							eq_high = temp_fmod
						else
							eq_high = 0
						end
					end
				elseif fms_txt == "START CARGO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							play_cargo = 1
						else
							play_cargo = 0
						end
					end
				elseif fms_txt == "COMPUTER VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_computer = temp_fmod
						else
							B738DR_vol_computer = 10
						end
					end
				elseif fms_txt == "FA COCKPIT VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_FAC = temp_fmod
						else
							B738DR_vol_FAC = 10
						end
					end
				elseif fms_txt == "WEATHER VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_weather = temp_fmod
						else
							B738DR_vol_weather = 10
						end
					end
				elseif fms_txt == "TRIMWHEEL VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							B738DR_vol_int_trim = temp_fmod
						else
							B738DR_vol_int_trim = 10
						end
					end
				-- OTHERS
				elseif fms_txt == "UNITS" then
					temp_fmod = tonumber(string.sub(fms_line, 17, -1))
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							units = 1
						else
							units = 0
						end
					end
				elseif fms_txt == "ALIGN TIME" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_align_time = 1
						elseif temp_fmod == 2 then
							B738DR_align_time = 2
						else
							B738DR_align_time = 0
						end
					end
				elseif fms_txt == "REFUEL TIME" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_refuel_time = 1
						elseif temp_fmod == 2 then
							B738DR_refuel_time = 2
						else
							B738DR_refuel_time = 0
						end
					end
				elseif fms_txt == "CHECKLIST" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_detail_cl = 1
						else
							B738DR_detail_cl = 0
						end
					end
				elseif fms_txt == "FMC INPUT LAG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fmc_input_lag = 1
						else
							B738DR_fmc_input_lag = 0
						end
					end
				elseif fms_txt == "AP YOKE TLRNC" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ap_yoke_tolerance = 1
						else
							B738DR_ap_yoke_tolerance = 0
						end
					end
				elseif fms_txt == "HIDE YOKE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							simDR_hide_yoke = 1
						else
							simDR_hide_yoke = 0
						end
					end
				elseif fms_txt == "HIDE SEATS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_hide_seats = 1
						else
							B738DR_hide_seats = 0
						end
					end
				elseif fms_txt == "FO CALLOUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_disable_fo_callout = 1
						else
							B738DR_disable_fo_callout = 0
						end
					end
				elseif fms_txt == "VNAV ALT DIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vnav_alt_disable = 1
						else
							B738DR_vnav_alt_disable = 0
						end
					end
				elseif fms_txt == "VNAV COMMON" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vnav_common = 1
						else
							B738DR_vnav_common = 0
						end
					end
				elseif fms_txt == "VNAVALT_ALTHLD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vnavalt_althld = 1
						else
							B738DR_vnavalt_althld = 0
						end
					end
				elseif fms_txt == "FD CAM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fd_cam_disable = 1
						else
							B738DR_fd_cam_disable = 0
						end
					end
				elseif fms_txt == "SELCAL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_selcal_disable = 1
						else
							B738DR_selcal_disable = 0
						end
					end
				elseif fms_txt == "ISFD_RMI" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_isfd_rmi = 1
						else
							B738DR_isfd_rmi = 0
						end
					end
				elseif fms_txt == "NEW_FUELCUTOFF" then
					if temp_fmod ~= nil then
						B738DR_new_fuel_cutoff = math.floor(temp_fmod)
					end
				elseif fms_txt == "NPS" then
					if temp_fmod ~= nil then
						B738DR_nps = math.floor(temp_fmod)
					end
				elseif fms_txt == "YAW_DAMPER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_yaw_damper = 1
						else
							B738DR_yaw_damper = 0
						end
					end
				elseif fms_txt == "MCP_SPD_INTV" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_spd_intv_but = 1
						else
							B738DR_spd_intv_but = 0
						end
					end
				elseif fms_txt == "MCP_ALT_INTV" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_alt_intv_but = 1
						else
							B738DR_alt_intv_but = 0
						end
					end
				elseif fms_txt == "VANISH_Y" then
					if temp_fmod ~= nil then
						if temp_fmod ~= nil then
							B738DR_vanish_y = math.floor(temp_fmod)
							if B738DR_vanish_y < 400 or B738DR_vanish_y > 600 then
								B738DR_vanish_y = 400
							end
						end
					end
				elseif fms_txt == "MUTE SWITCHES" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_mute_switches = 1
						else
							B738DR_mute_switches = 0
						end
					end
				-- elseif fms_txt == "CLOCK_VARIANT" then
					-- if temp_fmod ~= nil then
						-- B738DR_clock_variant = math.floor(temp_fmod)
					-- end
				elseif fms_txt == "CLB ALT" then
					if temp_fmod ~= nil then
						B738DR_def_clb_alt = math.floor(temp_fmod)
					end
				elseif fms_txt == "CLB ALT2" then
					if temp_fmod ~= nil then
						B738DR_def_clb_alt2 = math.floor(temp_fmod)
					end
				elseif fms_txt == "CUT ALT" then
					if temp_fmod ~= nil then
						B738DR_def_cut_alt = math.floor(temp_fmod)
					end
				elseif fms_txt == "ACCEL ALT" then
					if temp_fmod ~= nil then
						B738DR_def_accel_alt = math.floor(temp_fmod)
					end
				elseif fms_txt == "EO ACCEL ALT" then
					if temp_fmod ~= nil then
						B738DR_def_eo_accel_alt = math.floor(temp_fmod)
					end
				elseif fms_txt == "VNAV_WU_IDLE" then
					if temp_fmod ~= nil then
						B738DR_vnav_wakeup_idle = math.floor(temp_fmod)
					end
				elseif fms_txt == "ARPT_MIN_RW" then
					if temp_fmod ~= nil then
						B738DR_nd_arpt_rw_lenght = math.floor(temp_fmod)
					end
				elseif fms_txt == "PLUS HUNDRED" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_plus_hundred = 1
						else
							B738DR_plus_hundred = 0
						end
					end
				elseif fms_txt == "RADIO_ALT_CO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_radio_altimeter = 1
						else
							B738DR_ra_radio_altimeter = 0
						end
					end
				elseif fms_txt == "DECISION_HGT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_decision_height = 1
						else
							B738DR_ra_decision_height = 0
						end
					end
				elseif fms_txt == "500_1000_BARO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_500_1000_baro = 1
						else
							B738DR_ra_500_1000_baro = 0
						end
					end
					
				elseif fms_txt == "MISSED_APP_COL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_missed_app_colour = 1
						else
							B738DR_missed_app_colour = 0
						end
					end
				elseif fms_txt == "HIGH_ALT_LDG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_high_alt_ldg = 1
						else
							B738DR_high_alt_ldg = 0
						end
					end
				elseif fms_txt == "RA_MINIMUMS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_minimums = 1
						else
							B738DR_ra_minimums = 0
						end
					end
				elseif fms_txt == "EYEBROW" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_no_eyebrow_kill = 1
							B738DR_eyebrow_kill = 0
						else
							B738DR_no_eyebrow_kill = 0
							B738DR_eyebrow_kill = 1
						end
					end
				elseif fms_txt == "AFT_EXIT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_no_aft_exit_kill = 1
							B738DR_aft_exit_kill = 0
						else
							B738DR_no_aft_exit_kill = 0
							B738DR_aft_exit_kill = 1
						end
					end
				elseif fms_txt == "PIP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_no_pip_kill = 1
							B738DR_pip_kill = 0
						else
							B738DR_no_pip_kill = 0
							B738DR_pip_kill = 1
						end
					end
				elseif fms_txt == "FO_CALLOUT_VAR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fo_callout_var = 1
						else
							B738DR_fo_callout_var = 0
						end
					end
				elseif fms_txt == "FUEL_CUTOFF_SW" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fuel_cutoff_sw = 1
						else
							B738DR_fuel_cutoff_sw = 0
						end
					end
				elseif fms_txt == "HYD_COMPENS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_hyd_compensator = 1
						else
							B738DR_hyd_compensator = 0
						end
					end
				elseif fms_txt == "CTR_FPUMP_AUTO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ctr_fuel_pump_auto = 1
							B738DR_fuel_pumps_off_placard = 0
						else
							B738DR_ctr_fuel_pump_auto = 0
						end
					end
				elseif fms_txt == "EFB_COLORIZE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_efb_colorize = 1
						else
							B738DR_efb_colorize = 0
						end
					end
				elseif fms_txt == "3D_ARROW" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_arrow_3d_helper = 1
						else
							B738DR_arrow_3d_helper = 0
						end
					end
				elseif fms_txt == "CLOCK_VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_clock_variant = 1
						else
							B738DR_clock_variant = 0
						end
					end
				elseif fms_txt == "VORLOC_LNAV" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vorloc_lnav = 1
						else
							B738DR_vorloc_lnav = 0
						end
					end
				elseif fms_txt == "FMC TYPE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fmc_type = 1
						else
							B738DR_fmc_type = 0
						end
					end
				elseif fms_txt == "RAAS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_raas = 1
						elseif temp_fmod == 2 then
							B738DR_raas = 2
						else
							B738DR_raas = 0
						end
					end
				elseif fms_txt == "NOT_AIRSTAIRS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_not_airstairs = 1
						else
							B738DR_not_airstairs = 0
						end
					end
				elseif fms_txt == "ENG_START_AUTO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_engine_start_auto = 1
						else
							B738DR_engine_start_auto = 0
						end
					end
				elseif fms_txt == "PROBE_AUTO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_probe_heat_auto = 1
						else
							B738DR_probe_heat_auto = 0
						end
					end
				elseif fms_txt == "ROLLOUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_autoland_install = 1
						else
							B738DR_autoland_install = 0
						end
					end
				elseif fms_txt == "CT_DISABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_nps_disable = 1
						else
							B738DR_nps_disable = 0
						end
					end
				elseif fms_txt == "IAN_DISABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ian_not_install = 1
						else
							B738DR_ian_not_install = 0
						end
					end
				elseif fms_txt == "AUTOSAVE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_autosave = 1
						elseif temp_fmod == 2 then
							B738DR_autosave = 2
						elseif temp_fmod == 3 then
							B738DR_autosave = 3
						else
							B738DR_autosave = 0
						end
					end
				elseif fms_txt == "FF EICAS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ff_eicas = 1
						else
							B738DR_ff_eicas = 0
						end
					end
				elseif fms_txt == "DIG_PRESS_PNL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_digital_press_panel = 1
						else
							B738DR_digital_press_panel = 0
						end
					end
				elseif fms_txt == "NO RTE2" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_no_rte2 = 1
						else
							B738DR_no_rte2 = 0
						end
					end
				elseif fms_txt == "GS_ON_PFD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_gs_on_pfd = 1
						else
							B738DR_gs_on_pfd = 0
						end
					end
				elseif fms_txt == "ILS_GLS_ANN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ils_gls_annun = 1
						else
							B738DR_ils_gls_annun = 0
						end
					end
				elseif fms_txt == "NO_SMOKING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_no_smoking = 1
						elseif temp_fmod == 2 then
							B738DR_no_smoking = 2
						else
							B738DR_no_smoking = 0
						end
					end
				elseif fms_txt == "NO_VCR_SW" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_no_vcr_switch = 1
						else
							B738DR_no_vcr_switch = 0
						end
					end
				elseif fms_txt == "GW_ENTRY" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_gw_entry = 1
						else
							B738DR_gw_entry = 0
						end
					end
				elseif fms_txt == "FUEL PUMPS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fuel_pumps_off_placard = 1
						else
							B738DR_fuel_pumps_off_placard = 0
						end
					end
				elseif fms_txt == "CAT III" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_cat3_placard = 1
						elseif temp_fmod == 2 then
							B738DR_cat3_placard = 2
						elseif temp_fmod == 3 then
							B738DR_cat3_placard = 3
						elseif temp_fmod == 4 then
							B738DR_cat3_placard = 4
						elseif temp_fmod == 5 then
							B738DR_cat3_placard = 5
						else
							B738DR_cat3_placard = 0
						end
					end
				elseif fms_txt == "BRAKES" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_brakes_placard = 1
						else
							B738DR_brakes_placard = 0
						end
					end
				elseif fms_txt == "OLD MCP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_old_mcp = 1
						else
							B738DR_old_mcp = 0
						end
					end
				elseif fms_txt == "RMI" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_rmi = 1
						else
							B738DR_rmi = 0
						end
					end
				elseif fms_txt == "SFP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_sfp = 1
						else
							B738DR_sfp = 0
						end
					end
				elseif fms_txt == "AT_WAKEUP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_at_wakeup = 1
						else
							B738DR_at_wakeup = 0
						end
					end
				elseif fms_txt == "PLAN_FUEL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_plan_fuel = 1
						else
							B738DR_plan_fuel = 0
						end
					end
				elseif fms_txt == "OIL_QTY" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_oil_qty_pct_qts = 1
						else
							B738DR_oil_qty_pct_qts = 0
						end
					end
				elseif fms_txt == "PAUSE AT T/D" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_pause_td = 1
						else
							B738DR_pause_td = 0
						end
					end
				elseif fms_txt == "TOE BRAKE AXIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_toe_brakes_ovr = 1
						else
							B738DR_toe_brakes_ovr = 0
						end
					end
				elseif fms_txt == "LOCK IDLE THR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_lock_idle_thrust = 1
						else
							B738DR_lock_idle_thrust = 0
						end
					end
				elseif fms_txt == "LOCK THROTTLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_lock_thrust = 1
						else
							B738DR_lock_thrust = 0
						end
					end
				elseif fms_txt == "ENG NO RUNNING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_engine_no_running_state = 1
						else
							B738DR_engine_no_running_state = 0
						end
					end
				elseif fms_txt == "BRAKE REM CHOCK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_parkbrake_remove_chock = 1
						else
							B738DR_parkbrake_remove_chock = 0
						end
					end
				elseif fms_txt == "THROTTLE NOISE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and temp_fmod <= 10 then
							B738DR_throttle_noise = temp_fmod
						else
							B738DR_throttle_noise = 10
						end
					end
				elseif fms_txt == "SMART AP KNOB" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_smart_ap_knob = 1
						else
							B738DR_smart_ap_knob = 0
						end
					end
				elseif fms_txt == "FUEL GAUGE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fuelgauge = 1
						else
							B738DR_fuelgauge = 0
						end
					end
				elseif fms_txt == "COM DECIMAL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_com_decimal = 1
						else
							B738DR_com_decimal = 0
						end
					end
				elseif fms_txt == "MMR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_mmr = 1
						else
							B738DR_mmr = 0
						end
					end
				elseif fms_txt == "MMR COLOUR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_mmr_colour = 1
						else
							B738DR_mmr_colour = 0
						end
					end
				elseif fms_txt == "ASP TAT PROBE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_aspirated_probe = 1
						else
							B738DR_aspirated_probe = 0
						end
					end
				elseif fms_txt == "CALLOUT 5" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[0] = 1
						else
							B738DR_ra_callout_disable[0] = 0
						end
					end
				elseif fms_txt == "CALLOUT 10" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[1] = 1
						else
							B738DR_ra_callout_disable[1] = 0
						end
					end
				elseif fms_txt == "CALLOUT 20" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[2] = 1
						else
							B738DR_ra_callout_disable[2] = 0
						end
					end
				elseif fms_txt == "CALLOUT 30" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[3] = 1
						else
							B738DR_ra_callout_disable[3] = 0
						end
					end
				elseif fms_txt == "CALLOUT 40" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[4] = 1
						else
							B738DR_ra_callout_disable[4] = 0
						end
					end
				elseif fms_txt == "CALLOUT 50" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[5] = 1
						else
							B738DR_ra_callout_disable[5] = 0
						end
					end
				elseif fms_txt == "CALLOUT 60" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[6] = 1
						else
							B738DR_ra_callout_disable[6] = 0
						end
					end
				elseif fms_txt == "CALLOUT 100" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[7] = 1
						else
							B738DR_ra_callout_disable[7] = 0
						end
					end
				elseif fms_txt == "CALLOUT 200" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[8] = 1
						else
							B738DR_ra_callout_disable[8] = 0
						end
					end
				elseif fms_txt == "CALLOUT 300" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[9] = 1
						else
							B738DR_ra_callout_disable[9] = 0
						end
					end
				elseif fms_txt == "CALLOUT 400" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[10] = 1
						else
							B738DR_ra_callout_disable[10] = 0
						end
					end
				elseif fms_txt == "CALLOUT 500" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[11] = 1
						elseif temp_fmod == 2 then
							B738DR_ra_callout_disable[11] = 2
						else
							B738DR_ra_callout_disable[11] = 0
						end
					end
				elseif fms_txt == "CALLOUT 1000" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[12] = 1
						elseif temp_fmod == 2 then
							B738DR_ra_callout_disable[12] = 2
						else
							B738DR_ra_callout_disable[12] = 0
						end
					end
				elseif fms_txt == "CALLOUT 1000S" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[13] = 1
						else
							B738DR_ra_callout_disable[13] = 0
						end
					end
				elseif fms_txt == "CALLOUT 2500" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ra_callout_disable[14] = 1
						-- elseif temp_fmod == 2 then
							-- B738DR_ra_callout_disable[14] = 2
						else
							B738DR_ra_callout_disable[14] = 0
						end
					end
				elseif fms_txt == "NOSEWHEEL AXIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_nosewheel = 1
						elseif temp_fmod == 2 then
							B738DR_nosewheel = 2
						elseif temp_fmod == 3 then
							B738DR_nosewheel = 3
						else
							B738DR_nosewheel = 0
						end
					end
				elseif fms_txt == "FPLN SAVE FMT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fpln_format = 1
						else
							B738DR_fpln_format = 0
						end
					end
				elseif fms_txt == "TRACK UP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_track_up = 1
						else
							B738DR_track_up = 0
						end
					end
				elseif fms_txt == "BARO IN HPA" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_baro_in_hpa = 1
						else
							B738DR_baro_in_hpa = 0
						end
					end
				elseif fms_txt == "MIN BARO RADIO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_min_baro_radio = 1
						else
							B738DR_min_baro_radio = 0
						end
					end
				elseif fms_txt == "VOR RADIAL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_vor_radial = 1
						else
							B738DR_vor_radial = 0
						end
					end
				elseif fms_txt == "WINDSH.EFFECTS" then
					if temp_fmod ~= nil then
						if simDR_int_ver < 120000 then
							if temp_fmod == 1 then
								B738DR_kill_windshield = 1
							elseif temp_fmod == 2 then
								B738DR_kill_windshield = 2
							elseif temp_fmod == 3 then
								B738DR_kill_windshield = 3
							else
								B738DR_kill_windshield = 0
							end
						end
					end
				elseif fms_txt == "WINDSH.REFLECT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_hide_glass = 1
						else
							B738DR_hide_glass = 0
						end
					end
				elseif fms_txt == "TIRE BLOWN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_tire_blown = 1
						else
							B738DR_tire_blown = 0
						end
					end
				elseif fms_txt == "ND RINGS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_nd_rings = 1
						else
							B738DR_nd_rings = 0
						end
					end
				elseif fms_txt == "WHITE BUG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_white_bug = 1
						else
							B738DR_white_bug = 0
						end
					end
				elseif fms_txt == "GP CAB CONNECT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_gp_cable_connect = 1
						else
							B738DR_gp_cable_connect = 0
						end
					end
				elseif fms_txt == "CHRONO VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_chrono_variant = 1
						else
							B738DR_chrono_variant = 0
						end
					end
				elseif fms_txt == "PFD FMC FLPSPD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_pfd_fmc_flap_spd = 1
						elseif temp_fmod == 2 then
							B738DR_pfd_fmc_flap_spd = 2
						else
							B738DR_pfd_fmc_flap_spd = 0
						end
					end
				elseif fms_txt == "ND TCAS RANGE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_nd_tcas_range = 1
						else
							B738DR_nd_tcas_range = 0
						end
					end
				elseif fms_txt == "TAKEOFF HDG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_takeoff_hdg = 1
						else
							B738DR_takeoff_hdg = 0
						end
					end
				elseif fms_txt == "INIT REF SMART" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_init_ref_smart = 1
						else
							B738DR_init_ref_smart = 0
						end
					end
				elseif fms_txt == "IRS DING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_irs_ding = 1
						else
							B738DR_irs_ding = 0
						end
					end
				elseif fms_txt == "LONG STROBES" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_long_led_strobes = 1
						else
							B738DR_long_led_strobes = 0
						end
					end
				elseif fms_txt == "GAUGES REFLECT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_hide_gauges_reflection = 1
						else
							B738DR_hide_gauges_reflection = 0
						end
					end
				elseif fms_txt == "FLIGHT CONTROL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_flight_ctrl = 1
						else
							B738DR_flight_ctrl = 0
						end
					end
				elseif fms_txt == "BRAKES TEMP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_brake_temp = 1
						else
							B738DR_brake_temp = 0
						end
					end
				elseif fms_txt == "THROTTLE LEVER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_hardware_thr_lever = 1
						else
							B738DR_hardware_thr_lever = 0
						end
					end
				elseif fms_txt == "SYNC PILOT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_sync_pilot = 1
						elseif temp_fmod == 2 then
							B738DR_sync_pilot = 2
						else
							B738DR_sync_pilot = 0
						end
					end
				elseif fms_txt == "SYNC PILOT STD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_sync_pilot_isdf = 1
						elseif temp_fmod == 2 then
							B738DR_sync_pilot_isdf = 2
						else
							B738DR_sync_pilot_isdf = 0
						end
					end
				elseif fms_txt == "CUST NAVDATA" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_cust_nav_data = 1
						else
							B738DR_cust_nav_data = 0
						end
					end
				elseif fms_txt == "PRIOR TX COM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_prior_tx_com = 1
						elseif temp_fmod == 2 then
							B738DR_prior_tx_com = 2
						else
							B738DR_prior_tx_com = 0
						end
					end
				elseif fms_txt == "FMS ALT/SPD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_restrict_fms = 1
						else
							B738DR_restrict_fms = 0
						end
					end
				elseif fms_txt == "PASSENGERS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_passengers = 1
						elseif temp_fmod == 2 then
							B738DR_passengers = 2
						else
							B738DR_passengers = 0
						end
					end
				elseif fms_txt == "WINGS VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_wings_variant = 1
						elseif temp_fmod == 2 then
							B738DR_wings_variant = 2
						else
							B738DR_wings_variant = 0
						end
					end
				elseif fms_txt == "SATCOM VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_satcom_variant = 1
						elseif temp_fmod == 2 then
							B738DR_satcom_variant = 2
						elseif temp_fmod == 3 then
							B738DR_satcom_variant = 3
						else
							B738DR_satcom_variant = 0
						end
					end
				elseif fms_txt == "PULSELITE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_pulse_lite = 1
						else
							B738DR_pulse_lite = 0
						end
					end
				elseif fms_txt == "LED LIGHTS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_led_lights = 1
						else
							B738DR_led_lights = 0
						end
					end
				elseif fms_txt == "CIRC AOA RA" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_aoa_ra = 1
						elseif temp_fmod == 2 then
							B738DR_aoa_ra = 2
						else
							B738DR_aoa_ra = 0
						end
					end
				elseif fms_txt == "HUD DISABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_hud_disable = 1
						else
							B738DR_hud_disable = 0
						end
					end
				elseif fms_txt == "YOKE DISCO AP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_yoke_disco_ap = 1
						elseif temp_fmod == 2 then
							B738DR_yoke_disco_ap = 2
						else
							B738DR_yoke_disco_ap = 0
						end
					end
				elseif fms_txt == "PARKBRAKE SET" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_parkbrake_set = 1
						else
							B738DR_parkbrake_set = 0
						end
					end
				elseif fms_txt == "YOKE TO CENTER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_yoke_to_center = 1
						else
							B738DR_yoke_to_center = 0
						end
					end
				elseif fms_txt == "BRUNNER ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_brunner_enable = 1
						else
							B738DR_brunner_enable = 0
						end
					end
				elseif fms_txt == "XHSI ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_xhsi_enable = 1
						else
							B738DR_xhsi_enable = 0
						end
					end
				elseif fms_txt == "CFY ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_cfy_enable = 1
						elseif temp_fmod == 2 then
							B738DR_cfy_enable = 2
						else
							B738DR_cfy_enable = 0
						end
					end
				elseif fms_txt == "CFY TRIM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_cfy_trim_control = 1
						else
							B738DR_cfy_trim_control = 0
						end
					end
				elseif fms_txt == "SAM ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_sam_enable = 1
						else
							B738DR_sam_enable = 0
						end
					end
				elseif fms_txt == "EXT_PAYLOAD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_ext_payload = 1
						else
							B738DR_ext_payload = 0
						end
					end
				elseif fms_txt == "EICAS_FULL_N1" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_eicas_full_n1 = 1
						else
							B738DR_eicas_full_n1 = 0
						end
					end
				elseif fms_txt == "EICAS_N1_NUM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_eicas_n1_num = 1
						else
							B738DR_eicas_n1_num = 0
						end
					end
				elseif fms_txt == "SINGLE_CUE_FD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_single_cue_fd = 1
						else
							B738DR_single_cue_fd = 0
						end
					end
				elseif fms_txt == "STROBE COCKPIT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_strobe_cockpit = 1
						else
							B738DR_strobe_cockpit = 0
						end
					end
				elseif fms_txt == "PARTICLE RAIN" then
					if temp_fmod ~= nil then
						if simDR_int_ver < 120000 then
							if temp_fmod == 1 then
								B738DR_particle_rain = 1
							else
								B738DR_particle_rain = 0
							end
						end
					end
				elseif fms_txt == "SHOW FPS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fps_enable = 1
						else
							B738DR_fps_enable = 0
						end
					end
				elseif fms_txt == "FD HELPER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_fd_helper = 1
						else
							B738DR_fd_helper = 0
						end
					end
				elseif fms_txt == "MANIP_YOKE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_yoke = 1
						else
							B738DR_manip_yoke = 0
						end
					end
				elseif fms_txt == "MANIP_THR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_throttle = 1
						else
							B738DR_manip_throttle = 0
						end
					end
				elseif fms_txt == "MANIP_FLAP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_flaps = 1
						else
							B738DR_manip_flaps = 0
						end
					end
				elseif fms_txt == "MANIP_SPDBRK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_speedbrake = 1
						else
							B738DR_manip_speedbrake = 0
						end
					end
				elseif fms_txt == "MANIP_TRIMWH" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_trimwheel = 1
						else
							B738DR_manip_trimwheel = 0
						end
					end
				elseif fms_txt == "MANIP_PARKBRK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_park_brake = 1
						else
							B738DR_manip_park_brake = 0
						end
					end
				elseif fms_txt == "MANIP_STABTRIM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_stab_trim = 1
						else
							B738DR_manip_stab_trim = 0
						end
					end
				elseif fms_txt == "MANIP_FUELCUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_fuelcutout = 1
						else
							B738DR_manip_fuelcutout = 0
						end
					end
				elseif fms_txt == "MANIP_HORNCUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_horn_cutout = 1
						else
							B738DR_manip_horn_cutout = 0
						end
					end
				elseif fms_txt == "MANIP_HORNCUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_manip_horn_cutout = 1
						else
							B738DR_manip_horn_cutout = 0
						end
					end
				elseif fms_txt == "GUIDED_COVERS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_guarded_covers = 1
						else
							B738DR_guarded_covers = 0
						end
					end
				elseif fms_txt == "GUARDED_COVERS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_guarded_covers = 1
						else
							B738DR_guarded_covers = 0
						end
					end
				elseif fms_txt == "PITCH 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							simDR_pitch_nz = temp_fmod / 100
						else
							simDR_pitch_nz = 0
						end
					end
				elseif fms_txt == "YAW 0 ZONE" then
					if temp_fmod ~= nil then
						temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							simDR_yaw_nz = temp_fmod / 100
						else
							simDR_yaw_nz = 0
						end
					end
				elseif fms_txt == "ROLL 0 ZONE" then
					if temp_fmod ~= nil then
						 --temp_fmod = roundUpToIncrement(temp_fmod, 1 )
						temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							simDR_roll_nz = temp_fmod / 100
						else
							simDR_roll_nz = 0
						end
					end
				elseif fms_txt == "CHOCKS STATE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_chock_on_startup = 1
						else
							B738DR_chock_on_startup = 0
						end
					end
				elseif fms_txt == "FLAP DETENT UP" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[0] = temp_fmod / 1000000
						else
							B738DR_flap_detents[0] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 1" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[1] = temp_fmod / 1000000
						else
							B738DR_flap_detents[1] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 2" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[2] = temp_fmod / 1000000
						else
							B738DR_flap_detents[2] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 5" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[3] = temp_fmod / 1000000
						else
							B738DR_flap_detents[3] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 10" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[4] = temp_fmod / 1000000
						else
							B738DR_flap_detents[4] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 15" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[5] = temp_fmod / 1000000
						else
							B738DR_flap_detents[5] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 25" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[6] = temp_fmod / 1000000
						else
							B738DR_flap_detents[6] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 30" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[7] = temp_fmod / 1000000
						else
							B738DR_flap_detents[7] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 40" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents[8] = temp_fmod / 1000000
						else
							B738DR_flap_detents[8] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR UP" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[0] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[0] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 1" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[1] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[1] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 2" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[2] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[2] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 5" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[3] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[3] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 10" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[4] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[4] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 15" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_flap_detents_thr[5] = temp_fmod / 1000000
						else
							B738DR_flap_detents_thr[5] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 25" then
						if temp_fmod ~= nil then
							if temp_fmod >= 0 and  temp_fmod <= 1000000 then
								B738DR_flap_detents_thr[6] = temp_fmod / 1000000
							else
								B738DR_flap_detents_thr[6] = 0
							end
						end
				elseif fms_txt == "FLAP DT THR 30" then
						if temp_fmod ~= nil then
							if temp_fmod >= 0 and  temp_fmod <= 1000000 then
								B738DR_flap_detents_thr[7] = temp_fmod / 1000000
							else
								B738DR_flap_detents_thr[7] = 0
							end
						end
				elseif fms_txt == "FLAP DT THR 40" then
						if temp_fmod ~= nil then
							if temp_fmod >= 0 and  temp_fmod <= 1000000 then
								B738DR_flap_detents_thr[8] = temp_fmod / 1000000
							else
								B738DR_flap_detents_thr[8] = 0
							end
						end
				elseif fms_txt == "SPEEDBRAKE MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_speedbrake_mapped = temp_fmod
					else
						B738DR_cust_axis_speedbrake_mapped = -1
					end
				elseif fms_txt == "FLAP MAPPED" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_flaps_mapped = temp_fmod
					else
						B738DR_cust_axis_flaps_mapped = -1
					end
				elseif fms_txt == "SPEEDBRAKE REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_speedbrake_reverse = 0
						else
							B738DR_cust_axis_speedbrake_reverse = 1
						end
					end
				elseif fms_txt == "FLAP REVERSE" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_flaps_reverse = 0
						else
							B738DR_cust_axis_flaps_reverse = 1
						end
					end
				elseif fms_txt == "SPEEDBRAKE NOI" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							B738DR_cust_axis_speedbrake_noise = 1
						elseif temp_fmod == 2 then
							B738DR_cust_axis_speedbrake_noise = 2
						elseif temp_fmod == 3 then
							B738DR_cust_axis_speedbrake_noise = 3
						elseif temp_fmod == 4 then
							B738DR_cust_axis_speedbrake_noise = 4
						elseif temp_fmod == 5 then
							B738DR_cust_axis_speedbrake_noise = 5
						elseif temp_fmod == 6 then
							B738DR_cust_axis_speedbrake_noise = 6
						elseif temp_fmod == 7 then
							B738DR_cust_axis_speedbrake_noise = 7
						elseif temp_fmod == 8 then
							B738DR_cust_axis_speedbrake_noise = 8
						elseif temp_fmod == 9 then
							B738DR_cust_axis_speedbrake_noise = 9
						else
							B738DR_cust_axis_speedbrake_noise = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE ARM" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_cust_axis_speedbrake_arm = temp_fmod / 1000000
						else
							B738DR_cust_axis_speedbrake_arm = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE FLT" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_cust_axis_speedbrake_flight = temp_fmod / 1000000
						else
							B738DR_cust_axis_speedbrake_flight = 0
						end
					end
				elseif fms_txt == "LANDGEAR MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_landgear_mapped = temp_fmod
					else
						B738DR_cust_axis_landgear_mapped = -1
					end
				elseif fms_txt == "LANDGEAR REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_landgear_reverse = 0
						else
							B738DR_cust_axis_landgear_reverse = 1
						end
					end
				elseif fms_txt == "LANDGEAR OFF" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							B738DR_cust_axis_landgear_off = temp_fmod / 1000000
						else
							B738DR_cust_axis_landgear_off = 0
						end
					end
				elseif fms_txt == "MIXTURE1 MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_mixture1_mapped = temp_fmod
					else
						B738DR_cust_axis_mixture1_mapped = -1
					end
				elseif fms_txt == "MIXTURE1 REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_mixture1_reverse = 0
						else
							B738DR_cust_axis_mixture1_reverse = 1
						end
					end
				elseif fms_txt == "MIXTURE2 MAP" then
					if temp_fmod ~= nil then
						B738DR_cust_axis_mixture2_mapped = temp_fmod
					else
						B738DR_cust_axis_mixture2_mapped = -1
					end
				elseif fms_txt == "MIXTURE2 REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							B738DR_cust_axis_mixture2_reverse = 0
						else
							B738DR_cust_axis_mixture2_reverse = 1
						end
					end
				elseif fms_txt == "M STD WGHT" then
					if temp_fmod ~= nil then
						B738DR_std_pax_weight[0] = math.floor(temp_fmod * 10) / 10
					end
				elseif fms_txt == "F STD WGHT" then
					if temp_fmod ~= nil then
						B738DR_std_pax_weight[1] = math.floor(temp_fmod * 10) / 10
					end
				elseif fms_txt == "K STD WGHT" then
					if temp_fmod ~= nil then
						B738DR_std_pax_weight[2] = math.floor(temp_fmod * 10) / 10
					end
				elseif fms_txt == "PAX LAYOUT" then
					if temp_fmod ~= nil then
						if temp_fmod < 0 or temp_fmod > 3 then
							B738DR_pax_layout = 0
						else
							B738DR_pax_layout = math.floor(temp_fmod)
						end
					end
				elseif fms_txt == "FA FWD" then
					if temp_fmod ~= nil then
						B738DR_fa_fwd_kg = math.floor(temp_fmod * 10) / 10
					end
				elseif fms_txt == "FA AFT" then
					if temp_fmod ~= nil then
						B738DR_fa_aft_kg = math.floor(temp_fmod * 10) / 10
					end
				elseif fms_txt == "GALLEY FWD" then
					if temp_fmod ~= nil then
						B738DR_galley_fwd_kg = math.floor(temp_fmod * 10) / 10
					end
				elseif fms_txt == "GALLEY AFT" then
					if temp_fmod ~= nil then
						B738DR_galley_aft_kg = math.floor(temp_fmod * 10) / 10
					end
				end
			end
			fms_line = file_navdata:read()
		end
		file_navdata:close()
	end
end


function B738_load_cfg_config(xfile_path_cfg)
	
	local fms_line = ""
	local temp_fmod = 0
	local ff = 0
	
	cfg_preset = {}
	cfg_preset[1] = {}
	cfg_preset[2] = {}
	cfg_preset[3] = {}
	cfg_preset[4] = {}
	cfg_preset[5] = {}
	cfg_preset[1][1] = "PRESET 1"
	cfg_preset[2][1] = "PRESET 2"
	cfg_preset[3][1] = "PRESET 3"
	cfg_preset[4][1] = "PRESET 4"
	cfg_preset[5][1] = "PRESET 5"
	for temp_fmod = 1, 5 do
		cfg_preset[temp_fmod][2] = 0		-- UNITS
		cfg_preset[temp_fmod][3] = 0		-- ALIGN TIME
		cfg_preset[temp_fmod][4] = 0		-- REFUEL TIME
		cfg_preset[temp_fmod][5] = 0		-- CHECKLIST
		cfg_preset[temp_fmod][6] = 1		-- HIDE YOKE
		cfg_preset[temp_fmod][7] = 0		-- PAUSE AT T/D
		cfg_preset[temp_fmod][8] = 0		-- TOE BRAKE AXIS
		cfg_preset[temp_fmod][9] = 0		-- LOCK IDLE THR
		cfg_preset[temp_fmod][10] = 0		-- ENG NO RUNNING
		cfg_preset[temp_fmod][11] = 0		-- CHOCKS STATE
		cfg_preset[temp_fmod][12] = 1		-- BRAKE REM CHOCK
		cfg_preset[temp_fmod][13] = 0		-- THROTTLE NOISE
		cfg_preset[temp_fmod][14] = 0		-- FUEL GAUGE
		cfg_preset[temp_fmod][15] = 0		-- NOSEWHEEL AXIS
		cfg_preset[temp_fmod][16] = 0		-- FPLN SAVE FMT
		cfg_preset[temp_fmod][17] = 1		-- TRACK UP
		cfg_preset[temp_fmod][18] = 0		-- BARO IN HPA
		cfg_preset[temp_fmod][19] = 0		-- MIN BARO RADIO
		cfg_preset[temp_fmod][20] = 1		-- VOR RADIAL
		cfg_preset[temp_fmod][21] = 0		-- WINDSH.EFFECTS
		cfg_preset[temp_fmod][22] = 0		-- WINDSH.REFLECT
		cfg_preset[temp_fmod][23] = 1		-- TIRE BLOWN
		cfg_preset[temp_fmod][24] = 1		-- ND RINGS
		cfg_preset[temp_fmod][25] = 0		-- GAUGES REFLECT
		cfg_preset[temp_fmod][26] = 1		-- FLIGHT CONTROL
		cfg_preset[temp_fmod][27] = 1		-- BRAKES TEMP
		cfg_preset[temp_fmod][28] = 1		-- THROTTLE LEVER
		cfg_preset[temp_fmod][29] = 0		-- SYNC PILOT
		cfg_preset[temp_fmod][30] = 1		-- PASSENGERS
		cfg_preset[temp_fmod][31] = 0		-- PITCH 0 ZONE
		cfg_preset[temp_fmod][32] = 0		-- ROLL 0 ZONE
		cfg_preset[temp_fmod][33] = 0		-- YAW 0 ZONE
		cfg_preset[temp_fmod][34] = 0.0		-- FLAP DETENT UP
		cfg_preset[temp_fmod][35] = 0.0625		-- FLAP DETENT 1
		cfg_preset[temp_fmod][36] = 0.1875		-- FLAP DETENT 2
		cfg_preset[temp_fmod][37] = 0.3125		-- FLAP DETENT 5
		cfg_preset[temp_fmod][38] = 0.4375		-- FLAP DETENT 10
		cfg_preset[temp_fmod][39] = 0.5625		-- FLAP DETENT 15
		cfg_preset[temp_fmod][40] = 0.6875		-- FLAP DETENT 25
		cfg_preset[temp_fmod][41] = 0.8125		-- FLAP DETENT 30
		cfg_preset[temp_fmod][42] = 0.9375		-- FLAP DETENT 40
		cfg_preset[temp_fmod][43] = 0.000		-- FLAP DT THR UP
		cfg_preset[temp_fmod][44] = 0.125		-- FLAP DT THR 1
		cfg_preset[temp_fmod][45] = 0.250		-- FLAP DT THR 2
		cfg_preset[temp_fmod][46] = 0.375		-- FLAP DT THR 5
		cfg_preset[temp_fmod][47] = 0.500		-- FLAP DT THR 10
		cfg_preset[temp_fmod][48] = 0.625		-- FLAP DT THR 15
		cfg_preset[temp_fmod][49] = 0.750		-- FLAP DT THR 25
		cfg_preset[temp_fmod][50] = 0.875		-- FLAP DT THR 30
		cfg_preset[temp_fmod][51] = 1.000		-- FLAP DT THR 40
		cfg_preset[temp_fmod][52] 	= 0					--B738DR_irs_ding
		cfg_preset[temp_fmod][53] 	= 0					--B738DR_long_led_strobes
		cfg_preset[temp_fmod][54] 	= 0					--B738DR_wings_variant
		cfg_preset[temp_fmod][55] 	= 0					--B738DR_satcom_variant
		cfg_preset[temp_fmod][56] 	= 0					--B738DR_pulse_lite
		cfg_preset[temp_fmod][57] 	= 0					--B738DR_led_lights
		cfg_preset[temp_fmod][58] 	= 1					--B738DR_hud_disable
		cfg_preset[temp_fmod][59] 	= 2					--B738DR_yoke_disco_ap
		cfg_preset[temp_fmod][60] 	= 0					--B738DR_lock_thrust
		cfg_preset[temp_fmod][61] 	= 0					--B738DR_aoa_ra
		cfg_preset[temp_fmod][62] 	= 1					--B738DR_yoke_to_center
		cfg_preset[temp_fmod][63] 	= 0					--B738DR_parkbrake_set
		cfg_preset[temp_fmod][64] 	= 1					--B738DR_strobe_cockpit
		cfg_preset[temp_fmod][65] 	= 0					--B738DR_particle_rain
		cfg_preset[temp_fmod][66] 	= 0					--B738DR_fps_enable
		cfg_preset[temp_fmod][67] 	= 0					--B738DR_brunner_enable
		cfg_preset[temp_fmod][68] 	= 0					--B738DR_com_decimal
		cfg_preset[temp_fmod][69] 	= 0					--B738DR_smart_ap_knob
		
		cfg_preset[temp_fmod][70] 	= -1				--B738DR_cust_axis_speedbrake_mapped
		cfg_preset[temp_fmod][71] 	= -1				--B738DR_cust_axis_flaps_mapped
		cfg_preset[temp_fmod][72] 	= 0					--B738DR_cust_axis_speedbrake_reverse
		cfg_preset[temp_fmod][73] 	= 0					--B738DR_cust_axis_flaps_reverse
		cfg_preset[temp_fmod][74] 	= 0					--B738DR_cust_axis_speedbrake_noise
		cfg_preset[temp_fmod][75] 	= 0					--B738DR_cust_axis_speedbrake_arm
		cfg_preset[temp_fmod][76] 	= 0					--B738DR_cust_axis_speedbrake_flight
		cfg_preset[temp_fmod][77] 	= 0					--B738DR_restrict_fms
		
		cfg_preset[temp_fmod][78] 	= -1					--B738DR_cust_axis_landgear_mapped
		cfg_preset[temp_fmod][79] 	= 0					--B738DR_cust_axis_landgear_reverse
		cfg_preset[temp_fmod][80] 	= 0					--B738DR_cust_axis_landgear_off
		cfg_preset[temp_fmod][81] 	= -1				--B738DR_cust_axis_mixture1_mapped
		cfg_preset[temp_fmod][82] 	= 0					--B738DR_cust_axis_mixture1_reverse
		cfg_preset[temp_fmod][83] 	= -1				--B738DR_cust_axis_mixture2_mapped
		cfg_preset[temp_fmod][84] 	= 0					--B738DR_cust_axis_mixture2_reverse
		
		cfg_preset[temp_fmod][85] 	= 0					--B738DR_mmr
		cfg_preset[temp_fmod][86] 	= 1					--B738DR_aspirated_probe
		
		-- [87] .. [99]
		for uuu = 0, 14 do
			cfg_preset[temp_fmod][87+uuu] 	= 1					--B738DR_ra_callout_disable
			
		end
		cfg_preset[temp_fmod][100] 	= 0					--B738DR_fmc_input_lag
		cfg_preset[temp_fmod][101] 	= 0					--B738DR_xhsi_enable
		cfg_preset[temp_fmod][102] 	= 0					--B738DR_white_bug
		cfg_preset[temp_fmod][103] 	= 0					--B738DR_gp_cable_connect
		cfg_preset[temp_fmod][104] 	= 0					--B738DR_chrono_variant
		cfg_preset[temp_fmod][105] 	= 0					--B738DR_cust_nav_data
		cfg_preset[temp_fmod][106] 	= 0					--B738DR_prior_tx_com
		cfg_preset[temp_fmod][107] 	= 1					--B738DR_pfd_fmc_flap_spd
		cfg_preset[temp_fmod][108] 	= 0					--B738DR_ap_yoke_tolerance
		cfg_preset[temp_fmod][109] 	= 0					--B738DR_fd_helper
		cfg_preset[temp_fmod][110] 	= 0					--B738DR_mmr_colour
		cfg_preset[temp_fmod][111] 	= 0					--B738DR_nd_tcas_range
		cfg_preset[temp_fmod][112] 	= 0					--B738DR_takeoff_hdg
		cfg_preset[temp_fmod][113] 	= 0					--B738DR_init_ref_smart
		cfg_preset[temp_fmod][114] 	= 0					--B738DR_sync_pilot_isdf
		cfg_preset[temp_fmod][115] 	= 0					--B738DR_cfy_enable
		cfg_preset[temp_fmod][116] 	= 0					--B738DR_hide_seats
		cfg_preset[temp_fmod][117] 	= 0					--B738DR_sam_enable
		cfg_preset[temp_fmod][118] 	= 0					--B738DR_disable_fo_callout
		cfg_preset[temp_fmod][119] 	= 0					--B738DR_vnav_alt_disable
		cfg_preset[temp_fmod][120] 	= 0					--B738DR_vnavalt_althld
		cfg_preset[temp_fmod][121] 	= 0					--B738DR_fd_cam_disable
		
		cfg_preset[temp_fmod][122] 	= 0					--B738DR_manip_yoke
		cfg_preset[temp_fmod][123] 	= 0					--B738DR_manip_throttle
		cfg_preset[temp_fmod][124] 	= 0					--B738DR_manip_flaps
		cfg_preset[temp_fmod][125] 	= 0					--B738DR_manip_speedbrake
		cfg_preset[temp_fmod][126] 	= 0					--B738DR_manip_trimwheel
		cfg_preset[temp_fmod][127] 	= 0					--B738DR_manip_park_brake
		cfg_preset[temp_fmod][128] 	= 0					--B738DR_manip_stab_trim
		cfg_preset[temp_fmod][129] 	= 0					--B738DR_manip_fuelcutout
		cfg_preset[temp_fmod][130] 	= 0					--B738DR_manip_horn_cutout
		
		cfg_preset[temp_fmod][131] 	= 0					--B738DR_selcal_disable
		cfg_preset[temp_fmod][132] 	= 0					--B738DR_isfd_rmi
		cfg_preset[temp_fmod][133] 	= 0					--B738DR_new_fuel_cutoff
		cfg_preset[temp_fmod][134] 	= 0					--B738DR_nps
		cfg_preset[temp_fmod][135] 	= 0					--B738DR_yaw_damper
		cfg_preset[temp_fmod][136] 	= 0					--B738DR_mute_switches
		cfg_preset[temp_fmod][137] 	= 0					--B738DR_clock_variant
		cfg_preset[temp_fmod][138] 	= 0					--B738DR_old_mcp
		cfg_preset[temp_fmod][139] 	= 0					--B738DR_plus_hundred
		cfg_preset[temp_fmod][140] 	= 0					--B738DR_vorloc_lnav
		cfg_preset[temp_fmod][141] 	= 0					--B738DR_fmc_type
		cfg_preset[temp_fmod][142] 	= 0					--B738DR_raas
		cfg_preset[temp_fmod][143] 	= 0					--B738DR_not_airstairs
		cfg_preset[temp_fmod][144] 	= 0					--B738DR_engine_start_auto
		cfg_preset[temp_fmod][145] 	= 0					--B738DR_probe_heat_auto
		cfg_preset[temp_fmod][146] 	= 0					--B738DR_autoland_install
		cfg_preset[temp_fmod][147] 	= 0					--B738DR_nps_disable
		cfg_preset[temp_fmod][148] 	= 0					--B738DR_ian_not_install
		cfg_preset[temp_fmod][149] 	= 0					--B738DR_autosave
		cfg_preset[temp_fmod][150] 	= 0					--B738DR_fuel_pumps_off_placard
		cfg_preset[temp_fmod][151] 	= 0					--B738DR_cat3_placard
		cfg_preset[temp_fmod][152] 	= 0					--B738DR_brakes_placard
		cfg_preset[temp_fmod][153] 	= 0					--B738DR_ff_eicas
		cfg_preset[temp_fmod][154] 	= 0					--B738DR_digital_press_panel
		cfg_preset[temp_fmod][155] 	= 0					--B738DR_no_rte2
		cfg_preset[temp_fmod][156] 	= 0					--B738DR_gs_on_pfd
		cfg_preset[temp_fmod][157] 	= 0					--B738DR_ils_gls_annun
		cfg_preset[temp_fmod][158] 	= 0					--B738DR_no_smoking
		cfg_preset[temp_fmod][159] 	= 0					--B738DR_no_vcr_switch
		cfg_preset[temp_fmod][160] 	= 0					--B738DR_gw_entry
		cfg_preset[temp_fmod][161] 	= 0					--B738DR_spd_intv_but
		cfg_preset[temp_fmod][162] 	= 0					--B738DR_alt_intv_but
		cfg_preset[temp_fmod][163] 	= 0					--B738DR_rmi
		cfg_preset[temp_fmod][164] 	= 0					--B738DR_sfp
		cfg_preset[temp_fmod][165] 	= 0					--B738DR_at_wakeup
		cfg_preset[temp_fmod][166] 	= 0					--B738DR_plan_fuel
		cfg_preset[temp_fmod][167] 	= 0					--B738DR_oil_qty_pct_qts
		cfg_preset[temp_fmod][168] 	= 0					--B738DR_guarded_covers
		cfg_preset[temp_fmod][169] 	= 0					--B738DR_ra_radio_altimeter
		cfg_preset[temp_fmod][170] 	= 0					--B738DR_ra_decision_height
		cfg_preset[temp_fmod][171] 	= 0					--B738DR_ra_500_1000_baro
	end
	
	local preset_nr_act = 0
	local ignore_line = 0
	local fms_txt = ""
	local fms_val = ""
  local jj = 0
  local kk = 0
	
	if xfile_path_cfg == "" then
		file_name = "Output/preferences/" .. FILE_NAME_ACF_CFG
	else
		file_name = xfile_path_cfg .. FILE_NAME_ACF_CFG
	end
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		fms_line = file_navdata:read()
		while fms_line do
			if string.len(fms_line) > 1 and string.byte(fms_line, -1) == 13 then	-- CR
				fms_line = string.sub(fms_line, 1, -2)
			end
			
			if string.len(fms_line) > 10 then
				ff = tonumber(string.sub(fms_line, 11, -1))
				if ff ~= nil then
					if string.sub(fms_line, 1, 10) == "PRESET NR " and ff > 0 and ff < 6 then
						preset_nr_act = ff
					end
				end
			end
			
			ignore_line = 0
			jj,kk = string.find(fms_line, "=")
			if jj == nil then
				ignore_line = 1
			else
				if jj > 1 and jj < string.len(fms_line) then
					fms_txt = string.sub(fms_line, 1, jj-1)
					fms_txt = rtrim(fms_txt)
					fms_txt = string.upper(fms_txt)
					fms_val = string.sub(fms_line, jj+1, -1)
					temp_fmod = tonumber(fms_val)
				else
					ignore_line = 1
				end
			end
			
			--if preset_nr_act > 0 and preset_nr_act < 6 and string.len(fms_line) > 16 then
			if preset_nr_act > 0 and preset_nr_act < 6 and ignore_line == 0 then
				-- OTHERS
				if fms_txt == "PRESET NAME" then
					cfg_preset[preset_nr_act][1] = fms_val
				elseif fms_txt == "UNITS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][2] = 1
						else
							cfg_preset[preset_nr_act][2] = 0
						end
					end
				elseif fms_txt == "ALIGN TIME" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][3] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][3] = 2
						else
							cfg_preset[preset_nr_act][3] = 0
						end
					end
				elseif fms_txt == "REFUEL TIME" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][4] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][4] = 2
						else
							cfg_preset[preset_nr_act][4] = 0
						end
					end
				elseif fms_txt == "CHECKLIST" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][5] = 1
						else
							cfg_preset[preset_nr_act][5] = 0
						end
					end
				elseif fms_txt == "HIDE YOKE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][6] = 1
						else
							cfg_preset[preset_nr_act][6] = 0
						end
					end
				elseif fms_txt == "PAUSE AT T/D" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][7] = 1
						else
							cfg_preset[preset_nr_act][7] = 0
						end
					end
				elseif fms_txt == "TOE BRAKE AXIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][8] = 1
						else
							cfg_preset[preset_nr_act][8] = 0
						end
					end
				elseif fms_txt == "LOCK IDLE THR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][9] = 1
						else
							cfg_preset[preset_nr_act][9] = 0
						end
					end
				elseif fms_txt == "ENG NO RUNNING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][10] = 1
						else
							cfg_preset[preset_nr_act][10] = 0
						end
					end
				elseif fms_txt == "CHOCKS STATE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][11] = 1
						else
							cfg_preset[preset_nr_act][11] = 0
						end
					end
				elseif fms_txt == "BRAKE REM CHOCK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][12] = 1
						else
							cfg_preset[preset_nr_act][12] = 0
						end
					end
				elseif fms_txt == "THROTTLE NOISE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and temp_fmod <= 10 then
							cfg_preset[preset_nr_act][13] = temp_fmod
						else
							cfg_preset[preset_nr_act][13] = 10
						end
					end
				elseif fms_txt == "FUEL GAUGE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][14] = 1
						else
							cfg_preset[preset_nr_act][14] = 0
						end
					end
				elseif fms_txt == "NOSEWHEEL AXIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][15] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][15] = 2
						elseif temp_fmod == 3 then
							cfg_preset[preset_nr_act][15] = 3
						else
							cfg_preset[preset_nr_act][15] = 0
						end
					end
				elseif fms_txt == "FPLN SAVE FMT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][16] = 1
						else
							cfg_preset[preset_nr_act][16] = 0
						end
					end
				elseif fms_txt == "TRACK UP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][17] = 1
						else
							cfg_preset[preset_nr_act][17] = 0
						end
					end
				elseif fms_txt == "BARO IN HPA" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][18] = 1
						else
							cfg_preset[preset_nr_act][18] = 0
						end
					end
				elseif fms_txt == "MIN BARO RADIO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][19] = 1
						else
							cfg_preset[preset_nr_act][19] = 0
						end
					end
				elseif fms_txt == "VOR RADIAL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][20] = 1
						else
							cfg_preset[preset_nr_act][20] = 0
						end
					end
				elseif fms_txt == "WINDSH.EFFECTS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][21] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][21] = 2
						elseif temp_fmod == 3 then
							cfg_preset[preset_nr_act][21] = 3
						else
							cfg_preset[preset_nr_act][21] = 0
						end
					end
				elseif fms_txt == "WINDSH.REFLECT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][22] = 1
						else
							cfg_preset[preset_nr_act][22] = 0
						end
					end
				elseif fms_txt == "TIRE BLOWN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][23] = 1
						else
							cfg_preset[preset_nr_act][23] = 0
						end
					end
				elseif fms_txt == "ND RINGS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][24] = 1
						else
							cfg_preset[preset_nr_act][24] = 0
						end
					end
				elseif fms_txt == "GAUGES REFLECT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][25] = 1
						else
							cfg_preset[preset_nr_act][25] = 0
						end
					end
				elseif fms_txt == "FLIGHT CONTROL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][26] = 1
						else
							cfg_preset[preset_nr_act][26] = 0
						end
					end
				elseif fms_txt == "BRAKES TEMP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][27] = 1
						else
							cfg_preset[preset_nr_act][27] = 0
						end
					end
				elseif fms_txt == "THROTTLE LEVER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][28] = 1
						else
							cfg_preset[preset_nr_act][28] = 0
						end
					end
				elseif fms_txt == "SYNC PILOT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][29] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][29] = 1
						else
							cfg_preset[preset_nr_act][29] = 0
						end
					end
				elseif fms_txt == "PASSENGERS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][30] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][30] = 2
						else
							cfg_preset[preset_nr_act][30] = 0
						end
					end
				elseif fms_txt == "PITCH 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							cfg_preset[preset_nr_act][31] = temp_fmod / 100
						else
							cfg_preset[preset_nr_act][31] = 0
						end
					end
				elseif fms_txt == "ROLL 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							cfg_preset[preset_nr_act][32] = temp_fmod / 100
						else
							cfg_preset[preset_nr_act][32] = 0
						end
					end
				elseif fms_txt == "YAW 0 ZONE" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 30 then
							cfg_preset[preset_nr_act][33] = temp_fmod / 100
						else
							cfg_preset[preset_nr_act][33] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT UP" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][34] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][34] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 1" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][35] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][35] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 2" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][36] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][36] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 5" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][37] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][37] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 10" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][38] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][38] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 15" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][39] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][39] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 25" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][40] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][40] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 30" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][41] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][41] = 0
						end
					end
				elseif fms_txt == "FLAP DETENT 40" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][42] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][42] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR UP" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][43] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][43] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 1" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][44] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][44] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 2" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][45] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][45] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 5" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][46] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][46] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 10" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][47] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][47] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 15" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][48] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][48] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 25" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][49] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][49] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 30" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][50] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][50] = 0
						end
					end
				elseif fms_txt == "FLAP DT THR 40" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][51] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][51] = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE MAP" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][70] = temp_fmod
					else
						cfg_preset[preset_nr_act][70] = -1
					end
				elseif fms_txt == "FLAP MAPPED" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][71] = temp_fmod
					else
						cfg_preset[preset_nr_act][71] = -1
					end
				elseif fms_txt == "SPEEDBRAKE REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							cfg_preset[preset_nr_act][72] = 0
						else
							cfg_preset[preset_nr_act][72] = 1
						end
					end
				elseif fms_txt == "FLAP REVERSE" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							cfg_preset[preset_nr_act][73] = 0
						else
							cfg_preset[preset_nr_act][73] = 1
						end
					end
				elseif fms_txt == "SPEEDBRAKE NOI" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][74] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][74] = 2
						elseif temp_fmod == 3 then
							cfg_preset[preset_nr_act][74] = 3
						elseif temp_fmod == 4 then
							cfg_preset[preset_nr_act][74] = 4
						elseif temp_fmod == 5 then
							cfg_preset[preset_nr_act][74] = 5
						elseif temp_fmod == 6 then
							cfg_preset[preset_nr_act][74] = 6
						elseif temp_fmod == 7 then
							cfg_preset[preset_nr_act][74] = 7
						elseif temp_fmod == 8 then
							cfg_preset[preset_nr_act][74] = 8
						elseif temp_fmod == 9 then
							cfg_preset[preset_nr_act][74] = 9
						else
							cfg_preset[preset_nr_act][74] = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE ARM" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][75] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][75] = 0
						end
					end
				elseif fms_txt == "SPEEDBRAKE FLT" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][76] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][76] = 0
						end
					end
				elseif fms_txt == "LANDGEAR MAP" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][78] = temp_fmod
					else
						cfg_preset[preset_nr_act][78] = -1
					end
				elseif fms_txt == "LANDGEAR REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							cfg_preset[preset_nr_act][79] = 0
						else
							cfg_preset[preset_nr_act][79] = 1
						end
					end
				elseif fms_txt == "LANDGEAR OFF" then
					if temp_fmod ~= nil then
						if temp_fmod >= 0 and  temp_fmod <= 1000000 then
							cfg_preset[preset_nr_act][80] = temp_fmod / 1000000
						else
							cfg_preset[preset_nr_act][80] = 0
						end
					end
				elseif fms_txt == "MIXTURE1 MAP" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][81] = temp_fmod
					else
						cfg_preset[preset_nr_act][81] = -1
					end
				elseif fms_txt == "MIXTURE1 REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							cfg_preset[preset_nr_act][82] = 0
						else
							cfg_preset[preset_nr_act][82] = 1
						end
					end
				elseif fms_txt == "MIXTURE2 MAP" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][83] = temp_fmod
					else
						cfg_preset[preset_nr_act][83] = -1
					end
				elseif fms_txt == "MIXTURE2 REV" then
					if temp_fmod ~= nil then
						if temp_fmod == 0 then
							cfg_preset[preset_nr_act][84] = 0
						else
							cfg_preset[preset_nr_act][84] = 1
						end
					end
				elseif fms_txt == "IRS DING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][52] = 1
						else
							cfg_preset[preset_nr_act][52] = 0
						end
					end
				elseif fms_txt == "LONG STROBES" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][53] = 1
						else
							cfg_preset[preset_nr_act][53] = 0
						end
					end
				elseif fms_txt == "WINGS VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][54] = 1
						else
							cfg_preset[preset_nr_act][54] = 0
						end
					end
				elseif fms_txt == "SATCOM VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][55] = 1
						else
							cfg_preset[preset_nr_act][55] = 0
						end
					end
				elseif fms_txt == "PULSELITE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][56] = 1
						else
							cfg_preset[preset_nr_act][56] = 0
						end
					end
				elseif fms_txt == "LED LIGHTS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][57] = 1
						else
							cfg_preset[preset_nr_act][57] = 0
						end
					end
				elseif fms_txt == "HUD DISABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][58] = 1
						else
							cfg_preset[preset_nr_act][58] = 0
						end
					end
				elseif fms_txt == "YOKE DISCO AP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][59] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][59] = 2
						else
							cfg_preset[preset_nr_act][59] = 0
						end
					end
				elseif fms_txt == "LOCK THROTTLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][60] = 1
						else
							cfg_preset[preset_nr_act][60] = 0
						end
					end
				elseif fms_txt == "CIRC AOA RA" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][61] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][61] = 2
						else
							cfg_preset[preset_nr_act][61] = 0
						end
					end
				elseif fms_txt == "YOKE TO CENTER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][62] = 1
						else
							cfg_preset[preset_nr_act][62] = 0
						end
					end
				elseif fms_txt == "PARKBRAKE SET" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][63] = 1
						else
							cfg_preset[preset_nr_act][63] = 0
						end
					end
				elseif fms_txt == "STROBE COCKPIT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][64] = 1
						else
							cfg_preset[preset_nr_act][64] = 0
						end
					end
				elseif fms_txt == "PARTICLE RAIN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][65] = 1
						else
							cfg_preset[preset_nr_act][65] = 0
						end
					end
				elseif fms_txt == "SHOW FPS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][66] = 1
						else
							cfg_preset[preset_nr_act][66] = 0
						end
					end
				elseif fms_txt == "BRUNNER ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][67] = 1
						else
							cfg_preset[preset_nr_act][67] = 0
						end
					end
				elseif fms_txt == "COM DECIMAL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][68] = 1
						else
							cfg_preset[preset_nr_act][68] = 0
						end
					end
				elseif fms_txt == "SMART AP KNOB" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][69] = 1
						else
							cfg_preset[preset_nr_act][69] = 0
						end
					end
				elseif fms_txt == "FMS ALT/SPD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][77] = 1
						else
							cfg_preset[preset_nr_act][77] = 0
						end
					end
				elseif fms_txt == "MMR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][85] = 1
						else
							cfg_preset[preset_nr_act][85] = 0
						end
					end
				elseif fms_txt == "ASP TAT PROBE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][86] = 1
						else
							cfg_preset[preset_nr_act][86] = 0
						end
					end
				elseif fms_txt == "CALLOUT 10" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][87] = 1
						else
							cfg_preset[preset_nr_act][87] = 0
						end
					end
				elseif fms_txt == "CALLOUT 20" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][88] = 1
						else
							cfg_preset[preset_nr_act][88] = 0
						end
					end
				elseif fms_txt == "CALLOUT 30" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][89] = 1
						else
							cfg_preset[preset_nr_act][89] = 0
						end
					end
				elseif fms_txt == "CALLOUT 40" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][90] = 1
						else
							cfg_preset[preset_nr_act][90] = 0
						end
					end
				elseif fms_txt == "CALLOUT 50" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][91] = 1
						else
							cfg_preset[preset_nr_act][91] = 0
						end
					end
				elseif fms_txt == "CALLOUT 100" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][92] = 1
						else
							cfg_preset[preset_nr_act][92] = 0
						end
					end
				elseif fms_txt == "CALLOUT 200" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][93] = 1
						else
							cfg_preset[preset_nr_act][93] = 0
						end
					end
				elseif fms_txt == "CALLOUT 300" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][94] = 1
						else
							cfg_preset[preset_nr_act][94] = 0
						end
					end
				elseif fms_txt == "CALLOUT 400" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][95] = 1
						else
							cfg_preset[preset_nr_act][95] = 0
						end
					end
				elseif fms_txt == "CALLOUT 500" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][96] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][96] = 2
						else
							cfg_preset[preset_nr_act][96] = 0
						end
					end
				elseif fms_txt == "CALLOUT 1000" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][97] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][97] = 2
						else
							cfg_preset[preset_nr_act][97] = 0
						end
					end
				elseif fms_txt == "CALLOUT 1000S" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][98] = 1
						else
							cfg_preset[preset_nr_act][98] = 0
						end
					end
				elseif fms_txt == "CALLOUT 2500" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][99] = 1
						-- elseif temp_fmod == 2 then
							-- cfg_preset[preset_nr_act][99] = 2
						else
							cfg_preset[preset_nr_act][99] = 0
						end
					end
				elseif fms_txt == "FMC INPUT LAG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][100] = 1
						else
							cfg_preset[preset_nr_act][100] = 0
						end
					end
				elseif fms_txt == "XHSI ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][101] = 1
						else
							cfg_preset[preset_nr_act][101] = 0
						end
					end
				elseif fms_txt == "WHITE BUG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][102] = 1
						else
							cfg_preset[preset_nr_act][102] = 0
						end
					end
				elseif fms_txt == "GP CAB CONNECT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][103] = 1
						else
							cfg_preset[preset_nr_act][103] = 0
						end
					end
				elseif fms_txt == "CHRONO VARIANT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][104] = 1
						else
							cfg_preset[preset_nr_act][104] = 0
						end
					end
				elseif fms_txt == "CUST NAVDATA" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][105] = 1
						else
							cfg_preset[preset_nr_act][105] = 0
						end
					end
				elseif fms_txt == "PRIOR TX COM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][106] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][106] = 2
						else
							cfg_preset[preset_nr_act][106] = 0
						end
					end
				elseif fms_txt == "PFD FMC FLPSPD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][107] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][107] = 2
						else
							cfg_preset[preset_nr_act][107] = 0
						end
					end
				elseif fms_txt == "AP YOKE TLRNC" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][108] = 1
						else
							cfg_preset[preset_nr_act][108] = 0
						end
					end
				elseif fms_txt == "FD HELPER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][109] = 1
						else
							cfg_preset[preset_nr_act][109] = 0
						end
					end
				elseif fms_txt == "MMR COLOUR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][110] = 1
						else
							cfg_preset[preset_nr_act][110] = 0
						end
					end
				elseif fms_txt == "ND TCAS RANGE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][111] = 1
						else
							cfg_preset[preset_nr_act][111] = 0
						end
					end
				elseif fms_txt == "TAKEOFF HDG" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][112] = 1
						else
							cfg_preset[preset_nr_act][112] = 0
						end
					end
				elseif fms_txt == "INIT REF SMART" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][113] = 1
						else
							cfg_preset[preset_nr_act][113] = 0
						end
					end
				elseif fms_txt == "SYNC PILOT STD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][114] = 1
						else
							cfg_preset[preset_nr_act][114] = 0
						end
					end
				elseif fms_txt == "CFY ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][115] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][115] = 2
						else
							cfg_preset[preset_nr_act][115] = 0
						end
					end
				elseif fms_txt == "HIDE SEATS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][116] = 1
						else
							cfg_preset[preset_nr_act][116] = 0
						end
					end
				elseif fms_txt == "SAM ENABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][117] = 1
						else
							cfg_preset[preset_nr_act][117] = 0
						end
					end
				elseif fms_txt == "FO CALLOUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][118] = 1
						else
							cfg_preset[preset_nr_act][118] = 0
						end
					end
				elseif fms_txt == "VNAV ALT DIS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][119] = 1
						else
							cfg_preset[preset_nr_act][119] = 0
						end
					end
				elseif fms_txt == "VNAVALT_ALTHLD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][120] = 1
						else
							cfg_preset[preset_nr_act][120] = 0
						end
					end
				elseif fms_txt == "FD CAM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][121] = 1
						else
							cfg_preset[preset_nr_act][121] = 0
						end
					end
				elseif fms_txt == "MANIP_YOKE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][122] = 1
						else
							cfg_preset[preset_nr_act][122] = 0
						end
					end
				elseif fms_txt == "MANIP_THR" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][123] = 1
						else
							cfg_preset[preset_nr_act][123] = 0
						end
					end
				elseif fms_txt == "MANIP_FLAP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][124] = 1
						else
							cfg_preset[preset_nr_act][124] = 0
						end
					end
				elseif fms_txt == "MANIP_SPDBRK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][125] = 1
						else
							cfg_preset[preset_nr_act][125] = 0
						end
					end
				elseif fms_txt == "MANIP_TRIMWH" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][126] = 1
						else
							cfg_preset[preset_nr_act][126] = 0
						end
					end
				elseif fms_txt == "MANIP_PARKBRK" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][127] = 1
						else
							cfg_preset[preset_nr_act][127] = 0
						end
					end
				elseif fms_txt == "MANIP_STABTRIM" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][128] = 1
						else
							cfg_preset[preset_nr_act][128] = 0
						end
					end
				elseif fms_txt == "MANIP_FUELCUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][129] = 1
						else
							cfg_preset[preset_nr_act][129] = 0
						end
					end
				elseif fms_txt == "MANIP_HORNCUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][130] = 1
						else
							cfg_preset[preset_nr_act][130] = 0
						end
					end
				elseif fms_txt == "SELCAL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][131] = 1
						else
							cfg_preset[preset_nr_act][131] = 0
						end
					end
				elseif fms_txt == "ISFD_RMI" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][132] = math.floor(temp_fmod)
					end
				elseif fms_txt == "NEW_FUELCUTOFF" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][133] = math.floor(temp_fmod)
					end
				elseif fms_txt == "NPS" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][134] = math.floor(temp_fmod)
					end
				elseif fms_txt == "YAW_DAMPER" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][135] = math.floor(temp_fmod)
					end
				elseif fms_txt == "MUTE SWITCHES" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][136] = math.floor(temp_fmod)
					end
				elseif fms_txt == "CLOCK_VARIANT" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][137] = math.floor(temp_fmod)
					end
				elseif fms_txt == "OLD MCP" then
					if temp_fmod ~= nil then
						cfg_preset[preset_nr_act][138] = math.floor(temp_fmod)
					end
				elseif fms_txt == "PLUS HUNDRED" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][139] = 1
						else
							cfg_preset[preset_nr_act][139] = 0
						end
					end
				elseif fms_txt == "VORLOC_LNAV" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][140] = 1
						else
							cfg_preset[preset_nr_act][140] = 0
						end
					end
				elseif fms_txt == "FMC TYPE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][141] = 1
						else
							cfg_preset[preset_nr_act][141] = 0
						end
					end
				elseif fms_txt == "RAAS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][142] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][142] = 1
						else
							cfg_preset[preset_nr_act][142] = 0
						end
					end
				elseif fms_txt == "NOT_AIRSTAIRS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][143] = 1
						else
							cfg_preset[preset_nr_act][143] = 0
						end
					end
				elseif fms_txt == "ENG_START_AUTO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][144] = 1
						else
							cfg_preset[preset_nr_act][144] = 0
						end
					end
				elseif fms_txt == "PROBE_AUTO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][145] = 1
						else
							cfg_preset[preset_nr_act][145] = 0
						end
					end
				elseif fms_txt == "ROLLOUT" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][146] = 1
						else
							cfg_preset[preset_nr_act][146] = 0
						end
					end
				elseif fms_txt == "CT_DISABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][147] = 1
						else
							cfg_preset[preset_nr_act][147] = 0
						end
					end
				elseif fms_txt == "IAN_DISABLE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][148] = 1
						else
							cfg_preset[preset_nr_act][148] = 0
						end
					end
				elseif fms_txt == "AUTOSAVE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][149] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][149] = 2
						elseif temp_fmod == 3 then
							cfg_preset[preset_nr_act][149] = 3
						else
							cfg_preset[preset_nr_act][149] = 0
						end
					end
				elseif fms_txt == "FUEL PUMPS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][150] = 1
						else
							cfg_preset[preset_nr_act][150] = 0
						end
					end
				elseif fms_txt == "CAT III" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][151] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][151] = 2
						elseif temp_fmod == 3 then
							cfg_preset[preset_nr_act][151] = 3
						elseif temp_fmod == 4 then
							cfg_preset[preset_nr_act][151] = 4
						elseif temp_fmod == 5 then
							cfg_preset[preset_nr_act][151] = 5
						else
							cfg_preset[preset_nr_act][151] = 0
						end
					end
				elseif fms_txt == "BRAKES" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][152] = 1
						else
							cfg_preset[preset_nr_act][152] = 0
						end
					end
				elseif fms_txt == "FF EICAS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][153] = 1
						else
							cfg_preset[preset_nr_act][153] = 0
						end
					end
				elseif fms_txt == "DIG_PRESS_PNL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][154] = 1
						else
							cfg_preset[preset_nr_act][154] = 0
						end
					end
				elseif fms_txt == "NO RTE2" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][155] = 1
						else
							cfg_preset[preset_nr_act][155] = 0
						end
					end
				elseif fms_txt == "GS_ON_PFD" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][156] = 1
						else
							cfg_preset[preset_nr_act][156] = 0
						end
					end
				elseif fms_txt == "ILS_GLS_ANN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][157] = 1
						else
							cfg_preset[preset_nr_act][157] = 0
						end
					end
				elseif fms_txt == "NO_SMOKING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][158] = 1
						elseif temp_fmod == 2 then
							cfg_preset[preset_nr_act][158] = 2
						else
							cfg_preset[preset_nr_act][158] = 0
						end
					end
				elseif fms_txt == "NO_VCR_SW" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][159] = 1
						else
							cfg_preset[preset_nr_act][159] = 0
						end
					end
				elseif fms_txt == "GW_ENTRY" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][160] = 1
						else
							cfg_preset[preset_nr_act][160] = 0
						end
					end
				elseif fms_txt == "MCP_SPD_INTV" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][161] = 1
						else
							cfg_preset[preset_nr_act][161] = 0
						end
					end
				elseif fms_txt == "MCP_ALT_INTV" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][162] = 1
						else
							cfg_preset[preset_nr_act][162] = 0
						end
					end
				elseif fms_txt == "RMI" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][163] = 1
						else
							cfg_preset[preset_nr_act][163] = 0
						end
					end
				elseif fms_txt == "SFP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][164] = 1
						else
							cfg_preset[preset_nr_act][164] = 0
						end
					end
				elseif fms_txt == "AT_WAKEUP" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][165] = 1
						else
							cfg_preset[preset_nr_act][165] = 0
						end
					end
				elseif fms_txt == "PLAN_FUEL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][166] = 1
						else
							cfg_preset[preset_nr_act][166] = 0
						end
					end
				elseif fms_txt == "OIL_QTY" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][167] = 1
						else
							cfg_preset[preset_nr_act][167] = 0
						end
					end
				elseif fms_txt == "GUIDED_COVERS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][168] = 1
						else
							cfg_preset[preset_nr_act][168] = 0
						end
					end
				elseif fms_txt == "GUARDED_COVERS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							cfg_preset[preset_nr_act][168] = 1
						else
							cfg_preset[preset_nr_act][168] = 0
						end
					end
				end
			end
			fms_line = file_navdata:read()
		end
		file_navdata:close()
	end
end

function B738_set_cfg_config(fmod_nr)
	
	local ff = fmod_nr
	
	units					 		= cfg_preset[ff][2]
	B738DR_align_time 				= cfg_preset[ff][3]
	B738DR_refuel_time 				= cfg_preset[ff][4]
	B738DR_detail_cl	 			= cfg_preset[ff][5]
	simDR_hide_yoke 				= cfg_preset[ff][6]
	B738DR_pause_td		 			= cfg_preset[ff][7]
	B738DR_toe_brakes_ovr			= cfg_preset[ff][8]
	B738DR_lock_idle_thrust			= cfg_preset[ff][9]
	B738DR_engine_no_running_state	= cfg_preset[ff][10]
	B738DR_chock_on_startup			= cfg_preset[ff][11]
	B738DR_parkbrake_remove_chock	= cfg_preset[ff][12]
	B738DR_throttle_noise 			= cfg_preset[ff][13]
	B738DR_fuelgauge 				= cfg_preset[ff][14]
	B738DR_nosewheel			 	= cfg_preset[ff][15]
	B738DR_fpln_format		 		= cfg_preset[ff][16]
	B738DR_track_up		 			= cfg_preset[ff][17]
	B738DR_baro_in_hpa 				= cfg_preset[ff][18]
	B738DR_min_baro_radio			= cfg_preset[ff][19]
	B738DR_vor_radial				= cfg_preset[ff][20]
	B738DR_kill_windshield			= cfg_preset[ff][21]
	B738DR_hide_glass 				= cfg_preset[ff][22]
	B738DR_tire_blown		 		= cfg_preset[ff][23]
	B738DR_nd_rings					= cfg_preset[ff][24]
	B738DR_hide_gauges_reflection	= cfg_preset[ff][25]
	B738DR_flight_ctrl				= cfg_preset[ff][26]
	B738DR_brake_temp				= cfg_preset[ff][27]
	B738DR_hardware_thr_lever		= cfg_preset[ff][28]
	B738DR_sync_pilot				= cfg_preset[ff][29]
	B738DR_passengers				= cfg_preset[ff][30]
	simDR_pitch_nz					= cfg_preset[ff][31]
	simDR_roll_nz					= cfg_preset[ff][32]
	simDR_yaw_nz					= cfg_preset[ff][33]
	B738DR_flap_detents[0] 			= cfg_preset[ff][34]
	B738DR_flap_detents[1] 			= cfg_preset[ff][35]
	B738DR_flap_detents[2] 			= cfg_preset[ff][36]
	B738DR_flap_detents[3] 			= cfg_preset[ff][37]
	B738DR_flap_detents[4] 			= cfg_preset[ff][38]
	B738DR_flap_detents[5] 			= cfg_preset[ff][39]
	B738DR_flap_detents[6] 			= cfg_preset[ff][40]
	B738DR_flap_detents[7] 			= cfg_preset[ff][41]
	B738DR_flap_detents[8] 			= cfg_preset[ff][42]
	B738DR_flap_detents_thr[0]		= cfg_preset[ff][43]
	B738DR_flap_detents_thr[1]		= cfg_preset[ff][44]
	B738DR_flap_detents_thr[2]		= cfg_preset[ff][45]
	B738DR_flap_detents_thr[3]		= cfg_preset[ff][46]
	B738DR_flap_detents_thr[4]		= cfg_preset[ff][47]
	B738DR_flap_detents_thr[5]		= cfg_preset[ff][48]
	B738DR_flap_detents_thr[6]		= cfg_preset[ff][49]
	B738DR_flap_detents_thr[7]		= cfg_preset[ff][50]
	B738DR_flap_detents_thr[8]		= cfg_preset[ff][51]
	B738DR_irs_ding					= cfg_preset[ff][52]
	B738DR_long_led_strobes			= cfg_preset[ff][53]
	B738DR_wings_variant			= cfg_preset[ff][54]
	B738DR_satcom_variant			= cfg_preset[ff][55]
	B738DR_pulse_lite				= cfg_preset[ff][56]
	B738DR_led_lights				= cfg_preset[ff][57]
	B738DR_hud_disable				= cfg_preset[ff][58]
	B738DR_yoke_disco_ap			= cfg_preset[ff][59]
	B738DR_lock_thrust				= cfg_preset[ff][60]
	B738DR_aoa_ra					= cfg_preset[ff][61]
	B738DR_yoke_to_center			= cfg_preset[ff][62]
	B738DR_parkbrake_set			= cfg_preset[ff][63]
	B738DR_strobe_cockpit			= cfg_preset[ff][64]
	if simDR_int_ver < 120000 then
		B738DR_particle_rain			= cfg_preset[ff][65]
	end
	B738DR_fps_enable				= cfg_preset[ff][66]
	B738DR_brunner_enable			= cfg_preset[ff][67]
	B738DR_com_decimal				= cfg_preset[ff][68]
	B738DR_smart_ap_knob			= cfg_preset[ff][69]
	
	B738DR_cust_axis_speedbrake_mapped = cfg_preset[ff][70]
	B738DR_cust_axis_flaps_mapped = cfg_preset[ff][71]
	B738DR_cust_axis_speedbrake_reverse = cfg_preset[ff][72]
	B738DR_cust_axis_flaps_reverse = cfg_preset[ff][73]
	B738DR_cust_axis_speedbrake_noise = cfg_preset[ff][74]
	B738DR_cust_axis_speedbrake_arm = cfg_preset[ff][75]
	B738DR_cust_axis_speedbrake_flight = cfg_preset[ff][76]
	
	B738DR_restrict_fms				= cfg_preset[ff][77]
	
	B738DR_cust_axis_landgear_mapped = cfg_preset[ff][78]
	B738DR_cust_axis_landgear_reverse = cfg_preset[ff][79]
	B738DR_cust_axis_landgear_off = cfg_preset[ff][80]
	B738DR_cust_axis_mixture1_mapped = cfg_preset[ff][81]
	B738DR_cust_axis_mixture1_reverse = cfg_preset[ff][82]
	B738DR_cust_axis_mixture2_mapped = cfg_preset[ff][83]
	B738DR_cust_axis_mixture2_reverse = cfg_preset[ff][84]
	
	B738DR_mmr = cfg_preset[ff][85]
	B738DR_aspirated_probe = cfg_preset[ff][86]
	
	-- [87] .. [99]
	for uuu = 0, 14 do
		B738DR_ra_callout_disable[uuu] = cfg_preset[ff][87+uuu]
	end
	
	B738DR_fmc_input_lag = cfg_preset[ff][100]
	B738DR_xhsi_enable = cfg_preset[ff][101]
	B738DR_white_bug = cfg_preset[ff][102]
	B738DR_gp_cable_connect = cfg_preset[ff][103]
	B738DR_chrono_variant = cfg_preset[ff][104]
	B738DR_cust_nav_data = cfg_preset[ff][105]
	B738DR_prior_tx_com = cfg_preset[ff][106]
	B738DR_pfd_fmc_flap_spd = cfg_preset[ff][107]
	B738DR_ap_yoke_tolerance = cfg_preset[ff][108]
	B738DR_fd_helper = cfg_preset[ff][109]
	B738DR_mmr_colour = cfg_preset[ff][110]
	B738DR_nd_tcas_range = cfg_preset[ff][111]
	B738DR_takeoff_hdg = cfg_preset[ff][112]
	B738DR_init_ref_smart = cfg_preset[ff][113]
	B738DR_sync_pilot_isdf = cfg_preset[ff][114]
	B738DR_cfy_enable = cfg_preset[ff][115]
	B738DR_hide_seats = cfg_preset[ff][116]
	B738DR_sam_enable = cfg_preset[ff][117]
	B738DR_disable_fo_callout = cfg_preset[ff][118]
	B738DR_vnav_alt_disable = cfg_preset[ff][119]
	B738DR_vnavalt_althld = cfg_preset[ff][120]
	B738DR_fd_cam_disable = cfg_preset[ff][121]
	B738DR_manip_yoke = cfg_preset[ff][122]
	B738DR_manip_throttle = cfg_preset[ff][123]
	B738DR_manip_flaps = cfg_preset[ff][124]
	B738DR_manip_speedbrake = cfg_preset[ff][125]
	B738DR_manip_trimwheel = cfg_preset[ff][126]
	B738DR_manip_park_brake = cfg_preset[ff][127]
	B738DR_manip_stab_trim = cfg_preset[ff][128]
	B738DR_manip_fuelcutout = cfg_preset[ff][129]
	B738DR_manip_horn_cutout = cfg_preset[ff][130]
	B738DR_selcal_disable = cfg_preset[ff][131]
	B738DR_isfd_rmi = cfg_preset[ff][132]
	B738DR_new_fuel_cutoff = cfg_preset[ff][133]
	B738DR_nps = cfg_preset[ff][134]
	B738DR_yaw_damper = cfg_preset[ff][135]
	B738DR_mute_switches = cfg_preset[ff][136]
	B738DR_clock_variant = cfg_preset[ff][137]
	B738DR_old_mcp = cfg_preset[ff][138]
	B738DR_plus_hundred = cfg_preset[ff][139]
	B738DR_vorloc_lnav = cfg_preset[ff][140]
	B738DR_fmc_type = cfg_preset[ff][141]
	B738DR_raas = cfg_preset[ff][142]
	B738DR_not_airstairs = cfg_preset[ff][143]
	B738DR_engine_start_auto = cfg_preset[ff][144]
	B738DR_probe_heat_auto = cfg_preset[ff][145]
	B738DR_autoland_install = cfg_preset[ff][146]
	B738DR_nps_disable = cfg_preset[ff][147]
	B738DR_ian_not_install = cfg_preset[ff][148]
	B738DR_autosave = cfg_preset[ff][149]
	B738DR_fuel_pumps_off_placard = cfg_preset[ff][150]
	B738DR_cat3_placard = cfg_preset[ff][151]
	B738DR_brakes_placard = cfg_preset[ff][152]
	B738DR_ff_eicas = cfg_preset[ff][153]
	B738DR_digital_press_panel = cfg_preset[ff][154]
	B738DR_no_rte2 = cfg_preset[ff][155]
	B738DR_gs_on_pfd = cfg_preset[ff][156]
	B738DR_ils_gls_annun = cfg_preset[ff][157]
	B738DR_no_smoking = cfg_preset[ff][158]
	B738DR_no_vcr_switch = cfg_preset[ff][159]
	B738DR_gw_entry = cfg_preset[ff][160]
	B738DR_spd_intv_but = cfg_preset[ff][161]
	B738DR_alt_intv_but = cfg_preset[ff][162]
	B738DR_rmi = cfg_preset[ff][163]
	B738DR_sfp = cfg_preset[ff][164]
	B738DR_at_wakeup = cfg_preset[ff][165]
	B738DR_plan_fuel = cfg_preset[ff][166]
	B738DR_oil_qty_pct_qts = cfg_preset[ff][167]
	B738DR_guarded_covers = cfg_preset[ff][168]
	B738DR_ra_radio_altimeter = cfg_preset[ff][169]
	B738DR_ra_decision_height = cfg_preset[ff][170]
	B738DR_ra_500_1000_baro = cfg_preset[ff][171]
end

function B738_mod_cfg_config(fmod_nr)
	
	local ff = fmod_nr
	
	cfg_preset[ff][2] 	= units
	cfg_preset[ff][3] 	= B738DR_align_time
	cfg_preset[ff][4] 	= B738DR_refuel_time
	cfg_preset[ff][5] 	= B738DR_detail_cl
	cfg_preset[ff][6] 	= simDR_hide_yoke
	cfg_preset[ff][7] 	= B738DR_pause_td
	cfg_preset[ff][8] 	= B738DR_toe_brakes_ovr
	cfg_preset[ff][9] 	= B738DR_lock_idle_thrust
	cfg_preset[ff][10] 	= B738DR_engine_no_running_state
	cfg_preset[ff][11] 	= B738DR_chock_on_startup
	cfg_preset[ff][12] 	= B738DR_parkbrake_remove_chock
	cfg_preset[ff][13] 	= B738DR_throttle_noise
	cfg_preset[ff][14] 	= B738DR_fuelgauge
	cfg_preset[ff][15] 	= B738DR_nosewheel
	cfg_preset[ff][16] 	= B738DR_fpln_format
	cfg_preset[ff][17] 	= B738DR_track_up
	cfg_preset[ff][18] 	= B738DR_baro_in_hpa
	cfg_preset[ff][19] 	= B738DR_min_baro_radio
	cfg_preset[ff][20] 	= B738DR_vor_radial
	cfg_preset[ff][21] 	= B738DR_kill_windshield
	cfg_preset[ff][22] 	= B738DR_hide_glass
	cfg_preset[ff][23] 	= B738DR_tire_blown
	cfg_preset[ff][24] 	= B738DR_nd_rings
	cfg_preset[ff][25] 	= B738DR_hide_gauges_reflection
	cfg_preset[ff][26] 	= B738DR_flight_ctrl
	cfg_preset[ff][27] 	= B738DR_brake_temp
	cfg_preset[ff][28] 	= B738DR_hardware_thr_lever
	cfg_preset[ff][29] 	= B738DR_sync_pilot
	cfg_preset[ff][30] 	= B738DR_passengers
	cfg_preset[ff][31] 	= simDR_pitch_nz
	cfg_preset[ff][32] 	= simDR_roll_nz
	cfg_preset[ff][33] 	= simDR_yaw_nz
	cfg_preset[ff][34] 	= B738DR_flap_detents[0]
	cfg_preset[ff][35] 	= B738DR_flap_detents[1]
	cfg_preset[ff][36] 	= B738DR_flap_detents[2]
	cfg_preset[ff][37] 	= B738DR_flap_detents[3]
	cfg_preset[ff][38] 	= B738DR_flap_detents[4]
	cfg_preset[ff][39] 	= B738DR_flap_detents[5]
	cfg_preset[ff][40] 	= B738DR_flap_detents[6]
	cfg_preset[ff][41] 	= B738DR_flap_detents[7]
	cfg_preset[ff][42] 	= B738DR_flap_detents[8]
	cfg_preset[ff][43] 	= B738DR_flap_detents_thr[0]
	cfg_preset[ff][44] 	= B738DR_flap_detents_thr[1]
	cfg_preset[ff][45] 	= B738DR_flap_detents_thr[2]
	cfg_preset[ff][46] 	= B738DR_flap_detents_thr[3]
	cfg_preset[ff][47] 	= B738DR_flap_detents_thr[4]
	cfg_preset[ff][48] 	= B738DR_flap_detents_thr[5]
	cfg_preset[ff][49] 	= B738DR_flap_detents_thr[6]
	cfg_preset[ff][50] 	= B738DR_flap_detents_thr[7]
	cfg_preset[ff][51] 	= B738DR_flap_detents_thr[8]
	cfg_preset[ff][52] 	= B738DR_irs_ding
	cfg_preset[ff][53] 	= B738DR_long_led_strobes
	cfg_preset[ff][54] 	= B738DR_wings_variant
	cfg_preset[ff][55] 	= B738DR_satcom_variant
	cfg_preset[ff][56] 	= B738DR_pulse_lite
	cfg_preset[ff][57] 	= B738DR_led_lights
	cfg_preset[ff][58] 	= B738DR_hud_disable
	cfg_preset[ff][59] 	= B738DR_yoke_disco_ap
	cfg_preset[ff][60] 	= B738DR_lock_thrust
	cfg_preset[ff][61] 	= B738DR_aoa_ra
	cfg_preset[ff][62] 	= B738DR_yoke_to_center
	cfg_preset[ff][63] 	= B738DR_parkbrake_set
	cfg_preset[ff][64] 	= B738DR_strobe_cockpit
	cfg_preset[ff][65] 	= B738DR_particle_rain
	cfg_preset[ff][66] 	= B738DR_fps_enable
	cfg_preset[ff][67] 	= B738DR_brunner_enable
	cfg_preset[ff][68] 	= B738DR_com_decimal
	cfg_preset[ff][69] 	= B738DR_smart_ap_knob
	
	cfg_preset[ff][70] 	= B738DR_cust_axis_speedbrake_mapped
	cfg_preset[ff][71] 	= B738DR_cust_axis_flaps_mapped
	cfg_preset[ff][72] 	= B738DR_cust_axis_speedbrake_reverse
	cfg_preset[ff][73] 	= B738DR_cust_axis_flaps_reverse
	cfg_preset[ff][74] 	= B738DR_cust_axis_speedbrake_noise
	cfg_preset[ff][75] 	= B738DR_cust_axis_speedbrake_arm
	cfg_preset[ff][76] 	= B738DR_cust_axis_speedbrake_flight
	cfg_preset[ff][77] 	= B738DR_restrict_fms
	
	cfg_preset[ff][78] 	= B738DR_cust_axis_landgear_mapped
	cfg_preset[ff][79] 	= B738DR_cust_axis_landgear_reverse
	cfg_preset[ff][80] 	= B738DR_cust_axis_landgear_off
	cfg_preset[ff][81] 	= B738DR_cust_axis_mixture1_mapped
	cfg_preset[ff][82] 	= B738DR_cust_axis_mixture1_reverse
	cfg_preset[ff][83] 	= B738DR_cust_axis_mixture2_mapped
	cfg_preset[ff][84] 	= B738DR_cust_axis_mixture2_reverse
	
	cfg_preset[ff][85] 	= B738DR_mmr
	cfg_preset[ff][86] 	= B738DR_aspirated_probe
	
	-- [87] .. [99]
	for uuu = 0, 14 do
		cfg_preset[ff][87+uuu] 	= B738DR_ra_callout_disable[uuu]
	end
	
	cfg_preset[ff][100] = B738DR_fmc_input_lag
	cfg_preset[ff][101] = B738DR_xhsi_enable
	cfg_preset[ff][102] = B738DR_white_bug
	cfg_preset[ff][103] = B738DR_gp_cable_connect
	cfg_preset[ff][104] = B738DR_chrono_variant
	cfg_preset[ff][105] = B738DR_cust_nav_data
	cfg_preset[ff][106] = B738DR_prior_tx_com
	cfg_preset[ff][107] = B738DR_pfd_fmc_flap_spd
	cfg_preset[ff][108] = B738DR_ap_yoke_tolerance
	cfg_preset[ff][109] = B738DR_fd_helper
	cfg_preset[ff][110] = B738DR_mmr_colour
	cfg_preset[ff][111] = B738DR_nd_tcas_range
	cfg_preset[ff][112] = B738DR_takeoff_hdg
	cfg_preset[ff][113] = B738DR_init_ref_smart
	cfg_preset[ff][114] = B738DR_sync_pilot_isdf
	cfg_preset[ff][115] = B738DR_cfy_enable
	cfg_preset[ff][116] = B738DR_hide_seats
	cfg_preset[ff][117] = B738DR_sam_enable
	cfg_preset[ff][118] = B738DR_disable_fo_callout
	cfg_preset[ff][119] = B738DR_vnav_alt_disable
	cfg_preset[ff][120] = B738DR_vnavalt_althld
	cfg_preset[ff][121] = B738DR_fd_cam_disable
	cfg_preset[ff][122] = B738DR_manip_yoke
	cfg_preset[ff][123] = B738DR_manip_throttle
	cfg_preset[ff][124] = B738DR_manip_flaps
	cfg_preset[ff][125] = B738DR_manip_speedbrake
	cfg_preset[ff][126] = B738DR_manip_trimwheel
	cfg_preset[ff][127] = B738DR_manip_park_brake
	cfg_preset[ff][128] = B738DR_manip_stab_trim
	cfg_preset[ff][129] = B738DR_manip_fuelcutout
	cfg_preset[ff][130] = B738DR_manip_horn_cutout
	cfg_preset[ff][131] = B738DR_selcal_disable
	cfg_preset[ff][132] = B738DR_isfd_rmi
	cfg_preset[ff][133] = B738DR_new_fuel_cutoff
	cfg_preset[ff][134] = B738DR_nps
	cfg_preset[ff][135] = B738DR_yaw_damper
	cfg_preset[ff][136] = B738DR_mute_switches
	cfg_preset[ff][137] = B738DR_clock_variant
	cfg_preset[ff][138] = B738DR_old_mcp
	cfg_preset[ff][139] = B738DR_plus_hundred
	cfg_preset[ff][140] = B738DR_vorloc_lnav
	cfg_preset[ff][141] = B738DR_fmc_type
	cfg_preset[ff][142] = B738DR_raas
	cfg_preset[ff][143] = B738DR_not_airstairs
	cfg_preset[ff][144] = B738DR_engine_start_auto
	cfg_preset[ff][145] = B738DR_probe_heat_auto
	cfg_preset[ff][146] = B738DR_autoland_install
	cfg_preset[ff][147] = B738DR_nps_disable
	cfg_preset[ff][148] = B738DR_ian_not_install
	cfg_preset[ff][149] = B738DR_autosave
	cfg_preset[ff][150] = B738DR_fuel_pumps_off_placard
	cfg_preset[ff][151] = B738DR_cat3_placard
	cfg_preset[ff][152] = B738DR_brakes_placard
	cfg_preset[ff][153] = B738DR_ff_eicas
	cfg_preset[ff][154] = B738DR_digital_press_panel
	cfg_preset[ff][155] = B738DR_no_rte2
	cfg_preset[ff][156] = B738DR_gs_on_pfd
	cfg_preset[ff][157] = B738DR_ils_gls_annun
	cfg_preset[ff][158] = B738DR_no_smoking
	cfg_preset[ff][159] = B738DR_no_vcr_switch
	cfg_preset[ff][160] = B738DR_gw_entry
	cfg_preset[ff][161] = B738DR_spd_intv_but
	cfg_preset[ff][162] = B738DR_alt_intv_but
	cfg_preset[ff][163] = B738DR_rmi
	cfg_preset[ff][164] = B738DR_sfp
	cfg_preset[ff][165] = B738DR_at_wakeup
	cfg_preset[ff][166] = B738DR_plan_fuel
	cfg_preset[ff][167] = B738DR_oil_qty_pct_qts
	cfg_preset[ff][168] = B738DR_guarded_covers
	cfg_preset[ff][169] = B738DR_ra_radio_altimeter
	cfg_preset[ff][170] = B738DR_ra_decision_height
	cfg_preset[ff][171] = B738DR_ra_500_1000_baro
end

function B738_save_cfg_config(xfile_path_cfg)

	local fms_line = ""
	local ff = 0
	
	if xfile_path_cfg == "" then
		file_name = "Output/preferences/" .. FILE_NAME_ACF_CFG
	else
		file_name = xfile_path_cfg .. FILE_NAME_ACF_CFG
	end
	file_navdata = io.open(file_name, "w")
	if file_navdata ~= nil then
		-- OTHERS
		fms_line = "*** B737-800X ZIBO MOD ***\n"
		file_navdata:write(fms_line)
		-- ACF CFG PRESETS
		fms_line = "\n*** ACF CFG Presets ***\n"
		file_navdata:write(fms_line)
		for ff = 1, 5 do
			fms_line = "\nPRESET NR " .. string.format("%1d", ff) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PRESET NAME    =" .. cfg_preset[ff][1] .. "\n"
			file_navdata:write(fms_line)
			
			fms_line = "UNITS          = " .. string.format("%2d", cfg_preset[ff][2]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TRACK UP       = " .. string.format("%2d", cfg_preset[ff][17]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "BARO IN HPA    = " .. string.format("%2d", cfg_preset[ff][18]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MIN BARO RADIO = " .. string.format("%2d", cfg_preset[ff][19]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "VOR RADIAL     = " .. string.format("%2d", cfg_preset[ff][20]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ND RINGS       = " .. string.format("%2d", cfg_preset[ff][24]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "IRS DING       = " .. string.format("%2d", cfg_preset[ff][52]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LONG STROBES   = " .. string.format("%2d", cfg_preset[ff][53]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "WINGS VARIANT  = " .. string.format("%2d", cfg_preset[ff][54]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SATCOM VARIANT = " .. string.format("%2d", cfg_preset[ff][55]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PULSELITE      = " .. string.format("%2d", cfg_preset[ff][56]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LED LIGHTS     = " .. string.format("%2d", cfg_preset[ff][57]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "HUD DISABLE    = " .. string.format("%2d", cfg_preset[ff][58]) .. "\n"
			file_navdata:write(fms_line)
			
			if xfile_path_cfg == "" then
				fms_line = "ALIGN TIME     = " .. string.format("%2d", cfg_preset[ff][3]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "REFUEL TIME    = " .. string.format("%2d", cfg_preset[ff][4]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "CHECKLIST      = " .. string.format("%2d", cfg_preset[ff][5]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "HIDE YOKE      = " .. string.format("%2d", cfg_preset[ff][6]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "PAUSE AT T/D   = " .. string.format("%2d", cfg_preset[ff][7]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "TOE BRAKE AXIS = " .. string.format("%2d", cfg_preset[ff][8]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "LOCK IDLE THR  = " .. string.format("%2d", cfg_preset[ff][9]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "ENG NO RUNNING = " .. string.format("%2d", cfg_preset[ff][10]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "CHOCKS STATE   = " .. string.format("%2d", cfg_preset[ff][11]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "BRAKE REM CHOCK= " .. string.format("%2d", cfg_preset[ff][12]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "THROTTLE NOISE = " .. string.format("%2d", cfg_preset[ff][13]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FUEL GAUGE     = " .. string.format("%2d", cfg_preset[ff][14]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "NOSEWHEEL AXIS = " .. string.format("%2d", cfg_preset[ff][15]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FPLN SAVE FMT  = " .. string.format("%2d", cfg_preset[ff][16]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "WINDSH.EFFECTS = " .. string.format("%2d", cfg_preset[ff][21]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "WINDSH.REFLECT = " .. string.format("%2d", cfg_preset[ff][22]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "TIRE BLOWN     = " .. string.format("%2d", cfg_preset[ff][23]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "GAUGES REFLECT = " .. string.format("%2d", cfg_preset[ff][25]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLIGHT CONTROL = " .. string.format("%2d", cfg_preset[ff][26]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "BRAKES TEMP    = " .. string.format("%2d", cfg_preset[ff][27]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "THROTTLE LEVER = " .. string.format("%2d", cfg_preset[ff][28]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "SYNC PILOT     = " .. string.format("%2d", cfg_preset[ff][29]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "PASSENGERS     = " .. string.format("%2d", cfg_preset[ff][30]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "PITCH 0 ZONE   = " .. string.format("%2d", cfg_preset[ff][31] * 100) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "ROLL 0 ZONE    = " .. string.format("%2d", cfg_preset[ff][32] * 100) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "YAW 0 ZONE     = " .. string.format("%2d", cfg_preset[ff][33] * 100) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT UP = " .. string.format("%7d", cfg_preset[ff][34] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 1  = " .. string.format("%7d", cfg_preset[ff][35] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 2  = " .. string.format("%7d", cfg_preset[ff][36] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 5  = " .. string.format("%7d", cfg_preset[ff][37] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 10 = " .. string.format("%7d", cfg_preset[ff][38] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 15 = " .. string.format("%7d", cfg_preset[ff][39] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 25 = " .. string.format("%7d", cfg_preset[ff][40] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 30 = " .. string.format("%7d", cfg_preset[ff][41] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DETENT 40 = " .. string.format("%7d", cfg_preset[ff][42] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR UP = " .. string.format("%7d", cfg_preset[ff][43] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 1  = " .. string.format("%7d", cfg_preset[ff][44] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 2  = " .. string.format("%7d", cfg_preset[ff][45] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 5  = " .. string.format("%7d", cfg_preset[ff][46] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 10 = " .. string.format("%7d", cfg_preset[ff][47] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 15 = " .. string.format("%7d", cfg_preset[ff][48] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 25 = " .. string.format("%7d", cfg_preset[ff][49] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 30 = " .. string.format("%7d", cfg_preset[ff][50] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP DT THR 40 = " .. string.format("%7d", cfg_preset[ff][51] * 1000000) .. "\n"
				file_navdata:write(fms_line)
				
				fms_line = "SPEEDBRAKE MAP = " .. string.format("%3d", B738DR_cust_axis_speedbrake_mapped) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP MAPPED    = " .. string.format("%3d", B738DR_cust_axis_flaps_mapped) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "SPEEDBRAKE REV = " .. string.format("%2d", B738DR_cust_axis_speedbrake_reverse) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "FLAP REVERSE   = " .. string.format("%2d", B738DR_cust_axis_flaps_reverse) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "SPEEDBRAKE NOI = " .. string.format("%2d", B738DR_cust_axis_speedbrake_noise) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "SPEEDBRAKE ARM = " .. string.format("%7d", B738DR_cust_axis_speedbrake_arm * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "SPEEDBRAKE FLT = " .. string.format("%7d", B738DR_cust_axis_speedbrake_flight * 1000000) .. "\n"
				file_navdata:write(fms_line)
				
				fms_line = "LANDGEAR MAP   = " .. string.format("%3d", B738DR_cust_axis_landgear_mapped) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "LANDGEAR REV   = " .. string.format("%2d", B738DR_cust_axis_landgear_reverse) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "LANDGEAR OFF   = " .. string.format("%7d", B738DR_cust_axis_landgear_off * 1000000) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "MIXTURE1 MAP   = " .. string.format("%3d", B738DR_cust_axis_mixture1_mapped) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "MIXTURE1 REV   = " .. string.format("%2d", B738DR_cust_axis_mixture1_reverse) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "MIXTURE2 MAP   = " .. string.format("%3d", B738DR_cust_axis_mixture2_mapped) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "MIXTURE2 REV   = " .. string.format("%2d", B738DR_cust_axis_mixture2_reverse) .. "\n"
				file_navdata:write(fms_line)
				
				fms_line = "YOKE DISCO AP  = " .. string.format("%2d", cfg_preset[ff][59]) .. "\n"
				file_navdata:write(fms_line)
				fms_line = "LOCK THROTTLE  = " .. string.format("%2d", cfg_preset[ff][60]) .. "\n"
				file_navdata:write(fms_line)
			end
		end
		file_navdata:close()
	end

end




----------------------------------------------------------------------
function B738_load_fmod_config(xfile_path_cfg)
	
	local fms_line = ""
	local temp_fmod = 0
	local ff = 0
	
	fmod_preset = {}
	fmod_preset[1] = {}
	fmod_preset[2] = {}
	fmod_preset[3] = {}
	fmod_preset[4] = {}
	fmod_preset[5] = {}
	fmod_preset[1][1] = "PRESET 1"
	fmod_preset[2][1] = "PRESET 2"
	fmod_preset[3][1] = "PRESET 3"
	fmod_preset[4][1] = "PRESET 4"
	fmod_preset[5][1] = "PRESET 5"
	for temp_fmod = 1, 5 do
		fmod_preset[temp_fmod][2] = 1		-- PAX BOARDING
		fmod_preset[temp_fmod][3] = 0		-- GYRO VIBRATORS
		fmod_preset[temp_fmod][4] = 1		-- CREW ANNOUN
		fmod_preset[temp_fmod][5] = 1		-- CHATTER PASS
		fmod_preset[temp_fmod][6] = 1		-- AIRPORT SET
		fmod_preset[temp_fmod][7] = 0		-- INTERNAL VOL
		fmod_preset[temp_fmod][8] = 1		-- INT ENG SND
		fmod_preset[temp_fmod][9] = 1		-- INT ENG START
		fmod_preset[temp_fmod][10] = 1		-- AC FANS
		fmod_preset[temp_fmod][11] = 1		-- GYRO VOL
		fmod_preset[temp_fmod][12] = 1		-- ROLL VOL
		fmod_preset[temp_fmod][13] = 1		-- BUMP INTENS
		fmod_preset[temp_fmod][14] = 5		-- PAX VOLUME
		fmod_preset[temp_fmod][15] = 5		-- PAX APPLAUSE
		fmod_preset[temp_fmod][16] = 5		-- INT WIND
		fmod_preset[temp_fmod][17] = 0		-- MUTE TRIM WH
		fmod_preset[temp_fmod][18] = 5		-- AIRPORT VOL
		fmod_preset[temp_fmod][19] = 0		-- ANNOUNCEM SET
		fmod_preset[temp_fmod][20] = 0		-- MUTE FMC
		fmod_preset[temp_fmod][21] = 5		-- PM VOLUME
		fmod_preset[temp_fmod][22] = 5		-- ANNOUNC.VOLUME
		fmod_preset[temp_fmod][23] = 0		-- ANNOUNC.SET NR
		fmod_preset[temp_fmod][24] = 0		-- ADD LOWS
		fmod_preset[temp_fmod][25] = 0		-- ADD MIDS
		fmod_preset[temp_fmod][26] = 0		-- ADD HIGHS
		fmod_preset[temp_fmod][27] = 0		-- START CARGO
		fmod_preset[temp_fmod][28] = 5		-- COMPUTER VOLUME
		fmod_preset[temp_fmod][29] = 5		-- FA IN COCKPIT
		fmod_preset[temp_fmod][30] = 5		-- WEATHER VOLUME
		fmod_preset[temp_fmod][31] = 0		-- TO TRIGGER
		fmod_preset[temp_fmod][32] = 5		-- TRIMWHEEL VOLUME
	end
	
	
	local preset_nr_act = 0
	local ignore_line = 0
	local fms_txt = ""
	local fms_val = ""
  local jj = 0
  local kk = 0
	
	if xfile_path_cfg == "" then
		file_name = "Output/preferences/" .. FILE_NAME_FMOD_CFG	--"b738x_fmod.cfg"
	else
		file_name = xfile_path_cfg .. FILE_NAME_FMOD_CFG
	end
	file_navdata = io.open(file_name, "r")
	if file_navdata ~= nil then
		fms_line = file_navdata:read()
		while fms_line do
			if string.len(fms_line) > 1 and string.byte(fms_line, -1) == 13 then	-- CR
				fms_line = string.sub(fms_line, 1, -2)
			end
			
			if string.len(fms_line) > 10 then
				ff = tonumber(string.sub(fms_line, 11, -1))
				if ff ~= nil then
					if string.sub(fms_line, 1, 10) == "PRESET NR " and ff > 0 and ff < 6 then
						preset_nr_act = ff
					end
				end
			end
			
			ignore_line = 0
			jj,kk = string.find(fms_line, "=")
			if jj == nil then
				ignore_line = 1
			else
				if jj > 1 and jj < string.len(fms_line) then
					fms_txt = string.sub(fms_line, 1, jj-1)
					fms_txt = rtrim(fms_txt)
					fms_txt = string.upper(fms_txt)
					fms_val = string.sub(fms_line, jj+1, -1)
					temp_fmod = tonumber(fms_val)
				else
					ignore_line = 1
				end
			end
			
			--if preset_nr_act > 0 and preset_nr_act < 6 and string.len(fms_line) > 16 then
			if preset_nr_act > 0 and preset_nr_act < 6 and ignore_line == 0 then
				-- FMOD
				if fms_txt == "PRESET NAME" then
					fmod_preset[preset_nr_act][1] = fms_val
				elseif fms_txt == "PAX BOARDING" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][2] = 1
						else
							fmod_preset[preset_nr_act][2] = 0
						end
					end
				elseif fms_txt == "GYRO VIBRATORS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][3] = 1
						else
							fmod_preset[preset_nr_act][3] = 0
						end
					end
				elseif fms_txt == "CREW ANNOUN" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][4] = 1
						else
							fmod_preset[preset_nr_act][4] = 0
						end
					end
				elseif fms_txt == "CHATTER PASS" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][5] = 1
						else
							fmod_preset[preset_nr_act][5] = 0
						end
					end
				elseif fms_txt == "AIRPORT SET" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][6] = 1
						elseif temp_fmod == 2 then
							fmod_preset[preset_nr_act][6] = 2
						else
							fmod_preset[preset_nr_act][6] = 3
						end
					end
				elseif fms_txt == "INTERNAL VOL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][7] = 1
						else
							fmod_preset[preset_nr_act][7] = 0
						end
					end
				elseif fms_txt == "INT ENG SND" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][8] = temp_fmod
						else
							fmod_preset[preset_nr_act][8] = 10
						end
					end
				elseif fms_txt == "INT ENG START" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][9] = temp_fmod
						else
							fmod_preset[preset_nr_act][9] = 10
						end
					end
				elseif fms_txt == "AC FANS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][10] = temp_fmod
						else
							fmod_preset[preset_nr_act][10] = 10
						end
					end
				elseif fms_txt == "GYRO VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][11] = temp_fmod
						else
							fmod_preset[preset_nr_act][11] = 10
						end
					end
				elseif fms_txt == "ROLL VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][12] = temp_fmod
						else
							fmod_preset[preset_nr_act][12] = 10
						end
					end
				elseif fms_txt == "BUMP INTENS" then
						if temp_fmod ~= nil then
							 temp_fmod = math.floor(temp_fmod + 0.5)
							if temp_fmod >= 0 and  temp_fmod <= 10 then
								fmod_preset[preset_nr_act][13] = temp_fmod
							else
								fmod_preset[preset_nr_act][13] = 10
							end
						end
				elseif fms_txt == "PAX VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and temp_fmod <= 10 then
							fmod_preset[preset_nr_act][14] = temp_fmod
						else
							fmod_preset[preset_nr_act][14] = 5
						end
					end
				elseif fms_txt == "PAX APPLAUSE" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][15] = 1
						else
							fmod_preset[preset_nr_act][15] = 0
						end
					end
				elseif fms_txt == "INT WIND" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][16] = temp_fmod
						else
							fmod_preset[preset_nr_act][16] = 10
						end
					end
				elseif fms_txt == "MUTE TRIM WH" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][17] = 1
						else
							fmod_preset[preset_nr_act][17] = 0
						end
					end
				elseif fms_txt == "AIRPORT VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][18] = temp_fmod
						else
							fmod_preset[preset_nr_act][18] = 10
						end
					end
				elseif fms_txt == "ANNOUNCEM SET" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 3 then
							fmod_preset[preset_nr_act][19] = temp_fmod
						else
							fmod_preset[preset_nr_act][19] = 0
						end
					end
				elseif fms_txt == "MUTE FMC" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][20] = 1
						else
							fmod_preset[preset_nr_act][20] = 0
						end
					end
				elseif fms_txt == "PM VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][21] = temp_fmod
						else
							fmod_preset[preset_nr_act][21] = 10
						end
					end
				elseif fms_txt == "ANNOUNC.VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][22] = temp_fmod
						else
							fmod_preset[preset_nr_act][22] = 10
						end
					end
				elseif fms_txt == "ANNOUNC.SET NR" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 23 then
							fmod_preset[preset_nr_act][23] = temp_fmod
						else
							fmod_preset[preset_nr_act][23] = 0
						end
					end
				elseif fms_txt == "ADD LOWS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][24] = temp_fmod
						else
							fmod_preset[preset_nr_act][24] = 0
						end
					end
				elseif fms_txt == "ADD MIDS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][25] = temp_fmod
						else
							fmod_preset[preset_nr_act][25] = 0
						end
					end
				elseif fms_txt == "ADD HIGHS" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][26] = temp_fmod
						else
							fmod_preset[preset_nr_act][26] = 0
						end
					end
				elseif fms_txt == "START CARGO" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][27] = 1
						else
							fmod_preset[preset_nr_act][27] = 0
						end
					end
				elseif fms_txt == "COMPUTER VOLUME" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][28] = temp_fmod
						else
							fmod_preset[preset_nr_act][28] = 10
						end
					end
				elseif fms_txt == "FA COCKPIT VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][29] = temp_fmod
						else
							fmod_preset[preset_nr_act][29] = 10
						end
					end
				elseif fms_txt == "WEATHER VOL" then
					if temp_fmod ~= nil then
						 temp_fmod = math.floor(temp_fmod + 0.5)
						if temp_fmod >= 0 and  temp_fmod <= 10 then
							fmod_preset[preset_nr_act][30] = temp_fmod
						else
							fmod_preset[preset_nr_act][30] = 10
						end
					end
				elseif fms_txt == "TO TRIGGER" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][31] = 1
						else
							fmod_preset[preset_nr_act][31] = 0
						end
					end
				elseif fms_txt == "TRIMWHEEL VOL" then
					if temp_fmod ~= nil then
						if temp_fmod == 1 then
							fmod_preset[preset_nr_act][32] = 1
						else
							fmod_preset[preset_nr_act][32] = 0
						end
					end
				end
			end
			fms_line = file_navdata:read()
		end
		file_navdata:close()
	end
end

function B738_set_fmod_config(fmod_nr)
	
	local ff = fmod_nr
	
	B738DR_enable_pax_boarding 		= fmod_preset[ff][2]
	-- B738DR_enable_gyro 				= fmod_preset[ff][3]
	B738DR_enable_crew 				= fmod_preset[ff][4]
	B738DR_enable_chatter 			= fmod_preset[ff][5]
	B738DR_airport_set 				= fmod_preset[ff][6]
	B738DR_vol_int_ducker 			= fmod_preset[ff][7]
	B738DR_vol_int_eng 				= fmod_preset[ff][8]
	B738DR_vol_int_start 			= fmod_preset[ff][9]
	B738DR_vol_int_ac 				= fmod_preset[ff][10]
	B738DR_vol_int_gyro 			= fmod_preset[ff][11]
	B738DR_vol_int_roll 			= fmod_preset[ff][12]
	B738DR_vol_int_bump 			= fmod_preset[ff][13]
	B738DR_vol_int_pax 				= fmod_preset[ff][14]
	B738DR_vol_int_pax_applause 	= fmod_preset[ff][15]
	B738DR_vol_int_wind_vol 		= fmod_preset[ff][16]
	B738DR_enable_mutetrim 			= fmod_preset[ff][17]
	B738DR_vol_airport 				= fmod_preset[ff][18]
	--B738DR_ann_set 					= fmod_preset[ff][19]
	--B738DR_announcement_set			= fmod_preset[ff][19]
	B738DR_enable_fmc_mute_on 		= fmod_preset[ff][20]
	B738DR_vol_PM 					= fmod_preset[ff][21]
	B738DR_vol_crew 				= fmod_preset[ff][22]
	--B738DR_announcement_set 		= fmod_preset[ff][23]
	B738DR_pa_announc_set			= fmod_preset[ff][23]
	eq_low 							= fmod_preset[ff][24]
	eq_mid 							= fmod_preset[ff][25]
	eq_high 						= fmod_preset[ff][26]
	play_cargo 						= fmod_preset[ff][27]
	B738DR_vol_computer 			= fmod_preset[ff][28]
	B738DR_vol_FAC					= fmod_preset[ff][29]
	B738DR_vol_weather				= fmod_preset[ff][30]
	B738DR_TO_trigger				= fmod_preset[ff][31]
	B738DR_vol_int_trim				= fmod_preset[ff][32]
	
end

function B738_mod_fmod_config(fmod_nr)
	
	local ff = fmod_nr
	
	fmod_preset[ff][2] 	= B738DR_enable_pax_boarding
	fmod_preset[ff][3] 	= 0	--B738DR_enable_gyro
	fmod_preset[ff][4] 	= B738DR_enable_crew
	fmod_preset[ff][5] 	= B738DR_enable_chatter
	fmod_preset[ff][6] 	= B738DR_airport_set
	fmod_preset[ff][7] 	= B738DR_vol_int_ducker
	fmod_preset[ff][8] 	= B738DR_vol_int_eng
	fmod_preset[ff][9] 	= B738DR_vol_int_start
	fmod_preset[ff][10] 	= B738DR_vol_int_ac
	fmod_preset[ff][11] 	= B738DR_vol_int_gyro
	fmod_preset[ff][12] 	= B738DR_vol_int_roll
	fmod_preset[ff][13] 	= B738DR_vol_int_bump
	fmod_preset[ff][14] 	= B738DR_vol_int_pax
	fmod_preset[ff][15] 	= B738DR_vol_int_pax_applause
	fmod_preset[ff][16] 	= B738DR_vol_int_wind_vol
	fmod_preset[ff][17] 	= B738DR_enable_mutetrim
	fmod_preset[ff][18] 	= B738DR_vol_airport
	fmod_preset[ff][19] 	= 0	--B738DR_ann_set
	--fmod_preset[ff][19] 	= B738DR_announcement_set
	fmod_preset[ff][20] 	= B738DR_enable_fmc_mute_on
	fmod_preset[ff][21] 	= B738DR_vol_PM
	fmod_preset[ff][22] 	= B738DR_vol_crew
	--fmod_preset[ff][23] 	= B738DR_announcement_set
	fmod_preset[ff][23] 	= B738DR_pa_announc_set
	fmod_preset[ff][24] 	= eq_low
	fmod_preset[ff][25] 	= eq_mid
	fmod_preset[ff][26] 	= eq_high
	fmod_preset[ff][27] 	= play_cargo
	fmod_preset[ff][28] 	= B738DR_vol_computer
	fmod_preset[ff][29] 	= B738DR_vol_FAC
	fmod_preset[ff][30] 	= B738DR_vol_weather
	fmod_preset[ff][31] 	= B738DR_TO_trigger
	fmod_preset[ff][32] 	= B738DR_vol_int_trim
	
end

function B738_save_hw_config()
	
	local fms_line = ""
	
	file_name = "Output/preferences/" .. FILE_NAME_HW_CFG
	file_navdata = io.open(file_name, "w")
	if file_navdata ~= nil then
		-- OTHERS
		fms_line = "*** B737-800X ZIBO MOD ***\n"
		file_navdata:write(fms_line)
		fms_line = "*** Config file        ***\n"
		file_navdata:write(fms_line)
		fms_line = "TOE BRAKE AXIS = " .. string.format("%2d", B738DR_toe_brakes_ovr) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LOCK IDLE THR  = " .. string.format("%2d", B738DR_lock_idle_thrust) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LOCK THROTTLE  = " .. string.format("%2d", B738DR_lock_thrust) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "BRAKE REM CHOCK= " .. string.format("%2d", B738DR_parkbrake_remove_chock) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "THROTTLE NOISE = " .. string.format("%2d", B738DR_throttle_noise) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FPLN SAVE FMT  = " .. string.format("%2d", B738DR_fpln_format) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PITCH 0 ZONE   = " .. string.format("%2d", simDR_pitch_nz * 100) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ROLL 0 ZONE    = " .. string.format("%2d", simDR_roll_nz * 100) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "YAW 0 ZONE     = " .. string.format("%2d", simDR_yaw_nz * 100) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT UP = " .. string.format("%7d", B738DR_flap_detents[0] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 1  = " .. string.format("%7d", B738DR_flap_detents[1] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 2  = " .. string.format("%7d", B738DR_flap_detents[2] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 5  = " .. string.format("%7d", B738DR_flap_detents[3] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 10 = " .. string.format("%7d", B738DR_flap_detents[4] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 15 = " .. string.format("%7d", B738DR_flap_detents[5] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 25 = " .. string.format("%7d", B738DR_flap_detents[6] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 30 = " .. string.format("%7d", B738DR_flap_detents[7] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DETENT 40 = " .. string.format("%7d", B738DR_flap_detents[8] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR UP = " .. string.format("%7d", B738DR_flap_detents_thr[0] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 1  = " .. string.format("%7d", B738DR_flap_detents_thr[1] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 2  = " .. string.format("%7d", B738DR_flap_detents_thr[2] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 5  = " .. string.format("%7d", B738DR_flap_detents_thr[3] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 10 = " .. string.format("%7d", B738DR_flap_detents_thr[4] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 15 = " .. string.format("%7d", B738DR_flap_detents_thr[5] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 25 = " .. string.format("%7d", B738DR_flap_detents_thr[6] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 30 = " .. string.format("%7d", B738DR_flap_detents_thr[7] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP DT THR 40 = " .. string.format("%7d", B738DR_flap_detents_thr[8] * 1000000) .. "\n"
		file_navdata:write(fms_line)
		
		fms_line = "SPEEDBRAKE MAP = " .. string.format("%3d", B738DR_cust_axis_speedbrake_mapped) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP MAPPED    = " .. string.format("%3d", B738DR_cust_axis_flaps_mapped) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SPEEDBRAKE REV = " .. string.format("%2d", B738DR_cust_axis_speedbrake_reverse) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FLAP REVERSE   = " .. string.format("%2d", B738DR_cust_axis_flaps_reverse) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SPEEDBRAKE NOI = " .. string.format("%2d", B738DR_cust_axis_speedbrake_noise) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SPEEDBRAKE ARM = " .. string.format("%7d", B738DR_cust_axis_speedbrake_arm * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SPEEDBRAKE FLT = " .. string.format("%7d", B738DR_cust_axis_speedbrake_flight * 1000000) .. "\n"
		file_navdata:write(fms_line)
		
		fms_line = "LANDGEAR MAP   = " .. string.format("%3d", B738DR_cust_axis_landgear_mapped) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LANDGEAR REV   = " .. string.format("%2d", B738DR_cust_axis_landgear_reverse) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LANDGEAR OFF   = " .. string.format("%7d", B738DR_cust_axis_landgear_off * 1000000) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MIXTURE1 MAP   = " .. string.format("%3d", B738DR_cust_axis_mixture1_mapped) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MIXTURE1 REV   = " .. string.format("%2d", B738DR_cust_axis_mixture1_reverse) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MIXTURE2 MAP   = " .. string.format("%3d", B738DR_cust_axis_mixture2_mapped) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MIXTURE2 REV   = " .. string.format("%2d", B738DR_cust_axis_mixture2_reverse) .. "\n"
		file_navdata:write(fms_line)
		
		file_navdata:close()
	end
	
end


function B738_save_config(xfile_path_cfg)
	
	B738_save_hw_config()
	
	local fms_line = ""
	local ff = 0
	
	if xfile_path_cfg == "" then
		file_name = "Output/preferences/" .. FILE_NAME_CFG	--"b738x.cfg"
	else
		file_name = xfile_path_cfg .. FILE_NAME_CFG
	end
	file_navdata = io.open(file_name, "w")
	if file_navdata ~= nil then
		-- OTHERS
		fms_line = "*** B737-800X ZIBO MOD ***\n"
		file_navdata:write(fms_line)
		fms_line = "*** Config file        ***\n"
		file_navdata:write(fms_line)
		fms_line = "UNITS          = " .. string.format("%2d", B738DR_fmc_units) .. "\n"
		file_navdata:write(fms_line)
		
		fms_line = "TRACK UP       = " .. string.format("%2d", B738DR_track_up) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "BARO IN HPA    = " .. string.format("%2d", B738DR_baro_in_hpa) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MIN BARO RADIO = " .. string.format("%2d", B738DR_min_baro_radio) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "VOR RADIAL     = " .. string.format("%2d", B738DR_vor_radial) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ND RINGS       = " .. string.format("%2d", B738DR_nd_rings) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "WHITE BUG      = " .. string.format("%2d", B738DR_white_bug) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "GP CAB CONNECT = " .. string.format("%2d", B738DR_gp_cable_connect) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CHRONO VARIANT = " .. string.format("%2d", B738DR_chrono_variant) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PFD FMC FLPSPD = " .. string.format("%2d", B738DR_pfd_fmc_flap_spd) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ND TCAS RANGE  = " .. string.format("%2d", B738DR_nd_tcas_range) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "TAKEOFF HDG    = " .. string.format("%2d", B738DR_takeoff_hdg) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "INIT REF SMART = " .. string.format("%2d", B738DR_init_ref_smart) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "IRS DING       = " .. string.format("%2d", B738DR_irs_ding) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LONG STROBES   = " .. string.format("%2d", B738DR_long_led_strobes) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "WINGS VARIANT  = " .. string.format("%2d", B738DR_wings_variant) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SATCOM VARIANT = " .. string.format("%2d", B738DR_satcom_variant) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PULSELITE      = " .. string.format("%2d", B738DR_pulse_lite) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LED LIGHTS     = " .. string.format("%2d", B738DR_led_lights) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "HUD DISABLE    = " .. string.format("%2d", B738DR_hud_disable) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ANNOUNC.SET NR = " .. string.format("%2d", B738DR_pa_announc_set) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CIRC AOA RA    = " .. string.format("%2d", B738DR_aoa_ra) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FUEL GAUGE     = " .. string.format("%2d", B738DR_fuelgauge) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "COM DECIMAL    = " .. string.format("%2d", B738DR_com_decimal) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MMR            = " .. string.format("%2d", B738DR_mmr) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MMR COLOUR     = " .. string.format("%2d", B738DR_mmr_colour) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ASP TAT PROBE  = " .. string.format("%2d", B738DR_aspirated_probe) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 5      = " .. string.format("%2d", B738DR_ra_callout_disable[0]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 10     = " .. string.format("%2d", B738DR_ra_callout_disable[1]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 20     = " .. string.format("%2d", B738DR_ra_callout_disable[2]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 30     = " .. string.format("%2d", B738DR_ra_callout_disable[3]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 40     = " .. string.format("%2d", B738DR_ra_callout_disable[4]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 50     = " .. string.format("%2d", B738DR_ra_callout_disable[5]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 60     = " .. string.format("%2d", B738DR_ra_callout_disable[6]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 100    = " .. string.format("%2d", B738DR_ra_callout_disable[7]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 200    = " .. string.format("%2d", B738DR_ra_callout_disable[8]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 300    = " .. string.format("%2d", B738DR_ra_callout_disable[9]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 400    = " .. string.format("%2d", B738DR_ra_callout_disable[10]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 500    = " .. string.format("%2d", B738DR_ra_callout_disable[11]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 1000   = " .. string.format("%2d", B738DR_ra_callout_disable[12]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 1000s  = " .. string.format("%2d", B738DR_ra_callout_disable[13]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CALLOUT 2500   = " .. string.format("%2d", B738DR_ra_callout_disable[14]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "BRAKES TEMP    = " .. string.format("%2d", B738DR_brake_temp) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "M STD WGHT     = " .. string.format("%4.1f", B738DR_std_pax_weight[0]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "F STD WGHT     = " .. string.format("%4.1f", B738DR_std_pax_weight[1]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "K STD WGHT     = " .. string.format("%4.1f", B738DR_std_pax_weight[2]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PAX LAYOUT     = " .. string.format("%2d", B738DR_pax_layout) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FA FWD         = " .. string.format("%4.1f", B738DR_fa_fwd_kg) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FA AFT         = " .. string.format("%4.1f", B738DR_fa_aft_kg) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "GALLEY FWD     = " .. string.format("%4.1f", B738DR_galley_fwd_kg) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "GALLEY AFT     = " .. string.format("%4.1f", B738DR_galley_aft_kg) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "VNAV ALT DIS   = " .. string.format("%2d", B738DR_vnav_alt_disable) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "VNAVALT_ALTHLD = " .. string.format("%2d", B738DR_vnavalt_althld) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FD CAM         = " .. string.format("%2d", B738DR_fd_cam_disable) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SELCAL         = " .. string.format("%2d", B738DR_selcal_disable) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ISFD_RMI       = " .. string.format("%2d", math.floor(B738DR_isfd_rmi)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "NEW_FUELCUTOFF = " .. string.format("%2d", math.floor(B738DR_new_fuel_cutoff)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "NPS            = " .. string.format("%2d", math.floor(B738DR_nps)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "YAW_DAMPER     = " .. string.format("%2d", math.floor(B738DR_yaw_damper)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MUTE SWITCHES  = " .. string.format("%2d", math.floor(B738DR_mute_switches)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CLOCK_VARIANT  = " .. string.format("%2d", math.floor(B738DR_clock_variant)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "OLD MCP        = " .. string.format("%2d", math.floor(B738DR_old_mcp)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CLB ALT        = " .. string.format("%4d", math.floor(B738DR_def_clb_alt)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CLB ALT2       = " .. string.format("%4d", math.floor(B738DR_def_clb_alt2)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CUT ALT        = " .. string.format("%4d", math.floor(B738DR_def_cut_alt)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ACCEL ALT      = " .. string.format("%4d", math.floor(B738DR_def_accel_alt)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "EO ACCEL ALT   = " .. string.format("%4d", math.floor(B738DR_def_eo_accel_alt)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PLUS HUNDRED   = " .. string.format("%2d", math.floor(B738DR_plus_hundred)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "VORLOC_LNAV    = " .. string.format("%2d", math.floor(B738DR_vorloc_lnav)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FMC TYPE       = " .. string.format("%2d", math.floor(B738DR_fmc_type)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "RAAS           = " .. string.format("%2d", math.floor(B738DR_raas)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "NOT_AIRSTAIRS  = " .. string.format("%2d", math.floor(B738DR_not_airstairs)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ENG_START_AUTO = " .. string.format("%2d", math.floor(B738DR_engine_start_auto)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PROBE_AUTO     = " .. string.format("%2d", math.floor(B738DR_probe_heat_auto)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ROLLOUT        = " .. string.format("%2d", math.floor(B738DR_autoland_install)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CT_DISABLE     = " .. string.format("%2d", math.floor(B738DR_nps_disable)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "IAN_DISABLE    = " .. string.format("%2d", math.floor(B738DR_ian_not_install)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FUEL PUMPS     = " .. string.format("%2d", math.floor(B738DR_fuel_pumps_off_placard)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CAT III        = " .. string.format("%2d", math.floor(B738DR_cat3_placard)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "BRAKES         = " .. string.format("%2d", math.floor(B738DR_brakes_placard)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FF EICAS       = " .. string.format("%2d", math.floor(B738DR_ff_eicas)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "DIG_PRESS_PNL  = " .. string.format("%2d", math.floor(B738DR_digital_press_panel)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "NO RTE2        = " .. string.format("%2d", math.floor(B738DR_no_rte2)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "GS_ON_PFD      = " .. string.format("%2d", math.floor(B738DR_gs_on_pfd)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ILS_GLS_ANN    = " .. string.format("%2d", math.floor(B738DR_ils_gls_annun)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "NO_SMOKING     = " .. string.format("%2d", math.floor(B738DR_no_smoking)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "NO_VCR_SW      = " .. string.format("%2d", math.floor(B738DR_no_vcr_switch)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "GW_ENTRY       = " .. string.format("%2d", math.floor(B738DR_gw_entry)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MCP_SPD_INTV   = " .. string.format("%2d", math.floor(B738DR_spd_intv_but)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MCP_ALT_INTV   = " .. string.format("%2d", math.floor(B738DR_alt_intv_but)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "RMI            = " .. string.format("%2d", math.floor(B738DR_rmi)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SFP            = " .. string.format("%2d", math.floor(B738DR_sfp)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "AT_WAKEUP      = " .. string.format("%2d", math.floor(B738DR_at_wakeup)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PLAN_FUEL      = " .. string.format("%2d", math.floor(B738DR_plan_fuel)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "OIL_QTY        = " .. string.format("%2d", math.floor(B738DR_oil_qty_pct_qts)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "VNAV_WU_IDLE   = " .. string.format("%2d", math.floor(B738DR_vnav_wakeup_idle)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "ARPT_MIN_RW    = " .. string.format("%4d", math.floor(B738DR_nd_arpt_rw_lenght)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "RADIO_ALT_CO   = " .. string.format("%2d", math.floor(B738DR_ra_radio_altimeter)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "DECISION_HGT   = " .. string.format("%2d", math.floor(B738DR_ra_decision_height)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "RA_MINIMUMS    = " .. string.format("%2d", math.floor(B738DR_ra_minimums)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "500_1000_BARO  = " .. string.format("%2d", math.floor(B738DR_ra_500_1000_baro)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MISSED_APP_COL = " .. string.format("%2d", math.floor(B738DR_missed_app_colour)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "HIGH_ALT_LDG   = " .. string.format("%2d", math.floor(B738DR_high_alt_ldg)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "EYEBROW        = " .. string.format("%2d", math.floor(B738DR_no_eyebrow_kill)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "AFT_EXIT       = " .. string.format("%2d", math.floor(B738DR_no_aft_exit_kill)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "PIP            = " .. string.format("%2d", math.floor(B738DR_no_pip_kill)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FO_CALLOUT_VAR = " .. string.format("%2d", math.floor(B738DR_fo_callout_var)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FUEL_CUTOFF_SW = " .. string.format("%2d", math.floor(B738DR_fuel_cutoff_sw)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "HYD_COMPENS    = " .. string.format("%2d", math.floor(B738DR_hyd_compensator)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CTR_FPUMP_AUTO = " .. string.format("%2d", math.floor(B738DR_ctr_fuel_pump_auto)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "VNAV COMMON    = " .. string.format("%2d", math.floor(B738DR_vnav_common)) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "EICAS_FULL_N1  = " .. string.format("%2d", B738DR_eicas_full_n1) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "EICAS_N1_NUM   = " .. string.format("%2d", B738DR_eicas_n1_num) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SINGLE_CUE_FD  = " .. string.format("%2d", math.floor(B738DR_single_cue_fd)) .. "\n"
		file_navdata:write(fms_line)
		if xfile_path_cfg == "" then
			fms_line = "ALIGN TIME     = " .. string.format("%2d", B738DR_align_time) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "REFUEL TIME    = " .. string.format("%2d", B738DR_refuel_time) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CHECKLIST      = " .. string.format("%2d", B738DR_detail_cl) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FMC INPUT LAG  = " .. string.format("%2d", B738DR_fmc_input_lag) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AP YOKE TLRNC  = " .. string.format("%2d", B738DR_ap_yoke_tolerance) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "HIDE YOKE      = " .. string.format("%2d", simDR_hide_yoke) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "HIDE SEATS     = " .. string.format("%2d", B738DR_hide_seats) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FO CALLOUT     = " .. string.format("%2d", B738DR_disable_fo_callout) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PAUSE AT T/D   = " .. string.format("%2d", B738DR_pause_td) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TOE BRAKE AXIS = " .. string.format("%2d", B738DR_toe_brakes_ovr) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LOCK IDLE THR  = " .. string.format("%2d", B738DR_lock_idle_thrust) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LOCK THROTTLE  = " .. string.format("%2d", B738DR_lock_thrust) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ENG NO RUNNING = " .. string.format("%2d", B738DR_engine_no_running_state) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CHOCKS STATE   = " .. string.format("%2d", B738DR_chock_on_startup) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "BRAKE REM CHOCK= " .. string.format("%2d", B738DR_parkbrake_remove_chock) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "THROTTLE NOISE = " .. string.format("%2d", B738DR_throttle_noise) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SMART AP KNOB  = " .. string.format("%2d", B738DR_smart_ap_knob) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "NOSEWHEEL AXIS = " .. string.format("%2d", B738DR_nosewheel) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FPLN SAVE FMT  = " .. string.format("%2d", B738DR_fpln_format) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "WINDSH.EFFECTS = " .. string.format("%2d", B738DR_kill_windshield) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "WINDSH.REFLECT = " .. string.format("%2d", B738DR_hide_glass) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TIRE BLOWN     = " .. string.format("%2d", B738DR_tire_blown) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "GAUGES REFLECT = " .. string.format("%2d", B738DR_hide_gauges_reflection) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLIGHT CONTROL = " .. string.format("%2d", B738DR_flight_ctrl) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "THROTTLE LEVER = " .. string.format("%2d", B738DR_hardware_thr_lever) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SYNC PILOT     = " .. string.format("%2d", B738DR_sync_pilot) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SYNC PILOT STD = " .. string.format("%2d", B738DR_sync_pilot_isdf) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CUST NAVDATA   = " .. string.format("%2d", B738DR_cust_nav_data) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PRIOR TX COM   = " .. string.format("%2d", B738DR_prior_tx_com) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FMS ALT/SPD    = " .. string.format("%2d", B738DR_restrict_fms) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PASSENGERS     = " .. string.format("%2d", B738DR_passengers) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "YOKE DISCO AP  = " .. string.format("%2d", B738DR_yoke_disco_ap) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "YOKE TO CENTER = " .. string.format("%2d", B738DR_yoke_to_center) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "BRUNNER ENABLE = " .. string.format("%2d", B738DR_brunner_enable) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "XHSI ENABLE    = " .. string.format("%2d", B738DR_xhsi_enable) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CFY ENABLE     = " .. string.format("%2d", B738DR_cfy_enable) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CFY TRIM       = " .. string.format("%2d", B738DR_cfy_trim_control) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SAM ENABLE     = " .. string.format("%2d", B738DR_sam_enable) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "EXT_PAYLOAD    = " .. string.format("%2d", B738DR_ext_payload) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PARKBRAKE SET  = " .. string.format("%2d", B738DR_parkbrake_set) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "STROBE COCKPIT = " .. string.format("%2d", B738DR_strobe_cockpit) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PARTICLE RAIN  = " .. string.format("%2d", B738DR_particle_rain) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SHOW FPS       = " .. string.format("%2d", B738DR_fps_enable) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FD HELPER      = " .. string.format("%2d", B738DR_fd_helper) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_YOKE     = " .. string.format("%2d", B738DR_manip_yoke) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_THR      = " .. string.format("%2d", B738DR_manip_throttle) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_FLAP     = " .. string.format("%2d", B738DR_manip_flaps) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_SPDBRK   = " .. string.format("%2d", B738DR_manip_speedbrake) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_TRIMWH   = " .. string.format("%2d", B738DR_manip_trimwheel) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_PARKBRK  = " .. string.format("%2d", B738DR_manip_park_brake) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_STABTRIM = " .. string.format("%2d", B738DR_manip_stab_trim) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_FUELCUT  = " .. string.format("%2d", B738DR_manip_fuelcutout) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MANIP_HORNCUT  = " .. string.format("%2d", B738DR_manip_horn_cutout) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "GUARDED_COVERS = " .. string.format("%2d", B738DR_guarded_covers) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PITCH 0 ZONE   = " .. string.format("%2d", simDR_pitch_nz * 100) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ROLL 0 ZONE    = " .. string.format("%2d", simDR_roll_nz * 100) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "YAW 0 ZONE     = " .. string.format("%2d", simDR_yaw_nz * 100) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT UP = " .. string.format("%7d", B738DR_flap_detents[0] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 1  = " .. string.format("%7d", B738DR_flap_detents[1] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 2  = " .. string.format("%7d", B738DR_flap_detents[2] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 5  = " .. string.format("%7d", B738DR_flap_detents[3] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 10 = " .. string.format("%7d", B738DR_flap_detents[4] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 15 = " .. string.format("%7d", B738DR_flap_detents[5] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 25 = " .. string.format("%7d", B738DR_flap_detents[6] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 30 = " .. string.format("%7d", B738DR_flap_detents[7] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DETENT 40 = " .. string.format("%7d", B738DR_flap_detents[8] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR UP = " .. string.format("%7d", B738DR_flap_detents_thr[0] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 1  = " .. string.format("%7d", B738DR_flap_detents_thr[1] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 2  = " .. string.format("%7d", B738DR_flap_detents_thr[2] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 5  = " .. string.format("%7d", B738DR_flap_detents_thr[3] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 10 = " .. string.format("%7d", B738DR_flap_detents_thr[4] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 15 = " .. string.format("%7d", B738DR_flap_detents_thr[5] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 25 = " .. string.format("%7d", B738DR_flap_detents_thr[6] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 30 = " .. string.format("%7d", B738DR_flap_detents_thr[7] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP DT THR 40 = " .. string.format("%7d", B738DR_flap_detents_thr[8] * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SPEEDBRAKE MAP = " .. string.format("%3d", B738DR_cust_axis_speedbrake_mapped) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP MAPPED    = " .. string.format("%3d", B738DR_cust_axis_flaps_mapped) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SPEEDBRAKE REV = " .. string.format("%2d", B738DR_cust_axis_speedbrake_reverse) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FLAP REVERSE   = " .. string.format("%2d", B738DR_cust_axis_flaps_reverse) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SPEEDBRAKE NOI = " .. string.format("%2d", B738DR_cust_axis_speedbrake_noise) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SPEEDBRAKE ARM = " .. string.format("%7d", B738DR_cust_axis_speedbrake_arm * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "SPEEDBRAKE FLT = " .. string.format("%7d", B738DR_cust_axis_speedbrake_flight * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LANDGEAR MAP   = " .. string.format("%3d", B738DR_cust_axis_landgear_mapped) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LANDGEAR REV   = " .. string.format("%2d", B738DR_cust_axis_landgear_reverse) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "LANDGEAR OFF   = " .. string.format("%7d", B738DR_cust_axis_landgear_off * 1000000) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MIXTURE1 MAP   = " .. string.format("%3d", B738DR_cust_axis_mixture1_mapped) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MIXTURE1 REV   = " .. string.format("%2d", B738DR_cust_axis_mixture1_reverse) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MIXTURE2 MAP   = " .. string.format("%3d", B738DR_cust_axis_mixture2_mapped) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MIXTURE2 REV   = " .. string.format("%2d", B738DR_cust_axis_mixture2_reverse) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AUTOSAVE       = " .. string.format("%2d", B738DR_autosave) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "VANISH_Y       = " .. string.format("%3d", B738DR_vanish_y) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "3D_ARROW       = " .. string.format("%2d", B738DR_arrow_3d_helper) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "EFB_COLORIZE   = " .. string.format("%2d", B738DR_efb_colorize) .. "\n"
			file_navdata:write(fms_line)
			
			-- FMOD
			fms_line = "\n*** FMOD by AudioBird XP ***\n"
			file_navdata:write(fms_line)
			fms_line = "PAX BOARDING   = " .. string.format("%2d", B738DR_enable_pax_boarding) .. "\n"
			file_navdata:write(fms_line)
			-- fms_line = "GYRO VIBRATORS = " .. string.format("%2d", B738DR_enable_gyro) .. "\n"
			-- file_navdata:write(fms_line)
			fms_line = "CREW ANNOUN    = " .. string.format("%2d", B738DR_enable_crew) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CHATTER PASS   = " .. string.format("%2d", B738DR_enable_chatter) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AIRPORT SET    = " .. string.format("%2d", B738DR_airport_set) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INTERNAL VOL   = " .. string.format("%2d", B738DR_vol_int_ducker) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INT ENG SND    = " .. string.format("%2d", B738DR_vol_int_eng) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INT ENG START  = " .. string.format("%2d", B738DR_vol_int_start) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AC FANS        = " .. string.format("%2d", B738DR_vol_int_ac) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "GYRO VOL       = " .. string.format("%2d", B738DR_vol_int_gyro) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ROLL VOL       = " .. string.format("%2d", B738DR_vol_int_roll) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "BUMP INTENS    = " .. string.format("%2d", B738DR_vol_int_bump) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PAX VOLUME     = " .. string.format("%2d", B738DR_vol_int_pax) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PAX APPLAUSE   = " .. string.format("%2d", B738DR_vol_int_pax_applause) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INT WIND       = " .. string.format("%2d", B738DR_vol_int_wind_vol) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MUTE TRIM WH   = " .. string.format("%2d", B738DR_enable_mutetrim) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AIRPORT VOL    = " .. string.format("%2d", B738DR_vol_airport) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MUTE FMC       = " .. string.format("%2d", B738DR_enable_fmc_mute_on) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TO TRIGGER     = " .. string.format("%2d", B738DR_TO_trigger) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PM VOLUME      = " .. string.format("%2d", B738DR_vol_PM) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ANNOUNC.VOLUME = " .. string.format("%2d", B738DR_vol_crew) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ADD LOWS       = " .. string.format("%2d", eq_low) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ADD MIDS       = " .. string.format("%2d", eq_mid) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ADD HIGHS      = " .. string.format("%2d", eq_high) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "START CARGO    = " .. string.format("%2d", play_cargo) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "COMPUTER VOLUME= " .. string.format("%2d", B738DR_vol_computer) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FA COCKPIT VOL = " .. string.format("%2d", B738DR_vol_FAC) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "WEATHER VOL    = " .. string.format("%2d", B738DR_vol_weather) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TRIMWHEEL VOL  = " .. string.format("%2d", B738DR_vol_int_trim) .. "\n"
			file_navdata:write(fms_line)
		end
		file_navdata:close()
	end
	
end

function B738_save_fmod_config(xfile_path_cfg)

	local fms_line = ""
	local ff = 0
	
	if xfile_path_cfg == "" then
		file_name = "Output/preferences/" .. FILE_NAME_FMOD_CFG	--"b738x_fmod.cfg"
	else
		file_name = xfile_path_cfg .. FILE_NAME_FMOD_CFG
	end
	file_navdata = io.open(file_name, "w")
	if file_navdata ~= nil then
		-- OTHERS
		fms_line = "*** B737-800X ZIBO MOD ***\n"
		file_navdata:write(fms_line)
		-- FMOD PRESETS
		fms_line = "\n*** FMOD by AudioBird XP ***\n"
		file_navdata:write(fms_line)
		fms_line = "\n*** FMOD Presets ***\n"
		file_navdata:write(fms_line)
		for ff = 1, 5 do
			fms_line = "\nPRESET NR " .. string.format("%1d", ff) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PRESET NAME    =" .. fmod_preset[ff][1] .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PAX BOARDING   = " .. string.format("%2d", fmod_preset[ff][2]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "GYRO VIBRATORS = " .. string.format("%2d", fmod_preset[ff][3]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CREW ANNOUN    = " .. string.format("%2d", fmod_preset[ff][4]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "CHATTER PASS   = " .. string.format("%2d", fmod_preset[ff][5]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AIRPORT SET    = " .. string.format("%2d", fmod_preset[ff][6]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INTERNAL VOL   = " .. string.format("%2d", fmod_preset[ff][7]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INT ENG SND    = " .. string.format("%2d", fmod_preset[ff][8]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INT ENG START  = " .. string.format("%2d", fmod_preset[ff][9]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AC FANS        = " .. string.format("%2d", fmod_preset[ff][10]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "GYRO VOL       = " .. string.format("%2d", fmod_preset[ff][11]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ROLL VOL       = " .. string.format("%2d", fmod_preset[ff][12]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "BUMP INTENS    = " .. string.format("%2d", fmod_preset[ff][13]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PAX VOLUME     = " .. string.format("%2d", fmod_preset[ff][14]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PAX APPLAUSE   = " .. string.format("%2d", fmod_preset[ff][15]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "INT WIND       = " .. string.format("%2d", fmod_preset[ff][16]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MUTE TRIM WH   = " .. string.format("%2d", fmod_preset[ff][17]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "AIRPORT VOL    = " .. string.format("%2d", fmod_preset[ff][18]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ANNOUNCEM SET  = " .. string.format("%2d", fmod_preset[ff][19]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "MUTE FMC       = " .. string.format("%2d", fmod_preset[ff][20]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "PM VOLUME      = " .. string.format("%2d", fmod_preset[ff][21]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ANNOUNC.VOLUME = " .. string.format("%2d", fmod_preset[ff][22]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ANNOUNC.SET NR = " .. string.format("%2d", fmod_preset[ff][23]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ADD LOWS       = " .. string.format("%2d", fmod_preset[ff][24]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ADD MIDS       = " .. string.format("%2d", fmod_preset[ff][25]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "ADD HIGHS      = " .. string.format("%2d", fmod_preset[ff][26]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "START CARGO    = " .. string.format("%2d", fmod_preset[ff][27]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "COMPUTER VOLUME= " .. string.format("%2d", fmod_preset[ff][28]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "FA COCKPIT VOL = " .. string.format("%2d", fmod_preset[ff][29]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "WEATHER VOL    = " .. string.format("%2d", fmod_preset[ff][30]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TO TRIGGER     = " .. string.format("%2d", fmod_preset[ff][31]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = "TRIMWHEEL VOL  = " .. string.format("%2d", fmod_preset[ff][32]) .. "\n"
			file_navdata:write(fms_line)
		end
		file_navdata:close()
	end

end

function B738_load_status()
	
	local fms_line = ""
	local temp_fmod = 0
	local temp_fmod_str = ""
	local pressure_panel_set = 0
	local squawk_ok = 0
	local dlink_data_import = -1
	local dlink_data_import2 = 0
	
	file_name = "Output/preferences/" .. FILE_NAME_STATUS	--"b738x_status.dat"
	file_navdata = io.open(file_name, "r")
	
	if file_navdata ~= nil then
		fms_line = file_navdata:read()
		while fms_line do
			if string.len(fms_line) > 1 and string.byte(fms_line, -1) == 13 then	-- CR
				fms_line = string.sub(fms_line, 1, -2)
			end
			
			if fms_line == "DLINK DATA" then
				dlink_data_import = 0
			end
			
			if dlink_data_import < 0 then
				if fms_line == "**AUTO**" then
					if B738DR_autosave ~= 0 and B738DR_autoload_on == 0 then
						B738DR_autoload_on = 1
					end
				elseif string.len(fms_line) > 16 then
					if string.sub(fms_line, 1, 16) == "LAST POS LAT   =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod == nil then
								B738DR_last_pos_lat = 0
							else
								B738DR_last_pos_lat = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "LAST POS LON   =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod == nil then
								B738DR_last_pos_lon = 0
							else
								B738DR_last_pos_lon = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "FUEL TANK LFT  =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								fuel_tank_last_left = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "FUEL TANK RGT  =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								fuel_tank_last_right = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "FUEL TANK CTR  =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								fuel_tank_last_center = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "MMR STBY GLS1  =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								B738DR_mmr_cpt_stby_gls = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "MMR STBY GLS2  =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								B738DR_mmr_fo_stby_gls = temp_fmod
							end
						end
					elseif string.sub(fms_line, 1, 16) == "CRUISE ALT     =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								local mcp_alt_dial = roundUpToIncrement(temp_fmod, 500)
								mcp_alt_dial = math.min(mcp_alt_dial, 42000)
								mcp_alt_dial = math.max(mcp_alt_dial, 0)
								simDR_press_max_alt = mcp_alt_dial
								pressure_panel_set = 1
							end
						end
					elseif string.sub(fms_line, 1, 16) == "LANDING ALT    =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod > 16 then
							temp_fmod = tonumber(string.sub(fms_line, 17, -1))
							if temp_fmod ~= nil then
								local mcp_alt_dial = roundUpToIncrement(temp_fmod, 50)
								mcp_alt_dial = math.min(mcp_alt_dial, 13600)
								mcp_alt_dial = math.max(mcp_alt_dial, -1000)
								B738DR_land_alt_knob = mcp_alt_dial
								pressure_panel_set = 1
							end
						end
					elseif string.sub(fms_line, 1, 16) == "SQUAWK         =" then
						temp_fmod = string.len(fms_line)
						if temp_fmod == 21 then
							squawk_ok = 1
							for xyz = 18, 21 do
								temp_fmod_str = string.sub(fms_line, xyz, xyz)
								temp_fmod = tonumber(temp_fmod_str)
								if temp_fmod == nil then
									squawk_ok = 0
									break
								elseif temp_fmod < 0 or temp_fmod > 7 then
									squawk_ok = 0
									break
								end
							end
							
							if squawk_ok ~= 0 then
								simDR_squawk = tonumber(string.sub(fms_line, 18, 21))
							end
						end
					end
				end
			elseif dlink_data_import > 0 and dlink_data_import < 5 then
				if dlink_data_import2 == 1 then
					dl_flights_ref[dlink_data_import] = fms_line
				elseif dlink_data_import2 == 2 then
					dl_flights_des[dlink_data_import] = fms_line
				elseif dlink_data_import2 == 3 then
					dl_flights_flt_num[dlink_data_import] = fms_line
				elseif dlink_data_import2 == 4 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_day[dlink_data_import] = 0
					else
						dl_flights_day[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 5 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_out_hrs[dlink_data_import] = 0
					else
						dl_flights_out_hrs[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 6 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_out_min[dlink_data_import] = 0
					else
						dl_flights_out_min[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 7 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_out_fuel[dlink_data_import] = -1
					else
						dl_flights_out_fuel[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 8 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_in_hrs[dlink_data_import] = 0
					else
						dl_flights_in_hrs[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 9 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_in_min[dlink_data_import] = 0
					else
						dl_flights_in_min[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 10 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_in_fuel[dlink_data_import] = -1
					else
						dl_flights_in_fuel[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 11 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_on_hrs[dlink_data_import] = 0
					else
						dl_flights_on_hrs[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 12 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_on_min[dlink_data_import] = 0
					else
						dl_flights_on_min[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 13 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_on_fuel[dlink_data_import] = -1
					else
						dl_flights_on_fuel[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 14 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_off_hrs[dlink_data_import] = 0
					else
						dl_flights_off_hrs[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 15 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_off_min[dlink_data_import] = 0
					else
						dl_flights_off_min[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 16 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_off_fuel[dlink_data_import] = -1
					else
						dl_flights_off_fuel[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 17 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_flight_time[dlink_data_import] = 0
					else
						dl_flights_flight_time[dlink_data_import] = temp_fmod
					end
				elseif dlink_data_import2 == 18 then
					temp_fmod = tonumber(fms_line)
					if temp_fmod == nil then
						dl_flights_block_time[dlink_data_import] = 0
					else
						dl_flights_block_time[dlink_data_import] = temp_fmod
					end
					dlink_data_import = dlink_data_import + 1
					dlink_data_import2 = 0
				end
				dlink_data_import2 = dlink_data_import2 + 1
			else
				dlink_data_import = 1
				dlink_data_import2 = 1
			end
			fms_line = file_navdata:read()
		end
		if pressure_panel_set == 0 then
			simDR_press_max_alt = 0
			B738DR_land_alt_knob = 0
		end
		file_navdata:close()
	end
end

function B738_save_status(in_auto)
	
	local fms_line = ""
	local ff = 0
	
	file_name = "Output/preferences/" .. FILE_NAME_STATUS	--"b738x_status.dat"
	file_navdata = io.open(file_name, "w")
	if file_navdata ~= nil then
		-- OTHERS
		fms_line = "*** B737-800X ZIBO MOD ***\n"
		file_navdata:write(fms_line)
		if in_auto ~= 0 then
			if B738DR_autosave ~= 0 then
				fms_line = "**AUTO**\n"
				file_navdata:write(fms_line)
			end
		end
		fms_line = "*** Statusfile         ***\n"
		file_navdata:write(fms_line)
		fms_line = "LAST POS LAT   = " .. tostring(simDR_latitude) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LAST POS LON   = " .. tostring(simDR_longitude) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FUEL TANK LFT  = " .. tostring(simDR_fuel_tank_weight_kg[0]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FUEL TANK RGT  = " .. tostring(simDR_fuel_tank_weight_kg[2]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "FUEL TANK CTR  = " .. tostring(simDR_fuel_tank_weight_kg[1]) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MMR STBY GLS1  = " .. tostring(B738DR_mmr_cpt_stby_gls) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "MMR STBY GLS2  = " .. tostring(B738DR_mmr_fo_stby_gls) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "CRUISE ALT     = " .. tostring(simDR_press_max_alt) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "LANDING ALT    = " .. tostring(land_alt) .. "\n"
		file_navdata:write(fms_line)
		fms_line = "SQUAWK         = " .. string.format("%04d", simDR_squawk) .. "\n"
		file_navdata:write(fms_line)
		
		fms_line = "DLINK DATA" .. "\n"
		file_navdata:write(fms_line)
		for iii = 1, 4 do
			fms_line = dl_flights_ref[iii] .. "\n"
			file_navdata:write(fms_line)
			fms_line = dl_flights_des[iii] .. "\n"
			file_navdata:write(fms_line)
			fms_line = dl_flights_flt_num[iii] .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_day[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_out_hrs[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_out_min[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_out_fuel[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_in_hrs[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_in_min[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_in_fuel[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_on_hrs[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_on_min[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_on_fuel[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_off_hrs[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_off_min[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_off_fuel[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_flight_time[iii]) .. "\n"
			file_navdata:write(fms_line)
			fms_line = tostring(dl_flights_block_time[iii]) .. "\n"
			file_navdata:write(fms_line)
		end
		file_navdata:close()
	end
end

--dump_legsk
function dump_leg()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		file_navdata2:write(tostring(legs_num2) .. "\n")
		if legs_num2 > 0 then
			for vvv = 1, legs_num2 + 1 do
				fms_line = legs_data2[vvv][1] .. "," .. legs_data2[vvv][16]  .. ",>" .. legs_data2[vvv][9] .. "<," .. legs_data2[vvv][31] .. ","
				fms_line = fms_line .. tostring(legs_data2[vvv][7]) .. "," .. tostring(legs_data2[vvv][8]) .. "," 
				fms_line = fms_line .. tostring(math.deg(legs_data2[vvv][2])) .. "," .. tostring(legs_data2[vvv][32]) .. "\n"
				--fms_line = legs_data2[vvv][1] .. "\n"
				file_navdata2:write(fms_line)
			end
			fms_line = "Offset = " .. tostring(offset)
			file_navdata2:write(fms_line)
			file_navdata2:write("\n\n")
		end
		file_navdata2:close()
	end
end

function dump_vnav_tab()
	local vvv = 0
	local fms_line = ""
	local calc_ang = 0
	local calc_ang_alt = 0
	local calc_ang_dist = 0
	
	local file_name2 = "legs.txt"
	--local file_navdata2 = io.open(file_name2, "w")
	local file_navdata2 = io.open(file_name2, "a")
	
	local vnav_tab_num = ed_fix_num + 1
	
	if file_navdata2 ~= nil then
		B738DR_fms_test = 500
		file_navdata2:write("\n")
		for hhh = 1, vnav_tab_num do
			fms_line = "Table: " .. tostring(hhh) .. "\n"
			file_navdata2:write(fms_line)
			if vnav_des_table_num[hhh] > 0 then
				for vvv = 1, vnav_des_table_num[hhh] do
					if vvv < vnav_des_table_num[hhh] then
						calc_ang_alt = (vnav_des_table_alt[hhh][vvv+1] - vnav_des_table_alt[hhh][vvv]) * 0.3048
						calc_ang_dist = (vnav_des_table_dist[hhh][vvv+1] - vnav_des_table_dist[hhh][vvv]) * 1852
						if calc_ang_dist == 0 then
							calc_ang = 0
						else
							calc_ang = math.atan(calc_ang_alt/calc_ang_dist)
							calc_ang = math.deg(calc_ang)
						end
					else
						calc_ang = 0
					end
					fms_line = tostring(vnav_des_table_dist[hhh][vvv]) .. "," .. tostring(vnav_des_table_alt[hhh][vvv]) .. ","
					fms_line = fms_line .. tostring(vnav_des_table_spd[hhh][vvv]) .. "," .. tostring(calc_ang) .. ","
					fms_line = fms_line .. tostring(vnav_des_table_flap[hhh][vvv]) .. "," .. tostring(vnav_des_table_act_spd[hhh][vvv]) .. ","
					if vnav_des_table_type[hhh][vvv] > 0 and vnav_des_table_type[hhh][vvv] < 1000 then
						fms_line = fms_line .. "WPT " .. legs_data[vnav_des_table_type[hhh][vvv]][1] .. " " .. tostring(vnav_des_table_type[hhh][vvv]) .. "\n"
					elseif vnav_des_table_type[hhh][vvv] == 9000 then
						fms_line = fms_line .. "KTS/MACH " .. tostring(vnav_des_table_type[hhh][vvv]) .. "\n"
					elseif vnav_des_table_type[hhh][vvv] == 0 then
						fms_line = fms_line .. "NEW ALT " .. "\n"	--.. tostring(vnav_des_table_type[hhh][vvv]) .. "\n"
					elseif vnav_des_table_type[hhh][vvv] >= 5000 then
						fms_line = fms_line .. "FLAP " .. tostring(vnav_des_table_type[hhh][vvv]) .. "\n"
					else
						fms_line = fms_line .. "DECEL " .. tostring(vnav_des_table_type[hhh][vvv]) .. "\n"
					end
					file_navdata2:write(fms_line)
				end
			end
		end
		B738DR_fms_test = 501
		-- descent wind data
		if desc_wind_tab_num > 0 then
			file_navdata2:write("\n")
			file_navdata2:write("Descent wind data: (alt, dir, spd)\n")
			for hhh = 1, desc_wind_tab_num do
				fms_line = string.format("%5d",tostring(desc_wind_alt_tab[hhh])) .. "," .. string.format("%03d",tostring(desc_wind_dir_tab[hhh])) .. ","
				fms_line = fms_line .. string.format("%3d",tostring(desc_wind_spd_tab[hhh])) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		B738DR_fms_test = 502
		if ed_found ~= 0 then
			fms_line = "\nDescent table:\n"
			file_navdata2:write(fms_line)
			fms_line = legs_data[ed_found][1] .. "," .. tonumber(ed_found) .. "," .. tonumber(ed_alt) .. "," .. tonumber(ed_vpa) .. "," .. tonumber(ed_type) .. "\n"
			file_navdata2:write(fms_line)
			if ed_fix_num > 0 then
				for hhh = ed_fix_num, 1, -1 do
					fms_line = legs_data[ed_fix_found2[hhh]][1] .. "," .. tonumber(ed_fix_found2[hhh]) .. "," .. tonumber(ed_fix_alt2[hhh]) .. "," .. tonumber(ed_fix_vpa2[hhh])
					fms_line = fms_line  .. "," .. tonumber(ed_fix_type[hhh]) .. ","  .. tonumber(ed_fix_tl[hhh]) .. ","  .. tonumber(ed_fix_distance[hhh]) .. "\n"
					file_navdata2:write(fms_line)
				end
			end
		end
		B738DR_fms_test = 503
		file_navdata2:close()
	end
	
	--B738DR_fms_test = 0
end

function dump_leg_mod()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if legs_num > 0 then
			for vvv = 1, legs_num + 1 do
				fms_line = tostring(vvv) .. "," .. legs_data[vvv][1] .. "," .. tostring(math.deg(legs_data[vvv][2])) .. "," .. tostring(math.deg(legs_data[vvv][18])) .. "," .. tostring(legs_data[vvv][5]) .. ","
				if legs_data[vvv][6] == 43 then
					fms_line = fms_line .. "A,"
				elseif legs_data[vvv][6] == 45 then
					fms_line = fms_line .. "B,"
				elseif legs_data[vvv][6] == 41 then 	-- Between
					fms_line = fms_line .. "A," .. tostring(legs_data[vvv][41]) .. ", B,"
				else
					fms_line = fms_line .. " ,"
				end
				fms_line = fms_line .. tostring(legs_data[vvv][11]) --.. "\n"
				fms_line = fms_line .. " -> " .. legs_data[vvv][38] .. " *** " .. legs_data[vvv][39] .. " IDX: " .. tostring(legs_data[vvv][44])-- .. "\n"
				fms_line = fms_line .. ", FUEL P> " .. tostring(legs_data[vvv][40])
				fms_line = fms_line .. " / R> " .. tostring(legs_data[vvv][15])
				fms_line = fms_line .. " / D> " .. tostring(legs_data[vvv][40] - legs_data[vvv][15]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		
		if ed_fix_num > 0 then
			fms_line = "\n FIXs:\n"
			file_navdata2:write(fms_line)
			for vvv = 1, ed_fix_num do
				fms_line = legs_data[ed_fix_found2[vvv]][1] .. "," .. tostring(ed_fix_found2[vvv]) .. ","
				fms_line = fms_line .. tostring(ed_fix_alt2[vvv]) .. "," .. tostring(ed_fix_vpa2[vvv]) .. "," .. tostring(ed_fix_type[vvv]) .. ","
				fms_line = fms_line .. tostring(ed_fix_distance[vvv]) .. "," .. tostring(ed_fix_tl[vvv]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		
		if first_app_idx ~= 0 then
			fms_line = "\n IAF:\n" .. tostring(first_app_idx) .. "," .. legs_data[first_app_idx][1] .. "\n"
			file_navdata2:write(fms_line)
		end
		
		if ed_found ~= 0 then
			fms_line = "\n FAF:\n" .. tostring(ed_found) .. "," .. legs_data[ed_found][1] .. ","
			fms_line = fms_line .. tostring(ed_alt) .. "," .. tostring(ed_vpa) .. "," .. tostring(ed_type) .. "\n\n"
			file_navdata2:write(fms_line)
		end
		
		if legs_num > 0 then
			for vvv = 1, legs_num + 1 do
				fms_line = tostring(vvv) .. "," .. legs_data[vvv][1] .. "," .. tostring(legs_data[vvv][3]) .. ","
				fms_line = fms_line .. tostring(legs_data[vvv][10]) .. "," .. tostring(legs_data[vvv][4])
				if legs_data[vvv][42] == 1 then
					fms_line = fms_line .. "B,"
				elseif legs_data[vvv][42] == 2 then
					fms_line = fms_line .. "A,"
				else
					fms_line = fms_line .. ","
				end
				fms_line = fms_line .. string.sub(time_to_string(legs_data[vvv][13]), 1, 4)
				fms_line = fms_line .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		
		file_navdata2:close()
	end
	--B738DR_fms_test = 0
end

function dump_leg_mod2()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if legs_num2 > 0 then
			for vvv = 1, legs_num2 + 1 do
				fms_line = tostring(vvv) .. "," .. legs_data2[vvv][1] .. "," .. tostring(math.deg(legs_data2[vvv][2])) .. "," .. tostring(math.deg(legs_data2[vvv][18])) .. "," .. tostring(legs_data2[vvv][5]) .. ","
				if legs_data2[vvv][6] == 43 then
					fms_line = fms_line .. "A,"
				elseif legs_data2[vvv][6] == 45 then
					fms_line = fms_line .. "B,"
				elseif legs_data2[vvv][6] == 41 then 	-- Between
					fms_line = fms_line .. "A," .. tostring(legs_data2[vvv][41]) .. ", B,"
				else
					fms_line = fms_line .. " ,"
				end
				fms_line = fms_line .. tostring(legs_data2[vvv][11]) --.. "\n"
				fms_line = fms_line .. " -> " .. legs_data2[vvv][38] .. " *** " .. legs_data2[vvv][39] .. " IDX: " .. tostring(legs_data2[vvv][44]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		
		
		file_navdata2:close()
	end
	--B738DR_fms_test = 0
end

function dump_leg_append(data_str)
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		fms_line = data_str
		file_navdata2:write(fms_line)
		file_navdata2:close()
	end
	--B738DR_fms_test = 0
end



function dump_leg4()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if legs_num > 0 then
			for vvv = 1, legs_num + 1 do
				fms_line = legs_data[vvv][1] .. "," .. legs_data[vvv][16] .. "," .. legs_data[vvv][31] .. ","
				fms_line = fms_line .. tostring(legs_data[vvv][7]) .. "," .. tostring(legs_data[vvv][8]) .. ","
				fms_line = fms_line .. tostring(legs_data[vvv][5]) .. "," .. tostring(legs_data[vvv][21]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end


function dump_leg6()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if rte_sid_num > 0 then
			for vvv = 1, rte_sid_num do
				fms_line = rte_sid[vvv][1] .. "," .. rte_sid[vvv][2] .. ","
				fms_line = fms_line .. rte_sid[vvv][3] .. "," .. rte_sid[vvv][30] .. "\n" --.. "," .. legs_data[vvv][9] .. ","
				--fms_line = fms_line .. tostring(legs_data[vvv][7]) .. "," .. tostring(legs_data[vvv][8]) .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end


function dump_ed_fix()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if ed_fix_num > 0 then
			for vvv = 1, ed_fix_num do
				fms_line = tostring(ed_fix_found2[vvv]) .. "," .. tostring(ed_fix_alt2[vvv]) .. ","
				fms_line = fms_line .. tostring(ed_fix_vpa2[vvv])  .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

function dump_leg_temp()
	local vvv = 0
	local fms_line = ""
	
	local file_name2 = "legs.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if legs_data2_tmp_n > 0 then
			for vvv = 1, legs_data2_tmp_n do
				fms_line = legs_data2_tmp[vvv][1] .. "\n"
				file_navdata2:write(fms_line)
			end
		end
		file_navdata2:close()
	end
end

-- function dump_leg3()
	-- local vvv = 0
	-- local fms_line = ""
	
	-- local file_name2 = "legs.txt"
	-- local file_navdata2 = io.open(file_name2, "w")
	
	-- if file_navdata2 ~= nil then
		-- if legs_num3 > 0 then
			-- for vvv = 1, legs_num3 + 1 do
				-- fms_line = legs_data3[vvv][1] .. "," .. legs_data3[vvv][16] .. "," .. legs_data3[vvv][9] .. ","
				-- fms_line = fms_line .. "" .. "," .. "" .. "\n"
				-- --fms_line = legs_data2[vvv][1] .. "\n"
				-- file_navdata2:write(fms_line)
			-- end
		-- end
		-- file_navdata2:close()
	-- end
-- end


function dump_log()
	local vvv = 0
	local vvvv = 0
	local fms_line = ""
	
	local file_name2 = "B738X_log.txt"
	local file_navdata2 = io.open(file_name2, "w")
	
	if file_navdata2 ~= nil then
		if awy_data_num > 10 then
			for vvv = 1, 10 do
				fms_line = awy_data[vvv][1] .. "," .. awy_data[vvv][2] .. "," .. awy_data[vvv][3] .. ","
				fms_line = fms_line .. awy_data[vvv][4] .. "," .. awy_data[vvv][5] .. "\n"
				file_navdata2:write(fms_line)
				fms_line = ""
				if string.len(awy_data[vvv][1]) > 0 then
					for vvvv = 1, string.len(awy_data[vvv][1]) do
						fms_line = fms_line .. tostring(string.byte(awy_data[vvv][1], vvvv)) .. "."
					end
					fms_line = fms_line .. "\n"
					file_navdata2:write(fms_line)
				end
			end
		end
		file_navdata2:close()
	end
end

function B738_find_rnav()
	
	local ii = 0
	local jj = 0
	local kk = 0
	local mm = 0
	local nn = 0
	local pom_dist = 0
	
	local fms_line = ""
	
	local calc_vpa = 0
	local pom_alt = 0
	local last_offset_tmp = offset
	
	id_ed = ""
	
	-- local last_offset_tmp = offset
	offset = math.min(offset, legs_num)
	offset = math.max(offset, 1)
	
	if last_offset_tmp ~= offset then
		if offset_seq < offset or offset_seq > legs_num then
			offset_seq = offset
		end
	end
	
	-- legs
	idx_ed = 0
	rnav_idx_first = 0
	rnav_idx_last = 0
	first_miss_app_idx = 0
	last_miss_app_idx = 0
	missed_app_via = ""
	B738DR_missed_app_act = 0
	rnav_alt = 0
	rnav_vpa = 0
	gp_available = 0
	idx_iaf = 0
	
	if legs_num > 1 then
		for ii = 1, legs_num + 1 do
			if ii <= legs_num then
				if legs_data[ii][17] == 1 and idx_ed == 0 then	-- FAF wpt
					idx_ed = ii
					if legs_data[ii][20] < 0 and ii > 1 then
						if legs_data[ii-1][5] > legs_data[ii][5] and legs_data[ii][5] ~= 0 and legs_data[ii-1][17] == 5 then
							idx_ed = ii-1
							
						end
					end
					id_ed = legs_data[idx_ed][1]
					alt_ed = legs_data[idx_ed][5]
					alt_type_ed = legs_data[idx_ed][6]
					-- find first and last RNAV wpt
					rnav_idx_first = idx_ed
					if rnav_idx_first > legs_num then
						rnav_idx_first = 0
					else
						rnav_alt = legs_data[rnav_idx_first][5]
						rnav_vpa = legs_data[rnav_idx_first][20]
						rnav_idx_last = rnav_idx_first
						jj = rnav_idx_last + 1
						while jj <= legs_num do
							if legs_data[jj][20] < 0 then	--vpa < 0
								rnav_idx_last = jj
								rnav_alt = legs_data[rnav_idx_last][5]
								rnav_vpa = legs_data[rnav_idx_last][20]
							else
								break
							end
							jj = jj + 1
						end
					end
					
					if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 and idx_ed ~= 0 and rnav_idx_last > rnav_idx_first then
						if legs_data[idx_ed][5] == 0 or legs_data[idx_ed][6] == 43 or legs_data[idx_ed][6] == 2 then
							-- find last rnav idx with alt
							nn = rnav_idx_last
							for mm = rnav_idx_first, rnav_idx_last do
								if legs_data[mm][5] ~= 0 and (legs_data[mm][6] == 0 or legs_data[mm][6] == 32) and legs_data[mm][20] < 0 then
									nn = mm
								end
							end
							
							--calc distance
							pom_dist = 0
							for mm = nn, idx_ed + 1, -1 do
								pom_dist = pom_dist + legs_data[mm][3]
							end
							
							--calc alt
							if legs_data[nn][20] >= 0 then
								pom_alt = (math.tan(math.rad(3)) * pom_dist * 6076.11549) + legs_data[nn][5]
								if legs_data[idx_ed][5] == 0 then
									alt_ed = pom_alt
								elseif pom_alt > legs_data[idx_ed][5] then
									alt_ed = pom_alt
								end
							else
								pom_alt = (math.tan(math.rad(-legs_data[nn][20])) * pom_dist * 6076.11549) + legs_data[nn][5]
								if legs_data[idx_ed][5] == 0 then
									alt_ed = pom_alt
								elseif pom_alt > legs_data[idx_ed][5] then
									alt_ed = pom_alt
								end
							end
						else
							if legs_data[rnav_idx_last][20] >= 0 then
								--none vpa
								
								--calc distance
								pom_dist = 0
								for mm = rnav_idx_last, idx_ed + 1, -1 do
									pom_dist = pom_dist + legs_data[mm][3]
								end
								
								if pom_dist > 0 then
									calc_vpa = -math.deg(math.atan((alt_ed - legs_data[nn][5]) / (pom_dist * 6076.11549)))
								end
							end
						end
					end
				end
				
				-- find first Missed approach waypoints
				if legs_data[ii][17] == 2 then	-- FMA wpt
					first_miss_app_idx = ii
					jj = first_miss_app_idx - 1
					missed_app_via = legs_data[ii][9]
					if idx_ed ~= 0 and rnav_idx_last ~= jj and jj > 0 then
						rnav_idx_last = jj
						rnav_alt = legs_data[rnav_idx_last][5]
						if legs_data[rnav_idx_last][20] < 0 then
							rnav_vpa = legs_data[rnav_idx_last][20]
						elseif calc_vpa < 0 then
							rnav_vpa = calc_vpa
						else
							rnav_vpa = -3.0
						end
					elseif idx_ed == 0 then
						-- find rnav_idx's
						while jj <= legs_num and jj > 0 do
							if legs_data[jj][20] < 0 then	--vpa < 0
								if rnav_idx_last == 0 then
									rnav_idx_last = jj
									rnav_alt = legs_data[rnav_idx_last][5]
									rnav_vpa = legs_data[rnav_idx_last][20]
								end
								rnav_idx_first = jj
								idx_ed = jj
							else
								break
							end
							jj = jj - 1
						end
					end
					
					if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 and idx_ed ~= 0 and rnav_idx_last > rnav_idx_first then
						if legs_data[idx_ed][5] == 0 or legs_data[idx_ed][6] == 43 or legs_data[idx_ed][6] == 2 then
							-- find last rnav idx with alt
							nn = rnav_idx_last
							for mm = rnav_idx_first, rnav_idx_last do
								if legs_data[mm][5] ~= 0 and (legs_data[mm][6] == 0 or legs_data[mm][6] == 32) and legs_data[mm][20] < 0 then
									nn = mm
								end
							end
							
							--calc distance
							pom_dist = 0
							for mm = nn, idx_ed + 1, -1 do
								pom_dist = pom_dist + legs_data[mm][3]
							end
							
							--calc alt
							if legs_data[nn][20] >= 0 then
								pom_alt = (math.tan(math.rad(3)) * pom_dist * 6076.11549) + legs_data[nn][5]
								if legs_data[idx_ed][5] == 0 then
									alt_ed = pom_alt
								elseif pom_alt > legs_data[idx_ed][5] then
									alt_ed = pom_alt
								end
							else
								pom_alt = (math.tan(math.rad(-legs_data[nn][20])) * pom_dist * 6076.11549) + legs_data[nn][5]
								if legs_data[idx_ed][5] == 0 then
									alt_ed = pom_alt
								elseif pom_alt > legs_data[idx_ed][5] then
									alt_ed = pom_alt
								end
							end
						end
					end
				end
				if first_miss_app_idx ~= 0 and legs_data[ii][19] >= 7 and legs_data[ii][19] <= 9 then
					last_miss_app_idx = ii
				end
				
				-- find IAF
				if legs_data[ii][17] == 3 and idx_iaf == 0 then	-- IAF wpt
					idx_iaf = ii
				end
			end
		end
	end
	
	local nd_fpa = 3
	if rte12_act == 0 then
		nd_fpa = tonumber(rw_ext_fpa)
	elseif rte12_act == 1 then
		nd_fpa = tonumber(rw_ext_fpa7)
	end
	
	if nd_fpa == nil then
		nd_fpa = 3.00
	end
	if rnav_idx_last > 0 and rnav_idx_last <= legs_num then
			gp_available = 1
			if rnav_vpa == 0 then
				B738DR_glidepath_type = 0	-- none
			else
				B738DR_glidepath_type = 1	-- GS/GP
			end
			if string.sub(des_app, 1, 2) == "RW" then
				rnav_vpa = -nd_fpa
				B738DR_glidepath_type = 1	-- GS/GP !!!!!???????
			end
	else
		if rnav_vpa == 0 then
			B738DR_glidepath_type = 0	-- none
		else
			B738DR_glidepath_type = 2	-- 3-degrees
		end
	end
	
	vnav_update = 1
	
end


function B738_find_rnav_mod(in_rte)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local mm = 0
	local nn = 0
	local pom_dist = 0
	
	local fms_line = ""
	
	if in_rte == 0 then
		-- id_ed_mod = ""
		
		-- legs
		idx_ed_mod = 0
		rnav_idx_first_mod = 0
		rnav_idx_last_mod = 0
		first_miss_app_idx_mod = 0
		-- last_miss_app_idx_mod = 0
		rnav_alt_mod = 0
		rnav_vpa_mod = 0
		-- alt_ed_mod = 0
		-- alt_type_ed_mod = 0
		-- idx_iaf_mod = 0
		
		if legs_num2 > 1 then
			for ii = 1, legs_num2 + 1 do
				if ii <= legs_num2 then
					
					
					
					if legs_data2[ii][17] == 1 and idx_ed_mod == 0 then	-- FAF wpt
						idx_ed_mod = ii
						if legs_data2[ii][20] < 0 and ii > 1 then
							if legs_data2[ii-1][5] > legs_data2[ii][5] and legs_data2[ii][5] ~= 0 and legs_data2[ii-1][17] == 5 then
								idx_ed_mod = ii-1
								
							end
						end
						-- id_ed_mod = legs_data2[idx_ed_mod][1]
						-- alt_ed_mod = legs_data2[idx_ed_mod][5]
						-- alt_type_ed_mod = legs_data2[idx_ed_mod][6]
						-- find first and last RNAV wpt
						rnav_idx_first_mod = idx_ed_mod
						if rnav_idx_first_mod > legs_num2 then
							rnav_idx_first_mod = 0
						else
							rnav_alt_mod = legs_data2[rnav_idx_first_mod][5]
							rnav_vpa_mod = legs_data2[rnav_idx_first_mod][20]
							rnav_idx_last_mod = rnav_idx_first_mod
							jj = rnav_idx_last_mod + 1
							while jj <= legs_num2 do
								if legs_data2[jj][20] < 0 then	--vpa < 0
									rnav_idx_last_mod = jj
									rnav_alt_mod = legs_data2[rnav_idx_last_mod][5]
									rnav_vpa_mod = legs_data2[rnav_idx_last_mod][20]
								else
									break
								end
								jj = jj + 1
							end
						end
						
						if rnav_idx_first_mod ~= 0 and rnav_idx_last_mod ~= 0 and idx_ed_mod ~= 0 and rnav_idx_last_mod > rnav_idx_first_mod then
							if legs_data2[idx_ed_mod][5] == 0 or legs_data2[idx_ed_mod][6] == 43 or legs_data2[idx_ed_mod][6] == 2 then
								-- find last rnav idx with alt
								nn = rnav_idx_last_mod
								for mm = rnav_idx_first_mod, rnav_idx_last_mod do
									if legs_data2[mm][5] ~= 0 and (legs_data2[mm][6] == 0 or legs_data2[mm][6] == 32) and legs_data2[mm][20] < 0 then
										nn = mm
									end
								end
								
								--calc distance
								pom_dist = 0
								for mm = nn, idx_ed_mod + 1, -1 do
									pom_dist = pom_dist + legs_data2[mm][3]
								end
								
								--calc alt
								-- if legs_data2[nn][20] >= 0 then
									-- alt_ed_mod = (math.tan(math.rad(3)) * pom_dist * 6076.11549) + legs_data2[nn][5]
								-- else
									-- alt_ed_mod = (math.tan(math.rad(-legs_data2[nn][20])) * pom_dist * 6076.11549) + legs_data2[nn][5]
								-- end
							end
						end
					end
					
					-- find first Missed approach waypoints
					if legs_data2[ii][17] == 2 then	-- FMA wpt
						first_miss_app_idx_mod = ii
						jj = first_miss_app_idx_mod - 1
						missed_app_via = legs_data2[ii][9]
						if idx_ed_mod ~= 0 and rnav_idx_last_mod ~= jj and jj > 0 then
							rnav_idx_last_mod = jj
							rnav_alt_mod = legs_data2[rnav_idx_last_mod][5]
							if legs_data2[rnav_idx_last_mod][20] < 0 then
								rnav_vpa_mod = legs_data2[rnav_idx_last_mod][20]
							else
								rnav_vpa_mod = -3.0
							end
						elseif idx_ed_mod == 0 then
							-- find rnav_idx's
							while jj <= legs_num2 and jj > 0 do
								if legs_data2[jj][20] < 0 then	--vpa < 0
									if rnav_idx_last_mod == 0 then
										rnav_idx_last_mod = jj
										rnav_alt_mod = legs_data2[rnav_idx_last_mod][5]
										rnav_vpa_mod = legs_data2[rnav_idx_last_mod][20]
									end
									rnav_idx_first_mod = jj
									idx_ed_mod = jj
								else
									break
								end
								jj = jj - 1
							end
						end
						
						if rnav_idx_first_mod ~= 0 and rnav_idx_last_mod ~= 0 and idx_ed_mod ~= 0 and rnav_idx_last_mod > rnav_idx_first_mod then
							if legs_data2[idx_ed_mod][5] == 0 or legs_data2[idx_ed_mod][6] == 43 or legs_data2[idx_ed_mod][6] == 2 then
								-- find last rnav idx with alt
								nn = rnav_idx_last_mod
								for mm = rnav_idx_first_mod, rnav_idx_last_mod do
									if legs_data2[mm][5] ~= 0 and (legs_data2[mm][6] == 0 or legs_data2[mm][6] == 32) and legs_data2[mm][20] < 0 then
										nn = mm
									end
								end
								
								--calc distance
								pom_dist = 0
								for mm = nn, idx_ed_mod + 1, -1 do
									pom_dist = pom_dist + legs_data2[mm][3]
								end
								
								--calc alt
								-- if legs_data2[nn][20] >= 0 then
									-- alt_ed_mod = (math.tan(math.rad(3)) * pom_dist * 6076.11549) + legs_data2[nn][5]
								-- else
									-- alt_ed_mod = (math.tan(math.rad(-legs_data2[nn][20])) * pom_dist * 6076.11549) + legs_data2[nn][5]
								-- end
							end
						end
					end
					-- if first_miss_app_idx_mod ~= 0 and legs_data2[ii][19] >= 7 and legs_data2[ii][19] <= 9 then
						-- last_miss_app_idx_mod = ii
					-- end
					
					-- find IAF
					-- if legs_data2[ii][17] == 3 and idx_iaf_mod == 0 then	-- IAF wpt
						-- idx_iaf_mod = ii
					-- end
				end
				
			end
		end
		
		vnav_update_mod = 1
	elseif in_rte == 1 then
		-- id_ed_mod = ""
		
		-- legs
		idx_ed_mod2 = 0
		rnav_idx_first_mod2 = 0
		rnav_idx_last_mod2 = 0
		first_miss_app_idx_mod2 = 0
		-- last_miss_app_idx_mod = 0
		rnav_alt_mod2 = 0
		rnav_vpa_mod2 = 0
		-- alt_ed_mod = 0
		-- alt_type_ed_mod = 0
		-- idx_iaf_mod = 0
		
		if legs_num8 > 1 then
			for ii = 1, legs_num8 + 1 do
				if ii <= legs_num8 then
					
					
					
					if legs_data8[ii][17] == 1 and idx_ed_mod2 == 0 then	-- FAF wpt
						idx_ed_mod2 = ii
						if legs_data8[ii][20] < 0 and ii > 1 then
							if legs_data8[ii-1][5] > legs_data8[ii][5] and legs_data8[ii][5] ~= 0 and legs_data8[ii-1][17] == 5 then
								idx_ed_mod2 = ii-1
								
							end
						end
						-- id_ed_mod = legs_data8[idx_ed_mod2][1]
						-- alt_ed_mod = legs_data8[idx_ed_mod2][5]
						-- alt_type_ed_mod = legs_data8[idx_ed_mod2][6]
						-- find first and last RNAV wpt
						rnav_idx_first_mod2 = idx_ed_mod2
						if rnav_idx_first_mod2 > legs_num8 then
							rnav_idx_first_mod2 = 0
						else
							rnav_alt_mod2 = legs_data8[rnav_idx_first_mod2][5]
							rnav_vpa_mod2 = legs_data8[rnav_idx_first_mod2][20]
							rnav_idx_last_mod2 = rnav_idx_first_mod2
							jj = rnav_idx_last_mod2 + 1
							while jj <= legs_num8 do
								if legs_data8[jj][20] < 0 then	--vpa < 0
									rnav_idx_last_mod2 = jj
									rnav_alt_mod2 = legs_data8[rnav_idx_last_mod2][5]
									rnav_vpa_mod2 = legs_data8[rnav_idx_last_mod2][20]
								else
									break
								end
								jj = jj + 1
							end
						end
						
						if rnav_idx_first_mod2 ~= 0 and rnav_idx_last_mod2 ~= 0 and idx_ed_mod2 ~= 0 and rnav_idx_last_mod2 > rnav_idx_first_mod2 then
							if legs_data8[idx_ed_mod2][5] == 0 or legs_data8[idx_ed_mod2][6] == 43 or legs_data8[idx_ed_mod2][6] == 2 then
								-- find last rnav idx with alt
								nn = rnav_idx_last_mod2
								for mm = rnav_idx_first_mod2, rnav_idx_last_mod2 do
									if legs_data8[mm][5] ~= 0 and (legs_data8[mm][6] == 0 or legs_data8[mm][6] == 32) and legs_data8[mm][20] < 0 then
										nn = mm
									end
								end
								
								--calc distance
								pom_dist = 0
								for mm = nn, idx_ed_mod2 + 1, -1 do
									pom_dist = pom_dist + legs_data8[mm][3]
								end
								
								--calc alt
								-- if legs_data8[nn][20] >= 0 then
									-- alt_ed_mod = (math.tan(math.rad(3)) * pom_dist * 6076.11549) + legs_data8[nn][5]
								-- else
									-- alt_ed_mod = (math.tan(math.rad(-legs_data8[nn][20])) * pom_dist * 6076.11549) + legs_data8[nn][5]
								-- end
							end
						end
					end
					
					-- find first Missed approach waypoints
					if legs_data8[ii][17] == 2 then	-- FMA wpt
						first_miss_app_idx_mod2 = ii
						jj = first_miss_app_idx_mod2 - 1
						missed_app_via = legs_data8[ii][9]
						if idx_ed_mod2 ~= 0 and rnav_idx_last_mod2 ~= jj and jj > 0 then
							rnav_idx_last_mod2 = jj
							rnav_alt_mod2 = legs_data8[rnav_idx_last_mod2][5]
							if legs_data8[rnav_idx_last_mod2][20] < 0 then
								rnav_vpa_mod2 = legs_data8[rnav_idx_last_mod2][20]
							else
								rnav_vpa_mod2 = -3.0
							end
						elseif idx_ed_mod2 == 0 then
							-- find rnav_idx's
							while jj <= legs_num8 and jj > 0 do
								if legs_data8[jj][20] < 0 then	--vpa < 0
									if rnav_idx_last_mod2 == 0 then
										rnav_idx_last_mod2 = jj
										rnav_alt_mod2 = legs_data8[rnav_idx_last_mod2][5]
										rnav_vpa_mod2 = legs_data8[rnav_idx_last_mod2][20]
									end
									rnav_idx_first_mod2 = jj
									idx_ed_mod2 = jj
								else
									break
								end
								jj = jj - 1
							end
						end
						
						if rnav_idx_first_mod2 ~= 0 and rnav_idx_last_mod2 ~= 0 and idx_ed_mod2 ~= 0 and rnav_idx_last_mod2 > rnav_idx_first_mod2 then
							if legs_data8[idx_ed_mod2][5] == 0 or legs_data8[idx_ed_mod2][6] == 43 or legs_data8[idx_ed_mod2][6] == 2 then
								-- find last rnav idx with alt
								nn = rnav_idx_last_mod2
								for mm = rnav_idx_first_mod2, rnav_idx_last_mod2 do
									if legs_data8[mm][5] ~= 0 and (legs_data8[mm][6] == 0 or legs_data8[mm][6] == 32) and legs_data8[mm][20] < 0 then
										nn = mm
									end
								end
								
								--calc distance
								pom_dist = 0
								for mm = nn, idx_ed_mod2 + 1, -1 do
									pom_dist = pom_dist + legs_data8[mm][3]
								end
								
								--calc alt
								-- if legs_data8[nn][20] >= 0 then
									-- alt_ed_mod = (math.tan(math.rad(3)) * pom_dist * 6076.11549) + legs_data8[nn][5]
								-- else
									-- alt_ed_mod = (math.tan(math.rad(-legs_data8[nn][20])) * pom_dist * 6076.11549) + legs_data8[nn][5]
								-- end
							end
						end
					end
					-- if first_miss_app_idx_mod ~= 0 and legs_data8[ii][19] >= 7 and legs_data8[ii][19] <= 9 then
						-- last_miss_app_idx_mod = ii
					-- end
					
					-- find IAF
					-- if legs_data8[ii][17] == 3 and idx_iaf_mod == 0 then	-- IAF wpt
						-- idx_iaf_mod = ii
					-- end
				end
				
			end
		end
		
		vnav_update_mod = 1
	end
end


---------------------------------------------------------------

function apt_exist(eee)
	
	local ii = 0
	local res = false
	local nd_x = 0
	local idx_rec = 0
	
	icao_latitude = 0
	icao_longitude = 0
	
	if apt_data_num > 0 and string.len(eee) > 0 then
		nd_x = string.byte(string.sub(eee, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_apt[nd_x][99999] > 0 then
				for ii = 1, idx_apt[nd_x][99999] do
					idx_rec = idx_apt[nd_x][ii]
					if eee == apt_data[idx_rec][1] then
						icao_latitude = apt_data[idx_rec][2]
						icao_longitude = apt_data[idx_rec][3]
						icao_tns_alt = apt_data[idx_rec][4]
						if apt_data[idx_rec][5] == 0 and apt_data[idx_rec][4] ~= 0 then
							icao_tns_lvl = apt_data[idx_rec][4] + 1000
						else
							icao_tns_lvl = apt_data[idx_rec][5]
						end
						res = true
						break
					end
				end
			end
		end
	end
	return res
	
end

function apt_exist2(eee)
	-- only check APT
	local ii = 0
	local res = false
	local nd_x = 0
	local idx_rec = 0
	
	if apt_data_num > 0 and string.len(eee) > 0 then
		nd_x = string.byte(string.sub(eee, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_apt[nd_x][99999] > 0 then
				for ii = 1, idx_apt[nd_x][99999] do
					idx_rec = idx_apt[nd_x][ii]
					if eee == apt_data[idx_rec][1] then
						res = true
						break
					end
				end
			end
		end
	end
	return res
	
end


function rnw_exist(eee, rrr)
	
	local ii = 0
	local res = false
	local nd_x = 0
	local idx_rec = 0
	
	if rnw_data_num > 0 and string.len(eee) > 0 then
		nd_x = string.byte(string.sub(eee, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if eee == rnw_data[idx_rec][1] and rrr == rnw_data[idx_rec][2] then
						res = true
						break
					end
				end
			end
		end
	end
	return res
	
end

---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------

-- input mode: 0-xxx, 1-xxx/
function entry_spd_kts(input_str, input_min, input_max, input_mode)
	
	local str_len = string.len(input_str)
	local num_val = 0
	local result_output = nil
	local spd_rest_type = 0
	output_str = ""
	
	if input_mode == 0 then
		-- format xxx, xxxB
		if str_len == 3 then
			num_val = tonumber(input_str)
			if num_val ~= nil then
				if num_val >= input_min and num_val <=input_max then
					result_output = num_val
				end
			end
		elseif str_len == 4 then
			if string.sub(input_str, 4, 4) == "B" then
				num_val = tonumber(string.sub(input_str, 1, 3))
				if num_val ~= nil then
					if num_val >= input_min and num_val <=input_max then
						result_output = num_val
						spd_rest_type = 1
					end
				end
			elseif string.sub(input_str, 4, 4) == "A" then
				num_val = tonumber(string.sub(input_str, 1, 3))
				if num_val ~= nil then
					if num_val >= input_min and num_val <=input_max then
						result_output = num_val
						spd_rest_type = 2
					end
				end
			end
		end
	else
		-- format xxx/, xxxB/
		if str_len == 4 then
			if string.sub(input_str, -1, -1) == "/" then
				num_val = tonumber(string.sub(input_str, 1, -2))
				if num_val ~= nil then
					if num_val >= input_min and num_val <=input_max then
						result_output = num_val
					end
				end
			end
		elseif str_len == 5 then
			if string.sub(input_str, -1, -1) == "/" then
				if string.sub(input_str, 4, 4) == "B" then
					num_val = tonumber(string.sub(input_str, 1, 3))
					if num_val ~= nil then
						if num_val >= input_min and num_val <=input_max then
							result_output = num_val
							spd_rest_type = 1
						end
					end
				elseif string.sub(input_str, 4, 4) == "A" then
					num_val = tonumber(string.sub(input_str, 1, 3))
					if num_val ~= nil then
						if num_val >= input_min and num_val <=input_max then
							result_output = num_val
							spd_rest_type = 2
						end
					end
				end
			end
		end
	end
	
	if result_output ~= nil then
		output_str = string.format("%3d", result_output)
		if spd_rest_type == 1 then
			output_str = output_str .. "B"
		elseif spd_rest_type == 2 then
			output_str = output_str .. "A"
		end
	end
	return result_output
	
end

-- mode: 0-without Above/Below, 1-with Above/Below
-- alttns: altitude/level transition
-- format LVL: xxx, FLxxx / xxxA, FLxxxA
-- format ALT: xxxxx / xxxxxA / xxxx / xxxxA
function entry_alt(input_str, input_min, input_max, input_mode, input_alttns)
	
	local str_len = string.len(input_str)
	local num_val = nil
	local str_type = ""
	local result_output = nil
	output_str = ""
	
	if str_len == 3 and input_mode == 1 then
		-- format xxA
		str_type = string.sub(input_str, -1, -1)
		if str_type == "A" or str_type == "B" then
			num_val = tonumber(string.sub(input_str, 1, -2)) * 100
			if num_val ~= nil then
				if num_val < input_min or num_val > input_max then
					num_val = nil
				end
			end
		else
			-- format xxx
			num_val = tonumber(input_str) * 100
			if num_val ~= nil then
				if num_val < input_min or num_val > input_max then
					num_val = nil
				end
			end
		end
	elseif str_len == 2 then
		-- format xx
		num_val = tonumber(input_str) * 100
		if num_val ~= nil then
			if num_val < input_min or num_val > input_max then
				num_val = nil
			end
		end
	elseif str_len == 3 then
		-- format xxx
		num_val = tonumber(input_str) * 100
		if num_val ~= nil then
			if num_val < input_min or num_val > input_max then
				num_val = nil
			end
		end
	elseif str_len == 4 and input_mode == 1 then
		-- format xxxA
		str_type = string.sub(input_str, -1, -1)
		if str_type == "A" or str_type == "B" then
			num_val = tonumber(string.sub(input_str, 1, -2)) * 100
			if num_val ~= nil then
				if num_val < input_min or num_val > input_max then
					num_val = nil
				end
			end
		else
			--format xxxx
			num_val = tonumber(input_str)
			if num_val ~= nil then
				if num_val < input_min or num_val > input_max then
					num_val = nil
				end
			end
		end
	elseif str_len == 4 then
		-- format xxxx
		num_val = tonumber(input_str)
		if num_val ~= nil then
			if num_val < input_min or num_val > input_max then
				num_val = nil
			end
		end
	elseif str_len == 5 then
		if string.sub(input_str, 1, 2) == "FL" then
			-- format FLxxx
			num_val = tonumber(string.sub(input_str, 3, -1)) * 100
			if num_val ~= nil then
				if num_val < input_min or num_val > input_max then
					num_val = nil
				end
			end
		else
			str_type = string.sub(input_str, -1, -1)
			if (str_type == "A" or str_type == "B") and input_mode == 1 then
				-- format xxxxA
				num_val = tonumber(string.sub(input_str, 1, -2))
				if num_val ~= nil then
					if num_val < input_min or num_val > input_max then
						num_val = nil
					end
				end
			else
				-- format xxxxx
				num_val = tonumber(input_str)
				if num_val ~= nil then
					if num_val < input_min or num_val > input_max then
						num_val = nil
					end
				end
			end
		end
	elseif str_len == 6 and input_mode == 1 then
		-- format FLxxxA, xxxxxA
		str_type = string.sub(input_str, -1, -1)
		if str_type == "A" or str_type == "B" then
			if string.sub(input_str, 1, 2) == "FL" then
				-- format FLxxxA
				num_val = tonumber(string.sub(input_str, 3, -2)) * 100
				if num_val ~= nil then
					if num_val < input_min or num_val > input_max then
						num_val = nil
					end
				end
			else
				-- format xxxxxA
				num_val = tonumber(string.sub(input_str, 1, -2))
				if num_val ~= nil then
					if num_val < input_min or num_val > input_max then
						num_val = nil
					end
				end
			end
		end
	end
	
	-- change to alt/lvl
	if num_val ~= nil then
		result_output = roundUpToIncrement(num_val, 10)
		if num_val >= input_alttns then
			-- LVL FLxxx
			output_str = "FL".. string.sub(string.format("%05d", num_val), 1, 3) .. str_type
		else
			-- ALT xxxxx
			output_str = string.format("%5d", num_val) .. str_type
		end
	end
	
	return result_output
	
end

function entry_spd_alt(input_str, input_min_spd, input_max_spd, input_min_alt, input_max_alt, input_alttns2)
	
	local inp_str = ""
	local res = 0
	local result_output = nil
	local inp_str_len = string.len(input_str)
	output_str1 = ""
	output_str2 = ""
	output_num1 = 0
	output_num2 = 0
	
	-- B738DR_fms_test4 = 0
	-- B738DR_fms_test5 = 0
	
	-- SPD xxx/, xxxB/
	--inp_str = string.sub(input_str, 1, 4)
	if inp_str_len > 3 then
		if string.sub(input_str, 4, 4) == "B" or string.sub(input_str, 4, 4) == "A" then
			res = entry_spd_kts(string.sub(input_str, 1, 5), input_min_spd, input_max_spd, 1)
			if res ~= nil then
				output_num1 = res
				output_str1 = output_str
				res = entry_alt(string.sub(input_str, 6, -1), input_min_alt, input_max_alt, 1, input_alttns2)
				if res ~= nil then
					output_num2 = res
					output_str2 = output_str
					result_output = 0
				end
			end
		else
			res = entry_spd_kts(string.sub(input_str, 1, 4), input_min_spd, input_max_spd, 1)
			if res ~= nil then
				-- B738DR_fms_test4 = 1
				output_num1 = res
				output_str1 = output_str
				res = entry_alt(string.sub(input_str, 5, -1), input_min_alt, input_max_alt, 1, input_alttns2)
				if res ~= nil then
					-- B738DR_fms_test5 = 1
					output_num2 = res
					output_str2 = output_str
					result_output = 0
				end
			end
		end
	end
	
	return result_output
	
end


function read_spd_alt(itemx)
	
	local result_x = ""
	
	if legs_data2[itemx][4] ~= 0 then
		-- restrict speed
		result_x = string.format("%d", legs_data2[itemx][4])
		if legs_data2[itemx][42] == 0 then
			-- do nothing
		elseif legs_data2[itemx][42] == 1 then
			result_x = result_x .. "B/"
		else
			result_x = result_x .. "A/"
		end
	elseif legs_data2[itemx][10] ~= 0 then
		-- calc speed
		result_x = string.format("%d",  math.floor(legs_data2[itemx][10]+0.5)) .. "/"
	end
	
	if legs_data2[itemx][5] ~= 0 then
		-- restrict alt
		result_x = result_x .. string.format("%d", legs_data2[itemx][5])
		if legs_data2[itemx][6] == 43 then
			result_x = result_x .. "A"
		elseif legs_data2[itemx][6] == 45 then
			result_x = result_x .. "B"
		elseif legs_data2[itemx][6] == 41 then
			result_x = result_x .. "A"
			result_x = result_x .. string.format("%d", legs_data2[itemx][41]) .. "B"
		end
	elseif legs_data2[itemx][11] ~= 0 then
		-- calc alt
		result_x = result_x .. string.format("%d", math.floor(legs_data2[itemx][11]+0.5))
	end
	
	return result_x
	
end


function lvl_to_alt(lvl_str)
	return string.format("%5d", tonumber(string.sub(lvl_str, 3, -1)) * 100)
end

function alt_to_lvl(alt_str)
	local pom_str = string.format("%05d", tonumber(alt_str))
	return "FL" .. string.sub(pom_str, 1, 3)
end

function set_spd_alt_rest(item_idx, in_rte)
	
	local n = 0
	local nn = 0
	local nnn = 0
	local strlen = string.len(entry)
	local alt_slash = 0
	local alt_slash2 = 0
	local valid_entry = true
	
	if in_rte == 0 then
		if item_idx > legs_num2 then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif legs_data2[item_idx][1] == "DISCONTINUITY" then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif string.sub(legs_data2[item_idx][1], 1, 2) == "RW" then
			if entry == ">DELETE" then
				entry = ""
			end
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif entry == ">DELETE" then
			legs_data2[item_idx][4] = 0
			legs_data2[item_idx][42] = 0
			legs_data2[item_idx][5] = 0
			legs_data2[item_idx][6] = 0
			entry = ""
			msg_chk_alt_constr = 0
			vnav_update = 1
		else
			if strlen > 2 then
				if string.sub(entry, 1, 1) == "/" then
					entry = string.sub(entry, 2, -1)
					alt_slash = 1
				end
			end
			if strlen > 3 then
				if string.sub(entry, 4, 4) == "/" and strlen > 4 then
					alt_slash2 = 1
				end
			end
			if strlen > 4 then
				if string.sub(entry, 5, 5) == "/" and strlen > 5 then
					alt_slash2 = 1
				end
			end
			if alt_slash == 1 then
				-- only /ALT
				n = entry_alt(entry, 50, 41000, 1, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					if string.sub(output_str, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data2[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
					legs_data2[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry = ""
				end
			elseif strlen < 7 and alt_slash2 == 0 then
				-- only SPD or ALT
				n = entry_spd_kts(entry, 100, 340, 1)
				if n == nil then
					n = entry_alt(entry, 50, 41000, 1, 18000)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
						valid_entry = false
					else
						if string.sub(output_str, -1, -1) == "A" then
							nn = 43
						elseif string.sub(output_str, -1, -1) == "B" then
							nn = 45
						else
							nn = 32
						end
						legs_data2[item_idx][5] = math.floor(n + 0.5)	--roundUpToIncrement(n, 10)
						legs_data2[item_idx][6] = nn
						vnav_update = 1
						msg_chk_alt_constr = 0
						entry = ""
					end
				else
					legs_data2[item_idx][4] = n		-- speed
					if string.sub(output_str, -1, -1) == "B" then
						legs_data2[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str, -1, -1) == "A" then
						legs_data2[item_idx][42] = 2		-- speed type
					else
						legs_data2[item_idx][42] = 0		-- speed type
					end
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry = ""
				end
			else
				-- SPD/ALT
				n = entry_spd_alt(entry, 100, 340, 50, 41000, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					legs_data2[item_idx][4] = output_num1		-- speed
					if string.sub(output_str1, -1, -1) == "B" then
						legs_data2[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str1, -1, -1) == "A" then
						legs_data2[item_idx][42] = 2		-- speed type
					else
						legs_data2[item_idx][42] = 0		-- speed type
					end
					if string.sub(output_str2, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str2, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data2[item_idx][5] = math.floor(output_num2 + 0.5)	--roundUpToIncrement(output_num2, 10)		-- altitude
					legs_data2[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry = ""
				end
			end
			
			-- if legs_data2[item_idx][21] == 0 or legs_data2[item_idx][21] == 1 then
				-- if legs_data2[item_idx-1][7] == legs_data2[item_idx][7] and legs_data2[item_idx-1][8] == legs_data2[item_idx][8] then
					-- legs_data2[item_idx-1][4] = legs_data2[item_idx][4]
					-- legs_data2[item_idx-1][5] = legs_data2[item_idx][5]
					-- legs_data2[item_idx-1][6] = legs_data2[item_idx][6]
					-- legs_data2[item_idx-1][42] = legs_data2[item_idx][42]
				-- end
			-- elseif item_idx < legs_num2 then
				-- if legs_data2[item_idx+1][21] == 0 or legs_data2[item_idx+1][21] == 1 then
					-- if legs_data2[item_idx+1][7] == legs_data2[item_idx][7] and legs_data2[item_idx+1][8] == legs_data2[item_idx][8] then
						-- legs_data2[item_idx+1][4] = legs_data2[item_idx][4]
						-- legs_data2[item_idx+1][5] = legs_data2[item_idx][5]
						-- legs_data2[item_idx+1][6] = legs_data2[item_idx][6]
						-- legs_data2[item_idx+1][42] = legs_data2[item_idx][42]
					-- end
				-- end
			-- end
		end
	elseif in_rte == 1 then
		if item_idx > legs_num8 then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif legs_data8[item_idx][1] == "DISCONTINUITY" then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif string.sub(legs_data8[item_idx][1], 1, 2) == "RW" then
			if entry == ">DELETE" then
				entry = ""
			end
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif entry == ">DELETE" then
			legs_data8[item_idx][4] = 0
			legs_data8[item_idx][42] = 0
			legs_data8[item_idx][5] = 0
			legs_data8[item_idx][6] = 0
			entry = ""
			msg_chk_alt_constr = 0
			vnav_update = 1
		else
			if strlen > 2 then
				if string.sub(entry, 1, 1) == "/" then
					entry = string.sub(entry, 2, -1)
					alt_slash = 1
				end
			end
			if strlen > 3 then
				if string.sub(entry, 4, 4) == "/" and strlen > 4 then
					alt_slash2 = 1
				end
			end
			if strlen > 4 then
				if string.sub(entry, 5, 5) == "/" and strlen > 5 then
					alt_slash2 = 1
				end
			end
			if alt_slash == 1 then
				-- only /ALT
				n = entry_alt(entry, 50, 41000, 1, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					if string.sub(output_str, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data8[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
					legs_data8[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry = ""
				end
			elseif strlen < 7 and alt_slash2 == 0 then
				-- only SPD or ALT
				n = entry_spd_kts(entry, 100, 340, 1)
				if n == nil then
					n = entry_alt(entry, 50, 41000, 1, 18000)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
						valid_entry = false
					else
						if string.sub(output_str, -1, -1) == "A" then
							nn = 43
						elseif string.sub(output_str, -1, -1) == "B" then
							nn = 45
						else
							nn = 32
						end
						legs_data8[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
						legs_data8[item_idx][6] = nn
						vnav_update = 1
						msg_chk_alt_constr = 0
						entry = ""
					end
				else
					legs_data8[item_idx][4] = n		-- speed
					if string.sub(output_str, -1, -1) == "B" then
						legs_data8[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str, -1, -1) == "A" then
						legs_data8[item_idx][42] = 2		-- speed type
					else
						legs_data8[item_idx][42] = 0		-- speed type
					end
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry = ""
				end
			else
				-- SPD/ALT
				n = entry_spd_alt(entry, 100, 340, 50, 41000, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					legs_data8[item_idx][4] = output_num1		-- speed
					if string.sub(output_str1, -1, -1) == "B" then
						legs_data8[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str1, -1, -1) == "A" then
						legs_data8[item_idx][42] = 2		-- speed type
					else
						legs_data8[item_idx][42] = 0		-- speed type
					end
					if string.sub(output_str2, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str2, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data8[item_idx][5] = math.floor(output_num2 + 0.5) --roundUpToIncrement(output_num2, 10)		-- altitude
					legs_data8[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry = ""
				end
			end
			
			-- if legs_data8[item_idx][21] == 0 or legs_data8[item_idx][21] == 1 then
				-- if legs_data8[item_idx-1][7] == legs_data8[item_idx][7] and legs_data8[item_idx-1][8] == legs_data8[item_idx][8] then
					-- legs_data8[item_idx-1][4] = legs_data8[item_idx][4]
					-- legs_data8[item_idx-1][5] = legs_data8[item_idx][5]
					-- legs_data8[item_idx-1][6] = legs_data8[item_idx][6]
					-- legs_data8[item_idx-1][42] = legs_data8[item_idx][42]
				-- end
			-- elseif item_idx < legs_num8 then
				-- if legs_data8[item_idx+1][21] == 0 or legs_data8[item_idx+1][21] == 1 then
					-- if legs_data8[item_idx+1][7] == legs_data8[item_idx][7] and legs_data8[item_idx+1][8] == legs_data8[item_idx][8] then
						-- legs_data8[item_idx+1][4] = legs_data8[item_idx][4]
						-- legs_data8[item_idx+1][5] = legs_data8[item_idx][5]
						-- legs_data8[item_idx+1][6] = legs_data8[item_idx][6]
						-- legs_data8[item_idx+1][42] = legs_data8[item_idx][42]
					-- end
				-- end
			-- end
		end
	end
	
	return valid_entry
end

function set_spd_alt_rest2(item_idx, in_rte)
	
	local n = 0
	local nn = 0
	local nnn = 0
	local strlen = string.len(entry2)
	local alt_slash = 0
	local alt_slash2 = 0
	local valid_entry = true
	
	if in_rte == 0 then
		if item_idx > legs_num2 then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif legs_data2[item_idx][1] == "DISCONTINUITY" then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif string.sub(legs_data2[item_idx][1], 1, 2) == "RW" then
			if entry2 == ">DELETE" then
				entry2 = ""
			end
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif entry2 == ">DELETE" then
			legs_data2[item_idx][4] = 0
			legs_data2[item_idx][42] = 0
			legs_data2[item_idx][5] = 0
			legs_data2[item_idx][6] = 0
			entry2 = ""
			msg_chk_alt_constr = 0
			vnav_update = 1
		else
			if strlen > 2 then
			--if strlen > 3 then
				if string.sub(entry2, 1, 1) == "/" then
					entry2 = string.sub(entry2, 2, -1)
					alt_slash = 1
				end
			end
			if strlen > 3 then
				if string.sub(entry2, 4, 4) == "/" and strlen > 4 then
					alt_slash2 = 1
				end
			end
			if strlen > 4 then
				if string.sub(entry2, 5, 5) == "/" and strlen > 5 then
					alt_slash2 = 1
				end
			end
			if alt_slash == 1 then
				-- only /ALT
				n = entry_alt(entry2, 50, 41000, 1, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					if string.sub(output_str, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data2[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
					legs_data2[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry2 = ""
				end
			elseif strlen < 7 and alt_slash2 == 0 then
				-- only SPD or ALT
				n = entry_spd_kts(entry2, 100, 340, 1)
				if n == nil then
					n = entry_alt(entry2, 50, 41000, 1, 18000)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
						valid_entry = false
					else
						if string.sub(output_str, -1, -1) == "A" then
							nn = 43
						elseif string.sub(output_str, -1, -1) == "B" then
							nn = 45
						else
							nn = 32
						end
						legs_data2[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
						legs_data2[item_idx][6] = nn
						vnav_update = 1
						msg_chk_alt_constr = 0
						entry2 = ""
					end
				else
					legs_data2[item_idx][4] = n		-- speed
					if string.sub(output_str, -1, -1) == "B" then
						legs_data2[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str, -1, -1) == "A" then
						legs_data2[item_idx][42] = 2		-- speed type
					else
						legs_data2[item_idx][42] = 0		-- speed type
					end
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry2 = ""
				end
			else
				-- SPD/ALT
				n = entry_spd_alt(entry2, 50, 340, 100, 41000, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					legs_data2[item_idx][4] = output_num1		-- speed
					if string.sub(output_str1, -1, -1) == "B" then
						legs_data2[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str1, -1, -1) == "A" then
						legs_data2[item_idx][42] = 2		-- speed type
					else
						legs_data2[item_idx][42] = 0		-- speed type
					end
					if string.sub(output_str2, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str2, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data2[item_idx][5] = math.floor(output_num2 + 0.5) --roundUpToIncrement(output_num2, 10)		-- altitude
					legs_data2[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry2 = ""
				end
			end
			
			-- if legs_data2[item_idx][21] == 0 or legs_data2[item_idx][21] == 1 then
				-- if legs_data2[item_idx-1][7] == legs_data2[item_idx][7] and legs_data2[item_idx-1][8] == legs_data2[item_idx][8] then
					-- legs_data2[item_idx-1][4] = legs_data2[item_idx][4]
					-- legs_data2[item_idx-1][5] = legs_data2[item_idx][5]
					-- legs_data2[item_idx-1][6] = legs_data2[item_idx][6]
					-- legs_data2[item_idx-1][42] = legs_data2[item_idx][42]
				-- end
			-- elseif item_idx < legs_num2 then
				-- if legs_data2[item_idx+1][21] == 0 or legs_data2[item_idx+1][21] == 1 then
					-- if legs_data2[item_idx+1][7] == legs_data2[item_idx][7] and legs_data2[item_idx+1][8] == legs_data2[item_idx][8] then
						-- legs_data2[item_idx+1][4] = legs_data2[item_idx][4]
						-- legs_data2[item_idx+1][5] = legs_data2[item_idx][5]
						-- legs_data2[item_idx+1][6] = legs_data2[item_idx][6]
						-- legs_data2[item_idx+1][42] = legs_data2[item_idx][42]
					-- end
				-- end
			-- end
		end
	elseif in_rte == 1 then
		if item_idx > legs_num8 then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif legs_data8[item_idx][1] == "DISCONTINUITY" then
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif string.sub(legs_data8[item_idx][1], 1, 2) == "RW" then
			if entry2 == ">DELETE" then
				entry2 = ""
			end
			add_fmc_msg(INVALID_INPUT, 1)
			valid_entry = false
		elseif entry2 == ">DELETE" then
			legs_data8[item_idx][4] = 0
			legs_data8[item_idx][42] = 0
			legs_data8[item_idx][5] = 0
			legs_data8[item_idx][6] = 0
			entry2 = ""
			msg_chk_alt_constr = 0
			vnav_update = 1
		else
			if strlen > 2 then
			--if strlen > 3 then
				if string.sub(entry2, 1, 1) == "/" then
					entry2 = string.sub(entry2, 2, -1)
					alt_slash = 1
				end
			end
			if strlen > 3 then
				if string.sub(entry2, 4, 4) == "/" and strlen > 4 then
					alt_slash2 = 1
				end
			end
			if strlen > 4 then
				if string.sub(entry2, 5, 5) == "/" and strlen > 5 then
					alt_slash2 = 1
				end
			end
			if alt_slash == 1 then
				-- only /ALT
				n = entry_alt(entry2, 50, 41000, 1, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					if string.sub(output_str, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data8[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
					legs_data8[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry2 = ""
				end
			elseif strlen < 7 and alt_slash2 == 0 then
				-- only SPD or ALT
				n = entry_spd_kts(entry2, 100, 340, 1)
				if n == nil then
					n = entry_alt(entry2, 50, 41000, 1, 18000)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
						valid_entry = false
					else
						if string.sub(output_str, -1, -1) == "A" then
							nn = 43
						elseif string.sub(output_str, -1, -1) == "B" then
							nn = 45
						else
							nn = 32
						end
						legs_data8[item_idx][5] = math.floor(n + 0.5) --roundUpToIncrement(n, 10)
						legs_data8[item_idx][6] = nn
						vnav_update = 1
						msg_chk_alt_constr = 0
						entry2 = ""
					end
				else
					legs_data8[item_idx][4] = n		-- speed
					if string.sub(output_str, -1, -1) == "B" then
						legs_data8[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str, -1, -1) == "A" then
						legs_data8[item_idx][42] = 2		-- speed type
					else
						legs_data8[item_idx][42] = 0		-- speed type
					end
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry2 = ""
				end
			else
				-- SPD/ALT
				n = entry_spd_alt(entry2, 50, 340, 100, 41000, 18000)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
					valid_entry = false
				else
					legs_data8[item_idx][4] = output_num1		-- speed
					if string.sub(output_str1, -1, -1) == "B" then
						legs_data8[item_idx][42] = 1		-- speed type
					elseif string.sub(output_str1, -1, -1) == "A" then
						legs_data8[item_idx][42] = 2		-- speed type
					else
						legs_data8[item_idx][42] = 0		-- speed type
					end
					if string.sub(output_str2, -1, -1) == "A" then
						nn = 43
					elseif string.sub(output_str2, -1, -1) == "B" then
						nn = 45
					else
						nn = 32
					end
					legs_data8[item_idx][5] = math.floor(output_num2 + 0.5) --roundUpToIncrement(output_num2, 10)		-- altitude
					legs_data8[item_idx][6] = nn
					vnav_update = 1
					msg_chk_alt_constr = 0
					entry2 = ""
				end
			end
			
			-- if legs_data8[item_idx][21] == 0 or legs_data8[item_idx][21] == 1 then
				-- if legs_data8[item_idx-1][7] == legs_data8[item_idx][7] and legs_data8[item_idx-1][8] == legs_data8[item_idx][8] then
					-- legs_data8[item_idx-1][4] = legs_data8[item_idx][4]
					-- legs_data8[item_idx-1][5] = legs_data8[item_idx][5]
					-- legs_data8[item_idx-1][6] = legs_data8[item_idx][6]
					-- legs_data8[item_idx-1][42] = legs_data8[item_idx][42]
				-- end
			-- elseif item_idx < legs_num8 then
				-- if legs_data8[item_idx+1][21] == 0 or legs_data8[item_idx+1][21] == 1 then
					-- if legs_data8[item_idx+1][7] == legs_data8[item_idx][7] and legs_data8[item_idx+1][8] == legs_data8[item_idx][8] then
						-- legs_data8[item_idx+1][4] = legs_data8[item_idx][4]
						-- legs_data8[item_idx+1][5] = legs_data8[item_idx][5]
						-- legs_data8[item_idx+1][6] = legs_data8[item_idx][6]
						-- legs_data8[item_idx+1][42] = legs_data8[item_idx][42]
					-- end
				-- end
			-- end
		end
	end
	
	return valid_entry
end



function set_spd_alt_rest_default(x_fmc, x_crs)
	-- descent
	local n = 0
	local nn = 0
	local nnn = 0
	local strlen = 0
	local x_entry = ""
	if x_fmc == 0 then
		strlen = string.len(entry)
		x_entry = entry
	else
		strlen = string.len(entry2)
		x_entry = entry2
	end
	
	local alt_slash = 0
	local alt_slash2 = 0
	
	if x_entry == ">DELETE" then
		
		descent_r_alt1_mod = 0
		descent_r_speed1_mod = 0
		fmc_descent_r_alt1_mod = ""
		
		if x_fmc == 0 then
			entry = ""
		else
			entry2 = ""
		end
		if x_crs == 0 then
			descent_exec = 1
		end
	else
		if strlen > 2 then
			if string.sub(x_entry, 1, 1) == "/" then
				x_entry = string.sub(x_entry, 2, -1)
				alt_slash = 1
			elseif string.sub(x_entry, -1, -1) == "/" then
				x_entry = string.sub(x_entry, 1, -2)
				alt_slash2 = 1
			elseif string.sub(x_entry, 4, 4) == "/" and strlen > 4 then
				alt_slash = 0
			else
				alt_slash2 = 1
			end
		end
		
		if alt_slash == 1 and descent_r_speed1_mod ~= 0 then
			-- only /ALT
			n = entry_alt(x_entry, 50, 41000, 1, 18000)
			if n == nil then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				descent_r_alt1_mod = roundUpToIncrement(n, 10)
				fmc_descent_r_alt1_mod = spaces_after(output_str, 5)
				descent_exec = 1
				if x_fmc == 0 then
					entry = ""
				else
					entry2 = ""
				end
			end
		elseif alt_slash2 == 1 and descent_r_speed1_mod ~= 0 then
			-- only SPD
			n = entry_spd_kts(x_entry, 100, 340, 0)
			if n == nil then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				descent_r_speed1_mod = n		-- speed
				--vnav_update = 1
				descent_exec = 1
				--msg_chk_alt_constr = 0
				if x_fmc == 0 then
					entry = ""
				else
					entry2 = ""
				end
			end
		else
			-- SPD/ALT
			n = entry_spd_alt(x_entry, 50, 340, 100, 41000, 18000)
			if n == nil then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				descent_r_speed1_mod = output_num1		-- speed
				descent_r_alt1_mod = roundUpToIncrement(output_num2, 10)		-- altitude
				fmc_descent_r_alt1_mod = spaces_after(output_str2, 5)
				descent_exec = 1
				if x_fmc == 0 then
					entry = ""
				else
					entry2 = ""
				end
			end
		end
	end
end

function set_spd_alt_rest_default2(x_fmc, x_crs)
	-- climb
	local n = 0
	local nn = 0
	local nnn = 0
	local strlen = 0
	local x_entry = ""
	if x_fmc == 0 then
		strlen = string.len(entry)
		x_entry = entry
	else
		strlen = string.len(entry2)
		x_entry = entry2
	end
	
	local alt_slash = 0
	local alt_slash2 = 0
	
	if x_entry == ">DELETE" then
		
		climb_r_alt1_mod = 0
		climb_r_speed1_mod = 0
		fmc_climb_r_alt1_mod = ""
		
		if x_fmc == 0 then
			entry = ""
		else
			entry2 = ""
		end
		if x_crs == 0 then
			climb_exec = 1
		end
	else
		if strlen > 2 then
			if string.sub(x_entry, 1, 1) == "/" then
				x_entry = string.sub(x_entry, 2, -1)
				alt_slash = 1
			elseif string.sub(x_entry, -1, -1) == "/" then
				x_entry = string.sub(x_entry, 1, -2)
				alt_slash2 = 1
			elseif string.sub(x_entry, 4, 4) == "/" and strlen > 4 then
				alt_slash = 0
			else
				alt_slash2 = 1
			end
		end
		
		if alt_slash == 1 and climb_r_speed1_mod ~= 0 then
			-- only /ALT
			n = entry_alt(x_entry, 50, 41000, 1, 18000)
			if n == nil then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				climb_r_alt1_mod = roundUpToIncrement(n, 10)
				fmc_climb_r_alt1_mod = spaces_after(output_str, 5)
				--vnav_update = 1
				climb_exec = 1
				--msg_chk_alt_constr = 0
				if x_fmc == 0 then
					entry = ""
				else
					entry2 = ""
				end
			end
		elseif alt_slash2 == 1 and climb_r_speed1_mod ~= 0 then
			-- only SPD
			n = entry_spd_kts(x_entry, 100, 340, 0)
			if n == nil then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				climb_r_speed1_mod = n		-- speed
				climb_exec = 1
				if x_fmc == 0 then
					entry = ""
				else
					entry2 = ""
				end
			end
		else
			-- SPD/ALT
			n = entry_spd_alt(x_entry, 50, 340, 50, 41000, 18000)
			if n == nil then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				climb_r_speed1_mod = output_num1		-- speed
				climb_r_alt1_mod = roundUpToIncrement(output_num2, 10)		-- altitude
				fmc_climb_r_alt1_mod = spaces_after(output_str2, 5)
				climb_exec = 1
				if x_fmc == 0 then
					entry = ""
				else
					entry2 = ""
				end
			end
		end
	end
end



function transf_lat_lon(trans_pos)
	
	transf_lat = tonumber(string.sub(trans_pos, 2, 3)) + (tonumber(string.sub(trans_pos, 4, 7)) / 60)
	if string.sub(trans_pos, 1, 1) == "S" then
		transf_lat = -transf_lat
	end
	
	transf_lon = tonumber(string.sub(trans_pos, 9, 11)) + (tonumber(string.sub(trans_pos, 12, 15)) / 60)
	if string.sub(trans_pos, 8, 8) == "W" then
		transf_lon = -transf_lon
	end

end

function wpt_lat_lon(aaa_txt)
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local pos_N_S = 0
	local pos_E_W = 0
	local input_lat_ok = 0
	local input_lon_ok = 0
	local pos_tempp = ""
	
	if string.sub(aaa_txt, 1, 1) == "N" or string.sub(aaa_txt, 1, 1) == "S" then
		pos_N_S = 1
		if string.sub(aaa_txt, 4, 4) == "E" or string.sub(aaa_txt, 4, 4) == "W" then
			pos_E_W = 4
		elseif string.sub(aaa_txt, 8, 8) == "E" or string.sub(aaa_txt, 8, 8) == "W" then
			pos_E_W = 8
		end
	end
	
	if pos_N_S ~= 0 and pos_E_W ~= 0 then
		-- latitutde
		pos_tempp = string.sub(aaa_txt, 1, 1)
		if string.len(aaa_txt) == 7 then
			jj = tonumber(string.sub(aaa_txt, 2, 3))
			if jj >= 0 and jj <= 90 then
				pos_tempp = pos_tempp .. string.sub(aaa_txt, 2, 3) .. "00.0"
				input_lat_ok = 1
			end
		elseif string.sub(aaa_txt, 6, 6) == "." and string.len(aaa_txt) == 15 then
			jj = tonumber(string.sub(aaa_txt, 2, 3))
			if jj >= 0 and jj < 90 then
				jj = tonumber(string.sub(aaa_txt, 4, 7))
				if jj >= 0 and jj < 60 then
					pos_tempp = pos_tempp .. string.sub(aaa_txt, 2, 7)
					input_lat_ok = 1
				end
			end
		end
		
		-- longitude
		pos_tempp = pos_tempp .. string.sub(aaa_txt, pos_E_W, pos_E_W)
		if string.len(aaa_txt) == 7 then
			jj = tonumber(string.sub(aaa_txt, 5, 7))
			if jj >= 0 and jj <= 90 then
				pos_tempp = pos_tempp .. string.sub(aaa_txt, 5, 7) .. "00.0"
				input_lon_ok = 1
			end
		elseif string.sub(aaa_txt, 14, 14) == "." and string.len(aaa_txt) == 15 then
			jj = tonumber(string.sub(aaa_txt, 9, 11))
			if jj >= 0 and jj < 180 then
				jj = tonumber(string.sub(aaa_txt, 12, 15))
				if jj >= 0 and jj < 60 then
					pos_tempp = pos_tempp .. string.sub(aaa_txt, 9, 15)
					input_lon_ok = 1
				end
			end
		end
	end
	
	if input_lat_ok == 0 or input_lon_ok == 0 then
		return false
	else
		transf_lat_lon(pos_tempp)
		return true
	end
end



function reset_fmc_pages()
	page_dep_arr = 0
	page_climb = 0
	page_menu = 0
	page_init = 0
	page_ident = 0
	page_takeoff = 0
	page_approach = 0
	page_perf = 0
	page_n1_limit = 0
	page_pos_init = 0
	page_cruise = 0
	page_descent = 0
	--page_clear = 0
	page_route = 0
	page_legs = 0
	page_descent_forecast = 0
	page_rte_init = 0
	page_dep = 0
	page_arr = 0
	page_progress = 0
	page_hold = 0
	page_sel_wpt = 0
	page_sel_wpt2 = 0
	page_sel_wpt3 = 0
	page_sel_wpt4 = 0
	fix_idx1 = 0
	fix_idx2 = 0
	page_offset = 0
	page_ref_nav_data = 0
	page_ref_nav_data_wpt = 0
	page_ref_nav_data_navaid = 0
	page_ref_nav_data_apt = 0
	page_ref_sel = 0
	page_fix = 0
	page_rte_legs = 0
	page_ac = 0
	page_ac_atis = 0
	page_ac_msg = 0
	page_nearest_apt = 0
	page_altern_apt = 0
	page_nav_status = 0
	page_atc = 0
	page_fmc_comm = 0
	page_dl_menu = 0
	page_dl_acars = 0
	page_dl_requests = 0
	page_dl_wxr_req = 0
	page_dl_wxr_info = 0
	page_dl_fltlog = 0
	page_dl_sensors = 0
	page_eo_climb = 0
	page_eo_cruise = 0
	page_flt_plns = 0
	
end

function reset_fmc_pages_fo()
	page_dep_arr2 = 0
	page_climb2 = 0
	page_menu2 = 0
	page_init2 = 0
	page_ident2 = 0
	page_takeoff2 = 0
	page_approach2 = 0
	page_perf2 = 0
	page_n1_limit2 = 0
	page_pos_init2 = 0
	page_cruise2 = 0
	page_descent2 = 0
	page_route2 = 0
	page_legs2 = 0
	page_descent_forecast2 = 0
	page_rte_init2 = 0
	page_dep2 = 0
	page_arr2 = 0
	page_progress2 = 0
	page_hold2 = 0
	page_sel_wpt_2 = 0
	page_sel_wpt2_2 = 0
	page_sel_wpt3_2 = 0
	page_sel_wpt4_2 = 0
	page_offset2 = 0
	page_ref_nav_data2 = 0
	page_ref_nav_data_wpt2 = 0
	page_ref_nav_data_navaid2 = 0
	page_ref_nav_data_apt2 = 0
	page_ref_sel2 = 0
	page_fix2 = 0
	page_rte_legs2 = 0
	page_ac2 = 0
	page_ac_atis2 = 0
	page_ac_msg2 = 0
	page_nearest_apt2 = 0
	page_altern_apt2 = 0
	page_nav_status2 = 0
	page_atc2 = 0
	page_fmc_comm2 = 0
	page_dl_menu2 = 0
	page_dl_acars2 = 0
	page_dl_requests2 = 0
	page_dl_wxr_req2 = 0
	page_dl_wxr_info2 = 0
	page_dl_fltlog2 = 0
	page_dl_sensors2 = 0
	page_eo_climb2 = 0
	page_eo_cruise2 = 0
	page_flt_plns2 = 0
end

function check_rte(idx_in, star_in, app_in, in_rte)
	
	local ii = 0
	
	if in_rte == 0 then
		if legs_num2 > 0 and idx_in <= legs_num2 and idx_in > 1 then
			for ii = 1, idx_in do
				if star_in == 1 then
					if legs_data2[ii][19] == 2 or legs_data2[ii][19] == 4 
					or legs_data2[ii][19] == 7 or legs_data2[ii][19] == 8 
					or legs_data2[ii][19] == 9 then
						legs_data2[ii][19] = 0
						legs_data2[ii][17] = 0
					end
				elseif app_in == 1 then
					-- 8 -> STAR
					-- 9 -> RTE
					if legs_data2[ii][19] == 8 then
						legs_data2[ii][19] = 2
					elseif legs_data2[ii][19] == 9 then
						legs_data2[ii][19] = 0
					end
					if legs_data2[ii][19] == 7 or legs_data2[ii][19] == 8 
					or legs_data2[ii][19] == 9 then
						legs_data2[ii][19] = 0
						legs_data2[ii][17] = 0
					end
				end
			end
		end
	elseif in_rte == 1 then
		if legs_num8 > 0 and idx_in <= legs_num8 and idx_in > 1 then
			for ii = 1, idx_in do
				if star_in == 1 then
					if legs_data8[ii][19] == 2 or legs_data8[ii][19] == 4 
					or legs_data8[ii][19] == 7 or legs_data8[ii][19] == 8 
					or legs_data8[ii][19] == 9 then
						legs_data8[ii][19] = 0
						legs_data8[ii][17] = 0
					end
				elseif app_in == 1 then
					-- 8 -> STAR
					-- 9 -> RTE
					if legs_data8[ii][19] == 8 then
						legs_data8[ii][19] = 2
					elseif legs_data8[ii][19] == 9 then
						legs_data8[ii][19] = 0
					end
					if legs_data8[ii][19] == 7 or legs_data8[ii][19] == 8 
					or legs_data8[ii][19] == 9 then
						legs_data8[ii][19] = 0
						legs_data8[ii][17] = 0
					end
				end
			end
		end
	end
	
end


function rte_add_dep_arr(in_rte)
	
	local exec_sum = 0
	
	if in_rte == 0 then
		if ref_rwy_exec == 1 or ref_sid_exec == 1 or ref_tns_exec == 1 then
			rte_add_sid_act = 1
			add_sid_star_app_act = 0
		end
		if des_star_exec == 1 or des_star_tns_exec == 1 or des_app_exec == 1 or des_app_tns_exec == 1 then
			
			if simDR_radio_height_pilot_ft > 50 then
				
				if des_star2 ~= des_star2_old or des_star_trans2 ~= des_star_trans2_old then
					--B738DR_fms_test3 = 99
					rte_add_star_act = 1
					rte_add_app_act = 1
					add_sid_star_app_act = 0
				end
				if des_app2 ~= des_app2_old or des_app_tns2 ~= des_app_tns2_old then
					add_star = 0
					first_add_star_idx = 0
					-- rte_add_star_act = 1
					rte_add_app_act = 1
					add_sid_star_app_act = 0
				end
				
				check_rte(offset, rte_add_star_act, rte_add_app_act, 0)
			
			else
				rte_add_star_act = 1
				rte_add_app_act = 1
				add_sid_star_app_act = 0
			end
			
			des_star2_old = des_star2
			des_star_trans2_old = des_star_trans2
			des_app2_old = des_app2
			des_app_tns2_old = des_app_tns2
			
		end
		exec_sum = ref_rwy_exec + ref_sid_exec + ref_tns_exec + des_star_exec + des_star_tns_exec + des_app_exec + des_app_tns_exec
		ref_rwy_exec = 0
		ref_sid_exec = 0
		ref_tns_exec = 0
		des_star_exec = 0
		des_star_tns_exec = 0
		des_app_exec = 0
		des_app_tns_exec = 0
		if exec_sum ~= 0 then
			calc_rte_enable2 = 1
			if rte12_act == 0 then
				legs_delete = 1
			end
			fms_recalc = 1
		end
		msg_unavaible_crz_alt = 0
	elseif in_rte == 1 then
		if ref_rwy_exec == 1 or ref_sid_exec == 1 or ref_tns_exec == 1 then
			rte_add_sid_act = 1
			add_sid_star_app_act = 1
		end
		if des_star_exec == 1 or des_star_tns_exec == 1 or des_app_exec == 1 or des_app_tns_exec == 1 then
			
			if simDR_radio_height_pilot_ft > 50 then
				
				if des_star8 ~= des_star8_old or des_star_trans8 ~= des_star_trans8_old then
					rte_add_star_act = 1
					rte_add_app_act = 1
					add_sid_star_app_act = 1
				end
				if des_app8 ~= des_app8_old or des_app_tns8 ~= des_app_tns8_old then
					add_star = 0
					first_add_star_idx = 0
					-- rte_add_star_act = 1
					rte_add_app_act = 1
					add_sid_star_app_act = 1
				end
				
				check_rte(offset, rte_add_star_act, rte_add_app_act, 1)
			
			else
				rte_add_star_act = 1
				rte_add_app_act = 1
				add_sid_star_app_act = 1
			end
			
			des_star8_old = des_star8
			des_star_trans8_old = des_star_trans8
			des_app8_old = des_app8
			des_app_tns8_old = des_app_tns8
			
		end
		exec_sum = ref_rwy_exec + ref_sid_exec + ref_tns_exec + des_star_exec + des_star_tns_exec + des_app_exec + des_app_tns_exec
		ref_rwy_exec = 0
		ref_sid_exec = 0
		ref_tns_exec = 0
		des_star_exec = 0
		des_star_tns_exec = 0
		des_app_exec = 0
		des_app_tns_exec = 0
		if exec_sum ~= 0 then
			calc_rte_enable8 = 1
			if rte12_act == 1 then
				legs_delete = 1
			end
			fms_recalc = 1
		end
		msg_unavaible_crz_alt = 0
	end
	
end

function B738_dist_to_dest()
	if legs_num > 0 and offset > 0 then
		if calc_rte_enable == 0 then
			if offset > legs_num then
				B738DR_fmc_dist_to_dest = B738DR_fmc_dist
			else
				B738DR_fmc_dist_to_dest = B738DR_fmc_dist + legs_data[offset][50]
			end
		end
	else
		B738DR_fmc_dist_to_dest = 0	-- -1
	end
	
	if rte12_act == 0 then
		if legs_num2 > 0 and offset_mod > 0 then
			if calc_rte_enable2 == 0 then
				if offset_mod > legs_num2 then
					B738DR_fmc_dist_to_dest2 = B738DR_fmc_dist2
				else
					B738DR_fmc_dist_to_dest2 = B738DR_fmc_dist2 + legs_data2[offset_mod][50]
				end
			end
		else
			B738DR_fmc_dist_to_dest2 = 0	-- -1
		end
	elseif rte12_act == 1 then
		if legs_num8 > 0 and offset_mod > 0 then
			if calc_rte_enable8 == 0 then
				if offset_mod > legs_num8 then
					B738DR_fmc_dist_to_dest2 = B738DR_fmc_dist2
				else
					B738DR_fmc_dist_to_dest2 = B738DR_fmc_dist2 + legs_data2[offset_mod][50]
				end
			end
		else
			B738DR_fmc_dist_to_dest2 = 0	-- -1
		end
	else
		B738DR_fmc_dist_to_dest2 = 0
	end
end

function B738_mod_change2()
	
	local ii = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	
	local new_hold_idx_tmp = 0
	local new_hold_idx2_tmp = 0
	
	if rte_add_sid_act == 0 and rte_add_star_act == 0 and rte_add_app_act == 0 and rte12_act == 0 then
		
		if offset_mod_x2 > offset then
			offset_mod_x2 = offset
		end
		if offset_mod_x2 > legs_num2 then
			offset_mod_x2 = legs_num2
		end
		
		offset_mod = offset_mod_x2
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			if legs_num2 < 2 then
				offset_mod = 0
			elseif offset_mod > legs_num2 and legs_num2 > 2 then
				offset_mod = legs_num2
			elseif offset_mod > legs_num2 + 1 and legs_num2 > 0 then
				offset_mod = legs_num2 + 1
			elseif offset_mod <= 2 and legs_num2 > 1 then
				offset_mod = 2
				if offset_mod <= legs_num2 then
					if string.sub(legs_data2[offset_mod][1], 1, 2) == "RW" and legs_data2[offset_mod][19] == 1 then
						offset_mod = offset_mod + 1
					end
				end
			elseif offset_mod == 3 then
				if string.sub(legs_data2[2][1], 1, 2) ~= "RW" or legs_data2[2][19] ~= 1 then
					offset_mod = 2
				end
			end
			offset_mod_x2 = offset_mod
		elseif legs_num2 < 2 then
			offset_mod = 0
		elseif legs_delete == 0 and legs_num > 1 then
			if offset ~= last_offset2 then
				legs_num2mm = 0
				if offset_mod > 0 and offset_mod <= legs_num2 then
					if legs_data2[offset_mod][52] < 0 then
						legs_data2mm = {}
						ii = offset_mod
						while legs_data2[ii][52] < 0 do
							legs_num2mm = legs_num2mm + 1
							legs_data2mm[legs_num2mm] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2mm[legs_num2mm][jj] = legs_data2[ii][jj]
							end
							ii = ii + 1
							if ii > legs_num2 then
								break
							end
						end
						if legs_num2mm > 0 then
							
							if legs_data2[offset_mod][1] == "PPOS" and (legs_data2[offset_mod][21] == 0 or legs_data2[offset_mod][21] == 1) then
								if new_hold_idx == offset_mod then
									new_hold_idx_tmp = new_hold_idx + 1
								end
							end
							
							kk = offset_mod
							for ii = offset_mod + legs_num2mm, legs_num2 + 1 do
								for jj = 1, MAX_LEGS_DATA do
									legs_data2[kk][jj] = legs_data2[ii][jj]
								end
								kk = kk + 1
							end
							
						end
						legs_num2 = legs_num2 - legs_num2mm
						kk = 0
						
					end
				end
				
				offset_mod = offset
				
				if legs_num2mm > 0 then
					if offset_mod > legs_num2 then
						-- ERASE PPOS HOLD
						if legs_data2mm[1][1] == "PPOS" and (legs_data2mm[1][21] == 0 or legs_data2mm[1][21] == 1) then
								new_hold_act_page = 0
								hold_exec = 0
								new_hold = 0
								new_hold_idx = 0
								if rte12_act == rte12_sel then
									item_sel = 0
									item_sel_act = 0
									item_sel_via = 0
								end
								if rte12_act == rte12_sel_fo then
									item_sel2 = 0
									item_sel_act2 = 0
									item_sel_via2 = 0
								end
						end
						calc_rte_enable2 = 1
					else
						legs_data2m = {}
						legs_num2m = 0
						
						-- dump_leg_append(" LEGS_NUM2: " .. tostring(legs_num2) .. "\n")
						
						-- dump_leg_append("COPY LEGSDATA: " .. tostring(offset_mod) .. " .. " .. tostring(legs_num2 + 1))
						
						kk = offset_mod
						for ii = kk, legs_num2 + 1 do
							legs_num2m = legs_num2m + 1
							legs_data2m[legs_num2m] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2m[legs_num2m][jj] = legs_data2[ii][jj]
							end
						end
						
						-- dump_leg_append("PASTE PPOS: " .. tostring(kk) .. " .. " .. tostring(kk + legs_num2mm - 1))
						
						for ii = 1, legs_num2mm do
							legs_data2[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2[kk][jj] = legs_data2mm[ii][jj]
							end
							kk = kk + 1
						end
						
						-- dump_leg_append("PASTE LEGSDATA: " .. tostring(kk) .. " .. " .. tostring(kk + legs_num2m - 1))
						
						for ii = 1, legs_num2m do
							legs_data2[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2[kk][jj] = legs_data2m[ii][jj]
							end
							kk = kk + 1
						end
						
						legs_num2 = legs_num2 + legs_num2mm
						
						--recreate Hold data
						hold_data_num2 = 0
						hold_data2 = {}
						for ii = 1, legs_num2 do
							if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
								hold_data_num2 = hold_data_num2 + 1
								hold_data2[hold_data_num2] = ii
							end
						end
						
						if new_hold_idx_tmp ~= 0 then
							new_hold_idx = new_hold_idx_tmp
						end
						
						calc_rte_enable2 = 1
						
					end
				end
			else
				offset_mod = offset
			end
		elseif offset ~= last_offset2 and legs_num > 1 then
			if legs_intdir == 0 and offset_mod > 0 and offset_mod <= legs_num2 - 1 and offset > 0 and offset <= legs_num then
				legs_num2mm = 0
				if legs_data2[offset_mod][52] < 0 then
					legs_data2mm = {}
					ii = offset_mod
					while legs_data2[ii][52] < 0 do
						legs_num2mm = legs_num2mm + 1
						legs_data2mm[legs_num2mm] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data2mm[legs_num2mm][jj] = legs_data2[ii][jj]
						end
						ii = ii + 1
						if ii > legs_num2 then
							break
						end
					end
					if legs_num2mm > 0 then
						if legs_data2[offset_mod][1] == "PPOS" and (legs_data2[offset_mod][21] == 0 or legs_data2[offset_mod][21] == 1) then
							if new_hold_idx == offset_mod then
								new_hold_idx_tmp = new_hold_idx + 1
							end
						end
						
						kk = offset_mod
						for ii = offset_mod + legs_num2mm, legs_num2 + 1 do
							for jj = 1, MAX_LEGS_DATA do
								legs_data2[kk][jj] = legs_data2[ii][jj]
							end
							kk = kk + 1
						end
						
					end
					legs_num2 = legs_num2 - legs_num2mm
					kk = 0
					
				end
				if legs_data2[offset_mod + 1][25] == 0 then
					if offset_mod + 2 < legs_num2 then
						for ii = offset_mod + 2, legs_num2 do
							if legs_data2[ii][25] ~= 0 then
								ll = ii
								mm = legs_data2[ii][25]
								break
							end
						end
						if ll ~= 0 and mm >= offset and mm <= legs_num2 then
							-- copy legs_data2
							legs_data2m = {}
							legs_num2m = 0
							for ii = ll, legs_num2 + 1 do
								kk = kk + 1
								legs_data2m[kk] = {}
								for jj = 1, MAX_LEGS_DATA do
									legs_data2m[kk][jj] = legs_data2[ii][jj]
								end
							end
							-- add current wpt and next wpts
							for ii = offset, mm do
								nn = nn + 1
								legs_data2[offset_mod+nn] = {}
								for jj = 1, MAX_LEGS_DATA do
									legs_data2[offset_mod+nn][jj] = legs_data[ii][jj]
								end
								legs_data2[offset_mod+nn][25] = ii
							end
							-- paste legs_data2
							for ii = 1, kk do
								legs_data2[offset_mod + nn + ii] = {}
								for jj = 1, MAX_LEGS_DATA do
									legs_data2[offset_mod + nn + ii][jj] = legs_data2m[ii][jj]
								end
							end
							legs_num2 = legs_num2 + nn
							offset_mod = offset
							calc_rte_enable2 = 1
						end
					end
				else
					if legs_data2[offset_mod+1][25] > offset then
						-- copy legs_data2
						legs_data2m = {}
						legs_num2m = 0
						for ii = offset_mod + 1, legs_num2 + 1 do
							kk = kk + 1
							legs_data2m[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2m[kk][jj] = legs_data2[ii][jj]
							end
						end
						-- add current wpt
						for jj = 1, MAX_LEGS_DATA do
							legs_data2[offset_mod+1][jj] = legs_data[offset][jj]
						end
						legs_data2[offset_mod+1][25] = offset
						-- paste legs_data2
						for ii = 1, kk do
							legs_data2[offset_mod + ii + 1] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2[offset_mod + ii + 1][jj] = legs_data2m[ii][jj]
							end
						end
						legs_num2 = legs_num2 + 1
						offset_mod = offset
						calc_rte_enable2 = 1
					elseif legs_data2[offset_mod+1][25] == offset then
						offset_mod = offset
						calc_rte_enable2 = 1
					end
				end
				
				if legs_num2mm > 0 then
					if offset_mod > legs_num2 then
						-- ERASE PPOS HOLD
						if legs_data2mm[1][1] == "PPOS" and (legs_data2mm[1][21] == 0 or legs_data2mm[1][21] == 1) then
							hold_exec = 0
							new_hold = 0
							new_hold_idx = 0
							new_hold_act_page = 0
							if rte12_act == rte12_sel then
								item_sel = 0
								item_sel_act = 0
								item_sel_via = 0
							end
							if rte12_act == rte12_sel_fo then
								item_sel2 = 0
								item_sel_act2 = 0
								item_sel_via2 = 0
							end
						end
					else
						legs_data2m = {}
						legs_num2m = 0
						
						kk = offset_mod
						for ii = kk, legs_num2 + 1 do
							legs_num2m = legs_num2m + 1
							legs_data2m[legs_num2m] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2m[legs_num2m][jj] = legs_data2[ii][jj]
							end
						end
						
						for ii = 1, legs_num2mm do
							legs_data2[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2[kk][jj] = legs_data2mm[ii][jj]
							end
							kk = kk + 1
						end
						
						for ii = 1, legs_num2m do
							legs_data2[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data2[kk][jj] = legs_data2m[ii][jj]
							end
							kk = kk + 1
						end
						
						legs_num2 = legs_num2 + legs_num2mm
						
						--recreate Hold data
						hold_data_num2 = 0
						hold_data2 = {}
						for ii = 1, legs_num2 do
							if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
								hold_data_num2 = hold_data_num2 + 1
								hold_data2[hold_data_num2] = ii
							end
						end
						
						if new_hold_idx_tmp ~= 0 then
							new_hold_idx = new_hold_idx_tmp
						end
						
						calc_rte_enable2 = 1
					end
				end
			end
		end
		if offset_mod > legs_num2 and legs_num2 > 2 then
			offset_mod = legs_num2
		elseif offset_mod > legs_num2 + 1 and legs_num2 > 0 then
			offset_mod = legs_num2 + 1
		elseif offset_mod <= 2 and legs_num2 > 1 then
			offset_mod = 2
			if offset_mod <= legs_num2 then
				if string.sub(legs_data2[offset_mod][1], 1, 2) == "RW" and legs_data2[offset_mod][19] == 1 then
					offset_mod = offset_mod + 1
				end
			end
		end
		
		last_offset2  = offset
		offset_mod_x2 = offset_mod
	else
		if legs_num2 < 2 then
			offset_mod = 0
		elseif offset_mod > legs_num2 and legs_num2 > 2 then
			offset_mod = legs_num2
		elseif offset_mod > legs_num2 + 1 and legs_num2 > 0 then
			offset_mod = legs_num2 + 1
		elseif offset_mod <= 2 and legs_num2 > 1 then
			offset_mod = 2
			if offset_mod <= legs_num2 then
				if string.sub(legs_data2[offset_mod][1], 1, 2) == "RW" and legs_data2[offset_mod][19] == 1 then
					offset_mod = offset_mod + 1
				end
			end
		elseif offset_mod == 3 then
			if string.sub(legs_data2[2][1], 1, 2) ~= "RW" or legs_data2[2][19] ~= 1 then
				offset_mod = 2
			end
		end
		offset_mod_x2 = offset_mod
	end
	
	if offset_mod > 1 and offset_mod <= legs_num2 and (legs_delete ~= 0 or legs_num <= 1) and rte12_act == 0 then
		B738DR_fmc_dist_mod = nd_calc_dist(simDR_latitude, simDR_longitude, legs_data2[offset_mod][7], legs_data2[offset_mod][8])
		B738DR_fmc_crs_mod = (nd_calc_brg(simDR_latitude, simDR_longitude, legs_data2[offset_mod][7], legs_data2[offset_mod][8]) + simDR_mag_variation + 360) % 360
	elseif rte12_act == 0 then
		B738DR_fmc_dist_mod = B738DR_fmc_dist
		B738DR_fmc_crs_mod = B738DR_fmc_crs
	end
	
	--RTE2
	if rte_add_sid_act == 0 and rte_add_star_act == 0 and rte_add_app_act == 0 and rte12_act == 1 then
		
		if offset_mod_x8 > offset then
			offset_mod_x8 = offset
		end
		if offset_mod_x8 > legs_num8 then
			offset_mod_x8 = legs_num8
		end
		
		offset_mod2 = offset_mod_x8
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			if legs_num8 < 2 then
				offset_mod2 = 0
			elseif offset_mod2 > legs_num8 and legs_num8 > 2 then
				offset_mod2 = legs_num8
			elseif offset_mod2 > legs_num8 + 1 and legs_num8 > 0 then
				offset_mod2 = legs_num8 + 1
			elseif offset_mod2 <= 2 and legs_num8 > 1 then
				offset_mod2 = 2
				if offset_mod2 <= legs_num8 then
					if string.sub(legs_data8[offset_mod2][1], 1, 2) == "RW" and legs_data8[offset_mod2][19] == 1 then
						offset_mod2 = offset_mod2 + 1
					end
				end
			elseif offset_mod2 == 3 then
				if string.sub(legs_data8[2][1], 1, 2) ~= "RW" or legs_data8[2][19] ~= 1 then
					offset_mod2 = 2
				end
			end
			offset_mod_x8 = offset_mod2
			
		elseif legs_num8 < 2 then
			offset_mod2 = 0
		elseif legs_delete == 0 and legs_num > 1 then
			if offset ~= last_offset8 then
				legs_num8mm = 0
				if offset_mod2 > 0 and offset_mod2 <= legs_num8 then
					if legs_data8[offset_mod2][52] < 0 then
						legs_data8mm = {}
						ii = offset_mod2
						while legs_data8[ii][52] < 0 do
							legs_num8mm = legs_num8mm + 1
							legs_data8mm[legs_num8mm] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8mm[legs_num8mm][jj] = legs_data8[ii][jj]
							end
							ii = ii + 1
							if ii > legs_num8 then
								break
							end
						end
						if legs_num8mm > 0 then
							
							if legs_data8[offset_mod2][1] == "PPOS" and (legs_data8[offset_mod2][21] == 0 or legs_data8[offset_mod2][21] == 1) then
								if new_hold_idx2 == offset_mod2 then
									new_hold_idx2_tmp = new_hold_idx2 + 1
								end
							end
							
							kk = offset_mod2
							for ii = offset_mod2 + legs_num8mm, legs_num8 + 1 do
								for jj = 1, MAX_LEGS_DATA do
									legs_data8[kk][jj] = legs_data8[ii][jj]
								end
								kk = kk + 1
							end
							
						end
						legs_num8 = legs_num8 - legs_num8mm
						kk = 0
						
					end
				end
				
				offset_mod2 = offset
				
				if legs_num8mm > 0 then
					if offset_mod2 > legs_num8 then
						-- ERASE PPOS HOLD
						if legs_data8mm[1][1] == "PPOS" and (legs_data8mm[1][21] == 0 or legs_data8mm[1][21] == 1) then
							hold_exec2 = 0
							new_hold2 = 0
							new_hold_idx2 = 0
							new_hold_act_page2 = 0
							if rte12_act == rte12_sel then
								item_sel = 0
								item_sel_act = 0
								item_sel_via = 0
							end
							if rte12_act == rte12_sel_fo then
								item_sel2 = 0
								item_sel_act2 = 0
								item_sel_via2 = 0
							end
						end
						calc_rte_enable8 = 1
					else
						legs_data8m = {}
						legs_num8m = 0
						
						kk = offset_mod2
						for ii = kk, legs_num8 + 1 do
							legs_num8m = legs_num8m + 1
							legs_data8m[legs_num8m] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8m[legs_num8m][jj] = legs_data8[ii][jj]
							end
						end
						
						for ii = 1, legs_num8mm do
							legs_data8[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8[kk][jj] = legs_data8mm[ii][jj]
							end
							kk = kk + 1
						end
						
						for ii = 1, legs_num8m do
							legs_data8[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8[kk][jj] = legs_data8m[ii][jj]
							end
							kk = kk + 1
						end
						
						legs_num8 = legs_num8 + legs_num8mm
						
						--recreate Hold data
						hold_data_num4 = 0
						hold_data4 = {}
						for ii = 1, legs_num8 do
							if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
								hold_data_num4 = hold_data_num4 + 1
								hold_data4[hold_data_num4] = ii
							end
						end
						
						if new_hold_idx2_tmp ~= 0 then
							new_hold_idx2 = new_hold_idx2_tmp
						end
						
						calc_rte_enable8 = 1
						
					end
				end
			else
				offset_mod2 = offset
			end
		elseif offset ~= last_offset8 and legs_num > 1 then
			if legs_intdir == 0 and offset_mod2 > 0 and offset_mod2 <= legs_num8 - 1 and offset > 0 and offset <= legs_num then
				legs_num8mm = 0
				if legs_data8[offset_mod2][52] < 0 then
					legs_data8mm = {}
					ii = offset_mod2
					while legs_data8[ii][52] < 0 do
						legs_num8mm = legs_num8mm + 1
						legs_data8mm[legs_num8mm] = {}
						for jj = 1, MAX_LEGS_DATA do
							legs_data8mm[legs_num8mm][jj] = legs_data8[ii][jj]
						end
						ii = ii + 1
						if ii > legs_num8 then
							break
						end
					end
					if legs_num8mm > 0 then
						if legs_data8[offset_mod2][1] == "PPOS" and (legs_data8[offset_mod2][21] == 0 or legs_data8[offset_mod2][21] == 1) then
							if new_hold_idx2 == offset_mod2 then
								new_hold_idx2_tmp = new_hold_idx2 + 1
							end
						end
						
						kk = offset_mod2
						for ii = offset_mod2 + legs_num8mm, legs_num8 + 1 do
							for jj = 1, MAX_LEGS_DATA do
								legs_data8[kk][jj] = legs_data8[ii][jj]
							end
							kk = kk + 1
						end
						
					end
					legs_num8 = legs_num8 - legs_num8mm
					kk = 0
					
				end
				if legs_data8[offset_mod2 + 1][25] == 0 then
					if offset_mod2 + 2 < legs_num8 then
						for ii = offset_mod2 + 2, legs_num8 do
							if legs_data8[ii][25] ~= 0 then
								ll = ii
								mm = legs_data8[ii][25]
								break
							end
						end
						if ll ~= 0 and mm >= offset and mm <= legs_num8 then
							-- copy legs_data8
							legs_data8m = {}
							legs_num8m = 0
							for ii = ll, legs_num8 + 1 do
								kk = kk + 1
								legs_data8m[kk] = {}
								for jj = 1, MAX_LEGS_DATA do
									legs_data8m[kk][jj] = legs_data8[ii][jj]
								end
							end
							-- add current wpt and next wpts
							for ii = offset, mm do
								nn = nn + 1
								legs_data8[offset_mod2+nn] = {}
								for jj = 1, MAX_LEGS_DATA do
									legs_data8[offset_mod2+nn][jj] = legs_data[ii][jj]
								end
								legs_data8[offset_mod2+nn][25] = ii
							end
							-- paste legs_data8
							for ii = 1, kk do
								legs_data8[offset_mod2 + nn + ii] = {}
								for jj = 1, MAX_LEGS_DATA do
									legs_data8[offset_mod2 + nn + ii][jj] = legs_data8m[ii][jj]
								end
							end
							legs_num8 = legs_num8 + nn
							offset_mod2 = offset
							calc_rte_enable8 = 1
						end
					end
				else
					if legs_data8[offset_mod2+1][25] > offset then
						-- copy legs_data8
						legs_data8m = {}
						legs_num8m = 0
						for ii = offset_mod2 + 1, legs_num8 + 1 do
							kk = kk + 1
							legs_data8m[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8m[kk][jj] = legs_data8[ii][jj]
							end
						end
						-- add current wpt
						for jj = 1, MAX_LEGS_DATA do
							legs_data8[offset_mod2+1][jj] = legs_data[offset][jj]
						end
						legs_data8[offset_mod2+1][25] = offset
						-- paste legs_data8
						for ii = 1, kk do
							legs_data8[offset_mod2 + ii + 1] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8[offset_mod2 + ii + 1][jj] = legs_data8m[ii][jj]
							end
						end
						legs_num8 = legs_num8 + 1
						offset_mod2 = offset
						calc_rte_enable8 = 1
					elseif legs_data8[offset_mod2+1][25] == offset then
						offset_mod2 = offset
						calc_rte_enable8 = 1
					end
				end
				
				if legs_num8mm > 0 then
					if offset_mod2 > legs_num8 then
						-- ERASE PPOS HOLD
						if legs_data8mm[1][1] == "PPOS" and (legs_data8mm[1][21] == 0 or legs_data8mm[1][21] == 1) then
							hold_exec2 = 0
							new_hold2 = 0
							new_hold_idx2 = 0
							new_hold_act_page2 = 0
							if rte12_act == rte12_sel then
								item_sel = 0
								item_sel_act = 0
								item_sel_via = 0
							end
							if rte12_act == rte12_sel_fo then
								item_sel2 = 0
								item_sel_act2 = 0
								item_sel_via2 = 0
							end
						end
					else
						legs_data8m = {}
						legs_num8m = 0
						
						kk = offset_mod2
						for ii = kk, legs_num8 + 1 do
							legs_num8m = legs_num8m + 1
							legs_data8m[legs_num8m] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8m[legs_num8m][jj] = legs_data8[ii][jj]
							end
						end
						
						for ii = 1, legs_num8mm do
							legs_data8[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8[kk][jj] = legs_data8mm[ii][jj]
							end
							kk = kk + 1
						end
						
						for ii = 1, legs_num8m do
							legs_data8[kk] = {}
							for jj = 1, MAX_LEGS_DATA do
								legs_data8[kk][jj] = legs_data8m[ii][jj]
							end
							kk = kk + 1
						end
						
						legs_num8 = legs_num8 + legs_num8mm
						
						--recreate Hold data
						hold_data_num4 = 0
						hold_data4 = {}
						for ii = 1, legs_num8 do
							if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
								hold_data_num4 = hold_data_num4 + 1
								hold_data4[hold_data_num4] = ii
							end
						end
						
						if new_hold_idx2_tmp ~= 0 then
							new_hold_idx2 = new_hold_idx2_tmp
						end
						
						calc_rte_enable8 = 1
					end
				end
			end
		end
		if offset_mod2 > legs_num8 and legs_num8 > 2 then
			offset_mod2 = legs_num8
		elseif offset_mod2 > legs_num8 + 1 and legs_num8 > 0 then
			offset_mod2 = legs_num8 + 1
		elseif offset_mod2 <= 2 and legs_num8 > 1 then
			offset_mod2 = 2
			if offset_mod2 <= legs_num8 then
				if string.sub(legs_data8[offset_mod2][1], 1, 2) == "RW" and legs_data8[offset_mod2][19] == 1 then
					offset_mod2 = offset_mod2 + 1
				end
			end
		end
		
		last_offset8  = offset
		offset_mod_x8 = offset_mod2
	else
		if legs_num8 < 2 then
			offset_mod2 = 0
		elseif offset_mod2 > legs_num8 and legs_num8 > 2 then
			offset_mod2 = legs_num8
		elseif offset_mod2 > legs_num8 + 1 and legs_num8 > 0 then
			offset_mod2 = legs_num8 + 1
		elseif offset_mod2 <= 2 and legs_num8 > 1 then
			offset_mod2 = 2
			if offset_mod2 <= legs_num8 then
				if string.sub(legs_data8[offset_mod2][1], 1, 2) == "RW" and legs_data8[offset_mod2][19] == 1 then
					offset_mod2 = offset_mod2 + 1
				end
			end
		elseif offset_mod2 == 3 then
			if string.sub(legs_data8[2][1], 1, 2) ~= "RW" or legs_data8[2][19] ~= 1 then
				offset_mod2 = 2
			end
		end
		offset_mod_x8 = offset_mod2
	end
	
	if offset_mod2 > 1 and offset_mod2 <= legs_num8 and (legs_delete ~= 0 or legs_num <= 1) and rte12_act == 1 then
		B738DR_fmc_dist_mod = nd_calc_dist(simDR_latitude, simDR_longitude, legs_data8[offset_mod2][7], legs_data8[offset_mod2][8])
		B738DR_fmc_crs_mod = (nd_calc_brg(simDR_latitude, simDR_longitude, legs_data8[offset_mod2][7], legs_data8[offset_mod2][8]) + simDR_mag_variation + 360) % 360
	elseif rte12_act == 1 then
		B738DR_fmc_dist_mod = B738DR_fmc_dist
		B738DR_fmc_crs_mod = B738DR_fmc_crs
	end
	
	
	
	
	B738DR_legs_mod_active = legs_delete
	
	if fms_recalc == 1 then
		if is_timer_scheduled(rst_fms_recalc) == true then
			stop_timer(rst_fms_recalc)
		end
		run_after_time(rst_fms_recalc, 1.5)
		fms_recalc = 2
	elseif fms_recalc == 3 then
		if vnav_update_mod == 0 then
			fms_recalc = 0
		end
	end
end


function nav_data_find(nav_data_inp, nav_in_mode)
	
	raw_table('nav_data')
	local nav_data = {}
	local nav_data_n = 0
	local ii = 0
	local add_nav_data = 0
	
	-- find_navaid(entry, "", 0, "", in_rte)
	find_navaid(nav_data_inp, "", 4, "", 0)
	
	if navaid_list_n == 0 then
		ref_nav_wpt = "-----"
		ref_nav_navaid = "----"
		ref_nav_apt = "----"
		ref_nav_new = "*****"
		add_fmc_msg(NOT_IN_DATABASE, 1)
	else
		-- B738DR_fms_test3 = navaid_list[1][1]
		for ii = 1, navaid_list_n do
			add_nav_data = 0
			if nav_data_type == 0 then -- WPT
				if navaid_list[ii][1] == 4 then
					add_nav_data = 1
				end
			elseif nav_data_type == 1 then -- NAVAID
				-- if navaid_list[ii][1] < 4 or navaid_list[ii][1] == 5 then
				if navaid_list[ii][1] ~= 4 and navaid_list[ii][1] ~= 9 then
					add_nav_data = 1
				end
			else -- APT
				if navaid_list[ii][1] == 9 then
					add_nav_data = 1
				end
			end
			if add_nav_data == 1 then
				nav_data_n = nav_data_n + 1
				nav_data[nav_data_n] = {}
				for jj = 1, 8 do
					nav_data[nav_data_n][jj] = navaid_list[ii][jj]
				end
				nav_data[nav_data_n][20] = navaid_list[ii][20]
				nav_data[nav_data_n][21] = navaid_list[ii][21]
				nav_data[nav_data_n][22] = navaid_list[ii][22]
				nav_data[nav_data_n][23] = navaid_list[ii][23]
				nav_data[nav_data_n][24] = navaid_list[ii][24]
			end
		end
		navaid_list_n = nav_data_n
		if navaid_list_n == 0 then
			add_fmc_msg(NOT_IN_DATABASE, 1)
		else
			for ii = 1, nav_data_n do
				for jj = 1, 8 do
					navaid_list[ii][jj] = nav_data[ii][jj]
				end
				navaid_list[ii][20] = nav_data[ii][20]
				navaid_list[ii][21] = nav_data[ii][21]
				navaid_list[ii][22] = nav_data[ii][22]
				navaid_list[ii][23] = nav_data[ii][23]
				navaid_list[ii][24] = nav_data[ii][24]
			end
			if nav_in_mode == 0 then
				if navaid_list_n == 1 then
					if nav_data_type == 0 then -- WPT
						ref_nav_wpt = entry
						ref_nav_navaid = "----"
						ref_nav_apt = "----"
					elseif nav_data_type == 1 then -- NAVAID
						ref_nav_wpt = "-----"
						ref_nav_navaid = entry
						ref_nav_apt = "----"
					else -- APT
						ref_nav_wpt = "-----"
						ref_nav_navaid = "----"
						ref_nav_apt = entry
					end
					ref_nav_new = "*****"
					nav_data_idx = 1
				else
					page_ref_nav_data = 0
					page_ref_sel = 1
				end
				nav_data_entry = entry
				entry = ""
			else
				if navaid_list_n == 1 then
					if nav_data_type == 0 then -- WPT
						ref_nav_wpt = entry2
						ref_nav_navaid = "----"
						ref_nav_apt = "----"
					elseif nav_data_type == 1 then -- NAVAID
						ref_nav_wpt = "-----"
						ref_nav_navaid = entry2
						ref_nav_apt = "----"
					else -- APT
						ref_nav_wpt = "-----"
						ref_nav_navaid = "----"
						ref_nav_apt = entry2
					end
					ref_nav_new = "*****"
					nav_data_idx = 1
				else
					page_ref_nav_data2 = 0
					page_ref_sel2 = 1
				end
				nav_data_entry = entry2
				entry2 = ""
			end
		end
	end
	
end

function entry_altitude(x_entry)
	
	local result = 0
	local strlen = string.len(x_entry)
	local n = 0
	
	if strlen == 5 and string.sub(x_entry, 1, 2) == "FL" then
		n = tonumber(string.sub(x_entry, 3, 5))
		if n ~= nil then
			if n >= 1 and n <= 9 then
				if string.sub(x_entry, 3, 4) == "00" then
					result = roundUpToIncrement(n * 100, 10)
				end
			elseif n >= 10 and n <= 99 then
				if string.sub(x_entry, 3, 3) == "0" then
					result = roundUpToIncrement(n * 100, 10)
				end
			elseif n >= 100 and n <= 410 then
				result = roundUpToIncrement(n * 100, 10)
			end
		end
	elseif strlen == 2 then
		n = tonumber(x_entry)
		if n ~= nil then
			if n >= 1 and n <= 9 then
				if string.sub(x_entry, 1, 1) == "0" then
					result = roundUpToIncrement(n * 100, 10)
				end
			elseif n >= 10 and n <= 99 then
				result = roundUpToIncrement(n * 100, 10)
			end
		end
	elseif strlen == 3 then
		n = tonumber(x_entry)
		if n ~= nil then
			if n >= 1 and n <= 9 then
				if string.sub(x_entry, 1, 2) == "00" then
					result = roundUpToIncrement(n * 100, 10)
				end
			elseif n >= 10 and n <= 99 then
				if string.sub(x_entry, 1, 1) == "0" then
					result = roundUpToIncrement(n * 100, 10)
				end
			elseif n >= 100 and n <= 410 then
				result = roundUpToIncrement(n * 100, 10)
			end
		end
	elseif strlen == 4 then
		n = tonumber(x_entry)
		if n ~= nil then
			if n >= 1 and n <= 9 then
				if string.sub(x_entry, 1, 3) == "000" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 10 and n <= 99 then
				if string.sub(x_entry, 1, 2) == "00" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 100 and n <= 999 then
				if string.sub(x_entry, 1, 1) == "0" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 1000 and n <= 9999 then
				result = roundUpToIncrement(n, 10)
			end
		end
	elseif strlen == 5 then
		n = tonumber(x_entry)
		if n ~= nil then
			if n >= 1 and n <= 9 then
				if string.sub(x_entry, 1, 4) == "0000" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 10 and n <= 99 then
				if string.sub(x_entry, 1, 3) == "000" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 100 and n <= 999 then
				if string.sub(x_entry, 1, 2) == "00" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 1000 and n <= 9999 then
				if string.sub(x_entry, 1, 1) == "0" then
					result = roundUpToIncrement(n, 10)
				end
			elseif n >= 10000 and n <= 41000 then
				result = roundUpToIncrement(n, 10)
			end
		end
	end
	return result
end

---------------------------------------------------------------

function read_ref_list_icao(in_ref_icao, in_rte)
	
	if apt_exist2(in_ref_icao) == true then
		if cust_def_data == 1 then
			file_name = "Custom Data/CIFP/" .. in_ref_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist2(in_ref_icao) == true then
					sid_num = 0
					sid_tns_num = 0
					star_num = 0
					ref_data_star_tns_n = 0
					ref_data_app_n = 0
					ref_data_app_tns_n = 0
					ref_ed_app_n = 0
				end
			else
				read_ref_data(in_ref_icao, in_rte)		-- read reference airport data
				file_navdata:close()
			end
		elseif cust_def_data == 2 then
			file_name = "Resources/default data/CIFP/" .. in_ref_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist2(in_ref_icao) == true then
					sid_num = 0
					sid_tns_num = 0
					star_num = 0
					ref_data_star_tns_n = 0
					ref_data_app_n = 0
					ref_data_app_tns_n = 0
					ref_ed_app_n = 0
				end
			else
				read_ref_data(in_ref_icao, in_rte)		-- read reference airport data
				file_navdata:close()
			end
		end
	
	end
end

function read_des_list_icao(in_des_icao, in_rte)
	
	if apt_exist2(in_des_icao) == true then
	
		if cust_def_data == 1 then
			file_name = "Custom Data/CIFP/" .. in_des_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist2(in_des_icao) == true then
					-- delete list of approach
					data_des_star_n = 0
					data_des_star_tns_n = 0
					data_des_app_n = 0
					data_des_app_tns_n = 0
					ed_app_num = 0
				end
			else
				read_des_data(in_des_icao, in_rte)		-- read reference airport data
				file_navdata:close()
			end
		elseif cust_def_data == 2 then
			file_name = "Resources/default data/CIFP/" .. in_des_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist2(in_des_icao) == true then
					-- delete list of approach
					data_des_star_n = 0
					data_des_star_tns_n = 0
					data_des_app_n = 0
					data_des_app_tns_n = 0
					ed_app_num = 0
				end
			else
				read_des_data(in_des_icao, in_rte)		-- read reference airport data
				file_navdata:close()
			end
		end
		
	end
	
end

function ref_apt_exist(in_ref_icao, in_rte)
	
	local result = false
	
	if apt_exist(in_ref_icao) == true then
		if cust_def_data == 1 then
			file_name = "Custom Data/CIFP/" .. in_ref_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist(in_ref_icao) == true then
					result = true
					sid_num = 0
					sid_tns_num = 0
					star_num = 0
					ref_data_star_tns_n = 0
					ref_data_app_n = 0
					ref_data_app_tns_n = 0
					ref_ed_app_n = 0
				end
			else
				read_ref_data(in_ref_icao, in_rte)		-- read reference airport data
				file_navdata:close()
				result = true
			end
		elseif cust_def_data == 2 then
			file_name = "Resources/default data/CIFP/" .. in_ref_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist(in_ref_icao) == true then
					result = true
					sid_num = 0
					sid_tns_num = 0
					star_num = 0
					ref_data_star_tns_n = 0
					ref_data_app_n = 0
					ref_data_app_tns_n = 0
					ref_ed_app_n = 0
				end
			else
				read_ref_data(in_ref_icao, in_rte)		-- read reference airport data
				file_navdata:close()
				result = true
			end
		end
	
	end
	
	return result
	
end

function des_apt_exist(in_des_icao, in_rte)
	
	local result = false
	
	if apt_exist(in_des_icao) == true then
	
		if cust_def_data == 1 then
			file_name = "Custom Data/CIFP/" .. in_des_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist(in_des_icao) == true then
					result = true
					-- delete list of approach
					data_des_star_n = 0
					data_des_star_tns_n = 0
					data_des_app_n = 0
					data_des_app_tns_n = 0
					ed_app_num = 0
				end
			else
				read_des_data(in_des_icao, in_rte)		-- read reference airport data
				file_navdata:close()
				result = true
			end
		elseif cust_def_data == 2 then
			file_name = "Resources/default data/CIFP/" .. in_des_icao .. ".dat"
			file_navdata = io.open(file_name, "r")
			if file_navdata == nil then
				if apt_exist(in_des_icao) == true then
					result = true
					-- delete list of approach
					data_des_star_n = 0
					data_des_star_tns_n = 0
					data_des_app_n = 0
					data_des_app_tns_n = 0
					ed_app_num = 0
				end
			else
				read_des_data(in_des_icao, in_rte)		-- read reference airport data
				file_navdata:close()
				result = true
			end
		end
		
	end
	
	return result
	
end

function entry_wind(in_entry)
	local strlen = string.len(in_entry)
	local n = 0
	local nn = 0
	local dir_tmp = 0
	local spd_tmp = 0
	local result_dir = 0
	local result_spd = 0
	if strlen > 2 then
		nn,n = string.find(in_entry, "/")
		if nn == nil then
			result_spd = 0
		elseif nn > 1 and nn < strlen then
			dir_tmp = tonumber(string.sub(in_entry, 1, nn - 1))
			spd_tmp = tonumber(string.sub(in_entry, nn + 1, -1))
			if dir_tmp ~= nil and spd_tmp ~= nil then
				if dir_tmp >= 0 and dir_tmp <= 360 and spd_tmp > 0 and spd_tmp < 200 then
					result_dir = displ_crs(dir_tmp)
					result_spd = spd_tmp
				end
			end
		end
	end
	return result_dir, result_spd
end

function clr_star_app(in_rte)
	local xyz = 0
	
	if in_rte == 0 then
		for xyz = 2, legs_num2 do
			-- change STAR,APP to route
			if legs_data2[xyz][19] == 2 or legs_data2[xyz][19] == 4 or legs_data2[xyz][19] == 7 or legs_data2[xyz][19] == 8 or legs_data2[xyz][19] == 9 then
				legs_data2[xyz][19] = 0	-- wpt type STAR, APP
				legs_data2[xyz][4] = 0	-- spd rest
				legs_data2[xyz][5] = 0	-- alt rest
				legs_data2[xyz][6] = 0	-- alt type rest
				legs_data2[xyz][9] = ""		--via
			end
			-- clear FAF,FixMA
			legs_data2[xyz][17] = 0
			legs_data2[xyz][44] = 0
		end
	elseif in_rte == 1 then
		for xyz = 2, legs_num8 do
			-- change STAR,APP to route
			if legs_data8[xyz][19] == 2 or legs_data8[xyz][19] == 4 or legs_data8[xyz][19] == 7 or legs_data8[xyz][19] == 8 or legs_data8[xyz][19] == 9 then
				legs_data8[xyz][19] = 0	-- wpt type STAR, APP
				legs_data8[xyz][4] = 0	-- spd rest
				legs_data8[xyz][5] = 0	-- alt rest
				legs_data8[xyz][6] = 0	-- alt type rest
				legs_data8[xyz][9] = ""		--via
			end
			-- clear FAF,FixMA
			legs_data8[xyz][17] = 0
			legs_data8[xyz][44] = 0
		end
	end
end

function check_sid_rwy()
	local sid_ok = 0
	if rte12_act == 0 then
		if ref_rwy2 ~= "-----" and ref_sid2 ~= "------" then
			if sid_list_num_1 ~= 0 then
				for sss = 1, sid_list_num_1 do
					if sid_list_1[sss][1] == ref_sid2 then
						sid_ok = 1
						break
					end
				end
			end
		else
			sid_ok = 1
		end
	elseif rte12_act == 1 then
		if ref_rwy8 ~= "-----" and ref_sid8 ~= "------" then
			if sid_list_num_2 ~= 0 then
				for sss = 1, sid_list_num_2 do
					if sid_list_2[sss][1] == ref_sid8 then
						sid_ok = 1
						break
					end
				end
			end
		else
			sid_ok = 1
		end
	end
	if sid_ok == 0 then
		return false
	else
		return true
	end
end

function erase_modify(in_rte)
	if in_rte == 0 then
		ref_icao_x = ref_icao
		des_icao_x = des_icao
		ref_gate_x =  ref_gate
		co_route_x = co_route
		ref_rwy2 = ref_rwy
		ref_rwy3 = ref_rwy
		ref_sid2 = ref_sid
		ref_sid_tns2 = ref_sid_tns
		des_star2 = des_star
		des_star_trans2 = des_star_trans
		des_app2 = des_app
		des_app_tns2 = des_app_tns
		rw_ext_dist_x = rw_ext_dist
		rw_ext_fpa_x = rw_ext_fpa
		
		ref_icao_lat_x = ref_icao_lat
		ref_icao_lon_x = ref_icao_lon
		des_icao_lat_x = des_icao_lat
		des_icao_lon_x = des_icao_lon
		ref_tns_alt_x = ref_tns_alt
		des_tns_alt_x = des_tns_alt
		des_tns_lvl_x = des_tns_lvl
		ref_icao_alt_x = ref_icao_alt
		des_icao_alt_x = des_icao_alt
		arr_data_x = arr_data
		des_app_rw_only_x = des_app_rw_only
		
		des_star2_old = des_star2
		des_star_trans2_old = des_star_trans2
		des_app2_old = des_app2
		des_app_tns2_old = des_app_tns2
		
		sel_ref_rwy2 = ref_rwy2
		sel_ref_sid2 = ref_sid2
		sel_ref_sid_tns2 = ref_sid_tns2
		sel_des_star2 = des_star2
		sel_des_star_trans2 = des_star_trans2
		sel_des_app2 = des_app2
		sel_des_app_tns2 = des_app_tns2
	elseif in_rte == 1 then
		ref_icao7_x = ref_icao7
		des_icao7_x = des_icao7
		ref_gate7_x =  ref_gate7
		co_route7_x = co_route7
		ref_rwy8 = ref_rwy7
		ref_rwy9 = ref_rwy7
		ref_sid8 = ref_sid7
		ref_sid_tns8 = ref_sid_tns7
		des_star8 = des_star7
		des_star_trans8 = des_star_trans7
		des_app8 = des_app7
		des_app_tns8 = des_app_tns7
		rw_ext_dist7_x = rw_ext_dist7
		rw_ext_fpa7_x = rw_ext_fpa7
		
		ref_icao_lat7_x = ref_icao_lat7
		ref_icao_lon7_x = ref_icao_lon7
		des_icao_lat7_x = des_icao_lat7
		des_icao_lon7_x = des_icao_lon7
		ref_tns_alt7_x = ref_tns_alt7
		des_tns_alt7_x = des_tns_alt7
		des_tns_lvl7_x = des_tns_lvl7
		ref_icao_alt7_x = ref_icao_alt7
		des_icao_alt7_x = des_icao_alt7
		arr_data7_x = arr_data7
		des_app_rw_only7_x = des_app_rw_only7
		
		des_star8_old = des_star8
		des_star_trans8_old = des_star_trans8
		des_app8_old = des_app8
		des_app_tns8_old = des_app_tns8
		
		sel_ref_rwy8 = ref_rwy8
		sel_ref_sid8 = ref_sid8
		sel_ref_sid_tns8 = ref_sid_tns8
		sel_des_star8 = des_star8
		sel_des_star_trans8 = des_star_trans8
		sel_des_app8 = des_app8
		sel_des_app_tns8 = des_app_tns8
	end
end

function copy_rte_data(in_rte)
	if in_rte == 0 then
		ref_icao_x = ref_icao7
		des_icao_x = des_icao7
		ref_gate_x =  ref_gate7
		co_route_x = co_route7
		ref_rwy2 = ref_rwy7
		ref_rwy3 = ref_rwy7
		ref_sid2 = ref_sid7
		ref_sid_tns2 = ref_sid_tns7
		des_star2 = des_star7
		des_star_trans2 = des_star_trans7
		des_app2 = des_app7
		des_app_tns2 = des_app_tns7
		rw_ext_dist_x = rw_ext_dist7
		rw_ext_fpa_x = rw_ext_fpa7
		
		ref_icao_lat_x = ref_icao_lat7
		ref_icao_lon_x = ref_icao_lon7
		des_icao_lat_x = des_icao_lat7
		des_icao_lon_x = des_icao_lon7
		ref_tns_alt_x = ref_tns_alt7
		des_tns_alt_x = des_tns_alt7
		des_tns_lvl_x = des_tns_lvl7
		ref_icao_alt_x = ref_icao_alt7
		des_icao_alt_x = des_icao_alt7
		arr_data_x = arr_data7
		des_app_rw_only_x = des_app_rw_only7
		
		des_star2_old = des_star8
		des_star_trans2_old = des_star_trans8
		des_app2_old = des_app8
		des_app_tns2_old = des_app_tns8
		
		sel_ref_rwy2 = ref_rwy8
		sel_ref_sid2 = ref_sid8
		sel_ref_sid_tns2 = ref_sid_tns8
		sel_des_star2 = des_star8
		sel_des_star_trans2 = des_star_trans8
		sel_des_app2 = des_app8
		sel_des_app_tns2 = des_app_tns8
		
		if ref_rwy7 ~= "-----" then
			ref_rwy = ref_rwy7
		end
	elseif in_rte == 1 then
		ref_icao7_x = ref_icao
		des_icao7_x = des_icao
		ref_gate7_x =  ref_gate
		co_route7_x = co_route
		ref_rwy8 = ref_rwy
		ref_rwy9 = ref_rwy
		ref_sid8 = ref_sid
		ref_sid_tns8 = ref_sid_tns
		des_star8 = des_star
		des_star_trans8 = des_star_trans
		des_app8 = des_app
		des_app_tns8 = des_app_tns
		rw_ext_dist7_x = rw_ext_dist
		rw_ext_fpa7_x = rw_ext_fpa
		
		ref_icao_lat7_x = ref_icao_lat
		ref_icao_lon7_x = ref_icao_lon
		des_icao_lat7_x = des_icao_lat
		des_icao_lon7_x = des_icao_lon
		ref_tns_alt7_x = ref_tns_alt
		des_tns_alt7_x = des_tns_alt
		des_tns_lvl7_x = des_tns_lvl
		ref_icao_alt7_x = ref_icao_alt
		des_icao_alt7_x = des_icao_alt
		arr_data7_x = arr_data
		des_app_rw_only7_x = des_app_rw_only
		
		des_star8_old = des_star2
		des_star_trans8_old = des_star_trans2
		des_app8_old = des_app2
		des_app_tns8_old = des_app_tns2
		
		sel_ref_rwy8 = ref_rwy2
		sel_ref_sid8 = ref_sid2
		sel_ref_sid_tns8 = ref_sid_tns2
		sel_des_star8 = des_star2
		sel_des_star_trans8 = des_star_trans2
		sel_des_app8 = des_app2
		sel_des_app_tns8 = des_app_tns2
		
		if ref_rwy ~= "-----" then
			ref_rwy7 = ref_rwy
		end
	end
end

function rte12_copy_t()
	rte12_copy_done = 2
	copy_to_rte(1, 1)
end

function rte21_copy_t()
	rte21_copy_done = 2
	copy_to_rte(0, 1)
end

function copy_to_rte(in_rte, in_reset)
	-- in_rte = target
	if in_rte == 0 then
		
		copy_rte_data(in_rte)
		
		legs_num2 = 0
		legs_data2 = {}
		
		if legs_num > 0 then
			for ii = 1, legs_num + 1 do
				legs_data2[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data2[ii][jj] = legs_data[ii][jj]
				end
				if in_reset == 1 then
					legs_data2[ii][10] = 0
					legs_data2[ii][11] = 0
					legs_data2[ii][13] = 0
					legs_data2[ii][15] = 0
					legs_data2[ii][38] = ""
					legs_data2[ii][39] = ""
					legs_data2[ii][40] = 0
					legs_data2[ii][53] = 0
					legs_data2[ii][54] = 0
				end
			end
			legs_num2 = legs_num
		end
		create_fpln(in_rte)
		
		offset_mod = 0
		
		if legs_num2 > 1 then
			if in_flight_mode == 0 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_mod < 3 then
						offset_mod = 3
					end
				end
			else
				offset_mod = offset
				if offset_mod > legs_num2 then
					offset_mod = legs_num2
				end
			end
		end
		offset_mod_x2 = offset_mod
		-- last_offset = offset
	else
		
		copy_rte_data(in_rte)
		
		legs_num8 = 0
		legs_data8 = {}
		
		if legs_num > 0 then
			for ii = 1, legs_num + 1 do
				legs_data8[ii] = {}
				for jj = 1, MAX_LEGS_DATA do
					legs_data8[ii][jj] = legs_data[ii][jj]
				end
				if in_reset == 1 then
					legs_data8[ii][10] = 0
					legs_data8[ii][11] = 0
					legs_data8[ii][13] = 0
					legs_data8[ii][15] = 0
					legs_data8[ii][38] = ""
					legs_data8[ii][39] = ""
					legs_data8[ii][40] = 0
					legs_data8[ii][53] = 0
					legs_data8[ii][54] = 0
				end
			end
			legs_num8 = legs_num
		end
		create_fpln(in_rte)
		
		offset_mod2 = 0
		
		if legs_num8 > 1 then
			if in_flight_mode == 0 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_mod2 < 3 then
						offset_mod2 = 3
					end
				end
			else
				offset_mod2 = offset
				if offset_mod2 > legs_num8 then
					offset_mod2 = legs_num8
				end
			end
		end
		
		offset_mod_x8 = offset_mod2
		-- last_offset8 = offset
	end
end
---------------------------------------------------------------
function legs_1_lsk(in_key, in_fmc, in_rte)
	
	local item = 0
	local button = in_key
	local tmp_tmp = 0
	local jj = 0
	local kk = 0
	local invalid = 0
	local offset_tmp = 0
	
	if in_rte == 0 then
		if in_fmc == 0 then
			offset_tmp = offset_mod
			
			if nav_mode == 99 and legs_num2 == legs_num and rte12_act == 0 then
				-- item = legs_num2 + 1
				item = legs_num2 + button
			else
				if true_legs_num(2) == 0 then
					item = legs_num2 + 1
				elseif true_legs_num(2) > 0 then
					item = (act_page - 1) * 5 + offset_tmp - 1 + button
					if ref_icao_x ~= "----" and des_icao_x ~= "****" and legs_num2 == 1 then
						item = item + 2
					else
						if B738DR_no_rte2 == 0 and act_page > 1 then
							item = item - 1
						end
					end
				end
			end
			
			jj,kk = string.find(entry, "/")
			
			if new_hold_at ~= 0 then
				if item > 0 and item <= legs_num2 then
					rte_add_hold_fix(item, 0, in_rte)
					
					if act_page == 1 and rte12_act == in_rte then
						legs_intdir = 1
						legs_intdir_rte = in_rte
						legs_intdir_idx_mod = item
						
						-- compute >legs_intdir_crs
						local nd_lat = math.rad(simDR_latitude)
						local nd_lon = math.rad(simDR_longitude)
						
						local nd_lat2 = 0
						local nd_lon2 = 0
						if in_rte == 0 then
							legs_data2[item][31] = "DF"
							legs_data2[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs2(item)
						else
							legs_data8[item][31] = "DF"
							legs_data8[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs8(item)
						end
						nd_lat2 = math.rad(nd_lat2)
						nd_lon2 = math.rad(nd_lon2)
						
						local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
						local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
						local nd_hdg = math.atan2(nd_y, nd_x)
						nd_hdg = math.deg(nd_hdg)
						nd_hdg = (nd_hdg + 360) % 360
						legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
					end
				end
			elseif item > legs_num2 then
				tmp_tmp = legs_num2 + 1
				if item == tmp_tmp then
					if jj ~= nil then
						rte_add_wpt3(item, entry, 0, in_rte)
						if act_page == 1 and nav_mode == 99 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
						item_sel = 0
						item_sel_act = 0
					elseif string.len(entry) > 1 and string.len(entry) < 6 and item_sel == 0 then
						if wpt_lat_lon(entry) == true then
							legs_data2[item][31] = "TF"
							legs_data2[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
						else
						-- add waypoint last
							rte_add_wpt(item, entry, 0, in_rte)
						end
						
						if act_page == 1 and nav_mode == 99 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							
						end
						
						item_sel = 0
						item_sel_act = 0
					else
						if wpt_lat_lon(entry) == true then
							legs_data2[item][31] = "TF"
							legs_data2[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
							
							if act_page == 1 and nav_mode == 99 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
								
							end
							
							item_sel = 0
							item_sel_act = 0
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				if entry == ">DELETE" then
					-- delete waypoint
					if act_page == 1 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						rte_del_wpt(item, in_fmc, in_rte)
						entry = ""
					end
					item_sel = 0
					item_sel_act = 0
				elseif jj ~= nil and item > 1 then
					rte_add_wpt3(item, entry, 0, in_rte)
					item_sel = 0
					item_sel_act = 0
				elseif string.len(entry) > 1 and string.len(entry) < 6 and item_sel == 0 and item > 1 then
					-- Active wpt
					item_sel = find_act_route_wpt2(entry, item)
					if item_sel < 0 then
						item_sel = 0
					end
					if item > 1 and item_sel > 0 then
						if legs_data2[item-1][1] == legs_data2[item_sel][1] and legs_data2[item-1][7] == legs_data2[item_sel][7] and
						legs_data2[item-1][8] == legs_data2[item_sel][8] and item-1 ~= item_sel then
							item = item - 1
						elseif legs_data2[item+1][1] == legs_data2[item_sel][1] and legs_data2[item+1][7] == legs_data2[item_sel][7] and
						legs_data2[item+1][8] == legs_data2[item_sel][8] and item+1 ~= item_sel then
							item = item + 1
						end
					end
					if act_page == 1 then
						if item_sel > 0 then
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel >= item then
										if item_sel ~= item then
											create_legs_abeam_list(item, item_sel - 1, in_rte)
											rte_copy(item_sel, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable2 = 1
										end
										if rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
									if invalid == 0  then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
									end
									msg_unavaible_crz_alt = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item and item > 1 then
									create_legs_abeam_list(item, item_sel - 1, in_rte)
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								elseif item_sel == item and item > 1 and legs_data2[item_sel][1] == entry then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry = ""
								item_sel = 0
								item_sel_act = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel = 0
							item_sel_act = 0
						else
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry, 0, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry, 0, in_rte)
							end
							
							if invalid == 0 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							end
						end
					else
						if item_sel > 0 then
							if item > 1 then
								if legs_data2[item-1][1] == legs_data2[item_sel][1] and legs_data2[item-1][7] == legs_data2[item_sel][7] and
								legs_data2[item-1][8] == legs_data2[item_sel][8] and item-1 ~= item_sel then
									item = item - 1
								elseif legs_data2[item+1][1] == legs_data2[item_sel][1] and legs_data2[item+1][7] == legs_data2[item_sel][7] and
								legs_data2[item+1][8] == legs_data2[item_sel][8] and item+1 ~= item_sel then
									item = item + 1
								end
							end
							-- Item selected
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel > item then
										legs_data2[item_sel][31] = "TF"
										legs_data2[item_sel][21] = -1
										rte_copy(item_sel, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable2 = 1
									elseif item_sel < item then
										item_sel = item_sel + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel, in_rte)
										calc_rte_enable2 = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item and item > 1 then
									legs_data2[item_sel][31] = "TF"
									legs_data2[item_sel][21] = -1
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
								elseif item_sel < item and item > 1 then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable2 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry = ""
								item_sel = 0
								item_sel_act = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel = 0
							item_sel_act = 0
						else
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry, 0, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry, 0, in_rte)
							end
						end
					end
					item_sel = 0
					item_sel_act = 0
				elseif wpt_lat_lon(entry) == true and item_sel == 0 and item > 1 then
						legs_data2[item][31] = "TF"
						legs_data2[item_sel][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
						if act_page == 1 then
							if rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							end
						end
				elseif item_sel == 0 then
					if string.len(entry) == 0 then
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 then
								-- select item
								if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel = 0
									item_sel_act = 0
								else
									item_sel = item
									entry = legs_data2[item][1]
									item_sel_act = item
									item_sel_len = string.len(entry)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							end
						else
							-- select item
							if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							else
								item_sel = item
								entry = legs_data2[item][1]
								item_sel_act = item
								item_sel_len = string.len(entry)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel = 0
						item_sel_act = 0
					end
				else
					if item_sel > 0 and item > 1 then
						if string.sub(legs_data2[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data2[item_sel][1] ~= entry then
								item = 0
								item_sel = 0
							elseif legs_data2[item-1][1] == legs_data2[item_sel][1] and legs_data2[item-1][7] == legs_data2[item_sel][7] and
							legs_data2[item-1][8] == legs_data2[item_sel][8] and item-1 ~= item_sel then
								item = item - 1
							elseif legs_data2[item+1][1] == legs_data2[item_sel][1] and legs_data2[item+1][7] == legs_data2[item_sel][7] and
							legs_data2[item+1][8] == legs_data2[item_sel][8] and item+1 ~= item_sel then
								item = item + 1
							end
							-- Item selected
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 then
									-- entry item
									if item_sel > item or (item_sel == item and act_page == 1) then
										if item_sel ~= item or act_page ~= 1 then
											if act_page ~= 1 then
												legs_data2[item_sel][31] = "TF"
												legs_data2[item_sel][21] = -1
											end
											if act_page == 1 then
												create_legs_abeam_list(item, item_sel - 1, in_rte)
											end
											rte_copy(item_sel, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable2 = 1
										end
										if act_page == 1 and rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
										entry = ""
										item_sel = 0
										item_sel_act = 0
									elseif item_sel < item then
										item_sel = item_sel + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel, in_rte)
										calc_rte_enable2 = 1
										entry = ""
										item_sel = 0
										item_sel_act = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item then
									if act_page ~= 1 then
										legs_data2[item_sel][31] = "TF"
										legs_data2[item_sel][21] = -1
									end
									if act_page == 1 then
										create_legs_abeam_list(item, item_sel - 1, in_rte)
									end
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
									if act_page == 1 and rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
								elseif item_sel < item then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable2 = 1
									entry = ""
									item_sel = 0
									item_sel_act = 0
								elseif item_sel == item and act_page == 1 and legs_data2[item_sel][1] == entry then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					elseif item_sel > 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		else
			offset_tmp = offset_mod
			
			if nav_mode == 99 and legs_num2 == legs_num and rte12_act == 0 then
				-- item = legs_num2 + 1
				item = legs_num2 + button
			else
				if true_legs_num(2) == 0 then
					item = legs_num2 + 1
				elseif true_legs_num(2) > 0 then
					item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
					if ref_icao_x ~= "----" and des_icao_x ~= "****" and legs_num2 == 1 then
						item = item + 1
					else
						if B738DR_no_rte2 == 0 and act_page2 > 1 then
							item = item - 1
						end
					end
				end
			end
			jj,kk = string.find(entry2, "/")
			
			if new_hold_at ~= 0 then
				if item > 0 and item <= legs_num2 then
					rte_add_hold_fix(item, 1, in_rte)
					
					if act_page2 == 1 and rte12_act == in_rte then
						legs_intdir = 1
						legs_intdir_rte = in_rte
						legs_intdir_idx_mod = item
						
						-- compute >legs_intdir_crs
						local nd_lat = math.rad(simDR_latitude)
						local nd_lon = math.rad(simDR_longitude)
						
						local nd_lat2 = 0
						local nd_lon2 = 0
						if in_rte == 0 then
							legs_data2[item][31] = "DF"
							legs_data2[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs2(item)
						else
							legs_data8[item][31] = "DF"
							legs_data8[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs8(item)
						end
						nd_lat2 = math.rad(nd_lat2)
						nd_lon2 = math.rad(nd_lon2)
						
						local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
						local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
						local nd_hdg = math.atan2(nd_y, nd_x)
						nd_hdg = math.deg(nd_hdg)
						nd_hdg = (nd_hdg + 360) % 360
						legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
					end
				end
			elseif item > legs_num2 or (item == 1 and legs_num2 == 1) then
				tmp_tmp = legs_num2 + 1
				if item == tmp_tmp or (item == 1 and legs_num2 == 1) then
					if jj ~= nil then
						rte_add_wpt3(item, entry2, 1, in_rte)
						if act_page2 == 1 and nav_mode == 99 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
						item_sel2 = 0
						item_sel_act2 = 0
					elseif string.len(entry2) > 1 and string.len(entry2) < 6 and item_sel2 == 0 then
						if wpt_lat_lon(entry2) == true then
							legs_data2[item][31] = "TF"
							legs_data2[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
						else
						-- add waypoint last
							rte_add_wpt(item, entry2, 1, in_rte)
						end
						if act_page2 == 1 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
						item_sel2 = 0
						item_sel_act2 = 0
					else
						if wpt_lat_lon(entry2) == true then
							legs_data2[item][31] = "TF"
							legs_data2[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
							
							if act_page2 == 1 and nav_mode == 99 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
								
							end
							
							item_sel2 = 0
							item_sel_act2 = 0
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				if entry2 == ">DELETE" then
					-- delete waypoint
					if act_page2 == 1 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						rte_del_wpt(item, in_fmc, in_rte)
						entry2 = ""
					end
					item_sel2 = 0
					item_sel_act2 = 0
				elseif jj ~= nil then
					rte_add_wpt3(item, entry2, 1, in_rte)
					item_sel2 = 0
					item_sel_act2 = 0
				elseif string.len(entry2) > 1 and string.len(entry2) < 6 and item_sel2 == 0 then
					item_sel2 = find_act_route_wpt2(entry2, item)
					if item_sel2 < 0 then
						item_sel2 = 0
					end
					if item > 1 and item_sel2 > 0 then
						if legs_data2[item-1][1] == legs_data2[item_sel2][1] and legs_data2[item-1][7] == legs_data2[item_sel2][7] and
						legs_data2[item-1][8] == legs_data2[item_sel2][8] and item-1 ~= item_sel2 then
							item = item - 1
						elseif legs_data2[item+1][1] == legs_data2[item_sel2][1] and legs_data2[item+1][7] == legs_data2[item_sel2][7] and
						legs_data2[item+1][8] == legs_data2[item_sel2][8] and item+1 ~= item_sel2 then
							item = item + 1
						end
					end
					if act_page2 == 1 then
						if item_sel2 > 0 then
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel2 >= item then
										if item_sel2 ~= item then
											-- create legs abeam wpts
											-- from item to item_sel2 - 1
											create_legs_abeam_list(item, item_sel2 - 1, in_rte)
											rte_copy(item_sel2, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable2 = 1
										end
										if rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel2 > item and item > 1 then
									-- create legs abeam wpts
									-- from item to item_sel2 - 1
									create_legs_abeam_list(item, item_sel2 - 1, in_rte)
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								elseif item_sel2 == item and item > 1 and legs_data2[item_sel2][1] == entry2 then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry2 = ""
								item_sel2 = 0
								item_sel_act2 = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel2 = 0
							item_sel_act2 = 0
						else
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry2, 1, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry2, 1, in_rte)
							end
							
							if invalid == 0 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							end
						end
					else
						if item_sel2 > 0 then
							-- Item selected
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel2 > item then
										legs_data2[item_sel2][31] = "TF"
										legs_data2[item_sel2][21] = -1
										rte_copy(item_sel2, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable2 = 1
									elseif item_sel2 < item then
										item_sel2 = item_sel2 + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel2, in_rte)
										calc_rte_enable2 = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel2 > item and item > 1 then
									legs_data2[item_sel2][31] = "TF"
									legs_data2[item_sel2][21] = -1
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
								elseif item_sel2 < item and item > 1 then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable2 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry2 = ""
								item_sel2 = 0
								item_sel_act2 = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel2 = 0
							item_sel_act2 = 0
						else
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry2, 1, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry2, 1, in_rte)
							end
						end
					end
					item_sel2 = 0
					item_sel_act2 = 0
				elseif wpt_lat_lon(entry2) == true and item_sel2 == 0 then
						legs_data2[item][31] = "TF"
						legs_data2[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
						if act_page2 == 1 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
				elseif item_sel2 == 0 then
					if string.len(entry2) == 0 then
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 then
								-- select item
								if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel2 = 0
									item_sel_act2 = 0
								else
									item_sel2 = item
									entry2 = legs_data2[item][1]
									item_sel_act2 = item
									item_sel_len2 = string.len(entry2)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							end
						else
							-- select item
							if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							else
								item_sel2 = item
								entry2 = legs_data2[item][1]
								item_sel_act2 = item
								item_sel_len2 = string.len(entry2)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel2 = 0
						item_sel_act2 = 0
					end
				else
					if item_sel2 > 0 and item > 1 then
						if string.sub(legs_data2[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data2[item_sel2][1] ~= entry2 then
								item = 0
								item_sel2 = 0
							elseif legs_data2[item-1][1] == legs_data2[item_sel2][1] and legs_data2[item-1][7] == legs_data2[item_sel2][7] and
							legs_data2[item-1][8] == legs_data2[item_sel2][8] and item-1 ~= item_sel2 then
								item = item - 1
							elseif legs_data2[item+1][1] == legs_data2[item_sel2][1] and legs_data2[item+1][7] == legs_data2[item_sel2][7] and
							legs_data2[item+1][8] == legs_data2[item_sel2][8] and item+1 ~= item_sel2 then
								item = item + 1
							end
							if offset_act >= 5 then
								if legs_data2[item][19] ~= 6 then
									-- entry2 item
									if item_sel2 > item or (item_sel2 == item and act_page2 == 1) then
										if item_sel2 ~= item or act_page2 ~= 1 then
											if act_page2 ~= 1 then
												legs_data2[item_sel2][31] = "TF"
												legs_data2[item_sel2][21] = -1
											end
											if act_page2 == 1 then
												-- create legs abeam wpts
												-- from item to item_sel2 - 1
												create_legs_abeam_list(item, item_sel2 - 1, in_rte)
											end
											rte_copy(item_sel2, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable2 = 1
										end
										if act_page2 == 1 and rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									elseif item_sel2 < item then
										item_sel2 = item_sel2 + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel2, in_rte)
										calc_rte_enable2 = 1
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry2 item
								if item_sel2 > item then
									if act_page2 ~= 1 then
										legs_data2[item_sel2][31] = "TF"
										legs_data2[item_sel2][21] = -1
									end
									if act_page2 == 1 then
										-- create legs abeam wpts
										-- from item to item_sel2 - 1
										create_legs_abeam_list(item, item_sel2 - 1, in_rte)
									end
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
									if act_page2 == 1 and rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								elseif item_sel2 < item then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable2 = 1
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								elseif item_sel2 == item and act_page2 == 1 and legs_data2[item_sel2][1] == entry2 then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		end
	elseif in_rte == 1 then
		-- LSKRTE2
		if in_fmc == 0 then
			offset_tmp = offset_mod2
			
			if nav_mode == 99 and legs_num8 == legs_num and rte12_act == 1 then
				-- item = legs_num8 + 1
				item = legs_num8 + button
			else
				if true_legs_num(8) == 0 then
					item = legs_num8 + 1
				elseif true_legs_num(8) > 0 then
					item = (act_page - 1) * 5 + offset_tmp - 1 + button
					if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and legs_num8 == 1 then
						item = item + 2
					else
						if B738DR_no_rte2 == 0 and act_page > 1 then
							item = item - 1
						end
					end
				end
			end
			
			jj,kk = string.find(entry, "/")
			
			if new_hold_at2 ~= 0 then
				if item > 0 and item <= legs_num8 and rte12_act == in_rte then
					rte_add_hold_fix(item, 0, in_rte)
					
					if act_page == 1 and rte12_act == in_rte then
						legs_intdir = 1
						legs_intdir_rte = in_rte
						legs_intdir_idx_mod = item
						
						-- compute >legs_intdir_crs
						local nd_lat = math.rad(simDR_latitude)
						local nd_lon = math.rad(simDR_longitude)
						
						local nd_lat2 = 0
						local nd_lon2 = 0
						if in_rte == 0 then
							legs_data2[item][31] = "DF"
							legs_data2[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs2(item)
						else
							legs_data8[item][31] = "DF"
							legs_data8[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs8(item)
						end
						nd_lat2 = math.rad(nd_lat2)
						nd_lon2 = math.rad(nd_lon2)
						
						local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
						local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
						local nd_hdg = math.atan2(nd_y, nd_x)
						nd_hdg = math.deg(nd_hdg)
						nd_hdg = (nd_hdg + 360) % 360
						legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
					end
				end
			elseif item > legs_num8 then
				tmp_tmp = legs_num8 + 1
				if item == tmp_tmp then
					if jj ~= nil then
						rte_add_wpt3(item, entry, 0, in_rte)
						if act_page == 1 and nav_mode == 99 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
						item_sel = 0
						item_sel_act = 0
					elseif string.len(entry) > 1 and string.len(entry) < 6 and item_sel == 0 then
						if wpt_lat_lon(entry) == true then
							legs_data8[item][31] = "TF"
							legs_data8[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
						else
						-- add waypoint last
							rte_add_wpt(item, entry, 0, in_rte)
						end
						
						if act_page == 1 and nav_mode == 99 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							
						end
						
						item_sel = 0
						item_sel_act = 0
					else
						if wpt_lat_lon(entry) == true then
							legs_data8[item][31] = "TF"
							legs_data8[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
							
							if act_page == 1 and nav_mode == 99 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
								
							end
							
							item_sel = 0
							item_sel_act = 0
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				if entry == ">DELETE" then
					-- delete waypoint
					if act_page == 1 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						rte_del_wpt(item, in_fmc, in_rte)
						entry = ""
					end
					item_sel = 0
					item_sel_act = 0
				elseif jj ~= nil and item > 1 then
					rte_add_wpt3(item, entry, 0, in_rte)
					item_sel = 0
					item_sel_act = 0
				elseif string.len(entry) > 1 and string.len(entry) < 6 and item_sel == 0 and item > 1 then
					-- Active wpt
					item_sel = find_act_route_wpt2(entry, item)
					if item_sel < 0 then
						item_sel = 0
					end
					if item > 1 and item_sel > 0 then
						if legs_data8[item-1][1] == legs_data8[item_sel][1] and legs_data8[item-1][7] == legs_data8[item_sel][7] and
						legs_data8[item-1][8] == legs_data8[item_sel][8] and item-1 ~= item_sel then
							item = item - 1
						elseif legs_data8[item+1][1] == legs_data8[item_sel][1] and legs_data8[item+1][7] == legs_data8[item_sel][7] and
						legs_data8[item+1][8] == legs_data8[item_sel][8] and item+1 ~= item_sel then
							item = item + 1
						end
					end
					if act_page == 1 then
						if item_sel > 0 then
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel >= item then
										if item_sel ~= item then
											create_legs_abeam_list(item, item_sel - 1, in_rte)
											rte_copy(item_sel, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable8 = 1
										end
										if rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
									if invalid == 0  then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
									end
									msg_unavaible_crz_alt = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item and item > 1 then
									create_legs_abeam_list(item, item_sel - 1, in_rte)
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								elseif item_sel == item and item > 1 and legs_data8[item_sel][1] == entry then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry = ""
								item_sel = 0
								item_sel_act = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel = 0
							item_sel_act = 0
						else
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry, 0, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry, 0, in_rte)
							end
							
							if invalid == 0 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							end
						end
					else
						if item_sel > 0 then
							if item > 1 then
								if legs_data8[item-1][1] == legs_data8[item_sel][1] and legs_data8[item-1][7] == legs_data8[item_sel][7] and
								legs_data8[item-1][8] == legs_data8[item_sel][8] and item-1 ~= item_sel then
									item = item - 1
								elseif legs_data8[item+1][1] == legs_data8[item_sel][1] and legs_data8[item+1][7] == legs_data8[item_sel][7] and
								legs_data8[item+1][8] == legs_data8[item_sel][8] and item+1 ~= item_sel then
									item = item + 1
								end
							end
							-- Item selected
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel > item then
										legs_data8[item_sel][31] = "TF"
										legs_data8[item_sel][21] = -1
										rte_copy(item_sel, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable8 = 1
									elseif item_sel < item then
										item_sel = item_sel + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel, in_rte)
										calc_rte_enable8 = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item and item > 1 then
									legs_data8[item_sel][31] = "TF"
									legs_data8[item_sel][21] = -1
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
								elseif item_sel < item and item > 1 then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable8 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry = ""
								item_sel = 0
								item_sel_act = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel = 0
							item_sel_act = 0
						else
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry, 0, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry, 0, in_rte)
							end
						end
					end
					item_sel = 0
					item_sel_act = 0
				elseif wpt_lat_lon(entry) == true and item_sel == 0 and item > 1 then
						legs_data8[item][31] = "TF"
						legs_data8[item_sel][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
						if act_page == 1 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							legs_data8[item][31] = "DF"
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
				elseif item_sel == 0 then
					if string.len(entry) == 0 then
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 then
								-- select item
								if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel = 0
									item_sel_act = 0
								else
									item_sel = item
									entry = legs_data8[item][1]
									item_sel_act = item
									item_sel_len = string.len(entry)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							end
						else
							-- select item
							if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							else
								item_sel = item
								entry = legs_data8[item][1]
								item_sel_act = item
								item_sel_len = string.len(entry)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel = 0
						item_sel_act = 0
					end
				else
					if item_sel > 0 and item > 1 then
						if string.sub(legs_data8[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data8[item_sel][1] ~= entry then
								item = 0
								item_sel = 0
							elseif legs_data8[item-1][1] == legs_data8[item_sel][1] and legs_data8[item-1][7] == legs_data8[item_sel][7] and
							legs_data8[item-1][8] == legs_data8[item_sel][8] and item-1 ~= item_sel then
								item = item - 1
							elseif legs_data8[item+1][1] == legs_data8[item_sel][1] and legs_data8[item+1][7] == legs_data8[item_sel][7] and
							legs_data8[item+1][8] == legs_data8[item_sel][8] and item+1 ~= item_sel then
								item = item + 1
							end
							-- Item selected
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 then
									-- entry item
									if item_sel > item or (item_sel == item and act_page == 1) then
										if item_sel ~= item or act_page ~= 1 then
											if act_page ~= 1 then
												legs_data8[item_sel][31] = "TF"
												legs_data8[item_sel][21] = -1
											end
											if act_page == 1 then
												create_legs_abeam_list(item, item_sel - 1, in_rte)
											end
											rte_copy(item_sel, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable8 = 1
										end
										if act_page == 1 and rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
										entry = ""
										item_sel = 0
										item_sel_act = 0
									elseif item_sel < item then
										item_sel = item_sel + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel, in_rte)
										calc_rte_enable8 = 1
										entry = ""
										item_sel = 0
										item_sel_act = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item then
									if act_page ~= 1 then
										legs_data8[item_sel][31] = "TF"
										legs_data8[item_sel][21] = -1
									end
									if act_page == 1 then
										create_legs_abeam_list(item, item_sel - 1, in_rte)
									end
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
									if act_page == 1 and rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
								elseif item_sel < item then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable8 = 1
									entry = ""
									item_sel = 0
									item_sel_act = 0
								elseif item_sel == item and act_page == 1 and legs_data8[item_sel][1] == entry then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry = ""
									item_sel = 0
									item_sel_act = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		else
			offset_tmp = offset_mod2
			
			if nav_mode == 99 and legs_num8 == legs_num and rte12_act == 1 then
				-- item = legs_num8 + 1
				item = legs_num8 + button
			else
				if true_legs_num(8) == 0 then
					item = legs_num8 + 1
				elseif true_legs_num(8) > 0 then
					item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
					if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and legs_num8 == 1 then
						item = item + 1
					else
						if B738DR_no_rte2 == 0 and act_page2 > 1 then
							item = item - 1
						end
					end
				end
			end
			jj,kk = string.find(entry2, "/")
			
			if new_hold_at2 ~= 0 then
				if item > 0 and item <= legs_num8 and rte12_act == in_rte then
					rte_add_hold_fix(item, 1, in_rte)
					
					if act_page2 == 1 and rte12_act == in_rte then
						legs_intdir = 1
						legs_intdir_rte = in_rte
						legs_intdir_idx_mod = item
						
						-- compute >legs_intdir_crs
						local nd_lat = math.rad(simDR_latitude)
						local nd_lon = math.rad(simDR_longitude)
						
						local nd_lat2 = 0
						local nd_lon2 = 0
						if in_rte == 0 then
							legs_data2[item][31] = "DF"
							legs_data2[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs2(item)
						else
							legs_data8[item][31] = "DF"
							legs_data8[item][9] = ""
							nd_lat2, nd_lon2 = lat_lon_legs8(item)
						end
						nd_lat2 = math.rad(nd_lat2)
						nd_lon2 = math.rad(nd_lon2)
						
						local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
						local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
						local nd_hdg = math.atan2(nd_y, nd_x)
						nd_hdg = math.deg(nd_hdg)
						nd_hdg = (nd_hdg + 360) % 360
						legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
					end
				end
			elseif item > legs_num8 or (item == 1 and legs_num8 == 1) then
				tmp_tmp = legs_num8 + 1
				if item == tmp_tmp or (item == 1 and legs_num8 == 1) then
					if jj ~= nil then
						rte_add_wpt3(item, entry2, 1, in_rte)
						if act_page2 == 1 and nav_mode == 99 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
						item_sel2 = 0
						item_sel_act2 = 0
					elseif string.len(entry2) > 1 and string.len(entry2) < 6 and item_sel2 == 0 then
						if wpt_lat_lon(entry2) == true then
							legs_data8[item][31] = "TF"
							legs_data8[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
						else
						-- add waypoint last
							rte_add_wpt(item, entry2, 1, in_rte)
						end
						if act_page2 == 1 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
						item_sel2 = 0
						item_sel_act2 = 0
					else
						if wpt_lat_lon(entry2) == true then
							legs_data8[item][31] = "TF"
							legs_data8[item][21] = -1
							rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
							
							if act_page2 == 1 and nav_mode == 99 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
								
							end
							
							item_sel2 = 0
							item_sel_act2 = 0
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				if entry2 == ">DELETE" then
					-- delete waypoint
					if act_page2 == 1 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						rte_del_wpt(item, in_fmc, in_rte)
						entry2 = ""
					end
					item_sel2 = 0
					item_sel_act2 = 0
				elseif jj ~= nil then
					rte_add_wpt3(item, entry2, 1, in_rte)
					item_sel2 = 0
					item_sel_act2 = 0
				elseif string.len(entry2) > 1 and string.len(entry2) < 6 and item_sel2 == 0 then
					item_sel2 = find_act_route_wpt2(entry2, item)
					if item_sel2 < 0 then
						item_sel2 = 0
					end
					if item > 1 and item_sel2 > 0 then
						if legs_data8[item-1][1] == legs_data8[item_sel2][1] and legs_data8[item-1][7] == legs_data8[item_sel2][7] and
						legs_data8[item-1][8] == legs_data8[item_sel2][8] and item-1 ~= item_sel2 then
							item = item - 1
						elseif legs_data8[item+1][1] == legs_data8[item_sel2][1] and legs_data8[item+1][7] == legs_data8[item_sel2][7] and
						legs_data8[item+1][8] == legs_data8[item_sel2][8] and item+1 ~= item_sel2 then
							item = item + 1
						end
					end
					if act_page2 == 1 then
						if item_sel2 > 0 then
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel2 >= item then
										if item_sel2 ~= item then
											-- create legs abeam wpts
											-- from item to item_sel2 - 1
											create_legs_abeam_list(item, item_sel2 - 1, in_rte)
											rte_copy(item_sel2, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable8 = 1
										end
										if rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel2 > item and item > 1 then
									-- create legs abeam wpts
									-- from item to item_sel2 - 1
									create_legs_abeam_list(item, item_sel2 - 1, in_rte)
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								elseif item_sel2 == item and item > 1 and legs_data8[item_sel2][1] == entry2 then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry2 = ""
								item_sel2 = 0
								item_sel_act2 = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel2 = 0
							item_sel_act2 = 0
						else
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry2, 1, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry2, 1, in_rte)
							end
							
							if invalid == 0 and rte12_act == in_rte then
								legs_intdir = 1
								legs_intdir_rte = in_rte
								legs_intdir_idx_mod = item
								
								-- compute >legs_intdir_crs
								local nd_lat = math.rad(simDR_latitude)
								local nd_lon = math.rad(simDR_longitude)
								
								local nd_lat2 = 0
								local nd_lon2 = 0
								if in_rte == 0 then
									legs_data2[item][31] = "DF"
									legs_data2[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs2(item)
								else
									legs_data8[item][31] = "DF"
									legs_data8[item][9] = ""
									nd_lat2, nd_lon2 = lat_lon_legs8(item)
								end
								nd_lat2 = math.rad(nd_lat2)
								nd_lon2 = math.rad(nd_lon2)
								
								local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
								local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
								local nd_hdg = math.atan2(nd_y, nd_x)
								nd_hdg = math.deg(nd_hdg)
								nd_hdg = (nd_hdg + 360) % 360
								legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
							end
						end
					else
						if item_sel2 > 0 then
							-- Item selected
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 and item > 1 then
									-- entry item
									if item_sel2 > item then
										legs_data8[item_sel2][31] = "TF"
										legs_data8[item_sel2][21] = -1
										rte_copy(item_sel2, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable8 = 1
									elseif item_sel2 < item then
										item_sel2 = item_sel2 + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel2, in_rte)
										calc_rte_enable8 = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel2 > item and item > 1 then
									legs_data8[item_sel2][31] = "TF"
									legs_data8[item_sel2][21] = -1
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
								elseif item_sel2 < item and item > 1 then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable8 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry2 = ""
								item_sel2 = 0
								item_sel_act2 = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
							item_sel2 = 0
							item_sel_act2 = 0
						else
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 then
									-- add waypoint
									rte_add_wpt(item, entry2, 1, in_rte)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- add waypoint
								rte_add_wpt(item, entry2, 1, in_rte)
							end
						end
					end
					item_sel2 = 0
					item_sel_act2 = 0
				elseif wpt_lat_lon(entry2) == true and item_sel2 == 0 then
						legs_data8[item][31] = "TF"
						legs_data8[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
						if act_page2 == 1 and rte12_act == in_rte then
							legs_intdir = 1
							legs_intdir_rte = in_rte
							legs_intdir_idx_mod = item
							
							-- compute >legs_intdir_crs
							local nd_lat = math.rad(simDR_latitude)
							local nd_lon = math.rad(simDR_longitude)
							
							local nd_lat2 = 0
							local nd_lon2 = 0
							if in_rte == 0 then
								legs_data2[item][31] = "DF"
								legs_data2[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs2(item)
							else
								legs_data8[item][31] = "DF"
								legs_data8[item][9] = ""
								nd_lat2, nd_lon2 = lat_lon_legs8(item)
							end
							nd_lat2 = math.rad(nd_lat2)
							nd_lon2 = math.rad(nd_lon2)
							
							local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
							local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
							local nd_hdg = math.atan2(nd_y, nd_x)
							nd_hdg = math.deg(nd_hdg)
							nd_hdg = (nd_hdg + 360) % 360
							legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
						end
				elseif item_sel2 == 0 then
					if string.len(entry2) == 0 then
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 then
								-- select item
								if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel2 = 0
									item_sel_act2 = 0
								else
									item_sel2 = item
									entry2 = legs_data8[item][1]
									item_sel_act2 = item
									item_sel_len2 = string.len(entry2)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							end
						else
							-- select item
							if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							else
								item_sel2 = item
								entry2 = legs_data8[item][1]
								item_sel_act2 = item
								item_sel_len2 = string.len(entry2)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel2 = 0
						item_sel_act2 = 0
					end
				else
					if item_sel2 > 0 and item > 1 then
						if string.sub(legs_data8[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data8[item_sel2][1] ~= entry2 then
								item = 0
								item_sel2 = 0
							elseif legs_data8[item-1][1] == legs_data8[item_sel2][1] and legs_data8[item-1][7] == legs_data8[item_sel2][7] and
							legs_data8[item-1][8] == legs_data8[item_sel2][8] and item-1 ~= item_sel2 then
								item = item - 1
							elseif legs_data8[item+1][1] == legs_data8[item_sel2][1] and legs_data8[item+1][7] == legs_data8[item_sel2][7] and
							legs_data8[item+1][8] == legs_data8[item_sel2][8] and item+1 ~= item_sel2 then
								item = item + 1
							end
							if offset_act >= 5 then
								if legs_data8[item][19] ~= 6 then
									-- entry2 item
									if item_sel2 > item or (item_sel2 == item and act_page2 == 1) then
										if item_sel2 ~= item or act_page2 ~= 1 then
											if act_page2 ~= 1 then
												legs_data8[item_sel2][31] = "TF"
												legs_data8[item_sel2][21] = -1
											end
											if act_page2 == 1 then
												-- create legs abeam wpts
												-- from item to item_sel2 - 1
												create_legs_abeam_list(item, item_sel2 - 1, in_rte)
											end
											rte_copy(item_sel2, in_rte)
											rte_paste(item, in_rte)
											calc_rte_enable8 = 1
										end
										if act_page2 == 1 and rte12_act == in_rte then
											legs_intdir = 1
											legs_intdir_rte = in_rte
											legs_intdir_idx_mod = item
											
											-- compute >legs_intdir_crs
											local nd_lat = math.rad(simDR_latitude)
											local nd_lon = math.rad(simDR_longitude)
											
											local nd_lat2 = 0
											local nd_lon2 = 0
											if in_rte == 0 then
												legs_data2[item][31] = "DF"
												legs_data2[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs2(item)
											else
												legs_data8[item][31] = "DF"
												legs_data8[item][9] = ""
												nd_lat2, nd_lon2 = lat_lon_legs8(item)
											end
											nd_lat2 = math.rad(nd_lat2)
											nd_lon2 = math.rad(nd_lon2)
											
											local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
											local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
											local nd_hdg = math.atan2(nd_y, nd_x)
											nd_hdg = math.deg(nd_hdg)
											nd_hdg = (nd_hdg + 360) % 360
											legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
										end
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									elseif item_sel2 < item then
										item_sel2 = item_sel2 + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel2, in_rte)
										calc_rte_enable8 = 1
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry2 item
								if item_sel2 > item then
									if act_page2 ~= 1 then
										legs_data8[item_sel2][31] = "TF"
										legs_data8[item_sel2][21] = -1
									end
									if act_page2 == 1 then
										-- create legs abeam wpts
										-- from item to item_sel2 - 1
										create_legs_abeam_list(item, item_sel2 - 1, in_rte)
									end
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
									if act_page2 == 1 and rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								elseif item_sel2 < item then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable8 = 1
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								elseif item_sel2 == item and act_page2 == 1 and legs_data8[item_sel2][1] == entry2 then
									if rte12_act == in_rte then
										legs_intdir = 1
										legs_intdir_rte = in_rte
										legs_intdir_idx_mod = item
										
										-- compute >legs_intdir_crs
										local nd_lat = math.rad(simDR_latitude)
										local nd_lon = math.rad(simDR_longitude)
										
										local nd_lat2 = 0
										local nd_lon2 = 0
										if in_rte == 0 then
											legs_data2[item][31] = "DF"
											legs_data2[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs2(item)
										else
											legs_data8[item][31] = "DF"
											legs_data8[item][9] = ""
											nd_lat2, nd_lon2 = lat_lon_legs8(item)
										end
										nd_lat2 = math.rad(nd_lat2)
										nd_lon2 = math.rad(nd_lon2)
										
										local nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
										local nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
										local nd_hdg = math.atan2(nd_y, nd_x)
										nd_hdg = math.deg(nd_hdg)
										nd_hdg = (nd_hdg + 360) % 360
										legs_intdir_crs_mod = (nd_hdg + simDR_mag_variation + 360) % 360
									end
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		end
	
	end
end

function legs_x_lsk(in_key, in_fmc, in_rte)
			
	local item = 0
	local button = in_key
	local tmp_tmp = 0
	local jj = 0
	local kk = 0
	local xy = 0
	local invalid = 0
	
	local offset_tmp = 0
	
	if in_rte == 0 then
		if in_fmc == 0 then
			
			offset_tmp = offset_mod
			
			if nav_mode == 99 and rte12_act == 0 then
				if legs_num2 == legs_num then
					-- item = legs_num2 + 2
					item = legs_num2 + button
				else
					item = (act_page - 1) * 5 + offset_tmp + button
					if B738DR_no_rte2 == 0 and act_page > 1 then
						item = item - 1
					end
				end
			else
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if ref_icao_x ~= "----" and des_icao_x ~= "****" and legs_num2 == 1 then
					item = item + 2
				else
					if B738DR_no_rte2 == 0 and act_page > 1 then
						item = item - 1
					end
				end
			end
			
			jj,kk = string.find(entry, "/")
			
			if new_hold_at ~= 0 then
				if item > 0 and item <= legs_num2 then
					rte_add_hold_fix(item, 0, in_rte)
				end
			elseif item > legs_num2 then
				tmp_tmp = legs_num2 + 1
				if item == tmp_tmp then
					if wpt_lat_lon(entry) == true then
						legs_data2[item][31] = "TF"
						legs_data2[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
					elseif jj ~= nil then
						rte_add_wpt3(item, entry, 0, in_rte)
					elseif item_sel == 0 then
						-- add waypoint last
						rte_add_wpt(item, entry, 0, in_rte)
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					item_sel = 0
					item_sel_act = 0
				end
			else
				if entry == ">DELETE" then
					-- delete waypoint
					rte_del_wpt(item, in_fmc, in_rte)
					entry = ""
				elseif jj ~= nil then
					rte_add_wpt3(item, entry, 0, in_rte)
					item_sel = 0
					item_sel_act = 0
				elseif string.len(entry) > 1 and string.len(entry) < 6 and item_sel == 0 then
					item_sel = find_act_route_wpt2(entry, item)
					if item_sel < 0 then
						item_sel = 0
					end
					if item > 1 and item_sel > 0 then
						if legs_data2[item-1][1] == legs_data2[item_sel][1] and legs_data2[item-1][7] == legs_data2[item_sel][7] and
						legs_data2[item-1][8] == legs_data2[item_sel][8] and item-1 ~= item_sel then
							item = item - 1
						elseif legs_data2[item+1][1] == legs_data2[item_sel][1] and legs_data2[item+1][7] == legs_data2[item_sel][7] and
						legs_data2[item+1][8] == legs_data2[item_sel][8] and item+1 ~= item_sel then
							item = item + 1
						end
					end
					if item_sel > 0 then
						-- Item selected
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 and item > 1 then
								-- entry item
								if item_sel > item then
									legs_data2[item_sel][31] = "TF"
									legs_data2[item_sel][21] = -1
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
								elseif item_sel < item then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable2 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry = ""
								item_sel = 0
								item_sel_act = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- entry item
							if item_sel > item and item > 1 then
								legs_data2[item_sel][31] = "TF"
								legs_data2[item_sel][21] = -1
								rte_copy(item_sel, in_rte)
								rte_paste(item, in_rte)
								calc_rte_enable2 = 1
							elseif item_sel < item and item > 1 then
								item_sel = item_sel + 1
								item = item + 1
								rte_copy(item, in_rte)
								rte_paste(item_sel, in_rte)
								calc_rte_enable2 = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
								invalid = 1
							end
							entry = ""
							item_sel = 0
							item_sel_act = 0
							if invalid == 0 then
								if rte12_act == in_rte then
									legs_delete = 1
								end
								fms_recalc = 1
								msg_unavaible_crz_alt = 0
							end
						end
						item_sel = 0
						item_sel_act = 0
					else
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 then
								-- add waypoint
								rte_add_wpt(item, entry, 0, in_rte)
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- add waypoint
							rte_add_wpt(item, entry, 0, in_rte)
						end
						item_sel = 0
						item_sel_act = 0
					end
				elseif wpt_lat_lon(entry) == true and item_sel == 0 then
						legs_data2[item][31] = "TF"
						legs_data2[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
				elseif item_sel == 0 then
					if string.len(entry) == 0 then
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 then
								-- select item
								if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel = 0
									item_sel_act = 0
								else
									item_sel = item
									entry = legs_data2[item][1]
									item_sel_act = item
									item_sel_len = string.len(entry)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							end
						else
							-- select item
							if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							else
								item_sel = item
								entry = legs_data2[item][1]
								item_sel_act = item
								item_sel_len = string.len(entry)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel = 0
						item_sel_act = 0
					end
				else
					if item_sel > 0 and item > 1 then
						if string.sub(legs_data2[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data2[item_sel][1] ~= entry then
								item = 0
								item_sel = 0
							elseif legs_data2[item-1][1] == legs_data2[item_sel][1] and legs_data2[item-1][7] == legs_data2[item_sel][7] and
							legs_data2[item-1][8] == legs_data2[item_sel][8] and item-1 ~= item_sel then
								item = item - 1
							elseif legs_data2[item+1][1] == legs_data2[item_sel][1] and legs_data2[item+1][7] == legs_data2[item_sel][7] and
							legs_data2[item+1][8] == legs_data2[item_sel][8] and item+1 ~= item_sel then
								item = item + 1
							end
							if offset_act >= 5 and item > 0 then
								if legs_data2[item][19] ~= 6 then
									-- entry item
									if item_sel > item then
										legs_data2[item_sel][31] = "TF"
										legs_data2[item_sel][21] = -1
										rte_copy(item_sel, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable2 = 1
										entry = ""
										item_sel = 0
										item_sel_act = 0
									elseif item_sel < item then
										item_sel = item_sel + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel, in_rte)
										calc_rte_enable2 = 1
										entry = ""
										item_sel = 0
										item_sel_act = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item then
									legs_data2[item_sel][31] = "TF"
									legs_data2[item_sel][21] = -1
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
									entry = ""
									item_sel = 0
									item_sel_act = 0
								elseif item_sel < item then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable2 = 1
									entry = ""
									item_sel = 0
									item_sel_act = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		else
			offset_tmp = offset_mod
			
			if nav_mode == 99 and rte12_act == 0 then
				if legs_num2 == legs_num then
					-- item = legs_num2 + 2
					item = legs_num2 + button
				else
					item = (act_page2 - 1) * 5 + offset_tmp + button
					if B738DR_no_rte2 == 0 and act_page2 > 1 then
						item = item - 1
					end
				end
			else
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if ref_icao_x ~= "----" and des_icao_x ~= "****" and legs_num2 == 1 then
					item = item + 1
				else
					if B738DR_no_rte2 == 0 and act_page2 > 1 then
						item = item - 1
					end
				end
			end
			jj,kk = string.find(entry2, "/")
			
			if new_hold_at ~= 0 then
				if item > 0 and item <= legs_num2 then
					rte_add_hold_fix(item, 1, in_rte)
				end
			elseif item > legs_num2 then
				tmp_tmp = legs_num2 + 1
				if item == tmp_tmp then
					if wpt_lat_lon(entry2) == true then
						legs_data2[item][31] = "TF"
						legs_data2[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
					elseif jj ~= nil then
						rte_add_wpt3(item, entry2, 1, in_rte)
					elseif item_sel2 == 0 then
						-- add waypoint last
						rte_add_wpt(item, entry2, 1, in_rte)
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					item_sel2 = 0
					item_sel_act2 = 0
				end
			else
				if entry2 == ">DELETE" then
					-- delete waypoint
					rte_del_wpt(item, in_fmc, in_rte)
					entry2 = ""
				elseif jj ~= nil then
					rte_add_wpt3(item, entry2, 1, in_rte)
					item_sel2 = 0
					item_sel_act2 = 0
				elseif string.len(entry2) > 1 and string.len(entry2) < 6 and item_sel2 == 0 then
					item_sel2 = find_act_route_wpt2(entry2, item)
					if item_sel2 < 0 then
						item_sel2 = 0
					end
					if item > 1 and item_sel2 > 0 then
						if legs_data2[item-1][1] == legs_data2[item_sel2][1] and legs_data2[item-1][7] == legs_data2[item_sel2][7] and
						legs_data2[item-1][8] == legs_data2[item_sel2][8] and item-1 ~= item_sel2 then
							item = item - 1
						elseif legs_data2[item+1][1] == legs_data2[item_sel2][1] and legs_data2[item+1][7] == legs_data2[item_sel2][7] and
						legs_data2[item+1][8] == legs_data2[item_sel2][8] and item+1 ~= item_sel2 then
							item = item + 1
						end
					end
					if item_sel2 > 0 then
						-- Item selected
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 and item > 1 then
								-- entry item
								if item_sel2 > item then
									legs_data2[item_sel2][31] = "TF"
									legs_data2[item_sel2][21] = -1
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
								elseif item_sel2 < item then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable2 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry2 = ""
								item_sel2 = 0
								item_sel_act2 = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- entry item
							if item_sel2 > item and item > 1 then
								legs_data2[item_sel2][31] = "TF"
								legs_data2[item_sel2][21] = -1
								rte_copy(item_sel2, in_rte)
								rte_paste(item, in_rte)
								calc_rte_enable2 = 1
							elseif item_sel2 < item and item > 1 then
								item_sel2 = item_sel2 + 1
								item = item + 1
								rte_copy(item, in_rte)
								rte_paste(item_sel2, in_rte)
								calc_rte_enable2 = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
								invalid = 1
							end
							entry2 = ""
							item_sel2 = 0
							item_sel_act2 = 0
							if invalid == 0 then
								if rte12_act == in_rte then
									legs_delete = 1
								end
								fms_recalc = 1
								msg_unavaible_crz_alt = 0
							end
						end
						item_sel2 = 0
						item_sel_act2 = 0
					else
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 then
								-- add waypoint
								rte_add_wpt(item, entry2, 1, in_rte)
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- add waypoint
							rte_add_wpt(item, entry2, 1, in_rte)
						end
						item_sel2 = 0
						item_sel_act2 = 0
					end
				elseif wpt_lat_lon(entry2) == true and item_sel2 == 0 then
						legs_data2[item][31] = "TF"
						legs_data2[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
				elseif item_sel2 == 0 then
					if string.len(entry2) == 0 then
						if offset_act >= 5 then
							if legs_data2[item][19] ~= 6 then
								-- select item
								if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel2 = 0
									item_sel_act2 = 0
								else
									item_sel2 = item
									entry2 = legs_data2[item][1]
									item_sel_act2 = item
									item_sel_len2 = string.len(entry2)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							end
						else
							-- select item
							if legs_data2[item][1] == "DISCONTINUITY" or string.sub(legs_data2[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							else
								item_sel2 = item
								entry2 = legs_data2[item][1]
								item_sel_act2 = item
								item_sel_len2 = string.len(entry2)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel2 = 0
						item_sel_act2 = 0
					end
				else
					if item_sel2 > 0 and item > 1 then
						if string.sub(legs_data2[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data2[item_sel2][1] ~= entry2 then
								item = 0
								item_sel2 = 0
							elseif legs_data2[item-1][1] == legs_data2[item_sel2][1] and legs_data2[item-1][7] == legs_data2[item_sel2][7] and
							legs_data2[item-1][8] == legs_data2[item_sel2][8] and item-1 ~= item_sel2 then
								item = item - 1
							elseif legs_data2[item+1][1] == legs_data2[item_sel2][1] and legs_data2[item+1][7] == legs_data2[item_sel2][7] and
							legs_data2[item+1][8] == legs_data2[item_sel2][8] and item+1 ~= item_sel2 then
								item = item + 1
							end
							if offset_act >= 5 and item > 0 then
								if legs_data2[item][19] ~= 6 then
									-- entry2 item
									if item_sel2 > item then
										legs_data2[item_sel2][31] = "TF"
										legs_data2[item_sel2][21] = -1
										rte_copy(item_sel2, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable2 = 1
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									elseif item_sel2 < item then
										item_sel2 = item_sel2 + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel2, in_rte)
										calc_rte_enable2 = 1
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry2 item
								if item_sel2 > item then
									legs_data2[item_sel2][31] = "TF"
									legs_data2[item_sel2][21] = -1
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable2 = 1
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								elseif item_sel2 < item then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable2 = 1
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		end
	elseif in_rte == 1 then
		-- XLSKRTE2
		if in_fmc == 0 then
			
			offset_tmp = offset_mod2
			
			if nav_mode == 99 and rte12_act == 1 then
				if legs_num8 == legs_num then
					-- item = legs_num8 + 2
					item = legs_num8 + button
				else
					item = (act_page - 1) * 5 + offset_tmp + button
					if B738DR_no_rte2 == 0 and act_page > 1 then
						item = item - 1
					end
				end
			else
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and legs_num8 == 1 then
					item = item + 2
				else
					if B738DR_no_rte2 == 0 and act_page > 1 then
						item = item - 1
					end
				end
			end
			
			jj,kk = string.find(entry, "/")
			
			if new_hold_at2 ~= 0 then
				if item > 0 and item <= legs_num8 then
					rte_add_hold_fix(item, 0, in_rte)
				end
			elseif item > legs_num8 then
				tmp_tmp = legs_num8 + 1
				if item == tmp_tmp then
					if wpt_lat_lon(entry) == true then
						legs_data8[item][31] = "TF"
						legs_data8[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
					elseif jj ~= nil then
						rte_add_wpt3(item, entry, 0, in_rte)
					elseif item_sel == 0 then
						-- add waypoint last
						rte_add_wpt(item, entry, 0, in_rte)
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					item_sel = 0
					item_sel_act = 0
				end
			else
				if entry == ">DELETE" then
					-- delete waypoint
					rte_del_wpt(item, in_fmc, in_rte)
					entry = ""
				elseif jj ~= nil then
					rte_add_wpt3(item, entry, 0, in_rte)
					item_sel = 0
					item_sel_act = 0
				elseif string.len(entry) > 1 and string.len(entry) < 6 and item_sel == 0 then
					item_sel = find_act_route_wpt2(entry, item)
					if item_sel < 0 then
						item_sel = 0
					end
					if item > 1 and item_sel > 0 then
						if legs_data8[item-1][1] == legs_data8[item_sel][1] and legs_data8[item-1][7] == legs_data8[item_sel][7] and
						legs_data8[item-1][8] == legs_data8[item_sel][8] and item-1 ~= item_sel then
							item = item - 1
						elseif legs_data8[item+1][1] == legs_data8[item_sel][1] and legs_data8[item+1][7] == legs_data8[item_sel][7] and
						legs_data8[item+1][8] == legs_data8[item_sel][8] and item+1 ~= item_sel then
							item = item + 1
						end
					end
					if item_sel > 0 then
						-- Item selected
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 and item > 1 then
								-- entry item
								if item_sel > item then
									legs_data8[item_sel][31] = "TF"
									legs_data8[item_sel][21] = -1
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
								elseif item_sel < item then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable8 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry = ""
								item_sel = 0
								item_sel_act = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- entry item
							if item_sel > item and item > 1 then
								legs_data8[item_sel][31] = "TF"
								legs_data8[item_sel][21] = -1
								rte_copy(item_sel, in_rte)
								rte_paste(item, in_rte)
								calc_rte_enable8 = 1
							elseif item_sel < item and item > 1 then
								item_sel = item_sel + 1
								item = item + 1
								rte_copy(item, in_rte)
								rte_paste(item_sel, in_rte)
								calc_rte_enable8 = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
								invalid = 1
							end
							entry = ""
							item_sel = 0
							item_sel_act = 0
							if invalid == 0 then
								if rte12_act == in_rte then
									legs_delete = 1
								end
								fms_recalc = 1
								msg_unavaible_crz_alt = 0
							end
						end
						item_sel = 0
						item_sel_act = 0
					else
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 then
								-- add waypoint
								rte_add_wpt(item, entry, 0, in_rte)
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- add waypoint
							rte_add_wpt(item, entry, 0, in_rte)
						end
						item_sel = 0
						item_sel_act = 0
					end
				elseif wpt_lat_lon(entry) == true and item_sel == 0 then
						legs_data8[item][31] = "TF"
						legs_data8[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry, 0, in_rte)
				elseif item_sel == 0 then
					if string.len(entry) == 0 then
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 then
								-- select item
								if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel = 0
									item_sel_act = 0
								else
									item_sel = item
									entry = legs_data8[item][1]
									item_sel_act = item
									item_sel_len = string.len(entry)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							end
						else
							-- select item
							if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel = 0
								item_sel_act = 0
							else
								item_sel = item
								entry = legs_data8[item][1]
								item_sel_act = item
								item_sel_len = string.len(entry)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel = 0
						item_sel_act = 0
					end
				else
					if item_sel > 0 and item > 1 then
						if string.sub(legs_data8[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data8[item_sel][1] ~= entry then
								item = 0
								item_sel = 0
							elseif legs_data8[item-1][1] == legs_data8[item_sel][1] and legs_data8[item-1][7] == legs_data8[item_sel][7] and
							legs_data8[item-1][8] == legs_data8[item_sel][8] and item-1 ~= item_sel then
								item = item - 1
							elseif legs_data8[item+1][1] == legs_data8[item_sel][1] and legs_data8[item+1][7] == legs_data8[item_sel][7] and
							legs_data8[item+1][8] == legs_data8[item_sel][8] and item+1 ~= item_sel then
								item = item + 1
							end
							if offset_act >= 5 and item > 0 then
								if legs_data8[item][19] ~= 6 then
									-- entry item
									if item_sel > item then
										legs_data8[item_sel][31] = "TF"
										legs_data8[item_sel][21] = -1
										rte_copy(item_sel, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable8 = 1
										entry = ""
										item_sel = 0
										item_sel_act = 0
									elseif item_sel < item then
										item_sel = item_sel + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel, in_rte)
										calc_rte_enable8 = 1
										entry = ""
										item_sel = 0
										item_sel_act = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry item
								if item_sel > item then
									legs_data8[item_sel][31] = "TF"
									legs_data8[item_sel][21] = -1
									rte_copy(item_sel, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
									entry = ""
									item_sel = 0
									item_sel_act = 0
								elseif item_sel < item then
									item_sel = item_sel + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel, in_rte)
									calc_rte_enable8 = 1
									entry = ""
									item_sel = 0
									item_sel_act = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		else
			offset_tmp = offset_mod2
			
			if nav_mode == 99 and rte12_act == 1 then
				if legs_num8 == legs_num then
					-- item = legs_num8 + 2
					item = legs_num8 + button
				else
					item = (act_page2 - 1) * 5 + offset_tmp + button
					if B738DR_no_rte2 == 0 and act_page2 > 1 then
						item = item - 1
					end
				end
			else
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if ref_icao_x ~= "----" and des_icao_x ~= "****" and legs_num8 == 1 then
					item = item + 1
				else
					if B738DR_no_rte2 == 0 and act_page2 > 1 then
						item = item - 1
					end
				end
			end
			jj,kk = string.find(entry2, "/")
			
			if new_hold_at2 ~= 0 then
				if item > 0 and item <= legs_num8 then
					rte_add_hold_fix(item, 1, in_rte)
				end
			elseif item > legs_num8 then
				tmp_tmp = legs_num8 + 1
				if item == tmp_tmp then
					if wpt_lat_lon(entry2) == true then
						legs_data8[item][31] = "TF"
						legs_data8[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
					elseif jj ~= nil then
						rte_add_wpt3(item, entry2, 1, in_rte)
					elseif item_sel2 == 0 then
						-- add waypoint last
						rte_add_wpt(item, entry2, 1, in_rte)
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					item_sel2 = 0
					item_sel_act2 = 0
				end
			else
				if entry2 == ">DELETE" then
					-- delete waypoint
					rte_del_wpt(item, in_fmc, in_rte)
					entry2 = ""
				elseif jj ~= nil then
					rte_add_wpt3(item, entry2, 1, in_rte)
					item_sel2 = 0
					item_sel_act2 = 0
				elseif string.len(entry2) > 1 and string.len(entry2) < 6 and item_sel2 == 0 then
					item_sel2 = find_act_route_wpt2(entry2, item)
					if item_sel2 < 0 then
						item_sel2 = 0
					end
					if item > 1 and item_sel2 > 0 then
						if legs_data8[item-1][1] == legs_data8[item_sel2][1] and legs_data8[item-1][7] == legs_data8[item_sel2][7] and
						legs_data8[item-1][8] == legs_data8[item_sel2][8] and item-1 ~= item_sel2 then
							item = item - 1
						elseif legs_data8[item+1][1] == legs_data8[item_sel2][1] and legs_data8[item+1][7] == legs_data8[item_sel2][7] and
						legs_data8[item+1][8] == legs_data8[item_sel2][8] and item+1 ~= item_sel2 then
							item = item + 1
						end
					end
					if item_sel2 > 0 then
						-- Item selected
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 and item > 1 then
								-- entry item
								if item_sel2 > item then
									legs_data8[item_sel2][31] = "TF"
									legs_data8[item_sel2][21] = -1
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
								elseif item_sel2 < item then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable8 = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								entry2 = ""
								item_sel2 = 0
								item_sel_act2 = 0
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- entry item
							if item_sel2 > item and item > 1 then
								legs_data8[item_sel2][31] = "TF"
								legs_data8[item_sel2][21] = -1
								rte_copy(item_sel2, in_rte)
								rte_paste(item, in_rte)
								calc_rte_enable8 = 1
							elseif item_sel2 < item and item > 1 then
								item_sel2 = item_sel2 + 1
								item = item + 1
								rte_copy(item, in_rte)
								rte_paste(item_sel2, in_rte)
								calc_rte_enable8 = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
								invalid = 1
							end
							entry2 = ""
							item_sel2 = 0
							item_sel_act2 = 0
							if invalid == 0 then
								if rte12_act == in_rte then
									legs_delete = 1
								end
								fms_recalc = 1
								msg_unavaible_crz_alt = 0
							end
						end
						item_sel2 = 0
						item_sel_act2 = 0
					else
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 then
								-- add waypoint
								rte_add_wpt(item, entry2, 1, in_rte)
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							-- add waypoint
							rte_add_wpt(item, entry2, 1, in_rte)
						end
						item_sel2 = 0
						item_sel_act2 = 0
					end
				elseif wpt_lat_lon(entry2) == true and item_sel2 == 0 then
						legs_data8[item][31] = "TF"
						legs_data8[item][21] = -1
						rte_add_wpt_cust(item, "WPT01", transf_lat, transf_lon, entry2, 1, in_rte)
				elseif item_sel2 == 0 then
					if string.len(entry2) == 0 then
						if offset_act >= 5 then
							if legs_data8[item][19] ~= 6 then
								-- select item
								if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
									add_fmc_msg(INVALID_INPUT, 1)
									item_sel2 = 0
									item_sel_act2 = 0
								else
									item_sel2 = item
									entry2 = legs_data8[item][1]
									item_sel_act2 = item
									item_sel_len2 = string.len(entry2)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							end
						else
							-- select item
							if legs_data8[item][1] == "DISCONTINUITY" or string.sub(legs_data8[item][1], 1, 1) == "(" then
								add_fmc_msg(INVALID_INPUT, 1)
								item_sel2 = 0
								item_sel_act2 = 0
							else
								item_sel2 = item
								entry2 = legs_data8[item][1]
								item_sel_act2 = item
								item_sel_len2 = string.len(entry2)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
						item_sel2 = 0
						item_sel_act2 = 0
					end
				else
					if item_sel2 > 0 and item > 1 then
						if string.sub(legs_data8[item-1][1], 1, 1) == "{" then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if legs_data8[item_sel2][1] ~= entry2 then
								item = 0
								item_sel2 = 0
							elseif legs_data8[item-1][1] == legs_data8[item_sel2][1] and legs_data8[item-1][7] == legs_data8[item_sel2][7] and
							legs_data8[item-1][8] == legs_data8[item_sel2][8] and item-1 ~= item_sel2 then
								item = item - 1
							elseif legs_data8[item+1][1] == legs_data8[item_sel2][1] and legs_data8[item+1][7] == legs_data8[item_sel2][7] and
							legs_data8[item+1][8] == legs_data8[item_sel2][8] and item+1 ~= item_sel2 then
								item = item + 1
							end
							if offset_act >= 5 and item > 0 then
								if legs_data8[item][19] ~= 6 then
									-- entry2 item
									if item_sel2 > item then
										legs_data8[item_sel2][31] = "TF"
										legs_data8[item_sel2][21] = -1
										rte_copy(item_sel2, in_rte)
										rte_paste(item, in_rte)
										calc_rte_enable8 = 1
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									elseif item_sel2 < item then
										item_sel2 = item_sel2 + 1
										item = item + 1
										rte_copy(item, in_rte)
										rte_paste(item_sel2, in_rte)
										calc_rte_enable8 = 1
										entry2 = ""
										item_sel2 = 0
										item_sel_act2 = 0
									else
										add_fmc_msg(INVALID_INPUT, 1)
										invalid = 1
									end
									if invalid == 0 then
										if rte12_act == in_rte then
											legs_delete = 1
										end
										fms_recalc = 1
										msg_unavaible_crz_alt = 0
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								-- entry2 item
								if item_sel2 > item then
									legs_data8[item_sel2][31] = "TF"
									legs_data8[item_sel2][21] = -1
									rte_copy(item_sel2, in_rte)
									rte_paste(item, in_rte)
									calc_rte_enable8 = 1
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								elseif item_sel2 < item then
									item_sel2 = item_sel2 + 1
									item = item + 1
									rte_copy(item, in_rte)
									rte_paste(item_sel2, in_rte)
									calc_rte_enable8 = 1
									entry2 = ""
									item_sel2 = 0
									item_sel_act2 = 0
								else
									add_fmc_msg(INVALID_INPUT, 1)
									invalid = 1
								end
								if invalid == 0 then
									if rte12_act == in_rte then
										legs_delete = 1
									end
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
								end
							end
						end
					end
				end
			end
		end
	end
end

function rte_1_lsk(in_key, in_fmc, in_rte)

	local item = 0
	local button = in_key
	local tmp_tmp = 0
	local offset_fpln = 0
	local ll = 0
	
	if in_rte == 0 then
		if in_fmc == 0 then
			
			if rte12_act == 0 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod, 2)
			else
				offset_fpln = offset_mod	--2
			end
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if item > 1 then
				if entry == ">DELETE" then
					if item <= fpln_num2 then
						del_via(item, 0, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data2[fpln_num2][1] == "" and fpln_data2[fpln_num2][2] ~= "" then
							-- add new via via
							via_via_add(entry, 0, in_rte)
						elseif fpln_data2[fpln_num2][1] ~= "" then
							-- add new via
							via_add(fpln_data2[fpln_num2][1], fpln_data2[fpln_num2][3], entry, 0, in_rte)
						end
						item_sel_via = 0
					elseif item <= fpln_num2 then
						-- change via
						via_chg(fpln_data2[item-1][1], fpln_data2[item-1][3], item, entry, 0, in_rte)
						item_sel_via = 0
					end
				end
			else
				entry = ">INVALID ENTRY"
			end
		else
			if rte12_act == 0 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod, 2)
			else
				offset_fpln = offset_mod	--2
			end
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if item > 1 then
				if entry2 == ">DELETE" then
					if item <= fpln_num2 then
						del_via(item, 1, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data2[fpln_num2][1] == "" and fpln_data2[fpln_num2][2] ~= "" then
							-- add new via via
							via_via_add(entry2, 1, in_rte)
						elseif fpln_data2[fpln_num2][1] ~= "" then
							-- add new via
							via_add(fpln_data2[fpln_num2][1], fpln_data2[fpln_num2][3], entry2, 1, in_rte)
						end
						item_sel_via2 = 0
					elseif item <= fpln_num2 then
						-- change via
						via_chg(fpln_data2[item-1][1], fpln_data2[item-1][3], item, entry2, 1, in_rte)
						item_sel_via2 = 0
					end
				end
			else
				entry2 = ">INVALID ENTRY"
			end
		end
	elseif in_rte == 1 then
		-- RTE2
		if in_fmc == 0 then
			if rte12_act == 1 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod2, 2)
			else
				offset_fpln = offset_mod2	--2
			end
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if item > 1 then
				if entry == ">DELETE" then
					if item <= fpln_num4 then
						del_via(item, 0, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data4[fpln_num4][1] == "" and fpln_data4[fpln_num4][2] ~= "" then
							-- add new via via
							via_via_add(entry, 0, in_rte)
						elseif fpln_data4[fpln_num4][1] ~= "" then
							-- add new via
							via_add(fpln_data4[fpln_num4][1], fpln_data4[fpln_num4][3], entry, 0, in_rte)
						end
						item_sel_via = 0
					elseif item <= fpln_num4 then
						-- change via
						via_chg(fpln_data4[item-1][1], fpln_data4[item-1][3], item, entry, 0, in_rte)
						item_sel_via = 0
					end
				end
			else
				entry = ">INVALID ENTRY"
			end
		else
			if rte12_act == 1 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod2, 2)
			else
				offset_fpln = offset_mod2	--2
			end
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if item > 1 then
				if entry2 == ">DELETE" then
					if item <= fpln_num4 then
						del_via(item, 1, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data4[fpln_num4][1] == "" and fpln_data4[fpln_num4][2] ~= "" then
							-- add new via via
							via_via_add(entry2, 1, in_rte)
						elseif fpln_data4[fpln_num4][1] ~= "" then
							-- add new via
							via_add(fpln_data4[fpln_num4][1], fpln_data4[fpln_num4][3], entry2, 1, in_rte)
						end
						item_sel_via2 = 0
					elseif item <= fpln_num4 then
						-- change via
						via_chg(fpln_data4[item-1][1], fpln_data4[item-1][3], item, entry2, 1, in_rte)
						item_sel_via2 = 0
					end
				end
			else
				entry2 = ">INVALID ENTRY"
			end
		end
	end
end


function rte_1_rsk(in_key, in_fmc, in_rte)
	local item = 0
	local button = in_key
	local tmp_tmp = 0
	local offset_fpln = 0
	local ll = 0
	
	if in_rte == 0 then
		if in_fmc == 0 then
			if rte12_act == 0 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod, 2)
			else
				offset_fpln = offset_mod	--2
			end
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if fpln_num2 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					item_sel_via = 0
				elseif string.len(entry) == 0 then
					if item > 0 and item <= fpln_num2 then
						item_sel_via = item
						entry = fpln_data2[item][1]
					end
				elseif entry == ">DELETE" then
					if item > 0 and item <= fpln_num2 then
						if fpln_data2[item][1] ~= "" and offset < (fpln_data2[item][4] + fpln_data2[item][5] - 1) then
							rte_copy(fpln_data2[item][4] + fpln_data2[item][5], in_rte)
							if item < fpln_num2 and legs_data2[fpln_data2[item][4]-1][1] ~= "DISCONTINUITY" and legs_data2[fpln_data2[item][4]+fpln_data2[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data2[item][4], in_rte)
								rte_paste(fpln_data2[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data2[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry = ""
						end
					end
				elseif item_sel_via > item and item > 0 and item <= fpln_num2 and item > 0 then
					if fpln_data2[item][2] == "" then
						rte_copy(fpln_data2[item_sel_via][4], in_rte)
						rte_paste(fpln_data2[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry = ""
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry, "", fpln_data2[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				elseif item <= fpln_num2 and item > 0 then
					if item == 1 then
						dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry, "", fpln_data2[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				end
			else
				-- first waypoint
				if item == tmp_tmp then
					-- add new direct to navaid
					dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
				end
				item_sel_via = 0
			end
		else
			if rte12_act == 0 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod, 2)
			else
				offset_fpln = offset_mod	--2
			end
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if fpln_num2 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					item_sel_via2 = 0
				elseif string.len(entry2) == 0 then
					if item > 0 and item <= fpln_num2 then
						item_sel_via2 = item
						entry2 = fpln_data2[item][1]
					end
				elseif entry2 == ">DELETE" then
					if item > 0 and item <= fpln_num2 then
						if fpln_data2[item][1] ~= "" and offset < (fpln_data2[item][4] + fpln_data2[item][5] - 1) then
							rte_copy(fpln_data2[item][4] + fpln_data2[item][5], in_rte)
							if item < fpln_num2 and legs_data2[fpln_data2[item][4]-1][1] ~= "DISCONTINUITY" and legs_data2[fpln_data2[item][4]+fpln_data2[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data2[item][4], in_rte)
								rte_paste(fpln_data2[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data2[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry2 = ""
						end
					end
				elseif item_sel_via2 > item and item > 0 and item <= fpln_num2 and item > 0 then
					if fpln_data2[item][2] == "" then
						rte_copy(fpln_data2[item_sel_via2][4], in_rte)
						rte_paste(fpln_data2[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry2 = ""
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry2, "", fpln_data2[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				elseif item <= fpln_num2 and item > 0 then
					if item == 1 then
						dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry2, "", fpln_data2[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				end
			else
				-- first waypoint
				if item == tmp_tmp then
					-- add new direct to navaid
					dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
				end
				item_sel_via2 = 0
			end
		end
	elseif in_rte == 1 then
		--RTE2
		if in_fmc == 0 then
			if rte12_act == 0 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod2, 2)
			else
				offset_fpln = offset_mod2	--2
			end
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if fpln_num4 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					item_sel_via = 0
				elseif string.len(entry) == 0 then
					if item > 0 and item <= fpln_num4 then
						item_sel_via = item
						entry = fpln_data4[item][1]
					end
				elseif entry == ">DELETE" then
					if item > 0 and item <= fpln_num4 then
						if fpln_data4[item][1] ~= "" and offset < (fpln_data4[item][4] + fpln_data4[item][5] - 1) then
							rte_copy(fpln_data4[item][4] + fpln_data4[item][5], in_rte)
							if item < fpln_num4 and legs_data8[fpln_data4[item][4]-1][1] ~= "DISCONTINUITY" and legs_data8[fpln_data4[item][4]+fpln_data4[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data4[item][4], in_rte)
								rte_paste(fpln_data4[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data4[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry = ""
						end
					end
				elseif item_sel_via > item and item > 0 and item <= fpln_num4 and item > 0 then
					if fpln_data4[item][2] == "" then
						rte_copy(fpln_data4[item_sel_via][4], in_rte)
						rte_paste(fpln_data4[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry = ""
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry, "", fpln_data4[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				elseif item <= fpln_num4 and item > 0 then
					if item == 1 then
						dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry, "", fpln_data4[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				end
			else
				-- first waypoint
				if item == tmp_tmp then
					-- add new direct to navaid
					dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
				end
				item_sel_via = 0
			end
		else
			if rte12_act == 0 then
				-- offset_fpln = math.max(offset, 2)
				offset_fpln = math.max(offset_mod2, 2)
			else
				offset_fpln = offset_mod2		--2
			end
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if fpln_num4 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					item_sel_via2 = 0
				elseif string.len(entry2) == 0 then
					if item > 0 and item <= fpln_num4 then
						item_sel_via2 = item
						entry2 = fpln_data4[item][1]
					end
				elseif entry2 == ">DELETE" then
					if item > 0 and item <= fpln_num4 then
						if fpln_data4[item][1] ~= "" and offset < (fpln_data4[item][4] + fpln_data4[item][5] - 1) then
							rte_copy(fpln_data4[item][4] + fpln_data4[item][5], in_rte)
							if item < fpln_num4 and legs_data8[fpln_data4[item][4]-1][1] ~= "DISCONTINUITY" and legs_data8[fpln_data4[item][4]+fpln_data4[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data4[item][4], in_rte)
								rte_paste(fpln_data4[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data4[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry2 = ""
						end
					end
				elseif item_sel_via2 > item and item > 0 and item <= fpln_num4 and item > 0 then
					if fpln_data4[item][2] == "" then
						rte_copy(fpln_data4[item_sel_via2][4], in_rte)
						rte_paste(fpln_data4[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry2 = ""
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry2, "", fpln_data4[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				elseif item <= fpln_num4 and item > 0 then
					if item == 1 then
						dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry2, "", fpln_data4[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				end
			else
				-- first waypoint
				if item == tmp_tmp then
					-- add new direct to navaid
					dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
				end
				item_sel_via2 = 0
			end
		end
	end

end

function rte_x_lsk(in_key, in_fmc, in_rte)
	local item = 0
	local button = in_key
	local tmp_tmp = 0
	local offset_fpln = 0
	local ll = 0
	
	if in_rte == 0 then
		if in_fmc == 0 then
			-- if rte12_act == 0 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod, 2)
			-- else
				-- offset_fpln = offset_mod		--2
			-- end
			
			offset_fpln = math.max(offset_mod, 2)
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if fpln_num2 > 0 then
				if entry == ">DELETE" then
					if item > 0 and item <= fpln_num2 then
						del_via(item, 0, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data2[fpln_num2][1] == "" and fpln_data2[fpln_num2][2] ~= "" then
							-- add new via via
							via_via_add(entry, 0, in_rte)
						elseif fpln_data2[fpln_num2][1] ~= "" then
							-- add new via
							via_add(fpln_data2[fpln_num2][1], fpln_data2[fpln_num2][3], entry, 0, in_rte)
						end
						item_sel_via = 0
					elseif item <= fpln_num2 and fpln_num2 > 1 and item > 0 then
						-- change via
						via_chg(fpln_data2[item-1][1], fpln_data2[item-1][3], item, entry, 0, in_rte)
						item_sel_via = 0
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		else
			-- if rte12_act == 0 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod, 2)
			-- else
				-- offset_fpln = offset_mod		--2
			-- end
			
			offset_fpln = math.max(offset_mod, 2)
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if fpln_num2 > 0 then
				if entry2 == ">DELETE" then
					if item > 0 and item <= fpln_num2 then
						del_via(item, 1, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data2[fpln_num2][1] == "" and fpln_data2[fpln_num2][2] ~= "" then
							-- add new via via
							via_via_add(entry2, 1, in_rte)
						elseif fpln_data2[fpln_num2][1] ~= "" then
							-- add new via
							via_add(fpln_data2[fpln_num2][1], fpln_data2[fpln_num2][3], entry2, 1, in_rte)
						end
						item_sel_via2 = 0
					elseif item <= fpln_num2 and fpln_num2 > 1 and item > 0 then
						-- change via
						via_chg(fpln_data2[item-1][1], fpln_data2[item-1][3], item, entry2, 1, in_rte)
						item_sel_via2 = 0
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		end
	elseif in_rte == 1 then
		--RTE2
		if in_fmc == 0 then
			
			-- if rte12_act == 1 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod2, 2)
			-- else
				-- offset_fpln = offset_mod2		--2
			-- end
			
			offset_fpln = math.max(offset_mod2, 2)
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if fpln_num4 > 0 then
				if entry == ">DELETE" then
					if item > 0 and item <= fpln_num4 then
						del_via(item, 0, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data4[fpln_num4][1] == "" and fpln_data4[fpln_num4][2] ~= "" then
							-- add new via via
							via_via_add(entry, 0, in_rte)
						elseif fpln_data4[fpln_num4][1] ~= "" then
							-- add new via
							via_add(fpln_data4[fpln_num4][1], fpln_data4[fpln_num4][3], entry, 0, in_rte)
						end
						item_sel_via2 = 0
					elseif item <= fpln_num4 and fpln_num4 > 1 and item > 0 then
						-- change via
						via_chg(fpln_data4[item-1][1], fpln_data4[item-1][3], item, entry, 0, in_rte)
						item_sel_via2 = 0
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		else
			-- if rte12_act == 1 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod2, 2)
			-- else
				-- offset_fpln = offset_mod2		--2
			-- end
			
			offset_fpln = math.max(offset_mod2, 2)
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if fpln_num4 > 0 then
				if entry2 == ">DELETE" then
					if item > 0 and item <= fpln_num4 then
						del_via(item, 1, in_rte)
					end
				else
					if item == tmp_tmp then
						if fpln_data4[fpln_num4][1] == "" and fpln_data4[fpln_num4][2] ~= "" then
							-- add new via via
							via_via_add(entry2, 1, in_rte)
						elseif fpln_data4[fpln_num4][1] ~= "" then
							-- add new via
							via_add(fpln_data4[fpln_num4][1], fpln_data4[fpln_num4][3], entry2, 1, in_rte)
						end
						item_sel_via2 = 0
					elseif item <= fpln_num4 and fpln_num4 > 1 and item > 0 then
						-- change via
						via_chg(fpln_data4[item-1][1], fpln_data4[item-1][3], item, entry2, 1, in_rte)
						item_sel_via2 = 0
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		end
	end
end

function rte_x_rsk(in_key, in_fmc, in_rte)
	local item = 0
	local button = in_key
	local tmp_tmp = 0
	local offset_fpln = 0
	local ll = 0
	
	if in_rte == 0 then
		if in_fmc == 0 then
			-- if rte12_act == 0 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod, 2)
			-- else
				-- offset_fpln = offset_mod		--2
			-- end
			
			offset_fpln = math.max(offset_mod, 2)
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if fpln_num2 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					item_sel_via = 0
				elseif string.len(entry) == 0 then
					if item > 0 and item <= fpln_num2 then
						item_sel_via = item
						entry = fpln_data2[item][1]
					end
				elseif entry == ">DELETE" then
					if item > 0 and item <= fpln_num2 then
						if fpln_data2[item][1] ~= "" and offset < (fpln_data2[item][4] + fpln_data2[item][5] - 1) then
							rte_copy(fpln_data2[item][4] + fpln_data2[item][5], in_rte)
							if item < fpln_num2 and legs_data2[fpln_data2[item][4]-1][1] ~= "DISCONTINUITY" and legs_data2[fpln_data2[item][4]+fpln_data2[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data2[item][4], in_rte)
								rte_paste(fpln_data2[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data2[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry = ""
						end
					end
				elseif item_sel_via > item and item > 0 then
					if fpln_data2[item][2] == "" then
						rte_copy(fpln_data2[item_sel_via][4], in_rte)
						rte_paste(fpln_data2[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry = ""
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry, "", fpln_data2[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				elseif item <= fpln_num2 and item > 0 then
					-- add new via to navaid
					if item == 1 then
						dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry, "", fpln_data2[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				end
			end
		else
			-- if rte12_act == 0 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod, 2)
			-- else
				-- offset_fpln = offset_mod		--2
			-- end
			
			offset_fpln = math.max(offset_mod, 2)
			
			if legs_num2 > 1 then
				if string.sub(legs_data2[2][1], 1, 2) == "RW" and legs_data2[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num2
			if fpln_num2 > 0 then
				for ii = fpln_num2, 1, -1 do
					if offset_fpln >= fpln_data2[ii][4] and fpln_data2[ii][5] ~= 0 and offset_fpln < fpln_data2[ii][4] + fpln_data2[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num2 + 1
			if fpln_num2 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					item_sel_via2 = 0
				elseif string.len(entry2) == 0 then
					if item > 0 and item <= fpln_num2 then
						item_sel_via2 = item
						entry2 = fpln_data2[item][1]
					end
				elseif entry2 == ">DELETE" then
					if item > 0 and item <= fpln_num2 then
						if fpln_data2[item][1] ~= "" and offset < (fpln_data2[item][4] + fpln_data2[item][5] - 1) then
							rte_copy(fpln_data2[item][4] + fpln_data2[item][5], in_rte)
							if item < fpln_num2 and legs_data2[fpln_data2[item][4]-1][1] ~= "DISCONTINUITY" and legs_data2[fpln_data2[item][4]+fpln_data2[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data2[item][4], in_rte)
								rte_paste(fpln_data2[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data2[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry2 = ""
						end
					end
				elseif item_sel_via2 > item and item > 0 then
					if fpln_data2[item][2] == "" then
						rte_copy(fpln_data2[item_sel_via2][4], in_rte)
						rte_paste(fpln_data2[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry2 = ""
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry2, "", fpln_data2[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				elseif item <= fpln_num2 and item > 0 then
					-- add new via to navaid
					if item == 1 then
						dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					else
						dir_via_add(fpln_data2[item-1][1], fpln_data2[item-1][3], entry2, "", fpln_data2[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				end
			end
		end
	elseif in_rte == 1 then
		--RTE2
		if in_fmc == 0 then
			-- if rte12_act == 1 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod2, 2)
			-- else
				-- offset_fpln = offset_mod2		--2
			-- end
			
			offset_fpln = math.max(offset_mod2, 2)
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if fpln_num4 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					item_sel_via = 0
				elseif string.len(entry) == 0 then
					if item > 0 and item <= fpln_num4 then
						item_sel_via = item
						entry = fpln_data4[item][1]
					end
				elseif entry == ">DELETE" then
					if item > 0 and item <= fpln_num4 then
						if fpln_data4[item][1] ~= "" and offset < (fpln_data4[item][4] + fpln_data4[item][5] - 1) then
							rte_copy(fpln_data4[item][4] + fpln_data4[item][5], in_rte)
							if item < fpln_num4 and legs_data2[fpln_data4[item][4]-1][1] ~= "DISCONTINUITY" and legs_data2[fpln_data4[item][4]+fpln_data4[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data4[item][4], in_rte)
								rte_paste(fpln_data4[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data4[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry = ""
						end
					end
				elseif item_sel_via > item and item > 0 then
					if fpln_data4[item][2] == "" then
						rte_copy(fpln_data4[item_sel_via][4], in_rte)
						rte_paste(fpln_data4[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry = ""
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry, "", fpln_data4[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				elseif item <= fpln_num4 and item > 0 then
					-- add new via to navaid
					if item == 1 then
						dir_via_add("", "", entry, "", "", item, entry, 0, in_rte)
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry, "", fpln_data4[item][2], item, entry, 0, in_rte)
					end
					item_sel_via = 0
				end
			end
		else
			-- if rte12_act == 1 then
				-- -- offset_fpln = math.max(offset, 2)
				-- offset_fpln = math.max(offset_mod2, 2)
			-- else
				-- offset_fpln = offset_mod2		--2
			-- end
			
			offset_fpln = math.max(offset_mod2, 2)
			
			if legs_num8 > 1 then
				if string.sub(legs_data8[2][1], 1, 2) == "RW" and legs_data8[2][19] == 1 then
					if offset_fpln < 3 then
						offset_fpln = 3
					end
				end
			end
			
			ll = fpln_num4
			if fpln_num4 > 0 then
				for ii = fpln_num4, 1, -1 do
					if offset_fpln >= fpln_data4[ii][4] and fpln_data4[ii][5] ~= 0 and offset_fpln < fpln_data4[ii][4] + fpln_data4[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			item = ((act_page2 - 2) * 5) + ll + button
			
			tmp_tmp = fpln_num4 + 1
			if fpln_num4 > 0 then
				if item == tmp_tmp then
					dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					item_sel_via2 = 0
				elseif string.len(entry2) == 0 then
					if item > 0 and item <= fpln_num4 then
						item_sel_via2 = item
						entry2 = fpln_data4[item][1]
					end
				elseif entry2 == ">DELETE" then
					if item > 0 and item <= fpln_num4 then
						if fpln_data4[item][1] ~= "" and offset < (fpln_data4[item][4] + fpln_data4[item][5] - 1) then
							rte_copy(fpln_data4[item][4] + fpln_data4[item][5], in_rte)
							if item < fpln_num4 and legs_data2[fpln_data4[item][4]-1][1] ~= "DISCONTINUITY" and legs_data2[fpln_data4[item][4]+fpln_data4[item][5]][1] ~= "DISCONTINUITY" then
								rte_add_disco(fpln_data4[item][4], in_rte)
								rte_paste(fpln_data4[item][4] + 1, in_rte)
							else
								rte_paste(fpln_data4[item][4], in_rte)
							end
							create_fpln(in_rte)
							calc_rte_enable2 = 1
							if rte12_act == in_rte then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
							entry2 = ""
						end
					end
				elseif item_sel_via2 > item and item > 0 then
					if fpln_data4[item][2] == "" then
						rte_copy(fpln_data4[item_sel_via2][4], in_rte)
						rte_paste(fpln_data4[item][4], in_rte)
						create_fpln(in_rte)
						calc_rte_enable2 = 1
						if rte12_act == in_rte then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
						entry2 = ""
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry2, "", fpln_data4[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				elseif item <= fpln_num4 and item > 0 then
					-- add new via to navaid
					if item == 1 then
						dir_via_add("", "", entry2, "", "", item, entry2, 1, in_rte)
					else
						dir_via_add(fpln_data4[item-1][1], fpln_data4[item-1][3], entry2, "", fpln_data4[item][2], item, entry2, 1, in_rte)
					end
					item_sel_via2 = 0
				end
			end
		end
	end

end
-- rte12_sel
-- legs_num2
-- legs_data2
-- calc_rte_enable2
-- create_legs_abeam_list
---------------------------------------------------------------


-- 1LSK
function B738_fmc1_1L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		if page_dep == 1 then
			if rte12_sel == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel[1] ~= "" then
						if ref_sid2 == ref_sid_sel[1] then
							if sid_list_num == 1 then
								ref_sid2 = "------"
								ref_sid_tns2 = "------"
								sel_ref_sid2 = ref_sid2
								sel_ref_sid_tns2 = ref_sid_tns2
								if sel_ref_rwy2 == "-----" then
									create_rnw_list(rte12_sel)
								end
								create_sid_list(rte12_sel)
								ref_sid_exec = 1
							else
								sel_ref_sid2 = ref_sid2
								if sel_ref_rwy2 == "-----" then
									create_rnw_list(rte12_sel)
								end
								create_sid_tns_list(rte12_sel)
							end
						else
							ref_sid2 = ref_sid_sel[1]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						
						act_page = 1
					end
				else
					ref_sid2 = "------"
					ref_sid_tns2 = "------"
					sel_ref_sid2 = ref_sid2
					sel_ref_sid_tns2 = ref_sid_tns2
					if sel_ref_rwy2 == "-----" then
						create_rnw_list(rte12_sel)
					end
					create_sid_list(rte12_sel)
					act_page = 1
					ref_sid_exec = 1
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel[1] ~= "" then
						if ref_sid8 == ref_sid_sel[1] then
							if sid_list_num == 1 then
								ref_sid8 = "------"
								ref_sid_tns8 = "------"
								sel_ref_sid8 = ref_sid8
								sel_ref_sid_tns8 = ref_sid_tns8
								if sel_ref_rwy8 == "-----" then
									create_rnw_list(rte12_sel)
								end
								create_sid_list(rte12_sel)
								ref_sid_exec = 1
							else
								sel_ref_sid8 = ref_sid8
								if sel_ref_rwy8 == "-----" then
									create_rnw_list(rte12_sel)
								end
								create_sid_tns_list(rte12_sel)
							end
						else
							ref_sid8 = ref_sid_sel[1]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						
						act_page = 1
					end
				else
					ref_sid8 = "------"
					ref_sid_tns8 = "------"
					sel_ref_sid8 = ref_sid8
					sel_ref_sid_tns8 = ref_sid_tns8
					if sel_ref_rwy8 == "-----" then
						create_rnw_list(rte12_sel)
					end
					create_sid_list(rte12_sel)
					act_page = 1
					ref_sid_exec = 1
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel[1] ~= "" then
						if des_star2 == des_star_sel[1] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star2 = des_star_sel[1]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					des_star2 = "------"
					des_star_trans2 = "------"
					sel_des_star2 = des_star2
					sel_des_star_trans2 = des_star_trans2
					if sel_des_app2 == "------" then
						create_app_list(rte12_sel)
					end
					create_star_list(rte12_sel)
					act_page = 1
					des_star_exec = 1
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel[1] ~= "" then
						if des_star8 == des_star_sel[1] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star8 = des_star_sel[1]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					des_star8 = "------"
					des_star_trans8 = "------"
					sel_des_star8 = des_star8
					sel_des_star_trans8 = des_star_trans8
					if sel_des_app8 == "------" then
						create_app_list(rte12_sel)
					end
					create_star_list(rte12_sel)
					act_page = 1
					des_star_exec = 1
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_dep_arr == 1 then
			-- local dep_arr_enable = false
			-- -- if legs_num2 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 1 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			-- if legs_num2 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
				-- if in_flight_mode == 0 and legs_data2[1][17] == 4 then
					-- dep_arr_enable = true
				-- elseif legs_data2[1][17] == 0 then
					-- dep_arr_enable = true
				-- end
			-- end
			-- if dep_arr_enable == true then
			if legs_num2 > 0 then
				-- SID departures
				rte12_sel = 0
				if ref_icao_x ~= "----" and des_icao_x ~= "****" and ref_icao_x ~= "ORGN" then
					page_dep = 1
					page_dep_arr = 0
					arr_data_x = 0
					find_ref_rnw_data(1, 0)
					read_ref_list_icao(ref_icao_x, 0)
					
					sel_ref_rwy2 = "-----"
					sel_ref_sid2 = "------"
					sel_ref_sid_tns2 = "------"
					
					create_rnw_list(0)
					create_sid_list(0)
					create_sid_tns_list(0)
				end
			end
		elseif page_rte_init == 1 then --and in_flight_mode == 0 then	-- only on the groundthen
			if act_page == 1 then
				-- entry Ref airport ICAO
				local apt_ok = 0
				local xy = 0
				if entry == ">DELETE" then
					entry = INVALID_DELETE
				else
					if rte12_sel == 0 then
						if string.len(entry) == 4 then
							
							if ref_apt_exist(entry, 0) then
								ref_icao_x = entry
								ref_tns_alt_x = 0
								ref_tns_lvl_x = 0
								ref_icao_alt_x = 0
								trans_alt_mod = "-----"
								ref_gate_x = "-----"
								
								ref_rwy2 = "-----"
								ref_sid2 = "------"
								ref_sid_tns2 = "------"
								
								des_icao_x = "****"
								des_tns_alt_x = 0
								des_tns_lvl_x = 0
								des_icao_alt_x = 0
								trans_lvl_mod = "-----"
								
								des_app2 = "------"
								des_app_tns2 = "------"
								des_star2 = "------"
								des_star_trans2 = "------"
								
								co_route_x = "------------"
								if rte12_act >= 0 then
									flt_num_x = flt_num
								else
									flt_num_x = "--------"
								end
								
								ref_rwy3 = "-----"
								
								if rte12_act ~= 0 then
									ref_icao = "----"
									des_icao = "****"
									ref_gate = "-----"
									co_route = "------------"
									ref_rwy = "-----"
									ref_sid = "------"
									ref_sid_tns = "------"
									des_star = "------"
									des_star_trans = "------"
									des_app = "------"
									des_app_tns = "------"
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
									
									ref_icao_lat = 0
									ref_icao_lon = 0
									des_icao_lat = 0
									des_icao_lon = 0
									ref_tns_alt = 0
									des_tns_alt = 0
									des_tns_lvl = 0
									ref_icao_alt = 0
									des_icao_alt = 0
									arr_data = 0
									des_app_rw_only = 0
									trans_alt = "-----"
									trans_lvl = "-----"
								end
								
								if apt_exist(entry) == true then
									ref_icao_lat_x = icao_latitude
									ref_icao_lon_x = icao_longitude
									ref_tns_alt_x = icao_tns_alt
									ref_tns_lvl_x = icao_tns_lvl
									ref_icao_alt_x = 0
									B738DR_find_icao = entry
									B738DR_find_icao_cmd = 1
									if B738DR_find_icao_cmd == 2 then
										ref_icao_alt_x = B738DR_find_icao_height * 3.280839895
									end
								else
									ref_tns_alt_x = 0
									ref_tns_lvl_x = 0
								end
								if ref_tns_alt_x == 0 then
									trans_alt_mod = "-----"
								else
									trans_alt_mod = string.format("%5d", ref_tns_alt_x)
								end
								
								hold_data_num2 = 0
								
								-- REF ICAO
								legs_num2 = 0
								legs_data2[1] = {}
								legs_data2[1][1] = ref_icao_x
								legs_data2[1][2] = 0		-- brg
								legs_data2[1][3] = 0		-- distance
								legs_data2[1][4] = 0		-- speed
								legs_data2[1][5] = ref_icao_alt_x		-- altitude
								legs_data2[1][6] = 0	-- altitude type
								legs_data2[1][7] = ref_icao_lat_x		-- latitude
								legs_data2[1][8] = ref_icao_lon_x		-- longitude
								legs_data2[1][9] = ""			-- via id
								legs_data2[1][10] = 0		-- calc speed
								legs_data2[1][11] = 0		-- calc altitude
								legs_data2[1][12] = 0		-- calc altitude vnav pth
								legs_data2[1][13] = 0
								legs_data2[1][14] = 0		-- rest alt
								legs_data2[1][15] = 0		-- last fuel
								legs_data2[1][16] = ""
								legs_data2[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
								legs_data2[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
								legs_data2[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
								legs_data2[1][20] = 0
								legs_data2[1][21] = -1
								legs_data2[1][22] = ""
								legs_data2[1][23] = 0
								legs_data2[1][24] = 0
								legs_data2[1][25] = 0
								legs_data2[1][26] = 0
								legs_data2[1][27] = ""
								legs_data2[1][28] = ""
								legs_data2[1][29] = ""
								legs_data2[1][30] = ""
								legs_data2[1][31] = ""
								legs_data2[1][32] = 0
								legs_data2[1][33] = ""
								legs_data2[1][34] = ""
								legs_data2[1][35] = ""
								legs_data2[1][36] = 9
								legs_data2[1][37] = 0
								legs_data2[1][38] = ""
								legs_data2[1][39] = ""
								legs_data2[1][40] = 0
								legs_data2[1][41] = 0
								legs_data2[1][42] = 0
								legs_data2[1][43] = ""
								legs_data2[1][44] = 0
								legs_data2[1][45] = 0
								legs_data2[1][46] = 0
								legs_data2[1][47] = 0
								legs_data2[1][48] = ""
								legs_data2[1][49] = 0
								legs_data2[1][50] = 0
								legs_data2[1][51] = 0
								legs_data2[1][52] = 0
								legs_data2[1][53] = 0
								legs_data2[1][54] = 0
								offset_mod = 0
								
								arr_data_x = 0
								airport_pos(rte12_sel)
								create_rnw_list(rte12_sel)
								create_sid_list(rte12_sel)
								
								if rte12_act == rte12_sel then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
								end
								
								if legs_num ~= 0 then
									if rte12_act == 0 then
										legs_delete = 1
										ref_exec = 1
										des_exec = 0
									end
								end
								
								if exec_activate_fpln ~= 0 then
									rte_exec = 0
									exec_activate_fpln = 0
									
									copy_to_legs_data2_buf(0)
									if legs_num2_buf > 1 then
										for n = 1, (legs_num2_buf + 1) do
											legs_data2_buf[n][11] = 0
											legs_data2_buf[n][10] = 0
											legs_data2_buf[n][14] = 50000
											legs_data2_buf[n][38] = ""
										end
									end
									copy_from_legs_data2_buf(0)
									
								end
								
								find_ref_rnw_data(1, 0)
								entry = ""
								
							else
								add_fmc_msg(NOT_IN_DATABASE, 1)
							end
						elseif entry == "" and ref_icao_x ~= "----" and ref_icao_x ~= "ORGN" then
							entry = ref_icao_x
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						if string.len(entry) == 4 then
							
							if ref_apt_exist(entry, 1) then
								ref_icao7_x = entry
								ref_tns_alt7_x = 0
								ref_tns_lvl7_x = 0
								ref_icao_alt7_x = 0
								trans_alt_mod2 = "-----"
								ref_gate7_x = "-----"
								
								ref_rwy8 = "-----"
								ref_sid8 = "------"
								ref_sid_tns8 = "------"
								
								des_icao7_x = "****"
								des_tns_alt7_x = 0
								des_tns_lvl7_x = 0
								des_icao_alt7_x = 0
								trans_lvl_mod2 = "-----"
								
								des_app8 = "------"
								des_app_tns8 = "------"
								des_star8 = "------"
								des_star_trans8 = "------"
								
								co_route7_x = "------------"
								if rte12_act >= 0 then
									flt_num_x = flt_num
								else
									flt_num_x = "--------"
								end
								
								ref_rwy9 = "-----"
								
								if rte12_act ~= 1 then
									ref_icao7 = "----"
									des_icao7 = "****"
									ref_gate7 = "-----"
									co_route7 = "------------"
									ref_rwy7 = "-----"
									ref_sid7 = "------"
									ref_sid_tns7 = "------"
									des_star7 = "------"
									des_star_trans7 = "------"
									des_app7 = "------"
									des_app_tns7 = "------"
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
									
									ref_icao_lat7 = 0
									ref_icao_lon7 = 0
									des_icao_lat7 = 0
									des_icao_lon7 = 0
									ref_tns_alt7 = 0
									des_tns_alt7 = 0
									des_tns_lvl7 = 0
									ref_icao_alt7 = 0
									des_icao_alt7 = 0
									arr_data7 = 0
									des_app_rw_only7 = 0
									trans_alt7 = "-----"
									trans_lvl7 = "-----"
								end
								
								if apt_exist(entry) == true then
									ref_icao_lat7_x = icao_latitude
									ref_icao_lon7_x = icao_longitude
									ref_tns_alt7_x = icao_tns_alt
									ref_tns_lvl7_x = icao_tns_lvl
									ref_icao_alt7_x = 0
									B738DR_find_icao = entry
									B738DR_find_icao_cmd = 1
									if B738DR_find_icao_cmd == 2 then
										ref_icao_alt7_x = B738DR_find_icao_height * 3.280839895
									end
								else
									ref_tns_alt7_x = 0
									ref_tns_lvl7_x = 0
								end
								if ref_tns_alt7_x == 0 then
									trans_alt_mod2 = "-----"
								else
									trans_alt_mod2 = string.format("%5d", ref_tns_alt7_x)
								end
								
								hold_data_num4 = 0
								
								-- REF ICAO
								legs_num8 = 0
								legs_data8[1] = {}
								legs_data8[1][1] = ref_icao7_x
								legs_data8[1][2] = 0		-- brg
								legs_data8[1][3] = 0		-- distance
								legs_data8[1][4] = 0		-- speed
								legs_data8[1][5] = ref_icao_alt7_x		-- altitude
								legs_data8[1][6] = 0	-- altitude type
								legs_data8[1][7] = ref_icao_lat7_x		-- latitude
								legs_data8[1][8] = ref_icao_lon7_x		-- longitude
								legs_data8[1][9] = ""			-- via id
								legs_data8[1][10] = 0		-- calc speed
								legs_data8[1][11] = 0		-- calc altitude
								legs_data8[1][12] = 0		-- calc altitude vnav pth
								legs_data8[1][13] = 0
								legs_data8[1][14] = 0		-- rest alt
								legs_data8[1][15] = 0		-- last fuel
								legs_data8[1][16] = ""
								legs_data8[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
								legs_data8[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
								legs_data8[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
								legs_data8[1][20] = 0
								legs_data8[1][21] = -1
								legs_data8[1][22] = ""
								legs_data8[1][23] = 0
								legs_data8[1][24] = 0
								legs_data8[1][25] = 0
								legs_data8[1][26] = 0
								legs_data8[1][27] = ""
								legs_data8[1][28] = ""
								legs_data8[1][29] = ""
								legs_data8[1][30] = ""
								legs_data8[1][31] = ""
								legs_data8[1][32] = 0
								legs_data8[1][33] = ""
								legs_data8[1][34] = ""
								legs_data8[1][35] = ""
								legs_data8[1][36] = 9
								legs_data8[1][37] = 0
								legs_data8[1][38] = ""
								legs_data8[1][39] = ""
								legs_data8[1][40] = 0
								legs_data8[1][41] = 0
								legs_data8[1][42] = 0
								legs_data8[1][43] = ""
								legs_data8[1][44] = 0
								legs_data8[1][45] = 0
								legs_data8[1][46] = 0
								legs_data8[1][47] = 0
								legs_data8[1][48] = ""
								legs_data8[1][49] = 0
								legs_data8[1][50] = 0
								legs_data8[1][51] = 0
								legs_data8[1][52] = 0
								legs_data8[1][53] = 0
								legs_data8[1][54] = 0
								offset_mod2 = 0
								
								arr_data7_x = 0
								airport_pos(rte12_sel)
								create_rnw_list(rte12_sel)
								create_sid_list(rte12_sel)
								
								if rte12_act == rte12_sel then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
								end
								
								if legs_num ~= 0 then
									if rte12_act == 1 then
										legs_delete = 1
										ref_exec2 = 1
										des_exec2 = 0
									end
								end
								
								if exec_activate_fpln2 ~= 0 then
									rte_exec2 = 0
									exec_activate_fpln2 = 0
									
									copy_to_legs_data2_buf(1)
									if legs_num2_buf > 1 then
										for n = 1, (legs_num2_buf + 1) do
											legs_data2_buf[n][11] = 0
											legs_data2_buf[n][10] = 0
											legs_data2_buf[n][14] = 50000
											legs_data2_buf[n][38] = ""
										end
									end
									copy_from_legs_data2_buf(1)
									
								end
								
								find_ref_rnw_data(1, 1)
								entry = ""
							else
								add_fmc_msg(NOT_IN_DATABASE, 1)
							end
						elseif entry == "" and ref_icao7_x ~= "----" and ref_icao7_x ~= "ORGN" then
							entry = ref_icao7_x
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				-- button 1 LSK
				rte_1_lsk(1, 0, rte12_sel)
			end
		elseif page_sel_wpt == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry, 0, rte12_sel)
			end
		elseif page_sel_wpt3 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 0)
			end
		elseif page_sel_hold_wpt == 1 then
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 0, rte12_sel)
			end
		elseif page_ref_sel == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				ref_nav_new = "*****"
				page_ref_nav_data = 1
				page_ref_sel = 0
			end
		elseif page_fix == 1 then
			local ii = 0
			local jj = 0
			local fix_idx = 0
			local fix_x_lat = 0
			local fix_x_lon = 0
			if entry == ">DELETE" then
				if act_page <= 0 then
					if string.sub(fix_data[act_page][1], 1, 2) == "RW" then
						fix_rnw_lat = 0
						fix_rnw_lon = 0
						fix_rnw_crs = 0
						fix_rnw_num = ""
					end
				end
				if fix_data_num == act_page then
					fix_abm_act[fix_data_num] = 0
					fix_abm_wpt_idx[fix_data_num] = -1
					fix_abm_wpt_dist[fix_data_num] = -1
					fix_abm_lat[fix_data_num] = -1
					fix_abm_lon[fix_data_num] = -1
					fix_abm_brg[fix_data_num] = -1
					fix_abm_dist[fix_data_num] = -1
					fix_abm_eta[fix_data_num] = -1
					fix_abm_alt[fix_data_num] = -1
					
					fix_data_num = fix_data_num - 1
					entry = ""
				elseif act_page < fix_data_num then
					for ii = act_page, (fix_data_num - 1) do
						for jj = 1, 38 do
							fix_data[ii][jj] = fix_data[ii+1][jj]
						end
						fix_abm_act[ii] = fix_abm_act[ii+1]
						fix_abm_wpt_idx[ii] = fix_abm_wpt_idx[ii+1]
						fix_abm_wpt_dist[ii] = fix_abm_wpt_dist[ii+1]
						fix_abm_lat[ii] = fix_abm_lat[ii+1]
						fix_abm_lon[ii] = fix_abm_lon[ii+1]
						fix_abm_brg[ii] = fix_abm_brg[ii+1]
						fix_abm_dist[ii] = fix_abm_dist[ii+1]
						fix_abm_eta[ii] = fix_abm_eta[ii+1]
						fix_abm_alt[ii] = fix_abm_alt[ii+1]
					end
					fix_data_num = fix_data_num - 1
					entry = ""
				end
			elseif entry == "" then
				if act_page > 0 and act_page <= fix_data_num then
					entry = fix_data[act_page][1]
				end
			elseif entry ~= "" then
				fix_idx = act_page
				
				if item_sel > 0 then
					if rte12_act == 0 then
						if item_sel <= legs_num2 then
							if fix_idx > fix_data_num then
								fix_data_num = fix_data_num + 1
								fix_idx = fix_data_num
							else
								if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
									fix_rnw_lat = 0
									fix_rnw_lon = 0
									fix_rnw_crs = 0
									fix_rnw_num = ""
								end
							end
							fix_data[fix_idx] = {}
							fix_data[fix_idx][1] = legs_data2[item_sel][1]
							fix_data[fix_idx][2] = legs_data2[item_sel][36] -- navaid type: 1-VOR, 2-VOR TAG, 3-NDB, 4-WPT, 5-DME, 9-APT (legs_data2[item_sel2][1])
							if fix_data[fix_idx][2] == 0 then
								fix_data[fix_idx][2] = 3
							elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
								fix_data[fix_idx][2] = 3
							end
							-- fix_data[fix_idx][3] = legs_data2[item_sel][7]
							-- fix_data[fix_idx][4] = legs_data2[item_sel][8]
							fix_data[fix_idx][3], fix_data[fix_idx][4] = lat_lon_legs2(item_sel)
							fix_data[fix_idx][5] = legs_data2[item_sel][16]	-- reg code
							fix_data[fix_idx][6] = -1
							fix_data[fix_idx][7] = -1
							fix_data[fix_idx][8] = -1
							fix_data[fix_idx][9] = -1
							fix_data[fix_idx][10] = -1
							fix_data[fix_idx][11] = -1
							-- 1. radial
							fix_data[fix_idx][12] = 0	-- wpt before
							fix_data[fix_idx][13] = 0	-- dist before
							fix_data[fix_idx][14] = 0	-- lat
							fix_data[fix_idx][15] = 0	-- lon
							fix_data[fix_idx][16] = 0	-- dist calc
							fix_data[fix_idx][17] = 0	-- rad calc
							fix_data[fix_idx][18] = -1	-- eta calc
							fix_data[fix_idx][19] = 0	-- dtg calc
							fix_data[fix_idx][20] = -1	-- alt calc
							-- 2. radial
							fix_data[fix_idx][21] = 0	-- wpt before
							fix_data[fix_idx][22] = 0	-- dist before
							fix_data[fix_idx][23] = 0	-- lat
							fix_data[fix_idx][24] = 0	-- lon
							fix_data[fix_idx][25] = 0	-- dist calc
							fix_data[fix_idx][26] = 0	-- rad calc
							fix_data[fix_idx][27] = -1	-- eta calc
							fix_data[fix_idx][28] = 0	-- dtg calc
							fix_data[fix_idx][29] = -1	-- alt calc
							-- 3. radial
							fix_data[fix_idx][30] = 0	-- wpt before
							fix_data[fix_idx][31] = 0	-- dist before
							fix_data[fix_idx][32] = 0	-- lat
							fix_data[fix_idx][33] = 0	-- lon
							fix_data[fix_idx][34] = 0	-- dist calc
							fix_data[fix_idx][35] = 0	-- rad calc
							fix_data[fix_idx][36] = -1	-- eta calc
							fix_data[fix_idx][37] = 0	-- dtg calc
							fix_data[fix_idx][38] = -1	-- alt calc
							
							fix_abm_act[fix_idx] = 0
							fix_abm_wpt_idx[fix_idx] = -1
							fix_abm_wpt_dist[fix_idx] = -1
							fix_abm_lat[fix_idx] = -1
							fix_abm_lon[fix_idx] = -1
							fix_abm_brg[fix_idx] = -1
							fix_abm_dist[fix_idx] = -1
							fix_abm_eta[fix_idx] = -1
							fix_abm_alt[fix_idx] = -1
							
							item_sel = 0
							item_sel_act = 0
							entry = ""
						end
					elseif rte12_act == 1 then
						if item_sel <= legs_num8 then
							if fix_idx > fix_data_num then
								fix_data_num = fix_data_num + 1
								fix_idx = fix_data_num
							else
								if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
									fix_rnw_lat = 0
									fix_rnw_lon = 0
									fix_rnw_crs = 0
									fix_rnw_num = ""
								end
							end
							fix_data[fix_idx] = {}
							fix_data[fix_idx][1] = legs_data8[item_sel][1]
							fix_data[fix_idx][2] = legs_data8[item_sel][36] -- navaid type: 1-VOR, 2-VOR TAG, 3-NDB, 4-WPT, 5-DME, 9-APT (legs_data8[item_sel2][1])
							if fix_data[fix_idx][2] == 0 then
								fix_data[fix_idx][2] = 3
							elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
								fix_data[fix_idx][2] = 3
							end
							-- fix_data[fix_idx][3] = legs_data8[item_sel][7]
							-- fix_data[fix_idx][4] = legs_data8[item_sel][8]
							fix_data[fix_idx][3], fix_data[fix_idx][4] = lat_lon_legs8(item_sel)
							fix_data[fix_idx][5] = legs_data8[item_sel][16]	-- reg code
							fix_data[fix_idx][6] = -1
							fix_data[fix_idx][7] = -1
							fix_data[fix_idx][8] = -1
							fix_data[fix_idx][9] = -1
							fix_data[fix_idx][10] = -1
							fix_data[fix_idx][11] = -1
							-- 1. radial
							fix_data[fix_idx][12] = 0	-- wpt before
							fix_data[fix_idx][13] = 0	-- dist before
							fix_data[fix_idx][14] = 0	-- lat
							fix_data[fix_idx][15] = 0	-- lon
							fix_data[fix_idx][16] = 0	-- dist calc
							fix_data[fix_idx][17] = 0	-- rad calc
							fix_data[fix_idx][18] = -1	-- eta calc
							fix_data[fix_idx][19] = 0	-- dtg calc
							fix_data[fix_idx][20] = -1	-- alt calc
							-- 2. radial
							fix_data[fix_idx][21] = 0	-- wpt before
							fix_data[fix_idx][22] = 0	-- dist before
							fix_data[fix_idx][23] = 0	-- lat
							fix_data[fix_idx][24] = 0	-- lon
							fix_data[fix_idx][25] = 0	-- dist calc
							fix_data[fix_idx][26] = 0	-- rad calc
							fix_data[fix_idx][27] = -1	-- eta calc
							fix_data[fix_idx][28] = 0	-- dtg calc
							fix_data[fix_idx][29] = -1	-- alt calc
							-- 3. radial
							fix_data[fix_idx][30] = 0	-- wpt before
							fix_data[fix_idx][31] = 0	-- dist before
							fix_data[fix_idx][32] = 0	-- lat
							fix_data[fix_idx][33] = 0	-- lon
							fix_data[fix_idx][34] = 0	-- dist calc
							fix_data[fix_idx][35] = 0	-- rad calc
							fix_data[fix_idx][36] = -1	-- eta calc
							fix_data[fix_idx][37] = 0	-- dtg calc
							fix_data[fix_idx][38] = -1	-- alt calc
							
							fix_abm_act[fix_idx] = 0
							fix_abm_wpt_idx[fix_idx] = -1
							fix_abm_wpt_dist[fix_idx] = -1
							fix_abm_lat[fix_idx] = -1
							fix_abm_lon[fix_idx] = -1
							fix_abm_brg[fix_idx] = -1
							fix_abm_dist[fix_idx] = -1
							fix_abm_eta[fix_idx] = -1
							fix_abm_alt[fix_idx] = -1
							
							item_sel = 0
							item_sel_act = 0
							entry = ""
						end
					end
				elseif string.len(entry) > 1 then
					local use_des_icao = "****"
					if rte12_act == 0 then
						use_des_icao = des_icao
					elseif rte12_act == 1 then
						use_des_icao = des_icao7
					end
					if string.sub(entry, 1, 2) == "RW" and use_des_icao ~= "****" then
						-- find destination runway
						fix_x_lat = 0
						fix_x_lon = 0
						if rte12_act == 0 then
							if arr_data_x == 0 then
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_des_runway(entry, rte12_act)
							else
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_ref_runway(entry, rte12_act)
							end
						elseif rte12_act == 1 then
							if arr_data7_x == 0 then
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_des_runway(entry, rte12_act)
							else
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_ref_runway(entry, rte12_act)
							end
						end
						if fix_x_lat == 0 and fix_x_lon == 0 then
							add_fmc_msg(NOT_IN_DATABASE, 1)
						else
							if fix_idx > fix_data_num then
								fix_data_num = fix_data_num + 1
								fix_idx = fix_data_num
							end
							fix_data[fix_idx] = {}
							fix_data[fix_idx][1] = entry
							fix_data[fix_idx][2] = 4	-- type
							fix_data[fix_idx][3] = fix_x_lat	-- lat
							fix_data[fix_idx][4] = fix_x_lon	-- lon
							fix_data[fix_idx][5] = ""	-- reg code
							fix_data[fix_idx][6] = -1	-- rad1
							fix_data[fix_idx][7] = -1	-- dist1
							fix_data[fix_idx][8] = -1	-- rad2
							fix_data[fix_idx][9] = -1	-- dist2
							fix_data[fix_idx][10] = -1	-- rad3
							fix_data[fix_idx][11] = -1	-- dist3
							-- 1. radial
							fix_data[fix_idx][12] = 0	-- wpt before
							fix_data[fix_idx][13] = 0	-- dist before
							fix_data[fix_idx][14] = 0	-- lat
							fix_data[fix_idx][15] = 0	-- lon
							fix_data[fix_idx][16] = 0	-- dist calc
							fix_data[fix_idx][17] = 0	-- rad calc
							fix_data[fix_idx][18] = -1	-- eta calc
							fix_data[fix_idx][19] = 0	-- dtg calc
							fix_data[fix_idx][20] = -1	-- alt calc
							-- 2. radial
							fix_data[fix_idx][21] = 0	-- wpt before
							fix_data[fix_idx][22] = 0	-- dist before
							fix_data[fix_idx][23] = 0	-- lat
							fix_data[fix_idx][24] = 0	-- lon
							fix_data[fix_idx][25] = 0	-- dist calc
							fix_data[fix_idx][26] = 0	-- rad calc
							fix_data[fix_idx][27] = -1	-- eta calc
							fix_data[fix_idx][28] = 0	-- dtg calc
							fix_data[fix_idx][29] = -1	-- alt calc
							-- 3. radial
							fix_data[fix_idx][30] = 0	-- wpt before
							fix_data[fix_idx][31] = 0	-- dist before
							fix_data[fix_idx][32] = 0	-- lat
							fix_data[fix_idx][33] = 0	-- lon
							fix_data[fix_idx][34] = 0	-- dist calc
							fix_data[fix_idx][35] = 0	-- rad calc
							fix_data[fix_idx][36] = -1	-- eta calc
							fix_data[fix_idx][37] = 0	-- dtg calc
							fix_data[fix_idx][38] = -1	-- alt calc
							
							fix_abm_act[fix_idx] = 0
							fix_abm_wpt_idx[fix_idx] = -1
							fix_abm_wpt_dist[fix_idx] = -1
							fix_abm_lat[fix_idx] = -1
							fix_abm_lon[fix_idx] = -1
							fix_abm_brg[fix_idx] = -1
							fix_abm_dist[fix_idx] = -1
							fix_abm_eta[fix_idx] = -1
							fix_abm_alt[fix_idx] = -1
							
							entry = ""
							-- find runway crs
							fix_rnw_lat = fix_x_lat
							fix_rnw_lon = fix_x_lon
						end
					else
						find_navaid(entry, "", 0, "", rte12_act)
						if navaid_list_n == 0 then
							--entry = ""
							add_fmc_msg(NOT_IN_DATABASE, 1)
						else
							-- add new fix from sel desires
							if navaid_list_n == 1 then
								if fix_idx > fix_data_num then
									fix_data_num = fix_data_num + 1
									fix_idx = fix_data_num
								else
									if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
										fix_rnw_lat = 0
										fix_rnw_lon = 0
										fix_rnw_crs = 0
										fix_rnw_num = ""
									end
								end
								fix_data[fix_idx] = {}
								fix_data[fix_idx][1] = entry
								fix_data[fix_idx][2] = navaid_list[1][1]	-- type
								if fix_data[fix_idx][2] == 0 then
									fix_data[fix_idx][2] = 3
								elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
									fix_data[fix_idx][2] = 3
								end
								fix_data[fix_idx][3] = navaid_list[1][2]	-- lat
								fix_data[fix_idx][4] = navaid_list[1][3]	-- lon
								fix_data[fix_idx][5] = navaid_list[1][8]	-- reg code
								fix_data[fix_idx][6] = -1	-- rad1
								fix_data[fix_idx][7] = -1	-- dist1
								fix_data[fix_idx][8] = -1	-- rad2
								fix_data[fix_idx][9] = -1	-- dist2
								fix_data[fix_idx][10] = -1	-- rad3
								fix_data[fix_idx][11] = -1	-- dist3
								-- 1. radial
								fix_data[fix_idx][12] = 0	-- wpt before
								fix_data[fix_idx][13] = 0	-- dist before
								fix_data[fix_idx][14] = 0	-- lat
								fix_data[fix_idx][15] = 0	-- lon
								fix_data[fix_idx][16] = 0	-- dist calc
								fix_data[fix_idx][17] = 0	-- rad calc
								fix_data[fix_idx][18] = -1	-- eta calc
								fix_data[fix_idx][19] = 0	-- dtg calc
								fix_data[fix_idx][20] = -1	-- alt calc
								-- 2. radial
								fix_data[fix_idx][21] = 0	-- wpt before
								fix_data[fix_idx][22] = 0	-- dist before
								fix_data[fix_idx][23] = 0	-- lat
								fix_data[fix_idx][24] = 0	-- lon
								fix_data[fix_idx][25] = 0	-- dist calc
								fix_data[fix_idx][26] = 0	-- rad calc
								fix_data[fix_idx][27] = -1	-- eta calc
								fix_data[fix_idx][28] = 0	-- dtg calc
								fix_data[fix_idx][29] = -1	-- alt calc
								-- 3. radial
								fix_data[fix_idx][30] = 0	-- wpt before
								fix_data[fix_idx][31] = 0	-- dist before
								fix_data[fix_idx][32] = 0	-- lat
								fix_data[fix_idx][33] = 0	-- lon
								fix_data[fix_idx][34] = 0	-- dist calc
								fix_data[fix_idx][35] = 0	-- rad calc
								fix_data[fix_idx][36] = -1	-- eta calc
								fix_data[fix_idx][37] = 0	-- dtg calc
								fix_data[fix_idx][38] = -1	-- alt calc
								
								fix_abm_act[fix_idx] = 0
								fix_abm_wpt_idx[fix_idx] = -1
								fix_abm_wpt_dist[fix_idx] = -1
								fix_abm_lat[fix_idx] = -1
								fix_abm_lon[fix_idx] = -1
								fix_abm_brg[fix_idx] = -1
								fix_abm_dist[fix_idx] = -1
								fix_abm_eta[fix_idx] = -1
								fix_abm_alt[fix_idx] = -1
								
							else
								--sel desired
								page_sel_wpt4 = 1
								fix_idx1 = fix_idx
								page_fix = 0
								act_page_old = act_page
								act_page = 1
							end
							entry = ""
						end
					end
				else
					add_fmc_msg(NOT_IN_DATABASE, 1)
				end
				
			end
		elseif page_legs == 1 then
			legs_1_lsk(1, 0, rte12_sel)
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			-- entry flaps
			if entry == "1" then
				flaps = " 1"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry == "5" then
				flaps = " 5"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry == "10" then
				flaps = "10"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry == "15" then
				flaps = "15"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry == "25" then
				flaps = "25"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry ~= "" then
				if entry == ">DELETE" then
					flaps = "**"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							verify_to_spd = 0
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							qrh = "OFF"
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
			display_update = 1
		elseif page_init == 1 then
			-- go to Ident page
			page_ident = 1
			page_init = 0
			display_update = 1
		elseif page_menu == 1 then
			-- go to Ident page
			page_ident = 1
			page_menu = 0
		elseif page_pos_init == 2 then
			if fmc_pos == "-----.-------.-" then
				entry = ""
			else
				entry = fmc_pos
			end
		elseif page_perf == 1 and B738DR_gw_entry == 0 then
			-- entry GW
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen == 0 then
				gw_mod = gw_calc
				zfw_mod = zfw_calc
				if units == 0 then
					gw_lbs_mod = gw_mod
					gw_kgs_mod = string.format("%5.1f", (tonumber(gw_mod) / KGS_LBS))
					zfw_lbs_mod = zfw_mod
					zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))
				else
					gw_kgs_mod = gw_mod
					gw_lbs_mod = string.format("%5.1f", (tonumber(gw_mod) * KGS_LBS))
					zfw_kgs_mod = zfw_mod
					zfw_lbs_mod = string.format("%5.1f", (tonumber(zfw_mod) * KGS_LBS))
				end
				if perf_exec < 2 then
					gw = gw_mod
					gw_lbs = gw_lbs_mod
					gw_kgs = gw_kgs_mod
					zfw = zfw_mod
					zfw_lbs = zfw_lbs_mod
					zfw_kgs = zfw_kgs_mod
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
							qrh = "OFF"
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
				else
					perf_exec = 3
				end
				entry = ""
			else
				if entry == ">DELETE" then
					if perf_exec < 2 then
						gw_mod = "***.*"
						gw_lbs_mod = gw_mod
						gw_kgs_mod = gw_mod
						zfw_mod = "***.*"
						zfw_kgs_mod = zfw_mod
						zfw_lbs_mod = zfw_mod
						
						gw = gw_mod
						gw_lbs = gw_lbs_mod
						gw_kgs = gw_kgs_mod
						zfw = zfw_mod
						zfw_lbs = zfw_lbs_mod
						zfw_kgs = zfw_kgs_mod
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry = ""
							else
								add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								verify_to_spd = 1
								qrh = "OFF"
								entry = ""
							end
						end
						B738DR_calc_vspd = 1
						B738DR_calc_trim = 1
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < weight_min or n > weight_max then	-- GW min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							gw_mod = string.format("%5.1f", n)
							if units == 0 then
								gw_lbs_mod = gw_mod
								gw_kgs_mod = string.format("%5.1f", (tonumber(gw_mod) / KGS_LBS))		-- to kgs
								local weight_lbs = (simDR_fuel_weight / 1000) * KGS_LBS		-- to lbs
								local xxx = tonumber(gw_lbs_mod)
								xxx = xxx - weight_lbs
								zfw_mod = string.format("%5.1f", (tonumber(xxx)))
								zfw_lbs_mod = zfw_mod
								zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))	-- to kgs
							else
								gw_kgs_mod = gw_mod
								gw_lbs_mod = string.format("%5.1f", (tonumber(gw_mod) * KGS_LBS))		-- to lbs
								local weight_lbs = (simDR_fuel_weight / 1000) * KGS_LBS		-- to lbs
								local xxx = tonumber(gw_lbs_mod)
								xxx = xxx - weight_lbs
								zfw_lbs_mod = string.format("%5.1f", (tonumber(xxx)))
								zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_lbs_mod) / KGS_LBS))	-- to kgs
								zfw_mod = zfw_kgs_mod
							end
							
							if perf_exec < 2 then
								gw = gw_mod
								gw_lbs = gw_lbs_mod
								gw_kgs = gw_kgs_mod
								zfw = zfw_mod
								zfw_lbs = zfw_lbs_mod
								zfw_kgs = zfw_kgs_mod
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if v1_set == "---" and vr_set == "---" and v2_set == "---" then
										entry = ""
									else
										add_fmc_msg(VERIFY_TO_SPEEDS, 1)
										verify_to_spd = 1
										qrh = "OFF"
										entry = ""
									end
								end
								B738DR_calc_vspd = 1
								B738DR_calc_trim = 1
							else
								perf_exec = 3
							end
							entry = ""
						end
					end
				end
			end
			B738_calc_vnav_spd()
			vnav_update = 1
		elseif page_approach == 1 then
			-- entry GW
			local strlen = string.len(entry)
			local n = tonumber(entry)
			local qqq = 0
			if strlen > 0 then
				if entry == ">DELETE" then
					entry = ""
					gw_app_nul = 0
					gw_app = gw
					gw_app_lbs = gw_lbs
					gw_app_kgs = gw_kgs
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < weight_min or n > weight_max then	-- GW min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							gw_app = string.format("%5.1f", n)
							if units == 0 then
								gw_app_lbs = gw_app
								gw_app_kgs = string.format("%5.1f", (tonumber(gw_app) / KGS_LBS))		-- to kgs
							else
								gw_app_kgs = gw_app
								gw_app_lbs = string.format("%5.1f", (tonumber(gw_app) * KGS_LBS))		-- to lbs
							end
							gw_app_nul = 1
							entry = ""
						end
					end
				end
			end
		elseif page_descent_forecast == 1 then
			-- entry Trans level
			local strlen = string.len(entry)
			local n = tonumber(entry)
			local nn = 0
			local n_str = ""
			if strlen > 0 then
				if entry == ">DELETE" then
					trans_lvl_mod = "-----"
					entry = ""
					forecast_exec = 1
				else
					if strlen == 3 or strlen == 2 then
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 30 or n > 410 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								trans_lvl_mod = "FL" .. string.format("%03d", n)
								entry = ""
								forecast_exec = 1
								nn = n * 100
								if forec_alt_1_mod ~= "-----" then
									n_str = string.sub(forec_alt_1_mod, 1, 2)
									if forec_alt_1_num_mod > nn then
										if n_str ~= "FL" then
											n = tonumber(forec_alt_1_mod) / 100
											forec_alt_1 = "FL" .. string.format("%03d", n)
										end
									else
										if n_str == "FL" then
											n_str = string.sub(forec_alt_1_mod, 3, 5)
											n = tonumber(n_str) * 100
											forec_alt_1_mod = string.format("%5d", n)
										end
									end
								end
								if forec_alt_2_mod ~= "-----" then
									n_str = string.sub(forec_alt_2_mod, 1, 2)
									if forec_alt_2_num_mod > nn then
										if n_str ~= "FL" then
											n = tonumber(forec_alt_2_mod) / 100
											forec_alt_2_mod = "FL" .. string.format("%03d", n)
										end
									else
										if n_str == "FL" then
											n_str = string.sub(forec_alt_2_mod, 3, 5)
											n = tonumber(n_str) * 100
											forec_alt_2_mod = string.format("%5d", n)
										end
									end
								end
								if forec_alt_3_mod ~= "-----" then
									n_str = string.sub(forec_alt_3_mod, 1, 2)
									if forec_alt_3_num_mod > nn then
										if n_str ~= "FL" then
											n = tonumber(forec_alt_3_mod) / 100
											forec_alt_3_mod = "FL" .. string.format("%03d", n)
										end
									else
										if n_str == "FL" then
											n_str = string.sub(forec_alt_3_mod, 3, 5)
											n = tonumber(n_str) * 100
											forec_alt_3_mod = string.format("%5d", n)
										end
									end
								end
							end
						end
					elseif strlen == 5 then
						if string.sub(entry, 1, 2) == "FL" then
							n = tonumber(string.sub(entry, 3, 5))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < 30 or n > 410 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									trans_lvl_mod = "FL" .. string.format("%03d", n)
									entry = ""
									forecast_exec = 1
									nn = n * 100
									if forec_alt_1_mod ~= "-----" then
										n_str = string.sub(forec_alt_1_mod, 1, 2)
										if forec_alt_1_num_mod > nn then
											if n_str ~= "FL" then
												n = tonumber(forec_alt_1_mod) / 100
												forec_alt_1_mod = "FL" .. string.format("%03d", n)
											end
										else
											if n_str == "FL" then
												n_str = string.sub(forec_alt_1_mod, 3, 5)
												n = tonumber(n_str) * 100
												forec_alt_1_mod = string.format("%5d", n)
											end
										end
									end
									if forec_alt_2_mod ~= "-----" then
										n_str = string.sub(forec_alt_2_mod, 1, 2)
										if forec_alt_2_num_mod > nn then
											if n_str ~= "FL" then
												n = tonumber(forec_alt_2_mod) / 100
												forec_alt_2_mod = "FL" .. string.format("%03d", n)
											end
										else
											if n_str == "FL" then
												n_str = string.sub(forec_alt_2_mod, 3, 5)
												n = tonumber(n_str) * 100
												forec_alt_2_mod = string.format("%5d", n)
											end
										end
									end
									if forec_alt_3_mod ~= "-----" then
										n_str = string.sub(forec_alt_3_mod, 1, 2)
										if forec_alt_3_num_mod > nn then
											if n_str ~= "FL" then
												n = tonumber(forec_alt_3_mod) / 100
												forec_alt_3_mod = "FL" .. string.format("%03d", n)
											end
										else
											if n_str == "FL" then
												n_str = string.sub(forec_alt_3_mod, 3, 5)
												n = tonumber(n_str) * 100
												forec_alt_3_mod = string.format("%5d", n)
											end
										end
									end
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			-- entry RW WIND
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					rw_wind_dir = "---"
					rw_wind_spd = "---"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						rw_wind_dir = string.format("%03d", q)
						rw_wind_spd = string.format("%3d", qq)
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry = ""
							else
								v1_set = "---"
								vr_set = "---"
								v2_set = "---"
								verify_to_spd = 0
								add_fmc_msg(TO_SPEEDS_DELETED, 1)
								entry = ""
							end
						end
						B738DR_calc_vspd = 1
					end
					
					-- if strlen > 4 and strlen < 8 and string.sub(entry, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- rw_wind_dir = wind_dir
										-- rw_wind_spd = string.format("%03d", n)
										-- if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											-- if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												-- entry = ""
											-- else
												-- -- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- -- verify_to_spd = 1
												-- v1_set = "---"
												-- vr_set = "---"
												-- v2_set = "---"
												-- verify_to_spd = 0
												-- add_fmc_msg(TO_SPEEDS_DELETED, 1)
												-- entry = ""
											-- end
										-- end
										-- B738DR_calc_vspd = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_perf == 2 then
			-- Time Error
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					time_err = "  "
					entry = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 5 or n > 30 then	-- Time error min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							time_err = string.format("%2d", n)
							entry = ""
						end
					end
				end
			end
			
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb < 2 then
				-- Change cruise alt
				local n = entry_altitude(entry)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					if crz_alt == "*****" then
						crz_alt_num = n
						if crz_alt_num >= B738DR_trans_alt_mod then
							n = n / 100
							crz_alt = "FL" .. string.format("%03d", n)
						else
							crz_alt = string.format("%5d", n)
						end
						B738DR_fmc_cruise_alt = crz_alt_num
						entry = ""
						msg_unavaible_crz_alt = 0
						msg_chk_alt_constr = 0
						if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
							add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
						end
					else
						crz_alt_num2 = n
						crz_alt_old = crz_alt
						crz_exec = 9
						exec1_light = 1
						if crz_alt_num2 >= B738DR_trans_alt_mod then
							n = n / 100
							crz_alt = "FL" .. string.format("%03d", n)
						else
							crz_alt = string.format("%5d", n)
						end
						entry = ""
					end
				end
			end
		elseif page_cruise == 1 then
			-- Change cruise alt
			local n = entry_altitude(entry)
			if n == 0 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				if crz_alt == "*****" then
					crz_alt_num = n
					if crz_alt_num >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					B738DR_fmc_cruise_alt = crz_alt_num
					entry = ""
					msg_unavaible_crz_alt = 0
					msg_chk_alt_constr = 0
					crz_alt_num2 = 0
					crz_alt_old = "     "
					crz_alt_changed = 1
					if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
						add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
					end
					altitude_last = elevation_ft
					B738DR_fms_descent_now = 0
				else
					crz_alt_num2 = n
					crz_alt_old = crz_alt
					crz_exec = 9
					exec1_light = 1
					if crz_alt_num2 >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					entry = ""
				end
			end
		elseif page_n1_limit == 1 then
			if in_flight_mode == 0 then
				-- entry SEL TEMP and OAT
				local strlen = string.len(entry)
				local n = 0
				if strlen == 0 then
					-- autofill OAT
					n = math.floor(simDR_OAT + 0.5)
					if n < 0 then
						oat = string.format("%4d", n)
					else
						if n < 10 then
							oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
						else
							oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
						end
					end
					n = (n * 9 / 5) + 32
					oat_f = string.format("%4d", n)
					oat_unit = "`C"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							verify_to_spd = 0
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							qrh = "OFF"
							entry = ""
						end
					end
					v1_set = "---"
					vr_set = "---"
					v2_set = "---"
					B738DR_calc_vspd = 1
				elseif strlen > 0 then
					if entry == ">DELETE" then
						sel_temp = "----"
						oat = "****"
						sel_temp_f = "----"
						oat_f = "****"
						oat_unit = "`C"
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry = ""
							else
								-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								-- verify_to_spd = 1
								add_fmc_msg(TO_SPEEDS_DELETED, 1)
								verify_to_spd = 0
								v1_set = "---"
								vr_set = "---"
								v2_set = "---"
								qrh = "OFF"
								entry = ""
							end
						else
							entry = ""
						end
						B738DR_calc_vspd = 1
						if to_2 == "<ACT>" then
							clb = "     "
							clb_1 = "<SEL>"
							clb_2 = "     "
						else
							clb = "<SEL>"
							clb_1 = "     "
							clb_2 = "     "
						end
					else
						if strlen > 0 and strlen < 6 then
							local oat_set = 0
							if string.sub(entry, 1, 1) == "/" and strlen > 1 then
								oat_set = 1		-- entry OAT
								entry = string.sub(entry, 2, strlen)
								strlen = strlen -1
							end
							
							local n_str = string.sub(entry, strlen, strlen)
							if n_str == "C" then 
								n = tonumber(string.sub(entry, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
											--set CLB thrust
											clb = "     "
											clb_1 = "     "
											clb_2 = "<SEL>"
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							elseif n_str == "F" then
								n = tonumber(string.sub(entry, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 50 or n > 167) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 140) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`F"
										if oat_set == 0 then
											sel_temp_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											clb = "     "
											clb_1 = "     "
											clb_2 = "<SEL>"
										else
											oat_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							else
								n = tonumber(string.sub(entry, 1, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
											clb = "     "
											clb_1 = "     "
											clb_2 = "<SEL>"
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							end
							
							
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				-- select AUTO
				auto_act = "<ACT>"
				ga_act = "     "
				con_act = "     "
				clb_act = "     "
				crz_act = "     "
			end
		elseif page_ref_nav_data == 1 then
			local strlen = string.len(entry)
			if strlen == 0 or strlen > 5 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				nav_data_type = 0 -- wpt
				nav_data_find(entry, 0)
			end
		elseif page_ref_nav_data_apt == 1 then
			local nd_x = 0
			local idx_rec = 0
			local ii = 0
			local entry_len = string.len(entry)
			if entry_len > 0 then
				if entry_len > 3 then
					if string.sub(entry, 1, 2) == "RW" then
						entry = string.sub(entry, 3, -1)
						if rnw_data_num > 0 and string.len(ref_nav_apt) > 0 then
							nd_x = string.byte(string.sub(ref_nav_apt, 1, 1))
							if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
								if idx_rnw[nd_x][99999] > 0 then
									for ii = 1, idx_rnw[nd_x][99999] do
										idx_rec = idx_rnw[nd_x][ii]
										if ref_nav_apt == rnw_data[idx_rec][1] and entry == rnw_data[idx_rec][2] then
											ref_nav_rnw = "RW" .. entry
											ref_elevation = 0
											ref_nav_rnw_length = rnw_data[idx_rec][7]
											entry = ""
										end
									end
								end
							end
						end
					end
				end
				if entry ~= "" then
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_ac == 1 then
			-- go to ACARS ATIS
			page_ac_atis = 1
			page_ac = 0
			display_update = 1
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				entry = nearest_apt_list_icao[1]
				if entry == "----" then
					entry = ""
				end
			else
				entry = nearest_apt_list_icao[act_page - 1]
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				add_altn_apt(entry, 1, 0)
			else
				add_altn_apt(entry, act_page - 1, 0)
			end
		elseif page_dl_requests == 1 then
			page_dl_requests = 0
			page_dl_wxr_req = 1
			act_page = 1
			display_update = 1
		elseif page_flt_plns == 1 then
			entry = sel_flt_plns[1]
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 2LSK
function B738_fmc1_2L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if page_dep == 1 then
			if rte12_sel == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel[2] ~= "" then
						if ref_sid2 == ref_sid_sel[2] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid2 = ref_sid_sel[2]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel[2] ~= "" then
							if ref_sid_tns2 == ref_tns_sel[2] then
								if sid_tns_list_num == 1 then
									ref_sid_tns2 = "------"
									sel_ref_sid_tns2 = ref_sid_tns2
									act_page = 1
									ref_tns_exec = 1
								else
									sel_ref_sid_tns2 = ref_sid_tns2
								end
							else
								ref_sid_tns2 = ref_tns_sel[2]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page = 1
						end
					else
						ref_sid_tns2 = "------"
						sel_ref_sid_tns2 = ref_sid_tns2
						act_page = 1
						ref_tns_exec = 1
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel[2] ~= "" then
						if ref_sid8 == ref_sid_sel[2] then
							sel_ref_sid8 = ref_sid2
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid8 = ref_sid_sel[2]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel[2] ~= "" then
							if ref_sid_tns8 == ref_tns_sel[2] then
								if sid_tns_list_num == 1 then
									ref_sid_tns8 = "------"
									sel_ref_sid_tns8 = ref_sid_tns8
									act_page = 1
									ref_tns_exec = 1
								else
									sel_ref_sid_tns8 = ref_sid_tns8
								end
							else
								ref_sid_tns8 = ref_tns_sel[2]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page = 1
						end
					else
						ref_sid_tns8 = "------"
						sel_ref_sid_tns8 = ref_sid_tns8
						act_page = 1
						ref_tns_exec = 1
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel[2] ~= "" then
						if des_star2 == des_star_sel[2] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star2 = des_star_sel[2]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel[2] ~= "" then
							if des_star_trans2 == des_star_tns_sel[2] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel[2]
								sel_des_star_trans2 = des_star_trans2
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					else
						des_star_trans2 = "------"
						sel_des_star_trans2 = des_star_trans2
						act_page = 1
						des_star_tns_exec = 1
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel[2] ~= "" then
						if des_star8 == des_star_sel[2] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star8 = des_star_sel[2]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel[2] ~= "" then
							if des_star_trans8 == des_star_tns_sel[2] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans2 = des_star_tns_sel[2]
								sel_des_star_trans8 = des_star_trans8
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					else
						des_star_trans8 = "------"
						sel_des_star_trans8 = des_star_trans8
						act_page = 1
						des_star_tns_exec = 1
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_sel_wpt == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry, 0, rte12_sel)
			end
		elseif page_sel_wpt3 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 0)
			end
		elseif page_sel_hold_wpt == 1 then
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 0, rte12_sel)
			end
		elseif page_ref_sel == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				--ref_nav_wpt = nav_data_entry
				ref_nav_new = "*****"
				page_ref_nav_data = 1
				page_ref_sel = 0
			end
		elseif page_fix == 1 then
			local ii = 0
			local jj = string.len(entry)
			local kk = 0
			local radial_temp = 0
			local data_ok = 0
			if act_page <= fix_data_num then
				if jj == 0 then
					-- copy wpt to clipboard
					if fix_data[act_page][6] ~= -1 and fix_data[act_page][7] ~= -1 then
						entry = fix_data[act_page][1] .. string.format("%05.1f", fix_data[act_page][6]) .. "/"
						if fix_data[act_page][7] < 10 then
							entry = entry .. string.format("%3.1f", fix_data[act_page][7])
						elseif fix_data[act_page][7] < 100 then
							entry = entry .. string.format("%4.1f", fix_data[act_page][7])
						else
							entry = entry .. string.format("%5.1f", fix_data[act_page][7])
						end
					elseif fix_data[act_page][12] > 0 then
						if fix_data[act_page][6] ~= -1 then
							entry = fix_data[act_page][1] .. string.format("%05.1f", fix_data[act_page][6]) .. "/"
							if fix_data[act_page][16] < 10 then
								entry = entry .. string.format("%3.1f", fix_data[act_page][16])
							elseif fix_data[act_page][16] < 100 then
								entry = entry .. string.format("%4.1f", fix_data[act_page][16])
							else
								entry = entry .. string.format("%5.1f", fix_data[act_page][16])
							end
						elseif fix_data[act_page][7] ~= -1 then
							--
						end
					end
				elseif entry == ">DELETE" then
					if fix_data[act_page][6] ~= -1 or fix_data[act_page][7] ~= -1 then
						fix_data[act_page][6] = -1
						fix_data[act_page][7] = -1
						fix_data[act_page][12] = -1
						entry = ""
					end
				elseif jj > 1 then
					if string.sub(entry, 1, 1) == "/" then
						-- distance
						ii = tonumber(string.sub(entry, 2, -1))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii <= 0 or ii > 511 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fix_data[act_page][7] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page][6] = -1
								fix_data[act_page][12] = -1
								entry = ""
							end
						end
					elseif string.sub(entry, -1, -1) == "/" then
						-- radial
						ii = tonumber(string.sub(entry, 1, -2))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii < 0 or ii > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								ii = ii % 360
								fix_data[act_page][6] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page][7] = -1
								fix_data[act_page][12] = -1
								entry = ""
							end
						end
					else
						--fixsk
						kk,ii = string.find(entry, "/")
						if kk ~= nil and kk > 1 and kk < jj then
							radial_temp = tonumber(string.sub(entry, 1, kk - 1))
							if radial_temp == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									data_ok = 1
								end
								if data_ok == 1 then
									ii = tonumber(string.sub(entry, kk + 1, -1))
									if ii == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if ii <= 0 or ii > 511 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fix_data[act_page][6] = tonumber(string.format("%5.1f", radial_temp))
											fix_data[act_page][7] = tonumber(string.format("%5.1f", ii))
											fix_data[act_page][12] = -1
											entry = ""
										end
									end
								end
							end
						else
							radial_temp = tonumber(entry)
							if jj == 3 and radial_temp ~= nil then
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									fix_data[act_page][6] = tonumber(string.format("%5.1f", radial_temp))
									fix_data[act_page][7] = -1
									fix_data[act_page][12] = -1
									entry = ""
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs == 1 then
			-- 2 LSK
			legs_x_lsk(2, 0, rte12_sel)
		elseif page_pos_init == 1 and in_flight_mode == 0 then	-- only on the ground
			-- entry Ref airport ICAO
			local apt_ok = 0
			local xy = 0
			if entry == ">DELETE" then
				entry = INVALID_DELETE
			else
				if string.len(entry) == 4 then
					
					if apt_exist(entry) == true then
					
					if cust_def_data == 1 then
						file_name = "Custom Data/CIFP/" .. entry
						file_name = file_name .. ".dat"
						file_navdata = io.open(file_name, "r")
						if file_navdata == nil then
							if apt_exist(entry) == true then
								apt_ok = 1
							end
						else
							file_navdata:close()
							apt_ok = 1
						end
					elseif cust_def_data == 2 then
						file_name = "Resources/default data/CIFP/" .. entry
						file_name = file_name .. ".dat"
						file_navdata = io.open(file_name, "r")
						if file_navdata == nil then
							if apt_exist(entry) == true then
								apt_ok = 1
							end
						else
							file_navdata:close()
							apt_ok = 1
						end
					end
					
					end
					
					if apt_ok == 0 then
						add_fmc_msg(NOT_IN_DATABASE, 1)
					else
						ref_icao2 = entry
						airport_pos2(rte12_sel)
						entry = ""
					end
				elseif entry == "" and ref_icao2 ~= "----" then
					entry = ref_icao2
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_rte_init == 1 then
			if act_page == 1 then
				-- entry CO ROUTE
				local ii = 0
				if (save_rte1 ~= 0 and rte12_sel == 0) or (save_rte2 ~= 0 and rte12_sel == 1) then
					ii = string.len(entry)
					if ii > 0 and ii < 13 then
						if rte12_sel == 0 then
							co_route_save1 = entry
						else
							co_route_save2 = entry
						end
						entry = ""
					else
						add_fmc_msg(NOT_IN_DATABASE, 1)
					end
				elseif entry == ">DELETE" then
					if rte12_sel == 0 then
						co_route_x = "------------"
						co_route = co_route_x
					else
						co_route7_x = "------------"
						co_route7 = co_route7_x
					end
					entry = ""
					if rte12_sel == 0 then
						wind_data_clb_num = 0
						wind_data_toc_num = 0
						wind_data_tod_num = 0
						wind_data_des_num = 0
					else
						wind_data_clb_num7 = 0
						wind_data_toc_num7 = 0
						wind_data_tod_num7 = 0
						wind_data_des_num7 = 0
					end
				else
					ii = string.len(entry)
					if ii > 0 and ii < 13 then
						ii = load_fpln(entry, rte12_sel)
						if ii ~= 0 then
							entry = ""
						end
					else
						add_fmc_msg(NOT_IN_DATABASE, 1)
					end
				end
			else
				-- button 2 LSK
				rte_x_lsk(2, 0, rte12_sel)
			end
		elseif page_ref_nav_data == 1 then
			local strlen = string.len(entry)
			if strlen ~= 4 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				nav_data_type = 2 -- apt
				nav_data_find(entry, 0)
			end
		elseif page_init == 1 then
			-- go to Pos init page
			page_init = 0
			page_pos_init = 1
			display_update = 1
		elseif page_pos_init == 2 then
			if B738DR_irs_pos == "-----.-------.-" then
				entry = ""
			else
				entry = B738DR_irs_pos
			end
		elseif page_perf == 1 then
			-- entry Plan weight
			if B738DR_plan_fuel ~= 0 then
				if simDR_eng_N2[0] < 50 and simDR_eng_N2[1] < 50 and was_on_air == 0 then
					local strlen = string.len(entry)
					local n = tonumber(entry)
					local m = 20.8	--23.8
					local mm = 45.7	--52.3
					if B738DR_b737_variant == 4 then
						m = 23.8
						mm = 52.3
					end
					
					if strlen > 0 then
						if entry == ">DELETE" then
							plan_weight_mod = "---.-"
							plan_weight_lbs_mod = plan_weight_mod
							plan_weight_kgs_mod = plan_weight_mod
							
							if perf_exec < 2 then
								plan_weight = plan_weight_mod
								plan_weight_kgs = plan_weight_kgs_mod
								plan_weight_lbs = plan_weight_lbs_mod
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if v1_set == "---" and vr_set == "---" and v2_set == "---" then
										entry = ""
									else
										add_fmc_msg(VERIFY_TO_SPEEDS, 1)
										verify_to_spd = 1
										qrh = "OFF"
										entry = ""
									end
								end
							else
								perf_exec = 3
								entry = ""
							end
						else
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if units == 0 then
									if n < 0 or n > mm then	-- Plan min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										plan_weight_mod = string.format("%5.1f", n)
										plan_weight_lbs_mod = plan_weight_mod
										plan_weight_kgs_mod = string.format("%5.1f", (tonumber(plan_weight_mod) / KGS_LBS))
										if perf_exec < 2 then
											plan_weight = plan_weight_mod
											plan_weight_kgs = plan_weight_kgs_mod
											plan_weight_lbs = plan_weight_lbs_mod
											if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
												if v1_set == "---" and vr_set == "---" and v2_set == "---" then
													entry = ""
												else
													add_fmc_msg(VERIFY_TO_SPEEDS, 1)
													verify_to_spd = 1
													qrh = "OFF"
													entry = ""
												end
											end
										else
											perf_exec = 3
											entry = ""
										end
									end
								else
									if n < 0 or n > m then	-- Plan min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										plan_weight_mod = string.format("%5.1f", n)
										plan_weight_kgs_mod = plan_weight_mod
										plan_weight_lbs_mod = string.format("%5.1f", (tonumber(plan_weight_mod) * KGS_LBS))
										if perf_exec < 2 then
											plan_weight = plan_weight_mod
											plan_weight_kgs = plan_weight_kgs_mod
											plan_weight_lbs = plan_weight_lbs_mod
											if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
												if v1_set == "---" and vr_set == "---" and v2_set == "---" then
													entry = ""
												else
													add_fmc_msg(VERIFY_TO_SPEEDS, 1)
													verify_to_spd = 1
													qrh = "OFF"
													entry = ""
												end
											end
										else
											perf_exec = 3
											entry = ""
										end
									end
								end
							end
						end
					end
				end
			end
		elseif page_n1_limit == 1 then
			if in_flight_mode == 0 then
				--if B738DR_thr_takeoff_N1 ~= 0 then
				if B738DR_fmc_oat_temp ~= 99 then
					-- select TO
					to = "<ACT>"
					to_1 = "     "
					to_2 = "     "
					if sel_temp == "----" then
					--if sel_clb_thr == 0 then
						clb = "<SEL>"
						clb_1 = "     "
						clb_2 = "     "
					else
						clb = "     "
						clb_1 = "     "
						clb_2 = "<SEL>"
					end
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
				end
			else
				-- select GA
				auto_act = "     "
				ga_act = "<ACT>"
				con_act = "     "
				clb_act = "     "
				crz_act = "     "
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			-- entry RW SLOPE / HDG
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					rw_slope = "--.-"
					rw_hdg = "---"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
				else
					if strlen == 4 then
						if string.sub(entry, 1, 1) == "/" then
							local n = tonumber(string.sub(entry, 2, 4))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < 0 or n > 360 then	-- HDG min and max
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n = n % 360
									rw_hdg = string.format("%03d", n)
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										if v1_set == "---" and vr_set == "---" and v2_set == "---" then
											entry = ""
										else
											-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
											-- verify_to_spd = 1
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
											entry = ""
										end
									end
									B738DR_calc_vspd = 1
								end
							end
						elseif string.sub(entry, 3, 3) == "." then
							local up_down = string.sub(entry, 1, 1)
							if up_down == "D" or up_down == "U" then
								local n = tonumber(string.sub(entry, 2, 4))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									rw_slope = up_down .. string.format("%03.1f", n)
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										if v1_set == "---" and vr_set == "---" and v2_set == "---" then
											entry = ""
										else
											-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
											-- verify_to_spd = 1
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
											entry = ""
										end
									end
									B738DR_calc_vspd = 1
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					elseif strlen == 8 then
						if string.sub(entry, 3, 3) == "." and string.sub(entry, 5, 5) == "/" then
							local up_down = string.sub(entry, 1, 1)
							if up_down == "D" or up_down == "U" then
								local n = tonumber(string.sub(entry, 2, 4))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									local rw_slope_temp = up_down .. string.format("%03.1f", n)
									
									-- HDG
									n = tonumber(string.sub(entry, 6, 8))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if n < 0 or n > 360 then	-- HDG min and max
											add_fmc_msg(INVALID_INPUT, 1)
										else
											rw_slope = rw_slope_temp
											n = n % 360
											rw_hdg = string.format("%03d", n)
											if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
												if v1_set == "---" and vr_set == "---" and v2_set == "---" then
													entry = ""
												else
													-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
													-- verify_to_spd = 1
													v1_set = "---"
													vr_set = "---"
													v2_set = "---"
													verify_to_spd = 0
													add_fmc_msg(TO_SPEEDS_DELETED, 1)
													entry = ""
												end
											end
											B738DR_calc_vspd = 1
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_descent == 1 then	--and B738DR_flight_phase < 5 then
			-- DES speed kts/mach
			local strlen = string.len(entry)
			local n = 0
			local nn = 0
			local nnn = 0
			local n2 = 0
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					entry = ""
				else
					if strlen == 4 and string.sub(entry, 1, 1) == "/" then		-- only kts
						n = tonumber(string.sub(entry, 2, -1))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_kts)
							nnn = tonumber(des_min_kts)
							if nn == nil then
								nn = 340
							end
							if nnn == nil then
								nnn = 100		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								entry = ""
								fmc_descent_speed_mod = n
								fmc_descent_mode_mod = 2
								des_spd_exec = 1
							end
						end
					elseif strlen > 1 and strlen < 5 and string.sub(entry, 1, 1) == "." then		-- only mach
						n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_mach)
							nnn = tonumber(des_min_mach)
							if nn == nil then
								nn = 0.820		-- max
							else
								nn = nn / 1000
							end
							if nnn == nil then
								nnn = 0.400		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fmc_descent_speed_mach_mod = n
								entry = ""
								fmc_descent_mode_mod = 2
								des_spd_exec = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_kts)
							nnn = tonumber(des_min_kts)
							if nn == nil then
								nn = 340
							end
							if nnn == nil then
								nnn = 100		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								des_spd_exec = 1
								entry = ""
								fmc_descent_speed_mod = n
								fmc_descent_mode_mod = 2
							end
						end
					elseif strlen == 5 and string.sub(entry, 1, 1) == "." and string.sub(entry, -1, -1) == "/" then	-- only mach
						n = tonumber(string.sub(entry, 1, -2))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_mach)
							nnn = tonumber(des_min_mach)
							if nn == nil then
								nn = 0.820		-- max
							else
								nn = nn / 1000
							end
							if nnn == nil then
								nnn = 0.400		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fmc_descent_speed_mach_mod = n
								entry = ""
								des_spd_exec = 1
								fmc_descent_mode_mod = 2
							end
						end
					elseif strlen > 5 then		-- mach/kts .xxx/xxx, .xx/xxx, .x/xxx
						if string.sub(entry, -4, -4) == "/" then
							n = tonumber(string.sub(entry, -3, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_kts)
								nnn = tonumber(des_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n2 = n
									n = tonumber(string.sub(entry, 1, -5))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(des_max_mach)
										nnn = tonumber(des_min_mach)
										if nn == nil then
											nn = 0.820		-- max
										else
											nn = nn / 1000
										end
										if nnn == nil then
											nnn = 0.400		-- min
										end
										if n < nnn or n > nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fmc_descent_speed_mach_mod = n
											fmc_descent_speed_mod = n2
											des_spd_exec = 1
											fmc_descent_mode_mod = 2
											entry = ""
										end
									end
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb < 2 then
				-- CLB speed kts/mach
				local strlen = string.len(entry)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry == ">DELETE" then
						entry = ""
					else
						if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
							n = tonumber(string.sub(entry, 2, strlen))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mach_mod = n
									entry = ""
									clb_spd_exec = 1
									fmc_climb_mode_mod = 3
								end
							end
						elseif strlen > 1 and strlen < 5 and string.sub(entry, 1, 1) == "." then		-- only mach
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mach_mod = n
									entry = ""
									clb_spd_exec = 1
									fmc_climb_mode_mod = 3
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mod = n
									entry = ""
									clb_spd_exec = 1
									fmc_climb_mode_mod = 3
								end
							end
						elseif strlen == 4 and string.sub(entry, -1, -1) == "/" then			-- only kts
							n = tonumber(string.sub(entry, 1, -2))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mod = n
									entry = ""
									clb_spd_exec = 1
									fmc_climb_mode_mod = 3
								end
							end
						elseif strlen > 5 then		-- kts/mach xxx/.xxx
							if string.sub(entry, 4, 5) == "/." then
								n = tonumber(string.sub(entry, 1, 3))
								if n == nil then
								else
									nn = tonumber(clb_max_kts)
									nnn = tonumber(clb_min_kts)
									if nn == nil then
										nn = 340
									end
									if nnn == nil then
										nnn = 100		-- min
									end
									if n < nnn or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n2 = n
										n = tonumber(string.sub(entry, 5, -1))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											nn = tonumber(clb_max_mach)
											nnn = tonumber(clb_min_mach)
											if nn == nil then
												nn = 0.820		-- max
											else
												nn = nn / 1000
											end
											if nnn == nil then
												nnn = 0.400		-- min
											end
											if n < nnn or n > nn then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												clb_spd_exec = 1
												fmc_climb_mode_mod = 3
												fmc_climb_speed_mach_mod = n
												fmc_climb_speed_mod = n2
												entry = ""
											end
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
		elseif page_cruise == 1 then
			-- CRZ speed kts/mach
			if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 or (crz_exec == 0 and B738DR_flight_phase == 2) then
				
				local strlen = string.len(entry)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry == ">DELETE" then
						entry = ""
					else
						if strlen > 1 and  strlen < 5 and string.sub(entry, 1, 1) == "." then		-- only mach
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(crz_max_mach)
								nnn = tonumber(crz_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								else
									nnn = nnn / 1000
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_cruise_speed_mach_mod = n
									fmc_cruise_speed_mod = 0
									entry = ""
									crz_spd_exec = 1
									fmc_cruise_mode_mod = 2
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(crz_max_kts)
								nnn = tonumber(crz_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_cruise_speed_mod = n
									fmc_cruise_speed_mach_mod = 0
									entry = ""
									crz_spd_exec = 1
									fmc_cruise_mode_mod = 2
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			elseif B738DR_flight_phase == 3 then
				-- CRZ CLB speed kts/mach
				local strlen = string.len(entry)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry == ">DELETE" then
						entry = ""
					else
						if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
							n = tonumber(string.sub(entry, 2, strlen))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mach_mod = n
									entry = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen > 1 and strlen < 5 and string.sub(entry, 1, 1) == "." then		-- only mach
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mach_mod = n
									entry = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mod = n
									entry = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen == 4 and string.sub(entry, -1, -1) == "/" then			-- only kts
							n = tonumber(string.sub(entry, 1, -2))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mod = n
									entry = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen > 5 then		-- kts/mach xxx/.xxx
							if string.sub(entry, 4, 5) == "/." then
								n = tonumber(string.sub(entry, 1, 3))
								if n == nil then
								else
									nn = tonumber(clb_max_kts)
									nnn = tonumber(clb_min_kts)
									if nn == nil then
										nn = 340
									end
									if nnn == nil then
										nnn = 100		-- min
									end
									if n < nnn or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n2 = n
										n = tonumber(string.sub(entry, 5, -1))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											nn = tonumber(clb_max_mach)
											nnn = tonumber(clb_min_mach)
											if nn == nil then
												nn = 0.820		-- max
											else
												nn = nn / 1000
											end
											if nnn == nil then
												nnn = 0.400		-- min
											end
											if n < nnn or n > nn then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												crz_spd_exec = 1
												fmc_climb_speed_mach_mod = n
												fmc_climb_speed_mod = n2
												entry = ""
											end
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			elseif B738DR_flight_phase == 4 then
				-- DES speed kts/mach
				local strlen = string.len(entry)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry == ">DELETE" then
						entry = ""
					else
						if strlen == 4 and string.sub(entry, 1, 1) == "/" then		-- only kts
							n = tonumber(string.sub(entry, 2, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_kts)
								nnn = tonumber(des_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									entry = ""
									fmc_descent_speed_mod = n
									crz_spd_exec = 1
								end
							end
						elseif strlen > 1 and strlen < 5 and string.sub(entry, 1, 1) == "." then		-- only mach
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_mach)
								nnn = tonumber(des_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_descent_speed_mach_mod = n
									entry = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_kts)
								nnn = tonumber(des_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									crz_spd_exec = 1
									entry = ""
									fmc_descent_speed_mod = n
								end
							end
						elseif strlen == 5 and string.sub(entry, 1, 1) == "." and string.sub(entry, -1, -1) == "/" then	-- only mach
							n = tonumber(string.sub(entry, 1, -2))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_mach)
								nnn = tonumber(des_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_descent_speed_mach_mod = n
									entry = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen > 5 then		-- mach/kts .xxx/xxx, .xx/xxx, .x/xxx
							if string.sub(entry, -4, -4) == "/" then
								n = tonumber(string.sub(entry, -3, -1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nn = tonumber(des_max_kts)
									nnn = tonumber(des_min_kts)
									if nn == nil then
										nn = 340
									end
									if nnn == nil then
										nnn = 100		-- min
									end
									if n < nnn or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n2 = n
										n = tonumber(string.sub(entry, 1, -5))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											nn = tonumber(des_max_mach)
											nnn = tonumber(des_min_mach)
											if nn == nil then
												nn = 0.820		-- max
											else
												nn = nn / 1000
											end
											if nnn == nil then
												nnn = 0.400		-- min
											end
											if n < nnn or n > nn then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												fmc_descent_speed_mach_mod = n
												fmc_descent_speed_mod = n2
												crz_spd_exec = 1
												entry = ""
											end
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
		elseif page_perf == 2 then
			-- CLB min speed kts/mach
			local strlen = string.len(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					clb_min_kts = "   "
					clb_min_mach = "   "
					entry = ""
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_max_mach)
							if nn == nil then
								nn = 0.82
							else
								nn = nn / 1000
							end
							if n < 0.4 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								clb_min_mach = string.format("%03d", n)
								entry = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_min_kts = string.format("%03d", n)
								B738_calc_vnav_spd()
								vnav_update = 1
								entry = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(clb_max_mach)
										if nn == nil then
											nn = 0.82
										else
											nn = nn / 1000
										end
										if n < 0.4 or n > nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											clb_min_kts = kts
											clb_min_mach = string.format("%03d", n)
											entry = ""
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		-----------
		elseif page_offset == 1 and legs_num > 1 and legs_intdir == 0 then
			local strlen = string.len(entry)
			local n = 0
			
			if entry == ">DELETE" then
				if offset_dist_mod == 0 then
					entry = INVALID_DELETE
				else
					entry = ""
					if offset_act >= 5 and offset_act <= 7 then
						offset_act_mod = 4
						offset_dist_mod = 0
					else	--if offset_act == 0 then
						offset_act_mod = 0
						offset_dist_mod = 0
						offset_side_mod = 0
						offset_start_mod = 0
						offset_end_mod = 0
						offset_start_def_mod = 0
						offset_end_def_mod = 0
						-- legs_off_tmp_n = 0
						delete_offset_legs2(rte12_act)
					end
				end
			elseif entry == "0" then
					entry = ""
					if offset_act >= 5 and offset_act <= 7 then
						offset_act_mod = 4
						offset_dist_mod = 0
					else	--if offset_act == 0 then
						offset_act_mod = 0
						offset_dist_mod = 0
						offset_side_mod = 0
						offset_start_mod = 0
						offset_end_mod = 0
						offset_start_def_mod = 0
						offset_end_def_mod = 0
						-- legs_off_tmp_n = 0
						delete_offset_legs2(rte12_act)
					end
			elseif strlen > 1 and strlen < 6 then
				if string.sub(entry, 1, 1) == "L" then
					offset_side_mod = 1
					n = tonumber(string.sub(entry, 2, -1))
				elseif string.sub(entry, -1, -1) == "L" then
					offset_side_mod = 1
					n = tonumber(string.sub(entry, 1, -2))
				elseif string.sub(entry, 1, 1) == "R" then
					offset_side_mod = 2
					n = tonumber(string.sub(entry, 2, -1))
				elseif string.sub(entry, -1, -1) == "R" then
					offset_side_mod = 2
					n = tonumber(string.sub(entry, 1, -2))
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
				if offset_side_mod ~= 0 then
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
						offset_side_mod = 0
					else
						if n > 0 and n < 50 then
							offset_dist_mod = n
							entry = ""
							offset_act_mod = 1
							offset_check(rte12_act)
							if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
								if offset_create(rte12_act) == true then
									-- if offset_start_mod == 0 then
										-- offset_act_mod = 3
									-- else
										-- offset_act_mod = 2
									-- end
									
									if rte12_act == 0 then
										if offset_start_mod == 0 then
											if legs_data2[offset][45] ~= 0 then
												offset_act_mod = 3
											else
												offset_act_mod = 1
											end
										else
											offset_act_mod = 2
										end
									else
										if offset_start_mod == 0 then
											if legs_data8[offset][45] ~= 0 then
												offset_act_mod = 3
											else
												offset_act_mod = 1
											end
										else
											offset_act_mod = 2
										end
									end
								end
							else
								add_fmc_msg(UNABLE_OFFSET, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
							offset_side_mod = 0
						end
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_menu == 1 then
			-- go to ACARS
			-- page_ac = 1
			page_dl_menu = 1
			page_menu = 0
			act_page = 1
			display_update = 1
		elseif page_ac_atis == 1 then
			-- entry ICAO ATIS
			if entry == ">DELETE" then
				ac_atis_icao = "----"
				entry = ""
			elseif entry == "" and ac_atis_icao ~= "----" then
				entry = ac_atis_icao
			elseif string.len(entry) == 4 then
				ac_atis_icao = entry
				entry = ""
				-- atis_msg_status = 0
				-- atis_send_time = "     "
			end
		elseif page_descent_forecast == 1 then
			-- entry Cabin Rate
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					cabin_rate_mod = "---"
					entry = ""
					forecast_exec = 1
				elseif strlen == 3 then
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 100 or n > 999 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							cabin_rate_mod = string.format("%3d", n)
							entry = ""
							forecast_exec = 1
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				entry = nearest_apt_list_icao[2]
				if entry == "----" then
					entry = ""
				end
			else
				if nearest_apt_list_via[act_page - 1] == 0 then
					if entry == ">DELETE" then
						nearest_apt_list_dist[act_page - 1] = 0
						entry = ""
					else
						local nnn = tonumber(entry)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_dist[act_page - 1] = nnn
							entry = ""
						end
					end
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				add_altn_apt(entry, 2, 0)
			else
				if altern_apt_list_via[act_page - 1] == 0 then
					if entry == ">DELETE" then
						altern_apt_list_dist[act_page - 1] = 0
						entry = ""
					else
					local nnn = tonumber(entry)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_dist[act_page - 1] = nnn
							entry = ""
						end
					end
				end
			end
		elseif page_hold == 1 then
			local strlen = string.len(entry)
			local m = 0
			local n = 0
			if entry == ">DELETE" then
				add_fmc_msg(INVALID_INPUT, 1)
			elseif strlen == 3 then
				-- format XXX
				n = tonumber(entry)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
				elseif n >= 0 and n <= 360 then
					m = (n + 180) % 360
					
					-- save data
					if rte12_sel == 0 then
						new_hold_inbd = string.format("%03d", m)
						if new_hold_inbd == "---" then
							legs_data2[hold_offset_idx][29] = "000"
						else
							legs_data2[hold_offset_idx][29] = tostring(tonumber(new_hold_inbd) * 10)
						end
						if rte12_act == rte12_sel then
							hold_exec = 1
						end
					else
						new_hold_inbd2 = string.format("%03d", m)
						if new_hold_inbd2 == "---" then
							legs_data8[hold_offset_idx7][29] = "000"
						else
							legs_data8[hold_offset_idx7][29] = tostring(tonumber(new_hold_inbd2) * 10)
						end
						if rte12_act == rte12_sel then
							hold_exec2 = 1
						end
					end
					entry = ""
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_flt_plns == 1 then
			entry = sel_flt_plns[2]
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 3LSK
function B738_fmc1_3L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep == 1 then
			if rte12_sel == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel[3] ~= "" then
						if ref_sid2 == ref_sid_sel[3] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid2 = ref_sid_sel[3]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel[3] ~= "" then
							if ref_sid_tns2 == ref_tns_sel[3] then
								sel_ref_sid_tns2 = ref_sid_tns2
							else
								ref_sid_tns2 = ref_tns_sel[3]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel[3] ~= "" then
						if ref_sid8 == ref_sid_sel[3] then
							sel_ref_sid8 = ref_sid8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid8 = ref_sid_sel[3]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel[3] ~= "" then
							if ref_sid_tns8 == ref_tns_sel[3] then
								sel_ref_sid_tns8 = ref_sid_tns8
							else
								ref_sid_tns8 = ref_tns_sel[3]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel[3] ~= "" then
						if des_star2 == des_star_sel[3] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star2 = des_star_sel[3]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel[3] ~= "" then
							if des_star_trans2 == des_star_tns_sel[3] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel[3]
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel[3] ~= "" then
						if des_star8 == des_star_sel[3] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star8 = des_star_sel[3]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel[3] ~= "" then
							if des_star_trans8 == des_star_tns_sel[3] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans8 = des_star_tns_sel[3]
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_dep_arr == 1 then
			-- local dep_arr_enable = false
			-- -- if legs_num8 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 1 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			-- if legs_num8 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
				-- if in_flight_mode == 0 and legs_data8[1][17] == 4 then
					-- dep_arr_enable = true
				-- elseif legs_data8[1][17] == 0 then
					-- dep_arr_enable = true
				-- end
			-- end
			-- if dep_arr_enable == true then
			if legs_num8 > 0 then
				-- SID departures
				if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and ref_icao7_x ~= "ORGN" then
					rte12_sel = 1
					page_dep = 1
					page_dep_arr = 0
					arr_data7_x = 0
					find_ref_rnw_data(1, 1)
					read_ref_list_icao(ref_icao7_x, 1)
					
					sel_ref_rwy8 = "-----"
					sel_ref_sid8 = "------"
					sel_ref_sid_tns8 = "------"
					
					create_rnw_list(1)
					create_sid_list(1)
					create_sid_tns_list(1)
				end
			end
		elseif page_sel_wpt == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry, 0, rte12_sel)
			end
		elseif page_sel_wpt3 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 0)
			end
		elseif page_sel_hold_wpt == 1 then
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 0, rte12_sel)
			end
		elseif page_ref_sel == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				ref_nav_new = "*****"
				page_ref_nav_data = 1
				page_ref_sel = 0
			end
		elseif page_hold == 1 then
			local strlen = string.len(entry)
			local m = 0
			local n = 0
			local ww = 0
			if entry == ">DELETE" then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				if strlen == 5 then
					-- format XXX/X
					if string.sub(entry, 4, 4) ~= "/" then
						ww = 1
					end
					m = tonumber(string.sub(entry, 1, 3))
					if m == nil then
						ww = 1
					else
						if m < 0 and m > 360 then
							ww = 1
						end
					end
					if string.sub(entry, 5, 5) ~= "L" and string.sub(entry, 5, 5) ~= "R" then
						ww = 1
					end
					if ww == 0 then
						m = m % 360
						-- save data
						if rte12_sel == 0 then
							new_hold_inbd = string.format("%03d", m)
							if string.sub(entry, 5, 5) == "L" then
								new_hold_turn = 0
							else
								new_hold_turn = 1
							end
							
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx][21] = 1
							else
								legs_data2[hold_offset_idx][21] = new_hold_turn
							end
							if new_hold_inbd == "---" then
								legs_data2[hold_offset_idx][29] = "000"
							else
								legs_data2[hold_offset_idx][29] = tostring(tonumber(new_hold_inbd) * 10)
							end
							if rte12_act == rte12_sel then
								hold_exec = 1
							end
						else
							new_hold_inbd2 = string.format("%03d", m)
							if string.sub(entry, 5, 5) == "L" then
								new_hold_turn2 = 0
							else
								new_hold_turn2 = 1
							end
							
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx7][21] = 1
							else
								legs_data8[hold_offset_idx7][21] = new_hold_turn2
							end
							if new_hold_inbd2 == "---" then
								legs_data8[hold_offset_idx7][29] = "000"
							else
								legs_data8[hold_offset_idx7][29] = tostring(tonumber(new_hold_inbd2) * 10)
							end
							if rte12_act == rte12_sel then
								hold_exec2 = 1
							end
						end
						
						entry = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 3 then
					-- format XXX
					m = tonumber(entry)
					if m == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						
						-- save data
						if rte12_sel == 0 then
							new_hold_inbd = string.format("%03d", m)
							
							if new_hold_inbd == "---" then
								legs_data2[hold_offset_idx][29] = "000"
							else
								legs_data2[hold_offset_idx][29] = tostring(tonumber(new_hold_inbd) * 10)
							end
							if rte12_act == rte12_sel then
								hold_exec = 1
							end
						else
							new_hold_inbd2 = string.format("%03d", m)
							
							if new_hold_inbd2 == "---" then
								legs_data8[hold_offset_idx7][29] = "000"
							else
								legs_data8[hold_offset_idx7][29] = tostring(tonumber(new_hold_inbd2) * 10)
							end
							if rte12_act == rte12_sel then
								hold_exec2 = 1
							end
						end
						entry = ""
					end
				elseif strlen == 2 then
					-- format /X
					if string.sub(entry, 1, 1) ~= "/" then
						ww = 1
					end
					if string.sub(entry, 2, 2) ~= "L" and string.sub(entry, 2, 2) ~= "R" then
						ww = 1
					end
					if ww == 0 then
						-- save data
						if rte12_sel == 0 then
							if string.sub(entry, 2, 2) == "L" then
								new_hold_turn = 0
							else
								new_hold_turn = 1
							end
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx][21] = 1
							else
								legs_data2[hold_offset_idx][21] = new_hold_turn
							end
							if rte12_act == rte12_sel then
								hold_exec = 1
							end
						else
							if string.sub(entry, 2, 2) == "L" then
								new_hold_turn2 = 0
							else
								new_hold_turn2 = 1
							end
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx7][21] = 1
							else
								legs_data8[hold_offset_idx7][21] = new_hold_turn2
							end
							if rte12_act == rte12_sel then
								hold_exec2 = 1
							end
						end
						entry = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 1 then
					-- format X
					if entry == "L" then
						-- save data
						if rte12_sel == 0 then
							new_hold_turn = 0
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx][21] = 1
							else
								legs_data2[hold_offset_idx][21] = new_hold_turn
							end
							if rte12_act == rte12_sel then
								hold_exec = 1
							end
						else
							new_hold_turn2 = 0
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx7][21] = 1
							else
								legs_data8[hold_offset_idx7][21] = new_hold_turn2
							end
							if rte12_act == rte12_sel then
								hold_exec2 = 1
							end
						end
						entry = ""
					elseif entry == "R" then
						-- save data
						if rte12_sel == 0 then
							new_hold_turn = 1
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx][21] = 1
							else
								legs_data2[hold_offset_idx][21] = new_hold_turn
							end
							if rte12_act == rte12_sel then
								hold_exec = 1
							end
						else
							new_hold_turn2 = 1
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx7][21] = 1
							else
								legs_data8[hold_offset_idx7][21] = new_hold_turn2
							end
							if rte12_act == rte12_sel then
								hold_exec2 = 1
							end
						end
						entry = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_rte_init == 1 then
			if act_page == 1 and ref_icao_x ~= "----" then
				-- entry RWY
				if rte12_sel == 0 then
					if entry == ">DELETE" then
						entry = INVALID_DELETE
					else
						if rnw_exist(ref_icao_x, entry) == false then
							add_fmc_msg(NOT_IN_DATABASE, 1)
						else
							ref_rwy2 = entry
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							ref_rwy3 = ref_rwy2
							entry = ""
							ref_rwy_exec = 1
							rte_add_dep_arr(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
					end
				else
					if entry == ">DELETE" then
						entry = INVALID_DELETE
					else
						if rnw_exist(ref_icao7_x, entry) == false then
							add_fmc_msg(NOT_IN_DATABASE, 1)
						else
							ref_rwy8 = entry
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							ref_rwy9 = ref_rwy8
							entry = ""
							ref_rwy_exec = 1
							rte_add_dep_arr(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
					end
				end
			else
				-- button 3 LSK
				rte_x_lsk(3, 0, rte12_sel)
			end
		elseif page_fix == 1 then
			local ii = 0
			local jj = string.len(entry)
			local kk = 0
			local radial_temp = 0
			local data_ok = 0
			if act_page <= fix_data_num then
				if jj == 0 then
					-- copy wpt to clipboard
					if fix_data[act_page][8] ~= -1 and fix_data[act_page][9] ~= -1 then
						entry = fix_data[act_page][1] .. string.format("%05.1f", fix_data[act_page][8]) .. "/"
						if fix_data[act_page][9] < 10 then
							entry = entry .. string.format("%3.1f", fix_data[act_page][9])
						elseif fix_data[act_page][9] < 100 then
							entry = entry .. string.format("%4.1f", fix_data[act_page][9])
						else
							entry = entry .. string.format("%5.1f", fix_data[act_page][9])
						end
					elseif fix_data[act_page][21] > 0 then
						if fix_data[act_page][8] ~= -1 then
							entry = fix_data[act_page][1] .. string.format("%05.1f", fix_data[act_page][8]) .. "/"
							if fix_data[act_page][25] < 10 then
								entry = entry .. string.format("%3.1f", fix_data[act_page][25])
							elseif fix_data[act_page][25] < 100 then
								entry = entry .. string.format("%4.1f", fix_data[act_page][25])
							else
								entry = entry .. string.format("%5.1f", fix_data[act_page][25])
							end
						elseif fix_data[act_page][9] ~= -1 then
							--
						end
					end
				elseif entry == ">DELETE" then
					if fix_data[act_page][8] ~= -1 or fix_data[act_page][9] ~= -1 then
						fix_data[act_page][8] = -1
						fix_data[act_page][9] = -1
						fix_data[act_page][21] = -1
						entry = ""
					end
				elseif jj > 1 then
					if string.sub(entry, 1, 1) == "/" then
						-- distance
						ii = tonumber(string.sub(entry, 2, -1))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii <= 0 or ii > 511 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fix_data[act_page][9] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page][8] = -1
								fix_data[act_page][21] = -1
								entry = ""
							end
						end
					elseif string.sub(entry, -1, -1) == "/" then
						-- radial
						ii = tonumber(string.sub(entry, 1, -2))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii < 0 or ii > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								ii = ii % 360
								fix_data[act_page][8] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page][9] = -1
								fix_data[act_page][21] = -1
								entry = ""
							end
						end
					else
						--fixsk
						kk,ii = string.find(entry, "/")
						if kk ~= nil and kk > 1 and kk < jj then
							radial_temp = tonumber(string.sub(entry, 1, kk - 1))
							if radial_temp == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									data_ok = 1
								end
								if data_ok == 1 then
									ii = tonumber(string.sub(entry, kk + 1, -1))
									if ii == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if ii <= 0 or ii > 511 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fix_data[act_page][8] = tonumber(string.format("%5.1f", radial_temp))
											fix_data[act_page][9] = tonumber(string.format("%5.1f", ii))
											fix_data[act_page][21] = -1
											entry = ""
										end
									end
								end
							end
						else
							radial_temp = tonumber(entry)
							if jj == 3 and radial_temp ~= nil then
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									fix_data[act_page][8] = tonumber(string.format("%5.1f", radial_temp))
									fix_data[act_page][9] = -1
									fix_data[act_page][21] = -1
									entry = ""
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs == 1 then
			-- 3 LSK
			legs_x_lsk(3, 0, rte12_sel)
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			-- CG entry
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen == 0 then
				if B738DR_calc_to_cg ~= 0 then
					cg = string.format("%4.1f", B738DR_calc_to_cg)
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							--add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							entry = ""
						end
					end
					B738DR_calc_trim = 1
				end
			else
				if entry == ">DELETE" then
					cg = "--.-"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							--add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							entry = ""
						end
					end
					B738DR_calc_trim = 1
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 6 or n > 36 then	-- CG min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							cg = string.format("%4.1f", n)
							if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
								if v1_set == "---" and vr_set == "---" and v2_set == "---" then
									entry = ""
								else
									--add_fmc_msg(VERIFY_TO_SPEEDS, 1)
									entry = ""
								end
							end
							B738DR_calc_trim = 1
						end
					end
				end
			end
		elseif page_init == 1 then
			-- go to Perf init page
			page_init = 0
			page_perf = 1
			display_update = 1
		elseif page_pos_init == 2 then
			if B738DR_irs2_pos == "-----.-------.-" then
				entry = ""
			else
				entry = B738DR_irs2_pos
			end
		elseif page_pos_init == 1 and disable_POS_3L == 0 then
			-- entry Gate
			if entry == ">DELETE" then
				ref_gate = "-----"
				entry = ""
			else
				if string.len(entry) > 0 and string.len(entry) < 6 then
					ref_gate = entry
					local lenstr = string.len(ref_gate)
					if lenstr == 1 then
						ref_gate = ref_gate .. "    "
					elseif lenstr == 2 then
						ref_gate = ref_gate .. "   "
					elseif lenstr == 3 then
						ref_gate = ref_gate .. "  "
					elseif lenstr == 4 then
						ref_gate = ref_gate .. " "
					end
					entry = ""
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_perf == 1 then
			-- entry ZFW
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen == 0 then
				zfw_mod = zfw_calc
				if units == 0 then
					zfw_lbs_mod = zfw_mod
					zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))
				else
					zfw_kgs_mod = zfw_mod
					zfw_lbs_mod = string.format("%5.1f", (tonumber(zfw_mod) * KGS_LBS))
				end
				if perf_exec < 2 then
					gw = gw_mod
					gw_lbs = gw_lbs_mod
					gw_kgs = gw_kgs_mod
					zfw = zfw_mod
					zfw_lbs = zfw_lbs_mod
					zfw_kgs = zfw_kgs_mod
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
							qrh = "OFF"
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
				else
					perf_exec = 3
				end
				entry = ""
				
			else
				if entry == ">DELETE" then
					if perf_exec < 2 then
						zfw_mod = "***.*"
						zfw_lbs_mod = zfw_mod
						zfw_kgs_mod = zfw_mod
						gw = gw_mod
						gw_lbs = gw_lbs_mod
						gw_kgs = gw_kgs_mod
						zfw = zfw_mod
						zfw_lbs = zfw_lbs_mod
						zfw_kgs = zfw_kgs_mod
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry = ""
							else
								add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								verify_to_spd = 1
								qrh = "OFF"
								entry = ""
							end
						end
						B738DR_calc_vspd = 1
						B738DR_calc_trim = 1
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < weight_min or n > weight_max then	-- ZFW min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							zfw_mod = string.format("%5.1f", n)
							if units == 0 then
								zfw_lbs_mod = zfw_mod
								zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))
							else
								zfw_kgs_mod = zfw_mod
								zfw_lbs_mod = string.format("%5.1f", (tonumber(zfw_mod) * KGS_LBS))
							end
							if perf_exec < 2 then
								gw = gw_mod
								gw_lbs = gw_lbs_mod
								gw_kgs = gw_kgs_mod
								zfw = zfw_mod
								zfw_lbs = zfw_lbs_mod
								zfw_kgs = zfw_kgs_mod
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if v1_set == "---" and vr_set == "---" and v2_set == "---" then
										entry = ""
									else
										add_fmc_msg(VERIFY_TO_SPEEDS, 1)
										verify_to_spd = 1
										qrh = "OFF"
										entry = ""
									end
								end
								B738DR_calc_vspd = 1
								B738DR_calc_trim = 1
							else
								perf_exec = 3
							end
							entry = ""
						end
					end
				end
			end
		elseif page_n1_limit == 1 then
			if in_flight_mode == 0 then
				--if B738DR_thr_takeoff_N1 ~= 0 then
				if B738DR_fmc_oat_temp ~= 99 then
					to_1 = "<ACT>"
					to = "     "
					to_2 = "     "
					--if sel_clb_thr == 0 then
					if sel_temp == "----" then
						clb = "<SEL>"
						clb_1 = "     "
						clb_2 = "     "
					else
						clb = "     "
						clb_1 = "     "
						clb_2 = "<SEL>"
					end
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
				end
			else
				-- select CON
				auto_act = "     "
				ga_act = "     "
				con_act = "<ACT>"
				clb_act = "     "
				crz_act = "     "
			end
		elseif page_descent_forecast == 1 then
			-- entry WIND ALT LAYER 1
			if entry == ">DELETE" then
				forec_alt_1_mod = "-----"
				forec_alt_1_num_mod = 0
				entry = ""
				forecast_exec = 1
			else
				local n = entry_altitude(entry)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					forec_alt_1_num_mod = n
					if forec_alt_1_num_mod >= B738DR_trans_lvl_mod then
						n = n / 100
						forec_alt_1_mod = "FL" .. string.format("%03d", n)
					else
						forec_alt_1_mod = string.format("%5d", n)
					end
					entry = ""
					if forec_dir_1 ~= "---" and forec_spd_1 ~= "---" then
						forecast_exec = 1
					end
					wind_alt_order()
				end
			end
		elseif page_perf == 2 then
			-- CRZ min speed kts/mach
			local strlen = string.len(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					crz_min_kts = "   "
					crz_min_mach = "   "
					entry = ""
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_max_mach)
							if nn == nil then
								nn = 0.82
							else
								nn = nn / 1000
							end
							if n < 0.4 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								crz_min_mach = string.format("%03d", n)
								entry = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								crz_min_kts = string.format("%03d", n)
								entry = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(crz_max_mach)
										if nn == nil then
											nn = 0.82
										else
											nn = nn / 1000
										end
										if n < 0.4 or n > nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											crz_min_kts = kts
											crz_min_mach = string.format("%03d", n)
											entry = ""
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		-----------
		elseif page_ref_nav_data_wpt == 1 then
			if string.len(entry) > 0 then
				local err_pos = 0
				local n = 0
				if string.len(entry) ~= 7 or string.sub(entry, 6, 6) ~= "." then
					err_pos = 1
				end
				n = tonumber(string.sub(entry, 2, 3))
				if (n == nil) or (n < 0) or (n > 90) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry, 4, 5))
				if (n == nil) or (n < 0) or (n > 59) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry, 7, 7))
				if n == nil then
					err_pos = 1
				end
				if string.sub(entry, 1, 1) ~= "N" 
				and string.sub(entry, 1, 1) ~= "S" then
					err_pos = 1
				end
				if err_pos == 0 then
					ref_nav_lat = tonumber(string.sub(entry, 2, 3)) + (tonumber(string.sub(entry, 4, 7)) / 60)
					if string.sub(entry, 1, 1) == "S" then
						ref_nav_lat = -ref_nav_lat
					end
					entry = ""
					ref_nav_id = "-----"
					ref_nav_rc = ""
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					if ref_nav_lon ~= 0 then
						ref_nav_exec = 1
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_offset == 1 and offset_dist_mod ~= 0 and offset_act < 5 then
			-- entry Start Offset
			local strlen = string.len(entry)
			if strlen == 0 then
				if offset_start_mod ~= 0 then
					if rte12_act == 0 then
						entry = legs_data2[offset_start_mod][1]
					elseif rte12_act == 1 then
						entry = legs_data8[offset_start_mod][1]
					end
				end
			else
				if entry == ">DELETE" then
					offset_start_mod = 0
					entry = ""
					offset_check(rte12_act)
					if offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
						if offset_create(rte12_act) == true then
							-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
								-- offset_act_mod = 3
							-- else
								-- offset_act_mod = 2
							-- end
							if rte12_act == 0 then
								if offset_start_mod == 0 then
									if legs_data2[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							else
								if offset_start_mod == 0 then
									if legs_data8[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							end
						else
							offset_act_mod = 1
						end
					end
				elseif strlen < 6 then
					offset_check1(entry, rte12_act)
					if offset_start_mod == 0 then
						-- add_fmc_msg(INVALID_INPUT, 1)
						-- add_fmc_msg(UNABLE_OFFSET, 1)
						add_fmc_msg(NO_OFFSET_AT .. entry , 1)
					else
						entry = ""
						offset_act_mod = 1
						offset_check(rte12_act)
						if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
							if offset_create(rte12_act) == true then
								-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
									-- offset_act_mod = 3
								-- else
									-- offset_act_mod = 2
								-- end
								if rte12_act == 0 then
									if offset_start_mod == 0 then
										if legs_data2[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								else
									if offset_start_mod == 0 then
										if legs_data8[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								end
							else
								offset_act_mod = 1
							end
						else
							add_fmc_msg(UNABLE_OFFSET, 1)
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb < 2 then
				if entry == ">DELETE" and clb_del_spd_idx ~= 0 then
					entry = ""
					clb_del_spd_idx2 = clb_del_spd_idx
					climb_exec = 1
				else
					set_spd_alt_rest_default2(0, 0)
				end
			end
		elseif page_descent == 1 then
			if entry == ">DELETE" and des_del_spd_idx ~= 0 then
				entry = ""
				des_del_spd_idx2 = des_del_spd_idx
				descent_exec = 1
			else
				-- change restrict speed by altitude
				set_spd_alt_rest_default(0, 0)
			end
		elseif page_cruise == 1 then
			if (crz_exec ~= 0 and B738DR_flight_phase >= 2 and B738DR_flight_phase < 8) or B738DR_flight_phase == 3 or B738DR_flight_phase == 4 then
				if B738DR_flight_phase == 3 or crz_alt_num2 > (simDR_altitude_pilot + 200) then
					set_spd_alt_rest_default2(0, 1)
				elseif B738DR_flight_phase == 4 or crz_alt_num2 <= (simDR_altitude_pilot + 200) then
					set_spd_alt_rest_default(0, 1)
				end
			end
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				entry = nearest_apt_list_icao[3]
				if entry == "----" then
					entry = ""
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				add_altn_apt(entry, 3, 0)
			end
		elseif page_flt_plns == 1 then
			entry = sel_flt_plns[3]
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 4LSK
function B738_fmc1_4L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep == 1 then
			if rte12_sel == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel[4] ~= "" then
						if ref_sid2 == ref_sid_sel[4] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid2 = ref_sid_sel[4]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel[4] ~= "" then
							if ref_sid_tns2 == ref_tns_sel[4] then
								sel_ref_sid_tns2 = ref_sid_tns2
							else
								ref_sid_tns2 = ref_tns_sel[4]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel[4] ~= "" then
						if ref_sid8 == ref_sid_sel[4] then
							sel_ref_sid8 = ref_sid8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid8 = ref_sid_sel[4]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel[4] ~= "" then
							if ref_sid_tns8 == ref_tns_sel[4] then
								sel_ref_sid_tns8 = ref_sid_tns8
							else
								ref_sid_tns8 = ref_tns_sel[4]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel[4] ~= "" then
						if des_star2 == des_star_sel[4] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star2 = des_star_sel[4]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel[4] ~= "" then
							if des_star_trans2 == des_star_tns_sel[4] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel[4]
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel[4] ~= "" then
						if des_star8 == des_star_sel[4] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star8 = des_star_sel[4]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel[4] ~= "" then
							if des_star_trans8 == des_star_tns_sel[4] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans8 = des_star_tns_sel[4]
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_sel_wpt == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry, 0, rte12_sel)
			end
		elseif page_sel_wpt3 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 0)
			end
		elseif page_sel_hold_wpt == 1 then
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 0, rte12_sel)
			end
		elseif page_ref_sel == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				ref_nav_new = "*****"
				page_ref_nav_data = 1
				page_ref_sel = 0
			end
		elseif page_rte_init == 1 then
			if act_page == 1 then
				if rte12_sel == 0 then
					if legs_num2 > 1 then
						if save_rte1 == 0 then
							save_rte1 = 1
						elseif save_rte1 == 1 and co_route_save1 ~= "************" then
							--save route
							save_fpln(co_route_save1, 0)
							co_route = co_route_save1
							co_route_x = co_route
							save_rte1 = 0
						end
					end
				else
					if legs_num8 > 1 then
						if save_rte2 == 0 then
							save_rte2 = 1
						elseif save_rte2 == 1 and co_route_save2 ~= "************" then
							--save route
							save_fpln(co_route_save2, 1)
							co_route7 = co_route_save2
							co_route7_x = co_route7
							save_rte2 = 1
						end
					end
				end
			else
				-- button 4 LSK
				rte_x_lsk(4, 0, rte12_sel)
			end
		elseif page_hold == 1 then
			local strlen = string.len(entry)
			local m = 0
			local n = 0
			local ww = 0
			if entry == ">DELETE" then
				if rte12_sel == 0 then
					new_hold_time = "-.-"
					if new_hold_time == "-.-" then
						if new_hold_dist == "--.-" then
							-- default 1.5 min
							legs_data2[hold_offset_idx][30] = ""
						else
							m = tonumber(new_hold_dist) * 10
							legs_data2[hold_offset_idx][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time) * 10
						legs_data2[hold_offset_idx][30] = "T" .. string.format("%03d", m)
					end
				else
					new_hold_time2 = "-.-"
					if new_hold_time2 == "-.-" then
						if new_hold_dist2 == "--.-" then
							-- default 1.5 min
							legs_data8[hold_offset_idx7][30] = ""
						else
							m = tonumber(new_hold_dist2) * 10
							legs_data8[hold_offset_idx7][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time2) * 10
						legs_data8[hold_offset_idx7][30] = "T" .. string.format("%03d", m)
					end
				end
				entry = ""
			else
				if strlen > 0 and strlen < 4 then
					m = tonumber(entry)
					-- if ww == 0 then
					if m ~= nil then
						if m >= 1 and m <= 9.9 then
							-- m = tonumber(entry)
							if rte12_sel == 0 then
								new_hold_time = string.format("%03.1f", m)
								new_hold_dist = "--.-"
								if new_hold_time == "-.-" then
									if new_hold_dist == "--.-" then
										-- default 1.5 min
										legs_data2[hold_offset_idx][30] = ""
									else
										m = tonumber(new_hold_dist) * 10
										legs_data2[hold_offset_idx][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time) * 10
									legs_data2[hold_offset_idx][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel then
									hold_exec = 1
								end
							else
								new_hold_time2 = string.format("%03.1f", m)
								new_hold_dist2 = "--.-"
								if new_hold_time2 == "-.-" then
									if new_hold_dist2 == "--.-" then
										-- default 1.5 min
										legs_data8[hold_offset_idx7][30] = ""
									else
										m = tonumber(new_hold_dist2) * 10
										legs_data8[hold_offset_idx7][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time2) * 10
									legs_data8[hold_offset_idx7][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel then
									hold_exec2 = 1
								end
							end
							entry = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_fix == 1 then
			local ii = 0
			local jj = string.len(entry)
			local kk = 0
			local radial_temp = 0
			local data_ok = 0
			if act_page <= fix_data_num then
				if jj == 0 then
					-- copy wpt to clipboard
					if fix_data[act_page][10] ~= -1 and fix_data[act_page][11] ~= -1 then
						entry = fix_data[act_page][1] .. string.format("%05.1f", fix_data[act_page][10]) .. "/"
						if fix_data[act_page][11] < 10 then
							entry = entry .. string.format("%3.1f", fix_data[act_page][11])
						elseif fix_data[act_page][11] < 100 then
							entry = entry .. string.format("%4.1f", fix_data[act_page][11])
						else
							entry = entry .. string.format("%5.1f", fix_data[act_page][11])
						end
					elseif fix_data[act_page][30] > 0 then
						if fix_data[act_page][10] ~= -1 then
							entry = fix_data[act_page][1] .. string.format("%05.1f", fix_data[act_page][10]) .. "/"
							if fix_data[act_page][34] < 10 then
								entry = entry .. string.format("%3.1f", fix_data[act_page][34])
							elseif fix_data[act_page][34] < 100 then
								entry = entry .. string.format("%4.1f", fix_data[act_page][34])
							else
								entry = entry .. string.format("%5.1f", fix_data[act_page][34])
							end
						elseif fix_data[act_page][11] ~= -1 then
							--
						end
					end
				elseif entry == ">DELETE" then
					if fix_data[act_page][10] ~= -1 or fix_data[act_page][11] ~= -1 then
						fix_data[act_page][10] = -1
						fix_data[act_page][11] = -1
						fix_data[act_page][30] = -1
						entry = ""
					end
				elseif jj > 1 then
					if string.sub(entry, 1, 1) == "/" then
						-- distance
						ii = tonumber(string.sub(entry, 2, -1))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii <= 0 or ii > 511 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fix_data[act_page][11] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page][10] = -1
								fix_data[act_page][30] = -1
								entry = ""
							end
						end
					elseif string.sub(entry, -1, -1) == "/" then
						-- radial
						ii = tonumber(string.sub(entry, 1, -2))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii < 0 or ii > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								ii = ii % 360
								fix_data[act_page][10] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page][11] = -1
								fix_data[act_page][30] = -1
								entry = ""
							end
						end
					else
						--fixsk
						kk,ii = string.find(entry, "/")
						if kk ~= nil and kk > 1 and kk < jj then
							radial_temp = tonumber(string.sub(entry, 1, kk - 1))
							if radial_temp == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									data_ok = 1
								end
								if data_ok == 1 then
									ii = tonumber(string.sub(entry, kk + 1, -1))
									if ii == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if ii <= 0 or ii > 511 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fix_data[act_page][10] = tonumber(string.format("%5.1f", radial_temp))
											fix_data[act_page][11] = tonumber(string.format("%5.1f", ii))
											fix_data[act_page][30] = -1
											entry = ""
										end
									end
								end
							end
						else
							radial_temp = tonumber(entry)
							if jj == 3 and radial_temp ~= nil then
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									fix_data[act_page][10] = tonumber(string.format("%5.1f", radial_temp))
									fix_data[act_page][11] = -1
									fix_data[act_page][30] = -1
									entry = ""
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs == 1 then
			-- 4 LSK
			legs_x_lsk(4, 0, rte12_sel)
		elseif page_init == 1 then
			-- if B738DR_flight_phase == 1 then
				-- -- go to Climb page
				-- reset_fmc_pages()
				-- page_climb = 1
			-- elseif B738DR_flight_phase == 2 then
				-- -- go to Cruise page
				-- reset_fmc_pages()
				-- page_cruise = 1
			-- else
				-- go to Takeoff page
				reset_fmc_pages()
				page_takeoff = 1
			-- end
		elseif page_pos_init == 2 then
			if B738DR_gps_pos == "-----.-------.-" then
				entry = ""
			else
				entry = B738DR_gps_pos
			end
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
				-- if perf_exec == 2 and B738DR_climb_mode ~= 0 then
					-- -- CLB ECON
					-- entry = ""
					-- B738DR_climb_mode = 0
					-- B738DR_fmc_climb_speed_mach = econ_clb_spd_mach		-- temporary
					-- B738DR_fmc_climb_speed = econ_clb_spd				-- temporary
				-- end
				if perf_exec == 2 and fmc_climb_mode_mod ~= 0 then
					-- CLB ECON
					entry = ""
					fmc_climb_mode_mod = 0
					fmc_climb_speed_mach_mod = econ_clb_spd_mach		-- temporary
					fmc_climb_speed_mod = econ_clb_spd				-- temporary
					clb_spd_exec = 1
				end
			else
				B738DR_eng_out_disable = 1
			end
		elseif page_eo_climb == 1 then
			engout_clb = 2
		elseif page_eo_cruise == 1 then
			engout_crz = 2
		elseif page_descent_forecast == 1 then
			-- entry WIND ALT LAYER 2
			if entry == ">DELETE" then
				forec_alt_2_mod = "-----"
				forec_alt_2_num_mod = 0
				entry = ""
				forecast_exec = 1
			else
				local n = entry_altitude(entry)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					forec_alt_2_num_mod = n
					if forec_alt_2_num_mod >= B738DR_trans_lvl_mod then
						n = n / 100
						forec_alt_2_mod = "FL" .. string.format("%03d", n)
					else
						forec_alt_2_mod = string.format("%5d", n)
					end
					entry = ""
					if forec_dir_2 ~= "---" or forec_spd_2 ~= "---" then
						forecast_exec = 1
					end
					wind_alt_order()
				end
			end
		elseif page_perf == 1 then
			-- entry Reserve fuel
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					if perf_exec < 2 then
						reserves_mod = "**.*"
						reserves_kgs_mod = reserves_mod
						reserves_lbs_mod = reserves_mod
						reserves = reserves_mod
						reserves_lbs = reserves
						reserves_kgs = reserves
						msg_using_rsv_fuel = 0
						msg_using_ins_fuel = 0
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						--36.2 lbs / 16.4
						local max_res_value = 36.2
						if units ~= 0 then
							max_res_value = 16.4
						end
						if n < 0 or n > max_res_value then	-- Reserves min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							reserves_mod = string.format("%4.1f", n)
							if units == 0 then
								reserves_lbs_mod = reserves_mod
								reserves_kgs_mod = string.format("%4.1f", (tonumber(reserves_mod) / KGS_LBS))
							else
								reserves_kgs_mod = reserves_mod
								reserves_lbs_mod = string.format("%4.1f", (tonumber(reserves_mod) * KGS_LBS))
							end
							if perf_exec < 2 then
								reserves = reserves_mod
								reserves_lbs = reserves_lbs_mod
								reserves_kgs = reserves_kgs_mod
								msg_using_rsv_fuel = 0
								msg_using_ins_fuel = 0
							else
								perf_exec = 3
							end
							entry = ""
						end
					end
				end
			end
		elseif page_n1_limit == 1 then
			if in_flight_mode == 0 then
				--if B738DR_thr_takeoff_N1 ~= 0 then
				if B738DR_fmc_oat_temp ~= 99 then
					to_2 = "<ACT>"
					to = "     "
					to_1 = "     "
					--if sel_clb_thr == 0 then
					if sel_temp == "----" then
						clb = "     "
						clb_1 = "<SEL>"
						clb_2 = "     "
					else
						clb = "     "
						clb_1 = "     "
						clb_2 = "<SEL>"
					end
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
				end
			else
				-- select CLB
				auto_act = "     "
				ga_act = "     "
				con_act = "     "
				clb_act = "<ACT>"
				crz_act = "     "
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			-- enter ACCELERATION HEIGHT AGL
			if B738DR_thr_cutback == 0 then
				local strlen = string.len(entry)
				if strlen > 0 then
					if entry == ">DELETE" then
						accel_alt = "----"
						accel_alt_num = B738DR_def_accel_alt
						entry = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 400 or n > 9999 then	-- EO ACCEL HT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								accel_alt = string.format("%4d", n)
								entry = ""
								accel_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_perf == 2 then
			-- DES min speed kts/mach
			local strlen = string.len(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					des_min_kts = "   "
					des_min_mach = "   "
					entry = ""
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_max_mach)
							if nn == nil then
								nn = 0.82
							else
								nn = nn / 1000
							end
							if n < 0.4 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								des_min_mach = string.format("%03d", n)
								entry = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								des_min_kts = string.format("%03d", n)
								entry = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(des_max_mach)
										if nn == nil then
											nn = 0.82
										else
											nn = nn / 1000
										end
										if n < 0.4 or n > nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											des_min_kts = kts
											des_min_mach = string.format("%03d", n)
											entry = ""
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		-----------
		elseif page_offset == 1 and offset_dist_mod ~= 0 and offset_act < 8 then
			-- entry End Offset
			local strlen = string.len(entry)
			if strlen == 0 then
				if offset_end_mod ~= 0 then
					if rte12_act == 0 then
						entry = legs_data2[offset_end_mod][1]
					elseif rte12_act == 1 then
						entry = legs_data8[offset_end_mod][1]
					end
				end
			else
				if entry == ">DELETE" then
					offset_end_mod = 0
					entry = ""
					offset_check(rte12_act)
					if offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
						if offset_create(rte12_act) == true then
							-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
								-- offset_act_mod = 3
							-- else
								-- offset_act_mod = 2
							-- end
							if rte12_act == 0 then
								if offset_start_mod == 0 then
									if legs_data2[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							else
								if offset_start_mod == 0 then
									if legs_data8[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							end
						else
							offset_act_mod = 1
						end
					end
				elseif strlen < 6 then	--and offset_start ~= 0 then
					offset_check2(entry, rte12_act)
					if offset_end_mod == 0 then
						-- add_fmc_msg(INVALID_INPUT, 1)
						-- add_fmc_msg(UNABLE_OFFSET, 1)
						add_fmc_msg(NO_OFFSET_AT .. entry , 1)
					else
						entry = ""
						offset_act_mod = 1
						offset_check(rte12_act)
						if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
							if offset_create(rte12_act) == true then
								-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
									-- offset_act_mod = 3
								-- else
									-- offset_act_mod = 2
								-- end
								if rte12_act == 0 then
									if offset_start_mod == 0 then
										if legs_data2[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								else
									if offset_start_mod == 0 then
										if legs_data8[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								end
							else
								offset_act_mod = 1
							end
						else
							add_fmc_msg(UNABLE_OFFSET, 1)
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				entry = nearest_apt_list_icao[4]
				if entry == "----" then
					entry = ""
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				add_altn_apt(entry, 4, 0)
			end
		elseif page_flt_plns == 1 then
			entry = sel_flt_plns[4]
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 5LSK
function B738_fmc1_5L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep == 1 then
			if rte12_sel == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel[5] ~= "" then
						if ref_sid2 == ref_sid_sel[5] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid2 = ref_sid_sel[5]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel[5] ~= "" then
							if ref_sid_tns2 == ref_tns_sel[5] then
								sel_ref_sid_tns2 = ref_sid_tns2
							else
								ref_sid_tns2 = ref_tns_sel[5]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel[5] ~= "" then
						if ref_sid8 == ref_sid_sel[5] then
							sel_ref_sid8 = ref_sid8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
						else
							ref_sid8 = ref_sid_sel[5]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel)
							end
							create_sid_tns_list(rte12_sel)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							-- check SID for selected runway
							if rte12_act == rte12_sel then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel[5] ~= "" then
							if ref_sid_tns8 == ref_tns_sel[5] then
								sel_ref_sid_tns8 = ref_sid_tns8
							else
								ref_sid_tns8 = ref_tns_sel[5]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel[5] ~= "" then
						if des_star2 == des_star_sel[5] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star2 = des_star_sel[5]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel[5] ~= "" then
							if des_star_trans2 == des_star_tns_sel[5] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel[5]
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel[5] ~= "" then
						if des_star8 == des_star_sel[5] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
						else
							des_star8 = des_star_sel[5]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel)
							end
							create_star_tns_list(rte12_sel)
							des_star_exec = 1
						end
						act_page = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel[5] ~= "" then
							if des_star_trans8 == des_star_tns_sel[5] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans8 = des_star_tns_sel[5]
								des_star_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_sel_wpt == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		
		elseif page_sel_wpt2 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry, 0, rte12_sel)
			end
		elseif page_sel_wpt3 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 0, rte12_sel)
				if rte12_act == rte12_sel then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 0)
			end
		elseif page_sel_hold_wpt == 1 then
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 0, rte12_sel)
			end
		elseif page_ref_sel == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				--ref_nav_wpt = nav_data_entry
				ref_nav_new = "*****"
				page_ref_nav_data = 1
				page_ref_sel = 0
			end
		elseif page_rte_init == 1 then
			if act_page == 1 then
				if save_rte1 ~= 0 and rte12_sel == 0 then
					save_rte1 = 0
					co_route_save1 = "************"
				elseif save_rte2 ~= 0 and rte12_sel == 1 then
					save_rte2 = 0
					co_route_save2 = "************"
				else
					page_altern_apt = 1
					page_rte_init = 0
					act_page = 1
				end
			else
				-- button 5 LSK
				rte_x_lsk(5, 0, rte12_sel)
			end
		elseif page_hold == 1 then
			local strlen = string.len(entry)
			local m = 0
			local n = 0
			local ww = 0
			if entry == ">DELETE" then
				if rte12_sel == 0 then
					new_hold_dist = "--.-"
					if new_hold_time == "-.-" then
						if new_hold_dist == "--.-" then
							-- default 1.5 min
							legs_data2[hold_offset_idx][30] = ""
						else
							m = tonumber(new_hold_dist) * 10
							legs_data2[hold_offset_idx][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time) * 10
						legs_data2[hold_offset_idx][30] = "T" .. string.format("%03d", m)
					end
				else
					new_hold_dist2 = "--.-"
					if new_hold_time2 == "-.-" then
						if new_hold_dist2 == "--.-" then
							-- default 1.5 min
							legs_data8[hold_offset_idx7][30] = ""
						else
							m = tonumber(new_hold_dist2) * 10
							legs_data8[hold_offset_idx7][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time2) * 10
						legs_data8[hold_offset_idx7][30] = "T" .. string.format("%03d", m)
					end
				end
				entry = ""
			else
				if strlen > 0 and strlen < 5 then
					-- if ww == 0 then
					m = tonumber(entry)
					if m ~= nil then
						if m >= 1 and m <= 15 then
							if rte12_sel == 0 then
								new_hold_dist = string.format("%04.1f", m)
								new_hold_time = "-.-"
								if new_hold_time == "-.-" then
									if new_hold_dist == "--.-" then
										-- default 1.5 min
										legs_data2[hold_offset_idx][30] = ""
									else
										m = tonumber(new_hold_dist) * 10
										legs_data2[hold_offset_idx][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time) * 10
									legs_data2[hold_offset_idx][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel then
									hold_exec = 1
								end
							else
								new_hold_dist2 = string.format("%04.1f", m)
								new_hold_time2 = "-.-"
								if new_hold_time2 == "-.-" then
									if new_hold_dist2 == "--.-" then
										-- default 1.5 min
										legs_data8[hold_offset_idx7][30] = ""
									else
										m = tonumber(new_hold_dist2) * 10
										legs_data8[hold_offset_idx7][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time2) * 10
									legs_data8[hold_offset_idx7][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel then
									hold_exec2 = 1
								end
							end
							entry = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs == 1 then
			if B738DR_no_rte2 ~= 0 or act_page ~= 1 then
				-- 5 LSK
				legs_x_lsk(5, 0, rte12_sel)
			end
		elseif page_init == 1 then
			-- go to Approach page
			page_init = 0
			if page_approach == 0 and page_approach2 == 0 then
				gw_app_nul = 0
				gw_app = gw
				gw_app_lbs = gw_lbs
				gw_app_kgs = gw_kgs
			end
			page_approach = 1
			display_update = 1
		elseif page_pos_init == 2 then
			if B738DR_gps2_pos == "-----.-------.-" then
				entry = ""
			else
				entry = B738DR_gps2_pos
			end
		elseif page_approach == 1 then
			-- G/S enable/disable
			if des_app ~= "------" then
				if string.sub(des_app2, 1, 1) ~= "R" and string.sub(des_app2, 1, 1) ~= "D" and string.sub(des_app2, 1, 1) ~= "S" 
				and string.sub(des_app2, 1, 1) ~= "N" and string.sub(des_app2, 1, 1) ~= "Q" and string.sub(des_app2, 1, 1) ~= "V" then
					if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
						if B738DR_fms_ils_disable == 0 then
							B738DR_fms_ils_disable = 1
						else
							B738DR_fms_ils_disable = 0
						end
					end
				end
			end
		elseif page_perf == 1 then --and was_on_air == 0 then	-- only on ground
			-- entry Cost Index
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					if perf_exec < 2 then
						cost_index_mod = "***"
						cost_index = cost_index_mod
						vnav_update = 1
						msg_chk_alt_constr = 0
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 0 or n > 500 then	-- Cost Index min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							cost_index_mod = string.format("%3d", n)
							if perf_exec < 2 then
								cost_index = cost_index_mod
								vnav_update = 1
								msg_chk_alt_constr = 0
							else
								perf_exec = 3
							end
							entry = ""
						end
					end
				end
				B738_calc_vnav_spd()
			end
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb == 0 then
				if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
					-- if B738DR_climb_mode ~= 1 then
						-- -- MAX RATE
						-- entry = ""
						-- B738DR_climb_mode = 1
						-- clb_spd_exec = 1
						-- B738DR_fmc_climb_speed_mach = 0.74	--0.76		-- temporary
						-- if B738DR_pfd_flaps_up == 0 then
							-- B738DR_fmc_climb_speed = 265	--250
						-- else
							-- B738DR_fmc_climb_speed = math.max(B738DR_pfd_flaps_up + 70, 250)
							-- B738DR_fmc_climb_speed = math.min(B738DR_fmc_climb_speed, 250)
						-- end
					-- end
					if fmc_climb_mode_mod ~= 1 then
						-- MAX RATE
						entry = ""
						fmc_climb_mode_mod = 1
						clb_spd_exec = 1
						fmc_climb_speed_mach_mod = 0.74	--0.76		-- temporary
						if B738DR_pfd_flaps_up == 0 then
							fmc_climb_speed_mod = 265	--250
						else
							fmc_climb_speed_mod = math.max(B738DR_pfd_flaps_up + 70, 250)
							fmc_climb_speed_mod = math.min(fmc_climb_speed_mod, 250)
						end
					end
				end
			elseif engout_clb == 1 then
				engout_clb = 2
				page_climb = 0
				page_eo_climb = 1
			end
		elseif page_cruise == 1 then
			if engout_crz == 0 then
				if fmc_cruise_mode_mod ~= 0 then
					-- CRZ ECON
					entry = ""
					fmc_cruise_mode_mod = 0
					fmc_cruise_speed_mod = econ_crz_spd
					fmc_cruise_speed_mach_mod = econ_crz_spd_mach
					crz_spd_exec = 1
				end
			elseif engout_crz == 1 then
				engout_crz = 2
				page_cruise = 0
				page_eo_cruise = 1
			end
		elseif page_descent == 1 then
			if fmc_descent_mode_mod == 2 then
				-- DES PATH ECON
				entry = ""
				fmc_descent_mode_mod = 0
				des_spd_exec = 1
				fmc_descent_speed_mod = econ_des_spd
				fmc_descent_speed_mach_mod = econ_des_spd_mach
			elseif fmc_descent_mode_mod == 3 then
				-- DES SPD ECON
				entry = ""
				fmc_descent_mode_mod = 1
				des_spd_exec = 1
				fmc_descent_speed_mod = econ_des_spd
				fmc_descent_speed_mach_mod = econ_des_spd_mach
			end
		elseif page_n1_limit == 1 then
			if in_flight_mode == 1 then
				-- select CRZ
				auto_act = "     "
				ga_act = "     "
				con_act = "     "
				clb_act = "     "
				crz_act = "<ACT>"
			end
		elseif page_ref_nav_data_wpt == 1 then
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" and ref_nav_id ~= "-----" then
					ref_nav_id = "-----"
					ref_nav_id_lat = 0
					ref_nav_id_lon = 0
					ref_nav_rc = ""
					ref_nav_lat = 0
					ref_nav_lon = 0
					ref_nav_exec = 1
					entry = ""
				else
					find_navaid(entry, "", 0, "", rte12_act)
					if navaid_list_n > 0 then
						-- first navaid
						ref_nav_id = navaid_list[1][4]
						ref_nav_rc = navaid_list[1][8]
						ref_nav_id_lat = navaid_list[1][2]
						ref_nav_id_lon = navaid_list[1][3]
						
						-- calc lat / lon
						if ref_nav_brg ~= "---" and ref_nav_dist ~= "---" then
							calc_brg_dist(ref_nav_id_lat, ref_nav_id_lon, math.rad(tonumber(ref_nav_brg)), tonumber(ref_nav_dist))
							ref_nav_lat = calc_lat
							ref_nav_lon = calc_lon
							ref_nav_exec = 1
						end
						entry = ""
					end
				end
			end
		elseif page_descent_forecast == 1 then
			-- entry WIND ALT LAYER 3
			if entry == ">DELETE" then
				forec_alt_3_mod = "-----"
				forec_alt_3_num_mod = 0
				entry = ""
				forecast_exec = 1
			else
				local n = entry_altitude(entry)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					forec_alt_3_num_mod = n
					if forec_alt_3_num_mod >= B738DR_trans_lvl_mod then
						n = n / 100
						forec_alt_3_mod = "FL" .. string.format("%03d", n)
					else
						forec_alt_3_mod = string.format("%5d", n)
					end
					entry = ""
					if forec_dir_3 ~= "---" or forec_spd_3 ~= "---" then
						forecast_exec = 1
					end
					wind_alt_order()
				end
			end
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			if verify_to_spd ~= 0 then
				-- REJECT
				verify_to_spd = 0
				v1_set = "---"
				vr_set = "---"
				v2_set = "---"
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			-- entry THR REDUCTION ALT AGL
			local strlen = string.len(entry)
			if B738DR_thr_cutback == 1 then
				if strlen > 0 then
					if entry == ">DELETE" then
						cut_alt = "----"
						cut_alt_num = B738DR_def_cut_alt
						entry = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 800 or n > 9999 then	-- THR RED ALT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								cut_alt = string.format("%4d", n)
								entry = ""
								cut_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				entry = nearest_apt_list_icao[5]
				if entry == "----" then
					entry = ""
				end
			else
				if nearest_apt_list_via[act_page - 1] == 0 then
					nearest_apt_list_via[act_page - 1] = 1
				else
					nearest_apt_list_via[act_page - 1] = 0
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				add_altn_apt(entry, 5, 0)
			else
				if altern_apt_list_via[act_page - 1] == 0 then
					altern_apt_list_via[act_page - 1] = 1
				else
					altern_apt_list_via[act_page - 1] = 0
				end
			end
			
		elseif page_fix == 1 then
			if act_page > 0 and act_page < 6 then
				if entry == ">DELETE" then
					fix_abm_act[act_page] = 0
					entry = ""
				elseif fix_abm_act[act_page] == 0 then
					if fix_abm_wpt_idx[act_page] > 0 then
						fix_abm_act[act_page] = 1
					end
				elseif fix_abm_wpt_idx[act_page] > 0 then
					if entry == "" then
						entry = fix_data[act_page][1] .. string.format("%05.1f", fix_abm_brg[act_page]) .. "/"
						if fix_abm_dist[act_page] >= 1000 then
							entry = entry .. "999.9"
						elseif fix_abm_dist[act_page] >= 100 then
							entry = entry .. string.format("%5.1f", fix_abm_dist[act_page])
						elseif fix_abm_dist[act_page] >= 10 then
							entry = entry .. string.format("%4.1f", fix_abm_dist[act_page])
						else
							entry = entry .. string.format("%3.1f", fix_abm_dist[act_page])
						end
					end
				end
			end
		elseif page_flt_plns == 1 then
			entry = sel_flt_plns[5]
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 6LSK
function B738_fmc1_6L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_takeoff ~= 0  then
			if pre_flt_pos_init == 0 then
				-- go to Pos init
				page_pos_init = 1
			elseif pre_flt_rte == 0 then
				-- go to Route init
				page_rte_init = 1
			elseif pre_flt_dep == 0 then
				-- go to Departure
				page_dep = 1
			elseif pre_flt_perf_init == 0 then
				-- go to Perf_init
				page_perf = 1
			else
				-- go to Index page
				page_init = 1
			end
			page_takeoff = 0
			display_update = 1
		elseif page_cruise == 1 then
			local show_crz_clb_des = -1
			if B738DR_flight_phase >= 2 then
				if crz_exec ~= 0 then
					if crz_alt_num2 > (simDR_altitude_pilot + 200) then
						show_crz_clb_des = 0	-- CRZ CLB
					else
						show_crz_clb_des = 1	-- CRZ DES
					end
				elseif B738DR_flight_phase == 3 then
					show_crz_clb_des = 0
				elseif B738DR_flight_phase == 4 then
					show_crz_clb_des = 1
				end
			end
			if show_crz_clb_des == 1 then
				-- Forecast data
				local disable_forecast = 0
				if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and crz_exec == 0 then
					disable_forecast = 1
				elseif B738DR_flight_phase < 2 or B738DR_flight_phase == 8 or (crz_exec == 0 and B738DR_flight_phase == 2) then
					disable_forecast = 1
				end
				if disable_forecast == 0 then
					page_descent_forecast = 1
					page_cruise = 0
				end
			else
				--LRC
				if engout_crz == 0 then
					if fmc_cruise_mode_mod ~= 1 then
						-- CRZ LRC
						entry = ""
						fmc_cruise_mode_mod = 1
						fmc_cruise_speed_mod = lrc_crz_spd
						fmc_cruise_speed_mach_mod = lrc_crz_spd_mach
						crz_spd_exec = 1
					end
				end
			end
		elseif page_dep == 1 then
			if B738DR_fmc_exec_lights == 1 then
				if legs_num > 1 then
					if rte_exec == 1 then
						legs_delete = 0
						legs_intdir = 0
						rte_exec = 0
						copy_to_legsdata2(rte12_sel)
					end
					if legs_delete == 1 then
						legs_delete = 0
						legs_intdir = 0
						copy_to_legsdata2(rte12_sel)
					end
				end
				if legs_intdir == 1 and legs_intdir_rte == rte12_act then
					legs_intdir = 0
					copy_to_legsdata2(rte12_sel)
				end
				
				ref_exec = 0
				des_exec = 0
				
				ref_icao_x = ref_icao
				ref_icao_lat_x = ref_icao_lat
				ref_icao_lon_x = ref_icao_lon
				ref_tns_alt_x = ref_tns_alt
				ref_tns_lvl_x = ref_tns_lvl
				ref_icao_alt_x = ref_icao_alt
				trans_alt_mod = trans_alt
				
				ref_gate_x = ref_gate
				co_route_x = co_route
				flt_num_x = flt_num
				ref_rwy2 = ref_rwy
				ref_rwy3 = ref_rwy2
				
				ref_sid2 = ref_sid
				ref_sid_tns2 = ref_sid_tns
				
				des_icao_x = des_icao
				des_rnw2 = des_rnw
				des_icao_lat_x = des_icao_lat
				des_icao_lon_x = des_icao_lon
				des_tns_alt_x = des_tns_alt
				des_tns_lvl_x = des_tns_lvl
				des_icao_alt_x = des_icao_alt
				trans_lvl_mod = trans_lvl
				
				des_app2 = des_app
				des_app_tns2 = des_app_tns
				des_star2 = des_star
				des_star_trans2 = des_star_trans
				
				arr_data_x = arr_data
				des_app_rw_only_x = des_app_rw_only
				rw_ext_dist_x = rw_ext_dist
				rw_ext_fpa_x = rw_ext_fpa
				
				des_star2_old = des_star2
				des_star_trans2_old = des_star_trans2
				des_app2_old = des_app2
				des_app_tns2_old = des_app_tns2
				
				sel_ref_rwy2 = "-----"
				sel_ref_sid2 = "------"
				sel_ref_sid_tns2 = "------"
				create_rnw_list(rte12_sel)
				create_sid_list(rte12_sel)
			end
		elseif page_arr == 1 then
			if B738DR_fmc_exec_lights == 1 then
				if legs_num > 1 then
					if rte_exec == 1 then
						legs_delete = 0
						legs_intdir = 0
						rte_exec = 0
						copy_to_legsdata2(rte12_sel)
					end
					if legs_delete == 1 then
						legs_delete = 0
						legs_intdir = 0
						copy_to_legsdata2(rte12_sel)
					end
				end
				if legs_intdir == 1 and legs_intdir_rte == rte12_act then
					legs_intdir = 0
					copy_to_legsdata2(rte12_sel)
				end
				
				ref_exec = 0
				des_exec = 0
				
				ref_icao_x = ref_icao
				ref_icao_lat_x = ref_icao_lat
				ref_icao_lon_x = ref_icao_lon
				ref_tns_alt_x = ref_tns_alt
				ref_tns_lvl_x = ref_tns_lvl
				ref_icao_alt_x = ref_icao_alt
				trans_alt_mod = trans_alt
				
				ref_gate_x = ref_gate
				co_route_x = co_route
				flt_num_x = flt_num
				ref_rwy2 = ref_rwy
				ref_rwy3 = ref_rwy2
				
				ref_sid2 = ref_sid
				ref_sid_tns2 = ref_sid_tns
				
				des_icao_x = des_icao
				des_rnw2 = des_rnw
				des_icao_lat_x = des_icao_lat
				des_icao_lon_x = des_icao_lon
				des_tns_alt_x = des_tns_alt
				des_tns_lvl_x = des_tns_lvl
				des_icao_alt_x = des_icao_alt
				trans_lvl_mod = trans_lvl
				
				des_app2 = des_app
				des_app_tns2 = des_app_tns
				des_star2 = des_star
				des_star_trans2 = des_star_trans
				
				arr_data_x = arr_data
				des_app_rw_only_x = des_app_rw_only
				rw_ext_dist_x = rw_ext_dist
				rw_ext_fpa_x = rw_ext_fpa
				
				des_star2_old = des_star2
				des_star_trans2_old = des_star_trans2
				des_app2_old = des_app2
				des_app_tns2_old = des_app_tns2
				
				sel_des_star2 = "------"
				sel_des_star_trans2 = "------"
				sel_des_app2 = "------"
				sel_des_app_tns2 = "------"
			end
		elseif page_rte_init == 1 then
			if B738DR_fmc_exec_lights == 1 then
				if legs_num > 1 and rte12_act == rte12_sel then
					copy_to_legsdata2(rte12_sel)
					erase_modify(rte12_sel)
					
					if rte12_sel == 0 then
						ref_exec = 0
						des_exec = 0
						rte_exec = 0
					else
						ref_exec2 = 0
						des_exec2 = 0
						rte_exec2 = 0
					end
					legs_delete = 0
				elseif legs_num2 > 1 and rte12_sel == 0 then
					-- ACTIVATE/ERASE
					rte_exec = 0
					exec_activate_fpln = 0
					
					copy_to_legs_data2_buf(0)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(0)
				elseif legs_num8 > 1 and rte12_sel == 1 then
					-- ACTIVATE/ERASE
					rte_exec2 = 0
					exec_activate_fpln2 = 0
					
					copy_to_legs_data2_buf(1)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(1)
				end
			else
				if B738DR_no_rte2 == 0 then
					if rte12_sel == 0 then
						rte12_sel = 1
					else
						rte12_sel = 0
					end
					act_page = 1
					item_sel = 0
					item_sel2 = 0
					item_sel_act = 0
					item_sel_via = 0
					item_sel_act2 = 0
					item_sel_via2 = 0
					
					if rte12_sel == 0 then
						legs_step = offset_mod
						if legs_data2[legs_step][1] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step > legs_num2 or legs_step < offset_mod then
								legs_step = offset_mod
							end
							if legs_step > legs_num2 then
								legs_step = legs_num2
							end
						end
					else
						legs_step = offset_mod2
						if legs_data8[legs_step][1] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step > legs_num8 or legs_step < offset_mod2 then
								legs_step = offset_mod2
							end
							if legs_step > legs_num8 then
								legs_step = legs_num8
							end
						end
					end
				end
			end
		elseif page_legs == 1 then
			local ww = 0
			local uu = 0
			-- cancel MOD
			if B738DR_fmc_exec_lights == 1 then
				if legs_num > 1 and rte12_act == rte12_sel then
					copy_to_legsdata2(rte12_sel)
					erase_modify(rte12_sel)
					
					if rte12_sel == 0 then
						ref_exec = 0
						des_exec = 0
						rte_exec = 0
					else
						ref_exec2 = 0
						des_exec2 = 0
						rte_exec2 = 0
					end
					legs_delete = 0
					
					if rte12_act == rte12_sel then
						legs_abeam_data_n = 0
						legs_abeam_data = {}
						legs_abeam_selected = 0
						B738DR_legs_abeam_avail = 0
						B738DR_legs_abeam_num = 0
					end
				elseif legs_num2 > 1 and rte12_sel == 0 then
					-- ACTIVATE/ERASE
					rte_exec = 0
					exec_activate_fpln = 0
					
					copy_to_legs_data2_buf(0)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(0)
				elseif legs_num8 > 1 and rte12_sel == 1 then
					-- ACTIVATE/ERASE
					rte_exec2 = 0
					exec_activate_fpln2 = 0
					
					copy_to_legs_data2_buf(1)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(1)
				end
				if legs_intdir == 1 and rte12_act == rte12_sel and legs_intdir_rte == rte12_act then
					legs_intdir = 0
					legs_intdir_crs2 = -1
					copy_to_legsdata2(rte12_sel)
				end
			else
				if new_hold == 1 and rte12_sel == 0 then
					if new_hold_at == 0 then
						if item_sel == 0 then
							item_sel = find_act_route_wpt2(entry, offset)
							if item_sel == -1 then
								item_sel = 0
								rte_add_hold_wpt(entry, 0, rte12_sel)
							end
						end
						if item_sel ~= 0 then
							new_hold_idx = item_sel
							if hold_data_num2 > 0 then
								for ww = 1, hold_data_num2 do
									if hold_data2[ww] == new_hold_idx + 1 then
										uu = 1
									end
								end
							end
							if legs_data2[new_hold_idx][21] == 0 or legs_data2[new_hold_idx][21] == 1 then
								uu = 1
							end
							
							if uu == 0 then
								-- add new hold
								rte_add_hold(item_sel, 0, rte12_sel)
								page_hold = 1
								page_legs = 0
								entry = ""
								if rte12_act == rte12_sel then
									hold_exec = 1
								end
								act_page = 1
								new_hold = 0
								item_sel = 0
								item_sel_act = 0
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				elseif new_hold2 == 1 and rte12_sel == 1 then
					if new_hold_at2 == 0 then
						if item_sel == 0 then
							item_sel = find_act_route_wpt2(entry, offset)
							if item_sel == -1 then
								item_sel = 0
								rte_add_hold_wpt(entry, 0, rte12_sel)
							end
						end
						if item_sel ~= 0 then
							new_hold_idx2 = item_sel
							if hold_data_num4 > 0 then
								for ww = 1, hold_data_num4 do
									if hold_data4[ww] == new_hold_idx2 + 1 then
										uu = 1
									end
								end
							end
							if legs_data8[new_hold_idx2][21] == 0 or legs_data8[new_hold_idx2][21] == 1 then
								uu = 1
							end
							
							if uu == 0 then
								-- add new hold
								rte_add_hold(item_sel, 0, rte12_sel)
								page_hold = 1
								page_legs = 0
								entry = ""
								if rte12_act == rte12_sel then
									hold_exec2 = 1
								end
								act_page = 1
								new_hold2 = 0
								item_sel = 0
								item_sel_act = 0
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				elseif B738DR_no_rte2 == 0 then
					if rte12_sel == 0 then
						rte12_sel = 1
					else
						rte12_sel = 0
					end
					act_page = 1
					item_sel = 0
					item_sel2 = 0
					item_sel_act = 0
					item_sel_via = 0
					item_sel_act2 = 0
					item_sel_via2 = 0
					
					if rte12_sel == 0 then
						legs_step = offset_mod
						if legs_data2[legs_step][1] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step > legs_num2 or legs_step < offset_mod then
								legs_step = offset_mod
							end
							if legs_step > legs_num2 then
								legs_step = legs_num2
							end
						end
					else
						legs_step = offset_mod2
						if legs_data8[legs_step][1] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step > legs_num8 or legs_step < offset_mod2 then
								legs_step = offset_mod2
							end
							if legs_step > legs_num8 then
								legs_step = legs_num8
							end
						end
					end
				end
			end
		elseif page_hold == 1 then
			if hold_exec == 1 then
				if rte12_sel == 0 then
					if B738DR_hold_term == 0 or hold_offset_idx ~= offset or rte12_act ~= rte12_sel then
						-- ERASE
						copy_to_legsdata2(rte12_sel)
						entry = ""
						hold_exec = 0
						new_hold_idx = 0
						new_hold_act_page = 0
						item_sel = 0
						item_sel_act = 0
						item_sel_via = 0
					else
						B738DR_hold_term = 0
						hold_exec = 0
						new_hold_idx = 0
						new_hold_act_page = 0
					end
				else
					if B738DR_hold_term == 0 or hold_offset_idx7 ~= offset or rte12_act ~= rte12_sel then
						-- ERASE
						copy_to_legsdata2(rte12_sel)
						entry = ""
						hold_exec2 = 0
						new_hold_idx2 = 0
						new_hold_act_page2 = 0
						item_sel = 0
						item_sel_act = 0
						item_sel_via = 0
					else
						B738DR_hold_term = 0
						hold_exec2 = 0
						new_hold_idx2 = 0
						new_hold_act_page2 = 0
					end
				end
			else
				-- show Legs page with Hold select
				act_page = 1
				page_hold = 0
				page_legs = 1
				if rte12_sel == 0 then
					new_hold = 1
				else
					new_hold2 = 1
				end
				-- new_hold_wpt = "*****"
			end
		elseif page_ident ~= 0 then
			-- go to Index page
			page_ident = 0
			page_init = 1
			display_update = 1
		elseif page_approach == 1 then
			-- go to Index page
			page_approach = 0
			page_init = 1
			display_update = 1
		elseif page_n1_limit == 1 and disable_N1_6L == 0 then
			if in_flight_mode == 0 then
				-- go to Perf init page
				page_n1_limit = 0
				page_perf = 1
				display_update = 1
			else
				-- select / deselect CLB-1
				if entry == ">DELETE" then
					clb = "<SEL>"
					clb_1 = "     "
					clb_2 = "     "
					entry = ""
				-- elseif clb_1 == "<SEL>" then
					-- clb = "<SEL>"
					-- clb_1 = "     "
					-- clb_2 = "     "
				else
					clb = "     "
					clb_1 = "<SEL>"
					clb_2 = "     "
				end
				sel_clb_thr = 1
				flight_phase_old2 = -1
			end
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
				-- if B738DR_climb_mode ~= 2 then
					-- -- MAX ANGLE
					-- entry = ""
					-- B738DR_climb_mode = 2
					-- B738DR_fmc_climb_speed_mach = 0.74	--0.76		-- temporary
					-- if B738DR_pfd_flaps_up == 0 then
						-- B738DR_fmc_climb_speed = 250	--270
					-- else
						-- B738DR_fmc_climb_speed = math.max(B738DR_pfd_flaps_up + 20, 250)
					-- end
				-- end
				if fmc_climb_mode_mod ~= 2 then
					-- MAX ANGLE
					entry = ""
					fmc_climb_mode_mod = 2
					fmc_climb_speed_mach_mod = 0.74	--0.76		-- temporary
					if B738DR_pfd_flaps_up == 0 then
						fmc_climb_speed_mod = 250	--270
					else
						fmc_climb_speed_mod = math.max(B738DR_pfd_flaps_up + 20, 250)
					end
					clb_spd_exec = 1
				end
			end
		elseif page_perf ~= 0 then
			if page_perf == 1 and perf_exec == 3 then
				gw_mod = gw
				gw_lbs_mod = gw_lbs
				gw_kgs_mod = gw_kgs
				zfw_mod = zfw
				zfw_lbs_mod = zfw_lbs
				zfw_kgs_mod = zfw_kgs
				plan_weight_mod = plan_weight
				plan_weight_kgs_mod = plan_weight_kgs
				plan_weight_lbs_mod = plan_weight_lbs
				reserves_mod = reserves
				reserves_kgs_mod = reserves_kgs
				reserves_lbs_mod = reserves_lbs
				cost_index_mod = cost_index
				crz_wind_dir_mod = crz_wind_dir
				crz_wind_spd_mod = crz_wind_spd
				isa_dev_f_mod = isa_dev_f
				isa_dev_c_mod = isa_dev_c
				tc_oat_c_mod = tc_oat_c
				tc_oat_f_mod = tc_oat_f
				perf_exec = 2
				if crz_exec ~= 0 then
					crz_alt = crz_alt_old
					crz_exec = 0
					exec1_light = 0
					crz_alt_num2 = 0
					crz_alt_old = "     "
				end
			elseif crz_exec ~= 0 then
				crz_alt = crz_alt_old
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
			else
				-- go to Index page
				page_perf = 0
				page_init = 1
			end
			display_update = 1
		elseif page_pos_init == 1 or page_pos_init == 3 then
			-- go to Index page
			page_pos_init = 0
			page_init = 1
			display_update = 1
		elseif page_descent == 1 then
			page_descent = 0
			page_descent_forecast = 1
		elseif page_init == 1 then
			-- go to OFFSET
			page_init = 0
			page_offset = 1
		elseif page_ref_nav_data == 1 then
			-- go to INDEX
			page_ref_nav_data = 0
			page_init = 1
		elseif page_ref_nav_data_wpt == 1 then
			-- go to INDEX
			page_ref_nav_data_wpt = 0
			page_init = 1
		elseif page_offset == 1 and (offset_act_mod >= 1 and offset_act_mod <= 4) then
			offset_dist_mod = offset_dist
			offset_side_mod = offset_side
			offset_start_mod = offset_start
			offset_end_mod = offset_end
			offset_start_def_mod = offset_start_def
			offset_end_def_mod = offset_end_def
			offset_act_mod = offset_act
			legs_delete = 0
		elseif page_ac == 1 then
			-- go to MENU
			page_ac = 0
			page_menu = 1
			display_update = 1
		elseif page_ac_atis == 1 then
			-- go to ACARS menu
			page_ac = 1
			page_ac_atis = 0
			display_update = 1
		elseif page_ac_msg == 1 then
			page_ac_msg = 0
			page_ac_atis = 1
		elseif page_altern_apt == 1 then
			page_altern_apt = 0
			page_init = 1
			act_page = 1
		elseif page_nearest_apt == 1 then
			page_nearest_apt = 0
			page_init = 1
			act_page = 1
		elseif page_nav_status > 0 then
			page_init = 1
			page_nav_status = 0
			act_page = 1
		elseif page_progress == 4 then
			local len_entry = string.len(entry)
			local num_entry = tonumber(entry)
			if entry == ">DELETE" then
				app_rnp = ""
				app_rnp_num = 0.30
				entry = ""
			elseif len_entry < 5 then
				if num_entry == nil then
					add_fmc_msg(INVALID_INPUT, 1)
				elseif num_entry >= 0.05 and num_entry <= 1 then
					app_rnp = string.format("%04.2f", num_entry)
					app_rnp_num = tonumber(app_rnp)
					entry = ""
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_dl_acars == 1 then
			page_dl_acars = 0
			page_dl_menu = 1
			act_page = 1
			display_update = 1
		elseif page_dl_requests == 1 then
			page_dl_requests = 0
			page_dl_acars = 1
			act_page = 1
			display_update = 1
		elseif page_dl_wxr_req == 1 then
			page_dl_wxr_req = 0
			page_dl_requests = 1
			act_page = 1
			display_update = 1
		elseif page_dl_wxr_info == 1 then
			page_dl_wxr_info = 0
			page_dl_requests = 1
			act_page = 1
			display_update = 1
		elseif page_dl_fltlog == 1 then
			page_dl_fltlog = 0
			page_dl_acars = 1
			act_page = 1
			display_update = 1
		elseif page_dl_sensors == 1 then
			page_dl_sensors = 0
			page_dl_fltlog = 1
			act_page = 1
			display_update = 1
		elseif page_descent_forecast == 1 then
			if rte12_act == 0 then
				if wind_data_des_num == 0 then
					entry = ">NONE WIND DATA"
				else
					uplink_wind_des(rte12_act)
				end
			elseif rte12_act == 1 then
				if wind_data_des_num7 == 0 then
					entry = ">NONE WIND DATA"
				else
					uplink_wind_des(rte12_act)
				end
			end
		elseif page_flt_plns == 1 then
			-- go to INDEX
			page_flt_plns = 0
			page_init = 1
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 1RSK
function B738_fmc1_1R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep == 1 then
			if rte12_sel == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
				if sel_ref_rwy2 == "-----" then
					if ref_rwy_sel[1] ~= "" then
						if ref_rwy2 == ref_rwy_sel[1] then
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
						else
							ref_rwy2 = ref_rwy_sel[1]
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							ref_rwy3 = ref_rwy2
							ref_rwy_exec = 1
						end
						act_page = 1
					end
				else
					ref_rwy2 = "-----"
					ref_rwy3 = "-----"
					sel_ref_rwy2 = ref_rwy2
					create_sid_list(rte12_sel)
					create_rnw_list(rte12_sel)
					act_page = 1
					ref_rwy_exec = 1
				end
				rte_add_dep_arr(rte12_sel)
				
				-- check SID for selected runway
				if rte12_act == rte12_sel then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			elseif rte12_sel == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
				if sel_ref_rwy8 == "-----" then
					if ref_rwy_sel[1] ~= "" then
						if ref_rwy8 == ref_rwy_sel[1] then
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
						else
							ref_rwy8 = ref_rwy_sel[1]
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							ref_rwy9 = ref_rwy8
							ref_rwy_exec = 1
						end
						act_page = 1
					end
				else
					ref_rwy8 = "-----"
					ref_rwy9 = "-----"
					sel_ref_rwy8 = ref_rwy8
					create_sid_list(rte12_sel)
					create_rnw_list(rte12_sel)
					act_page = 1
					ref_rwy_exec = 1
				end
				rte_add_dep_arr(rte12_sel)
				
				-- check SID for selected runway
				if rte12_act == rte12_sel then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			end
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_app2 == "------" then
					if des_app_sel[1] ~= "" then
						if des_app2 == des_app_sel[1] then
							if app_list_num == 1 then
								des_app2 = "------"
								sel_des_app2 = des_app2
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								create_app_list(rte12_sel)
								act_page = 1
								des_app_exec = 1
							else
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							end
						else
							des_app2 = des_app_sel[1]
							sel_des_app2 = des_app2
							if string.sub(sel_des_app2, 1, 2) == "RW" then
								des_app_rw_only_x = 1
								des_star_trans2 = "------"
								sel_des_star_trans2 = des_star_trans2
								create_star_list(rte12_sel)
							else
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
							end
							if sel_des_star2 == "------" then
								create_star_list(rte12_sel)
							end
							des_app_tns2 = "------"
							sel_des_app_tns2 = des_app_tns2
							des_app_exec = 1
							create_app_tns_list(rte12_sel)
						end
						act_page = 1
					end
				else
					des_app2 = "------"
					sel_des_app2 = des_app2
					des_app_rw_only_x = 0
					rw_ext_dist_x = "--.-"
					rw_ext_fpa_x = "-.--"
					if sel_des_star2 == "------" then
						create_star_list(rte12_sel)
					end
					des_app_tns2 = "------"
					sel_des_app_tns2 = des_app_tns2
					create_app_list(rte12_sel)
					act_page = 1
					des_app_exec = 1
				end
			else
				if sel_des_app8 == "------" then
					if des_app_sel[1] ~= "" then
						if des_app8 == des_app_sel[1] then
							if app_list_num == 1 then
								des_app8 = "------"
								sel_des_app8 = des_app8
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								create_app_list(rte12_sel)
								act_page = 1
								des_app_exec = 1
							else
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							end
						else
							des_app8 = des_app_sel[1]
							sel_des_app8 = des_app8
							if string.sub(sel_des_app8, 1, 2) == "RW" then
								des_app_rw_only7_x = 1
								des_star_trans8 = "------"
								sel_des_star_trans8 = des_star_trans8
								create_star_list(rte12_sel)
							else
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
							end
							if sel_des_star8 == "------" then
								create_star_list(rte12_sel)
							end
							des_app_tns8 = "------"
							sel_des_app_tns8 = des_app_tns8
							des_app_exec = 1
							create_app_tns_list(rte12_sel)
						end
						act_page = 1
					end
				else
					des_app8 = "------"
					sel_des_app8 = des_app8
					des_app_rw_only7_x = 0
					rw_ext_dist7_x = "--.-"
					rw_ext_fpa7_x = "-.--"
					if sel_des_star8 == "------" then
						create_star_list(rte12_sel)
					end
					des_app_tns8 = "------"
					sel_des_app_tns8 = des_app_tns8
					create_app_list(rte12_sel)
					act_page = 1
					des_app_exec = 1
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_dep_arr == 1 then
			-- Reference ARR
			if ref_icao_x ~= "----" and des_icao_x ~= "****" and ref_icao_x ~= "ORGN" then
				
				local apt_ok = 0
				local xy = 0
				
				rte12_sel = 0
				
				if arr_data_x == 0 then
					-- DES ICAO
					if legs_num2 > 1 then	--and des_icao_x ~= des_icao then
						-- -- delete all STAR/APP
						if first_star_idx_mod > 0 and offset < first_star_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_star_idx_mod
							if first_star_idx_mod ~= 0 and first_star_idx_mod < legs_num2 then
								if legs_data2[first_star_idx_mod][19] == 4 then
									apt_ok = first_star_idx_mod + 1
									legs_data2[first_star_idx_mod][19] = 0
									legs_data2[first_star_idx_mod][4] = 0
									legs_data2[first_star_idx_mod][5] = 0
									legs_data2[first_star_idx_mod][6] = 0
									legs_data2[first_star_idx_mod][9] = ""
									legs_data2[first_star_idx_mod][21] = -1
									legs_data2[first_star_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						elseif first_app_idx_mod > 0 and offset < first_app_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_app_idx_mod
							if first_app_idx_mod ~= 0 and first_app_idx_mod < legs_num2 then
								if legs_data2[first_app_idx_mod][19] == 9 then
									apt_ok = first_app_idx_mod + 1
									legs_data2[first_app_idx_mod][19] = 0
									legs_data2[first_app_idx_mod][4] = 0
									legs_data2[first_app_idx_mod][5] = 0
									legs_data2[first_app_idx_mod][6] = 0
									legs_data2[first_app_idx_mod][9] = ""
									legs_data2[first_app_idx_mod][21] = -1
									legs_data2[first_app_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						elseif last_sid_idx_mod > 0 and offset < last_sid_idx_mod and last_sid_idx_mod <= legs_num2 then
							clr_star_app(0)
							rte_copy(legs_num2 + 1, 0)
							rte_paste(last_sid_idx_mod + 1, 0)
							calc_rte_enable2 = 1
						elseif offset <= legs_num2 then
							clr_star_app(0)
							rte_copy(legs_num2 + 1, 0)
							rte_paste(offset, 0)
							calc_rte_enable2 = 1
						end
						last_sid_idx_mod = legs_num2
						if rte12_act == rte12_sel then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num2 + 1
						legs_data2[apt_ok] = {}
						legs_data2[apt_ok][1] = ref_icao_x
						legs_data2[apt_ok][2] = 0		-- brg
						legs_data2[apt_ok][3] = 0		-- distance
						legs_data2[apt_ok][4] = 0		-- speed
						legs_data2[apt_ok][5] = 0		-- altitude
						legs_data2[apt_ok][6] = 0	-- altitude type
						legs_data2[apt_ok][7] = ref_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[apt_ok][8] = ref_icao_lon_x	--des_icao_lon		-- longitude
						legs_data2[apt_ok][9] = ""			-- via id
						legs_data2[apt_ok][10] = 0		-- calc speed
						legs_data2[apt_ok][11] = 0		-- calc altitude
						legs_data2[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data2[apt_ok][13] = 0
						legs_data2[apt_ok][14] = 0		-- rest alt
						legs_data2[apt_ok][15] = 0		-- last fuel
						legs_data2[apt_ok][16] = ""
						legs_data2[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data2[apt_ok][20] = 0
						legs_data2[apt_ok][21] = -1
						legs_data2[apt_ok][22] = ""
						legs_data2[apt_ok][23] = 0
						legs_data2[apt_ok][24] = 0
						legs_data2[apt_ok][25] = 0
						legs_data2[apt_ok][26] = 0
						legs_data2[apt_ok][27] = ""
						legs_data2[apt_ok][28] = ""
						legs_data2[apt_ok][29] = ""
						legs_data2[apt_ok][30] = ""
						legs_data2[apt_ok][31] = ""
						legs_data2[apt_ok][32] = 0
						legs_data2[apt_ok][33] = ""
						legs_data2[apt_ok][34] = "DEST"
						legs_data2[apt_ok][35] = ""
						legs_data2[apt_ok][36] = 9
						legs_data2[apt_ok][37] = 0
						legs_data2[apt_ok][38] = ""
						legs_data2[apt_ok][39] = ""
						legs_data2[apt_ok][40] = 0
						legs_data2[apt_ok][41] = 0
						legs_data2[apt_ok][42] = 0
						legs_data2[apt_ok][43] = ""
						legs_data2[apt_ok][44] = 0
						legs_data2[apt_ok][45] = 0
						legs_data2[apt_ok][46] = 0
						legs_data2[apt_ok][47] = 0
						legs_data2[apt_ok][48] = ""
						legs_data2[apt_ok][49] = 0
						legs_data2[apt_ok][50] = 0
						legs_data2[apt_ok][51] = 0
						legs_data2[apt_ok][52] = 0
						legs_data2[apt_ok][53] = 0
						legs_data2[apt_ok][54] = 0
						
						if rte12_act == 0 then
							offset_mod = offset
						else
							offset_mod = 2
						end
						if offset_mod > legs_num2 then
							offset_mod = legs_num2
						end
						
					elseif legs_num2 > 0 then
						legs_data2[legs_num2 + 1][1] = ref_icao_x	--des_icao_x
						legs_data2[legs_num2 + 1][7] = ref_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[legs_num2 + 1][8] = ref_icao_lon_x	--des_icao_lon		-- longitude
					end
					
					if rte12_act == 0 then
						legs_delete = 1
					end
					des_app_rw_only_x = 0
					des_star2 = "------"
					des_star_trans2 = "------"
					des_app2 = "------"
					des_app_tns2 = "------"
				end
				arr_data_x = 1
				page_arr = 1
				find_ref_rnw_data(1, 0)
				read_ref_list_icao(ref_icao_x, 0)
				read_des_list_icao(ref_icao_x, 0)
				page_dep_arr = 0
				
				sel_des_star2 = "------"
				sel_des_star_trans2 = "------"
				sel_des_app2 = "------"
				sel_des_app_tns2 = "------"
				
				create_star_list(0)
				create_star_tns_list(0)
				create_app_list(0)
				create_app_tns_list(0)
				
			end
		elseif page_rte_init == 1 then
			if act_page == 1 then
				-- entry Destination airport ICAO
				local xy = 0
				local apt_ok = 0
				local dest_available = 1
				
				if dest_available ~= 0 then
					if entry == ">DELETE" then
						entry = INVALID_DELETE
					else
						if rte12_sel == 0 then
							if string.len(entry) == 4 then
								des_app_from_apt = 0
								
								if des_apt_exist(entry, 0) then
									des_icao_x = entry
									update_rnw_list(0)
									
									des_tns_alt_x = 0
									des_tns_lvl_x = 0
									des_icao_alt_x = 0
									
									des_app2 = "------"
									des_app_tns2 = "------"
									des_star2 = "------"
									des_star_trans2 = "------"
									
									trans_lvl_mod = "-----"
									
									if rte12_act ~= 0 then
										des_icao = "****"
										des_star = "------"
										des_star_trans = "------"
										des_app = "------"
										des_app_tns = "------"
										rw_ext_dist_x = "--.-"
										rw_ext_fpa_x = "-.--"
										
										des_icao_lat = 0
										des_icao_lon = 0
										des_tns_alt = 0
										des_tns_lvl = 0
										des_icao_alt = 0
										arr_data = 0
										des_app_rw_only = 0
										trans_lvl = "-----"
									end
									
									if apt_exist(entry) == true then
										des_icao_lat_x = icao_latitude
										des_icao_lon_x = icao_longitude
										des_tns_alt_x = icao_tns_alt
										des_tns_lvl_x = icao_tns_lvl
										des_icao_alt_x = 2500
										B738DR_find_icao = entry
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt_x = B738DR_find_icao_height * 3.280839895
										end
									else
										des_icao_alt_x = 2500
										B738DR_find_icao = entry
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt_x = B738DR_find_icao_height * 3.280839895
										end
										des_tns_alt_x = 0
										des_tns_lvl_x = 0
									end
									if des_tns_lvl_x == 0 then
										des_tns_lvl_x = des_tns_alt_x
									end
									if des_tns_lvl_x == 0 then
										trans_lvl_mod = "-----"
									else
										apt_ok = des_tns_lvl_x / 100
										trans_lvl_mod = "FL" .. string.format("%03d", apt_ok)
									end
									
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
									
									if legs_num ~= 0 and rte12_act == rte12_sel then
										des_exec = 1
									end
									
									des_app2 = "------"
									des_app_tns2 = "------"
									des_star2 = "------"
									des_star_trans2 = "------"
									
									-- if ref_icao_x ~= "----" then
										-- dump_leg_append(legs_data2[1][1] .. "\n")
									-- end
									
									if ref_icao_x == "----" then
										ref_icao_x = "ORGN"
										ref_icao_lat_x = simDR_latitude
										ref_icao_lon_x = simDR_longitude
										ref_gate_x = ref_gate
										ref_tns_alt_x = 0
										ref_tns_lvl_x = 0
										trans_alt_mod = "-----"
										ref_icao_alt_x = simDR_altitude_pilot
										ref_gate_x = "-----"
										
										ref_rwy2 = "-----"
										ref_sid2 = "------"
										ref_sid_tns2 = "------"
										
										ref_rnw_list = {}
										ref_rnw_list_num = 0
										ref_rnw_list2 = {}
										ref_rnw_list_num2 = 0
										sid_list = {}
										sid_list_num = 0
									end
									
									if legs_num2 == 0 then
										if ref_icao_x == "ORGN" then
											-- REF ICAO
											legs_data2[1] = {}
											legs_data2[1][1] = ref_icao_x
											legs_data2[1][2] = 0		-- brg
											legs_data2[1][3] = 0		-- distance
											legs_data2[1][4] = 0		-- speed
											legs_data2[1][5] = ref_icao_alt_x		-- altitude
											legs_data2[1][6] = 0	-- altitude type
											legs_data2[1][7] = ref_icao_lat_x		-- latitude
											legs_data2[1][8] = ref_icao_lon_x	--ref_icao_lon		-- longitude
											legs_data2[1][9] = ""			-- via id
											legs_data2[1][10] = 0		-- calc speed
											legs_data2[1][11] = ref_icao_alt_x		-- calc altitude
											legs_data2[1][12] = 0		-- calc altitude vnav pth
											legs_data2[1][13] = 0
											legs_data2[1][14] = 0		-- rest alt
											legs_data2[1][15] = 0		-- last fuel
											legs_data2[1][16] = ""
											legs_data2[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
											legs_data2[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
											legs_data2[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
											legs_data2[1][20] = 0
											legs_data2[1][21] = -1
											legs_data2[1][22] = ""
											legs_data2[1][23] = 0
											legs_data2[1][24] = 0
											legs_data2[1][25] = 0
											legs_data2[1][26] = 0
											legs_data2[1][27] = ""
											legs_data2[1][28] = ""
											legs_data2[1][29] = ""
											legs_data2[1][30] = ""
											legs_data2[1][31] = ""
											legs_data2[1][32] = 0
											legs_data2[1][33] = ""
											legs_data2[1][34] = ""
											legs_data2[1][35] = ""
											legs_data2[1][36] = 9
											legs_data2[1][37] = 0
											legs_data2[1][38] = ""
											legs_data2[1][39] = ""
											legs_data2[1][40] = 0
											legs_data2[1][41] = 0
											legs_data2[1][42] = 0
											legs_data2[1][43] = ""
											legs_data2[1][44] = 0
											legs_data2[1][45] = 0
											legs_data2[1][46] = 0
											legs_data2[1][47] = 0
											legs_data2[1][48] = ""
											legs_data2[1][49] = 0
											legs_data2[1][50] = 0
											legs_data2[1][51] = 0
											legs_data2[1][52] = 0
											legs_data2[1][53] = 0
											legs_data2[1][54] = 0
										end
										legs_num2 = 1
										apt_ok = 2
									else
										if offset_mod == 0 then
											apt_ok = 1
										else
											if offset_mod < first_star_idx_mod then
												rte_copy(legs_num2 + 1, rte12_sel)
												apt_ok = first_star_idx_mod
												if first_star_idx_mod ~= 0 and first_star_idx_mod < legs_num2 then
													if legs_data2[first_star_idx_mod][19] == 4 then
														apt_ok = first_star_idx_mod + 1
														legs_data2[first_star_idx_mod][19] = 0
														legs_data2[first_star_idx_mod][4] = 0
														legs_data2[first_star_idx_mod][5] = 0
														legs_data2[first_star_idx_mod][6] = 0
														legs_data2[first_star_idx_mod][9] = ""
														legs_data2[first_star_idx_mod][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel)
											elseif offset_mod < first_app_idx_mod then
												rte_copy(legs_num2 + 1, rte12_sel)
												apt_ok = first_app_idx_mod
												if first_app_idx_mod ~= 0 and first_app_idx_mod < legs_num2 then
													if legs_data2[first_app_idx_mod][19] == 9 then
														apt_ok = first_app_idx_mod + 1
														legs_data2[first_app_idx_mod][19] = 0
														legs_data2[first_app_idx_mod][4] = 0
														legs_data2[first_app_idx_mod][5] = 0
														legs_data2[first_app_idx_mod][6] = 0
														legs_data2[first_app_idx_mod][9] = ""
														legs_data2[first_app_idx_mod][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel)
											elseif first_app_idx_mod ~= 0 then
												rte_copy(legs_num2 + 1, rte12_sel)
												rte_paste(offset_mod + 1, rte12_sel)
											elseif first_star_idx_mod ~= 0 then
												rte_copy(legs_num2 + 1, rte12_sel)
												rte_paste(offset_mod + 1, rte12_sel)
											end
											
											apt_ok = offset_mod
										
										end
										
										if ref_icao_x == "ORGN" then
											apt_ok = 1
										end
										
										if apt_ok <= legs_num2 then
											for xy = apt_ok, legs_num2 do
											-- change STAR,APP to route
												if legs_data2[xy][19] == 2 or legs_data2[xy][19] == 4
												or legs_data2[xy][19] == 7 or legs_data2[xy][19] == 8 
												or legs_data2[xy][19] == 9 then
													legs_data2[xy][19] = 0	-- wpt type STAR, APP
													legs_data2[xy][4] = 0	-- spd rest
													legs_data2[xy][5] = 0	-- alt rest
													legs_data2[xy][6] = 0	-- alt type rest
													legs_data2[xy][9] = ""		--via
												elseif (legs_data2[xy][19] == 1 or legs_data2[xy][19] == 3) then
													legs_data2[xy][19] = 0	-- wpt type STAR, APP
													legs_data2[xy][9] = ""		--via
												end
											end
										end
										last_sid_idx_mod = 0
										if rte12_act == rte12_sel then
											if rte12_act == 0 then
												idx_ed_mod = 0
												rnav_idx_first_mod = 0
												rnav_idx_last_mod = 0
											else
												idx_ed_mod2 = 0
												rnav_idx_first_mod2 = 0
												rnav_idx_last_mod2 = 0
											end
										end
										apt_ok = legs_num2 + 1
									end
									
									-- DES ICAO
									legs_data2[apt_ok] = {}
									legs_data2[apt_ok][1] = des_icao_x
									legs_data2[apt_ok][2] = 0		-- brg
									legs_data2[apt_ok][3] = 0		-- distance
									legs_data2[apt_ok][4] = 0		-- speed
									legs_data2[apt_ok][5] = 0		-- altitude
									legs_data2[apt_ok][6] = 0	-- altitude type
									legs_data2[apt_ok][7] = des_icao_lat_x		-- latitude
									legs_data2[apt_ok][8] = des_icao_lon_x		-- longitude
									legs_data2[apt_ok][9] = ""			-- via id
									legs_data2[apt_ok][10] = 0		-- calc speed
									legs_data2[apt_ok][11] = 0		-- calc altitude
									legs_data2[apt_ok][12] = 0		-- calc altitude vnav pth
									legs_data2[apt_ok][13] = 0
									legs_data2[apt_ok][14] = 0		-- rest alt
									legs_data2[apt_ok][15] = 0		-- last fuel
									legs_data2[apt_ok][16] = ""
									legs_data2[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
									legs_data2[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data2[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data2[apt_ok][20] = 0
									legs_data2[apt_ok][21] = -1
									legs_data2[apt_ok][22] = ""
									legs_data2[apt_ok][23] = 0
									legs_data2[apt_ok][24] = 0
									legs_data2[apt_ok][25] = 0
									legs_data2[apt_ok][26] = 0
									legs_data2[apt_ok][27] = ""
									legs_data2[apt_ok][28] = ""
									legs_data2[apt_ok][29] = ""
									legs_data2[apt_ok][30] = ""
									legs_data2[apt_ok][31] = ""
									legs_data2[apt_ok][32] = 0
									legs_data2[apt_ok][33] = ""
									legs_data2[apt_ok][34] = "DEST"
									legs_data2[apt_ok][35] = ""
									legs_data2[apt_ok][36] = 9
									legs_data2[apt_ok][37] = 0
									legs_data2[apt_ok][38] = ""
									legs_data2[apt_ok][39] = ""
									legs_data2[apt_ok][40] = 0
									legs_data2[apt_ok][41] = 0
									legs_data2[apt_ok][42] = 0
									legs_data2[apt_ok][43] = ""
									legs_data2[apt_ok][44] = 0
									legs_data2[apt_ok][45] = 0
									legs_data2[apt_ok][46] = 0
									legs_data2[apt_ok][47] = 0
									legs_data2[apt_ok][48] = ""
									legs_data2[apt_ok][49] = 0
									legs_data2[apt_ok][50] = 0
									legs_data2[apt_ok][51] = 0
									legs_data2[apt_ok][52] = 0
									legs_data2[apt_ok][53] = 0
									legs_data2[apt_ok][54] = 0
									
									if rte12_act == rte12_sel then
										offset_mod = offset
									else
										offset_mod = 2
									end
									if offset_mod > legs_num2 then
										offset_mod = legs_num2
									end
									
									if legs_num ~= 0 then
										if rte12_act == rte12_sel then
											legs_delete = 1
										end
										calc_rte_enable2 = 1
									end
									
									if exec_activate_fpln ~= 0 then
										rte_exec = 0
										exec_activate_fpln = 0
										
										copy_to_legs_data2_buf(0)
										if legs_num2_buf > 1 then
											for n = 1, (legs_num2_buf + 1) do
												legs_data2_buf[n][11] = 0
												legs_data2_buf[n][10] = 0
												legs_data2_buf[n][14] = 50000
												legs_data2_buf[n][38] = ""
											end
										end
										copy_from_legs_data2_buf(0)
										
									end
									
									create_fpln(rte12_sel)
									
									arr_data_x = 0
									find_des_rnw_data(1, 0)
									read_des_list_icao(des_icao_x, 0)
									create_rnw_list(rte12_sel)
									create_star_list(rte12_sel)
									create_app_list(rte12_sel)
									
									entry = ""
									
								else
									add_fmc_msg(NOT_IN_DATABASE, 1)
								end
							elseif entry == "" and des_icao_x ~= "****" then
								entry = des_icao_x
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							if string.len(entry) == 4 then
								des_app_from_apt7 = 0
								
								if des_apt_exist(entry, 1) then
									des_icao7_x = entry
									update_rnw_list(1)
									
									des_tns_alt7_x = 0
									des_tns_lvl7_x = 0
									des_icao_alt7_x = 0
									
									des_app8 = "------"
									des_app_tns8 = "------"
									des_star8 = "------"
									des_star_trans8 = "------"
									
									trans_lvl_mod2 = "-----"
									
									if rte12_act ~= 1 then
										des_icao7 = "****"
										des_star7 = "------"
										des_star_trans7 = "------"
										des_app7 = "------"
										des_app_tns7 = "------"
										rw_ext_dist7_x = "--.-"
										rw_ext_fpa7_x = "-.--"
										
										des_icao_lat7 = 0
										des_icao_lon7 = 0
										des_tns_alt7 = 0
										des_tns_lvl7 = 0
										des_icao_alt7 = 0
										arr_data7 = 0
										des_app_rw_only7 = 0
										trans_lvl7 = "-----"
									end
									
									if apt_exist(entry) == true then
										des_icao_lat7_x = icao_latitude
										des_icao_lon7_x = icao_longitude
										des_tns_alt7_x = icao_tns_alt
										des_tns_lvl7_x = icao_tns_lvl
										des_icao_alt7_x = 2500
										B738DR_find_icao = entry
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt7_x = B738DR_find_icao_height * 3.280839895
										end
									else
										des_icao_alt7_x = 2500
										B738DR_find_icao = entry
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt7_x = B738DR_find_icao_height * 3.280839895
										end
										des_tns_alt7_x = 0
										des_tns_lvl7_x = 0
									end
									if des_tns_lvl7_x == 0 then
										des_tns_lvl7_x = des_tns_alt7_x
									end
									if des_tns_lvl7_x == 0 then
										trans_lvl_mod2 = "-----"
									else
										apt_ok = des_tns_lvl7_x / 100
										trans_lvl_mod2 = "FL" .. string.format("%03d", apt_ok)
									end
									
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
									
									if legs_num ~= 0 and rte12_act == rte12_sel then
										des_exec2 = 1
									end
									
									des_app8 = "------"
									des_app_tns8 = "------"
									des_star8 = "------"
									des_star_trans8 = "------"
									
									if ref_icao7_x == "----" then
										ref_icao7_x = "ORGN"
										ref_icao_lat7_x = simDR_latitude
										ref_icao_lon7_x = simDR_longitude
										ref_gate7_x = ref_gate
										ref_tns_alt7_x = 0
										ref_tns_lvl7_x = 0
										trans_alt_mod2 = "-----"
										ref_icao_alt7_x = simDR_altitude_pilot
										ref_gate7_x = "-----"
										
										ref_rwy8 = "-----"
										ref_sid8 = "------"
										ref_sid_tns8 = "------"
										
										ref_rnw_list = {}
										ref_rnw_list_num = 0
										ref_rnw_list2 = {}
										ref_rnw_list_num2 = 0
										sid_list = {}
										sid_list_num = 0
									end
									
									if legs_num8 == 0 then
										if ref_icao7_x == "ORGN" then
											-- REF ICAO
											legs_data8[1] = {}
											legs_data8[1][1] = ref_icao7_x
											legs_data8[1][2] = 0		-- brg
											legs_data8[1][3] = 0		-- distance
											legs_data8[1][4] = 0		-- speed
											legs_data8[1][5] = ref_icao_alt7_x		-- altitude
											legs_data8[1][6] = 0	-- altitude type
											legs_data8[1][7] = ref_icao_lat7_x		-- latitude
											legs_data8[1][8] = ref_icao_lon7_x	--ref_icao_lon		-- longitude
											legs_data8[1][9] = ""			-- via id
											legs_data8[1][10] = 0		-- calc speed
											legs_data8[1][11] = ref_icao_alt7_x		-- calc altitude
											legs_data8[1][12] = 0		-- calc altitude vnav pth
											legs_data8[1][13] = 0
											legs_data8[1][14] = 0		-- rest alt
											legs_data8[1][15] = 0		-- last fuel
											legs_data8[1][16] = ""
											legs_data8[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
											legs_data8[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
											legs_data8[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
											legs_data8[1][20] = 0
											legs_data8[1][21] = -1
											legs_data8[1][22] = ""
											legs_data8[1][23] = 0
											legs_data8[1][24] = 0
											legs_data8[1][25] = 0
											legs_data8[1][26] = 0
											legs_data8[1][27] = ""
											legs_data8[1][28] = ""
											legs_data8[1][29] = ""
											legs_data8[1][30] = ""
											legs_data8[1][31] = ""
											legs_data8[1][32] = 0
											legs_data8[1][33] = ""
											legs_data8[1][34] = ""
											legs_data8[1][35] = ""
											legs_data8[1][36] = 9
											legs_data8[1][37] = 0
											legs_data8[1][38] = ""
											legs_data8[1][39] = ""
											legs_data8[1][40] = 0
											legs_data8[1][41] = 0
											legs_data8[1][42] = 0
											legs_data8[1][43] = ""
											legs_data8[1][44] = 0
											legs_data8[1][45] = 0
											legs_data8[1][46] = 0
											legs_data8[1][47] = 0
											legs_data8[1][48] = ""
											legs_data8[1][49] = 0
											legs_data8[1][50] = 0
											legs_data8[1][51] = 0
											legs_data8[1][52] = 0
											legs_data8[1][53] = 0
											legs_data8[1][54] = 0
										end
										legs_num8 = 1
										apt_ok = 2
									else
										if offset_mod2 == 0 then
											apt_ok = 1
										else
											if offset_mod2 < first_star_idx_mod2 then
												rte_copy(legs_num8 + 1, rte12_sel)
												apt_ok = first_star_idx_mod2
												if first_star_idx_mod2 ~= 0 and first_star_idx_mod2 < legs_num8 then
													if legs_data8[first_star_idx_mod2][19] == 4 then
														apt_ok = first_star_idx_mod2 + 1
														legs_data8[first_star_idx_mod2][19] = 0
														legs_data8[first_star_idx_mod2][4] = 0
														legs_data8[first_star_idx_mod2][5] = 0
														legs_data8[first_star_idx_mod2][6] = 0
														legs_data8[first_star_idx_mod2][9] = ""
														legs_data8[first_star_idx_mod2][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel)
											elseif offset_mod2 < first_app_idx_mod2 then
												rte_copy(legs_num8 + 1, rte12_sel)
												apt_ok = first_app_idx_mod2
												if first_app_idx_mod2 ~= 0 and first_app_idx_mod2 < legs_num8 then
													if legs_data8[first_app_idx_mod2][19] == 9 then
														apt_ok = first_app_idx_mod2 + 1
														legs_data8[first_app_idx_mod2][19] = 0
														legs_data8[first_app_idx_mod2][4] = 0
														legs_data8[first_app_idx_mod2][5] = 0
														legs_data8[first_app_idx_mod2][6] = 0
														legs_data8[first_app_idx_mod2][9] = ""
														legs_data8[first_app_idx_mod2][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel)
											elseif first_app_idx_mod2 ~= 0 then
												rte_copy(legs_num8 + 1, rte12_sel)
												rte_paste(offset_mod2 + 1, rte12_sel)
											elseif first_star_idx_mod2 ~= 0 then
												rte_copy(legs_num8 + 1, rte12_sel)
												rte_paste(offset_mod2 + 1, rte12_sel)
											end
											
											apt_ok = offset_mod2
										
										end
										
										if ref_icao7_x == "ORGN" then
											apt_ok = 1
										end
										
										if apt_ok <= legs_num8 then
											for xy = apt_ok, legs_num8 do
											-- change STAR,APP to route
												if legs_data8[xy][19] == 2 or legs_data8[xy][19] == 4
												or legs_data8[xy][19] == 7 or legs_data8[xy][19] == 8 
												or legs_data8[xy][19] == 9 then
													legs_data8[xy][19] = 0	-- wpt type STAR, APP
													legs_data8[xy][4] = 0	-- spd rest
													legs_data8[xy][5] = 0	-- alt rest
													legs_data8[xy][6] = 0	-- alt type rest
													legs_data8[xy][9] = ""		--via
												elseif (legs_data8[xy][19] == 1 or legs_data8[xy][19] == 3) then
													legs_data8[xy][19] = 0	-- wpt type STAR, APP
													legs_data8[xy][9] = ""		--via
												end
											end
										end
										last_sid_idx_mod2 = 0
										if rte12_act == rte12_sel then
											if rte12_act == 0 then
												idx_ed_mod = 0
												rnav_idx_first_mod = 0
												rnav_idx_last_mod = 0
											else
												idx_ed_mod2 = 0
												rnav_idx_first_mod2 = 0
												rnav_idx_last_mod2 = 0
											end
										end
										apt_ok = legs_num8 + 1
									end
									
									-- DES ICAO
									legs_data8[apt_ok] = {}
									legs_data8[apt_ok][1] = des_icao7_x
									legs_data8[apt_ok][2] = 0		-- brg
									legs_data8[apt_ok][3] = 0		-- distance
									legs_data8[apt_ok][4] = 0		-- speed
									legs_data8[apt_ok][5] = 0		-- altitude
									legs_data8[apt_ok][6] = 0	-- altitude type
									legs_data8[apt_ok][7] = des_icao_lat7_x		-- latitude
									legs_data8[apt_ok][8] = des_icao_lon7_x		-- longitude
									legs_data8[apt_ok][9] = ""			-- via id
									legs_data8[apt_ok][10] = 0		-- calc speed
									legs_data8[apt_ok][11] = 0		-- calc altitude
									legs_data8[apt_ok][12] = 0		-- calc altitude vnav pth
									legs_data8[apt_ok][13] = 0
									legs_data8[apt_ok][14] = 0		-- rest alt
									legs_data8[apt_ok][15] = 0		-- last fuel
									legs_data8[apt_ok][16] = ""
									legs_data8[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
									legs_data8[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data8[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data8[apt_ok][20] = 0
									legs_data8[apt_ok][21] = -1
									legs_data8[apt_ok][22] = ""
									legs_data8[apt_ok][23] = 0
									legs_data8[apt_ok][24] = 0
									legs_data8[apt_ok][25] = 0
									legs_data8[apt_ok][26] = 0
									legs_data8[apt_ok][27] = ""
									legs_data8[apt_ok][28] = ""
									legs_data8[apt_ok][29] = ""
									legs_data8[apt_ok][30] = ""
									legs_data8[apt_ok][31] = ""
									legs_data8[apt_ok][32] = 0
									legs_data8[apt_ok][33] = ""
									legs_data8[apt_ok][34] = "DEST"
									legs_data8[apt_ok][35] = ""
									legs_data8[apt_ok][36] = 9
									legs_data8[apt_ok][37] = 0
									legs_data8[apt_ok][38] = ""
									legs_data8[apt_ok][39] = ""
									legs_data8[apt_ok][40] = 0
									legs_data8[apt_ok][41] = 0
									legs_data8[apt_ok][42] = 0
									legs_data8[apt_ok][43] = ""
									legs_data8[apt_ok][44] = 0
									legs_data8[apt_ok][45] = 0
									legs_data8[apt_ok][46] = 0
									legs_data8[apt_ok][47] = 0
									legs_data8[apt_ok][48] = ""
									legs_data8[apt_ok][49] = 0
									legs_data8[apt_ok][50] = 0
									legs_data8[apt_ok][51] = 0
									legs_data8[apt_ok][52] = 0
									legs_data8[apt_ok][53] = 0
									legs_data8[apt_ok][54] = 0
									
									if rte12_act == rte12_sel then
										offset_mod2 = offset
									else
										offset_mod2 = 2
									end
									if offset_mod2 > legs_num8 then
										offset_mod2 = legs_num8
									end
									
									if legs_num ~= 0 then
										if rte12_act == rte12_sel then
											legs_delete = 1
										end
										calc_rte_enable8 = 1
									end
									
									if exec_activate_fpln2 ~= 0 then
										rte_exec2 = 0
										exec_activate_fpln2 = 0
										
										copy_to_legs_data2_buf(1)
										if legs_num2_buf > 1 then
											for n = 1, (legs_num2_buf + 1) do
												legs_data2_buf[n][11] = 0
												legs_data2_buf[n][10] = 0
												legs_data2_buf[n][14] = 50000
												legs_data2_buf[n][38] = ""
											end
										end
										copy_from_legs_data2_buf(1)
										
									end
									
									create_fpln(rte12_sel)
									
									arr_data7_x = 0
									find_des_rnw_data(1, 1)
									read_des_list_icao(des_icao7_x, 1)
									create_rnw_list(rte12_sel)
									create_star_list(rte12_sel)
									create_app_list(rte12_sel)
									
									entry = ""
									
								else
									add_fmc_msg(NOT_IN_DATABASE, 1)
								end
							elseif entry == "" and des_icao7_x ~= "****" then
								entry = des_icao7_x
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				end
			else
				-- button 1 RSK
				rte_1_rsk(1, 0, rte12_sel)
			end
		
		elseif page_legs == 1 then
			local item = 0
			local button = 1	-- button 1 RSK
			
			local offset_tmp = 0
			
			if rte12_sel == 0 then
				offset_tmp = offset_mod
			else
				offset_tmp = offset_mod2
			end
			
			item = (act_page - 1) * 5 + offset_tmp - 1 + button
			if B738DR_no_rte2 == 0 and act_page > 1 then
				item = item - 1
			end
			
			if entry ~= "" then
				if set_spd_alt_rest(item, rte12_sel) == true then
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		elseif page_hold == 1 then
			local test_cond = false
			
			if entry ~= "" then
				if rte12_sel == 0 then
					test_cond = set_spd_alt_rest(hold_offset_idx, rte12_sel)
				else
					test_cond = set_spd_alt_rest(hold_offset_idx7, rte12_sel)
				end
				if rte12_sel == 0 then
					if rte12_act == rte12_sel then
						hold_exec = 1
					end
				else
					if rte12_act == rte12_sel then
						hold_exec2 = 1
					end
				end
			end
		elseif page_ref_nav_data == 1 then
			local strlen = string.len(entry)
			if strlen == 0 or strlen > 4 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				nav_data_type = 1 -- navaid
				nav_data_find(entry, 0)
			end
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			-- entry V1
			if entry == "" then
				if v1 ~= "---" and qrh == "OFF" and v1_set == "---" then
					v1_set = v1
					verify_to_spd = 0
				elseif v1_set ~= "---" then
					entry = v1_set
				end
			else
				-- from scratch (test speed correct)
				if entry == ">DELETE" then
					v1_set = "---"
					verify_to_spd = 0
					entry = ""
				else
					local n = tonumber(entry)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n > 99 and n < 170 then
							v1_set = entry
							verify_to_spd = 0
							entry = ""
							local v1_val = tonumber(v1_set)
							local vr_val = tonumber(vr_set)
							local v2_val = tonumber(v2_set)
							if v1_val ~= nil and v2_val ~= nil and vr_val ~= nil then
								if v1_val > vr_val or vr_val >= v2_val then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
									add_fmc_msg(TO_SPEEDS_DELETED, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
			display_update = 1
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			if rw_cond == 2 then
				rw_cond = 0
			else
				rw_cond = rw_cond + 1
			end
			if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				if v1_set == "---" and vr_set == "---" and v2_set == "---" then
					entry = ""
				else
					-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
					-- verify_to_spd = 1
					v1_set = "---"
					vr_set = "---"
					v2_set = "---"
					verify_to_spd = 0
					add_fmc_msg(TO_SPEEDS_DELETED, 1)
					entry = ""
				end
			end
			B738DR_calc_vspd = 1
		elseif page_climb == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if entry == ">DELETE" then
				if set_spd_alt_rest(B738DR_rest_wpt_alt_idx, rte12_sel) == true then
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		elseif page_approach == 1 then
			-- select flaps 15
			if vref_15 == "---" then
				entry = ""
			else
				if flaps_app == "15" then
					app_flap = "15"
					app_spd = vref_15
					flaps_app = "  "
					entry = ""
				else
					flaps_app = "15"
					entry = "15/" .. string.format("%3d", vref_15)
				end
			end
		elseif page_perf == 1 then --and was_on_air == 0 then	-- only on ground
			-- Change cruise alt
			local n = entry_altitude(entry)
			if n == 0 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				if crz_alt == "*****" then
					crz_alt_num = n
					if crz_alt_num >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					B738DR_fmc_cruise_alt = crz_alt_num
					entry = ""
					msg_unavaible_crz_alt = 0
					msg_chk_alt_constr = 0
					vnav_update = 1
					crz_alt_num2 = 0
					crz_alt_old = "     "
					if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
						add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
					end
					if isa_dev_c ~= "---" then
						n = tonumber(isa_dev_c)
						local alt_temp = math.min(crz_alt_num, 37000)
						tc_oat_c = string.format("%3d", (B738_rescale(0, 15, 37000, -56.5, alt_temp) + n))
						n = tonumber(tc_oat_c)
						n = (n * 9 / 5) + 32
						tc_oat_f = string.format("%4d", n)
					end
					B738_calc_vnav_spd()
				else
					crz_alt_num2 = n
					crz_alt_old = crz_alt
					crz_exec = 9
					exec1_light = 1
					if perf_exec == 2 then
						perf_exec = 3
					end
					if crz_alt_num2 >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					entry = ""
				end
			end
		elseif page_init == 1 then
			-- go to REF NAV DATA
			page_init = 0
			page_ref_nav_data = 1
			ref_nav_wpt = "-----"
			ref_nav_navaid = "----"
			ref_nav_apt = "----"
			ref_nav_new = "*****"
		elseif page_pos_init == 1 then
			entry = last_pos
		elseif page_descent_forecast == 1 then
			local strlen = string.len(entry)
			local n = 0
			if strlen > 0 then
				if entry == ">DELETE" then
					tai_on_alt_mod = "-----"
					tai_off_alt_mod = "-----"
					tai_on_alt_num_mod = 0
					tai_off_alt_num_mod = 0
					entry = ""
					forecast_exec = 1
				else
					if string.sub(entry, 1, 1) == "/" then
						if strlen == 6 then
							if string.sub(entry, 2, 3) == "FL" then
								n = tonumber(string.sub(entry, 4, -1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n = n * 100
									if n >= 1000 and n <= 41000 then
										tai_off_alt_num_mod = n
										if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
											tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
										else
											tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
										end
										entry = ""
										forecast_exec = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
							else
								n = tonumber(string.sub(entry, 2, -1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if n >= 1000 and n <= 41000 then
										tai_off_alt_num_mod = n
										if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
											tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
										else
											tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
										end
										entry = ""
										forecast_exec = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
							end
						elseif strlen == 5 then
							n = tonumber(string.sub(entry, 2, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1000 and n <= 41000 then
									tai_off_alt_num_mod = n
									if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
										tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
									else
										tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
									end
									entry = ""
									forecast_exec = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						elseif strlen == 4 then
							n = tonumber(string.sub(entry, 2, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 100
								if n >= 1000 and n <= 41000 then
									tai_off_alt_num_mod = n
									if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
										tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
									else
										tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
									end
									entry = ""
									forecast_exec = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					elseif strlen == 5 and string.sub(entry, 1, 2) == "FL" then
						n = tonumber(string.sub(entry, 3, -1))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							n = n * 100
							if n >= 1000 and n <= 41000 then
								tai_on_alt_num_mod = n
								if tai_on_alt_num_mod >= B738DR_trans_lvl_mod then
									tai_on_alt_mod = "FL" .. string.format("%03d", tai_on_alt_num_mod / 100)
								else
									tai_on_alt_mod = string.format("%5d", tai_on_alt_num_mod)
								end
								entry = ""
								forecast_exec = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					elseif strlen == 4 or strlen == 5 then
						n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n >= 1000 and n <= 41000 then
								tai_on_alt_num_mod = n
								if tai_on_alt_num_mod >= B738DR_trans_lvl_mod then
									tai_on_alt_mod = "FL" .. string.format("%03d", tai_on_alt_num_mod / 100)
								else
									tai_on_alt_mod = string.format("%5d", tai_on_alt_num_mod)
								end
								entry = ""
								forecast_exec = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					elseif strlen == 3 then
						n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							n = n * 100
							if n >= 1000 and n <= 41000 then
								tai_on_alt_num_mod = n
								if tai_on_alt_num_mod >= B738DR_trans_lvl_mod then
									tai_on_alt_mod = "FL" .. string.format("%03d", tai_on_alt_num_mod / 100)
								else
									tai_on_alt_mod = string.format("%5d", tai_on_alt_num_mod)
								end
								entry = ""
								forecast_exec = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_rte_legs > 0 then
			-- entry WIND DATA
			local strlen = string.len(entry)
			local item = 0
			local button = 1	-- button 1 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry == ">DELETE" then
					legs_data2[item][39] = ""
					entry = ""
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry = ""
						if rte12_act == rte12_sel then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				if nearest_apt_list_num >= 1 then
					act_page = 2
				end
			else
				if entry == ">DELETE" then
					nearest_apt_list_alt[act_page - 1] = 0
					entry = ""
				else
					local nnn = entry_altitude(entry)
					if nnn == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if nnn < 1000 or nnn > 41000 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_alt[act_page - 1] = nnn
							entry = ""
						end
					end
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				if altern_apt_list_num >= 1 then
					act_page = 2
				end
			else
				if entry == ">DELETE" then
					altern_apt_list_alt[act_page - 1] = 0
					entry = ""
				else
					local nnn = entry_altitude(entry)
					if nnn == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if nnn < 1000 or nnn > 41000 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_alt[act_page - 1] = nnn
							entry = ""
						end
					end
				end
			end
		elseif page_dl_acars == 1 then
			page_dl_acars = 0
			page_dl_fltlog = 1
			act_page = 1
			display_update = 1
		elseif page_dl_wxr_req == 1 then
			-- input ICAO weather request
			-- entry ICAO ATIS
			if entry == ">DELETE" then
				ac_atis_icao = "----"
				entry = ""
			elseif entry == "" and ac_atis_icao ~= "----" then
				entry = ac_atis_icao
			elseif string.len(entry) == 4 then
				ac_atis_icao = entry
				entry = ""
			end
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 2RSK
function B738_fmc1_2R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep == 1 then
			if rte12_sel == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
				if sel_ref_rwy2 == "-----" then
					if ref_rwy_sel[2] ~= "" then
						if ref_rwy2 == ref_rwy_sel[2] then
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
						else
							ref_rwy2 = ref_rwy_sel[2]
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel)
							ref_rwy3 = ref_rwy2
							ref_rwy_exec = 1
						end
						act_page = 1
					end
				end
				rte_add_dep_arr(rte12_sel)
				-- check SID for selected runway
				if rte12_act == rte12_sel then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			elseif rte12_sel == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
				if sel_ref_rwy8 == "-----" then
					if ref_rwy_sel[2] ~= "" then
						if ref_rwy8 == ref_rwy_sel[2] then
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
						else
							ref_rwy8 = ref_rwy_sel[2]
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel)
							ref_rwy9 = ref_rwy8
							ref_rwy_exec = 1
						end
						act_page = 1
					end
				end
				rte_add_dep_arr(rte12_sel)
				-- check SID for selected runway
				if rte12_act == rte12_sel then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			end
		elseif page_arr == 1 then
			local n = 0
			if rte12_sel == 0 then
				if gs_prompt == 0 then
					if sel_des_app2 == "------" then
						if des_app_sel[2] ~= "" then
							if des_app2 == des_app_sel[2] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app2 = des_app_sel[2]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel[2] ~= "" then
									if des_app_tns2 == des_tns_sel[2] then
										if app_tns_list_num == 1 then
											des_app_tns2 = "------"
											sel_des_app_tns2 = des_app_tns2
											act_page = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns2 = des_app_tns2
										end
									else
										des_app_tns2 = des_tns_sel[2]
										sel_des_app_tns2 = des_app_tns2
										des_app_tns_exec = 1
									end
									act_page = 1
								end
							else
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								act_page = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension distance RX...
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist_x = string.format("%4.1f", entry)
									entry = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app2 == "------" then
						if des_app_sel[2] ~= "" then
							if des_app2 == des_app_sel[2] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app2 = des_app_sel[2]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
							rte_add_dep_arr(rte12_sel)
						else
							if string.sub(des_app2, 1, 1) ~= "R" and string.sub(des_app2, 1, 1) ~= "D" and string.sub(des_app2, 1, 1) ~= "S" 
							and string.sub(des_app2, 1, 1) ~= "N" and string.sub(des_app2, 1, 1) ~= "Q" and string.sub(des_app2, 1, 1) ~= "V" then
								if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
									if B738DR_fms_ils_disable == 0 then
										B738DR_fms_ils_disable = 1
									else
										B738DR_fms_ils_disable = 0
									end
								end
							end
						end
					else
						if string.sub(des_app2, 1, 1) ~= "R" and string.sub(des_app2, 1, 1) ~= "D" and string.sub(des_app2, 1, 1) ~= "S" 
						and string.sub(des_app2, 1, 1) ~= "N" and string.sub(des_app2, 1, 1) ~= "Q" and string.sub(des_app2, 1, 1) ~= "V" then
							if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
								if B738DR_fms_ils_disable == 0 then
									B738DR_fms_ils_disable = 1
								else
									B738DR_fms_ils_disable = 0
								end
							end
						end
					end
				end
			else
				if gs_prompt == 0 then
					if sel_des_app8 == "------" then
						if des_app_sel[2] ~= "" then
							if des_app8 == des_app_sel[2] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app8 = des_app_sel[2]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel[2] ~= "" then
									if des_app_tns8 == des_tns_sel[2] then
										if app_tns_list_num == 1 then
											des_app_tns8 = "------"
											sel_des_app_tns8 = des_app_tns8
											act_page = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns8 = des_app_tns8
										end
									else
										des_app_tns8 = des_tns_sel[2]
										sel_des_app_tns8 = des_app_tns8
										des_app_tns_exec = 1
									end
									act_page = 1
								end
							else
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								act_page = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension distance RX...
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist7_x = string.format("%4.1f", entry)
									entry = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app8 == "------" then
						if des_app_sel[2] ~= "" then
							if des_app8 == des_app_sel[2] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app8 = des_app_sel[2]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
							rte_add_dep_arr(rte12_sel)
						else
							if string.sub(des_app8, 1, 1) ~= "R" and string.sub(des_app8, 1, 1) ~= "D" and string.sub(des_app8, 1, 1) ~= "S" 
							and string.sub(des_app8, 1, 1) ~= "N" and string.sub(des_app8, 1, 1) ~= "Q" and string.sub(des_app8, 1, 1) ~= "V" then
								if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
									if B738DR_fms_ils_disable == 0 then
										B738DR_fms_ils_disable = 1
									else
										B738DR_fms_ils_disable = 0
									end
								end
							end
						end
					else
						if string.sub(des_app8, 1, 1) ~= "R" and string.sub(des_app8, 1, 1) ~= "D" and string.sub(des_app8, 1, 1) ~= "S" 
						and string.sub(des_app8, 1, 1) ~= "N" and string.sub(des_app8, 1, 1) ~= "Q" and string.sub(des_app8, 1, 1) ~= "V" then
							if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
								if B738DR_fms_ils_disable == 0 then
									B738DR_fms_ils_disable = 1
								else
									B738DR_fms_ils_disable = 0
								end
							end
						end
					end
				end
			end
		elseif page_dep_arr == 1 then
			-- Destination ARR
			if ref_icao_x ~= "----" and des_icao_x ~= "****" then
				rte12_sel = 0
				page_arr = 1
				page_dep_arr = 0
				
				if arr_data_x ~= 0 then
					
					local apt_ok = 0
					
					if legs_num2 > 1 then	--and des_icao_x ~= des_icao then
						-- -- delete all STAR/APP
						if first_star_idx_mod > 0 and offset < first_star_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_star_idx_mod
							if first_star_idx_mod ~= 0 and first_star_idx_mod < legs_num2 then
								if legs_data2[first_star_idx_mod][19] == 4 then
									apt_ok = first_star_idx_mod + 1
									legs_data2[first_star_idx_mod][19] = 0
									legs_data2[first_star_idx_mod][4] = 0
									legs_data2[first_star_idx_mod][5] = 0
									legs_data2[first_star_idx_mod][6] = 0
									legs_data2[first_star_idx_mod][9] = ""
									legs_data2[first_star_idx_mod][21] = -1
									legs_data2[first_star_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							-- rte_paste(first_star_idx_mod)
							calc_rte_enable2 = 1
						elseif first_app_idx_mod > 0 and offset < first_app_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_app_idx_mod
							if first_app_idx_mod ~= 0 and first_app_idx_mod < legs_num2 then
								if legs_data2[first_app_idx_mod][19] == 9 then
									apt_ok = first_app_idx_mod + 1
									legs_data2[first_app_idx_mod][19] = 0
									legs_data2[first_app_idx_mod][4] = 0
									legs_data2[first_app_idx_mod][5] = 0
									legs_data2[first_app_idx_mod][6] = 0
									legs_data2[first_app_idx_mod][9] = ""
									legs_data2[first_app_idx_mod][21] = -1
									legs_data2[first_app_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						end
						last_sid_idx_mod = legs_num2
						if rte12_act == rte12_sel then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num2 + 1
						legs_data2[apt_ok] = {}
						legs_data2[apt_ok][1] = des_icao_x	--des_icao_x
						legs_data2[apt_ok][2] = 0		-- brg
						legs_data2[apt_ok][3] = 0		-- distance
						legs_data2[apt_ok][4] = 0		-- speed
						legs_data2[apt_ok][5] = 0		-- altitude
						legs_data2[apt_ok][6] = 0	-- altitude type
						legs_data2[apt_ok][7] = des_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[apt_ok][8] = des_icao_lon_x	--des_icao_lon		-- longitude
						legs_data2[apt_ok][9] = ""			-- via id
						legs_data2[apt_ok][10] = 0		-- calc speed
						legs_data2[apt_ok][11] = 0		-- calc altitude
						legs_data2[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data2[apt_ok][13] = 0
						legs_data2[apt_ok][14] = 0		-- rest alt
						legs_data2[apt_ok][15] = 0		-- last fuel
						legs_data2[apt_ok][16] = ""
						legs_data2[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data2[apt_ok][20] = 0
						legs_data2[apt_ok][21] = -1
						legs_data2[apt_ok][22] = ""
						legs_data2[apt_ok][23] = 0
						legs_data2[apt_ok][24] = 0
						legs_data2[apt_ok][25] = 0
						legs_data2[apt_ok][26] = 0
						legs_data2[apt_ok][27] = ""
						legs_data2[apt_ok][28] = ""
						legs_data2[apt_ok][29] = ""
						legs_data2[apt_ok][30] = ""
						legs_data2[apt_ok][31] = ""
						legs_data2[apt_ok][32] = 0
						legs_data2[apt_ok][33] = ""
						legs_data2[apt_ok][34] = "DEST"
						legs_data2[apt_ok][35] = ""
						legs_data2[apt_ok][36] = 9
						legs_data2[apt_ok][37] = 0
						legs_data2[apt_ok][38] = ""
						legs_data2[apt_ok][39] = ""
						legs_data2[apt_ok][40] = 0
						legs_data2[apt_ok][41] = 0
						legs_data2[apt_ok][42] = 0
						legs_data2[apt_ok][43] = ""
						legs_data2[apt_ok][44] = 0
						legs_data2[apt_ok][45] = 0
						legs_data2[apt_ok][46] = 0
						legs_data2[apt_ok][47] = 0
						legs_data2[apt_ok][48] = ""
						legs_data2[apt_ok][49] = 0
						legs_data2[apt_ok][50] = 0
						legs_data2[apt_ok][51] = 0
						legs_data2[apt_ok][52] = 0
						legs_data2[apt_ok][53] = 0
						legs_data2[apt_ok][54] = 0
						
						if rte12_act == 0 then
							offset_mod = offset
						else
							offset_mod = 2
						end
						if offset_mod > legs_num2 then
							offset_mod = legs_num2
						end
						
					elseif legs_num2 > 0 then
						legs_data2[legs_num2 + 1][1] = des_icao_x	--des_icao_x
						legs_data2[legs_num2 + 1][7] = des_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[legs_num2 + 1][8] = des_icao_lon_x	--des_icao_lon		-- longitude
					end
					
					
					des_app_rw_only_x = 0
					des_star2 = "------"
					des_star_trans2 = "------"
					des_app2 = "------"
					des_app_tns2 = "------"
					if rte12_act == 0 then
						legs_delete = 1
					end
				end
				arr_data_x = 0
				read_des_list_icao(des_icao_x, 0)
				
				sel_des_star2 = "------"
				sel_des_star_trans2 = "------"
				sel_des_app2 = "------"
				sel_des_app_tns2 = "------"
				
				create_star_list(0)
				create_star_tns_list(0)
				create_app_list(0)
				create_app_tns_list(0)
				
			end
		elseif page_rte_init == 1 then
			-- entry FLIGHT NUMBER
			if act_page == 1 then
				if entry == ">DELETE" then
					--entry = INVALID_DELETE
					flt_num_x = "--------"
					entry = ""
					if dl_block_time < 0 then
						dl_flt_num = flt_num_x
						dl_day = simDR_time_day
					end
				else
					if string.len(entry) > 8 then
						add_fmc_msg(INVALID_INPUT, 1)
					elseif string.len(entry) > 0 then
						flt_num_x = entry
						entry = ""
						if dl_block_time < 0 then
							dl_flt_num = flt_num_x
							dl_day = simDR_time_day
						end
					end
				end
			else
				-- button 2 RSK
				rte_x_rsk(2, 0, rte12_sel)
			end
		elseif page_legs == 1 then
			local item = 0
			local button = 2	-- button 2 RSK
			
			local offset_tmp = offset_mod
			if rte12_sel ~= 0 then
				offset_tmp = offset_mod2
			end
			item = (act_page - 1) * 5 + offset_tmp - 1 + button
			if B738DR_no_rte2 == 0 and act_page > 1 then
				item = item - 1
			end
			
			if entry ~= "" then
				if set_spd_alt_rest(item, rte12_sel) == true then
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			-- entry VR
			if entry == "" then
				if vr ~= "---" and qrh == "OFF" and vr_set == "---" then
					vr_set = vr
					verify_to_spd = 0
				elseif vr_set ~= "---" then
					entry = vr_set
				end
			else
				-- from scratch (test speed correct)
				if entry == ">DELETE" then
					vr_set = "---"
					verify_to_spd = 0
					entry = ""
				else
					local n = tonumber(entry)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n > 99 and n < 170 then
							vr_set = entry
							verify_to_spd = 0
							entry = ""
							local v1_val = tonumber(v1_set)
							local vr_val = tonumber(vr_set)
							local v2_val = tonumber(v2_set)
							if v1_val ~= nil and v2_val ~= nil and vr_val ~= nil then
								if v1_val > vr_val or vr_val >= v2_val then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
									add_fmc_msg(TO_SPEEDS_DELETED, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
			display_update = 1
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
				-- entry SEL TEMP and OAT
				local strlen = string.len(entry)
				local n = 0
				if strlen > 0 then
					if entry == ">DELETE" then
						sel_temp = "----"
						oat = "****"
						sel_temp_f = "----"
						oat_f = "****"
						oat_unit = "`C"
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry = ""
							else
								-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								-- verify_to_spd = 1
								add_fmc_msg(TO_SPEEDS_DELETED, 1)
								verify_to_spd = 0
								v1_set = "---"
								vr_set = "---"
								v2_set = "---"
							qrh = "OFF"
								entry = ""
							end
						end
						B738DR_calc_vspd = 1
					else
						if strlen > 0 and strlen < 6 then
							local oat_set = 0
							if string.sub(entry, 1, 1) == "/" and strlen > 1 then
								oat_set = 1		-- entry OAT
								entry = string.sub(entry, 2, strlen)
								strlen = strlen -1
							end
							
							local n_str = string.sub(entry, strlen, strlen)
							if n_str == "C" then 
								n = tonumber(string.sub(entry, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							elseif n_str == "F" then
								n = tonumber(string.sub(entry, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 50 or n > 167) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 140) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`F"
										if oat_set == 0 then
											sel_temp_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
										else
											oat_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							else
								n = tonumber(string.sub(entry, 1, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							end
							
							
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
		elseif page_approach == 1 then
			-- select flaps 30
			if vref_30 == "---" then
				entry = ""
			else
				if flaps_app == "30" then
					app_flap = "30"
					app_spd = vref_30
					flaps_app = "  "
					entry = ""
				else
					flaps_app = "30"
					entry = "30/" .. string.format("%3d", vref_30)
				end
			end
		elseif page_n1_limit == 1 then
			-- select CLB
			clb = "<SEL>"
			clb_1 = "     "
			clb_2 = "     "
			sel_clb_thr = 1
		elseif page_descent_forecast == 1 then
			-- entry ISA DEV / QNH
			local strlen = string.len(entry)
			local n = 0
			local nn = 0
			local nnn = 0
			local n_str = ""
			local forec_isa_dev_tmp = ""
			local data_ok = 0
			local mode_temp_qnh = 0
			if strlen > 0 then
				if entry == ">DELETE" then
					forec_isa_dev_mod = "---"
					entry = ""
					forecast_exec = 1
				else
					if string.sub(entry, -1, -1) == "/" then
						if strlen > 1 then
							entry = string.sub(entry, 1, -2)
							strlen = strlen - 1
						end
						if strlen < 4 then
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < -40 or n > 40 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									forec_isa_dev_mod = string.format("%3d", n)
									entry = ""
									forecast_exec = 1
								end
							end
						else
							nn,n = string.find(entry, "/")
							if nn ~= nil and nn > 1 then
								-- ISA
								n = tonumber(string.sub(entry, 1, nn - 1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if n < -40 or n > 40 then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										forec_isa_dev_tmp = string.format("%3d", n)
										data_ok = 1
									end
								end
								if data_ok == 1 then
									n = strlen - nn
									if n > 0 and n <= 5 then
										if n == 5 then
											-- QNH xx.xx
											if string.sub(entry, nn + 3, nn + 3) == "." then
												n = tonumber(string.sub(entry, nn + 1, strlen))
												if n == nil then
													add_fmc_msg(INVALID_INPUT, 1)
												else
													if n < 0 or n > 40.0 then
														add_fmc_msg(INVALID_INPUT, 1)
													else
														forec_qnh_mod = " " .. string.sub(entry, nn + 1, strlen)
														data_ok = 1
													end
												end
											else
												add_fmc_msg(INVALID_INPUT, 1)
											end
										else
											n = tonumber(string.sub(entry, nn + 1, strlen))
											if n == nil then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												if n < 0 or n > 1355 then
													add_fmc_msg(INVALID_INPUT, 1)
												else
													forec_qnh_mod = "  " .. string.sub(entry, nn + 1, strlen)
													data_ok = 1
												end
											end
										end
										if data_ok == 1 then
											forec_isa_dev_mod = forec_isa_dev_tmp
											entry = ""
											forecast_exec = 1
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
								
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					else
						if string.sub(entry, 1, 1) == "/" and (strlen == 5 or strlen == 6) then
							strlen = strlen - 1
							entry = string.sub(entry, 2, -1)
						else
							-- find "/"
							nn,n = string.find(entry, "/")
							if nn ~= nil then
								if nn == 1 or strlen < 3 then
									mode_temp_qnh = 2
								else
									mode_temp_qnh = 1
								end
							end
						end
						if mode_temp_qnh == 0 then
							if strlen <= 4 then		-- QNH /XXXX
								n = tonumber(string.sub(entry, 1, 4))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if n < 0 or n > 1355 then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										forec_qnh_mod = "  " .. string.sub(entry, 1, 4)
										entry = ""
										forecast_exec = 1
									end
								end
							elseif strlen == 5 then		-- QNH /XX.XX in hg
								if string.sub(entry, 3, 3) == "." then
									n = tonumber(string.sub(entry, 1, 5))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if n < 0 or n > 40.0 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											forec_qnh_mod = " " .. string.sub(entry, 1, 5)
											entry = ""
											forecast_exec = 1
										end
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						elseif mode_temp_qnh == 1 then
							-- ISA DEV / QNH
							n_str = string.sub(entry, 1, nn-1)
							n = tonumber(n_str)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < -40 or n > 40 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nnn = n
									
									n_str = string.sub(entry, nn+1, -1)
									strlen = string.len(n_str)
									if strlen <= 4 then		-- QNH /XXXX
										n = tonumber(string.sub(n_str, 1, 4))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											if n < 0 or n > 1355 then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												forec_isa_dev_mod = string.format("%3d", nnn)
												forec_qnh_mod = "  " .. string.sub(n_str, 1, 4)
												entry = ""
												forecast_exec = 1
											end
										end
									elseif strlen == 5 then		-- QNH /XX.XX in hg
										if string.sub(n_str, 3, 3) == "." then
											n = tonumber(string.sub(n_str, 1, 5))
											if n == nil then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												if n < 0 or n > 40.0 then
													add_fmc_msg(INVALID_INPUT, 1)
												else
													forec_isa_dev_mod = string.format("%3d", nnn)
													forec_qnh_mod = " " .. string.sub(n_str, 1, 5)
													entry = ""
													forecast_exec = 1
												end
											end
										else
											add_fmc_msg(INVALID_INPUT, 1)
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
									
									
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
		elseif page_perf == 1 then
			-- entry CRZ WIND
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					crz_wind_dir_mod = "---"
					crz_wind_spd_mod = "---"
					entry = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						crz_wind_dir = crz_wind_dir_mod
						crz_wind_spd = crz_wind_spd_mod
					end
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						crz_wind_dir_mod = string.format("%03d", q)
						crz_wind_spd_mod = string.format("%3d", qq)
						entry = ""
						if perf_exec == 2 then
							perf_exec = 3
						elseif perf_exec < 2 then
							crz_wind_dir = crz_wind_dir_mod
							crz_wind_spd = crz_wind_spd_mod
						end
						B738_calc_vnav_spd()
						vnav_update = 1
					end
				end
			end
		elseif page_perf == 2 then
			-- CLB max speed kts/mach
			local strlen = string.len(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					clb_max_kts = "   "
					clb_max_mach = "   "
					entry = ""
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_min_mach)
							if nn == nil then
								nn = 0.4
							else
								nn = nn / 1000
							end
							if n > 0.82 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								clb_max_mach = string.format("%03d", n)
								entry = ""
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340  or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_max_kts = string.format("%03d", n)
								entry = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(clb_min_mach)
										if nn == nil then
											nn = 0.4
										else
											nn = nn / 1000
										end
										if n > 0.82 or n < nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											clb_max_kts = kts
											clb_max_mach = string.format("%03d", n)
											entry = ""
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		----
		elseif page_pos_init == 1 then
			if rte12_sel == 0 then
				if ref_icao_pos ~= "               " then
					entry = ref_icao_pos
				end
			else
				if ref_icao_pos2 ~= "               " then
					entry = ref_icao_pos2
				end
			end
		elseif page_rte_legs > 0 then
			-- entry WIND DATA
			local strlen = string.len(entry)
			local item = 0
			local button = 2	-- button 2 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry == ">DELETE" then
					legs_data2[item][39] = ""
					entry = ""
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry = ""
						if rte12_act == rte12_sel then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_progress == 4 and legs_num > 0 then
			local strlen = string.len(entry)
			local entry_tmp = entry
			local n = 0
			if entry == ">DELETE" then
				vrnp = ""
				vrnp_num = 400
				entry = ""
			else
				if strlen > 1 then
					if string.sub(entry_tmp, -1, -1) == "/" then
						entry_tmp = string.sub(entry_tmp, 1, -2)
					end
					strlen = string.len(entry_tmp)
					if strlen > 1 and strlen < 4 then
						n = tonumber(entry_tmp)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						elseif n < 20 or n > 400 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							vrnp = string.format("%3d", n)
							vrnp_num = n
							entry = ""
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen > 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				if nearest_apt_list_num >= 2 then
					act_page = 3
				end
			else
				-- entry WIND
				local strlen = string.len(entry)
				if strlen > 0 then
					if entry == ">DELETE" then
						nearest_apt_list_wind[act_page - 1] = ""
						entry = ""
					else
						local q = 0
						local qq = 0
						q, qq = entry_wind(entry)
						if qq == 0 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_wind[act_page - 1] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
							entry = ""
						end
						-- if strlen > 4 and strlen < 8 and string.sub(entry, 4, 4) == "/" then
							-- local n = tonumber(string.sub(entry, 1, 3))
							-- if n == nil then
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- if n < 0 or n > 360 then		-- wind heading 0 - 359
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- n = n % 360
									-- local wind_dir = string.format("%03d", n)
									-- n = tonumber(string.sub(entry, 5, strlen))
									-- if n == nil then
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- if n < 1 or n > 199 then	-- wind speed 1 - 199
											-- add_fmc_msg(INVALID_INPUT, 1)
										-- else
											-- nearest_apt_list_wind[act_page - 1] = wind_dir .. "`/" .. string.format("%3d", n)
											-- entry = ""
										-- end
									-- end
								-- end
							-- end
						-- else
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- end
					end
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				if altern_apt_list_num >= 2 then
					act_page = 3
				end
			else
				-- entry WIND
				local strlen = string.len(entry)
				if strlen > 0 then
					if entry == ">DELETE" then
						altern_apt_list_wind[act_page - 1] = ""
						entry = ""
					else
						local q = 0
						local qq = 0
						q, qq = entry_wind(entry)
						if qq == 0 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_wind[act_page - 1] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
							entry = ""
						end
						-- if strlen > 4 and strlen < 8 and string.sub(entry, 4, 4) == "/" then
							-- local n = tonumber(string.sub(entry, 1, 3))
							-- if n == nil then
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- if n < 0 or n > 360 then		-- wind heading 0 - 359
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- n = n % 360
									-- local wind_dir = string.format("%03d", n)
									-- n = tonumber(string.sub(entry, 5, strlen))
									-- if n == nil then
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- if n < 1 or n > 199 then	-- wind speed 1 - 199
											-- add_fmc_msg(INVALID_INPUT, 1)
										-- else
											-- altern_apt_list_wind[act_page - 1] = wind_dir .. "`/" .. string.format("%3d", n)
											-- entry = ""
										-- end
									-- end
								-- end
							-- end
						-- else
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- end
					end
				end
			end
		elseif page_dl_menu == 1 then
			page_dl_menu = 0
			page_dl_acars = 1
			act_page = 1
			display_update = 1
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 3RSK
function B738_fmc1_3R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep == 1 then
				if rte12_sel == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
					if sel_ref_rwy2 == "-----" then
						if ref_rwy_sel[3] ~= "" then
							if ref_rwy2 == ref_rwy_sel[3] then
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel)
							else
								ref_rwy2 = ref_rwy_sel[3]
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel)
								ref_rwy3 = ref_rwy2
								ref_rwy_exec = 1
							end
							act_page = 1
						end
					end
					rte_add_dep_arr(rte12_sel)
					-- check SID for selected runway
					if rte12_act == rte12_sel then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				elseif rte12_sel == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
					if sel_ref_rwy8 == "-----" then
						if ref_rwy_sel[3] ~= "" then
							if ref_rwy8 == ref_rwy_sel[3] then
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel)
							else
								ref_rwy8 = ref_rwy_sel[3]
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel)
								ref_rwy9 = ref_rwy8
								ref_rwy_exec = 1
							end
							act_page = 1
						end
					end
					rte_add_dep_arr(rte12_sel)
					-- check SID for selected runway
					if rte12_act == rte12_sel then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				end
		elseif page_arr == 1 then
			local n = 0
			if rte12_sel == 0 then
				if gs_prompt == 0 then
					if sel_des_app2 == "------" then
						if des_app_sel[3] ~= "" then
							if des_app2 == des_app_sel[3] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app2 = des_app_sel[3]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel[3] ~= "" then
									if des_app_tns2 == des_tns_sel[3] then
										sel_des_app_tns2 = des_app_tns2
									else
										des_app_tns2 = des_tns_sel[3]
										sel_des_app_tns2 = des_app_tns2
										des_app_tns_exec = 1
									end
									act_page = 1
								end
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry)
							if entry == ">DELETE" then
								rw_ext_fpa_x = "-.--"
								entry = ""
								des_app2_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa_x = string.format("%4.2f", entry)
									entry = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app2 == "------" then
						if des_app_sel[3] ~= "" then
							if des_app2 == des_app_sel[3] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app2 = des_app_sel[3]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel[3] ~= "" then
									if des_app_tns2 == des_tns_sel[3] then
										if app_tns_list_num == 1 then
											des_app_tns2 = "------"
											sel_des_app_tns2 = des_app_tns2
											act_page = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns2 = des_app_tns2
										end
									else
										des_app_tns2 = des_tns_sel[3]
										sel_des_app_tns2 = des_app_tns2
										des_app_tns_exec = 1
									end
									act_page = 1
								end
							else
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								act_page = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension distance RX...
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist_x = string.format("%4.1f", entry)
									entry = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			else
				if gs_prompt == 0 then
					if sel_des_app8 == "------" then
						if des_app_sel[3] ~= "" then
							if des_app8 == des_app_sel[3] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app8 = des_app_sel[3]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel[3] ~= "" then
									if des_app_tns8 == des_tns_sel[3] then
										sel_des_app_tns8 = des_app_tns8
									else
										des_app_tns8 = des_tns_sel[3]
										sel_des_app_tns8 = des_app_tns8
										des_app_tns_exec = 1
									end
									act_page = 1
								end
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry)
							if entry == ">DELETE" then
								rw_ext_fpa7_x = "-.--"
								entry = ""
								des_app8_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa7_x = string.format("%4.2f", entry)
									entry = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app8 == "------" then
						if des_app_sel[3] ~= "" then
							if des_app8 == des_app_sel[3] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app8 = des_app_sel[3]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel[3] ~= "" then
									if des_app_tns8 == des_tns_sel[3] then
										if app_tns_list_num == 1 then
											des_app_tns8 = "------"
											sel_des_app_tns8 = des_app_tns8
											act_page = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns8 = des_app_tns8
										end
									else
										des_app_tns8 = des_tns_sel[3]
										sel_des_app_tns8 = des_app_tns8
										des_app_tns_exec = 1
									end
									act_page = 1
								end
							else
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								act_page = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension distance RX...
							n = tonumber(entry)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist7_x = string.format("%4.1f", entry)
									entry = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			end
		elseif page_dep_arr == 1 then
			-- Reference ARR
			if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and ref_icao7_x ~= "ORGN" then
				
				local apt_ok = 0
				local xy = 0
				
				rte12_sel = 1
				
				if arr_data7_x == 0 then
					-- DES ICAO
					if legs_num8 > 1 then
						-- -- delete all STAR/APP
						if first_star_idx_mod2 > 0 and offset < first_star_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_star_idx_mod2
							if first_star_idx_mod2 ~= 0 and first_star_idx_mod2 < legs_num8 then
								if legs_data8[first_star_idx_mod2][19] == 4 then
									apt_ok = first_star_idx_mod2 + 1
									legs_data8[first_star_idx_mod2][19] = 0
									legs_data8[first_star_idx_mod2][4] = 0
									legs_data8[first_star_idx_mod2][5] = 0
									legs_data8[first_star_idx_mod2][6] = 0
									legs_data8[first_star_idx_mod2][9] = ""
									legs_data8[first_star_idx_mod2][21] = -1
									legs_data8[first_star_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						elseif first_app_idx_mod2 > 0 and offset < first_app_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_app_idx_mod2
							if first_app_idx_mod2 ~= 0 and first_app_idx_mod2 < legs_num8 then
								if legs_data8[first_app_idx_mod2][19] == 9 then
									apt_ok = first_app_idx_mod2 + 1
									legs_data8[first_app_idx_mod2][19] = 0
									legs_data8[first_app_idx_mod2][4] = 0
									legs_data8[first_app_idx_mod2][5] = 0
									legs_data8[first_app_idx_mod2][6] = 0
									legs_data8[first_app_idx_mod2][9] = ""
									legs_data8[first_app_idx_mod2][21] = -1
									legs_data8[first_app_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						elseif last_sid_idx_mod2 > 0 and offset < last_sid_idx_mod2 and last_sid_idx_mod2 <= legs_num8 then
							clr_star_app(1)
							rte_copy(legs_num8 + 1, 1)
							rte_paste(last_sid_idx_mod2 + 1, 1)
							calc_rte_enable8 = 1
						elseif offset <= legs_num8 then
							clr_star_app(1)
							rte_copy(legs_num8 + 1, 1)
							rte_paste(offset, 1)
							calc_rte_enable8 = 1
						end
						last_sid_idx_mod2 = legs_num8
						if rte12_act == rte12_sel then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num8 + 1
						legs_data8[apt_ok] = {}
						legs_data8[apt_ok][1] = ref_icao7_x
						legs_data8[apt_ok][2] = 0		-- brg
						legs_data8[apt_ok][3] = 0		-- distance
						legs_data8[apt_ok][4] = 0		-- speed
						legs_data8[apt_ok][5] = 0		-- altitude
						legs_data8[apt_ok][6] = 0	-- altitude type
						legs_data8[apt_ok][7] = ref_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[apt_ok][8] = ref_icao_lon7_x	--des_icao_lon		-- longitude
						legs_data8[apt_ok][9] = ""			-- via id
						legs_data8[apt_ok][10] = 0		-- calc speed
						legs_data8[apt_ok][11] = 0		-- calc altitude
						legs_data8[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data8[apt_ok][13] = 0
						legs_data8[apt_ok][14] = 0		-- rest alt
						legs_data8[apt_ok][15] = 0		-- last fuel
						legs_data8[apt_ok][16] = ""
						legs_data8[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data8[apt_ok][20] = 0
						legs_data8[apt_ok][21] = -1
						legs_data8[apt_ok][22] = ""
						legs_data8[apt_ok][23] = 0
						legs_data8[apt_ok][24] = 0
						legs_data8[apt_ok][25] = 0
						legs_data8[apt_ok][26] = 0
						legs_data8[apt_ok][27] = ""
						legs_data8[apt_ok][28] = ""
						legs_data8[apt_ok][29] = ""
						legs_data8[apt_ok][30] = ""
						legs_data8[apt_ok][31] = ""
						legs_data8[apt_ok][32] = 0
						legs_data8[apt_ok][33] = ""
						legs_data8[apt_ok][34] = "DEST"
						legs_data8[apt_ok][35] = ""
						legs_data8[apt_ok][36] = 9
						legs_data8[apt_ok][37] = 0
						legs_data8[apt_ok][38] = ""
						legs_data8[apt_ok][39] = ""
						legs_data8[apt_ok][40] = 0
						legs_data8[apt_ok][41] = 0
						legs_data8[apt_ok][42] = 0
						legs_data8[apt_ok][43] = ""
						legs_data8[apt_ok][44] = 0
						legs_data8[apt_ok][45] = 0
						legs_data8[apt_ok][46] = 0
						legs_data8[apt_ok][47] = 0
						legs_data8[apt_ok][48] = ""
						legs_data8[apt_ok][49] = 0
						legs_data8[apt_ok][50] = 0
						legs_data8[apt_ok][51] = 0
						legs_data8[apt_ok][52] = 0
						legs_data8[apt_ok][53] = 0
						legs_data8[apt_ok][54] = 0
						
						if rte12_act == 1 then
							offset_mod2 = offset
						else
							offset_mod2 = offset
						end
						if offset_mod2 > legs_num8 then
							offset_mod2 = legs_num8
						end
						
					elseif legs_num8 > 0 then
						legs_data8[legs_num8 + 1][1] = ref_icao7_x	--des_icao_x
						legs_data8[legs_num8 + 1][7] = ref_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[legs_num8 + 1][8] = ref_icao_lon7_x	--des_icao_lon		-- longitude
					end
					
					if rte12_act == 1 then
						legs_delete = 1
					end
					des_app_rw_only7_x = 0
					des_star8 = "------"
					des_star_trans8 = "------"
					des_app8 = "------"
					des_app_tns8 = "------"
				end
				arr_data7_x = 1
				page_arr = 1
				find_ref_rnw_data(1, 1)
				read_ref_list_icao(ref_icao7_x, 1)
				read_des_list_icao(ref_icao7_x, 1)
				page_dep_arr = 0
				
				sel_des_star8 = "------"
				sel_des_star_trans8 = "------"
				sel_des_app8 = "------"
				sel_des_app_tns8 = "------"
				
				create_star_list(1)
				create_star_tns_list(1)
				create_app_list(1)
				create_app_tns_list(1)
				
			end
		elseif page_descent == 1 then
			-- V/B WPT entry
			local n = 0
			local nn = 0
			local nnn = string.len(entry)
			
			nn,n = string.find(entry, "/")
			if nn == nil then
				item_sel = 0
				item_sel_act = 0
			elseif item_sel == 0 then
				if nn > 1 and legs_num > 1 and offset > 0 then
					if offset <= legs_num then
						for zzz = offset, legs_num do
							if string.sub(entry, 1, nn - 1) == legs_data[zzz][1] then
								item_sel = zzz
								break
							end
						end
					end
				end
			elseif item_sel > 0 and item_sel <= legs_num then
				if string.sub(entry, 1, nn - 1) ~= legs_data[item_sel][1] then
					item_sel = 0
					item_sel_act = 0
				end
			else
				item_sel = 0
				item_sel_act = 0
			end
			if entry == ">DELETE" and vb_calc_id ~= "" then
				vb_calc_id = ""
				vb_calc_alt = 0
				vb_calc_lat = 0
				vb_calc_lon = 0
				item_sel = 0
				item_sel_act = 0
			elseif item_sel > 0 and item_sel <= legs_num and nnn > 2 then
				if string.sub(entry, -1, -1) == "/" then
					if string.sub(entry, 1, 2) == "RW" then
						vb_calc_id = legs_data[item_sel][1]
						vb_calc_alt = legs_data[item_sel][5]
						vb_calc_lat = legs_data[item_sel][7]
						vb_calc_lon = legs_data[item_sel][8]
						item_sel = 0
						item_sel_act = 0
						entry = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					n = tonumber(string.sub(entry, nn + 1, -1))
					nn = nnn - nn
					if nn == 3 then
						--FLxxx
						if n >= 100 and n <= 410 then
							vb_calc_id = legs_data[item_sel][1]
							vb_calc_alt = n * 100
							vb_calc_lat = legs_data[item_sel][7]
							vb_calc_lon = legs_data[item_sel][8]
							item_sel = 0
							item_sel_act = 0
							entry = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					elseif nn == 4 or nn == 5 then
						--xxxxx
						if n >= 0 and n <= 41000 then
							vb_calc_id = legs_data[item_sel][1]
							vb_calc_alt = n
							vb_calc_lat = legs_data[item_sel][7]
							vb_calc_lon = legs_data[item_sel][8]
							item_sel = 0
							item_sel_act = 0
							entry = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_rte_init == 1 then
			if act_page > 1 then
				-- button 3 RSK
				rte_x_rsk(3, 0, rte12_sel)
			end
		elseif page_legs == 1 then
			local item = 0
			local button = 3	-- button 3 RSK
			
			local offset_tmp = offset_mod
			if rte12_sel ~= 0 then
				offset_tmp = offset_mod2
			end
			
			item = (act_page - 1) * 5 + offset_tmp - 1 + button
			if B738DR_no_rte2 == 0 and act_page > 1 then
				item = item - 1
			end
			
			if entry ~= "" then
				if set_spd_alt_rest(item, rte12_sel) == true then
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
			
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			-- entry V2
			if entry == "" then
				if v2 ~= "---" and qrh == "OFF" and v2_set == "---" then
					v2_set = v2
					verify_to_spd = 0
				elseif v2_set ~= "---" then
					entry = v2_set
				end
			else
				-- from scratch (test speed correct)
				if entry == ">DELETE" then
					v2_set = "---"
					verify_to_spd = 0
					entry = ""
				else
					local n = tonumber(entry)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n > 99 and n < 170 then
							v2_set = entry
							verify_to_spd = 0
							entry = ""
							local v1_val = tonumber(v1_set)
							local vr_val = tonumber(vr_set)
							local v2_val = tonumber(v2_set)
							if v1_val ~= nil and v2_val ~= nil and vr_val ~= nil then
								if v1_val > vr_val or vr_val >= v2_val then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
									add_fmc_msg(TO_SPEEDS_DELETED, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
			display_update = 1
		elseif page_approach == 1 then
			-- select flaps 40
			if vref_40 == "---" then
				entry = ""
			else
				if flaps_app == "40" then
					app_flap = "40"
					app_spd = vref_40
					flaps_app = "  "
					entry = ""
				else
					flaps_app = "40"
					entry = "40/" .. string.format("%3d", vref_40)
				end
			end
		elseif page_n1_limit == 1 then
			-- select CLB-1
			clb_1 = "<SEL>"
			clb = "     "
			clb_2 = "     "
			sel_clb_thr = 1
		elseif page_ref_nav_data_wpt == 1 then
			if string.len(entry) > 0 then
				local err_pos = 0
				local n = 0
				if string.len(entry) ~= 8 or string.sub(entry, 7, 7) ~= "." then
					err_pos = 1
				end
				n = tonumber(string.sub(entry, 2, 4))
				if (n == nil) or (n < 0) or (n > 180) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry, 5, 6))
				if (n == nil) or (n < 0) or (n > 59) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry, 8, 8))
				if n == nil then
					err_pos = 1
				end
				if string.sub(entry, 1, 1) ~= "E" 
				and string.sub(entry, 1, 1) ~= "W" then
					err_pos = 1
				end
				if err_pos == 0 then
					ref_nav_lon = tonumber(string.sub(entry, 2, 4)) + (tonumber(string.sub(entry, 5, 8)) / 60)
					if string.sub(entry, 1, 1) == "W" then
						ref_nav_lon = -ref_nav_lon
					end
					entry = ""
					ref_nav_id = "-----"
					ref_nav_rc = ""
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					if ref_nav_lat ~= 0 then
						ref_nav_exec = 1
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_descent_forecast == 1 then
			-- entry WIND LAYER 1
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					forec_dir_1_mod = "---"
					forec_spd_1_mod = "---"
					entry = ""
					forecast_exec = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						forec_dir_1_mod = string.format("%03d", q)
						forec_spd_1_mod = string.format("%3d", qq)
						entry = ""
						forecast_exec = 1
					end
				end
			end
		elseif page_perf == 1 and disable_PERF_3R == 0 then
			-- entry ISA DEV
			local strlen = string.len(entry)
			local alt_temp = crz_alt_num
			local n = 0
			local n_str = ""
			if strlen > 0 then
				if entry == ">DELETE" then
					isa_dev_f_mod = "----"
					isa_dev_c_mod = "---"
					tc_oat_c_mod = "---"
					tc_oat_f_mod = "----"
					entry = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						isa_dev_f = isa_dev_f_mod
						isa_dev_c = isa_dev_c_mod
						tc_oat_c = tc_oat_c_mod
						tc_oat_f = tc_oat_f_mod
					end
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen < 6 then
						n = tonumber(entry)
						if n == nil and strlen > 1 then
							n = tonumber(string.sub(entry, 1, strlen-1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n_str = string.sub(entry, strlen, strlen)
								if n_str == "C" then 
									if n < -40 or n > 70 then	-- ISA DEV Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_c_mod = string.format("%3d", n)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								elseif n_str == "F" then
									if n < -40 or n > 158 then	-- ISA DEV Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_f_mod = string.format("%4d", n)
										n = n * 5 / 9
										isa_dev_c_mod = string.format("%3d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						else
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if B738DR_fmc_units == 0 then
									if n < -40 or n > 158 then	-- ISA DEV Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_f_mod = string.format("%4d", n)
										n = n * 5 / 9
										isa_dev_c_mod = string.format("%3d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								else
									if n < -40 or n > 70 then	-- ISA DEV Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_c_mod = string.format("%3d", n)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_perf == 2 then
			-- CRZ max speed kts/mach
			local strlen = string.len(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					crz_max_kts = "   "
					crz_max_mach = "   "
					entry = ""
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_min_mach)
							if nn == nil then
								nn = 0.4
							else
								nn = nn / 1000
							end
							if n > 0.82 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								crz_max_mach = string.format("%03d", n)
								entry = ""
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340  or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								crz_max_kts = string.format("%03d", n)
								entry = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(crz_min_mach)
										if nn == nil then
											nn = 0.4
										else
											nn = nn / 1000
										end
										if n > 0.82 or n < nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											crz_max_kts = kts
											entry = ""
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		----------
		elseif page_rte_legs > 0 then
			-- entry WIND DATA
			local strlen = string.len(entry)
			local item = 0
			local button = 3	-- button 3 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry == ">DELETE" then
					legs_data2[item][39] = ""
					entry = ""
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry = ""
						if rte12_act == rte12_sel then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_init == 1 then
			page_altern_apt = 1
			page_init = 0
			act_page = 1
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				if nearest_apt_list_num >= 3 then
					act_page = 4
				end
			else
				if nearest_apt_list_via[act_page - 1] ~= 0 then
					if entry == ">DELETE" then
						nearest_apt_list_dist2[act_page - 1] = 0
						entry = ""
					else
						local nnn = tonumber(entry)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_dist2[act_page - 1] = nnn
							entry = ""
						end
					end
				end
			end
		elseif page_altern_apt == 1 then
			if act_page == 1 then
				if altern_apt_list_num >= 2 then
					act_page = 4
				end
			else
				if altern_apt_list_via[act_page - 1] ~= 0 then
					if entry == ">DELETE" then
						altern_apt_list_dist2[act_page - 1] = 0
						entry = ""
					else
						local nnn = tonumber(entry)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_dist2[act_page - 1] = nnn
							entry = ""
						end
					end
				end
			end
		elseif page_ref_nav_data == 1 then
			page_flt_plns = 1
			page_ref_nav_data = 0
			create_flt_plns()
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 4RSK
function B738_fmc1_4R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if page_dep == 1 then
			-- if in_flight_mode == 0 then
				if rte12_sel == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
					if sel_ref_rwy2 == "-----" then
						if ref_rwy_sel[4] ~= "" then
							if ref_rwy2 == ref_rwy_sel[4] then
								sel_ref_rwy2 = ref_rwy2
									create_sid_list(rte12_sel)
							else
								ref_rwy2 = ref_rwy_sel[4]
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel)
								ref_rwy3 = ref_rwy2
								ref_rwy_exec = 1
							end
							act_page = 1
						end
					end
					rte_add_dep_arr(rte12_sel)
					-- check SID for selected runway
					if rte12_act == rte12_sel then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				elseif rte12_sel == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
					if sel_ref_rwy8 == "-----" then
						if ref_rwy_sel[4] ~= "" then
							if ref_rwy8 == ref_rwy_sel[4] then
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel)
							else
								ref_rwy8 = ref_rwy_sel[4]
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel)
								ref_rwy9 = ref_rwy8
								ref_rwy_exec = 1
							end
							act_page = 1
						end
					end
					rte_add_dep_arr(rte12_sel)
					-- check SID for selected runway
					if rte12_act == rte12_sel then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				end
			-- end
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if gs_prompt == 0 then
					if sel_des_app2 == "------" then
						if des_app_sel[4] ~= "" then
							if des_app2 == des_app_sel[4] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app2 = des_app_sel[4]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
					else
						if sel_des_app_tns2 == "------" then
							if des_tns_sel[4] ~= "" then
								if des_app_tns2 == des_tns_sel[4] then
									sel_des_app_tns2 = des_app_tns2
								else
									des_app_tns2 = des_tns_sel[4]
									sel_des_app_tns2 = des_app_tns2
									act_page = 1
									des_app_tns_exec = 1
								end
							end
						end
					end
					rte_add_dep_arr(rte12_sel)
				else
					if sel_des_app2 == "------" then
						if des_app_sel[4] ~= "" then
							if des_app2 == des_app_sel[4] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app2 = des_app_sel[4]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel[4] ~= "" then
									if des_app_tns2 == des_tns_sel[4] then
										sel_des_app_tns2 = des_app_tns2
									else
										des_app_tns2 = des_tns_sel[4]
										sel_des_app_tns2 = des_app_tns2
										act_page = 1
										des_app_tns_exec = 1
									end
								end
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry)
							if entry == ">DELETE" then
								rw_ext_fpa_x = "-.--"
								entry = ""
								des_app2_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa_x = string.format("%4.2f", entry)
									entry = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			else
				if gs_prompt == 0 then
					if sel_des_app8 == "------" then
						if des_app_sel[4] ~= "" then
							if des_app8 == des_app_sel[4] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app8 = des_app_sel[4]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
					else
						if sel_des_app_tns8 == "------" then
							if des_tns_sel[4] ~= "" then
								if des_app_tns8 == des_tns_sel[4] then
									sel_des_app_tns8 = des_app_tns8
								else
									des_app_tns8 = des_tns_sel[4]
									sel_des_app_tns8 = des_app_tns8
									act_page = 1
									des_app_tns_exec = 1
								end
							end
						end
					end
					rte_add_dep_arr(rte12_sel)
				else
					if sel_des_app8 == "------" then
						if des_app_sel[4] ~= "" then
							if des_app8 == des_app_sel[4] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								create_app_tns_list(rte12_sel)
							else
								des_app8 = des_app_sel[4]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel)
							end
							act_page = 1
						end
						rte_add_dep_arr(rte12_sel)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel[4] ~= "" then
									if des_app_tns8 == des_tns_sel[4] then
										sel_des_app_tns8 = des_app_tns8
									else
										des_app_tns8 = des_tns_sel[4]
										sel_des_app_tns8 = des_app_tns8
										act_page = 1
										des_app_tns_exec = 1
									end
								end
							end
							rte_add_dep_arr(rte12_sel)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry)
							if entry == ">DELETE" then
								rw_ext_fpa7_x = "-.--"
								entry = ""
								des_app8_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa7_x = string.format("%4.2f", entry)
									entry = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			end
		elseif page_dep_arr == 1 then
			-- Destination ARR
			if ref_icao7_x ~= "----" and des_icao7_x ~= "****" then
				page_arr = 1
				page_dep_arr = 0
				rte12_sel = 1
				
				if arr_data7_x ~= 0 then
					
					local apt_ok = 0
					
					if legs_num8 > 1 then	--and des_icao_x ~= des_icao then
						-- -- delete all STAR/APP
						if first_star_idx_mod2 > 0 and offset < first_star_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_star_idx_mod2
							if first_star_idx_mod2 ~= 0 and first_star_idx_mod2 < legs_num8 then
								if legs_data8[first_star_idx_mod2][19] == 4 then
									apt_ok = first_star_idx_mod2 + 1
									legs_data8[first_star_idx_mod2][19] = 0
									legs_data8[first_star_idx_mod2][4] = 0
									legs_data8[first_star_idx_mod2][5] = 0
									legs_data8[first_star_idx_mod2][6] = 0
									legs_data8[first_star_idx_mod2][9] = ""
									legs_data8[first_star_idx_mod2][21] = -1
									legs_data8[first_star_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						elseif first_app_idx_mod2 > 0 and offset < first_app_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_app_idx_mod2
							if first_app_idx_mod2 ~= 0 and first_app_idx_mod2 < legs_num8 then
								if legs_data8[first_app_idx_mod2][19] == 9 then
									apt_ok = first_app_idx_mod2 + 1
									legs_data8[first_app_idx_mod2][19] = 0
									legs_data8[first_app_idx_mod2][4] = 0
									legs_data8[first_app_idx_mod2][5] = 0
									legs_data8[first_app_idx_mod2][6] = 0
									legs_data8[first_app_idx_mod2][9] = ""
									legs_data8[first_app_idx_mod2][21] = -1
									legs_data8[first_app_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						end
						last_sid_idx_mod2 = legs_num8
						if rte12_act == rte12_sel then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num8 + 1
						legs_data8[apt_ok] = {}
						legs_data8[apt_ok][1] = des_icao7_x	--des_icao_x
						legs_data8[apt_ok][2] = 0		-- brg
						legs_data8[apt_ok][3] = 0		-- distance
						legs_data8[apt_ok][4] = 0		-- speed
						legs_data8[apt_ok][5] = 0		-- altitude
						legs_data8[apt_ok][6] = 0	-- altitude type
						legs_data8[apt_ok][7] = des_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[apt_ok][8] = des_icao_lon7_x	--des_icao_lon		-- longitude
						legs_data8[apt_ok][9] = ""			-- via id
						legs_data8[apt_ok][10] = 0		-- calc speed
						legs_data8[apt_ok][11] = 0		-- calc altitude
						legs_data8[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data8[apt_ok][13] = 0
						legs_data8[apt_ok][14] = 0		-- rest alt
						legs_data8[apt_ok][15] = 0		-- last fuel
						legs_data8[apt_ok][16] = ""
						legs_data8[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data8[apt_ok][20] = 0
						legs_data8[apt_ok][21] = -1
						legs_data8[apt_ok][22] = ""
						legs_data8[apt_ok][23] = 0
						legs_data8[apt_ok][24] = 0
						legs_data8[apt_ok][25] = 0
						legs_data8[apt_ok][26] = 0
						legs_data8[apt_ok][27] = ""
						legs_data8[apt_ok][28] = ""
						legs_data8[apt_ok][29] = ""
						legs_data8[apt_ok][30] = ""
						legs_data8[apt_ok][31] = ""
						legs_data8[apt_ok][32] = 0
						legs_data8[apt_ok][33] = ""
						legs_data8[apt_ok][34] = "DEST"
						legs_data8[apt_ok][35] = ""
						legs_data8[apt_ok][36] = 9
						legs_data8[apt_ok][37] = 0
						legs_data8[apt_ok][38] = ""
						legs_data8[apt_ok][39] = ""
						legs_data8[apt_ok][40] = 0
						legs_data8[apt_ok][41] = 0
						legs_data8[apt_ok][42] = 0
						legs_data8[apt_ok][43] = ""
						legs_data8[apt_ok][44] = 0
						legs_data8[apt_ok][45] = 0
						legs_data8[apt_ok][46] = 0
						legs_data8[apt_ok][47] = 0
						legs_data8[apt_ok][48] = ""
						legs_data8[apt_ok][49] = 0
						legs_data8[apt_ok][50] = 0
						legs_data8[apt_ok][51] = 0
						legs_data8[apt_ok][52] = 0
						legs_data8[apt_ok][53] = 0
						legs_data8[apt_ok][54] = 0
						
						if rte12_act == 1 then
							offset_mod2 = offset
						else
							offset_mod2 = 2
						end
						if offset_mod2 > legs_num8 then
							offset_mod2 = legs_num8
						end
						
					elseif legs_num8 > 0 then
						legs_data8[legs_num8 + 1][1] = des_icao7_x	--des_icao_x
						legs_data8[legs_num8 + 1][7] = des_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[legs_num8 + 1][8] = des_icao_lon7_x	--des_icao_lon		-- longitude
					end
					
					
					des_app_rw_only7_x = 0
					des_star8 = "------"
					des_star_trans8 = "------"
					des_app8 = "------"
					des_app_tns8 = "------"
					if rte12_act == 1 then
						legs_delete = 1
					end
				end
				arr_data7_x = 0
				read_des_list_icao(des_icao7_x, 1)
				
				sel_des_star8 = "------"
				sel_des_star_trans8 = "------"
				sel_des_app8 = "------"
				sel_des_app_tns8 = "------"
				
				create_star_list(1)
				create_star_tns_list(1)
				create_app_list(1)
				create_app_tns_list(1)
				
			end
		elseif page_rte_init == 1 then
			if act_page == 1 then
				if (rte12_sel == 0 and co_route_x ~= "------------" and legs_num2 > 1) or (rte12_sel == 1 and co_route7_x ~= "------------" and legs_num8 > 1) then
					-- REVERSE
				end
			else
				-- button 4 RSK
				rte_x_rsk(4, 0, rte12_sel)
			end
		elseif page_legs == 1 then
			local item = 0
			local button = 4	-- button 4 RSK
			
			local ab_avail = 0
			if B738DR_no_rte2 == 0 and act_page == 1 then
				if legs_intdir ~= 0 and legs_intdir_rte == rte12_act then
					if B738DR_legs_abeam_avail ~= 0 then
						ab_avail = 1
					end
				end
			end
			
			if ab_avail == 0 then
				local offset_tmp = offset_mod
				if rte12_sel ~= 0 then
					offset_tmp = offset_mod2
				end
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page > 1 then
					item = item - 1
				end
				
				if entry ~= "" then
					if set_spd_alt_rest(item, rte12_sel) == true then
						if rte12_act == rte12_sel then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				-- RTE COPY
				if B738DR_fmc_exec_lights == 0 then
					if rte12_sel == 0 and legs_num > 1 and rte12_act == rte12_sel then
						if rte12_copy_done == 0 then
							rte12_copy_done = 1
							rte21_copy_done = 0
							run_after_time(rte12_copy_t, 0.5)
						end
					elseif rte12_sel ~= 0 and legs_num > 1 and rte12_act == rte12_sel then
						if rte21_copy_done == 0 then
							rte21_copy_done = 1
							rte12_copy_done = 0
							run_after_time(rte21_copy_t, 0.5)
						end
					end
				end
			end
		elseif page_pos_init == 1 and disable_POS_4R == 0 then
			-- set IRS POS
			if entry == ">DELETE" then
				irs_pos = "*****.*******.*"
				entry = ""
				B738DR_irs_pos_fmc_set = irs_pos
				B738CMD_fmc_to_irs_pos:once()
			else
				if string.len(entry) > 0 then
					local err_pos = 0
					local n = 0
					if string.len(entry) ~= 15
					or string.sub(entry, 6, 6) ~= "."
					or  string.sub(entry, 14, 14) ~= "." then
						err_pos = 1
					end
					n = tonumber(string.sub(entry, 2, 3))
					if (n == nil) or (n < 0) or (n > 90) then
						err_pos = 1
					end
					n = tonumber(string.sub(entry, 4, 5))
					if (n == nil) or (n < 0) or (n > 59) then
						err_pos = 1
					end
					n = tonumber(string.sub(entry, 9, 11))
					if (n == nil) or (n < 0) or (n > 180) then
						err_pos = 1
					end
					n = tonumber(string.sub(entry, 12, 13))
					if (n == nil) or (n < 0) or (n > 59) then
						err_pos = 1
					end
					if string.sub(entry, 1, 1) ~= "N" 
					and string.sub(entry, 1, 1) ~= "S" then
						err_pos = 1
					end
					if string.sub(entry, 8, 8) ~= "E" 
					and string.sub(entry, 8, 8) ~= "W" then
						err_pos = 1
					end
					if err_pos == 0 then
						irs_pos = entry
						entry = ""
						B738DR_irs_pos_fmc_set = irs_pos
						B738CMD_fmc_to_irs_pos:once()
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_n1_limit == 1 then
			-- select CLB-2
			clb_2 = "<SEL>"
			clb = "     "
			clb_1 = "     "
			sel_clb_thr = 1
		elseif page_descent_forecast == 1 then
			-- entry WIND LAYER 2
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					forec_dir_2_mod = "---"
					forec_spd_2_mod = "---"
					entry = ""
					forecast_exec = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						forec_dir_2_mod = string.format("%03d", q)
						forec_spd_2_mod = string.format("%3d", qq)
						entry = ""
						forecast_exec = 1
					end
					-- if strlen > 4 and strlen < 8 and string.sub(entry, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- forec_dir_2_mod = wind_dir
										-- forec_spd_2_mod = string.format("%3d", n)
										-- entry = ""
										-- forecast_exec = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_approach == 1 then
			-- FLAP / SPD
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					app_flap = "--"
					app_spd = "---"
					entry = ""
				elseif strlen == 2 then
					if string.sub(entry, 2, 2) == "/" then
						local flp = string.sub(entry, 1, 1)
						if flp == "0" or flp == "1" or flp == "2" or flp == "5" then
							app_flap = flp
							entry = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n > 250 or n < 100 then	-- SPEED min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--app_flap = flp
								app_spd = string.format("%3d", n)
								entry = ""
							end
						end
					end
				elseif strlen == 3 then
					if string.sub(entry, 3, 3) == "/" then
						local flp = string.sub(entry, 1, 2)
						if flp == "10" or flp == "15" or flp == "25" or flp == "30" or flp == "40" then
							app_flap = flp
							entry = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n > 250 or n < 100 then	-- SPEED min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--app_flap = flp
								app_spd = string.format("%3d", n)
								entry = ""
							end
						end
					end
				elseif strlen == 4 then
					if string.sub(entry, 1, 1) == "/" then
						local n = tonumber(string.sub(entry, 2, 4))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n > 250 or n < 100 then	-- SPEED min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--app_flap = flp
								app_spd = string.format("%3d", n)
								entry = ""
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 5 then
					if string.sub(entry, 2, 2) == "/" then
						local flp = string.sub(entry, 1, 1)
						if flp == "0" or flp == "1" or flp == "2" or flp == "5" then
							local n = tonumber(string.sub(entry, 3, 5))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n > 250 or n < 100 then	-- SPEED min and max
									add_fmc_msg(INVALID_INPUT, 1)
								else
									app_flap = flp
									app_spd = string.format("%3d", n)
									entry = ""
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 6 then
					if string.sub(entry, 3, 3) == "/" then
						local flp = string.sub(entry, 1, 2)
						if flp == "10" or flp == "15" or flp == "25" or flp == "30" or flp == "40" then
							local n = tonumber(string.sub(entry, 4, 6))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n > 250 or n < 100 then	-- SPEED min and max
									add_fmc_msg(INVALID_INPUT, 1)
								else
									app_flap = flp
									app_spd = string.format("%3d", n)
									entry = ""
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_perf == 1 and disable_PERF_4R == 0 then
			-- entry T/C OAT
			local strlen = string.len(entry)
			local alt_temp = crz_alt_num
			local n = 0
			local n_str = ""
			if strlen > 0 then
				if entry == ">DELETE" then
					tc_oat_f_mod = "----"
					tc_oat_c_mod = "---"
					isa_dev_f_mod = "----"
					isa_dev_c_mod = "---"
					entry = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						isa_dev_f = isa_dev_f_mod
						isa_dev_c = isa_dev_c_mod
						tc_oat_c = tc_oat_c_mod
						tc_oat_f = tc_oat_f_mod
					end
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen < 6 then
						n = tonumber(entry)
						if n == nil and strlen > 1 then
							n = tonumber(string.sub(entry, 1, strlen-1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = tonumber(string.sub(entry, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n_str = string.sub(entry, strlen, strlen)
									if n_str == "C" then 
										if n < -70 or n > 70 then	-- T/C OAT Celsius min and max
											add_fmc_msg(INVALID_INPUT, 1)
										else
											tc_oat_c_mod = string.format("%3d", n)
											n = (n * 9 / 5) + 32
											tc_oat_f_mod = string.format("%4d", n)
											-- calc ISA DEV
											n = tonumber(tc_oat_c_mod)
											alt_temp = math.min(alt_temp, 37000)
											alt_temp = 15 - (2 * (alt_temp / 1000))
											alt_temp = math.max(alt_temp, -56.5)
											isa_dev_c_mod = string.format("%3d", n - alt_temp)
											n = tonumber(isa_dev_c_mod)
											n = n * 9 / 5
											isa_dev_f_mod = string.format("%4d", n)
											entry = ""
											if perf_exec == 2 then
												perf_exec = 3
											elseif perf_exec < 2 then
												isa_dev_f = isa_dev_f_mod
												isa_dev_c = isa_dev_c_mod
												tc_oat_c = tc_oat_c_mod
												tc_oat_f = tc_oat_f_mod
											end
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									elseif n_str == "F" then
										if n < -94 or n > 158 then	-- T/C OAT Fahrenheit min and max
											add_fmc_msg(INVALID_INPUT, 1)
										else
											tc_oat_f_mod = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											tc_oat_c_mod = string.format("%3d", n)
											-- calc ISA DEV
											n = tonumber(tc_oat_c_mod)
											alt_temp = math.min(alt_temp, 37000)
											alt_temp = 15 - (2 * (alt_temp / 1000))
											alt_temp = math.max(alt_temp, -56.5)
											isa_dev_c_mod = string.format("%3d", n - alt_temp)
											n = tonumber(isa_dev_c_mod)
											n = n * 9 / 5
											isa_dev_f_mod = string.format("%4d", n)
											entry = ""
											if perf_exec == 2 then
												perf_exec = 3
											elseif perf_exec < 2 then
												isa_dev_f = isa_dev_f_mod
												isa_dev_c = isa_dev_c_mod
												tc_oat_c = tc_oat_c_mod
												tc_oat_f = tc_oat_f_mod
											end
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
							end
						else
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if B738DR_fmc_units == 0 then
									if n < -94 or n > 158 then	-- T/C OAT Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										tc_oat_f_mod = string.format("%4d", n)
										n = (n - 32) * 5 / 9
										tc_oat_c_mod = string.format("%3d", n)
										-- calc ISA DEV
										n = tonumber(tc_oat_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										isa_dev_c_mod = string.format("%3d", n - alt_temp)
										n = tonumber(isa_dev_c_mod)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										entry = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								else
									if n < -70 or n > 70 then	-- T/C OAT Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										tc_oat_c_mod = string.format("%3d", n)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										-- calc ISA DEV
										n = tonumber(tc_oat_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										isa_dev_c_mod = string.format("%3d", n - alt_temp)
										n = tonumber(isa_dev_c_mod)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										entry = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_perf == 2 then
			-- DES max speed kts/mach
			local strlen = string.len(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					des_max_kts = "   "
					des_max_mach = "   "
					entry = ""
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_min_mach)
							if nn == nil then
								nn = 0.4
							else
								nn = nn / 1000
							end
							if n > 0.82 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								des_max_mach = string.format("%03d", n)
								entry = ""
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340  or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								des_max_kts = string.format("%03d", n)
								entry = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(des_min_mach)
										if nn == nil then
											nn = 0.4
										else
											nn = nn / 1000
										end
										if n > 0.82 or n < nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											des_max_kts = kts
											des_max_mach = string.format("%03d", n)
											entry = ""
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		----------
		elseif page_rte_legs > 0 then
			-- entry WIND DATA
			local strlen = string.len(entry)
			local item = 0
			local button = 4	-- button 4 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry == ">DELETE" then
					legs_data2[item][39] = ""
					entry = ""
					if rte12_act == rte12_sel then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry = ""
						if rte12_act == rte12_sel then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			-- enter EO ACCELERATION HEIGHT AGL
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					eo_accel_alt = "----"
					eo_accel_alt_num = B738DR_def_eo_accel_alt
					entry = ""
				elseif strlen > 2 and strlen < 5 then
					local n = tonumber(entry)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 400 or n > 9999 then	-- ACCEL HT AGL min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							eo_accel_alt = string.format("%4d", n)
							entry = ""
							eo_accel_alt_num = n
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_nearest_apt == 1 and act_page == 1 then
			if nearest_apt_list_num >= 4 then
				act_page = 5
			end
		elseif page_altern_apt == 1 and act_page == 1 then
			if altern_apt_list_num >= 4 then
				act_page = 5
			end
		elseif page_dl_acars == 1 then
			page_dl_acars = 0
			page_dl_requests = 1
			act_page = 1
			display_update = 1
		elseif page_eo_cruise == 1 then
			engout_crz = 3
		elseif page_eo_climb == 1 then
			engout_clb = 3
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 5RSK
function B738_fmc1_5R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if page_dep == 1 then
			-- if in_flight_mode == 0 then
				if rte12_sel == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
					if sel_ref_rwy2 == "-----" then
						if ref_rwy_sel[5] ~= "" then
							if ref_rwy2 == ref_rwy_sel[5] then
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel)
							else
								ref_rwy2 = ref_rwy_sel[5]
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel)
								ref_rwy3 = ref_rwy2
								act_page = 1
								ref_rwy_exec = 1
							end
						end
					end
					rte_add_dep_arr(rte12_sel)
					-- check SID for selected runway
					if rte12_act == rte12_sel then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				elseif rte12_sel == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
					if sel_ref_rwy8 == "-----" then
						if ref_rwy_sel[5] ~= "" then
							if ref_rwy8 == ref_rwy_sel[5] then
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel)
							else
								ref_rwy8 = ref_rwy_sel[5]
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel)
								ref_rwy9 = ref_rwy8
								act_page = 1
								ref_rwy_exec = 1
							end
						end
					end
					rte_add_dep_arr(rte12_sel)
					-- check SID for selected runway
					if rte12_act == rte12_sel then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				end
			-- end
		elseif page_arr == 1 then
			if rte12_sel == 0 then
				if sel_des_app2 == "------" then
					if des_app_sel[5] ~= "" then
						if des_app2 == des_app_sel[5] then
							sel_des_app2 = des_app2
							if string.sub(sel_des_app2, 1, 2) == "RW" then
								des_app_rw_only_x = 1
								des_star_trans2 = "------"
								sel_des_star_trans2 = des_star_trans2
								create_star_list(rte12_sel)
							else
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
							end
							if sel_des_star2 == "------" then
								create_star_list(rte12_sel)
							end
							create_app_tns_list(rte12_sel)
						else
							des_app2 = des_app_sel[5]
							sel_des_app2 = des_app2
							if string.sub(sel_des_app2, 1, 2) == "RW" then
								des_app_rw_only_x = 1
								des_star_trans2 = "------"
								sel_des_star_trans2 = des_star_trans2
								create_star_list(rte12_sel)
							else
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
							end
							if sel_des_star2 == "------" then
								create_star_list(rte12_sel)
							end
							des_app_exec = 1
							create_app_tns_list(rte12_sel)
						end
						act_page = 1
					end
				else
					if sel_des_app_tns2 == "------" then
						if des_tns_sel[5] ~= "" then
							if des_app_tns2 == des_tns_sel[5] then
								sel_des_app_tns2 = des_app_tns2
							else
								des_app_tns2 = des_tns_sel[5]
								sel_des_app_tns2 = des_app_tns2
								des_app_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			else
				if sel_des_app8 == "------" then
					if des_app_sel[5] ~= "" then
						if des_app8 == des_app_sel[5] then
							sel_des_app8 = des_app8
							if string.sub(sel_des_app8, 1, 2) == "RW" then
								des_app_rw_only7_x = 1
								des_star_trans8 = "------"
								sel_des_star_trans8 = des_star_trans8
								create_star_list(rte12_sel)
							else
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
							end
							if sel_des_star8 == "------" then
								create_star_list(rte12_sel)
							end
							create_app_tns_list(rte12_sel)
						else
							des_app8 = des_app_sel[5]
							sel_des_app8 = des_app8
							if string.sub(sel_des_app8, 1, 2) == "RW" then
								des_app_rw_only7_x = 1
								des_star_trans8 = "------"
								sel_des_star_trans8 = des_star_trans8
								create_star_list(rte12_sel)
							else
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
							end
							if sel_des_star8 == "------" then
								create_star_list(rte12_sel)
							end
							des_app_exec = 1
							create_app_tns_list(rte12_sel)
						end
						act_page = 1
					end
				else
					if sel_des_app_tns8 == "------" then
						if des_tns_sel[5] ~= "" then
							if des_app_tns8 == des_tns_sel[5] then
								sel_des_app_tns8 = des_app_tns8
							else
								des_app_tns8 = des_tns_sel[5]
								sel_des_app_tns8 = des_app_tns8
								des_app_tns_exec = 1
							end
							act_page = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel)
		elseif page_rte_init == 1 then
			if act_page == 1 then
				if B738DR_fmc_exec_lights == 0 then
					if rte12_sel == 0 and legs_num > 1 and rte12_act == rte12_sel then
						if rte12_copy_done == 0 then
							rte12_copy_done = 1
							rte21_copy_done = 0
							run_after_time(rte12_copy_t, 0.5)
						end
					elseif rte12_sel ~= 0 and legs_num > 1 and rte12_act == rte12_sel then
						if rte21_copy_done == 0 then
							rte21_copy_done = 1
							rte12_copy_done = 0
							run_after_time(rte21_copy_t, 0.5)
						end
					end
				end
			elseif act_page > 1 then
				-- button 5 RSK
				rte_x_rsk(5, 0, rte12_sel)
			end
		elseif page_legs == 1 then
			local item = 0
			local button = 5	-- button 5 RSK
			
			if B738DR_no_rte2 ~= 0 or act_page ~= 1 then
				
				local ab_avail = 0
				if B738DR_no_rte2 ~= 0 and act_page == 1 then
					if legs_intdir ~= 0 then
						if B738DR_legs_abeam_avail ~= 0 then
							ab_avail = 1
						end
					end
				end
				
				if ab_avail == 0 then
					local offset_tmp = offset_mod
					if rte12_sel ~= 0 then
						offset_tmp = offset_mod2
					end
					item = (act_page - 1) * 5 + offset_tmp - 1 + button
					if B738DR_no_rte2 == 0 and act_page > 1 then
						item = item - 1
					end
					
					if entry ~= "" then
						if set_spd_alt_rest(item, rte12_sel) == true then
							if rte12_act == rte12_sel then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
						end
					end
				else
					if legs_abeam_selected == 0 then
						legs_abeam_selected = 1
					else
						legs_abeam_selected = 0
					end
				end
			else
				-- if legs_abeam_data_n > 0 and legs_intdir ~= 0 then
				if B738DR_legs_abeam_avail ~= 0 and legs_intdir ~= 0 and legs_intdir_rte == rte12_act then
					if legs_abeam_selected == 0 then
						legs_abeam_selected = 1
					else
						legs_abeam_selected = 0
					end
				end
			end
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			if verify_to_spd ~= 0 then
				-- ACCEPT
				-- v1_set = v1
				-- vr_set = vr
				-- v2_set = v2
				verify_to_spd = 0
				qrh = " ON"
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			-- entry THR REDUCTION ALT AGL
			local strlen = string.len(entry)
			if B738DR_thr_cutback == 0 then
				if strlen > 0 then
					if entry == ">DELETE" then
						clb_alt = "----"
						if B738DR_thr_cutback == 0 then
							clb_alt_num = B738DR_def_clb_alt
						else
							clb_alt_num = B738DR_def_clb_alt2
						end
						entry = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 800 or n > 9999 then	-- THR RED ALT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_alt = string.format("%4d", n)
								entry = ""
								clb_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				if strlen > 0 then
					if entry == ">DELETE" then
						clb_alt = "----"
						if B738DR_thr_cutback == 0 then
							clb_alt_num = B738DR_def_clb_alt
						else
							clb_alt_num = B738DR_def_clb_alt2
						end
						entry = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 800 or n > 9999 then	-- THR RED ALT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_alt = string.format("%4d", n)
								entry = ""
								clb_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_approach == 1 then
			-- WIND CORR
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					wind_corr = "--"
					entry = ""
				elseif strlen < 3 then
					local n = tonumber(entry)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 5 or n > 20 then	-- WIND CORR min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							wind_corr = string.format("%02d", n)
							entry = ""
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				if wind_corr == "--" then
					entry = 5
				else
					entry = wind_corr
				end
			end
		elseif page_descent_forecast == 1 then
			-- entry WIND LAYER 3
			local strlen = string.len(entry)
			if strlen > 0 then
				if entry == ">DELETE" then
					forec_dir_3_mod = "---"
					forec_spd_3_mod = "---"
					entry = ""
					forecast_exec = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						forec_dir_3_mod = string.format("%03d", q)
						forec_spd_3_mod = string.format("%3d", qq)
						entry = ""
						forecast_exec = 1
					end
					-- if strlen > 4 and strlen < 8 and string.sub(entry, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- forec_dir_3_mod = wind_dir
										-- forec_spd_3_mod = string.format("%3d", n)
										-- entry = ""
										-- forecast_exec = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_perf == 1 then
			-- entry Trans alt
			local strlen = string.len(entry)
			local n = tonumber(entry)
			local n_str = ""
			if strlen > 0 then
				if entry == ">DELETE" then
					trans_alt_mod = "-----"
					n = 18000
					if crz_alt ~= "*****" then
						n_str = string.sub(crz_alt, 1, 2)
						if crz_alt_num >= n then
							if n_str ~= "FL" then
								--n_str = string.sub(crz_alt)
								n = tonumber(crz_alt) / 100
								crz_alt = "FL" .. string.format("%03d", n)
							end
						else
							if n_str == "FL" then
								n_str = string.sub(crz_alt, 3, 5)
								n = tonumber(n_str) * 100
								crz_alt = string.format("%5d", n)
							end
						end
					end
					entry = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						trans_alt = trans_alt_mod
					end
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 1000 or n > 99999 then	-- Trans alt min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							trans_alt_mod = string.format("%5d", n)
							if crz_alt ~= "*****" then
								n_str = string.sub(crz_alt, 1, 2)
								if crz_alt_num >= n then
									if n_str ~= "FL" then
										--n_str = string.sub(crz_alt, 3, 5)
										n = tonumber(crz_alt) / 100
										crz_alt = "FL" .. string.format("%03d", n)
									end
								else
									if n_str == "FL" then
										n_str = string.sub(crz_alt, 3, 5)
										n = tonumber(n_str) * 100
										crz_alt = string.format("%5d", n)
									end
								end
							end
							entry = ""
							if perf_exec == 2 then
								perf_exec = 3
							elseif perf_exec < 2 then
								trans_alt = trans_alt_mod
							end
						end
					end
				end
			end
		elseif page_ref_nav_data_wpt == 1 then
			local strlen = string.len(entry)
			local ii = 0
			local jj = 0
			local entry_ok = 0
			if strlen > 0 then
				if entry == ">DELETE" and ref_nav_brg ~= "-----" then
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					ref_nav_lat = 0
					ref_nav_lon = 0
					ref_nav_exec = 1
					entry = ""
				else
					if strlen > 4 and strlen < 8 and string.sub(entry, 4, 4) == "/" then
						ii = tonumber(string.sub(entry, 1, 3))
						if ii ~= nil then
							if ii >= 0 and ii <= 359 then
								entry_ok = entry_ok + 1
							end
						end
						jj = tonumber(string.sub(entry, 5, -1))
						if jj ~= nil then
							if jj >= 0 and jj <= 999 then
								entry_ok = entry_ok + 1
							end
						end
						entry = ""
					end
					if entry_ok == 2 then
						ref_nav_brg = string.format("%03d", ii)
						ref_nav_dist = string.format("%3d", jj)
						-- calc lat / lon
						if ref_nav_id ~= "-----" then
							calc_brg_dist(ref_nav_id_lat, ref_nav_id_lon, math.rad(tonumber(ref_nav_brg)), tonumber(ref_nav_dist))
							ref_nav_lat = calc_lat
							ref_nav_lon = calc_lon
							ref_nav_exec = 1
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_pos_init == 1 and disable_POS_5R == 0 then
			-- SET IRS HDG
			local strlen = string.len(entry)
			local n = tonumber(entry)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry == ">DELETE" then
					irs_hdg = "---`"
					B738DR_irs_hdg_fmc_set = string.sub(irs_hdg, 1, 3)
					B738CMD_fmc_to_irs_hdg:once()
					entry = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 0 or n > 360 or strlen ~= 3 then	-- HDG min and max
							-- TO DO INHIBIT FRAC NUMBER
							add_fmc_msg(INVALID_INPUT, 1)
						else
							n = n % 360
							irs_hdg = string.format("%03d", n)
							irs_hdg = irs_hdg .. "`"
							entry = ""
							B738DR_irs_hdg_fmc_set = string.sub(irs_hdg, 1, 3)
							B738CMD_fmc_to_irs_hdg:once()
						end
					end
				end
			end
		---
		elseif page_rte_legs > 0 then
			-- entry WIND DATA
			local strlen = string.len(entry)
			local item = 0
			local button = 5	-- button 5 RSK
			local qqq = 0
			
			if B738DR_no_rte2 ~= 0 or act_page ~= 1 then
				-- local offset_tmp = offset
				-- if legs_num <= 1 then
					-- offset_tmp = 1
				-- end
				local offset_tmp = offset_mod
				if nav_mode == 99 then
					item = legs_num2 + 2
				else
					item = (act_page - 1) * 5 + offset_tmp - 1 + button
					if B738DR_no_rte2 == 0 and act_page > 1 then
						item = item - 1
					end
				end
				
				if strlen > 0 and item <= legs_num2 and item > 0 then
					if entry == ">DELETE" then
						legs_data2[item][39] = ""
						entry = ""
						if rte12_act == rte12_sel then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					else
						local q = 0
						local qq = 0
						q, qq = entry_wind(entry)
						if qq == 0 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
							if item + 1 <= legs_num2 + 1 then
								for qqq = item + 1, legs_num2 + 1 do
									legs_data2[qqq][38] = legs_data2[item][39]
								end
							end
							entry = ""
							if rte12_act == rte12_sel then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
						end
					end
				else
					if legs_data2[item][39] ~= "" then
						-- entry = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
						entry = string.sub(legs_data2[item][39], 1, 3) .. "/"
						entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
					elseif legs_data2[item][38] ~= "" then
						-- entry = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
						entry = string.sub(legs_data2[item][38], 1, 3) .. "/"
						entry = entry .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
					end
				end
			end
		elseif page_ac_atis == 1 then
			-- send ACARS ATIS req
			if acars_atis_status == 0 and ac_atis_icao ~= "----" then	--and B738DR_com3_data ~= 0 then
				acars_atis_status = 1
				atis_msg_status = 0
				vhf_in_prog = " VHF IN PROG"
				receive_msg = 0
--				atis_msg_status = 0
				atis_send_time = "     "
			end
		elseif page_descent == 1 then
			if vnav_desc_spd_disable == 0 then
				if fmc_descent_mode_mod == 1 or fmc_descent_mode_mod == 3 then
					-- DES SPD
					if fmc_descent_mode_mod ~= 0 then
						if fmc_descent_mode_mod == 1 then
							-- DES PTH ECON
							entry = ""
							fmc_descent_mode_mod = 0
							des_spd_exec = 1
							fmc_descent_speed_mod = econ_des_spd
							fmc_descent_speed_mach_mod = econ_des_spd_mach
						else
							-- DES PTH
							entry = ""
							fmc_descent_mode_mod = 2
							des_spd_exec = 1
						end
					end
				else
					-- DES PTH
					if fmc_descent_mode_mod == 0 then
						-- DES SPD ECON
						entry = ""
						fmc_descent_mode_mod = 1
						des_spd_exec = 1
						fmc_descent_speed_mod = econ_des_spd
						fmc_descent_speed_mach_mod = econ_des_spd_mach
					else
						-- DES SPD
						entry = ""
						fmc_descent_mode_mod = 3
						des_spd_exec = 1
					end
				end
			end
		elseif page_nearest_apt == 1 and act_page == 1 then
			if nearest_apt_list_num >= 5 then
				act_page = 6
			end
		elseif page_altern_apt == 1 and act_page == 1 then
			if altern_apt_list_num >= 5 then
				act_page = 6
			end
		elseif page_dl_wxr_req == 1 then
			-- send ICAO WXR request
			if acars_atis_status == 0 and ac_atis_icao ~= "----" then
				acars_atis_status = 1
				atis_msg_status = 0
				vhf_in_prog = " VHF IN PROG"
				receive_msg = 0
				atis_send_time = "     "
				page_dl_wxr_req = 0
				page_dl_requests = 1
				act_page = 1
				display_update = 1
			end
		elseif page_dl_fltlog == 1 then
			page_dl_fltlog = 0
			page_dl_sensors = 1
			act_page = 1
			display_update = 1
		elseif page_cruise == 1 then
			if engout_crz == 0 then
				engout_crz = 1
			elseif engout_crz == 1 then
				engout_crz = 3
				page_cruise = 0
				page_eo_cruise = 1
			else
				page_cruise = 0
				page_eo_cruise = 1
			end
		elseif page_climb == 1 then
			if engout_clb == 0 then
				engout_clb = 1
			else
				engout_clb = 3
				page_climb = 0
				page_eo_climb = 1
			end
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 6RSK
function B738_fmc1_6R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_rte_init == 1 then
			if B738DR_fmc_exec_lights == 0 then
				if legs_num > 1 and rte12_act == rte12_sel then
					if in_flight_mode == 0 then
						if pre_flt_perf_init == 0 then
							-- go to perf init
							page_perf = 1
							page_rte_init = 0
						else
							-- go to takeoff
							page_takeoff = 1
							page_rte_init = 0
						end
					else
						-- go to offset page
						page_rte_init = 0
						page_offset = 1
						item_sel = 0
						item_sel_act = 0
						item_sel_via = 0
					end
				else
					if legs_num2 > 1 and rte12_act ~= rte12_sel and rte12_sel == 0 then
						-- ACTIVATE Flight plan
						rte_exec = 1
						exec_activate_fpln = 1
						exec_activate_fpln2 = 0
						
						copy_to_legs_data2_buf(1)
						if legs_num2_buf > 1 then
							for n = 1, (legs_num2_buf + 1) do
								legs_data2_buf[n][11] = 0
								legs_data2_buf[n][10] = 0
								legs_data2_buf[n][14] = 50000
								legs_data2_buf[n][38] = ""
							end
						end
						copy_from_legs_data2_buf(1)
						
						local disable_disco = false
						if in_flight_mode == 0 then
							disable_disco = true
						elseif offset_mod > 0 and offset_mod <= legs_num2 then
							if legs_data2[offset_mod][1] == "DISCONTINUITY" then
								disable_disco = true
							end
						end
						if disable_disco == false then
							--create discontinuity
							rte_copy(offset_mod, rte12_sel)
							rte_add_disco(offset_mod, rte12_sel)
							rte_paste(offset_mod + 1, rte12_sel)
						end
					elseif legs_num8 > 1 and rte12_act ~= rte12_sel and rte12_sel == 1 then
						-- ACTIVATE Flight plan
						rte_exec2 = 1
						exec_activate_fpln2 = 1
						exec_activate_fpln = 0
						
						copy_to_legs_data2_buf(0)
						if legs_num2_buf > 1 then
							for n = 1, (legs_num2_buf + 1) do
								legs_data2_buf[n][11] = 0
								legs_data2_buf[n][10] = 0
								legs_data2_buf[n][14] = 50000
								legs_data2_buf[n][38] = ""
							end
						end
						copy_from_legs_data2_buf(0)
						
						local disable_disco = false
						if in_flight_mode == 0 then
							disable_disco = true
						elseif offset_mod2 > 0 and offset_mod2 <= legs_num8 then
							if legs_data8[offset_mod2][1] == "DISCONTINUITY" then
								disable_disco = true
							end
						end
						if disable_disco == false then
							--create discontinuity
							rte_copy(offset_mod2, rte12_sel)
							rte_add_disco(offset_mod2, rte12_sel)
							rte_paste(offset_mod2 + 1, rte12_sel)
						end
					end
				end
			end
		elseif page_dep == 1 then
			-- go to route
			reset_fmc_pages()
			page_rte_init = 1
			act_page = 1
		elseif page_arr == 1 then
			-- go to route
			reset_fmc_pages()
			page_rte_init = 1
			act_page = 1
		elseif page_legs == 1 then
			local strlen = string.len(entry)
			local n = tonumber(entry)
			local rte2_corr = 0
			if B738DR_fmc_exec_lights == 1 then --and legs_intdir == 1 then
				if legs_intdir == 1 and legs_intdir_rte == rte12_act then
					if strlen == 3 then
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 0 or n > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n % 360
								legs_intdir_crs2 = n
								legs_intdir_crs_mod = legs_intdir_crs2
								entry = ""
							end
						end
					elseif strlen == 5 then
						if string.sub(entry, 4, 4) == "." then
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < 0 or n > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n = n % 360
									legs_intdir_crs2 = n
									legs_intdir_crs_mod = legs_intdir_crs2
									entry = ""
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					if B738DR_capt_map_mode == 3 then
						
						local offset_step = 1
						if rte12_sel == 0 then
							if legs_num > 1 and rte12_act == 0 then
								offset_step = offset_mod
							elseif legs_num <= 0 or legs_num2 <= 1 then
								offset_step = offset_mod
							end
							
							legs_step = legs_step + 1
							if legs_step > legs_num2 or legs_step < offset_step then
								legs_step = offset_step
							end
							if legs_step > legs_num2 then
								legs_step = legs_num2
							end
							if legs_data2[legs_step][1] == "DISCONTINUITY" then
								legs_step = legs_step + 1
								if legs_step > legs_num2 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num2 then
									legs_step = legs_num2
								end
							end
							if B738DR_no_rte2 == 0 then
								if (legs_step - offset_step) >= 4 then
									rte2_corr = 1
								end
							end
							page_legs_step = math.floor((legs_step - offset_step + rte2_corr) / 5) + 1
							act_page = page_legs_step
						else
							if legs_num > 1 and rte12_act == 1 then
								offset_step = offset_mod2
							elseif legs_num <= 0 or legs_num2 <= 1 then
								offset_step = offset_mod2
							end
							
							legs_step = legs_step + 1
							if legs_step > legs_num8 or legs_step < offset_step then
								legs_step = offset_step
							end
							if legs_step > legs_num8 then
								legs_step = legs_num8
							end
							if legs_data8[legs_step][1] == "DISCONTINUITY" then
								legs_step = legs_step + 1
								if legs_step > legs_num8 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num8 then
									legs_step = legs_num8
								end
							end
							if B738DR_no_rte2 == 0 then
								if (legs_step - offset_step) >= 4 then
									rte2_corr = 1
								end
							end
							page_legs_step = math.floor((legs_step - offset_step + rte2_corr) / 5) + 1
							act_page = page_legs_step
						end
					else
						-- RTE DATA
						page_rte_legs = 1
						page_legs = 0
					end
				end
			else
				local n = 0
				local kk = 0
				local ii = 0
				local jj = 0
				local lock_hold_ppos = 0
				local offset_step = offset_mod
				if rte12_sel ~= 0 then
					offset_step = offset_mod2
				end
				
				if legs_num > 1 and rte12_act == rte12_sel then
					if B738DR_capt_map_mode == 3 then
						if new_hold ~= 0 and new_hold_at == 0 and rte12_sel == 0 then
							if in_flight_mode == 1 and (nav_mode ~= 3 or rte12_act ~= rte12_sel) and new_hold_at == 0 then
								n = 0
								if hold_data_num2 > 0 then
									for kk = 1, hold_data_num2 do
										if hold_data2[kk] >= offset_mod then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data2[hold_data2[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel = 0
									item_sel_act = 0
									item_sel_via = 0
									entry = ""
									if rte12_act == rte12_sel then
										hold_exec = 1
									end
									page_hold = 1
									page_legs = 0
									act_page = 1
									kk = 0
									legs_data2b = {}
									for ii = offset_mod, legs_num2 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data2[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod
									legs_data2[n] = {}
									legs_data2[n][1] = "PPOS"
									legs_data2[n][2] = 0
									legs_data2[n][3] = 0
									
									legs_data2[n][4] = AP_airspeed	--0
									legs_data2[n][5] = 0
									legs_data2[n][6] = 0
									
									if legs_data2[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data2[n][5] <= 6000 then
											if legs_data2[n][4] > 200 then
												legs_data2[n][4] = 200
											end
										elseif legs_data2[n][5] <= 14000 then
											if legs_data2[n][4] > 230 then
												legs_data2[n][4] = 230
											end
										else
											if legs_data2[n][4] > 265 then
												legs_data2[n][4] = 265
											end
										end
									elseif legs_data2[n][4] > 0 and legs_data2[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data2[n][4] > 0.76 then
											legs_data2[n][4] = 0.76
										end
									end
									
									legs_data2[n][7] = 0		-- latitude
									legs_data2[n][8] = 0		-- longitude
									legs_data2[n][9] = ""			-- via id
									legs_data2[n][10] = 0		-- calc speed
									legs_data2[n][11] = 0		-- calc altitude
									legs_data2[n][12] = 0		-- calc altitude vnav pth
									legs_data2[n][13] = 0
									legs_data2[n][14] = 0		-- rest alt
									legs_data2[n][15] = 0		-- last fuel
									legs_data2[n][16] = ""
									legs_data2[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data2[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data2[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data2[n][20] = 0	-- vpa
									legs_data2[n][21] = 1		-- HOLD right default
									legs_data2[n][22] = ""
									legs_data2[n][23] = 0
									legs_data2[n][24] = 0
									legs_data2[n][25] = 0
									legs_data2[n][26] = 0
									legs_data2[n][27] = ""
									legs_data2[n][28] = ""
									legs_data2[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data2[n][30] = ""
									legs_data2[n][31] = "HM"
									legs_data2[n][32] = 0
									legs_data2[n][33] = ""
									legs_data2[n][34] = ""
									legs_data2[n][35] = ""
									legs_data2[n][36] = 0	--4
									legs_data2[n][37] = 0
									legs_data2[n][38] = ""
									legs_data2[n][39] = ""
									legs_data2[n][40] = 0
									legs_data2[n][41] = 0
									legs_data2[n][42] = 0
									legs_data2[n][43] = ""
									legs_data2[n][44] = 0
									legs_data2[n][45] = 0
									legs_data2[n][46] = 0
									legs_data2[n][47] = 0
									legs_data2[n][48] = ""
									legs_data2[n][49] = 0
									legs_data2[n][50] = 0
									legs_data2[n][51] = 0
									legs_data2[n][52] = -1
									legs_data2[n][53] = 0
									legs_data2[n][54] = 0
									-- add disco
									n = n + 1
									legs_data2[n] = {}
									rte_add_disco(n, rte12_sel)
									legs_data2[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data2[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num2 = n - 1
									
									-- create hold data
									hold_data_num2 = 0
									hold_data2 = {}
									for ii = 1, legs_num2 do
										if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
											hold_data_num2 = hold_data_num2 + 1
											hold_data2[hold_data_num2] = ii
											if ii == new_hold_idx + 1 then
												new_hold_act_page = hold_data_num2
											end
										end
									end
								end
							end
						elseif new_hold2 ~= 0 and new_hold_at2 == 0 and rte12_sel == 1 then
							if in_flight_mode == 1 and (nav_mode ~= 3 or rte12_act ~= rte12_sel) and new_hold_at2 == 0 then
								n = 0
								if hold_data_num4 > 0 then
									for kk = 1, hold_data_num4 do
										if hold_data4[kk] >= offset_mod2 then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data8[hold_data4[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel = 0
									item_sel_act = 0
									item_sel_via = 0
									entry = ""
									if rte12_act == rte12_sel then
										hold_exec2 = 1
									end
									page_hold = 1
									page_legs = 0
									act_page = 1
									kk = 0
									legs_data2b = {}
									for ii = offset_mod2, legs_num8 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data8[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod2
									legs_data8[n] = {}
									legs_data8[n][1] = "PPOS"
									legs_data8[n][2] = 0
									legs_data8[n][3] = 0
									
									legs_data8[n][4] = AP_airspeed	--0
									legs_data8[n][5] = 0
									legs_data8[n][6] = 0
									
									if legs_data8[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data8[n][5] <= 6000 then
											if legs_data8[n][4] > 200 then
												legs_data8[n][4] = 200
											end
										elseif legs_data8[n][5] <= 14000 then
											if legs_data8[n][4] > 230 then
												legs_data8[n][4] = 230
											end
										else
											if legs_data8[n][4] > 265 then
												legs_data8[n][4] = 265
											end
										end
									elseif legs_data8[n][4] > 0 and legs_data8[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data8[n][4] > 0.76 then
											legs_data8[n][4] = 0.76
										end
									end
									
									legs_data8[n][7] = 0		-- latitude
									legs_data8[n][8] = 0		-- longitude
									legs_data8[n][9] = ""			-- via id
									legs_data8[n][10] = 0		-- calc speed
									legs_data8[n][11] = 0		-- calc altitude
									legs_data8[n][12] = 0		-- calc altitude vnav pth
									legs_data8[n][13] = 0
									legs_data8[n][14] = 0		-- rest alt
									legs_data8[n][15] = 0		-- last fuel
									legs_data8[n][16] = ""
									legs_data8[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data8[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data8[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data8[n][20] = 0	-- vpa
									legs_data8[n][21] = 1		-- HOLD right default
									legs_data8[n][22] = ""
									legs_data8[n][23] = 0
									legs_data8[n][24] = 0
									legs_data8[n][25] = 0
									legs_data8[n][26] = 0
									legs_data8[n][27] = ""
									legs_data8[n][28] = ""
									legs_data8[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data8[n][30] = ""
									legs_data8[n][31] = "HM"
									legs_data8[n][32] = 0
									legs_data8[n][33] = ""
									legs_data8[n][34] = ""
									legs_data8[n][35] = ""
									legs_data8[n][36] = 0	--4
									legs_data8[n][37] = 0
									legs_data8[n][38] = ""
									legs_data8[n][39] = ""
									legs_data8[n][40] = 0
									legs_data8[n][41] = 0
									legs_data8[n][42] = 0
									legs_data8[n][43] = ""
									legs_data8[n][44] = 0
									legs_data8[n][45] = 0
									legs_data8[n][46] = 0
									legs_data8[n][47] = 0
									legs_data8[n][48] = ""
									legs_data8[n][49] = 0
									legs_data8[n][50] = 0
									legs_data8[n][51] = 0
									legs_data8[n][52] = -1
									legs_data8[n][53] = 0
									legs_data8[n][54] = 0
									-- add disco
									n = n + 1
									legs_data8[n] = {}
									rte_add_disco(n, rte12_sel)
									legs_data8[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data8[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data8[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num8 = n - 1
									
									-- create hold data
									hold_data_num4 = 0
									hold_data4 = {}
									for ii = 1, legs_num8 do
										if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
											hold_data_num4 = hold_data_num4 + 1
											hold_data4[hold_data_num4] = ii
											if ii == new_hold_idx2 + 1 then
												new_hold_act_page2 = hold_data_num4
											end
										end
									end
								end
							end
						else
							-- STEP
							if rte12_sel == 0 then
								legs_step = legs_step + 1
								if legs_step > legs_num2 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num2 then
									legs_step = legs_num2
								end
								if legs_data2[legs_step][1] == "DISCONTINUITY" then
									legs_step = legs_step + 1
									if legs_step > legs_num2 or legs_step < offset_step then
										legs_step = offset_step
									end
									if legs_step > legs_num2 then
										legs_step = legs_num2
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step - offset_mod) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step = math.floor((legs_step - offset_mod + rte2_corr) / 5) + 1
								act_page = page_legs_step
							else
								legs_step = legs_step + 1
								if legs_step > legs_num8 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num8 then
									legs_step = legs_num8
								end
								if legs_data8[legs_step][1] == "DISCONTINUITY" then
									legs_step = legs_step + 1
									if legs_step > legs_num8 or legs_step < offset_step then
										legs_step = offset_step
									end
									if legs_step > legs_num8 then
										legs_step = legs_num8
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step - offset_mod2) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step = math.floor((legs_step - offset_mod2 + rte2_corr) / 5) + 1
								act_page = page_legs_step
							end
						end
					else
						if new_hold == 0 and rte12_sel == 0 then
							-- RTE DATA
							page_rte_legs = 1
							page_legs = 0
							--act_page = 1
						elseif new_hold2 == 0 and rte12_sel == 1 then
							-- RTE DATA
							page_rte_legs = 1
							page_legs = 0
							--act_page = 1
						else
							if in_flight_mode == 1 and (nav_mode ~= 3 or rte12_act ~= rte12_sel) and new_hold_at2 == 0 then
								n = 0
								if hold_data_num2 > 0 then
									for kk = 1, hold_data_num2 do
										-- if hold_data2[kk] >= offset then
										if hold_data2[kk] >= offset_mod then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data2[hold_data2[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel = 0
									item_sel_act = 0
									item_sel_via = 0
									entry = ""
									if rte12_act == rte12_sel then
										hold_exec = 1
									end
									page_hold = 1
									page_legs = 0
									act_page = 1
									kk = 0
									legs_data2b = {}
									for ii = offset_mod, legs_num2 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data2[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod
									legs_data2[n] = {}
									legs_data2[n][1] = "PPOS"
									legs_data2[n][2] = 0
									legs_data2[n][3] = 0
									legs_data2[n][4] = AP_airspeed	--0
									legs_data2[n][5] = 0
									legs_data2[n][6] = 0
									
									if legs_data2[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data2[n][5] <= 6000 then
											if legs_data2[n][4] > 200 then
												legs_data2[n][4] = 200
											end
										elseif legs_data2[n][5] <= 14000 then
											if legs_data2[n][4] > 230 then
												legs_data2[n][4] = 230
											end
										else
											if legs_data2[n][4] > 265 then
												legs_data2[n][4] = 265
											end
										end
									elseif legs_data2[n][4] > 0 and legs_data2[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data2[n][4] > 0.76 then
											legs_data2[n][4] = 0.76
										end
									end
									
									legs_data2[n][7] = 0		-- latitude
									legs_data2[n][8] = 0		-- longitude
									legs_data2[n][9] = ""			-- via id
									legs_data2[n][10] = 0		-- calc speed
									legs_data2[n][11] = 0		-- calc altitude
									legs_data2[n][12] = 0		-- calc altitude vnav pth
									legs_data2[n][13] = 0
									legs_data2[n][14] = 0		-- rest alt
									legs_data2[n][15] = 0		-- last fuel
									legs_data2[n][16] = ""
									legs_data2[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data2[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data2[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data2[n][20] = 0	-- vpa
									legs_data2[n][21] = 1		-- HOLD right default
									legs_data2[n][22] = ""
									legs_data2[n][23] = 0
									legs_data2[n][24] = 0
									legs_data2[n][25] = 0
									legs_data2[n][26] = 0
									legs_data2[n][27] = ""
									legs_data2[n][28] = ""
									legs_data2[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data2[n][30] = ""
									legs_data2[n][31] = "HM"
									legs_data2[n][32] = 0
									legs_data2[n][33] = ""
									legs_data2[n][34] = ""
									legs_data2[n][35] = ""
									legs_data2[n][36] = 0	--4
									legs_data2[n][37] = 0
									legs_data2[n][38] = ""
									legs_data2[n][39] = ""
									legs_data2[n][40] = 0
									legs_data2[n][41] = 0
									legs_data2[n][42] = 0
									legs_data2[n][43] = ""
									legs_data2[n][44] = 0
									legs_data2[n][45] = 0
									legs_data2[n][46] = 0
									legs_data2[n][47] = 0
									legs_data2[n][48] = ""
									legs_data2[n][49] = 0
									legs_data2[n][50] = 0
									legs_data2[n][51] = 0
									legs_data2[n][52] = -1
									legs_data2[n][53] = 0
									legs_data2[n][54] = 0
									-- add disco
									n = n + 1
									legs_data2[n] = {}
									rte_add_disco(n, rte12_sel)
									legs_data2[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data2[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num2 = n - 1
									
									-- create hold data
									hold_data_num2 = 0
									hold_data2 = {}
									for ii = 1, legs_num2 do
										if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
											hold_data_num2 = hold_data_num2 + 1
											hold_data2[hold_data_num2] = ii
											if ii == new_hold_idx + 1 then
												new_hold_act_page = hold_data_num2
											end
										end
									end
								end
							elseif in_flight_mode == 1 and (nav_mode ~= 3 or rte12_act ~= rte12_sel) and new_hold_at2 == 0 then
								n = 0
								if hold_data_num4 > 0 then
									for kk = 1, hold_data_num4 do
										if hold_data4[kk] >= offset_mod2 then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data8[hold_data4[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel = 0
									item_sel_act = 0
									item_sel_via = 0
									entry = ""
									if rte12_act == rte12_sel then
										hold_exec2 = 1
									end
									page_hold = 1
									page_legs = 0
									act_page = 1
									kk = 0
									legs_data2b = {}
									for ii = offset_mod2, legs_num8 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data8[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod2
									legs_data8[n] = {}
									legs_data8[n][1] = "PPOS"
									legs_data8[n][2] = 0
									legs_data8[n][3] = 0
									legs_data8[n][4] = AP_airspeed	--0
									legs_data8[n][5] = 0
									legs_data8[n][6] = 0
									
									if legs_data8[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data8[n][5] <= 6000 then
											if legs_data8[n][4] > 200 then
												legs_data8[n][4] = 200
											end
										elseif legs_data8[n][5] <= 14000 then
											if legs_data8[n][4] > 230 then
												legs_data8[n][4] = 230
											end
										else
											if legs_data8[n][4] > 265 then
												legs_data8[n][4] = 265
											end
										end
									elseif legs_data8[n][4] > 0 and legs_data8[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data8[n][4] > 0.76 then
											legs_data8[n][4] = 0.76
										end
									end
									
									legs_data8[n][7] = 0		-- latitude
									legs_data8[n][8] = 0		-- longitude
									legs_data8[n][9] = ""			-- via id
									legs_data8[n][10] = 0		-- calc speed
									legs_data8[n][11] = 0		-- calc altitude
									legs_data8[n][12] = 0		-- calc altitude vnav pth
									legs_data8[n][13] = 0
									legs_data8[n][14] = 0		-- rest alt
									legs_data8[n][15] = 0		-- last fuel
									legs_data8[n][16] = ""
									legs_data8[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data8[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data8[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data8[n][20] = 0	-- vpa
									legs_data8[n][21] = 1		-- HOLD right default
									legs_data8[n][22] = ""
									legs_data8[n][23] = 0
									legs_data8[n][24] = 0
									legs_data8[n][25] = 0
									legs_data8[n][26] = 0
									legs_data8[n][27] = ""
									legs_data8[n][28] = ""
									legs_data8[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data8[n][30] = ""
									legs_data8[n][31] = "HM"
									legs_data8[n][32] = 0
									legs_data8[n][33] = ""
									legs_data8[n][34] = ""
									legs_data8[n][35] = ""
									legs_data8[n][36] = 0	--4
									legs_data8[n][37] = 0
									legs_data8[n][38] = ""
									legs_data8[n][39] = ""
									legs_data8[n][40] = 0
									legs_data8[n][41] = 0
									legs_data8[n][42] = 0
									legs_data8[n][43] = ""
									legs_data8[n][44] = 0
									legs_data8[n][45] = 0
									legs_data8[n][46] = 0
									legs_data8[n][47] = 0
									legs_data8[n][48] = ""
									legs_data8[n][49] = 0
									legs_data8[n][50] = 0
									legs_data8[n][51] = 0
									legs_data8[n][52] = -1
									legs_data8[n][53] = 0
									legs_data8[n][54] = 0
									-- add disco
									n = n + 1
									legs_data8[n] = {}
									rte_add_disco(n, rte12_sel)
									legs_data8[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data8[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data8[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num8 = n - 1
									
									-- create hold data
									hold_data_num4 = 0
									hold_data4 = {}
									for ii = 1, legs_num8 do
										if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
											hold_data_num4 = hold_data_num4 + 1
											hold_data4[hold_data_num4] = ii
											if ii == new_hold_idx2 + 1 then
												new_hold_act_page2 = hold_data_num4
											end
										end
									end
								end
							end
						end
					end
				else
					if rte12_sel == 0 then
						if legs_num2 > 1 and rte12_act ~= rte12_sel then
							if B738DR_capt_map_mode == 3 then
								legs_step = legs_step + 1
								if legs_step > legs_num2 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num2 then
									legs_step = legs_num2
								end
								if legs_data2[legs_step][1] == "DISCONTINUITY" then
									legs_step = legs_step + 1
									if legs_step > legs_num2 or legs_step < offset_step then
										legs_step = offset_step
									end
									if legs_step > legs_num2 then
										legs_step = legs_num2
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step - offset_mod) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step = math.floor((legs_step - offset_mod + rte2_corr) / 5) + 1
								act_page = page_legs_step
								
							else
								-- ACTIVATE Flight plan
								rte_exec = 1
								exec_activate_fpln = 1
								exec_activate_fpln2 = 0
								
								copy_to_legs_data2_buf(1)
								if legs_num2_buf > 1 then
									for n = 1, (legs_num2_buf + 1) do
										legs_data2_buf[n][11] = 0
										legs_data2_buf[n][10] = 0
										legs_data2_buf[n][14] = 50000
										legs_data2_buf[n][38] = ""
									end
								end
								copy_from_legs_data2_buf(1)
								
								local disable_disco = false
								if in_flight_mode == 0 then
									disable_disco = true
								elseif offset_mod > 0 and offset_mod <= legs_num2 then
									if legs_data2[offset_mod][1] == "DISCONTINUITY" then
										disable_disco = true
									end
								end
								if disable_disco == false then
									--create discontinuity
									rte_copy(offset_mod, rte12_sel)
									rte_add_disco(offset_mod, rte12_sel)
									rte_paste(offset_mod + 1, rte12_sel)
								end
							end
						else
							if B738DR_capt_map_mode == 3 then
								legs_step = legs_step + 1
								if legs_step > legs_num2 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num2 then
									legs_step = legs_num2
								end
								if legs_data2[legs_step][1] == "DISCONTINUITY" then
									legs_step = legs_step + 1
									if legs_step > legs_num2 or legs_step < offset_step then
										legs_step = offset_step
									end
									if legs_step > legs_num2 then
										legs_step = legs_num2
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step - offset_mod) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step = math.floor((legs_step - offset_mod + rte2_corr) / 5) + 1
								act_page = page_legs_step
								
							else
								-- RTA DATA
								page_rte_legs = 1
								page_legs = 0
							end
						end
					else
						if legs_num8 > 1 and rte12_act ~= rte12_sel then
							if B738DR_capt_map_mode == 3 then
								legs_step = legs_step + 1
								if legs_step > legs_num8 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num8 then
									legs_step = legs_num8
								end
								if legs_data8[legs_step][1] == "DISCONTINUITY" then
									legs_step = legs_step + 1
									if legs_step > legs_num8 or legs_step < offset_step then
										legs_step = offset_step
									end
									if legs_step > legs_num8 then
										legs_step = legs_num8
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step - offset_mod2) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step = math.floor((legs_step - offset_mod2 + rte2_corr) / 5) + 1
								act_page = page_legs_step
								
							else
								-- ACTIVATE Flight plan
								rte_exec2 = 1
								exec_activate_fpln2 = 1
								exec_activate_fpln = 0
								
								copy_to_legs_data2_buf(0)
								if legs_num2_buf > 1 then
									for n = 1, (legs_num2_buf + 1) do
										legs_data2_buf[n][11] = 0
										legs_data2_buf[n][10] = 0
										legs_data2_buf[n][14] = 50000
										legs_data2_buf[n][38] = ""
									end
								end
								copy_from_legs_data2_buf(0)
								
								local disable_disco = false
								if in_flight_mode == 0 then
									disable_disco = true
								elseif offset_mod2 > 0 and offset_mod2 <= legs_num8 then
									if legs_data8[offset_mod2][1] == "DISCONTINUITY" then
										disable_disco = true
									end
								end
								if disable_disco == false then
									--create discontinuity
									rte_copy(offset_mod2, rte12_sel)
									rte_add_disco(offset_mod2, rte12_sel)
									rte_paste(offset_mod2 + 1, rte12_sel)
								end
							end
						else
							if B738DR_capt_map_mode == 3 then
								legs_step = legs_step + 1
								if legs_step > legs_num8 or legs_step < offset_step then
									legs_step = offset_step
								end
								if legs_step > legs_num8 then
									legs_step = legs_num8
								end
								if legs_data8[legs_step][1] == "DISCONTINUITY" then
									legs_step = legs_step + 1
									if legs_step > legs_num8 or legs_step < offset_step then
										legs_step = offset_step
									end
									if legs_step > legs_num8 then
										legs_step = legs_num8
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step - offset_mod2) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step = math.floor((legs_step - offset_mod2 + rte2_corr) / 5) + 1
								act_page = page_legs_step
								
							else
								-- RTA DATA
								page_rte_legs = 1
								page_legs = 0
							end
						end
					end
				end
			end
		elseif page_hold == 1 then
			if rte12_sel == 0 then
				if hold_offset_idx == offset and B738DR_hold_term == 0 and rte12_act == rte12_sel then
					hold_exec = 1
					B738DR_hold_term = 1
				end
			else
				if hold_offset_idx7 == offset and B738DR_hold_term == 0 and rte12_act == rte12_sel then
					hold_exec2 = 1
					B738DR_hold_term = 1
				end
			end
		elseif page_takeoff == 1 and B738DR_flight_phase == 0 then
			-- set QRH on / off
			if qrh == " ON" then
				qrh = "OFF"
			else
				qrh = " ON"
			end
			display_update = 1
		elseif page_n1_limit == 1 and disable_N1_6R == 0 then
			if in_flight_mode == 0 then
				-- go to Takeoff page
				page_n1_limit = 0
				page_takeoff = 1
				display_update = 1
			else
				-- select / deselect CLB-2
				if entry == ">DELETE" then
					clb = "<SEL>"
					clb_1 = "     "
					clb_2 = "     "
					entry = ""
				-- elseif clb_2 == "<SEL>" then
					-- clb = "<SEL>"
					-- clb_1 = "     "
					-- clb_2 = "     "
				else
					clb = "     "
					clb_1 = "     "
					clb_2 = "<SEL>"
				end
				sel_clb_thr = 1
				flight_phase_old2 = -1
			end
		elseif page_descent_forecast == 1 then
			if forecast_exec == 1 then
				-- ERASE
				trans_lvl_mod = trans_lvl
				forec_alt_1_mod = forec_alt_1
				forec_alt_1_num_mod = forec_alt_1_num
				forec_dir_1_mod = forec_dir_1
				forec_spd_1_mod = forec_spd_1
				forec_alt_2_mod = forec_alt_2
				forec_alt_2_num_mod = forec_alt_2_num
				forec_dir_2_mod = forec_dir_2
				forec_spd_2_mod = forec_spd_2
				forec_alt_3_mod = forec_alt_3
				forec_alt_3_num_mod = forec_alt_3_num
				forec_dir_3_mod = forec_dir_3
				forec_spd_3_mod = forec_spd_3
				cabin_rate_mod = cabin_rate
				forec_isa_dev_mod = forec_isa_dev
				forec_qnh_mod = forec_qnh
				tai_on_alt_mod = tai_on_alt
				tai_off_alt_mod = tai_off_alt
				tai_on_alt_num_mod = tai_on_alt_num
				tai_off_alt_num_mod = tai_off_alt_num
				forecast_exec = 0
				--page_descent_forecast = 0
				--page_descent = 1
			end
		elseif page_perf == 1 then
			-- go to N1 limit page
			page_perf = 0
			page_n1_limit = 1
			display_update = 1
		elseif page_perf == 2 then
			-- go to RTA progress page
			-- page_perf = 0
			-- page_rta = 1
		elseif page_pos_init == 1 then
			-- go to Route page
			page_pos_init = 0
			page_rte_init = 1
			if rte12_act == 0 then
				if entry == "" and ref_icao2 ~= "----" and ref_icao_x == "----" then
					entry = ref_icao2
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			elseif rte12_act == 1 then
				if entry == "" and ref_icao8 ~= "----" and ref_icao7_x == "----" then
					entry = ref_icao8
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			elseif rte12_sel == 0 then
				if entry == "" and ref_icao2 ~= "----" and ref_icao_x == "----" then
					entry = ref_icao2
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			else
				if entry == "" and ref_icao8 ~= "----" and ref_icao7_x == "----" then
					entry = ref_icao8
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			end
		elseif page_ident == 1 or page_ident == 2 then
			-- go to Pos init page
			page_ident = 0
			page_pos_init = 1
			display_update = 1
		elseif page_cruise == 1 then
			-- ERASE change cruise alt and spd
			if crz_exec ~= 0 then
				crz_alt = crz_alt_old
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
			end
			if crz_spd_exec ~= 0 then
				crz_spd_exec = 0
				fmc_cruise_speed_mod = 0
				fmc_cruise_speed_mach_mod = 0
				fmc_cruise_mode_mod = B738DR_cruise_mode
			end
		elseif page_descent == 1 then
			if descent_exec == 0 and des_spd_exec == 0 then
				local delta_alt_crz = B738DR_fmc_cruise_alt - simDR_ap_altitude_dial_ft
				if B738DR_fms_descent_now < 2 and delta_alt_crz > 1000 and des_now_enable == 1 then
					if B738DR_fms_descent_now == 0 then
						B738DR_fms_descent_now = 1
						--exec1_light = 1
					else
						B738DR_fms_descent_now = 0
						--exec1_light = 0
					end
				end
			else
				-- ERASE
				if descent_exec ~= 0 then
					if des_del_spd_idx2 ~= 0 then
						des_del_spd_idx2 = 0
					else
						descent_r_alt1_mod = B738DR_fmc_descent_r_alt1
						descent_r_speed1_mod = B738DR_fmc_descent_r_speed1
						fmc_descent_r_alt1_mod = fmc_descent_r_alt1
					end
					descent_exec = 0
				end
				if des_spd_exec ~= 0 then
					des_spd_exec = 0
					fmc_descent_speed_mod = 0
					fmc_descent_speed_mach_mod = 0
					fmc_descent_mode_mod = B738DR_descent_mode
				end
			end
			display_update = 1
		elseif page_climb == 1 then
			-- ERASE
			if climb_exec == 1 then
				if clb_del_spd_idx2 ~= 0 then
					clb_del_spd_idx2 = 0
				else
					climb_r_alt1_mod = B738DR_fmc_climb_r_alt1
					climb_r_speed1_mod = B738DR_fmc_climb_r_speed1
					fmc_climb_r_alt1_mod = fmc_climb_r_alt1
				end
				climb_exec = 0
			end
			if clb_spd_exec ~= 0 then
				clb_spd_exec = 0
				fmc_climb_speed_mod = 0
				fmc_climb_speed_mach_mod = 0
				fmc_climb_mode_mod = B738DR_climb_mode
			end
			if crz_exec ~= 0 then
				crz_alt = crz_alt_old
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
			end
			display_update = 1
		elseif page_rte_legs > 0 then
			-- go to LEGS
			page_rte_legs = 0
			page_legs = 1
		elseif page_ref_nav_data == 1 then
			local strlen = string.len(entry)
			if ref_nav_wpt == "-----" and ref_nav_navaid == "----" and ref_nav_apt == "----" then
				if ref_nav_new == "*****" then
					if strlen == 0 or strlen > 5 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						ref_nav_new = entry
						nav_data_idx2 = 0
						entry = ""
					end
				else
					-- go to NAV OPTIONS
					page_ref_nav_data = 0
					page_ref_nav_data_wpt = 1
					ref_nav_lat = 0
					ref_nav_lon = 0
					ref_nav_mag_var = ""
					ref_nav_id = "-----"
					ref_nav_rc = ""
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					entry = ""
				end
			else
				-- go to NAV OPTIONS
				page_ref_nav_data = 0
				if ref_nav_wpt ~= "-----" then
					page_ref_nav_data_wpt = 1
				elseif ref_nav_navaid ~= "----" then
					page_ref_nav_data_navaid = 1
				elseif ref_nav_apt ~= "----" then
					page_ref_nav_data_apt = 1
				end
				ref_nav_lat = navaid_list[nav_data_idx][2]
				ref_nav_lon = navaid_list[nav_data_idx][3]
				if navaid_list[nav_data_idx][20] == ">" then
					ref_nav_mag_var = string.format("%3d",(mag_variation_deg(navaid_list[nav_data_idx][2], navaid_list[nav_data_idx][3])))
				else
					if navaid_list[nav_data_idx][20] == "x" then
						ref_nav_mag_var = ">" .. string.format("%3d",(mag_variation_deg(navaid_list[nav_data_idx][2], navaid_list[nav_data_idx][3])))
					else
						ref_nav_mag_var = navaid_list[nav_data_idx][20]
					end
				end
				if navaid_list[nav_data_idx][21] == "" then
					ref_nav_id = "-----"
				else
					ref_nav_id = navaid_list[nav_data_idx][21]
				end
				if navaid_list[nav_data_idx][22] == "" then
					ref_nav_brg = "---"
				else
					ref_nav_brg = navaid_list[nav_data_idx][22]
				end
				if navaid_list[nav_data_idx][23] == "" then
					ref_nav_dist = "---"
				else
					ref_nav_dist = navaid_list[nav_data_idx][23]
				end
				nav_data_idx2 = navaid_list[nav_data_idx][24]
				
				ref_nav_freq = navaid_list[nav_data_idx][6]
				local ref_len = string.len(ref_nav_freq)
				if navaid_list[nav_data_idx][1] > 10 then
					ref_nav_freq = navaid_list[nav_data_idx][6] .. " "
				elseif ref_len == 3 then
					ref_nav_freq = navaid_list[nav_data_idx][6] .. "   "
				elseif ref_len == 4 then
					ref_nav_freq = string.sub(navaid_list[nav_data_idx][6], 1, 3) .. "." .. string.sub(navaid_list[nav_data_idx][6], -1, -1) .. " "
				elseif ref_len == 5 then
					ref_nav_freq = string.sub(navaid_list[nav_data_idx][6], 1, 3) .. "." .. string.sub(navaid_list[nav_data_idx][6], -2, -1)
				else
					ref_nav_freq = "      "
				end
				
				ref_elevation = 0
				
				ref_nav_rnw = "-----"
				ref_nav_rnw_length = 0
				
				entry = ""
			end
			ref_nav_exec = 0
		elseif page_ac_atis == 1 then
			if atis_msg_status ~= 0 and acars_atis_status == 0 and receive_msg == 2 then
				page_ac_atis = 0
				page_ac_msg = 1
				act_page = 1
			end
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			if B738DR_thr_cutback == 0 then
				B738DR_thr_cutback = 1
				clb_alt = "----"
				clb_alt_num = B738DR_def_clb_alt2
			else
				B738DR_thr_cutback = 0
				clb_alt = "----"
				clb_alt_num = B738DR_def_clb_alt
			end
			accel_alt = "----"
			accel_alt_num = B738DR_def_accel_alt
		elseif page_altern_apt == 1 then
			create_near_apt_list()
			page_nearest_apt = 1
			page_altern_apt = 0
			act_page = 1
		elseif page_nearest_apt == 1 then
			if act_page == 1 then
				page_nearest_apt = 0
				page_altern_apt = 1
			else
				act_page = 1
			end
		elseif page_approach == 1 then
			page_altern_apt = 1
			page_approach = 0
			act_page = 1
		elseif page_progress == 1 then
			page_nav_status = 1
			page_progress = 0
			act_page = 1
		elseif page_init == 1 then
			page_nav_status = 1
			page_init = 0
			act_page = 1
		elseif page_dl_requests == 1 and atis_msg_status ~= 0 and receive_msg == 2 then
			page_dl_requests = 0
			page_dl_wxr_info = 1
			act_page = 1
			display_update = 1
		end
		
		--end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-------------------------------------------------------------------------


-- FMC NUMBER BUTTON
function B738_fmc1_0_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "0")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_1_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "1")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_2_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "2")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_3_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "3")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_4_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "4")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_5_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "5")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_6_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "6")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_7_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "7")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_8_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "8")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_9_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "9")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_period_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. ".")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_minus_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				if string.sub(entry, -1, -1) == "-" then
					if string.len(entry) == 1 then
						entry = "+"
					else
						entry = string.sub(entry, 1, -2) .. "+"
					end
				elseif string.sub(entry, -1, -1) == "+" then
					if string.len(entry) == 1 then
						entry = "-"
					else
						entry = string.sub(entry, 1, -2) .. "-"
					end
				else
					entry = (entry .. "-")
				end
				display_update = 1
			elseif string.len(entry) == 24 then
				if string.sub(entry, -1, -1) == "-" then
					entry = string.sub(entry, 1, -2) .. "+"
				elseif string.sub(entry, -1, -1) == "+" then
					entry = string.sub(entry, 1, -2) .. "-"
				end
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_slash_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "/")
				display_update = 1
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- FMC BUTTON CLR (delete last)
function B738_fmc1_clr_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
		local lenstr = string.len(entry)
		if fmc_message_num ~= 0 then
			if string.sub(fmc_message[fmc_message_num], 1, 8) == HOLD_AT and rte12_sel == 0 then
				new_hold_at = 0
			elseif string.sub(fmc_message[fmc_message_num], 1, 8) == HOLD_AT and rte12_sel == 1 then
				new_hold_at2 = 0
			end
			fmc_message_num = fmc_message_num - 1
			display_update = 1
		else
			if lenstr == 1 then
				entry = ""
				display_update = 1
			elseif lenstr > 1 then
				if string.sub(entry, 1, 1) == ">" then
					entry = ""
				elseif fmc1_msg_alt == 0 then
					if item_sel_act > 0 then
						if lenstr <= item_sel_len then
							item_sel = 0
							item_sel_act = 0
						elseif lenstr == item_sel_len + 1 then
							if item_sel_act ~= 0 then
								item_sel = item_sel_act
							else
								item_sel = 0
							end
						end
					end
					entry = string.sub(entry, 1, -2)
				end
				display_update = 1
			end
		end
		
		item_sel_via = 0
		
		fmc1_msg_alt = 0
		
		fmc1_input_lag = 0
	elseif phase == 1 then
		if duration > 1 then
			entry = ""
			fmc_message_num = 0
		end
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- FMC BUTTON DEL (delete all)
function B738_fmc1_del_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if string.len(entry) > 1 then
				if string.sub(entry, 1, 1) == ">" then
					entry = ""
				else
					entry = ">DELETE"
				end
		else
			entry = ">DELETE"
		end
		item_sel = 0
		item_sel_via = 0
		item_sel_act = 0
		display_update = 1
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- FMC BUTTON PREVIOUS and NEXT PAGE
function B738_fmc1_prev_page_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
			-- local act_page_bck = act_page
			local rte2_corr = 0
			act_page = act_page - 1
			if act_page < 1 then
				act_page = max_page
			end
			
			if B738DR_fmc_type ~= 0 and (page_climb ~= 0 or page_cruise ~= 0 or page_descent ~= 0) then
				if page_climb ~= 0 then
					page_climb = 0
					page_descent = 1
				elseif page_cruise ~= 0 then
					page_cruise = 0
					page_climb = 1
				else
					page_descent = 0
					page_cruise = 1
				end
			else
				if page_init ~= 0 then
					page_init = act_page
				elseif page_ident ~= 0 then
					page_ident = act_page
				elseif page_takeoff ~= 0 then
					page_takeoff = act_page
				elseif page_approach ~= 0 then
					page_approach = act_page
				elseif page_perf ~= 0 then
					page_perf = act_page
				elseif page_n1_limit~= 0 then
					page_n1_limit = act_page
				elseif page_pos_init ~= 0 then
					page_pos_init = act_page
				elseif page_progress ~= 0 then
					page_progress = act_page
				elseif page_legs == 1 then
					if B738DR_capt_map_mode == 3 then
						if B738DR_no_rte2 == 0 then
							rte2_corr = 1
						end
						if rte12_sel == 0 then
							legs_step = offset_mod + (5 * (act_page - 1)) - rte2_corr
							if legs_data2[legs_step][1] == "DISCONTINUITY" then
								legs_step = legs_step + 1
								if legs_step > legs_num2 or legs_step < offset_mod then
									legs_step = offset_mod
								end
								if legs_step > legs_num2 then
									legs_step = legs_num2
								end
							end
						else
							legs_step = offset_mod2 + (5 * (act_page - 1)) - rte2_corr
							if legs_data8[legs_step][1] == "DISCONTINUITY" then
								legs_step = legs_step + 1
								if legs_step > legs_num8 or legs_step < offset_mod2 then
									legs_step = offset_mod2
								end
								if legs_step > legs_num8 then
									legs_step = legs_num8
								end
							end
						end
					end
				elseif page_offset ~= 0 then
					page_offset = act_page
				elseif page_hold ~= 0 then
					if rte12_sel == 0 then
						hold_offset = 0
						if hold_data_num2 > 0 then
							for ww = 1, hold_data_num2 do
								if hold_data2[ww] >= offset_mod then
									hold_offset = ww
									break
								end
							end
						end
						new_hold_act_page = 0
						max_page = hold_data_num2 - hold_offset + 1
					else
						hold_offset2 = 0
						if hold_data_num4 > 0 then
							for ww = 1, hold_data_num4 do
								if hold_data4[ww] >= offset_mod2 then
									hold_offset2 = ww
									break
								end
							end
						end
						new_hold_act_page2 = 0
						max_page = hold_data_num4 - hold_offset2 + 1
					end
					if act_page > max_page then
						act_page = max_page
					end
					
				elseif page_ac ~= 0 then
					page_ac = act_page
				elseif page_ac_atis ~= 0 then
					page_ac_atis = act_page
				elseif page_atc ~= 0 then
					page_atc = act_page
				end
			end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end



function B738_fmc1_next_page_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
			-- local act_page_bck = act_page
			local rte2_corr = 0
			act_page = act_page + 1
			if act_page > max_page then
				act_page = 1
			end
			
			if B738DR_fmc_type ~= 0 and (page_climb ~= 0 or page_cruise ~= 0 or page_descent ~= 0) then
				page_eo_climb = 0
				page_eo_cruise = 0
				if page_climb ~= 0 then
					page_climb = 0
					page_cruise = 1
					if engout_crz < 2 then
						engout_crz = 0
					end
				elseif page_cruise ~= 0 then
					page_cruise = 0
					page_descent = 1
				else
					page_descent = 0
					page_climb = 1
					if engout_clb < 2 then
						engout_clb = 0
					end
				end
			else
				if page_init ~= 0 then
					page_init = act_page
				elseif page_ident ~= 0 then
					page_ident = act_page
				elseif page_takeoff ~= 0 then
					page_takeoff = act_page
				elseif page_approach ~= 0 then
					page_approach = act_page
				elseif page_perf ~= 0 then
					page_perf = act_page
				elseif page_n1_limit ~= 0 then
					page_n1_limit = act_page
				elseif page_pos_init ~= 0 then
					page_pos_init = act_page
				elseif page_progress ~= 0 then
					page_progress = act_page
				elseif page_legs == 1 then
					if B738DR_capt_map_mode == 3 then
						if B738DR_no_rte2 == 0 then
							rte2_corr = 1
						end
						if rte12_sel == 0 then
							legs_step = offset_mod + (5 * (act_page - 1)) - rte2_corr
							if legs_data2[legs_step][1] == "DISCONTINUITY" then
								legs_step = legs_step + 1
								if legs_step > legs_num2 or legs_step < offset_mod then
									legs_step = offset_mod
								end
								if legs_step > legs_num2 then
									legs_step = legs_num2
								end
							end
						else
							legs_step = offset_mod2 + (5 * (act_page - 1)) - rte2_corr
							if legs_data8[legs_step][1] == "DISCONTINUITY" then
								legs_step = legs_step + 1
								if legs_step > legs_num8 or legs_step < offset_mod2 then
									legs_step = offset_mod2
								end
								if legs_step > legs_num8 then
									legs_step = legs_num8
								end
							end
						end
					end
				elseif page_offset ~= 0 then
					page_offset = act_page
				elseif page_hold ~= 0 then
					if rte12_sel == 0 then
						hold_offset = 0
						if hold_data_num2 > 0 then
							for ww = 1, hold_data_num2 do
								if hold_data2[ww] >= offset_mod then
									hold_offset = ww
									break
								end
							end
						end
						new_hold_act_page = 0
						max_page = hold_data_num2 - hold_offset + 1
					else
						hold_offset2 = 0
						if hold_data_num4 > 0 then
							for ww = 1, hold_data_num4 do
								if hold_data4[ww] >= offset_mod2 then
									hold_offset2 = ww
									break
								end
							end
						end
						new_hold_act_page2 = 0
						max_page = hold_data_num4 - hold_offset + 1
					end
					if act_page > max_page then
						act_page = max_page
					end
					
				elseif page_ac ~= 0 then
					page_ac = act_page
				elseif page_ac_atis ~= 0 then
					page_ac_atis = act_page
				elseif page_atc ~= 0 then
					page_atc = act_page
				end
			end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc1_init_ref_CMDhandler(phase, duration)
	
	local qqq = 0
	
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		local irs_align_ok = 0
		if B738DR_irs_left_mode > 1 or B738DR_irs_right_mode > 1 then
			irs_align_ok = 1
		end
		local perf_init_complete = 1
		if gw == "***.*" or cost_index == "***" or reserves == "**.*" or crz_alt == "*****" then
			perf_init_complete = 0
		end
		if perf_exec < 2 then
			perf_init_complete = 0
		end
		
		local ref_des_icao = 1
		if ref_icao == "----" or des_icao == "****" then
			ref_des_icao = 0
		end
		
		local n1_oat = 1
		if oat == "****" and B738DR_aspirated_probe == 0 then
			n1_oat = 0
		end
		
		reset_fmc_pages()
		if B738DR_init_ref_smart ~= 0 then
			if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				-- page_perf = 1
				if irs_align_ok == 0 then
					page_pos_init = 1
				else
					page_perf = 1
				end
			else
				-- in fligt -> Approach page
				if page_approach == 0 and page_approach2 == 0 then
					gw_app_nul = 0
					gw_app = gw
					gw_app_lbs = gw_lbs
					gw_app_kgs = gw_kgs
				end
				page_approach = 1
			end
		else
			-- if irs_align_ok == 0 then
				-- page_pos_init = 1
			-- elseif B738DR_flight_phase == 0 and (simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1) then
			-- elseif simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				-- -- if ref_des_icao == 0 then
					-- -- page_rte_init = 1
				-- -- elseif perf_init_complete == 0 then
					-- page_perf = 1
				-- -- elseif n1_oat == 0 then
					-- -- page_n1_limit = 1
				-- -- else
					-- -- page_takeoff = 1
				-- -- end
			-- else	--if was_on_air == 1 then
				-- -- in fligt -> Approach page
				-- if page_approach == 0 and page_approach2 == 0 then
					-- gw_app_nul = 0
					-- gw_app = gw
					-- gw_app_lbs = gw_lbs
					-- gw_app_kgs = gw_kgs
				-- end
				-- page_approach = 1
			if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				if irs_align_ok == 0 then
					page_pos_init = 1
				else
					page_perf = 1
				end
			else
				-- in fligt -> Approach page
				if page_approach == 0 and page_approach2 == 0 then
					gw_app_nul = 0
					gw_app = gw
					gw_app_lbs = gw_lbs
					gw_app_kgs = gw_kgs
				end
				page_approach = 1
			end
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc1_menu_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages()
		if B738DR_fmc_type == 0 then
			page_menu = 1
		else
			-- FIX
			page_fix = 1
			act_page = 1
		end
		menu_timer = 0
	elseif phase == 1 then
		if B738DR_fmc_type == 0 then
			if duration > 3 and menu_timer == 0 then
				B738_init2()
				B738_init3()
				B738_init4()
				B738_init_vspd()
				entry = ">FMC RESET OK"
				B738DR_fmc_message_warn = 1
				fms_msg_sound = 1
				B738DR_lnav_disconnect = 1
				B738DR_vnav_disconnect = 1
				menu_timer = 1
			end
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
		menu_timer = 0
	end
end
function B738_fmc1_n1_lim_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages()
		if B738DR_fmc_type == 0 then
			page_n1_limit = 1
		else
			-- MENU
			page_menu = 1
		end
		menu_timer = 0
		fmc1_input_lag = 0
	elseif phase == 1 then
		if B738DR_fmc_type ~= 0 then
			-- HOLD
			if duration > 3 and menu_timer == 0 then
				B738_init2()
				B738_init3()
				B738_init4()
				B738_init_vspd()
				entry = ">FMC RESET OK"
				B738DR_fmc_message_warn = 1
				fms_msg_sound = 1
				B738DR_lnav_disconnect = 1
				B738DR_vnav_disconnect = 1
				menu_timer = 1
			end
		end
	elseif phase == 2 then
		B738DR_fms_key = 0
		menu_timer = 0
	end
end
function B738_fmc1_rte_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		reset_fmc_pages()
		page_rte_init = 1
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			act_page = 1
		else
			act_page = 2
			local max_page_fpln = 0
			local jj = 0
			local kk = 0
			-- Flight plan
			if ref_icao_x == "----" or des_icao_x == "****" then
				max_page_fpln = 0
			else
				jj = math.floor(fpln_num2 / 5)
				kk = fpln_num2 % 5
				max_page_fpln = jj + 2
			end
			if act_page > max_page_fpln then
				act_page = max_page_fpln
				if act_page == 0 then
					act_page = 1
				end
			end
		end
		
		if entry == "" and ref_icao2 ~= "----" and ref_icao_x == "----" then
			entry = ref_icao2
			add_fmc_msg(REF_ICAO_MSG, 1)
			msg_ref_icao_alert = 1
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc1_legs_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
			
			reset_fmc_pages()
			page_legs = 1
			if rte12_sel == 0 then
				new_hold = 0
			else
				new_hold2 = 0
			end
			
			if B738DR_capt_map_mode == 3 then
				page_legs_step = 1
				act_page = page_legs_step
				if rte12_sel == 0 then
					legs_step = offset_mod
					if legs_data2[legs_step][1] == "DISCONTINUITY" then
						legs_step = legs_step + 1
						if legs_step > legs_num2 or legs_step < offset_mod then
							legs_step = offset_mod
						end
						if legs_step > legs_num2 then
							legs_step = legs_num2
						end
					end
				else
					legs_step = offset_mod2
					if legs_data8[legs_step][1] == "DISCONTINUITY" then
						legs_step = legs_step + 1
						if legs_step > legs_num8 or legs_step < offset_mod2 then
							legs_step = offset_mod2
						end
						if legs_step > legs_num8 then
							legs_step = legs_num8
						end
					end
				end
			else
				act_page = 1
			end
		
		--end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_fix_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		reset_fmc_pages()
		if B738DR_fmc_type == 0 then
			page_fix = 1
			act_page = 1
		else
			-- N1 Limit
			page_n1_limit = 1
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_clb_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages()
		if B738DR_fmc_type == 0 then
			page_climb = 1
			if engout_clb < 2 then
				engout_clb = 0
			end
		else
			-- DEP ARR
			page_dep_arr = 1
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_crz_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages()
		if B738DR_fmc_type == 0 then
			page_cruise = 1
			if engout_crz < 2 then
				engout_crz = 0
			end
		else
			page_atc = 1
			act_page = 1
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_des_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages()
		if B738DR_fmc_type == 0 then
			page_descent = 1
		else
			if B738DR_flight_phase < 2 then
				-- CLB
				page_climb = 1
			elseif B738DR_flight_phase < 5 then
				-- CRZ
				page_cruise = 1
			elseif B738DR_flight_phase ~= 8 then
				-- DES
				page_descent = 1
			else
				-- CLB
				page_climb = 1
			end
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_dep_app_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		if B738DR_fmc_type == 0 then
			reset_fmc_pages()
			page_dep_arr = 1
		else
			-- HOLD
			if B738DR_fmc_exec_lights == 0 then
			
				reset_fmc_pages()
				
				if rte12_sel == 0 then
					hold_offset = 0
					if hold_data_num2 > 0 then
						for ww = 1, hold_data_num2 do
							if hold_data2[ww] >= offset_mod then
								hold_offset = ww
								break
							end
						end
					end
					
					if hold_offset == 0 then
						page_hold = 0
						page_legs = 1
						act_page = 1
						new_hold = 1
						new_hold_inbd = "---"
						new_hold_turn = -1
						new_hold_time = "-.-"
						new_hold_dist = "--.-"
					else
						page_hold = 1
						page_legs = 0
						act_page = 1
					end
				else
					hold_offset2 = 0
					if hold_data_num4 > 0 then
						for ww = 1, hold_data_num4 do
							if hold_data4[ww] >= offset_mod2 then
								hold_offset2 = ww
								break
							end
						end
					end
					
					if hold_offset2 == 0 then
						page_hold = 0
						page_legs = 1
						act_page = 1
						new_hold2 = 1
						new_hold_inbd2 = "---"
						new_hold_turn2 = -1
						new_hold_time2 = "-.-"
						new_hold_dist2 = "--.-"
					else
						page_hold = 1
						page_legs = 0
						act_page = 1
					end
				end
			end
			fmc1_input_lag = 0
			
		end
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_hold_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		local ww = 0
		
		B738DR_fms_key = 1
		if B738DR_fmc_type == 0 then
			if B738DR_fmc_exec_lights == 0 then
			
				reset_fmc_pages()
				
				if rte12_sel == 0 then
					hold_offset = 0
					if hold_data_num2 > 0 then
						for ww = 1, hold_data_num2 do
							if hold_data2[ww] >= offset_mod then
								hold_offset = ww
								break
							end
						end
					end
					
					if hold_offset == 0 then
						page_hold = 0
						page_legs = 1
						act_page = 1
						new_hold = 1
						new_hold_inbd = "---"
						new_hold_turn = -1
						new_hold_time = "-.-"
						new_hold_dist = "--.-"
					else
						page_hold = 1
						page_legs = 0
						act_page = 1
					end
				else
					hold_offset2 = 0
					if hold_data_num4 > 0 then
						for ww = 1, hold_data_num4 do
							if hold_data4[ww] >= offset_mod2 then
								hold_offset2 = ww
								break
							end
						end
					end
					
					if hold_offset2 == 0 then
						page_hold = 0
						page_legs = 1
						act_page = 1
						new_hold2 = 1
						new_hold_inbd2 = "---"
						new_hold_turn2 = -1
						new_hold_time2 = "-.-"
						new_hold_dist2 = "--.-"
					else
						page_hold = 1
						page_legs = 0
						act_page = 1
					end
				end
			end
		else
			reset_fmc_pages()
			page_fmc_comm = 1
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_prog_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		
			reset_fmc_pages()
			page_progress = 1
		
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- EXECSK
function B738_fmc1_exec_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 and calc_rte_enable2 == 0 and calc_rte_enable8 == 0 then
		B738DR_fms_key = 1
		local qq = 0
		local xy = 0
		local apt_ok = 0
		local exec_enable = 0
		local rte12_changed = 0
		-- local lat_offset_changed = true
		
		if entry == "FLTREC DES" then
			if start_rec == 3 or start_rec == 4 then
				file_rec_path:close()
			end
			start_rec = 1
		elseif entry == "FLTREC CLB" then
			if start_rec == 3 or start_rec == 4 then
				file_rec_path:close()
			end
			start_rec = 2
		elseif entry == "FLTREC" then
			if start_rec == 5 then
				start_rec = 0
			else
				start_rec = 6
			end
		end
		
		if B738DR_fmc_exec_lights == 1 then
			update_rnw_list(rte12_sel)
			
			if page_descent_forecast == 1 then
				if forecast_exec == 1 then
					forecast_exec = 0
					trans_lvl = trans_lvl_mod
					forec_alt_1 = forec_alt_1_mod
					forec_alt_1_num = forec_alt_1_num_mod
					forec_dir_1 = forec_dir_1_mod
					forec_spd_1 = forec_spd_1_mod
					forec_alt_2 = forec_alt_2_mod
					forec_alt_2_num = forec_alt_2_num_mod
					forec_dir_2 = forec_dir_2_mod
					forec_spd_2 = forec_spd_2_mod
					forec_alt_3 = forec_alt_3_mod
					forec_alt_3_num = forec_alt_3_num_mod
					forec_dir_3 = forec_dir_3_mod
					forec_spd_3 = forec_spd_3_mod
					cabin_rate = cabin_rate_mod
					forec_isa_dev = forec_isa_dev_mod
					forec_qnh = forec_qnh_mod
					tai_on_alt = tai_on_alt_mod
					tai_off_alt = tai_off_alt_mod
					tai_on_alt_num = tai_on_alt_num_mod
					tai_off_alt_num = tai_off_alt_num_mod
					if cabin_rate == "---" then
						simDR_press_set_vvi = 500
					else
						simDR_press_set_vvi = cabin_rate
					end
				end
			end
			
			-- if page_cruise == 1 or page_climb == 1 or page_perf == 1 then
			if page_perf == 1 or page_legs > 0 or page_dep > 0 or page_arr > 0 or page_rte_init > 0 or page_rte_legs > 0 
			or page_climb > 0 or page_cruise > 0 or page_descent > 0 then
			
				if B738DR_flight_phase >= 2 and B738DR_flight_phase < 8 then
					if crz_exec ~= 0 then
						
						local altitude_actual_1 = simDR_altitude_pilot + 200
						local altitude_actual_2 = simDR_altitude_pilot - 200
						if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
							altitude_actual_1 = B738DR_preselected_alt
							altitude_actual_2 = altitude_actual_1
						end
						
						if crz_alt_num2 > altitude_actual_1 then
							crz_exec = 1	-- CRZ CLB
							if B738DR_mcp_alt_dial > crz_alt_num then
								B738DR_flight_phase = 3
							end
						elseif crz_alt_num2 < altitude_actual_2 then
							crz_exec = 2	-- CRZ DES
							if B738DR_mcp_alt_dial < crz_alt_num then
								B738DR_flight_phase = 4
							end
						else	-- crz_exec == 9
							crz_exec = 3	-- CRZ
						end
						altitude_last = elevation_ft
						B738DR_fms_descent_now = 0
						vnav_update = 1
						crz_alt_changed = 1
						cruise_alt_changed = 1
						cruise_alt_changed2 = 1
						if crz_alt_num2 > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
							add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
						end
					end
					
					-- CRZ CLB
					if crz_exec == 1 then
						
						-- engage CRZ CLB
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						simDR_ap_altitude_dial_ft = crz_alt_num
						if B738DR_mcp_alt_dial >= crz_alt_num then
							if B738DR_altitude_mode == 5 then
								if simDR_autopilot_altitude_mode ~= 5 then
									simCMD_autopilot_lvl_chg:once()
								end
							end
							B738DR_flight_phase = 3
							B738DR_fms_descent_now = 0
							vnav_alt_mode = 0
						else
							
							add_fmc_msg(CHECK_ALT_TGT, 1)
							B738DR_fmc_message_warn = 1
							B738DR_flight_phase = 1
							missed_app_alt_change = 0
							-- if B738DR_vnav_alt_disable ~= 0 or B738DR_vnav_alt_disable ~= 0 then
							if B738DR_vnav_alt_disable ~= 0 then
								-- ALT HLD
								B738DR_activate_pitch_mode = 3
							else
								-- VNAV ALT
								vnav_alt_mode = 1
							end
						end
					
					-- CRZ DES
					elseif crz_exec == 2 then
						
						-- engage CRZ DES
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						simDR_ap_altitude_dial_ft = crz_alt_num
						if B738DR_mcp_alt_dial <= crz_alt_num then
							if B738DR_altitude_mode == 5 then
								if simDR_autopilot_altitude_mode ~= 4 then
									simDR_ap_vvi_dial = -1000
									B738DR_ap_vvi_dial = -1000
									simCMD_autopilot_vs_sel:once()
								end
							end
							B738DR_flight_phase = 4
							B738DR_fms_descent_now = 0
							vnav_alt_mode = 0
						else
							add_fmc_msg(CHECK_ALT_TGT, 1)
							vnav_alt_mode = 1
						end
					elseif crz_exec == 3 then
						-- engage CRZ
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						simDR_ap_altitude_dial_ft = crz_alt_num
						B738DR_flight_phase = 2
						B738DR_fms_descent_now = 0
						vnav_alt_mode = 0
						if B738DR_altitude_mode == 5 then
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								simCMD_autopilot_alt_hold:once()
							end
						end
					end
					if page_perf == 1 and crz_exec ~= 0 then
						B738_calc_vnav_spd()
					end
				else
					-- climb mode
					if crz_exec ~= 0 then
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						msg_unavaible_crz_alt = 0
						msg_chk_alt_constr = 0
						vnav_update = 1
						if page_perf == 1 then
							B738_calc_vnav_spd()
						end
						crz_alt_changed = 1
						altitude_last = elevation_ft
						B738DR_fms_descent_now = 0
						
						if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
							add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
						end
						
						local altitude_actual_1 = simDR_altitude_pilot + 200
						local altitude_actual_2 = simDR_altitude_pilot - 200
						if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
							altitude_actual_1 = B738DR_preselected_alt
							altitude_actual_2 = altitude_actual_1
						end
						
						if crz_alt_num > altitude_actual_1 then
							-- if B738DR_mcp_alt_dial < crz_alt_num then
							if B738DR_mcp_alt_dial < altitude_actual_1 then
								if (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
									add_fmc_msg(CHECK_ALT_TGT, 1)
									vnav_alt_mode = 1
								end
							end
						elseif crz_alt_num < altitude_actual_2 then
							B738DR_flight_phase = 2
							-- if B738DR_mcp_alt_dial > crz_alt_num then
							if B738DR_mcp_alt_dial > altitude_actual_2 then
								if (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
									add_fmc_msg(CHECK_ALT_TGT, 1)
									vnav_alt_mode = 1
								else
									-- engage CRZ DES
									crz_alt_num = crz_alt_num2
									B738DR_fmc_cruise_alt = crz_alt_num
									simDR_ap_altitude_dial_ft = crz_alt_num
									if B738DR_mcp_alt_dial <= crz_alt_num then
										if B738DR_altitude_mode == 5 then
											if simDR_autopilot_altitude_mode ~= 4 then
												simDR_ap_vvi_dial = -1000
												B738DR_ap_vvi_dial = -1000
												simCMD_autopilot_vs_sel:once()
											end
										end
										B738DR_flight_phase = 4
										B738DR_fms_descent_now = 0
										vnav_alt_mode = 0
									else
										add_fmc_msg(CHECK_ALT_TGT, 1)
										vnav_alt_mode = 1
									end
								end
							end
						else	-- crz_exec == 9
							B738DR_flight_phase = 2
						end
					end
					
				end
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
				
				if crz_spd_exec == 1 then
					if B738DR_flight_phase == 3 then
						--CRZ CLB
						if fmc_climb_speed_mach_mod ~= 0 then
							B738DR_fmc_climb_speed_mach = fmc_climb_speed_mach_mod
						end
						if fmc_climb_speed_mod ~= 0 then
							B738DR_fmc_climb_speed = fmc_climb_speed_mod
						end
						fmc_climb_speed_mod = 0
						fmc_climb_speed_mach_mod = 0
						crz_spd_exec = 0
					elseif B738DR_flight_phase == 4 then
						--CRZ DES
						if fmc_descent_speed_mach_mod ~= 0 then
							B738DR_fmc_descent_speed_mach = fmc_descent_speed_mach_mod
						end
						if fmc_descent_speed_mod ~= 0 then
							B738DR_fmc_descent_speed = fmc_descent_speed_mod
						end
						fmc_descent_speed_mod = 0
						fmc_descent_speed_mach_mod = 0
						crz_spd_exec = 0
					else
						if B738DR_cruise_mode ~= fmc_cruise_mode_mod and fmc_cruise_mode_mod == 0 then
							B738DR_descent_mode = 0
							B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
							B738DR_fmc_descent_speed = econ_des_spd				-- temporary
							change_desc_to_path = 1
							fmc_descent_mode_mod = 0
						end
						
						B738DR_cruise_mode = fmc_cruise_mode_mod
						B738DR_fmc_cruise_speed = fmc_cruise_speed_mod
						B738DR_fmc_cruise_speed_mach = fmc_cruise_speed_mach_mod
						
						if B738DR_fmc_descent_speed > B738DR_fmc_cruise_speed and B738DR_fmc_cruise_speed ~= 0 then
							B738DR_fmc_descent_speed = B738DR_fmc_cruise_speed
						elseif B738DR_fmc_descent_speed < B738DR_fmc_cruise_speed - 20 then
							B738DR_fmc_descent_speed = B738DR_fmc_cruise_speed - 20
						end
						fmc_descent_speed_mod = 0
						
						if B738DR_fmc_descent_speed_mach > B738DR_fmc_cruise_speed_mach and B738DR_fmc_cruise_speed_mach ~= 0 then
							B738DR_fmc_descent_speed_mach = B738DR_fmc_cruise_speed_mach
						elseif B738DR_fmc_descent_speed_mach < B738DR_fmc_cruise_speed_mach - 0.04 then
							B738DR_fmc_descent_speed_mach = B738DR_fmc_cruise_speed_mach - 0.04
						end
						fmc_descent_speed_mach_mod = 0
						
						fmc_cruise_speed_mod = 0
						fmc_cruise_speed_mach_mod = 0
						crz_spd_exec = 0
						
						B738DR_fmc_descent_r_alt1 = descent_r_alt1_mod
						B738DR_fmc_descent_r_speed1 = descent_r_speed1_mod
						fmc_descent_r_alt1 = fmc_descent_r_alt1_mod
						
						B738DR_fmc_climb_r_alt1 = climb_r_alt1_mod
						B738DR_fmc_climb_r_speed1 = climb_r_speed1_mod
						fmc_climb_r_alt1 = fmc_climb_r_alt1_mod
					end
				end
				
			-- end
			
				if descent_exec == 1 then
					if des_del_spd_idx2 ~= 0 then
						if des_del_spd_idx2 < legs_num then
							legs_data[des_del_spd_idx2][4] = 0
							legs_data[des_del_spd_idx2][42] = 0
						end
						des_del_spd_idx2 = 0
					else
						B738DR_fmc_descent_r_alt1 = descent_r_alt1_mod
						B738DR_fmc_descent_r_speed1 = descent_r_speed1_mod
						fmc_descent_r_alt1 = fmc_descent_r_alt1_mod
					end
					descent_exec = 0
					vnav_update = 1
					msg_chk_alt_constr = 0
				end
				
				if B738DR_fms_descent_now == 1 then
					B738DR_fms_descent_now = 2
					if B738DR_altitude_mode == 5 then
						if simDR_autopilot_altitude_mode ~= 4 then
							simDR_ap_vvi_dial = 0	---1000
							B738DR_ap_vvi_dial = 0
							simCMD_autopilot_vs_sel:once()
						end
					end
					B738DR_flight_phase = 5
					switch_fmc_page(3)
				end
				
				if des_spd_exec == 1 then
					if fmc_descent_speed_mach_mod ~= 0 then
						B738DR_fmc_descent_speed_mach = fmc_descent_speed_mach_mod
					end
					if fmc_descent_speed_mod ~= 0 then
						B738DR_fmc_descent_speed = fmc_descent_speed_mod
					end
					B738DR_descent_mode = fmc_descent_mode_mod
					if B738DR_descent_mode == 0 then
						B738DR_descent_mode = 2
						change_desc_to_path = 1
					elseif B738DR_descent_mode == 1 then
						B738DR_descent_mode = 3
						change_desc_to_path = 0
					end
					fmc_descent_speed_mod = 0
					fmc_descent_speed_mach_mod = 0
					des_spd_exec = 0
				end
			
				if climb_exec == 1 then
					if clb_del_spd_idx2 ~= 0 then
						if clb_del_spd_idx2 < legs_num then
							legs_data[clb_del_spd_idx2][4] = 0
							legs_data[clb_del_spd_idx2][42] = 0
						end
						clb_del_spd_idx2 = 0
					else
						B738DR_fmc_climb_r_alt1 = climb_r_alt1_mod
						B738DR_fmc_climb_r_speed1 = climb_r_speed1_mod
						fmc_climb_r_alt1 = fmc_climb_r_alt1_mod
					end
					climb_exec = 0
				end
				
				if clb_spd_exec == 1 then
					if fmc_climb_speed_mach_mod ~= 0 then
						B738DR_fmc_climb_speed_mach = fmc_climb_speed_mach_mod
						-- B738DR_climb_mode = 3
						B738DR_climb_mode = fmc_climb_mode_mod
					end
					if fmc_climb_speed_mod ~= 0 then
						B738DR_fmc_climb_speed = fmc_climb_speed_mod
						-- B738DR_climb_mode = 3
						B738DR_climb_mode = fmc_climb_mode_mod
					end
					fmc_climb_speed_mod = 0
					fmc_climb_speed_mach_mod = 0
					clb_spd_exec = 0
				end
			end
			
			if rte_add_sid_act == 0 and rte_add_star_act == 0 and rte_add_app_act == 0 then		--and mod_offset == 0 then
				exec_enable = 1
			end
			
			if exec_enable == 1 then
				
				-- if page_legs > 0 or page_dep > 0 or page_arr > 0 or page_rte_init > 0 or page_rte_legs > 0 or lat_offset_changed == true then
				if page_legs > 0 or page_dep > 0 or page_arr > 0 or page_rte_init > 0 or page_rte_legs > 0 or (page_offset == 1 and offset_act ~= offset_act_mod and legs_num > 1) then
					
					if rte12_sel == 0 then
						
						if rte12_act ~= 0 then
							rte12_act = 0
							rte12_changed = 1
						end
						
						hold_exec2 = 0
						ref_exec2 = 0
						des_exec2 = 0
						rte_exec2 = 0
						-- forecast_exec = 0
						
						if ref_exec ~= 0 or des_exec ~= 0 then
							if ref_exec ~= 0 then
								offset = 0
								offset_seq = 0
								nav_mode = 0
								
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									reset_dl_times()
									if ref_icao_x == "ORGN" then
										dl_ref = "----"
									else
										dl_ref = ref_icao_x
									end
									dl_flt_num = flt_num_x
									dl_day = simDR_time_day
								end
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
							end
							
							ref_exec = 0
							des_exec = 0
							
							if dl_ref ~= "----" then
								dl_des = des_icao_x
							end
							
							
							ref_icao = ref_icao_x
							ref_icao_lat = ref_icao_lat_x
							ref_icao_lon = ref_icao_lon_x
							ref_tns_alt = ref_tns_alt_x
							ref_tns_lvl = ref_tns_lvl_x
							ref_icao_alt = ref_icao_alt_x
							trans_alt = trans_alt_mod
							
							ref_gate = ref_gate_x
							co_route = co_route_x
							flt_num = flt_num_x
							ref_rwy = ref_rwy2
							ref_rwy3 = ref_rwy2
							
							ref_sid = ref_sid2
							ref_sid_tns = ref_sid_tns2
							
							des_app_from_apt = 0
							
							des_icao = des_icao_x
							des_rnw = des_rnw2
							des_icao_lat = des_icao_lat_x
							des_icao_lon = des_icao_lon_x
							des_tns_alt = des_tns_alt_x
							des_tns_lvl = des_tns_lvl_x
							des_icao_alt = des_icao_alt_x
							trans_lvl = trans_lvl_mod
							
							des_app = des_app2
							des_app_tns = des_app_tns2
							des_star = des_star2
							des_star_trans = des_star_trans2
							
							arr_data = arr_data_x
							des_app_rw_only = des_app_rw_only_x
							rw_ext_dist = rw_ext_dist_x
							rw_ext_fpa = rw_ext_fpa_x
							
							offset_act = 0
							offset_start = 0
							offset_end = 0
							offset_dist = 0
							offset_side = 0
							offset_act_mod = 0
							offset_start_mod = 0
							offset_end_mod = 0
							offset_dist_mod = 0
							offset_side_mod = 0
							offset_start_def = 0
							offset_end_def = 0
							offset_start_def_mod = 0
							offset_end_def_mod = 0
							if rte12_changed == 0 then
								crz_alt = "*****"
								crz_alt_num = 0
								crz_alt_num2 = 0
							end
							
							if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then	--descent, approach, goaround arm
								B738DR_flight_phase = 1
								missed_app_alt_change = 0
								B738DR_fms_descent_now = 0
								if B738DR_mcp_alt_dial <= simDR_altitude_pilot and B738DR_altitude_mode == 5 then
									vnav_alt_mode = 1
								end
							end
							altitude_last = elevation_ft
							reverse_rte1 = 0
						end
						
						
						if legs_delete == 1 or rte_exec == 1 then
							
							if rte12_sel == 0 then
								rte12_copy_done = 0
							elseif rte12_sel ~= 0 then
								rte21_copy_done = 0
							end
							
							if simDR_radio_height_pilot_ft < 50 and ref_rwy ~= ref_rwy2 then
								if true_legs_num(1) > 0 then
									offset = math.max(offset, 2)
									if offset_seq < offset or offset_seq > legs_num then
										offset_seq = offset
									end
								else
									offset = 0
									offset_seq = 0
								end
								nav_mode = 0
								last_offset = offset
							end
							
							reverse_rte1 = 0
							
							if rte_exec == 1 then
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if ref_rwy ~= ref_rwy2 and ref_rwy ~= "-----" then
										if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
										end
									end
								end
								
								ref_icao = ref_icao_x
								ref_icao_lat = ref_icao_lat_x
								ref_icao_lon = ref_icao_lon_x
								ref_tns_alt = ref_tns_alt_x
								ref_tns_lvl = ref_tns_lvl_x
								ref_icao_alt = ref_icao_alt_x
								trans_alt = trans_alt_mod
								
								ref_gate = ref_gate_x
								co_route = co_route_x
								flt_num = flt_num_x
								ref_rwy = ref_rwy2
								ref_rwy3 = ref_rwy2
								
								ref_rwy = ref_rwy2
								ref_sid = ref_sid2
								ref_sid_tns = ref_sid_tns2
								
								des_app_from_apt = 0
								
								des_icao = des_icao_x
								des_rnw = des_rnw2
								des_icao_lat = des_icao_lat_x
								des_icao_lon = des_icao_lon_x
								des_tns_alt = des_tns_alt_x
								des_tns_lvl = des_tns_lvl_x
								des_icao_alt = des_icao_alt_x
								trans_lvl = trans_lvl_mod
								
								des_app = des_app2
								des_app_tns = des_app_tns2
								des_star = des_star2
								des_star_trans = des_star_trans2
								
								arr_data = arr_data_x
								des_app_rw_only = des_app_rw_only_x
								rw_ext_dist = rw_ext_dist_x
								rw_ext_fpa = rw_ext_fpa_x
								
								create_fpln(rte12_sel)
								copy_to_legsdata(rte12_sel)
								
								if rte12_changed ~= 0 then
									rte12_changed = 0
									
									offset = offset_mod
									-- offset = offset_mod2
									offset_seq = offset
									
									last_offset2  = offset
									offset_mod_x2 = offset_mod
									
									-- last_offset8  = offset
									-- offset_mod_x8 = offset_mod2
								end
								
								if exec_activate_fpln == 1 then
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										reset_dl_times()
										if ref_icao_x == "ORGN" then
											dl_ref = "----"
										else
											dl_ref = ref_icao_x
										end
										dl_flt_num = flt_num_x
										dl_day = simDR_time_day
										dl_des = des_icao_x
									end
								end
								
							else
								if legs_num > 1 then
									
									if legs_abeam_selected ~= 0 and B738DR_legs_abeam_avail ~= 0 and legs_abeam_data_n > 0 then
										-- rte_copy(offset)
										rte_copy(offset_mod, rte12_sel)
										local abeam_wpt_added = 0
										local act_abeam_wpt = 0
										local delta_crs = 0
										for ab = 1, legs_abeam_data_n do
											if B738DR_legs_abeam_lat2[ab - 1] ~= 0 or B738DR_legs_abeam_lon2[ab - 1] ~= 0 then
												-- act_abeam_wpt = offset + abeam_wpt_added
												act_abeam_wpt = offset_mod + abeam_wpt_added
												-- add abeam wpt
												legs_data2[act_abeam_wpt] = {}
												legs_data2[act_abeam_wpt][1] = legs_abeam_data[ab][4]		--entry
												-- legs_data2[act_abeam_wpt][2] = 0		-- brg
												-- legs_data2[act_abeam_wpt][3] = 0		-- distance
												legs_data2[act_abeam_wpt][4] = 0		-- speed
												legs_data2[act_abeam_wpt][5] = 0		-- altitude
												legs_data2[act_abeam_wpt][6] = 0	-- altitude type
												legs_data2[act_abeam_wpt][7] = B738DR_legs_abeam_lat2[ab - 1]		-- latitude
												legs_data2[act_abeam_wpt][8] = B738DR_legs_abeam_lon2[ab - 1]		-- longitude
												legs_data2[act_abeam_wpt][9] = "DIRECT"			-- via id
												legs_data2[act_abeam_wpt][10] = 0		-- calc speed
												legs_data2[act_abeam_wpt][11] = 0		-- calc altitude
												legs_data2[act_abeam_wpt][12] = 0		-- calc altitude vnav pth
												legs_data2[act_abeam_wpt][13] = 0
												legs_data2[act_abeam_wpt][14] = 0		-- rest alt
												legs_data2[act_abeam_wpt][15] = 0		-- last fuel
												legs_data2[act_abeam_wpt][16] = ""		-- reg code
												legs_data2[act_abeam_wpt][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
												-- legs_data2[act_abeam_wpt][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
												legs_data2[act_abeam_wpt][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
												legs_data2[act_abeam_wpt][20] = 0
												legs_data2[act_abeam_wpt][21] = -1
												legs_data2[act_abeam_wpt][22] = ""
												legs_data2[act_abeam_wpt][23] = 0
												legs_data2[act_abeam_wpt][24] = 0
												legs_data2[act_abeam_wpt][25] = 0
												legs_data2[act_abeam_wpt][26] = 0
												legs_data2[act_abeam_wpt][27] = ""
												legs_data2[act_abeam_wpt][28] = ""
												legs_data2[act_abeam_wpt][29] = ""
												legs_data2[act_abeam_wpt][30] = ""
												-- legs_data2[act_abeam_wpt][31] = "TF"
												legs_data2[act_abeam_wpt][32] = 0
												legs_data2[act_abeam_wpt][33] = ""
												legs_data2[act_abeam_wpt][34] = ""
												legs_data2[act_abeam_wpt][35] = ""
												legs_data2[act_abeam_wpt][36] = 0
												-- legs_data2[act_abeam_wpt][37] = 0
												legs_data2[act_abeam_wpt][38] = ""
												if B738DR_legs_abeam_dist[ab - 1] < 100 then
													legs_data2[act_abeam_wpt][39] = legs_abeam_data[ab][5]		-- Wind data
												else
													legs_data2[act_abeam_wpt][39] = ""
												end
												legs_data2[act_abeam_wpt][40] = 0
												legs_data2[act_abeam_wpt][41] = 0
												legs_data2[act_abeam_wpt][42] = 0
												legs_data2[act_abeam_wpt][43] = ""
												legs_data2[act_abeam_wpt][44] = 0
												legs_data2[act_abeam_wpt][45] = 0
												legs_data2[act_abeam_wpt][46] = 0
												legs_data2[act_abeam_wpt][47] = 0
												legs_data2[act_abeam_wpt][48] = ""
												legs_data2[act_abeam_wpt][49] = 0
												legs_data2[act_abeam_wpt][50] = 0
												legs_data2[act_abeam_wpt][51] = 0
												legs_data2[act_abeam_wpt][52] = 0
												legs_data2[act_abeam_wpt][53] = 0
												legs_data2[act_abeam_wpt][54] = 0
												
												-- recalc_rte2(act_abeam_wpt)
												legs_data2[act_abeam_wpt][3] = nd_calc_dist2(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
												
												if ab == 1 then
													legs_data2[act_abeam_wpt][31] = "DF"
												else
													legs_data2[act_abeam_wpt][31] = "TF"
												end
												
												if legs_data2[act_abeam_wpt][3] == 0 then
													legs_data2[act_abeam_wpt][2] = legs_data2[act_abeam_wpt-1][2]
												else
													legs_data2[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
												end
												legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
												if legs_data2[act_abeam_wpt][18] < 0 then
													legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][18] + (2 * math.pi)
												end
												if string.sub(legs_data2[act_abeam_wpt][1], 1, 2) == "RW" then
													legs_data2[act_abeam_wpt][37] = legs_data2[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
												else
													delta_crs = (nd_calc_brg(legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8], legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8]) + 180) % 360
													legs_data2[act_abeam_wpt][37] = math.rad(delta_crs)
												end
												
												abeam_wpt_added = abeam_wpt_added + 1
											end
										end
										rte_paste(offset_mod + abeam_wpt_added, rte12_sel)
										
										if abeam_wpt_added ~= 0 then
											act_abeam_wpt = offset_mod + abeam_wpt_added
											
											legs_data2[act_abeam_wpt][3] = nd_calc_dist2(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
											
											legs_data2[act_abeam_wpt][31] = "TF"
											
											if legs_data2[act_abeam_wpt][3] == 0 then
												legs_data2[act_abeam_wpt][2] = legs_data2[act_abeam_wpt-1][2]
											else
												legs_data2[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
											end
											legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
											if legs_data2[act_abeam_wpt][18] < 0 then
												legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][18] + (2 * math.pi)
											end
											if string.sub(legs_data2[act_abeam_wpt][1], 1, 2) == "RW" then
												legs_data2[act_abeam_wpt][37] = legs_data2[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
											else
												delta_crs = (nd_calc_brg(legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8], legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8]) + 180) % 360
												legs_data2[act_abeam_wpt][37] = math.rad(delta_crs)
											end
										end
										
										-- calc_rte_enable2 = 1
									end
									
									-- recalc abeam data: distance, brg,...
									-- abeamsk
									
									copy_to_legsdata(rte12_sel)
									
									--- new ---
									if ref_rwy ~= ref_rwy2 and ref_rwy ~= "-----" then
										if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
										end
									end
									ref_rwy = ref_rwy2
									ref_sid = ref_sid2
									ref_sid_tns = ref_sid_tns2
									des_rnw = des_rnw2
									des_app = des_app2
									des_app_tns = des_app_tns2
									des_star = des_star2
									des_star_trans = des_star_trans2
									arr_data = arr_data_x
									des_app_rw_only = des_app_rw_only_x
									rw_ext_dist = rw_ext_dist_x
									rw_ext_fpa = rw_ext_fpa_x
								end
								offset = offset_mod
								
							end
							-- legs_delete = 0
							rte_exec = 0
							if offset > legs_num then
								offset = legs_num
								offset_seq = offset
							end
							
							exec_activate_fpln = 0
							
							legs_abeam_data_n = 0
							legs_abeam_data = {}
							legs_abeam_selected = 0
							B738DR_legs_abeam_avail = 0
							B738DR_legs_abeam_num = 0
							
							--dump_leg4()
							-- legs_delete = 0
						end
						
						if (page_offset == 1 or legs_delete ~= 0) and offset_act ~= offset_act_mod and legs_num > 1 then
							-- execute Lateral Offset
							if offset_dist_mod == 0 then
								if offset_act >= 5 and offset_act < 8 then
									offset_act = 9
									offset_act_mod = 9
									B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
									B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
									B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
									B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
									B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
									B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
									B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
									B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
								elseif offset_act >= 8 then
									offset_act_mod = offset_act
								else
									offset_act = 0
									offset_start = 0
									offset_end = 0
									offset_dist = 0
									offset_side = 0
									-- legs_off_tmp_n = 0
									delete_offset_legs()
									delete_offset_legs2(rte12_act)
									offset_act_mod = 0
									offset_start_mod = 0
									offset_end_mod = 0
									offset_dist_mod = 0
									offset_side_mod = 0
									offset_start_def = 0
									offset_end_def = 0
									offset_start_def_mod = 0
									offset_end_def_mod = 0
								end
							else
								-- update_offset_idx2(rte12_act)
								offset_check(rte12_act)
								if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
									-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
										-- offset_act_mod = 5
									-- else
										-- offset_act_mod = 7
									-- end
									
									if rte12_act == 0 then
										if offset_start_mod == 0 then
											if legs_data2[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									else
										if offset_start_mod == 0 then
											if legs_data8[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									end
								end
								offset_act = offset_act_mod
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
								
								offset_start = offset_start_mod
								offset_end = offset_end_mod
								offset_dist = offset_dist_mod
								offset_side = offset_side_mod
								offset_start_def = offset_start_def_mod
								offset_end_def = offset_end_def_mod
								B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
								B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
								B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
								B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
								B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
								B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
								B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
								B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
							end
							
							-- B738DR_fms_test3 = offset_act_mod
							
						end
						
						legs_delete = 0
						
						rte12_copy_done = 0
						
					else
						if rte12_act ~= 1 then
							rte12_act = 1
							rte12_changed = 1
						end
						
						hold_exec = 0
						ref_exec = 0
						des_exec = 0
						rte_exec = 0
						-- forecast_exec = 0
						
						if ref_exec2 ~= 0 or des_exec2 ~= 0 then
							if ref_exec2 ~= 0 then
								offset = 0
								offset_seq = 0
								nav_mode = 0
								
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									reset_dl_times()
									if ref_icao7_x == "ORGN" then
										dl_ref = "----"
									else
										dl_ref = ref_icao7_x
									end
									dl_flt_num = flt_num_x
									dl_day = simDR_time_day
								end
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
							end
							
							ref_exec2 = 0
							des_exec2 = 0
							
							if dl_ref ~= "----" then
								dl_des = des_icao7_x
							end
							
							ref_icao7 = ref_icao7_x
							ref_icao_lat7 = ref_icao_lat7_x
							ref_icao_lon7 = ref_icao_lon7_x
							ref_tns_alt7 = ref_tns_alt7_x
							ref_tns_lvl7 = ref_tns_lvl7_x
							ref_icao_alt7 = ref_icao_alt7_x
							trans_alt7 = trans_alt_mod2
							
							ref_gate7 = ref_gate7_x
							co_route7 = co_route7_x
							flt_num = flt_num_x
							ref_rwy7 = ref_rwy8
							ref_rwy9 = ref_rwy8
							
							ref_sid7 = ref_sid8
							ref_sid_tns7 = ref_sid_tns8
							
							des_app_from_apt7 = 0
							
							
							des_icao7 = des_icao7_x
							des_rnw7 = des_rnw8
							des_icao_lat7 = des_icao_lat7_x
							des_icao_lon7 = des_icao_lon7_x
							des_tns_alt7 = des_tns_alt7_x
							des_tns_lvl7 = des_tns_lvl7_x
							des_icao_alt7 = des_icao_alt7_x
							trans_lvl7 = trans_lvl_mod2
							
							des_app7 = des_app8
							des_app_tns7 = des_app_tns8
							des_star7 = des_star8
							des_star_trans7 = des_star_trans8
							
							arr_data7 = arr_data7_x
							des_app_rw_only7 = des_app_rw_only7_x
							rw_ext_dist7 = rw_ext_dist7_x
							rw_ext_fpa7 = rw_ext_fpa7_x
							
							offset_act = 0
							offset_start = 0
							offset_end = 0
							offset_dist = 0
							offset_side = 0
							offset_act_mod = 0
							offset_start_mod = 0
							offset_end_mod = 0
							offset_dist_mod = 0
							offset_side_mod = 0
							offset_start_def = 0
							offset_end_def = 0
							offset_start_def_mod = 0
							offset_end_def_mod = 0
							if rte12_changed == 0 then
								crz_alt = "*****"
								crz_alt_num = 0
								crz_alt_num2 = 0
							end
							
							if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
								B738DR_flight_phase = 1
								missed_app_alt_change = 0
								B738DR_fms_descent_now = 0
								if B738DR_mcp_alt_dial <= simDR_altitude_pilot and B738DR_altitude_mode == 5 then
									vnav_alt_mode = 1
								end
							end
							altitude_last = elevation_ft
							
							reverse_rte2 = 0
						end
						
						
						if legs_delete == 1 or rte_exec2 == 1 then
							
							if simDR_radio_height_pilot_ft < 50 and ref_rwy7 ~= ref_rwy8 then
								if true_legs_num(1) > 0 then
									offset = math.max(offset, 2)
									if offset_seq < offset or offset_seq > legs_num then
										offset_seq = offset
									end
								else
									offset = 0
									offset_seq = 0
								end
								nav_mode = 0
								last_offset = offset
							end
							
							reverse_rte2 = 0
							
							if rte_exec2 == 1 then
								if ref_rwy7 ~= ref_rwy8 and ref_rwy7 ~= "-----" then
									if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
										v1_set = "---"
										vr_set = "---"
										v2_set = "---"
										verify_to_spd = 0
										add_fmc_msg(TO_SPEEDS_DELETED, 1)
									end
								end
								
								ref_icao7 = ref_icao7_x
								ref_icao_lat7 = ref_icao_lat7_x
								ref_icao_lon7 = ref_icao_lon7_x
								ref_tns_alt7 = ref_tns_alt7_x
								ref_tns_lvl7 = ref_tns_lvl7_x
								ref_icao_alt7 = ref_icao_alt7_x
								trans_alt7 = trans_alt_mod2
								
								ref_gate7 = ref_gate7_x
								co_route7 = co_route7_x
								flt_num = flt_num_x
								ref_rwy7 = ref_rwy8
								ref_rwy9 = ref_rwy8
								
								ref_rwy7 = ref_rwy8
								ref_sid7 = ref_sid8
								ref_sid_tns7 = ref_sid_tns8
								
								des_app_from_apt7 = 0
								
								des_icao7 = des_icao7_x
								des_rnw7 = des_rnw8
								des_icao_lat7 = des_icao_lat7_x
								des_icao_lon7 = des_icao_lon7_x
								des_tns_alt7 = des_tns_alt7_x
								des_tns_lvl7 = des_tns_lvl7_x
								des_icao_alt7 = des_icao_alt7_x
								trans_lvl7 = trans_lvl_mod2
								
								des_app7 = des_app8
								des_app_tns7 = des_app_tns8
								des_star7 = des_star8
								des_star_trans7 = des_star_trans8
								
								arr_data7 = arr_data7_x
								des_app_rw_only7 = des_app_rw_only7_x
								rw_ext_dist7 = rw_ext_dist7_x
								rw_ext_fpa7 = rw_ext_fpa7_x
								
								create_fpln(rte12_sel)
								copy_to_legsdata(rte12_sel)
								
								if rte12_changed ~= 0 then
									rte12_changed = 0
									
									-- offset = offset_mod
									offset = offset_mod2
									offset_seq = offset
									
									-- last_offset2  = offset
									-- offset_mod_x2 = offset_mod
									
									last_offset8  = offset
									offset_mod_x8 = offset_mod2
								end
								
								if exec_activate_fpln2 == 1 then
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										reset_dl_times()
										if ref_icao7_x == "ORGN" then
											dl_ref = "----"
										else
											dl_ref = ref_icao7_x
										end
										dl_flt_num = flt_num_x
										dl_day = simDR_time_day
										dl_des = des_icao7_x
									end
								end
								offset_mod_x2 = 0
								
							else
								if legs_num > 1 then
									
									if legs_abeam_selected ~= 0 and B738DR_legs_abeam_avail ~= 0 and legs_abeam_data_n > 0 then
										rte_copy(offset_mod2, rte12_sel)
										local abeam_wpt_added = 0
										local act_abeam_wpt = 0
										local delta_crs = 0
										for ab = 1, legs_abeam_data_n do
											if B738DR_legs_abeam_lat2[ab - 1] ~= 0 or B738DR_legs_abeam_lon2[ab - 1] ~= 0 then
												act_abeam_wpt = offset_mod2 + abeam_wpt_added
												-- add abeam wpt
												legs_data8[act_abeam_wpt] = {}
												legs_data8[act_abeam_wpt][1] = legs_abeam_data[ab][4]		--entry
												legs_data8[act_abeam_wpt][4] = 0		-- speed
												legs_data8[act_abeam_wpt][5] = 0		-- altitude
												legs_data8[act_abeam_wpt][6] = 0	-- altitude type
												legs_data8[act_abeam_wpt][7] = B738DR_legs_abeam_lat2[ab - 1]		-- latitude
												legs_data8[act_abeam_wpt][8] = B738DR_legs_abeam_lon2[ab - 1]		-- longitude
												legs_data8[act_abeam_wpt][9] = "DIRECT"			-- via id
												legs_data8[act_abeam_wpt][10] = 0		-- calc speed
												legs_data8[act_abeam_wpt][11] = 0		-- calc altitude
												legs_data8[act_abeam_wpt][12] = 0		-- calc altitude vnav pth
												legs_data8[act_abeam_wpt][13] = 0
												legs_data8[act_abeam_wpt][14] = 0		-- rest alt
												legs_data8[act_abeam_wpt][15] = 0		-- last fuel
												legs_data8[act_abeam_wpt][16] = ""		-- reg code
												legs_data8[act_abeam_wpt][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
												legs_data8[act_abeam_wpt][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
												legs_data8[act_abeam_wpt][20] = 0
												legs_data8[act_abeam_wpt][21] = -1
												legs_data8[act_abeam_wpt][22] = ""
												legs_data8[act_abeam_wpt][23] = 0
												legs_data8[act_abeam_wpt][24] = 0
												legs_data8[act_abeam_wpt][25] = 0
												legs_data8[act_abeam_wpt][26] = 0
												legs_data8[act_abeam_wpt][27] = ""
												legs_data8[act_abeam_wpt][28] = ""
												legs_data8[act_abeam_wpt][29] = ""
												legs_data8[act_abeam_wpt][30] = ""
												legs_data8[act_abeam_wpt][32] = 0
												legs_data8[act_abeam_wpt][33] = ""
												legs_data8[act_abeam_wpt][34] = ""
												legs_data8[act_abeam_wpt][35] = ""
												legs_data8[act_abeam_wpt][36] = 0
												legs_data8[act_abeam_wpt][38] = ""
												if B738DR_legs_abeam_dist[ab - 1] < 100 then
													legs_data8[act_abeam_wpt][39] = legs_abeam_data[ab][5]		-- Wind data
												else
													legs_data8[act_abeam_wpt][39] = ""
												end
												legs_data8[act_abeam_wpt][40] = 0
												legs_data8[act_abeam_wpt][41] = 0
												legs_data8[act_abeam_wpt][42] = 0
												legs_data8[act_abeam_wpt][43] = ""
												legs_data8[act_abeam_wpt][44] = 0
												legs_data8[act_abeam_wpt][45] = 0
												legs_data8[act_abeam_wpt][46] = 0
												legs_data8[act_abeam_wpt][47] = 0
												legs_data8[act_abeam_wpt][48] = ""
												legs_data8[act_abeam_wpt][49] = 0
												legs_data8[act_abeam_wpt][50] = 0
												legs_data8[act_abeam_wpt][51] = 0
												legs_data8[act_abeam_wpt][52] = 0
												legs_data8[act_abeam_wpt][53] = 0
												legs_data8[act_abeam_wpt][54] = 0
												
												legs_data8[act_abeam_wpt][3] = nd_calc_dist2(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
												
												if ab == 1 then
													legs_data8[act_abeam_wpt][31] = "DF"
												else
													legs_data8[act_abeam_wpt][31] = "TF"
												end
												
												if legs_data8[act_abeam_wpt][3] == 0 then
													legs_data8[act_abeam_wpt][2] = legs_data8[act_abeam_wpt-1][2]
												else
													legs_data8[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
												end
												legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
												if legs_data8[act_abeam_wpt][18] < 0 then
													legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][18] + (2 * math.pi)
												end
												if string.sub(legs_data8[act_abeam_wpt][1], 1, 2) == "RW" then
													legs_data8[act_abeam_wpt][37] = legs_data8[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
												else
													delta_crs = (nd_calc_brg(legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8], legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8]) + 180) % 360
													legs_data8[act_abeam_wpt][37] = math.rad(delta_crs)
												end
												
												abeam_wpt_added = abeam_wpt_added + 1
											end
										end
										rte_paste(offset_mod2 + abeam_wpt_added, rte12_sel)
										
										if abeam_wpt_added ~= 0 then
											act_abeam_wpt = offset_mod + abeam_wpt_added
											
											legs_data8[act_abeam_wpt][3] = nd_calc_dist2(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
											
											legs_data8[act_abeam_wpt][31] = "TF"
											
											if legs_data8[act_abeam_wpt][3] == 0 then
												legs_data8[act_abeam_wpt][2] = legs_data8[act_abeam_wpt-1][2]
											else
												legs_data8[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
											end
											legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
											if legs_data8[act_abeam_wpt][18] < 0 then
												legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][18] + (2 * math.pi)
											end
											if string.sub(legs_data8[act_abeam_wpt][1], 1, 2) == "RW" then
												legs_data8[act_abeam_wpt][37] = legs_data8[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
											else
												delta_crs = (nd_calc_brg(legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8], legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8]) + 180) % 360
												legs_data8[act_abeam_wpt][37] = math.rad(delta_crs)
											end
										end
										
									end
									
									-- recalc abeam data: distance, brg,...
									-- abeamsk
									
									copy_to_legsdata(rte12_sel)
									
									--- new ---
									if ref_rwy7 ~= ref_rwy8 and ref_rwy7 ~= "-----" then
										if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
										end
									end
									ref_rwy7 = ref_rwy8
									ref_sid7 = ref_sid8
									ref_sid_tns7 = ref_sid_tns8
									des_rnw7 = des_rnw8
									des_app7 = des_app8
									des_app_tns7 = des_app_tns8
									des_star7 = des_star8
									des_star_trans7 = des_star_trans8
									arr_data7 = arr_data7_x
									des_app_rw_only7 = des_app_rw_only7_x
									rw_ext_dist7 = rw_ext_dist7_x
									rw_ext_fpa7 = rw_ext_fpa7_x
								end
								offset = offset_mod2
								
							end
							-- legs_delete = 0
							rte_exec2 = 0
							if offset > legs_num then
								offset = legs_num
								offset_seq = offset
							end
							
							exec_activate_fpln2 = 0
							
							legs_abeam_data_n = 0
							legs_abeam_data = {}
							legs_abeam_selected = 0
							B738DR_legs_abeam_avail = 0
							B738DR_legs_abeam_num = 0
							
							-- legs_delete = 0
						end
						
						if (page_offset == 1 or legs_delete ~= 0) and offset_act ~= offset_act_mod and legs_num > 1 then
							-- execute Lateral Offset
							if offset_dist_mod == 0 then
								if offset_act >= 5 and offset_act < 8 then
									offset_act = 9
									offset_act_mod = 9
									B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
									B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
									B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
									B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
									B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
									B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
									B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
									B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
								elseif offset_act >= 8 then
									offset_act_mod = offset_act
								else
									offset_act = 0
									offset_start = 0
									offset_end = 0
									offset_dist = 0
									offset_side = 0
									-- legs_off_tmp_n = 0
									delete_offset_legs()
									delete_offset_legs2(rte12_act)
									offset_act_mod = 0
									offset_start_mod = 0
									offset_end_mod = 0
									offset_dist_mod = 0
									offset_side_mod = 0
									offset_start_def = 0
									offset_end_def = 0
									offset_start_def_mod = 0
									offset_end_def_mod = 0
								end
							else
								-- update_offset_idx2(rte12_act)
								offset_check(rte12_act)
								if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
									-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
										-- offset_act_mod = 5
									-- else
										-- offset_act_mod = 7
									-- end
									
									if rte12_act == 0 then
										if offset_start_mod == 0 then
											if legs_data2[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									else
										if offset_start_mod == 0 then
											if legs_data8[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									end
								end
								offset_act = offset_act_mod
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
								
								offset_start = offset_start_mod
								offset_end = offset_end_mod
								offset_dist = offset_dist_mod
								offset_side = offset_side_mod
								offset_start_def = offset_start_def_mod
								offset_end_def = offset_end_def_mod
								B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
								B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
								B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
								B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
								B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
								B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
								B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
								B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
							end
						end
						legs_delete = 0
						
						rte21_copy_done = 0
					end
				end
			end
			
			-- B738DR_fms_test6 = 2
			
			-- if page_perf == 1 then
			if page_perf == 1 or page_legs > 0 or page_dep > 0 or page_arr > 0 or page_rte_init > 0 or page_rte_legs > 0 
			or page_climb > 0 or page_cruise > 0 or page_descent > 0 then
				if perf_exec == 1 then
					perf_exec = 2
					B738_calc_vnav_spd()
					vnav_update = 1
				elseif perf_exec == 3 then
					perf_exec = 2
					
					gw = gw_mod
					gw_lbs = gw_lbs_mod
					gw_kgs = gw_kgs_mod
					zfw = zfw_mod
					zfw_lbs = zfw_lbs_mod
					zfw_kgs = zfw_kgs_mod
					plan_weight = plan_weight_mod
					plan_weight_kgs = plan_weight_kgs_mod
					plan_weight_lbs = plan_weight_lbs_mod
					reserves = reserves_mod
					reserves_kgs = reserves_kgs_mod
					reserves_lbs = reserves_lbs_mod
					cost_index = cost_index_mod
					crz_wind_dir = crz_wind_dir_mod
					crz_wind_spd = crz_wind_spd_mod
					isa_dev_f = isa_dev_f_mod
					isa_dev_c = isa_dev_c_mod
					tc_oat_c = tc_oat_c_mod
					tc_oat_f = tc_oat_f_mod
					msg_chk_alt_constr = 0
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
							qrh = "OFF"
							entry = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
					
					B738_calc_vnav_spd()
					vnav_update = 1
				end
			end
			
			-- B738DR_fms_test6 = 3
			
			
			-- B738DR_fms_test6 = 4
			
			-- if page_legs > 0 then
			if page_perf == 1 or page_legs > 0 or page_dep > 0 or page_arr > 0 or page_rte_init > 0 or page_rte_legs > 0 
			or page_climb > 0 or page_cruise > 0 or page_descent > 0 or page_hold > 0 then
				local disable_legs_intdir = false
				if exec_activate_fpln ~= 0 and legs_intdir_rte == 0 then
					disable_legs_intdir = true
				elseif exec_activate_fpln2 ~= 0 and legs_intdir_rte == 1 then
					disable_legs_intdir = true
				end
				if legs_intdir == 1 and disable_legs_intdir == false then
					-- take current position
					last_lat = simDR_latitude
					last_lon = simDR_longitude
					if legs_intdir_crs2 < 0 then
						legs_intdir_crs = -1
					else
						legs_intdir_crs = legs_intdir_crs_mod
					end
					legs_intdir = 0
					legs_intdir_act = 1
					legs_intdir_crs2 = -1
					calc_rte_enable = 1
					block_df = 0
					B738DR_fmc_trk_turn = -1
					B738DR_fmc_trk_turn2 = -1
					legs_intdir_trk2_mem = legs_intdir_trk2
					legs_intdir_radius_mem = legs_intdir_radius_mod
					legs_intdir_rad_lat_mem = legs_intdir_rad_lat_mod
					legs_intdir_rad_lon_mem = legs_intdir_rad_lon_mod
					legs_intdir_turn_mem = legs_intdir_turn_mod
					legs_intdir_end_lat_mem = legs_intdir_end_lat_mod
					legs_intdir_end_lon_mem = legs_intdir_end_lon_mod
					B738DR_fms_legs_bypass[offset] = 0
					B738DR_fms_legs_change = 0
				end
			end
			
			if page_hold > 0 then
				if hold_exec == 1 and rte12_sel == 0 then
					if B738DR_hold_term == 0 or rte12_act ~= rte12_sel then
						if rte12_act == rte12_sel then
							copy_to_legsdata(rte12_sel)
						end
						entry = ""
						hold_exec = 0
						new_hold = 0
						new_hold_idx = 0
						new_hold_act_page = 0
						hold_offset = 0
						if hold_data_num2 > 0 then
							for qq = 1, hold_data_num2 do
								if hold_data2[qq] >= offset_mod then
									hold_offset = qq
									break
								end
							end
							if legs_data[offset][1] == "PPOS" and rte12_act == rte12_sel then
								legs_data[offset][7] = simDR_latitude
								legs_data[offset][8] = simDR_longitude
								legs_data2[offset][7] = simDR_latitude
								legs_data2[offset][8] = simDR_longitude
								-- -- PPOS HOLD activated
								
								calc_rte_enable2 = 1
								
								B738DR_hold_phase = 1
								B738DR_fmc_trk_turn = -1
								hold_timer = 0
								nav_mode = 3
								legs_intdir_act = 0
								legs_intdir_crs = -1
							end
						end
					else
						B738DR_hold_term = 2
						hold_exec = 0
						new_hold_idx = 0
						new_hold_act_page = 0
					end
				elseif hold_exec2 == 1 and rte12_sel == 1 then
					if B738DR_hold_term == 0 or rte12_act ~= rte12_sel then
						if rte12_act == rte12_sel then
							copy_to_legsdata(rte12_sel)
						end
						
						entry = ""
						hold_exec2 = 0
						new_hold2 = 0
						new_hold_idx2 = 0
						new_hold_act_page2 = 0
						hold_offset2 = 0
						if hold_data_num4 > 0 then
							for qq = 1, hold_data_num4 do
								if hold_data4[qq] >= offset_mod2 then
									hold_offset2 = qq
									break
								end
							end
							if legs_data[offset][1] == "PPOS" and rte12_act == rte12_sel then
								legs_data[offset][7] = simDR_latitude
								legs_data[offset][8] = simDR_longitude
								legs_data2[offset][7] = simDR_latitude
								legs_data2[offset][8] = simDR_longitude
								-- -- PPOS HOLD activated
								
								calc_rte_enable8 = 1
								
								B738DR_hold_phase = 1
								B738DR_fmc_trk_turn = -1
								hold_timer = 0
								nav_mode = 3
								legs_intdir_act = 0
								legs_intdir_crs = -1
							end
						end
					else
						B738DR_hold_term = 2
						hold_exec2 = 0
						new_hold_idx2 = 0
						new_hold_act_page2 = 0
					end
				end
			end
			
			if page_ref_nav_data_wpt == 1 then
				if ref_nav_exec == 1 then
					local ref_nav_new_act = 0
					local ii = 0
					
					if nav_data_idx2 == 0 then
						wptx_n = wptx_n + 1
						wptx[wptx_n] = {}
						nav_data_idx2 = wptx_n
					end
					ref_nav_new_act = nav_data_idx2
					
					if ref_nav_new == "*****" then
						wptx[ref_nav_new_act][1] = ref_nav_wpt
					else
						wptx[ref_nav_new_act][1] = ref_nav_new
					end
					wptx[ref_nav_new_act][2] = ref_nav_lat
					wptx[ref_nav_new_act][3] = ref_nav_lon
					if ref_nav_id == "" then
						wptx[ref_nav_new_act][4] = " "
					else
						wptx[ref_nav_new_act][4] = ref_nav_id
					end
					if ref_nav_rc == "" then
						wptx[ref_nav_new_act][5] = " "
					else
						wptx[ref_nav_new_act][5] = ref_nav_rc
					end
					wptx[ref_nav_new_act][6] = ref_nav_brg
					wptx[ref_nav_new_act][7] = ref_nav_dist
					if ref_nav_mag_var == "" then
						wptx[ref_nav_new_act][8] = "x"
					elseif string.sub(ref_nav_mag_var, 1, 1) == ">" then
						wptx[ref_nav_new_act][8] = "x"
					else
						wptx[ref_nav_new_act][8] = ref_nav_mag_var
					end
					save_cust_wpt()
					ref_nav_exec = 0
				end
			end
			
			update_rnw_list(rte12_sel)
			
			-- B738DR_fms_test3 = 1
		end
		
		
		fmc1_input_lag = 0
		rte12_changed = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc1_A_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "A")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_B_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "B")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_C_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "C")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_D_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "D")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_E_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "E")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_F_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "F")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_G_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "G")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_H_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "H")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_I_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "I")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_J_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "J")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_K_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "K")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_L_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "L")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_M_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "M")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_N_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "N")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_O_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "O")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_P_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "P")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_Q_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "Q")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_R_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "R")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_S_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "S")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_T_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "T")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_U_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "U")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_V_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "V")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_W_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "W")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_X_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "X")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_Y_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "Y")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_Z_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. "Z")
			end
			item_sel = 0
			item_sel_act = 0
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc1_SP_CMDhandler(phase, duration)
	if phase == 0 and fmc1_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc_lock == 0 then
			if string.len(entry) < 24 then
				entry = (entry .. " ")
			end
			item_sel_via = 0
		end
		fmc1_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_autopilot_alt_interv_CMDhandler(phase, duration)
	
	local n = 0
	local nn = 0
	local nnn = 0
	local end_idx = 0
	local temp_spd_rest = 0
	local temp_page = 0
	local temp_key = 0
	local temp_spd_txt = ""
	local restrict_deleted = 0
	local delete_enabled = 0
	local delete_enabled2 = 0
	local first_alt_intv = 0
	
	if phase == 0 then
		B738DR_autopilot_alt_interv_pos = 1
		
		if B738DR_alt_intv_but ~= 0 then
			
			local fms_descent_now_old = B738DR_fms_descent_now
			
			if B738DR_autopilot_vnav_status == 1 then
				if vnav_alt_mode == 0 then
					if B738DR_flight_phase == 1 or B738DR_flight_phase == 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 4 or B738DR_flight_phase == 8 then
						if crz_alt_num ~= B738DR_mcp_alt_dial then
							crz_alt_num2 = B738DR_mcp_alt_dial
							-- if B738DR_flight_phase == 2 or B738DR_flight_phase == 8 then
							if B738DR_flight_phase == 1 or B738DR_flight_phase == 8 then
								
								first_alt_intv = 1
								
								if crz_alt_num2 > crz_alt_num then
									-- climb mode
									crz_alt_num = crz_alt_num2
									B738DR_fmc_cruise_alt = crz_alt_num
									n = crz_alt_num
									if n >= B738DR_trans_alt_mod then
										n = n / 100
										crz_alt = "FL" .. string.format("%03d", n)
									else
										crz_alt = string.format("%5d", n)
									end
									msg_unavaible_crz_alt = 0
									msg_chk_alt_constr = 0
									vnav_update = 1
									if page_perf == 1 then
										B738_calc_vnav_spd()
									end
									crz_alt_changed = 1
									altitude_last = elevation_ft
									B738DR_fms_descent_now = 0
									crz_alt_num2 = 0
									crz_alt_old = "     "
									crz_alt_changed = 1
									
									local altitude_actual_1 = simDR_altitude_pilot + 200
									local altitude_actual_2 = simDR_altitude_pilot - 200
									if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
										altitude_actual_1 = B738DR_preselected_alt
										altitude_actual_2 = altitude_actual_1
									end
									
									if crz_alt_num > altitude_actual_1 then
										if B738DR_mcp_alt_dial < altitude_actual_1 then
										-- if B738DR_mcp_alt_dial < crz_alt_num then
											if (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
												add_fmc_msg(CHECK_ALT_TGT, 1)
												vnav_alt_mode = 1
											end
										end
									elseif crz_alt_num < altitude_actual_2 then
										B738DR_flight_phase = 2
										if B738DR_mcp_alt_dial > altitude_actual_2 then
										-- if B738DR_mcp_alt_dial > crz_alt_num then
											if (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
												add_fmc_msg(CHECK_ALT_TGT, 1)
												vnav_alt_mode = 1
											else
												-- engage CRZ DES
												B738DR_fmc_cruise_alt = crz_alt_num
												simDR_ap_altitude_dial_ft = crz_alt_num
												if B738DR_mcp_alt_dial <= crz_alt_num then
													if B738DR_altitude_mode == 5 then
														if simDR_autopilot_altitude_mode ~= 4 then
															simDR_ap_vvi_dial = -1000
															B738DR_ap_vvi_dial = -1000
															simCMD_autopilot_vs_sel:once()
														end
													end
													B738DR_flight_phase = 4
													B738DR_fms_descent_now = 0
													vnav_alt_mode = 0
												else
													add_fmc_msg(CHECK_ALT_TGT, 1)
													vnav_alt_mode = 1
												end
											end
										end
									else	-- crz_exec == 9
										B738DR_flight_phase = 2
									end
								end
							elseif B738DR_flight_phase == 2 then
								if crz_alt_num2 > crz_alt_num then
									--crz_exec = 1	-- CRZ CLB
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
									end
									B738DR_flight_phase = 3
									crz_alt_num = crz_alt_num2
									B738DR_fmc_cruise_alt = crz_alt_num
									n = crz_alt_num
									if n >= B738DR_trans_alt_mod then
										n = n / 100
										crz_alt = "FL" .. string.format("%03d", n)
									else
										crz_alt = string.format("%5d", n)
									end
									crz_alt_num2 = 0
									crz_alt_old = "     "
									-- display Cruise page
									reset_fmc_pages()
									reset_fmc_pages_fo()
									page_cruise = 1
									page_cruise2 = 1
								else
									--crz_exec = 2	
									-- CRZ DES to new cruise altitude
									if B738DR_vnav_td_dist > 50 and crz_alt_num2 >= first_desc_rest then
										crz_alt_num = crz_alt_num2
										B738DR_fmc_cruise_alt = crz_alt_num
										n = crz_alt_num
										if n >= B738DR_trans_alt_mod then
											n = n / 100
											crz_alt = "FL" .. string.format("%03d", n)
										else
											crz_alt = string.format("%5d", n)
										end
										if simDR_autopilot_altitude_mode ~= 4 then
											simDR_ap_vvi_dial = -1000
											B738DR_ap_vvi_dial = -1000
											simCMD_autopilot_vs_sel:once()
										end
										B738DR_fms_descent_now = 0
										B738DR_flight_phase = 4
										-- display Cruise page
										reset_fmc_pages()
										reset_fmc_pages_fo()
										page_cruise = 1
										page_cruise2 = 1
									else
										if B738DR_fms_descent_now == 0 then
											B738DR_fms_descent_now = 2
											if simDR_autopilot_altitude_mode ~= 4 then
												simDR_ap_vvi_dial = 0	---1000
												B738DR_ap_vvi_dial = 0
												simCMD_autopilot_vs_sel:once()
											end
											B738DR_flight_phase = 5
											first_alt_intv = 1
										end
									end
									crz_alt_num2 = 0
									crz_alt_old = "     "
								end
								crz_alt_changed = 1
							elseif B738DR_flight_phase == 4 then
								if simDR_altitude_pilot > 5000 then
									B738DR_fms_descent_now = 2
									if simDR_autopilot_altitude_mode ~= 4 then
										simDR_ap_vvi_dial = 0	---1000
										B738DR_ap_vvi_dial = 0
										simCMD_autopilot_vs_sel:once()
									end
									B738DR_flight_phase = 5
									first_alt_intv = 1
								end
								crz_alt_num2 = 0
								crz_alt_old = "     "
							else
								n = crz_alt_num
								if n >= B738DR_trans_alt_mod then
									n = n / 100
									crz_alt_old = "FL" .. string.format("%03d", n)
								else
									crz_alt_old = string.format("%5d", n)
								end
								n = crz_alt_num2
								if n >= B738DR_trans_alt_mod then
									n = n / 100
									crz_alt = "FL" .. string.format("%03d", n)
								else
									crz_alt = string.format("%5d", n)
								end
								if crz_alt_num2 > simDR_altitude_pilot then
									crz_exec = 1	-- CRZ CLB
								else
									crz_exec = 2	-- CRZ DES
								end
								exec1_light = 1
								-- display Cruise page
								reset_fmc_pages()
								reset_fmc_pages_fo()
								page_cruise = 1
								page_cruise2 = 1
							end
						end
					end
					-- if B738DR_vnav_alt_disable ~= 0 and B738DR_alt_hold_vnav ~= 0 then
						-- --activate_vnav_mode
						-- B738DR_activate_pitch_mode = 5
					-- end
				else
					if (B738DR_flight_phase < 2 or B738DR_flight_phase == 8) and B738DR_mcp_alt_dial > (simDR_altitude_pilot + 300) then
						vnav_alt_mode = 0
						if simDR_autopilot_altitude_mode ~= 5 then
							simCMD_autopilot_lvl_chg:once()
						end
						if B738DR_mcp_alt_dial > crz_alt_num then
							crz_alt_num = B738DR_mcp_alt_dial
							B738DR_fmc_cruise_alt = crz_alt_num
							n = crz_alt_num
							if n >= B738DR_trans_alt_mod then
								n = n / 100
								crz_alt = "FL" .. string.format("%03d", n)
							else
								crz_alt = string.format("%5d", n)
							end
						end
						crz_alt_num2 = 0
						crz_alt_old = "     "
						crz_alt_changed = 1
					-- end
					elseif B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and B738DR_mcp_alt_dial < (simDR_altitude_pilot - 300) then
						vnav_alt_mode = 0
						B738DR_fms_descent_now = 3
						if simDR_autopilot_altitude_mode ~= 4 then
							simDR_ap_vvi_dial = -1000
							B738DR_ap_vvi_dial = -1000
							simCMD_autopilot_vs_sel:once()
						end
					-- end
					elseif B738DR_flight_phase == 2 and B738DR_mcp_alt_dial < (simDR_altitude_pilot - 300) then
						vnav_alt_mode = 0
						B738DR_fms_descent_now = 2
						if simDR_autopilot_altitude_mode ~= 4 then
							simDR_ap_vvi_dial = 0	---1000
							B738DR_ap_vvi_dial = 0
							simCMD_autopilot_vs_sel:once()
						end
						B738DR_flight_phase = 5
					end
				end
				
			end
			
			if first_alt_intv == 0 then
				
				if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
					if B738DR_mcp_alt_dial > crz_alt_num then
						crz_alt_num = B738DR_mcp_alt_dial
						B738DR_fmc_cruise_alt = crz_alt_num
						n = crz_alt_num
						if n >= B738DR_trans_alt_mod then
							n = n / 100
							crz_alt = "FL" .. string.format("%03d", n)
						else
							crz_alt = string.format("%5d", n)
						end
					end
					crz_alt_num2 = 0
					crz_alt_old = "     "
					crz_alt_changed = 1
				end
			end
			
				if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
					-- delete alt restricts to MCP alt
						end_idx = tc_idx - 1
						if end_idx >= offset and end_idx > 0 and end_idx <= legs_num then
							for n = offset, end_idx do
								-- find alt restrict
								delete_enabled2 = 0
								if legs_restr_alt_n > 0 then	-- if exist alt restricts
									for nn = 1, legs_restr_alt_n do
										if legs_restr_alt[nn][2] > end_idx then
											-- no alt restricts to T/C
											break
										end
										delete_enabled = 0
										-- if legs_restr_alt[nn][2] == n then
											-- if legs_restr_alt[nn][5] <= B738DR_mcp_alt_dial and legs_restr_alt[nn][4] == 41 then	-- Between
												-- delete_enabled = 1
											-- elseif legs_restr_alt[nn][3] <= B738DR_mcp_alt_dial then	-- alt restrict found
												-- delete_enabled = 1
											-- end
										-- end
										
										if legs_restr_alt[nn][2] == n then
											if legs_restr_alt[nn][5] < B738DR_mcp_alt_dial and legs_restr_alt[nn][4] == 41 then	-- Between
												delete_enabled = 1
											elseif legs_restr_alt[nn][3] < B738DR_mcp_alt_dial then	-- alt restrict found
												delete_enabled = 1
											end
										end
										
										if delete_enabled == 1 then
											delete_enabled2 = 1
											legs_data[n][5] = 0
											legs_data[n][6] = 0
											legs_data[n][41] = 0
											if legs_delete == 0 and n <= legs_num2 then
												legs_data2[n][5] = 0
												legs_data2[n][6] = 0
												legs_data2[n][41] = 0
											end
											break
										end
									end
								end
								if delete_enabled2 == 1 then
									fms_recalc = 1
									msg_unavaible_crz_alt = 0
									break
								end
							end
							vnav_update = 1
						end
					--end
				elseif B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and fms_descent_now_old > 1 then
					-- delete alt restricts to MCP alt
					if ed_found ~= 0 then
						end_idx = ed_found - 1
					else
						end_idx = legs_num
					end
					if end_idx >= offset then
						for n = offset, end_idx do
							-- find alt restrict
							delete_enabled2 = 0
							if legs_restr_alt_n > 0 then	-- if exist alt restricts
								for nn = 1, legs_restr_alt_n do
									if legs_restr_alt[nn][2] > end_idx then
										-- no alt restricts to END of ROUTE
										break
									end
									delete_enabled = 0
									
									-- if legs_restr_alt[nn][2] == n then
										-- if legs_restr_alt[nn][3] >= B738DR_mcp_alt_dial and legs_restr_alt[nn][4] == 41 then	-- Between
											-- delete_enabled = 1
										-- elseif legs_restr_alt[nn][5] >= B738DR_mcp_alt_dial and legs_restr_alt[nn][4] == 41 then	-- Between
											-- delete_enabled = 2
										-- elseif legs_restr_alt[nn][3] >= B738DR_mcp_alt_dial then	-- alt restrict found
											-- delete_enabled = 1
										-- end
									-- end
									
									if legs_restr_alt[nn][2] == n then
										if legs_restr_alt[nn][3] > B738DR_mcp_alt_dial and legs_restr_alt[nn][4] == 41 then	-- Between
											delete_enabled = 1
										elseif legs_restr_alt[nn][5] > B738DR_mcp_alt_dial and legs_restr_alt[nn][4] == 41 then	-- Between
											delete_enabled = 2
										elseif legs_restr_alt[nn][3] > B738DR_mcp_alt_dial then	-- alt restrict found
											delete_enabled = 1
										end
									end
									
									if delete_enabled == 1 then
										delete_enabled2 = 1
										legs_data[n][5] = 0
										legs_data[n][6] = 0
										legs_data[n][41] = 0
										if legs_delete == 0 and n <= legs_num2 then
											legs_data2[n][5] = 0
											legs_data2[n][6] = 0
											legs_data2[n][41] = 0
										end
										break
									-- elseif delete_enabled == 1 then
										-- delete_enabled2 = 1
										-- legs_data[n][6] = 43
										-- legs_data[n][41] = 0
										-- if legs_delete == 0 and n <= legs_num2 then
											-- legs_data2[n][6] = 43
											-- legs_data2[n][41] = 0
										-- end
										-- break
									end
								end
							end
							if delete_enabled2 == 1 then
								fms_recalc = 1
								msg_unavaible_crz_alt = 0
								break
							end
						end
						vnav_update = 1
					end
					
					if B738DR_autopilot_vnav_status == 1 and B738DR_mcp_alt_dial < (simDR_altitude_pilot - 300) then
						if simDR_altitude_pilot > 5000 then
							B738DR_fms_descent_now = 2
							simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
							B738DR_ignore_td_idx2 = 1
							if simDR_autopilot_altitude_mode ~= 4 then
								simCMD_autopilot_vs_sel:once()
								simDR_ap_vvi_dial = 0	-- -1000
								B738DR_ap_vvi_dial = 0
							end
						end
					end
				end
			-- end
		end
	elseif phase == 2 then
		B738DR_autopilot_alt_interv_pos = 0
	end
end








--*************************************************************************************--
--** 				                 FMC FIRST OFFICER                   	    	 **--
--*************************************************************************************--
--fmcsk
function B738_fmc2_A_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "A")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_B_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "B")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_C_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "C")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_D_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "D")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_E_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "E")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_F_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "F")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_G_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "G")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_H_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "H")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_I_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "I")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_J_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "J")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_K_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "K")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "L")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_M_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "M")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_N_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "N")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_O_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "O")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_P_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "P")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_Q_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "Q")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "R")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_S_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "S")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_T_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "T")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_U_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "U")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_V_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "V")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_W_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "W")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_X_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "X")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_Y_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "Y")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_Z_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "Z")
			end
			item_sel2 = 0
			item_sel_act2 = 0
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_SP_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. " ")
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc2_0_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "0")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_1_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "1")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_2_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "2")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_3_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "3")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_4_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "4")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_5_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "5")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_6_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "6")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_7_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "7")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_8_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "8")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_9_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "9")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_period_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. ".")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_minus_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				if string.sub(entry2, -1, -1) == "-" then
					if string.len(entry2) == 1 then
						entry2 = "+"
					else
						entry2 = string.sub(entry2, 1, -2) .. "+"
					end
				elseif string.sub(entry2, -1, -1) == "+" then
					if string.len(entry2) == 1 then
						entry2 = "-"
					else
						entry2 = string.sub(entry2, 1, -2) .. "-"
					end
				else
					entry2 = (entry2 .. "-")
				end
				display_update = 1
			elseif string.len(entry2) == 24 then
				if string.sub(entry2, -1, -1) == "-" then
					entry2 = string.sub(entry2, 1, -2) .. "+"
				elseif string.sub(entry2, -1, -1) == "+" then
					entry2 = string.sub(entry2, 1, -2) .. "-"
				end
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_slash_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if fmc2_lock == 0 then
			if string.len(entry2) < 24 then
				entry2 = (entry2 .. "/")
				display_update = 1
			end
			item_sel_via2 = 0
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- FMC BUTTON CLR (delete last)
function B738_fmc2_clr_CMDhandler(phase, duration)
	
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		local lenstr = string.len(entry2)
		if fmc_message_num ~= 0 then
			if string.sub(fmc_message[fmc_message_num], 1, 8) == HOLD_AT and rte12_sel_fo == 0 then
				new_hold_at = 0
			elseif string.sub(fmc_message[fmc_message_num], 1, 8) == HOLD_AT and rte12_sel_fo == 1 then
				new_hold_at2 = 0
			end
			fmc_message_num = fmc_message_num - 1
			display_update = 1
		else
			if lenstr == 1 then
				entry2 = ""
				display_update = 1
			elseif lenstr > 1 then
				if string.sub(entry2, 1, 1) == ">" then
					entry2 = ""
				elseif fmc1_msg_alt == 0 then
					if item_sel_act2 > 0 then
						if lenstr <= item_sel_len2 then
							item_sel2 = 0
							item_sel_act2 = 0
						elseif lenstr == item_sel_len2 + 1 then
							if item_sel_act2 ~= 0 then
								item_sel2 = item_sel_act2
							else
								item_sel2 = 0
							end
						end
					end
					entry2 = string.sub(entry2, 1, -2)
				end
				display_update = 1
			end
		end
		
		--item_sel2 = 0
		item_sel_via2 = 0
		
		--fmc1_msg_alt_rst = 0
		fmc1_msg_alt = 0
		
		fmc2_input_lag = 0
	elseif phase == 1 then
		if duration > 1 then
			entry2 = ""
			fmc_message_num = 0
		end
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
	
end

-- FMC BUTTON DEL (delete all)
function B738_fmc2_del_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if string.len(entry2) > 1 then
				if string.sub(entry2, 1, 1) == ">" then
					entry2 = ""
				else
					entry2 = ">DELETE"
				end
		else
			entry2 = ">DELETE"
		end
		item_sel2 = 0
		item_sel_via2 = 0
		item_sel_act2 = 0
		display_update = 1
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end


-- FMC BUTTON PREVIOUS and NEXT PAGE

function B738_fmc2_prev_page_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
			-- local act_page_bck = act_page2
			local rte2_corr = 0
			act_page2 = act_page2 - 1
			if act_page2 < 1 then
				act_page2 = max_page2
			end
			
			if B738DR_fmc_type ~= 0 and (page_climb2 ~= 0 or page_cruise2 ~= 0 or page_descent2 ~= 0) then
				page_eo_climb2 = 0
				page_eo_cruise2 = 0
				if page_climb2 ~= 0 then
					page_climb2 = 0
					page_descent2 = 1
				elseif page_cruise2 ~= 0 then
					page_cruise2 = 0
					page_climb2 = 1
				else
					page_descent2 = 0
					page_cruise2 = 1
				end
			else
				if page_init2 ~= 0 then
					page_init2 = act_page2
				elseif page_ident2 ~= 0 then
					page_ident2 = act_page2
				elseif page_takeoff2 ~= 0 then
					page_takeoff2 = act_page2
				elseif page_approach2 ~= 0 then
					page_approach2 = act_page2
				elseif page_perf2 ~= 0 then
					page_perf2 = act_page2
				elseif page_n1_limit2 ~= 0 then
					page_n1_limit2 = act_page2
				elseif page_pos_init2 ~= 0 then
					page_pos_init2 = act_page2
				elseif page_progress2 ~= 0 then
					page_progress2 = act_page2
				elseif page_legs2 == 1 then
					if B738DR_fo_map_mode == 3 then
						if B738DR_no_rte2 == 0 then
							rte2_corr = 1
						end
						if rte12_sel_fo == 0 then
							legs_step2 = offset_mod + (5 * (act_page2 - 1)) - rte2_corr
							if legs_data2[legs_step2][1] == "DISCONTINUITY" then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num2 or legs_step2 < offset_mod then
									legs_step2 = offset_mod
								end
								if legs_step2 > legs_num2 then
									legs_step2 = legs_num2
								end
							end
						else
							legs_step2 = offset_mod2 + (5 * (act_page2 - 1)) - rte2_corr
							if legs_data2[legs_step2][1] == "DISCONTINUITY" then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num8 or legs_step2 < offset_mod2 then
									legs_step2 = offset_mod2
								end
								if legs_step2 > legs_num8 then
									legs_step2 = legs_num8
								end
							end
						end
					end
				elseif page_offset2 ~= 0 then
					page_offset2 = act_page2
				elseif page_hold2 ~= 0 then
					if rte12_sel_fo == 0 then
						hold_offset = 0
						if hold_data_num2 > 0 then
							for ww = 1, hold_data_num2 do
								if hold_data2[ww] >= offset_mod then
									hold_offset = ww
									break
								end
							end
						end
						new_hold_act_page = 0
						max_page2 = hold_data_num2 - hold_offset + 1
					else
						hold_offset2 = 0
						if hold_data_num4 > 0 then
							for ww = 1, hold_data_num4 do
								if hold_data4[ww] >= offset_mod2 then
									hold_offset2 = ww
									break
								end
							end
						end
						new_hold_act_page2 = 0
						max_page2 = hold_data_num4 - hold_offset2 + 1
					end
					if act_page2 > max_page2 then
						act_page2 = max_page2
					end
					
				elseif page_ac2 ~= 0 then
					page_ac2 = act_page2
				elseif page_ac_atis2 ~= 0 then
					page_ac_atis2 = act_page2
				elseif page_atc2 ~= 0 then
					page_atc2 = act_page2
				end
			end
			
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc2_next_page_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
			-- local act_page_bck = act_page2
			local rte2_corr = 0
			act_page2 = act_page2 + 1
			if act_page2 > max_page2 then
				act_page2 = 1
			end
			
			if B738DR_fmc_type ~= 0 and (page_climb2 ~= 0 or page_cruise2 ~= 0 or page_descent2 ~= 0) then
				page_eo_climb2 = 0
				page_eo_cruise2 = 0
				if page_climb2 ~= 0 then
					page_climb2 = 0
					page_cruise2 = 1
					if engout_crz < 2 then
						engout_crz = 0
					end
				elseif page_cruise2 ~= 0 then
					page_cruise2 = 0
					page_descent2 = 1
				else
					page_descent2 = 0
					page_climb2 = 1
					if engout_clb < 2 then
						engout_clb = 0
					end
				end
			else
				if page_init2 ~= 0 then
					page_init2 = act_page2
				elseif page_ident2 ~= 0 then
					page_ident2 = act_page2
				elseif page_takeoff2 ~= 0 then
					page_takeoff2 = act_page2
				elseif page_approach2 ~= 0 then
					page_approach2 = act_page2
				elseif page_perf2 ~= 0 then
					page_perf2 = act_page2
				elseif page_n1_limit2 ~= 0 then
					page_n1_limit2 = act_page2
				elseif page_pos_init2 ~= 0 then
					page_pos_init2 = act_page2
				elseif page_progress2 ~= 0 then
					page_progress2 = act_page2
				elseif page_legs2 == 1 then
					if B738DR_fo_map_mode == 3 then
						if B738DR_no_rte2 == 0 then
							rte2_corr = 1
						end
						if rte12_sel_fo == 0 then
							legs_step2 = offset_mod + (5 * (act_page2 - 1)) - rte2_corr
							if legs_data2[legs_step2][1] == "DISCONTINUITY" then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num2 or legs_step2 < offset_mod then
									legs_step2 = offset_mod
								end
								if legs_step2 > legs_num2 then
									legs_step2 = legs_num2
								end
							end
						else
							legs_step2 = offset_mod2 + (5 * (act_page2 - 1)) - rte2_corr
							if legs_data2[legs_step2][1] == "DISCONTINUITY" then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num8 or legs_step2 < offset_mod2 then
									legs_step2 = offset_mod2
								end
								if legs_step2 > legs_num8 then
									legs_step2 = legs_num8
								end
							end
						end
					end
				elseif page_offset2 ~= 0 then
					page_offset2 = act_page2
				elseif page_hold2 ~= 0 then
					if rte12_sel_fo == 0 then
						hold_offset = 0
						if hold_data_num2 > 0 then
							for ww = 1, hold_data_num2 do
								if hold_data2[ww] >= offset_mod then
									hold_offset = ww
									break
								end
							end
						end
						new_hold_act_page = 0
						max_page2 = hold_data_num2 - hold_offset + 1
					else
						hold_offset2 = 0
						if hold_data_num4 > 0 then
							for ww = 1, hold_data_num4 do
								if hold_data4[ww] >= offset_mod2 then
									hold_offset2 = ww
									break
								end
							end
						end
						new_hold_act_page2 = 0
						max_page2 = hold_data_num4 - hold_offset + 1
					end
					if act_page2 > max_page2 then
						act_page2 = max_page2
					end
					
				elseif page_ac2 ~= 0 then
					page_ac2 = act_page2
				elseif page_ac_atis2 ~= 0 then
					page_ac_atis2 = act_page2
				elseif page_atc2 ~= 0 then
					page_atc2 = act_page2
				end
			end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end


-------------------
-- LSK/RSK buttons
-------------------

-- 1LSK
function B738_fmc2_1L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel_fo[1] ~= "" then
						if ref_sid2 == ref_sid_sel_fo[1] then
							if sid_list_num == 1 then
								ref_sid2 = "------"
								ref_sid_tns2 = "------"
								sel_ref_sid2 = ref_sid2
								sel_ref_sid_tns2 = ref_sid_tns2
								if sel_ref_rwy2 == "-----" then
									create_rnw_list(rte12_sel_fo)
								end
								create_sid_list(rte12_sel_fo)
								ref_sid_exec = 1
							else
								sel_ref_sid2 = ref_sid2
								if sel_ref_rwy2 == "-----" then
									create_rnw_list(rte12_sel_fo)
								end
								create_sid_tns_list(rte12_sel_fo)
							end
						else
							ref_sid2 = ref_sid_sel_fo[1]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						
						act_page2 = 1
					end
				else
					ref_sid2 = "------"
					ref_sid_tns2 = "------"
					sel_ref_sid2 = ref_sid2
					sel_ref_sid_tns2 = ref_sid_tns2
					if sel_ref_rwy2 == "-----" then
						create_rnw_list(rte12_sel_fo)
					end
					create_sid_list(rte12_sel_fo)
					act_page2 = 1
					ref_sid_exec = 1
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel_fo[1] ~= "" then
						if ref_sid8 == ref_sid_sel_fo[1] then
							if sid_list_num == 1 then
								ref_sid8 = "------"
								ref_sid_tns8 = "------"
								sel_ref_sid8 = ref_sid8
								sel_ref_sid_tns8 = ref_sid_tns8
								if sel_ref_rwy8 == "-----" then
									create_rnw_list(rte12_sel_fo)
								end
								create_sid_list(rte12_sel_fo)
								ref_sid_exec = 1
							else
								sel_ref_sid8 = ref_sid8
								if sel_ref_rwy8 == "-----" then
									create_rnw_list(rte12_sel_fo)
								end
								create_sid_tns_list(rte12_sel_fo)
							end
						else
							ref_sid8 = ref_sid_sel_fo[1]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						
						act_page2 = 1
					end
				else
					ref_sid8 = "------"
					ref_sid_tns8 = "------"
					sel_ref_sid8 = ref_sid8
					sel_ref_sid_tns8 = ref_sid_tns8
					if sel_ref_rwy8 == "-----" then
						create_rnw_list(rte12_sel_fo)
					end
					create_sid_list(rte12_sel_fo)
					act_page2 = 1
					ref_sid_exec = 1
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel_fo[1] ~= "" then
						if des_star2 == des_star_sel_fo[1] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star2 = des_star_sel_fo[1]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					des_star2 = "------"
					des_star_trans2 = "------"
					sel_des_star2 = des_star2
					sel_des_star_trans2 = des_star_trans2
					if sel_des_app2 == "------" then
						create_app_list(rte12_sel_fo)
					end
					create_star_list(rte12_sel_fo)
					act_page2 = 1
					des_star_exec = 1
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel_fo[1] ~= "" then
						if des_star8 == des_star_sel_fo[1] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star8 = des_star_sel_fo[1]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					des_star8 = "------"
					des_star_trans8 = "------"
					sel_des_star8 = des_star8
					sel_des_star_trans8 = des_star_trans8
					if sel_des_app8 == "------" then
						create_app_list(rte12_sel_fo)
					end
					create_star_list(rte12_sel_fo)
					act_page2 = 1
					des_star_exec = 1
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_dep_arr2 == 1 then
			-- local dep_arr_enable = false
			-- -- if legs_num2 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 1 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			-- if legs_num2 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
				-- if in_flight_mode == 0 and legs_data2[1][17] == 4 then
					-- dep_arr_enable = true
				-- elseif legs_data2[1][17] == 0 then
					-- dep_arr_enable = true
				-- end
			-- end
			-- if dep_arr_enable == true then
			if legs_num2 > 0 then
				-- SID departures
				if ref_icao_x ~= "----" and des_icao_x ~= "****" and ref_icao_x ~= "ORGN" then
					rte12_sel_fo = 0
					page_dep2 = 1
					page_dep_arr2 = 0
					arr_data_x = 0
					find_ref_rnw_data(1, 0)
					read_ref_list_icao(ref_icao_x, 0)
					
					sel_ref_rwy2 = "-----"
					sel_ref_sid2 = "------"
					sel_ref_sid_tns2 = "------"
					
					create_rnw_list(0)
					create_sid_list(0)
					create_sid_tns_list(0)
				end
			end
		elseif page_rte_init2 == 1 then --and in_flight_mode == 0 then	-- only on the groundthen
			if act_page2 == 1 then
				-- entry2 Ref airport ICAO
				local apt_ok = 0
				local xy = 0
				if entry2 == ">DELETE" then
					entry2 = INVALID_DELETE
				else
					if rte12_sel_fo == 0 then
						if string.len(entry2) == 4 then
							
							if ref_apt_exist(entry2, 0) then
								ref_icao_x = entry2
								ref_tns_alt_x = 0
								ref_tns_lvl_x = 0
								ref_icao_alt_x = 0
								trans_alt_mod = "-----"
								ref_gate_x = "-----"
								
								ref_rwy2 = "-----"
								ref_sid2 = "------"
								ref_sid_tns2 = "------"
								
								des_icao_x = "****"
								des_tns_alt_x = 0
								des_tns_lvl_x = 0
								des_icao_alt_x = 0
								trans_lvl_mod = "-----"
								
								des_app2 = "------"
								des_app_tns2 = "------"
								des_star2 = "------"
								des_star_trans2 = "------"
								
								co_route_x = "------------"
								if rte12_act >= 0 then
									flt_num_x = flt_num
								else
									flt_num_x = "--------"
								end
								ref_rwy3 = "-----"
								
								if rte12_act ~= 0 then
									ref_icao = "----"
									des_icao = "****"
									ref_gate = "-----"
									co_route = "------------"
									ref_rwy = "-----"
									ref_sid = "------"
									ref_sid_tns = "------"
									des_star = "------"
									des_star_trans = "------"
									des_app = "------"
									des_app_tns = "------"
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
									
									ref_icao_lat = 0
									ref_icao_lon = 0
									des_icao_lat = 0
									des_icao_lon = 0
									ref_tns_alt = 0
									des_tns_alt = 0
									des_tns_lvl = 0
									ref_icao_alt = 0
									des_icao_alt = 0
									arr_data = 0
									des_app_rw_only = 0
									trans_alt = "-----"
									trans_lvl = "-----"
								end
								
								if apt_exist(entry2) == true then
									ref_icao_lat_x = icao_latitude
									ref_icao_lon_x = icao_longitude
									ref_tns_alt_x = icao_tns_alt
									ref_tns_lvl_x = icao_tns_lvl
									ref_icao_alt_x = 0
									B738DR_find_icao = entry2
									B738DR_find_icao_cmd = 1
									if B738DR_find_icao_cmd == 2 then
										ref_icao_alt_x = B738DR_find_icao_height * 3.280839895
									end
								else
									ref_tns_alt_x = 0
									ref_tns_lvl_x = 0
								end
								if ref_tns_alt_x == 0 then
									trans_alt_mod = "-----"
								else
									trans_alt_mod = string.format("%5d", ref_tns_alt_x)
								end
								
								hold_data_num2 = 0
								
								-- REF ICAO
								legs_num2 = 0
								legs_data2[1] = {}
								legs_data2[1][1] = ref_icao_x
								legs_data2[1][2] = 0		-- brg
								legs_data2[1][3] = 0		-- distance
								legs_data2[1][4] = 0		-- speed
								legs_data2[1][5] = ref_icao_alt_x		-- altitude
								legs_data2[1][6] = 0	-- altitude type
								legs_data2[1][7] = ref_icao_lat_x		-- latitude
								legs_data2[1][8] = ref_icao_lon_x		-- longitude
								legs_data2[1][9] = ""			-- via id
								legs_data2[1][10] = 0		-- calc speed
								legs_data2[1][11] = 0		-- calc altitude
								legs_data2[1][12] = 0		-- calc altitude vnav pth
								legs_data2[1][13] = 0
								legs_data2[1][14] = 0		-- rest alt
								legs_data2[1][15] = 0		-- last fuel
								legs_data2[1][16] = ""
								legs_data2[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
								legs_data2[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
								legs_data2[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
								legs_data2[1][20] = 0
								legs_data2[1][21] = -1
								legs_data2[1][22] = ""
								legs_data2[1][23] = 0
								legs_data2[1][24] = 0
								legs_data2[1][25] = 0
								legs_data2[1][26] = 0
								legs_data2[1][27] = ""
								legs_data2[1][28] = ""
								legs_data2[1][29] = ""
								legs_data2[1][30] = ""
								legs_data2[1][31] = ""
								legs_data2[1][32] = 0
								legs_data2[1][33] = ""
								legs_data2[1][34] = ""
								legs_data2[1][35] = ""
								legs_data2[1][36] = 9
								legs_data2[1][37] = 0
								legs_data2[1][38] = ""
								legs_data2[1][39] = ""
								legs_data2[1][40] = 0
								legs_data2[1][41] = 0
								legs_data2[1][42] = 0
								legs_data2[1][43] = ""
								legs_data2[1][44] = 0
								legs_data2[1][45] = 0
								legs_data2[1][46] = 0
								legs_data2[1][47] = 0
								legs_data2[1][48] = ""
								legs_data2[1][49] = 0
								legs_data2[1][50] = 0
								legs_data2[1][51] = 0
								legs_data2[1][52] = 0
								legs_data2[1][53] = 0
								legs_data2[1][54] = 0
								offset_mod = 0
								
								arr_data_x = 0
								airport_pos(rte12_sel_fo)
								create_rnw_list(rte12_sel_fo)
								create_sid_list(rte12_sel_fo)
								
								if rte12_act == rte12_sel_fo then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
								end
								
								if legs_num ~= 0 then
									if rte12_act == rte12_sel_fo then
										legs_delete = 1
										ref_exec = 1
										des_exec = 0
									end
								end
								
								if exec_activate_fpln ~= 0 then
									rte_exec = 0
									exec_activate_fpln = 0
									
									copy_to_legs_data2_buf(0)
									if legs_num2_buf > 1 then
										for n = 1, (legs_num2_buf + 1) do
											legs_data2_buf[n][11] = 0
											legs_data2_buf[n][10] = 0
											legs_data2_buf[n][14] = 50000
											legs_data2_buf[n][38] = ""
										end
									end
									copy_from_legs_data2_buf(0)
									
								end
								
								find_ref_rnw_data(1, 0)
								entry2 = ""
							else
								add_fmc_msg(NOT_IN_DATABASE, 1)
							end
						elseif entry2 == "" and ref_icao_x ~= "----" and ref_icao_x ~= "ORGN" then
							entry2 = ref_icao_x
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						if string.len(entry2) == 4 then
							
							if ref_apt_exist(entry2, 1) then
								ref_icao7_x = entry2
								ref_tns_alt7_x = 0
								ref_tns_lvl7_x = 0
								ref_icao_alt7_x = 0
								trans_alt_mod2 = "-----"
								ref_gate7_x = "-----"
								
								ref_rwy8 = "-----"
								ref_sid8 = "------"
								ref_sid_tns8 = "------"
								
								des_icao7_x = "****"
								des_tns_alt7_x = 0
								des_tns_lvl7_x = 0
								des_icao_alt7_x = 0
								trans_lvl_mod2 = "-----"
								
								des_app8 = "------"
								des_app_tns8 = "------"
								des_star8 = "------"
								des_star_trans8 = "------"
								
								co_route7_x = "------------"
								if rte12_act >= 0 then
									flt_num_x = flt_num
								else
									flt_num_x = "--------"
								end
								
								ref_rwy9 = "-----"
								
								if rte12_act ~= 1 then
									ref_icao7 = "----"
									des_icao7 = "****"
									ref_gate7 = "-----"
									co_route7 = "------------"
									ref_rwy7 = "-----"
									ref_sid7 = "------"
									ref_sid_tns7 = "------"
									des_star7 = "------"
									des_star_trans7 = "------"
									des_app7 = "------"
									des_app_tns7 = "------"
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
									
									ref_icao_lat7 = 0
									ref_icao_lon7 = 0
									des_icao_lat7 = 0
									des_icao_lon7 = 0
									ref_tns_alt7 = 0
									des_tns_alt7 = 0
									des_tns_lvl7 = 0
									ref_icao_alt7 = 0
									des_icao_alt7 = 0
									arr_data7 = 0
									des_app_rw_only = 0
									trans_alt7 = "-----"
									trans_lvl7 = "-----"
								end
								
								if apt_exist(entry2) == true then
									ref_icao_lat7_x = icao_latitude
									ref_icao_lon7_x = icao_longitude
									ref_tns_alt7_x = icao_tns_alt
									ref_tns_lvl7_x = icao_tns_lvl
									ref_icao_alt7_x = 0
									B738DR_find_icao = entry2
									B738DR_find_icao_cmd = 1
									if B738DR_find_icao_cmd == 2 then
										ref_icao_alt7_x = B738DR_find_icao_height * 3.280839895
									end
								else
									ref_tns_alt7_x = 0
									ref_tns_lvl7_x = 0
								end
								if ref_tns_alt7_x == 0 then
									trans_alt_mod2 = "-----"
								else
									trans_alt_mod = string.format("%5d", ref_tns_alt7_x)
								end
								
								hold_data_num4 = 0
								
								-- REF ICAO
								legs_num8 = 0
								legs_data8[1] = {}
								legs_data8[1][1] = ref_icao7_x
								legs_data8[1][2] = 0		-- brg
								legs_data8[1][3] = 0		-- distance
								legs_data8[1][4] = 0		-- speed
								legs_data8[1][5] = ref_icao_alt7_x		-- altitude
								legs_data8[1][6] = 0	-- altitude type
								legs_data8[1][7] = ref_icao_lat7_x		-- latitude
								legs_data8[1][8] = ref_icao_lon7_x		-- longitude
								legs_data8[1][9] = ""			-- via id
								legs_data8[1][10] = 0		-- calc speed
								legs_data8[1][11] = 0		-- calc altitude
								legs_data8[1][12] = 0		-- calc altitude vnav pth
								legs_data8[1][13] = 0
								legs_data8[1][14] = 0		-- rest alt
								legs_data8[1][15] = 0		-- last fuel
								legs_data8[1][16] = ""
								legs_data8[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
								legs_data8[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
								legs_data8[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
								legs_data8[1][20] = 0
								legs_data8[1][21] = -1
								legs_data8[1][22] = ""
								legs_data8[1][23] = 0
								legs_data8[1][24] = 0
								legs_data8[1][25] = 0
								legs_data8[1][26] = 0
								legs_data8[1][27] = ""
								legs_data8[1][28] = ""
								legs_data8[1][29] = ""
								legs_data8[1][30] = ""
								legs_data8[1][31] = ""
								legs_data8[1][32] = 0
								legs_data8[1][33] = ""
								legs_data8[1][34] = ""
								legs_data8[1][35] = ""
								legs_data8[1][36] = 9
								legs_data8[1][37] = 0
								legs_data8[1][38] = ""
								legs_data8[1][39] = ""
								legs_data8[1][40] = 0
								legs_data8[1][41] = 0
								legs_data8[1][42] = 0
								legs_data8[1][43] = ""
								legs_data8[1][44] = 0
								legs_data8[1][45] = 0
								legs_data8[1][46] = 0
								legs_data8[1][47] = 0
								legs_data8[1][48] = ""
								legs_data8[1][49] = 0
								legs_data8[1][50] = 0
								legs_data8[1][51] = 0
								legs_data8[1][52] = 0
								legs_data8[1][53] = 0
								legs_data8[1][54] = 0
								offset_mod2 = 0
								
								arr_data7_x = 0
								airport_pos(rte12_sel_fo)
								create_rnw_list(rte12_sel_fo)
								create_sid_list(rte12_sel_fo)
								
								if rte12_act == rte12_sel_fo then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
								end
								
								if legs_num ~= 0 then
									if rte12_act == rte12_sel_fo then
										legs_delete = 1
										ref_exec2 = 1
										des_exec2 = 0
									end
								end
								
								if exec_activate_fpln2 ~= 0 then
									rte_exec2 = 0
									exec_activate_fpln2 = 0
									
									copy_to_legs_data2_buf(1)
									if legs_num2_buf > 1 then
										for n = 1, (legs_num2_buf + 1) do
											legs_data2_buf[n][11] = 0
											legs_data2_buf[n][10] = 0
											legs_data2_buf[n][14] = 50000
											legs_data2_buf[n][38] = ""
										end
									end
									copy_from_legs_data2_buf(1)
									
								end
								
								find_ref_rnw_data(1, 1)
								entry2 = ""
							else
								add_fmc_msg(NOT_IN_DATABASE, 1)
							end
						elseif entry2 == "" and ref_icao7_x ~= "----" and ref_icao7_x ~= "ORGN" then
							entry2 = ref_icao7_x
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				-- button 1 LSK
				rte_1_lsk(1, 1, rte12_sel_fo)
			end
		elseif page_sel_wpt_2 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2_2 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry2, 1, rte12_sel_fo)
			end
		elseif page_sel_wpt3_2 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4_2 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 1)
			end
		elseif page_sel_hold_wpt_2 == 1 then
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 1, rte12_sel_fo)
			end
		elseif page_ref_sel2 == 1 then
			
			local item = 0
			local button = 1	-- button 1 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				ref_nav_new = "*****"
				page_ref_nav_data2 = 1
				page_ref_sel2 = 0
			end
		elseif page_fix2 == 1 then
			local ii = 0
			local jj = 0
			local fix_idx = 0
			local fix_x_lat = 0
			local fix_x_lon = 0
			
			if entry2 == ">DELETE" then
				if act_page2 <= fix_data_num then
					if string.sub(fix_data[act_page2][1], 1, 2) == "RW" then
						fix_rnw_lat = 0
						fix_rnw_lon = 0
						fix_rnw_crs = 0
						fix_rnw_num = ""
					end
				end
				
				if fix_data_num == act_page2 then
					fix_abm_act[fix_data_num] = 0
					fix_abm_wpt_idx[fix_data_num] = -1
					fix_abm_wpt_dist[fix_data_num] = -1
					fix_abm_lat[fix_data_num] = -1
					fix_abm_lon[fix_data_num] = -1
					fix_abm_brg[fix_data_num] = -1
					fix_abm_dist[fix_data_num] = -1
					fix_abm_eta[fix_data_num] = -1
					fix_abm_alt[fix_data_num] = -1
					
					fix_data_num = fix_data_num - 1
					entry2 = ""
				elseif act_page2 < fix_data_num then
					for ii = act_page2, (fix_data_num - 1) do
						for jj = 1, 38 do
							fix_data[ii][jj] = fix_data[ii+1][jj]
						end
						fix_abm_act[ii] = fix_abm_act[ii+1]
						fix_abm_wpt_idx[ii] = fix_abm_wpt_idx[ii+1]
						fix_abm_wpt_dist[ii] = fix_abm_wpt_dist[ii+1]
						fix_abm_lat[ii] = fix_abm_lat[ii+1]
						fix_abm_lon[ii] = fix_abm_lon[ii+1]
						fix_abm_brg[ii] = fix_abm_brg[ii+1]
						fix_abm_dist[ii] = fix_abm_dist[ii+1]
						fix_abm_eta[ii] = fix_abm_eta[ii+1]
						fix_abm_alt[ii] = fix_abm_alt[ii+1]
					end
					fix_data_num = fix_data_num - 1
					entry2 = ""
				end
			elseif entry2 == "" then
				if act_page2 > 0 and act_page2 <= fix_data_num then
					entry2 = fix_data[act_page2][1]
				end
			elseif entry2 ~= "" then
				fix_idx = act_page2
				if item_sel2 > 0 then
					if rte12_act == 0 then
						if item_sel2 <= legs_num2 then
							if fix_idx > fix_data_num then
								fix_data_num = fix_data_num + 1
								fix_idx = fix_data_num
							else
								if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
									fix_rnw_lat = 0
									fix_rnw_lon = 0
									fix_rnw_crs = 0
									fix_rnw_num = ""
								end
							end
							fix_data[fix_idx] = {}
							fix_data[fix_idx][1] = legs_data2[item_sel2][1]
							fix_data[fix_idx][2] = legs_data2[item_sel2][36] -- navaid type: 1-VOR, 2-VOR TAG, 3-NDB, 4-WPT, 5-DME, 9-APT (legs_data2[item_sel2][1])
							if fix_data[fix_idx][2] == 0 then
								fix_data[fix_idx][2] = 3
							elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
								fix_data[fix_idx][2] = 3
							end
							-- fix_data[fix_idx][3] = legs_data2[item_sel2][7]
							-- fix_data[fix_idx][4] = legs_data2[item_sel2][8]
							fix_data[fix_idx][3], fix_data[fix_idx][4] = lat_lon_legs2(item_sel2)
							fix_data[fix_idx][5] = legs_data2[item_sel2][16]	-- reg code
							fix_data[fix_idx][6] = -1
							fix_data[fix_idx][7] = -1
							fix_data[fix_idx][8] = -1
							fix_data[fix_idx][9] = -1
							fix_data[fix_idx][10] = -1
							fix_data[fix_idx][11] = -1
							-- 1. radial
							fix_data[fix_idx][12] = 0	-- wpt before
							fix_data[fix_idx][13] = 0	-- dist before
							fix_data[fix_idx][14] = 0	-- lat
							fix_data[fix_idx][15] = 0	-- lon
							fix_data[fix_idx][16] = 0	-- dist calc
							fix_data[fix_idx][17] = 0	-- rad calc
							fix_data[fix_idx][18] = -1	-- eta calc
							fix_data[fix_idx][19] = 0	-- dtg calc
							fix_data[fix_idx][20] = -1	-- alt calc
							-- 2. radial
							fix_data[fix_idx][21] = 0	-- wpt before
							fix_data[fix_idx][22] = 0	-- dist before
							fix_data[fix_idx][23] = 0	-- lat
							fix_data[fix_idx][24] = 0	-- lon
							fix_data[fix_idx][25] = 0	-- dist calc
							fix_data[fix_idx][26] = 0	-- rad calc
							fix_data[fix_idx][27] = -1	-- eta calc
							fix_data[fix_idx][28] = 0	-- dtg calc
							fix_data[fix_idx][29] = -1	-- alt calc
							-- 3. radial
							fix_data[fix_idx][30] = 0	-- wpt before
							fix_data[fix_idx][31] = 0	-- dist before
							fix_data[fix_idx][32] = 0	-- lat
							fix_data[fix_idx][33] = 0	-- lon
							fix_data[fix_idx][34] = 0	-- dist calc
							fix_data[fix_idx][35] = 0	-- rad calc
							fix_data[fix_idx][36] = -1	-- eta calc
							fix_data[fix_idx][37] = 0	-- dtg calc
							fix_data[fix_idx][38] = -1	-- alt calc
							
							fix_abm_act[fix_idx] = 0
							fix_abm_wpt_idx[fix_idx] = -1
							fix_abm_wpt_dist[fix_idx] = -1
							fix_abm_lat[fix_idx] = -1
							fix_abm_lon[fix_idx] = -1
							fix_abm_brg[fix_idx] = -1
							fix_abm_dist[fix_idx] = -1
							fix_abm_eta[fix_idx] = -1
							fix_abm_alt[fix_idx] = -1
							
							item_sel2 = 0
							item_sel_act2 = 0
							entry2 = ""
						end
					elseif rte12_act == 0 then
						if item_sel2 <= legs_num8 then
							if fix_idx > fix_data_num then
								fix_data_num = fix_data_num + 1
								fix_idx = fix_data_num
							else
								if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
									fix_rnw_lat = 0
									fix_rnw_lon = 0
									fix_rnw_crs = 0
									fix_rnw_num = ""
								end
							end
							fix_data[fix_idx] = {}
							fix_data[fix_idx][1] = legs_data8[item_sel2][1]
							fix_data[fix_idx][2] = legs_data8[item_sel2][36] -- navaid type: 1-VOR, 2-VOR TAG, 3-NDB, 4-WPT, 5-DME, 9-APT (legs_data8[item_sel2][1])
							if fix_data[fix_idx][2] == 0 then
								fix_data[fix_idx][2] = 3
							elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
								fix_data[fix_idx][2] = 3
							end
							-- fix_data[fix_idx][3] = legs_data8[item_sel2][7]
							-- fix_data[fix_idx][4] = legs_data8[item_sel2][8]
							fix_data[fix_idx][3], fix_data[fix_idx][4] = lat_lon_legs8(item_sel2)
							fix_data[fix_idx][5] = legs_data8[item_sel2][16]	-- reg code
							fix_data[fix_idx][6] = -1
							fix_data[fix_idx][7] = -1
							fix_data[fix_idx][8] = -1
							fix_data[fix_idx][9] = -1
							fix_data[fix_idx][10] = -1
							fix_data[fix_idx][11] = -1
							-- 1. radial
							fix_data[fix_idx][12] = 0	-- wpt before
							fix_data[fix_idx][13] = 0	-- dist before
							fix_data[fix_idx][14] = 0	-- lat
							fix_data[fix_idx][15] = 0	-- lon
							fix_data[fix_idx][16] = 0	-- dist calc
							fix_data[fix_idx][17] = 0	-- rad calc
							fix_data[fix_idx][18] = -1	-- eta calc
							fix_data[fix_idx][19] = 0	-- dtg calc
							fix_data[fix_idx][20] = -1	-- alt calc
							-- 2. radial
							fix_data[fix_idx][21] = 0	-- wpt before
							fix_data[fix_idx][22] = 0	-- dist before
							fix_data[fix_idx][23] = 0	-- lat
							fix_data[fix_idx][24] = 0	-- lon
							fix_data[fix_idx][25] = 0	-- dist calc
							fix_data[fix_idx][26] = 0	-- rad calc
							fix_data[fix_idx][27] = -1	-- eta calc
							fix_data[fix_idx][28] = 0	-- dtg calc
							fix_data[fix_idx][29] = -1	-- alt calc
							-- 3. radial
							fix_data[fix_idx][30] = 0	-- wpt before
							fix_data[fix_idx][31] = 0	-- dist before
							fix_data[fix_idx][32] = 0	-- lat
							fix_data[fix_idx][33] = 0	-- lon
							fix_data[fix_idx][34] = 0	-- dist calc
							fix_data[fix_idx][35] = 0	-- rad calc
							fix_data[fix_idx][36] = -1	-- eta calc
							fix_data[fix_idx][37] = 0	-- dtg calc
							fix_data[fix_idx][38] = -1	-- alt calc
							
							fix_abm_act[fix_idx] = 0
							fix_abm_wpt_idx[fix_idx] = -1
							fix_abm_wpt_dist[fix_idx] = -1
							fix_abm_lat[fix_idx] = -1
							fix_abm_lon[fix_idx] = -1
							fix_abm_brg[fix_idx] = -1
							fix_abm_dist[fix_idx] = -1
							fix_abm_eta[fix_idx] = -1
							fix_abm_alt[fix_idx] = -1
							
							item_sel2 = 0
							item_sel_act2 = 0
							entry2 = ""
						end
					end
				elseif string.len(entry2) > 1 then
					local use_des_icao = "****"
					if rte12_act == 0 then
						use_des_icao = des_icao
					elseif rte12_act == 1 then
						use_des_icao = des_icao7
					end
					if string.sub(entry2, 1, 2) == "RW" and use_des_icao ~= "****" then
						-- find destination runway
						fix_x_lat = 0
						fix_x_lon = 0
						if rte12_act == 0 then
							if arr_data_x == 0 then
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_des_runway(entry2, rte12_act)
							else
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_ref_runway(entry2, rte12_act)
							end
						elseif rte12_act == 1 then
							if arr_data7_x == 0 then
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_des_runway(entry2, rte12_act)
							else
								fix_x_lat, fix_x_lon, fix_rnw_crs, fix_rnw_num = find_ref_runway(entry2, rte12_act)
							end
						end
						if fix_x_lat == 0 and fix_x_lon == 0 then
							add_fmc_msg(NOT_IN_DATABASE, 1)
						else
							if fix_idx > fix_data_num then
								fix_data_num = fix_data_num + 1
								fix_idx = fix_data_num
							end
							fix_data[fix_idx] = {}
							fix_data[fix_idx][1] = entry2
							fix_data[fix_idx][2] = 4	-- type
							fix_data[fix_idx][3] = fix_x_lat	-- lat
							fix_data[fix_idx][4] = fix_x_lon	-- lon
							fix_data[fix_idx][5] = ""	-- reg code
							fix_data[fix_idx][6] = -1	-- rad1
							fix_data[fix_idx][7] = -1	-- dist1
							fix_data[fix_idx][8] = -1	-- rad2
							fix_data[fix_idx][9] = -1	-- dist2
							fix_data[fix_idx][10] = -1	-- rad3
							fix_data[fix_idx][11] = -1	-- dist3
							-- 1. radial
							fix_data[fix_idx][12] = 0	-- wpt before
							fix_data[fix_idx][13] = 0	-- dist before
							fix_data[fix_idx][14] = 0	-- lat
							fix_data[fix_idx][15] = 0	-- lon
							fix_data[fix_idx][16] = 0	-- dist calc
							fix_data[fix_idx][17] = 0	-- rad calc
							fix_data[fix_idx][18] = -1	-- eta calc
							fix_data[fix_idx][19] = 0	-- dtg calc
							fix_data[fix_idx][20] = -1	-- alt calc
							-- 2. radial
							fix_data[fix_idx][21] = 0	-- wpt before
							fix_data[fix_idx][22] = 0	-- dist before
							fix_data[fix_idx][23] = 0	-- lat
							fix_data[fix_idx][24] = 0	-- lon
							fix_data[fix_idx][25] = 0	-- dist calc
							fix_data[fix_idx][26] = 0	-- rad calc
							fix_data[fix_idx][27] = -1	-- eta calc
							fix_data[fix_idx][28] = 0	-- dtg calc
							fix_data[fix_idx][29] = -1	-- alt calc
							-- 3. radial
							fix_data[fix_idx][30] = 0	-- wpt before
							fix_data[fix_idx][31] = 0	-- dist before
							fix_data[fix_idx][32] = 0	-- lat
							fix_data[fix_idx][33] = 0	-- lon
							fix_data[fix_idx][34] = 0	-- dist calc
							fix_data[fix_idx][35] = 0	-- rad calc
							fix_data[fix_idx][36] = -1	-- eta calc
							fix_data[fix_idx][37] = 0	-- dtg calc
							fix_data[fix_idx][38] = -1	-- alt calc
							
							fix_abm_act[fix_idx] = 0
							fix_abm_wpt_idx[fix_idx] = -1
							fix_abm_wpt_dist[fix_idx] = -1
							fix_abm_lat[fix_idx] = -1
							fix_abm_lon[fix_idx] = -1
							fix_abm_brg[fix_idx] = -1
							fix_abm_dist[fix_idx] = -1
							fix_abm_eta[fix_idx] = -1
							fix_abm_alt[fix_idx] = -1
							
							entry2 = ""
							-- find runway crs
							fix_rnw_lat = fix_x_lat
							fix_rnw_lon = fix_x_lon
						end
					else
						find_navaid(entry2, "", 0, "", rte12_act)
						if navaid_list_n == 0 then
							--entry2 = ""
							add_fmc_msg(NOT_IN_DATABASE, 1)
						else
							-- add new fix from sel desires
							if navaid_list_n == 1 then
								if fix_idx > fix_data_num then
									fix_data_num = fix_data_num + 1
									fix_idx = fix_data_num
								else
									if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
										fix_rnw_lat = 0
										fix_rnw_lon = 0
										fix_rnw_crs = 0
										fix_rnw_num = ""
									end
								end
								fix_data[fix_idx] = {}
								fix_data[fix_idx][1] = entry2
								fix_data[fix_idx][2] = navaid_list[1][1]	-- type
								if fix_data[fix_idx][2] == 0 then
									fix_data[fix_idx][2] = 3
								elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
									fix_data[fix_idx][2] = 3
								end
								fix_data[fix_idx][3] = navaid_list[1][2]	-- lat
								fix_data[fix_idx][4] = navaid_list[1][3]	-- lon
								fix_data[fix_idx][5] = navaid_list[1][8]	-- reg code
								fix_data[fix_idx][6] = -1	-- rad1
								fix_data[fix_idx][7] = -1	-- dist1
								fix_data[fix_idx][8] = -1	-- rad2
								fix_data[fix_idx][9] = -1	-- dist2
								fix_data[fix_idx][10] = -1	-- rad3
								fix_data[fix_idx][11] = -1	-- dist3
								-- 1. radial
								fix_data[fix_idx][12] = 0	-- wpt before
								fix_data[fix_idx][13] = 0	-- dist before
								fix_data[fix_idx][14] = 0	-- lat
								fix_data[fix_idx][15] = 0	-- lon
								fix_data[fix_idx][16] = 0	-- dist calc
								fix_data[fix_idx][17] = 0	-- rad calc
								fix_data[fix_idx][18] = -1	-- eta calc
								fix_data[fix_idx][19] = 0	-- dtg calc
								fix_data[fix_idx][20] = -1	-- alt calc
								-- 2. radial
								fix_data[fix_idx][21] = 0	-- wpt before
								fix_data[fix_idx][22] = 0	-- dist before
								fix_data[fix_idx][23] = 0	-- lat
								fix_data[fix_idx][24] = 0	-- lon
								fix_data[fix_idx][25] = 0	-- dist calc
								fix_data[fix_idx][26] = 0	-- rad calc
								fix_data[fix_idx][27] = -1	-- eta calc
								fix_data[fix_idx][28] = 0	-- dtg calc
								fix_data[fix_idx][29] = -1	-- alt calc
								-- 3. radial
								fix_data[fix_idx][30] = 0	-- wpt before
								fix_data[fix_idx][31] = 0	-- dist before
								fix_data[fix_idx][32] = 0	-- lat
								fix_data[fix_idx][33] = 0	-- lon
								fix_data[fix_idx][34] = 0	-- dist calc
								fix_data[fix_idx][35] = 0	-- rad calc
								fix_data[fix_idx][36] = -1	-- eta calc
								fix_data[fix_idx][37] = 0	-- dtg calc
								fix_data[fix_idx][38] = -1	-- alt calc
								
								fix_abm_act[fix_idx] = 0
								fix_abm_wpt_idx[fix_idx] = -1
								fix_abm_wpt_dist[fix_idx] = -1
								fix_abm_lat[fix_idx] = -1
								fix_abm_lon[fix_idx] = -1
								fix_abm_brg[fix_idx] = -1
								fix_abm_dist[fix_idx] = -1
								fix_abm_eta[fix_idx] = -1
								fix_abm_alt[fix_idx] = -1
								
							else
								--sel desired
								page_sel_wpt4_2 = 1
								fix_idx2 = fix_idx
								page_fix2 = 0
								act_page_old2 = act_page2
								act_page2 = 1
							end
							entry2 = ""
						end
					end
				else
					add_fmc_msg(NOT_IN_DATABASE, 1)
				end
			end
		elseif page_legs2 == 1 then
			legs_1_lsk(1, 1, rte12_sel_fo)
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			-- entry2 flaps
			if entry2 == "1" then
				flaps = " 1"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry2 = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry2 = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry2 == "5" then
				flaps = " 5"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry2 = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry2 = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry2 == "10" then
				flaps = "10"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry2 = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry2 = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry2 == "15" then
				flaps = "15"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry2 = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry2 = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry2 == "25" then
				flaps = "25"
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if v1_set == "---" and vr_set == "---" and v2_set == "---" then
						entry2 = ""
					else
						-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
						-- verify_to_spd = 1
						add_fmc_msg(TO_SPEEDS_DELETED, 1)
						verify_to_spd = 0
						v1_set = "---"
						vr_set = "---"
						v2_set = "---"
						qrh = "OFF"
						entry2 = ""
					end
				end
				B738DR_calc_vspd = 1
				B738DR_calc_trim = 1
			elseif entry2 ~= "" then
				if entry2 == ">DELETE" then
					flaps = "**"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							verify_to_spd = 0
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							qrh = "OFF"
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
			display_update = 1
		elseif page_init2 == 1 then
			-- go to Ident page
			page_ident2 = 1
			page_init2 = 0
			display_update = 1
		elseif page_menu2 == 1 then
			-- go to Ident page
			page_ident2 = 1
			page_menu2 = 0
		elseif page_pos_init2 == 2 then
			if fmc_pos == "-----.-------.-" then
				entry2 = ""
			else
				entry2 = fmc_pos
			end
		elseif page_perf2 == 1 and B738DR_gw_entry == 0 then
			-- entry2 GW
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen == 0 then
				gw_mod = gw_calc
				zfw_mod = zfw_calc
				if units == 0 then
					gw_lbs_mod = gw_mod
					gw_kgs_mod = string.format("%5.1f", (tonumber(gw_mod) / KGS_LBS))
					zfw_lbs_mod = zfw_mod
					zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))
				else
					gw_kgs_mod = gw_mod
					gw_lbs_mod = string.format("%5.1f", (tonumber(gw_mod) * KGS_LBS))
					zfw_kgs_mod = zfw_mod
					zfw_lbs_mod = string.format("%5.1f", (tonumber(zfw_mod) * KGS_LBS))
				end
				
				if perf_exec < 2 then
					gw = gw_mod
					gw_lbs = gw_lbs_mod
					gw_kgs = gw_kgs_mod
					zfw = zfw_mod
					zfw_lbs = zfw_lbs_mod
					zfw_kgs = zfw_kgs_mod
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
							qrh = "OFF"
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
				else
					perf_exec = 3
				end
				entry2 = ""
			else
				if entry2 == ">DELETE" then
					if perf_exec < 2 then
						gw_mod = "***.*"
						gw_lbs_mod = gw_mod
						gw_kgs_mod = gw_mod
						zfw_mod = "***.*"
						zfw_kgs_mod = zfw_mod
						zfw_lbs_mod = zfw_mod
						gw = gw_mod
						gw_lbs = gw_lbs_mod
						gw_kgs = gw_kgs_mod
						zfw = zfw_mod
						zfw_lbs = zfw_lbs_mod
						zfw_kgs = zfw_kgs_mod
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry2 = ""
							else
								add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								verify_to_spd = 1
								qrh = "OFF"
								entry2 = ""
							end
						end
						B738DR_calc_vspd = 1
						B738DR_calc_trim = 1
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry2 = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < weight_min or n > weight_max then	-- GW min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							gw_mod = string.format("%5.1f", n)
							if units == 0 then
								gw_lbs_mod = gw_mod
								gw_kgs_mod = string.format("%5.1f", (tonumber(gw_mod) / KGS_LBS))		-- to kgs
								local weight_lbs = (simDR_fuel_weight / 1000) * KGS_LBS		-- to lbs
								local xxx = tonumber(gw_lbs_mod)
								xxx = xxx - weight_lbs
								zfw_mod = string.format("%5.1f", (tonumber(xxx)))
								zfw_lbs_mod = zfw_mod
								zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))	-- to kgs
							else
								gw_kgs_mod = gw_mod
								gw_lbs_mod = string.format("%5.1f", (tonumber(gw_mod) * KGS_LBS))		-- to lbs
								local weight_lbs = (simDR_fuel_weight / 1000) * KGS_LBS		-- to lbs
								local xxx = tonumber(gw_lbs_mod)
								xxx = xxx - weight_lbs
								zfw_lbs_mod = string.format("%5.1f", (tonumber(xxx)))
								zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_lbs_mod) / KGS_LBS))	-- to kgs
								zfw_mod = zfw_kgs_mod
							end
							if perf_exec < 2 then
								gw = gw_mod
								gw_lbs = gw_lbs_mod
								gw_kgs = gw_kgs_mod
								zfw = zfw_mod
								zfw_lbs = zfw_lbs_mod
								zfw_kgs = zfw_kgs_mod
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if v1_set == "---" and vr_set == "---" and v2_set == "---" then
										entry2 = ""
									else
										add_fmc_msg(VERIFY_TO_SPEEDS, 1)
										verify_to_spd = 1
										qrh = "OFF"
										entry2 = ""
									end
								end
								B738DR_calc_vspd = 1
								B738DR_calc_trim = 1
							else
								perf_exec = 3
							end
							entry2 = ""
						end
					end
				end
			end
			B738_calc_vnav_spd()
			vnav_update = 1
		elseif page_approach2 == 1 then
			-- entry2 GW
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			local qqq = 0
			if strlen > 0 then
				if entry2 == ">DELETE" then
					entry2 = ""
					gw_app_nul = 0
					gw_app = gw
					gw_app_lbs = gw_lbs
					gw_app_kgs = gw_kgs
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < weight_min or n > weight_max then	-- GW min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							gw_app = string.format("%5.1f", n)
							if units == 0 then
								gw_app_lbs = gw_app
								gw_app_kgs = string.format("%5.1f", (tonumber(gw_app) / KGS_LBS))		-- to kgs
							else
								gw_app_kgs = gw_app
								gw_app_lbs = string.format("%5.1f", (tonumber(gw_app) * KGS_LBS))		-- to lbs
							end
							gw_app_nul = 1
							entry2 = ""
						end
					end
				end
			end
		
		elseif page_descent_forecast2 == 1 then
			-- entry2 Trans level
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			local nn = 0
			local n_str = ""
			if strlen > 0 then
				if entry2 == ">DELETE" then
					trans_lvl_mod = "-----"
					entry2 = ""
					forecast_exec = 1
				else
					if strlen == 3 or strlen == 2 then
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 30 or n > 410 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								trans_lvl_mod = "FL" .. string.format("%03d", n)
								entry2 = ""
								forecast_exec = 1
								nn = n * 100
								if forec_alt_1_mod ~= "-----" then
									n_str = string.sub(forec_alt_1_mod, 1, 2)
									if forec_alt_1_num_mod > nn then
										if n_str ~= "FL" then
											n = tonumber(forec_alt_1_mod) / 100
											forec_alt_1_mod = "FL" .. string.format("%03d", n)
										end
									else
										if n_str == "FL" then
											n_str = string.sub(forec_alt_1_mod, 3, 5)
											n = tonumber(n_str) * 100
											forec_alt_1_mod = string.format("%5d", n)
										end
									end
								end
								if forec_alt_2_mod ~= "-----" then
									n_str = string.sub(forec_alt_2_mod, 1, 2)
									if forec_alt_2_num_mod > nn then
										if n_str ~= "FL" then
											n = tonumber(forec_alt_2_mod) / 100
											forec_alt_2_mod = "FL" .. string.format("%03d", n)
										end
									else
										if n_str == "FL" then
											n_str = string.sub(forec_alt_2_mod, 3, 5)
											n = tonumber(n_str) * 100
											forec_alt_2_mod = string.format("%5d", n)
										end
									end
								end
								if forec_alt_3_mod ~= "-----" then
									n_str = string.sub(forec_alt_3_mod, 1, 2)
									if forec_alt_3_num_mod > nn then
										if n_str ~= "FL" then
											n = tonumber(forec_alt_3_mod) / 100
											forec_alt_3_mod = "FL" .. string.format("%03d", n)
										end
									else
										if n_str == "FL" then
											n_str = string.sub(forec_alt_3_mod, 3, 5)
											n = tonumber(n_str) * 100
											forec_alt_3_mod = string.format("%5d", n)
										end
									end
								end
							end
						end
					elseif strlen == 5 then
						if string.sub(entry2, 1, 2) == "FL" then
							n = tonumber(string.sub(entry2, 3, 5))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < 30 or n > 410 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									trans_lvl_mod = "FL" .. string.format("%03d", n)
									entry2 = ""
									forecast_exec = 1
									nn = n * 100
									if forec_alt_1_mod ~= "-----" then
										n_str = string.sub(forec_alt_1_mod, 1, 2)
										if forec_alt_1_num_mod > nn then
											if n_str ~= "FL" then
												n = tonumber(forec_alt_1_mod) / 100
												forec_alt_1_mod = "FL" .. string.format("%03d", n)
											end
										else
											if n_str == "FL" then
												n_str = string.sub(forec_alt_1_mod, 3, 5)
												n = tonumber(n_str) * 100
												forec_alt_1_mod = string.format("%5d", n)
											end
										end
									end
									if forec_alt_2_mod ~= "-----" then
										n_str = string.sub(forec_alt_2_mod, 1, 2)
										if forec_alt_2_num_mod > nn then
											if n_str ~= "FL" then
												n = tonumber(forec_alt_2_mod) / 100
												forec_alt_2_mod = "FL" .. string.format("%03d", n)
											end
										else
											if n_str == "FL" then
												n_str = string.sub(forec_alt_2_mod, 3, 5)
												n = tonumber(n_str) * 100
												forec_alt_2_mod = string.format("%5d", n)
											end
										end
									end
									if forec_alt_3_mod ~= "-----" then
										n_str = string.sub(forec_alt_3_mod, 1, 2)
										if forec_alt_3_num_mod > nn then
											if n_str ~= "FL" then
												n = tonumber(forec_alt_3_mod) / 100
												forec_alt_3_mod = "FL" .. string.format("%03d", n)
											end
										else
											if n_str == "FL" then
												n_str = string.sub(forec_alt_3_mod, 3, 5)
												n = tonumber(n_str) * 100
												forec_alt_3_mod = string.format("%5d", n)
											end
										end
									end
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			-- entry2 RW WIND
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					rw_wind_dir = "---"
					rw_wind_spd = "---"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						rw_wind_dir = string.format("%03d", q)
						rw_wind_spd = string.format("%3d", qq)
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry2 = ""
							else
								v1_set = "---"
								vr_set = "---"
								v2_set = "---"
								verify_to_spd = 0
								add_fmc_msg(TO_SPEEDS_DELETED, 1)
								entry2 = ""
							end
						end
						B738DR_calc_vspd = 1
					end
					-- if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry2, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry2, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- rw_wind_dir = wind_dir
										-- rw_wind_spd = string.format("%03d", n)
										-- if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											-- if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												-- entry2 = ""
											-- else
												-- -- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- -- verify_to_spd = 1
												-- v1_set = "---"
												-- vr_set = "---"
												-- v2_set = "---"
												-- verify_to_spd = 0
												-- add_fmc_msg(TO_SPEEDS_DELETED, 1)
												-- entry2 = ""
											-- end
										-- end
										-- B738DR_calc_vspd = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_perf2 == 2 then
			-- Time Error
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					time_err = "  "
					entry2 = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 5 or n > 30 then	-- Time error min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							time_err = string.format("%2d", n)
							entry2 = ""
						end
					end
				end
			end
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb < 2 then
				-- Change cruise alt
				local n = entry_altitude(entry2)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					if crz_alt == "*****" then
						crz_alt_num = n
						if crz_alt_num >= B738DR_trans_alt_mod then
							n = n / 100
							crz_alt = "FL" .. string.format("%03d", n)
						else
							crz_alt = string.format("%5d", n)
						end
						B738DR_fmc_cruise_alt = crz_alt_num
						entry2 = ""
						msg_unavaible_crz_alt = 0
						msg_chk_alt_constr = 0
						if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
							add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
						end
					else
						crz_alt_num2 = n
						crz_alt_old = crz_alt
						crz_exec = 9
						exec1_light = 1
						if crz_alt_num2 >= B738DR_trans_alt_mod then
							n = n / 100
							crz_alt = "FL" .. string.format("%03d", n)
						else
							crz_alt = string.format("%5d", n)
						end
						entry2 = ""
					end
				end
			end
		elseif page_cruise2 == 1 then
			-- Change cruise alt
			local n = entry_altitude(entry2)
			if n == 0 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				if crz_alt == "*****" then
					crz_alt_num = n
					if crz_alt_num >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					B738DR_fmc_cruise_alt = crz_alt_num
					entry2 = ""
					msg_unavaible_crz_alt = 0
					msg_chk_alt_constr = 0
					crz_alt_num2 = 0
					crz_alt_old = "     "
					crz_alt_changed = 1
					if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
						add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
					end
					altitude_last = elevation_ft
					B738DR_fms_descent_now = 0
				else
					crz_alt_num2 = n
					crz_alt_old = crz_alt
					crz_exec = 9
					exec1_light = 1
					if crz_alt_num2 >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					entry2 = ""
				end
			end
		elseif page_n1_limit2 == 1 then
			if in_flight_mode == 0 then
				-- entry2 SEL TEMP and OAT
				local strlen = string.len(entry2)
				local n = 0
				if strlen == 0 then
					-- autofill OAT
					n = math.floor(simDR_OAT + 0.5)
					if n < 0 then
						oat = string.format("%4d", n)
					else
						if n < 10 then
							oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
						else
							oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
						end
					end
					n = (n * 9 / 5) + 32
					oat_f = string.format("%4d", n)
					oat_unit = "`C"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							verify_to_spd = 0
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							qrh = "OFF"
							entry2 = ""
						end
					else
						entry2 = ""
					end
					v1_set = "---"
					vr_set = "---"
					v2_set = "---"
					B738DR_calc_vspd = 1
				elseif strlen > 0 then
					if entry2 == ">DELETE" then
						sel_temp = "----"
						oat = "****"
						sel_temp_f = "----"
						oat_f = "****"
						oat_unit = "`C"
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry2 = ""
							else
								-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								-- verify_to_spd = 1
								add_fmc_msg(TO_SPEEDS_DELETED, 1)
								verify_to_spd = 0
								v1_set = "---"
								vr_set = "---"
								v2_set = "---"
								qrh = "OFF"
								entry2 = ""
							end
						end
						B738DR_calc_vspd = 1
						if to_2 == "<ACT>" then
							clb = "     "
							clb_1 = "<SEL>"
							clb_2 = "     "
						else
							clb = "<SEL>"
							clb_1 = "     "
							clb_2 = "     "
						end
					else
						if strlen > 0 and strlen < 6 then
							local oat_set = 0
							if string.sub(entry2, 1, 1) == "/" and strlen > 1 then
								oat_set = 1		-- entry2 OAT
								entry2 = string.sub(entry2, 2, strlen)
								strlen = strlen -1
							end
							
							local n_str = string.sub(entry2, strlen, strlen)
							if n_str == "C" then 
								n = tonumber(string.sub(entry2, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
											clb = "     "
											clb_1 = "     "
											clb_2 = "<SEL>"
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry2 = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry2 = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							elseif n_str == "F" then
								n = tonumber(string.sub(entry2, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 50 or n > 167) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 140) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`F"
										if oat_set == 0 then
											sel_temp_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											clb = "     "
											clb_1 = "     "
											clb_2 = "<SEL>"
										else
											oat_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry2 = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry2 = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							else
								n = tonumber(string.sub(entry2, 1, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set == 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
											clb = "     "
											clb_1 = "     "
											clb_2 = "<SEL>"
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry2 = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry2 = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							end
							
							
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			else
				-- select AUTO
				auto_act = "<ACT>"
				ga_act = "     "
				con_act = "     "
				clb_act = "     "
				crz_act = "     "
			end
		elseif page_ref_nav_data2 == 1 then
			local strlen = string.len(entry2)
			if strlen == 0 or strlen > 5 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				nav_data_type = 0 -- wpt
				nav_data_find(entry2, 1)
			end
		elseif page_ref_nav_data_apt2 == 1 then
			local nd_x = 0
			local idx_rec = 0
			local ii = 0
			local entry_len = string.len(entry2)
			if entry_len > 0 then
				if entry_len > 3 then
					if string.sub(entry2, 1, 2) == "RW" then
						entry2 = string.sub(entry2, 3, -1)
						if rnw_data_num > 0 and string.len(ref_nav_apt) > 0 then
							nd_x = string.byte(string.sub(ref_nav_apt, 1, 1))
							if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
								if idx_rnw[nd_x][99999] > 0 then
									for ii = 1, idx_rnw[nd_x][99999] do
										idx_rec = idx_rnw[nd_x][ii]
										if ref_nav_apt == rnw_data[idx_rec][1] and entry2 == rnw_data[idx_rec][2] then
											ref_nav_rnw = "RW" .. entry2
											ref_elevation = 0
											ref_nav_rnw_length = rnw_data[idx_rec][7]
											entry2 = ""
										end
									end
								end
							end
						end
					end
				end
				if entry2 ~= "" then
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_ac2 == 1 then
			-- go to ACARS ATIS
			page_ac_atis2 = 1
			page_ac2 = 0
			display_update = 1
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				entry2 = nearest_apt_list_icao[1]
				if entry2 == "----" then
					entry2 = ""
				end
			else
				entry2 = nearest_apt_list_icao[act_page2 - 1]
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				add_altn_apt(entry2, 1, 1)
			else
				add_altn_apt(entry2, act_page2 - 1, 1)
			end
		elseif page_dl_requests2 == 1 then
			page_dl_requests2 = 0
			page_dl_wxr_req2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_flt_plns2 == 1 then
			entry2 = sel_flt_plns2[1]
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 2LSK
function B738_fmc2_2L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel_fo[2] ~= "" then
						if ref_sid2 == ref_sid_sel_fo[2] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid2 = ref_sid_sel_fo[2]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel_fo[2] ~= "" then
							if ref_sid_tns2 == ref_tns_sel_fo[2] then
								if sid_tns_list_num == 1 then
									ref_sid_tns2 = "------"
									sel_ref_sid_tns2 = ref_sid_tns2
									act_page2 = 1
									ref_tns_exec = 1
								else
									sel_ref_sid_tns2 = ref_sid_tns2
								end
							else
								ref_sid_tns2 = ref_tns_sel_fo[2]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					else
						ref_sid_tns2 = "------"
						sel_ref_sid_tns2 = ref_sid_tns2
						act_page2 = 1
						ref_tns_exec = 1
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel_fo[2] ~= "" then
						if ref_sid8 == ref_sid_sel_fo[2] then
							sel_ref_sid8 = ref_sid2
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid8 = ref_sid_sel_fo[2]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel_fo[2] ~= "" then
							if ref_sid_tns8 == ref_tns_sel_fo[2] then
								if sid_tns_list_num == 1 then
									ref_sid_tns8 = "------"
									sel_ref_sid_tns8 = ref_sid_tns8
									act_page2 = 1
									ref_tns_exec = 1
								else
									sel_ref_sid_tns8 = ref_sid_tns8
								end
							else
								ref_sid_tns8 = ref_tns_sel_fo[2]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					else
						ref_sid_tns8 = "------"
						sel_ref_sid_tns8 = ref_sid_tns8
						act_page2 = 1
						ref_tns_exec = 1
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel_fo[2] ~= "" then
						if des_star2 == des_star_sel_fo[2] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star2 = des_star_sel_fo[2]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel_fo[2] ~= "" then
							if des_star_trans2 == des_star_tns_sel_fo[2] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel_fo[2]
								sel_des_star_trans2 = des_star_trans2
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					else
						des_star_trans2 = "------"
						sel_des_star_trans2 = des_star_trans2
						act_page2 = 1
						des_star_tns_exec = 1
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel_fo[2] ~= "" then
						if des_star8 == des_star_sel_fo[2] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star8 = des_star_sel_fo[2]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel_fo[2] ~= "" then
							if des_star_trans8 == des_star_tns_sel_fo[2] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans2 = des_star_tns_sel_fo[2]
								sel_des_star_trans8 = des_star_trans8
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					else
						des_star_trans8 = "------"
						sel_des_star_trans8 = des_star_trans8
						act_page2 = 1
						des_star_tns_exec = 1
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_sel_wpt_2 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2_2 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry2, 1, rte12_sel_fo)
			end
		elseif page_sel_wpt3_2 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4_2 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 1)
			end
		elseif page_sel_hold_wpt_2 == 1 then
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 1, rte12_sel_fo)
			end
		elseif page_ref_sel2 == 1 then
			
			local item = 0
			local button = 2	-- button 2 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				--ref_nav_wpt = nav_data_entry
				ref_nav_new = "*****"
				page_ref_nav_data2 = 1
				page_ref_sel2 = 0
			end
		elseif page_fix2 == 1 then
			local ii = 0
			local jj = string.len(entry2)
			local kk = 0
			local radial_temp = 0
			local data_ok = 0
			if act_page2 <= fix_data_num then
				if jj == 0 then
					-- copy wpt to clipboard
					if fix_data[act_page2][6] ~= -1 and fix_data[act_page2][7] ~= -1 then
						entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_data[act_page2][6]) .. "/"
						if fix_data[act_page2][7] < 10 then
							entry2 = entry2 .. string.format("%3.1f", fix_data[act_page2][7])
						elseif fix_data[act_page2][7] < 100 then
							entry2 = entry2 .. string.format("%4.1f", fix_data[act_page2][7])
						else
							entry2 = entry2 .. string.format("%5.1f", fix_data[act_page2][7])
						end
					elseif fix_data[act_page2][12] > 0 then
						if fix_data[act_page2][6] ~= -1 then
							entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_data[act_page2][6]) .. "/"
							if fix_data[act_page2][16] < 10 then
								entry2 = entry2 .. string.format("%3.1f", fix_data[act_page2][16])
							elseif fix_data[act_page2][16] < 100 then
								entry2 = entry2 .. string.format("%4.1f", fix_data[act_page2][16])
							else
								entry2 = entry2 .. string.format("%5.1f", fix_data[act_page2][16])
							end
						elseif fix_data[act_page2][7] ~= -1 then
							--
						end
					end
				elseif entry2 == ">DELETE" then
					if fix_data[act_page2][6] ~= -1 or fix_data[act_page2][7] ~= -1 then
						fix_data[act_page2][6] = -1
						fix_data[act_page2][7] = -1
						fix_data[act_page2][12] = -1
						entry2 = ""
					end
				elseif jj > 1 then
					if string.sub(entry2, 1, 1) == "/" then
						-- distance
						ii = tonumber(string.sub(entry2, 2, -1))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii <= 0 or ii > 511 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fix_data[act_page2][7] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page2][6] = -1
								fix_data[act_page2][12] = -1
								entry2 = ""
							end
						end
					elseif string.sub(entry2, -1, -1) == "/" then
						-- radial
						ii = tonumber(string.sub(entry2, 1, -2))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii < 0 or ii > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								ii = ii % 360
								fix_data[act_page2][6] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page2][7] = -1
								fix_data[act_page2][12] = -1
								entry2 = ""
							end
						end
					else
						--fixsk
						kk,ii = string.find(entry2, "/")
						if kk ~= nil and kk > 1 and kk < jj then
							radial_temp = tonumber(string.sub(entry2, 1, kk - 1))
							if radial_temp == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									data_ok = 1
								end
								if data_ok == 1 then
									ii = tonumber(string.sub(entry2, kk + 1, -1))
									if ii == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if ii <= 0 or ii > 511 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fix_data[act_page2][6] = tonumber(string.format("%5.1f", radial_temp))
											fix_data[act_page2][7] = tonumber(string.format("%5.1f", ii))
											fix_data[act_page2][12] = -1
											entry2 = ""
										end
									end
								end
							end
						else
							radial_temp = tonumber(entry2)
							if jj == 3 and radial_temp ~= nil then
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									fix_data[act_page][6] = tonumber(string.format("%5.1f", radial_temp))
									fix_data[act_page][7] = -1
									fix_data[act_page][12] = -1
									entry2 = ""
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs2 == 1 then
			-- 2 LSK
			legs_x_lsk(2, 1, rte12_sel_fo)
		elseif page_pos_init2 == 1 and in_flight_mode == 0 then	-- only on the ground
			-- entry2 Ref airport ICAO
			local apt_ok = 0
			local xy = 0
			if entry2 == ">DELETE" then
				entry2 = INVALID_DELETE
			else
				if string.len(entry2) == 4 then
					
					if apt_exist(entry2) == true then
					
					if cust_def_data == 1 then
						file_name = "Custom Data/CIFP/" .. entry2
						file_name = file_name .. ".dat"
						file_navdata = io.open(file_name, "r")
						if file_navdata == nil then
							if apt_exist(entry2) == true then
								apt_ok = 1
							end
						else
							file_navdata:close()
							apt_ok = 1
						end
					elseif cust_def_data == 2 then
						file_name = "Resources/default data/CIFP/" .. entry2
						file_name = file_name .. ".dat"
						file_navdata = io.open(file_name, "r")
						if file_navdata == nil then
							if apt_exist(entry2) == true then
								apt_ok = 1
							end
						else
							file_navdata:close()
							apt_ok = 1
						end
					end
					
					end
					
					if apt_ok == 0 then
						add_fmc_msg(NOT_IN_DATABASE, 1)
					else
						ref_icao2 = entry2
						airport_pos2(rte12_sel_fo)
						entry2 = ""
					end
				elseif entry2 == "" and ref_icao2 ~= "----" then
					entry2 = ref_icao2
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 then
				-- entry2 CO ROUTE
				local ii = 0
				if (save_rte1 ~= 0 and rte12_sel_fo == 0) or (save_rte2 ~= 0 and rte12_sel_fo == 1) then
					ii = string.len(entry2)
					if ii > 0 and ii < 13 then
						if rte12_sel_fo == 0 then
							co_route_save1 = entry2
						else
							co_route_save2 = entry2
						end
						entry2 = ""
					else
						add_fmc_msg(NOT_IN_DATABASE, 1)
					end
				elseif entry2 == ">DELETE" then
					if rte12_sel_fo == 0 then
						co_route_x = "------------"
						co_route = co_route_x
					else
						co_route7_x = "------------"
						co_route7 = co_route7_x
					end
					entry2 = ""
					if rte12_sel_fo == 0 then
						wind_data_clb_num = 0
						wind_data_toc_num = 0
						wind_data_tod_num = 0
						wind_data_des_num = 0
					else
						wind_data_clb_num7 = 0
						wind_data_toc_num7 = 0
						wind_data_tod_num7 = 0
						wind_data_des_num7 = 0
					end
				else
					ii = string.len(entry2)
					if ii > 0 and ii < 13 then
						ii = load_fpln(entry2, rte12_sel_fo)
						if ii ~= 0 then
							entry2 = ""
						end
					else
						add_fmc_msg(NOT_IN_DATABASE, 1)
					end
				end
			else
				-- button 2 LSK
				rte_x_lsk(2, 1, rte12_sel_fo)
			end
		elseif page_ref_nav_data2 == 1 then
			local strlen = string.len(entry2)
			if strlen ~= 4 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				nav_data_type = 2 -- apt
				nav_data_find(entry2, 1)
			end
		elseif page_init2 == 1 then
			-- go to Pos init page
			page_init2 = 0
			page_pos_init2 = 1
			display_update = 1
		elseif page_pos_init2 == 2 then
			if B738DR_irs_pos == "-----.-------.-" then
				entry2 = ""
			else
				entry2 = B738DR_irs_pos
			end
		elseif page_perf2 == 1 then
			if B738DR_plan_fuel ~= 0 then
				-- if simDR_flaps_ratio <= 0.625 and was_on_air == 0 and simDR_engine_on[0] == 0 and simDR_engine_on[1] == 0 then
				-- if simDR_engine_on[0] == 0 and simDR_engine_on[1] == 0 and was_on_air == 0 then
				if simDR_eng_N2[0] < 50 and simDR_eng_N2[1] < 50 and was_on_air == 0 then
					-- entry2 Plan weight
					local strlen = string.len(entry2)
					local n = tonumber(entry2)
					if strlen > 0 then
						if entry2 == ">DELETE" then
							plan_weight_mod = "---.-"
							plan_weight_lbs_mod = plan_weight_mod
							plan_weight_kgs_mod = plan_weight_mod
							if perf_exec < 2 then
								plan_weight = plan_weight_mod
								plan_weight_kgs = plan_weight_kgs_mod
								plan_weight_lbs = plan_weight_lbs_mod
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if v1_set == "---" and vr_set == "---" and v2_set == "---" then
										entry2 = ""
									else
										add_fmc_msg(VERIFY_TO_SPEEDS, 1)
										verify_to_spd = 1
										qrh = "OFF"
										entry2 = ""
									end
								end
							else
								perf_exec = 3
								entry = ""
							end
						else
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if units == 0 then
									if n < 0 or n > 45.1 then	-- Plan min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										plan_weight_mod = string.format("%5.1f", n)
										plan_weight_lbs_mod = plan_weight_mod
										plan_weight_kgs_mod = string.format("%5.1f", (tonumber(plan_weight_mod) / KGS_LBS))
										if perf_exec < 2 then
											plan_weight = plan_weight_mod
											plan_weight_kgs = plan_weight_kgs_mod
											plan_weight_lbs = plan_weight_lbs_mod
											if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
												if v1_set == "---" and vr_set == "---" and v2_set == "---" then
													entry2 = ""
												else
													add_fmc_msg(VERIFY_TO_SPEEDS, 1)
													verify_to_spd = 1
													qrh = "OFF"
													entry2 = ""
												end
											end
										else
											perf_exec = 3
											entry = ""
										end
									end
								else
									if n < 0 or n > 20.5 then	-- Plan min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										plan_weight_mod = string.format("%5.1f", n)
										plan_weight_kgs_mod = plan_weight_mod
										plan_weight_lbs_mod = string.format("%5.1f", (tonumber(plan_weight_mod) * KGS_LBS))
										if perf_exec < 2 then
											plan_weight = plan_weight_mod
											plan_weight_kgs = plan_weight_kgs_mod
											plan_weight_lbs = plan_weight_lbs_mod
											if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
												if v1_set == "---" and vr_set == "---" and v2_set == "---" then
													entry2 = ""
												else
													add_fmc_msg(VERIFY_TO_SPEEDS, 1)
													verify_to_spd = 1
													qrh = "OFF"
													entry2 = ""
												end
											end
										else
											perf_exec = 3
											entry = ""
										end
									end
								end
							end
						end
					end
				end
			end
		elseif page_n1_limit2 == 1 then
			if in_flight_mode == 0 then
				--if B738DR_thr_takeoff_N1 ~= 0 then
				if B738DR_fmc_oat_temp ~= 99 then
					-- select TO
					to = "<ACT>"
					to_1 = "     "
					to_2 = "     "
					--if sel_clb_thr == 0 then
					if sel_temp == "----" then
						clb = "<SEL>"
						clb_1 = "     "
						clb_2 = "     "
					else
						clb = "     "
						clb_1 = "     "
						clb_2 = "<SEL>"
					end
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
				end
			else
				-- select GA
				auto_act = "     "
				ga_act = "<ACT>"
				con_act = "     "
				clb_act = "     "
				crz_act = "     "
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			-- entry2 RW SLOPE / HDG
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					rw_slope = "--.-"
					rw_hdg = "---"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
				else
					if strlen == 4 then
						if string.sub(entry2, 1, 1) == "/" then
							local n = tonumber(string.sub(entry2, 2, 4))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < 0 or n > 360 then	-- HDG min and max
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n = n % 360
									rw_hdg = string.format("%03d", n)
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										if v1_set == "---" and vr_set == "---" and v2_set == "---" then
											entry2 = ""
										else
											-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
											-- verify_to_spd = 1
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
											entry2 = ""
										end
									end
									B738DR_calc_vspd = 1
								end
							end
						elseif string.sub(entry2, 3, 3) == "." then
							local up_down = string.sub(entry2, 1, 1)
							if up_down == "D" or up_down == "U" then
								local n = tonumber(string.sub(entry2, 2, 4))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									rw_slope = up_down .. string.format("%03.1f", n)
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										if v1_set == "---" and vr_set == "---" and v2_set == "---" then
											entry2 = ""
										else
											-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
											-- verify_to_spd = 1
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
											entry2 = ""
										end
									end
									B738DR_calc_vspd = 1
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					elseif strlen == 8 then
						if string.sub(entry2, 3, 3) == "." and string.sub(entry2, 5, 5) == "/" then
							local up_down = string.sub(entry2, 1, 1)
							if up_down == "D" or up_down == "U" then
								local n = tonumber(string.sub(entry2, 2, 4))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									local rw_slope_temp = up_down .. string.format("%03.1f", n)
									
									-- HDG
									n = tonumber(string.sub(entry2, 6, 8))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if n < 0 or n > 360 then	-- HDG min and max
											add_fmc_msg(INVALID_INPUT, 1)
										else
											rw_slope = rw_slope_temp
											n = n % 360
											rw_hdg = string.format("%03d", n)
											if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
												if v1_set == "---" and vr_set == "---" and v2_set == "---" then
													entry2 = ""
												else
													-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
													-- verify_to_spd = 1
													v1_set = "---"
													vr_set = "---"
													v2_set = "---"
													verify_to_spd = 0
													add_fmc_msg(TO_SPEEDS_DELETED, 1)
													entry2 = ""
												end
											end
											B738DR_calc_vspd = 1
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_descent2 == 1 then	--and B738DR_flight_phase < 5 then
			-- DES speed kts/mach
			local strlen = string.len(entry2)
			local n = 0
			local nn = 0
			local nnn = 0
			local n2 = 0
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					entry2 = ""
				else
					if strlen == 4 and string.sub(entry2, 1, 1) == "/" then		-- only kts
						n = tonumber(string.sub(entry2, 2, -1))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_kts)
							nnn = tonumber(des_min_kts)
							if nn == nil then
								nn = 340
							end
							if nnn == nil then
								nnn = 100		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fmc_descent_mode_mod = 2
								des_spd_exec = 1
								entry2 = ""
								--B738DR_fmc_descent_speed = n
								fmc_descent_speed_mod = n
							end
						end
					elseif strlen > 1 and strlen < 5 and string.sub(entry2, 1, 1) == "." then		-- only mach
						n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_mach)
							nnn = tonumber(des_min_mach)
							if nn == nil then
								nn = 0.820		-- max
							else
								nn = nn / 1000
							end
							if nnn == nil then
								nnn = 0.400		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--B738DR_fmc_descent_speed_mach = n
								fmc_descent_speed_mach_mod = n
								entry2 = ""
								fmc_descent_mode_mod = 2
								des_spd_exec = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_kts)
							nnn = tonumber(des_min_kts)
							if nn == nil then
								nn = 340
							end
							if nnn == nil then
								nnn = 100		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fmc_descent_mode_mod = 2
								des_spd_exec = 1
								entry2 = ""
								--B738DR_fmc_descent_speed = n
								fmc_descent_speed_mod = n
							end
						end
					elseif strlen == 5 and string.sub(entry2, 1, 1) == "." and string.sub(entry2, -1, -1) == "/" then	-- only mach
						n = tonumber(string.sub(entry2, 1, -2))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nn = tonumber(des_max_mach)
							nnn = tonumber(des_min_mach)
							if nn == nil then
								nn = 0.820		-- max
							else
								nn = nn / 1000
							end
							if nnn == nil then
								nnn = 0.400		-- min
							end
							if n < nnn or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--B738DR_fmc_descent_speed_mach = n
								fmc_descent_speed_mach_mod = n
								entry2 = ""
								fmc_descent_mode_mod = 2
								des_spd_exec = 1
							end
						end
					elseif strlen > 5 then		-- mach/kts .xxx/xxx, .xx/xxx, .x/xxx
						if string.sub(entry2, -4, -4) == "/" then
							n = tonumber(string.sub(entry2, -3, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_kts)
								nnn = tonumber(des_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n2 = n
									n = tonumber(string.sub(entry2, 1, -5))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(des_max_mach)
										nnn = tonumber(des_min_mach)
										if nn == nil then
											nn = 0.820		-- max
										else
											nn = nn / 1000
										end
										if nnn == nil then
											nnn = 0.400		-- min
										end
										if n < nnn or n > nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											--B738DR_fmc_descent_speed_mach = n
											--B738DR_fmc_descent_speed = n2
											fmc_descent_speed_mach_mod = n
											fmc_descent_speed_mod = n2
											fmc_descent_mode_mod = 2
											des_spd_exec = 1
											entry2 = ""
										end
									end
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb < 2 then
				-- CLB speed kts/mach
				local strlen = string.len(entry2)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry2 == ">DELETE" then
						entry2 = ""
					else
						if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
							n = tonumber(string.sub(entry2, 2, strlen))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									--B738DR_fmc_climb_speed_mach = n
									fmc_climb_speed_mach_mod = n
									entry2 = ""
									fmc_climb_mode_mod = 3
									clb_spd_exec = 1
								end
							end
						elseif strlen > 1 and strlen < 5 and string.sub(entry2, 1, 1) == "." then		-- only mach
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									--B738DR_fmc_climb_speed_mach = n
									fmc_climb_speed_mach_mod = n
									entry2 = ""
									fmc_climb_mode_mod = 3
									clb_spd_exec = 1
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									--B738DR_fmc_climb_speed = n
									fmc_climb_speed_mod = n
									entry2 = ""
									fmc_climb_mode_mod = 3
									clb_spd_exec = 1
								end
							end
						elseif strlen == 4 and string.sub(entry2, -1, -1) == "/" then			-- only kts
							n = tonumber(string.sub(entry2, 1, -2))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									--B738DR_fmc_climb_speed = n
									fmc_climb_speed_mod = n
									entry2 = ""
									fmc_climb_mode_mod = 3
									clb_spd_exec = 1
								end
							end
						elseif strlen > 5 then		-- kts/mach xxx/.xxx
							if string.sub(entry2, 4, 5) == "/." then
								n = tonumber(string.sub(entry2, 1, 3))
								if n == nil then
								else
									nn = tonumber(clb_max_kts)
									nnn = tonumber(clb_min_kts)
									if nn == nil then
										nn = 340
									end
									if nnn == nil then
										nnn = 100		-- min
									end
									if n < nnn or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n2 = n
										n = tonumber(string.sub(entry2, 5, -1))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											nn = tonumber(clb_max_mach)
											nnn = tonumber(clb_min_mach)
											if nn == nil then
												nn = 0.820		-- max
											else
												nn = nn / 1000
											end
											if nnn == nil then
												nnn = 0.400		-- min
											end
											if n < nnn or n > nn then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												--B738DR_fmc_climb_speed_mach = n
												--B738DR_fmc_climb_speed = n2
												fmc_climb_speed_mod = n2
												fmc_climb_speed_mach_mod = n
												fmc_climb_mode_mod = 3
												clb_spd_exec = 1
												entry2 = ""
											end
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
		elseif page_cruise2 == 1 then
			-- CRZ speed kts/mach
			if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 or (crz_exec == 0 and B738DR_flight_phase == 2) then
				local strlen = string.len(entry2)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry2 == ">DELETE" then
						entry2 = ""
					else
						if strlen > 1 and  strlen < 5 and string.sub(entry2, 1, 1) == "." then		-- only mach
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(crz_max_mach)
								nnn = tonumber(crz_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								else
									nnn = nnn / 1000
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_cruise_speed_mach_mod = n
									fmc_cruise_speed_mod = 0
									entry2 = ""
									crz_spd_exec = 1
									fmc_cruise_mode_mod = 2
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(crz_max_kts)
								nnn = tonumber(crz_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_cruise_speed_mod = n
									fmc_cruise_speed_mach_mod = 0
									entry2 = ""
									crz_spd_exec = 1
									fmc_cruise_mode_mod = 2
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			elseif B738DR_flight_phase == 3 then
				-- CRZ CLB speed kts/mach
				local strlen = string.len(entry2)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry2 == ">DELETE" then
						entry2 = ""
					else
						if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
							n = tonumber(string.sub(entry2, 2, strlen))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mach_mod = n
									entry2 = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen > 1 and strlen < 5 and string.sub(entry2, 1, 1) == "." then		-- only mach
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_mach)
								nnn = tonumber(clb_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mach_mod = n
									entry2 = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mod = n
									entry2 = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen == 4 and string.sub(entry2, -1, -1) == "/" then			-- only kts
							n = tonumber(string.sub(entry2, 1, -2))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(clb_max_kts)
								nnn = tonumber(clb_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_climb_speed_mod = n
									entry2 = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen > 5 then		-- kts/mach xxx/.xxx
							if string.sub(entry2, 4, 5) == "/." then
								n = tonumber(string.sub(entry2, 1, 3))
								if n == nil then
								else
									nn = tonumber(clb_max_kts)
									nnn = tonumber(clb_min_kts)
									if nn == nil then
										nn = 340
									end
									if nnn == nil then
										nnn = 100		-- min
									end
									if n < nnn or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n2 = n
										n = tonumber(string.sub(entry2, 5, -1))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											nn = tonumber(clb_max_mach)
											nnn = tonumber(clb_min_mach)
											if nn == nil then
												nn = 0.820		-- max
											else
												nn = nn / 1000
											end
											if nnn == nil then
												nnn = 0.400		-- min
											end
											if n < nnn or n > nn then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												crz_spd_exec = 1
												fmc_climb_speed_mach_mod = n
												fmc_climb_speed_mod = n2
												entry2 = ""
											end
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			elseif B738DR_flight_phase == 4 then
				-- DES speed kts/mach
				local strlen = string.len(entry2)
				local n = 0
				local nn = 0
				local nnn = 0
				local n2 = 0
				if strlen == 0 then
					 add_fmc_msg(INVALID_INPUT, 1)
				else
					if entry2 == ">DELETE" then
						entry2 = ""
					else
						if strlen == 4 and string.sub(entry2, 1, 1) == "/" then		-- only kts
							n = tonumber(string.sub(entry2, 2, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_kts)
								nnn = tonumber(des_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									entry2 = ""
									fmc_descent_speed_mod = n
									crz_spd_exec = 1
								end
							end
						elseif strlen > 1 and strlen < 5 and string.sub(entry2, 1, 1) == "." then		-- only mach
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_mach)
								nnn = tonumber(des_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_descent_speed_mach_mod = n
									entry2 = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen == 3 then			-- only kts
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_kts)
								nnn = tonumber(des_min_kts)
								if nn == nil then
									nn = 340
								end
								if nnn == nil then
									nnn = 100		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									crz_spd_exec = 1
									entry2 = ""
									fmc_descent_speed_mod = n
								end
							end
						elseif strlen == 5 and string.sub(entry2, 1, 1) == "." and string.sub(entry2, -1, -1) == "/" then	-- only mach
							n = tonumber(string.sub(entry2, 1, -2))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								nn = tonumber(des_max_mach)
								nnn = tonumber(des_min_mach)
								if nn == nil then
									nn = 0.820		-- max
								else
									nn = nn / 1000
								end
								if nnn == nil then
									nnn = 0.400		-- min
								end
								if n < nnn or n > nn then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									fmc_descent_speed_mach_mod = n
									entry2 = ""
									crz_spd_exec = 1
								end
							end
						elseif strlen > 5 then		-- mach/kts .xxx/xxx, .xx/xxx, .x/xxx
							if string.sub(entry2, -4, -4) == "/" then
								n = tonumber(string.sub(entry2, -3, -1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nn = tonumber(des_max_kts)
									nnn = tonumber(des_min_kts)
									if nn == nil then
										nn = 340
									end
									if nnn == nil then
										nnn = 100		-- min
									end
									if n < nnn or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n2 = n
										n = tonumber(string.sub(entry2, 1, -5))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											nn = tonumber(des_max_mach)
											nnn = tonumber(des_min_mach)
											if nn == nil then
												nn = 0.820		-- max
											else
												nn = nn / 1000
											end
											if nnn == nil then
												nnn = 0.400		-- min
											end
											if n < nnn or n > nn then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												fmc_descent_speed_mach_mod = n
												fmc_descent_speed_mod = n2
												crz_spd_exec = 1
												entry2 = ""
											end
										end
									end
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
		elseif page_perf2 == 2 then
			-- CLB min speed kts/mach
			local strlen = string.len(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					clb_min_kts = "   "
					clb_min_mach = "   "
					entry2 = ""
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry2, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_max_mach)
							if nn == nil then
								nn = 0.82
							else
								nn = nn / 1000
							end
							if n < 0.4 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								clb_min_mach = string.format("%03d", n)
								entry2 = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_min_kts = string.format("%03d", n)
								entry2 = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry2, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry2, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
								n = tonumber(string.sub(entry2, 5, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nn = tonumber(clb_max_mach)
									if nn == nil then
										nn = 0.82
									else
										nn = nn / 1000
									end
									if n < 0.4 or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n = n * 1000
										clb_min_kts = kts
										clb_min_mach = string.format("%03d", n)
										entry2 = ""
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		-----------
		elseif page_offset2 == 1 and legs_num > 1 and legs_intdir == 0 then
			local strlen = string.len(entry2)
			local n = 0
			if entry2 == ">DELETE" then
				if offset_dist_mod == 0 then
					entry2 = INVALID_DELETE
				else
					entry2 = ""
					if offset_act >= 5 and offset_act <= 7 then
						offset_act_mod = 4
						offset_dist_mod = 0
					else	--if offset_act == 0 then
						offset_act_mod = 0
						offset_dist_mod = 0
						offset_side_mod = 0
						offset_start_mod = 0
						offset_end_mod = 0
						offset_start_def_mod = 0
						offset_end_def_mod = 0
						-- legs_off_tmp_n = 0
						delete_offset_legs2(rte12_act)
					end
				end
			elseif entry2 == "0" then
					entry2 = ""
					if offset_act >= 5 and offset_act <= 7 then
						offset_act_mod = 4
						offset_dist_mod = 0
					else	--if offset_act == 0 then
						offset_act_mod = 0
						offset_dist_mod = 0
						offset_side_mod = 0
						offset_start_mod = 0
						offset_end_mod = 0
						offset_start_def_mod = 0
						offset_end_def_mod = 0
						-- legs_off_tmp_n = 0
						delete_offset_legs2(rte12_act)
					end
			elseif strlen > 1 and strlen < 6 then
				if string.sub(entry2, 1, 1) == "L" then
					offset_side_mod = 1
					n = tonumber(string.sub(entry2, 2, -1))
				elseif string.sub(entry2, -1, -1) == "L" then
					offset_side_mod = 1
					n = tonumber(string.sub(entry2, 1, -2))
				elseif string.sub(entry2, 1, 1) == "R" then
					offset_side_mod = 2
					n = tonumber(string.sub(entry2, 2, -1))
				elseif string.sub(entry2, -1, -1) == "R" then
					offset_side_mod = 2
					n = tonumber(string.sub(entry2, 1, -2))
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
				if offset_side_mod ~= 0 then
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
						offset_side_mod = 0
					else
						if n > 0 and n < 50 then
							offset_dist_mod = n
							entry2 = ""
							offset_act_mod = 1
							offset_check(rte12_act)
							if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
								if offset_create(rte12_act) == true then
									-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
										-- offset_act_mod = 3
									-- else
										-- offset_act_mod = 2
									-- end
									if rte12_act == 0 then
										if offset_start_mod == 0 then
											if legs_data2[offset][45] ~= 0 then
												offset_act_mod = 3
											else
												offset_act_mod = 1
											end
										else
											offset_act_mod = 2
										end
									else
										if offset_start_mod == 0 then
											if legs_data8[offset][45] ~= 0 then
												offset_act_mod = 3
											else
												offset_act_mod = 1
											end
										else
											offset_act_mod = 2
										end
									end
								end
							else
								add_fmc_msg(UNABLE_OFFSET, 1)
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
							offset_side_mod = 0
						end
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_menu2 == 1 then
			-- go to ACARS
			-- page_ac2 = 1
			page_dl_menu2 = 1
			page_menu2 = 0
			act_page2 = 1
			display_update = 1
		elseif page_ac_atis2 == 1 then
			-- entry ICAO ATIS
			if entry == ">DELETE" then
				ac_atis_icao = "----"
				entry2 = ""
			elseif entry2 == "" and ac_atis_icao ~= "----" then
				entry2 = ac_atis_icao
			elseif string.len(entry2) == 4 then
				ac_atis_icao = entry2
				entry2 = ""
				-- atis_msg_status = 0
				-- atis_send_time = "     "
			end
		elseif page_descent_forecast2 == 1 then
			-- entry2 Cabin Rate
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					cabin_rate_mod = "---"
					entry2 = ""
					forecast_exec = 1
				elseif strlen == 3 then
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 100 or n > 999 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							cabin_rate_mod = string.format("%3d", n)
							entry2 = ""
							forecast_exec = 1
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				entry2 = nearest_apt_list_icao[2]
				if entry2 == "----" then
					entry2 = ""
				end
			else
				if nearest_apt_list_via[act_page2 - 1] == 0 then
					if entry2 == ">DELETE" then
						nearest_apt_list_dist[act_page2 - 1] = 0
						entry2 = ""
					else
						local nnn = tonumber(entry2)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_dist[act_page2 - 1] = nnn
							entry2 = ""
						end
					end
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				add_altn_apt(entry2, 2, 1)
			else
				if altern_apt_list_via[act_page2 - 1] == 0 then
					if entry2 == ">DELETE" then
						altern_apt_list_dist[act_page2 - 1] = 0
						entry2 = ""
					else
						local nnn = tonumber(entry2)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_dist[act_page2 - 1] = nnn
							entry2 = ""
						end
					end
				end
			end
		elseif page_hold2 == 1 then
			local strlen = string.len(entry2)
			local m = 0
			local n = 0
			if entry2 == ">DELETE" then
				add_fmc_msg(INVALID_INPUT, 1)
			elseif strlen == 3 then
				-- format XXX
				n = tonumber(entry2)
				if n == nil then
					add_fmc_msg(INVALID_INPUT, 1)
				elseif n >= 0 and n <= 360 then
					m = (n + 180) % 360
					-- save data
					if rte12_sel_fo == 0 then
						new_hold_inbd = string.format("%03d", m)
						if new_hold_inbd == "---" then
							legs_data2[hold_offset_idx2][29] = "000"
						else
							legs_data2[hold_offset_idx2][29] = tostring(tonumber(new_hold_inbd) * 10)
						end
						if rte12_act == rte12_sel_fo then
							hold_exec = 1
						end
					else
						new_hold_inbd2 = string.format("%03d", m)
						if new_hold_inbd2 == "---" then
							legs_data8[hold_offset_idx8][29] = "000"
						else
							legs_data8[hold_offset_idx8][29] = tostring(tonumber(new_hold_inbd2) * 10)
						end
						if rte12_act == rte12_sel_fo then
							hold_exec2 = 1
						end
					end
					entry2 = ""
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_flt_plns2 == 1 then
			entry2 = sel_flt_plns2[2]
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 3LSK
function B738_fmc2_3L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel_fo[3] ~= "" then
						if ref_sid2 == ref_sid_sel_fo[3] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid2 = ref_sid_sel_fo[3]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel_fo[3] ~= "" then
							if ref_sid_tns2 == ref_tns_sel_fo[3] then
								sel_ref_sid_tns2 = ref_sid_tns2
							else
								ref_sid_tns2 = ref_tns_sel_fo[3]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel_fo[3] ~= "" then
						if ref_sid8 == ref_sid_sel_fo[3] then
							sel_ref_sid8 = ref_sid8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid8 = ref_sid_sel_fo[3]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel_fo[3] ~= "" then
							if ref_sid_tns8 == ref_tns_sel_fo[3] then
								sel_ref_sid_tns8 = ref_sid_tns8
							else
								ref_sid_tns8 = ref_tns_sel_fo[3]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel_fo[3] ~= "" then
						if des_star2 == des_star_sel_fo[3] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star2 = des_star_sel_fo[3]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel_fo[3] ~= "" then
							if des_star_trans2 == des_star_tns_sel_fo[3] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel_fo[3]
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel_fo[3] ~= "" then
						if des_star8 == des_star_sel_fo[3] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star8 = des_star_sel_fo[3]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel_fo[3] ~= "" then
							if des_star_trans8 == des_star_tns_sel_fo[3] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans8 = des_star_tns_sel_fo[3]
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_dep_arr2 == 1 then
			-- local dep_arr_enable = false
			-- -- if legs_num8 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 1 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
			-- if legs_num8 > 0 and (B738DR_flight_phase == 0 or B738DR_flight_phase == 8) and simDR_radio_height_pilot_ft < 2500 then
				-- if in_flight_mode == 0 and legs_data8[1][17] == 4 then
					-- dep_arr_enable = true
				-- elseif legs_data8[1][17] == 0 then
					-- dep_arr_enable = true
				-- end
			-- end
			-- if dep_arr_enable == true then
			if legs_num8 > 0 then
				-- SID departures
				if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and ref_icao7_x ~= "ORGN" then
					rte12_sel_fo = 1
					page_dep2 = 1
					page_dep_arr2 = 0
					arr_data7_x = 0
					find_ref_rnw_data(1, 1)
					read_ref_list_icao(ref_icao7_x, 1)
					
					sel_ref_rwy8 = "-----"
					sel_ref_sid8 = "------"
					sel_ref_sid_tns8 = "------"
					
					create_rnw_list(1)
					create_sid_list(1)
					create_sid_tns_list(1)
				end
			end
		elseif page_sel_wpt_2 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2_2 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry2, 1, rte12_sel_fo)
			end
		elseif page_sel_wpt3_2 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4_2 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 1)
			end
		elseif page_sel_hold_wpt_2 == 1 then
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 1, rte12_sel_fo)
			end
		elseif page_ref_sel2 == 1 then
			
			local item = 0
			local button = 3	-- button 3 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				ref_nav_new = "*****"
				page_ref_nav_data2 = 1
				page_ref_sel2 = 0
			end
		elseif page_hold2 == 1 then
			local strlen = string.len(entry2)
			local m = 0
			local n = 0
			local ww = 0
			if entry2 == ">DELETE" then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				if strlen == 5 then
					-- format XXX/X
					if string.sub(entry2, 4, 4) ~= "/" then
						ww = 1
					end
					m = tonumber(string.sub(entry2, 1, 3))
					if m == nil then
						ww = 1
					else
						if m < 0 and m > 360 then
							ww = 1
						end
					end
					if string.sub(entry2, 5, 5) ~= "L" and string.sub(entry2, 5, 5) ~= "R" then
						ww = 1
					end
					if ww == 0 then
						m = m % 360
						
						-- save data
						if rte12_sel_fo == 0 then
							new_hold_inbd = string.format("%03d", m)
							if string.sub(entry2, 5, 5) == "L" then
								new_hold_turn = 0
							else
								new_hold_turn = 1
							end
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx2][21] = 1
							else
								legs_data2[hold_offset_idx2][21] = new_hold_turn
							end
							if new_hold_inbd == "---" then
								legs_data2[hold_offset_idx2][29] = "000"
							else
								legs_data2[hold_offset_idx2][29] = tostring(tonumber(new_hold_inbd) * 10)
							end
							if rte12_act == rte12_sel_fo then
								hold_exec = 1
							end
						else
							new_hold_inbd2 = string.format("%03d", m)
							if string.sub(entry2, 5, 5) == "L" then
								new_hold_turn2 = 0
							else
								new_hold_turn2 = 1
							end
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx8][21] = 1
							else
								legs_data8[hold_offset_idx8][21] = new_hold_turn2
							end
							if new_hold_inbd2 == "---" then
								legs_data8[hold_offset_idx8][29] = "000"
							else
								legs_data8[hold_offset_idx8][29] = tostring(tonumber(new_hold_inbd2) * 10)
							end
							if rte12_act == rte12_sel_fo then
								hold_exec2 = 1
							end
						end
						entry2 = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 3 then
					-- format XXX
					m = tonumber(entry2)
					if m == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						
						-- save data
						if rte12_sel_fo == 0 then
							new_hold_inbd = string.format("%03d", m)
							if new_hold_inbd == "---" then
								legs_data2[hold_offset_idx2][29] = "000"
							else
								legs_data2[hold_offset_idx2][29] = tostring(tonumber(new_hold_inbd) * 10)
							end
							if rte12_act == rte12_sel_fo then
								hold_exec = 1
							end
						else
							new_hold_inbd2 = string.format("%03d", m)
							if new_hold_inbd2 == "---" then
								legs_data8[hold_offset_idx8][29] = "000"
							else
								legs_data8[hold_offset_idx8][29] = tostring(tonumber(new_hold_inbd2) * 10)
							end
							if rte12_act == rte12_sel_fo then
								hold_exec2 = 1
							end
						end
						entry2 = ""
					end
				elseif strlen == 2 then
					-- format /X
					if string.sub(entry2, 1, 1) ~= "/" then
						ww = 1
					end
					if string.sub(entry2, 2, 2) ~= "L" and string.sub(entry2, 2, 2) ~= "R" then
						ww = 1
					end
					if ww == 0 then
						-- save data
						if rte12_sel_fo == 0 then
							if string.sub(entry2, 2, 2) == "L" then
								new_hold_turn = 0
							else
								new_hold_turn = 1
							end
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx2][21] = 1
							else
								legs_data2[hold_offset_idx2][21] = new_hold_turn
							end
							if rte12_act == rte12_sel_fo then
								hold_exec = 1
							end
						else
							if string.sub(entry2, 2, 2) == "L" then
								new_hold_turn2 = 0
							else
								new_hold_turn2 = 1
							end
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx8][21] = 1
							else
								legs_data8[hold_offset_idx8][21] = new_hold_turn2
							end
							if rte12_act == rte12_sel_fo then
								hold_exec2 = 1
							end
						end
						entry2 = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 1 then
					-- format X
					if entry2 == "L" then
						-- save data
						if rte12_sel_fo == 0 then
							new_hold_turn = 0
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx2][21] = 1
							else
								legs_data2[hold_offset_idx2][21] = new_hold_turn
							end
							if rte12_act == rte12_sel_fo then
								hold_exec = 1
							end
						else
							new_hold_turn2 = 0
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx8][21] = 1
							else
								legs_data8[hold_offset_idx8][21] = new_hold_turn2
							end
							if rte12_act == rte12_sel_fo then
								hold_exec2 = 1
							end
						end
						entry2 = ""
					elseif entry2 == "R" then
						-- save data
						if rte12_sel_fo == 0 then
							new_hold_turn = 1
							if new_hold_turn == -1 then
								legs_data2[hold_offset_idx2][21] = 1
							else
								legs_data2[hold_offset_idx2][21] = new_hold_turn
							end
							if rte12_act == rte12_sel_fo then
								hold_exec = 1
							end
						else
							new_hold_turn2 = 1
							if new_hold_turn2 == -1 then
								legs_data8[hold_offset_idx8][21] = 1
							else
								legs_data8[hold_offset_idx8][21] = new_hold_turn2
							end
							if rte12_act == rte12_sel_fo then
								hold_exec2 = 1
							end
						end
						entry2 = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 and ref_icao_x ~= "----" then
				-- entry2 RWY
				if entry2 == ">DELETE" then
					entry2 = INVALID_DELETE
				else
					if rnw_exist(ref_icao_x, entry2) == false then
						add_fmc_msg(NOT_IN_DATABASE, 1)
					else
						ref_rwy2 = entry2
						sel_ref_rwy2 = ref_rwy2
						-- if sel_ref_sid2 == "------" then
							create_sid_list(rte12_sel_fo)
						-- end
						ref_rwy3 = ref_rwy2
						entry2 = ""
						ref_rwy_exec = 1
						rte_add_dep_arr(rte12_sel_fo)
						-- check SID for selected runway
						if rte12_act == rte12_sel_fo then
							if check_sid_rwy() == false then
								add_fmc_msg(RWY_NA_FOR_SID, 1)
							end
						end
					end
				end
			else
				-- button 3 LSK
				rte_x_lsk(3, 1, rte12_sel_fo)
			end
		elseif page_fix2 == 1 then
			local ii = 0
			local jj = string.len(entry2)
			local kk = 0
			local radial_temp = 0
			local data_ok = 0
			if act_page2 <= fix_data_num then
				if jj == 0 then
					-- copy wpt to clipboard
					if fix_data[act_page2][8] ~= -1 and fix_data[act_page2][9] ~= -1 then
						entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_data[act_page2][8]) .. "/"
						if fix_data[act_page2][9] < 10 then
							entry2 = entry2 .. string.format("%3.1f", fix_data[act_page2][9])
						elseif fix_data[act_page2][9] < 100 then
							entry2 = entry2 .. string.format("%4.1f", fix_data[act_page2][9])
						else
							entry2 = entry2 .. string.format("%5.1f", fix_data[act_page2][9])
						end
					elseif fix_data[act_page2][21] > 0 then
						if fix_data[act_page2][8] ~= -1 then
							entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_data[act_page2][8]) .. "/"
							if fix_data[act_page2][25] < 10 then
								entry2 = entry2 .. string.format("%3.1f", fix_data[act_page2][25])
							elseif fix_data[act_page2][25] < 100 then
								entry2 = entry2 .. string.format("%4.1f", fix_data[act_page2][25])
							else
								entry2 = entry2 .. string.format("%5.1f", fix_data[act_page2][25])
							end
						elseif fix_data[act_page2][9] ~= -1 then
							--
						end
					end
				elseif entry2 == ">DELETE" then
					if fix_data[act_page2][8] ~= -1 or fix_data[act_page2][9] ~= -1 then
						fix_data[act_page2][8] = -1
						fix_data[act_page2][9] = -1
						fix_data[act_page2][21] = -1
						entry2 = ""
					end
				elseif jj > 1 then
					if string.sub(entry2, 1, 1) == "/" then
						-- distance
						ii = tonumber(string.sub(entry2, 2, -1))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii <= 0 or ii > 511 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fix_data[act_page2][9] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page2][8] = -1
								fix_data[act_page2][21] = -1
								entry2 = ""
							end
						end
					elseif string.sub(entry2, -1, -1) == "/" then
						-- radial
						ii = tonumber(string.sub(entry2, 1, -2))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii < 0 or ii > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								ii = ii % 360
								fix_data[act_page2][8] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page2][9] = -1
								fix_data[act_page2][21] = -1
								entry2 = ""
							end
						end
					else
						--fixsk
						kk,ii = string.find(entry2, "/")
						if kk ~= nil and kk > 1 and kk < jj then
							radial_temp = tonumber(string.sub(entry2, 1, kk - 1))
							if radial_temp == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									data_ok = 1
								end
								if data_ok == 1 then
									ii = tonumber(string.sub(entry2, kk + 1, -1))
									if ii == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if ii <= 0 or ii > 511 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fix_data[act_page2][8] = tonumber(string.format("%5.1f", radial_temp))
											fix_data[act_page2][9] = tonumber(string.format("%5.1f", ii))
											fix_data[act_page2][21] = -1
											entry2 = ""
										end
									end
								end
							end
						else
							radial_temp = tonumber(entry2)
							if jj == 3 and radial_temp ~= nil then
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									fix_data[act_page][8] = tonumber(string.format("%5.1f", radial_temp))
									fix_data[act_page][9] = -1
									fix_data[act_page][21] = -1
									entry2 = ""
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs2 == 1 then
			-- 3 LSK
			legs_x_lsk(3, 1, rte12_sel_fo)
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			-- CG entry2
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen == 0 then
				if B738DR_calc_to_cg ~= 0 then
					cg = string.format("%4.1f", B738DR_calc_to_cg)
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							--add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							entry2 = ""
						end
					end
					B738DR_calc_trim = 1
				end
			else
				if entry2 == ">DELETE" then
					cg = "--.-"
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							--add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							entry2 = ""
						end
					end
					B738DR_calc_trim = 1
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 6 or n > 36 then	-- CG min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							cg = string.format("%4.1f", n)
							if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
								if v1_set == "---" and vr_set == "---" and v2_set == "---" then
									entry2 = ""
								else
									--add_fmc_msg(VERIFY_TO_SPEEDS, 1)
									entry2 = ""
								end
							end
							B738DR_calc_trim = 1
						end
					end
				end
			end
		elseif page_init2 == 1 then
			-- go to Perf init page
			page_init2 = 0
			page_perf2 = 1
			display_update = 1
		elseif page_pos_init2 == 2 then
			if B738DR_irs2_pos == "-----.-------.-" then
				entry2 = ""
			else
				entry2 = B738DR_irs2_pos
			end
		elseif page_pos_init2 == 1 and disable_POS_3L == 0 then
			-- entry2 Gate
			if entry2 == ">DELETE" then
				ref_gate = "-----"
				entry2 = ""
			else
				if string.len(entry2) > 0 and string.len(entry2) < 6 then
					ref_gate = entry2
					local lenstr = string.len(ref_gate)
					if lenstr == 1 then
						ref_gate = ref_gate .. "    "
					elseif lenstr == 2 then
						ref_gate = ref_gate .. "   "
					elseif lenstr == 3 then
						ref_gate = ref_gate .. "  "
					elseif lenstr == 4 then
						ref_gate = ref_gate .. " "
					end
					entry2 = ""
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_perf2 == 1 then
			-- entry2 ZFW
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen == 0 then
				zfw_mod = zfw_calc
				if units == 0 then
					zfw_lbs_mod = zfw_mod
					zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))
				else
					zfw_kgs_mod = zfw_mod
					zfw_lbs_mod = string.format("%5.1f", (tonumber(zfw_mod) * KGS_LBS))
				end
				if perf_exec < 2 then
					gw = gw_mod
					gw_lbs = gw_lbs_mod
					gw_kgs = gw_kgs_mod
					zfw = zfw_mod
					zfw_lbs = zfw_lbs_mod
					zfw_kgs = zfw_kgs_mod
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
							qrh = "OFF"
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
				else
					perf_exec = 3
				end
				entry2 = ""
			else
				if entry2 == ">DELETE" then
					if perf_exec < 2 then
						zfw_mod = "***.*"
						zfw_lbs_mod = zfw_mod
						zfw_kgs_mod = zfw_mod
						gw = gw_mod
						gw_lbs = gw_lbs_mod
						gw_kgs = gw_kgs_mod
						zfw = zfw_mod
						zfw_lbs = zfw_lbs_mod
						zfw_kgs = zfw_kgs_mod
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry2 = ""
							else
								add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								verify_to_spd = 1
								qrh = "OFF"
								entry2 = ""
							end
						end
						B738DR_calc_vspd = 1
						B738DR_calc_trim = 1
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry2 = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < weight_min or n > weight_max then	-- ZFW min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							zfw_mod = string.format("%5.1f", n)
							if units == 0 then
								zfw_lbs_mod = zfw_mod
								zfw_kgs_mod = string.format("%5.1f", (tonumber(zfw_mod) / KGS_LBS))
							else
								zfw_kgs_mod = zfw_mod
								zfw_lbs_mod = string.format("%5.1f", (tonumber(zfw_mod) * KGS_LBS))
							end
							if perf_exec < 2 then
								gw = gw_mod
								gw_lbs = gw_lbs_mod
								gw_kgs = gw_kgs_mod
								zfw = zfw_mod
								zfw_lbs = zfw_lbs_mod
								zfw_kgs = zfw_kgs_mod
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if v1_set == "---" and vr_set == "---" and v2_set == "---" then
										entry2 = ""
									else
										add_fmc_msg(VERIFY_TO_SPEEDS, 1)
										verify_to_spd = 1
										qrh = "OFF"
										entry2 = ""
									end
								end
								B738DR_calc_vspd = 1
								B738DR_calc_trim = 1
							else
								perf_exec = 3
							end
							entry2 = ""
						end
					end
				end
			end
		elseif page_n1_limit2 == 1 then
			if in_flight_mode == 0 then
				--if B738DR_thr_takeoff_N1 ~= 0 then
				if B738DR_fmc_oat_temp ~= 99 then
					to_1 = "<ACT>"
					to = "     "
					to_2 = "     "
					--if sel_clb_thr == 0 then
					if sel_temp == "----" then
						clb = "<SEL>"
						clb_1 = "     "
						clb_2 = "     "
					else
						clb = "     "
						clb_1 = "     "
						clb_2 = "<SEL>"
					end
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							-- verify_to_spd = 1
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
				end
			else
				-- select CON
				auto_act = "     "
				ga_act = "     "
				con_act = "<ACT>"
				clb_act = "     "
				crz_act = "     "
			end
		elseif page_descent_forecast2 == 1 then
			-- entry2 WIND ALT LAYER 1
			if entry2 == ">DELETE" then
				forec_alt_1_mod = "-----"
				forec_alt_1_num_mod = 0
				entry2 = ""
				forecast_exec = 1
			else
				local n = entry_altitude(entry2)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					forec_alt_1_num_mod = n
					if forec_alt_1_num_mod >= B738DR_trans_lvl_mod then
						n = n / 100
						forec_alt_1_mod = "FL" .. string.format("%03d", n)
					else
						forec_alt_1_mod = string.format("%5d", n)
					end
					entry2 = ""
					if forec_dir_1 ~= "---" or forec_spd_1 ~= "---" then
						forecast_exec = 1
					end
					wind_alt_order()
				end
			end
		elseif page_perf2 == 2 then
			-- CRZ min speed kts/mach
			local strlen = string.len(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					crz_min_kts = "   "
					crz_min_mach = "   "
					entry2 = ""
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry2, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_max_mach)
							if nn == nil then
								nn = 0.82
							else
								nn = nn / 1000
							end
							if n < 0.4 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								crz_min_mach = string.format("%03d", n)
								entry2 = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								crz_min_kts = string.format("%03d", n)
								entry2 = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry2, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry2, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
								n = tonumber(string.sub(entry2, 5, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nn = tonumber(crz_max_mach)
									if nn == nil then
										nn = 0.82
									else
										nn = nn / 1000
									end
									if n < 0.4 or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n = n * 1000
										crz_min_kts = kts
										crz_min_mach = string.format("%03d", n)
										entry2 = ""
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		-----------
		elseif page_ref_nav_data_wpt2 == 1 then
			if string.len(entry2) > 0 then
				local err_pos = 0
				local n = 0
				if string.len(entry2) ~= 7 or string.sub(entry2, 6, 6) ~= "." then
					err_pos = 1
				end
				n = tonumber(string.sub(entry2, 2, 3))
				if (n == nil) or (n < 0) or (n > 90) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry2, 4, 5))
				if (n == nil) or (n < 0) or (n > 59) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry2, 7, 7))
				if n == nil then
					err_pos = 1
				end
				if string.sub(entry2, 1, 1) ~= "N" 
				and string.sub(entry2, 1, 1) ~= "S" then
					err_pos = 1
				end
				if err_pos == 0 then
					ref_nav_lat = tonumber(string.sub(entry2, 2, 3)) + (tonumber(string.sub(entry2, 4, 7)) / 60)
					if string.sub(entry2, 1, 1) == "S" then
						ref_nav_lat = -ref_nav_lat
					end
					entry2 = ""
					ref_nav_id = "-----"
					ref_nav_rc = ""
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					if ref_nav_lon ~= 0 then
						ref_nav_exec = 1
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_offset2 == 1 and offset_dist_mod ~= 0 and offset_act < 5 then
			-- entry Start Offset
			local strlen = string.len(entry2)
			if strlen == 0 then
				if offset_start_mod ~= 0 then
					if rte12_act == 0 then
						entry2 = legs_data2[offset_start_mod][1]
					elseif rte12_act == 1 then
						entry2 = legs_data8[offset_start_mod][1]
					end
				end
			else
				if entry2 == ">DELETE" then
					offset_start_mod = 0
					entry2 = ""
					offset_check(rte12_act)
					if offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
						if offset_create(rte12_act) == true then
							-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
								-- offset_act_mod = 3
							-- else
								-- offset_act_mod = 2
							-- end
							if rte12_act == 0 then
								if offset_start_mod == 0 then
									if legs_data2[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							else
								if offset_start_mod == 0 then
									if legs_data8[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							end
						else
							offset_act_mod = 1
						end
					end
				elseif strlen < 6 then
					offset_check1(entry2, rte12_act)
					if offset_start_mod == 0 then
						-- add_fmc_msg(INVALID_INPUT, 1)
						-- add_fmc_msg(UNABLE_OFFSET, 1)
						add_fmc_msg(NO_OFFSET_AT .. entry2 , 1)
					else
						entry2 = ""
						offset_act_mod = 1
						offset_check(rte12_act)
						if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
							if offset_create(rte12_act) == true then
								-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
									-- offset_act_mod = 3
								-- else
									-- offset_act_mod = 2
								-- end
								if rte12_act == 0 then
									if offset_start_mod == 0 then
										if legs_data2[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								else
									if offset_start_mod == 0 then
										if legs_data8[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								end
							else
								offset_act_mod = 1
							end
						else
							add_fmc_msg(UNABLE_OFFSET, 1)
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb < 2 then
				-- change restrict speed by altitude
				if entry2 == ">DELETE" and clb_del_spd_idx_fo ~= 0 then
					entry2 = ""
					clb_del_spd_idx2_fo = clb_del_spd_idx_fo
					climb_exec = 1
				else
					set_spd_alt_rest_default2(1, 0)
				end
			end
		elseif page_descent2 == 1 then
			if entry2 == ">DELETE" and des_del_spd_idx_fo ~= 0 then
				entry2 = ""
				des_del_spd_idx2_fo = des_del_spd_idx_fo
				descent_exec = 1
			else
				-- change restrict speed by altitude
				set_spd_alt_rest_default(1, 0)
			end
		elseif page_cruise2 == 1 then
			if (crz_exec ~= 0 and B738DR_flight_phase >= 2 and B738DR_flight_phase < 8) or B738DR_flight_phase == 3 or B738DR_flight_phase == 4 then
				if B738DR_flight_phase == 3 or crz_alt_num2 > (simDR_altitude_pilot + 200) then
					set_spd_alt_rest_default2(1, 1)
				elseif B738DR_flight_phase == 4 or crz_alt_num2 <= (simDR_altitude_pilot + 200) then
					set_spd_alt_rest_default(1, 1)
				end
			end
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				entry2 = nearest_apt_list_icao[3]
				if entry2 == "----" then
					entry2 = ""
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				add_altn_apt(entry2, 3, 1)
			end
		elseif page_flt_plns2 == 1 then
			entry2 = sel_flt_plns2[3]
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 4LSK
function B738_fmc2_4L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel_fo[4] ~= "" then
						if ref_sid2 == ref_sid_sel_fo[4] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid2 = ref_sid_sel_fo[4]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel_fo[4] ~= "" then
							if ref_sid_tns2 == ref_tns_sel_fo[4] then
								sel_ref_sid_tns2 = ref_sid_tns2
							else
								ref_sid_tns2 = ref_tns_sel_fo[4]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel_fo[4] ~= "" then
						if ref_sid8 == ref_sid_sel_fo[4] then
							sel_ref_sid8 = ref_sid8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid8 = ref_sid_sel_fo[4]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy() == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel_fo[4] ~= "" then
							if ref_sid_tns8 == ref_tns_sel_fo[4] then
								sel_ref_sid_tns8 = ref_sid_tns8
							else
								ref_sid_tns8 = ref_tns_sel_fo[4]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel_fo[4] ~= "" then
						if des_star2 == des_star_sel_fo[4] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star2 = des_star_sel_fo[4]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel_fo[4] ~= "" then
							if des_star_trans2 == des_star_tns_sel_fo[4] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel_fo[4]
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel_fo[4] ~= "" then
						if des_star8 == des_star_sel_fo[4] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star8 = des_star_sel_fo[4]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel_fo[4] ~= "" then
							if des_star_trans8 == des_star_tns_sel_fo[4] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans8 = des_star_tns_sel_fo[4]
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_sel_wpt_2 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt2_2 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry2, 1, rte12_sel_fo)
			end
		elseif page_sel_wpt3_2 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4_2 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 1)
			end
		elseif page_sel_hold_wpt_2 == 1 then
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 1, rte12_sel_fo)
			end
		elseif page_ref_sel2 == 1 then
			
			local item = 0
			local button = 4	-- button 4 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				ref_nav_new = "*****"
				page_ref_nav_data2 = 1
				page_ref_sel2 = 0
			end
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 then
				if rte12_sel_fo == 0 then
					if legs_num2 > 1 then
						if save_rte1 == 0 then
							save_rte1 = 1
						elseif save_rte1 == 1 and co_route_save1 ~= "************" then
							--save route
							save_fpln(co_route_save1, 0)
							co_route = co_route_save1
							co_route_x = co_route
							save_rte1 = 0
						end
					end
				else
					if legs_num8 > 1 then
						if save_rte2 == 0 then
							save_rte2 = 1
						elseif save_rte2 == 1 and co_route_save2 ~= "************" then
							--save route
							save_fpln(co_route_save2, 1)
							co_route7 = co_route_save2
							co_route7_x = co_route7
							save_rte2 = 0
						end
					end
				end
			else
				-- button 4 LSK
				rte_x_lsk(4, 1, rte12_sel_fo)
			end
		elseif page_hold2 == 1 then
			local strlen = string.len(entry2)
			local m = 0
			local n = 0
			local ww = 0
			if entry2 == ">DELETE" then
				if rte12_sel_fo == 0 then
					new_hold_time = "-.-"
					if new_hold_time == "-.-" then
						if new_hold_dist == "--.-" then
							-- default 1.5 min
							legs_data2[hold_offset_idx2][30] = ""
						else
							m = tonumber(new_hold_dist) * 10
							legs_data2[hold_offset_idx2][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time) * 10
						legs_data2[hold_offset_idx2][30] = "T" .. string.format("%03d", m)
					end
				else
					new_hold_time2 = "-.-"
					if new_hold_time2 == "-.-" then
						if new_hold_dist2 == "--.-" then
							-- default 1.5 min
							legs_data8[hold_offset_idx8][30] = ""
						else
							m = tonumber(new_hold_dist2) * 10
							legs_data8[hold_offset_idx8][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time2) * 10
						legs_data8[hold_offset_idx8][30] = "T" .. string.format("%03d", m)
					end
				end
				entry2 = ""
			else
				if strlen > 0 and strlen < 4 then
					m = tonumber(entry2)
					-- if ww == 0 then
					if m ~= nil then
						if m >= 1 and m <= 9.9 then
							-- m = tonumber(entry2)
							if rte12_sel_fo == 0 then
								new_hold_time = string.format("%03.1f", m)
								new_hold_dist = "--.-"
								if new_hold_time == "-.-" then
									if new_hold_dist == "--.-" then
										-- default 1.5 min
										legs_data2[hold_offset_idx2][30] = ""
									else
										m = tonumber(new_hold_dist) * 10
										legs_data2[hold_offset_idx2][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time) * 10
									legs_data2[hold_offset_idx2][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel_fo then
									hold_exec = 1
								end
							else
								new_hold_time2 = string.format("%03.1f", m)
								new_hold_dist2 = "--.-"
								if new_hold_time2 == "-.-" then
									if new_hold_dist2 == "--.-" then
										-- default 1.5 min
										legs_data8[hold_offset_idx8][30] = ""
									else
										m = tonumber(new_hold_dist2) * 10
										legs_data8[hold_offset_idx8][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time2) * 10
									legs_data8[hold_offset_idx8][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel_fo then
									hold_exec2 = 1
								end
							end
							entry2 = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_fix2 == 1 then
			local ii = 0
			local jj = string.len(entry2)
			local kk = 0
			local radial_temp = 0
			local data_ok = 0
			if act_page2 <= fix_data_num then
				if jj == 0 then
					-- copy wpt to clipboard
					if fix_data[act_page2][10] ~= -1 and fix_data[act_page2][11] ~= -1 then
						entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_data[act_page2][10]) .. "/"
						if fix_data[act_page2][11] < 10 then
							entry2 = entry2 .. string.format("%3.1f", fix_data[act_page2][11])
						elseif fix_data[act_page2][11] < 100 then
							entry2 = entry2 .. string.format("%4.1f", fix_data[act_page2][11])
						else
							entry2 = entry2 .. string.format("%5.1f", fix_data[act_page2][11])
						end
					elseif fix_data[act_page2][30] > 0 then
						if fix_data[act_page2][10] ~= -1 then
							entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_data[act_page2][10]) .. "/"
							if fix_data[act_page2][34] < 10 then
								entry2 = entry2 .. string.format("%3.1f", fix_data[act_page2][34])
							elseif fix_data[act_page2][34] < 100 then
								entry2 = entry2 .. string.format("%4.1f", fix_data[act_page2][34])
							else
								entry2 = entry2 .. string.format("%5.1f", fix_data[act_page2][34])
							end
						elseif fix_data[act_page2][11] ~= -1 then
							--
						end
					end
				elseif entry2 == ">DELETE" then
					if fix_data[act_page2][10] ~= -1 or fix_data[act_page2][11] ~= -1 then
						fix_data[act_page2][10] = -1
						fix_data[act_page2][11] = -1
						fix_data[act_page2][30] = -1
						entry2 = ""
					end
				elseif jj > 1 then
					if string.sub(entry2, 1, 1) == "/" then
						-- distance
						ii = tonumber(string.sub(entry2, 2, -1))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii <= 0 or ii > 511 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								fix_data[act_page2][11] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page2][10] = -1
								fix_data[act_page2][30] = -1
								entry2 = ""
							end
						end
					elseif string.sub(entry2, -1, -1) == "/" then
						-- radial
						ii = tonumber(string.sub(entry2, 1, -2))
						if ii == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if ii < 0 or ii > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								ii = ii % 360
								fix_data[act_page2][10] = tonumber(string.format("%5.1f", ii))
								fix_data[act_page2][11] = -1
								fix_data[act_page2][30] = -1
								entry2 = ""
							end
						end
					else
						--fixsk
						kk,ii = string.find(entry2, "/")
						if kk ~= nil and kk > 1 and kk < jj then
							radial_temp = tonumber(string.sub(entry2, 1, kk - 1))
							if radial_temp == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									data_ok = 1
								end
								if data_ok == 1 then
									ii = tonumber(string.sub(entry2, kk + 1, -1))
									if ii == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if ii <= 0 or ii > 511 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											fix_data[act_page2][10] = tonumber(string.format("%5.1f", radial_temp))
											fix_data[act_page2][11] = tonumber(string.format("%5.1f", ii))
											fix_data[act_page2][30] = -1
											entry2 = ""
										end
									end
								end
							end
						else
							radial_temp = tonumber(entry2)
							if jj == 3 and radial_temp ~= nil then
								if radial_temp < 0 or radial_temp > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									radial_temp = radial_temp % 360
									fix_data[act_page][10] = tonumber(string.format("%5.1f", radial_temp))
									fix_data[act_page][11] = -1
									fix_data[act_page][30] = -1
									entry2 = ""
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs2 == 1 then
			-- 4 LSK
			legs_x_lsk(4, 1, rte12_sel_fo)
		elseif page_init2 == 1 then
			-- if B738DR_flight_phase == 1 then
				-- -- go to Climb page
				-- reset_fmc_pages_fo()
				-- page_climb2 = 1
			-- elseif B738DR_flight_phase == 2 then
				-- -- go to Cruise page
				-- reset_fmc_pages_fo()
				-- page_cruise2 = 1
			-- else
				-- go to Takeoff page
				reset_fmc_pages_fo()
				page_takeoff2 = 1
			-- end
		elseif page_pos_init2 == 2 then
			if B738DR_gps_pos == "-----.-------.-" then
				entry2 = ""
			else
				entry2 = B738DR_gps_pos
			end
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
				-- if perf_exec == 2 and B738DR_climb_mode ~= 0 then
					-- -- CLB ECON
					-- entry2 = ""
					-- B738DR_climb_mode = 0
					-- B738DR_fmc_climb_speed_mach = econ_clb_spd_mach		-- temporary
					-- B738DR_fmc_climb_speed = econ_clb_spd				-- temporary
				-- end
				if perf_exec == 2 and fmc_climb_mode_mod ~= 0 then
					-- CLB ECON
					entry2 = ""
					fmc_climb_mode_mod = 0
					fmc_climb_speed_mach_mod = econ_clb_spd_mach		-- temporary
					fmc_climb_speed_mod = econ_clb_spd				-- temporary
					clb_spd_exec = 1
				end
			else
				B738DR_eng_out_disable = 1
			end
		elseif page_eo_climb2 == 1 then
			engout_clb = 2
		elseif page_eo_cruise2 == 1 then
			engout_crz = 2
		elseif page_descent_forecast2 == 1 then
			-- entry2 WIND ALT LAYER 2
			if entry2 == ">DELETE" then
				forec_alt_2_mod = "-----"
				forec_alt_2_num_mod = 0
				entry2 = ""
				forecast_exec = 1
			else
				local n = entry_altitude(entry2)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					forec_alt_2_num_mod = n
					if forec_alt_2_num_mod >= B738DR_trans_lvl_mod then
						n = n / 100
						forec_alt_2_mod = "FL" .. string.format("%03d", n)
					else
						forec_alt_2_mod = string.format("%5d", n)
					end
					entry2 = ""
					if forec_dir_2 ~= "---" or forec_spd_2 ~= "---" then
						forecast_exec = 1
					end
					wind_alt_order()
				end
			end
		elseif page_perf2 == 1 then
			-- entry2 Reserve fuel
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					if perf_exec < 2 then
						reserves_mod = "**.*"
						reserves_lbs = reserves_mod
						reserves_kgs = reserves_mod
						reserves = reserves_mod
						reserves_lbs = reserves
						reserves_kgs = reserves
						msg_using_rsv_fuel = 0
						msg_using_ins_fuel = 0
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry2 = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						--36.2 lbs / 16.4
						local max_res_value = 36.2
						if units ~= 0 then
							max_res_value = 16.4
						end
						if n < 0 or n > max_res_value then	-- Reserves min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							reserves_mod = string.format("%4.1f", n)
							if units == 0 then
								reserves_lbs_mod = reserves_mod
								reserves_kgs_mod = string.format("%4.1f", (tonumber(reserves_mod) / KGS_LBS))
							else
								reserves_kgs_mod = reserves_mod
								reserves_lbs_mod = string.format("%4.1f", (tonumber(reserves_mod) * KGS_LBS))
							end
							if perf_exec < 2 then
								reserves = reserves_mod
								reserves_lbs = reserves_lbs_mod
								reserves_kgs = reserves_kgs_mod
								msg_using_rsv_fuel = 0
								msg_using_ins_fuel = 0
							else
								perf_exec = 3
							end
							entry2 = ""
						end
					end
				end
			end
		elseif page_n1_limit2 == 1 then
			if in_flight_mode == 0 then
				if B738DR_fmc_oat_temp ~= 99 then
					to_2 = "<ACT>"
					to = "     "
					to_1 = "     "
					if sel_temp == "----" then
						clb = "     "
						clb_1 = "<SEL>"
						clb_2 = "     "
					else
						clb = "     "
						clb_1 = "     "
						clb_2 = "<SEL>"
					end
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							v1_set = "---"
							vr_set = "---"
							v2_set = "---"
							verify_to_spd = 0
							add_fmc_msg(TO_SPEEDS_DELETED, 1)
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
				end
			else
				-- select CLB
				auto_act = "     "
				ga_act = "     "
				con_act = "     "
				clb_act = "<ACT>"
				crz_act = "     "
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			-- enter ACCELERATION HEIGHT AGL
			if B738DR_thr_cutback == 0 then
				local strlen = string.len(entry2)
				if strlen > 0 then
					if entry2 == ">DELETE" then
						accel_alt = "----"
						accel_alt_num = B738DR_def_accel_alt
						entry2 = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 400 or n > 9999 then	-- ACCEL HT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								accel_alt = string.format("%4d", n)
								entry2 = ""
								accel_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_perf2 == 2 then
			-- DES min speed kts/mach
			local strlen = string.len(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					des_min_kts = "   "
					des_min_mach = "   "
					entry2 = ""
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry2, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_max_mach)
							if nn == nil then
								nn = 0.82
							else
								nn = nn / 1000
							end
							if n < 0.4 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								des_min_mach = string.format("%03d", n)
								entry2 = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								des_min_kts = string.format("%03d", n)
								entry2 = ""
								B738_calc_vnav_spd()
								vnav_update = 1
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry2, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry2, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_max_kts)
							if nn == nil then
								nn = 340
							end
							if n < 100 or n > nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
								n = tonumber(string.sub(entry2, 5, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nn = tonumber(des_max_mach)
									if nn == nil then
										nn = 0.82
									else
										nn = nn / 1000
									end
									if n < 0.4 or n > nn then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										n = n * 1000
										des_min_kts = kts
										des_min_mach = string.format("%03d", n)
										entry2 = ""
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		-----------
		elseif page_offset2 == 1 and offset_dist_mod ~= 0 and offset_act < 8 then
			-- entry End Offset
			local strlen = string.len(entry2)
			if strlen == 0 then
				if offset_end_mod ~= 0 then
					if rte12_act == 0 then
						entry2 = legs_data2[offset_end_mod][1]
					elseif rte12_act == 1 then
						entry2 = legs_data8[offset_end_mod][1]
					end
				end
			else
				if entry2 == ">DELETE" then
					offset_end_mod = 0
					entry2 = ""
					offset_check(rte12_act)
					if offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
						if offset_create(rte12_act) == true then
							-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
								-- offset_act_mod = 3
							-- else
								-- offset_act_mod = 2
							-- end
							if rte12_act == 0 then
								if offset_start_mod == 0 then
									if legs_data2[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							else
								if offset_start_mod == 0 then
									if legs_data8[offset][45] ~= 0 then
										offset_act_mod = 3
									else
										offset_act_mod = 1
									end
								else
									offset_act_mod = 2
								end
							end
						else
							offset_act_mod = 1
						end
					end
				elseif strlen < 6 then
					offset_check2(entry2, rte12_act)
					if offset_end_mod == 0 then
						-- add_fmc_msg(INVALID_INPUT, 1)
						-- add_fmc_msg(UNABLE_OFFSET, 1)
						add_fmc_msg(NO_OFFSET_AT .. entry2 , 1)
					else
						entry2 = ""
						offset_act_mod = 1
						offset_check(rte12_act)
						if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
							if offset_create(rte12_act) == true then
								-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
									-- offset_act_mod = 3
								-- else
									-- offset_act_mod = 2
								-- end
								if rte12_act == 0 then
									if offset_start_mod == 0 then
										if legs_data2[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								else
									if offset_start_mod == 0 then
										if legs_data8[offset][45] ~= 0 then
											offset_act_mod = 3
										else
											offset_act_mod = 1
										end
									else
										offset_act_mod = 2
									end
								end
							else
								offset_act_mod = 1
							end
						else
							add_fmc_msg(UNABLE_OFFSET, 1)
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				entry2 = nearest_apt_list_icao[4]
				if entry2 == "----" then
					entry2 = ""
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				add_altn_apt(entry2, 4, 1)
			end
		elseif page_flt_plns2 == 1 then
			entry2 = sel_flt_plns2[4]
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 5LSK
function B738_fmc2_5L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_ref_sid2 == "------" then
					if ref_sid_sel_fo[5] ~= "" then
						if ref_sid2 == ref_sid_sel_fo[5] then
							sel_ref_sid2 = ref_sid2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid2 = ref_sid_sel_fo[5]
							ref_sid_tns2 = "------"
							sel_ref_sid2 = ref_sid2
							sel_ref_sid_tns2 = ref_sid_tns2
							if sel_ref_rwy2 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy(rte12_sel_fo) == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns2 == "------" then
						if ref_tns_sel_fo[5] ~= "" then
							if ref_sid_tns2 == ref_tns_sel_fo[5] then
								sel_ref_sid_tns2 = ref_sid_tns2
							else
								ref_sid_tns2 = ref_tns_sel_fo[5]
								sel_ref_sid_tns2 = ref_sid_tns2
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_ref_sid8 == "------" then
					if ref_sid_sel_fo[5] ~= "" then
						if ref_sid8 == ref_sid_sel_fo[5] then
							sel_ref_sid8 = ref_sid8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
						else
							ref_sid8 = ref_sid_sel_fo[5]
							ref_sid_tns8 = "------"
							sel_ref_sid8 = ref_sid8
							sel_ref_sid_tns8 = ref_sid_tns8
							if sel_ref_rwy8 == "-----" then
								create_rnw_list(rte12_sel_fo)
							end
							create_sid_tns_list(rte12_sel_fo)
							ref_sid_exec = 1
							
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							-- check SID for selected runway
							if rte12_act == rte12_sel_fo then
								if check_sid_rwy(rte12_sel_fo) == false then
									add_fmc_msg(RWY_NA_FOR_SID, 1)
								end
							end
						end
						act_page2 = 1
					end
				else
					if sel_ref_sid_tns8 == "------" then
						if ref_tns_sel_fo[5] ~= "" then
							if ref_sid_tns8 == ref_tns_sel_fo[5] then
								sel_ref_sid_tns8 = ref_sid_tns8
							else
								ref_sid_tns8 = ref_tns_sel_fo[5]
								sel_ref_sid_tns8 = ref_sid_tns8
								ref_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_star2 == "------" then
					if des_star_sel_fo[5] ~= "" then
						if des_star2 == des_star_sel_fo[5] then
							sel_des_star2 = des_star2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star2 = des_star_sel_fo[5]
							des_star_trans2 = "------"
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							if sel_des_app2 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans2 == "------" then
						if des_star_tns_sel_fo[5] ~= "" then
							if des_star_trans2 == des_star_tns_sel_fo[5] then
								sel_des_star_trans2 = des_star_trans2
							else
								des_star_trans2 = des_star_tns_sel_fo[5]
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_des_star8 == "------" then
					if des_star_sel_fo[5] ~= "" then
						if des_star8 == des_star_sel_fo[5] then
							sel_des_star8 = des_star8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
						else
							des_star8 = des_star_sel_fo[5]
							des_star_trans8 = "------"
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							if sel_des_app8 == "------" then
								create_app_list(rte12_sel_fo)
							end
							create_star_tns_list(rte12_sel_fo)
							des_star_exec = 1
						end
						act_page2 = 1
					end
				else
					if sel_des_star_trans8 == "------" then
						if des_star_tns_sel_fo[5] ~= "" then
							if des_star_trans8 == des_star_tns_sel_fo[5] then
								sel_des_star_trans8 = des_star_trans8
							else
								des_star_trans8 = des_star_tns_sel_fo[5]
								des_star_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_sel_wpt_2 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt2(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		
		elseif page_sel_wpt2_2 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				dir_add(item, entry2, 1, rte12_sel_fo)
			end
		elseif page_sel_wpt3_2 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				rte_add_wpt4(item, 1, rte12_sel_fo)
				if rte12_act == rte12_sel_fo then
					legs_delete = 1
				end
				fms_recalc = 1
				msg_unavaible_crz_alt = 0
			end
		elseif page_sel_wpt4_2 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				fix_add_wpt4(item, 1)
			end
		elseif page_sel_hold_wpt_2 == 1 then
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				sel_add_hold_wpt(item, 1, rte12_sel_fo)
			end
		elseif page_ref_sel2 == 1 then
			
			local item = 0
			local button = 5	-- button 5 LSK
			
			item = (act_page2 - 1) * 5 + button
			if item <= navaid_list_n then
				-- select item
				if nav_data_type == 0 then -- WPT
					ref_nav_wpt = nav_data_entry	--entry
					ref_nav_navaid = "----"
					ref_nav_apt = "----"
				elseif nav_data_type == 1 then -- NAVAID
					ref_nav_wpt = "-----"
					ref_nav_navaid = nav_data_entry	--entry
					ref_nav_apt = "----"
				else -- APT
					ref_nav_wpt = "-----"
					ref_nav_navaid = "----"
					ref_nav_apt = nav_data_entry	--entry
				end
				nav_data_idx = item
				--ref_nav_wpt = nav_data_entry
				ref_nav_new = "*****"
				page_ref_nav_data2 = 1
				page_ref_sel2 = 0
			end
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 then
				if save_rte1 ~= 0 and rte12_sel_fo == 0 then
					save_rte1 = 0
					co_route_save1 = "************"
				elseif save_rte2 ~= 0 and rte12_sel_fo == 1 then
					save_rte2 = 0
					co_route_save2 = "************"
				else
					page_altern_apt2 = 1
					page_rte_init2 = 0
					act_page2 = 1
				end
			else
				-- button 5 LSK
				rte_x_lsk(5, 1, rte12_sel_fo)
			end
		elseif page_hold2 == 1 then
			local strlen = string.len(entry2)
			local m = 0
			local n = 0
			local ww = 0
			if entry2 == ">DELETE" then
				if rte12_sel_fo == 0 then
					new_hold_dist = "--.-"
					if new_hold_time == "-.-" then
						if new_hold_dist == "--.-" then
							-- default 1.5 min
							legs_data2[hold_offset_idx2][30] = ""
						else
							m = tonumber(new_hold_dist) * 10
							legs_data2[hold_offset_idx2][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time) * 10
						legs_data2[hold_offset_idx2][30] = "T" .. string.format("%03d", m)
					end
				else
					new_hold_dist2 = "--.-"
					if new_hold_time == "-.-" then
						if new_hold_dist2 == "--.-" then
							-- default 1.5 min
							legs_data8[hold_offset_idx8][30] = ""
						else
							m = tonumber(new_hold_dist2) * 10
							legs_data8[hold_offset_idx8][30] = string.format("%04d", m)
						end
					else
						m = tonumber(new_hold_time2) * 10
						legs_data8[hold_offset_idx8][30] = "T" .. string.format("%03d", m)
					end
				end
				entry2 = ""
			else
				if strlen > 0 and strlen < 5 then
					-- if ww == 0 then
					m = tonumber(entry2)
					if m ~= nil then
						if m >= 1 and m <= 15 then
							if rte12_sel_fo == 0 then
								new_hold_dist = string.format("%04.1f", m)
								new_hold_time = "-.-"
								if new_hold_time == "-.-" then
									if new_hold_dist == "--.-" then
										-- default 1.5 min
										legs_data2[hold_offset_idx2][30] = ""
									else
										m = tonumber(new_hold_dist) * 10
										legs_data2[hold_offset_idx2][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time) * 10
									legs_data2[hold_offset_idx2][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel_fo then
									hold_exec = 1
								end
							else
								new_hold_dist2 = string.format("%04.1f", m)
								new_hold_time2 = "-.-"
								if new_hold_time2 == "-.-" then
									if new_hold_dist2 == "--.-" then
										-- default 1.5 min
										legs_data8[hold_offset_idx8][30] = ""
									else
										m = tonumber(new_hold_dist2) * 10
										legs_data8[hold_offset_idx8][30] = string.format("%04d", m)
									end
								else
									m = tonumber(new_hold_time2) * 10
									legs_data8[hold_offset_idx8][30] = "T" .. string.format("%03d", m)
								end
								if rte12_act == rte12_sel_fo then
									hold_exec2 = 1
								end
							end
							entry2 = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_legs2 == 1 then
			if B738DR_no_rte2 ~= 0 or act_page2 ~= 1 then
				-- 5 LSK
				legs_x_lsk(5, 1, rte12_sel_fo)
			end
		elseif page_init2 == 1 then
			-- go to Approach page
			page_init2 = 0
			if page_approach == 0 and page_approach2 == 0 then
				gw_app_nul = 0
				gw_app = gw
				gw_app_lbs = gw_lbs
				gw_app_kgs = gw_kgs
			end
			page_approach2 = 1
			display_update = 1
		elseif page_pos_init2 == 2 then
			if B738DR_gps2_pos == "-----.-------.-" then
				entry2 = ""
			else
				entry2 = B738DR_gps2_pos
			end
		elseif page_approach2 == 1 then
			-- G/S enable/disable
			if des_app ~= "------" then
				if B738DR_ian_not_install == 0 then
					if string.sub(des_app2, 1, 1) ~= "R" and string.sub(des_app2, 1, 1) ~= "D" and string.sub(des_app2, 1, 1) ~= "S" 
					and string.sub(des_app2, 1, 1) ~= "N" and string.sub(des_app2, 1, 1) ~= "Q" and string.sub(des_app2, 1, 1) ~= "V" then
						if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
							if B738DR_fms_ils_disable == 0 then
								B738DR_fms_ils_disable = 1
							else
								B738DR_fms_ils_disable = 0
							end
						end
					end
				end
			end
		elseif page_perf2 == 1 then --and was_on_air == 0 then	-- only on ground
			-- entry2 Cost Index
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					if perf_exec < 2 then
						cost_index_mod = "***"
						cost_index = cost_index_mod
						vnav_update = 1
						msg_chk_alt_constr = 0
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
					entry2 = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 0 or n > 500 then	-- Cost Index min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							cost_index_mod = string.format("%3d", n)
							if perf_exec < 2 then
								cost_index = cost_index_mod
								vnav_update = 1
								msg_chk_alt_constr = 0
							else
								perf_exec = 3
							end
							entry2 = ""
						end
					end
				end
			end
			B738_calc_vnav_spd()
		elseif page_cruise2 == 1 then
			if engout_crz == 0 then
				if fmc_cruise_mode_mod ~= 0 then
					-- CRZ ECON
					entry2 = ""
					fmc_cruise_mode_mod = 0
					fmc_cruise_speed_mod = econ_crz_spd
					fmc_cruise_speed_mach_mod = econ_crz_spd_mach
					crz_spd_exec = 1
				end
			else
				engout_crz = 2
				page_cruise2 = 0
				page_eo_cruise2 = 1
			end
		elseif page_descent2 == 1 then
			if fmc_descent_mode_mod == 2 then
				-- DES PATH ECON
				entry2 = ""
				fmc_descent_mode_mod = 0
				des_spd_exec = 1
				fmc_descent_speed_mod = econ_des_spd
				fmc_descent_speed_mach_mod = econ_des_spd_mach
			elseif fmc_descent_mode_mod == 3 then
				-- DES SPD ECON
				entry2 = ""
				fmc_descent_mode_mod = 1
				des_spd_exec = 1
				fmc_descent_speed_mod = econ_des_spd
				fmc_descent_speed_mach_mod = econ_des_spd_mach
			end
		elseif page_n1_limit2 == 1 then
			if in_flight_mode == 1 then
				-- select CRZ
				auto_act = "     "
				ga_act = "     "
				con_act = "     "
				clb_act = "     "
				crz_act = "<ACT>"
			end
		elseif page_ref_nav_data_wpt2 == 1 then
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" and ref_nav_id ~= "-----" then
					ref_nav_id = "-----"
					ref_nav_id_lat = 0
					ref_nav_id_lon = 0
					ref_nav_rc = ""
					ref_nav_lat = 0
					ref_nav_lon = 0
					ref_nav_exec = 1
					entry2 = ""
				else
					find_navaid(entry2, "", 0, "", rte12_act)
					if navaid_list_n > 0 then
						-- first navaid
						ref_nav_id = navaid_list[1][4]
						ref_nav_rc = navaid_list[1][8]
						ref_nav_id_lat = navaid_list[1][2]
						ref_nav_id_lon = navaid_list[1][3]
						
						-- calc lat / lon
						if ref_nav_brg ~= "---" and ref_nav_dist ~= "---" then
							calc_brg_dist(ref_nav_id_lat, ref_nav_id_lon, math.rad(tonumber(ref_nav_brg)), tonumber(ref_nav_dist))
							ref_nav_lat = calc_lat
							ref_nav_lon = calc_lon
							ref_nav_exec = 1
						end
						entry2 = ""
					end
				end
			end
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if engout_clb == 0 then
				-- if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
					-- if B738DR_climb_mode ~= 1 then
						-- -- MAX RATE
						-- entry2 = ""
						-- B738DR_climb_mode = 1
						-- B738DR_fmc_climb_speed_mach = 0.74	--0.76		-- temporary
						-- if B738DR_pfd_flaps_up == 0 then
							-- B738DR_fmc_climb_speed = 265	--250
						-- else
							-- B738DR_fmc_climb_speed = math.max(B738DR_pfd_flaps_up + 70, 250)
						-- end
					-- end
				-- end
				if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
					if fmc_climb_mode_mod ~= 1 then
						-- MAX RATE
						entry2 = ""
						fmc_climb_mode_mod = 1
						fmc_climb_speed_mach_mod = 0.74	--0.76		-- temporary
						if B738DR_pfd_flaps_up == 0 then
							fmc_climb_speed_mod = 265	--250
						else
							fmc_climb_speed_mod = math.max(B738DR_pfd_flaps_up + 70, 250)
						end
						clb_spd_exec = 1
					end
				end
			else
				engout_clb = 2
				page_climb2 = 0
				page_eo_climb2 = 1
			end
		elseif page_descent_forecast2 == 1 then
			-- entry2 WIND ALT LAYER 3
			if entry2 == ">DELETE" then
				forec_alt_3_mod = "-----"
				forec_alt_3_num_mod = 0
				entry2 = ""
				forecast_exec = 1
			else
				local n = entry_altitude(entry2)
				if n == 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				else
					forec_alt_3_num_mod = n
					if forec_alt_3_num_mod >= B738DR_trans_lvl_mod then
						n = n / 100
						forec_alt_3_mod = "FL" .. string.format("%03d", n)
					else
						forec_alt_3_mod = string.format("%5d", n)
					end
					entry2 = ""
					if forec_dir_3 ~= "---" or forec_spd_3 ~= "---" then
						forecast_exec = 1
					end
					wind_alt_order()
				end
			end
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			if verify_to_spd ~= 0 then
				-- REJECT
				verify_to_spd = 0
				v1_set = "---"
				vr_set = "---"
				v2_set = "---"
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			-- entry THR REDUCTION ALT AGL
			local strlen = string.len(entry2)
			if B738DR_thr_cutback == 1 then
				if strlen > 0 then
					if entry2 == ">DELETE" then
						cut_alt = "----"
						cut_alt_num = B738DR_def_cut_alt
						entry2 = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 800 or n > 9999 then	-- THR RED ALT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								cut_alt = string.format("%4d", n)
								entry2 = ""
								cut_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				entry2 = nearest_apt_list_icao[5]
				if entry2 == "----" then
					entry2 = ""
				end
			else
				if nearest_apt_list_via[act_page2 - 1] == 0 then
					nearest_apt_list_via[act_page2 - 1] = 1
				else
					nearest_apt_list_via[act_page2 - 1] = 0
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				add_altn_apt(entry2, 5, 1)
			else
				if altern_apt_list_via[act_page2 - 1] == 0 then
					altern_apt_list_via[act_page2 - 1] = 1
				else
					altern_apt_list_via[act_page2 - 1] = 0
				end
			end
		elseif page_fix2 == 1 then
			if act_page2 > 0 and act_page2 < 6 then
				if entry2 == ">DELETE" then
					fix_abm_act[act_page2] = 0
					entry2 = ""
				elseif fix_abm_act[act_page2] == 0 then
					if fix_abm_wpt_idx[act_page2] > 0 then
						fix_abm_act[act_page2] = 1
					end
				elseif fix_abm_wpt_idx[act_page2] > 0 then
					if entry2 == "" then
						entry2 = fix_data[act_page2][1] .. string.format("%05.1f", fix_abm_brg[act_page2]) .. "/"
						if fix_abm_dist[act_page2] >= 1000 then
							entry2 = entry2 .. "999.9"
						elseif fix_abm_dist[act_page2] >= 100 then
							entry2 = entry2 .. string.format("%5.1f", fix_abm_dist[act_page2])
						elseif fix_abm_dist[act_page2] >= 10 then
							entry2 = entry2 .. string.format("%4.1f", fix_abm_dist[act_page2])
						else
							entry2 = entry2 .. string.format("%3.1f", fix_abm_dist[act_page2])
						end
					end
				end
			end
		elseif page_flt_plns2 == 1 then
			entry2 = sel_flt_plns2[5]
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 6LSK
function B738_fmc2_6L_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_takeoff2 ~= 0  then
			if pre_flt_pos_init == 0 then
				-- go to Pos init
				page_pos_init2 = 1
			elseif pre_flt_rte == 0 then
				-- go to Route init
				page_rte_init2 = 1
			elseif pre_flt_dep == 0 then
				-- go to Departure
				page_dep2 = 1
			elseif pre_flt_perf_init == 0 then
				-- go to Perf_init
				page_perf2 = 1
			else
				-- go to Index page
				page_init2 = 1
			end
			page_takeoff2 = 0
			display_update = 1
		elseif page_cruise2 == 1 then
			local show_crz_clb_des = -1
			if B738DR_flight_phase >= 2 then
				if crz_exec ~= 0 then
					if crz_alt_num2 > (simDR_altitude_pilot + 200) then
						show_crz_clb_des = 0	-- CRZ CLB
					else
						show_crz_clb_des = 1	-- CRZ DES
					end
				elseif B738DR_flight_phase == 3 then
					show_crz_clb_des = 0
				elseif B738DR_flight_phase == 4 then
					show_crz_clb_des = 1
				end
			end
			if show_crz_clb_des == 1 then
				-- Forecast data
				local disable_forecast = 0
				if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and crz_exec == 0 then
					disable_forecast = 1
				elseif B738DR_flight_phase < 2 or B738DR_flight_phase == 8 or (crz_exec == 0 and B738DR_flight_phase == 2) then
					disable_forecast = 1
				end
				if disable_forecast == 0 then
					page_descent_forecast2 = 1
					page_cruise2 = 0
				end
			else
				--LRC
				if engout_crz == 0 then
					if fmc_cruise_mode_mod ~= 1 then
						-- CRZ LRC
						entry2 = ""
						fmc_cruise_mode_mod = 1
						fmc_cruise_speed_mod = lrc_crz_spd
						fmc_cruise_speed_mach_mod = lrc_crz_spd_mach
						crz_spd_exec = 1
					end
				end
			end
		elseif page_dep2 == 1 then
			if B738DR_fmc_exec_lights_fo == 1 then
				if legs_num > 1 then
					if rte_exec == 1 then
						legs_delete = 0
						legs_intdir = 0
						rte_exec = 0
						copy_to_legsdata2(rte12_sel_fo)
					end
					if legs_delete == 1 then
						legs_delete = 0
						legs_intdir = 0
						copy_to_legsdata2(rte12_sel_fo)
					end
				end
				if legs_intdir == 1 and legs_intdir_rte == rte12_act then
					legs_intdir = 0
					copy_to_legsdata2(rte12_sel_fo)
				end
				
				ref_exec = 0
				des_exec = 0
				
				ref_icao_x = ref_icao
				ref_icao_lat_x = ref_icao_lat
				ref_icao_lon_x = ref_icao_lon
				ref_tns_alt_x = ref_tns_alt
				ref_tns_lvl_x = ref_tns_lvl
				ref_icao_alt_x = ref_icao_alt
				trans_alt_mod = trans_alt
				
				ref_gate_x = ref_gate
				co_route_x = co_route
				flt_num_x = flt_num
				ref_rwy2 = ref_rwy
				ref_rwy3 = ref_rwy2
				
				ref_sid2 = ref_sid
				ref_sid_tns2 = ref_sid_tns
				
				des_icao_x = des_icao
				des_rnw2 = des_rnw
				des_icao_lat_x = des_icao_lat
				des_icao_lon_x = des_icao_lon
				des_tns_alt_x = des_tns_alt
				des_tns_lvl_x = des_tns_lvl
				des_icao_alt_x = des_icao_alt
				trans_lvl_mod = trans_lvl
				
				des_app2 = des_app
				des_app_tns2 = des_app_tns
				des_star2 = des_star
				des_star_trans2 = des_star_trans
				
				arr_data_x = arr_data
				des_app_rw_only_x = des_app_rw_only
				rw_ext_dist_x = rw_ext_dist
				rw_ext_fpa_x = rw_ext_fpa
				
				des_star2_old = des_star2
				des_star_trans2_old = des_star_trans2
				des_app2_old = des_app2
				des_app_tns2_old = des_app_tns2
				
				sel_ref_rwy2 = "-----"
				sel_ref_sid2 = "------"
				sel_ref_sid_tns2 = "------"
				create_rnw_list(rte12_sel_fo)
				create_sid_list(rte12_sel_fo)
			end
		elseif page_arr2 == 1 then
			if B738DR_fmc_exec_lights_fo == 1 then
				if legs_num > 1 then
					if rte_exec == 1 then
						legs_delete = 0
						legs_intdir = 0
						rte_exec = 0
						copy_to_legsdata2(rte12_sel_fo)
					end
					if legs_delete == 1 then
						legs_delete = 0
						legs_intdir = 0
						copy_to_legsdata2(rte12_sel_fo)
					end
				end
				if legs_intdir == 1 and legs_intdir_rte == rte12_act then
					legs_intdir = 0
					copy_to_legsdata2(rte12_sel_fo)
				end
				
				ref_exec = 0
				des_exec = 0
				
				ref_icao_x = ref_icao
				ref_icao_lat_x = ref_icao_lat
				ref_icao_lon_x = ref_icao_lon
				ref_tns_alt_x = ref_tns_alt
				ref_tns_lvl_x = ref_tns_lvl
				ref_icao_alt_x = ref_icao_alt
				trans_alt_mod = trans_alt
				
				ref_gate_x = ref_gate
				co_route_x = co_route
				flt_num_x = flt_num
				ref_rwy2 = ref_rwy
				ref_rwy3 = ref_rwy2
				
				ref_sid2 = ref_sid
				ref_sid_tns2 = ref_sid_tns
				
				des_icao_x = des_icao
				des_rnw2 = des_rnw
				des_icao_lat_x = des_icao_lat
				des_icao_lon_x = des_icao_lon
				des_tns_alt_x = des_tns_alt
				des_tns_lvl_x = des_tns_lvl
				des_icao_alt_x = des_icao_alt
				trans_lvl_mod = trans_lvl
				
				des_app2 = des_app
				des_app_tns2 = des_app_tns
				des_star2 = des_star
				des_star_trans2 = des_star_trans
				
				arr_data_x = arr_data
				des_app_rw_only_x = des_app_rw_only
				rw_ext_dist_x = rw_ext_dist
				rw_ext_fpa_x = rw_ext_fpa
				
				des_star2_old = des_star2
				des_star_trans2_old = des_star_trans2
				des_app2_old = des_app2
				des_app_tns2_old = des_app_tns2
				
				sel_des_star2 = "------"
				sel_des_star_trans2 = "------"
				sel_des_app2 = "------"
				sel_des_app_tns2 = "------"
			end
		elseif page_rte_init2 == 1 then
			if B738DR_fmc_exec_lights_fo == 1 then
				if legs_num > 1 and rte12_act == rte12_sel_fo then
					copy_to_legsdata2(rte12_sel_fo)
					erase_modify(rte12_sel_fo)
					
					if rte12_sel_fo == 0 then
						ref_exec = 0
						des_exec = 0
						rte_exec = 0
					else
						ref_exec2 = 0
						des_exec2 = 0
						rte_exec2 = 0
					end
					legs_delete = 0
				elseif legs_num2 > 1 and rte12_sel_fo == 0 then
					-- ACTIVATE/ERASE
					rte_exec = 0
					exec_activate_fpln = 0
					
					copy_to_legs_data2_buf(0)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(0)
				elseif legs_num8 > 1 and rte12_sel_fo == 1 then
					-- ACTIVATE/ERASE
					rte_exec2 = 0
					exec_activate_fpln2 = 0
					
					copy_to_legs_data2_buf(1)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(1)
				end
			else
				if B738DR_no_rte2 == 0 then
					if rte12_sel_fo == 0 then
						rte12_sel_fo = 1
					else
						rte12_sel_fo = 0
					end
					act_page2 = 1
					item_sel = 0
					item_sel2 = 0
					item_sel_act = 0
					item_sel_via = 0
					item_sel_act2 = 0
					item_sel_via2 = 0
					
					if rte12_sel_fo == 0 then
						legs_step = offset_mod
						if legs_data2[legs_step][1] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step > legs_num2 or legs_step < offset_mod then
								legs_step = offset_mod
							end
							if legs_step > legs_num2 then
								legs_step = legs_num2
							end
						end
					else
						legs_step = offset_mod2
						if legs_data8[legs_step][1] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step > legs_num8 or legs_step < offset_mod2 then
								legs_step = offset_mod2
							end
							if legs_step > legs_num8 then
								legs_step = legs_num8
							end
						end
					end
				end
			end
		elseif page_legs2 == 1 then
			local ww = 0
			local uu = 0
			-- cancel MOD
			if B738DR_fmc_exec_lights_fo == 1 then
				if legs_num > 1 and rte12_act == rte12_sel_fo then
					copy_to_legsdata2(rte12_sel_fo)
					erase_modify(rte12_sel_fo)
					
					if rte12_sel_fo == 0 then
						ref_exec = 0
						des_exec = 0
						rte_exec = 0
					else
						ref_exec2 = 0
						des_exec2 = 0
						rte_exec2 = 0
					end
					legs_delete = 0
					
					if rte12_act == rte12_sel_fo then
						legs_abeam_data_n = 0
						legs_abeam_data = {}
						legs_abeam_selected = 0
						B738DR_legs_abeam_avail = 0
						B738DR_legs_abeam_num = 0
					end
				elseif legs_num2 > 1 and rte12_sel_fo == 0 then
					-- ACTIVATE/ERASE
					rte_exec = 0
					exec_activate_fpln = 0
					
					copy_to_legs_data2_buf(0)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(0)
				elseif legs_num8 > 1 and rte12_sel_fo == 1 then
					-- ACTIVATE/ERASE
					rte_exec2 = 0
					exec_activate_fpln2 = 0
					
					copy_to_legs_data2_buf(1)
					if legs_num2_buf > 1 then
						for n = 1, (legs_num2_buf + 1) do
							legs_data2_buf[n][11] = 0
							legs_data2_buf[n][10] = 0
							legs_data2_buf[n][14] = 50000
							legs_data2_buf[n][38] = ""
						end
					end
					copy_from_legs_data2_buf(1)
					
				end
				if legs_intdir == 1 and rte12_act == rte12_sel_fo and legs_intdir_rte == rte12_act then
					legs_intdir = 0
					legs_intdir_crs2 = -1
					copy_to_legsdata2(rte12_sel_fo)
				end
			else
				if new_hold == 1 and rte12_sel_fo == 0 then
					if new_hold_at == 0 then
						if item_sel2 == 0 then
							item_sel2 = find_act_route_wpt2(entry2, offset)
							if item_sel2 == -1 then
								item_sel2 = 0
								rte_add_hold_wpt(entry2, 0, rte12_sel_fo)
							end
						end
						if item_sel2 ~= 0 then
							new_hold_idx = item_sel2
							if hold_data_num2 > 0 then
								for ww = 1, hold_data_num2 do
									if hold_data2[ww] == new_hold_idx + 1 then
										uu = 1
									end
								end
							end
							if legs_data2[new_hold_idx][21] == 0 or legs_data2[new_hold_idx][21] == 1 then
								uu = 1
							end
							
							if uu == 0 then
								-- add new hold
								rte_add_hold(item_sel2, 0, rte12_sel_fo)
								page_hold2 = 1
								page_legs2 = 0
								entry2 = ""
								if rte12_act == rte12_sel_fo then
									hold_exec = 1
								end
								act_page2 = 1
								new_hold = 0
								item_sel2 = 0
								item_sel_act2 = 0
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				elseif new_hold2 == 1 and rte12_sel_fo == 1 then
					if new_hold_at2 == 0 then
						if item_sel2 == 0 then
							item_sel2 = find_act_route_wpt2(entry2, offset)
							if item_sel2 == -1 then
								item_sel2 = 0
								rte_add_hold_wpt(entry2, 0, rte12_sel_fo)
							end
						end
						if item_sel2 ~= 0 then
							new_hold_idx2 = item_sel2
							if hold_data_num4 > 0 then
								for ww = 1, hold_data_num4 do
									if hold_data4[ww] == new_hold_idx2 + 1 then
										uu = 1
									end
								end
							end
							if legs_data8[new_hold_idx2][21] == 0 or legs_data8[new_hold_idx2][21] == 1 then
								uu = 1
							end
							
							if uu == 0 then
								-- add new hold
								rte_add_hold(item_sel2, 0, rte12_sel_fo)
								page_hold2 = 1
								page_legs2 = 0
								entry2 = ""
								if rte12_act == rte12_sel_fo then
									hold_exec2 = 1
								end
								act_page2 = 1
								new_hold2 = 0
								item_sel2 = 0
								item_sel_act2 = 0
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				elseif B738DR_no_rte2 == 0 then
					if rte12_sel_fo == 0 then
						rte12_sel_fo = 1
					else
						rte12_sel_fo = 0
					end
					act_page2 = 1
					item_sel = 0
					item_sel2 = 0
					item_sel_act = 0
					item_sel_via = 0
					item_sel_act2 = 0
					item_sel_via2 = 0
					
					if rte12_sel_fo == 0 then
						legs_step2 = offset_mod
						if legs_data2[legs_step2][1] == "DISCONTINUITY" then
							legs_step2 = legs_step2 + 1
							if legs_step2 > legs_num2 or legs_step2 < offset_mod then
								legs_step2 = offset_mod
							end
							if legs_step2 > legs_num2 then
								legs_step2 = legs_num2
							end
						end
					else
						legs_step2 = offset_mod2
						if legs_data8[legs_step2][1] == "DISCONTINUITY" then
							legs_step2 = legs_step2 + 1
							if legs_step2 > legs_num8 or legs_step2 < offset_mod2 then
								legs_step2 = offset_mod2
							end
							if legs_step2 > legs_num8 then
								legs_step2 = legs_num8
							end
						end
					end
				end
			end
		elseif page_hold2 == 1 then
			if hold_exec == 1 and rte12_sel_fo == 0 then
				if B738DR_hold_term == 0 or hold_offset_idx2 ~= offset then	--or rte12_act ~= rte12_sel_fo then
					-- ERASE
					copy_to_legsdata2(rte12_sel_fo)
					entry2 = ""
					hold_exec = 0
					new_hold_idx = 0
					new_hold_act_page = 0
					item_sel2 = 0
					item_sel_act2 = 0
					item_sel_via2 = 0
				else
					B738DR_hold_term = 0
					hold_exec = 0
					new_hold_idx = 0
					new_hold_act_page = 0
				end
			elseif hold_exec2 == 1 and rte12_sel_fo == 1 then
				if B738DR_hold_term == 0 or hold_offset_idx8 ~= offset then	--or rte12_act ~= rte12_sel_fo then
					-- ERASE
					copy_to_legsdata2(rte12_sel_fo)
					entry2 = ""
					hold_exec2 = 0
					new_hold_idx2 = 0
					new_hold_act_page2 = 0
					item_sel2 = 0
					item_sel_act2 = 0
					item_sel_via2 = 0
				else
					B738DR_hold_term = 0
					hold_exec2 = 0
					new_hold_idx2 = 0
					new_hold_act_page2 = 0
				end
			else
				-- show Legs page with Hold select
				act_page2 = 1
				page_hold2 = 0
				page_legs2 = 1
				if rte12_sel_fo == 0 then
					new_hold = 1
				else
					new_hold2 = 1
				end
			end
		elseif page_ident2 ~= 0 then
			-- go to Index page
			page_ident2 = 0
			page_init2 = 1
			display_update = 1
		elseif page_approach2 == 1 then
			-- go to Index page
			page_approach2 = 0
			page_init2 = 1
			display_update = 1
		elseif page_n1_limit2 == 1 and disable_N1_6L == 0 then
			if in_flight_mode == 0 then
				-- go to Perf init page
				page_n1_limit2 = 0
				page_perf2 = 1
				display_update = 1
			else
				-- select / deselect CLB-1
				if entry2 == ">DELETE" then
					clb = "<SEL>"
					clb_1 = "     "
					clb_2 = "     "
					entry2 = ""
				-- elseif clb_1 == "<SEL>" then
					-- clb = "<SEL>"
					-- clb_1 = "     "
					-- clb_2 = "     "
				else
					clb = "     "
					clb_1 = "<SEL>"
					clb_2 = "     "
				end
				sel_clb_thr = 1
				flight_phase_old2 = -1
			end
		elseif page_perf2 ~= 0 then
			if page_perf2 == 1 and perf_exec == 3 then
				gw_mod = gw
				gw_lbs_mod = gw_lbs
				gw_kgs_mod = gw_kgs
				zfw_mod = zfw
				zfw_lbs_mod = zfw_lbs
				zfw_kgs_mod = zfw_kgs
				plan_weight_mod = plan_weight
				plan_weight_kgs_mod = plan_weight_kgs
				plan_weight_lbs_mod = plan_weight_lbs
				reserves_mod = reserves
				reserves_kgs_mod = reserves_kgs
				reserves_lbs_mod = reserves_lbs
				cost_index_mod = cost_index
				crz_wind_dir_mod = crz_wind_dir
				crz_wind_spd_mod = crz_wind_spd
				isa_dev_f_mod = isa_dev_f
				isa_dev_c_mod = isa_dev_c
				tc_oat_c_mod = tc_oat_c
				tc_oat_f_mod = tc_oat_f
				perf_exec = 2
				if crz_exec ~= 0 then
					crz_alt = crz_alt_old
					crz_exec = 0
					exec1_light = 0
					crz_alt_num2 = 0
					crz_alt_old = "     "
				end
			elseif crz_exec ~= 0 then
				crz_alt = crz_alt_old
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
			else
				-- go to Index page
				page_perf2 = 0
				page_init2 = 1
			end
			display_update = 1
		elseif page_pos_init2 == 1 or page_pos_init2 == 3 then
			-- go to Index page
			page_pos_init2 = 0
			page_init2 = 1
			display_update = 1
		elseif page_descent2 == 1 then
			page_descent2 = 0
			page_descent_forecast2 = 1
		elseif page_init2 == 1 then
			-- go to OFFSET
			page_init2 = 0
			page_offset2 = 1
		elseif page_ref_nav_data2 == 1 then
			-- go to INDEX
			page_ref_nav_data2 = 0
			page_init2 = 1
		elseif page_ref_nav_data_wpt2 == 1 then
			-- go to INDEX
			page_ref_nav_data_wpt2 = 0
			page_init2 = 1
		elseif page_offset2 == 1 and (offset_act_mod >= 1 and offset_act_mod <= 4) then
			offset_dist_mod = offset_dist
			offset_side_mod = offset_side
			offset_start_mod = offset_start
			offset_end_mod = offset_end
			offset_start_def_mod = offset_start_def
			offset_end_def_mod = offset_end_def
			offset_act_mod = offset_act
			legs_delete = 0
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
				if fmc_climb_mode_mod ~= 2 then
					-- MAX ANGLE
					entry2 = ""
					fmc_climb_mode_mod = 2
					fmc_climb_speed_mach_mod = 0.74	--0.76		-- temporary
					if B738DR_pfd_flaps_up == 0 then
						fmc_climb_speed_mod = 250	--270
					else
						fmc_climb_speed_mod = math.max(B738DR_pfd_flaps_up + 20, 250)
					end
					clb_spd_exec = 1
				end
			end
		elseif page_ac2 == 1 then
			-- go to MENU
			page_ac2 = 0
			page_init2 = 1
			display_update = 1
		elseif page_ac_atis2 == 1 then
			-- go to ACARS menu
			page_ac2 = 1
			page_ac_atis2 = 0
			display_update = 1
		elseif page_ac_msg2 == 1 then
			page_ac_msg2 = 0
			page_ac_atis2 = 1
		elseif page_altern_apt2 == 1 then
			page_altern_apt2 = 0
			page_init2 = 1
			act_page2 = 1
		elseif page_nearest_apt2 == 1 then
			page_nearest_apt2 = 0
			page_init2 = 1
			act_page2 = 1
		elseif page_nav_status2 > 0 then
			page_init2 = 1
			page_nav_status2 = 0
			act_page2 = 1
		elseif page_progress2 == 4 then
			local len_entry = string.len(entry2)
			local num_entry = tonumber(entry2)
			if entry2 == ">DELETE" then
				app_rnp = ""
				app_rnp_num = 0.30
				entry2 = ""
			elseif len_entry < 5 then
				if num_entry == nil then
					add_fmc_msg(INVALID_INPUT, 1)
				elseif num_entry >= 0.05 and num_entry <= 1 then
					app_rnp = string.format("%04.2f", num_entry)
					app_rnp_num = tonumber(app_rnp)
					entry2 = ""
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			-- elseif entry2 == "0.10" or entry2 == "0.1" then
				-- app_rnp = "0.10"
				-- app_rnp_num = 0.10
				-- entry2 = ""
			-- elseif entry2 == "0.30" or entry2 == "0.3" then
				-- app_rnp = "0.30"
				-- app_rnp_num = 0.30
				-- entry2 = ""
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_dl_acars2 == 1 then
			page_dl_acars2 = 0
			page_dl_menu2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_dl_requests2 == 1 then
			page_dl_requests2 = 0
			page_dl_acars2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_dl_wxr_req2 == 1 then
			page_dl_wxr_req2 = 0
			page_dl_requests2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_dl_wxr_info2 == 1 then
			page_dl_wxr_info2 = 0
			page_dl_requests2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_dl_fltlog2 == 1 then
			page_dl_fltlog2 = 0
			page_dl_acars2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_dl_sensors2 == 1 then
			page_dl_sensors2 = 0
			page_dl_fltlog2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_descent_forecast2 == 1 then
			if rte12_act == 0 then
				if wind_data_des_num == 0 then
					entry2 = ">NONE WIND DATA"
				else
					uplink_wind_des(rte12_act)
				end
			elseif rte12_act == 1 then
				if wind_data_des_num7 == 0 then
					entry2 = ">NONE WIND DATA"
				else
					uplink_wind_des(rte12_act)
				end
			end
		elseif page_flt_plns2 == 1 then
			-- go to INDEX
			page_flt_plns2 = 0
			page_init2 = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 1RSK
function B738_fmc2_1R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
				if sel_ref_rwy2 == "-----" then
					if ref_rwy_sel_fo[1] ~= "" then
						if ref_rwy2 == ref_rwy_sel_fo[1] then
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
						else
							ref_rwy2 = ref_rwy_sel_fo[1]
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							ref_rwy3 = ref_rwy2
							ref_rwy_exec = 1
						end
						act_page2 = 1
					end
				else
					ref_rwy2 = "-----"
					ref_rwy3 = "-----"
					sel_ref_rwy2 = ref_rwy2
					create_sid_list(rte12_sel_fo)
					create_rnw_list(rte12_sel_fo)
					act_page2 = 1
					ref_rwy_exec = 1
				end
				rte_add_dep_arr(rte12_sel_fo)
				
				-- check SID for selected runway
				if rte12_act == rte12_sel_fo then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			elseif rte12_sel_fo == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
				if sel_ref_rwy8 == "-----" then
					if ref_rwy_sel_fo[1] ~= "" then
						if ref_rwy8 == ref_rwy_sel_fo[1] then
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
						else
							ref_rwy8 = ref_rwy_sel_fo[1]
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							ref_rwy9 = ref_rwy8
							ref_rwy_exec = 1
						end
						act_page2 = 1
					end
				else
					ref_rwy8 = "-----"
					ref_rwy9 = "-----"
					sel_ref_rwy8 = ref_rwy8
					create_sid_list(rte12_sel_fo)
					create_rnw_list(rte12_sel_fo)
					act_page2 = 1
					ref_rwy_exec = 1
				end
				rte_add_dep_arr(rte12_sel_fo)
				
				-- check SID for selected runway
				if rte12_act == rte12_sel_fo then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			end
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_app2 == "------" then
					if des_app_sel_fo[1] ~= "" then
						if des_app2 == des_app_sel_fo[1] then
							if app_list_num == 1 then
								des_app2 = "------"
								sel_des_app2 = des_app2
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								create_app_list(rte12_sel_fo)
								act_page2 = 1
								des_app_exec = 1
							else
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							end
						else
							des_app2 = des_app_sel_fo[1]
							sel_des_app2 = des_app2
							if string.sub(sel_des_app2, 1, 2) == "RW" then
								des_app_rw_only_x = 1
								des_star_trans2 = "------"
								sel_des_star_trans2 = des_star_trans2
								create_star_list(rte12_sel_fo)
							else
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
							end
							if sel_des_star2 == "------" then
								create_star_list(rte12_sel_fo)
							end
							des_app_tns2 = "------"
							sel_des_app_tns2 = des_app_tns2
							des_app_exec = 1
							create_app_tns_list(rte12_sel_fo)
						end
						act_page2 = 1
					end
				else
					des_app2 = "------"
					sel_des_app2 = des_app2
					des_app_rw_only_x = 0
					rw_ext_dist_x = "--.-"
					rw_ext_fpa_x = "-.--"
					if sel_des_star2 == "------" then
						create_star_list(rte12_sel_fo)
					end
					des_app_tns2 = "------"
					sel_des_app_tns2 = des_app_tns2
					create_app_list(rte12_sel_fo)
					act_page2 = 1
					des_app_exec = 1
				end
			else
				if sel_des_app8 == "------" then
					if des_app_sel_fo[1] ~= "" then
						if des_app8 == des_app_sel_fo[1] then
							if app_list_num == 1 then
								des_app8 = "------"
								sel_des_app8 = des_app8
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								create_app_list(rte12_sel_fo)
								act_page2 = 1
								des_app_exec = 1
							else
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							end
						else
							des_app8 = des_app_sel_fo[1]
							sel_des_app8 = des_app8
							if string.sub(sel_des_app8, 1, 2) == "RW" then
								des_app_rw_only7_x = 1
								des_star_trans8 = "------"
								sel_des_star_trans8 = des_star_trans8
								create_star_list(rte12_sel_fo)
							else
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
							end
							if sel_des_star8 == "------" then
								create_star_list(rte12_sel_fo)
							end
							des_app_tns8 = "------"
							sel_des_app_tns8 = des_app_tns8
							des_app_exec = 1
							create_app_tns_list(rte12_sel_fo)
						end
						act_page2 = 1
					end
				else
					des_app8 = "------"
					sel_des_app8 = des_app8
					des_app_rw_only7_x = 0
					rw_ext_dist7_x = "--.-"
					rw_ext_fpa7_x = "-.--"
					if sel_des_star8 == "------" then
						create_star_list(rte12_sel_fo)
					end
					des_app_tns8 = "------"
					sel_des_app_tns8 = des_app_tns8
					create_app_list(rte12_sel_fo)
					act_page2 = 1
					des_app_exec = 1
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_dep_arr2 == 1 then
			-- Reference ARR
			if ref_icao_x ~= "----" and des_icao_x ~= "****" and ref_icao_x ~= "ORGN" then
				
				local apt_ok = 0
				local xy = 0
				rte12_sel_fo = 0
				
				if arr_data_x == 0 then
					-- DES ICAO
					if legs_num2 > 1 then	--and des_icao_x ~= des_icao then
						-- -- delete all STAR/APP
						if first_star_idx_mod > 0 and offset < first_star_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_star_idx_mod
							if first_star_idx_mod ~= 0 and first_star_idx_mod < legs_num2 then
								if legs_data2[first_star_idx_mod][19] == 4 then
									apt_ok = first_star_idx_mod + 1
									legs_data2[first_star_idx_mod][19] = 0
									legs_data2[first_star_idx_mod][4] = 0
									legs_data2[first_star_idx_mod][5] = 0
									legs_data2[first_star_idx_mod][6] = 0
									legs_data2[first_star_idx_mod][9] = ""
									legs_data2[first_star_idx_mod][21] = -1
									legs_data2[first_star_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						elseif first_app_idx_mod > 0 and offset < first_app_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_app_idx_mod
							if first_app_idx_mod ~= 0 and first_app_idx_mod < legs_num2 then
								if legs_data2[first_app_idx_mod][19] == 9 then
									apt_ok = first_app_idx_mod + 1
									legs_data2[first_app_idx_mod][19] = 0
									legs_data2[first_app_idx_mod][4] = 0
									legs_data2[first_app_idx_mod][5] = 0
									legs_data2[first_app_idx_mod][6] = 0
									legs_data2[first_app_idx_mod][9] = ""
									legs_data2[first_app_idx_mod][21] = -1
									legs_data2[first_app_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						elseif last_sid_idx_mod > 0 and offset < last_sid_idx_mod and last_sid_idx_mod <= legs_num2 then
							clr_star_app(0)
							rte_copy(legs_num2 + 1, 0)
							rte_paste(last_sid_idx_mod + 1, 0)
							calc_rte_enable2 = 1
						elseif offset <= legs_num2 then
							clr_star_app(0)
							rte_copy(legs_num2 + 1, 0)
							rte_paste(offset, 0)
							calc_rte_enable2 = 1
						end
						last_sid_idx_mod = legs_num2
						if rte12_act == rte12_sel_fo then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num2 + 1
						legs_data2[apt_ok] = {}
						legs_data2[apt_ok][1] = ref_icao_x
						legs_data2[apt_ok][2] = 0		-- brg
						legs_data2[apt_ok][3] = 0		-- distance
						legs_data2[apt_ok][4] = 0		-- speed
						legs_data2[apt_ok][5] = 0		-- altitude
						legs_data2[apt_ok][6] = 0	-- altitude type
						legs_data2[apt_ok][7] = ref_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[apt_ok][8] = ref_icao_lon_x	--des_icao_lon		-- longitude
						legs_data2[apt_ok][9] = ""			-- via id
						legs_data2[apt_ok][10] = 0		-- calc speed
						legs_data2[apt_ok][11] = 0		-- calc altitude
						legs_data2[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data2[apt_ok][13] = 0
						legs_data2[apt_ok][14] = 0		-- rest alt
						legs_data2[apt_ok][15] = 0		-- last fuel
						legs_data2[apt_ok][16] = ""
						legs_data2[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data2[apt_ok][20] = 0
						legs_data2[apt_ok][21] = -1
						legs_data2[apt_ok][22] = ""
						legs_data2[apt_ok][23] = 0
						legs_data2[apt_ok][24] = 0
						legs_data2[apt_ok][25] = 0
						legs_data2[apt_ok][26] = 0
						legs_data2[apt_ok][27] = ""
						legs_data2[apt_ok][28] = ""
						legs_data2[apt_ok][29] = ""
						legs_data2[apt_ok][30] = ""
						legs_data2[apt_ok][31] = ""
						legs_data2[apt_ok][32] = 0
						legs_data2[apt_ok][33] = ""
						legs_data2[apt_ok][34] = "DEST"
						legs_data2[apt_ok][35] = ""
						legs_data2[apt_ok][36] = 9
						legs_data2[apt_ok][37] = 0
						legs_data2[apt_ok][38] = ""
						legs_data2[apt_ok][39] = ""
						legs_data2[apt_ok][40] = 0
						legs_data2[apt_ok][41] = 0
						legs_data2[apt_ok][42] = 0
						legs_data2[apt_ok][43] = ""
						legs_data2[apt_ok][44] = 0
						legs_data2[apt_ok][45] = 0
						legs_data2[apt_ok][46] = 0
						legs_data2[apt_ok][47] = 0
						legs_data2[apt_ok][48] = ""
						legs_data2[apt_ok][49] = 0
						legs_data2[apt_ok][50] = 0
						legs_data2[apt_ok][51] = 0
						legs_data2[apt_ok][52] = 0
						legs_data2[apt_ok][53] = 0
						legs_data2[apt_ok][54] = 0
						
						if rte12_act == 0 then
							offset_mod = offset
						else
							offset_mod = 2
						end
						if offset_mod > legs_num2 then
							offset_mod = legs_num2
						end
						
					elseif legs_num2 > 0 then
						legs_data2[legs_num2 + 1][1] = ref_icao_x	--des_icao_x
						legs_data2[legs_num2 + 1][7] = ref_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[legs_num2 + 1][8] = ref_icao_lon_x	--des_icao_lon		-- longitude
					end
					
					if rte12_act == 0 then
						legs_delete = 1
					end
					des_app_rw_only_x = 0
					des_star2 = "------"
					des_star_trans2 = "------"
					des_app2 = "------"
					des_app_tns2 = "------"
				end
				arr_data_x = 1
				page_arr2 = 1
				find_ref_rnw_data(1, 0)
				read_ref_list_icao(ref_icao_x, 0)
				read_des_list_icao(ref_icao_x, 0)
				page_dep_arr2 = 0
				
				sel_des_star2 = "------"
				sel_des_star_trans2 = "------"
				sel_des_app2 = "------"
				sel_des_app_tns2 = "------"
				
				create_star_list(0)
				create_star_tns_list(0)
				create_app_list(0)
				create_app_tns_list(0)
				
			end
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 then
				-- entry2 Destination airport ICAO
				local xy = 0
				local apt_ok = 0
				local dest_available = 1
				
				if dest_available ~= 0 then
					if entry2 == ">DELETE" then
						entry2 = INVALID_DELETE
					else
						if rte12_sel_fo == 0 then
							if string.len(entry2) == 4 then
								des_app_from_apt = 0
								
								if des_apt_exist(entry2, 0) then
									des_icao_x = entry2
									update_rnw_list(0)
									
									des_tns_alt_x = 0
									des_tns_lvl_x = 0
									des_icao_alt_x = 0
									
									des_app2 = "------"
									des_app_tns2 = "------"
									des_star2 = "------"
									des_star_trans2 = "------"
									
									trans_lvl_mod = "-----"
									
									if rte12_act ~= 0 then
										des_icao = "****"
										des_star = "------"
										des_star_trans = "------"
										des_app = "------"
										des_app_tns = "------"
										rw_ext_dist_x = "--.-"
										rw_ext_fpa_x = "-.--"
										
										des_icao_lat = 0
										des_icao_lon = 0
										des_tns_alt = 0
										des_tns_lvl = 0
										des_icao_alt = 0
										arr_data = 0
										des_app_rw_only = 0
										trans_lvl = "-----"
									end
									
									if apt_exist(entry2) == true then
										des_icao_lat_x = icao_latitude
										des_icao_lon_x = icao_longitude
										des_tns_alt_x = icao_tns_alt
										des_tns_lvl_x = icao_tns_lvl
										des_icao_alt_x = 2500
										B738DR_find_icao = entry2
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt_x = B738DR_find_icao_height * 3.280839895
										end
									else
										des_icao_alt_x = 2500
										B738DR_find_icao = entry2
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt_x = B738DR_find_icao_height * 3.280839895
										end
										des_tns_alt_x = 0
										des_tns_lvl_x = 0
									end
									if des_tns_lvl_x == 0 then
										des_tns_lvl_x = des_tns_alt_x
									end
									if des_tns_lvl_x == 0 then
										trans_lvl_mod = "-----"
									else
										apt_ok = des_tns_lvl_x / 100
										trans_lvl_mod = "FL" .. string.format("%03d", apt_ok)
									end
									
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
									
									if legs_num ~= 0 and rte12_act == rte12_sel_fo then
										des_exec = 1
									end
									
									des_app2 = "------"
									des_app_tns2 = "------"
									des_star2 = "------"
									des_star_trans2 = "------"
									
									if ref_icao_x == "----" then
										ref_icao_x = "ORGN"
										ref_icao_lat_x = simDR_latitude
										ref_icao_lon_x = simDR_longitude
										ref_gate_x = ref_gate
										ref_tns_alt_x = 0
										ref_tns_lvl_x = 0
										trans_alt_mod = "-----"
										ref_icao_alt_x = simDR_altitude_pilot
										ref_gate_x = "-----"
										
										ref_rwy2 = "-----"
										ref_sid2 = "------"
										ref_sid_tns2 = "------"
										
										ref_rnw_list = {}
										ref_rnw_list_num = 0
										ref_rnw_list2 = {}
										ref_rnw_list_num2 = 0
										sid_list = {}
										sid_list_num = 0
									end
									
									if legs_num2 == 0 then
										if ref_icao_x == "ORGN" then
											-- REF ICAO
											legs_data2[1] = {}
											legs_data2[1][1] = ref_icao_x
											legs_data2[1][2] = 0		-- brg
											legs_data2[1][3] = 0		-- distance
											legs_data2[1][4] = 0		-- speed
											legs_data2[1][5] = ref_icao_alt_x		-- altitude
											legs_data2[1][6] = 0	-- altitude type
											legs_data2[1][7] = ref_icao_lat_x		-- latitude
											legs_data2[1][8] = ref_icao_lon_x	--ref_icao_lon		-- longitude
											legs_data2[1][9] = ""			-- via id
											legs_data2[1][10] = 0		-- calc speed
											legs_data2[1][11] = ref_icao_alt_x		-- calc altitude
											legs_data2[1][12] = 0		-- calc altitude vnav pth
											legs_data2[1][13] = 0
											legs_data2[1][14] = 0		-- rest alt
											legs_data2[1][15] = 0		-- last fuel
											legs_data2[1][16] = ""
											legs_data2[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
											legs_data2[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
											legs_data2[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
											legs_data2[1][20] = 0
											legs_data2[1][21] = -1
											legs_data2[1][22] = ""
											legs_data2[1][23] = 0
											legs_data2[1][24] = 0
											legs_data2[1][25] = 0
											legs_data2[1][26] = 0
											legs_data2[1][27] = ""
											legs_data2[1][28] = ""
											legs_data2[1][29] = ""
											legs_data2[1][30] = ""
											legs_data2[1][31] = ""
											legs_data2[1][32] = 0
											legs_data2[1][33] = ""
											legs_data2[1][34] = ""
											legs_data2[1][35] = ""
											legs_data2[1][36] = 9
											legs_data2[1][37] = 0
											legs_data2[1][38] = ""
											legs_data2[1][39] = ""
											legs_data2[1][40] = 0
											legs_data2[1][41] = 0
											legs_data2[1][42] = 0
											legs_data2[1][43] = ""
											legs_data2[1][44] = 0
											legs_data2[1][45] = 0
											legs_data2[1][46] = 0
											legs_data2[1][47] = 0
											legs_data2[1][48] = ""
											legs_data2[1][49] = 0
											legs_data2[1][50] = 0
											legs_data2[1][51] = 0
											legs_data2[1][52] = 0
											legs_data2[1][53] = 0
											legs_data2[1][54] = 0
										end
										legs_num2 = 1
										apt_ok = 2
									else
										if offset_mod == 0 then
											apt_ok = 1
										else
											if offset_mod < first_star_idx_mod then
												rte_copy(legs_num2 + 1, rte12_sel_fo)
												apt_ok = first_star_idx_mod
												if first_star_idx_mod ~= 0 and first_star_idx_mod < legs_num2 then
													if legs_data2[first_star_idx_mod][19] == 4 then
														apt_ok = first_star_idx_mod + 1
														legs_data2[first_star_idx_mod][19] = 0
														legs_data2[first_star_idx_mod][4] = 0
														legs_data2[first_star_idx_mod][5] = 0
														legs_data2[first_star_idx_mod][6] = 0
														legs_data2[first_star_idx_mod][9] = ""
														legs_data2[first_star_idx_mod][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel_fo)
											elseif offset_mod < first_app_idx_mod then
												rte_copy(legs_num2 + 1, rte12_sel_fo)
												apt_ok = first_app_idx_mod
												if first_app_idx_mod ~= 0 and first_app_idx_mod < legs_num2 then
													if legs_data2[first_app_idx_mod][19] == 9 then
														apt_ok = first_app_idx_mod + 1
														legs_data2[first_app_idx_mod][19] = 0
														legs_data2[first_app_idx_mod][4] = 0
														legs_data2[first_app_idx_mod][5] = 0
														legs_data2[first_app_idx_mod][6] = 0
														legs_data2[first_app_idx_mod][9] = ""
														legs_data2[first_app_idx_mod][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel_fo)
											elseif first_app_idx_mod ~= 0 then
												rte_copy(legs_num2 + 1, rte12_sel_fo)
												rte_paste(offset_mod + 1, rte12_sel_fo)
											elseif first_star_idx_mod ~= 0 then
												rte_copy(legs_num2 + 1, rte12_sel_fo)
												rte_paste(offset_mod + 1, rte12_sel_fo)
											end
											
											apt_ok = offset_mod
											
										end
										
										if ref_icao_x == "ORGN" then
											apt_ok = 1
										end
										
										if apt_ok <= legs_num2 then
											for xy = apt_ok, legs_num2 do
											-- change STAR,APP to route
												if legs_data2[xy][19] == 2 or legs_data2[xy][19] == 4
												or legs_data2[xy][19] == 7 or legs_data2[xy][19] == 8 
												or legs_data2[xy][19] == 9 then
													legs_data2[xy][19] = 0	-- wpt type STAR, APP
													legs_data2[xy][4] = 0	-- spd rest
													legs_data2[xy][5] = 0	-- alt rest
													legs_data2[xy][6] = 0	-- alt type rest
													legs_data2[xy][9] = ""		--via
												elseif (legs_data2[xy][19] == 1 or legs_data2[xy][19] == 3) then
													legs_data2[xy][19] = 0	-- wpt type STAR, APP
													legs_data2[xy][9] = ""		--via
												end
											end
										end
										last_sid_idx_mod = 0
										if rte12_act == rte12_sel_fo then
											if rte12_act == 0 then
												idx_ed_mod = 0
												rnav_idx_first_mod = 0
												rnav_idx_last_mod = 0
											else
												idx_ed_mod2 = 0
												rnav_idx_first_mod2 = 0
												rnav_idx_last_mod2 = 0
											end
										end
										apt_ok = legs_num2 + 1
									end
									
									-- DES ICAO
									legs_data2[apt_ok] = {}
									legs_data2[apt_ok][1] = des_icao_x
									legs_data2[apt_ok][2] = 0		-- brg
									legs_data2[apt_ok][3] = 0		-- distance
									legs_data2[apt_ok][4] = 0		-- speed
									legs_data2[apt_ok][5] = 0		-- altitude
									legs_data2[apt_ok][6] = 0	-- altitude type
									legs_data2[apt_ok][7] = des_icao_lat_x		-- latitude
									legs_data2[apt_ok][8] = des_icao_lon_x		-- longitude
									legs_data2[apt_ok][9] = ""			-- via id
									legs_data2[apt_ok][10] = 0		-- calc speed
									legs_data2[apt_ok][11] = 0		-- calc altitude
									legs_data2[apt_ok][12] = 0		-- calc altitude vnav pth
									legs_data2[apt_ok][13] = 0
									legs_data2[apt_ok][14] = 0		-- rest alt
									legs_data2[apt_ok][15] = 0		-- last fuel
									legs_data2[apt_ok][16] = ""
									legs_data2[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
									legs_data2[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data2[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data2[apt_ok][20] = 0
									legs_data2[apt_ok][21] = -1
									legs_data2[apt_ok][22] = ""
									legs_data2[apt_ok][23] = 0
									legs_data2[apt_ok][24] = 0
									legs_data2[apt_ok][25] = 0
									legs_data2[apt_ok][26] = 0
									legs_data2[apt_ok][27] = ""
									legs_data2[apt_ok][28] = ""
									legs_data2[apt_ok][29] = ""
									legs_data2[apt_ok][30] = ""
									legs_data2[apt_ok][31] = ""
									legs_data2[apt_ok][32] = 0
									legs_data2[apt_ok][33] = ""
									legs_data2[apt_ok][34] = "DEST"
									legs_data2[apt_ok][35] = ""
									legs_data2[apt_ok][36] = 9
									legs_data2[apt_ok][37] = 0
									legs_data2[apt_ok][38] = ""
									legs_data2[apt_ok][39] = ""
									legs_data2[apt_ok][40] = 0
									legs_data2[apt_ok][41] = 0
									legs_data2[apt_ok][42] = 0
									legs_data2[apt_ok][43] = ""
									legs_data2[apt_ok][44] = 0
									legs_data2[apt_ok][45] = 0
									legs_data2[apt_ok][46] = 0
									legs_data2[apt_ok][47] = 0
									legs_data2[apt_ok][48] = ""
									legs_data2[apt_ok][49] = 0
									legs_data2[apt_ok][50] = 0
									legs_data2[apt_ok][51] = 0
									legs_data2[apt_ok][52] = 0
									legs_data2[apt_ok][53] = 0
									legs_data2[apt_ok][54] = 0
									
									if rte12_act == rte12_sel_fo then
										offset_mod = offset
									else
										offset_mod = 2
									end
									if offset_mod > legs_num2 then
										offset_mod = legs_num2
									end
									
									if legs_num ~= 0 then
										if rte12_act == rte12_sel_fo then
											legs_delete = 1
										end
									end
									
									if exec_activate_fpln ~= 0 then
										rte_exec = 0
										exec_activate_fpln = 0
										
										copy_to_legs_data2_buf(0)
										if legs_num2_buf > 1 then
											for n = 1, (legs_num2_buf + 1) do
												legs_data2_buf[n][11] = 0
												legs_data2_buf[n][10] = 0
												legs_data2_buf[n][14] = 50000
												legs_data2_buf[n][38] = ""
											end
										end
										copy_from_legs_data2_buf(0)
										
									end
									
									calc_rte_enable2 = 1
									
									create_fpln(rte12_sel_fo)
									
									arr_data_x = 0
									find_des_rnw_data(1, 0)
									read_des_list_icao(des_icao_x, 0)
									create_rnw_list(rte12_sel_fo)
									create_star_list(rte12_sel_fo)
									create_app_list(rte12_sel_fo)
									
									entry2 = ""
									
								else
									add_fmc_msg(NOT_IN_DATABASE, 1)
								end
							elseif entry2 == "" and des_icao_x ~= "****" then
								entry2 = des_icao_x
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						else
							if string.len(entry2) == 4 then
								des_app_from_apt = 0
								
								if des_apt_exist(entry2, 1) then
									des_icao7_x = entry2
									update_rnw_list(1)
									
									des_tns_alt7_x = 0
									des_tns_lvl7_x = 0
									des_icao_alt7_x = 0
									
									des_app8 = "------"
									des_app_tns8 = "------"
									des_star8 = "------"
									des_star_trans8 = "------"
									
									trans_lvl_mod2 = "-----"
									
									if rte12_act ~= 1 then
										des_icao7 = "****"
										des_star7 = "------"
										des_star_trans7 = "------"
										des_app7 = "------"
										des_app_tns7 = "------"
										rw_ext_dist7_x = "--.-"
										rw_ext_fpa7_x = "-.--"
										
										des_icao_lat7 = 0
										des_icao_lon7 = 0
										des_tns_alt7 = 0
										des_tns_lvl7 = 0
										des_icao_alt7 = 0
										arr_data7 = 0
										des_app_rw_only7 = 0
										trans_lvl7 = "-----"
									end
									
									if apt_exist(entry2) == true then
										des_icao_lat7_x = icao_latitude
										des_icao_lon7_x = icao_longitude
										des_tns_alt7_x = icao_tns_alt
										des_tns_lvl7_x = icao_tns_lvl
										des_icao_alt7_x = 2500
										B738DR_find_icao = entry2
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt7_x = B738DR_find_icao_height * 3.280839895
										end
									else
										des_icao_alt7_x = 2500
										B738DR_find_icao = entry2
										B738DR_find_icao_cmd = 1
										if B738DR_find_icao_cmd == 2 then
											des_icao_alt7_x = B738DR_find_icao_height * 3.280839895
										end
										des_tns_alt7_x = 0
										des_tns_lvl7_x = 0
									end
									if des_tns_lvl7_x == 0 then
										des_tns_lvl7_x = des_tns_alt7_x
									end
									if des_tns_lvl7_x == 0 then
										trans_lvl_mod2 = "-----"
									else
										apt_ok = des_tns_lvl7_x / 100
										trans_lvl_mod2 = "FL" .. string.format("%03d", apt_ok)
									end
									
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
									
									if legs_num ~= 0 then
										des_exec2 = 1
									end
									
									des_app8 = "------"
									des_app_tns8 = "------"
									des_star8 = "------"
									des_star_trans8 = "------"
									
									if ref_icao7_x == "----" then
										ref_icao7_x = "ORGN"
										ref_icao_lat7_x = simDR_latitude
										ref_icao_lon7_x = simDR_longitude
										ref_gate7_x = ref_gate
										ref_tns_alt7_x = 0
										ref_tns_lvl7_x = 0
										trans_alt_mod2 = "-----"
										ref_icao_alt7_x = simDR_altitude_pilot
										ref_gate7_x = "-----"
										
										ref_rwy8 = "-----"
										ref_sid8 = "------"
										ref_sid_tns8 = "------"
										
										ref_rnw_list = {}
										ref_rnw_list_num = 0
										ref_rnw_list2 = {}
										ref_rnw_list_num2 = 0
										sid_list = {}
										sid_list_num = 0
									end
									
									if legs_num8 == 0 then
										if ref_icao7_x == "ORGN" then
											-- REF ICAO
											legs_data8[1] = {}
											legs_data8[1][1] = ref_icao7_x
											legs_data8[1][2] = 0		-- brg
											legs_data8[1][3] = 0		-- distance
											legs_data8[1][4] = 0		-- speed
											legs_data8[1][5] = ref_icao_alt7_x		-- altitude
											legs_data8[1][6] = 0	-- altitude type
											legs_data8[1][7] = ref_icao_lat7_x		-- latitude
											legs_data8[1][8] = ref_icao_lon7_x	--ref_icao_lon		-- longitude
											legs_data8[1][9] = ""			-- via id
											legs_data8[1][10] = 0		-- calc speed
											legs_data8[1][11] = ref_icao_alt7_x		-- calc altitude
											legs_data8[1][12] = 0		-- calc altitude vnav pth
											legs_data8[1][13] = 0
											legs_data8[1][14] = 0		-- rest alt
											legs_data8[1][15] = 0		-- last fuel
											legs_data8[1][16] = ""
											legs_data8[1][17] = 4		-- spd flag 0-default restrict, 1-custom restrict
											legs_data8[1][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
											legs_data8[1][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
											legs_data8[1][20] = 0
											legs_data8[1][21] = -1
											legs_data8[1][22] = ""
											legs_data8[1][23] = 0
											legs_data8[1][24] = 0
											legs_data8[1][25] = 0
											legs_data8[1][26] = 0
											legs_data8[1][27] = ""
											legs_data8[1][28] = ""
											legs_data8[1][29] = ""
											legs_data8[1][30] = ""
											legs_data8[1][31] = ""
											legs_data8[1][32] = 0
											legs_data8[1][33] = ""
											legs_data8[1][34] = ""
											legs_data8[1][35] = ""
											legs_data8[1][36] = 9
											legs_data8[1][37] = 0
											legs_data8[1][38] = ""
											legs_data8[1][39] = ""
											legs_data8[1][40] = 0
											legs_data8[1][41] = 0
											legs_data8[1][42] = 0
											legs_data8[1][43] = ""
											legs_data8[1][44] = 0
											legs_data8[1][45] = 0
											legs_data8[1][46] = 0
											legs_data8[1][47] = 0
											legs_data8[1][48] = ""
											legs_data8[1][49] = 0
											legs_data8[1][50] = 0
											legs_data8[1][51] = 0
											legs_data8[1][52] = 0
											legs_data8[1][53] = 0
											legs_data8[1][54] = 0
										end
										legs_num8 = 1
										apt_ok = 2
									else
										if offset_mod2 == 0 then
											apt_ok = 1
										else
											if offset_mod2 < first_star_idx_mod2 then
												rte_copy(legs_num8 + 1, rte12_sel_fo)
												apt_ok = first_star_idx_mod2
												if first_star_idx_mod2 ~= 0 and first_star_idx_mod2 < legs_num8 then
													if legs_data8[first_star_idx_mod2][19] == 4 then
														apt_ok = first_star_idx_mod2 + 1
														legs_data8[first_star_idx_mod2][19] = 0
														legs_data8[first_star_idx_mod2][4] = 0
														legs_data8[first_star_idx_mod2][5] = 0
														legs_data8[first_star_idx_mod2][6] = 0
														legs_data8[first_star_idx_mod2][9] = ""
														legs_data8[first_star_idx_mod2][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel_fo)
											elseif offset_mod2 < first_app_idx_mod2 then
												rte_copy(legs_num8 + 1, rte12_sel_fo)
												apt_ok = first_app_idx_mod2
												if first_app_idx_mod2 ~= 0 and first_app_idx_mod2 < legs_num8 then
													if legs_data8[first_app_idx_mod2][19] == 9 then
														apt_ok = first_app_idx_mod2 + 1
														legs_data8[first_app_idx_mod2][19] = 0
														legs_data8[first_app_idx_mod2][4] = 0
														legs_data8[first_app_idx_mod2][5] = 0
														legs_data8[first_app_idx_mod2][6] = 0
														legs_data8[first_app_idx_mod2][9] = ""
														legs_data8[first_app_idx_mod2][31] = "TF"
													end
												end
												rte_paste(apt_ok, rte12_sel_fo)
											elseif first_app_idx_mod2 ~= 0 then
												rte_copy(legs_num8 + 1, rte12_sel_fo)
												rte_paste(offset_mod2 + 1, rte12_sel_fo)
											elseif first_star_idx_mod2 ~= 0 then
												rte_copy(legs_num8 + 1, rte12_sel_fo)
												rte_paste(offset_mod2 + 1, rte12_sel_fo)
											end
											
											apt_ok = offset_mod2
											
										end
										
										if ref_icao7_x == "ORGN" then
											apt_ok = 1
										end
										
										if apt_ok <= legs_num8 then
											for xy = apt_ok, legs_num8 do
											-- change STAR,APP to route
												if legs_data8[xy][19] == 2 or legs_data8[xy][19] == 4
												or legs_data8[xy][19] == 7 or legs_data8[xy][19] == 8 
												or legs_data8[xy][19] == 9 then
													legs_data8[xy][19] = 0	-- wpt type STAR, APP
													legs_data8[xy][4] = 0	-- spd rest
													legs_data8[xy][5] = 0	-- alt rest
													legs_data8[xy][6] = 0	-- alt type rest
													legs_data8[xy][9] = ""		--via
												elseif (legs_data8[xy][19] == 1 or legs_data8[xy][19] == 3) then
													legs_data8[xy][19] = 0	-- wpt type STAR, APP
													legs_data8[xy][9] = ""		--via
												end
											end
										end
										last_sid_idx_mod2 = 0
										if rte12_act == rte12_sel_fo then
											if rte12_act == 0 then
												idx_ed_mod = 0
												rnav_idx_first_mod = 0
												rnav_idx_last_mod = 0
											else
												idx_ed_mod2 = 0
												rnav_idx_first_mod2 = 0
												rnav_idx_last_mod2 = 0
											end
										end
										apt_ok = legs_num8 + 1
									end
									
									-- DES ICAO
									legs_data8[apt_ok] = {}
									legs_data8[apt_ok][1] = des_icao7_x
									legs_data8[apt_ok][2] = 0		-- brg
									legs_data8[apt_ok][3] = 0		-- distance
									legs_data8[apt_ok][4] = 0		-- speed
									legs_data8[apt_ok][5] = 0		-- altitude
									legs_data8[apt_ok][6] = 0	-- altitude type
									legs_data8[apt_ok][7] = des_icao_lat7_x		-- latitude
									legs_data8[apt_ok][8] = des_icao_lon7_x		-- longitude
									legs_data8[apt_ok][9] = ""			-- via id
									legs_data8[apt_ok][10] = 0		-- calc speed
									legs_data8[apt_ok][11] = 0		-- calc altitude
									legs_data8[apt_ok][12] = 0		-- calc altitude vnav pth
									legs_data8[apt_ok][13] = 0
									legs_data8[apt_ok][14] = 0		-- rest alt
									legs_data8[apt_ok][15] = 0		-- last fuel
									legs_data8[apt_ok][16] = ""
									legs_data8[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
									legs_data8[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data8[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data8[apt_ok][20] = 0
									legs_data8[apt_ok][21] = -1
									legs_data8[apt_ok][22] = ""
									legs_data8[apt_ok][23] = 0
									legs_data8[apt_ok][24] = 0
									legs_data8[apt_ok][25] = 0
									legs_data8[apt_ok][26] = 0
									legs_data8[apt_ok][27] = ""
									legs_data8[apt_ok][28] = ""
									legs_data8[apt_ok][29] = ""
									legs_data8[apt_ok][30] = ""
									legs_data8[apt_ok][31] = ""
									legs_data8[apt_ok][32] = 0
									legs_data8[apt_ok][33] = ""
									legs_data8[apt_ok][34] = "DEST"
									legs_data8[apt_ok][35] = ""
									legs_data8[apt_ok][36] = 9
									legs_data8[apt_ok][37] = 0
									legs_data8[apt_ok][38] = ""
									legs_data8[apt_ok][39] = ""
									legs_data8[apt_ok][40] = 0
									legs_data8[apt_ok][41] = 0
									legs_data8[apt_ok][42] = 0
									legs_data8[apt_ok][43] = ""
									legs_data8[apt_ok][44] = 0
									legs_data8[apt_ok][45] = 0
									legs_data8[apt_ok][46] = 0
									legs_data8[apt_ok][47] = 0
									legs_data8[apt_ok][48] = ""
									legs_data8[apt_ok][49] = 0
									legs_data8[apt_ok][50] = 0
									legs_data8[apt_ok][51] = 0
									legs_data8[apt_ok][52] = 0
									legs_data8[apt_ok][53] = 0
									legs_data8[apt_ok][54] = 0
									
									if rte12_act == rte12_sel_fo then
										offset_mod2 = offset
									else
										offset_mod2 = 2
									end
									if offset_mod2 > legs_num8 then
										offset_mod2 = legs_num8
									end
									
									if legs_num ~= 0 then
										if rte12_act == rte12_sel_fo then
											legs_delete = 1
										end
									end
									
									if exec_activate_fpln2 ~= 0 then
										rte_exec2 = 0
										exec_activate_fpln2 = 0
										
										copy_to_legs_data2_buf(1)
										if legs_num2_buf > 1 then
											for n = 1, (legs_num2_buf + 1) do
												legs_data2_buf[n][11] = 0
												legs_data2_buf[n][10] = 0
												legs_data2_buf[n][14] = 50000
												legs_data2_buf[n][38] = ""
											end
										end
										copy_from_legs_data2_buf(1)
										
									end
									
									calc_rte_enable8 = 1
									
									create_fpln(rte12_sel_fo)
									
									arr_data7_x = 0
									find_des_rnw_data(1, 1)
									
									read_des_list_icao(des_icao7_x, 1)
									
									create_rnw_list(rte12_sel_fo)
									
									create_star_list(rte12_sel_fo)
									
									create_app_list(rte12_sel_fo)
									
									entry2 = ""
									
								else
									add_fmc_msg(NOT_IN_DATABASE, 1)
								end
							elseif entry2 == "" and des_icao7_x ~= "****" then
								entry2 = des_icao7_x
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					end
				end
			else
				-- button 1 RSK
				rte_1_rsk(1, 1, rte12_sel_fo)
			end
		elseif page_legs2 == 1 then
			local item = 0
			local button = 1	-- button 1 RSK
			
			local offset_tmp = 0
			
			if rte12_sel_fo == 0 then
				offset_tmp = offset_mod
			else
				offset_tmp = offset_mod2
			end
			item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
			if B738DR_no_rte2 == 0 and act_page2 > 1 then
				item = item - 1
			end
			
			if entry2 ~= "" then
				if set_spd_alt_rest2(item, rte12_sel_fo) == true then
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		elseif page_hold2 == 1 then
			local test_cond = false
			if entry2 ~= "" then
				if rte12_sel_fo == 0 then
					test_cond = set_spd_alt_rest2(hold_offset_idx2, rte12_sel_fo)
					if rte12_act == rte12_sel_fo then
						hold_exec = 1
					end
				else
					test_cond = set_spd_alt_rest2(hold_offset_idx8, rte12_sel_fo)
					if rte12_act == rte12_sel_fo then
						hold_exec2 = 1
					end
				end
			end
		elseif page_ref_nav_data2 == 1 then
			local strlen = string.len(entry2)
			if strlen == 0 or strlen > 4 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				nav_data_type = 1 -- navaid
				nav_data_find(entry2, 1)
			end
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			-- entry2 V1
			if entry2 == "" then
				if v1 ~= "---" and qrh == "OFF" and v1_set == "---" then
					v1_set = v1
					verify_to_spd = 0
				elseif v1_set ~= "---" then
					entry2 = v1_set
				end
			else
				-- from scratch (test speed correct)
				if entry2 == ">DELETE" then
					v1_set = "---"
					verify_to_spd = 0
					entry2 = ""
				else
					local n = tonumber(entry2)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n > 99 and n < 170 then
							v1_set = entry2
							verify_to_spd = 0
							entry2 = ""
							local v1_val = tonumber(v1_set)
							local vr_val = tonumber(vr_set)
							local v2_val = tonumber(v2_set)
							if v1_val ~= nil and v2_val ~= nil and vr_val ~= nil then
								if v1_val > vr_val or vr_val >= v2_val then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
									add_fmc_msg(TO_SPEEDS_DELETED, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
			display_update = 1
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			if rw_cond == 2 then
				rw_cond = 0
			else
				rw_cond = rw_cond + 1
			end
			if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				if v1_set == "---" and vr_set == "---" and v2_set == "---" then
					entry2 = ""
				else
					-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
					-- verify_to_spd = 1
					v1_set = "---"
					vr_set = "---"
					v2_set = "---"
					verify_to_spd = 0
					add_fmc_msg(TO_SPEEDS_DELETED, 1)
					entry2 = ""
				end
			end
			B738DR_calc_vspd = 1
		elseif page_climb2 == 1 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 7) then
			if entry2 == ">DELETE" then
				if set_spd_alt_rest2(B738DR_rest_wpt_alt_idx, rte12_sel_fo) == true then
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		-- elseif page_menu2 == 1 then
			-- -- select Units
			-- if units == 0 then
				-- units = 1
			-- else
				-- units = 0
			-- end
			-- units_recalc = 1
		elseif page_approach2 == 1 then
			-- select flaps 15
			if vref_15 == "---" then
				entry2 = ""
			else
				if flaps_app == "15" then
					app_flap = "15"
					app_spd = vref_15
					flaps_app = "  "
					entry2 = ""
				else
					flaps_app = "15"
					entry2 = "15/" .. string.format("%3d", vref_15)
				end
			end
		elseif page_perf2 == 1 then --and was_on_air == 0 then	-- only on ground
			-- Change cruise alt
			local n = entry_altitude(entry2)
			if n == 0 then
				add_fmc_msg(INVALID_INPUT, 1)
			else
				if crz_alt == "*****" then
					crz_alt_num = n
					if crz_alt_num >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					B738DR_fmc_cruise_alt = crz_alt_num
					entry2 = ""
					msg_unavaible_crz_alt = 0
					msg_chk_alt_constr = 0
					vnav_update = 1
					crz_alt_num2 = 0
					crz_alt_old = "     "
					if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
						add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
					end
					if isa_dev_c ~= "---" then
						n = tonumber(isa_dev_c)
						local alt_temp = math.min(crz_alt_num, 37000)
						tc_oat_c = string.format("%3d", (B738_rescale(0, 15, 37000, -56.5, alt_temp) + n))
						n = tonumber(tc_oat_c)
						n = (n * 9 / 5) + 32
						tc_oat_f = string.format("%4d", n)
					end
					B738_calc_vnav_spd()
				else
					crz_alt_num2 = n
					crz_alt_old = crz_alt
					crz_exec = 9
					exec1_light = 1
					if perf_exec == 2 then
						perf_exec = 3
					end
					if crz_alt_num2 >= B738DR_trans_alt_mod then
						n = n / 100
						crz_alt = "FL" .. string.format("%03d", n)
					else
						crz_alt = string.format("%5d", n)
					end
					entry2 = ""
				end
			end
		elseif page_init2 == 1 then
			-- go to REF NAV DATA
			page_init2 = 0
			page_ref_nav_data2 = 1
			ref_nav_wpt = "-----"
			ref_nav_navaid = "----"
			ref_nav_apt = "----"
			ref_nav_new = "*****"
		elseif page_pos_init2 == 1 then
			entry2 = last_pos
		elseif page_rte_legs2 > 0 then
			-- entry2 WIND DATA
			local strlen = string.len(entry2)
			local item = 0
			local button = 1	-- button 1 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page2 > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry2 == ">DELETE" then
					legs_data2[item][39] = ""
					entry2 = ""
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry2 = ""
						if rte12_act == rte12_sel_fo then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry2 = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry2 = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry2 = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry2 = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_descent_forecast2 == 1 then
			local strlen = string.len(entry2)
			local n = 0
			if strlen > 0 then
				if entry2 == ">DELETE" then
					tai_on_alt_mod = "-----"
					tai_off_alt_mod = "-----"
					tai_on_alt_num_mod = 0
					tai_off_alt_num_mod = 0
					entry2 = ""
					forecast_exec = 1
				else
					if string.sub(entry2, 1, 1) == "/" then
						if strlen == 6 then
							if string.sub(entry2, 2, 3) == "FL" then
								n = tonumber(string.sub(entry2, 4, -1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n = n * 100
									if n >= 1000 and n <= 41000 then
										tai_off_alt_num_mod = n
										if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
											tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
										else
											tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
										end
										entry2 = ""
										forecast_exec = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
							else
								n = tonumber(string.sub(entry2, 2, -1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if n >= 1000 and n <= 41000 then
										tai_off_alt_num_mod = n
										if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
											tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
										else
											tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
										end
										entry2 = ""
										forecast_exec = 1
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
							end
						elseif strlen == 5 then
							n = tonumber(string.sub(entry2, 2, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1000 and n <= 41000 then
									tai_off_alt_num_mod = n
									if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
										tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
									else
										tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
									end
									entry2 = ""
									forecast_exec = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						elseif strlen == 4 then
							n = tonumber(string.sub(entry2, 2, -1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 100
								if n >= 1000 and n <= 41000 then
									tai_off_alt_num_mod = n
									if tai_off_alt_num_mod >= B738DR_trans_lvl_mod then
										tai_off_alt_mod = "FL" .. string.format("%03d", tai_off_alt_num_mod / 100)
									else
										tai_off_alt_mod = string.format("%5d", tai_off_alt_num_mod)
									end
									entry2 = ""
									forecast_exec = 1
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					elseif strlen == 5 and string.sub(entry2, 1, 2) == "FL" then
						n = tonumber(string.sub(entry2, 3, -1))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							n = n * 100
							if n >= 1000 and n <= 41000 then
								tai_on_alt_num_mod = n
								if tai_on_alt_num_mod >= B738DR_trans_lvl_mod then
									tai_on_alt_mod = "FL" .. string.format("%03d", tai_on_alt_num_mod / 100)
								else
									tai_on_alt_mod = string.format("%5d", tai_on_alt_num_mod)
								end
								entry2 = ""
								forecast_exec = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					elseif strlen == 4 or strlen == 5 then
						n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n >= 1000 and n <= 41000 then
								tai_on_alt_num_mod = n
								if tai_on_alt_num_mod >= B738DR_trans_lvl_mod then
									tai_on_alt_mod = "FL" .. string.format("%03d", tai_on_alt_num_mod / 100)
								else
									tai_on_alt_mod = string.format("%5d", tai_on_alt_num_mod)
								end
								entry2 = ""
								forecast_exec = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					elseif strlen == 3 then
						n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							n = n * 100
							if n >= 1000 and n <= 41000 then
								tai_on_alt_num_mod = n
								if tai_on_alt_num_mod >= B738DR_trans_lvl_mod then
									tai_on_alt_mod = "FL" .. string.format("%03d", tai_on_alt_num_mod / 100)
								else
									tai_on_alt_mod = string.format("%5d", tai_on_alt_num_mod)
								end
								entry2 = ""
								forecast_exec = 1
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				if nearest_apt_list_num >= 1 then
					act_page2 = 2
				end
			else
				if entry2 == ">DELETE" then
					nearest_apt_list_alt[act_page2 - 1] = 0
					entry2 = ""
				else
					local nnn = entry_altitude(entry2)
					if nnn == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if nnn < 1000 or nnn > 41000 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_alt[act_page2 - 1] = nnn
							entry2 = ""
						end
					end
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				if altern_apt_list_num >= 1 then
					act_page2 = 2
				end
			else
				if entry2 == ">DELETE" then
					altern_apt_list_alt[act_page2 - 1] = 0
					entry2 = ""
				else
					local nnn = entry_altitude(entry2)
					if nnn == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if nnn < 1000 or nnn > 41000 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_alt[act_page2 - 1] = nnn
							entry2 = ""
						end
					end
				end
			end
		elseif page_dl_acars2 == 1 then
			page_dl_acars2 = 0
			page_dl_fltlog2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_dl_wxr_req2 == 1 then
			-- input ICAO weather request
			-- entry ICAO ATIS
			if entry2 == ">DELETE" then
				ac_atis_icao = "----"
				entry2 = ""
			elseif entry2 == "" and ac_atis_icao ~= "----" then
				entry2 = ac_atis_icao
			elseif string.len(entry2) == 4 then
				ac_atis_icao = entry2
				entry2 = ""
			end
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 2RSK
function B738_fmc2_2R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			if rte12_sel_fo == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
				if sel_ref_rwy2 == "-----" then
					if ref_rwy_sel_fo[2] ~= "" then
						if ref_rwy2 == ref_rwy_sel_fo[2] then
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
						else
							ref_rwy2 = ref_rwy_sel_fo[2]
							sel_ref_rwy2 = ref_rwy2
							create_sid_list(rte12_sel_fo)
							ref_rwy3 = ref_rwy2
							ref_rwy_exec = 1
						end
						act_page2 = 1
					end
				end
				rte_add_dep_arr(rte12_sel_fo)
				-- check SID for selected runway
				if rte12_act == rte12_sel_fo then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			elseif rte12_sel_fo == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
				if sel_ref_rwy8 == "-----" then
					if ref_rwy_sel_fo[2] ~= "" then
						if ref_rwy8 == ref_rwy_sel_fo[2] then
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
						else
							ref_rwy8 = ref_rwy_sel_fo[2]
							sel_ref_rwy8 = ref_rwy8
							create_sid_list(rte12_sel_fo)
							ref_rwy9 = ref_rwy8
							ref_rwy_exec = 1
						end
						act_page2 = 1
					end
				end
				rte_add_dep_arr(rte12_sel_fo)
				-- check SID for selected runway
				if rte12_act == rte12_sel_fo then
					if check_sid_rwy() == false then
						add_fmc_msg(RWY_NA_FOR_SID, 1)
					end
				end
			end
		elseif page_arr2 == 1 then
			local n = 0
			if rte12_sel_fo == 0 then
				if gs_prompt == 0 then
					if sel_des_app2 == "------" then
						if des_app_sel_fo[2] ~= "" then
							if des_app2 == des_app_sel_fo[2] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app2 = des_app_sel_fo[2]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel_fo[2] ~= "" then
									if des_app_tns2 == des_tns_sel_fo[2] then
										if app_tns_list_num == 1 then
											des_app_tns2 = "------"
											sel_des_app_tns2 = des_app_tns2
											act_page2 = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns2 = des_app_tns2
										end
									else
										des_app_tns2 = des_tns_sel_fo[2]
										sel_des_app_tns2 = des_app_tns2
										des_app_tns_exec = 1
									end
									act_page2 = 1
								end
							else
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								act_page2 = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension distance RX...
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist_x = string.format("%4.1f", entry2)
									entry2 = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app2 == "------" then
						if des_app_sel_fo[2] ~= "" then
							if des_app2 == des_app_sel_fo[2] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app2 = des_app_sel_fo[2]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
							rte_add_dep_arr(rte12_sel_fo)
						else
							if string.sub(des_app2, 1, 1) ~= "R" and string.sub(des_app2, 1, 1) ~= "D" and string.sub(des_app2, 1, 1) ~= "S" 
							and string.sub(des_app2, 1, 1) ~= "N" and string.sub(des_app2, 1, 1) ~= "Q" and string.sub(des_app2, 1, 1) ~= "V" then
								if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
									if B738DR_fms_ils_disable == 0 then
										B738DR_fms_ils_disable = 1
									else
										B738DR_fms_ils_disable = 0
									end
								end
							end
						end
					else
						if string.sub(des_app2, 1, 1) ~= "R" and string.sub(des_app2, 1, 1) ~= "D" and string.sub(des_app2, 1, 1) ~= "S" 
						and string.sub(des_app2, 1, 1) ~= "N" and string.sub(des_app2, 1, 1) ~= "Q" and string.sub(des_app2, 1, 1) ~= "V" then
							if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
								if B738DR_fms_ils_disable == 0 then
									B738DR_fms_ils_disable = 1
								else
									B738DR_fms_ils_disable = 0
								end
							end
						end
					end
				end
			else
				if gs_prompt == 0 then
					if sel_des_app8 == "------" then
						if des_app_sel_fo[2] ~= "" then
							if des_app8 == des_app_sel_fo[2] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app8 = des_app_sel_fo[2]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel_fo[2] ~= "" then
									if des_app_tns8 == des_tns_sel_fo[2] then
										if app_tns_list_num == 1 then
											des_app_tns8 = "------"
											sel_des_app_tns8 = des_app_tns8
											act_page2 = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns8 = des_app_tns8
										end
									else
										des_app_tns8 = des_tns_sel_fo[2]
										sel_des_app_tns8 = des_app_tns8
										des_app_tns_exec = 1
									end
									act_page2 = 1
								end
							else
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								act_page2 = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension distance RX...
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist7_x = string.format("%4.1f", entry2)
									entry2 = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app8 == "------" then
						if des_app_sel_fo[2] ~= "" then
							if des_app8 == des_app_sel_fo[2] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app8 = des_app_sel_fo[2]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
							rte_add_dep_arr(rte12_sel_fo)
						else
							if string.sub(des_app8, 1, 1) ~= "R" and string.sub(des_app8, 1, 1) ~= "D" and string.sub(des_app8, 1, 1) ~= "S" 
							and string.sub(des_app8, 1, 1) ~= "N" and string.sub(des_app8, 1, 1) ~= "Q" and string.sub(des_app8, 1, 1) ~= "V" then
								if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
									if B738DR_fms_ils_disable == 0 then
										B738DR_fms_ils_disable = 1
									else
										B738DR_fms_ils_disable = 0
									end
								end
							end
						end
					else
						if string.sub(des_app8, 1, 1) ~= "R" and string.sub(des_app8, 1, 1) ~= "D" and string.sub(des_app8, 1, 1) ~= "S" 
						and string.sub(des_app8, 1, 1) ~= "N" and string.sub(des_app8, 1, 1) ~= "Q" and string.sub(des_app8, 1, 1) ~= "V" then
							if simDR_glideslope_status < 2 and B738DR_gls_gs_status < 2 then
								if B738DR_fms_ils_disable == 0 then
									B738DR_fms_ils_disable = 1
								else
									B738DR_fms_ils_disable = 0
								end
							end
						end
					end
				end
			end
		elseif page_dep_arr2 == 1 then
			-- Destination ARR
			if ref_icao_x ~= "----" and des_icao_x ~= "****" then
				page_arr2 = 1
				page_dep_arr2 = 0
				rte12_sel_fo = 0
				
				if arr_data_x ~= 0 then
					
					local apt_ok = 0
					
					if legs_num2 > 1 then	--and des_icao_x ~= des_icao then
						-- -- delete all STAR/APP
						if first_star_idx_mod > 0 and offset < first_star_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_star_idx_mod
							if first_star_idx_mod ~= 0 and first_star_idx_mod < legs_num2 then
								if legs_data2[first_star_idx_mod][19] == 4 then
									apt_ok = first_star_idx_mod + 1
									legs_data2[first_star_idx_mod][19] = 0
									legs_data2[first_star_idx_mod][4] = 0
									legs_data2[first_star_idx_mod][5] = 0
									legs_data2[first_star_idx_mod][6] = 0
									legs_data2[first_star_idx_mod][9] = ""
									legs_data2[first_star_idx_mod][21] = -1
									legs_data2[first_star_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						elseif first_app_idx_mod > 0 and offset < first_app_idx_mod then
							rte_copy(legs_num2 + 1, 0)
							apt_ok = first_app_idx_mod
							if first_app_idx_mod ~= 0 and first_app_idx_mod < legs_num2 then
								if legs_data2[first_app_idx_mod][19] == 9 then
									apt_ok = first_app_idx_mod + 1
									legs_data2[first_app_idx_mod][19] = 0
									legs_data2[first_app_idx_mod][4] = 0
									legs_data2[first_app_idx_mod][5] = 0
									legs_data2[first_app_idx_mod][6] = 0
									legs_data2[first_app_idx_mod][9] = ""
									legs_data2[first_app_idx_mod][21] = -1
									legs_data2[first_app_idx_mod][31] = "TF"
								end
							end
							clr_star_app(0)
							rte_paste(apt_ok, 0)
							calc_rte_enable2 = 1
						end
						last_sid_idx_mod = legs_num2
						if rte12_act == rte12_sel_fo then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num2 + 1
						legs_data2[apt_ok] = {}
						legs_data2[apt_ok][1] = des_icao_x	--des_icao_x
						legs_data2[apt_ok][2] = 0		-- brg
						legs_data2[apt_ok][3] = 0		-- distance
						legs_data2[apt_ok][4] = 0		-- speed
						legs_data2[apt_ok][5] = 0		-- altitude
						legs_data2[apt_ok][6] = 0	-- altitude type
						legs_data2[apt_ok][7] = des_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[apt_ok][8] = des_icao_lon_x	--des_icao_lon		-- longitude
						legs_data2[apt_ok][9] = ""			-- via id
						legs_data2[apt_ok][10] = 0		-- calc speed
						legs_data2[apt_ok][11] = 0		-- calc altitude
						legs_data2[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data2[apt_ok][13] = 0
						legs_data2[apt_ok][14] = 0		-- rest alt
						legs_data2[apt_ok][15] = 0		-- last fuel
						legs_data2[apt_ok][16] = ""
						legs_data2[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data2[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data2[apt_ok][20] = 0
						legs_data2[apt_ok][21] = -1
						legs_data2[apt_ok][22] = ""
						legs_data2[apt_ok][23] = 0
						legs_data2[apt_ok][24] = 0
						legs_data2[apt_ok][25] = 0
						legs_data2[apt_ok][26] = 0
						legs_data2[apt_ok][27] = ""
						legs_data2[apt_ok][28] = ""
						legs_data2[apt_ok][29] = ""
						legs_data2[apt_ok][30] = ""
						legs_data2[apt_ok][31] = ""
						legs_data2[apt_ok][32] = 0
						legs_data2[apt_ok][33] = ""
						legs_data2[apt_ok][34] = "DEST"
						legs_data2[apt_ok][35] = ""
						legs_data2[apt_ok][36] = 9
						legs_data2[apt_ok][37] = 0
						legs_data2[apt_ok][38] = ""
						legs_data2[apt_ok][39] = ""
						legs_data2[apt_ok][40] = 0
						legs_data2[apt_ok][41] = 0
						legs_data2[apt_ok][42] = 0
						legs_data2[apt_ok][43] = ""
						legs_data2[apt_ok][44] = 0
						legs_data2[apt_ok][45] = 0
						legs_data2[apt_ok][46] = 0
						legs_data2[apt_ok][47] = 0
						legs_data2[apt_ok][48] = ""
						legs_data2[apt_ok][49] = 0
						legs_data2[apt_ok][50] = 0
						legs_data2[apt_ok][51] = 0
						legs_data2[apt_ok][52] = 0
						legs_data2[apt_ok][53] = 0
						legs_data2[apt_ok][54] = 0
						
						if rte12_act == 0 then
							offset_mod = offset
						else
							offset_mod = 2
						end
						if offset_mod > legs_num2 then
							offset_mod = legs_num2
						end
						
					elseif legs_num2 > 0 then
						legs_data2[legs_num2 + 1][1] = des_icao_x	--des_icao_x
						legs_data2[legs_num2 + 1][7] = des_icao_lat_x	--des_icao_lat		-- latitude
						legs_data2[legs_num2 + 1][8] = des_icao_lon_x	--des_icao_lon		-- longitude
					end
					
					
					des_app_rw_only_x = 0
					des_star2 = "------"
					des_star_trans2 = "------"
					des_app2 = "------"
					des_app_tns2 = "------"
					if rte12_act == 0 then
						legs_delete = 1
					end
				end
				arr_data_x = 0
				read_des_list_icao(des_icao_x, 0)
				
				sel_des_star2 = "------"
				sel_des_star_trans2 = "------"
				sel_des_app2 = "------"
				sel_des_app_tns2 = "------"
				
				create_star_list(0)
				create_star_tns_list(0)
				create_app_list(0)
				create_app_tns_list(0)
				
			end
		elseif page_rte_init2 == 1 then
			-- entry2 FLIGHT NUMBER
			if act_page2 == 1 then
				if entry2 == ">DELETE" then
					--entry2 = INVALID_DELETE
					flt_num_x = "--------"
					entry2 = ""
					if dl_block_time < 0 then
						dl_flt_num = flt_num_x
						dl_day = simDR_time_day
					end
				else
					if string.len(entry2) > 8 then
						add_fmc_msg(INVALID_INPUT, 1)
					elseif string.len(entry2) > 0 then
						flt_num_x = entry2
						entry2 = ""
						if dl_block_time < 0 then
							dl_flt_num = flt_num_x
							dl_day = simDR_time_day
						end
					end
				end
			else
				-- button 2 RSK
				rte_x_rsk(2, 1, rte12_sel_fo)
			end
		elseif page_legs2 == 1 then
			local item = 0
			local button = 2	-- button 2 RSK
			
			local offset_tmp = offset_mod
			if rte12_sel_fo ~= 0 then
				offset_tmp = offset_mod2
			end
			item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
			if B738DR_no_rte2 == 0 and act_page2 > 1 then
				item = item - 1
			end
			
			if entry2 ~= "" then
				if set_spd_alt_rest2(item, rte12_sel_fo) == true then
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			-- entry2 VR
			if entry2 == "" then
				if vr ~= "---" and qrh == "OFF" and vr_set == "---" then
					vr_set = vr
					verify_to_spd = 0
				elseif vr_set ~= "---" then
					entry2 = vr_set
				end
			else
				-- from scratch (test speed correct)
				if entry2 == ">DELETE" then
					vr_set = "---"
					verify_to_spd = 0
					entry2 = ""
				else
					local n = tonumber(entry2)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n > 99 and n < 170 then
							vr_set = entry2
							verify_to_spd = 0
							entry2 = ""
							local v1_val = tonumber(v1_set)
							local vr_val = tonumber(vr_set)
							local v2_val = tonumber(v2_set)
							if v1_val ~= nil and v2_val ~= nil and vr_val ~= nil then
								if v1_val > vr_val or vr_val >= v2_val then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
									add_fmc_msg(TO_SPEEDS_DELETED, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
			display_update = 1
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
				-- entry2 SEL TEMP and OAT
				local strlen = string.len(entry2)
				local n = 0
				if strlen > 0 then
					if entry2 == ">DELETE" then
						sel_temp = "----"
						oat = "****"
						sel_temp_f = "----"
						oat_f = "****"
						oat_unit = "`C"
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							if v1_set == "---" and vr_set == "---" and v2_set == "---" then
								entry2 = ""
							else
								-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
								-- verify_to_spd = 1
								add_fmc_msg(TO_SPEEDS_DELETED, 1)
								verify_to_spd = 0
								v1_set = "---"
								vr_set = "---"
								v2_set = "---"
								qrh = "OFF"
								entry2 = ""
							end
						end
						B738DR_calc_vspd = 1
					else
						if strlen > 0 and strlen < 6 then
							local oat_set = 0
							if string.sub(entry2, 1, 1) == "/" and strlen > 1 then
								oat_set = 1		-- entry2 OAT
								entry2 = string.sub(entry2, 2, strlen)
								strlen = strlen -1
							end
							
							local n_str = string.sub(entry2, strlen, strlen)
							if n_str == "C" then 
								n = tonumber(string.sub(entry2, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry2 = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry2 = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							elseif n_str == "F" then
								n = tonumber(string.sub(entry2, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 50 or n > 167) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 140) then	-- Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`F"
										if oat_set == 0 then
											sel_temp_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
										else
											oat_f = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry2 = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry2 = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							else
								n = tonumber(string.sub(entry2, 1, strlen))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if oat_set == 0 and (n < 10 or n > 75) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									elseif oat_set ~= 0 and (n < -40 or n > 60) then	-- Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										oat_unit = "`C"
										if oat_set == 0 then
											if n < 0 then
												sel_temp = string.format("%4d", n)
											else
												if n < 10 then
													sel_temp = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													sel_temp = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											sel_temp_f = string.format("%4d", n)
										else
											if n < 0 then
												oat = string.format("%4d", n)
											else
												n = math.floor(n + 0.5)
												if n < 10 then
													oat = "  +" .. string.sub(string.format("%4d", n), 4, 4)
												else
													oat = " +" .. string.sub(string.format("%4d", n), 3, 4)
												end
											end
											n = (n * 9 / 5) + 32
											oat_f = string.format("%4d", n)
										end
										if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
											if v1_set == "---" and vr_set == "---" and v2_set == "---" then
												entry2 = ""
											else
												-- add_fmc_msg(VERIFY_TO_SPEEDS, 1)
												-- verify_to_spd = 1
												add_fmc_msg(TO_SPEEDS_DELETED, 1)
												verify_to_spd = 0
												v1_set = "---"
												vr_set = "---"
												v2_set = "---"
												qrh = "OFF"
												entry2 = ""
											end
										end
										B738DR_calc_vspd = 1
									end
								end
							end
							
							
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
		elseif page_approach2 == 1 then
			-- select flaps 30
			if vref_30 == "---" then
				entry2 = ""
			else
				if flaps_app == "30" then
					app_flap = "30"
					app_spd = vref_30
					flaps_app = "  "
					entry2 = ""
				else
					flaps_app = "30"
					entry2 = "30/" .. string.format("%3d", vref_30)
				end
			end
		elseif page_n1_limit2 == 1 then
			-- select CLB
			clb = "<SEL>"
			clb_1 = "     "
			clb_2 = "     "
			sel_clb_thr = 1
		elseif page_descent_forecast2 == 1 then
			-- entry2 ISA DEV / QNH
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			local nn = 0
      local nnn = 0
			local n_str = ""
			local forec_isa_dev_tmp = ""
			local data_ok = 0
			local mode_temp_qnh = 0
			if strlen > 0 then
				if entry2 == ">DELETE" then
					forec_isa_dev_mod = "---"
					forec_qnh_mod = "------"
					entry2 = ""
					forecast_exec = 1
				else
					if string.sub(entry2, -1, -1) == "/" then
						if strlen > 1 then
							entry2 = string.sub(entry2, 1, -2)
							strlen = strlen - 1
						end
						if strlen < 4 then
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < -40 or n > 40 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									forec_isa_dev_mod = string.format("%3d", n)
									entry2 = ""
									forecast_exec = 1
								end
							end
						else
							nn,n = string.find(entry2, "/")
							if nn ~= nil and nn > 1 then
								-- ISA
								n = tonumber(string.sub(entry2, 1, nn - 1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if n < -40 or n > 40 then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										forec_isa_dev_tmp = string.format("%3d", n)
										data_ok = 1
									end
								end
								if data_ok == 1 then
									n = strlen - nn
									if n > 0 and n <= 5 then
										if n == 5 then
											-- QNH xx.xx
											if string.sub(entry2, nn + 3, nn + 3) == "." then
												n = tonumber(string.sub(entry2, nn + 1, strlen))
												if n == nil then
													add_fmc_msg(INVALID_INPUT, 1)
												else
													if n < 0 or n > 40.0 then
														add_fmc_msg(INVALID_INPUT, 1)
													else
														forec_qnh_mod = " " .. string.sub(entry2, nn + 1, strlen)
														data_ok = 1
													end
												end
											else
												add_fmc_msg(INVALID_INPUT, 1)
											end
										else
											n = tonumber(string.sub(entry2, nn + 1, strlen))
											if n == nil then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												if n < 0 or n > 1355 then
													add_fmc_msg(INVALID_INPUT, 1)
												else
													forec_qnh_mod = "  " .. string.sub(entry2, nn + 1, strlen)
													data_ok = 1
												end
											end
										end
										if data_ok == 1 then
											forec_isa_dev_mod = forec_isa_dev_tmp
											entry2 = ""
											forecast_exec = 1
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
								
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						end
					else
						
						if string.sub(entry2, 1, 1) == "/" and (strlen == 5 or strlen == 6) then
							strlen = strlen - 1
							entry2 = string.sub(entry2, 2, -1)
						else
							-- find "/"
							nn,n = string.find(entry2, "/")
							if nn ~= nil then
								if nn == 1 or strlen < 3 then
									mode_temp_qnh = 2
								else
									mode_temp_qnh = 1
								end
							end
						end
						if mode_temp_qnh == 0 then
							if strlen <= 4 then		-- QNH /XXXX
								n = tonumber(string.sub(entry2, 1, 4))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									if n < 0 or n > 1355 then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										forec_qnh_mod = "  " .. string.sub(entry2, 1, 4)
										entry2 = ""
										forecast_exec = 1
									end
								end
							elseif strlen == 5 then		-- QNH /XX.XX in hg
								if string.sub(entry2, 3, 3) == "." then
									n = tonumber(string.sub(entry2, 1, 5))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										if n < 0 or n > 40.0 then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											forec_qnh_mod = " " .. string.sub(entry2, 1, 5)
											entry2 = ""
											forecast_exec = 1
										end
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							else
								add_fmc_msg(INVALID_INPUT, 1)
							end
						elseif mode_temp_qnh == 1 then
							-- ISA DEV / QNH
							n_str = string.sub(entry2, 1, nn-1)
							n = tonumber(n_str)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < -40 or n > 40 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									nnn = n
									
									n_str = string.sub(entry2, nn+1, -1)
									strlen = string.len(n_str)
									if strlen <= 4 then		-- QNH /XXXX
										n = tonumber(string.sub(n_str, 1, 4))
										if n == nil then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											if n < 0 or n > 1355 then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												forec_isa_dev_mod = string.format("%3d", nnn)
												forec_qnh_mod = "  " .. string.sub(n_str, 1, 4)
												entry2 = ""
												forecast_exec = 1
											end
										end
									elseif strlen == 5 then		-- QNH /XX.XX in hg
										if string.sub(n_str, 3, 3) == "." then
											n = tonumber(string.sub(n_str, 1, 5))
											if n == nil then
												add_fmc_msg(INVALID_INPUT, 1)
											else
												if n < 0 or n > 40.0 then
													add_fmc_msg(INVALID_INPUT, 1)
												else
													forec_isa_dev_mod = string.format("%3d", nnn)
													forec_qnh_mod = " " .. string.sub(n_str, 1, 5)
													entry2 = ""
													forecast_exec = 1
												end
											end
										else
											add_fmc_msg(INVALID_INPUT, 1)
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
									
									
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
		elseif page_perf2 == 1 then
			-- entry2 CRZ WIND
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					crz_wind_dir_mod = "---"
					crz_wind_spd_mod = "---"
					entry2 = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						crz_wind_dir = crz_wind_dir_mod
						crz_wind_spd = crz_wind_spd_mod
					end
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						crz_wind_dir_mod = string.format("%03d", q)
						crz_wind_spd_mod = string.format("%3d", qq)
						entry2 = ""
						if perf_exec == 2 then
							perf_exec = 3
						elseif perf_exec < 2 then
							crz_wind_dir = crz_wind_dir_mod
							crz_wind_spd = crz_wind_spd_mod
						end
						B738_calc_vnav_spd()
						vnav_update = 1
					end
				end
			end
		elseif page_perf2 == 2 then
			-- CLB max speed kts/mach
			local strlen = string.len(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					clb_max_kts = "   "
					clb_max_mach = "   "
					entry2 = ""
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry2, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_min_mach)
							if nn == nil then
								nn = 0.4
							else
								nn = nn / 1000
							end
							if n > 0.82 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								clb_max_mach = string.format("%03d", n)
								entry2 = ""
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340  or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_max_kts = string.format("%03d", n)
								entry2 = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry2, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry2, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(clb_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry2, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(clb_min_mach)
										if nn == nil then
											nn = 0.4
										else
											nn = nn / 1000
										end
										if n > 0.82 or n < nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											clb_max_kts = kts
											clb_max_mach = string.format("%03d", n)
											entry2 = ""
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		----
		elseif page_pos_init2 == 1 then
			if rte12_sel_fo == 0 then
				if ref_icao_pos ~= "               " then
					entry2 = ref_icao_pos
				end
			else
				if ref_icao_pos2 ~= "               " then
					entry2 = ref_icao_pos2
				end
			end
		elseif page_rte_legs2 > 0 then
			-- entry2 WIND DATA
			local strlen = string.len(entry2)
			local item = 0
			local button = 2	-- button 2 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page2 > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry2 == ">DELETE" then
					legs_data2[item][39] = ""
					entry2 = ""
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry2 = ""
						if rte12_act == rte12_sel_fo then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry2 = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry2 = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry2 = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry2 = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_progress2 == 4 and legs_num > 0 then
			local strlen = string.len(entry2)
			local entry_tmp = entry2
			local n = 0
			if entry2 == ">DELETE" then
				vrnp = ""
				vrnp_num = 400
				entry2 = ""
			else
				if strlen > 1 then
					if string.sub(entry_tmp, -1, -1) == "/" then
						entry_tmp = string.sub(entry_tmp, 1, -2)
					end
					strlen = string.len(entry_tmp)
					if strlen > 1 and strlen < 4 then
						n = tonumber(entry_tmp)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						elseif n < 20 or n > 400 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							vrnp = string.format("%3d", n)
							vrnp_num = n
							entry2 = ""
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen > 0 then
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_nearest_apt2 == 1 then
			if act_page == 1 then
				if nearest_apt_list_num >= 2 then
					act_page2 = 3
				end
			else
				-- entry2 WIND
				local strlen = string.len(entry2)
				if strlen > 0 then
					if entry2 == ">DELETE" then
						nearest_apt_list_wind[act_page2 - 1] = ""
						entry2 = ""
					else
						local q = 0
						local qq = 0
						q, qq = entry_wind(entry2)
						if qq == 0 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_wind[act_page2 - 1] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
							entry2 = ""
						end
						-- if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
							-- local n = tonumber(string.sub(entry2, 1, 3))
							-- if n == nil then
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- if n < 0 or n > 360 then		-- wind heading 0 - 359
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- n = n % 360
									-- local wind_dir = string.format("%03d", n)
									-- n = tonumber(string.sub(entry2, 5, strlen))
									-- if n == nil then
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- if n < 1 or n > 199 then	-- wind speed 1 - 199
											-- add_fmc_msg(INVALID_INPUT, 1)
										-- else
											-- nearest_apt_list_wind[act_page2 - 1] = wind_dir .. "`/" .. string.format("%3d", n)
											-- entry2 = ""
										-- end
									-- end
								-- end
							-- end
						-- else
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- end
					end
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page == 1 then
				if altern_apt_list_num >= 2 then
					act_page2 = 3
				end
			else
				-- entry2 WIND
				local strlen = string.len(entry2)
				if strlen > 0 then
					if entry2 == ">DELETE" then
						altern_apt_list_wind[act_page2 - 1] = ""
						entry2 = ""
					else
						local q = 0
						local qq = 0
						q, qq = entry_wind(entry2)
						if qq == 0 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_wind[act_page2 - 1] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
							entry2 = ""
						end
						-- if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
							-- local n = tonumber(string.sub(entry2, 1, 3))
							-- if n == nil then
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- if n < 0 or n > 360 then		-- wind heading 0 - 359
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- n = n % 360
									-- local wind_dir = string.format("%03d", n)
									-- n = tonumber(string.sub(entry2, 5, strlen))
									-- if n == nil then
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- if n < 1 or n > 199 then	-- wind speed 1 - 199
											-- add_fmc_msg(INVALID_INPUT, 1)
										-- else
											-- altern_apt_list_wind[act_page2 - 1] = wind_dir .. "`/" .. string.format("%3d", n)
											-- entry2 = ""
										-- end
									-- end
								-- end
							-- end
						-- else
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- end
					end
				end
			end
		elseif page_dl_menu2 == 1 then
			page_dl_menu2 = 0
			page_dl_acars2 = 1
			act_page2 = 1
			display_update = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 3RSK
function B738_fmc2_3R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
				if rte12_sel_fo == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
					if sel_ref_rwy2 == "-----" then
						if ref_rwy_sel_fo[3] ~= "" then
							if ref_rwy2 == ref_rwy_sel_fo[3] then
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel_fo)
							else
								ref_rwy2 = ref_rwy_sel_fo[3]
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel_fo)
								ref_rwy3 = ref_rwy2
								ref_rwy_exec = 1
							end
							act_page2 = 1
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
					-- check SID for selected runway
					if rte12_act == rte12_sel_fo then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				elseif rte12_sel_fo == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
					if sel_ref_rwy8 == "-----" then
						if ref_rwy_sel_fo[3] ~= "" then
							if ref_rwy8 == ref_rwy_sel_fo[3] then
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel_fo)
							else
								ref_rwy8 = ref_rwy_sel_fo[3]
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel_fo)
								ref_rwy9 = ref_rwy8
								ref_rwy_exec = 1
							end
							act_page2 = 1
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
					-- check SID for selected runway
					if rte12_act == rte12_sel_fo then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				end
		elseif page_arr2 == 1 then
			local n = 0
			if rte12_sel_fo == 0 then
				if gs_prompt == 0 then
					if sel_des_app2 == "------" then
						if des_app_sel_fo[3] ~= "" then
							if des_app2 == des_app_sel_fo[3] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app2 = des_app_sel_fo[3]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel_fo[3] ~= "" then
									if des_app_tns2 == des_tns_sel_fo[3] then
										sel_des_app_tns2 = des_app_tns2
									else
										des_app_tns2 = des_tns_sel_fo[3]
										sel_des_app_tns2 = des_app_tns2
										des_app_tns_exec = 1
									end
									act_page2 = 1
								end
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry2)
							if entry2 == ">DELETE" then
								rw_ext_fpa_x = "-.--"
								entry2 = ""
								des_app2_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel_fo)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa_x = string.format("%4.2f", entry2)
									entry2 = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app2 == "------" then
						if des_app_sel_fo[3] ~= "" then
							if des_app2 == des_app_sel_fo[3] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app2 = des_app_sel_fo[3]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel_fo[3] ~= "" then
									if des_app_tns2 == des_tns_sel_fo[3] then
										if app_tns_list_num == 1 then
											des_app_tns2 = "------"
											sel_des_app_tns2 = des_app_tns2
											act_page2 = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns2 = des_app_tns2
										end
									else
										des_app_tns2 = des_tns_sel_fo[3]
										sel_des_app_tns2 = des_app_tns2
										des_app_tns_exec = 1
									end
									act_page2 = 1
								end
							else
								des_app_tns2 = "------"
								sel_des_app_tns2 = des_app_tns2
								act_page2 = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension distance RX...
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist_x = string.format("%4.1f", entry2)
									entry2 = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			else
				if gs_prompt == 0 then
					if sel_des_app8 == "------" then
						if des_app_sel_fo[3] ~= "" then
							if des_app8 == des_app_sel_fo[3] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app8 = des_app_sel_fo[3]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel_fo[3] ~= "" then
									if des_app_tns8 == des_tns_sel_fo[3] then
										sel_des_app_tns8 = des_app_tns8
									else
										des_app_tns8 = des_tns_sel_fo[3]
										sel_des_app_tns8 = des_app_tns8
										des_app_tns_exec = 1
									end
									act_page2 = 1
								end
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry2)
							if entry2 == ">DELETE" then
								rw_ext_fpa7_x = "-.--"
								entry2 = ""
								des_app8_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel_fo)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa7_x = string.format("%4.2f", entry2)
									entry2 = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				else
					if sel_des_app8 == "------" then
						if des_app_sel_fo[3] ~= "" then
							if des_app8 == des_app_sel_fo[3] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app8 = des_app_sel_fo[3]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel_fo[3] ~= "" then
									if des_app_tns8 == des_tns_sel_fo[3] then
										if app_tns_list_num == 1 then
											des_app_tns8 = "------"
											sel_des_app_tns8 = des_app_tns8
											act_page2 = 1
											des_app_tns_exec = 1
										else
											sel_des_app_tns8 = des_app_tns8
										end
									else
										des_app_tns8 = des_tns_sel_fo[3]
										sel_des_app_tns8 = des_app_tns8
										des_app_tns_exec = 1
									end
									act_page2 = 1
								end
							else
								des_app_tns8 = "------"
								sel_des_app_tns8 = des_app_tns8
								act_page2 = 1
								des_app_tns_exec = 1
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension distance RX...
							n = tonumber(entry2)
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 1 and n <= 25 then
									rw_ext_dist7_x = string.format("%4.1f", entry2)
									entry2 = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			end
		elseif page_dep_arr2 == 1 then
			-- Reference ARR
			if ref_icao7_x ~= "----" and des_icao7_x ~= "****" and ref_icao7_x ~= "ORGN" then
				
				local apt_ok = 0
				local xy = 0
				rte12_sel_fo = 1
				
				if arr_data7_x == 0 then
					-- DES ICAO
					if legs_num8 > 1 then
						-- -- delete all STAR/APP
						if first_star_idx_mod2 > 0 and offset < first_star_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_star_idx_mod2
							if first_star_idx_mod2 ~= 0 and first_star_idx_mod2 < legs_num8 then
								if legs_data8[first_star_idx_mod2][19] == 4 then
									apt_ok = first_star_idx_mod2 + 1
									legs_data8[first_star_idx_mod2][19] = 0
									legs_data8[first_star_idx_mod2][4] = 0
									legs_data8[first_star_idx_mod2][5] = 0
									legs_data8[first_star_idx_mod2][6] = 0
									legs_data8[first_star_idx_mod2][9] = ""
									legs_data8[first_star_idx_mod2][21] = -1
									legs_data8[first_star_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						elseif first_app_idx_mod2 > 0 and offset < first_app_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_app_idx_mod2
							if first_app_idx_mod2 ~= 0 and first_app_idx_mod2 < legs_num8 then
								if legs_data8[first_app_idx_mod2][19] == 9 then
									apt_ok = first_app_idx_mod2 + 1
									legs_data8[first_app_idx_mod2][19] = 0
									legs_data8[first_app_idx_mod2][4] = 0
									legs_data8[first_app_idx_mod2][5] = 0
									legs_data8[first_app_idx_mod2][6] = 0
									legs_data8[first_app_idx_mod2][9] = ""
									legs_data8[first_app_idx_mod2][21] = -1
									legs_data8[first_app_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						elseif last_sid_idx_mod2 > 0 and offset < last_sid_idx_mod2 and last_sid_idx_mod2 <= legs_num8 then
							clr_star_app(1)
							rte_copy(legs_num8 + 1, 1)
							rte_paste(last_sid_idx_mod2 + 1, 1)
							calc_rte_enable8 = 1
						elseif offset <= legs_num8 then
							clr_star_app(1)
							rte_copy(legs_num8 + 1, 1)
							rte_paste(offset, 1)
							calc_rte_enable8 = 1
						end
						last_sid_idx_mod2 = legs_num8
						if rte12_act == rte12_sel_fo then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num8 + 1
						legs_data8[apt_ok] = {}
						legs_data8[apt_ok][1] = ref_icao7_x
						legs_data8[apt_ok][2] = 0		-- brg
						legs_data8[apt_ok][3] = 0		-- distance
						legs_data8[apt_ok][4] = 0		-- speed
						legs_data8[apt_ok][5] = 0		-- altitude
						legs_data8[apt_ok][6] = 0	-- altitude type
						legs_data8[apt_ok][7] = ref_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[apt_ok][8] = ref_icao_lon7_x	--des_icao_lon		-- longitude
						legs_data8[apt_ok][9] = ""			-- via id
						legs_data8[apt_ok][10] = 0		-- calc speed
						legs_data8[apt_ok][11] = 0		-- calc altitude
						legs_data8[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data8[apt_ok][13] = 0
						legs_data8[apt_ok][14] = 0		-- rest alt
						legs_data8[apt_ok][15] = 0		-- last fuel
						legs_data8[apt_ok][16] = ""
						legs_data8[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data8[apt_ok][20] = 0
						legs_data8[apt_ok][21] = -1
						legs_data8[apt_ok][22] = ""
						legs_data8[apt_ok][23] = 0
						legs_data8[apt_ok][24] = 0
						legs_data8[apt_ok][25] = 0
						legs_data8[apt_ok][26] = 0
						legs_data8[apt_ok][27] = ""
						legs_data8[apt_ok][28] = ""
						legs_data8[apt_ok][29] = ""
						legs_data8[apt_ok][30] = ""
						legs_data8[apt_ok][31] = ""
						legs_data8[apt_ok][32] = 0
						legs_data8[apt_ok][33] = ""
						legs_data8[apt_ok][34] = "DEST"
						legs_data8[apt_ok][35] = ""
						legs_data8[apt_ok][36] = 9
						legs_data8[apt_ok][37] = 0
						legs_data8[apt_ok][38] = ""
						legs_data8[apt_ok][39] = ""
						legs_data8[apt_ok][40] = 0
						legs_data8[apt_ok][41] = 0
						legs_data8[apt_ok][42] = 0
						legs_data8[apt_ok][43] = ""
						legs_data8[apt_ok][44] = 0
						legs_data8[apt_ok][45] = 0
						legs_data8[apt_ok][46] = 0
						legs_data8[apt_ok][47] = 0
						legs_data8[apt_ok][48] = ""
						legs_data8[apt_ok][49] = 0
						legs_data8[apt_ok][50] = 0
						legs_data8[apt_ok][51] = 0
						legs_data8[apt_ok][52] = 0
						legs_data8[apt_ok][53] = 0
						legs_data8[apt_ok][54] = 0
						
						if rte12_act == 1 then
							offset_mod2 = offset
						else
							offset_mod2 = 2
						end
						if offset_mod2 > legs_num8 then
							offset_mod2 = legs_num8
						end
						
					elseif legs_num8 > 0 then
						legs_data8[legs_num8 + 1][1] = ref_icao7_x	--des_icao_x
						legs_data8[legs_num8 + 1][7] = ref_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[legs_num8 + 1][8] = ref_icao_lon7_x	--des_icao_lon		-- longitude
					end
					
					if rte12_act == 1 then
						legs_delete = 1
					end
					des_app_rw_only7_x = 0
					des_star8 = "------"
					des_star_trans8 = "------"
					des_app8 = "------"
					des_app_tns8 = "------"
				end
				arr_data7_x = 1
				page_arr2 = 1
				find_ref_rnw_data(1, 1)
				read_ref_list_icao(ref_icao7_x, 1)
				read_des_list_icao(ref_icao7_x, 1)
				page_dep_arr2 = 0
				
				sel_des_star8 = "------"
				sel_des_star_trans8 = "------"
				sel_des_app8 = "------"
				sel_des_app_tns8 = "------"
				
				create_star_list(1)
				create_star_tns_list(1)
				create_app_list(1)
				create_app_tns_list(1)
				
			end
		elseif page_descent2 == 1 then
			
			
			local n = 0
			local nn = 0
			local nnn = string.len(entry2)
			
			nn,n = string.find(entry2, "/")
			if nn == nil then
				item_sel2 = 0
				item_sel_act2 = 0
			elseif item_sel2 == 0 then
				if nn > 1 and legs_num > 1 and offset > 0 then
					if offset <= legs_num then
						for zzz = offset, legs_num do
							if string.sub(entry2, 1, nn - 1) == legs_data[zzz][1] then
								item_sel2 = zzz
								break
							end
						end
					end
				end
			elseif item_sel2 > 0 and item_sel2 <= legs_num then
				if string.sub(entry2, 1, nn - 1) ~= legs_data[item_sel2][1] then
					item_sel2 = 0
					item_sel_act2 = 0
				end
			else
				item_sel2 = 0
				item_sel_act2 = 0
			end
			if entry2 == ">DELETE" and vb_calc_id ~= "" then
				vb_calc_id = ""
				vb_calc_alt = 0
				vb_calc_lat = 0
				vb_calc_lon = 0
				item_sel2 = 0
				item_sel_act2 = 0
			elseif item_sel2 > 0 and item_sel2 <= legs_num and nnn > 2 then
				if string.sub(entry2, -1, -1) == "/" then
					if string.sub(entry2, 1, 2) == "RW" then
						vb_calc_id = legs_data[item_sel2][1]
						vb_calc_alt = legs_data[item_sel2][5]
						vb_calc_lat = legs_data[item_sel2][7]
						vb_calc_lon = legs_data[item_sel2][8]
						item_sel2 = 0
						item_sel_act2 = 0
						entry2 = ""
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					n = tonumber(string.sub(entry2, nn + 1, -1))
					nn = nnn - nn
					if nn == 3 then
						--FLxxx
						if n >= 100 and n <= 410 then
							vb_calc_id = legs_data[item_sel2][1]
							vb_calc_alt = n * 100
							vb_calc_lat = legs_data[item_sel2][7]
							vb_calc_lon = legs_data[item_sel2][8]
							item_sel2 = 0
							item_sel_act2 = 0
							entry2 = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					elseif nn == 4 or nn == 5 then
						--xxxxx
						if n >= 0 and n <= 41000 then
							vb_calc_id = legs_data[item_sel2][1]
							vb_calc_alt = n
							vb_calc_lat = legs_data[item_sel2][7]
							vb_calc_lon = legs_data[item_sel2][8]
							item_sel2 = 0
							item_sel_act2 = 0
							entry2 = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_rte_init2 == 1 then
			if act_page2 > 1 then
				-- button 3 RSK
				rte_x_rsk(3, 1, rte12_sel_fo)
			end
		elseif page_legs2 == 1 then
			local item = 0
			local button = 3	-- button 3 RSK
			
			local offset_tmp = offset_mod
			if rte12_sel_fo ~= 0 then
				offset_tmp = offset_mod2
			end
			item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
			if B738DR_no_rte2 == 0 and act_page2 > 1 then
				item = item - 1
			end
			
			if entry2 ~= "" then
				if set_spd_alt_rest2(item, rte12_sel_fo) == true then
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				end
			end
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			-- entry2 V2
			if entry2 == "" then
				if v2 ~= "---" and qrh == "OFF" and v2_set == "---" then
					v2_set = v2
					verify_to_spd = 0
				elseif v2_set ~= "---" then
					entry2 = v2_set
				end
			else
				-- from scratch (test speed correct)
				if entry2 == ">DELETE" then
					v2_set = "---"
					verify_to_spd = 0
					entry2 = ""
				else
					local n = tonumber(entry2)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n > 99 and n < 170 then
							v2_set = entry2
							verify_to_spd = 0
							entry2 = ""
							local v1_val = tonumber(v1_set)
							local vr_val = tonumber(vr_set)
							local v2_val = tonumber(v2_set)
							if v1_val ~= nil and v2_val ~= nil and vr_val ~= nil then
								if v1_val > vr_val or vr_val >= v2_val then
									v1_set = "---"
									vr_set = "---"
									v2_set = "---"
									verify_to_spd = 0
									add_fmc_msg(TO_SPEEDS_DELETED, 1)
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					end
				end
			end
			display_update = 1
		elseif page_approach2 == 1 then
			-- select flaps 40
			if vref_40 == "---" then
				entry2 = ""
			else
				if flaps_app == "40" then
					app_flap = "40"
					app_spd = vref_40
					flaps_app = "  "
					entry2 = ""
				else
					flaps_app = "40"
					entry2 = "40/" .. string.format("%3d", vref_40)
				end
			end
		elseif page_n1_limit2 == 1 then
			-- select CLB-1
			clb_1 = "<SEL>"
			clb = "     "
			clb_2 = "     "
			sel_clb_thr = 1
		elseif page_ref_nav_data_wpt2 == 1 then
			if string.len(entry2) > 0 then
				local err_pos = 0
				local n = 0
				if string.len(entry2) ~= 8 or string.sub(entry2, 7, 7) ~= "." then
					err_pos = 1
				end
				n = tonumber(string.sub(entry2, 2, 4))
				if (n == nil) or (n < 0) or (n > 180) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry2, 5, 6))
				if (n == nil) or (n < 0) or (n > 59) then
					err_pos = 1
				end
				n = tonumber(string.sub(entry2, 8, 8))
				if n == nil then
					err_pos = 1
				end
				if string.sub(entry2, 1, 1) ~= "E" 
				and string.sub(entry2, 1, 1) ~= "W" then
					err_pos = 1
				end
				if err_pos == 0 then
					ref_nav_lon = tonumber(string.sub(entry2, 2, 4)) + (tonumber(string.sub(entry2, 5, 8)) / 60)
					if string.sub(entry2, 1, 1) == "W" then
						ref_nav_lon = -ref_nav_lon
					end
					entry2 = ""
					ref_nav_id = "-----"
					ref_nav_rc = ""
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					if ref_nav_lat ~= 0 then
						ref_nav_exec = 1
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_descent_forecast2 == 1 then
			-- entry2 WIND LAYER 1
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					forec_dir_1_mod = "---"
					forec_spd_1_mod = "---"
					entry2 = ""
					forecast_exec = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						forec_dir_1_mod = string.format("%03d", q)
						forec_spd_1_mod = string.format("%3d", qq)
						entry2 = ""
						forecast_exec = 1
					end
					-- if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry2, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry2, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- forec_dir_1_mod = wind_dir
										-- forec_spd_1_mod = string.format("%3d", n)
										-- entry2 = ""
										-- forecast_exec = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_perf2 == 1 and disable_PERF_3R == 0 then
			-- entry2 ISA DEV
			local strlen = string.len(entry2)
			local alt_temp = crz_alt_num
			local n = 0
			local n_str = ""
			if strlen > 0 then
				if entry2 == ">DELETE" then
					isa_dev_f_mod = "----"
					isa_dev_c_mod = "---"
					tc_oat_f_mod = "----"
					tc_oat_c_mod = "---"
					entry2 = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						isa_dev_f = isa_dev_f_mod
						isa_dev_c = isa_dev_c_mod
						tc_oat_c = tc_oat_c_mod
						tc_oat_f = tc_oat_f_mod
					end
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen < 6 then
						n = tonumber(entry2)
						if n == nil and strlen > 1 then
							n = tonumber(string.sub(entry2, 1, strlen-1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n_str = string.sub(entry2, strlen, strlen)
								if n_str == "C" then 
									if n < -40 or n > 70 then	-- ISA DEV Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_c_mod = string.format("%3d", n)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry2 = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								elseif n_str == "F" then
									if n < -40 or n > 158 then	-- ISA DEV Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_f_mod = string.format("%4d", n)
										n = n * 5 / 9
										isa_dev_c_mod = string.format("%3d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry2 = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						else
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if B738DR_fmc_units == 0 then
									if n < -40 or n > 158 then	-- ISA DEV Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_f_mod = string.format("%4d", n)
										n = n * 5 / 9
										isa_dev_c_mod = string.format("%3d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry2 = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								else
									if n < -40 or n > 70 then	-- ISA DEV Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										isa_dev_c_mod = string.format("%3d", n)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										-- calc TC OAT
										n = tonumber(isa_dev_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										tc_oat_c_mod = string.format("%3d", alt_temp + n)
										n = tonumber(tc_oat_c_mod)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										entry2 = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_perf2 == 2 then
			-- CRZ max speed kts/mach
			local strlen = string.len(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					crz_max_kts = "   "
					crz_max_mach = "   "
					entry2 = ""
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry2, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_min_mach)
							if nn == nil then
								nn = 0.4
							else
								nn = nn / 1000
							end
							if n > 0.82 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								crz_max_mach = string.format("%03d", n)
								entry2 = ""
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340  or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								crz_max_kts = string.format("%03d", n)
								entry2 = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry2, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry2, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(crz_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry2, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(crz_min_mach)
										if nn == nil then
											nn = 0.4
										else
											nn = nn / 1000
										end
										if n > 0.82 or n < nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											crz_max_kts = kts
											entry2 = ""
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		----------
		elseif page_rte_legs2 > 0 then
			-- entry2 WIND DATA
			local strlen = string.len(entry2)
			local item = 0
			local button = 3	-- button 3 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page2 > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry2 == ">DELETE" then
					legs_data2[item][39] = ""
					entry2 = ""
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry2 = ""
						if rte12_act == rte12_sel_fo then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry2 = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry2 = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry2 = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry2 = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_init2 == 1 then
			page_altern_apt2 = 1
			page_init2 = 0
			act_page2 = 1
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				if nearest_apt_list_num >= 3 then
					act_page2 = 4
				end
			else
				if nearest_apt_list_via[act_page2 - 1] ~= 0 then
					if entry2 == ">DELETE" then
						nearest_apt_list_dist2[act_page2 - 1] = 0
						entry2 = ""
					else
						local nnn = tonumber(entry2)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							nearest_apt_list_dist2[act_page2 - 1] = nnn
							entry2 = ""
						end
					end
				end
			end
		elseif page_altern_apt2 == 1 then
			if act_page2 == 1 then
				if altern_apt_list_num >= 3 then
					act_page2 = 4
				end
			else
				if altern_apt_list_via[act_page2 - 1] ~= 0 then
					if entry2 == ">DELETE" then
						altern_apt_list_dist2[act_page2 - 1] = 0
						entry2 = ""
					else
						local nnn = tonumber(entry2)
						if nnn == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							altern_apt_list_dist2[act_page2 - 1] = nnn
							entry2 = ""
						end
					end
				end
			end
		elseif page_ref_nav_data2 == 1 then
			page_flt_plns2 = 1
			page_ref_nav_data2 = 0
			create_flt_plns()
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 4RSK
function B738_fmc2_4R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			-- if in_flight_mode == 0 then
				if rte12_sel_fo == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
					if sel_ref_rwy2 == "-----" then
						if ref_rwy_sel_fo[4] ~= "" then
							if ref_rwy2 == ref_rwy_sel_fo[4] then
								sel_ref_rwy2 = ref_rwy2
									create_sid_list(rte12_sel_fo)
							else
								ref_rwy2 = ref_rwy_sel_fo[4]
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel_fo)
								ref_rwy3 = ref_rwy2
								ref_rwy_exec = 1
							end
							act_page2 = 1
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
					-- check SID for selected runway
					if rte12_act == rte12_sel_fo then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				elseif rte12_sel_fo == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
					if sel_ref_rwy8 == "-----" then
						if ref_rwy_sel_fo[4] ~= "" then
							if ref_rwy8 == ref_rwy_sel_fo[4] then
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel_fo)
							else
								ref_rwy8 = ref_rwy_sel_fo[4]
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel_fo)
								ref_rwy9 = ref_rwy8
								ref_rwy_exec = 1
							end
							act_page2 = 1
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
					-- check SID for selected runway
					if rte12_act == rte12_sel_fo then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				end
			-- end
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if gs_prompt == 0 then
					if sel_des_app2 == "------" then
						if des_app_sel_fo[4] ~= "" then
							if des_app2 == des_app_sel_fo[4] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app2 = des_app_sel_fo[4]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
					else
						if sel_des_app_tns2 == "------" then
							if des_tns_sel_fo[4] ~= "" then
								if des_app_tns2 == des_tns_sel_fo[4] then
									sel_des_app_tns2 = des_app_tns2
								else
									des_app_tns2 = des_tns_sel_fo[4]
									sel_des_app_tns2 = des_app_tns2
									act_page2 = 1
									des_app_tns_exec = 1
								end
							end
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
				else
					if sel_des_app2 == "------" then
						if des_app_sel_fo[4] ~= "" then
							if des_app2 == des_app_sel_fo[4] then
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app2 = des_app_sel_fo[4]
								sel_des_app2 = des_app2
								if string.sub(sel_des_app2, 1, 2) == "RW" then
									des_app_rw_only_x = 1
									des_star_trans2 = "------"
									sel_des_star_trans2 = des_star_trans2
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only_x = 0
									rw_ext_dist_x = "--.-"
									rw_ext_fpa_x = "-.--"
								end
								if sel_des_star2 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only_x == 0 then
							if sel_des_app_tns2 == "------" then
								if des_tns_sel_fo[4] ~= "" then
									if des_app_tns2 == des_tns_sel_fo[4] then
										sel_des_app_tns2 = des_app_tns2
									else
										des_app_tns2 = des_tns_sel_fo[4]
										sel_des_app_tns2 = des_app_tns2
										act_page2 = 1
										des_app_tns_exec = 1
									end
								end
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry2)
							if entry2 == ">DELETE" then
								rw_ext_fpa_x = "-.--"
								entry2 = ""
								des_app2_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel_fo)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa_x = string.format("%4.2f", entry2)
									entry2 = ""
									des_app2_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			else
				if gs_prompt == 0 then
					if sel_des_app8 == "------" then
						if des_app_sel_fo[4] ~= "" then
							if des_app8 == des_app_sel_fo[4] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app8 = des_app_sel_fo[4]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
					else
						if sel_des_app_tns8 == "------" then
							if des_tns_sel_fo[4] ~= "" then
								if des_app_tns8 == des_tns_sel_fo[4] then
									sel_des_app_tns8 = des_app_tns8
								else
									des_app_tns8 = des_tns_sel_fo[4]
									sel_des_app_tns8 = des_app_tns8
									act_page2 = 1
									des_app_tns_exec = 1
								end
							end
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
				else
					if sel_des_app8 == "------" then
						if des_app_sel_fo[4] ~= "" then
							if des_app8 == des_app_sel_fo[4] then
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								create_app_tns_list(rte12_sel_fo)
							else
								des_app8 = des_app_sel_fo[4]
								sel_des_app8 = des_app8
								if string.sub(sel_des_app8, 1, 2) == "RW" then
									des_app_rw_only7_x = 1
									des_star_trans8 = "------"
									sel_des_star_trans8 = des_star_trans8
									create_star_list(rte12_sel_fo)
								else
									des_app_rw_only7_x = 0
									rw_ext_dist7_x = "--.-"
									rw_ext_fpa7_x = "-.--"
								end
								if sel_des_star8 == "------" then
									create_star_list(rte12_sel_fo)
								end
								des_app_exec = 1
								create_app_tns_list(rte12_sel_fo)
							end
							act_page2 = 1
						end
						rte_add_dep_arr(rte12_sel_fo)
					else
						if des_app_rw_only7_x == 0 then
							if sel_des_app_tns8 == "------" then
								if des_tns_sel_fo[4] ~= "" then
									if des_app_tns8 == des_tns_sel_fo[4] then
										sel_des_app_tns8 = des_app_tns8
									else
										des_app_tns8 = des_tns_sel_fo[4]
										sel_des_app_tns8 = des_app_tns8
										act_page2 = 1
										des_app_tns_exec = 1
									end
								end
							end
							rte_add_dep_arr(rte12_sel_fo)
						else
							-- rwy extension fpa RX...
							local n = tonumber(entry2)
							if entry2 == ">DELETE" then
								rw_ext_fpa7_x = "-.--"
								entry2 = ""
								des_app8_old = "------"
								des_app_exec = 1
								rte_add_dep_arr(rte12_sel_fo)
							elseif n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n >= 2.0 and n <= 5.5 then
									rw_ext_fpa7_x = string.format("%4.2f", entry2)
									entry2 = ""
									des_app8_old = "------"
									des_app_exec = 1
									rte_add_dep_arr(rte12_sel_fo)
								else
									add_fmc_msg(INVALID_INPUT, 1)
								end
							end
						end
					end
				end
			end
		elseif page_dep_arr2 == 1 then
			-- Destination ARR
			if ref_icao7_x ~= "----" and des_icao7_x ~= "****" then
				page_arr2 = 1
				page_dep_arr2 = 0
				rte12_sel_fo = 1
				
				if arr_data7_x ~= 0 then
					
					local apt_ok = 0
					
					if legs_num8 > 1 then	--and des_icao_x ~= des_icao then
						-- -- delete all STAR/APP
						if first_star_idx_mod2 > 0 and offset < first_star_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_star_idx_mod2
							if first_star_idx_mod2 ~= 0 and first_star_idx_mod2 < legs_num8 then
								if legs_data8[first_star_idx_mod2][19] == 4 then
									apt_ok = first_star_idx_mod2 + 1
									legs_data8[first_star_idx_mod2][19] = 0
									legs_data8[first_star_idx_mod2][4] = 0
									legs_data8[first_star_idx_mod2][5] = 0
									legs_data8[first_star_idx_mod2][6] = 0
									legs_data8[first_star_idx_mod2][9] = ""
									legs_data8[first_star_idx_mod2][21] = -1
									legs_data8[first_star_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						elseif first_app_idx_mod2 > 0 and offset < first_app_idx_mod2 then
							rte_copy(legs_num8 + 1, 1)
							apt_ok = first_app_idx_mod2
							if first_app_idx_mod2 ~= 0 and first_app_idx_mod2 < legs_num8 then
								if legs_data8[first_app_idx_mod2][19] == 9 then
									apt_ok = first_app_idx_mod2 + 1
									legs_data8[first_app_idx_mod2][19] = 0
									legs_data8[first_app_idx_mod2][4] = 0
									legs_data8[first_app_idx_mod2][5] = 0
									legs_data8[first_app_idx_mod2][6] = 0
									legs_data8[first_app_idx_mod2][9] = ""
									legs_data8[first_app_idx_mod2][21] = -1
									legs_data8[first_app_idx_mod2][31] = "TF"
								end
							end
							clr_star_app(1)
							rte_paste(apt_ok, 1)
							calc_rte_enable8 = 1
						end
						last_sid_idx_mod2 = legs_num8
						if rte12_act == rte12_sel_fo then
							if rte12_act == 0 then
								idx_ed_mod = 0
								rnav_idx_first_mod = 0
								rnav_idx_last_mod = 0
							else
								idx_ed_mod2 = 0
								rnav_idx_first_mod2 = 0
								rnav_idx_last_mod2 = 0
							end
						end
						
						apt_ok = legs_num8 + 1
						legs_data8[apt_ok] = {}
						legs_data8[apt_ok][1] = des_icao7_x	--des_icao_x
						legs_data8[apt_ok][2] = 0		-- brg
						legs_data8[apt_ok][3] = 0		-- distance
						legs_data8[apt_ok][4] = 0		-- speed
						legs_data8[apt_ok][5] = 0		-- altitude
						legs_data8[apt_ok][6] = 0	-- altitude type
						legs_data8[apt_ok][7] = des_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[apt_ok][8] = des_icao_lon7_x	--des_icao_lon		-- longitude
						legs_data8[apt_ok][9] = ""			-- via id
						legs_data8[apt_ok][10] = 0		-- calc speed
						legs_data8[apt_ok][11] = 0		-- calc altitude
						legs_data8[apt_ok][12] = 0		-- calc altitude vnav pth
						legs_data8[apt_ok][13] = 0
						legs_data8[apt_ok][14] = 0		-- rest alt
						legs_data8[apt_ok][15] = 0		-- last fuel
						legs_data8[apt_ok][16] = ""
						legs_data8[apt_ok][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
						legs_data8[apt_ok][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
						legs_data8[apt_ok][20] = 0
						legs_data8[apt_ok][21] = -1
						legs_data8[apt_ok][22] = ""
						legs_data8[apt_ok][23] = 0
						legs_data8[apt_ok][24] = 0
						legs_data8[apt_ok][25] = 0
						legs_data8[apt_ok][26] = 0
						legs_data8[apt_ok][27] = ""
						legs_data8[apt_ok][28] = ""
						legs_data8[apt_ok][29] = ""
						legs_data8[apt_ok][30] = ""
						legs_data8[apt_ok][31] = ""
						legs_data8[apt_ok][32] = 0
						legs_data8[apt_ok][33] = ""
						legs_data8[apt_ok][34] = "DEST"
						legs_data8[apt_ok][35] = ""
						legs_data8[apt_ok][36] = 9
						legs_data8[apt_ok][37] = 0
						legs_data8[apt_ok][38] = ""
						legs_data8[apt_ok][39] = ""
						legs_data8[apt_ok][40] = 0
						legs_data8[apt_ok][41] = 0
						legs_data8[apt_ok][42] = 0
						legs_data8[apt_ok][43] = ""
						legs_data8[apt_ok][44] = 0
						legs_data8[apt_ok][45] = 0
						legs_data8[apt_ok][46] = 0
						legs_data8[apt_ok][47] = 0
						legs_data8[apt_ok][48] = ""
						legs_data8[apt_ok][49] = 0
						legs_data8[apt_ok][50] = 0
						legs_data8[apt_ok][51] = 0
						legs_data8[apt_ok][52] = 0
						legs_data8[apt_ok][53] = 0
						legs_data8[apt_ok][54] = 0
						
						if rte12_act == 1 then
							offset_mod2 = offset
						else
							offset_mod2 = 2
						end
						if offset_mod2 > legs_num8 then
							offset_mod2 = legs_num8
						end
						
					elseif legs_num8 > 0 then
						legs_data8[legs_num8 + 1][1] = des_icao7_x	--des_icao_x
						legs_data8[legs_num8 + 1][7] = des_icao_lat7_x	--des_icao_lat		-- latitude
						legs_data8[legs_num8 + 1][8] = des_icao_lon7_x	--des_icao_lon		-- longitude
					end
					
					
					des_app_rw_only7_x = 0
					des_star8 = "------"
					des_star_trans8 = "------"
					des_app8 = "------"
					des_app_tns8 = "------"
					if rte12_act == 1 then
						legs_delete = 1
					end
				end
				arr_data7_x = 0
				read_des_list_icao(des_icao7_x, 1)
				
				sel_des_star8 = "------"
				sel_des_star_trans8 = "------"
				sel_des_app8 = "------"
				sel_des_app_tns8 = "------"
				
				create_star_list(1)
				create_star_tns_list(1)
				create_app_list(1)
				create_app_tns_list(1)
				
			end
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 then
				if (rte12_sel_fo == 0 and co_route_x ~= "------------" and legs_num2 > 1) or (rte12_sel_fo == 1 and co_route7_x ~= "------------" and legs_num8 > 1) then
					-- REVERSE
				end
			else
				-- button 4 RSK
				rte_x_rsk(4, 1, rte12_sel_fo)
			end
		elseif page_legs2 == 1 then
			local item = 0
			local button = 4	-- button 4 RSK
			
			local ab_avail = 0
			if B738DR_no_rte2 == 0 and act_page == 1 then
				if legs_intdir ~= 0 and legs_intdir_rte == rte12_act then
					if B738DR_legs_abeam_avail ~= 0 then
						ab_avail = 1
					end
				end
			end
			
			if ab_avail == 0 then
				local offset_tmp = offset_mod
				if rte12_sel_fo ~= 0 then
					offset_tmp = offset_mod2
				end
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page2 > 1 then
					item = item - 1
				end
				
				if entry2 ~= "" then
					if set_spd_alt_rest2(item, rte12_sel_fo) == true then
						if rte12_act == rte12_sel_fo then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				-- RTE COPY
				if B738DR_fmc_exec_lights == 0 then
					if rte12_sel_fo == 0 and legs_num > 1 and rte12_act == rte12_sel_fo then
						if rte12_copy_done == 0 then
							rte12_copy_done = 1
							rte21_copy_done = 0
							run_after_time(rte12_copy_t, 0.5)
						end
					elseif rte12_sel_fo ~= 0 and legs_num > 1 and rte12_act == rte12_sel_fo then
						if rte21_copy_done == 0 then
							rte21_copy_done = 1
							rte12_copy_done = 0
							run_after_time(rte21_copy_t, 0.5)
						end
					end
				end
			end
		elseif page_pos_init2 == 1 and disable_POS_4R == 0 then
			-- set IRS POS
			if entry2 == ">DELETE" then
				irs_pos = "*****.*******.*"
				entry2 = ""
				B738DR_irs_pos_fmc_set = irs_pos
				B738CMD_fmc_to_irs_pos:once()
			else
				if string.len(entry2) > 0 then
					local err_pos = 0
					local n = 0
					if string.len(entry2) ~= 15
					or string.sub(entry2, 6, 6) ~= "."
					or  string.sub(entry2, 14, 14) ~= "." then
						err_pos = 1
					end
					n = tonumber(string.sub(entry2, 2, 3))
					if (n == nil) or (n < 0) or (n > 90) then
						err_pos = 1
					end
					n = tonumber(string.sub(entry2, 4, 5))
					if (n == nil) or (n < 0) or (n > 59) then
						err_pos = 1
					end
					n = tonumber(string.sub(entry2, 9, 11))
					if (n == nil) or (n < 0) or (n > 180) then
						err_pos = 1
					end
					n = tonumber(string.sub(entry2, 12, 13))
					if (n == nil) or (n < 0) or (n > 59) then
						err_pos = 1
					end
					if string.sub(entry2, 1, 1) ~= "N" 
					and string.sub(entry2, 1, 1) ~= "S" then
						err_pos = 1
					end
					if string.sub(entry2, 8, 8) ~= "E" 
					and string.sub(entry2, 8, 8) ~= "W" then
						err_pos = 1
					end
					if err_pos == 0 then
						irs_pos = entry2
						entry2 = ""
						B738DR_irs_pos_fmc_set = irs_pos
						B738CMD_fmc_to_irs_pos:once()
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_n1_limit2 == 1 then
			-- select CLB-2
			clb_2 = "<SEL>"
			clb = "     "
			clb_1 = "     "
			sel_clb_thr = 1
		elseif page_descent_forecast2 == 1 then
			-- entry2 WIND LAYER 2
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					forec_dir_2_mod = "---"
					forec_spd_2_mod = "---"
					entry2 = ""
					forecast_exec = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						forec_dir_2_mod = string.format("%03d", q)
						forec_spd_2_mod = string.format("%3d", qq)
						entry2 = ""
						forecast_exec = 1
					end
					-- if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry2, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry2, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- forec_dir_2_mod = wind_dir
										-- forec_spd_2_mod = string.format("%3d", n)
										-- entry2 = ""
										-- forecast_exec = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_approach2 == 1 then
			-- FLAP / SPD
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					app_flap = "--"
					app_spd = "---"
					entry2 = ""
				elseif strlen == 2 then
					if string.sub(entry2, 2, 2) == "/" then
						local flp = string.sub(entry2, 1, 1)
						if flp == "0" or flp == "1" or flp == "2" or flp == "5" then
							app_flap = flp
							entry2 = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n > 250 or n < 100 then	-- SPEED min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--app_flap = flp
								app_spd = string.format("%3d", n)
								entry2 = ""
							end
						end
					end
				elseif strlen == 3 then
					if string.sub(entry2, 3, 3) == "/" then
						local flp = string.sub(entry2, 1, 2)
						if flp == "10" or flp == "15" or flp == "25" or flp == "30" or flp == "40" then
							app_flap = flp
							entry2 = ""
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n > 250 or n < 100 then	-- SPEED min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--app_flap = flp
								app_spd = string.format("%3d", n)
								entry2 = ""
							end
						end
					end
				elseif strlen == 4 then
					if string.sub(entry2, 1, 1) == "/" then
						local n = tonumber(string.sub(entry2, 2, 4))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n > 250 or n < 100 then	-- SPEED min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								--app_flap = flp
								app_spd = string.format("%3d", n)
								entry2 = ""
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 5 then
					if string.sub(entry2, 2, 2) == "/" then
						local flp = string.sub(entry2, 1, 1)
						if flp == "0" or flp == "1" or flp == "2" or flp == "5" then
							local n = tonumber(string.sub(entry2, 3, 5))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n > 250 or n < 100 then	-- SPEED min and max
									add_fmc_msg(INVALID_INPUT, 1)
								else
									app_flap = flp
									app_spd = string.format("%3d", n)
									entry2 = ""
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				elseif strlen == 6 then
					if string.sub(entry2, 3, 3) == "/" then
						local flp = string.sub(entry2, 1, 2)
						if flp == "10" or flp == "15" or flp == "25" or flp == "30" or flp == "40" then
							local n = tonumber(string.sub(entry2, 4, 6))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n > 250 or n < 100 then	-- SPEED min and max
									add_fmc_msg(INVALID_INPUT, 1)
								else
									app_flap = flp
									app_spd = string.format("%3d", n)
									entry2 = ""
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_perf2 == 1 and disable_PERF_4R == 0 then
			-- entry2 T/C OAT
			local strlen = string.len(entry2)
			local alt_temp = crz_alt_num
			local n = 0
			local n_str = ""
			if strlen > 0 then
				if entry2 == ">DELETE" then
					tc_oat_f_mod = "----"
					tc_oat_c_mod = "---"
					isa_dev_f_mod = "----"
					isa_dev_c_mod = "---"
					entry2 = ""
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						isa_dev_f = isa_dev_f_mod
						isa_dev_c = isa_dev_c_mod
						tc_oat_c = tc_oat_c_mod
						tc_oat_f = tc_oat_f_mod
					end
					B738_calc_vnav_spd()
					vnav_update = 1
				else
					if strlen < 6 then
						n = tonumber(entry2)
						if n == nil and strlen > 1 then
							n = tonumber(string.sub(entry2, 1, strlen-1))
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = tonumber(string.sub(entry2, 1, strlen-1))
								if n == nil then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n_str = string.sub(entry2, strlen, strlen)
									if n_str == "C" then 
										if n < -70 or n > 70 then	-- T/C OAT Celsius min and max
											add_fmc_msg(INVALID_INPUT, 1)
										else
											tc_oat_c_mod = string.format("%3d", n)
											n = (n * 9 / 5) + 32
											tc_oat_f_mod = string.format("%4d", n)
											-- calc ISA DEV
											n = tonumber(tc_oat_c_mod)
											alt_temp = math.min(alt_temp, 37000)
											alt_temp = 15 - (2 * (alt_temp / 1000))
											alt_temp = math.max(alt_temp, -56.5)
											isa_dev_c_mod = string.format("%3d", n - alt_temp)
											n = tonumber(isa_dev_c_mod)
											n = n * 9 / 5
											isa_dev_f_mod = string.format("%4d", n)
											entry2 = ""
											if perf_exec == 2 then
												perf_exec = 3
											elseif perf_exec < 2 then
												isa_dev_f = isa_dev_f_mod
												isa_dev_c = isa_dev_c_mod
												tc_oat_c = tc_oat_c_mod
												tc_oat_f = tc_oat_f_mod
											end
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									elseif n_str == "F" then
										if n < -94 or n > 158 then	-- T/C OAT Fahrenheit min and max
											add_fmc_msg(INVALID_INPUT, 1)
										else
											tc_oat_f_mod = string.format("%4d", n)
											n = (n - 32) * 5 / 9
											tc_oat_c_mod = string.format("%3d", n)
											-- calc ISA DEV
											n = tonumber(tc_oat_c_mod)
											alt_temp = math.min(alt_temp, 37000)
											alt_temp = 15 - (2 * (alt_temp / 1000))
											alt_temp = math.max(alt_temp, -56.5)
											isa_dev_c_mod = string.format("%3d", n - alt_temp)
											n = tonumber(isa_dev_c_mod)
											n = n * 9 / 5
											isa_dev_f_mod = string.format("%4d", n)
											entry2 = ""
											if perf_exec == 2 then
												perf_exec = 3
											elseif perf_exec < 2 then
												isa_dev_f = isa_dev_f_mod
												isa_dev_c = isa_dev_c_mod
												tc_oat_c = tc_oat_c_mod
												tc_oat_f = tc_oat_f_mod
											end
											B738_calc_vnav_spd()
											vnav_update = 1
										end
									else
										add_fmc_msg(INVALID_INPUT, 1)
									end
								end
							end
						else
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if B738DR_fmc_units == 0 then
									if n < -94 or n > 158 then	-- T/C OAT Fahrenheit min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										tc_oat_f_mod = string.format("%4d", n)
										n = (n - 32) * 5 / 9
										tc_oat_c_mod = string.format("%3d", n)
										-- calc ISA DEV
										n = tonumber(tc_oat_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										isa_dev_c_mod = string.format("%3d", n - alt_temp)
										n = tonumber(isa_dev_c_mod)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										entry2 = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								else
									if n < -70 or n > 70 then	-- T/C OAT Celsius min and max
										add_fmc_msg(INVALID_INPUT, 1)
									else
										tc_oat_c_mod = string.format("%3d", n)
										n = (n * 9 / 5) + 32
										tc_oat_f_mod = string.format("%4d", n)
										-- calc ISA DEV
										n = tonumber(tc_oat_c_mod)
										alt_temp = math.min(alt_temp, 37000)
										alt_temp = 15 - (2 * (alt_temp / 1000))
										alt_temp = math.max(alt_temp, -56.5)
										isa_dev_c_mod = string.format("%3d", n - alt_temp)
										n = tonumber(isa_dev_c_mod)
										n = n * 9 / 5
										isa_dev_f_mod = string.format("%4d", n)
										entry2 = ""
										if perf_exec == 2 then
											perf_exec = 3
										elseif perf_exec < 2 then
											isa_dev_f = isa_dev_f_mod
											isa_dev_c = isa_dev_c_mod
											tc_oat_c = tc_oat_c_mod
											tc_oat_f = tc_oat_f_mod
										end
										B738_calc_vnav_spd()
										vnav_update = 1
									end
								end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_perf2 == 2 then
			-- DES max speed kts/mach
			local strlen = string.len(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					des_max_kts = "   "
					des_max_mach = "   "
					entry2 = ""
				else
					if strlen > 2 and  strlen < 6 and string.sub(entry2, 1, 2) == "/." then		-- only mach
						local n = tonumber(string.sub(entry2, 2, strlen))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_min_mach)
							if nn == nil then
								nn = 0.4
							else
								nn = nn / 1000
							end
							if n > 0.82 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n * 1000
								des_max_mach = string.format("%03d", n)
								entry2 = ""
							end
						end
					elseif strlen == 3 then			-- only kts
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340  or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								des_max_kts = string.format("%03d", n)
								entry2 = ""
							end
						end
					elseif strlen > 5 and strlen < 9 and string.sub(entry2, 4, 5) == "/." then 	-- kts and mach
						local n = tonumber(string.sub(entry2, 1, 3))
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							local nn = tonumber(des_min_kts)
							if nn == nil then
								nn = 100
							end
							if n > 340 or n < nn then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								local kts = string.format("%03d", n)
									n = tonumber(string.sub(entry2, 5, strlen))
									if n == nil then
										add_fmc_msg(INVALID_INPUT, 1)
									else
										nn = tonumber(des_min_mach)
										if nn == nil then
											nn = 0.4
										else
											nn = nn / 1000
										end
										if n > 0.82 or n < nn then
											add_fmc_msg(INVALID_INPUT, 1)
										else
											n = n * 1000
											des_max_kts = kts
											des_max_mach = string.format("%03d", n)
											entry2 = ""
										end
									end
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		----------
		elseif page_rte_legs2 > 0 then
			-- entry2 WIND DATA
			local strlen = string.len(entry2)
			local item = 0
			local button = 4	-- button 4 RSK
			local qqq = 0
			
			-- local offset_tmp = offset
			-- if legs_num <= 1 then
				-- offset_tmp = 1
			-- end
			local offset_tmp = offset_mod
			if nav_mode == 99 then
				item = legs_num2 + 2
			else
				item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
				if B738DR_no_rte2 == 0 and act_page2 > 1 then
					item = item - 1
				end
			end
			
			if strlen > 0 and item <= legs_num2 and item > 0 then
				if entry2 == ">DELETE" then
					legs_data2[item][39] = ""
					entry2 = ""
					if rte12_act == rte12_sel_fo then
						legs_delete = 1
					end
					fms_recalc = 1
					msg_unavaible_crz_alt = 0
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
						if item + 1 <= legs_num2 + 1 then
							for qqq = item + 1, legs_num2 + 1 do
								legs_data2[qqq][38] = legs_data2[item][39]
							end
						end
						entry2 = ""
						if rte12_act == rte12_sel_fo then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					end
				end
			else
				if legs_data2[item][39] ~= "" then
					-- entry2 = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
					entry2 = string.sub(legs_data2[item][39], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
				elseif legs_data2[item][38] ~= "" then
					-- entry2 = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
					entry2 = string.sub(legs_data2[item][38], 1, 3) .. "/"
					entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
				end
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			-- enter EO ACCELERATION HEIGHT AGL
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					eo_accel_alt = "----"
					eo_accel_alt_num = B738DR_def_eo_accel_alt
					entry2 = ""
				elseif strlen > 2 and strlen < 5 then
					local n = tonumber(entry2)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 400 or n > 9999 then	-- ACCEL HT AGL min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							eo_accel_alt = string.format("%4d", n)
							entry2 = ""
							eo_accel_alt_num = n
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				add_fmc_msg(INVALID_INPUT, 1)
			end
		elseif page_nearest_apt2 == 1 and act_page2 == 1 then
			if nearest_apt_list_num >= 4 then
				act_page2 = 5
			end
		elseif page_altern_apt2 == 1 and act_page2 == 1 then
			if altern_apt_list_num >= 4 then
				act_page2 = 5
			end
		elseif page_dl_acars2 == 1 then
			page_dl_acars2 = 0
			page_dl_requests2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_eo_cruise2 == 1 then
			engout_crz = 3
		elseif page_eo_climb2 == 1 then
			engout_clb = 3
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 5RSK
function B738_fmc2_5R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_dep2 == 1 then
			-- if in_flight_mode == 0 then
				if rte12_sel_fo == 0 and (in_flight_mode == 0 or rte12_act ~= 0) then
					if sel_ref_rwy2 == "-----" then
						if ref_rwy_sel_fo[5] ~= "" then
							if ref_rwy2 == ref_rwy_sel_fo[5] then
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel_fo)
							else
								ref_rwy2 = ref_rwy_sel_fo[5]
								sel_ref_rwy2 = ref_rwy2
								create_sid_list(rte12_sel_fo)
								ref_rwy3 = ref_rwy2
								act_page2 = 1
								ref_rwy_exec = 1
							end
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
					-- check SID for selected runway
					if rte12_act == rte12_sel_fo then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				elseif rte12_sel_fo == 1 and (in_flight_mode == 0 or rte12_act ~= 1) then
					if sel_ref_rwy8 == "-----" then
						if ref_rwy_sel_fo[5] ~= "" then
							if ref_rwy8 == ref_rwy_sel_fo[5] then
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel_fo)
							else
								ref_rwy8 = ref_rwy_sel_fo[5]
								sel_ref_rwy8 = ref_rwy8
								create_sid_list(rte12_sel_fo)
								ref_rwy9 = ref_rwy8
								act_page2 = 1
								ref_rwy_exec = 1
							end
						end
					end
					rte_add_dep_arr(rte12_sel_fo)
					-- check SID for selected runway
					if rte12_act == rte12_sel_fo then
						if check_sid_rwy() == false then
							add_fmc_msg(RWY_NA_FOR_SID, 1)
						end
					end
				end
			-- end
		elseif page_arr2 == 1 then
			if rte12_sel_fo == 0 then
				if sel_des_app2 == "------" then
					if des_app_sel_fo[5] ~= "" then
						if des_app2 == des_app_sel_fo[5] then
							sel_des_app2 = des_app2
							if string.sub(sel_des_app2, 1, 2) == "RW" then
								des_app_rw_only_x = 1
								des_star_trans2 = "------"
								sel_des_star_trans2 = des_star_trans2
								create_star_list(rte12_sel_fo)
							else
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
							end
							if sel_des_star2 == "------" then
								create_star_list(rte12_sel_fo)
							end
							create_app_tns_list(rte12_sel_fo)
						else
							des_app2 = des_app_sel_fo[5]
							sel_des_app2 = des_app2
							if string.sub(sel_des_app2, 1, 2) == "RW" then
								des_app_rw_only_x = 1
								des_star_trans2 = "------"
								sel_des_star_trans2 = des_star_trans2
								create_star_list(rte12_sel_fo)
							else
								des_app_rw_only_x = 0
								rw_ext_dist_x = "--.-"
								rw_ext_fpa_x = "-.--"
							end
							if sel_des_star2 == "------" then
								create_star_list(rte12_sel_fo)
							end
							des_app_exec = 1
							create_app_tns_list(rte12_sel_fo)
						end
						act_page2 = 1
					end
				else
					if sel_des_app_tns2 == "------" then
						if des_tns_sel_fo[5] ~= "" then
							if des_app_tns2 == des_tns_sel_fo[5] then
								sel_des_app_tns2 = des_app_tns2
							else
								des_app_tns2 = des_tns_sel_fo[5]
								sel_des_app_tns2 = des_app_tns2
								des_app_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			else
				if sel_des_app8 == "------" then
					if des_app_sel_fo[5] ~= "" then
						if des_app8 == des_app_sel_fo[5] then
							sel_des_app8 = des_app8
							if string.sub(sel_des_app8, 1, 2) == "RW" then
								des_app_rw_only7_x = 1
								des_star_trans8 = "------"
								sel_des_star_trans8 = des_star_trans8
								create_star_list(rte12_sel_fo)
							else
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
							end
							if sel_des_star8 == "------" then
								create_star_list(rte12_sel_fo)
							end
							create_app_tns_list(rte12_sel_fo)
						else
							des_app8 = des_app_sel_fo[5]
							sel_des_app8 = des_app8
							if string.sub(sel_des_app8, 1, 2) == "RW" then
								des_app_rw_only7_x = 1
								des_star_trans8 = "------"
								sel_des_star_trans8 = des_star_trans8
								create_star_list(rte12_sel_fo)
							else
								des_app_rw_only7_x = 0
								rw_ext_dist7_x = "--.-"
								rw_ext_fpa7_x = "-.--"
							end
							if sel_des_star8 == "------" then
								create_star_list(rte12_sel_fo)
							end
							des_app_exec = 1
							create_app_tns_list(rte12_sel_fo)
						end
						act_page2 = 1
					end
				else
					if sel_des_app_tns8 == "------" then
						if des_tns_sel_fo[5] ~= "" then
							if des_app_tns8 == des_tns_sel_fo[5] then
								sel_des_app_tns8 = des_app_tns8
							else
								des_app_tns8 = des_tns_sel_fo[5]
								sel_des_app_tns8 = des_app_tns8
								des_app_tns_exec = 1
							end
							act_page2 = 1
						end
					end
				end
			end
			rte_add_dep_arr(rte12_sel_fo)
		elseif page_rte_init2 == 1 then
			if act_page2 == 1 then
				if B738DR_fmc_exec_lights_fo == 0 then
					if rte12_sel_fo == 0 and legs_num > 1 and rte12_act == rte12_sel_fo then
						if rte12_copy_done == 0 then
							rte12_copy_done = 1
							run_after_time(rte12_copy_t, 0.5)
						end
					elseif rte12_sel_fo ~= 0 and legs_num > 1 and rte12_act == rte12_sel_fo then
						if rte21_copy_done == 0 then
							rte21_copy_done = 1
							run_after_time(rte21_copy_t, 0.5)
						end
					end
				end
			elseif act_page2 > 1 then
				-- button 5 RSK
				rte_x_rsk(5, 1, rte12_sel_fo)
			end
		elseif page_legs2 == 1 then
			local item = 0
			local button = 5	-- button 5 RSK
			
			if B738DR_no_rte2 ~= 0 or act_page2 ~= 1 then
				local ab_avail = 0
				if B738DR_no_rte2 ~= 0 and act_page2 == 1 then
					if legs_intdir ~= 0 then
						if B738DR_legs_abeam_avail ~= 0 then
							ab_avail = 1
						end
					end
				end
				
				if ab_avail == 0 then
					local offset_tmp = offset_mod
					if rte12_sel_fo ~= 0 then
						offset_tmp = offset_mod2
					end
					item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
					if B738DR_no_rte2 == 0 and act_page2 > 1 then
						item = item - 1
					end
					
					if entry2 ~= "" then
						if set_spd_alt_rest2(item, rte12_sel_fo) == true then
							if rte12_act == rte12_sel_fo then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
						end
					end
				else
					if legs_abeam_selected == 0 then
						legs_abeam_selected = 1
					else
						legs_abeam_selected = 0
					end
				end
			else
				if B738DR_legs_abeam_avail ~= 0 and legs_intdir ~= 0 and legs_intdir_rte == rte12_act then
					if legs_abeam_selected == 0 then
						legs_abeam_selected = 1
					else
						legs_abeam_selected = 0
					end
				end
			end
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			if verify_to_spd ~= 0 then
				-- ACCEPT
				-- v1_set = v1
				-- vr_set = vr
				-- v2_set = v2
				verify_to_spd = 0
				qrh = " ON"
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			-- entry THR REDUCTION ALT AGL
			local strlen = string.len(entry2)
			if B738DR_thr_cutback == 0 then
				if strlen > 0 then
					if entry2 == ">DELETE" then
						clb_alt = "----"
						if B738DR_thr_cutback == 0 then
							clb_alt_num = B738DR_def_clb_alt
						else
							clb_alt_num = B738DR_def_clb_alt2
						end
						entry2 = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 800 or n > 9999 then	-- THR RED ALT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_alt = string.format("%4d", n)
								entry2 = ""
								clb_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				if strlen > 0 then
					if entry2 == ">DELETE" then
						clb_alt = "----"
						if B738DR_thr_cutback == 0 then
							clb_alt_num = B738DR_def_clb_alt
						else
							clb_alt_num = B738DR_def_clb_alt2
						end
						entry2 = ""
					elseif strlen > 2 and strlen < 5 then
						local n = tonumber(entry2)
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 800 or n > 9999 then	-- THR RED ALT AGL min and max
								add_fmc_msg(INVALID_INPUT, 1)
							else
								clb_alt = string.format("%4d", n)
								entry2 = ""
								clb_alt_num = n
							end
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			end
		elseif page_approach2 == 1 then
			-- WIND CORR
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					wind_corr = "--"
					entry2 = ""
				elseif strlen < 3 then
					local n = tonumber(entry2)
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 5 or n > 20 then	-- WIND CORR min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							wind_corr = string.format("%02d", n)
							entry2 = ""
						end
					end
				else
					add_fmc_msg(INVALID_INPUT, 1)
				end
			else
				if wind_corr == "--" then
					entry2 = 5
				else
					entry2 = wind_corr
				end
			end
		elseif page_descent_forecast2 == 1 then
			-- entry2 WIND LAYER 3
			local strlen = string.len(entry2)
			if strlen > 0 then
				if entry2 == ">DELETE" then
					forec_dir_3_mod = "---"
					forec_spd_3_mod = "---"
					entry2 = ""
					forecast_exec = 1
				else
					local q = 0
					local qq = 0
					q, qq = entry_wind(entry2)
					if qq == 0 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						forec_dir_3_mod = string.format("%03d", q)
						forec_spd_3_mod = string.format("%3d", qq)
						entry2 = ""
						forecast_exec = 1
					end
					-- if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
						-- local n = tonumber(string.sub(entry2, 1, 3))
						-- if n == nil then
							-- add_fmc_msg(INVALID_INPUT, 1)
						-- else
							-- if n < 0 or n > 360 then		-- wind heading 0 - 359
								-- add_fmc_msg(INVALID_INPUT, 1)
							-- else
								-- n = n % 360
								-- local wind_dir = string.format("%03d", n)
								-- n = tonumber(string.sub(entry2, 5, strlen))
								-- if n == nil then
									-- add_fmc_msg(INVALID_INPUT, 1)
								-- else
									-- if n < 1 or n > 199 then	-- wind speed 1 - 199
										-- add_fmc_msg(INVALID_INPUT, 1)
									-- else
										-- forec_dir_3_mod = wind_dir
										-- forec_spd_3_mod = string.format("%3d", n)
										-- entry2 = ""
										-- forecast_exec = 1
									-- end
								-- end
							-- end
						-- end
					-- else
						-- add_fmc_msg(INVALID_INPUT, 1)
					-- end
				end
			end
		elseif page_perf2 == 1 then
			-- entry2 Trans alt
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			local n_str = ""
			if strlen > 0 then
				if entry2 == ">DELETE" then
					trans_alt_mod = "-----"
					n = 18000
					if crz_alt ~= "*****" then
						n_str = string.sub(crz_alt, 1, 2)
						if crz_alt_num >= n then
							if n_str ~= "FL" then
								--n_str = string.sub(crz_alt)
								n = tonumber(crz_alt) / 100
								crz_alt = "FL" .. string.format("%03d", n)
							end
						else
							if n_str == "FL" then
								n_str = string.sub(crz_alt, 3, 5)
								n = tonumber(n_str) * 100
								crz_alt = string.format("%5d", n)
							end
						end
					end
					if perf_exec == 2 then
						perf_exec = 3
					elseif perf_exec < 2 then
						trans_alt = trans_alt_mod
					end
					entry2 = ""
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 1000 or n > 99999 then	-- Trans alt min and max
							add_fmc_msg(INVALID_INPUT, 1)
						else
							trans_alt_mod = string.format("%5d", n)
							if crz_alt ~= "*****" then
								n_str = string.sub(crz_alt, 1, 2)
								if crz_alt_num >= n then
									if n_str ~= "FL" then
										--n_str = string.sub(crz_alt, 3, 5)
										n = tonumber(crz_alt) / 100
										crz_alt = "FL" .. string.format("%03d", n)
									end
								else
									if n_str == "FL" then
										n_str = string.sub(crz_alt, 3, 5)
										n = tonumber(n_str) * 100
										crz_alt = string.format("%5d", n)
									end
								end
							end
							entry2 = ""
							if perf_exec == 2 then
								perf_exec = 3
							elseif perf_exec < 2 then
								trans_alt = trans_alt_mod
							end
						end
					end
				end
			end
		elseif page_ref_nav_data_wpt2 == 1 then
			local strlen = string.len(entry2)
			local ii = 0
			local jj = 0
			local entry2_ok = 0
			if strlen > 0 then
				if entry2 == ">DELETE" and ref_nav_brg ~= "-----" then
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					ref_nav_lat = 0
					ref_nav_lon = 0
					ref_nav_exec = 1
					entry2 = ""
				else
					if strlen > 4 and strlen < 8 and string.sub(entry2, 4, 4) == "/" then
						ii = tonumber(string.sub(entry2, 1, 3))
						if ii ~= nil then
							if ii >= 0 and ii <= 359 then
								entry2_ok = entry2_ok + 1
							end
						end
						jj = tonumber(string.sub(entry2, 5, -1))
						if jj ~= nil then
							if jj >= 0 and jj <= 999 then
								entry2_ok = entry2_ok + 1
							end
						end
						entry2 = ""
					end
					if entry2_ok == 2 then
						ref_nav_brg = string.format("%03d", ii)
						ref_nav_dist = string.format("%3d", jj)
						-- calc lat / lon
						if ref_nav_id ~= "-----" then
							calc_brg_dist(ref_nav_id_lat, ref_nav_id_lon, math.rad(tonumber(ref_nav_brg)), tonumber(ref_nav_dist))
							ref_nav_lat = calc_lat
							ref_nav_lon = calc_lon
							ref_nav_exec = 1
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				end
			end
		elseif page_pos_init2 == 1 and disable_POS_5R == 0 then
			-- SET IRS HDG
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			if strlen == 0 then
				 add_fmc_msg(INVALID_INPUT, 1)
			else
				if entry2 == ">DELETE" then
					irs_hdg = "---`"
					entry2 = ""
					B738DR_irs_hdg_fmc_set = string.sub(irs_hdg, 1, 3)
					B738CMD_fmc_to_irs_hdg:once()
				else
					if n == nil then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						if n < 0 or n > 360 or strlen ~= 3 then	-- HDG min and max
							-- TO DO INHIBIT FRAC NUMBER
							add_fmc_msg(INVALID_INPUT, 1)
						else
							n = n % 360
							irs_hdg = string.format("%03d", n)
							irs_hdg = irs_hdg .. "`"
							entry2 = ""
							B738DR_irs_hdg_fmc_set = string.sub(irs_hdg, 1, 3)
							B738CMD_fmc_to_irs_hdg:once()
						end
					end
				end
			end
		---
		elseif page_rte_legs2 > 0 then
			-- entry2 WIND DATA
			local strlen = string.len(entry2)
			local item = 0
			local button = 5	-- button 5 RSK
			local qqq = 0
			
			if B738DR_no_rte2 ~= 0 or act_page2 ~= 1 then
				-- local offset_tmp = offset
				-- if legs_num <= 1 then
					-- offset_tmp = 1
				-- end
				local offset_tmp = offset_mod
				if nav_mode == 99 then
					item = legs_num2 + 2
				else
					item = (act_page2 - 1) * 5 + offset_tmp - 1 + button
					if B738DR_no_rte2 == 0 and act_page2 > 1 then
						item = item - 1
					end
				end
				
				if strlen > 0 and item <= legs_num2 and item > 0 then
					if entry2 == ">DELETE" then
						legs_data2[item][39] = ""
						entry2 = ""
						if rte12_act == rte12_sel_fo then
							legs_delete = 1
						end
						fms_recalc = 1
						msg_unavaible_crz_alt = 0
					else
						local q = 0
						local qq = 0
						q, qq = entry_wind(entry2)
						if qq == 0 then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							legs_data2[item][39] = string.format("%03d", q) .. "`/" .. string.format("%3d", qq)
							if item + 1 <= legs_num2 + 1 then
								for qqq = item + 1, legs_num2 + 1 do
									legs_data2[qqq][38] = legs_data2[item][39]
								end
							end
							entry2 = ""
							if rte12_act == rte12_sel_fo then
								legs_delete = 1
							end
							fms_recalc = 1
							msg_unavaible_crz_alt = 0
						end
					end
				else
					if legs_data2[item][39] ~= "" then
						-- entry2 = string.sub(legs_data2[item][39], 1, 3) .. string.sub(legs_data2[item][39], -4, -1)
						entry2 = string.sub(legs_data2[item][39], 1, 3) .. "/"
						entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][39], -3, -1)))
					elseif legs_data2[item][38] ~= "" then
						-- entry2 = string.sub(legs_data2[item][38], 1, 3) .. string.sub(legs_data2[item][38], -4, -1)
						entry2 = string.sub(legs_data2[item][38], 1, 3) .. "/"
						entry2 = entry2 .. string.format("%d", tonumber(string.sub(legs_data2[item][38], -3, -1)))
					end
				end
			end
		elseif page_ac_atis2 == 1 then
			if acars_atis_status == 0 and ac_atis_icao ~= "----" then	--and B738DR_com3_data ~= 0 then
				acars_atis_status = 1
				atis_msg_status = 0
				vhf_in_prog = " VHF IN PROG"
				receive_msg = 0
				-- atis_msg_status = 0
				atis_send_time = "     "
			end
		elseif page_descent2 == 1 then
			if vnav_desc_spd_disable == 0 then
				-- if B738DR_descent_mode == 1 or B738DR_descent_mode == 3 then
					-- -- DES SPD
					-- if B738DR_descent_mode ~= 0 then
						-- if B738DR_descent_mode == 1 then
							-- -- DES PTH ECON
							-- entry2 = ""
							-- B738DR_descent_mode = 0
							-- B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
							-- B738DR_fmc_descent_speed = econ_des_spd				-- temporary
							-- change_desc_to_path = 1
						-- else
							-- -- DES PTH
							-- entry2 = ""
							-- B738DR_descent_mode = 2
							-- change_desc_to_path = 1
						-- end
					-- end
				-- else
					-- -- DES PTH
					-- if B738DR_descent_mode == 0 then
						-- -- DES SPD ECON
						-- entry2 = ""
						-- B738DR_descent_mode = 1
						-- B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
						-- B738DR_fmc_descent_speed = econ_des_spd				-- temporary
						-- change_desc_to_path = 0
					-- else
						-- -- DES SPD
						-- entry2 = ""
						-- B738DR_descent_mode = 3
						-- change_desc_to_path = 0
					-- end
				-- end
				if fmc_descent_mode_mod == 1 or fmc_descent_mode_mod == 3 then
					-- DES SPD
					if fmc_descent_mode_mod ~= 0 then
						if fmc_descent_mode_mod == 1 then
							-- DES PTH ECON
							entry2 = ""
							fmc_descent_mode_mod = 0
							des_spd_exec = 1
							fmc_descent_speed_mod = econ_des_spd
							fmc_descent_speed_mach_mod = econ_des_spd_mach
							-- B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
							-- B738DR_fmc_descent_speed = econ_des_spd				-- temporary
							-- change_desc_to_path = 1
						else
							-- DES PTH
							entry2 = ""
							fmc_descent_mode_mod = 2
							des_spd_exec = 1
							-- change_desc_to_path = 1
						end
					end
				else
					-- DES PTH
					if fmc_descent_mode_mod == 0 then
						-- DES SPD ECON
						entry2 = ""
						fmc_descent_mode_mod = 1
						des_spd_exec = 1
						fmc_descent_speed_mod = econ_des_spd
						fmc_descent_speed_mach_mod = econ_des_spd_mach
						-- B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
						-- B738DR_fmc_descent_speed = econ_des_spd				-- temporary
						-- change_desc_to_path = 0
					else
						-- DES SPD
						entry2 = ""
						fmc_descent_mode_mod = 3
						des_spd_exec = 1
						-- change_desc_to_path = 0
					end
				end
			end
		elseif page_nearest_apt2 == 1 and act_page2 == 1 then
			if nearest_apt_list_num >= 5 then
				act_page2 = 6
			end
		elseif page_altern_apt2 == 1 and act_page2 == 1 then
			if altern_apt_list_num >= 5 then
				act_page2 = 6
			end
		elseif page_dl_wxr_req2 == 1 then
			-- send ICAO WXR request
			if acars_atis_status == 0 and ac_atis_icao ~= "----" then
				acars_atis_status = 1
				atis_msg_status = 0
				vhf_in_prog = " VHF IN PROG"
				receive_msg = 0
				atis_send_time = "     "
				page_dl_wxr_req2 = 0
				page_dl_requests2 = 1
				act_page2 = 1
				display_update = 1
			end
		elseif page_dl_fltlog2 == 1 then
			page_dl_fltlog2 = 0
			page_dl_sensors2 = 1
			act_page2 = 1
			display_update = 1
		elseif page_cruise2 == 1 then
			if engout_crz == 0 then
				engout_crz = 1
			elseif engout_crz == 1 then
				engout_crz = 3
				page_cruise2 = 0
				page_eo_cruise2 = 1
			else
				page_cruise2 = 0
				page_eo_cruise2 = 1
			end
		elseif page_climb2 == 1 then
			if engout_clb == 0 then
				engout_clb = 1
			else
				engout_clb = 3
				page_climb2 = 0
				page_eo_climb2 = 1
			end
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- 6RSK
function B738_fmc2_6R_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		
		if page_rte_init2 == 1 then
			if B738DR_fmc_exec_lights_fo == 0 then
				if legs_num > 1 and rte12_act == rte12_sel_fo then
					if in_flight_mode == 0 then
						if pre_flt_perf_init == 0 then
							-- go to perf init
							page_perf2 = 1
							page_rte_init2 = 0
						else
							-- go to takeoff
							page_takeoff2 = 1
							page_rte_init2 = 0
						end
					else
						-- go to offset page
						page_rte_init2 = 0
						page_offset2 = 1
						item_sel2 = 0
						item_sel_act2 = 0
						item_sel_via2 = 0
					end
				else
					if legs_num2 > 1 and rte12_act ~= rte12_sel_fo and rte12_sel_fo == 0 then
						-- ACTIVATE Flight plan
						rte_exec = 1
						exec_activate_fpln = 1
						exec_activate_fpln2 = 0
						
						copy_to_legs_data2_buf(1)
						if legs_num2_buf > 1 then
							for n = 1, (legs_num2_buf + 1) do
								legs_data2_buf[n][11] = 0
								legs_data2_buf[n][10] = 0
								legs_data2_buf[n][14] = 50000
								legs_data2_buf[n][38] = ""
							end
						end
						copy_from_legs_data2_buf(1)
						
						local disable_disco = false
						if in_flight_mode == 0 then
							disable_disco = true
						elseif offset_mod > 0 and offset_mod <= legs_num2 then
							if legs_data2[offset_mod][1] == "DISCONTINUITY" then
								disable_disco = true
							end
						end
						if disable_disco == false then
							--create discontinuity
							rte_copy(offset_mod, rte12_sel_fo)
							rte_add_disco(offset_mod, rte12_sel_fo)
							rte_paste(offset_mod + 1, rte12_sel_fo)
						end
					elseif legs_num8 > 1 and rte12_act ~= rte12_sel_fo and rte12_sel_fo == 1 then
						-- ACTIVATE Flight plan
						rte_exec2 = 1
						exec_activate_fpln2 = 1
						exec_activate_fpln = 0
						
						copy_to_legs_data2_buf(0)
						if legs_num2_buf > 1 then
							for n = 1, (legs_num2_buf + 1) do
								legs_data2_buf[n][11] = 0
								legs_data2_buf[n][10] = 0
								legs_data2_buf[n][14] = 50000
								legs_data2_buf[n][38] = ""
							end
						end
						copy_from_legs_data2_buf(0)
						
						local disable_disco = false
						if in_flight_mode == 0 then
							disable_disco = true
						elseif offset_mod2 > 0 and offset_mod2 <= legs_num8 then
							if legs_data8[offset_mod2][1] == "DISCONTINUITY" then
								disable_disco = true
							end
						end
						if disable_disco == false then
							--create discontinuity
							rte_copy(offset_mod2, rte12_sel_fo)
							rte_add_disco(offset_mod2, rte12_sel_fo)
							rte_paste(offset_mod2 + 1, rte12_sel_fo)
						end
					end
				end
			end
		elseif page_dep2 == 1 then
			-- go to route
			reset_fmc_pages_fo()
			page_rte_init2 = 1
			act_page2 = 1
		elseif page_arr2 == 1 then
			-- go to route
			reset_fmc_pages_fo()
			page_rte_init2 = 1
			act_page2 = 1
		elseif page_legs2 == 1 then
			local strlen = string.len(entry2)
			local n = tonumber(entry2)
			local rte2_corr = 0
			if B738DR_fmc_exec_lights_fo == 1 then --and legs_intdir == 1 then
				if legs_intdir == 1 and legs_intdir_rte == rte12_act then
					if strlen == 3 then
						if n == nil then
							add_fmc_msg(INVALID_INPUT, 1)
						else
							if n < 0 or n > 360 then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								n = n % 360
								legs_intdir_crs2 = n
								legs_intdir_crs_mod = legs_intdir_crs2
								entry2 = ""
							end
						end
					elseif strlen == 5 then
						if string.sub(entry2, 4, 4) == "." then
							if n == nil then
								add_fmc_msg(INVALID_INPUT, 1)
							else
								if n < 0 or n > 360 then
									add_fmc_msg(INVALID_INPUT, 1)
								else
									n = n % 360
									legs_intdir_crs2 = n
									legs_intdir_crs_mod = legs_intdir_crs2
									entry2 = ""
								end
							end
						else
							add_fmc_msg(INVALID_INPUT, 1)
						end
					else
						add_fmc_msg(INVALID_INPUT, 1)
					end
				else
					if B738DR_fo_map_mode == 3 then
						
						local offset_step = 1
						if rte12_sel_fo == 0 then
							if legs_num > 1 and rte12_act == 0 then
								offset_step = offset_mod
							elseif legs_num <= 0 or legs_num2 <= 1 then
								offset_step = offset_mod
							end
							
							legs_step2 = legs_step2 + 1
							if legs_step2 > legs_num2 or legs_step2 < offset_step then
								legs_step2 = offset_step
							end
							if legs_step2 > legs_num2 then
								legs_step2 = legs_num2
							end
							if legs_data2[legs_step2][1] == "DISCONTINUITY" then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num2 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num2 then
									legs_step2 = legs_num2
								end
							end
							if B738DR_no_rte2 == 0 then
								if (legs_step2 - offset_step) >= 4 then
									rte2_corr = 1
								end
							end
							page_legs_step2 = math.floor((legs_step2 - offset_step + rte2_corr) / 5) + 1
							act_page2 = page_legs_step2
						else
							if legs_num > 1 and rte12_act == 1 then
								offset_step = offset_mod2
							elseif legs_num <= 0 or legs_num2 <= 1 then
								offset_step = offset_mod2
							end
							
							legs_step2 = legs_step2 + 1
							if legs_step2 > legs_num8 or legs_step2 < offset_step then
								legs_step2 = offset_step
							end
							if legs_step2 > legs_num8 then
								legs_step2 = legs_num8
							end
							if legs_data8[legs_step2][1] == "DISCONTINUITY" then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num8 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num8 then
									legs_step2 = legs_num8
								end
							end
							if B738DR_no_rte2 == 0 then
								if (legs_step2 - offset_step) >= 4 then
									rte2_corr = 1
								end
							end
							page_legs_step2 = math.floor((legs_step2 - offset_step + rte2_corr) / 5) + 1
							act_page2 = page_legs_step2
						end
					else
						-- RTE DATA
						page_rte_legs2 = 1
						page_legs2 = 0
					end
				end
			else
				
				
				local n = 0
				local kk = 0
				local ii = 0
				local jj = 0
				local lock_hold_ppos = 0
				
				local offset_step = offset_mod
				if rte12_sel_fo ~= 0 then
					offset_step = offset_mod2
				end
				
				if legs_num > 1 and rte12_act == rte12_sel_fo then
					if B738DR_fo_map_mode == 3 then
						
						if new_hold ~= 0 and new_hold_at == 0 and rte12_sel_fo == 0 then
							if in_flight_mode == 1 and (nav_mode ~= 3 or rte12_sel_fo ~= rte12_act) and new_hold_at == 0 then
								n = 0
								if hold_data_num2 > 0 then
									for kk = 1, hold_data_num2 do
										if hold_data2[kk] >= offset_mod then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data2[hold_data2[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel2 = 0
									item_sel_act2 = 0
									item_sel_via2 = 0
									entry2 = ""
									if rte12_act == rte12_sel_fo then
										hold_exec = 1
									end
									page_hold2 = 1
									page_legs2 = 0
									act_page2 = 1
									--create new hold
									kk = 0
									legs_data2b = {}
									for ii = offset_mod, legs_num2 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data2[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod
									legs_data2[n] = {}
									legs_data2[n][1] = "PPOS"
									legs_data2[n][2] = 0
									legs_data2[n][3] = 0
									
									legs_data2[n][4] = AP_airspeed	--0
									legs_data2[n][5] = 0
									legs_data2[n][6] = 0
									
									if legs_data2[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data2[n][5] <= 6000 then
											if legs_data2[n][4] > 200 then
												legs_data2[n][4] = 200
											end
										elseif legs_data2[n][5] <= 14000 then
											if legs_data2[n][4] > 230 then
												legs_data2[n][4] = 230
											end
										else
											if legs_data2[n][4] > 265 then
												legs_data2[n][4] = 265
											end
										end
									elseif legs_data2[n][4] > 0 and legs_data2[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data2[n][4] > 0.76 then
											legs_data2[n][4] = 0.76
										end
									end
									
									legs_data2[n][7] = 0		-- latitude
									legs_data2[n][8] = 0		-- longitude
									legs_data2[n][9] = ""			-- via id
									legs_data2[n][10] = 0		-- calc speed
									legs_data2[n][11] = 0		-- calc altitude
									legs_data2[n][12] = 0		-- calc altitude vnav pth
									legs_data2[n][13] = 0
									legs_data2[n][14] = 0		-- rest alt
									legs_data2[n][15] = 0		-- last fuel
									legs_data2[n][16] = ""
									legs_data2[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data2[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data2[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data2[n][20] = 0	-- vpa
									legs_data2[n][21] = 1		-- HOLD right default
									legs_data2[n][22] = ""
									legs_data2[n][23] = 0
									legs_data2[n][24] = 0
									legs_data2[n][25] = 0
									legs_data2[n][26] = 0
									legs_data2[n][27] = ""
									legs_data2[n][28] = ""
									legs_data2[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data2[n][30] = ""
									legs_data2[n][31] = "HM"
									legs_data2[n][32] = 0
									legs_data2[n][33] = ""
									legs_data2[n][34] = ""
									legs_data2[n][35] = ""
									legs_data2[n][36] = 0	--4
									legs_data2[n][37] = 0
									legs_data2[n][38] = ""
									legs_data2[n][39] = ""
									legs_data2[n][40] = 0
									legs_data2[n][41] = 0
									legs_data2[n][42] = 0
									legs_data2[n][43] = ""
									legs_data2[n][44] = 0
									legs_data2[n][45] = 0
									legs_data2[n][46] = 0
									legs_data2[n][47] = 0
									legs_data2[n][48] = ""
									legs_data2[n][49] = 0
									legs_data2[n][50] = 0
									legs_data2[n][51] = 0
									legs_data2[n][52] = -1
									legs_data2[n][53] = 0
									legs_data2[n][54] = 0
									-- add disco
									n = n + 1
									legs_data2[n] = {}
									rte_add_disco(n, rte12_sel_fo)
									legs_data2[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data2[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num2 = n - 1
									
									-- create hold data
									hold_data_num2 = 0
									hold_data2 = {}
									for ii = 1, legs_num2 do
										if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
											hold_data_num2 = hold_data_num2 + 1
											hold_data2[hold_data_num2] = ii
											if ii == new_hold_idx + 1 then
												new_hold_act_page = hold_data_num2
											end
										end
									end
								end
							end
						elseif new_hold2 ~= 0 and new_hold_at2 == 0 and rte12_sel_fo == 1 then
							if in_flight_mode == 1 and (nav_mode ~= 3 or rte12_sel_fo ~= rte12_act) and new_hold_at2 == 0 then
								n = 0
								if hold_data_num4 > 0 then
									for kk = 1, hold_data_num4 do
										if hold_data4[kk] >= offset_mod2 then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data8[hold_data4[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel2 = 0
									item_sel_act2 = 0
									item_sel_via2 = 0
									entry2 = ""
									if rte12_act == rte12_sel_fo then
										hold_exec2 = 1
									end
									page_hold2 = 1
									page_legs2 = 0
									act_page2 = 1
									--new_hold = 1
									--create new hold
									kk = 0
									legs_data2b = {}
									for ii = offset_mod2, legs_num8 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data8[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod2
									legs_data8[n] = {}
									legs_data8[n][1] = "PPOS"
									legs_data8[n][2] = 0
									legs_data8[n][3] = 0
									
									legs_data8[n][4] = AP_airspeed	--0
									legs_data8[n][5] = 0
									legs_data8[n][6] = 0
									
									if legs_data8[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data8[n][5] <= 6000 then
											if legs_data8[n][4] > 200 then
												legs_data8[n][4] = 200
											end
										elseif legs_data8[n][5] <= 14000 then
											if legs_data8[n][4] > 230 then
												legs_data8[n][4] = 230
											end
										else
											if legs_data8[n][4] > 265 then
												legs_data8[n][4] = 265
											end
										end
									elseif legs_data8[n][4] > 0 and legs_data8[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data8[n][4] > 0.76 then
											legs_data8[n][4] = 0.76
										end
									end
									
									legs_data8[n][7] = 0		-- latitude
									legs_data8[n][8] = 0		-- longitude
									legs_data8[n][9] = ""			-- via id
									legs_data8[n][10] = 0		-- calc speed
									legs_data8[n][11] = 0		-- calc altitude
									legs_data8[n][12] = 0		-- calc altitude vnav pth
									legs_data8[n][13] = 0
									legs_data8[n][14] = 0		-- rest alt
									legs_data8[n][15] = 0		-- last fuel
									legs_data8[n][16] = ""
									legs_data8[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data8[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data8[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data8[n][20] = 0	-- vpa
									legs_data8[n][21] = 1		-- HOLD right default
									legs_data8[n][22] = ""
									legs_data8[n][23] = 0
									legs_data8[n][24] = 0
									legs_data8[n][25] = 0
									legs_data8[n][26] = 0
									legs_data8[n][27] = ""
									legs_data8[n][28] = ""
									legs_data8[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data8[n][30] = ""
									legs_data8[n][31] = "HM"
									legs_data8[n][32] = 0
									legs_data8[n][33] = ""
									legs_data8[n][34] = ""
									legs_data8[n][35] = ""
									legs_data8[n][36] = 0	--4
									legs_data8[n][37] = 0
									legs_data8[n][38] = ""
									legs_data8[n][39] = ""
									legs_data8[n][40] = 0
									legs_data8[n][41] = 0
									legs_data8[n][42] = 0
									legs_data8[n][43] = ""
									legs_data8[n][44] = 0
									legs_data8[n][45] = 0
									legs_data8[n][46] = 0
									legs_data8[n][47] = 0
									legs_data8[n][48] = ""
									legs_data8[n][49] = 0
									legs_data8[n][50] = 0
									legs_data8[n][51] = 0
									legs_data8[n][52] = -1
									legs_data8[n][53] = 0
									legs_data8[n][54] = 0
									-- add disco
									n = n + 1
									legs_data8[n] = {}
									rte_add_disco(n, rte12_sel_fo)
									legs_data8[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data8[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data8[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num8 = n - 1
									
									-- create hold data
									hold_data_num4 = 0
									hold_data4 = {}
									for ii = 1, legs_num8 do
										if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
											hold_data_num4 = hold_data_num4 + 1
											hold_data4[hold_data_num4] = ii
											if ii == new_hold_idx2 + 1 then
												new_hold_act_page2 = hold_data_num4
											end
										end
									end
								end
							end
						else
							-- STEP
							if rte12_sel_fo == 0 then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num2 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num2 then
									legs_step2 = legs_num2
								end
								if legs_data2[legs_step2][1] == "DISCONTINUITY" then
									legs_step2 = legs_step2 + 1
									if legs_step2 > legs_num2 or legs_step2 < offset_step then
										legs_step2 = offset_step
									end
									if legs_step2 > legs_num2 then
										legs_step2 = legs_num2
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step2 - offset_mod) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step2 = math.floor((legs_step2 - offset_mod + rte2_corr) / 5) + 1
								act_page2 = page_legs_step2
							else
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num8 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num8 then
									legs_step2 = legs_num8
								end
								if legs_data8[legs_step2][1] == "DISCONTINUITY" then
									legs_step2 = legs_step2 + 1
									if legs_step2 > legs_num8 or legs_step2 < offset_step then
										legs_step2 = offset_step
									end
									if legs_step2 > legs_num8 then
										legs_step2 = legs_num8
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step2 - offset_mod2) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step2 = math.floor((legs_step2 - offset_mod2 + rte2_corr) / 5) + 1
								act_page2 = page_legs_step2
							end
						end
					else
						if new_hold == 0 and rte12_sel_fo == 0 then
							-- RTE DATA
							page_rte_legs2 = 1
							page_legs2 = 0
							--act_page = 1
						elseif new_hold2 == 0 and rte12_sel_fo == 1 then
							-- RTE DATA
							page_rte_legs2 = 1
							page_legs2 = 0
							--act_page = 1
						else
							if in_flight_mode == 1 and (nav_mode ~= 3 or rte12_sel_fo ~= rte12_act) and new_hold_at == 0 and rte12_sel_fo == 0 then
								n = 0
								if hold_data_num2 > 0 then
									for kk = 1, hold_data_num2 do
										if hold_data2[kk] >= offset_mod then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data2[hold_data2[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel2 = 0
									item_sel_act2 = 0
									item_sel_via2 = 0
									entry2 = ""
									if rte12_act == rte12_sel_fo then
										hold_exec = 1
									end
									page_hold2 = 1
									page_legs2 = 0
									act_page2 = 1
									--create new hold
									kk = 0
									legs_data2b = {}
									for ii = offset_mod, legs_num2 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data2[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod
									legs_data2[n] = {}
									legs_data2[n][1] = "PPOS"
									legs_data2[n][2] = 0
									legs_data2[n][3] = 0
									
									legs_data2[n][4] = AP_airspeed	--0
									legs_data2[n][5] = 0
									legs_data2[n][6] = 0
									
									if legs_data2[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data2[n][5] <= 6000 then
											if legs_data2[n][4] > 200 then
												legs_data2[n][4] = 200
											end
										elseif legs_data2[n][5] <= 14000 then
											if legs_data2[n][4] > 230 then
												legs_data2[n][4] = 230
											end
										else
											if legs_data2[n][4] > 265 then
												legs_data2[n][4] = 265
											end
										end
									elseif legs_data2[n][4] > 0 and legs_data2[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data2[n][4] > 0.76 then
											legs_data2[n][4] = 0.76
										end
									end
									
									legs_data2[n][7] = 0		-- latitude
									legs_data2[n][8] = 0		-- longitude
									legs_data2[n][9] = ""			-- via id
									legs_data2[n][10] = 0		-- calc speed
									legs_data2[n][11] = 0		-- calc altitude
									legs_data2[n][12] = 0		-- calc altitude vnav pth
									legs_data2[n][13] = 0
									legs_data2[n][14] = 0		-- rest alt
									legs_data2[n][15] = 0		-- last fuel
									legs_data2[n][16] = ""
									legs_data2[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data2[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data2[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data2[n][20] = 0	-- vpa
									legs_data2[n][21] = 1		-- HOLD right default
									legs_data2[n][22] = ""
									legs_data2[n][23] = 0
									legs_data2[n][24] = 0
									legs_data2[n][25] = 0
									legs_data2[n][26] = 0
									legs_data2[n][27] = ""
									legs_data2[n][28] = ""
									legs_data2[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data2[n][30] = ""
									legs_data2[n][31] = "HM"
									legs_data2[n][32] = 0
									legs_data2[n][33] = ""
									legs_data2[n][34] = ""
									legs_data2[n][35] = ""
									legs_data2[n][36] = 0	--4
									legs_data2[n][37] = 0
									legs_data2[n][38] = ""
									legs_data2[n][39] = ""
									legs_data2[n][40] = 0
									legs_data2[n][41] = 0
									legs_data2[n][42] = 0
									legs_data2[n][43] = ""
									legs_data2[n][44] = 0
									legs_data2[n][45] = 0
									legs_data2[n][46] = 0
									legs_data2[n][47] = 0
									legs_data2[n][48] = ""
									legs_data2[n][49] = 0
									legs_data2[n][50] = 0
									legs_data2[n][51] = 0
									legs_data2[n][52] = -1
									legs_data2[n][53] = 0
									legs_data2[n][54] = 0
									-- add disco
									n = n + 1
									legs_data2[n] = {}
									rte_add_disco(n, rte12_sel_fo)
									legs_data2[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data2[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num2 = n - 1
									
									-- create hold data
									hold_data_num2 = 0
									hold_data2 = {}
									for ii = 1, legs_num2 do
										if legs_data2[ii][31] == "HA" or legs_data2[ii][31] == "HF" or legs_data2[ii][31] == "HM" then
											hold_data_num2 = hold_data_num2 + 1
											hold_data2[hold_data_num2] = ii
											if ii == new_hold_idx + 1 then
												new_hold_act_page = hold_data_num2
											end
										end
									end
								end
							elseif in_flight_mode == 1 and (nav_mode ~= 3 or rte12_sel_fo ~= rte12_act) and new_hold_at2 == 0 and rte12_sel_fo == 1 then
								n = 0
								if hold_data_num4 > 0 then
									for kk = 1, hold_data_num4 do
										if hold_data4[kk] >= offset_mod2 then
											n = kk
											break
										end
									end
									if n > 0 then
										if legs_data8[hold_data4[n]][1] == "PPOS" then
											lock_hold_ppos = 1
										end
									end
								end
								if lock_hold_ppos == 0 then
									-- HOLD at PPOS
									item_sel2 = 0
									item_sel_act2 = 0
									item_sel_via2 = 0
									entry2 = ""
									if rte12_act == rte12_sel_fo then
										hold_exec2 = 1
									end
									page_hold2 = 1
									page_legs2 = 0
									act_page2 = 1
									--create new hold
									kk = 0
									legs_data2b = {}
									for ii = offset_mod2, legs_num8 + 1 do
										kk = kk + 1
										legs_data2b[kk] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data2b[kk][jj] = legs_data8[ii][jj]
										end
									end
									legs_num2b = kk
									
									-- entry HOLD PPOS
									n = offset_mod2
									legs_data8[n] = {}
									legs_data8[n][1] = "PPOS"
									legs_data8[n][2] = 0
									legs_data8[n][3] = 0
									
									legs_data8[n][4] = AP_airspeed	--0
									legs_data8[n][5] = 0
									legs_data8[n][6] = 0
									
									if legs_data8[n][4] > 100 and simDR_airspeed_is_mach == 0 then
										if legs_data8[n][5] <= 6000 then
											if legs_data8[n][4] > 200 then
												legs_data8[n][4] = 200
											end
										elseif legs_data8[n][5] <= 14000 then
											if legs_data8[n][4] > 230 then
												legs_data8[n][4] = 230
											end
										else
											if legs_data8[n][4] > 265 then
												legs_data8[n][4] = 265
											end
										end
									elseif legs_data8[n][4] > 0 and legs_data8[n][4] < 100 and simDR_airspeed_is_mach == 1 then
										if legs_data8[n][4] > 0.76 then
											legs_data8[n][4] = 0.76
										end
									end
									
									legs_data8[n][7] = 0		-- latitude
									legs_data8[n][8] = 0		-- longitude
									legs_data8[n][9] = ""			-- via id
									legs_data8[n][10] = 0		-- calc speed
									legs_data8[n][11] = 0		-- calc altitude
									legs_data8[n][12] = 0		-- calc altitude vnav pth
									legs_data8[n][13] = 0
									legs_data8[n][14] = 0		-- rest alt
									legs_data8[n][15] = 0		-- last fuel
									legs_data8[n][16] = ""
									legs_data8[n][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
									legs_data8[n][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
									legs_data8[n][19] = 0		-- 0-none, 1-SID, 2-STAR, 3-APP
									legs_data8[n][20] = 0	-- vpa
									legs_data8[n][21] = 1		-- HOLD right default
									legs_data8[n][22] = ""
									legs_data8[n][23] = 0
									legs_data8[n][24] = 0
									legs_data8[n][25] = 0
									legs_data8[n][26] = 0
									legs_data8[n][27] = ""
									legs_data8[n][28] = ""
									legs_data8[n][29] = string.format("%03d", simDR_mag_hdg * 10)		-- ""
									legs_data8[n][30] = ""
									legs_data8[n][31] = "HM"
									legs_data8[n][32] = 0
									legs_data8[n][33] = ""
									legs_data8[n][34] = ""
									legs_data8[n][35] = ""
									legs_data8[n][36] = 0	--4
									legs_data8[n][37] = 0
									legs_data8[n][38] = ""
									legs_data8[n][39] = ""
									legs_data8[n][40] = 0
									legs_data8[n][41] = 0
									legs_data8[n][42] = 0
									legs_data8[n][43] = ""
									legs_data8[n][44] = 0
									legs_data8[n][45] = 0
									legs_data8[n][46] = 0
									legs_data8[n][47] = 0
									legs_data8[n][48] = ""
									legs_data8[n][49] = 0
									legs_data8[n][50] = 0
									legs_data8[n][51] = 0
									legs_data8[n][52] = -1
									legs_data8[n][53] = 0
									legs_data8[n][54] = 0
									-- add disco
									n = n + 1
									legs_data8[n] = {}
									rte_add_disco(n, rte12_sel_fo)
									legs_data8[n][52] = -1
									for ii = 1, legs_num2b do
										n = n + 1
										legs_data8[n] = {}
										for jj = 1, MAX_LEGS_DATA do
											legs_data8[n][jj] = legs_data2b[ii][jj]
										end
									end
									legs_num8 = n - 1
									
									-- create hold data
									hold_data_num4 = 0
									hold_data4 = {}
									for ii = 1, legs_num8 do
										if legs_data8[ii][31] == "HA" or legs_data8[ii][31] == "HF" or legs_data8[ii][31] == "HM" then
											hold_data_num4 = hold_data_num4 + 1
											hold_data4[hold_data_num4] = ii
											if ii == new_hold_idx2 + 1 then
												new_hold_act_page2 = hold_data_num4
											end
										end
									end
								end
							end
						end
					end
				else
					if rte12_sel_fo == 0 then
						if legs_num2 > 1 and rte12_act ~= rte12_sel_fo then
							if B738DR_fo_map_mode == 3 then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num2 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num2 then
									legs_step2 = legs_num2
								end
								if legs_data2[legs_step2][1] == "DISCONTINUITY" then
									legs_step2 = legs_step2 + 1
									if legs_step2 > legs_num2 or legs_step2 < offset_step then
										legs_step2 = offset_step
									end
									if legs_step2 > legs_num2 then
										legs_step2 = legs_num2
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step2 - offset_mod) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step2 = math.floor((legs_step2 - offset_mod + rte2_corr) / 5) + 1
								act_page2 = page_legs_step2
							else
								-- ACTIVATE Flight plan
								rte_exec = 1
								exec_activate_fpln = 1
								exec_activate_fpln2 = 0
								
								copy_to_legs_data2_buf(1)
								if legs_num2_buf > 1 then
									for n = 1, (legs_num2_buf + 1) do
										legs_data2_buf[n][11] = 0
										legs_data2_buf[n][10] = 0
										legs_data2_buf[n][14] = 50000
										legs_data2_buf[n][38] = ""
									end
								end
								copy_from_legs_data2_buf(1)
								
								local disable_disco = false
								if in_flight_mode == 0 then
									disable_disco = true
								elseif offset_mod > 0 and offset_mod <= legs_num2 then
									if legs_data2[offset_mod][1] == "DISCONTINUITY" then
										disable_disco = true
									end
								end
								if disable_disco == false then
									--create discontinuity
									rte_copy(offset_mod, rte12_sel_fo)
									rte_add_disco(offset_mod, rte12_sel_fo)
									rte_paste(offset_mod + 1, rte12_sel_fo)
								end
							end
						else
							if B738DR_fo_map_mode == 3 then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num2 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num2 then
									legs_step2 = legs_num2
								end
								if legs_data2[legs_step2][1] == "DISCONTINUITY" then
									legs_step2 = legs_step2 + 1
									if legs_step2 > legs_num2 or legs_step2 < offset_step then
										legs_step2 = offset_step
									end
									if legs_step2 > legs_num2 then
										legs_step2 = legs_num2
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step2 - offset_mod) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step2 = math.floor((legs_step2 - offset_mod + rte2_corr) / 5) + 1
								act_page2 = page_legs_step2
								
							else
								-- RTA DATA
								page_rte_legs2 = 1
								page_legs2 = 0
							end
						end
					else
						if legs_num8 > 1 and rte12_act ~= rte12_sel_fo then
							if B738DR_fo_map_mode == 3 then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num8 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num8 then
									legs_step2 = legs_num8
								end
								if legs_data2[legs_step2][1] == "DISCONTINUITY" then
									legs_step2 = legs_step2 + 1
									if legs_step2 > legs_num8 or legs_step2 < offset_step then
										legs_step2 = offset_step
									end
									if legs_step2 > legs_num8 then
										legs_step2 = legs_num8
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step2 - offset_mod2) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step2 = math.floor((legs_step2 - offset_mod2 + rte2_corr) / 5) + 1
								act_page2 = page_legs_step2
							else
								-- ACTIVATE Flight plan
								rte_exec2 = 1
								exec_activate_fpln2 = 1
								exec_activate_fpln = 0
								
								copy_to_legs_data2_buf(0)
								if legs_num2_buf > 1 then
									for n = 1, (legs_num2_buf + 1) do
										legs_data2_buf[n][11] = 0
										legs_data2_buf[n][10] = 0
										legs_data2_buf[n][14] = 50000
										legs_data2_buf[n][38] = ""
									end
								end
								copy_from_legs_data2_buf(0)
								
								local disable_disco = false
								if in_flight_mode == 0 then
									disable_disco = true
								elseif offset_mod2 > 0 and offset_mod2 <= legs_num8 then
									if legs_data8[offset_mod2][1] == "DISCONTINUITY" then
										disable_disco = true
									end
								end
								if disable_disco == false then
									--create discontinuity
									rte_copy(offset_mod2, rte12_sel_fo)
									rte_add_disco(offset_mod2, rte12_sel_fo)
									rte_paste(offset_mod2 + 1, rte12_sel_fo)
								end
							end
						else
							if B738DR_fo_map_mode == 3 then
								legs_step2 = legs_step2 + 1
								if legs_step2 > legs_num8 or legs_step2 < offset_step then
									legs_step2 = offset_step
								end
								if legs_step2 > legs_num8 then
									legs_step2 = legs_num8
								end
								if legs_data2[legs_step2][1] == "DISCONTINUITY" then
									legs_step2 = legs_step2 + 1
									if legs_step2 > legs_num8 or legs_step2 < offset_step then
										legs_step2 = offset_step
									end
									if legs_step2 > legs_num8 then
										legs_step2 = legs_num8
									end
								end
								if B738DR_no_rte2 == 0 then
									if (legs_step2 - offset_mod2) >= 4 then
										rte2_corr = 1
									end
								end
								page_legs_step2 = math.floor((legs_step2 - offset_mod2 + rte2_corr) / 5) + 1
								act_page2 = page_legs_step2
								
							else
								-- RTA DATA
								page_rte_legs2 = 1
								page_legs2 = 0
							end
						end
					end
				end
			end
		elseif page_hold2 == 1 then
			if rte12_sel_fo == 0 then
				if hold_offset_idx2 == offset and B738DR_hold_term == 0 and rte12_act == rte12_sel_fo then
					hold_exec = 1
					B738DR_hold_term = 1
				end
			else
				if hold_offset_idx8 == offset and B738DR_hold_term == 0 and rte12_act == rte12_sel_fo then
					hold_exec2 = 1
					B738DR_hold_term = 1
				end
			end
		elseif page_takeoff2 == 1 and B738DR_flight_phase == 0 then
			-- set QRH on / off
			if qrh == " ON" then
				qrh = "OFF"
			else
				qrh = " ON"
			end
			display_update = 1
		elseif page_n1_limit2 == 1 and disable_N1_6R == 0 then
			if in_flight_mode == 0 then
				-- go to Takeoff page
				page_n1_limit2 = 0
				page_takeoff2 = 1
				display_update = 1
			else
				-- select / deselect CLB-2
				if entry2 == ">DELETE" then
					clb = "<SEL>"
					clb_1 = "     "
					clb_2 = "     "
					entry2 = ""
				-- elseif clb_2 == "<SEL>" then
					-- clb = "<SEL>"
					-- clb_1 = "     "
					-- clb_2 = "     "
				else
					clb = "     "
					clb_1 = "     "
					clb_2 = "<SEL>"
				end
				sel_clb_thr = 1
				flight_phase_old2 = -1
			end
		elseif page_descent_forecast2 == 1 then
			if forecast_exec == 1 then
				-- ERASE
				trans_lvl_mod = trans_lvl
				forec_alt_1_mod = forec_alt_1
				forec_alt_1_num_mod = forec_alt_1_num
				forec_dir_1_mod = forec_dir_1
				forec_spd_1_mod = forec_spd_1
				forec_alt_2_mod = forec_alt_2
				forec_alt_2_num_mod = forec_alt_2_num
				forec_dir_2_mod = forec_dir_2
				forec_spd_2_mod = forec_spd_2
				forec_alt_3_mod = forec_alt_3
				forec_alt_3_num_mod = forec_alt_3_num
				forec_dir_3_mod = forec_dir_3
				forec_spd_3_mod = forec_spd_3
				cabin_rate_mod = cabin_rate
				forec_isa_dev_mod = forec_isa_dev
				forec_qnh_mod = forec_qnh
				tai_on_alt_mod = tai_on_alt
				tai_off_alt_mod = tai_off_alt
				tai_on_alt_num_mod = tai_on_alt_num
				tai_off_alt_num_mod = tai_off_alt_num
				forecast_exec = 0
				--page_descent_forecast2 = 0
				--page_descent2 = 1
			end
		elseif page_perf2 == 1 then
			-- go to N1 limit page
			page_perf2 = 0
			page_n1_limit2 = 1
			display_update = 1
		elseif page_perf2 == 2 then
			-- go to RTA progress page
			-- page_perf2 = 0
			-- page_rta2 = 1
		elseif page_pos_init2 == 1 then
			-- go to Route page
			page_pos_init2 = 0
			page_rte_init2 = 1
			if rte12_act == 0 then
				if entry2 == "" and ref_icao2 ~= "----" and ref_icao_x == "----" then
					entry2 = ref_icao2
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			elseif rte12_act == 1 then
				if entry2 == "" and ref_icao8 ~= "----" and ref_icao7_x == "----" then
					entry2 = ref_icao8
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			elseif rte12_sel == 0 then
				if entry2 == "" and ref_icao2 ~= "----" and ref_icao_x == "----" then
					entry2 = ref_icao2
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			else
				if entry2 == "" and ref_icao8 ~= "----" and ref_icao7_x == "----" then
					entry2 = ref_icao8
					add_fmc_msg(REF_ICAO_MSG, 1)
					msg_ref_icao_alert = 1
				end
			end
		elseif page_ident2 == 1 or page_ident2 == 2 then
			-- go to Pos init page
			page_ident2 = 0
			page_pos_init2 = 1
			display_update = 1
		elseif page_cruise2 == 1 then
			-- ERASE change cruise alt and spd
			if crz_exec ~= 0 then
				crz_alt = crz_alt_old
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
			end
			if crz_spd_exec ~= 0 then
				crz_spd_exec = 0
				fmc_cruise_speed_mod = 0
				fmc_cruise_speed_mach_mod = 0
				fmc_cruise_mode_mod = B738DR_cruise_mode
			end
		elseif page_descent2 == 1 then
			if descent_exec == 0 and des_spd_exec == 0 then
				local delta_alt_crz = B738DR_fmc_cruise_alt - simDR_ap_altitude_dial_ft
				if B738DR_fms_descent_now < 2 and delta_alt_crz > 1000 and des_now_enable == 1 then
					if B738DR_fms_descent_now == 0 then
						B738DR_fms_descent_now = 1
					else
						B738DR_fms_descent_now = 0
					end
				end
			else
				-- ERASE
				if descent_exec ~= 0 then
					if des_del_spd_idx2_fo ~= 0 then
						des_del_spd_idx2_fo = 0
					else
						descent_r_alt1_mod = B738DR_fmc_descent_r_alt1
						descent_r_speed1_mod = B738DR_fmc_descent_r_speed1
						fmc_descent_r_alt1_mod = fmc_descent_r_alt1
					end
					descent_exec = 0
				end
				if des_spd_exec ~= 0 then
					des_spd_exec = 0
					fmc_descent_speed_mod = 0
					fmc_descent_speed_mach_mod = 0
					fmc_descent_mode_mod = B738DR_descent_mode
				end
			end
			display_update = 1
			
		elseif page_climb2 == 1 then
			if climb_exec == 1 then
				-- ERASE
				if clb_del_spd_idx2_fo ~= 0 then
					clb_del_spd_idx2_fo = 0
				else
					climb_r_alt1_mod = B738DR_fmc_climb_r_alt1
					climb_r_speed1_mod = B738DR_fmc_climb_r_speed1
					fmc_climb_r_alt1_mod = fmc_climb_r_alt1
				end
				climb_exec = 0
			end
			if clb_spd_exec ~= 0 then
				clb_spd_exec = 0
				fmc_climb_speed_mod = 0
				fmc_climb_speed_mach_mod = 0
				fmc_climb_mode_mod = B738DR_climb_mode
			end
			if crz_exec ~= 0 then
				crz_alt = crz_alt_old
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
			end
			display_update = 1
		elseif page_rte_legs2 > 0 then
			-- go to LEGS
			page_rte_legs2 = 0
			page_legs2 = 1
		elseif page_ref_nav_data2 == 1 then
			local strlen = string.len(entry2)
			if ref_nav_wpt == "-----" and ref_nav_navaid == "----" and ref_nav_apt == "----" then
				if ref_nav_new == "*****" then
					if strlen == 0 or strlen > 5 then
						add_fmc_msg(INVALID_INPUT, 1)
					else
						ref_nav_new = entry2
						nav_data_idx2 = 0
						entry2 = ""
					end
				else
					-- go to NAV OPTIONS
					page_ref_nav_data2 = 0
					page_ref_nav_data_wpt2 = 1
					ref_nav_lat = 0
					ref_nav_lon = 0
					ref_nav_mag_var = ""
					ref_nav_id = "-----"
					ref_nav_rc = ""
					ref_nav_brg = "---"
					ref_nav_dist = "---"
					entry2 = ""
				end
			else
				-- go to NAV OPTIONS
				page_ref_nav_data2 = 0
				if ref_nav_wpt ~= "-----" then
					page_ref_nav_data_wpt2 = 1
				elseif ref_nav_navaid ~= "----" then
					page_ref_nav_data_navaid2 = 1
				elseif ref_nav_apt ~= "----" then
					page_ref_nav_data_apt2 = 1
				end
				ref_nav_lat = navaid_list[nav_data_idx][2]
				ref_nav_lon = navaid_list[nav_data_idx][3]
				if navaid_list[nav_data_idx][20] == ">" then
					ref_nav_mag_var = string.format("%3d",(mag_variation_deg(navaid_list[nav_data_idx][2], navaid_list[nav_data_idx][3])))
				else
					if navaid_list[nav_data_idx][20] == "x" then
						ref_nav_mag_var = ">" .. string.format("%3d",(mag_variation_deg(navaid_list[nav_data_idx][2], navaid_list[nav_data_idx][3])))
					else
						ref_nav_mag_var = navaid_list[nav_data_idx][20]
					end
				end
				if navaid_list[nav_data_idx][21] == "" then
					ref_nav_id = "-----"
				else
					ref_nav_id = navaid_list[nav_data_idx][21]
				end
				if navaid_list[nav_data_idx][22] == "" then
					ref_nav_brg = "---"
				else
					ref_nav_brg = navaid_list[nav_data_idx][22]
				end
				if navaid_list[nav_data_idx][23] == "" then
					ref_nav_dist = "---"
				else
					ref_nav_dist = navaid_list[nav_data_idx][23]
				end
				nav_data_idx2 = navaid_list[nav_data_idx][24]
				
				ref_nav_freq = navaid_list[nav_data_idx][6]
				local ref_len = string.len(ref_nav_freq)
				if navaid_list[nav_data_idx][1] > 10 then
					ref_nav_freq = navaid_list[nav_data_idx][6] .. " "
				elseif ref_len == 3 then
					ref_nav_freq = navaid_list[nav_data_idx][6] .. "   "
				elseif ref_len == 4 then
					ref_nav_freq = string.sub(navaid_list[nav_data_idx][6], 1, 3) .. "." .. string.sub(navaid_list[nav_data_idx][6], -1, -1) .. " "
				elseif ref_len == 5 then
					ref_nav_freq = string.sub(navaid_list[nav_data_idx][6], 1, 3) .. "." .. string.sub(navaid_list[nav_data_idx][6], -2, -1)
				else
					ref_nav_freq = "      "
				end
				
				ref_elevation = 0
				
				ref_nav_rnw = "-----"
				ref_nav_rnw_length = 0
				
				entry2 = ""
			end
			ref_nav_exec = 0
		elseif page_ac_atis2 == 1 then
			if atis_msg_status ~= 0 and acars_atis_status == 0 and receive_msg == 2 then
				page_ac_atis2 = 0
				page_ac_msg2 = 1
				act_page2 = 1
			end
		elseif page_takeoff2 == 2 and B738DR_flight_phase == 0 then
			if B738DR_thr_cutback == 0 then
				B738DR_thr_cutback = 1
				clb_alt = "----"
				clb_alt_num = B738DR_def_clb_alt2
			else
				B738DR_thr_cutback = 0
				clb_alt = "----"
				clb_alt_num = B738DR_def_clb_alt
			end
			accel_alt = "----"
			accel_alt_num = B738DR_def_accel_alt
		elseif page_altern_apt2 == 1 then
			create_near_apt_list()
			page_nearest_apt2 = 1
			page_altern_apt2 = 0
			act_page2 = 1
		elseif page_nearest_apt2 == 1 then
			if act_page2 == 1 then
				page_nearest_apt2 = 0
				page_altern_apt2 = 1
			else
				act_page2 = 1
			end
		elseif page_approach2 == 1 then
			page_altern_apt2 = 1
			page_approach2 = 0
			act_page2 = 1
		elseif page_progress2 == 1 then
			page_nav_status2 = 1
			page_progress2 = 0
			act_page2 = 1
		elseif page_init2 == 1 then
			page_nav_status2 = 1
			page_init2 = 0
			act_page2 = 1
		elseif page_dl_requests2 == 1 and atis_msg_status ~= 0 and receive_msg == 2 then
			page_dl_requests2 = 0
			page_dl_wxr_info2 = 1
			act_page2 = 1
			display_update = 1
		end
		
		--end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end










-----------------------------------------------

function B738_fmc2_init_ref_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		local irs_align_ok = 0
		if B738DR_irs_left_mode > 1 or B738DR_irs_right_mode > 1 then
			irs_align_ok = 1
		end
		local perf_init_complete = 1
		if gw == "***.*" or cost_index == "***" or reserves == "**.*" or crz_alt == "*****" then
			perf_init_complete = 0
		end
		if perf_exec < 2 then
			perf_init_complete = 0
		end
		
		local ref_des_icao = 1
		if ref_icao == "----" or des_icao == "****" then
			ref_des_icao = 0
		end
		
		local n1_oat = 1
		if oat == "****" and B738DR_aspirated_probe == 0 then
			n1_oat = 0
		end
		
		reset_fmc_pages_fo()
		
		if B738DR_init_ref_smart ~= 0 then
			if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				if irs_align_ok == 0 then
					page_pos_init2 = 1
				else
					page_perf2 = 1
				end
				-- page_perf2 = 1
			else
				-- in fligt -> Approach page
				if page_approach == 0 and page_approach2 == 0 then
					gw_app_nul = 0
					gw_app = gw
					gw_app_lbs = gw_lbs
					gw_app_kgs = gw_kgs
				end
				page_approach2 = 1
			end
		else
			-- if irs_align_ok == 0 then
				-- page_pos_init2 = 1
			-- -- elseif B738DR_flight_phase == 0 and (simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1) then
			-- elseif simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				-- -- if ref_des_icao == 0 then
					-- -- page_rte_init2 = 1
				-- -- elseif perf_init_complete == 0 then
					-- page_perf2 = 1
				-- -- elseif n1_oat == 0 then
					-- -- page_n1_limit2 = 1
				-- -- else
					-- -- page_takeoff2 = 1
				-- -- end
			-- else	--if was_on_air == 1 then
				-- -- in fligt -> Approach page
				-- if page_approach == 0 and page_approach2 == 0 then
					-- gw_app_nul = 0
					-- gw_app = gw
					-- gw_app_lbs = gw_lbs
					-- gw_app_kgs = gw_kgs
				-- end
				-- page_approach2 = 1
			if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
				if irs_align_ok == 0 then
					page_pos_init2 = 1
				else
					page_perf2 = 1
				end
			else
				-- in fligt -> Approach page
				if page_approach == 0 and page_approach2 == 0 then
					gw_app_nul = 0
					gw_app = gw
					gw_app_lbs = gw_lbs
					gw_app_kgs = gw_kgs
				end
				page_approach2 = 1
			end
		end
		
		fmc1_input_lag = 0
		
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_menu_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
			
		reset_fmc_pages_fo()
		if B738DR_fmc_type == 0 then
			page_menu2 = 1
		else
			-- FIX
			page_fix2 = 1
			act_page2 = 1
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_n1_lim_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages_fo()
		if B738DR_fmc_type == 0 then
			page_n1_limit2 = 1
		else
			-- MENU
			page_menu2 = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_rte_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages_fo()
		page_rte_init2 = 1
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			act_page2 = 1
		else
			act_page2 = 2
			local max_page_fpln = 0
			local jj = 0
			local kk = 0
			-- Flight plan
			if ref_icao_x == "----" or des_icao_x == "****" then
				max_page_fpln = 0
			else
				jj = math.floor(fpln_num2 / 5)
				kk = fpln_num2 % 5
				max_page_fpln = jj + 2
			end
			if act_page2 > max_page_fpln then
				act_page2 = max_page_fpln
				if act_page2 == 0 then
					act_page2 = 1
				end
			end
		end
		
		if entry == "" and ref_icao2 ~= "----" and ref_icao_x == "----" then
			entry = ref_icao2
			add_fmc_msg(REF_ICAO_MSG, 1)
			msg_ref_icao_alert = 1
		end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc2_legs_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
			reset_fmc_pages_fo()
			page_legs2 = 1
			if rte12_sel_fo == 0 then
				new_hold = 0
			else
				new_hold2 = 0
			end
			
			if B738DR_fo_map_mode == 3 then
				page_legs_step2 = 1
				act_page2 = page_legs_step2
				if rte12_sel_fo == 0 then
					legs_step2 = offset_mod
					if legs_data2[legs_step2][1] == "DISCONTINUITY" then
						legs_step2 = legs_step2 + 1
						if legs_step2 > legs_num2 or legs_step2 < offset_mod then
							legs_step2 = offset_mod
						end
						if legs_step2 > legs_num2 then
							legs_step2 = legs_num2
						end
					end
				else
					legs_step2 = offset_mod2
					if legs_data2[legs_step2][1] == "DISCONTINUITY" then
						legs_step2 = legs_step2 + 1
						if legs_step2 > legs_num8 or legs_step2 < offset_mod2 then
							legs_step2 = offset_mod2
						end
						if legs_step2 > legs_num8 then
							legs_step2 = legs_num8
						end
					end
				end
			else
				act_page2 = 1
			end
		
		--end
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_fix_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
			
		reset_fmc_pages_fo()
		if B738DR_fmc_type == 0 then
			page_fix2 = 1
			act_page2 = 1
		else
			-- N1 Limit
			page_n1_limit2 = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_clb_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages_fo()
		if B738DR_fmc_type == 0 then
			page_climb2 = 1
			if engout_clb < 2 then
				engout_clb = 0
			end
		else
			-- DEP ARR
			page_dep_arr2 = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_crz_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages_fo()
		if B738DR_fmc_type == 0 then
			page_cruise2 = 1
			if engout_crz < 2 then
				engout_crz = 0
			end
		else
			page_atc2 = 1
			act_page2 = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_des_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		
		B738DR_fms_key = 1
		
		reset_fmc_pages_fo()
		if B738DR_fmc_type == 0 then
			page_descent2 = 1
		else
			if B738DR_flight_phase < 2 then
				-- CLB
				page_climb2 = 1
			elseif B738DR_flight_phase < 5 then
				-- CRZ
				page_cruise2 = 1
			elseif B738DR_flight_phase ~= 8 then
				-- DES
				page_descent2 = 1
			else
				-- CLB
				page_climb2 = 1
			end
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

function B738_fmc2_dep_app_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		local ww = 0
		B738DR_fms_key = 1
		
		if B738DR_fmc_type == 0 then
			reset_fmc_pages_fo()
			page_dep_arr2 = 1
		else
			if B738DR_fmc_exec_lights_fo == 0 then
			
				reset_fmc_pages_fo()
				
				if rte12_sel_fo == 0 then
					hold_offset = 0
					if hold_data_num2 > 0 then
						for ww = 1, hold_data_num2 do
							if hold_data2[ww] >= offset_mod then
								hold_offset = ww
								break
							end
						end
					end
					
					if hold_offset == 0 then
						page_hold2 = 0
						page_legs2 = 1
						act_page2 = 1
						new_hold = 1
						new_hold_inbd = "---"
						new_hold_turn = -1
						new_hold_time = "-.-"
						new_hold_dist = "--.-"
					else
						page_hold2 = 1
						page_legs2 = 0
						act_page2 = 1
					end
				else
					hold_offset2 = 0
					if hold_data_num4 > 0 then
						for ww = 1, hold_data_num4 do
							if hold_data4[ww] >= offset_mod2 then
								hold_offset2 = ww
								break
							end
						end
					end
					
					if hold_offset2 == 0 then
						page_hold2 = 0
						page_legs2 = 1
						act_page2 = 1
						new_hold2 = 1
						new_hold_inbd2 = "---"
						new_hold_turn2 = -1
						new_hold_time2 = "-.-"
						new_hold_dist2 = "--.-"
					else
						page_hold2 = 1
						page_legs2 = 0
						act_page2 = 1
					end
				end
			end
		end
			
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_hold_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		local ww = 0
		
		B738DR_fms_key = 1
		
		if B738DR_fmc_type == 0 then
			if B738DR_fmc_exec_lights_fo == 0 then
			
				reset_fmc_pages_fo()
				
				if rte12_sel_fo == 0 then
					hold_offset = 0
					if hold_data_num2 > 0 then
						for ww = 1, hold_data_num2 do
							if hold_data2[ww] >= offset_mod then
								hold_offset = ww
								break
							end
						end
					end
					
					if hold_offset == 0 then
						page_hold2 = 0
						page_legs2 = 1
						act_page2 = 1
						new_hold = 1
						new_hold_inbd = "---"
						new_hold_turn = -1
						new_hold_time = "-.-"
						new_hold_dist = "--.-"
					else
						page_hold2 = 1
						page_legs2 = 0
						act_page2 = 1
					end
				else
					hold_offset2 = 0
					if hold_data_num4 > 0 then
						for ww = 1, hold_data_num4 do
							if hold_data4[ww] >= offset_mod2 then
								hold_offset2 = ww
								break
							end
						end
					end
					
					if hold_offset2 == 0 then
						page_hold2 = 0
						page_legs2 = 1
						act_page2 = 1
						new_hold2 = 1
						new_hold_inbd2 = "---"
						new_hold_turn2 = -1
						new_hold_time2 = "-.-"
						new_hold_dist2 = "--.-"
					else
						page_hold2 = 1
						page_legs2 = 0
						act_page2 = 1
					end
				end
			end
		else
			reset_fmc_pages_fo()
			page_fmc_comm2 = 1
		end
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
function B738_fmc2_prog_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 then
		B738DR_fms_key = 1
		

			reset_fmc_pages_fo()
			page_progress2 = 1
		
		fmc2_input_lag = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end

-- EXECSK2
function B738_fmc2_exec_CMDhandler(phase, duration)
	if phase == 0 and fmc2_input_lag == 1 and calc_rte_enable2 == 0 and calc_rte_enable8 == 0 then
		B738DR_fms_key = 1
		local qq = 0
		local xy = 0
		local apt_ok = 0
		local exec_enable = 0
		local rte12_changed = 0
	
		if B738DR_fmc_exec_lights_fo == 1 then
			update_rnw_list(rte12_sel_fo)
			if page_descent_forecast2 == 1 then
				if forecast_exec == 1 then
					forecast_exec = 0
					trans_lvl = trans_lvl_mod
					forec_alt_1 = forec_alt_1_mod
					forec_alt_1_num = forec_alt_1_num_mod
					forec_dir_1 = forec_dir_1_mod
					forec_spd_1 = forec_spd_1_mod
					forec_alt_2 = forec_alt_2_mod
					forec_alt_2_num = forec_alt_2_num_mod
					forec_dir_2 = forec_dir_2_mod
					forec_spd_2 = forec_spd_2_mod
					forec_alt_3 = forec_alt_3_mod
					forec_alt_3_num = forec_alt_3_num_mod
					forec_dir_3 = forec_dir_3_mod
					forec_spd_3 = forec_spd_3_mod
					cabin_rate = cabin_rate_mod
					forec_isa_dev = forec_isa_dev_mod
					forec_qnh = forec_qnh_mod
					tai_on_alt = tai_on_alt_mod
					tai_off_alt = tai_off_alt_mod
					tai_on_alt_num = tai_on_alt_num_mod
					tai_off_alt_num = tai_off_alt_num_mod
					if cabin_rate == "---" then
						simDR_press_set_vvi = 500
					else
						simDR_press_set_vvi = cabin_rate
					end
				end
			end
			
			-- if page_cruise2 == 1 or page_climb2 == 1 or page_perf2 == 1 then
			if page_perf2 == 1 or page_legs2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_init2 > 0 or page_rte_legs2 > 0 
			or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 then
			
				if B738DR_flight_phase >= 2 and B738DR_flight_phase < 8 then
					if crz_exec ~= 0 then
						
						local altitude_actual_1 = simDR_altitude_pilot + 200
						local altitude_actual_2 = simDR_altitude_pilot - 200
						if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
							altitude_actual_1 = B738DR_preselected_alt
							altitude_actual_2 = altitude_actual_1
						end
						
						if crz_alt_num2 > altitude_actual_1 then
							crz_exec = 1	-- CRZ CLB
							if B738DR_mcp_alt_dial > crz_alt_num then
								B738DR_flight_phase = 3
							end
						elseif crz_alt_num2 < altitude_actual_2 then
							crz_exec = 2	-- CRZ DES
							if B738DR_mcp_alt_dial < crz_alt_num then
								B738DR_flight_phase = 4
							end
						else	-- crz_exec == 9
							crz_exec = 3	-- CRZ
						end
						altitude_last = elevation_ft
						B738DR_fms_descent_now = 0
						vnav_update = 1
						crz_alt_changed = 1
						cruise_alt_changed = 1
						cruise_alt_changed2 = 1
						if crz_alt_num2 > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
							add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
						end
					end
					
					-- CRZ CLB
					if crz_exec == 1 then
						
						-- engage CRZ CLB
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						simDR_ap_altitude_dial_ft = crz_alt_num
						if B738DR_mcp_alt_dial >= crz_alt_num then
							if B738DR_altitude_mode == 5 then
								if simDR_autopilot_altitude_mode ~= 5 then
									simCMD_autopilot_lvl_chg:once()
								end
							end
							B738DR_flight_phase = 3
							B738DR_fms_descent_now = 0
							vnav_alt_mode = 0
						else
							add_fmc_msg(CHECK_ALT_TGT, 1)
							B738DR_fmc_message_warn = 1
							B738DR_flight_phase = 1
							missed_app_alt_change = 0
							if B738DR_vnav_alt_disable ~= 0 or B738DR_vnav_alt_disable ~= 0 then
								-- ALT HLD
								B738DR_activate_pitch_mode = 3
							else
								-- VNAV ALT
								vnav_alt_mode = 1
							end
						end
					
					-- CRZ DES
					elseif crz_exec == 2 then
						
						-- engage CRZ DES
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						simDR_ap_altitude_dial_ft = crz_alt_num
						if B738DR_mcp_alt_dial <= crz_alt_num then
							if B738DR_altitude_mode == 5 then
								if simDR_autopilot_altitude_mode ~= 4 then
									simDR_ap_vvi_dial = -1000
									B738DR_ap_vvi_dial = -1000
									simCMD_autopilot_vs_sel:once()
								end
							end
							B738DR_flight_phase = 4
							B738DR_fms_descent_now = 0
							vnav_alt_mode = 0
						else
							add_fmc_msg(CHECK_ALT_TGT, 1)
							vnav_alt_mode = 1
						end
					elseif crz_exec == 3 then
						-- engage CRZ
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						simDR_ap_altitude_dial_ft = crz_alt_num
						B738DR_flight_phase = 2
						B738DR_fms_descent_now = 0
						vnav_alt_mode = 0
						if B738DR_altitude_mode == 5 then
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								simCMD_autopilot_alt_hold:once()
							end
						end
					end
					if page_perf2 == 1 and crz_exec ~= 0 then
						B738_calc_vnav_spd()
					end
				
				else
					-- climb mode
					if crz_exec ~= 0 then
						crz_alt_num = crz_alt_num2
						B738DR_fmc_cruise_alt = crz_alt_num
						msg_unavaible_crz_alt = 0
						msg_chk_alt_constr = 0
						vnav_update = 1
						if page_perf == 1 then
							B738_calc_vnav_spd()
						end
						crz_alt_changed = 1
						altitude_last = elevation_ft
						B738DR_fms_descent_now = 0
						
						if crz_alt_num > B738DR_cruise_max_alt and B738DR_cruise_max_alt ~= 0 then
							add_fmc_msg(MAX_ALT_FLXXX .. string.format("%03d", B738DR_cruise_max_alt/100), 1)
						end
						
						local altitude_actual_1 = simDR_altitude_pilot + 200
						local altitude_actual_2 = simDR_altitude_pilot - 200
						if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
							altitude_actual_1 = B738DR_preselected_alt
							altitude_actual_2 = altitude_actual_1
						end
						
						if crz_alt_num > altitude_actual_1 then
							if B738DR_mcp_alt_dial < altitude_actual_1 then
							-- if B738DR_mcp_alt_dial < crz_alt_num then
								if (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
									add_fmc_msg(CHECK_ALT_TGT, 1)
									vnav_alt_mode = 1
								end
							end
						elseif crz_alt_num < altitude_actual_2 then
							B738DR_flight_phase = 2
							-- if B738DR_mcp_alt_dial > crz_alt_num then
							if B738DR_mcp_alt_dial > altitude_actual_2 then
								if (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
									add_fmc_msg(CHECK_ALT_TGT, 1)
									vnav_alt_mode = 1
								else
									-- engage CRZ DES
									crz_alt_num = crz_alt_num2
									B738DR_fmc_cruise_alt = crz_alt_num
									simDR_ap_altitude_dial_ft = crz_alt_num
									if B738DR_mcp_alt_dial <= crz_alt_num then
										if B738DR_altitude_mode == 5 then
											if simDR_autopilot_altitude_mode ~= 4 then
												simDR_ap_vvi_dial = -1000
												B738DR_ap_vvi_dial = -1000
												simCMD_autopilot_vs_sel:once()
											end
										end
										B738DR_flight_phase = 4
										B738DR_fms_descent_now = 0
										vnav_alt_mode = 0
									else
										add_fmc_msg(CHECK_ALT_TGT, 1)
										vnav_alt_mode = 1
									end
								end
							end
						else
							B738DR_flight_phase = 2
						end
					end
				end
				crz_exec = 0
				exec1_light = 0
				crz_alt_num2 = 0
				crz_alt_old = "     "
				
				-- if crz_spd_exec == 1 and page_cruise2 == 1 then
				if crz_spd_exec == 1 then
					if B738DR_flight_phase == 3 then
						--CRZ CLB
						if fmc_climb_speed_mach_mod ~= 0 then
							B738DR_fmc_climb_speed_mach = fmc_climb_speed_mach_mod
							B738DR_climb_mode = 3
							fmc_climb_mode_mod = B738DR_climb_mode
						end
						if fmc_climb_speed_mod ~= 0 then
							B738DR_fmc_climb_speed = fmc_climb_speed_mod
							B738DR_climb_mode = 3
							fmc_climb_mode_mod = B738DR_climb_mode
						end
						fmc_climb_speed_mod = 0
						fmc_climb_speed_mach_mod = 0
						crz_spd_exec = 0
					elseif B738DR_flight_phase == 4 then
						--CRZ DES
						if fmc_descent_speed_mach_mod ~= 0 then
							B738DR_fmc_descent_speed_mach = fmc_descent_speed_mach_mod
						end
						if fmc_descent_speed_mod ~= 0 then
							B738DR_fmc_descent_speed = fmc_descent_speed_mod
						end
						fmc_descent_speed_mod = 0
						fmc_descent_speed_mach_mod = 0
						crz_spd_exec = 0
					else
						if B738DR_cruise_mode ~= fmc_cruise_mode_mod and fmc_cruise_mode_mod == 0 then
							B738DR_descent_mode = 0
							B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
							B738DR_fmc_descent_speed = econ_des_spd				-- temporary
							change_desc_to_path = 1
							fmc_descent_mode_mod = 0
						end
						
						B738DR_cruise_mode = fmc_cruise_mode_mod
						B738DR_fmc_cruise_speed = fmc_cruise_speed_mod
						B738DR_fmc_cruise_speed_mach = fmc_cruise_speed_mach_mod
						fmc_cruise_speed_mod = 0
						fmc_cruise_speed_mach_mod = 0
						crz_spd_exec = 0
						
						if B738DR_fmc_descent_speed > B738DR_fmc_cruise_speed and B738DR_fmc_cruise_speed ~= 0 then
							B738DR_fmc_descent_speed = B738DR_fmc_cruise_speed
						elseif B738DR_fmc_descent_speed < B738DR_fmc_cruise_speed - 20 then
							B738DR_fmc_descent_speed = B738DR_fmc_cruise_speed - 20
						end
						fmc_descent_speed_mod = 0
						
						if B738DR_fmc_descent_speed_mach > B738DR_fmc_cruise_speed_mach and B738DR_fmc_cruise_speed_mach ~= 0 then
							B738DR_fmc_descent_speed_mach = B738DR_fmc_cruise_speed_mach
						elseif B738DR_fmc_descent_speed_mach < B738DR_fmc_cruise_speed_mach - 0.04 then
							B738DR_fmc_descent_speed_mach = B738DR_fmc_cruise_speed_mach - 0.04
						end
						fmc_descent_speed_mach_mod = 0
						
						B738DR_fmc_descent_r_alt1 = descent_r_alt1_mod
						B738DR_fmc_descent_r_speed1 = descent_r_speed1_mod
						fmc_descent_r_alt1 = fmc_descent_r_alt1_mod
						
						B738DR_fmc_climb_r_alt1 = climb_r_alt1_mod
						B738DR_fmc_climb_r_speed1 = climb_r_speed1_mod
						fmc_climb_r_alt1 = fmc_climb_r_alt1_mod
					end
				end
			-- end
			
			-- if page_descent2 == 1 then
				if descent_exec == 1 then
					if des_del_spd_idx2_fo ~= 0 then
						if des_del_spd_idx2 < legs_num then
							legs_data[des_del_spd_idx2][4] = 0
							legs_data[des_del_spd_idx2][42] = 0
							des_del_spd_idx2_fo = 0
						end
					else
						B738DR_fmc_descent_r_alt1 = descent_r_alt1_mod
						B738DR_fmc_descent_r_speed1 = descent_r_speed1_mod
						fmc_descent_r_alt1 = fmc_descent_r_alt1_mod
					end
					descent_exec = 0
					vnav_update = 1
					msg_chk_alt_constr = 0
				end
				
				if B738DR_fms_descent_now == 1 then
					B738DR_fms_descent_now = 2
					if B738DR_altitude_mode == 5 then
						if simDR_autopilot_altitude_mode ~= 4 then
							simDR_ap_vvi_dial = 0	---1000
							B738DR_ap_vvi_dial = 0
							simCMD_autopilot_vs_sel:once()
						end
					end
					B738DR_flight_phase = 5
					switch_fmc_page(3)
				end
				
				if des_spd_exec == 1 then
					if fmc_descent_speed_mach_mod ~= 0 then
						B738DR_fmc_descent_speed_mach = fmc_descent_speed_mach_mod
						-- B738DR_descent_mode = 2
					end
					if fmc_descent_speed_mod ~= 0 then
						B738DR_fmc_descent_speed = fmc_descent_speed_mod
						-- B738DR_descent_mode = 2
					end
					B738DR_descent_mode = fmc_descent_mode_mod
					if B738DR_descent_mode == 0 then
						B738DR_descent_mode = 2
						change_desc_to_path = 1
					elseif B738DR_descent_mode == 1 then
						B738DR_descent_mode = 3
						change_desc_to_path = 0
					end
					fmc_descent_speed_mod = 0
					fmc_descent_speed_mach_mod = 0
					des_spd_exec = 0
				end
			-- end
			
			-- if page_climb2 == 1 then
				if climb_exec == 1 then
					if clb_del_spd_idx2_fo ~= 0 then
						if clb_del_spd_idx2 < legs_num then
							legs_data[clb_del_spd_idx2][4] = 0
							legs_data[clb_del_spd_idx2][42] = 0
							clb_del_spd_idx2_fo = 0
						end
					else
						B738DR_fmc_climb_r_alt1 = climb_r_alt1_mod
						B738DR_fmc_climb_r_speed1 = climb_r_speed1_mod
						fmc_climb_r_alt1 = fmc_climb_r_alt1_mod
					end
					climb_exec = 0
				end
				
				if clb_spd_exec == 1 then
					if fmc_climb_speed_mach_mod ~= 0 then
						B738DR_fmc_climb_speed_mach = fmc_climb_speed_mach_mod
						B738DR_climb_mode = fmc_climb_mode_mod
					end
					if fmc_climb_speed_mod ~= 0 then
						B738DR_fmc_climb_speed = fmc_climb_speed_mod
						B738DR_climb_mode = fmc_climb_mode_mod
					end
					fmc_climb_speed_mod = 0
					fmc_climb_speed_mach_mod = 0
					clb_spd_exec = 0
				end
			-- end
			end
			
			if rte_add_sid_act == 0 and rte_add_star_act == 0 and rte_add_app_act == 0 then		--and mod_offset == 0 then
				exec_enable = 1
			end
			if exec_enable == 1 then
				-- if page_legs2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_init2 > 0 or page_rte_legs2 > 0 then
				if page_legs2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_init2 > 0 or page_rte_legs2 > 0 or (page_offset2 == 1 and offset_act ~= offset_act_mod and legs_num > 1) then
					
					if rte12_sel_fo == 0 then
						
						if rte12_act ~= 0 then
							rte12_act = 0
							rte12_changed = 1
						end
						
						hold_exec2 = 0
						ref_exec2 = 0
						des_exec2 = 0
						rte_exec2 = 0
						-- forecast_exec = 0
						
						if ref_exec ~= 0 or des_exec ~= 0 then
							if ref_exec ~= 0 then
								offset = 0
								offset_seq = 0
								
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									reset_dl_times()
									if ref_icao_x == "ORGN" then
										dl_ref = "----"
									else
										dl_ref = ref_icao_x
									end
									dl_flt_num = flt_num_x
									dl_day = simDR_time_day
								end
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
							end
							
							ref_exec = 0
							des_exec = 0
							if dl_ref ~= "----" then
								dl_des = des_icao_x
							end
							
							ref_icao = ref_icao_x
							ref_icao_lat = ref_icao_lat_x
							ref_icao_lon = ref_icao_lon_x
							ref_tns_alt = ref_tns_alt_x
							ref_tns_lvl = ref_tns_lvl_x
							ref_icao_alt = ref_icao_alt_x
							trans_alt = trans_alt_mod
							
							ref_gate = ref_gate_x
							co_route = co_route_x
							flt_num = flt_num_x
							ref_rwy = ref_rwy2
							ref_rwy3 = ref_rwy2
							
							ref_rwy = ref_rwy2
							ref_sid = ref_sid2
							ref_sid_tns = ref_sid_tns2
							
							des_app_from_apt = 0
							
							
							des_icao = des_icao_x
							des_rnw = des_rnw2
							des_icao_lat = des_icao_lat_x
							des_icao_lon = des_icao_lon_x
							des_tns_alt = des_tns_alt_x
							des_tns_lvl = des_tns_lvl_x
							des_icao_alt = des_icao_alt_x
							trans_lvl = trans_lvl_mod
							
							des_app = des_app2
							des_app_tns = des_app_tns2
							des_star = des_star2
							des_star_trans = des_star_trans2
							
							arr_data = arr_data_x
							des_app_rw_only = des_app_rw_only_x
							rw_ext_dist = rw_ext_dist_x
							rw_ext_fpa = rw_ext_fpa_x
							
							offset_act = 0
							offset_start = 0
							offset_end = 0
							offset_dist = 0
							offset_side = 0
							offset_act_mod = 0
							offset_start_mod = 0
							offset_end_mod = 0
							offset_dist_mod = 0
							offset_side_mod = 0
							offset_start_def = 0
							offset_end_def = 0
							offset_start_def_mod = 0
							offset_end_def_mod = 0
							if rte12_changed == 0 then
								crz_alt = "*****"
								crz_alt_num = 0
								crz_alt_num2 = 0
							end
							
							if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then	--descent, approach, goaround arm
								B738DR_flight_phase = 1
								missed_app_alt_change = 0
								B738DR_fms_descent_now = 0
								if B738DR_mcp_alt_dial <= simDR_altitude_pilot and B738DR_altitude_mode == 5 then
									vnav_alt_mode = 1
								end
							end
							altitude_last = elevation_ft
							
							reverse_rte1 = 0
						end
						
						if legs_delete == 1 or rte_exec == 1 then
							
							if rte12_sel_fo == 0 then
								rte12_copy_done = 0
							elseif rte12_sel_fo ~= 0 then
								rte21_copy_done = 0
							end
							
							if simDR_radio_height_pilot_ft < 50 and ref_rwy ~= ref_rwy2 then
								if true_legs_num(1) > 0 then
									offset = math.max(offset, 2)
									if offset_seq < offset or offset_seq > legs_num then
										offset_seq = offset
									end
								else
									offset = 0
									offset_seq = 0
								end
								last_offset = offset
							end
							
							reverse_rte1 = 0
							
							if rte_exec == 1 then
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									if ref_rwy ~= ref_rwy2 and ref_rwy ~= "-----" then
										if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
										end
									end
								end
								
								ref_icao = ref_icao_x
								ref_icao_lat = ref_icao_lat_x
								ref_icao_lon = ref_icao_lon_x
								ref_tns_alt = ref_tns_alt_x
								ref_tns_lvl = ref_tns_lvl_x
								ref_icao_alt = ref_icao_alt_x
								trans_alt = trans_alt_mod
								
								ref_gate = ref_gate_x
								co_route = co_route_x
								flt_num = flt_num_x
								ref_rwy = ref_rwy2
								ref_rwy3 = ref_rwy2
								
								ref_rwy = ref_rwy2
								ref_sid = ref_sid2
								ref_sid_tns = ref_sid_tns2
								
								des_app_from_apt = 0
								
								des_icao = des_icao_x
								des_rnw = des_rnw2
								des_icao_lat = des_icao_lat_x
								des_icao_lon = des_icao_lon_x
								des_tns_alt = des_tns_alt_x
								des_tns_lvl = des_tns_lvl_x
								des_icao_alt = des_icao_alt_x
								trans_lvl = trans_lvl_mod
								
								des_app = des_app2
								des_app_tns = des_app_tns2
								des_star = des_star2
								des_star_trans = des_star_trans2
								
								arr_data = arr_data_x
								des_app_rw_only = des_app_rw_only_x
								rw_ext_dist = rw_ext_dist_x
								rw_ext_fpa = rw_ext_fpa_x
								
								create_fpln(rte12_sel_fo)
								copy_to_legsdata(rte12_sel_fo)
								
								if rte12_changed ~= 0 then
									rte12_changed = 0
									
									offset = offset_mod
									-- offset = offset_mod2
									offset_seq = offset
									
									last_offset2  = offset
									offset_mod_x2 = offset_mod
									
									-- last_offset8  = offset
									-- offset_mod_x8 = offset_mod2
								end
								
								if exec_activate_fpln == 1 then
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										reset_dl_times()
										if ref_icao_x == "ORGN" then
											dl_ref = "----"
										else
											dl_ref = ref_icao_x
										end
										dl_flt_num = flt_num_x
										dl_day = simDR_time_day
										dl_des = des_icao_x
									end
								end
								
							else
								if legs_num > 1 then
									
									if legs_abeam_selected ~= 0 and B738DR_legs_abeam_avail ~= 0 and legs_abeam_data_n > 0 then
										-- rte_copy(offset)
										rte_copy(offset_mod, rte12_sel_fo)
										local abeam_wpt_added = 0
										local act_abeam_wpt = 0
										local delta_crs = 0
										for ab = 1, legs_abeam_data_n do
											if B738DR_legs_abeam_lat2[ab - 1] ~= 0 or B738DR_legs_abeam_lon2[ab - 1] ~= 0 then
												-- act_abeam_wpt = offset + abeam_wpt_added
												act_abeam_wpt = offset_mod + abeam_wpt_added
												-- add abeam wpt
												legs_data2[act_abeam_wpt] = {}
												legs_data2[act_abeam_wpt][1] = legs_abeam_data[ab][4]		--entry
												-- legs_data2[act_abeam_wpt][2] = 0		-- brg
												-- legs_data2[act_abeam_wpt][3] = 0		-- distance
												legs_data2[act_abeam_wpt][4] = 0		-- speed
												legs_data2[act_abeam_wpt][5] = 0		-- altitude
												legs_data2[act_abeam_wpt][6] = 0	-- altitude type
												legs_data2[act_abeam_wpt][7] = B738DR_legs_abeam_lat2[ab - 1]		-- latitude
												legs_data2[act_abeam_wpt][8] = B738DR_legs_abeam_lon2[ab - 1]		-- longitude
												legs_data2[act_abeam_wpt][9] = "DIRECT"			-- via id
												legs_data2[act_abeam_wpt][10] = 0		-- calc speed
												legs_data2[act_abeam_wpt][11] = 0		-- calc altitude
												legs_data2[act_abeam_wpt][12] = 0		-- calc altitude vnav pth
												legs_data2[act_abeam_wpt][13] = 0
												legs_data2[act_abeam_wpt][14] = 0		-- rest alt
												legs_data2[act_abeam_wpt][15] = 0		-- last fuel
												legs_data2[act_abeam_wpt][16] = ""		-- reg code
												legs_data2[act_abeam_wpt][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
												-- legs_data2[act_abeam_wpt][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
												legs_data2[act_abeam_wpt][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
												legs_data2[act_abeam_wpt][20] = 0
												legs_data2[act_abeam_wpt][21] = -1
												legs_data2[act_abeam_wpt][22] = ""
												legs_data2[act_abeam_wpt][23] = 0
												legs_data2[act_abeam_wpt][24] = 0
												legs_data2[act_abeam_wpt][25] = 0
												legs_data2[act_abeam_wpt][26] = 0
												legs_data2[act_abeam_wpt][27] = ""
												legs_data2[act_abeam_wpt][28] = ""
												legs_data2[act_abeam_wpt][29] = ""
												legs_data2[act_abeam_wpt][30] = ""
												-- legs_data2[act_abeam_wpt][31] = "TF"
												legs_data2[act_abeam_wpt][32] = 0
												legs_data2[act_abeam_wpt][33] = ""
												legs_data2[act_abeam_wpt][34] = ""
												legs_data2[act_abeam_wpt][35] = ""
												legs_data2[act_abeam_wpt][36] = 0
												-- legs_data2[act_abeam_wpt][37] = 0
												legs_data2[act_abeam_wpt][38] = ""
												if B738DR_legs_abeam_dist[ab - 1] < 100 then
													legs_data2[act_abeam_wpt][39] = legs_abeam_data[ab][5]		-- Wind data
												else
													legs_data2[act_abeam_wpt][39] = ""
												end
												legs_data2[act_abeam_wpt][40] = 0
												legs_data2[act_abeam_wpt][41] = 0
												legs_data2[act_abeam_wpt][42] = 0
												legs_data2[act_abeam_wpt][43] = ""
												legs_data2[act_abeam_wpt][44] = 0
												legs_data2[act_abeam_wpt][45] = 0
												legs_data2[act_abeam_wpt][46] = 0
												legs_data2[act_abeam_wpt][47] = 0
												legs_data2[act_abeam_wpt][48] = ""
												legs_data2[act_abeam_wpt][49] = 0
												legs_data2[act_abeam_wpt][50] = 0
												legs_data2[act_abeam_wpt][51] = 0
												legs_data2[act_abeam_wpt][52] = 0
												legs_data2[act_abeam_wpt][53] = 0
												legs_data2[act_abeam_wpt][54] = 0
												
												--abeamsk
												-- recalc_rte2(act_abeam_wpt)
												legs_data2[act_abeam_wpt][3] = nd_calc_dist2(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
												
												if ab == 1 then
													legs_data2[act_abeam_wpt][31] = "DF"
												else
													legs_data2[act_abeam_wpt][31] = "TF"
												end
												
												if legs_data2[act_abeam_wpt][3] == 0 then
													legs_data2[act_abeam_wpt][2] = legs_data2[act_abeam_wpt-1][2]
												else
													legs_data2[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
												end
												legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
												if legs_data2[act_abeam_wpt][18] < 0 then
													legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][18] + (2 * math.pi)
												end
												if string.sub(legs_data2[act_abeam_wpt][1], 1, 2) == "RW" then
													legs_data2[act_abeam_wpt][37] = legs_data2[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
												else
													delta_crs = (nd_calc_brg(legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8], legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8]) + 180) % 360
													legs_data2[act_abeam_wpt][37] = math.rad(delta_crs)
												end
												
												abeam_wpt_added = abeam_wpt_added + 1
											end
										end
										-- rte_paste(offset + abeam_wpt_added)
										rte_paste(offset_mod + abeam_wpt_added, rte12_sel_fo)
										
										if abeam_wpt_added ~= 0 then
											act_abeam_wpt = offset_mod + abeam_wpt_added
											
											legs_data2[act_abeam_wpt][3] = nd_calc_dist2(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
											
											legs_data2[act_abeam_wpt][31] = "TF"
											
											if legs_data2[act_abeam_wpt][3] == 0 then
												legs_data2[act_abeam_wpt][2] = legs_data2[act_abeam_wpt-1][2]
											else
												legs_data2[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8], legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8])
											end
											legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
											if legs_data2[act_abeam_wpt][18] < 0 then
												legs_data2[act_abeam_wpt][18] = legs_data2[act_abeam_wpt][18] + (2 * math.pi)
											end
											if string.sub(legs_data2[act_abeam_wpt][1], 1, 2) == "RW" then
												legs_data2[act_abeam_wpt][37] = legs_data2[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data2[act_abeam_wpt][7]), math.rad(legs_data2[act_abeam_wpt][8]))
											else
												delta_crs = (nd_calc_brg(legs_data2[act_abeam_wpt][7], legs_data2[act_abeam_wpt][8], legs_data2[act_abeam_wpt-1][7], legs_data2[act_abeam_wpt-1][8]) + 180) % 360
												legs_data2[act_abeam_wpt][37] = math.rad(delta_crs)
											end
										end
											
										-- calc_rte_enable2 = 1
									end
									
									copy_to_legsdata(rte12_sel_fo)
									
									--- new ---
									if ref_rwy ~= ref_rwy2 and ref_rwy ~= "-----" then
										if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
										end
									end
									ref_rwy = ref_rwy2
									ref_sid = ref_sid2
									ref_sid_tns = ref_sid_tns2
									des_rnw = des_rnw2
									des_app = des_app2
									des_app_tns = des_app_tns2
									des_star = des_star2
									des_star_trans = des_star_trans2
								end
								offset = offset_mod
								-- offset_seq = offset
							end
							-- legs_delete = 0
							rte_exec = 0
							if offset > legs_num then
								offset = legs_num
								offset_seq = offset
							end
							exec_activate_fpln = 0
							--dump_leg4()
							
							legs_abeam_data_n = 0
							legs_abeam_data = {}
							legs_abeam_selected = 0
							B738DR_legs_abeam_avail = 0
							B738DR_legs_abeam_num = 0
							
							
							-- legs_delete = 0
						end
						
						if (page_offset2 == 1 or legs_delete ~= 0) and offset_act ~= offset_act_mod and legs_num > 1 then
							-- execute Lateral Offset
							if offset_dist_mod == 0 then
								if offset_act >= 5 and offset_act < 8 then
									offset_act = 9
									offset_act_mod = 9
									B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
									B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
									B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
									B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
									B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
									B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
									B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
									B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
								elseif offset_act >= 8 then
									offset_act_mod = offset_act
								else
									offset_act = 0
									offset_start = 0
									offset_end = 0
									offset_dist = 0
									offset_side = 0
									-- legs_off_tmp_n = 0
									delete_offset_legs()
									delete_offset_legs2(rte12_act)
									offset_act_mod = 0
									offset_start_mod = 0
									offset_end_mod = 0
									offset_dist_mod = 0
									offset_side_mod = 0
									offset_start_def = 0
									offset_end_def = 0
									offset_start_def_mod = 0
									offset_end_def_mod = 0
								end
							else
								-- update_offset_idx2(rte12_act)
								offset_check(rte12_act)
								if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
									-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
										-- offset_act_mod = 5
									-- else
										-- offset_act_mod = 7
									-- end
									
									if rte12_act == 0 then
										if offset_start_mod == 0 then
											if legs_data2[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									else
										if offset_start_mod == 0 then
											if legs_data8[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									end
								end
								offset_act = offset_act_mod
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
								
								offset_start = offset_start_mod
								offset_end = offset_end_mod
								offset_dist = offset_dist_mod
								offset_side = offset_side_mod
								offset_start_def = offset_start_def_mod
								offset_end_def = offset_end_def_mod
								B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
								B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
								B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
								B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
								B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
								B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
								B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
								B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
							end
						end
						legs_delete = 0
						
						rte12_copy_done = 0
					else
						if rte12_act ~= 1 then
							rte12_act = 1
							rte12_changed = 1
						end
						
						hold_exec = 0
						ref_exec = 0
						des_exec = 0
						rte_exec = 0
						-- forecast_exec = 0
						
						if ref_exec2 ~= 0 or des_exec2 ~= 0 then
							if ref_exec2 ~= 0 then
								offset = 0
								offset_seq = 0
								nav_mode = 0
								
								if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
									reset_dl_times()
									if ref_icao7_x == "ORGN" then
										dl_ref = "----"
									else
										dl_ref = ref_icao7_x
									end
									dl_flt_num = flt_num_x
									dl_day = simDR_time_day
								end
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
							end
							
							ref_exec2 = 0
							des_exec2 = 0
							
							if dl_ref ~= "----" then
								dl_des = des_icao7_x
							end
							
							ref_icao7 = ref_icao7_x
							ref_icao_lat7 = ref_icao_lat7_x
							ref_icao_lon7 = ref_icao_lon7_x
							ref_tns_alt7 = ref_tns_alt7_x
							ref_tns_lvl7 = ref_tns_lvl7_x
							ref_icao_alt7 = ref_icao_alt7_x
							trans_alt7 = trans_alt_mod2
							
							ref_gate7 = ref_gate7_x
							co_route7 = co_route7_x
							flt_num = flt_num_x
							ref_rwy7 = ref_rwy8
							ref_rwy9 = ref_rwy8
							
							ref_sid7 = ref_sid8
							ref_sid_tns7 = ref_sid_tns8
							
							des_app_from_apt7 = 0
							
							
							des_icao7 = des_icao7_x
							des_rnw7 = des_rnw8
							des_icao_lat7 = des_icao_lat7_x
							des_icao_lon7 = des_icao_lon7_x
							des_tns_alt7 = des_tns_alt7_x
							des_tns_lvl7 = des_tns_lvl7_x
							des_icao_alt7 = des_icao_alt7_x
							trans_lvl7 = trans_lvl_mod2
							
							des_app7 = des_app8
							des_app_tns7 = des_app_tns8
							des_star7 = des_star8
							des_star_trans7 = des_star_trans8
							
							arr_data7 = arr_data7_x
							des_app_rw_only7 = des_app_rw_only7_x
							rw_ext_dist7 = rw_ext_dist7_x
							rw_ext_fpa7 = rw_ext_fpa7_x
							
							offset_act = 0
							offset_start = 0
							offset_end = 0
							offset_dist = 0
							offset_side = 0
							offset_act_mod = 0
							offset_start_mod = 0
							offset_end_mod = 0
							offset_dist_mod = 0
							offset_side_mod = 0
							offset_start_def = 0
							offset_end_def = 0
							offset_start_def_mod = 0
							offset_end_def_mod = 0
							if rte12_changed == 0 then
								crz_alt = "*****"
								crz_alt_num = 0
								crz_alt_num2 = 0
							end
							
							if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
								B738DR_flight_phase = 1
								missed_app_alt_change = 0
								B738DR_fms_descent_now = 0
								if B738DR_mcp_alt_dial <= simDR_altitude_pilot and B738DR_altitude_mode == 5 then
									vnav_alt_mode = 1
								end
							end
							altitude_last = elevation_ft
							
							reverse_rte2 = 0
						end
						
						if legs_delete == 1 or rte_exec2 == 1 then
							
							
							if simDR_radio_height_pilot_ft < 50 and ref_rwy7 ~= ref_rwy8 then
								if true_legs_num(1) > 0 then
									offset = math.max(offset, 2)
									if offset_seq < offset or offset_seq > legs_num then
										offset_seq = offset
									end
								else
									offset = 0
									offset_seq = 0
								end
								nav_mode = 0
								last_offset = offset
							end
							
							reverse_rte2 = 0
							
							if rte_exec2 == 1 then
								if ref_rwy7 ~= ref_rwy8 and ref_rwy7 ~= "-----" then
									if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
										v1_set = "---"
										vr_set = "---"
										v2_set = "---"
										verify_to_spd = 0
										add_fmc_msg(TO_SPEEDS_DELETED, 1)
									end
								end
								
								ref_icao7 = ref_icao7_x
								ref_icao_lat7 = ref_icao_lat7_x
								ref_icao_lon7 = ref_icao_lon7_x
								ref_tns_alt7 = ref_tns_alt7_x
								ref_tns_lvl7 = ref_tns_lvl7_x
								ref_icao_alt7 = ref_icao_alt7_x
								trans_alt7 = trans_alt_mod2
								
								ref_gate7 = ref_gate7_x
								co_route7 = co_route7_x
								flt_num = flt_num_x
								ref_rwy7 = ref_rwy8
								ref_rwy9 = ref_rwy8
								
								ref_rwy7 = ref_rwy8
								ref_sid7 = ref_sid8
								ref_sid_tns7 = ref_sid_tns8
								
								des_app_from_apt7 = 0
								
								des_icao7 = des_icao7_x
								des_rnw7 = des_rnw8
								des_icao_lat7 = des_icao_lat7_x
								des_icao_lon7 = des_icao_lon7_x
								des_tns_alt7 = des_tns_alt7_x
								des_tns_lvl7 = des_tns_lvl7_x
								des_icao_alt7 = des_icao_alt7_x
								trans_lvl7 = trans_lvl_mod2
								
								des_app7 = des_app8
								des_app_tns7 = des_app_tns8
								des_star7 = des_star8
								des_star_trans7 = des_star_trans8
								
								arr_data7 = arr_data7_x
								des_app_rw_only7 = des_app_rw_only7_x
								rw_ext_dist7 = rw_ext_dist7_x
								rw_ext_fpa7 = rw_ext_fpa7_x
								
								create_fpln(rte12_sel_fo)
								copy_to_legsdata(rte12_sel_fo)
								
								if rte12_changed ~= 0 then
									rte12_changed = 0
									
									-- offset = offset_mod
									offset = offset_mod2
									offset_seq = offset
									
									-- last_offset2  = offset
									-- offset_mod_x2 = offset_mod
									
									last_offset8  = offset
									offset_mod_x8 = offset_mod2
								end
								
								if exec_activate_fpln2 == 1 then
									if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
										reset_dl_times()
										if ref_icao7_x == "ORGN" then
											dl_ref = "----"
										else
											dl_ref = ref_icao7_x
										end
										dl_flt_num = flt_num_x
										dl_day = simDR_time_day
										dl_des = des_icao7_x
									end
								end
								
								
							else
								if legs_num > 1 then
									
									if legs_abeam_selected ~= 0 and B738DR_legs_abeam_avail ~= 0 and legs_abeam_data_n > 0 then
										rte_copy(offset_mod2, rte12_sel_fo)
										local abeam_wpt_added = 0
										local act_abeam_wpt = 0
										local delta_crs = 0
										for ab = 1, legs_abeam_data_n do
											if B738DR_legs_abeam_lat2[ab - 1] ~= 0 or B738DR_legs_abeam_lon2[ab - 1] ~= 0 then
												act_abeam_wpt = offset_mod2 + abeam_wpt_added
												-- add abeam wpt
												legs_data8[act_abeam_wpt] = {}
												legs_data8[act_abeam_wpt][1] = legs_abeam_data[ab][4]		--entry
												legs_data8[act_abeam_wpt][4] = 0		-- speed
												legs_data8[act_abeam_wpt][5] = 0		-- altitude
												legs_data8[act_abeam_wpt][6] = 0	-- altitude type
												legs_data8[act_abeam_wpt][7] = B738DR_legs_abeam_lat2[ab - 1]		-- latitude
												legs_data8[act_abeam_wpt][8] = B738DR_legs_abeam_lon2[ab - 1]		-- longitude
												legs_data8[act_abeam_wpt][9] = "DIRECT"			-- via id
												legs_data8[act_abeam_wpt][10] = 0		-- calc speed
												legs_data8[act_abeam_wpt][11] = 0		-- calc altitude
												legs_data8[act_abeam_wpt][12] = 0		-- calc altitude vnav pth
												legs_data8[act_abeam_wpt][13] = 0
												legs_data8[act_abeam_wpt][14] = 0		-- rest alt
												legs_data8[act_abeam_wpt][15] = 0		-- last fuel
												legs_data8[act_abeam_wpt][16] = ""		-- reg code
												legs_data8[act_abeam_wpt][17] = 0		-- spd flag 0-default restrict, 1-custom restrict
												legs_data8[act_abeam_wpt][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
												legs_data8[act_abeam_wpt][20] = 0
												legs_data8[act_abeam_wpt][21] = -1
												legs_data8[act_abeam_wpt][22] = ""
												legs_data8[act_abeam_wpt][23] = 0
												legs_data8[act_abeam_wpt][24] = 0
												legs_data8[act_abeam_wpt][25] = 0
												legs_data8[act_abeam_wpt][26] = 0
												legs_data8[act_abeam_wpt][27] = ""
												legs_data8[act_abeam_wpt][28] = ""
												legs_data8[act_abeam_wpt][29] = ""
												legs_data8[act_abeam_wpt][30] = ""
												legs_data8[act_abeam_wpt][32] = 0
												legs_data8[act_abeam_wpt][33] = ""
												legs_data8[act_abeam_wpt][34] = ""
												legs_data8[act_abeam_wpt][35] = ""
												legs_data8[act_abeam_wpt][36] = 0
												legs_data8[act_abeam_wpt][38] = ""
												if B738DR_legs_abeam_dist[ab - 1] < 100 then
													legs_data8[act_abeam_wpt][39] = legs_abeam_data[ab][5]		-- Wind data
												else
													legs_data8[act_abeam_wpt][39] = ""
												end
												legs_data8[act_abeam_wpt][40] = 0
												legs_data8[act_abeam_wpt][41] = 0
												legs_data8[act_abeam_wpt][42] = 0
												legs_data8[act_abeam_wpt][43] = ""
												legs_data8[act_abeam_wpt][44] = 0
												legs_data8[act_abeam_wpt][45] = 0
												legs_data8[act_abeam_wpt][46] = 0
												legs_data8[act_abeam_wpt][47] = 0
												legs_data8[act_abeam_wpt][48] = ""
												legs_data8[act_abeam_wpt][49] = 0
												legs_data8[act_abeam_wpt][50] = 0
												legs_data8[act_abeam_wpt][51] = 0
												legs_data8[act_abeam_wpt][52] = 0
												legs_data8[act_abeam_wpt][53] = 0
												legs_data8[act_abeam_wpt][54] = 0
												
												legs_data8[act_abeam_wpt][3] = nd_calc_dist2(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
												
												if ab == 1 then
													legs_data8[act_abeam_wpt][31] = "DF"
												else
													legs_data8[act_abeam_wpt][31] = "TF"
												end
												
												if legs_data8[act_abeam_wpt][3] == 0 then
													legs_data8[act_abeam_wpt][2] = legs_data8[act_abeam_wpt-1][2]
												else
													legs_data8[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
												end
												legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
												if legs_data8[act_abeam_wpt][18] < 0 then
													legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][18] + (2 * math.pi)
												end
												if string.sub(legs_data8[act_abeam_wpt][1], 1, 2) == "RW" then
													legs_data8[act_abeam_wpt][37] = legs_data8[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
												else
													delta_crs = (nd_calc_brg(legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8], legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8]) + 180) % 360
													legs_data8[act_abeam_wpt][37] = math.rad(delta_crs)
												end
												
												abeam_wpt_added = abeam_wpt_added + 1
											end
										end
										rte_paste(offset_mod2 + abeam_wpt_added, rte12_sel_fo)
										
										if abeam_wpt_added ~= 0 then
											act_abeam_wpt = offset_mod + abeam_wpt_added
											
											legs_data8[act_abeam_wpt][3] = nd_calc_dist2(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
											
											legs_data8[act_abeam_wpt][31] = "TF"
											
											if legs_data8[act_abeam_wpt][3] == 0 then
												legs_data8[act_abeam_wpt][2] = legs_data8[act_abeam_wpt-1][2]
											else
												legs_data8[act_abeam_wpt][2] = nd_calc_brg_rad(legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8], legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8])
											end
											legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][2] + mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
											if legs_data8[act_abeam_wpt][18] < 0 then
												legs_data8[act_abeam_wpt][18] = legs_data8[act_abeam_wpt][18] + (2 * math.pi)
											end
											if string.sub(legs_data8[act_abeam_wpt][1], 1, 2) == "RW" then
												legs_data8[act_abeam_wpt][37] = legs_data8[act_abeam_wpt][18] - mag_variation_rad(math.rad(legs_data8[act_abeam_wpt][7]), math.rad(legs_data8[act_abeam_wpt][8]))
											else
												delta_crs = (nd_calc_brg(legs_data8[act_abeam_wpt][7], legs_data8[act_abeam_wpt][8], legs_data8[act_abeam_wpt-1][7], legs_data8[act_abeam_wpt-1][8]) + 180) % 360
												legs_data8[act_abeam_wpt][37] = math.rad(delta_crs)
											end
										end
										
									end
									
									-- recalc abeam data: distance, brg,...
									-- abeamsk
									
									copy_to_legsdata(rte12_sel_fo)
									
									--- new ---
									if ref_rwy7 ~= ref_rwy8 and ref_rwy7 ~= "-----" then
										if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
											v1_set = "---"
											vr_set = "---"
											v2_set = "---"
											verify_to_spd = 0
											add_fmc_msg(TO_SPEEDS_DELETED, 1)
										end
									end
									ref_rwy7 = ref_rwy8
									ref_sid7 = ref_sid8
									ref_sid_tns7 = ref_sid_tns8
									des_rnw7 = des_rnw8
									des_app7 = des_app8
									des_app_tns7 = des_app_tns8
									des_star7 = des_star8
									des_star_trans7 = des_star_trans8
									arr_data7 = arr_data7_x
									des_app_rw_only7 = des_app_rw_only7_x
									rw_ext_dist7 = rw_ext_dist7_x
									rw_ext_fpa7 = rw_ext_fpa7_x
								end
								offset = offset_mod2
								
							end
							-- legs_delete = 0
							rte_exec2 = 0
							if offset > legs_num then
								offset = legs_num
								offset_seq = offset
							end
							
							exec_activate_fpln2 = 0
							
							legs_abeam_data_n = 0
							legs_abeam_data = {}
							legs_abeam_selected = 0
							B738DR_legs_abeam_avail = 0
							B738DR_legs_abeam_num = 0
							
							-- legs_delete = 0
						end
						
						if (page_offset2 == 1 or legs_delete ~= 0) and offset_act ~= offset_act_mod and legs_num > 1 then
							-- execute Lateral Offset
							if offset_dist_mod == 0 then
								if offset_act >= 5 and offset_act < 8 then
									offset_act = 9
									offset_act_mod = 9
									B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
									B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
									B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
									B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
									B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
									B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
									B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
									B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
								elseif offset_act >= 8 then
									offset_act_mod = offset_act
								else
									offset_act = 0
									offset_start = 0
									offset_end = 0
									offset_dist = 0
									offset_side = 0
									-- legs_off_tmp_n = 0
									delete_offset_legs()
									delete_offset_legs2(rte12_act)
									offset_act_mod = 0
									offset_start_mod = 0
									offset_end_mod = 0
									offset_dist_mod = 0
									offset_side_mod = 0
									offset_start_def = 0
									offset_end_def = 0
									offset_start_def_mod = 0
									offset_end_def_mod = 0
								end
							else
								-- update_offset_idx2(rte12_act)
								offset_check(rte12_act)
								if offset_dist_mod ~= 0 and offset_start_def_mod ~= 0 and offset_end_def_mod ~= 0 then
									-- if offset_start_mod == 0 and offset_start_def_mod <= offset and offset_act == 0 then
										-- offset_act_mod = 5
									-- else
										-- offset_act_mod = 7
									-- end
									
									if rte12_act == 0 then
										if offset_start_mod == 0 then
											if legs_data2[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									else
										if offset_start_mod == 0 then
											if legs_data8[offset][45] ~= 0 then
												offset_act_mod = 5
											else
												if offset_act >= 5 and offset_act < 8 then
													offset_act = 9
													offset_act_mod = 9
												elseif offset_act >= 8 then
													offset_act_mod = offset_act
												else
													offset_act_mod = offset_act
												end
											end
										else
											offset_act_mod = 7
										end
									end
								end
								offset_act = offset_act_mod
								
								legs_intdir_act = 0	-- direct to wpt
								legs_intdir_crs = -1
								
								offset_start = offset_start_mod
								offset_end = offset_end_mod
								offset_dist = offset_dist_mod
								offset_side = offset_side_mod
								offset_start_def = offset_start_def_mod
								offset_end_def = offset_end_def_mod
								B738DR_fms_legs_offset_latx_1 = B738DR_fms_legs_offset_latx_1_mod
								B738DR_fms_legs_offset_lonx_1 = B738DR_fms_legs_offset_lonx_1_mod
								B738DR_fms_legs_offset_latx_2 = B738DR_fms_legs_offset_latx_2_mod
								B738DR_fms_legs_offset_lonx_2 = B738DR_fms_legs_offset_lonx_2_mod
								B738DR_fms_legs_offset_latx_3 = B738DR_fms_legs_offset_latx_3_mod
								B738DR_fms_legs_offset_lonx_3 = B738DR_fms_legs_offset_lonx_3_mod
								B738DR_fms_legs_offset_latx_4 = B738DR_fms_legs_offset_latx_4_mod
								B738DR_fms_legs_offset_lonx_4 = B738DR_fms_legs_offset_lonx_4_mod
							end
						end
						legs_delete = 0
						
						rte21_copy_done = 0
					end
				end
			end
			
			-- if page_perf2 == 1 then
			if page_perf2 == 1 or page_legs2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_init2 > 0 or page_rte_legs2 > 0 
			or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 then
				if perf_exec == 1 then
					perf_exec = 2
					B738_calc_vnav_spd()
					vnav_update = 1
				elseif perf_exec == 3 then
					perf_exec = 2
					
					gw = gw_mod
					gw_lbs = gw_lbs_mod
					gw_kgs = gw_kgs_mod
					zfw = zfw_mod
					zfw_lbs = zfw_lbs_mod
					zfw_kgs = zfw_kgs_mod
					plan_weight = plan_weight_mod
					plan_weight_kgs = plan_weight_kgs_mod
					plan_weight_lbs = plan_weight_lbs_mod
					reserves = reserves_mod
					reserves_kgs = reserves_kgs_mod
					reserves_lbs = reserves_lbs_mod
					cost_index = cost_index_mod
					crz_wind_dir = crz_wind_dir_mod
					crz_wind_spd = crz_wind_spd_mod
					isa_dev_f = isa_dev_f_mod
					isa_dev_c = isa_dev_c_mod
					tc_oat_c = tc_oat_c_mod
					tc_oat_f = tc_oat_f_mod
					msg_chk_alt_constr = 0
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if v1_set == "---" and vr_set == "---" and v2_set == "---" then
							entry2 = ""
						else
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
							qrh = "OFF"
							entry2 = ""
						end
					end
					B738DR_calc_vspd = 1
					B738DR_calc_trim = 1
					
					B738_calc_vnav_spd()
					vnav_update = 1
				end
			end
			
			-- if page_legs2 > 0 then
			if page_perf2 == 1 or page_legs2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_init2 > 0 or page_rte_legs2 > 0 
			or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 or page_hold2 > 0 then
				local disable_legs_intdir = false
				if exec_activate_fpln ~= 0 and legs_intdir_rte == 0 then
					disable_legs_intdir = true
				elseif exec_activate_fpln2 ~= 0 and legs_intdir_rte == 1 then
					disable_legs_intdir = true
				end
				if legs_intdir == 1 and disable_legs_intdir == false then
					-- take current position
					last_lat = simDR_latitude
					last_lon = simDR_longitude
					if legs_intdir_crs2 < 0 then
						legs_intdir_crs = -1
					else
						legs_intdir_crs = legs_intdir_crs_mod
					end
					legs_intdir = 0
					legs_intdir_act = 1
					legs_intdir_crs2 = -1
					calc_rte_enable = 1
					block_df = 0
					B738DR_fmc_trk_turn = -1
					B738DR_fmc_trk_turn2 = -1
					legs_intdir_trk2_mem = legs_intdir_trk2
					legs_intdir_radius_mem = legs_intdir_radius_mod
					legs_intdir_rad_lat_mem = legs_intdir_rad_lat_mod
					legs_intdir_rad_lon_mem = legs_intdir_rad_lon_mod
					legs_intdir_turn_mem = legs_intdir_turn_mod
					legs_intdir_end_lat_mem = legs_intdir_end_lat_mod
					legs_intdir_end_lon_mem = legs_intdir_end_lon_mod
					B738DR_fms_legs_bypass[offset] = 0
					B738DR_fms_legs_change = 0
				end
			end
			
			if page_hold2 > 0 then
				if hold_exec == 1 and rte12_sel_fo == 0 then
					
					if B738DR_hold_term == 0 or rte12_act ~= rte12_sel_fo then
						
						if rte12_act == rte12_sel_fo then
							copy_to_legsdata(rte12_sel_fo)
						end
						
						entry2 = ""
						hold_exec = 0
						new_hold = 0
						new_hold_idx = 0
						new_hold_act_page = 0
						hold_offset = 0
						if hold_data_num2 > 0 then
							for qq = 1, hold_data_num2 do
								if hold_data2[qq] >= offset_mod then
									hold_offset = qq
									break
								end
							end
							if legs_data[offset][1] == "PPOS" and rte12_act == rte12_sel_fo then
								legs_data[offset][7] = simDR_latitude
								legs_data[offset][8] = simDR_longitude
								legs_data2[offset][7] = simDR_latitude
								legs_data2[offset][8] = simDR_longitude
								
								-- -- PPOS HOLD activated
								calc_rte_enable2 = 1
								
								B738DR_hold_phase = 1
								B738DR_fmc_trk_turn = -1
								hold_timer = 0
								nav_mode = 3
								legs_intdir_act = 0
								legs_intdir_crs = -1
							end
						end
					else
						B738DR_hold_term = 2
						hold_exec = 0
						new_hold_idx = 0
						new_hold_act_page = 0
					end
				elseif hold_exec2 == 1 and rte12_sel_fo == 1 then
					
					if B738DR_hold_term == 0 or rte12_act ~= rte12_sel_fo then
						
						if rte12_act == rte12_sel_fo then
							copy_to_legsdata(rte12_sel_fo)
						end
						
						entry2 = ""
						hold_exec2 = 0
						new_hold2 = 0
						new_hold_idx2 = 0
						new_hold_act_page2 = 0
						hold_offset2 = 0
						if hold_data_num4 > 0 then
							for qq = 1, hold_data_num4 do
								if hold_data4[qq] >= offset_mod2 then
									hold_offset2 = qq
									break
								end
							end
							if legs_data[offset][1] == "PPOS" and rte12_act == rte12_sel_fo then
								legs_data[offset][7] = simDR_latitude
								legs_data[offset][8] = simDR_longitude
								legs_data2[offset][7] = simDR_latitude
								legs_data2[offset][8] = simDR_longitude
								
								-- -- PPOS HOLD activated
								calc_rte_enable8 = 1
								
								B738DR_hold_phase = 1
								B738DR_fmc_trk_turn = -1
								hold_timer = 0
								nav_mode = 3
								legs_intdir_act = 0
								legs_intdir_crs = -1
							end
						end
					else
						B738DR_hold_term = 2
						hold_exec2 = 0
						new_hold_idx2 = 0
						new_hold_act_page2 = 0
					end
				end
			end
			
			if page_ref_nav_data_wpt2 == 1 then
				if ref_nav_exec == 1 then
					local ref_nav_new_act = 0
					local ii = 0
					
					if nav_data_idx2 == 0 then
						wptx_n = wptx_n + 1
						wptx[wptx_n] = {}
						nav_data_idx2 = wptx_n
					end
					ref_nav_new_act = nav_data_idx2
					
					if ref_nav_new == "*****" then
						wptx[ref_nav_new_act][1] = ref_nav_wpt
					else
						wptx[ref_nav_new_act][1] = ref_nav_new
					end
					wptx[ref_nav_new_act][2] = ref_nav_lat
					wptx[ref_nav_new_act][3] = ref_nav_lon
					if ref_nav_id == "" then
						wptx[ref_nav_new_act][4] = " "
					else
						wptx[ref_nav_new_act][4] = ref_nav_id
					end
					if ref_nav_rc == "" then
						wptx[ref_nav_new_act][5] = " "
					else
						wptx[ref_nav_new_act][5] = ref_nav_rc
					end
					wptx[ref_nav_new_act][6] = ref_nav_brg
					wptx[ref_nav_new_act][7] = ref_nav_dist
					if ref_nav_mag_var == "" then
						wptx[ref_nav_new_act][8] = "x"
					elseif string.sub(ref_nav_mag_var, 1, 1) == ">" then
						wptx[ref_nav_new_act][8] = "x"
					else
						wptx[ref_nav_new_act][8] = ref_nav_mag_var
					end
					save_cust_wpt()
					ref_nav_exec = 0
				end
			end
			update_rnw_list(rte12_sel_fo)
		end
		fmc2_input_lag = 0
		rte12_changed = 0
	elseif phase == 2 then
		B738DR_fms_key = 0
	end
end
-- FOvarsk
-- entry2
-- item_sel2
-- legs_step2
-- act_page2
-- max_page2
-- item_sel_via2
-- new_hold2

function B738_fmc_reset_CMDhandler(phase, duration)
	if phase == 0 then
		B738_init2()
		B738_init3()
		B738_init4()
		B738_init_vspd()
		entry = ">FMC RESET OK"
		entry2 = ""
		B738DR_fmc_message_warn = 1
		fms_msg_sound = 1
		B738DR_lnav_disconnect = 1
		B738DR_vnav_disconnect = 1
		menu_timer = 1
	end
end

function B738_nosewheel_yaw_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_nosewheel = 1
	end
end

function B738_nosewheel_roll_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_nosewheel = 2
	end
end

function B738_nosewheel_vr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_nosewheel = 3
	end
end

function B738_toe_brake_axis_switch_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_toe_brakes_ovr == 0 then
			B738DR_toe_brakes_ovr = 1
		else
			B738DR_toe_brakes_ovr = 0
		end
	end
end

function B738_change_units_CMDhandler(phase, duration)
	if phase == 0 then
		if units == 0 then
			units = 1
		else
			units = 0
		end
	end
end

-- LOAD/SAVE CONFIG AUDIO SETTINGS
function B738_save_config_CMDhandler(phase, duration)
	if phase == 0 then
		B738_save_config(file_path_cfg)
		B738_save_config("")
	end
end

function B738_reset_cfg_audio_CMDhandler(phase, duration)
	if phase == 0 then
		B738_default_fmod_config()
		B738_default_others_config()
	end
end

function B738_reset_cfg_CMDhandler(phase, duration)
	if phase == 0 then
		B738_default_others_config()
	end
end

function B738_reset_audio_CMDhandler(phase, duration)
	if phase == 0 then
		B738_default_fmod_config()
	end
end


function B738_load_cfg1_CMDhandler(phase, duration)
	if phase == 0 then
		-- B738_set_cfg_config(1)
	end
end

function B738_load_cfg2_CMDhandler(phase, duration)
	if phase == 0 then
		-- B738_set_cfg_config(2)
	end
end

function B738_load_cfg3_CMDhandler(phase, duration)
	if phase == 0 then
		-- B738_set_cfg_config(3)
	end
end

function B738_load_cfg4_CMDhandler(phase, duration)
	if phase == 0 then
		-- B738_set_cfg_config(4)
	end
end

function B738_save_cfg1_CMDhandler(phase, duration)
	if phase == 0 then
		-- cfg_preset[1][1] = "PRESET 1"
		-- B738_mod_cfg_config(1)
		-- B738_save_cfg_config(file_path_cfg)
		-- B738_save_cfg_config("")
	end
end

function B738_save_cfg2_CMDhandler(phase, duration)
	if phase == 0 then
		-- cfg_preset[2][1] = "PRESET 2"
		-- B738_mod_cfg_config(2)
		-- B738_save_cfg_config(file_path_cfg)
		-- B738_save_cfg_config("")
	end
end

function B738_save_cfg3_CMDhandler(phase, duration)
	if phase == 0 then
		-- cfg_preset[3][1] = "PRESET 3"
		-- B738_mod_cfg_config(3)
		-- B738_save_cfg_config(file_path_cfg)
		-- B738_save_cfg_config("")
	end
end

function B738_save_cfg4_CMDhandler(phase, duration)
	if phase == 0 then
		-- cfg_preset[4][1] = "PRESET 4"
		-- B738_mod_cfg_config(4)
		-- B738_save_cfg_config(file_path_cfg)
		-- B738_save_cfg_config("")
	end
end

function B738_load_audio1_CMDhandler(phase, duration)
	if phase == 0 then
		B738_set_fmod_config(1)
	end
end

function B738_load_audio2_CMDhandler(phase, duration)
	if phase == 0 then
		B738_set_fmod_config(2)
	end
end

function B738_load_audio3_CMDhandler(phase, duration)
	if phase == 0 then
		B738_set_fmod_config(3)
	end
end

function B738_load_audio4_CMDhandler(phase, duration)
	if phase == 0 then
		B738_set_fmod_config(4)
	end
end

function B738_save_audio1_CMDhandler(phase, duration)
	if phase == 0 then
		fmod_preset[1][1] = "PRESET 1"
		B738_mod_fmod_config(1)
		-- B738_save_fmod_config(file_path_cfg)
		B738_save_fmod_config("")
	end
end

function B738_save_audio2_CMDhandler(phase, duration)
	if phase == 0 then
		fmod_preset[2][1] = "PRESET 2"
		B738_mod_fmod_config(2)
		-- B738_save_fmod_config(file_path_cfg)
		B738_save_fmod_config("")
	end
end

function B738_save_audio3_CMDhandler(phase, duration)
	if phase == 0 then
		fmod_preset[3][1] = "PRESET 3"
		B738_mod_fmod_config(3)
		-- B738_save_fmod_config(file_path_cfg)
		B738_save_fmod_config("")
	end
end

function B738_save_audio4_CMDhandler(phase, duration)
	if phase == 0 then
		fmod_preset[4][1] = "PRESET 4"
		B738_mod_fmod_config(4)
		-- B738_save_fmod_config(file_path_cfg)
		B738_save_fmod_config("")
	end
end

function B738_set_flap_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[0] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[0] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[0] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(1)
	end
end

function B738_set_flap_1_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[1] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[1] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[1] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(2)
	end
end

function B738_set_flap_2_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[2] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[2] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[2] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(3)
	end
end

function B738_set_flap_5_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[3] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[3] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[3] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(4)
	end
end

function B738_set_flap_10_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[4] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[4] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[4] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(5)
	end
end

function B738_set_flap_15_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[5] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[5] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[5] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(6)
	end
end

function B738_set_flap_25_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[6] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[6] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[6] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(7)
	end
end

function B738_set_flap_30_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[7] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[7] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[7] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(8)
	end
end

function B738_set_flap_40_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cfy_enable ~= 0 then
			B738DR_flap_detents[8] = B738DR_cust_axis_flaps
		elseif B738DR_cust_axis_flaps_mapped == -1 then
			B738DR_flap_detents[8] = B738DR_joy_axis_flap_lever
		else
			B738DR_flap_detents[8] = B738DR_cust_axis_flaps
		end
		calc_detents_thr(9)
	end
end

simDR_yaw_damper_switch				= find_dataref("sim/cockpit2/switches/yaw_damper_on")
B738DR_yaw_damper_switch_pos		= find_dataref("laminar/B738/toggle_switch/yaw_dumper_pos")

function B738_load_cfg99_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_yaw_damper_switch_pos = 1
		simDR_yaw_damper_switch = 1
	end
end

function B738_msg_not_intcpt_CMDhandler(phase, duration)
	if phase == 0 then
		add_fmc_msg(NOT_INTCPT_HDG, 2)
	end
end

function B738_change_payload_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_gpu_test_enable = 1
		if is_timer_scheduled(gpu_test_reset) == false then
			run_after_time(gpu_test_reset, 3.5)
		end
	end
end

function B738_update_factory_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_thr_cutback == 0 then
			clb_alt_num = B738DR_def_clb_alt
		else
			clb_alt_num = B738DR_def_clb_alt2
		end
		cut_alt_num = B738DR_def_cut_alt
		accel_alt_num = B738DR_def_accel_alt
		eo_accel_alt_num = B738DR_def_eo_accel_alt
	end
end

--*************************************************************************************--
--** 				                 X-PLANE COMMANDS                   	    	 **--
--*************************************************************************************--

-- FMC CAPTAIN
B738CMD_fmc1_1L = create_command("laminar/B738/button/fmc1_1L", "FMC capt 1L", B738_fmc1_1L_CMDhandler)
B738CMD_fmc1_2L = create_command("laminar/B738/button/fmc1_2L", "FMC capt 2L", B738_fmc1_2L_CMDhandler)
B738CMD_fmc1_3L = create_command("laminar/B738/button/fmc1_3L", "FMC capt 3L", B738_fmc1_3L_CMDhandler)
B738CMD_fmc1_4L = create_command("laminar/B738/button/fmc1_4L", "FMC capt 4L", B738_fmc1_4L_CMDhandler)
B738CMD_fmc1_5L = create_command("laminar/B738/button/fmc1_5L", "FMC capt 5L", B738_fmc1_5L_CMDhandler)
B738CMD_fmc1_6L = create_command("laminar/B738/button/fmc1_6L", "FMC capt 6L", B738_fmc1_6L_CMDhandler)

B738CMD_fmc1_1R = create_command("laminar/B738/button/fmc1_1R", "FMC capt 1R", B738_fmc1_1R_CMDhandler)
B738CMD_fmc1_2R = create_command("laminar/B738/button/fmc1_2R", "FMC capt 2R", B738_fmc1_2R_CMDhandler)
B738CMD_fmc1_3R = create_command("laminar/B738/button/fmc1_3R", "FMC capt 3R", B738_fmc1_3R_CMDhandler)
B738CMD_fmc1_4R = create_command("laminar/B738/button/fmc1_4R", "FMC capt 4R", B738_fmc1_4R_CMDhandler)
B738CMD_fmc1_5R = create_command("laminar/B738/button/fmc1_5R", "FMC capt 5R", B738_fmc1_5R_CMDhandler)
B738CMD_fmc1_6R = create_command("laminar/B738/button/fmc1_6R", "FMC capt 6R", B738_fmc1_6R_CMDhandler)

B738CMD_fmc1_0 = create_command("laminar/B738/button/fmc1_0", "FMC capt 0", B738_fmc1_0_CMDhandler)
B738CMD_fmc1_1 = create_command("laminar/B738/button/fmc1_1", "FMC capt 1", B738_fmc1_1_CMDhandler)
B738CMD_fmc1_2 = create_command("laminar/B738/button/fmc1_2", "FMC capt 2", B738_fmc1_2_CMDhandler)
B738CMD_fmc1_3 = create_command("laminar/B738/button/fmc1_3", "FMC capt 3", B738_fmc1_3_CMDhandler)
B738CMD_fmc1_4 = create_command("laminar/B738/button/fmc1_4", "FMC capt 4", B738_fmc1_4_CMDhandler)
B738CMD_fmc1_5 = create_command("laminar/B738/button/fmc1_5", "FMC capt 5", B738_fmc1_5_CMDhandler)
B738CMD_fmc1_6 = create_command("laminar/B738/button/fmc1_6", "FMC capt 6", B738_fmc1_6_CMDhandler)
B738CMD_fmc1_7 = create_command("laminar/B738/button/fmc1_7", "FMC capt 7", B738_fmc1_7_CMDhandler)
B738CMD_fmc1_8 = create_command("laminar/B738/button/fmc1_8", "FMC capt 8", B738_fmc1_8_CMDhandler)
B738CMD_fmc1_9 = create_command("laminar/B738/button/fmc1_9", "FMC capt 9", B738_fmc1_9_CMDhandler)

B738CMD_fmc1_period = create_command("laminar/B738/button/fmc1_period", "FMC capt .", B738_fmc1_period_CMDhandler)
B738CMD_fmc1_minus = create_command("laminar/B738/button/fmc1_minus", "FMC capt -", B738_fmc1_minus_CMDhandler)
B738CMD_fmc1_slash = create_command("laminar/B738/button/fmc1_slash", "FMC capt SLASH", B738_fmc1_slash_CMDhandler)

B738CMD_fmc1_A = create_command("laminar/B738/button/fmc1_A", "FMC capt A", B738_fmc1_A_CMDhandler)
B738CMD_fmc1_B = create_command("laminar/B738/button/fmc1_B", "FMC capt B", B738_fmc1_B_CMDhandler)
B738CMD_fmc1_C = create_command("laminar/B738/button/fmc1_C", "FMC capt C", B738_fmc1_C_CMDhandler)
B738CMD_fmc1_D = create_command("laminar/B738/button/fmc1_D", "FMC capt D", B738_fmc1_D_CMDhandler)
B738CMD_fmc1_E = create_command("laminar/B738/button/fmc1_E", "FMC capt E", B738_fmc1_E_CMDhandler)
B738CMD_fmc1_F = create_command("laminar/B738/button/fmc1_F", "FMC capt F", B738_fmc1_F_CMDhandler)
B738CMD_fmc1_G = create_command("laminar/B738/button/fmc1_G", "FMC capt G", B738_fmc1_G_CMDhandler)
B738CMD_fmc1_H = create_command("laminar/B738/button/fmc1_H", "FMC capt H", B738_fmc1_H_CMDhandler)
B738CMD_fmc1_I = create_command("laminar/B738/button/fmc1_I", "FMC capt I", B738_fmc1_I_CMDhandler)
B738CMD_fmc1_J = create_command("laminar/B738/button/fmc1_J", "FMC capt J", B738_fmc1_J_CMDhandler)
B738CMD_fmc1_K = create_command("laminar/B738/button/fmc1_K", "FMC capt K", B738_fmc1_K_CMDhandler)
B738CMD_fmc1_L = create_command("laminar/B738/button/fmc1_L", "FMC capt L", B738_fmc1_L_CMDhandler)
B738CMD_fmc1_M = create_command("laminar/B738/button/fmc1_M", "FMC capt M", B738_fmc1_M_CMDhandler)
B738CMD_fmc1_N = create_command("laminar/B738/button/fmc1_N", "FMC capt N", B738_fmc1_N_CMDhandler)
B738CMD_fmc1_O = create_command("laminar/B738/button/fmc1_O", "FMC capt O", B738_fmc1_O_CMDhandler)
B738CMD_fmc1_P = create_command("laminar/B738/button/fmc1_P", "FMC capt P", B738_fmc1_P_CMDhandler)
B738CMD_fmc1_Q = create_command("laminar/B738/button/fmc1_Q", "FMC capt Q", B738_fmc1_Q_CMDhandler)
B738CMD_fmc1_R = create_command("laminar/B738/button/fmc1_R", "FMC capt R", B738_fmc1_R_CMDhandler)
B738CMD_fmc1_S = create_command("laminar/B738/button/fmc1_S", "FMC capt S", B738_fmc1_S_CMDhandler)
B738CMD_fmc1_T = create_command("laminar/B738/button/fmc1_T", "FMC capt T", B738_fmc1_T_CMDhandler)
B738CMD_fmc1_U = create_command("laminar/B738/button/fmc1_U", "FMC capt U", B738_fmc1_U_CMDhandler)
B738CMD_fmc1_V = create_command("laminar/B738/button/fmc1_V", "FMC capt V", B738_fmc1_V_CMDhandler)
B738CMD_fmc1_W = create_command("laminar/B738/button/fmc1_W", "FMC capt W", B738_fmc1_W_CMDhandler)
B738CMD_fmc1_X = create_command("laminar/B738/button/fmc1_X", "FMC capt X", B738_fmc1_X_CMDhandler)
B738CMD_fmc1_Y = create_command("laminar/B738/button/fmc1_Y", "FMC capt Y", B738_fmc1_Y_CMDhandler)
B738CMD_fmc1_Z = create_command("laminar/B738/button/fmc1_Z", "FMC capt Z", B738_fmc1_Z_CMDhandler)
B738CMD_fmc1_SP = create_command("laminar/B738/button/fmc1_SP", "FMC capt Z", B738_fmc1_SP_CMDhandler)



B738CMD_fmc1_clr = create_command("laminar/B738/button/fmc1_clr", "FMC capt CLR", B738_fmc1_clr_CMDhandler)
B738CMD_fmc1_del = create_command("laminar/B738/button/fmc1_del", "FMC capt DEL", B738_fmc1_del_CMDhandler)

B738CMD_fmc1_prev_page = create_command("laminar/B738/button/fmc1_prev_page", "FMC capt PREV PAGE", B738_fmc1_prev_page_CMDhandler)
B738CMD_fmc1_next_page = create_command("laminar/B738/button/fmc1_next_page", "FMC capt NEXT PAGE", B738_fmc1_next_page_CMDhandler)

B738CMD_fmc1_init_ref = create_command("laminar/B738/button/fmc1_init_ref", "FMC capt INIT REF", B738_fmc1_init_ref_CMDhandler)
B738CMD_fmc1_menu = create_command("laminar/B738/button/fmc1_menu", "FMC capt MENU", B738_fmc1_menu_CMDhandler)
B738CMD_fmc1_n1_lim = create_command("laminar/B738/button/fmc1_n1_lim", "FMC capt N1 LIMIT", B738_fmc1_n1_lim_CMDhandler)
B738CMD_fmc1_rte = create_command("laminar/B738/button/fmc1_rte", "FMC capt RTE", B738_fmc1_rte_CMDhandler)
B738CMD_fmc1_legs = create_command("laminar/B738/button/fmc1_legs", "FMC capt LEGS", B738_fmc1_legs_CMDhandler)
B738CMD_fmc1_fix = create_command("laminar/B738/button/fmc1_fix", "FMC capt FIX", B738_fmc1_fix_CMDhandler)
B738CMD_fmc1_clb = create_command("laminar/B738/button/fmc1_clb", "FMC capt CLB", B738_fmc1_clb_CMDhandler)
B738CMD_fmc1_crz = create_command("laminar/B738/button/fmc1_crz", "FMC capt CRZ", B738_fmc1_crz_CMDhandler)
B738CMD_fmc1_des = create_command("laminar/B738/button/fmc1_des", "FMC capt DES", B738_fmc1_des_CMDhandler)
B738CMD_fmc1_dep_app = create_command("laminar/B738/button/fmc1_dep_app", "FMC capt DEP/APP", B738_fmc1_dep_app_CMDhandler)
B738CMD_fmc1_hold = create_command("laminar/B738/button/fmc1_hold", "FMC capt HOLD", B738_fmc1_hold_CMDhandler)
B738CMD_fmc1_prog = create_command("laminar/B738/button/fmc1_prog", "FMC capt PROG", B738_fmc1_prog_CMDhandler)
B738CMD_fmc1_exec = create_command("laminar/B738/button/fmc1_exec", "FMC capt EXEC", B738_fmc1_exec_CMDhandler)


-- FMC FIRST OFFICER
B738CMD_fmc2_1L = create_command("laminar/B738/button/fmc2_1L", "FMC2 capt 1L", B738_fmc2_1L_CMDhandler)
B738CMD_fmc2_2L = create_command("laminar/B738/button/fmc2_2L", "FMC2 capt 2L", B738_fmc2_2L_CMDhandler)
B738CMD_fmc2_3L = create_command("laminar/B738/button/fmc2_3L", "FMC2 capt 3L", B738_fmc2_3L_CMDhandler)
B738CMD_fmc2_4L = create_command("laminar/B738/button/fmc2_4L", "FMC2 capt 4L", B738_fmc2_4L_CMDhandler)
B738CMD_fmc2_5L = create_command("laminar/B738/button/fmc2_5L", "FMC2 capt 5L", B738_fmc2_5L_CMDhandler)
B738CMD_fmc2_6L = create_command("laminar/B738/button/fmc2_6L", "FMC2 capt 6L", B738_fmc2_6L_CMDhandler)

B738CMD_fmc2_1R = create_command("laminar/B738/button/fmc2_1R", "FMC2 capt 1R", B738_fmc2_1R_CMDhandler)
B738CMD_fmc2_2R = create_command("laminar/B738/button/fmc2_2R", "FMC2 capt 2R", B738_fmc2_2R_CMDhandler)
B738CMD_fmc2_3R = create_command("laminar/B738/button/fmc2_3R", "FMC2 capt 3R", B738_fmc2_3R_CMDhandler)
B738CMD_fmc2_4R = create_command("laminar/B738/button/fmc2_4R", "FMC2 capt 4R", B738_fmc2_4R_CMDhandler)
B738CMD_fmc2_5R = create_command("laminar/B738/button/fmc2_5R", "FMC2 capt 5R", B738_fmc2_5R_CMDhandler)
B738CMD_fmc2_6R = create_command("laminar/B738/button/fmc2_6R", "FMC2 capt 6R", B738_fmc2_6R_CMDhandler)

B738CMD_fmc2_0 = create_command("laminar/B738/button/fmc2_0", "FMC2 capt 0", B738_fmc2_0_CMDhandler)
B738CMD_fmc2_1 = create_command("laminar/B738/button/fmc2_1", "FMC2 capt 1", B738_fmc2_1_CMDhandler)
B738CMD_fmc2_2 = create_command("laminar/B738/button/fmc2_2", "FMC2 capt 2", B738_fmc2_2_CMDhandler)
B738CMD_fmc2_3 = create_command("laminar/B738/button/fmc2_3", "FMC2 capt 3", B738_fmc2_3_CMDhandler)
B738CMD_fmc2_4 = create_command("laminar/B738/button/fmc2_4", "FMC2 capt 4", B738_fmc2_4_CMDhandler)
B738CMD_fmc2_5 = create_command("laminar/B738/button/fmc2_5", "FMC2 capt 5", B738_fmc2_5_CMDhandler)
B738CMD_fmc2_6 = create_command("laminar/B738/button/fmc2_6", "FMC2 capt 6", B738_fmc2_6_CMDhandler)
B738CMD_fmc2_7 = create_command("laminar/B738/button/fmc2_7", "FMC2 capt 7", B738_fmc2_7_CMDhandler)
B738CMD_fmc2_8 = create_command("laminar/B738/button/fmc2_8", "FMC2 capt 8", B738_fmc2_8_CMDhandler)
B738CMD_fmc2_9 = create_command("laminar/B738/button/fmc2_9", "FMC2 capt 9", B738_fmc2_9_CMDhandler)

B738CMD_fmc2_period = create_command("laminar/B738/button/fmc2_period", "FMC2 capt .", B738_fmc2_period_CMDhandler)
B738CMD_fmc2_minus = create_command("laminar/B738/button/fmc2_minus", "FMC2 capt -", B738_fmc2_minus_CMDhandler)
B738CMD_fmc2_slash = create_command("laminar/B738/button/fmc2_slash", "FMC2 capt SLASH", B738_fmc2_slash_CMDhandler)

B738CMD_fmc2_A = create_command("laminar/B738/button/fmc2_A", "FMC2 capt A", B738_fmc2_A_CMDhandler)
B738CMD_fmc2_B = create_command("laminar/B738/button/fmc2_B", "FMC2 capt B", B738_fmc2_B_CMDhandler)
B738CMD_fmc2_C = create_command("laminar/B738/button/fmc2_C", "FMC2 capt C", B738_fmc2_C_CMDhandler)
B738CMD_fmc2_D = create_command("laminar/B738/button/fmc2_D", "FMC2 capt D", B738_fmc2_D_CMDhandler)
B738CMD_fmc2_E = create_command("laminar/B738/button/fmc2_E", "FMC2 capt E", B738_fmc2_E_CMDhandler)
B738CMD_fmc2_F = create_command("laminar/B738/button/fmc2_F", "FMC2 capt F", B738_fmc2_F_CMDhandler)
B738CMD_fmc2_G = create_command("laminar/B738/button/fmc2_G", "FMC2 capt G", B738_fmc2_G_CMDhandler)
B738CMD_fmc2_H = create_command("laminar/B738/button/fmc2_H", "FMC2 capt H", B738_fmc2_H_CMDhandler)
B738CMD_fmc2_I = create_command("laminar/B738/button/fmc2_I", "FMC2 capt I", B738_fmc2_I_CMDhandler)
B738CMD_fmc2_J = create_command("laminar/B738/button/fmc2_J", "FMC2 capt J", B738_fmc2_J_CMDhandler)
B738CMD_fmc2_K = create_command("laminar/B738/button/fmc2_K", "FMC2 capt K", B738_fmc2_K_CMDhandler)
B738CMD_fmc2_L = create_command("laminar/B738/button/fmc2_L", "FMC2 capt L", B738_fmc2_L_CMDhandler)
B738CMD_fmc2_M = create_command("laminar/B738/button/fmc2_M", "FMC2 capt M", B738_fmc2_M_CMDhandler)
B738CMD_fmc2_N = create_command("laminar/B738/button/fmc2_N", "FMC2 capt N", B738_fmc2_N_CMDhandler)
B738CMD_fmc2_O = create_command("laminar/B738/button/fmc2_O", "FMC2 capt O", B738_fmc2_O_CMDhandler)
B738CMD_fmc2_P = create_command("laminar/B738/button/fmc2_P", "FMC2 capt P", B738_fmc2_P_CMDhandler)
B738CMD_fmc2_Q = create_command("laminar/B738/button/fmc2_Q", "FMC2 capt Q", B738_fmc2_Q_CMDhandler)
B738CMD_fmc2_R = create_command("laminar/B738/button/fmc2_R", "FMC2 capt R", B738_fmc2_R_CMDhandler)
B738CMD_fmc2_S = create_command("laminar/B738/button/fmc2_S", "FMC2 capt S", B738_fmc2_S_CMDhandler)
B738CMD_fmc2_T = create_command("laminar/B738/button/fmc2_T", "FMC2 capt T", B738_fmc2_T_CMDhandler)
B738CMD_fmc2_U = create_command("laminar/B738/button/fmc2_U", "FMC2 capt U", B738_fmc2_U_CMDhandler)
B738CMD_fmc2_V = create_command("laminar/B738/button/fmc2_V", "FMC2 capt V", B738_fmc2_V_CMDhandler)
B738CMD_fmc2_W = create_command("laminar/B738/button/fmc2_W", "FMC2 capt W", B738_fmc2_W_CMDhandler)
B738CMD_fmc2_X = create_command("laminar/B738/button/fmc2_X", "FMC2 capt X", B738_fmc2_X_CMDhandler)
B738CMD_fmc2_Y = create_command("laminar/B738/button/fmc2_Y", "FMC2 capt Y", B738_fmc2_Y_CMDhandler)
B738CMD_fmc2_Z = create_command("laminar/B738/button/fmc2_Z", "FMC2 capt Z", B738_fmc2_Z_CMDhandler)
B738CMD_fmc2_SP = create_command("laminar/B738/button/fmc2_SP", "FMC2 capt Z", B738_fmc2_SP_CMDhandler)



B738CMD_fmc2_clr = create_command("laminar/B738/button/fmc2_clr", "FMC2 capt CLR", B738_fmc2_clr_CMDhandler)
B738CMD_fmc2_del = create_command("laminar/B738/button/fmc2_del", "FMC2 capt DEL", B738_fmc2_del_CMDhandler)

B738CMD_fmc2_prev_page = create_command("laminar/B738/button/fmc2_prev_page", "FMC2 capt PREV PAGE", B738_fmc2_prev_page_CMDhandler)
B738CMD_fmc2_next_page = create_command("laminar/B738/button/fmc2_next_page", "FMC2 capt NEXT PAGE", B738_fmc2_next_page_CMDhandler)

B738CMD_fmc2_init_ref = create_command("laminar/B738/button/fmc2_init_ref", "FMC2 capt INIT REF", B738_fmc2_init_ref_CMDhandler)
B738CMD_fmc2_menu = create_command("laminar/B738/button/fmc2_menu", "FMC2 capt MENU", B738_fmc2_menu_CMDhandler)
B738CMD_fmc2_n1_lim = create_command("laminar/B738/button/fmc2_n1_lim", "FMC2 capt N1 LIMIT", B738_fmc2_n1_lim_CMDhandler)
B738CMD_fmc2_rte = create_command("laminar/B738/button/fmc2_rte", "FMC2 capt RTE", B738_fmc2_rte_CMDhandler)
B738CMD_fmc2_legs = create_command("laminar/B738/button/fmc2_legs", "FMC2 capt LEGS", B738_fmc2_legs_CMDhandler)
B738CMD_fmc2_fix = create_command("laminar/B738/button/fmc2_fix", "FMC2 capt FIX", B738_fmc2_fix_CMDhandler)
B738CMD_fmc2_clb = create_command("laminar/B738/button/fmc2_clb", "FMC2 capt CLB", B738_fmc2_clb_CMDhandler)
B738CMD_fmc2_crz = create_command("laminar/B738/button/fmc2_crz", "FMC2 capt CRZ", B738_fmc2_crz_CMDhandler)
B738CMD_fmc2_des = create_command("laminar/B738/button/fmc2_des", "FMC2 capt DES", B738_fmc2_des_CMDhandler)
B738CMD_fmc2_dep_app = create_command("laminar/B738/button/fmc2_dep_app", "FMC2 capt DEP/APP", B738_fmc2_dep_app_CMDhandler)
B738CMD_fmc2_hold = create_command("laminar/B738/button/fmc2_hold", "FMC2 capt HOLD", B738_fmc2_hold_CMDhandler)
B738CMD_fmc2_prog = create_command("laminar/B738/button/fmc2_prog", "FMC2 capt PROG", B738_fmc2_prog_CMDhandler)
B738CMD_fmc2_exec = create_command("laminar/B738/button/fmc2_exec", "FMC2 capt EXEC", B738_fmc2_exec_CMDhandler)

B738CMD_fmc_reset = create_command("laminar/B738/button/reset_fmc", "Reset FMC", B738_fmc_reset_CMDhandler)


B738CMD_autopilot_alt_interv		= create_command("laminar/B738/autopilot/alt_interv", "ALT intervention", B738_autopilot_alt_interv_CMDhandler)

B738CMD_nosewheel_yaw		= create_command("laminar/B738/control/nosewheel_yaw", "Yaw axis controls nosewheel steering", B738_nosewheel_yaw_CMDhandler)
B738CMD_nosewheel_roll		= create_command("laminar/B738/control/nosewheel_roll", "Roll axis controls nosewheel steering", B738_nosewheel_roll_CMDhandler)
B738CMD_nosewheel_vr		= create_command("laminar/B738/control/nosewheel_vr", "VR controls nosewheel steering", B738_nosewheel_vr_CMDhandler)

B738CMD_toe_brake_axis_switch = create_command("laminar/B738/control/toe_brake_axis_switch", "Toe brakes different/symetrical", B738_toe_brake_axis_switch_CMDhandler)

B738CMD_change_units 		= create_command("laminar/B738/fms/change_units", "Change Units", B738_change_units_CMDhandler)
B738CMD_change_payload		= create_command("laminar/B738/fms/change_payload", "Change Payload", B738_change_payload_CMDhandler)
B738CMD_update_factory		= create_command("laminar/B738/fms/update_factory", "Update Factory preset", B738_update_factory_CMDhandler)

B738CMD_save_config 		= create_command("laminar/B738/fms/save_config_audio", "Save config/audio", B738_save_config_CMDhandler)
B738CMD_reset_cfg_audio 	= create_command("laminar/B738/fms/reset_config_audio", "Reset config/audio", B738_reset_cfg_audio_CMDhandler)
B738CMD_reset_cfg 			= create_command("laminar/B738/fms/reset_config", "Reset config", B738_reset_cfg_CMDhandler)
B738CMD_reset_audio 		= create_command("laminar/B738/fms/reset_audio", "Reset audio", B738_reset_audio_CMDhandler)

B738CMD_load_cfg1 			= create_command("laminar/B738/fms/load_cfg1", "Load config preset 1", B738_load_cfg1_CMDhandler)
B738CMD_load_cfg2 			= create_command("laminar/B738/fms/load_cfg2", "Load config preset 2", B738_load_cfg2_CMDhandler)
B738CMD_load_cfg3 			= create_command("laminar/B738/fms/load_cfg3", "Load config preset 3", B738_load_cfg3_CMDhandler)
B738CMD_load_cfg4 			= create_command("laminar/B738/fms/load_cfg4", "Load config preset 4", B738_load_cfg4_CMDhandler)
B738CMD_save_cfg1 			= create_command("laminar/B738/fms/save_cfg1", "Save config preset 1", B738_save_cfg1_CMDhandler)
B738CMD_save_cfg2 			= create_command("laminar/B738/fms/save_cfg2", "Save config preset 2", B738_save_cfg2_CMDhandler)
B738CMD_save_cfg3 			= create_command("laminar/B738/fms/save_cfg3", "Save config preset 3", B738_save_cfg3_CMDhandler)
B738CMD_save_cfg4 			= create_command("laminar/B738/fms/save_cfg4", "Save config preset 4", B738_save_cfg4_CMDhandler)

B738CMD_load_audio1 		= create_command("laminar/B738/fms/load_audio1", "Load config preset 1", B738_load_audio1_CMDhandler)
B738CMD_load_audio2 		= create_command("laminar/B738/fms/load_audio2", "Load config preset 2", B738_load_audio2_CMDhandler)
B738CMD_load_audio3 		= create_command("laminar/B738/fms/load_audio3", "Load config preset 3", B738_load_audio3_CMDhandler)
B738CMD_load_audio4 		= create_command("laminar/B738/fms/load_audio4", "Load config preset 4", B738_load_audio4_CMDhandler)
B738CMD_save_audio1 		= create_command("laminar/B738/fms/save_audio1", "Save config preset 1", B738_save_audio1_CMDhandler)
B738CMD_save_audio2 		= create_command("laminar/B738/fms/save_audio2", "Save config preset 2", B738_save_audio2_CMDhandler)
B738CMD_save_audio3 		= create_command("laminar/B738/fms/save_audio3", "Save config preset 3", B738_save_audio3_CMDhandler)
B738CMD_save_audio4 		= create_command("laminar/B738/fms/save_audio4", "Save config preset 4", B738_save_audio4_CMDhandler)

B738CMD_load_cfg99 			= create_command("laminar/B738/fms/load_cfg99", "Load config preset 1", B738_load_cfg99_CMDhandler)
B738CMD_msg_not_intcpt		= create_command("laminar/B738/fms/msg_not_intcpt", "Internal", B738_msg_not_intcpt_CMDhandler)

B738CMD_set_flap_up = create_command("laminar/B738/fms/set_flap_up", "Set detent flap UP", B738_set_flap_up_CMDhandler)
B738CMD_set_flap_1 = create_command("laminar/B738/fms/set_flap_1", "Set detent flap 1", B738_set_flap_1_CMDhandler)
B738CMD_set_flap_2 = create_command("laminar/B738/fms/set_flap_2", "Set detent flap 2", B738_set_flap_2_CMDhandler)
B738CMD_set_flap_5 = create_command("laminar/B738/fms/set_flap_5", "Set detent flap 5", B738_set_flap_5_CMDhandler)
B738CMD_set_flap_10 = create_command("laminar/B738/fms/set_flap_10", "Set detent flap 10", B738_set_flap_10_CMDhandler)
B738CMD_set_flap_15 = create_command("laminar/B738/fms/set_flap_15", "Set detent flap 15", B738_set_flap_15_CMDhandler)
B738CMD_set_flap_25 = create_command("laminar/B738/fms/set_flap_25", "Set detent flap 25", B738_set_flap_25_CMDhandler)
B738CMD_set_flap_30 = create_command("laminar/B738/fms/set_flap_30", "Set detent flap 30", B738_set_flap_30_CMDhandler)
B738CMD_set_flap_40 = create_command("laminar/B738/fms/set_flap_40", "Set detent flap 40", B738_set_flap_40_CMDhandler)


--*************************************************************************************--
--** 				              CREATE CUSTOM COMMANDS              			     **--
--*************************************************************************************--

-- MMR (Multi mode receiver)

function B738_mmr1_act_stby_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		--if B738DR_mmr_cpt_stby_cursor == 0 then
		if B738DR_mmr_cpt_stby_cursor <= 2 then
			
			local mode_status = B738DR_mmr_cpt_act_mode
			local mmr_txt = string.format("%05d", B738DR_mmr_cpt_act_value)
			
			-- copy value from standby to active
			local mmr_txt2 = ""
			for mmr_ii = 4, 0, -1 do
				if mmr_ii < B738DR_mmr_cpt_stby_cursor then
					B738DR_mmr_cpt_stby_value[mmr_ii] = 0
				end
				mmr_txt2 = mmr_txt2 .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
			end
			B738DR_mmr_cpt_stby_cursor = 0
			local mmr_freq = tonumber(mmr_txt2)
			
			local mmr_err = 0
			if B738DR_mmr_cpt_stby_mode == 0 and vor_or_ils(mmr_freq) ~= 0 then
				mmr_err = 1
			elseif B738DR_mmr_cpt_stby_mode == 1 and vor_or_ils(mmr_freq) ~= 1 then
				mmr_err = 1
			end
			B738DR_mmr_cpt_err = mmr_err
			
			if mmr_err == 0 then
				B738DR_mmr_cpt_act_value = mmr_freq
				B738DR_mmr_cpt_act_mode = B738DR_mmr_cpt_stby_mode
				B738DR_mmr_cpt_stby_mode = mode_status
				-- copy value from active to standby
				for mmr_ii = 0, 4 do
					B738DR_mmr_cpt_stby_value[mmr_ii] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
					if B738DR_mmr_cpt_stby_mode == 0 then
						vor_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
					elseif B738DR_mmr_cpt_stby_mode == 1 then
						ils_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
					else
						gls_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
					end
					if mode_status == 0 then
						vor_cpt_stby_value2[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
					else
						ils_cpt_stby_value2[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
					end
				end
				
				if B738DR_mmr_cpt_act_mode < 2 then
					B738DR_nav1_hz = B738DR_mmr_cpt_act_value
				end
				--if B738DR_mmr_cpt_stby_mode < 2 then
				if mode_status < 2 then
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				else
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
		else
			B738DR_mmr_cpt_err = 1
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_mode_dn_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if B738DR_mmr_cpt_stby_mode == 0 then
			B738DR_mmr_cpt_stby_mode = 2
			for mmr_ii = 0, 4 do
				if B738DR_mmr_cpt_err ~= 0 then
					vor_cpt_stby_value[mmr_ii+1] = vor_cpt_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_cpt_stby_value[mmr_ii] = gls_cpt_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", vor_cpt_stby_value[mmr_ii+1])
			end
			B738DR_nav1_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_cpt_stby_mode == 1 then
			B738DR_mmr_cpt_stby_mode = 0
			for mmr_ii = 0, 4 do
				if B738DR_mmr_cpt_err ~= 0 then
					ils_cpt_stby_value[mmr_ii+1] = ils_cpt_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_cpt_stby_value[mmr_ii] = vor_cpt_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", vor_cpt_stby_value[mmr_ii+1])
			end
			B738DR_nav1_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_cpt_stby_mode == 2 then
			B738DR_mmr_cpt_stby_mode = 1
			for mmr_ii = 0, 4 do
				B738DR_mmr_cpt_stby_value[mmr_ii] = ils_cpt_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", ils_cpt_stby_value[mmr_ii+1])
			end
			B738DR_nav1_stby_hz = tonumber(mmr_txt)
		end
		B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
		B738DR_mmr_cpt_err = 0
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_mode_up_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if B738DR_mmr_cpt_stby_mode == 0 then
			B738DR_mmr_cpt_stby_mode = 1
			for mmr_ii = 0, 4 do
				if B738DR_mmr_cpt_err ~= 0 then
					vor_cpt_stby_value[mmr_ii+1] = vor_cpt_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_cpt_stby_value[mmr_ii] = ils_cpt_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", ils_cpt_stby_value[mmr_ii+1])
			end
			B738DR_nav1_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_cpt_stby_mode == 1 then
			B738DR_mmr_cpt_stby_mode = 2
			for mmr_ii = 0, 4 do
				if B738DR_mmr_cpt_err ~= 0 then
					ils_cpt_stby_value[mmr_ii+1] = ils_cpt_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_cpt_stby_value[mmr_ii] = gls_cpt_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", ils_cpt_stby_value[mmr_ii+1])
			end
			B738DR_nav1_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_cpt_stby_mode == 2 then
			B738DR_mmr_cpt_stby_mode = 0
			for mmr_ii = 0, 4 do
				B738DR_mmr_cpt_stby_value[mmr_ii] = vor_cpt_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", vor_cpt_stby_value[mmr_ii+1])
			end
			B738DR_nav1_stby_hz = tonumber(mmr_txt)
		end
		B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
		B738DR_mmr_cpt_err = 0
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function mmr1_clear_stby()
		for mmr_ii = 0, 4 do
			B738DR_mmr_cpt_stby_value[mmr_ii] = 0
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
			else
				gls_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
			end
		end
		B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = 5
		B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
end

function B738_mmr1_clr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if B738DR_mmr_cpt_stby_cursor ~= 5 then
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] + 1
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 1 and duration > 1 then
		if B738DR_mmr_cpt_stby_cursor ~= 5 then
			mmr1_clear_stby()
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function mmr_cursor_block(mmr_key)
	local result = false
	
	--if B738DR_mmr_cpt_stby_cursor > 0 then
		if B738DR_mmr_cpt_stby_mode < 2 then
			-- VOR/ILS -> 108.00 .. 117.95 (step 0.05)
			if B738DR_mmr_cpt_stby_cursor == 0 or B738DR_mmr_cpt_stby_cursor == 5 then
				if mmr_key == 1 then
					result = true
				end
			elseif B738DR_mmr_cpt_stby_cursor == 4 then
				if mmr_key >= 0 and mmr_key <= 1 then
					result = true
				end
			elseif B738DR_mmr_cpt_stby_cursor == 3 then
				if B738DR_mmr_cpt_stby_value[4] == 1 and B738DR_mmr_cpt_stby_value[3] == 0 then
					if mmr_key >= 8 and mmr_key <= 9 then
						result = true
					end
				else
					if mmr_key >= 0 and mmr_key <= 7 then
						result = true
					end
				end
			elseif B738DR_mmr_cpt_stby_cursor == 1 then
				if mmr_key == 0 or mmr_key == 5 then
					result = true
				end
			else
				result = true
			end
		else
			-- GLS -> 20000 .. 99999
			if B738DR_mmr_cpt_stby_cursor == 0 or B738DR_mmr_cpt_stby_cursor == 5 then
				if mmr_key >= 2 then
					result = true
				end
			else
				result = true
			end
		end
	--end
	
	return result
end

function B738_mmr1_0_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(0) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 0
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_1_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(1) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 1
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_2_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(2) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 2
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_3_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(3) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 3
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_4_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(4) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 4
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_5_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(5) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 5
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_6_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(6) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 6
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_7_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(7) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 7
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_8_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(8) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 8
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_9_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block(9) then
			if B738DR_mmr_cpt_stby_cursor == 0 then
				mmr1_clear_stby()
			end
			B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] - 1
			B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]] = 9
			B738DR_mmr_cpt_stby_cursor = B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]
			
			if B738DR_mmr_cpt_stby_mode == 0 then
				vor_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			elseif B738DR_mmr_cpt_stby_mode == 1 then
				ils_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			else
				gls_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]+1] = B738DR_mmr_cpt_stby_value[B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode]]
			end
			if B738DR_mmr_cpt_stby_mode < 2 then
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_nav1_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_cpt_stby_cur[B738DR_mmr_cpt_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_cpt_stby_value[mmr_ii])
					end
					B738DR_mmr_cpt_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_cpt_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr1_test_CMDhandler(phase, duration)
end

B738CMD_mmr1_act_stby 	= create_command("laminar/B738/push_button/mmr1_act_stby", "MMR1 active/standby", B738_mmr1_act_stby_CMDhandler)
B738CMD_mmr1_mode_dn 	= create_command("laminar/B738/push_button/mmr1_mode_dn", "MMR1 Mode down", B738_mmr1_mode_dn_CMDhandler)
B738CMD_mmr1_mode_up 	= create_command("laminar/B738/push_button/mmr1_mode_up", "MMR1 Mode down", B738_mmr1_mode_up_CMDhandler)
B738CMD_mmr1_clr	 	= create_command("laminar/B738/push_button/mmr1_clr", "MMR1 CLR key", B738_mmr1_clr_CMDhandler)
B738CMD_mmr1_0		 	= create_command("laminar/B738/push_button/mmr1_0", "MMR1 0 key", B738_mmr1_0_CMDhandler)
B738CMD_mmr1_1		 	= create_command("laminar/B738/push_button/mmr1_1", "MMR1 1 key", B738_mmr1_1_CMDhandler)
B738CMD_mmr1_2		 	= create_command("laminar/B738/push_button/mmr1_2", "MMR1 2 key", B738_mmr1_2_CMDhandler)
B738CMD_mmr1_3		 	= create_command("laminar/B738/push_button/mmr1_3", "MMR1 3 key", B738_mmr1_3_CMDhandler)
B738CMD_mmr1_4		 	= create_command("laminar/B738/push_button/mmr1_4", "MMR1 4 key", B738_mmr1_4_CMDhandler)
B738CMD_mmr1_5		 	= create_command("laminar/B738/push_button/mmr1_5", "MMR1 5 key", B738_mmr1_5_CMDhandler)
B738CMD_mmr1_6		 	= create_command("laminar/B738/push_button/mmr1_6", "MMR1 6 key", B738_mmr1_6_CMDhandler)
B738CMD_mmr1_7		 	= create_command("laminar/B738/push_button/mmr1_7", "MMR1 7 key", B738_mmr1_7_CMDhandler)
B738CMD_mmr1_8		 	= create_command("laminar/B738/push_button/mmr1_8", "MMR1 8 key", B738_mmr1_8_CMDhandler)
B738CMD_mmr1_9		 	= create_command("laminar/B738/push_button/mmr1_9", "MMR1 9 key", B738_mmr1_9_CMDhandler)
B738CMD_mmr1_test	 	= create_command("laminar/B738/push_button/mmr1_test", "MMR1 Test key", B738_mmr1_test_CMDhandler)


function B738_mmr2_act_stby_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 0
		--if B738DR_mmr_fo_stby_cursor == 0 then
		if B738DR_mmr_fo_stby_cursor <= 2 then
			
			local mode_status = B738DR_mmr_fo_act_mode
			local mmr_txt = string.format("%05d", B738DR_mmr_fo_act_value)
			
			-- copy value from standby to active
			local mmr_txt2 = ""
			for mmr_ii = 4, 0, -1 do
				if mmr_ii < B738DR_mmr_fo_stby_cursor then
					B738DR_mmr_fo_stby_value[mmr_ii] = 0
				end
				mmr_txt2 = mmr_txt2 .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
			end
			B738DR_mmr_fo_stby_cursor = 0
			local mmr_freq = tonumber(mmr_txt2)
			
			local mmr_err = 0
			if B738DR_mmr_fo_stby_mode == 0 and vor_or_ils(mmr_freq) ~= 0 then
				mmr_err = 1
			elseif B738DR_mmr_fo_stby_mode == 1 and vor_or_ils(mmr_freq) ~= 1 then
				mmr_err = 1
			end
			B738DR_mmr_fo_err = mmr_err
			
			if mmr_err == 0 then
				B738DR_mmr_fo_act_value = mmr_freq
				B738DR_mmr_fo_act_mode = B738DR_mmr_fo_stby_mode
				B738DR_mmr_fo_stby_mode = mode_status
				
				-- copy value from active to standby
				for mmr_ii = 0, 4 do
					B738DR_mmr_fo_stby_value[mmr_ii] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
					if B738DR_mmr_fo_stby_mode == 0 then
						vor_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
					elseif B738DR_mmr_fo_stby_mode == 1 then
						ils_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
					else
						gls_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
					end
					if mode_status == 0 then
						vor_fo_stby_value2[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
					else
						ils_fo_stby_value2[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
					end
				end
				
				if B738DR_mmr_fo_act_mode < 2 then
					B738DR_nav2_hz = B738DR_mmr_fo_act_value
				end
				--if B738DR_mmr_fo_stby_mode < 2 then
				if mode_status < 2 then
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				else
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
		else
			B738DR_mmr_fo_err = 1
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_mode_dn_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if B738DR_mmr_fo_stby_mode == 0 then
			B738DR_mmr_fo_stby_mode = 2
			for mmr_ii = 0, 4 do
				if B738DR_mmr_fo_err ~= 0 then
					vor_fo_stby_value[mmr_ii+1] = vor_fo_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_fo_stby_value[mmr_ii] = gls_fo_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", vor_fo_stby_value[mmr_ii+1])
			end
			B738DR_nav2_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_fo_stby_mode == 1 then
			B738DR_mmr_fo_stby_mode = 0
			for mmr_ii = 0, 4 do
				if B738DR_mmr_fo_err ~= 0 then
					ils_fo_stby_value[mmr_ii+1] = ils_fo_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_fo_stby_value[mmr_ii] = vor_fo_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", vor_fo_stby_value[mmr_ii+1])
			end
			B738DR_nav2_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_fo_stby_mode == 2 then
			B738DR_mmr_fo_stby_mode = 1
			for mmr_ii = 0, 4 do
				B738DR_mmr_fo_stby_value[mmr_ii] = ils_fo_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", ils_fo_stby_value[mmr_ii+1])
			end
			B738DR_nav2_stby_hz = tonumber(mmr_txt)
		end
		B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
		B738DR_mmr_fo_err = 0
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_mode_up_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if B738DR_mmr_fo_stby_mode == 0 then
			B738DR_mmr_fo_stby_mode = 1
			for mmr_ii = 0, 4 do
				if B738DR_mmr_fo_err ~= 0 then
					vor_fo_stby_value[mmr_ii+1] = vor_fo_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_fo_stby_value[mmr_ii] = ils_fo_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", ils_fo_stby_value[mmr_ii+1])
			end
			B738DR_nav2_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_fo_stby_mode == 1 then
			B738DR_mmr_fo_stby_mode = 2
			for mmr_ii = 0, 4 do
				if B738DR_mmr_fo_err ~= 0 then
					ils_fo_stby_value[mmr_ii+1] = ils_fo_stby_value2[mmr_ii+1]
				end
				B738DR_mmr_fo_stby_value[mmr_ii] = gls_fo_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", ils_fo_stby_value[mmr_ii+1])
			end
			B738DR_nav2_stby_hz = tonumber(mmr_txt)
		elseif B738DR_mmr_fo_stby_mode == 2 then
			B738DR_mmr_fo_stby_mode = 0
			for mmr_ii = 0, 4 do
				B738DR_mmr_fo_stby_value[mmr_ii] = vor_fo_stby_value[mmr_ii+1]
			end
			local mmr_txt = ""
			for mmr_ii = 4, 0, -1 do
				mmr_txt = mmr_txt .. string.format("%01d", vor_fo_stby_value[mmr_ii+1])
			end
			B738DR_nav2_stby_hz = tonumber(mmr_txt)
		end
		B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
		B738DR_mmr_fo_err = 0
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function mmr2_clear_stby()
	for mmr_ii = 0, 4 do
		B738DR_mmr_fo_stby_value[mmr_ii] = 0
		if B738DR_mmr_fo_stby_mode == 0 then
			vor_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
		elseif B738DR_mmr_fo_stby_mode == 1 then
			ils_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
		else
			gls_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
		end
	end
	B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = 5
	B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
end

function B738_mmr2_clr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if B738DR_mmr_fo_stby_cursor ~= 5 then
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] + 1
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 1 and duration > 1 then
		if B738DR_mmr_fo_stby_cursor ~= 5 then
			mmr2_clear_stby()
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function mmr_cursor_block2(mmr_key)
	local result = false
	
	--if B738DR_mmr_fo_stby_cursor > 0 then
		if B738DR_mmr_fo_stby_mode < 2 then
			-- VOR/ILS -> 108.00 .. 117.95 (step 0.05)
			if B738DR_mmr_fo_stby_cursor == 0 or B738DR_mmr_fo_stby_cursor == 5 then
				if mmr_key == 1 then
					result = true
				end
			elseif B738DR_mmr_fo_stby_cursor == 4 then
				if mmr_key >= 0 and mmr_key <= 1 then
					result = true
				end
			elseif B738DR_mmr_fo_stby_cursor == 3 then
				if B738DR_mmr_fo_stby_value[4] == 1 and B738DR_mmr_fo_stby_value[3] == 0 then
					if mmr_key >= 8 and mmr_key <= 9 then
						result = true
					end
				else
					if mmr_key >= 0 and mmr_key <= 7 then
						result = true
					end
				end
			elseif B738DR_mmr_fo_stby_cursor == 1 then
				if mmr_key == 0 or mmr_key == 5 then
					result = true
				end
			else
				result = true
			end
		else
			-- GLS -> 20000 .. 99999
			if B738DR_mmr_fo_stby_cursor == 0 or B738DR_mmr_fo_stby_cursor == 5 then
				if mmr_key >= 2 then
					result = true
				end
			else
				result = true
			end
		end
	--end
	
	return result
end

function B738_mmr2_0_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(0) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 0
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_1_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(1) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 1
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_2_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(2) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 2
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_3_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(3) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 3
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_4_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(4) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 4
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_5_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(5) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 5
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_6_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(6) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 6
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_7_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(7) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 7
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_8_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(8) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 8
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_9_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fmod_mmr = 1
		if mmr_cursor_block2(9) then
			if B738DR_mmr_fo_stby_cursor == 0 then
				mmr2_clear_stby()
			end
			B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] - 1
			B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]] = 9
			B738DR_mmr_fo_stby_cursor = B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]
			
			if B738DR_mmr_fo_stby_mode == 0 then
				vor_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			elseif B738DR_mmr_fo_stby_mode == 1 then
				ils_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			else
				gls_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]+1] = B738DR_mmr_fo_stby_value[B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode]]
			end
			if B738DR_mmr_fo_stby_mode < 2 then
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_nav2_stby_hz = tonumber(mmr_txt)
				end
			else
				if B738DR_mmr_fo_stby_cur[B738DR_mmr_fo_stby_mode] == 0 then
					local mmr_txt = ""
					for mmr_ii = 4, 0, -1 do
						mmr_txt = mmr_txt .. string.format("%01d", B738DR_mmr_fo_stby_value[mmr_ii])
					end
					B738DR_mmr_fo_stby_gls = tonumber(mmr_txt)
				end
			end
			B738DR_mmr_fo_err = 0
		end
	elseif phase == 2 then
		B738DR_fmod_mmr = 0
	end
end

function B738_mmr2_test_CMDhandler(phase, duration)
end

B738CMD_mmr2_act_stby 	= create_command("laminar/B738/push_button/mmr2_act_stby", "MMR2 active/standby", B738_mmr2_act_stby_CMDhandler)
B738CMD_mmr2_mode_dn 	= create_command("laminar/B738/push_button/mmr2_mode_dn", "MMR2 Mode down", B738_mmr2_mode_dn_CMDhandler)
B738CMD_mmr2_mode_up 	= create_command("laminar/B738/push_button/mmr2_mode_up", "MMR2 Mode down", B738_mmr2_mode_up_CMDhandler)
B738CMD_mmr2_clr	 	= create_command("laminar/B738/push_button/mmr2_clr", "MMR2 CLR key", B738_mmr2_clr_CMDhandler)
B738CMD_mmr2_0		 	= create_command("laminar/B738/push_button/mmr2_0", "MMR2 0 key", B738_mmr2_0_CMDhandler)
B738CMD_mmr2_1		 	= create_command("laminar/B738/push_button/mmr2_1", "MMR2 1 key", B738_mmr2_1_CMDhandler)
B738CMD_mmr2_2		 	= create_command("laminar/B738/push_button/mmr2_2", "MMR2 2 key", B738_mmr2_2_CMDhandler)
B738CMD_mmr2_3		 	= create_command("laminar/B738/push_button/mmr2_3", "MMR2 3 key", B738_mmr2_3_CMDhandler)
B738CMD_mmr2_4		 	= create_command("laminar/B738/push_button/mmr2_4", "MMR2 4 key", B738_mmr2_4_CMDhandler)
B738CMD_mmr2_5		 	= create_command("laminar/B738/push_button/mmr2_5", "MMR2 5 key", B738_mmr2_5_CMDhandler)
B738CMD_mmr2_6		 	= create_command("laminar/B738/push_button/mmr2_6", "MMR2 6 key", B738_mmr2_6_CMDhandler)
B738CMD_mmr2_7		 	= create_command("laminar/B738/push_button/mmr2_7", "MMR2 7 key", B738_mmr2_7_CMDhandler)
B738CMD_mmr2_8		 	= create_command("laminar/B738/push_button/mmr2_8", "MMR2 8 key", B738_mmr2_8_CMDhandler)
B738CMD_mmr2_9		 	= create_command("laminar/B738/push_button/mmr2_9", "MMR2 9 key", B738_mmr2_9_CMDhandler)
B738CMD_mmr2_test	 	= create_command("laminar/B738/push_button/mmr2_test", "MMR2 Test key", B738_mmr2_test_CMDhandler)

--*************************************************************************************--
--** 					            OBJECT CONSTRUCTORS         		    		 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				               CREATE SYSTEM OBJECTS            				 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				                  SYSTEM FUNCTIONS           	    			 **--
--*************************************************************************************--

----- ANIMATION UTILITY -----------------------------------------------------------------
function B738_set_animation_position(current_value, target, min, max, speed)

    if target >= (max - 0.001) and current_value >= (max - 0.01) then
        return max
    elseif target <= (min + 0.001) and current_value <= (min + 0.01) then
        return min
    else
        return current_value + ((target - current_value) * (speed * SIM_PERIOD))
    end

end

------------------------------------------


----------------------------------------------------------------------------------------------------



function B738_fmc_menu()

	if page_menu == 1 then
		act_page_buf = 1
		max_page_buf = 1
		line0_l = "         MENU           "
		line1_l = "<FMC    <ACT>           "
		-- line2_l = "<ACARS                  "
		line2_l = "<DLNK                   "
	end

end

function B738_fmc_flt_plns(in_mode)
	
	if page_flt_plns == 1 then
		
		max_page_buf = math.floor((flt_plns_data_num - 1) / 5) + 1;
		
		line0_l = "FLT PLAN SUMMARY        "
		if max_page_buf < 10 then
			line0_s = "                   " .. string.format("%2d",act_page_buf)
			line0_s = line0_s .. "/"
			line0_s = line0_s .. string.format("%1d",max_page_buf)
		else
			line0_s = "                  " .. string.format("%2d",act_page_buf)
			line0_s = line0_s .. "/"
			line0_s = line0_s .. string.format("%2d",max_page_buf)
		end
		
		local flt_offset = math.floor((act_page_buf - 1) * 5) + 1;
		
		if in_mode == 0 then
			if flt_plns_data_num > 0 then
				line1_x = " FLIGHT PLAN NAV DATA   "
				if flt_offset <= flt_plns_data_num then
					line1_l = flt_plns_data[flt_offset]
					sel_flt_plns[1] = flt_plns_data[flt_offset]
				else
					sel_flt_plns[1] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line2_l = flt_plns_data[flt_offset]
					sel_flt_plns[2] = flt_plns_data[flt_offset]
				else
					sel_flt_plns[2] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line3_l = flt_plns_data[flt_offset]
					sel_flt_plns[3] = flt_plns_data[flt_offset]
				else
					sel_flt_plns[3] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line4_l = flt_plns_data[flt_offset]
					sel_flt_plns[4] = flt_plns_data[flt_offset]
				else
					sel_flt_plns[4] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line5_l = flt_plns_data[flt_offset]
					sel_flt_plns[5] = flt_plns_data[flt_offset]
				else
					sel_flt_plns[5] = ""
				end
			else
				sel_flt_plns[1] = ""
				sel_flt_plns[2] = ""
				sel_flt_plns[3] = ""
				sel_flt_plns[4] = ""
				sel_flt_plns[5] = ""
			end
		else
			if flt_plns_data_num > 0 then
				line1_x = " FLIGHT PLAN NAV DATA   "
				if flt_offset <= flt_plns_data_num then
					line1_l = flt_plns_data[flt_offset]
					sel_flt_plns2[1] = flt_plns_data[flt_offset]
				else
					sel_flt_plns2[1] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line2_l = flt_plns_data[flt_offset]
					sel_flt_plns2[2] = flt_plns_data[flt_offset]
				else
					sel_flt_plns2[2] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line3_l = flt_plns_data[flt_offset]
					sel_flt_plns2[3] = flt_plns_data[flt_offset]
				else
					sel_flt_plns2[3] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line4_l = flt_plns_data[flt_offset]
					sel_flt_plns2[4] = flt_plns_data[flt_offset]
				else
					sel_flt_plns2[4] = ""
				end
				
				flt_offset = flt_offset + 1
				if flt_offset <= flt_plns_data_num then
					line5_l = flt_plns_data[flt_offset]
					sel_flt_plns2[5] = flt_plns_data[flt_offset]
				else
					sel_flt_plns2[5] = ""
				end
			else
				sel_flt_plns2[1] = ""
				sel_flt_plns2[2] = ""
				sel_flt_plns2[3] = ""
				sel_flt_plns2[4] = ""
				sel_flt_plns2[5] = ""
			end
		end
		
		line6_x = "------------------------"
		line6_l = "<INDEX"
	end
end

function create_flt_plns()
	B738DR_filename_list_cmd = 1
	flt_plns_data_num = 0
	flt_plns_data = {}
	if B738DR_filename_list_n > 0 then
		local temp_str1 = ""
		local temp_str2 = ""
		local valid_data = false
		for ii = 0, 4095 do
			temp_str1 = string.sub(B738DR_filename_list, ii, ii)
			if temp_str1 ~= "," then
				temp_str2 = temp_str2 .. temp_str1
			else
				if string.len(temp_str2) > 0 and string.len(temp_str2) < 13 then
					flt_plns_data_num = flt_plns_data_num + 1
					flt_plns_data[flt_plns_data_num] = temp_str2
				end
				temp_str2 = ""
			end
			if flt_plns_data_num >= B738DR_filename_list_n then
				break
			end
		end
	end
end

function B738_fmc_ref_nav_data()

	if page_ref_nav_data == 1 then
		act_page_buf = 1
		max_page_buf = 1
		
		line0_l = "   REF NAV DATA         "
		line1_x = " WPT IDENT  NAVAID IDENT"
		line1_l = spaces_after(ref_nav_wpt, 5) .. "               "
		line1_l = line1_l .. spaces_before(ref_nav_navaid, 4)
		line2_x = " AIRPORT IDENT          "
		line2_l = spaces_after(ref_nav_apt, 4) .. "            SUMMARY>"
		line3_l = "               FLT PLNS>"
		line6_x = "------------            "
		if ref_nav_wpt == "-----" and ref_nav_navaid == "----" and ref_nav_apt == "----" then
			line6_l = "<INDEX            " .. spaces_before(ref_nav_new, 5) .. ">"
		else
			line6_l = "<INDEX      NAV OPTIONS>"
		end
	end
end

function B738_fmc_ref_nav_data_wpt()

	if page_ref_nav_data_wpt == 1 then
		act_page_buf = 1
		max_page_buf = 1
		
		local pos_str = ""
		local ii = 0
		
		line0_l = "   REF NAV DATA         "
		line1_x = " WPT IDENT              "
		if ref_nav_wpt == "-----" then
			line1_l = ref_nav_new
		else
			line1_l = ref_nav_wpt
		end
		line3_x = " LATITUDE      LONGITUDE"
		
		if ref_nav_lat == 0 then
			line3_l = "---`--.-       "
		else
			pos_str = string_lat_pos(ref_nav_lat)
			line3_l = string.sub(pos_str, 1, 3) .. "`" .. string.sub(pos_str, 4, 7)
			line3_l = line3_l .. "       "
		end
		if ref_nav_lon == 0 then
			line3_l = line3_l .. "----`--.-"
		else
			pos_str = string_lon_pos(ref_nav_lon)
			line3_l = line3_l .. string.sub(pos_str, 1, 4) .. "`" .. string.sub(pos_str, 5, 8)
		end
		
		line4_x = " MAG VAR                "
		if ref_nav_mag_var == "" then
			line4_l = "----`"
		elseif string.sub(ref_nav_mag_var, 1, 1) == ">" then
			ii = tonumber(string.sub(ref_nav_mag_var, 2, -1))
			if ii == nil then
				line4_l = "   0`"
			else
				if ii == 0 then
					line4_s = "   0`"
				elseif ii < 0 then
					line4_s = "W" .. string.format("%3d", -ii) .. "`"
				else
					line4_s = "E" .. string.format("%3d", ii) .. "`"
				end
			end
		else
			ii = tonumber(ref_nav_mag_var)
			if ii == nil then
				line4_l = "   0`"
			else
				if ii == 0 then
					line4_l = "   0`"
				elseif ii < 0 then
					line4_l = "W" .. string.format("%3d", -ii) .. "`"
				else
					line4_l = "E" .. string.format("%3d", ii) .. "`"
				end
			end
		end
		line5_x = " REF IDENT   RADIAL/DIST"
		line5_l = spaces_after(ref_nav_id, 5) .. "         "
		line5_l = line5_l .. ref_nav_brg .. "`/" .. ref_nav_dist .. "NM"
		line6_x = "------------            "
		line6_l = "<INDEX"
	end
end


function B738_fmc_ref_nav_data_navaid()

	if page_ref_nav_data_navaid == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local pos_str = ""
		local ii = 0
		
		line0_l = "   REF NAV DATA         "
		-- line1_x = " NAVAID IDENT           "
		-- line1_l = ref_nav_navaid
		line1_x =    "            NAVAID IDENT"
		line1_l = string.sub ("                        ".. ref_nav_navaid, -24, -1)
		
		line2_x = "                   CLASS"
		
		line3_x = " LATITUDE      LONGITUDE"
		if ref_nav_lat == 0 then
			line3_l = "---`--.-       "
		else
			pos_str = string_lat_pos(ref_nav_lat)
			line3_l = string.sub(pos_str, 1, 3) .. "`" .. string.sub(pos_str, 4, 7)
			line3_l = line3_l .. "       "
		end
		if ref_nav_lon == 0 then
			line3_l = line3_l .. "----`--.-"
		else
			pos_str = string_lon_pos(ref_nav_lon)
			line3_l = line3_l .. string.sub(pos_str, 1, 4) .. "`" .. string.sub(pos_str, 5, 8)
		end
		
		line4_x = " FREQ/CH       ELEVATION"
		line4_l = ref_nav_freq .. "           " .. string.format("%5d", ref_elevation) .. "FT"
		
		line5_x = " MAG VAR                "
		if ref_nav_mag_var == "" then
			line5_l = "----`"
		elseif string.sub(ref_nav_mag_var, 1, 1) == ">" then
			ii = tonumber(string.sub(ref_nav_mag_var, 2, -1))
			if ii == nil then
				line5_l = "   0`"
			else
				if ii == 0 then
					line5_s = "   0`"
				elseif ii < 0 then
					line5_s = "W" .. string.format("%3d", -ii) .. "`"
				else
					line5_s = "E" .. string.format("%3d", ii) .. "`"
				end
			end
		else
			ii = tonumber(ref_nav_mag_var)
			if ii == nil then
				line5_l = "   0`"
			else
				if ii == 0 then
					line5_l = "   0`"
				elseif ii < 0 then
					line5_l = "W" .. string.format("%3d", -ii) .. "`"
				else
					line5_l = "E" .. string.format("%3d", ii) .. "`"
				end
			end
		end
	end
end

function B738_fmc_ref_nav_data_apt()

	if page_ref_nav_data_apt == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local pos_str = ""
		local ii = 0
		
		line0_l = "   REF NAV DATA         "
		
		line1_x = " RUNWAY IDENT           "
		line1_l = ref_nav_rnw
		
		line2_x = " AIRPORT IDENT          "
		line2_l = ref_nav_apt
		
		line3_x = " LATITUDE      LONGITUDE"
		if ref_nav_lat == 0 then
			line3_l = "---`--.-       "
		else
			pos_str = string_lat_pos(ref_nav_lat)
			line3_l = string.sub(pos_str, 1, 3) .. "`" .. string.sub(pos_str, 4, 7)
			line3_l = line3_l .. "       "
		end
		if ref_nav_lon == 0 then
			line3_l = line3_l .. "----`--.-"
		else
			pos_str = string_lon_pos(ref_nav_lon)
			line3_l = line3_l .. string.sub(pos_str, 1, 4) .. "`" .. string.sub(pos_str, 5, 8)
		end
		
		line4_x = "               ELEVATION"
		line4_l = string.sub ("                        ".. tostring(ref_elevation), -22, -1) .. "FT"
		
		if ref_nav_rnw == "-----" then
			line5_x = " MAG VAR                "
		else
			line5_x = " MAG VAR          LENGTH"
		end
		
		if ref_nav_mag_var == "" then
			line5_l = "----`"
		elseif string.sub(ref_nav_mag_var, 1, 1) == ">" then
			ii = tonumber(string.sub(ref_nav_mag_var, 2, -1))
			if ii == nil then
				line5_l = "   0`"
			else
				if ii == 0 then
					line5_s = "   0`"
				elseif ii < 0 then
					line5_s = "W" .. string.format("%3d", -ii) .. "`"
				else
					line5_s = "E" .. string.format("%3d", ii) .. "`"
				end
			end
		else
			ii = tonumber(ref_nav_mag_var)
			if ii == nil then
				line5_l = "   0`"
			else
				if ii == 0 then
					line5_l = "   0`"
				elseif ii < 0 then
					line5_l = "W" .. string.format("%3d", -ii) .. "`"
				else
					line5_l = "E" .. string.format("%3d", ii) .. "`"
				end
			end
		end
		
		if ref_nav_rnw ~= "-----" then
			if ref_nav_rnw_length ~= 0 then
				line5_l = line5_l .. "       "
				line5_l = line5_l .. string.format("%5d", ref_nav_rnw_length * 3.28084)
				line5_l = line5_l .. "  " .. string.format("%4d", ref_nav_rnw_length)
				line5_s = "                 FT    M"
			end
		end
	end
end

function B738_ref_nav_sel()

	if page_ref_sel == 1 then
		
		local ii = 0
		local jj = 0
		local kk = 0
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_linex')
		local left_linex = {true, true, true, true, true}
		left_linex[1] = ""
		left_linex[2] = ""
		left_linex[3] = ""
		left_linex[4] = ""
		left_linex[5] = ""
		
		local sid_len = 0
		local sid_len2 = 0
		local temp_str = ""
		local rw_tgt = ""
		local right_len = 0
		
		local max_page_wpt = 0
		
		jj = math.floor(navaid_list_n / 5)
		kk = navaid_list_n % 5
		
		if kk > 0 then
			max_page_wpt = jj + 1
		else
			max_page_wpt = jj
		end
		
		kk = (act_page_buf - 1) * 5
		for ii = 1, 5 do
			jj = kk + ii
			if jj > navaid_list_n then
				left_linex[ii] = ""
				left_line[ii] = ""
				right_line[ii] = ""
			else
				sid_len = navaid_list[jj][1]
				if sid_len == 1 then
					left_linex[ii] = " VOR    " .. navaid_list[jj][7]
				elseif sid_len == 2 then
					left_linex[ii] = " VORDME " .. navaid_list[jj][7]
				elseif sid_len == 3 then
					left_linex[ii] = " NDB    " .. navaid_list[jj][7]
				elseif sid_len == 4 then
					left_linex[ii] = " WPT    " .. navaid_list[jj][7]
				elseif sid_len == 5 then
					left_linex[ii] = " DME    " .. navaid_list[jj][7]
				elseif sid_len == 6 then
					left_linex[ii] = " LOC-GS " .. navaid_list[jj][7]
				elseif sid_len == 7 then
					left_linex[ii] = " LOC    " .. navaid_list[jj][7]
				elseif sid_len == 8 then
					left_linex[ii] = " ILS    " .. navaid_list[jj][7]
				elseif sid_len == 9 then
					left_linex[ii] = " APT    " .. navaid_list[jj][7]
				elseif sid_len == 10 then
					left_linex[ii] = " LDA-GS " .. navaid_list[jj][7]
				elseif sid_len == 11 then
					left_linex[ii] = " GLS    " .. navaid_list[jj][7]
				elseif sid_len == 12 then
					left_linex[ii] = " LPV    " .. navaid_list[jj][7]
				end
				
				temp_str = navaid_list[jj][6]
				sid_len2 = string.len(temp_str)
				if sid_len > 10 then
					temp_str = navaid_list[jj][6] .. " "
				elseif sid_len2 == 3 then
					temp_str = navaid_list[jj][6]
				elseif sid_len2 == 4 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -1, -1)
				elseif sid_len2 == 5 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -2, -1)
				else
					temp_str = ""
				end
				left_line[ii] = temp_str
				right_line[ii] = string_pos(navaid_list[jj][2], navaid_list[jj][3])
			end
		end
		
		-- Type: VOR, VORTAC, VORDME, NDB, LOC, ILS, DME, ILSDME, LOCDME, APT, WPT
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 7 then
				for jj = sid_len, 6 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		
		line1_x = left_linex[1] 
		line2_x = left_linex[2]
		line3_x = left_linex[3]
		line4_x = left_linex[4]
		line5_x = left_linex[5]
		
		
		max_page_buf = math.max(1, max_page_wpt)
		
		temp_str = navaid_list[1][4]
		if string.len(temp_str) > 6 then
			temp_str = string.sub(temp_str, 1, 6)
		end
		line0_l = " SEL DESIRED " .. temp_str
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
	end

end


function calc_detents_thr(x_fd)
	
	if x_fd == 1 then
		B738DR_flap_detents_thr[x_fd-1] = 0
		--B738DR_flap_detents[x_fd-1] / 2
	else
		B738DR_flap_detents_thr[x_fd-1] = (B738DR_flap_detents[x_fd-2] + B738DR_flap_detents[x_fd-1]) / 2
		--B738DR_flap_detents_thr[x_fd-1] = (B738DR_flap_detents[x_fd-1] - B738DR_flap_detents[x_fd-2]) / 4
	end
	
end

-- function detents_saved_timer()
	-- detents_saved = 0
-- end

function B738_fmc_ident()

	if page_ident == 1 then
		act_page_buf = 1
		max_page_buf = 2
		line0_l = "      IDENT             "
		line0_s = "                    1/2 "
		line1_x = " MODEL        ENG RATING"
		
		if B738DR_b737_variant == 4 then
			if B738DR_wings_variant == 0 then
				line1_l = ident_str .. "ERW  "
			elseif B738DR_wings_variant == 1 then
				line1_l = ident_str .. "ERW.1"
			else
				line1_l = ident_str .. "     "
			end
			line1_l = line1_l .. "         " .. engine_1k
		elseif B738DR_b737_variant >= 0 then
			if B738DR_wings_variant == 0 then
				line1_l = ident_str .. "W  "
			elseif B738DR_wings_variant == 1 then
				line1_l = ident_str .. "W.1"
			else
				line1_l = ident_str .. "   "
			end
			line1_l = line1_l .. "           " .. engine_1k
		elseif B738DR_73x == 0 then
			if B738DR_wings_variant == 0 then
				line1_l = "737-800W             " .. engine_1k
			elseif B738DR_wings_variant == 1 then
				line1_l = "737-800W.1           " .. engine_1k
			else
				line1_l = "737-800              " .. engine_1k
			end
		elseif B738DR_73x == 1 then
			if B738DR_wings_variant == 0 then
				line1_l = "737-900W             " .. engine_1k
			elseif B738DR_wings_variant == 1 then
				line1_l = "737-900W.1           " .. engine_1k
			else
				line1_l = "737-900ER            " .. engine_1k
			end
		elseif B738DR_73x == 2 then
			if B738DR_wings_variant == 0 then
				line1_l = "737-700W             " .. engine_1k
			elseif B738DR_wings_variant == 1 then
				line1_l = "737-700W.1           " .. engine_1k
			else
				line1_l = "737-700ER            " .. engine_1k
			end
		end
		line2_x = " NAV DATA         ACTIVE"
		line2_l = airac_navdata .. " "
		line2_l = line2_l .. airac_navdata_active
		line4_x = " OP PROGRAM             "
		if B738DR_vnav_common == 0 then
			line4_l = "559777-001  (U14.0)     "
		else
			line4_l = "559777-001  (U13.0)     "
		end
		line5_x = "               SUPP DATA"
		line6_l = "<INDEX         POS INIT>"
	elseif page_ident == 2 then
		act_page_buf = 2
		max_page_buf = 2
		line0_l = "      IDENT             "
		line0_s = "                    2/2 "
		line1_x = " PERF DEFAULTS          "
		line1_l = "651489-002              "
		line2_x = " SW OPTIONS             "
		line2_l = "BCG-01P-D8              "
		line5_x = " MODEL/ENGINE DATA      "
		line5_l = "BCG-01T-A0              "
		line6_l = "<INDEX         POS INIT>"
	end

end

function B738_fmc_offset()

	if page_offset == 1 then
		act_page_buf = 1
		max_page_buf = 1
		
		if legs_num > 1 then
			if offset_act ~= offset_act_mod then
				line0_inv = " MOD"
				line0_l = "     LATERAL OFFSET     "
			else
				line0_l = " ACT LATERAL OFFSET     "
			end
		else
			line0_l = "     LATERAL OFFSET     "
		end
		line0_s = "                    1/1 "
		
		line2_x = " OFFSET DIST            "
		if offset_dist_mod == 0 then
			line2_l = "---.-"
		else
			if offset_dist_mod == offset_dist and offset_side_mod == offset_side then
				if offset_side_mod == 1 then
					line2_l = "L"
				else
					line2_l = "R"
				end
				if offset_dist_mod < 10 then
					line2_l = line2_l .. string.format("%3.1f", offset_dist_mod)
				else
					line2_l = line2_l .. string.format("%4.1f", offset_dist_mod)
				end
			else
				if offset_side_mod == 1 then
					line2_inv = "L"
				else
					line2_inv = "R"
				end
				if offset_dist_mod < 10 then
					line2_inv = line2_inv .. string.format("%3.1f", offset_dist_mod)
				else
					line2_inv = line2_inv .. string.format("%4.1f", offset_dist_mod)
				end
			end
			
			line3_x = " START WAYPOINT         "
			if offset_start_mod == 0 then
				if offset_act >= 5 then
					line3_l = ""
				else
					if rte12_act == 0 then
						if legs_data2[offset][45] ~= 0 then
							line3_l = "-----"
						else
							line3_l = "*****"
						end
					else
						if legs_data8[offset][45] ~= 0 then
							line3_l = "-----"
						else
							line3_l = "*****"
						end
					end
				end
				-- elseif offset_start_def_mod == 0 then
					-- line3_l = "*****"
				-- elseif offset_start_def_mod <= offset then
					-- line3_l = "-----"
				-- else
					-- line3_l = "*****"
				-- end
			else
				if rte12_act == 0 then
					if offset_start_mod == offset_start then
						line3_l = legs_data2[offset_start_mod][1]
					else
						line3_inv = legs_data2[offset_start_mod][1]
					end
				elseif rte12_act == 1 then
					if offset_start_mod == offset_start then
						line3_l = legs_data8[offset_start_mod][1]
					else
						line3_inv = legs_data8[offset_start_mod][1]
					end
				end
			end
			
			line4_x = " END WAYPOINT           "
			if offset_end_mod == 0 then
				if offset_act >= 8 then
					line4_l = ""
				else
					line4_l = "-----"
				end
			else
				if rte12_act == 0 then
					if offset_end_mod == offset_end then
						line4_l = legs_data2[offset_end_mod][1]
					else
						line4_inv = legs_data2[offset_end_mod][1]
					end
				elseif rte12_act == 1 then
					if offset_end_mod == offset_end then
						line4_l = legs_data8[offset_end_mod][1]
					else
						line4_inv = legs_data2[offset_end_mod][1]
					end
				end
			end
		end
		
		if offset_act_mod >= 1 and offset_act_mod <= 4 then
			line6_l = "<ERASE                  "
		end
	end

end


function pos_to_str(pos_pos)
	local position2 = pos_pos
	local pos_str2 = ""
	pos_str2 = string.sub(position2, 1, 3) .. "`"
	pos_str2 = pos_str2 .. string.sub(position2, 4, 7)
	pos_str2 = pos_str2 .. " "
	pos_str2 = pos_str2 .. string.sub(position2, 8, 11)
	pos_str2 = pos_str2 .. "`"
	pos_str2 = pos_str2 .. string.sub(position2, 12, 15)
	return pos_str2
end

function B738_fmc_pos_init(in_mode)

	local pos_str = ""
	
	if page_pos_init == 1 then
		act_page_buf = 1
		max_page_buf = 3
		line0_l = "      POS INIT          "
		line0_s = "                    1/3 "
		pos_str = pos_to_str(last_pos)
		line1_x = "                LAST POS"
		line1_l = "      " .. pos_str
		line2_x = "REF AIRPORT             "
		line3_x = "GATE                    "
		
		local align_mode = 0
		if B738DR_irs_left_mode == 1 and B738DR_irs_left == 3 then
			align_mode = 1
		end
		if B738DR_irs_right_mode == 1 and B738DR_irs_right == 3 then
			align_mode = 1
		end
		if B738DR_irs_left_mode == 3 or B738DR_irs_right_mode == 3 then
			align_mode = 0
		end
		
		if align_mode == 1 then
			line5_x = "GMT-MON/DY   SET IRS HDG"
			line5_l = zulu_time .. "       "
			line5_l = line5_l .. irs_hdg
			disable_POS_5R = 0
		else
			line5_x = "GMT-MON/DY              "
			line5_l = zulu_time .. "           "
			disable_POS_5R = 1
			irs_hdg = "---`"
		end
		
		if B738DR_flight_phase == 0 then
			if rte12_act == 0 then
				line2_l = ref_icao2
				if ref_icao_pos ~= "               " then
					line2_l = line2_l .. "  ".. pos_to_str(ref_icao_pos)
				end
			line3_l = (ref_gate .. "                   ")
			elseif rte12_act == 2 then
				line2_l = ref_icao8
				if ref_icao_pos2 ~= "               " then
					line2_l = line2_l .. "  ".. pos_to_str(ref_icao_pos2)
				end
			elseif in_mode == 0 then
				if rte12_sel == 0 then
					line2_l = ref_icao2
					if ref_icao_pos ~= "               " then
						line2_l = line2_l .. "  ".. pos_to_str(ref_icao_pos)
					end
				line3_l = (ref_gate .. "                   ")
				else
					line2_l = ref_icao8
					if ref_icao_pos2 ~= "               " then
						line2_l = line2_l .. "  ".. pos_to_str(ref_icao_pos2)
					end
				end
			else
				if rte12_sel_fo == 0 then
					line2_l = ref_icao2
					if ref_icao_pos ~= "               " then
						line2_l = line2_l .. "  ".. pos_to_str(ref_icao_pos)
					end
				line3_l = (ref_gate .. "                   ")
				else
					line2_l = ref_icao8
					if ref_icao_pos2 ~= "               " then
						line2_l = line2_l .. "  ".. pos_to_str(ref_icao_pos2)
					end
				end
			end
			align_mode = 0
			
			if B738DR_irs_left_mode == 1 and B738DR_irs_left == 2 then
				if B738DR_alignment_left_remain == 0 or B738DR_irs_align_left2 ~= 0 then
					align_mode = 1
				end
			elseif B738DR_irs_left_mode == 1 and B738DR_irs_left == 1 and B738DR_irs_was_full_allign ~= 0 then
				if B738DR_alignment_left_remain == 0 or B738DR_irs_align_left2 ~= 0 then
					align_mode = 1
				end
			end
			if B738DR_irs_right_mode == 1 and B738DR_irs_right == 2 then
				if B738DR_alignment_right_remain == 0 or B738DR_irs_align_right2 ~= 0 then
					align_mode = 1
				end
			elseif B738DR_irs_right_mode == 1 and B738DR_irs_right == 1 and B738DR_irs_was_full_allign2 ~= 0 then
				if B738DR_alignment_right_remain == 0 or B738DR_irs_align_right2 ~= 0 then
					align_mode = 1
				end
			end
			if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
				align_mode = 0
			end
			
			if align_mode == 1 then
				if irs_pos == "" then
					pos_str = pos_to_str("*****.*******.*")
				else
					pos_str = pos_to_str(irs_pos)
				end
				line4_x = "             SET IRS POS"
				line4_l = "      " .. pos_str
				disable_POS_4R = 0
			else
				disable_POS_4R = 1
				irs_pos = ""
			end
			
			line6_x = "------------------------"
			line6_l = "<INDEX            ROUTE>"
			if ref_icao2 == "----" then
				disable_POS_3L = 1
			else
				disable_POS_3L = 0
			end
		end
	elseif page_pos_init == 2 then
		act_page_buf = 2
		max_page_buf = 3
		line0_l = "    POS REF             "
		line0_s = "                    2/3 "
		pos_str = pos_to_str(fmc_pos)
		line1_x = " FMC POS              GS"
		if pos_str == "---`--.- ----`--.-" then
			line1_l = "                "
		else
			line1_l = pos_str .. " "
		end
		line1_l = line1_l .. fmc_gs
		line1_s = "                      KT"
		pos_str = pos_to_str(B738DR_irs_pos)
		line2_x = " IRS L                  "
		if pos_str == "---`--.- ----`--.-" then
			line2_l = "                "
		else
			line2_l = pos_str .. " "
		end
		line2_l = line2_l .. irs_gs
		line2_s = "                      KT"
		pos_str = pos_to_str(B738DR_irs2_pos)
		line3_x = " IRS R                  "
		if pos_str == "---`--.- ----`--.-" then
			line3_l = "                "
		else
			line3_l = pos_str .. " "
		end
		line3_l = line3_l .. irs2_gs
		line3_s = "                      KT"
		pos_str = pos_to_str(B738DR_gps_pos)
		line4_x = " GPS L                  "
		if pos_str == "---`--.- ----`--.-" then
			line4_l = "                "
		else
			line4_l = pos_str .. "     "
		end
		line4_s = "                        "
		pos_str = pos_to_str(B738DR_gps2_pos)
		line5_x = " GPS R                  "
		if pos_str == "---`--.- ----`--.-" then
			line5_l = "                "
		else
			line5_l = pos_str .. "     "
		end
		line6_x = " RADIO                  "
	elseif page_pos_init == 3 then
		act_page_buf = 3
		max_page_buf = 3
		line0_l = "    POS SHIFT           "
		line0_s = "                    3/3 "
		
		line1_x = " FMC-L            FMC-R "
		line1_l = "<072`/0.0    248`/0.0  >"
		line1_s = "         NM          NM "
		
		line2_x = " GPS-L            GPS-R "
		line2_l = "<148`/0.0    145`/0.0  >"
		line2_s = "         NM          NM "
		
		line3_x = " IRS-L            IRS-R "
		line3_l = "<238`/3.0    224`/3.3  >"
		line3_s = "         NM          NM "
		
		line4_x = " RNP/ACTUAL       RADIO "
		line4_l = "             080`/0.0  >"
		if B738DR_rnp < 10 then
			line4_s = string.format("%4.2f", B738DR_rnp) .. "/"
		else
			line4_s = string.format("%4.1f", B738DR_rnp) .. "/"
		end
		if B738DR_anp < 9.995 then
			line4_s = line4_s .. string.format("%4.2f", B738DR_anp) .. "NM          NM>"
		else
			line4_s = line4_s .. "-.--NM          NM>"
		end
		
		line5_x = "------------------------"
		line5_l = "             NAV STATUS>"
		line6_l = "<INDEX                  "
	end

end

function B738_fmc_rte_init(in_mode)

	local exec_light_in = B738DR_fmc_exec_lights
	local use_rte12_sel = rte12_sel
	local use_rte_header_str = rte_header_str
	local use_rte_option_str = rte_option_str
	-- local use_legs_num_modx = legs_num_modx
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
		use_rte12_sel = rte12_sel_fo
		use_rte_header_str = rte_header_str_fo
		use_rte_option_str = rte_option_str_fo
		-- use_legs_num_modx = legs_num_modx_fo
	end
	
	copy_to_legs_data2_buf(use_rte12_sel)
	copy_to_fpln_buf(use_rte12_sel)
	
	local ref_icao_act = ref_icao
	local ref_icao_x_act = ref_icao_x
	local des_icao_act = des_icao
	local des_icao_x_act = des_icao_x
	local co_route_act = co_route
	local co_route_x_act = co_route_x
	local ref_rwy_act = ref_rwy
	local ref_rwy2_act = ref_rwy2
	local ref_rwy3_act = ref_rwy3
	local use_reverse_rte1 = reverse_rte1
	local use_save_rte1 = save_rte1
	local use_co_route_save1 = co_route_save1
	if use_rte12_sel ~= 0 then
		ref_icao_act = ref_icao7
		ref_icao_x_act = ref_icao7_x
		des_icao_act = des_icao7
		des_icao_x_act = des_icao7_x
		co_route_act = co_route7
		co_route_x_act = co_route7_x
		ref_rwy_act = ref_rwy7
		ref_rwy2_act = ref_rwy8
		ref_rwy3_act = ref_rwy9
		use_reverse_rte1 = reverse_rte2
		use_save_rte1 = save_rte2
		use_co_route_save1 = co_route_save2
	end
	
	if page_rte_init == 1 then
		--act_page = 1
		
		local ref_icao_disp = ""
		local des_icao_disp = ""
		local fpln_len = 0
		
		local ii = 0
		local jj = 0
		local kk = 0
		local ll = 0
		
		local temp_str = ""
		local temp_num = 0
		local temp_num2 = 0
		local temp_string = ""
		
		local max_page_fpln = 0
		
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('left_line_m')
		local left_line_m = {true, true, true, true, true}
		left_line_m[1] = ""
		left_line_m[2] = ""
		left_line_m[3] = ""
		left_line_m[4] = ""
		left_line_m[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('right_line_m')
		local right_line_m = {true, true, true, true, true}
		right_line_m[1] = ""
		right_line_m[2] = ""
		right_line_m[3] = ""
		right_line_m[4] = ""
		right_line_m[5] = ""
		raw_table('left_line_x')
		local left_line_x = {true, true, true, true, true}
		left_line_x[1] = ""
		left_line_x[2] = ""
		left_line_x[3] = ""
		left_line_x[4] = ""
		left_line_x[5] = ""
		left_line_x[6] = "------------------------"
		raw_table('left_line_inv')
		local left_line_inv = {true, true, true, true, true}
		left_line_inv[1] = ""
		left_line_inv[2] = ""
		left_line_inv[3] = ""
		left_line_inv[4] = ""
		left_line_inv[5] = ""
		raw_table('right_line_inv')
		local right_line_inv = {true, true, true, true, true}
		right_line_inv[1] = ""
		right_line_inv[2] = ""
		right_line_inv[3] = ""
		right_line_inv[4] = ""
		right_line_inv[5] = ""
		
		local sid_len = 0
		local txt_dspl = ""
		local draw_magenta = false
		local off_temp = 0
		local active_wpt = false
		
		local offset_fpln = 0
		if rte12_act == use_rte12_sel then
			-- offset_fpln = math.max(offset, 2)
			if use_rte12_sel == 0 then
				offset_fpln = math.max(offset_mod, 2)
			else
				offset_fpln = math.max(offset_mod2, 2)
			end
		else
			if use_rte12_sel == 0 then
				-- offset_fpln = offset_mod	--2
				offset_fpln = math.max(offset_mod, 2)
			else
				-- offset_fpln = offset_mod2	--2
				offset_fpln = math.max(offset_mod2, 2)
			end
		end
		
		if legs_num2_buf > 0 then
			if string.sub(legs_data2_buf[2][1], 1, 2) == "RW" and legs_data2_buf[2][19] == 1 then
				if offset_fpln < 3 then
					offset_fpln = 3
				end
			end
		end
		
		if ref_icao_x_act == "----" then
			ref_icao_disp = "----"
		else
			ref_icao_disp  = ref_icao_x_act
		end
		des_icao_disp = des_icao_x_act
		-- Flight plan
		if ref_icao_x_act == "----" or des_icao_x_act == "****" then
			max_page_fpln = 0
		else
			ll = fpln_num_buf - ll
			if ll < 0 then
				ll = 0
			end
			max_page_fpln = math.floor(ll / 5) + 2
			
		end
		
		if act_page_buf > max_page_fpln then
			act_page_buf = max_page_fpln
			if act_page_buf == 0 then
				act_page_buf = 1
			end
		end
		
		if act_page_buf == 1 then
			if exec_light_in == 1 then
				if legs_num > 1 and rte12_act == use_rte12_sel then
					line0_inv = "  MOD"
					line0_l   = "      "
				else
					line0_l = "      "
				end
			else
				if ref_icao_act == "----" or des_icao_act == "****" then
					line0_l = "      "
				else
					if legs_num > 1 and rte12_act == use_rte12_sel then
						line0_l = "  ACT "
					else
						line0_l = "      "
					end
				end
				line0_inv = ""
			end
			line1_x = " ORIGIN             DEST"
			if ref_icao_act ~= "----" and ref_icao_x_act ~= ref_icao_act and ref_icao_x_act ~= "----" then
				line1_inv = ref_icao_disp .. "                "
				line1_l = "                    "
			else
				line1_inv = "                    "
				line1_l = ref_icao_disp .. "                "
			end
			if des_icao_act ~= "****" and des_icao_x_act ~= des_icao_act and des_icao_x_act ~= "****" then
				line1_inv = line1_inv .. des_icao_disp
			else
				line1_l = line1_l .. des_icao_disp
			end
			
			line2_x = " CO ROUTE        FLT NO."
			if use_save_rte1 == 1 then
				line2_x = " REF CO ROUTE    FLT NO."
				temp_str = use_co_route_save1
				temp_num2 = string.len(temp_str)
				if temp_num2 < 12 then
					for temp_num = temp_num2, 11 do
						temp_str = temp_str .. " "
					end
				end
				line2_l = temp_str .. "    "
				line2_inv = "                "
			elseif co_route_act ~= "------------" and co_route_x_act ~= co_route_act and co_route_x_act ~= "------------" then
				temp_str = co_route_x_act
				if co_route_x_act ~= "------------" then
					temp_num2 = string.len(temp_str)
					if temp_num2 < 12 then
						for temp_num = temp_num2, 11 do
							temp_str = temp_str .. " "
						end
					end
				end
				line2_inv = temp_str .. "    "
				line2_l = "                "
			else
				temp_str = co_route_x_act
				if co_route_x_act ~= "------------" then
					temp_num2 = string.len(temp_str)
					if temp_num2 < 12 then
						for temp_num = temp_num2, 11 do
							temp_str = temp_str .. " "
						end
					end
				end
				line2_l = temp_str .. "    "
				line2_inv = "                "
			end
			temp_str = flt_num_x
			if flt_num_x ~= "--------" then
				temp_num2 = string.len(temp_str)
				if temp_num2 < 8 then
					for temp_num = temp_num2, 7 do
						temp_str = " " .. temp_str
					end
				end
			end
			line2_l = line2_l .. temp_str
			
			-- line3_x = " RUNWAY                 "
			if in_flight_mode == 1 or ref_icao_x_act == "----" then
				line3_x = ""
				line3_l = ""
				-- line3_l = "             SAVE ROUTE>"
			else
				line3_x = " RUNWAY                 "
				if ref_rwy2_act ~= ref_rwy_act and ref_rwy_act ~= "-----" and ref_rwy2_act ~= "-----" and rte12_act == use_rte12_sel then
					txt_dspl = "   "
					line3_inv = ref_rwy2_act
				else
					txt_dspl = ref_rwy2_act
				end
				txt_dspl = spaces_after(txt_dspl, 13)
				line3_l = txt_dspl 		--.. "SAVE ROUTE>"
			end
			line4_x = "------------------------"
			
			if legs_num2_buf < 2 then
				line4_l = "             "
				line4_inv = "             "
			elseif use_save_rte1 == 0 then
				line4_l = "<SAVE        "
				line4_inv = "             "
			elseif use_save_rte1 == 1 and use_co_route_save1 == "************" then
				line4_l = "             "
				line4_inv = " SAVE        "
			else
				line4_l = "<CONFIRM SAVE"
				line4_inv = "             "
			end
			
			-- if co_route_x_act ~= "------------" and legs_num2_buf > 1 then
				-- if use_reverse_rte1 == 1 then
					-- line4_l = line4_l .. "   REVERSE>"
				-- elseif use_reverse_rte1 == 2 then
					-- line4_inv = line4_inv .. "   REVERSE"
				-- end
			-- end
			
			if use_save_rte1 ~= 0 then
				line5_l = "<CANCEL     "
			else
				line5_l = "<ALTN DEST  "
			end
			if B738DR_no_rte2 == 0 and exec_light_in == 0 then
				if legs_num > 1 and rte12_act == use_rte12_sel and use_rte12_sel == 0 then
					if rte12_copy_done < 2 then
						line5_l = line5_l .. "   RTE COPY>"
					else
						line5_x = "                RTE COPY"
						line5_l = line5_l .. "   COMPLETE"
					end
				elseif legs_num > 1 and rte12_act == use_rte12_sel and use_rte12_sel == 1 then
					if rte21_copy_done < 2 then
						line5_l = line5_l .. "   RTE COPY>"
					else
						line5_x = "                RTE COPY"
						line5_l = line5_l .. "   COMPLETE"
					end
				-- -- else
					-- -- line5_l = "<ALTN DEST              "
				end
			-- else
				-- line5_l = "<ALTN DEST              "
			end
		elseif act_page_buf > 1 then
			
			if exec_light_in == 1 then
				if legs_num > 1 and rte12_act == use_rte12_sel then
					line0_inv = "  MOD"
					line0_l   = "      "
				else
					line0_l = "      "
				end
			else
				if legs_num > 1 and rte12_act == use_rte12_sel then
					line0_l = "  ACT "
				else
					line0_l = "      "
				end
				line0_inv = ""
			end
			
			ll = fpln_num_buf
			if fpln_num_buf > 0 then
				for ii = fpln_num_buf, 1, -1 do
					if offset_fpln >= fpln_data_buf[ii][4] and fpln_data_buf[ii][5] ~= 0 and offset_fpln < fpln_data_buf[ii][4] + fpln_data_buf[ii][5] then
						ll = ii - 1
						break
					end
				end
			end
			
			kk = ((act_page_buf - 2) * 5) + ll
			
			for ii = 1, 5 do
				jj = kk + ii
				
				if jj > fpln_num_buf then
					if jj == fpln_num_buf + 1 then
						if fpln_num_buf > 1 then
							if fpln_data_buf[fpln_num_buf][2] ~= "" and fpln_data_buf[fpln_num_buf][1] == "" then
								left_line[ii] = "--------"
								right_line[ii] = ""
							else
								left_line[ii] = "--------"
								right_line[ii] = "-----"
							end
						elseif fpln_num_buf > 0 then
							if fpln_data_buf[fpln_num_buf][2] ~= "" and fpln_data_buf[fpln_num_buf][1] == "" then
								left_line[ii] = ""
								right_line[ii] = ""
							else
								left_line[ii] = "--------"
								right_line[ii] = "-----"
							end
						else
							left_line[ii] = "--------"
							right_line[ii] = "-----"
						end
					else
						left_line[ii] = ""
						right_line[ii] = ""
					end
				else
					if fpln_data_buf[jj][2] == "" and fpln_data_buf[jj][1] == "" then
						left_line[ii] = "--------"
						right_line[ii] = "*****"
						left_line_x[ii] = "          THEN          "
						left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
					elseif fpln_data_buf[jj][2] == "" and fpln_data_buf[jj][1] ~= "" then
						if jj > 1 then
							if fpln_data_buf[jj-1][1] == "" then
								left_line[ii] = "--------"
								if fpln_data_buf[jj][6] == 0 or rte12_act ~= use_rte12_sel or legs_num == 0 then
									right_line[ii] = fpln_data_buf[jj][1]
								else
									right_line_inv[ii] = fpln_data_buf[jj][1]
								end
							else
								left_line[ii] = "DIRECT"
								draw_magenta = false
								active_wpt = false
								off_temp = fpln_data_buf[jj][4] + fpln_data_buf[jj][5] - 1
								if off_temp <= legs_num2_buf and off_temp > 0  and rte12_act == use_rte12_sel then
									if legs_data2_buf[off_temp][52] > 0 and offset == off_temp and legs_intdir == 0 then
										active_wpt = true
									end
								end
								if act_page_buf == 2 and ii == 1 and active_wpt == true then
									if off_temp <= legs_num2_buf and off_temp > 0 then
										if legs_data2_buf[off_temp][31] == "HA" or legs_data2_buf[off_temp][31] == "HF" or legs_data2_buf[off_temp][31] == "HM" and off_temp > 1 then
											if offset == off_temp or offset == off_temp - 1 then
												draw_magenta = true
											end
										else
											if offset == off_temp then
												draw_magenta = true
											end
										end
									end
								end
								if draw_magenta == false then
									if fpln_data_buf[jj][6] == 0 or rte12_act ~= use_rte12_sel or legs_intdir ~= 0 then
										right_line[ii] = fpln_data_buf[jj][1]
									else
										right_line_inv[ii] = fpln_data_buf[jj][1]
									end
								else
									right_line_m[ii] = fpln_data_buf[jj][1]
								end
							end
						else
							left_line[ii] = "DIRECT"
							draw_magenta = false
							active_wpt = false
							off_temp = fpln_data_buf[jj][4] + fpln_data_buf[jj][5] - 1
							if off_temp <= legs_num2_buf and off_temp > 0 and rte12_act == use_rte12_sel then
								if legs_data2_buf[off_temp][52] > 0 and offset == off_temp and legs_intdir == 0 then
									active_wpt = true
								end
							end
							if act_page_buf == 2 and ii == 1 and active_wpt == true then
								if off_temp <= legs_num2_buf and off_temp > 0 then
									if (legs_data2_buf[off_temp][31] == "HA" or legs_data2_buf[off_temp][31] == "HF" or legs_data2_buf[off_temp][31] == "HM") and off_temp > 1 then
										if offset == off_temp or offset == off_temp - 1 then
											draw_magenta = true
										end
									else
										if offset == off_temp then
											draw_magenta = true
										end
									end
								end
							end
							if draw_magenta == false then
								if fpln_data_buf[jj][6] == 0 or rte12_act ~= use_rte12_sel or legs_intdir ~= 0 then
									right_line[ii] = fpln_data_buf[jj][1]
								else
									right_line_inv[ii] = fpln_data_buf[jj][1]
								end
							else
								right_line_m[ii] = fpln_data_buf[jj][1]
							end
						end
					else
						if jj > 1 then
							if fpln_data_buf[jj-1][1] == "" then
								left_line[ii] = "--------"
							else
								if fpln_data_buf[jj][6] == 0 or rte12_act ~= use_rte12_sel or legs_num == 0 or fpln_data_buf[jj][2] == "DIRECT" then
									left_line[ii] = fpln_data_buf[jj][2]
								else
									left_line_inv[ii] = fpln_data_buf[jj][2]
								end
							end
						else
							if fpln_data_buf[jj][6] == 0 or rte12_act ~= use_rte12_sel or legs_num == 0 or fpln_data_buf[jj][2] == "DIRECT" then
								left_line[ii] = fpln_data_buf[jj][2]
							else
								left_line_inv[ii] = fpln_data_buf[jj][2]
							end
						end
						if fpln_data_buf[jj][1] == "" then
							right_line[ii] = "*****"
							-- create discontinuity to line_x
							left_line_x[ii] = "          THEN          "
							left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
						else
							draw_magenta = false
							active_wpt = false
							off_temp = fpln_data_buf[jj][4] + fpln_data_buf[jj][5] - 1
							if off_temp <= legs_num2_buf and off_temp > 0 and rte12_act == use_rte12_sel then
								if legs_data2_buf[off_temp][52] > 0 and offset == off_temp and legs_intdir == 0 then
									active_wpt = true
								end
							end
							if act_page_buf == 2 and ii == 1 and active_wpt == true then
								if off_temp <= legs_num2_buf and off_temp > 0 then
									if legs_data2_buf[off_temp][31] == "HA" or legs_data2_buf[off_temp][31] == "HF" or legs_data2_buf[off_temp][31] == "HM" and off_temp > 1 then
										if offset == off_temp or offset == off_temp - 1 then
											draw_magenta = true
										end
									else
										if offset == off_temp then
											draw_magenta = true
										end
									end
								end
							end
							if draw_magenta == false then
								if fpln_data_buf[jj][6] == 0 or rte12_act ~= use_rte12_sel or legs_intdir ~= 0 then
									right_line[ii] = fpln_data_buf[jj][1]
								else
									right_line_inv[ii] = fpln_data_buf[jj][1]
								end
							else
								right_line_m[ii] = fpln_data_buf[jj][1]
							end
						end
					end
				end
			end
			
			-- display engine
			for ii = 1, 5 do
				sid_len = string.len(left_line[ii])
				if sid_len < 15 then
					for jj = sid_len, 14 do
						left_line[ii] = left_line[ii] .. " "
					end
				end
				sid_len = string.len(right_line[ii])
				if sid_len < 9 then
					for jj = sid_len, 8 do
						right_line[ii] = " " .. right_line[ii]
					end
				end
				sid_len = string.len(left_line_m[ii])
				if sid_len < 15 then
					for jj = sid_len, 14 do
						left_line_m[ii] = left_line_m[ii] .. " "
					end
				end
				sid_len = string.len(right_line_m[ii])
				if sid_len < 9 then
					for jj = sid_len, 8 do
						right_line_m[ii] = " " .. right_line_m[ii]
					end
				end
				sid_len = string.len(left_line_inv[ii])
				if sid_len > 0 then
					local via_tmp = left_line_inv[ii]
					left_line_inv[ii] = ""
					for jj = 1, sid_len do
						if string.sub(via_tmp, jj, jj) == " " then
							left_line_inv[ii] = left_line_inv[ii] .. "*"
						else
							left_line_inv[ii] = left_line_inv[ii] .. string.sub(via_tmp, jj, jj)
						end
					end
				end
				if sid_len < 15 then
					for jj = sid_len, 14 do
						left_line_inv[ii] = left_line_inv[ii] .. " "
					end
				end
				sid_len = string.len(right_line_inv[ii])
				if sid_len < 9 then
					for jj = sid_len, 8 do
						right_line_inv[ii] = " " .. right_line_inv[ii]
					end
				end
			end
			line1_x = " VIA                 TO "
			line2_x = left_line_x[2]
			line3_x = left_line_x[3]
			line4_x = left_line_x[4]
			line5_x = left_line_x[5]
			line6_x = left_line_x[6]
			line1_l = left_line[1] .. right_line[1]
			line2_l = left_line[2] .. right_line[2]
			line3_l = left_line[3] .. right_line[3]
			line4_l = left_line[4] .. right_line[4]
			line5_l = left_line[5] .. right_line[5]
			line1_m = left_line_m[1] .. right_line_m[1]
			line2_m = left_line_m[2] .. right_line_m[2]
			line3_m = left_line_m[3] .. right_line_m[3]
			line4_m = left_line_m[4] .. right_line_m[4]
			line5_m = left_line_m[5] .. right_line_m[5]
			line1_inv = left_line_inv[1] .. right_line_inv[1]
			line2_inv = left_line_inv[2] .. right_line_inv[2]
			line3_inv = left_line_inv[3] .. right_line_inv[3]
			line4_inv = left_line_inv[4] .. right_line_inv[4]
			line5_inv = left_line_inv[5] .. right_line_inv[5]
		end
		
		max_page_buf = math.max(max_page_fpln, 1)
		
		line0_s = string.format("%2d",act_page_buf)
		line0_s = line0_s .. "/"
		if max_page_buf > 9 then
			line0_s = line0_s .. string.format("%2d",max_page_buf)
		else
			line0_s = line0_s .. string.format("%1d",max_page_buf)
		end
		line0_s = spaces_before(line0_s, 23)
		
		if legs_num > 1 and rte12_act == use_rte12_sel then
			line0_l = line0_l .. use_rte_header_str .. "              "
		else
			line0_c = "      " .. use_rte_header_str
			line0_l = line0_l .. "                   "
		end
		
		if exec_light_in == 1 then
			if legs_num > 1 and rte12_act == use_rte12_sel then
				line6_l = "<ERASE                  "
			else
				-- if use_legs_num_modx > 1 then
				if legs_num2_buf > 1 then
					line6_l = "<ERASE                  "
				elseif B738DR_no_rte2 == 0 then
					line6_l = use_rte_option_str .. "                  "
				end
			end
		else
			if legs_num > 1 and rte12_act == use_rte12_sel then
				if in_flight_mode == 0 then
					if pre_flt_perf_init == 0 then
						if B738DR_no_rte2 == 0 then
							line6_l = use_rte_option_str .. "        PERF INIT>"
						else
							line6_l = "              PERF INIT>"
						end
					else
						if B738DR_no_rte2 == 0 then
							line6_l = use_rte_option_str .. "          TAKEOFF>"
						else
							line6_l = "                TAKEOFF>"
						end
					end
				else
					if B738DR_no_rte2 == 0 then
						line6_l = use_rte_option_str .. "           OFFSET>"
					else
						line6_l = "                 OFFSET>"
					end
				end
			else
				-- if legs_num <= 1 and use_legs_num_modx > 1 then
				-- if use_legs_num_modx > 1 and rte12_act ~= use_rte12_sel then
				if legs_num2_buf > 1 and rte12_act ~= use_rte12_sel then
					if B738DR_no_rte2 == 0 then
						line6_l = use_rte_option_str .. "         ACTIVATE>"
					else
						line6_l = "               ACTIVATE>"
					end
				else
					if B738DR_no_rte2 == 0 then
						line6_l = use_rte_option_str .. "                  "
					end
				end
			end
		end
	end

end

function B738_fmc_dep_arr()

	if page_dep_arr == 1 then
		act_page_buf = 1
		max_page_buf = 1
		line0_l = "      DEP/ARR INDEX     "
		line0_s = "                    1/1 "
		if B738DR_no_rte2 == 0 then
			line1_x = "        RTE 1 "
			if rte12_act == 0 then
				line1_x = line1_x .. "(ACT)"
			end
		end
		if ref_icao_x == "----" then
			line1_l = "<DEP                ARR>"
		else
			line1_l = "<DEP     " .. ref_icao_x .. "       ARR>"
		end
		if des_icao_x == "****" then
			line2_l = "                    ARR>"
		else
			line2_l = "         " .. des_icao_x
			line2_l = line2_l .. "       ARR>"
		end
		if B738DR_no_rte2 == 0 then
			line3_x = "------- RTE 2 "
			if rte12_act == 1 then
				line3_x = line3_x .. "(ACT) ----"
			else
				line3_x = line3_x .. "----------"
			end
			
			if ref_icao7_x == "----" then
				line3_l = "<DEP                ARR>"
			else
				line3_l = "<DEP     " .. ref_icao7_x .. "       ARR>"
			end
			if des_icao7_x == "****" then
				line4_l = "                    ARR>"
			else
				line4_l = "         " .. des_icao7_x .. "       ARR>"
			end
			line5_x = "------------------------"
		end
		line6_lx = " DEP     OTHER      ARR "
		line6_l =  "<----              ---->"
	end
end

function flight_record()
	local fms_line = ""
	local fix_tmp = 0
	
	if start_rec == 1 then
		file_rec_path = io.open("Flight_Recorder.txt", "w")
		if file_rec_path == nil then
			start_rec = 5
			entry = "FLTREC ERROR"
		else
			entry = "FLTREC DES RUNNING..."
			start_rec = 3
			rec_alt = 42000
			rec_lat = simDR_latitude
			rec_lon = simDR_longitude
			rec_timer = 0
			fms_line = "Altitude / Speed Mach,IAS: Faxil_aero, Faxil_prop, Faxil_gear, Faxil engine, Faxil_total, N1\n"
			file_rec_path:write(fms_line)
			fms_line = "GW(tons), ATT pitch, V/S, FF (one engine), FF (all engines)\n"
			file_rec_path:write(fms_line)
		end
	elseif start_rec == 2 then
		file_rec_path = io.open("Flight_Recorder.txt", "w")
		if file_rec_path == nil then
			start_rec = 5
			entry = "FLTREC ERROR"
		else
			entry = "FLTREC CLB RUNNING..."
			start_rec = 4
			rec_alt = 0
			rec_lat = simDR_latitude
			rec_lon = simDR_longitude
			rec_timer = 0
			fms_line = "Altitude / Speed Mach,IAS: Faxil_aero, Faxil_prop, Faxil_gear, Faxil engine, Faxil_total, N1\n"
			file_rec_path:write(fms_line)
			fms_line = "GW(tons), ATT pitch, V/S, FF (one engine), FF (all engines)\n"
			file_rec_path:write(fms_line)
		end
	elseif start_rec == 3 then
		rec_timer = rec_timer + SIM_PERIOD
		if simDR_altitude_pilot < rec_alt then
			fms_line = string.format("%5.1f", simDR_altitude_pilot) .. "ft / " .. string.format("%5.3f", simDR_mach_no) .. " Mach / " .. tostring(simDR_airspeed_pilot) .. " kts: "
			fms_line = fms_line .. tostring(simDR_faxil_aero) .. ", " .. tostring(simDR_faxil_prop) .. ", " .. tostring(simDR_faxil_gear) .. ", " .. tostring(simDR_faxil_plugin + simDR_faxil_prop)
			fms_line = fms_line .. ", " .. tostring(simDR_faxil_total) .. ", " .. tostring(simDR_eng_N1[0]) .. "\n"
			file_rec_path:write(fms_line)
			fms_line = tostring(simDR_total_weight / 1000) .. ", " .. tostring(simDR_ahars_pitch_deg_pilot) .. ", " .. tostring(simDR_vvi_fpm_pilot) .. ", " .. tostring(simDR_ff[0]) .. ", " .. tostring(simDR_ff[0] * 2) .. "\n"
			file_rec_path:write(fms_line)
			fix_tmp = nd_calc_dist2(simDR_latitude, simDR_longitude, rec_lat, rec_lon)
			fms_line = "Timer = " .. tostring(rec_timer) .. ", Distance = " .. tostring(fix_tmp) .. "\n"
			file_rec_path:write(fms_line)
			file_rec_path:write("\n")
			rec_alt = roundDownToIncrement(simDR_altitude_pilot, 1000)
		end
	elseif start_rec == 4 then
		rec_timer = rec_timer + SIM_PERIOD
		if simDR_altitude_pilot > rec_alt then
			fms_line = string.format("%5.1f", simDR_altitude_pilot) .. "ft / " .. string.format("%5.3f", simDR_mach_no) .. " Mach / " .. tostring(simDR_airspeed_pilot) .. " kts: "
			fms_line = fms_line .. tostring(simDR_faxil_aero) .. ", " .. tostring(simDR_faxil_prop) .. ", " .. tostring(simDR_faxil_gear) .. ", " .. tostring(simDR_faxil_plugin + simDR_faxil_prop)
			fms_line = fms_line .. ", " .. tostring(simDR_faxil_total) .. ", " .. tostring(simDR_eng_N1[0]) .. "\n"
			file_rec_path:write(fms_line)
			fms_line = tostring(simDR_total_weight / 1000) .. ", " .. tostring(simDR_ahars_pitch_deg_pilot) .. ", " .. tostring(simDR_vvi_fpm_pilot) .. ", " .. tostring(simDR_ff[0]) .. ", " .. tostring(simDR_ff[0] * 2) .. "\n"
			file_rec_path:write(fms_line)
			fix_tmp = nd_calc_dist2(simDR_latitude, simDR_longitude, rec_lat, rec_lon)
			fms_line = "Timer = " .. tostring(rec_timer) .. ", Distance = " .. tostring(fix_tmp) .. "\n"
			file_rec_path:write(fms_line)
			file_rec_path:write("\n")
			rec_alt = roundUpToIncrement(simDR_altitude_pilot, 1000)
		end
	elseif start_rec == 6 then
		start_rec = 0
		file_rec_path:close()
		entry = "FLTREC STOPPED..."
	end
end

function dump_idle_path()
	local vvv = 0
	local vvvv = 0
	local fms_line = ""
	
	if B738DR_idle_path == 1 then
		file_idle_path = io.open("idle_path", "w")
		if file_idle_path ~= nil then
			B738DR_idle_path = 2
		else
			B738DR_idle_path = 0
		end
	elseif B738DR_idle_path == 2 then
		if simDR_airspeed_pilot >= B738DR_idle_path_kts then
			if simDR_airspeed_is_mach == 1 then
				simCMD_autopilot_co:once()
				simDR_airspeed_dial = B738DR_idle_path_kts
			end
		end
		if simDR_altitude_pilot < 1500 then
			B738DR_idle_path = 3
		end
	elseif B738DR_idle_path == 3 then
		file_idle_path:close()
		B738DR_idle_path = 0
		idle_alt = 40000
	end
end


function B738_fmc_fix()

	if page_fix == 1 then
		max_page_buf = math.min(fix_data_num + 1, 5)
		if act_page_buf > max_page_buf then
			act_page_buf = max_page_buf
		end
		
		local nd_lat = simDR_latitude
		local nd_lon = simDR_longitude
		local rd_from = ""
		local rd_from2 = ""
		local fix_tmp = 0
		
		local fms_line = ""
		
		line0_l = "      FIX INFO          "
		--line0_s = "                    1/1 "
		line0_s = "                    "
		line0_s = line0_s .. string.format("%1d", act_page_buf) .. "/" .. string.format("%1d", max_page_buf)
		
		if act_page_buf <= fix_data_num then
			-- calculate radial/distance from fix
			fix_tmp = (nd_calc_brg(fix_data[act_page_buf][3], fix_data[act_page_buf][4], nd_lat, nd_lon) + simDR_mag_variation + 360) % 360
			fix_tmp = displ_crs(fix_tmp)
			rd_from = rd_from .. string.format("%03d", fix_tmp) .. "/"
			fix_tmp = nd_calc_dist2(fix_data[act_page_buf][3], fix_data[act_page_buf][4], nd_lat, nd_lon)
			if fix_tmp > 9999 then
				rd_from = rd_from .. "9999"
			elseif fix_tmp >= 100 then
				rd_from = rd_from .. string.format("%4d", fix_tmp)
			elseif fix_tmp >= 10 then
				rd_from = rd_from .. string.format("%4.1f", fix_tmp)
			else
				rd_from = rd_from .. string.format("%4.1f", fix_tmp)
			end
			line1_x = " FIX  RAD/DIS FR        "
			line1_l = spaces_after(fix_data[act_page_buf][1], 5) .. "   " .. rd_from
			
			if B738DR_idle_path == 2 then
				if simDR_altitude_pilot < idle_alt then
					fms_line = string.format("%5.1f", idle_alt) .. " / " .. string.format("%8.4f", fix_tmp) .. "\n"
					file_idle_path:write(fms_line)
					idle_alt = roundDownToIncrement(simDR_altitude_pilot, 1000)
				end
			end
			
			line2_x = "RAD/DIS  ETA   DTG   ALT"
			--line2_l = "130/24  2004.5  10 12000"
			rd_from = "---"
			rd_from2 = ""
			if fix_data[act_page_buf][6] ~= -1 then
				-- radial
				rd_from = string.format("%03d", displ_crs(fix_data[act_page_buf][6])) .. "/"
				rd_from2 = "    "
				if fix_data[act_page_buf][7] > 0 then
					if fix_data[act_page_buf][7] >= 999 then
						rd_from = rd_from .. "999"
					elseif fix_data[act_page_buf][7] >= 10 then
						rd_from = rd_from .. string.format("%3d", fix_data[act_page_buf][7])
					else
						rd_from = rd_from .. string.format("%3.1f", fix_data[act_page_buf][7])
					end
				elseif fix_data[act_page_buf][12] <= 0 then
					rd_from = rd_from .. "---"
				else
					rd_from = rd_from .. "   "
					if fix_data[act_page_buf][16] >= 999 then
						rd_from2 = rd_from2 .. "999"
					elseif fix_data[act_page_buf][16] >= 10 then
						rd_from2 = rd_from2 .. string.format("%3d", fix_data[act_page_buf][16])
					else
						rd_from2 = rd_from2 .. string.format("%3.1f", fix_data[act_page_buf][16])
					end
				end
			elseif fix_data[act_page_buf][7] ~= -1 then
				-- distance
				if fix_data[act_page_buf][6] >= 0 then
					rd_from = string.format("%03d", displ_crs(fix_data[act_page_buf][6]))
				elseif fix_data[act_page_buf][12] <= 0 then
					rd_from = "---"
				else
					rd_from = "   "
					rd_from2 = string.format("%03d", displ_crs(fix_data[act_page_buf][17])) .. "    "
				end
				rd_from = rd_from .. "/"
				if fix_data[act_page_buf][7] >= 999 then
					rd_from = rd_from .. "999"
				elseif fix_data[act_page_buf][7] >= 10 then
					rd_from = rd_from .. string.format("%3d", fix_data[act_page_buf][7])
				else
					rd_from = rd_from .. string.format("%3.1f", fix_data[act_page_buf][7])
				end
			end
			
			if (fix_data[act_page_buf][6] == -1 or fix_data[act_page_buf][7] == -1) and fix_data[act_page_buf][12] > 0 then
				
				-- DTG
				fix_tmp = 0
				if fix_data[act_page_buf][12] == offset then
					fix_tmp = B738DR_rt_abeam_dist_bp - fix_data[act_page_buf][13]
				elseif fix_data[act_page_buf][12] > offset then
					fix_tmp = B738DR_rt_abeam_dist_bp + legs_data[offset][50] - legs_data[fix_data[act_page_buf][12]][50] - fix_data[act_page_buf][13]
				end
				
				if fix_tmp > 0 then
					-- ETA
					if fix_data[act_page_buf][18] < 0 then
						-- rd_from = rd_from .. " ----  "
						-- rd_from2 = rd_from2 .. "     z "
						rd_from = rd_from .. "       "
						rd_from2 = rd_from2 .. "       "
					else
						rd_from = rd_from .. " " .. string.sub(time_to_string(fix_data[act_page_buf][18]), 1, 4) .. "  "
						rd_from2 = rd_from2 .. "     z "
					end
					
					-- DTG
					if fix_tmp > 9999 then
						rd_from = rd_from .. "9999"
					elseif fix_tmp >= 100 then
						rd_from = rd_from .. string.format("%4d", fix_tmp)
					elseif fix_tmp >= 10 then
						rd_from = rd_from .. string.format("%4.1f", fix_tmp)
					else
						rd_from = rd_from .. string.format("%4.1f", fix_tmp)
					end
					rd_from2 = rd_from2 .. "     "
					
					-- ALT
					if fix_data[act_page_buf][20] >= 0 then
						if legs_data[fix_data[act_page_buf][12]][19] == 1 or legs_data[fix_data[act_page_buf][12]][19] == 3 or fix_data[act_page_buf][12] < td_idx then
							rd_from2 = rd_from2 .. fl_format(fix_data[act_page_buf][20], B738DR_trans_alt)
						else
							rd_from2 = rd_from2 .. fl_format(fix_data[act_page_buf][20], B738DR_trans_lvl)
						end
						-- rd_from2 = rd_from2 .. string.format("%5d", fix_data[act_page_buf][20])
					end
				end
			end
			
			line2_l = rd_from
			line2_s = rd_from2
			
			rd_from = "---"
			rd_from2 = ""
			if fix_data[act_page_buf][8] ~= -1 then
				-- rd_from = string.format("%03d", fix_data[act_page_buf][8])
				rd_from = string.format("%03d", displ_crs(fix_data[act_page_buf][8])) .. "/"
				rd_from2 = "    "
				if fix_data[act_page_buf][9] > 0 then
					if fix_data[act_page_buf][9] >= 999 then
						rd_from = rd_from .. "999"
					elseif fix_data[act_page_buf][9] >= 10 then
						rd_from = rd_from .. string.format("%3d", fix_data[act_page_buf][9])
					else
						rd_from = rd_from .. string.format("%3.1f", fix_data[act_page_buf][9])
					end
				elseif fix_data[act_page_buf][21] <= 0 then
					rd_from = rd_from .. "---"
				else
					rd_from = rd_from .. "   "
					if fix_data[act_page_buf][25] >= 999 then
						rd_from2 = rd_from2 .. "999"
					elseif fix_data[act_page_buf][25] >= 10 then
						rd_from2 = rd_from2 .. string.format("%3d", fix_data[act_page_buf][25])
					else
						rd_from2 = rd_from2 .. string.format("%3.1f", fix_data[act_page_buf][25])
					end
				end
			elseif fix_data[act_page_buf][9] ~= -1 then
				if fix_data[act_page_buf][8] >= 0 then
					rd_from = string.format("%03d", displ_crs(fix_data[act_page_buf][8]))
				elseif fix_data[act_page_buf][21] <= 0 then
					rd_from = "---"
				else
					rd_from = "   "
					rd_from2 = string.format("%03d", displ_crs(fix_data[act_page_buf][26])) .. "    "
				end
				rd_from = rd_from .. "/"
				if fix_data[act_page_buf][9] >= 999 then
					rd_from = rd_from .. "999"
				elseif fix_data[act_page_buf][9] >= 10 then
					rd_from = rd_from .. string.format("%3d", fix_data[act_page_buf][9])
				else
					rd_from = rd_from .. string.format("%3.1f", fix_data[act_page_buf][9])
				end
			end
			
			if (fix_data[act_page_buf][8] == -1 or fix_data[act_page_buf][9] == -1) and fix_data[act_page_buf][21] > 0 then
				-- DTG
				fix_tmp = 0
				if fix_data[act_page_buf][21] == offset then
					fix_tmp = B738DR_rt_abeam_dist_bp - fix_data[act_page_buf][22]
				elseif fix_data[act_page_buf][21] > offset then
					fix_tmp = B738DR_rt_abeam_dist_bp + legs_data[offset][50] - legs_data[fix_data[act_page_buf][21]][50] - fix_data[act_page_buf][22]
				end
				
				if fix_tmp > 0 then
					-- ETA
					if fix_data[act_page_buf][27] < 0 then
						-- rd_from = rd_from .. " ----  "
						-- rd_from2 = rd_from2 .. "     z "
						rd_from = rd_from .. "       "
						rd_from2 = rd_from2 .. "       "
					else
						rd_from = rd_from .. " " .. string.sub(time_to_string(fix_data[act_page_buf][27]), 1, 4) .. "  "
						rd_from2 = rd_from2 .. "     z "
					end
					
					-- DTG
					if fix_tmp > 9999 then
						rd_from = rd_from .. "9999"
					elseif fix_tmp >= 100 then
						rd_from = rd_from .. string.format("%4d", fix_tmp)
					elseif fix_tmp >= 10 then
						rd_from = rd_from .. string.format("%4.1f", fix_tmp)
					else
						rd_from = rd_from .. string.format("%4.1f", fix_tmp)
					end
					rd_from2 = rd_from2 .. "     "
					
					-- ALT
					if fix_data[act_page_buf][29] >= 0 then
						if legs_data[fix_data[act_page_buf][21]][19] == 1 or legs_data[fix_data[act_page_buf][21]][19] == 3 or fix_data[act_page_buf][21] < td_idx then
							rd_from2 = rd_from2 .. fl_format(fix_data[act_page_buf][29], B738DR_trans_alt)
						else
							rd_from2 = rd_from2 .. fl_format(fix_data[act_page_buf][29], B738DR_trans_lvl)
						end
						-- rd_from2 = rd_from2 .. string.format("%5d", fix_data[act_page_buf][29])
					end
				end
			end
			
			line3_l = rd_from
			line3_s = rd_from2
			
			rd_from = "---"
			rd_from2 = ""
			if fix_data[act_page_buf][10] ~= -1 then
				-- rd_from = string.format("%03d", fix_data[act_page_buf][10])
				rd_from = string.format("%03d", displ_crs(fix_data[act_page_buf][10])) .. "/"
				rd_from2 = "    "
				if fix_data[act_page_buf][11] > 0 then
					if fix_data[act_page_buf][11] >= 999 then
						rd_from = rd_from .. "999"
					elseif fix_data[act_page_buf][11] >= 10 then
						rd_from = rd_from .. string.format("%3d", fix_data[act_page_buf][11])
					else
						rd_from = rd_from .. string.format("%3.1f", fix_data[act_page_buf][11])
					end
				elseif fix_data[act_page_buf][30] <= 0 then
					rd_from = rd_from .. "---"
				else
					rd_from = rd_from .. "   "
					if fix_data[act_page_buf][34] >= 999 then
						rd_from2 = rd_from2 .. "999"
					elseif fix_data[act_page_buf][34] >= 10 then
						rd_from2 = rd_from2 .. string.format("%3d", fix_data[act_page_buf][34])
					else
						rd_from2 = rd_from2 .. string.format("%3.1f", fix_data[act_page_buf][34])
					end
				end
			elseif fix_data[act_page_buf][11] ~= -1 then
				if fix_data[act_page_buf][10] >= 0 then
					rd_from = string.format("%03d", displ_crs(fix_data[act_page_buf][10]))
				elseif fix_data[act_page_buf][30] <= 0 then
					rd_from = "---"
				else
					rd_from = "   "
					rd_from2 = string.format("%03d", displ_crs(fix_data[act_page_buf][35])) .. "    "
				end
				rd_from = rd_from .. "/"
				if fix_data[act_page_buf][11] >= 999 then
					rd_from = rd_from .. "999"
				elseif fix_data[act_page_buf][11] >= 10 then
					rd_from = rd_from .. string.format("%3d", fix_data[act_page_buf][11])
				else
					rd_from = rd_from .. string.format("%3.1f", fix_data[act_page_buf][11])
				end
			end
			
			if (fix_data[act_page_buf][10] == -1 or fix_data[act_page_buf][11] == -1) and fix_data[act_page_buf][30] > 0 then
				-- DTG
				fix_tmp = 0
				if fix_data[act_page_buf][30] == offset then
					fix_tmp = B738DR_rt_abeam_dist_bp - fix_data[act_page_buf][31]
				elseif fix_data[act_page_buf][30
				] > offset then
					fix_tmp = B738DR_rt_abeam_dist_bp + legs_data[offset][50] - legs_data[fix_data[act_page_buf][30]][50] - fix_data[act_page_buf][31]
				end
				
				if fix_tmp > 0 then
					-- ETA
					if fix_data[act_page_buf][36] < 0 then
						-- rd_from = rd_from .. " ----  "
						-- rd_from2 = rd_from2 .. "     z "
						rd_from = rd_from .. "       "
						rd_from2 = rd_from2 .. "       "
					else
						rd_from = rd_from .. " " .. string.sub(time_to_string(fix_data[act_page_buf][36]), 1, 4) .. "  "
						rd_from2 = rd_from2 .. "     z "
					end
					
					-- DTG
					if fix_tmp > 9999 then
						rd_from = rd_from .. "9999"
					elseif fix_tmp >= 100 then
						rd_from = rd_from .. string.format("%4d", fix_tmp)
					elseif fix_tmp >= 10 then
						rd_from = rd_from .. string.format("%4.1f", fix_tmp)
					else
						rd_from = rd_from .. string.format("%4.1f", fix_tmp)
					end
					rd_from2 = rd_from2 .. "     "
					
					-- ALT
					if fix_data[act_page_buf][38] >= 0 then
						if legs_data[fix_data[act_page_buf][30]][19] == 1 or legs_data[fix_data[act_page_buf][30]][19] == 3 or fix_data[act_page_buf][30] < td_idx then
							rd_from2 = rd_from2 .. fl_format(fix_data[act_page_buf][38], B738DR_trans_alt)
						else
							rd_from2 = rd_from2 .. fl_format(fix_data[act_page_buf][38], B738DR_trans_lvl)
						end
						-- rd_from2 = rd_from2 .. string.format("%5d", fix_data[act_page_buf][38])
					end
				end
			end
			
			line4_l = rd_from
			line4_s = rd_from2
			
			if fix_abm_act[act_page_buf] == 0 then
				if fix_abm_wpt_idx[act_page_buf] > 0 then
					line5_l = "<ABM"
				end
			else
				line5_x = " ABEAM                  "
				
				if fix_abm_wpt_idx[act_page_buf] > 0 then
					
					-- BRG/DIST
					if fix_abm_brg[act_page_buf] >= 0 then
						rd_from = string.format("%03d", displ_crs(fix_abm_brg[act_page_buf]))
					else
						rd_from = "---"
					end
					rd_from2 = "            "
					rd_from = rd_from .. "/"
					
					if fix_abm_dist[act_page_buf] >= 999 then
						rd_from = rd_from .. "999"
					elseif fix_abm_dist[act_page_buf] >= 10 then
						rd_from = rd_from .. string.format("%3d", fix_abm_dist[act_page_buf])
					else
						rd_from = rd_from .. string.format("%3.1f", fix_abm_dist[act_page_buf])
					end
					
					-- DTG
					fix_tmp = 0
					if fix_abm_wpt_idx[act_page_buf] == offset then
						fix_tmp = B738DR_rt_abeam_dist_bp - fix_abm_wpt_dist[act_page_buf]
					elseif fix_abm_wpt_idx[act_page_buf] > offset then
						fix_tmp = B738DR_rt_abeam_dist_bp + legs_data[offset][50] - legs_data[fix_abm_wpt_idx[act_page_buf]][50] - fix_abm_wpt_dist[act_page_buf]
					end
					
					if fix_tmp > 0 then
						-- ETA
						if fix_abm_eta[act_page_buf] < 0 then
							rd_from = rd_from .. "       "
							rd_from2 = rd_from2 .. "  "
						else
							rd_from = rd_from .. " " .. string.sub(time_to_string(fix_abm_eta[act_page_buf]), 1, 4) .. "  "
							rd_from2 = rd_from2 .. "z "
						end
						
						-- DTG
						if fix_tmp > 9999 then
							rd_from = rd_from .. "9999"
						elseif fix_tmp >= 100 then
							rd_from = rd_from .. string.format("%4d", fix_tmp)
						elseif fix_tmp >= 10 then
							rd_from = rd_from .. string.format("%4.1f", fix_tmp)
						else
							rd_from = rd_from .. string.format("%4.1f", fix_tmp)
						end
						rd_from2 = rd_from2 .. "     "
						
						-- ALT
						if fix_abm_alt[act_page_buf] >= 0 then
							if legs_data[fix_abm_wpt_idx[act_page_buf]][19] == 1 or legs_data[fix_abm_wpt_idx[act_page_buf]][19] == 3 or fix_abm_wpt_idx[act_page_buf] < td_idx then
								rd_from2 = rd_from2 .. fl_format(fix_abm_alt[act_page_buf], B738DR_trans_alt)
							else
								rd_from2 = rd_from2 .. fl_format(fix_abm_alt[act_page_buf], B738DR_trans_lvl)
							end
						end
					end
					
					line5_l = rd_from
					line5_s = rd_from2
				end
			end
			
		else
			line1_x = " FIX  RAD/DIS FR        "
			line1_l = "*****                   "
			line2_x = "RAD/ DIS ETA   DTG   ALT"
			line2_l = "---                     "
			line3_l = "---                     "
			line4_l = "---                     "
		end
	end

end

function update_rnw_list(in_rte)

	local ii = 0
	local nd_x = 0
	local idx_rec = 0
	
	raw_table('mem_ref_rnw_list')
	local mem_ref_rnw_list = {}
	local mem_ref_rnw_list_num = 0
	raw_table('mem_des_rnw_list')
	local mem_des_rnw_list = {}
	local mem_des_rnw_list_num = 0
	
	local use_ref_icao = ""
	local use_des_icao = ""
	
	if in_rte == 0 then
		use_ref_icao = ref_icao_x
		use_des_icao = des_icao_x
	else
		use_ref_icao = ref_icao7_x
		use_des_icao = des_icao7_x
	end
	
	if rnw_data_num > 0 then
		if string.len(use_ref_icao) > 0 then
			nd_x = string.byte(string.sub(use_ref_icao, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_rnw[nd_x][99999] > 0 then
					for ii = 1, idx_rnw[nd_x][99999] do
						idx_rec = idx_rnw[nd_x][ii]
						if use_ref_icao == rnw_data[idx_rec][1] then
							mem_ref_rnw_list_num = mem_ref_rnw_list_num + 1
							mem_ref_rnw_list[mem_ref_rnw_list_num] = {true, true, true, true, true, true, true, true}
							mem_ref_rnw_list[mem_ref_rnw_list_num][1] = rnw_data[idx_rec][1]
							mem_ref_rnw_list[mem_ref_rnw_list_num][2] = rnw_data[idx_rec][2]
							mem_ref_rnw_list[mem_ref_rnw_list_num][3] = rnw_data[idx_rec][3]
							mem_ref_rnw_list[mem_ref_rnw_list_num][4] = rnw_data[idx_rec][4]
							mem_ref_rnw_list[mem_ref_rnw_list_num][5] = rnw_data[idx_rec][5]
							mem_ref_rnw_list[mem_ref_rnw_list_num][6] = rnw_data[idx_rec][6]
							mem_ref_rnw_list[mem_ref_rnw_list_num][7] = rnw_data[idx_rec][7]
							mem_ref_rnw_list[mem_ref_rnw_list_num][8] = rnw_data[idx_rec][8]
						end
					end
				end
			end
		end
		
		if arr_data_x == 0 then
			if string.len(use_des_icao) > 0 then
				nd_x = string.byte(string.sub(use_des_icao, 1, 1))
				if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
					if idx_rnw[nd_x][99999] > 0 then
						for ii = 1, idx_rnw[nd_x][99999] do
							idx_rec = idx_rnw[nd_x][ii]
							if use_des_icao == rnw_data[idx_rec][1] then
								mem_des_rnw_list_num = mem_des_rnw_list_num + 1
								mem_des_rnw_list[mem_des_rnw_list_num] = {true, true, true, true, true, true, true, true}
								mem_des_rnw_list[mem_des_rnw_list_num][1] = rnw_data[idx_rec][1]
								mem_des_rnw_list[mem_des_rnw_list_num][2] = rnw_data[idx_rec][2]
								mem_des_rnw_list[mem_des_rnw_list_num][3] = rnw_data[idx_rec][3]
								mem_des_rnw_list[mem_des_rnw_list_num][4] = rnw_data[idx_rec][4]
								mem_des_rnw_list[mem_des_rnw_list_num][5] = rnw_data[idx_rec][5]
								mem_des_rnw_list[mem_des_rnw_list_num][6] = rnw_data[idx_rec][6]
								mem_des_rnw_list[mem_des_rnw_list_num][7] = rnw_data[idx_rec][7]
								mem_des_rnw_list[mem_des_rnw_list_num][8] = rnw_data[idx_rec][8]
							end
						end
					end
				end
			end
		else
			for ii = 1, mem_ref_rnw_list_num do
				mem_des_rnw_list_num = mem_des_rnw_list_num + 1
				mem_des_rnw_list[mem_des_rnw_list_num] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					mem_des_rnw_list[mem_des_rnw_list_num][jj] = mem_ref_rnw_list[ii][jj]
				end
			end
		end
	end
	
	-- REF runways
	local list_rwy = ""
	local max_rwy = mem_ref_rnw_list_num
	if max_rwy > 0 then
		if max_rwy > 16 then
			max_rwy = 16
		end
		for ii = 1, max_rwy do
			list_rwy = list_rwy .. spaces_after(mem_ref_rnw_list[ii][2], 3)
			B738DR_ref_list_rwy_len[ii-1] = mem_ref_rnw_list[ii][7]
			B738DR_ref_list_rwy_hdg[ii-1] = (mem_ref_rnw_list[ii][8] +  mag_variation_deg(mem_ref_rnw_list[ii][3], mem_ref_rnw_list[ii][4]) + 360) % 360
		end
	end
	B738DR_ref_list_rwy = list_rwy
	if use_ref_icao == "----" then
		B738DR_ref_list_rwy_icao = ""
	else
		B738DR_ref_list_rwy_icao = use_ref_icao
	end
	
	-- DES runways
	list_rwy = ""
	max_rwy = mem_des_rnw_list_num
	if max_rwy > 0 then
		if max_rwy > 16 then
			max_rwy = 16
		end
		for ii = 1, max_rwy do
			list_rwy = list_rwy .. spaces_after(mem_des_rnw_list[ii][2], 3)
			B738DR_des_list_rwy_len[ii-1] = mem_des_rnw_list[ii][7]
			B738DR_des_list_rwy_hdg[ii-1] = (mem_des_rnw_list[ii][8] +  mag_variation_deg(mem_des_rnw_list[ii][3], mem_des_rnw_list[ii][4]) + 360) % 360
		end
	end
	B738DR_des_list_rwy = list_rwy
	
	if arr_data_x == 0 then
		if use_des_icao == "****" then
			B738DR_des_list_rwy_icao = ""
		else
			B738DR_des_list_rwy_icao = use_des_icao
		end
	else
		if use_ref_icao == "****" then
			B738DR_des_list_rwy_icao = ""
		else
			B738DR_des_list_rwy_icao = use_ref_icao
		end
	end
end

function create_rnw_list(in_rte)

	local rnw_temp = ""
	local temp_str1 = ""
	local temp_str2 = ""
	local ii = 0
	local jj = 0
	local nd_x = 0
	local idx_rec = 0
	
	local use_ref_icao = ""
	local use_des_icao = ""
	local use_sel_ref_sid = ""
	
	if in_rte == 0 then
		use_ref_icao = ref_icao_x
		use_des_icao = des_icao_x
		use_sel_ref_sid = sel_ref_sid2
	else
		use_ref_icao = ref_icao7_x
		use_des_icao = des_icao7_x
		use_sel_ref_sid = sel_ref_sid8
	end
	
	copy_to_ref_data(in_rte)
	
	ref_rnw_list = {}
	ref_rnw_list_num = 0
	ref_rnw_list2 = {}
	ref_rnw_list_num2 = 0
	des_rnw_list = {}
	des_rnw_list_num = 0
	
	if rnw_data_num > 0 then
		if string.len(use_ref_icao) > 0 then
			nd_x = string.byte(string.sub(use_ref_icao, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_rnw[nd_x][99999] > 0 then
					for ii = 1, idx_rnw[nd_x][99999] do
						idx_rec = idx_rnw[nd_x][ii]
						if use_ref_icao == rnw_data[idx_rec][1] then
							ref_rnw_list_num2 = ref_rnw_list_num2 + 1
							ref_rnw_list2[ref_rnw_list_num2] = {true, true, true, true, true, true, true, true}
							ref_rnw_list2[ref_rnw_list_num2][1] = rnw_data[idx_rec][1]
							ref_rnw_list2[ref_rnw_list_num2][2] = rnw_data[idx_rec][2]
							ref_rnw_list2[ref_rnw_list_num2][3] = rnw_data[idx_rec][3]
							ref_rnw_list2[ref_rnw_list_num2][4] = rnw_data[idx_rec][4]
							ref_rnw_list2[ref_rnw_list_num2][5] = rnw_data[idx_rec][5]
							ref_rnw_list2[ref_rnw_list_num2][6] = rnw_data[idx_rec][6]
							ref_rnw_list2[ref_rnw_list_num2][7] = rnw_data[idx_rec][7]
							ref_rnw_list2[ref_rnw_list_num2][8] = rnw_data[idx_rec][8]
							if use_sel_ref_sid == "------" then
								ref_rnw_list_num = ref_rnw_list_num + 1
								ref_rnw_list[ref_rnw_list_num] = {true, true, true, true, true, true, true, true}
								ref_rnw_list[ref_rnw_list_num][1] = rnw_data[idx_rec][1]
								ref_rnw_list[ref_rnw_list_num][2] = rnw_data[idx_rec][2]
								ref_rnw_list[ref_rnw_list_num][3] = rnw_data[idx_rec][3]
								ref_rnw_list[ref_rnw_list_num][4] = rnw_data[idx_rec][4]
								ref_rnw_list[ref_rnw_list_num][5] = rnw_data[idx_rec][5]
								ref_rnw_list[ref_rnw_list_num][6] = rnw_data[idx_rec][6]
								ref_rnw_list[ref_rnw_list_num][7] = rnw_data[idx_rec][7]
								ref_rnw_list[ref_rnw_list_num][8] = rnw_data[idx_rec][8]
							else
								-- runways by SID
								rnw_temp = "RW" .. rnw_data[idx_rec][2]
								for jj = 1, sid_num do
									if ref_data_sid[jj][1] == use_sel_ref_sid then
										if string.sub(ref_data_sid[jj][2],-1,-1) == "B" then
											temp_str1 = string.sub(ref_data_sid[jj][2],1,-2)
											temp_str2 = string.sub(rnw_temp,1,-2)
										else
											temp_str1 = "A"
											temp_str2 = "B"
										end
										if rnw_temp == ref_data_sid[jj][2] or ref_data_sid[jj][2] == "ALL" or temp_str1 == temp_str2 then
											ref_rnw_list_num = ref_rnw_list_num + 1
											ref_rnw_list[ref_rnw_list_num] = {true, true, true, true, true, true, true, true}
											ref_rnw_list[ref_rnw_list_num][1] = rnw_data[idx_rec][1]
											ref_rnw_list[ref_rnw_list_num][2] = rnw_data[idx_rec][2]
											ref_rnw_list[ref_rnw_list_num][3] = rnw_data[idx_rec][3]
											ref_rnw_list[ref_rnw_list_num][4] = rnw_data[idx_rec][4]
											ref_rnw_list[ref_rnw_list_num][5] = rnw_data[idx_rec][5]
											ref_rnw_list[ref_rnw_list_num][6] = rnw_data[idx_rec][6]
											ref_rnw_list[ref_rnw_list_num][7] = rnw_data[idx_rec][7]
											ref_rnw_list[ref_rnw_list_num][8] = rnw_data[idx_rec][8]
											break
										end
									end
								end
							end
						end
					end
				end
			end
		end
		
		if string.len(use_des_icao) > 0 then
			nd_x = string.byte(string.sub(use_des_icao, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_rnw[nd_x][99999] > 0 then
					for ii = 1, idx_rnw[nd_x][99999] do
						idx_rec = idx_rnw[nd_x][ii]
						if use_des_icao == rnw_data[idx_rec][1] then
							des_rnw_list_num = des_rnw_list_num + 1
							des_rnw_list[des_rnw_list_num] = {true, true, true, true, true, true, true, true}
							des_rnw_list[des_rnw_list_num][1] = rnw_data[idx_rec][1]
							des_rnw_list[des_rnw_list_num][2] = rnw_data[idx_rec][2]
							des_rnw_list[des_rnw_list_num][3] = rnw_data[idx_rec][3]
							des_rnw_list[des_rnw_list_num][4] = rnw_data[idx_rec][4]
							des_rnw_list[des_rnw_list_num][5] = rnw_data[idx_rec][5]
							des_rnw_list[des_rnw_list_num][6] = rnw_data[idx_rec][6]
							des_rnw_list[des_rnw_list_num][7] = rnw_data[idx_rec][7]
							des_rnw_list[des_rnw_list_num][8] = rnw_data[idx_rec][8]
						end
					end
				end
			end
		end
	end
	
	copy_from_rnw_list(in_rte)
end

function copy_from_rnw_list(in_rte)
	if in_rte == 0 then
		ref_rnw_list_1 = {}
		ref_rnw_list_num_1 = 0
		ref_rnw_list2_1 = {}
		ref_rnw_list_num2_1 = 0
		des_rnw_list_1 = {}
		des_rnw_list_num_1 = 0
		
		if ref_rnw_list_num > 0 then
			ref_rnw_list_num_1 = ref_rnw_list_num
			for ii = 1, ref_rnw_list_num do
				ref_rnw_list_1[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list_1[ii][jj] = ref_rnw_list[ii][jj]
				end
			end
		end
		
		if ref_rnw_list_num2 > 0 then
			ref_rnw_list_num2_1 = ref_rnw_list_num2
			for ii = 1, ref_rnw_list_num2 do
				ref_rnw_list2_1[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list2_1[ii][jj] = ref_rnw_list2[ii][jj]
				end
			end
		end
		
		if des_rnw_list_num > 0 then
			des_rnw_list_num_1 = des_rnw_list_num
			for ii = 1, des_rnw_list_num do
				des_rnw_list_1[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					des_rnw_list_1[ii][jj] = des_rnw_list[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		ref_rnw_list_2 = {}
		ref_rnw_list_num_2 = 0
		ref_rnw_list2_2 = {}
		ref_rnw_list_num2_2 = 0
		des_rnw_list_2 = {}
		des_rnw_list_num_2 = 0
		
		if ref_rnw_list_num > 0 then
			ref_rnw_list_num_2 = ref_rnw_list_num
			for ii = 1, ref_rnw_list_num do
				ref_rnw_list_2[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list_2[ii][jj] = ref_rnw_list[ii][jj]
				end
			end
		end
		
		if ref_rnw_list_num2 > 0 then
			ref_rnw_list_num2_2 = ref_rnw_list_num2
			for ii = 1, ref_rnw_list_num2 do
				ref_rnw_list2_2[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list2_2[ii][jj] = ref_rnw_list2[ii][jj]
				end
			end
		end
		
		if des_rnw_list_num > 0 then
			des_rnw_list_num_2 = des_rnw_list_num
			for ii = 1, des_rnw_list_num do
				des_rnw_list_2[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					des_rnw_list_2[ii][jj] = des_rnw_list[ii][jj]
				end
			end
		end
	end
end

function copy_to_rnw_list(in_rte)
	if in_rte == 0 then
		ref_rnw_list = {}
		ref_rnw_list_num = 0
		ref_rnw_list2 = {}
		ref_rnw_list_num2 = 0
		des_rnw_list = {}
		des_rnw_list_num = 0
		
		if ref_rnw_list_num_1 > 0 then
			ref_rnw_list_num = ref_rnw_list_num_1
			for ii = 1, ref_rnw_list_num do
				ref_rnw_list[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list[ii][jj] = ref_rnw_list_1[ii][jj]
				end
			end
		end
		
		if ref_rnw_list_num2_1 > 0 then
			ref_rnw_list_num2 = ref_rnw_list_num2_1
			for ii = 1, ref_rnw_list_num2 do
				ref_rnw_list2[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list2[ii][jj] = ref_rnw_list2_1[ii][jj]
				end
			end
		end
		
		if des_rnw_list_num_1 > 0 then
			des_rnw_list_num = des_rnw_list_num_1
			for ii = 1, des_rnw_list_num do
				des_rnw_list[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					des_rnw_list[ii][jj] = des_rnw_list_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		ref_rnw_list = {}
		ref_rnw_list_num = 0
		ref_rnw_list2 = {}
		ref_rnw_list_num2 = 0
		des_rnw_list = {}
		des_rnw_list_num = 0
		
		if ref_rnw_list_num_2 > 0 then
			ref_rnw_list_num = ref_rnw_list_num_2
			for ii = 1, ref_rnw_list_num do
				ref_rnw_list[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list[ii][jj] = ref_rnw_list_2[ii][jj]
				end
			end
		end
		
		if ref_rnw_list_num2_2 > 0 then
			ref_rnw_list_num2 = ref_rnw_list_num2_2
			for ii = 1, ref_rnw_list_num2 do
				ref_rnw_list2[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					ref_rnw_list2[ii][jj] = ref_rnw_list2_2[ii][jj]
				end
			end
		end
		
		if des_rnw_list_num_2 > 0 then
			des_rnw_list_num = des_rnw_list_num_2
			for ii = 1, des_rnw_list_num do
				des_rnw_list[ii] = {true, true, true, true, true, true, true, true}
				for jj = 1, 8 do
					des_rnw_list[ii][jj] = des_rnw_list_2[ii][jj]
				end
			end
		end
	end
end

function create_sid_list(in_rte)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local rnw_temp = ""
	local temp_str1 = ""
	local temp_str2 = ""
	local sid_len = 0
	
	local use_sel_ref_rwy = ""
	
	if in_rte == 0 then
		use_sel_ref_rwy = sel_ref_rwy2
	else
		use_sel_ref_rwy = sel_ref_rwy8
	end
	
	copy_to_ref_data(in_rte)
	
	sid_list = {}
	sid_list_num = 0
	
	if sid_num > 0 then
		-- Create list of SIDs
		for ii = 1, sid_num do
			if use_sel_ref_rwy == "-----" then
				sid_list_num = sid_list_num + 1
				sid_list[sid_list_num] = {true, true}
				sid_list[sid_list_num][1] = ref_data_sid[ii][1]
				sid_list[sid_list_num][2] = ref_data_sid[ii][2]
			else
				-- SIDs by runway
				rnw_temp = "RW" .. use_sel_ref_rwy
				if string.sub(ref_data_sid[ii][2],-1,-1) == "B" then
					temp_str1 = string.sub(ref_data_sid[ii][2],1,-2)
					temp_str2 = string.sub(rnw_temp,1,-2)
				else
					temp_str1 = "A"
					temp_str2 = "B"
				end
				if rnw_temp == ref_data_sid[ii][2] or ref_data_sid[ii][2] == "ALL" or temp_str1 == temp_str2 then
					sid_list_num = sid_list_num + 1
					sid_list[sid_list_num] = {true, true}
					sid_list[sid_list_num][1] = ref_data_sid[ii][1]
					sid_list[sid_list_num][2] = ref_data_sid[ii][2]
				end
			end
		end
		
		-- Create the one SID for more runways
		if sid_list_num > 1 then
			
			temp_list = {}
			temp_list_num = 0
			
			temp_list_num = temp_list_num + 1
			temp_list[temp_list_num] = {true, true}
			temp_list[temp_list_num][1] = sid_list[1][1]
			temp_list[temp_list_num][2] = sid_list[1][2]
			temp_str1 = sid_list[1][1]
			
			for ii = 2, sid_list_num do
				if sid_list[ii][1] ~= temp_str1 then
					temp_list_num = temp_list_num + 1
					temp_list[temp_list_num] = {true, true}
					temp_list[temp_list_num][1] = sid_list[ii][1]
					temp_list[temp_list_num][2] = sid_list[ii][2]
					temp_str1 = sid_list[ii][1]
				end
			end
			
			sid_list = {}
			sid_list_num = 0
			for ii = 1, temp_list_num do
				sid_list_num = sid_list_num + 1
				sid_list[sid_list_num] = {true, true}
				sid_list[sid_list_num][1] = temp_list[ii][1]
				sid_list[sid_list_num][2] = temp_list[ii][2]
			end
		end
	end
	
	copy_from_sid_list(in_rte)
end

function copy_from_sid_list(in_rte)
	if in_rte == 0 then
		sid_list_1 = {}
		sid_list_num_1 = 0
		
		if sid_list_num > 0 then
			sid_list_num_1 = sid_list_num
			for ii = 1, sid_list_num do
				sid_list_1[ii] = {true, true}
				for jj = 1, 2 do
					sid_list_1[ii][jj] = sid_list[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		sid_list_2 = {}
		sid_list_num_2 = 0
		
		if sid_list_num > 0 then
			sid_list_num_2 = sid_list_num
			for ii = 1, sid_list_num do
				sid_list_2[ii] = {true, true}
				for jj = 1, 2 do
					sid_list_2[ii][jj] = sid_list[ii][jj]
				end
			end
		end
	end
end

function copy_to_sid_list(in_rte)
	if in_rte == 0 then
		sid_list = {}
		sid_list_num = 0
		
		if sid_list_num_1 > 0 then
			sid_list_num = sid_list_num_1
			for ii = 1, sid_list_num do
				sid_list[ii] = {true, true}
				for jj = 1, 2 do
					sid_list[ii][jj] = sid_list_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		sid_list = {}
		sid_list_num = 0
		
		if sid_list_num_2 > 0 then
			sid_list_num = sid_list_num_2
			for ii = 1, sid_list_num do
				sid_list[ii] = {true, true}
				for jj = 1, 2 do
					sid_list[ii][jj] = sid_list_2[ii][jj]
				end
			end
		end
	end
end

function create_sid_tns_list(in_rte)
	
	local ii = 0
	
	local use_sel_ref_sid = ""
	
	if in_rte == 0 then
		use_sel_ref_sid = sel_ref_sid2
	else
		use_sel_ref_sid = sel_ref_sid8
	end
	
	copy_to_ref_data(in_rte)
	
	sid_tns_list = {}
	sid_tns_list_num = 0
	
	if sid_tns_num > 0 then
		for ii = 1, sid_tns_num do
			if ref_data_sid_tns[ii][1] == use_sel_ref_sid then
				sid_tns_list_num = sid_tns_list_num + 1
				sid_tns_list[sid_tns_list_num] = ref_data_sid_tns[ii][2]
			end
		end
	end
	
	copy_from_sid_tns_list(in_rte)
end

function copy_from_sid_tns_list(in_rte)
	if in_rte == 0 then
		sid_tns_list_1 = {}
		sid_tns_list_num_1 = 0
		
		if sid_tns_list_num > 0 then
			sid_tns_list_num_1 = sid_tns_list_num
			for ii = 1, sid_tns_list_num_1 do
				sid_tns_list_1[ii] = sid_tns_list[ii]
			end
		end
	elseif in_rte == 1 then
		sid_tns_list_2 = {}
		sid_tns_list_num_2 = 0
		
		if sid_tns_list_num > 0 then
			sid_tns_list_num_2 = sid_tns_list_num
			for ii = 1, sid_tns_list_num_1 do
				sid_tns_list_2[ii] = sid_tns_list[ii]
			end
		end
	end
end

function copy_to_sid_tns_list(in_rte)
	if in_rte == 0 then
		sid_tns_list = {}
		sid_tns_list_num = 0
		
		if sid_tns_list_num_1 > 0 then
			sid_tns_list_num = sid_tns_list_num_1
			for ii = 1, sid_tns_list_num_1 do
				sid_tns_list[ii] = sid_tns_list_1[ii]
			end
		end
	elseif in_rte == 1 then
		sid_tns_list = {}
		sid_tns_list_num = 0
		
		if sid_tns_list_num_2 > 0 then
			sid_tns_list_num = sid_tns_list_num_2
			for ii = 1, sid_tns_list_num_1 do
				sid_tns_list[ii] = sid_tns_list_2[ii]
			end
		end
	end
end

function B738_fmc_dep99(in_mode)
	local exec_light_in = B738DR_fmc_exec_lights
	local use_rte12_sel = rte12_sel
	local use_rte_header_str = rte_header_str
	local use_rte_option_str = rte_option_str
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
		use_rte12_sel = rte12_sel_fo
		use_rte_header_str = rte_header_str_fo
		use_rte_option_str = rte_option_str_fo
	end
	
	if page_dep == 1 then
		
		copy_to_rnw_list(use_rte12_sel)
		copy_to_sid_list(use_rte12_sel)
		copy_to_sid_tns_list(use_rte12_sel)
		
		clr_repeat = 1
		
		local ii = 0
		local jj = 0
		local kk = 0
		local ll = 0
		
		local max_page_rwy = 0
		local max_page_sid = 0
		local max_page_tns = 0
		
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		
		local sid_len = 0
		
		local temp_str1 = ""
		local temp_str2 = ""
		
		local sel_act = ""
		
		local use_ref_rwy = ""
		local use_sel_ref_rwy = ""
		local use_ref_sid = ""
		local use_sel_ref_sid = ""
		local use_ref_sid_tns = ""
		local use_sel_ref_sid_tns = ""
		
		if use_rte12_sel == 0 then
			use_ref_rwy = ref_rwy2
			use_sel_ref_rwy = sel_ref_rwy2
			use_ref_sid = ref_sid2
			use_sel_ref_sid = sel_ref_sid2
			use_ref_sid_tns = ref_sid_tns2
			use_sel_ref_sid_tns = sel_ref_sid_tns2
		else
			use_ref_rwy = ref_rwy8
			use_sel_ref_rwy = sel_ref_rwy8
			use_ref_sid = ref_sid8
			use_sel_ref_sid = sel_ref_sid8
			use_ref_sid_tns = ref_sid_tns8
			use_sel_ref_sid_tns = sel_ref_sid_tns8
		end
		
		if legs_num < 2 or rte12_act ~= use_rte12_sel then
			sel_act = "<SEL>"
		else
			if legs_delete == 0 then
				sel_act= "<ACT>"
			else
				sel_act = "<SEL>"
			end
		end
		
		raw_table('ref_rwy_sel_tmp')
		local ref_rwy_sel_tmp = {true, true, true, true, true}
		ref_rwy_sel_tmp[1] = ""
		ref_rwy_sel_tmp[2] = ""
		ref_rwy_sel_tmp[3] = ""
		ref_rwy_sel_tmp[4] = ""
		ref_rwy_sel_tmp[5] = ""

		raw_table('ref_sid_sel_tmp')
		local ref_sid_sel_tmp = {true, true, true, true, true}
		ref_sid_sel_tmp[1] = ""
		ref_sid_sel_tmp[2] = ""
		ref_sid_sel_tmp[3] = ""
		ref_sid_sel_tmp[4] = ""
		ref_sid_sel_tmp[5] = ""

		raw_table('ref_tns_sel_tmp')
		local ref_tns_sel_tmp = {true, true, true, true, true}
		ref_tns_sel_tmp[1] = ""
		ref_tns_sel_tmp[2] = ""
		ref_tns_sel_tmp[3] = ""
		ref_tns_sel_tmp[4] = ""
		ref_tns_sel_tmp[5] = ""

		-- Create Runways
		jj = math.floor(ref_rnw_list_num / 5)
		kk = ref_rnw_list_num % 5
		if kk > 0 then
			max_page_rwy = jj + 1
		else
			max_page_rwy = jj
		end
		
		if use_sel_ref_rwy == "-----" then
			kk = (act_page_buf - 1) * 5
			for ii = 1, 5 do
				jj = kk + ii
				if jj > ref_rnw_list_num then
					right_line[ii] = ""
					ref_rwy_sel_tmp[ii] = ""
				else
					if use_ref_rwy == ref_rnw_list[jj][2] then
						right_line[ii] = sel_act .. "   " .. ref_rnw_list[jj][2]
					else
						right_line[ii] = ref_rnw_list[jj][2]
					end
					ref_rwy_sel_tmp[ii] = ref_rnw_list[jj][2]
				end
			end
		else
			right_line[1] = sel_act .. "   " .. use_ref_rwy
			max_page_rwy = 1
		end
		
		-- Create SIDs
		jj = math.floor(sid_list_num / 5)
		kk = sid_list_num % 5
		if kk > 0 then
			max_page_sid = jj + 1
		else
			max_page_sid = jj
		end
		if use_sel_ref_sid == "------" then
			kk = (act_page_buf - 1) * 5
			for ii = 1, 5 do
				jj = kk + ii
				if jj > sid_list_num then
					if sid_list_num == 0 and ii == 1 then
						left_line[ii] = "-NONE-"
					else
						left_line[ii] = ""
					end
					ref_sid_sel_tmp[ii] = ""
				else
					if use_ref_sid == sid_list[jj][1] then
						left_line[ii] = sid_list[jj][1].. " " .. sel_act
					else
						left_line[ii] = sid_list[jj][1]
					end
					ref_sid_sel_tmp[ii] = sid_list[jj][1]
				end
			end
		else
			left_line[1] = use_ref_sid .. " " .. sel_act
			max_page_sid = 1
		end
		if sid_list_num == 0 then
			left_line[1] = "-NONE-"
		end
		
		-- Create Transitions
		if use_sel_ref_sid ~= "------" then
			line2_x = " TRANS"
			if sid_tns_list_num == 0 then
				ref_tns_sel_tmp[1] = ""
				ref_tns_sel_tmp[2] = ""
				ref_tns_sel_tmp[3] = ""
				ref_tns_sel_tmp[4] = ""
				ref_tns_sel_tmp[5] = ""
				left_line[2] = "-NONE-"
			else
				jj = math.floor(sid_tns_list_num / 4)
				kk = sid_tns_list_num % 4
				if kk > 0 then
					max_page_tns = jj + 1
				else
					max_page_tns = jj
				end
				if use_sel_ref_sid_tns == "------" then
					kk = (act_page_buf - 1) * 4
					for ii = 2, 5 do
						jj = kk + ii - 1
						if jj > sid_tns_list_num then
							left_line[ii] = ""
							ref_tns_sel_tmp[ii] = ""
						else
							if use_ref_sid_tns == sid_tns_list[jj] then
								left_line[ii] = sid_tns_list[jj] .. " " .. sel_act
							else
								left_line[ii] = sid_tns_list[jj]
							end
							ref_tns_sel_tmp[ii] = sid_tns_list[jj]
						end
					end
				else
					left_line[2] = use_ref_sid_tns .. " " .. sel_act
					max_page_tns = 1
				end
			end
		end
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 12 then
				for jj = sid_len, 11 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			sid_len = string.len(right_line[ii])
			if sid_len < 12 then
				for jj = sid_len, 11 do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		max_page_buf = math.max(1, max_page_rwy, max_page_sid, max_page_tns)
		
		if use_rte12_sel == 0 then
			line0_l = "   " .. ref_icao_x
		else
			line0_l = "   " .. ref_icao7_x
		end
		line0_l = line0_l .. " DEPARTURES      "
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		if B738DR_no_rte2 == 0 then
			line1_x =  " SIDS            RUNWAYS"
			line1_lx = "         " .. use_rte_header_str .. "          "
		else
			line1_x = " SIDS            RUNWAYS"
		end
		if exec_light_in == 1 and legs_num > 1 then
			line6_l = "<ERASE            ROUTE>"
		else
			line6_l = "                  ROUTE>"
		end
		
		if in_mode == 0 then
			for ii = 1, 5 do
				ref_rwy_sel[ii] = ref_rwy_sel_tmp[ii]
				ref_sid_sel[ii] = ref_sid_sel_tmp[ii]
				ref_tns_sel[ii] = ref_tns_sel_tmp[ii]
			end
		else
			for ii = 1, 5 do
				ref_rwy_sel_fo[ii] = ref_rwy_sel_tmp[ii]
				ref_sid_sel_fo[ii] = ref_sid_sel_tmp[ii]
				ref_tns_sel_fo[ii] = ref_tns_sel_tmp[ii]
			end
		end
	end
end


function create_star_list(in_rte)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local rnw_temp = ""
	local temp_str1 = ""
	local temp_str2 = ""
	local sid_len = 0
	local app_no_rwy = 0
	
	local use_sel_des_app = ""
	local use_arr_data = 0
	local use_des_app_rw_only = 0
	
	if in_rte == 0 then
		use_sel_des_app = sel_des_app2
		use_arr_data = arr_data_x
		use_des_app_rw_only = des_app_rw_only_x
	else
		use_sel_des_app = sel_des_app8
		use_arr_data = arr_data7_x
		use_des_app_rw_only = des_app_rw_only7_x
	end
	
	copy_to_des_data(in_rte)
	
	star_list = {}
	star_list_num = 0
	
	if use_arr_data == 0 then	-- Des ICAO
	
		if data_des_star_n > 0 then
			-- Create list of STARs
			for ii = 1, data_des_star_n do
				if use_sel_des_app == "------" or use_des_app_rw_only == 1 then
					if use_sel_des_app == "------" then
						star_list_num = star_list_num + 1
						star_list[star_list_num] = {true, true}
						star_list[star_list_num][1] = data_des_star[ii][1]
						star_list[star_list_num][2] = data_des_star[ii][2]
					else
					-- STARs by RW
						rnw_temp = use_sel_des_app
						if string.sub(data_des_star[ii][2],-1,-1) == "B" then
							temp_str1 = string.sub(data_des_star[ii][2],1,-2)
							temp_str2 = string.sub(rnw_temp,1,-2)
						else
							temp_str1 = "A"
							temp_str2 = "B"
						end
						if rnw_temp == data_des_star[ii][2] or data_des_star[ii][2] == "ALL" or temp_str1 == temp_str2 then
							star_list_num = star_list_num + 1
							star_list[star_list_num] = {true, true}
							star_list[star_list_num][1] = data_des_star[ii][1]
							star_list[star_list_num][2] = data_des_star[ii][2]
						end
					end
				else
				-- STARs by APP
					rnw_temp = ""
					if string.len(use_sel_des_app) > 4 then
						jj, kk = string.find(use_sel_des_app, "-")
						if jj == nil then
							rnw_temp = "RW" .. string.sub(use_sel_des_app, 2, -2)
						else
							rnw_temp = "RW" .. string.sub(use_sel_des_app, 2, jj-1)
						end
					else
						rnw_temp = "RW" .. string.sub(use_sel_des_app, 2, -1)
					end
					
					app_no_rwy = 0
					if string.len(rnw_temp) == 4 then
						if tonumber(string.sub(rnw_temp, 3, -1)) == nil then
							app_no_rwy = 1
						end
					else
						if tonumber(string.sub(rnw_temp, 3, -2)) == nil then
							app_no_rwy = 1
						end
					end
					
					if string.sub(data_des_star[ii][2],-1,-1) == "B" then
						temp_str1 = string.sub(data_des_star[ii][2],1,-2)
						temp_str2 = string.sub(rnw_temp,1,-2)
					else
						temp_str1 = "A"
						temp_str2 = "B"
					end
					if rnw_temp == data_des_star[ii][2] or data_des_star[ii][2] == "ALL" or temp_str1 == temp_str2 or app_no_rwy == 1 then
						star_list_num = star_list_num + 1
						star_list[star_list_num] = {true, true}
						star_list[star_list_num][1] = data_des_star[ii][1]
						star_list[star_list_num][2] = data_des_star[ii][2]
					end
				end
			end
		end
		
		-- Create the one SID for more runways
		if star_list_num > 1 then
			
			temp_list = {}
			temp_list_num = 0
			
			temp_list_num = temp_list_num + 1
			temp_list[temp_list_num] = {true, true}
			temp_list[temp_list_num][1] = star_list[1][1]
			temp_list[temp_list_num][2] = star_list[1][2]
			temp_str1 = star_list[1][1]
			
			for ii = 2, star_list_num do
				if star_list[ii][1] ~= temp_str1 then
					temp_list_num = temp_list_num + 1
					temp_list[temp_list_num] = {true, true}
					temp_list[temp_list_num][1] = star_list[ii][1]
					temp_list[temp_list_num][2] = star_list[ii][2]
					temp_str1 = star_list[ii][1]
				end
			end
			
			star_list = {}
			star_list_num = 0
			for ii = 1, temp_list_num do
				star_list_num = star_list_num + 1
				star_list[star_list_num] = {true, true}
				star_list[star_list_num][1] = temp_list[ii][1]
				star_list[star_list_num][2] = temp_list[ii][2]
			end
		end
	
	elseif use_arr_data == 1 then	-- Ref ICAO
		
		if star_num > 0 then
			-- Create list of STARs
			for ii = 1, star_num do
				if use_sel_des_app == "------" or use_des_app_rw_only == 1 then
					if use_sel_des_app == "------" then
						star_list_num = star_list_num + 1
						star_list[star_list_num] = {true, true}
						star_list[star_list_num][1] = ref_data_star[ii][1]
						star_list[star_list_num][2] = ref_data_star[ii][2]
					else
					-- STARs by RW
						rnw_temp = use_sel_des_app
						if string.sub(ref_data_star[ii][2],-1,-1) == "B" then
							temp_str1 = string.sub(ref_data_star[ii][2],1,-2)
							temp_str2 = string.sub(rnw_temp,1,-2)
						else
							temp_str1 = "A"
							temp_str2 = "B"
						end
						if rnw_temp == ref_data_star[ii][2] or ref_data_star[ii][2] == "ALL" or temp_str1 == temp_str2 then
							star_list_num = star_list_num + 1
							star_list[star_list_num] = {true, true}
							star_list[star_list_num][1] = ref_data_star[ii][1]
							star_list[star_list_num][2] = ref_data_star[ii][2]
						end
					end
				else
				-- STARs by APP
					rnw_temp = ""
					if string.len(use_sel_des_app) > 4 then
						jj, kk = string.find(use_sel_des_app, "-")
						if jj == nil then
							rnw_temp = "RW" .. string.sub(use_sel_des_app, 2, -2)
						else
							rnw_temp = "RW" .. string.sub(use_sel_des_app, 2, jj-1)
						end
					else
						rnw_temp = "RW" .. string.sub(use_sel_des_app, 2, -1)
					end
					if string.sub(ref_data_star[ii][2],-1,-1) == "B" then
						temp_str1 = string.sub(ref_data_star[ii][2],1,-2)
						temp_str2 = string.sub(rnw_temp,1,-2)
					else
						temp_str1 = "A"
						temp_str2 = "B"
					end
					if rnw_temp == ref_data_star[ii][2] or ref_data_star[ii][2] == "ALL" or temp_str1 == temp_str2 then
						star_list_num = star_list_num + 1
						star_list[star_list_num] = {true, true}
						star_list[star_list_num][1] = ref_data_star[ii][1]
						star_list[star_list_num][2] = ref_data_star[ii][2]
					end
				end
			end
			
			-- Create the one SID for more runways
			if star_list_num > 1 then
				
				temp_list = {}
				temp_list_num = 0
				
				temp_list_num = temp_list_num + 1
				temp_list[temp_list_num] = {true, true}
				temp_list[temp_list_num][1] = star_list[1][1]
				temp_list[temp_list_num][2] = star_list[1][2]
				temp_str1 = star_list[1][1]
				
				for ii = 2, star_list_num do
					if star_list[ii][1] ~= temp_str1 then
						temp_list_num = temp_list_num + 1
						temp_list[temp_list_num] = {true, true}
						temp_list[temp_list_num][1] = star_list[ii][1]
						temp_list[temp_list_num][2] = star_list[ii][2]
						temp_str1 = star_list[ii][1]
					end
				end
				
				star_list = {}
				star_list_num = 0
				for ii = 1, temp_list_num do
					star_list_num = star_list_num + 1
					star_list[star_list_num] = {true, true}
					star_list[star_list_num][1] = temp_list[ii][1]
					star_list[star_list_num][2] = temp_list[ii][2]
				end
			end
		end
	end
	
	copy_from_star_list(in_rte)
end

function copy_from_star_list(in_rte)
	if in_rte == 0 then
		star_list_1 = {}
		star_list_num_1 = 0
		
		if star_list_num > 0 then
			star_list_num_1 = star_list_num
			for ii = 1, star_list_num do
				star_list_1[ii] = {true, true}
				for jj = 1, 2 do
					star_list_1[ii][jj] = star_list[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		star_list_2 = {}
		star_list_num_2 = 0
		
		if star_list_num > 0 then
			star_list_num_2 = star_list_num
			for ii = 1, star_list_num do
				star_list_2[ii] = {true, true}
				for jj = 1, 2 do
					star_list_2[ii][jj] = star_list[ii][jj]
				end
			end
		end
	end
end

function copy_to_star_list(in_rte)
	if in_rte == 0 then
		star_list = {}
		star_list_num = 0
		
		if star_list_num_1 > 0 then
			star_list_num = star_list_num_1
			for ii = 1, star_list_num do
				star_list[ii] = {true, true}
				for jj = 1, 2 do
					star_list[ii][jj] = star_list_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		star_list = {}
		star_list_num = 0
		
		if star_list_num_2 > 0 then
			star_list_num = star_list_num_2
			for ii = 1, star_list_num do
				star_list[ii] = {true, true}
				for jj = 1, 2 do
					star_list[ii][jj] = star_list_2[ii][jj]
				end
			end
		end
	end
end

function create_star_tns_list(in_rte)
	
	local ii = 0
	
	local use_sel_des_star = ""
	local use_arr_data = 0
	
	if in_rte == 0 then
		use_sel_des_star = sel_des_star2
		use_arr_data = arr_data_x
	else
		use_sel_des_star = sel_des_star8
		use_arr_data = arr_data7_x
	end
	
	copy_to_des_data(in_rte)
	
	star_tns_list = {}
	star_tns_list_num = 0
	
	if use_arr_data == 0 then	-- Des ICAO
		if data_des_star_tns_n > 0 then
			for ii = 1, data_des_star_tns_n do
				if data_des_star_tns[ii][1] == use_sel_des_star then
					star_tns_list_num = star_tns_list_num + 1
					star_tns_list[star_tns_list_num] = data_des_star_tns[ii][2]
				end
			end
		end
	
	elseif use_arr_data == 1 then	-- Ref ICAO
		if ref_data_star_tns_n > 0 then
			for ii = 1, ref_data_star_tns_n do
				if ref_data_star_tns[ii][1] == use_sel_des_star then
					star_tns_list_num = star_tns_list_num + 1
					star_tns_list[star_tns_list_num] = ref_data_star_tns[ii][2]
				end
			end
		end
	end
	
	copy_from_star_tns_list(in_rte)
end

function copy_from_star_tns_list(in_rte)
	if in_rte == 0 then
		star_tns_list_1 = {}
		star_tns_list_num_1 = 0
		
		if star_tns_list_num > 0 then
			star_tns_list_num_1 = star_tns_list_num
			for ii = 1, star_tns_list_num do
				star_tns_list_1[ii] = star_tns_list[ii]
			end
		end
	elseif in_rte == 1 then
		star_tns_list_2 = {}
		star_tns_list_num_2 = 0
		
		if star_tns_list_num > 0 then
			star_tns_list_num_2 = star_tns_list_num
			for ii = 1, star_tns_list_num do
				star_tns_list_2[ii] = star_tns_list[ii]
			end
		end
	end
end

function copy_to_star_tns_list(in_rte)
	if in_rte == 0 then
		star_tns_list = {}
		star_tns_list_num = 0
		
		if star_tns_list_num_1 > 0 then
			star_tns_list_num = star_tns_list_num_1
			for ii = 1, star_tns_list_num do
				star_tns_list[ii] = star_tns_list_1[ii]
			end
		end
	elseif in_rte == 1 then
		star_tns_list = {}
		star_tns_list_num = 0
		
		if star_tns_list_num_2 > 0 then
			star_tns_list_num = star_tns_list_num_2
			for ii = 1, star_tns_list_num do
				star_tns_list[ii] = star_tns_list_2[ii]
			end
		end
	end
end

function create_app_list(in_rte)
	
	local ii = 0
	local jj = 0
	local kk = 0
	local rnw_temp = ""
	local rnw_temp2 = ""
	local temp_str1 = ""
	local temp_str2 = ""
	local sid_len = 0
	local rw_numeric = 0
	local app_no_rwy = 0
	
	local use_sel_des_star = ""
	local use_arr_data = 0
	
	if in_rte == 0 then
		use_sel_des_star = sel_des_star2
		use_arr_data = arr_data_x
	else
		use_sel_des_star = sel_des_star8
		use_arr_data = arr_data7_x
	end
	
	copy_to_des_data(in_rte)
	
	app_list = {}
	app_list_num = 0
	
	if use_arr_data == 0 then	-- Des ICAO
	
		if data_des_app_n > 0 then
			-- Create list of APPs
			for ii = 1, data_des_app_n do
				if use_sel_des_star == "------" then
					app_list_num = app_list_num + 1
					app_list[app_list_num] = {true, true, true}
					rnw_temp = ""
					if string.len(data_des_app[ii]) > 4 then
						jj, kk = string.find(data_des_app[ii], "-")
						if jj == nil then
							rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, -2)
						else
							rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, jj-1)
						end
					else
						rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, -1)
					end
					app_list[app_list_num][1] = data_des_app[ii]
					app_list[app_list_num][2] = rnw_temp
					if string.len(rnw_temp) > 4 then
						temp_str1 = string.sub(rnw_temp, 3, -2)
					else
						temp_str1 = string.sub(rnw_temp, 3, -1)
					end
					rw_numeric = tonumber(temp_str1)
					if rw_numeric == nil then
						app_list[app_list_num][3] = 0
					else
						app_list[app_list_num][3] = rw_numeric
					end
				else
				-- APPs by STAR
					rnw_temp2 = ""
					rnw_temp = ""
					if string.len(data_des_app[ii]) > 4 then
						jj, kk = string.find(data_des_app[ii], "-")
						if jj == nil then
							rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, -2)
						else
							rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, jj-1)
						end
					else
						rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, -1)
					end
					for jj = 1, data_des_star_n do
						if data_des_star[jj][1] == use_sel_des_star then
							rnw_temp2 = data_des_star[jj][2]
							if string.sub(rnw_temp2,-1,-1) == "B" then
								temp_str1 = string.sub(rnw_temp2,1,-2)
								temp_str2 = string.sub(rnw_temp,1,-2)
							else
								temp_str1 = "A"
								temp_str2 = "B"
							end
							
							app_no_rwy = 0
							if string.len(rnw_temp) == 4 then
								if tonumber(string.sub(rnw_temp, 3, -1)) == nil then
									app_no_rwy = 1
								end
							else
								if tonumber(string.sub(rnw_temp, 3, -2)) == nil then
									app_no_rwy = 1
								end
							end
							
							if rnw_temp == rnw_temp2 or rnw_temp2 == "ALL" or temp_str1 == temp_str2 or app_no_rwy == 1 then
								app_list_num = app_list_num + 1
								app_list[app_list_num] = {true, true, true}
								app_list[app_list_num][1] = data_des_app[ii]
								app_list[app_list_num][2] = rnw_temp
								if string.len(rnw_temp) > 4 then
									temp_str1 = string.sub(rnw_temp, 3, -2)
								else
									temp_str1 = string.sub(rnw_temp, 3, -1)
								end
								rw_numeric = tonumber(temp_str1)
								if rw_numeric == nil then
									app_list[app_list_num][3] = 0
								else
									app_list[app_list_num][3] = rw_numeric
								end
							end
						end
					end
				end
			end
			
		end
			
		if des_rnw_list_num > 0 then
			if use_sel_des_star == "------" then
				for ii = 1, des_rnw_list_num do
					app_list_num = app_list_num + 1
					app_list[app_list_num] = {true, true, true}
					app_list[app_list_num][1] = "RW" .. des_rnw_list[ii][2]
					app_list[app_list_num][2] = "RW" .. des_rnw_list[ii][2]
					if string.len(des_rnw_list[ii][2]) > 2 then
						temp_str1 = string.sub(des_rnw_list[ii][2], 1, -2)
					else
						temp_str1 = des_rnw_list[ii][2]
					end
					app_list[app_list_num][3] = tonumber(temp_str1)
				end
			else
			-- APPs by STAR
				for ii = 1, des_rnw_list_num do
					rnw_temp2 = ""
					rnw_temp = "RW" .. des_rnw_list[ii][2]
					for jj = 1, data_des_star_n do
						if data_des_star[jj][1] == use_sel_des_star then
							rnw_temp2 = data_des_star[jj][2]
							if string.sub(rnw_temp2,-1,-1) == "B" then
								temp_str1 = string.sub(rnw_temp2,1,-2)
								temp_str2 = string.sub(rnw_temp,1,-2)
							else
								temp_str1 = "A"
								temp_str2 = "B"
							end
							if rnw_temp == rnw_temp2 or rnw_temp2 == "ALL" or temp_str1 == temp_str2 then
								app_list_num = app_list_num + 1
								app_list[app_list_num] = {true, true, true}
								app_list[app_list_num][1] = rnw_temp
								app_list[app_list_num][2] = rnw_temp
								if string.len(des_rnw_list[ii][2]) > 2 then
									temp_str1 = string.sub(des_rnw_list[ii][2], 1, -2)
								else
									temp_str1 = des_rnw_list[ii][2]
								end
								app_list[app_list_num][3] = tonumber(temp_str1)
							end
						end
					end
				end
			end
		end
		
	elseif use_arr_data == 1 then	-- Ref ICAO
		if ref_data_app_n > 0 then
			-- Create list of APPs
			for ii = 1, ref_data_app_n do
				if use_sel_des_star == "------" then
					app_list_num = app_list_num + 1
					app_list[app_list_num] = {true, true, true}
					rnw_temp = ""
					if string.len(ref_data_app[ii]) > 4 then
						jj, kk = string.find(ref_data_app[ii], "-")
						if jj == nil then
							rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, -2)
						else
							rnw_temp = "RW" .. string.sub(ref_data_app[ii], 2, jj-1)
						end
					else
						rnw_temp = "RW" .. string.sub(ref_data_app[ii], 2, -1)
					end
					app_list[app_list_num][1] = ref_data_app[ii]
					app_list[app_list_num][2] = rnw_temp
					if string.len(rnw_temp) > 4 then
						temp_str1 = string.sub(rnw_temp, 3, -2)
					else
						temp_str1 = string.sub(rnw_temp, 3, -1)
					end
					rw_numeric = tonumber(temp_str1)
					if rw_numeric == nil then
						app_list[app_list_num][3] = 0
					else
						app_list[app_list_num][3] = rw_numeric
					end
				else
				-- APPs by STAR
					rnw_temp2 = ""
					rnw_temp = ""
					if string.len(ref_data_app[ii]) > 4 then
						jj, kk = string.find(ref_data_app[ii], "-")
						if jj == nil then
							rnw_temp = "RW" .. string.sub(data_des_app[ii], 2, -2)
						else
							rnw_temp = "RW" .. string.sub(ref_data_app[ii], 2, jj-1)
						end
					else
						rnw_temp = "RW" .. string.sub(ref_data_app[ii], 2, -1)
					end
					for jj = 1, star_num do
						if ref_data_star[jj][1] == use_sel_des_star then
							rnw_temp2 = ref_data_star[jj][2]
							if string.sub(rnw_temp2,-1,-1) == "B" then
								temp_str1 = string.sub(rnw_temp2,1,-2)
								temp_str2 = string.sub(rnw_temp,1,-2)
							else
								temp_str1 = "A"
								temp_str2 = "B"
							end
							if rnw_temp == rnw_temp2 or rnw_temp2 == "ALL" or temp_str1 == temp_str2 then
								app_list_num = app_list_num + 1
								app_list[app_list_num] = {true, true, true}
								app_list[app_list_num][1] = ref_data_app[ii]
								app_list[app_list_num][2] = rnw_temp
								if string.len(rnw_temp) > 4 then
									temp_str1 = string.sub(rnw_temp, 3, -2)
								else
									temp_str1 = string.sub(rnw_temp, 3, -1)
								end
								rw_numeric = tonumber(temp_str1)
								if rw_numeric == nil then
									app_list[app_list_num][3] = 0
								else
									app_list[app_list_num][3] = rw_numeric
								end
							end
						end
					end
				end
			end
			
		end
		
		if ref_rnw_list_num2 > 0 then
			if use_sel_des_star == "------" then
				for ii = 1, ref_rnw_list_num2 do
					app_list_num = app_list_num + 1
					app_list[app_list_num] = {true, true, true}
					app_list[app_list_num][1] = "RW" .. ref_rnw_list2[ii][2]
					app_list[app_list_num][2] = "RW" .. ref_rnw_list2[ii][2]
					if string.len(ref_rnw_list2[ii][2]) > 2 then
						temp_str1 = string.sub(ref_rnw_list2[ii][2], 1, -2)
					else
						temp_str1 = ref_rnw_list2[ii][2]
					end
					app_list[app_list_num][3] = tonumber(temp_str1)
				end
			else
			-- APPs by STAR
				for ii = 1, ref_rnw_list_num2 do
					rnw_temp2 = ""
					rnw_temp = "RW" .. ref_rnw_list2[ii][2]
					for jj = 1, data_des_star_n do
						if data_des_star[jj][1] == use_sel_des_star then
							rnw_temp2 = data_des_star[jj][2]
							if string.sub(rnw_temp2,-1,-1) == "B" then
								temp_str1 = string.sub(rnw_temp2,1,-2)
								temp_str2 = string.sub(rnw_temp,1,-2)
							else
								temp_str1 = "A"
								temp_str2 = "B"
							end
							if rnw_temp == rnw_temp2 or rnw_temp2 == "ALL" or temp_str1 == temp_str2 then
								app_list_num = app_list_num + 1
								app_list[app_list_num] = {true, true, true}
								app_list[app_list_num][1] = rnw_temp
								app_list[app_list_num][2] = rnw_temp
								if string.len(ref_rnw_list2[ii][2]) > 2 then
									temp_str1 = string.sub(ref_rnw_list2[ii][2], 1, -2)
								else
									temp_str1 = ref_rnw_list2[ii][2]
								end
								app_list[app_list_num][3] = tonumber(temp_str1)
							end
						end
					end
				end
			end
		end
	
	end
	
	copy_from_app_list(in_rte)
end

function copy_from_app_list(in_rte)
	if in_rte == 0 then
		app_list_1 = {}
		app_list_num_1 = 0
		
		if app_list_num > 0 then
			app_list_num_1 = app_list_num
			for ii = 1, app_list_num do
				app_list_1[ii] = {true, true, true}
				for jj = 1, 3 do
					app_list_1[ii][jj] = app_list[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		app_list_2 = {}
		app_list_num_2 = 0
		
		if app_list_num > 0 then
			app_list_num_2 = app_list_num
			for ii = 1, app_list_num do
				app_list_2[ii] = {true, true, true}
				for jj = 1, 3 do
					app_list_2[ii][jj] = app_list[ii][jj]
				end
			end
		end
	end
end

function copy_to_app_list(in_rte)
	if in_rte == 0 then
		app_list = {}
		app_list_num = 0
		
		if app_list_num_1 > 0 then
			app_list_num = app_list_num_1
			for ii = 1, app_list_num do
				app_list[ii] = {true, true, true}
				for jj = 1, 3 do
					app_list[ii][jj] = app_list_1[ii][jj]
				end
			end
		end
	elseif in_rte == 1 then
		app_list = {}
		app_list_num = 0
		
		if app_list_num_2 > 0 then
			app_list_num = app_list_num_2
			for ii = 1, app_list_num do
				app_list[ii] = {true, true, true}
				for jj = 1, 3 do
					app_list[ii][jj] = app_list_2[ii][jj]
				end
			end
		end
	end
end

function create_app_tns_list(in_rte)
	
	local ii = 0
	
	local use_sel_des_app = ""
	local use_arr_data = 0
	
	if in_rte == 0 then
		use_sel_des_app = sel_des_app2
		use_arr_data = arr_data_x
	else
		use_sel_des_app = sel_des_app8
		use_arr_data = arr_data7_x
	end
	
	copy_to_des_data(in_rte)
	
	app_tns_list = {}
	app_tns_list_num = 0
	
	if use_arr_data == 0 then	-- Des ICAO
		if data_des_app_tns_n > 0 then
			for ii = 1, data_des_app_tns_n do
				if data_des_app_tns[ii][1] == use_sel_des_app then
					app_tns_list_num = app_tns_list_num + 1
					app_tns_list[app_tns_list_num] = data_des_app_tns[ii][2]
				end
			end
		end
	
	elseif use_arr_data == 1 then	-- Ref ICAO
		if ref_data_app_tns_n > 0 then
			for ii = 1, ref_data_app_tns_n do
				if ref_data_app_tns[ii][1] == use_sel_des_app then
					app_tns_list_num = app_tns_list_num + 1
					app_tns_list[app_tns_list_num] = ref_data_app_tns[ii][2]
				end
			end
		end
	end
	
	copy_from_app_tns_list(in_rte)
end

function copy_from_app_tns_list(in_rte)
	if in_rte == 0 then
		app_tns_list_1 = {}
		app_tns_list_num_1 = 0
		
		if app_tns_list_num > 0 then
			app_tns_list_num_1 = app_tns_list_num
			for ii = 1, app_tns_list_num do
				app_tns_list_1[ii] = app_tns_list[ii]
			end
		end
	elseif in_rte == 1 then
		app_tns_list_2 = {}
		app_tns_list_num_2 = 0
		
		if app_tns_list_num > 0 then
			app_tns_list_num_2 = app_tns_list_num
			for ii = 1, app_tns_list_num do
				app_tns_list_2[ii] = app_tns_list[ii]
			end
		end
	end
end

function copy_to_app_tns_list(in_rte)
	if in_rte == 0 then
		app_tns_list = {}
		app_tns_list_num = 0
		
		if app_tns_list_num_1 > 0 then
			app_tns_list_num = app_tns_list_num_1
			for ii = 1, app_tns_list_num do
				app_tns_list[ii] = app_tns_list_1[ii]
			end
		end
	elseif in_rte == 1 then
		app_tns_list = {}
		app_tns_list_num = 0
		
		if app_tns_list_num_2 > 0 then
			app_tns_list_num = app_tns_list_num_2
			for ii = 1, app_tns_list_num do
				app_tns_list[ii] = app_tns_list_2[ii]
			end
		end
	end
end

function B738_fmc_arr99(in_mode)
	
	local exec_light_in = B738DR_fmc_exec_lights
	local use_rte12_sel = rte12_sel
	local use_rte_header_str = rte_header_str
	local use_rte_option_str = rte_option_str
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
		use_rte12_sel = rte12_sel_fo
		use_rte_header_str = rte_header_str_fo
		use_rte_option_str = rte_option_str_fo
	end
	
	if page_arr == 1 then
		
		copy_to_star_list(use_rte12_sel)
		copy_to_star_tns_list(use_rte12_sel)
		copy_to_app_list(use_rte12_sel)
		copy_to_app_tns_list(use_rte12_sel)
		
		clr_repeat = 1
		
		local ii = 0
		local jj = 0
		local kk = 0
		local ll = 0
		
		local max_page_star = 0
		local max_page_star_tns = 0
		local max_page_app = 0
		local max_page_tns = 0
		
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		
		raw_table('left_line_x')
		local left_line_x = {true, true, true, true, true}
		left_line_x[1] = ""
		left_line_x[2] = ""
		left_line_x[3] = ""
		left_line_x[4] = ""
		left_line_x[5] = ""
		raw_table('right_line_x')
		local right_line_x = {true, true, true, true, true}
		right_line_x[1] = ""
		right_line_x[2] = ""
		right_line_x[3] = ""
		right_line_x[4] = ""
		right_line_x[5] = ""
		
		local sid_len = 0
		local right_len = 0
		local temp_str = ""
		local temp_str2 = ""
		local temp_str3 = ""
		local temp_str_rnw = ""
		local temp_str_add = ""
		local temp_str_app = ""
		local temp_str_rnw_len = 0
		
		local sel_act = ""
		
		local use_des_star = ""
		local use_sel_des_star = ""
		local use_des_star_trans = ""
		local use_sel_des_star_trans = ""
		local use_des_app = ""
		local use_sel_des_app = ""
		local use_des_app_tns = ""
		local use_sel_des_app_tns = ""
		local use_des_app_rw_only = 0
		local use_rw_ext_dist = ""
		local use_rw_ext_fpa = 0
		
		if use_rte12_sel == 0 then
			use_des_star = des_star2
			use_sel_des_star = sel_des_star2
			use_des_star_trans = des_star_trans2
			use_sel_des_star_trans = sel_des_star_trans2
			use_des_app = des_app2
			use_sel_des_app = sel_des_app2
			use_des_app_tns = des_app_tns2
			use_sel_des_app_tns = sel_des_app_tns2
			use_des_app_rw_only = des_app_rw_only_x
			use_rw_ext_dist = rw_ext_dist_x
			use_rw_ext_fpa = rw_ext_fpa_x
		else
			use_des_star = des_star8
			use_sel_des_star = sel_des_star8
			use_des_star_trans = des_star_trans8
			use_sel_des_star_trans = sel_des_star_trans8
			use_des_app = des_app8
			use_sel_des_app = sel_des_app8
			use_des_app_tns = des_app_tns8
			use_sel_des_app_tns = sel_des_app_tns8
			use_des_app_rw_only = des_app_rw_only7_x
			use_rw_ext_dist = rw_ext_dist7_x
			use_rw_ext_fpa = rw_ext_fpa7_x
		end
		
		if legs_num < 2 or rte12_act ~= use_rte12_sel then
			sel_act = "<SEL>"
		else
			if legs_delete == 0 then
				sel_act= "<ACT>"
			else
				sel_act = "<SEL>"
			end
		end
		
		raw_table('des_star_sel_tmp')
		local des_star_sel_tmp = {true, true, true, true, true}
		des_star_sel_tmp[1] = ""
		des_star_sel_tmp[2] = ""
		des_star_sel_tmp[3] = ""
		des_star_sel_tmp[4] = ""
		des_star_sel_tmp[5] = ""

		raw_table('des_star_tns_sel_tmp')
		local des_star_tns_sel_tmp = {true, true, true, true, true}
		des_star_tns_sel_tmp[1] = ""
		des_star_tns_sel_tmp[2] = ""
		des_star_tns_sel_tmp[3] = ""
		des_star_tns_sel_tmp[4] = ""
		des_star_tns_sel_tmp[5] = ""

		raw_table('des_app_sel_tmp')
		local des_app_sel_tmp = {true, true, true, true, true}
		des_app_sel_tmp[1] = ""
		des_app_sel_tmp[2] = ""
		des_app_sel_tmp[3] = ""
		des_app_sel_tmp[4] = ""
		des_app_sel_tmp[5] = ""

		raw_table('des_tns_sel_tmp')
		local des_tns_sel_tmp = {true, true, true, true, true}
		des_tns_sel_tmp[1] = ""
		des_tns_sel_tmp[2] = ""
		des_tns_sel_tmp[3] = ""
		des_tns_sel_tmp[4] = ""
		des_tns_sel_tmp[5] = ""
		
		-- Create STARs
		jj = math.floor(star_list_num / 5)
		kk = star_list_num % 5
		if kk > 0 then
			max_page_star = jj + 1
		else
			max_page_star = jj
		end
		
		if use_sel_des_star == "------" then
			kk = (act_page_buf - 1) * 5
			for ii = 1, 5 do
				jj = kk + ii
				if jj > star_list_num then
					left_line[ii] = ""
					des_star_sel_tmp[ii] = ""
				else
					des_star_sel_tmp[ii] = star_list[jj][1]
					left_line[ii] = spaces_after(star_list[jj][1], 6)
					if use_des_star == star_list[jj][1] then
						left_line[ii] = left_line[ii] .. sel_act
					end
				end
			end
		else
			left_line[1] = spaces_after(use_des_star, 6) .. sel_act
			max_page_star = 1
		end
		if star_list_num == 0 then
			left_line[1] = "-NONE-"
		end
		
		-- Create STAR Transitions
		if use_sel_des_star ~= "------" then
			left_line_x[2] = "TRANS"
			if star_tns_list_num == 0 then
				des_star_tns_sel_tmp[1] = ""
				des_star_tns_sel_tmp[2] = ""
				des_star_tns_sel_tmp[3] = ""
				des_star_tns_sel_tmp[4] = ""
				des_star_tns_sel_tmp[5] = ""
				left_line[2] = "-NONE-"
			else
				jj = math.floor(star_tns_list_num / 4)
				kk = star_tns_list_num % 4
				if kk > 0 then
					max_page_star_tns = jj + 1
				else
					max_page_star_tns = jj
				end
				if use_sel_des_star_trans == "------" then
					kk = (act_page_buf - 1) * 4
					for ii = 2, 5 do
						jj = kk + ii - 1
						if jj > star_tns_list_num then
							left_line[ii] = ""
							des_star_tns_sel_tmp[ii] = ""
						else
							left_line[ii] = star_tns_list[jj]
							des_star_tns_sel_tmp[ii] = star_tns_list[jj]
							if use_sel_des_star_trans == star_tns_list[jj] then
								left_line[ii] = left_line[ii] .. " " .. sel_act
							end
						end
					end
				else
					left_line[2] = use_des_star_trans .. " " .. sel_act
					max_page_star_tns = 1
				end
			end
		else
			left_line_x[2] = ""
			max_page_star_tns = 1
		end
		
		-- Create APPs
		jj = math.floor(app_list_num / 5)
		kk = app_list_num % 5
		if kk > 0 then
			max_page_app = jj + 1
		else
			max_page_app = jj
		end
		
		local first_runway = 0
		right_line_x[1] = "APPROACHES"
		if B738DR_no_rte2 == 0 then
			left_line_x[1] = " STARS       "
			line1_lx =       "        " .. use_rte_header_str
		else
			left_line_x[1] = " STARS       "
		end
		
		if use_sel_des_app == "------" then
			kk = (act_page_buf - 1) * 5
			for ii = 1, 5 do
				jj = kk + ii
				if jj > app_list_num then
					right_line[ii] = ""
					des_app_sel_tmp[ii] = ""
				else
					temp_str = string.sub(app_list[jj][1], 1, 1)
					
					temp_str2 = app_list[jj][1]
					temp_str_rnw = string.sub(temp_str2, 2, 3)
					if tonumber(temp_str_rnw) == nil then
						temp_str_rnw = ""
						temp_str_add = ""
						temp_str_app = temp_str2
					else
						if string.len(temp_str2) > 3 then
							if string.sub(temp_str2, 4, 4) == "L" or string.sub(temp_str2, 4, 4) == "R" or string.sub(temp_str2, 4, 4) == "C" then
								temp_str_rnw = temp_str_rnw .. string.sub(temp_str2, 4, 4)
								if string.len(temp_str2) > 4 then
									temp_str_add = string.sub(temp_str2, 5, -1)
								else
									temp_str_add = ""
								end
							else
								temp_str_add = string.sub(temp_str2, 4, -1)
							end
							if string.len(temp_str_add) > 0 then
								temp_str3 = ""
								for aaa = 1, string.len(temp_str_add) do
									if string.sub(temp_str_add, aaa, aaa) ~= "-" then
										temp_str3 = temp_str3 .. string.sub(temp_str_add, aaa, aaa)
									end
								end
								temp_str_add = temp_str3
							end
						end
						if temp_str == "I" then
							temp_str_app = "ILS"
						elseif temp_str == "R" then
							temp_str_app = "RNV"
						elseif temp_str == "D" or temp_str == "S" then
							temp_str_app = "VDM"
						elseif temp_str == "L" then
							temp_str_app = "LOC"
						elseif temp_str == "N" or temp_str == "Q" then
							temp_str_app = "NDB"
						elseif temp_str == "J" then
							temp_str_app = "GLS"
						elseif temp_str == "V" then
							temp_str_app = "VOR"
						elseif temp_str == "X" then
							temp_str_app = "LDA"
						elseif temp_str == "G" then
							temp_str_app = "IGS"
						elseif temp_str == "M" then
							temp_str_app = "MLS"
						elseif temp_str == "B" then
							temp_str_app = "BLOC"
						elseif temp_str == "P" then
							temp_str_app = "GPS"
						elseif temp_str == "T" then
							temp_str_app = "TAC"
						elseif temp_str == "U" then
							temp_str_app = "SDF"
						elseif temp_str == "W" or temp_str == "Y" then
							temp_str_app = "MLS"
						else
							temp_str_app = temp_str2
						end
					end
					
					temp_str_rnw_len = string.len(temp_str_rnw)
					temp_str3 = temp_str_app .. temp_str_add
					
					if string.sub(temp_str2, 1, 2) == "RW" then
						temp_str3 = string.sub(temp_str2, 3, -1)
						if first_runway == 0 then
							right_line_x[ii] = "RUNWAYS"
							first_runway = 1
						end
					elseif temp_str_rnw_len == 2 then
						if string.len(temp_str3) > 6 then
							temp_str3 = string.sub(temp_str3, 1, 6) .. temp_str_rnw
						elseif string.len(temp_str3) == 6 then
							temp_str3 = spaces_after(temp_str3, 6) .. temp_str_rnw
						else
							temp_str3 = temp_str3 .. " " .. temp_str_rnw
						end
					elseif temp_str_rnw_len == 3 then
						if string.len(temp_str3) > 5 then
							temp_str3 = string.sub(temp_str3, 1, 5) .. temp_str_rnw
						elseif string.len(temp_str3) == 5 then
							temp_str3 = spaces_after(temp_str3, 5) .. temp_str_rnw
						else
							temp_str3 = temp_str3 .. " " .. temp_str_rnw
						end
					else
						temp_str3 = temp_str_app
					end
					
					if string.len(temp_str3) > 8 then
						temp_str3 = string.sub(temp_str3, 1, 8)
					end
					
					if use_des_app == app_list[jj][1] then
						right_line[ii] = sel_act .. temp_str3
					else
						right_line[ii] = "     " .. temp_str3
					end
					
					des_app_sel_tmp[ii] = app_list[jj][1]
				end
			end
		else
			if des_app_exec == 1 then
				temp_str = string.sub(use_des_app, 1, 1)
				
				temp_str2 = use_des_app
				temp_str_rnw = string.sub(temp_str2, 2, 3)
				if tonumber(temp_str_rnw) == nil then
					temp_str_rnw = ""
					temp_str_add = ""
					temp_str_app = temp_str2
				else
					if string.len(temp_str2) > 3 then
						if string.sub(temp_str2, 4, 4) == "L" or string.sub(temp_str2, 4, 4) == "R" or string.sub(temp_str2, 4, 4) == "C" then
							temp_str_rnw = temp_str_rnw .. string.sub(temp_str2, 4, 4)
							if string.len(temp_str2) > 4 then
								temp_str_add = string.sub(temp_str2, 5, -1)
							else
								temp_str_add = ""
							end
						else
							temp_str_add = string.sub(temp_str2, 4, -1)
						end
						if string.len(temp_str_add) > 0 then
							temp_str3 = ""
							for aaa = 1, string.len(temp_str_add) do
								if string.sub(temp_str_add, aaa, aaa) ~= "-" then
									temp_str3 = temp_str3 .. string.sub(temp_str_add, aaa, aaa)
								end
							end
							temp_str_add = temp_str3
						end
					end
					if temp_str == "I" then
						temp_str_app = "ILS"
					elseif temp_str == "R" then
						temp_str_app = "RNV"
					elseif temp_str == "D" or temp_str == "S" then
						temp_str_app = "VDM"
					elseif temp_str == "L" then
						temp_str_app = "LOC"
					elseif temp_str == "N" or temp_str == "Q" then
						temp_str_app = "NDB"
					elseif temp_str == "J" then
						temp_str_app = "GLS"
					elseif temp_str == "V" then
						temp_str_app = "VOR"
					elseif temp_str == "X" then
						temp_str_app = "LDA"
					elseif temp_str == "G" then
						temp_str_app = "IGS"
					elseif temp_str == "M" then
						temp_str_app = "MLS"
					elseif temp_str == "B" then
						temp_str_app = "BLOC"
					elseif temp_str == "P" then
						temp_str_app = "GPS"
					elseif temp_str == "T" then
						temp_str_app = "TAC"
					elseif temp_str == "U" then
						temp_str_app = "SDF"
					elseif temp_str == "W" or temp_str == "Y" then
						temp_str_app = "MLS"
					else
						temp_str_app = temp_str2
					end
				end
				
				temp_str_rnw_len = string.len(temp_str_rnw)
				temp_str3 = temp_str_app .. temp_str_add
				
				if string.sub(temp_str2, 1, 2) == "RW" then
					temp_str3 = string.sub(temp_str2, 3, -1)
					if first_runway == 0 then
						right_line_x[ii] = "RUNWAYS"
						first_runway = 1
					end
				elseif temp_str_rnw_len == 2 then
					if string.len(temp_str3) > 6 then
						temp_str3 = string.sub(temp_str3, 1, 6) .. temp_str_rnw
					elseif string.len(temp_str3) == 6 then
						temp_str3 = spaces_after(temp_str3, 6) .. temp_str_rnw
					else
						temp_str3 = temp_str3 .. " " .. temp_str_rnw
					end
				elseif temp_str_rnw_len == 3 then
					if string.len(temp_str3) > 5 then
						temp_str3 = string.sub(temp_str3, 1, 5) .. temp_str_rnw
					elseif string.len(temp_str3) == 5 then
						temp_str3 = spaces_after(temp_str3, 5) .. temp_str_rnw
					else
						temp_str3 = temp_str3 .. " " .. temp_str_rnw
					end
				else
					temp_str3 = temp_str_app
				end
				
				if string.len(temp_str3) > 8 then
					temp_str3 = string.sub(temp_str3, 1, 8)
				end
				
				right_line[1] = sel_act .. temp_str3
				
				
			else
				temp_str = string.sub(use_des_app, 1, 1)
				
				B738DR_rnav_enable = 0
				
				temp_str2 = use_des_app
				temp_str_rnw = string.sub(temp_str2, 2, 3)
				if tonumber(temp_str_rnw) == nil then
					temp_str_rnw = ""
					temp_str_add = ""
					temp_str_app = temp_str2
				else
					if string.len(temp_str2) > 3 then
						if string.sub(temp_str2, 4, 4) == "L" or string.sub(temp_str2, 4, 4) == "R" or string.sub(temp_str2, 4, 4) == "C" then
							temp_str_rnw = temp_str_rnw .. string.sub(temp_str2, 4, 4)
							if string.len(temp_str2) > 4 then
								temp_str_add = string.sub(temp_str2, 5, -1)
							else
								temp_str_add = ""
							end
						else
							temp_str_add = string.sub(temp_str2, 4, -1)
						end
						if string.len(temp_str_add) > 0 then
							temp_str3 = ""
							for aaa = 1, string.len(temp_str_add) do
								if string.sub(temp_str_add, aaa, aaa) ~= "-" then
									temp_str3 = temp_str3 .. string.sub(temp_str_add, aaa, aaa)
								end
							end
							temp_str_add = temp_str3
						end
					end
					if temp_str == "I" then
						temp_str_app = "ILS"
					elseif temp_str == "R" then
						temp_str_app = "RNV"
					elseif temp_str == "D" or temp_str == "S" then
						temp_str_app = "VDM"
					elseif temp_str == "L" then
						temp_str_app = "LOC"
					elseif temp_str == "N" or temp_str == "Q" then
						temp_str_app = "NDB"
					elseif temp_str == "J" then
						temp_str_app = "GLS"
					elseif temp_str == "V" then
						temp_str_app = "VOR"
					elseif temp_str == "X" then
						temp_str_app = "LDA"
					elseif temp_str == "G" then
						temp_str_app = "IGS"
					elseif temp_str == "M" then
						temp_str_app = "MLS"
					elseif temp_str == "B" then
						temp_str_app = "BLOC"
					elseif temp_str == "P" then
						temp_str_app = "GPS"
					elseif temp_str == "T" then
						temp_str_app = "TAC"
					elseif temp_str == "U" then
						temp_str_app = "SDF"
					elseif temp_str == "W" or temp_str == "Y" then
						temp_str_app = "MLS"
					else
						temp_str_app = temp_str2
					end
				end
				
				temp_str_rnw_len = string.len(temp_str_rnw)
				temp_str3 = temp_str_app .. temp_str_add
				
				if string.sub(temp_str2, 1, 2) == "RW" then
					temp_str3 = string.sub(temp_str2, 3, -1)
					if first_runway == 0 then
						right_line_x[ii] = "RUNWAYS"
						first_runway = 1
					end
				elseif temp_str_rnw_len == 2 then
					if string.len(temp_str3) > 6 then
						temp_str3 = string.sub(temp_str3, 1, 6) .. temp_str_rnw
					elseif string.len(temp_str3) == 6 then
						temp_str3 = spaces_after(temp_str3, 6) .. temp_str_rnw
					else
						temp_str3 = temp_str3 .. " " .. temp_str_rnw
					end
				elseif temp_str_rnw_len == 3 then
					if string.len(temp_str3) > 5 then
						temp_str3 = string.sub(temp_str3, 1, 5) .. temp_str_rnw
					elseif string.len(temp_str3) == 5 then
						temp_str3 = spaces_after(temp_str3, 5) .. temp_str_rnw
					else
						temp_str3 = temp_str3 .. " " .. temp_str_rnw
					end
				else
					temp_str3 = temp_str_app
				end
				
				if string.len(temp_str3) > 8 then
					temp_str3 = string.sub(temp_str3, 1, 8)
				end
				
				right_line[1] = sel_act .. temp_str3
				
			end
			max_page_app = 1
		end
		
		-- Create APP Transitions
		gs_prompt = 0
		if use_sel_des_app ~= "------" and use_des_app_rw_only == 0 then
			
			if B738DR_ian_not_install == 0 then
				if set_ils == 0 then
					set_ils = 1
					if string.sub(use_des_app, 1, 1) == "I" or string.sub(use_des_app, 1, 1) == "J" or string.sub(use_des_app, 1, 1) == "X" then
						B738DR_fms_ils_disable = 0
					elseif string.sub(use_des_app, 1, 1) ~= "R" and string.sub(use_des_app, 1, 1) ~= "D" and string.sub(use_des_app, 1, 1) ~= "S" 
					and string.sub(use_des_app, 1, 1) ~= "N" and string.sub(use_des_app, 1, 1) ~= "Q" and string.sub(use_des_app, 1, 1) ~= "V" then
						B738DR_fms_ils_disable = 1
					else
						B738DR_fms_ils_disable = 0
					end
				end
				if string.sub(use_des_app, 1, 1) ~= "R" and string.sub(use_des_app, 1, 1) ~= "D" and string.sub(use_des_app, 1, 1) ~= "S" 
				and string.sub(use_des_app, 1, 1) ~= "N" and string.sub(use_des_app, 1, 1) ~= "Q" and string.sub(use_des_app, 1, 1) ~= "V" then
					gs_prompt = 1
				end
			else
				set_ils = 0
				B738DR_fms_ils_disable = 0
			end
			
			if app_tns_list_num == 0 then
				des_tns_sel_tmp[1] = ""
				des_tns_sel_tmp[2] = ""
				des_tns_sel_tmp[3] = ""
				des_tns_sel_tmp[4] = ""
				des_tns_sel_tmp[5] = ""
				--right_line[3] = "-NONE-"
			else
				if gs_prompt == 0 then
					jj = math.floor(app_tns_list_num / 4)
					kk = app_tns_list_num % 4
					if kk > 0 then
						max_page_tns = jj + 1
					else
						max_page_tns = jj
					end
				else
					jj = math.floor(app_tns_list_num / 3)
					kk = app_tns_list_num % 3
					if kk > 0 then
						max_page_tns = jj + 1
					else
						max_page_tns = jj
					end
				end
				
				if use_sel_des_app_tns == "------" then
					
					if gs_prompt ~= 0 then
						kk = (act_page_buf - 1) * 3
						
						des_tns_sel_tmp[1] = ""
						des_tns_sel_tmp[2] = ""
						
						for ii = 3, 5 do
							jj = kk + ii - 2
							if jj > app_tns_list_num then
								right_line[ii] = ""
								des_tns_sel_tmp[ii] = ""
							else
								if use_des_app_tns == app_tns_list[jj] then
									right_line[ii] = sel_act .. app_tns_list[jj]
								else
									right_line[ii] = app_tns_list[jj]
								end
								des_tns_sel_tmp[ii] = app_tns_list[jj]
							end
						end
					else
						kk = (act_page_buf - 1) * 4
						des_tns_sel_tmp[1] = ""
						
						for ii = 2, 5 do
							jj = kk + ii - 1
							if jj > app_tns_list_num then
								right_line[ii] = ""
								des_tns_sel_tmp[ii] = ""
							else
								if use_des_app_tns == app_tns_list[jj] then
									right_line[ii] = sel_act .. app_tns_list[jj]
								else
									right_line[ii] = app_tns_list[jj]
								end
								des_tns_sel_tmp[ii] = app_tns_list[jj]
							end
						end
					end
				else
					if gs_prompt ~= 0 then
						right_line[3] = sel_act .. use_des_app_tns
					else
						right_line[2] = sel_act .. use_des_app_tns
					end
					max_page_tns = 1
				end
			end
		else
			max_page_tns = 1
		end
		
		if use_sel_des_app ~= "------" then
			if use_des_app_rw_only == 0 then
				if gs_prompt ~= 0 then
					right_line_x[2] = "G/S "
					if B738DR_fms_ils_disable == 0 then
						right_line[2] = "  /   >"
						line2_g = "                 ON     "
						line2_s = "                    OFF "
					else
						right_line[2] = "  /   >"
						line2_g = "                    OFF "
						line2_s = "                 ON     "
					end
					right_line_x[3] = "TRANS"
					if app_tns_list_num == 0 then
						right_line[3] = "-NONE-"
					end
				else
					right_line_x[2] = "TRANS"
					if app_tns_list_num == 0 then
						right_line[2] = "-NONE-"
					end
				end
			else
				-- runway extension
				if gs_prompt ~= 0 then
					if set_ils == 0 then
						set_ils = 1
						B738DR_fms_ils_disable = 0	--1
					end
					
					right_line_x[3] = "RWY EXT"
					right_line[3] = use_rw_ext_dist .. "  "
					line3_s = "                      NM"
					
					right_line_x[4] = "FPA"
					right_line[4] = use_rw_ext_fpa
				else
					set_ils = 0
					B738DR_fms_ils_disable = 0
					
					right_line_x[2] = "RWY EXT"
					right_line[2] = use_rw_ext_dist .. "  "
					line2_s = "                      NM"
					
					right_line_x[3] = "FPA"
					right_line[3] = use_rw_ext_fpa
				end
			end
		else
			B738DR_fms_ils_disable = 0
			set_ils = 0
		end
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 11 then
				for jj = sid_len, 10 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		for ii = 1, 5 do
			sid_len = string.len(left_line_x[ii])
			if ii == 1 then
				if sid_len < 13 then
					for jj = sid_len, 12 do
						left_line_x[ii] = left_line_x[ii] .. " "
					end
				end
			else
				if sid_len < 11 then
					for jj = sid_len, 10 do
						left_line_x[ii] = left_line_x[ii] .. " "
					end
				end
			end
			right_len = 24 - string.len(left_line_x[ii])
			sid_len = string.len(right_line_x[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line_x[ii] = " " .. right_line_x[ii]
				end
			end
		end
		
		line1_x = left_line_x[1] .. right_line_x[1]
		line2_x = left_line_x[2] .. right_line_x[2]
		line3_x = left_line_x[3] .. right_line_x[3]
		line4_x = left_line_x[4] .. right_line_x[4]
		line5_x = left_line_x[5] .. right_line_x[5]
		
		max_page_buf = math.max(1, max_page_star, max_page_app, max_page_tns, max_page_star_tns)
		
		if use_rte12_sel == 0 then
			if arr_data_x == 0 then
				line0_l = "   " .. des_icao_x
			elseif arr_data_x == 1 then
				line0_l = "   " .. ref_icao_x
			end
		else
			if arr_data7_x == 0 then
				line0_l = "   " .. des_icao7_x
			elseif arr_data7_x == 1 then
				line0_l = "   " .. ref_icao7_x
			end
		end
		line0_l = line0_l .. " ARRIVALS        "
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
		if exec_light_in == 1 and legs_num > 1 then
			line6_l = "<ERASE            ROUTE>"
		else
			line6_l = "                  ROUTE>"
		end
		
		if in_mode == 0 then
			for ii = 1,5 do
				des_star_sel[ii] = des_star_sel_tmp[ii]
				des_star_tns_sel[ii] = des_star_tns_sel_tmp[ii]
				des_app_sel[ii] = des_app_sel_tmp[ii]
				des_tns_sel[ii] = des_tns_sel_tmp[ii]
			end
		else
			for ii = 1,5 do
				des_star_sel_fo[ii] = des_star_sel_tmp[ii]
				des_star_tns_sel_fo[ii] = des_star_tns_sel_tmp[ii]
				des_app_sel_fo[ii] = des_app_sel_tmp[ii]
				des_tns_sel_fo[ii] = des_tns_sel_tmp[ii]
			end
		end
	end

end

function string_pos(lat_pos, lon_pos)

	local position_min = ""
	local position_deg = ""
	local position = ""
	local temp_deg = 0
	local temp_min = 0
	local navaid_lat_deg = 0
	local navaid_lat_min = 0
	local navaid_lon_deg = 0
	local navaid_lon_min = 0
	local navaid_lat_ns = 0
	local navaid_lon_ew = 0
	
	local lat = (math.abs(lat_pos))
	local lat_min_dec = (math.fmod(lat,1.0)*600)
	navaid_lat_deg = (math.floor(lat))
	navaid_lat_min = (string.format("%06.3f",lat_min_dec))
	navaid_lat_ns = 0
	if lat_pos < 0 then
		navaid_lat_ns = 1
	end
	
	local lon = (math.abs(lon_pos))
	local lon_min_dec = (math.fmod(lon,1.0)*600)
	navaid_lon_deg = (math.floor(lon))
	navaid_lon_min = (string.format("%06.3f",lon_min_dec))
	navaid_lon_ew = 0
	if lon_pos < 0 then
		navaid_lon_ew = 1
	end
			
	temp_deg = string.format("%02d", navaid_lat_deg)
	temp_min = string.format("%05.1f", navaid_lat_min)
	if navaid_lat_ns == 1 then
		position = ("S" .. temp_deg)
	else
		position = ("N" .. temp_deg)
	end
	position = position .. string.sub(temp_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(temp_min, 3, 3)
	temp_deg = string.format("%03d", navaid_lon_deg)
	temp_min = string.format("%05.1f", navaid_lon_min)
	if navaid_lon_ew == 1 then
		position = (position .. "W")
	else
		position = (position .. "E")
	end
	position = position .. temp_deg
	position = position .. string.sub(temp_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(temp_min, 3, 3)
	
	return position

end

function B738_fmc_sel_wpt()

	if page_sel_wpt == 1 then
		
		local ii = 0
		local jj = 0
		local kk = 0
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_linex')
		local left_linex = {true, true, true, true, true}
		left_linex[1] = ""
		left_linex[2] = ""
		left_linex[3] = ""
		left_linex[4] = ""
		left_linex[5] = ""
		
		local sid_len = 0
		local sid_len2 = 0
		local temp_str = ""
		local rw_tgt = ""
		local right_len = 0
		
		local max_page_wpt = 0
		
		jj = math.floor(navaid_list_n / 5)
		kk = navaid_list_n % 5
		
		if kk > 0 then
			max_page_wpt = jj + 1
		else
			max_page_wpt = jj
		end
		
		kk = (act_page_buf - 1) * 5
		for ii = 1, 5 do
			jj = kk + ii
			if jj > navaid_list_n then
				left_linex[ii] = ""
				left_line[ii] = ""
				right_line[ii] = ""
			else
				sid_len = navaid_list[jj][1]
				if sid_len == 1 then
					left_linex[ii] = " VOR    " .. navaid_list[jj][7]
				elseif sid_len == 2 then
					left_linex[ii] = " VORDME " .. navaid_list[jj][7]
				elseif sid_len == 3 then
					left_linex[ii] = " NDB    " .. navaid_list[jj][7]
				elseif sid_len == 4 then
					left_linex[ii] = " WPT    " .. navaid_list[jj][7]
				elseif sid_len == 5 then
					left_linex[ii] = " DME    " .. navaid_list[jj][7]
				elseif sid_len == 6 then
					left_linex[ii] = " LOC-GS " .. navaid_list[jj][7]
				elseif sid_len == 7 then
					left_linex[ii] = " LOC    " .. navaid_list[jj][7]
				elseif sid_len == 8 then
					left_linex[ii] = " ILS    " .. navaid_list[jj][7]
				elseif sid_len == 9 then
					left_linex[ii] = " APT    " .. navaid_list[jj][7]
				elseif sid_len == 10 then
					left_linex[ii] = " LDA-GS " .. navaid_list[jj][7]
				elseif sid_len == 11 then
					left_linex[ii] = " GLS    " .. navaid_list[jj][7]
				elseif sid_len == 12 then
					left_linex[ii] = " LPV    " .. navaid_list[jj][7]
				end
				
				temp_str = navaid_list[jj][6]
				sid_len2 = string.len(temp_str)
				if sid_len > 10 then
					temp_str = navaid_list[jj][6] .. " "
				elseif sid_len2 == 3 then
					temp_str = navaid_list[jj][6]
				elseif sid_len2 == 4 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -1, -1)
				elseif sid_len2 == 5 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -2, -1)
				else
					temp_str = ""
				end
				left_line[ii] = temp_str
				right_line[ii] = string_pos(navaid_list[jj][2], navaid_list[jj][3])
			end
		end
		
		-- Type: VOR, VORTAC, VORDME, NDB, LOC, ILS, DME, ILSDME, LOCDME, APT, WPT
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 7 then
				for jj = sid_len, 6 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		line1_x = left_linex[1]
		line2_x = left_linex[2]
		line3_x = left_linex[3]
		line4_x = left_linex[4]
		line5_x = left_linex[5]
		
		
		max_page_buf = math.max(1, max_page_wpt)
		
		temp_str = navaid_list[1][4]
		if string.len(temp_str) > 6 then
			temp_str = string.sub(temp_str, 1, 6)
		end
		line0_l = " SEL DESIRED " .. temp_str
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
	
	end

end


function B738_fmc_sel_wpt2()

	if page_sel_wpt2 == 1 then
		
		local ii = 0
		local jj = 0
		local kk = 0
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_linex')
		local left_linex = {true, true, true, true, true}
		left_linex[1] = ""
		left_linex[2] = ""
		left_linex[3] = ""
		left_linex[4] = ""
		left_linex[5] = ""
		
		local sid_len = 0
		local sid_len2 = 0
		local temp_str = ""
		local rw_tgt = ""
		local right_len = 0
		
		local max_page_wpt = 0
		
		jj = math.floor(navaid_list_n / 5)
		kk = navaid_list_n % 5
		
		if kk > 0 then
			max_page_wpt = jj + 1
		else
			max_page_wpt = jj
		end
		
		kk = (act_page_buf - 1) * 5
		for ii = 1, 5 do
			jj = kk + ii
			if jj > navaid_list_n then
				left_linex[ii] = ""
				left_line[ii] = ""
				right_line[ii] = ""
			else
				sid_len = navaid_list[jj][1]
				if sid_len == 1 then
					left_linex[ii] = " VOR    " .. navaid_list[jj][7]
				elseif sid_len == 2 then
					left_linex[ii] = " VORDME " .. navaid_list[jj][7]
				elseif sid_len == 3 then
					left_linex[ii] = " NDB    " .. navaid_list[jj][7]
				elseif sid_len == 4 then
					left_linex[ii] = " WPT    " .. navaid_list[jj][7]
				elseif sid_len == 5 then
					left_linex[ii] = " DME    " .. navaid_list[jj][7]
				elseif sid_len == 6 then
					left_linex[ii] = " LOC-GS " .. navaid_list[jj][7]
				elseif sid_len == 7 then
					left_linex[ii] = " LOC    " .. navaid_list[jj][7]
				elseif sid_len == 8 then
					left_linex[ii] = " ILS    " .. navaid_list[jj][7]
				elseif sid_len == 9 then
					left_linex[ii] = " APT    " .. navaid_list[jj][7]
				elseif sid_len == 10 then
					left_linex[ii] = " LDA-GS " .. navaid_list[jj][7]
				elseif sid_len == 11 then
					left_linex[ii] = " GLS    " .. navaid_list[jj][7]
				elseif sid_len == 12 then
					left_linex[ii] = " LPV    " .. navaid_list[jj][7]
				end
				
				temp_str = navaid_list[jj][6]
				sid_len2 = string.len(temp_str)
				if sid_len > 10 then
					temp_str = navaid_list[jj][6] .. " "
				elseif sid_len2 == 3 then
					temp_str = navaid_list[jj][6]
				elseif sid_len2 == 4 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -1, -1)
				elseif sid_len2 == 5 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -2, -1)
				else
					temp_str = ""
				end
				left_line[ii] = temp_str
				right_line[ii] = string_pos(navaid_list[jj][2], navaid_list[jj][3])
			end
		end
		
		-- Type: VOR, VORTAC, VORDME, NDB, LOC, ILS, DME, ILSDME, LOCDME, APT, WPT
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 7 then
				for jj = sid_len, 6 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		
		line1_x = left_linex[1] 
		line2_x = left_linex[2]
		line3_x = left_linex[3]
		line4_x = left_linex[4]
		line5_x = left_linex[5]
		
		
		max_page_buf = math.max(1, max_page_wpt)
		
		temp_str = navaid_list[1][4]
		if string.len(temp_str) > 6 then
			temp_str = string.sub(temp_str, 1, 6)
		end
		line0_l = " SEL DESIRED " .. temp_str
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
	
	end

end

function B738_fmc_sel_wpt3()

	if page_sel_wpt3 == 1 then
		
		local ii = 0
		local jj = 0
		local kk = 0
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_linex')
		local left_linex = {true, true, true, true, true}
		left_linex[1] = ""
		left_linex[2] = ""
		left_linex[3] = ""
		left_linex[4] = ""
		left_linex[5] = ""
		
		local sid_len = 0
		local sid_len2 = 0
		local temp_str = ""
		local rw_tgt = ""
		local right_len = 0
		
		local max_page_wpt = 0
		
		jj = math.floor(navaid_list_n / 5)
		kk = navaid_list_n % 5
		
		if kk > 0 then
			max_page_wpt = jj + 1
		else
			max_page_wpt = jj
		end
		
		kk = (act_page_buf - 1) * 5
		for ii = 1, 5 do
			jj = kk + ii
			if jj > navaid_list_n then
				left_linex[ii] = ""
				left_line[ii] = ""
				right_line[ii] = ""
			else
				sid_len = navaid_list[jj][1]
				if sid_len == 1 then
					left_linex[ii] = " VOR    " .. navaid_list[jj][7]
				elseif sid_len == 2 then
					left_linex[ii] = " VORDME " .. navaid_list[jj][7]
				elseif sid_len == 3 then
					left_linex[ii] = " NDB    " .. navaid_list[jj][7]
				elseif sid_len == 4 then
					left_linex[ii] = " WPT    " .. navaid_list[jj][7]
				elseif sid_len == 5 then
					left_linex[ii] = " DME    " .. navaid_list[jj][7]
				elseif sid_len == 6 then
					left_linex[ii] = " LOC-GS " .. navaid_list[jj][7]
				elseif sid_len == 7 then
					left_linex[ii] = " LOC    " .. navaid_list[jj][7]
				elseif sid_len == 8 then
					left_linex[ii] = " ILS    " .. navaid_list[jj][7]
				elseif sid_len == 9 then
					left_linex[ii] = " APT    " .. navaid_list[jj][7]
				elseif sid_len == 10 then
					left_linex[ii] = " LDA-GS " .. navaid_list[jj][7]
				elseif sid_len == 11 then
					left_linex[ii] = " GLS    " .. navaid_list[jj][7]
				elseif sid_len == 12 then
					left_linex[ii] = " LPV    " .. navaid_list[jj][7]
				end
				
				temp_str = navaid_list[jj][6]
				sid_len2 = string.len(temp_str)
				if sid_len > 10 then
					temp_str = navaid_list[jj][6] .. " "
				elseif sid_len2 == 3 then
					temp_str = navaid_list[jj][6]
				elseif sid_len2 == 4 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -1, -1)
				elseif sid_len2 == 5 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -2, -1)
				else
					temp_str = ""
				end
				left_line[ii] = temp_str
				right_line[ii] = string_pos(navaid_list[jj][2], navaid_list[jj][3])
			end
		end
		
		-- Type: VOR, VORTAC, VORDME, NDB, LOC, ILS, DME, ILSDME, LOCDME, APT, WPT
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 7 then
				for jj = sid_len, 6 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		
		line1_x = left_linex[1] 
		line2_x = left_linex[2]
		line3_x = left_linex[3]
		line4_x = left_linex[4]
		line5_x = left_linex[5]
		
		
		max_page_buf = math.max(1, max_page_wpt)
		
		temp_str = navaid_list[1][4]
		if string.len(temp_str) > 6 then
			temp_str = string.sub(temp_str, 1, 6)
		end
		line0_l = " SEL DESIRED " .. temp_str
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
	
	end

end

function B738_fmc_sel_wpt4()

	if page_sel_wpt4 == 1 then
		
		local ii = 0
		local jj = 0
		local kk = 0
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_linex')
		local left_linex = {true, true, true, true, true}
		left_linex[1] = ""
		left_linex[2] = ""
		left_linex[3] = ""
		left_linex[4] = ""
		left_linex[5] = ""
		
		local sid_len = 0
		local sid_len2 = 0
		local temp_str = ""
		local rw_tgt = ""
		local right_len = 0
		
		local max_page_wpt = 0
		
		jj = math.floor(navaid_list_n / 5)
		kk = navaid_list_n % 5
		
		if kk > 0 then
			max_page_wpt = jj + 1
		else
			max_page_wpt = jj
		end
		
		kk = (act_page_buf - 1) * 5
		for ii = 1, 5 do
			jj = kk + ii
			if jj > navaid_list_n then
				left_linex[ii] = ""
				left_line[ii] = ""
				right_line[ii] = ""
			else
				sid_len = navaid_list[jj][1]
				if sid_len == 1 then
					left_linex[ii] = " VOR    " .. navaid_list[jj][7]
				elseif sid_len == 2 then
					left_linex[ii] = " VORDME " .. navaid_list[jj][7]
				elseif sid_len == 3 then
					left_linex[ii] = " NDB    " .. navaid_list[jj][7]
				elseif sid_len == 4 then
					left_linex[ii] = " WPT    " .. navaid_list[jj][7]
				elseif sid_len == 5 then
					left_linex[ii] = " DME    " .. navaid_list[jj][7]
				elseif sid_len == 6 then
					left_linex[ii] = " LOC-GS " .. navaid_list[jj][7]
				elseif sid_len == 7 then
					left_linex[ii] = " LOC    " .. navaid_list[jj][7]
				elseif sid_len == 8 then
					left_linex[ii] = " ILS    " .. navaid_list[jj][7]
				elseif sid_len == 9 then
					left_linex[ii] = " APT    " .. navaid_list[jj][7]
				elseif sid_len == 10 then
					left_linex[ii] = " LDA-GS " .. navaid_list[jj][7]
				elseif sid_len == 11 then
					left_linex[ii] = " GLS    " .. navaid_list[jj][7]
				elseif sid_len == 12 then
					left_linex[ii] = " LPV    " .. navaid_list[jj][7]
				end
				
				temp_str = navaid_list[jj][6]
				sid_len2 = string.len(temp_str)
				if sid_len > 10 then
					temp_str = navaid_list[jj][6] .. " "
				elseif sid_len2 == 3 then
					temp_str = navaid_list[jj][6]
				elseif sid_len2 == 4 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -1, -1)
				elseif sid_len2 == 5 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -2, -1)
				else
					temp_str = ""
				end
				left_line[ii] = temp_str
				right_line[ii] = string_pos(navaid_list[jj][2], navaid_list[jj][3])
			end
		end
		
		-- Type: VOR, VORTAC, VORDME, NDB, LOC, ILS, DME, ILSDME, LOCDME, APT, WPT
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 7 then
				for jj = sid_len, 6 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		
		line1_x = left_linex[1] 
		line2_x = left_linex[2]
		line3_x = left_linex[3]
		line4_x = left_linex[4]
		line5_x = left_linex[5]
		
		
		max_page_buf = math.max(1, max_page_wpt)
		
		temp_str = navaid_list[1][4]
		if string.len(temp_str) > 6 then
			temp_str = string.sub(temp_str, 1, 6)
		end
		line0_l = " SEL DESIRED " .. temp_str
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
	
	end

end

function fix_add_wpt4(wpt_idx, mode_in)
	
	--page_sel_wpt4
	local fix_idx = 0
	
	if wpt_idx <= navaid_list_n and wpt_idx ~= 0 then
		if mode_in == 0 then
			fix_idx = fix_idx1
		else
			fix_idx = fix_idx2
		end
		
		if fix_idx > fix_data_num then
			fix_data_num = fix_data_num + 1
			fix_idx = fix_data_num
		else
			if string.sub(fix_data[fix_idx][1], 1, 2) == "RW" then
				fix_rnw_lat = 0
				fix_rnw_lon = 0
				fix_rnw_crs = 0
				fix_rnw_num = ""
			end
		end
		
		fix_data[fix_idx] = {}
		fix_data[fix_idx][1] = navaid_list[wpt_idx][4]	-- id name
		fix_data[fix_idx][2] = navaid_list[wpt_idx][1]	-- type
		if fix_data[fix_idx][2] == 0 then
			fix_data[fix_idx][2] = 3
		elseif fix_data[fix_idx][2] > 5 and fix_data[fix_idx][2] ~= 9 then
			fix_data[fix_idx][2] = 3
		end
		fix_data[fix_idx][3] = navaid_list[wpt_idx][2]	-- lat
		fix_data[fix_idx][4] = navaid_list[wpt_idx][3]	-- lon
		fix_data[fix_idx][5] = navaid_list[wpt_idx][8]	-- reg code
		fix_data[fix_idx][6] = -1	-- rad1
		fix_data[fix_idx][7] = -1	-- dist1
		fix_data[fix_idx][8] = -1	-- rad2
		fix_data[fix_idx][9] = -1	-- dist2
		fix_data[fix_idx][10] = -1	-- rad3
		fix_data[fix_idx][11] = -1	-- dist3
		-- 1. radial
		fix_data[fix_idx][12] = 0	-- wpt before
		fix_data[fix_idx][13] = 0	-- dist before
		fix_data[fix_idx][14] = 0	-- lat
		fix_data[fix_idx][15] = 0	-- lon
		fix_data[fix_idx][16] = 0	-- dist calc
		fix_data[fix_idx][17] = 0	-- rad calc
		fix_data[fix_idx][18] = -1	-- eta calc
		fix_data[fix_idx][19] = 0	-- dtg calc
		fix_data[fix_idx][20] = -1	-- alt calc
		-- 2. radial
		fix_data[fix_idx][21] = 0	-- wpt before
		fix_data[fix_idx][22] = 0	-- dist before
		fix_data[fix_idx][23] = 0	-- lat
		fix_data[fix_idx][24] = 0	-- lon
		fix_data[fix_idx][25] = 0	-- dist calc
		fix_data[fix_idx][26] = 0	-- rad calc
		fix_data[fix_idx][27] = -1	-- eta calc
		fix_data[fix_idx][28] = 0	-- dtg calc
		fix_data[fix_idx][29] = -1	-- alt calc
		-- 3. radial
		fix_data[fix_idx][30] = 0	-- wpt before
		fix_data[fix_idx][31] = 0	-- dist before
		fix_data[fix_idx][32] = 0	-- lat
		fix_data[fix_idx][33] = 0	-- lon
		fix_data[fix_idx][34] = 0	-- dist calc
		fix_data[fix_idx][35] = 0	-- rad calc
		fix_data[fix_idx][36] = -1	-- eta calc
		fix_data[fix_idx][37] = 0	-- dtg calc
		fix_data[fix_idx][38] = -1	-- alt calc
	end
	
	if mode_in == 0 then
		page_sel_wpt4 = 0
		page_fix = 1
		act_page = act_page_old
		max_page = math.max(max_page, act_page)
	else
		page_sel_wpt4_2 = 0
		page_fix2 = 1
		act_page2 = act_page_old2
		max_page2 = math.max(max_page2, act_page2)
	end
	
end

function B738_fmc_sel_hold_wpt()

	if page_sel_hold_wpt == 1 then
		
		local ii = 0
		local jj = 0
		local kk = 0
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_linex')
		local left_linex = {true, true, true, true, true}
		left_linex[1] = ""
		left_linex[2] = ""
		left_linex[3] = ""
		left_linex[4] = ""
		left_linex[5] = ""
		
		local sid_len = 0
		local sid_len2 = 0
		local temp_str = ""
		local rw_tgt = ""
		local right_len = 0
		
		local max_page_wpt = 0
		
		jj = math.floor(navaid_list_n / 5)
		kk = navaid_list_n % 5
		
		if kk > 0 then
			max_page_wpt = jj + 1
		else
			max_page_wpt = jj
		end
		
		kk = (act_page_buf - 1) * 5
		for ii = 1, 5 do
			jj = kk + ii
			if jj > navaid_list_n then
				left_linex[ii] = ""
				left_line[ii] = ""
				right_line[ii] = ""
			else
				sid_len = navaid_list[jj][1]
				if sid_len == 1 then
					left_linex[ii] = " VOR    " .. navaid_list[jj][7]
				elseif sid_len == 2 then
					left_linex[ii] = " VORDME " .. navaid_list[jj][7]
				elseif sid_len == 3 then
					left_linex[ii] = " NDB    " .. navaid_list[jj][7]
				elseif sid_len == 4 then
					left_linex[ii] = " WPT    " .. navaid_list[jj][7]
				elseif sid_len == 5 then
					left_linex[ii] = " DME    " .. navaid_list[jj][7]
				elseif sid_len == 6 then
					left_linex[ii] = " LOC-GS " .. navaid_list[jj][7]
				elseif sid_len == 7 then
					left_linex[ii] = " LOC    " .. navaid_list[jj][7]
				elseif sid_len == 8 then
					left_linex[ii] = " ILS    " .. navaid_list[jj][7]
				elseif sid_len == 9 then
					left_linex[ii] = " APT    " .. navaid_list[jj][7]
				elseif sid_len == 10 then
					left_linex[ii] = " LDA-GS " .. navaid_list[jj][7]
				elseif sid_len == 11 then
					left_linex[ii] = " GLS    " .. navaid_list[jj][7]
				elseif sid_len == 12 then
					left_linex[ii] = " LPV    " .. navaid_list[jj][7]
				end
				
				temp_str = navaid_list[jj][6]
				sid_len2 = string.len(temp_str)
				if sid_len > 10 then
					temp_str = navaid_list[jj][6] .. " "
				elseif sid_len2 == 3 then
					temp_str = navaid_list[jj][6]
				elseif sid_len2 == 4 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -1, -1)
				elseif sid_len2 == 5 then
					temp_str = string.sub(navaid_list[jj][6], 1, 3) .. "." .. string.sub(navaid_list[jj][6], -2, -1)
				else
					temp_str = ""
				end
				left_line[ii] = temp_str
				right_line[ii] = string_pos(navaid_list[jj][2], navaid_list[jj][3])
			end
		end
		
		-- Type: VOR, VORTAC, VORDME, NDB, LOC, ILS, DME, ILSDME, LOCDME, APT, WPT
		
		-- Display engine
		for ii = 1, 5 do
			sid_len = string.len(left_line[ii])
			if sid_len < 7 then
				for jj = sid_len, 6 do
					left_line[ii] = left_line[ii] .. " "
				end
			end
			right_len = 24 - string.len(left_line[ii])
			sid_len = string.len(right_line[ii])
			if sid_len < right_len then
				for jj = sid_len, (right_len - 1) do
					right_line[ii] = " " .. right_line[ii]
				end
			end
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		
		line1_x = left_linex[1] 
		line2_x = left_linex[2]
		line3_x = left_linex[3]
		line4_x = left_linex[4]
		line5_x = left_linex[5]
		
		
		max_page_buf = math.max(1, max_page_wpt)
		
		temp_str = navaid_list[1][4]
		if string.len(temp_str) > 6 then
			temp_str = string.sub(temp_str, 1, 6)
		end
		line0_l = " SEL DESIRED " .. temp_str
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		
	
	end

end



function fl_format(x_alt, x_fl)
	local result = ""
	if x_alt > x_fl then
		result = "FL" .. string.format("%03d", x_alt/100)
	else
		result = string.format("%5d", x_alt)
	end
	return result
end

function B738_fmc_legs99(in_mode)

	-- if page_legs == 1 then
		
		local step_in = legs_step
		local map_mode_in = B738DR_capt_map_mode
		local new_hold_in = 0
		local use_new_hold_at = 0
		local exec_light_in = B738DR_fmc_exec_lights
		local use_rte12_sel = rte12_sel
		local use_rte_header_str = rte_header_str
		local use_rte_option_str = rte_option_str
		local use_offset_mod = 0
		if in_mode ~= 0 then
			step_in = legs_step2
			map_mode_in = B738DR_fo_map_mode
			exec_light_in = B738DR_fmc_exec_lights_fo
			use_rte12_sel = rte12_sel_fo
			use_rte_header_str = rte_header_str_fo
			use_rte_option_str = rte_option_str_fo
		end
		if use_rte12_sel == 0 then
			new_hold_in = new_hold
			use_new_hold_at = new_hold_at
			use_offset_mod = offset_mod
		else
			new_hold_in = new_hold2
			use_new_hold_at = new_hold_at2
			use_offset_mod = offset_mod2
		end
		
		copy_to_legs_data2_buf(use_rte12_sel)
		
		local ii = 0
		local jj = 0
		local kk = 0
		local ll = 0
		
		local max_page_legs = 0
		
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_line_x')
		local left_line_x = {true, true, true, true, true}
		left_line_x[1] = ""
		left_line_x[2] = ""
		left_line_x[3] = ""
		left_line_x[4] = ""
		left_line_x[5] = ""
		raw_table('line_s')
		local line_s = {true, true, true, true, true}
		line_s[1] = ""
		line_s[2] = ""
		line_s[3] = ""
		line_s[4] = ""
		line_s[5] = ""
		raw_table('line_ml')
		local line_ml = {true, true, true, true, true}
		line_ml[1] = ""
		line_ml[2] = ""
		line_ml[3] = ""
		line_ml[4] = ""
		line_ml[5] = ""
		raw_table('line_m')
		local line_m = {true, true, true, true, true}
		line_m[1] = ""
		line_m[2] = ""
		line_m[3] = ""
		line_m[4] = ""
		line_m[5] = ""
		
		raw_table('line_in')
		local line_in = {true, true, true, true, true}
		line_in[1] = ""
		line_in[2] = ""
		line_in[3] = ""
		line_in[4] = ""
		line_in[5] = ""
		
		raw_table('line_right_in')
		local line_right_in = {true, true, true, true, true}
		line_right_in[1] = ""
		line_right_in[2] = ""
		line_right_in[3] = ""
		line_right_in[4] = ""
		line_right_in[5] = ""
		
		raw_table('line_ctr')
		local line_ctr = {true, true, true, true, true}
		line_ctr[1] = 0
		line_ctr[2] = 0
		line_ctr[3] = 0
		line_ctr[4] = 0
		line_ctr[5] = 0
		
		local val_changed = false
		
		local sid_len = 0
		local temp_string = ""
		local overwrite_disc = 0
		local discon_last = 0
		
		local allign_ok = 0
		local disable_page = 0
		
		local step_loc = step_in
		local fms_trk_act = 0
		local fms_dist_act = 0
		local nd_lat = simDR_latitude
		local nd_lon = simDR_longitude
		
		local show_gp = 0
		
		local lat_tmp = 0
		local lon_tmp = 0
		line1_x = ""
		line2_x = ""
		line3_x = ""
		line4_x = ""
		line5_x = ""
		if B738DR_no_rte2 == 0 and act_page_buf == 1 then
			line6_x = ""
		else
			line6_x = "------------------------"
		end
		
		if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
			allign_ok = 1
		end
		
		local offset_temp = use_offset_mod
		if legs_num2_buf > 1 and offset_temp < 2 then
			offset_temp = 2
		end
		
		if offset_temp > legs_num2_buf then
			disable_page = 1
		end
		
		local rte2_corr = 0
		if B738DR_no_rte2 == 0 and legs_num2_buf >= 5 then
			rte2_corr = 1
		end
		jj = math.floor((legs_num2_buf + 2 - offset_temp + rte2_corr) / 5)
		kk = (legs_num2_buf + 2 - offset_temp  + rte2_corr) % 5
		if kk > 0 then
			max_page_legs = jj + 1
		else
			max_page_legs = jj
		end
		
		if legs_num2_buf == 0 then
			max_page_legs = 1
		end
		if act_page_buf > max_page_legs then
			act_page_buf = max_page_legs
			if act_page_buf == 0 then
				act_page_buf = 1
			end
		end
		if step_loc > legs_num2_buf then
			step_loc = legs_num2_buf
		end
		
		if nav_mode == 99 then
			if legs_delete == 0 then
				disable_page = 1
			else
				-- offset_temp = offset + 1
				offset_temp = use_offset_mod + 1
				if offset_temp > legs_num2_buf then
					disable_page = 1
				end
			end
		end
		
		if legs_num2_buf > 1 and disable_page == 0 then
			
			if offset_temp > legs_num2_buf then
				offset_temp = legs_num2_buf
			end
		
			if offset_temp == 0 then
				offset_temp = 1
			end
			
			if step_loc < offset_temp then
				step_loc = offset_temp
			end
			
			kk = (act_page_buf - 1) * 5
			local max_items = 5
			local corr_rte2 = 0
			if B738DR_no_rte2 == 0 then
				if act_page_buf > 1 then
					corr_rte2 = 1
				else
					max_items = 4
				end
			end
			for ii = 1, max_items do
				jj = kk + ii + (offset_temp - 1) - corr_rte2
				if ii == 1 then
					line_m[1] = ""
				end
				-- B738DR_fms_test3 = jj
				if jj == legs_num2_buf + 1 and legs_num2_buf > 1 then
					left_line_x[ii] = ""
					left_line[ii] = "-----"
					right_line[ii] = ""
				elseif jj > legs_num2_buf or jj == 0 then
					left_line_x[ii] = ""
					left_line[ii] = ""
					right_line[ii] = ""
				else
					if act_page_buf == 1 and ii == 1 and legs_data2_buf[jj][1] ~= "DISCONTINUITY" and legs_num > 0 and rte12_act == use_rte12_sel then
						-- is HOLD ?
						if legs_data2_buf[jj][21] == -1 or legs_data2_buf[jj][21] > 1 then
							-- course
							if legs_intdir == 1 and legs_intdir_rte == rte12_act then
								if legs_intdir_crs2 < 0 then
									fms_trk_act = (legs_intdir_trk2 + simDR_mag_variation + 360) % 360
								else
									fms_trk_act = legs_intdir_crs_mod
								end
							elseif legs_intdir_act == 2 then
								if B738DR_fms_legs_df_crs_2[jj-1] < 0 then
									fms_trk_act = nd_calc_brg(simDR_latitude, simDR_longitude, legs_data2_buf[jj][7], legs_data2_buf[jj][8])
								else
									fms_trk_act = (B738DR_fms_legs_df_crs_2[jj-1] + simDR_mag_variation + 360) % 360
								end
							else
								if string.sub(legs_data2_buf[jj][31], 1, 1) == "C" or string.sub(legs_data2_buf[jj][31], 1, 1) == "V" then
									fms_trk_act = math.deg(legs_data2_buf[jj][18])
								elseif legs_data2_buf[jj][31] == "TF" or legs_data2_buf[jj][31] == "IF" or legs_data2_buf[jj][31] == "" then
									-- fms_trk_act = (math.deg(legs_data2_buf[jj][2]) + simDR_mag_variation + 360) % 360
									local trk_dis = nd_calc_dist2(simDR_latitude, simDR_longitude, legs_data2_buf[jj][7], legs_data2_buf[jj][8])
									if trk_dis < legs_data2_buf[jj][3] then
										local trk_init = math.deg(legs_data2_buf[jj][2])
										local trk_final = math.deg(legs_data2_buf[jj][37])
										local trk_delta = (trk_final - trk_init + 360) % 360
										if trk_delta > 180 then
											trk_delta = trk_delta - 360
										end
										local trk_act = B738_rescale(legs_data2_buf[jj][3], 0, 0, trk_delta, trk_dis)
										fms_trk_act = (trk_init + trk_act + 360) % 360
									else
										fms_trk_act = math.deg(legs_data2_buf[jj][2])
									end
									fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
									
								elseif legs_data2_buf[jj][31] == "RF" or legs_data2_buf[jj][31] == "AF" then
									if jj <= legs_num2_buf then
										if string.sub(legs_data2_buf[jj+1][31], 1, 1) == "C" or string.sub(legs_data2_buf[jj+1][31], 1, 1) == "V" then
											fms_trk_act = math.deg(legs_data2_buf[jj+1][18])
										else
											fms_trk_act = (math.deg(legs_data2_buf[jj+1][2]) + simDR_mag_variation + 360) % 360
										end
									else
										fms_trk_act = (math.deg(legs_data2_buf[jj][2]) + simDR_mag_variation + 360) % 360
									end
								elseif legs_data2_buf[jj][31] == "DF" then
									if legs_delete == 0 then
										if jj > 0 and jj < FMC_MAX_WPT and legs_num > 1 then
											if B738DR_fms_legs_df_crs_2[jj-1] < 0 then
												-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
												lat_tmp = legs_data2_buf[jj][7]
												lon_tmp = legs_data2_buf[jj][8]
												fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
												fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
											else
												fms_trk_act = (B738DR_fms_legs_df_crs_2[jj-1] + simDR_mag_variation + 360) % 360
											end
										else
											-- fms_trk_act = nd_calc_brg(nd_lat, nd_lon, legs_data2_buf[jj][7], legs_data2_buf[jj][8])
											-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
											lat_tmp = legs_data2_buf[jj][7]
											lon_tmp = legs_data2_buf[jj][8]
											fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
											fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
										end
									else
										-- fms_trk_act = nd_calc_brg(nd_lat, nd_lon, legs_data2_buf[jj][7], legs_data2_buf[jj][8])
										-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
										lat_tmp = legs_data2_buf[jj][7]
										lon_tmp = legs_data2_buf[jj][8]
										fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
										fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
									end
								else
									fms_trk_act = (math.deg(legs_data2_buf[jj][2]) + simDR_mag_variation + 360) % 360
								end
							end
							-- fms_trk_act = (fms_trk_act + 360) % 360
							
							fms_trk_act = displ_crs(fms_trk_act)
							
							-- " 020`     xx.xNM"
							-- "  1.5 ARC L     "
							-- " PROC TURN L    "
							if legs_data2_buf[jj][31] == "AF" or legs_data2_buf[jj][31] == "RF" then
								
								-- fms_dist_act = wpt_distance2(jj, rte12_sel)
								
								-- if legs_intdir == 1 then
									-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
									lat_tmp = legs_data2_buf[jj][7]
									lon_tmp = legs_data2_buf[jj][8]
									fms_dist_act = nd_calc_dist2(nd_lat, nd_lon, lat_tmp, lon_tmp)
								-- else
									-- fms_dist_act = B738DR_rt_abeam_dist_bp
								-- end
								
								if fms_dist_act < 10 then
									left_line_x[ii] = " " .. string.format("%4.1f",fms_dist_act)
								else
									left_line_x[ii] = " " .. string.format("%4d",fms_dist_act)
								end
								left_line_x[ii] = left_line_x[ii] .. " ARC "
								if legs_data2_buf[jj][21] == 2 then
									left_line_x[ii] = left_line_x[ii] .. "L     "
								else
									left_line_x[ii] = left_line_x[ii] .. "R     "
								end
							elseif legs_data2_buf[jj][31] == "PI" then
								left_line_x[ii] = " PROC TURN "
								if legs_data2_buf[jj][21] == 2 then
									left_line_x[ii] = left_line_x[ii] .. "L    "
								else
									left_line_x[ii] = left_line_x[ii] .. "R    "
								end
							else
								left_line_x[ii] = " " .. string.format("%3d", fms_trk_act)
								if string.sub(legs_data2_buf[jj][31], 1, 1) == "V" then
									left_line_x[ii] = left_line_x[ii] .. "`HDG  "
								else
									left_line_x[ii] = left_line_x[ii] .. "`     "
								end
								
								-- if legs_intdir == 1 then
									-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
									lat_tmp = legs_data2_buf[jj][7]
									lon_tmp = legs_data2_buf[jj][8]
									fms_dist_act = nd_calc_dist2(nd_lat, nd_lon, lat_tmp, lon_tmp)
								-- else
									-- fms_dist_act = B738DR_rt_abeam_dist_bp
								-- end
								if fms_dist_act < 10 then
									left_line_x[ii] = left_line_x[ii] .. string.format("%4.1f",fms_dist_act)
								else
									left_line_x[ii] = left_line_x[ii] .. string.format("%4d",fms_dist_act)
								end
								left_line_x[ii] = left_line_x[ii] .. "NM"
								
							end
							
							show_gp = 0
							
							-- GP
							if legs_data2_buf[jj][20] ~= 0 and string.sub(legs_data2_buf[jj][1], 1, 2) ~= "RX" 
							and jj > rnav_idx_first_mod_buf and rnav_idx_first_mod_buf ~= 0 then
								left_line_x[ii] = left_line_x[ii] .. " GP" .. string.format("%4.2f", -legs_data2_buf[jj][20]) .. "`"
								show_gp = 1
							else
								lat_tmp = legs_data2_buf[jj][45]
								if lat_tmp ~= 0 then
									left_line_x[ii] = left_line_x[ii] .. " OFS"
									if lat_tmp < 0 then
										left_line_x[ii] = left_line_x[ii] .. " L"
										if lat_tmp > -9.5 then
											left_line_x[ii] = left_line_x[ii] .. string.format("%1d", -lat_tmp)
										else
											left_line_x[ii] = left_line_x[ii] .. string.format("%2d", -lat_tmp)
										end
									else
										left_line_x[ii] = left_line_x[ii] .. " R"
										if lat_tmp < 9.5 then
											left_line_x[ii] = left_line_x[ii] .. string.format("%1d", lat_tmp)
										else
											left_line_x[ii] = left_line_x[ii] .. string.format("%2d", lat_tmp)
										end
									end
									show_gp = 1
								end
							end
							
							if show_gp == 0 then
								-- BYPASS
								if legs_data2_buf[jj][1] ~= "DISCONTINUITY" and legs_data2_buf[jj][1] ~= "(VECTOR)" then
									if legs_data2_buf[jj][3] == 0 and legs_data2_buf[jj][31] ~= "PI" then
										left_line_x[ii] = left_line_x[ii] .. "  BYPASS"
									elseif jj <= legs_num and jj >= 0 and legs_delete == 0 then
										if B738DR_fms_legs_bypass[jj-1] == 1 then
											left_line_x[ii] = left_line_x[ii] .. "  BYPASS"
										end
									end
								end
							end
							
							-- id
							if legs_data2_buf[jj][1] == "DISCONTINUITY" then
								left_line_x[ii] = " THEN"
								if allign_ok == 0 then
									line_ml[1] = ""
									left_line[ii] = "*****"
								else
									line_ml[1] = "*****"
									left_line[ii] = ""
								end
								right_line[ii] = ""
								left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
								discon_last = 1
							else
								if allign_ok == 0 or offset_temp > legs_num2_buf then
									if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == use_rte12_sel then
										line_ml[1] = "     "
										left_line[ii] = "     "
										line_in[1] = legs_data2_buf[jj][1]
									else
										line_ml[1] = "     "
										left_line[ii] = legs_data2_buf[jj][1]
									end
								else
									if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == use_rte12_sel then
										line_ml[1] = "     "
										left_line[ii] = "     "
										line_in[1] = legs_data2_buf[jj][1]
									else
										if B738DR_fpln_active == 0 then
											line_ml[1] = "     "
											left_line[ii] = legs_data2_buf[jj][1]
										else
											if legs_intdir == 1 and legs_intdir_rte == rte12_act then
												line_ml[1] = "     "
												left_line[ii] = "     "
												line_in[1] = legs_data2_buf[jj][1]
											else
												line_ml[1] = legs_data2_buf[jj][1]
												left_line[ii] = "     "
											end
										end
									end
								end
								if map_mode_in == 3 and step_loc == jj and legs_data2_buf[jj][6] ~= 41 then
									line_ctr[ii] = 1
								end
							end
						else
							if legs_data2_buf[jj][31] == "HF" then
								left_line_x[ii] = " PROC HOLD"
								line_ml[ii] = "(INBD)"
							else
								left_line_x[ii] = " HOLD AT"
								if allign_ok == 0 or offset_temp > legs_num2_buf then
									if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == use_rte12_sel then
										line_ml[ii] = "     "
										left_line[ii] = "     "
										line_in[ii] = legs_data2_buf[jj][1]
									else
										left_line[ii] = legs_data2_buf[jj][1]
										line_ml[1] = "     "
									end
								else
									if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == use_rte12_sel then
										line_ml[ii] = "     "
										left_line[ii] = "     "
										line_in[ii] = legs_data2_buf[jj][1]
									else
										if B738DR_fpln_active == 0 then
											line_ml[1] = "     "
											left_line[ii] = legs_data2_buf[jj][1]
										else
											line_ml[1] = legs_data2_buf[jj][1]
											left_line[ii] = "     "
										end
									end
								end
							end
							
							if map_mode_in == 3 and step_loc == jj and legs_data2_buf[jj][6] ~= 41 then
								line_ctr[ii] = 1
							end
						end
						
						-- speed
						show_gp = 0
						if rnav_idx_last_mod_buf ~= 0 and ed_found_mod ~= 0 and rnav_idx_last_mod_buf <= legs_num2_buf and jj == rnav_idx_last_mod_buf then
							if B738DR_fms_approach_speed > 0 then
								if string.sub(legs_data2_buf[rnav_idx_last_mod_buf][1], 1, 2) == "RW" then
									show_gp = 1
								end
							end
						end
						
						if show_gp ~= 0 and legs_data2_buf[jj][10] >= 100 then
							temp_string = string.format("%3d",legs_data2_buf[jj][10])
							right_line[ii] = " " .. temp_string
							line_s[ii] = "                 "
							line_m[ii] = "    "
						elseif legs_data2_buf[jj][4] == 0 then
							if legs_data2_buf[jj][10] == 0 then
								right_line[ii] = "----"
								line_m[ii] = "    "
								if legs_data2_buf[jj][6] == 41 then	--between
									line_s[ii] = "            "
								else
									line_s[ii] = "                 "
								end
								line_m[ii] = "    "
							else
								if fms_recalc == 0 then
									if legs_data2_buf[jj][10] < 1 then
										temp_string = string.format("%5.3f",legs_data2_buf[jj][10])
										temp_string = string.sub(temp_string, -4, -1)
									else
										temp_string = string.format("%4d",legs_data2_buf[jj][10])
									end
								else
									temp_string = "----"
								end
								if legs_data2_buf[jj][6] == 41 then	--between
									line_s[ii] = "        " .. temp_string
								else
									line_s[ii] = "             " .. temp_string
								end
								right_line[ii] = "    "
								line_m[ii] = "    "
							end
						else
							if legs_data2_buf[jj][4] < 1 then
								temp_string = string.format("%5.3f",legs_data2_buf[jj][4])
								temp_string = string.sub(temp_string, -4, -1)
							else
								temp_string = string.format("%3d",legs_data2_buf[jj][4])
							end
							
							val_changed = false
							if legs_data2_buf[jj][52] > 0 and legs_data2_buf[jj][52] <= legs_num and legs_num > 0 then
								if legs_data2_buf[jj][4] ~= legs_data[legs_data2_buf[jj][52]][4] or legs_data2_buf[jj][42] ~= legs_data[legs_data2_buf[jj][52]][42] then
									val_changed = true
								end
							end
							
							if jj ~= B738DR_rest_wpt_spd_idx or legs_num == 0 or rte12_act ~= use_rte12_sel then
								if val_changed == true and rte12_act == use_rte12_sel then
									if legs_data2_buf[jj][42] == 0 then
										line_right_in[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										line_right_in[ii] = temp_string .. "B"
									else
										line_right_in[ii] = temp_string .. "A"
									end
									right_line[ii] = "    "
								else
									if legs_data2_buf[jj][42] == 0 then
										right_line[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										right_line[ii] = temp_string .. "B"
									else
										right_line[ii] = temp_string .. "A"
									end
								end
								line_s[ii] = "                 "
								line_m[ii] = "    "
							else
								right_line[ii] = "    "
								line_s[ii] = "                 "
								
								if val_changed == true then
									line_m[ii] = "    "
									if legs_data2_buf[jj][42] == 0 then
										line_right_in[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										line_right_in[ii] = temp_string .. "B"
									else
										line_right_in[ii] = temp_string .. "A"
									end
								else
									if legs_data2_buf[jj][42] == 0 then
										line_m[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										line_m[ii] = temp_string .. "B"
									else
										line_m[ii] = temp_string .. "A"
									end
								end
							end
						end
						
						-- altitude
						if legs_data2_buf[jj][5] == 0 then
							if legs_data2_buf[jj][11] == 0 then
								right_line[ii] = right_line[ii] .. "/----- "
								line_m[ii] = line_m[ii] .. "       "
							else
								if fms_recalc == 0 then
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										temp_string = " " .. fl_format(legs_data2_buf[jj][11], B738DR_trans_alt_mod)
									else
										temp_string = " " .. fl_format(legs_data2_buf[jj][11], B738DR_trans_lvl_mod)
									end
								else
									temp_string = " -----"
								end
								line_s[ii] = line_s[ii] .. temp_string
								right_line[ii] = right_line[ii] .. "/      "
								line_m[ii] = line_m[ii] .. "       "
							end
							line_right_in[ii] = line_right_in[ii] .. "       "
						else
							val_changed = false
							if legs_data2_buf[jj][52] > 0 and legs_data2_buf[jj][52] <= legs_num and legs_num > 0 then
								if legs_data2_buf[jj][5] ~= legs_data[legs_data2_buf[jj][52]][5] or legs_data2_buf[jj][6] ~= legs_data[legs_data2_buf[jj][52]][6] then
									val_changed = true
								end
							end
							
							if jj ~= B738DR_rest_wpt_alt_idx2 or legs_num == 0 or rte12_act ~= use_rte12_sel then
								if val_changed == true and rte12_act == use_rte12_sel then
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									end
									
									if legs_data2_buf[jj][6] == 41 then	--between
										right_line[ii] = right_line[ii] .. "/          "
									else
										right_line[ii] = right_line[ii] .. "/      "
									end
								else
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										right_line[ii] = right_line[ii] .. "/" .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											right_line[ii] = right_line[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											right_line[ii] = right_line[ii] .. "B"
										else
											right_line[ii] = right_line[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										right_line[ii] = right_line[ii] .. "/" .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											right_line[ii] = right_line[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											right_line[ii] = right_line[ii] .. "B"
										else
											right_line[ii] = right_line[ii] .. " "
										end
									end
									
									if legs_data2_buf[jj][6] == 41 then	--between
										line_right_in[ii] = line_right_in[ii] .. "           "
									else
										line_right_in[ii] = line_right_in[ii] .. "       "
									end
								end
								
								if legs_data2_buf[jj][6] == 41 then	--between
									line_m[ii] = line_m[ii] .. "               "
								else
									line_m[ii] = line_m[ii] .. "       "
								end
							else
								if val_changed == true then
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										-- line_m[ii] = line_m[ii] .. " "
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									end
									
									if legs_data2_buf[jj][6] == 41 then	--between
										line_m[ii] = line_m[ii] .. "           "
									else
										line_m[ii] = line_m[ii] .. "       "
									end
								else
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										line_m[ii] = line_m[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_m[ii] = line_m[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_m[ii] = line_m[ii] .. "B"
										else
											line_m[ii] = line_m[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										line_m[ii] = line_m[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_m[ii] = line_m[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_m[ii] = line_m[ii] .. "B"
										else
											line_m[ii] = line_m[ii] .. " "
										end
									end
									
									if legs_data2_buf[jj][6] == 41 then	--between
										line_right_in[ii] = line_right_in[ii] .. "           "
									else
										line_right_in[ii] = line_right_in[ii] .. "       "
									end
								end
								
								if legs_data2_buf[jj][6] == 41 then	--between
									right_line[ii] = right_line[ii] .. "/           "
								else
									right_line[ii] = right_line[ii] .. "/      "
								end
							end
						end
							
							
						-----------------------------------------------------------------
						
						if ii < 5 then
							left_line_x[ii+1] = ""
						end
					else
						overwrite_disc = 0
						if legs_data2_buf[jj][1] == "DISCONTINUITY" or overwrite_disc == 1 then
							left_line_x[ii] = " THEN"
							left_line[ii] = "*****"
							right_line[ii] = ""
							if B738DR_no_rte2 == 0 and act_page_buf == 1 then
								if ii == 4 then
									line5_x = "-- ROUTE DISCONTINUITY -"
								else
									left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
								end
							else
								if ii == 5 then
									line6_x = "-- ROUTE DISCONTINUITY -"
								else
									left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
								end
							end
							discon_last = 1
						else
							-- id
							if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == use_rte12_sel then
								left_line[ii] = "     "
								line_in[ii] = legs_data2_buf[jj][1]
							else
								left_line[ii] = legs_data2_buf[jj][1]
							end
							if map_mode_in == 3 and step_loc == jj and legs_data2_buf[jj][6] ~= 41 then
								line_ctr[ii] = 1
							end
							if discon_last == 1 then
								discon_last = 0
							else
								-- is HOLD ?
								if legs_data2_buf[jj][21] == -1 or legs_data2_buf[jj][21] > 1 then
									-- course
									
									if string.sub(legs_data2_buf[jj][31], 1, 1) == "C" or string.sub(legs_data2_buf[jj][31], 1, 1) == "V" then
										sid_len = math.deg(legs_data2_buf[jj][18])
									elseif legs_data2_buf[jj][31] == "TF" or legs_data2_buf[jj][31] == "IF" or legs_data2_buf[jj][31] == "" then
										-- sid_len = (math.deg(legs_data2_buf[jj][2] + mag_variation_rad(math.rad(legs_data2_buf[jj][7]), math.rad(legs_data2_buf[jj][8]))) + 360) % 360
										-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
										lat_tmp = legs_data2_buf[jj][7]
										lon_tmp = legs_data2_buf[jj][8]
										-- sid_len = (math.deg(legs_data2_buf[jj][2] + mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))) + 360) % 360
										if jj > 1 then
											sid_len = (math.deg(legs_data2_buf[jj][2] + mag_variation_rad(math.rad(legs_data2_buf[jj-1][7]), math.rad(legs_data2_buf[jj-1][8]))) + 360) % 360
										else
											sid_len = (math.deg(legs_data2_buf[jj][2] + mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))) + 360) % 360
										end
									elseif legs_data2_buf[jj][31] == "RF" or legs_data2_buf[jj][31] == "AF" then
										if jj <= legs_num2_buf then
											if string.sub(legs_data2_buf[jj+1][31], 1, 1) == "C" or string.sub(legs_data2_buf[jj+1][31], 1, 1) == "V" then
												sid_len = math.deg(legs_data2_buf[jj+1][18])
											else
												-- sid_len = (math.deg(legs_data2_buf[jj+1][2] + mag_variation_rad(math.rad(legs_data2_buf[jj+1][7]), math.rad(legs_data2_buf[jj+1][8]))) + 360) % 360
												-- lat_tmp, lon_tmp = lat_lon_legs2(jj+1)
												lat_tmp = legs_data2_buf[jj+1][7]
												lon_tmp = legs_data2_buf[jj+1][8]
												sid_len = (math.deg(legs_data2_buf[jj+1][2] + mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))) + 360) % 360
											end
										else
											sid_len = (math.deg(legs_data2_buf[jj][2] + mag_variation_rad(math.rad(lat_tmp), math.rad(lon_tmp))) + 360) % 360
										end
									elseif legs_data2_buf[jj][31] == "DF" then
										if jj > 0 and jj < FMC_MAX_WPT and legs_num > 1 then
											sid_len = B738DR_fms_legs_df_crs_2[jj-1]
										else
											sid_len = math.deg(legs_data2_buf[jj][18])
										end
									else
										sid_len = math.deg(legs_data2_buf[jj][18])
									end
									
									-- sid_len = (sid_len + 360) % 360
									sid_len = displ_crs(sid_len)
									
									if legs_data2_buf[jj][31] == "AF" or legs_data2_buf[jj][31] == "RF" then
										fms_dist_act = wpt_distance2(jj, use_rte12_sel)
										if fms_dist_act < 10 then
											left_line_x[ii] = " " .. string.format("%4.1f",fms_dist_act)
										else
											left_line_x[ii] = " " .. string.format("%4d",fms_dist_act)
										end
										left_line_x[ii] = left_line_x[ii] .. " ARC "
										if legs_data2_buf[jj][21] == 2 then
											left_line_x[ii] = left_line_x[ii] .. "L     "
										else
											left_line_x[ii] = left_line_x[ii] .. "R     "
										end
									elseif legs_data2_buf[jj][31] == "PI" then
										left_line_x[ii] = " PROC TURN "
										if legs_data2_buf[jj][21] == 2 then
											left_line_x[ii] = left_line_x[ii] .. "L    "
										else
											left_line_x[ii] = left_line_x[ii] .. "R    "
										end
									else
										left_line_x[ii] = " " .. string.format("%3d",sid_len)
										if string.sub(legs_data2_buf[jj][31], 1, 1) == "V" then
											left_line_x[ii] = left_line_x[ii] .. "`HDG  "
										else
											left_line_x[ii] = left_line_x[ii] .. "`     "
										end
										
										-- distance
										leg_dist = wpt_distance2(jj, use_rte12_sel)
										if leg_dist > 0 then
											if leg_dist < 10 then
												left_line_x[ii] = left_line_x[ii] .. string.format("%4.1f",leg_dist)
											else
												left_line_x[ii] = left_line_x[ii] .. string.format("%4d",leg_dist)
											end
											left_line_x[ii] = left_line_x[ii] .. "NM"
										else
											left_line_x[ii] = left_line_x[ii] .. "      "
										end
									end
										
									show_gp = 0
									-- GP
									if legs_data2_buf[jj][20] ~= 0 and string.sub(legs_data2_buf[jj][1], 1, 2) ~= "RX" 
									and jj > rnav_idx_first_mod_buf and rnav_idx_first_mod_buf ~= 0 then
										left_line_x[ii] = left_line_x[ii] .. " GP" .. string.format("%4.2f", -legs_data2_buf[jj][20]) .. "`"
										show_gp = 1
									else
										lat_tmp = legs_data2_buf[jj][45]
										if lat_tmp ~= 0 then
											left_line_x[ii] = left_line_x[ii] .. " OFS"
											if lat_tmp < 0 then
												left_line_x[ii] = left_line_x[ii] .. " L"
												if lat_tmp > -9.5 then
													left_line_x[ii] = left_line_x[ii] .. string.format("%1d", -lat_tmp)
												else
													left_line_x[ii] = left_line_x[ii] .. string.format("%2d", -lat_tmp)
												end
											else
												left_line_x[ii] = left_line_x[ii] .. " R"
												if lat_tmp < 9.5 then
													left_line_x[ii] = left_line_x[ii] .. string.format("%1d", lat_tmp)
												else
													left_line_x[ii] = left_line_x[ii] .. string.format("%2d", lat_tmp)
												end
											end
											show_gp = 1
										end
									end
									
									if show_gp == 0 then
										-- BYPASS
										if legs_data2_buf[jj][1] ~= "DISCONTINUITY" and legs_data2_buf[jj][1] ~= "(VECTOR)" then
											if legs_data2_buf[jj][3] == 0 and legs_data2_buf[jj][31] ~= "PI" then
												left_line_x[ii] = left_line_x[ii] .. "  BYPASS"
											elseif jj <= legs_num and jj >= 0 and legs_delete == 0 then
												if B738DR_fms_legs_bypass[jj-1] == 1 then
													left_line_x[ii] = left_line_x[ii] .. "  BYPASS"
												end
											end
										end
									end
								else
									if legs_data2_buf[jj][31] == "HF" then
										left_line_x[ii] = " PROC HOLD"
										left_line[ii] = "(INBD)"
										line_in[ii] = ""
									else
										left_line_x[ii] = " HOLD AT"
									end
								end
							end
						
						
						-- speed
						show_gp = 0
						if rnav_idx_last_mod_buf ~= 0 and ed_found_mod ~= 0 and rnav_idx_last_mod_buf <= legs_num2_buf and jj == rnav_idx_last_mod_buf then
							if B738DR_fms_approach_speed > 0 then
								if string.sub(legs_data2_buf[rnav_idx_last_mod_buf][1], 1, 2) == "RW" then
									show_gp = 1
								end
							end
						end
						
						if show_gp ~= 0 and legs_data2_buf[jj][10] >= 100 then
							temp_string = string.format("%3d",legs_data2_buf[jj][10])
							right_line[ii] = " " .. temp_string
							line_s[ii] = "                 "
							line_m[ii] = "    "
						elseif legs_data2_buf[jj][4] == 0 then
							if legs_data2_buf[jj][10] == 0 then
								right_line[ii] = "----"
								if legs_data2_buf[jj][6] == 41 then	--between
									line_s[ii] = "            "
								else
									line_s[ii] = "                 "
								end
								line_m[ii] = "    "
							else
								if fms_recalc == 0 then
									if legs_data2_buf[jj][10] < 1 then
										temp_string = string.format("%5.3f",legs_data2_buf[jj][10])
										temp_string = string.sub(temp_string, -4, -1)
									else
										temp_string = string.format("%4d",legs_data2_buf[jj][10])
									end
								else
									temp_string = "----"
								end
								if legs_data2_buf[jj][6] == 41 then	--between
									line_s[ii] = "        " .. temp_string
								else
									line_s[ii] = "             " .. temp_string
								end
								right_line[ii] = "    "
								line_m[ii] = "    "
							end
						else
							if legs_data2_buf[jj][4] < 1 then
								temp_string = string.format("%5.3f",legs_data2_buf[jj][4])
								temp_string = string.sub(temp_string, -4, -1)
							else
								temp_string = string.format("%3d",legs_data2_buf[jj][4])
							end
							
							val_changed = false
							if legs_data2_buf[jj][52] > 0 and legs_data2_buf[jj][52] <= legs_num and legs_num > 0 then
								if legs_data2_buf[jj][4] ~= legs_data[legs_data2_buf[jj][52]][4] or legs_data2_buf[jj][42] ~= legs_data[legs_data2_buf[jj][52]][42] then
									val_changed = true
								end
							end
							
							if jj ~= B738DR_rest_wpt_spd_idx or legs_num == 0 or rte12_act ~= use_rte12_sel then
								if val_changed == true and rte12_act == use_rte12_sel then
									if legs_data2_buf[jj][42] == 0 then
										line_right_in[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										line_right_in[ii] = temp_string .. "B"
									else
										line_right_in[ii] = temp_string .. "A"
									end
								else
									if legs_data2_buf[jj][42] == 0 then
										right_line[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										right_line[ii] = temp_string .. "B"
									else
										right_line[ii] = temp_string .. "A"
									end
								end
								line_s[ii] = "                 "
								line_ml[ii] = "    "
							else
								right_line[ii] = "    "
								line_s[ii] = "                 "
								line_ml[ii] = "    "
								
								if val_changed == true then
									line_m[ii] = "    "
									if legs_data2_buf[jj][42] == 0 then
										line_right_in[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										line_right_in[ii] = temp_string .. "B"
									else
										line_right_in[ii] = temp_string .. "A"
									end
									
									
								else
									if legs_data2_buf[jj][42] == 0 then
										line_m[ii] = " " .. temp_string
									elseif legs_data2_buf[jj][42] == 1 then
										line_m[ii] = temp_string .. "B"
									else
										line_m[ii] = temp_string .. "A"
									end
								end
							end
						end
						
						-- altitude
						if legs_data2_buf[jj][5] == 0 then
							if legs_data2_buf[jj][11] == 0 then
								right_line[ii] = right_line[ii] .. "/----- "
								line_m[ii] = line_m[ii] .. "       "
							else
								if fms_recalc == 0 then
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										temp_string = " " .. fl_format(legs_data2_buf[jj][11], B738DR_trans_alt_mod)
									else
										temp_string = " " .. fl_format(legs_data2_buf[jj][11], B738DR_trans_lvl_mod)
									end
								else
									temp_string = " -----"
								end
								line_s[ii] = line_s[ii] .. temp_string
								right_line[ii] = right_line[ii] .. "/      "
								line_m[ii] = line_m[ii] .. "       "
							end
							line_right_in[ii] = line_right_in[ii] .. "       "
						else
							val_changed = false
							if legs_data2_buf[jj][52] > 0 and legs_data2_buf[jj][52] <= legs_num and legs_num > 0 then
								if legs_data2_buf[jj][5] ~= legs_data[legs_data2_buf[jj][52]][5] or legs_data2_buf[jj][6] ~= legs_data[legs_data2_buf[jj][52]][6] then
									val_changed = true
								end
							end
							
							if jj ~= B738DR_rest_wpt_alt_idx2 or legs_num == 0 or rte12_act ~= use_rte12_sel then
								if val_changed == true and rte12_act == use_rte12_sel then
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									end
									
									if legs_data2_buf[jj][6] == 41 then	--between
										right_line[ii] = right_line[ii] .. "/          "
									else
										right_line[ii] = right_line[ii] .. "/      "
									end
								else
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										right_line[ii] = right_line[ii] .. "/" .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											right_line[ii] = right_line[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											right_line[ii] = right_line[ii] .. "B"
										else
											right_line[ii] = right_line[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										right_line[ii] = right_line[ii] .. "/" .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											right_line[ii] = right_line[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											right_line[ii] = right_line[ii] .. "B"
										else
											right_line[ii] = right_line[ii] .. " "
										end
									end
									
									if legs_data2_buf[jj][6] == 41 then	--between
										line_right_in[ii] = line_right_in[ii] .. "           "
									else
										line_right_in[ii] = line_right_in[ii] .. "       "
									end
								end
								if legs_data2_buf[jj][6] == 41 then	--between
									line_m[ii] = line_m[ii] .. "           "
								else
									line_m[ii] = line_m[ii] .. "       "
								end
							else
								if val_changed == true then
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										-- line_m[ii] = line_m[ii] .. "      "
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										line_right_in[ii] = line_right_in[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_right_in[ii] = line_right_in[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_right_in[ii] = line_right_in[ii] .. "B"
										else
											line_right_in[ii] = line_right_in[ii] .. " "
										end
									end
									if legs_data2_buf[jj][6] == 41 then	--between
										line_m[ii] = line_m[ii] .. "           "
									else
										line_m[ii] = line_m[ii] .. "       "
									end
								else
									if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_alt)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
										end
										line_m[ii] = line_m[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_m[ii] = line_m[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_m[ii] = line_m[ii] .. "B"
										else
											line_m[ii] = line_m[ii] .. " "
										end
									else
										if legs_data2_buf[jj][6] == 41 then	--between
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod)
										else
											temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
										end
										line_m[ii] = line_m[ii] .. " " .. temp_string
										if legs_data2_buf[jj][6] == 43 then
											line_m[ii] = line_m[ii] .. "A"
										elseif legs_data2_buf[jj][6] == 45 then
											line_m[ii] = line_m[ii] .. "B"
										else
											line_m[ii] = line_m[ii] .. " "
										end
									end
									if legs_data2_buf[jj][6] == 41 then	--between
										line_right_in[ii] = line_right_in[ii] .. "           "
									else
										line_right_in[ii] = line_right_in[ii] .. "       "
									end
								end
								if legs_data2_buf[jj][6] == 41 then	--between
									right_line[ii] = right_line[ii] .. "/           "
								else
									right_line[ii] = right_line[ii] .. "/      "
								end
							end
						end
						
						
					-----------------------------------------------------------------------------
					
					
					
							-- clear line
							if ii < 5 then
								left_line_x[ii+1] = ""
							end
						end
					end
				end
			end
		else
			if ref_icao_x ~= "----" and des_icao_x ~= "****" and legs_num2_buf >= 1 then
				left_line[1] = "-----"
				right_line[1] = ""
			else
				max_page_legs = 1
				left_line[1] = " "
				left_line[2] = " "
				left_line[3] = " "
				left_line[4] = " "
				left_line[5] = " "
			end
		end
		
		-- RTE2 not installed -> abeam pts available
		local ab_avail = 0
		local ab_avail_line = 0
		if B738DR_no_rte2 ~= 0 and act_page_buf == 1 then
			if exec_light_in == 1 and legs_intdir ~= 0 then
				if B738DR_legs_abeam_avail ~= 0 then
					ab_avail = 1
					ab_avail_line = 5
				end
			end
		elseif B738DR_no_rte2 == 0 and act_page_buf == 1 then
			if exec_light_in == 1 and legs_intdir ~= 0 and legs_intdir_rte == rte12_act then
				if B738DR_legs_abeam_avail ~= 0 then
					ab_avail = 2
					ab_avail_line = 4
				end
			end
		end
		-- display engine
		for ii = 1, 5 do
			left_line[ii] = spaces_after(left_line[ii], 8)
			if ab_avail == 0 or ii < ab_avail_line then
				right_line[ii] = spaces_before(right_line[ii], 16)
				left_line[ii] = left_line[ii] .. right_line[ii]
			elseif ab_avail == 1 then
				-- left_line_x[5] = "              ----------"
				if string.len(left_line_x[5]) > 7 then
					left_line_x[5] = string.sub(left_line_x[5], 1, 7) .. "       ----------"
				else
					left_line_x[5] = "              ----------"
				end
				line_s[5] = ""
				line_m[5] = ""
				if legs_abeam_selected == 0 then
					left_line[5] = left_line[5] .. "       ABEAM PTS>"
					line_in[5] = ""
				else
					line_in[5] = "              ABEAM*PTS "
				end
			elseif ab_avail == 2 then
				line_s[4] = ""
				line_m[4] = ""
				line_in[4] = ""
				-- left_line[4] = left_line[4] .. "        RTE COPY>"
				if B738DR_no_rte2 == 0 and exec_light_in == 0 then
					if legs_num > 1 and rte12_act == use_rte12_sel and use_rte12_sel == 0 then
						if rte12_copy_done < 2 then
							left_line[4] = left_line[4] .. "        RTE COPY>"
							if string.len(left_line_x[4]) > 7 then
								left_line_x[4] = string.sub(left_line_x[4], 1, 7) .. "       ----------"
							else
								left_line_x[4] = "              ----------"
							end
						else
							left_line[4] = left_line[4] .. "         COMPLETE"
							if string.len(left_line_x[4]) > 7 then
								left_line_x[4] = string.sub(left_line_x[4], 1, 7) .. "       --RTE COPY"
							else
								left_line_x[4] = "              ----------"
							end
						end
					elseif legs_num > 1 and rte12_act == use_rte12_sel and use_rte12_sel == 1 then
						if rte21_copy_done < 2 then
							left_line[4] = left_line[4] .. "        RTE COPY>"
							if string.len(left_line_x[4]) > 7 then
								left_line_x[4] = string.sub(left_line_x[4], 1, 7) .. "       ----------"
							else
								left_line_x[4] = "              ----------"
							end
						else
							left_line[4] = left_line[4] .. "         COMPLETE"
							if string.len(left_line_x[4]) > 7 then
								left_line_x[4] = string.sub(left_line_x[4], 1, 7) .. "       --RTE COPY"
							else
								left_line_x[4] = "              ----------"
							end
						end
					end
				else
					-- left_line_x[4] = "              ----------"
					if string.len(left_line_x[4]) > 7 then
						left_line_x[4] = string.sub(left_line_x[4], 1, 7) .. "       ----------"
					else
						left_line_x[4] = "              ----------"
					end
				end
			end
			if line_ctr[ii] == 1 then
				left_line[ii] = string.sub(left_line[ii], 1, 8) .. "<CTR>" .. string.sub(left_line[ii], -11, -1)		--14
			end
		end
		
		for ii = 1, 5 do
			line_ml[ii] = spaces_after(line_ml[ii], 8)
			line_m[ii] = spaces_before(line_m[ii], 16)
			line_in[ii] = spaces_after(line_in[ii], 8)
			line_right_in[ii] = spaces_before(line_right_in[ii], 16)
		end
		
		line1_l = left_line[1]
		line2_l = left_line[2]
		line3_l = left_line[3]
		line4_l = left_line[4]
		line5_l = left_line[5]
		
		line1_x = left_line_x[1]
		line2_x = left_line_x[2]
		line3_x = left_line_x[3]
		line4_x = left_line_x[4]
		line5_x = left_line_x[5]
		
		line1_s = line_s[1]
		line2_s = line_s[2]
		line3_s = line_s[3]
		line4_s = line_s[4]
		line5_s = line_s[5]
		
		line1_m = line_ml[1] .. line_m[1]
		line2_m = line_ml[2] .. line_m[2]
		line3_m = line_ml[3] .. line_m[3]
		line4_m = line_ml[4] .. line_m[4]
		line5_m = line_ml[5] .. line_m[5]
		
		line1_inv = line_in[1] .. line_right_in[1]
		line2_inv = line_in[2] .. line_right_in[2]
		line3_inv = line_in[3] .. line_right_in[3]
		line4_inv = line_in[4] .. line_right_in[4]
		line5_inv = line_in[5] .. line_right_in[5]
		
		max_page_buf = max_page_legs
		
		if exec_light_in == 1 then
			if legs_num > 1 and rte12_act == use_rte12_sel then
				line0_inv = "  MOD"
				line0_l   = "      "
			else
				line0_l = "      "
			end
		else
			if legs_num > 1 and rte12_act == use_rte12_sel then
				line0_l = "  ACT "
			else
				line0_l = "      "
			end
			line0_inv = ""
		end
		
		-- line6_x = "------------------------"
		local line6_x_str = ""
		local line5_x_str = ""
		if B738DR_no_rte2 == 0 and act_page_buf == 1 then
			line5_x_str = "--------"
			-- line6_x_str = "----------------"
		else
			line6_x_str = "--------"
		end
		if exec_light_in == 1 then
			if legs_intdir == 0 then
				if legs_num > 1 and rte12_act == use_rte12_sel then
					if map_mode_in == 3 then
						line6_l = "<ERASE             STEP>"
						if B738DR_no_rte2 == 0 then
							line6_x_str = "----------------MAP CTR "
						else
							line6_x_str = "MAP CTR "
						end
					else
						line6_l = "<ERASE         RTE DATA>"
					end
				else
					if map_mode_in == 3 then
						if B738DR_no_rte2 == 0 then
							-- line6_l = use_rte_option_str .. " LEGS        STEP>"
							line6_l = "<ERASE             STEP>"
							line6_x_str = "----------------MAP CTR "
						else
							-- if B738DR_no_rte2 == 0 then
								-- line6_l = use_rte_option_str .. " LEGS        STEP>"
							-- else
								-- line6_l = "                   STEP>"
							-- end
							line6_l = "<ERASE             STEP>"
							line6_x_str = "MAP CTR "
						end
					else
						-- if B738DR_no_rte2 == 0 then
							-- line6_l = use_rte_option_str .. " LEGS    RTE DATA>"
						-- else
							-- line6_l = "               RTE DATA>"
						-- end
						line6_l = "<ERASE         RTE DATA>"
					end
				end
			else
				if legs_num > 1 and rte12_act == use_rte12_sel then
					line6_l = "<ERASE               "
				end
				if legs_intdir_crs2 < 0 then
					line6_s = "                    "
					line6_s = line6_s .. string.format("%03d", legs_intdir_crs_mod) .. ">"
				else
					line6_l = line6_l .. string.format("%03d", legs_intdir_crs_mod)
				end
				if B738DR_no_rte2 == 0 then
					line6_x_str = "----------------INTC CRS"
				else
					line6_x_str = "INTC CRS"
				end
			end
		else
			if legs_num > 1 and rte12_act == use_rte12_sel then
				if map_mode_in == 3 then
					if new_hold_in ~= 0 and use_new_hold_at == 0 then
						line6_l = "*****"
						line6_x_str = "--------HOLD AT---------"
						if in_flight_mode == 1 and (nav_mode ~= 3 or use_rte12_sel ~= rte12_act) then
							line6_l = line6_l .. "              PPOS>"
						end
					else
						if B738DR_no_rte2 == 0 then
							line6_l = use_rte_option_str .. " LEGS        STEP>"
						else
							line6_l = "                   STEP>"
						end
						if B738DR_no_rte2 == 0 then
							line6_x_str = "----------------MAP CTR "
						else
							line6_x_str = "MAP CTR "
						end
					end
				else
					if new_hold_in == 0 then
						if B738DR_no_rte2 == 0 and act_page_buf == 1 then
							line6_l = use_rte_option_str .. " LEGS    RTE DATA>"
						else
							if B738DR_no_rte2 == 0 then
								line6_l = use_rte_option_str .. " LEGS    RTE DATA>"
							else
								line6_l = "               RTE DATA>"
							end
						end
					else
						if use_new_hold_at == 0 then
							line6_l = "*****"
							line6_x_str = "--------HOLD AT---------"
							if in_flight_mode == 1 and (nav_mode ~= 3 or use_rte12_sel ~= rte12_act) then
								line6_l = line6_l .. "              PPOS>"
							end
						else
							if B738DR_no_rte2 == 0 and act_page_buf == 1 then
								line6_l = use_rte_option_str .. " LEGS    RTE DATA>"
							else
								if B738DR_no_rte2 == 0 then
									line6_l = use_rte_option_str .. " LEGS    RTE DATA>"
								else
									line6_l = "               RTE DATA>"
								end
							end
						end
					end
				end
			else
				if legs_num2_buf > 1 and rte12_act ~= use_rte12_sel then
					if map_mode_in == 3 then
						if new_hold_in ~= 0 and use_new_hold_at == 0 then
							line6_l = "*****"
							line6_x_str = "--------HOLD AT---------"
							if in_flight_mode == 1 and (nav_mode ~= 3 or use_rte12_sel ~= rte12_act) then
								line6_l = line6_l .. "              PPOS>"
							end
						elseif B738DR_no_rte2 == 0 then
							line6_l = use_rte_option_str .. " LEGS        STEP>"
							line6_x_str = "----------------MAP CTR "
						else
							if B738DR_no_rte2 == 0 then
								line6_l = use_rte_option_str .. " LEGS        STEP>"
							else
								line6_l = "                   STEP>"
							end
							line6_x_str = "MAP CTR "
						end
					else
						if new_hold_in ~= 0 and use_new_hold_at == 0 then
							line6_l = "*****"
							line6_x_str = "--------HOLD AT---------"
							if in_flight_mode == 1 and (nav_mode ~= 3 or use_rte12_sel ~= rte12_act) then
								line6_l = line6_l .. "              PPOS>"
							end
						elseif B738DR_no_rte2 == 0 then
							line6_l = use_rte_option_str .. " LEGS    ACTIVATE>"
						else
							line6_l = "               ACTIVATE>"
						end
					end
				else
					if map_mode_in == 3 then
						line6_l = "                   STEP>"
						if B738DR_no_rte2 == 0 then
							line6_x_str = "----------------MAP CTR "
						else
							line6_x_str = "MAP CTR "
						end
					else
						if B738DR_no_rte2 == 0 then
							line6_l = use_rte_option_str .. " LEGS    RTE DATA>"
						else
							line6_l = "               RTE DATA>"
						end
					end
				end
			end
		end
		
		if legs_num > 1 and rte12_act == use_rte12_sel then
			line0_l = line0_l .. use_rte_header_str .. " LEGS        "
		else
			line0_c = "      " .. use_rte_header_str .. " LEGS"
			line0_l = line0_l .. "                   "
		end
		
		line0_s = string.format("%2d", act_page_buf)
		line0_s = line0_s .. "/"
		if max_page_buf > 9 then
			line0_s = line0_s .. string.format("%2d",max_page_buf)
		else
			line0_s = line0_s .. string.format("%1d",max_page_buf)
		end
		line0_s = spaces_before(line0_s, 23)
		
		-- RNP
		if B738DR_no_rte2 == 0 and act_page_buf == 1 then
			if (B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2) then
				if ab_avail == 2 then
					line5_x = "-RNP/ACTUAL"
				else
					line5_x = "-RNP/ACTUAL-----" .. line5_x_str
				end
				if B738DR_rnp < 10 then
					line5_s = string.format("%4.2f", B738DR_rnp) .. "/"
				else
					line5_s = string.format("%4.1f", B738DR_rnp) .. "/"
				end
				if B738DR_anp < 9.995 then
					line5_s = line5_s .. string.format("%4.2f", B738DR_anp) .. "NM"
				else
					line5_s = line5_s .. "-.--NM"
				end
				-- if legs_abeam_data_n > 0 and legs_intdir ~= 0 then
				if B738DR_legs_abeam_avail ~= 0 and legs_intdir ~= 0 and legs_intdir_rte == rte12_act then
					if legs_abeam_selected == 0 then
						line5_l =   "              ABEAM PTS>"
					else
						line5_inv = "              ABEAM*PTS "
					end
				end
			else
				if ab_avail == 2 then
					line5_x = ""
				else
					line5_x = "----------------" .. line5_x_str
				end
			end
			line6_x = line6_x_str
		elseif B738DR_no_rte2 ~= 0 then
			if string.sub(line6_l, 1, 1) == " " then
				if (B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2) then
					line6_x = "-RNP/ACTUAL-----" .. line6_x_str
					if B738DR_rnp < 10 then
						line6_s = string.format("%4.2f", B738DR_rnp) .. "/"
					else
						line6_s = string.format("%4.1f", B738DR_rnp) .. "/"
					end
					if B738DR_anp < 9.995 then
						line6_s = line6_s .. string.format("%4.2f", B738DR_anp) .. "NM"
					else
						line6_s = line6_s .. "-.--NM"
					end
				else
					-- line6_x = "----------------" .. line6_x_str
					line6_x = "----------------" .. line6_x_str
				end
			else
				-- line6_x = line6_x_str
				line6_x = "----------------" .. line6_x_str
			end
		elseif act_page_buf > 1 then
			-- line6_x = "----------------" .. line6_x_str
			line6_x = line6_x_str
		end
	-- end

end

-- function time_string(time_num)
	
	-- local tmp_wpt_eta2 = math.floor(time_num)
	-- local tmp_wpt_eta3 = (time_num - tmp_wpt_eta2) * 60
	-- local result = string.format("%02d", tmp_wpt_eta2) .. string.format("%04.1f", tmp_wpt_eta3)
	-- return result
-- end

function B738_rte_data(in_mode)

	local exec_light_in = B738DR_fmc_exec_lights
	local use_rte_header_str = rte_header_str
	local use_rte_option_str = rte_option_str
	local use_rte12_sel = rte12_sel
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
		use_rte_header_str = rte_header_str_fo
		use_rte_option_str = rte_option_str_fo
		use_rte12_sel = rte12_sel_fo
	end
	
	if page_rte_legs == 1 then
		--act_page = 1
		--max_page = 1
		
		local ii = 0
		local jj = 0
		local kk = 0
		local ll = 0
		--local lll = 0
		
		local max_page_legs = 0
		
		raw_table('left_line')
		local left_line = {true, true, true, true, true}
		left_line[1] = ""
		left_line[2] = ""
		left_line[3] = ""
		left_line[4] = ""
		left_line[5] = ""
		raw_table('right_line')
		local right_line = {true, true, true, true, true}
		right_line[1] = ""
		right_line[2] = ""
		right_line[3] = ""
		right_line[4] = ""
		right_line[5] = ""
		raw_table('left_line_x')
		local left_line_x = {true, true, true, true, true}
		left_line_x[1] = ""
		left_line_x[2] = ""
		left_line_x[3] = ""
		left_line_x[4] = ""
		left_line_x[5] = ""
		raw_table('line_s')
		local line_s = {true, true, true, true, true}
		line_s[1] = ""
		line_s[2] = ""
		line_s[3] = ""
		line_s[4] = ""
		line_s[5] = ""
		raw_table('line_ml')
		local line_ml = {true, true, true, true, true}
		line_ml[1] = ""
		line_ml[2] = ""
		line_ml[3] = ""
		line_ml[4] = ""
		line_ml[5] = ""
		raw_table('line_m')
		local line_m = {true, true, true, true, true}
		line_m[1] = ""
		line_m[2] = ""
		line_m[3] = ""
		line_m[4] = ""
		line_m[5] = ""
		raw_table('line_in')
		local line_in = {true, true, true, true, true}
		line_in[1] = ""
		line_in[2] = ""
		line_in[3] = ""
		line_in[4] = ""
		line_in[5] = ""
		
		local sid_len = 0
		local temp_string = ""
		local overwrite_disc = 0
		local discon_last = 0
		
		local allign_ok = 0
		
		local disable_page = 0
		
		copy_to_legs_data2_buf(rte12_sel)
		
		if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
			allign_ok = 1
		end
		
		local use_offset_mod = 0
		if use_rte12_sel == 0 then
			use_offset_mod = offset_mod
		else
			use_offset_mod = offset_mod2
		end
		
		local offset_temp = use_offset_mod
		
		jj = math.floor((legs_num2_buf + 2 - offset_temp ) / 5)
		kk = (legs_num2_buf + 2 - offset_temp ) % 5
		if kk > 0 then
			max_page_legs = jj + 1
		else
			max_page_legs = jj
		end
		
		if legs_num2_buf == 0 then
			max_page_legs = 1
		end
		if act_page_buf > max_page_legs then
			act_page_buf = max_page_legs
			if act_page_buf == 0 then
				act_page_buf = 1
			end
		end
		
		if nav_mode == 99 then
			if legs_delete == 0 then
				disable_page = 1
			else
				offset_temp = use_offset_mod + 1
				if offset_temp > legs_num2_buf then
					disable_page = 1
				end
			end
		end
		
		if legs_num2_buf > 1 and disable_page == 0 then
			
			if offset_temp > legs_num2_buf then
				offset_temp = legs_num2_buf
			end
		
			if offset_temp == 0 then
				offset_temp = 1
			end
			
			kk = (act_page_buf - 1) * 5
			local max_items = 5
			local corr_rte2 = 0
			if B738DR_no_rte2 == 0 then
				if act_page_buf > 1 then
					corr_rte2 = 1
				else
					max_items = 4
				end
			end
			for ii = 1, max_items do
				jj = kk + ii + (offset_temp - 1) - corr_rte2
				if ii == 1 then
					line_m[1] = ""
				end
				if jj > legs_num2_buf or jj == 0 then
					left_line_x[ii] = ""
					left_line[ii] = ""
					right_line[ii] = ""
				else
					if act_page_buf == 1 and ii == 1 then
						-- is HOLD ?
						if legs_data2_buf[jj][21] == -1 or legs_data2_buf[jj][21] > 1 then
							-- id
							if legs_data2_buf[jj][1] == "DISCONTINUITY" then
								left_line_x[ii] = " THEN"
								line_ml[1] = ""
								left_line[ii] = "*****"
								right_line[ii] = ""
								left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
								discon_last = 1
							else
								if allign_ok == 0 or offset_temp > legs_num2_buf then
									if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == rte12_sel then
										line_ml[1] = "     "
										left_line[ii] = "     "
										line_in[ii] = legs_data2_buf[jj][1]
									else
										line_ml[1] = "     "
										left_line[ii] = legs_data2_buf[jj][1]
									end
								else
									if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == rte12_sel then
										line_ml[1] = "     "
										left_line[ii] = "     "
										line_in[ii] = legs_data2_buf[jj][1]
									else
										line_ml[1] = legs_data2_buf[jj][1]
										left_line[ii] = "     "
									end
								end
							end
						else
							left_line_x[ii] = " HOLD "
							if legs_data2_buf[jj][21] == 0 then
								left_line_x[ii] = left_line_x[ii] .. "L"
							else
								left_line_x[ii] = left_line_x[ii] .. "R"
							end
							if allign_ok == 0 or offset_temp > legs_num2_buf then
								if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == rte12_sel then
									line_ml[1] = "     "
									left_line[ii] = "     "
									line_in[ii] = legs_data2_buf[jj][1]
								else
									left_line[ii] = legs_data2_buf[jj][1]
									line_ml[1] = "     "
								end
							else
								if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == rte12_sel then
									line_ml[1] = "     "
									left_line[ii] = "     "
									line_in[ii] = legs_data2_buf[jj][1]
								else
									line_ml[1] = legs_data2_buf[jj][1]
									left_line[ii] = "     "
								end
							end
						end
						
						-- eta
						if legs_data2_buf[jj][13] == 0 then
							right_line[ii] = "       "
							line_s[ii] = "                "
						else
							right_line[ii] = string.sub(time_to_string(legs_data2_buf[jj][13]), 1, 4) .. "   "
							line_s[ii] = "             z  "
						end
						
						-- wind
						if legs_data2_buf[jj][39] == "" then
							if td_idx ~= 0 and td_idx < legs_num2_buf then
								if (forec_alt_1_num ~= 0 and forec_spd_1 ~= 0) or jj < td_idx then
									if legs_data2_buf[jj][38] ~= "" then
										line_s[ii] = line_s[ii] .. legs_data2_buf[jj][38]
									end
								end
							end
						else
							right_line[ii] = right_line[ii] .. legs_data2_buf[jj][39]
						end
						
						if ii < 5 then
							left_line_x[ii+1] = ""
						end
					else
						overwrite_disc = 0
						if legs_data2_buf[jj][1] == "DISCONTINUITY" or overwrite_disc == 1 then
							left_line_x[ii] = " THEN"
							left_line[ii] = "*****"
							right_line[ii] = ""
							if ii == 5 then
								line6_x = "-- ROUTE DISCONTINUITY -"
							else
								left_line_x[ii+1] = "-- ROUTE DISCONTINUITY -"
							end
							discon_last = 1
						else
							-- id
							if legs_data2_buf[jj][52] <= 0 and legs_num > 1 and rte12_act == rte12_sel then
								left_line[ii] = "     "
								line_in[ii] = legs_data2_buf[jj][1]
							else
								left_line[ii] = legs_data2_buf[jj][1]
							end
							if B738DR_capt_map_mode == 3 and legs_step == jj then
								left_line[ii] = left_line[ii] .. "<CTR>"
							end
							if discon_last == 1 then
								discon_last = 0
							else
								-- is HOLD ?
								if legs_data2_buf[jj][21] == -1 or legs_data2_buf[jj][21] > 1 then
									left_line_x[ii] = "      "
								else
									left_line_x[ii] = " HOLD "
									if legs_data2_buf[jj][21] == 0 then
										left_line_x[ii] = left_line_x[ii] .. "L"
									else
										left_line_x[ii] = left_line_x[ii] .. "R"
									end
								end
							end
							
							-- eta
							if legs_data2_buf[jj][13] == 0 then
								right_line[ii] = "       "
								line_s[ii] = "                "
							else
								right_line[ii] = string.sub(time_to_string(legs_data2_buf[jj][13]), 1, 4) .. "   "
								line_s[ii] = "             z  "
							end
							
							-- wind
							if legs_data2_buf[jj][39] == "" then
								if td_idx ~= 0 and td_idx < legs_num2_buf then
									if (forec_alt_1_num ~= 0 and forec_spd_1 ~= 0) or jj < td_idx then
										if legs_data2_buf[jj][38] ~= "" then
											line_s[ii] = line_s[ii] .. legs_data2_buf[jj][38]
										end
									end
								end
							else
								right_line[ii] = right_line[ii] .. legs_data2_buf[jj][39]
							end
							
							-- clear line
							if ii < 5 then
								left_line_x[ii+1] = ""
							end
						end
					end
				end
			end
		else
			max_page_legs = 1
			left_line[1] = " "
			left_line[2] = " "
			left_line[3] = " "
			left_line[4] = " "
			left_line[5] = " "
		end
		
		
		-- display engine
		for ii = 1, 5 do
			left_line[ii] = spaces_after(left_line[ii], 9)
			line_ml[ii] = spaces_after(line_ml[ii], 9)
		end
		
		line1_l = left_line[1] .. right_line[1]
		line2_l = left_line[2] .. right_line[2]
		line3_l = left_line[3] .. right_line[3]
		line4_l = left_line[4] .. right_line[4]
		line5_l = left_line[5] .. right_line[5]
		
		line1_x = left_line_x[1]
		line2_x = left_line_x[2]
		line3_x = left_line_x[3]
		line4_x = left_line_x[4]
		line5_x = left_line_x[5]
		
		line1_s = line_s[1]
		line2_s = line_s[2]
		line3_s = line_s[3]
		line4_s = line_s[4]
		line5_s = line_s[5]
		
		line1_m = line_ml[1]
		line2_m = line_ml[2]
		line3_m = line_ml[3]
		line4_m = line_ml[4]
		line5_m = line_ml[5]
		
		line1_inv = line_in[1]
		line2_inv = line_in[2]
		line3_inv = line_in[3]
		line4_inv = line_in[4]
		line5_inv = line_in[5]
		
		max_page_buf = max_page_legs
		
		if exec_light_in == 1 then
			if legs_num > 1 and rte12_act == rte12_sel then
				line0_inv = "  MOD"
				line0_l   = "      "
			else
				line0_l = "      "
			end
		else
			if legs_num > 1 and rte12_act == rte12_sel then
				line0_l = "  ACT "
			else
				line0_l = "      "
			end
			line0_inv = ""
		end
		
		line0_l = line0_l .. use_rte_header_str .. " DATA       "
		line0_s = "                    " .. string.format("%1d",act_page_buf)
		line0_s = line0_s .. "/"
		line0_s = line0_s .. string.format("%1d",max_page_buf)
		line1_x = "          ETA       WIND"
		line6_x = "------------------------"
		line6_l = "                   LEGS>"
	end

end

function B738_fmc_nav_status()
	local nav1_str = ""
	local nav2_str = ""
	local nav1_freq_str = ""
	local nav2_freq_str = ""
	
	max_page_buf = 2
	line0_s = "                    " .. string.format("%1d",act_page_buf)
	line0_s = line0_s .. "/"
	line0_s = line0_s .. string.format("%1d",max_page_buf)
	if act_page_buf == 1 then
		line0_l = "      NAV STATUS"
		-- line1_x =   "   VOR-L        VOR-R   "
		-- line1_l   = "             ARU  115.40"
		-- line1_s   = "    M114.40      M      "
		line2_x =   "   DME-L        DME-R   "
		-- line2_l   = "             ARU  115.40"
		-- line2_s   = "     114.40             "
		-- line3_inv = "ARU                     "
		-- line3_l   = "     115.40             "
		-- line4_inv = "SVD                     "
		-- line4_l   = "     116.50             "
		
		if B738DR_tuned_nav1_type ~= 0 then
			nav1_str = string.format("%05d", B738DR_nav1_hz)
			nav1_freq_str = string.sub(nav1_str, 1, 3) .. "." .. string.sub(nav1_str, 4, 5)
			
			if B738DR_tuned_nav1_type == 1 then
				line1_x =   "   VOR-L     "
				line1_l = spaces_after(simDR_nav1_nav_id, 4) .. " " .. nav1_freq_str .. "  "
				line1_s = "    M        "
			elseif B738DR_tuned_nav1_type == 2 then
				line1_x =   "   LOC-L     "
				line1_l = spaces_after(simDR_nav1_nav_id, 4) .. " " .. nav1_freq_str .. "  "
				line1_s = "    M        "
			elseif B738DR_tuned_nav1_type == 3 then
				line1_x =   "   ILS-L     "
				line1_l = spaces_after(simDR_nav1_nav_id, 4) .. " " .. nav1_freq_str .. "  "
				line1_s = "    M        "
			else
				line1_l = "             "
				line1_s = "    M" .. nav1_freq_str .. "  "
			end
			
			if simDR_nav1_has_dme == 1 then
				if B738DR_tuned_nav1_type == 1 then
					line2_l = spaces_after(simDR_nav1_nav_id, 4) .. " " .. nav1_freq_str .. "  "
					line2_s = "             "
				else
					line2_l = "             "
					line2_s = "     " .. nav1_freq_str .. " "
				end
			else
				line2_l   = "             "
				line2_s   = "             "
			end
			
			line3_inv = spaces_after(simDR_nav1_nav_id, 4)
			line3_l   = "     " .. nav1_freq_str
		else
				line1_x =   "             "
				line1_l   = "             "
				line1_s   = "             "
				line2_l   = "             "
				line2_s   = "             "
		end
		
		if B738DR_tuned_nav2_type ~= 0 then
			nav2_str = string.format("%05d", B738DR_nav2_hz)
			nav2_freq_str = string.sub(nav2_str, 1, 3) .. "." .. string.sub(nav2_str, 4, 5)
			
			if B738DR_tuned_nav2_type == 1 then
				line1_x = line1_x .. "   VOR-R"
				line1_l = line1_l .. spaces_after(simDR_nav2_nav_id, 4) .. " " .. nav2_freq_str
				line1_s = line1_s .. "    M"
			elseif B738DR_tuned_nav2_type == 2 then
				line1_x = line1_x .. "   LOC-R"
				line1_l = line1_l .. spaces_after(simDR_nav2_nav_id, 4) .. " " .. nav2_freq_str
				line1_s = line1_s .. "    M"
			elseif B738DR_tuned_nav2_type == 3 then
				line1_x = line1_x .. "   ILS-R"
				line1_l = line1_l .. spaces_after(simDR_nav2_nav_id, 4) .. " " .. nav2_freq_str
				line1_s = line1_s .. "    M"
			else
				line1_s = line1_s .. "    M" .. nav2_freq_str
			end
			
			if simDR_nav2_has_dme == 1 then
				if B738DR_tuned_nav2_type == 1 then
					line2_l = line2_l .. spaces_after(simDR_nav2_nav_id, 4) .. " " .. nav2_freq_str
				else
					line2_s = line2_s .. "     " .. nav2_freq_str
				end
			end
			
			line4_inv = spaces_after(simDR_nav2_nav_id, 4)
			line4_l   = "     " .. nav2_freq_str
		end
		
		line5_s =   "   GPS (2)    IRS (2)   "
		line6_l =   "<INDEX        POS SHIFT>"
	else
		line0_l = "      NAV OPTIONS"
		line1_x =   "       DME INHIBIT      "
		line1_l   = "----                ----"
		line2_x =   "       VOR INHIBIT      "
		line2_l   = "----                ----"
		line3_x =   " DME UPDATE   GPS UPDATE"
		line3_l   = "<  /               /   >"
		line3_g   = " ON              ON     "
		line3_s =   "    OFF             OFF "
		line4_x =   " VOR UPDATE   LOC UPDATE"
		line4_l   = "<  /               /   >"
		line4_g   = " ON              ON     "
		line4_s =   "    OFF             OFF "
		line6_l =   "<INDEX                  "
		
	end
end

function B738_fmc_hold(in_mode)

	local exec_light_in = B738DR_fmc_exec_lights
	local use_rte12_sel = rte12_sel
	local use_rte_header_str = rte_header_str
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
		use_rte12_sel = rte12_sel_fo
		use_rte_header_str = rte_header_str_fo
	end
	
	if page_hold == 1 then
		
		local x_temp = 0
		local y_temp = 0
		local qq = 0
		local hold_page_offset = 0
		local hold_offset_idx_act = 0
		local best_spd = 0
		
		local use_offset_mod = 0
		local use_hold_offset = 0
		local use_new_hold_act_page = 0
		
		if use_rte12_sel == 0 then
			use_offset_mod = offset_mod
			use_hold_offset = hold_offset
			use_new_hold_act_page = new_hold_act_page
			if in_mode == 0 then
				hold_offset_idx = 0
			else
				hold_offset_idx2 = 0
			end
		else
			use_offset_mod = offset_mod2
			use_hold_offset = hold_offset2
			use_new_hold_act_page = new_hold_act_page2
			if in_mode == 0 then
				hold_offset_idx7 = 0
			else
				hold_offset_idx8 = 0
			end
		end
		
		copy_to_legs_data2_buf(use_rte12_sel)
		copy_to_hold_data2_buf(use_rte12_sel)
		
		-- find hold offset
		use_hold_offset = 0
		if hold_data_num_buf > 0 then
			for qq = 1, hold_data_num_buf do
				if hold_data_buf[qq] >= use_offset_mod then
					use_hold_offset = qq
					break
				end
			end
		end
		
		if use_rte12_sel == 0 then
			hold_offset = use_hold_offset
		else
			hold_offset2 = use_hold_offset
		end
		
		if use_hold_offset > 0 then
		
			-- if in_mode == 0 then
			if use_rte12_sel == 0 then
				if use_new_hold_act_page ~= 0 then
					act_page_buf = use_new_hold_act_page
				end
				
				max_page_buf = hold_data_num_buf - use_hold_offset + 1
				if act_page_buf > max_page_buf then
					act_page_buf = max_page_buf
				end
				hold_page_offset = act_page_buf + use_hold_offset - 1
				
				if in_mode == 0 then
					hold_offset_idx = hold_data_buf[hold_page_offset]
					hold_offset_idx_act = hold_offset_idx
				else
					hold_offset_idx2 = hold_data_buf[hold_page_offset]
					hold_offset_idx_act = hold_offset_idx2
				end
			else
				if use_new_hold_act_page ~= 0 then
					act_page_buf = use_new_hold_act_page
				end
				
				max_page_buf = hold_data_num_buf - use_hold_offset + 1
				if act_page_buf > max_page_buf then
					act_page_buf = max_page_buf
				end
				hold_page_offset = act_page_buf + use_hold_offset - 1
				
				if in_mode == 0 then
					hold_offset_idx7 = hold_data_buf[hold_page_offset]
					hold_offset_idx_act = hold_offset_idx7
				else
					hold_offset_idx8 = hold_data_buf[hold_page_offset]
					hold_offset_idx_act = hold_offset_idx8
				end
			end
			
			-- B738DR_fms_test3 = page_hold
			-- B738DR_fms_test4 = hold_offset_idx_act
			-- B738DR_fms_test5 = hold_data_num_buf
			-- B738DR_fms_test6 = act_page_buf
			
			
			local hold_fix = legs_data2_buf[hold_offset_idx_act][1]
			local hold_quad = "--"
			local hold_radial = "---"
			local hold_radial_num = 0
			local hold_inbd = "---"
			x_temp = tonumber(legs_data2_buf[hold_offset_idx_act][29])
			if x_temp ~= nil then
				x_temp = x_temp / 10
				hold_inbd = string.format("%03d", displ_crs(x_temp))
				hold_radial_num = (x_temp + 180) % 360
				hold_radial = string.format("%03d", displ_crs(hold_radial_num))
				if hold_radial_num <= 22.5 then
					hold_quad = " N"
				elseif hold_radial_num <= 67.5 then
					hold_quad = "NE"
				elseif hold_radial_num <= 112.5 then
					hold_quad = " E"
				elseif hold_radial_num <= 157.5 then
					hold_quad = "SE"
				elseif hold_radial_num <= 202.5 then
					hold_quad = " S"
				elseif hold_radial_num <= 247.5 then
					hold_quad = "SW"
				elseif hold_radial_num <= 292.5 then
					hold_quad = " W"
				elseif hold_radial_num <= 337.5 then
					hold_quad = "NW"
				else
					hold_quad = " N"
				end
			end
			local hold_turn = legs_data2_buf[hold_offset_idx_act][21]
			
			-- default 1.5 mins
			local hold_time = "-.-"
			local hold_dist = "--.-"
			if string.len(legs_data2_buf[hold_offset_idx_act][30]) == 4 then
				if string.sub(legs_data2_buf[hold_offset_idx_act][30], 1, 1) == "T" then
					-- Time
					x_temp = tonumber(string.sub(legs_data2_buf[hold_offset_idx_act][30], 3, 3))
					y_temp = tonumber(string.sub(legs_data2_buf[hold_offset_idx_act][30], 4, 4))
					if x_temp ~= nil and y_temp ~= nil then
						hold_time = tostring(x_temp) .. "." .. tostring(y_temp)
					end
				else
					-- Distance
					x_temp = tonumber(legs_data2_buf[hold_offset_idx_act][30])
					if x_temp ~= nil then
						x_temp = x_temp / 10
						hold_dist = string.format("%4.1f", x_temp)
					end
				end
			end
			
			local hold_alt_txt = ""
			local hold_alt = legs_data2_buf[hold_offset_idx_act][5]
			local hold_alt2 = legs_data2_buf[hold_offset_idx_act][41]
			if hold_alt == 0 then
				if legs_data2_buf[hold_offset_idx_act][1] == "PPOS" then
					hold_alt = math.floor((simDR_altitude_pilot / 100) + 0.5) * 100
				else
					hold_alt = legs_data2_buf[hold_offset_idx_act][11]
				end
			end
			local hold_alt_rest = " "
			if legs_data2_buf[hold_offset_idx_act][6] == 43 then
				hold_alt_rest = "A"
			elseif legs_data2_buf[hold_offset_idx_act][6] == 45 then
				hold_alt_rest = "B"
			end
			
			best_spd = 0
			if B738DR_pfd_min_maneuver_speed > 0 then
				best_spd = B738DR_pfd_min_maneuver_speed * 1.03
			end
			if simDR_flaps_ratio == 0 then
				if best_spd < B738DR_pfd_flaps_up then
					best_spd = B738DR_pfd_flaps_up
				end
			elseif simDR_flaps_ratio == 0.125 then
				if best_spd < B738DR_pfd_flaps_1 then
					best_spd = B738DR_pfd_flaps_1
				end
			elseif simDR_flaps_ratio == 0.375 then
				if best_spd < B738DR_pfd_flaps_5 then
					best_spd = B738DR_pfd_flaps_5
				end
			elseif simDR_flaps_ratio == 0.5 then
				if best_spd < B738DR_pfd_flaps_10 then
					best_spd = B738DR_pfd_flaps_10
				end
			elseif simDR_flaps_ratio == 0.625 then
				if best_spd < B738DR_pfd_flaps_15 then
					best_spd = B738DR_pfd_flaps_15
				end
			end
			
			local hold_spd = legs_data2_buf[hold_offset_idx_act][4]
			if hold_spd == 0 then
				if legs_data2_buf[hold_offset_idx_act][1] == "PPOS" then
					if best_spd ~= 0 then
						hold_spd = best_spd
					else
						hold_spd = AP_airspeed
					end
				else
					hold_spd = legs_data2_buf[hold_offset_idx_act][10]
				end
				if hold_spd > 0 and hold_spd < 100 then
					hold_spd = mach_to_tas(hold_spd, hold_alt)
					hold_spd = tas_to_ias(hold_spd, hold_alt)
				end
			end
			local hold_spd_rest = " "
			if legs_data2_buf[hold_offset_idx_act][42] == 1 then
				hold_spd_rest = "B"
			elseif legs_data2_buf[hold_offset_idx_act][42] == 2 then
				hold_spd_rest = "A"
			end
			
			local hold_eta = 0
			local hold_efc = 0
			local hold_avail = 0
			local hold_best_spd = 0
				
			
			local hold_active = 0
			if legs_num > 1 and hold_offset_idx_act == offset and exec_light_in == 0 and rte12_act == use_rte12_sel then
				hold_active = 1
			end
			
			if hold_active == 0 then
				if exec_light_in == 1 then
					if legs_num2_buf > 1 then
						line0_inv = "  MOD"
						line0_l   = "      "
					else
						line0_l = "      "
					end
				else
					line0_l = "      "
				end
			else
				line0_l = "  ACT "
			end
			
			if hold_active == 0 then
				line1_x   = " FIX         SPD/TGT ALT"
				
				line1_l = spaces_after(hold_fix, 5)
				if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
					line1_l = line1_l .. "  "
				else
					line1_l = line1_l .. "        "
				end
				
				if hold_spd == 0 then
					line1_l = line1_l .. " ---/"
					if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
						line1_s = "             "
					else
						line1_s = "                   "
					end
				else
					if legs_data2_buf[hold_offset_idx_act][4] == 0 then
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							line1_s = "       "
						else
							line1_s = "             "
						end
						line1_l = line1_l .. "    /"
						if hold_spd < 1 then
							line1_s = line1_s .. string.sub(string.format("%5.3f", hold_spd), -4, -1) .. " "
						else
							line1_s = line1_s .. " " .. string.format("%3d", hold_spd) .. " "
						end
					else
						if hold_spd < 1 then
							line1_l = line1_l .. string.sub(string.format("%5.3f", hold_spd), -4, -1) .. "/"
						else
							if legs_data2_buf[hold_offset_idx_act][42] == 0 then
								line1_l = line1_l .. " " .. string.format("%3d", hold_spd) .. "/"
							else
								line1_l = line1_l .. string.format("%3d", hold_spd) .. hold_spd_rest .. "/"
							end
						end
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							line1_s = "            "
						else
							line1_s = "                  "
						end
					end
				end
				
				if hold_alt == 0 then
					line1_l = line1_l .. "-----"
				else
					if legs_data2_buf[hold_offset_idx_act][19] == 1 or legs_data2_buf[hold_offset_idx_act][19] == 3 or hold_offset_idx_act < td_idx then
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							hold_alt_txt = fl_format(hold_alt2, B738DR_trans_alt_mod) .. "B"
							hold_alt_txt = hold_alt_txt .. fl_format(hold_alt, B738DR_trans_alt_mod) .. "A"
						else
							hold_alt_txt = fl_format(hold_alt, B738DR_trans_alt_mod) .. hold_alt_rest
						end
					else
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							hold_alt_txt = fl_format(hold_alt2, B738DR_trans_lvl_mod) .. "B"
							hold_alt_txt = hold_alt_txt .. fl_format(hold_alt, B738DR_trans_lvl_mod) .. "A"
						else
							hold_alt_txt = fl_format(hold_alt, B738DR_trans_lvl_mod) .. hold_alt_rest
						end
					end
					
					if legs_data2_buf[hold_offset_idx_act][5] == 0 then
						line1_s = line1_s .. hold_alt_txt	--string.format("%5d", hold_alt)
					else
						line1_l = line1_l .. hold_alt_txt	--string.format("%5d", hold_alt) .. hold_alt_rest
					end
				end
				
				line2_x   = " QUAD/RADIAL     FIX ETA"
				line2_l   = hold_quad .. "/" .. hold_radial .. "`          ----.- "
				line2_s   = "                       Z"
				
				line3_x   = " INBD CRS/DIR   EFC TIME"
				line3_l = hold_inbd .. "`/"
				if hold_turn == -1 then
					line3_l = line3_l .. "      "
					line3_s = "     R TURN"
				elseif hold_turn == 0 then
					line3_l = line3_l .. "L TURN"
					line3_s = "           "
				else
					line3_l = line3_l .. "R TURN"
					line3_s = "           "
				end
				line3_l = line3_l .. "        ---- "
				line3_s   = line3_s .. "            Z"
				
				line4_x   = " LEG TIME     HOLD AVAIL"
				--line4_l   = "-.-                 ----"
				if hold_time == "-.-" and hold_dist == "--.-" then
					line4_l = "                    "	-- ----"
					line4_s = "1.5"		-- default time
				else
					line4_l = hold_time .. "                 "	-- ----"
					line4_s = "   "
				end
				
				local res_fuel = tonumber(reserves_kgs)
				local ff_time = 0
				local ff_time2 = 0
				local ff_time_str = ""
				if res_fuel == nil then
					res_fuel = 0
				else
					res_fuel = res_fuel * 1000
				end
				if legs_num2_buf > 1 then
					if legs_data2_buf[legs_num2_buf+1][40] ~= 0 and ff_approx > 0 then
						ff_time = legs_data2_buf[legs_num2_buf+1][40] - res_fuel + 200
						ff_time = ff_time / ff_approx
					end
				end
				if ff_time <= 0 then
					ff_time_str = "----"
				else
					ff_time2 = math.floor(ff_time)
					ff_time = (ff_time - ff_time2) * 60
					if ff_time2 > 9 then
						ff_time2 = 9
					end
					if ff_time > 59.49 then
						ff_time = 59
					end
					ff_time_str = string.format("%1d", ff_time2)
					ff_time_str = ff_time_str .. "+" .. string.format("%02d", ff_time)
				end
				line4_l = line4_l .. ff_time_str
				
				line4_s   = line4_s .. "MIN                  "
				
				line5_x   = " LEG DIST     BEST SPEED"
				-- line5_l = hold_dist .. "               ---  "
				line5_l = hold_dist .. "               "
				
				if best_spd > 0 then
					line5_l = line5_l .. string.format("%3d", best_spd)
				else
					line5_l = line5_l .. "---  "
				end
				line5_s   = "    NM                KT"
			else
				line1_x   = " FIX         SPD/TGT ALT"
				line1_l = "             "
				line1_m = spaces_after(hold_fix, 5)
				
				if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
					line1_l = "       "
					line1_m = line1_m .. "  "
				else
					line1_l = "             "
					line1_m = line1_m .. "        "
				end
				
				if hold_spd == 0 then
					line1_l = line1_l .. " ---/"
					line1_m = line1_m .. "     "
					if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
						line1_s = "             "
					else
						line1_s = "                   "
					end
				else
					if legs_data2_buf[hold_offset_idx_act][4] == 0 then
						line1_l = line1_l .. "    /"
						line1_m = line1_m .. "     "
						
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							line1_s = "       "
						else
							line1_s = "             "
						end
						
						if hold_spd < 1 then
							line1_s = line1_s .. string.sub(string.format("%5.3f", hold_spd), -4, -1) .. " "
						else
							line1_s = line1_s .. " " .. string.format("%3d", hold_spd) .. " "
						end
					else
						if hold_spd < 1 then
							line1_m = line1_m .. string.sub(string.format("%5.3f", hold_spd), -4, -1)
						else
							
							if legs_data2_buf[hold_offset_idx_act][42] == 0 then
								line1_m = line1_m .. " " .. string.format("%3d", hold_spd)
							else
								line1_m = line1_m .. string.format("%3d", hold_spd) .. hold_spd_rest
							end
						end
						if legs_data2_buf[hold_offset_idx_act][5] == 0 then
							line1_l = line1_l .. "    /"
							line1_m = line1_m .. " "
						else
							line1_m = line1_m .. "/"
						end
						
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							line1_s = "            "
						else
							line1_s = "                  "
						end
					end
				end
				
				if hold_alt == 0 then
					line1_l = line1_l .. "-----"
				else
					if legs_data2_buf[hold_offset_idx_act][19] == 1 or legs_data2_buf[hold_offset_idx_act][19] == 3 or hold_offset_idx_act < td_idx then
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							hold_alt_txt = fl_format(hold_alt2, B738DR_trans_alt_mod) .. "B"
							hold_alt_txt = hold_alt_txt .. fl_format(hold_alt, B738DR_trans_alt_mod) .. "A"
						else
							hold_alt_txt = fl_format(hold_alt, B738DR_trans_alt_mod) .. hold_alt_rest
						end
					else
						if legs_data2_buf[hold_offset_idx_act][6] == 41 then	--between
							hold_alt_txt = fl_format(hold_alt2, B738DR_trans_lvl_mod) .. "B"
							hold_alt_txt = hold_alt_txt .. fl_format(hold_alt, B738DR_trans_lvl_mod) .. "A"
						else
							hold_alt_txt = fl_format(hold_alt, B738DR_trans_lvl_mod) .. hold_alt_rest
						end
					end
					
					if legs_data2_buf[hold_offset_idx_act][5] == 0 then
						line1_s = line1_s .. hold_alt_txt	--string.format("%5d", hold_alt)
					else
						line1_m = line1_m .. hold_alt_txt	--string.format("%5d", hold_alt) .. hold_alt_rest
					end
				end
				
				line2_x   = " QUAD/RADIAL     FIX ETA"
				
				line2_l   = hold_quad .. "/" .. hold_radial .. "`          ----.- "
				-- line2_l   = "                 ----.- "
				-- line2_m   = hold_quad .. "/" .. hold_radial .. "`"
				line2_s   = "                       Z"
				
				line3_x   = " INBD CRS/DIR   EFC TIME"
				--line3_l   = "---`/R TURN        ---- "
				line3_l = "    /"
				line3_m = hold_inbd .. "` "
				if hold_turn == -1 then
					line3_l = line3_l .. "  TURN"
					line3_m = line3_m .. "R     "
					line3_s = "           "
				elseif hold_turn == 0 then
					line3_l = line3_l .. "  TURN"
					line3_m = line3_m .. "L     "
					line3_s = "           "
				else
					line3_l = line3_l .. "  TURN"
					line3_m = line3_m .. "R     "
					line3_s = "           "
				end
				line3_l = line3_l .. "        ---- "
				line3_s = line3_s .. "            Z"
				
				line4_x   = " LEG TIME     HOLD AVAIL"
				local res_fuel = tonumber(reserves_kgs)
				local ff_time = 0
				local ff_time2 = 0
				local ff_time_str = ""
				if res_fuel == nil then
					res_fuel = 0
				else
					res_fuel = res_fuel * 1000
				end
				if legs_num > 1 then
					if legs_data[legs_num+1][40] ~= 0 and ff_approx > 0 then
						ff_time = legs_data[legs_num+1][40] - res_fuel + 200
						ff_time = ff_time / ff_approx
					end
				end
				if ff_time <= 0 then
					ff_time_str = "----"
				else
					ff_time2 = math.floor(ff_time)
					ff_time = (ff_time - ff_time2) * 60
					if ff_time2 > 9 then
						ff_time2 = 9
					end
					if ff_time > 59.49 then
						ff_time = 59
					end
					ff_time_str = string.format("%1d", ff_time2)
					ff_time_str = ff_time_str .. "+" .. string.format("%02d", ff_time)
				end
				--line4_l   = "-.-                 ----"
				if hold_time == "-.-" and hold_dist == "--.-" then
					line4_l = "                    " .. ff_time_str
					line4_m = "1.5"		-- default time
					line4_s = "   "
				else
					if hold_time == "-.-" then
						line4_l = hold_time .. "                 "
						line4_l = line4_l .. ff_time_str
						line4_s = "   "
					else
						line4_l = "                    "
						line4_l = line4_l .. ff_time_str
						line4_m = hold_time
						line4_s = "   "
					end
				end
				line4_s   = line4_s .. "MIN                  "
				
				line5_x   = " LEG DIST     BEST SPEED"
				--line5_l   = "--.-               ---  "
				if hold_dist == "--.-" then
					-- line5_l = hold_dist .. "               ---  "
					line5_l = hold_dist .. "               "
				else
					-- line5_l = "                   ---  "
					line5_l = "                   "
					line5_m = hold_dist
				end
				
				if best_spd > 0 then
					line5_l = line5_l .. string.format("%3d", best_spd)
				else
					line5_l = line5_l .. "---  "
				end
				
				line5_s   = "    NM                KT"
			end
			-------------------------------------------------------
			
			if exec_light_in == 1 then
				if B738DR_hold_term == 0 or hold_offset_idx_act ~= offset then	--or rte12_act ~= use_rte12_sel then
					line6_l = "<ERASE                  "
				elseif B738DR_hold_term == 1 then
					-- HOLD active EXIT ARMED confirm
					line6_l = "<ERASE                  "
					line6_inv = "              EXIT*ARMED"
				else
					-- HOLD active EXIT ARMED
					line6_l = "<NEXT HOLD              "
					line6_m = "              EXIT ARMED"
				end
			else
				if hold_offset_idx_act == offset then
					-- HOLD active
					if B738DR_hold_term == 0 then
						line6_l = "<NEXT HOLD    EXIT HOLD>"
					elseif B738DR_hold_term == 1 then
						-- HOLD active EXIT ARMED confirm
						line6_l   = "<NEXT HOLD              "
						line6_inv = "              EXIT*ARMED"
					else
						-- HOLD active EXIT ARMED
						line6_l = "<NEXT HOLD              "
						line6_m = "              EXIT ARMED"
					end
				else
				-- HOLD inactive
					line6_l = "<NEXT HOLD              "
				end
			end
			
			line0_l = line0_l .. use_rte_header_str .. " HOLD        "
			line0_s = "                    " .. string.format("%1d",act_page_buf)
			line0_s = line0_s .. "/"
			line0_s = line0_s .. string.format("%1d",max_page_buf)
			-- line6_x = "------------------------"
		
		else
			act_page_buf = 1
			if in_mode == 0 then
				page_hold = 0
				page_legs = 1
			else
				page_hold2 = 0
				page_legs2 = 1
			end
			if use_rte12_sel == 0 then
				new_hold = 1
			else
				new_hold2 = 1
			end
		end
		
	end

end

function B738_fmc_init()

	if page_init == 1 then
		act_page_buf = 1
		max_page_buf = 1
		line0_l = "    INIT/REF INDEX      "
		line0_s = "                    1/1 "
		line1_l = "<IDENT         NAV DATA>"
		line2_l = "<POS         MSG RECALL>"
		line3_l = "<PERF         ALTN DEST>"
		line4_l = "<TAKEOFF                "
		line5_l = "<APPROACH               "
		line6_l = "<OFFSET      NAV STATUS>"
	end

end


function B738_fmc_takeoff()

	local thr_dspl_0 = ""
	local thr_dspl_1 = ""
	local thr_num = 0
	
	if B738DR_flight_phase == 0 then
		if page_takeoff == 1 then
			act_page_buf = 1
			max_page_buf = 2
			local gw_str = ""
			local rnw_len_temp = 0
			local rnw_len = ""
			
			if gw == "***.*" then
				gw_str = "---.-"
			else
				gw_str = gw
			end
			local trim_str = "TRIM"
			if trim == "    " then
				trim_str = "    "
			end
			line0_l = "      TAKEOFF REF       "
			line0_s = "                    1/2 "
			line1_x = " FLAPS          QRH   V1"
			line1_l = flaps .. "`                  "
			if qrh == "OFF" then
				line1_s = "                " .. v1
				if v1_set == "---" and v1 ~= "---" then
					line1_s = line1_s .. ">"
				end
				line2_s = "                " .. vr
				if vr_set == "---" and vr ~= "---" then
					line2_s = line2_s .. ">"
				end
				line3_s = "                " .. v2
				if v2_set == "---" and v2 ~= "---" then
					line3_s = line3_s .. ">"
				end
			else	-- QRH on
				line1_s = ""
				line2_s = ""
				line3_s = ""
			end
			if sel_temp == "----" then
				if to == "<ACT>" then
					line2_x = "    " .. engine_1k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_1 == "<ACT>" then
					line2_x = "    " .. engine_2k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_2 == "<ACT>" then
					line2_x = "    " .. engine_3k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				end
			else
				if to == "<ACT>" then
					line2_x = "RED " .. engine_1k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_1 == "<ACT>" then
					line2_x = "RED " .. engine_2k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_2 == "<ACT>" then
					line2_x = "RED " .. engine_3k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				end
			end
			line2_l = thr_dspl_0 .. "/"
			line2_l = line2_l .. thr_dspl_1
			
			line3_x = " CG    " .. trim_str
			line3_x = line3_x .. "           V2"
			line3_l = cg .. "%  "
			line3_l = line3_l .. trim
			
			if verify_to_spd == 0 then
				line1_l = line1_l .. v1_set
				line2_l = line2_l .. "          "
				line2_l = line2_l .. vr_set
				line3_l = line3_l .."          "
				line3_l = line3_l .. v2_set
			else
				if line1_s == "" then
					line1_s = "                     " .. v1_set
				else
					line1_s = line1_s .. " " .. v1_set
				end
				if line2_s == "" then
					line2_s = "                     " .. vr_set
				else
					line2_s = line2_s .. " " .. vr_set
				end
				if line3_s == "" then
					line3_s = "                     " .. v2_set
				else
					line3_s = line3_s .. " " .. v2_set
				end
			end
			
			line4_x = "               GW  / TOW"
			if v1_set ~= "---" and vr_set ~= "---" and v2_set ~= "---" then
				if tonumber(gw_str) == nil then
					line4_l = "              " .. gw_str
					line4_l = line4_l .. "/"
				else
					line4_l = "             " .. gw_str
					line4_l = line4_l .. "/"
					line4_l = line4_l .. gw_str
				end
			else
				line4_l = "              " .. gw_str
				line4_l = line4_l .. "/"
			end
			line4_s = "                        "
			
			if verify_to_spd == 0 then
				line5_x = " RUNWAY                 "
				if ref_icao == "----" or ref_rwy == "-----" then
					line5_l = "                        "
					line5_s = "                        "
				else
					line5_l = "RW" .. ref_rwy
					rnw_len_temp = (ref_runway_lenght * 3.280839995) + 0.5
					rnw_len_temp = math.floor(rnw_len_temp)
					if rnw_len_temp == 0 then
						rnw_len = "-----"
					elseif rnw_len_temp > 99999 then
						rnw_len = "99999"
					else
						rnw_len = string.format("%5d",rnw_len_temp)
					end
					line5_l = line5_l .. "/" .. rnw_len .. "F "
					if string.len(ref_rwy) == 2 then
						line5_s = "           T           "
					else
						line5_s = "            T          "
					end
					
					if ref_runway_lenght == 0 then
						rnw_len = "----"
					elseif ref_runway_lenght > 9999 then
						rnw_len = "9999"
					else
						rnw_len = string.format("%4d",ref_runway_lenght)
					end
					line5_l = line5_l .. rnw_len .. "M"
				end
			else
				line5_x = " RW" .. ref_rwy .. " TAKEOFF SPDS"
				line5_l = "<REJECT          ACCEPT>"
			end
			
			line6_x = "------------------SELECT"
			
			if pre_flt_pos_init == 0 then
				line6_l = "<POS INIT "
			elseif pre_flt_rte == 0 then
				line6_l = "<ROUTE    "
			elseif pre_flt_dep == 0 then
				line6_l = "<DEPARTURE"
			elseif pre_flt_perf_init == 0 then
				line6_l = "<PERF INIT"
			else
				line6_l = "<INDEX    "
			end
			
			line6_l = line6_l .. "      QRH " .. qrh
			
			line6_l = line6_l .. ">"
		
		elseif page_takeoff == 2 and B738DR_flight_phase == 0 then
			act_page = 2
			max_page = 2
			local rw_str_l = ""
			local rw_str_i = ""
			local rw_str_s = ""
			if rw_cond == 0 then
				rw_str_i = "DRY          "
				rw_str_s = "   /WET/SK-R>"
			elseif rw_cond == 1 then
				rw_str_i = "    WET      "
				rw_str_s = "DRY/   /SK-R>"
			elseif rw_cond == 2 then
				rw_str_i = "        SK-R "
				rw_str_s = "DRY/WET/    >"
			end
			line0_l = "      TAKEOFF REF       "
			line0_s = "                    2/2 "
			line1_x = " RW WIND        RW COND "
			line1_l = rw_wind_dir .. "`/"
			line1_l = line1_l .. rw_wind_spd .. "  "
			line1_g = "           " .. rw_str_i
			line1_s = "           " .. rw_str_s
			line2_x = " RW SLOPE/HDG    SEL/OAT"
			line2_l = rw_slope .. "%/"
			line2_l = line2_l .. rw_hdg
			
			local sel_temp_dspl = ""
			local oat_dspl = ""
			local oat_sim_dspl = ""
			if oat_unit == "`C" then
				sel_temp_dspl = sel_temp
				oat_dspl = oat
				local tat_temp = math.floor(simDR_TAT + 0.5)
				if tat_temp < 0 then
					oat_sim_dspl = string.format("%4d", tat_temp)	--oat_sim
				else
					if tat_temp < 10 then
						oat_sim_dspl = "  +" .. string.sub(string.format("%4d", tat_temp), 4, 4)
					else
						oat_sim_dspl = " +" .. string.sub(string.format("%4d", tat_temp), 3, 4)
					end
				end
				
			else
				sel_temp_dspl = sel_temp_f
				oat_dspl = oat_f
				oat_sim_dspl = (simDR_TAT * 9 / 5) + 32
				oat_sim_dspl = string.format("%4d", oat_sim_dspl)
			end
			
			line2_l = line2_l .. "    " .. sel_temp_dspl .. "/"
			if B738DR_aspirated_probe == 0 then
				line2_l = line2_l .. oat_dspl
			else
				if oat_dspl == "****" then
					line2_l = line2_l .. oat_sim_dspl
				else
					line2_l = line2_l .. oat_dspl
				end
			end
			line2_s = "                      " .. oat_unit
			
			if B738DR_thr_cutback == 0 then
				line4_x = " ACCEL HT    EO ACCEL HT"
				if accel_alt == "----" then
					line4_s = string.format("%4d", accel_alt_num) .. "AGL          "
					line4_l = "                 "
				else
					line4_l = accel_alt .. "             "
					line4_s = "    AGL          "
				end
				if eo_accel_alt == "----" then
					line4_s = line4_s .. string.format("%4d", eo_accel_alt_num) .. "AGL"
				else
					line4_l = line4_l .. eo_accel_alt
					line4_s = line4_s .. "    AGL"
				end
			else
				line4_x = "             EO ACCEL HT"
				line4_l = "                 "
				line4_s = "                 "
				if eo_accel_alt == "----" then
					line4_s = line4_s .. string.format("%4d", eo_accel_alt_num) .. "AGL"
				else
					line4_l = line4_l .. eo_accel_alt
					line4_s = line4_s .. "    AGL"
				end
			end
			
			if B738DR_thr_cutback == 0 then
				line5_x = "           THR REDUCTION"
				line5_l = "           "
				if clb == "<SEL>" then
					line5_l = line5_l .. "CLB   "
				elseif clb_1 == "<SEL>" then
					line5_l = line5_l .. "CLB-1 "
				elseif clb_2 == "<SEL>" then
					line5_l = line5_l .. "CLB-2 "
				end
				if clb_alt == "----" then
					line5_s = "                 " .. string.format("%4d", clb_alt_num) .. "AGL"
				else
					line5_l = line5_l .. clb_alt
					line5_s = "                     AGL"
				end
				line6_g = "                     OFF"
				line6_s = "                  ON    "
			else
				line3_x = "              CUTBACK N1"
				thr_num = B738DR_thr_cutback_N1[0] * 100
				if thr_num == 0 then
					thr_dspl_0 = "--.-"
				else
					thr_dspl_0 = string.format("%4.1f", thr_num)
				end
				thr_num = B738DR_thr_cutback_N1[1] * 100
				if thr_num == 0 then
					thr_dspl_1 = "--.-"
				else
					thr_dspl_1 = string.format("%4.1f", thr_num)
				end
				line3_l = "               " .. thr_dspl_0 .. "/" .. thr_dspl_1
				
				line5_x = " REDUCTION THR   RESTORE"
				if cut_alt == "----" then
					line5_l = "           "
					line5_s = string.format("%4d", cut_alt_num) .. "AGL          "
				else
					line5_l = cut_alt .. "       "
					line5_s = "    AGL          "
				end
				if clb == "<SEL>" then
					line5_l = line5_l .. "CLB   "
				elseif clb_1 == "<SEL>" then
					line5_l = line5_l .. "CLB-1 "
				elseif clb_2 == "<SEL>" then
					line5_l = line5_l .. "CLB-2 "
				end
				if clb_alt == "----" then
					line5_s = line5_s .. string.format("%4d", clb_alt_num) .. "AGL"
				else
					line5_l = line5_l .. clb_alt
					line5_s = line5_s .. "    AGL"
				end
				line6_g = "                  ON    "
				line6_s = "                     OFF"
			end
			
			line6_x = "-----------------CUTBACK"
			line6_l = "<INDEX              /   "
		end
	else
	
		if page_takeoff == 1 then
			act_page_buf = 1
			max_page_buf = 2
			line0_l = "      TAKEOFF REF       "
			line0_s = "                    1/2 "
			line1_x = " FLAPS                V1"
			if sel_temp == "----" then
				if to == "<ACT>" then
					line2_x = "    " .. engine_1k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_1 == "<ACT>" then
					line2_x = "    " .. engine_2k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_2 == "<ACT>" then
					line2_x = "    " .. engine_3k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				end
			else
				if to == "<ACT>" then
					line2_x = "RED " .. engine_1k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_1 == "<ACT>" then
					line2_x = "RED " .. engine_2k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				elseif to_2 == "<ACT>" then
					line2_x = "RED " .. engine_3k .. " N1            VR"
					thr_num = B738DR_thr_takeoff_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "---.-"
					else
						thr_dspl_0 = string.format("%5.1f", thr_num)
					end
					thr_num = B738DR_thr_takeoff_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "---.-"
					else
						thr_dspl_1 = string.format("%5.1f", thr_num)
					end
				end
			end
			line2_l = thr_dspl_0 .. "/" .. thr_dspl_1
			line3_x = "  CG                  V2"
			line4_x = "               GW  / TOW"
			line5_x = " RUNWAY                 "
			line6_x = "------------------------"
			line6_l = "<INDEX                  "
		elseif page_takeoff == 2 then
			act_page = 2
			max_page = 2
			line0_l = "      TAKEOFF REF       "
			line0_s = "                    2/2 "
			line2_x = "                 SEL/OAT"
			
			local sel_temp_dspl = ""
			local oat_dspl = ""
			local oat_sim_dspl = ""
			if oat_unit == "`C" then
				sel_temp_dspl = sel_temp
				oat_dspl = oat
				local tat_temp = math.floor(simDR_TAT + 0.5)
				if tat_temp < 0 then
					oat_sim_dspl = string.format("%4d", tat_temp)	--oat_sim
				else
					if tat_temp < 10 then
						oat_sim_dspl = "  +" .. string.sub(string.format("%4d", tat_temp), 4, 4)
					else
						oat_sim_dspl = " +" .. string.sub(string.format("%4d", tat_temp), 3, 4)
					end
				end
				
			else
				sel_temp_dspl = sel_temp_f
				oat_dspl = oat_f
				oat_sim_dspl = (simDR_TAT * 9 / 5) + 32
				oat_sim_dspl = string.format("%4d", oat_sim_dspl)
			end
			
			line2_l = "             " .. sel_temp_dspl .. "/"
			if B738DR_aspirated_probe == 0 then
				line2_l = line2_l .. oat_dspl
			else
				if oat_dspl == "****" then
					line2_l = line2_l .. oat_sim_dspl
				else
					line2_l = line2_l .. oat_dspl
				end
			end
			line2_s = "                      " .. oat_unit
			
			if B738DR_thr_cutback == 0 then
				line4_x = " ACCEL HT    EO ACCEL HT"
				if accel_alt == "----" then
					line4_s = string.format("%4d", accel_alt_num) .. "AGL          "
					line4_l = "                 "
				else
					line4_l = accel_alt .. "             "
					line4_s = "    AGL          "
				end
				if eo_accel_alt == "----" then
					line4_s = line4_s .. string.format("%4d", eo_accel_alt_num) .. "AGL"
				else
					line4_l = line4_l .. eo_accel_alt
					line4_s = line4_s .. "    AGL"
				end
			else
				line4_x = "             EO ACCEL HT"
				line4_l = "                 "
				line4_s = "                 "
				if eo_accel_alt == "----" then
					line4_s = line4_s .. string.format("%4d", eo_accel_alt_num) .. "AGL"
				else
					line4_l = line4_l .. eo_accel_alt
					line4_s = line4_s .. "    AGL"
				end
			end
			
			if B738DR_thr_cutback == 0 then
				line5_x = "           THR REDUCTION"
				line5_l = "           "
				if clb == "<SEL>" then
					line5_l = line5_l .. "CLB   "
				elseif clb_1 == "<SEL>" then
					line5_l = line5_l .. "CLB-1 "
				elseif clb_2 == "<SEL>" then
					line5_l = line5_l .. "CLB-2 "
				end
				if clb_alt == "----" then
					line5_s = "                 " .. string.format("%4d", clb_alt_num) .. "AGL"
				else
					line5_l = line5_l .. clb_alt
					line5_s = "                     AGL"
				end
				line6_g = "                     OFF"
				line6_s = "                  ON    "
			else
				if simDR_radio_height_pilot_ft <= clb_alt_num then
					line3_x = "              CUTBACK N1"
					thr_num = B738DR_thr_cutback_N1[0] * 100
					if thr_num == 0 then
						thr_dspl_0 = "--.-"
					else
						thr_dspl_0 = string.format("%4.1f", thr_num)
					end
					thr_num = B738DR_thr_cutback_N1[1] * 100
					if thr_num == 0 then
						thr_dspl_1 = "--.-"
					else
						thr_dspl_1 = string.format("%4.1f", thr_num)
					end
					line3_l = "               " .. thr_dspl_0 .. "/" .. thr_dspl_1
				end
				line5_x = " REDUCTION THR   RESTORE"
				if cut_alt == "----" then
					line5_l = "           "
					line5_s = string.format("%4d", cut_alt_num) .. "AGL          "
				else
					line5_l = cut_alt .. "       "
					line5_s = "    AGL          "
				end
				if clb == "<SEL>" then
					line5_l = line5_l .. "CLB   "
				elseif clb_1 == "<SEL>" then
					line5_l = line5_l .. "CLB-1 "
				elseif clb_2 == "<SEL>" then
					line5_l = line5_l .. "CLB-2 "
				end
				if clb_alt == "----" then
					line5_s = line5_s .. string.format("%4d", clb_alt_num) .. "AGL"
				else
					line5_l = line5_l .. clb_alt
					line5_s = line5_s .. "    AGL"
				end
			end
			line6_x = "------------------------"
			line6_l = "<INDEX                  "
		end
	end

end

function B738_fmc_approach()

	if page_approach == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local thr_dspl = ""
		local thr_num = 0
		local temp_ils = ""
		local temp_ils2 = ""
		local temp_ils5 = ""
		local temp_ils6 = 0
		local i_ils = 0
		local rw_ils = ""
		local app_ils = ""
		local rnw_len = ""
		local rnw_len_temp = 0
		
		local use_des_app = "------"
		local use_des_app2 = ""
		
		if rte12_act == 0 then
			use_des_app = des_app
			use_des_app2 = des_app2
		elseif rte12_act == 1 then
			use_des_app = des_app7
			use_des_app2 = des_app8
		end
		
		line0_l = "      APPROACH REF      "
		line0_s = "                    1/1 "
		line1_x = " GROSS WT   FLAPS   VREF"
		line1_l = gw_app
		line1_s = "                   "
		line1_l = line1_l .. "        15`   "
		if flaps_app == "15" then
			line1_l = (line1_l .. vref_15)
			line1_l = (line1_l .. "KT")
		else
			line1_l = (line1_l .. "     ")
			line1_s = line1_s .. vref_15
			line1_s = (line1_s .. "KT")
		end
		
		line2_l = "             30`   "
		if flaps_app == "30" then
			line2_l = line2_l .. vref_30
			line2_l = line2_l .. "KT"
			-- line2_s = "                        "
		else
			line2_l = (line2_l .. "     ")
			line2_s = ("                   " .. vref_30)
			line2_s = (line2_s .. "KT")
		end
		
		if use_des_app ~= "------" then
			rnw_len_temp = (des_runway_lenght * 3.280839995) + 0.5
			rnw_len_temp = math.floor(rnw_len_temp)
			if rnw_len_temp == 0 then
				rnw_len = "-----"
			elseif rnw_len_temp > 99999 then
				rnw_len = "99999"
			else
				rnw_len = string.format("%5d",rnw_len_temp)
			end
			line3_l = rnw_len .. "F "
			
			if des_runway_lenght == 0 then
				rnw_len = "----"
			elseif des_runway_lenght > 9999 then
				rnw_len = "9999"
			else
				rnw_len = string.format("%4d",des_runway_lenght)
			end
			line3_l = line3_l .. rnw_len .. "M 40`   "
			
		else
			line3_l = "             40`   "
		end
		if flaps_app == "40" then
			line3_l = (line3_l .. vref_40)
			line3_l = (line3_l .. "KT")
			if use_des_app ~= "------" then
				line3_s = "      T                 "
			end
		else
			line3_l = (line3_l .. "     ")
			if use_des_app ~= "------" then
				line3_s = ("      T            " .. vref_40)
			else
				line3_s = ("                   " .. vref_40)
			end
			line3_s = (line3_s .. "KT")
		end
		
		if use_des_app ~= "------" then
			rw_ils = string.sub(use_des_app, 2, -1)
			if string.len(rw_ils) > 3 then
				rw_ils = string.sub(rw_ils, 1, 3)
			end
			if string.len(rw_ils) > 2 then
				if string.sub(rw_ils, -1, -1) ~= "L" and string.sub(rw_ils, -1, -1) ~= "R" and string.sub(rw_ils, -1, -1) ~= "C" then
					rw_ils = string.sub(rw_ils, 1, 2) .. " "
				end
			else
				rw_ils = rw_ils .. " "
			end
			app_ils = string.sub(use_des_app, 2, -1)
			
			if ils_freq ~= 0 then
				temp_ils3 = string.format("%5d", ils_freq)
				temp_ils3 = string.sub(temp_ils3, 1, 3) .. "." .. string.sub(temp_ils3, 4, 5)
				temp_ils6 = displ_crs(ils_course)
				temp_ils4 = string.format("%03d", (temp_ils6)) .. "`"
			end
			
			local temp_lda3 = ""
			local temp_lda4 = ""
			local temp_lda6 = 0
			if lda_freq ~= 0 then
				temp_lda3 = string.format("%5d", lda_freq)
				temp_lda3 = string.sub(temp_lda3, 1, 3) .. "." .. string.sub(temp_lda3, 4, 5)
				temp_lda6 = displ_crs(lda_course)
				temp_lda4 = string.format("%03d", (temp_lda6)) .. "`"
			end
			
			local temp_gls3 = ""
			local temp_gls4 = ""
			local temp_gls6 = 0
			if gls_channel ~= 0 then
				temp_gls3 = string.format("%5d", gls_channel)
				temp_gls6 = displ_crs(gls_course)
				temp_gls4 = string.format("%03d", (temp_gls6)) .. "`"
			end
			
			if rte12_act == 0 then
				if arr_data == 0 then
					line3_x = " " .. des_icao .. des_rnw
				else
					line3_x = " " .. ref_icao .. des_rnw
				end
			elseif rte12_act == 1 then
				if arr_data7 == 0 then
					line3_x = " " .. des_icao7 .. des_rnw7
				else
					line3_x = " " .. ref_icao7 .. des_rnw7
				end
			end
			
			line4_s = "                  "
			if string.sub(use_des_app, 1, 1) == "I" then 	-- ILS
				temp_ils = " ILS " .. app_ils .. "/CRS"
				if string.len(app_ils) == 4 then
					temp_ils = temp_ils .. "   "
				elseif string.len(app_ils) == 2 then
					temp_ils = temp_ils .. "     "
				else
					temp_ils = temp_ils .. "    "
				end
				line4_s = "      " .. ils_id
				if ils_freq ~= 0 then
					line4_l = temp_ils3 .. "    /" .. temp_ils4 .. "   "
				else
					line4_l = "                  "
				end
			elseif string.sub(use_des_app, 1, 1) == "L" then 	-- LOC
				temp_ils = " LOC " .. app_ils .. "/CRS"
				if string.len(app_ils) == 4 then
					temp_ils = temp_ils .. "   "
				elseif string.len(app_ils) == 2 then
					temp_ils = temp_ils .. "     "
				else
					temp_ils = temp_ils .. "    "
				end
				line4_s = "      " .. ils_id
				if ils_freq ~= 0 then
					line4_l = temp_ils3 .. "    /" .. temp_ils4 .. "   "
				else
					line4_l = "                  "
				end
			elseif string.sub(use_des_app, 1, 1) == "X" then 	-- LDA
				temp_ils = " LDA " .. app_ils .. "/CRS"
				if string.len(app_ils) == 4 then
					temp_ils = temp_ils .. "   "
				elseif string.len(app_ils) == 2 then
					temp_ils = temp_ils .. "     "
				else
					temp_ils = temp_ils .. "    "
				end
				line4_s = "      " .. lda_id
				if lda_freq ~= 0 then
					line4_l = temp_lda3 .. "    /" .. temp_lda4 .. "   "
				else
					line4_l = "                  "
				end
			elseif string.sub(use_des_app, 1, 1) == "J" then 	-- GLS
				temp_ils = " GLS " .. app_ils .. "/CRS"
				if string.len(app_ils) == 4 then
					temp_ils = temp_ils .. "   "
				elseif string.len(app_ils) == 2 then
					temp_ils = temp_ils .. "     "
				else
					temp_ils = temp_ils .. "    "
				end
				line4_s = "      " .. gls_id
				
				if gls_channel ~= 0 then
					line4_l = temp_gls3 .. "     /" .. temp_gls4 .. "   "
				else
					line4_l = "                  "
				end
			elseif string.sub(use_des_app, 1, 1) == "R" and gls_channel ~= 0 then
				temp_ils = " RNAV" .. app_ils .. "/CRS"
				if string.len(app_ils) == 4 then
					temp_ils = temp_ils .. "   "
				elseif string.len(app_ils) == 2 then
					temp_ils = temp_ils .. "     "
				else
					temp_ils = temp_ils .. "    "
				end
				line4_s = "     " .. gls_id
				
				if gls_channel ~= 0 then
					line4_l = temp_gls3 .. "     /" .. temp_gls4 .. "   "
				else
					line4_l = "                  "
				end
			else
				temp_ils = "                "
				line4_l = "                  "
			end
			if B738DR_ian_not_install ~= 0 then
				line5_x = "               WIND CORR"
					line5_l = "       "
					line5_g = "       "
					line5_s = "       "
			elseif string.sub(use_des_app, 1, 1) ~= "R" and string.sub(use_des_app, 1, 1) ~= "D" and string.sub(use_des_app, 1, 1) ~= "S" 
			and string.sub(use_des_app, 1, 1) ~= "N" and string.sub(use_des_app, 1, 1) ~= "Q" and string.sub(use_des_app, 1, 1) ~= "V" then
				line5_x = " G/S           WIND CORR"
				if B738DR_fms_ils_disable == 0 then
					line5_l = "<  /   "
					line5_g = " ON    "
					line5_s = "    OFF"
				else
					line5_l = "<  /   "
					line5_g = "    OFF"
					line5_s = " ON    "
				end
			else
				line5_x = "               WIND CORR"
					line5_l = "       "
					line5_g = "       "
					line5_s = "       "
			end
		else
			line3_x = ""
			temp_ils = "                "
			line4_s = "                  "
			line5_x = "               WIND CORR"
			line5_l = "       "
			line5_s = "       "
			line4_l = "                  "
		end
		
		line4_x = temp_ils .. "FLAP/SPD"
		
		
		line4_l = line4_l .. app_flap
		line4_l = line4_l .. "/"
		line4_l = line4_l .. app_spd
		
		if wind_corr == "--" then
			line5_l = line5_l .. ""
			line5_s = line5_s .. "            +05KT"
		else
			line5_l = line5_l .. "            +"
			line5_l = line5_l .. wind_corr
			line5_s = line5_s .. "               KT"
		end
		line6_x = "------------------------"
		line6_l = "<INDEX        ALTN DEST>"
	end

end

function B738_fmc_perf()

	if page_perf == 1 then
		act_page_buf = 1
		max_page_buf = 2
		clr_repeat = 1
		local trip = "     "
		if B738DR_cruise_opt_alt > 0 then
			trip = "FL" .. string.format("%03d", (B738DR_cruise_opt_alt/100))
		end
		
		if perf_exec == 3 or crz_exec ~= 0 then
			line0_l = "      "
			line0_inv = "  MOD "
		elseif perf_exec == 2 then
			line0_l = "  ACT "
		else
			line0_l = "      "
		end
		
		line0_l = line0_l .. "PERF INIT          "
		line0_s = "                    1/2 "
		line1_x = " GW/CRZ CG  TRIP/CRZ ALT"
		
		local crz_alt_txt = crz_alt
		if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and crz_alt_num2 == 0 then
			if crz_alt_num >= B738DR_trans_alt_mod then
				crz_alt_txt = "FL" .. string.format("%03d", crz_alt_num / 100)
			else
				crz_alt_txt = string.format("%5d", crz_alt_num)
			end
		end
		
		local gw_mod_dspl = gw_mod
		
		if B738DR_gw_entry ~= 0 then
			if zfw_mod == "***.*" then
				gw_mod_dspl = "     "
			end
		end
		
		if cg == "--.-" then
			-- default CG
			line1_l = gw_mod_dspl .. "/ 8.0%"
		else
			line1_l = gw_mod_dspl .. "/"
			line1_l = line1_l .. cg
			line1_l = line1_l .. "%"
		end
		if gw_mod == "***.*" then
			line1_l = line1_l .."        "
			line1_l = line1_l .. crz_alt_txt
		else
			line1_l = line1_l .."  "
			line1_l = line1_l .. trip
			line1_l = line1_l .. "/"
			line1_l = line1_l .. crz_alt_txt
		end
		
		if B738DR_plan_fuel == 0 then
			line2_x = " FUEL           CRZ WIND"
			line2_l = fuel_weight .. "      "
		else
			line2_x = " PLAN/FUEL      CRZ WIND"
			if simDR_eng_N2[0] > 50 or simDR_eng_N2[1] > 50 or was_on_air ~= 0 then
				line2_l = "     /"
			else
				line2_l = plan_weight_mod .. "/"
			end
			line2_l = line2_l .. fuel_weight
		end
		
		line2_l = line2_l .. "      "
		line2_l = line2_l .. crz_wind_dir_mod
		line2_l = line2_l .. "`/"
		line2_l = line2_l .. crz_wind_spd_mod
		line3_x = " ZFW             "
		line3_l = zfw_mod .. "       "
		if crz_alt == "*****" then
			isa_dev_f = "----"
			isa_dev_c = "---"
			isa_dev_f_mod = "----"
			isa_dev_c_mod = "---"
			disable_PERF_3R = 1
		else
			line3_x = line3_x .. "ISA DEV"
			line3_l = line3_l .. isa_dev_f_mod
			line3_l = line3_l .. "   "
			line3_l = line3_l .. isa_dev_c_mod
			line3_s =   "                `F    `C"
			disable_PERF_3R = 0
		end
		line4_x = " RESERVES        "
		line4_l = reserves_mod .. "        "
		if crz_alt == "*****" then
			tc_oat_f = "----"
			tc_oat_c = "---"
			tc_oat_f_mod = "----"
			tc_oat_c_mod = "---"
			disable_PERF_4R = 1
		else
			line4_x = line4_x .. "T/C OAT"
			line4_l = line4_l .. tc_oat_f_mod
			line4_l = line4_l .. "   "
			line4_l = line4_l .. tc_oat_c_mod
			line4_s = "                `F    `C"
			disable_PERF_4R = 0
		end
		line5_x = " COST INDEX    TRANS ALT"
		line5_l = cost_index_mod .. "                "
		if trans_alt_mod == "-----" then
			line5_l = line5_l .. "18000"
		else
			line5_l = line5_l .. trans_alt_mod
		end
		line6_x = "------------------------"
		if perf_exec == 3 or crz_exec ~= 0 then
			line6_l = "<ERASE         N1 LIMIT>"
		else
			line6_l = "<INDEX         N1 LIMIT>"
		end
	elseif page_perf == 2 then
		act_page_buf = 2
		max_page_buf = 2
		line0_l = "      PERF LIMITS       "
		line0_s = "                    2/2 "
		line1_x = " TIME ERROR TOLERANCE   "
		line1_l = "  " .. time_err
		if time_err == "  " then
			line1_s = "  30 SEC AT RTA WPT     "
		else
			line1_s = "     SEC AT RTA WPT     "
		end
		line2_x = " MIN SPD --CLB-- MAX SPD"
		line2_l = clb_min_kts .. "/."
		line2_l = line2_l .. clb_min_mach
		line2_l = line2_l .. "        "
		line2_l = line2_l .. clb_max_kts
		line2_l = line2_l .. "/."
		line2_l = line2_l .. clb_max_mach
		if clb_min_kts == "   " then
			line2_s = "100  "
		else
			line2_s = "     "
		end
		if clb_min_mach == "   " then
			line2_s = line2_s .. "400        "
		else
			line2_s = line2_s .. "           "
		end
		if clb_max_kts == "   " then
			line2_s = line2_s .. "340  "
		else
			line2_s = line2_s .. "     "
		end
		if clb_max_mach == "   " then
			line2_s = line2_s .. "820"
		end
		line3_x = "         --CRZ--        "
		line3_l = crz_min_kts .. "/."
		line3_l = line3_l .. crz_min_mach
		line3_l = line3_l .. "        "
		line3_l = line3_l .. crz_max_kts
		line3_l = line3_l .. "/."
		line3_l = line3_l .. crz_max_mach
		if crz_min_kts == "   " then
			line3_s = "100  "
		else
			line3_s = "     "
		end
		if crz_min_mach == "   " then
			line3_s = line3_s .. "400        "
		else
			line3_s = line3_s .. "           "
		end
		if crz_max_kts == "   " then
			line3_s = line3_s .. "340  "
		else
			line3_s = line3_s .. "     "
		end
		if crz_max_mach == "   " then
			line3_s = line3_s .. "820"
		end
		line4_x = "         --DES--        "
		line4_l = des_min_kts .. "/."
		line4_l = line4_l .. des_min_mach
		line4_l = line4_l .. "        "
		line4_l = line4_l .. des_max_kts
		line4_l = line4_l .. "/."
		line4_l = line4_l .. des_max_mach
		if des_min_kts == "   " then
			line4_s = "100  "
		else
			line4_s = "     "
		end
		if des_min_mach == "   " then
			line4_s = line4_s .. "400        "
		else
			line4_s = line4_s .. "           "
		end
		if des_max_kts == "   " then
			line4_s = line4_s .. "340  "
		else
			line4_s = line4_s .. "     "
		end
		if des_max_mach == "   " then
			line4_s = line4_s .. "820"
		end
		line6_l = "<INDEX              RTA>"
	end

end

function B738_oat_n1_calc()
	local oat_dspl = ""
	local thr_num = 0
	if oat_unit == "`C" then
		if B738DR_aspirated_probe == 0 then
			oat_dspl = oat
		else
			if oat == "****" then
				local tat_temp = math.floor(simDR_TAT + 0.5)
				if tat_temp < 0 then
					oat_dspl = string.format("%4d", tat_temp)	--oat_sim
				else
					if tat_temp < 10 then
						oat_dspl = "  +" .. string.sub(string.format("%4d", tat_temp), 4, 4)
					else
						oat_dspl = " +" .. string.sub(string.format("%4d", tat_temp), 3, 4)
					end
				end
			else
				oat_dspl = oat
			end
		end
		if oat_old ~= oat_dspl then
			if is_timer_scheduled(oat_n1_calc_timer) == true then
				stop_timer(oat_n1_calc_timer)
			end
			run_after_time(oat_n1_calc_timer, 2)
			oat_n1_calc = 0
		end
		oat_old = oat_dspl
	else
		if B738DR_aspirated_probe == 0 then
			oat_dspl = oat_f
		else
			if oat_f == "****" then
				thr_num = (simDR_TAT * 9 / 5) + 32
				oat_dspl = string.format("%4d", thr_num)
			else
				oat_dspl = oat_f
			end
		end
		if oat_f_old ~= oat_dspl then
			if is_timer_scheduled(oat_n1_calc_timer) == true then
				stop_timer(oat_n1_calc_timer)
			end
			run_after_time(oat_n1_calc_timer, 2)
			oat_n1_calc = 0
		end
		oat_f_old = oat_dspl
	end
end

function oat_n1_calc_timer()
	oat_n1_calc = 1
end

function B738_fmc_n1_limit()

	if page_n1_limit == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local thr_dspl_0 = ""
		local thr_dspl_1 = ""
		local thr_num = 0
		
		if in_flight_mode == 0 then
			local sel_temp_dspl = ""
			local oat_dspl = ""
			local oat_sim_dspl = ""
			
			if oat_unit == "`C" then
				sel_temp_dspl = sel_temp
				oat_dspl = oat
				local tat_temp = math.floor(simDR_TAT + 0.5)
				if tat_temp < 0 then
					oat_sim_dspl = string.format("%4d", tat_temp)
				else
					if tat_temp < 10 then
						oat_sim_dspl = "  +" .. string.sub(string.format("%4d", tat_temp), 4, 4)
					else
						oat_sim_dspl = " +" .. string.sub(string.format("%4d", tat_temp), 3, 4)
					end
				end
				
			else
				sel_temp_dspl = sel_temp_f
				oat_dspl = oat_f
				thr_num = (simDR_TAT * 9 / 5) + 32
				oat_sim_dspl = string.format("%4d", thr_num)
				thr_num = 0
			end
			
			line0_l = "      N1 LIMIT          "
			line0_s = "                    1/1 "
			line1_x = " SEL/OAT      "
			if sel_temp == "----" then
				if to == "<ACT>" then
					if B738DR_aspirated_probe == 0 then
						if oat_dspl ~= "****" then
							line1_x = line1_x .. "    " .. engine_1k .. " N1"
						end
						if oat_dspl == "****" or oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					else
						line1_x = line1_x .. "    " .. engine_1k .. " N1"
						if oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					end
				elseif to_1 == "<ACT>" then
					if B738DR_aspirated_probe == 0 then
						if oat_dspl ~= "****" then
							line1_x = line1_x .. "    " .. engine_2k .. " N1"
						end
						if oat_dspl == "****" or oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					else
						line1_x = line1_x .. "    " .. engine_2k .. " N1"
						if oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					end
				elseif to_2 == "<ACT>" then
					if B738DR_aspirated_probe == 0 then
						if oat_dspl ~= "****" then
							line1_x = line1_x .. "    " .. engine_3k .. " N1"
						end
						if oat_dspl == "****" or oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					else
						line1_x = line1_x .. "    " .. engine_3k .. " N1"
						if oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					end
				end
			else
				if to == "<ACT>" then
					if B738DR_aspirated_probe == 0 then
						if oat_dspl ~= "****" then
							line1_x = line1_x .. "RED " .. engine_1k .. " N1"
						end
						if oat_dspl == "****" or oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					else
						line1_x = line1_x .. "RED " .. engine_1k .. " N1"
						if oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					end
				elseif to_1 == "<ACT>" then
					if B738DR_aspirated_probe == 0 then
						if oat_dspl ~= "****" then
							line1_x = line1_x .. "RED " .. engine_2k .. " N1"
						end
						if oat_dspl == "****" or oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					else
						line1_x = line1_x .. "RED " .. engine_2k .. " N1"
						if oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					end
				elseif to_2 == "<ACT>" then
					if B738DR_aspirated_probe == 0 then
						if oat_dspl ~= "****" then
							line1_x = line1_x .. "RED " .. engine_3k .. " N1"
						end
						if oat_dspl == "****" or oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					else
						line1_x = line1_x .. "RED " .. engine_3k .. " N1"
						if oat_n1_calc == 0 then
							thr_dspl_0 = ""
							thr_dspl_1 = ""
						else
							thr_num = B738DR_thr_takeoff_N1[0] * 100
							if thr_num == 0 then
								thr_dspl_0 = "---.-"
							else
								thr_dspl_0 = string.format("%5.1f", thr_num)
							end
							thr_num = B738DR_thr_takeoff_N1[1] * 100
							if thr_num == 0 then
								thr_dspl_1 = "---.-"
							else
								thr_dspl_1 = string.format("%5.1f", thr_num)
							end
						end
					end
				end
			end
			
			line1_l = sel_temp_dspl .. "/"
			if B738DR_aspirated_probe == 0 then
				line1_l = line1_l .. oat_dspl
			else
				if oat_dspl == "****" then
					line1_l = line1_l .. oat_sim_dspl
				else
					line1_l = line1_l .. oat_dspl
				end
			end
			line1_l = line1_l .."    "
			if thr_dspl_0 == "" then
				line1_l = line1_l .. "     "
			else
				line1_l = line1_l .. thr_dspl_0
			end
			if thr_dspl_0 ~= "" and thr_dspl_1 ~= "" then
				line1_l = line1_l .. "/"
			end
			if thr_dspl_1 == "" then
				line1_l = line1_l .. "     "
			else
				line1_l = line1_l .. thr_dspl_1
			end
			line1_s = "         " .. oat_unit
			line2_x = " " .. engine_1k
			line3_x = " " .. engine_2k .. " DERATE"
			line4_x = " " .. engine_3k .. " DERATE"
			
			if B738DR_aspirated_probe == 0 then
				if oat_dspl == "****" or oat_n1_calc == 0 then
					line2_l = "<TO   " .. "     "
					line2_l = line2_l .. " "
					line2_l = line2_l .. "     "
					line2_l = line2_l .. "   CLB>"
					line2_s = "                        "
					line3_l = "<TO-1 " .. "     "
					line3_l = line3_l .. " "
					line3_l = line3_l .. "     "
					line3_l = line3_l .. " CLB-1>"
					line3_s = "                        "
					line4_l = "<TO-2 " .. "     "
					line4_l = line4_l .." "
					line4_l = line4_l .. "     "
					line4_l = line4_l .. " CLB-2>"
				else
					line2_l = "<TO   " .. to
					line2_l = line2_l .. " "
					line2_l = line2_l .. clb
					line2_l = line2_l .. "   CLB>"
					line2_s = "                        "
					line3_l = "<TO-1 " .. to_1
					line3_l = line3_l .. " "
					line3_l = line3_l .. clb_1
					line3_l = line3_l .. " CLB-1>"
					line3_s = "                        "
					line4_l = "<TO-2 " .. to_2
					line4_l = line4_l .." "
					line4_l = line4_l .. clb_2
					line4_l = line4_l .. " CLB-2>"
				end
			else
				if oat_n1_calc == 0 then
					line2_l = "<TO   " .. "     "
					line2_l = line2_l .. " "
					line2_l = line2_l .. "     "
					line2_l = line2_l .. "   CLB>"
					line2_s = "                        "
					line3_l = "<TO-1 " .. "     "
					line3_l = line3_l .. " "
					line3_l = line3_l .. "     "
					line3_l = line3_l .. " CLB-1>"
					line3_s = "                        "
					line4_l = "<TO-2 " .. "     "
					line4_l = line4_l .." "
					line4_l = line4_l .. "     "
					line4_l = line4_l .. " CLB-2>"
				else
					line2_l = "<TO   " .. to
					line2_l = line2_l .. " "
					line2_l = line2_l .. clb
					line2_l = line2_l .. "   CLB>"
					line2_s = "                        "
					line3_l = "<TO-1 " .. to_1
					line3_l = line3_l .. " "
					line3_l = line3_l .. clb_1
					line3_l = line3_l .. " CLB-1>"
					line3_s = "                        "
					line4_l = "<TO-2 " .. to_2
					line4_l = line4_l .." "
					line4_l = line4_l .. clb_2
					line4_l = line4_l .. " CLB-2>"
				end
			end
			
			line6_x = "------------------------"
			if ground_air == 0 then
				line6_l = "<PERF INIT      TAKEOFF>"
				disable_N1_6R = 0
			else
				line6_l = "<PERF INIT"
				disable_N1_6R = 1
			end
			disable_N1_6L = 0
		
		elseif in_flight_mode == 1 then
			line0_l = "      N1 LIMIT          "
			line0_s = "                    1/1 "
			line1_l = "<AUTO " .. auto_act
			line1_s = "                        "
			line2_x = "                        "
			line2_l = "<GA   " .. ga_act
			line2_l = line2_l .. "  " 
			thr_num = fmc_ga_thrust * 100
			if thr_num == 0 then
				thr_dspl_0 = "---.-"
			else
				thr_dspl_0 = string.format("%5.1f", thr_num)
			end
			thr_num = fmc_ga_thrust2 * 100
			if thr_num == 0 then
				thr_dspl_1 = "---.-"
			else
				thr_dspl_1 = string.format("%5.1f", thr_num)
			end
			line2_l = line2_l .. thr_dspl_0
			line2_l = line2_l .. "/"
			line2_l = line2_l .. thr_dspl_1
			line3_l = "<CON  " .. con_act
			line3_l = line3_l .. "  " 
			thr_num = fmc_con_thrust * 100
			if thr_num == 0 then
				thr_dspl_0 = "---.-"
			else
				thr_dspl_0 = string.format("%5.1f", thr_num)
			end
			thr_num = fmc_con_thrust2 * 100
			if thr_num == 0 then
				thr_dspl_1 = "---.-"
			else
				thr_dspl_1 = string.format("%5.1f", thr_num)
			end
			line3_l = line3_l .. thr_dspl_0
			line3_l = line3_l .. "/"
			line3_l = line3_l .. thr_dspl_1
			line4_l = "<CLB  " .. clb_act
			line4_l = line4_l .. "  " 
			thr_num = fmc_clb_thrust * 100
			if thr_num == 0 then
				thr_dspl_0 = "---.-"
			else
				thr_dspl_0 = string.format("%5.1f", thr_num)
			end
			thr_num = fmc_clb_thrust2 * 100
			if thr_num == 0 then
				thr_dspl_1 = "---.-"
			else
				thr_dspl_1 = string.format("%5.1f", thr_num)
			end
			line4_l = line4_l .. thr_dspl_0
			line4_l = line4_l .. "/"
			line4_l = line4_l .. thr_dspl_1
			line5_l = "<CRZ  " .. crz_act
			line5_l = line5_l .. "  " 
			thr_num = fmc_crz_thrust * 100
			if thr_num == 0 then
				thr_dspl_0 = "---.-"
			else
				thr_dspl_0 = string.format("%5.1f", thr_num)
			end
			thr_num = fmc_crz_thrust2 * 100
			if thr_num == 0 then
				thr_dspl_1 = "---.-"
			else
				thr_dspl_1 = string.format("%5.1f", thr_num)
			end
			line5_l = line5_l .. thr_dspl_0
			line5_l = line5_l .. "/"
			line5_l = line5_l .. thr_dspl_1
			line6_x = "------REDUCED CLB-------"
			if clb == "<SEL>" then
				line6_l = "<CLB-1            CLB-2>"
			elseif clb_1 == "<SEL>" then
				line6_l = "<CLB-1 <SEL>      CLB-2>"
			elseif clb_2 == "<SEL>" then
				line6_l = "<CLB-1      <SEL> CLB-2>"
			end
			if simDR_altitude_pilot > 15000 then
				disable_N1_6L = 1
				disable_N1_6R = 1
			end
			if simDR_altitude_pilot < 14900 then
				disable_N1_6L = 0
				disable_N1_6R = 0
			end
		end
	end

end

function B738_fmc_climb()

	if page_climb == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local thrust_0 = 0
		local thrust_1 = 0
		local v11 = 0
		local v21 = 0
		local clb_100 = 0
		local time_TOD = 0
		local time_TOD_h = 0
		local time_TOD_m = 0
		local time_TOD_s = 0
		local time1_TOD_h = 0
		local time1_TOD_m = 0
		local time1_TOD_s = 0
		local delta_alt = 0
		local nav_id = ""
		local rest_alt_id = ""
		local rest_alt = ""
		local ii = 0
		local tmp_wpt_eta = ""
		local tmp_wpt_eta2 = 0
		local tmp_wpt_eta3 = 0
		
		local lo_hi = "---"
		
		local ap_altitude = 0
		if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
			ap_altitude = B738DR_preselected_alt
		else
			ap_altitude = simDR_altitude_pilot
		end
		
		if perf_exec == 0 then
			line0_l = "       120 CLB          "
			if B738DR_fmc_type == 0 then
				line0_s = "                    1/1 "
			else
				line0_s = "                    1/3 "
			end
			line1_x = " CRZ ALT"
			line1_l = crz_alt
			line2_x = " TGT SPD"
			line4_x = "------------      CLB N1"
			line5_x = "            ------------"
			line5_l = "<MAX RATE               "
			line6_l = "<MAX ANGLE          RTA>"
		else
			
			if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
				line0_l = line0_l .. "           CLB          "
				if B738DR_fmc_type == 0 then
					line0_s = "                    1/1 "
				else
					line0_s = "                    1/3 "
				end
				line1_x = " CRZ ALT"
				line1_l = "*****"
			else
				if perf_exec < 2 then
					line0_l = "      "
					lo_hi = ""
				elseif climb_exec == 1 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
					line0_l = "      "
					line0_inv = "  MOD "
				elseif B738DR_flight_phase < 2 then
					line0_l = "  ACT "
				else
					line0_l = "      "
				end
				line0_m = "      "
				
				if climb_r_alt1_mod >= B738DR_trans_alt_mod then
					fmc_climb_r_alt1_mod = "FL" .. string.sub(string.format("%05d", climb_r_alt1_mod), 1, 3)
				else
					fmc_climb_r_alt1_mod = string.format("%5d", climb_r_alt1_mod)
				end
					
				if B738DR_fmc_climb_speed_l == 340 or B738DR_fmc_climb_speed_l == 0.82 
				or B738DR_fmc_climb_speed_l == 0 then
					if perf_exec < 2 then
						line0_l = line0_l .. " 120 CLB          "
					elseif (B738DR_altitude_mode == 8 or B738DR_altitude_mode == 11) and ap_altitude < B738DR_aerodrom_altitude + B738DR_accel_alt then
						line0_m = line0_m .. string.format("%3d", simDR_airspeed_dial)
						line0_l = line0_l .. "    CLB         "
					elseif (B738DR_altitude_mode == 8 or B738DR_altitude_mode == 11) and simDR_flaps_ratio ~= 0 then
						line0_m = line0_m .. string.format("%3d", simDR_airspeed_dial)
						line0_l = line0_l .. "    LIM CLB     "
					elseif fmc_climb_mode_mod == 0 then
						line0_l = line0_l .. "ECON CLB          "
					elseif fmc_climb_mode_mod == 1 then
						line0_l = line0_l .. "MAX RATE CLB      "
					elseif fmc_climb_mode_mod == 2 then
						line0_l = line0_l .. "MAX ANGLE CLB     "
					elseif fmc_climb_mode_mod == 3 then
						if simDR_airspeed_is_mach == 0 then
							line0_m = line0_m .. string.format("%3d", B738DR_fmc_climb_speed)
							line0_l = line0_l .. "    CLB         "
						else
							line0_m = line0_m .. "M."
							line0_m = line0_m .. string.format("%3d", (B738DR_fmc_climb_speed_mach * 1000))
							line0_l = line0_l .. "      CLB         "
						end
					end
					
					if climb_exec == 1 then
						if climb_r_speed1_mod == 0 or climb_r_alt1_mod == 0 then
							if B738DR_fmc_climb_r_speed1 == 0 or climb_r_alt1_mod == 0 then
								line3_l = "---/-----          " .. lo_hi
								line3_m = ""
							else
								line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
								line3_m = ""
							end
						else
							line3_l = string.format("%03d", climb_r_speed1_mod) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
							line3_m = ""
						end
						
						
					elseif B738DR_fmc_climb_speed_l == B738DR_fmc_climb_r_speed1 and ap_altitude < B738DR_fmc_climb_r_alt1 
					and B738DR_fmc_climb_speed_l ~= 0 then
						line3_l = "   /" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
						line3_m = string.format("%03d", B738DR_fmc_climb_r_speed1)
					else
						if B738DR_fmc_climb_speed_l == 0 then
							if B738DR_fmc_climb_r_speed1 == 0 or ap_altitude > B738DR_fmc_climb_r_alt1 then
								line3_l = "---/-----          " .. lo_hi
							elseif ap_altitude <= B738DR_fmc_climb_r_alt1 then
								line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
							end
						else
							if B738DR_fmc_climb_r_speed1 == 0 or ap_altitude > B738DR_fmc_climb_r_alt1 then
								line3_l = "---/-----          " .. lo_hi
							else
								line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
							end
						end
						line3_m = ""
					end
					if B738DR_flight_phase < 2 and B738DR_autopilot_vnav_status == 1 then
						if simDR_airspeed_is_mach == 0 then
							if clb_spd_exec ~= 0 then
								if fmc_climb_speed_mod ~= 0 then
									line2_m = string.format("%3d", fmc_climb_speed_mod)
								else
									line2_m = string.format("%3d", B738DR_fmc_climb_speed)
								end
								if fmc_climb_speed_mach_mod ~= 0 then
									line2_l = "   /" .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
								else
									line2_l = "   /" .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
								end
							elseif B738DR_ap_spd_interv_status == 0 then
								line2_m = string.format("%3d", B738DR_fmc_climb_speed)
								line2_l = "   /" .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							else
								line2_m = string.format("%3d", B738DR_mcp_speed_dial)	--simDR_airspeed_dial)
								line2_l = "   /MCP "
							end
						else
							if clb_spd_exec ~= 0 then
								if fmc_climb_speed_mod ~= 0 then
									line2_l = string.format("%3d", fmc_climb_speed_mod) .. "/    "
								else
									line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/    "
								end
								if fmc_climb_speed_mach_mod ~= 0 then
									line2_m = "    " .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
								else
									line2_m = "    " .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
								end
							elseif B738DR_ap_spd_interv_status == 0 then
								line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/    "
								line2_m = "    " .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							else
								line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)	--simDR_airspeed_dial), 2, 5)
								line2_l = "    /MCP"
							end
						end
					else
						if clb_spd_exec ~= 0 then
							if fmc_climb_speed_mod ~= 0 then
								line2_l = string.format("%3d", fmc_climb_speed_mod) .. "/"
							else
								line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/"
							end
							if fmc_climb_speed_mach_mod ~= 0 then
								line2_l = line2_l .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
							else
								line2_l = line2_l .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							end
						else
							line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/"
							line2_l = line2_l .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
						end
						line2_m = ""
					end
				else
					if perf_exec < 2 then
						line0_l = line0_l .. " 120 CLB          "
					elseif B738DR_altitude_mode == 8 or B738DR_altitude_mode == 11 then
						line0_m = line0_m .. string.format("%3d", simDR_airspeed_dial)
						line0_l = line0_l .. "    CLB         "
					elseif B738DR_vnav_speed_trg == B738DR_fmc_vnav_v2_20 then
						line0_m = line0_m .. string.format("%3d", B738DR_fmc_vnav_v2_20)
						line0_l = line0_l .. "    CLB         "
					elseif simDR_flaps_ratio ~= 0 then
						line0_m = line0_m .. string.format("%3d", B738DR_fmc_climb_speed_l)
						line0_l = line0_l .. "    LIM CLB     "
					elseif fmc_climb_mode_mod == 0 then
						line0_l = line0_l .. "ECON CLB          "
					elseif fmc_climb_mode_mod == 1 then
						line0_l = line0_l .. "MAX RATE CLB      "
					elseif fmc_climb_mode_mod == 2 then
						line0_l = line0_l .. "MAX ANGLE CLB     "
					elseif fmc_climb_mode_mod == 3 then
						if simDR_airspeed_is_mach == 0 then
							line0_m = line0_m .. string.format("%3d", B738DR_fmc_climb_speed)
							line0_l = line0_l .. "    CLB         "
						else
							line0_m = line0_m .. "M."
							line0_m = line0_m .. string.format("%3d", (B738DR_fmc_climb_speed_mach * 1000))
							line0_l = line0_l .. "      CLB         "
						end
					else
						line0_l = line0_l .. "ECON CLB          "
					end
					if climb_exec == 1 then
						if climb_r_speed1_mod == 0 or climb_r_alt1_mod == 0 then
							if B738DR_fmc_climb_r_speed1 == 0 or climb_r_alt1_mod == 0 then
								line3_l = "---/-----          " .. lo_hi
								line3_m = ""
							else
								line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
								line3_m = ""
							end
						else
							line3_l = string.format("%03d", climb_r_speed1_mod) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
							line3_m = ""
						end
					elseif flaps_speed == B738DR_fmc_climb_speed_l or flaps_speed < vnav_speed then
						if flaps_speed == B738DR_fmc_climb_speed_l then
							line3_m = string.format("%3d", B738DR_fmc_climb_speed_l)
							line3_l = "   /FLAPS          ---  "
						else
							line3_l = string.format("%3d", B738DR_fmc_climb_speed_l) .. "/FLAPS          " .. lo_hi
							line3_m = ""
						end
					elseif vnav_speed == B738DR_fmc_climb_speed_l then
						nav_id = B738DR_rest_wpt_spd_id
						if string.len(nav_id) > 5 then
							nav_id = string.sub(nav_id, 1, 5)
						end
						line3_m = string.format("%3d", B738DR_fmc_climb_speed_l)
						line3_l = "   /"
						line3_l = line3_l .. nav_id
						line3_l = line3_l .. "          " .. lo_hi
						clb_del_spd_idx = B738DR_rest_wpt_spd_idx
					elseif B738DR_fmc_climb_speed_l == B738DR_fmc_climb_r_speed1 and ap_altitude <= B738DR_fmc_climb_r_alt1 
					and B738DR_fmc_climb_speed_l ~= 0 then
						line3_l = "   /" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
						line3_m = string.format("%03d", B738DR_fmc_climb_r_speed1)
					else
						if B738DR_fmc_climb_r_speed1 == 0 or ap_altitude > B738DR_fmc_climb_r_alt1 then
							line3_l = "---/-----          " .. lo_hi
							line3_m = ""
						else
							line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "          " .. lo_hi
							line3_m = ""
						end
					end
					if clb_spd_exec ~= 0 then
						if fmc_climb_speed_mod ~= 0 then
							line2_l = string.format("%3d", fmc_climb_speed_mod) .. "/"
						else
							line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/"
						end
						if fmc_climb_speed_mach_mod ~= 0 then
							line2_l = line2_l .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
						else
							line2_l = line2_l .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
						end
					elseif B738DR_ap_spd_interv_status == 1 and B738DR_flight_phase < 2 then
						line2_m = string.format("%3d", B738DR_mcp_speed_dial)	--simDR_airspeed_dial)
						line2_l = "   /MCP "
					else
						line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/"
						line2_l = line2_l .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
						line2_m = ""
					end
				end
				if B738DR_fmc_type == 0 then
					line0_s = "                    1/1 "
				else
					line0_s = "                    1/3 "
				end
					
				if B738DR_flight_phase > 1 then
						rest_alt_id = ""
						rest_alt = ""
				else
					if B738DR_rest_wpt_alt == 0 or legs_delete == 1 then
						if crz_alt == "*****" then
							rest_alt_id = ""
							rest_alt = ""
						else
							rest_alt_id = "T/C"
							rest_alt = crz_alt
						end
					else
						if string.len(B738DR_rest_wpt_alt_id) > 5 then
							rest_alt_id = string.sub(B738DR_rest_wpt_alt_id, 1, 5)
						else
							rest_alt_id = B738DR_rest_wpt_alt_id
						end
						
						if B738DR_rest_wpt_alt > B738DR_trans_alt_mod then
							rest_alt = "FL" .. string.format("%03d", B738DR_rest_wpt_alt/100)
						else
							rest_alt = string.format("%5d",B738DR_rest_wpt_alt)
						end
						if B738DR_rest_wpt_alt_t == 43 then
							rest_alt = rest_alt .. "A"
						elseif B738DR_rest_wpt_alt_t == 45 then
							rest_alt = rest_alt .. "B"
						end
					end
				end
				line1_x = " CRZ ALT        AT " .. rest_alt_id
				line2_x = " TGT SPD        TO " .. rest_alt_id
				
				local crz_alt_txt = crz_alt
				if B738DR_flight_phase > 2 and B738DR_flight_phase < 8 then
					crz_alt_txt = "*****"
				end
				if B738DR_flight_phase < 2 and B738DR_autopilot_vnav_status == 1 then
					if rest_alt_id == "T/C" then
						if crz_alt_num2 == 0 then
							line1_m = crz_alt_txt
						else
							line1_inv = crz_alt_txt
						end
						line1_l = "                  " .. rest_alt
					else
						if crz_alt_num2 == 0 then
							line1_m = crz_alt_txt .. "             " .. rest_alt
						else
							line1_inv = crz_alt_txt
							line1_m = "                  " .. rest_alt
						end
						line1_l = ""
					end
				else
					line1_m = ""
					line1_l = crz_alt_txt .. "             " .. rest_alt
				end
				
				if B738DR_flight_phase < 2 then
					
					if B738DR_rest_wpt_alt == 0 then
						if time_tc == 0 or dist_tc <= 0 then
							line2_l = line2_l .. "   ----.- /---NM"
						else
							tmp_wpt_eta = time_to_string2(time_tc)
							line2_l = line2_l .. "   "
							line2_l = line2_l .. tmp_wpt_eta .. " /"
							line2_l = line2_l .. string.format("%3d", dist_tc)
							line2_l = line2_l .. "NM"
						end
					else
						if legs_num > 1 then
							if B738DR_rest_wpt_alt_idx == 0 then
								line2_l = line2_l .. "   ----.- /---NM"
							else
								if B738DR_rest_wpt_alt_idx == offset then
									time_TOD = B738DR_fmc_dist
								else
									time_TOD = B738DR_fmc_dist
									for ii = offset + 1, B738DR_rest_wpt_alt_idx do
										time_TOD = time_TOD + wpt_distance(ii)
									end
								end
								tmp_wpt_eta = time_to_string2(legs_data[B738DR_rest_wpt_alt_idx][13])
								line2_l = line2_l .. "   " 
								line2_l = line2_l .. tmp_wpt_eta
								line2_l = line2_l .. " /"
								line2_l = line2_l .. string.format("%3d", time_TOD)
								line2_l = line2_l .. "NM"
							end
							
						else
							line2_l = line2_l .. "   ----.- /---NM"
						end
					end
				else
					line2_l = line2_l .. "   ----.- /---NM"
					
				end
				
				line2_s = "                 Z      "
				
				if perf_exec < 2 then
					line3_x = " SPD REST               "
				else
					line3_x = " SPD REST      ERR -----"
					line3_s = "                      LO"
				end
				line4_x = "------------    "
				
				if B738DR_eng_out == 0 or B738DR_eng_out_disable == 1 then
					if perf_exec < 2 or fmc_climb_mode_mod == 0 then
						line4_l = "            "
					else
						line4_l = "<ECON       "
					end
					-- if  not prompt ENG OUT
					if engout_clb == 0 or engout_clb ~= 1 then
						if fmc_climb_mode_mod == 1 then
							line5_l = "                ENG OUT>"
						else
							line5_l = "<MAX RATE       ENG OUT>"
						end
						
						if climb_exec == 0 and clb_spd_exec == 0 and crz_exec == 0 then
							if fmc_climb_mode_mod == 2 then
								line6_l = "                    RTA>"
							else
								line6_l = "<MAX ANGLE          RTA>"
							end
						else
							if fmc_climb_mode_mod == 2 then
								line6_l = "                  ERASE>"
							else
								line6_l = "<MAX ANGLE        ERASE>"
							end
						end
					elseif engout_clb == 1 then
						line5_l = "<LT ENG OUT  RT ENG OUT>"
						if climb_exec ~= 0 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
							line6_l = "                  ERASE>"
						end
					end
					
				else
					line4_l = "<ALL ENG    "
					
					if engout_clb == 0 or engout_clb ~= 1 then
						if fmc_climb_mode_mod == 1 then
							line5_l = "                ENG OUT>"
						else
							line5_l = "<MAX RATE       ENG OUT>"
						end
						
						if climb_exec == 0 and clb_spd_exec == 0 and crz_exec == 0 then
							if fmc_climb_mode_mod == 2 then
								line6_l = "                    RTA>"
							else
								line6_l = "<MAX ANGLE          RTA>"
							end
						else
							if fmc_climb_mode_mod == 2 then
								line6_l = "                  ERASE>"
							else
								line6_l = "<MAX ANGLE        ERASE>"
							end
						end
					elseif engout_clb == 1 then
						line5_l = "<LT ENG OUT  RT ENG OUT>"
						if climb_exec ~= 0 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
							line6_l = "                  ERASE>"
						end
					end
					
					
				end
				
				if clb == "<SEL>" then
					line4_x = line4_x .. "CLB   N1"
					thrust_0 = B738DR_thr_climb_N1[0] * 100
					thrust_1 = B738DR_thr_climb_N1[1] * 100
				elseif clb_1 == "<SEL>" then
					line4_x = line4_x .. "CLB-1 N1"
					thrust_0 = B738DR_thr_climb_N1[0] * 97
					thrust_1 = B738DR_thr_climb_N1[1] * 97
				elseif clb_2 == "<SEL>" then
					line4_x = line4_x .. "CLB-2 N1"
					thrust_0 = B738DR_thr_climb_N1[0] * 94
					thrust_1 = B738DR_thr_climb_N1[1] * 94
				end
				if thrust_0 == 0 then
					line4_l = line4_l .. "---.-/"
				else
					line4_l = line4_l .. string.format("%5.1f", thrust_0) .. "/"
				end
				if thrust_1 == 0 then
					line4_l = line4_l .. "---.-%"
				else
					line4_l = line4_l .. string.format("%5.1f", thrust_1) .. "%"
				end
				
				line5_x = "            ------------"
			end
		end
	end

end

-- function B738_fmc_climb_rta()

	-- if page_climb_rta == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  ACT RTA CLB           "
		-- line0_s = "                        "
		-- line1_x = " CRZ ALT        AT MACEY"
		-- line1_l = "FL350              6000A"
		-- line1_s = "                        "
		-- line2_x = " TGT SPD      TIME ERROR"
		-- line2_l = "280/.720         ON TIME"
		-- line2_s = "                 Z      "
		-- line3_x = " SPD REST               "
		-- line3_l = "250/10000               "
		-- line3_s = "                        "
		-- -- if  not prompt ENG OUT
		-- line4_x = "------------            "
		-- line4_l = "<ECON                   "
		-- line4_s = "                        "
		-- line5_x = "            ------------"
		-- line5_l = "<MAX RATE       ENG OUT>"
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "<MAX ANGLE          RTA>"
		-- line6_s = "                        "
		-- -- if prompt ENG OUT
		-- -- line4_x = "                        "
		-- -- line4_l = "                        "
		-- -- line4_s = "                        "
		-- -- line5_x = "------------------------"
		-- -- line5_l = "<                      >"
		-- -- line5_s = " LT ENG OUT  RT ENG OUT "
		-- -- line6_x = "                        "
		-- -- line6_l = "                        "
		-- -- line6_s = "                        "
	-- end

-- end

-- function B738_fmc_climb_eng_out()

	-- if page_climb_eng_out == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  ENG OUT CLB           "
		-- line0_s = "                        "
		-- line1_x = " CRZ ALT         MAX ALT"
		-- line1_l = "FL350              FL185"
		-- line1_s = "                        "
		-- line2_x = " ENG OUT SPD      CON N1"
		-- line2_l = "210KT              92.6%"
		-- line2_s = "                        "
		-- line3_x = "                        "
		-- line3_l = "                        "
		-- line3_s = "                        "
		-- line4_x = "------------------------"
		-- -- Left engine out
		-- line4_l = "<LT ENG OUT            >"
		-- line4_s = "             RT ENG OUT "
		-- -- Right engine out
		-- -- line4_l = "<            RT ENG OUT>"
		-- -- line4_s = " LT ENG OUT             "
		-- -- fix
		-- line5_x = "                        "
		-- line5_l = "                        "
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "                        "
		-- line6_s = "                        "
	-- end

-- end

function B738_fmc_cruise(in_mode)
	
	local exec_light_in = B738DR_fmc_exec_lights
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
	end
	
	if page_cruise == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local thrust = 0
		local trip = "     "
		local max_alt = "     "
		local time_TOD = 0
		local time_TOD_h = 0
		local time_TOD_m = 0
		local time_TOD_s = 0
		local delta_alt = 0
		local tmp_wpt_eta = ""
		local tmp_wpt_eta2 = 0
		local tmp_wpt_eta3 = 0
		local show_crz_clb_des = 0
		local prev_idx = 0
		
		local crz_alt_txt = ""
		
		if perf_exec == 0 then
			line0_l = "      ECON CRZ          "
			if B738DR_fmc_type == 0 then
				line0_s = "                    1/1 "
			else
				line0_s = "                    2/3 "
			end
			line1_x = " CRZ ALT  OPT/MAX       "
			line1_l = crz_alt	--"*****"
			line2_x = " TGT SPD"
			line3_x = " TURB N1     ACTUAL WIND"
			line4_x = " FUEL AT"
			line5_x = "------------------------"
			line6_l = "<LRC                RTA>"
		else
			
			if B738DR_cruise_opt_alt > 0 then
				trip = "FL" .. string.format("%03d", (B738DR_cruise_opt_alt/100))
			end
			if B738DR_cruise_max_alt > 0 then
				max_alt = "FL" .. string.format("%03d", (B738DR_cruise_max_alt/100))
			end
			
			local show_cruise_page = 0
			if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
				show_cruise_page = 1
			elseif crz_exec == 0 and B738DR_flight_phase == 2 then
				show_cruise_page = 1
			elseif crz_spd_exec ~= 0 and B738DR_flight_phase == 2 then
				show_cruise_page = 1
			elseif crz_exec ~= 0 and (B738DR_flight_phase < 2 or B738DR_flight_phase > 4) then
				show_cruise_page = 1
			end
			
			if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and crz_exec == 0 then
				-- descent mode without new crz alt
				if B738DR_fmc_type == 0 then
					line0_s = "                    1/1 "
				else
					line0_s = "                    2/3 "
				end
				line0_l = "      "
				if perf_exec < 2 then
					line0_l = line0_l .. "ECON CRZ          "
				else
					line0_l = line0_l .. "     CRZ          "
				end
				line1_x = " CRZ ALT                "
				line1_l = "*****"
			elseif show_cruise_page ~= 0 then
				if perf_exec < 2 then
					line0_l = "      "
				elseif crz_spd_exec ~= 0 or crz_exec ~= 0 then
					line0_l = "      "
					line0_inv = "  MOD "
				elseif B738DR_flight_phase == 2 then	-- phase cruise
					line0_l = "  ACT "
				else
					line0_l = "      "
				end
				
				local x_spd_mach_alt = find_mach_alt(B738DR_fmc_cruise_speed, B738DR_fmc_cruise_speed_mach)
				
				if perf_exec < 2 then
					line0_l = line0_l .. "ECON CRZ          "
				elseif fmc_cruise_mode_mod == 0 then
					line0_l = line0_l .. "ECON CRZ          "
				elseif fmc_cruise_mode_mod == 1 then
					line0_l = line0_l .. "LRC CRZ           "
				elseif fmc_cruise_mode_mod == 2 then
					if crz_spd_exec == 0 then
						if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
							if crz_alt_num > 0 then
								if crz_alt_num < x_spd_mach_alt then
									line0_l = line0_l .. string.format("%3d", B738DR_fmc_cruise_speed)
									line0_l = line0_l .. "KT CRZ         "
								else
									line0_l = line0_l .. "M."
									line0_l = line0_l .. string.format("%3d", (B738DR_fmc_cruise_speed_mach * 1000))
									line0_l = line0_l .. " CRZ         "
								end
							else
								line0_l = line0_l .. "M."
								line0_l = line0_l .. string.format("%3d", (B738DR_fmc_cruise_speed_mach * 1000))
								line0_l = line0_l .. " CRZ         "
							end
						elseif B738DR_fmc_cruise_speed ~= 0 then
							line0_l = line0_l .. string.format("%3d", B738DR_fmc_cruise_speed)
							line0_l = line0_l .. "KT CRZ         "
						elseif B738DR_fmc_cruise_speed_mach ~= 0 then
							line0_l = line0_l .. "M."
							line0_l = line0_l .. string.format("%3d", (B738DR_fmc_cruise_speed_mach * 1000))
							line0_l = line0_l .. " CRZ         "
						end
					else
						if fmc_cruise_speed_mod ~= 0 and fmc_cruise_speed_mach_mod ~= 0 then
							if crz_alt_num > 0 then
								if crz_alt_num < x_spd_mach_alt then
									line0_l = line0_l .. string.format("%3d", fmc_cruise_speed_mod)
									line0_l = line0_l .. "KT CRZ         "
								else
									line0_l = line0_l .. "M."
									line0_l = line0_l .. string.format("%3d", (fmc_cruise_speed_mach_mod * 1000))
									line0_l = line0_l .. " CRZ         "
								end
							else
								line0_l = line0_l .. "M."
								line0_l = line0_l .. string.format("%3d", (fmc_cruise_speed_mach_mod * 1000))
								line0_l = line0_l .. " CRZ         "
							end
						elseif fmc_cruise_speed_mod ~= 0 then
							line0_l = line0_l .. string.format("%3d", fmc_cruise_speed_mod)
							line0_l = line0_l .. "KT CRZ         "
						elseif fmc_cruise_speed_mach_mod ~= 0 then
							line0_l = line0_l .. "M."
							line0_l = line0_l .. string.format("%3d", (fmc_cruise_speed_mach_mod * 1000))
							line0_l = line0_l .. " CRZ         "
						end
					end
				end
				if B738DR_fmc_type == 0 then
					line0_s = "                    1/1 "
				else
					line0_s = "                    2/3 "
				end
				line1_x = " CRZ ALT  OPT/MAX       "
				
				if B738DR_flight_phase == 2 then
					if crz_alt_num2 == 0 then
						line1_m = crz_alt
					else
						line1_inv = crz_alt
					end
					line1_l = "        " .. trip
					line1_l = line1_l .. "/"
					line1_l = line1_l .. max_alt
				else
					line1_m = ""
					line1_l = crz_alt .. "   "
					line1_l = line1_l .. trip
					line1_l = line1_l .. "/"
					line1_l = line1_l .. max_alt
				end
				
				-- line1_s = "                        "
				line2_x = " TGT SPD          TO T/D"
				
				if B738DR_flight_phase == 2 then
					if simDR_airspeed_is_mach == 0 then
						if crz_spd_exec ~= 0 then
							if fmc_cruise_speed_mod ~= 0 and fmc_cruise_speed_mach_mod ~= 0 then
								if crz_alt_num > 0 then
									if crz_alt_num < x_spd_mach_alt then
										line2_inv = string.format("%3d", fmc_cruise_speed_mod) .. "     "
									else
										line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
									end
								else
									line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
								end
							elseif fmc_cruise_speed_mod ~= 0 then
								line2_inv = string.format("%3d", fmc_cruise_speed_mod) .. "     "
							elseif fmc_cruise_speed_mach_mod ~= 0 then
								line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
							end
							line2_l = "        "
						elseif B738DR_rest_wpt_spd_idx ~= 0 and B738DR_ap_spd_interv_status == 0 then
							if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
								if crz_alt_num > 0 then
									if crz_alt_num < x_spd_mach_alt then
										line2_l = string.format("%3d", B738DR_fmc_cruise_speed) .. "     "
									else
										line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
									end
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
								end
							elseif B738DR_fmc_cruise_speed ~= 0 then
								line2_l = string.format("%3d", B738DR_fmc_cruise_speed) .. "     "
							elseif B738DR_fmc_cruise_speed_mach ~= 0 then
								line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
							end
						elseif B738DR_ap_spd_interv_status == 0 then
							if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
								if crz_alt_num > 0 then
									line2_m = string.format("%3d", B738DR_fmc_cruise_speed)
									line2_l = "        "
								else
									line2_m = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5)
									line2_l = "        "
								end
							elseif B738DR_fmc_cruise_speed ~= 0 then
								line2_m = string.format("%3d", B738DR_fmc_cruise_speed)
								line2_l = "        "
							elseif B738DR_fmc_cruise_speed_mach ~= 0 then
								line2_m = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5)
								line2_l = "        "
							end
						else
							line2_m = string.format("%3d", B738DR_mcp_speed_dial)
							line2_l = "   /MCP "
						end
					else
						if crz_spd_exec ~= 0 then
							if fmc_cruise_speed_mod ~= 0 and fmc_cruise_speed_mach_mod ~= 0 then
								if crz_alt_num > 0 then
									if crz_alt_num < x_spd_mach_alt then
										line2_inv = string.format("%3d", fmc_cruise_speed_mod) .. "     "
									else
										line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
									end
								else
									line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
								end
							elseif fmc_cruise_speed_mod ~= 0 then
								line2_inv = string.format("%3d", fmc_cruise_speed_mod) .. "     "
							elseif fmc_cruise_speed_mach_mod ~= 0 then
								line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
							end
							line2_l = "        "
						elseif B738DR_rest_wpt_spd_idx ~= 0 and B738DR_ap_spd_interv_status == 0 then
							if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
								if crz_alt_num > 0 then
									if crz_alt_num < x_spd_mach_alt then
										line2_l = string.format("%3d", B738DR_fmc_cruise_speed) .. "     "
									else
										line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
									end
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
								end
							elseif B738DR_fmc_cruise_speed ~= 0 then
								line2_l = string.format("%3d", B738DR_fmc_cruise_speed) .. "     "
							elseif B738DR_fmc_cruise_speed_mach ~= 0 then
								line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
							end
						elseif B738DR_ap_spd_interv_status == 0 then
							if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
								if crz_alt_num > 0 then
									line2_m = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5)
									line2_l = "        "
								else
									line2_m = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5)
									line2_l = "        "
								end
							elseif B738DR_fmc_cruise_speed ~= 0 then
								line2_m = string.format("%3d", B738DR_fmc_cruise_speed)
								line2_l = "        "
							elseif B738DR_fmc_cruise_speed_mach ~= 0 then
								line2_m = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5)
								line2_l = "        "
							end
						else
							line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)
							line2_l = "    /MCP"
						end
					end
				else
					if crz_spd_exec ~= 0 then
						if fmc_cruise_speed_mod ~= 0 and fmc_cruise_speed_mach_mod ~= 0 then
							if crz_alt_num > 0 then
								if crz_alt_num < x_spd_mach_alt then
									line2_inv = string.format("%3d", fmc_cruise_speed_mod) .. "     "
								else
									line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
								end
							else
								line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
							end
						elseif fmc_cruise_speed_mod ~= 0 then
							line2_inv = string.format("%3d", fmc_cruise_speed_mod) .. "     "
						elseif fmc_cruise_speed_mach_mod ~= 0 then
							line2_inv = string.sub(string.format("%5.3f", fmc_cruise_speed_mach_mod), 2, 5) .. "    "
						end
						line2_l = "        "
					else
						if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
							if crz_alt_num > 0 then
								if crz_alt_num < x_spd_mach_alt then
									line2_l = string.format("%3d", B738DR_fmc_cruise_speed) .. "     "
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
								end
							else
								line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
							end
						elseif B738DR_fmc_cruise_speed ~= 0 then
							line2_l = string.format("%3d", B738DR_fmc_cruise_speed) .. "     "
						elseif B738DR_fmc_cruise_speed_mach ~= 0 then
							line2_l = string.sub(string.format("%5.3f", B738DR_fmc_cruise_speed_mach), 2, 5) .. "    "
						end
					end
					line2_m = ""
				end
				
				if B738DR_vnav_td_dist <= 0 then
					if td_idx == 0 then
						line2_l = line2_l .. " "
						line2_s = " "
					else
						if B738DR_vnav_td_dist > -50 then
							line2_l = line2_l .. "          /"
							line2_l = line2_l .. string.format("%3d", B738DR_vnav_td_dist)
							line2_l = line2_l .. "NM"
						else
							line2_l = line2_l .. " "
							line2_s = " "
						end
					end
				else
					line2_s = "                 Z      "
					if time_td == 0 then
						line2_l = line2_l .. " "
						line2_s = " "
					else
						tmp_wpt_eta = time_to_string2(time_td)
						if dist_td >= 999.5 then
							line2_s = "                Z      "
							line2_l = line2_l .. "  "
						else
							line2_s = "                 Z      "
							line2_l = line2_l .. "   "
						end
						line2_l = line2_l .. tmp_wpt_eta .. " /"
						line2_l = line2_l .. string.format("%3d", dist_td)
						line2_l = line2_l .. "NM"
					end
				end
				
				line3_x = " TURB N1     ACTUAL WIND"
				thrust = (B738DR_thr_cruise_N1[0] * 100) - 3.8
				if thrust == 0 then
					line3_l = "---.-/"
				else
					line3_l = string.format("%5.1f", thrust) .. "/"
				end
				thrust = (B738DR_thr_cruise_N1[1] * 100) - 3.8
				if thrust == 0 then
					line3_l = line3_l .. "---.-"
				else
					line3_l = line3_l .. string.format("%5.1f", thrust)
				end
				line3_l = line3_l .. "%    "
				
				if simDR_wind_spd > 0.5 then
					line3_l = line3_l .. string.format("%03d", simDR_wind_hdg)
					line3_l = line3_l .. "`/"
					line3_l = line3_l .. string.format("%03d", simDR_wind_spd)
				else
					line3_l = line3_l .. "---`/---"
				end

				-- DEST FUEL
				if des_icao ~= "****" then
					prev_idx = legs_num + 1
					line4_x = " FUEL AT " .. legs_data[prev_idx][1]
					-- fuel qty
					if exec_light_in == 0 then
						if legs_data[prev_idx][40] <= 0 then
							line4_l = line4_l .. "         --.-           "
						else
							if units == 0 then
								line4_l = line4_l .. "         " .. string.format("%4.1f", (legs_data[prev_idx][40] / 1000) * KGS_LBS)
							else
								line4_l = line4_l .. "         " .. string.format("%4.1f", (legs_data[prev_idx][40] / 1000))
							end
						end
					else
						line4_l = "         --.-           "
					end
				end
				
				line5_x = "------------------------"
				
				if engout_crz == 0 or engout_crz ~= 1 then
					if fmc_cruise_mode_mod == 0 then
						line5_l = "                ENG OUT>"
					else
						line5_l = "<ECON           ENG OUT>"
					end
					if crz_spd_exec == 0 and crz_exec == 0 then
						if fmc_cruise_mode_mod == 1 then
							line6_l = "                    RTA>"
						else
							line6_l = "<LRC                RTA>"
						end
					else
						if fmc_cruise_mode_mod == 1 then
							line6_l = "                  ERASE>"
						else
							line6_l = "<LRC              ERASE>"
						end
					end
				elseif engout_crz == 1 then
					line5_l = "<LT ENG OUT  RT ENG OUT>"
					if crz_spd_exec ~= 0 or crz_exec ~= 0 then
						line6_l = "                  ERASE>"
					end
				end
			elseif B738DR_flight_phase >= 2 then
				if crz_exec ~= 0 then
					if crz_alt_num2 > (simDR_altitude_pilot + 200) then
						show_crz_clb_des = 0	-- CRZ CLB
					else
						show_crz_clb_des = 1	-- CRZ DES
					end
				elseif B738DR_flight_phase == 3 then
					show_crz_clb_des = 0
				elseif B738DR_flight_phase == 4 then
					show_crz_clb_des = 1
				end
				
				-- CRZ CLB
				if show_crz_clb_des == 0 then
					if crz_exec == 0 then
						if B738DR_flight_phase == 3 and crz_exec == 0 then
							line0_l = "  ACT CRZ CLB           "
							line0_inv = ""
							line1_l = crz_alt .. "   "
							line1_inv = ""
							line6_l = "                    RTA>"
						else
							line0_l = "      CRZ CLB           "
							line0_inv = "  MOD"
							line1_l = "        "
							line1_inv = crz_alt
							line6_l = "                  ERASE>"
						end
						if B738DR_fmc_type == 0 then
							line0_s = "                    1/1 "
						else
							line0_s = "                    2/3 "
						end
						line1_x = " CRZ ALT  OPT/MAX       "
						line1_l = line1_l .. trip
						line1_l = line1_l .. "/"
						line1_l = line1_l .. max_alt
						line1_s = "                        "
						line2_x = " TGT SPD        TO " .. crz_alt
						
						if simDR_airspeed_is_mach == 0 then
							if crz_spd_exec ~= 0 then
								if fmc_climb_speed_mod ~= 0 then
									line2_m = string.format("%3d", fmc_climb_speed_mod)
								else
									line2_m = string.format("%3d", B738DR_fmc_climb_speed)
								end
								if fmc_climb_speed_mach_mod ~= 0 then
									line2_l = "   /" .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
								else
									line2_l = "   /" .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
								end
							elseif B738DR_ap_spd_interv_status == 0 then
								line2_m = string.format("%3d", B738DR_fmc_climb_speed)
								line2_l = "   /" .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							else
								line2_m = string.format("%3d", B738DR_mcp_speed_dial)
								line2_l = "   /MCP "
							end
						else
							if crz_spd_exec ~= 0 then
								if fmc_climb_speed_mod ~= 0 then
									line2_l = string.format("%3d", fmc_climb_speed_mod) .. "/    "
								else
									line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/    "
								end
								if fmc_climb_speed_mach_mod ~= 0 then
									line2_m = "    " .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
								else
									line2_m = "    " .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
								end
							elseif B738DR_ap_spd_interv_status == 0 then
								line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/    "
								line2_m = "    " .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							else
								line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)	--simDR_airspeed_dial), 2, 5)
								line2_l = "    /MCP"
							end
						end
						
						if simDR_vvi_fpm_pilot > 100 then
							delta_alt = crz_alt_num - simDR_altitude_pilot
							time_TOD = delta_alt / (simDR_vvi_fpm_pilot / 60)
							delta_alt = time_TOD
							time_TOD_h = math.floor((time_TOD / 3600))
							time_TOD = time_TOD - (time_TOD_h * 3600)
							time_TOD_m = math.floor((time_TOD / 60))
							time_TOD = time_TOD - (time_TOD_m * 60)
							time_TOD_s = math.floor(time_TOD)
							time_TOD_s = simDR_zulu_seconds + time_TOD_s
							if time_TOD_s > 59 then
								time_TOD_s = time_TOD_s - 60
								time_TOD_m = time_TOD_m + 1
							end
							time_TOD_m = time_TOD_m + simDR_zulu_minutes
							if time_TOD_m > 59 then
								time_TOD_m = time_TOD_m - 60
								time_TOD_h = time_TOD_h + 1
							end
							time_TOD_h = time_TOD_h + simDR_zulu_hours
							if time_TOD_h > 23 then
								time_TOD_h = time_TOD_h - 24
							end
							line2_l = line2_l .. "   "
							line2_l = line2_l .. string.format("%02d", time_TOD_h)
							line2_l = line2_l .. string.format("%02d", time_TOD_m)
							line2_l = line2_l .. "." 
							line2_l = line2_l .. string.sub(string.format("%02d", time_TOD_s), 1, 1)
							line2_l = line2_l .. " /"
							time_TOD = simDR_ground_spd * delta_alt * 0.00054
							line2_l = line2_l .. string.format("%3d", time_TOD)
							line2_l = line2_l .. "NM"
						else
							line2_l = line2_l .. "   ----.- /---NM"
						end
						line2_s = "                 Z      "
						
						line3_x = " SPD REST       EST WIND"
						
						if climb_r_speed1_mod == 0 or climb_r_alt1_mod == 0 then
							if B738DR_fmc_climb_r_speed1 == 0 or climb_r_alt1_mod == 0 then
								line3_l = "---/-----       "
							else
								line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "       "
							end
						else
							line3_l = string.format("%03d", climb_r_speed1_mod) .. "/" .. fmc_climb_r_alt1_mod .. "       "
						end
						
						if simDR_wind_spd > 0.5 then
							line3_l = line3_l .. string.format("%03d", simDR_wind_hdg)
							line3_l = line3_l .. "`/"
							line3_l = line3_l .. string.format("%03d", simDR_wind_spd)
						else
							line3_l = line3_l .. "---`/---"
						end
						line4_x = "------------            "
						line4_l = "<ECON                   "
						line5_l = "<MAX RATE       ENG OUT>"
					else
						-- mod crz climb
						line0_l = "      CRZ CLB           "
						line0_inv = "  MOD"
						if B738DR_fmc_type == 0 then
							line0_s = "                    1/1 "
						else
							line0_s = "                    2/3 "
						end
						line1_x = " CRZ ALT                "
						line1_l = crz_alt
						line2_x = " TGT SPD        TO " .. crz_alt
						
						if simDR_airspeed_is_mach == 0 then
							if crz_spd_exec ~= 0 then
								if fmc_climb_speed_mod ~= 0 then
									line2_m = string.format("%3d", fmc_climb_speed_mod)
								else
									line2_m = string.format("%3d", B738DR_fmc_climb_speed)
								end
								if fmc_climb_speed_mach_mod ~= 0 then
									line2_l = "   /" .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
								else
									line2_l = "   /" .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
								end
							elseif B738DR_ap_spd_interv_status == 0 then
								line2_m = string.format("%3d", B738DR_fmc_climb_speed)
								line2_l = "   /" .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							else
								line2_m = string.format("%3d", B738DR_mcp_speed_dial)
								line2_l = "   /MCP "
							end
						else
							if crz_spd_exec ~= 0 then
								if fmc_climb_speed_mod ~= 0 then
									line2_l = string.format("%3d", fmc_climb_speed_mod) .. "/    "
								else
									line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/    "
								end
								if fmc_climb_speed_mach_mod ~= 0 then
									line2_m = "    " .. string.sub(string.format("%5.3f", fmc_climb_speed_mach_mod), 2, 5)
								else
									line2_m = "    " .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
								end
							elseif B738DR_ap_spd_interv_status == 0 then
								line2_l = string.format("%3d", B738DR_fmc_climb_speed) .. "/    "
								line2_m = "    " .. string.sub(string.format("%5.3f", B738DR_fmc_climb_speed_mach), 2, 5)
							else
								line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)	--simDR_airspeed_dial), 2, 5)
								line2_l = "    /MCP"
							end
						end
						line2_l = line2_l .. "   "
						
						delta_alt = crz_alt_num2 - simDR_altitude_pilot
						time_TOD = delta_alt / 16.66666 	--- 1000 ft/min
						delta_alt = time_TOD
						time_TOD_h = math.floor((time_TOD / 3600))
						time_TOD = time_TOD - (time_TOD_h * 3600)
						time_TOD_m = math.floor((time_TOD / 60))
						time_TOD = time_TOD - (time_TOD_m * 60)
						time_TOD_s = math.floor(time_TOD)
						time_TOD_s = simDR_zulu_seconds + time_TOD_s
						if time_TOD_s > 59 then
							time_TOD_s = time_TOD_s - 60
							time_TOD_m = time_TOD_m + 1
						end
						time_TOD_m = time_TOD_m + simDR_zulu_minutes
						if time_TOD_m > 59 then
							time_TOD_m = time_TOD_m - 60
							time_TOD_h = time_TOD_h + 1
						end
						time_TOD_h = time_TOD_h + simDR_zulu_hours
						if time_TOD_h > 23 then
							time_TOD_h = time_TOD_h - 24
						end
						line2_l = line2_l .. string.format("%02d", time_TOD_h)
						line2_l = line2_l .. string.format("%02d", time_TOD_m)
						line2_l = line2_l .. "." 
						line2_l = line2_l .. string.sub(string.format("%02d", time_TOD_s), 1, 1)
						line2_l = line2_l .. " /"
						time_TOD = simDR_ground_spd * delta_alt * 0.00054
						line2_l = line2_l .. string.format("%3d", time_TOD)
						line2_l = line2_l .. "NM"
						line2_s = "                 Z      "
						
						line3_x = " SPD REST    ACTUAL WIND"
						
						if climb_r_speed1_mod == 0 or climb_r_alt1_mod == 0 then
							if B738DR_fmc_climb_r_speed1 == 0 or climb_r_alt1_mod == 0 then
								line3_l = "---/-----       "
							else
								line3_l = string.format("%03d", B738DR_fmc_climb_r_speed1) .. "/" .. fmc_climb_r_alt1_mod .. "       "
							end
						else
							line3_l = string.format("%03d", climb_r_speed1_mod) .. "/" .. fmc_climb_r_alt1_mod .. "       "
						end
						
						if simDR_wind_spd > 0.5 then
							line3_l = line3_l .. string.format("%03d", simDR_wind_hdg)
							line3_l = line3_l .. "`/"
							line3_l = line3_l .. string.format("%03d", simDR_wind_spd)
						else
							line3_l = line3_l .. "---`/---"
						end
						line5_l = "                        "
						line6_l = "<FORECAST         ERASE>"
					end
				
				-- CRZ DES
				elseif show_crz_clb_des == 1 then
					local ap_mcp_spd = 0
					local temp_speed = 0
					if crz_exec == 0 then
						if B738DR_flight_phase == 4 and crz_exec == 0 then
							line0_l = "  ACT CRZ DES           "
							line0_inv = ""
							line1_l = crz_alt .. "   "
							line1_inv = ""
							line6_l = "<FORECAST           RTA>"
						else
							line0_l = "      CRZ DES           "
							line0_inv = "  MOD"
							line1_l = "        "
							line1_inv = crz_alt
							line6_l = "<FORECAST         ERASE>"
						end
						if B738DR_fmc_type == 0 then
							line0_s = "                    1/1 "
						else
							line0_s = "                    2/3 "
						end
						line1_x = " CRZ ALT  OPT/MAX       "
						line1_l = line1_l .. trip
						line1_l = line1_l .. "/"
						line1_l = line1_l .. max_alt
						line2_x = " TGT SPD        TO " .. crz_alt
						
						if crz_spd_exec ~= 0 then
							if simDR_airspeed_is_mach == 0 then
								if fmc_descent_speed_mod ~= 0 then
									line2_m = "     " .. string.format("%3d", fmc_descent_speed_mod)
								else
									if ap_mcp_spd == temp_speed then
										line2_m = "     " .. string.format("%3d", B738DR_fmc_descent_speed)
									else
										line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
									end
								end
								if fmc_descent_speed_mach_mod ~= 0 then
									line2_l = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5) .. "/   "
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/   "
								end
							else
								if fmc_descent_speed_mod ~= 0 then
									line2_l = "    /" .. string.format("%3d", fmc_descent_speed_mod)
								else
									line2_l = "    /" .. string.format("%3d", B738DR_fmc_descent_speed)
								end
								if fmc_descent_speed_mach_mod ~= 0 then
									line2_m = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5)
								else
									line2_m = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5)
								end
							end
						elseif B738DR_ap_spd_interv_status == 0 then
							ap_mcp_spd = tonumber(string.format("%3d", simDR_airspeed_dial))
							temp_speed = tonumber(string.format("%3d", B738DR_fmc_descent_speed))
							if simDR_airspeed_is_mach == 0 then
								if ap_mcp_spd == temp_speed then
									line2_m = "     " .. string.format("%3d", B738DR_fmc_descent_speed)
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/   "
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
									line2_l = line2_l .. string.format("%3d", B738DR_fmc_descent_speed)
									line2_m = ""
								end
							else
								line2_l = "    /" .. string.format("%3d", B738DR_fmc_descent_speed)
								line2_m = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5)
							end
						else
							if simDR_airspeed_is_mach == 0 then
								line2_m = string.format("%3d", B738DR_mcp_speed_dial)	--simDR_airspeed_dial)
								line2_l = "   /MCP "
							else
								line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)	--simDR_airspeed_dial), 2, 5)
								line2_l = "    /MCP"
							end
						end
						
						
						
						if simDR_vvi_fpm_pilot < -100 then
							delta_alt = simDR_altitude_pilot - crz_alt_num
							time_TOD = delta_alt / 16.66666 	--- 1000 ft/min = simDR_vvi_fpm_pilot / 60
							delta_alt = time_TOD
							time_TOD_h = math.floor((time_TOD / 3600))
							time_TOD = time_TOD - (time_TOD_h * 3600)
							time_TOD_m = math.floor((time_TOD / 60))
							time_TOD = time_TOD - (time_TOD_m * 60)
							time_TOD_s = math.floor(time_TOD)
							time_TOD_s = simDR_zulu_seconds + time_TOD_s
							if time_TOD_s > 59 then
								time_TOD_s = time_TOD_s - 60
								time_TOD_m = time_TOD_m + 1
							end
							time_TOD_m = time_TOD_m + simDR_zulu_minutes
							if time_TOD_m > 59 then
								time_TOD_m = time_TOD_m - 60
								time_TOD_h = time_TOD_h + 1
							end
							time_TOD_h = time_TOD_h + simDR_zulu_hours
							if time_TOD_h > 23 then
								time_TOD_h = time_TOD_h - 24
							end
							line2_l = line2_l .. "   "
							line2_l = line2_l .. string.format("%02d", time_TOD_h)
							line2_l = line2_l .. string.format("%02d", time_TOD_m)
							line2_l = line2_l .. "." 
							line2_l = line2_l .. string.sub(string.format("%02d", time_TOD_s), 1, 1)
							line2_l = line2_l .. " /"
							time_TOD = simDR_ground_spd * delta_alt * 0.00054
							line2_l = line2_l .. string.format("%3d", time_TOD)
							line2_l = line2_l .. "NM"
						else
							line2_l = line2_l .. "   ----.- /---NM"
						end
						line2_s = "                 Z      "
						line3_x = " SPD REST       EST WIND"
						
						if descent_r_speed1_mod == 0 then
							line3_l = "---/-----       "
						else
							line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "       "
						end
						
						if simDR_wind_spd > 0.5 then
							line3_l = line3_l .. string.format("%03d", simDR_wind_hdg)
							line3_l = line3_l .. "`/"
							line3_l = line3_l .. string.format("%03d", simDR_wind_spd)
						else
							line3_l = line3_l .. "---`/---"
						end
						line4_x = "------------            "
						line4_l = "<ECON                   "
						line5_x = "            ------------"
						line5_l = "<MAX RATE       ENG OUT>"
					else
						-- mod crz descent
						line0_l = "      CRZ DES           "
						line0_inv = "  MOD"
						if B738DR_fmc_type == 0 then
							line0_s = "                    1/1 "
						else
							line0_s = "                    2/3 "
						end
						line1_x = " CRZ ALT                "
						line1_l = crz_alt
						line2_x = " TGT SPD        TO " .. crz_alt
						
						if crz_spd_exec ~= 0 then
							if simDR_airspeed_is_mach == 0 then
								if fmc_descent_speed_mod ~= 0 then
									line2_m = "     " .. string.format("%3d", fmc_descent_speed_mod)
								else
									if ap_mcp_spd == temp_speed then
										line2_m = "     " .. string.format("%3d", B738DR_fmc_descent_speed)
									else
										line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
									end
								end
								if fmc_descent_speed_mach_mod ~= 0 then
									line2_l = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5) .. "/   "
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/   "
								end
							else
								if fmc_descent_speed_mod ~= 0 then
									line2_l = "    /" .. string.format("%3d", fmc_descent_speed_mod)
								else
									line2_l = "    /" .. string.format("%3d", B738DR_fmc_descent_speed)
								end
								if fmc_descent_speed_mach_mod ~= 0 then
									line2_m = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5)
								else
									line2_m = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5)
								end
							end
						elseif B738DR_ap_spd_interv_status == 0 then
							ap_mcp_spd = tonumber(string.format("%3d", simDR_airspeed_dial))
							temp_speed = tonumber(string.format("%3d", B738DR_fmc_descent_speed))
							if simDR_airspeed_is_mach == 0 then
								if ap_mcp_spd == temp_speed then
									line2_m = "     " .. string.format("%3d", B738DR_fmc_descent_speed)
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/   "
								else
									line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
									line2_l = line2_l .. string.format("%3d", B738DR_fmc_descent_speed)
									line2_m = ""
								end
							else
								line2_l = "    /" .. string.format("%3d", B738DR_fmc_descent_speed)
								line2_m = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5)
							end
						else
							if simDR_airspeed_is_mach == 0 then
								line2_m = string.format("%3d", B738DR_mcp_speed_dial)
								line2_l = "   /MCP "
							else
								line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)
								line2_l = "    /MCP"
							end
						end
						line2_l = line2_l .. "   "
						
						delta_alt = simDR_altitude_pilot - crz_alt_num2
						time_TOD = delta_alt / 16.66666		-- 1000 fpm
						delta_alt = time_TOD
						time_TOD_h = math.floor((time_TOD / 3600))
						time_TOD = time_TOD - (time_TOD_h * 3600)
						time_TOD_m = math.floor((time_TOD / 60))
						time_TOD = time_TOD - (time_TOD_m * 60)
						time_TOD_s = math.floor(time_TOD)
						time_TOD_s = simDR_zulu_seconds + time_TOD_s
						if time_TOD_s > 59 then
							time_TOD_s = time_TOD_s - 60
							time_TOD_m = time_TOD_m + 1
						end
						time_TOD_m = time_TOD_m + simDR_zulu_minutes
						if time_TOD_m > 59 then
							time_TOD_m = time_TOD_m - 60
							time_TOD_h = time_TOD_h + 1
						end
						time_TOD_h = time_TOD_h + simDR_zulu_hours
						if time_TOD_h > 23 then
							time_TOD_h = time_TOD_h - 24
						end
						line2_l = line2_l .. string.format("%02d", time_TOD_h)
						line2_l = line2_l .. string.format("%02d", time_TOD_m)
						line2_l = line2_l .. "." 
						line2_l = line2_l .. string.sub(string.format("%02d", time_TOD_s), 1, 1)
						line2_l = line2_l .. " /"
						time_TOD = simDR_ground_spd * delta_alt * 0.00054
						line2_l = line2_l .. string.format("%3d", time_TOD)
						line2_l = line2_l .. "NM"
						line2_s = "                 Z      "
						
						line3_x = " SPD REST    ACTUAL WIND"
						
						if descent_r_speed1_mod == 0 then
							line3_l = "---/-----       "
						else
							line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "       "
						end
						
						if simDR_wind_spd > 0.5 then
							line3_l = line3_l .. string.format("%03d", simDR_wind_hdg)
							line3_l = line3_l .. "`/"
							line3_l = line3_l .. string.format("%03d", simDR_wind_spd)
						else
							line3_l = line3_l .. "---`/---"
						end
						line5_l = "            PLANNED DES>"
						line6_l = "<FORECAST         ERASE>"
					end
				end
			end
		end
	end

end

function fmc_engout_crz()
	-- B738DR_cont_spd				= find_dataref("laminar/B738/engine/calc/cont_spd")
	-- B738DR_cont_lvl				= find_dataref("laminar/B738/engine/calc/cont_lvl")
	
	local tmp_wpt_eta = ""
	
	line0_l = "      ENG OUT CRZ       "
	line0_s = "                    1/1 "
	line1_x = " CRZ ALT         MAX ALT"
	line1_m = crz_alt
	line1_l = "                   "
	if B738DR_cont_lvl > B738DR_trans_lvl_mod then
		line1_l = line1_l .. "FL" .. string.format("%03d", B738DR_cont_lvl / 100)
	else
		line1_l = line1_l .. string.format("%5d", B738DR_cont_lvl)
	end
	
	line2_x = " ENG OUT SPD"	--"
	if B738DR_ap_spd_interv_status == 0 then
		line2_l = string.format("%3d", B738DR_cont_spd) .. "     "
		line2_s = "   KT"
	else
		line2_s = "     "
		if simDR_airspeed_is_mach == 0 then
			line2_m = string.format("%3d", B738DR_mcp_speed_dial)
			line2_l = "   /MCP "
		else
			line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)
			line2_l = "    /MCP"
		end
	end
	
	if B738DR_vnav_td_dist > 0 then
		line2_x = line2_x .. "      TO T/D"
		line2_s = line2_s .. "            Z      "
		if time_td ~= 0 then
			tmp_wpt_eta = time_to_string2(time_td)
			if dist_td >= 999.5 then
				line2_s = line2_s .. "           Z      "
				line2_l = line2_l .. "  "
			else
				line2_s = line2_s .. "            Z      "
				line2_l = line2_l .. "   "
			end
			line2_l = line2_l .. tmp_wpt_eta .. " /"
			line2_l = line2_l .. string.format("%3d", dist_td)
			line2_l = line2_l .. "NM"
		end
	end
	
	line3_x = " CON N1                 "
	if B738DR_thr_cont_N1[0] == 0 then
		line3_l = "---.-"
	else
		line3_l = string.format("%5.1f", B738DR_thr_cont_N1[0] * 100) .. "%"
	end
	line4_x = "------------------------"
	if engout_crz == 2 then
		line4_l   = "             RT ENG OUT>"
		line4_inv = " LT*ENG*OUT"
	elseif engout_crz == 3 then
		line4_l   = "<LT ENG OUT             "
		line4_inv = "             RT*ENG*OUT "
	end
end

function fmc_engout_clb()
	line0_l = "      ENG OUT CLB       "
	line0_s = "                    1/1 "
	line1_x = " CRZ ALT         MAX ALT"
	line1_m = crz_alt
	line1_l = "                   "
	if B738DR_cont_lvl > B738DR_trans_lvl_mod then
		line1_l = line1_l .. "FL" .. string.format("%03d", B738DR_cont_lvl / 100)
	else
		line1_l = line1_l .. string.format("%5d", B738DR_cont_lvl)
	end
	line2_x = " ENG OUT SPD      CON N1"
	line2_l = string.format("%3d", B738DR_cont_spd) .. "KT             "
	if B738DR_thr_cont_N1[0] == 0 then
		line2_l = line2_l .. "---.-"
	else
		line2_l = line2_l .. string.format("%5.1f", B738DR_thr_cont_N1[0] * 100) .. "%"
	end
	line4_x = "------------------------"
	if engout_clb == 2 then
		line4_l   = "             RT ENG OUT>"
		line4_inv = " LT*ENG*OUT"
	elseif engout_clb == 3 then
		line4_l   = "<LT ENG OUT             "
		line4_inv = "             RT*ENG*OUT "
	end
end

-- page_cruise == 1
-- -- 6LSK NO Forecast
-- if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and crz_exec == 0 then
-- elseif B738DR_flight_phase < 2 or B738DR_flight_phase == 8 or (crz_exec == 0 and B738DR_flight_phase == 2) then

-- -- 2LSK Speed cruise
-- if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 or (crz_exec == 0 and B738DR_flight_phase == 2) then

-- -- 3LSK Restrict 250/10000
-- if (crz_exec ~= 0 and B738DR_flight_phase >= 2 and B738DR_flight_phase < 8) or B738DR_flight_phase == 3 or B738DR_flight_phase == 4 then



-- function B738_fmc_cruise_rta()

	-- if page_cruise_rta == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  ACT RTA CRZ           "
		-- line0_s = "                    1/1 "
		-- line1_x = " CRZ ALT  OPT/MAX       "
		-- line1_l = "FL350   FL340/361       "
		-- line1_s = "                        "
		-- line2_x = " TGT SPD          TO T/D"
		-- line2_l = ".780       2004.3 /100NM"
		-- line2_s = "                 Z      "
		-- line3_x = " TIME ERROR  ACTUAL WIND"
		-- line3_l = "EARLY 05:31     129`/ 14"
		-- line3_s = "                        "
		-- line4_x = " FUEL AT KATL           "
		-- line4_l = "         12.5           "
		-- line4_s = "                        "
		-- line5_x = "------------------------"
		-- line5_l = "<ECON           ENG OUT>"
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "<LRC                RTA>"
		-- line6_s = "                        "
	-- end

-- end

-- function B738_fmc_cruise_clb()

	-- if page_cruise_clb == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  MOD CRZ CLB           "
		-- line0_s = "                    1/1 "
		-- line1_x = " CRZ ALT                "
		-- line1_l = "FL370                   "
		-- line1_s = "                        "
		-- line2_x = " TGT SPD        TO FL370"
		-- line2_l = ".780       2004.5 / 15NM"
		-- line2_s = "                 Z      "
		-- line3_x = " SPD REST       EST WIND"
		-- line3_l = "---/-----       129`/ 14"
		-- line3_s = "                        "
		-- line4_x = "------------     SAVINGS"
		-- line4_l = "<ECON               1.3%"
		-- line4_s = "                        "
		-- line5_x = "            ------------"
		-- line5_l = "<MAX RATE       ENG OUT>"
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "<MAX ANGLE        ERASE>"
		-- line6_s = "                        "
	-- end

-- end

-- function B738_fmc_cruise_rta_clb()

	-- if page_cruise_rta_clb == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  ACT RTA CRZ CLB       "
		-- line0_s = "                    1/1 "
		-- line1_x = " CRZ ALT      TIME ERROR"
		-- line1_l = "FL370        EARLY 01:22"
		-- line1_s = "                        "
		-- line2_x = " TGT SPD        TO FL370"
		-- line2_l = ".780       2004.5 / 15NM"
		-- line2_s = "                 Z      "
		-- line3_x = " SPD REST       EST WIND"
		-- line3_l = "---/-----       129`/ 14"
		-- line3_s = "                        "
		-- line4_x = "------------            "
		-- line4_l = "<ECON                   "
		-- line4_s = "                        "
		-- line5_x = "            ------------"
		-- line5_l = "<MAX RATE       ENG OUT>"
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "<MAX ANGLE        ERASE>"
		-- line6_s = "                        "
	-- end

-- end

-- function B738_fmc_cruise_des()

	-- if page_cruise_des == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  MOD CRZ DES           "
		-- line0_s = "                    1/1 "
		-- line1_x = " CRZ ALT                "
		-- line1_l = "FL370                   "
		-- line1_s = "                        "
		-- line2_x = " TGT SPD        TO FL370"
		-- line2_l = ".780       2004.5 / 15NM"
		-- line2_s = "                 Z      "
		-- line3_x = " SPD REST       EST WIND"
		-- line3_l = "---/-----       129`/ 14"
		-- line3_s = "                        "
		-- line4_x = "                 PENALTY"
		-- line4_l = "                    1.3%"
		-- line4_s = "                        "
		-- line5_x = "------------------------"
		-- line5_l = "            PLANNED DES>"
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "<FORECAST         ERASE>"
		-- line6_s = "                        "
	-- end

-- end

-- function B738_fmc_cruise_rta_des()

	-- if page_cruise_rta_des == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  ACT RTA CRZ DES       "
		-- line0_s = "                    1/1 "
		-- line1_x = " CRZ ALT      TIME ERROR"
		-- line1_l = "FL280         LATE 10:54"
		-- line1_s = "                        "
		-- line2_x = " TGT SPD        TO FL280"
		-- line2_l = ".780       2004.5 / 15NM"
		-- line2_s = "                 Z      "
		-- line3_x = " SPD REST    ACTUAL WIND"
		-- line3_l = "250/10000       129`/ 14"
		-- line3_s = "                        "
		-- line4_x = "                        "
		-- line4_l = "                        "
		-- line4_s = "                        "
		-- line5_x = "------------------------"
		-- line5_l = "            PLANNED DES>"
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "<FORECAST           RTA>"
		-- line6_s = "                        "
	-- end

-- end

-- function B738_fmc_cruise_eng_out()

	-- if page_cruise_eng_out == 1 then
		-- act_page = 1
		-- max_page = 1
		-- line0_l = "  ENG OUT CRZ           "
		-- line0_s = "                        "
		-- line1_x = " CRZ ALT         MAX ALT"
		-- line1_l = "FL350              FL185"
		-- line1_s = "                        "
		-- line2_x = " ENG OUT SPD      CON N1"
		-- line2_l = "210KT              92.6%"
		-- line2_s = "                        "
		-- line3_x = " CON N1                 "
		-- line3_l = " 91.9%                  "
		-- line3_s = "                        "
		-- line4_x = "------------------------"
		-- -- Left engine out
		-- line4_l = "<LT ENG OUT            >"
		-- line4_s = "             RT ENG OUT "
		-- -- Right engine out
		-- -- line4_l = "<            RT ENG OUT>"
		-- -- line4_s = " LT ENG OUT             "
		-- -- fix
		-- line5_x = "                        "
		-- line5_l = "                        "
		-- line5_s = "                        "
		-- line6_x = "                        "
		-- line6_l = "                        "
		-- line6_s = "                        "
	-- end

-- end

--page_nearest_apt
function create_near_apt_list()
	nearest_apt_list_num = 0
	nearest_apt_list_icao = {true, true, true, true, true}
	nearest_apt_list_dist = {true, true, true, true, true}
	nearest_apt_list_dist2 = {true, true, true, true, true}
	nearest_apt_list_via = {true, true, true, true, true}
	nearest_apt_list_lat = {true, true, true, true, true}
	nearest_apt_list_lon = {true, true, true, true, true}
	nearest_apt_list_wind = {true, true, true, true, true}
	nearest_apt_list_alt = {true, true, true, true, true}
	nearest_apt_list_dir_dist = {true, true, true, true, true}
	
	local nrst_apt_icao = ""
	local jj = 0
	if string.len(B738DR_nearest_apt_icao) > 19 then
		for ii = 0, 4 do
			jj = 1 + (ii * 4)
			nrst_apt_icao = string.sub(B738DR_nearest_apt_icao, jj, jj + 3)
			if nearest_apt_icao == "    " then
				break
			else
				nearest_apt_list_num = nearest_apt_list_num + 1
				nearest_apt_list_icao[nearest_apt_list_num] = nrst_apt_icao
				nearest_apt_list_dist[nearest_apt_list_num] = 0
				nearest_apt_list_dist2[nearest_apt_list_num] = 0
				nearest_apt_list_via[nearest_apt_list_num] = 0
				nearest_apt_list_lat[nearest_apt_list_num] = B738DR_nearest_apt_lat[ii]
				nearest_apt_list_lon[nearest_apt_list_num] = B738DR_nearest_apt_lon[ii]
				nearest_apt_list_wind[nearest_apt_list_num] = ""
				nearest_apt_list_alt[nearest_apt_list_num] = 0
				nearest_apt_list_dir_dist[nearest_apt_list_num] = 0
			end
		end
	end
	
end

function B738_fmc_nearest_apt()
	max_page_buf = nearest_apt_list_num + 1
	
	local nearest_apt_dist_tmp = 0
	local nearest_apt_fuel_tmp = 0
	local nearest_apt_time_tmp = 0
	local time_zulu = simDR_zulu_hours + (simDR_zulu_minutes/60) + (simDR_zulu_seconds/3600)
	local tmp_wpt_eta2 = 0
	local tmp_wpt_eta3 = 0
	local tmp_wpt_eta = "----"
	local ii = 0
	local jj = 0
	local trip_alt = 0
	local nearest_apt_dist_tmp2 = 0
	local lat_tmp = 0
	local lon_tmp = 0
	
	local nearest_apt_list_icao_x = ""
	local nearest_apt_list_dist_x = 0
	local nearest_apt_list_dist2_x = 0
	local nearest_apt_list_via_x = 0
	local nearest_apt_list_lat_x = 0
	local nearest_apt_list_lon_x = 0
	local nearest_apt_list_wind_x = ""
	local nearest_apt_list_alt_x = 0
	local nearest_apt_list_dir_dist_x = 0
	
	local use_des_icao = ""
	if rte12_act == 0 then
		use_des_icao = des_icao
	elseif rte12_act == 1 then
		use_des_icao = des_icao7
	end
	
	if page_nearest_apt == 1 then
		line0_l = "  NEAREST AIRPORTS      "
		line0_s = "                    "
		line0_s = line0_s .. string.format("%1d", act_page_buf) .. "/" .. string.format("%1d", max_page_buf)
		
		if act_page_buf == 1 then
			line1_x = "ALTN VIA DTG  ETA  FUEL "
			
			raw_table('line_tmp')
			local line_tmp = {true, true, true, true, true}
			raw_table('line_tmps')
			local line_tmps = {true, true, true, true, true}
			for ii = 1, 5 do
				line_tmp[ii] = ""
				line_tmps[ii] = ""
			end
			
			if nearest_apt_list_num > 0 then
				if nearest_apt_list_num > 1 then
					for ii = 1, nearest_apt_list_num do
						nearest_apt_list_dir_dist[ii] = nd_calc_dist2(simDR_latitude, simDR_longitude, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
					end
					for ii = 1, nearest_apt_list_num - 1 do
						for jj = ii + 1, nearest_apt_list_num do
							if nearest_apt_list_dir_dist[ii] > nearest_apt_list_dir_dist[jj] then
								-- swap data
								nearest_apt_list_icao_x = nearest_apt_list_icao[ii]
								nearest_apt_list_dist_x = nearest_apt_list_dist[ii]
								nearest_apt_list_dist2_x = nearest_apt_list_dist2[ii]
								nearest_apt_list_via_x = nearest_apt_list_via[ii]
								nearest_apt_list_lat_x = nearest_apt_list_lat[ii]
								nearest_apt_list_lon_x = nearest_apt_list_lon[ii]
								nearest_apt_list_wind_x = nearest_apt_list_wind[ii]
								nearest_apt_list_alt_x = nearest_apt_list_alt[ii]
								nearest_apt_list_dir_dist_x = nearest_apt_list_dir_dist[ii]
								
								nearest_apt_list_icao[ii] = nearest_apt_list_icao[jj]
								nearest_apt_list_dist[ii] = nearest_apt_list_dist[jj]
								nearest_apt_list_dist2[ii] = nearest_apt_list_dist2[jj]
								nearest_apt_list_via[ii] = nearest_apt_list_via[jj]
								nearest_apt_list_lat[ii] = nearest_apt_list_lat[jj]
								nearest_apt_list_lon[ii] = nearest_apt_list_lon[jj]
								nearest_apt_list_wind[ii] = nearest_apt_list_wind[jj]
								nearest_apt_list_alt[ii] = nearest_apt_list_alt[jj]
								nearest_apt_list_dir_dist[ii] = nearest_apt_list_dir_dist[jj]
								
								nearest_apt_list_icao[jj] = nearest_apt_list_icao_x
								nearest_apt_list_dist[jj] = nearest_apt_list_dist_x
								nearest_apt_list_dist2[jj] = nearest_apt_list_dist2_x
								nearest_apt_list_via[jj] = nearest_apt_list_via_x
								nearest_apt_list_lat[jj] = nearest_apt_list_lat_x
								nearest_apt_list_lon[jj] = nearest_apt_list_lon_x
								nearest_apt_list_wind[jj] = nearest_apt_list_wind_x
								nearest_apt_list_alt[jj] = nearest_apt_list_alt_x
								nearest_apt_list_dir_dist[jj] = nearest_apt_list_dir_dist_x
							end
						end
					end
				end
				
				for ii = 1, nearest_apt_list_num do
					
					-- calculate distance, ETA, fuel
					if nearest_apt_list_via[ii] == 0 then
						if nearest_apt_list_dist[ii] == 0 then
							-- via direct-to
							nearest_apt_dist_tmp = nd_calc_dist2(ndx_lat, ndx_lon, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
						else
							nearest_apt_dist_tmp = nearest_apt_list_dist[ii]
						end
					else
						if last_miss_app_idx > 0 and last_miss_app_idx <= legs_num then
							-- via missed approach
							lat_tmp, lon_tmp = lat_lon_legs(last_miss_app_idx)
							nearest_apt_dist_tmp = nd_calc_dist2(lat_tmp, lon_tmp, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
							nearest_apt_dist_tmp = nearest_apt_dist_tmp + dist_dest + missed_app_distance
						else
							if use_des_icao == "****" then
								nearest_apt_dist_tmp = 0
							else
								-- via direct DEST ICAO to NEAR ICAO (if missed app active)
								nearest_apt_dist_tmp = nd_calc_dist2(des_icao_lat, des_icao_lon, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
							end
						end
						if nearest_apt_list_dist2[ii] == 0 then
							if use_des_icao == "****" then
								nearest_apt_dist_tmp2 = 0
							else
								nearest_apt_dist_tmp2 = nd_calc_dist2(des_icao_lat, des_icao_lon, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
							end
						else
							nearest_apt_dist_tmp2 = nearest_apt_list_dist2[ii]
						end
					end
					
					if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
						if simDR_ground_spd > 0 then
							nearest_apt_time_tmp = (nearest_apt_dist_tmp * 1852) / simDR_ground_spd
							nearest_apt_time_tmp = nearest_apt_time_tmp / 3600	-- in hours
						end
						if units == 0 then
							nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48)	--lbs
						else
							nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48 / KGS_LBS)	--kgs
						end
						nearest_apt_time_tmp = time_zulu + nearest_apt_time_tmp
						tmp_wpt_eta = time_to_string(nearest_apt_time_tmp)
					end
					
					line_tmp[ii] = nearest_apt_list_icao[ii] .. " " 
					if nearest_apt_list_via[ii] == 0 then
						line_tmp[ii] = line_tmp[ii] .. " D  "
					else
						line_tmp[ii] = line_tmp[ii] .. " M  "
					end
					nearest_apt_dist_tmp = math.min(nearest_apt_dist_tmp, 999)
					line_tmp[ii] = line_tmp[ii] .. string.format("%3d", nearest_apt_dist_tmp) .. " "
					line_tmp[ii] = line_tmp[ii] .. tmp_wpt_eta .. "  "
					if nearest_apt_fuel_tmp <= 0 then
						line_tmp[ii] = line_tmp[ii] .. "--.-"
					else
						nearest_apt_fuel_tmp = math.min(nearest_apt_fuel_tmp, 99.9)
						line_tmp[ii] = line_tmp[ii] .. string.format("%4.1f", nearest_apt_fuel_tmp)
					end
					line_tmp[ii] = line_tmp[ii] .. ">"
					line_tmps[ii] = "                 z      "
				end
			end
			
			line1_l = line_tmp[1]
			line1_s = line_tmps[1]
			line2_l = line_tmp[2]
			line2_s = line_tmps[2]
			line3_l = line_tmp[3]
			line3_s = line_tmps[3]
			line4_l = line_tmp[4]
			line4_s = line_tmps[4]
			line5_l = line_tmp[5]
			line5_s = line_tmps[5]
			
			line6_l = "<INDEX         PREVIOUS>"
		else
			ii = act_page_buf - 1
			-- calculate distance, ETA, fuel
			
			if nearest_apt_list_via[ii] == 0 then
				if nearest_apt_list_dist[ii] == 0 then
					-- via direct-to
					nearest_apt_dist_tmp = nd_calc_dist2(ndx_lat, ndx_lon, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
				else
					nearest_apt_dist_tmp = nearest_apt_list_dist[ii]
				end
			else
				if last_miss_app_idx > 0 and last_miss_app_idx <= legs_num then
					-- via missed approach
					lat_tmp, lon_tmp = lat_lon_legs(last_miss_app_idx)
					nearest_apt_dist_tmp = nd_calc_dist2(lat_tmp, lon_tmp, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
					nearest_apt_dist_tmp = nearest_apt_dist_tmp + dist_dest + missed_app_distance
				else
					if use_des_icao == "****" then
						nearest_apt_dist_tmp = 0
					else
						-- via direct DEST ICAO to NEAR ICAO (if missed app active)
						nearest_apt_dist_tmp = nd_calc_dist2(des_icao_lat, des_icao_lon, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
					end
				end
				if nearest_apt_list_dist2[ii] == 0 then
					if use_des_icao == "****" then
						nearest_apt_dist_tmp2 = 0
					else
						nearest_apt_dist_tmp2 = nd_calc_dist2(des_icao_lat, des_icao_lon, nearest_apt_list_lat[ii], nearest_apt_list_lon[ii])
					end
				else
					nearest_apt_dist_tmp2 = nearest_apt_list_dist2[ii]
				end
			end
			
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if simDR_ground_spd > 0 then
					nearest_apt_time_tmp = (nearest_apt_dist_tmp * 1852) / simDR_ground_spd
					nearest_apt_time_tmp = nearest_apt_time_tmp / 3600	-- in hours
				end
				if units == 0 then
					nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48)	--lbs
				else
					nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48 / KGS_LBS)	--kgs
				end
				nearest_apt_time_tmp = time_zulu + nearest_apt_time_tmp
				tmp_wpt_eta = time_to_string(nearest_apt_time_tmp)
			end
			
			line1_x = " ALTN   VIA     TRIP ALT"
			line1_l = nearest_apt_list_icao[ii] .. "   "
			
			line3_x = " ETA           "
			line3_l = tmp_wpt_eta
			
			if nearest_apt_list_via[ii] == 0 then
				line1_l = line1_l .. "DIRECT"
				line5_l = "<MISSED APP             "
				line3_s = "    z                "
				nearest_apt_list_dist2[ii] = 0
			else
				line1_l = line1_l .. "MISAPP"
				line5_l = "<DIRECT-TO              "
				line3_s = "    z                "
				if use_des_icao ~= "****" then
					line3_x = line3_x .. use_des_icao .. "-" .. nearest_apt_list_icao[ii]
					if nearest_apt_list_dist2[ii] == 0 then
						line3_s = line3_s .. string.format("%3d", nearest_apt_dist_tmp2)
					else
						line3_l = line3_l .. "                 " .. string.format("%3d", nearest_apt_dist_tmp2)
					end
				end
			end
			
			if nearest_apt_list_alt[ii] == 0 then
				if nearest_apt_list_via[ii] == 0 then
					if nearest_apt_dist_tmp ~= 0 then
						trip_alt = calc_trip(nearest_apt_dist_tmp) / 100
						trip_alt = math.max(trip_alt, 40)
						line1_s = "                   " .. "FL" .. string.format("%03d", trip_alt)
					end
				else
					if nearest_apt_dist_tmp2 ~= 0 then
						trip_alt = calc_trip(nearest_apt_dist_tmp2) / 100
						trip_alt = math.max(trip_alt, 40)
						line1_s = "                   " .. "FL" .. string.format("%03d", trip_alt)
					end
				end
			else
				line1_l = line1_l .. "      " .. "FL" .. string.sub(string.format("%05d", nearest_apt_list_alt[ii]), 1, 3)
			end
			
			line2_x = " DTG"
			nearest_apt_dist_tmp = math.min(nearest_apt_dist_tmp, 999)
			line2_l = string.format("%3d", nearest_apt_dist_tmp)
			line2_s = "                "
			if nearest_apt_list_wind[ii] == "" then
				if nearest_apt_list_via[ii] == 0 then
					line2_x = "         ACTUAL WIND"
					if simDR_wind_spd > 0.5 then
						line2_s = line2_s .. string.format("%03d", simDR_wind_hdg)
						line2_s = line2_s .. "`/"
						line2_s = line2_s .. string.format("%03d", simDR_wind_spd)
					else
						line2_s = line2_s .. "---`/---"
					end
				else
					line2_x = "            EST WIND"
					line2_s = line2_s .. "---`/---"
				end
			else
				line2_x = "            EST WIND"
				line2_l = line2_l .. "             " .. nearest_apt_list_wind[ii]
			end
			
			line4_x = " FUEL                   "
			if nearest_apt_fuel_tmp <= 0 then
				line4_l = "--.-"
			else
				nearest_apt_fuel_tmp = math.min(nearest_apt_fuel_tmp, 99.9)
				line4_l = string.format("%4.1f", nearest_apt_fuel_tmp)
			end
			
			line6_l = "<INDEX         PREVIOUS>"
		end
	end
end

function calc_trip(in_distance)
	
	local trip_dist = math.min(in_distance, 300)
	local alt_min = 0
	local alt_max = 0
	local weight_min = 0
	local weight_max = 0
	local result = 0
	
	if trip_dist <= 10 then
		alt_min = 10
		alt_max = 30
		weight_min = 4000
		weight_max = 6000
	elseif trip_dist <= 30 then
		alt_min = 30
		alt_max = 50
		weight_min = 7000
		weight_max = 9000
	elseif trip_dist <= 70 then
		alt_min = 50
		alt_max = 70
		weight_min = 9000
		weight_max = 11000
	elseif trip_dist <= 100 then
		alt_min = 70
		alt_max = 100
		weight_min = 11000
		weight_max = 13000
	elseif trip_dist <= 150 then
		alt_min = 100
		alt_max = 150
		weight_min = 13000
		weight_max = 17000
	elseif trip_dist <= 185 then
		alt_min = 150
		alt_max = 185
		weight_min = 17000
		weight_max = 23000
	elseif trip_dist <= 210 then
		alt_min = 185
		alt_max = 210
		weight_min = 23000
		weight_max = 25000
	elseif trip_dist <= 250 then
		alt_min = 210
		alt_max = 250
		weight_min = 25000
		weight_max = 27000
	elseif trip_dist <= 300 then
		alt_min = 250
		alt_max = 300
		weight_min = 27000
		weight_max = 29000
	end
	result = B738_rescale(alt_min, weight_min, alt_max, weight_max, trip_dist)
	result = roundUpToIncrement(result, 100)
	
	return result
end

function add_altn_apt(in_apt_icao, in_lsk, in_fmc)
	
	local altern_apt_list_icao_tmp = "----"
	local altern_apt_list_dist_tmp = 0
	local altern_apt_list_via_tmp = 0
	local altern_apt_list_lat_tmp = 0
	local altern_apt_list_lon_tmp = 0
	local altern_apt_list_alt_tmp = 0
	local altern_apt_list_wind_tmp = ""
	local altern_apt_list_dist2_tmp = 0
	local ii = 0
	
	if in_apt_icao == ">DELETE" then
		if altern_apt_list_icao[in_lsk] ~= "----" then
			if in_lsk < 5 then
				for ii = in_lsk, 4 do
					altern_apt_list_icao[ii] = altern_apt_list_icao[ii+1]
					altern_apt_list_dist[ii] = altern_apt_list_dist[ii+1]
					altern_apt_list_via[ii] = altern_apt_list_via[ii+1]
					altern_apt_list_lat[ii] = altern_apt_list_lat[ii+1]
					altern_apt_list_lon[ii] = altern_apt_list_lon[ii+1]
					altern_apt_list_alt[ii] = altern_apt_list_alt[ii+1]
					altern_apt_list_wind[ii] = altern_apt_list_wind[ii+1]
					altern_apt_list_dist2[ii] = altern_apt_list_dist2[ii+1]
				end
			end
			altern_apt_list_icao[5] = "----"
			altern_apt_list_dist[5] = 0
			altern_apt_list_via[5] = 0
			altern_apt_list_lat[5] = 0
			altern_apt_list_lon[5] = 0
			altern_apt_list_alt[5] = 0
			altern_apt_list_wind[5] = ""
			altern_apt_list_dist2[5] = 0
			altern_apt_list_num = altern_apt_list_num - 1
		end
		if in_fmc == 0 then
			entry = ""
		else
			entry2 = ""
		end
	elseif string.len(in_apt_icao) == 0 then
		if in_fmc == 0 then
			entry = altern_apt_list_icao[in_lsk]
			if entry == "----" then
				entry = ""
			end
		else
			entry2 = altern_apt_list_icao[in_lsk]
			if entry2 == "----" then
				entry2 = ""
			end
		end
	elseif string.len(in_apt_icao) ~= 4 then
		add_fmc_msg(NOT_IN_DATABASE, 1)
	else
		-- find airport (ICAO)
		find_navaid(in_apt_icao, "", 0, "", rte12_act)
		if navaid_list_n > 0 then
			altern_apt_list_icao_tmp = in_apt_icao
			altern_apt_list_dist_tmp = 0
			altern_apt_list_via_tmp = 0
			altern_apt_list_lat_tmp = navaid_list[1][2]
			altern_apt_list_lon_tmp = navaid_list[1][3]
			altern_apt_list_alt_tmp = 0
			altern_apt_list_wind_tmp = ""
			altern_apt_list_dist2_tmp = 0
			if altern_apt_list_num == 5 or altern_apt_list_icao[in_lsk] ~= "----" then
				altern_apt_list_icao[in_lsk] = altern_apt_list_icao_tmp
				altern_apt_list_dist[in_lsk] = altern_apt_list_dist_tmp
				altern_apt_list_via[in_lsk] = altern_apt_list_via_tmp
				altern_apt_list_lat[in_lsk] = altern_apt_list_lat_tmp
				altern_apt_list_lon[in_lsk] = altern_apt_list_lon_tmp
				altern_apt_list_alt[in_lsk] = altern_apt_list_alt_tmp
				altern_apt_list_wind[in_lsk] = altern_apt_list_wind_tmp
				altern_apt_list_dist2[in_lsk] = altern_apt_list_dist2_tmp
			else
				for ii = 1, 5 do
					if altern_apt_list_icao[ii] == "----" then
						altern_apt_list_icao[ii] = altern_apt_list_icao_tmp
						altern_apt_list_dist[ii] = altern_apt_list_dist_tmp
						altern_apt_list_via[ii] = altern_apt_list_via_tmp
						altern_apt_list_lat[ii] = altern_apt_list_lat_tmp
						altern_apt_list_lon[ii] = altern_apt_list_lon_tmp
						altern_apt_list_alt[ii] = altern_apt_list_alt_tmp
						altern_apt_list_wind[ii] = altern_apt_list_wind_tmp
						altern_apt_list_dist2[ii] = altern_apt_list_dist2_tmp
						altern_apt_list_num = ii
						break
					end
				end
			end
			if in_fmc == 0 then
				entry = ""
			else
				entry2 = ""
			end
		else
			add_fmc_msg(NOT_IN_DATABASE, 1)
		end
	end
end

function B738_fmc_altern_apt()
	
	max_page_buf = altern_apt_list_num + 1
	
	local nearest_apt_dist_tmp = 0
	local nearest_apt_fuel_tmp = 0
	local nearest_apt_time_tmp = 0
	local time_zulu = simDR_zulu_hours + (simDR_zulu_minutes/60) + (simDR_zulu_seconds/3600)
	local tmp_wpt_eta2 = 0
	local tmp_wpt_eta3 = 0
	local tmp_wpt_eta = "----"
	local ii = 0
	local trip_alt = 0
	local nearest_apt_dist_tmp2 = 0
	local lat_tmp = 0
	local lon_tmp = 0
	
	local use_des_icao = ""
	if rte12_act == 0 then
		use_des_icao = des_icao
	elseif rte12_act == 1 then
		use_des_icao = des_icao7
	end
	
	if page_altern_apt == 1 then
		line0_l = "  ALTERNATE DESTS       "
		line0_s = "                    "
		line0_s = line0_s .. string.format("%1d", act_page_buf) .. "/" .. string.format("%1d", max_page_buf)
		
		if act_page_buf == 1 then
			line1_x = "ALTN VIA DTG  ETA  FUEL "
			
			raw_table('line_tmp')
			local line_tmp = {true, true, true, true, true}
			raw_table('line_tmps')
			local line_tmps = {true, true, true, true, true}
			for ii = 1, 5 do
				line_tmp[ii] = ""
				line_tmps[ii] = ""
			end
			
			for ii = 1, 5 do
				if altern_apt_list_icao[ii] == "----" then
					line_tmp[ii] = "----"
				else
					
					-- calculate distance, ETA, fuel
					if altern_apt_list_via[ii] == 0 then
						if altern_apt_list_dist[ii] == 0 then
							-- via direct-to
							nearest_apt_dist_tmp = nd_calc_dist2(ndx_lat, ndx_lon, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
						else
							nearest_apt_dist_tmp = altern_apt_list_dist[ii]
						end
					else
						if last_miss_app_idx > 0 and last_miss_app_idx <= legs_num then
							-- via missed approach
							lat_tmp, lon_tmp = lat_lon_legs(last_miss_app_idx)
							nearest_apt_dist_tmp = nd_calc_dist2(lat_tmp, lon_tmp, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
							nearest_apt_dist_tmp = nearest_apt_dist_tmp + dist_dest + missed_app_distance
						else
							if use_des_icao == "****" then
								nearest_apt_dist_tmp = 0
							else
								-- via direct DEST ICAO to NEAR ICAO (if missed app active)
								nearest_apt_dist_tmp = nd_calc_dist2(des_icao_lat, des_icao_lon, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
							end
						end
						if altern_apt_list_dist2[ii] == 0 then
							if use_des_icao == "****" then
								nearest_apt_dist_tmp2 = 0
							else
								nearest_apt_dist_tmp2 = nd_calc_dist2(des_icao_lat, des_icao_lon, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
							end
						else
							nearest_apt_dist_tmp2 = altern_apt_list_dist2[ii]
						end
					end
					
					if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
						if simDR_ground_spd > 0 then
							nearest_apt_time_tmp = (nearest_apt_dist_tmp * 1852) / simDR_ground_spd
							nearest_apt_time_tmp = nearest_apt_time_tmp / 3600	-- in hours
						end
						if units == 0 then
							nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48)	--lbs
						else
							nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48 / KGS_LBS)	--kgs
						end
						nearest_apt_time_tmp = time_zulu + nearest_apt_time_tmp
						tmp_wpt_eta = time_to_string(nearest_apt_time_tmp)
					end
					
					line_tmp[ii] = altern_apt_list_icao[ii] .. " " 
					if altern_apt_list_via[ii] == 0 then
						line_tmp[ii] = line_tmp[ii] .. " D  "
					else
						line_tmp[ii] = line_tmp[ii] .. " M  "
					end
					nearest_apt_dist_tmp = math.min(nearest_apt_dist_tmp, 999)
					line_tmp[ii] = line_tmp[ii] .. string.format("%3d", nearest_apt_dist_tmp) .. " "
					line_tmp[ii] = line_tmp[ii] .. tmp_wpt_eta .. "  "
					if nearest_apt_fuel_tmp <= 0 then
						line_tmp[ii] = line_tmp[ii] .. "--.-"
					else
						nearest_apt_fuel_tmp = math.min(nearest_apt_fuel_tmp, 99.9)
						line_tmp[ii] = line_tmp[ii] .. string.format("%4.1f", nearest_apt_fuel_tmp)
					end
					line_tmp[ii] = line_tmp[ii] .. ">"
					line_tmps[ii] = "                 z      "
				end
			end
			
			line1_l = line_tmp[1]
			line1_s = line_tmps[1]
			line2_l = line_tmp[2]
			line2_s = line_tmps[2]
			line3_l = line_tmp[3]
			line3_s = line_tmps[3]
			line4_l = line_tmp[4]
			line4_s = line_tmps[4]
			line5_l = line_tmp[5]
			line5_s = line_tmps[5]
			
			line6_l = "<INDEX    NEAREST ARPTS>"
		else
			ii = act_page_buf - 1
			-- calculate distance, ETA, fuel
			
			
			if altern_apt_list_via[ii] == 0 then
				if altern_apt_list_dist[ii] == 0 then
					-- via direct-to
					nearest_apt_dist_tmp = nd_calc_dist2(ndx_lat, ndx_lon, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
				else
					nearest_apt_dist_tmp = altern_apt_list_dist[ii]
				end
			else
				if last_miss_app_idx > 0 and last_miss_app_idx <= legs_num then
					-- via missed approach
					lat_tmp, lon_tmp = lat_lon_legs(last_miss_app_idx)
					nearest_apt_dist_tmp = nd_calc_dist2(lat_tmp, lon_tmp, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
					nearest_apt_dist_tmp = nearest_apt_dist_tmp + dist_dest + missed_app_distance
				else
					if use_des_icao == "****" then
						nearest_apt_dist_tmp = 0
					else
						-- via direct DEST ICAO to NEAR ICAO (if missed app active)
						nearest_apt_dist_tmp = nd_calc_dist2(des_icao_lat, des_icao_lon, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
					end
				end
				if altern_apt_list_dist2[ii] == 0 then
					if use_des_icao == "****" then
						nearest_apt_dist_tmp2 = 0
					else
						nearest_apt_dist_tmp2 = nd_calc_dist2(des_icao_lat, des_icao_lon, altern_apt_list_lat[ii], altern_apt_list_lon[ii])
					end
				else
					nearest_apt_dist_tmp2 = altern_apt_list_dist2[ii]
				end
			end
			
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if simDR_ground_spd > 0 then
					nearest_apt_time_tmp = (nearest_apt_dist_tmp * 1852) / simDR_ground_spd
					nearest_apt_time_tmp = nearest_apt_time_tmp / 3600	-- in hours
				end
				if units == 0 then
					nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48)	--lbs
				else
					nearest_apt_fuel_tmp = fuel_weight - (nearest_apt_time_tmp * 6.48 / KGS_LBS)	--kgs
				end
				nearest_apt_time_tmp = time_zulu + nearest_apt_time_tmp
				tmp_wpt_eta = time_to_string(nearest_apt_time_tmp)
			end
			
			line1_x = " ALTN   VIA     TRIP ALT"
			line1_l = altern_apt_list_icao[ii] .. "   "
			
			line3_x = " ETA           "
			line3_l = tmp_wpt_eta
			
			if altern_apt_list_via[ii] == 0 then
				line1_l = line1_l .. "DIRECT"
				line5_l = "<MISSED APP             "
				line3_s = "    z                "
				altern_apt_list_dist2[ii] = 0
			else
				line1_l = line1_l .. "MISAPP"
				line5_l = "<DIRECT-TO              "
				line3_s = "    z                "
				if use_des_icao ~= "****" then
					line3_x = line3_x .. use_des_icao .. "-" .. altern_apt_list_icao[ii]
					if altern_apt_list_dist2[ii] == 0 then
						line3_s = line3_s .. string.format("%3d", nearest_apt_dist_tmp2)
					else
						line3_l = line3_l .. "                 " .. string.format("%3d", nearest_apt_dist_tmp2)
					end
				end
			end
			
			if altern_apt_list_alt[ii] == 0 then
				if altern_apt_list_via[ii] == 0 then
					if nearest_apt_dist_tmp ~= 0 then
						trip_alt = calc_trip(nearest_apt_dist_tmp) / 100
						trip_alt = math.max(trip_alt, 40)
						line1_s = "                   " .. "FL" .. string.format("%03d", trip_alt)
					end
				else
					if nearest_apt_dist_tmp2 ~= 0 then
						trip_alt = calc_trip(nearest_apt_dist_tmp2) / 100
						trip_alt = math.max(trip_alt, 40)
						line1_s = "                   " .. "FL" .. string.format("%03d", trip_alt)
					end
				end
			else
				line1_l = line1_l .. "      " .. "FL" .. string.sub(string.format("%05d", altern_apt_list_alt[ii]), 1, 3)
			end
			
			line2_x = " DTG"
			nearest_apt_dist_tmp = math.min(nearest_apt_dist_tmp, 999)
			line2_l = string.format("%3d", nearest_apt_dist_tmp)
			line2_s = "                "
			if altern_apt_list_wind[ii] == "" then
				if altern_apt_list_via[ii] == 0 then
					line2_x = "         ACTUAL WIND"
					if simDR_wind_spd > 0.5 then
						line2_s = line2_s .. string.format("%03d", simDR_wind_hdg)
						line2_s = line2_s .. "`/"
						line2_s = line2_s .. string.format("%03d", simDR_wind_spd)
					else
						line2_s = line2_s .. "---`/---"
					end
				else
					line2_x = "            EST WIND"
					line2_s = line2_s .. "---`/---"
				end
			else
				line2_x = "            EST WIND"
				line2_l = line2_l .. "             " .. altern_apt_list_wind[ii]
			end
			
			line4_x = " FUEL                   "
			if nearest_apt_fuel_tmp <= 0 then
				line4_l = "--.-"
			else
				nearest_apt_fuel_tmp = math.min(nearest_apt_fuel_tmp, 99.9)
				line4_l = string.format("%4.1f", nearest_apt_fuel_tmp)
			end
			
			line6_l = "<INDEX    NEAREST ARPTS>"
		end
	end
end

function B738_fmc_descent()

	if page_descent == 1 then
		act_page_buf = 1
		max_page_buf = 1
		local v_s = 0
		local vpa = 0
		local dis_rest = 0
		local ap_mcp_spd = simDR_airspeed_dial
		local temp_speed = 0
		local temp_speed2 = 0
		local econ_on = 0
		local TOD_str = ""
		local time_TOD = 0
		local time_TOD_h = 0
		local time_TOD_m = 0
		local time_TOD_s = 0
		local delta_alt = 0
		local nav_id = ""
		local str_rest_alt = ""
		local tmp_wpt_eta = ""
		local tmp_wpt_eta2 = 0
		local tmp_wpt_eta3 = 0
		local phase_des_app = 0
		
		local lat_tmp = 0
		local lon_tmp = 0
		
		if perf_exec == 0 then
			line0_l = "     ECON PATH DES      "
			if B738DR_fmc_type == 0 then
				line0_s = "                    1/1 "
			else
				line0_s = "                    3/3 "
			end
			line1_x = " E/D ALT"
			line2_x = " TGT SPD"
			line3_x = "                 WPT/ALT"
			line3_l = "            ------/-----"
			line4_x = " VERT DEV   FPA V/B  V/S"
			line4_l = "            "
			if simDR_ground_spd == 0 then
				line4_l = line4_l .. "0.0"
			else
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					vpa = 0
				else
					vpa = (simDR_vvi_fpm_pilot / 60) * 0.3048
					vpa = vpa / simDR_ground_spd
					vpa = -vpa
					if vpa < 0 then
						vpa = 0
					end
					vpa = math.atan(vpa)
					vpa = math.deg(vpa)
				end
				line4_l = line4_l .. string.format("%3.1f", vpa)
			end
			line5_x = "------------------------"
			line5_l = "                  SPEED>"
			line6_l = "<FORECAST           RTA>"
		else
			
			if B738DR_flight_phase == 5 or B738DR_flight_phase == 6 or B738DR_flight_phase == 7 then
				phase_des_app = 1
			end
			
			if phase_des_app == 1 then
				if perf_exec < 2 then
					line0_l = "      "
				elseif descent_exec == 0 and des_spd_exec == 0 then
					line0_l = " ACT "
				else
					line0_l = "      "
					line0_inv = " MOD "
				end
				if B738DR_rest_wpt_alt_idx == 0 then
					TOD_str = "   ----.- /---NM"
				else
					tmp_wpt_eta = time_to_string2(legs_data[B738DR_rest_wpt_alt_idx][13])
					TOD_str = "   " .. tmp_wpt_eta
					TOD_str = TOD_str .. " /---NM"
				end
				line2_x = " TGT SPD        TO " .. B738DR_rest_wpt_alt_id
				TOD_str = "   ----.- /---NM"
			else
				if perf_exec < 2 then
					line0_l = "     "
				elseif descent_exec == 0 and des_spd_exec == 0 then
					line0_l = "     "
				else
					-- line0_l = "  MOD "
					line0_l = "     "
					line0_inv = " MOD "
				end
				if B738DR_vnav_td_dist > 0 and B738DR_vnav_td_dist < 1000 then
					if time_td == 0 then
						TOD_str = "   ----.- /---NM"
						line2_x = " TGT SPD        TO "
					else
						tmp_wpt_eta = time_to_string2(time_td)
						TOD_str = "   " .. tmp_wpt_eta .. " /"
						if dist_td >= 999.5 then
							TOD_str = TOD_str .. "999"
						else
							TOD_str = TOD_str .. string.format("%3d", dist_td)
						end
						TOD_str = TOD_str .. "NM"
						line2_x = " TGT SPD          TO T/D"
					end
				else
					TOD_str = "   ----.- /---NM"
					line2_x = " TGT SPD        TO "
				end
			end
			if perf_exec < 2 then
				line0_l = line0_l .. "ECON PATH DES      "
			elseif fmc_descent_mode_mod == 0 then
				line0_l = line0_l .. "ECON PATH DES      "
			elseif fmc_descent_mode_mod == 1 then
				line0_l = line0_l .. "ECON SPD DES       "
			elseif fmc_descent_mode_mod == 2 then
				if simDR_airspeed_is_mach == 0 then
					if B738DR_ap_spd_interv_status == 0 then
						ap_mcp_spd = B738DR_fmc_descent_speed
					else
						ap_mcp_spd = B738DR_mcp_speed_dial
					end
					line0_l = line0_l .. string.format("%3d", ap_mcp_spd)
					line0_l = line0_l .. "KT PATH DES    "
				else
					if B738DR_ap_spd_interv_status == 0 then
						ap_mcp_spd = B738DR_fmc_descent_speed_mach
					else
						ap_mcp_spd = B738DR_mcp_speed_dial
					end
					line0_l = line0_l .. "M."
					line0_l = line0_l .. string.format("%3d", (ap_mcp_spd * 1000))
					line0_l = line0_l .. " PATH DES    "
				end
			else
				if simDR_airspeed_is_mach == 0 then
					if B738DR_ap_spd_interv_status == 0 then
						ap_mcp_spd = B738DR_fmc_descent_speed
					else
						ap_mcp_spd = B738DR_mcp_speed_dial
					end
					line0_l = line0_l .. string.format("%3d", ap_mcp_spd)
					line0_l = line0_l .. "KT SPD DES     "
				else
					if B738DR_ap_spd_interv_status == 0 then
						ap_mcp_spd = B738DR_fmc_descent_speed_mach
					else
						ap_mcp_spd = B738DR_mcp_speed_dial
					end
					line0_l = line0_l .. "M."
					line0_l = line0_l .. string.format("%3d", (ap_mcp_spd * 1000))
					line0_l = line0_l .. " SPD DES     "
				end
			end
			
			ap_mcp_spd = simDR_airspeed_dial
			
			if B738DR_fmc_type == 0 then
				line0_s = "                    1/1 "
			else
				line0_s = "                    3/3 "
			end
			str_rest_alt = ""
			line1_s = ""
			if B738DR_rest_wpt_alt > 0 and phase_des_app == 1 then
				line1_x = " E/D ALT        AT " .. B738DR_rest_wpt_alt_id
				if B738DR_rest_wpt_alt_idx > 0 then
					if legs_data[B738DR_rest_wpt_alt_idx][4] == 0 then
						if legs_data[B738DR_rest_wpt_alt_idx][10] == 0 then
							str_rest_alt = "            "
							line1_s = "              ---"
						else
							str_rest_alt = "            "
							if legs_data[B738DR_rest_wpt_alt_idx][10] < 0 then
								-- calc spd is mach
								line1_s = "             " ..  string.sub(string.format("%5.1f",legs_data[B738DR_rest_wpt_alt_idx][10]), 2, -1)
							else
								-- calc spd is kts
								line1_s = "              " ..  string.format("%3d",legs_data[B738DR_rest_wpt_alt_idx][10])
							end
						end
					else
						str_rest_alt = "         " .. string.format("%3d", legs_data[B738DR_rest_wpt_alt_idx][4])
						line1_s = ""
					end
				else
					str_rest_alt = "            "
					line1_s = ""
				end
				str_rest_alt = str_rest_alt .. " "
				if B738DR_rest_wpt_alt >= B738DR_trans_lvl then
					str_rest_alt = str_rest_alt .. "FL"
					str_rest_alt = str_rest_alt .. string.sub(string.format("%05d", B738DR_rest_wpt_alt), 1, 3)
				else
					str_rest_alt = str_rest_alt .. string.format("%5d", B738DR_rest_wpt_alt)
				end
				if B738DR_rest_wpt_alt_t == 43 then
					str_rest_alt = str_rest_alt .. "A"
				elseif B738DR_rest_wpt_alt_t == 45 then
					str_rest_alt = str_rest_alt .. "B"
				end
			else
				line1_x = " E/D ALT"
			end
			
			local ed_alt_new = 0
			if rnav_idx_last ~= 0 and rnav_idx_last <= legs_num then
				ed_alt_new = rnav_alt
				if str_rest_alt == "" then
					line1_m = ""
					line1_l = string.format("%5d", ed_alt_new)
				else
					line1_m = "     " .. str_rest_alt
					line1_l = string.format("%5d", ed_alt_new) .. "            /"
				end
			else
				if str_rest_alt == "" then
					line1_m = ""
					line1_l = ""
				else
					line1_m = "     " .. str_rest_alt
					line1_l = "                 /"
				end
			end
			
			if phase_des_app == 1 then
				if des_spd_exec ~= 0 then
					if simDR_airspeed_is_mach == 0 then
						if fmc_descent_speed_mod ~= 0 then
							line2_m = "     " .. string.format("%3d", fmc_descent_speed_mod)
						else
							if ap_mcp_spd == temp_speed then
								line2_m = "     " .. string.format("%3d", B738DR_fmc_descent_speed)
							else
								line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
							end
						end
						if fmc_descent_speed_mach_mod ~= 0 then
							line2_l = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5) .. "/   "
						else
							line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/   "
						end
					else
						if fmc_descent_speed_mod ~= 0 then
							line2_l = "    /" .. string.format("%3d", fmc_descent_speed_mod)
						else
							line2_l = "    /" .. string.format("%3d", B738DR_fmc_descent_speed)
						end
						if fmc_descent_speed_mach_mod ~= 0 then
							line2_m = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5)
						else
							line2_m = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5)
						end
					end
				elseif B738DR_ap_spd_interv_status == 0 then
					ap_mcp_spd = tonumber(string.format("%3d", simDR_airspeed_dial))
					temp_speed = tonumber(string.format("%3d", B738DR_fmc_descent_speed))
					if simDR_airspeed_is_mach == 0 then
						if ap_mcp_spd == temp_speed then
							line2_m = "     " .. string.format("%3d", B738DR_fmc_descent_speed)
							line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/   "
						else
							line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
							line2_l = line2_l .. string.format("%3d", B738DR_fmc_descent_speed)
							line2_m = ""
						end
					else
						line2_l = "    /" .. string.format("%3d", B738DR_fmc_descent_speed)
						line2_m = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5)
					end
				else
					if simDR_airspeed_is_mach == 0 then
						line2_m = string.format("%3d", B738DR_mcp_speed_dial)
						line2_l = "   /MCP "
					else
						line2_m = string.sub(string.format("%5.3f", B738DR_mcp_speed_dial), 2, 5)
						line2_l = "    /MCP"
					end
				end
				if B738DR_rest_wpt_alt_dist > 0 then
					tmp_wpt_eta = "   " .. time_to_string(legs_data[B738DR_rest_wpt_alt_idx][13])
					tmp_wpt_eta = tmp_wpt_eta .. ".0 /"
					TOD_str = tmp_wpt_eta
					if B738DR_rest_wpt_alt_dist >= 999.5 then
						TOD_str = TOD_str .. "999"
					else
						TOD_str = TOD_str .. string.format("%3d", B738DR_rest_wpt_alt_dist)
					end
					TOD_str = TOD_str .. "NM"
				end
			else
				if des_spd_exec ~= 0 then
					if fmc_descent_speed_mach_mod ~= 0 then
						line2_l = string.sub(string.format("%5.3f", fmc_descent_speed_mach_mod), 2, 5) .. "/"
					else
						line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
					end
					if fmc_descent_speed_mod ~= 0 then
						line2_l = line2_l .. string.format("%3d", fmc_descent_speed_mod)
					else
						line2_l = line2_l .. string.format("%3d", B738DR_fmc_descent_speed)
					end
				else
					line2_l = string.sub(string.format("%5.3f", B738DR_fmc_descent_speed_mach), 2, 5) .. "/"
					line2_l = line2_l .. string.format("%3d", B738DR_fmc_descent_speed)
				end
				line2_m = ""
			end
			line2_l = line2_l .. TOD_str
			
			if descent_r_alt1_mod >= B738DR_trans_lvl_mod then
				fmc_descent_r_alt1_mod = "FL" .. string.sub(string.format("%05d", descent_r_alt1_mod), 1, 3)
			else
				fmc_descent_r_alt1_mod = string.format("%5d", descent_r_alt1_mod)
			end
			
			line2_s = "                 Z      "
			line3_x = " SPD REST        WPT/ALT"
			str_rest_alt = "-----/-----"
			if phase_des_app == 1 then
				if B738DR_rest_wpt_alt > 0 then
					str_rest_alt = B738DR_rest_wpt_alt_id .. "/"
					if legs_data[B738DR_rest_wpt_alt_idx][11] == 0 then
						str_rest_alt = str_rest_alt .. "-----"
					else
						if B738DR_rest_wpt_alt >= B738DR_trans_lvl then
							str_rest_alt = str_rest_alt .. "FL"
							str_rest_alt = str_rest_alt .. string.sub(string.format("%05d", legs_data[B738DR_rest_wpt_alt_idx][11]), 1, 3)
						else
							str_rest_alt = str_rest_alt .. string.format("%5d", legs_data[B738DR_rest_wpt_alt_idx][11])
						end
					end
				end
				if B738DR_ap_spd_interv_status == 0 then
					
					if simDR_airspeed_dial < B738DR_fmc_descent_speed then
						if descent_exec == 1 then
							if descent_r_speed1_mod == 0 then
								line3_l = "---/-----    " .. str_rest_alt
								line3_m = ""
							else
								line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "    " .. str_rest_alt
								line3_m = ""
							end
						elseif B738DR_fmc_descent_speed_l_act == 1 then
							line3_l = "   /" .. B738DR_rest_wpt_spd_id
							line3_l = line3_l .. "    "
							line3_l = line3_l .. str_rest_alt
							line3_m = string.format("%3d", B738DR_rest_wpt_spd)
							des_del_spd_idx = B738DR_rest_wpt_spd_idx
						elseif B738DR_fmc_descent_speed_l_act == 2 then
							line3_l = "   /" .. fmc_descent_r_alt1_mod .. "    " .. str_rest_alt
							line3_m = string.format("%03d", descent_r_speed1_mod)
						elseif B738DR_fmc_descent_speed_l_act == 3 then
							line3_l = "   /FLAPS"
							line3_l = line3_l .. "    "
							line3_l = line3_l .. str_rest_alt
							line3_m = string.format("%3d", flaps_speed)
						elseif B738DR_fmc_descent_speed_l_act == 4 then
							line3_l = "   /FLAPS"
							line3_l = line3_l .. "    "
							line3_l = line3_l .. str_rest_alt
							line3_m = string.format("%3d", B738DR_pfd_flaps_up)
						else
							if descent_r_speed1_mod == 0 then
								line3_l = "---/-----    " .. str_rest_alt
								line3_m = ""
							else
								line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "    " .. str_rest_alt
								line3_m = ""
							end
						end
					else
						if descent_r_speed1_mod == 0 then
							line3_l = "---/-----    " .. str_rest_alt
							line3_m = ""
						else
							line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "    " .. str_rest_alt
							line3_m = ""
						end
					end
				else
					if descent_r_speed1_mod == 0 then
						line3_l = "---/-----    " .. str_rest_alt
						line3_m = ""
					else
						line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "    " .. str_rest_alt
						line3_m = ""
					end
				end
			else
				if descent_r_speed1_mod == 0 then
					line3_l = "---/-----    -----/-----"
					line3_m = ""
				else
					line3_l = string.format("%03d", descent_r_speed1_mod) .. "/".. fmc_descent_r_alt1_mod .. "    -----/-----"
					line3_m = ""
				end
			end
			if B738DR_nd_vert_path == 1 then
				line4_x = " VERT DEV   FPA V/B  V/S"
				
				-- VERT DEV
				vpa = -B738DR_vnav_err_pfd
				if vpa < 0 then
					vpa = -vpa
					line4_s = "     LO"
				else
					line4_s = "     HI"
				end
				if vpa > 9999 then
					vpa = 9999
				end
				line4_l = string.format("%4d", vpa) .. "        "
				
				-- FPA
				if simDR_ground_spd == 0 then
					line4_l = line4_l .. "0.0"
				else
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						vpa = 0
					else
						vpa = (simDR_vvi_fpm_pilot / 60) * 0.3048
						vpa = vpa / simDR_ground_spd
						vpa = -vpa
						if vpa < 0 then
							vpa = 0
						end
						vpa = math.atan(vpa)
						vpa = math.deg(vpa)
					end
					line4_l = line4_l .. string.format("%3.1f", vpa)
				end
				line4_l = line4_l .. " "
				
				if vb_calc_id ~= "" then
					line3_l = string.sub(line3_l, 1, 13) .. spaces_before(vb_calc_id, 5) .. "/" .. string.format ("%5d", vb_calc_alt)
					if vb_calc_alt > simDR_altitude_pilot then
						-- V/B V/S
						line4_l = line4_l .. "0.0    0"
					else
						-- V/B
						dis_rest = nd_calc_dist2(ndx_lat, ndx_lon, vb_calc_lat, vb_calc_lon)
						v_s = simDR_altitude_pilot - vb_calc_alt
						if dis_rest == 0 then
							line4_l = line4_l .. "    "
						else
							vpa = math.deg(math.atan((v_s * 0.3048) / (dis_rest * 1852)))
							if vpa > 9.9 then
								vpa = 9.9
							end
							line4_l = line4_l .. string.format("%3.1f", vpa) .. " "
						end
						-- V/S
						vpa = ((dis_rest * 1852) / simDR_ground_spd) / 60	-- time in mins
						if vpa == 0 then
							line4_l = line4_l .. "    "
						else
							v_s = v_s / vpa
							if v_s > 9999 then
								line4_l = line4_l .. "9999"
							else
								line4_l = line4_l .. string.format("%4d", v_s)
							end
						end
					end
				elseif B738DR_rest_wpt_alt > 0 and B738DR_rest_wpt_alt_idx >= offset and B738DR_rest_wpt_alt_idx <= legs_num then
					if legs_data[B738DR_rest_wpt_alt_idx][11] ~= 0 then
						if legs_data[B738DR_rest_wpt_alt_idx][11] > simDR_altitude_pilot then
							-- V/B V/S
							line4_l = line4_l .. "0.0    0"
							
						else
							
							-- V/B
							lat_tmp, lon_tmp = lat_lon_legs(B738DR_rest_wpt_alt_idx)
							dis_rest = nd_calc_dist2(ndx_lat, ndx_lon, lat_tmp, lon_tmp)
							v_s = simDR_altitude_pilot - legs_data[B738DR_rest_wpt_alt_idx][11]
							if dis_rest == 0 then
								line4_l = line4_l .. "    "
							else
								vpa = math.deg(math.atan((v_s * 0.3048) / (dis_rest * 1852)))
								if vpa > 9.9 then
									vpa = 9.9
								end
								line4_l = line4_l .. string.format("%3.1f", vpa) .. " "
							end
							-- V/S
							vpa = ((dis_rest * 1852) / simDR_ground_spd) / 60	-- time in mins
							if vpa == 0 then
								line4_l = line4_l .. "    "
							else
								v_s = v_s / vpa
								if v_s > 9999 then
									line4_l = line4_l .. "9999"
								else
									line4_l = line4_l .. string.format("%4d", v_s)
								end
							end
						end
					end
				end
			else
				if vb_calc_id ~= "" then
					line3_l = string.sub(line3_l, 1, 13) .. spaces_before(vb_calc_id, 5) .. "/" .. string.format ("%5d", vb_calc_alt)
					
					line4_l = "            "
					
					-- FPA
					if simDR_ground_spd == 0 then
						line4_l = line4_l .. "0.0"
					else
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							vpa = 0
						else
							vpa = (simDR_vvi_fpm_pilot / 60) * 0.3048
							vpa = vpa / simDR_ground_spd
							vpa = -vpa
							if vpa < 0 then
								vpa = 0
							end
							vpa = math.atan(vpa)
							vpa = math.deg(vpa)
						end
						line4_l = line4_l .. string.format("%3.1f", vpa)
					end
					line4_l = line4_l .. " "
					
					if vb_calc_alt > simDR_altitude_pilot then
						-- V/B V/S
						line4_l = line4_l .. "0.0    0"
					else
						-- V/B
						dis_rest = nd_calc_dist2(ndx_lat, ndx_lon, vb_calc_lat, vb_calc_lon)
						v_s = simDR_altitude_pilot - vb_calc_alt
						if dis_rest == 0 then
							line4_l = line4_l .. "    "
						else
							vpa = math.deg(math.atan((v_s * 0.3048) / (dis_rest * 1852)))
							if vpa > 9.9 then
								vpa = 9.9
							end
							line4_l = line4_l .. string.format("%3.1f", vpa) .. " "
						end
						-- V/S
						vpa = ((dis_rest * 1852) / simDR_ground_spd) / 60	-- time in mins
						if vpa == 0 then
							line4_l = line4_l .. "    "
						else
							v_s = v_s / vpa
							if v_s > 9999 then
								line4_l = line4_l .. "9999"
							else
								line4_l = line4_l .. string.format("%4d", v_s)
							end
						end
					end
				end
				line4_x = "            FPA V/B  V/S"
			end
			line5_x = "------------------------"
			
			-- if vnav_desc_spd_disable == 0 then
				if fmc_descent_mode_mod == 0 then
					line5_l = "                  SPEED>"
				elseif fmc_descent_mode_mod == 1 then
					line5_l = "                   PATH>"
				elseif fmc_descent_mode_mod == 2 then
					line5_l = "<ECON             SPEED>"
				else
					line5_l = "<ECON              PATH>"
				end
			-- else
				-- line5_l = "<ECON                   "
			-- end
			
			if phase_des_app == 1 then
				if descent_exec == 0 and des_spd_exec == 0 then
					line6_l = "<FORECAST           RTA>"
				else
					line6_l = "<FORECAST         ERASE>"
				end
			else
				if des_now_enable == 1 then
					if B738DR_fms_descent_now == 0 then
						if descent_exec == 0 and des_spd_exec == 0 then
							line6_l = "<FORECAST       DES NOW>"
						else
							line6_l = "<FORECAST         ERASE>"
						end
					else
						if descent_exec == 0 and des_spd_exec == 0 then
							line6_l = "<FORECAST              >"
							line6_inv = "                DES*NOW"
						else
							line6_l = "<FORECAST         ERASE>"
						end
					end
				else
					if descent_exec == 0 and des_spd_exec == 0 then
						line6_l = "<FORECAST "
					else
						line6_l = "<FORECAST         ERASE>"
					end
				end
			end
		end
	end

end

function B738_fmc_descent_forecast(in_mode)

	local exec_light_in = B738DR_fmc_exec_lights
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
	end
	
	if page_descent_forecast == 1 then
		act_page_buf = 1
		max_page_buf = 1
		if exec_light_in == 0 then
			line0_l = "      DES FORECASTS     "
		else
			line0_l = "      DES FORECASTS     "
			line0_inv = "  MOD "
		end
		line0_s = "                    1/1 "
		line1_x = " TRANS LVL    TAI ON/OFF"
		if exec_light_in == 0 then
			if trans_lvl_mod == "-----" then
				line1_l = "FL" .. string.format("%03d", (B738DR_trans_lvl_mod/100))
			else
				line1_l = trans_lvl_mod
			end
			line1_l = line1_l .. "        "
		else
			if trans_lvl == "-----" or trans_lvl == trans_lvl_mod then
				if trans_lvl_mod == "-----" then
					line1_l = "FL" .. string.format("%03d", (B738DR_trans_lvl_mod/100))
				else
					line1_l = trans_lvl_mod
				end
				line1_l = line1_l .. "        "
			else
				if trans_lvl_mod == "-----" then
					line1_inv = "FL" .. string.format("%03d", (B738DR_trans_lvl_mod/100))
				else
					line1_inv = trans_lvl_mod
				end
				line1_l = "             "
			end
		end
		
		line1_l = line1_l .. tai_on_alt_mod
		line1_l = line1_l .. "/"
		line1_l = line1_l .. tai_off_alt_mod
		
		line2_x = " CABIN RATE  ISA DEV/QNH"
		if cabin_rate_mod == "---" then
			line2_l = "500         "
		else
			line2_l = cabin_rate_mod .. "         "
		end
		
		line3_x = " ALT-----WIND----DIR/SPD"
		
		if exec_light_in == 0 then
			line2_l = line2_l .. forec_isa_dev_mod .. "` /"
			line2_l = line2_l .. forec_qnh_mod
			line2_s = "   FPM          C       "
			
			line3_x = " ALT-----WIND----DIR/SPD"
			line3_l = forec_alt_1_mod .. "         "
			line3_l = line3_l .. forec_dir_1_mod
			line3_l = line3_l .. "`/"
			line3_l = line3_l .. forec_spd_1_mod
			line3_s = "                      KT"
			
			line4_l = forec_alt_2_mod .. "         "
			line4_l = line4_l .. forec_dir_2_mod
			line4_l = line4_l .. "`/"
			line4_l = line4_l .. forec_spd_2_mod
			line4_s = "                      KT"
			
			line5_l = forec_alt_3_mod .. "         "
			line5_l = line5_l .. forec_dir_3_mod
			line5_l = line5_l .. "`/"
			line5_l = line5_l .. forec_spd_3_mod
			line5_s = "                      KT"
			line6_x = "              ----------"
		else
			-- MOD
			if forec_isa_dev == "---" or forec_isa_dev == forec_isa_dev_mod then
				line2_l = line2_l .. forec_isa_dev_mod .. "` /"
				line2_inv = "                  "
			else
				line2_l = line2_l .. "   ` /"
				line2_inv = "            " .. forec_isa_dev_mod .. "   "
			end
			if forec_qnh == "------" or forec_qnh == forec_qnh_mod then
				line2_l = line2_l .. forec_qnh_mod
			else
				line2_inv = line2_inv .. forec_qnh_mod
			end
			line2_s = "   FPM          C       "
			
			--Layer1
			if forec_alt_1 == "-----" or forec_alt_1 == forec_alt_1_mod then
				line3_l = forec_alt_1_mod .. "         "
				line3_inv = "              "
			else
				line3_l = "              "
				line3_inv = forec_alt_1_mod .. "         "
			end
			if (forec_dir_1 == "---" and forec_spd_1 == "---") or (forec_dir_1 == forec_dir_1_mod and forec_spd_1 == forec_spd_1_mod) then
				line3_l = line3_l .. forec_dir_1_mod .. "`/" .. forec_spd_1_mod
			else
				line3_l = line3_l .. "   `/"
				line3_inv = line3_inv .. forec_dir_1_mod .. "  " .. forec_spd_1_mod
			end
			line3_s = "                      KT"
			
			--Layer2
			if forec_alt_2 == "-----"  or forec_alt_2 == forec_alt_2_mod then
				line4_l = forec_alt_2_mod .. "         "
				line4_inv = "              "
			else
				line4_l = "              "
				line4_inv = forec_alt_2_mod .. "         "
			end
			if (forec_dir_2 == "---" and forec_spd_2 == "---") or (forec_dir_2 == forec_dir_2_mod and forec_spd_2 == forec_spd_2_mod) then
				line4_l = line4_l .. forec_dir_2_mod .. "`/" .. forec_spd_2_mod
			else
				line4_l = line4_l .. "   `/"
				line4_inv = line4_inv .. forec_dir_2_mod .. "  " .. forec_spd_2_mod
			end
			line4_s = "                      KT"
			
			--Layer3
			if forec_alt_3 == "-----"  or forec_alt_3 == forec_alt_3_mod then
				line5_l = forec_alt_3_mod .. "         "
				line5_inv = "              "
			else
				line5_l = "              "
				line5_inv = forec_alt_3_mod .. "         "
			end
			if (forec_dir_3 == "---" and forec_spd_3 == "---")  or (forec_dir_3 == forec_dir_3_mod and forec_spd_3 == forec_spd_3_mod) then
				line5_l = line5_l .. forec_dir_3_mod .. "`/" .. forec_spd_3_mod
			else
				line5_l = line5_l .. "   `/"
				line5_inv = line5_inv .. forec_dir_3_mod .. "  " .. forec_spd_3_mod
			end
			line5_s = "                      KT"
		end
		
		line6_x = "              ----------"
		
		if exec_light_in == 0 then
			if rte12_act == 0 then
				if wind_data_des_num == 0 then
					line6_l = "                        "
				else
					line6_l = "<UPLINK DATA            "
				end
			elseif rte12_act == 1 then
				if wind_data_des_num7 == 0 then
					line6_l = "                        "
				else
					line6_l = "<UPLINK DATA            "
				end
			end
		else
			if rte12_act == 0 then
				if wind_data_des_num == 0 then
					line6_l = "                  ERASE>"
				else
					line6_l = "<UPLINK DATA      ERASE>"
				end
			elseif rte12_act == 1 then
				if wind_data_des_num7 == 0 then
					line6_l = "                  ERASE>"
				else
					line6_l = "<UPLINK DATA      ERASE>"
				end
			end
		end
	end

end


function B738_fmc_progress(in_mode)

	local exec_light_in = B738DR_fmc_exec_lights
	if in_mode ~= 0 then
		exec_light_in = B738DR_fmc_exec_lights_fo
	end
	
	local prev_idx = 0
	local temp_str = ""
	local tmp_wpt_eta = ""
	local tmp_wpt_eta2 = 0
	local tmp_wpt_eta3 = 0
	local temp_num = 0
	local temp_num2 = 0
	
	local lat_tmp = 0
	local lon_tmp = 0
	local prog_dist = 0
	
	local use_ref_icao_x = "----"
	local use_des_icao_x = "****"
	local use_rte12_act = rte12_act
	local use_des_icao = "****"
	
	if use_rte12_act < 0 then
		if exec_activate_fpln == 1 then
			use_rte12_act = 0
		elseif exec_activate_fpln2 == 1 then
			use_rte12_act = 1
		end
	end
	
	if use_rte12_act == 0 then
		use_ref_icao_x = ref_icao_x
		use_des_icao_x = des_icao_x
		use_des_icao = des_icao
	elseif use_rte12_act == 1 then
		use_ref_icao_x = ref_icao7_x
		use_des_icao_x = des_icao7_x
		use_des_icao = des_icao7
	end
	
	copy_to_legs_data2_buf(use_rte12_act)
	
	if page_progress == 1 then
		act_page_buf = 1
		max_page_buf = 4
		temp_str = spaces_before(flt_num_x, 8)
		
		line0_l = "  " .. temp_str .. " PROGRESS     "
		line0_s = "                    1/4 "
		
		line1_x = " FROM     ALT  ATA  FUEL"
		
		if legs_num > 1 then
			--PREV WAYPOINT
			if offset > 1 and legs_num > 1 then
				-- nav id
				if offset > legs_num then
					offset = legs_num
					offset_seq = offset
				end
				prev_idx = offset - 1
				
				if legs_data[prev_idx][1] ~= "DISCONTINUITY" then
					
					line1_l = spaces_after(legs_data[prev_idx][1], 8)
					
					if (string.sub(legs_data[prev_idx][1], 1, 2) == "RW" and prev_idx == 2) or legs_data[prev_idx][1] == "ORGN" then
						
						-- calc alt
						if legs_data[prev_idx][11] == 0 then
							line1_l = line1_l .. "      "
						else
							if legs_data[prev_idx][11] > B738DR_trans_alt_mod then
								line1_l = line1_l .. "FL"
								line1_l = line1_l .. string.format("%03d", (legs_data[prev_idx][11]/100))
							else
								line1_l = line1_l .. string.format("%5d", legs_data[prev_idx][11])
							end
							line1_l = line1_l .. " "
						end
						-- calc time
						line1_s = "                  Z    "
						line1_l = line1_l .. "        ."
					else
						-- calc alt
						if legs_data[prev_idx][11] == 0 then
							line1_l = line1_l .. "      "
						else
							if legs_data[prev_idx][11] > B738DR_trans_alt_mod then
								line1_l = line1_l .. "FL"
								line1_l = line1_l .. string.format("%03d", (legs_data[prev_idx][11]/100))
							else
								line1_l = line1_l .. string.format("%5d", legs_data[prev_idx][11])
							end
							line1_l = line1_l .. " "
						end
						-- calc time
						if legs_data[prev_idx][13] == 0 then
							tmp_wpt_eta = "    "
							line1_s = ""
						else
							tmp_wpt_eta = time_to_string(legs_data[prev_idx][13])
							line1_s = "                  Z    "
						end
						line1_l = line1_l .. tmp_wpt_eta
						-- fuel qty
						line1_l = line1_l .. "  "
						if legs_data[prev_idx][15] <= 0 then
							tmp_wpt_eta = ""	--"--.-"
						else
							if units == 0 then
								tmp_wpt_eta = string.format("%4.1f", (legs_data[prev_idx][15] / 1000) * KGS_LBS)
							else
								tmp_wpt_eta = string.format("%4.1f", (legs_data[prev_idx][15] / 1000))
							end
						end
						line1_l = line1_l .. tmp_wpt_eta
					end
				end
			end
			
			-- ACTUAL WAYPOINT
			if legs_data[offset][1] ~= "ORGN" then
				
				if legs_data[offset][1] == "DISCONTINUITY" then
					line2_x = "          DTG  ETA  FUEL"
					line2_l = "*****    "
					line2_m = "         "
				else
					line2_x = " " .. string.format("%03d", B738DR_fmc_crs)
					line2_x = line2_x .. "`     DTG  ETA  FUEL"
					
					line2_l = "         "
					line2_m = spaces_after(legs_data[offset][1], 8) .. " "
					
					-- distance
					prog_dist = math.floor(B738DR_fmc_dist * 10 + 0.5) * 0.1
					if prog_dist > 9999 then
						line2_l = line2_l .. "9999"
					elseif prog_dist >= 10 then
						line2_l = line2_l .. string.format("%4d", prog_dist)
					else
						line2_l = line2_l .. string.format("%4.1f", prog_dist)
					end
					line2_l = line2_l .. " "
					-- calc time
					if legs_data[offset][13] == 0 then
						tmp_wpt_eta = "    "
						line2_s = ""
					else
						tmp_wpt_eta = time_to_string(legs_data[offset][13])
						line2_s = "                  Z    "
					end
					line2_l = line2_l .. tmp_wpt_eta .. "  "
					-- fuel qty
					if legs_data[offset][40] > 0 then
						if units == 0 then
							line2_l = line2_l .. string.format("%4.1f", (legs_data[offset][40] / 1000) * KGS_LBS)
						else
							line2_l = line2_l .. string.format("%4.1f", (legs_data[offset][40] / 1000))
						end
					end
				end
				
			end
			
			-- NEXT WAYPOINT
			prev_idx = offset + 1
			if prev_idx <= legs_num then
				if legs_data[prev_idx][1] ~= "DISCONTINUITY" then
					
					line3_l = spaces_after(legs_data[prev_idx][1], 8) .. " "
					
					if legs_data[offset][1] ~= "DISCONTINUITY" then
						tmp_wpt_eta2 = (math.deg(legs_data[prev_idx][18]) + 360) % 360
						if tmp_wpt_eta2 < 0 then
							tmp_wpt_eta2 = tmp_wpt_eta2 + 360
						end
						line3_x = " " .. string.format("%03d", tmp_wpt_eta2) .. "`"
					
						leg_dist = wpt_distance(prev_idx) + B738DR_fmc_dist
						
						prog_dist = math.floor(leg_dist * 10 + 0.5) * 0.1
						if prog_dist > 9999 then
							line3_l = line3_l .. "9999"
						elseif prog_dist >= 10 then
							line3_l = line3_l .. string.format("%4d", prog_dist)
						else
							line3_l = line3_l .. string.format("%4.1f", prog_dist)
						end
					else
						line3_l = line3_l .. "    "
					end
					
					line3_l = line3_l .. " "
					-- calc time
					if legs_data[prev_idx][13] == 0 then
						tmp_wpt_eta = "    "
						line3_s = ""
					else
						tmp_wpt_eta = time_to_string(legs_data[prev_idx][13])
						line3_s = "                  Z    "
					end
					line3_l = line3_l .. tmp_wpt_eta .. "  "
					-- fuel qty
					if legs_data[prev_idx][40] > 0 then
						if units == 0 then
							line3_l = line3_l .. string.format("%4.1f", (legs_data[prev_idx][40] / 1000) * KGS_LBS)
						else
							line3_l = line3_l .. string.format("%4.1f", (legs_data[prev_idx][40] / 1000))
						end
					end
				else
					line3_l = "*****"
				end
			end
		else
			if legs_num2_buf >= 1 then
				line2_x = "          DTG  ETA  FUEL"
			end
		end
		
		if legs_num2_buf >= 1 then
			-- DEST ICAO
			if use_des_icao_x ~= "****" then
				prev_idx = legs_num2_buf + 1
				
				if (use_des_icao_x ~= use_des_icao and use_des_icao ~= "----" and legs_num > 0) or (legs_delete ~= 0 and legs_num > 1) then
					line4_x = " MOD"
					line4_inv = legs_data2_buf[prev_idx][1]
					if string.len(line4_inv) == 3 then
						temp_str = "*     "
					elseif string.len(line4_inv) == 4 then
						temp_str = "     "
					else
						temp_str = "    "
					end
					line4_inv = line4_inv .. temp_str
					-- distance
					
					prog_dist = math.floor(dist_dest_mod * 10 + 0.5) * 0.1
					if prog_dist > 9999 then
						line4_inv = line4_inv .. "9999"
					elseif prog_dist == 0 then
						line4_inv = line4_inv .. "****"
					elseif prog_dist < 10 then
						line4_inv = line4_inv .. "*" .. string.format("%3.1f", prog_dist)
					elseif prog_dist < 100 then
						line4_inv = line4_inv .. "**" .. string.format("%2d", prog_dist)
					elseif prog_dist < 1000 then
						line4_inv = line4_inv .. "*" .. string.format("%3d", prog_dist)
					else
						line4_inv = line4_inv .. string.format("%4d", prog_dist)
					end
					
					
					line4_inv = line4_inv .. " "
					-- calc time
					if legs_data2_buf[prev_idx][13] == 0 then
						tmp_wpt_eta = "    "
						line4_s = ""
					else
						tmp_wpt_eta = time_to_string(legs_data2_buf[prev_idx][13])
						line4_si = "                  Z    "
					end
					line4_inv = line4_inv .. tmp_wpt_eta .. "  "
					-- fuel qty
					if legs_data2_buf[prev_idx][40] > 0 then
						if units == 0 then
							temp_num = math.floor(legs_data2_buf[prev_idx][40] / 100 * KGS_LBS + 0.5) / 10
							if temp_num < 10 then
								line4_inv = line4_inv .. "*" .. string.format("%3.1f", temp_num)
							else
								line4_inv = line4_inv .. string.format("%4.1f", temp_num)
							end
						else
							temp_num = math.floor(legs_data2_buf[prev_idx][40] / 100 + 0.5) / 10
							if temp_num < 10 then
								line4_inv = line4_inv .. "*" .. string.format("%3.1f", temp_num)
							else
								line4_inv = line4_inv .. string.format("%4.1f", temp_num)
							end
						end
					end
				else
					line4_l = legs_data2_buf[prev_idx][1]
					if string.len(line4_l) == 3 then
						temp_str = "      "
					elseif string.len(line4_l) == 4 then
						temp_str = "     "
					else
						temp_str = "    "
					end
					line4_l = line4_l .. temp_str
					-- distance
					if legs_num2_buf == 1 and use_ref_icao_x ~= "----" and use_des_icao_x ~= "****" then
						prog_dist = nd_calc_dist2(legs_data2_buf[1][7], legs_data2_buf[1][8], legs_data2_buf[2][7], legs_data2_buf[2][8])
						prog_dist = math.floor(prog_dist * 10 + 0.5) * 0.1
					else
						prog_dist = math.floor(dist_dest_mod * 10 + 0.5) * 0.1
					end
					if prog_dist > 9999 then
						line4_l = line4_l .. "9999"
					elseif prog_dist >= 10 then
						line4_l = line4_l .. string.format("%4d", prog_dist)
					else
						line4_l = line4_l .. string.format("%4.1f", prog_dist)
					end
					
					line4_l = line4_l .. " "
					-- calc time
					if legs_data2_buf[prev_idx][13] == 0 then
						tmp_wpt_eta = "    "
						line4_s = ""
					else
						tmp_wpt_eta = time_to_string(legs_data2_buf[prev_idx][13])
						line4_s = "                  Z    "
					end
					line4_l = line4_l .. tmp_wpt_eta .. "  "
					-- fuel qty
					if legs_data2_buf[prev_idx][40] > 0 then
						if units == 0 then
							line4_l = line4_l .. string.format("%4.1f", (legs_data2_buf[prev_idx][40] / 1000) * KGS_LBS)
						else
							line4_l = line4_l .. string.format("%4.1f", (legs_data2_buf[prev_idx][40] / 1000))
						end
					end
				end
			end
			
			local crz_alt_num_mod = crz_alt_num2
			if crz_alt_num_mod == 0 then
				crz_alt_num_mod = crz_alt_num
			end
			
			if crz_alt_num_mod > 0 and cost_index ~= "***" and use_ref_icao_x ~= "----" and use_des_icao_x ~= "****" then
				if legs_delete == 0 and legs_num > 1 then
					if offset > 0 then
						if B738DR_flight_phase < 2 and dist_tc > 0 then
							if plan_weight_mod == "---.-" then
								line5_x = " TO T/C         FUEL QTY"
							else
								line5_x = " TO T/C        PLAN FUEL"
							end
							tmp_wpt_eta = time_to_string(time_tc)
							line5_l = tmp_wpt_eta .. " /"
							prog_dist = math.floor(dist_tc * 10 + 0.5) * 0.1
							if prog_dist > 9999 then
								line5_l = line5_l .. "9999"
							elseif prog_dist >= 10 then
								line5_l = line5_l .. string.format("%4d", prog_dist)
							else
								line5_l = line5_l .. string.format("%4.1f", prog_dist)
							end
							line5_l = line5_l .. "         "
							
							line5_s = "    Z     NM            "
						elseif B738DR_flight_phase < 5 then
							if B738DR_vnav_td_dist > 0 then
								if plan_weight_mod == "---.-" then
									line5_x = " TO T/D         FUEL QTY"
								else
									line5_x = " TO T/D        PLAN FUEL"
								end
								tmp_wpt_eta = time_to_string(time_td)
								line5_l = tmp_wpt_eta .. " /"
								
								prog_dist = math.floor(dist_td * 10 + 0.5) * 0.1
								if prog_dist > 9999 then
									line5_l = line5_l .. "9999"
								elseif prog_dist >= 10 then
									line5_l = line5_l .. string.format("%4d", prog_dist)
								else
									line5_l = line5_l .. string.format("%4.1f", prog_dist)
								end
								line5_l = line5_l .. "         "
								
								line5_s = "    Z     NM            "
							elseif td_idx ~= 0 and B738DR_flight_phase == 2 then
								prog_dist = -B738DR_vnav_td_dist
								if prog_dist < 50 then
									if plan_weight_mod == "---.-" then
										line5_x = " TO T/D         FUEL QTY"
									else
										line5_x = " TO T/D        PLAN FUEL"
									end
									tmp_wpt_eta = "    "
									line5_l = tmp_wpt_eta .. " /"
									
									prog_dist = math.floor(prog_dist * 10 + 0.5) * 0.1
									if prog_dist > 9999 then
										line5_l = line5_l .. "9999"
									elseif prog_dist >= 10 then
										line5_l = line5_l .. string.format("%4d", -prog_dist)
									else
										line5_l = line5_l .. string.format("%4.1f", -prog_dist)
									end
									line5_l = line5_l .. "         "
									
									line5_s = "    Z     NM            "
								end
							end
						else
							if offset <= ed_found and dist_ed > 0 then
								if plan_weight_mod == "---.-" then
									line5_x = " TO E/D         FUEL QTY"
								else
									line5_x = " TO E/D        PLAN FUEL"
								end
								tmp_wpt_eta = time_to_string(time_ed)
								line5_l = tmp_wpt_eta .. " /"
								prog_dist = math.floor(dist_ed * 10 + 0.5) * 0.1
								if prog_dist > 9999 then
									line5_l = line5_l .. "9999"
								elseif prog_dist >= 10 then
									line5_l = line5_l .. string.format("%4d", prog_dist)
								else
									line5_l = line5_l .. string.format("%4.1f", prog_dist)
								end
								line5_l = line5_l .. "         "
								
								line5_s = "    Z     NM            "
							end
						end
					end
				elseif legs_num2_buf > 1 and offset_mod > 0 then
					if dist_tc_mod > 0 then
						if plan_weight_mod == "---.-" then
							line5_x = " TO T/C         FUEL QTY"
						else
							line5_x = " TO T/C        PLAN FUEL"
						end
						tmp_wpt_eta = time_to_string(time_tc_mod)
						line5_l = tmp_wpt_eta .. " /"
						prog_dist = math.floor(dist_tc_mod * 10 + 0.5) * 0.1
						if prog_dist > 9999 then
							line5_l = line5_l .. "9999"
						elseif prog_dist >= 10 then
							line5_l = line5_l .. string.format("%4d", prog_dist)
						else
							line5_l = line5_l .. string.format("%4.1f", prog_dist)
						end
						line5_l = line5_l .. "         "
						
						line5_s = "    Z     NM            "
					elseif B738DR_vnav_td_dist_mod > 0 then
						if plan_weight_mod == "---.-" then
							line5_x = " TO T/D         FUEL QTY"
						else
							line5_x = " TO T/D        PLAN FUEL"
						end
						tmp_wpt_eta = time_to_string(time_td_mod)
						line5_l = tmp_wpt_eta .. " /"
						prog_dist = math.floor(dist_td_mod * 10 + 0.5) * 0.1
						if prog_dist > 9999 then
							line5_l = line5_l .. "9999"
						elseif prog_dist >= 10 then
							line5_l = line5_l .. string.format("%4d", prog_dist)
						else
							line5_l = line5_l .. string.format("%4.1f", prog_dist)
						end
						line5_l = line5_l .. "         "
						
						line5_s = "    Z     NM            "
					else
						if offset_mod <= ed_found_mod and dist_ed_mod > 0 then
							if plan_weight_mod == "---.-" then
								line5_x = " TO E/D         FUEL QTY"
							else
								line5_x = " TO E/D        PLAN FUEL"
							end
							tmp_wpt_eta = time_to_string(time_ed_mod)
							line5_l = tmp_wpt_eta .. " /"
							prog_dist = math.floor(dist_ed_mod * 10 + 0.5) * 0.1
							if prog_dist > 9999 then
								line5_l = line5_l .. "9999"
							elseif prog_dist >= 10 then
								line5_l = line5_l .. string.format("%4d", prog_dist)
							else
								line5_l = line5_l .. string.format("%4.1f", prog_dist)
							end
							line5_l = line5_l .. "         "
							
							line5_s = "    Z     NM            "
						end
					end
				end
			end
		else
			line2_x = "          DTG  ETA  FUEL"
		end
		
		-- FUEL QTY
		if line5_l == "" then
			line5_l = "                   "
			if plan_weight_mod == "---.-" then
				line5_x = "                FUEL QTY"
			else
				line5_x = "               PLAN FUEL"
			end
		end
		if plan_weight_mod == "---.-" then
			line5_l = line5_l .. " " .. fuel_weight
		else
			line5_l = line5_l .. plan_weight_mod
		end
		
		-- WIND
		line6_x = " WIND                   "
		if B738DR_irs_nd_allign_12 ~= 0 or simDR_TAS < 51.9859 then
			line6_l = "        "
		else
			if simDR_wind_spd > 0.5 then
				line6_l = string.format("%03d", displ_crs(simDR_wind_hdg)) .. "`/"
				line6_l = line6_l .. string.format("%3d", simDR_wind_spd)
				line6_s = "        KT              "
			else
				line6_l = "        "
			end
		end
		line6_l = line6_l .. "     NAV STATUS>"
		
	elseif page_progress == 2 then
		act_page_buf = 2
		max_page_buf = 4
		temp_str = spaces_before(flt_num_x, 8)
		line0_l = "  " .. temp_str .. " PROGRESS     "
		line0_s = "                    2/4 "
		
		if B738DR_irs_nd_allign_12 == 0 and simDR_TAS >= 51.9859 then
			if head_wind < 0 then
				-- tailwind
				temp_num = -head_wind
				line1_x = " HEADWIND      CROSSWIND"
				line1_l = string.format("%3d", temp_num)
			else
				-- headwind
				line1_x = " TAILWIND      CROSSWIND"
				line1_l = string.format("%3d", head_wind)
			end
			line1_l = line1_l .. "               "
			if cross_wind == 0 then
				temp_num = -cross_wind
				line1_l = line1_l .. " "
				line1_l = line1_l .. string.format("%3d", temp_num)
			elseif cross_wind < 0 then
				temp_num = -cross_wind
				line1_l = line1_l .. "R"
				line1_l = line1_l .. string.format("%3d", temp_num)
			else
				line1_l = line1_l .. "L"
				line1_l = line1_l .. string.format("%3d", cross_wind)
			end
			line1_s = "   KT                 KT"
			
			-- wind
			line2_x = " WIND        SAT/ISA DEV"
			if simDR_wind_spd > 0.5 then
				line2_l = string.format("%03d", simDR_wind_hdg) .. "`/"
				line2_l = line2_l .. string.format("%3d", simDR_wind_spd)
			else
				line2_l = "        "
			end
			line2_l = line2_l .. "     "
		else
			line1_x = " HEADWIND      CROSSWIND"
			line1_l = "---                ---"
			line1_s = "   KT                 KT"
			
			-- wind ISA
			line2_x = " WIND        SAT/ISA DEV"
			line2_l = "---`/---     "
		end
		
		
		-- ISA / DEV
		temp_num = math.min(simDR_altitude_pilot, 37000)
		temp_num = simDR_OAT - B738_rescale(0, 15, 37000, -56.5, temp_num)
		line2_l = line2_l .. string.format("%3d", simDR_OAT) .. "` /"
		line2_l = line2_l .. string.format("%3d", temp_num) .. "` "
		line2_l = line2_l .. "-40` /  0` "
		line2_s = "                 C     C"
		
		line3_x = " XTK ERROR      VERT DEV"
		if legs_num > 0 then
			if B738DR_xtrack == 999 or B738DR_xtrack == -999 then
				line3_l = " --.--            "
			else
				local xtrack_str = string.format("%5.2f", math.abs(B738DR_xtrack))
				if xtrack_str == " 0.00" then
					line3_l = " "
				elseif B738DR_xtrack < 0 then
					line3_l = "L"
				else
					line3_l = "R"
				end
				line3_l = line3_l.. xtrack_str
				
				line3_l = line3_l .. "            "
			end
		else
			line3_l = "                  "
		end
		line3_s = ""
		if B738DR_nd_vert_path == 1 then
			temp_num = -B738DR_vnav_err_pfd
			if temp_num < 0 then
				temp_num = -temp_num
				line3_s = "                      LO"
			else
				line3_s = "                      HI"
			end
			if temp_num > 9999 then
				temp_num = 9999
			end
			line3_l = line3_l .. string.format("%4d", temp_num)
		end
		
		line4_x = " GPS-L TRK           TAS"
		line4_l = "---`T              "
		if B738DR_irs_nd_allign_12 ~= 0 then
			line4_l = line4_l .. "---"
		elseif simDR_TAS < 51.9859 then
			line4_l = line4_l .. "---"
		else
			temp_num = 1.943844 * simDR_TAS
			line4_l = line4_l .. string.format("%3d", temp_num)
		end
		line4_s = "                      KT"
	elseif page_progress == 3 then
		act_page_buf = 3
		max_page_buf = 4
		line0_l = "     RTA PROGRESS       "
		line0_s = "                    3/4 "
		line1_x = " RTA WPT                "
		line1_l = "------                  "
		line5_x = "------------------------"
		line6_l = "<LIMITS                 "
	elseif page_progress == 4 then
		act_page_buf = 4
		max_page_buf = 4
		line0_l = "     RNP PROGRESS       "
		line0_s = "                    4/4 "
		line1_x = " "
		if offset > 0 and offset <= legs_num2_buf then
			local jj = offset
			local ii = 1
			local allign_ok = 0
			local fms_trk_act = 0
			local fms_dist_act = 0
			local offset_temp = offset
			local temp_string = ""
			raw_table('left_line')
			local left_line = {}
			left_line[1] = ""
			raw_table('right_line')
			local right_line = {}
			right_line[1] = ""
			raw_table('left_line_x')
			local left_line_x = {}
			left_line_x[1] = ""
			raw_table('line_s')
			local line_s = {}
			line_s[1] = ""
			raw_table('line_ml')
			local line_ml = {}
			line_ml[1] = ""
			raw_table('line_m')
			local line_m = {}
			line_m[1] = ""
			raw_table('line_in')
			local line_in = {}
			line_in[1] = ""
			local nd_lat = simDR_latitude
			local nd_lon = simDR_longitude
			
			if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
				allign_ok = 1
			end
			
			if legs_data2_buf[jj][1] ~= "DISCONTINUITY" then
				if legs_data2_buf[jj][21] == -1 or legs_data2_buf[jj][21] > 1 then
					-- course
					if legs_intdir_act == 0 then
						if string.sub(legs_data2_buf[jj][31], 1, 1) == "C" or string.sub(legs_data2_buf[jj][31], 1, 1) == "V" then
							fms_trk_act = math.deg(legs_data2_buf[jj][18])
						elseif legs_data2_buf[jj][31] == "TF" or legs_data2_buf[jj][31] == "IF" or legs_data2_buf[jj][31] == "" then
							fms_trk_act = (math.deg(legs_data2_buf[jj][2]) + simDR_mag_variation + 360) % 360
							
						elseif legs_data2_buf[jj][31] == "RF" or legs_data2_buf[jj][31] == "AF" then
							if jj <= legs_num2_buf then
								if string.sub(legs_data2_buf[jj+1][31], 1, 1) == "C" or string.sub(legs_data2_buf[jj+1][31], 1, 1) == "V" then
									fms_trk_act = math.deg(legs_data2_buf[jj+1][18])
								else
									fms_trk_act = (math.deg(legs_data2_buf[jj+1][2]) + simDR_mag_variation + 360) % 360
								end
							else
								fms_trk_act = (math.deg(legs_data2_buf[jj][2]) + simDR_mag_variation + 360) % 360
							end
						elseif legs_data2_buf[jj][31] == "DF" then
							if legs_delete == 0 then
								if jj > 0 and jj < FMC_MAX_WPT and legs_num > 1 then
									fms_trk_act = B738DR_fms_legs_df_crs_2[jj-1]
								else
									-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
									lat_tmp = legs_data2_buf[jj][7]
									lon_tmp = legs_data2_buf[jj][8]
									fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
									fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
								end
							else
								-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
								lat_tmp = legs_data2_buf[jj][7]
								lon_tmp = legs_data2_buf[jj][8]
								fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
								fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
							end
						else
							fms_trk_act = (math.deg(legs_data2_buf[jj][2]) + simDR_mag_variation + 360) % 360
						end
					elseif legs_intdir_act == 2 then
						fms_trk_act = (legs_intdir_trk + simDR_mag_variation + 360) % 360
					else
						if legs_delete == 0 then
							if legs_intdir_crs == -1 then
								-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
								lat_tmp = legs_data2_buf[jj][7]
								lon_tmp = legs_data2_buf[jj][8]
								fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
								fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
							else
								fms_trk_act = legs_intdir_crs
							end
						else
							-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
							lat_tmp = legs_data2_buf[jj][7]
							lon_tmp = legs_data2_buf[jj][8]
							fms_trk_act = nd_calc_brg(nd_lat, nd_lon, lat_tmp, lon_tmp)
							fms_trk_act = (fms_trk_act + simDR_mag_variation + 360) % 360
						end
					end
					fms_trk_act = (fms_trk_act + 360) % 360
					left_line_x[ii] = " " .. string.format("%03d", fms_trk_act)
					left_line_x[ii] = left_line_x[ii] .. "`     "
					
					-- distance
					-- lat_tmp, lon_tmp = lat_lon_legs2(jj)
					lat_tmp = legs_data2_buf[jj][7]
					lon_tmp = legs_data2_buf[jj][8]
					fms_dist_act = nd_calc_dist2(nd_lat, nd_lon, lat_tmp, lon_tmp)
					if fms_dist_act < 10 then
						left_line_x[ii] = left_line_x[ii] .. string.format("%4.1f",fms_dist_act)
					else
						left_line_x[ii] = left_line_x[ii] .. string.format("%4d",fms_dist_act)
					end
					left_line_x[ii] = left_line_x[ii] .. "NM"
					
					-- GP
					if legs_data2_buf[jj][20] ~= 0 and string.sub(legs_data2_buf[jj][1], 1, 2) ~= "RX" 
					and jj > rnav_idx_first_mod_buf and rnav_idx_first_mod_buf ~= 0 then
						left_line_x[ii] = left_line_x[ii] .. " GP" .. string.format("%4.2f", -legs_data2_buf[jj][20]) .. "`"
					end
					-- id
					if legs_data2_buf[jj][52] <= 0 and legs_num > 1 then
						left_line[ii] = "     "
						line_in[ii] = legs_data2_buf[jj][1]
					else
						left_line[ii] = legs_data2_buf[jj][1]
					end
				else
					-- id
					left_line_x[ii] = " HOLD "
					if legs_data2_buf[jj][21] == 0 then
						left_line_x[ii] = left_line_x[ii] .. "L"
					else
						left_line_x[ii] = left_line_x[ii] .. "R"
					end
				end
				
				-- speed
				if legs_data2_buf[jj][4] == 0 then
					if legs_data2_buf[jj][10] == 0 then
						right_line[ii] = "----"
						if legs_data2_buf[jj][6] == 41 then	--between
							line_s[ii] = "           "
						else
							line_s[ii] = "                 "
						end
						line_m[ii] = "    "
					else
						if fms_recalc == 0 then
							if legs_data2_buf[jj][10] < 1 then
								temp_string = string.format("%5.3f",legs_data2_buf[jj][10])
								temp_string = string.sub(temp_string, -4, -1)
							else
								temp_string = string.format("%4d",legs_data2_buf[jj][10])
							end
						else
							temp_string = "----"
						end
						if legs_data2_buf[jj][6] == 41 then	--between
							line_s[ii] = "       " .. temp_string
						else
							line_s[ii] = "             " .. temp_string
						end
						right_line[ii] = "    "
						line_m[ii] = "    "
					end
				else
					if legs_data2_buf[jj][4] < 1 then
						temp_string = string.format("%5.3f",legs_data2_buf[jj][4])
						temp_string = string.sub(temp_string, -4, -1)
					else
						temp_string = string.format("%3d",legs_data2_buf[jj][4])
					end
					if jj ~= B738DR_rest_wpt_spd_idx then
						if legs_data2_buf[jj][42] == 0 then
							right_line[ii] = temp_string .. " "
						elseif legs_data2_buf[jj][42] == 1 then
							right_line[ii] = temp_string .. "B"
						else
							right_line[ii] = temp_string .. "A"
						end
						line_s[ii] = "                 "
						line_ml[ii] = "    "
					else
						right_line[ii] = "    "
						line_s[ii] = "                 "
						if legs_data2_buf[jj][42] == 0 then
							line_m[ii] = temp_string .. " "
						elseif legs_data2_buf[jj][42] == 1 then
							line_m[ii] = temp_string .. "B"
						else
							line_m[ii] = temp_string .. "A"
						end
					end
				end
				
				-- altitude
				if legs_data2_buf[jj][5] == 0 then
					if legs_data2_buf[jj][11] == 0 then
						right_line[ii] = right_line[ii] .. "/----- "
						line_m[ii] = line_m[ii] .. "       "
					else
						if fms_recalc == 0 then
							if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
								temp_string = " " .. fl_format(legs_data2_buf[jj][11], B738DR_trans_alt_mod)
							else
								temp_string = " " .. fl_format(legs_data2_buf[jj][11], B738DR_trans_lvl_mod)
							end
						else
							temp_string = " -----"
						end
						line_s[ii] = line_s[ii] .. temp_string
						right_line[ii] = right_line[ii] .. "/      "
						line_m[ii] = line_m[ii] .. "       "
					end
				else
					if jj ~= B738DR_rest_wpt_alt_idx then
						if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
							if legs_data2_buf[jj][6] == 41 then	--between
								temp_string = fl_format(legs_data2_buf[jj][41], B738DR_trans_alt_mod) .. "B"
								temp_string = temp_string .. fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. "A"
							else
								temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
							end
							right_line[ii] = right_line[ii] .. "/" .. temp_string
							if legs_data2_buf[jj][6] == 43 then
								right_line[ii] = right_line[ii] .. "A"
							elseif legs_data2_buf[jj][6] == 45 then
								right_line[ii] = right_line[ii] .. "B"
							elseif legs_data2_buf[jj][6] ~= 41 then
								right_line[ii] = right_line[ii] .. " "
							end
						else
							if legs_data2_buf[jj][6] == 41 then	--between
								temp_string = fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod) .. "B"
								temp_string = temp_string .. fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. "A"
							else
								temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
							end
							right_line[ii] = right_line[ii] .. "/" .. temp_string
							if legs_data2_buf[jj][6] == 43 then
								right_line[ii] = right_line[ii] .. "A"
							elseif legs_data2_buf[jj][6] == 45 then
								right_line[ii] = right_line[ii] .. "B"
							elseif legs_data2_buf[jj][6] ~= 41 then
								right_line[ii] = right_line[ii] .. " "
							end
						end
						if jj == B738DR_rest_wpt_spd_idx then
							if legs_data2_buf[jj][6] == 41 then	--between
								line_m[ii] = line_m[ii] .. "             "
							else
								line_m[ii] = line_m[ii] .. "       "
							end
						end
					else
						if legs_data2_buf[jj][19] == 1 or legs_data2_buf[jj][19] == 3 or jj < td_idx then
							if legs_data2_buf[jj][6] == 41 then	--between
								temp_string = fl_format(legs_data2_buf[jj][41], B738DR_trans_alt_mod) .. "B"
								temp_string = temp_string .. fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod) .. "A"
							else
								temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_alt_mod)
							end
							line_m[ii] = line_m[ii] .. " " .. temp_string
							if legs_data2_buf[jj][6] == 43 then
								line_m[ii] = line_m[ii] .. "A"
							elseif legs_data2_buf[jj][6] == 45 then
								line_m[ii] = line_m[ii] .. "B"
							elseif legs_data2_buf[jj][6] ~= 41 then
								line_m[ii] = line_m[ii] .. " "
							end
						else
							if legs_data2_buf[jj][6] == 41 then	--between
								temp_string = fl_format(legs_data2_buf[jj][41], B738DR_trans_lvl_mod) .. "B"
								temp_string = temp_string .. fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod) .. "A"
							else
								temp_string = fl_format(legs_data2_buf[jj][5], B738DR_trans_lvl_mod)
							end
							line_m[ii] = line_m[ii] .. " " .. temp_string
							if legs_data2_buf[jj][6] == 43 then
								line_m[ii] = line_m[ii] .. "A"
							elseif legs_data2_buf[jj][6] == 45 then
								line_m[ii] = line_m[ii] .. "B"
							elseif legs_data2_buf[jj][6] ~= 41 then
								line_m[ii] = line_m[ii] .. " "
							end
						end
						if legs_data2_buf[jj][6] == 41 then	--between
							right_line[ii] = right_line[ii] .. "/            "
						else
							right_line[ii] = right_line[ii] .. "/      "
						end
					end
				end
				
				-- display engine
				left_line[ii] = spaces_after(left_line[ii], 7)
				right_line[ii] = spaces_before(right_line[ii], 17)
				left_line[ii] = left_line[ii] .. right_line[ii]
				line_ml[ii] = spaces_after(line_ml[ii], 7)
				line_m[ii] = spaces_before(line_m[ii], 17)
				
				line1_l = left_line[1] .. right_line[1]
				line1_x = left_line_x[1]
				line1_s = line_s[1]
				line1_m = line_ml[1] .. line_m[1]
				line1_inv = line_in[1]
				
			end
		end
		
		-- RNP
		if legs_num > 0 then
			line2_x = "RNP/ACTUAL  VERT RNP/ANP"
			if B738DR_rnp < 10 then
				line2_s = string.format("%4.2f", B738DR_rnp) .. "/"
			else
				line2_s = string.format("%4.1f", B738DR_rnp) .. "/"
			end
			if B738DR_anp < 9.995 then
				line2_s = line2_s .. string.format("%4.2f", B738DR_anp) .. "NM     "
			else
				line2_s = line2_s .. "-.--NM     "
			end
			if vrnp == "" then
				line2_s = line2_s .. string.format("%3d", vrnp_num) .. "/60FT"
			else
				line2_l = "                " .. string.format("%3d", vrnp_num)
				line2_s = line2_s .. "   /60FT"
			end
		end
		
		-- XTK ERROR
		if legs_num > 0 then
			line3_x = " XTK ERROR      VERT DEV"
			if B738DR_xtrack == 999 or B738DR_xtrack == -999 then
				line3_l = " --.--            "
			else
				local xtrack_str = string.format("%5.2f", math.abs(B738DR_xtrack))
				if xtrack_str == " 0.00" then
					line3_l = " "
				elseif B738DR_xtrack < 0 then
					line3_l = "L"
				else
					line3_l = "R"
				end
				line3_l = line3_l.. xtrack_str
			end
			line3_s = "      NM"
		end
		
		line6_x = " RNP--APPROACH--VERT RNP"
		if app_rnp == "" then
			line6_s = "0.30NM             "
			line6_l = "                   "
		else
			line6_s = "    NM             "
			line6_l = app_rnp .. "                 "
		end
		if vrnp == "" then
			line6_s = line6_s .. "400FT"
		else
			line6_s = line6_s .. "   FT"
			line6_l = line6_l .. string.format("%3d", vrnp_num)
		end
	end
--progresssk
end

function B738_fmc_acars()
	
	if page_ac == 1 then
		act_page_buf = 1
		max_page_buf = 1
		line0_l = "      ACARS MENU        "
		line0_s = "                    1/1 "
		line1_l = "<ATIS REQ               "
		line6_l = "<RETURN                 "
	end
	
end

function B738_fmc_ac_atis()
	
	if page_ac_atis == 1 then
		act_page_buf = 1
		max_page_buf = 1
		line0_l = "      ATIS REQUEST      "
		line0_s = "                    1/1 "
		line2_x = " AIRPORT                "
		line2_l = ac_atis_icao
		line5_l = "                   SEND"
		if acars_atis_status == 0 and ac_atis_icao ~= "----" then
			line5_l = line5_l .. ">"
		end
		line6_x = "     " .. vhf_in_prog .. "  " .. atis_send_time
		line6_l = "<RETURN            "
		if atis_msg_status ~= 0 and acars_atis_status == 0 and receive_msg == 2 then
			line6_l = line6_l .. "ATIS>"
		end
	end
	
end

function B738_fmc_ac_msg()
	
	local ii = 0
	local jj = 0
	local max_page_msg = 0
	
	if page_ac_msg == 1 then
		
		
		jj = math.floor(atis_msg_status / 5)
		max_page_msg = jj + 1
		if act_page_buf > max_page_msg then
			act_page_buf = max_page_msg
			if act_page_buf == 0 then
				act_page_buf = 1
			end
		end
		line0_l = "      ATIS UPLINK       "
		line0_s = "                    "
		line0_s = line0_s .. string.format("%1d", act_page_buf) .. "/" 
		line0_s = line0_s .. string.format("%1d", max_page_msg)
		for ii = 1, 5 do
			jj = ((act_page_buf - 1) * 5) + ii
			if jj <= atis_msg_status then
				if ii == 1 then
					line1_l = atis_message[jj]
				elseif ii == 2 then
					line2_l = atis_message[jj]
				elseif ii == 3 then
					line3_l = atis_message[jj]
				elseif ii == 4 then
					line4_l = atis_message[jj]
				elseif ii == 5 then
					line5_l = atis_message[jj]
				end
			end
		end
		line6_x = "                        "
		line6_l = "<RETURN                 "
		line6_s = "                        "
		
		max_page_buf = max_page_msg
	
	end
	
end

function B738_read_atis()
	
	local ii = 0
	local jj = 0
	local kk = 0
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	local apt_line = ""
	raw_table('apt_word')
	local apt_word = {}
	local line_buf = 0
	local pom = 0

	if acars_atis_status == 1 then
		atis_time = ""
		atis_message = {}
		atis_msg_status = 0
		atis_tick = 0
		atis_validtime = 0
		receive_msg = 0
		if B738DR_com3_data == 0 then
			if is_timer_scheduled(no_comm) == false then
				run_after_time(no_comm, 0.9)
			end
			acars_atis_status = 6
			atis_msg_status = 1
		else
			if is_timer_scheduled(send_time) == false then
				run_after_time(send_time, 0.9)
			end
			if simDR_int_ver < 120000 then
				file_atis = io.open("METAR.rwx", "r")
				
				if file_atis == nil then
					acars_atis_status = 5
					atis_msg_status = 1
					atis_message[atis_msg_status] = "NO METAR DATA"
				else
					acars_atis_status = 2
				end
				if is_timer_scheduled(send_timer) == false then
					run_after_time(send_timer, 4.9)
				end
				receive_msg = 1
			else
				acars_atis_status = 3
				receive_msg = 1
				if is_timer_scheduled(send_timer) == false then
					run_after_time(send_timer, 4.9)
				end
			end
		end
	elseif acars_atis_status == 2 then
		local atis_timestamp = 0
		apt_line = file_atis:read()
		if apt_line and atis_validtime ~= 2 then
			while apt_line do
				atis_tick = atis_tick + 1
				ii = 0
				jj = 0
				line_trim = ""
				line_char = ""
				line_lenght = string.len(apt_line)
				if line_lenght > 0 then
					
					if line_lenght == 16 then
						if string.sub(apt_line, 5, 5) == "/" and string.sub(apt_line, 14, 14) == ":" then
							atis_timestamp = 1
							-- Time
							atis_time = apt_line
						end
					end
					
					-- if line_lenght == 16 then
						-- -- Time
						-- atis_time = apt_line
					-- else
					if atis_timestamp == 0 and line_lenght > 14 then
						-- Data
						for kk = 1, line_lenght do
							line_char = string.sub(apt_line, kk, kk)
							if line_char == " " then
								if ii == 1 then
									jj = jj + 1
									apt_word[jj] = line_trim
									ii = 0
									line_trim = ""
								end
							else
								line_trim = line_trim .. line_char
								ii = 1
							end
						end
						if string.len(line_trim) > 0 then
							jj = jj + 1
							apt_word[jj] = line_trim
						end
						
						if jj > 0 then
							if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
								apt_word[jj] = string.sub(apt_word[jj], 1, -2)
							end
							
							if ac_atis_icao == apt_word[1] then
								if atis_time == "" then
									atis_validtime = 1	--2
								else
									atis_validtime = 1
								end
								
								if atis_validtime ~= 0 then
									atis_msg_status = 1
									atis_message[atis_msg_status] = atis_time
									atis_msg_status = atis_msg_status + 1
									atis_message[atis_msg_status] = ""
									for kk = 1, jj do
										pom = string.len(apt_word[kk])
										if (line_buf + pom) < 24 then
											atis_message[atis_msg_status] = atis_message[atis_msg_status] .. apt_word[kk] .. " "
											line_buf = line_buf + pom + 1
										else
											atis_msg_status = atis_msg_status + 1
											atis_message[atis_msg_status] = apt_word[kk] .. " "
											line_buf = pom + 1
										end
									end
								end
							end
							
							if atis_validtime == 2 then
								atis_tick = 0
								break
							end
							atis_time = ""
							atis_validtime = 0
						end
					end
					atis_timestamp = 0
				end
				if atis_tick > 100 then
				--if atis_tick > 200 then
					atis_tick = 0
					break
				end
				apt_line = file_atis:read()
			end
		else
			file_atis:close()
			acars_atis_status = 5
			if atis_msg_status == 0 then
				acars_atis_status = 3
			end
		end
	elseif acars_atis_status == 3 then
		B738DR_metar_icao = ac_atis_icao
		B738DR_metar_cmd = 1
		B738DR_metar_data = ""
		acars_atis_status = 4
	elseif acars_atis_status == 4 then
		if B738DR_metar_cmd == 1 then
			acars_atis_status = 5
			B738DR_metar_cmd = 0
			atis_msg_status = 1
			atis_message[atis_msg_status] = "NO METAR DATA"
		elseif B738DR_metar_cmd == 3 then
			apt_line = B738DR_metar_data
			ii = 0
			jj = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(apt_line)
			
			local min_lenght = 12
			if simDR_int_ver < 120000 then
				min_lenght = 7
			end
			
			if line_lenght > min_lenght then
				-- Time
				if simDR_int_ver < 120000 then
					atis_time = string.sub(apt_line, 1, 7)
				else
					atis_time = string.sub(apt_line, 6, 12)
				end
				-- Data
				if simDR_int_ver < 120000 then
					for kk = 1, line_lenght do
						line_char = string.sub(apt_line, kk, kk)
						if line_char == " " then
							if ii == 1 then
								jj = jj + 1
								apt_word[jj] = line_trim
								ii = 0
								line_trim = ""
							end
						else
							line_trim = line_trim .. line_char
							ii = 1
						end
					end
				else
					for kk = 14, line_lenght do
						line_char = string.sub(apt_line, kk, kk)
						if line_char == " " then
							if ii == 1 then
								jj = jj + 1
								apt_word[jj] = line_trim
								ii = 0
								line_trim = ""
							end
						else
							line_trim = line_trim .. line_char
							ii = 1
						end
					end
				end
				if string.len(line_trim) > 0 then
					jj = jj + 1
					apt_word[jj] = line_trim
				end
				
				if jj > 1 then
					if string.len(apt_word[jj]) > 1 and string.byte(apt_word[jj], -1) == 13 then	-- CR
						apt_word[jj] = string.sub(apt_word[jj], 1, -2)
					end
					
					if simDR_int_ver < 120000 then
						atis_time = apt_word[1]
					end
					atis_msg_status = 1
					atis_message[atis_msg_status] = atis_time
					atis_msg_status = atis_msg_status + 1
					if simDR_int_ver < 120000 then
						apt_word[1] = ac_atis_icao
						atis_message[atis_msg_status] = ""
					else
						atis_message[atis_msg_status] = ac_atis_icao .. " "
					end
					for kk = 1, jj do
						pom = string.len(apt_word[kk])
						if (line_buf + pom) < 24 then
							atis_message[atis_msg_status] = atis_message[atis_msg_status] .. apt_word[kk] .. " "
							line_buf = line_buf + pom + 1
						else
							atis_msg_status = atis_msg_status + 1
							atis_message[atis_msg_status] = apt_word[kk] .. " "
							line_buf = pom + 1
						end
					end
				end
			end
			B738DR_metar_cmd = 0
			B738DR_metar_data = ""
			acars_atis_status = 5
			if atis_msg_status == 0 then
				atis_msg_status = 1
				atis_message[atis_msg_status] = "NO METAR DATA"
			end
		end
	elseif acars_atis_status == 6 then
		if B738DR_com3_data ~= 0 then
			rst_vhf_prog()
			atis_send_time = "     "
			acars_atis_status = 0
		elseif vhf_in_prog == "   NO COMM  " then
			atis_send_time = "     "
		end
	else
		if acars_atis_status == 0 then
			if B738DR_com3_data == 0 then
				if vhf_in_prog ~= "   NO COMM  " then
					vhf_in_prog = "VHF VOICE MODE"
				end
				atis_send_time = "     "
				if is_timer_scheduled(rst_vhf_prog) == true then
					stop_timer(rst_vhf_prog)
				end
			elseif vhf_in_prog == "VHF VOICE MODE" then
				rst_vhf_prog()
			elseif vhf_in_prog == " VHF IN PROG" then
				if is_timer_scheduled(rst_vhf_prog) == false then
					run_after_time(rst_vhf_prog, 1.5)
				end
			end
		else
			if B738DR_com3_data == 0 then
				vhf_in_prog = "   NO COMM  "
				atis_send_time = "     "
				atis_msg_status = 0
				receive_msg = 0
				acars_atis_status = 0
			elseif receive_msg ~= 0 then
				acars_atis_status = 0
			else
				atis_msg_status = 0
				receive_msg = 0
				acars_atis_status = 0
			end
		end
	end
end

function rst_vhf_prog()
	vhf_in_prog = "            "
end

function send_time()
	atis_send_time = string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
end

function send_timer()
	receive_msg = 2
end

function no_comm()
	vhf_in_prog = "   NO COMM  "
end


function fmc_atc()
	
	max_page_buf = 2
	line0_l = "   ATC LOGON/STATUS     "
	if page_atc == 1 then
		line0_s = "                    1/2 "
		line1_x = " LOGON TO               "
		line1_l = "****                    "
		line2_x = " FLT NO                 "
		line2_l = "********                "
		line3_x = " TAIL NO                "
		line3_l = "*******                 "
	else
		line0_s = "                    2/2 "
		line2_x = " ADS(ACT)     ADS EMERG "
		line2_l = "<SELECT OFF  SELECT ON> "
	end
	
	line6_x = "-------------- DATA LINK"
	if B738DR_com3_data == 0 then
		datalink_status = 2
	else
		datalink_status = 4
	end
	
	line6_l = "<ATC INDEX   " .. datalink_status_txt[datalink_status + 1]

	-- DATALINK status:
-- 0 - NO COMM
-- 1 - READY
-- 2 - VOICE
-- 3 - FAIL
-- 4 - NO ATC COMM
end

function fmc_fmc_comm()
	if page_fmc_comm == 1 then
		line0_l = "        FMC COMM        "
		line0_s = "                    1/1 "
		line1_x = " FLT PLAN               "
		line1_l = "<REQUEST                "
		line2_x = " WINDS                  "
		line2_l = "<REQUEST                "
	end
end

--*** DLINK ***

-- page_ac_msg
-- page_dl_menu = 0
-- page_dl_acars = 0
-- page_dl_requests = 0
-- page_dl_wxr_req = 0
-- page_dl_wxr_info = 0
-- page_dl_fltlog = 0
-- page_dl_sensors = 0

function dl_menu()
	max_page_buf = 1
	line0_l = " DLNK-APPLICATION MENU  "
	line2_l = "<CPDLC              AOC>"
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
	line6_x = "     " .. vhf_in_prog
end

function dl_acars()
	max_page_buf = 1
	line0_l = " ACARS-AOC MENU         "
	line1_l = "<PREFLIGHT      FLT LOG>"
	line2_l = "<ENROUTE        ATS LOG>"
	line3_l = "<POSTFLIGHT     REPORTS>"
	line4_l = "<CLOCK SET     REQUESTS>"
	line5_l = "<TECH MENU    MISC MENU>"
	line6_l = "<RETURN                 "
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
	line6_x = "     " .. vhf_in_prog
end

function dl_requests()
	max_page_buf = 1
	line0_l = " ACARS-REQUESTS         "
	line1_l = "<WEATHER REQ            "
	-- if atis_msg_status == 0 then
	if atis_msg_status ~= 0 and acars_atis_status == 0 and receive_msg == 2 then
		line6_l = "<RETURN         MESSAGE>"
		line6_x = "     " .. vhf_in_prog .. "  " .. atis_send_time
	else
		line6_l = "<RETURN                 "
		line6_x = "     " .. vhf_in_prog
	end
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
end

function dl_wxr_req()
	max_page_buf = 1
	line0_l = " ACARS-WEATHER REQ      "
	line1_x = "HOURLY WX          STA 1"
	if ac_atis_icao == "----" then
		line1_l = "<WEATHER TYPE     (    )"
		line5_l = "                   SEND "
	else
		line1_l = "<WEATHER TYPE      " .. ac_atis_icao
		line5_l = "                   SEND>"
	end
	line6_l = "<RETURN                 "
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
	line6_x = "     " .. vhf_in_prog .. "  " .. atis_send_time
end

function dl_wxr_info()
	
	local ii = 0
	local jj = 0
	local max_page_msg = 0
	
	jj = math.floor(atis_msg_status / 5)
	max_page_msg = jj + 1
	if act_page_buf > max_page_msg then
		act_page_buf = max_page_msg
		if act_page_buf == 0 then
			act_page_buf = 1
		end
	end
	line0_l = "      WXR INFO          "
	line0_s = "                    "
	line0_s = line0_s .. string.format("%1d", act_page_buf) .. "/" 
	line0_s = line0_s .. string.format("%1d", max_page_msg)
	for ii = 1, 5 do
		jj = ((act_page_buf - 1) * 5) + ii
		if jj <= atis_msg_status then
			if ii == 1 then
				line1_l = atis_message[jj]
			elseif ii == 2 then
				line2_l = atis_message[jj]
			elseif ii == 3 then
				line3_l = atis_message[jj]
			elseif ii == 4 then
				line4_l = atis_message[jj]
			elseif ii == 5 then
				line5_l = atis_message[jj]
			end
		end
	end
	line6_l = "<RETURN                 "
	
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
	
	max_page_buf = max_page_msg
	
end

function dl_fltlog()
	
	local dl_ref_tmp = "----"
	local dl_des_tmp = "****"
	local dl_flt_num_tmp = "--------"
	local dl_day_tmp = 0
	local dl_out_hrs_tmp = 0
	local dl_out_min_tmp = 0
	local dl_out_fuel_tmp = -1
	local dl_in_hrs_tmp = 0
	local dl_in_min_tmp = 0
	local dl_in_fuel_tmp = -1
	local dl_on_hrs_tmp = 0
	local dl_on_min_tmp = 0
	local dl_on_fuel_tmp = -1
	local dl_off_hrs_tmp = 0
	local dl_off_min_tmp = 0
	local dl_off_fuel_tmp = -1
	local dl_flight_time_tmp = -1
	local dl_block_time_tmp = -1
	
	line0_s = "                     " .. string.format("%1d", act_page_buf) .. "/5"
	
	max_page_buf = 5
	if act_page_buf > max_page_buf then
		act_page_buf = max_page_buf
	elseif act_page_buf < 0 then
		act_page_buf = 1
	end
	if act_page_buf == 1 then
		line0_l = " ACARS-FLT LOG-CURR     "
		dl_ref_tmp = dl_ref
		dl_des_tmp = dl_des
		dl_flt_num_tmp = dl_flt_num
		dl_day_tmp = dl_day
		dl_out_hrs_tmp = dl_out_hrs
		dl_out_min_tmp = dl_out_min
		dl_out_fuel_tmp = dl_out_fuel
		dl_in_hrs_tmp = dl_in_hrs
		dl_in_min_tmp = dl_in_min
		dl_in_fuel_tmp = dl_in_fuel
		dl_on_hrs_tmp = dl_on_hrs
		dl_on_min_tmp = dl_on_min
		dl_on_fuel_tmp = dl_on_fuel
		dl_off_hrs_tmp = dl_off_hrs
		dl_off_min_tmp = dl_off_min
		dl_off_fuel_tmp = dl_off_fuel
		dl_flight_time_tmp = dl_flight_time
		dl_block_time_tmp = dl_block_time
	elseif act_page_buf > 1 and act_page_buf < 6 then
		line0_l = " ACARS-FLT LOG-PREV     "
		dl_ref_tmp = dl_flights_ref[act_page_buf-1]
		dl_des_tmp = dl_flights_des[act_page_buf-1]
		dl_flt_num_tmp = dl_flights_flt_num[act_page_buf-1]
		dl_day_tmp = dl_flights_day[act_page_buf-1]
		dl_out_hrs_tmp = dl_flights_out_hrs[act_page_buf-1]
		dl_out_min_tmp = dl_flights_out_min[act_page_buf-1]
		dl_out_fuel_tmp = dl_flights_out_fuel[act_page_buf-1]
		dl_in_hrs_tmp = dl_flights_in_hrs[act_page_buf-1]
		dl_in_min_tmp = dl_flights_in_min[act_page_buf-1]
		dl_in_fuel_tmp = dl_flights_in_fuel[act_page_buf-1]
		dl_on_hrs_tmp = dl_flights_on_hrs[act_page_buf-1]
		dl_on_min_tmp = dl_flights_on_min[act_page_buf-1]
		dl_on_fuel_tmp = dl_flights_on_fuel[act_page_buf-1]
		dl_off_hrs_tmp = dl_flights_off_hrs[act_page_buf-1]
		dl_off_min_tmp = dl_flights_off_min[act_page_buf-1]
		dl_off_fuel_tmp = dl_flights_off_fuel[act_page_buf-1]
		dl_flight_time_tmp = dl_flights_flight_time[act_page_buf-1]
		dl_block_time_tmp = dl_flights_block_time[act_page_buf-1]
	end
	
	line1_x = " FLT NUM-DATE ORIG-DEST "
	
	line2_x = "         TIME    FOB    "
	
	local dl_phase = 0
	if dl_des_tmp == "****" then
		line2_s = "   OUT -"
		line3_x = "   OFF -"
		line3_s = "    ON -"
		line4_x = "    IN -"
	else
		if dl_flt_num_tmp == "--------" then
			line1_s = "           "
		else
			line1_s = spaces_before(dl_flt_num_tmp, 8) .. "-" .. string.format("%02d", dl_day_tmp)
		end
		line1_s = line1_s .. "   "
		
		if dl_ref_tmp == "----" then
			line1_s = line1_s .. "    "
		else
			line1_s = line1_s .. dl_ref_tmp
		end
		line1_s = line1_s .. "-"
		if dl_des_tmp == "****" then
			line1_s = line1_s .. "    "
		else
			line1_s = line1_s .. dl_des_tmp
		end
		
		dl_phase = dl_phase + 1
		
		if dl_out_fuel_tmp < 0 then
			line2_s = "   OUT -"
		else
			if dl_out_hrs_tmp == -1 then
				line2_s = "   OUT -"
			else
				line2_s = "   OUT - " .. string.format("%02d", dl_out_hrs_tmp) .. ":" .. string.format("%02d", dl_out_min_tmp)
				if units == 0 then
					line2_s = line2_s .. "  " .. string.format("%05.1f", dl_out_fuel_tmp * KGS_LBS)
				else
					line2_s = line2_s .. "  " .. string.format("%05.1f", dl_out_fuel_tmp)
				end
			end
			dl_phase = dl_phase + 1
		end
		if dl_off_fuel_tmp < 0 then
			line3_x = "   OFF -"
		else
			if dl_off_hrs_tmp == -1 then
				line3_x = "   OFF -"
			else
				line3_x = "   OFF - " .. string.format("%02d", dl_off_hrs_tmp) .. ":" .. string.format("%02d", dl_off_min_tmp)
				if units == 0 then
					line3_x = line3_x .. "  " .. string.format("%05.1f", dl_off_fuel_tmp * KGS_LBS)
				else
					line3_x = line3_x .. "  " .. string.format("%05.1f", dl_off_fuel_tmp)
				end
			end
			dl_phase = dl_phase + 1
		end
		if dl_on_fuel_tmp < 0 then
			line3_s = "    ON -"
		else
			if dl_on_hrs_tmp == -1 then
				line3_s = "    ON -"
			else
				line3_s = "    ON - " .. string.format("%02d", dl_on_hrs_tmp) .. ":" .. string.format("%02d", dl_on_min_tmp)
				if units == 0 then
					line3_s = line3_s .. "  " .. string.format("%05.1f", dl_on_fuel_tmp * KGS_LBS)
				else
					line3_s = line3_s .. "  " .. string.format("%05.1f", dl_on_fuel_tmp)
				end
			end
			dl_phase = dl_phase + 1
		end
		if dl_in_fuel_tmp < 0 then
			line4_x = "    IN -"
		else
			if dl_in_hrs_tmp == -1 then
				line4_x = "    IN -"
			else
				line4_x = "    IN - " .. string.format("%02d", dl_in_hrs_tmp) .. ":" .. string.format("%02d", dl_in_min_tmp)
				if units == 0 then
					line4_x = line4_x .. "  " .. string.format("%05.1f", dl_in_fuel_tmp * KGS_LBS)
				else
					line4_x = line4_x .. "  " .. string.format("%05.1f", dl_in_fuel_tmp)
				end
			end
			dl_phase = dl_phase + 1
		end
	end
	
	line4_s = " FLIGHT --TIMES-- BLOCK "
	
	if act_page_buf == 1 then
		if dl_phase == 1 then
			line5_s = "          BEGIN"
		elseif dl_phase == 2 then
			line5_s = "           OUT"
		elseif dl_phase == 3 then
			line5_s = "           OFF"
		elseif dl_phase == 4 then
			line5_s = "           ON"
		elseif dl_phase == 5 then
			line5_s = "           END"
		end
	end
	
	local time_tmp1 = 0
	local time_tmp2 = 0
	
	if dl_flight_time_tmp < 0 then
		line5_x = "        "
	else
		time_tmp1 = math.floor(dl_flight_time_tmp / 60)
		time_tmp2 = dl_flight_time_tmp - (time_tmp1 * 60)
		line5_x = " " .. string.format("%02d", time_tmp1) .. ":" .. string.format("%02d", time_tmp2) .. "  "
	end
	line5_x = line5_x .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes) .. ":" .. string.format("%02d", simDR_zulu_seconds)
	if dl_block_time_tmp >= 0 then
		time_tmp1 = math.floor(dl_block_time_tmp / 60)
		time_tmp2 = dl_block_time_tmp - (time_tmp1 * 60)
		line5_x = line5_x .. "  " .. string.format("%02d", time_tmp1) .. ":" .. string.format("%02d", time_tmp2)
	end
	line5_l = "                SENSORS>"
	line6_l = "<RETURN                 "
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
	line6_x = "     " .. vhf_in_prog
end

function save_prev_dl_flight()
	for fff = 2, 4 do
		dl_flights_ref[fff] = dl_flights_ref[fff-1]
		dl_flights_des[fff] = dl_flights_des[fff-1]
		dl_flights_flt_num[fff] = dl_flights_flt_num[fff-1]
		dl_flights_day[fff] = dl_flights_day[fff-1]
		dl_flights_out_hrs[fff] = dl_flights_out_hrs[fff-1]
		dl_flights_out_min[fff] = dl_flights_out_min[fff-1]
		dl_flights_out_fuel[fff] = dl_flights_out_fuel[fff-1]
		dl_flights_in_hrs[fff] = dl_flights_in_hrs[fff-1]
		dl_flights_in_min[fff] = dl_flights_in_min[fff-1]
		dl_flights_in_fuel[fff] = dl_flights_in_fuel[fff-1]
		dl_flights_on_hrs[fff] = dl_flights_on_hrs[fff-1]
		dl_flights_on_min[fff] = dl_flights_on_min[fff-1]
		dl_flights_on_fuel[fff] = dl_flights_on_fuel[fff-1]
		dl_flights_off_hrs[fff] = dl_flights_off_hrs[fff-1]
		dl_flights_off_min[fff] = dl_flights_off_min[fff-1]
		dl_flights_off_fuel[fff] = dl_flights_off_fuel[fff-1]
		dl_flights_flight_time[fff] = dl_flights_flight_time[fff-1]
		dl_flights_block_time[fff] = dl_flights_block_time[fff-1]
	end
	dl_flights_ref[1] = dl_ref
	dl_flights_des[1] = dl_des
	dl_flights_flt_num[1] = dl_flt_num
	dl_flights_day[1] = dl_day
	dl_flights_out_hrs[1] = dl_out_hrs
	dl_flights_out_min[1] = dl_out_min
	dl_flights_out_fuel[1] = dl_out_fuel
	dl_flights_in_hrs[1] = dl_in_hrs
	dl_flights_in_min[1] = dl_in_min
	dl_flights_in_fuel[1] = dl_in_fuel
	dl_flights_on_hrs[1] = dl_on_hrs
	dl_flights_on_min[1] = dl_on_min
	dl_flights_on_fuel[1] = dl_on_fuel
	dl_flights_off_hrs[1] = dl_off_hrs
	dl_flights_off_min[1] = dl_off_min
	dl_flights_off_fuel[1] = dl_off_fuel
	dl_flights_flight_time[1] = dl_flight_time
	dl_flights_block_time[1] = dl_block_time
	
end

function reset_dl_times()
	if dl_block_time >= 0 then
		save_prev_dl_flight()
	end
	dl_ref = "----"
	dl_des = "****"
	dl_flt_num = "--------"
	dl_day = 0
	dl_out_hrs = 0
	dl_out_min = 0
	dl_out_fuel = -1
	dl_in_hrs = 0
	dl_in_min = 0
	dl_in_fuel = -1
	dl_on_hrs = 0
	dl_on_min = 0
	dl_on_fuel = -1
	dl_off_hrs = 0
	dl_off_min = 0
	dl_off_fuel = -1
	dl_flight_time = -1
	dl_block_time = -1
end


function dl_times()
	
	local time_tmp1 = 0
	local time_tmp2 = 0
	
	if dl_des ~= "****" then
		local fuel_weight_num = (simDR_fuel_weight / 1000) --* KGS_LBS
		if dl_out_fuel < 0 then
			if B738DR_parking_brake_pos == 0 and door_ratio_fwd_L == 0 and door_ratio_aft_L == 0 and 
			   door_ratio_fwd_cargo == 0 and door_ratio_aft_cargo == 0  and door_ratio_fwd_R == 0 and door_ratio_aft_R == 0 then
				dl_out_fuel = fuel_weight_num
				dl_out_hrs = simDR_zulu_hours
				dl_out_min = simDR_zulu_minutes
			end
			dl_out_of_gate = 0
		elseif dl_off_fuel < 0 then
			if B738DR_air_ground_sensor == 0 then
				dl_off_fuel = fuel_weight_num
				dl_off_hrs = simDR_zulu_hours
				dl_off_min = simDR_zulu_minutes
			else
				if B738DR_parking_brake_pos ~= 0 and (door_ratio_fwd_L ~= 0 or door_ratio_aft_L ~= 0 or 
				   door_ratio_fwd_cargo ~= 0 or door_ratio_aft_cargo ~= 0 or door_ratio_fwd_R ~= 0 or door_ratio_aft_R ~= 0) 
				   and B738DR_autogate_nearest <= 0.008 and dl_out_of_gate ~= 0 then
					
					-- Flight time
					dl_off_fuel = fuel_weight_num
					dl_off_hrs = -1
					dl_off_min = -1
					
					dl_on_fuel = fuel_weight_num
					dl_on_hrs = -1
					dl_on_min = -1
					dl_flight_time = -1
					
					dl_in_fuel = dl_on_fuel
					dl_in_hrs = simDR_zulu_hours
					dl_in_min = simDR_zulu_minutes
					
					time_tmp1 = dl_in_hrs * 60 + dl_in_min
					time_tmp2 = dl_out_hrs * 60 + dl_out_min
					if time_tmp1 > time_tmp2 then
						dl_block_time = time_tmp1 - time_tmp2
					else
						dl_block_time = time_tmp1 + 1440 - time_tmp2	--in minutes
					end
				elseif B738DR_autogate_nearest > 0.02 then
					dl_out_of_gate = 1
				end
			end
		elseif dl_on_fuel < 0 then
			if B738DR_air_ground_sensor ~= 0 then
				dl_on_fuel = fuel_weight_num
				dl_on_hrs = simDR_zulu_hours
				dl_on_min = simDR_zulu_minutes
				time_tmp1 = dl_on_hrs * 60 + dl_on_min
				time_tmp2 = dl_off_hrs * 60 + dl_off_min
				if time_tmp1 > time_tmp2 then
					dl_flight_time = time_tmp1 - time_tmp2
				else
					dl_flight_time = time_tmp1 + 1440 - time_tmp2	--in minutes
				end
			end
		elseif dl_in_fuel < 0 then
			if B738DR_air_ground_sensor == 0 then
				dl_on_hrs = 0
				dl_on_min = 0
				dl_on_fuel = -1
				dl_flight_time = -1
				dl_block_time = -1
			elseif B738DR_parking_brake_pos ~= 0 and (door_ratio_fwd_L ~= 0 or door_ratio_aft_L ~= 0 or 
			   door_ratio_fwd_cargo ~= 0 or door_ratio_aft_cargo ~= 0 or door_ratio_fwd_R ~= 0 or door_ratio_aft_R ~= 0) then
				dl_in_fuel = fuel_weight_num
				dl_in_hrs = simDR_zulu_hours
				dl_in_min = simDR_zulu_minutes
				time_tmp1 = dl_in_hrs * 60 + dl_in_min
				time_tmp2 = dl_out_hrs * 60 + dl_out_min
				if time_tmp1 > time_tmp2 then
					dl_block_time = time_tmp1 - time_tmp2
				else
					dl_block_time = time_tmp1 + 1440 - time_tmp2	--in minutes
				end
			end
		end
	end
end

function dl_sensors()
	max_page_buf = 1
	line0_l = " ACARS-SENSORS          "
	--SET/RELEASED
	if B738DR_parking_brake_pos == 0 then
		line1_x = "PARK BRAKE      RELEASED"
	else
		line1_x = "PARK BRAKE      SET     "
	end
	
	--AIRBORNE/GROUND
	if B738DR_air_ground_sensor == 0 then
		line1_s = "AIR/GND         AIRBORNE"
	else
		line1_s = "AIR/GND         GROUND  "
	end
	
	--CLOSED/OPEN
	if door_ratio_fwd_L == 0 then
		line2_x = "FWD DOOR        CLOSED  "
	else
		line2_x = "FWD DOOR        OPEN    "
	end
	if door_ratio_aft_L == 0 then
		line2_s = "AFT DOOR        CLOSED  "
	else
		line2_s = "AFT DOOR        OPEN    "
	end
	if door_ratio_fwd_cargo == 0 and door_ratio_aft_cargo == 0 then
		line3_x = "CARGO/EE DOORS  CLOSED  "
	else
		line3_x = "CARGO/EE DOORS  OPEN    "
	end
	if door_ratio_fwd_R == 0 and door_ratio_aft_R == 0 then
		line3_s = "SERVICE DOORS   CLOSED  "
	else
		line3_s = "SERVICE DOORS   OPEN    "
	end
	-- current fuel
	line4_x = "FOB             ".. fuel_weight
	
	line6_l = "<RETURN                 "
	line6_s = "        " .. string.format("%02d", simDR_zulu_hours) .. ":" .. string.format("%02d", simDR_zulu_minutes)
	line6_x = "     " .. vhf_in_prog
end


--*** DLINK ***

function txt_trunc(txt_txt, txt_trunc2)
	
	local result_txt = txt_txt
	if string.len(txt_txt) > txt_trunc2 then
		result_txt = string.sub(txt_txt, 1, txt_trunc2)
	elseif string.len(txt_txt) == 0 then
		result_txt = " "
	end
	
	return result_txt
	
end

function null_fmc_disp()
	line0_l = ""
	line0_s = ""
	line0_m = ""
	line0_c = ""
	line0_g = ""
	line1_x = ""
	line1_l = ""
	line1_s = ""
	line1_m = ""
	line1_g = ""
	line2_x = ""
	line2_l = ""
	line2_s = ""
	line2_m = ""
	line2_g = ""
	line3_x = ""
	line3_l = ""
	line3_s = ""
	line3_m = ""
	line3_g = ""
	line4_x = ""
	line4_l = ""
	line4_s = ""
	line4_m = ""
	line4_g = ""
	line5_x = ""
	line5_l = ""
	line5_s = ""
	line5_m = ""
	line5_g = ""
	line6_x = ""
	line6_l = ""
	line6_s = ""
	line6_m = ""
	line6_g = ""
	line0_inv = ""
	line1_inv = ""
	line2_inv = ""
	line3_inv = ""
	line4_inv = ""
	line5_inv = ""
	line6_inv = ""
	
	line4_si = ""
	line1_lx = ""
	line6_lx = ""
end

function B738_fmc_display()

	if fmc_enable == 0 then
		null_fmc_disp()
	end
	
	if display_update == 1 then
		--display_update = 0

		B738DR_fmc1_Line00_L = txt_trunc(line0_l, 24)
		B738DR_fmc1_Line01_L = txt_trunc(line1_l, 24)
		B738DR_fmc1_Line02_L = txt_trunc(line2_l, 24)
		B738DR_fmc1_Line03_L = txt_trunc(line3_l, 24)
		B738DR_fmc1_Line04_L = txt_trunc(line4_l, 24)
		B738DR_fmc1_Line05_L = txt_trunc(line5_l, 24)
		B738DR_fmc1_Line06_L = txt_trunc(line6_l, 24)
		
		B738DR_fmc1_Line00_S = txt_trunc(line0_s, 24)
		B738DR_fmc1_Line01_S = txt_trunc(line1_s, 24)
		B738DR_fmc1_Line02_S = txt_trunc(line2_s, 24)
		B738DR_fmc1_Line03_S = txt_trunc(line3_s, 24)
		B738DR_fmc1_Line04_S = txt_trunc(line4_s, 24)
		B738DR_fmc1_Line05_S = txt_trunc(line5_s, 24)
		B738DR_fmc1_Line06_S = txt_trunc(line6_s, 24)

		B738DR_fmc1_Line01_X = txt_trunc(line1_x, 24)
		B738DR_fmc1_Line02_X = txt_trunc(line2_x, 24)
		B738DR_fmc1_Line03_X = txt_trunc(line3_x, 24)
		B738DR_fmc1_Line04_X = txt_trunc(line4_x, 24)
		B738DR_fmc1_Line05_X = txt_trunc(line5_x, 24)
		B738DR_fmc1_Line06_X = txt_trunc(line6_x, 24)
		
		B738DR_fmc1_Line00_M = txt_trunc(line0_m, 24)
		B738DR_fmc1_Line01_M = txt_trunc(line1_m, 24)
		B738DR_fmc1_Line02_M = txt_trunc(line2_m, 24)
		B738DR_fmc1_Line03_M = txt_trunc(line3_m, 24)
		B738DR_fmc1_Line04_M = txt_trunc(line4_m, 24)
		B738DR_fmc1_Line05_M = txt_trunc(line5_m, 24)
		B738DR_fmc1_Line06_M = txt_trunc(line6_m, 24)
		
		B738DR_fmc1_Line00_G = txt_trunc(line0_g, 24)
		B738DR_fmc1_Line01_G = txt_trunc(line1_g, 24)
		B738DR_fmc1_Line02_G = txt_trunc(line2_g, 24)
		B738DR_fmc1_Line03_G = txt_trunc(line3_g, 24)
		B738DR_fmc1_Line04_G = txt_trunc(line4_g, 24)
		B738DR_fmc1_Line05_G = txt_trunc(line5_g, 24)
		B738DR_fmc1_Line06_G = txt_trunc(line6_g, 24)
		
		B738DR_fmc1_Line00_I = txt_trunc(line0_inv, 24)
		B738DR_fmc1_Line01_I = txt_trunc(line1_inv, 24)
		B738DR_fmc1_Line02_I = txt_trunc(line2_inv, 24)
		B738DR_fmc1_Line03_I = txt_trunc(line3_inv, 24)
		B738DR_fmc1_Line04_I = txt_trunc(line4_inv, 24)
		B738DR_fmc1_Line05_I = txt_trunc(line5_inv, 24)
		B738DR_fmc1_Line06_I = txt_trunc(line6_inv, 24)
		
		B738DR_fmc1_Line00_C = txt_trunc(line0_c, 24)
		
		B738DR_fmc1_Line04_SI = txt_trunc(line4_si, 24)
		
		B738DR_fmc1_Line01_LX = txt_trunc(line1_lx, 24)
		B738DR_fmc1_Line06_LX = txt_trunc(line6_lx, 24)
		
		if fmc_message_num ~= 0 then
			if fmc_message_num == 1 and fmc_message[1] == REF_ICAO_MSG then
				if string.len(entry) > 1 then
						if string.sub(entry, 1, 1) == ">" then
							B738DR_fmc1_Line_entry = string.sub(entry, 2,-1)
						else
							B738DR_fmc1_Line_entry = entry
						end
				else
					B738DR_fmc1_Line_entry = entry
				end
			else
				B738DR_fmc1_Line_entry = fmc_message[fmc_message_num]
				if fmc_message_num > 1 then
					if fmc_message[fmc_message_num] == fmc_message[fmc_message_num-1] then
						fmc_message_num = fmc_message_num - 1
					end
				end
			end
		else
			if string.len(entry) > 1 then
					if string.sub(entry, 1, 1) == ">" then
						B738DR_fmc1_Line_entry = string.sub(entry, 2,-1)
					else
						B738DR_fmc1_Line_entry = entry
					end
			else
				B738DR_fmc1_Line_entry = entry
			end
		end
		if string.len(B738DR_fmc1_Line_entry) == 0 then
			B738DR_fmc1_Line_entry = " "
		end
		
		null_fmc_disp()
		
	end

end



function B738_fmc_display2()

	if fmc_enable == 0 then
		null_fmc_disp()
	end
	
	if display_update == 1 then
		--display_update = 0
		
		B738DR_fmc2_Line00_L = txt_trunc(line0_l, 24)
		B738DR_fmc2_Line01_L = txt_trunc(line1_l, 24)
		B738DR_fmc2_Line02_L = txt_trunc(line2_l, 24)
		B738DR_fmc2_Line03_L = txt_trunc(line3_l, 24)
		B738DR_fmc2_Line04_L = txt_trunc(line4_l, 24)
		B738DR_fmc2_Line05_L = txt_trunc(line5_l, 24)
		B738DR_fmc2_Line06_L = txt_trunc(line6_l, 24)

		B738DR_fmc2_Line00_S = txt_trunc(line0_s, 24)
		B738DR_fmc2_Line01_S = txt_trunc(line1_s, 24)
		B738DR_fmc2_Line02_S = txt_trunc(line2_s, 24)
		B738DR_fmc2_Line03_S = txt_trunc(line3_s, 24)
		B738DR_fmc2_Line04_S = txt_trunc(line4_s, 24)
		B738DR_fmc2_Line05_S = txt_trunc(line5_s, 24)
		B738DR_fmc2_Line06_S = txt_trunc(line6_s, 24)

		B738DR_fmc2_Line01_X = txt_trunc(line1_x, 24)
		B738DR_fmc2_Line02_X = txt_trunc(line2_x, 24)
		B738DR_fmc2_Line03_X = txt_trunc(line3_x, 24)
		B738DR_fmc2_Line04_X = txt_trunc(line4_x, 24)
		B738DR_fmc2_Line05_X = txt_trunc(line5_x, 24)
		B738DR_fmc2_Line06_X = txt_trunc(line6_x, 24)
		
		B738DR_fmc2_Line00_M = txt_trunc(line0_m, 24)
		B738DR_fmc2_Line01_M = txt_trunc(line1_m, 24)
		B738DR_fmc2_Line02_M = txt_trunc(line2_m, 24)
		B738DR_fmc2_Line03_M = txt_trunc(line3_m, 24)
		B738DR_fmc2_Line04_M = txt_trunc(line4_m, 24)
		B738DR_fmc2_Line05_M = txt_trunc(line5_m, 24)
		B738DR_fmc2_Line06_M = txt_trunc(line6_m, 24)
		
		B738DR_fmc2_Line00_G = txt_trunc(line0_g, 24)
		B738DR_fmc2_Line01_G = txt_trunc(line1_g, 24)
		B738DR_fmc2_Line02_G = txt_trunc(line2_g, 24)
		B738DR_fmc2_Line03_G = txt_trunc(line3_g, 24)
		B738DR_fmc2_Line04_G = txt_trunc(line4_g, 24)
		B738DR_fmc2_Line05_G = txt_trunc(line5_g, 24)
		B738DR_fmc2_Line06_G = txt_trunc(line6_g, 24)
		
		B738DR_fmc2_Line00_I = txt_trunc(line0_inv, 24)
		B738DR_fmc2_Line01_I = txt_trunc(line1_inv, 24)
		B738DR_fmc2_Line02_I = txt_trunc(line2_inv, 24)
		B738DR_fmc2_Line03_I = txt_trunc(line3_inv, 24)
		B738DR_fmc2_Line04_I = txt_trunc(line4_inv, 24)
		B738DR_fmc2_Line05_I = txt_trunc(line5_inv, 24)
		B738DR_fmc2_Line06_I = txt_trunc(line6_inv, 24)
		
		B738DR_fmc2_Line00_C = txt_trunc(line0_c, 24)
		
		B738DR_fmc2_Line04_SI = txt_trunc(line4_si, 24)
		
		B738DR_fmc2_Line01_LX = txt_trunc(line1_lx, 24)
		B738DR_fmc2_Line06_LX = txt_trunc(line6_lx, 24)
		
		if fmc_message_num ~= 0 then
			if fmc_message_num == 1 and fmc_message[1] == REF_ICAO_MSG then
				if string.len(entry2) > 1 then
						if string.sub(entry2, 1, 1) == ">" then
							B738DR_fmc2_Line_entry = string.sub(entry2, 2,-1)
						else
							B738DR_fmc2_Line_entry = entry2
						end
				else
					B738DR_fmc2_Line_entry = entry2
				end
			else
				B738DR_fmc2_Line_entry = fmc_message[fmc_message_num]
				if fmc_message_num > 1 then
					if fmc_message[fmc_message_num] == fmc_message[fmc_message_num-1] then
						fmc_message_num = fmc_message_num - 1
					end
				end
			end
		else
			if string.len(entry2) > 1 then
					if string.sub(entry2, 1, 1) == ">" then
						B738DR_fmc2_Line_entry = string.sub(entry2, 2,-1)
					else
						B738DR_fmc2_Line_entry = entry2
					end
			else
				B738DR_fmc2_Line_entry = entry2
			end
		end
		if string.len(B738DR_fmc2_Line_entry) == 0 then
			B738DR_fmc2_Line_entry = " "
		end
		
		null_fmc_disp()	
	end

end

function B738_fmc_disp_capt()
	
	act_page_buf = act_page
	max_page_buf = max_page
	
	if page_menu > 0 then
		B738_fmc_menu()
	elseif page_pos_init > 0 then
		B738_fmc_pos_init(0)
	elseif page_takeoff > 0 then
		B738_fmc_takeoff()
	elseif page_approach > 0 then
		B738_fmc_approach()
	elseif page_perf > 0 then
		B738_fmc_perf()
	elseif page_n1_limit > 0 then
		B738_fmc_n1_limit()
	elseif page_init > 0 then
		B738_fmc_init()
	elseif page_ident > 0 then
		B738_fmc_ident()
	elseif page_climb > 0 then
		clb_del_spd_idx = 0
		B738_fmc_climb()
	elseif page_cruise > 0 then
		B738_fmc_cruise(0)
	elseif page_descent > 0 then
		des_del_spd_idx = 0
		B738_fmc_descent()
	elseif page_descent_forecast > 0 then
		B738_fmc_descent_forecast(0)
	elseif page_rte_init > 0 then
		B738_fmc_rte_init(0)
	elseif page_legs > 0 then
		if rte12_sel == 0 then
			if legs_step > legs_num2 then
				legs_step = legs_num2
			end
		else
			if legs_step > legs_num8 then
				legs_step = legs_num8
			end
		end
		B738_fmc_legs99(0)
	elseif page_dep_arr > 0 then
		B738_fmc_dep_arr()
	elseif page_dep > 0 then
		B738_fmc_dep99(0)
	elseif page_arr > 0 then
		B738_fmc_arr99(0)
	elseif page_sel_wpt > 0 then
		B738_fmc_sel_wpt()
	elseif page_sel_wpt2 > 0 then
		B738_fmc_sel_wpt2()
	elseif page_sel_wpt3 > 0 then
		B738_fmc_sel_wpt3()
	elseif page_sel_wpt4 > 0 then
		B738_fmc_sel_wpt4()
	elseif page_sel_hold_wpt > 0 then
		B738_fmc_sel_hold_wpt()
	elseif page_progress > 0 then
		B738_fmc_progress(0)
	elseif page_offset > 0 then
		B738_fmc_offset()
	elseif page_hold > 0 then
		B738_fmc_hold(0)
	elseif page_ref_nav_data > 0 then
		B738_fmc_ref_nav_data()
	elseif page_ref_nav_data_wpt > 0 then
		B738_fmc_ref_nav_data_wpt()
	elseif page_ref_nav_data_navaid > 0 then
		B738_fmc_ref_nav_data_navaid()
	elseif page_ref_nav_data_apt > 0 then
		B738_fmc_ref_nav_data_apt()
	elseif page_ref_sel > 0 then
		B738_ref_nav_sel()
	elseif page_fix > 0 then
		B738_fmc_fix()
	elseif page_rte_legs > 0 then
		B738_rte_data(0)
	elseif page_ac > 0 then
		B738_fmc_acars()
	elseif page_ac_atis > 0 then
		B738_fmc_ac_atis()
	elseif page_ac_msg > 0 then
		B738_fmc_ac_msg()
	elseif page_nearest_apt > 0 then
		B738_fmc_nearest_apt()
	elseif page_altern_apt > 0 then
		B738_fmc_altern_apt()
	elseif page_nav_status > 0 then
		B738_fmc_nav_status()
	elseif page_atc > 0 then
		fmc_atc()
	elseif page_fmc_comm > 0 then
		fmc_fmc_comm()
	elseif page_dl_menu > 0 then
		dl_menu()
	elseif page_dl_acars > 0 then
		dl_acars()
	elseif page_dl_requests > 0 then
		dl_requests()
	elseif page_dl_wxr_req > 0 then
		dl_wxr_req()
	elseif page_dl_wxr_info > 0 then
		dl_wxr_info()
	elseif page_dl_fltlog > 0 then
		dl_fltlog()
	elseif page_dl_sensors > 0 then
		dl_sensors()
	elseif page_eo_cruise > 0 then
		fmc_engout_crz()
	elseif page_eo_climb > 0 then
		fmc_engout_clb()
	elseif page_flt_plns > 0 then
		B738_fmc_flt_plns(0)
	end
	
	act_page = act_page_buf
	max_page = max_page_buf
	
	B738_fmc_display()
	

end

function B738_fmc_disp_fo()
	
	-- local act_page_00 = act_page
	-- local max_page_00 = max_page
	
	local page_menu_00 = page_menu
	local page_dep_arr_00 = page_dep_arr
	local page_climb_00 = page_climb
	local page_init_00 = page_init
	local page_ident_00 = page_ident
	local page_takeoff_00 = page_takeoff
	local page_approach_00 = page_approach
	local page_perf_00 = page_perf
	local page_n1_limit_00 = page_n1_limit
	local page_pos_init_00 = page_pos_init
	local page_cruise_00 = page_cruise
	local page_descent_00 = page_descent
	local page_route_00 = page_route
	local page_legs_00 = page_legs
	local page_descent_forecast_00 = page_descent_forecast
	local page_rte_init_00 = page_rte_init
	local page_dep_00 = page_dep
	local page_arr_00 = page_arr
	local page_progress_00 = page_progress
	local page_hold_00 = page_hold
	local page_sel_wpt_00 = page_sel_wpt
	local page_sel_wpt2_00 = page_sel_wpt2
	local page_sel_wpt3_00 = page_sel_wpt3
	local page_sel_wpt4_00 = page_sel_wpt4
	local page_sel_hold_wpt_00 = page_sel_hold_wpt

	local page_offset_00 = page_offset
	local page_ref_nav_data_00 = page_ref_nav_data
	local page_ref_nav_data_wpt_00 = page_ref_nav_data_wpt
	local page_ref_nav_data_navaid_00 = page_ref_nav_data_navaid
	local page_ref_nav_data_apt_00 = page_ref_nav_data_apt
	local page_ref_sel_00 = page_ref_sel
	local page_fix_00 = page_fix
	local page_rte_legs_00 = page_rte_legs
	local page_ac_00 = page_ac
	local page_ac_atis_00 = page_ac_atis
	local page_ac_msg_00 = page_ac_msg
	local page_nearest_apt_00 = page_nearest_apt
	local page_altern_apt_00 = page_altern_apt
	local page_nav_status_00 = page_nav_status
	local page_atc_00 = page_atc
	local page_fmc_comm_00 = page_fmc_comm
	
	local page_dl_menu_00 = page_dl_menu
	local page_dl_acars_00 = page_dl_acars
	local page_dl_requests_00 = page_dl_requests
	local page_dl_wxr_req_00 = page_dl_wxr_req
	local page_dl_wxr_info_00 = page_dl_wxr_info
	local page_dl_fltlog_00 = page_dl_fltlog
	local page_dl_sensors_00 = page_dl_sensors
	local page_eo_cruise_00 = page_eo_cruise
	local page_eo_climb_00 = page_eo_climb
	local page_flt_plns_00 = page_flt_plns
	
	local clb_del_spd_idx_00 = clb_del_spd_idx
	local des_del_spd_idx_00 = des_del_spd_idx
	
	act_page_buf = act_page2
	max_page_buf = max_page2
	-- if page_legs2 == 1 or page_route2 == 1 or page_rte_init2 == 1 then
		-- if act_page_buf > max_page_buf then
			-- act_page_buf = max_page_buf
		-- end
	-- end
	
	page_menu = page_menu2
	page_dep_arr = page_dep_arr2
	page_climb = page_climb2
	page_init = page_init2
	page_ident = page_ident2
	page_takeoff = page_takeoff2
	page_approach = page_approach2
	page_perf = page_perf2
	page_n1_limit = page_n1_limit2
	page_pos_init = page_pos_init2
	page_cruise = page_cruise2
	page_descent = page_descent2
	page_route = page_route2
	page_legs = page_legs2
	page_descent_forecast = page_descent_forecast2
	page_rte_init = page_rte_init2
	page_dep = page_dep2
	page_arr = page_arr2
	page_progress = page_progress2
	page_hold = page_hold2
	page_sel_wpt = page_sel_wpt_2
	page_sel_wpt2 = page_sel_wpt2_2
	page_sel_wpt3 = page_sel_wpt3_2
	page_sel_wpt4 = page_sel_wpt4_2
	page_sel_hold_wpt = page_sel_hold_wpt_2
	page_offset = page_offset2
	page_ref_nav_data = page_ref_nav_data2
	page_ref_nav_data_wpt = page_ref_nav_data_wpt2
	page_ref_nav_data_navaid = page_ref_nav_data_navaid2
	page_ref_nav_data_apt = page_ref_nav_data_apt2
	page_ref_sel = page_ref_sel2
	page_fix = page_fix2
	page_rte_legs = page_rte_legs2
	page_ac = page_ac2
	page_ac_atis = page_ac_atis2
	page_ac_msg = page_ac_msg2
	page_nearest_apt = page_nearest_apt2
	page_altern_apt = page_altern_apt2
	page_nav_status = page_nav_status2
	page_atc = page_atc2
	page_fmc_comm = page_fmc_comm2
	page_dl_menu = page_dl_menu2
	page_dl_acars = page_dl_acars2
	page_dl_requests = page_dl_requests2
	page_dl_wxr_req = page_dl_wxr_req2
	page_dl_wxr_info = page_dl_wxr_info2
	page_dl_fltlog = page_dl_fltlog2
	page_dl_sensors = page_dl_sensors2
	page_eo_cruise = page_eo_cruise2
	page_eo_climb = page_eo_climb2
	page_flt_plns = page_flt_plns2
	
	if page_menu > 0 then
		B738_fmc_menu()
	elseif page_pos_init > 0 then
		B738_fmc_pos_init(1)
	elseif page_takeoff > 0 then
		B738_fmc_takeoff()
	elseif page_approach > 0 then
		B738_fmc_approach()
	elseif page_perf > 0 then
		B738_fmc_perf()
	elseif page_n1_limit > 0 then
		B738_fmc_n1_limit()
	elseif page_init > 0 then
		B738_fmc_init()
	elseif page_ident > 0 then
		B738_fmc_ident()
	elseif page_climb > 0 then
		clb_del_spd_idx = 0
		B738_fmc_climb()
		clb_del_spd_idx_fo = clb_del_spd_idx
	elseif page_cruise > 0 then
		B738_fmc_cruise(1)
	elseif page_descent > 0 then
		des_del_spd_idx = 0
		B738_fmc_descent()
		des_del_spd_idx_fo = des_del_spd_idx
	elseif page_descent_forecast > 0 then
		B738_fmc_descent_forecast(1)
	elseif page_rte_init > 0 then
		B738_fmc_rte_init(1)
	elseif page_legs > 0 then
		if rte12_sel_fo == 0 then
			if legs_step2 > legs_num2 then
				legs_step2 = legs_num2
			end
		else
			if legs_step2 > legs_num8 then
				legs_step2 = legs_num8
			end
		end
		B738_fmc_legs99(1)
	elseif page_dep_arr > 0 then
		B738_fmc_dep_arr()
	elseif page_dep > 0 then
		B738_fmc_dep99(1)
	elseif page_arr > 0 then
		B738_fmc_arr99(1)
	elseif page_sel_wpt > 0 then
		B738_fmc_sel_wpt()
	elseif page_sel_wpt2 > 0 then
		B738_fmc_sel_wpt2()
	elseif page_sel_wpt3 > 0 then
		B738_fmc_sel_wpt3()
	elseif page_sel_wpt4 > 0 then
		B738_fmc_sel_wpt4()
	elseif page_sel_hold_wpt > 0 then
		B738_fmc_sel_hold_wpt()
	elseif page_progress > 0 then
		B738_fmc_progress(1)
	elseif page_offset > 0 then
		B738_fmc_offset()
	elseif page_hold > 0 then
		B738_fmc_hold(1)
	elseif page_ref_nav_data > 0 then
		B738_fmc_ref_nav_data()
	elseif page_ref_nav_data_wpt > 0 then
		B738_fmc_ref_nav_data_wpt()
	elseif page_ref_nav_data_navaid > 0 then
		B738_fmc_ref_nav_data_navaid()
	elseif page_ref_nav_data_apt > 0 then
		B738_fmc_ref_nav_data_apt()
	elseif page_ref_sel > 0 then
		B738_ref_nav_sel()
	elseif page_fix > 0 then
		B738_fmc_fix()
	elseif page_rte_legs > 0 then
		B738_rte_data(1)
	elseif page_ac > 0 then
		B738_fmc_acars()
	elseif page_ac_atis > 0 then
		B738_fmc_ac_atis()
	elseif page_ac_msg > 0 then
		B738_fmc_ac_msg()
	elseif page_nearest_apt > 0 then
		B738_fmc_nearest_apt()
	elseif page_altern_apt > 0 then
		B738_fmc_altern_apt()
	elseif page_nav_status > 0 then
		B738_fmc_nav_status()
	elseif page_atc > 0 then
		fmc_atc()
	elseif page_fmc_comm > 0 then
		fmc_fmc_comm()
	elseif page_dl_menu > 0 then
		dl_menu()
	elseif page_dl_acars > 0 then
		dl_acars()
	elseif page_dl_requests > 0 then
		dl_requests()
	elseif page_dl_wxr_req > 0 then
		dl_wxr_req()
	elseif page_dl_wxr_info > 0 then
		dl_wxr_info()
	elseif page_dl_fltlog > 0 then
		dl_fltlog()
	elseif page_dl_sensors > 0 then
		dl_sensors()
	elseif page_eo_cruise > 0 then
		fmc_engout_crz()
	elseif page_eo_climb > 0 then
		fmc_engout_clb()
	elseif page_flt_plns > 0 then
		B738_fmc_flt_plns(1)
	end
	
	B738_fmc_display2()
	
	act_page2 = act_page_buf
	max_page2 = max_page_buf
	
	-- act_page = act_page_00
	-- max_page = max_page_00
	
	page_menu = page_menu_00
	page_dep_arr = page_dep_arr_00
	page_climb = page_climb_00
	page_init = page_init_00
	page_ident = page_ident_00
	page_takeoff = page_takeoff_00
	page_approach = page_approach_00
	page_perf = page_perf_00
	page_n1_limit = page_n1_limit_00
	page_pos_init = page_pos_init_00
	page_cruise = page_cruise_00
	page_descent = page_descent_00
	page_route = page_route_00
	page_legs = page_legs_00
	page_descent_forecast = page_descent_forecast_00
	page_rte_init = page_rte_init_00
	page_dep = page_dep_00
	page_arr = page_arr_00
	page_progress = page_progress_00
	page_hold = page_hold_00
	page_sel_wpt = page_sel_wpt_00
	page_sel_wpt2 = page_sel_wpt2_00
	page_sel_wpt3 = page_sel_wpt3_00
	page_sel_wpt4 = page_sel_wpt4_00
	page_sel_hold_wpt = page_sel_hold_wpt_00
	page_offset = page_offset_00
	page_ref_nav_data = page_ref_nav_data_00
	page_ref_nav_data_wpt = page_ref_nav_data_wpt_00
	page_ref_nav_data_navaid = page_ref_nav_data_navaid_00
	page_ref_nav_data_apt = page_ref_nav_data_apt_00
	page_ref_sel = page_ref_sel_00
	page_fix = page_fix_00
	page_rte_legs = page_rte_legs_00
	page_ac = page_ac_00
	page_ac_atis = page_ac_atis_00
	page_ac_msg = page_ac_msg_00
	page_nearest_apt = page_nearest_apt_00
	page_altern_apt = page_altern_apt_00
	page_nav_status = page_nav_status_00
	page_atc = page_atc_00
	page_fmc_comm = page_fmc_comm_00
	page_dl_menu = page_dl_menu_00
	page_dl_acars = page_dl_acars_00
	page_dl_requests = page_dl_requests_00
	page_dl_wxr_req = page_dl_wxr_req_00
	page_dl_wxr_info = page_dl_wxr_info_00
	page_dl_fltlog = page_dl_fltlog_00
	page_dl_sensors = page_dl_sensors_00
	page_eo_cruise = page_eo_cruise_00
	page_eo_climb = page_eo_climb_00
	page_flt_plns = page_flt_plns_00
	
	clb_del_spd_idx = clb_del_spd_idx_00
	des_del_spd_idx = des_del_spd_idx_00
	--B738_fmc_display2()

end


function B738_calc()
	
	local v = 0
	local pos_min = ""
	local pos_deg = ""
	local weight_lbs = 0
	local act_fuel = 0
	
	if units_recalc == 1 then
		if units == 0 then
			zfw = zfw_lbs
			zfw_mod = zfw_lbs_mod
			plan_weight_mod = plan_weight_lbs_mod
			reserves = reserves_lbs
		else
			zfw = zfw_kgs
			zfw_mod = zfw_kgs_mod
			plan_weight_mod = plan_weight_kgs_mod
			reserves = reserves_kgs
		end
	end
	units_recalc = 0
	
	if B738DR_fms_v1_calc == 0 then
		v1 = "---"
		vr = "---"
		v2 = "---"
	else
		v1 = string.format("%3d", B738DR_fms_v1_calc)
		vr = string.format("%3d", B738DR_fms_vr_calc)
		v2 = string.format("%3d", B738DR_fms_v2_calc)
	end
	
	vref_15 = string.format("%3d", B738DR_fms_vref_15)	-- + B738DR_fms_approach_wind_corr)
	vref_30 = string.format("%3d", B738DR_fms_vref_30)	-- + B738DR_fms_approach_wind_corr)
	vref_40 = string.format("%3d", B738DR_fms_vref_40)	-- + B738DR_fms_approach_wind_corr)
	
	if B738DR_trim_calc == 0 then
		trim = "    "
	else
		trim = string.format("%4.2f", B738DR_trim_calc)
	end

	weight_lbs = (simDR_total_weight / 1000) * KGS_LBS			-- 1000 lbs
	gw_act = string.format("%5.1f", weight_lbs)
	
	-- Fuel_weight
	weight_lbs = (simDR_fuel_weight / 1000) * KGS_LBS			-- 1000 lbs
	fuel_weight_lbs = string.format("%4.1f", weight_lbs)
	fuel_weight_kgs = string.format("%4.1f", weight_lbs / KGS_LBS)
	if units == 0 then
		fuel_weight = fuel_weight_lbs
	else
		fuel_weight = fuel_weight_kgs
	end
	
	-- ZFC calc
	-- v = ((simDR_total_weight / 1000) - (simDR_fuel_weight / 1000)) * KGS_LBS		-- 1000 lbs
	v = (zfw_real / 1000) * KGS_LBS		-- 1000 lbs
	zfw_calc_lbs = string.format("%5.1f", v)
	zfw_calc_kgs = string.format("%5.1f", v / KGS_LBS)
	if units == 0 then
		zfw_calc = zfw_calc_lbs
	else
		zfw_calc = zfw_calc_kgs
	end
	
	-- PLAN FUEL used
	if B738DR_plan_fuel == 0 then
		act_fuel = (simDR_fuel_weight / 1000) * KGS_LBS
		init_fuel = simDR_fuel_weight
		plan_weight = "---.-"
		plan_weight_kgs = "---.-"
		plan_weight_lbs = "---.-"
		plan_weight_mod = "---.-"
		plan_weight_kgs_mod = "---.-"
		plan_weight_lbs_mod = "---.-"
	else
		if simDR_eng_N2[0] > 50 or simDR_eng_N2[1] > 50 or was_on_air ~= 0 then
			act_fuel = (simDR_fuel_weight / 1000) * KGS_LBS
			init_fuel = simDR_fuel_weight
			plan_weight = "---.-"
			plan_weight_kgs = "---.-"
			plan_weight_lbs = "---.-"
			plan_weight_mod = "---.-"
			plan_weight_kgs_mod = "---.-"
			plan_weight_lbs_mod = "---.-"
		else
			act_fuel = tonumber(plan_weight_kgs_mod)
			if act_fuel == nil then
				init_fuel = simDR_fuel_weight
				act_fuel = (init_fuel / 1000) * KGS_LBS
			else
				act_fuel = act_fuel * KGS_LBS
			end
		end
	end
	
	-- CALC GW
	v = tonumber(zfw_lbs)
	if v == nil then
		gw = "***.*"
		gw_lbs = gw
		gw_kgs = gw
		v = ((simDR_total_weight / 1000) - (simDR_fuel_weight / 1000)) * KGS_LBS		-- 1000 lbs
		-- v = tonumber(zfw_calc_lbs)
		v = v + tonumber(act_fuel)
		-- v = v + tonumber(fuel_weight_lbs)
		if units == 0 then
			gw_calc = string.format("%5.1f", v)
		else
			gw_calc = string.format("%5.1f", v / KGS_LBS)
		end
		
	else
		v = v + tonumber(act_fuel)
		-- v = v + tonumber(fuel_weight_lbs)
		gw_lbs = string.format("%5.1f", v)
		gw_kgs = string.format("%5.1f", v / KGS_LBS)
		if units == 0 then
			gw = gw_lbs
		else
			gw = gw_kgs
		end
		gw_calc = gw
	end
	
	v = tonumber(zfw_lbs_mod)
	if v == nil then
		gw_mod = "***.*"
		gw_lbs_mod = gw_mod
		gw_kgs_mod = gw_mod
		v = ((simDR_total_weight / 1000) - (simDR_fuel_weight / 1000)) * KGS_LBS		-- 1000 lbs
		-- v = tonumber(zfw_calc_lbs)
		v = v + tonumber(act_fuel)
		-- v = v + tonumber(fuel_weight_lbs)
		
	else
		v = v + tonumber(act_fuel)
		-- v = v + tonumber(fuel_weight_lbs)
		gw_lbs_mod = string.format("%5.1f", v)
		gw_kgs_mod = string.format("%5.1f", v / KGS_LBS)
		if units == 0 then
			gw_mod = gw_lbs_mod
		else
			gw_mod = gw_kgs_mod
		end
	end
	
	-- SET V SPEEDS
	v = tonumber(flaps)
	if v == nil then
		B738DR_fms_takeoff_flaps = 0
	else
		B738DR_fms_takeoff_flaps = v
	end
	
	v = tonumber(app_flap)
	if v == nil then
		B738DR_fms_approach_flaps = 0
	else
		B738DR_fms_approach_flaps = v
	end
	v = tonumber(app_spd)
	if v == nil then
		B738DR_fms_approach_speed = 0
	else
		B738DR_fms_approach_speed = v
	end
	v = tonumber(wind_corr)
	if v == nil then
		B738DR_fms_approach_wind_corr = 5
	else
		B738DR_fms_approach_wind_corr = v
	end
	
	
	v = tonumber(v1_set)
	if v == nil then
		B738DR_fms_v1_set = 0
	else
		B738DR_fms_v1_set = v
	end
	v = tonumber(vr_set)
	if v == nil then
		B738DR_fms_vr_set = 0
	else
		B738DR_fms_vr_set = v
	end
	v = tonumber(v2_set)
	if v == nil then
		B738DR_fms_v2_set = 0
	else
		B738DR_fms_v2_set = v
	end
	
	-- V Speed calculate enable
	if gw_lbs == "***.*" then
		-- vref_15 = "---"
		-- vref_30 = "---"
		-- vref_40 = "---"
		v1 = "---"
		vr = "---"
		v2 = "---"
		trim = "    "
		-- flaps_app = "  "
		B738DR_fmc_gw = 0
	else
		if flaps == "**" then
			B738DR_calc_spd_enable = 0
			v1 = "---"
			vr = "---"
			v2 = "---"
			trim = "    "
		end
		B738DR_calc_spd_enable = 1
		v = tonumber(gw_lbs)
		if v == nil then
			B738DR_fmc_gw = 0
		else
			B738DR_fmc_gw = v	-- GW lbs
		end
	end
	
	-- GW approach
	if gw_app == "***.*" then
		B738DR_fmc_gw_app = 0
		vref_15 = "---"
		vref_30 = "---"
		vref_40 = "---"
		flaps_app = "  "
	else
		v = tonumber(gw_app_lbs)
		if v == nil then
			B738DR_fmc_gw_app = 0
		else
			B738DR_fmc_gw_app = v	-- GW lbs
		end
	end
	
	-- CG
	v = tonumber(cg)
	if v == nil then
		B738DR_fmc_cg = 0
		trim = "    "
	else
		B738DR_fmc_cg = v	-- CG %MAC
	end
	
	
	-- RWY wind, slope, hdg
	v = tonumber(rw_wind_dir)
	if v == nil then
		B738DR_rw_wind_dir = -1
	else
		B738DR_rw_wind_dir = v
	end
	v = tonumber(rw_wind_spd)
	if v == nil then
		B738DR_rw_wind_spd = -1
	else
		B738DR_rw_wind_spd = v
	end
	v = tonumber(string.sub(rw_slope, 2, -1))
	if v == nil then
		B738DR_rw_slope = 0
	else
		if string.sub(rw_slope, 1, 1) == "D" then
			v = -v
		end
		B738DR_rw_slope = v
	end
	v = tonumber(rw_hdg)
	if v == nil then
		B738DR_rw_hdg = -1
	else
		B738DR_rw_hdg = v
	end
	
	-- UNITS
	B738DR_fmc_units = units
	if units == 0 then
		weight_min = 90
		weight_max = 180
	else
		weight_min = 40
		weight_max = 90
	end
	-- B739U
	-- if units == 0 then
		-- weight_min = 94
		-- weight_max = 190
	-- else
		-- weight_min = 43
		-- weight_max = 90
	-- end
	
	-- ZULU TIME
	if simDR_gps_fail == 0 or simDR_gps2_fail == 0 then
		zulu_time = string.format("%02d", simDR_zulu_hours) .. string.format("%02d", simDR_zulu_minutes)
		zulu_time = zulu_time .. "."
		zulu_time = zulu_time .. string.sub(string.format("%02d", simDR_zulu_seconds), 1, 1)
		zulu_time = zulu_time .. "z "
		zulu_time = zulu_time .. string.format("%02d", simDR_time_month)
		zulu_time = zulu_time .. "/"
		zulu_time = zulu_time .. string.format("%02d", simDR_time_day)
	else
		zulu_time = "0000.0z      "
	end
	
	B738DR_clock_year = tonumber(os.date("!%y"))
	
	-- SEL TEMP
	v = tonumber(sel_temp)
	if v == nil then
		B738DR_fmc_sel_temp = 99	-- no entry
	else
		B738DR_fmc_sel_temp = v
	end
	
	-- OUT TEMP manually
	v = tonumber(oat)
	if B738DR_aspirated_probe == 0 then
		if v == nil then
			B738DR_fmc_oat_temp = 99	-- no entry
		else
			B738DR_fmc_oat_temp = v
		end
	else
		if v == nil then
			B738DR_fmc_oat_temp = simDR_TAT	-- no entry
		else
			B738DR_fmc_oat_temp = v
		end
	end
	
	-- Runway condition
	B738DR_fmc_rw_cond = rw_cond
	
	-- Transition altitude
	if trans_alt == "-----" then
		B738DR_trans_alt = 18000
	else
		B738DR_trans_alt = tonumber(trans_alt)
	end
	if trans_alt_mod == "-----" then
		B738DR_trans_alt_mod = 18000
	else
		B738DR_trans_alt_mod = tonumber(trans_alt_mod)
	end
	
	
	-- Transition level
	v = tonumber(string.sub(trans_lvl, 3, 5))
	if v == nil then
		B738DR_trans_lvl = 18000	--B738DR_trans_alt
	else
		B738DR_trans_lvl = v * 100
	end
	v = tonumber(string.sub(trans_lvl_mod, 3, 5))
	if v == nil then
		B738DR_trans_lvl_mod = 18000	--B738DR_trans_alt
	else
		B738DR_trans_lvl_mod = v * 100
	end
	
	-- Forecast QNH
	v = tonumber(forec_qnh)
	if v == nil then
		B738DR_forecast_in_hg = 29.92
	else
		if v <= 40 then
			-- in hg
			B738DR_forecast_in_hg = v
		else
			-- hpa
			B738DR_forecast_in_hg = 0.02953 * v
		end
	end
	v = tonumber(forec_qnh_mod)
	if v == nil then
		B738DR_forecast_in_hg_mod = 29.92
	else
		if v <= 40 then
			-- in hg
			B738DR_forecast_in_hg_mod = v
		else
			-- hpa
			B738DR_forecast_in_hg_mod = 0.02953 * v
		end
	end
	
	-- Forecast ISA
	v = tonumber(forec_isa_dev)
	if v == nil then
		B738DR_forecast_isa_dev = 0
	else
		B738DR_forecast_isa_dev = v
	end
	
	v = tonumber(forec_isa_dev_mod)
	if v == nil then
		B738DR_forecast_isa_dev_mod = 0
	else
		B738DR_forecast_isa_dev_mod = v
	end
	
	-- Throttle reduction alt
	B738DR_thr_red_alt = clb_alt_num
	B738DR_eo_accel_alt = eo_accel_alt_num
	B738DR_thr_cut_alt = cut_alt_num
	if B738DR_thr_cutback == 0 then
		B738DR_accel_alt = accel_alt_num
	else
		B738DR_accel_alt = cut_alt_num - 100
	end
	
	-- on ground / air
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		if was_on_air == 1 then
			was_on_air = 0
			-- on the ground
			ground_air = 0
		end
	end
	if simDR_radio_height_pilot_ft > 50 then
		if was_on_air == 0 then
			was_on_air = 1
			-- air
			ground_air = 1
		end
		verify_to_spd = 0
	end
	-- ISA DEV C
	if isa_dev_c ~= "---" then
		v = tonumber(isa_dev_c)
		if v == nil then
			B738DR_isa_dev_c = 0	--10
		else
			B738DR_isa_dev_c = v
		end
	else
		B738DR_isa_dev_c = 0
	end
	
	-- Cabin rate
	v = tonumber(cabin_rate)
	if v == nil then
		B738DR_fmc_cabin_rate = 500
	else
		B738DR_fmc_cabin_rate = v
	end
	
end


function pos_to_string(pos_lat_deg0, pos_lat_min0, pos_lat_NS0, pos_lon_deg0, pos_lon_min0, pos_lon_EW0)
	local position_min = ""
	local position_deg = ""
	local position = ""

	local pos_deg = string.format("%02d", pos_lat_deg0)
	local pos_min = string.format("%05.1f", pos_lat_min0)
	if pos_lat_NS0 == 1 then
		position = ("S" .. pos_deg)
	else
		position = ("N" .. pos_deg)
	end
	position = position .. string.sub(pos_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(pos_min, 3, 3)
	pos_deg = string.format("%03d", pos_lon_deg0)
	pos_min = string.format("%05.1f", pos_lon_min0)
	if pos_lon_EW0 == 1 then
		position = (position .. "W")
	else
		position = (position .. "E")
	end
	position = position .. pos_deg
	position = position .. string.sub(pos_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(pos_min, 3, 3)

	return position
end

function B738_set_last_pos()


	local lat = (math.abs(B738DR_last_pos_lat))
	local lat_deg = (math.floor(lat))
	local lat_min_dec = (math.fmod(lat,1.0)*600)
	local lat_min_dec2 = (string.format("%06.3f",lat_min_dec))

	local last_latitude_NS = 0
	if B738DR_last_pos_lat < 0 then
		last_latitude_NS = 1
	end

	local lon = (math.abs(B738DR_last_pos_lon))
	local lon_deg = (math.floor(lon))
	local lon_min_dec = (math.fmod(lon,1.0)*600)
	local lon_min_dec2 = (string.format("%06.3f",lon_min_dec))

	local last_longitude_EW = 0
	if B738DR_last_pos_lon < 0 then
		last_longitude_EW = 1
	end

	if B738DR_last_pos_lat == 0 and B738DR_last_pos_lon == 0 then
		last_pos = "-----.-------.-"
	else
		last_pos = pos_to_string(lat_deg, lat_min_dec2, last_latitude_NS, lon_deg, lon_min_dec2, last_longitude_EW)
	end

end


function B738_last_pos()

	-- Last Position
	
	local gps_irs_fail = 0
	if B738DR_gps_pos == "-----.-------.-" 
	and B738DR_gps2_pos == "-----.-------.-" 
	and B738DR_irs_pos =="-----.-------.-"
	and B738DR_irs2_pos == "-----.-------.-" then
		gps_irs_fail = 1
	end
	
	if B738DR_irs_pos ~= "-----.-------.-" or B738DR_irs2_pos ~= "-----.-------.-" then
		last_pos = pos_to_string(B738DR_latitude_deg, B738DR_latitude_min, B738DR_latitude_NS, B738DR_longitude_deg, B738DR_longitude_min, B738DR_longitude_EW)
	end
	B738DR_last_pos_str = last_pos
	
	if gps_irs_fail == 0 then
		if gps_ok == 0 and B738DR_chock_status == 1 then
			gps_ok = 1
			set_chock = 1
		end
		
	else
		gps_ok = 0
	end

end

function airport_pos(in_rte)

	local temp_i = 0
	
	local nd_x = 0
	local idx_rec = 0
	
	local use_ref_icao = "----"
	
	if in_rte == 0 then
		use_ref_icao = ref_icao
	elseif in_rte == 1 then
		use_ref_icao = ref_icao7
	end
	
	if use_ref_icao ~= "----" then
		
		icao_latitude = 0
		icao_longitude = 0
		
		if apt_data_num > 0 and string.len(use_ref_icao) > 0 then
			nd_x = string.byte(string.sub(use_ref_icao, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_apt[nd_x][99999] > 0 then
					for temp_i = 1, idx_apt[nd_x][99999] do
						idx_rec = idx_apt[nd_x][temp_i]
						if use_ref_icao == apt_data[idx_rec][1] then
							icao_latitude = apt_data[idx_rec][2]
							icao_longitude = apt_data[idx_rec][3]
							break
						end
					end
				end
			end
		end
	end

end

function airport_pos2(in_rte)

	local position_min = ""
	local position_deg = ""
	local position = ""
	local temp_deg = 0
	local temp_min = 0
	local airport_lat_deg = 0
	local airport_lat_min = 0
	local airport_lon_deg = 0
	local airport_lon_min = 0
	local airport_lat_ns = 0
	local airport_lon_ew = 0
	local temp_i = 0
	
	local nd_x = 0
	local idx_rec = 0
	
	local icao_latitude2 = 0
	local icao_longitude2 = 0
	
	local use_ref_icao = "----"
	
	if in_rte == 0 then
		use_ref_icao = ref_icao2
	elseif in_rte == 1 then
		use_ref_icao = ref_icao8
	end
	
	if use_ref_icao == "----" then
		if in_rte == 0 then
			ref_icao_pos = "               "
		else
			ref_icao_pos2 = "               "
		end
	else
		
		
		if apt_data_num > 0 and string.len(use_ref_icao) > 0 then
			nd_x = string.byte(string.sub(use_ref_icao, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_apt[nd_x][99999] > 0 then
					for temp_i = 1, idx_apt[nd_x][99999] do
						idx_rec = idx_apt[nd_x][temp_i]
						if use_ref_icao == apt_data[idx_rec][1] then
							icao_latitude2 = apt_data[idx_rec][2]
							icao_longitude2 = apt_data[idx_rec][3]
							break
						end
					end
				end
			end
		
			local lat = (math.abs(icao_latitude2))
			local lat_min_dec = (math.fmod(lat,1.0)*600)
			airport_lat_deg = (math.floor(lat))
			airport_lat_min = (string.format("%06.3f",lat_min_dec))
			airport_lat_ns = 0
			if icao_latitude2 < 0 then
				airport_lat_ns = 1
			end
			
			local lon = (math.abs(icao_longitude2))
			local lon_min_dec = (math.fmod(lon,1.0)*600)
			airport_lon_deg = (math.floor(lon))
			airport_lon_min = (string.format("%06.3f",lon_min_dec))
			airport_lon_ew = 0
			if icao_longitude2 < 0 then
				airport_lon_ew = 1
			end
			
			
			
			temp_deg = string.format("%02d", airport_lat_deg)
			temp_min = string.format("%05.1f", airport_lat_min)
			if airport_lat_ns == 1 then
				position = ("S" .. temp_deg)
			else
				position = ("N" .. temp_deg)
			end
			position = position .. string.sub(temp_min, 1, 2)
			position = position .. "."
			position = position .. string.sub(temp_min, 3, 3)
			temp_deg = string.format("%03d", airport_lon_deg)
			temp_min = string.format("%05.1f", airport_lon_min)
			if airport_lon_ew == 1 then
				position = (position .. "W")
			else
				position = (position .. "E")
			end
			position = position .. temp_deg
			position = position .. string.sub(temp_min, 1, 2)
			position = position .. "."
			position = position .. string.sub(temp_min, 3, 3)
			
			if in_rte == 0 then
				ref_icao_pos = position
			else
				ref_icao_pos2 = position
			end
			
		else
			if in_rte == 0 then
				ref_icao_pos = "               "
			else
				ref_icao_pos2 = "               "
			end
		end
	end

end

function string_lat_pos(lat_pos)

	local position_min = ""
	local position_deg = ""
	local position = ""
	local temp_deg = 0
	local temp_min = 0
	local navaid_lat_deg = 0
	local navaid_lat_min = 0
	local navaid_lat_ns = 0
	
	local lat = (math.abs(lat_pos))
	local lat_min_dec = (math.fmod(lat,1.0)*600)
	navaid_lat_deg = (math.floor(lat))
	navaid_lat_min = (string.format("%06.3f",lat_min_dec))
	navaid_lat_ns = 0
	if lat_pos < 0 then
		navaid_lat_ns = 1
	end
	
	temp_deg = string.format("%02d", navaid_lat_deg)
	temp_min = string.format("%05.1f", navaid_lat_min)
	if navaid_lat_ns == 1 then
		position = ("S" .. temp_deg)
	else
		position = ("N" .. temp_deg)
	end
	position = position .. string.sub(temp_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(temp_min, 3, 3)
	
	return position

end

function string_lon_pos(lon_pos)

	local position_min = ""
	local position_deg = ""
	local position = ""
	local temp_deg = 0
	local temp_min = 0
	local navaid_lat_deg = 0
	local navaid_lat_min = 0
	local navaid_lon_deg = 0
	local navaid_lon_min = 0
	local navaid_lat_ns = 0
	local navaid_lon_ew = 0
	
	local lon = (math.abs(lon_pos))
	local lon_min_dec = (math.fmod(lon,1.0)*600)
	navaid_lon_deg = (math.floor(lon))
	navaid_lon_min = (string.format("%06.3f",lon_min_dec))
	navaid_lon_ew = 0
	if lon_pos < 0 then
		navaid_lon_ew = 1
	end
	
	temp_deg = string.format("%03d", navaid_lon_deg)
	temp_min = string.format("%05.1f", navaid_lon_min)
	if navaid_lon_ew == 1 then
		position = ("W" .. temp_deg)
	else
		position = ("E" .. temp_deg)
	end
	position = position .. string.sub(temp_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(temp_min, 3, 3)
	
	return position

end


function B738_irs_sys()

	if B738DR_irs_left_mode == 0 and B738DR_irs_right_mode == 0 then
		msg_irs_pos = 0
		msg_irs_hdg = 0
		irs_hdg = "---`"
		irs_pos = "*****.*******.*"
	end
	
	B738DR_fms_test1 = 271
	
	local gs = string.format("%3d", (simDR_ground_spd * 1.94384))
	if B738DR_irs_left_mode > 1 then
		if B738DR_irs_left_mode == 2 then
			irs_gs = gs
		else
			irs_gs = "   "
		end
		fmc_pos = B738DR_irs_pos
	else
		irs_gs = "   "
	end
	
	B738DR_fms_test1 = 272
	
	if B738DR_irs_right_mode > 1 then
		if B738DR_irs_right_mode == 2 then
			irs2_gs = gs
		else
			irs2_gs = "   "
		end
		fmc_pos = B738DR_irs2_pos
	else
		irs2_gs = "   "
	end
	
	B738DR_fms_test1 = 273
	
	if B738DR_irs_left_mode > 1 or B738DR_irs_right_mode > 1 then
		if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
			fmc_gs = gs
		else
			fmc_gs = "   "
		end
		irs_pos = ""
	else
		fmc_gs = "   "
		fmc_pos = "-----.-------.-"
	end
	
	B738DR_fms_test1 = 274
	
	if B738DR_irs_pos ~= "-----.-------.-" then
		B738DR_irs1_restart = 0
	end
	if B738DR_irs2_pos ~= "-----.-------.-" then
		B738DR_irs2_restart = 0
	end
	if B738DR_irs1_restart == 0 and B738DR_irs2_restart == 0 then
		msg_irs_motion = 0
	end
	
	B738DR_fms_test1 = 275
	
	if B738DR_gps_pos ~= "-----.-------.-" then
		msg_gps_l_fail = 0
	end
	
	if B738DR_gps2_pos ~= "-----.-------.-" then
		msg_gps_r_fail = 0
	end
	
	if B738DR_gps_pos ~= "-----.-------.-" 
	and B738DR_gps2_pos ~= "-----.-------.-" then
		msg_gps_lr_fail = 0
	end

end


function B738_N1_sel_thr()

	if sel_temp == "----" then
		if to == "<ACT>" then	-- 26K
			B738DR_fms_N1_to_sel = 1
		elseif to_1 == "<ACT>" then		-- 24K
			B738DR_fms_N1_to_sel = 2
		elseif to_2 == "<ACT>" then		-- 22K
			B738DR_fms_N1_to_sel = 3
		end
	else
		if to == "<ACT>" then	-- RED 26K
			B738DR_fms_N1_to_sel = 4
		elseif to_1 == "<ACT>" then		-- RED 24K
			B738DR_fms_N1_to_sel = 5
		elseif to_2 == "<ACT>" then		-- RED 22K
			B738DR_fms_N1_to_sel = 6
		end
	end

end

function B738_N1_thrust_calc()

	fmc_full_thrust = B738DR_thr_takeoff_N1[0]
	fmc_full_thrust2 = B738DR_thr_takeoff_N1[1]
	
	fmc_full_thrust_ind = B738DR_thr_takeoff_full_N1[0]
	fmc_full_thrust_ind2 = B738DR_thr_takeoff_full_N1[1]
	
	fmc_dto_thrust = B738DR_thr_takeoff_N1[0]
	fmc_dto_thrust2 = B738DR_thr_takeoff_N1[1]
	
	fmc_dto_thrust_ind = B738DR_thr_takeoff_full_N1[0]
	fmc_dto_thrust_ind2 = B738DR_thr_takeoff_full_N1[1]
	
	fmc_clb_thrust = B738DR_thr_climb_N1[0]
	fmc_clb_thrust2 = B738DR_thr_climb_N1[1]
	
	fmc_crz_thrust = B738DR_thr_cruise_N1[0]
	fmc_crz_thrust2 = B738DR_thr_cruise_N1[1]
	
	-- --fmc_con_thrust = B738DR_thr_goaround_N1 * 0.96
	-- fmc_con_thrust = B738DR_thr_cruise_N1 * 1.04
	-- fmc_con_thrust = math.min(fmc_con_thrust, 1.015)
	
	fmc_con_thrust = B738DR_thr_cont_N1[0]
	fmc_con_thrust2 = B738DR_thr_cont_N1[1]
	
	if B738DR_fd_goaround == 0 then
		if B738DR_ap_goaround <= 1 then
			fmc_ga_thrust = B738DR_thr_goaround_N1[0] * 0.94
			fmc_ga_thrust2 = B738DR_thr_goaround_N1[1] * 0.94
		else
			fmc_ga_thrust = B738DR_thr_goaround_N1[0]
			fmc_ga_thrust2 = B738DR_thr_goaround_N1[1]
		end
	elseif B738DR_fd_goaround <= 1 then
		fmc_ga_thrust = B738DR_thr_goaround_N1[0] * 0.94
		fmc_ga_thrust2 = B738DR_thr_goaround_N1[1] * 0.94
	else
		fmc_ga_thrust = B738DR_thr_goaround_N1[0]
		fmc_ga_thrust2 = B738DR_thr_goaround_N1[1]
	end
	
	fmc_ga_thrust_ind = B738DR_thr_goaround_N1[0]
	fmc_ga_thrust_ind2 = B738DR_thr_goaround_N1[1]

	if B738DR_fmc_oat_temp == 99 then
		to = "<ACT>"
		to_1 = "     "
		to_2 = "     "
		sel_clb_thr = 0
		clb = "<SEL>"
		clb_1 = "     "
		clb_2 = "     "
	end
end


function on_ground_25sec()
	takeoff_enable = 1
end

function switch_fmc_page(sw_page)
	if B738DR_fmc_exec_lights == 0 then
		--elseif page_climb == 1 or page_cruise == 1 or page_descent == 1 or page_takeoff ~= 0 then
		if page_cruise == 1 and sw_page == 3 then
			reset_fmc_pages()
			page_descent = 1
			act_page = 1
		elseif page_takeoff ~= 0 and sw_page == 1 then
			reset_fmc_pages()
			page_climb = 1
			act_page = 1
		elseif page_cruise == 1 and sw_page == 1 then
			reset_fmc_pages()
			page_climb = 1
			act_page = 1
		elseif page_descent == 1 and sw_page == 1 then
			reset_fmc_pages()
			page_climb = 1
			act_page = 1
		elseif page_climb == 1 and sw_page == 2 then
			reset_fmc_pages()
			page_cruise = 1
			act_page = 1
		end
		--elseif page_climb2 == 1 or page_cruise2 == 1 or page_descent2 == 1 or page_takeoff2 ~= 0 then
		if page_cruise2 == 1 and sw_page == 3 then
			reset_fmc_pages_fo()
			page_descent2 = 1
			act_page2 = 1
		elseif page_takeoff2 ~= 0 and sw_page == 1 then
			reset_fmc_pages_fo()
			page_climb2 = 1
			act_page2 = 1
		elseif page_cruise2 == 1 and sw_page == 1 then
			reset_fmc_pages_fo()
			page_climb2 = 1
			act_page2 = 1
		elseif page_descent2 == 1 and sw_page == 1 then
			reset_fmc_pages_fo()
			page_climb2 = 1
			act_page2 = 1
		elseif page_climb2 == 1 and sw_page == 2 then
			reset_fmc_pages_fo()
			page_cruise2 = 1
			act_page2 = 1
		end
	end
end

function landing_reset()
	
	local page_menu_00 = page_menu
	local page_dep_arr_00 = page_dep_arr
	local page_climb_00 = page_climb
	local page_init_00 = page_init
	local page_ident_00 = page_ident
	local page_takeoff_00 = page_takeoff
	local page_approach_00 = page_approach
	local page_perf_00 = page_perf
	local page_n1_limit_00 = page_n1_limit
	local page_pos_init_00 = page_pos_init
	local page_cruise_00 = page_cruise
	local page_descent_00 = page_descent
	local page_route_00 = page_route
	local page_legs_00 = page_legs
	local page_descent_forecast_00 = page_descent_forecast
	local page_rte_init_00 = page_rte_init
	local page_dep_00 = page_dep
	local page_arr_00 = page_arr
	local page_progress_00 = page_progress
	local page_hold_00 = page_hold
	local page_sel_wpt_00 = page_sel_wpt
	local page_sel_wpt2_00 = page_sel_wpt2
	local page_sel_wpt3_00 = page_sel_wpt3
	local page_sel_wpt4_00 = page_sel_wpt4
	local page_offset_00 = page_offset
	local page_ref_nav_data_00 = page_ref_nav_data
	local page_ref_nav_data_wpt_00 = page_ref_nav_data_wpt
	local page_ref_nav_data_navaid_00 = page_ref_nav_data_navaid
	local page_ref_nav_data_apt_00 = page_ref_nav_data_apt
	local page_ref_sel_00 = page_ref_sel
	local page_fix_00 = page_fix
	local page_rte_legs_00 = page_rte_legs
	local page_ac_00 = page_ac
	local page_ac_atis_00 = page_ac_atis
	local page_ac_msg_00 = page_ac_msg
	local page_nearest_apt_00 = page_nearest_apt
	local page_altern_apt_00 = page_altern_apt
	local page_nav_status_00 = page_nav_status
	local page_atc_00 = page_atc
	local page_fmc_comm_00 = page_fmc_comm
	local page_dl_menu_00 = page_dl_menu
	local page_dl_acars_00 = page_dl_acars
	local page_dl_requests_00 = page_dl_requests
	local page_dl_wxr_req_00 = page_dl_wxr_req
	local page_dl_wxr_info_00 = page_dl_wxr_info
	local page_dl_fltlog_00 = page_dl_fltlog
	local page_dl_sensors_00 = page_dl_sensors
	
	local page_menu_01 = page_menu2
	local page_dep_arr_01 = page_dep_arr2
	local page_climb_01 = page_climb2
	local page_init_01 = page_init2
	local page_ident_01 = page_ident2
	local page_takeoff_01 = page_takeoff2
	local page_approach_01 = page_approach2
	local page_perf_01 = page_perf2
	local page_n1_limit_01 = page_n1_limit2
	local page_pos_init_01 = page_pos_init2
	local page_cruise_01 = page_cruise2
	local page_descent_01 = page_descent2
	local page_route_01 = page_route2
	local page_legs_01 = page_legs2
	local page_descent_forecast_01 = page_descent_forecast2
	local page_rte_init_01 = page_rte_init2
	local page_dep_01 = page_dep2
	local page_arr_01 = page_arr2
	local page_progress_01 = page_progress2
	local page_hold_01 = page_hold2
	local page_sel_wpt_01 = page_sel_wpt_2
	local page_sel_wpt2_01 = page_sel_wpt2_2
	local page_sel_wpt3_01 = page_sel_wpt3_2
	local page_sel_wpt4_01 = page_sel_wpt4_2
	local page_offset_01 = page_offset2
	local page_ref_nav_data_01 = page_ref_nav_data2
	local page_ref_nav_data_wpt_01 = page_ref_nav_data_wpt2
	local page_ref_nav_data_navaid_01 = page_ref_nav_data_navaid2
	local page_ref_nav_data_apt_01 = page_ref_nav_data_apt2
	local page_ref_sel_01 = page_ref_sel2
	local page_fix_01 = page_fix2
	local page_rte_legs_01 = page_rte_legs2
	local page_ac_01 = page_ac2
	local page_ac_atis_01 = page_ac_atis2
	local page_ac_msg_01 = page_ac_msg2
	local page_nearest_apt_01 = page_nearest_apt2
	local page_altern_apt_01 = page_altern_apt2
	local page_nav_status_01 = page_nav_status2
	local page_atc_01 = page_atc2
	local page_fmc_comm_01 = page_fmc_comm2
	local page_dl_menu_01 = page_dl_menu2
	local page_dl_acars_01 = page_dl_acars2
	local page_dl_requests_01 = page_dl_requests2
	local page_dl_wxr_req_01 = page_dl_wxr_req2
	local page_dl_wxr_info_01 = page_dl_wxr_info2
	local page_dl_fltlog_01 = page_dl_fltlog2
	local page_dl_sensors_01 = page_dl_sensors2
	
	if rte12_act == 0 then
		B738_init3()
	elseif rte12_act == 1 then
		B738_init4()
	end
	B738_init2()
	
	act_page = 1
	max_page = 1
	page_menu = page_menu_00
	page_dep_arr = page_dep_arr_00
	page_climb = page_climb_00
	page_init = page_init_00
	page_ident = page_ident_00
	page_takeoff = page_takeoff_00
	page_approach = page_approach_00
	page_perf = page_perf_00
	page_n1_limit = page_n1_limit_00
	page_pos_init = page_pos_init_00
	page_cruise = page_cruise_00
	page_descent = page_descent_00
	page_route = page_route_00
	page_legs = page_legs_00
	page_descent_forecast = page_descent_forecast_00
	page_rte_init = page_rte_init_00
	page_dep = page_dep_00
	page_arr = page_arr_00
	page_progress = page_progress_00
	page_hold = page_hold_00
	if page_sel_wpt_00 ~= 0 or page_sel_wpt2_00 ~= 0 or page_sel_wpt3_00 ~= 0 or page_sel_wpt4_00 ~= 0 then
		page_menu = 1
	end
	page_offset = page_offset_00
	page_ref_nav_data = page_ref_nav_data_00
	page_ref_nav_data_wpt = page_ref_nav_data_wpt_00
	page_ref_nav_data_navaid = page_ref_nav_data_navaid_00
	page_ref_nav_data_apt = page_ref_nav_data_apt_00
	page_ref_sel = page_ref_sel_00
	page_fix = page_fix_00
	page_rte_legs = page_rte_legs_00
	page_ac = page_ac_00
	page_ac_atis = page_ac_atis_00
	page_ac_msg = page_ac_msg_00
	page_nearest_apt = page_nearest_apt_00
	page_altern_apt = page_altern_apt_00
	page_nav_status = page_nav_status_00
	page_atc = page_atc_00
	page_fmc_comm = page_fmc_comm_00
	page_dl_menu = page_dl_menu_00
	page_dl_acars = page_dl_acars_00
	page_dl_requests = page_dl_requests_00
	page_dl_wxr_req = page_dl_wxr_req_00
	page_dl_wxr_info = page_dl_wxr_info_00
	page_dl_fltlog = page_dl_fltlog_00
	page_dl_sensors = page_dl_sensors_00
	
	act_page2 = 1
	max_page2 = 1
	page_menu2 = page_menu_01
	page_dep_arr2 = page_dep_arr_01
	page_climb2 = page_climb_01
	page_init2 = page_init_01
	page_ident2 = page_ident_01
	page_takeoff2 = page_takeoff_01
	page_approach2 = page_approach_01
	page_perf2 = page_perf_01
	page_n1_limit2 = page_n1_limit_01
	page_pos_init2 = page_pos_init_01
	page_cruise2 = page_cruise_01
	page_descent2 = page_descent_01
	page_route2 = page_route_01
	page_legs2 = page_legs_01
	page_descent_forecast2 = page_descent_forecast_01
	page_rte_init2 = page_rte_init_01
	page_dep2 = page_dep_01
	page_arr2 = page_arr_01
	page_progress2 = page_progress_01
	page_hold2 = page_hold_00
	if page_sel_wpt_01 ~= 0 or page_sel_wpt2_01 ~= 0 or page_sel_wpt3_01 ~= 0 or page_sel_wpt4_01 ~= 0 then
		page_menu2 = 1
	end
	page_offset2 = page_offset_01
	page_ref_nav_data2 = page_ref_nav_data_01
	page_ref_nav_data_wpt2 = page_ref_nav_data_wpt_01
	page_ref_nav_data_navaid2 = page_ref_nav_data_navaid_01
	page_ref_nav_data_apt2 = page_ref_nav_data_apt_01
	page_ref_sel2 = page_ref_sel_01
	page_fix2 = page_fix_01
	page_rte_legs2 = page_rte_legs_01
	page_ac2 = page_ac_01
	page_ac_atis2 = page_ac_atis_01
	page_ac_msg2 = page_ac_msg_01
	page_nearest_apt2 = page_nearest_apt_01
	page_altern_apt2 = page_altern_apt_01
	page_nav_status2 = page_nav_status_01
	page_atc2 = page_atc_01
	page_fmc_comm2 = page_fmc_comm_01
	page_dl_menu2 = page_dl_menu_01
	page_dl_acars2 = page_dl_acars_01
	page_dl_requests2 = page_dl_requests_01
	page_dl_wxr_req2 = page_dl_wxr_req_01
	page_dl_wxr_info2 = page_dl_wxr_info_01
	page_dl_fltlog2 = page_dl_fltlog_01
	page_dl_sensors2 = page_dl_sensors_01
end

function B738_flight_phase3()

	local alt_temp = 0
	local descent_active = 0
	local ii = 0
	local last_fma = 0
	local del_des_alt_rest = true
	
	local execute_state = legs_delete + des_star_exec + des_star_tns_exec + des_app_exec + des_app_tns_exec
	if legs_delete_old ~= 0 and execute_state == 0 then
		legs_executed = 1
	end
	legs_delete_old = legs_delete + des_star_exec + des_star_tns_exec + des_app_exec + des_app_tns_exec
	
	local use_des_icao_alt = 0
	local rnw_alt_found = 0
	local rnw_temp = ""
	local jj = 0
	local kk = 0
	
	if rte12_act == 0 then
		use_des_icao_alt = des_icao_alt
	elseif rte12_act == 1 then
		use_des_icao_alt = des_icao_alt7
	end
	
	--local elevation_ft = simDR_elevation_m * 3.28 --ft
		-- if altitude_last >= crz_alt_num and crz_alt_num > 0 then
			-- altitude_last = crz_alt_num
		-- end
		if B738DR_flight_phase < 7 then
			ga_data_deleted = false
		end
		
		
		if after_load_flight_TMR == 0 then
			if crz_alt_num > 0 then
				if B738DR_flight_phase == 4 then
					-- cruise descent
					if altitude_last > elevation_ft then
						altitude_last = elevation_ft
					end
				elseif B738DR_flight_phase == 2 and (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and B738DR_fms_descent_now < 2 then
					-- cruise mode
					if B738DR_preselected_alt == crz_alt_num and math.abs(simDR_altitude_pilot - crz_alt_num) < 20 then
						altitude_last = elevation_ft
					end
				elseif B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8 then
					if altitude_last < elevation_ft then
						altitude_last = elevation_ft
					end
				-- else
					-- if altitude_last < elevation_ft then
						-- altitude_last = elevation_ft
					-- end
				end
			else
				if altitude_last < elevation_ft then
					altitude_last = elevation_ft
				end
			end
			
			if B738DR_flight_phase < 4 then
				alt_temp = elevation_ft + 600	--700	--1000
				if altitude_last > alt_temp then
					descent_active = 1
				end
			end
		end
		
		-- B738DR_fms_test3 = descent_active
		
		if simDR_radio_height_pilot_ft > 15 then
			if in_flight_mode == 0 and B738DR_flight_phase <= 1 then
				switch_fmc_page(1)
			end
			in_flight_mode = 1
		elseif simDR_radio_height_pilot_ft <= 0 then
			in_flight_mode = 0
		end
		
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			
			-- reset FMC
			if reset_fmc_act == 1 and simDR_ground_spd < 10.3 then	--and B738DR_flight_phase > 0 then	-- AP_airspeed < 20 kts
			--if reset_fmc_act == 1 and simDR_airspeed_pilot < 20 then
				landing_reset()
				legs_num = 0
				-- legs_num2 = 0
				legs_data = {}
				-- legs_data2 = {}
				offset = 0
				offset_seq = 0
				msg_nav_data = 2
			end
			
			if reset_fmc_act2 == 1 and simDR_ground_spd < 25.7 then	-- < 50kts
			-- if reset_fmc_act2 == 1 and simDR_airspeed_pilot < 50 then
				B738_init_vspd()
			end
			
			-- flight phase TAKEOFF
			-- if is_timer_scheduled(on_ground_25sec) == false and takeoff_enable == 0 and AP_airspeed < 50 then
				-- run_after_time(on_ground_25sec, 25)	-- 25 seconds on the ground
			-- end
			--if takeoff_enable == 0 and simDR_ground_spd < 10.3 then	--and is_timer_scheduled(on_ground_25sec) == false then	--or 20 kts???
			if takeoff_enable == 0 and simDR_airspeed_pilot < 20 then
				run_after_time(on_ground_25sec, 5)	-- 5 seconds on the ground
			end
			if takeoff_enable == 1 then
				climb_enable = 1
				B738DR_flight_phase = 0
				B738DR_missed_app_act = 0
				in_flight_mode = 0
				missed_app_alt_change = 0
			end
			altitude_last = elevation_ft
			goaround_altitude_last = 41000
			B738DR_lift_timer = 0
			nd_vert_path_enable = false
		else
			
			if B738DR_missed_app_act ~= 0 then
				if B738DR_flight_phase >= 2 and B738DR_flight_phase <= 4 then
					-- cruise phase
					B738DR_missed_app_act = 0
				-- elseif B738DR_flight_phase == 8 then
					-- -- not TOGA
					-- if B738DR_altitude_mode < 9 or B738DR_altitude_mode > 10 then
						-- B738DR_flight_phase = 1
						-- B738DR_missed_app_act = 0
					-- end
				end
			end
			
			if B738DR_aerodrom_altitude == -9999 then
				B738DR_aerodrom_altitude = simDR_altitude_pilot
			end
			
			if B738DR_lift_timer < 150 then
				B738DR_lift_timer = B738DR_lift_timer + SIM_PERIOD
			end
			
			if B738DR_flight_phase == 0 then
				-- flight phase CLIMB
				alt_temp = clb_alt_num
				if B738DR_thr_cutback ~= 0 then
					alt_temp = math.min(alt_temp, cut_alt_num)
				end
				missed_app_alt_change = 0
				-- if simDR_radio_height_pilot_ft > alt_temp and climb_enable == 1 then
				if (simDR_altitude_pilot >= B738DR_aerodrom_altitude + alt_temp or B738DR_lift_timer >= 150) and climb_enable == 1 then
					takeoff_enable = 0
					B738DR_flight_phase = 1
					missed_app_alt_change = 0
				end
				nd_vert_path_enable = false
			end
			
			if legs_executed ~= 0 then
				if B738DR_flight_phase > 4 and B738DR_flight_phase ~= 8 then
					if offset <= td_idx and B738DR_vnav_td_dist > 0.5 and calc_rte_enable == 0 then
						if crz_alt_num > 0 then
							alt_temp = math.abs(crz_alt_num - simDR_altitude_pilot)
							if alt_temp < 100 then
								B738DR_flight_phase = 2
								B738DR_fms_descent_now = 0
								altitude_last = elevation_ft	--simDR_altitude_pilot
								descent_active = 0
								vnav_update = 1
								nd_vert_path_enable = false
							elseif offset < tc_idx then
								B738DR_flight_phase = 1
								missed_app_alt_change = 0
								B738DR_fms_descent_now = 0
								altitude_last = elevation_ft	--simDR_altitude_pilot
								descent_active = 0
								vnav_update = 1
								nd_vert_path_enable = false
							elseif offset == tc_idx and B738DR_fmc_dist > tc_dist then
								B738DR_flight_phase = 1
								missed_app_alt_change = 0
								B738DR_fms_descent_now = 0
								altitude_last = elevation_ft	--simDR_altitude_pilot
								descent_active = 0
								vnav_update = 1
								nd_vert_path_enable = false
							end
						end
					end
				end
			end
			
			if crz_alt_changed == 0 and calc_rte_enable == 0 and legs_executed == 0 then
				if td_idx == 0 then
					if legs_num > 0 and (B738DR_flight_phase == 1 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
						B738DR_fms_descent_now = 3
						B738DR_flight_phase = 5
						takeoff_enable = 0
						climb_enable = 0
					elseif B738DR_flight_phase == 4 then
						alt_temp = math.abs(simDR_altitude_pilot - crz_alt_num)
						if crz_alt_num > 0 and alt_temp < 100 then
							B738DR_flight_phase = 2
							altitude_last = elevation_ft	--simDR_altitude_pilot
							nd_vert_path_enable = false
						end
					else
						if descent_active == 1 and B738DR_flight_phase < 5 then
							B738DR_flight_phase = 5
							B738DR_fms_descent_now = 3
							takeoff_enable = 0
							climb_enable = 0
						end
					end
				else
					-- B738DR_fms_test4 = B738DR_mcp_alt_dial
					-- B738DR_fms_test3 = 1
					
					if B738DR_flight_phase == 1 or B738DR_flight_phase == 8 then
						if simDR_altitude_pilot > crz_alt_num and crz_alt_num ~= 0 then
							B738DR_flight_phase = 2
							nd_vert_path_enable = false
						end
					end
					
					-- B738DR_fms_test3 = 2
					
					if offset <= td_idx and B738DR_vnav_td_dist > 0.5 and B738DR_flight_phase ~= 8 then	-- before T/D   > 0
						nd_vert_path_enable = false
						-- B738DR_fms_test3 = 3
						if B738DR_fms_descent_now < 2 then
							-- B738DR_fms_test3 = 4
							if crz_alt_num > 0 then
								-- B738DR_fms_test3 = 5
								-- flight phase CRUISE
								-- alt_temp = crz_alt_num - 100	--70
								alt_temp = crz_alt_num - 100
								if B738DR_flight_phase == 4 then
									alt_temp = crz_alt_num + 100
									if simDR_altitude_pilot <= alt_temp then
										if B738DR_autopilot_vnav_status == 0 then
											B738DR_flight_phase = 2
											altitude_last = elevation_ft	--simDR_altitude_pilot
											-- descent_active = 0
										end
									end
								elseif simDR_altitude_pilot >= alt_temp then
									takeoff_enable = 0
									climb_enable = 0
									if B738DR_autopilot_vnav_status == 0 then
										if B738DR_flight_phase ~= 2 then
											B738DR_flight_phase = 2
											altitude_last = elevation_ft	--simDR_altitude_pilot
											-- descent_active = 0
										end
									else
										if B738DR_flight_phase ~= 3 and B738DR_flight_phase ~= 4 and B738DR_flight_phase ~= 2 then
											B738DR_flight_phase = 2
											altitude_last = elevation_ft	--simDR_altitude_pilot
											-- descent_active = 0
										end
									end
								end
							end
							if descent_active == 1 and B738DR_flight_phase < 5 then
								-- flight phase DESCENT
								B738DR_flight_phase = 5
								takeoff_enable = 0
								climb_enable = 0
								B738DR_fms_descent_now = 3
							end
						else
							if B738DR_flight_phase < 5 then
								-- flight phase DESCENT
								takeoff_enable = 0
								climb_enable = 0
								B738DR_flight_phase = 5
								B738DR_fms_descent_now = 3
							end
						end
					-- else
					-- elseif B738DR_vnav_td_dist == 0 and B738DR_flight_phase ~= 8 and B738DR_autopilot_vnav_status == 0 then
					-- elseif B738DR_vnav_td_dist == 0 and B738DR_flight_phase ~= 8 and B738DR_mcp_alt_dial < crz_alt_num then
					elseif B738DR_vnav_td_dist <= 0 then
						-- B738DR_fms_test3 = 6
						if B738DR_flight_phase < 5 and B738DR_mcp_alt_dial < crz_alt_num and B738DR_missed_app_act == 0 then
							-- B738DR_fms_test3 = 7
								nd_vert_path_enable = true
								if B738DR_altitude_mode == 1 or B738DR_altitude_mode == 2 then
									--V/S or LVL CHG
									B738DR_flight_phase = 5
									B738DR_fms_descent_now = 3
									takeoff_enable = 0
									climb_enable = 0
								end
								if B738DR_autopilot_vnav_status ~= 0 then
									if simDR_ap_altitude_dial_ft >= B738DR_fmc_cruise_alt and (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) then
										if B738DR_vnav_alt_disable ~= 0 then
											B738DR_activate_pitch_mode = 3
										else
											if B738DR_vnavalt_althld ~= 0 then
												-- ALT HLD
												B738DR_activate_pitch_mode = 3
											else
												-- VNAV ALT
												vnav_alt_mode = 1
											end
										end
									end
								end
							-- end
						elseif B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8 then
							-- B738DR_fms_test3 = 8
							if B738DR_autopilot_vnav_status ~= 0 then
								if B738DR_vnav_alt_disable ~= 0 then
									B738DR_activate_pitch_mode = 3
								else
									if B738DR_vnavalt_althld ~= 0 then
										-- ALT HLD
										B738DR_activate_pitch_mode = 3
									else
										-- VNAV ALT
										vnav_alt_mode = 1
									end
								end
							end
							B738DR_flight_phase = 5
							B738DR_fms_descent_now = 3
							takeoff_enable = 0
							climb_enable = 0
						end
					elseif descent_active == 1 and B738DR_flight_phase < 5 then
						-- B738DR_fms_test3 = 9
						B738DR_flight_phase = 5
						B738DR_fms_descent_now = 3
						takeoff_enable = 0
						climb_enable = 0
					end
				end
			else
				crz_alt_changed = 0
			end
				-- crz_alt_changed = 0
			
			if B738DR_missed_app_act ~= 0 then
				if B738DR_flight_phase >= 2 and B738DR_flight_phase <= 4 then
					-- cruise phase
					B738DR_missed_app_act = 0
				end
			end
			
			if B738DR_flight_phase == 1 or B738DR_flight_phase == 3 or B738DR_flight_phase == 4 then
				nd_vert_path_enable = false
			end
			
			-- flight phase Descent
			if B738DR_flight_phase == 5 then
				if B738DR_missed_app_act == 0 then
					if offset > first_app_idx and offset > 0 and first_app_idx > 0 then
						B738DR_flight_phase = 6
					elseif offset == first_app_idx and offset > 0 and first_app_idx > 0 and B738DR_fmc_dist < 2 then
						B738DR_flight_phase = 6
					elseif simDR_altitude_pilot < (use_des_icao_alt + 2000) or simDR_radio_height_pilot_ft < 2000 then
						B738DR_flight_phase = 6
					end
				else
					B738DR_flight_phase = 6
				end
				nd_vert_path_enable = true
			end
			
			-- flight phase Approach
			if B738DR_flight_phase == 6 then
				
				nd_vert_path_enable = true
				
				if simDR_radio_height_pilot_ft < 2000 then
					B738DR_flight_phase = 7
				end
			end
			
			-- flight phase Go-Around armed
			if B738DR_flight_phase == 7 then
				
				nd_vert_path_enable = true
				
				if goaround_altitude_last > elevation_ft then
					goaround_altitude_last = elevation_ft	--simDR_altitude_pilot
				end
				
				if B738DR_fd_goaround > 0 or B738DR_ap_goaround > 0 or elevation_ft > goaround_altitude_last + 800 then
					B738DR_flight_phase = 8
					if B738DR_missed_app_act == 0 then
						missed_app_act_idx = first_miss_app_idx
						B738DR_missed_app_act = 1
						-- if simDR_radio_height_pilot_ft < 2000 then
							crz_alt_num = math.max(B738DR_missed_app_alt, B738DR_mcp_alt_dial, use_des_icao_alt + 1500)
							crz_alt_num2 = 0
							B738DR_fmc_cruise_alt = crz_alt_num
							if crz_alt_num >= B738DR_trans_alt_mod then
								crz_alt = "FL" .. string.format("%03d", crz_alt_num / 100)
							else
								crz_alt = string.format("%5d", crz_alt_num)
							end
							B738_calc_vnav_spd()
							missed_app_alt_change = 2
							
							if legs_num > 0 then
								if rte12_act == 0 then
									ref_icao_x = des_icao
									ref_icao_lat_x = des_icao_lat
									ref_icao_lon_x = des_icao_lon
									ref_tns_alt_x = des_tns_alt
									ref_tns_lvl_x = des_tns_lvl
									trans_lvl_mod = trans_lvl
									
									ref_icao = ref_icao_x
									ref_icao_lat = ref_icao_lat_x
									ref_icao_lon = ref_icao_lon_x
									ref_tns_alt = ref_tns_alt_x
									ref_tns_lvl = ref_tns_lvl_x
									ref_icao_alt = ref_icao_alt_x
									
									ref_rwy2 = des_rnw
									
									ref_rwy = ref_rwy2
									ref_rwy3 = ref_rwy2
									ref_sid2 = "------"
									ref_sid_tns2 = "------"
									
									ref_rwy = ref_rwy2
									ref_sid = ref_sid2
									ref_sid_tns = ref_sid_tns2
									
									sel_ref_rwy2 = ref_rwy2
									sel_ref_sid2 = ref_sid2
									sel_ref_sid_tns2 = ref_sid_tns2
									
									legs_data[1][17] = 0
									if legs_num2 > 0 then
										legs_data2[1][17] = 0
									end
									
									find_ref_rnw_data(1, 0)
									read_ref_list_icao(ref_icao_x, 0)
									
									if ref_rwy ~= "-----" then
										ref_rnw_length_crs(0)
										-- find ref runway alt
										if ref_rwy_num > 0 then
											if string.len(ref_rwy) == 2 then
												rnw_temp = ref_rwy .. " "
											else
												rnw_temp = ref_rwy
											end
											for jj = 1, ref_rwy_num do
												if ref_data_rwy[jj][1] == rnw_temp then
													kk = tonumber(ref_data_rwy[jj][2])
													if kk ~= nil then
														B738DR_ref_rwy_altitude = kk
														rnw_alt_found = 1
													end
													break
												end
											end
										end
									end
									if rnw_alt_found == 0 then
										B738DR_ref_rwy_altitude = ref_icao_alt
									end
								elseif legs_num8 > 0 then
									ref_icao7_x = des_icao7
									ref_icao_lat7_x = des_icao_lat7
									ref_icao_lon7_x = des_icao_lon7
									ref_tns_alt7_x = des_tns_alt7
									ref_tns_lvl7_x = des_tns_lvl7
									trans_lvl_mod2 = trans_lvl7
									
									ref_icao7 = ref_icao7_x
									ref_icao_lat7 = ref_icao_lat7_x
									ref_icao_lon7 = ref_icao_lon7_x
									ref_tns_alt7 = ref_tns_alt7_x
									ref_tns_lvl7 = ref_tns_lvl7_x
									ref_icao_alt7 = ref_icao_alt7_x
									
									ref_rwy8 = des_rnw7
									
									ref_rwy7 = ref_rwy8
									ref_rwy9 = ref_rwy8
									ref_sid8 = "------"
									ref_sid_tns8 = "------"
									
									ref_rwy7 = ref_rwy8
									ref_sid7 = ref_sid8
									ref_sid_tns7 = ref_sid_tns8
									
									sel_ref_rwy8 = ref_rwy8
									sel_ref_sid8 = ref_sid8
									sel_ref_sid_tns8 = ref_sid_tns8
									
									legs_data[1][17] = 0
									if legs_num2 > 0 then
										legs_data8[1][17] = 0
									end
									
									find_ref_rnw_data(1, 1)
									read_ref_list_icao(ref_icao7_x, 1)
									
									if ref_rwy7 ~= "-----" then
										ref_rnw_length_crs(1)
										-- find ref runway alt
										if ref_rwy_num > 0 then
											if string.len(ref_rwy7) == 2 then
												rnw_temp = ref_rwy7 .. " "
											else
												rnw_temp = ref_rwy7
											end
											for jj = 1, ref_rwy_num do
												if ref_data_rwy[jj][1] == rnw_temp then
													kk = tonumber(ref_data_rwy[jj][2])
													if kk ~= nil then
														B738DR_ref_rwy_altitude = kk
														rnw_alt_found = 1
													end
													break
												end
											end
										end
									end
									if rnw_alt_found == 0 then
										B738DR_ref_rwy_altitude = ref_icao_alt7
									end
								end
							end
						-- end
					end
				end
				if B738DR_missed_app_act == 2 then
					-- B738DR_flight_phase = 8
					if missed_app_alt_change == 0 then
						missed_app_act_idx = first_miss_app_idx
						missed_app_alt_change = 1
						-- if simDR_radio_height_pilot_ft < 2000 then
							crz_alt_num = math.max(B738DR_missed_app_alt, use_des_icao_alt + 1500)
							crz_alt_num2 = 0
							B738DR_fmc_cruise_alt = crz_alt_num
							if crz_alt_num >= B738DR_trans_alt_mod then
								crz_alt = "FL" .. string.format("%03d", crz_alt_num / 100)
							else
								crz_alt = string.format("%5d", crz_alt_num)
							end
							B738_calc_vnav_spd()
							missed_app_alt_change = 2
							
							if legs_num > 0 then
								if rte12_act == 0 then
									ref_icao_x = des_icao
									ref_icao_lat_x = des_icao_lat
									ref_icao_lon_x = des_icao_lon
									ref_tns_alt_x = des_tns_alt
									ref_tns_lvl_x = des_tns_lvl
									if des_tns_alt == 0 then
										trans_alt_mod = "-----"
									else
										trans_alt_mod = string.format("%5d", des_tns_alt)
									end
									
									ref_icao = ref_icao_x
									ref_icao_lat = ref_icao_lat_x
									ref_icao_lon = ref_icao_lon_x
									ref_tns_alt = ref_tns_alt_x
									ref_tns_lvl = ref_tns_lvl_x
									ref_icao_alt = ref_icao_alt_x
									trans_alt = trans_alt_mod
									
									ref_rwy2 = des_rnw
									
									ref_rwy = ref_rwy2
									ref_rwy3 = ref_rwy2
									ref_sid2 = "------"
									ref_sid_tns2 = "------"
									
									ref_rwy = ref_rwy2
									ref_sid = ref_sid2
									ref_sid_tns = ref_sid_tns2
									
									sel_ref_rwy2 = ref_rwy2
									sel_ref_sid2 = ref_sid2
									sel_ref_sid_tns2 = ref_sid_tns2
									
									legs_data[1][17] = 0
									if legs_num2 > 0 then
										legs_data2[1][17] = 0
									end
									
									find_ref_rnw_data(1, 0)
									read_ref_list_icao(ref_icao_x, 0)
									
									if ref_rwy ~= "-----" then
										ref_rnw_length_crs(0)
										-- find ref runway alt
										if ref_rwy_num > 0 then
											if string.len(ref_rwy) == 2 then
												rnw_temp = ref_rwy .. " "
											else
												rnw_temp = ref_rwy
											end
											for jj = 1, ref_rwy_num do
												if ref_data_rwy[jj][1] == rnw_temp then
													kk = tonumber(ref_data_rwy[jj][2])
													if kk ~= nil then
														B738DR_ref_rwy_altitude = kk
														rnw_alt_found = 1
													end
													break
												end
											end
										end
									end
									if rnw_alt_found == 0 then
										B738DR_ref_rwy_altitude = ref_icao_alt
									end
								else
									ref_icao7_x = des_icao7
									ref_icao_lat7_x = des_icao_lat7
									ref_icao_lon7_x = des_icao_lon7
									ref_tns_alt7_x = des_tns_alt7
									ref_tns_lvl7_x = des_tns_lvl7
									if des_tns_alt7 == 0 then
										trans_alt_mod2 = "-----"
									else
										trans_alt_mod2 = string.format("%5d", des_tns_alt7)
									end
									
									ref_icao7 = ref_icao7_x
									ref_icao_lat7 = ref_icao_lat7_x
									ref_icao_lon7 = ref_icao_lon7_x
									ref_tns_alt7 = ref_tns_alt7_x
									ref_tns_lvl7 = ref_tns_lvl7_x
									ref_icao_alt7 = ref_icao_alt7_x
									trans_alt7 = trans_alt_mod2
									
									ref_rwy8 = des_rnw7
									
									ref_rwy7 = ref_rwy8
									ref_rwy9 = ref_rwy8
									ref_sid8 = "------"
									ref_sid_tns8 = "------"
									
									ref_rwy7 = ref_rwy8
									ref_sid7 = ref_sid8
									ref_sid_tns7 = ref_sid_tns8
									
									sel_ref_rwy8 = ref_rwy8
									sel_ref_sid8 = ref_sid8
									sel_ref_sid_tns8 = ref_sid_tns8
									
									legs_data[1][17] = 0
									if legs_num8 > 0 then
										legs_data8[1][17] = 0
									end
									
									find_ref_rnw_data(1, 1)
									read_ref_list_icao(ref_icao7_x, 1)
									
									if ref_rwy7 ~= "-----" then
										ref_rnw_length_crs(1)
										-- find ref runway alt
										if ref_rwy_num > 0 then
											if string.len(ref_rwy7) == 2 then
												rnw_temp = ref_rwy7 .. " "
											else
												rnw_temp = ref_rwy7
											end
											for jj = 1, ref_rwy_num do
												if ref_data_rwy[jj][1] == rnw_temp then
													kk = tonumber(ref_data_rwy[jj][2])
													if kk ~= nil then
														B738DR_ref_rwy_altitude = kk
														rnw_alt_found = 1
													end
													break
												end
											end
										end
									end
									if rnw_alt_found == 0 then
										B738DR_ref_rwy_altitude = ref_icao_alt7
									end
								end
							end
						-- end
					elseif missed_app_alt_change == 1 then
						-- if simDR_radio_height_pilot_ft < 2000 then
							crz_alt_num = math.max(B738DR_missed_app_alt, use_des_icao_alt + 1500)
							crz_alt_num2 = 0
							B738DR_fmc_cruise_alt = crz_alt_num
							if crz_alt_num >= B738DR_trans_alt_mod then
								crz_alt = "FL" .. string.format("%03d", crz_alt_num / 100)
							else
								crz_alt = string.format("%5d", crz_alt_num)
							end
							B738_calc_vnav_spd()
							missed_app_alt_change = 2
							
							if legs_num > 0 then
								if rte12_act == 0 then
									ref_icao_x = des_icao
									ref_icao_lat_x = des_icao_lat
									ref_icao_lon_x = des_icao_lon
									ref_tns_alt_x = des_tns_alt
									ref_tns_lvl_x = des_tns_lvl
									if des_tns_alt == 0 then
										trans_alt_mod = "-----"
									else
										trans_alt_mod = string.format("%5d", des_tns_alt)
									end
									
									ref_icao = ref_icao_x
									ref_icao_lat = ref_icao_lat_x
									ref_icao_lon = ref_icao_lon_x
									ref_tns_alt = ref_tns_alt_x
									ref_tns_lvl = ref_tns_lvl_x
									ref_icao_alt = ref_icao_alt_x
									trans_alt = trans_alt_mod
									
									ref_rwy2 = des_rnw
									
									ref_rwy = ref_rwy2
									ref_rwy3 = ref_rwy2
									ref_sid2 = "------"
									ref_sid_tns2 = "------"
									
									ref_rwy = ref_rwy2
									ref_sid = ref_sid2
									ref_sid_tns = ref_sid_tns2
									
									sel_ref_rwy2 = ref_rwy2
									sel_ref_sid2 = ref_sid2
									sel_ref_sid_tns2 = ref_sid_tns2
									
									legs_data[1][17] = 0
									if legs_num2 > 0 then
										legs_data2[1][17] = 0
									end
									
									find_ref_rnw_data(1, 0)
									read_ref_list_icao(ref_icao_x, 0)
									
									if ref_rwy ~= "-----" then
										ref_rnw_length_crs(0)
										-- find ref runway alt
										if ref_rwy_num > 0 then
											if string.len(ref_rwy) == 2 then
												rnw_temp = ref_rwy .. " "
											else
												rnw_temp = ref_rwy
											end
											for jj = 1, ref_rwy_num do
												if ref_data_rwy[jj][1] == rnw_temp then
													kk = tonumber(ref_data_rwy[jj][2])
													if kk ~= nil then
														B738DR_ref_rwy_altitude = kk
														rnw_alt_found = 1
													end
													break
												end
											end
										end
									end
									if rnw_alt_found == 0 then
										B738DR_ref_rwy_altitude = ref_icao_alt
									end
								else
									ref_icao7_x = des_icao7
									ref_icao_lat7_x = des_icao_lat7
									ref_icao_lon7_x = des_icao_lon7
									ref_tns_alt7_x = des_tns_alt7
									ref_tns_lvl7_x = des_tns_lvl7
									if des_tns_alt7 == 0 then
										trans_alt_mod2 = "-----"
									else
										trans_alt_mod2 = string.format("%5d", des_tns_alt7)
									end
									
									ref_icao7 = ref_icao7_x
									ref_icao_lat7 = ref_icao_lat7_x
									ref_icao_lon7 = ref_icao_lon7_x
									ref_tns_alt7 = ref_tns_alt7_x
									ref_tns_lvl7 = ref_tns_lvl7_x
									ref_icao_alt7 = ref_icao_alt7_x
									trans_alt7 = trans_alt_mod2
									
									ref_rwy8 = des_rnw7
									
									ref_rwy7 = ref_rwy8
									ref_rwy9 = ref_rwy8
									ref_sid8 = "------"
									ref_sid_tns8 = "------"
									
									ref_rwy7 = ref_rwy8
									ref_sid7 = ref_sid8
									ref_sid_tns7 = ref_sid_tns8
									
									sel_ref_rwy8 = ref_rwy8
									sel_ref_sid8 = ref_sid8
									sel_ref_sid_tns8 = ref_sid_tns8
									
									legs_data[1][17] = 0
									if legs_num8 > 0 then
										legs_data8[1][17] = 0
									end
									
									find_ref_rnw_data(1, 1)
									read_ref_list_icao(ref_icao7_x, 1)
									
									if ref_rwy7 ~= "-----" then
										ref_rnw_length_crs(1)
										-- find ref runway alt
										if ref_rwy_num > 0 then
											if string.len(ref_rwy7) == 2 then
												rnw_temp = ref_rwy7 .. " "
											else
												rnw_temp = ref_rwy7
											end
											for jj = 1, ref_rwy_num do
												if ref_data_rwy[jj][1] == rnw_temp then
													kk = tonumber(ref_data_rwy[jj][2])
													if kk ~= nil then
														B738DR_ref_rwy_altitude = kk
														rnw_alt_found = 1
													end
													break
												end
											end
										end
									end
									if rnw_alt_found == 0 then
										B738DR_ref_rwy_altitude = ref_icao_alt7
									end
								end
							end
						-- end
					end
					-- missed_app_alt_change = 1
					
					-- B738DR_fms_test8 = 1
					
					if B738DR_flight_phase ~= 8 then
						if rnav_idx_last ~= 0 and rnav_idx_last <= legs_num then
							if string.sub(legs_data[rnav_idx_last][1], 1, 2) == "RW" then
								B738DR_flight_phase = 8
								-- B738DR_fms_test8 = 2
							elseif offset > rnav_idx_last then
								B738DR_flight_phase = 8
								-- B738DR_fms_test8 = 3
							end
						end
					end
				end
				
				local manual_toga = 0
				if simDR_vvi_fpm_pilot > 600 and simDR_flaps_ratio <= 0.625 then
					if simDR_reverse_act[0] ~= 3 and simDR_reverse_act[1] ~= 3 then
						if simDR_throttle[0] > 0.6 or simDR_throttle[1] > 0.6 then
							if B738DR_ap_on == 1 then
								if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									manual_toga = 1
								end
							else
								manual_toga = 1
							end
						end
					end
				end
				if manual_toga ~= 0 and B738DR_flight_phase ~= 8 then
					B738DR_flight_phase = 8
					if B738DR_missed_app_act == 0 then
						missed_app_act_idx = first_miss_app_idx
						B738DR_missed_app_act = 1
						missed_app_alt_change = 1
						-- if simDR_radio_height_pilot_ft < 2000 then
							crz_alt_num = math.max(B738DR_missed_app_alt, use_des_icao_alt + 1500)
							crz_alt_num2 = 0
							B738DR_fmc_cruise_alt = crz_alt_num
							if crz_alt_num >= B738DR_trans_alt_mod then
								crz_alt = "FL" .. string.format("%03d", crz_alt_num / 100)
							else
								crz_alt = string.format("%5d", crz_alt_num)
							end
							B738_calc_vnav_spd()
							missed_app_alt_change = 2
							
							if rte12_act == 0 then
								ref_icao_x = des_icao
								ref_icao_lat_x = des_icao_lat
								ref_icao_lon_x = des_icao_lon
								ref_tns_alt_x = des_tns_alt
								ref_tns_lvl_x = des_tns_lvl
								if des_tns_alt == 0 then
									trans_alt_mod = "-----"
								else
									trans_alt_mod = string.format("%5d", des_tns_alt)
								end
								
								ref_icao = ref_icao_x
								ref_icao_lat = ref_icao_lat_x
								ref_icao_lon = ref_icao_lon_x
								ref_tns_alt = ref_tns_alt_x
								ref_tns_lvl = ref_tns_lvl_x
								ref_icao_alt = ref_icao_alt_x
								trans_alt = trans_alt_mod
								
								ref_rwy2 = des_rnw
								
								ref_rwy = ref_rwy2
								ref_rwy3 = ref_rwy2
								ref_sid2 = "------"
								ref_sid_tns2 = "------"
								
								ref_rwy = ref_rwy2
								ref_sid = ref_sid2
								ref_sid_tns = ref_sid_tns2
								
								sel_ref_rwy2 = ref_rwy2
								sel_ref_sid2 = ref_sid2
								sel_ref_sid_tns2 = ref_sid_tns2
								
								if legs_num > 0 then
									legs_data[1][17] = 0
								end
								if legs_num2 > 0 then
									legs_data2[1][17] = 0
								end
								
								find_ref_rnw_data(1, 0)
								read_ref_list_icao(ref_icao_x, 0)
								
								if ref_rwy ~= "-----" then
									ref_rnw_length_crs(0)
									-- find ref runway alt
									if ref_rwy_num > 0 then
										if string.len(ref_rwy) == 2 then
											rnw_temp = ref_rwy .. " "
										else
											rnw_temp = ref_rwy
										end
										for jj = 1, ref_rwy_num do
											if ref_data_rwy[jj][1] == rnw_temp then
												kk = tonumber(ref_data_rwy[jj][2])
												if kk ~= nil then
													B738DR_ref_rwy_altitude = kk
													rnw_alt_found = 1
												end
												break
											end
										end
									end
								end
								if rnw_alt_found == 0 then
									B738DR_ref_rwy_altitude = ref_icao_alt
								end
							else
								ref_icao7_x = des_icao7
								ref_icao_lat7_x = des_icao_lat7
								ref_icao_lon7_x = des_icao_lon7
								ref_tns_alt7_x = des_tns_alt7
								ref_tns_lvl7_x = des_tns_lvl7
								if des_tns_alt7 == 0 then
									trans_alt_mod2 = "-----"
								else
									trans_alt_mod2 = string.format("%5d", des_tns_alt7)
								end
								
								ref_icao7 = ref_icao7_x
								ref_icao_lat7 = ref_icao_lat7_x
								ref_icao_lon7 = ref_icao_lon7_x
								ref_tns_alt7 = ref_tns_alt7_x
								ref_tns_lvl7 = ref_tns_lvl7_x
								ref_icao_alt7 = ref_icao_alt7_x
								trans_alt7 = trans_alt_mod2
								
								ref_rwy8 = des_rnw7
								
								ref_rwy7 = ref_rwy8
								ref_rwy9 = ref_rwy8
								ref_sid8 = "------"
								ref_sid_tns8 = "------"
								
								ref_rwy7 = ref_rwy8
								ref_sid7 = ref_sid8
								ref_sid_tns7 = ref_sid_tns8
								
								sel_ref_rwy8 = ref_rwy8
								sel_ref_sid8 = ref_sid8
								sel_ref_sid_tns8 = ref_sid_tns8
								
								if legs_num > 0 then
									legs_data[1][17] = 0
								end
								if legs_num8 > 0 then
									legs_data8[1][17] = 0
								end
								
								find_ref_rnw_data(1, 1)
								read_ref_list_icao(ref_icao7_x, 1)
								
								if ref_rwy7 ~= "-----" then
									ref_rnw_length_crs(1)
									-- find ref runway alt
									if ref_rwy_num > 0 then
										if string.len(ref_rwy7) == 2 then
											rnw_temp = ref_rwy7 .. " "
										else
											rnw_temp = ref_rwy7
										end
										for jj = 1, ref_rwy_num do
											if ref_data_rwy[jj][1] == rnw_temp then
												kk = tonumber(ref_data_rwy[jj][2])
												if kk ~= nil then
													B738DR_ref_rwy_altitude = kk
													rnw_alt_found = 1
												end
												break
											end
										end
									end
								end
								if rnw_alt_found == 0 then
									B738DR_ref_rwy_altitude = ref_icao_alt7
								end
							end
						-- end
					end
				end
				
				if B738DR_flight_phase == 8 then
					
					nd_vert_path_enable = false
					
					-- active climb mode
					-- thrust limit to go-around thrust
					-- all descent restrict alt below current alt are deleted
					if legs_num > 1 then
						last_fma = 0
						del_des_alt_rest = true
						ga_data_deleted = true
						for ii = 2, legs_num do
							if legs_data[ii][17] == 2 then
								del_des_alt_rest = false
							end
							if del_des_alt_rest == true then
								if legs_data[ii][5] < simDR_altitude_pilot then
									if legs_data[ii][6] ~= 42 and legs_data[ii][6] ~= 2 then
										legs_data[ii][5] = 0
										legs_data[ii][6] = 0
									end
								end
							end
							-- change STAR,APP to route
							if legs_data[ii][44] >= 50000 and ii >= offset then
								legs_data[ii][19] = 10
							elseif legs_data[ii][19] == 2 or legs_data[ii][19] == 4 or legs_data[ii][19] == 7 or legs_data[ii][19] == 8 or legs_data[ii][19] == 9 then
								legs_data[ii][19] = 0
							elseif legs_data[ii][19] == 1 or legs_data[ii][19] == 3 then
								legs_data[ii][19] = 0
							end
							-- clear FAF,FixMA
							-- if legs_data[ii][17] ~= 0 then
							if legs_data[ii][17] == 2 then
								last_fma = ii - 1
							end
							legs_data[ii][17] = 0
							legs_data[ii][44] = 0
						end
						-- legs_data[legs_num][19] = 10	-- last wpt after engage Missed Approach
						if last_fma ~= 0 then
							legs_data[last_fma][17] = 4
							legs_data[legs_num][17] = 6
						end
						if last_miss_app_idx == 0 then
							last_sid_idx = legs_num
						else
							last_sid_idx = last_miss_app_idx
						end
						
						copy_to_legsdata2(rte12_act)
						if rte12_act == 0 then
							last_sid_idx_mod = legs_num2
						else
							last_sid_idx_mod2 = legs_num8
						end
						B738DR_fms_descent_now = 0
						altitude_last = elevation_ft	--simDR_altitude_pilot
						
						if rte12_act == 0 then
							des_rnw = ""
							des_app = "------"
							des_app_tns = "------"
							des_star = "------"
							des_star_trans = "------"
							des_rnw2 = ""
							des_app2 = "------"
							des_app_tns2 = "------"
							des_star2 = "------"
							des_star_trans2 = "------"
							
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							sel_des_app2 = des_app2
							sel_des_app_tns2 = des_app_tns2
							
							des_star2_old = des_star2
							des_star_trans2_old = des_star_trans2
							des_app2_old = des_app2
							des_app_tns2_old = des_app_tns2
						else
							des_rnw7 = ""
							des_app7 = "------"
							des_app_tns7 = "------"
							des_star7 = "------"
							des_star_trans7 = "------"
							des_rnw8 = ""
							des_app8 = "------"
							des_app_tns8 = "------"
							des_star8 = "------"
							des_star_trans8 = "------"
							
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							sel_des_app8 = des_app8
							sel_des_app_tns8 = des_app_tns8
							
							des_star8_old = des_star8
							des_star_trans8_old = des_star_trans8
							des_app8_old = des_app8
							des_app_tns8_old = des_app_tns8
						end
						
						id_ed = ""
						idx_ed = 0
						idx_iaf = 0
						rnav_idx_first = 0
						rnav_idx_last = 0
						rnav_alt = 0
						rnav_vpa = 0
						
						if rte12_act == 0 then
							idx_ed_mod = 0
							rnav_idx_first_mod = 0
							rnav_idx_last_mod = 0
							rnav_alt_mod = 0
							rnav_vpa_mod = 0
						elseif rte12_act == 1 then
							idx_ed_mod2 = 0
							rnav_idx_first_mod2 = 0
							rnav_idx_last_mod2 = 0
							rnav_alt_mod2 = 0
							rnav_vpa_mod2 = 0
						end
						
						---------------------------------------
						-- calc_rte_enable = 1
						-- B738_calc_rte()
						
						last_fma = B738DR_missed_app_act
						B738_find_rnav()
						B738DR_missed_app_act = last_fma
						
						B738DR_dest_runway_len = 0
						B738DR_dest_runway_crs = 0
						B738DR_dest_runway_start_lat = 0
						B738DR_dest_runway_start_lon = 0
						B738DR_dest_runway_end_lat = 0
						B738DR_dest_runway_end_lon = 0
						B738DR_dest_runway_id = ""
						B738DR_des_rwy_altitude = use_des_icao_alt
						find_id_app_data(rte12_act)
						--------------------------------------
						vnav_update = 1
					end
				end
			else
				-- Flight phase 8:
				
				-- goaround_altitude_last = 41000
				-- missed_app_alt_change = 0
				
				if B738DR_missed_app_act == 2 then
					if missed_app_alt_change == 0 then
						missed_app_act_idx = first_miss_app_idx
						missed_app_alt_change = 1
						
						crz_alt_num = math.max(B738DR_missed_app_alt, use_des_icao_alt + 1500)
						crz_alt_num2 = 0
						B738DR_fmc_cruise_alt = crz_alt_num
						if crz_alt_num >= B738DR_trans_alt_mod then
							crz_alt = "FL" .. string.format("%03d", crz_alt_num / 100)
						else
							crz_alt = string.format("%5d", crz_alt_num)
						end
						B738_calc_vnav_spd()
						missed_app_alt_change = 2
						
						if legs_num > 0 then
							if rte12_act == 0 then
								ref_icao_x = des_icao
								ref_icao_lat_x = des_icao_lat
								ref_icao_lon_x = des_icao_lon
								ref_tns_alt_x = des_tns_alt
								ref_tns_lvl_x = des_tns_lvl
								if des_tns_alt == 0 then
									trans_alt_mod = "-----"
								else
									trans_alt_mod = string.format("%5d", des_tns_alt)
								end
								
								ref_icao = ref_icao_x
								ref_icao_lat = ref_icao_lat_x
								ref_icao_lon = ref_icao_lon_x
								ref_tns_alt = ref_tns_alt_x
								ref_tns_lvl = ref_tns_lvl_x
								ref_icao_alt = ref_icao_alt_x
								trans_alt = trans_alt_mod
								
								ref_rwy2 = des_rnw
								
								ref_rwy = ref_rwy2
								ref_rwy3 = ref_rwy2
								ref_sid2 = "------"
								ref_sid_tns2 = "------"
								
								ref_rwy = ref_rwy2
								ref_sid = ref_sid2
								ref_sid_tns = ref_sid_tns2
								
								sel_ref_rwy2 = ref_rwy2
								sel_ref_sid2 = ref_sid2
								sel_ref_sid_tns2 = ref_sid_tns2
								
								legs_data[1][17] = 0
								if legs_num2 > 0 then
									legs_data2[1][17] = 0
								end
								
								find_ref_rnw_data(1, 0)
								read_ref_list_icao(ref_icao_x, 0)
								
								if ref_rwy ~= "-----" then
									ref_rnw_length_crs(0)
									-- find ref runway alt
									if ref_rwy_num > 0 then
										if string.len(ref_rwy) == 2 then
											rnw_temp = ref_rwy .. " "
										else
											rnw_temp = ref_rwy
										end
										for jj = 1, ref_rwy_num do
											if ref_data_rwy[jj][1] == rnw_temp then
												kk = tonumber(ref_data_rwy[jj][2])
												if kk ~= nil then
													B738DR_ref_rwy_altitude = kk
													rnw_alt_found = 1
												end
												break
											end
										end
									end
								end
								if rnw_alt_found == 0 then
									B738DR_ref_rwy_altitude = ref_icao_alt
								end
							else
								ref_icao7_x = des_icao7
								ref_icao_lat7_x = des_icao_lat7
								ref_icao_lon7_x = des_icao_lon7
								ref_tns_alt7_x = des_tns_alt7
								ref_tns_lvl7_x = des_tns_lvl7
								if des_tns_alt7 == 0 then
									trans_alt_mod2 = "-----"
								else
									trans_alt_mod2 = string.format("%5d", des_tns_alt7)
								end
								
								ref_icao7 = ref_icao7_x
								ref_icao_lat7 = ref_icao_lat7_x
								ref_icao_lon7 = ref_icao_lon7_x
								ref_tns_alt7 = ref_tns_alt7_x
								ref_tns_lvl7 = ref_tns_lvl7_x
								ref_icao_alt7 = ref_icao_alt7_x
								trans_alt7 = trans_alt_mod2
								
								ref_rwy8 = des_rnw7
								
								ref_rwy7 = ref_rwy8
								ref_rwy9 = ref_rwy8
								ref_sid8 = "------"
								ref_sid_tns8 = "------"
								
								ref_rwy7 = ref_rwy8
								ref_sid7 = ref_sid8
								ref_sid_tns7 = ref_sid_tns8
								
								sel_ref_rwy8 = ref_rwy8
								sel_ref_sid8 = ref_sid8
								sel_ref_sid_tns8 = ref_sid_tns8
								
								legs_data[1][17] = 0
								if legs_num8 > 0 then
									legs_data8[1][17] = 0
								end
								
								find_ref_rnw_data(1, 1)
								read_ref_list_icao(ref_icao7_x, 1)
								
								if ref_rwy7 ~= "-----" then
									ref_rnw_length_crs(1)
									-- find ref runway alt
									if ref_rwy_num > 0 then
										if string.len(ref_rwy7) == 2 then
											rnw_temp = ref_rwy7 .. " "
										else
											rnw_temp = ref_rwy7
										end
										for jj = 1, ref_rwy_num do
											if ref_data_rwy[jj][1] == rnw_temp then
												kk = tonumber(ref_data_rwy[jj][2])
												if kk ~= nil then
													B738DR_ref_rwy_altitude = kk
													rnw_alt_found = 1
												end
												break
											end
										end
									end
								end
								if rnw_alt_found == 0 then
									B738DR_ref_rwy_altitude = ref_icao_alt7
								end
							end
						end
					end
					
					
					-- active climb mode
					-- thrust limit to go-around thrust
					-- all descent restrict alt below current alt are deleted
					
					if legs_num > 1 and ga_data_deleted == false then
						last_fma = 0
						del_des_alt_rest = true
						ga_data_deleted = true
						for ii = 2, legs_num do
							if legs_data[ii][17] == 2 then
								del_des_alt_rest = false
							end
							if legs_data[ii][5] < simDR_altitude_pilot then
								if legs_data[ii][6] ~= 42 and legs_data[ii][6] ~= 2 then
									legs_data[ii][5] = 0
									legs_data[ii][6] = 0
								end
							end
							-- change STAR,APP to route
							if legs_data[ii][44] >= 50000 and ii >= offset then
								legs_data[ii][19] = 10
							elseif legs_data[ii][19] == 2 or legs_data[ii][19] == 4 or legs_data[ii][19] == 7 or legs_data[ii][19] == 8 or legs_data[ii][19] == 9 then
								legs_data[ii][19] = 0
							elseif legs_data[ii][19] == 1 or legs_data[ii][19] == 3 then
								legs_data[ii][19] = 0
							end
							-- clear FAF,FixMA
							if legs_data[ii][17] ~= 0 then
								last_fma = ii - 1
							end
							legs_data[ii][17] = 0
							legs_data[ii][44] = 0
						end
						-- legs_data[legs_num][19] = 10	-- last wpt after engage Missed Approach
						if last_fma ~= 0 then
							legs_data[last_fma][17] = 4
							legs_data[legs_num][17] = 6
						end
						if last_miss_app_idx == 0 then
							last_sid_idx = legs_num
						else
							last_sid_idx = last_miss_app_idx
						end
						
						copy_to_legsdata2(rte12_act)
						if rte12_act == 0 then
							last_sid_idx_mod = legs_num2
						else
							last_sid_idx_mod2 = legs_num8
						end
						B738DR_fms_descent_now = 0
						altitude_last = elevation_ft	--simDR_altitude_pilot
						
						if rte12_act == 0 then
							des_rnw = ""
							des_app = "------"
							des_app_tns = "------"
							des_star = "------"
							des_star_trans = "------"
							des_rnw2 = ""
							des_app2 = "------"
							des_app_tns2 = "------"
							des_star2 = "------"
							des_star_trans2 = "------"
							
							sel_des_star2 = des_star2
							sel_des_star_trans2 = des_star_trans2
							sel_des_app2 = des_app2
							sel_des_app_tns2 = des_app_tns2
							
							des_star2_old = des_star2
							des_star_trans2_old = des_star_trans2
							des_app2_old = des_app2
							des_app_tns2_old = des_app_tns2
						else
							des_rnw7 = ""
							des_app7 = "------"
							des_app_tns7 = "------"
							des_star7 = "------"
							des_star_trans7 = "------"
							des_rnw8 = ""
							des_app8 = "------"
							des_app_tns8 = "------"
							des_star8 = "------"
							des_star_trans8 = "------"
							
							sel_des_star8 = des_star8
							sel_des_star_trans8 = des_star_trans8
							sel_des_app8 = des_app8
							sel_des_app_tns8 = des_app_tns8
							
							des_star8_old = des_star8
							des_star_trans8_old = des_star_trans8
							des_app8_old = des_app8
							des_app_tns8_old = des_app_tns8
						end
						
						id_ed = ""
						idx_ed = 0
						idx_iaf = 0
						rnav_idx_first = 0
						rnav_idx_last = 0
						rnav_alt = 0
						rnav_vpa = 0
						
						if rte12_act == 0 then
							idx_ed_mod = 0
							rnav_idx_first_mod = 0
							rnav_idx_last_mod = 0
							rnav_alt_mod = 0
							rnav_vpa_mod = 0
						elseif rte12_act == 1 then
							idx_ed_mod2 = 0
							rnav_idx_first_mod2 = 0
							rnav_idx_last_mod2 = 0
							rnav_alt_mod2 = 0
							rnav_vpa_mod2 = 0
						end
						
						---------------------------------------
						-- calc_rte_enable = 1
						-- B738_calc_rte()
						last_fma = B738DR_missed_app_act
						B738_find_rnav()
						B738DR_missed_app_act = last_fma
						
						B738DR_dest_runway_len = 0
						B738DR_dest_runway_crs = 0
						B738DR_dest_runway_start_lat = 0
						B738DR_dest_runway_start_lon = 0
						B738DR_dest_runway_end_lat = 0
						B738DR_dest_runway_end_lon = 0
						B738DR_dest_runway_id = ""
						B738DR_des_rwy_altitude = use_des_icao_alt
						find_id_app_data(rte12_act)
						--------------------------------------
						
						vnav_update = 1
					end
					
					
					-- flight phase CLIMB
					alt_temp = clb_alt_num
					if B738DR_thr_cutback ~= 0 then
						alt_temp = math.min(alt_temp, cut_alt_num)
					end
					-- missed_app_alt_change = 0
					-- if simDR_altitude_pilot >= B738DR_aerodrom_altitude + alt_temp then
					if simDR_radio_height_pilot_ft > alt_temp then
						B738DR_flight_phase = 1
						missed_app_alt_change = 0
					end
					-- B738DR_flight_phase = 1
					
					B738DR_fms_descent_now = 0
					altitude_last = elevation_ft	--simDR_altitude_pilot
					descent_active = 0
					-- vnav_update = 1
					takeoff_enable = 0
					climb_enable = 1
					
					nd_vert_path_enable = false
				end
				
				goaround_altitude_last = 41000
				-- missed_app_alt_change = 0
				
			end
			
			
			if after_load_flight_TMR == 0 then
				-- flight phase Go-Around
				if B738DR_flight_phase == 8 then
					if B738DR_fd_goaround == 0 and B738DR_ap_goaround == 0 then
						if B738DR_missed_app_alt > 0 and simDR_altitude_pilot > (B738DR_missed_app_alt - 200) then
							B738DR_flight_phase = 1	-- climb phase
							altitude_last = elevation_ft	--simDR_altitude_pilot
							missed_app_alt_change = 0
						elseif use_des_icao_alt > 0 and simDR_altitude_pilot > (use_des_icao_alt + 1500) and simDR_vvi_fpm_pilot > 600 then
							B738DR_flight_phase = 1	-- climb phase
							altitude_last = elevation_ft	--simDR_altitude_pilot
							missed_app_alt_change = 0
						end
					end
				end
			end
			if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
				if B738DR_fms_descent_now == 2 and simDR_altitude_pilot < 5000 then
					B738DR_fms_descent_now = 3
				end
			end
		
		end
		
		if after_load_flight_TMR == 0 then
			if crz_alt_num > 0 then
				-- if simDR_altitude_pilot <= crz_alt_num and altitude_last < elevation_ft then
				if altitude_last < elevation_ft and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
					altitude_last = elevation_ft	--simDR_altitude_pilot
				end
			else
				if altitude_last < elevation_ft then
					altitude_last = elevation_ft	--simDR_altitude_pilot
				end
			end
			if B738DR_missed_app_act > 0 then
				altitude_last = elevation_ft	--simDR_altitude_pilot
			end
		end
		
		if calc_rte_enable == 0 then
			legs_executed = 0
		end
	
	if rte12_act == 0 then
		if B738_legs_num ~= 0 and des_icao ~= "****" then
			B738DR_aerodrom_altitude = B738DR_pfd_rwy_altitude
		elseif simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			B738DR_aerodrom_altitude = -9999
		end
	else
		if B738_legs_num ~= 0 and des_icao7 ~= "****" then
			B738DR_aerodrom_altitude = B738DR_pfd_rwy_altitude
		elseif simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			B738DR_aerodrom_altitude = -9999
		end
	end
	
	--DR_test = altitude_last
	
	if simDR_radio_height_pilot_ft > 150 then
		if reset_fmc_act == 0 then
			reset_fmc_act = 1
		end
		if reset_fmc_act2 == 0 then
			reset_fmc_act2 = 1
		end
	end
	
	if flight_phase_old ~= B738DR_flight_phase then
		if B738DR_autopilot_vnav_status ~= 0 then
			if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
				if flight_phase_old < 2 or flight_phase_old == 8 then
					if B738DR_vnav_alt_disable ~= 0 then
						B738DR_activate_pitch_mode = 3
					else
						if B738DR_vnavalt_althld ~= 0 then
							-- ALT HLD
							B738DR_activate_pitch_mode = 3
						else
							-- VNAV ALT
							vnav_alt_mode = 1
						end
					end
				end
			elseif B738DR_flight_phase == 1 or B738DR_flight_phase == 8 then
				if flight_phase_old > 4 and flight_phase_old < 8 then
					if B738DR_vnav_alt_disable ~= 0 then
						B738DR_activate_pitch_mode = 3
					else
						if B738DR_vnavalt_althld ~= 0 then
							-- ALT HLD
							B738DR_activate_pitch_mode = 3
						else
							-- VNAV ALT
							vnav_alt_mode = 1
						end
					end
				end
			end
		end
		if B738DR_flight_phase == 2 then
			switch_fmc_page(2)
		elseif B738DR_flight_phase == 5 then
			local disable_switch = 0
			if engout_crz > 1 and (B738DR_vnav_td_dist > 0 or simDR_altitude_pilot > B738DR_cont_lvl - 500) then
				disable_switch = 1
			end
			if disable_switch == 0 then
				switch_fmc_page(3)
			end
		elseif flight_phase_old < 5 and (B738DR_flight_phase == 6 or B738DR_flight_phase == 7) then
			local disable_switch = 0
			if engout_crz > 1 and (B738DR_vnav_td_dist > 0 or simDR_altitude_pilot > B738DR_cont_lvl - 500) then
				disable_switch = 1
			end
			if disable_switch == 0 then
				switch_fmc_page(3)
			end
		end
	end
	flight_phase_old = B738DR_flight_phase
	

end


function B738_N1_thrust_set()
	fmc_takeoff_mode = 1
	fmc_goaround_mode = 1
	fmc_cruise_mode = 1
	fmc_climb_mode = 1
	fmc_cont_mode = 1
	local alt_thrust = 0
	local alt_thrust_ratio = 0
	
	if simDR_altitude_pilot > 15000 then
		clb = "<SEL>"
		clb_1 = "     "
		clb_2 = "     "
	end
	
	local fmc_auto_mode = -1
	
	-- mode GO AROUND
	if B738DR_flight_phase >= 7 or B738DR_altitude_mode == 9 or B738DR_altitude_mode == 10 or B738DR_altitude_mode == 12 
	or B738DR_fd_goaround > 0 or B738DR_ap_goaround > 0 then
		fms_N1_mode = 11	-- mode GA
		fmc_auto_thrust = fmc_ga_thrust
		fmc_auto_thrust2 = fmc_ga_thrust2
	
		B738DR_fms_N1_full_thrust[0] = fmc_ga_thrust_ind
		B738DR_fms_N1_full_thrust[1] = fmc_ga_thrust_ind2
		
		fmc_auto_mode = 5
		
	-- mode TAKEOFF
	elseif B738DR_flight_phase == 0 then --or B738DR_altitude_mode == 8 or B738DR_altitude_mode == 11 then
		if sel_temp == "----" then
			if to == "<ACT>" then
				fms_N1_to_mode_sel = 1	-- mode TO
				fmc_auto_thrust = fmc_full_thrust
				fmc_auto_thrust2 = fmc_full_thrust2
			elseif to_1 == "<ACT>" then
				fms_N1_to_mode_sel = 2	-- mode TO 1
				fmc_auto_thrust = fmc_full_thrust --* 0.97
				fmc_auto_thrust2 = fmc_full_thrust2
			elseif to_2 == "<ACT>" then
				fms_N1_to_mode_sel = 3	-- mode TO 2
				fmc_auto_thrust = fmc_full_thrust --* 0.94
				fmc_auto_thrust2 = fmc_full_thrust2
			end
			B738DR_fms_N1_full_thrust[0] = fmc_full_thrust_ind
			B738DR_fms_N1_full_thrust[1] = fmc_full_thrust_ind2
		else
			if to == "<ACT>" then
				fms_N1_to_mode_sel = 4	-- mode D-TO
				fmc_auto_thrust = fmc_dto_thrust
				fmc_auto_thrust2 = fmc_dto_thrust2
			elseif to_1 == "<ACT>" then
				fms_N1_to_mode_sel = 5	-- mode D-TO 1
				fmc_auto_thrust = fmc_dto_thrust --* 0.97
				fmc_auto_thrust2 = fmc_dto_thrust2
			elseif to_2 == "<ACT>" then
				fms_N1_to_mode_sel = 6	-- mode D-TO 2
				fmc_auto_thrust = fmc_dto_thrust --* 0.94
				fmc_auto_thrust2 = fmc_dto_thrust2
			end
			B738DR_fms_N1_full_thrust[0] = fmc_dto_thrust_ind
			B738DR_fms_N1_full_thrust[1] = fmc_dto_thrust_ind2
		end
		fms_N1_mode = fms_N1_to_mode_sel
	
		-- if B738DR_thr_cutback == 1 and simDR_radio_height_pilot_ft >= B738DR_thr_cut_alt and simDR_radio_height_pilot_ft < B738DR_thr_red_alt then
		if B738DR_thr_cutback == 1 and (simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_cut_alt or B738DR_lift_timer >= 150)
		and simDR_altitude_pilot < B738DR_aerodrom_altitude + B738DR_thr_red_alt then
			fmc_auto_thrust = B738DR_thr_cutback_N1[0]
			fmc_auto_thrust2 = B738DR_thr_cutback_N1[1]
		end
		
		fmc_auto_mode = 0
		
	-- mode CLIMB
	elseif B738DR_flight_phase == 1 then
		
		alt_thrust = math.min(simDR_altitude_pilot, 15000)
		
		if clb_1 == "<SEL>" then
			alt_thrust = math.max(alt_thrust, 10000)
			alt_thrust_ratio = B738_rescale(10000, 0.97, 15000, 1.00, alt_thrust)
		elseif clb_2 == "<SEL>" then
			alt_thrust = math.max(alt_thrust, 5000)
			alt_thrust_ratio = B738_rescale(5000, 0.94, 15000, 1.00, alt_thrust)
		else
			alt_thrust_ratio = 1.00
		end
		
		if clb == "<SEL>" then
			fms_N1_clb_mode_sel = 7	-- mode CLB
			fmc_auto_thrust = fmc_clb_thrust
			fmc_auto_thrust2 = fmc_clb_thrust2
		elseif clb_1 == "<SEL>" then
			fms_N1_clb_mode_sel = 8	-- mode CLB 1
			fmc_auto_thrust = fmc_clb_thrust * alt_thrust_ratio 	--0.97
			fmc_auto_thrust2 = fmc_clb_thrust2 * alt_thrust_ratio
		elseif clb_2 == "<SEL>" then
			fms_N1_clb_mode_sel = 9	-- mode CLB 2
			fmc_auto_thrust = fmc_clb_thrust * alt_thrust_ratio 	--0.94
			fmc_auto_thrust2 = fmc_clb_thrust2 * alt_thrust_ratio
		end
		
		B738DR_fms_N1_full_thrust[0] = fmc_clb_thrust
		B738DR_fms_N1_full_thrust[1] = fmc_clb_thrust
		
		fms_N1_mode = fms_N1_clb_mode_sel
		
		-- if B738DR_thr_cutback == 1 and simDR_radio_height_pilot_ft >= B738DR_thr_cut_alt and simDR_radio_height_pilot_ft < B738DR_thr_red_alt then
		if B738DR_thr_cutback == 1 and (simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_cut_alt or B738DR_lift_timer >= 150) 
		and simDR_altitude_pilot < B738DR_aerodrom_altitude + B738DR_thr_red_alt then
			fmc_auto_thrust = B738DR_thr_cutback_N1[0]
			fmc_auto_thrust2 = B738DR_thr_cutback_N1[1]
		end
		
		fmc_auto_mode = 1
		
	-- mode CRUISE CLIMB
	elseif B738DR_flight_phase == 3 then
		fmc_auto_thrust = fmc_clb_thrust
		fmc_auto_thrust2 = fmc_clb_thrust2
		fms_N1_mode = 7	-- mode CLB
		
		if fmc_con_thrust > fmc_clb_thrust then
			B738DR_fms_N1_full_thrust[0] = fmc_con_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_con_thrust2
		else
			B738DR_fms_N1_full_thrust[0] = fmc_clb_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_clb_thrust2
		end
		fmc_auto_mode = 3
		
	-- mode CRUISE, CRUISE DESCENT, DESCENT, APPROACH
	elseif B738DR_flight_phase == 2 
	or B738DR_flight_phase == 4 
	or B738DR_flight_phase == 5
	or B738DR_flight_phase == 6 then
		if B738DR_lvl_chg_mode == 1 and B738DR_altitude_mode == 2 then	--LVL CHG climb
			alt_thrust = math.min(simDR_altitude_pilot, 15000)
			
			if clb_1 == "<SEL>" then
				alt_thrust = math.max(alt_thrust, 10000)
				alt_thrust_ratio = B738_rescale(10000, 0.97, 15000, 1.00, alt_thrust)
			elseif clb_2 == "<SEL>" then
				alt_thrust = math.max(alt_thrust, 5000)
				alt_thrust_ratio = B738_rescale(5000, 0.94, 15000, 1.00, alt_thrust)
			else
				alt_thrust_ratio = 1.00
			end
			
			if clb == "<SEL>" then
				fms_N1_clb_mode_sel = 7	-- mode CLB
				fmc_auto_thrust = fmc_clb_thrust
				fmc_auto_thrust2 = fmc_clb_thrust2
			elseif clb_1 == "<SEL>" then
				fms_N1_clb_mode_sel = 8	-- mode CLB 1
				fmc_auto_thrust = fmc_clb_thrust * alt_thrust_ratio 	--0.97
				fmc_auto_thrust2 = fmc_clb_thrust2 * alt_thrust_ratio
			elseif clb_2 == "<SEL>" then
				fms_N1_clb_mode_sel = 9	-- mode CLB 2
				fmc_auto_thrust = fmc_clb_thrust * alt_thrust_ratio 	--0.94
				fmc_auto_thrust2 = fmc_clb_thrust2 * alt_thrust_ratio
			end
			fms_N1_mode = fms_N1_clb_mode_sel
			if fmc_con_thrust > fmc_clb_thrust then
				B738DR_fms_N1_full_thrust[0] = fmc_con_thrust
				B738DR_fms_N1_full_thrust[1] = fmc_con_thrust2
			else
				B738DR_fms_N1_full_thrust[0] = fmc_clb_thrust
				B738DR_fms_N1_full_thrust[1] = fmc_clb_thrust2
			end
			
			fmc_auto_mode = 1
		else
			fms_N1_mode = 10	-- mode  CRZ
			fmc_auto_thrust = fmc_crz_thrust
			fmc_auto_thrust2 = fmc_crz_thrust2
			B738DR_fms_N1_full_thrust[0] = fmc_con_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_con_thrust2
			
			fmc_auto_mode = 2
		end
	end
	
	if B738DR_eng_out == 1 then
		if B738DR_flight_phase > 0 and B738DR_flight_phase < 7 then
			fms_N1_mode = 12
			fmc_auto_thrust = fmc_con_thrust
			fmc_auto_thrust2 = fmc_con_thrust2
			B738DR_fms_N1_full_thrust[0] = fmc_con_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_con_thrust2
			fmc_auto_mode = 4
		end
	end
	
	if vnav_status_old ~= B738DR_autopilot_vnav_status and B738DR_autopilot_vnav_status == 1 then
		auto_act = "<ACT>"
		ga_act = "     "
		con_act = "     "
		clb_act = "     "
		crz_act = "     "
	end
	vnav_status_old = B738DR_autopilot_vnav_status
	
	if auto_act == "     " then
		if B738DR_altitude_mode == 9 or B738DR_altitude_mode == 10 or B738DR_altitude_mode == 12 then
			ga_act = "<ACT>"
			con_act = "     "
			clb_act = "     "
			crz_act = "     "
		elseif flight_phase_old2 ~= B738DR_flight_phase and B738DR_flight_phase ~= 0 then
			ga_act = "     "
			con_act = "     "
			clb_act = "     "
			crz_act = "     "
			if B738DR_flight_phase == 1 or B738DR_flight_phase == 3 then
				clb_act = "<ACT>"
			elseif B738DR_flight_phase == 2 or (B738DR_flight_phase > 3 and B738DR_flight_phase < 7) then
				crz_act = "<ACT>"
			else
				ga_act = "<ACT>"
			end
		end
	end
	
	if simDR_engine_on[0] ~= 0 and simDR_eng_N1[0] > 18 then
		if start_eng1_timer > 0 then
			start_eng1_timer = start_eng1_timer - SIM_PERIOD
			if start_eng1_timer < 0 then
				start_eng1_timer = 0
			end
		end
	else
		start_eng1_timer = 5
	end
	
	if simDR_engine_on[1] ~= 0 and simDR_eng_N1[1] > 18 then
		if start_eng2_timer > 0 then
			start_eng2_timer = start_eng2_timer - SIM_PERIOD
			if start_eng2_timer < 0 then
				start_eng2_timer = 0
			end
		end
	else
		start_eng2_timer = 5
	end
	
	-- SET N1 THRUST
	if B738DR_eicas_full_n1 == 0 then
		B738DR_eicas_full_n1_show[0] = 0
		B738DR_eicas_full_n1_show[1] = 0
	else
		if simDR_reverse_deploy[0] > 0.01 or start_eng1_timer ~= 0 then
			B738DR_eicas_full_n1_show[0] = 0
		else
			B738DR_eicas_full_n1_show[0] = 1
		end
		if simDR_reverse_deploy[1] > 0.01 or start_eng2_timer ~= 0 then
			B738DR_eicas_full_n1_show[1] = 0
		else
			B738DR_eicas_full_n1_show[1] = 1
		end
	end
	
	if B738DR_n1_set_source == 0 then	-- N1 source AUTO
		
		if clb_act == "<ACT>" then
			alt_thrust = math.max(simDR_altitude_pilot, 10000)
			alt_thrust = math.min(alt_thrust, 15000)
			
			if clb_1 == "<SEL>" then
				alt_thrust_ratio = B738_rescale(10000, 0.97, 15000, 1.00, alt_thrust)
			elseif clb_2 == "<SEL>" then
				alt_thrust_ratio = B738_rescale(10000, 0.94, 15000, 1.00, alt_thrust)
			else
				alt_thrust_ratio = 1.00
			end
		end
		
		if auto_act == "<ACT>" then
			if fmc_auto_thrust == 0 or fmc_auto_thrust2 == 0 then	-- FMC thrust not able
				B738DR_fms_N1_mode = 13
				B738DR_fms_N1_thrust[0] = 1.00	-- full thrust
				B738DR_fms_N1_thrust[1] = 1.00	-- full thrust
				B738DR_fms_N1_full_thrust[0] = 1.0
				B738DR_fms_N1_full_thrust[1] = 1.0
				B738DR_eicas_full_n1_show[0] = 0
				B738DR_eicas_full_n1_show[1] = 0
			else
				local auto_change = 0
				if B738DR_flight_phase == 0 then
					auto_change = 1
				elseif flight_phase_old2 ~= B738DR_flight_phase then
					if B738DR_flight_phase > 0 and B738DR_flight_phase < 6 then
						auto_change = 1
					elseif B738DR_flight_phase > 6 then
						auto_change = 1
					end
				end 
				if auto_change ~= 0 then
					fms_auto_n1_active = 1
				end
				if fms_auto_n1_active ~= 0 then
					B738DR_fms_N1_mode = fms_N1_mode
					B738DR_fms_N1_thrust[0] = fmc_auto_thrust
					B738DR_fms_N1_thrust[1] = fmc_auto_thrust2
				end
				-- if fmc_auto_mode == 0 or fmc_auto_mode == 1 then
				if fmc_auto_mode < 0 then
					B738DR_eicas_full_n1_show[0] = 0
					B738DR_eicas_full_n1_show[1] = 0
				end
			end
		elseif ga_act == "<ACT>" then
			B738DR_fms_N1_mode = 11
			B738DR_fms_N1_thrust[0] = fmc_ga_thrust
			B738DR_fms_N1_thrust[1] = fmc_ga_thrust2
			fms_auto_n1_active = 0
			B738DR_fms_N1_full_thrust[0] = fmc_ga_thrust_ind
			B738DR_fms_N1_full_thrust[1] = fmc_ga_thrust_ind2
		elseif con_act == "<ACT>" then
			B738DR_fms_N1_mode = 12
			B738DR_fms_N1_thrust[0] = fmc_con_thrust
			B738DR_fms_N1_thrust[1] = fmc_con_thrust2
			fms_auto_n1_active = 0
			B738DR_fms_N1_full_thrust[0] = fmc_con_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_con_thrust2
		elseif clb_act == "<ACT>" then
			if clb == "<SEL>" then
				B738DR_fms_N1_mode = 7
				B738DR_fms_N1_thrust[0] = fmc_clb_thrust
				B738DR_fms_N1_thrust[1] = fmc_clb_thrust2
			elseif clb_1 == "<SEL>" then
				B738DR_fms_N1_mode = 8
				B738DR_fms_N1_thrust[0] = fmc_clb_thrust * alt_thrust_ratio 	--0.97
				B738DR_fms_N1_thrust[1] = fmc_clb_thrust2 * alt_thrust_ratio
			elseif clb_2 == "<SEL>" then
				B738DR_fms_N1_mode = 9
				B738DR_fms_N1_thrust[0] = fmc_clb_thrust * alt_thrust_ratio 	--0.94
				B738DR_fms_N1_thrust[1] = fmc_clb_thrust2 * alt_thrust_ratio
			end
			fms_auto_n1_active = 0
			B738DR_fms_N1_full_thrust[0] = fmc_clb_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_clb_thrust2
		elseif crz_act == "<ACT>" then
			B738DR_fms_N1_mode = 10
			B738DR_fms_N1_thrust[0] = fmc_crz_thrust
			B738DR_fms_N1_thrust[1] = fmc_crz_thrust2
			fms_auto_n1_active = 0
			B738DR_fms_N1_full_thrust[0] = fmc_con_thrust
			B738DR_fms_N1_full_thrust[1] = fmc_con_thrust2
		end
	else
		B738DR_fms_N1_mode = 0
		B738DR_eicas_full_n1_show[0] = 0
		B738DR_eicas_full_n1_show[1] = 0
	end
	flight_phase_old2 = B738DR_flight_phase
	
	
end

function drag_timer()
	drag_timeout = 2
end

function rst_fms_msg()
	fms_msg_sound = 0
end

function B738_fmc_msg()

	local day_act = ""
	local month_act = ""
	local year_act = ""
	local date_act = 0
	local date_temp = ""
	local align_mode = 0
	local airspeed = B738DR_mcp_speed_dial	--simDR_airspeed_dial
	local throttle_idle = 0
	local day_nav = ""
	local year_nav = ""
	local date_nav = 0
	local month_nav = ""
	local date_nav2 = 0
	local ii = 0
	
	local lnav_mode = 0
	if B738DR_heading_mode > 3 and B738DR_heading_mode < 7 then
		lnav_mode = 1
	elseif B738DR_heading_mode == 8 then
		lnav_mode = 1
	elseif B738DR_heading_mode == 13 then
		lnav_mode = 1
	end
	
	-- if simDR_electrical_bus_volts[0] > 10 or simDR_electrical_bus_volts[1] > 10 then
	if fmc_enable ~= 0 and simDR_instrument_brightness[10] ~= 0 then
		
		-- GPS_L_INVALID
		if B738DR_gps_pos == "-----.-------.-"  then
			if msg_gps_l_fail == 0 then
				msg_gps_l_fail = 1
				add_fmc_msg(GPS_L_INVALID, 2)
				--B738DR_fmc_message_warn = 1
			end
		end
		
		-- GPS_R_INVALID
		if B738DR_gps2_pos == "-----.-------.-"  then
			if msg_gps_r_fail == 0 then
				msg_gps_r_fail = 1
				add_fmc_msg(GPS_R_INVALID, 2)
				--B738DR_fmc_message_warn = 1
			end
		end
		
		-- GPS_LR_INVALID
		if B738DR_gps_pos == "-----.-------.-" 
		and B738DR_gps2_pos == "-----.-------.-"  then
			if msg_gps_lr_fail == 0 then
				msg_gps_lr_fail = 1
				add_fmc_msg(GPS_LR_INVALID, 1)
			end
		end
		
		-- IRS MOTION
		if B738DR_irs1_restart == 1 or B738DR_irs2_restart == 1 then
			if msg_irs_motion == 0 then
				msg_irs_motion = 1
				add_fmc_msg(IRS_MOTION, 2)
				--B738DR_fmc_message_warn = 1
			end
		end
		
		-- SET IRS
		if B738DR_irs_left == 0 and B738DR_irs_right == 0 then
			B738DR_irs_pos_fmc_set = ""
			B738DR_irs_hdg_fmc_set = "---"
		end
		-- ENTER IRS HDG
		align_mode = 0
		if B738DR_irs_left_mode == 1 and B738DR_irs_left == 3 then
			align_mode = 1
		end
		if B738DR_irs_right_mode == 1 and B738DR_irs_right == 3 then
			align_mode = 1
		end
		if B738DR_irs_left_mode == 3 or B738DR_irs_right_mode == 3 then
			align_mode = 0
		end
		if align_mode == 1 then
			if msg_irs_hdg == 0 then
				msg_irs_hdg = 1
				add_fmc_msg(ENTER_IRS_HDG, 1)
			end
		end
		
		-- ENTER IRS POS
		align_mode = 0
		if B738DR_irs_left_mode == 1 and B738DR_irs_left == 2 then	--and B738DR_irs_align_left == 1 then
			if B738DR_irs_pos_fmc_set == "" then
				if B738DR_irs_ding == 0 then
					if B738DR_alignment_left_remain == 0 then
						align_mode = 1
					end
				else
					if B738DR_alignment_left_remain == 0 or B738DR_irs_align_left2 ~= 0 then
						align_mode = 1
					end
				end
			end
		elseif B738DR_irs_left_mode == 1 and B738DR_irs_left == 1 and B738DR_irs_was_full_allign ~= 0 then	--and B738DR_irs_align_left == 1 then
			if B738DR_irs_pos_fmc_set == "" then
				if B738DR_irs_ding == 0 then
					if B738DR_alignment_left_remain == 0 then
						align_mode = 1
					end
				else
					if B738DR_alignment_left_remain == 0 or B738DR_irs_align_left2 ~= 0 then
						align_mode = 1
					end
				end
			end
		end
		if B738DR_irs_right_mode == 1 and B738DR_irs_right == 2 then	--and B738DR_irs_align_right == 1 then
			if B738DR_irs_pos_fmc_set == "" then
				if B738DR_irs_ding == 0 then
					if B738DR_alignment_right_remain == 0 then
						align_mode = 1
					end
				else
					if B738DR_alignment_right_remain == 0 or B738DR_irs_align_right2 ~= 0 then
						align_mode = 1
					end
				end
			end
		elseif B738DR_irs_right_mode == 1 and B738DR_irs_right == 2 and B738DR_irs_was_full_allign2 ~= 0 then	--and B738DR_irs_align_right == 1 then
			if B738DR_irs_pos_fmc_set == "" then
				if B738DR_irs_ding == 0 then
					if B738DR_alignment_right_remain == 0 then
						align_mode = 1
					end
				else
					if B738DR_alignment_right_remain == 0 or B738DR_irs_align_right2 ~= 0 then
						align_mode = 1
					end
				end
			end
		end
		if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
			align_mode = 0
		end
		if align_mode == 1 then
			if msg_irs_pos == 0 then
				msg_irs_pos = 1
				add_fmc_msg(ENTER_IRS_POS, 2)
				--B738DR_fmc_message_warn = 1
			end
		end
		
		-- RESET MCP ALT
		if td_idx ~= 0 and B738DR_vnav_td_dist < 6 and B738DR_vnav_td_dist > 0 and B738DR_flight_phase == 2 then	-- VNAV on and cruise phase
			if crz_alt_num > 0 and simDR_ap_altitude_dial_ft >= crz_alt_num then
				if msg_mcp_alt == 0 then
					msg_mcp_alt = 1
					add_fmc_msg(RESET_MCP_ALT, 2)
					--B738DR_fmc_message_warn = 1
				end
			end
		end
		--if B738DR_altitude_mode ~= 5 then	-- VNAV off
		if td_idx == 0 or B738DR_vnav_td_dist <= 0 or B738DR_flight_phase ~= 2 then 	-- VNAV off
			msg_mcp_alt = 0
		end
		
		-- DRAG REQUIRED
		if simDR_throttle_use[0] < 0.1 or simDR_throttle_use[1] < 0.1 then
			throttle_idle = 1
		end
		if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and B738DR_autopilot_vnav_status == 1 then
			if simDR_airspeed_is_mach == 0 then
				airspeed = airspeed + 2
				if B738DR_speed_ratio > -0.2 and msg_drag_req == 0 and AP_airspeed > airspeed then
					airspeed = airspeed + 10
					if AP_airspeed > airspeed then
						if drag_timeout == 0 and throttle_idle == 1 then
							drag_timeout = 1
							if is_timer_scheduled(drag_timer) == false then
								run_after_time(drag_timer, 18)
							end
						end
						if drag_timeout == 2 then
							msg_drag_req = 1
							add_fmc_msg(DRAG_REQUIRED, 1)
						end
					end
				else
					if is_timer_scheduled(drag_timer) == true then
						stop_timer(drag_timer)
						drag_timeout = 0
					end
					if AP_airspeed <= airspeed and drag_timeout == 2 then
						msg_drag_req = 0
						drag_timeout = 0
					end
				end
			else
				airspeed = airspeed + 0.01
				if B738DR_speed_ratio > -0.2 and msg_drag_req == 0 and AP_airspeed_mach > airspeed then
					airspeed = airspeed + 0.04
					if AP_airspeed_mach > airspeed then
						if drag_timeout == 0 and throttle_idle == 1 then
							drag_timeout = 1
							if is_timer_scheduled(drag_timer) == false then
								run_after_time(drag_timer, 18)
							end
						end
						if drag_timeout == 2 then
							msg_drag_req = 1
							add_fmc_msg(DRAG_REQUIRED, 1)
						end
					end
				else
					if is_timer_scheduled(drag_timer) == true then
						stop_timer(drag_timer)
						drag_timeout = 0
					end
					if AP_airspeed_mach <= airspeed and drag_timeout == 2 then
						msg_drag_req = 0
						drag_timeout = 0
					end
				end
			end
			
		else
			if is_timer_scheduled(drag_timer) == true then
				stop_timer(drag_timer)
			end
			drag_timeout = 0
			msg_drag_req = 0
		end
		
		if msg_nav_data == 0 then
			day_act = os.date("!%d")
			month_act = os.date("!%m")
			year_act = os.date("!%Y")
			date_act = tonumber(year_act .. month_act .. day_act)
			if string.len(airac_navdata_active) < 13 then
				date_nav = 0
			else
				year_nav = string.sub(airac_navdata_active, 12, 13) -- year
				date_temp = string.sub(airac_navdata_active, 6, 8) -- month
				for ii = 1, 12 do
					if date_temp == month_table[ii][1] then
						month_nav = month_table[ii][2]
						break
					end
				end
				day_nav = string.sub(airac_navdata_active, 9, 10) -- day
				date_nav = tonumber("20" .. year_nav .. month_nav .. day_nav)
			end
			if date_nav == 0 then
				msg_nav_data = 3
			elseif date_act > date_nav then
				msg_nav_data = 1
			else
				msg_nav_data = 2
			end
		end
		
		if msg_nav_data == 1 then
			msg_nav_data = 2
			add_fmc_msg(NAV_DATA_OF_DATE, 2)
			--B738DR_fmc_message_warn = 1
		elseif msg_nav_data == 3 then
			msg_nav_data = 4
			add_fmc_msg(CORRUPTED_NAVDATA, 2)
			--B738DR_fmc_message_warn = 1
		end
		
		-- UNABLE CRZ ALT
		--tc_disco
		local use_ref_icao = ""
		local use_des_icao = ""
		if rte12_act == 0 then
			use_ref_icao = ref_icao
			use_des_icao = des_icao
		elseif rte12_act == 0 then
			use_ref_icao = ref_icao7
			use_des_icao = des_icao7
		end
		
		if crz_alt_num > 0 and legs_num > 1 and offset > 0 and perf_exec > 0 and use_ref_icao ~= "----" and use_des_icao ~= "****" and vnav_update == 0 then
			if B738DR_flight_phase < 2 or B738DR_flight_phase == 3 then
				if fms_recalc == 0 and legs_delete == 0 then
					local msg_temp = 0
					if tc_idx == 0 then
						msg_temp = 1
					elseif td_idx_break == 0 then
						msg_temp = 1
					else
						if tc_idx > td_idx_break then
							msg_temp = 1
						elseif tc_idx == td_idx_break and td_dist > tc_dist then
							msg_temp = 1
						end
					end
					msg_unavaible_crz_alt_del = 0
					if msg_temp == 0 then
						if msg_unavaible_crz_alt == 1 then
							msg_unavaible_crz_alt = 0
							msg_unavaible_crz_alt_del = 1
						end
					else 
						if msg_unavaible_crz_alt == 0 then
							msg_unavaible_crz_alt = 1
							add_fmc_msg(UNABLE_CRUISE_ALT, 1)
							if B738DR_autopilot_vnav_status ~= 0 then
								if B738DR_vnavalt_althld ~= 0 then
									-- ALT HLD
									B738DR_activate_pitch_mode = 3
								else
									-- VNAV ALT
									vnav_alt_mode = 1
								end
							end
						end
					end
				end
				
				local next_alt_status = 0
				if B738DR_autopilot_vnav_status == 0 then
					msg_next_alt = 0
				else
					if ed_found ~= 0 and td_idx ~= 0 then
						if offset < ed_found and offset > td_idx and offset < legs_num then
							if legs_data[offset][11] < legs_data[offset+1][11] then
								next_alt_status = 1
							end
						end
					end
					
					if next_alt_status == 1 then
						if msg_next_alt == 0 then
							add_fmc_msg(UNABLE_NEXT_ALTITUDE, 1)
							if B738DR_vnav_alt_disable ~= 0 then
								B738DR_activate_pitch_mode = 3
							else
								if B738DR_vnavalt_althld ~= 0 then
									-- ALT HLD
									B738DR_activate_pitch_mode = 3
								else
									-- VNAV ALT
									vnav_alt_mode = 1
								end
							end
						end
						msg_next_alt = 1
					else
						if last_offset ~= offset then
							msg_next_alt = 0
						end
					end
				end
			else
				msg_unavaible_crz_alt = 0
			end
			
		else
			msg_unavaible_crz_alt = 0
		end
		
		-- CONFIG SAVED
		-- if entry == CONFIG_SAVED and fms_msg_cfg_saved == 0 then
			-- fms_msg_cfg_saved = 1
			-- fms_msg_sound = 1
		-- end
		
		-- TAI ABOVE 10 C
		--if simDR_TAT > 10 then
		local temp_comp = 0
		if simDR_radio_height_pilot_ft > 20 then
			temp_comp = B738DR_TAT
		else
			temp_comp = simDR_OAT
		end
		if temp_comp > 10 then
			-- if simDR_cowl_ice_on[0] == 1 or simDR_cowl_ice_on[1] == 1 or B738DR_wing_l_heat_on == 1 or B738DR_wing_r_heat_on == 1 then
			if simDR_cowl_ice_on[0] == 1 or simDR_cowl_ice_on[1] == 1 or B738DR_wing_heat_pos == 1 then
				if msg_tai_above_10 == 0 then
					msg_tai_above_10 = 1
					add_fmc_msg(TAI_ON_ABOVE_10C, 1)
				end
			else
				msg_tai_above_10 = 0
			end
		end
		-- DISCONTINUITY
		if ffirst_time2 ~= 0 then
			-- if B738DR_fpln_nav_id == "DISCON" or B738DR_fpln_nav_id == "-----" or B738DR_fpln_nav_id == "" then
			local enable_disco = false
			if B738DR_fpln_nav_id == "" then
				enable_disco = true
			elseif legs_num > 0 and offset > 0 and offset <= legs_num + 1 then
				if legs_data[offset][1] == "DISCONTINUITY" then
					enable_disco = true
				end
			end
			
			if enable_disco == true then
				if lnav_mode == 1 then
					-- if B738DR_fpln_nav_id == "" and legs_num > 0 and B738DR_lnav_disconnect == 0 then
					if B738DR_lnav_disconnect == 0 then
						add_fmc_msg(DISCON, 2)
						--B738DR_fmc_message_warn = 1
						B738DR_lnav_disconnect = 1
					else
						B738DR_lnav_disconnect = 1
					end
				end
				B738DR_vnav_disconnect = 1
				-- if B738DR_fms_test == 55 then
					-- dump_leg_append("\n")
					-- dump_leg_append("LNAV/VNAV disconnect: \n")
					-- dump_leg_append("B738DR_fpln_nav_id: " .. tostring(B738DR_fpln_nav_id) .. " \n")
				-- end
			end
		end
		
		-- ABOVE MAX CERT ALT
		if simDR_altitude_pilot > 41200 then
			if msg_above_max == 0 then
				msg_above_max = 1
				add_fmc_msg(ABOVE_MAX_CERT_ALT, 1)
			end
		end
		if simDR_altitude_pilot <= 41000 then
			msg_above_max = 0
		end
	
		-- APPRCH VREF NOT SELECTED
		if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and B738DR_spd_ref == 0 then
			if simDR_gear_deploy[0] > 0.1 and simDR_flaps_ratio > 0.624 then	-- gear down, flaps >= 15
				if app_flap == "--" and msg_vref_not_sel == 0 then
					msg_vref_not_sel = 1
					add_fmc_msg(APPRCH_VREF_NOT_SELECTED, 1)
				end
			end
		else
			msg_vref_not_sel = 0
		end
		
		-- LNAV DISCONNECT
		if ffirst_time2 ~= 0 then
			if legs_num < 1 then
				if lnav_mode == 1 and simDR_radio_height_pilot_ft > 20 then
					B738DR_lnav_disconnect = 1
					-- add_fmc_msg(LNAV_DISCON, 2)
					--B738DR_fmc_message_warn = 1
				end
				B738DR_vnav_disconnect = 1
				-- if B738DR_fms_test == 55 then
					-- dump_leg_append("\n")
					-- dump_leg_append("LNAV/VNAV disconnect: \n")
					-- dump_leg_append("ffirst_time2: " .. tostring(ffirst_time2) .. " \n")
					-- dump_leg_append("legs_num: " .. tostring(legs_num) .. " \n")
					-- dump_leg_append("lnav_mode: " .. tostring(lnav_mode) .. " \n")
					-- dump_leg_append("simDR_radio_height_pilot_ft: " .. tostring(simDR_radio_height_pilot_ft) .. " \n")
				-- end
			end
			if legs_num < 1 then
				B738DR_end_route = 1
			else
				B738DR_end_route = 0
			end
			
			if crz_alt_num > 0 and legs_num > 0 and offset > 0 and perf_exec > 1 and use_ref_icao ~= "----" and use_des_icao ~= "****" then
				B738DR_no_perf = 0
			else
				B738DR_no_perf = 1
			end
		end
		
		-- NOT ON INTERCEPT HEADING
		local intercept_hdg = 0
		if B738DR_lnav_engaged == 2 then
			local mag_trk = B738DR_fmc_trk + simDR_mag_variation
			local relative_brg = (mag_trk - simDR_mag_hdg + 360) % 360
			if relative_brg > 180 then
				relative_brg = relative_brg - 360
			end
			
			local relative_brg2 = (B738DR_fmc_crs - simDR_mag_hdg + 360) % 360
			if relative_brg2 > 180 then
				relative_brg2 = relative_brg2 - 360
			end
			if B738DR_xtrack <= 0 and relative_brg > -90 and relative_brg < 5 and relative_brg2 > -90 and relative_brg2 < 0 then
				intercept_hdg = 1
			elseif B738DR_xtrack >= 0 and relative_brg > -5 and relative_brg < 90 and relative_brg2 > 0 and relative_brg2 < 90 then
				intercept_hdg = 1
			end
			if offset <= legs_num then
				if legs_data[offset][31] ~= "DF" then
					if intercept_hdg == 0 then
						add_fmc_msg(NOT_INTCPT_HDG, 2)
						B738DR_lnav_disconnect = 1
					end
				elseif legs_intdir_act ~= 0 then
					if intercept_hdg == 0 then
						add_fmc_msg(NOT_INTCPT_HDG, 2)
						B738DR_lnav_disconnect = 1
					end
				end
			end
		end
		
		-- USING_RSV_FUEL
		local res_fuel = tonumber(reserves_kgs)
		local disable_rsv_fuel = 0
		-- if simDR_radio_height_pilot_ft < 50 then
			-- disable_rsv_fuel = 1
		-- elseif simDR_radio_height_pilot_ft < 200 and B738DR_flight_phase == 0 then
			-- disable_rsv_fuel = 1
		local perf_init_complete = 1
		if gw == "***.*" or cost_index == "***" or reserves == "**.*" or crz_alt == "*****" then
			perf_init_complete = 0
		end
		if perf_exec < 2 then
			perf_init_complete = 0
		end
		if perf_init_complete == 0 then
			disable_rsv_fuel = 1
		elseif res_fuel == nil then
			disable_rsv_fuel = 1
		elseif fuel_calculated == 0 then
			disable_rsv_fuel = 1
		elseif legs_num < 2 then
			disable_rsv_fuel = 1
		elseif legs_data[legs_num+1][40] == 0 then
			disable_rsv_fuel = 1
		end
		
		if calc_rte_enable == 0 then
			if disable_rsv_fuel == 0 then
				res_fuel = res_fuel * 1000
				if legs_data[legs_num+1][40] < 907 then
					if msg_using_ins_fuel == 0 then
						msg_using_ins_fuel = 1
						add_fmc_msg(INSUFICIENT_FUEL, 1)
					end
				elseif legs_data[legs_num+1][40] < res_fuel and msg_using_ins_fuel == 0 then
					if msg_using_rsv_fuel == 0 then
						msg_using_rsv_fuel = 1
						add_fmc_msg(USING_RSV_FUEL, 1)
					end
				end
			else
				if legs_num > 2 then
					if legs_data[legs_num+1][40] ~= 0 then
						msg_using_rsv_fuel = 0
						msg_using_ins_fuel = 0
					end
				else
					msg_using_rsv_fuel = 0
					msg_using_ins_fuel = 0
				end
			end
		end
		
		-- UNABLE_REQ_NAV_PERF
		local rnp_timer_max = 30	--30 * 1.5
		if B738DR_flight_phase > 5 then
			rnp_timer_max = 15		--10 * 1.5
		end
		
		local run_rnp_timer = 0
		local alert_ignore = 0
		
		align_mode = 1
		if B738DR_irs_left == 0 then
			align_mode = 0
		elseif B738DR_irs_right == 0 then
			align_mode = 0
		else
			if B738DR_irs_left_mode == 1 and B738DR_irs_left == 2 and B738DR_irs_align_left2 ~= 0 then
				if B738DR_alignment_left_remain == 0 and B738DR_irs_pos_fmc_set == "" then
					align_mode = 0
				end
			end
			if B738DR_irs_right_mode == 1 and B738DR_irs_right == 2 and B738DR_irs_align_right2 ~= 0 then
				if B738DR_alignment_right_remain == 0 and B738DR_irs_pos_fmc_set == "" then
					align_mode = 0
				end
			end
		end
		
		if simDR_radio_height_pilot_ft > 50 then
			-- in flight
			if align_mode == 1 then
				if offset <= legs_num and offset > 0 then
					if legs_data[offset][31] == "CF" or legs_data[offset][31] == "DF" or legs_data[offset][31] == "PI" then
						alert_ignore = 1
					end
					if legs_data[offset][31] == "HA" or legs_data[offset][31] == "HF" or legs_data[offset][31] == "HM" then
						alert_ignore = 1
					end
					if legs_intdir_act ~= 0 then
						alert_ignore = 1
					end
				end
			end
		else
			if align_mode == 1 then
				alert_ignore = 1
			end
		end
		
		if rnp_lock_time > 0 then
			rnp_lock_time = rnp_lock_time - SIM_PERIOD
		end
		if rnp_lock_time < 0 then
			rnp_lock_time = 0
		end
		
		if simDR_radio_height_pilot_ft > 50 then
			if B738DR_anp > B738DR_rnp and legs_num > 1 and lnav_mode == 1 then
				run_rnp_timer = 1
			end
		end
		--*************************----
		if legs_num > 1 and fmc_align == 0 and align_mode == 0 then
			run_rnp_timer = 1
		end
		
		if run_rnp_timer == 0 or rnp_lock_time ~= 0 or msg_rnp_alert == 1 then
			if is_timer_scheduled(rnp_timer) == true then
				stop_timer(rnp_timer)
			end
		else
			if is_timer_scheduled(rnp_timer) == false and msg_rnp_alert == 0 and alert_ignore == 0 then
				run_after_time(rnp_timer, rnp_timer_max)
			end
		end
		if rnp_alert == 1 and msg_rnp_alert == 0 then
			msg_rnp_alert = 1
			rnp_alert = 0
			add_fmc_msg(UNABLE_REQ_NAV_PERF, 2)
			--B738DR_fmc_message_warn = 1
		end
		ii = B738DR_anp + 0.2
		if ii < B738DR_rnp and simDR_radio_height_pilot_ft > 50 then
			msg_rnp_alert = 0
		end
		
		-- ACARS VOICE
		local dl_pages = page_dl_menu + page_dl_acars + page_dl_requests + page_dl_wxr_req + page_dl_wxr_info + page_dl_fltlog + page_dl_sensors
		dl_pages = dl_pages + page_dl_menu2 + page_dl_acars2 + page_dl_requests2 + page_dl_wxr_req2 + page_dl_wxr_info2 + page_dl_fltlog2 + page_dl_sensors2
		if dl_pages == 0 then
			if B738DR_com3_data == 0 and msg_com3_data_alert == 0 then
				msg_com3_data_alert = 1
				add_fmc_msg(ACARS_VOICE, 2)
			elseif B738DR_com3_data ~= 0 then
				msg_com3_data_alert = 0
			end
		else
			msg_com3_data_alert = 0
		end
		
	end
	
	
	B738DR_fms_msg_sound = fms_msg_sound
	if fms_msg_sound == 1 then
		if is_timer_scheduled(rst_fms_msg) == false then
			run_after_time(rst_fms_msg, 1)
		end
	end
	
	if fmc_message_num > 255 then
		fmc_message_num = 255
	end
	
	-- FMCs message light
	fmc1_msg_light = 0
	fmc2_msg_light = 0
	
	if fmc1_msg_alt == 1 then
		fmc1_msg_light = 1
	end
	if fmc2_msg_alt == 1 then
		fmc2_msg_light = 1
	end
	-- if fmc2_msg_alt == 0 then
		-- fmc2_msg_light = 0
	-- elseif fmc2_msg_alt == 1 then
		-- fmc2_msg_light = 1
	-- end
	
	if fmc_message_num > 0 then
		fmc1_msg_light = 1
		fmc2_msg_light = 1
	else
		fmc1_msg_light = 0
		fmc2_msg_light = 0
		fmc_message_warn_dis = 0
		B738DR_fmc_message_warn = 0
	end
	
	-- --fmcmsg
	if fmc_enable == 0 then
		fmc1_msg_light = 0
		fmc2_msg_light = 0
	elseif B738DR_lights_test == 1 then
		fmc1_msg_light = 1
		if B738DR_ac_tnsbus2_status ~= 0 then
			fmc2_msg_light = 1
		else
			fmc2_msg_light = 0
		end
	else
		if B738DR_ac_tnsbus2_status == 0 then
			fmc2_msg_light = 0
		end
	end
	
	B738DR_fmc_message = fmc1_msg_light
	
	B738DR_fmc_message2 = fmc2_msg_light
	
end

function B738_fmc_light()
	local fmc1_offset = 0
	local fmc1_dspy = 0
	local fmc1_fail = 0
	local fmc2_offset = 0
	local fmc2_dspy = 0
	local fmc2_fail = 0
	
	if offset_act >= 5 and offset > 1 and offset <= legs_num then
		
		if legs_data[offset_seq][45] ~= 0 then	--and offset_start ~= 0 and offset_end ~= 0 then
			-- local fly_lat_off = false
			-- if B738DR_fms_legs_offset_start_t == 0 and offset_seq >= offset_start and offset_seq <= offset_end then
				-- fly_lat_off = true
			-- elseif B738DR_fms_legs_offset_start_t ~= 0 and offset_seq > offset_start and offset_seq <= offset_end then
				-- fly_lat_off = true
			-- end
			
			-- local offset_start_in = offset_start
			-- local offset_end_in = offset_end
			-- if offset_start_in == 0 then
				-- offset_start_in = offset_start_def
			-- end
			-- if offset_end_in == 0 then
				-- offset_end_in = offset_end_def
			-- end
			
			-- if (offset_seq > offset_start_in or offset_act == 5) and offset_seq <= offset_end_in then
				-- -- if fly_lat_off == true then
					-- fmc1_offset = 1
					-- fmc2_offset = 1
				-- -- end
			-- end
			
			local fly_lat_offset = false
			if offset_start == 0 then
				fly_lat_offset = true
			elseif offset_seq > offset_start then
				fly_lat_offset = true
			end
			
			if fly_lat_offset == true then
				fmc1_offset = 1
				fmc2_offset = 1
			end
		end
		
	end
	if B738DR_fms_test1 ~= 44 then
		fmc1_fail = 1
		fmc2_fail = 1
	end
	
	if fmc_enable == 0 then
		fmc1_offset = 0
		fmc1_dspy = 0
		fmc1_fail = 0
		fmc2_offset = 0
		fmc2_dspy = 0
		fmc2_fail = 0
	elseif B738DR_lights_test == 1 then
		fmc1_offset = 1
		fmc1_dspy = 1
		fmc1_fail = 1
		if B738DR_ac_tnsbus2_status ~= 0 then
			fmc2_offset = 1
			fmc2_dspy = 1
			fmc2_fail = 1
		else
			fmc2_offset = 0
			fmc2_dspy = 0
			fmc2_fail = 0
		end
	else
		if B738DR_ac_tnsbus2_status == 0 then
			fmc2_offset = 0
			fmc2_dspy = 0
			fmc2_fail = 0
		end
	end
	
	B738DR_fmc_offset = fmc1_offset
	B738DR_fmc_dspy = fmc1_dspy
	B738DR_fmc_fail = fmc1_fail
	B738DR_fmc_offset2 = fmc2_offset
	B738DR_fmc_dspy2 = fmc2_dspy
	B738DR_fmc_fail2 = fmc2_fail
end

function B738_fmc_clr_msg()
	
	local ii = 0
	local jj = 0
	
	if fmc_message_num > 0 then
		for ii = 1, fmc_message_num do
			if fmc_message[ii] == RESET_MCP_ALT then
				if td_idx ~= 0 then	--and B738DR_altitude_mode == 5 then	-- VNAV on and cruise phase
					if crz_alt_num > 0 and simDR_ap_altitude_dial_ft < crz_alt_num then
						delete_fmc_msg(ii)
					end
				end
			elseif fmc_message[ii] == DRAG_REQUIRED then
				if simDR_airspeed_is_mach == 0 then
					if AP_airspeed <= (B738DR_mcp_speed_dial + 5) then
						delete_fmc_msg(ii)
					end
				else
					if AP_airspeed_mach <= (B738DR_mcp_speed_dial + 0.02) then
						delete_fmc_msg(ii)
					end
				end
			elseif fmc_message[ii] == ABOVE_MAX_CERT_ALT then
				if simDR_altitude_pilot <= 41000 then
					delete_fmc_msg(ii)
				end
			elseif fmc_message[ii] == TAI_ON_ABOVE_10C then
				if B738DR_TAT < 9 then
					delete_fmc_msg(ii)
					msg_tai_above_10 = 0
				else
					-- if simDR_cowl_ice_on[0] == 0 and simDR_cowl_ice_on[1] == 0 and B738DR_wing_l_heat_on == 0 and B738DR_wing_r_heat_on == 0 then
					if simDR_cowl_ice_on[0] == 0 and simDR_cowl_ice_on[1] == 0 and B738DR_wing_heat_pos == 0 then
						delete_fmc_msg(ii)
						msg_tai_above_10 = 0
					end
				end
			elseif fmc_message[ii] == UNABLE_CRUISE_ALT then
				if msg_unavaible_crz_alt_del == 1 then
					msg_unavaible_crz_alt_del = 0
					delete_fmc_msg(ii)
				end
			elseif fmc_message[ii] == ACARS_VOICE then
				local dl_pages = page_dl_menu + page_dl_acars + page_dl_requests + page_dl_wxr_req + page_dl_wxr_info + page_dl_fltlog + page_dl_sensors
				dl_pages = dl_pages + page_dl_menu2 + page_dl_acars2 + page_dl_requests2 + page_dl_wxr_req2 + page_dl_wxr_info2 + page_dl_fltlog2 + page_dl_sensors2
				if dl_pages == 0 then
					if B738DR_com3_data ~= 0 then
						delete_fmc_msg(ii)
						msg_com3_data_alert = 0
					end
				else
					delete_fmc_msg(ii)
					msg_com3_data_alert = 0
				end
			elseif fmc_message[ii] == VERIFY_TO_SPEEDS then
				if verify_to_spd == 0 then
					delete_fmc_msg(ii)
				end
			elseif fmc_message[ii] == REF_ICAO_MSG then
				if string.len(entry) ~= 4 and string.len(entry2) ~= 4 then
					msg_ref_icao_alert = 0
					delete_fmc_msg(ii)
				elseif page_rte_init == 0 and page_rte_init2 == 0 and (string.len(entry) == 4 or string.len(entry2) == 4) then
					msg_ref_icao_alert = 0
					delete_fmc_msg(ii)
					entry = ""
					entry2 = ""
				end
			elseif string.sub(fmc_message[ii], 1, 8) == HOLD_AT then
				if new_hold_at == 0 and rte12_act == 0 then
					delete_fmc_msg(ii)
				elseif new_hold_at2 == 0 and rte12_act == 1 then
					delete_fmc_msg(ii)
				end
			elseif fmc_message[ii] == APPRCH_VREF_NOT_SELECTED then
				if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and B738DR_spd_ref == 0 then
					if simDR_gear_deploy[0] > 0.1 and simDR_flaps_ratio > 0.624 then	-- gear down, flaps >= 15
						if app_flap == "--" and msg_vref_not_sel == 0 then
							jj = 1
						end
					end
				end
				if jj == 0 then
					delete_fmc_msg(ii)
				end
			end
		end
	end
	
end

function delete_fmc_msg(del_idx)
	
	local jj = 0
	
	if del_idx < fmc_message_num then
		for jj = del_idx + 1, fmc_message_num do
			fmc_message[jj-1] = fmc_message[jj-1]
			fmc_message_warn[jj-1] = fmc_message_warn[jj-1]
		end
	end
	fmc_message_num = fmc_message_num - 1
end

function rnp_timer()
	rnp_alert = 1
end

function add_fmc_msg(msg_idx, msg_annun)
	
	local ii = 0
	local no_fmc_message_warn = 1
	if fmc_message_num > 0 then
		for ii = 1, fmc_message_num do
			if fmc_message_warn[ii] > 1 then
				no_fmc_message_warn = 0
			end
		end
	end
	if no_fmc_message_warn == 1 then
		fmc_message_warn_dis = 0
	end
	if msg_annun == 2 then
		B738DR_fmc_message_warn = 1
	end
	
	fms_msg_sound = 1
	fmc_message_num = fmc_message_num + 1
	if fmc_message_num > 255 then
		fmc_message_num = 255
	end
	fmc_message[fmc_message_num] = msg_idx
	fmc_message_warn[fmc_message_num] = msg_annun
	
	simCMD_nosmoking_toggle:once()
	
end

function B738DR_checklist()

	local to_flaps_set = 0
	if B738DR_fms_takeoff_flaps > 0 then
		if simDR_int_ver < 120000 then
			if B738DR_fms_takeoff_flaps == 1 and simDR_flaps_handle_ratio == 0.125 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 5 and simDR_flaps_handle_ratio == 0.375 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 10 and simDR_flaps_handle_ratio == 0.5 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 15 and simDR_flaps_handle_ratio == 0.625 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 25 and simDR_flaps_handle_ratio == 0.75 then
				to_flaps_set = 1
			end
		else
			if B738DR_fms_takeoff_flaps == 1 and simDR_flaps_handle_ratio_xp12 == 0.125 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 5 and simDR_flaps_handle_ratio_xp12 == 0.375 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 10 and simDR_flaps_handle_ratio_xp12 == 0.5 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 15 and simDR_flaps_handle_ratio_xp12 == 0.625 then
				to_flaps_set = 1
			elseif B738DR_fms_takeoff_flaps == 25 and simDR_flaps_handle_ratio_xp12 == 0.75 then
				to_flaps_set = 1
			end
		end
	end
	B738DR_takeoff_flaps_set = to_flaps_set
	
	local app_flaps_set = 0
	if B738DR_fms_approach_flaps > 0 then
		if simDR_int_ver < 120000 then
			if B738DR_fms_approach_flaps == 15 and simDR_flaps_handle_ratio == 0.625 then
				app_flaps_set = 1
			elseif B738DR_fms_approach_flaps == 30 and simDR_flaps_handle_ratio == 0.875 then
				app_flaps_set = 1
			elseif B738DR_fms_approach_flaps == 40 and simDR_flaps_handle_ratio == 1 then
				app_flaps_set = 1
			end
		else
			if B738DR_fms_approach_flaps == 15 and simDR_flaps_handle_ratio_xp12 == 0.625 then
				app_flaps_set = 1
			elseif B738DR_fms_approach_flaps == 30 and simDR_flaps_handle_ratio_xp12 == 0.875 then
				app_flaps_set = 1
			elseif B738DR_fms_approach_flaps == 40 and simDR_flaps_handle_ratio_xp12 == 1 then
				app_flaps_set = 1
			end
		end
	end
	B738DR_approach_flaps_set = app_flaps_set
	
	local to_trim_set = 0
	local trim_xxx = 0
	local trim_yyy = simDR_elevator_trim
	local trim_aaa = 0
	if B738DR_trim_calc > 0 and B738DR_trim_calc < 15 then
		trim_yyy = trim_yyy + 1
		trim_xxx = B738_rescale(0, 0, 1.588244, 15, trim_yyy) - 1
		trim_aaa = B738DR_trim_calc - trim_xxx
		if trim_aaa < 0 then
			trim_aaa = -trim_aaa
		end
		if trim_aaa <= 0.15 then
			to_trim_set = 1
		end
	end
	--DR_test = trim_xxx
	B738DR_trim_set = to_trim_set
	
end


function B738_des_now()
	
	local des_now_nm = 0
	local tod_nm = 100
	
	if crz_alt_num >= 15000 then
		des_now_nm = B738_rescale(15000, 10, 41000, 70, crz_alt_num)
	end
	
	des_now_enable = 0
	if B738DR_flight_phase == 2 and B738DR_autopilot_vnav_status == 1 
	and des_now_nm ~= 0 and B738DR_vnav_td_dist < des_now_nm then
		des_now_enable = 1
	end
	
end

function B738_displ_acf()
	
	local nd_lat_acf = ndx_lat
	local nd_lon_acf = ndx_lon
	local nd_lat = 0
	local nd_lon = 0
	local mag_hdg = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	local nd_hdg = 0
	local delta_hdg = 0
	local nd_zoom = 0
	
	local enable_acf = false
	local enable_acf_fo = false
	
	-- if rte12_sel == 0 then
		-- if ref_icao ~= "----" and des_icao ~= "****" then
			-- enable_acf = true
		-- elseif ref_icao_x ~= "----" and des_icao_x ~= "****" then
			-- enable_acf = true
		-- end
	-- elseif rte12_sel == 1 then
		-- if ref_icao7 ~= "----" and des_icao7 ~= "****" then
			-- enable_acf = true
		-- elseif ref_icao7_x ~= "----" and des_icao7_x ~= "****" then
			-- enable_acf = true
		-- end
	-- end
	-- if rte12_sel_fo == 0 then
		-- if ref_icao ~= "----" and des_icao ~= "****" then
			-- enable_acf_fo = true
		-- elseif ref_icao_x ~= "----" and des_icao_x ~= "****" then
			-- enable_acf_fo = true
		-- end
	-- elseif rte12_sel_fo == 1 then
		-- if ref_icao7 ~= "----" and des_icao7 ~= "****" then
			-- enable_acf_fo = true
		-- elseif ref_icao7_x ~= "----" and des_icao7_x ~= "****" then
			-- enable_acf_fo = true
		-- end
	-- end
	
	if ref_icao ~= "----" and des_icao ~= "****" then
		enable_acf = true
	elseif ref_icao_x ~= "----" and des_icao_x ~= "****" then
		enable_acf = true
	elseif ref_icao7 ~= "----" and des_icao7 ~= "****" then
		enable_acf = true
	elseif ref_icao7_x ~= "----" and des_icao7_x ~= "****" then
		enable_acf = true
	end
	enable_acf_fo = enable_acf
	
	if legs_num > 0 and B738DR_irs_nd_allign_12 == 0 and (enable_acf == true or enable_acf_fo == true) then 
		
		-- CAPTAIN
		if B738DR_capt_map_mode == 3 and enable_acf == true then
			if rte12_sel == 0 then
				if legs_step > legs_num2 then
					legs_step = legs_num2
				end
				if legs_step == 0 then
					-- nd_lat = legs_data2[1][7]
					-- nd_lon = legs_data2[1][8]
					nd_lat, nd_lon = lat_lon_legs2(1)
				else
					-- nd_lat = legs_data2[legs_step][7]
					-- nd_lon = legs_data2[legs_step][8]
					nd_lat, nd_lon = lat_lon_legs2(legs_step)
				end
			else
				if legs_step > legs_num8 then
					legs_step = legs_num8
				end
				if legs_step == 0 then
					-- nd_lat = legs_data2[1][7]
					-- nd_lon = legs_data2[1][8]
					nd_lat, nd_lon = lat_lon_legs8(1)
				else
					-- nd_lat = legs_data2[legs_step][7]
					-- nd_lon = legs_data2[legs_step][8]
					nd_lat, nd_lon = lat_lon_legs8(legs_step)
				end
			end
			
			mag_hdg = -simDR_mag_variation
			
			nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat_acf, nd_lon_acf)
			
			if nd_dis < 645 then
				
				nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat_acf, nd_lon_acf)
				delta_hdg = calc_rel_brg(nd_hdg, mag_hdg)
					
				if delta_hdg >= 0 and delta_hdg <= 90 then
					-- right
					delta_hdg = 90 - delta_hdg
					delta_hdg = math.rad(delta_hdg)
					nd_y = nd_dis * math.sin(delta_hdg)
					nd_x = nd_dis * math.cos(delta_hdg)
				elseif delta_hdg < 0 and delta_hdg >= -90 then
					-- left
					delta_hdg = 90 + delta_hdg
					delta_hdg = math.rad(delta_hdg)
					nd_y = nd_dis * math.sin(delta_hdg)
					nd_x = -nd_dis * math.cos(delta_hdg)
				elseif delta_hdg >= 90 then
					-- right back
					delta_hdg = delta_hdg - 90
					delta_hdg = math.rad(delta_hdg)
					nd_y = -nd_dis * math.sin(delta_hdg)
					nd_x = nd_dis * math.cos(delta_hdg)
				elseif delta_hdg <= -90 then
					-- left back
					delta_hdg = -90 - delta_hdg
					delta_hdg = math.rad(delta_hdg)
					nd_y = -nd_dis * math.sin(delta_hdg)
					nd_x = -nd_dis * math.cos(delta_hdg)
				end
				
				if B738DR_efis_map_range_capt == 0 then	-- 5 NM
					nd_zoom = 2
				elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
					nd_zoom = 1
				elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
					nd_zoom = 0.5
				elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
					nd_zoom = 0.25
				elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
					nd_zoom = 0.125
				elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
					nd_zoom = 0.0625
				elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
					nd_zoom = 0.03125
				else	-- 640 NM
					nd_zoom = 0.015625
				end
				
				--nd_zoom = nd_zoom / 2
				nd_zoom = nd_zoom * 0.57
				
				nd_x = nd_x * nd_zoom		-- zoom
				nd_y = nd_y * nd_zoom		-- zoom
				nd_y = nd_y + 4.1	-- adjust center
				
				B738DR_acf_show = 1
				B738DR_acf_x = nd_x
				B738DR_acf_y = nd_y
				if simDR_radio_height_pilot_ft > 50 then
					--B738DR_acf_rot = ndx_mag_hdg
					B738DR_acf_rot = ndx_ahars_mag_hdg
				else
					B738DR_acf_rot = ndx_ahars_mag_hdg
				end
			else
				B738DR_acf_show = 0
			end
		else
			B738DR_acf_show = 0
		end
		
		-- FIRST OFFICER
		if B738DR_fo_map_mode == 3 and enable_acf_fo == true then
			
			if rte12_sel_fo == 0 then
				if legs_step2 > legs_num2 then
					legs_step2 = legs_num2
				end
				if legs_step2 == 0 then
					-- nd_lat = legs_data2[1][7]
					-- nd_lon = legs_data2[1][8]
					nd_lat, nd_lon = lat_lon_legs2(1)
				else
					-- nd_lat = legs_data2[legs_step2][7]
					-- nd_lon = legs_data2[legs_step2][8]
					nd_lat, nd_lon = lat_lon_legs2(legs_step2)
				end
			else
				if legs_step2 > legs_num8 then
					legs_step2 = legs_num8
				end
				if legs_step2 == 0 then
					-- nd_lat = legs_data2[1][7]
					-- nd_lon = legs_data2[1][8]
					nd_lat, nd_lon = lat_lon_legs8(1)
				else
					-- nd_lat = legs_data2[legs_step2][7]
					-- nd_lon = legs_data2[legs_step2][8]
					nd_lat, nd_lon = lat_lon_legs8(legs_step2)
				end
			end
			
			mag_hdg = -simDR_mag_variation
			
			nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat_acf, nd_lon_acf)
			--nd_dis = nd_calc_dist_d(nd_lat, nd_lon, nd_lat_acf, nd_lon_acf)
			
			if nd_dis < 645 then
				
				nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat_acf, nd_lon_acf)
				--nd_hdg = nd_calc_brg_d(nd_lat, nd_lon, nd_lat_acf, nd_lon_acf)
				delta_hdg = calc_rel_brg(nd_hdg, mag_hdg)
					
				if delta_hdg >= 0 and delta_hdg <= 90 then
					-- right
					delta_hdg = 90 - delta_hdg
					delta_hdg = math.rad(delta_hdg)
					nd_y = nd_dis * math.sin(delta_hdg)
					nd_x = nd_dis * math.cos(delta_hdg)
				elseif delta_hdg < 0 and delta_hdg >= -90 then
					-- left
					delta_hdg = 90 + delta_hdg
					delta_hdg = math.rad(delta_hdg)
					nd_y = nd_dis * math.sin(delta_hdg)
					nd_x = -nd_dis * math.cos(delta_hdg)
				elseif delta_hdg >= 90 then
					-- right back
					delta_hdg = delta_hdg - 90
					delta_hdg = math.rad(delta_hdg)
					nd_y = -nd_dis * math.sin(delta_hdg)
					nd_x = nd_dis * math.cos(delta_hdg)
				elseif delta_hdg <= -90 then
					-- left back
					delta_hdg = -90 - delta_hdg
					delta_hdg = math.rad(delta_hdg)
					nd_y = -nd_dis * math.sin(delta_hdg)
					nd_x = -nd_dis * math.cos(delta_hdg)
				end
				
				if B738DR_efis_map_range_fo == 0 then	-- 5 NM
					nd_zoom = 2
				elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
					nd_zoom = 1
				elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
					nd_zoom = 0.5
				elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
					nd_zoom = 0.25
				elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
					nd_zoom = 0.125
				elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
					nd_zoom = 0.0625
				elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
					nd_zoom = 0.03125
				else	-- 640 NM
					nd_zoom = 0.015625
				end
				
				--nd_zoom = nd_zoom / 2
				nd_zoom = nd_zoom * 0.57
				
				nd_x = nd_x * nd_zoom		-- zoom
				nd_y = nd_y * nd_zoom		-- zoom
				nd_y = nd_y + 4.1	-- adjust center
				
				B738DR_acf_fo_show = 1
				B738DR_acf_x_fo = nd_x
				B738DR_acf_y_fo = nd_y
				if simDR_radio_height_pilot_ft > 50 then
					--B738DR_acf_rot_fo = ndx_mag_hdg
					B738DR_acf_rot_fo = ndx_ahars_mag_hdg
				else
					B738DR_acf_rot_fo = ndx_ahars_mag_hdg
				end
			else
				B738DR_acf_fo_show = 0
			end
		else
			B738DR_acf_fo_show = 0
		end
	else
		B738DR_acf_show = 0
		B738DR_acf_fo_show = 0
	end
	
end

function calc_rel_brg(x_brg1, x_brg2)
	
	return ((x_brg1 - x_brg2) + 720) % 360
	
end

function calc_dist(c_spd, c_vvi, c_d_alt)

	local calc_temp = 0
	local calc_time = 0
	
	calc_time = c_vvi / 60				-- ft/s
	if calc_time == 0 then
		calc_temp = 92600	-- 50 NM
	else
		calc_time = c_d_alt / calc_time		-- sec
		calc_temp = c_spd * 0.5144444444	-- m/s
		calc_temp = calc_temp * calc_time	-- m
	end
	return  calc_temp * 0.0005399568	-- NM
	

end

function calc_alt_vvi(c_spd, c_vvi, c_dist)

	local calc_temp = 0
	local calc_time = 0
	
	calc_time = c_spd * 0.5144444444	-- m/s
	if calc_time == 0 then
		calc_temp = 1000
	else
		calc_time = c_dist * 1852 / calc_time 		-- sec
		calc_temp = c_vvi / 60				-- ft/s
		calc_temp = calc_temp * calc_time	-- sec
	end
	return  calc_temp	-- delta alt
	

end

function calc_clb_vvi(in_alt, in_gw)
	local clb1_vvi = vnav_vvi1[in_alt] * 0.85
	local clb2_vvi = vnav_vvi2[in_alt] * 0.9
	local clb_gw = math.max(in_gw, 48000)
	clb_gw = math.min(clb_gw, 69000)
	local result = B738_rescale(48000, clb1_vvi, 69000, clb2_vvi, clb_gw)
	return result
end

function vnav_timer()
	vnav_update = 1
end

function vnav_timer_mod()
	vnav_update_mod = 1
end


function find_clb_rest_spd(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	
	if rest_from <= legs_num and rest_to <= legs_num and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data[ii][19] == 1 or legs_data[ii][19] == 3 or legs_data[ii][19] == 10 then
					if legs_data[ii][4] > 0 and legs_data[ii][42] <= 1 then
						finded_rest_idx = ii
						break
					end
				end
			end
		else
			for ii = rest_from, rest_to, -1 do
				if legs_data[ii][19] == 1 or legs_data[ii][19] == 3 or legs_data[ii][19] == 10 then
					if legs_data[ii][4] > 0 and legs_data[ii][42] <= 1 then
						finded_rest_idx = ii
						break
					end
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_spd2(rest_from)
	
	local ii = 0
	local finded_rest_idx = 0
	local rest_to = legs_num
	
	if rest_from <= legs_num and rest_to <= legs_num and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data[ii][44] >= 60000 then
					if legs_data[ii][4] > 0 and legs_data[ii][42] <= 1 then
						finded_rest_idx = ii
						break
					end
				elseif legs_data[ii][44] == 0 then
					break
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_alt(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	
	if rest_from <= legs_num and rest_to <= legs_num and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data[ii][19] == 1 or legs_data[ii][19] == 3 then
					if legs_data[ii][41] > 0 and legs_data[ii][6] == 41 then
						finded_rest_idx = ii
						break
					elseif legs_data[ii][5] > 0 and legs_data[ii][6] ~= 43 then
						finded_rest_idx = ii
						break
					end
				end
			end
		else
			for ii = rest_from, rest_to, -1 do
				if legs_data[ii][19] == 1 or legs_data[ii][19] == 3 then
					if legs_data[ii][41] > 0 and legs_data[ii][6] == 41 then
						finded_rest_idx = ii
						break
					elseif legs_data[ii][5] > 0 and legs_data[ii][6] ~= 43 then
						finded_rest_idx = ii
						break
					end
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_alt2(rest_from)
	
	local ii = 0
	local finded_rest_idx = 0
	local rest_to = legs_num
	
	if rest_from <= legs_num and rest_to <= legs_num and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data[ii][44] >= 60000 then
					if legs_data[ii][41] > 0 and legs_data[ii][6] == 41 then
						finded_rest_idx = ii
						break
					elseif legs_data[ii][5] > 0 and legs_data[ii][6] ~= 43 then
						finded_rest_idx = ii
						break
					end
				elseif legs_data[ii][44] == 0 then
					break
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_spd_mod(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	
	if rest_from <= legs_num2_buf and rest_to <= legs_num2_buf and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data2_buf[ii][19] == 1 or legs_data2_buf[ii][19] == 3 or legs_data2_buf[ii][19] == 10 then
					if legs_data2_buf[ii][4] > 0 and legs_data2_buf[ii][42] <= 1 then
						finded_rest_idx = ii
						break
					end
				end
			end
		else
			for ii = rest_from, rest_to, -1 do
				if legs_data2_buf[ii][19] == 1 or legs_data2_buf[ii][19] == 3 or legs_data2_buf[ii][19] == 10 then
					if legs_data2_buf[ii][4] > 0 and legs_data2_buf[ii][42] <= 1 then
						finded_rest_idx = ii
						break
					end
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_spd2_mod(rest_from)
	
	local ii = 0
	local finded_rest_idx = 0
	local rest_to = legs_num2_buf
	
	if rest_from <= legs_num2_buf and rest_to <= legs_num2_buf and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data2_buf[ii][44] >= 60000 then
					if legs_data2_buf[ii][4] > 0 and legs_data2_buf[ii][42] <= 1 then
						finded_rest_idx = ii
						break
					end
				elseif legs_data2_buf[ii][44] == 0 then
					break
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_alt_mod(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	
	if rest_from <= legs_num2_buf and rest_to <= legs_num2_buf and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data2_buf[ii][19] == 1 or legs_data2_buf[ii][19] == 3 then
					if legs_data2_buf[ii][41] > 0 and legs_data2_buf[ii][6] == 41 then
						finded_rest_idx = ii
						break
					elseif legs_data2_buf[ii][5] > 0 and legs_data2_buf[ii][6] ~= 43 then
						finded_rest_idx = ii
						break
					end
				end
			end
		else
			for ii = rest_from, rest_to, -1 do
				if legs_data2_buf[ii][19] == 1 or legs_data2_buf[ii][19] == 3 then
					if legs_data2_buf[ii][41] > 0 and legs_data2_buf[ii][6] == 41 then
						finded_rest_idx = ii
						break
					elseif legs_data2_buf[ii][5] > 0 and legs_data2_buf[ii][6] ~= 43 then
						finded_rest_idx = ii
						break
					end
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function find_clb_rest_alt2_mod(rest_from)
	
	local ii = 0
	local finded_rest_idx = 0
	local rest_to = legs_num2_buf
	
	if rest_from <= legs_num2_buf and rest_to <= legs_num2_buf and rest_from > 0 and rest_to > 0 then
		
		if rest_from <= rest_to then
			for ii = rest_from, rest_to do
				if legs_data2_buf[ii][44] >= 60000 then
					if legs_data2_buf[ii][41] > 0 and legs_data2_buf[ii][6] == 41 then
						finded_rest_idx = ii
						break
					elseif legs_data2_buf[ii][5] > 0 and legs_data2_buf[ii][6] ~= 43 then
						finded_rest_idx = ii
						break
					end
				elseif legs_data2_buf[ii][44] == 0 then
					break
				end
			end
		end
		
	end
	
	return finded_rest_idx
	
end

function wind_pth(wpt_brg, wind_over, wind_pred)
	
	local tmp_wind_spd = 0
	local tmp_wind_dir = 0
	local result = 0
	
	if wind_over == "" then
		if wind_pred ~= "" then
			tmp_wind_dir = tonumber(string.sub(wind_pred, 1, 3))
			tmp_wind_spd = tonumber(string.sub(wind_pred, -3, -1))
		end
	else
		tmp_wind_dir = tonumber(string.sub(wind_over, 1, 3))
		tmp_wind_spd = tonumber(string.sub(wind_over, -3, -1))
	end
	
	local wc_speed = 0
	if tmp_wind_spd > 0 then
		wc_speed = tmp_wind_spd * math.cos(math.rad(Angle180(tmp_wind_dir))-wpt_brg)
		
		wc_speed = math.min(wc_speed, 200)
		wc_speed = math.max(wc_speed, -200)
		
		--result = B738_rescale(-200, 1.05, 200, 0.95, wc_speed)
		--result = B738_rescale(-200, 1.045, 200, 0.955, wc_speed)
		result = B738_rescale(-200, 0.955, 200, 1.045, wc_speed)
	else
		result = 1
	end
	
	return	result
end

function wind_pth_old(wpt_brg, wind_over, wind_pred)
	
	local tmp_wind_spd = 0
	local tmp_wind_dir = 0
	local result = 0
	
	if wind_over == "" then
		if wind_pred ~= "" then
			tmp_wind_dir = tonumber(string.sub(wind_pred, 1, 3))
			tmp_wind_spd = tonumber(string.sub(wind_pred, -3, -1))
		end
	else
		tmp_wind_dir = tonumber(string.sub(wind_over, 1, 3))
		tmp_wind_spd = tonumber(string.sub(wind_over, -3, -1))
	end
	
	local wc_speed = 0
	if tmp_wind_spd > 0 then
		wc_speed = tmp_wind_spd * math.cos(math.rad(Angle180(tmp_wind_dir))-wpt_brg)
		
		wc_speed = math.min(wc_speed, 200)
		wc_speed = math.max(wc_speed, -200)
		
		--result = B738_rescale(-200, 1.20, 200, 0.80, wc_speed)
		--result = B738_rescale(-200, 1.05, 200, 0.95, wc_speed)
		result = B738_rescale(-200, 1.045, 200, 0.955, wc_speed)
	else
		result = 1
	end
	
	return	result
end

function lnav_prior()
	local lnav_prior_arm = 0
	local delta_crs = 0
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		if legs_num > 2 and offset > 0 and offset < 4 then
			if string.sub(legs_data[2][1], 1, 2) == "RW" then
				delta_crs = (ref_runway_crs - math.deg(legs_data[3][2]) + 360) % 360
				if delta_crs > 180 then
					delta_crs = delta_crs - 360
				end
				if delta_crs < 0 then
					delta_crs = -delta_crs
				end
				if delta_crs <= 6 then
					lnav_prior_arm = 1
				end
			end
		end
	end
	B738DR_lnav_prior_arm = lnav_prior_arm
end

function B738_vnav_calc()

	local n = 0
	local nn = 0
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local delta_alt = 0
	local dist_10000 = 0
	local rest_idx_spd = 0
	local rest_idx_alt = 0
	local rest_idx_spd_p = 0
	local rest_idx_alt_p = 0
	local dist = 0
	local calc_wpt_alt = 0
	local calc_wpt_spd = 0
	local max_rest_spd = 0
	local calc_wpt_alt_distr = 0
	local calc_vvi = 0
	local rest_idx = 0
	local rest_spd = 0
	local rest_alt = 0
	local rest_alt_t = 0
	local rest_alt_idx = 0
	local calc_spd_alt = 0
	
	local temp_d_R = 0
	local temp_brg = 0
	local temp_lat = 0
	local temp_lon = 0
	local lat_wpt = 0
	local lon_wpt = 0
	
	local from_spd = 0
	
	local td_spd_rest_loc = 0
	local skip_vpa = 0
	local discon = 0
	
	local recalc_spd = 0
	local recalc_alt = 0
	local first_idx_alt = 1
	
	local climb_calc_enable = 0
	
	
	local ed_fix_dist = 0
	local ed_fix_vpa = 0
	local ignored = 0
	local idx_temp = 0
	local decel_idx_temp = 0
	local last_wpt_idx = 0
	--local temp_idx = 0
	
	local offset2 = 0

	local nd_lat2 = 0
	local nd_lon2= 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_y = 0
	local nd_x = 0
	
	local alt_calc_temp = 0
	local alt_calc_temp2 = 0
	
	local init_alt = 0
	local init_spd = 0
	local init_vvi = 0
	local init_distance = 0
	local first_restrict = 0
	local temp_alt = 0
	local temp_vpa = 0
	local temp_idx = 0
	local temp_type = 0
	local temp_table = 0
	local temp_table_distance = 0
	local temp_tl = 0
	
	local spd_lim = 0
	local spd_vvi_corr = 0
	
	local pom5 = 0
	local pom6 = 0
	local wind_temp = 0
	local wind_temp_old = 0
	local dist2 = 0
	
	local td_dist_temp = 0
	local first_app_rest = 0
	local td2_founded = 0
	local first_check_idx = 0
	
	local lat_tmp = 0
	local lon_tmp = 0
	
	-- local leg_dist = 0
	
	if legs_delete_old2 ~= legs_delete and legs_delete == 0 then
		vnav_update = 1
		msg_chk_alt_constr = 0
		msg_using_rsv_fuel = 0
		msg_using_ins_fuel = 0
	end
	
	if vnav_update == 1 and calc_rte_enable == 0 then
	
	time_calc_enable = 1
	-- fuel_calculated = 0
	
	if is_timer_scheduled(vnav_timer) == true then
		stop_timer(vnav_timer)
	end
	
	if true_legs_num(1) > 0 then
		offset = math.max(offset, 2)
		if offset_seq < offset or offset_seq > legs_num then
			offset_seq = offset
		end
	else
		offset = 0
		offset_seq = 0
	end
	
	local use_ref_icao = ""
	local use_des_icao = ""
	
	if rte12_act == 0 then
		use_ref_icao = ref_icao
		use_des_icao = des_icao
	elseif rte12_act == 1 then
		use_ref_icao = ref_icao7
		use_des_icao = des_icao7
	end
	
	--B738DR_fms_test1
	
	--if crz_alt_num > 0 and legs_num > 2 and offset > 0 and perf_exec > 0 and ref_icao ~= "----" and des_icao ~= "****" then
	if crz_alt_num > 0 and legs_num > 1 and offset > 0 and perf_exec > 0 and use_ref_icao ~= "----" and use_des_icao ~= "****" then
	
		if offset > legs_num then
			offset = legs_num
			offset_seq = offset
		end
		if offset == 0 then
			offset = 1
			offset_seq = offset
		end
		-- if offset == 1 and true_legs_num(1) > 0 then
			-- offset = 2
			-- offset_seq = offset
		-- end

		--if legs_data[offset][1] == ref_icao and ground_air == 0 then
		if offset == 1 and ground_air == 0 then
			offset2 = 2
		else
			offset2 = offset
		end

		--clear calculations data
		for n = offset2, (legs_num + 1) do
		-- for n = 1, (legs_num + 1) do
			if string.sub(legs_data[n][1], 1, 2) == "RW" and n == 2 and legs_data[n][19] == 1 then
				legs_data[n][11] = ref_icao_alt
			elseif n > 1 then
				legs_data[n][11] = 0
			end
			legs_data[n][10] = 0
			legs_data[n][14] = 50000
			legs_data[n][53] = 0
		end

		------------------------------------------------
		-- CLIMB PHASE
		climb_calc_enable = 0
		if B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8 then
			climb_calc_enable = 1
		end
		
		if tc_idx == 0 then
			if B738DR_flight_phase == 2 then
				tc_disco = offset - 1
				tc_dist = B738DR_rt_abeam_dist_bp
				if tc_disco > 1 then
					if tc_disco > last_sid_idx + 1 then
						tc_disco = last_sid_idx + 1
						tc_dist = 0
					end
					tc_idx = tc_disco
					-- tc_dist = 0
				end
			elseif B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and after_load_flight_TMR ~= 0 then
				tc_disco = offset - 1
				tc_dist = B738DR_rt_abeam_dist_bp
				if tc_disco > 1 then
					if tc_disco > last_sid_idx + 1 then
						tc_disco = last_sid_idx + 1
						tc_dist = 0
					else
						
					end
					tc_idx = tc_disco
					-- tc_dist = 0
				end
			end
		end
		
		if tc_idx > legs_num + 1 then
			tc_idx = 0
		end
		
		-- if ed_found ~= 0 then
			-- if offset2 > ed_found then
				-- climb_calc_enable = 0
			-- end
		-- end
		
		if climb_calc_enable == 1 then
			
			tc_idx = 0
			tc_disco = 0
			--jj = legs_num + 1
			
			-- intial speed and alt
			if ground_air == 0 then
				if string.sub(legs_data[2][1], 1, 2) == "RW" then
					if B738DR_fms_v2_set == 0 then
						calc_wpt_spd = 145
					else
						calc_wpt_spd = B738DR_fms_v2_set + 20
					end
				else
					calc_wpt_spd = 205
				end
				if rte12_act == 0 then
					calc_wpt_alt = ref_icao_alt
				else
					calc_wpt_alt = ref_icao_alt7
				end
			else
				calc_wpt_spd = AP_airspeed
				calc_wpt_alt = AP_altitude
			end
			
			-- find restricts spd and alt
			if legs_data[offset2][44] >= 50000 then
				rest_idx_spd = find_clb_rest_spd2(offset2)
				if rest_idx_spd == 0 then
					rest_spd = B738DR_fmc_climb_speed
				else
					rest_spd = legs_data[rest_idx_spd][4]
				end
				rest_idx_alt = find_clb_rest_alt2(offset2)
				if rest_idx_alt == 0 then
					rest_alt = 45000
				else
					if legs_data[rest_idx_alt][6] == 41 then
						rest_alt = legs_data[rest_idx_alt][41]
					else
						rest_alt = legs_data[rest_idx_alt][5]
					end
				end
			elseif offset2 > last_sid_idx or last_sid_idx == 0 then
				rest_spd = 340
				rest_alt = 45000
			else
				rest_idx_spd = find_clb_rest_spd(offset2, last_sid_idx)
				if rest_idx_spd == 0 then
					rest_spd = B738DR_fmc_climb_speed
				else
					rest_spd = legs_data[rest_idx_spd][4]
				end
				rest_idx_alt = find_clb_rest_alt(offset2, last_sid_idx)
				if rest_idx_alt == 0 then
					rest_alt = 45000
				else
					if legs_data[rest_idx_alt][6] == 41 then
						rest_alt = legs_data[rest_idx_alt][41]
					else
						rest_alt = legs_data[rest_idx_alt][5]
					end
				end
			end
			
			if rest_alt < 45000 and calc_wpt_alt > rest_alt then
				calc_wpt_alt = rest_alt
			end
			
			if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
				calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
			else
				calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed)
			end
			
			local calc_vvi_alt = 0
			local calc_vvi_1 = 0
			local calc_vvi_2 = 0
			
			local clb_vvi_gw = simDR_total_weight
			nn = 0
			
			local disable_tc = 0
			
			for n = offset2, (legs_num + 1) do
				
				disable_tc = 0
				
				--if n ~= 2 or string.sub(legs_data[n][1], 1, 2) ~= "RW" then
				if n == 2 then
					if string.sub(legs_data[n][1], 1, 2) == "RW" and legs_data[n][19] == 1 then
						disable_tc = 1
					end
				end
				
				if disable_tc == 0 then
					-- calc vvi
					alt_calc_temp = math.max(2000, calc_wpt_alt)
					alt_calc_temp = math.min(41000, alt_calc_temp)
					alt_calc_temp = roundUpToIncrement(alt_calc_temp, 1000)
					
					if alt_calc_temp > 2000 then
						calc_vvi_alt = alt_calc_temp - 1000
						calc_vvi_1 = calc_clb_vvi(calc_vvi_alt, clb_vvi_gw)
						calc_vvi_2 = calc_clb_vvi(alt_calc_temp, clb_vvi_gw)
						calc_vvi = B738_rescale(calc_vvi_alt, calc_vvi_1, alt_calc_temp, calc_vvi_2, calc_wpt_alt)
					else
						calc_vvi = calc_clb_vvi(alt_calc_temp, clb_vvi_gw)
					end
					
					local max_dist = 0
					if n == offset2 then
						calc_spd_alt = calc_wpt_spd * (1 + (calc_wpt_alt / 1000 * 0.02))
						if legs_data[n][1] == "(VECTOR)" then
							max_dist = 0
						else
							max_dist = B738DR_fmc_dist
						end
					else
						if calc_wpt_spd == 0 then
							calc_spd_alt = ias_to_tas(160, legs_data[n-1][11])
						elseif calc_wpt_spd < 100 then
							calc_spd_alt = mach_to_tas(calc_wpt_spd, legs_data[n-1][11])
						else
							calc_spd_alt = ias_to_tas(calc_wpt_spd, legs_data[n-1][11])
						end
						max_dist = wpt_distance(n)
					end
					
					if n == offset2 and ground_air ~= 0 then
						calc_vvi = math.max(simDR_vvi_fpm_pilot - 100, calc_vvi * 0.8)
					end
					
					local calc_wpt_alt_prev = calc_wpt_alt
					local cur_dist = max_dist
					local new_alt = alt_calc_temp
					local dist_to_1000 = calc_dist(calc_spd_alt, calc_vvi, alt_calc_temp - calc_wpt_alt)
					
					if dist_to_1000 > max_dist then
						calc_wpt_alt = calc_wpt_alt_prev + calc_alt_vvi(calc_spd_alt, calc_vvi, max_dist)
						if new_alt >= crz_alt_num then
							calc_wpt_alt = crz_alt_num
						end
						if rest_alt < 45000 and calc_wpt_alt > rest_alt then
							calc_wpt_alt = rest_alt
						end
						if calc_wpt_alt == crz_alt_num then
							-- T/C
							tc_dist = max_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num - calc_wpt_alt_prev)
							tc_idx = n
						end
					elseif new_alt >= crz_alt_num then
						calc_wpt_alt = crz_alt_num
						if rest_alt < 45000 and calc_wpt_alt > rest_alt then
							calc_wpt_alt = rest_alt
						end
						if calc_wpt_alt == crz_alt_num then
							-- T/C
							tc_dist = max_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num - calc_wpt_alt_prev)
							tc_idx = n
						end
					elseif rest_alt < 45000 and new_alt >= rest_alt then
						calc_wpt_alt = rest_alt
					else
						cur_dist = cur_dist - dist_to_1000
						while true do
							dist_to_1000 = calc_dist(calc_spd_alt, calc_vvi, 1000)
							
							if dist_to_1000 > cur_dist then
								calc_wpt_alt = new_alt + calc_alt_vvi(calc_spd_alt, calc_vvi, cur_dist)
								if calc_wpt_alt >= crz_alt_num then
									calc_wpt_alt = crz_alt_num
								end
								if rest_alt < 45000 and calc_wpt_alt > rest_alt then
									calc_wpt_alt = rest_alt
								end
								if calc_wpt_alt == crz_alt_num then
									-- T/C
									tc_dist = cur_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num - new_alt)
									tc_idx = n
								end
								break
							end
							
							new_alt = new_alt + 1000
							
							if new_alt >= crz_alt_num then
								calc_wpt_alt = crz_alt_num
								if rest_alt < 45000 and calc_wpt_alt > rest_alt then
									calc_wpt_alt = rest_alt
								end
								if calc_wpt_alt == crz_alt_num then
									-- T/C
									tc_dist = cur_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num - (new_alt - 1000))
									tc_idx = n
								end
								break
							elseif rest_alt < 45000 and new_alt >= rest_alt then
								calc_wpt_alt = rest_alt
								break
							end
							
							cur_dist = cur_dist - dist_to_1000
							if new_alt > 41000 then
								-- calc_vvi = vnav_vvi[42000] * 0.9
								calc_vvi = calc_clb_vvi(42000, clb_vvi_gw)
							elseif new_alt > 2000 then
								-- calc_vvi = vnav_vvi[new_alt + 1000] * 0.9
								calc_vvi = calc_clb_vvi(new_alt + 1000, clb_vvi_gw)
							else
								-- calc_vvi = vnav_vvi[2000] * 0.9
								calc_vvi = calc_clb_vvi(2000, clb_vvi_gw)
							end
							
							if n == offset2 and ground_air ~= 0 then
								calc_vvi = math.max(simDR_vvi_fpm_pilot - 100, calc_vvi * 0.8)
							end
							
							-- check restricts SPD
							if n > offset2 or ground_air ~= 0 then
								if new_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
									calc_spd_alt = math.min(rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
								elseif new_alt > 26000 and rest_spd == 340 then
									calc_spd_alt = B738DR_fmc_climb_speed_mach
								else
									calc_spd_alt = math.min(rest_spd, B738DR_fmc_climb_speed)
								end
								if calc_spd_alt == 0 then
									calc_spd_alt = ias_to_tas(160, new_alt)
								elseif calc_spd_alt < 100 then
									calc_spd_alt = mach_to_tas(calc_spd_alt, new_alt)
								else
									calc_spd_alt = ias_to_tas(calc_spd_alt, new_alt)
								end
							end
						end
					end
				end
				
				
				-- check restricts SPD
				if n == offset2 then
					if ground_air == 0 and string.sub(legs_data[2][1], 1, 2) == "RW" then
						calc_wpt_spd = calc_wpt_spd + (wpt_distance2(n, rte12_sel) / 0.128)
					else
						calc_wpt_spd = calc_wpt_spd + (B738DR_rt_abeam_dist_bp / 0.128)
					end
				else
					calc_wpt_spd = calc_wpt_spd + (wpt_distance(n) / 0.128)
				end
				
				if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
					max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
					calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
				elseif calc_wpt_alt > 26000 and rest_spd == 340 then
					max_rest_spd = B738DR_fmc_climb_speed_mach
					calc_wpt_spd = B738DR_fmc_climb_speed_mach
				else
					max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed)
					calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
				end
				
				if rest_alt == 45000 and legs_data[n][5] ~= 0 and tc_idx == 0 then
					if legs_data[n][6] == 45 or legs_data[n][6] == 0 or legs_data[n][6] == 32 then
						if calc_wpt_alt > legs_data[n][5] then
							calc_wpt_alt = legs_data[n][5]
							if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
								max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
								calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
							elseif calc_wpt_alt > 26000 and rest_spd == 340 then
								max_rest_spd = B738DR_fmc_climb_speed_mach
								calc_wpt_spd = B738DR_fmc_climb_speed_mach
							else
								max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed)
								calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
							end
							if n > 2 and n > offset2 then
								nn = n - 1
								while legs_data[nn][11] > legs_data[n][5] do
									legs_data[nn][11] = legs_data[n][5]
									if legs_data[nn][11] <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
										if legs_data[nn][53] > B738DR_fmc_climb_r_speed1 or legs_data[nn][53] == 0 then
											legs_data[nn][53] = B738DR_fmc_climb_r_speed1
										end
										if legs_data[nn][10] > B738DR_fmc_climb_r_speed1 then
											legs_data[nn][10] = B738DR_fmc_climb_r_speed1
										end
									elseif legs_data[nn][11] > 26000 and rest_spd == 340 then
										legs_data[nn][10] = B738DR_fmc_climb_speed_mach
										legs_data[nn][53] = B738DR_fmc_climb_speed_mach
									else
										if legs_data[nn][53] > B738DR_fmc_climb_speed or legs_data[nn][53] == 0 then
											legs_data[nn][53] = B738DR_fmc_climb_speed
										end
										if legs_data[nn][10] > B738DR_fmc_climb_speed then
											legs_data[nn][10] = B738DR_fmc_climb_speed
										end
									end
									nn = nn - 1
									if nn < 2 and nn > offset2 then
										break
									end
								end
							end
							-- break
						end
					elseif legs_data[n][6] == 41 then
						if calc_wpt_alt > legs_data[n][5] then
							calc_wpt_alt = legs_data[n][5]
							if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
								max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
								calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
							elseif calc_wpt_alt > 26000 and rest_spd == 340 then
								max_rest_spd = B738DR_fmc_climb_speed_mach
								calc_wpt_spd = B738DR_fmc_climb_speed_mach
							else
								max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed)
								calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
							end
							if n > 2 then
								nn = n - 1
								while legs_data[nn][11] > legs_data[n][5] do
									legs_data[nn][11] = legs_data[n][5]
									if legs_data[nn][11] <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
										if legs_data[nn][53] > B738DR_fmc_climb_r_speed1 or legs_data[nn][53] == 0 then
											legs_data[nn][53] = B738DR_fmc_climb_r_speed1
										end
										if legs_data[nn][10] > B738DR_fmc_climb_r_speed1 then
											legs_data[nn][10] = B738DR_fmc_climb_r_speed1
										end
									elseif legs_data[nn][11] > 26000 and rest_spd == 340 then
										legs_data[nn][10] = B738DR_fmc_climb_speed_mach
										legs_data[nn][53] = B738DR_fmc_climb_speed_mach
									else
										if legs_data[nn][53] > B738DR_fmc_climb_speed or legs_data[nn][53] == 0 then
											legs_data[nn][53] = B738DR_fmc_climb_speed
										end
										if legs_data[nn][10] > B738DR_fmc_climb_speed then
											legs_data[nn][10] = B738DR_fmc_climb_speed
										end
									end
									nn = nn - 1
									if nn < 2 then
										break
									end
								end
							end
							-- break
						end
					elseif legs_data[n][6] ~= 43 then 
						if calc_wpt_alt > legs_data[n][5] then
							calc_wpt_alt = legs_data[n][5]
							if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
								max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
								calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
							elseif calc_wpt_alt > 26000 and rest_spd == 340 then
								max_rest_spd = B738DR_fmc_climb_speed_mach
								calc_wpt_spd = B738DR_fmc_climb_speed_mach
							else
								max_rest_spd = math.min(rest_spd, B738DR_fmc_climb_speed)
								calc_wpt_spd = math.min(calc_wpt_spd, max_rest_spd)
							end
							if n > 2 then
								nn = n - 1
								while legs_data[nn][11] > legs_data[n][5] do
									legs_data[nn][11] = legs_data[n][5]
									if legs_data[nn][11] <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
										if legs_data[nn][53] > B738DR_fmc_climb_r_speed1 or legs_data[nn][53] == 0 then
											legs_data[nn][53] = B738DR_fmc_climb_r_speed1
										end
										if legs_data[nn][10] > B738DR_fmc_climb_r_speed1 then
											legs_data[nn][10] = B738DR_fmc_climb_r_speed1
										end
									elseif legs_data[nn][11] > 26000 and rest_spd == 340 then
										legs_data[nn][10] = B738DR_fmc_climb_speed_mach
										legs_data[nn][53] = B738DR_fmc_climb_speed_mach
									else
										if legs_data[nn][53] > B738DR_fmc_climb_speed or legs_data[nn][53] == 0 then
											legs_data[nn][53] = B738DR_fmc_climb_speed
										end
										if legs_data[nn][10] > B738DR_fmc_climb_speed then
											legs_data[nn][10] = B738DR_fmc_climb_speed
										end
									end
									nn = nn - 1
									if nn < 2 then
										break
									end
								end
							end
							-- break
						end
					end
				end
				
				legs_data[n][11] = calc_wpt_alt
				if legs_data[n][21] == 0 or legs_data[n][21] == 1 then
					calc_wpt_spd = hold_speed_calc(legs_data[n][4], legs_data[n][11])
					max_rest_spd = calc_wpt_spd
				end
				legs_data[n][10] = calc_wpt_spd
				legs_data[n][53] = max_rest_spd
				max_rest_spd = 0
				
				if tc_idx == 0 then
					if n <= legs_num then
						-- find next restricts ALT/SPD
						ii = n + 1
						if legs_data[ii][44] >= 50000 then
							-- find restricts spd and alt
							rest_idx_spd = find_clb_rest_spd2(ii)
							if rest_idx_spd == 0 then
								rest_spd = B738DR_fmc_climb_speed
							else
								rest_spd = legs_data[rest_idx_spd][4]
							end
							rest_idx_alt = find_clb_rest_alt2(ii)
							if rest_idx_alt == 0 then
								rest_alt = 45000
							else
								if legs_data[rest_idx_alt][6] == 41 then
									rest_alt = legs_data[rest_idx_alt][41]
								else
									rest_alt = legs_data[rest_idx_alt][5]
								end
							end
						elseif ii > legs_num or ii > last_sid_idx or last_sid_idx == 0 then
							rest_spd = 340
							rest_alt = 45000
						else
							-- find restricts spd and alt
							rest_idx_spd = find_clb_rest_spd(ii, last_sid_idx)
							if rest_idx_spd == 0 then
								rest_spd = B738DR_fmc_climb_speed
							else
								rest_spd = legs_data[rest_idx_spd][4]
							end
							rest_idx_alt = find_clb_rest_alt(ii, last_sid_idx)
							if rest_idx_alt == 0 then
								rest_alt = 45000
							else
								if legs_data[rest_idx_alt][6] == 41 then
									rest_alt = legs_data[rest_idx_alt][41]
								else
									rest_alt = legs_data[rest_idx_alt][5]
								end
							end
						end
					else
						rest_spd = 340
						rest_alt = 45000
					end
				else
					break
				end
			end
			
			if B738DR_fms_test == 444 then
				B738DR_fms_test = 0
			end
			
			if tc_idx ~= 0 then
				if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
					while wpt_distance(tc_idx) < tc_dist do
						if tc_idx > 2 then
							tc_dist = tc_dist - wpt_distance(tc_idx)
							tc_idx = tc_idx - 1
						else
							break
						end
					end
					-- calc lat/lon
					lat_tmp, lon_tmp = lat_lon_legs(tc_idx)
					if legs_intdir_act ~= 0 and tc_idx == offset2 then
						-- calc_brg_dist(legs_data[tc_idx][7], legs_data[tc_idx][8], math.rad((B738DR_fmc_trk + 180) % 360), td_dist)
						--calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), td_dist)
						calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), tc_dist)
					else
						-- calc_brg_dist(legs_data[tc_idx][7], legs_data[tc_idx][8], legs_data[tc_idx][2] + math.pi, td_dist)
						--calc_brg_dist(lat_tmp, lon_tmp, legs_data[tc_idx][2] + math.pi, td_dist)
						calc_brg_dist(lat_tmp, lon_tmp, legs_data[tc_idx][2] + math.pi, tc_dist)
					end
					B738DR_tc_lat = calc_lat
					B738DR_tc_lon = calc_lon
				else
					B738DR_tc_lat = 0
					B738DR_tc_lon = 0
				end
			end
		end
		
		B738DR_fms_test1 = 502
		--------------------------------------------------------------------------------------------
		--------------------------------------------------------------------------------------------
		-- Find E/D, T/D and DECEL
		--------------------------------------------------------------------------------------------
		--------------------------------------------------------------------------------------------
		
		if true then
			
			----------------------------------
			if B738DR_fms_test == 5 then
				dump_leg_mod()
				B738DR_fms_test = 0
			end
			----------------------------------
			local td_idx2_tmp = 0
			local td_alt2_tmp	= 0
			local td_dist2_tmp = 0
			
			td_idx = 0
			td_idx2 = 0
			td_dist99 = 0
			td_idx_break = 0
			ed_tl = 0
			distance_tl = 0
			
			local active_altitude = 0
			
			first_alt_restrict = 0
			-- find E/D
			ed_found = 0
			discon = 0
			-- ed_found_type = 0
			if idx_ed > 0 and idx_ed <= legs_num + 1 then
				ed_found = idx_ed
				ed_alt = legs_data[ed_found][5]
				ed_vpa = econ_des_vpa
				
				if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 and idx_ed ~= 0 and rnav_idx_last > rnav_idx_first then
					if legs_data[idx_ed][5] == 0 or legs_data[idx_ed][6] == 43 or legs_data[idx_ed][6] == 2 then
						-- find last rnav idx with alt
						nn = rnav_idx_last
						for n = rnav_idx_first, rnav_idx_last do
							if legs_data[n][5] ~= 0 and (legs_data[n][6] == 0 or legs_data[n][6] == 32) and legs_data[n][20] < 0 then
								nn = n
							end
						end
						
						--calc distance
						pom5 = 0
						for n = nn, idx_ed + 1, -1 do
							pom5 = pom5 + legs_data[n][3]
						end
						
						--calc alt
						if legs_data[nn][20] >= 0 then
							ed_alt = (math.tan(math.rad(3)) * pom5 * 6076.11549) + legs_data[nn][5]
						else
							ed_alt = (math.tan(math.rad(-legs_data[nn][20])) * pom5 * 6076.11549) + legs_data[nn][5]
						end
						
						if legs_data[idx_ed][5] ~= 0 then
							if ed_alt < legs_data[idx_ed][5] then
								ed_alt = legs_data[idx_ed][5]
							end
						end
					end
				end
			end
			
			if discon == 0 then
				--td_found = 0
				td_dist = 0 
				decel_idx = 0
				decel_wpt_idx = 0
				decel_wpt_dist = 0
				decel_wpt_spd = 0
				B738DR_FL100_idx2 = 0
				B738DR_FL100_dist2 = 0
				skip_vpa = 0
				if ed_found == 0 then	-- not found e/d
					ed_found = legs_num + 1
					ed_alt = math.floor((des_icao_alt + 1000)/100) * 100
					ed_vpa = econ_des_vpa
					if legs_data[ed_found][1] == nil then
						skip_vpa = 1
					end
				end
				
				if skip_vpa == 0 then
					
					local tl_wpt_created = 0
					local tl_wpt_just_created = 0
					local tl_wpt_disable = 0
					local tl_wpt_alt = 0
					local tl_wpt_dis = 0
					local tl_wpt_dis_tmp = 0
					local idx_rest_temp = 0
					local alt_rest_temp = 0
					
					if tc_idx == 0 then
						if last_sid_idx == 0 then
							jj = 2
						else
							jj = last_sid_idx + 1
						end
					else
						jj = tc_idx --- 1
						if jj < 1 then
							jj = 1
						end
					end
					idx_rest_temp, alt_rest_temp = find_alt_restrict_low(ed_found - 1, jj)
					if ed_alt == B738DR_trans_lvl and ed_alt ~= alt_rest_temp then
						tl_wpt_created = 1
						tl_wpt_just_created = 1
						ed_alt = alt_qnh_dn(B738DR_trans_lvl, B738DR_forecast_in_hg, 15 + B738DR_forecast_isa_dev)
						ed_tl = 1
					elseif B738DR_trans_lvl < ed_alt then
						tl_wpt_created = 1
						tl_wpt_just_created = 0
					end
					
					--vnavsk
					
					-----------------------------------------------------------
					-- find T/D , econ vnav descent path
					-----------------------------------------------------------
					--create_vpth_table(gw_app_kgs, ed_alt)
					B738DR_fms_test1 = 50
					create_desc_wind_table()
					B738DR_fms_test1 = 501
					vnav_des_table_dist = {}
					vnav_des_table_alt = {}
					vnav_des_table_spd = {}
					vnav_des_table_flap = {}
					vnav_des_table_act_spd = {}
					vnav_des_table_type = {}
					vnav_des_table_num = {}
					create_idle_path(ed_found, ed_alt, crz_alt_num, 1, 0)
					B738DR_fms_test1 = 51
					ed_dist = calc_vnav_pth_dist(ed_alt, crz_alt_num, 1)
					B738DR_fms_test1 = 52
					last_wpt_idx = 0
					ed_type = 0
					
					ed_fix_found2 = {}
					ed_fix_alt2 = {}
					ed_fix_vpa2 = {}
					ed_fix_type = {}
					ed_fix_num = 0
					ed_fix_table = {}
					ed_fix_distance = {}
					ed_fix_tl = {}
					
					local idx_last_rest_temp = 0
					local alt_last_rest_temp = 0
					-- local idx_rest_temp = 0
					-- local alt_rest_temp = 0
					local wpt_rest_alt = 0
					local wpt_rest_alt2 = 0
					--local ed_alt_temp = ed_alt
					local ed_dist_temp = 0
					local ed_table_temp = 0
					local skip_wpt_alt = 0
					local enable_cruise_alt = 0
					
					local segment_dist = 0
					local segment_delta_alt = 0
					local tl_td = 0
					
					local total_distance = 0
					
					ed_fix_alt = ed_alt
					td_dist_temp = 0
					
					local wpt_toc = 1
					
					first_check_idx = jj
					
					idx_last_rest_temp, alt_last_rest_temp = find_last_alt_rest_low(ed_found - 1, jj)
					enable_cruise_alt = 1
					-- if crz_alt_num <= alt_last_rest_temp and idx_last_rest_temp ~= 0 then
						-- enable_cruise_alt = 1
						-- if legs_data[idx_last_rest_temp][6] == 41 then
							-- if crz_alt_num < legs_data[idx_last_rest_temp][5] then
								-- enable_cruise_alt = 0
							-- end
						-- end
					-- else
						-- enable_cruise_alt = 1
					-- end
					idx_rest_temp, alt_rest_temp = find_alt_restrict_low(ed_found - 1, jj)
					--B738DR_fms_test3 = alt_rest_temp
					
					if B738DR_fms_test == 737 then
						-- activate dump data
						dump_leg_append("\n")
						dump_leg_append("Dump data \n")
					end
					
					local td_disco = 0
					local iii = 0
					leg_dist = 0
					--**********************************
					calc_wpt_alt = ed_alt
					-- calc sum distance
					if legs_data[ed_found][1] ~= "(VECTOR)" then
						leg_dist = wpt_distance(ed_found)
						td_dist_temp = td_dist_temp + leg_dist
						segment_dist = segment_dist + leg_dist
					end
					if legs_data[ed_found][17] == 1 and B738DR_fms_vref_40 > 0 then
						legs_data[ed_found][10] = B738DR_fms_vref_40 + 20
						max_rest_spd = legs_data[ed_found][10]
					else
						legs_data[ed_found][10] = 0
						legs_data[ed_found][53] = 0
					end
					legs_data[ed_found][11] = ed_alt
					legs_data[ed_found][14] = 50000
					
					local last_at_below_idx = 0
					local last_at_below_alt = 0
					local last_at_below_alt_tmp = 0
					for ii = ed_found-1, wpt_toc, -1 do
						if legs_data[ii][5] ~= 0 and ii > last_sid_idx then
							last_at_below_alt_tmp = 50000
							if legs_data[ii][6] == 0 or legs_data[ii][6] == 45 or legs_data[ii][6] == 32 then
								-- at or below
								last_at_below_alt_tmp = legs_data[ii][5]
							elseif legs_data[ii][6] == 41 then
								-- between
								last_at_below_alt_tmp = legs_data[ii][41]
							end
							
							if B738DR_trans_lvl == last_at_below_alt_tmp then
								last_at_below_alt = last_at_below_alt_tmp
								last_at_below_idx = ii - 1
							elseif B738DR_trans_lvl > last_at_below_alt_tmp then
								last_at_below_alt = last_at_below_alt_tmp
								last_at_below_idx = ii - 1
							end
						end
					end
					
					if B738DR_fms_test == 737 then
						dump_leg_append("\n")
						dump_leg_append("Last at/below Alt: " .. tostring(last_at_below_alt) .. "\n")
						dump_leg_append("Last at/below Idx: " .. tostring(last_at_below_idx) .. "\n")
						dump_leg_append("\n")
					end
					
					for ii = ed_found-1, wpt_toc, -1 do
					--**********************************
					-- for ii = ed_found, wpt_toc, -1 do
						
						legs_data[ii][49] = 0
						
						if string.sub(legs_data[ii][1], 1, 2) == "RW" and ii == 2 and legs_data[ii][19] == 1 then
							legs_data[ii][11] = ref_icao_alt
						elseif ii > 1 then
							legs_data[ii][11] = 0
						end
						legs_data[ii][10] = 0
						legs_data[ii][14] = 50000
						legs_data[ii][53] = 0
						
						iii = ii
						
							if skip_wpt_alt ~= 0 then
								calc_wpt_alt = ed_fix_alt
							else
								if leg_dist ~= 0 then
									calc_wpt_alt = calc_vnav_pth_alt(ed_fix_alt, td_dist_temp, ed_fix_num + 1)
									--- !!!!!!!!!!!!!
									-- calc_wpt_alt = calc_vnav_pth_alt(ed_fix_alt, td_dist_temp - tl_wpt_dis, ed_fix_num + 1)
									--- !!!!!!!!!!!!!
								end
							end
							
							wpt_rest_alt = 0
							wpt_rest_alt2 = 0
							if legs_data[ii][5] ~= 0 and ii > last_sid_idx then
								if legs_data[ii][6] == 0 or legs_data[ii][6] == 45 or legs_data[ii][6] == 32 then
									-- at or below
									wpt_rest_alt = legs_data[ii][5]
									if legs_data[ii][6] ~= 45 then
										-- at
										wpt_rest_alt2 = legs_data[ii][5]
									end
								elseif legs_data[ii][6] == 41 then
									-- between
									wpt_rest_alt = legs_data[ii][41]
									wpt_rest_alt2 = legs_data[ii][5]
								elseif legs_data[ii][6] == 43 then
									-- above
									wpt_rest_alt2 = legs_data[ii][5]
								end
							end
							
							if B738DR_fms_test == 737 then
								dump_leg_append("\n")
								dump_leg_append(tostring(ii) .. ": " .. legs_data[ii][1] .. "\n")
								dump_leg_append("Last Alt rest: " .. tostring(alt_last_rest_temp) .. "\n")
								dump_leg_append("Alt at/above limit: " .. tostring(alt_rest_temp) .. "\n")
								dump_leg_append("WPT Alt rest1 below: " .. tostring(wpt_rest_alt) .. "\n")
								dump_leg_append("WPT Alt rest2 above: " .. tostring(wpt_rest_alt2) .. "\n")
								dump_leg_append("Precalc alt  : " .. tostring(calc_wpt_alt) .. "\n")
								dump_leg_append("segment_dist = " .. tostring(segment_dist) .. "\n")
								dump_leg_append("td_dist_temp = " .. tostring(td_dist_temp) .. "\n")
								dump_leg_append("idx_rest_temp = " .. tostring(idx_rest_temp) .. "\n")
								dump_leg_append("skip_wpt_alt = " .. tostring(skip_wpt_alt) .. "\n")
							end
							
							if idx_rest_temp ~= 0 then
								------ ****************** -----------
								tl_wpt_disable = 0
								
								if B738DR_trans_lvl >= crz_alt_num then
									tl_wpt_disable = 1
								elseif last_at_below_idx ~= 0 then
									if ii > last_at_below_idx then
										tl_wpt_disable = 1
									end
								end
								
								if calc_wpt_alt >= B738DR_trans_lvl and tl_wpt_created == 0 and tl_wpt_disable == 0 then
									tl_wpt_created = 1
									tl_wpt_just_created = 1
									
									
									tl_wpt_dis = calc_vnav_pth_dist(ed_fix_alt, B738DR_trans_lvl, ed_fix_num + 1)
									tl_wpt_dis_tmp = segment_dist - tl_wpt_dis
									tl_wpt_dis = wpt_distance(ii+1) - tl_wpt_dis_tmp
									
									segment_dist = tl_wpt_dis_tmp
									td_dist_temp = tl_wpt_dis_tmp
									
									tl_wpt_alt = alt_qnh_dn(B738DR_trans_lvl, B738DR_forecast_in_hg, 15 + B738DR_forecast_isa_dev)
									ed_fix_num = ed_fix_num + 1
									ed_fix_found2[ed_fix_num] = ii + 1
									
									ed_fix_alt2[ed_fix_num] = tl_wpt_alt
									ed_fix_vpa2[ed_fix_num] = econ_des_vpa
									ed_fix_type[ed_fix_num] = 0	-- idle path
									ed_fix_table[ed_fix_num] = ed_fix_num + 1
									ed_fix_tl[ed_fix_num] = 1
									ed_fix_alt = tl_wpt_alt
									create_idle_path(ii+1, ed_fix_alt, crz_alt_num, ed_fix_num + 1, tl_wpt_dis)
									calc_wpt_alt = calc_vnav_pth_alt(ed_fix_alt, tl_wpt_dis_tmp, ed_fix_num + 1)
									
									if B738DR_fms_test == 737 then
										dump_leg_append("TL waypoint idx: " .. tostring(ed_fix_found2[ed_fix_num]) .. " " .. legs_data[ed_fix_found2[ed_fix_num]][1] .. "\n")
										dump_leg_append("tl_wpt_dis   = " .. tostring(tl_wpt_dis) .. "\n")
										dump_leg_append("segment_dist = " .. tostring(segment_dist) .. "\n")
										dump_leg_append("td_dist_temp = " .. tostring(td_dist_temp) .. "\n")
										dump_leg_append("calc_wpt_alt = " .. tostring(calc_wpt_alt) .. "\n")
									end
								end
								
								if calc_wpt_alt > crz_alt_num and enable_cruise_alt ~= 0 
								and (calc_wpt_alt <= alt_rest_temp or crz_alt_num <= alt_rest_temp) then
									if ii <= legs_num then
										-- T/D
										if crz_alt_num <= ed_fix_alt then
											calc_wpt_alt = crz_alt_num
											legs_data[ii][11] = calc_wpt_alt
											td_dist = tl_wpt_dis
											td_idx = ii + 1
											if (ii + 2) <= (legs_num + 1) then
												td_dist99 = td_dist + wpt_distance(ii+2)
											else
												td_dist99 = td_dist
											end
										else
											calc_wpt_alt = crz_alt_num
											legs_data[ii][11] = calc_wpt_alt
											ed_dist_temp = calc_vnav_pth_dist(ed_fix_alt, crz_alt_num, ed_fix_num + 1)
											td_dist = ed_dist_temp - (td_dist_temp - wpt_distance(ii+1))
											
											if td_dist < 0 then
												td_dist = 0
											end
											
											td_idx = ii + 1	-- before idx
											if (ii + 2) <= (legs_num + 1) then
												td_dist99 = td_dist + wpt_distance(ii+2)
											else
												td_dist99 = td_dist
											end
										end
										td_idx_break = td_idx
										if td_idx == tc_idx and td_dist > tc_dist and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
											td_idx = 0
										elseif td_idx < tc_idx then
											td_idx = 0
										end
									end
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt > crz_alt_num 1 = ")
										if td_idx <= legs_num and td_idx > 0 then
											dump_leg_append(tostring(td_idx) .. " " .. legs_data[td_idx][1].. " = " .. tostring(td_dist))
										end
										dump_leg_append("\n")
										dump_leg_append("td_dist_temp = " .. tostring(td_dist_temp) .. "\n")
										dump_leg_append("ed_dist_temp = " .. tostring(ed_dist_temp) .. "\n")
										-- dump_leg_append("tl_wpt_dis   = " .. tostring(tl_wpt_dis) .. "\n")
										-- dump_leg_append("ed_fix_alt   = " .. tostring(ed_fix_alt) .. "\n")
									end
									break
								elseif calc_wpt_alt == crz_alt_num and enable_cruise_alt ~= 0 and calc_wpt_alt <= alt_rest_temp then
									-- T/D
									if crz_alt_num <= ed_fix_alt then
										calc_wpt_alt = crz_alt_num
										legs_data[ii][11] = calc_wpt_alt
										td_dist = tl_wpt_dis
										td_idx = ii + 1
										if (ii + 2) <= (legs_num + 1) then
											td_dist99 = td_dist + wpt_distance(ii+2)
										else
											td_dist99 = td_dist
										end
									else
										legs_data[ii][11] = calc_wpt_alt
										td_dist = 0
										td_idx = ii
										if (ii + 1) <= (legs_num + 1) then
											td_dist99 = td_dist + wpt_distance(ii+1)
										else
											td_dist99 = td_dist
										end
									end
									td_idx_break = td_idx
									if td_idx == tc_idx and td_dist > tc_dist and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
										td_idx = 0
									elseif td_idx < tc_idx then
										td_idx = 0
									end
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt > crz_alt_num 2 = ")
										if td_idx <= legs_num and td_idx > 0 then
											dump_leg_append(tostring(td_idx) .. " " .. legs_data[td_idx][1].. " = " .. tostring(td_dist))
										end
										dump_leg_append("\n")
										dump_leg_append("td_dist_temp = " .. tostring(td_dist_temp) .. "\n")
										dump_leg_append("ed_dist_temp = " .. tostring(ed_dist_temp) .. "\n")
										dump_leg_append("tl_wpt_dis   = " .. tostring(tl_wpt_dis) .. "\n")
										dump_leg_append("ed_fix_alt   = " .. tostring(ed_fix_alt) .. "\n")
									end
									break
								elseif calc_wpt_alt < crz_alt_num and enable_cruise_alt ~= 0 and crz_alt_num <= wpt_rest_alt2 then
									-- T/D
									-- recalc geometric descent path
									if ed_fix_num == 0 then
										recalc_vnav(ed_fix_num, ii, crz_alt_num, segment_dist, 0)
									else
										if ed_fix_tl[ed_fix_num] == 0 then
											recalc_vnav(ed_fix_num, ii, crz_alt_num, segment_dist, 0)
										else
											recalc_vnav(ed_fix_num, ii, crz_alt_num, segment_dist, tl_wpt_dis)
										end
									end
									td_dist = 0
									td_idx = ii
									if (ii + 1) <= (legs_num + 1) then
										td_dist99 = td_dist + wpt_distance(ii+1)
									else
										td_dist99 = td_dist
									end
									td_idx_break = td_idx
									if td_idx == tc_idx and td_dist > tc_dist and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
										td_idx = 0
									elseif td_idx < tc_idx then
										td_idx = 0
									end
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt > crz_alt_num 3 = ")
										if td_idx <= legs_num and td_idx > 0 then
											dump_leg_append(tostring(td_idx) .. " " .. legs_data[td_idx][1].. " = " .. tostring(td_dist))
										end
										dump_leg_append("\n")
										dump_leg_append("td_dist_temp = " .. tostring(td_dist_temp) .. "\n")
										dump_leg_append("ed_dist_temp = " .. tostring(ed_dist_temp) .. "\n")
										dump_leg_append("tl_wpt_dis   = " .. tostring(tl_wpt_dis) .. "\n")
										dump_leg_append("ed_fix_alt   = " .. tostring(ed_fix_alt) .. "\n")
									end
									break
								elseif calc_wpt_alt > alt_rest_temp and alt_rest_temp ~= 0 then
									-- create T/D-alt waypoint
									if ed_fix_alt == alt_rest_temp then
										if ed_fix_num == 0 then
											ed_type = 1
											ed_vpa = 0
										else
											ed_fix_type[ed_fix_num] = 1
											ed_fix_vpa2[ed_fix_num] = 0
										end
										calc_wpt_alt = alt_rest_temp
										skip_wpt_alt = 1
										ed_fix_alt = alt_rest_temp
										
										if B738DR_fms_test == 737 then
											dump_leg_append("ed_fix_alt == alt_rest_temp\n")
										end
									else
										-- if tl_wpt_just_created == 0 or ed_fix_alt <= alt_rest_temp then
										if alt_rest_temp < ed_fix_alt then
											
											if B738DR_fms_test == 737 then
												dump_leg_append("alt_rest_temp < ed_fix_alt\n")
											end
											
											if ii + 1 >= offset and ii <= legs_num then
												if B738DR_fms_test == 737 then
													dump_leg_append("ii + 1 >= offset and ii <= legs_num\n")
												end
												
												td_idx2_tmp = ii + 1
												td_alt2_tmp	= alt_rest_temp
												td_dist2_tmp = tl_wpt_dis
												
												if B738DR_altitude_mode == 5 then
													if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
														active_altitude = B738DR_preselected_alt
													else
														if td_idx2_tmp > offset then
															active_altitude = simDR_altitude_pilot
														else
															active_altitude = simDR_altitude_pilot - 150
														end
													end
												elseif (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
													active_altitude = B738DR_preselected_alt
												else
													if td_idx2_tmp > offset then
														active_altitude = simDR_altitude_pilot
													else
														active_altitude = simDR_altitude_pilot - 150
													end
												end
												if td_idx2_tmp == offset and B738DR_fmc_dist < td_dist2_tmp then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
													
													if B738DR_fms_test == 737 then
														dump_leg_append("td_idx2 == offset and B738DR_fmc_dist < td_dist2\n")
													end
													
												elseif td_alt2_tmp > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
													
													if B738DR_fms_test == 737 then
														dump_leg_append("td_alt2 > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8\n")
													end
													
												elseif td_alt2_tmp == legs_data[ii+1][11] then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
													
													if B738DR_fms_test == 737 then
														dump_leg_append("td_alt2 == legs_data[ii+1][11]\n")
													end
													
												end
												
												if td_idx2_tmp ~= 0 then
													td_idx2 = td_idx2_tmp
													td_alt2 = td_alt2_tmp
													td_dist2 = td_dist2_tmp
												end
											end
											calc_wpt_alt = alt_rest_temp
											skip_wpt_alt = 1
											ed_fix_alt = alt_rest_temp
										else
											if ii + 1 >= offset and ii <= legs_num then
												
												if B738DR_fms_test == 737 then
													dump_leg_append("ii + 1 >= offset and ii <= legs_num\n")
												end
												
												ed_dist_temp = calc_vnav_pth_dist(ed_fix_alt, alt_rest_temp, ed_fix_num + 1)
												td_dist2_tmp = ed_dist_temp - (td_dist_temp - wpt_distance(ii+1))
												
												td_idx2_tmp = ii + 1
												td_alt2_tmp	= alt_rest_temp
												if B738DR_altitude_mode == 5 then
													if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 ) then
														active_altitude = B738DR_preselected_alt
													else
														if td_idx2_tmp > offset then
															active_altitude = simDR_altitude_pilot
														else
															active_altitude = simDR_altitude_pilot - 150
														end
													end
												elseif (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
													active_altitude = B738DR_preselected_alt
												else
													if td_idx2_tmp > offset then
														active_altitude = simDR_altitude_pilot
													else
														active_altitude = simDR_altitude_pilot - 150
													end
												end
												if td_idx2_tmp == offset and B738DR_fmc_dist < td_dist2 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
													
													if B738DR_fms_test == 737 then
														dump_leg_append("td_idx2 == offset and B738DR_fmc_dist < td_dist2\n")
													end
													
												elseif td_alt2_tmp > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
													
													if B738DR_fms_test == 737 then
														dump_leg_append("td_alt2 > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8\n")
													end
													
												elseif td_alt2_tmp == legs_data[ii+1][11] then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
													
													if B738DR_fms_test == 737 then
														dump_leg_append("td_alt2 == legs_data[ii+1][11]\n")
													end
													
												end
												
												if td_idx2_tmp ~= 0 then
													td_idx2 = td_idx2_tmp
													td_alt2 = td_alt2_tmp
													td_dist2 = td_dist2_tmp
												end
											end
											calc_wpt_alt = alt_rest_temp
											skip_wpt_alt = 1
											ed_fix_alt = alt_rest_temp
										end
									end
									--tl_wpt_just_created = 0
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt > alt_rest_temp\n")
									end
								elseif calc_wpt_alt > wpt_rest_alt and wpt_rest_alt ~= 0 then
									-- create T/D-alt waypoint
									if ed_fix_alt == wpt_rest_alt then
										if ed_fix_num == 0 then
											ed_type = 1
											ed_vpa = 0
										else
											ed_fix_type[ed_fix_num] = 1
											ed_fix_vpa2[ed_fix_num] = 0
										end
										calc_wpt_alt = wpt_rest_alt
										skip_wpt_alt = 1
										ed_fix_alt = wpt_rest_alt
									else
										-- if tl_wpt_just_created == 0 or ed_fix_alt <= wpt_rest_alt then
										if alt_rest_temp < ed_fix_alt then
											if ii + 1 >= offset and ii <= legs_num then
												td_idx2_tmp = ii + 1
												td_alt2_tmp	= alt_rest_temp
												td_dist2_tmp = tl_wpt_dis
												if B738DR_altitude_mode == 5 then
													if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
														active_altitude = B738DR_preselected_alt
													else
														if td_idx2_tmp > offset then
															active_altitude = simDR_altitude_pilot
														else
															active_altitude = simDR_altitude_pilot - 150
														end
													end
												elseif (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
													active_altitude = B738DR_preselected_alt
												else
													if td_idx2_tmp > offset then
														active_altitude = simDR_altitude_pilot
													else
														active_altitude = simDR_altitude_pilot - 150
													end
												end
												if td_idx2_tmp == offset and B738DR_fmc_dist < td_dist2_tmp then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp == legs_data[ii+1][11] then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												end
											end
											
											if td_idx2_tmp ~= 0 then
												td_idx2 = td_idx2_tmp
												td_alt2 = td_alt2_tmp
												td_dist2 = td_dist2_tmp
											end
											calc_wpt_alt = alt_rest_temp
											skip_wpt_alt = 1
											ed_fix_alt = alt_rest_temp
										else
											if ii + 1 >= offset and ii <= legs_num then
												
												ed_dist_temp = calc_vnav_pth_dist(ed_fix_alt, wpt_rest_alt, ed_fix_num + 1)
												td_dist2_tmp = ed_dist_temp - (td_dist_temp - wpt_distance(ii+1))
												
												td_idx2_tmp = ii + 1
												td_alt2_tmp	= wpt_rest_alt
												if B738DR_altitude_mode == 5 then
													if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
														active_altitude = B738DR_preselected_alt
													else
														if td_idx2_tmp > offset then
															active_altitude = simDR_altitude_pilot
														else
															active_altitude = simDR_altitude_pilot - 150
														end
													end
												elseif (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
													active_altitude = B738DR_preselected_alt
												else
													if td_idx2_tmp > offset then
														active_altitude = simDR_altitude_pilot
													else
														active_altitude = simDR_altitude_pilot - 150
													end
												end
												if td_idx2_tmp == offset and B738DR_fmc_dist < td_dist2_tmp then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp == legs_data[ii+1][11] then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												end
												
												if td_idx2_tmp ~= 0 then
													td_idx2 = td_idx2_tmp
													td_alt2 = td_alt2_tmp
													td_dist2 = td_dist2_tmp
												end
											end
											calc_wpt_alt = wpt_rest_alt
											skip_wpt_alt = 1
											ed_fix_alt = wpt_rest_alt
										end
									end
									-- tl_wpt_just_created = 0
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt > alt_rest_alt\n")
									end
								elseif calc_wpt_alt < wpt_rest_alt2 and wpt_rest_alt2 ~= 0 then
									-- if tl_wpt_just_created == 0 then
										-- skip to new E/D -> idx_rest_temp
										
										ll = calc_wpt_alt
										calc_wpt_alt = wpt_rest_alt2
										
										-- if ii >= iaf_app_idx and iaf_app_idx ~= 0 then
										if ii >= first_app_idx and first_app_idx ~= 0 then
											-- recalc geometric descent path
											if ed_fix_num == 0 then
												recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
											else
												if ed_fix_tl[ed_fix_num] == 0 then
													recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
												else
													recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis)
												end
											end
											if B738DR_fms_test == 737 then
												dump_leg_append("Calc wpt < alt_rest_alt2 = Recalc\n")
											end
										else
											if ll < wpt_rest_alt2 - 150 then
												-- steep descent after
												legs_data[ii][49] = 1
											end
										end
										
										-- create new E/D
										segment_dist = 0
										ed_fix_num = ed_fix_num + 1
										ed_fix_found2[ed_fix_num] = ii
										ed_fix_alt2[ed_fix_num] = wpt_rest_alt2
										ed_fix_vpa2[ed_fix_num] = econ_des_vpa
										ed_fix_type[ed_fix_num] = 0	-- idle path
										ed_fix_table[ed_fix_num] = ed_fix_num + 1
										ed_fix_tl[ed_fix_num] = 0
										create_idle_path(ii, wpt_rest_alt2, crz_alt_num, ed_fix_num + 1, 0)
										td_dist_temp = 0
									-- else
										-- -- steep descent after
										-- legs_data[ii][49] = 1
									-- end
									
									if ii <= idx_rest_temp then
										skip_wpt_alt = 0
										idx_rest_temp, alt_rest_temp = find_alt_restrict_low(ii - 1, jj)
										ed_fix_alt = wpt_rest_alt2
									end
									-- tl_wpt_just_created = 0
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt < alt_rest_alt2 (1)\n")
									end
								end
								
								legs_data[ii][11] = calc_wpt_alt
								if idx_rest_temp == ii then
									
									-- if ed_fix_alt <= wpt_rest_alt then
									if ed_fix_alt <= wpt_rest_alt2 then
										
										legs_data[ii][11] = wpt_rest_alt2
										
										if skip_wpt_alt ~= 0 then
											
											if calc_wpt_alt < wpt_rest_alt2 then
												
												ll = calc_wpt_alt
												
												-- if ii >= iaf_app_idx and iaf_app_idx ~= 0 then
												if ii >= first_app_idx and first_app_idx ~= 0 then
													if ed_fix_num == 0 then
														recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
													else
														if ed_fix_tl[ed_fix_num] == 0 then
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
														else
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis)
														end
													end
													if B738DR_fms_test == 737 then
														dump_leg_append("Recalc 1\n")
													end
												else
													if ll < wpt_rest_alt2 - 150 then
														-- steep descent after
														legs_data[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num = ed_fix_num + 1
											ed_fix_found2[ed_fix_num] = ii
											ed_fix_alt2[ed_fix_num] = wpt_rest_alt2
											ed_fix_vpa2[ed_fix_num] = econ_des_vpa
											ed_fix_type[ed_fix_num] = 0	-- idle path
											ed_fix_table[ed_fix_num] = ed_fix_num + 1
											ed_fix_tl[ed_fix_num] = 0
											create_idle_path(ii, wpt_rest_alt2, crz_alt_num, ed_fix_num + 1, 0)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									elseif ed_fix_alt <= wpt_rest_alt then
										
										legs_data[ii][11] = wpt_rest_alt
										
										if skip_wpt_alt ~= 0 then
											
											if calc_wpt_alt < wpt_rest_alt then
												
												ll = calc_wpt_alt
												
												-- if ii >= iaf_app_idx and iaf_app_idx ~= 0 then
												if ii >= first_app_idx and first_app_idx ~= 0 then
													if ed_fix_num == 0 then
														recalc_vnav(ed_fix_num, ii, wpt_rest_alt, segment_dist, 0)
													else
														if ed_fix_tl[ed_fix_num] == 0 then
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt, segment_dist, 0)
														else
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt, segment_dist, tl_wpt_dis)
														end
													end
													if B738DR_fms_test == 737 then
														dump_leg_append("Recalc 1\n")
													end
												else
													if ll < wpt_rest_alt - 150 then
														-- steep descent after
														legs_data[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num = ed_fix_num + 1
											ed_fix_found2[ed_fix_num] = ii
											ed_fix_alt2[ed_fix_num] = wpt_rest_alt
											ed_fix_vpa2[ed_fix_num] = econ_des_vpa
											ed_fix_type[ed_fix_num] = 0	-- idle path
											ed_fix_table[ed_fix_num] = ed_fix_num + 1
											ed_fix_tl[ed_fix_num] = 0
											create_idle_path(ii, wpt_rest_alt, crz_alt_num, ed_fix_num + 1, 0)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									end
									skip_wpt_alt = 0
									idx_rest_temp, alt_rest_temp = find_alt_restrict_low(ii - 1, jj)
									-- tl_wpt_just_created = 0
								end
							else
								
								tl_wpt_disable = 0
								
								if B738DR_trans_lvl >= crz_alt_num then
									tl_wpt_disable = 1
								elseif last_at_below_idx ~= 0 then
									if ii > last_at_below_idx then
										tl_wpt_disable = 1
									end
								end
								
								if calc_wpt_alt >= B738DR_trans_lvl and tl_wpt_created == 0 and tl_wpt_disable == 0 then
									tl_wpt_created = 1
									tl_wpt_just_created = 1
									
									tl_wpt_dis = calc_vnav_pth_dist(ed_fix_alt, B738DR_trans_lvl, ed_fix_num + 1)
									tl_wpt_dis_tmp = segment_dist - tl_wpt_dis
									tl_wpt_dis = wpt_distance(ii+1) - tl_wpt_dis_tmp
									
									segment_dist = tl_wpt_dis_tmp
									td_dist_temp = tl_wpt_dis_tmp
									
									tl_wpt_alt = alt_qnh_dn(B738DR_trans_lvl, B738DR_forecast_in_hg, 15 + B738DR_forecast_isa_dev)
									ed_fix_num = ed_fix_num + 1
									ed_fix_found2[ed_fix_num] = ii + 1
									
									ed_fix_alt2[ed_fix_num] = tl_wpt_alt
									ed_fix_vpa2[ed_fix_num] = econ_des_vpa
									ed_fix_type[ed_fix_num] = 0	-- idle path
									ed_fix_table[ed_fix_num] = ed_fix_num + 1
									ed_fix_tl[ed_fix_num] = 1
									ed_fix_alt = tl_wpt_alt
									create_idle_path(ii+1, ed_fix_alt, crz_alt_num, ed_fix_num + 1, tl_wpt_dis)
									calc_wpt_alt = calc_vnav_pth_alt(ed_fix_alt, tl_wpt_dis_tmp, ed_fix_num + 1)
									
									
								end
								------ ****************** -----------
								
								if calc_wpt_alt > crz_alt_num then
									if ii <= legs_num then
										
										-- B738DR_fms_test3 = crz_alt_num
										-- B738DR_fms_test4 = legs_data[ii+1][11]
										-- B738DR_fms_test5 = ed_fix_alt
										
										if legs_data[ii+1][11] <= crz_alt_num then
											-- T/D
											if crz_alt_num <= ed_fix_alt then
												calc_wpt_alt = crz_alt_num
												legs_data[ii][11] = calc_wpt_alt
												td_dist = tl_wpt_dis
												td_idx = ii + 1
												if (ii + 2) <= (legs_num + 1) then
													td_dist99 = td_dist + wpt_distance(ii+2)
												else
													td_dist99 = td_dist
												end
											else
												calc_wpt_alt = crz_alt_num
												legs_data[ii][11] = calc_wpt_alt
												ed_dist_temp = calc_vnav_pth_dist(ed_fix_alt, crz_alt_num, ed_fix_num + 1)
												td_dist = ed_dist_temp - (td_dist_temp - wpt_distance(ii+1))
												
												if td_dist < 0 then
													td_dist = 0
												end
												-- B738DR_fms_test6 = ed_dist_temp
												-- B738DR_fms_test7 = wpt_distance(ii+1)
												-- B738DR_fms_test8 = td_dist_temp
												
												td_idx = ii + 1	-- before idx
												if (ii + 2) <= (legs_num + 1) then
													td_dist99 = td_dist + wpt_distance(ii+2)
												else
													td_dist99 = td_dist
												end
											end
											td_idx_break = td_idx
											if td_idx == tc_idx and td_dist > tc_dist and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
												td_idx = 0
											elseif td_idx < tc_idx then
												td_idx = 0
											end
										end
									end
									break
								elseif calc_wpt_alt == crz_alt_num then
									-- T/D
									if crz_alt_num <= ed_fix_alt then
										calc_wpt_alt = crz_alt_num
										legs_data[ii][11] = calc_wpt_alt
										td_dist = tl_wpt_dis
										td_idx = ii + 1
										if (ii + 2) <= (legs_num + 1) then
											td_dist99 = td_dist + wpt_distance(ii+2)
										else
											td_dist99 = td_dist
										end
									else
										legs_data[ii][11] = calc_wpt_alt
										td_dist = 0
										td_idx = ii
										if (ii + 1) <= (legs_num + 1) then
											td_dist99 = td_dist + wpt_distance(ii+1)
										else
											td_dist99 = td_dist
										end
									end
									td_idx_break = td_idx
									if td_idx == tc_idx and td_dist > tc_dist and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
										td_idx = 0
									elseif td_idx < tc_idx then
										td_idx = 0
									end
									break
								elseif calc_wpt_alt > wpt_rest_alt and wpt_rest_alt ~= 0 then
									-- create T/D-alt waypoint
									if ed_fix_alt == wpt_rest_alt then
										if ed_fix_num == 0 then
											ed_type = 1
											ed_vpa = 0
										else
											ed_fix_type[ed_fix_num] = 1
											ed_fix_vpa2[ed_fix_num] = 0
										end
										calc_wpt_alt = wpt_rest_alt
										skip_wpt_alt = 1
										ed_fix_alt = wpt_rest_alt
									else
										if alt_rest_temp < ed_fix_alt and alt_rest_temp ~= 0 then
											if ii + 1 >= offset and ii <= legs_num then
												td_idx2_tmp = ii + 1
												td_alt2_tmp	= alt_rest_temp
												td_dist2_tmp = tl_wpt_dis
												if B738DR_altitude_mode == 5 then
													if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
														active_altitude = B738DR_preselected_alt
													else
														if td_idx2_tmp > offset then
															active_altitude = simDR_altitude_pilot
														else
															active_altitude = simDR_altitude_pilot - 150
														end
													end
												elseif (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
													active_altitude = B738DR_preselected_alt
												else
													if td_idx2_tmp > offset then
														active_altitude = simDR_altitude_pilot
													else
														active_altitude = simDR_altitude_pilot - 150
													end
												end
												if td_idx2_tmp == offset and B738DR_fmc_dist < td_dist2_tmp then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp == legs_data[ii+1][11] then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												end
												
												if td_idx2_tmp ~= 0 then
													td_idx2 = td_idx2_tmp
													td_alt2 = td_alt2_tmp
													td_dist2 = td_dist2_tmp
												end
											end
											calc_wpt_alt = alt_rest_temp
											skip_wpt_alt = 1
											ed_fix_alt = alt_rest_temp
										else
											if ii + 1 >= offset and ii <= legs_num then
												
												ed_dist_temp = calc_vnav_pth_dist(ed_fix_alt, wpt_rest_alt, ed_fix_num + 1)
												td_dist2_tmp = ed_dist_temp - (td_dist_temp - wpt_distance(ii+1))
												
												td_idx2_tmp = ii + 1
												td_alt2_tmp	= wpt_rest_alt
												if B738DR_altitude_mode == 5 then
													if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
														active_altitude = B738DR_preselected_alt
													else
														if td_idx2_tmp > offset then
															active_altitude = simDR_altitude_pilot
														else
															active_altitude = simDR_altitude_pilot - 150
														end
													end
												elseif (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
													active_altitude = B738DR_preselected_alt
												else
													if td_idx2_tmp > offset then
														active_altitude = simDR_altitude_pilot
													else
														active_altitude = simDR_altitude_pilot - 150
													end
												end
												if td_idx2_tmp == offset and B738DR_fmc_dist < td_dist2_tmp then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp > active_altitude and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												elseif td_alt2_tmp == legs_data[ii+1][11] then
													td_idx2_tmp = 0
													td_alt2_tmp = 0
												end
												
												if td_idx2_tmp ~= 0 then
													td_idx2 = td_idx2_tmp
													td_alt2 = td_alt2_tmp
													td_dist2 = td_dist2_tmp
												end
											end
											calc_wpt_alt = wpt_rest_alt
											skip_wpt_alt = 1
											ed_fix_alt = wpt_rest_alt
										end
									end
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt > alt_rest_alt\n")
									end
								elseif calc_wpt_alt < wpt_rest_alt2 and wpt_rest_alt2 ~= 0 then
									-- if tl_wpt_just_created == 0 then
										-- skip to new E/D -> idx_rest_temp
										
										ll = calc_wpt_alt
										calc_wpt_alt = wpt_rest_alt2
										
										-- if ii >= iaf_app_idx and iaf_app_idx ~= 0 then
										if ii >= first_app_idx and first_app_idx ~= 0 then
											-- recalc geometric descent path
											if ed_fix_num == 0 then
												recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
											else
												if ed_fix_tl[ed_fix_num] == 0 then
													recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
												else
													recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis)
												end
											end
											if B738DR_fms_test == 737 then
												dump_leg_append("Calc wpt < alt_rest_alt2 = Recalc 2\n")
											end
										else
											if ll < wpt_rest_alt2 - 150 then
												-- steep descent after
												legs_data[ii][49] = 1
											end
										end
										
										-- create new E/D
										segment_dist = 0
										ed_fix_num = ed_fix_num + 1
										ed_fix_found2[ed_fix_num] = ii
										ed_fix_alt2[ed_fix_num] = wpt_rest_alt2
										ed_fix_vpa2[ed_fix_num] = econ_des_vpa
										ed_fix_type[ed_fix_num] = 0	-- idle path
										ed_fix_table[ed_fix_num] = ed_fix_num + 1
										ed_fix_tl[ed_fix_num] = 0
										create_idle_path(ii, wpt_rest_alt2, crz_alt_num, ed_fix_num + 1, 0)
										td_dist_temp = 0
									-- else
										-- -- steep descent after
										-- legs_data[ii][49] = 1
									-- end
									if ii <= idx_rest_temp then
										skip_wpt_alt = 0
										idx_rest_temp, alt_rest_temp = find_alt_restrict_low(ii - 1, jj)
										ed_fix_alt = wpt_rest_alt2
									end
									-- tl_wpt_just_created = 0
									if B738DR_fms_test == 737 then
										dump_leg_append("Calc wpt < alt_rest_alt2 (2)\n")
									end
								end
								
								legs_data[ii][11] = calc_wpt_alt
								
								if idx_rest_temp == ii then
									
									-- if ed_fix_alt <= wpt_rest_alt then
									if ed_fix_alt <= wpt_rest_alt2 then
										legs_data[ii][11] = wpt_rest_alt2
										if skip_wpt_alt ~= 0 then
											
											if calc_wpt_alt < wpt_rest_alt2 then
												
												ll = calc_wpt_alt
												
												-- if ii >= iaf_app_idx and iaf_app_idx ~= 0 then
												if ii >= first_app_idx and first_app_idx ~= 0 then
													if ed_fix_num == 0 then
														recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
													else
														if ed_fix_tl[ed_fix_num] == 0 then
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, 0)
														else
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis)
														end
													end
													if B738DR_fms_test == 737 then
														dump_leg_append("Recalc 2\n")
													end
												else
													if ll < wpt_rest_alt2 - 150 then
														-- steep descent after
														legs_data[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num = ed_fix_num + 1
											ed_fix_found2[ed_fix_num] = ii
											ed_fix_alt2[ed_fix_num] = wpt_rest_alt2
											ed_fix_vpa2[ed_fix_num] = econ_des_vpa
											ed_fix_type[ed_fix_num] = 0	-- idle path
											ed_fix_table[ed_fix_num] = ed_fix_num + 1
											ed_fix_tl[ed_fix_num] = 0
											create_idle_path(ii, wpt_rest_alt2, crz_alt_num, ed_fix_num + 1, 0)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									elseif ed_fix_alt <= wpt_rest_alt then
										legs_data[ii][11] = wpt_rest_alt
										if skip_wpt_alt ~= 0 then
											
											if calc_wpt_alt < wpt_rest_alt then
												
												ll = calc_wpt_alt
												
												-- if ii >= iaf_app_idx and iaf_app_idx ~= 0 then
												if ii >= first_app_idx and first_app_idx ~= 0 then
													if ed_fix_num == 0 then
														recalc_vnav(ed_fix_num, ii, wpt_rest_alt, segment_dist, 0)
													else
														if ed_fix_tl[ed_fix_num] == 0 then
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt, segment_dist, 0)
														else
															recalc_vnav(ed_fix_num, ii, wpt_rest_alt, segment_dist, tl_wpt_dis)
														end
													end
													if B738DR_fms_test == 737 then
														dump_leg_append("Recalc 2\n")
													end
												else
													if ll < wpt_rest_alt - 150 then
														-- steep descent after
														legs_data[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num = ed_fix_num + 1
											ed_fix_found2[ed_fix_num] = ii
											ed_fix_alt2[ed_fix_num] = wpt_rest_alt
											ed_fix_vpa2[ed_fix_num] = econ_des_vpa
											ed_fix_type[ed_fix_num] = 0	-- idle path
											ed_fix_table[ed_fix_num] = ed_fix_num + 1
											ed_fix_tl[ed_fix_num] = 0
											create_idle_path(ii, wpt_rest_alt, crz_alt_num, ed_fix_num + 1, 0)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									end
									skip_wpt_alt = 0
									idx_rest_temp, alt_rest_temp = find_alt_restrict_low(ii - 1, jj)
								end
							end
						if B738DR_fms_test == 737 then
							dump_leg_append("Calc    alt  : " .. tostring(calc_wpt_alt) .. "\n")
						end
						
						-- calc sum distance
						if legs_data[ii][1] ~= "(VECTOR)" then
							leg_dist = wpt_distance(ii)
							if skip_wpt_alt == 0 then
								td_dist_temp = td_dist_temp + leg_dist
								segment_dist = segment_dist + leg_dist
							end
						end
						
						if td_idx ~= 0 then
							break
						end
					end
					
					
					if td_idx == 0 then
						td_idx_break = tc_idx - 1
					end
					-- if td_disco ~= 0 then
						-- if iii >= offset then
							-- td_idx = 0
						-- end
					-- end
					
					
					if td_idx == 0 then
						td_idx2 = 0
						td_alt2 = 0
					else
						if B738DR_flight_phase < 5 then
							while wpt_distance(td_idx) < td_dist do
								if td_idx > 2 then
									td_dist = td_dist - wpt_distance(td_idx)
									td_idx = td_idx - 1
								else
									break
								end
							end
							-- calc lat/lon
							lat_tmp, lon_tmp = lat_lon_legs(td_idx)
							if legs_intdir_act ~= 0 and td_idx == offset2 then
								-- calc_brg_dist(legs_data[td_idx][7], legs_data[td_idx][8], math.rad((B738DR_fmc_trk + 180) % 360), td_dist)
								calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), td_dist)
							else
								-- calc_brg_dist(legs_data[td_idx][7], legs_data[td_idx][8], legs_data[td_idx][2] + math.pi, td_dist)
								calc_brg_dist(lat_tmp, lon_tmp, legs_data[td_idx][2] + math.pi, td_dist)
							end
							B738DR_td_lat = calc_lat
							B738DR_td_lon = calc_lon
						else
							B738DR_td_lat = 0
							B738DR_td_lon = 0
							if td_idx2 ~= 0 then
								while wpt_distance(td_idx2) < td_dist2 do
									if td_idx2 > 2 then
										td_dist2 = td_dist2 - wpt_distance(td_idx2)
										td_idx2 = td_idx2 - 1
									else
										break
									end
								end
								lat_tmp, lon_tmp = lat_lon_legs(td_idx2)
								if legs_intdir_act ~= 0 and td_idx2 == offset2 then
									-- calc_brg_dist(legs_data[td_idx2][7], legs_data[td_idx2][8], math.rad((B738DR_fmc_trk + 180) % 360), td_dist2)
									calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), td_dist2)
								else
									-- calc_brg_dist(legs_data[td_idx2][7], legs_data[td_idx2][8], legs_data[td_idx2][2] + math.pi, td_dist2)
									calc_brg_dist(lat_tmp, lon_tmp, legs_data[td_idx2][2] + math.pi, td_dist2)
								end
								B738DR_td_lat = calc_lat
								B738DR_td_lon = calc_lon
							end
						end
					end
					
					if B738DR_fms_test == 737 then
						B738DR_fms_test = 0
					end
					----------------------------------
					if B738DR_fms_test == 1 then
						dump_leg_mod()
						B738DR_fms_test = 0
					end
					B738DR_fms_test1 = 55
					
					-- order E/D restrict data
					if ed_fix_num > 1 then
						for kk = 1, ed_fix_num-1 do
							for ll = kk+1, ed_fix_num do
								if ed_fix_found2[kk] > ed_fix_found2[ll] or (ed_fix_found2[kk] == ed_fix_found2[ll] and ed_fix_tl[kk] == 0) then
									temp_idx = ed_fix_found2[kk]
									temp_alt = ed_fix_alt2[kk]
									temp_vpa = ed_fix_vpa2[kk]
									temp_type = ed_fix_type[kk]
									temp_table = ed_fix_table[kk]
									temp_tl = ed_fix_tl[kk]
									
									ed_fix_found2[kk] = ed_fix_found2[ll]
									ed_fix_alt2[kk] = ed_fix_alt2[ll]
									ed_fix_vpa2[kk] = ed_fix_vpa2[ll]
									ed_fix_type[kk] = ed_fix_type[ll]
									ed_fix_table[kk] = ed_fix_table[ll]
									ed_fix_tl[kk] = ed_fix_tl[ll]
									
									ed_fix_found2[ll] = temp_idx
									ed_fix_alt2[ll] = temp_alt
									ed_fix_vpa2[ll] = temp_vpa
									ed_fix_type[ll] = temp_type
									ed_fix_table[ll] = temp_table
									ed_fix_tl[ll] = temp_tl
								end
							end
						end
					end
					
					B738DR_fms_test1 = 56
					
					--B738DR_fms_test3 = ed_fix_num
					if ed_fix_num > 0 then
						total_distance = 0
						segment_dist = ed_fix_num
						for ii = ed_found, ed_fix_found2[1], -1 do
							for kk = 1, ed_fix_num do
								if ed_fix_found2[kk] == ii then
									if ed_fix_tl[kk] == 0 then
										ed_fix_distance[kk] = total_distance
									else
										ed_fix_distance[kk] = total_distance + tl_wpt_dis
										distance_tl = ed_fix_distance[kk]
									end
								end
							end
							
							if legs_data[ii][1] ~= "(VECTOR)" then
								total_distance = total_distance + wpt_distance(ii)
							end
						end
					end
					
					-- B738DR_fms_test7 = distance_tl
					
					B738DR_fms_test1 = 57
					
					if B738DR_fms_test == 99 then
						dump_leg_mod()
						B738DR_fms_test = 0
					end
					
					if B738DR_fms_test == 77 then
						dump_vnav_tab()
						B738DR_fms_test = 0
					end
					
					-------------------------------------------------------------
					-- calculate speed for wpts
					
					decel_idx = 0
					decel_dist2 = 0
					decel_wpt_idx = 0
					decel_wpt_dist = 0
					decel_wpt_spd = 0
					B738DR_FL100_idx2 = 0
					B738DR_FL100_dist2 = 0
					
					local decel_dist_temp = 0
					local calc_wpt_spd_temp = 0
					
					if td_idx == 0 then
						last_wpt_idx = offset2
					else
						last_wpt_idx = td_idx
					end
					
					local ed_alt_tmp = 0
					local ed_idx_tmp = 0
					local ed_tl_tmp = 0
					local ed_vpa_tmp = 0
					local ed_type_tmp = 0
					local ed_table_tmp = 0
					-- FIXs:
					if crz_alt_num > B738DR_fmc_descent_r_alt1 then	--and B738DR_FL100_idx ~= 0 then
						if ed_fix_num > 0 then
							-- for n = ed_fix_num, 1, -1 do
							for n = 1, ed_fix_num + 1 do
								if n == ed_fix_num + 1 then
									ed_alt_tmp = ed_alt
									ed_idx_tmp = ed_found
									ed_tl_tmp = ed_tl
									ed_vpa_tmp = ed_vpa
									ed_type_tmp = ed_type
									ed_table_tmp = 1
								else
									ed_alt_tmp = ed_fix_alt2[n]
									ed_idx_tmp = ed_fix_found2[n]
									ed_tl_tmp = ed_fix_tl[n]
									ed_vpa_tmp = ed_fix_vpa2[n]
									ed_type_tmp = ed_fix_type[n]
									ed_table_tmp = ed_fix_table[n]
								end
								
								if B738DR_fms_test == 767 then
									dump_leg_append("FL100 - check table " .. tostring(n) .. ": " .. tostring(ed_alt_tmp) .. "\n")
								end
								if ed_alt_tmp == B738DR_fmc_descent_r_alt1 then
									B738DR_FL100_idx2 = ed_idx_tmp
									if ed_tl_tmp == 1 then
										B738DR_FL100_dist2 = tl_wpt_dis
									else
										B738DR_FL100_dist2 = 0
									end
									calc_wpt_spd_temp = ed_alt_tmp
									break
								elseif ed_alt_tmp < B738DR_fmc_descent_r_alt1 then
									if ed_type_tmp == 0 then
										decel_dist_temp = calc_vnav_pth_dist(ed_alt_tmp, B738DR_fmc_descent_r_alt1, ed_table_tmp)
									else
										--geo
										if ed_vpa_tmp ~= 0 then
											decel_dist_temp = (B738DR_fmc_descent_r_alt1 - ed_alt_tmp) / (6076.11549 * math.tan(math.rad(ed_vpa_tmp)))
										end
									end
									if ed_tl_tmp == 1 then
										decel_dist_temp = decel_dist_temp + tl_wpt_dis
									end
									if last_wpt_idx <= ed_idx_tmp then
										for ii = ed_idx_tmp, last_wpt_idx, -1 do
											if wpt_distance(ii) > decel_dist_temp then
												B738DR_FL100_idx2 = ii
												B738DR_FL100_dist2 = decel_dist_temp
												calc_wpt_spd_temp = ed_alt_tmp
												break
											else
												decel_dist_temp = decel_dist_temp - wpt_distance(ii)
											end
										end
									end
									break
								end
							end
						else
							if ed_alt == B738DR_fmc_descent_r_alt1 then
								B738DR_FL100_idx2 = ed_found
								if ed_tl == 1 then
									B738DR_FL100_dist2 = tl_wpt_dis
								else
									B738DR_FL100_dist2 = 0
								end
								calc_wpt_spd_temp = ed_alt
							elseif ed_alt < B738DR_fmc_descent_r_alt1 then
								if ed_type == 0 then
									decel_dist_temp = calc_vnav_pth_dist(ed_alt, B738DR_fmc_descent_r_alt1, 1)
								else
									--geo
									if ed_vpa ~= 0 then
										decel_dist_temp = (B738DR_fmc_descent_r_alt1 - ed_alt) / (6076.11549 * math.tan(math.rad(ed_vpa)))
									end
								end
								if ed_tl == 1 then
									decel_dist_temp = decel_dist_temp + tl_wpt_dis
								end
								if last_wpt_idx <= ed_found then
									for ii = ed_found , last_wpt_idx, -1 do
										if wpt_distance(ii) > decel_dist_temp then
											B738DR_FL100_idx2 = ii
											B738DR_FL100_dist2 = decel_dist_temp
											calc_wpt_spd_temp = ed_alt
											break
										else
											decel_dist_temp = decel_dist_temp - wpt_distance(ii)
										end
									end
								end
							end
						end
					end
					if B738DR_fms_test == 767 then
						-- ed_fix_tl[ed_fix_num] = 1
						if B738DR_FL100_idx2 > 0 and B738DR_FL100_idx2 < legs_num then
							dump_leg_append("FL100: " .. legs_data[B738DR_FL100_idx2][1] .. " before " .. tostring(B738DR_FL100_dist2) .." / " .. tostring(calc_wpt_spd_temp) .. "\n")
						else
							dump_leg_append("FL100: none\n")
						end
						if B738DR_FL100_idx > 0 and B738DR_FL100_idx < legs_num then
							dump_leg_append("FL100 DECEL: " .. legs_data[B738DR_FL100_idx][1] .. " before " .. tostring(B738DR_FL100_dist) .."\n")
						else
							dump_leg_append("FL100 DECEL: none\n")
						end
						-- B738DR_fms_test = 0
					end
					
					-- if B738DR_FL100_idx ~= 0 and B738DR_FL100_idx2 ~= 0 and B738DR_fmc_descent_r_speed1 < B738DR_fmc_descent_speed and B738DR_fmc_descent_r_speed1 ~= 0 then
						-- B738DR_FL100_dist_dest2 = legs_data[B738DR_FL100_idx2][50] + B738DR_FL100_dist2
						-- B738DR_FL100_dist_dest = B738DR_FL100_dist_dest2 + ((B738DR_fmc_descent_speed - B738DR_fmc_descent_r_speed1) * DES_SPEED_COEF)
					-- else
						-- B738DR_FL100_dist_dest = 0
						-- B738DR_FL100_dist_dest2 = 0
					-- end
					
					if B738DR_FL100_idx ~= 0 and B738DR_FL100_idx2 ~= 0 then
						B738DR_FL100_dist_dest = legs_data[B738DR_FL100_idx][50] + B738DR_FL100_dist
						B738DR_FL100_dist_dest2 = legs_data[B738DR_FL100_idx2][50] + B738DR_FL100_dist2
					else
						B738DR_FL100_dist_dest = 0
						B738DR_FL100_dist_dest2 = 0
					end
					
					if B738DR_fms_test == 767 then
						dump_leg_append("FL100 delta distance: " .. tostring(B738DR_FL100_dist_dest - B738DR_FL100_dist_dest2) .."\n")
						-- B738DR_fms_test = 0
					end
					
					decel_dist_temp = 0
					calc_wpt_spd_temp = 0
					
					-- local act_spd = 145
					-- local act_decel_spd = 0
					
					-- -- find spd restrict for ed_found
					-- if legs_data[ed_found][17] == 1 and B738DR_fms_vref_40 > 0 then
						-- act_spd = B738DR_fms_vref_40 + 20
					-- end
					
					
					
					local act_spd = 180
					local act_decel_spd = 0
					local init_decel_dist = 0
					local x_idx99 = ed_found
					while x_idx99 <= legs_num + 1 do
						if x_idx99 == ed_found then
							init_decel_dist = 0
						else
							init_decel_dist = init_decel_dist + wpt_distance(x_idx99)
						end
						if legs_data[x_idx99][17] == 1 then
							if B738DR_fms_vref_40 > 0 then
								act_spd = B738DR_fms_vref_40 + 20
							else
								act_spd = 145
							end
							if init_decel_dist ~= 0 then
								act_spd = act_spd + (init_decel_dist / DES_SPEED_COEF)
							end
							break
						end
						x_idx99 = x_idx99 + 1
					end
					
					
					
					
					
					if legs_restr_spd_n > 0 then
						for ii = legs_restr_spd_n, 1, -1 do
							if legs_restr_spd[ii][2] == ed_found and legs_restr_spd[ii][3] ~= 0 then
								act_spd = legs_restr_spd[ii][3]
							end
						end
					end
					calc_wpt_spd = act_spd
					act_decel_spd = act_spd
					
					local next_spd = B738DR_fmc_descent_speed
					
					local rest_spd_app = 0
					if B738DR_fms_vref_40 == 0 then
						rest_spd_app = 200
					else
						rest_spd_app = B738DR_fms_vref_40 + 70
					end
					
					local before_iaf_idx = 0
					if iaf_app_idx == 0 then
						before_iaf_idx = first_app_idx
					elseif iaf_app_idx - 1 >= first_app_idx then
						before_iaf_idx = iaf_app_idx - 1
					else
						before_iaf_idx = iaf_app_idx
					end
					
					B738DR_fms_test1 = 571
					
					-- find next spd restrict
					rest_idx_spd = 0
					rest_spd = 0
					if legs_restr_spd_n > 0 then
						for ii = legs_restr_spd_n, 1, -1 do
							if legs_restr_spd[ii][2] < ed_found and legs_restr_spd[ii][2] >= last_wpt_idx then
								
								if legs_restr_spd[ii][3] < next_spd then
									rest_idx_spd = legs_restr_spd[ii][2]
									next_spd = legs_restr_spd[ii][3]
									for jj = ii, 1, -1 do
										if legs_restr_spd[jj][2] < ed_found and legs_restr_spd[jj][2] >= last_wpt_idx then
											if legs_restr_spd[jj][3] < next_spd then
												rest_idx_spd = legs_restr_spd[jj][2]
												next_spd = legs_restr_spd[jj][3]
											end
										end
									end
									rest_spd = next_spd
									break
								end
								
							end
						end
						
						
						if rest_idx_spd < before_iaf_idx and before_iaf_idx < ed_found and before_iaf_idx ~= 0 and before_iaf_idx >= last_wpt_idx then
							if rest_spd_app < next_spd then
								rest_spd = rest_spd_app
								rest_idx_spd = before_iaf_idx
								next_spd = rest_spd
							end
						end
						
						
					else
						if before_iaf_idx < ed_found and before_iaf_idx ~= 0 and before_iaf_idx >= last_wpt_idx then
							if rest_spd_app < next_spd then
								rest_idx_spd = before_iaf_idx
								next_spd = rest_spd
								rest_spd = next_spd
							end
						end
					end
					
					local next_spd2 = B738DR_fmc_descent_speed
					-- if legs_data[ed_found][11] <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 and next_spd2 > B738DR_fmc_descent_r_speed1 then
						-- next_spd2 = B738DR_fmc_descent_r_speed1
					-- end
					-- if next_spd < next_spd2 then
						-- next_spd2 = next_spd
					-- end
					
					B738DR_fms_test1 = 58
					
					local x_spd_mach_alt = find_mach_alt(B738DR_fmc_descent_speed, B738DR_fmc_descent_speed_mach)
					local tmp_cal_alt = 0
					
					legs_data[ed_found][10] = act_spd
					legs_data[ed_found][53] = act_spd
					
					if last_wpt_idx <= ed_found - 1 then
						
						if B738DR_fms_test == 767 then
							dump_leg_append(" ed_found = " .. tostring(ed_found) .. "\n")
							dump_leg_append(" before_iaf_idx = " .. tostring(before_iaf_idx) .. "\n")
							dump_leg_append(" act_spd = " .. tostring(act_spd) .. "\n")
							dump_leg_append(" rest_spd = " .. tostring(rest_spd) .. "\n")
							dump_leg_append(" next_spd = " .. tostring(next_spd) .. "\n")
						end
						
						local decel_fl100_exist = 0
						local decel_type = 1
						local last_wpt_idx2 = last_wpt_idx - 1
						if td_idx == 0 then
							last_wpt_idx2 = offset
						else
							if last_wpt_idx2 < 1 then
								last_wpt_idx2 = 1
							end
						end
						for ii = ed_found - 1, last_wpt_idx2, -1 do
							if B738DR_fms_test == 767 then
								dump_leg_append("WPT: " .. legs_data[ii][1] .. "\n")
							end
							if legs_data[ii][5] == 0 or legs_data[ii][6] == 45 then
								tmp_cal_alt = legs_data[ii][11]
							else
								if legs_data[ii][6] == 43 then
									if legs_data[ii][11] > legs_data[ii][5] then
										tmp_cal_alt = legs_data[ii][11]
									else
										tmp_cal_alt = legs_data[ii][5]
									end
								elseif legs_data[ii][6] == 41 then
									if legs_data[ii][11] >= legs_data[ii][5] and legs_data[ii][11] <= legs_data[ii][41] then
										tmp_cal_alt = legs_data[ii][11]
									elseif legs_data[ii][11] < legs_data[ii][5] then
										tmp_cal_alt = legs_data[ii][5]
									else
										tmp_cal_alt = legs_data[ii][11]
									end
								else
									tmp_cal_alt = legs_data[ii][5]
								end
							end
							if act_spd == B738DR_fmc_descent_speed then
								if legs_data[ii][11] > x_spd_mach_alt then
									legs_data[ii][10] = B738DR_fmc_descent_speed_mach
									legs_data[ii][53] = B738DR_fmc_descent_speed_mach
								else
									legs_data[ii][10] = B738DR_fmc_descent_speed
									legs_data[ii][53] = B738DR_fmc_descent_speed
								end
							else
								leg_dist = wpt_distance(ii+1)
								-- calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
								if B738DR_FL100_idx2 == ii + 1 then
									if B738DR_fms_test == 767 then
										dump_leg_append(" -> at FL100 idx\n")
									end
									-- divide after and before FL100
									calc_wpt_spd = act_spd + (B738DR_FL100_dist2 / DES_SPEED_COEF)
									if B738DR_fmc_descent_r_speed1 ~= 0 and calc_wpt_spd > B738DR_fmc_descent_r_speed1 and B738DR_fmc_descent_r_speed1 < next_spd then
										-- check FL100 restrict
										-- create Decel WPT
										decel_dist_temp = (B738DR_fmc_descent_r_speed1 - act_spd) * DES_SPEED_COEF
										if ii + 1 > offset2 then
											decel_idx = ii + 1
											decel_dist2 = decel_dist_temp
											if decel_type ~= 0 then
												decel_wpt_idx = decel_idx
												decel_wpt_dist = decel_dist2
												decel_wpt_spd = act_decel_spd
											end
										elseif ii + 1 == offset2 then
											if B738DR_fmc_dist > decel_dist_temp then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											end
										end
										if decel_type == 0 then
											decel_fl100_exist = 1
										end
										
										act_spd = B738DR_fmc_descent_r_speed1
										act_decel_spd = act_spd
										calc_wpt_spd = act_spd
										
										if act_spd == rest_spd then
											calc_wpt_spd = act_spd
										else
											calc_wpt_spd = act_spd + ((leg_dist - B738DR_FL100_dist2) / DES_SPEED_COEF)
										end
										
										decel_type = 0
										-- FL100 speed restrict valid
										if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
											-- create Decel FL100
											decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist > decel_dist_temp then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
										
											decel_fl100_exist = 1
											act_spd = rest_spd
											act_decel_spd = act_spd
											decel_type = 1
											calc_wpt_spd = act_spd
										else
											if calc_wpt_spd > B738DR_fmc_descent_speed then
												-- create Decel FL100
												decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
												if ii + 1 > offset2 then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												elseif ii + 1 == offset2 then
													if B738DR_fmc_dist > decel_dist_temp then
														decel_idx = ii + 1
														decel_dist2 = decel_dist_temp
														if decel_type ~= 0 then
															decel_wpt_idx = decel_idx
															decel_wpt_dist = decel_dist2
															decel_wpt_spd = act_decel_spd
														end
													end
												end
												if decel_type == 0 then
													decel_fl100_exist = 1
												end
											
												decel_fl100_exist = 1
												act_spd = B738DR_fmc_descent_speed
												act_decel_spd = act_spd
												decel_type = 1
												calc_wpt_spd = act_spd
											else
												act_spd = calc_wpt_spd
											end
										end
									else
										if act_spd == rest_spd then
											calc_wpt_spd = act_spd
										else
											calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
										end
										if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
											-- create Decel wpt
											decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist > decel_dist_temp then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
											act_spd = rest_spd
											act_decel_spd = act_spd
											decel_type = 1
											calc_wpt_spd = act_spd
										else
											if calc_wpt_spd > B738DR_fmc_descent_speed then
												-- create Decel wpt
												decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
												if ii + 1 > offset2 then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												elseif ii + 1 == offset2 then
													if B738DR_fmc_dist > decel_dist_temp then
														decel_idx = ii + 1
														decel_dist2 = decel_dist_temp
														if decel_type ~= 0 then
															decel_wpt_idx = decel_idx
															decel_wpt_dist = decel_dist2
															decel_wpt_spd = act_decel_spd
														end
													end
												end
												if decel_type == 0 then
													decel_fl100_exist = 1
												end
												act_spd = B738DR_fmc_descent_speed
												act_decel_spd = act_spd
												decel_type = 1
												calc_wpt_spd = act_spd
											else
												act_spd = calc_wpt_spd
											end
										end
									end
									if B738DR_fms_test == 767 then
										dump_leg_append(" act_spd = " .. tostring(act_spd) .. "\n")
										dump_leg_append(" rest_spd = " .. tostring(rest_spd) .. "\n")
										dump_leg_append(" next_spd = " .. tostring(next_spd) .. "\n")
									end
								elseif B738DR_FL100_idx2 < ii + 1 and B738DR_FL100_idx2 ~= 0 then
									if B738DR_fms_test == 767 then
										dump_leg_append(" -> after FL100 idx\n")
									end
									-- after FL100
									calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
									if B738DR_fmc_descent_r_speed1 ~= 0 and calc_wpt_spd > B738DR_fmc_descent_r_speed1 and B738DR_fmc_descent_r_speed1 < next_spd then
										-- check decel FL100
										-- create Decel WPT
										decel_dist_temp = (B738DR_fmc_descent_r_speed1 - act_spd) * DES_SPEED_COEF
										if ii + 1 > offset2 then
											decel_idx = ii + 1
											decel_dist2 = decel_dist_temp
											if decel_type ~= 0 then
												decel_wpt_idx = decel_idx
												decel_wpt_dist = decel_dist2
												decel_wpt_spd = act_decel_spd
											end
										elseif ii + 1 == offset2 then
											if B738DR_fmc_dist > decel_dist_temp then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											end
										end
										if decel_type == 0 then
											decel_fl100_exist = 1
										end
										act_spd = B738DR_fmc_descent_r_speed1
										act_decel_spd = act_spd
										-- calc_wpt_spd = act_spd + ((leg_dist - B738DR_FL100_dist2) / DES_SPEED_COEF)
										decel_type = 0
										calc_wpt_spd = act_spd
									else
										if act_spd == rest_spd then
											calc_wpt_spd = act_spd
										else
											calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
										end
										if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
											-- create Decel wpt
											decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist > decel_dist_temp then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
											act_spd = rest_spd
											act_decel_spd = act_spd
											decel_type = 1
											calc_wpt_spd = act_spd
										else
											if calc_wpt_spd > B738DR_fmc_descent_speed then
												-- create Decel wpt
												decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
												if ii + 1 > offset2 then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												elseif ii + 1 == offset2 then
													if B738DR_fmc_dist > decel_dist_temp then
														decel_idx = ii + 1
														decel_dist2 = decel_dist_temp
														if decel_type ~= 0 then
															decel_wpt_idx = decel_idx
															decel_wpt_dist = decel_dist2
															decel_wpt_spd = act_decel_spd
														end
													end
												end
												if decel_type == 0 then
													decel_fl100_exist = 1
												end
												act_spd = B738DR_fmc_descent_speed
												act_decel_spd = act_spd
												decel_type = 1
												calc_wpt_spd = act_spd
											else
												act_spd = calc_wpt_spd
											end
										end
									end
									if B738DR_fms_test == 767 then
										dump_leg_append(" act_spd = " .. tostring(act_spd) .. "\n")
										dump_leg_append(" rest_spd = " .. tostring(rest_spd) .. "\n")
										dump_leg_append(" next_spd = " .. tostring(next_spd) .. "\n")
									end
								else
									-- before FL100
									if B738DR_fms_test == 767 then
										dump_leg_append(" -> before FL100 idx\n")
									end
									if act_spd == rest_spd then
										calc_wpt_spd = act_spd
									else
										calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
									end
									if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
										-- create Decel wpt
										decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
										if ii + 1 > offset2 then
											decel_idx = ii + 1
											decel_dist2 = decel_dist_temp
											if decel_type ~= 0 then
												decel_wpt_idx = decel_idx
												decel_wpt_dist = decel_dist2
												decel_wpt_spd = act_decel_spd
											end
										elseif ii + 1 == offset2 then
											if B738DR_fmc_dist > decel_dist_temp then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											end
										end
										if decel_type == 0 then
											decel_fl100_exist = 1
										end
										act_spd = rest_spd
										act_decel_spd = act_spd
										decel_type = 1
										calc_wpt_spd = act_spd
									else
										if calc_wpt_spd > B738DR_fmc_descent_speed then
											-- create Decel wpt
											decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx = ii + 1
												decel_dist2 = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx = decel_idx
													decel_wpt_dist = decel_dist2
													decel_wpt_spd = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist > decel_dist_temp then
													decel_idx = ii + 1
													decel_dist2 = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx = decel_idx
														decel_wpt_dist = decel_dist2
														decel_wpt_spd = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
											act_spd = B738DR_fmc_descent_speed
											act_decel_spd = act_spd
											decel_type = 1
											calc_wpt_spd = act_spd
										else
											act_spd = calc_wpt_spd
										end
									end
									if B738DR_fms_test == 767 then
										dump_leg_append(" act_spd = " .. tostring(act_spd) .. "\n")
										dump_leg_append(" rest_spd = " .. tostring(rest_spd) .. "\n")
										dump_leg_append(" next_spd = " .. tostring(next_spd) .. "\n")
									end
								end
								if legs_data[ii][11] > x_spd_mach_alt then
									act_spd = B738DR_fmc_descent_speed
									legs_data[ii][10] = B738DR_fmc_descent_speed_mach
									legs_data[ii][53] = B738DR_fmc_descent_speed_mach
								else
									legs_data[ii][10] = act_spd
									legs_data[ii][53] = act_spd
								end
								
								if rest_idx_spd == ii then
									next_spd = B738DR_fmc_descent_speed
									-- find next spd restrict
									rest_idx_spd = 0
									rest_spd = 0
									if legs_restr_spd_n > 0 then
										for jj = legs_restr_spd_n, 1, -1 do
											if legs_restr_spd[jj][2] < ii and legs_restr_spd[jj][2] >= last_wpt_idx then
												
												if legs_restr_spd[jj][3] < next_spd then
													rest_idx_spd = legs_restr_spd[jj][2]
													next_spd = legs_restr_spd[jj][3]
													for kk = jj, 1, -1 do
														if legs_restr_spd[kk][2] < ed_found and legs_restr_spd[kk][2] >= last_wpt_idx then
															if legs_restr_spd[kk][3] < next_spd then
																rest_idx_spd = legs_restr_spd[kk][2]
																next_spd = legs_restr_spd[kk][3]
															end
														end
													end
													rest_spd = next_spd
													break
												end
											end
										end
										
										if rest_idx_spd < before_iaf_idx and before_iaf_idx < ii and before_iaf_idx ~= 0 and before_iaf_idx >= last_wpt_idx then
											if rest_spd_app < next_spd then
												rest_spd = rest_spd_app
												rest_idx_spd = before_iaf_idx
												next_spd = rest_spd
											end
										end
									else
										if before_iaf_idx < ii and before_iaf_idx ~= 0 and before_iaf_idx >= last_wpt_idx then
											if rest_spd_app < next_spd then
												rest_idx_spd = before_iaf_idx
												next_spd = rest_spd
												rest_spd = next_spd
											end
										end
									end
								end
								
								if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
									calc_wpt_spd = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
								end
								legs_data[ii][10] = calc_wpt_spd
								legs_data[ii][53] = calc_wpt_spd
							
							if B738DR_fms_test == 767 then
								dump_leg_append(" -> " .. tostring(act_spd) .. "," .. tostring(decel_type) .. "\n")
								dump_leg_append(" -> Predict SPD: " .. tostring(legs_data[ii][10]) .. ", ALT: " .. tostring(legs_data[ii][11]) .. "\n")
							end
							
							
							end
						end
						
						if decel_fl100_exist == 0 then
							B738DR_FL100_idx = 0
							B738DR_FL100_idx2 = 0
						else
							if B738DR_FL100_idx < offset2 then
								B738DR_FL100_idx = 0
							elseif B738DR_FL100_idx == offset2 then
								if B738DR_fmc_dist < B738DR_FL100_dist then
									B738DR_FL100_idx = 0
								end
							end
							if B738DR_FL100_idx2 < offset2 then
								B738DR_FL100_idx2 = 0
							elseif B738DR_FL100_idx2 == offset2 then
								if B738DR_fmc_dist < B738DR_FL100_dist2 then
									B738DR_FL100_idx2 = 0
								end
							end
						end
						
					end
					
					if decel_hold_idx ~= 0 then
						if decel_wpt_idx == 0 then
							decel_wpt_idx = decel_hold_idx
							decel_wpt_dist = decel_hold_dist
							decel_wpt_spd = decel_hold_spd
						elseif decel_hold_idx == decel_wpt_idx then
							if decel_hold_dist > decel_wpt_dist then
								decel_wpt_idx = decel_hold_idx
								decel_wpt_dist = decel_hold_dist
								decel_wpt_spd = decel_hold_spd
							end
						elseif decel_hold_idx < decel_wpt_idx then
							decel_wpt_idx = decel_hold_idx
							decel_wpt_dist = decel_hold_dist
							decel_wpt_spd = decel_hold_spd
						end
					end
					
					if B738DR_fms_test == 767 then
						B738DR_fms_test = 0
					end
					
					if decel_idx ~= 0 then
						lat_tmp, lon_tmp = lat_lon_legs(decel_idx)
						if legs_intdir_act ~= 0 and decel_idx == offset2 then
							if legs_intdir_act == 2 then
								-- calc_brg_dist(legs_data[decel_idx][7], legs_data[decel_idx][8], math.rad((legs_intdir_trk + 180) % 360), decel_dist2)
								calc_brg_dist(lat_tmp, lon_tmp, math.rad((legs_intdir_trk + 180) % 360), decel_dist2)
							else
								-- calc_brg_dist(legs_data[decel_idx][7], legs_data[decel_idx][8], math.rad((B738DR_fmc_trk + 180) % 360), decel_dist2)
								calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), decel_dist2)
							end
						else
							-- calc_brg_dist(legs_data[decel_idx][7], legs_data[decel_idx][8], legs_data[decel_idx][2] + math.pi, decel_dist2)
							calc_brg_dist(lat_tmp, lon_tmp, legs_data[decel_idx][2] + math.pi, decel_dist2)
						end
						B738DR_decel_lat = calc_lat
						B738DR_decel_lon = calc_lon
						-- if decel_type == 0 then
							-- decel_wpt_idx = decel_idx
							-- decel_wpt_dist = decel_dist2
						-- end
					end
					-- B738DR_FL100_idx2 = 0
					-- B738DR_FL100_dist2 = 0
					
					if B738DR_FL100_idx ~= 0 then
						if B738DR_FL100_idx > offset2 or (B738DR_FL100_idx == offset2 and B738DR_FL100_dist < B738DR_fmc_dist) then
							if decel_idx == 0 then
								decel_idx = B738DR_FL100_idx
								decel_dist2 = B738DR_FL100_dist
							else
								if decel_idx > B738DR_FL100_idx then
									decel_idx = B738DR_FL100_idx
									decel_dist2 = B738DR_FL100_dist
								elseif decel_idx == B738DR_FL100_idx then
									if decel_dist2 < B738DR_FL100_dist then
										decel_idx = B738DR_FL100_idx
										decel_dist2 = B738DR_FL100_dist
									end
								end
							end
							lat_tmp, lon_tmp = lat_lon_legs(B738DR_FL100_idx)
							if legs_intdir_act ~= 0 and B738DR_FL100_idx == offset2 then
								if legs_intdir_act == 2 then
									-- calc_brg_dist(legs_data[B738DR_FL100_idx][7], legs_data[B738DR_FL100_idx][8], math.rad((legs_intdir_trk + 180) % 360), B738DR_FL100_dist)
									calc_brg_dist(lat_tmp, lon_tmp, math.rad((legs_intdir_trk + 180) % 360), B738DR_FL100_dist)
								else
									-- calc_brg_dist(legs_data[B738DR_FL100_idx][7], legs_data[B738DR_FL100_idx][8], math.rad((B738DR_fmc_trk + 180) % 360), B738DR_FL100_dist)
									calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), B738DR_FL100_dist)
								end
							else
								-- calc_brg_dist(legs_data[B738DR_FL100_idx][7], legs_data[B738DR_FL100_idx][8], legs_data[B738DR_FL100_idx][2] + math.pi, B738DR_FL100_dist)
								calc_brg_dist(lat_tmp, lon_tmp, legs_data[B738DR_FL100_idx][2] + math.pi, B738DR_FL100_dist)
							end
							B738DR_decel_def_lat = calc_lat
							B738DR_decel_def_lon = calc_lon
						end
					end
					
					-------------------------------------------------------------
					
					B738DR_fms_test1 = 59
					
					if rnav_idx_last ~= 0 and ed_found ~= 0 and rnav_idx_last-1 >= ed_found+1 and rnav_idx_last <= legs_num then
						calc_wpt_alt = 0
						for ii = rnav_idx_last-1, ed_found+1, -1 do
							leg_dist = wpt_distance(ii+1)
							calc_wpt_alt = calc_wpt_alt + leg_dist
							legs_data[ii][11] = rnav_alt + ((calc_wpt_alt * (math.tan(math.rad(-rnav_vpa)))) * 6076.11549)	-- (in ft)
							if legs_data[ii][5] ~= 0 then
								if legs_data[ii][6] ~= 43 and legs_data[ii][6] ~= 45 and legs_data[ii][6] ~= 41 then
									legs_data[ii][11] = legs_data[ii][5]
								end
							end
							if B738DR_fms_approach_speed > 0 then
								if leg_dist > 4 then
									legs_data[ii][10] = B738DR_fms_vref_40 + 20
									legs_data[ii][53] = B738DR_fms_vref_40 + 20
								else
									legs_data[ii][10] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr	--5
									legs_data[ii][53] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
								end
							end
						end
					end
					
					B738DR_fms_test1 = 60
					
					if rnav_idx_last ~= 0 and ed_found ~= 0 and rnav_idx_last <= legs_num then
						legs_data[rnav_idx_last][11] = rnav_alt
						if B738DR_fms_approach_speed > 0 then
							if string.sub(legs_data[rnav_idx_last][1], 1, 2) == "RW" then
								legs_data[rnav_idx_last][10] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
								legs_data[rnav_idx_last][53] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
							else
								legs_data[rnav_idx_last][10] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr	--5
								legs_data[rnav_idx_last][53] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
							end
						end
					end
					if B738DR_fms_test == 98 then
						dump_leg_mod()
						B738DR_fms_test = 0
					end
				end
			end
		end
		
		--B738DR_fms_test3 = decel_idx
		
		-- CRUISE
		----------------------------------
		if B738DR_fms_test == 4 then
			dump_leg_mod()
			B738DR_fms_test = 0
		end
		----------------------------------
		
		for n = 1, legs_num do
			legs_data[n][38] = ""
		end
		
		local last_wind = ""
		
		if td_idx > 1 then
		
			calc_wpt_alt = crz_alt_num
			if crz_alt_num < 26000 then
				calc_wpt_spd = B738DR_fmc_cruise_speed
			else
				calc_wpt_spd = B738DR_fmc_cruise_speed_mach
			end
			if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
				if calc_wpt_spd > B738DR_fmc_climb_r_speed1 then
					calc_wpt_spd = B738DR_fmc_climb_r_speed1
				end
			end
			nd_x = 360
			n = td_idx - 1
			local init_alt = simDR_altitude_pilot
			local pom_alt = 0
			local pom_dist = 0
			--if tc_idx ~= 0 and tc_idx <= td_idx then
			if tc_idx ~= 0 and tc_idx < td_idx then
				if tc_idx <= n then
					for ii = tc_idx, n do
						if B738DR_flight_phase == 4 then
							if offset <= ii then
								if offset == ii then
									pom_dist = B738DR_fmc_dist
								else
									pom_dist = wpt_distance(ii)
								end
								if simDR_ground_spd > 0 then
									pom_alt = pom_dist * 1852 / simDR_ground_spd	--time
									pom_alt = pom_alt * 5.08 * 3.28084	--ft
									init_alt = init_alt - pom_alt
								end
								if init_alt < crz_alt_num then
									legs_data[ii][11] = crz_alt_num
									if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
										legs_data[ii][10] = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
										-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 0 and legs_data[ii][10] < 100 then
										if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
											legs_data[ii][10] = ias_to_tas(legs_data[ii][10], legs_data[ii][11])
											legs_data[ii][10] = tas_to_mach(legs_data[ii][10], legs_data[ii][11])
										end
										if ii > 1 then
											if legs_data[ii-1][4] == 0 then
												legs_data[ii-1][10] = legs_data[ii][10]
											end
										end
									else
										legs_data[ii][10] = calc_wpt_spd
									end
								else
									legs_data[ii][11] = init_alt
									if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
										legs_data[ii][10] = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
										-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 0 and legs_data[ii][10] < 100 then
										if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
											legs_data[ii][10] = ias_to_tas(legs_data[ii][10], legs_data[ii][11])
											legs_data[ii][10] = tas_to_mach(legs_data[ii][10], legs_data[ii][11])
										end
										if ii > 1 then
											if legs_data[ii-1][4] == 0 then
												legs_data[ii-1][10] = legs_data[ii][10]
											end
										end
									else
										if init_alt < 26000 then
											legs_data[ii][10] = B738DR_fmc_descent_speed
										else
											legs_data[ii][10] = B738DR_fmc_descent_speed_mach
										end
									end
								end
								legs_data[ii][53] = legs_data[ii][10]
							end
						else
							legs_data[ii][11] = crz_alt_num
							if legs_data[ii][4] ~= 0 and last_sid_idx > 0 and ii > last_sid_idx then
								calc_wpt_spd = legs_data[ii][4]
								legs_data[ii][10] = calc_wpt_spd
							else
								if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
									legs_data[ii][10] = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
									-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 0 and legs_data[ii][10] < 100 then
									if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
										legs_data[ii][10] = ias_to_tas(legs_data[ii][10], legs_data[ii][11])
										legs_data[ii][10] = tas_to_mach(legs_data[ii][10], legs_data[ii][11])
									end
									if ii > 1 then
										if legs_data[ii-1][4] == 0 then
											legs_data[ii-1][10] = legs_data[ii][10]
										end
									end
								else
									legs_data[ii][10] = calc_wpt_spd
								end
								
								legs_data[ii][53] = legs_data[ii][10]
								
								-- find SID speed restrict
								if last_sid_idx > 0 and ii <= last_sid_idx then
									for jj = ii, last_sid_idx do
										if legs_data[jj][4] > 0 then
											legs_data[ii][10] = legs_data[jj][4]
											legs_data[ii][53] = legs_data[ii][10]
											break
										end
									end
								end
							end
						end
						-- if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
							if legs_data[ii][39] == "" then
								-- legs_data[ii][38] = crz_wind_dir .. "`/" .. crz_wind_spd
								if last_wind == "" then
									if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
										legs_data[ii][38] = crz_wind_dir .. "`/" .. crz_wind_spd
									end
								else
									legs_data[ii][38] = last_wind
								end
							else
								last_wind = legs_data[ii][39]
							end
						-- end
					end
				end
			else
				if tc_idx == 0 then
					if offset2 <= n then
						for ii = offset2, n do
							if B738DR_flight_phase == 4 then
								if offset <= ii then
									if offset == ii then
										pom_dist = B738DR_fmc_dist
									else
										pom_dist = wpt_distance(ii)
									end
									if simDR_ground_spd > 0 then
										pom_alt = pom_dist * 1852 / simDR_ground_spd	--time
										pom_alt = pom_alt * 5.08 * 3.28084	--ft
										init_alt = init_alt - pom_alt
									end
									if init_alt < crz_alt_num then
										legs_data[ii][11] = crz_alt_num
										if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
											legs_data[ii][10] = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
											-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 0 and legs_data[ii][10] < 100 then
											if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
												legs_data[ii][10] = ias_to_tas(legs_data[ii][10], legs_data[ii][11])
												legs_data[ii][10] = tas_to_mach(legs_data[ii][10], legs_data[ii][11])
											end
											if ii > 1 then
												if legs_data[ii-1][4] == 0 then
													legs_data[ii-1][10] = legs_data[ii][10]
												end
											end
										else
											legs_data[ii][10] = calc_wpt_spd
										end
									else
										legs_data[ii][11] = init_alt
										if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
											legs_data[ii][10] = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
											-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 0 and legs_data[ii][10] < 100 then
											if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
												legs_data[ii][10] = ias_to_tas(legs_data[ii][10], legs_data[ii][11])
												legs_data[ii][10] = tas_to_mach(legs_data[ii][10], legs_data[ii][11])
											end
											if ii > 1 then
												if legs_data[ii-1][4] == 0 then
													legs_data[ii-1][10] = legs_data[ii][10]
												end
											end
										else
											if init_alt < 26000 then
												legs_data[ii][10] = B738DR_fmc_descent_speed
											else
												legs_data[ii][10] = B738DR_fmc_descent_speed_mach
											end
										end
									end
								end
							else
								
								legs_data[ii][11] = crz_alt_num
								if legs_data[ii][21] == 0 or legs_data[ii][21] == 1 then
									legs_data[ii][10] = hold_speed_calc(legs_data[ii][4], legs_data[ii][11])
									-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 0 and legs_data[ii][10] < 100 then
									if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
										legs_data[ii][10] = ias_to_tas(legs_data[ii][10], legs_data[ii][11])
										legs_data[ii][10] = tas_to_mach(legs_data[ii][10], legs_data[ii][11])
									end
									if ii > 1 then
										if legs_data[ii-1][4] == 0 then
											legs_data[ii-1][10] = legs_data[ii][10]
										end
									end
								else
									legs_data[ii][10] = calc_wpt_spd
								end
							end
							
							legs_data[ii][53] = legs_data[ii][10]
							
							-- if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
								if legs_data[ii][39] == "" then
									-- legs_data[ii][38] = crz_wind_dir .. "`/" .. crz_wind_spd
									if last_wind == "" then
										if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
											legs_data[ii][38] = crz_wind_dir .. "`/" .. crz_wind_spd
										end
									else
										legs_data[ii][38] = last_wind
									end
								else
									last_wind = legs_data[ii][39]
								end
							-- end
						end
					end
				else
					legs_data[n][11] = crz_alt_num
					if legs_data[n][21] == 0 or legs_data[n][21] == 1 then
						legs_data[n][10] = hold_speed_calc(legs_data[n][4], legs_data[n][11])
						-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[n][10] > 0 and legs_data[n][10] < 100 then
						if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data[ii][10] > 100 then
							legs_data[n][10] = ias_to_tas(legs_data[n][10], legs_data[n][11])
							legs_data[n][10] = tas_to_mach(legs_data[n][10], legs_data[n][11])
						end
						if ii > 1 then
							if legs_data[ii-1][4] == 0 then
								legs_data[ii-1][10] = legs_data[ii][10]
							end
						end
					else
						legs_data[n][10] = calc_wpt_spd 
					end
					
					legs_data[n][53] = legs_data[n][10]
					
					-- if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
						if legs_data[n][39] == "" then
							if last_wind == "" then
								if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
									legs_data[n][38] = crz_wind_dir .. "`/" .. crz_wind_spd
								end
							else
								legs_data[n][38] = last_wind
							end
						else
							last_wind = legs_data[n][39]
						end
					-- end
				end
			end
			
			-- descent WIND
			local get_dir = 0
			local get_spd = 0
			-- last_wind = ""
			
			if ed_found >= td_idx and td_idx ~= 0 and ed_found ~= 0 then
				for ii = td_idx, ed_found do
					if legs_data[ii][39] == "" then
						-- if last_wind == "" then
							get_dir, get_spd = get_desc_wind(legs_data[ii][11])
							if get_spd ~= 0 then
								legs_data[ii][38] = string.format("%03d", displ_crs(get_dir)) .. "`/" .. string.format("%3d", get_spd)
							end
						-- else
							-- legs_data[ii][38] = last_wind
						-- end
					-- else
						-- last_wind = legs_data[ii][39]
					end
					if first_alt_restrict == 0 and legs_data[ii][5] > 0 then
						first_alt_restrict = ii
					end
				end
			end
			
		end
		
		if offset > 0 and offset <= legs_num then
			for n = offset, legs_num do
				if legs_data[n][53] == 0 and legs_data[n][10] ~= 0 then
					legs_data[n][53] = legs_data[n][10]
				end
			end
		end

		----------------------------------
		if B738DR_fms_test == 3 then
			dump_leg_mod()
			B738DR_fms_test = 0
		end
		----------------------------------
		
		B738DR_fms_test1 = 61
		
	else
		if legs_num > 1 then
			for n = 1, (legs_num + 1) do
				if string.sub(legs_data[n][1], 1, 2) == "RW" and n == 2 and legs_data[n][19] == 1 then
					legs_data[n][11] = ref_icao_alt
				elseif n > 1 then
					legs_data[n][11] = 0
				end
				legs_data[n][10] = 0
				legs_data[n][14] = 50000
				legs_data[n][38] = ""
				legs_data[n][53] = 0
			end
		end
		tc_idx = 0
		td_idx = 0
		ed_found = 0
		td_fix_idx = 0
		decel_idx = 0
		ed_fix_found = 0
		ed_fix_alt = 0
		ed_alt = 0

	
	end
	
	-- check alt constraint
	local check_alt_constr = false
	if rte12_act == 0 then
		if msg_chk_alt_constr == 0 and td_idx ~= 0 and calc_rte_enable == 0 and legs_delete == 0 and calc_rte_enable2 == 0 and perf_exec == 2 then
			check_alt_constr = true
		end
	elseif rte12_act == 0 then
		if msg_chk_alt_constr == 0 and td_idx ~= 0 and calc_rte_enable == 0 and legs_delete == 0 and calc_rte_enable8 == 0 and perf_exec == 2 then
			check_alt_constr = true
		end
	end
	
	if check_alt_constr == true then
		if offset > td_idx then
			ii = offset
		else
			ii = td_idx
		end
		if last_sid_idx > 0 and ii <= last_sid_idx then
			ii = last_sid_idx + 1
		end
		if ii > legs_num then
			ii = legs_num
		end
		
		local alt_constr_idx = 0
		local alt_constr1 = 0
		local alt_constr2 = 0
		local alt_constr_type = 0
		
		local alt_constr_idx2 = 0
		local prev_alt_contr = crz_alt_num
		local curr_alt_contr = 0
		local curr_alt_contr_type1 = 0
		local curr_alt_contr_type2 = 0
		local curr_alt_contr_ok = 0
		
		if rnav_idx_last <= legs_num then
			for n = ii, legs_num do
				if rnav_idx_last ~= 0 and n >= rnav_idx_last then
					break
				elseif rnav_idx_last == 0 or ed_found == 0 then
					break
				end
				curr_alt_contr_ok = 1
				if legs_data[n][5] ~= 0 then
					curr_alt_contr_type1 = 1
					curr_alt_contr_type2 = 1
					if legs_data[n][6] == 45 then
						-- bellow
						curr_alt_contr_type1 = 0
					elseif legs_data[n][6] == 43 then
						-- above
						curr_alt_contr_type2 = 0
					end
					if curr_alt_contr_type1 ~= 0 then
						if legs_data[n][5] > prev_alt_contr then
							curr_alt_contr_ok = 0
							alt_constr_idx2 = n
						end
						
					end
					if curr_alt_contr_type2 ~= 0 then
						if legs_data[n][6] == 41 then
							prev_alt_contr = legs_data[n][41]
						else
							prev_alt_contr = legs_data[n][5]
						end
					end
				end
				
				if curr_alt_contr_ok == 0 then
					fmc_message_num = fmc_message_num + 1
					fmc_message[fmc_message_num] = ALT_CONSTRAINT .. legs_data[alt_constr_idx2][1]
					fmc_message_warn[fmc_message_num] = 1
					fms_msg_sound = 1
					--alt_constr_idx2 = 0
				elseif alt_constr_idx ~= 0 and legs_data[n][5] ~= 0 then
					if alt_constr_type == 45 then --below
						if legs_data[n][6] == 43 or legs_data[n][6] == 41 then	-- above or between
							if legs_data[n][5] > alt_constr1 then
								fmc_message_num = fmc_message_num + 1
								fmc_message[fmc_message_num] = ALT_CONSTRAINT .. legs_data[alt_constr_idx][1]
								fmc_message_warn[fmc_message_num] = 1
								fms_msg_sound = 1
								alt_constr_idx = 0
							end
						elseif legs_data[n][6] == 45 then	-- below
						else	-- at
							if legs_data[n][5] > alt_constr1 then
								fmc_message_num = fmc_message_num + 1
								fmc_message[fmc_message_num] = ALT_CONSTRAINT .. legs_data[alt_constr_idx][1]
								fmc_message_warn[fmc_message_num] = 1
								fms_msg_sound = 1
								alt_constr_idx = 0
							end
						end
					elseif alt_constr_type == 41 then --between
						if legs_data[n][6] == 43 or legs_data[n][6] == 41 then	-- above or between
							if legs_data[n][5] > alt_constr2 then
								fmc_message_num = fmc_message_num + 1
								fmc_message[fmc_message_num] = ALT_CONSTRAINT .. legs_data[alt_constr_idx][1]
								fmc_message_warn[fmc_message_num] = 1
								fms_msg_sound = 1
								alt_constr_idx = 0
							end
						elseif legs_data[n][6] == 45 then	-- below
						else	-- at
							if legs_data[n][5] > alt_constr2 then
								fmc_message_num = fmc_message_num + 1
								fmc_message[fmc_message_num] = ALT_CONSTRAINT .. legs_data[alt_constr_idx][1]
								fmc_message_warn[fmc_message_num] = 1
								fms_msg_sound = 1
								alt_constr_idx = 0
							end
						end
					end
				end
				if legs_data[n][49] ~= 0 then
					fmc_message_num = fmc_message_num + 1
					fmc_message[fmc_message_num] = STEEP_DESC .. legs_data[n][1]
					fmc_message_warn[fmc_message_num] = 1
					fms_msg_sound = 1
				end
				if legs_data[n][5] ~= 0 then
					alt_constr_idx = n
					alt_constr1 = legs_data[n][5]
					alt_constr2 = legs_data[n][41]
					alt_constr_type = legs_data[n][6]
				end
				
			end
			msg_chk_alt_constr = 1
		end
	end
	
	B738DR_fms_test1 = 62
	
	if legs_num > 1 then
		for n = 1, legs_num do
			B738DR_fms_legs_alt_calc[n-1] = legs_data[n][11]
		end
	end
	
	B738DR_fms_test1 = 63
	
	if legs_num > 1 and calc_rte_enable == 0 then 
		if rte12_act == 0 then
			if legs_delete == 0 and hold_exec == 0 and rte_exec == 0 then
				copy_to_legsdata2(rte12_act)
			end
		elseif rte12_act == 1 then
			if legs_delete == 0 and hold_exec2 == 0 and rte_exec2 == 0 then
				copy_to_legsdata2(rte12_act)
			end
		end
	end
	
	B738DR_fms_test1 = 64
	
	vnav_update = 0
	if fms_recalc == 2 and legs_delete == 0 then
		fms_recalc = 3
	end
	
	end
	
	if vnav_update == 0 and is_timer_scheduled(vnav_timer) == false then
		run_after_time(vnav_timer, 2.5)	-- 5 seconds
	end
	
end

function recalc_vnav(in_fix_num, in_act_idx, in_wpt_rest, in_segment_dist, in_tl_distance)
	
	local segment_dist = in_segment_dist
	local act_idx = in_act_idx
	local wpt_rest = in_wpt_rest
	local fix_num_tmp = in_fix_num
	local segment_delta_alt = 0
	local segment_dist2 = 0
	-- local rf_dist = 0
	-- local leg_dist = 0
	local in_ed_idx = 0
	local iii = 0
	local calc_vpa = 0
	local calc_alt = 0
	
	if fix_num_tmp == 0 then
		in_ed_idx = ed_found
		segment_delta_alt = (wpt_rest - ed_alt) / 6076.11549 -- to NM
		if segment_dist ~= 0 and act_idx < in_ed_idx then
			ed_type = 1	-- descent path
			calc_vpa = math.atan(segment_delta_alt / segment_dist)
			ed_vpa = math.deg(calc_vpa)
			calc_alt = ed_alt
			segment_dist2 = wpt_distance(in_ed_idx) - in_tl_distance
			if in_ed_idx-1 > act_idx then
				for iii = in_ed_idx-1, act_idx+1, -1 do
					legs_data[iii][11] = calc_alt + ((segment_dist2 * (math.tan(calc_vpa))) * 6076.11549) -- ft
					
					if legs_data[iii][5] ~= 0 and iii > last_sid_idx then
						if legs_data[iii][6] == 45 then
							-- below
							if legs_data[iii][11] > legs_data[iii][5] then
								legs_data[iii][11] = legs_data[iii][5]
								segment_delta_alt = (wpt_rest - legs_data[iii][5]) / 6076.11549 -- to NM
								segment_dist = segment_dist - segment_dist2
								if segment_dist2 > 0 then
									
									if fix_num_tmp == 0 then
										recalc_vnav(fix_num_tmp, iii, legs_data[iii][5], segment_dist2, 0)
									else
										if ed_fix_tl[fix_num_tmp] == 0 then
											recalc_vnav(fix_num_tmp, iii, legs_data[iii][5], segment_dist2, 0)
										else
											recalc_vnav(fix_num_tmp, iii, legs_data[iii][5], segment_dist2, in_tl_distance)
										end
									end
									
									-- create new E/D
									segment_dist2 = 0
									ed_fix_num = ed_fix_num + 1
									ed_fix_found2[ed_fix_num] = iii
									
									if segment_dist > 0 then
										calc_vpa = math.atan(segment_delta_alt / segment_dist)
									else
										calc_vpa = 0
									end
									
									ed_fix_vpa2[ed_fix_num] = math.deg(calc_vpa)
									ed_fix_alt2[ed_fix_num] = legs_data[iii][5]
									ed_fix_type[ed_fix_num] = 1	-- idle path
									ed_fix_table[ed_fix_num] = ed_fix_num + 1
									ed_fix_tl[ed_fix_num] = 0
									create_idle_path(iii, legs_data[iii][5], crz_alt_num, ed_fix_num + 1, 0)
									calc_alt = ed_fix_alt2[ed_fix_num]
									-- fix_num_tmp = ed_fix_num
									fix_num_tmp = ed_fix_num
								end
							end
						end
					end
					
					if legs_data[iii][1] ~= "(VECTOR)" then
						segment_dist2 = segment_dist2 + wpt_distance(iii)
					end
				end
			end
		end
	else
		in_ed_idx = ed_fix_found2[fix_num_tmp]
		segment_delta_alt = (wpt_rest - ed_fix_alt2[fix_num_tmp]) / 6076.11549 -- to NM
		if segment_dist ~= 0 and act_idx < in_ed_idx then
			ed_fix_type[fix_num_tmp] = 1	-- descent path
			calc_vpa = math.atan(segment_delta_alt / segment_dist)
			ed_fix_vpa2[fix_num_tmp] = math.deg(calc_vpa)
			calc_alt = ed_fix_alt2[fix_num_tmp]
			segment_dist2 = wpt_distance(in_ed_idx) - in_tl_distance
			if in_ed_idx-1 > act_idx then
				for iii = in_ed_idx-1, act_idx+1, -1 do
					legs_data[iii][11] = calc_alt + ((segment_dist2 * (math.tan(calc_vpa))) * 6076.11549) -- ft
					
					if legs_data[iii][5] ~= 0 and iii > last_sid_idx then
						if legs_data[iii][6] == 45 then
							-- below
							if legs_data[iii][11] > legs_data[iii][5] then
								legs_data[iii][11] = legs_data[iii][5]
								segment_delta_alt = (wpt_rest - legs_data[iii][5]) / 6076.11549 -- to NM
								segment_dist = segment_dist - segment_dist2
								if segment_dist2 > 0 then
									
									if ed_fix_tl[fix_num_tmp] == 0 then
										recalc_vnav(fix_num_tmp, iii, legs_data[iii][5], segment_dist2, 0)
									else
										recalc_vnav(fix_num_tmp, iii, legs_data[iii][5], segment_dist2, in_tl_distance)
									end
									
									-- create new E/D
									segment_dist2 = 0
									ed_fix_num = ed_fix_num + 1
									ed_fix_found2[ed_fix_num] = iii
									
									if segment_dist > 0 then
										calc_vpa = math.atan(segment_delta_alt / segment_dist)
									else
										calc_vpa = 0
									end
									
									ed_fix_vpa2[ed_fix_num] = math.deg(calc_vpa)
									ed_fix_alt2[ed_fix_num] = legs_data[iii][5]
									ed_fix_type[ed_fix_num] = 1	-- idle path
									ed_fix_table[ed_fix_num] = ed_fix_num + 1
									ed_fix_tl[ed_fix_num] = 0
									create_idle_path(iii, legs_data[iii][5], crz_alt_num, ed_fix_num + 1, 0)
									-- fix_num_tmp = ed_fix_num
									calc_alt = ed_fix_alt2[ed_fix_num]
									fix_num_tmp = ed_fix_num
								end
							end
						end
					end
					
					if legs_data[iii][1] ~= "(VECTOR)" then
						segment_dist2 = segment_dist2 + wpt_distance(iii)
					end
				end
			end
		end
	end
end

function recalc_vnav_mod(in_fix_num, in_act_idx, in_wpt_rest, in_segment_dist, in_tl_distance, in_rte12_act)
	
	local segment_dist = in_segment_dist
	local act_idx = in_act_idx
	local wpt_rest = in_wpt_rest
	local fix_num_tmp = in_fix_num
	local segment_delta_alt = 0
	local segment_dist2 = 0
	local in_ed_idx = 0
	local iii = 0
	local calc_vpa = 0
	local calc_alt = 0
	
	
	if fix_num_tmp == 0 then
		in_ed_idx = ed_found_mod
		segment_delta_alt = (wpt_rest - ed_alt_mod) / 6076.11549 -- to NM
		if segment_dist ~= 0 and act_idx < in_ed_idx then
			ed_type_mod = 1	-- descent path
			calc_vpa = math.atan(segment_delta_alt / segment_dist)
			calc_alt = ed_alt_mod
			ed_vpa_mod = math.deg(calc_vpa)
			segment_dist2 = wpt_distance2(in_ed_idx, in_rte12_act) - in_tl_distance
			if in_ed_idx-1 > act_idx then
				for iii = in_ed_idx-1, act_idx+1, -1 do
					legs_data2_buf[iii][11] = calc_alt + ((segment_dist2 * (math.tan(calc_vpa))) * 6076.11549) -- ft
					
					if legs_data2_buf[iii][5] ~= 0 and iii > last_sid_idx_mod_buf then
						if legs_data2_buf[iii][6] == 45 then
							-- below
							if legs_data2_buf[iii][11] > legs_data2_buf[iii][5] then
								legs_data2_buf[iii][11] = legs_data2_buf[iii][5]
								segment_delta_alt = (wpt_rest - legs_data2_buf[iii][5]) / 6076.11549 -- to NM
								segment_dist = segment_dist - segment_dist2
								if segment_dist2 > 0 then
									if fix_num_tmp == 0 then
										recalc_vnav_mod(fix_num_tmp, iii, legs_data2_buf[iii][5], segment_dist2, 0, in_rte12_act)
									else
										if ed_fix_tl_mod[fix_num_tmp] == 0 then
											recalc_vnav_mod(fix_num_tmp, iii, legs_data2_buf[iii][5], segment_dist2, 0, in_rte12_act)
										else
											recalc_vnav_mod(fix_num_tmp, iii, legs_data2_buf[iii][5], segment_dist2, in_tl_distance, in_rte12_act)
										end
									end
									
									-- create new E/D
									segment_dist2 = 0
									ed_fix_num_mod = ed_fix_num_mod + 1
									ed_fix_found2_mod[ed_fix_num_mod] = iii
									
									if segment_dist > 0 then
										calc_vpa = math.atan(segment_delta_alt / segment_dist)
									else
										calc_vpa = 0
									end
									
									ed_fix_vpa2_mod[ed_fix_num_mod] = math.deg(calc_vpa)
									ed_fix_alt2_mod[ed_fix_num_mod] = legs_data2_buf[iii][5]
									ed_fix_type_mod[ed_fix_num_mod] = 1	-- idle path
									ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
									ed_fix_tl_mod[ed_fix_num_mod] = 0
									create_idle_path2(iii, legs_data2_buf[iii][5], crz_alt_num, ed_fix_num_mod + 1, 0, in_rte12_act)
									-- fix_num_tmp = ed_fix_num_mod
									calc_alt = ed_fix_alt2_mod[ed_fix_num_mod]
									fix_num_tmp = ed_fix_num_mod
								end
							end
						end
					end
					
					if legs_data2_buf[iii][1] ~= "(VECTOR)" then
						segment_dist2 = segment_dist2 + wpt_distance2(iii, in_rte12_act)
					end
				end
			end
		end
	else
		in_ed_idx = ed_fix_found2_mod[fix_num_tmp]
		segment_delta_alt = (in_wpt_rest - ed_fix_alt2_mod[fix_num_tmp]) / 6076.11549 -- to NM
		if segment_dist ~= 0 and in_act_idx < in_ed_idx then
			ed_fix_type_mod[fix_num_tmp] = 1	-- descent path
			calc_vpa = math.atan(segment_delta_alt / segment_dist)
			ed_fix_vpa2_mod[fix_num_tmp] = math.deg(calc_vpa)
			calc_alt = ed_fix_alt2_mod[fix_num_tmp]
			segment_dist2 = wpt_distance2(in_ed_idx, in_rte12_act) - in_tl_distance
			if in_ed_idx-1 > act_idx then
				for iii = in_ed_idx-1, act_idx+1, -1 do
					legs_data2_buf[iii][11] = calc_alt + ((segment_dist2 * (math.tan(calc_vpa))) * 6076.11549) -- ft
					
					if legs_data2_buf[iii][5] ~= 0 and iii > last_sid_idx_mod_buf then
						if legs_data2_buf[iii][6] == 45 then
							-- below
							if legs_data2_buf[iii][11] > legs_data2_buf[iii][5] then
								legs_data2_buf[iii][11] = legs_data2_buf[iii][5]
								segment_delta_alt = (wpt_rest - legs_data2_buf[iii][5]) / 6076.11549 -- to NM
								segment_dist = segment_dist - segment_dist2
								if segment_dist2 > 0 then
									if ed_fix_tl_mod[fix_num_tmp] == 0 then
										recalc_vnav_mod(fix_num_tmp, iii, legs_data2_buf[iii][5], segment_dist2, 0, in_rte12_act)
									else
										recalc_vnav_mod(fix_num_tmp, iii, legs_data2_buf[iii][5], segment_dist2, in_tl_distance, in_rte12_act)
									end
									
									-- create new E/D
									segment_dist2 = 0
									ed_fix_num_mod = ed_fix_num_mod + 1
									ed_fix_found2_mod[ed_fix_num_mod] = iii
									
									if segment_dist > 0 then
										calc_vpa = math.atan(segment_delta_alt / segment_dist)
									else
										calc_vpa = 0
									end
									
									ed_fix_vpa2_mod[ed_fix_num_mod] = math.deg(calc_vpa)
									ed_fix_alt2_mod[ed_fix_num_mod] = legs_data2_buf[iii][5]
									ed_fix_type_mod[ed_fix_num_mod] = 1	-- idle path
									ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
									ed_fix_tl_mod[ed_fix_num_mod] = 0
									create_idle_path2(iii, legs_data2_buf[iii][5], crz_alt_num, ed_fix_num_mod + 1, 0, in_rte12_act)
									-- fix_num_tmp = ed_fix_num_mod
									calc_alt = ed_fix_alt2_mod[ed_fix_num_mod]
									fix_num_tmp = ed_fix_num_mod
								end
							end
						end
					end
					
					if legs_data2_buf[iii][1] ~= "(VECTOR)" then
						segment_dist2 = segment_dist2 + wpt_distance2(iii, in_rte12_act)
					end
				end
			end
		end
	end
end

function find_last_alt_rest_low(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	local finded_rest_alt = 0
	
	if rest_from < rest_to then
		for ii = rest_from, rest_to do
			if ii > last_sid_idx then
				if legs_data[ii][6] == 0 or legs_data[ii][6] == 45 or legs_data[ii][6] == 32 then
					-- at or below
					if legs_data[ii][5] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data[ii][5]
					end
				elseif legs_data[ii][6] == 41 then
					-- between (below)
					if legs_data[ii][41] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data[ii][41]
					end
				end
			end
		end
	else
		for ii = rest_from, rest_to, -1 do
			if ii > last_sid_idx then
				if legs_data[ii][6] == 0 or legs_data[ii][6] == 45 or legs_data[ii][6] == 32 then
					-- at or below
					if legs_data[ii][5] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data[ii][5]
					end
				elseif legs_data[ii][6] == 41 then
					-- between (below)
					if legs_data[ii][41] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data[ii][41]
					end
				end
			end
		end
	end
	return finded_rest_idx, finded_rest_alt
	
end


function find_alt_restrict_low(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	local finded_rest_alt = 0
	
	if rest_from >= rest_to then
		for ii = rest_from, rest_to, -1 do
			if ii > last_sid_idx then
				if legs_data[ii][6] == 0 or legs_data[ii][6] == 45 or legs_data[ii][6] == 32 then
					-- at or below
					if legs_data[ii][5] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data[ii][5]
						break
					end
				elseif legs_data[ii][6] == 41 then
					-- between (below)
					if legs_data[ii][41] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data[ii][41]
						break
					end
				end
			end
		end
	end
	return finded_rest_idx, finded_rest_alt
	
end

function find_last_alt_rest_low2(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	local finded_rest_alt = 0
	
	if rest_from < rest_to then
		for ii = rest_from, rest_to do
			if ii > last_sid_idx_mod_buf then
				if legs_data2_buf[ii][6] == 0 or legs_data2_buf[ii][6] == 45 or legs_data2_buf[ii][6] == 32 then
					-- at or below
					if legs_data2_buf[ii][5] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data2_buf[ii][5]
					end
				elseif legs_data2_buf[ii][6] == 41 then
					-- between (below)
					if legs_data2_buf[ii][41] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data2_buf[ii][41]
					end
				end
			end
		end
	else
		for ii = rest_from, rest_to, -1 do
			if ii > last_sid_idx_mod_buf then
				if legs_data2_buf[ii][6] == 0 or legs_data2_buf[ii][6] == 45 or legs_data2_buf[ii][6] == 32 then
					-- at or below
					if legs_data2_buf[ii][5] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data2_buf[ii][5]
					end
				elseif legs_data2_buf[ii][6] == 41 then
					-- between (below)
					if legs_data2_buf[ii][41] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data2_buf[ii][41]
					end
				end
			end
		end
	end
	return finded_rest_idx, finded_rest_alt
	
end


function find_alt_restrict_low2(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	local finded_rest_alt = 0
	
	if rest_from >= rest_to then
		for ii = rest_from, rest_to, -1 do
			if ii > last_sid_idx_mod_buf then
				if legs_data2_buf[ii][6] == 0 or legs_data2_buf[ii][6] == 45 or legs_data2_buf[ii][6] == 32 then
					-- at or below
					if legs_data2_buf[ii][5] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data2_buf[ii][5]
						break
					end
				elseif legs_data2_buf[ii][6] == 41 then
					-- between (below)
					if legs_data2_buf[ii][41] ~= 0 then
						finded_rest_idx = ii
						finded_rest_alt = legs_data2_buf[ii][41]
						break
					end
				end
			end
		end
	end
	return finded_rest_idx, finded_rest_alt
	
end


function B738_vnav_calc_mod()

	local n = 0
	local nn = 0
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local delta_alt = 0
	local dist_10000 = 0
	local rest_idx_spd = 0
	local rest_idx_alt = 0
	local rest_idx_spd_p = 0
	local rest_idx_alt_p = 0
	local dist = 0
	local calc_wpt_alt = 0
	local calc_wpt_spd = 0
	local calc_wpt_alt_distr = 0
	local calc_vvi = 0
	local rest_idx = 0
	local rest_spd = 0
	local rest_alt = 0
	local rest_alt_t = 0
	local rest_alt_idx = 0
	local calc_spd_alt = 0
	local temp_tl = 0
	
	local temp_d_R = 0
	local temp_brg = 0
	local temp_lat = 0
	local temp_lon = 0
	local lat_wpt = 0
	local lon_wpt = 0
	
	local from_spd = 0
	
	local td_spd_rest_loc = 0
	local skip_vpa = 0
	local discon = 0
	
	local recalc_spd = 0
	local recalc_alt = 0
	local first_idx_alt = 1
	
	local climb_calc_enable = 0
	
	
	local ed_fix_dist_mod = 0
	local ed_fix_vpa_mod = 0
	local ignored = 0
	local idx_temp = 0
	local decel_idx_temp = 0
	local last_wpt_idx = 0
	
	local offset2 = 0

	local nd_lat2 = 0
	local nd_lon2= 0
	local nd_lat = 0
	local nd_lon = 0
	local nd_y = 0
	local nd_x = 0
	
	local alt_calc_temp = 0
	local alt_calc_temp2 = 0
	
	local init_alt = 0
	local init_spd = 0
	local init_vvi = 0
	local init_distance = 0
	local first_restrict = 0
	local temp_alt = 0
	local temp_vpa = 0
	local temp_idx = 0
	
	local spd_lim = 0
	local spd_vvi_corr = 0
	
	local pom5 = 0
	local wind_temp = 0
	local wind_temp_old = 0
	
	local td_dist_temp = 0
	local first_app_rest = 0
	local temp_table = 0
	local temp_type = 0
	--local leg_dist = 0
	
	local vnav_upd_enable = 0
	
	local use_calc_rte_enable2 = 0
	local use_offset_mod = 0
	local use_ref_icao = ""
	local use_ref_icao_x = ""
	local use_des_icao_x = ""
	local use_ref_icao_alt = 0
	local use_ref_icao_alt_x = 0
	
	local use_rte12_act = rte12_act
	
	if use_rte12_act < 0 then
		if exec_activate_fpln == 1 then
			use_rte12_act = 0
		elseif exec_activate_fpln2 == 1 then
			use_rte12_act = 1
		end
	end
	
	if use_rte12_act == 0 then
		use_calc_rte_enable2 = calc_rte_enable2
		use_offset_mod = offset_mod
		use_ref_icao = ref_icao
		use_ref_icao_x = ref_icao_x
		use_des_icao_x = des_icao_x
		use_ref_icao_alt = ref_icao_alt
		use_ref_icao_alt_x = ref_icao_alt_x
	elseif use_rte12_act == 1 then
		use_calc_rte_enable2 = calc_rte_enable8
		use_offset_mod = offset_mod2
		use_ref_icao = ref_icao
		use_ref_icao_x = ref_icao7_x
		use_des_icao_x = des_icao7_x
		use_ref_icao_alt = ref_icao_alt7
		use_ref_icao_alt_x = ref_icao_alt7_x
	end
	
	copy_to_legs_data2_buf(use_rte12_act)
	
	if legs_num2_buf > 1 then
		vnav_upd_enable = 1
	end
	
	if legs_delete_old2 ~= legs_delete or legs_delete ~= 0 or crz_exec ~= 0 then
		vnav_update_mod = 1
	elseif legs_num2_buf > 1 and legs_num <= 1 then
		vnav_update_mod = 1
	else
		if ed_found ~= 0 and ed_found_mod == 0 and ed_found <= legs_num2_buf + 1 and legs_num2_buf > 1 then
			ed_found_mod = ed_found
		end
		vnav_update_mod = 0
	end
	
	if vnav_upd_enable == 0 then
		vnav_update_mod = 0
	end
	
	if vnav_update_mod == 1 then
	
	if is_timer_scheduled(vnav_timer_mod) == true then
		stop_timer(vnav_timer_mod)
	end
	
	time_calc_enable2 = 1
	
	local crz_alt_num_mod = crz_alt_num2
	if crz_alt_num_mod == 0 then
		crz_alt_num_mod = crz_alt_num
	end
	
	-- if crz_alt_num > 0 and legs_num2_buf > 1 and use_offset_mod > 0 and perf_exec > 0 and ref_icao ~= "----" and des_icao ~= "****"  and use_calc_rte_enable2 == 0 then
	if crz_alt_num_mod > 0 and legs_num2_buf > 1 and use_offset_mod > 0 and perf_exec > 0 and use_ref_icao_x ~= "----" and use_des_icao_x ~= "****"  and use_calc_rte_enable2 == 0 then
		
		offset2 = use_offset_mod
		if offset2 == 0 then
			offset2 = 1
		end
		if offset2 > legs_num2_buf then
			offset2 = legs_num2_buf
		end
		if legs_data2_buf[offset2][1] == use_ref_icao_x and ground_air == 0 then
			offset2 = 2
		end
		
		--clear calculations data
		for n = offset2, (legs_num2_buf + 1) do
		-- for n = 1, (legs_num2_buf + 1) do
			if string.sub(legs_data2_buf[n][1], 1, 2) == "RW" and n == 2 and legs_data2_buf[n][19] == 1 then
				legs_data2_buf[n][11] = use_ref_icao_alt_x
			elseif n > 1 then
				--if n >= offset2 then
					legs_data2_buf[n][11] = 0
				--end
			end
			-- if (legs_data2_buf[n][17] == 1 or legs_data2_buf[n][17] == 101 or legs_data2_buf[n][17] == 201)
			-- and B738DR_fms_vref_40 > 0 then
				-- legs_data2_buf[n][10] = B738DR_fms_vref_40 + 20
			-- else
				legs_data2_buf[n][10] = 0
			-- end
			legs_data2_buf[n][14] = 50000
			legs_data2_buf[n][38] = ""
		end




		------------------------------------------------
		-- CLIMB PHASE
		climb_calc_enable = 0
		
		if B738DR_flight_phase < 5 or B738DR_flight_phase == 8 then
			if simDR_altitude_pilot < crz_alt_num_mod - 200 then
				climb_calc_enable = 1
			end
		end
		
		if tc_idx_mod > legs_num2_buf + 1 then
			tc_idx_mod = 0
		end
		
		cruise_alt_changed2 = 0
		
		if ed_found_mod ~= 0 then
			if offset2 > ed_found_mod then
				climb_calc_enable = 0
			end
		end
		
		if climb_calc_enable == 1 then
			
			tc_idx_mod = 0
			tc_disco_mod = 0
			
			-- intial speed and alt
			if ground_air == 0 then
				if string.sub(legs_data2_buf[2][1], 1, 2) == "RW" then
					if B738DR_fms_v2_set == 0 then
						calc_wpt_spd = 165
					else
						calc_wpt_spd = B738DR_fms_v2_set + 20
					end
				else
					calc_wpt_spd = 205
				end
				calc_wpt_alt = use_ref_icao_alt_x
			else
				calc_wpt_spd = AP_airspeed
				calc_wpt_alt = AP_altitude
			end
			
			-- find restricts spd and alt
			if legs_data2_buf[offset2][44] >= 50000 then
				rest_idx_spd = find_clb_rest_spd2_mod(offset2)
				if rest_idx_spd == 0 then
					rest_spd = B738DR_fmc_climb_speed
				else
					rest_spd = legs_data2_buf[rest_idx_spd][4]
				end
				rest_idx_alt = find_clb_rest_alt2_mod(offset2)
				if rest_idx_alt == 0 then
					rest_alt = 45000
				else
					if legs_data2_buf[rest_idx_alt][6] == 41 then
						rest_alt = legs_data2_buf[rest_idx_alt][41]
					else
						rest_alt = legs_data2_buf[rest_idx_alt][5]
					end
				end
			elseif offset2 > last_sid_idx_mod_buf or last_sid_idx_mod_buf == 0 then
				rest_spd = 340
				rest_alt = 45000
			else
				rest_idx_spd = find_clb_rest_spd_mod(offset2, last_sid_idx_mod_buf)
				if rest_idx_spd == 0 then
					rest_spd = B738DR_fmc_climb_speed
				else
					rest_spd = legs_data2_buf[rest_idx_spd][4]
				end
				rest_idx_alt = find_clb_rest_alt_mod(offset2, last_sid_idx_mod_buf)
				if rest_idx_alt == 0 then
					rest_alt = 45000
				else
					if legs_data2_buf[rest_idx_alt][6] == 41 then
						rest_alt = legs_data2_buf[rest_idx_alt][41]
					else
						rest_alt = legs_data2_buf[rest_idx_alt][5]
					end
				end
			end
			
			if rest_alt < 45000 and calc_wpt_alt > rest_alt then
				calc_wpt_alt = rest_alt
			end
			
			if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
				calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
			else
				calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed)
			end
			
			local calc_vvi_alt = 0
			local calc_vvi_1 = 0
			local calc_vvi_2 = 0
			
			local clb_vvi_gw = simDR_total_weight
			nn = 0
			
			local disable_tc = 0
			
			for n = offset2, (legs_num2_buf + 1) do
				
				disable_tc = 0
				if n == 2 and string.sub(legs_data2_buf[n][1], 1, 2) == "RW" and legs_data2_buf[n][19] == 1 then
					disable_tc  = 1
				end
				
				-- if B738DR_fms_test3 == n - 1 then
					-- B738DR_fms_test4 = calc_wpt_alt
					-- B738DR_fms_test5 = calc_wpt_spd
				-- end
				
				-- dump_leg_append("Before: " .. legs_data2_buf[n][1] .. " " .. tostring(calc_wpt_alt) .. "->" .. tostring(calc_wpt_spd).. "\n")
				
				if disable_tc == 0 then
					-- calc vvi
					alt_calc_temp = math.max(2000, calc_wpt_alt)
					alt_calc_temp = math.min(41000, alt_calc_temp)
					alt_calc_temp = roundUpToIncrement(alt_calc_temp, 1000)
					
					if alt_calc_temp > 2000 then
						calc_vvi_alt = alt_calc_temp - 1000
						calc_vvi_1 = calc_clb_vvi(calc_vvi_alt, clb_vvi_gw)
						calc_vvi_2 = calc_clb_vvi(alt_calc_temp, clb_vvi_gw)
						calc_vvi = B738_rescale(calc_vvi_alt, calc_vvi_1, alt_calc_temp, calc_vvi_2, calc_wpt_alt)
					else
						calc_vvi = calc_clb_vvi(alt_calc_temp, clb_vvi_gw)
					end
					
					local max_dist = 0
					if n == offset2 then
						calc_spd_alt = calc_wpt_spd * (1 + (calc_wpt_alt / 1000 * 0.02))
						if legs_data2_buf[n][1] == "(VECTOR)" then
							max_dist = 0
						else
							max_dist = B738DR_fmc_dist_mod
						end
					else
						if calc_wpt_spd == 0 then
							calc_spd_alt = ias_to_tas(160, legs_data2_buf[n-1][11])
						elseif calc_wpt_spd < 100 then
							calc_spd_alt = mach_to_tas(calc_wpt_spd, legs_data2_buf[n-1][11])
						else
							calc_spd_alt = ias_to_tas(calc_wpt_spd, legs_data2_buf[n-1][11])
						end
						max_dist = wpt_distance2(n, use_rte12_act)
					end
					
					if n == offset2 and ground_air ~= 0 then
						calc_vvi = math.max(simDR_vvi_fpm_pilot - 100, calc_vvi * 0.8)
					end
					
					local calc_wpt_alt_prev = calc_wpt_alt
					local cur_dist = max_dist
					local new_alt = alt_calc_temp
					local dist_to_1000 = calc_dist(calc_spd_alt, calc_vvi, alt_calc_temp - calc_wpt_alt)
					
					if dist_to_1000 > max_dist then
						calc_wpt_alt = calc_wpt_alt_prev + calc_alt_vvi(calc_spd_alt, calc_vvi, max_dist)
						if new_alt >= crz_alt_num_mod then
							calc_wpt_alt = crz_alt_num_mod
						end
						if rest_alt < 45000 and calc_wpt_alt > rest_alt then
							calc_wpt_alt = rest_alt
						end
						if calc_wpt_alt == crz_alt_num_mod then
							-- T/C
							tc_dist_mod = max_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num_mod - calc_wpt_alt_prev)
							tc_idx_mod = n
						end
						-- dump_leg_append("dist_to_1000 > max_dist: " .. legs_data2_buf[n][1] .. " " .. tostring(calc_wpt_alt) .. "->" .. tostring(max_dist).. "\n")
					elseif new_alt >= crz_alt_num_mod then
						calc_wpt_alt = crz_alt_num_mod
						if rest_alt < 45000 and calc_wpt_alt > rest_alt then
							calc_wpt_alt = rest_alt
						end
						if calc_wpt_alt == crz_alt_num_mod then
							-- T/C
							tc_dist_mod = max_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num_mod - calc_wpt_alt_prev)
							tc_idx_mod = n
						end
					elseif rest_alt < 45000 and new_alt >= rest_alt then
						calc_wpt_alt = rest_alt
					else
						cur_dist = cur_dist - dist_to_1000
						while true do
							dist_to_1000 = calc_dist(calc_spd_alt, calc_vvi, 1000)
							
							if dist_to_1000 > cur_dist then
								calc_wpt_alt = new_alt + calc_alt_vvi(calc_spd_alt, calc_vvi, cur_dist)
								if calc_wpt_alt >= crz_alt_num_mod then
									calc_wpt_alt = crz_alt_num_mod
								end
								if rest_alt < 45000 and calc_wpt_alt > rest_alt then
									calc_wpt_alt = rest_alt
								end
								if calc_wpt_alt == crz_alt_num_mod then
									-- T/C
									tc_dist_mod = cur_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num_mod - new_alt)
									tc_idx_mod = n
								end
								break
							end
							
							new_alt = new_alt + 1000
							
							if new_alt >= crz_alt_num_mod then
								calc_wpt_alt = crz_alt_num_mod
								if rest_alt < 45000 and calc_wpt_alt > rest_alt then
									calc_wpt_alt = rest_alt
								end
								if calc_wpt_alt == crz_alt_num_mod then
									-- T/C
									tc_dist_mod = cur_dist - calc_dist(calc_spd_alt, calc_vvi, crz_alt_num_mod - (new_alt - 1000))
									tc_idx_mod = n
								end
								break
							elseif rest_alt < 45000 and new_alt >= rest_alt then
								calc_wpt_alt = rest_alt
								break
							end
							
							cur_dist = cur_dist - dist_to_1000
							if new_alt > 41000 then
								calc_vvi = calc_clb_vvi(42000, clb_vvi_gw)
							elseif new_alt > 2000 then
								calc_vvi = calc_clb_vvi(new_alt + 1000, clb_vvi_gw)
							else
								calc_vvi = calc_clb_vvi(2000, clb_vvi_gw)
							end
							
							if n == offset2 and ground_air ~= 0 then
								calc_vvi = math.max(simDR_vvi_fpm_pilot - 100, calc_vvi * 0.8)
							end
							
							-- check restricts SPD
							if n > offset2 or ground_air ~= 0 then
								if new_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
									calc_spd_alt = math.min(rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
								elseif new_alt > 26000 and rest_spd == 340 then
									calc_spd_alt = B738DR_fmc_climb_speed_mach
								else
									calc_spd_alt = math.min(rest_spd, B738DR_fmc_climb_speed)
								end
								if calc_spd_alt == 0 then
									calc_spd_alt = ias_to_tas(160, new_alt)
								elseif calc_spd_alt < 100 then
									calc_spd_alt = mach_to_tas(calc_spd_alt, new_alt)
								else
									calc_spd_alt = ias_to_tas(calc_spd_alt, new_alt)
								end
							end
						end
					end
				end
				
				-- check restricts SPD
				if n == offset2 then
					if ground_air == 0 and string.sub(legs_data2_buf[2][1], 1, 2) == "RW" then
						calc_wpt_spd = calc_wpt_spd + (wpt_distance2(n, use_rte12_act) / 0.128)
					else
						local dir_dist = nd_calc_dist2(simDR_latitude, simDR_longitude, legs_data2_buf[n][7], legs_data2_buf[n][8])
						calc_wpt_spd = calc_wpt_spd + (dir_dist / 0.128)
					end
				else
					calc_wpt_spd = calc_wpt_spd + (wpt_distance2(n, use_rte12_act) / 0.128)
				end
				
				if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
					calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
				elseif calc_wpt_alt > 26000 and rest_spd == 340 then
					calc_wpt_spd = B738DR_fmc_climb_speed_mach
				else
					calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed)
				end
				
				if rest_alt == 45000 and legs_data2_buf[n][5] ~= 0 and tc_idx_mod == 0 then
					if legs_data2_buf[n][6] == 45 or legs_data2_buf[n][6] == 0 or legs_data2_buf[n][6] == 32 then
						if calc_wpt_alt > legs_data2_buf[n][5] then
							calc_wpt_alt = legs_data2_buf[n][5]
							if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
								calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
							elseif calc_wpt_alt > 26000 and rest_spd == 340 then
								calc_wpt_spd = B738DR_fmc_climb_speed_mach
							else
								calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed)
							end
							if n > 2 then
								nn = n - 1
								while legs_data2_buf[nn][11] > legs_data2_buf[n][5] do
									legs_data2_buf[nn][11] = legs_data2_buf[n][5]
									if legs_data2_buf[nn][11] <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
										if legs_data2_buf[nn][10] > B738DR_fmc_climb_r_speed1 then
											legs_data2_buf[nn][10] = B738DR_fmc_climb_r_speed1
										end
									elseif legs_data2_buf[nn][11] > 26000 and rest_spd == 340 then
										legs_data2_buf[nn][10] = B738DR_fmc_climb_speed_mach
									else
										if legs_data2_buf[nn][10] > B738DR_fmc_climb_speed then
											legs_data2_buf[nn][10] = B738DR_fmc_climb_speed
										end
									end
									nn = nn - 1
									if nn < 2 then
										break
									end
								end
							end
							-- break
						end
					elseif legs_data2_buf[n][6] == 41 then
						if calc_wpt_alt > legs_data2_buf[n][5] then
							calc_wpt_alt = legs_data2_buf[n][5]
							if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
								calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
							elseif calc_wpt_alt > 26000 and rest_spd == 340 then
								calc_wpt_spd = B738DR_fmc_climb_speed_mach
							else
								calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed)
							end
							if n > 2 then
								nn = n - 1
								while legs_data2_buf[nn][11] > legs_data2_buf[n][5] do
									legs_data2_buf[nn][11] = legs_data2_buf[n][5]
									if legs_data2_buf[nn][11] <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
										if legs_data2_buf[nn][10] > B738DR_fmc_climb_r_speed1 then
											legs_data2_buf[nn][10] = B738DR_fmc_climb_r_speed1
										end
									elseif legs_data2_buf[nn][11] > 26000 and rest_spd == 340 then
										legs_data2_buf[nn][10] = B738DR_fmc_climb_speed_mach
									else
										if legs_data2_buf[nn][10] > B738DR_fmc_climb_speed then
											legs_data2_buf[nn][10] = B738DR_fmc_climb_speed
										end
									end
									nn = nn - 1
									if nn < 2 then
										break
									end
								end
							end
							-- break
						end
					elseif legs_data2_buf[n][6] ~= 43 then 
						if calc_wpt_alt > legs_data2_buf[n][5] then
							calc_wpt_alt = legs_data2_buf[n][5]
							if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
								calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed, B738DR_fmc_climb_r_speed1)
							elseif calc_wpt_alt > 26000 and rest_spd == 340 then
								calc_wpt_spd = B738DR_fmc_climb_speed_mach
							else
								calc_wpt_spd = math.min(calc_wpt_spd, rest_spd, B738DR_fmc_climb_speed)
							end
							if n > 2 then
								nn = n - 1
								while legs_data2_buf[nn][11] > legs_data2_buf[n][5] do
									legs_data2_buf[nn][11] = legs_data2_buf[n][5]
									if legs_data2_buf[nn][11] <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
										if legs_data2_buf[nn][10] > B738DR_fmc_climb_r_speed1 then
											legs_data2_buf[nn][10] = B738DR_fmc_climb_r_speed1
										end
									elseif legs_data2_buf[nn][11] > 26000 and rest_spd == 340 then
										legs_data2_buf[nn][10] = B738DR_fmc_climb_speed_mach
									else
										if legs_data2_buf[nn][10] > B738DR_fmc_climb_speed then
											legs_data2_buf[nn][10] = B738DR_fmc_climb_speed
										end
									end
									nn = nn - 1
									if nn < 2 then
										break
									end
								end
							end
							-- break
						end
					end
				end
				
				legs_data2_buf[n][11] = calc_wpt_alt
				if legs_data2_buf[n][21] == 0 or legs_data2_buf[n][21] == 1 then
					calc_wpt_spd = hold_speed_calc(legs_data2_buf[n][4], legs_data2_buf[n][11])
				end
				legs_data2_buf[n][10] = calc_wpt_spd
				
				-- if B738DR_fms_test3 == n then
					-- B738DR_fms_test6 = calc_wpt_alt
					-- B738DR_fms_test7 = calc_wpt_spd
				-- end
				
				-- dump_leg_append("After: " .. legs_data2_buf[n][1] .. " " .. tostring(calc_wpt_alt) .. "->" .. tostring(calc_wpt_spd).. "\n")
				
				-- if B738DR_fms_test8 == 0 then
					-- B738DR_fms_test8 = tc_idx_mod
				-- end
				
				if tc_idx_mod == 0 then
					if n <= legs_num2_buf then
						-- find next restricts ALT/SPD
						ii = n + 1
						if legs_data2_buf[ii][44] >= 50000 then
							-- find restricts spd and alt
							rest_idx_spd = find_clb_rest_spd2_mod(ii)
							if rest_idx_spd == 0 then
								rest_spd = B738DR_fmc_climb_speed
							else
								rest_spd = legs_data2_buf[rest_idx_spd][4]
							end
							rest_idx_alt = find_clb_rest_alt2_mod(ii)
							if rest_idx_alt == 0 then
								rest_alt = 45000
							else
								if legs_data2_buf[rest_idx_alt][6] == 41 then
									rest_alt = legs_data2_buf[rest_idx_alt][41]
								else
									rest_alt = legs_data2_buf[rest_idx_alt][5]
								end
							end
						elseif ii > legs_num2_buf or ii > last_sid_idx_mod_buf or last_sid_idx_mod_buf == 0 then
							rest_spd = 340
							rest_alt = 45000
						else
							-- find restricts spd and alt
							rest_idx_spd = find_clb_rest_spd_mod(ii, last_sid_idx_mod_buf)
							if rest_idx_spd == 0 then
								rest_spd = B738DR_fmc_climb_speed
							else
								rest_spd = legs_data2_buf[rest_idx_spd][4]
							end
							rest_idx_alt = find_clb_rest_alt_mod(ii, last_sid_idx_mod_buf)
							if rest_idx_alt == 0 then
								rest_alt = 45000
							else
								if legs_data2_buf[rest_idx_alt][6] == 41 then
									rest_alt = legs_data2_buf[rest_idx_alt][41]
								else
									rest_alt = legs_data2_buf[rest_idx_alt][5]
								end
							end
						end
					else
						rest_spd = 340
						rest_alt = 45000
					end
				else
					while wpt_distance2(tc_idx_mod, use_rte12_act) < tc_dist_mod do
						if tc_idx_mod > 2 then
							tc_dist_mod = tc_dist_mod - wpt_distance2(tc_idx_mod, use_rte12_act)
							tc_idx_mod = tc_idx_mod - 1
						else
							break
						end
					end
					break
				end
			end
			-- dump_leg_append("\n")
		end
		
		--B738DR_fms_test3 = tc_idx_mod
		--------------------------------------------------------------------------------------------
		--------------------------------------------------------------------------------------------
		-- Find E/D, T/D and DECEL
		--------------------------------------------------------------------------------------------
		--------------------------------------------------------------------------------------------
		
		
		
		if true then
			
			td_dist99_mod = 0
			
			-- local crz_alt_num_mod = crz_alt_num2
			-- if crz_alt_num_mod == 0 then
				-- crz_alt_num_mod = crz_alt_num
			-- end
			td_idx_mod = 0
			td_idx2_mod = 0
			decel_idx_mod = 0
			
			first_alt_restrict = 0
			-- find E/D
			ed_found_mod = 0
			discon = 0
			-- ed_found_type = 0
			if idx_ed_mod_buf > 0 and idx_ed_mod_buf <= legs_num2_buf + 1 then
				ed_found_mod = idx_ed_mod_buf
				ed_alt_mod = legs_data2_buf[ed_found_mod][5]
				ed_vpa_mod = econ_des_vpa
				
				if rnav_idx_first_mod_buf ~= 0 and rnav_idx_last_mod_buf ~= 0 and idx_ed_mod_buf ~= 0 and rnav_idx_last_mod_buf > rnav_idx_first_mod_buf then
					if legs_data2_buf[idx_ed_mod_buf][5] == 0 or legs_data2_buf[idx_ed_mod_buf][6] == 43 or legs_data2_buf[idx_ed_mod_buf][6] == 2 then
						-- find last rnav idx with alt
						nn = rnav_idx_last_mod_buf
						for n = rnav_idx_first_mod_buf, rnav_idx_last_mod_buf do
							if legs_data2_buf[n][5] ~= 0 and (legs_data2_buf[n][6] == 0 or legs_data2_buf[n][6] == 32) and legs_data2_buf[n][20] < 0 then
								nn = n
							end
						end
						
						--calc distance
						pom5 = 0
						for n = nn, idx_ed_mod_buf + 1, -1 do
							pom5 = pom5 + legs_data2_buf[n][3]
						end
						
						--calc alt
						if legs_data2_buf[nn][20] >= 0 then
							ed_alt_mod = (math.tan(math.rad(3)) * pom5 * 6076.11549) + legs_data2_buf[nn][5]
						else
							ed_alt_mod = (math.tan(math.rad(-legs_data2_buf[nn][20])) * pom5 * 6076.11549) + legs_data2_buf[nn][5]
						end
						
						if legs_data2_buf[idx_ed_mod_buf][5] ~= 0 then
							if ed_alt_mod < legs_data2_buf[idx_ed_mod_buf][5] then
								ed_alt_mod = legs_data2_buf[idx_ed_mod_buf][5]
							end
						end
					end
				end
			end
			
			-- if tc_idx_mod == 0 then
				-- discon = 1
			-- end
			
			local compare_td = 1
			if B738DR_flight_phase == 2 then
				if crz_alt_num_mod == crz_alt_num then
					compare_td = 0
				end
			elseif B738DR_flight_phase > 4 then
				compare_td = 0
			end
			
			-- find T/D
			if discon == 0 then
				td_dist_mod = 0 
				--decel_idx_mod = 0
				decel_dist2_mod = 0
				skip_vpa = 0
				if ed_found_mod == 0 then	-- not found e/d
					ed_found_mod = legs_num2_buf + 1
					ed_alt_mod = math.floor((des_icao_alt + 1000)/100) * 100
					ed_vpa_mod = econ_des_vpa
					if legs_data2_buf[ed_found_mod][1] == nil then
						skip_vpa = 1
					end
				end
				if skip_vpa == 0 then
					
					
					local tl_wpt_created = 0
					local tl_wpt_just_created = 0
					local tl_wpt_disable = 0
					local tl_wpt_alt = 0
					local tl_wpt_dis = 0
					local tl_wpt_dis_tmp = 0
					local tl_td = 0
					local idx_rest_temp = 0
					local alt_rest_temp = 0
					ed_tl_mod = 0
					
					if tc_idx_mod == 0 then
						if last_sid_idx_mod_buf == 0 then
							jj = 2	--offset
						else
							jj = last_sid_idx_mod_buf + 1
						end
					else
						jj = tc_idx_mod --- 1
						if jj < 1 then
							jj = 1
						end
					end
					idx_rest_temp, alt_rest_temp = find_last_alt_rest_low2(ed_found_mod - 1, jj)
					if ed_alt_mod == B738DR_trans_lvl_mod and ed_alt ~= alt_rest_temp then
						tl_wpt_created = 1
						tl_wpt_just_created = 1
						ed_alt_mod = alt_qnh_dn(B738DR_trans_lvl_mod, B738DR_forecast_in_hg_mod, 15 + B738DR_forecast_isa_dev_mod)
						ed_tl_mod = 1
					elseif B738DR_trans_lvl_mod < ed_alt_mod then
						tl_wpt_created = 1
						tl_wpt_just_created = 0
					end
					
					-----------------------------------------------------------
					-- find T/D , econ vnav descent path
					-----------------------------------------------------------
					--create_vpth_table(gw_app_kgs, ed_alt)
					B738DR_fms_test1 = 150
					--create_desc_wind_table()
					vnav_des_table_dist2 = {}
					vnav_des_table_alt2 = {}
					vnav_des_table_spd2 = {}
					vnav_des_table_flap2 = {}
					vnav_des_table_act_spd2 = {}
					vnav_des_table_type2 = {}
					vnav_des_table_num2 = {}
					create_idle_path2(ed_found_mod, ed_alt_mod, crz_alt_num_mod, 1, 0, use_rte12_act)
					B738DR_fms_test1 = 151
					ed_dist_mod = calc_vnav_pth_dist2(ed_alt_mod, crz_alt_num_mod, 1)
					B738DR_fms_test1 = 152
					last_wpt_idx = 0
					ed_type_mod = 0
					
					ed_fix_found2_mod = {}
					ed_fix_alt2_mod = {}
					ed_fix_vpa2_mod = {}
					ed_fix_type_mod = {}
					ed_fix_num_mod = 0
					ed_fix_table_mod = {}
					ed_fix_tl_mod = {}
					
					local idx_last_rest_temp = 0
					local alt_last_rest_temp = 0
					-- local idx_rest_temp = 0
					-- local alt_rest_temp = 0
					local wpt_rest_alt = 0
					local wpt_rest_alt2 = 0
					--local ed_alt_temp = ed_alt
					local ed_dist_temp = 0
					local ed_table_temp = 0
					local skip_wpt_alt = 0
					local enable_cruise_alt = 0
					
					local segment_delta_alt = 0
					local segment_dist = 0
					
					ed_fix_alt_mod = ed_alt_mod
					td_dist_temp = 0
					
					local wpt_toc = 1
					
					idx_last_rest_temp, alt_last_rest_temp = find_last_alt_rest_low2(ed_found_mod - 1, jj)
					enable_cruise_alt = 1
					
					idx_rest_temp, alt_rest_temp = find_alt_restrict_low2(ed_found_mod - 1, jj)
					
					local td_disco_mod = 0
					local iii = 0
					--**********************************
					calc_wpt_alt = ed_alt_mod
					-- calc sum distance
					if legs_data2_buf[ed_found_mod][1] ~= "(VECTOR)" then
						leg_dist = wpt_distance2(ed_found_mod, use_rte12_act)
						td_dist_temp = td_dist_temp + leg_dist
						segment_dist = segment_dist + leg_dist
					end
					if legs_data2_buf[ed_found_mod][17] == 1 and B738DR_fms_vref_40 > 0 then
						legs_data2_buf[ed_found_mod][10] = B738DR_fms_vref_40 + 20
					else
						legs_data2_buf[ed_found_mod][10] = 0
					end
					legs_data2_buf[ed_found_mod][11] = ed_alt_mod
					legs_data2_buf[ed_found_mod][14] = 50000
					
					local last_at_below_idx = 0
					local last_at_below_alt = 0
					local last_at_below_alt_tmp = 0
					for ii = ed_found_mod-1, wpt_toc, -1 do
						if legs_data2_buf[ii][5] ~= 0 and ii > last_sid_idx then
							last_at_below_alt_tmp = 50000
							if legs_data2_buf[ii][6] == 0 or legs_data2_buf[ii][6] == 45 or legs_data2_buf[ii][6] == 32 then
								-- at or below
								last_at_below_alt_tmp = legs_data2_buf[ii][5]
							elseif legs_data2_buf[ii][6] == 41 then
								-- between
								last_at_below_alt_tmp = legs_data2_buf[ii][41]
							end
							
							if B738DR_trans_lvl_mod == last_at_below_alt_tmp then
								last_at_below_alt = last_at_below_alt_tmp
								last_at_below_idx = ii - 1
							elseif B738DR_trans_lvl_mod > last_at_below_alt_tmp then
								last_at_below_alt = last_at_below_alt_tmp
								last_at_below_idx = ii - 1
							end
						end
					end
					
					for ii = ed_found_mod-1, wpt_toc, -1 do
					--**********************************
					-- for ii = ed_found_mod, wpt_toc, -1 do
						iii = ii
							
							legs_data2_buf[ii][49] = 0
							
							-- if ii == ed_found_mod then
								-- calc_wpt_alt = ed_alt_mod
							-- else
								if skip_wpt_alt ~= 0 then
									calc_wpt_alt = ed_fix_alt_mod
								else
									if leg_dist ~= 0 then
										calc_wpt_alt = calc_vnav_pth_alt2(ed_fix_alt_mod, td_dist_temp, ed_fix_num_mod + 1)
									end
								end
							-- end
							
							if B738DR_fms_test == 8 then
								if B738DR_fms_test3 == ii then
									B738DR_fms_test4 = calc_wpt_alt
								end
							end
							
							
							wpt_rest_alt = 0
							wpt_rest_alt2 = 0
							if legs_data2_buf[ii][5] ~= 0 and ii > last_sid_idx_mod_buf then
								if legs_data2_buf[ii][6] == 0 or legs_data2_buf[ii][6] == 45 or legs_data2_buf[ii][6] == 32 then
									-- at or below
									wpt_rest_alt = legs_data2_buf[ii][5]
									if legs_data2_buf[ii][6] ~= 45 then
										-- at
										wpt_rest_alt2 = legs_data2_buf[ii][5]
									end
								elseif legs_data2_buf[ii][6] == 41 then
									-- between
									wpt_rest_alt = legs_data2_buf[ii][41]
									wpt_rest_alt2 = legs_data2_buf[ii][5]
								elseif legs_data2_buf[ii][6] == 43 then
									-- above
									wpt_rest_alt2 = legs_data2_buf[ii][5]
								end
							end
							
							if idx_rest_temp ~= 0 then
								tl_wpt_disable = 0
								-- if wpt_rest_alt ~= 0 then
									-- -- if B738DR_trans_lvl_mod > crz_alt_num_mod and B738DR_trans_lvl_mod >= wpt_rest_alt then
									-- if B738DR_trans_lvl_mod > crz_alt_num_mod or B738DR_trans_lvl_mod >= wpt_rest_alt then
										-- tl_wpt_disable = 1
									-- end
								-- else
									-- if B738DR_trans_lvl_mod > crz_alt_num_mod then
										-- tl_wpt_disable = 1
									-- end
								-- end
								
								if B738DR_trans_lvl_mod >= crz_alt_num_mod then
									tl_wpt_disable = 1
								elseif last_at_below_idx ~= 0 then
									if ii > last_at_below_idx then
										tl_wpt_disable = 1
									end
								end
								
								if calc_wpt_alt >= B738DR_trans_lvl_mod and tl_wpt_created == 0 and tl_wpt_disable == 0 then
									tl_wpt_created = 1
									tl_wpt_just_created = 1
									tl_wpt_dis = calc_vnav_pth_dist2(ed_fix_alt_mod, B738DR_trans_lvl_mod, ed_fix_num_mod + 1)
									tl_wpt_dis_tmp = segment_dist - tl_wpt_dis
									tl_wpt_dis = wpt_distance2(ii+1, use_rte12_act) - tl_wpt_dis_tmp
									
									segment_dist = tl_wpt_dis_tmp
									td_dist_temp = tl_wpt_dis_tmp
									
									tl_wpt_alt = alt_qnh_dn(B738DR_trans_lvl_mod, B738DR_forecast_in_hg_mod, 15 + B738DR_forecast_isa_dev_mod)
									ed_fix_num_mod = ed_fix_num_mod + 1
									ed_fix_found2_mod[ed_fix_num_mod] = ii + 1
									
									ed_fix_alt2_mod[ed_fix_num_mod] = tl_wpt_alt
									ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
									ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
									ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
									ed_fix_tl_mod[ed_fix_num_mod] = 1
									ed_fix_alt_mod = tl_wpt_alt
									create_idle_path2(ii+1, ed_fix_alt_mod, crz_alt_num_mod, ed_fix_num_mod + 1, tl_wpt_dis, use_rte12_act)
									calc_wpt_alt = calc_vnav_pth_alt2(ed_fix_alt_mod, tl_wpt_dis_tmp, ed_fix_num_mod + 1)
								end
								
								-- if B738DR_fms_test == 8 then
									-- if B738DR_fms_test3 == ii then
										-- B738DR_fms_test5 = calc_wpt_alt
									-- end
								-- end
								
								------ ****************** -----------
								if calc_wpt_alt > crz_alt_num_mod and enable_cruise_alt ~= 0 
								and (calc_wpt_alt <= alt_rest_temp or crz_alt_num_mod <= alt_rest_temp)then
									if ii <= legs_num2_buf then
										-- T/D
										if crz_alt_num_mod <= ed_fix_alt_mod then
											calc_wpt_alt = crz_alt_num_mod
											legs_data2_buf[ii][11] = calc_wpt_alt
											td_dist_mod = tl_wpt_dis
											td_idx_mod = ii + 1
											if (ii + 1) <= (legs_num2_buf + 1) then
												td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
											else
												td_dist99_mod  = td_dist_mod 
											end
										else
											calc_wpt_alt = crz_alt_num_mod
											legs_data2_buf[ii][11] = calc_wpt_alt
											ed_dist_temp = calc_vnav_pth_dist2(ed_fix_alt_mod, crz_alt_num_mod, ed_fix_num_mod + 1)
											td_dist_mod = ed_dist_temp - (td_dist_temp - wpt_distance2(ii+1, use_rte12_act))
											
											if td_dist_mod < 0 then
												td_dist_mod = 0
											end
											
											td_idx_mod = ii + 1	-- before idx
											if (ii + 1) <= (legs_num2_buf + 1) then
												td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
											else
												td_dist99_mod  = td_dist_mod 
											end
										end
										if tc_idx_mod ~= 0 then
											-- if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and compare_td == 1 then
											if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
												td_idx_mod = 0
											elseif td_idx_mod < tc_idx_mod then
												td_idx_mod = 0
											end
										end
									end
									break
								elseif calc_wpt_alt == crz_alt_num_mod and enable_cruise_alt ~= 0 and calc_wpt_alt <= alt_rest_temp then
									-- T/D
									if crz_alt_num_mod <= ed_fix_alt_mod then
										calc_wpt_alt = crz_alt_num_mod
										legs_data2_buf[ii][11] = calc_wpt_alt
										td_dist_mod = tl_wpt_dis
										td_idx_mod = ii + 1
										if (ii + 1) <= (legs_num2_buf + 1) then
											td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
										else
											td_dist99_mod  = td_dist_mod 
										end
									else
										legs_data2_buf[ii][11] = calc_wpt_alt
										td_dist_mod = 0
										td_idx_mod = ii
										if (ii + 1) <= (legs_num2_buf + 1) then
											td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
										else
											td_dist99_mod  = td_dist_mod 
										end
										-- if tc_idx_mod ~= 0 then
											-- if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and compare_td == 1 then
												-- td_idx_mod = 0
											-- elseif td_idx_mod < tc_idx_mod then
												-- td_idx_mod = 0
											-- end
										-- end
									end
									if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
										td_idx_mod = 0
									elseif td_idx_mod < tc_idx_mod then
										td_idx_mod = 0
									end
									break
								elseif calc_wpt_alt < crz_alt_num_mod and enable_cruise_alt ~= 0 and crz_alt_num_mod <= wpt_rest_alt2 then
									-- T/D
									-- recalc geometric descent path
									if ed_fix_num_mod == 0 then
										recalc_vnav_mod(ed_fix_num_mod, ii, crz_alt_num_mod, segment_dist, 0, use_rte12_act)
									else
										if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
											recalc_vnav_mod(ed_fix_num_mod, ii, crz_alt_num_mod, segment_dist, 0, use_rte12_act)
										else
											recalc_vnav_mod(ed_fix_num_mod, ii, crz_alt_num_mod, segment_dist, tl_wpt_dis, use_rte12_act)
										end
									end
									td_dist_mod = 0
									td_idx_mod = ii
									if (ii + 1) <= (legs_num2_buf + 1) then
										td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
									else
										td_dist99_mod  = td_dist_mod 
									end
									
									if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
										td_idx_mod = 0
									elseif td_idx_mod < tc_idx_mod then
										td_idx_mod = 0
									end
									
									break
								elseif calc_wpt_alt > alt_rest_temp then
									-- calc_wpt_alt = alt_rest_temp
									-- skip_wpt_alt = 1
									-- ed_fix_alt_mod = alt_rest_temp
									
									if ed_fix_alt_mod == alt_rest_temp then
										if ed_fix_num_mod == 0 then
											ed_type_mod = 1
											ed_vpa_mod = 0
										else
											ed_fix_type_mod[ed_fix_num] = 1
											ed_fix_vpa2_mod[ed_fix_num] = 0
										end
									end
									calc_wpt_alt = alt_rest_temp
									skip_wpt_alt = 1
									ed_fix_alt_mod = alt_rest_temp
								elseif calc_wpt_alt > wpt_rest_alt and wpt_rest_alt ~= 0 then
									if ed_fix_alt_mod == wpt_rest_alt then
										if ed_fix_num_mod == 0 then
											ed_type_mod = 1
											ed_vpa_mod = 0
										else
											ed_fix_type_mod[ed_fix_num] = 1
											ed_fix_vpa2_mod[ed_fix_num] = 0
										end
									end
									
									-- create T/D-alt waypoint
									calc_wpt_alt = wpt_rest_alt
									skip_wpt_alt = 1
									ed_fix_alt_mod = wpt_rest_alt
								elseif calc_wpt_alt < wpt_rest_alt2 and wpt_rest_alt2 ~= 0 then
									-- if tl_wpt_just_created == 0 then
										-- skip to new E/D -> idx_rest_temp
										
										ll = calc_wpt_alt
										calc_wpt_alt = wpt_rest_alt2
										
										-- if ii >= iaf_app_idx_mod and iaf_app_idx_mod ~= 0 then
										if ii >= first_app_idx_mod_buf and first_app_idx_mod_buf ~= 0 then
											-- recalc geometric descent path
											if ed_fix_num_mod == 0 then
												recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
											else
												if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
													recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
												else
													recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis, use_rte12_act)
												end
											end
										else
											if ll < wpt_rest_alt2 - 150 then
												-- steep descent after
												legs_data2_buf[ii][49] = 1
											end
										end
										
										-- create new E/D
										segment_dist = 0
										ed_fix_num_mod = ed_fix_num_mod + 1
										ed_fix_found2_mod[ed_fix_num_mod] = ii
										ed_fix_alt2_mod[ed_fix_num_mod] = wpt_rest_alt2
										ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
										ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
										ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
										ed_fix_tl_mod[ed_fix_num_mod] = 0
										create_idle_path2(ii, wpt_rest_alt2, crz_alt_num_mod, ed_fix_num_mod + 1, 0, use_rte12_act)
										td_dist_temp = 0
									-- else
										-- -- steep descent after
										-- legs_data2_buf[ii][49] = 1
									-- end
									-- tl_wpt_just_created = 0
									
									if ii <= idx_rest_temp then
										skip_wpt_alt = 0
										idx_rest_temp, alt_rest_temp = find_alt_restrict_low2(ii - 1, jj)
										ed_fix_alt_mod = wpt_rest_alt2
									end
								end
								
								-- if B738DR_fms_test == 8 then
									-- if B738DR_fms_test3 == ii then
										-- B738DR_fms_test6 = calc_wpt_alt
									-- end
								-- end
								
								legs_data2_buf[ii][11] = calc_wpt_alt
								if idx_rest_temp == ii then
									
									-- if ed_fix_alt_mod <= wpt_rest_alt then
									if ed_fix_alt_mod <= wpt_rest_alt2 then
										legs_data2_buf[ii][11] = wpt_rest_alt2
										
										if skip_wpt_alt ~= 0 then
											if calc_wpt_alt < wpt_rest_alt2 then
												
												ll = calc_wpt_alt
												
												-- if ii >= iaf_app_idx_mod and iaf_app_idx_mod ~= 0 then
												if ii >= first_app_idx_mod_buf and first_app_idx_mod_buf ~= 0 then
													if ed_fix_num_mod == 0 then
														recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
													else
														if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
														else
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis, use_rte12_act)
														end
													end
												else
													if ll < wpt_rest_alt2 - 150 then
														-- steep descent after
														legs_data2_buf[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num_mod = ed_fix_num_mod + 1
											ed_fix_found2_mod[ed_fix_num_mod] = ii
											ed_fix_alt2_mod[ed_fix_num_mod] = wpt_rest_alt2
											ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
											ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
											ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
											ed_fix_tl_mod[ed_fix_num_mod] = 0
											create_idle_path2(ii, wpt_rest_alt2, crz_alt_num_mod, ed_fix_num_mod + 1, 0, use_rte12_act)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									elseif ed_fix_alt_mod <= wpt_rest_alt then
										legs_data2_buf[ii][11] = wpt_rest_alt
										
										if skip_wpt_alt ~= 0 then
											if calc_wpt_alt < wpt_rest_alt then
												
												ll = calc_wpt_alt
												
												-- if ii >= iaf_app_idx_mod and iaf_app_idx_mod ~= 0 then
												if ii >= first_app_idx_mod_buf and first_app_idx_mod_buf ~= 0 then
													if ed_fix_num_mod == 0 then
														recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt, segment_dist, 0, use_rte12_act)
													else
														if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt, segment_dist, 0, use_rte12_act)
														else
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt, segment_dist, tl_wpt_dis, use_rte12_act)
														end
													end
												else
													if ll < wpt_rest_alt - 150 then
														-- steep descent after
														legs_data2_buf[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num_mod = ed_fix_num_mod + 1
											ed_fix_found2_mod[ed_fix_num_mod] = ii
											ed_fix_alt2_mod[ed_fix_num_mod] = wpt_rest_alt
											ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
											ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
											ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
											ed_fix_tl_mod[ed_fix_num_mod] = 0
											create_idle_path2(ii, wpt_rest_alt, crz_alt_num_mod, ed_fix_num_mod + 1, 0, use_rte12_act)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									end
									skip_wpt_alt = 0
									idx_rest_temp, alt_rest_temp = find_alt_restrict_low2(ii - 1, jj)
								end
							else
								tl_wpt_disable = 0
								if B738DR_trans_lvl_mod >= crz_alt_num_mod then
									tl_wpt_disable = 1
								elseif last_at_below_idx ~= 0 then
									if ii > last_at_below_idx then
										tl_wpt_disable = 1
									end
								end
								
								if calc_wpt_alt >= B738DR_trans_lvl_mod and tl_wpt_created == 0 and tl_wpt_disable == 0 then
									tl_wpt_created = 1
									tl_wpt_just_created = 1
									tl_wpt_dis = calc_vnav_pth_dist2(ed_fix_alt_mod, B738DR_trans_lvl_mod, ed_fix_num_mod + 1)
									tl_wpt_dis_tmp = segment_dist - tl_wpt_dis
									tl_wpt_dis = wpt_distance2(ii+1, use_rte12_act) - tl_wpt_dis_tmp
									
									segment_dist = tl_wpt_dis_tmp
									td_dist_temp = tl_wpt_dis_tmp
									
									tl_wpt_alt = alt_qnh_dn(B738DR_trans_lvl_mod, B738DR_forecast_in_hg_mod, 15 + B738DR_forecast_isa_dev_mod)
									ed_fix_num_mod = ed_fix_num_mod + 1
									ed_fix_found2_mod[ed_fix_num_mod] = ii + 1
									
									ed_fix_alt2_mod[ed_fix_num_mod] = tl_wpt_alt
									ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
									ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
									ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
									ed_fix_tl_mod[ed_fix_num_mod] = 1
									ed_fix_alt_mod = tl_wpt_alt
									create_idle_path2(ii+1, ed_fix_alt_mod, crz_alt_num_mod, ed_fix_num_mod + 1, tl_wpt_dis, use_rte12_act)
									calc_wpt_alt = calc_vnav_pth_alt2(ed_fix_alt_mod, tl_wpt_dis_tmp, ed_fix_num_mod + 1)
								end
								
								
								if B738DR_fms_test == 8 then
									if B738DR_fms_test3 == ii then
										B738DR_fms_test7 = calc_wpt_alt
									end
								end
								
								------ ****************** -----------
								if calc_wpt_alt > crz_alt_num_mod then
									if ii <= legs_num2_buf then
										if legs_data2_buf[ii+1][11] <= crz_alt_num_mod then
											-- T/D
											if crz_alt_num_mod <= ed_fix_alt_mod then
												calc_wpt_alt = crz_alt_num_mod
												legs_data2_buf[ii][11] = calc_wpt_alt
												td_dist_mod = tl_wpt_dis
												td_idx_mod = ii + 1
												if (ii + 1) <= (legs_num2_buf + 1) then
													td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
												else
													td_dist99_mod  = td_dist_mod 
												end
											else
												calc_wpt_alt = crz_alt_num_mod
												legs_data2_buf[ii][11] = calc_wpt_alt
												ed_dist_temp = calc_vnav_pth_dist2(ed_fix_alt_mod, crz_alt_num_mod, ed_fix_num_mod + 1)
												td_dist_mod = ed_dist_temp - (td_dist_temp - wpt_distance2(ii+1, use_rte12_act))
												
												if td_dist_mod < 0 then
													td_dist_mod = 0
												end
												
												td_idx_mod = ii + 1	-- before idx
												if (ii + 1) <= (legs_num2_buf + 1) then
													td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
												else
													td_dist99_mod  = td_dist_mod 
												end
											end
											if tc_idx_mod ~= 0 then
												-- if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and compare_td == 1 then
												if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
													td_idx_mod = 0
												elseif td_idx_mod < tc_idx_mod then
													td_idx_mod = 0
												end
											end
										end
									end
									break
								
								elseif calc_wpt_alt == crz_alt_num_mod then
									-- T/D
									if crz_alt_num_mod <= ed_fix_alt_mod then
										calc_wpt_alt = crz_alt_num_mod
										legs_data2_buf[ii][11] = calc_wpt_alt
										td_dist_mod = tl_wpt_dis
										td_idx_mod = ii + 1
										if (ii + 1) <= (legs_num2_buf + 1) then
											td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
										else
											td_dist99_mod  = td_dist_mod 
										end
									else
										legs_data2_buf[ii][11] = calc_wpt_alt
										td_dist_mod = 0
										td_idx_mod = ii
										if (ii + 1) <= (legs_num2_buf + 1) then
											td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
										else
											td_dist99_mod  = td_dist_mod 
										end
										if tc_idx_mod ~= 0 then
											-- if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and compare_td == 1 then
											if td_idx_mod == tc_idx_mod and td_dist_mod > tc_dist_mod and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) then
												td_idx_mod = 0
											elseif td_idx_mod < tc_idx_mod then
												td_idx_mod = 0
											end
										end
									end
									break
								elseif calc_wpt_alt > wpt_rest_alt and wpt_rest_alt ~= 0 then
									if ed_fix_alt_mod == wpt_rest_alt then
										if ed_fix_num_mod == 0 then
											ed_type_mod = 1
											ed_vpa_mod = 0
										else
											ed_fix_type_mod[ed_fix_num] = 1
											ed_fix_vpa2_mod[ed_fix_num] = 0
										end
									end
									
									-- create T/D-alt waypoint
									calc_wpt_alt = wpt_rest_alt
									skip_wpt_alt = 1
									ed_fix_alt_mod = wpt_rest_alt
								elseif calc_wpt_alt < wpt_rest_alt2 and wpt_rest_alt2 ~= 0 then
									-- if tl_wpt_just_created == 0 then
										-- skip to new E/D -> idx_rest_temp
										
										ll = calc_wpt_alt
										calc_wpt_alt = wpt_rest_alt2
										
										-- if ii >= iaf_app_idx_mod and iaf_app_idx_mod ~= 0 then
										if ii >= first_app_idx_mod_buf and first_app_idx_mod_buf ~= 0 then
											-- recalc geometric descent path
											if ed_fix_num_mod == 0 then
												recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
											else
												if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
													recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
												else
													recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis, use_rte12_act)
												end
											end
										else
											if ll < wpt_rest_alt2 - 150 then
												-- steep descent after
												legs_data2_buf[ii][49] = 1
											end
										end
										
										-- create new E/D
										segment_dist = 0
										ed_fix_num_mod = ed_fix_num_mod + 1
										ed_fix_found2_mod[ed_fix_num_mod] = ii
										ed_fix_alt2_mod[ed_fix_num_mod] = wpt_rest_alt2
										ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
										ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
										ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
										ed_fix_tl_mod[ed_fix_num_mod] = 0
										create_idle_path2(ii, wpt_rest_alt2, crz_alt_num_mod, ed_fix_num_mod + 1, 0, use_rte12_act)
										td_dist_temp = 0
									-- else
										-- -- steep descent after
										-- legs_data2_buf[ii][49] = 1
									-- end
									-- tl_wpt_just_created = 0
									if ii <= idx_rest_temp then
										skip_wpt_alt = 0
										idx_rest_temp, alt_rest_temp = find_alt_restrict_low2(ii - 1, jj)
										ed_fix_alt_mod = wpt_rest_alt2
									end
								end
								
								if B738DR_fms_test == 8 then
									if B738DR_fms_test3 == ii then
										B738DR_fms_test8 = calc_wpt_alt
									end
								end
								
								legs_data2_buf[ii][11] = calc_wpt_alt
								if idx_rest_temp == ii then
									
									-- if ed_fix_alt_mod <= wpt_rest_alt then
									if ed_fix_alt_mod <= wpt_rest_alt2 then
										legs_data2_buf[ii][11] = wpt_rest_alt2
										
										if skip_wpt_alt ~= 0 then
											
											if calc_wpt_alt < wpt_rest_alt2 then
												
												ll = wpt_rest_alt2
												
												-- if ii >= iaf_app_idx_mod and iaf_app_idx_mod ~= 0 then
												if ii >= first_app_idx_mod_buf and first_app_idx_mod_buf ~= 0 then
													if ed_fix_num_mod == 0 then
														recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
													else
														if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, 0, use_rte12_act)
														else
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt2, segment_dist, tl_wpt_dis, use_rte12_act)
														end
													end
												else
													if ll < wpt_rest_alt2 - 150 then
														-- steep descent after
														legs_data2_buf[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num_mod = ed_fix_num_mod + 1
											ed_fix_found2_mod[ed_fix_num_mod] = ii
											ed_fix_alt2_mod[ed_fix_num_mod] = wpt_rest_alt2
											ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
											ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
											ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
											ed_fix_tl_mod[ed_fix_num_mod] = 0
											create_idle_path2(ii, wpt_rest_alt2, crz_alt_num_mod, ed_fix_num_mod + 1, 0, use_rte12_act)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									elseif ed_fix_alt_mod <= wpt_rest_alt then
										legs_data2_buf[ii][11] = wpt_rest_alt
										
										if skip_wpt_alt ~= 0 then
											
											if calc_wpt_alt < wpt_rest_alt then
												
												ll = wpt_rest_alt
												
												-- if ii >= iaf_app_idx_mod and iaf_app_idx_mod ~= 0 then
												if ii >= first_app_idx_mod_buf and first_app_idx_mod_buf ~= 0 then
													if ed_fix_num_mod == 0 then
														recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt, segment_dist, 0, use_rte12_act)
													else
														if ed_fix_tl_mod[ed_fix_num_mod] == 0 then
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt, segment_dist, 0, use_rte12_act)
														else
															recalc_vnav_mod(ed_fix_num_mod, ii, wpt_rest_alt, segment_dist, tl_wpt_dis, use_rte12_act)
														end
													end
												else
													if ll < wpt_rest_alt - 150 then
														-- steep descent after
														legs_data2_buf[ii][49] = 1
													end
												end
											end
											
											-- create new E/D
											segment_dist = 0
											ed_fix_num_mod = ed_fix_num_mod + 1
											ed_fix_found2_mod[ed_fix_num_mod] = ii
											ed_fix_alt2_mod[ed_fix_num_mod] = wpt_rest_alt
											ed_fix_vpa2_mod[ed_fix_num_mod] = econ_des_vpa
											ed_fix_type_mod[ed_fix_num_mod] = 0	-- idle path
											ed_fix_table_mod[ed_fix_num_mod] = ed_fix_num_mod + 1
											ed_fix_tl_mod[ed_fix_num_mod] = 0
											create_idle_path2(ii, wpt_rest_alt, crz_alt_num_mod, ed_fix_num_mod + 1, 0, use_rte12_act)
											td_dist_temp = 0
											-- skip_wpt_alt = 0
										end
									end
									skip_wpt_alt = 0
									-- tl_wpt_just_created = 0
									idx_rest_temp, alt_rest_temp = find_alt_restrict_low2(ii - 1, jj)
								end
								
							end
							
							-- calc sum distance
							-- if skip_wpt_alt == 0 then
								-- if legs_data2_buf[ii][1] ~= "(VECTOR)" then
									-- leg_dist = wpt_distance2(ii, use_rte12_act)
									-- td_dist_temp = td_dist_temp + leg_dist
									-- segment_dist = segment_dist + leg_dist
								-- end
							-- end
							
							if legs_data2_buf[ii][1] ~= "(VECTOR)" then
								leg_dist = wpt_distance2(ii, use_rte12_act)
								if skip_wpt_alt == 0 then
									td_dist_temp = td_dist_temp + leg_dist
									segment_dist = segment_dist + leg_dist
								end
							end
							
						
						if td_idx_mod ~= 0 then
							while wpt_distance2(td_idx_mod, use_rte12_act) < td_dist_mod do
								if td_idx_mod > 2 then
									td_dist_mod = td_dist_mod - wpt_distance2(td_idx_mod, use_rte12_act)
									td_idx_mod = td_idx_mod - 1
									if (ii + 1) <= (legs_num2_buf + 1) then
										td_dist99_mod = td_dist_mod  + wpt_distance2(ii+1, use_rte12_act)
									else
										td_dist99_mod  = td_dist_mod 
									end
								else
									break
								end
							end
							break
						end
					end
					
					
					B738DR_fms_test1 = 153
					-------------------------------
					
					-- order E/D restrict data
					if ed_fix_num_mod > 1 then
						for kk = 1, ed_fix_num_mod-1 do
							for ll = kk+1, ed_fix_num_mod do
								if ed_fix_found2_mod[kk] > ed_fix_found2_mod[ll] or (ed_fix_found2_mod[kk] == ed_fix_found2_mod[ll] and ed_fix_tl_mod[kk] == 0) then
									temp_idx = ed_fix_found2_mod[kk]
									temp_alt = ed_fix_alt2_mod[kk]
									temp_vpa = ed_fix_vpa2_mod[kk]
									temp_type = ed_fix_type_mod[kk]
									temp_table = ed_fix_table_mod[kk]
									temp_tl = ed_fix_tl_mod[kk]
									
									ed_fix_found2_mod[kk] = ed_fix_found2_mod[ll]
									ed_fix_alt2_mod[kk] = ed_fix_alt2_mod[ll]
									ed_fix_vpa2_mod[kk] = ed_fix_vpa2_mod[ll]
									ed_fix_table_mod[kk] = ed_fix_table_mod[ll]
									ed_fix_type_mod[kk] = ed_fix_type_mod[ll]
									ed_fix_tl_mod[kk] = ed_fix_tl_mod[ll]
									
									ed_fix_found2_mod[ll] = temp_idx
									ed_fix_alt2_mod[ll] = temp_alt
									ed_fix_vpa2_mod[ll] = temp_vpa
									ed_fix_type_mod[ll] = temp_type
									ed_fix_table_mod[ll] = temp_table
									ed_fix_tl_mod[ll] = temp_tl
								end
							end
						end
					end
					
					B738DR_fms_test1 = 156
					
					-- revert back waypoint flag
					-- for kk = 1, legs_num2_buf do
						-- nd_x = 0
						-- if legs_data2_buf[kk][17] > 199 then
							-- nd_x = 200
						-- elseif legs_data2_buf[kk][17] > 99 then
							-- nd_x = 100
						-- end
						-- while legs_data2_buf[kk][17] > 99 do
							-- legs_data2_buf[kk][17] = legs_data2_buf[kk][17] - 100
						-- end
						-- if legs_data2_buf[kk][17] > 9 then
							-- legs_data2_buf[kk][17] = legs_data2_buf[kk][17] - 10
						-- end
						-- legs_data2_buf[kk][17] = legs_data2_buf[kk][17] + nd_x
					-- end
					
					B738DR_fms_test1 = 154
					
					----------------------------------------------------------------------------
					-- calculate speed for wpts
					
					decel_idx_mod = 0
					--decel_dist2 = 0
					local decel_dist_temp = 0
					local calc_wpt_spd_temp = 0
					
					local des_speed_res = 0
					local des_altit_res = 0
					
					-- if descent_r_speed1_mod == 0 then
						-- des_speed_res = B738DR_fmc_descent_r_speed1
						-- des_altit_res = B738DR_fmc_descent_r_alt1
					-- else
						des_speed_res = descent_r_speed1_mod
						des_altit_res = descent_r_alt1_mod
					-- end
					
					-- B738DR_fms_test5 = des_speed_res
					-- B738DR_fms_test6 = des_altit_res
					
					decel_idx_mod = 0
					decel_dist2_mod = 0
					decel_wpt_idx_mod = 0
					decel_wpt_dist_mod = 0
					local decel_wpt_spd_mod = 0
					B738DR_FL100_idx2_mod = 0
					B738DR_FL100_dist2_mod = 0
					
					-- local decel_dist_temp = 0
					-- local calc_wpt_spd_temp = 0
					
					if td_idx_mod == 0 then
						last_wpt_idx = offset2
					else
						last_wpt_idx = td_idx_mod
					end
					
					
					
					local ed_alt_tmp = 0
					local ed_idx_tmp = 0
					local ed_tl_tmp = 0
					local ed_vpa_tmp = 0
					local ed_type_tmp = 0
					local ed_table_tmp = 0
					
					if crz_alt_num_mod > des_altit_res then	--and B738DR_FL100_idx ~= 0 then
						if ed_fix_num_mod > 0 then
							for n = 1, ed_fix_num_mod + 1 do
								
								if n == ed_fix_num_mod + 1 then
									ed_alt_tmp = ed_alt_mod
									ed_idx_tmp = ed_found_mod
									ed_tl_tmp = ed_tl_mod
									ed_vpa_tmp = ed_vpa_mod
									ed_type_tmp = ed_type_mod
									ed_table_tmp = 1
								else
									ed_alt_tmp = ed_fix_alt2_mod[n]
									ed_idx_tmp = ed_fix_found2_mod[n]
									ed_tl_tmp = ed_fix_tl_mod[n]
									ed_vpa_tmp = ed_fix_vpa2_mod[n]
									ed_type_tmp = ed_fix_type_mod[n]
									ed_table_tmp = ed_fix_table_mod[n]
								end
								
								if ed_alt_tmp == des_altit_res then
									B738DR_FL100_idx2_mod = ed_idx_tmp
									if ed_tl_tmp == 1 then
										B738DR_FL100_dist2_mod = tl_wpt_dis
									else
										B738DR_FL100_dist2_mod = 0
									end
									break
								elseif ed_alt_tmp < des_altit_res then
									if ed_type_tmp == 0 then
										decel_dist_temp = calc_vnav_pth_dist2(ed_alt_tmp, des_altit_res, ed_table_tmp)
									else
										--geo
										if ed_vpa_tmp ~= 0 then
											decel_dist_temp = (des_altit_res - ed_alt_tmp) / (6076.11549 * math.tan(math.rad(ed_vpa_tmp)))
										end
									end
									if ed_tl_tmp == 1 then
										decel_dist_temp = decel_dist_temp + tl_wpt_dis
									end
									if last_wpt_idx <= ed_idx_tmp then
										for ii = ed_idx_tmp, last_wpt_idx, -1 do
											if wpt_distance2(ii, use_rte12_act) > decel_dist_temp then
												B738DR_FL100_idx2_mod = ii
												B738DR_FL100_dist2_mod = decel_dist_temp
												break
											else
												decel_dist_temp = decel_dist_temp - wpt_distance2(ii, use_rte12_act)
											end
										end
									end
									break
								end
							end
						else
							if ed_alt_mod == des_altit_res then
								B738DR_FL100_idx2_mod = ed_found_mod
								if ed_tl_mod == 1 then
									B738DR_FL100_dist2_mod = tl_wpt_dis
								else
									B738DR_FL100_dist2_mod = 0
								end
							elseif ed_alt_mod < des_altit_res then
								if ed_type_mod == 0 then
									decel_dist_temp = calc_vnav_pth_dist2(ed_alt_mod, des_altit_res, 1)
								else
									--geo
									if ed_vpa_mod ~= 0 then
										decel_dist_temp = (des_altit_res - ed_alt_mod) / (6076.11549 * math.tan(math.rad(ed_vpa_mod)))
									end
								end
								if ed_tl_mod == 1 then
									decel_dist_temp = decel_dist_temp + tl_wpt_dis
								end
								if last_wpt_idx <= ed_found_mod then
									for ii = ed_found_mod , last_wpt_idx, -1 do
										if wpt_distance2(ii, use_rte12_act) > decel_dist_temp then
											B738DR_FL100_idx2_mod = ii
											B738DR_FL100_dist2_mod = decel_dist_temp
											break
										else
											decel_dist_temp = decel_dist_temp - wpt_distance2(ii, use_rte12_act)
										end
									end
								end
							end
						end
					end
					
					decel_dist_temp = 0
					calc_wpt_spd_temp = 0
					
					
					B738DR_fms_test1 = 1540
					
					-- local act_spd = 145
					-- local act_decel_spd = 0
					
					-- -- find spd restrict for ed_found
					-- if legs_data2_buf[ed_found_mod][17] == 1 and B738DR_fms_vref_40 > 0 then
						-- act_spd = B738DR_fms_vref_40 + 20
					-- end
					
					
					local act_spd = 180
					local act_decel_spd = 0
					local init_decel_dist = 0
					local x_idx99 = ed_found_mod
					while x_idx99 <= legs_num2_buf + 1 do
						if x_idx99 == ed_found_mod then
							init_decel_dist = 0
						else
							init_decel_dist = init_decel_dist + wpt_distance2(x_idx99, use_rte12_act)
						end
						if legs_data2_buf[x_idx99][17] == 1 then
							if B738DR_fms_vref_40 > 0 then
								act_spd = B738DR_fms_vref_40 + 20
							else
								act_spd = 145
							end
							if init_decel_dist ~= 0 then
								act_spd = act_spd + (init_decel_dist / DES_SPEED_COEF)
							end
							break
						end
						x_idx99 = x_idx99 + 1
					end
					
					if legs_restr_spd_n_mod > 0 then
						for ii = legs_restr_spd_n_mod, 1, -1 do
							if legs_restr_spd_mod[ii][2] == ed_found_mod and legs_restr_spd_mod[ii][3] ~= 0 then
								act_spd = legs_restr_spd_mod[ii][3]
							end
						end
					end
					calc_wpt_spd = act_spd
					act_decel_spd = act_spd
					
					local next_spd = B738DR_fmc_descent_speed
					
					local rest_spd_app = 0
					if B738DR_fms_vref_40 == 0 then
						rest_spd_app = 200
					else
						rest_spd_app = B738DR_fms_vref_40 + 70
					end
					
					B738DR_fms_test1 = 1541
					
					-- find next spd restrict
					rest_idx_spd = 0
					rest_spd = 0
					if legs_restr_spd_n_mod > 0 then
						for ii = legs_restr_spd_n_mod, 1, -1 do
							if legs_restr_spd_mod[ii][2] < ed_found_mod and legs_restr_spd_mod[ii][2] >= last_wpt_idx then
								if legs_restr_spd_mod[ii][3] < next_spd then
									rest_idx_spd = legs_restr_spd_mod[ii][2]
									next_spd = legs_restr_spd_mod[ii][3]
									for jj = ii, 1, -1 do
										if legs_restr_spd_mod[jj][2] < ed_found_mod and legs_restr_spd_mod[jj][2] >= last_wpt_idx then
											if legs_restr_spd_mod[jj][3] < next_spd then
												rest_idx_spd = legs_restr_spd_mod[jj][2]
												next_spd = legs_restr_spd_mod[jj][3]
											end
										end
									end
									rest_spd = next_spd
									break
								end
							end
						end
						if rest_idx_spd < first_app_idx_mod_buf and first_app_idx_mod_buf < ed_found_mod and first_app_idx_mod_buf ~= 0 then
							if rest_spd_app < next_spd then
								rest_spd = rest_spd_app
								rest_idx_spd = first_app_idx_mod_buf
								next_spd = rest_spd
							end
						end
					else
						if first_app_idx_mod_buf < ed_found_mod and first_app_idx_mod_buf ~= 0 then
							if rest_spd_app < next_spd then
								rest_idx_spd = first_app_idx_mod_buf
								next_spd = rest_spd
								rest_spd = next_spd
							end
						end
					end
					
					B738DR_fms_test1 = 1542
					
					local next_spd2 = B738DR_fmc_descent_speed
					
					local x_spd_mach_alt = find_mach_alt(B738DR_fmc_descent_speed, B738DR_fmc_descent_speed_mach)
					local tmp_cal_alt = 0
					
					legs_data2_buf[ed_found_mod][10] = act_spd
					if last_wpt_idx <= ed_found_mod - 1 then
						
						
						
						
						
						local decel_fl100_exist = 0
						local decel_type = 1
						local last_wpt_idx2 = last_wpt_idx - 1
						if td_idx_mod == 0 then
							last_wpt_idx2 = use_offset_mod
						else
							if last_wpt_idx2 < 1 then
								last_wpt_idx2 = 1
							end
						end
						for ii = ed_found_mod - 1, last_wpt_idx2, -1 do
							if legs_data2_buf[ii][5] == 0 or legs_data2_buf[ii][6] == 45 then
								tmp_cal_alt = legs_data2_buf[ii][11]
							else
								if legs_data2_buf[ii][6] == 43 then
									if legs_data2_buf[ii][11] > legs_data2_buf[ii][5] then
										tmp_cal_alt = legs_data2_buf[ii][11]
									else
										tmp_cal_alt = legs_data2_buf[ii][5]
									end
								elseif legs_data2_buf[ii][6] == 41 then
									if legs_data2_buf[ii][11] >= legs_data2_buf[ii][5] and legs_data2_buf[ii][11] <= legs_data2_buf[ii][41] then
										tmp_cal_alt = legs_data2_buf[ii][11]
									elseif legs_data2_buf[ii][11] < legs_data2_buf[ii][5] then
										tmp_cal_alt = legs_data2_buf[ii][5]
									else
										tmp_cal_alt = legs_data2_buf[ii][11]
									end
								else
									tmp_cal_alt = legs_data2_buf[ii][5]
								end
							end
							if act_spd == B738DR_fmc_descent_speed then
								if legs_data2_buf[ii][11] > x_spd_mach_alt then
									legs_data2_buf[ii][10] = B738DR_fmc_descent_speed_mach
								else
									legs_data2_buf[ii][10] = B738DR_fmc_descent_speed
								end
							else
								leg_dist = wpt_distance2(ii+1, use_rte12_act)
								-- calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
								if B738DR_FL100_idx2_mod == ii + 1 then
									-- divide after and before FL100
									calc_wpt_spd = act_spd + (B738DR_FL100_dist2_mod / DES_SPEED_COEF)
									if des_speed_res ~= 0 and calc_wpt_spd > des_speed_res and des_speed_res < next_spd then
										-- check FL100 restrict
										-- create Decel WPT
										decel_dist_temp = (des_speed_res - act_spd) * DES_SPEED_COEF
										if ii + 1 > offset2 then
											decel_idx_mod = ii + 1
											decel_dist2_mod = decel_dist_temp
											if decel_type ~= 0 then
												decel_wpt_idx_mod = decel_idx_mod
												decel_wpt_dist_mod = decel_dist2_mod
												decel_wpt_spd_mod = act_decel_spd
											end
										elseif ii + 1 == offset2 then
											if B738DR_fmc_dist_mod > decel_dist_temp then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											end
										end
										if decel_type == 0 then
											decel_fl100_exist = 1
										end
										
										act_spd = des_speed_res
										act_decel_spd = act_spd
										calc_wpt_spd = act_spd + ((leg_dist - B738DR_FL100_dist2_mod) / DES_SPEED_COEF)
										decel_type = 0
										-- FL100 speed restrict valid
										if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
											-- create Decel FL100
											decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist_mod > decel_dist_temp then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
										
											decel_fl100_exist = 1
											act_spd = rest_spd
											act_decel_spd = act_spd
											decel_type = 1
										else
											if calc_wpt_spd > B738DR_fmc_descent_speed then
												-- create Decel FL100
												decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
												if ii + 1 > offset2 then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												elseif ii + 1 == offset2 then
													if B738DR_fmc_dist_mod > decel_dist_temp then
														decel_idx_mod = ii + 1
														decel_dist2_mod = decel_dist_temp
														if decel_type ~= 0 then
															decel_wpt_idx_mod = decel_idx_mod
															decel_wpt_dist_mod = decel_dist2_mod
															decel_wpt_spd_mod = act_decel_spd
														end
													end
												end
												if decel_type == 0 then
													decel_fl100_exist = 1
												end
											
												decel_fl100_exist = 1
												act_spd = B738DR_fmc_descent_speed
												act_decel_spd = act_spd
												decel_type = 1
											else
												act_spd = calc_wpt_spd
											end
										end
									else
										calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
										if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
											-- create Decel wpt
											decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist_mod > decel_dist_temp then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
											act_spd = rest_spd
											act_decel_spd = act_spd
											decel_type = 1
										else
											if calc_wpt_spd > B738DR_fmc_descent_speed then
												-- create Decel wpt
												decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
												if ii + 1 > offset2 then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												elseif ii + 1 == offset2 then
													if B738DR_fmc_dist_mod > decel_dist_temp then
														decel_idx_mod = ii + 1
														decel_dist2_mod = decel_dist_temp
														if decel_type ~= 0 then
															decel_wpt_idx_mod = decel_idx_mod
															decel_wpt_dist_mod = decel_dist2_mod
															decel_wpt_spd_mod = act_decel_spd
														end
													end
												end
												if decel_type == 0 then
													decel_fl100_exist = 1
												end
												act_spd = B738DR_fmc_descent_speed
												act_decel_spd = act_spd
												decel_type = 1
											else
												act_spd = calc_wpt_spd
											end
										end
									end
									
								elseif B738DR_FL100_idx2_mod < ii + 1 and B738DR_FL100_idx2_mod ~= 0 then
									-- after FL100
									calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
									if des_speed_res ~= 0 and calc_wpt_spd > des_speed_res and des_speed_res < next_spd then
										-- check decel FL100
										-- create Decel WPT
										decel_dist_temp = (des_speed_res - act_spd) * DES_SPEED_COEF
										if ii + 1 > offset2 then
											decel_idx_mod = ii + 1
											decel_dist2_mod = decel_dist_temp
											if decel_type ~= 0 then
												decel_wpt_idx_mod = decel_idx_mod
												decel_wpt_dist_mod = decel_dist2_mod
												decel_wpt_spd_mod = act_decel_spd
											end
										elseif ii + 1 == offset2 then
											if B738DR_fmc_dist_mod > decel_dist_temp then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											end
										end
										if decel_type == 0 then
											decel_fl100_exist = 1
										end
										act_spd = des_speed_res
										act_decel_spd = act_spd
										decel_type = 0
									else
										if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
											-- create Decel wpt
											decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist_mod > decel_dist_temp then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
											act_spd = rest_spd
											act_decel_spd = act_spd
											decel_type = 1
										else
											if calc_wpt_spd > B738DR_fmc_descent_speed then
												-- create Decel wpt
												decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
												if ii + 1 > offset2 then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												elseif ii + 1 == offset2 then
													if B738DR_fmc_dist_mod > decel_dist_temp then
														decel_idx_mod = ii + 1
														decel_dist2_mod = decel_dist_temp
														if decel_type ~= 0 then
															decel_wpt_idx_mod = decel_idx_mod
															decel_wpt_dist_mod = decel_dist2_mod
															decel_wpt_spd_mod = act_decel_spd
														end
													end
												end
												if decel_type == 0 then
													decel_fl100_exist = 1
												end
												act_spd = B738DR_fmc_descent_speed
												act_decel_spd = act_spd
												decel_type = 1
											else
												act_spd = calc_wpt_spd
											end
										end
									end
								else
									-- before FL100
									calc_wpt_spd = act_spd + (leg_dist / DES_SPEED_COEF)
									if rest_spd ~= 0 and calc_wpt_spd > rest_spd then
										-- create Decel wpt
										decel_dist_temp = (rest_spd - act_spd) * DES_SPEED_COEF
										if ii + 1 > offset2 then
											decel_idx_mod = ii + 1
											decel_dist2_mod = decel_dist_temp
											if decel_type ~= 0 then
												decel_wpt_idx_mod = decel_idx_mod
												decel_wpt_dist_mod = decel_dist2_mod
												decel_wpt_spd_mod = act_decel_spd
											end
										elseif ii + 1 == offset2 then
											if B738DR_fmc_dist_mod > decel_dist_temp then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											end
										end
										if decel_type == 0 then
											decel_fl100_exist = 1
										end
										act_spd = rest_spd
										act_decel_spd = act_spd
										decel_type = 1
									else
										if calc_wpt_spd > B738DR_fmc_descent_speed then
											-- create Decel wpt
											decel_dist_temp = (B738DR_fmc_descent_speed - act_spd) * DES_SPEED_COEF
											if ii + 1 > offset2 then
												decel_idx_mod = ii + 1
												decel_dist2_mod = decel_dist_temp
												if decel_type ~= 0 then
													decel_wpt_idx_mod = decel_idx_mod
													decel_wpt_dist_mod = decel_dist2_mod
													decel_wpt_spd_mod = act_decel_spd
												end
											elseif ii + 1 == offset2 then
												if B738DR_fmc_dist_mod > decel_dist_temp then
													decel_idx_mod = ii + 1
													decel_dist2_mod = decel_dist_temp
													if decel_type ~= 0 then
														decel_wpt_idx_mod = decel_idx_mod
														decel_wpt_dist_mod = decel_dist2_mod
														decel_wpt_spd_mod = act_decel_spd
													end
												end
											end
											if decel_type == 0 then
												decel_fl100_exist = 1
											end
											act_spd = B738DR_fmc_descent_speed
											act_decel_spd = act_spd
											decel_type = 1
										else
											act_spd = calc_wpt_spd
										end
									end
								end
								if legs_data2_buf[ii][11] > x_spd_mach_alt then
									act_spd = B738DR_fmc_descent_speed
									legs_data2_buf[ii][10] = B738DR_fmc_descent_speed_mach
								else
									legs_data2_buf[ii][10] = act_spd
								end
								
								if rest_idx_spd == ii then
									next_spd = B738DR_fmc_descent_speed
									-- find next spd restrict
									rest_idx_spd = 0
									rest_spd = 0
									if legs_restr_spd_n_mod > 0 then
										for jj = legs_restr_spd_n_mod, 1, -1 do
											if legs_restr_spd_mod[jj][2] < ii and legs_restr_spd_mod[jj][2] >= last_wpt_idx then
												
												if legs_restr_spd_mod[jj][3] < next_spd then
													rest_idx_spd = legs_restr_spd_mod[jj][2]
													next_spd = legs_restr_spd_mod[jj][3]
													for kk = jj, 1, -1 do
														if legs_restr_spd_mod[kk][2] < ed_found_mod and legs_restr_spd_mod[kk][2] >= last_wpt_idx then
															if legs_restr_spd_mod[kk][3] < next_spd then
																rest_idx_spd = legs_restr_spd_mod[kk][2]
																next_spd = legs_restr_spd_mod[kk][3]
															end
														end
													end
													rest_spd = next_spd
													break
												end
											end
										end
										if rest_idx_spd < first_app_idx_mod_buf and first_app_idx_mod_buf < ii and first_app_idx_mod_buf ~= 0 then
											if rest_spd_app < next_spd then
												rest_spd = rest_spd_app
												rest_idx_spd = first_app_idx_mod_buf
												next_spd = rest_spd
											end
										end
									else
										if first_app_idx_mod_buf < ii and first_app_idx_mod_buf ~= 0 then
											if rest_spd_app < next_spd then
												rest_idx_spd = first_app_idx_mod_buf
												next_spd = rest_spd
												rest_spd = next_spd
											end
										end
									end
								end
								if legs_data2_buf[ii][21] == 0 or legs_data2_buf[ii][21] == 1 then
									legs_data2_buf[ii][10] = hold_speed_calc(legs_data2_buf[ii][4], legs_data2_buf[ii][11])
								end
							end
						end
						
						if decel_fl100_exist == 0 then
							B738DR_FL100_idx_mod = 0
							B738DR_FL100_idx2_mod = 0
						else
							if B738DR_FL100_idx_mod < offset2 then
								B738DR_FL100_idx_mod = 0
							elseif B738DR_FL100_idx_mod == offset2 then
								if B738DR_fmc_dist_mod < B738DR_FL100_dist_mod then
									B738DR_FL100_idx_mod = 0
								end
							end
							if B738DR_FL100_idx2_mod < offset2 then
								B738DR_FL100_idx2_mod = 0
							elseif B738DR_FL100_idx2_mod == offset2 then
								if B738DR_fmc_dist_mod < B738DR_FL100_dist2_mod then
									B738DR_FL100_idx2_mod = 0
								end
							end
						end
						
						
						
						
						
						B738DR_fms_test1 = 1543
						
					end
					
					
					
					local lat_tmp = 0
					local lon_tmp = 0
					if decel_idx_mod ~= 0 then
						-- lat_tmp, lon_tmp = lat_lon_legs2(decel_idx_mod)
						lat_tmp = legs_data2_buf[decel_idx_mod][7]
						lon_tmp = legs_data2_buf[decel_idx_mod][8]
						if legs_intdir_act ~= 0 and decel_idx_mod == offset2 then
							if legs_intdir_act == 2 then
								-- calc_brg_dist(legs_data2_buf[decel_idx_mod][7], legs_data2_buf[decel_idx_mod][8], math.rad((legs_intdir_trk + 180) % 360), decel_dist2_mod)
								calc_brg_dist(lat_tmp, lon_tmp, math.rad((legs_intdir_trk + 180) % 360), decel_dist2_mod)
							else
								-- calc_brg_dist(legs_data2_buf[decel_idx_mod][7], legs_data2_buf[decel_idx_mod][8], math.rad((B738DR_fmc_trk + 180) % 360), decel_dist2_mod)
								calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), decel_dist2_mod)
							end
						else
							-- calc_brg_dist(legs_data2_buf[decel_idx_mod][7], legs_data2_buf[decel_idx_mod][8], legs_data2_buf[decel_idx_mod][2] + math.pi, decel_dist2_mod)
							calc_brg_dist(lat_tmp, lon_tmp, legs_data2_buf[decel_idx_mod][2] + math.pi, decel_dist2_mod)
						end
						B738DR_decel_mod_lat = calc_lat
						B738DR_decel_mod_lon = calc_lon
					end
					
					B738DR_fms_test1 = 1544
					
					if B738DR_FL100_idx_mod ~= 0 then
						if B738DR_FL100_idx_mod > offset2 or (B738DR_FL100_idx_mod == offset2 and B738DR_FL100_dist_mod < B738DR_fmc_dist_mod) then
							if decel_idx_mod == 0 then
								decel_idx_mod = B738DR_FL100_idx_mod
								decel_dist2_mod = B738DR_FL100_dist_mod
							else
								if decel_idx_mod > B738DR_FL100_idx_mod then
									decel_idx_mod = B738DR_FL100_idx_mod
									decel_dist2_mod = B738DR_FL100_dist_mod
								elseif decel_idx_mod == B738DR_FL100_idx_mod then
									if decel_dist2_mod < B738DR_FL100_dist_mod then
										decel_idx_mod = B738DR_FL100_idx_mod
										decel_dist2_mod = B738DR_FL100_dist_mod
									end
								end
							end
							-- lat_tmp, lon_tmp = lat_lon_legs2(B738DR_FL100_idx_mod)
							lat_tmp = legs_data2_buf[B738DR_FL100_idx_mod][7]
							lon_tmp = legs_data2_buf[B738DR_FL100_idx_mod][8]
							if legs_intdir_act ~= 0 and B738DR_FL100_idx_mod == offset2 then
								if legs_intdir_act == 2 then
									-- calc_brg_dist(legs_data2_buf[B738DR_FL100_idx][7], legs_data2_buf[B738DR_FL100_idx][8], math.rad((legs_intdir_trk + 180) % 360), B738DR_FL100_dist)
									calc_brg_dist(lat_tmp, lon_tmp, math.rad((legs_intdir_trk + 180) % 360), B738DR_FL100_dist_mod)
								else
									-- calc_brg_dist(legs_data2_buf[B738DR_FL100_idx][7], legs_data2_buf[B738DR_FL100_idx][8], math.rad((B738DR_fmc_trk + 180) % 360), B738DR_FL100_dist)
									calc_brg_dist(lat_tmp, lon_tmp, math.rad((B738DR_fmc_trk + 180) % 360), B738DR_FL100_dist_mod)
								end
							else
								-- calc_brg_dist(legs_data2_buf[B738DR_FL100_idx][7], legs_data2_buf[B738DR_FL100_idx][8], legs_data2_buf[B738DR_FL100_idx][2] + math.pi, B738DR_FL100_dist)
								calc_brg_dist(lat_tmp, lon_tmp, legs_data2_buf[B738DR_FL100_idx_mod][2] + math.pi, B738DR_FL100_dist_mod)
							end
							B738DR_decel_def_mod_lat = calc_lat
							B738DR_decel_def_mod_lon = calc_lon
						end
					end
					
					
					
					
					-------------------------------------------------------------
					B738DR_fms_test1 = 155
					----------------------------------------------------------------------------
					
					
					if rnav_idx_last_mod_buf ~= 0 and ed_found_mod ~= 0 and rnav_idx_last_mod_buf-1 >= ed_found_mod+1 and rnav_idx_last_mod_buf <= legs_num2_buf then
						calc_wpt_alt = 0
						for ii = rnav_idx_last_mod_buf-1, ed_found_mod+1, -1 do
							leg_dist = wpt_distance2(ii+1, use_rte12_act)
							calc_wpt_alt = calc_wpt_alt + leg_dist
							legs_data2_buf[ii][11] = rnav_alt_mod_buf + ((calc_wpt_alt * (math.tan(math.rad(-rnav_vpa_mod_buf)))) * 6076.11549)	-- (in ft)
							if legs_data2_buf[ii][5] ~= 0 then
								if legs_data2_buf[ii][6] ~= 43 and legs_data2_buf[ii][6] ~= 45 and legs_data2_buf[ii][6] ~= 41 then
									legs_data2_buf[ii][11] = legs_data2_buf[ii][5]
								end
							end
							if B738DR_fms_approach_speed > 0 then
								if leg_dist > 4 then
									legs_data2_buf[ii][10] = B738DR_fms_vref_40 + 20
								else
									legs_data2_buf[ii][10] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr	--5
								end
							end
						end
					end
					if rnav_idx_last_mod_buf ~= 0 and ed_found_mod ~= 0 and rnav_idx_last_mod_buf <= legs_num2_buf then
						legs_data2_buf[rnav_idx_last_mod_buf][11] = rnav_alt_mod_buf
						if B738DR_fms_approach_speed > 0 then
							if string.sub(legs_data2_buf[rnav_idx_last_mod_buf][1], 1, 2) == "RW" then
								legs_data2_buf[rnav_idx_last_mod_buf][10] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
							else
								legs_data2_buf[rnav_idx_last_mod_buf][10] = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr	--5
							end
						end
					end
				end
			end
		end
		--B738DR_fms_test3 = ed_fix_num_mod
		--B738DR_fms_test4 = ed_found_mod
		--B738DR_fms_test3 = td_idx_mod
		---------------------------------------------------------------------------------------------------
		
		-- CRUISE
		for n = 1, legs_num2_buf do
			legs_data2_buf[n][38] = ""
		end
		
		local last_wind = ""
		
		if td_idx_mod > 1 then
		
			-- local crz_alt_num_mod = crz_alt_num2
			-- if crz_alt_num_mod == 0 then
				-- crz_alt_num_mod = crz_alt_num
			-- end
			
			calc_wpt_alt = crz_alt_num_mod
			if crz_alt_num_mod < 26000 then
				calc_wpt_spd = B738DR_fmc_cruise_speed
			else
				calc_wpt_spd = B738DR_fmc_cruise_speed_mach
			end
			if calc_wpt_alt <= B738DR_fmc_climb_r_alt1 and B738DR_fmc_climb_r_speed1 ~= 0 then
				if calc_wpt_spd > B738DR_fmc_climb_r_speed1 then
					calc_wpt_spd = B738DR_fmc_climb_r_speed1
				end
			end
			nd_x = 360
			n = td_idx_mod - 1
			if tc_idx_mod ~= 0 and tc_idx_mod <= td_idx_mod then
				if tc_idx_mod <= n then
					for ii = tc_idx_mod, n do
						legs_data2_buf[ii][10] = calc_wpt_spd
						-- find SID speed restrict
						if last_sid_idx_mod_buf > 0 and ii <= last_sid_idx_mod_buf then
							for jj = ii, last_sid_idx_mod_buf do
								if legs_data2_buf[jj][4] > 0 then
									legs_data2_buf[ii][10] = legs_data2_buf[jj][4]
									break
								end
							end
						end
						legs_data2_buf[ii][11] = crz_alt_num_mod
						if legs_data2_buf[ii][21] == 0 or legs_data2_buf[ii][21] == 1 then
							legs_data2_buf[ii][10] = hold_speed_calc(legs_data2_buf[ii][4], legs_data2_buf[ii][11])
							-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data2_buf[ii][10] > 0 and legs_data2_buf[ii][10] < 100 then
							if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data2_buf[ii][10] > 100 then
								legs_data2_buf[ii][10] = ias_to_tas(legs_data2_buf[ii][10], legs_data2_buf[ii][11])
								legs_data2_buf[ii][10] = tas_to_mach(legs_data2_buf[ii][10], legs_data2_buf[ii][11])
							end
							if ii > 1 then
								if legs_data2_buf[ii-1][4] == 0 then
									legs_data2_buf[ii-1][10] = legs_data2_buf[ii][10]
								end
							end
						end
						-- if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
							if legs_data2_buf[ii][39] == "" then
								if last_wind == "" then
									if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
										legs_data2_buf[ii][38] = crz_wind_dir .. "`/" .. crz_wind_spd
									end
								else
									legs_data2_buf[ii][38] = last_wind
								end
							else
								last_wind = legs_data2_buf[ii][39]
							end
						-- end
					end
				end
			else
				if tc_idx_mod == 0 then
					if offset2 <= n then
						for ii = offset2, n do
							-- cruise phase
							legs_data2_buf[ii][11] = crz_alt_num_mod
							if legs_data2_buf[ii][21] == 0 or legs_data2_buf[ii][21] == 1 then
								legs_data2_buf[ii][10] = hold_speed_calc(legs_data2_buf[ii][4], legs_data2_buf[ii][11])
								-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data2_buf[ii][10] > 0 and legs_data2_buf[ii][10] < 100 then
								if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data2_buf[ii][10] > 100 then
									legs_data2_buf[ii][10] = ias_to_tas(legs_data2_buf[ii][10], legs_data2_buf[ii][11])
									legs_data2_buf[ii][10] = tas_to_mach(legs_data2_buf[ii][10], legs_data2_buf[ii][11])
								end
								if ii > 1 then
									if legs_data2_buf[ii-1][4] == 0 then
										legs_data2_buf[ii-1][10] = legs_data2_buf[ii][10]
									end
								end
							else
								legs_data2_buf[ii][10] = calc_wpt_spd
							end
							if legs_data2_buf[ii][39] == "" then
								if last_wind == "" then
									if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
										legs_data2_buf[ii][38] = crz_wind_dir .. "`/" .. crz_wind_spd
									end
								else
									legs_data2_buf[ii][38] = last_wind
								end
							else
								last_wind = legs_data2_buf[ii][39]
							end
						end
					end
				else
					legs_data2_buf[n][11] = crz_alt_num_mod
					if legs_data2_buf[n][21] == 0 or legs_data2_buf[n][21] == 1 then
						legs_data2_buf[n][10] = hold_speed_calc(legs_data2_buf[n][4], legs_data2_buf[n][11])
						-- if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data2_buf[n][10] > 0 and legs_data2_buf[n][10] < 100 then
						if B738DR_fmc_cruise_speed_mach ~= 0 and legs_data2_buf[ii][10] > 100 then
							legs_data2_buf[n][10] = ias_to_tas(legs_data2_buf[n][10], legs_data2_buf[n][11])
							legs_data2_buf[n][10] = tas_to_mach(legs_data2_buf[n][10], legs_data2_buf[n][11])
						end
						if ii > 1 then
							if legs_data2_buf[ii-1][4] == 0 then
								legs_data2_buf[ii-1][10] = legs_data2_buf[ii][10]
							end
						end
					else
						legs_data2_buf[n][10] = calc_wpt_spd 
					end
					if legs_data2_buf[n][39] == "" then
						if last_wind == "" then
							if crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
								legs_data2_buf[n][38] = crz_wind_dir .. "`/" .. crz_wind_spd
							end
						else
							legs_data2_buf[n][38] = last_wind
						end
					else
						last_wind = legs_data2_buf[n][39]
					end
				end
			end
			
			-- descent WIND
			local get_dir = 0
			local get_spd = 0
			-- last_wind = ""
			
			if ed_found_mod >= td_idx_mod and td_idx_mod ~= 0 and ed_found_mod ~= 0 then
				for ii = td_idx_mod, ed_found_mod do
					if legs_data2_buf[ii][39] == "" then
						-- if last_wind == "" then
							get_dir, get_spd = get_desc_wind(legs_data2_buf[ii][11])
							if get_spd ~= 0 then
								legs_data2_buf[ii][38] = string.format("%03d", displ_crs(get_dir)) .. "`/" .. string.format("%3d", get_spd)
							end
					end
				end
			end
			
		end
		
	else
		if legs_num2_buf > 1 then
			for n = 1, (legs_num2_buf + 1) do
				if string.sub(legs_data2_buf[n][1], 1, 2) == "RW" and n == 2 and legs_data2_buf[n][19] == 1 then
					legs_data2_buf[n][11] = use_ref_icao_alt
				elseif n > 1 then
					legs_data2_buf[n][11] = 0
				end
				legs_data2_buf[n][10] = 0
				legs_data2_buf[n][14] = 50000
				legs_data2_buf[n][38] = ""
			end
		end
		tc_idx_mod = 0
		td_idx_mod = 0
		ed_found_mod = 0
		td_fix_idx_mod = 0
		decel_idx_mod = 0
		decel_dist2_mod = 0
		ed_fix_found_mod = 0
		ed_fix_alt_mod = 0
		ed_alt_mod = 0
	
	end
	
	copy_from_legs_data2_buf(use_rte12_act)
	
	-- B738DR_fms_test1 = 2002
	-- copy_from_legs_data2_buf(rte12_act)
	
	vnav_update_mod = 0
	-- if fms_recalc == 2 then
		-- fms_recalc = 3
	-- end
	
	-- B738DR_fms_test1 = 2003
	end
	-- B738DR_fms_test1 = 2004
	if is_timer_scheduled(vnav_timer_mod) == false then
		run_after_time(vnav_timer_mod, 2.5)	-- 1 seconds
	end
	-- B738DR_fms_test1 = 2005
end


function find_alt_restrict(rest_from, rest_to)
	
	local ii = 0
	local finded_rest_idx = 0
	
	if rest_from < rest_to then
		for ii = rest_from, rest_to do
			if legs_data[ii][17] > 9 then
				finded_rest_idx = ii
				break
			end
		end
		if rest_to == ed_found and finded_rest_idx == 0 then
			finded_rest_idx = ed_found
		end
	else
		for ii = rest_from, rest_to, -1 do
			if legs_data[ii][17] > 9 then
				finded_rest_idx = ii
				break
			end
		end
	end
	return finded_rest_idx
	
end

function is_ils(nav_id, nav_freq)
	local nd_x = 0
	local idx_rec = 0
	local result = 0
	
	raw_table('idx_rec_table')
	local idx_rec_table = {}
	local idx_rec_table_n = 0
	
	if string.len(nav_id) > 0 then
		if ils_nav_num > 0 then
			nd_x = string.byte(string.sub(nav_id, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_ils[nd_x][99999] > 0 then
					for ii = 1, idx_ils[nd_x][99999] do
						idx_rec = idx_ils[nd_x][ii]
						if nav_id == ils_nav[idx_rec][1] and nav_freq == ils_nav[idx_rec][6] then
							-- result = idx_rec
							-- break
							idx_rec_table_n = idx_rec_table_n + 1
							idx_rec_table[idx_rec_table_n] = idx_rec
						end
					end
				end
			end
		end
	end
	
	local ils_latitude = simDR_latitude
	local ils_longitude = simDR_longitude
	local min_dist = 99
	if idx_rec_table_n == 1 then
		min_dist = nd_calc_dist2(ils_latitude, ils_longitude, ils_nav[idx_rec_table[1]][2], ils_nav[idx_rec_table[1]][3])
		result = idx_rec_table[1]
	elseif idx_rec_table_n > 1 then
		min_dist = nd_calc_dist2(ils_latitude, ils_longitude, ils_nav[idx_rec_table[1]][2], ils_nav[idx_rec_table[1]][3])
		local ils_dist = 0
		result = idx_rec_table[1]
		for ii = 2, idx_rec_table_n do
			ils_dist = nd_calc_dist2(ils_latitude, ils_longitude, ils_nav[idx_rec_table[ii]][2], ils_nav[idx_rec_table[ii]][3])
			if ils_dist < min_dist then
				min_dist = ils_dist
				result = idx_rec_table[ii]
			end
		end
	end
	if min_dist > 30 then
		result = 0
	end
	return result
end

function is_gls(nav_id, nav_channel)
	local nd_x = 0
	local idx_rec = 0
	local result = 0
	
	raw_table('idx_rec_table')
	local idx_rec_table = {}
	local idx_rec_table_n = 0
	
	if string.len(nav_id) > 0 then
		if gls_nav_num > 0 then
			nd_x = string.byte(string.sub(nav_id, 1, 1))
			if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
				if idx_gls[nd_x][99999] > 0 then
					for ii = 1, idx_gls[nd_x][99999] do
						idx_rec = idx_gls[nd_x][ii]
						if nav_id == gls_nav[idx_rec][1] and nav_channel == gls_nav[idx_rec][6] then
							-- result = idx_rec
							-- break
							idx_rec_table_n = idx_rec_table_n + 1
							idx_rec_table[idx_rec_table_n] = idx_rec
						end
					end
				end
			end
		end
	end
	
	local min_dist = 99
	local ils_latitude = simDR_latitude
	local ils_longitude = simDR_longitude
	if idx_rec_table_n == 1 then
		min_dist = nd_calc_dist2(ils_latitude, ils_longitude, gls_nav[idx_rec_table[1]][2], gls_nav[idx_rec_table[1]][3])
		result = idx_rec_table[1]
	elseif idx_rec_table_n > 1 then
		min_dist = nd_calc_dist2(ils_latitude, ils_longitude, gls_nav[idx_rec_table[1]][2], gls_nav[idx_rec_table[1]][3])
		local ils_dist = 0
		result = idx_rec_table[1]
		for ii = 2, idx_rec_table_n do
			ils_dist = nd_calc_dist2(ils_latitude, ils_longitude, gls_nav[idx_rec_table[ii]][2], gls_nav[idx_rec_table[ii]][3])
			if ils_dist < min_dist then
				min_dist = ils_dist
				result = idx_rec_table[ii]
			end
		end
	end
	if min_dist > 30 then
		result = 0
	end
	return result
end

function vor_or_ils(in_freq)
	local result = 0
	if in_freq <= 11195 then
		local freq_str = string.sub(string.format("%05d", in_freq), 4, 4)
		if freq_str == "1" or freq_str == "3" or freq_str == "5" or freq_str == "7" or freq_str == "9" then
			result = 1
		end
	end
	return result
end

function read_gls_runway_height(entry_icao, entry_record)
	
	local apt_ok = 0
	local line = ""
	local i = 0
	local j = 0
	local k = 0
	local line_trim = ""
	local line_char = ""
	local line_lenght = 0
	raw_table('words')
	local words = {}
	local temp_height = 0
	
	local result_height = 0
	local result_lat = 0
	local result_lon = 0
	local temp_lat_deg = 0
	local temp_lat_min = 0
	local temp_lat_sec = 0
	local temp_lat_sec2 = 0
	local temp_lon_deg = 0
	local temp_lon_min = 0
	local temp_lon_sec = 0
	local temp_lon_sec2 = 0
	
	if cust_def_data == 1 then
		file_name = "Custom Data/CIFP/" .. entry_icao
		file_name = file_name .. ".dat"
		file_navdata = io.open(file_name, "r")
		if file_navdata ~= nil then
			apt_ok = 1
		end
	elseif cust_def_data == 2 then
		file_name = "Resources/default data/CIFP/" .. entry_icao
		file_name = file_name .. ".dat"
		file_navdata = io.open(file_name, "r")
		if file_navdata ~= nil then
			apt_ok = 1
		end
	end
	
	if apt_ok ~= 0 then
		--B738DR_fms_test4 = cust_def_data
		line = file_navdata:read()
		while line do
			--B738DR_fms_test4 = B738DR_fms_test4 + 1
			i = 0
			j = 0
			line_trim = ""
			line_char = ""
			line_lenght = string.len(line)
			if line_lenght > 0 then
				for k = 1, line_lenght do
					line_char = string.sub(line, k, k)
					if line_char == "," or line_char == ";" then
						if i == 1 then
							j = j + 1
							words[j] = line_trim
							i = 0
							line_trim = ""
						end
					else
						line_trim = line_trim .. line_char
						i = 1
					end
				end
				if string.len(line_trim) > 0 then
					j = j + 1
					words[j] = line_trim
				end
				
				if j > 0 then
					if string.len(words[j]) > 1 and string.byte(words[j], -1) == 13 then	-- CR
						words[j] = string.sub(words[j], 1, -2)
					end
				end
				
				if j > 9 then
					if words[1] == entry_record then
						-- B738DR_fms_test4 = B738DR_fms_test4 + 10
						temp_height = tonumber(words[4])
						if temp_height ~= nil then
							result_height = temp_height
						end
						-- convert to lat/lon
						if string.len(words[9]) > 6 then
							temp_lat_deg = tonumber(string.sub(words[9], 2, 3))
							temp_lat_min = tonumber(string.sub(words[9], 4, 5))
							temp_lat_sec = tonumber(string.sub(words[9], 6, 7))
							temp_lat_sec2 = tonumber(string.sub(words[9], 8, 9))
							if temp_lat_deg ~= nil and temp_lat_min ~= nil and temp_lat_sec ~= nil then
								if temp_lat_sec2 == nil then
									result_lat = temp_lat_deg + (temp_lat_min / 60) + (temp_lat_sec / 3600)
								else
									result_lat = temp_lat_deg + (temp_lat_min / 60) + ((temp_lat_sec + (temp_lat_sec2 / 100))/ 3600)
								end
							end
							if string.sub(words[9], 1, 1) == "S" then
								result_lat = -result_lat
							end
						end
						
						if string.len(words[10]) > 7 then
							temp_lon_deg = tonumber(string.sub(words[10], 2, 4))
							temp_lon_min = tonumber(string.sub(words[10], 5, 6))
							temp_lon_sec = tonumber(string.sub(words[10], 7, 8))
							temp_lon_sec2 = tonumber(string.sub(words[10], 9, 10))
							if temp_lon_deg ~= nil and temp_lon_min ~= nil and temp_lon_sec ~= nil then
								if temp_lat_sec2 == nil then
									result_lon = temp_lon_deg + (temp_lon_min / 60) + (temp_lon_sec / 3600)
								else
									result_lon = temp_lon_deg + (temp_lon_min / 60) + ((temp_lon_sec + (temp_lon_sec2 / 100))/ 3600)
								end
							end
							if string.sub(words[10], 1, 1) == "W" then
								result_lon = -result_lon
							end
						end
						break
					end
				end
			end
			line = file_navdata:read()
		end
		file_navdata:close()
	end
	return result_lat, result_lon, result_height
end

function find_gls_runway(entry_in, entry_icao)
	
	local ii = 0
	local nd_x = 0
	local idx_rec = 0
	local result_lat = 0
	local result_lon = 0
	local result_height = 0
	local runway_txt = ""
	local res_lat = 0
	local res_lon = 0
	
	if rnw_data_num > 0 and string.len(entry_icao) > 0 then
		nd_x = string.byte(string.sub(entry_icao, 1, 1))
		if (nd_x >= 48 and nd_x <= 57) or (nd_x >= 65 and nd_x <= 90) then
			if idx_rnw[nd_x][99999] > 0 then
				for ii = 1, idx_rnw[nd_x][99999] do
					idx_rec = idx_rnw[nd_x][ii]
					if entry_icao == rnw_data[idx_rec][1] and entry_in == rnw_data[idx_rec][2] then
						result_lat = rnw_data[idx_rec][3]
						result_lon = rnw_data[idx_rec][4]
						break
					end
				end
			end
		end
	end
	
	if result_lat ~= 0 or result_lon ~= 0 then
		--B738DR_fms_test4 = 1
		runway_txt = "RWY:RW" .. entry_in
		if string.len(entry_in) == 2 then
			runway_txt = runway_txt .. " "
		end
		res_lat, res_lon, result_height = read_gls_runway_height(entry_icao, runway_txt)
		if res_lat ~= 0 or res_lon ~= 0 then
			result_lat = res_lat
			result_lon = res_lon
			--B738DR_fms_test4 = 1
		end
	end
	
	return result_lat, result_lon, result_height
end

function B738_gls_perf()
	
	local nd_lat = simDR_latitude
	local nd_lon = simDR_longitude
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	local n = 0
	local range = 40	-- 40 NM
	local delta_pos = 0
	local skip = 0
	
	local nd_teak_0 = 15
	local nd_teak_1 = 14
	
	if gls_nav_num > 0 then	--and B738DR_mmr ~= 0 then
		if first_time_gls < 2 then
			nd_teak_0 = 1000
			nd_teak_1 = 999
		end
		
		if gls_to == gls_nav_num then
			gls_teak = 0
			if first_time_gls < 2 then
				first_time_gls = first_time_gls + 1
			end
			if gls_page == 0 then
				gls_page = 1
				gls_page2_num = 0
				gls_page2 = {}
			else
				gls_page = 0
				gls_page1_num = 0
				gls_page1 = {}
			end
		else
			gls_teak = gls_teak + 1
		end
		
		gls_from = (gls_teak * nd_teak_0) + 1
		gls_to = gls_from + nd_teak_1
		
		if gls_to > gls_nav_num then
			gls_to = gls_nav_num
		end
		
		for n = gls_from, gls_to do 
			
			nd_lat2 = gls_nav[n][2]
			nd_lon2 = gls_nav[n][3]
			
			skip = 0
			delta_pos = nd_lat2 - nd_lat
			if delta_pos < 0 then
				delta_pos = -delta_pos
			end
			if delta_pos > 8 then
				skip = 1
			end
			delta_pos = nd_lon2 - nd_lon
			if delta_pos < 0 then
				delta_pos = -delta_pos
			end
			if delta_pos > 8 then
				skip = 1
			end
			
			if nd_lat2 == 0 then
				skip = 1
			end
			
			if skip == 0 then
				
				nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
				
				if nd_dis < range then
					if gls_page == 0 then	-- create nd_page1
						gls_page1_num = gls_page1_num + 1
						gls_page1[gls_page1_num] = {}
						gls_page1[gls_page1_num][1] = n
					else					-- create nd_page2
						gls_page2_num = gls_page2_num + 1
						gls_page2[gls_page2_num] = {}
						gls_page2[gls_page2_num][1] = n
					end
				end
			end
		end
	else
		gls_page1_num = 0
		gls_page2_num = 0
		first_time_gls = 2
	end
end

function B738_gls()
	local nd_lat = simDR_latitude
	local nd_lon = simDR_longitude
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_hdg = 0
	local nd_rel_hdg = 0
	local skip = 0
	local delta_pos = 0
	local gls_rnw_lat = 0
	local gls_rnw_lon = 0
	local gls_rnw_height = 0
	local gls_dist_scale = 0
	local gls_horz_scale = 0
	local gls_vert_scale = 0
	local gls_vert_path = 0
	
	local gls1_active = 0
	local gls1_id = ""
	local gls1_dme = 0
	local gls1_alt = 0
	local gls1_crs = 0
	local gls1_crs_mag = 0
	local gls1_gs = 0
	local gls1_height = 0
	local gls1_rnw = ""
	local gls1_horz_dev = 2.5
	local gls1_vert_dev = 2.5
	local gls1_xtrack = 999
	local gls1_type = 0
	
	local gls2_active = 0
	local gls2_id = ""
	local gls2_dme = 0
	local gls2_alt = 0
	local gls2_crs = 0
	local gls2_crs_mag = 0
	local gls2_gs = 0
	local gls2_height = 0
	local gls2_rnw = ""
	local gls2_horz_dev = 2.5
	local gls2_vert_dev = 2.5
	local gls2_xtrack = 999
	local gls2_type = 0
  local n = 0
	
	if gls_nav_num > 0 and B738DR_mmr ~= 0 then
		
		
		--for nn = 1, gls_nav_num do
		
		local gls_to_act = 0
		if gls_page == 0 then
			gls_to_act = gls_page2_num
		else
			gls_to_act = gls_page1_num
		end
		
		--B738DR_fms_test4 = gls_to_act
		
		if gls_to_act > 0 then
			for nn = 1, gls_to_act do
				
				if gls_page == 0 then
					n = gls_page2[nn][1]
				else
					n = gls_page1[nn][1]
				end
				
				nd_lat2 = gls_nav[n][2]
				nd_lon2 = gls_nav[n][3]
				
				skip = 0
				delta_pos = nd_lat2 - nd_lat
				if delta_pos < 0 then
					delta_pos = -delta_pos
				end
				if delta_pos > 8 then
					skip = 1
				end
				delta_pos = nd_lon2 - nd_lon
				if delta_pos < 0 then
					delta_pos = -delta_pos
				end
				if delta_pos > 8 then
					skip = 1
				end
				
				if skip == 0 then
					nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
					if nd_dis < 24.5 then	--24.5/23 NM
						if B738DR_mmr_cpt_act_mode == 2 and simDR_gps_fail ~= 6 then
							if B738DR_mmr_cpt_act_value == gls_nav[n][6] then
								gls1_active = 1
								gls1_id = gls_nav[n][1]
								gls1_gs = gls_nav[n][9]
								gls1_rnw = gls_nav[n][4]
								gls1_crs = gls_nav[n][5]
								gls1_crs_mag = (gls1_crs + mag_variation_deg(gls_nav[n][2], gls_nav[n][3]) + 360) % 360
								gls1_type = gls_nav[n][11]
								if gls1_idx ~= n then
									gls1_idx = n
									gls1_rnw_lat, gls1_rnw_lon, gls1_rnw_height = find_gls_runway(gls1_rnw, gls_nav[n][7])
									--B738DR_fms_test4 = 1
								end
								gls_rnw_lat = gls1_rnw_lat
								gls_rnw_lon = gls1_rnw_lon
								gls_rnw_height = gls1_rnw_height
								
								if gls_rnw_lat ~= 0 or gls_rnw_lon ~= 0 then
									nd_dis = nd_calc_dist2(nd_lat, nd_lon, gls_rnw_lat, gls_rnw_lon)
									gls1_dme = nd_dis
									if gls_rnw_height == 0 then
										gls1_height = gls_nav[n][10] + 50
									else
										gls1_height = gls_rnw_height + 50
									end
									gls1_alt = (gls1_dme * 6076 * math.tan(math.rad(gls1_gs))) + gls1_height
									
									if gls1_dme == 0 then
										gls1_horz_dev = 0
										gls1_vert_dev = 2.5
									else
										-- horizontal deviation
										nd_hdg = nd_calc_brg(nd_lat, nd_lon, gls_rnw_lat, gls_rnw_lon)
										nd_rel_hdg = (gls1_crs - nd_hdg + 360) % 360
										if nd_rel_hdg > 180 then
											nd_rel_hdg = nd_rel_hdg - 360
										end
										if nd_rel_hdg > -90 and nd_rel_hdg < 90 then
											gls1_horz_dev = gls1_dme * math.sin(math.rad(nd_rel_hdg))
										else
											if nd_rel_hdg > 0 then
												nd_rel_hdg = 180 - nd_rel_hdg
											else
												nd_rel_hdg = 180 + nd_rel_hdg
												nd_rel_hdg = -nd_rel_hdg
											end
											gls1_horz_dev = gls1_dme * math.sin(math.rad(nd_rel_hdg))
										end
										
										gls1_xtrack = gls1_horz_dev
										gls_dist_scale = gls1_dme * math.cos(math.rad(math.abs(nd_rel_hdg)))
										gls_horz_scale = (gls_dist_scale * math.tan(math.rad(2.5))) + 0.12
										gls1_horz_dev = (gls1_horz_dev / gls_horz_scale) * 2.5
										
										if gls1_horz_dev > 2.5 then
											gls1_horz_dev = 2.5
										elseif gls1_horz_dev < -2.5 then
											gls1_horz_dev = -2.5
										end
										
										gls_vert_scale = (gls1_dme * 6076 * math.tan(math.rad(gls1_gs + 0.7))) + gls1_height
										gls_vert_scale = gls_vert_scale - gls1_alt
										gls1_vert_dev = elevation_ft - gls1_alt
										
										gls1_vert_dev = (gls1_vert_dev / gls_vert_scale) * 2.5
										if gls1_vert_dev > 2.5 then
											gls1_vert_dev = 2.5
										elseif gls1_vert_dev < -2.5 then
											gls1_vert_dev = -2.5
										end
									end
								end
							end
						end
						if B738DR_mmr_fo_act_mode == 2 and simDR_gps2_fail ~= 6 then
							if B738DR_mmr_cpt_act_value == B738DR_mmr_fo_act_value then
								gls2_active = gls1_active
								gls2_idx = gls1_idx
								gls2_id = gls1_id
								gls2_dme = gls1_dme
								gls2_gs = gls1_gs
								gls2_height = gls1_height
								gls2_alt = gls1_alt
								gls2_rnw = gls1_rnw
								gls2_crs = gls1_crs
								gls2_crs_mag = gls1_crs_mag
								gls2_horz_dev = gls1_horz_dev
								gls2_vert_dev = gls1_vert_dev
								gls2_xtrack = gls1_xtrack
								gls2_type = gls1_type
							elseif B738DR_mmr_fo_act_value == gls_nav[n][6] and simDR_gps2_fail ~= 6 then
								gls2_active = 1
								gls2_id = gls_nav[n][1]
								gls2_gs = gls_nav[n][9]
								gls2_rnw = gls_nav[n][4]
								gls2_crs = gls_nav[n][5]
								gls2_crs_mag = (gls2_crs + mag_variation_deg(gls_nav[n][2], gls_nav[n][3]) + 360) % 360
								gls2_type = gls_nav[n][11]
								
								if gls2_idx ~= n then
									gls2_idx = n
									gls2_rnw_lat, gls2_rnw_lon, gls2_rnw_height = find_gls_runway(gls2_rnw, gls_nav[n][7])
								end
								gls_rnw_lat = gls2_rnw_lat
								gls_rnw_lon = gls2_rnw_lon
								gls_rnw_height = gls2_rnw_height
								
								if gls_rnw_lat ~= 0 or gls_rnw_lon ~= 0 then
									nd_dis = nd_calc_dist2(nd_lat, nd_lon, gls_rnw_lat, gls_rnw_lon)
									gls2_dme = nd_dis
									if gls_rnw_height == 0 then
										gls2_height = gls_nav[n][10] + 50
									else
										gls2_height = gls_rnw_height + 50
									end
									gls2_alt = (gls2_dme * 6076 * math.tan(math.rad(gls2_gs))) + gls2_height
									
									if gls2_dme == 0 then
										gls2_horz_dev = 0
										gls2_vert_dev = 2.5
									else
										-- horizontal deviation
										nd_hdg = nd_calc_brg(nd_lat, nd_lon, gls_rnw_lat, gls_rnw_lon)
										nd_rel_hdg = (gls2_crs - nd_hdg + 360) % 360
										if nd_rel_hdg > 180 then
											nd_rel_hdg = nd_rel_hdg - 360
										end
										if nd_rel_hdg > -90 and nd_rel_hdg < 90 then
											gls2_horz_dev = gls2_dme * math.sin(math.rad(nd_rel_hdg))
										else
											if nd_rel_hdg > 0 then
												nd_rel_hdg = 180 - nd_rel_hdg
											else
												nd_rel_hdg = 180 + nd_rel_hdg
												nd_rel_hdg = -nd_rel_hdg
											end
											gls2_horz_dev = gls2_dme * math.sin(math.rad(nd_rel_hdg))
										end
										
										gls2_xtrack = gls2_horz_dev
										
										gls_dist_scale = gls2_dme * 6076 * math.cos(math.rad(math.abs(nd_rel_hdg)))
										gls_horz_scale = (gls_dist_scale * 6076 * math.tan(math.rad(2.5))) + 0.12
										gls2_horz_dev = (gls2_horz_dev / gls_horz_scale) * 2.5
										
										if gls2_horz_dev > 2.5 then
											gls2_horz_dev = 2.5
										elseif gls2_horz_dev < -2.5 then
											gls2_horz_dev = -2.5
										end
										
										-- vertical deviation
										gls_vert_scale = (gls2_dme * 6076 * math.tan(math.rad(gls2_gs + 0.7))) + gls2_height
										gls_vert_scale = gls_vert_scale - gls2_alt
										gls2_vert_dev = elevation_ft - gls2_alt
										
										gls2_vert_dev = (gls2_vert_dev / gls_vert_scale) * 2.5
										if gls2_vert_dev > 2.5 then
											gls2_vert_dev = 2.5
										elseif gls2_vert_dev < -2.5 then
											gls2_vert_dev = -2.5
										end
									end
								end
							end
						end
						if gls1_active ~= 0 and gls2_active ~= 0 then
							break
						end
					end
				end
			end
		end
	end
	
	if gls1_active == 0 then
		gls1_idx = 0
	end
	if gls2_active == 0 then
		gls2_idx = 0
	end
	
	B738DR_gls1_active = gls1_active
	B738DR_gls1_id = gls1_id
	B738DR_gls1_idx = gls1_idx
	B738DR_gls1_dme = gls1_dme
	B738DR_gls1_alt = gls1_alt
	B738DR_gls1_rnw = gls1_rnw
	B738DR_gls1_crs = gls1_crs
	B738DR_gls1_crs_mag = gls1_crs_mag
	B738DR_gls1_horz_dev = gls1_horz_dev
	B738DR_gls1_vert_dev = gls1_vert_dev
	B738DR_gls1_xtrack = gls1_xtrack
	B738DR_gls1_type = gls1_type
	
	B738DR_gls2_active = gls2_active
	B738DR_gls2_id = gls2_id
	B738DR_gls2_idx = gls2_idx
	B738DR_gls2_dme = gls2_dme
	B738DR_gls2_alt = gls2_alt
	B738DR_gls2_rnw = gls2_rnw
	B738DR_gls2_crs = gls2_crs
	B738DR_gls2_crs_mag = gls2_crs_mag
	B738DR_gls2_horz_dev = gls2_horz_dev
	B738DR_gls2_vert_dev = gls2_vert_dev
	B738DR_gls2_xtrack = gls2_xtrack
	B738DR_gls2_type = gls2_type
	
	
end

function mmr_active_mode()
	if B738DR_autopilot_vhf_source_pos == -1 then
		-- pilot
		B738DR_mmr_act_mode = B738DR_mmr_cpt_act_mode
		B738DR_mmr_active = B738DR_gls1_active
		B738DR_mmr_channel = B738DR_mmr_cpt_act_value
		B738DR_mmr_id = B738DR_gls1_id
		B738DR_mmr_dme = B738DR_gls1_dme
		B738DR_mmr_rnw = B738DR_gls1_rnw
		B738DR_mmr_crs = B738DR_gls1_crs
		B738DR_mmr_crs_mag = B738DR_gls1_crs_mag
		B738DR_mmr_type = B738DR_gls1_type
		B738DR_mmr_vdef = B738DR_gls1_vert_dev
		B738DR_mmr_hdef = B738DR_gls1_horz_dev
		B738DR_mmr_no_id = B738DR_tuned_nav1_no_id
		-- copilot
		B738DR_mmr_act_mode_fo = B738DR_mmr_cpt_act_mode
		B738DR_mmr_active_fo = B738DR_gls1_active
		B738DR_mmr_channel_fo = B738DR_mmr_cpt_act_value
		B738DR_mmr_id_fo = B738DR_gls1_id
		B738DR_mmr_dme_fo = B738DR_gls1_dme
		B738DR_mmr_rnw_fo = B738DR_gls1_rnw
		B738DR_mmr_crs_fo = B738DR_gls1_crs
		B738DR_mmr_crs_mag_fo = B738DR_gls1_crs_mag
		B738DR_mmr_type_fo = B738DR_gls1_type
		B738DR_mmr_vdef_fo = B738DR_gls1_vert_dev
		B738DR_mmr_hdef_fo = B738DR_gls1_horz_dev
		B738DR_mmr_no_id_fo = B738DR_tuned_nav1_no_id
	elseif B738DR_autopilot_vhf_source_pos == 0 then
		-- pilot
		B738DR_mmr_act_mode = B738DR_mmr_cpt_act_mode
		B738DR_mmr_active = B738DR_gls1_active
		B738DR_mmr_channel = B738DR_mmr_cpt_act_value
		B738DR_mmr_id = B738DR_gls1_id
		B738DR_mmr_dme = B738DR_gls1_dme
		B738DR_mmr_rnw = B738DR_gls1_rnw
		B738DR_mmr_crs = B738DR_gls1_crs
		B738DR_mmr_crs_mag = B738DR_gls1_crs_mag
		B738DR_mmr_type = B738DR_gls1_type
		B738DR_mmr_vdef = B738DR_gls1_vert_dev
		B738DR_mmr_hdef = B738DR_gls1_horz_dev
		B738DR_mmr_no_id = B738DR_tuned_nav1_no_id
		-- copilot
		B738DR_mmr_act_mode_fo = B738DR_mmr_fo_act_mode
		B738DR_mmr_active_fo = B738DR_gls2_active
		B738DR_mmr_channel_fo = B738DR_mmr_fo_act_value
		B738DR_mmr_id_fo = B738DR_gls2_id
		B738DR_mmr_dme_fo = B738DR_gls2_dme
		B738DR_mmr_rnw_fo = B738DR_gls2_rnw
		B738DR_mmr_crs_fo = B738DR_gls2_crs
		B738DR_mmr_crs_mag_fo = B738DR_gls2_crs_mag
		B738DR_mmr_type_fo = B738DR_gls2_type
		B738DR_mmr_vdef_fo = B738DR_gls2_vert_dev
		B738DR_mmr_hdef_fo = B738DR_gls2_horz_dev
		B738DR_mmr_no_id_fo = B738DR_tuned_nav2_no_id
	elseif B738DR_autopilot_vhf_source_pos == 1 then
		-- pilot
		B738DR_mmr_act_mode = B738DR_mmr_fo_act_mode
		B738DR_mmr_active = B738DR_gls2_active
		B738DR_mmr_channel = B738DR_mmr_fo_act_value
		B738DR_mmr_id = B738DR_gls2_id
		B738DR_mmr_dme = B738DR_gls2_dme
		B738DR_mmr_rnw = B738DR_gls2_rnw
		B738DR_mmr_crs = B738DR_gls2_crs
		B738DR_mmr_crs_mag = B738DR_gls2_crs_mag
		B738DR_mmr_type = B738DR_gls2_type
		B738DR_mmr_vdef = B738DR_gls2_vert_dev
		B738DR_mmr_hdef = B738DR_gls2_horz_dev
		B738DR_mmr_no_id = B738DR_tuned_nav2_no_id
		-- copilot
		B738DR_mmr_act_mode_fo = B738DR_mmr_fo_act_mode
		B738DR_mmr_active_fo = B738DR_gls2_active
		B738DR_mmr_channel_fo = B738DR_mmr_fo_act_value
		B738DR_mmr_id_fo = B738DR_gls2_id
		B738DR_mmr_dme_fo = B738DR_gls2_dme
		B738DR_mmr_rnw_fo = B738DR_gls2_rnw
		B738DR_mmr_crs_fo = B738DR_gls2_crs
		B738DR_mmr_crs_mag_fo = B738DR_gls2_crs_mag
		B738DR_mmr_type_fo = B738DR_gls2_type
		B738DR_mmr_vdef_fo = B738DR_gls2_vert_dev
		B738DR_mmr_hdef_fo = B738DR_gls2_horz_dev
		B738DR_mmr_no_id_fo = B738DR_tuned_nav2_no_id
	end
end

function nav1_decoding()
	B738DR_tuned_nav1_decode = 0
end

function nav2_decoding()
	B738DR_tuned_nav2_decode = 0
end

function ils_cpt_id_dis_t1()
	ils_cpt_id_disagree = 0
end
function ils_cpt_id_dis_t2()
	ils_cpt_id_disagree = 1
end
function ils_cpt_crs_dis_t1()
	ils_cpt_crs_disagree = 0
end
function ils_cpt_crs_dis_t2()
	ils_cpt_crs_disagree = 1
end

function ils_cpt_id_dis_rst()
	if is_timer_scheduled(ils_cpt_id_dis_t1) == true then
		stop_timer(ils_cpt_id_dis_t1)
	end
	if is_timer_scheduled(ils_cpt_id_dis_t2) == true then
		stop_timer(ils_cpt_id_dis_t2)
	end
	ils_cpt_id_disagree = 0
end

function ils_cpt_crs_dis_rst()
	if is_timer_scheduled(ils_cpt_crs_dis_t1) == true then
		stop_timer(ils_cpt_crs_dis_t1)
	end
	if is_timer_scheduled(ils_cpt_crs_dis_t2) == true then
		stop_timer(ils_cpt_crs_dis_t2)
	end
	ils_cpt_crs_disagree = 0
end

function ils_fo_id_dis_t1()
	ils_fo_id_disagree = 0
end
function ils_fo_id_dis_t2()
	ils_fo_id_disagree = 1
end
function ils_fo_crs_dis_t1()
	ils_fo_crs_disagree = 0
end
function ils_fo_crs_dis_t2()
	ils_fo_crs_disagree = 1
end

function ils_fo_id_dis_rst()
	if is_timer_scheduled(ils_fo_id_dis_t1) == true then
		stop_timer(ils_fo_id_dis_t1)
	end
	if is_timer_scheduled(ils_fo_id_dis_t2) == true then
		stop_timer(ils_fo_id_dis_t2)
	end
	ils_fo_id_disagree = 0
end

function ils_fo_crs_dis_rst()
	if is_timer_scheduled(ils_fo_crs_dis_t1) == true then
		stop_timer(ils_fo_crs_dis_t1)
	end
	if is_timer_scheduled(ils_fo_crs_dis_t2) == true then
		stop_timer(ils_fo_crs_dis_t2)
	end
	ils_fo_crs_disagree = 0
end

function gls_cpt_id_dis_t1()
	gls_cpt_id_disagree = 0
end
function gls_cpt_id_dis_t2()
	gls_cpt_id_disagree = 1
end
function gls_cpt_crs_dis_t1()
	gls_cpt_crs_disagree = 0
end
function gls_cpt_crs_dis_t2()
	gls_cpt_crs_disagree = 1
end

function gls_cpt_id_dis_rst()
	if is_timer_scheduled(gls_cpt_id_dis_t1) == true then
		stop_timer(gls_cpt_id_dis_t1)
	end
	if is_timer_scheduled(gls_cpt_id_dis_t2) == true then
		stop_timer(gls_cpt_id_dis_t2)
	end
	gls_cpt_id_disagree = 0
end

function gls_cpt_crs_dis_rst()
	if is_timer_scheduled(gls_cpt_crs_dis_t1) == true then
		stop_timer(gls_cpt_crs_dis_t1)
	end
	if is_timer_scheduled(gls_cpt_crs_dis_t2) == true then
		stop_timer(gls_cpt_crs_dis_t2)
	end
	gls_cpt_crs_disagree = 0
end

function gls_fo_id_dis_t1()
	gls_fo_id_disagree = 0
end
function gls_fo_id_dis_t2()
	gls_fo_id_disagree = 1
end
function gls_fo_crs_dis_t1()
	gls_fo_crs_disagree = 0
end
function gls_fo_crs_dis_t2()
	gls_fo_crs_disagree = 1
end

function gls_fo_id_dis_rst()
	if is_timer_scheduled(gls_fo_id_dis_t1) == true then
		stop_timer(gls_fo_id_dis_t1)
	end
	if is_timer_scheduled(gls_fo_id_dis_t2) == true then
		stop_timer(gls_fo_id_dis_t2)
	end
	gls_fo_id_disagree = 0
end

function gls_fo_crs_dis_rst()
	if is_timer_scheduled(gls_fo_crs_dis_t1) == true then
		stop_timer(gls_fo_crs_dis_t1)
	end
	if is_timer_scheduled(gls_fo_crs_dis_t2) == true then
		stop_timer(gls_fo_crs_dis_t2)
	end
	gls_fo_crs_disagree = 0
end

function B738_ils_disagree()
	
	-- B738DR_missed_app_act
	local vorloc_lnav_enable = 0
	local miss_app_act_idx = 0
	
	if offset > missed_app_act_idx then
		missed_app_act_idx = 0
	elseif legs_num < 2 then
		missed_app_act_idx = 0
	elseif simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
		if simDR_ground_spd * 1.9438444 < 80 then
			missed_app_act_idx = 0
		end
	end
	
	if missed_app_act_idx == 0 then
		miss_app_act_idx = first_miss_app_idx
	else
		miss_app_act_idx = missed_app_act_idx
	end
	if miss_app_act_idx ~= 0 and miss_app_act_idx <= legs_num then
		if offset <= miss_app_act_idx and miss_app_act_idx > 1 then
			if (math.abs(legs_data[miss_app_act_idx-1][18] - legs_data[miss_app_act_idx][18]) < 5) and legs_data[miss_app_act_idx][3] ~= 0 then
				vorloc_lnav_enable = 1
			end
		end
	end
	B738DR_vorloc_lnav_enable = vorloc_lnav_enable
	
	-- CPT ILS/GLS
	if B738DR_mmr_cpt_act_mode == 0 then
		ils_cpt_id_dis_rst()
		ils_cpt_crs_dis_rst()
		gls_cpt_id_dis_rst()
		gls_cpt_crs_dis_rst()
	elseif B738DR_mmr_cpt_act_mode == 1 then
		-- ILS
		gls_cpt_id_dis_rst()
		gls_cpt_crs_dis_rst()
		if B738DR_nav_id_decode ~= 0 then
			ils_cpt_id_dis_rst()
			ils_cpt_crs_dis_rst()
		elseif B738DR_nav_nav_id == nil then
			ils_cpt_id_dis_rst()
			ils_cpt_crs_dis_rst()
		elseif B738DR_nav_nav_id == "" then
			ils_cpt_id_dis_rst()
			ils_cpt_crs_dis_rst()
		else
			if ils_id == "" and gls_id == "" and lda_id == "" then
				ils_cpt_id_dis_rst()
			elseif B738DR_nav_nav_id == ils_id and ils_id ~= "" then
				if is_timer_scheduled(ils_cpt_id_dis_t2) == true then
					stop_timer(ils_cpt_id_dis_t2)
				end
				if ils_cpt_id_disagree ~= 0 and is_timer_scheduled(ils_cpt_id_dis_t1) == false then
					run_after_time(ils_cpt_id_dis_t1, 1)
				end
			elseif B738DR_nav_nav_id == lda_id and lda_id ~= "" then
				if is_timer_scheduled(ils_cpt_id_dis_t2) == true then
					stop_timer(ils_cpt_id_dis_t2)
				end
				if ils_cpt_id_disagree ~= 0 and is_timer_scheduled(ils_cpt_id_dis_t1) == false then
					run_after_time(ils_cpt_id_dis_t1, 1)
				end
			else
				if is_timer_scheduled(ils_cpt_id_dis_t1) == true then
					stop_timer(ils_cpt_id_dis_t1)
				end
				if ils_cpt_id_disagree == 0 and is_timer_scheduled(ils_cpt_id_dis_t2) == false then
					run_after_time(ils_cpt_id_dis_t2, 60)
				end
			end
			--if math.floor(B738DR_nav_obs + 0.5) == math.floor(B738DR_nav_tuned_crs + 0.5) then
			if math.floor(B738DR_course_pilot + 0.5) == math.floor(B738DR_nav_tuned_crs + 0.5) then
				if is_timer_scheduled(ils_cpt_crs_dis_t2) == true then
					stop_timer(ils_cpt_crs_dis_t2)
				end
				if ils_cpt_crs_disagree ~= 0 and is_timer_scheduled(ils_cpt_crs_dis_t1) == false then
					run_after_time(ils_cpt_crs_dis_t1, 1)
				end
			else
				if is_timer_scheduled(ils_cpt_crs_dis_t1) == true then
					stop_timer(ils_cpt_crs_dis_t1)
				end
				if ils_cpt_crs_disagree == 0 and is_timer_scheduled(ils_cpt_crs_dis_t2) == false then
					run_after_time(ils_cpt_crs_dis_t2, 60)
				end
			end
		end
	elseif B738DR_mmr_cpt_act_mode == 2 then
		-- GLS
		ils_cpt_id_dis_rst()
		ils_cpt_crs_dis_rst()
		if B738DR_mmr_id == nil then
			gls_cpt_id_dis_rst()
			gls_cpt_crs_dis_rst()
		elseif B738DR_mmr_id == "" then
			gls_cpt_id_dis_rst()
			gls_cpt_crs_dis_rst()
		else
			if ils_id == "" and gls_id == "" and lda_id == "" then
				gls_cpt_id_dis_rst()
			elseif B738DR_mmr_id == gls_id then
				if is_timer_scheduled(gls_cpt_id_dis_t2) == true then
					stop_timer(gls_cpt_id_dis_t2)
				end
				if gls_cpt_id_disagree ~= 0 and is_timer_scheduled(gls_cpt_id_dis_t1) == false then
					run_after_time(gls_cpt_id_dis_t1, 1)
				end
			else
				if is_timer_scheduled(gls_cpt_id_dis_t1) == true then
					stop_timer(gls_cpt_id_dis_t1)
				end
				if gls_cpt_id_disagree == 0 and is_timer_scheduled(gls_cpt_id_dis_t2) == false then
					run_after_time(gls_cpt_id_dis_t2, 60)
				end
			end
			if math.floor(B738DR_course_pilot + 0.5) == math.floor(B738DR_mmr_crs_mag + 0.5) then
				if is_timer_scheduled(gls_cpt_crs_dis_t2) == true then
					stop_timer(gls_cpt_crs_dis_t2)
				end
				if gls_cpt_crs_disagree ~= 0 and is_timer_scheduled(gls_cpt_crs_dis_t1) == false then
					run_after_time(gls_cpt_crs_dis_t1, 1)
				end
			else
				if is_timer_scheduled(gls_cpt_crs_dis_t1) == true then
					stop_timer(ils_cpt_crs_dis_t1)
				end
				if gls_cpt_crs_disagree == 0 and is_timer_scheduled(gls_cpt_crs_dis_t2) == false then
					run_after_time(gls_cpt_crs_dis_t2, 60)
				end
			end
		end
	end
	
	-- FO ILS
	if B738DR_mmr_fo_act_mode == 0 then
		ils_fo_id_dis_rst()
		ils_fo_crs_dis_rst()
		gls_fo_id_dis_rst()
		gls_fo_crs_dis_rst()
	elseif B738DR_mmr_fo_act_mode == 1 then
		-- ILS
		gls_fo_id_dis_rst()
		gls_fo_crs_dis_rst()
		if B738DR_nav_id_decode_fo ~= 0 then
			ils_fo_id_dis_rst()
			ils_fo_crs_dis_rst()
		elseif B738DR_nav_nav_id_fo == nil then
			ils_fo_id_dis_rst()
			ils_fo_crs_dis_rst()
		elseif B738DR_nav_nav_id_fo == "" then
			ils_fo_id_dis_rst()
			ils_fo_crs_dis_rst()
		else
			if ils_id == "" and gls_id == "" and lda_id == "" then
				ils_fo_id_dis_rst()
			elseif B738DR_nav_nav_id_fo == ils_id and ils_id ~= "" then
				if is_timer_scheduled(ils_fo_id_dis_t2) == true then
					stop_timer(ils_fo_id_dis_t2)
				end
				if ils_fo_id_disagree ~= 0 and is_timer_scheduled(ils_fo_id_dis_t1) == false then
					run_after_time(ils_fo_id_dis_t1, 1)
				end
			elseif B738DR_nav_nav_id_fo == lda_id and lda_id ~= "" then
				if is_timer_scheduled(ils_fo_id_dis_t2) == true then
					stop_timer(ils_fo_id_dis_t2)
				end
				if ils_fo_id_disagree ~= 0 and is_timer_scheduled(ils_fo_id_dis_t1) == false then
					run_after_time(ils_fo_id_dis_t1, 1)
				end
			else
				if is_timer_scheduled(ils_fo_id_dis_t1) == true then
					stop_timer(ils_fo_id_dis_t1)
				end
				if ils_fo_id_disagree == 0 and is_timer_scheduled(ils_fo_id_dis_t2) == false then
					run_after_time(ils_fo_id_dis_t2, 60)
				end
			end
			-- if math.floor(B738DR_nav_obs_fo + 0.5) == math.floor(ils_course + 0.5) then
			if math.floor(B738DR_course_copilot + 0.5) == math.floor(B738DR_nav_tuned_crs_fo + 0.5) then
				if is_timer_scheduled(ils_fo_crs_dis_t2) == true then
					stop_timer(ils_fo_crs_dis_t2)
				end
				if ils_fo_crs_disagree ~= 0 and is_timer_scheduled(ils_fo_crs_dis_t1) == false then
					run_after_time(ils_fo_crs_dis_t1, 1)
				end
			else
				if is_timer_scheduled(ils_fo_crs_dis_t1) == true then
					stop_timer(ils_fo_crs_dis_t1)
				end
				if ils_fo_crs_disagree == 0 and is_timer_scheduled(ils_fo_crs_dis_t2) == false then
					run_after_time(ils_fo_crs_dis_t2, 60)
				end
			end
		end
	elseif B738DR_mmr_fo_act_mode == 2 then
		-- GLS
		ils_fo_id_dis_rst()
		ils_fo_crs_dis_rst()
		if B738DR_mmr_id_fo == nil then
			gls_fo_id_dis_rst()
			gls_fo_crs_dis_rst()
		elseif B738DR_mmr_id_fo == "" then
			gls_fo_id_dis_rst()
			gls_fo_crs_dis_rst()
		else
			if ils_id == "" and gls_id == "" and lda_id == "" then
				gls_fo_id_dis_rst()
			elseif B738DR_mmr_id_fo == gls_id then
				if is_timer_scheduled(gls_fo_id_dis_t2) == true then
					stop_timer(gls_fo_id_dis_t2)
				end
				if gls_fo_id_disagree ~= 0 and is_timer_scheduled(gls_fo_id_dis_t1) == false then
					run_after_time(gls_fo_id_dis_t1, 1)
				end
			else
				if is_timer_scheduled(gls_fo_id_dis_t1) == true then
					stop_timer(gls_fo_id_dis_t1)
				end
				if gls_fo_id_disagree == 0 and is_timer_scheduled(gls_fo_id_dis_t2) == false then
					run_after_time(gls_fo_id_dis_t2, 60)
				end
			end
			if math.floor(B738DR_course_copilot + 0.5) == math.floor(B738DR_mmr_crs_mag_fo + 0.5) then
				if is_timer_scheduled(gls_fo_crs_dis_t2) == true then
					stop_timer(gls_fo_crs_dis_t2)
				end
				if gls_fo_crs_disagree ~= 0 and is_timer_scheduled(gls_fo_crs_dis_t1) == false then
					run_after_time(gls_fo_crs_dis_t1, 1)
				end
			else
				if is_timer_scheduled(gls_fo_crs_dis_t1) == true then
					stop_timer(gls_fo_crs_dis_t1)
				end
				if gls_fo_crs_disagree == 0 and is_timer_scheduled(gls_fo_crs_dis_t2) == false then
					run_after_time(gls_fo_crs_dis_t2, 60)
				end
			end
		end
	end
	
end

function B738_vor_sel()

	local str_len = 0
	local vor_crs = 0
	local vor_bcrs = 0
	local vor_hdg = 0
	local vor_bearing = 0
	local vor_distance = 0
	local vor_bear_l = 0
	local vor_bear_r = 0
	local str_crs = ""
	local str_bcrs = ""
	local vor_angle = 0
	local efis_zoom = 0
	local out_of_range = 0
	local vor_x = 0
	local vor_y = 0
	local vor_quadr = 0
	local vor2_sel_disable = 0
	local relative_brg = 0
	
	local nav_lat2 = 0
	local nav_lon2 = 0
	local nav_lat = ndx_lat
	local nav_lon = ndx_lon
	local mag_hdg = 0
	local delta_ils_hdg = 0
	local ils_hdg = 0
	local ils_on_off = 0
	local ils_x = 0
	local ils_y = 0
	local ils_dis = 0
	local ils_zoom = 0
	local ils_disable = 0
	local rte_plan_mode = 0
	local ils_idx = 0
	
	simDR_hsi_crs1 = simDR_nav1_obs_pilot
	simDR_hsi_crs2 = simDR_nav2_obs_copilot
	
	if B738DR_mmr == 0 then
		B738DR_mmr_cpt_act_mode = vor_or_ils(B738DR_nav1_hz)
		B738DR_mmr_fo_act_mode = vor_or_ils(B738DR_nav2_hz)
	end
	if mmr_cpt_act_mode_old ~= B738DR_mmr_cpt_act_mode then
		tuned_nav1_id = ""
		B738DR_tuned_nav1_type = 0
		B738DR_vor_dme_id1 = 0
		if is_timer_scheduled(nav1_decoding) == true then
			stop_timer(nav1_decoding)
		end
		B738DR_tuned_nav1_decode = 1
	end
	if mmr_fo_act_mode_old ~= B738DR_mmr_fo_act_mode then
		tuned_nav2_id = ""
		B738DR_tuned_nav2_type = 0
		B738DR_vor_dme_id2 = 0
		if is_timer_scheduled(nav2_decoding) == true then
			stop_timer(nav2_decoding)
		end
		B738DR_tuned_nav2_decode = 1
	end
	mmr_cpt_act_mode_old = B738DR_mmr_cpt_act_mode
	mmr_fo_act_mode_old = B738DR_mmr_fo_act_mode
	-- tuned NAV
	if B738DR_mmr_cpt_act_mode <= 1 then
		if simDR_nav1_type == 0 then
			tuned_nav1_id = ""
			B738DR_tuned_nav1_type = 0
			B738DR_vor_dme_id1 = 0
			if is_timer_scheduled(nav1_decoding) == true then
				stop_timer(nav1_decoding)
			end
			B738DR_tuned_nav1_decode = 1
		elseif simDR_nav1_horz_dsp == 0 then
			if simDR_nav1_has_dme == 0 then
				tuned_nav1_id = ""
				B738DR_tuned_nav1_type = 0
				B738DR_vor_dme_id1 = 0
				if is_timer_scheduled(nav1_decoding) == true then
					stop_timer(nav1_decoding)
				end
				B738DR_tuned_nav1_decode = 1
			elseif vor_or_ils(B738DR_nav1_hz) == 0 then
				if is_timer_scheduled(nav1_decoding) == true then
					stop_timer(nav1_decoding)
				end
				run_after_time(nav1_decoding, 6.5)
				B738DR_tuned_nav1_decode = 1
				
				tuned_nav1_id = simDR_nav1_nav_id
				B738DR_tuned_nav1_type = 1	-- VOR, DME
				B738DR_vor_dme_id1 = 1
			else
				ils_idx = is_ils(simDR_nav1_nav_id, B738DR_nav1_hz)
				if ils_idx == 0 then
					tuned_nav1_id = ""
					B738DR_tuned_nav1_type = 0
					B738DR_vor_dme_id1 = 0
					if is_timer_scheduled(nav1_decoding) == true then
						stop_timer(nav1_decoding)
					end
					B738DR_tuned_nav1_decode = 1
				elseif simDR_nav1_dme < 38 then	--27.2
					-- ILS range
					if is_timer_scheduled(nav1_decoding) == true then
						stop_timer(nav1_decoding)
					end
					run_after_time(nav1_decoding, 6.5)
					B738DR_tuned_nav1_decode = 1
					tuned_nav1_id = simDR_nav1_nav_id
					if ils_nav[ils_idx][9] ~= 1 then
						B738DR_tuned_nav1_type = 3	-- ILS
					else
						B738DR_tuned_nav1_type = 2	-- LOC only
					end
					if ils_nav[ils_idx][10] < 0 then
						B738DR_tuned_nav1_crs = (ils_nav[ils_idx][5] + mag_variation_deg(ils_nav[ils_idx][2], ils_nav[ils_idx][3]) + 360) % 360
					else
						B738DR_tuned_nav1_crs = ils_nav[ils_idx][10]
					end
					B738DR_vor_dme_id1 = 0
				else
					tuned_nav1_id = ""
					B738DR_tuned_nav1_type = 0
					B738DR_vor_dme_id1 = 0
					if is_timer_scheduled(nav1_decoding) == true then
						stop_timer(nav1_decoding)
					end
					B738DR_tuned_nav1_decode = 1
				end
			end
		elseif simDR_nav1_nav_id == nil then
			tuned_nav1_id = ""
			B738DR_tuned_nav1_type = 0
			B738DR_vor_dme_id1 = 0
			if is_timer_scheduled(nav1_decoding) == true then
				stop_timer(nav1_decoding)
			end
			B738DR_tuned_nav1_decode = 1
		elseif simDR_nav1_nav_id == "" then
			tuned_nav1_id = ""
			B738DR_tuned_nav1_type = 0
			B738DR_vor_dme_id1 = 0
			if is_timer_scheduled(nav1_decoding) == true then
				stop_timer(nav1_decoding)
			end
			B738DR_tuned_nav1_decode = 1
		elseif simDR_nav1_nav_id ~= tuned_nav1_id then
			if is_timer_scheduled(nav1_decoding) == true then
				stop_timer(nav1_decoding)
			end
			run_after_time(nav1_decoding, 6.5)
			B738DR_tuned_nav1_decode = 1
			
			tuned_nav1_id = simDR_nav1_nav_id
			if vor_or_ils(B738DR_nav1_hz) == 0 then
				B738DR_tuned_nav1_type = 1	-- VOR, DME
				B738DR_vor_dme_id1 = 1
			else
				ils_idx = is_ils(tuned_nav1_id, B738DR_nav1_hz)
				if ils_idx ~= 0 then
					if ils_nav[ils_idx][9] ~= 1 then
						B738DR_tuned_nav1_type = 3	-- ILS
					else
						B738DR_tuned_nav1_type = 2	-- LOC only
					end
					if ils_nav[ils_idx][10] < 0 then
						B738DR_tuned_nav1_crs = (ils_nav[ils_idx][5] + mag_variation_deg(ils_nav[ils_idx][2], ils_nav[ils_idx][3]) + 360) % 360
					else
						B738DR_tuned_nav1_crs = ils_nav[ils_idx][10]
					end
					B738DR_vor_dme_id1 = 0
				else
					tuned_nav1_id = ""
					B738DR_tuned_nav1_type = 0
					B738DR_vor_dme_id1 = 0
					if is_timer_scheduled(nav1_decoding) == true then
						stop_timer(nav1_decoding)
					end
					B738DR_tuned_nav1_decode = 1
				end
			end
		end
	else
		tuned_nav1_id = ""
		B738DR_tuned_nav1_type = 0
		B738DR_vor_dme_id1 = 0
		if is_timer_scheduled(nav1_decoding) == true then
			stop_timer(nav1_decoding)
		end
		B738DR_tuned_nav1_decode = 1
	end
	
	if tuned_nav1_id == "" then
		B738DR_tuned_nav1_no_id = 1
	else
		B738DR_tuned_nav1_no_id = 0
	end
	
	if B738DR_mmr_fo_act_mode <= 1 then
		if simDR_nav2_type == 0 then
			tuned_nav2_id = ""
			B738DR_tuned_nav2_type = 0
			B738DR_vor_dme_id2 = 0
			if is_timer_scheduled(nav2_decoding) == true then
				stop_timer(nav2_decoding)
			end
			B738DR_tuned_nav2_decode = 1
		elseif simDR_nav2_horz_dsp == 0 then
			if simDR_nav2_has_dme == 0 then
				tuned_nav2_id = ""
				B738DR_tuned_nav2_type = 0
				B738DR_vor_dme_id2 = 0
				if is_timer_scheduled(nav2_decoding) == true then
					stop_timer(nav2_decoding)
				end
				B738DR_tuned_nav2_decode = 1
			elseif vor_or_ils(B738DR_nav2_hz) == 0 then
				if is_timer_scheduled(nav2_decoding) == true then
					stop_timer(nav2_decoding)
				end
				run_after_time(nav2_decoding, 6.5)
				B738DR_tuned_nav2_decode = 1
				
				tuned_nav2_id = simDR_nav2_nav_id
				B738DR_tuned_nav2_type = 1	-- VOR, DME
				B738DR_vor_dme_id2 = 1
			else
				ils_idx = is_ils(simDR_nav2_nav_id, B738DR_nav2_hz)
				if ils_idx == 0 then
					-- if is_timer_scheduled(nav2_decoding) == true then
						-- stop_timer(nav2_decoding)
					-- end
					-- run_after_time(nav2_decoding, 6.5)
					-- B738DR_tuned_nav2_decode = 1
					
					-- tuned_nav2_id = simDR_nav2_nav_id
					-- B738DR_tuned_nav2_type = 1	-- VOR, DME
					-- B738DR_vor_dme_id2 = 1
					tuned_nav2_id = ""
					B738DR_tuned_nav2_type = 0
					B738DR_vor_dme_id2 = 0
					if is_timer_scheduled(nav2_decoding) == true then
						stop_timer(nav2_decoding)
					end
					B738DR_tuned_nav2_decode = 1
				elseif simDR_nav2_dme < 38 then	--27.2
					-- ILS range
					if is_timer_scheduled(nav2_decoding) == true then
						stop_timer(nav2_decoding)
					end
					run_after_time(nav2_decoding, 6.5)
					B738DR_tuned_nav2_decode = 1
					tuned_nav2_id = simDR_nav2_nav_id
					if ils_nav[ils_idx][9] ~= 1 then
						B738DR_tuned_nav2_type = 3	-- ILS
					else
						B738DR_tuned_nav2_type = 2	-- LOC only
					end
					if ils_nav[ils_idx][10] < 0 then
						B738DR_tuned_nav2_crs = (ils_nav[ils_idx][5] + mag_variation_deg(ils_nav[ils_idx][2], ils_nav[ils_idx][3]) + 360) % 360
					else
						B738DR_tuned_nav2_crs = ils_nav[ils_idx][10]
					end
					B738DR_vor_dme_id2 = 0
				else
					tuned_nav2_id = ""
					B738DR_tuned_nav2_type = 0
					B738DR_vor_dme_id2 = 0
					if is_timer_scheduled(nav2_decoding) == true then
						stop_timer(nav2_decoding)
					end
					B738DR_tuned_nav2_decode = 1
				end
			end
		elseif simDR_nav2_nav_id == nil then
			tuned_nav2_id = ""
			B738DR_tuned_nav2_type = 0
			B738DR_vor_dme_id2 = 0
			if is_timer_scheduled(nav2_decoding) == true then
				stop_timer(nav2_decoding)
			end
			B738DR_tuned_nav2_decode = 1
		elseif simDR_nav2_nav_id == "" then
			tuned_nav2_id = ""
			B738DR_tuned_nav2_type = 0
			B738DR_vor_dme_id2 = 0
			if is_timer_scheduled(nav2_decoding) == true then
				stop_timer(nav2_decoding)
			end
			B738DR_tuned_nav2_decode = 1
		elseif simDR_nav2_nav_id ~= tuned_nav2_id then
			if is_timer_scheduled(nav2_decoding) == true then
				stop_timer(nav2_decoding)
			end
			run_after_time(nav2_decoding, 6.5)
			B738DR_tuned_nav2_decode = 1
			
			tuned_nav2_id = simDR_nav2_nav_id
			if vor_or_ils(B738DR_nav2_hz) == 0 then
				B738DR_tuned_nav2_type = 1	-- VOR, DME
				B738DR_vor_dme_id2 = 1
			else
				ils_idx = is_ils(tuned_nav2_id, B738DR_nav2_hz)
				if ils_idx ~= 0 then
					if ils_nav[ils_idx][9] ~= 1 then
						B738DR_tuned_nav2_type = 3	-- ILS
					else
						B738DR_tuned_nav2_type = 2	-- LOC only
					end
					--B738DR_tuned_nav2_crs = (ils_nav[ils_idx][5] + simDR_mag_variation + 360) % 360
					if ils_nav[ils_idx][10] < 0 then
						B738DR_tuned_nav2_crs = (ils_nav[ils_idx][5] + mag_variation_deg(ils_nav[ils_idx][2], ils_nav[ils_idx][3]) + 360) % 360
					else
						B738DR_tuned_nav2_crs = ils_nav[ils_idx][10]
					end
					B738DR_vor_dme_id2 = 0
				else
					tuned_nav2_id = ""
					B738DR_tuned_nav2_type = 0
					B738DR_vor_dme_id2 = 0
					if is_timer_scheduled(nav2_decoding) == true then
						stop_timer(nav2_decoding)
					end
					B738DR_tuned_nav2_decode = 1
				end
			end
		end
	else
		tuned_nav2_id = ""
		B738DR_tuned_nav2_type = 0
		B738DR_vor_dme_id2 = 0
		if is_timer_scheduled(nav2_decoding) == true then
			stop_timer(nav2_decoding)
		end
		B738DR_tuned_nav2_decode = 1
	end
	
	if tuned_nav2_id == "" then
		B738DR_tuned_nav2_no_id = 1
	else
		B738DR_tuned_nav2_no_id = 0
	end
	
	if B738DR_tuned_nav1_type == 0 or B738DR_tuned_nav1_type >= 2 then
		B738DR_vor_dme1 = 0
		if B738DR_mmr_cpt_act_mode == 0 then
			if vor_or_ils(B738DR_nav1_hz) == 0 then
				B738DR_vor1_id_show = 1
			else
				B738DR_vor1_id_show = 0
			end
		else
			B738DR_vor1_id_show = 0
		end
	else
		if simDR_nav1_has_dme == 0 then
			B738DR_vor_dme1 = 0
		else
			B738DR_vor_dme1 = 1
		end
		B738DR_vor1_id_show = 1
	end
	
	if B738DR_tuned_nav2_type == 0 or B738DR_tuned_nav2_type >= 2 then
		B738DR_vor_dme2 = 0
		if B738DR_mmr_fo_act_mode == 0 then
			if vor_or_ils(B738DR_nav2_hz) == 0 then
				B738DR_vor2_id_show = 1
			else
				B738DR_vor2_id_show = 0
			end
		else
			B738DR_vor2_id_show = 0
		end
	else
		if simDR_nav2_has_dme == 0 then
			B738DR_vor_dme2 = 0
		else
			B738DR_vor_dme2 = 1
		end
		B738DR_vor2_id_show = 1
	end
	
	-- VORs FREQ
	local vor_adf_freq = ""
	if B738DR_vor1_id_show == 0 then
		B738DR_vor_freq1 = ""
	else
		if simDR_nav1_nav_id == "" then
			vor_adf_freq = string.format("%05d", simDR_nav1_freq)
			B738DR_vor_freq1 = string.sub(vor_adf_freq, 1, 3) .. "." .. string.sub(vor_adf_freq, 4, 5)
		else
			B738DR_vor_freq1 = ""
		end
	end
	if B738DR_vor2_id_show == 0 then
		B738DR_vor_freq2 = ""
	else
		if simDR_nav2_nav_id == "" then
			vor_adf_freq = string.format("%05d", simDR_nav2_freq)
			B738DR_vor_freq2 = string.sub(vor_adf_freq, 1, 3) .. "." .. string.sub(vor_adf_freq, 4, 5)
		else
			B738DR_vor_freq2 = ""
		end
	end
	
	-- ADFs FREQ
	if simDR_adf1_nav_id == nil then
		--B738DR_adf_freq1 = string.format("%04d", simDR_adf1_freq) .. ".0"
		B738DR_adf_freq1 = tostring(simDR_adf1_freq) .. ".0"
	elseif simDR_adf1_nav_id == "" then
		--B738DR_adf_freq1 = string.format("%04d", simDR_adf1_freq) .. ".0"
		B738DR_adf_freq1 = tostring(simDR_adf1_freq) .. ".0"
	else
		B738DR_adf_freq1 = ""
	end
	
	if simDR_adf2_nav_id == nil then
		--B738DR_adf_freq2 = string.format("%04d", simDR_adf2_freq) .. ".0"
		B738DR_adf_freq2 = tostring(simDR_adf2_freq) .. ".0"
	elseif simDR_adf2_nav_id == "" then
		--B738DR_adf_freq2 = string.format("%04d", simDR_adf2_freq) .. ".0"
		B738DR_adf_freq2 = tostring(simDR_adf2_freq) .. ".0"
	else
		B738DR_adf_freq2 = ""
	end
	------
	
	if B738DR_capt_map_mode == 2 then
		
		-- CAPTAIN VOR1
		--if (simDR_nav1_type == 4 or simDR_nav1_type == 1024) and B738DR_efis_vor1_capt_pos == 1 then	-- VOR select
		if B738DR_tuned_nav1_type == 1 and B738DR_efis_vor1_capt_pos == 1 and B738DR_mmr_cpt_act_mode == 0 then	-- VOR select
			if simDR_nav1_nav_id == nil or simDR_nav1_dme == nil or simDR_nav1_has_dme == 0 then
				B738DR_vor1_show = 0
				nav1_nav_id_old = ""
			else
				-- find VOR1 id
				if nav1_nav_id_old ~= simDR_nav1_nav_id then
					nav1_lat = 0
					nav1_lon = 0
					nav1_nav_id_old = simDR_nav1_nav_id
					find_navaid(nav1_nav_id_old, "", 0, "", rte12_act)
					if navaid_list_n ~= 0 then
						for fff = 1, navaid_list_n do
							if navaid_list[fff][1] == 1 or navaid_list[fff][1] == 2 or navaid_list[fff][1] == 5 then
								nav1_lat = navaid_list[fff][2]
								nav1_lon = navaid_list[fff][3]
								break
							end
						end
					end
				end
				
				str_len = string.len(nav1_nav_id_old)
				B738DR_vor1_sel_id = ""
				if str_len < 4 then
					B738DR_vor1_sel_id = " "
				end
				B738DR_vor1_sel_id = B738DR_vor1_sel_id .. nav1_nav_id_old
				vor_crs = simDR_nav1_obs_pilot
				vor_bcrs = (vor_crs + 180) % 360
				if vor_crs < 10 then
					if vor_crs < 0 then
						B738DR_vor1_sel_crs = "000"
					else
						B738DR_vor1_sel_crs = "00" .. string.format("%1d", vor_crs)
					end
				elseif vor_crs < 100 then
					B738DR_vor1_sel_crs = "0" .. string.format("%2d", vor_crs)
				else
					B738DR_vor1_sel_crs = string.format("%3d", vor_crs)
				end
				if vor_bcrs < 10 then
					if vor_bcrs < 0 then
						B738DR_vor1_sel_bcrs = "000"
					else
						B738DR_vor1_sel_bcrs = "00" .. string.format("%1d", vor_bcrs)
					end
				elseif vor_bcrs < 100 then
					B738DR_vor1_sel_bcrs = "0" .. string.format("%2d", vor_bcrs)
				else
					B738DR_vor1_sel_bcrs = string.format("%3d", vor_bcrs)
				end
				
				--------------------
				nav_lat2 = nav1_lat
				nav_lon2 = nav1_lon
				--------------------
				
				if B738DR_track_up == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					if B738DR_track_up_active == 0 then
						mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
					else
						mag_hdg = ndx_mag_hdg - simDR_mag_variation
					end
				end
				
				ils_dis = nd_calc_dist2(nav_lat, nav_lon, nav_lat2, nav_lon2)
				
				ils_hdg = nd_calc_brg(nav_lat, nav_lon, nav_lat2, nav_lon2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
				
				if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
					-- right
					ils_on_off = 1
					delta_ils_hdg = 90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
					-- left
					ils_on_off = 1
					delta_ils_hdg = 90 + delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg >= 90 then
					-- right back
					ils_on_off = 1
					delta_ils_hdg = delta_ils_hdg - 90
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg <= -90 then
					-- left back
					ils_on_off = 1
					delta_ils_hdg = -90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				end
				
				if B738DR_efis_map_range_capt == 0 then	-- 5 NM
					ils_zoom = 2
				elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
					ils_zoom = 1
				elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
					ils_zoom = 0.5
				elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
					ils_zoom = 0.25
				elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
					ils_zoom = 0.125
				elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
					ils_zoom = 0.0625
				elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
					ils_zoom = 0.03125
				else
					-- ils_on_off = 0
					ils_zoom = 0.015625
				end
				
				if rte_plan_mode == 1 then
					ils_zoom = ils_zoom * 0.57
				end
				
				ils_x = ils_x * ils_zoom		-- zoom
				ils_y = ils_y * ils_zoom		-- zoom
				
				if B738DR_capt_vsd_map_mode == 1 then
					ils_y = ils_y + 5.1	-- adjust center
				elseif B738DR_capt_exp_map_mode == 0 then
					ils_y = ils_y + 4.1	-- adjust center
				end
				
				-- if ils_y > 14 or ils_y < -5 then		-- 7.7 / -1
					-- ils_on_off = 0
				-- end
				-- if ils_x < -11 or ils_x > 11 then		-- -6.0 / 6.0
					-- ils_on_off = 0
				-- end
					
				-- if ils_on_off == 1 then
					-- rotate
					if B738DR_track_up == 0 then
						vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
					else
						if B738DR_track_up_active == 0 then
							vor_hdg = simDR_ahars_mag_hdg
						else
							vor_hdg = simDR_mag_hdg
						end
					end
					
					vor_bearing = simDR_nav1_bearing
					vor_distance = simDR_nav1_dme
					
					relative_brg = (vor_bearing - vor_hdg + 360) % 360
					if relative_brg > 180 then
						relative_brg = relative_brg - 360
					end
					vor_hdg = (vor_crs - vor_hdg + 360) % 360
					vor_hdg = (90 + vor_hdg) % 360
					B738DR_vor1_sel_rotate = vor_hdg
					
					--position
					B738DR_vor1_sel_x = ils_x
					B738DR_vor1_sel_y = ils_y
					B738DR_vor1_show = 1
					if simDR_vor1_capt == 2 and B738DR_vor_radial ~= 0 then
						B738DR_vor1_line_show = 1
					else
						B738DR_vor1_line_show = 0
					end
					
				-- else
					-- B738DR_vor1_show = 0
				-- end
			end
		else
			B738DR_vor1_show = 0
			nav1_nav_id_old = ""
		end
		
		-- CAPTAIN VOR2
		-- if simDR_nav2_nav_id ~= nil then
			-- if B738DR_vor1_show == 1 and simDR_nav1_nav_id == simDR_nav2_nav_id and simDR_nav1_obs_pilot == simDR_nav1_obs_copilot then
				-- vor2_sel_disable = 1
			-- end
		-- end
		if B738DR_efis_vor2_capt_pos ~= 1 then
			vor2_sel_disable = 1
		end
		--if (simDR_nav2_type == 4 or simDR_nav2_type == 1024) and vor2_sel_disable == 0 then	-- VOR select
		if B738DR_tuned_nav2_type == 1 and vor2_sel_disable == 0 and B738DR_mmr_fo_act_mode == 0 then	-- VOR select
			if simDR_nav2_nav_id == nil or simDR_nav2_dme == nil or simDR_nav2_has_dme == 0 then
				B738DR_vor2_show = 0
			else
				
				-- find VOR2 id
				if nav2_nav_id_old ~= simDR_nav2_nav_id then
					nav2_lat = 0
					nav2_lon = 0
					nav2_nav_id_old = simDR_nav2_nav_id
					find_navaid(nav2_nav_id_old, "", 0, "", rte12_act)
					if navaid_list_n ~= 0 then
						for fff = 1, navaid_list_n do
							if navaid_list[fff][1] == 1 or navaid_list[fff][1] == 2 or navaid_list[fff][1] == 5 then
								nav2_lat = navaid_list[fff][2]
								nav2_lon = navaid_list[fff][3]
								break
							end
						end
					end
				end
				
				if B738DR_vor1_show == 1 and simDR_nav1_nav_id == simDR_nav2_nav_id then
					B738DR_vor2_sel_id = ""
				else
					str_len = string.len(nav2_nav_id_old)
					B738DR_vor2_sel_id = ""
					if str_len < 4 then
						B738DR_vor2_sel_id = " "
					end
					B738DR_vor2_sel_id = B738DR_vor2_sel_id .. nav2_nav_id_old
				end
				vor_crs = simDR_nav2_obs_copilot	--simDR_nav1_obs_pilot
				vor_bcrs = (vor_crs + 180) % 360
				if vor_crs < 10 then
					if vor_crs < 0 then
						B738DR_vor2_sel_crs = "000"
					else
						B738DR_vor2_sel_crs = "00" .. string.format("%1d", vor_crs)
					end
				elseif vor_crs < 100 then
					B738DR_vor2_sel_crs = "0" .. string.format("%2d", vor_crs)
				else
					B738DR_vor2_sel_crs = string.format("%3d", vor_crs)
				end
				if vor_bcrs < 10 then
					if vor_bcrs < 0 then
						B738DR_vor2_sel_bcrs = "000"
					else
						B738DR_vor2_sel_bcrs = "00" .. string.format("%1d", vor_bcrs)
					end
				elseif vor_bcrs < 100 then
					B738DR_vor2_sel_bcrs = "0" .. string.format("%2d", vor_bcrs)
				else
					B738DR_vor2_sel_bcrs = string.format("%3d", vor_bcrs)
				end
				
				
				--------------------
				nav_lat2 = nav2_lat
				nav_lon2 = nav2_lon
				--------------------
				
				if B738DR_track_up == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					if B738DR_track_up_active == 0 then
						mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
					else
						mag_hdg = ndx_mag_hdg - simDR_mag_variation
					end
				end
				
				ils_dis = nd_calc_dist2(nav_lat, nav_lon, nav_lat2, nav_lon2)
				
				ils_hdg = nd_calc_brg(nav_lat, nav_lon, nav_lat2, nav_lon2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
				
				if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
					-- right
					ils_on_off = 1
					delta_ils_hdg = 90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
					-- left
					ils_on_off = 1
					delta_ils_hdg = 90 + delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg >= 90 then
					-- right back
					ils_on_off = 1
					delta_ils_hdg = delta_ils_hdg - 90
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg <= -90 then
					-- left back
					ils_on_off = 1
					delta_ils_hdg = -90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				end
				
				if B738DR_efis_map_range_capt == 0 then	-- 5 NM
					ils_zoom = 2
				elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
					ils_zoom = 1
				elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
					ils_zoom = 0.5
				elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
					ils_zoom = 0.25
				elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
					ils_zoom = 0.125
				elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
					ils_zoom = 0.0625
				elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
					ils_zoom = 0.03125
				else
					-- ils_on_off = 0
					ils_zoom = 0.015625
				end
				
				if rte_plan_mode == 1 then
					ils_zoom = ils_zoom * 0.57
				end
				
				ils_x = ils_x * ils_zoom		-- zoom
				ils_y = ils_y * ils_zoom		-- zoom
				
				if B738DR_capt_vsd_map_mode == 1 then
					ils_y = ils_y + 5.1	-- adjust center
				elseif B738DR_capt_exp_map_mode == 0 then
					ils_y = ils_y + 4.1	-- adjust center
				end
				
				-- if ils_y > 14 or ils_y < -5 then		-- 7.7 / -1
					-- ils_on_off = 0
				-- end
				-- if ils_x < -11 or ils_x > 11 then		-- -6.0 / 6.0
					-- ils_on_off = 0
				-- end
					
				-- if ils_on_off == 1 then
					-- rotate
					if B738DR_track_up == 0 then
						vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
					else
						if B738DR_track_up_active == 0 then
							vor_hdg = simDR_ahars_mag_hdg
						else
							vor_hdg = simDR_mag_hdg
						end
					end
					
					vor_bearing = simDR_nav2_bearing
					vor_distance = simDR_nav2_dme
					
					relative_brg = (vor_bearing - vor_hdg + 360) % 360
					if relative_brg > 180 then
						relative_brg = relative_brg - 360
					end
					vor_hdg = (vor_crs - vor_hdg + 360) % 360
					vor_hdg = (90 + vor_hdg) % 360
					B738DR_vor2_sel_rotate = vor_hdg
					
					--position
					B738DR_vor2_sel_x = ils_x
					B738DR_vor2_sel_y = ils_y
					B738DR_vor2_show = 1
					if simDR_vor2_capt == 2 and B738DR_vor_radial ~= 0 then
						B738DR_vor2_line_show = 1
					else
						B738DR_vor2_line_show = 0
					end
					
				-- else
					-- B738DR_vor2_show = 0
				-- end
			end
		else
			B738DR_vor2_show = 0
			nav2_nav_id_old = ""
		end
	else
		B738DR_vor1_show = 0
		nav1_nav_id_old = ""
		B738DR_vor2_show = 0
		
	end
	
	
	if B738DR_capt_map_mode < 2 then
		vor_hdg =  simDR_ahars_mag_hdg
	elseif B738DR_capt_map_mode == 2 then
		if B738DR_track_up == 0 then
			vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
		else
			if B738DR_track_up_active == 0 then
				vor_hdg = simDR_ahars_mag_hdg
			else
				vor_hdg = simDR_mag_hdg
			end
		end
	elseif B738DR_capt_map_mode == 3 then
		vor_hdg = 0
	end
	
	B738DR_vor1_arrow = simDR_nav1_bearing - vor_hdg
	B738DR_vor2_arrow = simDR_nav2_bearing - vor_hdg
	
	B738DR_adf1_arrow = simDR_adf1_bearing - vor_hdg
	B738DR_adf2_arrow = simDR_adf2_bearing - vor_hdg
	
	B738DR_wind_arrow = (simDR_wind_hdg + 180 - vor_hdg) % 360
	
	if B738DR_fo_map_mode == 2 then
	
		-- FIRST OFFICER VOR1
		str_len = 0
		vor_crs = 0
		vor_bcrs = 0
		vor_hdg = 0
		vor_bearing = 0
		vor_distance = 0
		vor_bear_l = 0
		vor_bear_r = 0
		str_crs = ""
		str_bcrs = ""
		vor_angle = 0
		efis_zoom = 0
		out_of_range = 0
		vor_x = 0
		vor_y = 0
		vor_quadr = 0
		vor2_sel_disable = 0
		
		--if (simDR_nav1_type == 4 or simDR_nav1_type == 1024) and B738DR_efis_vor1_fo_pos == 1 then	-- VOR select
		if B738DR_tuned_nav1_type == 1 and B738DR_efis_vor1_fo_pos == 1 and B738DR_mmr_cpt_act_mode == 0 then 	-- VOR select
			if simDR_nav1_nav_id == nil or simDR_nav1_dme == nil or simDR_nav1_has_dme == 0 then
				B738DR_vor1_copilot_show = 0
			else
				-- find VOR1 id
				if nav1_nav_id_fo_old ~= simDR_nav1_nav_id then
					nav1_lat_fo = 0
					nav1_lon_fo = 0
					nav1_nav_id_fo_old = simDR_nav1_nav_id
					find_navaid(nav1_nav_id_fo_old, "", 0, "", rte12_act)
					if navaid_list_n ~= 0 then
						for fff = 1, navaid_list_n do
							if navaid_list[fff][1] == 1 or navaid_list[fff][1] == 2 or navaid_list[fff][1] == 5 then
								nav1_lat_fo = navaid_list[fff][2]
								nav1_lon_fo = navaid_list[fff][3]
								break
							end
						end
					end
				end
				
				str_len = string.len(nav1_nav_id_fo_old)
				B738DR_vor1_sel_id_fo = ""
				if str_len < 4 then
					B738DR_vor1_sel_id_fo = " "
				end
				B738DR_vor1_sel_id_fo = B738DR_vor1_sel_id_fo .. nav1_nav_id_fo_old
				vor_crs = simDR_nav1_obs_pilot
				vor_bcrs = (vor_crs + 180) % 360
				if vor_crs < 10 then
					if vor_crs < 0 then
						B738DR_vor1_sel_crs_fo = "000"
					else
						B738DR_vor1_sel_crs_fo = "00" .. string.format("%1d", vor_crs)
					end
				elseif vor_crs < 100 then
					B738DR_vor1_sel_crs_fo = "0" .. string.format("%2d", vor_crs)
				else
					B738DR_vor1_sel_crs_fo = string.format("%3d", vor_crs)
				end
				if vor_bcrs < 10 then
					if vor_bcrs < 0 then
						B738DR_vor1_sel_bcrs_fo = "000"
					else
						B738DR_vor1_sel_bcrs_fo = "00" .. string.format("%1d", vor_bcrs)
					end
				elseif vor_bcrs < 100 then
					B738DR_vor1_sel_bcrs_fo = "0" .. string.format("%2d", vor_bcrs)
				else
					B738DR_vor1_sel_bcrs_fo = string.format("%3d", vor_bcrs)
				end
				
				-- if B738DR_track_up == 0 then
					-- vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
				-- else
					-- if B738DR_track_up_active == 0 then
						-- vor_hdg = simDR_ahars_mag_hdg
					-- else
						-- vor_hdg = simDR_mag_hdg
					-- end
					-- -- vor_hdg = simDR_mag_hdg
				-- end
				
				
				--------------------
				nav_lat2 = nav1_lat_fo
				nav_lon2 = nav1_lon_fo
				--------------------
				
				if B738DR_track_up == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					if B738DR_track_up_active == 0 then
						mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
					else
						mag_hdg = ndx_mag_hdg - simDR_mag_variation
					end
				end
				
				ils_dis = nd_calc_dist2(nav_lat, nav_lon, nav_lat2, nav_lon2)
				
				ils_hdg = nd_calc_brg(nav_lat, nav_lon, nav_lat2, nav_lon2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
				
				if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
					-- right
					ils_on_off = 1
					delta_ils_hdg = 90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
					-- left
					ils_on_off = 1
					delta_ils_hdg = 90 + delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg >= 90 then
					-- right back
					ils_on_off = 1
					delta_ils_hdg = delta_ils_hdg - 90
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg <= -90 then
					-- left back
					ils_on_off = 1
					delta_ils_hdg = -90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				end
				
				if B738DR_efis_map_range_fo == 0 then	-- 5 NM
					ils_zoom = 2
				elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
					ils_zoom = 1
				elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
					ils_zoom = 0.5
				elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
					ils_zoom = 0.25
				elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
					ils_zoom = 0.125
				elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
					ils_zoom = 0.0625
				elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
					ils_zoom = 0.03125
				else
					-- ils_on_off = 0
					ils_zoom = 0.015625
				end
				
				if rte_plan_mode == 1 then
					ils_zoom = ils_zoom * 0.57
				end
				
				ils_x = ils_x * ils_zoom		-- zoom
				ils_y = ils_y * ils_zoom		-- zoom
				
				if B738DR_fo_vsd_map_mode == 1 then
					ils_y = ils_y + 5.1	-- adjust center
				elseif B738DR_fo_exp_map_mode == 0 then
					ils_y = ils_y + 4.1	-- adjust center
				end
				
				-- if ils_y > 14 or ils_y < -5 then		-- 7.7 / -1
					-- ils_on_off = 0
				-- end
				-- if ils_x < -11 or ils_x > 11 then		-- -6.0 / 6.0
					-- ils_on_off = 0
				-- end
					
				-- if ils_on_off == 1 then
					-- rotate
					if B738DR_track_up == 0 then
						vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
					else
						if B738DR_track_up_active == 0 then
							vor_hdg = simDR_ahars_mag_hdg
						else
							vor_hdg = simDR_mag_hdg
						end
					end
					
					vor_bearing = simDR_nav1_bearing
					vor_distance = simDR_nav1_dme
					
					relative_brg = (vor_bearing - vor_hdg + 360) % 360
					if relative_brg > 180 then
						relative_brg = relative_brg - 360
					end
					vor_hdg = (vor_crs - vor_hdg + 360) % 360
					vor_hdg = (90 + vor_hdg) % 360
					B738DR_vor1_sel_rotate_fo = vor_hdg
					
					--position
					B738DR_vor1_sel_x_fo = ils_x
					B738DR_vor1_sel_y_fo = ils_y
					B738DR_vor1_copilot_show = 1
					if simDR_vor1_fo == 2 and B738DR_vor_radial ~= 0 then
						B738DR_vor1_line_copilot_show = 1
					else
						B738DR_vor1_line_copilot_show = 0
					end
					
				-- else
					-- B738DR_vor1_copilot_show = 0
				-- end
			end
		else
			B738DR_vor1_copilot_show = 0
			nav1_nav_id_fo_old = ""
		end
		
		-- FIRST OFFICER VOR2
		-- if simDR_nav2_nav_id ~= nil then
			-- if B738DR_vor1_copilot_show == 1 and simDR_nav1_nav_id == simDR_nav2_nav_id and simDR_nav1_obs_pilot == simDR_nav1_obs_copilot then
				-- vor2_sel_disable = 1
			-- end
		-- end
		if B738DR_efis_vor2_fo_pos ~= 1 then
			vor2_sel_disable = 1
		end
		
		--if (simDR_nav2_type == 4 or simDR_nav2_type == 1024) and vor2_sel_disable == 0 then	-- VOR select
		if B738DR_tuned_nav2_type == 1 and vor2_sel_disable == 0 and B738DR_mmr_fo_act_mode == 0 then	-- VOR select
			if simDR_nav2_nav_id == nil or simDR_nav2_dme == nil or simDR_nav2_has_dme == 0 then
				B738DR_vor2_copilot_show = 0
			else
				-- find VOR2 id
				if nav2_nav_id_fo_old ~= simDR_nav2_nav_id then
					nav2_lat_fo = 0
					nav2_lon_fo = 0
					nav2_nav_id_fo_old = simDR_nav2_nav_id
					find_navaid(nav2_nav_id_fo_old, "", 0, "", rte12_act)
					if navaid_list_n ~= 0 then
						for fff = 1, navaid_list_n do
							if navaid_list[fff][1] == 1 or navaid_list[fff][1] == 2 or navaid_list[fff][1] == 5 then
								nav2_lat_fo = navaid_list[fff][2]
								nav2_lon_fo = navaid_list[fff][3]
								break
							end
						end
					end
				end
				
				if B738DR_vor1_copilot_show == 1 and simDR_nav1_nav_id == simDR_nav2_nav_id then
					B738DR_vor2_sel_id_fo = ""
				else
					str_len = string.len(simDR_nav2_nav_id)
					B738DR_vor2_sel_id_fo = ""
					if str_len < 4 then
						B738DR_vor2_sel_id_fo = " "
					end
					B738DR_vor2_sel_id_fo = B738DR_vor2_sel_id_fo .. simDR_nav2_nav_id
				end
				vor_crs = simDR_nav2_obs_copilot	--simDR_nav1_obs_pilot
				vor_bcrs = (vor_crs + 180) % 360
				if vor_crs < 10 then
					if vor_crs < 0 then
						B738DR_vor2_sel_crs_fo = "000"
					else
						B738DR_vor2_sel_crs_fo = "00" .. string.format("%1d", vor_crs)
					end
				elseif vor_crs < 100 then
					B738DR_vor2_sel_crs_fo = "0" .. string.format("%2d", vor_crs)
				else
					B738DR_vor2_sel_crs_fo = string.format("%3d", vor_crs)
				end
				if vor_bcrs < 10 then
					if vor_bcrs < 0 then
						B738DR_vor2_sel_bcrs_fo = "000"
					else
						B738DR_vor2_sel_bcrs_fo = "00" .. string.format("%1d", vor_bcrs)
					end
				elseif vor_bcrs < 100 then
					B738DR_vor2_sel_bcrs_fo = "0" .. string.format("%2d", vor_bcrs)
				else
					B738DR_vor2_sel_bcrs_fo = string.format("%3d", vor_bcrs)
				end
				
				
				
				--------------------
				nav_lat2 = nav2_lat_fo
				nav_lon2 = nav2_lon_fo
				--------------------
				
				if B738DR_track_up == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					if B738DR_track_up_active == 0 then
						mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
					else
						mag_hdg = ndx_mag_hdg - simDR_mag_variation
					end
				end
				
				ils_dis = nd_calc_dist2(nav_lat, nav_lon, nav_lat2, nav_lon2)
				
				ils_hdg = nd_calc_brg(nav_lat, nav_lon, nav_lat2, nav_lon2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
				
				if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
					-- right
					ils_on_off = 1
					delta_ils_hdg = 90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
					-- left
					ils_on_off = 1
					delta_ils_hdg = 90 + delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg >= 90 then
					-- right back
					ils_on_off = 1
					delta_ils_hdg = delta_ils_hdg - 90
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg <= -90 then
					-- left back
					ils_on_off = 1
					delta_ils_hdg = -90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				end
				
				if B738DR_efis_map_range_fo == 0 then	-- 5 NM
					ils_zoom = 2
				elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
					ils_zoom = 1
				elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
					ils_zoom = 0.5
				elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
					ils_zoom = 0.25
				elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
					ils_zoom = 0.125
				elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
					ils_zoom = 0.0625
				elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
					ils_zoom = 0.03125
				else
					-- ils_on_off = 0
					ils_zoom = 0.015625
				end
				
				if rte_plan_mode == 1 then
					ils_zoom = ils_zoom * 0.57
				end
				
				ils_x = ils_x * ils_zoom		-- zoom
				ils_y = ils_y * ils_zoom		-- zoom
				
				if B738DR_fo_vsd_map_mode == 1 then
					ils_y = ils_y + 5.1	-- adjust center
				elseif B738DR_fo_exp_map_mode == 0 then
					ils_y = ils_y + 4.1	-- adjust center
				end
				
				-- if ils_y > 14 or ils_y < -5 then		-- 7.7 / -1
					-- ils_on_off = 0
				-- end
				-- if ils_x < -11 or ils_x > 11 then		-- -6.0 / 6.0
					-- ils_on_off = 0
				-- end
				
				-- if ils_on_off == 1 then
					-- rotate
					if B738DR_track_up == 0 then
						vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
					else
						if B738DR_track_up_active == 0 then
							vor_hdg = simDR_ahars_mag_hdg
						else
							vor_hdg = simDR_mag_hdg
						end
					end
					
					vor_bearing = simDR_nav2_bearing
					vor_distance = simDR_nav2_dme
					
					relative_brg = (vor_bearing - vor_hdg + 360) % 360
					if relative_brg > 180 then
						relative_brg = relative_brg - 360
					end
					vor_hdg = (vor_crs - vor_hdg + 360) % 360
					vor_hdg = (90 + vor_hdg) % 360
					B738DR_vor2_sel_rotate_fo = vor_hdg
					
					--position
					B738DR_vor2_sel_x_fo = ils_x
					B738DR_vor2_sel_y_fo = ils_y
					B738DR_vor2_copilot_show = 1
					if simDR_vor2_fo == 2 and B738DR_vor_radial ~= 0 then
						B738DR_vor2_line_copilot_show = 1
					else
						B738DR_vor2_line_copilot_show = 0
					end
				-- else
					-- B738DR_vor2_copilot_show = 0
				-- end
				
			end
		else
			B738DR_vor2_copilot_show = 0
			nav2_nav_id_fo_old = ""
		end
	else
		B738DR_vor1_copilot_show = 0
		B738DR_vor2_copilot_show = 0
		nav1_nav_id_fo_old = ""
		nav2_nav_id_fo_old = ""
	end
	
	
	if B738DR_fo_map_mode < 2 then
		vor_hdg = simDR_ahars_mag_hdg
	elseif B738DR_fo_map_mode == 2 then
		if B738DR_track_up == 0 then
			vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
		else
			if B738DR_track_up_active == 0 then
				vor_hdg = simDR_ahars_mag_hdg
			else
				vor_hdg = simDR_mag_hdg
			end
		end
	elseif B738DR_fo_map_mode == 3 then
		vor_hdg = 0
	end
	
	B738DR_vor1_arrow_fo = simDR_nav1_bearing - vor_hdg
	B738DR_vor2_arrow_fo = simDR_nav2_bearing - vor_hdg
	
	B738DR_adf1_arrow_fo = simDR_adf1_bearing - vor_hdg
	B738DR_adf2_arrow_fo = simDR_adf2_bearing - vor_hdg
	
	B738DR_wind_arrow_fo = (simDR_wind_hdg + 180 - vor_hdg) % 360
end

function B738_displ_rnw2(in_mode)

	local ils_lat2 = 0
	local ils_lon2 = 0
	local ils_lat = ndx_lat
	local ils_lon = ndx_lon
	local mag_hdg = 0
	local delta_ils_hdg = 0
	local ils_hdg = 0
	local ils_on_off = 0
	local ils_x = 0
	local ils_y = 0
	local ils_dis = 0
	local ils_zoom = 0
	local ils_disable = 0
	local rte_plan_mode = 0
	
	local ils_rnw_lat = des_runway_lat_nd
	local ils_rnw_lon = des_runway_lon_nd
	local ils_rnw_crs = (des_runway_crs_nd - mag_variation_deg(des_runway_lat, des_runway_lon) + 360) % 360
	local ils_rnw_num = des_rnw_nd
	
	local use_legs_num2 = 0
	local use_ref_rwy = ""
	local use_ref_rwy2 = ""
	local use_ref_rwy3 = ""
	local use_ref_icao = ""
	local use_des_icao = ""
	local use_ref_icao_x = ""
	local use_des_icao_x = ""
	local use_legs_step = legs_step
	local use_des_rnw_nd_mod = ""
	
	local use_rte12_sel = rte12_sel
	local use_map_mode = B738DR_capt_map_mode
	local use_efis_map_range = B738DR_efis_map_range_capt
	local use_vsd_map_mode = B738DR_capt_vsd_map_mode
	local use_exp_map_mode = B738DR_capt_exp_map_mode
	local use_irs_nd_allign = B738DR_irs_nd_allign
	
	if in_mode ~= 0 then
		use_rte12_sel = rte12_sel_fo
		use_map_mode = B738DR_fo_map_mode
		use_efis_map_range = B738DR_efis_map_range_fo
		use_vsd_map_mode = B738DR_fo_vsd_map_mode
		use_exp_map_mode = B738DR_fo_exp_map_mode
		use_irs_nd_allign = B738DR_irs_nd_allign2
		use_legs_step = legs_step2
	end
	
	if rte12_act == 0 then
		use_ref_icao = ref_icao
		use_des_icao = des_icao
	elseif rte12_act == 1 then
		use_ref_icao = ref_icao7
		use_des_icao = des_icao7
	end
	if use_rte12_sel == 0 then
		use_legs_num2 = legs_num2
		use_ref_rwy = ref_rwy
		use_ref_rwy2 = ref_rwy2
		use_ref_rwy3 = ref_rwy3
		use_ref_icao_x = ref_icao_x
		use_des_icao_x = des_icao_x
		use_des_rnw_nd_mod = des_rnw_nd_mod
	else
		use_legs_num2 = legs_num8
		use_ref_rwy = ref_rwy7
		use_ref_rwy2 = ref_rwy8
		use_ref_rwy3 = ref_rwy9
		use_ref_icao_x = ref_icao7_x
		use_des_icao_x = des_icao7_x
		use_des_rnw_nd_mod = des_rnw_nd_mod2
	end
	
	if legs_num > 1 then
	
		-- DES RUNWAY
		if use_map_mode == 3 then
			if use_rte12_sel == 0 then
				if use_legs_step > legs_num2 then
					use_legs_step = legs_num2
				end
				
				if legs_num2 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs2(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs2(use_legs_step)
				end
			else
				if use_legs_step > legs_num8 then
					use_legs_step = legs_num8
				end
				
				if legs_num8 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs8(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs8(use_legs_step)
				end
			end
			mag_hdg = -simDR_mag_variation
			rte_plan_mode = 1
		elseif use_map_mode == 2 then
			if use_irs_nd_allign ~= 0 then
				ils_disable = 1
			elseif B738DR_track_up == 0 then
				mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
			else
				if B738DR_track_up_active == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					mag_hdg = ndx_mag_hdg - simDR_mag_variation
				end
			end
		else
			ils_disable = 1
		end
		
		if des_rnw_nd == "" then
			ils_disable = 1
		end
		
		if ils_disable == 0 then
			
			ils_dis = nd_calc_dist2(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			--ils_dis = nd_calc_dist_d(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			
			ils_hdg = nd_calc_brg(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			--ils_hdg = nd_calc_brg_d(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
			
			if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
				-- right
				ils_on_off = 1
				delta_ils_hdg = 90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
				-- left
				ils_on_off = 1
				delta_ils_hdg = 90 + delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg >= 90 then
				-- right back
				ils_on_off = 1
				delta_ils_hdg = delta_ils_hdg - 90
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg <= -90 then
				-- left back
				ils_on_off = 1
				delta_ils_hdg = -90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			end
			
			if use_efis_map_range == 0 then	-- 5 NM
				ils_zoom = 2
			elseif use_efis_map_range == 1 then	-- 10 NM
				ils_zoom = 1
			elseif use_efis_map_range == 2 then	-- 20 NM
				ils_zoom = 0.5
			elseif use_efis_map_range == 3 then	-- 40 NM
				ils_zoom = 0.25
			else
				ils_on_off = 0
			end
			
			if rte_plan_mode == 1 then
				ils_zoom = ils_zoom * 0.57
			end
			
			ils_x = ils_x * ils_zoom		-- zoom
			ils_y = ils_y * ils_zoom		-- zoom
			
			if use_map_mode == 3 then
				ils_y = ils_y + 4.1	-- adjust center
			elseif use_vsd_map_mode == 1 then
				ils_y = ils_y + 5.1	-- adjust center
			elseif use_exp_map_mode == 0 then
				ils_y = ils_y + 4.1	-- adjust center
			end
			
			if ils_y > 30 or ils_y < -30 then		-- 7.7 / -1
				ils_on_off = 0
			end
			if ils_x < -30 or ils_x > 30 then		-- -6.0 / 6.0
				ils_on_off = 0
			end
			
			if ils_on_off == 1 then
				-- rotate
				if use_map_mode == 3 then
					ils_hdg = ils_rnw_crs
				else
					if B738DR_track_up == 0 then
						ils_hdg = (ils_rnw_crs - simDR_ahars_mag_hdg + 360) % 360
					else
						if B738DR_track_up_active == 0 then
							ils_hdg = (ils_rnw_crs - simDR_ahars_mag_hdg + 360) % 360
						else
							ils_hdg = (ils_rnw_crs - simDR_mag_hdg + 360) % 360
						end
					end
				end
				ils_hdg = (90 + ils_hdg) % 360
				ils_hdg = ils_hdg + simDR_mag_variation
				
				if in_mode == 0 then
					B738DR_ils_rotate = ils_hdg
					
					B738DR_ils_x = ils_x
					B738DR_ils_y = ils_y
					B738DR_ils_runway = ils_rnw_num
					B738DR_ils_show = 1
				else
					B738DR_ils_fo_rotate = ils_hdg
					
					B738DR_ils_fo_x = ils_x
					B738DR_ils_fo_y = ils_y
					B738DR_ils_fo_runway = ils_rnw_num
					B738DR_ils_fo_show = 1
				end
			else
				if in_mode == 0 then
					B738DR_ils_show = 0
				else
					B738DR_ils_fo_show = 0
				end
			end
		else
			if in_mode == 0 then
				B738DR_ils_show = 0
			else
				B738DR_ils_fo_show = 0
			end
		end
		
		
		
		-- REF RUNWAY
		
		ils_disable = 0
		ils_on_off = 0
		if use_map_mode == 3 then
			if use_rte12_sel == 0 then
				if use_legs_step > legs_num2 then
					use_legs_step = legs_num2
				end
				
				if legs_num2 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs2(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs2(use_legs_step)
				end
			else
				if use_legs_step > legs_num8 then
					use_legs_step = legs_num8
				end
				
				if legs_num8 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs8(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs8(use_legs_step)
				end
			end
			mag_hdg = -simDR_mag_variation
		elseif use_map_mode == 2 then
			if use_irs_nd_allign ~= 0 then
				ils_disable = 1
			elseif B738DR_track_up == 0 then
				mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
			else
				if B738DR_track_up_active == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					mag_hdg = ndx_mag_hdg - simDR_mag_variation
				end
			end
		else
			ils_disable = 1
		end
		
		if use_ref_rwy == "-----"  then
			ils_disable = 1
		end
		
		if ils_disable == 0 and ref_runway_lenght > 0 then
			
			ils_dis = nd_calc_dist2(ils_lat, ils_lon, ref_runway_lat, ref_runway_lon)
			--ils_dis = nd_calc_dist_d(ils_lat, ils_lon, ref_runway_lat, ref_runway_lon)
			
			ils_hdg = nd_calc_brg(ils_lat, ils_lon, ref_runway_lat, ref_runway_lon)
			--ils_hdg = nd_calc_brg_d(ils_lat, ils_lon, ref_runway_lat, ref_runway_lon)
			delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
			
			if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
				-- right
				ils_on_off = 1
				delta_ils_hdg = 90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
				-- left
				ils_on_off = 1
				delta_ils_hdg = 90 + delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg >= 90 then
				-- right back
				ils_on_off = 1
				delta_ils_hdg = delta_ils_hdg - 90
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg <= -90 then
				-- left back
				ils_on_off = 1
				delta_ils_hdg = -90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			end
			
			if use_efis_map_range == 0 then	-- 5 NM
				ils_zoom = 2
			elseif use_efis_map_range == 1 then	-- 10 NM
				ils_zoom = 1
			elseif use_efis_map_range == 2 then	-- 20 NM
				ils_zoom = 0.5
			elseif use_efis_map_range == 3 then	-- 40 NM
				ils_zoom = 0.25
			else
				ils_on_off = 0
			end
			
			if rte_plan_mode == 1 then
				ils_zoom = ils_zoom * 0.57
			end
			
			ils_x = ils_x * ils_zoom		-- zoom
			ils_y = ils_y * ils_zoom		-- zoom
			
			if use_map_mode == 3 then
				ils_y = ils_y + 4.1	-- adjust center
			elseif use_vsd_map_mode == 1 then
				ils_y = ils_y + 5.1	-- adjust center
			elseif use_exp_map_mode == 0 then
				ils_y = ils_y + 4.1	-- adjust center
			end
			
			-- if ils_y > 14 or ils_y < -5 then		-- 7.7 / -1
				-- ils_on_off = 0
			-- end
			-- if ils_x < -11 or ils_x > 11 then		-- -6.0 / 6.0
				-- ils_on_off = 0
			-- end
			
			if ils_y > 30 or ils_y < -30 then		-- 7.7 / -1
				ils_on_off = 0
			end
			if ils_x < -30 or ils_x > 30 then		-- -6.0 / 6.0
				ils_on_off = 0
			end
			
			if ils_on_off == 1 then
				-- rotate
				if use_map_mode == 3 then
					ils_hdg = ref_runway_crs
				else
					if B738DR_track_up == 0 then
						ils_hdg = (ref_runway_crs - simDR_ahars_mag_hdg + 360) % 360
					else
						if B738DR_track_up_active == 0 then
							ils_hdg = (ref_runway_crs - simDR_ahars_mag_hdg + 360) % 360
						else
							ils_hdg = (ref_runway_crs - simDR_mag_hdg + 360) % 360
						end
					end
				end
				ils_hdg = (90 + ils_hdg) % 360
				ils_hdg = ils_hdg + simDR_mag_variation
				
				if in_mode == 0 then
					B738DR_ils_rotate0 = ils_hdg
					
					B738DR_ils_x0 = ils_x
					B738DR_ils_y0 = ils_y
					B738DR_ils_runway0 = use_ref_rwy
					B738DR_ils_show0 = 1
				else
					B738DR_ils_fo_rotate0 = ils_hdg
					
					B738DR_ils_fo_x0 = ils_x
					B738DR_ils_fo_y0 = ils_y
					B738DR_ils_fo_runway0 = use_ref_rwy
					B738DR_ils_fo_show0 = 1
				end
			else
				if in_mode == 0 then
					B738DR_ils_show0 = 0
				else
					B738DR_ils_fo_show0 = 0
				end
			end
		else
			if in_mode == 0 then
				B738DR_ils_show0 = 0
			else
				B738DR_ils_fo_show0 = 0
			end
		end
		
	else
		if in_mode == 0 then
			B738DR_ils_show = 0
			B738DR_ils_show0 = 0
		else
			B738DR_ils_fo_show = 0
			B738DR_ils_fo_show0 = 0
		end
	end
	
	if use_legs_num2 > 0 and (legs_delete ~= 0 or legs_num <= 1 or rte12_act ~= use_rte12_sel) then
	
		rte_plan_mode = 0
		ils_disable = 0
		ils_on_off = 0
		ils_lat = ndx_lat
		ils_lon = ndx_lon
		if use_rte12_sel == 0 then
			ils_rnw_lat = des_runway_lat_nd_mod
			ils_rnw_lon = des_runway_lon_nd_mod
			ils_rnw_crs = (des_runway_crs_nd_mod - mag_variation_deg(des_runway_lat_mod, des_runway_lon_mod) + 360) % 360
			ils_rnw_num = des_rnw_nd_mod
		else
			ils_rnw_lat = des_runway_lat_nd_mod2
			ils_rnw_lon = des_runway_lon_nd_mod2
			ils_rnw_crs = (des_runway_crs_nd_mod2 - mag_variation_deg(des_runway_lat_mod2, des_runway_lon_mod2) + 360) % 360
			ils_rnw_num = des_rnw_nd_mod2
		end
		
		-- CAPTAIN
		-- DES RUNWAY
		if use_map_mode == 3 then
			if use_rte12_sel == 0 then
				if use_legs_step > use_legs_num2 then
					use_legs_step = use_legs_num2
				end
				
				if use_legs_num2 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs2(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs2(use_legs_step)
				end
			else
				if use_legs_step > use_legs_num2 then
					use_legs_step = use_legs_num2
				end
				
				if use_legs_num2 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs8(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs8(use_legs_step)
				end
			end
			mag_hdg = -simDR_mag_variation
			rte_plan_mode = 1
		elseif use_map_mode == 2 then
			if use_irs_nd_allign ~= 0 then
				ils_disable = 1
			elseif B738DR_track_up == 0 then
				mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
			else
				if B738DR_track_up_active == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					mag_hdg = ndx_mag_hdg - simDR_mag_variation
				end
			end
		else
			ils_disable = 1
		end
		
		if use_rte12_sel == 0 then
			if use_des_rnw_nd_mod == "" then
				ils_disable = 1
			elseif (use_des_rnw_nd_mod == use_ref_rwy and use_des_icao_x == use_ref_icao) or (use_des_rnw_nd_mod == des_rnw_nd and use_des_icao_x == use_des_icao) then
				ils_disable = 1
			end
		else
			if use_des_rnw_nd_mod == "" then
				ils_disable = 1
			elseif (use_des_rnw_nd_mod == use_ref_rwy and use_des_icao_x == use_ref_icao) or (use_des_rnw_nd_mod == des_rnw_nd and use_des_icao_x == use_des_icao) then
				ils_disable = 1
			end
		end
		
		if ils_disable == 0 then
			
			ils_dis = nd_calc_dist2(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			--ils_dis = nd_calc_dist_d(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			
			ils_hdg = nd_calc_brg(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			--ils_hdg = nd_calc_brg_d(ils_lat, ils_lon, ils_rnw_lat, ils_rnw_lon)
			delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
			
			if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
				-- right
				ils_on_off = 1
				delta_ils_hdg = 90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
				-- left
				ils_on_off = 1
				delta_ils_hdg = 90 + delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg >= 90 then
				-- right back
				ils_on_off = 1
				delta_ils_hdg = delta_ils_hdg - 90
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg <= -90 then
				-- left back
				ils_on_off = 1
				delta_ils_hdg = -90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			end
			
			if use_efis_map_range == 0 then	-- 5 NM
				ils_zoom = 2
			elseif use_efis_map_range == 1 then	-- 10 NM
				ils_zoom = 1
			elseif use_efis_map_range == 2 then	-- 20 NM
				ils_zoom = 0.5
			elseif use_efis_map_range == 3 then	-- 40 NM
				ils_zoom = 0.25
			else
				ils_on_off = 0
			end
			
			if rte_plan_mode == 1 then
				ils_zoom = ils_zoom * 0.57
			end
			
			ils_x = ils_x * ils_zoom		-- zoom
			ils_y = ils_y * ils_zoom		-- zoom
			
			if use_map_mode == 3 then
				ils_y = ils_y + 4.1	-- adjust center
			elseif use_vsd_map_mode == 1 then
				ils_y = ils_y + 5.1	-- adjust center
			elseif use_exp_map_mode == 0 then
				ils_y = ils_y + 4.1	-- adjust center
			end
			
			if ils_y > 30 or ils_y < -30 then		-- 7.7 / -1
				ils_on_off = 0
			end
			if ils_x < -30 or ils_x > 30 then		-- -6.0 / 6.0
				ils_on_off = 0
			end
			
			if ils_on_off == 1 then
				-- rotate
				if use_map_mode == 3 then
					ils_hdg = ils_rnw_crs
				else
					if B738DR_track_up == 0 then
						ils_hdg = (ils_rnw_crs - simDR_ahars_mag_hdg + 360) % 360
					else
						if B738DR_track_up_active == 0 then
							ils_hdg = (ils_rnw_crs - simDR_ahars_mag_hdg + 360) % 360
						else
							ils_hdg = (ils_rnw_crs - simDR_mag_hdg + 360) % 360
						end
					end
				end
				ils_hdg = (90 + ils_hdg) % 360
				ils_hdg = ils_hdg + simDR_mag_variation
				
				if in_mode == 0 then
					B738DR_ils_rotate_mod = ils_hdg
					
					B738DR_ils_x_mod = ils_x
					B738DR_ils_y_mod = ils_y
					B738DR_ils_runway_mod = use_des_rnw_nd_mod		--ils_rnw_num
					B738DR_ils_show_mod = 1
				else
					B738DR_ils_fo_rotate_mod = ils_hdg
					
					B738DR_ils_fo_x_mod = ils_x
					B738DR_ils_fo_y_mod = ils_y
					B738DR_ils_fo_runway_mod = use_des_rnw_nd_mod		--ils_rnw_num
					B738DR_ils_fo_show_mod = 1
				end
			else
				if in_mode == 0 then
					B738DR_ils_show_mod = 0
				else
					B738DR_ils_fo_show_mod = 0
				end
			end
		else
			if in_mode == 0 then
				B738DR_ils_show_mod = 0
			else
				B738DR_ils_fo_show_mod = 0
			end
		end
		
		
		
		-- REF RUNWAY
		
		ils_disable = 0
		ils_on_off = 0
		if use_map_mode == 3 then
			if use_rte12_sel == 0 then
				if use_legs_step > use_legs_num2 then
					use_legs_step = use_legs_num2
				end
				
				if use_legs_num2 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs2(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs2(use_legs_step)
				end
			else
				if use_legs_step > use_legs_num2 then
					use_legs_step = use_legs_num2
				end
				
				if use_legs_num2 < 1 then
					ils_disable = 1
				elseif use_legs_step == 0 then
					-- ils_lat = legs_data2[1][7]
					-- ils_lon = legs_data2[1][8]
					ils_lat, ils_lon = lat_lon_legs8(1)
				else
					-- ils_lat = legs_data2[legs_step][7]
					-- ils_lon = legs_data2[legs_step][8]
					ils_lat, ils_lon = lat_lon_legs8(use_legs_step)
				end
			end
			mag_hdg = -simDR_mag_variation
		elseif use_map_mode == 2 then
			if use_irs_nd_allign ~= 0 then
				ils_disable = 1
			elseif B738DR_track_up == 0 then
				mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
			else
				if B738DR_track_up_active == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					mag_hdg = ndx_mag_hdg - simDR_mag_variation
				end
			end
		else
			ils_disable = 1
		end
		
		if use_rte12_sel == 0 then
			if use_ref_rwy2 == "-----" and use_ref_rwy3 == "-----" then
				ils_disable = 1
			elseif (use_ref_rwy2 == use_ref_rwy and use_ref_icao_x == use_ref_icao) or (use_ref_rwy2 == des_rnw_nd and use_ref_icao == use_des_icao) 
			or (use_ref_rwy2 == des_rnw_nd_mod and use_ref_icao == use_des_icao_x) then
				ils_disable = 1
			end
		else
			if use_ref_rwy2 == "-----" and use_ref_rwy3 == "-----" then
				ils_disable = 1
			elseif (use_ref_rwy2 == use_ref_rwy and use_ref_icao_x == use_ref_icao) or (use_ref_rwy2 == des_rnw_nd and use_ref_icao == use_des_icao) 
			or (use_ref_rwy2 == des_rnw_nd_mod2 and use_ref_icao == use_des_icao_x) then
				ils_disable = 1
			end
		end
		
		if ils_disable == 0 and ref_runway_lenght_mod > 0 then
			
			if use_rte12_sel == 0 then
				ils_dis = nd_calc_dist2(ils_lat, ils_lon, ref_runway_lat_mod, ref_runway_lon_mod)
				
				ils_hdg = nd_calc_brg(ils_lat, ils_lon, ref_runway_lat_mod, ref_runway_lon_mod)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
			else
				ils_dis = nd_calc_dist2(ils_lat, ils_lon, ref_runway_lat_mod2, ref_runway_lon_mod2)
				
				ils_hdg = nd_calc_brg(ils_lat, ils_lon, ref_runway_lat_mod2, ref_runway_lon_mod2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
			end
			
			if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
				-- right
				ils_on_off = 1
				delta_ils_hdg = 90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
				-- left
				ils_on_off = 1
				delta_ils_hdg = 90 + delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg >= 90 then
				-- right back
				ils_on_off = 1
				delta_ils_hdg = delta_ils_hdg - 90
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = ils_dis * math.cos(delta_ils_hdg)
			elseif delta_ils_hdg <= -90 then
				-- left back
				ils_on_off = 1
				delta_ils_hdg = -90 - delta_ils_hdg
				delta_ils_hdg = math.rad(delta_ils_hdg)
				ils_y = -ils_dis * math.sin(delta_ils_hdg)
				ils_x = -ils_dis * math.cos(delta_ils_hdg)
			end
			
			if use_efis_map_range == 0 then	-- 5 NM
				ils_zoom = 2
			elseif use_efis_map_range == 1 then	-- 10 NM
				ils_zoom = 1
			elseif use_efis_map_range == 2 then	-- 20 NM
				ils_zoom = 0.5
			elseif use_efis_map_range == 3 then	-- 40 NM
				ils_zoom = 0.25
			else
				ils_on_off = 0
			end
			
			if rte_plan_mode == 1 then
				ils_zoom = ils_zoom * 0.57
			end
			
			ils_x = ils_x * ils_zoom		-- zoom
			ils_y = ils_y * ils_zoom		-- zoom
			
			if use_map_mode == 3 then
				ils_y = ils_y + 4.1	-- adjust center
			elseif use_vsd_map_mode == 1 then
				ils_y = ils_y + 5.1	-- adjust center
			elseif use_exp_map_mode == 0 then
				ils_y = ils_y + 4.1	-- adjust center
			end
			
			-- if ils_y > 14 or ils_y < -5 then		-- 7.7 / -1
				-- ils_on_off = 0
			-- end
			-- if ils_x < -11 or ils_x > 11 then		-- -6.0 / 6.0
				-- ils_on_off = 0
			-- end
			
			if ils_y > 30 or ils_y < -30 then		-- 7.7 / -1
				ils_on_off = 0
			end
			if ils_x < -30 or ils_x > 30 then		-- -6.0 / 6.0
				ils_on_off = 0
			end
			
			if ils_on_off == 1 then
				-- rotate
				if use_rte12_sel == 0 then
					if use_map_mode == 3 then
						ils_hdg = ref_runway_crs_mod
					else
						if B738DR_track_up == 0 then
							ils_hdg = (ref_runway_crs_mod - simDR_ahars_mag_hdg + 360) % 360
						else
							if B738DR_track_up_active == 0 then
								ils_hdg = (ref_runway_crs_mod - simDR_ahars_mag_hdg + 360) % 360
							else
								ils_hdg = (ref_runway_crs_mod - simDR_mag_hdg + 360) % 360
							end
						end
					end
				else
					if use_map_mode == 3 then
						ils_hdg = ref_runway_crs_mod2
					else
						if B738DR_track_up == 0 then
							ils_hdg = (ref_runway_crs_mod2 - simDR_ahars_mag_hdg + 360) % 360
						else
							if B738DR_track_up_active == 0 then
								ils_hdg = (ref_runway_crs_mod2 - simDR_ahars_mag_hdg + 360) % 360
							else
								ils_hdg = (ref_runway_crs_mod2 - simDR_mag_hdg + 360) % 360
							end
						end
					end
				end
				ils_hdg = (90 + ils_hdg) % 360
				ils_hdg = ils_hdg + simDR_mag_variation
				
				if in_mode == 0 then
					B738DR_ils_rotate0_mod = ils_hdg
					
					B738DR_ils_x0_mod = ils_x
					B738DR_ils_y0_mod = ils_y
					if use_ref_rwy2 == "-----" then
						B738DR_ils_runway0_mod = use_ref_rwy3
					else
						B738DR_ils_runway0_mod = use_ref_rwy2
					end
					B738DR_ils_show0_mod = 1
				else
					B738DR_ils_fo_rotate0_mod = ils_hdg
					
					B738DR_ils_fo_x0_mod = ils_x
					B738DR_ils_fo_y0_mod = ils_y
					if use_ref_rwy2 == "-----" then
						B738DR_ils_fo_runway0_mod = use_ref_rwy3
					else
						B738DR_ils_fo_runway0_mod = use_ref_rwy2
					end
					B738DR_ils_fo_show0_mod = 1
				end
			else
				if in_mode == 0 then
					B738DR_ils_show0_mod = 0
				else
					B738DR_ils_fo_show0_mod = 0
				end
			end
		else
			if in_mode == 0 then
				B738DR_ils_show0_mod = 0
			else
				B738DR_ils_fo_show0_mod = 0
			end
		end
		
	else
		if in_mode == 0 then
			B738DR_ils_show_mod = 0
			B738DR_ils_show0_mod = 0
		else
			B738DR_ils_fo_show_mod = 0
			B738DR_ils_fo_show0_mod = 0
		end
	end
	
	
end

function B738_displ_fix()
	
	local ils_lat2 = 0
	local ils_lon2 = 0
	local ils_lat = ndx_lat
	local ils_lon = ndx_lon
	local mag_hdg = 0
	local delta_ils_hdg = 0
	local ils_hdg = 0
	local ils_on_off = 0
	local ils_x = 0
	local ils_y = 0
	local ils_dis = 0
	local ils_zoom = 0
	local ils_disable = 0
	local ii = 0
	local jj = 0
	local kk = 0
	
	if fix_data_num > 0 then
	
		-- CAPTAIN
		if B738DR_capt_map_mode == 2 then
			if B738DR_irs_nd_allign ~= 0 then
				ils_disable = 1
			elseif B738DR_track_up == 0 then
				mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
			else
				if B738DR_track_up_active == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					mag_hdg = ndx_mag_hdg - simDR_mag_variation
				end
			end
		elseif B738DR_capt_map_mode == 3 then
			if rte12_sel == 0 then
				if legs_num2 > 1 then
					if legs_step == 0 then
						-- ils_lat = legs_data2[1][7]
						-- ils_lon = legs_data2[1][8]
						ils_lat, ils_lon = lat_lon_legs2(1)
					else
						-- ils_lat = legs_data2[legs_step][7]
						-- ils_lon = legs_data2[legs_step][8]
						ils_lat, ils_lon = lat_lon_legs2(legs_step)
					end
					mag_hdg = -simDR_mag_variation
				else
					ils_disable = 1
				end
			else
				if legs_num8 > 1 then
					if legs_step == 0 then
						-- ils_lat = legs_data2[1][7]
						-- ils_lon = legs_data2[1][8]
						ils_lat, ils_lon = lat_lon_legs8(1)
					else
						-- ils_lat = legs_data2[legs_step][7]
						-- ils_lon = legs_data2[legs_step][8]
						ils_lat, ils_lon = lat_lon_legs8(legs_step)
					end
					mag_hdg = -simDR_mag_variation
				else
					ils_disable = 1
				end
			end
		else
			ils_disable = 1
		end
		
		if ils_disable == 0 then
			
			for ii = 1, fix_data_num do
			
				ils_lat2 = fix_data[ii][3]
				ils_lon2 = fix_data[ii][4]
					
				ils_dis = nd_calc_dist2(ils_lat, ils_lon, ils_lat2, ils_lon2)
				
				ils_hdg = nd_calc_brg(ils_lat, ils_lon, ils_lat2, ils_lon2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
			
				if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
					-- right
					ils_on_off = 1
					delta_ils_hdg = 90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
					-- left
					ils_on_off = 1
					delta_ils_hdg = 90 + delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg >= 90 then
					-- right back
					ils_on_off = 1
					delta_ils_hdg = delta_ils_hdg - 90
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg <= -90 then
					-- left back
					ils_on_off = 1
					delta_ils_hdg = -90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				end
				
				if B738DR_efis_map_range_capt == 0 then	-- 5 NM
					ils_zoom = 2
				elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
					ils_zoom = 1
				elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
					ils_zoom = 0.5
				elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
					ils_zoom = 0.25
				elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
					ils_zoom = 0.125
				elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
					ils_zoom = 0.0625
				elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
					ils_zoom = 0.03125
				elseif B738DR_efis_map_range_capt == 7 then	-- 640 NM
					ils_zoom = 0.015625
				else
					ils_on_off = 0
				end
				
				if B738DR_capt_map_mode == 3 then
					ils_zoom = ils_zoom * 0.57
				end
				
				ils_x = ils_x * ils_zoom		-- zoom
				ils_y = ils_y * ils_zoom		-- zoom
				
				if B738DR_capt_map_mode == 3 then
					ils_y = ils_y + 4.1	-- adjust center
				elseif B738DR_capt_vsd_map_mode == 1 then
					ils_y = ils_y + 5.1	-- adjust center
				elseif B738DR_capt_exp_map_mode == 0 then
					ils_y = ils_y + 4.1	-- adjust center
				end
				
				if ils_on_off == 1 then
					
					-- show FIX
					B738DR_fix_show[ii-1] = 1
					B738DR_fix_x[ii-1] = ils_x
					B738DR_fix_y[ii-1] = ils_y
					B738DR_fix_type[ii-1] = fix_data[ii][2]
					if ii == 1 then
						B738DR_fix_id00 = fix_data[ii][1]
					elseif ii == 2 then
						B738DR_fix_id01 = fix_data[ii][1]
					elseif ii == 3 then
						B738DR_fix_id02 = fix_data[ii][1]
					elseif ii == 4 then
						B738DR_fix_id03 = fix_data[ii][1]
					elseif ii == 5 then
						B738DR_fix_id04 = fix_data[ii][1]
					end
				else
					B738DR_fix_show[ii-1] = 0
				end
					for jj = 0, 2 do
						kk = 6 + (jj * 2)
						if fix_data[ii][kk] ~= -1 and fix_data[ii][kk+1] ~= -1 then
							-- radial / distance
							-- radial
							if B738DR_capt_map_mode == 3 then
								ils_hdg = fix_data[ii][kk]
							else
								if B738DR_track_up == 0 then
									ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
								else
									if B738DR_track_up_active == 0 then
										ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
									else
										ils_hdg = (fix_data[ii][kk] - simDR_mag_hdg + 360) % 360
									end
								end
							end
							ils_hdg = (90 + ils_hdg) % 360
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad00_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad00_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad00_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad01_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad01_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad01_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad02_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad02_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad02_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad03_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad03_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad03_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad04_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad04_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad04_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							end
							--distance
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							end
							
						elseif fix_data[ii][kk] ~= -1 then
							-- radial
							if B738DR_capt_map_mode == 3 then
								ils_hdg = fix_data[ii][kk]
							else
								if B738DR_track_up == 0 then
									ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
								else
									if B738DR_track_up_active == 0 then
										ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
									else
										ils_hdg = (fix_data[ii][kk] - simDR_mag_hdg + 360) % 360
									end
								end
							end
							ils_hdg = (90 + ils_hdg) % 360
							--ils_hdg = ils_hdg + simDR_mag_variation
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad00_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad00_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad00_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad01_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad01_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad01_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad02_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad02_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad02_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad03_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad03_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad03_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad04_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad04_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad04_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_rot_2[ii-1] = ils_hdg
								end
							end
						elseif fix_data[ii][kk+1] ~= -1 then
							--distance
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							end
						else
							-- nothing
							if jj == 0 then
								B738DR_fix_rad_dist_0[ii-1] = 0	-- 0-none, 1-radial, 2-distance
								B738DR_fix_rad_dist_0a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
							elseif jj == 1 then
								B738DR_fix_rad_dist_1[ii-1] = 0	-- 0-none, 1-radial, 2-distance
								B738DR_fix_rad_dist_1a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
							elseif jj == 2 then
								B738DR_fix_rad_dist_2[ii-1] = 0	-- 0-none, 1-radial, 2-distance
								B738DR_fix_rad_dist_2a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
							end
						end
					end
				-- else
					-- B738DR_fix_show[ii-1] = 0
				-- end
				B738DR_fix_lat[ii-1] = fix_data[ii][3]
				B738DR_fix_lon[ii-1] = fix_data[ii][4]
				B738DR_fix_en[ii-1] = 1
			end
			if fix_data_num < 5 then
				for ii = fix_data_num, 4 do
					B738DR_fix_show[ii] = 0
					B738DR_fix_en[ii] = 0
				end
			end
			
		else
			for ii = 0, 4 do
				B738DR_fix_show[ii] = 0
				B738DR_fix_en[ii] = 0
			end
		end
		
		
		-- FIRST OFFICER
		ils_lat = ndx_lat
		ils_lon = ndx_lon
		ils_disable = 0
		if B738DR_fo_map_mode == 2 then
			if B738DR_irs_nd_allign2 ~= 0 then
				ils_disable = 1
			elseif B738DR_track_up == 0 then
				mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
			else
				if B738DR_track_up_active == 0 then
					mag_hdg = ndx_ahars_mag_hdg - simDR_mag_variation
				else
					mag_hdg = ndx_mag_hdg - simDR_mag_variation
				end
			end
		elseif B738DR_fo_map_mode == 3 then
			if rte12_sel_fo == 0 then
				if legs_num2 > 1 then
					if legs_step2 == 0 then
						-- ils_lat = legs_data2[1][7]
						-- ils_lon = legs_data2[1][8]
						ils_lat, ils_lon = lat_lon_legs2(1)
					else
						-- ils_lat = legs_data2[legs_step2][7]
						-- ils_lon = legs_data2[legs_step2][8]
						ils_lat, ils_lon = lat_lon_legs2(legs_step2)
					end
					mag_hdg = -simDR_mag_variation
				else
					ils_disable = 1
				end
			else
				if legs_num8 > 1 then
					if legs_step2 == 0 then
						-- ils_lat = legs_data2[1][7]
						-- ils_lon = legs_data2[1][8]
						ils_lat, ils_lon = lat_lon_legs8(1)
					else
						-- ils_lat = legs_data2[legs_step2][7]
						-- ils_lon = legs_data2[legs_step2][8]
						ils_lat, ils_lon = lat_lon_legs8(legs_step2)
					end
					mag_hdg = -simDR_mag_variation
				else
					ils_disable = 1
				end
			end
		else
			ils_disable = 1
		end
		
		if ils_disable == 0 then
			
			for ii = 1, fix_data_num do
			
				ils_lat2 = fix_data[ii][3]
				ils_lon2 = fix_data[ii][4]
					
				ils_dis = nd_calc_dist2(ils_lat, ils_lon, ils_lat2, ils_lon2)
				--ils_dis = nd_calc_dist_d(ils_lat, ils_lon, ils_lat2, ils_lon2)
				
				ils_hdg = nd_calc_brg(ils_lat, ils_lon, ils_lat2, ils_lon2)
				--ils_hdg = nd_calc_brg_d(ils_lat, ils_lon, ils_lat2, ils_lon2)
				delta_ils_hdg = calc_rel_brg(ils_hdg, mag_hdg)
				
				if delta_ils_hdg >= 0 and delta_ils_hdg <= 90 then
					-- right
					ils_on_off = 1
					delta_ils_hdg = 90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg < 0 and delta_ils_hdg >= -90 then
					-- left
					ils_on_off = 1
					delta_ils_hdg = 90 + delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg >= 90 then
					-- right back
					ils_on_off = 1
					delta_ils_hdg = delta_ils_hdg - 90
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = ils_dis * math.cos(delta_ils_hdg)
				elseif delta_ils_hdg <= -90 then
					-- left back
					ils_on_off = 1
					delta_ils_hdg = -90 - delta_ils_hdg
					delta_ils_hdg = math.rad(delta_ils_hdg)
					ils_y = -ils_dis * math.sin(delta_ils_hdg)
					ils_x = -ils_dis * math.cos(delta_ils_hdg)
				end
				
				if B738DR_efis_map_range_fo == 0 then	-- 5 NM
					ils_zoom = 2
				elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
					ils_zoom = 1
				elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
					ils_zoom = 0.5
				elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
					ils_zoom = 0.25
				elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
					ils_zoom = 0.125
				elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
					ils_zoom = 0.0625
				elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
					ils_zoom = 0.03125
				elseif B738DR_efis_map_range_fo == 7 then	-- 640 NM
					ils_zoom = 0.015625
				else
					ils_on_off = 0
				end
				
				if B738DR_fo_map_mode == 3 then
					--ils_zoom = ils_zoom / 2
					ils_zoom = ils_zoom * 0.57
				end
				
				ils_x = ils_x * ils_zoom		-- zoom
				ils_y = ils_y * ils_zoom		-- zoom
				
				if B738DR_fo_map_mode == 3 then
					ils_y = ils_y + 4.1	-- adjust center
				elseif B738DR_fo_vsd_map_mode == 1 then
					ils_y = ils_y + 5.1	-- adjust center
				elseif B738DR_fo_exp_map_mode == 0 then
					ils_y = ils_y + 4.1	-- adjust center
				end
				
				if ils_on_off == 1 then
					
					-- show FIX
					B738DR_fix_fo_show[ii-1] = 1
					B738DR_fix_fo_x[ii-1] = ils_x
					B738DR_fix_fo_y[ii-1] = ils_y
					B738DR_fix_fo_type[ii-1] = fix_data[ii][2]
					if ii == 1 then
						B738DR_fix_fo_id00 = fix_data[ii][1]
					elseif ii == 2 then
						B738DR_fix_fo_id01 = fix_data[ii][1]
					elseif ii == 3 then
						B738DR_fix_fo_id02 = fix_data[ii][1]
					elseif ii == 4 then
						B738DR_fix_fo_id03 = fix_data[ii][1]
					elseif ii == 5 then
						B738DR_fix_fo_id04 = fix_data[ii][1]
					end
				
				else
					B738DR_fix_fo_show[ii-1] = 0
				end
					for jj = 0, 2 do
						kk = 6 + (jj * 2)
						
						if fix_data[ii][kk] ~= -1 and fix_data[ii][kk+1] ~= -1 then
							-- radial/distance
							-- radial
							if B738DR_fo_map_mode == 3 then
								ils_hdg = fix_data[ii][kk]
							else
								if B738DR_track_up == 0 then
									ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
								else
									if B738DR_track_up_active == 0 then
										ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
									else
										ils_hdg = (fix_data[ii][kk] - simDR_mag_hdg + 360) % 360
									end
								end
							end
							ils_hdg = (90 + ils_hdg) % 360
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad00_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad00_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad00_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad01_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad01_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad01_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad02_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad02_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad02_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad03_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad03_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad03_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad04_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad04_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad04_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							end
							--distance
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							end
						elseif fix_data[ii][kk] ~= -1 then
							-- radial
							if B738DR_fo_map_mode == 3 then
								ils_hdg = fix_data[ii][kk]
							else
								if B738DR_track_up == 0 then
									ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
								else
									if B738DR_track_up_active == 0 then
										ils_hdg = (fix_data[ii][kk] - simDR_ahars_mag_hdg + 360) % 360
									else
										ils_hdg = (fix_data[ii][kk] - simDR_mag_hdg + 360) % 360
									end
								end
							end
							ils_hdg = (90 + ils_hdg) % 360
							--ils_hdg = ils_hdg + simDR_mag_variation
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad00_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad00_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad00_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad01_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad01_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad01_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad02_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad02_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad02_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad03_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad03_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad03_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad04_0 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_0[ii-1] = ils_hdg
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad04_1 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_1[ii-1] = ils_hdg
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 1	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad04_2 = string.format("%03d", fix_data[ii][kk])
									B738DR_fix_fo_rot_2[ii-1] = ils_hdg
								end
							end
						elseif fix_data[ii][kk+1] ~= -1 then
							--distance
							if ii == 1 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 2 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 3 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 4 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							elseif ii == 5 then
								if jj == 0 then
									B738DR_fix_fo_rad_dist_0[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_0a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_0[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_0_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 1 then
									B738DR_fix_fo_rad_dist_1[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_1a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_1[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_1_nm[ii-1] = fix_data[ii][kk+1]
								elseif jj == 2 then
									B738DR_fix_fo_rad_dist_2[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_rad_dist_2a[ii-1] = 2	-- 0-none, 1-radial, 2-distance
									B738DR_fix_fo_dist_2[ii-1] = fix_data[ii][kk+1] * ils_zoom
									B738DR_fix_fo_dist_2_nm[ii-1] = fix_data[ii][kk+1]
								end
							end
						else
							-- nothing
							if jj == 0 then
								B738DR_fix_fo_rad_dist_0[ii-1] = 0	-- 0-none, 1-radial, 2-distance
								B738DR_fix_fo_rad_dist_0a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
							elseif jj == 1 then
								B738DR_fix_fo_rad_dist_1[ii-1] = 0	-- 0-none, 1-radial, 2-distance
								B738DR_fix_fo_rad_dist_1a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
							elseif jj == 2 then
								B738DR_fix_fo_rad_dist_2[ii-1] = 0	-- 0-none, 1-radial, 2-distance
								B738DR_fix_fo_rad_dist_2a[ii-1] = 0	-- 0-none, 1-radial, 2-distance
							end
						end
					end
				-- else
					-- B738DR_fix_fo_show[ii-1] = 0
				-- end
				B738DR_fix_fo_lat[ii-1] = fix_data[ii][3]
				B738DR_fix_fo_lon[ii-1] = fix_data[ii][4]
				B738DR_fix_fo_en[ii-1] = 1
			end
			if fix_data_num < 5 then
				for ii = fix_data_num, 4 do
					B738DR_fix_fo_show[ii] = 0
					B738DR_fix_fo_en[ii] = 0
				end
			end
		else
			for ii = 0, 4 do
				B738DR_fix_fo_show[ii] = 0
				B738DR_fix_fo_en[ii] = 0
			end
		end
		
	else
		for ii = 0, 4 do
			B738DR_fix_show[ii] = 0
			B738DR_fix_fo_show[ii] = 0
			B738DR_fix_en[ii] = 0
			B738DR_fix_fo_en[ii] = 0
		end
	end
	
end

function B738_restrict_data()

	local ii = 0
	local jj = 0
	local kk = 0
	local temp_alt = 0
	local td_idx_temp = 0
	local find_next_rest = 0
	local skip_rest = 0
	local last_rest_spd = 0
	local last_rest_idx = 0
	local rest_spd = 0
	local before_idx = 0
	local spd_250_10000 = 0
	local fmc_descent_speed = 0
	local rnav_idx_temp = legs_num
	if rnav_idx_last ~= 0 then
		rnav_idx_temp = rnav_idx_last
	end
	
	local enable_acf = true
	if rte12_act == 0 then
		if ref_icao == "----" or des_icao == "****" then
			enable_acf = false
		end
	elseif rte12_act == 0 then
		if ref_icao7 == "----" or des_icao7 == "****" then
			enable_acf = false
		end
	else
		enable_acf = false
	end
	
	local first_ma_idx = 0
	
	B738DR_rest_wpt_spd_id = ""
	B738DR_rest_wpt_spd = 0
	B738DR_rest_wpt_spd2 = 0
	B738DR_rest_wpt_spd_idx = 0
	B738DR_rest_wpt_spd_tgt = 0
	B738DR_rest_wpt_spd_type = 0
	B738DR_rest_wpt_alt_id = ""
	B738DR_rest_wpt_alt = 0
	B738DR_rest_wpt_alt_t = 0
	B738DR_rest_wpt_alt_idx = 0
	B738DR_rest_wpt_alt_dist = 0
	B738DR_calc_wpt_spd = 0
	B738DR_calc_wpt_alt = 0
	
	B738DR_rest_wpt_alt_id2 = ""
	B738DR_rest_wpt_alt2 = 0
	B738DR_rest_wpt_alt_idx2 = 0
	
	decel_hold_idx = 0
	decel_hold_dist = 0
	decel_hold_spd = 0
	
	if legs_num > 1 and enable_acf == true then
		
		if true_legs_num(1) > 0 then
			offset = math.max(offset, 2)
			if offset_seq < offset or offset_seq > legs_num then
				offset_seq = offset
			end
		else
			offset = 0
			offset_seq = 0
		end
		
		if offset > legs_num then
			offset = legs_num
			offset_seq = offset
		end
		
		if offset == 0 then
			offset = 1
			offset_seq = offset
		end
		
		local tmp_spd_rest_dist = 0
		local disable_faf_restrict = 0
		
		local hold_active_idx = 0
		local time_to_hold = 0
		local spd_pom = 0
		local spd_rest_pom = 0
		
		if legs_data[offset][21] == 0 or legs_data[offset][21] == 1 then
			hold_active_idx = offset
		else
			jj = legs_num - offset
			if jj > 20 then
				jj = 20
			end
			if jj > 0 then
				jj = offset + jj
				for ii = offset, jj do
					if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
						if ii <= rnav_idx_temp then
							if legs_data[ii][4] > 0 then
								spd_rest_pom = legs_data[ii][4]
							else
								spd_rest_pom = legs_data[ii][10]
							end
						else
							spd_rest_pom = 0
						end
					else
						if legs_data[ii][4] > 0 then
							spd_rest_pom = legs_data[ii][4]
						else
							spd_rest_pom = legs_data[ii][10]
						end
					end
					if (legs_data[ii][21] == 0 or legs_data[ii][21] == 1) and spd_rest_pom > 0 then
						spd_pom = 0
						time_to_hold = (B738DR_rt_abeam_dist_bp + legs_data[offset][50]) - legs_data[ii][50]
						if time_to_hold > 0 then
							
							if spd_rest_pom < 100 then
								if legs_data[ii][5] ~= 0 then
									spd_pom = mach_to_tas(spd_rest_pom, legs_data[ii][5])
								else
									spd_pom = mach_to_tas(spd_rest_pom, simDR_altitude_pilot)
								end
							else
								if legs_data[ii][5] ~= 0 then
									spd_pom = ias_to_tas(spd_rest_pom, legs_data[ii][5])
								else
									spd_pom = ias_to_tas(spd_rest_pom, simDR_altitude_pilot)
								end
							end
							spd_pom = spd_pom * 0.5144444
							if spd_pom > 0 then
								time_to_hold = (time_to_hold * 1852) / spd_pom;
							else
								time_to_hold = 999
							end
						else
							time_to_hold = 0
						end
						if time_to_hold < 180 then
							hold_active_idx = ii
						else
							--calc decel_hold
							if spd_pom > 0 then
								decel_hold_idx = ii
								decel_hold_dist = (180 * spd_pom) / 1852
								decel_hold_spd = spd_pom * 1.94384449
								kk = ii
								if kk > 1 then
									while decel_hold_dist > wpt_distance(kk) do
										decel_hold_dist = decel_hold_dist - wpt_distance(kk)
										kk = kk - 1
										decel_hold_idx = kk
										if kk < 2 then
											decel_hold_idx = 0
											break
										end
									end
								end
							end
						end
						
						break
					end
				end
			end
		end
		
		if hold_active_idx ~= 0 then
			if legs_data[hold_active_idx][4] == 0 and legs_data[hold_active_idx][10] == 0 then
				hold_active_idx = 0
				decel_hold_idx = 0
				decel_hold_dist = 0
				decel_hold_spd = 0
			end
		end
		
		if hold_active_idx ~= 0 then
			B738DR_rest_wpt_spd_id = legs_data[hold_active_idx][1]
			if legs_data[hold_active_idx][4] > 0 then
				B738DR_rest_wpt_spd = legs_data[hold_active_idx][4]
			else
				B738DR_rest_wpt_spd = legs_data[hold_active_idx][10]
			end
			B738DR_rest_wpt_spd_idx = hold_active_idx
			B738DR_rest_wpt_spd2 = B738DR_rest_wpt_spd
			B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
			B738DR_rest_wpt_spd_type = 1
			decel_dist = 90000
		elseif legs_restr_spd_n > 0 then
			
			if B738DR_flight_phase < 2 or B738DR_flight_phase > 7 then		-- climb and go-around
				
				td_idx_temp = math.max(last_sid_idx, tc_idx - 1)
				
				-- B738DR_fms_test3 = td_idx_temp
				if td_idx_temp > 0 then
					for ii = 1, legs_restr_spd_n do
						
						if legs_restr_spd[ii][2] >= offset and legs_restr_spd[ii][2] <= td_idx_temp then
							B738DR_rest_wpt_spd_id = legs_restr_spd[ii][1]
							B738DR_rest_wpt_spd = legs_restr_spd[ii][3]
							B738DR_rest_wpt_spd_idx = legs_restr_spd[ii][2]
							B738DR_rest_wpt_spd2 = B738DR_rest_wpt_spd
							B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
							if legs_restr_spd[ii][4] == 2 then	-- speed above
								find_next_rest = 0
								jj = ii + 1
								while jj <= legs_restr_spd_n do
									if legs_restr_spd[jj][2] >= offset and legs_restr_spd[jj][2] <= td_idx_temp then
										if legs_restr_spd[jj][3] > B738DR_rest_wpt_spd then
											find_next_rest = 1
											B738DR_rest_wpt_spd2 = legs_restr_spd[jj][3]
											break
										end
									end
									jj = jj + 1
								end
								if find_next_rest == 0 then
									B738DR_rest_wpt_spd2 = 340
								end
							end
							break
						end
						if legs_restr_spd[ii][2] >= offset and B738DR_missed_app_act > 0 and B738DR_rest_wpt_spd == 0 then
							B738DR_rest_wpt_spd_id = legs_restr_spd[ii][1]
							B738DR_rest_wpt_spd = legs_restr_spd[ii][3]
							B738DR_rest_wpt_spd_idx = legs_restr_spd[ii][2]
							B738DR_rest_wpt_spd2 = B738DR_rest_wpt_spd
							B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
							if legs_restr_spd[ii][4] == 2 then	-- speed above
								find_next_rest = 0
								jj = ii + 1
								while jj <= legs_restr_spd_n do
									if legs_restr_spd[jj][2] >= offset and legs_restr_spd[jj][2] <= td_idx_temp then
										if legs_restr_spd[jj][3] > B738DR_rest_wpt_spd then
											find_next_rest = 1
											B738DR_rest_wpt_spd2 = legs_restr_spd[jj][3]
											break
										end
									end
									jj = jj + 1
								end
								if find_next_rest == 0 then
									B738DR_rest_wpt_spd2 = 340
								end
							end
							break
						end
					end
				end
				
			elseif B738DR_flight_phase >= 2 and B738DR_flight_phase <= 4 then	-- cruise before T/D
				
				-- check to last SID wpt
				-- td_idx_temp = math.max(last_sid_idx, tc_idx)	--tc_idx - 1
				if tc_idx > last_sid_idx then
					td_idx_temp = tc_idx - 1
				else
					td_idx_temp = last_sid_idx
				end
				-- if td_idx == 0 then
					-- td_idx_temp = last_sid_idx
				-- else
					-- td_idx_temp = td_idx
				-- end
				if td_idx_temp > 0 then
					for ii = 1, legs_restr_spd_n do
						if offset <= legs_restr_spd[ii][2] and legs_restr_spd[ii][2] <= td_idx_temp then
							B738DR_rest_wpt_spd_id = legs_restr_spd[ii][1]
							B738DR_rest_wpt_spd = legs_restr_spd[ii][3]
							B738DR_rest_wpt_spd_idx = legs_restr_spd[ii][2]
							B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
							break
						end
					end
				else
					for ii = offset, legs_num do
						if ii > rnav_idx_last and rnav_idx_last ~= 0 then
							if legs_data[ii][19] == 10 and legs_data[ii][4] ~= 0 then
								B738DR_rest_wpt_spd_id = legs_data[ii][1]
								B738DR_rest_wpt_spd = legs_data[ii][4]
								B738DR_rest_wpt_spd_idx = ii
								B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
								break
							end
						elseif legs_data[ii][19] == 10 and first_ma_idx == 0 then
							first_ma_idx = ii
						end
						if first_ma_idx ~= 0 and ii >= first_ma_idx then
							if legs_data[ii][19] == 10 and legs_data[ii][4] ~= 0 then
								B738DR_rest_wpt_spd_id = legs_data[ii][1]
								B738DR_rest_wpt_spd = legs_data[ii][4]
								B738DR_rest_wpt_spd_idx = ii
								B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
								break
							end
						end
					end
				end
				
				-- check from T/C
				if B738DR_rest_wpt_spd == 0 then
				
					if tc_idx == 0 then
						if last_sid_idx == 0 then
							if td_idx == 0 then
								if first_star_idx == 0 and first_app_idx == 0 then
									td_idx_temp = legs_num
								elseif first_star_idx == 0 then
									td_idx_temp = first_app_idx
								elseif first_app_idx == 0 then
									td_idx_temp = first_star_idx
								else
									td_idx_temp = math.min(first_star_idx, first_app_idx)
								end
							else
								if first_star_idx == 0 and first_app_idx == 0 then
									td_idx_temp = td_idx
								elseif first_star_idx == 0 then
									td_idx_temp = math.min(first_app_idx, td_idx)
								elseif first_app_idx == 0 then
									td_idx_temp = math.min(first_star_idx, td_idx)
								else
									td_idx_temp = math.min(first_star_idx, first_app_idx, td_idx)
								end
							end
						else
							td_idx_temp = last_sid_idx + 1
						end
					else
						--td_idx_temp = tc_idx
						td_idx_temp = math.max(tc_idx, last_sid_idx + 1)
					end
					
					for ii = legs_restr_spd_n, 1, -1 do
						if legs_restr_spd[ii][2] >= td_idx_temp and legs_restr_spd[ii][3] <= B738DR_fmc_cruise_speed then
							decel_before_idx = legs_restr_spd[ii][2]
							if legs_data[decel_before_idx][3] < 4.3 and decel_before_idx > 1 then	--6.7
								decel_dist = 4.3 - legs_data[decel_before_idx][3]	--6.7
								decel_before_idx = decel_before_idx - 1
								if legs_data[decel_before_idx][4] ~= 0 then
									decel_before_idx = legs_restr_spd[ii][2]
									decel_dist = legs_data[decel_before_idx][3] - 0.3
								else
									if legs_data[decel_before_idx][3] < decel_dist then
										decel_dist = legs_data[decel_before_idx][3] - 0.3
									end
								end
							else
								decel_dist = 4.3	--6.7
							end
							if decel_before_idx < offset then
								B738DR_rest_wpt_spd_id = legs_restr_spd[ii][1]
								B738DR_rest_wpt_spd = legs_restr_spd[ii][3]
								B738DR_rest_wpt_spd_idx = legs_restr_spd[ii][2]
								B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
								break
							elseif decel_before_idx == offset and B738DR_fmc_dist <= decel_dist then
								B738DR_rest_wpt_spd_id = legs_restr_spd[ii][1]
								B738DR_rest_wpt_spd = legs_restr_spd[ii][3]
								B738DR_rest_wpt_spd_idx = legs_restr_spd[ii][2]
								B738DR_rest_wpt_spd_tgt = B738DR_rest_wpt_spd
								break
							else
								decel_before_idx = 0
							end
						end
					end
					if B738DR_rest_wpt_spd_idx > 0 and B738DR_rest_wpt_spd_idx < offset then
						decel_dist = 90000
					end
				else
					decel_dist = 90000
				end
				
			elseif B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then		-- descent
				if td_idx == 0 then
					if math.max(first_star_idx, first_app_idx) == 0 and last_sid_idx == 0 then
						td_idx_temp = 2
					elseif math.max(first_star_idx, first_app_idx) == 0 then
						td_idx_temp = last_sid_idx + 1
					else
						td_idx_temp = math.min(first_star_idx, first_app_idx)
					end
				else
					td_idx_temp = td_idx
				end
				
				rnav_idx_temp = legs_num
				if rnav_idx_last ~= 0 then
					rnav_idx_temp = rnav_idx_last
				end
				
				
				
				
				--calcsk
				
				local table_rest_spd_n = 0
				raw_table('spd_rest_tmp')
				local spd_rest_tmp = {}
				raw_table('spd_min_tmp')
				local spd_min_tmp = {}
				raw_table('spd_max_tmp')
				local spd_max_tmp = {}
				raw_table('spd_idx_tmp')
				local spd_idx_tmp = {}
				raw_table('spd_show_idx_tmp')
				local spd_show_idx_tmp = {}
				
				local decel_dist_tmp = 0
				local decel_spd_tmp = 0
				local res_spd_app = 0
				local app_spd_used = 0
				
				local before_iaf_idx = 0
				if iaf_app_idx == 0 then
					before_iaf_idx = first_app_idx
				elseif iaf_app_idx - 1 >= first_app_idx then
					before_iaf_idx = iaf_app_idx - 1
				else
					before_iaf_idx = iaf_app_idx
				end
				
				if before_iaf_idx ~= 0 and before_iaf_idx >= td_idx_temp and before_iaf_idx <= rnav_idx_temp then
					if B738DR_fms_vref_40 == 0 then
						res_spd_app = 200
					else
						res_spd_app = B738DR_fms_vref_40 + 70
					end
				end
				
				for ii = legs_restr_spd_n, 1, -1 do
					if legs_restr_spd[ii][2] >= td_idx_temp and legs_restr_spd[ii][2] <= rnav_idx_temp and legs_restr_spd[ii][3] < B738DR_fmc_descent_speed then
						if table_rest_spd_n == 0 then
							-- first spd rest
							table_rest_spd_n = table_rest_spd_n + 1
							spd_rest_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
							spd_min_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
							spd_max_tmp[table_rest_spd_n] = B738DR_fmc_descent_speed
							spd_idx_tmp[table_rest_spd_n] = legs_restr_spd[ii][2]
							spd_show_idx_tmp[table_rest_spd_n] = 0
						else
							if app_spd_used == 0 and legs_restr_spd[ii][2] < before_iaf_idx and res_spd_app < B738DR_fmc_descent_speed then
								table_rest_spd_n = table_rest_spd_n + 1
								spd_rest_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
								spd_min_tmp[table_rest_spd_n] = res_spd_app
								spd_max_tmp[table_rest_spd_n] = B738DR_fmc_descent_speed
								spd_idx_tmp[table_rest_spd_n] = before_iaf_idx
								spd_show_idx_tmp[table_rest_spd_n] = spd_show_idx_tmp[table_rest_spd_n-1]
								
								decel_dist_tmp = legs_data[before_iaf_idx][50] - legs_data[spd_idx_tmp[table_rest_spd_n-1]][50]
								decel_spd_tmp = spd_min_tmp[table_rest_spd_n-1] + (decel_dist_tmp / DES_SPEED_COEF)
								if decel_spd_tmp > spd_max_tmp[table_rest_spd_n-1] then
									decel_spd_tmp = spd_max_tmp[table_rest_spd_n-1]
								end
								
								if decel_spd_tmp < res_spd_app then
									spd_min_tmp[table_rest_spd_n] = decel_spd_tmp
								end
								for jj = table_rest_spd_n - 1, 1, -1 do
									if spd_max_tmp[jj] > res_spd_app then
										spd_max_tmp[jj] = res_spd_app
									end
								end
								app_spd_used = 1
							end
							
							if spd_min_tmp[table_rest_spd_n] >= 100 then
								decel_dist_tmp = legs_data[legs_restr_spd[ii][2]][50] - legs_data[spd_idx_tmp[table_rest_spd_n]][50]
								decel_spd_tmp = spd_min_tmp[table_rest_spd_n] + (decel_dist_tmp / DES_SPEED_COEF)
								if decel_spd_tmp > spd_max_tmp[table_rest_spd_n] then
									if spd_max_tmp[table_rest_spd_n] >= 100 then
										decel_spd_tmp = spd_max_tmp[table_rest_spd_n]
									else
										decel_spd_tmp = B738DR_fmc_descent_speed
									end
								end
							else
								decel_spd_tmp = B738DR_fmc_descent_speed
							end
							
							table_rest_spd_n = table_rest_spd_n + 1
							spd_rest_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
							spd_min_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
							spd_max_tmp[table_rest_spd_n] = B738DR_fmc_descent_speed
							spd_idx_tmp[table_rest_spd_n] = legs_restr_spd[ii][2]
							spd_show_idx_tmp[table_rest_spd_n] = legs_restr_spd[ii][2]
							
							if legs_restr_spd[ii][4] == 0 then
								-- at
								spd_min_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
								for jj = table_rest_spd_n - 1, 1, -1 do
									if legs_restr_spd[ii][3] >= 100 then
										if spd_max_tmp[jj] > legs_restr_spd[ii][3] then
											spd_max_tmp[jj] = legs_restr_spd[ii][3]
										end
									else
										if spd_max_tmp[jj] > B738DR_fmc_descent_speed then
											spd_max_tmp[jj] = B738DR_fmc_descent_speed
										end
									end
								end
							elseif legs_restr_spd[ii][4] == 1 then
								-- below
								if decel_spd_tmp < legs_restr_spd[ii][3] then
									spd_min_tmp[table_rest_spd_n] = decel_spd_tmp
								end
								for jj = table_rest_spd_n - 1, 1, -1 do
									if legs_restr_spd[ii][3] >= 100 then
										if spd_max_tmp[jj] > legs_restr_spd[ii][3] then
											spd_max_tmp[jj] = legs_restr_spd[ii][3]
										end
									else
										if spd_max_tmp[jj] > B738DR_fmc_descent_speed then
											spd_max_tmp[jj] = B738DR_fmc_descent_speed
										end
									end
								end
							else
								-- above
								spd_min_tmp[table_rest_spd_n] = legs_restr_spd[ii][3]
							end
						end
					end
				end
				
				if app_spd_used == 0 and before_iaf_idx ~= 0 then
					if before_iaf_idx >= td_idx_temp and before_iaf_idx <= rnav_idx_temp and res_spd_app < B738DR_fmc_descent_speed then
						if table_rest_spd_n == 0 then
							decel_spd_tmp = 0
						else
							decel_dist_tmp = legs_data[before_iaf_idx][50] - legs_data[spd_idx_tmp[table_rest_spd_n]][50]
							decel_spd_tmp = spd_min_tmp[table_rest_spd_n] + (decel_dist_tmp / DES_SPEED_COEF)
							if decel_spd_tmp < spd_min_tmp[table_rest_spd_n] then
								decel_spd_tmp = spd_min_tmp[table_rest_spd_n]
							end
						end
						table_rest_spd_n = table_rest_spd_n + 1
						spd_rest_tmp[table_rest_spd_n] = res_spd_app
						spd_min_tmp[table_rest_spd_n] = res_spd_app
						spd_max_tmp[table_rest_spd_n] = B738DR_fmc_descent_speed
						spd_idx_tmp[table_rest_spd_n] = before_iaf_idx
						spd_show_idx_tmp[table_rest_spd_n] = 0
						if decel_spd_tmp < res_spd_app and decel_spd_tmp > 0 then
							spd_min_tmp[table_rest_spd_n] = decel_spd_tmp
						end
						if table_rest_spd_n > 1 then
							for jj = table_rest_spd_n - 1, 1, -1 do
								if spd_max_tmp[jj] > res_spd_app then
									spd_max_tmp[jj] = res_spd_app
								end
							end
						end
						app_spd_used = 1
					end
				end
				
				
				if B738DR_fms_test == 123 then
					B738DR_fms_test = 0
					if table_rest_spd_n > 0 then
						dump_leg_append("\nSpeed restrict table:\n")
						for ii = 1, table_rest_spd_n do
							dump_leg_append(legs_data[spd_idx_tmp[ii]][1] .. ", " .. tostring(spd_min_tmp[ii]) .. ", " .. tostring(spd_max_tmp[ii]) .. ", " .. tostring(spd_rest_tmp[ii]) .. " \n")
						end
					end
				end
				
				if table_rest_spd_n > 0 then
					if offset > spd_idx_tmp[1] then
						B738DR_rest_wpt_spd_id = legs_data[spd_idx_tmp[1]][1]
						B738DR_rest_wpt_spd = spd_rest_tmp[1]
						B738DR_rest_wpt_spd_idx = spd_idx_tmp[1]
						decel_dist = 90000
						decel_before_idx = 0
						B738DR_rest_wpt_spd_tgt = math.floor(spd_rest_tmp[1])
					else
						for ii = table_rest_spd_n, 1, -1 do
							if spd_idx_tmp[ii] >= offset then
								B738DR_rest_wpt_spd_id = legs_data[spd_idx_tmp[ii]][1]
								B738DR_rest_wpt_spd = spd_rest_tmp[ii]
								B738DR_rest_wpt_spd_idx = spd_idx_tmp[ii]
								decel_dist = 90000
								decel_before_idx = 0
								decel_dist_tmp = B738DR_fmc_dist_to_dest - legs_data[spd_idx_tmp[ii]][50]
								decel_spd_tmp = spd_min_tmp[ii] + (decel_dist_tmp / DES_SPEED_COEF)
								if decel_spd_tmp > spd_max_tmp[ii] then
									decel_spd_tmp = spd_max_tmp[ii]
								end
								B738DR_rest_wpt_spd_tgt = math.floor(decel_spd_tmp)
								break
							end
						end
					end
				end
				
			end
		end
		
		-- restrict alt
		local alt_rest_temp = 0
		-- B738DR_rest_wpt_alt_id = ""
		-- B738DR_rest_wpt_alt = 0
		-- B738DR_rest_wpt_alt_t = 0
		-- B738DR_rest_wpt_alt_idx = 0
		if legs_restr_alt_n > 0 then
			for ii = 1, legs_restr_alt_n do
				if B738DR_flight_phase < 2 or B738DR_flight_phase > 7 then		-- climb
					
					if B738DR_missed_app_act > 0 then
						td_idx_temp = legs_num
					else
						td_idx_temp = math.max(last_sid_idx, tc_idx - 1)
					end
					
					if td_idx_temp > 0 then
						if legs_restr_alt[ii][2] >= offset and legs_restr_alt[ii][2] <= td_idx_temp then
							
							if legs_restr_alt[ii][4] == 43 then	-- above
								jj = ii + 1
								if jj <= legs_restr_alt_n then
									-- find first at or below
									while legs_restr_alt[jj][2] <= td_idx_temp do
										if legs_restr_alt[jj][4] ~= 43 then	-- not above
											if legs_restr_alt[jj][4] == 41 then	-- between
												if legs_restr_alt[jj][5] == legs_restr_alt[ii][3] or (legs_restr_alt[jj][5] > legs_restr_alt[ii][3] and simDR_altitude_pilot > legs_restr_alt[ii][3]) then
													B738DR_rest_wpt_alt_id = legs_restr_alt[jj][1]
													B738DR_rest_wpt_alt = legs_restr_alt[jj][5]
													B738DR_rest_wpt_alt_t = legs_restr_alt[jj][4]
													B738DR_rest_wpt_alt_idx = legs_restr_alt[jj][2]
													break
												end
											else
												if legs_restr_alt[jj][3] == legs_restr_alt[ii][3] or (legs_restr_alt[jj][3] > legs_restr_alt[ii][3] and simDR_altitude_pilot > legs_restr_alt[ii][3]) then
													B738DR_rest_wpt_alt_id = legs_restr_alt[jj][1]
													B738DR_rest_wpt_alt = legs_restr_alt[jj][3]
													B738DR_rest_wpt_alt_t = legs_restr_alt[jj][4]
													B738DR_rest_wpt_alt_idx = legs_restr_alt[jj][2]
													break
												end
											end
										end
										jj = jj + 1
										if jj > legs_restr_alt_n then
											break
										end
									end
								end
								if B738DR_rest_wpt_alt_idx == 0 then
									B738DR_rest_wpt_alt_id = legs_restr_alt[ii][1]
									B738DR_rest_wpt_alt = legs_restr_alt[ii][3]
									B738DR_rest_wpt_alt_t = legs_restr_alt[ii][4]
									B738DR_rest_wpt_alt_idx = legs_restr_alt[ii][2]
									break
								else
									break
								end
							else
								if legs_restr_alt[ii][4] == 41 then	-- between
									B738DR_rest_wpt_alt_id = legs_restr_alt[ii][1]
									B738DR_rest_wpt_alt = legs_restr_alt[ii][5]
									B738DR_rest_wpt_alt_t = legs_restr_alt[ii][4]
									B738DR_rest_wpt_alt_idx = legs_restr_alt[ii][2]
									break
								else
									B738DR_rest_wpt_alt_id = legs_restr_alt[ii][1]
									B738DR_rest_wpt_alt = legs_restr_alt[ii][3]
									B738DR_rest_wpt_alt_t = legs_restr_alt[ii][4]
									B738DR_rest_wpt_alt_idx = legs_restr_alt[ii][2]
									break
								end
							end
						end
							
					end
				elseif B738DR_flight_phase >= 2 and B738DR_flight_phase <= 4 then
					first_desc_rest = 0
					if td_idx ~= 0 and rnav_idx_last ~= 0 then
						if legs_restr_alt[ii][2] >= offset and legs_restr_alt[ii][2] <= rnav_idx_last then
							first_desc_rest = legs_restr_alt[ii][3]
							break
						end
					end
					
				elseif B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then		-- descent
					if td_idx == 0 then
						td_idx_temp = 2
					else
						td_idx_temp = td_idx
					end
					rnav_idx_temp = legs_num
					if rnav_idx_last ~= 0 then
						rnav_idx_temp = rnav_idx_last
					end
					if legs_restr_alt[ii][2] >= offset and legs_restr_alt[ii][2] <= rnav_idx_temp and legs_restr_alt[ii][2] >= td_idx_temp then
						if legs_restr_alt[ii][4] == 45 then	-- below
							jj = ii + 1
							if jj <= legs_restr_alt_n then
								-- find first at or above
								while legs_restr_alt[jj][2] >= td_idx_temp and legs_restr_alt[jj][2] <= rnav_idx_temp do
									if legs_restr_alt[jj][4] ~= 45 then	-- not below
										if legs_restr_alt[jj][3] == legs_restr_alt[ii][3] or (legs_restr_alt[jj][3] < legs_restr_alt[ii][3] and simDR_altitude_pilot < legs_restr_alt[ii][3]) then
											B738DR_rest_wpt_alt_id = legs_restr_alt[jj][1]
											B738DR_rest_wpt_alt = legs_restr_alt[jj][3]
											B738DR_rest_wpt_alt_t = legs_restr_alt[jj][4]
											B738DR_rest_wpt_alt_idx = legs_restr_alt[jj][2]
											break
										end
									end
									jj = jj + 1
									if jj > legs_restr_alt_n then
										break
									end
								end
							end
							if B738DR_rest_wpt_alt_idx == 0 then
								B738DR_rest_wpt_alt_id = legs_restr_alt[ii][1]
								B738DR_rest_wpt_alt = legs_restr_alt[ii][3]
								B738DR_rest_wpt_alt_t = legs_restr_alt[ii][4]
								B738DR_rest_wpt_alt_idx = legs_restr_alt[ii][2]
								break
							else
								break
							end
						else
							B738DR_rest_wpt_alt_id = legs_restr_alt[ii][1]
							B738DR_rest_wpt_alt = legs_restr_alt[ii][3]
							B738DR_rest_wpt_alt_t = legs_restr_alt[ii][4]
							B738DR_rest_wpt_alt_idx = legs_restr_alt[ii][2]
							break
						end
					end
				end
			end
			
			if B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then		-- descent
				for ii = 1, legs_restr_alt_n do
					if td_idx == 0 then
						td_idx_temp = 2
					else
						td_idx_temp = td_idx
					end
					rnav_idx_temp = legs_num
					if rnav_idx_last ~= 0 then
						rnav_idx_temp = rnav_idx_last
					end
					if legs_restr_alt[ii][2] >= offset and legs_restr_alt[ii][2] <= rnav_idx_temp and legs_restr_alt[ii][2] >= td_idx_temp then
						B738DR_rest_wpt_alt_id2 = legs_restr_alt[ii][1]
						B738DR_rest_wpt_alt2 = legs_restr_alt[ii][3]
						B738DR_rest_wpt_alt_idx2 = legs_restr_alt[ii][2]
						break
					end
				end
			else
				B738DR_rest_wpt_alt_id2 = B738DR_rest_wpt_alt_id
				B738DR_rest_wpt_alt2 = B738DR_rest_wpt_alt
				B738DR_rest_wpt_alt_idx2 = B738DR_rest_wpt_alt_idx
			end
		end
		
		jj = 0
		if offset ~= 0 and B738DR_rest_wpt_alt_idx >= offset then
			if offset == B738DR_rest_wpt_alt_idx then
				jj = B738DR_fmc_dist
			elseif offset < B738DR_rest_wpt_alt_idx then
				jj = B738DR_fmc_dist + (legs_data[offset][50] - legs_data[B738DR_rest_wpt_alt_idx][50])
			end
		end
		B738DR_rest_wpt_alt_dist = jj
		
		--calc SPD and ALT
		B738DR_calc_wpt_spd = legs_data[offset][10]
		B738DR_calc_wpt_alt = legs_data[offset][11]
		
	end

end

function calc_brg_brg_fix(req_lat, req_lon, req_brg, req_lat2, req_lon2, req_brg2)
	
	-- first waypoint
	local nd_lat1 = math.rad(req_lat)
	local nd_lon1 = math.rad(req_lon)
	local nd_brg1 = req_brg --+ mag_variation_rad(nd_lat1, nd_lon1)
	
	-- second waypoint
	local nd_lat2 = math.rad(req_lat2)
	local nd_lon2 = math.rad(req_lon2)
	local nd_brg2 = req_brg2 --+ mag_variation_rad(nd_lat2, nd_lon2)
	
	local nd_lat = 0
	local nd_lon = 0
	local nd_x = 0
	local nd_y = 0
	local nd_a = 0
	local nd_b = 0
	local nd_c = 0
	local nd_xy = 0
	local nd_brg12 = 0
	local nd_brg21 = 0
	local pi = math.pi	--math.rad(180) --3.141592653589 --math.pi()
	
	-- calculate intercept two waypoints
	nd_xy = 2 * math.asin(math.sqrt((math.sin((nd_lat1-nd_lat2)/2))^2+math.cos(nd_lat1)*math.cos(nd_lat2)*math.sin((nd_lon1-nd_lon2)/2)^2))
	
	
	nd_x = math.sin(nd_lon2 - nd_lon1)
	if nd_x > 0 then
		nd_brg12 = math.acos((math.sin(nd_lat2)-math.sin(nd_lat1)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat1)))
		nd_brg21 = 2 * pi-math.acos((math.sin(nd_lat1)-math.sin(nd_lat2)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat2)))
	else
		nd_brg12 = 2 * pi-math.acos((math.sin(nd_lat2)-math.sin(nd_lat1)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat1)))
		nd_brg21 = math.acos((math.sin(nd_lat1)-math.sin(nd_lat2)*math.cos(nd_xy))/(math.sin(nd_xy)*math.cos(nd_lat2)))
	end
	
	nd_a = math.rad(((math.deg(nd_brg1) - math.deg(nd_brg12)) + 360) % 360)
	nd_b = math.rad(((math.deg(nd_brg21) - math.deg(nd_brg2)) + 360) % 360)
	
	if (math.sin(nd_a) == 0 and math.sin(nd_b) == 0) then
		-- infinity of intersections
		calc_lat = 0
		calc_lon = 0
	elseif (math.sin(nd_a) * math.sin(nd_b)) < 0 then
		-- intersection ambiguous
		calc_lat = 0
		calc_lon = 0
	else
		nd_a = math.abs(nd_a)
		nd_b = math.abs(nd_b)
		nd_c = math.acos(-math.cos(nd_a)*math.cos(nd_b)+math.sin(nd_a)*math.sin(nd_b)*math.cos(nd_xy))
		
		nd_x = math.atan2(math.sin(nd_xy)*math.sin(nd_a)*math.sin(nd_b),math.cos(nd_b)+math.cos(nd_a)*math.cos(nd_c))
		nd_lat = math.asin(math.sin(nd_lat1)*math.cos(nd_x)+math.cos(nd_lat1)*math.sin(nd_x)*math.cos(nd_brg1))
		nd_lon = math.atan2(math.sin(nd_brg1)*math.sin(nd_x)*math.cos(nd_lat1),math.cos(nd_x)-math.sin(nd_lat1)*math.sin(nd_lat))
		nd_lon = ((nd_lon1+nd_lon+pi) % (2*pi)) - pi
		
		calc_lat_fix = math.deg(nd_lat)		-- latitude
		calc_lon_fix = math.deg(nd_lon)		-- longitude
	end

end



function B738_fix_calc()
	
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local mm2 = 0
	local nn = 0
	local oo = 0
	local idx1 = 0
	local idx2 = 0
	local fix_to_dist = 0
	local fix_to_dist2 = 0
	local lat_tmp = 0
	local lon_tmp = 0
	local lat_tmp2 = 0
	local lon_tmp2 = 0
	local fix_tmp = 0
	local skip_next = 0
	local off_temp = 0
	local cur_dist = 0
	
	if fix_data_num > 0 and offset > 1 and offset <= legs_num and legs_num > 1 then
		if offset < 2 then
			off_temp = 2
		else
			off_temp = offset
		end
		for ii = 1, fix_data_num do
			for jj = 0, 2 do
				idx1 = 6 + (jj * 2)
				idx2 = 12 + (jj * 9)
				
				if calc_rte_enable_old == 0 and fix_data[ii][idx2] <= legs_num and off_temp <= legs_num then
					cur_dist = nd_calc_dist2(simDR_latitude, simDR_longitude, legs_data[offset][7], legs_data[offset][8])
					
					if fix_data[ii][idx2] > 0 then
						if calc_rte_enable ~= calc_rte_enable_old then
							fix_data[ii][idx2] = -1
							fix_data[ii][idx2+4] = -1
							fix_data[ii][idx2+5] = -1
							fix_data[ii][idx2+6] = -1	--eta
							fix_data[ii][idx2+8] = -1	--alt
						elseif fix_data[ii][idx2] == offset then
							if fix_data[ii][idx2+1] >= cur_dist then
								fix_data[ii][idx2] = -1
								fix_data[ii][idx2+4] = -1
								fix_data[ii][idx2+5] = -1
								fix_data[ii][idx2+6] = -1	--eta
								fix_data[ii][idx2+8] = -1	--alt
							end
						elseif fix_data[ii][idx2] < offset then
							fix_data[ii][idx2] = -1
							fix_data[ii][idx2+4] = -1
							fix_data[ii][idx2+5] = -1
							fix_data[ii][idx2+6] = -1	--eta
							fix_data[ii][idx2+8] = -1	--alt
						end
					end
					
					if fix_data[ii][idx2] == -1 then	-- calculate
						if fix_data[ii][idx1] ~= -1 then	-- radial
							fix_data[ii][idx2] = 0
							fix_data[ii][idx2+1] = -1
							fix_data[ii][idx2+4] = -1
							for kk = off_temp, legs_num do
								if legs_data[kk][1] ~= "DISCONTINUITY" and legs_data[kk-1][1] ~= "DISCONTINUITY" then
									
									
									lat_tmp2, lon_tmp2 = lat_lon_legs(kk)
									
									if (legs_intdir_crs == -1) and (kk == offset_seq-1) then
										if B738DR_fms_legs_df_crs[offset_seq-1] == -1 then
											-- nn = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
											-- lat_tmp = 
											lat_tmp, lon_tmp = lat_lon_legs(kk-1)
											nn = math.deg(legs_data[kk][2])
											oo = legs_data[kk][3]
										else
											if B738DR_fms_dir_seg3_end_lat == 0 and B738DR_fms_dir_seg3_end_lon == 0 then
												if B738DR_fms_dir_seg1_end_lat ~= 0 or B738DR_fms_dir_seg1_end_lon ~= 0 then
													lat_tmp = B738DR_fms_dir_seg1_end_lat
													lon_tmp = B738DR_fms_dir_seg1_end_lon
												else
													lat_tmp = B738DR_fms_dir_seg1_start_lat
													lon_tmp = B738DR_fms_dir_seg1_start_lon
												end
											else
												lat_tmp = B738DR_fms_dir_seg3_end_lat
												lon_tmp = B738DR_fms_dir_seg3_end_lon
											end
											
											nn = B738DR_fms_legs_df_crs[offset_seq-1]
											oo = nd_calc_dist2(lat_tmp, lon_tmp, lat_tmp2, lon_tmp2)
										end
									else
										lat_tmp, lon_tmp = lat_lon_legs(kk-1)
										nn = math.deg(legs_data[kk][2])
										oo = legs_data[kk][3]
									end
									
									-- lat_tmp, lon_tmp = lat_lon_legs(kk-1)
									-- calc_brg_brg_fix(lat_tmp, lon_tmp, legs_data[kk][2], fix_data[ii][3], fix_data[ii][4], math.rad((fix_data[ii][idx1] - mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360))
									-- lat_tmp, lon_tmp = lat_lon_legs(kk)
									-- mm = nd_calc_dist2(calc_lat_fix, calc_lon_fix, lat_tmp, lon_tmp)
									-- lat_tmp, lon_tmp = lat_lon_legs(kk-1)
									-- nn = nd_calc_dist2(calc_lat_fix, calc_lon_fix, lat_tmp, lon_tmp)
									-- fix_to_dist = legs_data[kk][3] - mm - nn + 0.1
									
									calc_brg_brg_fix(lat_tmp, lon_tmp, math.rad(nn), fix_data[ii][3], fix_data[ii][4], math.rad((fix_data[ii][idx1] - mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360))
									mm = nd_calc_dist2(calc_lat_fix, calc_lon_fix, lat_tmp2, lon_tmp2)
									nn = nd_calc_dist2(calc_lat_fix, calc_lon_fix, lat_tmp, lon_tmp)
									fix_to_dist = oo - mm - nn + 0.1
									
									
									if fix_to_dist > 0 then
										fix_data[ii][idx2] = kk
										fix_data[ii][idx2+1] = mm
										fix_data[ii][idx2+2] = calc_lat_fix
										fix_data[ii][idx2+3] = calc_lon_fix
										fix_data[ii][idx2+4] = nd_calc_dist2(calc_lat_fix, calc_lon_fix, fix_data[ii][3], fix_data[ii][4])
										break
									end
								end
							end
						elseif fix_data[ii][idx1+1] ~= -1 then	-- distance
							--- calc code
							fix_data[ii][idx2] = 0
							fix_data[ii][idx2+1] = -1
							fix_data[ii][idx2+5] = -1
							for kk = off_temp, legs_num do
								if legs_data[kk][1] ~= "DISCONTINUITY" and legs_data[kk-1][1] ~= "DISCONTINUITY" then
									fix_tmp = inter(fix_data[ii][3], fix_data[ii][4], fix_data[ii][idx1+1], legs_data[kk-1][7], legs_data[kk-1][8], legs_data[kk][7], legs_data[kk][8])
									if fix_tmp == 1 then
										
										lat_tmp, lon_tmp = lat_lon_legs(kk)
										mm = nd_calc_dist2(inter1_x, inter1_y, lat_tmp, lon_tmp)
										lat_tmp, lon_tmp = lat_lon_legs(kk-1)
										nn = nd_calc_dist2(inter1_x, inter1_y, lat_tmp, lon_tmp)
										fix_to_dist = legs_data[kk][3] - mm - nn + 0.1
										
										if fix_to_dist > 0 then
											if kk == offset then
												-- if mm < B738DR_rt_abeam_dist_bp then
												if mm < cur_dist then
													fix_data[ii][idx2] = kk
													fix_data[ii][idx2+1] = mm
													fix_data[ii][idx2+2] = inter1_x
													fix_data[ii][idx2+3] = inter1_y
													fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
													break
												end
											else
												fix_data[ii][idx2] = kk
												fix_data[ii][idx2+1] = mm
												fix_data[ii][idx2+2] = inter1_x
												fix_data[ii][idx2+3] = inter1_y
												fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
												break
											end
										end
									elseif fix_tmp == 2 then
										
										lat_tmp, lon_tmp = lat_lon_legs(kk)
										mm = nd_calc_dist2(inter1_x, inter1_y, lat_tmp, lon_tmp)
										lat_tmp, lon_tmp = lat_lon_legs(kk-1)
										nn = nd_calc_dist2(inter1_x, inter1_y, lat_tmp, lon_tmp)
										fix_to_dist = legs_data[kk][3] - mm - nn + 0.1
										
										skip_next = 0
										if fix_to_dist > 0 then
											skip_next = 1
										end
										
										lat_tmp, lon_tmp = lat_lon_legs(kk)
										mm2 = nd_calc_dist2(inter2_x, inter2_y, lat_tmp, lon_tmp)
										lat_tmp, lon_tmp = lat_lon_legs(kk-1)
										nn = nd_calc_dist2(inter2_x, inter2_y, lat_tmp, lon_tmp)
										fix_to_dist2 = legs_data[kk][3] - mm2 - nn + 0.1
										
										if fix_to_dist2 > 0 then
											if skip_next ~= 0 then
												if kk == offset then
													-- if mm < B738DR_rt_abeam_dist_bp and mm2 < B738DR_rt_abeam_dist_bp then
													if mm < cur_dist and mm2 < cur_dist then
														if mm > mm2 then
															fix_data[ii][idx2] = kk
															fix_data[ii][idx2+1] = mm
															fix_data[ii][idx2+2] = inter1_x
															fix_data[ii][idx2+3] = inter1_y
															fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
														else
															fix_data[ii][idx2] = kk
															fix_data[ii][idx2+1] = mm2
															fix_data[ii][idx2+2] = inter2_x
															fix_data[ii][idx2+3] = inter2_y
															fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter2_x, inter2_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
														end
													-- elseif mm < B738DR_rt_abeam_dist_bp then
													elseif mm < cur_dist then
														fix_data[ii][idx2] = kk
														fix_data[ii][idx2+1] = mm
														fix_data[ii][idx2+2] = inter1_x
														fix_data[ii][idx2+3] = inter1_y
														fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
													-- elseif mm2 < B738DR_rt_abeam_dist_bp then
													elseif mm2 < cur_dist then
														fix_data[ii][idx2] = kk
														fix_data[ii][idx2+1] = mm2
														fix_data[ii][idx2+2] = inter2_x
														fix_data[ii][idx2+3] = inter2_y
														fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter2_x, inter2_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
													end
												else
													if mm > mm2 then
														fix_data[ii][idx2] = kk
														fix_data[ii][idx2+1] = mm
														fix_data[ii][idx2+2] = inter1_x
														fix_data[ii][idx2+3] = inter1_y
														fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
													else
														fix_data[ii][idx2] = kk
														fix_data[ii][idx2+1] = mm2
														fix_data[ii][idx2+2] = inter2_x
														fix_data[ii][idx2+3] = inter2_y
														fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter2_x, inter2_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
													end
													break
												end
											else
												fix_data[ii][idx2] = kk
												fix_data[ii][idx2+1] = mm2
												fix_data[ii][idx2+2] = inter2_x
												fix_data[ii][idx2+3] = inter2_y
												fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter2_x, inter2_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
												break
											end
										else
											if skip_next ~= 0 then
												if kk == offset then
													-- if mm < B738DR_rt_abeam_dist_bp then
													if mm < cur_dist then
														fix_data[ii][idx2] = kk
														fix_data[ii][idx2+1] = mm
														fix_data[ii][idx2+2] = inter1_x
														fix_data[ii][idx2+3] = inter1_y
														fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
														break
													end
												else
													fix_data[ii][idx2] = kk
													fix_data[ii][idx2+1] = mm
													fix_data[ii][idx2+2] = inter1_x
													fix_data[ii][idx2+3] = inter1_y
													fix_data[ii][idx2+5] = (nd_calc_brg(fix_data[ii][3], fix_data[ii][4], inter1_x, inter1_y) + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
													break
												end
											end
										end
									end
								end
							end
						end
					elseif fix_data[ii][idx2] > 1 then
						if perf_exec > 0 then
							-- calculate ETA
							local n = fix_data[ii][idx2]
							local speed_temp1 = 0
							local speed_temp2 = 0
							local dist_temp = 0
							
							if n == 1 then
								dist_temp = B738DR_rt_abeam_dist_bp		--B738DR_fmc_dist
								if AP_airspeed < 160 then
									speed_temp1 = 160
									speed_temp1 = ias_to_tas(160, simDR_altitude_pilot)
								else
									speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
								end
								speed_temp2 = speed_temp1
							elseif n == offset then
								dist_temp = B738DR_rt_abeam_dist_bp		--B738DR_fmc_dist
								if AP_airspeed < 160 then
									speed_temp1 = ias_to_tas(160, legs_data[n][11])
									speed_temp2 = ias_to_tas(160, legs_data[n][11])
								else
									speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
									speed_temp2 = ias_to_tas(AP_airspeed, legs_data[n][11])
								end
							else
								dist_temp = wpt_distance(n)
								if legs_data[n][10] == 0 then
									speed_temp1 = 160
									speed_temp2 = 160
								elseif legs_data[n][10] < 100 then
									speed_temp1 = mach_to_tas(legs_data[n][10], legs_data[n-1][11])
									speed_temp2 = mach_to_tas(legs_data[n][10], legs_data[n][11])
								else
									speed_temp1 = ias_to_tas(legs_data[n][10], legs_data[n-1][11])
									speed_temp2 = ias_to_tas(legs_data[n][10], legs_data[n][11])
								end
							end
							
							local time_zulu = simDR_zulu_hours + (simDR_zulu_minutes/60) + (simDR_zulu_seconds/3600)
							
							speed_temp1 = (speed_temp1 + speed_temp2) / 2
							
							local tmp_wind_spd = 0
							local tmp_wind_dir = 0
							if legs_data[n][39] == "" then
								if legs_data[n][38] ~= "" then
									tmp_wind_dir = tonumber(string.sub(legs_data[n][38], 1, 3))
									tmp_wind_spd = tonumber(string.sub(legs_data[n][38], -3, -1))
								end
							else
								tmp_wind_dir = tonumber(string.sub(legs_data[n][39], 1, 3))
								tmp_wind_spd = tonumber(string.sub(legs_data[n][39], -3, -1))
							end
							
							local wc_speed = 0
							if tmp_wind_spd > 0 then
								wc_speed = tmp_wind_spd * math.cos(math.rad(Angle180(tmp_wind_dir))-legs_data[n][2])
							end
							speed_temp1 = speed_temp1 + wc_speed
							
							if speed_temp1 == 0 then
								speed_temp1 = 160
							end
							
							dist_temp = dist_temp - fix_data[ii][idx2+1]
							if dist_temp < 0 then
								dist_temp = 0
							end
							
							local time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
							time_temp = time_temp / 3600	-- hours
							
							if n == offset or n == 1 then
								time_temp = (time_zulu + time_temp) % 24
							else
								time_temp = (legs_data[n-1][13] + time_temp) % 24
							end
							
							fix_data[ii][idx2+6] = time_temp
							
							-- calculate ALT
							local calc_alt_mode = 0
							if tc_idx == 0 and td_idx == 0 then
								fix_data[ii][idx2+8] = -1	--alt
							elseif tc_idx == 0 then
								if n < td_idx then
									fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
								elseif n == td_idx then
									if fix_data[ii][idx2+1] >= td_dist then
										fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
									else
										calc_alt_mode = 2
									end
								else
									calc_alt_mode = 2
								end
							elseif td_idx == 0 then
								if n < tc_idx then
									calc_alt_mode = 1
								elseif n == tc_idx then
									if fix_data[ii][idx2+1] <= tc_dist then
										fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
									else
										calc_alt_mode = 1
									end
								else
									fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
								end
							else
								if tc_idx == td_idx and n == td_idx then
									if fix_data[ii][idx2+1] > tc_dist then
										calc_alt_mode = 1
									elseif fix_data[ii][idx2+1] < td_dist then
										calc_alt_mode = 2
									else
										fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
									end
								elseif n == tc_idx then
									if fix_data[ii][idx2+1] > tc_dist then
										calc_alt_mode = 1
									else
										fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
									end
								elseif n == td_idx then
									if fix_data[ii][idx2+1] < td_dist then
										calc_alt_mode = 2
									else
										fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
									end
								elseif n < tc_idx then
									calc_alt_mode = 1
								elseif n > td_idx then
									calc_alt_mode = 2
								else
									fix_data[ii][idx2+8] = B738DR_fmc_cruise_alt
								end
							end
							if calc_alt_mode == 1 then
								-- climb mode
								if legs_data[n][3] == 0 then
									fix_data[ii][idx2+8] = -1	--alt
								else
									fix_data[ii][idx2+8] = legs_data[n][11] - ((legs_data[n][11] - legs_data[n-1][11]) * (fix_data[ii][idx2+1] / legs_data[n][3]))
									if n == offset and fix_data[ii][idx2+8] < simDR_altitude_pilot then
										fix_data[ii][idx2+8] = simDR_altitude_pilot
									elseif fix_data[ii][idx2+8] < legs_data[n-1][11] then
										fix_data[ii][idx2+8] = legs_data[n-1][11]
									end
								end
							elseif calc_alt_mode == 2 then
								-- descent mode
								fix_data[ii][idx2+8] = -1	--alt
								if ed_found ~= 0 and n <= ed_found then
									local ed_found_temp = ed_found
									local ed_alt_temp = ed_alt
									local descent_vpa = ed_vpa
									local ed_found_type = ed_type
									local ed_table_temp = 1
									local idle_pth_idx = 0
									
									if n == ed_found then
										fix_to_dist = fix_data[ii][idx2+1]
									elseif n < ed_found then
										fix_to_dist = fix_data[ii][idx2+1] + (legs_data[n][50] - legs_data[ed_found][50])
									end
									
									if ed_fix_num > 0 then
										for nn = 1, ed_fix_num do
											if fix_to_dist > ed_fix_distance[nn] then
												ed_found_temp = ed_fix_found2[nn]
												ed_alt_temp = ed_fix_alt2[nn]
												descent_vpa = ed_fix_vpa2[nn]
												ed_found_type = ed_fix_type[nn]
												ed_table_temp = ed_fix_table[nn]
												idle_pth_idx = nn
												break
											end
										end
									end
									
									if idle_pth_idx ~= 0 then
										fix_to_dist = fix_to_dist - ed_fix_distance[idle_pth_idx]
									end
									
									if ed_fix_num == 0 then
										if ed_found_type == 0 then
											mm = calc_vnav_pth_alt(ed_alt_temp, fix_to_dist, 1)
										else
											-- geometric path
											mm = ed_alt_temp + ((fix_to_dist * (math.tan(math.rad(descent_vpa)))) * 6076.11549) -- ft
										end
									else
										if ed_found_type == 0 then
											-- idle path
											mm = calc_vnav_pth_alt(ed_alt_temp, fix_to_dist, ed_table_temp)
										else
											-- geometric path
											mm = ed_alt_temp + ((fix_to_dist * (math.tan(math.rad(descent_vpa)))) * 6076.11549) -- ft
										end
									end
									
									if ed_fix_num > 0 then
										if idle_pth_idx == 0 then
											if mm > ed_fix_alt2[1] then
												mm = ed_fix_alt2[1]
											end
										elseif idle_pth_idx > 0 and ed_fix_num > idle_pth_idx then
											if mm > ed_fix_alt2[idle_pth_idx+1] then
												mm = ed_fix_alt2[idle_pth_idx+1]
											end
										end
									end
									if mm > B738DR_fmc_cruise_alt then
										mm = B738DR_fmc_cruise_alt
									end
									
									fix_data[ii][idx2+8] = mm	--alt
									
									
								elseif n > ed_found and n <= rnav_idx_last and rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
									
									local lat_tmp = 0
									local lon_tmp = 0
									local rnav_alt2 = 0
									local rw_dist_temp = 0
									local rnav_vpa_temp = 0
									local rw_dist = 0
									local rnav_vpa_temp2 = 0
					
									if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
										if string.sub(legs_data[rnav_idx_last][1], 1, 2) == "RW" then
											rnav_alt2 = rnav_alt
										else
											if B738DR_dest_runway_start_lat ~= 0 and B738DR_dest_runway_start_lon ~= 0 then
												lat_tmp, lon_tmp = lat_lon_legs(rnav_idx_last)
												rw_dist_temp = nd_calc_dist2(lat_tmp, lon_tmp, B738DR_dest_runway_start_lat, B738DR_dest_runway_start_lon)
												rnav_alt2 = B738DR_dest_runway_alt + 50 --+50
											end
										end
									end
									if rnav_alt2 ~= 0 and n <= rnav_idx_last then
										
										if rnav_idx_last == rnav_idx_first + 1 then
											rw_dist = rw_dist_temp + wpt_distance(rnav_idx_last)
											if rw_dist ~= 0 and legs_data[rnav_idx_first][5] > rnav_alt2 then
												rnav_vpa_temp = math.atan((legs_data[rnav_idx_first][5] - rnav_alt2) / (rw_dist * 6076.11549))
											end
											if rnav_vpa_temp == 0 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp < 2.75 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp > 3.77 then
												rnav_vpa_temp = 3.77
											end
										elseif rnav_idx_last == rnav_idx_first then
											rnav_vpa_temp = 3
										else
											rw_dist = rw_dist_temp
											for nn = rnav_idx_last, rnav_idx_first + 1, -1 do
												rw_dist = rw_dist + wpt_distance(nn)
											end
											if rw_dist ~= 0 and legs_data[rnav_idx_first][5] > rnav_alt2 then
												rnav_vpa_temp = math.atan((legs_data[rnav_idx_first][5] - rnav_alt2) / (rw_dist * 6076.11549))
											end
											if rnav_vpa_temp == 0 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp < 2.75 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp > 3.77 then
												rnav_vpa_temp = 3.77
											end
											rw_dist = rw_dist_temp
											for nn = rnav_idx_last, rnav_idx_first + 1, -1 do
												rw_dist = rw_dist + wpt_distance(nn)
												if legs_data[nn-1][5] ~= 0 and rw_dist ~= 0 and legs_data[nn-1][5] > rnav_alt2 then
													rnav_vpa_temp2 = math.atan((legs_data[nn-1][5] - rnav_alt2) / (rw_dist * 6076.11549))
													rnav_vpa_temp2 = math.deg(rnav_vpa_temp2)
													if rnav_vpa_temp2 > rnav_vpa_temp then
														rnav_vpa_temp = rnav_vpa_temp2
													end
												end
											end
											if rnav_vpa_temp == 0 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp < 2.75 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp > 3.77 then
												rnav_vpa_temp = 3.77
											end
										end
										if rnav_vpa < 0 and rnav_vpa_temp < -rnav_vpa then
											rnav_vpa_temp = -rnav_vpa
										end
										
										-- calc track distance to RW
										rw_dist = rw_dist_temp
										if n == rnav_idx_last then
											rw_dist = rw_dist + fix_data[ii][idx2+1]
										elseif n < rnav_idx_last then
											rw_dist = rw_dist + fix_data[ii][idx2+1] + (legs_data[n][50] - legs_data[rnav_idx_last][50])
										end
										
										mm = rnav_alt2 + ((rw_dist * (math.tan(math.rad(rnav_vpa_temp)))) * 6076.11549)	-- (in ft)
										
										if mm > ed_alt and ed_found ~= 0 then
											mm = ed_alt
										end
										if mm > B738DR_fmc_cruise_alt then
											mm = B738DR_fmc_cruise_alt
										end
										
										fix_data[ii][idx2+8] = mm	--alt
										
									end
									
								end
								
							end
						else
							fix_data[ii][idx2+6] = -1	--eta
							fix_data[ii][idx2+8] = -1	--alt
						end
						
					end
				else
					fix_data[ii][idx2] = -1
					fix_data[ii][idx2+4] = -1
					fix_data[ii][idx2+5] = -1
					fix_data[ii][idx2+6] = -1	--eta
					fix_data[ii][idx2+8] = -1	--alt
				end
			end
			
			-- Calc ABM
			-- if fix_abm_act[ii] == 0 then
				-- fix_abm_wpt_idx[ii] = -1
				-- fix_abm_wpt_dist[ii] = -1
				-- fix_abm_lat[ii] = -1
				-- fix_abm_lon[ii] = -1
				-- fix_abm_brg[ii] = -1
				-- fix_abm_dist[ii] = -1
				-- fix_abm_eta[ii] = -1
				-- fix_abm_alt[ii] = -1
			-- else
				if calc_rte_enable_old == 0 and fix_abm_wpt_idx[ii] <= legs_num and off_temp <= legs_num then
					cur_dist = nd_calc_dist2(simDR_latitude, simDR_longitude, legs_data[offset][7], legs_data[offset][8])
					
					if fix_abm_wpt_idx[ii] > 0 then
						if calc_rte_enable ~= calc_rte_enable_old then
							fix_data[ii][idx2] = -1
							fix_data[ii][idx2+4] = -1
							fix_data[ii][idx2+5] = -1
							fix_data[ii][idx2+6] = -1	--eta
							fix_data[ii][idx2+8] = -1	--alt
						elseif fix_abm_wpt_idx[ii] == offset then
							if fix_abm_wpt_dist[ii] >= cur_dist then
								fix_abm_act[ii] = 0
								fix_abm_wpt_idx[ii] = -1
								fix_abm_wpt_dist[ii] = -1
								fix_abm_lat[ii] = -1
								fix_abm_lon[ii] = -1
								fix_abm_brg[ii] = -1
								fix_abm_dist[ii] = -1
								fix_abm_eta[ii] = -1
								fix_abm_alt[ii] = -1
							end
						elseif fix_abm_wpt_idx[ii] < offset then
							fix_abm_act[ii] = 0
							fix_abm_wpt_idx[ii] = -1
							fix_abm_wpt_dist[ii] = -1
							fix_abm_lat[ii] = -1
							fix_abm_lon[ii] = -1
							fix_abm_brg[ii] = -1
							fix_abm_dist[ii] = -1
							fix_abm_eta[ii] = -1
							fix_abm_alt[ii] = -1
						end
					end
					
					if fix_abm_wpt_idx[ii] < 0 then  --calc
						fix_abm_act[ii] = 0
						fix_abm_wpt_idx[ii] = 0
						fix_abm_wpt_dist[ii] = -1
						fix_abm_lat[ii] = -1
						fix_abm_lon[ii] = -1
						fix_abm_brg[ii] = -1
						fix_abm_dist[ii] = -1
						fix_abm_eta[ii] = -1
						fix_abm_alt[ii] = -1
						
						for kk = off_temp, legs_num do
							if legs_data[kk][1] ~= "DISCONTINUITY" and legs_data[kk-1][1] ~= "DISCONTINUITY" then
								
								lat_tmp2, lon_tmp2 = lat_lon_legs(kk)
								
								if (legs_intdir_crs == -1) and (kk == offset_seq-1) then
									if B738DR_fms_legs_df_crs[offset_seq-1] == -1 then
										-- nn = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
										-- lat_tmp = 
										lat_tmp, lon_tmp = lat_lon_legs(kk-1)
										nn = math.deg(legs_data[kk][2])
										oo = legs_data[kk][3]
									else
										if B738DR_fms_dir_seg3_end_lat == 0 and B738DR_fms_dir_seg3_end_lon == 0 then
											if B738DR_fms_dir_seg1_end_lat ~= 0 or B738DR_fms_dir_seg1_end_lon ~= 0 then
												lat_tmp = B738DR_fms_dir_seg1_end_lat
												lon_tmp = B738DR_fms_dir_seg1_end_lon
											else
												lat_tmp = B738DR_fms_dir_seg1_start_lat
												lon_tmp = B738DR_fms_dir_seg1_start_lon
											end
										else
											lat_tmp = B738DR_fms_dir_seg3_end_lat
											lon_tmp = B738DR_fms_dir_seg3_end_lon
										end
										
										nn = B738DR_fms_legs_df_crs[offset_seq-1]
										oo = nd_calc_dist2(lat_tmp, lon_tmp, lat_tmp2, lon_tmp2)
									end
								else
									lat_tmp, lon_tmp = lat_lon_legs(kk-1)
									nn = math.deg(legs_data[kk][2])
									oo = legs_data[kk][3]
								end
								
								-- lat_tmp, lon_tmp = lat_lon_legs(kk-1)
								-- nn = math.deg(legs_data[kk][2])
								
								mm = nd_calc_brg(lat_tmp, lon_tmp, fix_data[ii][3], fix_data[ii][4])
								mm = (mm - nn + 360) % 360
								if mm > 180 then
									mm = mm - 360
								end
								if mm < 0 then
									mm = (nn + 90) % 360
								else
									mm = (nn + 270) % 360
								end
								fix_abm_brg[ii] = (mm + mag_variation_deg(fix_data[ii][3], fix_data[ii][4]) + 360) % 360
								
								
								calc_brg_brg_fix(lat_tmp, lon_tmp, math.rad(nn), fix_data[ii][3], fix_data[ii][4], math.rad(mm))
								-- lat_tmp2, lon_tmp2 = lat_lon_legs(kk)
								mm = nd_calc_dist2(calc_lat_fix, calc_lon_fix, lat_tmp2, lon_tmp2)
								-- lat_tmp, lon_tmp = lat_lon_legs(kk-1)
								nn = nd_calc_dist2(calc_lat_fix, calc_lon_fix, lat_tmp, lon_tmp)
								fix_to_dist = oo - mm - nn + 0.1
								
								-- if kk == off_temp then
									-- B738DR_fms_test3 = math.deg(legs_data[kk][2])
									-- B738DR_fms_test4 = fix_abm_brg[ii]
								-- end
								
								if fix_to_dist > 0 then
									fix_abm_wpt_idx[ii] = kk
									fix_abm_wpt_dist[ii] = mm
									fix_abm_lat[ii] = calc_lat_fix
									fix_abm_lon[ii] = calc_lon_fix
									-- fix_abm_brg[ii] = -1
									fix_abm_dist[ii] = nd_calc_dist2(calc_lat_fix, calc_lon_fix, fix_data[ii][3], fix_data[ii][4])
									-- fix_abm_eta[ii] = -1
									-- fix_abm_alt[ii] = -1
									break
								end
							end
						end
					
					
					
					
					elseif fix_abm_wpt_idx[ii] > 1 then
						if perf_exec > 0 then
							
							-- B738DR_fms_test3 = 1
							
							-- calculate ETA
							local n = fix_abm_wpt_idx[ii]
							local speed_temp1 = 0
							local speed_temp2 = 0
							local dist_temp = 0
							
							if n == 1 then
								dist_temp = B738DR_rt_abeam_dist_bp		--B738DR_fmc_dist
								if AP_airspeed < 160 then
									speed_temp1 = 160
									speed_temp1 = ias_to_tas(160, simDR_altitude_pilot)
								else
									speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
								end
								speed_temp2 = speed_temp1
							elseif n == offset then
								dist_temp = B738DR_rt_abeam_dist_bp		--B738DR_fmc_dist
								if AP_airspeed < 160 then
									speed_temp1 = ias_to_tas(160, legs_data[n][11])
									speed_temp2 = ias_to_tas(160, legs_data[n][11])
								else
									speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
									speed_temp2 = ias_to_tas(AP_airspeed, legs_data[n][11])
								end
							else
								dist_temp = wpt_distance(n)
								if legs_data[n][10] == 0 then
									speed_temp1 = 160
									speed_temp2 = 160
								elseif legs_data[n][10] < 100 then
									speed_temp1 = mach_to_tas(legs_data[n][10], legs_data[n-1][11])
									speed_temp2 = mach_to_tas(legs_data[n][10], legs_data[n][11])
								else
									speed_temp1 = ias_to_tas(legs_data[n][10], legs_data[n-1][11])
									speed_temp2 = ias_to_tas(legs_data[n][10], legs_data[n][11])
								end
							end
							
							-- B738DR_fms_test3 = 2
							
							local time_zulu = simDR_zulu_hours + (simDR_zulu_minutes/60) + (simDR_zulu_seconds/3600)
							
							speed_temp1 = (speed_temp1 + speed_temp2) / 2
							
							local tmp_wind_spd = 0
							local tmp_wind_dir = 0
							if legs_data[n][39] == "" then
								if legs_data[n][38] ~= "" then
									tmp_wind_dir = tonumber(string.sub(legs_data[n][38], 1, 3))
									tmp_wind_spd = tonumber(string.sub(legs_data[n][38], -3, -1))
								end
							else
								tmp_wind_dir = tonumber(string.sub(legs_data[n][39], 1, 3))
								tmp_wind_spd = tonumber(string.sub(legs_data[n][39], -3, -1))
							end
							
							-- B738DR_fms_test3 = 3
							
							local wc_speed = 0
							if tmp_wind_spd > 0 then
								wc_speed = tmp_wind_spd * math.cos(math.rad(Angle180(tmp_wind_dir))-legs_data[n][2])
							end
							speed_temp1 = speed_temp1 + wc_speed
							
							if speed_temp1 == 0 then
								speed_temp1 = 160
							end
							
							dist_temp = dist_temp - fix_abm_wpt_dist[ii]
							if dist_temp < 0 then
								dist_temp = 0
							end
							
							local time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
							time_temp = time_temp / 3600	-- hours
							
							-- B738DR_fms_test3 = 4
							
							if n == offset or n == 1 then
								time_temp = (time_zulu + time_temp) % 24
							else
								time_temp = (legs_data[n-1][13] + time_temp) % 24
							end
							
							fix_abm_eta[ii] = time_temp
							
							-- B738DR_fms_test3 = 5
							
							-- calculate ALT
							local calc_alt_mode = 0
							if tc_idx == 0 and td_idx == 0 then
								fix_abm_alt[ii] = -1	--alt
							elseif tc_idx == 0 then
								if n < td_idx then
									fix_abm_alt[ii] = B738DR_fmc_cruise_alt
								elseif n == td_idx then
									if fix_abm_wpt_dist[ii] >= td_dist then
										fix_abm_alt[ii] = B738DR_fmc_cruise_alt
									else
										calc_alt_mode = 2
									end
								else
									calc_alt_mode = 2
								end
							elseif td_idx == 0 then
								if n < tc_idx then
									calc_alt_mode = 1
								elseif n == tc_idx then
									if fix_abm_wpt_dist[ii] <= tc_dist then
										fix_abm_alt[ii] = B738DR_fmc_cruise_alt
									else
										calc_alt_mode = 1
									end
								else
									fix_abm_alt[ii] = B738DR_fmc_cruise_alt
								end
							else
								if tc_idx == td_idx and n == td_idx then
									if fix_abm_wpt_dist[ii] > tc_dist then
										calc_alt_mode = 1
									elseif fix_abm_wpt_dist[ii] < td_dist then
										calc_alt_mode = 2
									else
										fix_abm_alt[ii] = B738DR_fmc_cruise_alt
									end
								elseif n == tc_idx then
									if fix_abm_wpt_dist[ii] > tc_dist then
										calc_alt_mode = 1
									else
										fix_abm_alt[ii] = B738DR_fmc_cruise_alt
									end
								elseif n == td_idx then
									if fix_abm_wpt_dist[ii] < td_dist then
										calc_alt_mode = 2
									else
										fix_abm_alt[ii] = B738DR_fmc_cruise_alt
									end
								elseif n < tc_idx then
									calc_alt_mode = 1
								elseif n > td_idx then
									calc_alt_mode = 2
								else
									fix_abm_alt[ii] = B738DR_fmc_cruise_alt
								end
							end
							
							-- B738DR_fms_test3 = 6
							
							if calc_alt_mode == 1 then
								-- climb mode
								if legs_data[n][3] == 0 then
									fix_abm_alt[ii] = -1	--alt
								else
									fix_abm_alt[ii] = legs_data[n][11] - ((legs_data[n][11] - legs_data[n-1][11]) * (fix_abm_wpt_dist[ii] / legs_data[n][3]))
									if n == offset and fix_abm_alt[ii] < simDR_altitude_pilot then
										fix_abm_alt[ii] = simDR_altitude_pilot
									elseif fix_abm_alt[ii] < legs_data[n-1][11] then
										fix_abm_alt[ii] = legs_data[n-1][11]
									end
								end
								
								-- B738DR_fms_test3 = 7
								
							elseif calc_alt_mode == 2 then
								-- descent mode
								fix_abm_alt[ii] = -1	--alt
								if ed_found ~= 0 and n <= ed_found then
									local ed_found_temp = ed_found
									local ed_alt_temp = ed_alt
									local descent_vpa = ed_vpa
									local ed_found_type = ed_type
									local ed_table_temp = 1
									local idle_pth_idx = 0
									
									if n == ed_found then
										fix_to_dist = fix_abm_wpt_dist[ii]
									elseif n < ed_found then
										fix_to_dist = fix_abm_wpt_dist[ii] + (legs_data[n][50] - legs_data[ed_found][50])
									end
									
									if ed_fix_num > 0 then
										for nn = 1, ed_fix_num do
											if fix_to_dist > ed_fix_distance[nn] then
												ed_found_temp = ed_fix_found2[nn]
												ed_alt_temp = ed_fix_alt2[nn]
												descent_vpa = ed_fix_vpa2[nn]
												ed_found_type = ed_fix_type[nn]
												ed_table_temp = ed_fix_table[nn]
												idle_pth_idx = nn
												break
											end
										end
									end
									
									if idle_pth_idx ~= 0 then
										fix_to_dist = fix_to_dist - ed_fix_distance[idle_pth_idx]
									end
									
									if ed_fix_num == 0 then
										if ed_found_type == 0 then
											mm = calc_vnav_pth_alt(ed_alt_temp, fix_to_dist, 1)
										else
											-- geometric path
											mm = ed_alt_temp + ((fix_to_dist * (math.tan(math.rad(descent_vpa)))) * 6076.11549) -- ft
										end
									else
										if ed_found_type == 0 then
											-- idle path
											mm = calc_vnav_pth_alt(ed_alt_temp, fix_to_dist, ed_table_temp)
										else
											-- geometric path
											mm = ed_alt_temp + ((fix_to_dist * (math.tan(math.rad(descent_vpa)))) * 6076.11549) -- ft
										end
									end
									
									if ed_fix_num > 0 then
										if idle_pth_idx == 0 then
											if mm > ed_fix_alt2[1] then
												mm = ed_fix_alt2[1]
											end
										elseif idle_pth_idx > 0 and ed_fix_num > idle_pth_idx then
											if mm > ed_fix_alt2[idle_pth_idx+1] then
												mm = ed_fix_alt2[idle_pth_idx+1]
											end
										end
									end
									if mm > B738DR_fmc_cruise_alt then
										mm = B738DR_fmc_cruise_alt
									end
									
									fix_abm_alt[ii] = mm	--alt
									
									
								elseif n > ed_found and n <= rnav_idx_last and rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
									
									local lat_tmp = 0
									local lon_tmp = 0
									local rnav_alt2 = 0
									local rw_dist_temp = 0
									local rnav_vpa_temp = 0
									local rw_dist = 0
									local rnav_vpa_temp2 = 0
					
									if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
										if string.sub(legs_data[rnav_idx_last][1], 1, 2) == "RW" then
											rnav_alt2 = rnav_alt
										else
											if B738DR_dest_runway_start_lat ~= 0 and B738DR_dest_runway_start_lon ~= 0 then
												lat_tmp, lon_tmp = lat_lon_legs(rnav_idx_last)
												rw_dist_temp = nd_calc_dist2(lat_tmp, lon_tmp, B738DR_dest_runway_start_lat, B738DR_dest_runway_start_lon)
												rnav_alt2 = B738DR_dest_runway_alt + 50 --+50
											end
										end
									end
									if rnav_alt2 ~= 0 and n <= rnav_idx_last then
										
										if rnav_idx_last == rnav_idx_first + 1 then
											rw_dist = rw_dist_temp + wpt_distance(rnav_idx_last)
											if rw_dist ~= 0 and legs_data[rnav_idx_first][5] > rnav_alt2 then
												rnav_vpa_temp = math.atan((legs_data[rnav_idx_first][5] - rnav_alt2) / (rw_dist * 6076.11549))
											end
											if rnav_vpa_temp == 0 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp < 2.75 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp > 3.77 then
												rnav_vpa_temp = 3.77
											end
										elseif rnav_idx_last == rnav_idx_first then
											rnav_vpa_temp = 3
										else
											rw_dist = rw_dist_temp
											for nn = rnav_idx_last, rnav_idx_first + 1, -1 do
												rw_dist = rw_dist + wpt_distance(nn)
											end
											if rw_dist ~= 0 and legs_data[rnav_idx_first][5] > rnav_alt2 then
												rnav_vpa_temp = math.atan((legs_data[rnav_idx_first][5] - rnav_alt2) / (rw_dist * 6076.11549))
											end
											if rnav_vpa_temp == 0 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp < 2.75 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp > 3.77 then
												rnav_vpa_temp = 3.77
											end
											rw_dist = rw_dist_temp
											for nn = rnav_idx_last, rnav_idx_first + 1, -1 do
												rw_dist = rw_dist + wpt_distance(nn)
												if legs_data[nn-1][5] ~= 0 and rw_dist ~= 0 and legs_data[nn-1][5] > rnav_alt2 then
													rnav_vpa_temp2 = math.atan((legs_data[nn-1][5] - rnav_alt2) / (rw_dist * 6076.11549))
													rnav_vpa_temp2 = math.deg(rnav_vpa_temp2)
													if rnav_vpa_temp2 > rnav_vpa_temp then
														rnav_vpa_temp = rnav_vpa_temp2
													end
												end
											end
											if rnav_vpa_temp == 0 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp < 2.75 then
												rnav_vpa_temp = 3
											elseif rnav_vpa_temp > 3.77 then
												rnav_vpa_temp = 3.77
											end
										end
										if rnav_vpa < 0 and rnav_vpa_temp < -rnav_vpa then
											rnav_vpa_temp = -rnav_vpa
										end
										
										-- calc track distance to RW
										rw_dist = rw_dist_temp
										if n == rnav_idx_last then
											rw_dist = rw_dist + fix_abm_wpt_dist[ii]
										elseif n < rnav_idx_last then
											rw_dist = rw_dist + fix_abm_wpt_dist[ii] + (legs_data[n][50] - legs_data[rnav_idx_last][50])
										end
										
										mm = rnav_alt2 + ((rw_dist * (math.tan(math.rad(rnav_vpa_temp)))) * 6076.11549)	-- (in ft)
										
										if mm > ed_alt and ed_found ~= 0 then
											mm = ed_alt
										end
										if mm > B738DR_fmc_cruise_alt then
											mm = B738DR_fmc_cruise_alt
										end
										
										fix_abm_alt[ii] = mm	--alt
										
									end
									
								end
								-- B738DR_fms_test3 = 8
							end
							-- B738DR_fms_test3 = 9
						else
							fix_abm_eta[ii] = -1
							fix_abm_alt[ii] = -1
						end
					
					end
					
				else
					fix_abm_act[ii] = 0
					fix_abm_wpt_idx[ii] = -1
					fix_abm_wpt_dist[ii] = -1
					fix_abm_lat[ii] = -1
					fix_abm_lon[ii] = -1
					fix_abm_brg[ii] = -1
					fix_abm_dist[ii] = -1
					fix_abm_eta[ii] = -1
					fix_abm_alt[ii] = -1
				end
				
			-- end
		end
	else
		for ii = 1, 5 do
			fix_abm_act[ii] = 0
			fix_abm_wpt_idx[ii] = -1
			fix_abm_wpt_dist[ii] = -1
			fix_abm_lat[ii] = -1
			fix_abm_lon[ii] = -1
			fix_abm_brg[ii] = -1
			fix_abm_dist[ii] = -1
			fix_abm_eta[ii] = -1
			fix_abm_alt[ii] = -1
		end
	end
	for ii = 1, 5 do
		B738DR_fix_abm_act[ii-1] = fix_abm_act[ii]
		B738DR_fix_abm_brg[ii-1] = fix_abm_brg[ii]
		B738DR_fix_abm_dist[ii-1] = fix_abm_dist[ii]
	end
end


function pfd_ils(in_dspl)

	local pfd_txt = ""
	local pfd_txt_a = ""
	local pfd_txt2 = ""
	local nav_tuned_no_id = 0
	local nav_nav_id = ""
	local nav_id_decode = 0
	local pfd_ils_id_disagree = 0
	local pfd_ils_crs_disagree = 0
	local nav_freq = 0
	local nav_has_dme = 0
	local nav_dme = 0
	local pfd_course = 0
	
	if in_dspl == 0 then
		nav_tuned_no_id = B738DR_nav_tuned_no_id
		nav_nav_id = B738DR_nav_nav_id
		nav_id_decode = B738DR_nav_id_decode
		pfd_ils_id_disagree = ils_cpt_id_disagree
		pfd_ils_crs_disagree = ils_cpt_crs_disagree
		nav_freq = B738DR_nav_freq
		nav_has_dme = B738DR_nav_has_dme
		nav_dme = B738DR_nav_dme
		pfd_course = B738DR_course_pilot
	else
		nav_tuned_no_id = B738DR_nav_tuned_no_id_fo
		nav_nav_id = B738DR_nav_nav_id_fo
		nav_id_decode = B738DR_nav_id_decode_fo
		pfd_ils_id_disagree = ils_fo_id_disagree
		pfd_ils_crs_disagree = ils_fo_crs_disagree
		nav_freq = B738DR_nav_freq_fo
		nav_has_dme = B738DR_nav_has_dme_fo
		nav_dme = B738DR_nav_dme_fo
		pfd_course = B738DR_course_copilot
	end
	
	if nav_tuned_no_id ~= 0 then
		pfd_txt_a = "      "
		pfd_txt2 = string.format("%05d", nav_freq)
		pfd_txt = string.sub(pfd_txt2, 1, 3) .. "." .. string.sub(pfd_txt2, 4, 5)
		pfd_txt = pfd_txt .. "/"  .. string.format("%03d", math.floor(pfd_course + 0.5)) .. "`"
	elseif nav_nav_id == nil then
		pfd_txt_a = "      "
		pfd_txt2 = string.format("%05d", nav_freq)
		pfd_txt = string.sub(pfd_txt2, 1, 3) .. "." .. string.sub(pfd_txt2, 4, 5)
		pfd_txt = pfd_txt .. "/"  .. string.format("%03d", math.floor(pfd_course + 0.5)) .. "`"
	elseif nav_nav_id == "" then
		pfd_txt_a = "      "
		pfd_txt2 = string.format("%05d", nav_freq)
		pfd_txt = string.sub(pfd_txt2, 1, 3) .. "." .. string.sub(pfd_txt2, 4, 5)
		pfd_txt = pfd_txt .. "/"  .. string.format("%03d", math.floor(pfd_course + 0.5)) .. "`"
	else
		if nav_id_decode == 0 then
			if pfd_ils_id_disagree == 0  then
				pfd_txt = spaces_after(nav_nav_id, 4)
				pfd_txt_a = "    "
			else
				pfd_txt = "    "
				pfd_txt_a = spaces_after(nav_nav_id, 4)
			end
		else
			if pfd_ils_id_disagree == 0  then
				pfd_txt2 = string.format("%05d", nav_freq)
				pfd_txt = string.sub(pfd_txt2, 1, 3) .. "." .. string.sub(pfd_txt2, 4, 5)
				pfd_txt_a = "      "
			else
				pfd_txt = "      "
				pfd_txt2 = string.format("%05d", nav_freq)
				pfd_txt_a = string.sub(pfd_txt2, 1, 3) .. "." .. string.sub(pfd_txt2, 4, 5)
			end
		end
		
		if pfd_ils_crs_disagree == 0 then
			
			pfd_txt = pfd_txt .. "/"
			pfd_txt = pfd_txt .. string.format("%03d",math.floor(pfd_course + 0.5))
			pfd_txt = pfd_txt .. "`"
		else
			pfd_txt_a = pfd_txt_a .. "/"
			pfd_txt_a = pfd_txt_a .. string.format("%03d",math.floor(pfd_course + 0.5))
			pfd_txt_a = pfd_txt_a .. "`"
		end
	end
	
	if nav_has_dme == 0 or nav_tuned_no_id ~= 0 then
		pfd_txt2 = "DME ---"
	else
		pfd_txt2 = "DME " .. string.format("%4.1f", nav_dme)
	end
	
	if in_dspl == 0 then
		pfd_cpt_nav_txt1 = pfd_txt
		pfd_cpt_nav_txt1a = pfd_txt_a
		pfd_cpt_nav_txt2 = pfd_txt2
	else
		pfd_fo_nav_txt1 = pfd_txt
		pfd_fo_nav_txt1a = pfd_txt_a
		pfd_fo_nav_txt2 = pfd_txt2
	end
end


function pfd_gls(in_dspl)

	local pfd_txt = ""
	local pfd_txt_a = ""
	local pfd_txt2 = ""
	local pfd_mmr_active = 0
	local pfd_mmr_type = 0
	local pfd_mmr_ch = 0
	local pfd_mmr_id = ""
	local pfd_mmr_dme = 0
	local pfd_mmr_rnw = ""
	local pfd_gls_id_disagree = 0
	local pfd_gls_crs_disagree = 0
	local pfd_course = 0
	
	if in_dspl == 0 then
		pfd_mmr_active = B738DR_mmr_active
		pfd_mmr_type = B738DR_mmr_type
		pfd_mmr_id = B738DR_mmr_id
		pfd_gls_id_disagree = gls_cpt_id_disagree
		pfd_gls_crs_disagree = gls_cpt_crs_disagree
		pfd_mmr_ch = B738DR_mmr_channel
		pfd_mmr_rnw = B738DR_mmr_rnw
		pfd_mmr_dme = B738DR_mmr_dme
		pfd_course = B738DR_course_pilot
	else
		pfd_mmr_active = B738DR_mmr_active_fo
		pfd_mmr_type = B738DR_mmr_type_fo
		pfd_mmr_id = B738DR_mmr_id_fo
		pfd_gls_id_disagree = gls_fo_id_disagree
		pfd_gls_crs_disagree = gls_fo_crs_disagree
		pfd_mmr_ch = B738DR_mmr_channel_fo
		pfd_mmr_rnw = B738DR_mmr_rnw_fo
		pfd_mmr_dme = B738DR_mmr_dme_fo
		pfd_course = B738DR_course_copilot
	end
	
	if pfd_mmr_active == 0 or pfd_mmr_type ~= 0 then
		pfd_txt = string.format("%05d", pfd_mmr_ch) .. "/" .. string.format("%03d", math.floor(pfd_course + 0.5)) .. "`"
		pfd_txt_a = ""
		-- pfd_txt2 = "DME ---"
	else
		if pfd_gls_id_disagree == 0  then
			pfd_txt = spaces_after(pfd_mmr_id, 4)
			pfd_txt_a = "    "
		else
			pfd_txt = "    "
			pfd_txt_a = spaces_after(pfd_mmr_id, 4)
		end
		
		if pfd_gls_crs_disagree == 0 then
			
			pfd_txt = pfd_txt .. "/"
			pfd_txt = pfd_txt .. string.format("%03d", math.floor(pfd_course + 0.5))
			pfd_txt = pfd_txt .. "`"
		else
			pfd_txt_a = pfd_txt_a .. "/"
			pfd_txt_a = pfd_txt_a .. string.format("%03d", math.floor(pfd_course + 0.5))
			pfd_txt_a = pfd_txt_a .. "`"
		end
		
		pfd_txt2 = "RW" .. pfd_mmr_rnw .. " " .. string.format("%4.1f", pfd_mmr_dme)
	end
	
	if in_dspl == 0 then
		pfd_cpt_nav_txt1 = pfd_txt
		pfd_cpt_nav_txt1a = pfd_txt_a
		pfd_cpt_nav_txt2 = pfd_txt2
	else
		pfd_fo_nav_txt1 = pfd_txt
		pfd_fo_nav_txt1a = pfd_txt_a
		pfd_fo_nav_txt2 = pfd_txt2
	end
	
end


function pfd_rnav(in_dspl)

	local pfd_txt = ""
	local pfd_txt2 = ""
	local pfd_num = 0
	local pfd_num2 = 0
	local pfd_crs = 0
	
	-- if des_app ~= "------" and rw_dist > 0 and rw_dist < 24 then	--18
	if des_app ~= "------" and rw_dist2 > 0 and rw_dist2 <= 150 and B738DR_ian_not_install == 0 then	--18
		if string.sub(des_app, 1, 1) == "I" then 	-- ILS
			pfd_txt = "ILS " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "L" then 	-- LOC
			pfd_txt = "LOC " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "D" or string.sub(des_app, 1, 1) == "S" then 	-- VORDME
			pfd_txt = "VDM " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "V" then 	-- VOR
			pfd_txt = "VOR " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "R" then
			pfd_txt = "RNV " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "J" then
			pfd_txt = "GLS " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "N" or string.sub(des_app, 1, 1) == "Q" then
			pfd_txt = "NDB " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "X" then
			pfd_txt = "LDA " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "G" then
			pfd_txt = "IGS " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "M" then
			pfd_txt = "MLS " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "B" then
			pfd_txt = "BLC " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "P" then
			pfd_txt = "GPS " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "T" then
			pfd_txt = "TAC " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "U" then
			pfd_txt = "SDF " .. string.sub(des_app, 2, -1)
		elseif string.sub(des_app, 1, 1) == "W" or string.sub(des_app, 1, 1) == "Y" then
			pfd_txt = "MLS " .. string.sub(des_app, 2, -1)
		else
			pfd_txt = des_app
		end
		
		if string.len(pfd_txt) > 2 then
			if string.sub(pfd_txt, -2, -2) == "-" then
				pfd_txt = pfd_txt .. "/"
			else
				pfd_txt = pfd_txt .. " /"
			end
		else
			pfd_txt = pfd_txt .. " /"
		end
		pfd_crs = (rw_fac_crs2 + simDR_mag_variation + 360) % 360
		pfd_txt = pfd_txt .. string.format("%03d", displ_crs(pfd_crs))
		pfd_txt = pfd_txt .. "`"
		
		pfd_txt2 = rw_fac_id2 .. " "
		if rw_dist2 > 0 then
			if rw_dist2 < 99.95 then
				pfd_txt2 = pfd_txt2 .. string.format("%4.1f", rw_dist2)
			else
				pfd_txt2 = pfd_txt2 .. string.format("%4d", rw_dist2)
			end
		end
	end
	
	if in_dspl == 0 then
		pfd_cpt_nav_txt1 = pfd_txt
		pfd_cpt_nav_txt1a = ""
		pfd_cpt_nav_txt2 = pfd_txt2
	else
		pfd_fo_nav_txt1 = pfd_txt
		pfd_fo_nav_txt1a = ""
		pfd_fo_nav_txt2 = pfd_txt2
	end
end


-- calculate dist by alt
function calc_vnav_pth_dist_old(x_alt01, x_alt02)
	
	local result = 0
	local x_ed_alt_find = 0
	local x_dist = 0
	local x_dist2 = 0
	local x_last_alt = x_alt01
	
	if vnav_des_table_num > 0 and x_alt02 > x_alt01 then
		for ii = 1, vnav_des_table_num do
			if x_alt01 <= vnav_des_table_alt[ii] and x_ed_alt_find == 0 then
				x_ed_alt_find = 1
				if ii == 1 then
					x_dist2 = 9.0
				else
					x_dist2 = vnav_des_table_dist[ii] -((x_alt01 - vnav_des_table_alt[ii-1]) / (vnav_des_table_alt[ii] - vnav_des_table_alt[ii-1]) * vnav_des_table_dist[ii])
				end
			end
			if x_ed_alt_find == 1 then
				if x_alt02 >= vnav_des_table_alt[ii] then
					x_dist = x_dist2 -((x_alt02 - x_last_alt) / (vnav_des_table_alt[ii] - x_last_alt) * x_dist2)
					break
				end
				x_last_alt = vnav_des_table_alt[ii]
				x_dist = x_dist2
				x_ed_alt_find = 2
			elseif x_ed_alt_find == 2 then
				x_dist2 = vnav_des_table_dist[ii]
				if x_alt02 >= vnav_des_table_alt[ii] then
					x_dist = x_dist + x_dist2 -((x_alt02 - x_last_alt) / (vnav_des_table_alt[ii] - x_last_alt) * x_dist2)
					break
				end
				x_last_alt = vnav_des_table_alt[ii]
				x_dist = x_dist + x_dist2
			end
		end
	end
	
	return x_dist
	
end

-- calculate dist by alt
function calc_vnav_pth_dist(x_alt01, x_alt02, x_path)
	
	local ii = 0
	local x_alt1 = 0
	local x_alt2 = 0
	local x_dist1 = 0
	local x_dist2 = 0
	local result_tmp = 0
	local result = 0
	
	if vnav_des_table_num[x_path] > 0 then
		-- min alt
		for ii = 1, vnav_des_table_num[x_path] do
			if x_alt01 <= vnav_des_table_alt[x_path][ii] then
				x_dist2 = vnav_des_table_dist[x_path][ii]
				x_alt2 = vnav_des_table_alt[x_path][ii]
				if ii > 1 then
					x_dist1 = vnav_des_table_dist[x_path][ii-1]
					x_alt1 = vnav_des_table_alt[x_path][ii-1]
				end
				break
			end
		end

		result_tmp = B738_rescale(x_alt1, x_dist1, x_alt2, x_dist2, x_alt01)

		-- max alt
		x_alt1 = 0
		x_alt2 = 0
		x_dist1 = 0
		x_dist2 = 0
		for ii = 1, vnav_des_table_num[x_path] do
			if x_alt02 <= vnav_des_table_alt[x_path][ii] then
				x_dist2 = vnav_des_table_dist[x_path][ii]
				x_alt2 = vnav_des_table_alt[x_path][ii]
				if ii > 1 then
					x_dist1 = vnav_des_table_dist[x_path][ii-1]
					x_alt1 = vnav_des_table_alt[x_path][ii-1]
				end
				break
			end
		end

		result = B738_rescale(x_alt1, x_dist1, x_alt2, x_dist2, x_alt02)

		result = result - result_tmp
		if result < 0 then
			result = 0
		end
	else
		result = 0
	end
	
	return result
	
end

------------------------------------------------------

-- calculate alt by distance

function calc_vnav_pth_alt(x_alt01, x_dist, x_path)
	
	local ii = 0
	local result = 42000	--41000	--x_alt01
	
	if vnav_des_table_num[x_path] > 0 and x_dist > 0 then
		for ii = 1, vnav_des_table_num[x_path] do
			if x_dist < vnav_des_table_dist[x_path][ii] then
				if ii == 1 then
					result = 2000
				else
					result = B738_rescale(vnav_des_table_dist[x_path][ii-1], vnav_des_table_alt[x_path][ii-1], vnav_des_table_dist[x_path][ii], vnav_des_table_alt[x_path][ii], x_dist)
				end
				break
			end
		end
	end
	-- if result == 41000 then
		-- B738DR_fms_test3 = result
	-- end
	
	return result
	
end

-- create descent wind table


-- forec_alt_1_mod = forec_alt_1
-- forec_alt_1_num_mod = forec_alt_1_num
-- forec_dir_1_mod = forec_dir_1
-- forec_spd_1_mod = forec_spd_1
-- forec_alt_2_mod = forec_alt_2
-- forec_alt_2_num_mod = forec_alt_2_num
-- forec_dir_2_mod = forec_dir_2
-- forec_spd_2_mod = forec_spd_2
-- forec_alt_3_mod = forec_alt_3
-- forec_alt_3_num_mod = forec_alt_3_num
-- forec_dir_3_mod = forec_dir_3
-- forec_spd_3_mod = forec_spd_3

-- crz_alt_num = 0
-- crz_wind_dir = "---"
-- crz_wind_spd = "---"

function create_desc_wind_table()
	
	local wind_alt_temp = 0
	local wind_alt_temp2 = 0
	local wind_dir_temp2 = 0
	local wind_spd_temp2 = 0
	local table_created = 0
	
	local crz_wind_dir_num = 0
	local crz_wind_spd_num = 0
	
	desc_wind_tab_num = 0
	desc_wind_alt_tab = {}
	desc_wind_spd_tab = {}
	desc_wind_dir_tab = {}
	
	if crz_alt_num >= 1000 then	--and crz_wind_dir ~= "---" and crz_wind_spd ~= "---" then
		wind_alt_temp = roundUpToIncrement(crz_alt_num, 1000)
		if wind_alt_temp > 41000 then
			wind_alt_temp = 41000
		end
		
		crz_wind_dir_num = tonumber(crz_wind_dir)
		crz_wind_spd_num = tonumber(crz_wind_spd)
		if crz_wind_dir_num == nil then
			crz_wind_dir_num = 0
		end
		if crz_wind_spd_num == nil then
			crz_wind_spd_num = 0
		end
		
		wind_dir_temp2 = tonumber(forec_dir_1)
		wind_spd_temp2 = tonumber(forec_spd_1)
		if forec_alt_1_num ~= 0 and wind_dir_temp2 ~= nil and wind_spd_temp2 ~= nil then
			wind_alt_temp2 = roundUpToIncrement(forec_alt_1_num, 1000)
			if wind_alt_temp2 < wind_alt_temp then
				crt_wnd_tab(wind_alt_temp, tonumber(crz_wind_spd_num), tonumber(crz_wind_dir_num), wind_alt_temp2, wind_spd_temp2, wind_dir_temp2)
				table_created = 1
			else
				table_created = 0
			end
			wind_alt_temp = wind_alt_temp2
			
			wind_dir_temp2 = tonumber(forec_dir_2)
			wind_spd_temp2 = tonumber(forec_spd_2)
			if forec_alt_2_num ~= 0 and wind_dir_temp2 ~= nil and wind_spd_temp2 ~= nil then
				wind_alt_temp2 = roundUpToIncrement(forec_alt_2_num, 1000)
				if wind_alt_temp2 < wind_alt_temp then
					if table_created ~= 0 then
						desc_wind_tab_num = desc_wind_tab_num - 1
					end
					crt_wnd_tab(wind_alt_temp, tonumber(forec_spd_1), tonumber(forec_dir_1), wind_alt_temp2, wind_spd_temp2, wind_dir_temp2)
					table_created = 1
				else
					table_created = 0
				end
				wind_alt_temp = wind_alt_temp2
				
				wind_dir_temp2 = tonumber(forec_dir_3)
				wind_spd_temp2 = tonumber(forec_spd_3)
				if forec_alt_3_num ~= 0 and wind_dir_temp2 ~= nil and wind_spd_temp2 ~= nil then
					wind_alt_temp2 = roundUpToIncrement(forec_alt_3_num, 1000)
					if wind_alt_temp2 < wind_alt_temp then
						if table_created ~= 0 then
							desc_wind_tab_num = desc_wind_tab_num - 1
						end
						crt_wnd_tab(wind_alt_temp, tonumber(forec_spd_2), tonumber(forec_dir_2), wind_alt_temp2, wind_spd_temp2, wind_dir_temp2)
						table_created = 1
					else
						table_created = 0
					end
					wind_alt_temp = wind_alt_temp2
					-- 3rd alt wind to 0 ft
					if table_created ~= 0 then
						desc_wind_tab_num = desc_wind_tab_num - 1
					end
					crt_wnd_tab(wind_alt_temp, tonumber(forec_spd_3), tonumber(forec_dir_3), 0, tonumber(forec_spd_3), tonumber(forec_dir_3))
				else
					-- 2nd alt wind to 0 ft
					if table_created ~= 0 then
						desc_wind_tab_num = desc_wind_tab_num - 1
					end
					crt_wnd_tab(wind_alt_temp, tonumber(forec_spd_2), tonumber(forec_dir_2), 0, tonumber(forec_spd_2), tonumber(forec_dir_2))
				end
			else
				-- 1st alt wind to 0 ft
				if table_created ~= 0 then
					desc_wind_tab_num = desc_wind_tab_num - 1
				end
				crt_wnd_tab(wind_alt_temp, tonumber(forec_spd_1), tonumber(forec_dir_1), 0, 0, tonumber(forec_dir_1))
			end
			
		else
			-- cruise to 0 ft
			crt_wnd_tab(wind_alt_temp, tonumber(crz_wind_spd_num), tonumber(crz_wind_dir_num), 0, 0, tonumber(crz_wind_dir_num))
		end
	end
end

function crt_wnd_tab(x_wind_alt1, x_wind_spd1, x_wind_dir1, x_wind_alt2, x_wind_spd2, x_wind_dir2)
	
	local delta_wind_alt = x_wind_alt1 - x_wind_alt2
	
	local delta_wind_spd = x_wind_spd1 - x_wind_spd2
	local coef_wind_spd = delta_wind_spd / delta_wind_alt
	
	local delta_wind_dir = (x_wind_dir1 - x_wind_dir2 + 360) % 360
	if delta_wind_dir > 180 then
		delta_wind_dir = delta_wind_dir - 360
	end
	local coef_wind_dir = delta_wind_dir / delta_wind_alt
	
	for ii = x_wind_alt1, x_wind_alt2, -1000 do
		desc_wind_tab_num = desc_wind_tab_num + 1
		desc_wind_alt_tab[desc_wind_tab_num] = ii
		desc_wind_spd_tab[desc_wind_tab_num] = ((ii - x_wind_alt2) * coef_wind_spd) + x_wind_spd2
		desc_wind_dir_tab[desc_wind_tab_num] = (((ii - x_wind_alt2) * coef_wind_dir) + x_wind_dir2 + 360) % 360
	end
	
end

function get_desc_wind(x_alt)
	local result_dir = 0
	local result_spd = 0
	local low_spd = 0
	local high_spd = 0
	local low_dir = 0
	local high_dir = 0
	
	local delta_wind_alt = 0
	local delta_wind_spd = 0
	local delta_wind_dir = 0
	local coef_wind_spd = 0
	local coef_wind_dir = 0
	
	if desc_wind_tab_num > 0 then
		for iiii = 1, desc_wind_tab_num do
			if x_alt > desc_wind_alt_tab[iiii] then
				low_dir = desc_wind_dir_tab[iiii]
				low_spd = desc_wind_spd_tab[iiii]
				if iiii > 1 then
					high_dir = desc_wind_dir_tab[iiii-1]
					high_spd = desc_wind_spd_tab[iiii-1]
					delta_wind_alt = desc_wind_alt_tab[iiii-1] - desc_wind_alt_tab[iiii]
					delta_wind_spd = high_spd - low_spd
					delta_wind_dir = (high_dir - low_dir + 360) % 360
					if delta_wind_dir > 180 then
						delta_wind_dir = delta_wind_dir - 360
					end
					if delta_wind_alt == 0 then
						result_spd = low_spd
						result_dir = low_dir
					else
						coef_wind_spd = delta_wind_spd / delta_wind_alt
						coef_wind_dir = delta_wind_dir / delta_wind_alt
						result_spd = ((x_alt - desc_wind_alt_tab[iiii]) * coef_wind_spd) + low_spd
						result_dir = (((x_alt - desc_wind_alt_tab[iiii]) * coef_wind_dir) + low_dir + 360) % 360
					end
				else
					result_dir = low_dir
					result_spd = low_spd
				end
				break
			end
		end
	end
	
	return result_dir, result_spd
end

-- find alt mach speed
function find_mach_alt(x_desc_spd, x_desc_spd_mach)
	
	raw_table('mach_alt_082')
	local mach_alt_082 = { [180] = 42000, [190] = 42000, [200] = 42000, [210] = 42000, [220] = 42000, [230] = 42000, [240] = 41600, [250] = 39700, [260] = 37900, [270] = 36100, [280] = 34550, [290] = 33000 , [300] = 31200 , [310] = 29500 , [320] = 27800 , [330] = 26100 , [340] = 24400 }
	raw_table('mach_alt_079')
	local mach_alt_079 = { [180] = 42000, [190] = 42000, [200] = 42000, [210] = 42000, [220] = 42000, [230] = 41800, [240] = 39400, [250] = 37600, [260] = 35850, [270] = 34200, [280] = 32600, [290] = 31000 , [300] = 29300 , [310] = 27600 , [320] = 25900 , [330] = 24200 , [340] = 22600 }
	raw_table('mach_alt_076')
	local mach_alt_076 = { [180] = 42000, [190] = 42000, [200] = 42000, [210] = 42000, [220] = 41600, [230] = 39800, [240] = 37500, [250] = 35750, [260] = 34000, [270] = 32350, [280] = 30700, [290] = 29100 , [300] = 27400 , [310] = 25700 , [320] = 24000 , [330] = 22300 , [340] = 20700 }
	raw_table('mach_alt_073')
	local mach_alt_073 = { [180] = 42000, [190] = 42000, [200] = 42000, [210] = 42000, [220] = 39800, [230] = 37900, [240] = 35500, [250] = 33800, [260] = 32000, [270] = 30300, [280] = 28650, [290] = 27000 , [300] = 25300 , [310] = 23600 , [320] = 21900 , [330] = 20200 , [340] = 18500 }
	raw_table('mach_alt_070')
	local mach_alt_070 = { [180] = 42000, [190] = 42000, [200] = 42000, [210] = 39800, [220] = 37800, [230] = 36000, [240] = 33500, [250] = 31700, [260] = 30000, [270] = 28250, [280] = 26550, [290] = 24850 , [300] = 23100 , [310] = 21400 , [320] = 19600 , [330] = 17900 , [340] = 16200 }
	
	raw_table('mach_alt_067')
	local mach_alt_067 = { [180] = 42000, [190] = 42000, [200] = 39800, [210] = 37800, [220] = 35800, [230] = 33900, [240] = 31900, [250] = 30000, [260] = 28300, [270] = 26500, [280] = 24800, [290] = 23100 , [300] = 21300 , [310] = 19500 , [320] = 17800 , [330] = 16000 , [340] = 14300 }
	raw_table('mach_alt_064')
	local mach_alt_064 = { [180] = 42000, [190] = 39900, [200] = 37700, [210] = 35700, [220] = 33600, [230] = 31700, [240] = 29700, [250] = 28000, [260] = 26000, [270] = 24200, [280] = 22400, [290] = 20700 , [300] = 18900 , [310] = 17100 , [320] = 15300 , [330] = 13500 , [340] = 11700 }
	raw_table('mach_alt_061')
	local mach_alt_061 = { [180] = 39900, [190] = 37700, [200] = 35500, [210] = 33400, [220] = 31300, [230] = 29400, [240] = 27300, [250] = 25400, [260] = 23600, [270] = 21700, [280] = 20000, [290] = 18200 , [300] = 16300 , [310] = 14500 , [320] = 12700 , [330] = 10900 , [340] =  9100 }
	raw_table('mach_alt_058')
	local mach_alt_058 = { [180] = 37600, [190] = 35400, [200] = 33100, [210] = 31000, [220] = 28900, [230] = 26900, [240] = 24900, [250] = 22900, [260] = 21000, [270] = 19100, [280] = 17300, [290] = 15500 , [300] = 13600 , [310] = 11700 , [320] =  9800 , [330] =  8000 , [340] =  6100 }
	raw_table('mach_alt_055')
	local mach_alt_055 = { [180] = 35200, [190] = 33000, [200] = 30700, [210] = 28500, [220] = 26400, [230] = 24300, [240] = 22200, [250] = 20200, [260] = 18200, [270] = 16300, [280] = 14400, [290] = 12600 , [300] = 10600 , [310] =  8700 , [320] =  6800 , [330] =  4900 , [340] =  3200 }
	raw_table('mach_alt_052')
	local mach_alt_052 = { [180] = 32700, [190] = 30400, [200] = 28000, [210] = 25800, [220] = 23500, [230] = 21500, [240] = 19300, [250] = 17300, [260] = 15300, [270] = 13300, [280] = 11400, [290] =  9500 , [300] =  7500 , [310] =  5500 , [320] =  3600 , [330] =  1600 , [340] =     0 }
	raw_table('mach_alt_049')
	local mach_alt_049 = { [180] = 29900, [190] = 27500, [200] = 25100, [210] = 22900, [220] = 20600, [230] = 18500, [240] = 16200, [250] = 14200, [260] = 12100, [270] = 10100, [280] =  8100, [290] =  6200 , [300] =  4200 , [310] =  2200 , [320] =   200 , [330] =     0 , [340] =     0 }
	raw_table('mach_alt_046')
	local mach_alt_046 = { [180] = 26900, [190] = 24500, [200] = 22000, [210] = 19700, [220] = 17300, [230] = 15200, [240] = 12900, [250] = 10800, [260] =  8700, [270] =  6600, [280] =  4600, [290] =  2700 , [300] =   600 , [310] =     0 , [320] =     0 , [330] =     0 , [340] =     0 }
	raw_table('mach_alt_043')
	local mach_alt_043 = { [180] = 23800, [190] = 21200, [200] = 18600, [210] = 16300, [220] = 13800, [230] = 11600, [240] =  9300, [250] =  7100, [260] =  4900, [270] =  2850, [280] =  1000, [290] =     0 , [300] =     0 , [310] =     0 , [320] =     0 , [330] =     0 , [340] =     0 }
	raw_table('mach_alt_040')
	local mach_alt_040 = { [180] = 20200, [190] = 17500, [200] = 15000, [210] = 12500, [220] = 10200, [230] =  7700, [240] =  5400, [250] =  3300, [260] =  1200, [270] =     0, [280] =     0, [290] =     0 , [300] =     0 , [310] =     0 , [320] =     0 , [330] =     0 , [340] =     0 }
	
	-- above 300kts
	raw_table('cross_speed')
	local cross_speed = {180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340}
	
	local result = 41000
	local mach_alt1 = 0
	local mach_alt2 = 0
	local spd1 = 340
	local spd2 = 340
	
	for ii = 1, 17 do
		if x_desc_spd <= cross_speed[ii] then
			spd1 = cross_speed[ii]
			if ii == 1 then
				spd2 = cross_speed[ii]
			else
				spd2 = cross_speed[ii-1]
			end
			break
		end
	end
	
	if x_desc_spd_mach <= 0.40 then
		mach_alt1 = B738_rescale(spd1, mach_alt_040[spd1], spd2, mach_alt_040[spd2], x_desc_spd)
		result = mach_alt1
	elseif x_desc_spd_mach <= 0.43 then
		mach_alt1 = B738_rescale(spd1, mach_alt_040[spd1], spd2, mach_alt_040[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_043[spd1], spd2, mach_alt_043[spd2], x_desc_spd)
		result = B738_rescale(0.40, mach_alt1, 0.43, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.46 then
		mach_alt1 = B738_rescale(spd1, mach_alt_043[spd1], spd2, mach_alt_043[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_046[spd1], spd2, mach_alt_046[spd2], x_desc_spd)
		result = B738_rescale(0.43, mach_alt1, 0.46, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.49 then
		mach_alt1 = B738_rescale(spd1, mach_alt_046[spd1], spd2, mach_alt_046[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_049[spd1], spd2, mach_alt_049[spd2], x_desc_spd)
		result = B738_rescale(0.46, mach_alt1, 0.49, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.52 then
		mach_alt1 = B738_rescale(spd1, mach_alt_049[spd1], spd2, mach_alt_049[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_052[spd1], spd2, mach_alt_052[spd2], x_desc_spd)
		result = B738_rescale(0.49, mach_alt1, 0.52, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.55 then
		mach_alt1 = B738_rescale(spd1, mach_alt_052[spd1], spd2, mach_alt_052[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_055[spd1], spd2, mach_alt_055[spd2], x_desc_spd)
		result = B738_rescale(0.52, mach_alt1, 0.55, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.58 then
		mach_alt1 = B738_rescale(spd1, mach_alt_055[spd1], spd2, mach_alt_055[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_058[spd1], spd2, mach_alt_058[spd2], x_desc_spd)
		result = B738_rescale(0.55, mach_alt1, 0.58, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.61 then
		mach_alt1 = B738_rescale(spd1, mach_alt_058[spd1], spd2, mach_alt_058[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_061[spd1], spd2, mach_alt_061[spd2], x_desc_spd)
		result = B738_rescale(0.58, mach_alt1, 0.61, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.64 then
		mach_alt1 = B738_rescale(spd1, mach_alt_061[spd1], spd2, mach_alt_061[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_064[spd1], spd2, mach_alt_064[spd2], x_desc_spd)
		result = B738_rescale(0.61, mach_alt1, 0.64, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.67 then
		mach_alt1 = B738_rescale(spd1, mach_alt_064[spd1], spd2, mach_alt_064[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_067[spd1], spd2, mach_alt_067[spd2], x_desc_spd)
		result = B738_rescale(0.64, mach_alt1, 0.67, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.70 then
		mach_alt1 = B738_rescale(spd1, mach_alt_067[spd1], spd2, mach_alt_067[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_070[spd1], spd2, mach_alt_070[spd2], x_desc_spd)
		result = B738_rescale(0.67, mach_alt1, 0.70, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.73 then
		mach_alt1 = B738_rescale(spd1, mach_alt_070[spd1], spd2, mach_alt_070[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_073[spd1], spd2, mach_alt_073[spd2], x_desc_spd)
		result = B738_rescale(0.70, mach_alt1, 0.73, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.76 then
		mach_alt1 = B738_rescale(spd1, mach_alt_073[spd1], spd2, mach_alt_073[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_076[spd1], spd2, mach_alt_076[spd2], x_desc_spd)
		result = B738_rescale(0.73, mach_alt1, 0.76, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.79 then
		mach_alt1 = B738_rescale(spd1, mach_alt_076[spd1], spd2, mach_alt_076[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_079[spd1], spd2, mach_alt_079[spd2], x_desc_spd)
		result = B738_rescale(0.76, mach_alt1, 0.79, mach_alt2, x_desc_spd_mach)
	elseif x_desc_spd_mach <= 0.82 then
		mach_alt1 = B738_rescale(spd1, mach_alt_079[spd1], spd2, mach_alt_079[spd2], x_desc_spd)
		mach_alt2 = B738_rescale(spd1, mach_alt_082[spd1], spd2, mach_alt_082[spd2], x_desc_spd)
		result = B738_rescale(0.79, mach_alt1, 0.82, mach_alt2, x_desc_spd_mach)
	else
		mach_alt1 = B738_rescale(spd1, mach_alt_082[spd1], spd2, mach_alt_082[spd2], x_desc_spd)
		result = mach_alt1
	end
	
	return result
	
end


-- find spd restrict
function find_spd_rest(x_idx2, xspd_rest_dist, xspd_dist_corr)
	
	local ii = 0
	local jj = 0
	local res_idx = 0
	local res_spd = 0
	local res_dist = xspd_rest_dist
	local res_type = 0
	local res_spd_app = 0
	
	if x_idx2 > 2 and x_idx2 <= legs_num then
		for ii = x_idx2-1, 2, -1 do
			if xspd_dist_corr == 0 or ii ~= x_idx2-1 then
				res_dist = res_dist + wpt_distance(ii+1)
			else
				res_dist = res_dist + xspd_dist_corr
			end
			if legs_data[ii][4] ~= 0 then
				--if legs_data[ii][19] ~= 0 and legs_data[ii][19] ~= 1 and legs_data[ii][19] ~= 3 then
				if (legs_data[ii][19] ~= 1 and legs_data[ii][19] ~= 3) 
				or (legs_data[ii][19] == 0 and ii >= tc_idx and tc_idx ~= 0) then
					res_idx = ii
					res_spd = legs_data[ii][4]
					res_type = legs_data[ii][42]
					for jj = ii, 2, -1 do
						--if legs_data[jj][19] ~= 0 and legs_data[jj][19] ~= 1 and legs_data[jj][19] ~= 3 then
						if (legs_data[jj][19] ~= 1 and legs_data[jj][19] ~= 3) 
						or (legs_data[ii][19] == 0 and jj >= tc_idx and tc_idx ~= 0) then
							if legs_data[jj][4] < res_spd and legs_data[jj][4] ~= 0 and legs_data[jj][42] ~= 2 then
								res_idx = jj
								res_spd = legs_data[jj][4]
								res_type = legs_data[jj][42]
							end
						end
					end
					break
				end
			end
		end
		
		local before_iaf_idx = 0
		if iaf_app_idx == 0 then
			before_iaf_idx = first_app_idx
		elseif iaf_app_idx - 1 >= first_app_idx then
			before_iaf_idx = iaf_app_idx - 1
		else
			before_iaf_idx = iaf_app_idx
		end
		
		if before_iaf_idx < x_idx2 and before_iaf_idx ~= 0 then
			if B738DR_fms_vref_40 == 0 then
				res_spd_app = 200
			else
				res_spd_app = B738DR_fms_vref_40 + 70
			end
			if res_idx == 0 then
				if before_iaf_idx < x_idx2 and before_iaf_idx ~= 0 then
					res_idx = before_iaf_idx
					res_spd = res_spd_app
					res_type = 1	--0
				end
			else
				if res_idx == before_iaf_idx then
					if res_type == 1 and res_spd_app <= res_spd then
						-- below
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					elseif res_type == 2 and res_spd_app >= res_spd then
						-- above
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					end
				elseif res_idx < before_iaf_idx then
					if res_type == 1 and res_spd_app <= res_spd then
						-- below
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					else
						-- at and above
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					end
				end
			end
		end
	end
	if res_idx == 0 then
		res_dist = 0
	end
	return res_idx, res_spd, res_dist, res_type
	
end

-- function calc_path_angle(x_high, x_low, x_dist)
	-- if x_dist == 0 then
		-- return 0
	-- else
		-- return math.atan((x_high-x_low)/x_dist)
	-- end
-- end

-- function calc_path_distance(x_high, x_low, x_angle)
	-- local x_calc = math.tan(x_angle)
	-- if x_calc == 0 then
		-- return x_high
	-- else
		-- return (x_high-x_low)/math.tan(x_angle)
	-- end
-- end

-- function calc_path_altitude(x_dist, x_angle)
	-- return x_dist * math.tan(x_angle)
-- end

function calc_path_angle(x_high, x_low, x_dist)
	if x_dist == 0 then
		return 0
	else
		return math.atan((x_high-x_low)/(x_dist * 6076.115486))
	end
end

function calc_path_distance(x_high, x_low, x_angle)
	local x_calc = math.tan(x_angle)
	if x_calc == 0 then
		return 0	--x_high
	else
		return ((x_high-x_low)/math.tan(x_angle)) / 6076.115486
	end
end

function calc_path_altitude(x_dist, x_angle)
	return (x_dist * 6076.115486) * math.tan(x_angle)
end

function calc_path_fpm(in_angle, in_speed, in_alt, in_wind)
	local result = 0
	
	local tas_kts = 0
	if in_speed <= 0 then
		tas_kts = 145
	elseif in_speed < 100 then
		tas_kts = mach_to_tas(in_speed, in_alt)
	else
		tas_kts = ias_to_tas(in_speed, in_alt)
	end
	
	local calc_d = ((tas_kts + in_wind) * 0.5144444444 * 60) / 1852
	
	result = calc_path_altitude(calc_d, in_angle)
	
	return result
end

function calc_path_angle_fpm(in_fpm, in_speed, in_alt, in_wind)
	local result = 0
	
	local tas_kts = 0
	if in_speed <= 0 then
		tas_kts = 145
	elseif in_speed < 100 then
		tas_kts = mach_to_tas(in_speed, in_alt)
	else
		tas_kts = ias_to_tas(in_speed, in_alt)
	end
	
	local calc_d = ((tas_kts + in_wind) * 0.5144444444 * 60) / 1852
	
	result = calc_path_angle(in_fpm, 0, calc_d)
	
	return result
end

function calc_decel_dist(in_fpm, in_fpm2, in_speed, in_alt, in_wind)
	local result = 0
	local calc_t = 0
	local delta_fpm = in_fpm - in_fpm2
	
	if delta_fpm > 0 then
		local tas_kts = 0
		if in_speed <= 0 then
			tas_kts = 145
		elseif in_speed < 100 then
			tas_kts = mach_to_tas(in_speed, in_alt)
		else
			tas_kts = ias_to_tas(in_speed, in_alt)
		end
		
		calc_t = (delta_fpm / 500) * 10
		result = ((tas_kts + in_wind) * 0.5144444444 * calc_t) / 1852
	end
	
	return result
end

function take_alt_dist(x_idx_alt, x_spd_alt, x_flap)
	
	local altitude_distance = 0
	local x_speed = 0
	
	local faf_spd = B738DR_fms_vref_40
	
	if faf_spd < 130 then
		faf_spd = 130
	end
	
	-- if x_flap <= 1 then
	if x_flap == 0 then
		-- flap UP
		-- x_speed = math.max(x_spd_alt, 100)
		-- x_speed = math.min(x_speed, 400)
		x_speed = math.max(x_spd_alt, faf_spd + 70)
		x_speed = math.max(x_speed, 100)
		x_speed = math.min(x_speed, 400)
		if x_speed < 150 then
			altitude_distance = B738_rescale(100, vnav_des_dist_diff_100x[x_idx_alt], 150, vnav_des_dist_diff_150x[x_idx_alt], x_speed)
		elseif x_speed < 200 then
			altitude_distance = B738_rescale(150, vnav_des_dist_diff_150x[x_idx_alt], 200, vnav_des_dist_diff_200x[x_idx_alt], x_speed)
		elseif x_speed < 250 then
			altitude_distance = B738_rescale(200, vnav_des_dist_diff_200x[x_idx_alt], 250, vnav_des_dist_diff_250x[x_idx_alt], x_speed)
		elseif x_speed < 290 then
			altitude_distance = B738_rescale(250, vnav_des_dist_diff_250x[x_idx_alt], 290, vnav_des_dist_diff_290x[x_idx_alt], x_speed)
		elseif x_speed < 350 then
			altitude_distance = B738_rescale(290, vnav_des_dist_diff_290x[x_idx_alt], 350, vnav_des_dist_diff_350x[x_idx_alt], x_speed)
		else
			altitude_distance = B738_rescale(350, vnav_des_dist_diff_350x[x_idx_alt], 400, vnav_des_dist_diff_400x[x_idx_alt], x_speed)
		end
	-- elseif x_flap == 2 then
	elseif x_flap == 1 then
		-- flap 1
		-- x_speed = math.max(x_spd_alt, 100)
		-- x_speed = math.min(x_speed, 220)
		x_speed = math.max(x_spd_alt, faf_spd + 50)
		x_speed = math.max(x_speed, 100)
		x_speed = math.min(x_speed, 220)
		if x_speed < 155 then
			altitude_distance = B738_rescale(100, vnav_des_dist_diff_100xf1[x_idx_alt], 155, vnav_des_dist_diff_155xf1[x_idx_alt], x_speed)
		else
			altitude_distance = B738_rescale(155, vnav_des_dist_diff_155xf1[x_idx_alt], 220, vnav_des_dist_diff_220xf1[x_idx_alt], x_speed)
		end
	elseif x_flap == 2 then
		-- flap 5
		-- x_speed = math.max(x_spd_alt, 100)
		-- x_speed = math.min(x_speed, 220)
		x_speed = math.max(x_spd_alt, faf_spd + 30)
		x_speed = math.max(x_speed, 100)
		x_speed = math.min(x_speed, 220)
		if x_speed < 155 then
			altitude_distance = B738_rescale(100, vnav_des_dist_diff_100xf5[x_idx_alt], 155, vnav_des_dist_diff_155xf5[x_idx_alt], x_speed)
		else
			altitude_distance = B738_rescale(155, vnav_des_dist_diff_155xf5[x_idx_alt], 220, vnav_des_dist_diff_220xf5[x_idx_alt], x_speed)
		end
	else
		-- flap 10
		-- x_speed = math.max(x_spd_alt, 150)
		-- x_speed = math.min(x_speed, 200)
		x_speed = math.max(x_spd_alt, faf_spd + 30)
		x_speed = math.max(x_speed, 150)
		x_speed = math.min(x_speed, 200)
		altitude_distance = B738_rescale(150, vnav_des_dist_diff_150xf10[x_idx_alt], 200, vnav_des_dist_diff_200xf10[x_idx_alt], x_speed)
	end
	
	local x_idx_alt_corr = 0
	if x_idx_alt > 12000 and x_idx_alt < 34000 then
		x_idx_alt_corr = x_idx_alt - 12000
		if x_idx_alt_corr > 11000 then
			altitude_distance = altitude_distance + B738_rescale(11000, 0.06, 22000, 0, x_idx_alt_corr)	--0.13
		else
			altitude_distance = altitude_distance + B738_rescale(0, 0, 11000, 0.06, x_idx_alt_corr)	--0.13
		end
	end
	
	local gw_lim = math.max(simDR_total_weight / 1000, 40)
	gw_lim = math.min(gw_lim, 80)
	altitude_distance = altitude_distance + B738_rescale(40, 0.15, 80, 0.25, gw_lim)
	altitude_distance = altitude_distance * 0.98
	
	-- altitude_distance = altitude_distance * B738DR_fms_test3
	-- altitude_distance = altitude_distance + 0.21	--0.019	--0.015 0.01
	
	return altitude_distance --* B738DR_fms_test3 --* 0.96		-- 0.96
	
end

function take_alt_dist_mach(x_idx_alt, x_spd_alt)
	
	local altitude_distance = 0
	
	local x_speed = math.max(x_spd_alt, 0.40)
	--x_speed = math.min(x_speed, 0.82)
	x_speed = math.min(x_speed, 1.07)
	
	if x_speed < 0.45 then
		altitude_distance = B738_rescale(0.40, vnav_des_dist_diff_040x[x_idx_alt], 0.45, vnav_des_dist_diff_045x[x_idx_alt], x_speed)
	elseif x_speed < 0.50 then
		altitude_distance = B738_rescale(0.45, vnav_des_dist_diff_045x[x_idx_alt], 0.50, vnav_des_dist_diff_050x[x_idx_alt], x_speed)
	elseif x_speed < 0.54 then
		altitude_distance = B738_rescale(0.50, vnav_des_dist_diff_050x[x_idx_alt], 0.54, vnav_des_dist_diff_054x[x_idx_alt], x_speed)
	elseif x_speed < 0.58 then
		altitude_distance = B738_rescale(0.54, vnav_des_dist_diff_054x[x_idx_alt], 0.58, vnav_des_dist_diff_058x[x_idx_alt], x_speed)
	elseif x_speed < 0.62 then
		altitude_distance = B738_rescale(0.58, vnav_des_dist_diff_058x[x_idx_alt], 0.62, vnav_des_dist_diff_062x[x_idx_alt], x_speed)
	elseif x_speed < 0.66 then
		altitude_distance = B738_rescale(0.62, vnav_des_dist_diff_062x[x_idx_alt], 0.66, vnav_des_dist_diff_066x[x_idx_alt], x_speed)
	elseif x_speed < 0.70 then
		altitude_distance = B738_rescale(0.66, vnav_des_dist_diff_066x[x_idx_alt], 0.70, vnav_des_dist_diff_070x[x_idx_alt], x_speed)
	elseif x_speed < 0.73 then
		altitude_distance = B738_rescale(0.70, vnav_des_dist_diff_070x[x_idx_alt], 0.73, vnav_des_dist_diff_073x[x_idx_alt], x_speed)
	elseif x_speed < 0.76 then
		altitude_distance = B738_rescale(0.73, vnav_des_dist_diff_073x[x_idx_alt], 0.76, vnav_des_dist_diff_076x[x_idx_alt], x_speed)
	elseif x_speed < 0.79 then
		altitude_distance = B738_rescale(0.76, vnav_des_dist_diff_076x[x_idx_alt], 0.79, vnav_des_dist_diff_079x[x_idx_alt], x_speed)
	elseif x_speed < 0.82 then
		altitude_distance = B738_rescale(0.79, vnav_des_dist_diff_079x[x_idx_alt], 0.82, vnav_des_dist_diff_082x[x_idx_alt], x_speed)
	else
		altitude_distance = B738_rescale(0.82, vnav_des_dist_diff_082x[x_idx_alt], 1.07, vnav_des_dist_diff_107x[x_idx_alt], x_speed)
	end
	local x_idx_alt_corr = 0
	if x_idx_alt > 12000 and x_idx_alt < 34000 then
		x_idx_alt_corr = x_idx_alt - 12000
		if x_idx_alt_corr > 11000 then
			altitude_distance = altitude_distance + B738_rescale(11000, 0.04, 22000, 0, x_idx_alt_corr)	--0.06
		else
			altitude_distance = altitude_distance + B738_rescale(0, 0, 11000, 0.04, x_idx_alt_corr)		--0.06
		end
	end
	local gw_lim = math.max(simDR_total_weight / 1000, 40)
	gw_lim = math.min(gw_lim, 80)
	altitude_distance = altitude_distance + B738_rescale(40, 0.15, 80, 0.25, gw_lim)
	altitude_distance = altitude_distance * 1.01
	-- altitude_distance = altitude_distance * B738DR_fms_test3
	-- altitude_distance = altitude_distance + 0.25	--0.23	--0.21	--0.2
	
	return altitude_distance --* B738DR_fms_test3 --* 0.96		--0.96
	
end

function wpt_wind_spd(wpt_idx_path, wpt_idx_alt, idx_kts_mach)
	
	-- get avg wind
	local tmp_wind_spd1 = 0
	local tmp_wind_dir1 = 0
	local tmp_wind_spd2 = 0
	local tmp_wind_dir2 = 0
	local tmp_alt_alt1 = wpt_idx_alt
	local tmp_alt_alt2 = roundUpToIncrement(tmp_alt_alt1, 1000)
	
	local tmp_wind_spd = 0
	local tmp_wind_dir = 0
	local wind_spd_tas = 0
	local const_tas = 0
	local result = 0
	
	if wpt_idx_path > 0 and wpt_idx_path <= legs_num then
		-- average wind dir/spd
		tmp_wind_dir1, tmp_wind_spd1 = get_desc_wind(tmp_alt_alt1)
		if tmp_alt_alt1 == tmp_alt_alt2 then
			tmp_alt_alt2 = tmp_alt_alt2 + 1000
		end
		tmp_wind_dir2, tmp_wind_spd2 = get_desc_wind(tmp_alt_alt2)
		tmp_wind_spd = (tmp_wind_spd1 + tmp_wind_spd2) / 2
		tmp_wind_dir = (tmp_wind_dir2 - tmp_wind_dir1 + 360) % 360
		if tmp_wind_dir > 180 then
			tmp_wind_dir = tmp_wind_dir - 360
		end
		tmp_wind_dir = tmp_wind_dir / 2
		tmp_wind_dir = (tmp_wind_dir + tmp_wind_dir1 + 360) % 360
		
		if tmp_wind_spd > 0 then
			wind_spd_tas = tmp_wind_spd * math.cos(math.rad(tmp_wind_dir)-legs_data[wpt_idx_path][2])
			if idx_kts_mach == 0 then
				-- kts
				-- const_tas = 1 + (wpt_idx_alt / 1000 * 0.02) + math.max(0, (wpt_idx_alt-20000) / 1000 * 0.06)
				-- result = wind_spd_tas * const_tas
				-- if wpt_idx_path > 1 then
					-- if legs_data[wpt_idx_path-1][11] > tmp_alt_alt1 then
						-- tmp_alt_alt1 = (tmp_alt_alt1 + legs_data[wpt_idx_path-1][11]) / 2
					-- end
				-- end
				result = tas_to_ias(wind_spd_tas, tmp_alt_alt1)
			else
				-- mach
				-- result = wind_spd_tas / 600
				-- if wpt_idx_path > 1 then
					-- if legs_data[wpt_idx_path-1][11] > tmp_alt_alt1 then
						-- tmp_alt_alt1 = (tmp_alt_alt1 + legs_data[wpt_idx_path-1][11]) / 2
					-- end
				-- end
				result = tas_to_mach(wind_spd_tas, tmp_alt_alt1)
			end
			result = result * 0.98	--* 0.99
		end
	end
	return	result
end

function wpt_wind_spd2(wpt_idx_path, wpt_idx_alt, idx_kts_mach)
	
	-- get avg wind
	local tmp_wind_spd1 = 0
	local tmp_wind_dir1 = 0
	local tmp_wind_spd2 = 0
	local tmp_wind_dir2 = 0
	local tmp_alt_alt1 = wpt_idx_alt
	local tmp_alt_alt2 = roundUpToIncrement(tmp_alt_alt1, 1000)
	
	local tmp_wind_spd = 0
	local tmp_wind_dir = 0
	local wind_spd_tas = 0
	local const_tas = 0
	local result = 0
	
	if wpt_idx_path > 0 and wpt_idx_path <= legs_num2_buf then
		
		-- average wind dir/spd
		tmp_wind_dir1, tmp_wind_spd1 = get_desc_wind(tmp_alt_alt1)
		if tmp_alt_alt1 == tmp_alt_alt2 then
			tmp_alt_alt2 = tmp_alt_alt2 + 1000
		end
		tmp_wind_dir2, tmp_wind_spd2 = get_desc_wind(tmp_alt_alt2)
		tmp_wind_spd = (tmp_wind_spd1 + tmp_wind_spd2) / 2
		tmp_wind_dir = (tmp_wind_dir2 - tmp_wind_dir1 + 360) % 360
		if tmp_wind_dir > 180 then
			tmp_wind_dir = tmp_wind_dir - 360
		end
		tmp_wind_dir = tmp_wind_dir / 2
		tmp_wind_dir = (tmp_wind_dir + tmp_wind_dir1 + 360) % 360
		
		if tmp_wind_spd > 0 then
			wind_spd_tas = tmp_wind_spd * math.cos(math.rad(tmp_wind_dir)-legs_data2_buf[wpt_idx_path][2])
			if idx_kts_mach == 0 then
				-- kts
				-- const_tas = 1 + (wpt_idx_alt / 1000 * 0.02) + math.max(0, (wpt_idx_alt-20000) / 1000 * 0.06)
				-- result = wind_spd_tas * const_tas
				-- if wpt_idx_path > 1 then
					-- if legs_data2_buf[wpt_idx_path-1][11] > tmp_alt_alt1 then
						-- tmp_alt_alt1 = (tmp_alt_alt1 + legs_data2_buf[wpt_idx_path-1][11]) / 2
					-- end
				-- end
				result = tas_to_ias(wind_spd_tas, tmp_alt_alt1)
			else
				-- mach
				-- result = wind_spd_tas / 600
				-- if wpt_idx_path > 1 then
					-- if legs_data2_buf[wpt_idx_path-1][11] > tmp_alt_alt1 then
						-- tmp_alt_alt1 = (tmp_alt_alt1 + legs_data2_buf[wpt_idx_path-1][11]) / 2
					-- end
				-- end
				result = tas_to_mach(wind_spd_tas, tmp_alt_alt1)
			end
			result = result * 0.98	--0.99
		end
	end
	return	result
end

function wpt_distance(x_idx)
	
	local leg_dist_tt = 0
	if x_idx > 1 and x_idx <= legs_num + 1 then
		leg_dist_tt = legs_data[x_idx][51]
	end
	
	return leg_dist_tt
	
end

function wpt_distance2(x_idx, in_mode)
	
	local leg_dist_tt = 0
	local rf_dist_tt = 0
	
	if in_mode == 0 then
		if x_idx > 1 and x_idx <= legs_num2 + 1 then
			if legs_num < 1 or legs_data2[x_idx][52] == 0 or legs_data2[x_idx-1][52] == 0 or legs_data2[x_idx][52] ~= (legs_data2[x_idx-1][52] + 1) then
				-- RF dist
				rf_dist_tt = tonumber(legs_data2[x_idx][30])
				
				if legs_data2[x_idx][31] == "RF" and rf_dist_tt ~= nil then
					leg_dist_tt = rf_dist_tt / 10
				else
					leg_dist_tt = legs_data2[x_idx][3]
				end
				if legs_data2[x_idx][31] == "PI" then
					leg_dist_tt = leg_dist_tt + 18.59
				end
			else
				leg_dist_tt = legs_data2[x_idx][51]
			end
		end
	else
		if x_idx > 1 and x_idx <= legs_num8 + 1 then
			if legs_num < 1 or legs_data8[x_idx][52] == 0 or legs_data8[x_idx-1][52] == 0 or legs_data8[x_idx][52] ~= (legs_data8[x_idx-1][52] + 1) then
				-- RF dist
				rf_dist_tt = tonumber(legs_data8[x_idx][30])
				
				if legs_data8[x_idx][31] == "RF" and rf_dist_tt ~= nil then
					leg_dist_tt = rf_dist_tt / 10
				else
					leg_dist_tt = legs_data8[x_idx][3]
				end
				if legs_data8[x_idx][31] == "PI" then
					leg_dist_tt = leg_dist_tt + 18.59
				end
			else
				leg_dist_tt = legs_data8[x_idx][51]
			end
		end
	end
	
	return leg_dist_tt
end

function calc_flap_desc(x_flap_spd)
	
	local x_flap_descent = 0
	local faf_spd = B738DR_fms_vref_40
	
	if faf_spd < 130 then
		faf_spd = 130
	end
	
	-- -- if x_flap_spd <= faf_spd + 20 then
		-- -- x_flap_descent = 4	-- flap 15 -> 10
	-- -- elseif x_flap_spd <= faf_spd + 30 then
		-- -- x_flap_descent = 3	-- flap 10 -> 5
	-- -- elseif x_flap_spd <= faf_spd + 40 then
		-- -- x_flap_descent = 2	-- flap 5 -> 1
	-- -- elseif x_flap_spd <= faf_spd + 50 then
		-- -- x_flap_descent = 1	-- flap 1 -> UP
	-- -- end
	
	-- if x_flap_spd <= faf_spd + 30 then
		-- x_flap_descent = 2	-- flap 5 -> 1
	-- elseif x_flap_spd <= faf_spd + 50 then
		-- x_flap_descent = 1	-- flap 1 -> UP
	-- end
	
	-- if x_flap_spd <= faf_spd + 20 then
		-- x_flap_descent = 2	-- flap 5 -> 1
	-- elseif x_flap_spd <= faf_spd + 40 then
		-- x_flap_descent = 1	-- flap 1 -> UP
	-- end
	if x_flap_spd < faf_spd + 50 then
		x_flap_descent = 2	-- flap 5 -> 1
	elseif x_flap_spd < faf_spd + 70 then
		x_flap_descent = 1	-- flap 1 -> UP
	end
	return x_flap_descent
end

function alt_qnh_up(in_alt, in_qnh, in_temp)
	return in_alt + ((in_qnh - 29.92) * 915) + ((in_temp - 15) * 45)
end

function alt_qnh_dn(in_alt, in_qnh, in_temp)
	return in_alt - ((in_qnh - 29.92) * 915) + ((in_temp - 15) * 45)
end

function alt_qnh_shift2(in_alt, in_qnh, iq_nh_baro)
	return in_alt + (in_qnh - iq_nh_baro) * 915
end

function elev_to_alt_qnh(in_alt, in_qnh, in_tl)
	local result = 0
	if in_alt < in_tl then
		result = in_alt
	else
		result = in_alt - ((in_qnh - 29.92) * 915)
	end
	return result
end

function alt_qnh_to_elev(in_alt, in_qnh, in_tl)
	local result = 0
	if in_alt > in_tl then
		result = in_alt
	else
		result = in_alt + ((in_qnh - 29.92) * 915)
	end
	return result
end

--pathsk

function create_idle_path(x_idx, x_edalt, x_crzalt, x_path, x_in_dist_corr)
	
	local ii = 0
	local jj = 0
	local alt_dist = 0			-- alt distance all
	local alt_start_dist = 0	-- alt distance from start
	local alt_remain_dist = 0	-- alt distance remain
	local pth_angle = 0
	local low_alt = 0
	local high_alt = 0
	local idx_alt = 0
	local x_start_alt = x_edalt
	local x_spd = 0
	local x_actual_spd = 0
	local act_spd_dist = 0
	local x_dist = 0			-- total distance
	local x_last_spd = 0
	local alt_dist2 = 0
	
	local x_spd_mach = 0
	local x_spd_mach_test = 0
	local x_spd_mach_dist = 0
	local x_spd_mach_alt = 0
	
	local spd_rest_idx = 0
	local spd_rest = 0
	local spd_rest_dist = 0
	local spd_rest_type = 0
	local fixed_speed = 0
	
	local x_spd_delta = 0
	local x_spd_coef = 1
	-- local x_spd_coef = B738DR_fms_test3
	
	local spd_rest_dist_temp = 0
	local decel_rest_dist = 0
	local decel_active = 0
	local decel_low_spd = 0
	local decel_high_spd = 0
	local decel_coef = 0
	local decel_dist_full = 0
	local decel_dist_offset = 0
	local x_spd_delta_lim = 0
	
	local x_wpt_idx = x_idx
	local x_wpt_dist = wpt_distance(x_wpt_idx)	--legs_data[x_wpt_idx][3]
	if x_in_dist_corr ~= 0 then
		x_wpt_dist = wpt_distance(x_wpt_idx) - x_in_dist_corr
	end
	local x_wpt_wind = 0	-- wpt wind
	
	local work_loop = 0
	local end_path = 0
	
	local fl100_desc_dist = 0
	local fl100_active = 0
	local compare_dist = 0
	
	local flap_descent = 0
	local flap_descent_speed = 0
	local flap_distance = 0
	local flaps_check = 0
	-- raw_table('flap_dist_descent')
	-- local flap_dist_descent = {}
	-- flap_dist_descent[1] = 20 * DES_SPEED_COEF
	-- flap_dist_descent[2] = 20 * DES_SPEED_COEF
	-- flap_dist_descent[3] = 20 * DES_SPEED_COEF
	-- flap_dist_descent[4] = 10 * DES_SPEED_COEF
	
	local calc_fpm = 0
	local calc_fpm0 = 0
	local calc_fpm1 = 0
	local calc_fpm2 = 0
	local calc_fpm3 = 0
	local calc_fpm4 = 0
	local pom_pom1 = 0
	raw_table('decel_path')
	local decel_path = {}
	decel_path[1] = 0
	decel_path[2] = 0
	decel_path[3] = 0
	decel_path[4] = 0
	raw_table('decel_dist')
	local decel_dist = {}
	decel_dist[1] = 0
	decel_dist[2] = 0
	decel_dist[3] = 0
	decel_dist[4] = 0
	
	vnav_des_table_dist[x_path] = {}
	vnav_des_table_alt[x_path] = {}
	vnav_des_table_spd[x_path] = {}
	vnav_des_table_flap[x_path] = {}
	vnav_des_table_act_spd[x_path] = {}
	vnav_des_table_type[x_path] = {}
	vnav_des_table_num[x_path] = 0
	
	--if x_edalt == ed_alt then
	if x_path == 1 then
		B738DR_FL100_desc_dist = 0
		B738DR_FL100_idx = 0
		B738DR_FL100_dist = 0
	end
	
	while x_wpt_dist == 0 do
		x_wpt_idx = x_wpt_idx - 1
		if x_wpt_idx > 1 then
			x_wpt_dist = wpt_distance(x_wpt_idx)	--legs_data[x_wpt_idx][3]
		else
			break
		end
	end
	
	local active_dump = 0
	if x_start_alt == B738DR_fms_test3 then
		if B738DR_fms_test == 747 then
			active_dump = 1
		end
	end
	
	-- if x_start_alt <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
	if x_start_alt < B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
		fixed_speed = math.min(B738DR_fmc_descent_speed, B738DR_fmc_descent_r_speed1)
		if fixed_speed == B738DR_fmc_descent_r_speed1 then
			fl100_active = 1
		end
	else
		fixed_speed = B738DR_fmc_descent_speed
	end
	
	local init_speed = 0
	local init_speed_active = 0
	local init_decel_dist = 0
	local init_speed2 = 0
	
	local tmp_tmp = 0
	local tmp_tmp2 = 0
	
	if x_path == 1 then
		init_speed = 180
		local x_idx99 = x_idx
		while x_idx99 <= legs_num + 1 do
			if x_idx99 == x_idx then
				init_decel_dist = x_in_dist_corr
			else
				init_decel_dist = init_decel_dist + wpt_distance(x_idx99)
			end
			if legs_data[x_idx99][17] == 1 then
				if B738DR_fms_vref_40 > 0 then
					init_speed = B738DR_fms_vref_40 + 20
				end
				if init_decel_dist ~= 0 then
					init_speed = init_speed + (init_decel_dist / DES_SPEED_COEF)
				end
				break
			end
			x_idx99 = x_idx99 + 1
		end
		init_decel_dist = 0
		
	elseif x_path > 1 then
		if vnav_des_table_num[x_path-1] > 0 then
			for ii = 1, vnav_des_table_num[x_path-1] do
				if vnav_des_table_type[x_path-1][ii] > 0 and vnav_des_table_type[x_path-1][ii] < 1000 then
					if init_speed ~= 0 then
						if vnav_des_table_type[x_path-1][ii] <= jj - 1 then
							init_speed2 = vnav_des_table_act_spd[x_path-1][ii]
							if init_speed2 > init_speed then
								init_speed = init_speed + (x_in_dist_corr / DES_SPEED_COEF)
								if init_speed > init_speed2 then
									init_speed = init_speed2
								end
							end
							break
						end
					else
						if vnav_des_table_type[x_path-1][ii] <= x_idx then
							init_speed = vnav_des_table_act_spd[x_path-1][ii]
							jj = vnav_des_table_type[x_path-1][ii]
							if x_in_dist_corr == 0 then
								break
							end
						end
					end
				end
			end
			if init_speed == 0 then
				init_speed = fixed_speed
			end
		end
	end
	
	jj = 0
	
	if legs_data[x_idx][4] ~= 0 then
		-- res_idx = x_idx
		-- res_spd = legs_data[x_idx][4]
		-- res_type = legs_data[x_idx][42]
		
		if legs_data[x_idx][42] == 1 then
			-- below
			if init_speed > legs_data[x_idx][4] then
				init_speed = legs_data[x_idx][4]
			end
		elseif legs_data[x_idx][42] == 2 then
			-- above
			if init_speed < legs_data[x_idx][4] then
				init_speed = legs_data[x_idx][4]
			end
		else
			-- at
			init_speed = legs_data[x_idx][4]
		end
	end
	if init_speed > fixed_speed then
		init_speed = fixed_speed
	end
	if x_start_alt == B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then	--and init_speed > B738DR_fmc_descent_r_speed1 then
		if init_speed > B738DR_fmc_descent_r_speed1 then
			init_speed = B738DR_fmc_descent_r_speed1
		end
		fl100_active = 1
	-- elseif (x_in_dist_corr ~= 0 or ed_fix_tl[ed_fix_num] == 1) and x_start_alt > B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
	-- elseif (x_in_dist_corr ~= 0 or ed_tl == 1) and x_start_alt > B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
	end
	
	-- find next restrict
	spd_rest_idx, spd_rest, spd_rest_dist, spd_rest_type = find_spd_rest(x_idx, 0, 0)
	
	if active_dump ~= 0 then
		-- activate dump data
		dump_leg_append("\n")
		dump_leg_append("Create idle path: \n")
		dump_leg_append("spd_rest_idx: " .. tostring(spd_rest_idx) .. " \n")
		dump_leg_append("spd_rest: " .. tostring(spd_rest) .. " \n")
		dump_leg_append("first_app_idx: " .. tostring(first_app_idx) .. " \n")
	end
	
	if spd_rest_idx ~= 0 then
		
		if spd_rest_type == 1 then
			-- below
			if init_speed > spd_rest then
				init_speed = spd_rest
			end
		-- elseif spd_rest_type == 2 then
			-- -- above
		elseif spd_rest_type == 0 then
			-- at
			if init_speed > spd_rest then
				init_speed = spd_rest
			end
		end
		
	end
	
	if init_speed < spd_rest or init_speed < fixed_speed then
		init_speed_active = 1
	end
	
	x_spd = fixed_speed
	if spd_rest ~= 0 then
		x_spd = math.min(spd_rest, fixed_speed)
	end
	
	if active_dump ~= 0 then
		-- activate dump data
		dump_leg_append("\n")
		dump_leg_append("Create idle path: \n")
		dump_leg_append("tmp_tmp: " .. tostring(tmp_tmp) .. " \n")
		dump_leg_append("tmp_tmp2: " .. tostring(tmp_tmp2) .. " \n")
		
		dump_leg_append("init_speed: " .. tostring(init_speed) .. " \n")
		dump_leg_append("x_spd: " .. tostring(x_spd) .. " \n")
		dump_leg_append("fixed_speed: " .. tostring(fixed_speed) .. " \n")
	end
	
	if init_speed_active == 1 and x_spd > init_speed then
		x_spd_delta = x_spd - init_speed
		-- decel_rest_dist = x_spd_delta * DES_SPEED_COEF
		-- x_spd_delta_lim = math.min(x_spd_delta, 40)
		-- if x_spd_delta_lim < 10 then
			-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
		-- else
			-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
		-- end
		decel_dist_full = x_spd_delta * DES_SPEED_COEF
		decel_dist_offset = 0
		decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
		-- x_spd_coef = 1	--2.12
		-- x_spd_coef = 2.3
		-- x_spd_coef = B738DR_fms_test3
		-- x_spd_coef = init_speed
		-- decel_active = 1
		decel_active = 4
		decel_low_spd = init_speed
		decel_high_spd = x_spd
		x_spd_coef = decel_low_spd
		decel_coef = 0.55
		
		decel_dist[1] = decel_dist_full
		decel_dist[2] = decel_dist_full * 0.75
		decel_dist[3] = decel_dist_full * 0.5
		decel_dist[4] = decel_dist_full * 0.25
	else
		init_speed = x_spd
	end
	
	x_last_spd = x_spd
	x_actual_spd = init_speed
	
	x_spd_mach_alt = find_mach_alt(B738DR_fmc_descent_speed, B738DR_fmc_descent_speed_mach)
	if x_start_alt < x_spd_mach_alt then
		x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
	else
		x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
	end
	
	flap_descent = calc_flap_desc(x_actual_spd)
	
	if flap_descent == 1 then
			flap_descent_speed = B738DR_fms_vref_40 + 70
			if flap_descent_speed > x_actual_spd then
				flap_distance = (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.7	--0.9
			else
				flap_descent = 0
				flap_distance = 0
			end
	elseif flap_descent == 2 then
		flap_descent_speed = B738DR_fms_vref_40 + 50
		-- flap_descent_speed = B738DR_fms_vref_40 + 40
		if flap_descent_speed > x_actual_spd then
			flap_distance = flap_distance + (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.5	--0.9
		else
			flap_descent = 0
			flap_distance = 0
		end
	-- elseif flap_descent > 0 then
		-- -- flap_distance = flap_dist_descent[flap_descent]
		-- flap_descent_speed = B738DR_fms_vref_40 + 50
		-- flap_distance = (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.9
	end
	
	--B738DR_fms_test4 = x_wpt_wind
		-- find nearest high altitude
		-- for jj = 1, 36 do
		for jj = 1, 37 do
			x_start_alt = roundUpToIncrement(x_start_alt, 10)
			if x_start_alt <= vnav_des_alt[jj] then
				idx_alt = jj
				
				if decel_active == 0 then
					alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
				else
					alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
				end
				high_alt = vnav_des_alt[idx_alt]
				if idx_alt > 1 then
					low_alt = vnav_des_alt[idx_alt-1]
				else
					low_alt = high_alt
				end
				pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
				
				alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
				
				
				if decel_active ~= 0 then
					calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
					if calc_fpm > 0 then
						calc_fpm2 = calc_fpm * 0.28
						calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
						calc_fpm4 = calc_fpm - (2 * calc_fpm3)
						calc_fpm3 = calc_fpm - calc_fpm3
						calc_fpm1 = calc_fpm2
						
						pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
						if (pom_pom1 * 2 + 0.1) > decel_dist_full then
							decel_dist[1] = decel_dist_full
							decel_dist[2] = decel_dist_full * 0.75
							decel_dist[3] = decel_dist_full * 0.5
							decel_dist[4] = decel_dist_full * 0.25
						else
							decel_dist[1] = decel_dist_full
							decel_dist[2] = decel_dist_full - pom_pom1
							pom_pom1 = decel_dist[2] - pom_pom1
							decel_dist[3] = pom_pom1
							
							calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
							
							if calc_fpm0 > calc_fpm2 + 100 then
								calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
								decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
								if pom_pom1 < decel_dist[4] + 0.1 then
									decel_dist[4] = pom_pom1 * 0.5
									calc_fpm1 = calc_fpm2
								end
							else
								decel_dist[4] = pom_pom1 * 0.5
							end
						end
					else
						calc_fpm1 = 0
						calc_fpm2 = 0
						calc_fpm3 = 0
						calc_fpm4 = 0
						decel_dist[1] = decel_dist_full
						decel_dist[2] = decel_dist_full * 0.75
						decel_dist[3] = decel_dist_full * 0.5
						decel_dist[4] = decel_dist_full * 0.25
					end
					decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
					decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
					decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
					decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
					
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
					-- if decel_active == 1 then
						-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
						-- decel_coef = 1
					-- elseif decel_active == 2 then
						-- x_spd_coef = decel_low_spd
						-- decel_coef = 1
					-- elseif decel_active == 3 then
						-- x_spd_coef = decel_low_spd
						-- decel_coef = 0.75
					-- else
						-- x_spd_coef = decel_low_spd	--init_speed
						-- decel_coef = 0.55
					-- end
					
					-- if idx_alt > 1 then
						-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
					-- else
						-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
					-- end
					
					-- if low_alt < 10000 then
						-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
					-- else
						-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
					-- end
					
					decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
					pth_angle = decel_path[decel_active]
					
					alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
					
				end
				
				-- create alt point
				vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
				vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = 0
				vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = x_start_alt
				if decel_active == 0 then
					vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind
				else
					vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + 1000 + x_wpt_wind
				end
				vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
				vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
				vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = x_wpt_idx	--0
				
				-- if high_alt == vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] and idx_alt < 36 then
				if high_alt == vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] and idx_alt < 37 then
					idx_alt = idx_alt + 1
					if decel_active == 0 then
						alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
					else
						alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
					end
					high_alt = vnav_des_alt[idx_alt]
					if idx_alt > 1 then
						low_alt = vnav_des_alt[idx_alt-1]
					else
						low_alt = high_alt
					end
					pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
					
					alt_remain_dist = alt_dist
					if decel_active ~= 0 then
						calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
						if calc_fpm > 0 then
							calc_fpm2 = calc_fpm * 0.28
							calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
							calc_fpm4 = calc_fpm - (2 * calc_fpm3)
							calc_fpm3 = calc_fpm - calc_fpm3
							calc_fpm1 = calc_fpm2
							
							pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							if (pom_pom1 * 2 + 0.1) > decel_dist_full then
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							else
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full - pom_pom1
								pom_pom1 = decel_dist[2] - pom_pom1
								decel_dist[3] = pom_pom1
								
								calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
								if calc_fpm0 > calc_fpm2 + 100 then
									calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
									decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
									if pom_pom1 < decel_dist[4] + 0.1 then
										decel_dist[4] = pom_pom1 * 0.5
										calc_fpm1 = calc_fpm2
									end
								else
									decel_dist[4] = pom_pom1 * 0.5
								end
							end
						else
							calc_fpm1 = 0
							calc_fpm2 = 0
							calc_fpm3 = 0
							calc_fpm4 = 0
							decel_dist[1] = decel_dist_full
							decel_dist[2] = decel_dist_full * 0.75
							decel_dist[3] = decel_dist_full * 0.5
							decel_dist[4] = decel_dist_full * 0.25
						end
						decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
						decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
						decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
						decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
						-- if decel_active == 1 then
							-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
							-- decel_coef = 1
						-- elseif decel_active == 2 then
							-- x_spd_coef = decel_low_spd
							-- decel_coef = 1
						-- elseif decel_active == 3 then
							-- x_spd_coef = decel_low_spd
							-- decel_coef = 0.75
						-- else
							-- x_spd_coef = decel_low_spd	--init_speed
							-- decel_coef = 0.55
						-- end
						
						-- if idx_alt > 1 then
							-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
						-- else
							-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
						-- end
						
						-- if low_alt < 10000 then
							-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
						-- else
							-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
						-- end
						
						decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
						pth_angle = decel_path[decel_active]
						
						alt_remain_dist = calc_path_distance(high_alt, low_alt, pth_angle)
					end
				end
				x_last_spd = x_spd
				if x_start_alt < x_spd_mach_alt then
					x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
				else
					x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
				end
				
				if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
					x_spd_mach_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
					x_spd_mach_test = 1
				end
				break
			end
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("xspd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("x_wpt_dist:" .. tostring(x_wpt_dist) .. "\n")
				dump_leg_append("decel_rest_dist:" .. tostring(decel_rest_dist) .. "\n")
				dump_leg_append("decel_dist_offset:" .. tostring(decel_dist_offset) .. "\n")
				dump_leg_append("x_last_spd:" .. tostring(x_last_spd) .. "\n")
				dump_leg_append("x_actual_spd:" .. tostring(x_actual_spd) .. "\n")
			end
			
		end
		
		while vnav_des_table_num[x_path] ~= 0 and end_path == 0 do
			
			-- if idx_alt > 36 then
				-- break
			-- end
			
			-- if x_spd_mach == 0 then
				-- x_spd_mach_alt = find_mach_alt(B738DR_fmc_descent_speed, B738DR_fmc_descent_speed_mach)
				
				-- if vnav_des_alt[idx_alt] >= x_spd_mach_alt then
					-- x_spd_mach = 1
					-- x_spd = B738DR_fmc_descent_speed_mach
				-- end
			-- end
			
			if x_spd_mach == 0 then
			
				x_dist = alt_remain_dist + vnav_des_table_dist[x_path][vnav_des_table_num[x_path]]
				
				work_loop = 0
				
				while work_loop == 0 do
					
					if x_spd_mach_test ~= 0 then
						compare_dist = 0
						if x_wpt_dist ~= 0 and decel_rest_dist ~= 0 then
							if x_spd_mach_dist < x_wpt_dist and x_spd_mach_dist < decel_rest_dist then
								compare_dist = 1
							end
						elseif x_wpt_dist ~= 0 then
							if x_spd_mach_dist < x_wpt_dist then
								compare_dist = 1
							end
						elseif decel_rest_dist ~= 0 then
							if x_spd_mach_dist < decel_rest_dist then
								compare_dist = 1
							end
						end
					end
					
					if compare_dist ~= 0 and x_dist > x_spd_mach_dist and x_spd_mach_test ~= 0 then
						-- create new mach alt point
						alt_dist2 = alt_remain_dist - (x_dist - x_spd_mach_dist)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create alt point
						vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
						vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = x_spd_mach_dist
						vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = x_start_alt
						vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 5000
						vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
						vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
						vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = 9000
						
						x_spd = B738DR_fmc_descent_speed_mach
						
						-- recalc x_wpt_wind to mach
						x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						
						alt_dist = take_alt_dist_mach(idx_alt, x_spd + x_wpt_wind)
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						x_spd_mach = 1
						flap_descent = 0
						flap_distance = 0
						break
					end
					
					-- check flap speed
					flaps_check = 0
					if flap_descent > 0 then
						if decel_rest_dist == 0 and x_wpt_dist == 0 then
							flaps_check = 1
						elseif decel_rest_dist == 0 then
							-- only wpt distance
							if flap_distance < x_wpt_dist then
								flaps_check = 1
							end
						elseif x_wpt_dist == 0 then
							-- only decel distance
							if flap_distance < decel_rest_dist then
								flaps_check = 1
							end
						else
							if flap_distance < x_wpt_dist and flap_distance < decel_rest_dist then
								flaps_check = 1
							end
						end
					end
					
					if flaps_check ~= 0 and x_dist > flap_distance then
						-- create flap alt point
						alt_dist2 = alt_remain_dist - (x_dist - flap_distance)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create alt point
						vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
						vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = flap_distance
						vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = x_start_alt
						if decel_active == 0 then
							vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 10000
						else
							vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 11000
						end
						if flap_descent > 0 then
							flap_descent = flap_descent - 1
						end
						vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
						if decel_active == 0 then
							x_actual_spd = x_last_spd
						else
							act_spd_dist = 0
							if vnav_des_table_num[x_path] > 1 then
								act_spd_dist = flap_distance - vnav_des_table_dist[x_path][vnav_des_table_num[x_path]-1]
							end
							x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
						end
						vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
						vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = 5000 + flap_descent
						
						if flap_descent == 1 then
							-- if x_spd > x_actual_spd then
								-- flap_distance = flap_distance + ((x_spd - x_actual_spd) * DES_SPEED_COEF)
								flap_descent_speed = B738DR_fms_vref_40 + 70
								if flap_descent_speed > x_actual_spd then
									flap_distance = flap_distance + (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.7	--0.9
								else
									flap_descent = 0
									flap_distance = 0
								end
							-- end
						elseif flap_descent == 2 then
							flap_descent_speed = B738DR_fms_vref_40 + 50
							if flap_descent_speed > x_actual_spd then
								flap_distance = flap_distance + (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.5	--0.9
							else
								flap_descent = 0
								flap_distance = 0
							end
						-- elseif flap_descent > 0 then
							-- flap_distance = flap_distance + flap_dist_descent[flap_descent]
						end
						-- recalc new descent speed/flap
						if x_start_alt < x_spd_mach_alt then
							x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
						else
							x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						end
						
						if decel_active == 0 then
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						else
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
							
							calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
							if calc_fpm > 0 then
								calc_fpm2 = calc_fpm * 0.28
								calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
								calc_fpm4 = calc_fpm - (2 * calc_fpm3)
								calc_fpm3 = calc_fpm - calc_fpm3
								calc_fpm1 = calc_fpm2
								
								pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								if (pom_pom1 * 2 + 0.1) > decel_dist_full then
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								else
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full - pom_pom1
									pom_pom1 = decel_dist[2] - pom_pom1
									decel_dist[3] = pom_pom1
									
									calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
									if calc_fpm0 > calc_fpm2 + 100 then
										calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
										decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
										if pom_pom1 < decel_dist[4] + 0.1 then
											decel_dist[4] = pom_pom1 * 0.5
											calc_fpm1 = calc_fpm2
										end
									else
										decel_dist[4] = pom_pom1 * 0.5
									end
								end
							else
								calc_fpm1 = 0
								calc_fpm2 = 0
								calc_fpm3 = 0
								calc_fpm4 = 0
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							end
							decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
							decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
							decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
							
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
							-- if decel_active == 1 then
								-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								-- decel_coef = 1
							-- elseif decel_active == 2 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 1
							-- elseif decel_active == 3 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 0.75
							-- else
								-- x_spd_coef = decel_low_spd	--init_speed
								-- decel_coef = 0.55
							-- end
							
							-- if idx_alt > 1 then
								-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
							-- else
								-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
							-- end
							
							-- if low_alt < 10000 then
								-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
							-- else
								-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
							-- end
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
							pth_angle = decel_path[decel_active]
							
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						end
						
						-- change x dist
						x_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + alt_remain_dist
						
						-- calc mach alt distance
						if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
							x_spd_mach_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
							x_spd_mach_test = 1
						end
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("FLAPS\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("xspd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("x_wpt_dist:" .. tostring(x_wpt_dist) .. "\n")
				dump_leg_append("decel_rest_dist:" .. tostring(decel_rest_dist) .. "\n")
				dump_leg_append("decel_dist_offset:" .. tostring(decel_dist_offset) .. "\n")
				dump_leg_append("x_last_spd:" .. tostring(x_last_spd) .. "\n")
				dump_leg_append("x_dist:" .. tostring(x_dist) .. "\n")
				dump_leg_append("x_actual_spd:" .. tostring(x_actual_spd) .. "\n")
			end
					elseif (x_wpt_dist < decel_rest_dist and x_wpt_dist ~= 0) or (decel_rest_dist == 0) then
						if x_dist > x_wpt_dist and x_wpt_dist ~= 0 then
							-- create new wpt alt point
							alt_dist2 = alt_remain_dist - (x_dist - x_wpt_dist)
							x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
							-- create alt point
							vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
							vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = x_wpt_dist
							vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = x_start_alt
							if spd_rest_idx >= x_wpt_idx - 1 then
								if decel_active == 0 then
									vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 7000
								else
									vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 6000
								end
							else
								if decel_active == 0 then
									vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 9000
								else
									vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 8000
								end
							end
							vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
							if decel_active == 0 then
								x_actual_spd = x_last_spd
							else
								act_spd_dist = 0
								if vnav_des_table_num[x_path] > 1 then
									act_spd_dist = x_wpt_dist - vnav_des_table_dist[x_path][vnav_des_table_num[x_path]-1]
								end
								x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
							end
							
							-- wpt is spd rest idx ???
							if spd_rest_idx >= x_wpt_idx - 1 then
								if spd_rest_type == 1 then	-- below
									if x_actual_spd > x_spd then
										x_actual_spd = x_spd
									end
								else
									x_actual_spd = x_spd
								end
								spd_rest_dist_temp = 0
								if spd_rest_idx < x_idx then
									for xx = x_idx, spd_rest_idx + 1, -1 do
										spd_rest_dist_temp = spd_rest_dist_temp + wpt_distance(xx)	--legs_data[xx][3]
									end
								end
								-- find new spd rest
								spd_rest_idx, spd_rest, spd_rest_dist, spd_rest_type = find_spd_rest(spd_rest_idx, spd_rest_dist, 0)
							end
							
							
							vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
							vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = x_wpt_idx - 1
							
							-- -- save calculated speed
							-- legs_data[x_wpt_idx-1][10] = x_actual_spd
							
							
							-- if x_start_alt <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
							if x_start_alt < B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
								fixed_speed = math.min(B738DR_fmc_descent_speed, B738DR_fmc_descent_r_speed1)
								if fixed_speed == B738DR_fmc_descent_r_speed1 then
									fl100_active = 1
								end
							else
								fixed_speed = B738DR_fmc_descent_speed
							end
							x_spd = fixed_speed
							if spd_rest_idx ~= 0 then
								x_spd = math.min(x_spd, spd_rest)
							end
							
							if x_spd > x_last_spd then
								x_spd_delta = x_spd - x_actual_spd
								
								if spd_rest_idx ~= 0 and spd_rest <= x_spd then
									decel_dist_full = x_spd_delta * DES_SPEED_COEF
									decel_dist_offset = spd_rest_dist_temp
									decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								else
									decel_dist_full = x_spd_delta * DES_SPEED_COEF
									decel_dist_offset = x_wpt_dist
									decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								end
								decel_active = 4
								decel_low_spd = x_actual_spd
								decel_high_spd = x_spd
								x_spd_coef = decel_low_spd--init_speed
								decel_coef = 0.55
							end
							x_last_spd = x_spd
							
							-- find next wpt
							local wpt_finded = 0
							local temp_dist = 0
							if x_start_alt < x_spd_mach_alt then
								x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
							else
								x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
							end
							x_wpt_idx = x_wpt_idx - 1
							if x_wpt_idx > 1 then
								x_wpt_dist = x_wpt_dist + wpt_distance(x_wpt_idx)	--legs_data[x_wpt_idx][3]
								if legs_data[x_wpt_idx][3] ~= 0 then
									if x_start_alt < x_spd_mach_alt then
										x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
									else
										x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
									end
									wpt_finded = 1
								end
								while legs_data[x_wpt_idx][3] == 0 do
									x_wpt_idx = x_wpt_idx - 1
									if x_wpt_idx > 1 then
										if legs_data[x_wpt_idx][3] ~= 0 then
											x_wpt_dist = x_wpt_dist + wpt_distance(x_wpt_idx)	--legs_data[x_wpt_idx][3]
											wpt_finded = 1
											if x_start_alt < x_spd_mach_alt then
												x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
											else
												x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
											end
										end
									else
										break
									end
								end
							end
							if wpt_finded == 0 then
								x_wpt_idx = 0
								x_wpt_dist = 0
								x_wpt_wind = 0
							end
							
							if decel_active == 0 then
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
							else
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
							end
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
							
							if decel_active ~= 0 then
								calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
								if calc_fpm > 0 then
									calc_fpm2 = calc_fpm * 0.28
									calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
									calc_fpm4 = calc_fpm - (2 * calc_fpm3)
									calc_fpm3 = calc_fpm - calc_fpm3
									calc_fpm1 = calc_fpm2
									
									pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
									if (pom_pom1 * 2 + 0.1) > decel_dist_full then
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full * 0.75
										decel_dist[3] = decel_dist_full * 0.5
										decel_dist[4] = decel_dist_full * 0.25
									else
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full - pom_pom1
										pom_pom1 = decel_dist[2] - pom_pom1
										decel_dist[3] = pom_pom1
										
										calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
										if calc_fpm0 > calc_fpm2 + 100 then
											calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
											decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
											if pom_pom1 < decel_dist[4] + 0.1 then
												decel_dist[4] = pom_pom1 * 0.5
												calc_fpm1 = calc_fpm2
											end
										else
											decel_dist[4] = pom_pom1 * 0.5
										end
									end
								else
									calc_fpm1 = 0
									calc_fpm2 = 0
									calc_fpm3 = 0
									calc_fpm4 = 0
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								end
								decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
								decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
								decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
								
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
								-- if decel_active == 1 then
									-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
									-- decel_coef = 1
								-- elseif decel_active == 2 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 1
								-- elseif decel_active == 3 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 0.75
								-- else
									-- x_spd_coef = decel_low_spd	--init_speed
									-- decel_coef = 0.55
								-- end
								
								-- if idx_alt > 1 then
									-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
								-- else
									-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
								-- end
								
								-- if low_alt < 10000 then
									-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
								-- else
									-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
								-- end
								
								decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
								pth_angle = decel_path[decel_active]
								
								alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
							end
							
							-- change x dist
							x_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + alt_remain_dist
							
							-- calc mach alt distance
							if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
								x_spd_mach_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
								x_spd_mach_test = 1
							end
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("WPT " .. legs_data[x_wpt_idx][1] .. "\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("xspd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("x_wpt_dist:" .. tostring(x_wpt_dist) .. "\n")
				dump_leg_append("decel_rest_dist:" .. tostring(decel_rest_dist) .. "\n")
				dump_leg_append("decel_dist_offset:" .. tostring(decel_dist_offset) .. "\n")
				dump_leg_append("x_last_spd:" .. tostring(x_last_spd) .. "\n")
				dump_leg_append("x_dist:" .. tostring(x_dist) .. "\n")
				dump_leg_append("x_actual_spd:" .. tostring(x_actual_spd) .. "\n")
			end
						else
							work_loop = 1
							-- NEXT ALT
							vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
							vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = x_dist
							vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = high_alt
							if decel_active == 0 then
								vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind
							else
								vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 1000
							end
							vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
							if decel_active == 0 then
								x_actual_spd = x_last_spd
							else
								act_spd_dist = 0
								if vnav_des_table_num[x_path] > 1 then
									act_spd_dist = x_dist - vnav_des_table_dist[x_path][vnav_des_table_num[x_path]-1]
								end
								x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
							end
							vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
							vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = 0
							
							x_start_alt = high_alt
							idx_alt = idx_alt + 1
							-- if idx_alt > 36 then
							if idx_alt > 37 then
								end_path = 1
								break
							end
							low_alt = high_alt
							high_alt = vnav_des_alt[idx_alt]
							
							-- check speed
							-- if x_start_alt <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
							if x_start_alt < B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
								fixed_speed = math.min(B738DR_fmc_descent_speed, B738DR_fmc_descent_r_speed1)
								if fixed_speed == B738DR_fmc_descent_r_speed1 then
									fl100_active = 1
								end
							else
								fixed_speed = B738DR_fmc_descent_speed
							end
							x_spd = fixed_speed
							
							if spd_rest_idx ~= 0 then
								x_spd = math.min(x_spd, spd_rest)
							end
							
							if x_spd > x_last_spd then
								x_spd_delta = x_spd - x_last_spd
								decel_dist_full = x_spd_delta * DES_SPEED_COEF
								decel_dist_offset = x_dist
								decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								decel_active = 4
								decel_low_spd = x_last_spd
								decel_high_spd = x_spd
								x_spd_coef = decel_low_spd	--init_speed
								decel_coef = 0.55
							end
							x_last_spd = x_spd
							
							if x_start_alt < x_spd_mach_alt then
								x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
							else
								x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
							end
							
							if decel_active == 0 then
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
							else
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
							end
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							
							alt_remain_dist = alt_dist
							
							
							if decel_active ~= 0 then
					if active_dump ~= 0 then
						dump_leg_append("\n")
						dump_leg_append("alt_remain_dist:" .. tostring(alt_remain_dist) .. "\n")
						dump_leg_append("pth_angle deg:" .. tostring(math.deg(pth_angle)) .. "\n")
						dump_leg_append("\n")
					end
								
								calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
								if calc_fpm > 0 then
									calc_fpm2 = calc_fpm * 0.28
									calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
									calc_fpm4 = calc_fpm - (2 * calc_fpm3)
									calc_fpm3 = calc_fpm - calc_fpm3
									calc_fpm1 = calc_fpm2
									
									pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
									if (pom_pom1 * 2 + 0.1) > decel_dist_full then
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full * 0.75
										decel_dist[3] = decel_dist_full * 0.5
										decel_dist[4] = decel_dist_full * 0.25
									else
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full - pom_pom1
										pom_pom1 = decel_dist[2] - pom_pom1
										decel_dist[3] = pom_pom1
										
										calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
										if calc_fpm0 > calc_fpm2 + 100 then
											calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
											decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
											if pom_pom1 < decel_dist[4] + 0.1 then
												decel_dist[4] = pom_pom1 * 0.5
												calc_fpm1 = calc_fpm2
											end
										else
											decel_dist[4] = pom_pom1 * 0.5
										end
									end
								else
									calc_fpm1 = 0
									calc_fpm2 = 0
									calc_fpm3 = 0
									calc_fpm4 = 0
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								end
								decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
								decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
								decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
								
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
								-- if decel_active == 1 then
									-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
									-- decel_coef = 1
								-- elseif decel_active == 2 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 1
								-- elseif decel_active == 3 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 0.75
								-- else
									-- x_spd_coef = decel_low_spd	--init_speed
									-- decel_coef = 0.55
								-- end
								
								-- if idx_alt > 1 then
									-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
								-- else
									-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
								-- end
								
								-- if low_alt < 10000 then
									-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
								-- else
									-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
								-- end
								
								decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
								pth_angle = decel_path[decel_active]
								
								alt_remain_dist = calc_path_distance(high_alt, low_alt, pth_angle)
							end
							
							-- calc mach alt distance
							if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
								x_spd_mach_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
								x_spd_mach_test = 1
							end
							
							
							
							
							
							
							
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEXT ALT\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("xspd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("x_wpt_dist:" .. tostring(x_wpt_dist) .. "\n")
				dump_leg_append("decel_rest_dist:" .. tostring(decel_rest_dist) .. "\n")
				dump_leg_append("decel_dist_offset:" .. tostring(decel_dist_offset) .. "\n")
				dump_leg_append("x_last_spd:" .. tostring(x_last_spd) .. "\n")
				dump_leg_append("x_dist:" .. tostring(x_dist) .. "\n")
				dump_leg_append("x_actual_spd:" .. tostring(x_actual_spd) .. "\n")
				dump_leg_append("x_spd_coef:" .. tostring(x_spd_coef) .. "\n")
				dump_leg_append("alt_dist2:" .. tostring(alt_dist2) .. "\n")
				dump_leg_append("alt_remain_dist:" .. tostring(alt_remain_dist) .. "\n")
				dump_leg_append("pth_angle deg:" .. tostring(math.deg(pth_angle)) .. "\n")
			end
						end
						
					elseif x_dist > decel_rest_dist and decel_rest_dist ~= 0 then
						-- DECEL point
						alt_dist2 = alt_remain_dist - (x_dist - decel_rest_dist)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create DECEL point
						vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
						vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = decel_rest_dist
						vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = x_start_alt
						vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 3000
						vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
						
						
						
						-- x_actual_spd = x_last_spd
						
						-- if decel_active == 1 then
							-- x_actual_spd = decel_high_spd
						-- elseif decel_active == 2 then
							-- x_actual_spd = decel_low_spd + ((decel_high_spd - decel_low_spd) * 0.25 * 3)
						-- elseif decel_active == 3 then
							-- x_actual_spd = decel_low_spd + ((decel_high_spd - decel_low_spd) * 0.25 * 2)
						-- else
							-- x_actual_spd = decel_low_spd + ((decel_high_spd - decel_low_spd) * 0.25)
						-- end
						
						if decel_active == 1 then
							x_actual_spd = decel_high_spd
						elseif decel_active == 2 then
							x_actual_spd = decel_low_spd + (decel_dist[2] / DES_SPEED_COEF)
						elseif decel_active == 3 then
							x_actual_spd = decel_low_spd + (decel_dist[3] / DES_SPEED_COEF)
						else
							x_actual_spd = decel_low_spd + (decel_dist[4] / DES_SPEED_COEF)
						end
						
						-- x_actual_spd = x_last_spd
						vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
						vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = 3000 + decel_active
						
						-- if x_spd == B738DR_fmc_descent_speed and x_edalt <= B738DR_fmc_descent_r_alt1 then
							-- fl100_desc_dist = decel_rest_dist
						-- end
						
						if fl100_active == 1 and decel_active == 1 then
							-- fl100_active = 2
							-- fl100_desc_dist = decel_rest_dist
							if B738DR_fmc_descent_r_speed1 ~= 0 then
								-- if x_start_alt > B738DR_fmc_descent_r_alt1 and x_spd > B738DR_fmc_descent_r_speed1 then
								if x_start_alt >= B738DR_fmc_descent_r_alt1 and x_spd > B738DR_fmc_descent_r_speed1 then
									fl100_active = 2
									fl100_desc_dist = decel_rest_dist
								end
							end
						end
						
						x_spd_coef = 1.0
						if decel_active > 0 then
							decel_active = decel_active - 1
						end
						if decel_active == 0 then
							decel_rest_dist = 0
						else
							if decel_active == 1 then
								x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								decel_coef = 1
								-- decel_rest_dist = decel_dist_offset + decel_dist_full
							elseif decel_active == 2 then
								x_spd_coef = decel_low_spd
								decel_coef = 1
								-- decel_rest_dist = decel_dist_offset + decel_dist_full * 0.75
							elseif decel_active == 3 then
								x_spd_coef = decel_low_spd
								decel_coef = 0.75
								-- decel_rest_dist = decel_dist_offset + decel_dist_full * 0.5
							end
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
						end
						
						if decel_active == 0 then
							-- if x_start_alt <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
							if x_start_alt < B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
								fixed_speed = math.min(B738DR_fmc_descent_speed, B738DR_fmc_descent_r_speed1)
							else
								fixed_speed = B738DR_fmc_descent_speed
							end
							x_spd = fixed_speed
							if spd_rest_idx ~= 0 then
								x_spd = math.min(x_spd, spd_rest)
							end
							
							if x_spd > x_last_spd then
								x_spd_delta = x_spd - x_last_spd
								
								-- x_spd_delta_lim = math.min(x_spd_delta, 40)
								-- if x_spd_delta_lim < 10 then
									-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
								-- else
									-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
								-- end
								decel_dist_full = x_spd_delta * DES_SPEED_COEF
								decel_dist_offset = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]]
								decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								decel_active = 4	--1
								decel_low_spd = x_last_spd
								decel_high_spd = x_spd
								x_spd_coef = decel_low_spd	--init_speed
								decel_coef = 0.55
							end
							
							x_last_spd = x_spd
						end
						
						--x_actual_spd = x_spd
						
						if x_start_alt < x_spd_mach_alt then
							x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
						else
							x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						end
						
						if decel_active == 0 then
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
						else
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
						end
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						
						if decel_active ~= 0 then
							
							calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
							if calc_fpm > 0 then
								calc_fpm2 = calc_fpm * 0.28
								calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
								calc_fpm4 = calc_fpm - (2 * calc_fpm3)
								calc_fpm3 = calc_fpm - calc_fpm3
								calc_fpm1 = calc_fpm2
								
								pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								if (pom_pom1 * 2 + 0.1) > decel_dist_full then
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								else
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full - pom_pom1
									pom_pom1 = decel_dist[2] - pom_pom1
									decel_dist[3] = pom_pom1
									
									calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
									if calc_fpm0 > calc_fpm2 + 100 then
										calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
										decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
										if pom_pom1 < decel_dist[4] + 0.1 then
											decel_dist[4] = pom_pom1 * 0.5
											calc_fpm1 = calc_fpm2
										end
									else
										decel_dist[4] = pom_pom1 * 0.5
									end
								end
							else
								calc_fpm1 = 0
								calc_fpm2 = 0
								calc_fpm3 = 0
								calc_fpm4 = 0
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							end
							decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
							decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
							decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
							
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
							-- if decel_active == 1 then
								-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								-- decel_coef = 1
							-- elseif decel_active == 2 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 1
							-- elseif decel_active == 3 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 0.75
							-- else
								-- x_spd_coef = decel_low_spd	--init_speed
								-- decel_coef = 0.55
							-- end
							
							-- if idx_alt > 1 then
								-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
							-- else
								-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
							-- end
							
							-- if low_alt < 10000 then
								-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
							-- else
								-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
							-- end
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
							pth_angle = decel_path[decel_active]
							
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						end
						
						-- change x dist
						x_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + alt_remain_dist
						
						-- calc mach alt distance
						if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
							x_spd_mach_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
							x_spd_mach_test = 1
						end
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("DECEL\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("xspd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("x_wpt_dist:" .. tostring(x_wpt_dist) .. "\n")
				dump_leg_append("decel_rest_dist:" .. tostring(decel_rest_dist) .. "\n")
				dump_leg_append("decel_dist_offset:" .. tostring(decel_dist_offset) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("x_last_spd:" .. tostring(x_last_spd) .. "\n")
				dump_leg_append("x_dist:" .. tostring(x_dist) .. "\n")
				dump_leg_append("x_actual_spd:" .. tostring(x_actual_spd) .. "\n")
			end
					else
						work_loop = 1
						-- NEXT ALT
						vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
						vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = x_dist
						vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = high_alt
						if decel_active == 0 then
							vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind
						else
							vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 2000
						end
						vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
						if decel_active == 0 then
							x_actual_spd = x_last_spd
						else
							act_spd_dist = 0
							if vnav_des_table_num[x_path] > 1 then
								act_spd_dist = x_dist - vnav_des_table_dist[x_path][vnav_des_table_num[x_path]-1]
							end
							x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
						end
						vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
						vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = 0
						
						x_start_alt = high_alt
						idx_alt = idx_alt + 1
						-- if idx_alt > 36 then
						if idx_alt > 37 then
							end_path = 1
							break
						end
						low_alt = high_alt
						high_alt = vnav_des_alt[idx_alt]
						
						-- check speed
						-- if x_start_alt <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
						if x_start_alt < B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_speed1 ~= 0 then
							fixed_speed = math.min(B738DR_fmc_descent_speed, B738DR_fmc_descent_r_speed1)
							if fixed_speed == B738DR_fmc_descent_r_speed1 then
								fl100_active = 1
							end
						else
							fixed_speed = B738DR_fmc_descent_speed
						end
						x_spd = fixed_speed
						if spd_rest_idx ~= 0 then
							x_spd = math.min(x_spd, spd_rest)
						end
						
						if x_spd > x_last_spd then
							x_spd_delta = x_spd - x_last_spd
							-- x_spd_delta_lim = math.min(x_spd_delta, 40)
							-- if x_spd_delta_lim < 10 then
								-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
							-- else
								-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
							-- end
							decel_dist_full = x_spd_delta * DES_SPEED_COEF
							decel_dist_offset = x_dist
							decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
							decel_active = 4	--1
							decel_low_spd = x_last_spd
							decel_high_spd = x_spd
							x_spd_coef = decel_low_spd	--init_speed
							decel_coef = 0.55
						end
						x_last_spd = x_spd
						
						if x_start_alt < x_spd_mach_alt then
							x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 0)	-- wpt wind
						else
							x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						end
						
						if decel_active == 0 then
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
						else
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
						end
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						
						alt_remain_dist = alt_dist
						
						
						if decel_active ~= 0 then
							
							calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
							if calc_fpm > 0 then
								calc_fpm2 = calc_fpm * 0.28
								calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
								calc_fpm4 = calc_fpm - (2 * calc_fpm3)
								calc_fpm3 = calc_fpm - calc_fpm3
								calc_fpm1 = calc_fpm2
								
								pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								if (pom_pom1 * 2 + 0.1) > decel_dist_full then
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								else
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full - pom_pom1
									pom_pom1 = decel_dist[2] - pom_pom1
									decel_dist[3] = pom_pom1
									
									calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
									if calc_fpm0 > calc_fpm2 + 100 then
										calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
										decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
										if pom_pom1 < decel_dist[4] + 0.1 then
											decel_dist[4] = pom_pom1 * 0.5
											calc_fpm1 = calc_fpm2
										end
									else
										decel_dist[4] = pom_pom1 * 0.5
									end
								end
							else
								calc_fpm1 = 0
								calc_fpm2 = 0
								calc_fpm3 = 0
								calc_fpm4 = 0
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							end
							decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
							decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
							decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
							
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEW DECEL calc\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("Decel path:" .. tostring(math.deg(decel_path[decel_active])) .. "\n")
				dump_leg_append("Pth angle:" .. tostring(math.deg(pth_angle)) .. "\n")
				dump_leg_append("x_spd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("calc_fpm:" .. tostring(calc_fpm) .. "\n")
				dump_leg_append("calc_fpm1:" .. tostring(calc_fpm1) .. "\n")
				dump_leg_append("calc_fpm2:" .. tostring(calc_fpm2) .. "\n")
				dump_leg_append("calc_fpm3:" .. tostring(calc_fpm3) .. "\n")
				dump_leg_append("calc_fpm4:" .. tostring(calc_fpm4) .. "\n")
				dump_leg_append("decel_dist_full:" .. tostring(decel_dist_full) .. "\n")
				dump_leg_append("decel_dist[1]:" .. tostring(decel_dist[1]) .. "\n")
				dump_leg_append("decel_dist[2]:" .. tostring(decel_dist[2]) .. "\n")
				dump_leg_append("decel_dist[3]:" .. tostring(decel_dist[3]) .. "\n")
				dump_leg_append("decel_dist[4]:" .. tostring(decel_dist[4]) .. "\n")
			end
			
							-- if decel_active == 1 then
								-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								-- decel_coef = 1
							-- elseif decel_active == 2 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 1
							-- elseif decel_active == 3 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 0.75
							-- else
								-- x_spd_coef = decel_low_spd	--init_speed
								-- decel_coef = 0.55
							-- end
							
							-- if idx_alt > 1 then
								-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
							-- else
								-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
							-- end
							
							-- if low_alt < 10000 then
								-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
							-- else
								-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef	--0.55	--0.58
							-- end
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
							pth_angle = decel_path[decel_active]
							
							alt_remain_dist = calc_path_distance(high_alt, low_alt, pth_angle)
						end
						
						-- calc mach alt distance
						if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
							x_spd_mach_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
							x_spd_mach_test = 1
						end
						
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("NEXT ALT2\n")
				dump_leg_append(tostring(x_start_alt) .. "> \n")
				dump_leg_append("Decel active:" .. tostring(decel_active) .. "\n")
				dump_leg_append("xspd:" .. tostring(x_spd) .. "\n")
				dump_leg_append("x_wpt_dist:" .. tostring(x_wpt_dist) .. "\n")
				dump_leg_append("decel_rest_dist:" .. tostring(decel_rest_dist) .. "\n")
				dump_leg_append("decel_dist_offset:" .. tostring(decel_dist_offset) .. "\n")
				dump_leg_append("x_last_spd:" .. tostring(x_last_spd) .. "\n")
				dump_leg_append("x_dist:" .. tostring(x_dist) .. "\n")
				dump_leg_append("x_actual_spd:" .. tostring(x_actual_spd) .. "\n")
			end
					end
				end
			else
				x_spd = B738DR_fmc_descent_speed_mach
				
				x_dist = alt_remain_dist + vnav_des_table_dist[x_path][vnav_des_table_num[x_path]]
				
				-- mach speed
				work_loop = 0
				
				while work_loop == 0 do
					
					if x_dist > x_wpt_dist and x_wpt_dist ~= 0 then
						-- create new wpt alt point
						alt_dist2 = alt_remain_dist - (x_dist - x_wpt_dist)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create alt point
						vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
						vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = x_wpt_dist
						vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = x_start_alt
						vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind + 9000
						vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
						x_actual_spd = x_last_spd
						vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
						vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = x_wpt_idx - 1
						
						-- find next wpt
						local wpt_finded = 0
						x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						x_wpt_idx = x_wpt_idx - 1
						if x_wpt_idx > 1 then
							x_wpt_dist = x_wpt_dist + wpt_distance(x_wpt_idx)	--legs_data[x_wpt_idx][3]
							if legs_data[x_wpt_idx][3] ~= 0 then
								x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
								wpt_finded = 1
							end
							while legs_data[x_wpt_idx][3] == 0 do
								x_wpt_idx = x_wpt_idx - 1
								if x_wpt_idx > 1 then
									if legs_data[x_wpt_idx][3] ~= 0 then
										x_wpt_dist = x_wpt_dist + wpt_distance(x_wpt_idx)	--legs_data[x_wpt_idx][3]
										wpt_finded = 1
										x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
									end
								else
									break
								end
							end
						end
						if wpt_finded == 0 then
							x_wpt_idx = 0
							x_wpt_dist = 0
							x_wpt_wind = 0
						end
						
						alt_dist = take_alt_dist_mach(idx_alt, x_spd + x_wpt_wind)
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						
						-- change x dist
						x_dist = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] + alt_remain_dist
					else
						-- NEXT ALT
						
						work_loop = 1
						
						vnav_des_table_num[x_path] = vnav_des_table_num[x_path] + 1
						vnav_des_table_dist[x_path][vnav_des_table_num[x_path]] = x_dist
						vnav_des_table_alt[x_path][vnav_des_table_num[x_path]] = high_alt
						vnav_des_table_spd[x_path][vnav_des_table_num[x_path]] = x_spd + x_wpt_wind 
						vnav_des_table_flap[x_path][vnav_des_table_num[x_path]] = flap_descent
						x_actual_spd = x_last_spd
						vnav_des_table_act_spd[x_path][vnav_des_table_num[x_path]] = x_actual_spd
						vnav_des_table_type[x_path][vnav_des_table_num[x_path]] = 0
						
						x_start_alt = high_alt
						idx_alt = idx_alt + 1
						-- if idx_alt > 36 then
						if idx_alt > 37 then
							end_path = 1
							break
						end
						low_alt = high_alt
						high_alt = vnav_des_alt[idx_alt]
						
						x_wpt_wind = wpt_wind_spd(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						
						alt_dist = take_alt_dist_mach(idx_alt, x_spd + x_wpt_wind)
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						
						alt_remain_dist = alt_dist
					end
				end
			end
			
		end
		
		-- calc descent speed restrict FL100
		if active_dump ~= 0 then
			-- activate dump data
			dump_leg_append("FL100\n")
			dump_leg_append("fl100_active:" .. tostring(fl100_active) .. "\n")
			dump_leg_append("fl100_desc_dist:" .. tostring(fl100_desc_dist) .. "\n")
		end
		
		local fl100_idx = 0
		local fl100_dist = 0
		if x_edalt <= B738DR_fmc_descent_r_alt1 and B738DR_fmc_descent_r_alt1 ~= 0 and x_crzalt > B738DR_fmc_descent_r_alt1 then
			if fl100_desc_dist ~= 0 and B738DR_fmc_descent_r_alt1 ~= 0 and B738DR_fmc_descent_r_speed1 ~= 0 then
					local fl100_temp = 0
					if ed_found ~= x_idx then
						if ed_found > x_idx then
							for xx = ed_found, x_idx + 1, -1 do
								fl100_temp = fl100_temp + wpt_distance(xx)	--legs_data[xx][3]
							end
						end
					end
					fl100_desc_dist = fl100_desc_dist + fl100_temp
					if x_in_dist_corr ~= 0 then
						fl100_desc_dist = fl100_desc_dist + x_in_dist_corr
					end
					
					if active_dump ~= 0 then
						-- activate dump data
						dump_leg_append("fl100_desc_dist 2nd:" .. tostring(fl100_desc_dist) .. "\n")
					end
					
					fl100_temp = 0
					for xx = ed_found, 2, -1 do
						fl100_temp = fl100_temp + wpt_distance(xx)
						if fl100_desc_dist < fl100_temp then
							fl100_idx = xx
							fl100_dist = wpt_distance(xx) - (fl100_temp - fl100_desc_dist)
							break
						end
					end
					
					if active_dump ~= 0 then
						-- activate dump data
						dump_leg_append("fl100_idx:" .. tostring(fl100_idx) .. "\n")
					end
					
					if fl100_idx > legs_num + 1 or fl100_dist < 0 then
						fl100_idx = 0
						fl100_dist = 0
					end
			else
				fl100_desc_dist = 0
			end
			B738DR_FL100_desc_dist = fl100_desc_dist
			B738DR_FL100_idx = fl100_idx
			B738DR_FL100_dist = fl100_dist
			
			if active_dump ~= 0 then
				-- activate dump data
				dump_leg_append("fl100_idx final:" .. tostring(fl100_idx) .. "\n")
				dump_leg_append("fl100_dist final:" .. tostring(fl100_dist) .. "\n")
			end
		end

if active_dump ~= 0 then
	if B738DR_fms_test == 747 then
		B738DR_fms_test = 0
	end
end

end

function create_idle_path2(x_idx, x_edalt, x_crzalt, x_path, x_in_dist_corr, in_rte)
	
	local ii = 0
	local jj = 0
	local alt_dist = 0			-- alt distance all
	local alt_start_dist = 0	-- alt distance from start
	local alt_remain_dist = 0	-- alt distance remain
	local pth_angle = 0
	local low_alt = 0
	local high_alt = 0
	local idx_alt = 0
	local x_start_alt = x_edalt
	local x_spd = 0
	local x_dist = 0			-- total distance
	local x_last_spd = 0
	local x_actual_spd = 0
	local act_spd_dist = 0
	local alt_dist2 = 0
	
	local x_spd_mach = 0
	local x_spd_mach_test = 0
	local x_spd_mach_dist = 0
	local x_spd_mach_alt = 0
	
	local spd_rest_idx = 0
	local spd_rest = 0
	local spd_rest_dist = 0
	local spd_rest_type = 0
	local fixed_speed = 0
	
	local x_spd_delta = 0
	local x_spd_coef = 1
	
	local spd_rest_dist_temp = 0
	local decel_rest_dist = 0
	local decel_active = 0
	local decel_low_spd = 0
	local decel_high_spd = 0
	local decel_coef = 0
	local decel_dist_full = 0
	local decel_dist_offset = 0
	local x_spd_delta_lim = 0
	
	local x_wpt_idx = x_idx
	local x_wpt_dist = wpt_distance2(x_wpt_idx, in_rte)	--legs_data2_buf[x_wpt_idx][3]
	if x_in_dist_corr ~= 0 then
		x_wpt_dist = wpt_distance2(x_wpt_idx, in_rte) - x_in_dist_corr
	end
	local x_wpt_wind = 0
	
	local work_loop = 0
	local end_path = 0
	
	local fl100_desc_dist = 0
	local fl100_active = 0
	local compare_dist = 0
	
	local flap_descent = 0
	local flap_distance = 0
	local flap_descent_speed = 0
	local flaps_check = 0
	-- raw_table('flap_dist_descent')
	-- local flap_dist_descent = {}
	-- flap_dist_descent[1] = 20 * DES_SPEED_COEF
	-- flap_dist_descent[2] = 20 * DES_SPEED_COEF
	-- flap_dist_descent[3] = 20 * DES_SPEED_COEF
	-- flap_dist_descent[4] = 10 * DES_SPEED_COEF
	
	-- local des_speed_res = 0
	-- local des_altit_res = 0
	
	local des_speed_res = descent_r_speed1_mod
	local des_altit_res = descent_r_alt1_mod
	
	if x_path == 1 then
		B738DR_FL100_desc_dist_mod = 0
		B738DR_FL100_idx_mod = 0
		B738DR_FL100_dist_mod = 0
	end
	
	local calc_fpm = 0
	local calc_fpm0 = 0
	local calc_fpm1 = 0
	local calc_fpm2 = 0
	local calc_fpm3 = 0
	local calc_fpm4 = 0
	local pom_pom1 = 0
	raw_table('decel_path')
	local decel_path = {}
	decel_path[1] = 0
	decel_path[2] = 0
	decel_path[3] = 0
	decel_path[4] = 0
	raw_table('decel_dist')
	local decel_dist = {}
	decel_dist[1] = 0
	decel_dist[2] = 0
	decel_dist[3] = 0
	decel_dist[4] = 0
	
	
	vnav_des_table_dist2[x_path] = {}
	vnav_des_table_alt2[x_path] = {}
	vnav_des_table_spd2[x_path] = {}
	vnav_des_table_flap2[x_path] = {}
	vnav_des_table_act_spd2[x_path] = {}
	vnav_des_table_type2[x_path] = {}
	vnav_des_table_num2[x_path] = 0
	
	while x_wpt_dist == 0 do
		x_wpt_idx = x_wpt_idx - 1
		if x_wpt_idx > 1 then
			x_wpt_dist = wpt_distance2(x_wpt_idx, in_rte)	--legs_data2_buf[x_wpt_idx][3]
		else
			break
		end
	end
	
	-- if x_start_alt <= des_altit_res and des_speed_res ~= 0 then
	if x_start_alt < des_altit_res and des_speed_res ~= 0 then
		fixed_speed = math.min(B738DR_fmc_descent_speed, des_speed_res)
		-- if fixed_speed == B738DR_fmc_descent_r_speed1 then
		if fixed_speed == des_speed_res then
			fl100_active = 1
		end
	else
		fixed_speed = B738DR_fmc_descent_speed
	end
	
	local init_speed = 0
	local init_speed_active = 0
	local init_decel_dist = 0
	local init_speed2 = 0
	
	
	
	
	if x_path == 1 then
		init_speed = 180
		local x_idx99 = x_idx
		while x_idx99 <= legs_num2_buf + 1 do
			if x_idx99 == x_idx then
				init_decel_dist = x_in_dist_corr
			else
				init_decel_dist = init_decel_dist + wpt_distance2(x_idx99, in_rte)
			end
			if legs_data2_buf[x_idx99][17] == 1 then
				if B738DR_fms_vref_40 > 0 then
					init_speed = B738DR_fms_vref_40 + 20
				end
				if init_decel_dist ~= 0 then
					init_speed = init_speed + (init_decel_dist / DES_SPEED_COEF)
				end
				break
			end
			x_idx99 = x_idx99 + 1
		end
		init_decel_dist = 0
		
	elseif x_path > 1 then
		if vnav_des_table_num2[x_path-1] > 0 then
			for ii = 1, vnav_des_table_num2[x_path-1] do
				if vnav_des_table_type2[x_path-1][ii] > 0 and vnav_des_table_type2[x_path-1][ii] < 1000 then
					if init_speed ~= 0 then
						if vnav_des_table_type2[x_path-1][ii] <= jj - 1 then
							init_speed2 = vnav_des_table_act_spd2[x_path-1][ii]
							if init_speed2 > init_speed then
								init_speed = init_speed + (x_in_dist_corr / DES_SPEED_COEF)
								if init_speed > init_speed2 then
									init_speed = init_speed2
								end
							end
							break
						end
					else
						if vnav_des_table_type2[x_path-1][ii] <= x_idx then
							init_speed = vnav_des_table_act_spd2[x_path-1][ii]
							jj = vnav_des_table_type2[x_path-1][ii]
							if x_in_dist_corr == 0 then
								break
							end
						end
					end
				end
			end
			if init_speed == 0 then
				init_speed = fixed_speed
			end
		end
	end
	
	jj = 0
	
	if legs_data2_buf[x_idx][4] ~= 0 then
		
		if legs_data2_buf[x_idx][42] == 1 then
			-- below
			if init_speed > legs_data2_buf[x_idx][4] then
				init_speed = legs_data2_buf[x_idx][4]
			end
		elseif legs_data2_buf[x_idx][42] == 2 then
			-- above
			if init_speed < legs_data2_buf[x_idx][4] then
				init_speed = legs_data2_buf[x_idx][4]
			end
		else
			-- at
			init_speed = legs_data2_buf[x_idx][4]
		end
	end
	if init_speed > fixed_speed then
		init_speed = fixed_speed
	end
	if x_start_alt == des_altit_res and des_speed_res ~= 0 then
		if init_speed > des_speed_res then
			init_speed = des_speed_res
		end
		fl100_active = 1
	end
	
	-- find next restrict
	spd_rest_idx, spd_rest, spd_rest_dist, spd_rest_type = find_spd_rest2(x_idx, 0, 0, in_rte)
	
	if spd_rest_idx ~= 0 then
		
		if spd_rest_type == 1 then
			-- below
			if init_speed > spd_rest then
				init_speed = spd_rest
			end
		-- elseif spd_rest_type == 2 then
			-- -- above
		elseif spd_rest_type == 0 then
			-- at
			if init_speed > spd_rest then
				init_speed = spd_rest
			end
		end
		
	end
	
	if init_speed < spd_rest or init_speed < fixed_speed then
		init_speed_active = 1
	end
	
	x_spd = fixed_speed
	if spd_rest ~= 0 then
		x_spd = math.min(spd_rest, fixed_speed)
	end
	
	if init_speed_active == 1 and x_spd > init_speed then
		x_spd_delta = x_spd - init_speed
		-- decel_rest_dist = x_spd_delta * DES_SPEED_COEF
		-- x_spd_delta_lim = math.min(x_spd_delta, 40)
		-- if x_spd_delta_lim < 10 then
			-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
		-- else
			-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
		-- end
		decel_dist_full = x_spd_delta * DES_SPEED_COEF
		decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
		decel_active = 4
		decel_low_spd = init_speed
		decel_high_spd = x_spd
		x_spd_coef = decel_low_spd
		decel_coef = 0.55
	else
		init_speed = x_spd
	end
	
	x_last_spd = x_spd
	x_actual_spd = init_speed
	
	x_spd_mach_alt = find_mach_alt(B738DR_fmc_descent_speed, B738DR_fmc_descent_speed_mach)
	
	if x_start_alt < x_spd_mach_alt then
		x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
	else
		x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
	end
		
	flap_descent = calc_flap_desc(init_speed)
	
	if flap_descent == 1 then
			flap_descent_speed = B738DR_fms_vref_40 + 70
			if flap_descent_speed > x_actual_spd then
				flap_distance = (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.7	--0.9
			else
				flap_descent = 0
				flap_distance = 0
			end
	elseif flap_descent == 2 then
		-- flap_descent_speed = B738DR_fms_vref_40 + 50
		flap_descent_speed = B738DR_fms_vref_40 + 50
		if flap_descent_speed > x_actual_spd then
			flap_distance = flap_distance + (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.5	--0.9
		else
			flap_descent = 0
			flap_distance = 0
		end
	-- elseif flap_descent > 0 then
		-- -- flap_distance = flap_dist_descent[flap_descent]
		-- flap_descent_speed = B738DR_fms_vref_40 + 50
		-- flap_distance = (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.9
	end
	
		-- find nearest high altitude
		-- for jj = 1, 36 do
		for jj = 1, 37 do
			x_start_alt = roundUpToIncrement(x_start_alt, 10)
			if x_start_alt <= vnav_des_alt[jj] then
				idx_alt = jj
				if decel_active == 0 then
					alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
				else
					alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
				end
				high_alt = vnav_des_alt[idx_alt]
				if idx_alt > 1 then
					low_alt = vnav_des_alt[idx_alt-1]
				else
					low_alt = high_alt
				end
				pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
				
				alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
				
				if decel_active ~= 0 then
					
					calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
					if calc_fpm > 0 then
						calc_fpm2 = calc_fpm * 0.28
						calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
						calc_fpm4 = calc_fpm - (2 * calc_fpm3)
						calc_fpm3 = calc_fpm - calc_fpm3
						calc_fpm1 = calc_fpm2
						
						pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
						if (pom_pom1 * 2 + 0.1) > decel_dist_full then
							decel_dist[1] = decel_dist_full
							decel_dist[2] = decel_dist_full * 0.75
							decel_dist[3] = decel_dist_full * 0.5
							decel_dist[4] = decel_dist_full * 0.25
						else
							decel_dist[1] = decel_dist_full
							decel_dist[2] = decel_dist_full - pom_pom1
							pom_pom1 = decel_dist[2] - pom_pom1
							decel_dist[3] = pom_pom1
							
							calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
							if calc_fpm0 > calc_fpm2 + 100 then
								calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
								decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
								if pom_pom1 < decel_dist[4] + 0.1 then
									decel_dist[4] = pom_pom1 * 0.5
									calc_fpm1 = calc_fpm2
								end
							else
								decel_dist[4] = pom_pom1 * 0.5
							end
						end
					else
						calc_fpm1 = 0
						calc_fpm2 = 0
						calc_fpm3 = 0
						calc_fpm4 = 0
						decel_dist[1] = decel_dist_full
						decel_dist[2] = decel_dist_full * 0.75
						decel_dist[3] = decel_dist_full * 0.5
						decel_dist[4] = decel_dist_full * 0.25
					end
					decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
					decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
					decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
					decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
					
					decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
					pth_angle = decel_path[decel_active]
					
					-- if decel_active == 1 then
						-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
						-- decel_coef = 1
					-- elseif decel_active == 2 then
						-- x_spd_coef = decel_low_spd
						-- decel_coef = 1
					-- elseif decel_active == 3 then
						-- x_spd_coef = decel_low_spd
						-- decel_coef = 0.75
					-- else
						-- x_spd_coef = decel_low_spd	--init_speed
						-- decel_coef = 0.55
					-- end
					
					-- if idx_alt > 1 then
						-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
					-- else
						-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
					-- end
					-- -- if low_alt <= 10000 then
					-- if low_alt < 10000 then
						-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
					-- else
						-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
					-- end
					alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
				end
				
				-- create alt point
				vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
				vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = 0
				vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = x_start_alt
				if decel_active == 0 then
					vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + x_wpt_wind
				else
					vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 1000 + x_wpt_wind
				end
				vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
				vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
				vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = x_wpt_idx	--0
				
				--x_start_alt = vnav_des_table_alt[x_path][vnav_des_table_num[x_path]]
				
				-- if high_alt == vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] and idx_alt < 36 then
				if high_alt == vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] and idx_alt < 37 then
					idx_alt = idx_alt + 1
					if decel_active == 0 then
						alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
					else
						alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
					end
					high_alt = vnav_des_alt[idx_alt]
					if idx_alt > 1 then
						low_alt = vnav_des_alt[idx_alt-1]
					else
						low_alt = high_alt
					end
					pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
					
					alt_remain_dist = alt_dist
					if decel_active ~= 0 then
						calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
						if calc_fpm > 0 then
							calc_fpm2 = calc_fpm * 0.28
							calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
							calc_fpm4 = calc_fpm - (2 * calc_fpm3)
							calc_fpm3 = calc_fpm - calc_fpm3
							calc_fpm1 = calc_fpm2
							
							pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							if (pom_pom1 * 2 + 0.1) > decel_dist_full then
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							else
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full - pom_pom1
								pom_pom1 = decel_dist[2] - pom_pom1
								decel_dist[3] = pom_pom1
								
								calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
								if calc_fpm0 > calc_fpm2 + 100 then
									calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
									decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
									if pom_pom1 < decel_dist[4] + 0.1 then
										decel_dist[4] = pom_pom1 * 0.5
										calc_fpm1 = calc_fpm2
									end
								else
									decel_dist[4] = pom_pom1 * 0.5
								end
							end
						else
							calc_fpm1 = 0
							calc_fpm2 = 0
							calc_fpm3 = 0
							calc_fpm4 = 0
							decel_dist[1] = decel_dist_full
							decel_dist[2] = decel_dist_full * 0.75
							decel_dist[3] = decel_dist_full * 0.5
							decel_dist[4] = decel_dist_full * 0.25
						end
						decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
						decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
						decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
						decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
						
						decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
						pth_angle = decel_path[decel_active]
						
						-- if decel_active == 1 then
							-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
							-- decel_coef = 1
						-- elseif decel_active == 2 then
							-- x_spd_coef = decel_low_spd
							-- decel_coef = 1
						-- elseif decel_active == 3 then
							-- x_spd_coef = decel_low_spd
							-- decel_coef = 0.75
						-- else
							-- x_spd_coef = decel_low_spd	--init_speed
							-- decel_coef = 0.55
						-- end
						
						-- if idx_alt > 1 then
							-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
						-- else
							-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
						-- end
						-- -- if low_alt <= 10000 then
						-- if low_alt < 10000 then
							-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
						-- else
							-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
						-- end
						alt_remain_dist = calc_path_distance(high_alt, low_alt, pth_angle)
					end
				end
				x_last_spd = x_spd
				
				if x_start_alt < x_spd_mach_alt then
					x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
				else
					x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
				end
				if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
					x_spd_mach_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
					x_spd_mach_test = 1
				end
				break
			end
		end
		
		while vnav_des_table_num2[x_path] ~= 0 and end_path == 0 do
			
			if x_spd_mach == 0 then
			
				x_dist = alt_remain_dist + vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]]
				
				work_loop = 0
				
				while work_loop == 0 do
					
					if x_spd_mach_test ~= 0 then
						compare_dist = 0
						if x_wpt_dist ~= 0 and decel_rest_dist ~= 0 then
							if x_spd_mach_dist < x_wpt_dist and x_spd_mach_dist < decel_rest_dist then
								compare_dist = 1
							end
						elseif x_wpt_dist ~= 0 then
							if x_spd_mach_dist < x_wpt_dist then
								compare_dist = 1
							end
						elseif decel_rest_dist ~= 0 then
							if x_spd_mach_dist < decel_rest_dist then
								compare_dist = 1
							end
						end
					end
					
					if compare_dist ~= 0 and x_dist > x_spd_mach_dist and x_spd_mach_test ~= 0 then
						-- create new mach alt point
						alt_dist2 = alt_remain_dist - (x_dist - x_spd_mach_dist)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create alt point
						vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
						vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = x_spd_mach_dist
						vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = x_start_alt
						vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 5000 + x_wpt_wind
						vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
						vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
						vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = 9000
						
						x_spd = B738DR_fmc_descent_speed_mach
						
						-- recalc x_wpt_wind to mach
						x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						
						alt_dist = take_alt_dist_mach(idx_alt, x_spd + x_wpt_wind)
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						x_spd_mach = 1
						flap_descent = 0
						flap_distance = 0
						break
					end
					
					-- check flap speed
					flaps_check = 0
					if flap_descent > 0 then
						if decel_rest_dist == 0 and x_wpt_dist == 0 then
							flaps_check = 1
						elseif decel_rest_dist == 0 then
							-- only wpt distance
							if flap_distance < x_wpt_dist then
								flaps_check = 1
							end
						elseif x_wpt_dist == 0 then
							-- only decel distance
							if flap_distance < decel_rest_dist then
								flaps_check = 1
							end
						else
							if flap_distance < x_wpt_dist and flap_distance < decel_rest_dist then
								flaps_check = 1
							end
						end
					end
					
					if flaps_check ~= 0 and x_dist > flap_distance then
						-- create flap alt point
						alt_dist2 = alt_remain_dist - (x_dist - flap_distance)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create alt point
						vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
						vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = flap_distance
						vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = x_start_alt
						if decel_active == 0 then
							vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + x_wpt_wind + 10000
						else
							vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + x_wpt_wind + 11000
						end
						if flap_descent > 0 then
							flap_descent = flap_descent - 1
						end
						vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
						--B738DR_fms_test4 = 4
						if decel_active == 0 then
							x_actual_spd = x_last_spd
						else
							act_spd_dist = 0
							if vnav_des_table_num2[x_path] > 1 then
								act_spd_dist = flap_distance - vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]-1]
							end
							x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
						end
						vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
						vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = 5000 + flap_descent
						--B738DR_fms_test4 = 4
						
						if flap_descent == 1 then
							-- if x_spd > x_actual_spd then
								-- flap_distance = flap_distance + ((x_spd - x_actual_spd) * DES_SPEED_COEF)
								flap_descent_speed = B738DR_fms_vref_40 + 70
								if flap_descent_speed > x_actual_spd then
									flap_distance = flap_distance + (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.7	--0.9
								else
									flap_descent = 0
									flap_distance = 0
								end
							-- end
						elseif flap_descent == 2 then
							flap_descent_speed = B738DR_fms_vref_40 + 50
							if flap_descent_speed > x_actual_spd then
								flap_distance = flap_distance + (flap_descent_speed - x_actual_spd) * DES_SPEED_COEF * 0.5	--0.9
							else
								flap_descent = 0
								flap_distance = 0
							end
						-- elseif flap_descent > 0 then
							-- flap_distance = flap_distance + flap_dist_descent[flap_descent]
						end
						
						-- recalc new descent speed/flap
						if x_start_alt < x_spd_mach_alt then
							x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
						else
							x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						end
						
						if decel_active == 0 then
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						
						else
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
							
							calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
							if calc_fpm > 0 then
								calc_fpm2 = calc_fpm * 0.28
								calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
								calc_fpm4 = calc_fpm - (2 * calc_fpm3)
								calc_fpm3 = calc_fpm - calc_fpm3
								calc_fpm1 = calc_fpm2
								
								pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								if (pom_pom1 * 2 + 0.1) > decel_dist_full then
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								else
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full - pom_pom1
									pom_pom1 = decel_dist[2] - pom_pom1
									decel_dist[3] = pom_pom1
									
									calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
									if calc_fpm0 > calc_fpm2 + 100 then
										calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
										decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
										if pom_pom1 < decel_dist[4] + 0.1 then
											decel_dist[4] = pom_pom1 * 0.5
											calc_fpm1 = calc_fpm2
										end
									else
										decel_dist[4] = pom_pom1 * 0.5
									end
								end
							else
								calc_fpm1 = 0
								calc_fpm2 = 0
								calc_fpm3 = 0
								calc_fpm4 = 0
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							end
							decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
							decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
							decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
							pth_angle = decel_path[decel_active]
							
							-- if decel_active == 1 then
								-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								-- decel_coef = 1
							-- elseif decel_active == 2 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 1
							-- elseif decel_active == 3 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 0.75
							-- else
								-- x_spd_coef = decel_low_spd	--init_speed
								-- decel_coef = 0.55
							-- end
							
							-- if idx_alt > 1 then
								-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
							-- else
								-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
							-- end
							-- -- if low_alt <= 10000 then
							-- if low_alt < 10000 then
								-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
							-- else
								-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
							-- end
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						end
						
						-- change x dist
						x_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + alt_remain_dist
						
						-- calc mach alt distance
						if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
							x_spd_mach_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
							x_spd_mach_test = 1
						end
					elseif (x_wpt_dist < decel_rest_dist and x_wpt_dist ~= 0) or (decel_rest_dist == 0) then
						if x_dist > x_wpt_dist and x_wpt_dist ~= 0 then
							-- create new wpt alt point
							alt_dist2 = alt_remain_dist - (x_dist - x_wpt_dist)
							x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
							-- create alt point
							vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
							vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = x_wpt_dist
							vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = x_start_alt
							if spd_rest_idx >= x_wpt_idx - 1 then
								if decel_active == 0 then
									vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 7000
								else
									vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 6000
								end
							else
								if decel_active == 0 then
									vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 9000
								else
									vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 8000
								end
							end
							
							vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
							if decel_active == 0 then
								x_actual_spd = x_last_spd
							else
								act_spd_dist = 0
								if vnav_des_table_num2[x_path] > 1 then
									act_spd_dist = x_wpt_dist - vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]-1]
								end
								x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
							end
							
							-- wpt is spd rest idx ???
							if spd_rest_idx >= x_wpt_idx - 1 then
								if spd_rest_type == 1 then	-- below
									if x_actual_spd > x_spd then
										x_actual_spd = x_spd
									end
								else
									x_actual_spd = x_spd
								end
								spd_rest_dist_temp = 0
								if spd_rest_idx < x_idx then
									for xx = x_idx, spd_rest_idx + 1, -1 do
										spd_rest_dist_temp = spd_rest_dist_temp + wpt_distance2(xx, in_rte)	--legs_data2_buf[xx][3]
									end
								end
								-- find new spd rest
								spd_rest_idx, spd_rest, spd_rest_dist, spd_rest_type = find_spd_rest2(spd_rest_idx, spd_rest_dist, 0, in_rte)
							end
							
							
							vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
							vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = x_wpt_idx - 1
							
							-- save calculated speed
							-- legs_data2_buf[x_wpt_idx-1][10] = x_actual_spd
							
							
							if x_start_alt < des_altit_res and des_speed_res ~= 0 then
								fixed_speed = math.min(B738DR_fmc_descent_speed, des_speed_res)
								-- if fixed_speed == B738DR_fmc_descent_r_speed1 then
								if fixed_speed == des_speed_res then
									fl100_active = 1
								end
							else
								fixed_speed = B738DR_fmc_descent_speed
							end
							x_spd = fixed_speed
							if spd_rest_idx ~= 0 then
								x_spd = math.min(x_spd, spd_rest)
							end
							
							if x_spd > x_last_spd then
								x_spd_delta = x_spd - x_actual_spd
								
								if spd_rest_idx ~= 0 and spd_rest <= x_spd then
									-- x_spd_delta_lim = math.min(x_spd_delta, 40)
									-- if x_spd_delta_lim < 10 then
										-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
									-- else
										-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
									-- end
									decel_dist_full = x_spd_delta * DES_SPEED_COEF
									decel_dist_offset = spd_rest_dist_temp
									decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								else
									-- x_spd_delta_lim = math.min(x_spd_delta, 40)
									-- if x_spd_delta_lim < 10 then
										-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
									-- else
										-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
									-- end
									decel_dist_full = x_spd_delta * DES_SPEED_COEF
									decel_dist_offset = x_wpt_dist
									decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								end
								
								
								
								
								
							end
							x_last_spd = x_spd
							
							-- find next wpt
							local wpt_finded = 0
							local temp_dist = 0
							if x_start_alt < x_spd_mach_alt then
								x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
							else
								x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
							end
							x_wpt_idx = x_wpt_idx - 1
							if x_wpt_idx > 1 then
								x_wpt_dist = x_wpt_dist + wpt_distance2(x_wpt_idx, in_rte)	--legs_data2_buf[x_wpt_idx][3]
								if wpt_distance2(x_wpt_idx, in_rte) ~= 0 then
									if x_start_alt < x_spd_mach_alt then
										x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
									else
										x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
									end
									wpt_finded = 1
								end
								while wpt_distance2(x_wpt_idx, in_rte) == 0 do
									x_wpt_idx = x_wpt_idx - 1
									if x_wpt_idx > 1 then
										if wpt_distance2(x_wpt_idx, in_rte) ~= 0 then
											x_wpt_dist = x_wpt_dist + wpt_distance2(x_wpt_idx, in_rte)	--legs_data2_buf[x_wpt_idx][3]
											wpt_finded = 1
											if x_start_alt < x_spd_mach_alt then
												x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
											else
												x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
											end
										end
									else
										break
									end
								end
							end
							if wpt_finded == 0 then
								x_wpt_idx = 0
								x_wpt_dist = 0
								x_wpt_wind = 0
							end
							
							if decel_active == 0 then
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
							else
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
							end
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
							
							if decel_active ~= 0 then
								calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
								if calc_fpm > 0 then
									calc_fpm2 = calc_fpm * 0.28
									calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
									calc_fpm4 = calc_fpm - (2 * calc_fpm3)
									calc_fpm3 = calc_fpm - calc_fpm3
									calc_fpm1 = calc_fpm2
									
									pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
									if (pom_pom1 * 2 + 0.1) > decel_dist_full then
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full * 0.75
										decel_dist[3] = decel_dist_full * 0.5
										decel_dist[4] = decel_dist_full * 0.25
									else
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full - pom_pom1
										pom_pom1 = decel_dist[2] - pom_pom1
										decel_dist[3] = pom_pom1
										
										calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
										if calc_fpm0 > calc_fpm2 + 100 then
											calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
											decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
											if pom_pom1 < decel_dist[4] + 0.1 then
												decel_dist[4] = pom_pom1 * 0.5
												calc_fpm1 = calc_fpm2
											end
										else
											decel_dist[4] = pom_pom1 * 0.5
										end
									end
								else
									calc_fpm1 = 0
									calc_fpm2 = 0
									calc_fpm3 = 0
									calc_fpm4 = 0
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								end
								decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
								decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
								decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
								
								decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
								pth_angle = decel_path[decel_active]
								
								-- if decel_active == 1 then
									-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
									-- decel_coef = 1
								-- elseif decel_active == 2 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 1
								-- elseif decel_active == 3 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 0.75
								-- else
									-- x_spd_coef = decel_low_spd	--init_speed
									-- decel_coef = 0.55
								-- end
								
								-- if idx_alt > 1 then
									-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
								-- else
									-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
								-- end
								-- -- if low_alt <= 10000 then
								-- if low_alt < 10000 then
									-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
								-- else
									-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
								-- end
								alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
							end
							
							-- change x dist
							x_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + alt_remain_dist
							
							-- calc mach alt distance
							if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
								x_spd_mach_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
								x_spd_mach_test = 1
							end
						else
							work_loop = 1
							-- NEXT ALT
							vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
							vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = x_dist
							vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = high_alt
							if decel_active == 0 then
								vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd
							else
								vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 1000
							end
							
							vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
							if decel_active == 0 then
								x_actual_spd = x_last_spd
							else
								act_spd_dist = 0
								if vnav_des_table_num2[x_path] > 1 then
									act_spd_dist = x_dist - vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]-1]
								end
								x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
							end
							vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
							vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = 0
							
							x_start_alt = high_alt
							idx_alt = idx_alt + 1
							-- if idx_alt > 36 then
							if idx_alt > 37 then
								end_path = 1
								break
							end
							low_alt = high_alt
							high_alt = vnav_des_alt[idx_alt]
							
							-- check speed
							if x_start_alt < des_altit_res and des_speed_res ~= 0 then
								fixed_speed = math.min(B738DR_fmc_descent_speed, des_speed_res)
								-- if fixed_speed == B738DR_fmc_descent_r_speed1 then
								if fixed_speed == des_speed_res then
									fl100_active = 1
								end
							else
								fixed_speed = B738DR_fmc_descent_speed
							end
							x_spd = fixed_speed
							if spd_rest_idx ~= 0 then
								x_spd = math.min(x_spd, spd_rest)
							end
							
							if x_spd > x_last_spd then
								x_spd_delta = x_spd - x_last_spd
								
								-- x_spd_delta_lim = math.min(x_spd_delta, 40)
								-- if x_spd_delta_lim < 10 then
									-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
								-- else
									-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
								-- end
								decel_dist_full = x_spd_delta * DES_SPEED_COEF
								decel_dist_offset = x_dist
								decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								decel_active = 4	--1
								decel_low_spd = x_last_spd
								decel_high_spd = x_spd
								x_spd_coef = decel_low_spd	--init_speed
								decel_coef = 0.55
								
								
								
								
								
							end
							x_last_spd = x_spd
							
							if x_start_alt < x_spd_mach_alt then
								x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
							else
								x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
							end
							
							if decel_active == 0 then
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
							else
								alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
							end
							pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
							
							alt_remain_dist = alt_dist
							
							
							if decel_active ~= 0 then
								calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
								if calc_fpm > 0 then
									calc_fpm2 = calc_fpm * 0.28
									calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
									calc_fpm4 = calc_fpm - (2 * calc_fpm3)
									calc_fpm3 = calc_fpm - calc_fpm3
									calc_fpm1 = calc_fpm2
									
									pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
									if (pom_pom1 * 2 + 0.1) > decel_dist_full then
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full * 0.75
										decel_dist[3] = decel_dist_full * 0.5
										decel_dist[4] = decel_dist_full * 0.25
									else
										decel_dist[1] = decel_dist_full
										decel_dist[2] = decel_dist_full - pom_pom1
										pom_pom1 = decel_dist[2] - pom_pom1
										decel_dist[3] = pom_pom1
										
										calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
										if calc_fpm0 > calc_fpm2 + 100 then
											calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
											decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
											if pom_pom1 < decel_dist[4] + 0.1 then
												decel_dist[4] = pom_pom1 * 0.5
												calc_fpm1 = calc_fpm2
											end
										else
											decel_dist[4] = pom_pom1 * 0.5
										end
									end
								else
									calc_fpm1 = 0
									calc_fpm2 = 0
									calc_fpm3 = 0
									calc_fpm4 = 0
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								end
								decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
								decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
								decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
								
								decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
								pth_angle = decel_path[decel_active]
								
								-- if decel_active == 1 then
									-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
									-- decel_coef = 1
								-- elseif decel_active == 2 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 1
								-- elseif decel_active == 3 then
									-- x_spd_coef = decel_low_spd
									-- decel_coef = 0.75
								-- else
									-- x_spd_coef = decel_low_spd	--init_speed
									-- decel_coef = 0.55
								-- end
								
								-- if idx_alt > 1 then
									-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
								-- else
									-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
								-- end
								-- -- if low_alt <= 10000 then
								-- if low_alt < 10000 then
									-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
								-- else
									-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
								-- end
								alt_remain_dist = calc_path_distance(high_alt, low_alt, pth_angle)
							end
							-- calc mach alt distance
							if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
								x_spd_mach_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
								x_spd_mach_test = 1
							end
						end
						
					elseif x_dist > decel_rest_dist and decel_rest_dist ~= 0 then
						-- DECEL point
						alt_dist2 = alt_remain_dist - (x_dist - decel_rest_dist)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create DECEL point
						vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
						vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = decel_rest_dist
						vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = x_start_alt
						vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 3000
						vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
						
						-- if decel_active == 1 then
							-- x_actual_spd = decel_high_spd
						-- elseif decel_active == 2 then
							-- x_actual_spd = decel_low_spd + ((decel_high_spd - decel_low_spd) * 0.25 * 3)
						-- elseif decel_active == 3 then
							-- x_actual_spd = decel_low_spd + ((decel_high_spd - decel_low_spd) * 0.25 * 2)
						-- else
							-- x_actual_spd = decel_low_spd + ((decel_high_spd - decel_low_spd) * 0.25)
						-- end
						
						if decel_active == 1 then
							x_actual_spd = decel_high_spd
						elseif decel_active == 2 then
							x_actual_spd = decel_low_spd + (decel_dist[2] / DES_SPEED_COEF)
						elseif decel_active == 3 then
							x_actual_spd = decel_low_spd + (decel_dist[3] / DES_SPEED_COEF)
						else
							x_actual_spd = decel_low_spd + (decel_dist[4] / DES_SPEED_COEF)
						end
						
						-- x_actual_spd = x_last_spd
						vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
						vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = 3000 + decel_active
						
						-- if x_spd == B738DR_fmc_descent_speed and x_edalt <= des_altit_res then
							-- fl100_desc_dist = decel_rest_dist
						-- end
						
						if fl100_active == 1 and decel_active == 1 then
							-- fl100_active = 2
							-- fl100_desc_dist = decel_rest_dist
							if des_speed_res ~= 0 then
								if x_start_alt >= des_altit_res and x_spd > des_speed_res then
									fl100_active = 2
									fl100_desc_dist = decel_rest_dist
								end
							end
						end
						
						x_spd_coef = 1.0
						if decel_active > 0 then
							decel_active = decel_active - 1
						end
						if decel_active == 0 then
							decel_rest_dist = 0
						else
							if decel_active == 1 then
								x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								decel_coef = 1
								-- decel_rest_dist = decel_dist_offset + decel_dist_full
							elseif decel_active == 2 then
								x_spd_coef = decel_low_spd
								decel_coef = 1
								-- decel_rest_dist = decel_dist_offset + decel_dist_full * 0.75
							elseif decel_active == 3 then
								x_spd_coef = decel_low_spd
								decel_coef = 0.75
								-- decel_rest_dist = decel_dist_offset + decel_dist_full * 0.5
							end
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
						end
						
						if decel_active == 0 then
							if x_start_alt <= des_altit_res and des_speed_res ~= 0 then
								fixed_speed = math.min(B738DR_fmc_descent_speed, des_speed_res)
							else
								fixed_speed = B738DR_fmc_descent_speed
							end
							x_spd = fixed_speed
							if spd_rest_idx ~= 0 then
								x_spd = math.min(x_spd, spd_rest)
							end
							
							if x_spd > x_last_spd then
								x_spd_delta = x_spd - x_last_spd
								-- x_spd_delta_lim = math.min(x_spd_delta, 40)
								-- if x_spd_delta_lim < 10 then
									-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
								-- else
									-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
								-- end
								decel_dist_full = x_spd_delta * DES_SPEED_COEF
								decel_dist_offset = vnav_des_table_dist[x_path][vnav_des_table_num[x_path]]
								decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
								decel_active = 4	--1
								decel_low_spd = x_last_spd
								decel_high_spd = x_spd
								x_spd_coef = decel_low_spd	--init_speed
								decel_coef = 0.55
							end
							x_last_spd = x_spd
							
						end
						
						if x_start_alt < x_spd_mach_alt then
							x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
						else
							x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						end
						
						if decel_active == 0 then
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
						else
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
						end
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						
						if decel_active ~= 0 then
							calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
							if calc_fpm > 0 then
								calc_fpm2 = calc_fpm * 0.28
								calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
								calc_fpm4 = calc_fpm - (2 * calc_fpm3)
								calc_fpm3 = calc_fpm - calc_fpm3
								calc_fpm1 = calc_fpm2
								
								pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								if (pom_pom1 * 2 + 0.1) > decel_dist_full then
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								else
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full - pom_pom1
									pom_pom1 = decel_dist[2] - pom_pom1
									decel_dist[3] = pom_pom1
									
									calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
									if calc_fpm0 > calc_fpm2 + 100 then
										calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
										decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
										if pom_pom1 < decel_dist[4] + 0.1 then
											decel_dist[4] = pom_pom1 * 0.5
											calc_fpm1 = calc_fpm2
										end
									else
										decel_dist[4] = pom_pom1 * 0.5
									end
								end
							else
								calc_fpm1 = 0
								calc_fpm2 = 0
								calc_fpm3 = 0
								calc_fpm4 = 0
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							end
							decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
							decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
							decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
							pth_angle = decel_path[decel_active]
							
							-- if decel_active == 1 then
								-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								-- decel_coef = 1
							-- elseif decel_active == 2 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 1
							-- elseif decel_active == 3 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 0.75
							-- else
								-- x_spd_coef = decel_low_spd	--init_speed
								-- decel_coef = 0.55
							-- end
							
							-- if idx_alt > 1 then
								-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
							-- else
								-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
							-- end
							-- -- if low_alt <= 10000 then
							-- if low_alt < 10000 then
								-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
							-- else
								-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
							-- end
							alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						end
						
						-- change x dist
						x_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + alt_remain_dist
						
						-- calc mach alt distance
						if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
							x_spd_mach_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
							x_spd_mach_test = 1
						end
					else
						work_loop = 1
						-- NEXT ALT
						vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
						vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = x_dist
						vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = high_alt
						if decel_active == 0 then
							vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd
						else
							vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 2000
						end
						vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
						if decel_active == 0 then
							x_actual_spd = x_last_spd
						else
							act_spd_dist = 0
							if vnav_des_table_num2[x_path] > 1 then
								act_spd_dist = x_dist - vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]-1]
							end
							x_actual_spd = x_actual_spd + (act_spd_dist / DES_SPEED_COEF)
						end
						vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
						vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = 0
						
						x_start_alt = high_alt
						idx_alt = idx_alt + 1
						-- if idx_alt > 36 then
						if idx_alt > 37 then
							end_path = 1
							break
						end
						low_alt = high_alt
						high_alt = vnav_des_alt[idx_alt]
						
						-- check speed
						if x_start_alt < des_altit_res and des_speed_res ~= 0 then
							fixed_speed = math.min(B738DR_fmc_descent_speed, des_speed_res)
							-- if fixed_speed == B738DR_fmc_descent_r_speed1 then
							if fixed_speed == des_speed_res then
								fl100_active = 1
							end
						else
							fixed_speed = B738DR_fmc_descent_speed
						end
						x_spd = fixed_speed
						if spd_rest_idx ~= 0 then
							x_spd = math.min(x_spd, spd_rest)
						end
						
						if x_spd > x_last_spd then
							x_spd_delta = x_spd - x_last_spd
							-- x_spd_delta_lim = math.min(x_spd_delta, 40)
							-- if x_spd_delta_lim < 10 then
								-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(0, 0, 10, 0.4, x_spd_delta_lim)
							-- else
								-- decel_dist_full = (x_spd_delta * DES_SPEED_COEF) + B738_rescale(10, 0.4, 40, 0, x_spd_delta_lim)
							-- end
							decel_dist_full = x_spd_delta * DES_SPEED_COEF
							decel_dist_offset = x_dist
							decel_rest_dist = decel_dist_offset + decel_dist_full * 0.25
							decel_active = 4	--1
							decel_low_spd = x_last_spd
							decel_high_spd = x_spd
							x_spd_coef = decel_low_spd	--init_speed
							decel_coef = 0.55
						end
						x_last_spd = x_spd
						
						if x_start_alt < x_spd_mach_alt then
							x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 0)	-- wpt wind
						else
							x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						end
						
						if decel_active == 0 then
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, flap_descent)
						else
							alt_dist = take_alt_dist(idx_alt, x_spd + x_wpt_wind, 0)
						end
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						
						alt_remain_dist = alt_dist
						
						
						if decel_active ~= 0 then
							calc_fpm = calc_path_fpm(pth_angle, x_spd, x_start_alt, x_wpt_wind)
							if calc_fpm > 0 then
								calc_fpm2 = calc_fpm * 0.28
								calc_fpm3 = (calc_fpm - calc_fpm2) * 0.3333
								calc_fpm4 = calc_fpm - (2 * calc_fpm3)
								calc_fpm3 = calc_fpm - calc_fpm3
								calc_fpm1 = calc_fpm2
								
								pom_pom1 = calc_decel_dist(calc_fpm, calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
								if (pom_pom1 * 2 + 0.1) > decel_dist_full then
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full * 0.75
									decel_dist[3] = decel_dist_full * 0.5
									decel_dist[4] = decel_dist_full * 0.25
								else
									decel_dist[1] = decel_dist_full
									decel_dist[2] = decel_dist_full - pom_pom1
									pom_pom1 = decel_dist[2] - pom_pom1
									decel_dist[3] = pom_pom1
									
									calc_fpm0 = calc_path_fpm(pth_angle, decel_low_spd, x_start_alt, x_wpt_wind)
									if calc_fpm0 > calc_fpm2 + 100 then
										calc_fpm1 = (calc_fpm0 + calc_fpm2) * 0.5
										decel_dist[4] = calc_decel_dist(calc_fpm0, calc_fpm1, decel_low_spd, x_start_alt, x_wpt_wind)
										if pom_pom1 < decel_dist[4] + 0.1 then
											decel_dist[4] = pom_pom1 * 0.5
											calc_fpm1 = calc_fpm2
										end
									else
										decel_dist[4] = pom_pom1 * 0.5
									end
								end
							else
								calc_fpm1 = 0
								calc_fpm2 = 0
								calc_fpm3 = 0
								calc_fpm4 = 0
								decel_dist[1] = decel_dist_full
								decel_dist[2] = decel_dist_full * 0.75
								decel_dist[3] = decel_dist_full * 0.5
								decel_dist[4] = decel_dist_full * 0.25
							end
							decel_path[1] = calc_path_angle_fpm(calc_fpm3, x_spd, x_start_alt, x_wpt_wind)
							decel_path[2] = calc_path_angle_fpm(calc_fpm4, x_spd, x_start_alt, x_wpt_wind)
							decel_path[3] = calc_path_angle_fpm(calc_fpm2, x_spd, x_start_alt, x_wpt_wind)
							decel_path[4] = calc_path_angle_fpm(calc_fpm1, x_spd, x_start_alt, x_wpt_wind)
							
							decel_rest_dist = decel_dist_offset + decel_dist[decel_active]
							pth_angle = decel_path[decel_active]
							
							-- if decel_active == 1 then
								-- x_spd_coef = (decel_high_spd + decel_low_spd) * 0.5
								-- decel_coef = 1
							-- elseif decel_active == 2 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 1
							-- elseif decel_active == 3 then
								-- x_spd_coef = decel_low_spd
								-- decel_coef = 0.75
							-- else
								-- x_spd_coef = decel_low_spd	--init_speed
								-- decel_coef = 0.55
							-- end
							
							-- if idx_alt > 1 then
								-- alt_dist2 = take_alt_dist(idx_alt - 1, x_spd_coef + x_wpt_wind, flap_descent)
							-- else
								-- alt_dist2 = take_alt_dist(idx_alt, x_spd_coef + x_wpt_wind, flap_descent)
							-- end
							-- -- if low_alt <= 10000 then
							-- if low_alt < 10000 then
								-- pth_angle = calc_path_angle(4000, 2000, alt_dist2) * decel_coef
							-- else
								-- pth_angle = calc_path_angle(3000, 2000, alt_dist2) * decel_coef
							-- end
							alt_remain_dist = calc_path_distance(high_alt, low_alt, pth_angle)
						end
						
						-- calc mach alt distance
						if high_alt >= x_spd_mach_alt and x_spd_mach_test == 0 then
							x_spd_mach_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + calc_path_distance(x_spd_mach_alt, x_start_alt, pth_angle)
							x_spd_mach_test = 1
						end
					end
				end
			else
				x_dist = alt_remain_dist + vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]]
				
				-- mach speed
				work_loop = 0
				
				while work_loop == 0 do
					
					if x_dist > x_wpt_dist and x_wpt_dist ~= 0 then
						-- create new wpt alt point
						alt_dist2 = alt_remain_dist - (x_dist - x_wpt_dist)
						x_start_alt = x_start_alt + calc_path_altitude(alt_dist2, pth_angle)
						-- create alt point
						vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
						vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = x_wpt_dist
						vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = x_start_alt
						vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd + 9000
						vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
						x_actual_spd = x_last_spd
						vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
						vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = x_wpt_idx - 1
						
						-- find next wpt
						local wpt_finded = 0
						x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						x_wpt_idx = x_wpt_idx - 1
						if x_wpt_idx > 1 then
							x_wpt_dist = x_wpt_dist + wpt_distance2(x_wpt_idx, in_rte)	--legs_data2_buf[x_wpt_idx][3]
							if legs_data2_buf[x_wpt_idx][3] ~= 0 then
								x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
								wpt_finded = 1
							end
							while legs_data2_buf[x_wpt_idx][3] == 0 do
								x_wpt_idx = x_wpt_idx - 1
								if x_wpt_idx > 1 then
									if legs_data2_buf[x_wpt_idx][3] ~= 0 then
										x_wpt_dist = x_wpt_dist + wpt_distance2(x_wpt_idx, in_rte)	--legs_data2_buf[x_wpt_idx][3]
										wpt_finded = 1
										x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
									end
								else
									break
								end
							end
						end
						if wpt_finded == 0 then
							x_wpt_idx = 0
							x_wpt_dist = 0
							x_wpt_wind = 0
						end
						
						alt_dist = take_alt_dist_mach(idx_alt, x_spd + x_wpt_wind)
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						alt_remain_dist = calc_path_distance(high_alt, x_start_alt, pth_angle)
						
						-- change x dist
						x_dist = vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] + alt_remain_dist
					else
						-- NEXT ALT
						
						work_loop = 1
						
						vnav_des_table_num2[x_path] = vnav_des_table_num2[x_path] + 1
						vnav_des_table_dist2[x_path][vnav_des_table_num2[x_path]] = x_dist
						vnav_des_table_alt2[x_path][vnav_des_table_num2[x_path]] = high_alt
						vnav_des_table_spd2[x_path][vnav_des_table_num2[x_path]] = x_spd
						vnav_des_table_flap2[x_path][vnav_des_table_num2[x_path]] = flap_descent
						x_actual_spd = x_last_spd
						vnav_des_table_act_spd2[x_path][vnav_des_table_num2[x_path]] = x_actual_spd
						vnav_des_table_type2[x_path][vnav_des_table_num2[x_path]] = 0
						
						x_start_alt = high_alt
						idx_alt = idx_alt + 1
						-- if idx_alt > 36 then
						if idx_alt > 37 then
							end_path = 1
							break
						end
						low_alt = high_alt
						high_alt = vnav_des_alt[idx_alt]
						
						x_wpt_wind = wpt_wind_spd2(x_wpt_idx, x_start_alt, 1)	-- wpt wind
						
						alt_dist = take_alt_dist_mach(idx_alt, x_spd + x_wpt_wind)
						pth_angle = calc_path_angle(high_alt, low_alt, alt_dist)
						
						alt_remain_dist = alt_dist
					end
				end
			end
			
		end
		
		-- calc descent speed restrict FL100
		local fl100_idx = 0
		local fl100_dist = 0
		if x_edalt <= des_altit_res and des_altit_res ~= 0 and x_crzalt > des_altit_res then
			if fl100_desc_dist ~= 0 and des_altit_res ~= 0 and des_speed_res ~= 0 then
					local fl100_temp = 0
					if ed_found_mod ~= x_idx then
						if ed_found_mod > x_idx then
							for xx = ed_found_mod, x_idx + 1, -1 do
								fl100_temp = fl100_temp + wpt_distance2(xx, in_rte)	--legs_data[xx][3]
							end
						end
					end
					fl100_desc_dist = fl100_desc_dist + fl100_temp
					if x_in_dist_corr ~= 0 then
						fl100_desc_dist = fl100_desc_dist + x_in_dist_corr
					end
					fl100_temp = 0
					for xx = ed_found_mod, 2, -1 do
						fl100_temp = fl100_temp + wpt_distance2(xx, in_rte)
						if fl100_desc_dist < fl100_temp then
							fl100_idx = xx
							fl100_dist = wpt_distance2(xx, in_rte) - (fl100_temp - fl100_desc_dist)
							break
						end
					end
					if fl100_idx > legs_num2_buf + 1 or fl100_dist < 0 then
						fl100_idx = 0
						fl100_dist = 0
					end
			else
				fl100_desc_dist = 0
			end
			B738DR_FL100_desc_dist_mod = fl100_desc_dist
			B738DR_FL100_idx_mod = fl100_idx
			B738DR_FL100_dist_mod = fl100_dist
		end
end

-- find spd restrict
function find_spd_rest2(x_idx2, xspd_rest_dist, xspd_dist_corr, in_rte)
	
	local ii = 0
	local jj = 0
	local res_idx = 0
	local res_spd = 0
	local res_dist = xspd_rest_dist
	local res_type = 0
	local res_spd_app = 0
	
	if x_idx2 > 2 and x_idx2 <= legs_num2_buf then
		for ii = x_idx2-1, 2, -1 do
			if xspd_dist_corr == 0 or ii ~= x_idx2-1 then
				res_dist = res_dist + wpt_distance2(ii+1, in_rte)
			else
				res_dist = res_dist + xspd_dist_corr
			end
			if legs_data2_buf[ii][4] ~= 0 then
				--if legs_data2_buf[ii][19] ~= 0 and legs_data2_buf[ii][19] ~= 1 and legs_data2_buf[ii][19] ~= 3 then
				if (legs_data2_buf[ii][19] ~= 1 and legs_data2_buf[ii][19] ~= 3) 
				or (legs_data2_buf[ii][19] == 0 and ii >= tc_idx_mod and tc_idx_mod ~= 0) then
					res_idx = ii
					res_spd = legs_data2_buf[ii][4]
					res_type = legs_data2_buf[ii][42]
					for jj = ii, 2, -1 do
						--if legs_data2_buf[jj][19] ~= 0 and legs_data2_buf[jj][19] ~= 1 and legs_data2_buf[jj][19] ~= 3 then
						if (legs_data2_buf[jj][19] ~= 1 and legs_data2_buf[jj][19] ~= 3) 
						or (legs_data2_buf[jj][19] == 0 and jj >= tc_idx_mod and tc_idx_mod ~= 0) then
							if legs_data2_buf[jj][4] < res_spd and legs_data2_buf[jj][4] ~= 0 and legs_data2_buf[jj][42] ~= 2 then
								res_idx = jj
								res_spd = legs_data2_buf[jj][4]
								res_type = legs_data2_buf[ii][42]
							end
						end
					end
					break
				end
			end
		end
		
		local before_iaf_idx = 0
		if iaf_app_idx_mod == 0 then
			before_iaf_idx = first_app_idx_mod_buf
		elseif iaf_app_idx_mod - 1 >= first_app_idx_mod_buf then
			before_iaf_idx = iaf_app_idx_mod - 1
		else
			before_iaf_idx = iaf_app_idx_mod
		end
		
		if before_iaf_idx < x_idx2 and before_iaf_idx ~= 0 then
			if B738DR_fms_vref_40 == 0 then
				res_spd_app = 200
			else
				res_spd_app = B738DR_fms_vref_40 + 70
			end
			if res_idx == 0 then
				if before_iaf_idx < x_idx2 and before_iaf_idx ~= 0 then
					res_idx = before_iaf_idx
					res_spd = res_spd_app
					res_type = 1	--0
				end
			else
				if res_idx == before_iaf_idx then
					if res_type == 1 and res_spd_app <= res_spd then
						-- below
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					elseif res_type == 2 and res_spd_app >= res_spd then
						-- above
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					end
				elseif res_idx < before_iaf_idx then
					if res_type == 1 and res_spd_app <= res_spd then
						-- below
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					else
						-- at and above
						res_idx = before_iaf_idx
						res_spd = res_spd_app
						res_type = 1	--0
					end
				end
			end
		end
	end
	
	if res_idx == 0 then
		res_dist = 0
	end
	return res_idx, res_spd, res_dist, res_type
	
end


-- calculate dist by alt
function calc_vnav_pth_dist2(x_alt01, x_alt02, x_path)
	
	local ii = 0
	local x_alt1 = 0
	local x_alt2 = 0
	local x_dist1 = 0
	local x_dist2 = 0
	local result_tmp = 0
	local result = 0
	
	if vnav_des_table_num2[x_path] > 0 then
		-- min alt
		for ii = 1, vnav_des_table_num2[x_path] do
			if x_alt01 <= vnav_des_table_alt2[x_path][ii] then
				x_dist2 = vnav_des_table_dist2[x_path][ii]
				x_alt2 = vnav_des_table_alt2[x_path][ii]
				if ii > 1 then
					x_dist1 = vnav_des_table_dist2[x_path][ii-1]
					x_alt1 = vnav_des_table_alt2[x_path][ii-1]
				end
				break
			end
		end

		result_tmp = B738_rescale(x_alt1, x_dist1, x_alt2, x_dist2, x_alt01)

		-- max alt
		x_alt1 = 0
		x_alt2 = 0
		x_dist1 = 0
		x_dist2 = 0
		for ii = 1, vnav_des_table_num2[x_path] do
			if x_alt02 <= vnav_des_table_alt2[x_path][ii] then
				x_dist2 = vnav_des_table_dist2[x_path][ii]
				x_alt2 = vnav_des_table_alt2[x_path][ii]
				if ii > 1 then
					x_dist1 = vnav_des_table_dist2[x_path][ii-1]
					x_alt1 = vnav_des_table_alt2[x_path][ii-1]
				end
				break
			end
		end

		result = B738_rescale(x_alt1, x_dist1, x_alt2, x_dist2, x_alt02)

		result = result - result_tmp
		if result < 0 then
			result = 0
		end
	else
		result = 0
	end
	
	return result
	
end

-- calculate alt by distance
function calc_vnav_pth_alt2(x_alt01, x_dist, x_path)
	
	local ii = 0
	local result = 42000	--41000	--x_alt01
	
	if vnav_des_table_num2[x_path] > 0 and x_dist > 0 then
		for ii = 1, vnav_des_table_num2[x_path] do
			if x_dist < vnav_des_table_dist2[x_path][ii] then
				if ii == 1 then
					result = 2000
				else
					result = B738_rescale(vnav_des_table_dist2[x_path][ii-1], vnav_des_table_alt2[x_path][ii-1], vnav_des_table_dist2[x_path][ii], vnav_des_table_alt2[x_path][ii], x_dist)
				end
				break
			end
		end
	end
	
	return result
	
end

function B738_vrnp()
	if B738DR_nps_disable == 0 then
		B738DR_vrnp_enable = 1
	else
		B738DR_vrnp_enable = 0
	end
	B738DR_vrnp = vrnp_num
	B738DR_vanp = 60
end

function B738_path_err()
	
	timer_pth_err = timer_pth_err + SIM_PERIOD
	if timer_pth_err > 0.1 then
		vnav_alt_err_ratio = (vnav_alt_err_ratio_old - B738DR_vnav_alt_err) / timer_pth_err
		vnav_alt_err_ratio_old = B738DR_vnav_alt_err
		
		gp_alt_err_ratio = (gp_alt_err_ratio_old - B738DR_gp_alt_err) / timer_pth_err
		gp_alt_err_ratio_old = B738DR_gp_alt_err
		
		vnav_alt_err_ratio2 = (vnav_alt_err_ratio2_old - B738DR_vnav_alt_err2) / timer_pth_err
		vnav_alt_err_ratio2_old = B738DR_vnav_alt_err2
		
		gp_alt_err_ratio2 = (gp_alt_err_ratio2_old - B738DR_gp_alt_err2) / timer_pth_err
		gp_alt_err_ratio2_old = B738DR_gp_alt_err2
		
		timer_pth_err = 0
	end
	
end

function B738_vnav_pth3()
	
	local n = 0
	local dist = 0
	local ed_dist_remain = 0
	-- local leg_dist = 0
	-- local rf_dist = 0
	local vnav_td_dist = 0
	local vnav_td_dist_mod = 0
	local ii = 0
	local ed_found_temp = 0
	local ed_alt_temp = 0
	local ed_found_temp2 = 0
	local ed_fix_distance2 = 0
	--local ed_found_type = 0
	
	local rnav_idx_last_1 = 0
	local rnav_idx_post = 0
	
	local vnav_vvi_trg = 0
	local vnav_vvi_tmp = 0
	local gp_vvi_trg = 0
	local gp_vvi_tmp = 0
	
	local nd_lat = 0
	local nd_lon = 0
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_x = 0
	local nd_y = 0
	local td_enable = 0
	local rnav_disable = 0
	
	local nd_dis = 0
	local nd_hdg = 0
	local true_brg = 0
	local relative_brg = 0
	--local vnav_vvi_old = 0
	
	local nd_vert_path = 0
	local rnav_vpa_temp = 0
	local rnav_vpa_temp2 = 0
	local gp_active = 0
	local descent_vpa = 0
	local gp_disable = 0
	local vnav_app_active = 0
	local not_idle_pth = 0
	local idle_pth_idx = 0
	local vnav_alt_cor = 0
	local gp_alt_cor = 0
	local vnav_err_lim = 0
	local td2_alt_hold_x = 0
	local ed2_alt2_hold_x = 0
	
	local ed_table_temp = 0
	local vnav_pth_fpm = 0
	local vnav_pth_fpm2 = 0
	local gp_pth_fpm = 0
	
	local rnav_alt2 = 0
	local rw_dist_temp = 0
	
	local lat_tmp = 0
	local lon_tmp = 0
	
	local vnav_pth_elev = 0
	
	local ian_available = 0
	local pfd_gp_path = 0
	
	if simDR_int_ver < 120000 then
		simDR_sea_level_inhg = simDR_sea_level_inhg_xp11
		simDR_sea_level_temp = 15
	else
		simDR_sea_level_inhg = simDR_sea_level_inhg_xp11	--simDR_sea_level_inhg_xp12 * 0.00029529983071445
		simDR_sea_level_temp = simDR_sea_level_temp_xp12
	end
	
	rw_dist2 = 0
	ed_found_type = 0
	
	if B738DR_flight_phase < 5 then
		td_enable = 1
		rnav_disable = 1
	end
	if B738DR_flight_phase == 8 then
		-- if B738DR_fd_goaround > 0 or B738DR_ap_goaround > 0 then
			td_enable = 1
			rnav_disable = 1
		-- end
	end
	-- if B738DR_fms_ils_disable == 0 or rnav_disable == 1 or gp_available == 0 then
	if rnav_disable == 1 or gp_available == 0 then
		gp_disable = 1
	end
	
	if td_idx == 0 then	--and calc_rte_enable == 0 then
		td_enable = 0
	end
	
	if offset > legs_num then
		offset = legs_num
		offset_seq = offset
	end
	
	local use_ref_icao = ""
	local use_des_icao = ""
	if rte12_act == 0 then
		use_ref_icao = ref_icao
		use_des_icao = des_icao
	elseif rte12_act == 1 then
		use_ref_icao = ref_icao7
		use_des_icao = des_icao7
	end
	
	if crz_alt_num > 0 and legs_num > 1 and offset > 0 and offset <= legs_num and perf_exec > 0 and use_ref_icao ~= "----" and use_des_icao ~= "****" then
		
		-- NEW ----------------------------------------
		if offset ~= 0 then
			if ed_found ~= 0 then
				if offset == ed_found then
					ed_dist_remain = B738DR_fmc_dist2
				elseif offset < ed_found then
					ed_dist_remain = B738DR_fmc_dist2 + (legs_data[offset][50] - legs_data[ed_found][50])
				end
			end
			if td_idx ~= 0 then
				if offset == td_idx then
					vnav_td_dist = B738DR_fmc_dist - td_dist
				elseif offset < td_idx then
					vnav_td_dist = B738DR_fmc_dist + (legs_data[offset][50] - legs_data[td_idx][50]) - td_dist
				end
			end
		end
		-- NEW ----------------------------------------
		
		if offset == td_idx then
			vnav_td_dist = B738DR_fmc_dist - td_dist
		elseif offset > td_idx then
			if offset == td_idx + 1 then
				if B738DR_fmc_dist > td_dist99 then
					vnav_td_dist = B738DR_fmc_dist - td_dist99
				else
					vnav_td_dist = 0
				end
			else
				vnav_td_dist = 0
			end
		end
		if vnav_td_dist < 0 then
			vnav_td_dist = 0
		end
		B738DR_vnav_td_dist_fix = vnav_td_dist
		if td_enable == 0 then
			vnav_td_dist = 0
		end
		
		local vnav_td_dist2 = 0
		if td_idx ~= 0 and offset ~= 0 then
			vnav_td_dist2 = (B738DR_fmc_dist + legs_data[offset][50]) - (legs_data[td_idx][50] + td_dist)
		end
		B738DR_vnav_td_dist = vnav_td_dist2
		
		if rte12_act == 0 then
			if td_idx_mod ~= 0 and offset_mod ~= 0 and offset_mod <= legs_num2 and td_idx_mod <= legs_num2 then
				
				-- NEW ----------------------------------------
				if offset_mod ~= 0 then
					if td_idx_mod ~= 0 then
						if offset_mod == td_idx_mod then
							vnav_td_dist_mod = B738DR_fmc_dist_mod - td_dist_mod
						elseif offset_mod < td_idx_mod then
							vnav_td_dist_mod = B738DR_fmc_dist_mod + (legs_data2[offset_mod][50] - legs_data2[td_idx_mod][50]) - td_dist_mod
						end
					end
				end
				-- NEW ----------------------------------------
				
				if offset_mod == td_idx_mod then
					vnav_td_dist_mod = B738DR_fmc_dist_mod - td_dist_mod
				elseif offset_mod > td_idx_mod then
					if offset_mod == td_idx_mod + 1 then
						if B738DR_fmc_dist_mod > td_dist99_mod then
							vnav_td_dist_mod = B738DR_fmc_dist_mod - td_dist99_mod
						else
							vnav_td_dist_mod = 0
						end
					else
						vnav_td_dist_mod = 0
					end
				end
			end
		elseif rte12_act == 1 then
			if td_idx_mod ~= 0 and offset_mod2 ~= 0 and offset_mod2 <= legs_num8 and td_idx_mod <= legs_num8 then
				
				-- NEW ----------------------------------------
				if offset_mod2 ~= 0 then
					if td_idx_mod ~= 0 then
						if offset_mod2 == td_idx_mod then
							vnav_td_dist_mod = B738DR_fmc_dist_mod - td_dist_mod
						elseif offset_mod2 < td_idx_mod then
							vnav_td_dist_mod = B738DR_fmc_dist_mod + (legs_data8[offset_mod2][50] - legs_data8[td_idx_mod][50]) - td_dist_mod
						end
					end
				end
				-- NEW ----------------------------------------
				
				if offset_mod2 == td_idx_mod then
					vnav_td_dist_mod = B738DR_fmc_dist_mod - td_dist_mod
				elseif offset_mod2 > td_idx_mod then
					if offset_mod2 == td_idx_mod + 1 then
						if B738DR_fmc_dist_mod > td_dist99_mod then
							vnav_td_dist_mod = B738DR_fmc_dist_mod - td_dist99_mod
						else
							vnav_td_dist_mod = 0
						end
					else
						vnav_td_dist_mod = 0
					end
				end
			end
		end
		
		if vnav_td_dist_mod < 0 then
			vnav_td_dist_mod = 0
		end
		B738DR_vnav_td_dist_fix_mod = vnav_td_dist_mod
		if td_idx_mod == 0 then
			vnav_td_dist_mod = 0
		end
		B738DR_vnav_td_dist_mod = vnav_td_dist_mod
		
		
		if offset <= legs_num then
			if B738DR_flight_phase == 4 and td_idx ~= 0 and ed_found ~= 0 and calc_rte_enable == 0 then
				if ed_fix_num > 0 then
					ed_alt_temp = ed_fix_alt2[ed_fix_num]
					ed_table_temp = ed_fix_table[ed_fix_num]
					ed_found_temp = ed_fix_found2[ed_fix_num]
				else
					ed_alt_temp = ed_alt
					ed_table_temp = 1
					ed_found_temp = ed_found
				end
				
				if ed_fix_num == 0 then
					dist = ed_dist_remain
				else
					dist = ed_dist_remain - ed_fix_distance[ed_fix_num]
				end
				
				if dist > 0 then
					B738DR_vnav_pth_alt99 = calc_vnav_pth_alt(ed_alt_temp, dist, ed_table_temp)
					if vnav_pth_alt99_old == 0 then
						vnav_pth_alt99_old = B738DR_vnav_pth_alt99
					end
					vnav_pth_fpm2 = ((B738DR_vnav_pth_alt99 - vnav_pth_alt99_old) / SIM_PERIOD) * 60		-- vnav pth fpm
					
					-- B738DR_vnav_alt_err99 = B738DR_vnav_pth_alt99 - simDR_altitude_pilot
					
					if ed_dist_remain > distance_tl and (distance_tl ~= 0 or ed_tl == 1) then
						-- -- above QNH
						vnav_pth_elev = alt_qnh_up(B738DR_vnav_pth_alt99, simDR_sea_level_inhg, simDR_sea_level_temp)
					else
						-- -- below QNH
						if simDR_autopilot_side == 0 then
							vnav_pth_elev = alt_qnh_shift2(B738DR_vnav_pth_alt99, simDR_sea_level_inhg, simDR_barometer_setting_capt)
						else
							vnav_pth_elev = alt_qnh_shift2(B738DR_vnav_pth_alt99, simDR_sea_level_inhg, simDR_barometer_setting_fo)
						end
					end
					
					B738DR_vnav_alt_err99 = vnav_pth_elev - elevation_ft
					
					--***********************************--
					--***********************************--
					vnav_alt_cor = B738DR_vnav_alt_err99	-- => -500 = above 500ft, 500 = below 500ft
					
					if vnav_alt_cor > 700 then
						vnav_vvi_trg = 0
					else
						if math.abs(vnav_alt_cor) < 300 then
							vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
							vnav_alt_cor = math.sin(vnav_alt_cor) * 1200
						else
							vnav_alt_cor = math.min(vnav_alt_cor, 700)
							vnav_alt_cor = math.max(vnav_alt_cor, -700)
							if vnav_alt_cor > 0 then
								vnav_alt_cor = 1170 + (vnav_alt_cor - 300) * 2.2
							else
								vnav_alt_cor = ((vnav_alt_cor + 300) * 2.2) - 1170
							end
						end
						
						vnav_vvi_trg = vnav_pth_fpm2 + vnav_alt_cor
						
						vnav_vvi_trg = math.max (vnav_vvi_trg, -5500)
						vnav_vvi_trg = math.min (vnav_vvi_trg, 0)
					end
					
					-- VNAV engaged
					if B738DR_altitude_mode == 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then -- VNAV
						if vnav_vvi_trg > vnav_vvi_cur99 then
							vnav_vvi_cur99 = B738_set_animation_rate(vnav_vvi_cur99, vnav_vvi_trg, -5500, 5500, 0.002)
						else
							vnav_vvi_cur99 = B738_set_animation_rate(vnav_vvi_cur99, vnav_vvi_trg, -5500, 5500, 0.005)
						end
						B738DR_vnav_vvi99 = vnav_vvi_cur99
					else
						B738DR_vnav_vvi99 = 0
					end
					
					if B738DR_vnav_alt_err99 > 9999 then
						B738DR_vnav_alt_err99 = 9999
					elseif B738DR_vnav_alt_err99 < -9999 then
						B738DR_vnav_alt_err99 = -9999
					end
					
					if B738DR_vnav_vvi99 < -5500 then		-- max descent vvi -3800 -2500
						B738DR_vnav_vvi99 = -5500
					end
					
					if B738DR_vnav_vvi99 > 0 then		-- min descent vvi -3800 -1000
						B738DR_vnav_vvi99 = 0
					end
					vnav_pth_alt99_old = B738DR_vnav_pth_alt99
				else
					vnav_pth_alt99_old = 0
					B738DR_vnav_alt_err99 = 9999
					vnav_vvi_cur99 = simDR_vvi_fpm_pilot
					B738DR_vnav_pth_alt99 = 0
				end
			else
				vnav_pth_alt99_old = 0
				B738DR_vnav_alt_err99 = 9999
				vnav_vvi_cur99 = simDR_vvi_fpm_pilot
				B738DR_vnav_pth_alt99 = 0
			end
		end
	else
		vnav_pth_alt99_old = 0
		B738DR_vnav_alt_err99 = 9999
		vnav_vvi_cur99 = simDR_vvi_fpm_pilot
		B738DR_vnav_pth_alt99 = 0
		B738DR_vnav_vvi99 = 0
	end
	
	-- B738DR_fms_test4 = ed_dist_remain
	-- B738DR_fms_test5 = distance_tl
	
	if calc_rte_enable ~= 0 then
		-- wait for finish calc
		if td_idx == 0 then
			B738DR_vnav_td_dist = 0
		else
			if crz_alt_num > 0 and legs_num > 1 and offset > 0 and perf_exec > 0 and use_ref_icao ~= "----" and use_des_icao ~= "****" then
				B738DR_vnav_td_dist = 9999
			else
				B738DR_vnav_td_dist = 0
			end
		end
		
		vnav_pth_alt_old = 0
		
	elseif crz_alt_num > 0 and legs_num > 1 and offset > 0 and offset <= legs_num and perf_exec > 0 and use_ref_icao ~= "----" and use_des_icao ~= "****" then
		
		ed_found_temp = ed_found
		ed_alt_temp = ed_alt
		descent_vpa = ed_vpa
		ed_found_type = ed_type
		ed_table_temp = 1
		idle_pth_idx = 0
		if ed_fix_num > 0 then
			if offset <= ed_found then
				for n = 1, ed_fix_num do
					if ed_dist_remain > ed_fix_distance[n] then
						ed_found_temp = ed_fix_found2[n]
						ed_alt_temp = ed_fix_alt2[n]
						descent_vpa = ed_fix_vpa2[n]
						ed_found_type = ed_fix_type[n]
						ed_table_temp = ed_fix_table[n]
						idle_pth_idx = n
						break
					end
				end
			end
			
		end
		-- B738DR_fms_test4 = ed_dist_remain
		-- B738DR_fms_test5 = ed_found_temp
		-- B738DR_fms_test6 = ed_alt_temp
		-- B738DR_fms_test7 = idle_pth_idx
		-- B738DR_fms_test8 = ed_table_temp
		
		-- calculate VNAV PTH
			if offset > legs_num then
				offset = legs_num
				offset_seq = offset
			end
			
			ed_to_dist = 0
			rw_dist = 0
			
			
			if offset <= ed_found then
				
				 -- VNAV profile
				-- if vnav_td_dist == 0 or (B738DR_flight_phase > 4 and B738DR_flight_phase < 8) or B738DR_flight_phase == 2 then
				if vnav_td_dist == 0 or (B738DR_flight_phase > 4 and B738DR_flight_phase < 8) then
					
					if idle_pth_idx == 0 then
						dist = ed_dist_remain
					else
						dist = ed_dist_remain - ed_fix_distance[idle_pth_idx]
					end
					
					ed_to_dist = dist
					
					--if not_idle_pth == 0 then
					if ed_fix_num == 0 then
						B738DR_vnav_pth_alt = calc_vnav_pth_alt(ed_alt_temp, dist, 1)
					else
						if ed_found_type == 0 then
							-- idle path
							if td_idx2 ~= 0 and td_idx2 <= ed_found_temp then
								if td_idx2 > offset or (td_idx2 == offset and B738DR_fmc_dist > td_dist2 + 1.0) then		--0.3
									if simDR_altitude_pilot < (td_alt2 - 700) then
										B738DR_vnav_pth_alt = calc_vnav_pth_alt(ed_alt_temp, dist, ed_table_temp)
									else
										B738DR_vnav_pth_alt = td_alt2
										td2_alt_hold_x = 1
									end
								else
									B738DR_vnav_pth_alt = calc_vnav_pth_alt(ed_alt_temp, dist, ed_table_temp)
								end
							else
								B738DR_vnav_pth_alt = calc_vnav_pth_alt(ed_alt_temp, dist, ed_table_temp)
							end
						else
							-- geometric path
							B738DR_vnav_pth_alt = ed_alt_temp + ((dist * (math.tan(math.rad(descent_vpa)))) * 6076.11549) -- ft
						end
					end
					
					if ed_alt_temp == ed_alt then
						ed2_alt2_hold_x = 1
					end
					
					-- No recalc
					if ed_fix_num > 0 then
						if idle_pth_idx == 0 then
							if ed_alt_temp == ed_fix_alt2[ed_fix_num] then
								B738DR_vnav_pth_alt = ed_alt_temp
								ed_found_type = 1	-- non idle path
							end
							-- B738DR_fms_test5 = ed_fix_alt2[ed_fix_num]
						elseif idle_pth_idx > 1 then
							if ed_alt_temp == ed_fix_alt2[idle_pth_idx - 1] then
								B738DR_vnav_pth_alt = ed_alt_temp
								ed_found_type = 1	-- non idle path
							end
							-- B738DR_fms_test5 = ed_fix_alt2[idle_pth_idx - 1]
						end
						-- B738DR_fms_test4 = ed_alt_temp
					end
					
					-- B738DR_fms_test6 = B738DR_vnav_pth_alt
					
					-- B738DR_fms_test3 = idle_pth_idx
					
					last_ed_alt = B738DR_vnav_pth_alt
					
					local vnav_first_time = 0
					if vnav_pth_alt_old == 0 then
						vnav_pth_alt_old = B738DR_vnav_pth_alt
						vnav_first_time = 1
					end
					vnav_pth_fpm = ((B738DR_vnav_pth_alt - vnav_pth_alt_old) / SIM_PERIOD) * 60		-- vnav pth fpm
					vnav_pth_alt_old = B738DR_vnav_pth_alt
					
					--B738DR_fms_test3 = vnav_pth_fpm
					
					-- B738DR_vnav_alt_err2 = (B738DR_vnav_pth_alt + (0.2 * vnav_pth_fpm)) - (simDR_altitude_pilot + (0.2 * simDR_vvi_fpm_pilot))
					-- B738DR_vnav_alt_err = B738DR_vnav_pth_alt - simDR_altitude_pilot
					
					if ed_dist_remain > distance_tl and (distance_tl ~= 0 or ed_tl == 1) then
						-- above QNH
						vnav_pth_elev = alt_qnh_up(B738DR_vnav_pth_alt, simDR_sea_level_inhg, simDR_sea_level_temp)
					else
						-- below QNH
						if simDR_autopilot_side == 0 then
							vnav_pth_elev = alt_qnh_shift2(B738DR_vnav_pth_alt, simDR_sea_level_inhg, simDR_barometer_setting_capt)
						else
							vnav_pth_elev = alt_qnh_shift2(B738DR_vnav_pth_alt, simDR_sea_level_inhg, simDR_barometer_setting_fo)
						end
					end
					B738DR_vnav_alt_err = vnav_pth_elev - elevation_ft
					B738DR_vnav_alt_err2 = (vnav_pth_elev + (0.2 * vnav_pth_fpm)) - (elevation_ft + (0.2 * simDR_vvi_fpm_pilot))
					
					--***********************************--
					--***********************************--
					vnav_alt_cor = B738DR_vnav_alt_err	-- => -500 = above 500ft, 500 = below 500ft
					
					-- if vnav_alt_cor > 700 then
						-- B738DR_vnav_vvi_corr = 0
						-- vnav_vvi_trg = 0
					-- else
					-- B738DR_fms_test3 = 0
					-- B738DR_fms_test4 = vnav_pth_fpm
					if vnav_first_time == 0 then
						-- if math.abs(vnav_alt_cor) < 300 then
							-- vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
							-- vnav_alt_cor = math.sin(vnav_alt_cor) * 1200
						if vnav_alt_cor < 300 and vnav_alt_cor >= 0 then
							vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
							vnav_alt_cor = math.sin(vnav_alt_cor) * 2400
							if vnav_alt_cor > 1170 then
								vnav_alt_cor = 1170
							end
						elseif vnav_alt_cor > -300 and vnav_alt_cor < 0 then
							vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
							vnav_alt_cor = math.sin(vnav_alt_cor) * 1200
						else
							vnav_alt_cor = math.min(vnav_alt_cor, 700)
							vnav_alt_cor = math.max(vnav_alt_cor, -700)
							if vnav_alt_cor > 0 then
								vnav_alt_cor = 1170 + (vnav_alt_cor - 300) * 2.2
							else
								vnav_alt_cor = ((vnav_alt_cor + 300) * 2.2) - 1170
							end
						end
						
						B738DR_vnav_vvi_corr = vnav_alt_cor
						vnav_vvi_trg = vnav_pth_fpm + vnav_alt_cor
						
						if B738DR_vnav_alt_err > 50 and vnav_pth_fpm + 200 < simDR_vvi_fpm_pilot and simDR_vvi_fpm_pilot <= 0 
						and simDR_altitude_pilot > 5000 then
							vnav_vvi_trg = math.min(vnav_vvi_trg, simDR_vvi_fpm_pilot)
							-- B738DR_fms_test3 = 1
						else
							if B738DR_vnav_alt_err > 700 then
								B738DR_vnav_vvi_corr = 0
								vnav_vvi_trg = 0
								-- B738DR_fms_test3 = 2
							else
								vnav_vvi_trg = math.min(vnav_vvi_trg, 0)
								-- B738DR_fms_test3 = 3
							end
						end
						vnav_vvi_trg = math.max(vnav_vvi_trg, -5500)
					else
						B738DR_vnav_vvi_corr = 0
						vnav_vvi_trg = math.min(0, simDR_vvi_fpm_pilot)
						vnav_vvi_trg = math.max(vnav_vvi_trg, -5500)
						-- B738DR_fms_test3 = 4
					end
					-- B738DR_fms_test5 = vnav_vvi_trg
					
					-- VNAV engaged
					if B738DR_altitude_mode == 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then -- VNAV
						if vnav_vvi_trg > vnav_vvi_cur then
							vnav_vvi_cur = B738_set_animation_rate(vnav_vvi_cur, vnav_vvi_trg, -5500, 5500, 0.002)
						else
							vnav_vvi_cur = B738_set_animation_rate(vnav_vvi_cur, vnav_vvi_trg, -5500, 5500, 0.005)
						end
						B738DR_vnav_vvi = vnav_vvi_cur
					else
						B738DR_vnav_vvi = 0
					end
					
					-- if B738DR_vnav_alt_err > 9999 then
						-- B738DR_vnav_err_pfd = 9999
					-- else
						-- if B738DR_vnav_alt_err < -9999 then
							-- B738DR_vnav_err_pfd = -9999
						-- else
							-- B738DR_vnav_err_pfd = B738DR_vnav_alt_err
						-- end
					-- end
					
					B738DR_vnav_err_pfd = B738DR_vnav_alt_err
					B738DR_vnav_err_pfd = math.min(B738DR_vnav_err_pfd, crz_alt_num - simDR_altitude_pilot + 1000)
					if simDR_altitude_pilot < crz_alt_num - 900 then
						if td_idx2 ~= 0 and td_idx2 > offset and td_idx2 <= ed_found_temp and simDR_altitude_pilot < td_alt2 and B738DR_vnav_pth_alt > td_alt2 then
							B738DR_vnav_err_pfd = math.min(B738DR_vnav_err_pfd, td_alt2 - simDR_altitude_pilot)
						elseif B738DR_vnav_pth_alt > crz_alt_num then
							B738DR_vnav_err_pfd = math.min(B738DR_vnav_err_pfd, crz_alt_num - simDR_altitude_pilot)
						end
					end
					B738DR_vnav_err_pfd = math.min(B738DR_vnav_err_pfd, 9999)
					B738DR_vnav_err_pfd = math.max(B738DR_vnav_err_pfd, -9999)
					
					nd_vert_path = 1
					
					if B738DR_vnav_vvi < -5500 then		-- max descent vvi -3800 -2500
						B738DR_vnav_vvi= -5500
					end
					
					if B738DR_vnav_vvi > 0 then		-- min descent vvi -3800 -1000
						B738DR_vnav_vvi = 0
					end
					
					if B738DR_altitude_mode == 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and simDR_ap_altitude_dial_ft < (simDR_altitude_pilot - 150) then
						if B738DR_vnav_desc_spd_disable == 0 then
							-- if crz_alt_num ~= 0 and simDR_altitude_pilot < (crz_alt_num - 1000) then		--150
							if crz_alt_num ~= 0 and simDR_altitude_pilot < (crz_alt_num - 250) then
								if B738DR_rest_wpt_alt_t ~= 45 and simDR_altitude_pilot > B738DR_rest_wpt_alt + 150 then
									if simDR_altitude_pilot > (ed_alt + 1000) and simDR_altitude_pilot > 5000 then
										if B738DR_vnav_alt_err > 500 and B738DR_vnav_alt_err2 > 500 and B738DR_fms_descent_now == 3 then
											B738DR_fms_descent_now = 2
											simDR_ap_vvi_dial = math.min(0, simDR_vvi_fpm_pilot)
											B738DR_ap_vvi_dial = math.min(0, simDR_vvi_fpm_pilot)
										end
									end
								end
							end
						end
					end
					--B738DR_fms_test3 = 999
				end
			
			else
				--B738DR_fms_test3 = 1000
				-- RNAV profile
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					nd_vert_path = 0
				elseif rnav_disable == 1 then
					nd_vert_path = 0
				else
				
	-- rnav_alt = legs_data[rnav_idx_last][5]
	-- rnav_vpa = legs_data[rnav_idx_last][20]
					
					rnav_alt2 = 0
					rw_dist_temp = 0
					if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
						rnav_alt2 = rnav_alt
						-- if string.sub(legs_data[rnav_idx_last][1], 1, 2) == "RW" then
							-- rnav_alt2 = rnav_alt
						-- else
							-- if B738DR_dest_runway_start_lat ~= 0 and B738DR_dest_runway_start_lon ~= 0 then
								-- -- rw_dist_temp = nd_calc_dist2(legs_data[rnav_idx_last][7], legs_data[rnav_idx_last][8], B738DR_dest_runway_start_lat, B738DR_dest_runway_start_lon)
								-- lat_tmp, lon_tmp = lat_lon_legs(rnav_idx_last)
								-- rw_dist_temp = nd_calc_dist2(lat_tmp, lon_tmp, B738DR_dest_runway_start_lat, B738DR_dest_runway_start_lon)
								-- rnav_alt2 = B738DR_dest_runway_alt + 50 --+50
							-- end
						-- end
					end
					if rnav_alt2 ~= 0 and offset > ed_found and offset <= rnav_idx_last then
						
						-- rnav_vpa_temp = 0
						-- if rnav_idx_last == rnav_idx_first + 1 then
							-- rw_dist = rw_dist_temp + wpt_distance(rnav_idx_last)
							-- if rw_dist ~= 0 and legs_data[rnav_idx_first][5] > rnav_alt2 then
								-- rnav_vpa_temp = math.atan((legs_data[rnav_idx_first][5] - rnav_alt2) / (rw_dist * 6076.11549))
							-- end
							-- if rnav_vpa_temp == 0 then
								-- rnav_vpa_temp = 3
							-- elseif rnav_vpa_temp < 2.75 then
								-- rnav_vpa_temp = 3
							-- elseif rnav_vpa_temp > 3.77 then
								-- rnav_vpa_temp = 3.77
							-- end
						-- elseif rnav_idx_last == rnav_idx_first then
							-- rnav_vpa_temp = 3
						-- else
							-- rw_dist = rw_dist_temp
							-- for n = rnav_idx_last, rnav_idx_first + 1, -1 do
								-- rw_dist = rw_dist + wpt_distance(n)
							-- end
							-- if rw_dist ~= 0 and legs_data[rnav_idx_first][5] > rnav_alt2 then
								-- rnav_vpa_temp = math.atan((legs_data[rnav_idx_first][5] - rnav_alt2) / (rw_dist * 6076.11549))
							-- end
							-- if rnav_vpa_temp == 0 then
								-- rnav_vpa_temp = 3
							-- elseif rnav_vpa_temp < 2.75 then
								-- rnav_vpa_temp = 3
							-- elseif rnav_vpa_temp > 3.77 then
								-- rnav_vpa_temp = 3.77
							-- end
							-- rw_dist = rw_dist_temp
							-- for n = rnav_idx_last, rnav_idx_first + 1, -1 do
								-- rw_dist = rw_dist + wpt_distance(n)
								-- if legs_data[n-1][5] ~= 0 and rw_dist ~= 0 and legs_data[n-1][5] > rnav_alt2 then
									-- rnav_vpa_temp2 = math.atan((legs_data[n-1][5] - rnav_alt2) / (rw_dist * 6076.11549))
									-- rnav_vpa_temp2 = math.deg(rnav_vpa_temp2)
									-- if rnav_vpa_temp2 > rnav_vpa_temp then
										-- rnav_vpa_temp = rnav_vpa_temp2
									-- end
								-- end
							-- end
							-- if rnav_vpa_temp == 0 then
								-- rnav_vpa_temp = 3
							-- elseif rnav_vpa_temp < 2.75 then
								-- rnav_vpa_temp = 3
							-- elseif rnav_vpa_temp > 3.77 then
								-- rnav_vpa_temp = 3.77
							-- end
						-- end
						-- if rnav_vpa < 0 and rnav_vpa_temp < -rnav_vpa then
							-- rnav_vpa_temp = -rnav_vpa
						-- end
						
						if rnav_vpa < 0 then
							rnav_vpa_temp = -rnav_vpa
						else
							rnav_vpa_temp = 3
						end
						
						ed_found_type = 1
						
						-- B738DR_fms_test3 = rnav_vpa_temp
						
						-- calc track distance to RW
						rw_dist = rw_dist_temp
						if offset == rnav_idx_last then
							-- calc direct distance to RW
							nd_lat = simDR_latitude
							nd_lon = simDR_longitude
							-- nd_lat2 = legs_data[rnav_idx_last][7]
							-- nd_lon2 = legs_data[rnav_idx_last][8]
							nd_lat2, nd_lon2 = lat_lon_legs(rnav_idx_last)
							
							rw_dist = rw_dist + nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
						else
							for n = offset, rnav_idx_last do
								if n == offset then
									rw_dist = rw_dist + B738DR_fmc_dist2
								else
									rw_dist = rw_dist + wpt_distance(n)
								end
							end
						end
						-- calc VNAV path error
						B738DR_vnav_pth_alt = rnav_alt2 + ((rw_dist * (math.tan(math.rad(rnav_vpa_temp)))) * 6076.11549)	-- (in ft)
						
						if B738DR_vnav_pth_alt > ed_alt + 300 and last_ed_alt >= 0 then
							-- dist = nd_calc_dist2(simDR_latitude, simDR_longitude, legs_data[ed_found][7], legs_data[ed_found][8])
							lat_tmp, lon_tmp = lat_lon_legs(ed_found)
							dist = nd_calc_dist2(simDR_latitude, simDR_longitude, lat_tmp, lon_tmp)
							if ed_fix_num == 0 or ed_type == 0 then
								B738DR_vnav_pth_alt = calc_vnav_pth_alt(ed_alt, dist, 1)
							else
								B738DR_vnav_pth_alt = ed_alt + ((dist * (math.tan(math.rad(ed_vpa)))) * 6076.11549) -- ft
							end
							if B738DR_vnav_pth_alt > last_ed_alt then
								B738DR_vnav_pth_alt = ed_alt
							end
							last_ed_alt = B738DR_vnav_pth_alt
						end
						
						if vnav_pth_alt_old == 0 then
							vnav_pth_alt_old = B738DR_vnav_pth_alt
						end
						vnav_pth_fpm = ((B738DR_vnav_pth_alt - vnav_pth_alt_old) / SIM_PERIOD) * 60		-- vnav pth fpm
						vnav_pth_alt_old = B738DR_vnav_pth_alt
						
						B738DR_vnav_alt_err2 = (B738DR_vnav_pth_alt + (0.2 * vnav_pth_fpm)) - (simDR_altitude_pilot + (0.2 * simDR_vvi_fpm_pilot))
					
						B738DR_vnav_alt_err = B738DR_vnav_pth_alt - simDR_altitude_pilot
						
						--***********************************--
						vnav_alt_cor = B738DR_vnav_alt_err	-- => -500 = above 500ft, 500 = below 500ft
						if vnav_alt_cor > 700 then
							B738DR_vnav_vvi_corr = 0
							vnav_vvi_trg = 0
						else
							-- if math.abs(vnav_alt_cor) < 300 then
								-- vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
								-- vnav_alt_cor = math.sin(vnav_alt_cor) * 1200
							if vnav_alt_cor < 300 and vnav_alt_cor >= 0 then
								vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
								vnav_alt_cor = math.sin(vnav_alt_cor) * 2400
								if vnav_alt_cor > 1170 then
									vnav_alt_cor = 1170
								end
							elseif vnav_alt_cor > -300 and vnav_alt_cor < 0 then
								vnav_alt_cor = (vnav_alt_cor / 350) * 1.57
								vnav_alt_cor = math.sin(vnav_alt_cor) * 1200
							else
								vnav_alt_cor = math.min(vnav_alt_cor, 700)
								vnav_alt_cor = math.max(vnav_alt_cor, -700)
								if vnav_alt_cor > 0 then
									vnav_alt_cor = 1170 + (vnav_alt_cor - 300) * 2.2
								else
									vnav_alt_cor = ((vnav_alt_cor + 300) * 2.2) - 1170
								end
							end
							
							B738DR_vnav_vvi_corr = vnav_alt_cor
							vnav_vvi_trg = vnav_pth_fpm + vnav_alt_cor
							
							vnav_vvi_trg = math.max (vnav_vvi_trg, -5500)
							vnav_vvi_trg = math.min (vnav_vvi_trg, 0)
						end
						
						-- VNAV engaged
						if B738DR_altitude_mode == 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then -- VNAV
							if vnav_vvi_trg > vnav_vvi_cur then
								vnav_vvi_cur = B738_set_animation_rate(vnav_vvi_cur, vnav_vvi_trg, -5500, 5500, 0.002)
							else
								vnav_vvi_cur = B738_set_animation_rate(vnav_vvi_cur, vnav_vvi_trg, -5500, 5500, 0.005)
							end
							B738DR_vnav_vvi = vnav_vvi_cur
						else
							B738DR_vnav_vvi = 0
						end
						
						-- if B738DR_vnav_alt_err > 9999 then
							-- B738DR_vnav_err_pfd = 9999
						-- else
							-- if B738DR_vnav_alt_err < -9999 then
								-- B738DR_vnav_err_pfd = -9999
							-- else
								-- B738DR_vnav_err_pfd = B738DR_vnav_alt_err
							-- end
						-- end
						
						B738DR_vnav_err_pfd = B738DR_vnav_alt_err
						if simDR_altitude_pilot < crz_alt_num - 900 and B738DR_vnav_pth_alt > crz_alt_num then
							B738DR_vnav_err_pfd = math.min(B738DR_vnav_err_pfd, crz_alt_num - simDR_altitude_pilot)
						end
						B738DR_vnav_err_pfd = math.min(B738DR_vnav_err_pfd, 9999)
						B738DR_vnav_err_pfd = math.max(B738DR_vnav_err_pfd, -9999)
						
						-- VNAV path
						nd_vert_path = 1
						gp_active = 1
						vnav_app_active = 1
						
						if B738DR_vnav_vvi < -5500 then		-- max descent vvi -3800 -2500
							B738DR_vnav_vvi= -5500
						end
						
						if B738DR_vnav_vvi > 0 then		-- min descent vvi -3800 -1000
							B738DR_vnav_vvi = 0
						end
						
						--***********************************--
						--***********************************--
					else
						nd_vert_path = 0
					end
				end
			end
		--end
		
		B738DR_nd_vert_path = nd_vert_path
		if gp_disable == 0 then
			B738DR_gp_active = gp_active
		else
			B738DR_gp_active = 0
		end
		if nd_vert_path == 0 then
			B738DR_vnav_alt_err = 500
			B738DR_vnav_vvi = 0
			ed_to_dist = 0
			B738DR_vnav_alt_err2 = 500
		end
		
		-- calculate RNAV
		if offset > 1 and rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
			
			if simDR_on_ground[0] == 1 and simDR_on_ground[1] == 1 and simDR_on_ground[2] == 1 then
				B738DR_gp_alt_err = 500
				B738DR_fac_xtrack = 999
				pfd_gp_path = 0
			-- elseif gp_disable == 1 then
			elseif gp_available == 0 then
				B738DR_gp_alt_err = 500
				B738DR_fac_xtrack = 999
				pfd_gp_path = 0
			else
				
				if offset > legs_num then
					offset = legs_num
					offset_seq = offset
				end
				
				rnav_alt2 = 0
				rw_dist_temp = 0
				rnav_vpa_temp = 0
				if rnav_idx_first ~= 0 and rnav_idx_last ~= 0 then
					if string.sub(legs_data[rnav_idx_last][1], 1, 2) == "RW" then
						rnav_alt2 = rnav_alt
						rnav_vpa_temp = -rnav_vpa
						if rnav_vpa_temp < 2.75 then
							rnav_vpa_temp = 3
						elseif rnav_vpa_temp > 3.77 then
							rnav_vpa_temp = 3.77
						end
					else
						if B738DR_dest_runway_start_lat ~= 0 and B738DR_dest_runway_start_lon ~= 0 then
							rw_dist_temp = 1
							rnav_alt2 = B738DR_dest_runway_alt + 50
							rnav_vpa_temp = 3
						end
					end
				end
				
				-- vpa exist
				if rnav_vpa_temp ~= 0 then	--and offset <= rnav_idx_last then
					
					-- calc direct distance to RW
					nd_lat = simDR_latitude
					nd_lon = simDR_longitude
					local rnav_fac_trk = 0
					if rw_dist_temp == 0 then
						-- nd_lat2 = legs_data[rnav_idx_last][7]
						-- nd_lon2 = legs_data[rnav_idx_last][8]
						nd_lat2, nd_lon2 = lat_lon_legs(rnav_idx_last)
						rnav_fac_trk = math.deg(legs_data[rnav_idx_last][2])
						rw_fac_id2 = legs_data[rnav_idx_last][1]
					else
						nd_lat2 = B738DR_dest_runway_start_lat
						nd_lon2 = B738DR_dest_runway_start_lon
						rnav_fac_trk = (B738DR_dest_runway_crs - mag_variation_deg(nd_lat2, nd_lon2) + 360) % 360
						rw_fac_id2 = "RW" .. B738DR_dest_runway_id
					end
					
					nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
					rw_dist2 = nd_dis
					rw_fac_crs2 = rnav_fac_trk
					B738DR_fac_trk = rnav_fac_trk
					
					B738DR_fac_brg = (nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2) + simDR_mag_variation + 360) % 360
					
					ian_available = 1
					
					if nd_dis < 25 then	--24
						-- calc GP path error
						B738DR_gp_alt_err = rnav_alt2 + ((nd_dis * (math.tan(math.rad(rnav_vpa_temp)))) * 6076.11549)	-- GP 3 deg (in ft)
						
						if gp_pth_alt_old == 0 then
							gp_pth_alt_old = B738DR_gp_alt_err
						end
						gp_pth_fpm = ((B738DR_gp_alt_err - gp_pth_alt_old) / SIM_PERIOD) * 60		-- vnav pth fpm
						gp_pth_alt_old = B738DR_gp_alt_err
						
						B738DR_gp_alt_err2 = (B738DR_gp_alt_err + (0.1 * gp_pth_fpm)) - (simDR_altitude_pilot + (0.1 * simDR_vvi_fpm_pilot))
						
						B738DR_gp_alt_err = B738DR_gp_alt_err - simDR_altitude_pilot
						
						-- -- calc FAC xtrack
						-- nd_lat = math.rad(nd_lat)
						-- nd_lon = math.rad(nd_lon)
						-- nd_lat2 = math.rad(nd_lat2)
						-- nd_lon2 = math.rad(nd_lon2)
						-- -- B738DR_fac_trk = math.deg(legs_data[rnav_idx_last][2])
						-- -- B738DR_fac_trk = rnav_fac_trk
						
						-- nd_y = math.sin(nd_lon2 - nd_lon) * math.cos(nd_lat2)
						-- nd_x = math.cos(nd_lat) * math.sin(nd_lat2) - math.sin(nd_lat) * math.cos(nd_lat2) * math.cos(nd_lon2 - nd_lon)
						-- nd_hdg = math.atan2(nd_y, nd_x)
						-- nd_hdg = math.deg(nd_hdg)
						-- nd_hdg = (nd_hdg + simDR_mag_variation + 360) % 360
						
						nd_hdg = B738DR_fac_brg
						true_brg = (B738DR_fac_trk + simDR_mag_variation + 360) % 360
						
						B738DR_fac_crs = nd_hdg
						-- calculate xtrack
						relative_brg = (true_brg - nd_hdg + 360) % 360
						if relative_brg > 180 then
							relative_brg = relative_brg - 360
						end
						if relative_brg < 0 then
							if relative_brg > -90 then
								relative_brg = -relative_brg
								B738DR_fac_xtrack = -nd_dis * math.sin(math.rad(relative_brg))
							else
								B738DR_fac_xtrack = -999
							end
						else
							if relative_brg < 90 then
								B738DR_fac_xtrack = nd_dis * math.sin(math.rad(relative_brg))
							else
								B738DR_fac_xtrack = 999
							end
						end
						
						-- -- GP path
						--***********************************--
						gp_alt_cor = B738DR_gp_alt_err	-- => -500 = above 500ft, 500 = below 500ft
						
						if gp_alt_cor > 700 then
							B738DR_gp_vvi_corr = 0
							gp_vvi_trg = 0
						else
							if math.abs(gp_alt_cor) < 300 then
								gp_alt_cor = (gp_alt_cor / 350) * 1.57
								gp_alt_cor = math.sin(gp_alt_cor) * 1200
							else
								gp_alt_cor = math.min(gp_alt_cor, 700)
								gp_alt_cor = math.max(gp_alt_cor, -700)
								if gp_alt_cor > 0 then
									gp_alt_cor = 1170 + (gp_alt_cor - 300) * 2.2
								else
									gp_alt_cor = ((gp_alt_cor + 300) * 2.2) - 1170
								end
							end
							
							B738DR_gp_vvi_corr = gp_alt_cor
							gp_vvi_trg = gp_pth_fpm + gp_alt_cor
							
							gp_vvi_trg = math.max (gp_vvi_trg, -4500)
							gp_vvi_trg = math.min (gp_vvi_trg, 0)
						end
						
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							gp_vvi_cur = B738_set_animation_rate(gp_vvi_cur, gp_vvi_trg, -4500, 4500, 0.005)
							B738DR_gp_vvi = gp_vvi_cur
						else
							B738DR_gp_vvi = 0
						end
						
						if B738DR_gp_alt_err > 9999 then
							B738DR_gp_err_pfd = 9999
						else
							if B738DR_gp_alt_err < -9999 then
								B738DR_gp_err_pfd = -9999
							else
								B738DR_gp_err_pfd = B738DR_gp_alt_err
							end
						end
						
						-- GP path
						pfd_gp_path = 1
						
						if B738DR_gp_vvi < -4500 then		-- max descent vvi -3800 -2500
							B738DR_gp_vvi= -4500
						end
						
						if B738DR_gp_vvi > 0 then		-- min descent vvi -3800 -1000
							B738DR_gp_vvi = 0
						end
						
						--***********************************--
						--***********************************--
						
						
					else
						pfd_gp_path = 0
						B738DR_gp_alt_err = 500
						B738DR_fac_xtrack = 999
						B738DR_gp_alt_err2 = 500
					end
				else
					pfd_gp_path = 0
					B738DR_gp_alt_err = 500
					B738DR_fac_xtrack = 999
					B738DR_gp_alt_err2 = 500
				end
			end
		end
	else
		
		B738DR_nd_vert_path = 0
		B738DR_gp_active = 0
		B738DR_vnav_alt_err = 500
		B738DR_vnav_alt_err2 = 500
		B738DR_vnav_vvi = 0
		ed_to_dist = 0
		
		pfd_gp_path = 0
		B738DR_gp_alt_err = 500
		B738DR_fac_xtrack = 999
		B738DR_gp_alt_err2 = 500
		last_ed_alt = -1
		
		vnav_pth_alt_old = 0
	end
	
	-- if B738DR_flight_phase < 5 or B738DR_flight_phase == 8 then
	if nd_vert_path_enable == false then
		B738DR_nd_vert_path = 0
		B738DR_gp_active = 0
		B738DR_vnav_alt_err = 500
		B738DR_vnav_alt_err2 = 500
		B738DR_vnav_vvi = 0
		ed_to_dist = 0
		
		pfd_gp_path = 0
		B738DR_gp_alt_err = 500
		B738DR_fac_xtrack = 999
		B738DR_gp_alt_err2 = 500
	end
	
	if B738DR_nd_vert_path == 0 then
		-- vnav_pth_alt_old = 0
		vnav_vvi_cur = simDR_vvi_fpm_pilot
		vnav_mode = -1
		ed_found_old = 0
	-- else
		-- vnav_pth_alt_old = B738DR_vnav_pth_alt
	end
	
	B738DR_vnav_pth_fpm = vnav_pth_fpm
	
	if B738DR_ian_not_install ~= 0 then
		pfd_gp_path = 0
		gp_pth_alt_old = 0
		gp_vvi_cur = simDR_vvi_fpm_pilot
		gp_mode = -1
		B738DR_gp_active = 0
		ian_available = 0
	elseif pfd_gp_path == 0 then
		gp_pth_alt_old = 0
		gp_vvi_cur = simDR_vvi_fpm_pilot
		gp_mode = -1
	end
	
	
	B738DR_vnav_app_active = vnav_app_active
	td_alt2_hold = td2_alt_hold_x
	ed_alt2_hold = ed2_alt2_hold_x
	
	
	-- CPT: pfd_id_scale(0, pfd_trk_path, B738DR_mmr_act_mode, B738DR_nav_tuned_type, ian_available, pfd_gp_path)
	-- FO : pfd_id_scale(1, pfd_trk_path, B738DR_mmr_act_mode_fo, B738DR_nav_tuned_type_fo, ian_available, pfd_gp_path)
	
	if first_time_mmr ~= 0 then
		local pfd_trk_path = 0
		if legs_num > 1 and perf_exec > 0 and B738DR_nps_disable == 0 then
			if math.abs(B738DR_xtrack) < 2 * B738DR_rnp or math.abs(B738DR_xtrack) < 1 then
				if B738DR_heading_mode == 1 or B738DR_heading_mode == 16 or B738DR_heading_mode == 17 or B738DR_heading_mode == 12 or B738DR_heading_mode == 10 then
					-- HDG
					pfd_trk_path = 1
				elseif (B738DR_heading_mode == 4 and B738DR_lnav_engaged ~= 0) or B738DR_heading_mode == 5 or B738DR_heading_mode == 6 or B738DR_heading_mode == 7 
				or B738DR_heading_mode == 8 or B738DR_heading_mode == 13 then
					-- LNAV
					pfd_trk_path = 1
				elseif B738DR_altitude_mode >= 8 and B738DR_altitude_mode <= 12 then
					-- TOGA
					pfd_trk_path = 1
				elseif (B738DR_altitude_mode == 5 and B738DR_vnav_engaged ~= 0) then
					-- VNAV
					pfd_trk_path = 1
				end
			end
		end
		
		pfd_id_scale(0, pfd_trk_path, B738DR_mmr_act_mode, B738DR_nav_tuned_type, ian_available, pfd_gp_path)
		pfd_id_scale(1, pfd_trk_path, B738DR_mmr_act_mode_fo, B738DR_nav_tuned_type_fo, ian_available, pfd_gp_path)
	end
	
end






-- CPT: pfd_id_scale(0, pfd_trk_path, B738DR_mmr_act_mode, B738DR_nav_tuned_type, ian_available, pfd_gp_path)
-- FO : pfd_id_scale(1, pfd_trk_path, B738DR_mmr_act_mode_fo, B738DR_nav_tuned_type_fo, ian_available, pfd_gp_path)
function pfd_id_scale(in_dspl, in_htrk, in_mmr_act_mode, in_nav_tuned_type, in_ian_available, in_pfd_gp_path)
	
	local ian_info_enable = 0
	local check_fac_gp = 0
	local fac_on_pfd = 0
	local gp_on_pfd = 0
	local within_app_crs = 0
	local ian_info = 0
	local flight_phase_cond = 0
	
	local pfd_mmr_active = 0
	
	local use_des_app = ""
	if rte12_act == 0 then
		use_des_app = des_app
	elseif rte12_act == 1 then
		use_des_app = des_app7
	end
	
	if in_mmr_act_mode == 1 then
		if in_nav_tuned_type == 2 then
			-- LOC
			if in_ian_available ~= 0 then
				check_fac_gp = 2
			end
			ian_info_enable = 1
		else
			-- ILS or UNKNOWN
			if B738DR_fms_ils_disable ~= 0 then
				if in_ian_available ~= 0 then
					check_fac_gp = 2
				end
				ian_info_enable = 1
			end
		end
	elseif in_mmr_act_mode == 2 then
		--B738DR_mmr_type
		-- GLS
		if B738DR_fms_ils_disable ~= 0 then
			if in_ian_available ~= 0 then
				check_fac_gp = 2
			end
			ian_info_enable = 1
		end
		if in_dspl == 0 then
			pfd_mmr_active = B738DR_mmr_active
		else
			pfd_mmr_active = B738DR_mmr_active_fo
		end
	else
		if in_ian_available ~= 0 then
			if use_des_app ~= "------" then
				if string.sub(use_des_app, 1, 1) == "I" then 	-- ILS
					check_fac_gp = 0
				elseif string.sub(use_des_app, 1, 1) == "L" then 	-- LOC
					check_fac_gp = 0
				elseif string.sub(use_des_app, 1, 1) == "B" then 	-- BCRS
					check_fac_gp = 0
				elseif string.sub(use_des_app, 1, 1) == "J" then 	-- GLS
					check_fac_gp = 0
				else
					check_fac_gp = 1
				end
			end
		end
		ian_info_enable = 1
	end
	
	if check_fac_gp ~= 0 then
		if in_pfd_gp_path ~= 0 then
			if rw_dist2 > 0 and rw_dist2 < 25 then
				if simDR_altitude_pilot <= B738DR_aerodrom_altitude + 6000 then
					within_app_crs = math.abs((((B738DR_fac_brg + 180) % 360) - ((B738DR_fac_trk + simDR_mag_variation + 180) % 360) + 360) % 360)
					if within_app_crs > 180 then
						within_app_crs = within_app_crs - 360
					end
					if within_app_crs <= 60 then
						if check_fac_gp == 1 then
							fac_on_pfd = 1
						end
						gp_on_pfd = 1
					end
				end
			end
		end
		
		if ian_info_enable ~= 0 then
			if B738DR_flight_phase == 2 or (B738DR_flight_phase > 3 and B738DR_flight_phase ~= 8) then
				flight_phase_cond = 1
			end
			
			if rw_dist2 > 0 and rw_dist2 <= 150 and flight_phase_cond ~= 0 then
				ian_info = 1
			elseif td_idx ~= 0 and B738DR_vnav_td_dist <= 50 and flight_phase_cond ~= 0 then
				ian_info = 1
			elseif B738DR_flight_phase > 4 and B738DR_flight_phase ~= 8 then
				if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
					if (rw_dist2 > 0 and rw_dist2 <= 150) or (td_idx ~= 0 and B738DR_vnav_td_dist <= 50) then
						ian_info = 1
					end
				else
					ian_info = 1
				end
			end
		end
	end
	
	local nd_fac_horizontal = 0
	local nd_pfd_gp_path = 0
	
	if fac_on_pfd == 0 then
		nd_fac_horizontal = 0
	else
		nd_fac_horizontal = 1
	end
	
	if gp_on_pfd == 0 then
		nd_pfd_gp_path = 0
	else
		nd_pfd_gp_path = 1
	end
	
	local pfd_crs = 0
	local pfd_app = 0
	local pfd_nav = 0
	local pfd_gs = 0
	local pfd_mode = 0
	local pfd_horz_path = 0
	local pfd_vert_path = 0
	local pfd_vert_path2 = 0
	local autopilot_pfd_mode = 0
	local pfd_loc_ghost = 0
	local pfd_gs_ghost = 0
	local pfd_fac_ghost = 0
	local pfd_gp_ghost = 0
	local pfd_gls_loc_ghost = 0
	local pfd_gls_gs_ghost = 0
	
	if in_mmr_act_mode >= 1 then
		if in_mmr_act_mode == 1 then
			-- ILS/LOC
			pfd_crs = ils_course
			if (B738DR_heading_mode == 3 or B738DR_heading_mode == 9) and simDR_nav_status == 2 then
				pfd_app = 2
				if B738DR_altitude_mode == 4 then
					pfd_gs = 2
				end
			end
			pfd_nav = simDR_nav_status
		else
			-- GLS
			pfd_crs = gls_course
			pfd_app = B738DR_gls_app_status
			pfd_nav = B738DR_gls_nav_status
			pfd_gs = B738DR_gls_gs_status
		end
		
		-- ILS/LOC/GLS tuned
		nd_fac_horizontal = 0
		
		if in_htrk == 0 then
			-- none LNAV TRK
			pfd_horz_path = 0
			pfd_vert_path = 0
			if loc_gp_engaged == 2 then
				autopilot_pfd_mode = 5 	-- LOC/GP
			elseif pfd_app == 2 then
				autopilot_pfd_mode = 1 	-- ILS
			elseif B738DR_fms_ils_disable == 0 then
				autopilot_pfd_mode = 1 	-- ILS
			else
				autopilot_pfd_mode = 5 	-- LOC/GP
				nd_pfd_gp_path = 1
			end
		else
			-- LNAV TRK
			pfd_horz_path = 1
			-- if ian_info == 0 then
				-- if loc_gp_engaged == 2 then
				if B738DR_gp_status == 2 then
					autopilot_pfd_mode = 5 	-- LOC/GP
					pfd_horz_path = 0
				-- elseif pfd_app_loc_gs == 2 then
				elseif pfd_gs == 2 then
					autopilot_pfd_mode = 1 	-- ILS
					pfd_horz_path = 0
				elseif pfd_app == 2 or pfd_nav == 2 or loc_gp_engaged == 2 then
					autopilot_pfd_mode = 3	-- LOC/VNAV
					pfd_horz_path = 0
					if B738DR_fms_ils_disable == 0 then
						if in_mmr_act_mode == 1 then
							pfd_gs_ghost = 1
						else
							pfd_gls_gs_ghost = 1
						end
						pfd_gp_ghost = 2
					else
						if in_mmr_act_mode == 1 then
							pfd_gs_ghost = 2
						else
							pfd_gls_gs_ghost = 2
						end
						pfd_gp_ghost = 1
					end
					if B738DR_nd_vert_path == 0 then
						pfd_vert_path2 = 1
					else
						pfd_vert_path = 1
					end
				elseif B738DR_fms_ils_disable == 0 then
					autopilot_pfd_mode = 2 	-- LNAV/VNAV
					if in_mmr_act_mode == 1 then
						pfd_loc_ghost = 1
					else
						if pfd_mmr_active == 0 then
							pfd_gls_loc_ghost = 2
						else
							pfd_gls_loc_ghost = 1
						end
					end
					within_app_crs = math.abs(simDR_ahars_mag_hdg - (pfd_crs + 360) % 360)
					if within_app_crs > 180 then
						within_app_crs = within_app_crs - 360
					end
					if within_app_crs <= 90 then
						if in_mmr_act_mode == 1 then
							pfd_gs_ghost = 1
						else
							if pfd_mmr_active == 0 then
								pfd_gls_gs_ghost = 2
							else
								pfd_gls_gs_ghost = 1
							end
						end
					else
						if in_mmr_act_mode == 1 then
							pfd_gs_ghost = 2
						else
							pfd_gls_gs_ghost = 2
						end
					end
					pfd_gp_ghost = 2
					if B738DR_nd_vert_path == 0 then
						pfd_vert_path2 = 1
					else
						pfd_vert_path = 1
					end
				else
					autopilot_pfd_mode = 2 	-- LNAV/VNAV
					if in_mmr_act_mode == 1 then
						pfd_loc_ghost = 1
					else
						if pfd_mmr_active == 0 then
							pfd_gls_loc_ghost = 2
						else
							pfd_gls_loc_ghost = 1
						end
					end
					within_app_crs = math.abs(simDR_ahars_mag_hdg - (pfd_crs + 360) % 360)
					if within_app_crs > 180 then
						within_app_crs = within_app_crs - 360
					end
					if within_app_crs <= 90 then
						pfd_gp_ghost = 1
					else
						pfd_gp_ghost = 2
					end
					if in_mmr_act_mode == 1 then
						pfd_gs_ghost = 2
					else
						pfd_gls_gs_ghost = 2
					end
					if B738DR_nd_vert_path == 0 then
						pfd_vert_path2 = 1
					else
						pfd_vert_path = 1
					end
				end
			-- else
			-- end
		end
		
		if in_mmr_act_mode == 1 then
			pfd_ils(in_dspl)
		else
			if autopilot_pfd_mode == 1 then
				autopilot_pfd_mode = 6	--GLS
			end
			pfd_gls(in_dspl)
		end
		
	else
		-- not tuned ILS/LOC/GLS
		if in_htrk == 0 then
			-- none LNAV TRK
			if ian_info == 0 then
				-- none
				autopilot_pfd_mode = 0
			else
				autopilot_pfd_mode = 4 	-- FMC -> FAC/GP
				pfd_rnav(in_dspl)
			end
		else
			-- LNAV TRK
			pfd_horz_path = 1
			if ian_info == 0 then
				if fac_gp_engaged == 1 then
					autopilot_pfd_mode = 4 	-- FMC -> FAC/GP
					pfd_horz_path = 0
					pfd_rnav(in_dspl)
				else
					autopilot_pfd_mode = 2 	-- LNAV/VNAV
					if B738DR_nd_vert_path == 0 then
						pfd_vert_path2 = 1
					else
						pfd_vert_path = 1
					end
					if in_dspl == 0 then
						pfd_cpt_nav_txt1 = ""
						pfd_cpt_nav_txt1a = ""
						pfd_cpt_nav_txt2 = ""
					else
						pfd_fo_nav_txt1 = ""
						pfd_fo_nav_txt1a = ""
						pfd_fo_nav_txt2 = ""
					end
				end
			else
				-- if fac_gp_engaged == 1 then
				if B738DR_gp_status == 2 then
					autopilot_pfd_mode = 4 	-- FMC -> FAC/GP
					pfd_horz_path = 0
				elseif fac_gp_engaged ~= 0 then
					autopilot_pfd_mode = 7 	-- FAC/VNAV
					pfd_horz_path = 0
					within_app_crs = math.abs(simDR_ahars_mag_hdg - (B738DR_fac_trk + simDR_mag_variation + 360) % 360)
					if within_app_crs > 180 then
						within_app_crs = within_app_crs - 360
					end
					if within_app_crs <= 90 then
						pfd_gp_ghost = 1
					else
						pfd_gp_ghost = 2
					end
					if B738DR_nd_vert_path == 0 then
						pfd_vert_path2 = 1
					else
						pfd_vert_path = 1
					end
				else
					pfd_fac_ghost = 1
					within_app_crs = math.abs(simDR_ahars_mag_hdg - (B738DR_fac_trk + simDR_mag_variation + 360) % 360)
					if within_app_crs > 180 then
						within_app_crs = within_app_crs - 360
					end
					if within_app_crs <= 90 then
						pfd_gp_ghost = 1
					else
						pfd_gp_ghost = 2
					end
					autopilot_pfd_mode = 2 	-- LNAV/VNAV
					if B738DR_nd_vert_path == 0 then
						pfd_vert_path2 = 1
					else
						pfd_vert_path = 1
					end
				end
				pfd_rnav(in_dspl)
			end
		end
	end
	
	
	
	
	if in_dspl == 0 then
		B738DR_ian_info = ian_info
		B738DR_nd_fac_horizontal = nd_fac_horizontal
		B738DR_pfd_gp_path = nd_pfd_gp_path
		B738DR_pfd_trk_path = pfd_horz_path
		B738DR_pfd_vert_path = pfd_vert_path
		B738DR_pfd_vert_path2 = pfd_vert_path2
		
		B738DR_pfd_loc_ghost = pfd_loc_ghost
		B738DR_pfd_gs_ghost = pfd_gs_ghost
		B738DR_pfd_fac_ghost = pfd_fac_ghost
		B738DR_pfd_gp_ghost = pfd_gp_ghost
		B738DR_pfd_gls_loc_ghost = pfd_gls_loc_ghost
		B738DR_pfd_gls_gs_ghost = pfd_gls_gs_ghost
		
		B738DR_autopilot_pfd_mode = autopilot_pfd_mode
		if autopilot_pfd_mode == 0 then
			pfd_cpt_nav_txt1 = ""
			pfd_cpt_nav_txt1a = ""
			pfd_cpt_nav_txt2 = ""
		end
	else
		B738DR_ian_info_fo = ian_info
		B738DR_nd_fac_horizontal_fo = nd_fac_horizontal
		B738DR_pfd_gp_path_fo = nd_pfd_gp_path
		B738DR_pfd_trk_path_fo = pfd_horz_path
		B738DR_pfd_vert_path_fo = pfd_vert_path
		B738DR_pfd_vert_path2_fo = pfd_vert_path2
		
		B738DR_pfd_loc_ghost_fo = pfd_loc_ghost
		B738DR_pfd_gs_ghost_fo = pfd_gs_ghost
		B738DR_pfd_fac_ghost_fo = pfd_fac_ghost
		B738DR_pfd_gp_ghost_fo = pfd_gp_ghost
		B738DR_pfd_gls_loc_ghost_fo = pfd_gls_loc_ghost
		B738DR_pfd_gls_gs_ghost_fo = pfd_gls_gs_ghost
		
		B738DR_autopilot_pfd_mode_fo = autopilot_pfd_mode
		if autopilot_pfd_mode == 0 then
			pfd_fo_nav_txt1 = ""
			pfd_fo_nav_txt1a = ""
			pfd_fo_nav_txt2 = ""
		end
	end
	
end

function after_lift_timer()
	after_lift_2s = 2
end

function fly_over_wpt(fly_wpt)
	
	local result = false
	local kk = legs_data[fly_wpt][32]
	
	if legs_data[fly_wpt][1] == "(VECTOR)" then
		result = true
	elseif fly_wpt == first_miss_app_idx - 1 then
		result = true
	else
		while kk > 9 do
			kk = kk - 10
		end
		if kk == 1 then
			result = true
		end
	end
	return result
end

function fly_over_wpt2(fly_wpt, in_rte)
	
	local result = false
	local kk = 0
	
	if in_rte == 0 then
		
		kk = legs_data2[fly_wpt][32]
		
		if legs_data2[fly_wpt][1] == "(VECTOR)" then
			result = true
		elseif fly_wpt == first_miss_app_idx_mod - 1 then
			result = true
		else
			while kk > 9 do
				kk = kk - 10
			end
			if kk == 1 then
				result = true
			end
		end
	elseif in_rte == 1 then
		
		kk = legs_data8[fly_wpt][32]
		
		if legs_data8[fly_wpt][1] == "(VECTOR)" then
			result = true
		elseif fly_wpt == first_miss_app_idx_mod2 - 1 then
			result = true
		else
			while kk > 9 do
				kk = kk - 10
			end
			if kk == 1 then
				result = true
			end
		end
	end
	return result
end


function rel_crs_turn(in_crs1, in_crs2, in_turn)
	local result = (in_crs2 - in_crs1 + 360) % 360
	if result > 180 then
		result = result - 360
	end
	if in_turn == 0 or in_turn == 2 then
		-- left
		if result > 0 then
			result = result - 360
		end
	else
		-- right
		if result < 0 then
			result = result + 360
		end
	end
	return result
end


function B738_fmc_calc()

	local ii = 0
	local jj = 0
	local nd_lat = simDR_latitude
	local nd_lon = simDR_longitude
	local nd_lat2 = 0
	local nd_lon2 = 0
	local nd_dis = 0
	local nd_x = 0
	local nd_y = 0
	local nd_hdg = 0
	local n = 0
	local dist_corr = 0
	local speed_corr = 0
	local speed = AP_airspeed	--simDR_airspeed_pilot
	local mag_hdg = simDR_ahars_mag_hdg - simDR_mag_variation
	local offset_from = 0
	local time_zulu = 0
	local tmp_wpt_eta2 = 0
	local tmp_wpt_eta3 = 0
	local tmp_wpt_eta4 = ""
	local distance = 0
	
	local relative_brg = 0
	local relative_brg2 = 0
	local relative_brg3 = 0
	local back_crs = 0
	local true_brg = 0
	local true_hdg = 0
	
	local dist_thrshld = 0
	local gnd_spd = simDR_ground_spd
	local offset_1 = 0
	local temp_txt = ""
	local next_rel_brg = 0
	local next_rel_brg2 = 0
	local next_wpt_enable = 0
	local max_thr = 0
	local lnav_disco = 0
	local hold_skip = 0
	
	local dta_bank_angle = 4
	local dta_time = 0
	local dta_radius_dist = 0
	local dta_radius = 0
	local dta_angle = 0
	local dta_rate_speed = 0
	local delta_radius = 0
	local delta_radius2 = 0
	
	local act_crs = 0
	local next_crs = 0
	local abs_next_rel_brg = 0
	
	local af_lat0 = 0
	local af_lon0 = 0
	
	local crs_circle = 0
	local crs_circle2 = 0
	local dist_circle = 0
	local prev_crs = 0
	
	local ext_calc = 0
	local use_precalc = 0
	local fms_turn_next = 0
	
	local lat_tmp = 0
	local lon_tmp = 0
	local lat_tmp2 = 0
	local lon_tmp2 = 0
	
	--local time_zulu = 0
	local time_temp = 0
	local time_temp2 = 0
	local speed_temp = 0
	local speed_temp_prev = 0
	local speed_temp1 = 0
	local speed_temp2 = 0
	local last_speed = 0
	local dist_temp = 0
	local wc_speed = 0
	local tmp_wind_dir = 0
	local tmp_wind_spd = 0
	local act_fuel = 0	--simDR_fuel_weight
	local calc_fuel = 0
	local calc_fuel2 = 0
	local fuel_flow = 0
	local ff_phase = 0
	
	local tc_calculated = 0
	local td_calculated = 0
	
	if simDR_eng_N2[0] > 50 or simDR_eng_N2[1] > 50 or was_on_air ~= 0 then
		act_fuel = simDR_fuel_weight
	else
		act_fuel = tonumber(plan_weight_kgs_mod)
		if act_fuel == nil then
			act_fuel = simDR_fuel_weight
		else
			act_fuel = act_fuel * 1000
		end
	end
	
	dist_dest = 0
	time_dest = ""
	dist_tc = 0
	time_tc = 0
	dist_td = 0
	time_td = 0
	dist_ed = 0
	time_ed = 0
	fuel_ed = 0
	fuel_tc = 0
	fuel_td = 0
	
	dist_dest_mod = 0
	time_dest_mod = ""
	dist_tc_mod = 0
	time_tc_mod = 0
	dist_td_mod = 0
	time_td_mod = 0
	dist_ed_mod = 0
	time_ed_mod = 0
	fuel_ed_mod = 0
	fuel_tc_mod = 0
	fuel_td_mod = 0
	
	local use_legs_num2 = 0
	if rte12_act == 0 then
		if ref_icao == "----" or des_icao == "****" then
			legs_num = 0
		end
		use_legs_num2 = legs_num2
	elseif rte12_act == 1 then
		if ref_icao7 == "----" or des_icao7 == "****" then
			legs_num = 0
		end
		use_legs_num2 = legs_num8
	end
	
	if calc_rte_enable == 0 then
		if legs_num > legs_num_old then
			if nav_mode == 1 or nav_mode == 99 then	--cancel NAV to Dest ICAO
				nav_mode = 0
				offset = offset + 1
			end
		end
		legs_num_old = legs_num
		-----------------------------
		B738DR_fms_test1 = 40001
		
		if offset > 0 and offset <= legs_num + 1 then
			
			if legs_num > 1 and use_legs_num2 > 1 then
				if offset < 2 then
					offset = 2
					offset_seq = offset
				end
				if offset == 3 and in_flight_mode == 0 then
					if legs_data[offset-1][19] ~= 1 or string.sub(legs_data[offset-1][1], 1, 2) ~= "RW" then
						offset = 2
					end
				elseif offset == 2 then
					if legs_data[offset][19] == 1 and string.sub(legs_data[offset][1], 1, 2) == "RW" then
						offset = offset + 1
						offset_seq = offset
						nav_mode = 0
						if offset > legs_num then
							nav_mode = 99
						end
					end
				end
				
				B738DR_fpln_active = 1
				B738DR_fpln_active_fo = 1
				if nav_mode == 99 then
					temp_txt = "-----"
				else
					temp_txt = legs_data[offset][1]
				end
				if string.len(temp_txt) > 6 then
					temp_txt = string.sub(temp_txt, 1, 6)
				end
				if temp_txt == "DISCON" then
					B738DR_fpln_active = 0
					B738DR_fpln_active_fo = 0
					B738DR_fpln_nav_id = ""
					if offset < legs_num then
						temp_txt = legs_data[offset][1]
						if string.len(temp_txt) > 6 then
							temp_txt = string.sub(temp_txt, 1, 6)
						end
						B738DR_fpln_nav_id = temp_txt
						B738DR_fpln_active = 1
						B738DR_fpln_active_fo = 1
					end
				else
					B738DR_fpln_nav_id = temp_txt
				end
			else
				B738DR_fpln_active = 0
				B738DR_fpln_active_fo = 0
				B738DR_fpln_nav_id = ""
			end
			
			B738DR_fms_test1 = 40002
			
			if nav_mode == 99 then
				tmp_wpt_eta4 = ""
				B738DR_fms_id_dist = 0
			else
				if legs_num > 0 then
					time_zulu = legs_data[offset][13]
				else
					time_zulu = 0
				end
				if time_zulu > 0 then
					tmp_wpt_eta4 = time_to_string2(time_zulu)
				else
					tmp_wpt_eta4 = string.format("%02d", simDR_zulu_hours) .. string.format("%02d", simDR_zulu_minutes)
					tmp_wpt_eta4 = tmp_wpt_eta4 .. "." .. string.format("%1d", simDR_zulu_seconds/60)
				end
				B738DR_fms_id_dist = nd_calc_dist2(simDR_latitude, simDR_longitude, legs_data[offset][7], legs_data[offset][8])
			end
			B738DR_fms_id_eta = tmp_wpt_eta4
			
			B738DR_fms_test1 = 40003
		else
			B738DR_fpln_nav_id = ""
			B738DR_fms_id_eta = ""
			B738DR_fms_id_dist = 0
			B738DR_fpln_active = 0
			B738DR_fpln_active_fo = 0
		end
		
		
		
	end
	
	
	B738DR_fms_test1 = 40004
	
	skip_hold = 0
	
	if ffirst_time2 ~= 0 then
		if B738DR_irs_left_mode ~= 2 and B738DR_irs_right_mode ~= 2 then
			lnav_disco = 1
			B738DR_lnav_disconnect = 1
			B738DR_vnav_disconnect = 1
			if B738DR_fms_test == 55 then
				dump_leg_append("\n")
				dump_leg_append("LNAV/VNAV disconnect: \n")
				dump_leg_append("B738DR_irs_left_mode: " .. tostring(B738DR_irs_left_mode) .. " \n")
				dump_leg_append("B738DR_irs_right_mode: " .. tostring(B738DR_irs_right_mode) .. " \n")
			end
			if offset > legs_num then
				offset = legs_num
				offset_seq = offset
			end
			if legs_num > 1 then
				if offset < 2 then
					offset = 2
					offset_seq = offset
				end
				if offset == 3 and in_flight_mode == 0 then
					if legs_data[offset-1][19] ~= 1 or string.sub(legs_data[offset-1][1], 1, 2) ~= "RW" then
						offset = 2
					end
				elseif offset == 2 then
					if legs_data[offset][19] == 1 and string.sub(legs_data[offset][1], 1, 2) == "RW" then
						offset = offset + 1
						offset_seq = offset
						nav_mode = 0
						if offset > legs_num then
							nav_mode = 99
						end
					end
				end
			end
			B738DR_fpln_nav_id = ""
			B738DR_fms_id_eta = ""
			B738DR_fms_id_dist = 0
			B738DR_fpln_active = 0
			B738DR_fpln_active_fo = 0
		end
	end
	
	B738DR_fms_test1 = 40005
	
	if legs_num > 1 and lnav_disco == 0 then
		
		if offset < 2 then
			offset = 2
			nav_mode = 0
			offset_seq = offset
		elseif offset == 3 and legs_num > 2 and in_flight_mode == 0 then
			if legs_data[offset-1][19] ~= 1 or string.sub(legs_data[offset-1][1], 1, 2) ~= "RW" then
				offset = 2
				nav_mode = 0
				offset_seq = offset
			end
		end
		
		--
		if offset == 2 then
			if legs_data[offset][19] == 1 and string.sub(legs_data[offset][1], 1, 2) == "RW" then
				offset = offset + 1
				offset_seq = offset
				nav_mode = 0
				if offset > legs_num then
					nav_mode = 99
				end
			end
		end
		---
		
		B738DR_fms_test1 = 40006
		
		if nav_mode == 99 then
			if (B738DR_heading_mode > 3 and B738DR_heading_mode < 7) or B738DR_heading_mode == 8 or B738DR_heading_mode == 13 then
				if ffirst_time2 ~= 0 then
					B738DR_lnav_disconnect = 1
					add_fmc_msg(END_OF_ROUTE, 2)
					B738DR_vnav_disconnect = 1
				end
				if B738DR_fms_test == 55 then
					dump_leg_append("\n")
					dump_leg_append("LNAV/VNAV disconnect: \n")
					dump_leg_append("nav_mode: " .. tostring(nav_mode) .. " \n")
				end
			end
			use_precalc = 0
			
			if offset_act >= 5 then
				offset_act = 0
				offset_start = 0
				offset_end = 0
				offset_dist = 0
				offset_side = 0
			end
			if offset_act_mod >= 5 then
				offset_start_def_mod = 0
				offset_end_def_mod = 0
				offset_act_mod = 0
				offset_start_mod = 0
				offset_end_mod = 0
				offset_dist_mod = 0
				offset_side_mod = 0
			end
		end
		
		B738DR_fms_test1 = 40007
		
		if B738DR_lnav_engaged == 0 then
			if nav_mode ~= 0 and nav_mode ~= 99 then
				B738DR_fmc_trk_turn = -1
				B738DR_fmc_trk_turn2 = -1
				turn_off_turn = 1
				block_df = 0
				turn_radii_used = 0
			end
		end
		
		local distance_target = 0
		
		if skip_hold == 1 then
			nav_mode = 50
			turn_radii_used = 0
		end
		
		B738DR_fms_test1 = 40008
		
		
		if nav_mode == 0 then
			
			local offset_end_tmp = offset_end
			if offset_end_tmp == 0 then
				offset_end_tmp = offset_end_def
			end
			
			-- -- if offset_act >= 5 and offset > offset_end_tmp then
			if offset_act >= 5 and offset_seq > offset_end_tmp then
				-- last_lat = simDR_latitude
				-- last_lon = simDR_longitude
				-- legs_intdir_act = 1	-- direct to wpt
				-- legs_intdir_crs = -1
				-- -- legs_data2[offset][31] = "DF"
				-- calc_rte_enable2 = 1
				offset_act = 0
				offset_start = 0
				offset_end = 0
				offset_dist = 0
				offset_side = 0
				if offset_act_mod >= 5 then
					offset_start_def_mod = 0
					offset_end_def_mod = 0
					offset_act_mod = 0
					offset_start_mod = 0
					offset_end_mod = 0
					offset_dist_mod = 0
					offset_side_mod = 0
				end
			-- -- elseif offset_act >= 5 and offset == offset_end_tmp and legs_intdir_act == 0 then
				-- -- if B738DR_rt_abeam_dist_bp < math.abs(legs_data[offset][45]) then
					-- -- last_lat = simDR_latitude
					-- -- last_lon = simDR_longitude
					-- -- legs_intdir_act = 1	-- direct to wpt
					-- -- legs_intdir_crs = -1
					-- -- -- legs_data2[offset][31] = "DF"
					-- -- calc_rte_enable2 = 1
					-- -- offset_act = 0
					-- -- offset_start = 0
					-- -- offset_end = 0
					-- -- offset_dist = 0
					-- -- offset_side = 0
					-- -- offset_start_def_mod = 0
					-- -- offset_end_def_mod = 0
					-- -- offset_act_mod = 0
					-- -- offset_start_mod = 0
					-- -- offset_end_mod = 0
					-- -- offset_dist_mod = 0
					-- -- offset_side_mod = 0
				-- -- end
			end
			
			-- if offset_seq > 1 then
				-- if legs_data[offset_seq-1][1] == "(VECTOR)" and legs_intdir_act == 0 then
					-- legs_intdir_act = 1
					-- legs_intdir_crs = -1
					-- block_df = 0
				-- end
			-- end
			
			if legs_data[offset_seq][31] == "DF" or legs_intdir_act ~= 0 then
				B738DR_fmc_bank_angle = 6
			elseif legs_data[offset_seq][31] == "RF" or legs_data[offset_seq][31] == "AF" then
				B738DR_fmc_bank_angle = 6
			end
			
			B738DR_radii_turn_act = 0
			B738DR_xtrack2 = 999
			
			-- navigation to next waypoint
			-- nd_lat2 = legs_data[offset_seq][7]
			-- nd_lon2 = legs_data[offset_seq][8]
			nd_lat2, nd_lon2 = lat_lon_legs(offset_seq)
				
			B738DR_fmc_trk_turn = legs_data[offset_seq][21]
			
			-- if legs_intdir_act ~= 0 then
				-- nd_dis = nd_calc_dist(nd_lat, nd_lon, nd_lat2, nd_lon2)
			-- elseif legs_data[offset_seq][31] == "TF" or legs_data[offset_seq][31] == "IF" or legs_data[offset_seq][31] == "" then
				-- nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
			-- else
				nd_dis = nd_calc_dist(nd_lat, nd_lon, nd_lat2, nd_lon2)
			-- end
			
			-- if legs_intdir_act == 3 and (B738DR_lnav_engaged == 0 or B738DR_ap_on == 0) then
				-- B738DR_fmc_trk = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
				-- legs_intdir_trk = B738DR_fmc_trk
				
				-- legs_intdir_lat = nd_lat
				-- legs_intdir_lon =  nd_lon
				-- legs_intdir_init_trk = B738DR_fmc_trk
				
				-- legs_intdir_radius = 0
				-- legs_intdir_rad_lat = 0
				-- legs_intdir_rad_lon = 0
				-- legs_intdir_turn = -1
				-- legs_intdir_end_lat = 0
				-- legs_intdir_end_lon = 0
				
				-- legs_intdir_act = 2
				-- offset_seq = offset
				-- nav_mode = 0
				
				-- legs_intdir_direct = 0
			-- end
			
			local trk_act = 0
			local trk_init = 0
			local trk_final = 0
			local trk_delta = 0
			
			local dir_end_lat = nd_lat2
			local dir_end_lon = nd_lon2
			
			-- if legs_intdir_act == 0 then
			if legs_intdir_act == 2 and legs_intdir_idx == offset and offset == offset_seq then
				B738DR_fmc_trk_turn = -1
				B738DR_fmc_trk_turn2 = -1
				turn_off_turn = 0
				-- B738DR_fmc_trk = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
				
				if legs_intdir_crs == -1 then
					if B738DR_fms_legs_df_crs[offset_seq-1] == -1 then
						-- B738DR_fmc_trk = B738DR_fms_legs_df_crs[offset_seq-1]
						B738DR_fmc_trk = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
					else
						-- B738DR_fmc_trk = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
						if B738DR_fms_dir_seg3_end_lat == 0 and B738DR_fms_dir_seg3_end_lon == 0 then
							if B738DR_fms_dir_seg1_end_lat ~= 0 or B738DR_fms_dir_seg1_end_lon ~= 0 then
								dir_end_lat = B738DR_fms_dir_seg1_end_lat
								dir_end_lon = B738DR_fms_dir_seg1_end_lon
							else
								dir_end_lat = B738DR_fms_dir_seg1_start_lat
								dir_end_lon = B738DR_fms_dir_seg1_start_lon
							end
						else
							dir_end_lat = B738DR_fms_dir_seg3_end_lat
							dir_end_lon = B738DR_fms_dir_seg3_end_lon
						end
						
						local dir_dist = nd_calc_dist(dir_end_lat, dir_end_lon, legs_data[offset_seq][7], legs_data[offset_seq][8])
						if nd_dis < dir_dist then
							trk_init = B738DR_fms_legs_df_crs[offset_seq-1]
							trk_final = B738DR_fms_legs_df_crs_fin[offset_seq-1]
							trk_delta = (trk_final - trk_init + 360) % 360
							if trk_delta > 180 then
								trk_delta = trk_delta - 360
							end
							if nd_dis <= 0 then
								trk_act = trk_delta
							elseif nd_dis >= legs_data[offset_seq][3] then
								trk_act = 0
							else
								trk_act = B738_rescale(dir_dist, 0, 0, trk_delta, nd_dis)
							end
							B738DR_fmc_trk = (trk_init + trk_act + 360) % 360
						else
							B738DR_fmc_trk = B738DR_fms_legs_df_crs[offset_seq-1]
						end
					end
				else
					B738DR_fmc_trk = ((legs_intdir_crs - simDR_mag_variation) + 360) % 360
				end
				
			else
				if string.sub(legs_data[offset_seq][31], 1, 1) == "C" or string.sub(legs_data[offset_seq][31], 1, 1) == "V" then
					B738DR_fmc_trk = ((math.deg(legs_data[offset_seq][18]) - simDR_mag_variation) + 360) % 360
				elseif legs_data[offset_seq][31] == "TF" or legs_data[offset_seq][31] == "IF" or legs_data[offset_seq][31] == "" then
					if nd_dis < legs_data[offset_seq][3] then
						trk_init = math.deg(legs_data[offset_seq][2])
						trk_final = math.deg(legs_data[offset_seq][37])
						trk_delta = (trk_final - trk_init + 360) % 360
						if trk_delta > 180 then
							trk_delta = trk_delta - 360
						end
						-- local trk_act = 0
						if nd_dis <= 0 then
							trk_act = trk_delta
						elseif nd_dis >= legs_data[offset_seq][3] then
							trk_act = 0
						else
							trk_act = B738_rescale(legs_data[offset_seq][3], 0, 0, trk_delta, nd_dis)
						end
						B738DR_fmc_trk = (trk_init + trk_act + 360) % 360
						
					else
						B738DR_fmc_trk = math.deg(legs_data[offset_seq][2])
					end
					
				elseif legs_data[offset_seq][31] == "RF" or legs_data[offset_seq][31] == "AF" then
					if string.sub(legs_data[offset_seq+1][31], 1, 1) == "C" or string.sub(legs_data[offset_seq+1][31], 1, 1) == "V" then
						B738DR_fmc_trk = ((math.deg(legs_data[offset_seq+1][18]) - simDR_mag_variation) + 360) % 360
					else
						B738DR_fmc_trk = math.deg(legs_data[offset_seq+1][2])
					end
				elseif legs_data[offset_seq][31] == "DF" then
					-- B738DR_fmc_trk = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
					
					-- B738DR_fmc_trk = B738DR_fms_legs_df_crs[offset_seq-1]
					
					if B738DR_fms_legs_df_crs[offset_seq-1] == -1 then
						B738DR_fmc_trk = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
					else
						if B738DR_fms_legs_seg3_end_lat[offset_seq-1] == 0 and B738DR_fms_legs_seg3_end_lon[offset_seq-1] == 0 then
							if B738DR_fms_legs_seg1_end_lat[offset_seq-1] ~= 0 or B738DR_fms_legs_seg1_end_lon[offset_seq-1] ~= 0 then
								dir_end_lat = B738DR_fms_legs_seg1_end_lat[offset_seq-1]
								dir_end_lon = B738DR_fms_legs_seg1_end_lon[offset_seq-1]
							else
								dir_end_lat = B738DR_fms_legs_seg1_start_lat[offset_seq-1]
								dir_end_lon = B738DR_fms_legs_seg1_start_lon[offset_seq-1]
							end
						else
							dir_end_lat = B738DR_fms_legs_seg3_end_lat[offset_seq-1]
							dir_end_lon = B738DR_fms_legs_seg3_end_lon[offset_seq-1]
						end
						local dir_dist = nd_calc_dist(dir_end_lat, dir_end_lon, legs_data[offset_seq][7], legs_data[offset_seq][8])
						if nd_dis < dir_dist then
							trk_init = B738DR_fms_legs_df_crs[offset_seq-1]
							trk_final = B738DR_fms_legs_df_crs_fin[offset_seq-1]
							trk_delta = (trk_final - trk_init + 360) % 360
							if trk_delta > 180 then
								trk_delta = trk_delta - 360
							end
							-- local trk_act = 0
							if nd_dis <= 0 then
								trk_act = trk_delta
							elseif nd_dis >= legs_data[offset_seq][3] then
								trk_act = 0
							else
								trk_act = B738_rescale(dir_dist, 0, 0, trk_delta, nd_dis)
							end
							B738DR_fmc_trk = (trk_init + trk_act + 360) % 360
						else
							B738DR_fmc_trk = B738DR_fms_legs_df_crs[offset_seq-1]
						end
					end
					
				else
					B738DR_fmc_trk = math.deg(legs_data[offset_seq][2])
				end
				B738DR_wpt_path = legs_data[offset_seq][31]
			end
			legs_intdir_trk2_mem = -1
			
			
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
			
			B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			-- calculate rel hdg to wpt
			if string.sub(legs_data[offset_seq][31], 1, 1) == "C" or string.sub(legs_data[offset_seq][31], 1, 1) == "V" then
				relative_brg = (B738DR_fmc_crs - simDR_ahars_mag_hdg + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				if relative_brg > -85 and relative_brg < 85 then
					B738DR_fmc_trk_turn = -1
				end
			end
			
			if turn_off_turn == 1 then
				B738DR_fmc_trk_turn = -1
				B738DR_fmc_trk_turn2 = -1
			end
			
			B738DR_xtrack = B738DR_rt_xtrack
			
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			
			turn_radii_used = 0
			last_dta = 0
			turn_radii_used = 0
			radii_begin_crs = 0
			radii_end_crs = 0
			radii_end_angle = 0
			radii_remain_dist = 0
			
			if offset_act >= 5 and offset_act <= 7 and legs_data[offset_seq][45] ~= 0 then
				B738DR_xtrack = B738DR_xtrack - legs_data[offset_seq][45]
				if B738DR_xtrack > 999 then
					B738DR_xtrack = 999
				elseif B738DR_xtrack < -999 then
					B738DR_xtrack = -999
				end
			end
			B738DR_fmc_trk3 = -1
			hold_radius = 0
			pi_radius = 0
			
		elseif nav_mode == 6 then
			
			if legs_intdir_act == 2 and legs_intdir_idx == offset then
				B738DR_fmc_trk = B738DR_fms_dir_seg2_crs_true
				nd_hdg = nd_calc_brg(nd_lat, nd_lon, B738DR_fms_dir_seg2_end_lat, B738DR_fms_dir_seg2_end_lon)
			elseif offset_seq_prev - 1 >= 0 then
				B738DR_fmc_trk = B738DR_fms_legs_seg2_crs_true[offset_seq_prev-1]
				nd_hdg = nd_calc_brg(nd_lat, nd_lon, B738DR_fms_legs_seg2_end_lat[offset_seq_prev-1], B738DR_fms_legs_seg2_end_lon[offset_seq_prev-1])
			end
			
			B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			B738DR_fmc_trk_turn = -1
			B738DR_fmc_trk_turn2 = -1
			
			B738DR_xtrack = B738DR_rt_xtrack
			
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			
			B738DR_wpt_path = legs_data[offset_seq][31]
			
			if offset_act >= 5 and offset_act <= 7 and legs_data[offset_seq][45] ~= 0 then
				B738DR_xtrack = B738DR_xtrack - legs_data[offset_seq][45]
				if B738DR_xtrack > 999 then
					B738DR_xtrack = 999
				elseif B738DR_xtrack < -999 then
					B738DR_xtrack = -999
				end
			end
			B738DR_fmc_trk3 = -1
			hold_radius = 0
			pi_radius = 0
			
			
		elseif nav_mode == 1 then
			
			B738DR_wpt_path = "DF"
			
			B738DR_fmc_trk = 0;		-- ???????????????
			
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, B738DR_fms_legs_lat[offset_seq-1], B738DR_fms_legs_lon[offset_seq-1])
			B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			B738DR_fmc_trk_turn = -1
			B738DR_fmc_trk_turn2 = -1
			
			B738DR_xtrack = B738DR_rt_xtrack
			
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			
			B738DR_wpt_path = legs_data[offset_seq][31]
			
			if offset_act >= 5 and offset_act <= 7 and legs_data[offset_seq][45] ~= 0 then
				B738DR_xtrack = B738DR_xtrack - legs_data[offset_seq][45]
				if B738DR_xtrack > 999 then
					B738DR_xtrack = 999
				elseif B738DR_xtrack < -999 then
					B738DR_xtrack = -999
				end
			end
			B738DR_fmc_trk3 = -1
			hold_radius = 0
			pi_radius = 0
			
			
			
			
		elseif nav_mode == 2 then
			
			-- VECTOR
			B738DR_wpt_path = legs_data[offset_seq][31]
			B738DR_radii_turn_act = 0
			-- nd_lat2 = legs_data[offset_seq][7]
			-- nd_lon2 = legs_data[offset_seq][8]
			nd_lat2, nd_lon2 = lat_lon_legs(offset_seq)
			
			-- if B738DR_wpt_path == "VM" or B738DR_wpt_path == "FM" then
				nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
				B738DR_fmc_trk = ((math.deg(legs_data[offset_seq][18]) - simDR_mag_variation) + 360) % 360
			-- else
				-- nd_dis = 999
			-- end
			B738DR_fmc_dist = nd_dis
			B738DR_fmc_dist2 = nd_dis
			B738DR_fmc_trk3 = -1
			last_dta = 0
			
			B738DR_xtrack = 0
			
			hold_radius = 0
			pi_radius = 0
			
		elseif nav_mode == 3 then
			
			---------------------------------------------
			
			if B738DR_hold_phase == 3 then
				if hold_circuit < 1 then
					hold_circuit = 1
				end
			else
				if B738DR_hold_phase == 0 and hold_circuit ~= 0 then
					hold_circuit = 1
				else
					hold_circuit = 0
				end
			end
		
			B738DR_xtrack = B738DR_rt_xtrack
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp
			B738DR_wpt_path = legs_data[offset_seq][31]
			if legs_data[offset_seq][31] == "HF" then
				if hold_circuit > 0 then
					-- HOLD single circuit termination
					B738DR_hold_term = 2
				end
			end
			
			B738DR_fmc_trk3 = -1
			
		elseif nav_mode == 4 then
			
			
			B738DR_fmc_bank_angle = 6
			B738DR_radii_turn_act = 0
			B738DR_radii_turn = af_dist
			
			af_lat = legs_data[offset_seq][23]
			af_lon = legs_data[offset_seq][24]
			
			if legs_data[offset_seq][31] == "RF" then
				af_dist = tonumber(legs_data[offset_seq][33]) / 1000
			else
				af_dist = tonumber(legs_data[offset_seq][22]) / 10
			end
			
			B738DR_xtrack = B738DR_rt_xtrack
			if B738DR_xtrack > 99 then
				B738DR_xtrack = 999
			end
			if B738DR_xtrack < -99 then
				B738DR_xtrack = -999
			end
			
			B738DR_fmc_trk_turn = -1	--af_turn
			B738DR_fmc_trk_turn2 = legs_data[offset_seq][21]
			B738DR_wpt_path = legs_data[offset_seq][31]
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, af_lat, af_lon)
			relative_brg = (nd_hdg + 180) % 360
			
			if legs_data[offset_seq][21] == 2 then
				-- left
				nd_hdg = (nd_hdg + 90) % 360
			else
				-- right
				nd_hdg = (nd_hdg + 270) % 360
			end
			B738DR_fmc_trk = nd_hdg
			B738DR_fmc_trk2 = (nd_hdg + simDR_mag_variation + 360) % 360
			
			-- calculate next waypoint
			-- nd_lat2 = legs_data[offset_seq][7]
			-- nd_lon2 = legs_data[offset_seq][8]
			nd_lat2, nd_lon2 = lat_lon_legs(offset_seq)
				
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
			
			B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp
			
			B738DR_xtrack2 = 999
			B738DR_radii_final_crs = -1
			B738DR_fmc_trk3 = -1
			
			hold_radius = 0
			pi_radius = 0
			
			
		elseif nav_mode == 5 then
			
			
			--------------------------------------------------------------------------------------------
			
			radii_remain_dist = 0
			radii_add_dist = 0
			turn_radii_used = 0
			B738DR_radii_turn_act = 1
			last_dta = 0	--B738DR_fms_legs_radii_dta[offset_seq-2]
			
			--------------------------------------------------------------------------------------------
			if legs_intdir_act == 2 and legs_intdir_idx == offset then
				-- radii_turn_dir = -1
				radii_lat = B738DR_fms_dir_seg1_ctr_lat
				radii_lon = B738DR_fms_dir_seg1_ctr_lon
				radii_dist = B738DR_fms_dir_seg1_radius
				radii_turn_dir = B738DR_fms_dir_seg1_turn
			elseif offset_seq_prev - 1 >= 0 then
				radii_lat = B738DR_fms_legs_radii_ctr_lat[offset_seq_prev-1]
				radii_lon = B738DR_fms_legs_radii_ctr_lon[offset_seq_prev-1]
				radii_dist = B738DR_fms_legs_radii_radius[offset_seq_prev-1]
				radii_turn_dir = B738DR_fms_legs_turn[offset_seq_prev-1]
			end
			
			-- calculate Radii distance
			-- nd_dis = nd_calc_dist2(nd_lat, nd_lon, radii_lat, radii_lon)
			
			B738DR_radii_turn = radii_dist
			
			B738DR_fmc_trk_turn2 = radii_turn_dir
			
			B738DR_xtrack = B738DR_rt_xtrack
			
			if offset_act >= 5 and offset_act <= 7 and legs_data[offset_seq][45] ~= 0 then
				B738DR_xtrack = B738DR_xtrack - legs_data[offset_seq][45]
				if B738DR_xtrack > 999 then
					B738DR_xtrack = 999
				elseif B738DR_xtrack < -999 then
					B738DR_xtrack = -999
				end
				nav_mode = 0
				turn_radii_used = 0
				radii_add_dist = 0
			end
			
			-- calculate next waypoint
			-- nd_lat2 = legs_data[offset_seq][7]
			-- nd_lon2 = legs_data[offset_seq][8]
			nd_lat2, nd_lon2 = lat_lon_legs(offset_seq)
				
			B738DR_fmc_trk_turn = -1 --radii_turn_dir	--legs_data[offset_seq][21]
			B738DR_wpt_path = legs_data[offset_seq][31]
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, radii_lat, radii_lon)
			
			local nd_hdg_ctr = (nd_hdg + 180) % 360
			
			if radii_turn_dir == 2 then
				-- left
				nd_hdg = (nd_hdg + 90) % 360
			else
				-- right
				nd_hdg = (nd_hdg + 270) % 360
			end
			B738DR_fmc_trk = nd_hdg
			B738DR_fmc_trk2 = (nd_hdg + simDR_mag_variation + 360) % 360
			
			
			-- nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
			
			true_brg = (B738DR_fmc_trk + simDR_mag_variation + 360) % 360
			true_hdg = simDR_mag_hdg
			
			B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp	--nd_dis
			
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			B738DR_xtrack2 = 999
			
			
			hold_radius = 0
			pi_radius = 0
			
		elseif nav_mode == 9 then
			
			
			--------------------------------------------------------------------------------------------
			
			if legs_intdir_act == 2 and legs_intdir_idx == offset then
				-- radii_turn_dir = -1
				radii_lat = B738DR_fms_dir_seg3_ctr_lat
				radii_lon = B738DR_fms_dir_seg3_ctr_lon
				radii_dist = B738DR_fms_dir_seg3_radius
				radii_turn_dir = B738DR_fms_dir_seg3_turn
			else
				radii_lat = B738DR_fms_legs_seg3_ctr_lat[offset_seq_prev-1]
				radii_lon = B738DR_fms_legs_seg3_ctr_lon[offset_seq_prev-1]
				radii_dist = B738DR_fms_legs_seg3_radius[offset_seq_prev-1]
				radii_turn_dir = B738DR_fms_legs_seg3_turn[offset_seq_prev-1]
			end
			
			
			radii_remain_dist = 0
			radii_add_dist = 0
			turn_radii_used = 0
			B738DR_radii_turn_act = 1
			last_dta = 0	--B738DR_fms_legs_radii_dta[offset_seq-2]
			
			--------------------------------------------------------------------------------------------
			
			-- calculate Radii distance
			-- nd_dis = nd_calc_dist2(nd_lat, nd_lon, radii_lat, radii_lon)
			
			B738DR_radii_turn = radii_dist
			
			B738DR_xtrack = B738DR_rt_xtrack
			
			if offset_act >= 5 and offset_act <= 7 and legs_data[offset_seq][45] ~= 0 then
				B738DR_xtrack = B738DR_xtrack - legs_data[offset_seq][45]
				if B738DR_xtrack > 999 then
					B738DR_xtrack = 999
				elseif B738DR_xtrack < -999 then
					B738DR_xtrack = -999
				end
				nav_mode = 0
				turn_radii_used = 0
				radii_add_dist = 0
			end
			B738DR_fmc_trk_turn2 = radii_turn_dir
			
			-- calculate next waypoint
			-- nd_lat2 = legs_data[offset_seq][7]
			-- nd_lon2 = legs_data[offset_seq][8]
			nd_lat2, nd_lon2 = lat_lon_legs(offset_seq)
				
			B738DR_fmc_trk_turn = -1 --radii_turn_dir	--legs_data[offset_seq][21]
			B738DR_wpt_path = legs_data[offset_seq][31]
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, radii_lat, radii_lon)
			
			local nd_hdg_ctr = (nd_hdg + 180) % 360
			
			if radii_turn_dir == 2 then
				-- left
				nd_hdg = (nd_hdg + 90) % 360
			else
				-- right
				nd_hdg = (nd_hdg + 270) % 360
			end
			B738DR_fmc_trk = nd_hdg
			B738DR_fmc_trk2 = (nd_hdg + simDR_mag_variation + 360) % 360
			
			
			-- nd_dis = nd_calc_dist2(nd_lat, nd_lon, nd_lat2, nd_lon2)
			nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
			
			true_brg = (B738DR_fmc_trk + simDR_mag_variation + 360) % 360
			true_hdg = simDR_mag_hdg
			
			B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp	--nd_dis
			
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp	--B738DR_rt_abeam_dist
			B738DR_xtrack2 = 999
			
			
			hold_radius = 0
			pi_radius = 0
			
		elseif nav_mode == 7 then
			
			B738DR_fmc_trk_turn = -1
			
			-- nd_hdg = nd_calc_brg(nd_lat, nd_lon, nd_lat2, nd_lon2)
			-- B738DR_fmc_crs = (nd_hdg + simDR_mag_variation + 360) % 360
			
			B738DR_xtrack = B738DR_rt_xtrack
			B738DR_fmc_dist = B738DR_rt_abeam_dist_bp
			B738DR_fmc_dist2 = B738DR_rt_abeam_dist_bp
			B738DR_xtrack2 = 999
			hold_radius = 0
		else
			hold_radius = 0
			pi_radius = 0
		end
		
		if nav_mode ~= 3 then
			hold_circuit = 0
		end
		
		
		B738DR_fms_test1 = 40009
		
		-- Detect Missed Approach
		if first_miss_app_idx > 0 and offset >= first_miss_app_idx then
			if B738DR_missed_app_act == 0 then
				B738DR_missed_app_act = 2
				B738DR_vnav_disconnect = 1
			end
		end
		
		B738DR_fms_test1 = 40010
		
		-- local time_zulu = 0
		-- local time_temp = 0
		-- local time_temp2 = 0
		-- local speed_temp = 0
		-- local speed_temp_prev = 0
		-- local speed_temp1 = 0
		-- local speed_temp2 = 0
		-- local last_speed = 0
		-- local dist_temp = 0
		-- local wc_speed = 0
		-- local tmp_wind_dir = 0
		-- local tmp_wind_spd = 0
		-- local act_fuel = simDR_fuel_weight
		-- local calc_fuel = 0
		-- local calc_fuel2 = 0
		-- local fuel_flow = 0
		-- local ff_phase = 0
		-- local tmp_wpt_eta2 = 0
		-- local tmp_wpt_eta3 = 0
		
		-- local tc_calculated = 0
		-- local td_calculated = 0
		
		-- CALCSK
		-- create SPD and ALT restrict table
		legs_restr_spd = {}
		legs_restr_spd_n = 0
		legs_restr_alt = {}
		legs_restr_alt_n = 0
		
		max_cruise_alt = 0
		max_cruise_alt_idx = 0
		
		local spd_rest_disable = 0
		
		for n = 1, (legs_num + 1) do
			if n <= legs_num then
				if legs_data[n][17] == 1 then
					if B738DR_fms_vref_40 > 0 then
						legs_data[n][10] = B738DR_fms_vref_40 + 20	-- spd restrict
					else
						legs_data[n][10] = 0
					end
				end
				
				spd_rest_disable = 0
				if first_star_idx ~= 0 then
					if n >= first_star_idx then
						spd_rest_disable = 0
					end
				elseif first_app_idx ~= 0 then
					if n >= first_app_idx then
						spd_rest_disable = 0
					end
				elseif missed_app_act_idx ~= 0 then
					if n >= missed_app_act_idx then
						spd_rest_disable = 0
					end
				elseif td_idx ~= 0 then
					if n >= td_idx then
						spd_rest_disable = 0
					end
				elseif legs_data[n][21] == 0 or legs_data[n][21] == 1 then
					spd_rest_disable = 1
				-- elseif n < legs_num then
					-- if legs_data[n+1][21] == 0 or legs_data[n+1][21] == 1 then
						-- spd_rest_disable = 1
					-- end
				end
				
				if legs_data[n][4] > 0 and spd_rest_disable == 0 then
					legs_restr_spd_n = legs_restr_spd_n + 1
					legs_restr_spd[legs_restr_spd_n] = {true, true, true, true, true, true}
					legs_restr_spd[legs_restr_spd_n][1] = legs_data[n][1]	-- id
					legs_restr_spd[legs_restr_spd_n][2] = n					-- idx
					legs_restr_spd[legs_restr_spd_n][3] = legs_data[n][4]	-- spd restrict
					legs_restr_spd[legs_restr_spd_n][4] = legs_data[n][42]	-- spd restrict type
					legs_restr_spd[legs_restr_spd_n][5] = n	-- idx decel
					legs_restr_spd[legs_restr_spd_n][6] = 4.3	--6.7	-- idx decel distance
					if n > 1 then
						if legs_data[n][3] < 4.3 then	--6.7 then
							if legs_data[n-1][4] == 0 then
								legs_restr_spd[legs_restr_spd_n][5] = n - 1
								legs_restr_spd[legs_restr_spd_n][6] =  math.min(4.3 - legs_data[n][3], legs_data[n-1][3])	--6.7
							else
								legs_restr_spd[legs_restr_spd_n][6] = math.max(legs_data[n][3] - 0.3, 0.3)
							end
						end
					end
				else
					-- FAF speed restrict Vref40 + 20
					if legs_data[n][17] == 1 and B738DR_fms_vref_40 > 0 then
						legs_restr_spd_n = legs_restr_spd_n + 1
						legs_restr_spd[legs_restr_spd_n] = {true, true, true, true, true, true}
						legs_restr_spd[legs_restr_spd_n][1] = legs_data[n][1]	-- id
						legs_restr_spd[legs_restr_spd_n][2] = n					-- idx
						legs_restr_spd[legs_restr_spd_n][3] = B738DR_fms_vref_40 + 20	-- spd restrict
						legs_restr_spd[legs_restr_spd_n][4] = 0	-- spd restrict type
						legs_restr_spd[legs_restr_spd_n][5] = n	-- idx decel
						legs_restr_spd[legs_restr_spd_n][6] = 4.3	--6.7	-- idx decel distance
						if n > 1 then
							if legs_data[n][3] < 4.3 then	--6.7
								if legs_data[n-1][4] == 0 then
									legs_restr_spd[legs_restr_spd_n][5] = n - 1
									legs_restr_spd[legs_restr_spd_n][6] =  math.min(4.3 - legs_data[n][3], legs_data[n-1][3])	--6.7
								else
									legs_restr_spd[legs_restr_spd_n][6] = math.max(legs_data[n][3] - 0.3, 0.3)
								end
							end
						end
					else
						if (legs_data[n][21] == 0 or legs_data[n][21] == 1) and spd_rest_disable == 0 then
							legs_restr_spd_n = legs_restr_spd_n + 1
							legs_restr_spd[legs_restr_spd_n] = {true, true, true, true, true, true}
							legs_restr_spd[legs_restr_spd_n][1] = legs_data[n][1]	-- id
							legs_restr_spd[legs_restr_spd_n][2] = n					-- idx
							legs_restr_spd[legs_restr_spd_n][3] = hold_speed_calc(legs_data[n][4], legs_data[n][11])	-- spd restrict
							legs_restr_spd[legs_restr_spd_n][4] = 0	-- spd restrict type
							legs_restr_spd[legs_restr_spd_n][5] = n	-- idx decel
							legs_restr_spd[legs_restr_spd_n][6] = 4.3	--6.7	-- idx decel distance
						end
					end
				end
				if legs_data[n][5] > 0 then
					legs_restr_alt_n = legs_restr_alt_n + 1
					legs_restr_alt[legs_restr_alt_n] = {true, true, true, true, true}
					legs_restr_alt[legs_restr_alt_n][1] = legs_data[n][1]	-- id
					legs_restr_alt[legs_restr_alt_n][2] = n					-- idx
					legs_restr_alt[legs_restr_alt_n][3] = legs_data[n][5]	-- alt restrict
					legs_restr_alt[legs_restr_alt_n][4] = legs_data[n][6]	-- alt restrict A,B,-
					legs_restr_alt[legs_restr_alt_n][5] = legs_data[n][41]
					if legs_data[n][6] == 32 or legs_data[n][6] == 43 or legs_data[n][6] == 0 then
						if legs_data[n][5] > max_cruise_alt then
							max_cruise_alt = legs_data[n][5]
							max_cruise_alt_idx = n
						end
					elseif legs_data[n][6] == 41 then
						if legs_data[n][5] > max_cruise_alt then
							max_cruise_alt = legs_data[n][5]
							max_cruise_alt_idx = n
						end
					end
				end
				distance = distance + wpt_distance(n)
			end
			
			if n >= offset then
				
				if n == 1 or n == offset then
					dist_temp = B738DR_fmc_dist
				else
					dist_temp = wpt_distance(n)
				end
				
				if legs_data[n][1] == "(VECTOR)" then
					dist_temp = 0
				end
				if des_app == "------" then
					dist_dest = dist_dest + dist_temp
				else
					if n <= rnav_idx_last then
						dist_dest = dist_dest + dist_temp
					elseif offset > rnav_idx_last and simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
						dist_dest = dist_dest + dist_temp
					end
				end
				
				if perf_exec > 0 then
					
					legs_data[n][15] = act_fuel
					
					if n == 1 then
						if AP_airspeed < 160 then
							speed_temp1 = 160
							speed_temp1 = ias_to_tas(160, simDR_altitude_pilot)
						else
							speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
						end
						speed_temp2 = speed_temp1
						-- dist_temp = B738DR_fmc_dist
					elseif n == offset then
						if AP_airspeed < 160 then
							speed_temp1 = ias_to_tas(160, legs_data[n][11])
							speed_temp2 = ias_to_tas(160, legs_data[n][11])
						else
							speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
							speed_temp2 = ias_to_tas(AP_airspeed, legs_data[n][11])
						end
						-- dist_temp = B738DR_fmc_dist
					else
						if legs_data[n][10] == 0 then
							if last_speed == 0 then
								speed_temp1 = ias_to_tas(160, legs_data[n-1][11])
								speed_temp2 = ias_to_tas(160, legs_data[n][11])
							elseif last_speed < 100 then
								speed_temp1 = mach_to_tas(last_speed, legs_data[n-1][11])
								speed_temp2 = mach_to_tas(last_speed, legs_data[n][11])
							else
								speed_temp1 = last_speed	--ias_to_tas(last_speed, legs_data[n-1][11])
								speed_temp2 = last_speed	--ias_to_tas(last_speed, legs_data[n][11])
							end
						elseif legs_data[n][10] < 100 then
							speed_temp1 = mach_to_tas(legs_data[n][10], legs_data[n-1][11])
							speed_temp2 = mach_to_tas(legs_data[n][10], legs_data[n][11])
						else
							speed_temp1 = ias_to_tas(legs_data[n][10], legs_data[n-1][11])
							speed_temp2 = ias_to_tas(legs_data[n][10], legs_data[n][11])
						end
						-- dist_temp = wpt_distance(n)
					end
					last_speed = speed_temp2
					
					if time_calc_enable == 1 then
						if calc_rte_enable == 0 then
							fuel_calculated = 1
						end
						time_zulu = simDR_zulu_hours + (simDR_zulu_minutes/60) + (simDR_zulu_seconds/3600)
						
						speed_temp1 = (speed_temp1 + speed_temp2) / 2
						
						--add_dump
						-- if B738DR_fms_test3 == 737 then
							-- dump_leg_append(legs_data[n][1] .. " " .. tostring(speed_temp1) .. "->" .. tostring(legs_data[n][10]) .. "/" .. tostring(legs_data[n][11]) .. "\n")
						-- end
						
						tmp_wind_spd = 0
						if legs_data[n][39] == "" then
							if legs_data[n][38] ~= "" then
								tmp_wind_dir = tonumber(string.sub(legs_data[n][38], 1, 3))
								tmp_wind_spd = tonumber(string.sub(legs_data[n][38], -3, -1))
							end
						else
							tmp_wind_dir = tonumber(string.sub(legs_data[n][39], 1, 3))
							tmp_wind_spd = tonumber(string.sub(legs_data[n][39], -3, -1))
						end
						
						wc_speed = 0
						if tmp_wind_spd > 0 then
							wc_speed = tmp_wind_spd * math.cos(math.rad(Angle180(tmp_wind_dir))-legs_data[n][2])
						end
						speed_temp1 = speed_temp1 + wc_speed
						
						time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
						time_temp = time_temp / 3600	-- hours
						
						if n <= tc_idx then
							ff_phase = 0	-- climb
						elseif n <= td_idx then
							ff_phase = 1	-- cruise
						elseif n <= first_app_idx then
							ff_phase = 2	-- descent
						elseif n <= ed_found then
							if first_app_idx == 0 then
								ff_phase = 2	-- descent
							else
								ff_phase = 3	-- approach
							end
						else
							ff_phase = 4	-- final approach
						end
						if n == 1 then
							fuel_flow = 0
						elseif n == offset then
							fuel_flow = calc_fuel_flow(ff_phase, simDR_altitude_pilot, legs_data[n][11], legs_data[n][10], wc_speed)
						else
							fuel_flow = calc_fuel_flow(ff_phase, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
						end
						
						if n == offset or n == 1 then
							calc_fuel = act_fuel - (time_temp * fuel_flow)
							time_temp = (time_zulu + time_temp) % 24
						else
							calc_fuel = legs_data[n-1][40] - (time_temp * fuel_flow)
							time_temp = (legs_data[n-1][13] + time_temp) % 24
						end
						calc_fuel2 = calc_fuel
						time_temp2 = time_temp
						
						if n <= legs_num then
							legs_data[n][13] = time_temp
							legs_data[n][40] = calc_fuel
						end
						
						-- calc T/C distance and time
						if tc_idx ~= 0 and B738DR_flight_phase < 2 then
							if n <= tc_idx then
								dist_tc = dist_tc + dist_temp
								tc_calculated = 1
								if n == tc_idx then
									dist_tc = dist_tc - tc_dist
									if n == offset then
										speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot) + wc_speed
										-- if legs_data[n][10] < 100 then
											-- speed_temp2 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num)
											-- speed_temp2 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num)
										-- else
											-- speed_temp2 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num)
										-- end
										if legs_data[n][10] == 0 then
											speed_temp2 = speed_temp1
										elseif legs_data[n][10] < 100 then
											speed_temp2 = mach_to_tas(legs_data[n][10], crz_alt_num)
										else
											speed_temp2 = ias_to_tas(legs_data[n][10], crz_alt_num)
										end
										speed_temp2 = speed_temp2 + wc_speed
										-- speed_temp1 = math.max(speed_temp1, 160)
										if dist_temp > tc_dist then
											-- before T/C
											-- to T/C
											speed_temp1 = (speed_temp1 + speed_temp2) / 2
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = ((dist_temp - tc_dist) * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(0, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
											fuel_tc = act_fuel - (time_temp * fuel_flow)
											time_tc = (time_zulu + time_temp) % 24
											-- from T/C
											speed_temp2 = math.max(speed_temp2, 160)
											time_temp = (tc_dist * 1852) / (speed_temp2 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(1, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
											legs_data[n][40] = fuel_tc - (time_temp * fuel_flow)
											legs_data[n][13] = (time_tc + time_temp) % 24
										else
											-- after T/C
											-- from T/C
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(1, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
											legs_data[n][40] = act_fuel - (time_temp * fuel_flow)
											legs_data[n][13] = (time_zulu + time_temp) % 24
										end
									else
										-- to T/C
										if legs_data[n][10] < 100 then
											speed_temp1 = mach_to_tas(B738DR_fmc_climb_speed_mach, legs_data[n-1][11])
										else
											speed_temp1 = ias_to_tas(B738DR_fmc_climb_speed, legs_data[n-1][11])
										end
										speed_temp1 = speed_temp1 + wc_speed
										if legs_data[n][10] < 100 then
											speed_temp2 = mach_to_tas(B738DR_fmc_climb_speed_mach, crz_alt_num)
										else
											speed_temp2 = ias_to_tas(B738DR_fmc_climb_speed, crz_alt_num)
										end
										speed_temp2 = speed_temp2 + wc_speed
										
										speed_temp1 = (speed_temp1 + speed_temp2) / 2
										speed_temp1 = math.max(speed_temp1, 160)
										time_temp = ((dist_temp - tc_dist) * 1852) / (speed_temp1 * 0.51444)	-- seconds
										time_temp = time_temp / 3600	-- hours
										fuel_flow = calc_fuel_flow(0, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
										fuel_tc = legs_data[n-1][40] - (time_temp * fuel_flow)
										time_tc = (legs_data[n-1][13] + time_temp) % 24
										-- from T/C
										-- speed_temp2 = B738DR_fmc_cruise_speed * (1 + (crz_alt_num / 1000 * 0.02))
										-- if legs_data[n][10] < 100 then
											-- speed_temp2 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num)
										-- else
											-- speed_temp2 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num)
										-- end
										if legs_data[n][10] == 0 then
											speed_temp2 = speed_temp1
										elseif legs_data[n][10] < 100 then
											speed_temp2 = mach_to_tas(legs_data[n][10], crz_alt_num)
										else
											speed_temp2 = ias_to_tas(legs_data[n][10], crz_alt_num)
										end
										speed_temp2 = speed_temp2 + wc_speed
										speed_temp2 = math.max(speed_temp2, 160)
										time_temp = (tc_dist * 1852) / (speed_temp2 * 0.51444)	-- seconds
										time_temp = time_temp / 3600	-- hours
										fuel_flow = calc_fuel_flow(1, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
										legs_data[n][40] = fuel_tc - (time_temp * fuel_flow)
										legs_data[n][13] = (time_tc + time_temp) % 24
									end
								end
							end
						end
						
						-- calc T/D distance and time
						if td_idx ~= 0 and B738DR_flight_phase < 5 and offset <= td_idx then
							if n <= td_idx then
								td_calculated = 1
								dist_td = dist_td + dist_temp
								if n == td_idx then
									dist_td = dist_td - td_dist
									if td_idx == tc_idx then
										-- TC idx = TD idx
										if n == offset then
											if dist_temp > tc_dist and tc_calculated ~= 0 then
												-- to T/D
												-- if legs_data[n][10] < 100 then
													-- speed_temp1 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num)
												-- else
													-- speed_temp1 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num)
												-- end
												if legs_data[n][10] == 0 then
													speed_temp1 = 180
												elseif legs_data[n][10] < 100 then
													speed_temp1 = mach_to_tas(legs_data[n][10], crz_alt_num)
												else
													speed_temp1 = ias_to_tas(legs_data[n][10], crz_alt_num)
												end
												speed_temp1 = speed_temp1 + wc_speed
												if legs_data[n][10] < 100 then
													speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data[n][11])
												else
													speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data[n][11])
												end
												speed_temp2 = speed_temp2 + wc_speed
												
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (math.max(tc_dist - td_dist, 0) * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(1, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
												fuel_td = fuel_tc - (time_temp * fuel_flow)
												time_td = (time_tc + time_temp) % 24
												-- from T/D
												speed_temp1 = (speed_temp1 + speed_temp2) / 2
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (td_dist * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
												legs_data[n][40] = fuel_td - (time_temp * fuel_flow)
												legs_data[n][13] = (time_td + time_temp) % 24
											else
												speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
												speed_temp1 = speed_temp1 + wc_speed
												if legs_data[n][10] < 100 then
													speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data[n][11])
												else
													speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data[n][11])
												end
												speed_temp2 = speed_temp2 + wc_speed
												
												speed_temp1 = math.max(speed_temp1, 160)
												if dist_temp > td_dist then
													-- before T/D
													-- to T/D
													time_temp = ((dist_temp - td_dist) * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(1, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
													fuel_td = act_fuel - (time_temp * fuel_flow)
													time_td = (time_zulu + time_temp) % 24
													-- from T/D
													speed_temp1 = (speed_temp1 + speed_temp2) / 2
													speed_temp1 = math.max(speed_temp1, 160)
													time_temp = (td_dist * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
													legs_data[n][40] = fuel_td - (time_temp * fuel_flow)
													legs_data[n][13] = (time_td + time_temp) % 24
												else
													-- after T/D
													-- from T/D
													speed_temp1 = (speed_temp1 + speed_temp2) / 2
													speed_temp1 = math.max(speed_temp1, 160)
													time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
													legs_data[n][40] = act_fuel - (time_temp * fuel_flow)
													legs_data[n][13] = (time_zulu + time_temp) % 24
												end
											end
										else
											-- to T/D
											-- if legs_data[n][10] < 100 then
												-- speed_temp1 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num)
											-- else
												-- speed_temp1 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num)
											-- end
											if legs_data[n][10] == 0 then
												speed_temp1 = 180
											elseif legs_data[n][10] < 100 then
												speed_temp1 = mach_to_tas(legs_data[n][10], crz_alt_num)
											else
												speed_temp1 = ias_to_tas(legs_data[n][10], crz_alt_num)
											end
											speed_temp1 = speed_temp1 + wc_speed
											if legs_data[n][10] < 100 then
												speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data[n][11])
											else
												speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data[n][11])
											end
											speed_temp2 = speed_temp2 + wc_speed
											
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = (math.max(tc_dist - td_dist, 0) * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(1, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
											if tc_calculated == 0 then
												fuel_td = legs_data[n-1][40] - (time_temp * fuel_flow)
												time_td = (legs_data[n-1][13] + time_temp) % 24
											else
												fuel_td = fuel_tc - (time_temp * fuel_flow)
												time_td = (time_tc + time_temp) % 24
											end
											speed_temp1 = (speed_temp1 + speed_temp2) / 2
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = (td_dist * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
											legs_data[n][40] = fuel_td - (time_temp * fuel_flow)
											legs_data[n][13] = (time_td + time_temp) % 24
										end
									else
										if n == offset then
											speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
											speed_temp1 = speed_temp1 + wc_speed
											if legs_data[n][10] < 100 then
												speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data[n][11])
											else
												speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data[n][11])
											end
											speed_temp2 = speed_temp2 + wc_speed
											
											speed_temp1 = math.max(speed_temp1, 160)
											if dist_temp > td_dist then
												-- before T/D
												-- to T/D
												time_temp = ((dist_temp - td_dist) * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(1, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
												fuel_td = act_fuel - (time_temp * fuel_flow)
												time_td = (time_zulu + time_temp) % 24
												-- from T/D
												speed_temp1 = (speed_temp1 + speed_temp2) / 2
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (td_dist * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
												legs_data[n][40] = fuel_td - (time_temp * fuel_flow)
												legs_data[n][13] = (time_td + time_temp) % 24
											else
												-- after T/D
												-- from T/D
												speed_temp1 = (speed_temp1 + speed_temp2) / 2
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
												legs_data[n][40] = act_fuel - (time_temp * fuel_flow)
												legs_data[n][13] = (time_zulu + time_temp) % 24
											end
										else
											-- to T/D
											-- if legs_data[n][10] < 100 then
												-- speed_temp1 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num)
											-- else
												-- speed_temp1 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num)
											-- end
											if legs_data[n][10] == 0 then
												speed_temp1 = 180
											elseif legs_data[n][10] < 100 then
												speed_temp1 = mach_to_tas(legs_data[n][10], crz_alt_num)
											else
												speed_temp1 = ias_to_tas(legs_data[n][10], crz_alt_num)
											end
											speed_temp1 = speed_temp1 + wc_speed
											if legs_data[n][10] < 100 then
												speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data[n][11])
											else
												speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data[n][11])
											end
											speed_temp2 = speed_temp2 + wc_speed
											
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = ((dist_temp - td_dist) * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(1, legs_data[n-1][11], legs_data[n][11], legs_data[n][10], wc_speed)
											fuel_td = legs_data[n-1][40] - (time_temp * fuel_flow)
											time_td = (legs_data[n-1][13] + time_temp) % 24
											-- from T/D
											speed_temp1 = (speed_temp1 + speed_temp2) / 2
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = (td_dist * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(2, legs_data[n][11], legs_data[n][11], legs_data[n][10], wc_speed)
											legs_data[n][40] = fuel_td - (time_temp * fuel_flow)
											legs_data[n][13] = (time_td + time_temp) % 24
										end
									end
								end
							end
						end
						
						if n == legs_num + 1 then
							if n ~= td_idx then
								if rnav_idx_last > 0 and rnav_idx_last <= legs_num then
									legs_data[n][13] = legs_data[rnav_idx_last][13]
									legs_data[n][40] = legs_data[rnav_idx_last][40]
								else
									legs_data[n][13] = time_temp2
									legs_data[n][40] = calc_fuel2
								end
							end
							
							-- calc time dataref
							if legs_data[n][13] == 0 then
								time_dest = ""
							else
								time_dest = time_to_string(legs_data[n][13])
							end
							if legs_data[n][40] < 0 then
								legs_data[n][40] = 0.0001
							end
						end
						
						-- calc E/D distance and time
						if ed_found ~= 0 and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and offset <= ed_found then
							if n <= ed_found then
								dist_ed = dist_ed + dist_temp
								if n == ed_found then
									time_ed = time_temp
									fuel_ed = legs_data[n][40]
								end
							end
						end
					end
				end
			end
			
			
		end
		
		if nav_mode == 99 then
			B738DR_fpln_dist = B738DR_fmc_dist
		else
			nd_x = legs_num + 1
			B738DR_fpln_dist = distance + wpt_distance(nd_x)
		end
	else
		legs_restr_spd_n = 0
		legs_restr_alt_n = 0
		B738DR_fpln_dist = 0
		legs_restr_spd_n_mod = 0
		legs_restr_alt_n_mod = 0
		use_precalc = 0
	end
	
	-- B738DR_fms_test3 = 0
	-- B738DR_fms_test6 = 0
	
	local use_rte12_act = rte12_act
	
	if use_rte12_act < 0 then
		if exec_activate_fpln == 1 then
			use_rte12_act = 0
		elseif exec_activate_fpln2 == 1 then
			use_rte12_act = 1
		end
	end
	
	copy_to_legs_data2_buf(use_rte12_act)
	
	local use_calc_rte_enable2 = 0
	if use_rte12_act == 0 then
		use_calc_rte_enable2 = calc_rte_enable2
	elseif use_rte12_act == 1 then
		use_calc_rte_enable2 = calc_rte_enable8
	end
	
	if legs_num2_buf > 1 and use_calc_rte_enable2 == 0 then
		
		-- B738DR_fms_test3 = 1
		
		time_zulu = 0
		time_temp = 0
		time_temp2 = 0
		speed_temp = 0
		speed_temp_prev = 0
		speed_temp1 = 0
		speed_temp2 = 0
		last_speed = 0
		dist_temp = 0
		wc_speed = 0
		tmp_wind_dir = 0
		tmp_wind_spd = 0
		act_fuel = 0	--simDR_fuel_weight
		calc_fuel = 0
		calc_fuel2 = 0
		fuel_flow = 0
		ff_phase = 0
		tmp_wpt_eta2 = 0
		tmp_wpt_eta3 = 0
		
		tc_calculated = 0
		td_calculated = 0
		
		local crz_alt_num_mod = crz_alt_num2
		if crz_alt_num_mod == 0 then
			crz_alt_num_mod = crz_alt_num
		end
		
		-- if simDR_flaps_ratio > 0.624 or was_on_air ~= 0 or simDR_engine_on[0] ~= 0 or simDR_engine_on[1] ~= 0 then
		-- if simDR_engine_on[0] ~= 0 or simDR_engine_on[1] ~= 0 or was_on_air ~= 0 then
		if simDR_eng_N2[0] > 50 or simDR_eng_N2[1] > 50 or was_on_air ~= 0 then
			act_fuel = simDR_fuel_weight
		else
			act_fuel = tonumber(plan_weight_kgs_mod)
			if act_fuel == nil then
				act_fuel = simDR_fuel_weight
			else
				act_fuel = act_fuel * 1000
			end
		end
		
		-- local loop_num = 0
		-- B738DR_fms_test3 = offset_mod
		-- B738DR_fms_test4 = legs_num2_buf
		-- B738DR_fms_test6 = 1
		-- B738DR_fms_test7 = 0
		-- B738DR_fms_test8 = 0
		-- if use_calc_rte_enable2 == 0 then
		
		if legs_delete == 0 and legs_num == legs_num2_buf then
			-- B738DR_fms_test6 = 1
			dist_dest_mod = dist_dest
			legs_data2_buf[legs_num2_buf + 1][13] = legs_data[legs_num + 1][13]
			legs_data2_buf[legs_num2_buf + 1][40] = legs_data[legs_num + 1][40]
			
			-- B738DR_fms_test6 = 2
			
		elseif legs_delete == 1 or legs_num <= 1 then
			
			-- B738DR_fms_test6 = 3
			
			-- B738DR_fms_test3 = 2
			
			-- temporary
			legs_restr_spd_mod = {}
			legs_restr_spd_n_mod = 0
			legs_restr_alt_mod = {}
			legs_restr_alt_n_mod = 0
			
			-- B738DR_fms_test5 = legs_data2_buf[legs_num2_buf + 1][3]
			
			local spd_rest_disable = 0
			for n = 1, legs_num2_buf + 1 do
				
				-- B738DR_fms_test6 = 3
				-- B738DR_fms_test8 = legs_num2_buf + 1
				-- B738DR_fms_test7 = n
				
				-- loop_num = loop_num + 1
				
				-- if B738DR_fms_test == 222 then
					-- dump_leg_mod2()
					-- B738DR_fms_test = 0
				-- end
				-- B738DR_fms_test5 = loop_num
				
				if n <= legs_num2_buf and n > 1 then
					-- if (legs_data2_buf[n][17] == 1 or legs_data2_buf[n][17] == 101 or legs_data2_buf[n][17] == 201) then
					if legs_data2_buf[n][17] == 1 then
						if B738DR_fms_vref_40 > 0 then
							legs_data2_buf[n][10] = B738DR_fms_vref_40 + 20	-- spd restrict
						else
							legs_data2_buf[n][10] = 0
						end
					end
					
					spd_rest_disable = 0
					if first_star_idx_mod_buf ~= 0 then
						if n >= first_star_idx_mod_buf then
							spd_rest_disable = 0
						end
					elseif first_app_idx_mod_buf ~= 0 then
						if n >= first_app_idx_mod_buf then
							spd_rest_disable = 0
						end
					elseif td_idx_mod ~= 0 then
						if n >= td_idx_mod then
							spd_rest_disable = 0
						end
					elseif legs_data2_buf[n][21] == 0 or legs_data2_buf[n][21] == 1 then
						spd_rest_disable = 1
					-- elseif n < legs_num then
						-- if legs_data2_buf[n+1][21] == 0 or legs_data2_buf[n+1][21] == 1 then
							-- spd_rest_disable = 1
						-- end
					end
					
					if legs_data2_buf[n][4] > 0 and spd_rest_disable == 0 then
						legs_restr_spd_n_mod = legs_restr_spd_n_mod + 1
						legs_restr_spd_mod[legs_restr_spd_n_mod] = {true, true, true, true, true, true}
						legs_restr_spd_mod[legs_restr_spd_n_mod][1] = legs_data2_buf[n][1]	-- id
						legs_restr_spd_mod[legs_restr_spd_n_mod][2] = n					-- idx
						legs_restr_spd_mod[legs_restr_spd_n_mod][3] = legs_data2_buf[n][4]	-- spd restrict
						legs_restr_spd_mod[legs_restr_spd_n_mod][4] = legs_data2_buf[n][42]	-- spd restrict type
						legs_restr_spd_mod[legs_restr_spd_n_mod][5] = 0
						legs_restr_spd_mod[legs_restr_spd_n_mod][6] = 0
					else
						-- FAF speed restrict Vref40 + 20
						-- if (legs_data2_buf[n][17] == 1 or legs_data2_buf[n][17] == 101 or legs_data2_buf[n][17] == 201)
						if legs_data2_buf[n][17] == 1 and B738DR_fms_vref_40 > 0 then
							legs_restr_spd_n_mod = legs_restr_spd_n_mod + 1
							legs_restr_spd_mod[legs_restr_spd_n_mod] = {true, true, true, true, true, true}
							legs_restr_spd_mod[legs_restr_spd_n_mod][1] = legs_data2_buf[n][1]	-- id
							legs_restr_spd_mod[legs_restr_spd_n_mod][2] = n					-- idx
							legs_restr_spd_mod[legs_restr_spd_n_mod][3] = B738DR_fms_vref_40 + 20	-- spd restrict
							legs_restr_spd_mod[legs_restr_spd_n_mod][4] = 0	-- spd restrict type
							legs_restr_spd_mod[legs_restr_spd_n_mod][5] = 0
							legs_restr_spd_mod[legs_restr_spd_n_mod][6] = 0
						else
							if (legs_data2_buf[n][21] == 0 or legs_data2_buf[n][21] == 1) and spd_rest_disable == 0 then
								legs_restr_spd_n_mod = legs_restr_spd_n_mod + 1
								legs_restr_spd_mod[legs_restr_spd_n_mod] = {true, true, true, true, true, true}
								legs_restr_spd_mod[legs_restr_spd_n_mod][1] = legs_data2_buf[n][1]	-- id
								legs_restr_spd_mod[legs_restr_spd_n_mod][2] = n					-- idx
								legs_restr_spd_mod[legs_restr_spd_n_mod][3] = hold_speed_calc(legs_data2_buf[n][4], legs_data2_buf[n][11])	-- spd restrict
								legs_restr_spd_mod[legs_restr_spd_n_mod][4] = 0	-- spd restrict type
								legs_restr_spd_mod[legs_restr_spd_n_mod][5] = n	-- idx decel
								legs_restr_spd_mod[legs_restr_spd_n_mod][6] = 4.3	--6.7	-- idx decel distance
							end
						end
					end
					if legs_data2_buf[n][5] > 0 then
						legs_restr_alt_n_mod = legs_restr_alt_n_mod + 1
						legs_restr_alt_mod[legs_restr_alt_n_mod] = {true, true, true, true, true}
						legs_restr_alt_mod[legs_restr_alt_n_mod][1] = legs_data2_buf[n][1]	-- id
						legs_restr_alt_mod[legs_restr_alt_n_mod][2] = n					-- idx
						legs_restr_alt_mod[legs_restr_alt_n_mod][3] = legs_data2_buf[n][5]	-- alt restrict
						legs_restr_alt_mod[legs_restr_alt_n_mod][4] = legs_data2_buf[n][6]	-- alt restrict A,B,-
						legs_restr_alt_mod[legs_restr_alt_n_mod][5] = legs_data2_buf[n][41]
					end
				end
				
				-- B738DR_fms_test6 = loop_num
				
				-- B738DR_fms_test8 = n
				
				-- if n >= offset_mod and perf_exec > 0 then
				if n >= offset_mod then
					
					-- B738DR_fms_test5 = B738DR_fms_test5 + 1
					-- B738DR_fms_test3 = offset_mod
					-- loop_num = loop_num + 1
					
					if n == 1 or n == offset_mod then
						dist_temp = B738DR_fmc_dist_mod
					else
						dist_temp = wpt_distance2(n, rte12_sel)
					end
					
					if legs_data2_buf[n][1] == "(VECTOR)" then
						dist_temp = 0
					end
					if des_app2 == "------" then
						dist_dest_mod = dist_dest_mod + dist_temp
					else
						if n <= rnav_idx_last_mod_buf then
							dist_dest_mod = dist_dest_mod + dist_temp
						elseif offset_mod > rnav_idx_last_mod_buf and simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							dist_dest_mod = dist_dest_mod + dist_temp
						end
					end
					
					-- B738DR_fms_test7 = loop_num
					
					if perf_exec > 0 then
						
						legs_data2_buf[n][15] = act_fuel
						
						if n == 1 then
							if AP_airspeed < 160 then
								speed_temp1 = 160
								speed_temp1 = ias_to_tas(160, simDR_altitude_pilot)
							else
								speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
							end
							speed_temp2 = speed_temp1
							-- dist_temp = B738DR_fmc_dist_mod
						elseif n == offset_mod then
							if AP_airspeed < 160 then
								speed_temp1 = ias_to_tas(160, legs_data2_buf[n][11])
								speed_temp2 = ias_to_tas(160, legs_data2_buf[n][11])
							else
								speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
								speed_temp2 = ias_to_tas(AP_airspeed, legs_data2_buf[n][11])
							end
							-- dist_temp = B738DR_fmc_dist_mod
						else
							if legs_data2_buf[n][10] == 0 then
								if last_speed == 0 then
									speed_temp1 = ias_to_tas(160, legs_data2_buf[n-1][11])
									speed_temp2 = ias_to_tas(160, legs_data2_buf[n][11])
								elseif last_speed < 100 then
									speed_temp1 = mach_to_tas(last_speed, legs_data2_buf[n-1][11])
									speed_temp2 = mach_to_tas(last_speed, legs_data2_buf[n][11])
								else
									speed_temp1 = last_speed	--ias_to_tas(last_speed, legs_data2_buf[n-1][11])
									speed_temp2 = last_speed	--ias_to_tas(last_speed, legs_data2_buf[n][11])
								end
							elseif legs_data2_buf[n][10] < 100 then
								speed_temp1 = mach_to_tas(legs_data2_buf[n][10], legs_data2_buf[n-1][11])
								speed_temp2 = mach_to_tas(legs_data2_buf[n][10], legs_data2_buf[n][11])
							else
								speed_temp1 = ias_to_tas(legs_data2_buf[n][10], legs_data2_buf[n-1][11])
								speed_temp2 = ias_to_tas(legs_data2_buf[n][10], legs_data2_buf[n][11])
							end
							-- dist_temp = wpt_distance2(n, rte12_sel)
						end
						last_speed = speed_temp2
						
						if time_calc_enable2 == 1 then
							
							-- B738DR_fms_test3 = 5
							
							if use_calc_rte_enable2 == 0 then
								fuel_calculated2 = 1
							end
							time_zulu = simDR_zulu_hours + (simDR_zulu_minutes/60) + (simDR_zulu_seconds/3600)
							
							speed_temp1 = (speed_temp1 + speed_temp2) / 2
							
							tmp_wind_spd = 0
							if legs_data2_buf[n][39] == "" then
								if legs_data2_buf[n][38] ~= "" then
									tmp_wind_dir = tonumber(string.sub(legs_data2_buf[n][38], 1, 3))
									tmp_wind_spd = tonumber(string.sub(legs_data2_buf[n][38], -3, -1))
								end
							else
								tmp_wind_dir = tonumber(string.sub(legs_data2_buf[n][39], 1, 3))
								tmp_wind_spd = tonumber(string.sub(legs_data2_buf[n][39], -3, -1))
							end
							
							wc_speed = 0
							if tmp_wind_spd > 0 then
								wc_speed = tmp_wind_spd * math.cos(math.rad(Angle180(tmp_wind_dir))-legs_data2_buf[n][2])
							end
							speed_temp1 = speed_temp1 + wc_speed
							
							time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
							time_temp = time_temp / 3600	-- hours
							
							if n <= tc_idx_mod then
								ff_phase = 0	-- climb
							elseif n <= td_idx_mod then
								ff_phase = 1	-- cruise
							elseif n <= first_app_idx_mod_buf then
								ff_phase = 2	-- descent
							elseif n <= ed_found_mod then
								if first_app_idx_mod_buf == 0 then
									ff_phase = 2	-- descent
								else
									ff_phase = 3	-- approach
								end
							else
								ff_phase = 4	-- final approach
							end
							if n == 1 then
								fuel_flow = 0
							elseif n == offset_mod then
								fuel_flow = calc_fuel_flow(ff_phase, simDR_altitude_pilot, legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
							else
								fuel_flow = calc_fuel_flow(ff_phase, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
							end
							
							if n == offset_mod or n == 1 then
								calc_fuel = act_fuel - (time_temp * fuel_flow)
								time_temp = (time_zulu + time_temp) % 24
							else
								calc_fuel = legs_data2_buf[n-1][40] - (time_temp * fuel_flow)
								time_temp = (legs_data2_buf[n-1][13] + time_temp) % 24
							end
							calc_fuel2 = calc_fuel
							time_temp2 = time_temp
							
							if n <= legs_num2_buf then
								legs_data2_buf[n][13] = time_temp
								legs_data2_buf[n][40] = calc_fuel
							end
							
							-- calc T/C distance and time
							if tc_idx_mod ~= 0 then	--and B738DR_flight_phase < 2 then
								if n <= tc_idx_mod then
									dist_tc_mod = dist_tc_mod + dist_temp
									tc_calculated = 1
									if n == tc_idx_mod then
										dist_tc_mod = dist_tc_mod - tc_dist_mod
										if n == offset_mod then
											speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot) + wc_speed
											-- if legs_data2_buf[n][10] < 100 then
												-- speed_temp2 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num_mod)
											-- else
												-- speed_temp2 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num_mod)
											-- end
											if legs_data2_buf[n][10] == 0 then
												speed_temp2 = speed_temp1
											elseif legs_data2_buf[n][10] < 100 then
												speed_temp2 = mach_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
											else
												speed_temp2 = ias_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
											end
											speed_temp2 = speed_temp2 + wc_speed
											-- speed_temp1 = math.max(speed_temp1, 160)
											if dist_temp > tc_dist_mod then
												-- before T/C
												-- to T/C
												speed_temp1 = (speed_temp1 + speed_temp2) / 2
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = ((dist_temp - tc_dist_mod) * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(0, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												fuel_tc_mod = act_fuel - (time_temp * fuel_flow)
												time_tc_mod = (time_zulu + time_temp) % 24
												-- from T/C
												speed_temp2 = math.max(speed_temp2, 160)
												time_temp = (tc_dist_mod * 1852) / (speed_temp2 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(1, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												legs_data2_buf[n][40] = fuel_tc_mod - (time_temp * fuel_flow)
												legs_data2_buf[n][13] = (time_tc_mod + time_temp) % 24
											else
												-- after T/C
												-- from T/C
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(1, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												legs_data2_buf[n][40] = act_fuel - (time_temp * fuel_flow)
												legs_data2_buf[n][13] = (time_zulu + time_temp) % 24
											end
										else
											-- to T/C
											if legs_data2_buf[n][10] < 100 then
												speed_temp1 = mach_to_tas(B738DR_fmc_climb_speed_mach, legs_data2_buf[n-1][11])
											else
												speed_temp1 = ias_to_tas(B738DR_fmc_climb_speed, legs_data2_buf[n-1][11])
											end
											speed_temp1 = speed_temp1 + wc_speed
											if legs_data2_buf[n][10] < 100 then
												speed_temp2 = mach_to_tas(B738DR_fmc_climb_speed_mach, crz_alt_num_mod)
											else
												speed_temp2 = ias_to_tas(B738DR_fmc_climb_speed, crz_alt_num_mod)
											end
											speed_temp2 = speed_temp2 + wc_speed
											
											speed_temp1 = (speed_temp1 + speed_temp2) / 2
											speed_temp1 = math.max(speed_temp1, 160)
											time_temp = ((dist_temp - tc_dist_mod) * 1852) / (speed_temp1 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(0, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
											fuel_tc_mod = legs_data2_buf[n-1][40] - (time_temp * fuel_flow)
											time_tc_mod = (legs_data2_buf[n-1][13] + time_temp) % 24
											-- from T/C
											-- speed_temp2 = B738DR_fmc_cruise_speed * (1 + (crz_alt_num_mod / 1000 * 0.02))
											-- if legs_data2_buf[n][10] < 100 then
												-- speed_temp2 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num_mod)
											-- else
												-- speed_temp2 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num_mod)
											-- end
											if legs_data2_buf[n][10] == 0 then
												speed_temp2 = speed_temp1
											elseif legs_data2_buf[n][10] < 100 then
												speed_temp2 = mach_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
											else
												speed_temp2 = ias_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
											end
											speed_temp2 = speed_temp2 + wc_speed
											speed_temp2 = math.max(speed_temp2, 160)
											time_temp = (tc_dist_mod * 1852) / (speed_temp2 * 0.51444)	-- seconds
											time_temp = time_temp / 3600	-- hours
											fuel_flow = calc_fuel_flow(1, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
											legs_data2_buf[n][40] = fuel_tc_mod - (time_temp * fuel_flow)
											legs_data2_buf[n][13] = (time_tc_mod + time_temp) % 24
										end
									end
								end
							end
							
							-- calc T/D distance and time
							if td_idx_mod ~= 0 and B738DR_flight_phase < 5 and offset_mod <= td_idx_mod then
								if n <= td_idx_mod then
									td_calculated = 1
									dist_td_mod = dist_td_mod + dist_temp
									if n == td_idx_mod then
										dist_td_mod = dist_td_mod - td_dist_mod
										if td_idx_mod == tc_idx_mod then
											-- TC idx = TD idx
											if n == offset_mod then
												if dist_temp > tc_dist_mod and tc_calculated ~= 0 then
													-- to T/D
													-- if legs_data2_buf[n][10] < 100 then
														-- speed_temp1 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num_mod)
													-- else
														-- speed_temp1 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num_mod)
													-- end
													if legs_data2_buf[n][10] == 0 then
														speed_temp1 = 180
													elseif legs_data2_buf[n][10] < 100 then
														speed_temp1 = mach_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
													else
														speed_temp1 = ias_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
													end
													speed_temp1 = speed_temp1 + wc_speed
													if legs_data2_buf[n][10] < 100 then
														speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data2_buf[n][11])
													else
														speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data2_buf[n][11])
													end
													speed_temp2 = speed_temp2 + wc_speed
													
													speed_temp1 = math.max(speed_temp1, 160)
													time_temp = (math.max(tc_dist_mod - td_dist_mod, 0) * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(1, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
													fuel_td_mod = fuel_tc_mod - (time_temp * fuel_flow)
													time_td_mod = (time_tc_mod + time_temp) % 24
													-- from T/D
													speed_temp1 = (speed_temp1 + speed_temp2) / 2
													speed_temp1 = math.max(speed_temp1, 160)
													time_temp = (td_dist_mod * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
													legs_data2_buf[n][40] = fuel_td_mod - (time_temp * fuel_flow)
													legs_data2_buf[n][13] = (time_td_mod + time_temp) % 24
												else
													speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
													speed_temp1 = speed_temp1 + wc_speed
													if legs_data2_buf[n][10] < 100 then
														speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data2_buf[n][11])
													else
														speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data2_buf[n][11])
													end
													speed_temp2 = speed_temp2 + wc_speed
													
													speed_temp1 = math.max(speed_temp1, 160)
													if dist_temp > td_dist_mod then
														-- before T/D
														-- to T/D
														time_temp = ((dist_temp - td_dist_mod) * 1852) / (speed_temp1 * 0.51444)	-- seconds
														time_temp = time_temp / 3600	-- hours
														fuel_flow = calc_fuel_flow(1, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
														fuel_td_mod = act_fuel - (time_temp * fuel_flow)
														time_td_mod = (time_zulu + time_temp) % 24
														-- from T/D
														speed_temp1 = (speed_temp1 + speed_temp2) / 2
														speed_temp1 = math.max(speed_temp1, 160)
														time_temp = (td_dist_mod * 1852) / (speed_temp1 * 0.51444)	-- seconds
														time_temp = time_temp / 3600	-- hours
														fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
														legs_data2_buf[n][40] = fuel_td_mod - (time_temp * fuel_flow)
														legs_data2_buf[n][13] = (time_td_mod + time_temp) % 24
													else
														-- after T/D
														-- from T/D
														speed_temp1 = (speed_temp1 + speed_temp2) / 2
														speed_temp1 = math.max(speed_temp1, 160)
														time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
														time_temp = time_temp / 3600	-- hours
														fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
														legs_data2_buf[n][40] = act_fuel - (time_temp * fuel_flow)
														legs_data2_buf[n][13] = (time_zulu + time_temp) % 24
													end
												end
											else
												-- to T/D
												-- if legs_data2_buf[n][10] < 100 then
													-- speed_temp1 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num_mod)
												-- else
													-- speed_temp1 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num_mod)
												-- end
												if legs_data2_buf[n][10] == 0 then
													speed_temp1 = 180
												elseif legs_data2_buf[n][10] < 100 then
													speed_temp1 = mach_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
												else
													speed_temp1 = ias_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
												end
												speed_temp1 = speed_temp1 + wc_speed
												if legs_data2_buf[n][10] < 100 then
													speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data2_buf[n][11])
												else
													speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data2_buf[n][11])
												end
												speed_temp2 = speed_temp2 + wc_speed
												
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (math.max(tc_dist_mod - td_dist_mod, 0) * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(1, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												if tc_calculated == 0 then
													fuel_td_mod = legs_data2_buf[n-1][40] - (time_temp * fuel_flow)
													time_td_mod = (legs_data2_buf[n-1][13] + time_temp) % 24
												else
													fuel_td_mod = fuel_tc_mod - (time_temp * fuel_flow)
													time_td_mod = (time_tc_mod + time_temp) % 24
												end
												speed_temp1 = (speed_temp1 + speed_temp2) / 2
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (td_dist_mod * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												legs_data2_buf[n][40] = fuel_td_mod - (time_temp * fuel_flow)
												legs_data2_buf[n][13] = (time_td_mod + time_temp) % 24
											end
										else
											if n == offset_mod then
												speed_temp1 = ias_to_tas(AP_airspeed, simDR_altitude_pilot)
												speed_temp1 = speed_temp1 + wc_speed
												if legs_data2_buf[n][10] < 100 then
													speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data2_buf[n][11])
												else
													speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data2_buf[n][11])
												end
												speed_temp2 = speed_temp2 + wc_speed
												
												speed_temp1 = math.max(speed_temp1, 160)
												if dist_temp > td_dist_mod then
													-- before T/D
													-- to T/D
													time_temp = ((dist_temp - td_dist_mod) * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(1, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
													fuel_td_mod = act_fuel - (time_temp * fuel_flow)
													time_td_mod = (time_zulu + time_temp) % 24
													-- from T/D
													speed_temp1 = (speed_temp1 + speed_temp2) / 2
													speed_temp1 = math.max(speed_temp1, 160)
													time_temp = (td_dist_mod * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
													legs_data2_buf[n][40] = fuel_td_mod - (time_temp * fuel_flow)
													legs_data2_buf[n][13] = (time_td_mod + time_temp) % 24
												else
													-- after T/D
													-- from T/D
													speed_temp1 = (speed_temp1 + speed_temp2) / 2
													speed_temp1 = math.max(speed_temp1, 160)
													time_temp = (dist_temp * 1852) / (speed_temp1 * 0.51444)	-- seconds
													time_temp = time_temp / 3600	-- hours
													fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
													legs_data2_buf[n][40] = act_fuel - (time_temp * fuel_flow)
													legs_data2_buf[n][13] = (time_zulu + time_temp) % 24
												end
											else
												-- to T/D
												-- if legs_data2_buf[n][10] < 100 then
													-- speed_temp1 = mach_to_tas(B738DR_fmc_cruise_speed_mach, crz_alt_num_mod)
												-- else
													-- speed_temp1 = ias_to_tas(B738DR_fmc_cruise_speed, crz_alt_num_mod)
												-- end
												if legs_data2_buf[n][10] == 0 then
													speed_temp1 = 180
												elseif legs_data2_buf[n][10] < 100 then
													speed_temp1 = mach_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
												else
													speed_temp1 = ias_to_tas(legs_data2_buf[n][10], crz_alt_num_mod)
												end
												speed_temp1 = speed_temp1 + wc_speed
												if legs_data2_buf[n][10] < 100 then
													speed_temp2 = mach_to_tas(B738DR_fmc_descent_speed_mach, legs_data2_buf[n][11])
												else
													speed_temp2 = ias_to_tas(B738DR_fmc_descent_speed, legs_data2_buf[n][11])
												end
												speed_temp2 = speed_temp2 + wc_speed
												
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = ((dist_temp - td_dist_mod) * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(1, legs_data2_buf[n-1][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												fuel_td_mod = legs_data2_buf[n-1][40] - (time_temp * fuel_flow)
												time_td_mod = (legs_data2_buf[n-1][13] + time_temp) % 24
												-- from T/D
												speed_temp1 = (speed_temp1 + speed_temp2) / 2
												speed_temp1 = math.max(speed_temp1, 160)
												time_temp = (td_dist_mod * 1852) / (speed_temp1 * 0.51444)	-- seconds
												time_temp = time_temp / 3600	-- hours
												fuel_flow = calc_fuel_flow(2, legs_data2_buf[n][11], legs_data2_buf[n][11], legs_data2_buf[n][10], wc_speed)
												legs_data2_buf[n][40] = fuel_td_mod - (time_temp * fuel_flow)
												legs_data2_buf[n][13] = (time_td_mod + time_temp) % 24
											end
										end
									end
								end
							end
							
							if n == legs_num2_buf + 1 then
								if n ~= td_idx_mod then
									if rnav_idx_last_mod_buf > 0 and rnav_idx_last_mod_buf <= legs_num2_buf then
										legs_data2_buf[n][13] = legs_data2_buf[rnav_idx_last_mod_buf][13]
										legs_data2_buf[n][40] = legs_data2_buf[rnav_idx_last_mod_buf][40]
									else
										legs_data2_buf[n][13] = time_temp2
										legs_data2_buf[n][40] = calc_fuel2
									end
								end
								
								-- calc time dataref
								if legs_data2_buf[n][13] == 0 then
									time_dest_mod = ""
								else
									time_dest_mod = time_to_string(legs_data2_buf[n][13])
								end
								if legs_data2_buf[n][40] < 0 then
									legs_data2_buf[n][40] = 0.0001
								end
							end
							
							-- calc E/D distance and time
							if ed_found_mod ~= 0 and B738DR_flight_phase > 4 and B738DR_flight_phase < 8 and offset_mod <= ed_found_mod then
								if n <= ed_found_mod then
									dist_ed_mod = dist_ed_mod + dist_temp
									if n == ed_found_mod then
										time_ed_mod = time_temp
										fuel_ed_mod = legs_data2_buf[n][40]
									end
								end
							end
						end
					end
				end
			end
		else
			dist_dest_mod = dist_dest
			time_dest_mod = time_dest
			dist_tc_mod = dist_tc
			time_tc_mod = time_tc
			dist_td_mod = dist_td
			time_td_mod = time_td
			dist_ed_mod = dist_ed
			time_ed_mod = time_ed
			fuel_ed_mod = fuel_ed
			fuel_tc_mod = fuel_tc
			fuel_td_mod = fuel_td
		end
		
		-- B738DR_fms_test6 = 4
		-- B738DR_fms_test5 = loop_num
		
		-- if loop_num == 0 then
			-- B738DR_fms_test7 = legs_num2_buf
		-- end
		copy_from_legs_data2_buf(use_rte12_act)
		
	else
		legs_restr_spd_n_mod = 0
		legs_restr_alt_n_mod = 0
	end
	
	-- copy_from_legs_data2_buf(rte12_act)
	
	-- B738DR_fms_test6 = 5
	
	-- lnav_disco == 0
	
	-- dist_dest2 = 0
	-- if calc_rte_enable2 == 0 then
		-- if legs_num < 2 and legs_num2 > 1 then
			-- if B738DR_fms_test == 222 then
				-- dump_leg_mod2()
				-- B738DR_fms_test = 0
			-- end
			-- B738DR_fms_test1 = 1000
			-- for n = 1, (legs_num2 + 1) do
				-- B738DR_fms_test1 = 1000 + n
				-- dist_dest2 = dist_dest2 + wpt_distance2(n, rte12_sel)
			-- end
		-- end
	-- end
	
	if legs_num > 1 and lnav_disco == 0 then
		if nav_mode == 99 then
			B738DR_xtrack_show = 0
		else
			B738DR_xtrack_show = 1
		end
	else
		B738DR_xtrack_show = 0
	end
	B738DR_xtrack_nd = math.floor((B738DR_xtrack * 100) + 0.5) / 100
end

-- format xxxx
function time_to_string(in_time)
	local tmp_wpt_eta2 = math.floor(in_time) % 24
	local tmp_wpt_eta3 = (in_time - tmp_wpt_eta2) * 60
	tmp_wpt_eta3 = math.min(tmp_wpt_eta3, 59)
	if tmp_wpt_eta2 == 24 then
		tmp_wpt_eta2 = 0
	else
		tmp_wpt_eta2 = math.min(tmp_wpt_eta2, 23)
	end
	return string.format("%02d", tmp_wpt_eta2) .. string.format("%02d", tmp_wpt_eta3)
end

-- format xxxx.x
function time_to_string2(in_time)
	local tmp_wpt_eta2 = math.floor(in_time) % 24
	local tmp_wpt_eta3 = (in_time - tmp_wpt_eta2) * 60
	tmp_wpt_eta3 = math.min(tmp_wpt_eta3, 59.9)
	if tmp_wpt_eta2 == 24 then
		tmp_wpt_eta2 = 0
	else
		tmp_wpt_eta2 = math.min(tmp_wpt_eta2, 23)
	end
	return string.format("%02d", tmp_wpt_eta2) .. string.format("%04.1f", tmp_wpt_eta3)
end

function clb_ff(in_alt)
	
	local ff_calc = 0
	
	if in_alt <= 5000 then
		ff_calc = 6800
	elseif in_alt < 10000 then
		ff_calc = B738_rescale(5000, 6800, 10000, 5960, in_alt)
	elseif in_alt < 15000 then
		ff_calc = B738_rescale(10000, 5960, 15000, 5380, in_alt)
	elseif in_alt < 20000 then
		ff_calc = B738_rescale(15000, 5380, 20000, 4840, in_alt)
	elseif in_alt < 25000 then
		ff_calc = B738_rescale(20000, 4840, 25000, 4320, in_alt)
	elseif in_alt < 30000 then
		ff_calc = B738_rescale(25000, 4320, 30000, 3840, in_alt)
	elseif in_alt < 35000 then
		ff_calc = B738_rescale(30000, 3840, 35000, 3260, in_alt)
	else
		ff_calc = B738_rescale(35000, 3260, 40000, 2640, in_alt)
	end
	
	return ff_calc * 1.15
end

function crz_ff(alt1, spd1)
	
	local ff_calc = 0
	local ff_alt1 = math.min(alt1, 40000)
	
	if spd1 < 100 then
		ff_alt1 = math.max(ff_alt1, 25000)
		if ff_alt1 < 25000 then
			if spd1 < 0.60 then
				ff_calc = 2220
			elseif spd1 < 0.70 then
				ff_calc = B738_rescale(0.60, 2220, 0.70, 2800, spd1)
			elseif spd1 < 0.80 then
				ff_calc = B738_rescale(0.70, 2800, 0.80, 3600, spd1)
			else
				ff_calc = B738_rescale(0.80, 3600, 0.82, 3820, spd1)
			end
		elseif ff_alt1 < 30000 then
			if spd1 < 0.60 then
				ff_calc = 2080
			elseif spd1 < 0.70 then
				ff_calc = B738_rescale(0.60, 2080, 0.70, 3600, spd1)
			elseif spd1 < 0.80 then
				ff_calc = B738_rescale(0.70, 3600, 0.80, 3040, spd1)
			else
				ff_calc = B738_rescale(0.80, 3040, 0.82, 3220, spd1)
			end
		elseif ff_alt1 < 35000 then
			if spd1 < 0.60 then
				ff_calc = 1960
			elseif spd1 < 0.70 then
				ff_calc = B738_rescale(0.60, 1960, 0.70, 2120, spd1)
			elseif spd1 < 0.80 then
				ff_calc = B738_rescale(0.70, 2120, 0.80, 2620, spd1)
			else
				ff_calc = B738_rescale(0.80, 2620, 0.82, 2840, spd1)
			end
		else
			if spd1 < 0.60 then
				ff_calc = 1960
			elseif spd1 < 0.70 then
				ff_calc = B738_rescale(0.60, 1960, 0.70, 2010, spd1)
			elseif spd1 < 0.80 then
				ff_calc = B738_rescale(0.70, 2010, 0.80, 2440, spd1)
			else
				ff_calc = B738_rescale(0.80, 2440, 0.82, 2620, spd1)
			end
		end
	else
		ff_alt1 = math.max(ff_alt1, 10000)
		ff_alt1 = math.min(ff_alt1, 30000)
		if ff_alt1 < 10000 then
			if spd1 < 180 then
				ff_calc = 1500
			elseif spd1 < 250 then
				ff_calc = B738_rescale(180, 1500, 250, 2100, spd1)
			elseif spd1 < 270 then
				ff_calc = B738_rescale(250, 2100, 270, 2400, spd1)
			elseif spd1 < 290 then
				ff_calc = B738_rescale(270, 2400, 290, 2720, spd1)
			elseif spd1 < 320 then
				ff_calc = B738_rescale(290, 2720, 320, 3280, spd1)
			else
				ff_calc = B738_rescale(320, 3280, 340, 3660, spd1)
			end
		elseif ff_alt1 < 15000 then
			if spd1 < 250 then
				ff_calc = 2060
			elseif spd1 < 270 then
				ff_calc = B738_rescale(250, 2060, 270, 2360, spd1)
			elseif spd1 < 290 then
				ff_calc = B738_rescale(270, 2360, 290, 2680, spd1)
			elseif spd1 < 320 then
				ff_calc = B738_rescale(290, 2680, 320, 3240, spd1)
			else
				ff_calc = B738_rescale(320, 3240, 340, 3600, spd1)
			end
		elseif ff_alt1 < 20000 then
			if spd1 < 250 then
				ff_calc = 2040
			elseif spd1 < 270 then
				ff_calc = B738_rescale(250, 2040, 270, 2320, spd1)
			elseif spd1 < 290 then
				ff_calc = B738_rescale(270, 2320, 290, 2640, spd1)
			elseif spd1 < 320 then
				ff_calc = B738_rescale(290, 2640, 320, 3200, spd1)
			else
				ff_calc = B738_rescale(320, 3200, 340, 3600, spd1)
			end
		elseif ff_alt1 < 25000 then
			if spd1 < 250 then
				ff_calc = 2000
			elseif spd1 < 270 then
				ff_calc = B738_rescale(250, 2000, 270, 2300, spd1)
			elseif spd1 < 290 then
				ff_calc = B738_rescale(270, 2300, 290, 2640, spd1)
			elseif spd1 < 320 then
				ff_calc = B738_rescale(290, 2640, 320, 3200, spd1)
			else
				ff_calc = B738_rescale(320, 3200, 340, 3620, spd1)
			end
		else
			if spd1 < 250 then
				ff_calc = 1960
			elseif spd1 < 270 then
				ff_calc = B738_rescale(250, 1960, 270, 2280, spd1)
			elseif spd1 < 290 then
				ff_calc = B738_rescale(270, 2280, 290, 2640, spd1)
			elseif spd1 < 320 then
				ff_calc = B738_rescale(290, 2640, 320, 3280, spd1)
			else
				ff_calc = B738_rescale(320, 3280, 340, 3280, spd1)
			end
		end
	end
	
	return ff_calc
end

function mach_to_tas(in_mach, in_alt)
	
	local oat_lvl = (15 + B738DR_isa_dev_c) - (in_alt * 0.002)
	local result = in_mach * 39 * math.sqrt(oat_lvl + 273.15)
	return result	-- in kts
	
end

function tas_to_mach(in_tas, in_alt)
	
	local oat_lvl = (15 + B738DR_isa_dev_c) - (in_alt * 0.002)
	local result = in_tas /( 39 * math.sqrt(oat_lvl + 273.15))
	return result	-- in mach
	
end

function ias_to_tas (in_ias, in_alt)
	local result = in_ias * (1 + (in_alt / 1000 * 0.02))
	return result	-- in kts
end

function tas_to_ias (in_tas, in_alt)
	local result = in_tas / (1 + (in_alt / 1000 * 0.02))
	return result	-- in kts
end

function calc_fuel_flow(phase, alt1, alt2, spd1, spd2)
	
	local ff_calc = 0
	local ff_calc2 = 0
	local ff_spd = 0
	local ff_alt1 = math.min(alt1, 40000)
	local ff_alt2 = math.min(alt2, 40000)
	ff_alt1 = math.max(ff_alt1, 5000)
	ff_alt2 = math.max(ff_alt2, 5000)
	
	
	if phase == 0 then
		-- clb
		
		if alt1 == alt2 then
			if spd1 < 100 then
				ff_spd = spd1 - tas_to_mach(spd2, ff_alt1)
			else
				ff_spd = spd1 - tas_to_ias (spd2, ff_alt1)
			end
			ff_calc = crz_ff(ff_alt1, ff_spd)
		else
			ff_calc = clb_ff(ff_alt1)
			ff_calc2 = clb_ff(ff_alt2)
			ff_calc = (ff_calc + ff_calc2) / 2
		end
		
	elseif phase == 1 then
		-- crs
		
		if spd1 < 100 then
			ff_spd = spd1 - tas_to_mach(spd2, ff_alt1)
		else
			ff_spd = spd1 - tas_to_ias (spd2, ff_alt1)
		end
		ff_calc = crz_ff(ff_alt1, ff_spd)
		
	elseif phase == 2 then
		-- des
		
		if alt1 == alt2 then
			if spd1 < 100 then
				ff_spd = spd1 - tas_to_mach(spd2, ff_alt1)
			else
				ff_spd = spd1 - tas_to_ias(spd2, ff_alt1)
			end
			ff_calc = crz_ff(ff_alt1, ff_spd)
		else
			ff_calc = B738_rescale(5000, 550, 40000, 260, ff_alt1)
			ff_calc2 = B738_rescale(5000, 550, 40000, 260, ff_alt2)
			ff_calc = (ff_calc + ff_calc2) / 2
		end
		
	elseif phase == 3 then
		-- app
		ff_calc = 1120	--3950
	elseif phase == 4 then
		-- app
		ff_calc = 3950	--4200	--3950
	end
	
	return ff_calc
	
end

function B738_legs_step()

	map_mode = B738DR_capt_map_mode
	
	local rte2_corr = 0
	if rte12_sel == 0 then
		if page_legs == 1 and offset_mod > 0 then
			if map_mode ~= map_mode_old then		--step
				if map_mode == 3 then
					if act_page < 1 then
						act_page = max_page
					elseif act_page > max_page then
						act_page = 1
					end
					
					if B738DR_no_rte2 == 0 and act_page > 1 then
						rte2_corr = 1
					end
					page_legs_step = act_page
					-- act_page = page_legs_step
					local first_legs_step = offset_mod + (5 * (act_page - 1)) - rte2_corr
					local last_legs_step = offset_mod + (5 * (act_page - 1)) + 4 - rte2_corr
					
					if legs_step < first_legs_step or legs_step > last_legs_step then
						legs_step = first_legs_step
					end
					if legs_step > legs_num2 then
						legs_step = legs_num2
					end
					if legs_step > 0 then
						if legs_data2[legs_step] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step < first_legs_step or legs_step > last_legs_step then
								legs_step = first_legs_step
							end
							if legs_step > legs_num2 then
								legs_step = legs_num2
							end
						end
					end
				end
			end
		elseif map_mode ~= map_mode_old and offset_mod > 0 then
			if page_legs_step < offset_mod then
				legs_step = offset_mod
			elseif page_legs_step > legs_num2 then
				legs_step = legs_num2
			end
			if legs_step > 0 then
				if legs_data2[legs_step] == "DISCONTINUITY" then
					legs_step = legs_step + 1
				end
			end
		end
	else
		if page_legs == 1 and offset_mod2 > 0 then
			if map_mode ~= map_mode_old then		--step
				if map_mode == 3 then
					if act_page < 1 then
						act_page = max_page
					elseif act_page > max_page then
						act_page = 1
					end
					
					if B738DR_no_rte2 == 0 and act_page > 1 then
						rte2_corr = 1
					end
					page_legs_step = act_page
					-- act_page = page_legs_step
					local first_legs_step = offset_mod2 + (5 * (act_page - 1)) - rte2_corr
					local last_legs_step = offset_mod2 + (5 * (act_page - 1)) + 4 - rte2_corr
					
					if legs_step < first_legs_step or legs_step > last_legs_step then
						legs_step = first_legs_step
					end
					if legs_step > legs_num8 then
						legs_step = legs_num8
					end
					if legs_step > 0 then
						if legs_data8[legs_step] == "DISCONTINUITY" then
							legs_step = legs_step + 1
							if legs_step < first_legs_step or legs_step > last_legs_step then
								legs_step = first_legs_step
							end
							if legs_step > legs_num8 then
								legs_step = legs_num8
							end
						end
					end
				end
			end
		elseif map_mode ~= map_mode_old and offset_mod2 > 0 then
			if page_legs_step < offset_mod2 then
				legs_step = offset_mod2
			elseif page_legs_step > legs_num8 then
				legs_step = legs_num8
			end
			if legs_step > 0 then
				if legs_data8[legs_step] == "DISCONTINUITY" then
					legs_step = legs_step + 1
				end
			end
		end
	end
	
	map_mode_old = map_mode
	
	if legs_step < 0 then
		legs_step = 0
	end
end

function B738_legs_step2()

	map_mode2 = B738DR_fo_map_mode
	
	local rte2_corr = 0
	if rte12_sel_fo == 0 then
		if page_legs2 == 1 and offset_mod > 0 then
			if map_mode2 ~= map_mode_old2 then		--step
				if map_mode2 == 3 then
					if act_page2 < 1 then
						act_page2 = max_page2
					elseif act_page2 > max_page2 then
						act_page2 = 1
					end
					
					if B738DR_no_rte2 == 0 and act_page > 1 then
						rte2_corr = 1
					end
					page_legs_step2 = act_page2
					-- act_page2 = page_legs_step2
					local first_legs_step = offset_mod + (5 * (act_page2 - 1)) - rte2_corr
					local last_legs_step = offset_mod + (5 * (act_page2 - 1)) + 4 - rte2_corr
					
					if legs_step2 < first_legs_step or legs_step2 > last_legs_step then
						legs_step2 = first_legs_step
					end
					if legs_step2 > legs_num2 then
						legs_step2 = legs_num2
					end
					if legs_step2 > 0 then
						if legs_data2[legs_step2] == "DISCONTINUITY" then
							legs_step2 = legs_step2 + 1
							if legs_step2 < first_legs_step or legs_step2 > last_legs_step then
								legs_step2 = first_legs_step
							end
							if legs_step2 > legs_num2 then
								legs_step2 = legs_num2
							end
						end
					end
				end
			end
		elseif map_mode2 ~= map_mode_old2 and offset_mod > 0 then
			if legs_step2 < offset_mod then
				legs_step2 = offset_mod
			elseif legs_step2 > legs_num2 then
				legs_step2 = legs_num2
			end
			if legs_step2 > 0 then
				if legs_data2[legs_step2] == "DISCONTINUITY" then
					legs_step2 = legs_step2 + 1
				end
			end
		end
	else
		if page_legs2 == 1 and offset_mod2 > 0 then
			if map_mode2 ~= map_mode_old2 then		--step
				if map_mode2 == 3 then
					if act_page2 < 1 then
						act_page2 = max_page2
					elseif act_page2 > max_page2 then
						act_page2 = 1
					end
					
					if B738DR_no_rte2 == 0 and act_page > 1 then
						rte2_corr = 1
					end
					page_legs_step2 = act_page2
					-- act_page2 = page_legs_step2
					local first_legs_step = offset_mod2 + (5 * (act_page2 - 1)) - rte2_corr
					local last_legs_step = offset_mod2 + (5 * (act_page2 - 1)) + 4 - rte2_corr
					
					if legs_step2 < first_legs_step or legs_step2 > last_legs_step then
						legs_step2 = first_legs_step
					end
					if legs_step2 > legs_num8 then
						legs_step2 = legs_num8
					end
					if legs_step2 > 0 then
						if legs_data8[legs_step2] == "DISCONTINUITY" then
							legs_step2 = legs_step2 + 1
							if legs_step2 < first_legs_step or legs_step2 > last_legs_step then
								legs_step2 = first_legs_step
							end
							if legs_step2 > legs_num8 then
								legs_step2 = legs_num8
							end
						end
					end
				end
			end
		elseif map_mode2 ~= map_mode_old2 and offset_mod2 > 0 then
			if legs_step2 < offset_mod2 then
				legs_step2 = offset_mod2
			elseif legs_step2 > legs_num8 then
				legs_step2 = legs_num8
			end
			if legs_step2 > 0 then
				if legs_data8[legs_step2] == "DISCONTINUITY" then
					legs_step2 = legs_step2 + 1
				end
			end
		end
	end
	
	map_mode_old2 = map_mode2
	
	if legs_step2 < 0 then
		legs_step2 = 0
	end
end

function B738_chock()
	if set_chock == 0 and B738DR_chock_status == 1 then
		if simDR_on_ground[0] == 1 and simDR_on_ground[1] == 1 and simDR_on_ground[2] == 1 then
			simDR_pos_x = chock_pos_x
			simDR_pos_y = chock_pos_y
			simDR_pos_z = chock_pos_z
			simDR_pos_vx = 0
			simDR_pos_vy = 0
			simDR_pos_vz = 0
			simDR_pos_ax = 0
			simDR_pos_ay = 0
			simDR_pos_az = 0
		end
	end
end

function B738_pause_td()
	if B738DR_pause_td == 1 then
		if td_idx ~= 0 and B738DR_vnav_td_dist < 8 and B738DR_vnav_td_dist > 2 and pause_td_disable == 0 then
			pause_td_disable = 1
			if simDR_pause == 0 then
				simCMD_pause:once()
			end
		end
		if td_idx ~= 0 and B738DR_vnav_td_dist > 10 then
			pause_td_disable = 0
		end
		
	else
		pause_td_disable = 0
	end
end


function B738_calc_vnav_spd()
	
	local ci = 0
	
	ci = tonumber(cost_index)
	if ci == nil then
		ci = 0
	else
		if ci < 0 then
			ci = 0
		elseif ci > 500 then
			ci = 500
		end	
	end
	
	local total_weight_t = tonumber(gw_kgs)
	if total_weight_t == nil then
		econ_clb_spd = 280
		econ_clb_spd_mach = 0.760
		econ_crz_spd = 300
		econ_crz_spd_mach = 0.800
		econ_des_spd = 280
		econ_des_spd_mach = 0.760
		lrc_crz_spd = 280
		lrc_crz_spd_mach = 0.760
	else
		total_weight_t = math.min(total_weight_t, 80)
		total_weight_t = math.max(total_weight_t, 40)
		
		total_weight_t = total_weight_t * 2.20462262
		
		-- clb
		local ci_min = ci_clb_tab_n
		local ci_max = ci_clb_tab_n
		for ii = 1, ci_clb_tab_n do
			if ci <= ci_clb_tab[ii] then
				ci_max = ii
				if ii == 1 then
					ci_min = ci_max
				else
					ci_min = ci_max - 1
				end
				break
			end
		end
		
		local econ_spd = 0
		local econ_spd2 = 0
		local econ_spd3 = 0
		local econ_spd4 = 0
		
		if total_weight_t > ci_clb_gw2[ci_min] then
			econ_spd2 = ci_clb_ias2[ci_min]
		else
			econ_spd2 = B738_rescale(ci_clb_gw1[ci_min], ci_clb_ias1[ci_min], ci_clb_gw2[ci_min], ci_clb_ias2[ci_min], total_weight_t)
		end
		if total_weight_t > ci_clb_gw2[ci_max] then
			econ_spd3 = ci_clb_ias2[ci_max]
		else
			econ_spd3 = B738_rescale(ci_clb_gw1[ci_max], ci_clb_ias1[ci_max], ci_clb_gw2[ci_max], ci_clb_ias2[ci_max], total_weight_t)
		end
		econ_clb_spd = B738_rescale(ci_clb_tab[ci_min], econ_spd2, ci_clb_tab[ci_max], econ_spd3, ci)
		
		local wind_toc = 0
		local wind_crs_toc = tonumber(crz_wind_dir_mod)
		local wind_spd_toc = tonumber(crz_wind_spd_mod)
		local wind_toc_nm = 0
		local tmp_nm = 0
		local wind_crz_alt = crz_alt_num2
		if wind_crz_alt == 0 then
			wind_crz_alt = crz_alt_num
		end
		if wind_crz_alt > 41000 then
			wind_crz_alt = 41000
		end
		if wind_crz_alt > 0 and wind_crs_toc ~= nil and wind_spd_toc ~= nil then
			if rte12_act == 0 then
				if legs_num2 > 1 then
					wind_toc_nm = ((15 / 41000) * wind_crz_alt * 60 * 154) / 1852
					for ii = 2, legs_num2 do
						tmp_nm = tmp_nm + wpt_distance2(ii, rte12_act)
						if tmp_nm >= wind_toc_nm then
							wind_toc = wind_spd_toc * math.cos(math.rad(Angle180(wind_crs_toc)) - legs_data2[ii][2])
							break
						end
					end
				end
			elseif rte12_act == 1 then
				if legs_num8 > 1 then
					wind_toc_nm = ((15 / 41000) * wind_crz_alt * 60 * 154) / 1852
					for ii = 2, legs_num8 do
						tmp_nm = tmp_nm + wpt_distance2(ii, rte12_act)
						if tmp_nm >= wind_toc_nm then
							wind_toc = wind_spd_toc * math.cos(math.rad(Angle180(wind_crs_toc)) - legs_data8[ii][2])
							break
						end
					end
				end
			elseif rte12_sel == 0 then
				if legs_num2 > 1 then
					wind_toc_nm = ((15 / 41000) * wind_crz_alt * 60 * 154) / 1852
					for ii = 2, legs_num2 do
						tmp_nm = tmp_nm + wpt_distance2(ii, rte12_act)
						if tmp_nm >= wind_toc_nm then
							wind_toc = wind_spd_toc * math.cos(math.rad(Angle180(wind_crs_toc)) - legs_data2[ii][2])
							break
						end
					end
				end
			elseif rte12_sel == 1 then
				if legs_num8 > 1 then
					wind_toc_nm = ((15 / 41000) * wind_crz_alt * 60 * 154) / 1852
					for ii = 2, legs_num8 do
						tmp_nm = tmp_nm + wpt_distance2(ii, rte12_act)
						if tmp_nm >= wind_toc_nm then
							wind_toc = wind_spd_toc * math.cos(math.rad(Angle180(wind_crs_toc)) - legs_data8[ii][2])
							break
						end
					end
				end
			end
		end
		
		if wind_toc > 150 then
			econ_clb_spd = econ_clb_spd - 20
		elseif wind_toc > 50 then
			econ_clb_spd = econ_clb_spd - B738_rescale(50, 9, 150, 20, wind_toc)
		elseif wind_toc > 0 then
			econ_clb_spd = econ_clb_spd - B738_rescale(0, 0, 50, 9, wind_toc)
		elseif wind_toc > -100 then
			econ_clb_spd = econ_clb_spd - B738_rescale(-100, -21, 0, 0, wind_toc)
		elseif wind_toc > -150 then
			econ_clb_spd = econ_clb_spd - B738_rescale(-150, -40, -100, -21, wind_toc)
		else
			econ_clb_spd = econ_clb_spd + 40
		end
		
		local isa_toc = tonumber(isa_dev_c_mod)
		if isa_toc == nil then
			isa_toc = 0
		end
		if isa_toc > 40 then
			econ_clb_spd = econ_clb_spd - 30
		elseif isa_toc > 20 then
			econ_clb_spd = econ_clb_spd - B738_rescale(20, 15, 40, 30, isa_toc)
		elseif isa_toc > 10 then
			econ_clb_spd = econ_clb_spd - B738_rescale(10, 0, 20, 15, isa_toc)
		elseif isa_toc > 0 then
			econ_clb_spd = econ_clb_spd - 0
		elseif isa_toc > -40 then
			econ_clb_spd = econ_clb_spd - B738_rescale(-40, -4, 0, 0, isa_toc)
		else
			econ_clb_spd = econ_clb_spd + 4
		end
		econ_crz_spd = econ_clb_spd
		lrc_crz_spd = econ_clb_spd
		
		--des
		if ci > 53 then
			econ_des_spd = 330
		else
			econ_des_spd = B738_rescale(0, 240, 53, 330, ci)
		end
		
		-- crz
		ci_min = ci_crs_tab_n
		ci_max = ci_crs_tab_n
		for ii = 1, ci_crs_tab_n do
			if ci <= ci_crs_tab[ii] then
				ci_max = ii
				if ii == 1 then
					ci_min = ci_max
				else
					ci_min = ci_max - 1
				end
				break
			end
		end
		
		if wind_crz_alt > 35000 then
			if total_weight_t > ci_crs_35_gw5[ci_min] then
				econ_spd2 = ci_crs_35_m5[ci_min]
			elseif total_weight_t > ci_crs_35_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_35_gw4[ci_min], ci_crs_35_m4[ci_min], ci_crs_35_gw5[ci_min], ci_crs_35_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_35_gw3[ci_min], ci_crs_35_m3[ci_min], ci_crs_35_gw4[ci_min], ci_crs_35_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_35_gw2[ci_min], ci_crs_35_m2[ci_min], ci_crs_35_gw3[ci_min], ci_crs_35_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_35_gw1[ci_min], ci_crs_35_m1[ci_min], ci_crs_35_gw2[ci_min], ci_crs_35_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_35_gw5[ci_max] then
				econ_spd3 = ci_crs_35_m5[ci_max]
			elseif total_weight_t > ci_crs_35_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_35_gw4[ci_max], ci_crs_35_m4[ci_max], ci_crs_35_gw5[ci_max], ci_crs_35_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_35_gw3[ci_max], ci_crs_35_m3[ci_max], ci_crs_35_gw4[ci_max], ci_crs_35_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_35_gw2[ci_max], ci_crs_35_m2[ci_max], ci_crs_35_gw3[ci_max], ci_crs_35_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_35_gw1[ci_max], ci_crs_35_m1[ci_max], ci_crs_35_gw2[ci_max], ci_crs_35_m2[ci_max], total_weight_t)
			end
			
			econ_spd = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			if total_weight_t > ci_crs_41_gw5[ci_min] then
				econ_spd2 = ci_crs_41_m5[ci_min]
			elseif total_weight_t > ci_crs_41_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_41_gw4[ci_min], ci_crs_41_m4[ci_min], ci_crs_41_gw5[ci_min], ci_crs_41_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_41_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_41_gw3[ci_min], ci_crs_41_m3[ci_min], ci_crs_41_gw4[ci_min], ci_crs_41_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_41_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_41_gw2[ci_min], ci_crs_41_m2[ci_min], ci_crs_41_gw3[ci_min], ci_crs_41_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_41_gw1[ci_min], ci_crs_41_m1[ci_min], ci_crs_41_gw2[ci_min], ci_crs_41_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_41_gw5[ci_max] then
				econ_spd3 = ci_crs_41_m5[ci_max]
			elseif total_weight_t > ci_crs_41_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_41_gw4[ci_max], ci_crs_41_m4[ci_max], ci_crs_41_gw5[ci_max], ci_crs_41_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_41_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_41_gw3[ci_max], ci_crs_41_m3[ci_max], ci_crs_41_gw4[ci_max], ci_crs_41_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_41_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_41_gw2[ci_max], ci_crs_41_m2[ci_max], ci_crs_41_gw3[ci_max], ci_crs_41_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_41_gw1[ci_max], ci_crs_41_m1[ci_max], ci_crs_41_gw2[ci_max], ci_crs_41_m2[ci_max], total_weight_t)
			end
			
			econ_spd4 = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			econ_crz_spd_mach = B738_rescale(35000, econ_spd, 41000, econ_spd4, wind_crz_alt)
			
		elseif wind_crz_alt > 29000 then
			if total_weight_t > ci_crs_29_gw5[ci_min] then
				econ_spd2 = ci_crs_29_m5[ci_min]
			elseif total_weight_t > ci_crs_29_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_29_gw4[ci_min], ci_crs_29_m4[ci_min], ci_crs_29_gw5[ci_min], ci_crs_29_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_29_gw3[ci_min], ci_crs_29_m3[ci_min], ci_crs_29_gw4[ci_min], ci_crs_29_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_29_gw2[ci_min], ci_crs_29_m2[ci_min], ci_crs_29_gw3[ci_min], ci_crs_29_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_29_gw1[ci_min], ci_crs_29_m1[ci_min], ci_crs_29_gw2[ci_min], ci_crs_29_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_29_gw5[ci_max] then
				econ_spd3 = ci_crs_29_m5[ci_max]
			elseif total_weight_t > ci_crs_29_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_29_gw4[ci_max], ci_crs_29_m4[ci_max], ci_crs_29_gw5[ci_max], ci_crs_29_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_29_gw3[ci_max], ci_crs_29_m3[ci_max], ci_crs_29_gw4[ci_max], ci_crs_29_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_29_gw2[ci_max], ci_crs_29_m2[ci_max], ci_crs_29_gw3[ci_max], ci_crs_29_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_29_gw1[ci_max], ci_crs_29_m1[ci_max], ci_crs_29_gw2[ci_max], ci_crs_29_m2[ci_max], total_weight_t)
			end
			
			econ_spd = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			if total_weight_t > ci_crs_35_gw5[ci_min] then
				econ_spd2 = ci_crs_35_m5[ci_min]
			elseif total_weight_t > ci_crs_35_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_35_gw4[ci_min], ci_crs_35_m4[ci_min], ci_crs_35_gw5[ci_min], ci_crs_35_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_35_gw3[ci_min], ci_crs_35_m3[ci_min], ci_crs_35_gw4[ci_min], ci_crs_35_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_35_gw2[ci_min], ci_crs_35_m2[ci_min], ci_crs_35_gw3[ci_min], ci_crs_35_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_35_gw1[ci_min], ci_crs_35_m1[ci_min], ci_crs_35_gw2[ci_min], ci_crs_35_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_35_gw5[ci_max] then
				econ_spd3 = ci_crs_35_m5[ci_max]
			elseif total_weight_t > ci_crs_35_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_35_gw4[ci_max], ci_crs_35_m4[ci_max], ci_crs_35_gw5[ci_max], ci_crs_35_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_35_gw3[ci_max], ci_crs_35_m3[ci_max], ci_crs_35_gw4[ci_max], ci_crs_35_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_35_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_35_gw2[ci_max], ci_crs_35_m2[ci_max], ci_crs_35_gw3[ci_max], ci_crs_35_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_35_gw1[ci_max], ci_crs_35_m1[ci_max], ci_crs_35_gw2[ci_max], ci_crs_35_m2[ci_max], total_weight_t)
			end
			
			econ_spd4 = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			econ_crz_spd_mach = B738_rescale(29000, econ_spd, 35000, econ_spd4, wind_crz_alt)
			
		elseif wind_crz_alt > 20000 then
			if total_weight_t > ci_crs_20_gw5[ci_min] then
				econ_spd2 = ci_crs_20_m5[ci_min]
			elseif total_weight_t > ci_crs_20_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_20_gw4[ci_min], ci_crs_20_m4[ci_min], ci_crs_20_gw5[ci_min], ci_crs_20_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_20_gw3[ci_min], ci_crs_20_m3[ci_min], ci_crs_20_gw4[ci_min], ci_crs_20_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_20_gw2[ci_min], ci_crs_20_m2[ci_min], ci_crs_20_gw3[ci_min], ci_crs_20_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_20_gw1[ci_min], ci_crs_20_m1[ci_min], ci_crs_20_gw2[ci_min], ci_crs_20_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_20_gw5[ci_max] then
				econ_spd3 = ci_crs_20_m5[ci_max]
			elseif total_weight_t > ci_crs_20_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_20_gw4[ci_max], ci_crs_20_m4[ci_max], ci_crs_20_gw5[ci_max], ci_crs_20_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_20_gw3[ci_max], ci_crs_20_m3[ci_max], ci_crs_20_gw4[ci_max], ci_crs_20_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_20_gw2[ci_max], ci_crs_20_m2[ci_max], ci_crs_20_gw3[ci_max], ci_crs_20_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_20_gw1[ci_max], ci_crs_20_m1[ci_max], ci_crs_20_gw2[ci_max], ci_crs_20_m2[ci_max], total_weight_t)
			end
			
			econ_spd = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			if total_weight_t > ci_crs_29_gw5[ci_min] then
				econ_spd2 = ci_crs_29_m5[ci_min]
			elseif total_weight_t > ci_crs_29_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_29_gw4[ci_min], ci_crs_29_m4[ci_min], ci_crs_29_gw5[ci_min], ci_crs_29_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_29_gw3[ci_min], ci_crs_29_m3[ci_min], ci_crs_29_gw4[ci_min], ci_crs_29_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_29_gw2[ci_min], ci_crs_29_m2[ci_min], ci_crs_29_gw3[ci_min], ci_crs_29_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_29_gw1[ci_min], ci_crs_29_m1[ci_min], ci_crs_29_gw2[ci_min], ci_crs_29_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_29_gw5[ci_max] then
				econ_spd3 = ci_crs_29_m5[ci_max]
			elseif total_weight_t > ci_crs_29_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_29_gw4[ci_max], ci_crs_29_m4[ci_max], ci_crs_29_gw5[ci_max], ci_crs_29_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_29_gw3[ci_max], ci_crs_29_m3[ci_max], ci_crs_29_gw4[ci_max], ci_crs_29_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_29_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_29_gw2[ci_max], ci_crs_29_m2[ci_max], ci_crs_29_gw3[ci_max], ci_crs_29_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_29_gw1[ci_max], ci_crs_29_m1[ci_max], ci_crs_29_gw2[ci_max], ci_crs_29_m2[ci_max], total_weight_t)
			end
			
			econ_spd4 = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			econ_crz_spd_mach = B738_rescale(20000, econ_spd, 29000, econ_spd4, wind_crz_alt)
			
		elseif wind_crz_alt > 10000 then
			if total_weight_t > ci_crs_10_gw5[ci_min] then
				econ_spd2 = ci_crs_10_m5[ci_min]
			elseif total_weight_t > ci_crs_10_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_10_gw4[ci_min], ci_crs_10_m4[ci_min], ci_crs_10_gw5[ci_min], ci_crs_10_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_10_gw3[ci_min], ci_crs_10_m3[ci_min], ci_crs_10_gw4[ci_min], ci_crs_10_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_10_gw2[ci_min], ci_crs_10_m2[ci_min], ci_crs_10_gw3[ci_min], ci_crs_10_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_10_gw1[ci_min], ci_crs_10_m1[ci_min], ci_crs_10_gw2[ci_min], ci_crs_10_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_10_gw5[ci_max] then
				econ_spd3 = ci_crs_10_m5[ci_max]
			elseif total_weight_t > ci_crs_10_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_10_gw4[ci_max], ci_crs_10_m4[ci_max], ci_crs_10_gw5[ci_max], ci_crs_10_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_10_gw3[ci_max], ci_crs_10_m3[ci_max], ci_crs_10_gw4[ci_max], ci_crs_10_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_10_gw2[ci_max], ci_crs_10_m2[ci_max], ci_crs_10_gw3[ci_max], ci_crs_10_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_10_gw1[ci_max], ci_crs_10_m1[ci_max], ci_crs_10_gw2[ci_max], ci_crs_10_m2[ci_max], total_weight_t)
			end
			
			econ_spd = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			if total_weight_t > ci_crs_20_gw5[ci_min] then
				econ_spd2 = ci_crs_20_m5[ci_min]
			elseif total_weight_t > ci_crs_20_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_20_gw4[ci_min], ci_crs_20_m4[ci_min], ci_crs_20_gw5[ci_min], ci_crs_20_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_20_gw3[ci_min], ci_crs_20_m3[ci_min], ci_crs_20_gw4[ci_min], ci_crs_20_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_20_gw2[ci_min], ci_crs_20_m2[ci_min], ci_crs_20_gw3[ci_min], ci_crs_20_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_20_gw1[ci_min], ci_crs_20_m1[ci_min], ci_crs_20_gw2[ci_min], ci_crs_20_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_20_gw5[ci_max] then
				econ_spd3 = ci_crs_20_m5[ci_max]
			elseif total_weight_t > ci_crs_20_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_20_gw4[ci_max], ci_crs_20_m4[ci_max], ci_crs_29_gw5[ci_max], ci_crs_20_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_20_gw3[ci_max], ci_crs_20_m3[ci_max], ci_crs_29_gw4[ci_max], ci_crs_20_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_20_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_20_gw2[ci_max], ci_crs_20_m2[ci_max], ci_crs_29_gw3[ci_max], ci_crs_20_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_20_gw1[ci_max], ci_crs_20_m1[ci_max], ci_crs_29_gw2[ci_max], ci_crs_20_m2[ci_max], total_weight_t)
			end
			
			econ_spd4 = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
			econ_crz_spd_mach = B738_rescale(10000, econ_spd, 20000, econ_spd4, wind_crz_alt)
			
		else
			if total_weight_t > ci_crs_10_gw5[ci_min] then
				econ_spd2 = ci_crs_10_m5[ci_min]
			elseif total_weight_t > ci_crs_10_gw4[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_10_gw4[ci_min], ci_crs_10_m4[ci_min], ci_crs_10_gw5[ci_min], ci_crs_10_m5[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw3[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_10_gw3[ci_min], ci_crs_10_m3[ci_min], ci_crs_10_gw4[ci_min], ci_crs_10_m4[ci_min], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw2[ci_min] then
				econ_spd2 = B738_rescale(ci_crs_10_gw2[ci_min], ci_crs_10_m2[ci_min], ci_crs_10_gw3[ci_min], ci_crs_10_m3[ci_min], total_weight_t)
			else
				econ_spd2 = B738_rescale(ci_crs_10_gw1[ci_min], ci_crs_10_m1[ci_min], ci_crs_10_gw2[ci_min], ci_crs_10_m2[ci_min], total_weight_t)
			end
			
			if total_weight_t > ci_crs_10_gw5[ci_max] then
				econ_spd3 = ci_crs_10_m5[ci_max]
			elseif total_weight_t > ci_crs_10_gw4[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_10_gw4[ci_max], ci_crs_10_m4[ci_max], ci_crs_10_gw5[ci_max], ci_crs_10_m5[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw3[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_10_gw3[ci_max], ci_crs_10_m3[ci_max], ci_crs_10_gw4[ci_max], ci_crs_10_m4[ci_max], total_weight_t)
			elseif total_weight_t > ci_crs_10_gw2[ci_max] then
				econ_spd3 = B738_rescale(ci_crs_10_gw2[ci_max], ci_crs_10_m2[ci_max], ci_crs_10_gw3[ci_max], ci_crs_10_m3[ci_max], total_weight_t)
			else
				econ_spd3 = B738_rescale(ci_crs_10_gw1[ci_max], ci_crs_10_m1[ci_max], ci_crs_10_gw2[ci_max], ci_crs_10_m2[ci_max], total_weight_t)
			end
			
			econ_crz_spd_mach = B738_rescale(ci_crs_tab[ci_min], econ_spd2, ci_crs_tab[ci_max], econ_spd3, ci)
			
		end
		
		if wind_crz_alt > 35000 then
			--35000
			if total_weight_t > 180 then
				econ_spd = 0.790
			elseif total_weight_t > 165 then
				econ_spd = B738_rescale(165, 0.792, 180, 0.790, total_weight_t)
			elseif total_weight_t > 160 then
				econ_spd = B738_rescale(160, 0.790, 165, 0.792, total_weight_t)
			elseif total_weight_t > 135 then
				econ_spd = B738_rescale(135, 0.758, 160, 0.790, total_weight_t)
			elseif total_weight_t > 115 then
				econ_spd = B738_rescale(115, 0.724, 135, 0.758, total_weight_t)
			elseif total_weight_t > 95 then
				econ_spd = B738_rescale(95, 0.658, 115, 0.724, total_weight_t)
			else
				econ_spd = 0.618
			end
			
			--41000
			if total_weight_t > 140 then
				econ_spd4 = 0.784
			elseif total_weight_t > 125 then
				econ_spd4 = B738_rescale(125, 0.792, 140, 0.784, total_weight_t)
			elseif total_weight_t > 115 then
				econ_spd4 = B738_rescale(115, 0.786, 125, 0.792, total_weight_t)
			elseif total_weight_t > 105 then
				econ_spd4 = B738_rescale(105, 0.772, 115, 0.786, total_weight_t)
			elseif total_weight_t > 95 then
				econ_spd4 = B738_rescale(95, 0.752, 105, 0.772, total_weight_t)
			elseif total_weight_t > 85 then
				econ_spd4 = B738_rescale(85, 0.720, 95, 0.752, total_weight_t)
			else
				econ_spd4 = 0.698
			end
			
			lrc_crz_spd_mach = B738_rescale(35000, econ_spd, 41000, econ_spd4, wind_crz_alt)
		
		elseif wind_crz_alt > 29000 then
			--29000
			if total_weight_t > 180 then
				econ_spd = 0.790
			elseif total_weight_t > 155 then
				econ_spd = B738_rescale(155, 0.730, 180, 0.790, total_weight_t)
			elseif total_weight_t > 120 then
				econ_spd = B738_rescale(120, 0.645, 155, 0.730, total_weight_t)
			elseif total_weight_t > 95 then
				econ_spd = B738_rescale(95, 0.600, 120, 0.645, total_weight_t)
			else
				econ_spd = 0.560
			end
			
			--35000
			if total_weight_t > 180 then
				econ_spd4 = 0.790
			elseif total_weight_t > 165 then
				econ_spd4 = B738_rescale(165, 0.792, 180, 0.790, total_weight_t)
			elseif total_weight_t > 160 then
				econ_spd4 = B738_rescale(160, 0.790, 165, 0.792, total_weight_t)
			elseif total_weight_t > 135 then
				econ_spd4 = B738_rescale(135, 0.758, 160, 0.790, total_weight_t)
			elseif total_weight_t > 115 then
				econ_spd4 = B738_rescale(115, 0.724, 135, 0.758, total_weight_t)
			elseif total_weight_t > 95 then
				econ_spd4 = B738_rescale(95, 0.658, 115, 0.724, total_weight_t)
			else
				econ_spd4 = 0.618
			end
			
			lrc_crz_spd_mach = B738_rescale(29000, econ_spd, 35000, econ_spd4, wind_crz_alt)
		
		elseif wind_crz_alt > 20000 then
			--20000
			if total_weight_t > 180 then
				econ_spd = 0.650
			elseif total_weight_t > 165 then
				econ_spd = B738_rescale(165, 0.630, 180, 0.650, total_weight_t)
			elseif total_weight_t > 140 then
				econ_spd = B738_rescale(140, 0.600, 165, 0.630, total_weight_t)
			elseif total_weight_t > 120 then
				econ_spd = B738_rescale(120, 0.565, 140, 0.600, total_weight_t)
			elseif total_weight_t > 100 then
				econ_spd = B738_rescale(100, 0.515, 120, 0.565, total_weight_t)
			else
				econ_spd = 0.460
			end
			
			--29000
			if total_weight_t > 180 then
				econ_spd4 = 0.790
			elseif total_weight_t > 155 then
				econ_spd4 = B738_rescale(155, 0.730, 180, 0.790, total_weight_t)
			elseif total_weight_t > 120 then
				econ_spd4 = B738_rescale(120, 0.645, 155, 0.730, total_weight_t)
			elseif total_weight_t > 95 then
				econ_spd4 = B738_rescale(95, 0.600, 120, 0.645, total_weight_t)
			else
				econ_spd4 = 0.560
			end
			
			lrc_crz_spd_mach = B738_rescale(20000, econ_spd, 29000, econ_spd4, wind_crz_alt)
		
		elseif wind_crz_alt > 10000 then
			--10000
			if total_weight_t > 180 then
				econ_spd = 0.560
			elseif total_weight_t > 160 then
				econ_spd = B738_rescale(160, 0.530, 180, 0.560, total_weight_t)
			elseif total_weight_t > 135 then
				econ_spd = B738_rescale(135, 0.485, 160, 0.530,  total_weight_t)
			elseif total_weight_t > 105 then
				econ_spd = B738_rescale(105, 0.430, 135, 0.485, total_weight_t)
			else
				econ_spd = 0.375
			end
			
			--20000
			if total_weight_t > 180 then
				econ_spd4 = 0.650
			elseif total_weight_t > 165 then
				econ_spd4 = B738_rescale(165, 0.630, 180, 0.650, total_weight_t)
			elseif total_weight_t > 140 then
				econ_spd4 = B738_rescale(140, 0.600, 165, 0.630, total_weight_t)
			elseif total_weight_t > 120 then
				econ_spd4 = B738_rescale(120, 0.565, 140, 0.600, total_weight_t)
			elseif total_weight_t > 100 then
				econ_spd4 = B738_rescale(100, 0.515, 120, 0.565, total_weight_t)
			else
				econ_spd4 = 0.460
			end
			
			lrc_crz_spd_mach = B738_rescale(10000, econ_spd, 20000, econ_spd4, wind_crz_alt)
		
		else
			--10000
			if total_weight_t > 180 then
				econ_spd = 0.560
			elseif total_weight_t > 160 then
				econ_spd = B738_rescale(160, 0.530, 180, 0.560, total_weight_t)
			elseif total_weight_t > 135 then
				econ_spd = B738_rescale(135, 0.485, 160, 0.530, total_weight_t)
			elseif total_weight_t > 105 then
				econ_spd = B738_rescale(105, 0.430, 135, 0.485, total_weight_t)
			else
				econ_spd = 0.375
			end
			
			lrc_crz_spd_mach = econ_spd
		end
		
		econ_clb_spd_mach = econ_crz_spd_mach
		
		econ_des_spd_mach = econ_crz_spd_mach
		
		econ_clb_spd = math.floor(econ_clb_spd + 0.5)
		econ_clb_spd_mach = math.floor(econ_clb_spd_mach * 1000 + 0.5) / 1000
		econ_crz_spd = math.floor(econ_crz_spd + 0.5)
		econ_crz_spd_mach = math.floor(econ_crz_spd_mach * 1000 + 0.5) / 1000
		econ_des_spd = math.floor(econ_des_spd + 0.5)
		econ_des_spd_mach = math.floor(econ_des_spd_mach * 1000 + 0.5) / 1000
		
		lrc_crz_spd = math.floor(lrc_crz_spd + 0.5)
		lrc_crz_spd_mach = math.floor(lrc_crz_spd_mach * 1000 + 0.5) / 1000
	end
	
	-- check clb spd
	local min_kts = tonumber(clb_min_kts)
	if min_kts == nil then
		min_kts = 100
	elseif min_kts < 100 then
		min_kts = 100
	elseif min_kts > 340 then
		min_kts = 340
	end
	local min_mach = tonumber(clb_min_mach)
	if min_mach == nil then
		min_mach = 0.400
	elseif min_mach < 400 then
		min_mach = 0.400
	elseif min_mach > 820 then
		min_mach = 0.820
	else
		min_mach = min_mach / 1000
	end
	local max_kts = tonumber(clb_max_kts)
	if max_kts == nil then
		max_kts = 340
	elseif max_kts < 100 then
		max_kts = 100
	elseif max_kts > 340 then
		max_kts = 340
	end
	local max_mach = tonumber(clb_max_mach)
	if max_mach == nil then
		max_mach = 0.820
	elseif max_mach < 400 then
		max_mach = 0.400
	elseif max_mach > 820 then
		max_mach = 0.820
	else
		max_mach = max_mach / 1000
	end
	if econ_clb_spd < min_kts then
		econ_clb_spd = min_kts
	elseif econ_clb_spd > max_kts then
		econ_clb_spd = max_kts
	end
	if econ_clb_spd_mach < min_mach then
		econ_clb_spd_mach = min_mach
	elseif econ_clb_spd_mach > max_mach then
		econ_clb_spd_mach = max_mach
	end
	
	-- check crz spd
	min_kts = tonumber(crz_min_kts)
	if min_kts == nil then
		min_kts = 100
	elseif min_kts < 100 then
		min_kts = 100
	elseif min_kts > 340 then
		min_kts = 340
	end
	min_mach = tonumber(crz_min_mach)
	if min_mach == nil then
		min_mach = 0.400
	elseif min_mach < 400 then
		min_mach = 0.400
	elseif min_mach > 820 then
		min_mach = 0.820
	else
		min_mach = min_mach / 1000
	end
	max_kts = tonumber(crz_max_kts)
	if max_kts == nil then
		max_kts = 340
	elseif max_kts < 100 then
		max_kts = 100
	elseif max_kts > 340 then
		max_kts = 340
	end
	max_mach = tonumber(crz_max_mach)
	if max_mach == nil then
		max_mach = 0.820
	elseif max_mach < 400 then
		max_mach = 0.400
	elseif max_mach > 820 then
		max_mach = 0.820
	else
		max_mach = max_mach / 1000
	end
	if econ_crz_spd < min_kts then
		econ_crz_spd = min_kts
	elseif econ_crz_spd > max_kts then
		econ_crz_spd = max_kts
	end
	if econ_crz_spd_mach < min_mach then
		econ_crz_spd_mach = min_mach
	elseif econ_crz_spd_mach > max_mach then
		econ_crz_spd_mach = max_mach
	end
	if lrc_crz_spd < min_kts then
		lrc_crz_spd = min_kts
	elseif econ_crz_spd > max_kts then
		lrc_crz_spd = max_kts
	end
	if lrc_crz_spd_mach < min_mach then
		lrc_crz_spd_mach = min_mach
	elseif lrc_crz_spd_mach > max_mach then
		lrc_crz_spd_mach = max_mach
	end
	
	-- check des spd
	min_kts = tonumber(des_min_kts)
	if min_kts == nil then
		min_kts = 100
	elseif min_kts < 100 then
		min_kts = 100
	elseif min_kts > 340 then
		min_kts = 340
	end
	min_mach = tonumber(des_min_mach)
	if min_mach == nil then
		min_mach = 0.400
	elseif min_mach < 400 then
		min_mach = 0.400
	elseif min_mach > 820 then
		min_mach = 0.820
	else
		min_mach = min_mach / 1000
	end
	max_kts = tonumber(des_max_kts)
	if max_kts == nil then
		max_kts = 340
	elseif max_kts < 100 then
		max_kts = 100
	elseif max_kts > 340 then
		max_kts = 340
	end
	max_mach = tonumber(des_max_mach)
	if max_mach == nil then
		max_mach = 0.820
	elseif max_mach < 400 then
		max_mach = 0.400
	elseif max_mach > 820 then
		max_mach = 0.820
	else
		max_mach = max_mach / 1000
	end
	if econ_des_spd < min_kts then
		econ_des_spd = min_kts
	elseif econ_des_spd > max_kts then
		econ_des_spd = max_kts
	end
	if econ_des_spd_mach < min_mach then
		econ_des_spd_mach = min_mach
	elseif econ_des_spd_mach > max_mach then
		econ_des_spd_mach = max_mach
	end
	
	B738DR_fmc_climb_speed_mach = econ_clb_spd_mach		-- temporary
	B738DR_fmc_climb_speed = econ_clb_spd				-- temporary
	B738DR_fmc_cruise_speed_mach = econ_crz_spd_mach	-- temporary
	B738DR_fmc_cruise_speed = econ_crz_spd				-- temporary
	B738DR_fmc_descent_speed_mach = econ_des_spd_mach	-- temporary
	B738DR_fmc_descent_speed = econ_des_spd				-- temporary
	
	B738DR_fmc_descent_speed_mach_init = econ_des_spd_mach
	B738DR_fmc_descent_speed_init = econ_des_spd
	
	B738DR_climb_mode = 0
	B738DR_cruise_mode = 0
	B738DR_descent_mode = 0
	
	fmc_cruise_mode_mod = B738DR_cruise_mode
	fmc_descent_mode_mod = B738DR_descent_mode
	fmc_climb_mode_mod = B738DR_climb_mode
	
end

function read_axis()
	
	local delta_axis = 0
	
	if B738DR_cfy_enable ~= 0 then
		B738DR_cust_axis_speedbrake = B738DR_cust_axis_speedbrake_raw
	elseif B738DR_cust_axis_speedbrake_mapped >= 0 and B738DR_cust_axis_speedbrake_mapped < 500 then
		delta_axis = simDR_axis_max[B738DR_cust_axis_speedbrake_mapped] - simDR_axis_min[B738DR_cust_axis_speedbrake_mapped]
		if delta_axis ~= 0 then
			B738DR_cust_axis_speedbrake = (simDR_axis[B738DR_cust_axis_speedbrake_mapped] - simDR_axis_min[B738DR_cust_axis_speedbrake_mapped]) / delta_axis
			if B738DR_cust_axis_speedbrake_reverse ~= 0 then
				B738DR_cust_axis_speedbrake = 1 - B738DR_cust_axis_speedbrake
			end
		end
	end
	
	if B738DR_cfy_enable ~= 0 then
		B738DR_cust_axis_flaps = B738DR_cust_axis_flaps_raw
	elseif B738DR_cust_axis_flaps_mapped >= 0 and B738DR_cust_axis_flaps_mapped < 500 then
		delta_axis = simDR_axis_max[B738DR_cust_axis_flaps_mapped] - simDR_axis_min[B738DR_cust_axis_flaps_mapped]
		if delta_axis ~= 0 then
			B738DR_cust_axis_flaps = (simDR_axis[B738DR_cust_axis_flaps_mapped] - simDR_axis_min[B738DR_cust_axis_flaps_mapped]) / delta_axis
			if B738DR_cust_axis_flaps_reverse ~= 0 then
				B738DR_cust_axis_flaps = 1 - B738DR_cust_axis_flaps
			end
		end
	end
	
	if B738DR_cust_axis_landgear_mapped >= 0 and B738DR_cust_axis_landgear_mapped < 500 then
		delta_axis = simDR_axis_max[B738DR_cust_axis_landgear_mapped] - simDR_axis_min[B738DR_cust_axis_landgear_mapped]
		if delta_axis ~= 0 then
			B738DR_cust_axis_landgear = (simDR_axis[B738DR_cust_axis_landgear_mapped] - simDR_axis_min[B738DR_cust_axis_landgear_mapped]) / delta_axis
			if B738DR_cust_axis_landgear_reverse ~= 0 then
				B738DR_cust_axis_landgear = 1 - B738DR_cust_axis_landgear
			end
		end
	end
	
	if B738DR_cust_axis_mixture1_mapped >= 0 and B738DR_cust_axis_mixture1_mapped < 500 then
		delta_axis = simDR_axis_max[B738DR_cust_axis_mixture1_mapped] - simDR_axis_min[B738DR_cust_axis_mixture1_mapped]
		if delta_axis ~= 0 then
			B738DR_cust_axis_mixture1 = (simDR_axis[B738DR_cust_axis_mixture1_mapped] - simDR_axis_min[B738DR_cust_axis_mixture1_mapped]) / delta_axis
			if B738DR_cust_axis_mixture1_reverse ~= 0 then
				B738DR_cust_axis_mixture1 = 1 - B738DR_cust_axis_mixture1
			end
		end
	end
	
	if B738DR_cust_axis_mixture2_mapped >= 0 and B738DR_cust_axis_mixture2_mapped < 500 then
		delta_axis = simDR_axis_max[B738DR_cust_axis_mixture2_mapped] - simDR_axis_min[B738DR_cust_axis_mixture2_mapped]
		if delta_axis ~= 0 then
			B738DR_cust_axis_mixture2 = (simDR_axis[B738DR_cust_axis_mixture2_mapped] - simDR_axis_min[B738DR_cust_axis_mixture2_mapped]) / delta_axis
			if B738DR_cust_axis_mixture2_reverse ~= 0 then
				B738DR_cust_axis_mixture2 = 1 - B738DR_cust_axis_mixture2
			end
		end
	end
	
end

function probe_tat()
	-- probe TAT
	local tat_degc_tgt = 0
	local tat_degc_tgt2 = 0
	local tat_rate = 3.2
	local delta_tat = 0
	local tat_speed = math.min(simDR_ground_spd, 40)
	tat_speed = math.max(tat_speed, 5)
	
	if B738DR_aspirated_probe == 0 then
		if simDR_stat_pitot_capt == 0 then
			tat_degc_tgt = simDR_TAT
		else
			if simDR_engine_on[0] == 1 then
				delta_tat = 99 - math.min(simDR_TAT, 99)
				tat_degc_tgt2 = simDR_TAT + (delta_tat * 0.35)
			else
				tat_degc_tgt2 = 99
			end
			tat_degc_tgt = B738_rescale(5, tat_degc_tgt2, 40, simDR_TAT, tat_speed)
			if tat_degc_tgt == 99 then
				tat_rate = 0.12	--0.4
			end
		end
	else
		tat_degc_tgt = simDR_TAT
		tat_degc = tat_degc_tgt
	end
	tat_degc = B738_set_animation_rate(tat_degc, tat_degc_tgt, -50, 99, tat_rate)
	B738DR_TAT = tat_degc
end

function B738_fmc_on()
	-- if simDR_electrical_bus_volts[0] > 10 and simDR_bus_battery_on[0] == 1 then
	-- if B738DR_ac_stdbus_status ~= 0 or B738DR_batbus_status ~= 0 then
	if B738DR_batbus_status ~= 0 then
		fmc_enable = 1
		if reset_fmc == 1 then
			B738_init2()
			B738_init3()
			B738_init4()
			custom_wpt_num = 0
			custom_wpt  = {}
		end
		reset_fmc = 0
	else
		fmc_enable = 0
		if reset_fmc == 0 then
			reset_fmc = 1
			B738_init2()
			custom_wpt_num = 0
			custom_wpt  = {}
		end
	end
	
	
	local align_source = 0
	
	fmc_align = 0
	if fmc_enable == 1 then
		
		if B738DR_irs_source == -1 then
			if B738DR_irs_left_mode == 2 then
				fmc_align = 1
				align_source = align_source + 1
			end
		elseif B738DR_irs_source == 0 then
			if B738DR_irs_left_mode == 2 then
				fmc_align = 1
				align_source = align_source + 1
			end
			if B738DR_irs_right_mode == 2 then
				fmc_align = 1
				align_source = align_source + 1
			end
		else
			if B738DR_irs_right_mode == 2 then
				fmc_align = 1
				align_source = align_source + 1
			end
		end
		if align_source ~= 0 then
			if simDR_gps_fail == 0 then
				align_source = align_source + 1
			end
			if simDR_gps2_fail == 0 then
				align_source = align_source + 1
			end
		end
	end
	
	if align_source == 0 then
			align_anp_min = 19000
			align_anp_max = 20000
		if align_source ~= align_source_old then
			align_anp_time = 0
			msg_rnp_alert = 0
		end
	elseif align_source == 1 then
			align_anp_min = 1060
			align_anp_max = 1820
		if align_source ~= align_source_old then
			align_anp_time = 0
		end
	elseif align_source == 2 then
			align_anp_min = 976
			align_anp_max = 990
		if align_source ~= align_source_old then
			align_anp_time = 0
		end
	elseif align_source == 3 then
			align_anp_min = 130
			align_anp_max = 380
		if align_source ~= align_source_old then
			align_anp_time = 0
		end
	else
			align_anp_min = 0
			align_anp_max = 60
		if align_source ~= align_source_old then
			align_anp_time = 0
		end
	end
	align_source_old = align_source
	
	if align_anp_time == 0 then
		local int, frac = math.modf(os.clock())
		local seed = math.random(1, frac*1000.0)
		math.randomseed(seed)
		align_anp = math.random(align_anp_min, align_anp_max) + math.random()
	end
	align_anp_time = align_anp_time + SIM_PERIOD
	if align_anp_time > 8 then
		align_anp_time = 0
	end
	
	B738DR_anp = B738_set_anim_value((B738DR_anp * 1000), align_anp, 0, 20000, 0.11) / 1000
	if B738DR_anp > 9.99 then
		B738DR_anp = 9.99
	end
	
	-- RNP
	if B738DR_irs_nd_allign_12 ~= 0 then
		if B738DR_irs_pos_set == "*****.*******.*" then
			B738DR_anp = 10
			B738DR_rnp = 10
		else
			B738DR_anp = 9.99
			B738DR_rnp = 1
		end
	elseif legs_num > 1 and offset ~= 0 and offset <= legs_num then
		local ii = tonumber(string.sub(legs_data[offset][35], 1, 2))
		if ii == nil then
			if offset >= first_app_idx and first_app_idx ~= 0 then
				if first_star_idx == 0 and offset == first_app_idx then
					-- route
					ii = 2
				else
					-- approach
					if app_rnp == "" then
						if string.sub(des_app, 1, 1) == "R" then
							ii = 0.3
						else
							ii = 1
						end
					else
						ii = app_rnp_num
					end
				end
			elseif offset > first_star_idx and first_star_idx ~= 0 then
				-- arrival
				ii = 1
			elseif offset <= last_sid_idx and last_sid_idx ~= 0 then
				-- departure
				ii = 1
				
			else
				ii = 2
			end
		else
			if B738DR_flight_phase > 5 and B738DR_flight_phase < 8 and app_rnp ~= "" then
				ii = app_rnp_num
			else
				if string.sub(legs_data[offset][35], 3, 3) == "1" then
					ii = ii / 10
				elseif string.sub(legs_data[offset][35], 3, 3) == "2" then
					ii = ii / 100
				elseif string.sub(legs_data[offset][35], 3, 3) == "3" then
					ii = ii / 1000
				end
			end
		end
		B738DR_rnp = ii
	else
		B738DR_rnp = 1
	end
	
	if B738DR_anp > B738DR_rnp then
		B738DR_anp_rnp_colour = 1
	else
		B738DR_anp_rnp_colour = 0
	end
end

function B738_vnav_desc_spd()

	vnav_desc_spd_disable = 0
	
	-- if B738DR_autopilot_vnav_status == 1 then
		if legs_num > 0 and offset > 2 and offset < (legs_num + 1) then
			if rte12_act == 0 then
				if legs_data[offset-1][9] == des_app or legs_data[offset-1][9] == des_app_tns or offset > ed_found then
					vnav_desc_spd_disable = 1
				end
			elseif rte12_act == 1 then
				if legs_data[offset-1][9] == des_app7 or legs_data[offset-1][9] == des_app_tns7 or offset > ed_found then
					vnav_desc_spd_disable = 1
				end
			end
		end
		
		if B738DR_flight_phase > 5 and B738DR_flight_phase < 8 then
			vnav_desc_spd_disable = 1
		elseif simDR_flaps_ratio > 0 then
			vnav_desc_spd_disable = 1
		end
	-- else
		-- vnav_desc_spd_disable = 1
	-- end
	
	
	if vnav_desc_spd_disable == 1 then
		if B738DR_descent_mode == 1 then
			B738DR_descent_mode = 0
			B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
			B738DR_fmc_descent_speed = econ_des_spd				-- temporary
			fmc_descent_mode_mod = 0
		elseif B738DR_descent_mode == 3 then
			B738DR_descent_mode = 2
			B738DR_fmc_descent_speed_mach = econ_des_spd_mach		-- temporary
			B738DR_fmc_descent_speed = econ_des_spd				-- temporary
			fmc_descent_mode_mod = 2
		end
	end
	
	if change_desc_to_path == 0 then
		B738DR_vnav_desc_spd_disable = vnav_desc_spd_disable
	else
		B738DR_vnav_desc_spd_disable = 1
		change_desc_to_path = 0
	end
	if legs_num > 0 and offset > 0 and offset <= legs_num then
		B738DR_act_wpt_gp = legs_data[offset][20]
	end
	
end

function B738_pre_flt_status()
	
	pre_flt_pos_init = 1
	pre_flt_rte = 1
	pre_flt_perf_init = 1
	pre_flt_dep = 1
	
	if irs_pos == "*****.*******.*" then
		pre_flt_pos_init = 0
	end
	if legs_num == 0 then
		pre_flt_rte = 0
	end
	if gw == "***.*" or zfw == "***.*" or reserves == "**.*" or cost_index == "***" or crz_alt == "*****" then
		pre_flt_perf_init = 0
	end
	
	if rte12_act == 0 then
		if ref_rwy == "-----" then
			pre_flt_dep = 0
		end
	elseif rte12_act == 1 then
		if ref_rwy7 == "-----" then
			pre_flt_dep = 0
		end
	end
end


function B738_wind()
	
	head_wind = simDR_wind_spd * math.cos(math.rad(Angle180(simDR_wind_hdg))-math.rad(simDR_position_mag_psi))
	cross_wind = simDR_wind_spd * math.sin(math.rad(Angle180(simDR_wind_hdg))-math.rad(simDR_position_mag_psi))
	
	local wind_show = B738DR_wind_show
	if simDR_wind_spd >= 6 then
		wind_show = 1
	end
	if simDR_wind_spd < 4 then
		wind_show = 0
	end
	if simDR_TAS < 51.9859 then		-- 101 kts
		wind_show = 2
	end
	B738DR_wind_show = wind_show
	
end

function record_data()
	if rec_mode == 1 then
		file_rec = io.open("Data_records.txt", "w")
		if file_rec == nil then
			entry = "Error recording"
			rec_mode = 0
		else
			rec_mode_sample = 0
			rec_mode = 2
		end
	elseif rec_mode == 2 then
		if rec_mode_sample == 0 then
			-- write data
			local rec_data = tostring(B738DR_yoke_roll) .. "," .. tostring(B738DR_yoke_pitch) .. "," .. tostring(simDR_roll_AHARS_deg) .. ","
			rec_data = rec_data .. tostring(simDR_ahars_pitch_deg_pilot) .. "," .. tostring(simDR_fdir_roll) .. "," .. tostring(simDR_fdir_pitch)
			rec_data = rec_data .. "\n"
			file_rec:write(rec_data)
		end
		rec_mode_sample = rec_mode_sample + SIM_PERIOD
		if rec_mode_sample > 1 then
			rec_mode_sample = 0
		end
	elseif rec_mode == 3 then
		file_rec:close()
		rec_mode = 0
	end
end

function fuel_tank_correct()
	local temp_coef = 0
	local delta_temp = 0
	if B738DR_fuel_temp_real < 15 then
		delta_temp = 15 - B738DR_fuel_temp_real
		temp_coef = 1 + (delta_temp * 0.00092)
	else
		delta_temp = B738DR_fuel_temp_real - 15
		temp_coef = 1 - (delta_temp * 0.00092)
	end
	local temp_wing_tank_kg2 = 3905 * temp_coef
	local temp_max_fuel_kg2 = 20828 * temp_coef
	
	local kk = (simDR_fuel_tank_weight_kg[0] + simDR_fuel_tank_weight_kg[1] + simDR_fuel_tank_weight_kg[2]) / 1000
	local kk2 = simDR_fuel_tank_weight_kg[0] + simDR_fuel_tank_weight_kg[1] + simDR_fuel_tank_weight_kg[2]
	
	local max_payload_kg = 79
	-- local max_payload_lb = 174.2
	local empty_kg = 41.4
	-- local empty_lb = 91.3
	local wing_tank_kg = temp_wing_tank_kg2		--3900
	local max_fuel_kg = temp_max_fuel_kg2 / 1000		--20.9
	if B738DR_b737_variant == 0 then
		-- 800
		max_payload_kg = 79
		empty_kg = 41.4
		wing_tank_kg = 3900
		max_fuel_kg = 20.9
	elseif B738DR_b737_variant == 1 then
		-- 900
		max_payload_kg = 79
		empty_kg = 41.4
		wing_tank_kg = 3900
		max_fuel_kg = 20.9
	elseif B738DR_b737_variant == 2 then
		-- 700
		max_payload_kg = 70
		empty_kg = 37.7
		wing_tank_kg = 3900
		max_fuel_kg = 20.9
	elseif B738DR_b737_variant == 2 then
		-- 600
		max_payload_kg = 65
		empty_kg = 36.4
		wing_tank_kg = 3900
		max_fuel_kg = 20.9
	elseif B738DR_b737_variant == 4 then
		-- 900ER
		max_payload_kg = 85
		empty_kg = 44.67
		wing_tank_kg = 3900
		max_fuel_kg = 23.8
	elseif B738DR_73x == 1 then
		-- 900U
		max_payload_kg = 85
		-- max_payload_lb = 187.7
		empty_kg = 44.67
		-- empty_lb = 93.6
		wing_tank_kg = 3920
	elseif B738DR_73x == 2 then
		-- 700U
		max_payload_kg = 70
		-- max_payload_lb = 154.5
		empty_kg = 37.6
		-- empty_lb = 83
		wing_tank_kg = 3900
	end
	
	local both_wing_kg = wing_tank_kg * 0.002
	local ll = (simDR_payload_weight / 1000) + kk + empty_kg
	
	if kk >= 1 and kk <= max_fuel_kg and ll <= max_payload_kg then	--20.5
		if kk > both_wing_kg then
			simDR_fuel_tank_weight_kg[0] = wing_tank_kg
			simDR_fuel_tank_weight_kg[2] = wing_tank_kg
			simDR_fuel_tank_weight_kg[1] = kk2 - (both_wing_kg * 1000)	--(kk - both_wing_kg) * 1000
		else
			simDR_fuel_tank_weight_kg[0] = kk2 / 2	--(kk / 2) * 1000
			simDR_fuel_tank_weight_kg[2] = kk2 / 2	--(kk / 2) * 1000
			simDR_fuel_tank_weight_kg[1] = 0
		end
	end
end

function gpu_test_reset()
	B738DR_gpu_test_enable = 0
end

function calc_mac(cg_input)
	
	local basic_arm = DEFAULT_ARM + cg_input		-- basic arm in meters
	local moment_empty = 41412.98 * basic_arm	--kgm
	local moment_payload = simDR_payload_weight * basic_arm	-- kgm
	local moment_main_tank = (simDR_fuel_tank_weight_kg[0] + simDR_fuel_tank_weight_kg[2]) * MAIN_TANK_ARM -- kgm
	local moment_center_tank = simDR_fuel_tank_weight_kg[1] * CENTER_TANK_ARM		--kgm
	local cg_in_m = moment_empty + moment_payload + moment_main_tank + moment_center_tank
	local tow = 41412.98 + simDR_payload_weight + simDR_fuel_tank_weight_kg[0] + simDR_fuel_tank_weight_kg[1] + simDR_fuel_tank_weight_kg[2]
	cg_in_m = cg_in_m / tow
	local n = ((cg_in_m - 15.92834) / 3.95732) * 100
	if n < 6 or n > 36 then
		n = 0
	end
	return n
	
end

function calc_zfw_mac(cg_input)
	
	local basic_arm = DEFAULT_ARM + cg_input		-- basic arm in meters
	local moment_empty = 41412.98 * basic_arm	--kgm
	local moment_payload = simDR_payload_weight * basic_arm	-- kgm
	local cg_in_m = moment_empty + moment_payload
	local tow = 41412.98 + simDR_payload_weight
	cg_in_m = cg_in_m / tow
	local n = ((cg_in_m - 15.92834) / 3.95732) * 100
	if n < 6 or n > 36 then
		n = 0
	end
	return n
	
end

function fmc_entry_alt()
	if page_cruise == 1 then
		entry = tostring(B738DR_mcp_alt_dial)
		if fmc1_msg_alt == 0 then
			fmc1_msg_alt = 1
		end
	end
	if page_cruise2 == 1 then
		entry2 = tostring(B738DR_mcp_alt_dial)
		if fmc2_msg_alt == 0 then
			fmc2_msg_alt = 1
		end
	end
end

function B738_fmc_alt_dial()
	
	if page_cruise == 1 or page_cruise2 == 1 then 
		if mcp_alt_dial_old ~= B738DR_mcp_alt_dial then
			if is_timer_scheduled(fmc_entry_alt) == true then
				stop_timer(fmc_entry_alt)
			end
			if is_timer_scheduled(fmc_entry_alt) == false then
				run_after_time(fmc_entry_alt, 2.5)
			end
		end
	else
		if is_timer_scheduled(fmc_entry_alt) == true then
			stop_timer(fmc_entry_alt)
		end
	end
	mcp_alt_dial_old = B738DR_mcp_alt_dial
	
end

function B738_init_vspd()
	v1 = "---"
	vr = "---"
	v2 = "---"
	flaps = "**"
	vref_15 = "   "
	vref_30 = "   "
	vref_40 = "   "
	flaps_app = "  "

	v1_set = "---"
	vr_set = "---"
	v2_set = "---"
	reset_fmc_act2 = 0
end

function B738_init2()
--menu_timer = 0

version = ""
file_navdata = 0
reverse_rte1 = 0
reverse_rte2 = 0
save_rte1 = 0
save_rte2 = 0
co_route_save1 = "************"
co_route_save2 = "************"

clr_repeat = 0
clr_repeat_time = 0

line0_l = ""
line1_l = ""
line2_l = ""
line3_l = ""
line4_l = ""
line5_l = ""
line6_l = ""
line0_s = ""
line1_s = ""
line2_s = ""
line3_s = ""
line4_s = ""
line5_s = ""
line6_s = ""
line1_x = ""
line2_x = ""
line3_x = ""
line4_x = ""
line5_x = ""
line6_x = ""
line0_inv = ""
line1_inv = ""
line2_inv = ""
line3_inv = ""
line4_inv = ""
line5_inv = ""
line6_inv = ""
line0_c = ""
line4_si = ""
line1_lx = ""
line6_lx = ""

max_page = 0
act_page = 0
act_page_old = 0
act_page_old2 = 0
display_update = 1

page_legs_step = 1
legs_step = 0
map_mode = 2
map_mode_old = 2

entry = ""
entry2 = ""

entry_wind_dir = ""
entry_wind_spd = ""

gw_act = "    "
irs_pos_set = "*****.*******.*"
irs2_pos_set = "*****.*******.*"
gps_right = "-----.-------.-"
gps_left = "-----.-------.-"
fmc_pos = "-----.-------.-"
last_pos_enable = 1

flt_num = "--------"
flt_num_x = "--------"

gw = "***.*"
gw_calc = "***.*"
gw_lbs = "***.*"
gw_kgs = "***.*"

zfw_calc = "---.-"
zfw_calc_lbs = "---.-"
zfw_calc_kgs = "---.-"

fuel_weight = "--.-"
fuel_weight_lbs = "--.-"
fuel_weight_kgs = "--.-"

plan_weight = "---.-"
plan_weight_kgs = "---.-"
plan_weight_lbs = "---.-"

plan_weight_mod = "---.-"
plan_weight_kgs_mod = "---.-"
plan_weight_lbs_mod = "---.-"

zfw = "***.*"
zfw_kgs = "***.*"
zfw_lbs = "***.*"

gw_mod = "***.*"
gw_lbs_mod = "***.*"
gw_kgs_mod = "***.*"
zfw_mod = "***.*"
zfw_kgs_mod = "***.*"
zfw_lbs_mod = "***.*"

reserves = "**.*"
reserves_kgs = "**.*"
reserves_lbs = "**.*"

cost_index = "***"
reserves_mod = "**.*"
reserves_kgs_mod = "**.*"
reserves_lbs_mod = "**.*"
cost_index_mod = "***"

econ_clb_spd = 0
econ_clb_spd_mach = 0
econ_crz_spd = 0
econ_crz_spd_mach = 0
econ_des_spd = 0
econ_des_spd_mach = 0
econ_des_vpa = 0.0

crz_alt = "*****"
crz_alt_num = 0
crz_alt_num2 = 0
crz_spd = "---"
crz_spd_mach = ".---"
crz_alt_old = "*****"
crz_spd_old = "---"
crz_spd_mach_old = ".---"
crz_exec = 0
clb_alt = "----"
cut_alt = "----"
clb_alt_num = B738DR_def_clb_alt
cut_alt_num = B738DR_def_cut_alt
accel_alt_num = B738DR_def_accel_alt
eo_accel_alt_num = B738DR_def_eo_accel_alt
B738DR_thr_cutback = 0
accel_alt = "----"
eo_accel_alt = "----"
crz_wind_dir = "---"
crz_wind_spd = "---"
rw_wind_dir = "---"
rw_wind_spd = "---"
rw_slope = "--.-"
rw_hdg = "---"
isa_dev_f = "----"
isa_dev_c = "---"
tc_oat_f = "----"
tc_oat_c = "---"

forec_alt_1 = "-----"
forec_alt_1_num = 0
forec_dir_1 = "---"
forec_spd_1 = "---"
forec_alt_2 = "-----"
forec_alt_2_num = 0
forec_dir_2 = "---"
forec_spd_2 = "---"
forec_alt_3 = "-----"
forec_alt_3_num = 0
forec_dir_3 = "---"
forec_spd_3 = "---"
cabin_rate = "---"
forec_isa_dev = "---"
forec_qnh = "------"

crz_wind_dir_mod = "---"
crz_wind_spd_mod = "---"
isa_dev_f_mod = "----"
isa_dev_c_mod = "---"
tc_oat_f_mod = "----"
tc_oat_c_mod = "---"

forec_alt_1_mod = "-----"
forec_alt_1_num_mod = 0
forec_dir_1_mod = "---"
forec_spd_1_mod = "---"
forec_alt_2_mod = "-----"
forec_alt_2_num_mod = 0
forec_dir_2_mod = "---"
forec_spd_2_mod = "---"
forec_alt_3_mod = "-----"
forec_alt_3_num_mod = 0
forec_dir_3_mod = "---"
forec_spd_3_mod = "---"
cabin_rate_mod = "---"
forec_isa_dev_mod = "---"
forec_qnh_mod = "------"

to = "<ACT>"
to_1 = "     "
to_2 = "     "
clb = "<SEL>"
clb_1 = "     "
clb_2 = "     "
sel_clb_thr = 0
rw_cond = 0
cg = "--.-"
trim = "    "
time_err = "  "
units_recalc = 0
weight_min = 90
weight_max = 180

clb_min_kts = "   "
clb_min_mach = "   "
clb_max_kts = "   "
clb_max_mach = "   "
crz_min_kts = "   "
crz_min_mach = "   "
crz_max_kts = "   "
crz_max_mach = "   "
des_min_kts = "   "
des_min_mach = "   "
des_max_kts = "   "
des_max_mach = "   "


latitude_deg = ""
latitude_min = ""
longitude_deg = ""
longitude_min = ""
irs_hdg = "---`"
irs_pos = "*****.*******.*"

msg_irs_pos = 0
msg_irs_hdg = 0
zulu_time = "             "
ground_air = 0
fmc_gs = ""
irs_gs = ""
irs2_gs = ""
oat_sim = "    "
oat = "****"
sel_temp = "----"
sel_temp_f = "----"
oat_f = "****"
oat_sim_f = "    "
oat_unit = "`C"
oat_n1_calc = 0
oat_old = ""
oat_f_old = ""

wind_corr = "--"
app_flap = "--"
app_spd = "---"

qrh = "OFF"
msg_mcp_alt = 0
msg_gps_l_fail = 0
msg_gps_r_fail = 0
msg_gps_lr_fail = 0
msg_irs_motion = 0
msg_drag_req = 0
msg_nav_data = 0
msg_unavaible_crz_alt = 0
msg_next_alt = 0
msg_chk_alt_tgt = 0
msg_tai_above_10 = 0

auto_act = "<ACT>"
ga_act = "     "
con_act = "     "
clb_act = "     "
crz_act = "     "
flight_phase_old2 = -1
fms_auto_n1_active = 1

tai_on_alt = "-----"
tai_off_alt = "-----"
tai_on_alt_num = 0
tai_off_alt_num = 0


eng_out_prompt = 0

was_on_air = 0
takeoff_enable = 0
climb_enable = 1
descent_enable = 0
goaround_enable = 0
fmc_climb_mode = 0
fmc_cruise_mode = 0
fmc_cont_mode = 0
fmc_takeoff_mode = 0
fmc_goaround_mode = 0
fms_N1_mode = 0
fms_N1_to_mode_sel = 0
fms_N1_clb_mode_sel = 0
in_flight_mode = 0
B738DR_lift_timer = 0

disable_POS_2L = 0
disable_POS_3L = 0
disable_POS_4R = 0
disable_POS_5R = 0
disable_PERF_3R = 0
disable_PERF_4R = 0
disable_N1_6L = 0
disable_N1_6R = 0

fmc_full_thrust = 0.984
fmc_dto_thrust = 0.984
fmc_sel_thrust = 0.984
fmc_clb_thrust = 0.984
fmc_crz_thrust = 0.984
fmc_con_thrust = 0.984
fmc_ga_thrust = 0.984
fmc_auto_thrust = 0.984

fmc_full_thrust2 = 0.984
fmc_dto_thrust2 = 0.984
fmc_sel_thrust2 = 0.984
fmc_clb_thrust2 = 0.984
fmc_crz_thrust2 = 0.984
fmc_con_thrust2 = 0.984
fmc_ga_thrust2 = 0.984
fmc_auto_thrust2 = 0.984

fmc_full_thrust_ind = 0.984
fmc_full_thrust_ind2 = 0984
fmc_dto_thrust_ind = 0.984
fmc_dto_thrust_ind2 = 0.984

next_enable = 1
prev_enable = 1
exec1_light = 0

des_now_enable = 0
drag_timeout = 0

file_name = ""

ref_data_rwy = {}
ref_rwy_num = 0
ref_data_sid = {}
sid_num = 0
ref_data_sid_tns = {}
sid_tns_num = 0
ref_data_star = {}
star_num = 0
ref_data_star_tns = {}
ref_data_star_tns_n = 0
ref_data_app = {}
ref_data_app_n = 0
ref_data_app_tns = {}
ref_data_app_tns_n = 0
ref_ed_app = {}
ref_ed_app_n = 0

des_data_rwy = {}
des_rwy_num = 0
data_des_star = {}
data_des_star_n = 0
data_des_star_tns = {}
data_des_star_tns_n = 0
data_des_app = {}
data_des_app_n = 0
data_des_app_tns = {}
data_des_app_tns_n = 0
-- don't used data
ed_app_num = 0
ed_app = {}


ref_rwy_exec = 0
ref_sid_exec = 0
ref_tns_exec = 0
ref_app_tns_exec = 0
des_star_exec = 0
des_star_tns_exec = 0
des_app_exec = 0
des_app_tns_exec = 0

legs_data = {}
legs_num = 0

legs_num3 = 0
legs_data3 = {}

-- legs_num3 = 1
-- legs_data3[legs_num3] = {}
-- legs_data3[legs_num3][1] = ""	--entry
-- legs_data3[legs_num3][2] = 0		-- brg
-- legs_data3[legs_num3][3] = 0		-- distance
-- legs_data3[legs_num3][4] = 0		-- speed
-- legs_data3[legs_num3][5] = 0		-- altitude
-- legs_data3[legs_num3][6] = 0	-- altitude type
-- legs_data3[legs_num3][7] = 0		-- latitude
-- legs_data3[legs_num3][8] = 0		-- longitude
-- legs_data3[legs_num3][9] = ""			-- via id
-- legs_data3[legs_num3][10] = 0		-- calc speed
-- legs_data3[legs_num3][11] = 0		-- calc altitude
-- legs_data3[legs_num3][12] = 0		-- calc altitude vnav pth
-- legs_data3[legs_num3][13] = 0
-- legs_data3[legs_num3][14] = 0		-- rest alt
-- legs_data3[legs_num3][15] = 0		-- last fuel
-- legs_data3[legs_num3][16] = ""		-- reg code
-- legs_data3[legs_num3][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
-- legs_data3[legs_num3][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
-- legs_data3[legs_num3][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
-- legs_data3[legs_num3][20] = 0
-- legs_data3[legs_num3][21] = -1
-- legs_data3[legs_num3][22] = ""
-- legs_data3[legs_num3][23] = 0
-- legs_data3[legs_num3][24] = 0
-- legs_data3[legs_num3][25] = 0
-- legs_data3[legs_num3][26] = 0
-- legs_data3[legs_num3][27] = ""
-- legs_data3[legs_num3][28] = ""
-- legs_data3[legs_num3][29] = ""
-- legs_data3[legs_num3][30] = ""
-- legs_data3[legs_num3][31] = ""
-- legs_data3[legs_num3][32] = 0
-- legs_data3[legs_num3][33] = ""
-- legs_data3[legs_num3][34] = ""
-- legs_data3[legs_num3][35] = ""
-- legs_data3[legs_num3][36] = 0
-- legs_data3[legs_num3][37] = 0
-- legs_data3[legs_num3][38] = ""
-- legs_data3[legs_num3][39] = ""
-- legs_data3[legs_num3][40] = 0
-- legs_data3[legs_num3][41] = 0
-- legs_data3[legs_num3][42] = 0
-- legs_data3[legs_num3][43] = ""
-- legs_data3[legs_num3][44] = 0
-- legs_data3[legs_num3][45] = 0
-- legs_data3[legs_num3][46] = 0
-- legs_data3[legs_num3][47] = 0
-- legs_data3[legs_num3][48] = ""
-- legs_data3[legs_num3][49] = 0
-- legs_data3[legs_num3][50] = 0
-- legs_data3[legs_num3][51] = 0
-- legs_data3[legs_num3][52] = 0
-- legs_data3[legs_num3][53] = 0
-- legs_data3[legs_num3][54] = 0

-- legs_num3 = 2
-- legs_data3[legs_num3] = {}
-- legs_data3[legs_num3][1] = ""	--entry
-- legs_data3[legs_num3][2] = 0		-- brg
-- legs_data3[legs_num3][3] = 0		-- distance
-- legs_data3[legs_num3][4] = 0		-- speed
-- legs_data3[legs_num3][5] = 0		-- altitude
-- legs_data3[legs_num3][6] = 0	-- altitude type
-- legs_data3[legs_num3][7] = 0		-- latitude
-- legs_data3[legs_num3][8] = 0		-- longitude
-- legs_data3[legs_num3][9] = ""			-- via id
-- legs_data3[legs_num3][10] = 0		-- calc speed
-- legs_data3[legs_num3][11] = 0		-- calc altitude
-- legs_data3[legs_num3][12] = 0		-- calc altitude vnav pth
-- legs_data3[legs_num3][13] = 0
-- legs_data3[legs_num3][14] = 0		-- rest alt
-- legs_data3[legs_num3][15] = 0		-- last fuel
-- legs_data3[legs_num3][16] = ""		-- reg code
-- legs_data3[legs_num3][17] = 0	--200		-- spd flag 0-default restrict, 1-custom restrict
-- legs_data3[legs_num3][18] = 0		-- alt flag 0-default restrict, 1-custom restrict
-- legs_data3[legs_num3][19] = 0		-- 0-none, 1-SID, 2-STAR/APP, 3-DISCONTINUITY
-- legs_data3[legs_num3][20] = 0
-- legs_data3[legs_num3][21] = -1
-- legs_data3[legs_num3][22] = ""
-- legs_data3[legs_num3][23] = 0
-- legs_data3[legs_num3][24] = 0
-- legs_data3[legs_num3][25] = 0
-- legs_data3[legs_num3][26] = 0
-- legs_data3[legs_num3][27] = ""
-- legs_data3[legs_num3][28] = ""
-- legs_data3[legs_num3][29] = ""
-- legs_data3[legs_num3][30] = ""
-- legs_data3[legs_num3][31] = ""
-- legs_data3[legs_num3][32] = 0
-- legs_data3[legs_num3][33] = ""
-- legs_data3[legs_num3][34] = ""
-- legs_data3[legs_num3][35] = ""
-- legs_data3[legs_num3][36] = 0
-- legs_data3[legs_num3][37] = 0
-- legs_data3[legs_num3][38] = ""
-- legs_data3[legs_num3][39] = ""
-- legs_data3[legs_num3][40] = 0
-- legs_data3[legs_num3][41] = 0
-- legs_data3[legs_num3][42] = 0
-- legs_data3[legs_num3][43] = ""
-- legs_data3[legs_num3][44] = 0
-- legs_data3[legs_num3][45] = 0
-- legs_data3[legs_num3][46] = 0
-- legs_data3[legs_num3][47] = 0
-- legs_data3[legs_num3][48] = ""
-- legs_data3[legs_num3][49] = 0
-- legs_data3[legs_num3][50] = 0
-- legs_data3[legs_num3][51] = 0
-- legs_data3[legs_num3][52] = 0
-- legs_data3[legs_num3][53] = 0
-- legs_data3[legs_num3][54] = 0

-- legs_off_tmp_n = 0
-- legs_off_tmp = {}

offset_act = 0
offset_start = 0
offset_end = 0
offset_dist = 0
offset_side = 0
offset_start_def = 0
offset_end_def = 0
offset_start_def_mod = 0
offset_end_def_mod = 0
offset_act_mod = 0
offset_start_mod = 0
offset_end_mod = 0
offset_dist_mod = 0
offset_side_mod = 0
offset_act_old = 0

ref_rwy_sel = {true, true, true, true, true}
ref_rwy_sel[1] = ""
ref_rwy_sel[2] = ""
ref_rwy_sel[3] = ""
ref_rwy_sel[4] = ""
ref_rwy_sel[5] = ""

ref_sid_sel = {true, true, true, true, true}
ref_sid_sel[1] = ""
ref_sid_sel[2] = ""
ref_sid_sel[3] = ""
ref_sid_sel[4] = ""
ref_sid_sel[5] = ""

ref_tns_sel = {true, true, true, true, true}
ref_tns_sel[1] = ""
ref_tns_sel[2] = ""
ref_tns_sel[3] = ""
ref_tns_sel[4] = ""
ref_tns_sel[5] = ""

des_star_sel = {true, true, true, true, true}
des_star_sel[1] = ""
des_star_sel[2] = ""
des_star_sel[3] = ""
des_star_sel[4] = ""
des_star_sel[5] = ""

des_star_tns_sel = {true, true, true, true, true}
des_star_tns_sel[1] = ""
des_star_tns_sel[2] = ""
des_star_tns_sel[3] = ""
des_star_tns_sel[4] = ""
des_star_tns_sel[5] = ""

des_app_sel = {true, true, true, true, true}
des_app_sel[1] = ""
des_app_sel[2] = ""
des_app_sel[3] = ""
des_app_sel[4] = ""
des_app_sel[5] = ""

des_tns_sel = {true, true, true, true, true}
des_tns_sel[1] = ""
des_tns_sel[2] = ""
des_tns_sel[3] = ""
des_tns_sel[4] = ""
des_tns_sel[5] = ""

ref_rwy_sel_fo = {true, true, true, true, true}
ref_rwy_sel_fo[1] = ""
ref_rwy_sel_fo[2] = ""
ref_rwy_sel_fo[3] = ""
ref_rwy_sel_fo[4] = ""
ref_rwy_sel_fo[5] = ""

ref_sid_sel_fo = {true, true, true, true, true}
ref_sid_sel_fo[1] = ""
ref_sid_sel_fo[2] = ""
ref_sid_sel_fo[3] = ""
ref_sid_sel_fo[4] = ""
ref_sid_sel_fo[5] = ""

ref_tns_sel_fo = {true, true, true, true, true}
ref_tns_sel_fo[1] = ""
ref_tns_sel_fo[2] = ""
ref_tns_sel_fo[3] = ""
ref_tns_sel_fo[4] = ""
ref_tns_sel_fo[5] = ""

des_star_sel_fo = {true, true, true, true, true}
des_star_sel_fo[1] = ""
des_star_sel_fo[2] = ""
des_star_sel_fo[3] = ""
des_star_sel_fo[4] = ""
des_star_sel_fo[5] = ""

des_star_tns_sel_fo = {true, true, true, true, true}
des_star_tns_sel_fo[1] = ""
des_star_tns_sel_fo[2] = ""
des_star_tns_sel_fo[3] = ""
des_star_tns_sel_fo[4] = ""
des_star_tns_sel_fo[5] = ""

des_app_sel_fo = {true, true, true, true, true}
des_app_sel_fo[1] = ""
des_app_sel_fo[2] = ""
des_app_sel_fo[3] = ""
des_app_sel_fo[4] = ""
des_app_sel_fo[5] = ""

des_tns_sel_fo = {true, true, true, true, true}
des_tns_sel_fo[1] = ""
des_tns_sel_fo[2] = ""
des_tns_sel_fo[3] = ""
des_tns_sel_fo[4] = ""
des_tns_sel_fo[5] = ""

set_ils = 0
gs_prompt = 0
legs_offset = 0
legs_select = 0
legs_delete = 0
legs_delete_item = 0
legs_delete_key = 0

legs_restr_spd = {}
legs_restr_spd_n = 0

legs_restr_alt = {}
legs_restr_alt_n = 0

tc_dist = 0
tc_lat = 0
tc_lon = 0

td_lat = 0
td_lon = 0

decel_lat = 0
decel_lon = 0
was_decel = 0

-----------------------
tc_idx = 0
td_dist = 0
td_idx = 0
decel_dist = 0
decel_idx = 0
ed_found = 0
ed_alt = 0
td_idx_last = 0
td_spd_rest = 0
-------------------------
vnav_pth_alt_old = 0
gp_pth_alt_old = 0
vnav_vvi_cur = 0
gp_vvi_cur = 0
vnav_vvi_cur99 = 0
vnav_pth_alt99_old = 0
	


vnav_update = 0

offset_old = 0

temp_ils3 = ""
temp_ils4 = ""

	max_page = 0
	act_page = 0
	act_page_old = 0
	page_legs_step = 1
	act_page_old2 = 0

	display_update = 1

	entry = ""
	entry2 = ""

	v1 = "---"
	vr = "---"
	v2 = "---"
	flaps = "**"
	vref_15 = "   "
	vref_30 = "   "
	vref_40 = "   "
	flaps_app = "  "

	gw_act = "    "
	v1_set = "---"
	vr_set = "---"
	v2_set = "---"
	irs_pos_set = "*****.*******.*"
	irs2_pos_set = "*****.*******.*"
	gps_right = "-----.-------.-"
	gps_left = "-----.-------.-"
	fmc_pos = "-----.-------.-"
	last_pos_enable = 1

	gw = "***.*"
	gw_calc = "***.*"
	gw_lbs = "***.*"
	gw_kgs = "***.*"

	zfw_calc = "---.-"
	zfw_calc_lbs = "---.-"
	zfw_calc_kgs = "---.-"

	fuel_weight = "--.-"
	fuel_weight_lbs = "--.-"
	fuel_weight_kgs = "--.-"

	plan_weight = "---.-"
	plan_weight_kgs = "---.-"
	plan_weight_lbs = "---.-"

	plan_weight_mod = "---.-"
	plan_weight_kgs_mod = "---.-"
	plan_weight_lbs_mod = "---.-"

	zfw = "***.*"
	zfw_kgs = "***.*"
	zfw_lbs = "***.*"

	gw_mod = "***.*"
	gw_lbs_mod = "***.*"
	gw_kgs_mod = "***.*"
	zfw_mod = "***.*"
	zfw_kgs_mod = "***.*"
	zfw_lbs_mod = "***.*"

	reserves = "**.*"
	reserves_kgs = "**.*"
	reserves_lbs = "**.*"

	cost_index = "***"
	reserves_mod = "**.*"
	reserves_kgs_mod = "**.*"
	reserves_lbs_mod = "**.*"
	cost_index_mod = "***"
	crz_alt = "*****"
	crz_alt_num = 0
	crz_alt_num2 = 0
	crz_spd = "300"
	crz_spd_mach = ".800"
	crz_alt_old = "*****"
	crz_spd_old = "---"
	crz_spd_mach_old = ".---"
	crz_exec = 0
	crz_wind_dir = "---"
	crz_wind_spd = "---"
	isa_dev_f = "----"
	isa_dev_c = "---"
	tc_oat_f = "----"
	tc_oat_c = "---"
	
	forec_alt_1 = "-----"
	forec_dir_1 = "---"
	forec_spd_1 = "---"
	forec_alt_2 = "-----"
	forec_dir_2 = "---"
	forec_spd_2 = "---"
	forec_alt_3 = "-----"
	forec_dir_3 = "---"
	forec_spd_3 = "---"
	cabin_rate = "---"
	forec_isa_dev = "---"
	forec_qnh = "------"

	forec_alt_1_mod = "-----"
	forec_alt_1_num_mod = 0
	forec_dir_1_mod = "---"
	forec_spd_1_mod = "---"
	forec_alt_2_mod = "-----"
	forec_alt_2_num_mod = 0
	forec_dir_2_mod = "---"
	forec_spd_2_mod = "---"
	forec_alt_3_mod = "-----"
	forec_alt_3_num_mod = 0
	forec_dir_3_mod = "---"
	forec_spd_3_mod = "---"
	cabin_rate_mod = "---"
	forec_isa_dev_mod = "---"
	forec_qnh_mod = "------"
	
	crz_wind_dir_mod = "---"
	crz_wind_spd_mod = "---"
	isa_dev_f_mod = "----"
	isa_dev_c_mod = "---"
	tc_oat_f_mod = "----"
	tc_oat_c_mod = "---"

	to = "<ACT>"
	to_1 = "     "
	to_2 = "     "
	clb = "<SEL>"
	clb_1 = "     "
	clb_2 = "     "
	sel_clb_thr = 0
	rw_cond = 0
	cg = "--.-"
	trim = "    "
	time_err = "  "
	units_recalc = 0
	weight_min = 90
	weight_max = 180

	rw_wind_dir = "---"
	rw_wind_spd = "---"
	rw_slope = "--.-"
	rw_hdg = "---"

	tai_on_alt = "-----"
	tai_off_alt = "-----"


	clb_min_kts = "   "
	clb_min_mach = "   "
	clb_max_kts = "   "
	clb_max_mach = "   "
	crz_min_kts = "   "
	crz_min_mach = "   "
	crz_max_kts = "   "
	crz_max_mach = "   "
	des_min_kts = "   "
	des_min_mach = "   "
	des_max_kts = "   "
	des_max_mach = "   "

	latitude_deg = ""
	latitude_min = ""
	longitude_deg = ""
	longitude_min = ""
	irs_hdg = "---`"
	irs_pos = "*****.*******.*"
	msg_irs_pos = 0
	msg_irs_hdg = 0
	zulu_time = "             "
	ground_air = 0
	fmc_gs = ""
	irs_gs = ""
	irs2_gs = ""
	oat_sim = "    "
	oat = "****"
	sel_temp = "----"
	sel_temp_f = "----"
	oat_f = "****"
	oat_sim_f = "    "
	oat_unit = "`C"
	oat_n1_calc = 0
	oat_old = ""
	oat_f_old = ""
	auto_act = "<ACT>"
	ga_act = "     "
	con_act = "     "
	clb_act = "     "
	crz_act = "     "
	flight_phase_old2 = -1
	fms_auto_n1_active = 1
	
	was_on_air = 0
	
	wind_corr = "--"
	app_flap = "--"
	app_spd = "---"
	
	eng_out_prompt = 0

	fmc_message = {}
	fmc_message_warn = {}
	fmc_message_num = 0
	fmc_message2 = {}
	fmc_message_warn2 = {}
	fmc_message_num2 = 0

	qrh = "OFF"
	msg_mcp_alt = 0
	msg_gps_l_fail = 0
	msg_gps_r_fail = 0
	msg_gps_lr_fail = 0
	msg_irs_motion = 0
	msg_drag_req = 0
	msg_above_max = 0
	msg_vref_not_sel = 0
	msg_chk_alt_constr = 0
	msg_rnp_alert = 0
	rnp_alert = 0
	msg_using_rsv_fuel = 0
	msg_using_ins_fuel = 0
	msg_com3_data_alert = 0
	msg_ref_icao_alert = 0
	
	takeoff_enable = 0
	climb_enable = 1
	descent_enable = 0
	goaround_enable = 0
	fmc_climb_mode = 0
	fmc_cruise_mode = 0
	fmc_cont_mode = 0
	fmc_takeoff_mode = 0
	fmc_goaround_mode = 0
	in_flight_mode = 0


	disable_POS_2L = 0
	disable_POS_3L = 0
	disable_POS_4R = 0
	disable_POS_5R = 0
	disable_PERF_3R = 0
	disable_PERF_4R = 0
	disable_N1_6L = 0
	disable_N1_6R = 0


	max_page = 1
	act_page = 1
	act_page_old = 0
	
	act_page2 = 1
	max_page2 = 1
	act_page_old2 = 0
	
	v1_set = "---"
	vr_set = "---"
	v2_set = "---"
	v1 = "---"
	vr = "---"
	v2 = "---"
	flaps = "**"
	gw_act = "   "
	flaps_app = " "
	gps_right = "-----.-------.-"
	gps_left = "---`--.- ----`--.-"
	to = "<ACT>"
	to_1 = "     "
	to_2 = "     "
	clb = "<SEL>"
	clb_1 = "     "
	clb_2 = "     "
	irs_pos = "*****.*******.*"
	B738DR_irs_hdg_fmc_set = "---"
	B738DR_irs2_hdg_fmc_set = "---"
	last_pos_enable = 1
	irs_hdg = "---`"
	msg_irs_pos = 0
	msg_irs_hdg = 0
	isa_dev_f = "----"
	isa_dev_c = "---"
	tc_oat_f = "----"
	tc_oat_c = "---"
	time_err = "  "
	oat = "****"
	sel_temp = "----"
	sel_temp_f = "----"
	oat_f = "****"
	oat_unit = "`C"
	oat_n1_calc = 0
	oat_old = ""
	oat_f_old = ""
	takeoff_enable = 1
	fms_N1_mode = 0
	fms_N1_to_mode_sel = 0
	fms_N1_clb_mode_sel = 0
	clb_alt = "----"
	clb_alt_num = B738DR_def_clb_alt
	cut_alt = "----"
	cut_alt_num = B738DR_def_cut_alt
	B738DR_thr_cutback = 0

	fmc_full_thrust = 0.984
	fmc_dto_thrust = 0.984
	fmc_sel_thrust = 0.984
	fmc_clb_thrust = 0.984
	fmc_crz_thrust = 0.984
	fmc_con_thrust = 0.984
	fmc_ga_thrust = 0.984
	fmc_auto_thrust = 0.984
	
	fmc_full_thrust2 = 0.984
	fmc_dto_thrust2 = 0.984
	fmc_sel_thrust2 = 0.984
	fmc_clb_thrust2 = 0.984
	fmc_crz_thrust2 = 0.984
	fmc_con_thrust2 = 0.984
	fmc_ga_thrust2 = 0.984
	fmc_auto_thrust2 = 0.984
	
	next_enable = 1
	prev_enable = 1
	
	drag_timeout = 0

-- CLIMB default
	B738DR_fmc_climb_speed			= 280
	B738DR_fmc_climb_speed_mach		= 0.76
	B738DR_fmc_climb_r_speed1		= 250
	B738DR_fmc_climb_r_alt1			= 10000
	B738DR_fmc_climb_r_speed2		= 0
	B738DR_fmc_climb_r_alt2			= 0

-- CRUISE default
	B738DR_fmc_cruise_speed			= 300
	B738DR_fmc_cruise_speed_mach	= 0.80
	B738DR_fmc_cruise_alt			= 20000

-- DESCENT default
	B738DR_fmc_descent_speed		= 280
	B738DR_fmc_descent_speed_mach	= 0.78
	B738DR_fmc_descent_alt			= 4000
	B738DR_fmc_descent_r_speed1		= 240
	B738DR_fmc_descent_r_alt1		= 10000
	B738DR_fmc_descent_r_speed2		= 0
	B738DR_fmc_descent_r_alt2		= 0
-- APPROACH default
	B738DR_fmc_approach_alt			= 4000

	fmc_descent_r_alt1 = "10000"
	fmc_climb_r_alt1 = "10000"
	
	econ_clb_spd = 280
	econ_clb_spd_mach = 0.760
	econ_crz_spd = 300
	econ_crz_spd_mach = 0.800
	econ_des_spd = 280
	econ_des_spd_mach = 0.780
	econ_des_vpa = 2.5	--2.5
	lrc_crz_spd = 280
	lrc_crz_spd_mach = 0.760

	B738DR_climb_mode = 3		-- xxxKT/M.xxx CLB
	B738DR_cruise_mode = 2		-- xxxKT/M.xxx CRZ
	B738DR_descent_mode = 0		-- xxxKT/M.xxx SPD DES
	fmc_cruise_mode_mod = B738DR_cruise_mode
	fmc_descent_mode_mod = B738DR_descent_mode
	fmc_climb_mode_mod = B738DR_climb_mode
	
	B738DR_flight_phase = 0
	
	B738DR_fms_descent_now = 0
	des_now_enable = 0
	
	descent_r_alt1_mod = B738DR_fmc_descent_r_alt1
	descent_r_speed1_mod = B738DR_fmc_descent_r_speed1
	fmc_descent_r_alt1_mod = fmc_descent_r_alt1
	
	climb_r_alt1_mod = B738DR_fmc_climb_r_alt1
	climb_r_speed1_mod = B738DR_fmc_climb_r_speed1
	fmc_climb_r_alt1_mod = fmc_climb_r_alt1
	
	decode_value = 0
	index_pos = 0

	exec1_light = 0
	
	offset = 0
	offset_seq = 0
	
	msg_nav_data = 0
	
	tc_idx = 0
	td_idx = 0
	was_decel = 0
	ed_found = 0
	ed_alt = 0
	ed_vpa = 0
	ed_type = 0
	B738DR_vvi_const = 10
	clr_repeat = 0
	clr_repeat_time = 0
	legs_step = 1
	legs_step2 = 1
	new_hold2 = 0

	offset_old = 0
	temp_ils3 = ""
	temp_ils4 = ""
	td_fix_dist = 0
	td_fix_idx = 0
	legs_intdir = 0
	legs_dir = 0
	ed_fix_num = 0
	ed_fix_found2 = {}
	ed_fix_alt2 = {}
	ed_fix_vpa2 = {}
	ed_fix_type = {}
	ed_fix_table = {}
	ed_fix_distance = {}
	ed_fix_tl = {}
	ed_fix_found2_mod = {}
	ed_fix_alt2_mod = {}
	ed_fix_vpa2_mod = {}
	ed_fix_type_mod = {}
	ed_fix_table_mod = {}
	ed_fix_tl_mod = {}
	dist_dest = 0
	time_dest = ""
	dist_tc = 0
	time_tc = 0
	dist_td = 0
	time_td = 0
	dist_ed = 0
	time_ed = 0
	fuel_ed = 0
	fuel_tc = 0
	fuel_td = 0
	dist_dest_mod = 0
	time_dest_mod = ""
	dist_tc_mod = 0
	time_tc_mod = 0
	dist_td_mod = 0
	time_td_mod = 0
	dist_ed_mod = 0
	time_ed_mod = 0
	fuel_ed_mod = 0
	fuel_tc_mod = 0
	fuel_td_mod = 0
	fms_msg_sound = 0
	legs_page = 0
	legs_button = 0
	direct_to = 0
	direct_to_offset = 0
	B738DR_pfd_vert_path = 0
	B738DR_nd_vert_path = 0
	B738DR_pfd_trk_path = 0
	ed_found = 0
	lock_bank = 0
	DRindex = 0.02		--0.002
	pause_td_disable = 0
	pre_flt_pos_init = 0
	pre_flt_perf_init = 0
	pre_flt_rte = 0
	pre_flt_dep = 0
	last_lat = 0 
	last_lon = 0 
	last_offset = 0
	
	ref_runway_lenght = 0
	ref_runway_lat = 0
	ref_runway_lon = 0
	ref_runway_crs = 0
	des_runway_lenght = 0
	des_runway_lat = 0
	des_runway_lon = 0
	des_runway_crs = 0
	ref_runway_old_name = ""
	des_runway_old_name = ""
	
	ref_runway_lenght_mod = 0
	ref_runway_lat_mod = 0
	ref_runway_lon_mod = 0
	ref_runway_crs_mod = 0
	des_runway_lenght_mod = 0
	des_runway_lat_mod = 0
	des_runway_lon_mod = 0
	des_runway_crs_mod = 0
	ref_runway_old_name_mod = ""
	des_runway_old_name_mod = ""
	des_runway_end_lat_mod = 0
	des_runway_end_lon_mod = 0
	
	ref_runway_lenght_mod2 = 0
	ref_runway_lat_mod2 = 0
	ref_runway_lon_mod2 = 0
	ref_runway_crs_mod2 = 0
	des_runway_lenght_mod2 = 0
	des_runway_lat_mod2 = 0
	des_runway_lon_mod2 = 0
	des_runway_crs_mod2 = 0
	ref_runway_old_name_mod2 = ""
	des_runway_old_name_mod2 = ""
	des_runway_end_lat_mod2 = 0
	des_runway_end_lon_mod2 = 0


	des_runway_lat_nd = 0
	des_runway_lon_nd = 0
	des_runway_crs_nd = 0
	des_runway_lenght_nd = 0
	des_rnw_nd = ""
	
	xfirst_time3 = 0
	xfirst_5sec = 0
	
	nearest_apt_icao = {true, true, true, true, true}
	nearest_apt_dist = {true, true, true, true, true}
	nearest_apt_icao2 = {true, true, true, true, true}
	nearest_apt_dist2 = {true, true, true, true, true}
	nearest_apt_lat = {true, true, true, true, true}
	nearest_apt_lon = {true, true, true, true, true}
	nearest_apt_lat2 = {true, true, true, true, true}
	nearest_apt_lon2 = {true, true, true, true, true}
	nearest_apt_list_num = 0
	
	nearest_apt_list_icao = {true, true, true, true, true}
	nearest_apt_list_dist = {true, true, true, true, true}
	nearest_apt_list_dist2 = {true, true, true, true, true}
	nearest_apt_list_via = {true, true, true, true, true}
	nearest_apt_list_lat = {true, true, true, true, true}
	nearest_apt_list_lon = {true, true, true, true, true}
	nearest_apt_list_wind = {true, true, true, true, true}
	nearest_apt_list_alt = {true, true, true, true, true}
	nearest_apt_list_dir_dist = {true, true, true, true, true}
	
	altern_apt_list_num = 0
	altern_apt_list_icao = {true, true, true, true, true}
	altern_apt_list_dist = {true, true, true, true, true}
	altern_apt_list_via = {true, true, true, true, true}
	altern_apt_list_lat = {true, true, true, true, true}
	altern_apt_list_lon = {true, true, true, true, true}
	altern_apt_list_alt = {true, true, true, true, true}
	altern_apt_list_wind = {true, true, true, true, true}
	altern_apt_list_dist2 = {true, true, true, true, true}
	for ii = 1, 5 do
		nearest_apt_icao[ii] = ""
		nearest_apt_dist[ii] = 999
		nearest_apt_icao2[ii] = ""
		nearest_apt_dist2[ii] = 999
		nearest_apt_lat[ii] = 0
		nearest_apt_lon[ii] = 0
		nearest_apt_lat2[ii] = 0
		nearest_apt_lon2[ii] = 0
		altern_apt_list_icao[ii] = "----"
		altern_apt_list_dist[ii] = 0
		altern_apt_list_via[ii] = 0
		altern_apt_list_lat[ii] = 0
		altern_apt_list_lon[ii] = 0
		altern_apt_list_alt[ii] = 0
		altern_apt_list_wind[ii] = ""
		altern_apt_list_dist2[ii] = 0
	end
	
	first_time_gls = 0
	gls_from = 0
	gls_to = 0
	gls_page = 0
	gls_page1_num = 0
	gls_page2_num = 0
	gls_page1 = {}
	gls_page2 = {}
	gls_teak = 0

	near_apt1_dis = 0
	near_apt1_icao = ""
	near_apt2_dis = 0
	near_apt2_icao = ""
	after_load_flight_TMR = 0
	altitude_last = 0
	goaround_altitude_last = 41000
	ref_icao_pos = "               "
	ref_icao_pos2 = "               "
	icao_latitude = 0
	icao_longitude = 0
	icao_tns_alt = 0
	icao_tns_lvl = 0
	
	ref_rnw_list = {}
	ref_rnw_list_num = 0
	ref_rnw_list2 = {}
	ref_rnw_list_num2 = 0
	des_rnw_list = {}
	des_rnw_list_num = 0
	
	sid_list = {}
	sid_list_num = 0
	sid_tns_list = {}
	sid_tns_list_num = 0
	
	star_list = {}
	star_list_num = 0
	star_tns_list = {}
	star_tns_list_num = 0
	app_list = {}
	app_list_num = 0
	app_tns_list = {}
	app_tns_list_num = 0
	
	rte_add_sid_act = 0
	rte_add_star_act = 0
	rte_add_app_act = 0
	add_sid_star_app_act = 0
	sid_cnt = 0
	rnw_added = 0
	add_ok = 0
	
	rte_sid = {}
	rte_sid_num = 0
	rte_star = {}
	rte_star_num = 0
	rte_app = {}
	rte_app_num = 0
	
	rte_data_num = 0
	rte_data = {}
	
	item_sel = 0
	item_sel2 = 0
	item_sel_via = 0
	item_sel_via2 = 0
	item_sel_act = 0
	item_sel_len = 0
	item_sel_act2 = 0
	item_sel_len2 = 0
	
	navaid_list = {}
	navaid_list_n = 0
	
	temp_list = {}
	temp_list_num = 0
	
	rte_lat = 0
	rte_lon = 0
	
	calc_rte_enable = 0
	calc_rte_act = 0
	rte_calc_lat = 0
	rte_calc_lon = 0
	calc_rte_enable_old = -1
	
	calc_rte_enable2 = 0
	calc_rte_act2 = 0
	rte_calc_lat2 = 0
	rte_calc_lon2 = 0
	
	calc_rte_enable8 = 0
	
	add_disco = 0
	
	dir_change = 0
	dir_idx = 0
	dir_disco = 0
	
	legs_intdir_crs = -1
	legs_intdir_crs2 = -1
	legs_intdir_act = 0
	legs_intdir_lat = 0
	legs_intdir_lon =  0
	legs_intdir_trk = 0
	
	new_hold = 0
	new_hold_at = 0
	new_hold_inbd = "---"
	new_hold_turn = -1
	new_hold_time = "-.-"
	new_hold_dist = "--.-"
	new_hold_idx = 0
	
	new_hold2 = 0
	new_hold_at2 = 0
	new_hold_inbd2 = "---"
	new_hold_turn2 = -1
	new_hold_time2 = "-.-"
	new_hold_dist2 = "--.-"
	new_hold_idx2 = 0
	
	new_hold_act_page = 0
	new_hold_act_page2 = 0
	
	forecast_exec = 0
	B738DR_hold_term = 0
	hold_circuit = 0
	hold_time_set = 0
	hold_dist_set = 0
	hold_timer = 0
	nav_mode = 0
	pi_phase = 0
	last_sid_idx = 0
	first_sid_idx = 0
	first_app_idx = 0
	first_star_idx = 0
	iaf_app_idx = 0
	ils_id = ""
	ils_freq = 0
	ils_course = 0
	lda_id = ""
	lda_freq = 0
	lda_course = 0
	gls_id = ""
	gls_channel = 0
	gls_course = 0
	
	rte_copy_idx = 0
	add_star = 0
	add_star2 = 0
	add_app = 0
	del_execute = 0
	B738DR_missed_app_alt = 0
	
	legs_num_old = 0
	legs_num = 0
	
	missed_app_via = ""
	missed_app_distance = 0
	missed_app_via2 = ""
	missed_app_distance2 = 0
	
	reset_fmc_act = 0
	perf_exec = 0
	
	page_ref_nav_data = 0
	page_ref_nav_data_wpt = 0
	page_ref_nav_data_navaid = 0
	page_ref_nav_data_apt = 0
	page_ref_sel = 0
	ref_nav_wpt = "-----"
	ref_nav_navaid = "----"
	ref_nav_apt = "----"
	ref_nav_new = "*****"
	ref_nav_lat = 0
	ref_nav_lon = 0
	ref_nav_mag_var = ""
	ref_nav_id = "-----"
	ref_nav_rc = ""
	ref_nav_brg = "---"
	ref_nav_dist = "---"
	ref_nav_exec = 0
	ref_nav_id_lat = 0
	ref_nav_id_lon = 0
	ref_nav_freq = ""
	ref_nav_rnw = "-----"
	ref_nav_rnw_length = 0
	ref_elevation = 0
	
	page_flt_plns = 0
	page_flt_plns2 = 0
	
	flt_plns_data_num = 0
	flt_plns_data = {}
	
	fmc_align = 0
	fmc_align_old = 0
	B738DR_anp = 0
	rnp_lock_time = 0
	
	B738DR_gpu_test_enable = 1
	if is_timer_scheduled(gpu_test_reset) == false then
		run_after_time(gpu_test_reset, 3.5)
	end
	
	cg_set_m = 0
	cg_set_in = 0
	mac = calc_mac(cg_set_m)
	mac_zfw = calc_zfw_mac(cg_set_m)
	menu_tick = 0
	align_anp = 0
	align_anp_min = 0
	align_anp_max = 0
	align_anp_time = 0
	align_source_old = 99
	
	fix_data_num = 0
	fix_data = {}
	
	fix_abm_act = {}
	fix_abm_wpt_idx = {}
	fix_abm_wpt_dist = {}
	fix_abm_lat = {}
	fix_abm_lon = {}
	fix_abm_brg = {}
	fix_abm_dist = {}
	fix_abm_eta = {}
	fix_abm_alt = {}
	for eee = 1, 5 do
		fix_abm_act[eee] = 0
		fix_abm_wpt_idx[eee] = -1
		fix_abm_wpt_dist[eee] = -1
		fix_abm_lat[eee] = -1
		fix_abm_lon[eee] = -1
		fix_abm_brg[eee] = -1
		fix_abm_dist[eee] = -1
		fix_abm_eta[eee] = -1
		fix_abm_alt[eee] = -1
	end
	
	B738DR_fms_legs = ""
	
	radii_dist = 0
	radii_lat = 0
	radii_lon = 0
	radii_turn_dir = 0
	B738DR_radii_turn_act = 0
	radii_add_dist = 0
	radii_add_lat = 0
	radii_add_lon = 0
	radii_begin_crs = 0
	radii_end_crs = 0
	radii_end_angle = 0
	radii_remain_dist = 0
	turn_radii_used = 0
	
	ndx_lat = simDR_latitude
	ndx_lon = simDR_longitude
	ndx_mag_hdg = simDR_mag_hdg
	ndx_ahars_mag_hdg = simDR_ahars_mag_hdg
	
	first_alt_restrict = 0
	
	file_atis = 0
	ac_atis_icao = "----"
	acars_atis_status = 0
	atis_time = ""
	atis_message = {}
	atis_msg_status = 0
	atis_tick = 0
	atis_validtime = 0
	vhf_in_prog = "            "
	atis_send_time = "     "
	receive_msg = 0
	x_delay = 0
	gp_available = 0
	change_desc_to_path = 0
	B738DR_dest_runway_alt = -999
	B738DR_legs_mod_active = 0
	fms_recalc = 0
	B738DR_ref_rwy_altitude = 0
	B738DR_des_rwy_altitude = 0
	B738DR_pfd_rwy_show = 0
	B738DR_fms_ils_disable = 0
	
	B738DR_dest_runway_alt = 0
	B738DR_dest_runway_len = 0
	B738DR_dest_runway_crs = 0
	B738DR_dest_runway_start_lat = 0
	B738DR_dest_runway_start_lon = 0
	B738DR_dest_runway_end_lat = 0
	B738DR_dest_runway_end_lon = 0
	B738DR_dest_runway_id = ""
	
	B738DR_dest_runway_len_mod = 0
	B738DR_dest_runway_crs_mod = 0
	B738DR_dest_runway_start_lat_mod = 0
	B738DR_dest_runway_start_lon_mod = 0
	B738DR_dest_runway_end_lat_mod = 0
	B738DR_dest_runway_end_lon_mod = 0
	
	B738_legs_num = 0
	B738DR_fms_legs_num2 = 0
	B738DR_fms_legs_num3 = 0
	B738_num_of_wpts = 0
	B738DR_glidepath_type = 0
	B738DR_fms_legs = ""
	B738_num_of_wpts_2 = 0
	B738_num_of_wpts_3 = 0
	idle_alt = 40000
	file_idle_path = 0
	B738DR_idle_path = 0
	B738DR_idle_path_kts = 280
	B738DR_idle_path_mach = 0.78
	B738DR_fmc_trk_turn = -1
	B738DR_fmc_trk_turn2 = -1
	max_cruise_alt = 0
	max_cruise_alt_idx = 0
	block_df = 0
	skip_hold = 0
	first_desc_rest = 0
	msg_unavaible_crz_alt_del = 0
	
	crz_spd_exec = 0
	fmc_cruise_speed_mod = 0
	fmc_cruise_speed_mach_mod = 0
	des_spd_exec = 0
	fmc_descent_speed_mod = 0
	fmc_descent_speed_mach_mod = 0
	clb_spd_exec = 0
	fmc_climb_speed_mod = 0
	fmc_climb_speed_mach_mod = 0
	fmc_cruise_mode_mod = 0
	fmc_descent_mode_mod = 0
	
	B738DR_state_cmd = ""
	elevation_ft = 0
	B738DR_metar_cmd = 0
	fix_rnw_lat = 0
	fix_rnw_lon = 0
	fix_rnw_crs = 0
	fix_rnw_num = ""
	legs_intdir = 0
	legs_intdir_crs2 = -1
	legs_executed = 0
	legs_delete_old = 0
	legs_delete_old2 = 0
	
	nav1_nav_id_old = ""
	nav2_nav_id_old = ""
	nav1_lat = 0
	nav1_lon = 0
	nav2_lat = 0
	nav2_lon = 0
	nav1_nav_id_fo_old = ""
	nav2_nav_id_fo_old = ""
	nav1_lat_fo = 0
	nav1_lon_fo = 0
	nav2_lat_fo = 0
	nav2_lon_fo = 0
	B738DR_eng_out_disable = 0
	B738DR_fmc_bank_angle = 6
	B738DR_next_predict = 0
	cruise_alt_changed = 0
	cruise_alt_changed2 = 0
	B738DR_FL100_desc_dist = 0
	B738DR_FL100_idx = 0
	B738DR_FL100_dist = 0
	B738DR_FL100_desc_dist_mod = 0
	B738DR_FL100_idx_mod = 0
	B738DR_FL100_dist_mod = 0
	desc_wind_tab_num = 0
	desc_wind_alt_tab = {}
	desc_wind_spd_tab = {}
	desc_wind_dir_tab = {}
	vrnp_num = 400
	vrnp = ""
	app_rnp = ""
	app_rnp_num = 0.30
	clb_del_spd_idx = 0
	clb_del_spd_idx2 = 0
	clb_del_spd_idx_fo = 0
	clb_del_spd_idx2_fo = 0
	des_del_spd_idx = 0
	des_del_spd_idx2 = 0
	des_del_spd_idx_fo = 0
	des_del_spd_idx2_fo = 0
	B738DR_dest_fuel = -1
	time_calc_enable = 0
	fuel_calculated = 0
	time_calc_enable2 = 0
	fuel_calculated2 = 0
	rec_mode = 0
	last_dta = 0
	on_ground_1_old = 9
	
	page_menu = 1
	page_ident = 0
	page_init = 0
	page_takeoff = 0
	display_update = 1
	page_approach = 0
	page_perf = 0
	page_n1_limit = 0
	page_pos_init = 0
	page_route = 0
	page_dep_arr = 0
	page_dep = 0
	page_arr = 0
	page_descent = 0
	page_descent_forecast = 0
	page_legs = 0
	page_rte_init = 0
	page_climb = 0
	page_cruise = 0
	page_progress = 0
	page_hold = 0
	page_sel_wpt = 0
	page_sel_wpt2 = 0
	page_sel_wpt3 = 0
	page_sel_wpt4 = 0
	fix_idx1 = 0
	fix_idx2 = 0
	page_sel_hold_wpt = 0
	page_offset = 0
	page_legs_step = 0
	page_fix = 0
	page_rte_legs = 0
	page_ac = 0
	page_ac_atis = 0
	page_ac_msg = 0
	page_nearest_apt = 0
	page_altern_apt = 0
	page_nav_status = 0
	page_atc = 0
	page_fmc_comm = 0
	page_dl_menu = 0
	page_dl_acars = 0
	page_dl_requests = 0
	page_dl_wxr_req = 0
	page_dl_wxr_info = 0
	page_dl_fltlog = 0
	page_dl_sensors = 0
	page_eo_climb = 0
	page_eo_cruise = 0

	page_menu2 = 1
	page_ident2 = 0
	page_init2 = 0
	page_takeoff2 = 0
	page_approach2 = 0
	page_perf2 = 0
	page_n1_limit2 = 0
	page_pos_init2 = 0
	page_route2 = 0
	page_dep_arr2 = 0
	page_dep2 = 0
	page_arr2 = 0
	page_descent2 = 0
	page_descent_forecast2 = 0
	page_legs2 = 0
	page_rte_init2 = 0
	page_climb2 = 0
	page_cruise2 = 0
	page_progress2 = 0
	page_hold2 = 0
	page_sel_wpt_2 = 0
	page_sel_wpt2_2 = 0
	page_sel_wpt3_2 = 0
	page_sel_wpt4_2 = 0
	page_sel_hold_wpt_2 = 0
	page_offset2 = 0
	page_legs_step2 = 0
	page_ref_nav_data2 = 0
	page_ref_nav_data_wpt2 = 0
	page_ref_nav_data_navaid2 = 0
	page_ref_nav_data_apt2 = 0
	page_ref_sel2 = 0
	page_fix2 = 0
	page_rte_legs2 = 0
	page_ac2 = 0
	page_ac_atis2 = 0
	page_ac_msg2 = 0
	page_nearest_apt2 = 0
	page_altern_apt2 = 0
	page_nav_status2 = 0
	page_atc2 = 0
	page_fmc_comm2 = 0
	page_dl_menu2 = 0
	page_dl_acars2 = 0
	page_dl_requests2 = 0
	page_dl_wxr_req2 = 0
	page_dl_wxr_info2 = 0
	page_dl_fltlog2 = 0
	page_dl_sensors2 = 0
	page_eo_climb2 = 0
	page_eo_cruise2 = 0
	
	vb_calc_id = ""
	vb_calc_alt = 0
	vb_calc_lat = 0
	vb_calc_lon = 0
	after_lift_2s = 0
	B738DR_fmc_cabin_rate = 500
	
	B738DR_nearest_apt_icao = "                    "
	for ttt = 0, 4 do
		B738DR_nearest_apt_dist[ttt] = 999999
		B738DR_nearest_apt_lat[ttt] = 0
		B738DR_nearest_apt_lon[ttt] = 0
	end
	
	standby_altimeter_baro_old = 0
	verify_to_spd = 0
	missed_app_alt_change = 0
	rw_dist2 = 0
	rw_fac_id2 = ""
	
	nd_vert_path_enable = false
	ga_data_deleted = false
	
	rte_header_str = "RTE  "
	rte_option_str = "RTE  "
	
	rte_header_str_fo = "RTE  "
	rte_option_str_fo = "RTE  "
	
	rte12_copy_done = 0
	rte21_copy_done = 0
	rte12_act = -1
	rte12_act_old = -1
	rte12_sel = 0
	
	engout_clb = 0
	engout_crz = 0
	version = "v3.55"
	
end


function B738_init3()
	legs_num2 = 0
	legs_data2 = {}
	offset_mod = 0
	fpln_num2 = 0
	fpln_data2 = {}
	hold_data_num2 = 0
	hold_data2 = {}
	hold_offset = 0
	hold_offset_idx = 0
	hold_offset_idx2 = 0
	
	ref_icao = "----"
	ref_icao2 = "----"
	ref_icao_x = "----"
	des_icao = "****"
	des_icao_x = "****"
	co_route = "------------"
	co_route_x = "------------"
	ref_rwy = "-----"

	ref_rwy2 = "-----"
	ref_rwy3 = "-----"
	des_rwy = "----"
	
	sel_ref_rwy2 = "-----"

	ref_tns_alt_x = 0
	ref_tns_lvl_x = 0
	ref_icao_alt_x = 0
	trans_alt_mod = "-----"
	ref_gate_x = "-----"
	des_tns_alt_x = 0
	des_tns_lvl_x = 0
	des_icao_alt_x = 0
	trans_lvl_mod = "-----"
	ref_sid2 = "------"
	ref_sid_tns2 = "------"
	des_star2 = "------"
	des_star_trans2 = "------"
	des_app2 = "------"
	des_app_tns2 = "------"
	des_star2_old = des_star2
	des_star_trans2_old = des_star_trans2
	des_app2_old = des_app2
	des_app_tns2_old = des_app_tns2
	ref_icao_lat_x = 0
	ref_icao_lon_x = 0
	des_icao_lat_x = 0
	des_icao_lon_x = 0
	des_app_rw_only_x = 0
	rw_ext_dist_x = "--.-"
	rw_ext_fpa_x = "-.--"

	ref_exec = 0
	des_exec = 0
	rte_exec = 0
	hold_exec = 0
	exec_activate_fpln = 0
	arr_data_x = 0
	
	
	last_sid_idx_mod = 0
	first_sid_idx_mod = 0
	iaf_app_idx_mod = 0
	first_star_idx_mod = 0
	first_app_idx_mod = 0
	-- id_ed_mod = ""
	idx_ed_mod = 0
	idx_ed_mod2 = 0
	-- idx_iaf_mod = 0
	rnav_idx_first_mod = 0
	rnav_idx_first_mod2 = 0
	rnav_idx_last_mod = 0
	rnav_idx_last_mod2 = 0
	rnav_alt_mod = 0
	rnav_alt_mod2 = 0
	rnav_vpa_mod = 0
	rnav_vpa_mod2 = 0
	
	---****
	ref_icao_lat = 0
	ref_icao_lon = 0
	ref_tns_alt = 0
	ref_tns_lvl = 0
	ref_icao_alt = 0
	trans_alt = "-----"

	ref_gate = "-----"

	ref_sid = "------"
	ref_sid_tns = "------"
	des_star = "------"
	des_star_trans = "------"
	des_app = "------"
	des_app_tns = "------"

	des_app_from_apt = 0

	des_rnw = ""
	des_rnw2 = ""
	des_icao_lat = 0
	des_icao_lon = 0
	des_tns_alt = 0
	des_tns_lvl = 0
	des_icao_alt = 0
	trans_lvl = "-----"

	des_runway_lat_nd_mod = 0
	des_runway_lon_nd_mod = 0
	des_runway_crs_nd_mod = 0
	des_runway_lenght_nd_mod = 0
	des_rnw_nd_mod = ""
	
	arr_data = 0

	offset_mod_x2 = 0
	last_offset2 = 0
	legs_num2mm = 0
	legs_data2mm = {}
	legs_num2m = 0
	legs_data2m = {}
	
end

function B738_init4()
	legs_num8 = 0
	legs_data8 = {}
	offset_mod2 = 0
	fpln_num4 = 0
	fpln_data4 = {}
	hold_data_num4 = 0
	hold_data4 = {}
	hold_offset2 = 0
	hold_offset_idx7 = 0
	hold_offset_idx8 = 0
	
	ref_icao7 = "----"
	ref_icao8 = "----"
	ref_icao7_x = "----"
	des_icao7 = "****"
	des_icao7_x = "****"
	co_route7 = "------------"
	co_route7_x = "------------"
	ref_rwy7 = "-----"
	ref_rwy8 = "-----"
	ref_rwy9 = "-----"
	des_rwy7 = "----"
	sel_ref_rwy8 = "-----"
	sel_ref_sid8 = "------"
	sel_ref_sid_tns8 = "------"

	sel_des_star8 = "------"
	sel_des_star_trans8 = "------"
	sel_des_app8 = "------"
	sel_des_app_tns8 = "------"
	
	ref_tns_alt7_x = 0
	ref_tns_lvl7_x = 0
	ref_icao_alt7_x = 0
	trans_alt_mod2 = "-----"
	ref_gate7_x = "-----"
	des_tns_alt7_x = 0
	des_tns_lvl7_x = 0
	des_icao_alt7_x = 0
	trans_lvl_mod2 = "-----"
	ref_sid8 = "------"
	ref_sid_tns8 = "------"
	des_star8 = "------"
	des_star_trans8 = "------"
	des_app8 = "------"
	des_app_tns8 = "------"
	des_star8_old = des_star8
	des_star_trans8_old = des_star_trans8
	des_app8_old = des_app8
	des_app_tns8_old = des_app_tns8
	ref_icao_lat7_x = 0
	ref_icao_lon7_x = 0
	des_icao_lat7_x = 0
	des_icao_lon7_x = 0
	des_app_rw_only7 = 0
	rw_ext_dist7 = "--.-"
	rw_ext_fpa7 = "-.--"
	des_app_rw_only7_x = 0
	rw_ext_dist7_x = "--.-"
	rw_ext_fpa7_x = "-.--"

	ref_exec2 = 0
	des_exec2 = 0
	rte_exec2 = 0
	hold_exec2 = 0
	exec_activate_fpln2 = 0

	arr_data7_x = 0
	last_sid_idx_mod2 = 0
	first_sid_idx_mod2 = 0
	iaf_app_idx_mod = 0
	first_star_idx_mod2 = 0
	first_app_idx_mod2 = 0
	-- id_ed_mod = ""
	idx_ed_mod = 0
	idx_ed_mod2 = 0
	-- idx_iaf_mod = 0
	rnav_idx_first_mod = 0
	rnav_idx_first_mod2 = 0
	rnav_idx_last_mod = 0
	rnav_idx_last_mod2 = 0
	rnav_alt_mod = 0
	rnav_alt_mod2 = 0
	rnav_vpa_mod = 0
	rnav_vpa_mod2 = 0


	ref_icao_lat7 = 0
	ref_icao_lon7 = 0
	ref_tns_alt7 = 0
	ref_tns_lvl7 = 0
	ref_icao_alt7 = 0
	trans_alt7 = "-----"

	ref_gate7 = "-----"
	co_route7 = "------------"

	ref_sid7 = "------"
	ref_sid_tns7 = "------"
	des_star7 = "------"
	des_star_trans7 = "------"
	des_app7 = "------"
	des_app_tns7 = "------"

	des_app_from_apt7 = 0

	des_rnw7 = ""
	des_rnw8 = ""
	
	
	des_icao_lat7 = 0
	des_icao_lon7 = 0
	des_tns_alt7 = 0
	des_tns_lvl7 = 0
	des_icao_alt7 = 0
	trans_lvl7 = "-----"

	des_runway_lat_nd_mod2 = 0
	des_runway_lon_nd_mod2 = 0
	des_runway_crs_nd_mod2 = 0
	des_runway_lenght_nd_mod2 = 0
	des_rnw_nd_mod2 = ""
	
	arr_data7 = 0

	offset_mod_x8 = 0
	last_offset8 = 0
	legs_num8mm = 0
	legs_data8mm = {}
	legs_num8m = 0
	legs_data8m = {}
	
	
	wind_data_clb_num7 = 0
	wind_data_clb_alt7 = {}
	wind_data_clb_hdg7 = {}
	wind_data_clb_spd7 = {}
	wind_data_toc_num7 = 0
	wind_data_toc_alt7 = {}
	wind_data_toc_hdg7 = {}
	wind_data_toc_spd7 = {}
	wind_data_tod_num7 = 0
	wind_data_tod_alt7 = {}
	wind_data_tod_hdg7 = {}
	wind_data_tod_spd7 = {}
	wind_data_des_num7 = 0
	wind_data_des_alt7 = {}
	wind_data_des_hdg7 = {}
	wind_data_des_spd7 = {}
	qnh_data_dest7 = ""
	avg_isa7 = ""
end

function chocks_on_start()

	if B738DR_chock_status == 1 and (simDR_brake == 0 or simDR_startup_running ~= 0) then
		B738DR_chock_status = 0
		set_chock = 0
	end
	
end


function B738_exec_light()
	
	local exec_light_tmp = 0
	
	-- Captain
	if rte12_sel == 0 then
		if page_cruise == 1 then
			if exec1_light == 1 or crz_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_descent == 1 then
			if B738DR_fms_descent_now == 1 or descent_exec == 1 or des_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_climb == 1 then
			if climb_exec == 1 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_ref_nav_data_wpt == 1 then
			if ref_nav_exec == 1 then
				exec_light_tmp = 1
			end
		elseif page_offset == 1 then
			if offset_act ~= offset_act_mod and legs_num > 1 then
				exec_light_tmp = 1
			end
		elseif page_hold > 0 then
			if hold_exec == 1 then
				exec_light_tmp = 1
			end
		elseif page_legs > 0 then
			if legs_intdir == 1 and legs_intdir_rte == rte12_sel then
				exec_light_tmp = 1
			end
		end
		
		if perf_exec == 1 or perf_exec == 3 then
			-- if page_perf == 1 or page_legs > 0 or page_climb > 0 or page_cruise > 0 or page_descent > 0 or page_rte_init > 0 or page_dep > 0 or page_arr > 0 or page_rte_legs > 0 then
			if page_perf == 1 or page_climb > 0 or page_cruise > 0 or page_descent > 0 then
				exec_light_tmp = 1
			elseif page_legs > 0 or page_rte_init > 0 or page_dep > 0 or page_arr > 0 or page_rte_legs > 0 and (rte12_act == 0) then
				exec_light_tmp = 1
			end
		end
		
		-- if ((legs_delete ~= 0 and legs_num > 1) or rte_exec ~= 0 or ref_exec ~= 0 or des_exec ~= 0) and rte12_act == 0 then
		if (((legs_delete ~= 0 and legs_num > 1) or ref_exec ~= 0 or des_exec ~= 0) and rte12_act == 0) or (rte_exec ~= 0 and rte12_act ~= rte12_sel) then
			if page_legs > 0 or page_rte_init > 0 or page_dep > 0 or page_arr > 0 or page_rte_legs > 0 then
				exec_light_tmp = 1
			end
		end
		
		if page_descent_forecast == 1 then
			if forecast_exec == 1 then
				exec_light_tmp = 1
			end
		end
		
		if fmc_enable == 0 then
			B738DR_fmc_exec_lights = 0
			exec_light_tmp = 0
		elseif exec_light_tmp == 1 then
			B738DR_fmc_exec_lights = 1
		else
			B738DR_fmc_exec_lights = 0
		end
		
		if B738DR_lights_test == 1 and fmc_enable ~= 0 then
			exec_light_tmp = 1
		end
		B738DR_fmc_exec_lights2 = exec_light_tmp
	else
		if page_cruise == 1 then
			if exec1_light == 1 or crz_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_descent == 1 then
			if B738DR_fms_descent_now == 1 or descent_exec == 1 or des_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_climb == 1 then
			if climb_exec == 1 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_ref_nav_data_wpt == 1 then
			if ref_nav_exec == 1 then
				exec_light_tmp = 1
			end
		elseif page_offset == 1 then
			if offset_act ~= offset_act_mod and legs_num > 1 then
				exec_light_tmp = 1
			end
		elseif page_hold > 0 then
			if hold_exec2 == 1 then
				exec_light_tmp = 1
			end
		elseif page_legs > 0 then
			if legs_intdir == 1 and legs_intdir_rte == rte12_sel then
				exec_light_tmp = 1
			end
		end
		
		if perf_exec == 1 or perf_exec == 3 then
			-- if page_perf == 1 or page_legs > 0 or page_climb > 0 or page_cruise > 0 or page_descent > 0 or page_rte_init > 0 or page_dep > 0 or page_arr > 0 or page_rte_legs > 0 then
			if page_perf == 1 or page_climb > 0 or page_cruise > 0 or page_descent > 0 then
				exec_light_tmp = 1
			elseif page_legs > 0 or page_rte_init > 0 or page_dep > 0 or page_arr > 0 or page_rte_legs > 0 and (rte12_act == 1) then
				exec_light_tmp = 1
			end
		end
		
		-- if ((legs_delete ~= 0 and legs_num > 1) or rte_exec2 ~= 0 or ref_exec2 ~= 0 or des_exec2 ~= 0) and rte12_act == 1 then
		if (((legs_delete ~= 0 and legs_num > 1) or ref_exec2 ~= 0 or des_exec2 ~= 0) and rte12_act == 1) or (rte_exec2 ~= 0 and rte12_act ~= rte12_sel) then
			if page_legs > 0 or page_rte_init > 0 or page_dep > 0 or page_arr > 0 or page_rte_legs > 0 then
				exec_light_tmp = 1
			end
		end
		
		if page_descent_forecast == 1 then
			if forecast_exec == 1 then
				exec_light_tmp = 1
			end
		end
		
		if fmc_enable == 0 then
			B738DR_fmc_exec_lights = 0
			exec_light_tmp = 0
		elseif exec_light_tmp == 1 then
			B738DR_fmc_exec_lights = 1
		else
			B738DR_fmc_exec_lights = 0
		end
		
		if B738DR_lights_test == 1 and fmc_enable ~= 0 then
			exec_light_tmp = 1
		end
		B738DR_fmc_exec_lights2 = exec_light_tmp
	end
	
	
	-- First Officier
	exec_light_tmp = 0
	
	if rte12_sel_fo == 0 then
		if page_cruise2 == 1 then
			if exec1_light == 1 or crz_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_descent2 == 1 then
			if B738DR_fms_descent_now == 1 or descent_exec == 1 or des_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_climb2 == 1 then
			if climb_exec == 1 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_ref_nav_data_wpt2 == 1 then
			if ref_nav_exec == 1 then
				exec_light_tmp = 1
			end
		elseif page_offset2 == 1 then
			if offset_act ~= offset_act_mod and legs_num > 1 then
				exec_light_tmp = 1
			end
		elseif page_hold2 > 0 then
			if hold_exec == 1 then
				exec_light_tmp = 1
			end
		elseif page_legs2 > 0 then
			if legs_intdir == 1 and legs_intdir_rte == rte12_sel_fo then
				exec_light_tmp = 1
			end
		end
		
		if perf_exec == 1 or perf_exec == 3 then	--or crz_exec ~= 0 then
			-- if page_perf2 == 1 or page_legs2 > 0 or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 or page_rte_init2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_legs2 > 0 then
			if page_perf2 == 1 or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 then
				exec_light_tmp = 1
			elseif page_legs2 > 0 or page_rte_init2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_legs2 > 0 and (rte12_act == 0) then
				exec_light_tmp = 1
			end
		end
		
		-- if ((legs_delete ~= 0 and legs_num > 1) or rte_exec ~= 0 or ref_exec ~= 0 or des_exec ~= 0) and rte12_act == 0 then
		if (((legs_delete ~= 0 and legs_num > 1) or ref_exec ~= 0 or des_exec ~= 0) and rte12_act == 0) or (rte_exec ~= 0 and rte12_act ~= rte12_sel_fo) then
			if page_legs2 > 0 or page_rte_init2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_legs2 > 0 then
				exec_light_tmp = 1
			end
		end
		
		if page_descent_forecast2 == 1 then
			if forecast_exec == 1 then
				exec_light_tmp = 1
			end
		end
		
		if B738DR_ac_tnsbus2_status == 0 or fmc_enable == 0 then
			B738DR_fmc_exec_lights_fo = 0
			exec_light_tmp = 0
		else
			if exec_light_tmp == 1 then
				B738DR_fmc_exec_lights_fo = 1
			else
				B738DR_fmc_exec_lights_fo = 0
			end
		end
		
		if B738DR_lights_test == 1 and (B738DR_ac_tnsbus2_status ~= 0 and fmc_enable ~= 0) then
			exec_light_tmp = 1
		end
		
		B738DR_fmc_exec_lights2_fo = exec_light_tmp
	else
		if page_cruise2 == 1 then
			if exec1_light == 1 or crz_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_descent2 == 1 then
			if B738DR_fms_descent_now == 1 or descent_exec == 1 or des_spd_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_climb2 == 1 then
			if climb_exec == 1 or clb_spd_exec ~= 0 or crz_exec ~= 0 then
				exec_light_tmp = 1
			end
		elseif page_ref_nav_data_wpt2 == 1 then
			if ref_nav_exec == 1 then
				exec_light_tmp = 1
			end
		elseif page_offset2 == 1 then
			if offset_act ~= offset_act_mod and legs_num > 1 then
				exec_light_tmp = 1
			end
		elseif page_hold2 > 0 then
			if hold_exec2 == 1 then
				exec_light_tmp = 1
			end
		elseif page_legs2 > 0 then
			if legs_intdir == 1 and legs_intdir_rte == rte12_sel_fo then
				exec_light_tmp = 1
			end
		end
		
		if perf_exec == 1 or perf_exec == 3 then
			-- if page_perf2 == 1 or page_legs2 > 0 or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 or page_rte_init2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_legs2 > 0 then
			if page_perf2 == 1 or page_climb2 > 0 or page_cruise2 > 0 or page_descent2 > 0 then
				exec_light_tmp = 1
			elseif page_legs2 > 0 or page_rte_init2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_legs2 > 0 and (rte12_act == 1) then
				exec_light_tmp = 1
			end
		end
		
		-- if ((legs_delete ~= 0 and legs_num > 1) or rte_exec2 ~= 0 or ref_exec2 ~= 0 or des_exec2 ~= 0) and rte12_act == 1 then
		if (((legs_delete ~= 0 and legs_num > 1) or ref_exec2 ~= 0 or des_exec2 ~= 0) and rte12_act == 1) or (rte_exec2 ~= 0 and rte12_act ~= rte12_sel_fo) then
			if page_legs2 > 0 or page_rte_init2 > 0 or page_dep2 > 0 or page_arr2 > 0 or page_rte_legs2 > 0 then
				exec_light_tmp = 1
			end
		end
		
		if page_descent_forecast2 == 1 then
			if forecast_exec == 1 then
				exec_light_tmp = 1
			end
		end
		
		if B738DR_ac_tnsbus2_status == 0 or fmc_enable == 0 then
			B738DR_fmc_exec_lights_fo = 0
			exec_light_tmp = 0
		else
			if exec_light_tmp == 1 then
				B738DR_fmc_exec_lights_fo = 1
			else
				B738DR_fmc_exec_lights_fo = 0
			end
		end
		
		if B738DR_lights_test == 1 and (B738DR_ac_tnsbus2_status ~= 0 and fmc_enable ~= 0) then
			exec_light_tmp = 1
		end
		
		B738DR_fmc_exec_lights2_fo = exec_light_tmp
	end
	
	if calc_rte_enable ~= 0 then
		fuel_calculated = 0
	end
	if calc_rte_enable2 ~= 0 then
		fuel_calculated2 = 0
	end
	

end

function del_alt_intv()
	del_all_rest_intv = 0
end

function angle()
	local xx = 0
	xx = (simDR_vvi_fpm_pilot / 60) * 0.3048
	xx = xx / simDR_ground_spd
	xx = -xx
	if xx < 0 then
		xx = 0
	end
	xx = math.atan(xx)
	xx = math.deg(xx)
end

function set_cg()
	simDR_cg = 0
	fuel_tank_correct()
end


function irs_timer()
	irs_enable = 0
end

function B738_ff_approx()
	local ff_total = (simDR_ff[0] + simDR_ff[1]) * 3600	-- kgs/hour
	ff_sample = ff_sample + 1
	ff_total_old = ff_total_old + ff_total
	if ff_sample > 4 then
		ff_approx = ff_total_old / ff_sample
		ff_sample = 0
		ff_total_old = 0
	end
	
end

function rst_fms_recalc()
	fms_recalc = 0
end

function B738_gw_approach()
	
	if gw_app_nul == 0 then
		-- gw_app = "***.*"
		-- gw_app_lbs = gw_app
		-- gw_app_kgs = gw_app
		-- --gw_app_nul = 0
		-- if legs_num > 1 then
			-- if legs_data[legs_num][40] ~= 0 then
				-- qqq = tonumber(zfw_kgs)
				-- if qqq ~= nil then
					-- qqq = ((tonumber(zfw_kgs) * 1000) + legs_data[legs_num][40]) / 1000
					-- if qqq < 0 then
						-- qqq = 0
					-- end
					-- if units == 0 then
						-- gw_app = string.format("%5.1f", (qqq * KGS_LBS))		-- to lbs
						-- gw_app_lbs = gw_app
						-- gw_app_kgs = string.format("%5.1f", (tonumber(gw_app) / KGS_LBS))		-- to kgs
					-- else
						-- gw_app = string.format("%5.1f", qqq)		-- to kgs
						-- gw_app_kgs = gw_app
						-- gw_app_lbs = string.format("%5.1f", (tonumber(gw_app) * KGS_LBS))		-- to lbs
					-- end
				-- end
			-- end
		-- end
		
		gw_app = gw
		gw_app_lbs = gw_lbs
		gw_app_kgs = gw_kgs
	end
	
end

function fpln_distance2(in_rte)
	
	local distance2 = 0
	local distance3 = 0
	
	if in_rte == 0 then
		if legs_num2 > 1 and calc_rte_enable2 == 0 then
		
			if nav_mode == 99 then
				B738DR_fpln_dist2 = B738DR_fmc_dist
			elseif offset > rnav_idx_last and rnav_idx_last ~= 0 then
				B738DR_fpln_dist2 = B738DR_fmc_dist
			else
				for n = 1, (legs_num2 + 1) do
					if des_app == "------" then
						distance2 = distance2 + wpt_distance2(n, in_rte)
					else
						if n <= rnav_idx_last then
							distance2 = distance2 + wpt_distance2(n, in_rte)
						else
							if n <= legs_num2 then
								distance3 = distance3 + wpt_distance2(n, in_rte)
							end
						end
					end
				end
				B738DR_fpln_dist2 = distance2
			end
		else
			B738DR_fpln_dist2 = 0
		end
		missed_app_distance = distance3
	elseif in_rte == 1 then
		if legs_num8 > 1 and calc_rte_enable8 == 0 then
		
			if nav_mode == 99 then
				B738DR_fpln_dist2 = B738DR_fmc_dist
			elseif offset > rnav_idx_last and rnav_idx_last ~= 0 then
				B738DR_fpln_dist2 = B738DR_fmc_dist
			else
				for n = 1, (legs_num8 + 1) do
					if des_app == "------" then
						distance2 = distance2 + wpt_distance2(n, in_rte)
					else
						if n <= rnav_idx_last then
							distance2 = distance2 + wpt_distance2(n, in_rte)
						else
							if n <= legs_num8 then
								distance3 = distance3 + wpt_distance2(n, in_rte)
							end
						end
					end
				end
				B738DR_fpln_dist2 = distance2
			end
		else
			B738DR_fpln_dist2 = 0
		end
		missed_app_distance2 = distance3
	end
end

function nocase (s)
	s = string.gsub(s, "%a", function (c)
	return string.format("%s", string.lower(c)) end) 
	return s
end

function check_cfg()
	if B738DR_b737_variant >= 0 then
		if B738DR_b737_variant == 4 then
			if B738DR_wings_variant > 1 then
				B738DR_wings_variant = 0
			end
		end
		if B738DR_wings_variant > 1 then
			if B738DR_no_pip_kill ~= 0 then
				B738DR_no_pip_kill = 0
				B738DR_pip_kill = 1
			end
		end
		if simDR_int_ver < 120000 then
			if B738DR_kill_windshield < 3 then
				B738DR_kill_windshield = 2
			else
				B738DR_kill_windshield = 3
			end
		else
			B738DR_kill_windshield = 3
		end
		B738DR_particle_rain = 0
		
		if (B738DR_b737_variant == 0 or B738DR_b737_variant == 2 or B738DR_b737_variant == 4) and B738DR_wings_variant < 2 then
			if B738DR_no_pip_kill ~= 0 then
				B738DR_no_eyebrow_kill = 0
				B738DR_eyebrow_kill = 1
				
			end
		end
		
		if B738DR_no_eyebrow_kill ~= 0 then
			B738DR_hud_disable = 1
		end
		
		if B738DR_b737_variant ~= 4 then
			B738DR_no_aft_exit_kill = 0
			B738DR_aft_exit_kill = 1
			
		end
		
		if B738DR_b737_variant == 1 or B738DR_b737_variant == 3 then
			B738DR_fuel_cutoff_sw = 0
		end
	end
end

function update_config()
	
	B738_default_fmod_config()
	B738_default_others_config()
	
	B738DR_geo_des = 0
	
	B738_load_hw_config()
	
	-- -- load config
	B738_load_config("")
	-- B738_load_cfg_config("")
	-- B738_load_fmod_config("")
	
	-- ovewrite config with livery
	B738_load_config(file_path_cfg)
	-- B738_load_fmod_config(file_path_cfg)
	-- B738_load_cfg_config(file_path_cfg)
	
	B738DR_restrict_fms = 0
	
	check_cfg()
	
	if B738DR_thr_cutback == 0 then
		clb_alt_num = B738DR_def_clb_alt
	else
		clb_alt_num = B738DR_def_clb_alt2
	end
	cut_alt_num = B738DR_def_cut_alt
	accel_alt_num = B738DR_def_accel_alt
	eo_accel_alt_num = B738DR_def_eo_accel_alt
	
	--B738DR_chock_status = B738DR_chock_on_startup
	chocks_on_start()
	if B738DR_chock_status == 0 then
		B738DR_cones_hide = 1
	else
		B738DR_cones_hide = 0
	end
	xfirst_5sec = 0
	
	if B738DR_b737_variant == 3 then
		--600
		-- Layout 4
		B738DR_pax_layout = 4
		B738DR_std_pax_num[0] = 24
		B738DR_std_pax_num[1] = 30
		B738DR_std_pax_num[2] = 24
		B738DR_std_pax_num[3] = 30
		B738DR_std_pax_num[4] = 30
		B738DR_zone_arm[0] = 9.3218	-- 367 in
		B738DR_zone_arm[1] = 12.827	-- 505 in
		B738DR_zone_arm[2] = 16.4592	-- 648 in
		B738DR_zone_arm[3] = 19.939	-- 785 in
		B738DR_zone_arm[4] = 24.003	-- 945 in
		B738DR_fa_fwd_kg = 190.5	-- kg
		B738DR_fa_aft_kg = 190.5	-- kg
		B738DR_galley_fwd_kg = 286	-- kg
		B738DR_galley_aft_kg = 410	-- kg
		B738DR_oew_kg = 36378 + 524 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 972)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1179)
		ident_str = "737-600"
		engine_1k = "22K"
		engine_2k = "20K"
		engine_3k = "18K"
	elseif B738DR_b737_variant == 2 or B738DR_b737_variant == 5 then
		--700
		-- Layout 4
		B738DR_pax_layout = 4
		B738DR_std_pax_num[0] = 24
		B738DR_std_pax_num[1] = 30
		B738DR_std_pax_num[2] = 24
		B738DR_std_pax_num[3] = 30
		B738DR_std_pax_num[4] = 30
		B738DR_zone_arm[0] = 9.3218	-- 367 in
		B738DR_zone_arm[1] = 12.827	-- 505 in
		B738DR_zone_arm[2] = 16.4592	-- 648 in
		B738DR_zone_arm[3] = 19.939	-- 785 in
		B738DR_zone_arm[4] = 24.003	-- 945 in
		B738DR_fa_fwd_kg = 190.5	-- kg
		B738DR_fa_aft_kg = 190.5	-- kg
		B738DR_galley_fwd_kg = 286	-- kg
		B738DR_galley_aft_kg = 410	-- kg
		B738DR_oew_kg = 37648 + 524 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 972)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1179)
		ident_str = "737-700"
		engine_1k = "24K"
		engine_2k = "22K"
		engine_3k = "20K"
	elseif B738DR_b737_variant == 0 or B738DR_b737_variant == 7 then
		--800
		if B738DR_pax_layout == 0 then
			B738DR_std_pax_num[0] = 16
			B738DR_std_pax_num[1] = 36
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 36
			B738DR_std_pax_num[4] = 36
			B738DR_zone_arm[0] = 6.6548	-- 262 in
			B738DR_zone_arm[1] = 11.43	-- 450 in
			B738DR_zone_arm[2] = 16.764	-- 660 in
			B738DR_zone_arm[3] = 21.463	-- 845 in
			B738DR_zone_arm[4] = 26.289	-- 1035 in
		elseif B738DR_pax_layout == 1 then
			B738DR_std_pax_num[0] = 36
			B738DR_std_pax_num[1] = 36
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 36
			B738DR_std_pax_num[4] = 36
			B738DR_zone_arm[0] = 6.6548	-- 262 in
			B738DR_zone_arm[1] = 11.43	-- 450 in
			B738DR_zone_arm[2] = 16.764	-- 660 in
			B738DR_zone_arm[3] = 21.463	-- 845 in
			B738DR_zone_arm[4] = 26.289	-- 1035 in
		elseif B738DR_pax_layout == 2 then
			B738DR_std_pax_num[0] = 27
			B738DR_std_pax_num[1] = 42
			B738DR_std_pax_num[2] = 34
			B738DR_std_pax_num[3] = 42
			B738DR_std_pax_num[4] = 42
			B738DR_zone_arm[0] = 5.842	-- 230 in
			B738DR_zone_arm[1] = 10.16	-- 400 in
			B738DR_zone_arm[2] = 15.748	-- 620 in
			B738DR_zone_arm[3] = 20.828	-- 820 in
			B738DR_zone_arm[4] = 26.035	-- 1025 in
		else
			B738DR_std_pax_num[0] = 27
			B738DR_std_pax_num[1] = 42
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 42
			B738DR_std_pax_num[4] = 42
			B738DR_zone_arm[0] = 5.842	-- 230 in
			B738DR_zone_arm[1] = 10.16	-- 400 in
			B738DR_zone_arm[2] = 15.748	-- 620 in
			B738DR_zone_arm[3] = 20.828	-- 820 in
			B738DR_zone_arm[4] = 26.035	-- 1025 in
		end
		B738DR_oew_kg = 41503 + 524 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 1177)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1225)
		ident_str = "737-800"
		engine_1k = "26K"
		engine_2k = "24K"
		engine_3k = "22K"
	elseif B738DR_b737_variant == 1 then	--or B738DR_b737_variant == 4 or B738DR_b737_variant == 8 then
		--900
		if B738DR_pax_layout == 0 then
			B738DR_std_pax_num[0] = 16
			B738DR_std_pax_num[1] = 36
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 36
			B738DR_std_pax_num[4] = 36
			B738DR_zone_arm[0] = 6.6548	-- 262 in
			B738DR_zone_arm[1] = 11.43	-- 450 in
			B738DR_zone_arm[2] = 16.764	-- 660 in
			B738DR_zone_arm[3] = 21.463	-- 845 in
			B738DR_zone_arm[4] = 26.289	-- 1035 in
		elseif B738DR_pax_layout == 1 then
			B738DR_std_pax_num[0] = 36
			B738DR_std_pax_num[1] = 36
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 36
			B738DR_std_pax_num[4] = 36
			B738DR_zone_arm[0] = 6.6548	-- 262 in
			B738DR_zone_arm[1] = 11.43	-- 450 in
			B738DR_zone_arm[2] = 16.764	-- 660 in
			B738DR_zone_arm[3] = 21.463	-- 845 in
			B738DR_zone_arm[4] = 26.289	-- 1035 in
		elseif B738DR_pax_layout == 2 then
			B738DR_std_pax_num[0] = 27
			B738DR_std_pax_num[1] = 42
			B738DR_std_pax_num[2] = 34
			B738DR_std_pax_num[3] = 42
			B738DR_std_pax_num[4] = 42
			B738DR_zone_arm[0] = 5.842	-- 230 in
			B738DR_zone_arm[1] = 10.16	-- 400 in
			B738DR_zone_arm[2] = 15.748	-- 620 in
			B738DR_zone_arm[3] = 20.828	-- 820 in
			B738DR_zone_arm[4] = 26.035	-- 1025 in
		else
			B738DR_std_pax_num[0] = 27
			B738DR_std_pax_num[1] = 42
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 42
			B738DR_std_pax_num[4] = 42
			B738DR_zone_arm[0] = 5.842	-- 230 in
			B738DR_zone_arm[1] = 10.16	-- 400 in
			B738DR_zone_arm[2] = 15.748	-- 620 in
			B738DR_zone_arm[3] = 20.828	-- 820 in
			B738DR_zone_arm[4] = 26.035	-- 1025 in
		end
		B738DR_oew_kg = 41518 + 524 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 1177)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1225)
		ident_str = "737-900"
		engine_1k = "26K"
		engine_2k = "24K"
		engine_3k = "22K"
	
	
	elseif B738DR_b737_variant == 4 then
		-- 900ER
		-- Layout 5
		B738DR_pax_layout = 5
		B738DR_std_pax_num[0] = 31
		B738DR_std_pax_num[1] = 46
		B738DR_std_pax_num[2] = 46
		B738DR_std_pax_num[3] = 46
		B738DR_std_pax_num[4] = 46
		B738DR_zone_arm[0] = 5.842	-- 230 in
		B738DR_zone_arm[1] = 10.16	-- 400 in
		B738DR_zone_arm[2] = 15.748	-- 620 in
		B738DR_zone_arm[3] = 20.828	-- 820 in
		B738DR_zone_arm[4] = 26.035	-- 1025 in
		B738DR_oew_kg = 44677 + 524 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 1177)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1225)
		ident_str = "737-900"
		engine_1k = "26K"
		engine_2k = "24K"
		engine_3k = "22K"
	
	elseif B738DR_73x == 2 then
		--700
		-- Layout 4
		B738DR_pax_layout = 4
		B738DR_std_pax_num[0] = 24
		B738DR_std_pax_num[1] = 30
		B738DR_std_pax_num[2] = 24
		B738DR_std_pax_num[3] = 30
		B738DR_std_pax_num[4] = 30
		B738DR_zone_arm[0] = 9.3218	-- 367 in
		B738DR_zone_arm[1] = 12.827	-- 505 in
		B738DR_zone_arm[2] = 16.4592	-- 648 in
		B738DR_zone_arm[3] = 19.939	-- 785 in
		B738DR_zone_arm[4] = 24.003	-- 945 in
		B738DR_fa_fwd_kg = 190.5	-- kg
		B738DR_fa_aft_kg = 190.5	-- kg
		B738DR_galley_fwd_kg = 286	-- kg
		B738DR_galley_aft_kg = 410	-- kg
		B738DR_oew_kg = 38172 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 972)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1179)
		engine_1k = "26K"
		engine_2k = "24K"
		engine_3k = "22K"
	else
		--800/900
		if B738DR_pax_layout == 0 then
			B738DR_std_pax_num[0] = 16
			B738DR_std_pax_num[1] = 36
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 36
			B738DR_std_pax_num[4] = 36
			B738DR_zone_arm[0] = 6.6548	-- 262 in
			B738DR_zone_arm[1] = 11.43	-- 450 in
			B738DR_zone_arm[2] = 16.764	-- 660 in
			B738DR_zone_arm[3] = 21.463	-- 845 in
			B738DR_zone_arm[4] = 26.289	-- 1035 in
		elseif B738DR_pax_layout == 1 then
			B738DR_std_pax_num[0] = 36
			B738DR_std_pax_num[1] = 36
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 36
			B738DR_std_pax_num[4] = 36
			B738DR_zone_arm[0] = 6.6548	-- 262 in
			B738DR_zone_arm[1] = 11.43	-- 450 in
			B738DR_zone_arm[2] = 16.764	-- 660 in
			B738DR_zone_arm[3] = 21.463	-- 845 in
			B738DR_zone_arm[4] = 26.289	-- 1035 in
		elseif B738DR_pax_layout == 2 then
			B738DR_std_pax_num[0] = 27
			B738DR_std_pax_num[1] = 42
			B738DR_std_pax_num[2] = 34
			B738DR_std_pax_num[3] = 42
			B738DR_std_pax_num[4] = 42
			B738DR_zone_arm[0] = 5.842	-- 230 in
			B738DR_zone_arm[1] = 10.16	-- 400 in
			B738DR_zone_arm[2] = 15.748	-- 620 in
			B738DR_zone_arm[3] = 20.828	-- 820 in
			B738DR_zone_arm[4] = 26.035	-- 1025 in
		else
			B738DR_std_pax_num[0] = 27
			B738DR_std_pax_num[1] = 42
			B738DR_std_pax_num[2] = 36
			B738DR_std_pax_num[3] = 42
			B738DR_std_pax_num[4] = 42
			B738DR_zone_arm[0] = 5.842	-- 230 in
			B738DR_zone_arm[1] = 10.16	-- 400 in
			B738DR_zone_arm[2] = 15.748	-- 620 in
			B738DR_zone_arm[3] = 20.828	-- 820 in
			B738DR_zone_arm[4] = 26.035	-- 1025 in
		end
		B738DR_oew_kg = 42034 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
		B738DR_galley_fwd_kg = math.min(B738DR_galley_fwd_kg, 1177)
		B738DR_galley_aft_kg = math.min(B738DR_galley_aft_kg, 1225)
		-- engine_1k = "26K"
		-- engine_2k = "24K"
		-- engine_3k = "22K"
		if B738DR_sfp == 0 then
			engine_1k = "26K"
			engine_2k = "24K"
			engine_3k = "22K"
		else
			engine_1k = "27K"
			engine_2k = "26K"
			engine_3k = "24K"
		end
	end
	for pp = 0, 2 do
		if B738DR_std_pax_weight[pp] < 25 or B738DR_std_pax_weight[pp] > 88.5 then
			if pp == 0 then
				B738DR_std_pax_weight[pp] = 87
			elseif pp == 1 then
				B738DR_std_pax_weight[pp] = 76
			else
				B738DR_std_pax_weight[pp] = 45
			end
		end
	end
	B738DR_fa_fwd_kg = math.min(B738DR_fa_fwd_kg, 190.5)
	B738DR_fa_aft_kg = math.min(B738DR_fa_aft_kg, 190.5)
	B738DR_fa_fwd_kg = math.max(B738DR_fa_fwd_kg, 0)
	B738DR_fa_aft_kg = math.max(B738DR_fa_aft_kg, 0)
	B738DR_galley_fwd_kg = math.max(B738DR_galley_fwd_kg, 0)
	B738DR_galley_aft_kg = math.max(B738DR_galley_aft_kg, 0)
	
end

function B738_update_sfp()
	if B738DR_sfp ~= sfp_old then
		if B738DR_b737_variant == -1 and B738DR_73x < 2 then
			if B738DR_sfp == 0 then
				engine_1k = "26K"
				engine_2k = "24K"
				engine_3k = "22K"
			else
				engine_1k = "27K"
				engine_2k = "26K"
				engine_3k = "24K"
			end
		end
	end
	sfp_old = B738DR_sfp
end

function check_livery()
	if livery_idx ~= simDR_livery_idx or ffirst_time == 0 then
		livery_path = simDR_livery
		livery = nocase(livery_path)
		if simDR_livery_idx == 0 then
			file_path_cfg = ""
		else
			file_path_cfg = livery_path
		end
		update_config()
	end
	livery_idx = simDR_livery_idx
end

function check_cd_state(cd_statex)
	local result = ""
	
	if string.len(cd_statex) > 95 then
		result = string.sub(cd_statex, 1, 28) .. "0" .. string.sub(cd_statex, 30, 93) .. "1" .. string.sub(cd_statex, 95, -1)
	end
	
	return result
end

function check_ta_state(cd_statex)
	local result = ""
	
	if string.len(cd_statex) > 95 then
		result = string.sub(cd_statex, 1, 28) .. "1" .. string.sub(cd_statex, 30, 93) .. "0" .. string.sub(cd_statex, 95, -1)
	end
	
	return result
end

function B738_state_func()
	local rstate_cd = ""
	local rstate_ta = ""
	local rstate_fl1 = ""
	local rstate_fl2 = ""
	
	if B738DR_state_cmd2 == 0 then
		B738DR_state_cmd2 = 1
	elseif B738DR_state_cmd2 == 1 then
		-- if string.len(B738DR_state_cmd) ~= 0 then
			-- B738DR_state_cmd = ""
		-- end
		B738DR_state_cmd = ""
		B738DR_state_cmd2 = 2
	elseif B738DR_state_cmd2 == 3 then
		-- load state
		if B738DR_state_cmd3 == 0 then
			-- load "CD"
			B738DR_state_cmd = read_state_cmd("CD")
			B738DR_state_cmd = check_cd_state(B738DR_state_cmd)
			B738DR_state_cmd2 = 0
		elseif B738DR_state_cmd3 == 1 then
			-- load "TA"
			B738DR_state_cmd = read_state_cmd("TA")
			B738DR_state_cmd = check_ta_state(B738DR_state_cmd)
			B738DR_state_cmd2 = 0
		elseif B738DR_state_cmd3 == 2 then
			-- load "FL1"
			B738DR_state_cmd = read_state_cmd("SW1")
			B738DR_state_cmd2 = 0
		else
			-- load "FL2"
			B738DR_state_cmd = read_state_cmd("SW2")
			B738DR_state_cmd2 = 0
		end
	elseif B738DR_state_cmd2 == 10 then
		-- state ready to save
		rstate_cd = read_state_cmd("CD")
		rstate_ta = read_state_cmd("TA")
		rstate_fl1 = read_state_cmd("SW1")
		rstate_fl2 = read_state_cmd("SW2")
		if B738DR_state_cmd3 == 0 then
			-- save "CD"
			B738DR_state_cmd = check_cd_state(B738DR_state_cmd)
			save_state_cmd(B738DR_state_cmd, rstate_ta, rstate_fl1, rstate_fl2)
		elseif B738DR_state_cmd3 == 1 then
			-- save "TA"
			B738DR_state_cmd = check_ta_state(B738DR_state_cmd)
			save_state_cmd(rstate_cd, B738DR_state_cmd, rstate_fl1, rstate_fl2)
		elseif B738DR_state_cmd3 == 2 then
			-- save "FL1"
			save_state_cmd(rstate_cd, rstate_ta, B738DR_state_cmd, rstate_fl2)
		else
			-- save "FL2"
			save_state_cmd(rstate_cd, rstate_ta, rstate_fl1, B738DR_state_cmd)
		end
		B738DR_state_cmd2 = 2
		B738DR_state_cmd3 = 0
		B738DR_state_cmd = ""
	elseif B738DR_state_cmd2 == 20 then
		-- reset CD
		rstate_ta = read_state_cmd("TA")
		rstate_fl1 = read_state_cmd("SW1")
		rstate_fl2 = read_state_cmd("SW2")
		save_state_cmd("", rstate_ta, rstate_fl1, rstate_fl2)
		B738DR_state_cmd2 = 2
	elseif B738DR_state_cmd2 == 21 then
		-- reset TA
		rstate_cd = read_state_cmd("CD")
		rstate_fl1 = read_state_cmd("SW1")
		rstate_fl2 = read_state_cmd("SW2")
		save_state_cmd(rstate_cd, "", rstate_fl1, rstate_fl2)
		B738DR_state_cmd2 = 2
	end
	
end

function reset_mmr()
	B738DR_mmr_cpt_act_value = B738DR_nav1_hz
	B738DR_mmr_cpt_act_mode = vor_or_ils(B738DR_nav1_hz)
	B738DR_mmr_cpt_stby_mode = vor_or_ils(B738DR_nav1_stby_hz)
	
	B738DR_mmr_cpt_stby_gls = tonumber(string.format("%05d", B738DR_mmr_cpt_stby_gls))
	if B738DR_mmr_cpt_stby_gls < 20000 or B738DR_mmr_cpt_stby_gls > 99999 then
		B738DR_mmr_cpt_stby_gls = 20000
	end
	local stby_hz = B738DR_nav1_stby_hz
	local mmr_txt = string.format("%05d", stby_hz)
	local mmr_txt2 = string.format("%05d", B738DR_mmr_cpt_stby_gls)
	for mmr_ii = 0, 4 do
		B738DR_mmr_cpt_stby_value[mmr_ii] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
		if B738DR_mmr_cpt_stby_mode == 0 then
			vor_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
			vor_cpt_stby_value2[mmr_ii+1] =  vor_cpt_stby_value[mmr_ii+1]
		else
			ils_cpt_stby_value[mmr_ii+1] = B738DR_mmr_cpt_stby_value[mmr_ii]
			ils_cpt_stby_value2[mmr_ii+1] = ils_cpt_stby_value[mmr_ii+1]
		end
		gls_cpt_stby_value[mmr_ii+1] = tonumber(string.sub(mmr_txt2, 5 - mmr_ii, 5 - mmr_ii))
	end
	
	stby_hz = stby_hz + 10
	if B738DR_mmr_cpt_stby_mode == 0 then
		if stby_hz > 11195 then
			stby_hz = 10810
		end
		mmr_txt = string.format("%05d", stby_hz)
		for mmr_ii = 0, 4 do
			ils_cpt_stby_value[mmr_ii+1] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
			ils_cpt_stby_value2[mmr_ii+1] = ils_cpt_stby_value[mmr_ii+1]
		end
	else
		if stby_hz > 11795 then
			stby_hz = 10800
		end
		mmr_txt = string.format("%05d", stby_hz)
		for mmr_ii = 0, 4 do
			vor_cpt_stby_value[mmr_ii+1] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
			vor_cpt_stby_value2[mmr_ii+1] =  vor_cpt_stby_value[mmr_ii+1]
		end
	end
	B738DR_mmr_cpt_err = 0
	
	B738DR_mmr_fo_act_value = B738DR_nav2_hz
	B738DR_mmr_fo_act_mode = vor_or_ils(B738DR_nav2_hz)
	B738DR_mmr_fo_stby_mode = vor_or_ils(B738DR_nav2_stby_hz)
	
	B738DR_mmr_fo_stby_gls = tonumber(string.format("%05d", B738DR_mmr_fo_stby_gls))
	if B738DR_mmr_fo_stby_gls < 20000 or B738DR_mmr_fo_stby_gls > 99999 then
		B738DR_mmr_fo_stby_gls = 20000
	end
	stby_hz = B738DR_nav2_stby_hz
	mmr_txt = string.format("%05d", stby_hz)
	mmr_txt2 = string.format("%05d", B738DR_mmr_fo_stby_gls)
	for mmr_ii = 0, 4 do
		B738DR_mmr_fo_stby_value[mmr_ii] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
		if B738DR_mmr_fo_stby_mode == 0 then
			vor_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
			vor_fo_stby_value2[mmr_ii+1] = vor_fo_stby_value[mmr_ii+1]
		else
			ils_fo_stby_value[mmr_ii+1] = B738DR_mmr_fo_stby_value[mmr_ii]
			ils_fo_stby_value2[mmr_ii+1] = ils_fo_stby_value[mmr_ii+1]
		end
		gls_fo_stby_value[mmr_ii+1] = tonumber(string.sub(mmr_txt2, 5 - mmr_ii, 5 - mmr_ii))
	end
	
	stby_hz = stby_hz + 10
	if B738DR_mmr_fo_stby_mode == 0 then
		if stby_hz > 11195 then
			stby_hz = 10810
		end
		mmr_txt = string.format("%05d", stby_hz)
		for mmr_ii = 0, 4 do
			ils_fo_stby_value[mmr_ii+1] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
			ils_fo_stby_value2[mmr_ii+1] = ils_fo_stby_value[mmr_ii+1]
		end
	else
		if stby_hz > 11795 then
			stby_hz = 10800
		end
		mmr_txt = string.format("%05d", stby_hz)
		for mmr_ii = 0, 4 do
			vor_fo_stby_value[mmr_ii+1] = tonumber(string.sub(mmr_txt, 5 - mmr_ii, 5 - mmr_ii))
			vor_fo_stby_value2[mmr_ii+1] = vor_fo_stby_value[mmr_ii+1]
		end
	end
	B738DR_mmr_fo_err = 0
	
	B738DR_mmr_cpt_stby_cur[0] = 0
	B738DR_mmr_cpt_stby_cur[1] = 0
	B738DR_mmr_cpt_stby_cur[2] = 0
	B738DR_mmr_cpt_stby_cursor = 0
	
	B738DR_mmr_fo_stby_cur[0] = 0
	B738DR_mmr_fo_stby_cur[1] = 0
	B738DR_mmr_fo_stby_cur[2] = 0
	B738DR_mmr_fo_stby_cursor = 0
	
end

function stdby_alt_LUP()
	
	if B738DR_standby_altimeter_baro ~= standby_altimeter_baro_old then
		
		standby_altimeter_baro_old = B738DR_standby_altimeter_baro
		
		local stdby_baro_val = 0
		
		local stdby_baro_inhg = string.format("%05.2f", standby_altimeter_baro_old)
		for xxz = 1, 5 do
			if xxz ~= 3 then
				stdby_baro_val = tonumber(string.sub(stdby_baro_inhg, xxz, xxz))
				if stdby_baro_val == nil then
					stdby_baro_val = 0
				end
				if xxz == 1 then
					B738DR_stdby_baro_inhg_1000 = stdby_baro_val
				elseif xxz == 2 then
					B738DR_stdby_baro_inhg_100 = stdby_baro_val
				elseif xxz == 4 then
					B738DR_stdby_baro_inhg_10 = stdby_baro_val
				elseif xxz == 5 then
					B738DR_stdby_baro_inhg_1 = stdby_baro_val
				end
			end
		end
		
		local stdby_baro_mb = string.format("%04d", 33.8638816 * standby_altimeter_baro_old)
		for xxz = 1, 4 do
			stdby_baro_val = tonumber(string.sub(stdby_baro_mb, xxz, xxz))
			if stdby_baro_val == nil then
				stdby_baro_val = 0
			end
			if xxz == 1 then
				B738DR_stdby_baro_mb_1000 = stdby_baro_val
			elseif xxz == 2 then
				B738DR_stdby_baro_mb_100 = stdby_baro_val
			elseif xxz == 3 then
				B738DR_stdby_baro_mb_10 = stdby_baro_val
			elseif xxz == 4 then
				B738DR_stdby_baro_mb_1 = stdby_baro_val
			end
		end
	end
end

function yd_ind()
	local yd_indicator_tgt = 0
	if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
		yd_indicator_tgt = simDR_beta
	end
	B738DR_yd_indicator = B738_set_anim_value(B738DR_yd_indicator, yd_indicator_tgt, -1, 1, 1)
end

--*************************************************************************************--
--** 				               XLUA EVENT CALLBACKS       	        			 **--
--*************************************************************************************--

--function aircraft_load() end

function aircraft_unload() 
	if dl_block_time >= 0 then
		save_prev_dl_flight()
	end
	B738_save_status(0)
end

function flight_start()
	
	-- B738DR_fms_test8 = 1
	
	-- Initial declaration
	ref_rwy_sel = {true, true, true, true, true}
	ref_rwy_sel[1] = ""
	ref_rwy_sel[2] = ""
	ref_rwy_sel[3] = ""
	ref_rwy_sel[4] = ""
	ref_rwy_sel[5] = ""

	ref_sid_sel = {true, true, true, true, true}
	ref_sid_sel[1] = ""
	ref_sid_sel[2] = ""
	ref_sid_sel[3] = ""
	ref_sid_sel[4] = ""
	ref_sid_sel[5] = ""

	ref_tns_sel = {true, true, true, true, true}
	ref_tns_sel[1] = ""
	ref_tns_sel[2] = ""
	ref_tns_sel[3] = ""
	ref_tns_sel[4] = ""
	ref_tns_sel[5] = ""

	des_star_sel = {true, true, true, true, true}
	des_star_sel[1] = ""
	des_star_sel[2] = ""
	des_star_sel[3] = ""
	des_star_sel[4] = ""
	des_star_sel[5] = ""

	des_star_tns_sel = {true, true, true, true, true}
	des_star_tns_sel[1] = ""
	des_star_tns_sel[2] = ""
	des_star_tns_sel[3] = ""
	des_star_tns_sel[4] = ""
	des_star_tns_sel[5] = ""

	des_app_sel = {true, true, true, true, true}
	des_app_sel[1] = ""
	des_app_sel[2] = ""
	des_app_sel[3] = ""
	des_app_sel[4] = ""
	des_app_sel[5] = ""

	des_tns_sel = {true, true, true, true, true}
	des_tns_sel[1] = ""
	des_tns_sel[2] = ""
	des_tns_sel[3] = ""
	des_tns_sel[4] = ""
	des_tns_sel[5] = ""

	ref_rwy_sel_fo = {true, true, true, true, true}
	ref_rwy_sel_fo[1] = ""
	ref_rwy_sel_fo[2] = ""
	ref_rwy_sel_fo[3] = ""
	ref_rwy_sel_fo[4] = ""
	ref_rwy_sel_fo[5] = ""

	ref_sid_sel_fo = {true, true, true, true, true}
	ref_sid_sel_fo[1] = ""
	ref_sid_sel_fo[2] = ""
	ref_sid_sel_fo[3] = ""
	ref_sid_sel_fo[4] = ""
	ref_sid_sel_fo[5] = ""

	ref_tns_sel_fo = {true, true, true, true, true}
	ref_tns_sel_fo[1] = ""
	ref_tns_sel_fo[2] = ""
	ref_tns_sel_fo[3] = ""
	ref_tns_sel_fo[4] = ""
	ref_tns_sel_fo[5] = ""

	des_star_sel_fo = {true, true, true, true, true}
	des_star_sel_fo[1] = ""
	des_star_sel_fo[2] = ""
	des_star_sel_fo[3] = ""
	des_star_sel_fo[4] = ""
	des_star_sel_fo[5] = ""

	des_star_tns_sel_fo = {true, true, true, true, true}
	des_star_tns_sel_fo[1] = ""
	des_star_tns_sel_fo[2] = ""
	des_star_tns_sel_fo[3] = ""
	des_star_tns_sel_fo[4] = ""
	des_star_tns_sel_fo[5] = ""

	des_app_sel_fo = {true, true, true, true, true}
	des_app_sel_fo[1] = ""
	des_app_sel_fo[2] = ""
	des_app_sel_fo[3] = ""
	des_app_sel_fo[4] = ""
	des_app_sel_fo[5] = ""

	des_tns_sel_fo = {true, true, true, true, true}
	des_tns_sel_fo[1] = ""
	des_tns_sel_fo[2] = ""
	des_tns_sel_fo[3] = ""
	des_tns_sel_fo[4] = ""
	des_tns_sel_fo[5] = ""
	
	dl_flights_ref = {true, true, true, true}
	dl_flights_des = {true, true, true, true}
	dl_flights_flt_num = {true, true, true, true}
	dl_flights_day = {true, true, true, true}
	dl_flights_out_hrs = {true, true, true, true}
	dl_flights_out_min = {true, true, true, true}
	dl_flights_out_fuel = {true, true, true, true}
	dl_flights_in_hrs = {true, true, true, true}
	dl_flights_in_min = {true, true, true, true}
	dl_flights_in_fuel = {true, true, true, true}
	dl_flights_on_hrs = {true, true, true, true}
	dl_flights_on_min = {true, true, true, true}
	dl_flights_on_fuel = {true, true, true, true}
	dl_flights_off_hrs = {true, true, true, true}
	dl_flights_off_min = {true, true, true, true}
	dl_flights_off_fuel = {true, true, true, true}
	dl_flights_flight_time = {true, true, true, true}
	dl_flights_block_time = {true, true, true, true}
	for iii = 1, 4 do
		dl_flights_ref[iii] = "----"
		dl_flights_des[iii] = "****"
		dl_flights_flt_num[iii] = "--------"
		dl_flights_day[iii] = 0
		dl_flights_out_hrs[iii] = 0
		dl_flights_out_min[iii] = 0
		dl_flights_out_fuel[iii] = -1
		dl_flights_in_hrs[iii] = 0
		dl_flights_in_min[iii] = 0
		dl_flights_in_fuel[iii] = -1
		dl_flights_on_hrs[iii] = 0
		dl_flights_on_min[iii] = 0
		dl_flights_on_fuel[iii] = -1
		dl_flights_off_hrs[iii] = 0
		dl_flights_off_min[iii] = 0
		dl_flights_off_fuel[iii] = -1
		dl_flights_flight_time[iii] = -1
		dl_flights_block_time[iii] = -1
	end
	
	fix_abm_act = {true, true, true, true, true}
	fix_abm_wpt_idx = {true, true, true, true, true}
	fix_abm_wpt_dist = {true, true, true, true, true}
	fix_abm_lat = {true, true, true, true, true}
	fix_abm_lon = {true, true, true, true, true}
	fix_abm_brg = {true, true, true, true, true}
	fix_abm_dist = {true, true, true, true, true}
	fix_abm_eta = {true, true, true, true, true}
	fix_abm_alt = {true, true, true, true, true}
	for eee = 1, 5 do
		fix_abm_act[eee] = 0
		fix_abm_wpt_idx[eee] = -1
		fix_abm_wpt_dist[eee] = -1
		fix_abm_lat[eee] = -1
		fix_abm_lon[eee] = -1
		fix_abm_brg[eee] = -1
		fix_abm_dist[eee] = -1
		fix_abm_eta[eee] = -1
		fix_abm_alt[eee] = -1
	end
	
	sel_flt_plns = {true, true, true, true, true}
	sel_flt_plns[1] = ""
	sel_flt_plns[2] = ""
	sel_flt_plns[3] = ""
	sel_flt_plns[4] = ""
	sel_flt_plns[5] = ""
	sel_flt_plns2 = {true, true, true, true, true}
	sel_flt_plns2[1] = ""
	sel_flt_plns2[2] = ""
	sel_flt_plns2[3] = ""
	sel_flt_plns2[4] = ""
	sel_flt_plns2[5] = ""
	
	vor_cpt_stby_value = {true, true, true, true, true}
	ils_cpt_stby_value = {true, true, true, true, true}
	gls_cpt_stby_value = {true, true, true, true, true}
	vor_fo_stby_value = {true, true, true, true, true}
	ils_fo_stby_value = {true, true, true, true, true}
	gls_fo_stby_value = {true, true, true, true, true}

	vor_cpt_stby_value2 = {true, true, true, true, true}
	ils_cpt_stby_value2 = {true, true, true, true, true}
	vor_fo_stby_value2 = {true, true, true, true, true}
	ils_fo_stby_value2 = {true, true, true, true, true}


	for iii = 1, 5 do
		vor_cpt_stby_value[iii] = 0
		ils_cpt_stby_value[iii] = 0
		gls_cpt_stby_value[iii] = 0
		vor_fo_stby_value[iii] = 0
		ils_fo_stby_value[iii] = 0
		gls_fo_stby_value[iii] = 0
		
		vor_cpt_stby_value2[iii] = 0
		ils_cpt_stby_value2[iii] = 0
		vor_fo_stby_value2[iii] = 0
		ils_fo_stby_value2[iii] = 0
	end
	
	--file_path .. 
	FILE_NAME_APT_DAT = "B738X_apt.dat"
	FILE_NAME_RNW_DAT = "B738X_rnw.dat"
	FILE_NAME_GATE_DAT = "B738X_gate.dat"
	FILE_NAME_CUST_DAT = "Custom Data/B738X_wptx.dat"
	
	if B738DR_b737_variant < 0 then
		if B738DR_73x == 0 then
			-- B738X
			PLANE_NAME = "b738"
			FILE_NAME_HW_CFG = "b738x_hw.cfg"
			FILE_NAME_CFG = "b738x.cfg"
			FILE_NAME_STATUS = "b738x_status.dat"
			FILE_NAME_FMOD_CFG = "b738x_fmod.cfg"
			FILE_NAME_ACF_CFG = "b738x_acf.cfg"
			FILE_NAME_FMOD = "fmod/b738.snd"
			ACF_TYPE = 0
			FILE_NAME_STATE = "b738x_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_73x == 1 then
			-- B739U
			PLANE_NAME = "b739"
			FILE_NAME_HW_CFG = "b739u_hw.cfg"
			FILE_NAME_CFG = "b739u.cfg"
			FILE_NAME_STATUS = "b739u_status.dat"
			FILE_NAME_FMOD_CFG = "b739u_fmod.cfg"
			FILE_NAME_FMOD = "fmod/b739.snd"
			ACF_TYPE = 1
			FILE_NAME_STATE = "b739u_state.dat"
			PLANE_NAME_2 = PLANE_NAME_2 - 1
			B738DR_ap_colour = 0
		elseif B738DR_73x == 2 then
			-- B737U
			PLANE_NAME = "b737"
			FILE_NAME_HW_CFG = "b737u_hw.cfg"
			FILE_NAME_CFG = "b737u.cfg"
			FILE_NAME_STATUS = "b737u_status.dat"
			FILE_NAME_FMOD_CFG = "b737u_fmod.cfg"
			FILE_NAME_FMOD = "fmod/b737.snd"
			ACF_TYPE = 2
			FILE_NAME_STATE = "b737u_state.dat"
			PLANE_NAME_2 = PLANE_NAME_2 + 7
			B738DR_ap_colour = 0
		end
	else
		PLANE_NAME = "b738"
		ACF_TYPE = 0
		if B738DR_b737_variant == 0 then
			FILE_NAME_HW_CFG = "737_80NG_hw.cfg"
			FILE_NAME_CFG = "737_80NG.cfg"
			FILE_NAME_STATUS = "737_80NG_status.dat"
			FILE_NAME_FMOD_CFG = "737_80NG_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_80NG_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_80NG.snd"
			FILE_NAME_STATE = "737_80NG_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 1 then
			FILE_NAME_HW_CFG = "737_90NG_hw.cfg"
			FILE_NAME_CFG = "737_90NG.cfg"
			FILE_NAME_STATUS = "737_90NG_status.dat"
			FILE_NAME_FMOD_CFG = "737_90NG_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_90NG_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_90NG.snd"
			FILE_NAME_STATE = "737_90NG_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 2 then
			FILE_NAME_HW_CFG = "737_70NG_hw.cfg"
			FILE_NAME_CFG = "737_70NG.cfg"
			FILE_NAME_STATUS = "737_70NG_status.dat"
			FILE_NAME_FMOD_CFG = "737_70NG_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_70NG_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_70NG.snd"
			FILE_NAME_STATE = "737_70NG_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 3 then
			FILE_NAME_HW_CFG = "737_60NG_hw.cfg"
			FILE_NAME_CFG = "737_60NG.cfg"
			FILE_NAME_STATUS = "737_60NG_status.dat"
			FILE_NAME_FMOD_CFG = "737_60NG_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_60NG_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_60NG.snd"
			FILE_NAME_STATE = "737_60NG_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 4 then
			FILE_NAME_HW_CFG = "737_9ENG_hw.cfg"
			FILE_NAME_CFG = "737_9ENG.cfg"
			FILE_NAME_STATUS = "737_9ENG_status.dat"
			FILE_NAME_FMOD_CFG = "737_9ENG_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_9ENG_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_9ENG.snd"
			FILE_NAME_STATE = "737_9ENG_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 5 then
			FILE_NAME_HW_CFG = "737_70MX_hw.cfg"
			FILE_NAME_CFG = "737_70MX.cfg"
			FILE_NAME_STATUS = "737_70MX_status.dat"
			FILE_NAME_FMOD_CFG = "737_70MX_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_70MX_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_70MX.snd"
			FILE_NAME_STATE = "737_70MX_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 6 then
			FILE_NAME_HW_CFG = "737_2HMX_hw.cfg"
			FILE_NAME_CFG = "737_2HMX.cfg"
			FILE_NAME_STATUS = "737_2HMX_status.dat"
			FILE_NAME_FMOD_CFG = "737_2HMX_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_2HMX_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_2HMX.snd"
			FILE_NAME_STATE = "737_2HMX_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 7 then
			FILE_NAME_HW_CFG = "737_80MX_hw.cfg"
			FILE_NAME_CFG = "737_80MX.cfg"
			FILE_NAME_STATUS = "737_80MX_status.dat"
			FILE_NAME_FMOD_CFG = "737_80MX_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_80MX_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_80MX.snd"
			FILE_NAME_STATE = "737_80MX_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 8 then
			FILE_NAME_HW_CFG = "737_90MX_hw.cfg"
			FILE_NAME_CFG = "737_90MX.cfg"
			FILE_NAME_STATUS = "737_90MX_status.dat"
			FILE_NAME_FMOD_CFG = "737_90MX_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_90MX_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_90MX.snd"
			FILE_NAME_STATE = "737_90MX_state.dat"
			B738DR_ap_colour = 0
		elseif B738DR_b737_variant == 9 then
			FILE_NAME_HW_CFG = "737_1KMX_hw.cfg"
			FILE_NAME_CFG = "737_1KMX.cfg"
			FILE_NAME_STATUS = "737_1KMX_status.dat"
			FILE_NAME_FMOD_CFG = "737_1KMX_fmod.cfg"
			FILE_NAME_ACF_CFG = "737_1KMX_acf.cfg"
			FILE_NAME_FMOD = "fmod/737_1KMX.snd"
			FILE_NAME_STATE = "737_1KMX_state.dat"
			B738DR_ap_colour = 0
		end
	end
	livery_path = simDR_livery
	livery_idx = simDR_livery_idx
	livery = nocase(livery_path)
	if simDR_livery_idx == 0 then
		file_path_cfg = ""
	else
		file_path_cfg = livery_path
	end
	
	update_header()
	
	B738_init2()
	B738_init3()
	B738_init4()
	
	B738_init_vspd()
	
	autogate_connect_status = 0
	autogate_arrival = 2
	
	update_apt_dat = 0
	
	sfp_old = -1
	
	-- fuel_tank_last_left = 3500
	-- fuel_tank_last_right = 3500
	
	if plane_already_loaded == 0 then
		
		update_config()
		
		detect_apt_dat()
		
		earth_nav_num = 0
		earth_nav = {}
		ils_nav_num = 0
		ils_nav = {}
		gls_nav_num = 0
		gls_nav = {}
		
		rnw_data_num = 0
		rnw_data = {}
		
		read_rnw_dat()
		
		check_navdata()		-- check and read navdata
		
		-- load custom waypoints
		wptx_n = 0
		wptx = {}
		load_cust_wpt()
		
		apt_data_num = 0
		apt_data = {}
		
		read_apt_dat()
		
		awy_data_num = 0
		awy_data = {}
		if airac_navdata ~= "" then
			read_awy_dat2()
		end
		
		create_idx_table()
		
		awy_path = {}
		awy_path_num = 0
		
		awy_temp_num2 = 0
		awy_temp2 = {}
		
		B738_detect_fmod()
		
		sfp_old = B738DR_sfp
		
	end
	
	fuel_tank_last_left = -1
	fuel_tank_last_center = -1
	fuel_tank_last_right = -1
	if B738DR_autoload_on == 1 then
		B738DR_autoload_on = 0
	end
	B738_load_status()
	B738_set_last_pos()
	
	if fuel_tank_last_left == -1 then
		fuel_tank_last_left = simDR_fuel_tank_weight_kg[0]
	end
	if fuel_tank_last_center == -1 then
		fuel_tank_last_center = simDR_fuel_tank_weight_kg[1]
	end
	if fuel_tank_last_right == -1 then
		fuel_tank_last_right = simDR_fuel_tank_weight_kg[2]
	end
	
	plane_already_loaded = 1
	
	reset_fmc_pages()
	reset_fmc_pages_fo()
	page_menu = 1
	page_menu2 = 1
	
	via_via_entry = ""
	via_via_ok = 0
	
	calc_lat = 0
	calc_lon = 0
	
	if B738DR_b737_variant < 0 then
		B738DR_rel = txt_trunc("RELEASE " .. version, 28)
		B738DR_fmod_rel = txt_trunc("SOUND PACK " .. fmod_version, 28)
		if B738DR_73x == 0 then
			if simDR_int_ver < 120000 then
				B738DR_fm_rel = txt_trunc("FLIGHT MODEL TWKSTER 4.5", 28)
			else
				-- B738DR_fm_rel = txt_trunc("FM TWKSTER 1.0 XP12", 28)
				B738DR_fm_rel = txt_trunc("FM EARLY ACCESS XP12 4.2", 28)
			end
		elseif B738DR_73x == 1 then
			B738DR_fm_rel = txt_trunc("FLIGHT MODEL TWKSTER 4.2", 28)
		elseif B738DR_73x == 2 then
			B738DR_fm_rel = txt_trunc("FLIGHT MODEL TWKSTER 4.2", 28)
		end
	else
		B738DR_rel = txt_trunc("RELEASE " .. version, 28)
		B738DR_fmod_rel = "CORNUI ZIBOMOD SKIN"
		B738DR_fm_rel = "THRESHOLD A.S."
	end
	
	simDR_kill_map_fms = 1
	simDR_elevator_trim = -0.39	-- set trim in middle (5 units)
	
	B738DR_wind_show = 0
	menu_timer = 0
	
	--cold n dark
	if simDR_startup_running == 0 and B738DR_chock_on_startup == 1 then
		B738DR_chock_status = 1
		set_chock = 1
		B738DR_cones_hide = 0
	else
		B738DR_chock_status = 0
		set_chock = 0
		B738DR_cones_hide = 1
	end
	
	---
	B738DR_state_cmd = ""
	B738DR_state_cmd2 = 0
	B738DR_state_cmd3 = 0
	if simDR_startup_running == 0 then
		if B738DR_engine_no_running_state == 0 then
			B738DR_state_cmd2 = 3
			B738DR_state_cmd3 = 0
			B738DR_hide_gpu = 1
			simDR_ext_pwr_1_on = 0
		else
			B738DR_hide_gpu = 0
			simDR_ext_pwr_1_on = 1
			B738DR_state_cmd2 = 3
			B738DR_state_cmd3 = 1
		end
	end
	
	if is_timer_scheduled(run_after_time) == true then
		stop_timer(run_after_time)
	end
	irs_enable = 0
	
	if is_timer_scheduled(B738_ff_approx) == false then
		run_at_interval(B738_ff_approx, 1)
	end
	
	if is_timer_scheduled(B738_gw_approach) == false then
		run_at_interval(B738_gw_approach, 5)
	end
	
	B738DR_dest_runway_alt = 0
	B738DR_dest_runway_len = 0
	B738DR_dest_runway_crs = 0
	B738DR_dest_runway_start_lat = 0
	B738DR_dest_runway_start_lon = 0
	B738DR_dest_runway_end_lat = 0
	B738DR_dest_runway_end_lon = 0
	B738DR_dest_runway_id = ""
	
	B738DR_dest_runway_len_mod = 0
	B738DR_dest_runway_crs_mod = 0
	B738DR_dest_runway_start_lat_mod = 0
	B738DR_dest_runway_start_lon_mod = 0
	B738DR_dest_runway_end_lat_mod = 0
	B738DR_dest_runway_end_lon_mod = 0
	
	B738_legs_num = 0
	B738DR_fms_legs_num2 = 0
	B738DR_fms_legs_num3 = 0
	B738_num_of_wpts = 0
	B738DR_glidepath_type = 0
	B738DR_fms_legs = ""
	B738_num_of_wpts_2 = 0
	B738_num_of_wpts_3 = 0
	
	vnav_mode = 0
	gp_mode = 0
	
	idle_alt = 40000
	file_idle_path = 0
	B738DR_idle_path = 0
	B738DR_idle_path_kts = 280
	B738DR_idle_path_mach = 0.78
	
	-- measure_distance = 0
	-- x_distance = 0
	-- touchdown_lat = 0
	-- touchdown_lon = 0
	chocks_on_start()
	
	--B738DR_mmr = 1
	if B738DR_mmr == 0 then
		B738DR_hide_mmr = 1
	else
		B738DR_hide_mmr = 0
	end
	reseted_mmr = 0
	reseted_mmr2 = 0
	first_time_mmr = 0
	mmr_old = 0
	B738DR_mmr_cpt_act_mode = 0
	B738DR_mmr_fo_act_mode = 0
	mmr_cpt_act_mode_old = -1
	mmr_fo_act_mode_old = -1
	--reset_mmr()
	
	tuned_nav1_id = ""
	B738DR_tuned_nav1_type = 0
	tuned_nav2_id = ""
	B738DR_tuned_nav2_type = 0
	
	tat_degc = simDR_TAT
	fmc1_input_lag = 0
	fmc2_input_lag = 0
	fmc1_screen_lag = 0
	fmc2_screen_lag = 0
	ffirst_time = 0
	save_fuel_timer = 0
	autosave_timer = 0
	autosave_old = 0
	B738DR_near_apt_icao = ""
	B738DR_autogate_nearest_name = ""
	missed_app_act_idx = 0
	reset_dl_times()
	B738DR_activate_pitch_mode = -1
	vanish_y_status = 0
	ffirst_time2 = 0
	B738DR_arrow_3d_kill = 1
	B738DR_arrow_3d_kill2 = 1
end

function B738_load_save_flt(in_mode)
	local save_load_fmc = 0
	if B738DR_load_save == 1 then
		if in_mode ~= 0 then
			if B738DR_load_save2 == 1 then
				save_fmc(1)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 901
			elseif B738DR_load_save2 == 2 then
				save_fmc(2)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 902
			elseif B738DR_load_save2 == 3 then
				save_fmc(3)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 903
			elseif B738DR_load_save2 == 4 then
				save_fmc(4)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 904
			elseif B738DR_load_save2 == 5 then
				save_fmc(5)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 905
			elseif B738DR_load_save2 == 6 then
				save_fmc(6)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 906
			elseif B738DR_load_save2 == 7 then
				save_fmc(7)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 907
			elseif B738DR_load_save2 == 8 then
				save_fmc(8)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 908
			elseif B738DR_load_save2 == 9 then
				save_fmc(9)
				B738DR_load_save = 0
				B738DR_zibomod_debug = 909
			end
			B738DR_load_save2 = 0
		end
	elseif B738DR_load_save == 2 then
		if in_mode == 0 then
			if B738DR_load_save2 == 1 then
				B738DR_zibomod_debug = 801
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(1)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 2 then
				B738DR_zibomod_debug = 802
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(2)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 3 then
				B738DR_zibomod_debug = 803
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(3)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 4 then
				B738DR_zibomod_debug = 804
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(4)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 5 then
				B738DR_zibomod_debug = 805
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(5)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 6 then
				B738DR_zibomod_debug = 806
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(6)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 7 then
				B738DR_zibomod_debug = 807
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(7)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 8 then
				B738DR_zibomod_debug = 808
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(8)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			elseif B738DR_load_save2 == 9 then
				B738DR_autoload_on = 2
				B738DR_zibomod_debug = 809
				B738DR_load_save = 3
				autosave_timer = 0
				save_load_fmc = load_fmc(9)
				calc_rte_enable = 1
				-- B738_calc_rte()
				after_load_flight_TMR = 8
				ffirst_time2 = 0
			end
			B738DR_load_save2 = 0
		end
	elseif B738DR_load_save == 3 then
		B738DR_load_save = 4
		autosave_timer = 0
	else
		if in_mode ~= 0 then
			B738DR_load_save = 0
			B738DR_load_save2 = 0
			if B738DR_zibomod_debug == 2 then
				B738DR_zibomod_debug = 0
			end
		end
	end
	if after_load_flight_TMR > 0 then
		after_load_flight_TMR = after_load_flight_TMR - SIM_PERIOD
		if after_load_flight_TMR < 0 then
			after_load_flight_TMR = 0
		end
	else
		after_load_flight_TMR = 0
	end
end

function check_offset()
	if true_legs_num(1) > 0 then
		offset = math.max(offset, 2)
		if offset_seq < offset or offset_seq > legs_num then
			offset_seq = offset
		end
	else
		offset = 0
		offset_seq = 0
	end
	
	if true_legs_num(2) > 0 then
		offset_mod = math.max(offset_mod, 2)
		if legs_num2 > 2 then
			if legs_data2[2][19] == 1 and string.sub(legs_data2[2][1], 1, 2) == "RW" and offset_mod == 2 then
				offset_mod = offset_mod + 1
			end
			if offset_mod > legs_num2 then
				offset_mod = legs_num2
			end
		end
	else
		offset_mod = 0
	end
	
	if true_legs_num(8) > 0 then
		offset_mod2 = math.max(offset_mod2, 2)
		if legs_num8 > 2 then
			if legs_data8[2][19] == 1 and string.sub(legs_data8[2][1], 1, 2) == "RW" and offset_mod2 == 2 then
				offset_mod2 = offset_mod2 + 1
			end
			if offset_mod2 > legs_num8 then
				offset_mod2 = legs_num8
			end
		end
	else
		offset_mod2 = 0
	end
end

--function flight_crash() end

--function before_physics() 

--end

function check_DR()
	
	local file_read = io.open("zibomod.cpp", "r")
	local line_read = ""
	local ii = 0
	local jj = 0
	local kk = 0
	local ll = 0
	local mm = 0
	local nn = 0
	local oo = 0
	local pp = 0
	
	raw_table('datarefs')
	local datarefs = {}
	raw_table('datarefs_used')
	local datarefs_used = {}
	local datarefs_num = 0
	local datarefs_name = ""
	local duplicate = 0
	
	if B738DR_fms_test8 == -1000 then
		
		B738DR_fms_test8 = 0
		
		dump_leg_append("DUPLICATE DATAREFS:\n")
		
		if file_read ~= nil then
			
			line_read = file_read:read()
			
			while line_read do
				ii, jj = string.find(line_read, "XPLMFindDataRef")
				if ii ~= nil then
					kk, ll = string.find(line_read, ";")
					if kk ~= nil then
						oo, pp = string.find(line_read, "sim/")
						-- oo, pp = string.find(line_read, "laminar/")
						if kk-3 > jj+3 and oo ~= nil then
							datarefs_name = string.sub(line_read, jj+3, kk-3)
							if datarefs_num == 0 then
								datarefs_num = datarefs_num + 1
								datarefs[datarefs_num] = datarefs_name
							else
								nn = 0
								for mm = 1, datarefs_num do
									if datarefs_name == datarefs[mm] then
										nn = 1
										duplicate = duplicate + 1
										dump_leg_append(datarefs_name .. "\n")
										break
									end
								end
								if nn == 0 then
									datarefs_num = datarefs_num + 1
									datarefs[datarefs_num] = datarefs_name
								end
							end
						end
					end
				end
				line_read = file_read:read()
			end
			
			file_read:close()
			
		end
		
		dump_leg_append(tostring(duplicate) .. "\n")
		
		-- dump_leg_append("\nALL DATAREFS:\n")
		-- dump_leg_append(tostring(datarefs_num) .. "\n")
		-- if datarefs_num > 0 then
			-- for mm = 1, datarefs_num do
				-- dump_leg_append(datarefs[mm] .. "\n")
			-- end
		-- end
	else
		
		B738DR_fms_test8 = 0
		
		dump_leg_append("UNDEFINED DATAREFS:\n")
		
		if file_read ~= nil then
			
			line_read = file_read:read()
			
			while line_read do
				ii, jj = string.find(line_read, "XPLMDataRef")
				if ii ~= nil then
					kk, ll = string.find(line_read, "=")
					if kk ~= nil then
						if kk - 1 > jj + 1 then
							oo = 0
							pp = 0
							for mm = jj + 1, kk - 1 do
								-- SPACE or TAB
								if string.byte(string.sub(line_read, mm, mm)) ~= 32 and string.byte(string.sub(line_read, mm, mm)) ~= 9 and oo == 0 then
									oo = mm
								elseif string.byte(string.sub(line_read, mm, mm)) == 32 and oo ~= 0 then
									pp = mm - 1
									break
								end
							end
							if oo ~= 0 and pp ~= 0 and pp > oo then
								if string.sub(line_read, oo, oo + 5) == "DRsim_" then
									datarefs_num = datarefs_num + 1
									datarefs[datarefs_num] = string.sub(line_read, oo, pp)
									datarefs_used[datarefs_num] = 0
								end
							end
						end
					end
				end
				line_read = file_read:read()
			end
			
			file_read:close()
			
		end
		
		-- if datarefs_num > 0 then
			-- for mm = 1, datarefs_num do
				-- dump_leg_append(datarefs[mm] .. "\n")
			-- end
		-- end
		
		file_read = io.open("zibomod.cpp", "r")
		
		if file_read ~= nil then
			
			line_read = file_read:read()
			
			while line_read do
				ii, jj = string.find(line_read, "XPLMFindDataRef")
				if ii ~= nil then
					kk, ll = string.find(line_read, "=")
					if kk ~= nil then
						oo = 0
						for mm = 1, kk do
							if string.byte(string.sub(line_read, mm, mm)) ~= 32 and string.byte(string.sub(line_read, mm, mm)) ~= 9 and oo == 0 then
								oo = mm
							elseif (string.byte(string.sub(line_read, mm, mm)) == 32 or string.byte(string.sub(line_read, mm, mm)) == 9) and oo ~= 0 then
								pp = mm - 1
								break
							end
						end
						if oo ~= 0 and pp ~= 0 and pp > oo then
							datarefs_name = string.sub(line_read, oo, pp)
							if string.sub(datarefs_name, 1, 6) == "DRsim_" then
								for mm = 1, datarefs_num do
									if datarefs_name == datarefs[mm] then
										datarefs_used[mm] = 1
										break
									end
								end
							end
						end
					end
				end
				
				
				line_read = file_read:read()
			end
			
			file_read:close()
			
		end
		
		if datarefs_num > 0 then
			for mm = 1, datarefs_num do
				if datarefs_used[mm] == 0 then
					dump_leg_append(datarefs[mm] .. " ->" .. tostring(datarefs_used[mm]) .. "\n")
				end
			end
		end
	end
	
end

function add_fmod_sound()
	local rotary = false
	if simDR_nav1_stdby_Mhz ~= nav1_stdby_Mhz then
		rotary = true
	elseif simDR_nav1_stdby_khz ~= nav1_stdby_khz then
		rotary = true
	elseif simDR_nav2_stdby_Mhz ~= nav2_stdby_Mhz then
		rotary = true
	elseif simDR_nav2_stdby_khz ~= nav2_stdby_khz then
		rotary = true
	end
	if rotary then
		if B738DR_fmod_nav_rotary == 0 then
			B738DR_fmod_nav_rotary = 1
		else
			B738DR_fmod_nav_rotary = 0
		end
	end
	nav1_stdby_Mhz = simDR_nav1_stdby_Mhz
	nav1_stdby_khz = simDR_nav1_stdby_khz
	nav2_stdby_Mhz = simDR_nav2_stdby_Mhz
	nav2_stdby_khz = simDR_nav2_stdby_khz
end

function after_physics() 

	if B738DR_mmr == 0 then
		add_fmod_sound()
	end
	land_alt = B738DR_land_alt_knob
	fmc_lock = page_sel_wpt + page_sel_wpt2 + page_sel_wpt3 + page_sel_wpt4 + page_sel_hold_wpt
	fmc2_lock = page_sel_wpt_2 + page_sel_wpt2_2 + page_sel_wpt3_2 + page_sel_wpt4_2 + page_sel_hold_wpt_2
	check_livery()
	full_crew_weight = 524 + B738DR_fa_fwd_kg + B738DR_fa_aft_kg + B738DR_galley_fwd_kg + B738DR_galley_aft_kg
	if ffirst_time == 0 then
		ffirst_time = ffirst_time + 1
		simDR_fuel_tank_weight_kg[0] = fuel_tank_last_left
		simDR_fuel_tank_weight_kg[1] = fuel_tank_last_center
		simDR_fuel_tank_weight_kg[2] = fuel_tank_last_right
		tat_degc = simDR_TAT
		simDR_payload_weight = full_crew_weight
		set_cg()
		init_fuel = simDR_fuel_weight
	end
	zfw_real = B738DR_oew_kg + simDR_payload_weight - full_crew_weight
	
	save_fuel_timer = save_fuel_timer + SIM_PERIOD
	if save_fuel_timer > 60 then
		save_fuel_timer = 0
		B738_save_status(1)
	end
	
	if B738DR_load_save == 4 then
		autosave_timer = 0
	elseif B738DR_autosave == 0 or B738DR_autosave ~= autosave_old or B738DR_autoload_on ~= 0 then
		if B738DR_autosave == 0 then
			B738DR_autoload_on = 0
		end
		autosave_timer = 0
	else
		local autosave_time = 0
		if B738DR_autosave == 1 then
			autosave_time = 60	-- 1 min
		elseif B738DR_autosave == 2 then
			autosave_time = 300	-- 5 min
		elseif B738DR_autosave == 3 then
			autosave_time = 900	--15 min
		end
		autosave_timer = autosave_timer + SIM_PERIOD
		if autosave_timer > autosave_time then
			autosave_timer = 0
			B738DR_load_save = 1
			B738DR_load_save2 = 9
		end
	end
	autosave_old = B738DR_autosave
	
	if B738DR_kill_fms == 0 then
		
		if B738DR_fmc_input_lag ~= 0 and fmc1_input_lag ~= 1 then
			fmc1_screen_lag = 0
		end
		fmc1_input_lag = 1
		if fmc1_screen_lag < FMS_SCREEN_LAG then
			fmc1_screen_lag = fmc1_screen_lag + SIM_PERIOD
			if fmc1_screen_lag > FMS_SCREEN_LAG then
				fmc1_screen_lag = 1
			end
		end
		
		if B738DR_fmc_input_lag ~= 0 and fmc2_input_lag ~= 1 then
			fmc2_screen_lag = 0
		end
		fmc2_input_lag = 1
		if fmc2_screen_lag < FMS_SCREEN_LAG then
			fmc2_screen_lag = fmc2_screen_lag + SIM_PERIOD
			if fmc2_screen_lag > FMS_SCREEN_LAG then
				fmc2_screen_lag = 1
			end
		end
		
		if fmc_enable == 0 then
			fmc1_input_lag = 0
			fmc2_input_lag = 0
		elseif calc_rte_enable2 ~= 0 or calc_rte_enable8 ~= 0 then
			fmc1_input_lag = 0
			fmc2_input_lag = 0
		end
		B738_load_save_flt(0)
		
		read_axis()
		probe_tat()
		B738_oat_n1_calc()
		mmr_active_mode()
		elevation_ft = simDR_elevation_m * 3.28083989501 --ft
		
		if legs_num <= 1 then
			rte12_act = -1
		elseif B738DR_no_rte2 == 0 then
			--temporary
			if legs_num > 1 and rte12_act == -1 then
				rte12_act = 0
			end
		else
			rte12_sel = 0
			rte12_sel_fo = 0
			if legs_num > 1 then
				rte12_act = 0
			end
		end
		
		rte_header_str = "RTE  "
		rte_option_str = "RTE  "
		-- legs_num_modx = legs_num2
		-- fpln_num_actx = fpln_num2
		-- offsetx = offset
		-- offset_modx = offset_mod
		
		rte_header_str_fo = "RTE  "
		rte_option_str_fo = "RTE  "
		-- legs_num_modx_fo = legs_num2
		-- fpln_num_actx_fo = fpln_num2
		-- offsetx_fo = offset
		-- offset_modx_fo = offset_mod
		
		if B738DR_no_rte2 == 0 then
			if legs_num <= 1 and rte12_act == 0 then
				rte12_copy_done = 0
			end
			if legs_num <= 1 and rte12_act == 1 then
				rte21_copy_done = 0
			end
			if rte12_sel == 0 then
				rte_header_str = "RTE 1"
				rte_option_str = "<RTE 2"
			else
				rte_header_str = "RTE 2"
				rte_option_str = "<RTE 1"
				-- legs_num_modx = legs_num8
				-- fpln_num_actx = fpln_num4
				-- offset_modx = offset_mod2
			end
			if rte12_sel_fo == 0 then
				rte_header_str_fo = "RTE 1"
				rte_option_str_fo = "<RTE 2"
			else
				rte_header_str_fo = "RTE 2"
				rte_option_str_fo = "<RTE 1"
				-- legs_num_modx_fo = legs_num8
				-- fpln_num_actx_fo = fpln_num4
				-- offset_modx_fo = offset_mod2
			end
		else
			rte12_copy_done = 0
			rte21_copy_done = 0
			rte12_sel = 0
			rte12_sel_fo = 0
			legs_num8 = 0
			legs_data8 = {}
			offset_mod2 = 0
		end
		
		if offset_act_mod >= 5 then
			if offset_act >= 5 then
				offset_act_mod = offset_act
			end
		end
		
		if xfirst_time3 ~= 0 then
			B738_fmc_light()
		else
			xfirst_time3 = 1
			calc_rte_enable = 1
			-- B738_calc_rte()
		end
		
		B738DR_fms_test1 = 0
		
		B738_fmc_on()
		B738_exec_light()
		
		B738_read_calc_data()
		-- B738DR_fms_test1 = 1
		-- B738_calc_rte()
		B738DR_fms_test1 = 2
		B738_calc_rte2()
		B738_calc_rte8()
		B738DR_fms_test1 = 70
		
		if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
			if climb_exec == 1 then
				climb_r_alt1_mod = B738DR_fmc_climb_r_alt1
				climb_r_speed1_mod = B738DR_fmc_climb_r_speed1
				fmc_climb_r_alt1_mod = fmc_climb_r_alt1
				climb_exec = 0
			end
			if clb_spd_exec ~= 0 then
				clb_spd_exec = 0
				fmc_climb_speed_mod = 0
				fmc_climb_speed_mach_mod = 0
			end
		end
		
		B738_gls_perf()
		if first_time_gls > 0 then
			B738_gls()
		end
		
		B738DR_fms_test1 = 3
		B738_calc()
		B738DR_fms_test1 = 4
	if calc_rte_enable == 0 then
		B738_fmc_calc()
		B738DR_fms_test1 = 444
		B738_mod_change2()
		B738DR_fms_test1 = 445
		B738_dist_to_dest()
		B738DR_fms_test1 = 5
		
		B738_vnav_calc()
	end
		B738DR_fms_test1 = 6
		B738_vnav_calc_mod()
		B738DR_fms_test1 = 7
	
	if calc_rte_enable == 0 then
		B738DR_fms_test1 = 8
		B738_vnav_pth3()
	end
		B738DR_fms_test1 = 81
		B738_path_err()
		B738DR_fms_test1 = 82
		B738_vrnp()
		B738DR_fms_test1 = 9
	if calc_rte_enable == 0 then
		B738_restrict_data()
		B738DR_fms_test1 = 449
		B738_fix_calc()
		
	end
	calc_rte_enable_old = calc_rte_enable
	
	ndx_lat = simDR_latitude
	ndx_lon = simDR_longitude
	ndx_mag_hdg = simDR_mag_hdg
	ndx_ahars_mag_hdg = simDR_ahars_mag_hdg
		
		if B738DR_kill_fms_navaid2 == 0 then
			-- if xfirst_time2 == 0 then
				B738DR_fms_test1 = 12
				B738DR_fms_test1 = 13
				B738DR_fms_test1 = 14
				if first_time_mmr ~= 0 then
					B738_vor_sel()
					B738_ils_disagree()
				end
				B738DR_fms_test1 = 15
				if calc_rte_enable == 0 then
					-- B738_displ_rnw()
					B738_displ_rnw2(0)
					B738_displ_rnw2(1)
				end
			-- end
		end
		
		B738DR_fms_test1 = 16

		B738DR_fms_test1 = 17
		
		if B738DR_kill_fms_navaid3 == 0 then
			B738DR_fms_test1 = 18
			if calc_rte_enable == 0 then
					-- if xfirst_time2 == 0 then
						B738_displ_acf()
					-- end
			end
		end

		B738DR_fms_test1 = 19
		-- if xfirst_time2 == 0 then
			if calc_rte_enable == 0 then
				B738_displ_fix()
			end
		-- end
		
		xfirst_time2 = xfirst_time2 + 1
		if xfirst_time2 > 2 then
			xfirst_time2 = 0
		-- else
			-- ndx_lat = simDR_latitude
			-- ndx_lon = simDR_longitude
			-- ndx_mag_hdg = simDR_mag_hdg
			-- ndx_ahars_mag_hdg = simDR_ahars_mag_hdg
		end
		
		-- if offset_act_mod >= 5 then
			-- if offset_act >= 5 then
				-- offset_act_mod = offset_act
			-- end
		-- end
		
		if xfirst_time2 == 0 then
			B738DR_fms_test1 = 20
			if fmc1_screen_lag == 1 then
				if calc_rte_enable == 0 then
					B738_fmc_disp_capt()
					
					B738DR_fms_test1 = 10
					-- B738_legs_step()
				end
			end
		elseif xfirst_time2 == 1 then
			B738DR_fms_test1 = 21
			if fmc2_screen_lag == 1 then
				if calc_rte_enable == 0 then
					B738_fmc_disp_fo()
					
					B738DR_fms_test1 = 11
					-- B738_legs_step2()
				end
			end
		end
		
		B738DR_fms_test1 = 22
		if calc_rte_enable == 0 then
			B738_flight_phase3()
		end
		B738DR_fms_test1 = 23
		B738_N1_thrust_calc()
		B738DR_fms_test1 = 24
		B738_N1_thrust_set()
		B738DR_fms_test1 = 25
		B738_N1_sel_thr()

		-- last position
		if irs_enable == 0 and xfirst_time2 == 0 then
			B738_pre_flt_status()
			B738DR_fms_test1 = 26
			B738_last_pos()
			B738DR_fms_test1 = 27
			B738_irs_sys()
			if is_timer_scheduled(irs_timer) == false then
				run_after_time(irs_timer, 2)
			end
			irs_enable = 1
		end
		
		B738DR_fms_test1 = 28
		if calc_rte_enable == 0 then
			B738_fmc_msg()
			B738_fmc_clr_msg()
		end
		B738DR_fms_test1 = 29
		B738DR_checklist()
		B738DR_fms_test1 = 30
		B738_des_now()
		B738DR_fms_test1 = 31
		if B738DR_plan_fuel ~= 0 then
			if plan_weight_mod ~= "---.-" then
				if simDR_eng_N2[0] > 50 or simDR_eng_N2[1] > 50 or was_on_air ~= 0 then
					local fuel_weight_n = tonumber(fuel_weight)
					local plan_weight_n = tonumber(plan_weight_mod)
					
					if fuel_weight_n == nil then
						fuel_weight_n = 0
					end
					if plan_weight_n == nil then
						plan_weight_n = 0
					end
					if fuel_weight_n ~= plan_weight_n then
						if v1_set ~= "---" or vr_set ~= "---" or v2_set ~= "---" then
							add_fmc_msg(VERIFY_TO_SPEEDS, 1)
							verify_to_spd = 1
						end
					end
					
					plan_weight = "---.-"
					plan_weight_kgs = "---.-"
					plan_weight_lbs = "---.-"
					
					plan_weight_mod = "---.-"
					plan_weight_kgs_mod = "---.-"
					plan_weight_lbs_mod = "---.-"
				end
			end
		end
		
		B738DR_fms_test1 = 32
		B738_pause_td()

		B738DR_fms_test1 = 33
		via_via_check()
		B738DR_fms_test1 = 34

		B738DR_fms_test1 = 35
		if calc_rte_enable == 0 then
			B738_vnav_desc_spd()
		end
		B738DR_fms_test1 = 36
		B738_wind()
		B738DR_fms_test1 = 37
		
		-- if first_time_apt == 1 then
		-- else
			-- first_time_apt = 1
		-- end
		
		if perf_exec == 0 then
			if cost_index ~= "***" and crz_alt_num > 0 and gw ~= "***.*" and zfw_calc ~= "---.-" and reserves ~= "**.*" then
				perf_exec = 1
				B738_calc_vnav_spd()
				vnav_update = 1
			end
		end
		
		B738DR_fms_test1 = 40
		rte_add_sid()
		B738DR_fms_test1 = 41
		rte_add_star()
		B738DR_fms_test1 = 42
		rte_add_app()
		
		B738DR_fms_test1 = 38
		
		if x_delay == 0 then
			x_delay = 1
			if B738DR_load_save ~= 0 then
				x_delay = 2
			end
		elseif x_delay == 1 then
			if simDR_radio_height_pilot_ft > 500 then
				-- local altitude_actual_1 = simDR_altitude_pilot + 200
				-- local altitude_actual_2 = simDR_altitude_pilot - 200
				-- if (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6) and B738DR_ap_on ~= 0 then
					-- altitude_actual_1 = B738DR_preselected_alt
					-- altitude_actual_2 = altitude_actual_1
				-- end
				-- alt_intv
				-- if crz_alt_num > 0 and altitude_actual_1 > crz_alt_num then
					-- B738DR_flight_phase = 2
				-- end
				B738DR_flight_phase = 2
			end
			x_delay = 2
		end
		
		fpln_distance2(rte12_act)
		
		B738_read_atis()
		
		last_offset = offset
		
		if calc_rte_enable == 0 then
			if legs_num > 1 then
				if rnav_idx_last ~= 0 and rnav_idx_last <= legs_num then
					B738DR_dest_fuel = legs_data[rnav_idx_last][40]	-- + 0.3
				else
					B738DR_dest_fuel = legs_data[legs_num+1][40]	-- + 0.3
				end
				if B738DR_dest_fuel < 0 then
					B738DR_dest_fuel = 0
				end
				if tc_idx == 0 then
					B738DR_tocw = -1
				elseif tc_idx <= legs_num + 1 and B738DR_flight_phase < 2 then
					B738DR_tocw = (simDR_total_weight / 1000) - (simDR_fuel_weight / 1000) + (fuel_tc / 1000)
				else
					B738DR_tocw = -1
				end
			else
				B738DR_dest_fuel = -1
				B738DR_tocw = -1
			end
		end
		
		-- B738_legs_num = legs_num
		if legs_num == 0 then
			B738DR_fms_legs = ""
			B738_num_of_wpts = 0
			B738DR_glidepath_type = 0
		end
		if des_app2 == "------" then
			des_app_rw_only_x = 0
		end
		
		if calc_rte_enable == 0 then
			if legs_num == 0 or des_icao == "****" then
				B738DR_pfd_rwy_show = 0
				B738DR_pfd_rwy_altitude = 0
				B738DR_ref_rwy_altitude = 0
				B738DR_des_rwy_altitude = 0
			else
				B738DR_pfd_rwy_show = 2
				local dist_departure = 0
				local dist_detination = 0
				if rte12_act == 0 then
					dist_departure = nd_calc_dist2(simDR_latitude, simDR_longitude, ref_icao_lat, ref_icao_lon)
					dist_detination = nd_calc_dist2(simDR_latitude, simDR_longitude, des_icao_lat, des_icao_lon)
				elseif rte12_act == 1 then
					dist_departure = nd_calc_dist2(simDR_latitude, simDR_longitude, ref_icao_lat7, ref_icao_lon7)
					dist_detination = nd_calc_dist2(simDR_latitude, simDR_longitude, des_icao_lat7, des_icao_lon7)
				end
				local time_to_destination = 30
				if simDR_ground_spd > 0 then
					time_to_destination = ((dist_detination * 1852) / simDR_ground_spd) / 60
				end
				-- distance to REF ICAO
				if (dist_departure > 400 or time_to_destination < 30) and B738DR_flight_phase ~= 0 then
					-- destination altitude
					B738DR_pfd_rwy_altitude = B738DR_des_rwy_altitude
				else
					B738DR_pfd_rwy_altitude = B738DR_ref_rwy_altitude
				end
			end
		end
		
		if rte12_act == 0 then
			if des_icao == "****" then
				B738DR_dest_icao = ""
			else
				B738DR_dest_icao = des_icao
			end
		elseif rte12_act == 1 then
			if des_icao7 == "****" then
				B738DR_dest_icao = ""
			else
				B738DR_dest_icao = des_icao7
			end
		end
		
		if calc_rte_enable == 0 then
			lnav_prior()
		end
		
		if tc_idx == 0 then
			B738DR_tc_lat = 0
			B738DR_tc_lon = 0
		end
		if td_idx == 0 then
			B738DR_td_lat = 0
			B738DR_td_lon = 0
		end
		if decel_idx == 0 then
			B738DR_decel_lat = 0
			B738DR_decel_lon = 0
		end
		
		B738_state_func()
		dump_idle_path()
		flight_record()
		
		B738DR_fms_test1 = 45
		legs_delete_old2 = legs_delete
		
		if xfirst_5sec < 3 then
			xfirst_5sec = xfirst_5sec + SIM_PERIOD
		else
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				B738DR_chock_status = 0
				set_chock = 0
			end
			dl_times()
		end
		
		if B738DR_chock_status == 0 then
			B738DR_cones_hide = 1
		else
			B738DR_cones_hide = 0
		end
		
		if first_time_mmr == 0 then
			first_time_mmr = 1
			mmr_old = -1
		elseif first_time_mmr == 1 then
			if B738DR_mmr ~= mmr_old then
				if B738DR_mmr == 0 then
					B738DR_hide_mmr = 1
					if reseted_mmr == 0 then
						reset_mmr()
						reseted_mmr = 1
					end
					reseted_mmr2 = 0
				else
					B738DR_hide_mmr = 0
					if reseted_mmr2 == 0 then
						reset_mmr()
						reseted_mmr2 = 1
					end
					reseted_mmr = 0
				end
			end
			mmr_old = B738DR_mmr
		end
		
		check_lat_offset()
		
		if calc_rte_enable == 0 then
			
			if offset_act >= 5 and offset > 1 then
				local off_end_tmp = offset_end
				if off_end_tmp == 0 then
					off_end_tmp = offset_end_def
				end
				if offset_seq > 0 and offset_seq <= legs_num then
					-- if legs_data[offset][46] == 0 and legs_data[offset-1][46] == 0 and off_end_tmp <= offset then
					if legs_data[offset_seq][46] == 0 and offset_seq >= off_end_tmp then
						offset_act = 0
						offset_start = 0
						offset_end = 0
						offset_dist = 0
						offset_side = 0
						if offset_act_mod >= 5 then
							offset_start_def_mod = 0
							offset_end_def_mod = 0
							offset_act_mod = 0
							offset_start_mod = 0
							offset_end_mod = 0
							offset_dist_mod = 0
							offset_side_mod = 0
						end
					elseif offset_act == offset_act_old then
						if offset_act >= 8 and (B738DR_xtrack > -2 and B738DR_xtrack < 2) then
							offset_act = 0
							offset_start = 0
							offset_end = 0
							offset_dist = 0
							offset_side = 0
							if offset_act_mod >= 5 then
								offset_start_def_mod = 0
								offset_end_def_mod = 0
								offset_act_mod = 0
								offset_start_mod = 0
								offset_end_mod = 0
								offset_dist_mod = 0
								offset_side_mod = 0
							end
						end
					end
				end
			end
			offset_act_old = offset_act
		end
		
		if rte12_sel == 0 then
			B738DR_fms_legs_num2 = legs_num2
			B738DR_offset_mod = offset_mod
		else
			B738DR_offset_mod = offset_mod2
			B738DR_fms_legs_num2 = legs_num8
		end
		if rte12_sel_fo == 0 then
			B738DR_fms_legs_num3 = legs_num2
			B738DR_offset_mod3 = offset_mod
		else
			B738DR_offset_mod3 = offset_mod2
			B738DR_fms_legs_num3 = legs_num8
		end
		
		B738DR_fms_rte12_act = rte12_act
		B738DR_fms_rte12_sel = rte12_sel
		B738DR_fms_rte12_sel_fo = rte12_sel_fo
		
		B738_activate()
		
		B738DR_fms_test1 = 1
		B738_calc_rte()
		
		B738_legs_num = legs_num
		B738_legs_step()
		B738_legs_step2()
		B738DR_legs_step = legs_step
		B738DR_legs_step2 = legs_step2
		
		B738DR_fms_test1 = 101
		
		if calc_rte_enable == 0 then
			legs_fill()
			B738DR_fms_test1 = 1001
			B738DR_fms_legs_change = 1
			-- B738DR_fms_legs_offset_start = offset_start
			-- B738DR_fms_legs_offset_end = offset_end
			
			local offset_tmpp = offset_start
			if offset_tmpp == 0 then
				offset_tmpp = offset_start_def
			end
			B738DR_fms_legs_offset_start = offset_tmpp
			B738DR_fms_legs_offset_start_t = offset_start
			
			offset_tmpp = offset_end
			if offset_tmpp == 0 then
				offset_tmpp = offset_end_def
			end
			B738DR_fms_legs_offset_end = offset_tmpp
			
			offset_tmpp = offset_start_mod
			if offset_tmpp == 0 then
				offset_tmpp = offset_start_def_mod
			end
			B738DR_fms_legs_offset_start_mod = offset_tmpp
			B738DR_fms_legs_offset_start_t_mod = offset_start_mod
			
			offset_tmpp = offset_end_mod
			if offset_tmpp == 0 then
				offset_tmpp = offset_end_def_mod
			end
			B738DR_fms_legs_offset_end_mod = offset_tmpp
			
			if calc_rte_enable2 == 0 and rte12_sel == 0 then
				legs_fill2(rte12_sel)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_2 = 1
			elseif calc_rte_enable8 == 0 and rte12_sel == 1 then
				-- B738DR_fms_test3 = legs_num8
				legs_fill2(rte12_sel)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_2 = 1
				
			end
			if calc_rte_enable2 == 0 and rte12_sel_fo == 0 then
				legs_fill3(rte12_sel_fo)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_3 = 1
			elseif calc_rte_enable8 == 0 and rte12_sel_fo == 1 then
				-- B738DR_fms_test3 = legs_num8
				legs_fill3(rte12_sel_fo)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_3 = 1
			end
		else
			if calc_rte_enable2 == 0 and rte12_sel == 0 then
				legs_fill2(rte12_sel)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_2 = 1
				
			elseif calc_rte_enable8 == 0 and rte12_sel == 1 then
				legs_fill2(rte12_sel)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_2 = 1
				
			end
			if calc_rte_enable2 == 0 and rte12_sel_fo == 0 then
				legs_fill3(rte12_sel_fo)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_3 = 1
				
			elseif calc_rte_enable8 == 0 and rte12_sel_fo == 1 then
				legs_fill3(rte12_sel_fo)
				B738DR_fms_test1 = 1002
				B738DR_fms_legs_change_3 = 1
				
			end
		end
		
		B738DR_fms_test1 = 102
		
		-- B738DR_fms_legs_offset_act = offset_act
		B738DR_fms_legs_offset_act_mod = offset_act_mod
		if offset > 0 and offset <= legs_num then
			B738DR_fms_legs_offset_dist = legs_data[offset][45]
			if offset_act >= 5 and legs_data[offset][45] ~= 0 then
				B738DR_fms_legs_offset_engage = 1
			else
				B738DR_fms_legs_offset_engage = 0
			end
		else
			B738DR_fms_legs_offset_engage = 1
			B738DR_fms_legs_offset_dist = 0
		end
		
		B738DR_fms_test1 = 103
		
		--local abeam_wpts = legs_abeam_data_n
		if legs_abeam_data_n > 0 then
			local abeam_wpts = legs_abeam_data_n
			if abeam_wpts > FMC_MAX_WPT then
				abeam_wpts = FMC_MAX_WPT
			end
			local abeam_new_id = ""
			for ab = 1, abeam_wpts do
				B738DR_legs_abeam_lat[ab-1] = legs_abeam_data[ab][2]
				B738DR_legs_abeam_lon[ab-1] = legs_abeam_data[ab][3]
				abeam_new_id = abeam_new_id .. legs_abeam_data[ab][4] .. " "
			end
			B738DR_legs_abeam_new_id = abeam_new_id
		end
		B738DR_legs_abeam_num = legs_abeam_data_n
		
		B738DR_fms_test1 = 104
		
		if B738DR_old_mcp == 0 then
			B738DR_ap_colour = 0
		else
			B738DR_ap_colour = 1
		end
		B738_update_sfp()
		stdby_alt_LUP()
		yd_ind()
		if calc_rte_enable == 0 then
			ffirst_time2 = 1
		end
		
		rte12_act_old = rte12_act
		
		B738_load_save_flt(1)
		B738DR_fms_test1 = 44
		
		if B738DR_fms_test == 456 then
			dump_fpln2()
			B738DR_fms_test = 0
		end
		
		check_offset()
		
		if B738DR_autoload_on == 2 then
			B738DR_autoload_on = 0
		end
		
	end
	
end

function after_replay()
	B738DR_chock_status = 0
	B738DR_cones_hide = 1
	
	local vor_hdg = 0
	if B738DR_capt_map_mode < 2 then
		vor_hdg =  simDR_ahars_mag_hdg
	elseif B738DR_capt_map_mode == 2 then
		if B738DR_track_up == 0 then
			vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
		else
			if B738DR_track_up_active == 0 then
				vor_hdg = simDR_ahars_mag_hdg
			else
				vor_hdg = simDR_mag_hdg
			end
		end
	elseif B738DR_capt_map_mode == 3 then
		vor_hdg = 0
	end
	
	B738DR_vor1_arrow = simDR_nav1_bearing - vor_hdg
	B738DR_vor2_arrow = simDR_nav2_bearing - vor_hdg
	
	B738DR_adf1_arrow = simDR_adf1_bearing - vor_hdg
	B738DR_adf2_arrow = simDR_adf2_bearing - vor_hdg
	
	B738DR_wind_arrow = (simDR_wind_hdg + 180 - vor_hdg) % 360
	
	if B738DR_fo_map_mode < 2 then
		vor_hdg = simDR_ahars_mag_hdg
	elseif B738DR_fo_map_mode == 2 then
		if B738DR_track_up == 0 then
			vor_hdg = simDR_ahars_mag_hdg -- simDR_mag_variation
		else
			if B738DR_track_up_active == 0 then
				vor_hdg = simDR_ahars_mag_hdg
			else
				vor_hdg = simDR_mag_hdg
			end
		end
	elseif B738DR_fo_map_mode == 3 then
		vor_hdg = 0
	end
	
	B738DR_vor1_arrow_fo = simDR_nav1_bearing - vor_hdg
	B738DR_vor2_arrow_fo = simDR_nav2_bearing - vor_hdg
	
	B738DR_adf1_arrow_fo = simDR_adf1_bearing - vor_hdg
	B738DR_adf2_arrow_fo = simDR_adf2_bearing - vor_hdg
	
	B738DR_wind_arrow_fo = (simDR_wind_hdg + 180 - vor_hdg) % 360
end

--*************************************************************************************--
--** 				               SUB-MODULE PROCESSING       	        			 **--
--*************************************************************************************--

-- dofile("")



