--[[
*****************************************************************************************
* Program Script Name	:	B738.glaresheild
*
* Author Name			:	Alex Unruh
*
*   Revisions:
*   -- DATE --	--- REV NO ---		--- DESCRIPTION ---
*   2016-08-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

--]]


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

-- PFD SPEED MODES
PFD_SPD_ARM = 1
PFD_SPD_N1 = 2
PFD_SPD_MCP_SPD = 3
PFD_SPD_FMC_SPD = 4
PFD_SPD_GA = 5
PFD_SPD_THR_HLD = 6
PFD_SPD_RETARD = 7

-- PFD ROLL MODES
PFD_HDG_HDG_SEL = 1
PFD_HDG_VOR_LOC = 2
PFD_HDG_LNAV = 3
PFD_HDG_ROLLOUT = 4
PFD_HDG_FAC = 5

-- PFD PITCH MODES
PFD_ALT_VS = 1
PFD_ALT_MCP_SPD = 2
PFD_ALT_ALT_ACQ = 3
PFD_ALT_ALT_HOLD = 4
PFD_ALT_GS = 5
PFD_ALT_FLARE = 6
PFD_ALT_GP = 7
PFD_ALT_VNAV_SPD = 8
PFD_ALT_VNAV_PTH = 9
PFD_ALT_VNAV_ALT = 10
PFD_ALT_TO_GA = 11

-- PFD ROLL MODES ARM
PFD_HDG_VOR_LOC_ARM = 1
PFD_HDG_ROLLOUT_ARM = 2
PFD_HDG_LNAV_ARM = 3
PFD_HDG_FAC_ARM = 4
PFD_HDG_LNAV_VOR_LOC_ARM = 5
PFD_HDG_LNAV_ROLLOUT_ARM = 6
PFD_HDG_LNAV_FAC_ARM = 7

-- PFD PITCH MODES ARM
PFD_ALT_GS_ARM = 1
PFD_ALT_VS_ARM = 2
PFD_ALT_FLARE_ARM = 3
PFD_ALT_GP_ARM = 4
PFD_ALT_VNAV_ARM = 5
PFD_ALT_GS_VS_ARM = 6
PFD_ALT_GP_VS_ARM = 7

blink_rec_thr_status = 0
blink_rec_thr2_status = 0
blink_rec_hdg_status = 0
blink_rec_alt_status = 0
blink_rec_cmd_status1 = 0
blink_rec_cmd_status2 = 0
blink_rec_sch_status = 0
blink_rec_schg_status = 0
blink_rec_cws_r_status = 0
blink_rec_cws_p_status = 0
blink_rec_alt_alert_status = 0
blink_rec_spd_ind = 0
spd_ind_mode_old = 0

blink_to_ga = 0
blink_out = 0
blink_out_ap = 0
blink_out_at = 0

pfd_spd_old = 0
pfd_hdg_old = 0
pfd_alt_old = 0


autothrottle_pfd_old = 0
autothrottle_status_old = 0
retard_status_old = 0
n1_status_old = 0
fmc_spd_status_old = 0
alt_mode_status_old = 0
vnav_alt_status_old = 0
vnav_pth_status_old = 0
vnav_spd_status_old = 0
flare_status_old = 0
hdg_mode_status_old = 0
ga_pfd_old = 0
thr_hld_pfd_old = 0
to_ga_pfd_old = 0
alt_acq_pfd_old = 0
cmd_old1 = 0
cmd_old2 = 0
single_ch_status_old = 0
single_ch_g_status_old = 0
autopilot_cws_r_status_old = 0
autopilot_cws_p_status_old = 0

null_vvi = 0
ap_pitch_mode = 0
ap_roll_mode = 0
ap_pitch_mode_eng = 0
ap_roll_mode_eng = 0
ap_on = 0
ap1_on = 0
ap2_on = 0
ap_on_first = 0
at_on_first = 0
cws_on = 0
cws_on_first = 0
ap_disco_first = 0
ap_dis_time = 0
at_dis_time = 0
mem_airspeed_dial = 100
mem_speed_mode = 0
at_mode = 0
at_mode_eng = 0
ap_pitch_mode_old = 0
ap_roll_mode_old = 0
approach_status_old = 0
glideslope_status_old = 0
ap_pitch_mode_old2 = 0
takeoff_n1 = 0
cmd_first = 0
ap_app_block = 0
ap_app_block_800 = 0
ils_test_enable = 0
ils_test_on = 0
ils_test_ok = 0
ils_test_ok2 = 0
fd_cur = 0
flare_vvi_old = 0

ap_vnav_status = 0
ap_lnav_status = 0

to_after_80kts = 0
lift_off_150 = 0
on_ground_30 = 1
ap_goaround_block = 0
to_thrust_set = 0
ias_disagree = 0
alt_disagree = 0
lights_test_old = 0
vnav_alt = 0

eng1_N1_man = 0.984
eng2_N1_man = 0.984

-- at_throttle_hold = 0
init_climb = 0

baro_sel_old = 0
baro_sel_co_old = 0

fmc_vvi_cur = 0
vvi_trg = 0

thrust1 = 0
thrust2 = 0
airspeed_pilot_old = 0
v_speed_pilot_old = 0
vdot_ratio_old = 0
radio_height_old = 0
altitude_pilot_old = 0
vvi_pilot_old = 0
air_on_acf_old = 0
eng1_N1 = 0
eng1_N1_old = 0
eng2_N1 = 0
eng2_N1_old = 0

vnav_vs = 0
lvl_chg_vs = 0
vnav_alt_hld = 0
ap_spd_interv_status_old = 0
at_mode_vnav = 0
altitude_dial_ft_old = 0

reverse_max_enable1 = 0
reverse_max_on1 = 0
reverse_max_enable2 = 0
reverse_max_on2 = 0

radio_dh_pilot = 0
baro_dh_pilot = 0
baro_dh_pilot_disable = 0
radio_dh_copilot = 0
baro_dh_copilot = 0
baro_dh_copilot_disable = 0

radio_dh_pilot_mem = 0
baro_dh_pilot_mem = 0
radio_dh_copilot_mem = 0
baro_dh_copilot_mem = 0

dh_minimum_pilot = 0
dh_minimum_copilot = 0
dh_min_enable_pilot = 0
dh_min_enable_copilot = 0
dh_min_block_pilot = 0
dh_min_block_copilot = 0

first_time_baro_cpt = 0
first_time_baro_fo = 0

vnav_init = 0
vnav_init2 = 0

vs_first = 0
initial_vs_alt = 0
mdah_was_enable = 0

vnav_alt_hold = 0
vnav_alt_hold_act = 0
mcp_alt_hold = 0
rest_wpt_alt_old = 0
vnav_engaged = 0
ap_alt_hold_arm = 0
toga_was_engaged = 0

thr1_target = 0
thr2_target = 0

roll_mode_old = 0
pitch_mode_old = 0
at_on_old = 0
ap_disco_do = 0

vnav_desc_spd = 0
vnav_desc_protect_spd = 0

axis_throttle1_old = 0
axis_throttle2_old = 0
ap_disco2 = 0

turn_active = 0
intdir_act = 0

thr1_anim = 0
thr2_anim = 0
ap_wca_wind_hdg = 0
ap_wca_wind_spd = 0

fd_go_disable = 0
ap_go_disable = 0

vert_spd_timer = 0
vert_spd_timer2 = 0
at_status = 0
lvl_chg_co_status = 0
vs_co_status = 0
spd_interv_co_used = 0
retard_timer = 0

at_left_press = 0
at_right_press = 0

master_light_block_act = 0

cpt_tcas_enable = 1
fo_tcas_enable = 1
cpt_terr_enable = 0
fo_terr_enable = 0
cpt_pos_enable = 0
fo_pos_enable = 0

autothr_arm_pos = 0
autopilot_side = 0
autopilot_master_first = 0

mcp_hdg_dial_old = 0
hide_hdg_line = 0
hide_hdg_line_fo = 0
	

alt_acq_disable = 0

spd_ratio = 0
spd_ratio_old = 0
spd_ratio_sum = 0
spd_ratio_num = 0
time_spd_ratio = 0

dspl_ctrl_pnl_old = 0
in_hpa_cnt = 0

shake_cpt = 0
shake_fo = 0

relative_brg_old = 0

gear_deploy_0_old = 0
gear_deploy_1_old = 0
gear_deploy_2_old = 0

yoke_test_disabled = 0

thrust1_leveler_mem = 0
thrust2_leveler_mem = 0
flare_status_mem = 0
autothr_arm_pos_mem = 0
autothrottle_status_mem = 0

was_replay = 0
alt_deviation_active = 0
lnav_tune_enable = 0
idx_corr2_anim = 0
max_idx_dist_anim = 0
max_idx_dist_anim2 = 0
integral = 0

lvlchg_step_init = 0
vnav_step_init = 0

hold_bank_angle_enable = 0
hold_bank_angle = 0
pfd_loc_exp = 0
pfd_loc_exp_fo = 0
pfd_fac_exp = 0
pfd_fac_exp_fo = 0
pfd_gls_exp = 0
pfd_gls_exp_fo = 0

vor_loc1_captured = 0
vor_loc2_captured = 0
alt_hold_captured = 0
vs_armed = 0
vs_armed_enable = 0
alt_hld_mcp_alt = 0

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

local autopilot_cmd_a_status = 0
local autopilot_cmd_b_status = 0
local autopilot_cws_a_status = 0
local autopilot_cws_b_status = 0
local autopilot_cws_status = 0
local autopilot_cws_r_status = 0
local autopilot_cws_p_status = 0
autopilot_master_capt_status = 0
autopilot_master_fo_status = 0
autopilot_autothrottle_status = 0

local autopilot_fms_nav_status = 0

-- = 0 in vorloc mode and 1 in lnav mode, buttons swap. Use this to determine NAV 1,2, FMS Selection logic

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

local baro_pilot_timer = 0
local baro_pilot_up_active = 0
local baro_pilot_dn_active = 0
local baro_copilot_timer = 0
local baro_copilot_up_active = 0
local baro_copilot_dn_active = 0
local aircraft_was_on_air = 0
local bellow_400ft = 0

--*************************************************************************************--
--** 					            LOCAL VARIABLES                 				 **--
--*************************************************************************************--

local altitude_dial_ft = 0
local yoke_pitch = 0
local yoke_pitch2 = 0
local vorloc_only = 0
local throttle = 0
local throttle2 = 0

SPD_act2 = 0
spd_ratio2 = 0
lock_at = 0
at_lock_time = 0
at_lock_num = 0
at_spd_ratio = 0
at_lock_time2 = 0
at_spd = 0

SPD_err_old = 0
SPD_p = 0
SPD_i = 0
SPD_d = 0
SPD_out = 0
pid_time = 0

wind_hdg_old = 0
wind_spd_old = 0
wind_hdg_rel_old = 0

altitude_mode_old = 0
flight_phase_old = 0

capt_exp_map_mode = 0
fo_exp_map_mode = 0
capt_vsd_map_mode = 0
fo_vsd_map_mode = 0
capt_exp_vor_app_mode = 0
fo_exp_vor_app_mode = 0

rudder_out = 0
rudder_target = 0
-- hdg_ratio_old = 0
-- hdg_ratio = 0
-- hdef_ratio = 0
-- hdef_ratio_old = 0

ap_capt_disco_status = 0
ap_fo_disco_status = 0

shake_on1 = 0
shake_on2 = 0
lowerDU_set = 0
takeoff_eng_out = 0
vnav_desc_idle = 0
vnav_desc_idle_time = 0.5

mcp_speed_dial_old = 0
flaps_ratio_old = 0
hdg_turn = 0

radio_height_sum = 0
radio_height_num = 0
radio_h_ratio_s = 0
radio_h_tick = 0

hdg_sel_to = 0
ratio_timer = 0

mcp_alt_dial_lock = 0
mcp_alt_dial_app = 0
mcp_alt_dial_old = 0

mcp_alt_vs_lock = 0
mcp_alt_vs_dial = 0
mcp_alt_vs_dial_old = 0

lnav_lock_hdg = 0
retard_enabled = 0

throttle1_use_old = 0
throttle2_use_old = 0
throttle_all_use_old = 0
at_mode_change = 0

yoke_off = 0
last_yoke_pitch = 0
last_yoke_roll = 0
roll_ratio2_old = 0
flare_ratio_yoke = 0

roll_err_accu = 0

mem_attitude = 0
mem_attitude_deg = 0

pitch_ratio_trend = 0
pitch_ratio_last_trend = 0

servo_yoke = 0
servo_yoke_old = 0

roll_ratio_pilot = 0
roll_ratio_pilot_old = 0
roll_ratio_copilot = 0
roll_ratio_copilot_old = 0

fdir_pitch_anim = 0
fdir_pitch_ovr_old = 0

pitch_ratio_diff = 0
roll_ratio_diff = 0
servo_i_pitch = 0
servo_ii_pitch = 0
fdir_pitch_old = 0
last_err_pitch = 0
servo_k_pitch_old = 0

to_ga_hdg = 0

vvi_trend2 = 0
flare_vvi_old2 = 0

load_plane2 = 0

cmd_threshold_time = 0.025
cmd_sim_period = 0
cmd_ap_spd = 0
cmd_ap_spd_timer = 0
cmd_ap_hdg = 0
cmd_ap_hdg_timer = 0
cmd_ap_alt = 0
cmd_ap_alt_timer = 0
cmd_ap_crs1 = 0
cmd_ap_crs1_timer = 0
cmd_ap_crs2 = 0
cmd_ap_crs2_timer = 0

mcp_speed_dial_mem = 0
to_ga_mcp_changed = 0

gls_pth_alt_old = 0
gls_vvi_cur = 0
lpv_vvi_cur = 0
lpv_pth_alt_old = 0
gls_active = 0
mmr_act_mode = 0
mmr_act_mode_old = 0
gls_type = 0

ac_tnsbus1_status_old = 0
ac_tnsbus2_status_old = 0
source_off_bus1_old = 0
source_off_bus2_old = 0

lvlchg_tgt_spd = 0
vnav_tgt_spd = 0
afs_protect_speed = 0
snd_ap_disconnect_old = 0
snd_ap_disco_delay = 0
ignore_td_idx2 = 0

ap_lnav_hdg = 0
both_fd_was_on = 0
vnav_n1_dis_eng = 0
ap_warn = 0
ap_warn_off = 0
ap_cws_status_old = 0
at_warn_low = 0
at_warn_high = 0
at_warn_reset = 0

vorloc_lnav_enable = 0
lnav_ma_armed = 0
-- autopilot_altitude_mode_old = 0
autoland_install = 0
fd_on_both = 0
recycled_fd_roll = 0
elevation_ft = 0
fms_ils_disable_old = 0
ian_install_old = 0
ils_gls_not_tuned_old = 1
fd_goaround_single = 0

capt_map_mode_old = 0
fo_map_mode_old = 0

nd_refreshed = false
capt_map_mode_old2 = 0
capt_exp_map_mode_old2 = 0
capt_vsd_map_mode_old2 = 0
capt_range_old2 = 0

nd_refreshed_fo = false
fo_map_mode_old2 = 0
fo_exp_map_mode_old2 = 0
fo_vsd_map_mode_old2 = 0
fo_range_old2 = 0

timer_gyro = 0
ac_stdbus_timer = 0
roll_elec_deg_stdby = 0
pitch_elec_deg_stdby = 0

loaded_flight_timer = 0

ap_heading_buf = 0
ap_heading_timer = 0
ap_heading_used = false

ap_altitude_buf = 0
ap_altitude_timer = 0
ap_altitude_used = false

ap_speed_buf = 0
ap_speed_timer = 0
ap_speed_used = false

fma_roll_timer = 0
fma_pitch_timer = 0
fma_speed_timer = 0

vnav_pfd_alt_mode = 0
vnav_pfd_spd_mode = 0
lvl_chg_pfd_spd_mode = 0

max_REV = 0.57
max_THR = 1.08
shift_thr = 0
shift_off = 0.2;
show_ias_old = 0

glare_load = 0

decel_dist_calc = false
decel_dist_crz = 0
fmc_cruise_speed_old = -1
fmc_cruise_speed_mach_old = -1
fmc_descent_speed_old = -1
fmc_descent_speed_mach_old = -1


--*************************************************************************************--
--** 				             FIND X-PLANE DATAREFS            			    	 **--
--*************************************************************************************--
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_map_mode_is_HSI			= find_dataref("sim/cockpit2/EFIS/map_mode_is_HSI")

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_efis_ndb					= find_dataref("sim/cockpit2/EFIS/EFIS_ndb_on")


----- AUTOPILOT DATAREFS

simDR_autothrottle_status		= find_dataref("sim/cockpit2/autopilot/autothrottle_on")
simDR_autopilot_altitude_mode	= find_dataref("sim/cockpit2/autopilot/altitude_mode")
simDR_autopilot_heading_mode	= find_dataref("sim/cockpit2/autopilot/heading_mode")

-- simDR_autopilot_approach		= find_dataref("sim/cockpit2/autopilot/approach_status")

simDR_servos_on					= find_dataref("sim/cockpit2/autopilot/servos_on")
simDR_bank_angle				= find_dataref("sim/cockpit2/autopilot/bank_angle_mode")
simDR_flight_dir_mode			= find_dataref("sim/cockpit2/autopilot/flight_director_mode")

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

simDR_roll_status				= find_dataref("sim/cockpit2/autopilot/roll_status")
simDR_alt_hold_status			= find_dataref("sim/cockpit2/autopilot/altitude_hold_status")
simDR_vvi_status				= find_dataref("sim/cockpit2/autopilot/vvi_status")

simDR_autopilot_source			= find_dataref("sim/cockpit2/radios/actuators/HSI_source_select_pilot")
simDR_autopilot_fo_source		= find_dataref("sim/cockpit2/radios/actuators/HSI_source_select_copilot")

simDR_autopilot_side			= find_dataref("sim/cockpit2/autopilot/autopilot_source")

simDR_nav_status				= find_dataref("sim/cockpit2/autopilot/nav_status")

simDR_autopilot_on				= find_dataref("sim/cockpit2/autopilot/autopilot_on")

simDR_ap_capt_heading			= find_dataref("sim/cockpit2/autopilot/heading_dial_deg_mag_pilot")
simDR_ap_fo_heading				= find_dataref("sim/cockpit2/autopilot/heading_dial_deg_mag_copilot")

simDR_airspeed_dial				= find_dataref("sim/cockpit2/autopilot/airspeed_dial_kts_mach")
simDR_airspeed_dial_kts			= find_dataref("sim/cockpit2/autopilot/airspeed_dial_kts")

-- 0 for NAV1, 1 for NAV2, 2 for FMS/GPS

simDR_EFIS_mode					= find_dataref("sim/cockpit2/EFIS/map_mode")
--simDR_EFIS_WX					= find_dataref("sim/cockpit2/EFIS/EFIS_weather_on")
simDR_EFIS_TCAS					= find_dataref("sim/cockpit2/EFIS/EFIS_tcas_on")

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")

---- CUSTOM
simDR_radio_height_pilot_ft		= find_dataref("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot")
simDR_radio_height_copilot_ft	= find_dataref("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_copilot")
simDR_throttle_all				= find_dataref("sim/cockpit2/engine/actuators/throttle_ratio_all")
simDR_aircraft_on_ground		= find_dataref("sim/flightmodel/failures/onground_all")

simDR_on_ground					= find_dataref("sim/flightmodel2/gear/on_ground")
simDR_vvi_fpm_pilot				= find_dataref("sim/cockpit2/gauges/indicators/vvi_fpm_pilot")
simDR_vvi_fpm_copilot			= find_dataref("sim/cockpit2/gauges/indicators/vvi_fpm_copilot")
simDR_elevator_trim				= find_dataref("sim/cockpit2/controls/elevator_trim")

simDR_brake						= find_dataref("sim/cockpit2/controls/parking_brake_ratio")
simDR_autobrake_level			= find_dataref("sim/cockpit2/switches/auto_brake_level")
simDR_airspeed_pilot			= find_dataref("sim/cockpit2/gauges/indicators/airspeed_kts_pilot")
simDR_airspeed_mach_pilot		= find_dataref("sim/cockpit2/gauges/indicators/mach_pilot")
simDR_airspeed_accel_pilot		= find_dataref("sim/cockpit2/gauges/indicators/airspeed_acceleration_kts_sec_pilot")
simDR_airspeed_accel_copilot	= find_dataref("sim/cockpit2/gauges/indicators/airspeed_acceleration_kts_sec_copilot")
simDR_altitude_pilot			= find_dataref("sim/cockpit2/gauges/indicators/altitude_ft_pilot")
simDR_airspeed_copilot			= find_dataref("sim/cockpit2/gauges/indicators/airspeed_kts_copilot")
simDR_airspeed_mach_copilot		= find_dataref("sim/cockpit2/gauges/indicators/mach_copilot")
simDR_altitude_copilot			= find_dataref("sim/cockpit2/gauges/indicators/altitude_ft_copilot")
-- simDR_cur_alt_hold				= find_dataref("sim/cockpit/autopilot/current_altitude")

simDR_airspeed_is_mach			= find_dataref("sim/cockpit2/autopilot/airspeed_is_mach")
-- simDR_mach_no					= find_dataref("sim/flightmodel/misc/machno")
simDR_mach_no					= find_dataref("laminar/B738/indicated/mach_no")

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

simDR_eng1_fail					= find_dataref("sim/operation/failures/rel_engfai0")
simDR_eng2_fail					= find_dataref("sim/operation/failures/rel_engfai1")
simDR_eng1_separate 			= find_dataref("sim/operation/failures/rel_engsep0")
simDR_eng2_separate 			= find_dataref("sim/operation/failures/rel_engsep1")

B738DR_kill_engine1				= create_dataref("laminar/B738/kill_engine1", "number")
B738DR_kill_engine2				= create_dataref("laminar/B738/kill_engine2", "number")

B738DR_eng1_N1					= find_dataref("laminar/B738/engine/indicators/N1_percent_1")
B738DR_eng2_N1					= find_dataref("laminar/B738/engine/indicators/N1_percent_2")
B738DR_eng1_N2					= find_dataref("laminar/B738/engine/indicators/N2_percent_1")
B738DR_eng2_N2					= find_dataref("laminar/B738/engine/indicators/N2_percent_2")

B738DR_eng1_req_N1				= find_dataref("laminar/B738/engine/eng1_req_n1")
B738DR_eng2_req_N1				= find_dataref("laminar/B738/engine/eng2_req_n1")

B738DR_engine01_fire_ext_switch_pos_arm = find_dataref("laminar/B738/fire/engine01/ext_switch/pos_arm")
B738DR_engine02_fire_ext_switch_pos_arm = find_dataref("laminar/B738/fire/engine02/ext_switch/pos_arm")

B738DR_engine1_starter_pos		= find_dataref("laminar/B738/engine/starter1_pos")
B738DR_engine2_starter_pos		= find_dataref("laminar/B738/engine/starter2_pos")

simDR_throttle					= find_dataref("sim/cockpit2/engine/actuators/throttle_ratio")
simDR_throttle_override			= find_dataref("sim/operation/override/override_throttles")

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

simDR_autothrottle_enable		= find_dataref("sim/cockpit2/autopilot/autothrottle_enabled")

simDR_heading_pilot				= find_dataref("sim/cockpit2/gauges/indicators/heading_AHARS_deg_mag_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_fdir_pitch_ovr			= find_dataref("sim/operation/override/override_flightdir_ptch")
simDR_fdir_roll_ovr				= find_dataref("sim/operation/override/override_flightdir_roll")
simDR_fdir_pitch				= find_dataref("sim/cockpit/autopilot/flight_director_pitch")
simDR_fdir_roll					= find_dataref("sim/cockpit/autopilot/flight_director_roll")
simDR_ahars_pitch_deg_pilot		= find_dataref("sim/cockpit2/gauges/indicators/pitch_AHARS_deg_pilot")
simDR_ahars_pitch_deg_copilot	= find_dataref("sim/cockpit2/gauges/indicators/pitch_AHARS_deg_copilot")

simDR_total_weight				= find_dataref("sim/flightmodel/weight/m_total")


simDR_vnav_status				= find_dataref("sim/cockpit2/autopilot/fms_vnav")
simDR_vnav_tod_nm				= find_dataref("sim/cockpit2/radios/indicators/fms_distance_to_tod_pilot")
simDR_fms_fpta					= find_dataref("sim/cockpit2/radios/indicators/fms_fpta_pilot")
simDR_fms_vtk					= find_dataref("sim/cockpit2/radios/indicators/fms_vtk_pilot")
simDR_tod_before				= find_dataref("sim/cockpit2/radios/indicators/fms_tod_before_distance_pilot")
simDR_fms_override 				= find_dataref("sim/operation/override/override_fms_advance")

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

simDR_efis_map_range			= find_dataref("sim/cockpit2/EFIS/map_range")
simDR_efis_map_range_fo			= find_dataref("sim/cockpit2/EFIS/map_range_copilot")
simDR_ground_spd				= find_dataref("sim/flightmodel/position/groundspeed")
simDR_map_range_steps			= find_dataref("sim/cockpit2/EFIS/map_range_steps")

simDR_engine_mixture			= find_dataref("sim/cockpit2/engine/actuators/mixture_ratio")
simDR_reverse_deploy			= find_dataref("sim/flightmodel2/engines/thrust_reverser_deploy_ratio")

simDR_glideslope_armed			= find_dataref("sim/cockpit2/autopilot/glideslope_armed")
simDR_gps_nav_id				= find_dataref("sim/cockpit2/radios/indicators/gps_nav_id")

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

simDR_mag_hdg					= find_dataref("sim/cockpit2/gauges/indicators/ground_track_mag_pilot")
simDR_mag_hdg_fo				= find_dataref("sim/cockpit2/gauges/indicators/ground_track_mag_copilot")

simDR_dh_pilot					= find_dataref("sim/cockpit2/gauges/actuators/radio_altimeter_bug_ft_pilot")
simDR_dh_copilot				= find_dataref("sim/cockpit2/gauges/actuators/radio_altimeter_bug_ft_copilot")

simDR_ahars_mag_hdg		= find_dataref("sim/cockpit2/gauges/indicators/heading_AHARS_deg_mag_pilot")
simDR_ahars_mag_hdg_fo	= find_dataref("sim/cockpit2/gauges/indicators/heading_AHARS_deg_mag_copilot")
simDR_mag_variation		= find_dataref("sim/flightmodel/position/magnetic_variation")

simDR_int_ver			= find_dataref("sim/version/xplane_internal_version")
simDR_efis_map_mode		= find_dataref("sim/cockpit/switches/EFIS_map_mode")

simDR_efis_sub_mode				= find_dataref("sim/cockpit2/EFIS/map_mode")
simDR_efis_sub_mode_copilot		= find_dataref("sim/cockpit2/EFIS/map_mode_copilot")


simDR_efis_vor_on		= find_dataref("sim/cockpit2/EFIS/EFIS_vor_on")
simDR_efis_apt_on		= find_dataref("sim/cockpit2/EFIS/EFIS_airport_on")
simDR_efis_fix_on		= find_dataref("sim/cockpit2/EFIS/EFIS_fix_on")
simDR_efis_wxr_on		= find_dataref("sim/cockpit2/EFIS/EFIS_weather_on")

simDR_efis_vor_on_copilot		= find_dataref("sim/cockpit2/EFIS/EFIS_vor_on_copilot")
simDR_efis_apt_on_copilot		= find_dataref("sim/cockpit2/EFIS/EFIS_airport_on_copilot")
simDR_efis_fix_on_copilot		= find_dataref("sim/cockpit2/EFIS/EFIS_fix_on_copilot")
simDR_efis_wxr_on_copilot		= find_dataref("sim/cockpit2/EFIS/EFIS_weather_on_copilot")

simDR_servo_pitch_ratio		= find_dataref("sim/joystick/servo_pitch_ratio")
simDR_servo_roll_ratio		= find_dataref("sim/joystick/servo_roll_ratio")
simDR_servo_heading_ratio	= find_dataref("sim/joystick/servo_heading_ratio")

simDR_heading_ratio			= find_dataref("sim/joystick/yoke_heading_ratio")
simDR_pitch_ratio			= find_dataref("sim/joystick/yoke_pitch_ratio")
simDR_roll_ratio			= find_dataref("sim/joystick/yoke_roll_ratio")

simDR_heading_ratio2		= find_dataref("sim/cockpit2/controls/yoke_heading_ratio")
simDR_pitch_ratio2			= find_dataref("sim/cockpit2/controls/yoke_pitch_ratio")
simDR_roll_ratio2			= find_dataref("sim/cockpit2/controls/yoke_roll_ratio")

simDR_mem					= find_dataref("sim/cockpit2/switches/landing_lights_switch")
simDR_vr_enable				= find_dataref("sim/graphics/VR/enabled")

simDR_roll_elec_deg_pilot	= find_dataref("sim/cockpit2/gauges/indicators/roll_electric_deg_pilot")

simDR_faxil_prop			= find_dataref("sim/flightmodel/forces/faxil_prop")
simDR_flap1_deg				= find_dataref("sim/flightmodel2/wing/flap1_deg")
simDR_flap2_deg				= find_dataref("sim/flightmodel2/wing/flap2_deg")

B738DR_flap1_deg			= create_dataref("laminar/B738/wing/flap1_deg", "array[2]")
B738DR_flap2_deg			= create_dataref("laminar/B738/wing/flap2_deg", "array[4]")


--- VNAV
B738DR_vnav_td_dist 		= find_dataref("laminar/B738/fms/vnav_td_dist")
B738DR_vnav_pth_alt			= find_dataref("laminar/B738/fms/vnav_pth_alt")
B738DR_vnav_alt_err			= find_dataref("laminar/B738/fms/vnav_alt_err")
B738DR_vnav_alt_err2		= find_dataref("laminar/B738/fms/vnav_alt_err2")
B738DR_vnav_vvi				= find_dataref("laminar/B738/fms/vnav_vvi")
B738DR_vnav_vvi_corr		= find_dataref("laminar/B738/fms/vnav_vvi_corr")
B738DR_vnav_err_pfd			= find_dataref("laminar/B738/fms/vnav_err_pfd")
B738DR_vnav_pth_fpm			= find_dataref("laminar/B738/fms/vnav_pth_fpm")

B738DR_gp_vvi			= find_dataref("laminar/B738/fms/gp_vvi")
B738DR_gp_vvi_corr		= find_dataref("laminar/B738/fms/gp_vvi_corr")
B738DR_gp_err_pfd		= find_dataref("laminar/B738/fms/gp_err_pfd")
B738DR_gp_alt_err 		= find_dataref("laminar/B738/fms/gp_alt_err")
B738DR_gp_alt_err2		= find_dataref("laminar/B738/fms/gp_alt_err2")
B738DR_gp_pth_alt 		= find_dataref("laminar/B738/fms/gp_pth_alt")
B738DR_fac_xtrack		= find_dataref("laminar/B738/fms/fac_xtrack")
B738DR_fac_trk			= find_dataref("laminar/B738/fms/fac_trk")
B738DR_fac_crs 			= find_dataref("laminar/B738/fms/fac_crs")
B738DR_pfd_gp_path		= find_dataref("laminar/B738/fms/pfd_gp_path")

B738DR_ed_found_type	= find_dataref("laminar/B738/fms/vnav_ed_found_type")

B738DR_ignore_td_idx2		= find_dataref("laminar/B738/fms/vnav_ignore_td_idx2")

B738DR_vnav_alt_err99		= find_dataref("laminar/B738/fms/vnav_alt_err99")
B738DR_vnav_vvi99			= find_dataref("laminar/B738/fms/vnav_vvi99")

B738DR_rnav_enable			= find_dataref("laminar/B738/fms/rnav_enable")
B738DR_gp_active			= find_dataref("laminar/B738/fms/vnav_gp_active")
B738DR_vnav_app_active		= find_dataref("laminar/B738/fms/vnav_app_active")

B738DR_end_route			= find_dataref("laminar/B738/fms/end_route")
B738DR_no_perf				= find_dataref("laminar/B738/fms/no_perf")

B738DR_vnav_desc_spd_disable = find_dataref("laminar/B738/fms/vnav_desc_spd_disable")
B738DR_descent_mode			= find_dataref("laminar/B738/FMS/descent_mode")

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")

B738DR_fmc_crs				= find_dataref("laminar/B738/fms/gps_course_degtm")
B738DR_fmc_trk				= find_dataref("laminar/B738/fms/gps_track_degtm")
B738DR_fmc_trk2				= find_dataref("laminar/B738/fms/gps_track2_degtm")
B738DR_fmc_trk3				= find_dataref("laminar/B738/fms/gps_track3_degtm")
-- simDR_TAS					= find_dataref("sim/flightmodel/position/true_airspeed")
simDR_TAS					= find_dataref("laminar/B738/indicated/TAS")
simDR_IAS					= find_dataref("sim/flightmodel/position/indicated_airspeed")
B738DR_fmc_trk_turn			= find_dataref("laminar/B738/fms/gps_track_turn")
B738DR_fmc_trk_turn2		= find_dataref("laminar/B738/fms/gps_track_turn2")

B738DR_fpln_nav_id			= find_dataref("laminar/B738/fms/fpln_nav_id")
B738DR_hold_phase			= find_dataref("laminar/B738/fms/hold_phase")
B738DR_pi_phase				= find_dataref("laminar/B738/fms/pi_phase")
B738DR_legs_intdir_act		= find_dataref("laminar/B738/fms/intdir_act")
B738DR_legs_intdir_crs		= find_dataref("laminar/B738/fms/intdir_crs")
legs_intdir					= find_dataref("laminar/B738/fms/intdir")
legs_intdir_old = 0


B738DR_nav_mode					= find_dataref("laminar/B738/fms/nav_mode")

B738DR_track_up				= find_dataref("laminar/B738/fms/track_up")

B738DR_afs_spd_limit_max 	= find_dataref("laminar/B738/FMS/afs_spd_limit_max")
B738DR_pfd_min_speed		= find_dataref("laminar/B738/pfd/min_speed")

B738DR_dspl_ctrl_pnl 		= find_dataref("laminar/B738/toggle_switch/dspl_ctrl_pnl")

simDR_override_heading		= find_dataref("sim/operation/override/override_joystick_heading")
simDR_rudder_trim			= find_dataref("sim/cockpit2/controls/rudder_trim")
-- simDR_control_hdg_ratio		= find_dataref("sim/flightmodel2/controls/heading_ratio")
-- simDR_control_pitch_ratio	= find_dataref("sim/flightmodel2/controls/pitch_ratio")
simDR_nav1_hdef_pilot		= find_dataref("sim/cockpit2/radios/indicators/nav1_hdef_dots_pilot")
simDR_nav1_vdef_pilot		= find_dataref("sim/cockpit2/radios/indicators/nav1_vdef_dots_pilot")
simDR_nav2_hdef_pilot		= find_dataref("sim/cockpit2/radios/indicators/nav2_hdef_dots_pilot")
simDR_nav2_vdef_pilot		= find_dataref("sim/cockpit2/radios/indicators/nav2_vdef_dots_pilot")
simDR_nav1_hdef_copilot		= find_dataref("sim/cockpit2/radios/indicators/nav1_hdef_dots_copilot")
simDR_nav1_vdef_copilot		= find_dataref("sim/cockpit2/radios/indicators/nav1_vdef_dots_copilot")
simDR_nav2_hdef_copilot		= find_dataref("sim/cockpit2/radios/indicators/nav2_hdef_dots_copilot")
simDR_nav2_vdef_copilot		= find_dataref("sim/cockpit2/radios/indicators/nav2_vdef_dots_copilot")

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

simDR_aoa				= find_dataref("sim/flightmodel2/misc/AoA_angle_degrees")

simDR_roll_AHARS_deg 	= find_dataref("sim/cockpit2/gauges/indicators/roll_AHARS_deg_pilot")
simDR_roll_AHARS_deg_copilot 	= find_dataref("sim/cockpit2/gauges/indicators/roll_AHARS_deg_copilot")
-- simDR_ap_alt_hold_ft 			= find_dataref("sim/cockpit2/autopilot/altitude_hold_ft")
simDR_pitch_elec		= find_dataref("sim/cockpit2/gauges/indicators/pitch_electric_deg_pilot")

B738DR_takeoff_hdg			= find_dataref("laminar/B738/takeoff_hdg")

brunner_active			= find_dataref("zibomod/brunner_active")
BruDR_ext_ctrl			= find_dataref("beh/autopilot_external_control")
B738DR_smart_ap_knob	= find_dataref("laminar/B738/effects/smart_ap_knob")

simDR_bus_transfer_on 		= find_dataref("sim/cockpit2/electrical/cross_tie")
simDR_sigma					= find_dataref("sim/weather/sigma")
simDR_pause					= find_dataref("sim/time/paused")
simDR_engine_on 			= find_dataref("sim/flightmodel2/engines/engine_is_burning_fuel")


B738DR_roll_elec_deg_stdby	= create_dataref("laminar/B738/gyro/roll_elec_deg_stdby", "number")
B738DR_pitch_elec_deg_stdby	= create_dataref("laminar/B738/gyro/pitch_elec_deg_stdby", "number")

B738DR_allign_analog_horiz_pos	= create_dataref("laminar/B738/button/allign_analog_horiz_pos", "number")

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

simCMD_efis_wxr = find_command("sim/instruments/EFIS_wxr")
simCMD_efis_wxr_copilot = find_command("sim/instruments/EFIS_copilot_wxr")

---- AUTOPILOT COMMANDS

simCMD_autothrottle			= find_command("sim/autopilot/autothrottle_toggle")
simCMD_autothrottle_on		= find_command("sim/autopilot/autothrottle_on")
simCMD_autothrottle_off		= find_command("sim/autopilot/autothrottle_off")

simCMD_autopilot_vnav		= find_command("sim/autopilot/FMS")
simCMD_autopilot_lvl_chg	= find_command("sim/autopilot/level_change")
simCMD_autopilot_hdg		= find_command("sim/autopilot/heading")
simCMD_autopilot_lnav		= find_command("sim/autopilot/NAV")
simCMD_autopilot_app		= find_command("sim/autopilot/approach")
simCMD_autopilot_alt_hold	= find_command("sim/autopilot/altitude_hold")
simCMD_autopilot_vs			= find_command("sim/autopilot/vertical_speed")
simCMD_autopilot_vs_sel		= find_command("sim/autopilot/vertical_speed_pre_sel")
simCMD_autopilot_alt_vs		= find_command("sim/autopilot/alt_vs")

simCMD_autopilot_fd			= find_command("sim/autopilot/fdir_toggle")
simCMD_autopilot_servos		= find_command("sim/autopilot/servos_toggle")
simCMD_autopilot_cws		= find_command("sim/autopilot/control_wheel_steer")

simCMD_autopilot_co			= find_command("sim/autopilot/knots_mach_toggle")
simCMD_flight_director		= find_command("sim/autopilot/fdir_on")
simCMD_autothrottle_discon	= find_command("sim/autopilot/autothrottle_off")

simCMD_source_nav1			= find_command("sim/autopilot/hsi_select_nav_1")
simCMD_source_nav2			= find_command("sim/autopilot/hsi_select_nav_2")
simCDM_source_FMS			= find_command("sim/autopilot/hsi_select_gps")

simCMD_source_fo_nav1		= find_command("sim/autopilot/hsi_select_copilot_nav_1")
simCMD_source_fo_nav2		= find_command("sim/autopilot/hsi_select_copilot_nav_2")
simCDM_source_fo_FMS		= find_command("sim/autopilot/hsi_select_copilot_gps")

simCMD_servos_on			= find_command("sim/autopilot/servos_on")

simCMD_fdir_up				= find_command("sim/autopilot/fdir_servos_up_one")
simCMD_fdir_dn				= find_command("sim/autopilot/fdir_servos_down_one")

B738CMD_msg_not_intcpt		= find_command("laminar/B738/fms/msg_not_intcpt")


simDR_startup_running 	= find_dataref("sim/operation/prefs/startup_running")

simDR_air_on_acf				= find_dataref("sim/flightmodel/forces/vx_air_on_acf")

simDR_position_mag_psi 			= find_dataref("sim/flightmodel/position/mag_psi")
simDR_sos 						= find_dataref("laminar/B738/indicated/sos")

simDR_mem2				 		= find_dataref("sim/cockpit2/switches/generic_lights_switch")

B738DR_irs_source 				= find_dataref("laminar/B738/toggle_switch/irs_source")

B738DR_brightness2_export 		= find_dataref("laminar/B738/brightness_level2")

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

B738DR_mmr_cpt_act_mode		= find_dataref("laminar/B738/mmr/cpt/act_mode")
B738DR_mmr_fo_act_mode		= find_dataref("laminar/B738/mmr/fo/act_mode")
B738DR_mmr_act_mode 		= find_dataref("laminar/B738/nav/mmr_act_mode")
B738DR_mmr_act_mode_fo 		= find_dataref("laminar/B738/nav/mmr_act_mode_fo")

B738DR_gls1_active 			= find_dataref("laminar/B738/nav/gls1_active")
B738DR_gls1_id 				= find_dataref("laminar/B738/nav/gls1_id")
B738DR_gls1_idx				= find_dataref("laminar/B738/nav/gls1_idx")
B738DR_gls1_dme 			= find_dataref("laminar/B738/nav/gls1_dme")
B738DR_gls1_alt 			= find_dataref("laminar/B738/nav/gls1_alt")
B738DR_gls1_rnw 			= find_dataref("laminar/B738/nav/gls1_rnw")
B738DR_gls1_crs 			= find_dataref("laminar/B738/nav/gls1_crs")
B738DR_gls1_horz_dev 		= find_dataref("laminar/B738/nav/gls1_horz_dev")
B738DR_gls1_vert_dev 		= find_dataref("laminar/B738/nav/gls1_vert_dev")
B738DR_gls1_xtrack			= find_dataref("laminar/B738/nav/gls1_xtrack")
B738DR_gls1_type			= find_dataref("laminar/B738/nav/gls1_type")
B738DR_mmr_cpt_act_value	= find_dataref("laminar/B738/mmr/cpt/act_value")

-- GLS 2
B738DR_gls2_active 			= find_dataref("laminar/B738/nav/gls2_active")
B738DR_gls2_id 				= find_dataref("laminar/B738/nav/gls2_id")
B738DR_gls2_idx				= find_dataref("laminar/B738/nav/gls2_idx")
B738DR_gls2_dme 			= find_dataref("laminar/B738/nav/gls2_dme")
B738DR_gls2_alt 			= find_dataref("laminar/B738/nav/gls2_alt")
B738DR_gls2_rnw 			= find_dataref("laminar/B738/nav/gls2_rnw")
B738DR_gls2_crs 			= find_dataref("laminar/B738/nav/gls2_crs")
B738DR_gls2_horz_dev 		= find_dataref("laminar/B738/nav/gls2_horz_dev")
B738DR_gls2_vert_dev 		= find_dataref("laminar/B738/nav/gls2_vert_dev")
B738DR_gls2_xtrack			= find_dataref("laminar/B738/nav/gls2_xtrack")
B738DR_gls2_type			= find_dataref("laminar/B738/nav/gls2_type")
B738DR_mmr_fo_act_value		= find_dataref("laminar/B738/mmr/fo/act_value")

B738DR_gls_app_status		= create_dataref("laminar/B738/nav/gls_app_status", "number")
B738DR_gls_nav_status		= create_dataref("laminar/B738/nav/gls_nav_status", "number")
B738DR_gls_gs_status		= find_dataref("laminar/B738/nav/gls_gs_status")
B738DR_gls_crs				= create_dataref("laminar/B738/nav/gls_crs", "number")
B738DR_gls_xtrack			= create_dataref("laminar/B738/nav/gls_xtrack", "number")
B738DR_lpv_app_status		= find_dataref("laminar/B738/nav/lpv_app_status")
B738DR_lpv_gs_status		= find_dataref("laminar/B738/nav/lpv_gs_status")
function B738DR_gls_corr_handl() end
B738DR_gls_corr			= create_dataref("laminar/B738/nav/gls_corr", "number", B738DR_gls_corr_handl)

B738DR_autobrake_RTO_arm	= find_dataref("laminar/B738/autobrake/autobrake_RTO_arm")

B738DR_fms_v2_15			= find_dataref("laminar/B738/FMS/v2_15")
B738DR_fms_v2_set			= find_dataref("laminar/B738/FMS/v2_set")
B738DR_white_bug			= find_dataref("laminar/B738/white_bug")

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

B738DR_fms_N1_mode			= find_dataref("laminar/B738/FMS/N1_mode")
B738DR_fmc_oat_temp			= find_dataref("laminar/B738/FMS/fmc_oat_temp")
B738DR_fmc_gw				= find_dataref("laminar/B738/FMS/fmc_gw")

B738DR_flight_phase			= find_dataref("laminar/B738/FMS/flight_phase")
B738DR_aerodrom_altitude	= find_dataref("laminar/B738/FMS/aerodrom_altitude")
B738DR_lift_timer			= find_dataref("laminar/B738/FMS/lift_timer")

B738DR_trans_alt			= find_dataref("laminar/B738/FMS/fmc_trans_alt")
B738DR_trans_lvl			= find_dataref("laminar/B738/FMS/fmc_trans_lvl")

B738DR_fms_descent_now		= find_dataref("laminar/B738/FMS/descent_now")
B738DR_thr_red_alt			= find_dataref("laminar/B738/FMS/throttle_red_alt")
B738DR_accel_alt			= find_dataref("laminar/B738/FMS/accel_height")
B738DR_eo_accel_alt			= find_dataref("laminar/B738/FMS/eo_accel_height")
B738DR_thr_cut_alt			= find_dataref("laminar/B738/FMS/cutback_height")
B738DR_thr_cutback			= find_dataref("laminar/B738/FMS/cutback")

B738DR_fms_ils_disable		= find_dataref("laminar/B738/FMS/ils_disable")
B738DR_ian_not_install			= find_dataref("laminar/B738/ian_install")

B738DR_rest_wpt_spd_id 		= find_dataref("laminar/B738/fms/rest_wpt_spd_id")
B738DR_rest_wpt_spd 		= find_dataref("laminar/B738/fms/rest_wpt_spd")
B738DR_rest_wpt_spd2		= find_dataref("laminar/B738/fms/rest_wpt_spd2")
B738DR_rest_wpt_spd_idx		= find_dataref("laminar/B738/fms/rest_wpt_spd_idx")
B738DR_rest_wpt_spd_tgt		= find_dataref("laminar/B738/fms/rest_wpt_spd_tgt")
B738DR_rest_wpt_spd_type	= find_dataref("laminar/B738/fms/rest_wpt_spd_type")
B738DR_rest_wpt_alt_id 		= find_dataref("laminar/B738/fms/rest_wpt_alt_id")
B738DR_rest_wpt_alt 		= find_dataref("laminar/B738/fms/rest_wpt_alt")
B738DR_rest_wpt_alt_t 		= find_dataref("laminar/B738/fms/rest_wpt_alt_t")
B738DR_calc_wpt_spd 		= find_dataref("laminar/B738/fms/calc_wpt_spd")
B738DR_calc_wpt_alt 		= find_dataref("laminar/B738/fms/calc_wpt_alt")

B738DR_offset						= find_dataref("laminar/B738/fms/vnav_idx")
B738DR_missed_app_alt		= find_dataref("laminar/B738/fms/missed_app_alt")
B738DR_dest_runway_alt		= find_dataref("laminar/B738/fms/dest_runway_alt")


B738DR_td_idx 						= find_dataref("laminar/B738/fms/vnav_td_idx")
B738DR_ed_alt						= find_dataref("laminar/B738/fms/ed_alt")
B738DR_missed_app_act		= find_dataref("laminar/B738/fms/missed_app_act")

B738DR_irs_left_mode		= find_dataref("laminar/B738/irs/irs_mode")
B738DR_irs_right_mode		= find_dataref("laminar/B738/irs/irs2_mode")

B738DR_lock_idle_thrust		= find_dataref("laminar/B738/fms/lock_idle_thrust")

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_2			= find_dataref("laminar/B738/pfd/flaps_2")
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_flaps_25			= find_dataref("laminar/B738/pfd/flaps_25")

B738DR_vnav_disconnect		= find_dataref("laminar/B738/fms/vnav_disconnect")
B738DR_lnav_disconnect		= find_dataref("laminar/B738/fms/lnav_disconnect")
B738DR_decel_dist			= find_dataref("laminar/B738/fms/vnav_decel_dist")
B738DR_FL100_desc_dist		= find_dataref("laminar/B738/fms/vnav_fl100_desc_dist")
B738DR_dist_ed				= find_dataref("laminar/B738/fms/dist_to_ed")

B738DR_FL100_dist_dest 		= find_dataref("laminar/B738/fms/vnav_fl100_dist_dest")
B738DR_FL100_dist_dest2 	= find_dataref("laminar/B738/fms/vnav_fl100_dist_dest2")
B738DR_fmc_dist_to_dest		= find_dataref("laminar/B738/fms/dist_to_dest")

B738DR_autopilot_pfd_mode		= find_dataref("laminar/B738/autopilot/pfd_mode")
B738DR_autopilot_pfd_mode_fo	= find_dataref("laminar/B738/autopilot/pfd_mode_fo")

B738DR_nd_vert_path			= find_dataref("laminar/B738/pfd/nd_vert_path")

B738DR_rest_wpt_alt_idx		= find_dataref("laminar/B738/fms/rest_wpt_alt_idx")

B738DR_hyd_A_status			= find_dataref("laminar/B738/hydraulic/A_status")
B738DR_hyd_B_status			= find_dataref("laminar/B738/hydraulic/B_status")
B738DR_hyd_stdby_status		= find_dataref("laminar/B738/hydraulic/standby_status")

B738DR_yoke_to_center		= find_dataref("laminar/B738/yoke_to_center")
B738DR_brunner_enable		= find_dataref("laminar/B738/brunner_enable")
B738DR_xhsi_enable			= find_dataref("laminar/B738/xhsi_enable")
B738DR_cfy_enable			= find_dataref("laminar/B738/cfy_enable")

function B738DR_cfy_trim_raw_handl() end
B738DR_cfy_trim_raw				= create_dataref("laminar/B738/flt_ctrls/trim_raw", "number", B738DR_cfy_trim_raw_handl)
function B738DR_cfy_throttle_override_handl() end
B738DR_cfy_throttle_override	= create_dataref("laminar/B738/flt_ctrls/throttle_overrided", "number", B738DR_cfy_throttle_override_handl)

function B738DR_cfy_sb_servo_status_handl() end
B738DR_cfy_sb_servo_status		= create_dataref("laminar/B738/flt_ctrls/speedbrake_servo_status", "number", B738DR_cfy_sb_servo_status_handl)
function B738DR_cfy_sb_servo_target_handl() end
B738DR_cfy_sb_servo_target		= create_dataref("laminar/B738/flt_ctrls/speedbrake_target", "number", B738DR_cfy_sb_servo_target_handl)
function B738DR_cfy_trim_ind_handl() end
B738DR_cfy_trim_ind				= create_dataref("laminar/B738/flt_ctrls/trim_indicator", "number", B738DR_cfy_trim_ind_handl)
function B738DR_cfy_trim_servo_status_handl() end
B738DR_cfy_trim_servo_status	= create_dataref("laminar/B738/flt_ctrls/trim_servo_status", "number", B738DR_cfy_trim_servo_status_handl)
function B738DR_cfy_trim_servo_speed_handl() end
B738DR_cfy_trim_servo_speed		= create_dataref("laminar/B738/flt_ctrls/trim_servo_speed", "number", B738DR_cfy_trim_servo_speed_handl)
function B738DR_cfy_trim_servo_target_handl() end
B738DR_cfy_trim_servo_target	= create_dataref("laminar/B738/flt_ctrls/trim_servo_target", "number", B738DR_cfy_trim_servo_target_handl)
function B738DR_cfy_pb_lock_handl() end
B738DR_cfy_pb_lock				= create_dataref("laminar/B738/flt_ctrls/park_brake_lock", "number", B738DR_cfy_pb_lock_handl)


B738DR_joy_axis_throttle1	= find_dataref("laminar/B738/axis/throttle1")
B738DR_joy_axis_throttle2	= find_dataref("laminar/B738/axis/throttle2")

B738DR_xtrack				= find_dataref("laminar/B738/fms/xtrack")
B738DR_xtrack2				= find_dataref("laminar/B738/fms/xtrack2")

B738DR_wpt_path				= find_dataref("laminar/B738/fms/gps_wpt_path")
B738DR_pfd_max_speed		= find_dataref("laminar/B738/pfd/max_speed")

B738DR_baro_in_hpa			= find_dataref("laminar/B738/fms/baro_in_hpa")
B738DR_min_baro_radio		= find_dataref("laminar/B738/fms/min_baro_radio")

B738DR_fms_approach_speed	= find_dataref("laminar/B738/FMS/approach_speed")
B738DR_approach_flaps_set	= find_dataref("laminar/B738/FMS/approach_flaps_set")
B738DR_fmc_dist				= find_dataref("laminar/B738/fms/lnav_dist_next")
B738DR_rnp					= find_dataref("laminar/B738/fms/rnp")
B738DR_fmc_bank_angle		= find_dataref("laminar/B738/fms/bank_angle")
B738DR_fpln_active			= find_dataref("laminar/B738/fms/fpln_acive")

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

B738DR_fms_approach_wind_corr	= find_dataref("laminar/B738/FMS/approach_wind_corr")

B738DR_enable_gpwstest_long		= find_dataref("laminar/b738/fmodpack/fmod_gpwstest_long_on")
B738DR_enable_gpwstest_short	= find_dataref("laminar/b738/fmodpack/fmod_gpwstest_short_on")
B738DR_windshear				= find_dataref("laminar/b738/fmodpack/msg_windshear")

B738DR_nd_rings					= find_dataref("laminar/B738/effect/nd_rings")

B738DR_ed_found				= find_dataref("laminar/B738/fms/ed_idx")
B738DR_legs_num			= find_dataref("laminar/B738/vnav/legs_num")
B738DR_act_wpt_gp		= find_dataref("laminar/B738/fms/act_wpt_gp")

B738DR_stall_test_active1		= find_dataref("laminar/B738/push_button/stall_test_active1")
B738DR_stall_test_active2		= find_dataref("laminar/B738/push_button/stall_test_active2")

B738DR_ac_tns_bus1_pow4			= find_dataref("laminar/B738/electric/ac_tns_bus1_pow4")
B738DR_ac_tns_bus2_pow4			= find_dataref("laminar/B738/electric/ac_tns_bus2_pow4")

B738DR_dc_bus1_status			= find_dataref("laminar/B738/electric/dc_bus1_status")
B738DR_dc_bus2_status			= find_dataref("laminar/B738/electric/dc_bus2_status")
B738DR_ac_stdbus_status			= find_dataref("laminar/B738/electric/ac_stdbus_status")
B738DR_ac_tnsbus1_status		= find_dataref("laminar/B738/electric/ac_tnsbus1_status")
B738DR_ac_tnsbus2_status		= find_dataref("laminar/B738/electric/ac_tnsbus2_status")
B738DR_dc_stdbus_status			= find_dataref("laminar/B738/electric/dc_stdbus_status")
B738DR_source_off_bus1x			= find_dataref("laminar/B738/annunciator/source_off1x")
B738DR_source_off_bus2x			= find_dataref("laminar/B738/annunciator/source_off2x")
simDR_gen_off_bus				= find_dataref("sim/cockpit2/annunciators/generator_off")
-- simDR_bus_battery_on			= find_dataref("sim/cockpit2/electrical/battery_on")
B738DR_batbus_status			= find_dataref("laminar/B738/electric/batbus_status")

B738DR_lost_ac_ap_a				= find_dataref("laminar/B738/electric/lost_ac_ap_a")
B738DR_lost_ac_ap_b				= find_dataref("laminar/B738/electric/lost_ac_ap_b")

B738DR_hardware_thr_lever		= find_dataref("laminar/B738/effects/hardware_thr_lever")
B738DR_ap_yoke_tolerance		= find_dataref("laminar/B738/FMS/ap_yoke_tolerance")

B738DR_sync_pilot				= find_dataref("laminar/B738/effects/sync_pilot")

B738DR_td_idx2					= find_dataref("laminar/B738/fms/vnav_td_idx2")
B738DR_td_dist2					= find_dataref("laminar/B738/fms/vnav_td_dist2")
B738DR_td_alt2					= find_dataref("laminar/B738/fms/vnav_td_alt2")
B738DR_td_alt2_hold				= find_dataref("laminar/B738/fms/vnav_td_alt2_hold")
B738DR_ed_alt2_hold				= find_dataref("laminar/B738/fms/vnav_ed_alt2_hold")

B738DR_lnav_prior_arm			= find_dataref("laminar/B738/fms/lnav_prior_arm")
B738DR_yoke_disco_ap			= find_dataref("laminar/B738/yoke_disco_ap")
B738DR_joy_axis_pitch 			= find_dataref("laminar/B738/axis/pitch")
B738DR_joy_axis_roll 			= find_dataref("laminar/B738/axis/roll")

B738DR_thr_goaround_N1			= find_dataref("laminar/B738/engine/calc/thr_goaround_N1")
B738DR_gear_handle_pos			= find_dataref("laminar/B738/controls/gear_handle_down")
B738DR_ap_trim_pos				= find_dataref("laminar/B738/toggle_switch/ap_trim_pos")

B738DR_state_cmd 				= find_dataref("laminar/B738/state_cmd")
B738DR_state_cmd2 				= find_dataref("laminar/B738/state_cmd2")

B738DR_pfd_min_maneuver_speed	= find_dataref("laminar/B738/pfd/min_maneuver_speed")

B738DR_mixture_ratio1			= find_dataref("laminar/B738/engine/mixture_ratio1")
B738DR_mixture_ratio2			= find_dataref("laminar/B738/engine/mixture_ratio2")

B738DR_fms_legs_offset_dist		= find_dataref("laminar/B738/fms/legs_offset_dist")
B738DR_fms_legs_offset_engage	= find_dataref("laminar/B738/fms/legs_offset_engage")

B738DR_touchdown				= find_dataref("laminar/B738/effect/touchdown")

B738DR_irs_nd_allign			= find_dataref("laminar/B738/irs/nd_allign")
B738DR_irs_nd_allign_gs			= create_dataref("laminar/B738/irs/nd_allign_gs", "number")
B738DR_irs_nd_allign2			= find_dataref("laminar/B738/irs/nd_allign2")
B738DR_irs_nd_allign_gs2		= create_dataref("laminar/B738/irs/nd_allign_gs2", "number")
B738DR_irs_nd_allign_12			= find_dataref("laminar/B738/irs/nd_allign12")

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

B738DR_gpws_terr_pos 			= find_dataref("laminar/B738/toggle_switch/gpws_terr_pos")
B738DR_gpws_terr_warn 			= find_dataref("laminar/B738/gpws/terr_warn")
B738DR_efis_terr_warn			= create_dataref("laminar/B738/nd/terrain_warning", "number")
B738DR_efis_terr_warn_fo		= create_dataref("laminar/B738/nd/terrain_warning_fo", "number")
B738DR_efis_terr_inhibited		= create_dataref("laminar/B738/nd/terrain_inhibited", "number")

B738DR_load_save				= find_dataref("laminar/B738/load_save")
B738DR_glare					= find_dataref("laminar/B738/glare")
B738DR_glare_sw					= find_dataref("laminar/B738/glare_sw")

B738DR_flt_dk_door_ratio	= find_dataref("laminar/B738/door/flt_dk_door_ratio")
door_ratio_fwd_L 			= find_dataref("737u/doors/L1")
door_ratio_fwd_R 			= find_dataref("737u/doors/R1")
B738DR_hide_airstairs		= find_dataref("laminar/B738/airstairs_hide")

B738DR_legs_intdir_dist 	= find_dataref("laminar/B738/fms/legs_intdir_dist")

B738DR_activate_pitch_mode	= find_dataref("laminar/B738/FMS/activate_pitch_mode")

B738DR_vnav_wakeup_idle			= find_dataref("laminar/B738/vnav_wakeup_idle")

B738DR_autoland_fail_status		= find_dataref("laminar/B738/autoland/fail_status")
B738DR_vnav_common				= find_dataref("laminar/B738/vnav_common")

B738DR_b737_variant			= find_dataref("zibomod/b737_variant")

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

function B738DR_baro_sel_in_hg_pilot_hdlr()
	if B738DR_baro_sel_in_hg_pilot < 22 then
		B738DR_baro_sel_in_hg_pilot = 22
	elseif B738DR_baro_sel_in_hg_pilot > 32 then
		B738DR_baro_sel_in_hg_pilot = 32
	end
	if B738DR_dspl_ctrl_pnl == -1 then
		B738DR_baro_sel_in_hg_copilot = B738DR_baro_sel_in_hg_pilot
	end
end

function B738DR_baro_sel_in_hg_copilot_hdlr()
	if B738DR_baro_sel_in_hg_copilot < 22 then
		B738DR_baro_sel_in_hg_copilot = 22
	elseif B738DR_baro_sel_in_hg_copilot > 32 then
		B738DR_baro_sel_in_hg_copilot = 32
	end
	if B738DR_dspl_ctrl_pnl == 1 then
		B738DR_baro_sel_in_hg_pilot = B738DR_baro_sel_in_hg_copilot
	end
	
end

--*************************************************************************************--
--** 				        CREATE READ-ONLY CUSTOM DATAREFS               	         **--
--*************************************************************************************--

B738DR_gps1_pos_show		= create_dataref("laminar/B738/pfd/gps1_pos_show", "number")
B738DR_gps1_pos_x 			= create_dataref("laminar/B738/pfd/gps1_pos_x", "number")
B738DR_gps1_pos_y 			= create_dataref("laminar/B738/pfd/gps1_pos_y", "number")

B738DR_gps2_pos_show		= create_dataref("laminar/B738/pfd/gps2_pos_show", "number")
B738DR_gps2_pos_x 			= create_dataref("laminar/B738/pfd/gps2_pos_x", "number")
B738DR_gps2_pos_y 			= create_dataref("laminar/B738/pfd/gps2_pos_y", "number")

B738DR_adiru1_pos_show		= create_dataref("laminar/B738/pfd/adiru1_pos_show", "number")
B738DR_adiru1_pos_x 		= create_dataref("laminar/B738/pfd/adiru1_pos_x", "number")
B738DR_adiru1_pos_y 		= create_dataref("laminar/B738/pfd/adiru1_pos_y", "number")

B738DR_adiru2_pos_show		= create_dataref("laminar/B738/pfd/adiru2_pos_show", "number")
B738DR_adiru2_pos_x 		= create_dataref("laminar/B738/pfd/adiru2_pos_x", "number")
B738DR_adiru2_pos_y 		= create_dataref("laminar/B738/pfd/adiru2_pos_y", "number")

B738DR_gps1_pos_fo_show		= create_dataref("laminar/B738/pfd/gps1_pos_fo_show", "number")
B738DR_gps1_pos_x_fo 		= create_dataref("laminar/B738/pfd/gps1_pos_x_fo", "number")
B738DR_gps1_pos_y_fo 		= create_dataref("laminar/B738/pfd/gps1_pos_y_fo", "number")

B738DR_gps2_pos_fo_show		= create_dataref("laminar/B738/pfd/gps2_pos_fo_show", "number")
B738DR_gps2_pos_x_fo 		= create_dataref("laminar/B738/pfd/gps2_pos_x_fo", "number")
B738DR_gps2_pos_y_fo 		= create_dataref("laminar/B738/pfd/gps2_pos_y_fo", "number")

B738DR_adiru1_pos_fo_show	= create_dataref("laminar/B738/pfd/adiru1_pos_fo_show", "number")
B738DR_adiru1_pos_x_fo 		= create_dataref("laminar/B738/pfd/adiru1_pos_x_fo", "number")
B738DR_adiru1_pos_y_fo 		= create_dataref("laminar/B738/pfd/adiru1_pos_y_fo", "number")

B738DR_adiru2_pos_fo_show	= create_dataref("laminar/B738/pfd/adiru2_pos_fo_show", "number")
B738DR_adiru2_pos_x_fo 		= create_dataref("laminar/B738/pfd/adiru2_pos_x_fo", "number")
B738DR_adiru2_pos_y_fo 		= create_dataref("laminar/B738/pfd/adiru2_pos_y_fo", "number")

B738DR_vor1_pos_show		= create_dataref("laminar/B738/pfd/vor1_pos_show", "number")
B738DR_vor1_pos_x			= create_dataref("laminar/B738/pfd/vor1_pos_x", "number")
B738DR_vor1_pos_y			= create_dataref("laminar/B738/pfd/vor1_pos_y", "number")
B738DR_vor1_pos_rotate		= create_dataref("laminar/B738/pfd/vor1_pos_rotate", "number")
B738DR_vor1_pos_crs			= create_dataref("laminar/B738/pfd/vor1_pos_crs", "string")
B738DR_vor1_pos_lr			= create_dataref("laminar/B738/pfd/vor1_pos_lr", "number")

B738DR_vor2_pos_show		= create_dataref("laminar/B738/pfd/vor2_pos_show", "number")
B738DR_vor2_pos_x			= create_dataref("laminar/B738/pfd/vor2_pos_x", "number")
B738DR_vor2_pos_y			= create_dataref("laminar/B738/pfd/vor2_pos_y", "number")
B738DR_vor2_pos_rotate		= create_dataref("laminar/B738/pfd/vor2_pos_rotate", "number")
B738DR_vor2_pos_crs			= create_dataref("laminar/B738/pfd/vor2_pos_crs", "string")
B738DR_vor2_pos_lr			= create_dataref("laminar/B738/pfd/vor2_pos_lr", "number")

B738DR_vor1_pos_fo_show		= create_dataref("laminar/B738/pfd/vor1_pos_fo_show", "number")
B738DR_vor1_pos_x_fo		= create_dataref("laminar/B738/pfd/vor1_pos_x_fo", "number")
B738DR_vor1_pos_y_fo		= create_dataref("laminar/B738/pfd/vor1_pos_y_fo", "number")
B738DR_vor1_pos_rotate_fo	= create_dataref("laminar/B738/pfd/vor1_pos_rotate_fo", "number")
B738DR_vor1_pos_crs_fo		= create_dataref("laminar/B738/pfd/vor1_pos_crs_fo", "string")
B738DR_vor1_pos_lr_fo		= create_dataref("laminar/B738/pfd/vor1_pos_lr_fo", "number")

B738DR_vor2_pos_fo_show		= create_dataref("laminar/B738/pfd/vor2_pos_fo_show", "number")
B738DR_vor2_pos_x_fo		= create_dataref("laminar/B738/pfd/vor2_pos_x_fo", "number")
B738DR_vor2_pos_y_fo		= create_dataref("laminar/B738/pfd/vor2_pos_y_fo", "number")
B738DR_vor2_pos_rotate_fo	= create_dataref("laminar/B738/pfd/vor2_pos_rotate_fo", "number")
B738DR_vor2_pos_crs_fo		= create_dataref("laminar/B738/pfd/vor2_pos_crs_fo", "string")
B738DR_vor2_pos_lr_fo		= create_dataref("laminar/B738/pfd/vor2_pos_lr_fo", "number")

B738DR_vor1_show	 		= find_dataref("laminar/B738/pfd/vor1_show")
B738DR_vor2_show	 		= find_dataref("laminar/B738/pfd/vor2_show")

-- CAPT EFIS DATAREFS

B738DR_efis_wxr_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/wxr", "number")
B738DR_efis_sta_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/sta", "number")
B738DR_efis_wpt_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/wpt", "number")
B738DR_efis_arpt_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/arpt", "number")
B738DR_efis_data_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/data", "number")
B738DR_efis_pos_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/pos", "number")
B738DR_efis_terr_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/terr", "number")

B738DR_efis_data_capt_status	= create_dataref("laminar/B738/EFIS/capt/data_status", "number")


B738DR_efis_rst_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/rst", "number")
B738DR_efis_ctr_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/ctr", "number")
B738DR_efis_tfc_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/tfc", "number")
B738DR_efis_std_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/std", "number")

B738DR_efis_vor_on		= create_dataref("laminar/B738/EFIS/EFIS_vor_on", "number")
B738DR_efis_apt_on		= create_dataref("laminar/B738/EFIS/EFIS_airport_on", "number")
B738DR_efis_fix_on		= create_dataref("laminar/B738/EFIS/EFIS_fix_on", "number")
B738DR_efis_wxr_on		= create_dataref("laminar/B738/EFIS/EFIS_wx_on", "number")

B738DR_efis_wxr_terr	= create_dataref("laminar/B738/EFIS/EFIS_wxr_terr", "number")
B738DR_efis_nav			= create_dataref("laminar/B738/EFIS/EFIS_nav", "number")
B738DR_efis_fo_wxr_terr	= create_dataref("laminar/B738/EFIS/EFIS_fo_wxr_terr", "number")
B738DR_efis_fo_nav		= create_dataref("laminar/B738/EFIS/EFIS_fo_nav", "number")

---
B738DR_baro_set_std_pilot		= create_dataref("laminar/B738/EFIS/baro_set_std_pilot", "number")
B738DR_baro_sel_in_hg_pilot		= create_dataref("laminar/B738/EFIS/baro_sel_in_hg_pilot", "number", B738DR_baro_sel_in_hg_pilot_hdlr)
B738DR_baro_sel_pilot_show		= create_dataref("laminar/B738/EFIS/baro_sel_pilot_show", "number")
B738DR_baro_box_pilot_show		= create_dataref("laminar/B738/EFIS/baro_box_pilot_show", "number")
B738DR_baro_std_box_pilot_show	= create_dataref("laminar/B738/EFIS/baro_std_box_pilot_show", "number")
---

B738DR_efis_mtrs_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/mtrs", "number")
B738DR_efis_fpv_capt	= create_dataref("laminar/B738/EFIS_control/capt/push_button/fpv", "number")

B738DR_efis_baro_mode_capt		= create_dataref("laminar/B738/EFIS_control/capt/baro_in_hpa", "number")
B738DR_efis_baro_mode_capt_pfd	= create_dataref("laminar/B738/EFIS_control/capt/baro_in_hpa_pfd", "number")

B738DR_efis_vor1_capt_pos		= find_dataref("laminar/B738/EFIS_control/capt/vor1_off_pos")
B738DR_efis_vor2_capt_pos		= find_dataref("laminar/B738/EFIS_control/capt/vor2_off_pos")

B738DR_efis_vor1_capt_pfd	= create_dataref("laminar/B738/EFIS_control/capt/vor1_off_pfd", "number")
B738DR_efis_vor2_capt_pfd	= create_dataref("laminar/B738/EFIS_control/capt/vor2_off_pfd", "number")

B738DR_capt_alt_mode_meters		= create_dataref("laminar/B738/PFD/capt/alt_mode_is_meters", "number")
B738DR_capt_fpv_on				= create_dataref("laminar/B738/PFD/capt/fpv_on", "number")


-- FO EFIS DATAREFS

B738DR_efis_wxr_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/wxr", "number")
B738DR_efis_sta_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/sta", "number")
B738DR_efis_wpt_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/wpt", "number")
B738DR_efis_arpt_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/arpt", "number")
B738DR_efis_data_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/data", "number")
B738DR_efis_pos_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/pos", "number")
B738DR_efis_terr_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/terr", "number")

B738DR_efis_data_fo_status	= create_dataref("laminar/B738/EFIS/fo/data_status", "number")

B738DR_efis_rst_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/rst", "number")
B738DR_efis_ctr_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/ctr", "number")
B738DR_efis_tfc_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/tfc", "number")
B738DR_efis_std_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/std", "number")

B738DR_efis_fo_vor_on		= create_dataref("laminar/B738/EFIS/fo/EFIS_vor_on", "number")
B738DR_efis_fo_apt_on		= create_dataref("laminar/B738/EFIS/fo/EFIS_airport_on", "number")
B738DR_efis_fo_fix_on		= create_dataref("laminar/B738/EFIS/fo/EFIS_fix_on", "number")
B738DR_efis_fo_wxr_on		= create_dataref("laminar/B738/EFIS/fo/EFIS_wx_on", "number")
---
B738DR_baro_set_std_copilot			= create_dataref("laminar/B738/EFIS/baro_set_std_copilot", "number")
B738DR_baro_sel_in_hg_copilot		= create_dataref("laminar/B738/EFIS/baro_sel_in_hg_copilot", "number", B738DR_baro_sel_in_hg_copilot_hdlr)
B738DR_baro_sel_copilot_show		= create_dataref("laminar/B738/EFIS/baro_sel_copilot_show", "number")
B738DR_baro_box_copilot_show		= create_dataref("laminar/B738/EFIS/baro_box_copilot_show", "number")
B738DR_baro_std_box_copilot_show	= create_dataref("laminar/B738/EFIS/baro_std_box_copilot_show", "number")
---

B738DR_efis_mtrs_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/mtrs", "number")
B738DR_efis_fpv_fo	= create_dataref("laminar/B738/EFIS_control/fo/push_button/fpv", "number")

B738DR_efis_baro_mode_fo		= create_dataref("laminar/B738/EFIS_control/fo/baro_in_hpa", "number")
B738DR_efis_baro_mode_fo_pfd 	= create_dataref("laminar/B738/EFIS_control/fo/baro_in_hpa_fo", "number")

B738DR_efis_vor1_fo_pos			= find_dataref("laminar/B738/EFIS_control/fo/vor1_off_pos")
B738DR_efis_vor2_fo_pos			= find_dataref("laminar/B738/EFIS_control/fo/vor2_off_pos")

B738DR_efis_vor1_fo_pfd		= create_dataref("laminar/B738/EFIS_control/fo/vor1_off_pfd", "number")
B738DR_efis_vor2_fo_pfd		= create_dataref("laminar/B738/EFIS_control/fo/vor2_off_pfd", "number")

B738DR_fo_alt_mode_meters		= create_dataref("laminar/B738/PFD/fo/alt_mode_is_meters", "number")
B738DR_fo_fpv_on				= create_dataref("laminar/B738/PFD/fo/fpv_on", "number")

B738DR_efis_rings				= create_dataref("laminar/B738/EFIS_control/capt/rings", "number")
B738DR_efis_terr_on				= create_dataref("laminar/B738/EFIS_control/capt/terr_on", "number")

B738DR_efis_terr_fail			= create_dataref("laminar/B738/nd/terrain_fail", "number")
B738DR_efis_fo_terr_fail		= create_dataref("laminar/B738/nd/terrain_fail_fo", "number")
B738DR_efis_wx_fail				= create_dataref("laminar/B738/nd/wx_fail", "number")
B738DR_efis_fo_wx_fail			= create_dataref("laminar/B738/nd/wx_fail_fo", "number")

B738DR_efis_fo_rings			= create_dataref("laminar/B738/EFIS_control/fo/rings", "number")
B738DR_efis_fo_terr_on			= create_dataref("laminar/B738/EFIS_control/fo/terr_on", "number")

-- AP BUTTON / SWITCH POSITION DRS

B738DR_autopilot_n1_pos				= create_dataref("laminar/B738/autopilot/n1_pos", "number")
B738DR_autopilot_speed_pos			= create_dataref("laminar/B738/autopilot/speed_pos", "number")
B738DR_autopilot_lvl_chg_pos		= create_dataref("laminar/B738/autopilot/lvl_chg_pos", "number")
B738DR_autopilot_vnav_pos			= create_dataref("laminar/B738/autopilot/vnav_pos", "number")
B738DR_autopilot_co_pos				= create_dataref("laminar/B738/autopilot/change_over_pos", "number")
	
B738DR_autopilot_lnav_pos			= create_dataref("laminar/B738/autopilot/lnav_pos", "number")
B738DR_autopilot_vorloc_pos			= create_dataref("laminar/B738/autopilot/vorloc_pos", "number")
B738DR_autopilot_app_pos			= create_dataref("laminar/B738/autopilot/app_pos", "number")
B738DR_autopilot_hdg_sel_pos		= create_dataref("laminar/B738/autopilot/hdg_sel_pos", "number")

B738DR_autopilot_alt_hld_pos		= create_dataref("laminar/B738/autopilot/alt_hld_pos", "number")
B738DR_autopilot_vs_pos				= create_dataref("laminar/B738/autopilot/vs_pos", "number")

B738DR_autopilot_cmd_a_pos			= create_dataref("laminar/B738/autopilot/cmd_a_pos", "number")
B738DR_autopilot_cmd_b_pos			= create_dataref("laminar/B738/autopilot/cmd_b_pos", "number")
B738DR_autopilot_cws_a_pos			= create_dataref("laminar/B738/autopilot/cws_a_pos", "number")
B738DR_autopilot_cws_b_pos			= create_dataref("laminar/B738/autopilot/cws_b_pos", "number")
B738DR_autopilot_disconnect_pos		= create_dataref("laminar/B738/autopilot/disconnect_pos", "number")
B738DR_autopilot_disco2				= create_dataref("laminar/B738/autopilot/disconnect_button", "number")

B738DR_autopilot_fd_pos				= create_dataref("laminar/B738/autopilot/flight_director_pos", "number")
B738DR_autopilot_fd_fo_pos			= create_dataref("laminar/B738/autopilot/flight_director_fo_pos", "number")
B738DR_autopilot_bank_angle_pos		= create_dataref("laminar/B738/autopilot/bank_angle_pos", "number")
B738DR_autopilot_autothr_arm_pos	= create_dataref("laminar/B738/autopilot/autothrottle_arm_pos", "number")


-- AP STATUS LIGHT DRS

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

B738DR_autopilot_n1_status				= create_dataref("laminar/B738/autopilot/n1_status1", "number")
B738DR_autopilot_n1_status_cfy			= create_dataref("laminar/B738/autopilot/n1_status", "number")

B738DR_autopilot_speed_status			= create_dataref("laminar/B738/autopilot/speed_status1", "number")
B738DR_autopilot_lvl_chg_status			= create_dataref("laminar/B738/autopilot/lvl_chg_status", "number")
B738DR_autopilot_vnav_status			= create_dataref("laminar/B738/autopilot/vnav_status1", "number")
B738DR_autopilot_vnav_status2			= create_dataref("laminar/B738/autopilot/vnav_status2", "number")
	
B738DR_autopilot_lnav_status			= create_dataref("laminar/B738/autopilot/lnav_status", "number")
B738DR_autopilot_lnav_status2			= create_dataref("laminar/B738/autopilot/lnav_status2", "number")
	
B738DR_autopilot_vorloc_status			= create_dataref("laminar/B738/autopilot/vorloc_status", "number")
B738DR_autopilot_app_status				= create_dataref("laminar/B738/autopilot/app_status", "number")
B738DR_autopilot_hdg_sel_status			= create_dataref("laminar/B738/autopilot/hdg_sel_status", "number")

B738DR_autopilot_alt_hld_status			= create_dataref("laminar/B738/autopilot/alt_hld_status", "number")
B738DR_autopilot_vs_status				= create_dataref("laminar/B738/autopilot/vs_status", "number")

B738DR_autopilot_cmd_a_status			= create_dataref("laminar/B738/autopilot/cmd_a_status", "number")
B738DR_autopilot_cmd_b_status			= create_dataref("laminar/B738/autopilot/cmd_b_status", "number")
B738DR_autopilot_cws_a_status			= create_dataref("laminar/B738/autopilot/cws_a_status", "number")
B738DR_autopilot_cws_b_status			= create_dataref("laminar/B738/autopilot/cws_b_status", "number")

B738DR_autopilot_autothrottle_status	= create_dataref("laminar/B738/autopilot/autothrottle_status1", "number")
B738DR_autopilot_at_status_cfy			= find_dataref("laminar/B738/autopilot/autothrottle_status")

B738DR_autopilot_cfy					= create_dataref("laminar/B738/cfy/autopilot", "number")
B738DR_autothrottle_cfy					= create_dataref("laminar/B738/cfy/autothrottle", "number")
B738DR_trim_wheel_speed_cfy				= create_dataref("laminar/B738/cfy/trim_wheel_speed", "number")

B738DR_autopilot_master_capt_status		= create_dataref("laminar/B738/autopilot/master_capt_status", "number")
B738DR_autopilot_master_fo_status		= create_dataref("laminar/B738/autopilot/master_fo_status", "number")

B738DR_autopilot_vhf_source_pos			= find_dataref("laminar/B738/toggle_switch/vhf_nav_source")

B738DR_flare_status						= create_dataref("laminar/B738/autopilot/flare_status", "number")
B738DR_rollout_status					= create_dataref("laminar/B738/autopilot/rollout_status", "number")
B738DR_autoland_status					= create_dataref("laminar/B738/autopilot/autoland_status", "number")
B738DR_retard_status					= create_dataref("laminar/B738/autopilot/retard_status", "number")
B738DR_single_ch_status					= create_dataref("laminar/B738/autopilot/single_ch_status", "number")
B738DR_single_ch_g_status				= create_dataref("laminar/B738/autopilot/single_ch_g_status", "number")
B738DR_ils_pointer_disable				= create_dataref("laminar/B738/autopilot/ils_pointer_disable", "number")
B738DR_cws_status						= create_dataref("laminar/B738/autopilot/cws_status", "number")
B738DR_cws_r_status						= create_dataref("laminar/B738/autopilot/cws_r_status", "number")
B738DR_cws_p_status						= create_dataref("laminar/B738/autopilot/cws_p_status", "number")
B738DR_no_autoland						= create_dataref("laminar/B738/autopilot/no_autoland", "number")

B738DR_vorloc_lnav						= find_dataref("laminar/B738/vorloc_lnav")
B738DR_vorloc_lnav_enable				= find_dataref("laminar/B738/vorloc_lnav_enable")
B738DR_autoland_install					= find_dataref("laminar/B738/autoland_install")
--

B738DR_autopilot_autothrottle_on		= create_dataref("laminar/B738/autopilot/autothrottle_on_pfd", "number")
B738DR_autopilot_autothrottle_pfd		= create_dataref("laminar/B738/autopilot/autothrottle_pfd", "number")
B738DR_autopilot_n1_pfd					= create_dataref("laminar/B738/autopilot/n1_pfd", "number")
B738DR_autopilot_ga_pfd					= create_dataref("laminar/B738/autopilot/ga_pfd", "number")
B738DR_autopilot_thr_hld_pfd			= create_dataref("laminar/B738/autopilot/thr_hld_pfd", "number")
B738DR_autopilot_to_ga_pfd				= create_dataref("laminar/B738/autopilot/to_ga_pfd", "number")
B738DR_autopilot_alt_acq_pfd			= create_dataref("laminar/B738/autopilot/alt_acq_pfd", "number")

B738DR_autopilot_fmc_spd_pfd			= create_dataref("laminar/B738/autopilot/fmc_spd_pfd", "number")
B738DR_autopilot_vnav_alt_pfd			= create_dataref("laminar/B738/autopilot/vnav_alt_pfd", "number")
B738DR_autopilot_vnav_pth_pfd			= create_dataref("laminar/B738/autopilot/vnav_pth_pfd", "number")
B738DR_autopilot_vnav_spd_pfd			= create_dataref("laminar/B738/autopilot/vnav_spd_pfd", "number")
B738DR_autopilot_hnav_armed				= create_dataref("laminar/B738/autopilot/hnav_armed", "number")
B738DR_autopilot_alt_mode_pfd			= create_dataref("laminar/B738/autopilot/alt_mode_pfd", "number")
B738DR_autopilot_hdg_mode_pfd			= create_dataref("laminar/B738/autopilot/hdg_mode_pfd", "number")
B738DR_autopilot_vnav_arm_pfd			= create_dataref("laminar/B738/autopilot/vnav_arm_pfd", "number")

B738DR_autopilot_gs_armed				= create_dataref("laminar/B738/autopilot/gs_armed_pfd", "number")

-- AUTOPILOT PFD ANNUNCIATES --
B738DR_pfd_spd_mode						= create_dataref("laminar/B738/autopilot/pfd_spd_mode", "number")
B738DR_pfd_hdg_mode						= create_dataref("laminar/B738/autopilot/pfd_hdg_mode", "number")
B738DR_pfd_alt_mode						= create_dataref("laminar/B738/autopilot/pfd_alt_mode", "number")
B738DR_pfd_hdg_mode_arm					= create_dataref("laminar/B738/autopilot/pfd_hdg_mode_arm", "number")
B738DR_pfd_alt_mode_arm					= create_dataref("laminar/B738/autopilot/pfd_alt_mode_arm", "number")

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

B738DR_rec_thr_modes					= create_dataref("laminar/B738/autopilot/rec_thr_modes", "number")
B738DR_rec_thr2_modes					= create_dataref("laminar/B738/autopilot/rec_thr2_modes", "number")
B738DR_rec_hdg_modes					= create_dataref("laminar/B738/autopilot/rec_hdg_modes", "number")
B738DR_rec_alt_modes					= create_dataref("laminar/B738/autopilot/rec_alt_modes", "number")
B738DR_rec_cmd_modes1					= create_dataref("laminar/B738/autopilot/rec_cmd_modes", "number")
B738DR_rec_cmd_modes2					= create_dataref("laminar/B738/autopilot/rec_cmd_modes_fo", "number")
B738DR_rec_sch_modes					= create_dataref("laminar/B738/autopilot/rec_sch_modes", "number")
B738DR_rec_schg_modes					= create_dataref("laminar/B738/autopilot/rec_schg_modes", "number")
B738DR_rec_alt_alert					= create_dataref("laminar/B738/autopilot/rec_alt_alert", "number")
B738DR_play_fo_1000_to_go				= create_dataref("laminar/b738/fmodpack/play_fo_1000_to_go", "number")
B738DR_app_alt_alert					= create_dataref("laminar/B738/autopilot/rec_app_alert", "number")
B738DR_dev_alt_alert					= create_dataref("laminar/B738/autopilot/dev_alt_alert", "number")
B738DR_rec_cwr_r_modes					= create_dataref("laminar/B738/autopilot/rec_cwr_r_mode", "number")
B738DR_rec_cwr_p_modes					= create_dataref("laminar/B738/autopilot/rec_cwr_p_mode", "number")
B738DR_rec_spd_ind						= create_dataref("laminar/B738/autopilot/rec_spd_ind", "number")
B738DR_spd_ind_mode						= create_dataref("laminar/B738/autopilot/spd_ind_mode", "number")

B738DR_ap_warning						= create_dataref("laminar/B738/autopilot/ap_warn", "number")
B738DR_fmod_ian_autopilot				= create_dataref("laminar/B738/fmod/ian_autopilot", "number")

B738DR_AP_airspeed 						= create_dataref("laminar/B738/autopilot/airspeed", "number")
B738DR_AP_airspeed_mach 				= create_dataref("laminar/B738/autopilot/airspeed_mach", "number")
B738DR_AP_altitude 						= create_dataref("laminar/B738/autopilot/altitude", "number")
B738DR_AP_vvi							= create_dataref("laminar/B738/autopilot/vvi", "number")
B738DR_AP_acceleration					= create_dataref("laminar/B738/autopilot/acceleration", "number")
------------------------------

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


B738DR_blink							= create_dataref("laminar/B738/autopilot/blink", "number")

B738DR_kts_disable						= create_dataref("laminar/B738/autopilot/kts_disable", "number")
B738DR_mach_disable						= create_dataref("laminar/B738/autopilot/mach_disable", "number")

B738DR_autopilot_vvi_status_pfd			= create_dataref("laminar/B738/autopilot/vvi_status_pfd", "number")


B738DR_pfd_h_dots_pilot					= create_dataref("laminar/B738/PFD/h_dots_pilot", "number")
B738DR_pfd_v_dots_pilot					= create_dataref("laminar/B738/PFD/v_dots_pilot", "number")
B738DR_pfd_h_dots_pilot_show			= create_dataref("laminar/B738/PFD/h_dots_pilot_show", "number")
B738DR_pfd_v_dots_pilot_show			= create_dataref("laminar/B738/PFD/v_dots_pilot_show", "number")
B738DR_pfd_ils_pilot_show				= create_dataref("laminar/B738/PFD/ils_pilot_show", "number")

function DR_lowerDU_page_handler() end
function DR_lowerDU_page2_handler() end
B738DR_lowerDU_page						= create_dataref("laminar/B738/systems/lowerDU_page", "number", DR_lowerDU_page_handler)
B738DR_lowerDU_page2					= create_dataref("laminar/B738/systems/lowerDU_page2", "number", DR_lowerDU_page2_handler)
B738DR_EICAS_page						= create_dataref("laminar/B738/systems/EICAS_page", "number")
B738DR_ff_eicas_show					= create_dataref("laminar/B738/ff_eicas_show", "number")
B738DR_ff_eicas							= find_dataref("laminar/B738/ff_eicas")

B738DR_mfd_sys_pos						= create_dataref("laminar/B738/buttons/mfd_sys_pos", "number")
B738DR_mfd_eng_pos						= create_dataref("laminar/B738/buttons/mfd_eng_pos", "number")


B738DR_autopilot_side					= create_dataref("laminar/B738/autopilot/autopilot_source", "number")

B738DR_fd_on							= create_dataref("laminar/B738/autopilot/flight_direct_on", "number")

B738DR_pfd_fd_cmd						= create_dataref("laminar/B738/autopilot/pfd_fd_cmd", "number")
B738DR_pfd_fd_cmd_fo					= create_dataref("laminar/B738/autopilot/pfd_fd_cmd_fo", "number")
B738DR_pfd_fd_cmd_show					= create_dataref("laminar/B738/autopilot/pfd_fd_cmd_show", "number")
B738DR_pfd_fd_cmd_fo_show				= create_dataref("laminar/B738/autopilot/pfd_fd_cmd_fo_show", "number")

B738DR_altitude_mode					= find_dataref("laminar/B738/autopilot/altitude_mode")
B738DR_heading_mode						= find_dataref("laminar/B738/autopilot/heading_mode")
B738DR_speed_mode						= find_dataref("laminar/B738/autopilot/speed_mode")
B738DR_altitude_mode2					= create_dataref("laminar/B738/autopilot/altitude_mode2", "number")
B738DR_speed_mode2						= create_dataref("laminar/B738/autopilot/speed_mode2", "number")
B738DR_vnav_engaged						= create_dataref("laminar/B738/autopilot/vnav_engaged", "number")
-- B738DR_ap_alt_acq_status				= create_dataref("laminar/B738/autopilot/alt_acq_status", "number")

B738DR_ap_goaround 						= find_dataref("laminar/B738/autopilot/ap_goaround")
B738DR_fd_goaround 						= find_dataref("laminar/B738/autopilot/fd_goaround")
B738DR_at_takeoff						= create_dataref("laminar/B738/autopilot/ap_takeoff", "number")

B738DR_ap_disconnect					= create_dataref("laminar/B738/annunciator/ap_disconnect", "number")
B738DR_at_disconnect					= create_dataref("laminar/B738/annunciator/at_disconnect", "number")
B738DR_snd_ap_disconnect				= create_dataref("laminar/b738/fmodpack/fmod_ap_disconnect", "number")

B738DR_source_pilot						= create_dataref("laminar/B738/autopilot/source_pilot", "number")
B738DR_source_arm_pilot					= create_dataref("laminar/B738/autopilot/source_arm_pilot", "number")

B738DR_vvi_dial_show					= create_dataref("laminar/B738/autopilot/vvi_dial_show", "number")

B738DR_fd_target						= create_dataref("laminar/B738/flare/fd_target", "number")
B738DR_vvi_trend						= create_dataref("laminar/B738/flare/vvi_trend", "number")

B738DR_pfd_vorloc_lnav					= create_dataref("laminar/B738/autopilot/pfd_vorloc_lnav", "number")

B738DR_fd_roll_pilot_show				= create_dataref("laminar/B738/autopilot/fd_roll_pilot_show", "number")
B738DR_fd_pitch_pilot_show				= create_dataref("laminar/B738/autopilot/fd_pitch_pilot_show", "number")
B738DR_fd_roll_copilot_show				= create_dataref("laminar/B738/autopilot/fd_roll_copilot_show", "number")
B738DR_fd_pitch_copilot_show			= create_dataref("laminar/B738/autopilot/fd_pitch_copilot_show", "number")
B738DR_single_cue_fd					= find_dataref("laminar/B738/single_cue_fd")
B738DR_fd_pilot_show					= create_dataref("laminar/B738/autopilot/fd_pilot_show", "number")
B738DR_fd_copilot_show					= create_dataref("laminar/B738/autopilot/fd_copilot_show", "number")

B738DR_ias_disagree						= create_dataref("laminar/B738/autopilot/ias_disagree", "number")
B738DR_alt_disagree						= create_dataref("laminar/B738/autopilot/alt_disagree", "number")

B738DR_eicas_full_n1		= find_dataref("laminar/B738/eicas_full_n1")

B738DR_vnav_speed_trg				= create_dataref("laminar/B738/autopilot/vnav_speed_trg", "number")
B738DR_lnav_engaged				= create_dataref("laminar/B738/autopilot/lnav_engaged", "number")
B738DR_ap_wca						= create_dataref("laminar/B738/autopilot/lnav_ap_wca", "number")

B738DR_cb_d9				= create_dataref("laminar/B738/cb/d9", "number")

B738DR_spd_wup				= create_dataref("laminar/B738/pfd/spd_wup", "number")
B738DR_spd_wdn				= create_dataref("laminar/B738/pfd/spd_wdn", "number")
B738DR_at_wakeup			= find_dataref("laminar/B738/pfd/at_wakeup")
B738DR_at_wakeup_show		= create_dataref("laminar/B738/pfd/at_wakeup_show", "number")

function pitch_ratio2_old_handl() end
B738DR_pitch_ratio2_old			= create_dataref("laminar/B738/yoke/pitch_ratio2_old", "number", pitch_ratio2_old_handl)

B738DR_course_pilot			= find_dataref("laminar/B738/autopilot/course_pilot")
B738DR_course_copilot		= find_dataref("laminar/B738/autopilot/course_copilot")

B738DR_relative_hdg_crs_pilot	= create_dataref("laminar/B738/nd/relative_hdg_crs_pilot", "number")
B738DR_relative_hdg_crs_copilot	= create_dataref("laminar/B738/nd/relative_hdg_crs_copilot", "number")

B738DR_pfd_runway_show		= create_dataref("laminar/B738/pfd/runway_show", "number")
B738DR_pfd_runway_x			= create_dataref("laminar/B738/pfd/runway_x", "number")
B738DR_pfd_runway_y			= create_dataref("laminar/B738/pfd/runway_y", "number")
B738DR_pfd_runway_show_fo	= create_dataref("laminar/B738/pfd/runway_show_fo", "number")
B738DR_pfd_runway_x_fo		= create_dataref("laminar/B738/pfd/runway_x_fo", "number")
B738DR_pfd_runway_y_fo		= create_dataref("laminar/B738/pfd/runway_y_fo", "number")

B738DR_autopilot_spd_interv_pos	= create_dataref("laminar/B738/autopilot/spd_interv_pos", "number")
B738DR_ap_spd_interv_status		= create_dataref("laminar/B738/autopilot/spd_interv_status", "number")
B738DR_spd_intv_but				= find_dataref("laminar/B738/spd_intv_but")
B738DR_ap_vvi_pos				= create_dataref("laminar/B738/autopilot/ap_vvi_pos", "number")

B738DR_EFIS_TCAS_on			= create_dataref("laminar/B738/EFIS/tcas_on", "number")
B738DR_EFIS_TCAS_on_fo		= create_dataref("laminar/B738/EFIS/tcas_on_fo", "number")

B738DR_speed_ratio			= create_dataref("laminar/B738/FMS/speed_ratio", "number")
B738DR_v_speed_ratio		= create_dataref("laminar/B738/FMS/v_speed_ratio", "number")
B738DR_vdot_ratio			= create_dataref("laminar/B738/FMS/vdot_ratio", "number")
B738DR_radio_height_ratio	= create_dataref("laminar/B738/FMS/radio_height_ratio", "number")
B738DR_altitude_pilot_ratio	= create_dataref("laminar/B738/FMS/altitude_pilot_ratio", "number")
B738DR_vvi_pilot_ratio		= create_dataref("laminar/B738/FMS/vvi_pilot_ratio", "number")
B738DR_air_on_acf_ratio		= create_dataref("laminar/B738/FMS/air_on_acf_ratio", "number")
B738DR_eng1_N1_ratio		= create_dataref("laminar/B738/FMS/eng1_N1_ratio", "number")
B738DR_eng2_N1_ratio		= create_dataref("laminar/B738/FMS/eng2_N1_ratio", "number")

B738DR_eng_out				= create_dataref("laminar/B738/FMS/eng_out", "number")
B738DR_eng1_out				= create_dataref("laminar/B738/FMS/eng1_out", "number")
B738DR_eng2_out				= create_dataref("laminar/B738/FMS/eng2_out", "number")
B738DR_eng_out_disable		= find_dataref("laminar/B738/FMS/eng_out_disable")


B738DR_flaps_speed					= create_dataref("laminar/B738/FMS/flaps_speed", "number")
B738DR_vnav_speed					= create_dataref("laminar/B738/FMS/vnav_speed", "number")

B738DR_agl_pilot			= create_dataref("laminar/B738/PFD/agl_pilot", "number")
B738DR_agl_copilot			= create_dataref("laminar/B738/PFD/agl_copilot", "number")

B738DR_pfd_nav1_pilot		= create_dataref("laminar/B738/PFD/nav1_pilot", "number")
B738DR_pfd_nav2_pilot		= create_dataref("laminar/B738/PFD/nav2_pilot", "number")
B738DR_pfd_nav1_copilot		= create_dataref("laminar/B738/PFD/nav1_copilot", "number")
B738DR_pfd_nav2_copilot		= create_dataref("laminar/B738/PFD/nav2_copilot", "number")

B738DR_pfd_loc_exp			= create_dataref("laminar/B738/PFD/loc_exp", "number")
B738DR_pfd_loc_exp_fo		= create_dataref("laminar/B738/PFD/loc_exp_fo", "number")
B738DR_pfd_fac_exp			= create_dataref("laminar/B738/PFD/fac_exp", "number")
B738DR_pfd_fac_exp_fo		= create_dataref("laminar/B738/PFD/fac_exp_fo", "number")
B738DR_pfd_gls_exp			= create_dataref("laminar/B738/PFD/gls_exp", "number")
B738DR_pfd_gls_exp_fo		= create_dataref("laminar/B738/PFD/gls_exp_fo", "number")

B738DR_green_arc	 		= create_dataref("laminar/B738/EFIS/green_arc", "number")
B738DR_green_arc_show		= create_dataref("laminar/B738/EFIS/green_arc_show", "number")
B738DR_green_arc_fo	 		= create_dataref("laminar/B738/EFIS/green_arc_fo", "number")
B738DR_green_arc_fo_show	= create_dataref("laminar/B738/EFIS/green_arc_fo_show", "number")

B738DR_pfd_ias_dig1_pilot	= create_dataref("laminar/B738/PFD/pilot/ias_dig1", "number")
B738DR_pfd_ias_dig2_pilot	= create_dataref("laminar/B738/PFD/pilot/ias_dig2", "number")
B738DR_pfd_ias_dig3_pilot	= create_dataref("laminar/B738/PFD/pilot/ias_dig3", "number")
B738DR_pfd_ias_dig1_copilot	= create_dataref("laminar/B738/PFD/copilot/ias_dig1", "number")
B738DR_pfd_ias_dig2_copilot	= create_dataref("laminar/B738/PFD/copilot/ias_dig2", "number")
B738DR_pfd_ias_dig3_copilot	= create_dataref("laminar/B738/PFD/copilot/ias_dig3", "number")

B738DR_pfd_alt_dig1_pilot	= create_dataref("laminar/B738/PFD/pilot/alt_dig1", "number")
B738DR_pfd_alt_dig2_pilot	= create_dataref("laminar/B738/PFD/pilot/alt_dig2", "number")
B738DR_pfd_alt_dig3_pilot	= create_dataref("laminar/B738/PFD/pilot/alt_dig3", "number")
B738DR_pfd_alt_dig1_copilot	= create_dataref("laminar/B738/PFD/copilot/alt_dig1", "number")
B738DR_pfd_alt_dig2_copilot	= create_dataref("laminar/B738/PFD/copilot/alt_dig2", "number")
B738DR_pfd_alt_dig3_copilot	= create_dataref("laminar/B738/PFD/copilot/alt_dig3", "number")

B738DR_standby_altimeter	= find_dataref("laminar/B738/gauges/standby_altitude_ft")
B738DR_stdby_alt_dig1		= create_dataref("laminar/B738/PFD/stdby/alt_dig1", "number")
B738DR_stdby_alt_dig2		= create_dataref("laminar/B738/PFD/stdby/alt_dig2", "number")


function B738DR_dh_pilot_handl()
	if B738DR_minim_capt == 0 then
		if B738DR_dh_pilot < -1 then
			B738DR_dh_pilot = -1
		elseif B738DR_dh_pilot > 999 then
			B738DR_dh_pilot = 999
		end
		if B738DR_dh_pilot < 0 then
			B738DR_minim_capt_show = 0
			B738DR_dh_pilot = 0
		else
			B738DR_minim_capt_show = 1
		end
		radio_dh_pilot = B738DR_dh_pilot
	else
		if B738DR_dh_pilot < -1001 then
			B738DR_dh_pilot = -1001
		elseif B738DR_dh_pilot > 15000 then
			B738DR_dh_pilot = 15000
		end
		if B738DR_dh_pilot < -1000 then
			B738DR_minim_capt_show = 0
			B738DR_dh_pilot = -1000
		else
			B738DR_minim_capt_show = 1
		end
		baro_dh_pilot = B738DR_dh_pilot
	end
	if B738DR_dspl_ctrl_pnl == -1 then
		B738DR_dh_copilot = B738DR_dh_pilot
		radio_dh_copilot = radio_dh_pilot
		baro_dh_copilot = baro_dh_pilot
		B738DR_minim_fo_show = B738DR_minim_capt_show
	end
end

function B738DR_dh_copilot_handl()
	if B738DR_minim_fo == 0 then
		if B738DR_dh_copilot < -1 then
			B738DR_dh_copilot = -1
		elseif B738DR_dh_copilot > 999 then
			B738DR_dh_copilot = 999
		end
		if B738DR_dh_copilot < 0 then
			B738DR_minim_fo_show = 0
			B738DR_dh_copilot = 0
		else
			B738DR_minim_fo_show = 1
		end
		radio_dh_copilot = B738DR_dh_copilot
	else
		if B738DR_dh_copilot < -1001 then
			B738DR_dh_copilot = -1001
		elseif B738DR_dh_copilot > 15000 then
			B738DR_dh_copilot = 15000
		end
		if B738DR_dh_copilot < -1000 then
			B738DR_minim_fo_show = 0
			B738DR_dh_copilot = -1000
		else
			B738DR_minim_fo_show = 1
		end
		baro_dh_copilot = B738DR_dh_copilot
	end
	if B738DR_dspl_ctrl_pnl == 1 then
		B738DR_dh_pilot = B738DR_dh_copilot
		radio_dh_pilot = radio_dh_copilot
		baro_dh_pilot = baro_dh_copilot
		B738DR_minim_capt_show = B738DR_minim_fo_show
	end
end

B738DR_dh_pilot				= create_dataref("laminar/B738/pfd/dh_pilot", "number", B738DR_dh_pilot_handl)
B738DR_dh_copilot			= create_dataref("laminar/B738/pfd/dh_copilot", "number", B738DR_dh_copilot_handl)

B738DR_vnav_descent_disable		= create_dataref("laminar/B738/FMS/vnav_disable", "number")
B738DR_mcp_speed_dial_kts	= create_dataref("laminar/B738/autopilot/mcp_speed_dial_kts", "number")

function update_mcp_speed_dial()
	local kspd = 1
	if simDR_IAS > 45 then
		kspd = (simDR_TAS * 1.94384449244) / simDR_IAS
	end
	if simDR_airspeed_is_mach == 0 then
		B738DR_mcp_speed_dial_kts = B738DR_mcp_speed_dial
	else
		B738DR_mcp_speed_dial_kts = (B738DR_mcp_speed_dial * (simDR_sos * 1.94384449244)) / kspd
	end
end

function B738DR_mcp_speed_dial_handl()
	local mcp_airspeed_dial2 = B738DR_mcp_speed_dial
	local mcp_airspeed_dial3 = 0
	local kspd = 1
	if simDR_IAS > 45 then
		kspd = (simDR_TAS * 1.94384449244) / simDR_IAS
	end
	if simDR_airspeed_is_mach == 0 then
		mcp_airspeed_dial2 = math.min(mcp_airspeed_dial2, 340)
		mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, 100)
		B738DR_mcp_speed_dial = mcp_airspeed_dial2
		B738DR_mcp_speed_dial_kts = (mcp_airspeed_dial2 * kspd) / (simDR_sos * 1.94384449244)
	else
		mcp_airspeed_dial3 = (100 * kspd) / (simDR_sos * 1.94384449244)
		mcp_airspeed_dial2 = math.min(mcp_airspeed_dial2, 0.82)
		mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, mcp_airspeed_dial3)
		if B738DR_show_ias == 0 then
			mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, 0.40)
		else
			mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, 0.60)
		end
		B738DR_mcp_speed_dial = mcp_airspeed_dial2
		B738DR_mcp_speed_dial_kts = (mcp_airspeed_dial2 * (simDR_sos * 1.94384449244)) / kspd
	end
end

B738DR_mcp_speed_dial		= create_dataref("laminar/B738/autopilot/mcp_speed_dial_kts_mach", "number", B738DR_mcp_speed_dial_handl)

B738DR_efis_disagree	 	= create_dataref("laminar/B738/nd/capt/efis_disagree", "number")
B738DR_efis_disagree_fo	 	= create_dataref("laminar/B738/nd/fo/efis_disagree", "number")
B738DR_nav_vor	 			= create_dataref("laminar/B738/pfd/capt/nav_vor", "number")
B738DR_nav_vor_fo	 		= create_dataref("laminar/B738/pfd/capt/nav_vor_fo", "number")
B738DR_vor1_id_show			= find_dataref("laminar/B738/pfd/vor1_id_show")
B738DR_vor2_id_show			= find_dataref("laminar/B738/pfd/vor2_id_show")
B738DR_tuned_nav1_type		= find_dataref("laminar/B738/nav/nav1_type")
B738DR_tuned_nav2_type		= find_dataref("laminar/B738/nav/nav2_type")
B738DR_tuned_nav1_crs		= find_dataref("laminar/B738/nav/nav1_crs")
B738DR_tuned_nav2_crs		= find_dataref("laminar/B738/nav/nav2_crs")
B738DR_tuned_nav1_no_id		= find_dataref("laminar/B738/nav/nav1_no_id")
B738DR_tuned_nav2_no_id		= find_dataref("laminar/B738/nav/nav2_no_id")

-- FMOD SOUNDS DATAREFS
B738DR_dh_minimum_pilot2		= create_dataref("laminar/B738/fmod/dh_minimum_pilot2", "number")
B738DR_dh_minimum_copilot2		= create_dataref("laminar/B738/fmod/dh_minimum_copilot2", "number")
B738DR_dh_minimum_pilot2_ra		= create_dataref("laminar/B738/fmod/dh_minimum_pilot2_ra", "number")
B738DR_dh_minimum_copilot2_ra	= create_dataref("laminar/B738/fmod/dh_minimum_copilot2_ra", "number")

B738DR_ap_ils_active			= create_dataref("laminar/B738/ap/ils_active", "number")
B738DR_fac_gp_engaged			= create_dataref("laminar/B738/ap/fac_gp_engaged", "number")
B738DR_loc_gp_engaged			= create_dataref("laminar/B738/ap/loc_gp_engaged", "number")

B738DR_fac_status			= create_dataref("laminar/B738/ap/fac_status", "number")
B738DR_gp_status			= create_dataref("laminar/B738/pfd/gp_status", "number")

--- A/P, A/T light buttons
B738DR_ap_light_pilot		= create_dataref("laminar/B738/push_button/ap_light_pilot", "number")
B738DR_at_light_pilot		= create_dataref("laminar/B738/push_button/at_light_pilot", "number")
B738DR_ap_light_fo			= create_dataref("laminar/B738/push_button/ap_light_fo", "number")
B738DR_at_light_fo			= create_dataref("laminar/B738/push_button/at_light_fo", "number")


B738DR_fdir_pitch_pilot		= create_dataref("laminar/B738/pfd/flight_director_pitch_pilot", "number")
B738DR_fdir_pitch_copilot	= create_dataref("laminar/B738/pfd/flight_director_pitch_copilot", "number")
B738DR_fdir_roll_pilot		= create_dataref("laminar/B738/pfd/flight_director_roll_pilot", "number")
B738DR_fdir_roll_copilot	= create_dataref("laminar/B738/pfd/flight_director_roll_copilot", "number")

B738DR_fdir_pitch_main		= create_dataref("laminar/B738/pfd/flight_director_pitch_main", "number")
B738DR_fdir_roll_main		= create_dataref("laminar/B738/pfd/flight_director_roll_main", "number")

B738DR_fdir_roll_helper		= create_dataref("laminar/B738/fdir_roll_helper", "number")
B738DR_fdir_pitch_helper	= create_dataref("laminar/B738/fdir_pitch_helper", "number")
B738DR_fdir_roll_helper_fo	= create_dataref("laminar/B738/fdir_roll_helper_fo", "number")
B738DR_fdir_pitch_helper_fo	= create_dataref("laminar/B738/fdir_pitch_helper_fo", "number")
B738DR_fd_helper			= find_dataref("laminar/B738/fd_helper")

-- TO/GA BUTTONS
B738DR_autopilot_left_toga_pos	= create_dataref("laminar/B738/autopilot/left_toga_pos", "number")
B738DR_autopilot_right_toga_pos	= create_dataref("laminar/B738/autopilot/right_toga_pos", "number")

-- A/T DISENGAGE BUTTONS
B738DR_autopilot_left_at_diseng_pos	= create_dataref("laminar/B738/autopilot/left_at_diseng_pos", "number")
B738DR_autopilot_right_at_diseng_pos	= create_dataref("laminar/B738/autopilot/right_at_diseng_pos", "number")

-- OTHERS
B738DR_mic_pos				= create_dataref("laminar/B738/push_button/mic_pos", "number")
B738DR_mic_fo_pos			= create_dataref("laminar/B738/push_button/mic_fo_pos", "number")


B738DR_mcp_hdg_dial_nd			= create_dataref("laminar/B738/nd/mcp_hdg_dial", "number")
B738DR_mcp_hdg_dial_nd_show		= create_dataref("laminar/B738/nd/mcp_hdg_dial_show", "number")
B738DR_nd_crs					= create_dataref("laminar/B738/nd/crs_mag", "number")

B738DR_hdg_mag_nd				= create_dataref("laminar/B738/nd/hdg_mag", "number")
B738DR_track_nd					= create_dataref("laminar/B738/nd/track_nd", "number")
B738DR_hdg_bug_nd				= create_dataref("laminar/B738/nd/hdg_bug_nd", "number")

B738DR_mcp_hdg_dial_nd_fo			= create_dataref("laminar/B738/nd/mcp_hdg_dial_fo", "number")
B738DR_mcp_hdg_dial_nd_fo_show		= create_dataref("laminar/B738/nd/mcp_hdg_dial_fo_show", "number")
B738DR_nd_crs_fo					= create_dataref("laminar/B738/nd/crs_mag_fo", "number")

B738DR_hdg_mag_nd_fo				= create_dataref("laminar/B738/nd/hdg_mag_fo", "number")
B738DR_track_nd_fo					= create_dataref("laminar/B738/nd/track_nd_fo", "number")
B738DR_hdg_bug_nd_fo				= create_dataref("laminar/B738/nd/hdg_bug_nd_fo", "number")

B7378DR_hdg_line_enable				= create_dataref("laminar/B738/nd/hdg_bug_line", "number")
B7378DR_hdg_line_fo_enable			= create_dataref("laminar/B738/nd/hdg_bug_line_fo", "number")
B738DR_track_pfd					= create_dataref("laminar/B738/pfd/track_line", "number")
B738DR_hdg_bug_pfd					= create_dataref("laminar/B738/pfd/hdg_bug", "number")

B738DR_gps_horizont				= create_dataref("laminar/B738/autopilot/gps_horizont", "number")
B738DR_fac_horizont				= create_dataref("laminar/B738/autopilot/fac_horizont", "number")

B738DR_track_up_active		= create_dataref("laminar/B738/fms/track_up_active", "number")

B738DR_hdg_bug_tape					= create_dataref("laminar/B738/hud/hdg_bug_tape", "number")
B738DR_gnd_trk_tape					= create_dataref("laminar/B738/hud/gnd_trk_tape", "number")
B738DR_fpv_horiz					= create_dataref("laminar/B738/pfd/fpv_horiz", "number")
B738DR_fpv_horiz_fo					= create_dataref("laminar/B738/pfd/fpv_horiz_fo", "number")
B738DR_fpv_vert						= create_dataref("laminar/B738/pfd/fpv_vert", "number")
B738DR_fpv_vert_fo					= create_dataref("laminar/B738/pfd/fpv_vert_fo", "number")
B738DR_fpv_vert2					= create_dataref("laminar/B738/pfd/fpv_vert2", "number")
simDR_alpha							= find_dataref("sim/flightmodel/position/alpha")
simDR_vpath							= find_dataref("sim/flightmodel/position/vpath")

-- RADIOS
-- NAV1
simDR_nav1_flag_gs			= find_dataref("sim/cockpit2/radios/indicators/nav1_flag_glideslope")
simDR_nav1_vert_dsp			= find_dataref("sim/cockpit2/radios/indicators/nav1_display_vertical")
simDR_nav1_horz_dsp			= find_dataref("sim/cockpit2/radios/indicators/nav1_display_horizontal")
simDR_nav1_flag_ft			= find_dataref("sim/cockpit2/radios/indicators/nav1_flag_from_to_pilot")
simDR_nav1_nav_id			= find_dataref("sim/cockpit2/radios/indicators/nav1_nav_id")
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_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_nav1_type				= find_dataref("sim/cockpit2/radios/indicators/nav1_type")
simDR_nav1_freq				= find_dataref("sim/cockpit2/radios/actuators/nav1_frequency_hz")

-- NAV2
simDR_nav2_flag_gs			= find_dataref("sim/cockpit2/radios/indicators/nav2_flag_glideslope")
simDR_nav2_vert_dsp			= find_dataref("sim/cockpit2/radios/indicators/nav2_display_vertical")
simDR_nav2_horz_dsp			= find_dataref("sim/cockpit2/radios/indicators/nav2_display_horizontal")
simDR_nav2_flag_ft			= find_dataref("sim/cockpit2/radios/indicators/nav2_flag_from_to_pilot")
simDR_nav2_nav_id			= find_dataref("sim/cockpit2/radios/indicators/nav2_nav_id")
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_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_nav2_type				= find_dataref("sim/cockpit2/radios/indicators/nav2_type")
simDR_nav2_freq				= find_dataref("sim/cockpit2/radios/actuators/nav2_frequency_hz")

B738DR_vor_radial			= find_dataref("laminar/B738/fms/vor_radial")

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

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

B738DR_nav_tuned_no_id		= find_dataref("laminar/radios/pilot/nav_tuned_no_id")
B738DR_nav_tuned_no_id_fo	= find_dataref("laminar/radios/copilot/nav_tuned_no_id_fo")

B738DR_tuned_nav1_decode	= find_dataref("laminar/B738/nav/nav1_decode")
B738DR_tuned_nav2_decode	= find_dataref("laminar/B738/nav/nav2_decode")

function hadl_p_act() end
B738DR_fdir_pitch_active	= create_dataref("laminar/autopilot/fdir_pitch_active", "number", hadl_p_act)
B738DR_fdir_pitch_override	= create_dataref("laminar/autopilot/fdir_pitch_override", "number")

function hadl_r_act() end
B738DR_fdir_roll_active	= create_dataref("laminar/autopilot/fdir_roll_active", "number", hadl_r_act)


B738DR_alt_hold_mem 		= create_dataref("laminar/autopilot/alt_hold_mem", "number")

B738DR_shake_on_cpt				= create_dataref("laminar/autopilot/yoke_shake_cpt", "number")
B738DR_shake_on_fo					= create_dataref("laminar/autopilot/yoke_shake_fo", "number")

B738DR_ap_on					= find_dataref("laminar/autopilot/ap_on")
B738DR_ap_side				= create_dataref("laminar/autopilot/ap_side", "number")
B738DR_fd_side				= create_dataref("laminar/autopilot/fd_side", "number")
B738DR_ap_vvi_dial			= find_dataref("laminar/autopilot/ap_vvi_dial")

B738DR_fd_cam_disable		= find_dataref("laminar/B738/fd_cam_disable")
B738DR_ife_pass_seat_pos 	= find_dataref("laminar/B738/toggle_switch/ife_pass_seat_pos")
B738DR_fd_cam_pos			= create_dataref("laminar/B738/fd_cam_pos", "number")
B738DR_fd_cam_push_pos		= create_dataref("laminar/B738/fd_cam_push_pos", "number")
function DR_fd_cam_hadler() end
B738DR_fd_cam				= create_dataref("laminar/B738/fd_cam", "number", DR_fd_cam_hadler)

B738DR_at_fms_warn1						= create_dataref("laminar/B738/annunciator/at_fms_warn1", "number")
B738DR_at_fms_warn2						= create_dataref("laminar/B738/annunciator/at_fms_warn2", "number")
B738DR_ap_warn1							= create_dataref("laminar/B738/annunciator/ap_warn1", "number")
B738DR_ap_warn2							= create_dataref("laminar/B738/annunciator/ap_warn2", "number")
B738DR_ap_disconnect1_test_switch_pos	= find_dataref("laminar/B738/toggle_switch/ap_discon_test1")
B738DR_ap_disconnect2_test_switch_pos	= find_dataref("laminar/B738/toggle_switch/ap_discon_test2")
B738DR_brightness_level					= find_dataref("laminar/B738/annunciator/brightness_level")

B738DR_outflow_valve		= find_dataref("laminar/B738/outflow_valve")
B738DR_outflow_valve_dig	= create_dataref("laminar/B738/outflow_valve_dig", "number")

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

function B738DR_fmc_speed_DRhandler()end
function B738DR_fmc_mode_DRhandler()end
function B738DR_fmc_descent_now_DRhandler()end
function B738DR_was_on_cruise_DRhandler()end

-- 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

function B738DR_thrust_vvi_1_DRhandler()end
function B738DR_thrust_vvi_2_DRhandler()end
function B738DR_thrust_ratio_1_DRhandler()end
function B738DR_thrust_ratio_2_DRhandler()end
function B738DR_flare_ratio_DRhandler()end
function B738DR_pitch_last_DRhandler()end
function B738DR_pitch_ratio_DRhandler()end
function B738DR_pitch_offset_DRhandler()end
function B738DR_vvi_last_DRhandler()end
function B738DR_flare_offset_DRhandler()end

function B738DR_capt_map_mode_DRhandler()end
function B738DR_fo_map_mode_DRhandler()end

function B738DR_capt_exp_map_mode_DRhandler()end
function B738DR_fo_exp_map_mode_DRhandler()end

function B738DR_efis_map_range_capt_DRhandler()end
function B738DR_efis_map_range_fo_DRhandler()end

function B738DR_minim_fo_DRhandler() end
function B738DR_minim_capt_DRhandler() end
function B738DR_minim_dh_fo_DRhandler() end
function B738DR_minim_dh_capt_DRhandler() end

function B738DR_baro_knob_capt_DRhandler() end
function B738DR_baro_knob_fo_DRhandler() end

function B738DR_minim_capt_show_DRhandler() end
function B738DR_minim_fo_show_DRhandler() end

function B738DR_mcp_alt_dial_DRhandler() end
function B738DR_vnav_alt_mode_DRhandler() end
function B738DR_mcp_hdg_dial_DRhandler() end

function B738DR_test_glare_DRhandler() end

function B738DR_test_test_DRhandler() end
function B738DR_test_test2_DRhandler() end
function B738DR_test_test3_DRhandler() end
function B738DR_test_test4_DRhandler() end
function B738DR_test_test5_DRhandler() end

function B738DR_kill_glareshield_DRhandler() end

function B738DR_yoke_pitch2_DRhandler() end

function B738DR_yoke_capt_ap_disengage_DRhandler() end
function B738DR_yoke_fo_ap_disengage_DRhandler() end
function B738DR_yoke_capt_checklist_DRhandler() end
function B738DR_yoke_fo_checklist_DRhandler() end

function B738DR_minim_fo_pfd_DRhandler() end
function B738DR_minim_capt_pfd_DRhandler() end
function B738DR_minim_dh_fo_pfd_DRhandler() end
function B738DR_minim_dh_capt_pfd_DRhandler() end

function B738DR_eng_thr_lvr1_DRhandler() end
function B738DR_eng_thr_lvr2_DRhandler() end

function B738DR_idx_corr5_DRhandler() end

function B738DR_servo_override_DRhandler() end
function B738DR_servo_pitch_DRhandler() end
function B738DR_servo_roll_DRhandler() end
function B738DR_servo_pitch_elev_DRhandler() end
function B738DR_servo_yaw_DRhandler() end
function B738DR_servo_throttle_DRhandler() end

function B738DR_nose_down_DRhandler() end
--*************************************************************************************--
--** 				       CREATE READ-WRITE CUSTOM DATAREFS                         **--
--*************************************************************************************--
B738DR_at_throttle_hold			= create_dataref("laminar/autopilot/at_throttle_hold", "number")
B738DR_eng1_N1_thrust_trg		= create_dataref("laminar/autopilot/eng1_N1_thrust_trg", "number")
B738DR_eng2_N1_thrust_trg		= create_dataref("laminar/autopilot/eng2_N1_thrust_trg", "number")

B738DR_nose_down				= create_dataref("laminar/b738/fmod/nose_down", "number", B738DR_nose_down_DRhandler)

B738DR_enable_servo				= create_dataref("laminar/B738/autopilot/enable_servo", "number")
B738DR_enable_servo_pitch		= create_dataref("laminar/B738/autopilot/enable_servo_pitch", "number")
B738DR_enable_servo_roll		= create_dataref("laminar/B738/autopilot/enable_servo_roll", "number")
B738DR_enable_servo_yaw			= create_dataref("laminar/B738/autopilot/enable_servo_yaw", "number")
B738DR_enable_servo_elev_pitch	= create_dataref("laminar/B738/autopilot/enable_servo_elev_pitch", "number")
B738DR_enable_servo_throttle	= create_dataref("laminar/B738/autopilot/enable_servo_throttle", "number")

B738DR_servo_override			= create_dataref("laminar/B738/autopilot/servo_override", "number", B738DR_servo_override_DRhandler)
B738DR_servo_pitch				= create_dataref("laminar/B738/autopilot/servo_pitch", "number", B738DR_servo_pitch_DRhandler)
B738DR_servo_roll				= create_dataref("laminar/B738/autopilot/servo_roll", "number", B738DR_servo_roll_DRhandler)
B738DR_servo_pitch_elev			= create_dataref("laminar/B738/autopilot/servo_pitch_elev", "number", B738DR_servo_pitch_elev_DRhandler)
B738DR_servo_yaw				= create_dataref("laminar/B738/autopilot/servo_yaw", "number", B738DR_servo_yaw_DRhandler)
B738DR_servo_throttle			= create_dataref("laminar/B738/autopilot/servo_throttle", "number", B738DR_servo_throttle_DRhandler)

B738DR_idx_corr				= find_dataref("laminar/B738/fms/idx_corr")
B738DR_idx_corr2			= find_dataref("laminar/B738/fms/idx_corr2")
B738DR_idx_corr3			= find_dataref("laminar/B738/fms/idx_corr3")
B738DR_idx_corr4			= find_dataref("laminar/B738/fms/idx_corr4")
B738DR_idx_corr5			= create_dataref("laminar/B738/fms/idx_corr5", "number", B738DR_idx_corr5_DRhandler)

B738DR_yoke_roll				= find_dataref("laminar/yoke/roll")
B738DR_yoke_pitch				= find_dataref("laminar/yoke/pitch")
B738DR_yoke_pitch2			= create_dataref("laminar/yoke/pitch2", "number", B738DR_yoke_pitch2_DRhandler)


B738DR_kill_glareshield	= create_dataref("laminar/B738/perf/kill_glareshield", "number", B738DR_kill_glareshield_DRhandler)


B738DR_eng_thr_lvr1			= create_dataref("laminar/B738/engine/thr_lvr1", "number", B738DR_eng_thr_lvr1_DRhandler)
B738DR_eng_thr_lvr2			= create_dataref("laminar/B738/engine/thr_lvr2", "number", B738DR_eng_thr_lvr2_DRhandler)

B738DR_thrust1_leveler		= find_dataref("laminar/B738/engine/thrust1_leveler")
B738DR_thrust2_leveler		= find_dataref("laminar/B738/engine/thrust2_leveler")
B738DR_thrust12_leveler		= find_dataref("laminar/B738/engine/thrust12_leveler")

B738DR_throttle_override	= find_dataref("laminar/B738/throttle_override")

B738DR_test_test			= create_dataref("laminar/B738/test_test", "number", B738DR_test_test_DRhandler)
B738DR_test_test2			= create_dataref("laminar/B738/test_test2", "number", B738DR_test_test2_DRhandler)
B738DR_test_test3			= create_dataref("laminar/B738/test_test3", "number", B738DR_test_test3_DRhandler)
B738DR_test_test4			= create_dataref("laminar/B738/test_test4", "number", B738DR_test_test4_DRhandler)
B738DR_test_test5			= create_dataref("laminar/B738/test_test5", "number", B738DR_test_test5_DRhandler)

-----

B738DR_yoke_capt_ap_disengage	= create_dataref("laminar/B738/capt/yoke_ap_disengage", "number", B738DR_yoke_capt_ap_disengage_DRhandler)
B738DR_yoke_fo_ap_disengage		= create_dataref("laminar/B738/fo/yoke_ap_disengage", "number", B738DR_yoke_fo_ap_disengage_DRhandler)
B738DR_yoke_capt_checklist		= create_dataref("laminar/B738/capt/yoke_checklist", "number", B738DR_yoke_capt_checklist_DRhandler)
B738DR_yoke_fo_checklist		= create_dataref("laminar/B738/fo/yoke_checklist", "number", B738DR_yoke_fo_checklist_DRhandler)

B738DR_test_glare			= create_dataref("laminar/B738/test_glare", "number", B738DR_test_glare_DRhandler)

B738DR_mcp_hdg_dial			= create_dataref("laminar/B738/autopilot/mcp_hdg_dial", "number", B738DR_mcp_hdg_dial_DRhandler)
B738DR_mcp_alt_dial			= create_dataref("laminar/B738/autopilot/mcp_alt_dial", "number", B738DR_mcp_alt_dial_DRhandler)
B738DR_preselected_alt		= find_dataref("laminar/B738/autopilot/preselected_alt")


B738DR_mcp_alt_dial2			= create_dataref("laminar/B738/autopilot/mcp_alt_dial2", "number")
B738DR_mcp_speed_dial_kts2		= create_dataref("laminar/B738/autopilot/mcp_speed_dial_kts2", "number")
B738DR_mcp_alt_dial2_fo			= create_dataref("laminar/B738/autopilot/mcp_alt_dial2_fo", "number")
B738DR_mcp_speed_dial_kts2_fo	= create_dataref("laminar/B738/autopilot/mcp_speed_dial_kts2_fo", "number")

B738DR_vnav_alt_mode			= create_dataref("laminar/B738/autopilot/vnav_alt_mode", "number", B738DR_vnav_alt_mode_DRhandler)
B738DR_vnav_alt_disable			= find_dataref("laminar/B738/fms/vnav_alt_disable")
B738DR_vnavalt_althld			= find_dataref("laminar/B738/fms/vnavalt_althld")
B738DR_alt_hold_vnav			= find_dataref("laminar/B738/autopilot/alt_hold_vnav")

B738DR_capt_map_mode		= create_dataref("laminar/B738/EFIS_control/capt/map_mode_pos", "number", B738DR_capt_map_mode_DRhandler)
B738DR_fo_map_mode			= create_dataref("laminar/B738/EFIS_control/fo/map_mode_pos", "number", B738DR_fo_map_mode_DRhandler)
B738DR_capt_exp_map_mode	= create_dataref("laminar/B738/EFIS_control/capt/exp_map", "number", B738DR_capt_exp_map_mode_DRhandler)
B738DR_fo_exp_map_mode		= create_dataref("laminar/B738/EFIS_control/fo/exp_map", "number", B738DR_fo_exp_map_mode_DRhandler)

B738DR_capt_vsd_map_mode	= create_dataref("laminar/B738/EFIS_control/capt/vsd_map", "number")
B738DR_fo_vsd_map_mode		= create_dataref("laminar/B738/EFIS_control/fo/vsd_map", "number")

B738DR_efis_map_range_capt 		= create_dataref("laminar/B738/EFIS/capt/map_range", "number", B738DR_efis_map_range_capt_DRhandler)
B738DR_efis_map_range_fo 		= create_dataref("laminar/B738/EFIS/fo/map_range", "number", B738DR_efis_map_range_fo_DRhandler)

B738DR_nd_refresh			= find_dataref("laminar/B738/display/nd_refresh")
B738DR_nd_refresh_fo		= find_dataref("laminar/B738/display/nd_refresh_fo")

B738DR_fmc_speed			= create_dataref("laminar/B738/autopilot/fmc_speed", "number", B738DR_fmc_speed_DRhandler)
B738DR_fmc_mode				= create_dataref("laminar/B738/autopilot/fmc_mode", "number", B738DR_fmc_mode_DRhandler)
B738DR_fmc_descent_now		= create_dataref("laminar/B738/autopilot/fmc_descent_now", "number", B738DR_fmc_descent_now_DRhandler)
B738DR_was_on_cruise		= create_dataref("laminar/B738/autopilot/fmc_was_on_cruise", "number", B738DR_was_on_cruise_DRhandler)

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

-- CLIMB
B738DR_fmc_climb_speed			= find_dataref("laminar/B738/autopilot/fmc_climb_speed")
B738DR_fmc_climb_speed_l		= find_dataref("laminar/B738/autopilot/fmc_climb_speed_l")
B738DR_fmc_climb_speed_mach		= find_dataref("laminar/B738/autopilot/fmc_climb_speed_mach")
B738DR_fmc_climb_r_speed1		= find_dataref("laminar/B738/autopilot/fmc_climb_r_speed1")
B738DR_fmc_climb_r_alt1			= find_dataref("laminar/B738/autopilot/fmc_climb_r_alt1")
B738DR_fmc_climb_r_speed2		= find_dataref("laminar/B738/autopilot/fmc_climb_r_speed2")
B738DR_fmc_climb_r_alt2			= find_dataref("laminar/B738/autopilot/fmc_climb_r_alt2")

-- CRUISE
B738DR_fmc_cruise_speed			= find_dataref("laminar/B738/autopilot/fmc_cruise_speed")
B738DR_fmc_cruise_speed_mach	= find_dataref("laminar/B738/autopilot/fmc_cruise_speed_mach")
B738DR_fmc_cruise_alt			= find_dataref("laminar/B738/autopilot/fmc_cruise_alt")

-- DESCENT
B738DR_fmc_descent_speed		= find_dataref("laminar/B738/autopilot/fmc_descent_speed")
B738DR_fmc_descent_speed_mach	= find_dataref("laminar/B738/autopilot/fmc_descent_speed_mach")
B738DR_fmc_descent_alt			= find_dataref("laminar/B738/autopilot/fmc_descent_alt")
B738DR_fmc_descent_r_speed1		= find_dataref("laminar/B738/autopilot/fmc_descent_r_speed1")
B738DR_fmc_descent_r_alt1		= find_dataref("laminar/B738/autopilot/fmc_descent_r_alt1")
B738DR_fmc_descent_r_speed2		= find_dataref("laminar/B738/autopilot/fmc_descent_r_speed2")
B738DR_fmc_descent_r_alt2		= find_dataref("laminar/B738/autopilot/fmc_descent_r_alt2")

B738DR_fmc_vnav_v2_20			= find_dataref("laminar/B738/autopilot/fmc_vnav_v2_20")

B738DR_fmc_descent_speed_mach_init 	= find_dataref("laminar/B738/autopilot/fmc_descent_speed_mach_init")
B738DR_fmc_descent_speed_init 		= find_dataref("laminar/B738/autopilot/fmc_descent_speed_init")


-- APPROACH
B738DR_fmc_approach_alt			= find_dataref("laminar/B738/autopilot/fmc_approach_alt")

-- FLARE
B738DR_thrust_vvi_1				= create_dataref("laminar/B738/flare/thrust_vvi_1", "number", B738DR_thrust_vvi_1_DRhandler)
B738DR_thrust_vvi_2				= create_dataref("laminar/B738/flare/thrust_vvi_2", "number", B738DR_thrust_vvi_2_DRhandler)
B738DR_thrust_ratio_1			= create_dataref("laminar/B738/flare/thrust_ratio_1", "number", B738DR_thrust_ratio_1_DRhandler)
B738DR_thrust_ratio_2			= create_dataref("laminar/B738/flare/thrust_ratio_2", "number", B738DR_thrust_ratio_2_DRhandler)
B738DR_flare_ratio				= create_dataref("laminar/B738/flare/flare_ratio", "number", B738DR_flare_ratio_DRhandler)
B738DR_pitch_last				= create_dataref("laminar/B738/flare/pitch_last", "number", B738DR_pitch_last_DRhandler)
B738DR_pitch_ratio				= create_dataref("laminar/B738/flare/pitch_ratio", "number", B738DR_pitch_ratio_DRhandler)
B738DR_pitch_offset				= create_dataref("laminar/B738/flare/pitch_offset", "number", B738DR_pitch_offset_DRhandler)
B738DR_vvi_last					= create_dataref("laminar/B738/flare/vvi_last", "number", B738DR_vvi_last_DRhandler)
B738DR_flare_offset				= create_dataref("laminar/B738/flare/flare_offset", "number", B738DR_flare_offset_DRhandler)

B738DR_minim_fo = 				create_dataref("laminar/B738/EFIS_control/fo/minimums", "number", B738DR_minim_fo_DRhandler)
B738DR_minim_capt = 			create_dataref("laminar/B738/EFIS_control/cpt/minimums", "number", B738DR_minim_capt_DRhandler)

B738DR_minim_dh_fo = 				create_dataref("laminar/B738/EFIS_control/fo/minimums_dh", "number", B738DR_minim_dh_fo_DRhandler)
B738DR_minim_dh_capt = 				create_dataref("laminar/B738/EFIS_control/capt/minimums_dh", "number", B738DR_minim_dh_capt_DRhandler)
B738DR_baro_knob_capt = 			create_dataref("laminar/B738/EFIS_control/capt/baro_knob", "number", B738DR_baro_knob_capt_DRhandler)
B738DR_baro_knob_fo = 				create_dataref("laminar/B738/EFIS_control/fo/baro_knob", "number", B738DR_baro_knob_fo_DRhandler)

B738DR_minim_fo_pfd = 				create_dataref("laminar/B738/EFIS_control/fo/minimums_pfd", "number", B738DR_minim_fo_pfd_DRhandler)
B738DR_minim_capt_pfd = 			create_dataref("laminar/B738/EFIS_control/cpt/minimums_pfd", "number", B738DR_minim_capt_pfd_DRhandler)
B738DR_minim_dh_fo_pfd = 			create_dataref("laminar/B738/EFIS_control/fo/minimums_dh_pfd", "number", B738DR_minim_dh_fo_pfd_DRhandler)
B738DR_minim_dh_capt_pfd = 			create_dataref("laminar/B738/EFIS_control/capt/minimums_dh_pfd", "number", B738DR_minim_dh_capt_pfd_DRhandler)
B738DR_minim_capt_show = 			create_dataref("laminar/B738/EFIS_control/cpt/minimums_show", "number", B738DR_minim_capt_show_DRhandler)
B738DR_minim_fo_show = 				create_dataref("laminar/B738/EFIS_control/fo/minimums_show", "number", B738DR_minim_fo_show_DRhandler)

B738DR_baro_min_cpt = 				create_dataref("laminar/B738/EFIS_control/pfd/baro_min_cpt", "number")
B738DR_baro_min_cpt_show = 			create_dataref("laminar/B738/EFIS_control/pfd/baro_min_cpt_show", "number")
B738DR_baro_min_fo = 				create_dataref("laminar/B738/EFIS_control/pfd/baro_min_fo", "number")
B738DR_baro_min_fo_show = 			create_dataref("laminar/B738/EFIS_control/pfd/baro_min_fo_show", "number")

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

-- First Officer MINIMUMS SET SWITCH
function B738_fo_minimums_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_sync_pilot ~= 1 then
			if B738DR_minim_fo == 1 then
				B738DR_minim_fo = 0
				if B738DR_dspl_ctrl_pnl > -1 then
					B738DR_dh_copilot = radio_dh_copilot
					simDR_dh_copilot = B738DR_dh_copilot
				end
				if B738DR_dh_copilot < 0 then
					B738DR_minim_fo_show = 0
				else
					B738DR_minim_fo_show = 1
				end
			end
			if B738DR_dspl_ctrl_pnl == 0 then
				B738DR_minim_fo_pfd = B738DR_minim_fo
			elseif B738DR_dspl_ctrl_pnl == 1 then
				B738DR_minim_capt_pfd = B738DR_minim_fo
				B738DR_minim_fo_pfd = B738DR_minim_fo
				simDR_dh_pilot = simDR_dh_copilot
				B738DR_dh_pilot = B738DR_dh_copilot
				baro_dh_pilot_disable = baro_dh_copilot_disable
				if B738DR_dh_copilot < 0 then
					B738DR_minim_capt_show = 0
				else
					B738DR_minim_capt_show = 1
				end
			end
		end
	end
end

function B738_fo_minimums_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_sync_pilot ~= 1 then
			if B738DR_minim_fo == 0 then
				B738DR_minim_fo = 1
				if B738DR_dspl_ctrl_pnl > -1 then
					B738DR_dh_copilot = baro_dh_copilot
					simDR_dh_copilot = 0
					if simDR_altitude_copilot < baro_dh_copilot then
						baro_dh_copilot_disable = 1
					end
					if B738DR_dh_copilot < -1000 then
						B738DR_minim_fo_show = 0
					else
						B738DR_minim_fo_show = 1
					end
				end
			end
			if B738DR_dspl_ctrl_pnl == 0 then
				B738DR_minim_fo_pfd = B738DR_minim_fo
			elseif B738DR_dspl_ctrl_pnl == 1 then
				B738DR_minim_capt_pfd = B738DR_minim_fo
				B738DR_minim_fo_pfd = B738DR_minim_fo
				simDR_dh_pilot = simDR_dh_copilot
				B738DR_dh_pilot = B738DR_dh_copilot
				baro_dh_pilot_disable = baro_dh_copilot_disable
				if B738DR_dh_copilot < -1000 then
					B738DR_minim_capt_show = 0
				else
					B738DR_minim_capt_show = 1
				end
			end
		end
	end
end

-- Captain MINIMUMS SET SWITCH
function B738_cpt_minimums_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_sync_pilot ~= 2 then
			if B738DR_minim_capt == 1 then
				B738DR_minim_capt = 0
				if B738DR_dspl_ctrl_pnl < 1 then
					B738DR_dh_pilot = radio_dh_pilot
					simDR_dh_pilot = B738DR_dh_pilot
				end
				if B738DR_dh_pilot < 0 then
					B738DR_minim_capt_show = 0
				else
					B738DR_minim_capt_show = 1
				end
			end
			if B738DR_dspl_ctrl_pnl == 0 then
				B738DR_minim_capt_pfd = B738DR_minim_capt
			elseif B738DR_dspl_ctrl_pnl == -1 then
				B738DR_minim_capt_pfd = B738DR_minim_capt
				B738DR_minim_fo_pfd = B738DR_minim_capt
				simDR_dh_copilot = simDR_dh_pilot
				B738DR_dh_copilot = B738DR_dh_pilot
				baro_dh_copilot_disable = baro_dh_pilot_disable
				if B738DR_dh_pilot < 0 then
					B738DR_minim_fo_show = 0
				else
					B738DR_minim_fo_show = 1
				end
			end
		end
	end
end

function B738_cpt_minimums_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_sync_pilot ~= 2 then
			if B738DR_minim_capt == 0 then
				B738DR_minim_capt = 1
				if B738DR_dspl_ctrl_pnl < 1 then
					B738DR_dh_pilot = baro_dh_pilot
					simDR_dh_pilot = 0
					if simDR_altitude_pilot < baro_dh_pilot then
						baro_dh_pilot_disable = 1
					end
					if B738DR_dh_pilot < -1000 then
						B738DR_minim_capt_show = 0
					else
						B738DR_minim_capt_show = 1
					end
				end
			end
			if B738DR_dspl_ctrl_pnl == 0 then
				B738DR_minim_capt_pfd = B738DR_minim_capt
			elseif B738DR_dspl_ctrl_pnl == -1 then
				B738DR_minim_capt_pfd = B738DR_minim_capt
				B738DR_minim_fo_pfd = B738DR_minim_capt
				simDR_dh_copilot = simDR_dh_pilot
				B738DR_dh_copilot = B738DR_dh_pilot
				baro_dh_copilot_disable = baro_dh_pilot_disable
				if B738DR_dh_pilot < -1000 then
					B738DR_minim_fo_show = 0
				else
					B738DR_minim_fo_show = 1
				end
			end
		end
	end
end


function B738_ctrl_panel()
	
	if in_hpa_cnt == 0 then
		in_hpa_cnt = 1
	elseif in_hpa_cnt == 1 then
		in_hpa_cnt = 2
		B738DR_efis_baro_mode_capt = B738DR_baro_in_hpa
		B738DR_efis_baro_mode_capt_pfd = B738DR_baro_in_hpa
		B738DR_efis_baro_mode_fo = B738DR_baro_in_hpa
		B738DR_efis_baro_mode_fo_pfd = B738DR_baro_in_hpa
		
		B738DR_minim_capt = B738DR_min_baro_radio
		B738DR_minim_capt_pfd = B738DR_min_baro_radio
		B738DR_minim_fo = B738DR_min_baro_radio
		B738DR_minim_fo_pfd = B738DR_min_baro_radio
		
		B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_capt_pos
		B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_capt_pos
		B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_fo_pos
		B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_fo_pos
		
		radio_dh_pilot = -1		--100
		radio_dh_copilot = -1	--100
		
		baro_dh_pilot = -1001	--200
		baro_dh_copilot = -1001		--200
		
		radio_dh_pilot_mem = 100	-- -1		--100
		baro_dh_pilot_mem = 200		-- -1001	--200
		
		radio_dh_copilot_mem = 100		-- -1	--100
		baro_dh_copilot_mem = 200		-- -1001	--200
		
		if B738DR_minim_capt == 0 then	--radio DH
			B738DR_dh_pilot = -1
		else
			B738DR_dh_pilot = -1001
		end
		if B738DR_minim_fo == 0 then	--radio DH
			B738DR_dh_copilot = -1
		else
			B738DR_dh_copilot = -1001
		end
		
		B738DR_minim_capt_show = 0
		B738DR_minim_fo_show = 0
	end
	
	if B738DR_dspl_ctrl_pnl ~= dspl_ctrl_pnl_old then
		if B738DR_dspl_ctrl_pnl == -1 then
			-- CTRL PNL both Captain
			if B738DR_sync_pilot == 0 then
				B738DR_minim_fo_pfd = B738DR_minim_capt
				baro_dh_copilot = baro_dh_pilot
				radio_dh_copilot = radio_dh_pilot
				if B738DR_minim_fo_pfd == 0 then
					B738DR_minim_fo_pfd = 1
					B738DR_dh_copilot = baro_dh_copilot
					simDR_dh_copilot = simDR_dh_pilot
					if simDR_altitude_copilot < baro_dh_copilot then
						baro_dh_copilot_disable = 1
					end
				else
					B738DR_minim_fo_pfd = 0
					B738DR_dh_copilot = radio_dh_copilot
					simDR_dh_copilot = B738DR_dh_copilot
				end
				
				B738DR_baro_sel_copilot_show = B738DR_baro_sel_pilot_show
				simDR_barometer_setting_fo = simDR_barometer_setting_capt
				B738DR_baro_set_std_copilot = B738DR_baro_set_std_pilot
				B738DR_baro_sel_in_hg_copilot = B738DR_baro_sel_in_hg_pilot
				baro_sel_co_old = baro_sel_old
				B738DR_minim_fo_show = B738DR_minim_capt_show
				
				B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_capt_pfd
			end
			
			if simDR_int_ver < 120000 then
				B738DR_efis_fo_wxr_on = 0
			else
				B738DR_efis_fo_wxr_on = B738DR_efis_wxr_on
			end
			fo_terr_enable = cpt_terr_enable
			B738DR_efis_fo_vor_on = B738DR_efis_vor_on
			B738DR_efis_fo_fix_on = B738DR_efis_fix_on
			B738DR_efis_fo_apt_on = B738DR_efis_apt_on
			B738DR_efis_data_fo_status = B738DR_efis_data_capt_status
			
			B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_capt_pfd
			B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_capt_pfd
			simDR_vor1_fo = simDR_vor1_capt
			simDR_vor2_fo = simDR_vor2_capt
			
		elseif B738DR_dspl_ctrl_pnl == 1 then
			-- CTRL PNL both First Officer
			if B738DR_sync_pilot == 0 then
				B738DR_minim_capt_pfd = B738DR_minim_fo
				baro_dh_pilot = baro_dh_copilot
				radio_dh_pilot = radio_dh_copilot
				if B738DR_minim_capt_pfd == 0 then
					B738DR_minim_capt_pfd = 1
					B738DR_dh_pilot = baro_dh_pilot
					simDR_dh_pilot = simDR_dh_copilot
					if simDR_altitude_pilot < baro_dh_pilot then
						baro_dh_pilot_disable = 1
					end
				else
					B738DR_minim_capt_pfd = 0
					B738DR_dh_pilot = radio_dh_pilot
					simDR_dh_pilot = B738DR_dh_pilot
				end
				
				B738DR_baro_sel_pilot_show = B738DR_baro_sel_copilot_show
				simDR_barometer_setting_capt = simDR_barometer_setting_fo
				B738DR_baro_set_std_pilot = B738DR_baro_set_std_copilot
				B738DR_baro_sel_in_hg_pilot = B738DR_baro_sel_in_hg_copilot
				baro_sel_old = baro_sel_co_old
				B738DR_minim_capt_show = B738DR_minim_fo_show
				
				B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_fo_pfd
			end
			
			if simDR_int_ver < 120000 then
				B738DR_efis_wxr_on = 0
			else
				B738DR_efis_wxr_on = B738DR_efis_fo_wxr_on
			end
			cpt_terr_enable = fo_terr_enable
			B738DR_efis_vor_on = B738DR_efis_fo_vor_on
			B738DR_efis_fix_on = B738DR_efis_fo_fix_on
			B738DR_efis_apt_on = B738DR_efis_fo_apt_on
			B738DR_efis_data_capt_status = B738DR_efis_data_fo_status
			
			B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_fo_pfd
			B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_fo_pfd
			simDR_vor1_capt = simDR_vor1_fo
			simDR_vor2_capt = simDR_vor2_fo
			
		else
			--CTRL PNL Captain and First Officer
			B738DR_minim_fo_pfd = B738DR_minim_fo
			B738DR_minim_capt_pfd = B738DR_minim_capt
			B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_capt
			B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_fo
			B738DR_baro_sel_in_hg_pilot = B738DR_baro_sel_in_hg_copilot
			B738DR_baro_sel_in_hg_copilot = B738DR_baro_sel_in_hg_pilot
			
			B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_capt_pos
			B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_capt_pos
			B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_fo_pos
			B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_fo_pos
			
			if B738DR_efis_vor1_capt_pfd == 1 then
				simDR_vor1_capt = 2
			else
				simDR_vor1_capt = 1
			end
			if B738DR_efis_vor2_capt_pfd == 1 then
				simDR_vor2_capt = 2
			else
				simDR_vor2_capt = 1
			end
			if B738DR_efis_vor1_fo_pfd == 1 then
				simDR_vor1_fo = 2
			else
				simDR_vor1_fo = 1
			end
			if B738DR_efis_vor2_fo_pfd == 1 then
				simDR_vor2_fo = 2
			else
				simDR_vor2_fo = 1
			end
		end
	end
	dspl_ctrl_pnl_old = B738DR_dspl_ctrl_pnl
	
	if B738DR_sync_pilot == 1 then
		-- captain sync
		B738DR_minim_fo_pfd = B738DR_minim_capt
		baro_dh_copilot = baro_dh_pilot
		radio_dh_copilot = radio_dh_pilot
		B738DR_minim_fo_show = B738DR_minim_capt_show
		
		--knob
		B738DR_minim_fo = B738DR_minim_capt
		
		if B738DR_minim_fo_pfd == 0 then
			B738DR_dh_copilot = radio_dh_copilot
			simDR_dh_copilot = B738DR_dh_copilot
		else
			B738DR_dh_copilot = baro_dh_copilot
			simDR_dh_copilot = simDR_dh_pilot
			if simDR_altitude_copilot < baro_dh_copilot then
				baro_dh_copilot_disable = 1
			end
		end
		
		B738DR_baro_sel_copilot_show = B738DR_baro_sel_pilot_show
		simDR_barometer_setting_fo = simDR_barometer_setting_capt
		B738DR_baro_set_std_copilot = B738DR_baro_set_std_pilot
		B738DR_baro_sel_in_hg_copilot = B738DR_baro_sel_in_hg_pilot
		baro_sel_co_old = baro_sel_old
		B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_capt_pfd
	elseif B738DR_sync_pilot == 2 then
		-- first officier sync
		B738DR_minim_capt_pfd = B738DR_minim_fo
		baro_dh_pilot = baro_dh_copilot
		radio_dh_pilot = radio_dh_copilot
		B738DR_minim_capt_show = B738DR_minim_fo_show
		
		--knob
		B738DR_minim_capt = B738DR_minim_fo
		
		if B738DR_minim_capt_pfd == 0 then
			B738DR_dh_pilot = radio_dh_pilot
			simDR_dh_pilot = B738DR_dh_pilot
		else
			B738DR_dh_pilot = baro_dh_pilot
			simDR_dh_pilot = simDR_dh_copilot
			if simDR_altitude_pilot < baro_dh_pilot then
				baro_dh_pilot_disable = 1
			end
		end
		
		B738DR_baro_sel_pilot_show = B738DR_baro_sel_copilot_show
		simDR_barometer_setting_capt = simDR_barometer_setting_fo
		B738DR_baro_set_std_pilot = B738DR_baro_set_std_copilot
		B738DR_baro_sel_in_hg_pilot = B738DR_baro_sel_in_hg_copilot
		baro_sel_old = baro_sel_co_old
		B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_fo_pfd
	end
	
end

-- CAPTAIN EFIS CONTROLS

function up_dn_dh_pilot(val_hdg, val_mode)
	local dh_value = 0
	local val_hdg_add = val_hdg
	if val_mode == 1 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 360 	--60
	elseif val_mode == 2 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 1700 	--360
	end
	
	dh_value = B738DR_dh_pilot + val_hdg_add
	if B738DR_minim_capt == 0 then
		if B738DR_dh_pilot < 0 then
			-- dh_value = 100 --+ val_hdg_add
			dh_value = radio_dh_pilot_mem + val_hdg_add
		end
		dh_value = math.min(dh_value, 999)
		if B738DR_minim_capt_show == 0 and B738DR_dh_pilot == 0 and dh_value >= 0 then
			dh_value = 0
		end
		if dh_value < 0 then
			B738DR_minim_capt_show = 0
		else
			B738DR_minim_capt_show = 1
		end
		B738DR_dh_pilot = math.max(dh_value, 0)
		radio_dh_pilot = B738DR_dh_pilot
	else
		if B738DR_dh_pilot < -1000 then
			-- dh_value = 200 --+ val_hdg_add
			dh_value = baro_dh_pilot_mem + val_hdg_add
		end
		dh_value = math.min(dh_value, 15000)
		if B738DR_minim_capt_show == 0 and B738DR_dh_pilot == -1000 and dh_value >= -1000 then
			dh_value = -1000
		end
		if dh_value < -1000 then
			B738DR_minim_capt_show = 0
		else
			B738DR_minim_capt_show = 1
		end
		B738DR_dh_pilot = math.max(dh_value, -1000)
		baro_dh_pilot = B738DR_dh_pilot
	end
	if B738DR_dspl_ctrl_pnl == -1 then
		B738DR_dh_copilot = B738DR_dh_pilot
		radio_dh_copilot = radio_dh_pilot
		baro_dh_copilot = baro_dh_pilot
		B738DR_minim_fo_show = B738DR_minim_capt_show
	end
end

function B738_dh_pilot_ctr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_minim_dh_capt = 0
	end
end

function B738_dh_pilot_up_slow_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_minim_dh_capt = 1
			up_dn_dh_pilot(1, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				up_dn_dh_pilot(1, 1)
			end
		end
	else
		B738DR_minim_dh_capt = 1
	end
end

function B738_dh_pilot_up_fast_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 or phase == 1 then
			B738DR_minim_dh_capt = 2
			up_dn_dh_pilot(4, 1)
		end
	else
		B738DR_minim_dh_capt = 2
	end
end

function B738_dh_pilot_dn_slow_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_minim_dh_capt = -1
			up_dn_dh_pilot(-1, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				up_dn_dh_pilot(-1, 1)
			end
		end
	else
		B738DR_minim_dh_capt = -1
	end
end

function B738_dh_pilot_dn_fast_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 or phase == 1 then
			B738DR_minim_dh_capt = -2
			up_dn_dh_pilot(-4, 1)
		end
	else
		B738DR_minim_dh_capt = -2
	end
end

function B738_dh_pilot_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_minim_dh_capt = 1
			up_dn_dh_pilot(1, 0)
		elseif phase == 1 then
			if duration > 3.5 then
				B738DR_minim_dh_capt = 2
				up_dn_dh_pilot(4, 1)
			elseif duration > 0.3 then
				up_dn_dh_pilot(1, 1)
			end
		elseif phase == 2 then
			B738DR_minim_dh_capt = 0
		end
	else
		if phase == 0 then
			B738DR_minim_dh_capt = 1
		elseif phase == 2 then
			B738DR_minim_dh_capt = 0
		end
	end
end

function B738_dh_pilot_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_minim_dh_capt = -1
			up_dn_dh_pilot(-1, 0)
		elseif phase == 1 then
			if duration > 3.5 then
				B738DR_minim_dh_capt = -2
				up_dn_dh_pilot(-4, 1)
			elseif duration > 0.5 then
				up_dn_dh_pilot(-1, 1)
			end
		elseif phase == 2 then
			B738DR_minim_dh_capt = 0
		end
	else
		if phase == 0 then
			B738DR_minim_dh_capt = 1
		elseif phase == 2 then
			B738DR_minim_dh_capt = 0
		end
	end
end



function B738_efis_wxr_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_wxr_capt = 1
			if B738DR_efis_wxr_on == 0 then
				B738DR_efis_wxr_on = 1
				cpt_terr_enable = 0
			else
				B738DR_efis_wxr_on = 0
			end
			if simDR_int_ver < 120000 then
				B738DR_efis_fo_wxr_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_wxr_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			if simDR_int_ver < 120000 then
				if B738DR_efis_wxr_on == 0 then
					B738DR_efis_fo_wxr_on = 0
				elseif B738DR_efis_wxr_on == 1 then
					B738DR_efis_fo_wxr_on = 0
				end
			end
		end
	else
		if phase == 0 then
			B738DR_efis_wxr_capt = 1
		elseif phase == 2 then
			B738DR_efis_wxr_capt = 0
		end
	end
end

function B738_efis_sta_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_sta_capt = 1
			if B738DR_efis_vor_on == 0 then
				B738DR_efis_vor_on = 1
			else
				B738DR_efis_vor_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_sta_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_efis_fo_vor_on = B738DR_efis_vor_on
		end
	else
		if phase == 0 then
			B738DR_efis_sta_capt = 1
		elseif phase == 2 then
			B738DR_efis_sta_capt = 0
		end
	end
end

function B738_efis_wpt_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_wpt_capt = 1
			if B738DR_efis_fix_on == 0 then
				B738DR_efis_fix_on = 1
			else
				B738DR_efis_fix_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_wpt_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_efis_fo_fix_on = B738DR_efis_fix_on
		end
	else
		if phase == 0 then
			B738DR_efis_wpt_capt = 1
		elseif phase == 2 then
			B738DR_efis_wpt_capt = 0
		end
	end
end

function B738_efis_arpt_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_arpt_capt = 1
			if B738DR_efis_apt_on == 0 then
				B738DR_efis_apt_on = 1
			else
				B738DR_efis_apt_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_arpt_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_efis_fo_apt_on = B738DR_efis_apt_on
		end
	else
		if phase == 0 then
			B738DR_efis_arpt_capt = 1
		elseif phase == 2 then
			B738DR_efis_arpt_capt = 0
		end
	end
end

function B738_efis_data_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_data_capt = 1
			B738DR_efis_data_capt_status = 1 - B738DR_efis_data_capt_status
		elseif phase == 2 then
			B738DR_efis_data_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_efis_data_fo_status = B738DR_efis_data_capt_status
		end
	else
		if phase == 0 then
			B738DR_efis_data_capt = 1
		elseif phase == 2 then
			B738DR_efis_data_capt = 0
		end
	end
end

function B738_efis_pos_capt_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_pos_capt = 1
		if cpt_pos_enable == 0 then
			cpt_pos_enable = 1
		else
			cpt_pos_enable = 0
		end
	elseif phase == 2 then
		B738DR_efis_pos_capt = 0
	end
	
end

function B738_efis_terr_capt_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_terr_capt = 1
		if cpt_terr_enable == 0 then
			cpt_terr_enable = 1
			B738DR_efis_wxr_on = 0
		else
			cpt_terr_enable = 0
		end
	elseif phase == 2 then
		B738DR_efis_terr_capt = 0
	end
end

function B738_efis_rst_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_rst_capt = 1
			dh_min_block_pilot = 1
			if B738DR_minim_capt == 0 then	--radio DH
				if radio_dh_pilot < 0 then
					radio_dh_pilot = radio_dh_pilot_mem
					simDR_dh_pilot = radio_dh_pilot
					B738DR_dh_pilot = radio_dh_pilot_mem
					
					baro_dh_pilot = baro_dh_pilot_mem
				else
					radio_dh_pilot_mem = radio_dh_pilot
					radio_dh_pilot = -1
					simDR_dh_pilot = 0
					B738DR_dh_pilot = -1
					
					if baro_dh_pilot > -1001 then
						baro_dh_pilot_mem = baro_dh_pilot
					end
					baro_dh_pilot = -1001
				end
				
				if B738DR_dh_pilot < 0 then
					B738DR_minim_capt_show = 0
				else
					B738DR_minim_capt_show = 1
				end
				
			else	-- baro DH
				if baro_dh_pilot < -1000 then
					baro_dh_pilot = baro_dh_pilot_mem
					B738DR_dh_pilot = baro_dh_pilot_mem
					
					radio_dh_pilot = radio_dh_pilot_mem
				else
					baro_dh_pilot_mem = baro_dh_pilot
					baro_dh_pilot = -1001
					B738DR_dh_pilot = -1001
					
					if radio_dh_pilot >= 0 then
						radio_dh_pilot_mem = radio_dh_pilot
					end
					radio_dh_pilot = -1
				end
				
				
				if B738DR_dh_pilot < -1000 then
					B738DR_minim_capt_show = 0
				else
					B738DR_minim_capt_show = 1
				end
			end
		elseif phase == 2 then
			B738DR_efis_rst_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			baro_dh_copilot = baro_dh_pilot
			radio_dh_copilot = radio_dh_pilot
			B738DR_dh_copilot = B738DR_dh_pilot
			dh_min_block_copilot = dh_min_block_pilot
			simDR_dh_copilot = simDR_dh_pilot
			B738DR_minim_fo_show = B738DR_minim_capt_show
		end
	else
		if phase == 0 then
			B738DR_efis_rst_capt = 1
		elseif phase == 2 then
			B738DR_efis_rst_capt = 0
		end
	end
end

function B738_efis_ctr_capt_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_ctr_capt = 1
		if B738DR_capt_map_mode < 2 then
			-- APP/VOR mode
			if capt_exp_vor_app_mode == 0 then
				capt_exp_vor_app_mode = 1
			else
				capt_exp_vor_app_mode = 0
			end
			B738DR_capt_exp_map_mode = capt_exp_vor_app_mode
			B738DR_capt_vsd_map_mode = 0
		elseif B738DR_capt_map_mode == 2 then
			-- MAP mode
			if capt_vsd_map_mode == 1 then
				capt_vsd_map_mode = 0
				capt_exp_map_mode = 1
			elseif capt_exp_map_mode == 0 then
				capt_vsd_map_mode = 1
			else
				capt_exp_map_mode = 0
			end
			B738DR_capt_exp_map_mode = capt_exp_map_mode
			B738DR_capt_vsd_map_mode = capt_vsd_map_mode
		else
			-- PLN mode
			B738DR_capt_exp_map_mode = 1
			B738DR_capt_vsd_map_mode = 0
		end
		
		if (B738DR_capt_map_mode ~= capt_map_mode_old2) 
		or (B738DR_capt_exp_map_mode ~= capt_exp_map_mode_old2) 
		or (B738DR_capt_vsd_map_mode ~= capt_vsd_map_mode_old2) then
			B738DR_nd_refresh = 0.5
			nd_refreshed = true
		end
		capt_map_mode_old2 = B738DR_capt_map_mode
		capt_exp_map_mode_old2 = B738DR_capt_exp_map_mode
		capt_vsd_map_mode_old2 = B738DR_capt_vsd_map_mode
		capt_range_old2 = B738DR_efis_map_range_capt
	elseif phase == 2 then
		B738DR_efis_ctr_capt = 0
	end
end

function B738_efis_tfc_capt_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_tfc_capt = 1
		if cpt_tcas_enable == 0 then
			cpt_tcas_enable = 1
		else
			cpt_tcas_enable = 0
		end
		
	elseif phase == 2 then
		B738DR_efis_tfc_capt = 0
	end
end

function B738_efis_std_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_std_capt = 1
			if B738DR_baro_set_std_pilot == 0 then
				B738DR_baro_set_std_pilot = 1
				baro_sel_old = B738DR_baro_sel_in_hg_pilot
				simDR_barometer_setting_capt = 29.92
			else
				B738DR_baro_set_std_pilot = 0
				simDR_barometer_setting_capt = B738DR_baro_sel_in_hg_pilot
			end
			B738DR_baro_sel_pilot_show = 0
		elseif phase == 2 then
			B738DR_efis_std_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_baro_sel_copilot_show = B738DR_baro_sel_pilot_show
			simDR_barometer_setting_fo = simDR_barometer_setting_capt
			B738DR_baro_set_std_copilot = B738DR_baro_set_std_pilot
			B738DR_baro_sel_in_hg_copilot = B738DR_baro_sel_in_hg_pilot
			baro_sel_co_old = baro_sel_old
		end
	else
		if phase == 0 then
			B738DR_efis_std_capt = 1
		elseif phase == 2 then
			B738DR_efis_std_capt = 0
		end
	end
end

function B738_efis_mtrs_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_mtrs_capt = 1
			if B738DR_capt_alt_mode_meters == 0 then
				B738DR_capt_alt_mode_meters = 1
			elseif B738DR_capt_alt_mode_meters == 1 then
				B738DR_capt_alt_mode_meters = 0
			end
		elseif phase == 2 then
			B738DR_efis_mtrs_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_fo_alt_mode_meters = B738DR_capt_alt_mode_meters
		end
	else
		if phase == 0 then
			B738DR_efis_mtrs_capt = 1
		elseif phase == 2 then
			B738DR_efis_mtrs_capt = 0
		end
	end
end

function B738_efis_fpv_capt_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_efis_fpv_capt = 1
			if B738DR_capt_fpv_on == 0 then
				B738DR_capt_fpv_on = 1
			elseif B738DR_capt_fpv_on == 1 then
				B738DR_capt_fpv_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_fpv_capt = 0
		end
		if B738DR_dspl_ctrl_pnl == -1 then
			B738DR_fo_fpv_on = B738DR_capt_fpv_on
		end
	else
		if phase == 0 then
			B738DR_efis_fpv_capt = 1
		elseif phase == 2 then
			B738DR_efis_fpv_capt = 0
		end
	end
end

function B738_efis_baro_mode_capt_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			if B738DR_efis_baro_mode_capt == 0 then
				B738DR_efis_baro_mode_capt = 1
			end
			B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_capt
			if B738DR_dspl_ctrl_pnl == -1 then
				B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_capt_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_baro_mode_capt == 0 then
				B738DR_efis_baro_mode_capt = 1
			end
		end
	end
end

function B738_efis_baro_mode_capt_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			if B738DR_efis_baro_mode_capt == 1 then
				B738DR_efis_baro_mode_capt = 0
			end
			B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_capt
			if B738DR_dspl_ctrl_pnl == -1 then
				B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_capt_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_baro_mode_capt == 1 then
				B738DR_efis_baro_mode_capt = 0
			end
		end
	end
end

function B738_efis_vor1_capt_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			if B738DR_efis_vor1_capt_pos == -1 then
				B738DR_efis_vor1_capt_pos = 0
				simDR_vor1_capt = 1
			elseif B738DR_efis_vor1_capt_pos == 0 then
				B738DR_efis_vor1_capt_pos = 1
				simDR_vor1_capt = 2
			end
			B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_capt_pos
			if B738DR_dspl_ctrl_pnl == -1 then
				simDR_vor1_fo = simDR_vor1_capt
				B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_capt_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor1_capt_pos == -1 then
				B738DR_efis_vor1_capt_pos = 0
				simDR_vor1_capt = 1
			elseif B738DR_efis_vor1_capt_pos == 0 then
				B738DR_efis_vor1_capt_pos = 1
				simDR_vor1_capt = 2
			end
		end
	end
end

function B738_efis_vor1_capt_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			if B738DR_efis_vor1_capt_pos == 1 then
				B738DR_efis_vor1_capt_pos = 0
				simDR_vor1_capt = 1
			elseif B738DR_efis_vor1_capt_pos == 0 then
				B738DR_efis_vor1_capt_pos = -1
				simDR_vor1_capt = 1
			end
			B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_capt_pos
			if B738DR_dspl_ctrl_pnl == -1 then
				simDR_vor1_fo = simDR_vor1_capt
				B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_capt_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor1_capt_pos == 1 then
				B738DR_efis_vor1_capt_pos = 0
				simDR_vor1_capt = 1
			elseif B738DR_efis_vor1_capt_pos == 0 then
				B738DR_efis_vor1_capt_pos = -1
				simDR_vor1_capt = 1
			end
		end
	end
end

function B738_efis_vor2_capt_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			if B738DR_efis_vor2_capt_pos == -1 then
				B738DR_efis_vor2_capt_pos = 0
				simDR_vor2_capt = 1
			elseif B738DR_efis_vor2_capt_pos == 0 then
				B738DR_efis_vor2_capt_pos = 1
				simDR_vor2_capt = 2
			end
			B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_capt_pos
			if B738DR_dspl_ctrl_pnl == -1 then
				simDR_vor2_fo = simDR_vor2_capt
				B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_capt_pfd
			end
		end
	else
			if B738DR_efis_vor2_capt_pos == -1 then
				B738DR_efis_vor2_capt_pos = 0
				simDR_vor2_capt = 1
			elseif B738DR_efis_vor2_capt_pos == 0 then
				B738DR_efis_vor2_capt_pos = 1
				simDR_vor2_capt = 2
			end
	end
end
			
function B738_efis_vor2_capt_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			if B738DR_efis_vor2_capt_pos == 1 then
				B738DR_efis_vor2_capt_pos = 0
				simDR_vor2_capt = 1
			elseif B738DR_efis_vor2_capt_pos == 0 then
				B738DR_efis_vor2_capt_pos = -1
				simDR_vor2_capt = 1
			end
			B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_capt_pos
			if B738DR_dspl_ctrl_pnl == -1 then
				simDR_vor2_fo = simDR_vor2_capt
				B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_capt_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor2_capt_pos == 1 then
				B738DR_efis_vor2_capt_pos = 0
				simDR_vor2_capt = 1
			elseif B738DR_efis_vor2_capt_pos == 0 then
				B738DR_efis_vor2_capt_pos = -1
				simDR_vor2_capt = 1
			end
		end
	end
end


function B738_efis_map_range_capt_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_efis_map_range_capt == 0 then
			B738DR_efis_map_range_capt = 1
		elseif B738DR_efis_map_range_capt == 1 then
			B738DR_efis_map_range_capt = 2
		elseif B738DR_efis_map_range_capt == 2 then
			B738DR_efis_map_range_capt = 3
		elseif B738DR_efis_map_range_capt == 3 then
			B738DR_efis_map_range_capt = 4
		elseif B738DR_efis_map_range_capt == 4 then
			B738DR_efis_map_range_capt = 5
		elseif B738DR_efis_map_range_capt == 5 then
			B738DR_efis_map_range_capt = 6
		elseif B738DR_efis_map_range_capt == 6 then
			B738DR_efis_map_range_capt = 7
		end
		if B738DR_capt_map_mode > 1 and B738DR_efis_map_range_capt ~= capt_range_old2 then
			B738DR_nd_refresh = 0.19
			nd_refreshed = true
		end
		capt_range_old2 = B738DR_efis_map_range_capt
	end
end

function B738_efis_map_range_capt_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_efis_map_range_capt == 1 then
			B738DR_efis_map_range_capt = 0
		elseif B738DR_efis_map_range_capt == 2 then
			B738DR_efis_map_range_capt = 1
		elseif B738DR_efis_map_range_capt == 3 then
			B738DR_efis_map_range_capt = 2
		elseif B738DR_efis_map_range_capt == 4 then
			B738DR_efis_map_range_capt = 3
		elseif B738DR_efis_map_range_capt == 5 then
			B738DR_efis_map_range_capt = 4
		elseif B738DR_efis_map_range_capt == 6 then
			B738DR_efis_map_range_capt = 5
		elseif B738DR_efis_map_range_capt == 7 then
			B738DR_efis_map_range_capt = 6
		end
		if B738DR_capt_map_mode > 1 and B738DR_efis_map_range_capt ~= capt_range_old2 then
			B738DR_nd_refresh = 0.19
			nd_refreshed = true
		end
		capt_range_old2 = B738DR_efis_map_range_capt
	end
end

-- FIRST OFFICER EFIS CONTROLS


function up_dn_dh_copilot(val_hdg, val_mode)
	local dh_value = 0
	local val_hdg_add = val_hdg
	if val_mode == 1 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 360 	--60
	elseif val_mode == 2 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 1700 	--360
	end
	
	dh_value = B738DR_dh_copilot + val_hdg_add
	if B738DR_minim_fo == 0 then
		if B738DR_dh_copilot < 0 then
			-- dh_value = 100 --+ val_hdg_add
			dh_value = radio_dh_copilot_mem + val_hdg_add
		end
		dh_value = math.min(dh_value, 999)
		if B738DR_minim_fo_show == 0 and B738DR_dh_copilot == 0 and dh_value >= 0 then
			dh_value = 0
		end
		if dh_value < 0 then
			B738DR_minim_fo_show = 0
		else
			B738DR_minim_fo_show = 1
		end
		B738DR_dh_copilot = math.max(dh_value, 0)
		radio_dh_copilot = B738DR_dh_copilot
	else
		if B738DR_dh_copilot < -1000 then
			-- dh_value = 200 --+ val_hdg_add
			dh_value = baro_dh_copilot_mem + val_hdg_add
		end
		dh_value = math.min(dh_value, 15000)
		if B738DR_minim_fo_show == 0 and B738DR_dh_copilot == -1000 and dh_value >= -1000 then
			dh_value = -1000
		end
		if dh_value < -1000 then
			B738DR_minim_fo_show = 0
		else
			B738DR_minim_fo_show = 1
		end
		B738DR_dh_copilot = math.max(dh_value, -1000)
		baro_dh_copilot = B738DR_dh_copilot
	end
	if B738DR_dspl_ctrl_pnl == 1 then
		B738DR_dh_pilot = B738DR_dh_copilot
		radio_dh_pilot = radio_dh_copilot
		baro_dh_pilot = baro_dh_copilot
		B738DR_minim_capt_show = B738DR_minim_fo_show
	end
end

function B738_dh_copilot_ctr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_minim_dh_fo = 0
	end
end

function B738_dh_copilot_up_slow_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_minim_dh_fo = 1
			up_dn_dh_copilot(1, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				up_dn_dh_copilot(1, 1)
			end
		end
	else
		B738DR_minim_dh_fo = 1
	end
end

function B738_dh_copilot_up_fast_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 or phase == 1 then
			B738DR_minim_dh_fo = 2
			up_dn_dh_copilot(4, 1)
		end
	else
		B738DR_minim_dh_fo = 2
	end
end

function B738_dh_copilot_dn_slow_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_minim_dh_fo = -1
			up_dn_dh_copilot(-1, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				up_dn_dh_copilot(-1, 1)
			end
		end
	else
		B738DR_minim_dh_fo = -1
	end
end

function B738_dh_copilot_dn_fast_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 or phase == 1 then
			B738DR_minim_dh_fo = -2
			up_dn_dh_copilot(-4, 1)
		end
	else
		B738DR_minim_dh_fo = -2
	end
end


function B738_dh_copilot_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_minim_dh_fo = 1
			up_dn_dh_copilot(1, 0)
		elseif phase == 1 then
			if duration > 3.5 then
				B738DR_minim_dh_fo = 2
				up_dn_dh_copilot(4, 1)
			elseif duration > 0.3 then
				up_dn_dh_copilot(1, 1)
			end
		elseif phase == 2 then
			B738DR_minim_dh_fo = 0
		end
	else
		if phase == 0 then
			B738DR_minim_dh_fo = 1
		elseif phase == 2 then
			B738DR_minim_dh_fo = 0
		end
	end
end

function B738_dh_copilot_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_minim_dh_fo = -1
			up_dn_dh_copilot(-1, 0)
		elseif phase == 1 then
			if duration > 3.5 then
				B738DR_minim_dh_fo = -2
				up_dn_dh_copilot(-4, 1)
			elseif duration > 0.3 then
				up_dn_dh_copilot(-1, 1)
			end
		elseif phase == 2 then
			B738DR_minim_dh_fo = 0
		end
	else
		if phase == 0 then
			B738DR_minim_dh_fo = 1
		elseif phase == 2 then
			B738DR_minim_dh_fo = 0
		end
	end
end

function B738_efis_wxr_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_wxr_fo = 1
			if B738DR_efis_fo_wxr_on == 0 then
				B738DR_efis_fo_wxr_on = 1
				fo_terr_enable = 0
			else
				B738DR_efis_fo_wxr_on = 0
			end
			if simDR_int_ver < 120000 then
				B738DR_efis_wxr_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_wxr_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			if simDR_int_ver < 120000 then
				if B738DR_efis_fo_wxr_on == 0 then
					B738DR_efis_wxr_on = 0
				elseif B738DR_efis_fo_wxr_on == 1 then
					B738DR_efis_wxr_on = 0
				end
			end
		end
	else
		if phase == 0 then
			B738DR_efis_wxr_fo = 1
		elseif phase == 2 then
			B738DR_efis_wxr_fo = 0
		end
	end
end

function B738_efis_sta_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_sta_fo = 1
			if B738DR_efis_fo_vor_on == 0 then
				B738DR_efis_fo_vor_on = 1
			else
				B738DR_efis_fo_vor_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_sta_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_efis_vor_on = B738DR_efis_fo_vor_on
		end
	else
		if phase == 0 then
			B738DR_efis_sta_fo = 1
		elseif phase == 2 then
			B738DR_efis_sta_fo = 0
		end
	end
end

function B738_efis_wpt_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_wpt_fo = 1
			if B738DR_efis_fo_fix_on == 0 then
				B738DR_efis_fo_fix_on = 1
			else
				B738DR_efis_fo_fix_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_wpt_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_efis_fix_on = B738DR_efis_fo_fix_on
		end
	else
		if phase == 0 then
			B738DR_efis_wpt_fo = 1
		elseif phase == 2 then
			B738DR_efis_wpt_fo = 0
		end
	end
end

function B738_efis_arpt_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_arpt_fo = 1
			if B738DR_efis_fo_apt_on == 0 then
				B738DR_efis_fo_apt_on = 1
			else
				B738DR_efis_fo_apt_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_arpt_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_efis_apt_on = B738DR_efis_fo_apt_on
		end
	else
		if phase == 0 then
			B738DR_efis_arpt_fo = 1
		elseif phase == 2 then
			B738DR_efis_arpt_fo = 0
		end
	end
end

function B738_efis_data_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_data_fo = 1
			B738DR_efis_data_fo_status = 1 - B738DR_efis_data_fo_status
		elseif phase == 2 then
			B738DR_efis_data_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_efis_data_capt_status = B738DR_efis_data_fo_status
		end
	else
		if phase == 0 then
			B738DR_efis_data_fo = 1
		elseif phase == 2 then
			B738DR_efis_data_fo = 0
		end
	end
end

function B738_efis_pos_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_pos_fo = 1
		if fo_pos_enable == 0 then
			fo_pos_enable = 1
		else
			fo_pos_enable = 0
		end
	elseif phase == 2 then
		B738DR_efis_pos_fo = 0
	end
end

function B738_efis_terr_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_terr_fo = 1
		if fo_terr_enable == 0 then
			fo_terr_enable = 1
			B738DR_efis_fo_wxr_on = 0
		else
			fo_terr_enable = 0
		end
	elseif phase == 2 then
		B738DR_efis_terr_fo = 0
	end
end

function B738_efis_rst_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_rst_fo = 1
			dh_min_block_copilot = 1
			if B738DR_minim_fo == 0 then	--radio DH
				if radio_dh_copilot < 0 then
					radio_dh_copilot = radio_dh_copilot_mem
					simDR_dh_copilot = radio_dh_copilot
					B738DR_dh_copilot = radio_dh_copilot_mem
					
					baro_dh_copilot = baro_dh_copilot_mem
				else
					radio_dh_copilot_mem = radio_dh_copilot
					radio_dh_copilot = -1
					simDR_dh_copilot = 0
					B738DR_dh_copilot = -1
					
					if baro_dh_copilot > -1000 then
						baro_dh_copilot_mem = baro_dh_copilot
					end
					baro_dh_copilot = -1001
				end
				
				if B738DR_dh_copilot < 0 then
					B738DR_minim_fo_show = 0
				else
					B738DR_minim_fo_show = 1
				end
			else	-- baro DH
				if baro_dh_copilot < -1000 then
					baro_dh_copilot = baro_dh_copilot_mem
					B738DR_dh_copilot = baro_dh_copilot_mem
					
					radio_dh_copilot = radio_dh_copilot_mem
				else
					baro_dh_copilot_mem = baro_dh_copilot
					baro_dh_copilot = -1001
					B738DR_dh_copilot = -1001
					
					if radio_dh_copilot >= 0 then
						radio_dh_copilot_mem = radio_dh_copilot
					end
					radio_dh_copilot = -1
				end
				
				if B738DR_dh_copilot < -1000 then
					B738DR_minim_fo_show = 0
				else
					B738DR_minim_fo_show = 1
				end
			end
		elseif phase == 2 then
			B738DR_efis_rst_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			baro_dh_pilot = baro_dh_copilot
			radio_dh_pilot = radio_dh_copilot
			B738DR_dh_pilot = B738DR_dh_copilot
			dh_min_block_pilot = dh_min_block_copilot
			simDR_dh_pilot = simDR_dh_copilot
			B738DR_minim_capt_show = B738DR_minim_fo_show
		end
	else
		if phase == 0 then
			B738DR_efis_rst_fo = 1
		elseif phase == 2 then
			B738DR_efis_rst_fo = 0
		end
	end
end

function B738_efis_ctr_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_ctr_fo = 1
		
		if B738DR_fo_map_mode < 2 then
			-- APP/VOR mode
			if fo_exp_vor_app_mode == 0 then
				fo_exp_vor_app_mode = 1
			else
				fo_exp_vor_app_mode = 0
			end
			B738DR_fo_exp_map_mode = fo_exp_vor_app_mode
			B738DR_fo_vsd_map_mode = 0
		elseif B738DR_fo_map_mode == 2 then
			-- MAP mode
			if fo_vsd_map_mode == 1 then
				fo_vsd_map_mode = 0
				fo_exp_map_mode = 1
			elseif fo_exp_map_mode == 0 then
				fo_vsd_map_mode = 1
			else
				fo_exp_map_mode = 0
			end
			B738DR_fo_exp_map_mode = fo_exp_map_mode
			B738DR_fo_vsd_map_mode = fo_vsd_map_mode
		else
			-- PLN mode
			B738DR_fo_exp_map_mode = 1
			B738DR_fo_vsd_map_mode = 0
		end
		
		if (B738DR_fo_map_mode ~= fo_map_mode_old2) 
		or (B738DR_fo_exp_map_mode ~= fo_exp_map_mode_old2) 
		or (B738DR_fo_vsd_map_mode ~= fo_vsd_map_mode_old2) then
			B738DR_nd_refresh_fo = 0.5
			nd_refreshed_fo = true
		end
		fo_map_mode_old2 = B738DR_fo_map_mode
		fo_exp_map_mode_old2 = B738DR_fo_exp_map_mode
		fo_vsd_map_mode_old2 = B738DR_fo_vsd_map_mode
		fo_range_old2 = B738DR_efis_map_range_fo
	elseif phase == 2 then
		B738DR_efis_ctr_fo = 0
	end
end

function B738_efis_tfc_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_efis_tfc_fo = 1
		if fo_tcas_enable == 0 then
			fo_tcas_enable = 1
		else
			fo_tcas_enable = 0
		end
	elseif phase == 2 then
		B738DR_efis_tfc_fo = 0
	end
end

function B738_efis_std_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_std_fo = 1
			if B738DR_baro_set_std_copilot == 0 then
				B738DR_baro_set_std_copilot = 1
				baro_sel_co_old = B738DR_baro_sel_in_hg_copilot
				simDR_barometer_setting_fo = 29.92
			else
				B738DR_baro_set_std_copilot = 0
				simDR_barometer_setting_fo = B738DR_baro_sel_in_hg_copilot
			end
			B738DR_baro_sel_copilot_show = 0
		elseif phase == 2 then
			B738DR_efis_std_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_baro_sel_pilot_show = B738DR_baro_sel_copilot_show
			simDR_barometer_setting_capt = simDR_barometer_setting_fo
			B738DR_baro_set_std_pilot = B738DR_baro_set_std_copilot
			B738DR_baro_sel_in_hg_pilot = B738DR_baro_sel_in_hg_copilot
			baro_sel_old = baro_sel_co_old
		end
	else
		if phase == 0 then
			B738DR_efis_std_fo = 1
		elseif phase == 2 then
			B738DR_efis_std_fo = 0
		end
	end
end

function B738_efis_mtrs_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_mtrs_fo = 1
			if B738DR_fo_alt_mode_meters == 0 then
				B738DR_fo_alt_mode_meters = 1
			elseif B738DR_fo_alt_mode_meters == 1 then
				B738DR_fo_alt_mode_meters = 0
			end
		elseif phase == 2 then
			B738DR_efis_mtrs_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_capt_alt_mode_meters = B738DR_fo_alt_mode_meters
		end
	else
		if phase == 0 then
			B738DR_efis_mtrs_fo = 1
		elseif phase == 2 then
			B738DR_efis_mtrs_fo = 0
		end
	end
end

function B738_efis_fpv_fo_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_efis_fpv_fo = 1
			if B738DR_fo_fpv_on == 0 then
				B738DR_fo_fpv_on = 1
			elseif B738DR_fo_fpv_on == 1 then
				B738DR_fo_fpv_on = 0
			end
		elseif phase == 2 then
			B738DR_efis_fpv_fo = 0
		end
		if B738DR_dspl_ctrl_pnl == 1 then
			B738DR_capt_fpv_on = B738DR_fo_fpv_on
		end
	else
		if phase == 0 then
			B738DR_efis_fpv_fo = 1
		elseif phase == 2 then
			B738DR_efis_fpv_fo = 0
		end
	end
end

function B738_efis_baro_mode_fo_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			if B738DR_efis_baro_mode_fo == 0 then
				B738DR_efis_baro_mode_fo = 1
			end
			B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_fo
			if B738DR_dspl_ctrl_pnl == 1 then
				B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_fo_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_baro_mode_fo == 0 then
				B738DR_efis_baro_mode_fo = 1
			end
		end
	end
end

function B738_efis_baro_mode_fo_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			if B738DR_efis_baro_mode_fo == 1 then
				B738DR_efis_baro_mode_fo = 0
			end
			B738DR_efis_baro_mode_fo_pfd = B738DR_efis_baro_mode_fo
			if B738DR_dspl_ctrl_pnl == 1 then
				B738DR_efis_baro_mode_capt_pfd = B738DR_efis_baro_mode_fo_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_baro_mode_fo == 1 then
				B738DR_efis_baro_mode_fo = 0
			end
		end
	end
end

function B738_efis_vor1_fo_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			if B738DR_efis_vor1_fo_pos == -1 then
				B738DR_efis_vor1_fo_pos = 0
				simDR_vor1_fo = 1
			elseif B738DR_efis_vor1_fo_pos == 0 then
				B738DR_efis_vor1_fo_pos = 1
				simDR_vor1_fo = 2
			end
			B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_fo_pos
			if B738DR_dspl_ctrl_pnl == 1 then
				simDR_vor1_capt = simDR_vor1_fo
				B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_fo_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor1_fo_pos == -1 then
				B738DR_efis_vor1_fo_pos = 0
			elseif B738DR_efis_vor1_fo_pos == 0 then
				B738DR_efis_vor1_fo_pos = 1
			end
		end
	end
end

function B738_efis_vor1_fo_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			if B738DR_efis_vor1_fo_pos == 1 then
				B738DR_efis_vor1_fo_pos = 0
				simDR_vor1_fo = 1
			elseif B738DR_efis_vor1_fo_pos == 0 then
				B738DR_efis_vor1_fo_pos = -1
				simDR_vor1_fo = 1
			end
			B738DR_efis_vor1_fo_pfd = B738DR_efis_vor1_fo_pos
			if B738DR_dspl_ctrl_pnl == 1 then
				simDR_vor1_capt = simDR_vor1_fo
				B738DR_efis_vor1_capt_pfd = B738DR_efis_vor1_fo_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor1_fo_pos == 1 then
				B738DR_efis_vor1_fo_pos = 0
			elseif B738DR_efis_vor1_fo_pos == 0 then
				B738DR_efis_vor1_fo_pos = -1
			end
		end
	end
end

function B738_efis_vor2_fo_up_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			if B738DR_efis_vor2_fo_pos == -1 then
				B738DR_efis_vor2_fo_pos = 0
				simDR_vor2_fo = 1
			elseif B738DR_efis_vor2_fo_pos == 0 then
				B738DR_efis_vor2_fo_pos = 1
				simDR_vor2_fo = 2
			end
			B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_fo_pos
			if B738DR_dspl_ctrl_pnl == 1 then
				simDR_vor2_capt = simDR_vor2_fo
				B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_fo_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor2_fo_pos == -1 then
				B738DR_efis_vor2_fo_pos = 0
			elseif B738DR_efis_vor2_fo_pos == 0 then
				B738DR_efis_vor2_fo_pos = 1
			end
		end
	end
end
			
function B738_efis_vor2_fo_dn_CMDhandler(phase, duration)
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			if B738DR_efis_vor2_fo_pos == 1 then
				B738DR_efis_vor2_fo_pos = 0
				simDR_vor2_fo = 1
			elseif B738DR_efis_vor2_fo_pos == 0 then
				B738DR_efis_vor2_fo_pos = -1
				simDR_vor2_fo = 1
			end
			B738DR_efis_vor2_fo_pfd = B738DR_efis_vor2_fo_pos
			if B738DR_dspl_ctrl_pnl == 1 then
				simDR_vor2_capt = simDR_vor2_fo
				B738DR_efis_vor2_capt_pfd = B738DR_efis_vor2_fo_pfd
			end
		end
	else
		if phase == 0 then
			if B738DR_efis_vor2_fo_pos == 1 then
				B738DR_efis_vor2_fo_pos = 0
			elseif B738DR_efis_vor2_fo_pos == 0 then
				B738DR_efis_vor2_fo_pos = -1
			end
		end
	end
end



function B738_efis_map_mode_capt_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_capt_map_mode == 0 then
			B738DR_capt_map_mode = 1
		elseif B738DR_capt_map_mode == 1 then
			B738DR_capt_map_mode = 2
		elseif B738DR_capt_map_mode == 2 then
			B738DR_capt_map_mode = 3
		end
		
		if (B738DR_capt_map_mode ~= capt_map_mode_old2) 
		or (B738DR_capt_exp_map_mode ~= capt_exp_map_mode_old2) 
		or (B738DR_capt_vsd_map_mode ~= capt_vsd_map_mode_old2) then
			B738DR_nd_refresh = 0.5
			nd_refreshed = true
		end
		capt_map_mode_old2 = B738DR_capt_map_mode
		capt_exp_map_mode_old2 = B738DR_capt_exp_map_mode
		capt_vsd_map_mode_old2 = B738DR_capt_vsd_map_mode
		capt_range_old2 = B738DR_efis_map_range_capt
	end
end

function B738_efis_map_mode_capt_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_capt_map_mode == 1 then
			B738DR_capt_map_mode = 0
		elseif B738DR_capt_map_mode == 2 then
			B738DR_capt_map_mode = 1
		elseif B738DR_capt_map_mode == 3 then
			B738DR_capt_map_mode = 2
		end
		
		if (B738DR_capt_map_mode ~= capt_map_mode_old2) 
		or (B738DR_capt_exp_map_mode ~= capt_exp_map_mode_old2) 
		or (B738DR_capt_vsd_map_mode ~= capt_vsd_map_mode_old2) then
			B738DR_nd_refresh = 0.5
			nd_refreshed = true
		end
		capt_map_mode_old2 = B738DR_capt_map_mode
		capt_exp_map_mode_old2 = B738DR_capt_exp_map_mode
		capt_vsd_map_mode_old2 = B738DR_capt_vsd_map_mode
		capt_range_old2 = B738DR_efis_map_range_capt
	end
end

function B738_efis_map_mode_fo_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_fo_map_mode == 0 then
			B738DR_fo_map_mode = 1
		elseif B738DR_fo_map_mode == 1 then
			B738DR_fo_map_mode = 2
		elseif B738DR_fo_map_mode == 2 then
			B738DR_fo_map_mode = 3
		end
		
		if (B738DR_fo_map_mode ~= fo_map_mode_old2) 
		or (B738DR_fo_exp_map_mode ~= fo_exp_map_mode_old2) 
		or (B738DR_fo_vsd_map_mode ~= fo_vsd_map_mode_old2) then
			B738DR_nd_refresh_fo = 0.5
			nd_refreshed_fo = true
		end
		fo_map_mode_old2 = B738DR_fo_map_mode
		fo_exp_map_mode_old2 = B738DR_fo_exp_map_mode
		fo_vsd_map_mode_old2 = B738DR_fo_vsd_map_mode
		fo_range_old2 = B738DR_efis_map_range_fo
	end
end

function B738_efis_map_mode_fo_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_fo_map_mode == 1 then
			B738DR_fo_map_mode = 0
		elseif B738DR_fo_map_mode == 2 then
			B738DR_fo_map_mode = 1
		elseif B738DR_fo_map_mode == 3 then
			B738DR_fo_map_mode = 2
		end
		
		if (B738DR_fo_map_mode ~= fo_map_mode_old2) 
		or (B738DR_fo_exp_map_mode ~= fo_exp_map_mode_old2) 
		or (B738DR_fo_vsd_map_mode ~= fo_vsd_map_mode_old2) then
			B738DR_nd_refresh_fo = 0.5
			nd_refreshed_fo = true
		end
		fo_map_mode_old2 = B738DR_fo_map_mode
		fo_exp_map_mode_old2 = B738DR_fo_exp_map_mode
		fo_vsd_map_mode_old2 = B738DR_fo_vsd_map_mode
		fo_range_old2 = B738DR_efis_map_range_fo
	end
end

function B738_efis_map_range_fo_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_efis_map_range_fo == 0 then
			B738DR_efis_map_range_fo = 1
		elseif B738DR_efis_map_range_fo == 1 then
			B738DR_efis_map_range_fo = 2
		elseif B738DR_efis_map_range_fo == 2 then
			B738DR_efis_map_range_fo = 3
		elseif B738DR_efis_map_range_fo == 3 then
			B738DR_efis_map_range_fo = 4
		elseif B738DR_efis_map_range_fo == 4 then
			B738DR_efis_map_range_fo = 5
		elseif B738DR_efis_map_range_fo == 5 then
			B738DR_efis_map_range_fo = 6
		elseif B738DR_efis_map_range_fo == 6 then
			B738DR_efis_map_range_fo = 7
		end
		if B738DR_fo_map_mode > 1 and B738DR_efis_map_range_fo ~= fo_range_old2 then
			B738DR_nd_refresh_fo = 0.19
			nd_refreshed_fo = true
		end
		fo_range_old2 = B738DR_efis_map_range_fo
	end
end

function B738_efis_map_range_fo_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_efis_map_range_fo == 1 then
			B738DR_efis_map_range_fo = 0
		elseif B738DR_efis_map_range_fo == 2 then
			B738DR_efis_map_range_fo = 1
		elseif B738DR_efis_map_range_fo == 3 then
			B738DR_efis_map_range_fo = 2
		elseif B738DR_efis_map_range_fo == 4 then
			B738DR_efis_map_range_fo = 3
		elseif B738DR_efis_map_range_fo == 5 then
			B738DR_efis_map_range_fo = 4
		elseif B738DR_efis_map_range_fo == 6 then
			B738DR_efis_map_range_fo = 5
		elseif B738DR_efis_map_range_fo == 7 then
			B738DR_efis_map_range_fo = 6
		end
		if B738DR_fo_map_mode > 1 and B738DR_efis_map_range_fo ~= fo_range_old2 then
			B738DR_nd_refresh_fo = 0.19
			nd_refreshed_fo = true
		end
		fo_range_old2 = B738DR_efis_map_range_fo
	end
end


------------ AUTOPILOT ------------------------------------------------------------------

-- function revert_to_vs()
	-- if B738DR_AP_vvi > -100 and B738DR_AP_vvi < 100 then
		-- simDR_ap_vvi_dial = 0
		-- B738DR_ap_vvi_dial = 0
		-- ap_pitch_mode = 3	-- ALT HLD
	-- elseif B738DR_AP_vvi < -100 then
		-- simDR_ap_vvi_dial = roundUpToIncrement(B738DR_AP_vvi, 100)
		-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
		-- ap_pitch_mode = 1	-- V/S
	-- else
		-- simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
		-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
		-- ap_pitch_mode = 1	-- V/S
	-- end
-- end

function turn_off_dual_cmd()
	if B738DR_autoland_status ~= 0 then
		if ap_roll_mode ~= 3 and ap_roll_mode ~= 6 and ap_roll_mode ~= 17 and B738DR_flare_status == 0 then
			if cmd_first == 0 then
				autopilot_cmd_b_status = 0
				ap1_on = 1
				ap2_on = 0
			else
				autopilot_cmd_a_status = 0
				ap1_on = 0
				ap2_on = 1
			end
			B738DR_autoland_status = 0
			B738DR_flare_status = 0
			B738DR_rollout_status = 0
			B738DR_no_autoland = 0
			vorloc_only = 0
			if ap_roll_mode == 9 or ap_roll_mode == 19 then
				ap_roll_mode = 3	-- APP
				ap_roll_mode_eng = 3	-- APP
			end
			if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
				ap_pitch_mode = 4	-- GS
				ap_pitch_mode_eng = 4	-- GS
			end
		end
	else
		B738DR_flare_status = 0
		B738DR_rollout_status = 0
		B738DR_no_autoland = 0
		vorloc_only = 0
	end
end

function B738_autopilot_co_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_co_pos = 1
		if B738DR_show_ias == 1 then
			if simDR_airspeed_is_mach == 0 then
				local kspd = 1
				if simDR_IAS > 45 then
					kspd = (simDR_TAS * 1.94384449244) / simDR_IAS
				end
				kspd = (0.60 * (simDR_sos * 1.94384449244)) / kspd
				-- kspd = (0.40 * (simDR_sos * 1.94384449244)) / kspd
				if B738DR_mcp_speed_dial_kts >= kspd then
					simCMD_autopilot_co:once()
				end
			else
				simCMD_autopilot_co:once()
			end
		end
	elseif phase == 2 then
		B738DR_autopilot_co_pos = 0
	end
end

--------

function B738_nav_source_swap()

	if autopilot_fms_nav_status == 0 then
		B738DR_pfd_vorloc_lnav = 0
		if B738DR_autopilot_vhf_source_pos == -1 then
			simDR_autopilot_source = 0		-- NAV 1
			simDR_autopilot_fo_source = 0	-- NAV 1
		elseif B738DR_autopilot_vhf_source_pos == 0 then
		 	simDR_autopilot_source = 0		-- NAV 1
			simDR_autopilot_fo_source = 1	-- NAV 2
		elseif B738DR_autopilot_vhf_source_pos == 1 then
		 	simDR_autopilot_source = 1		-- NAV 2
			simDR_autopilot_fo_source = 1	-- NAV 2
		end
	elseif autopilot_fms_nav_status == 1 then
		simDR_autopilot_source = 2			-- LNAV
		simDR_autopilot_fo_source = 2		-- LNAV
		B738DR_pfd_vorloc_lnav = 1
	end

end

function B738_nav1_nav2_source()
	if B738DR_autopilot_vhf_source_pos == -1 then
		-- pilot
		B738DR_nav_flag_gs = simDR_nav1_flag_gs
		B738DR_nav_vert_dsp = simDR_nav1_vert_dsp
		B738DR_nav_flag_ft = simDR_nav1_flag_ft
		B738DR_nav_nav_id = simDR_nav1_nav_id
		B738DR_nav_dme = simDR_nav1_dme
		B738DR_nav_has_dme = simDR_nav1_has_dme
		B738DR_nav_obs = simDR_nav1_obs_pilot
		B738DR_nav_bearing = simDR_nav1_bearing
		B738DR_nav_course = simDR_nav1_course_pilot
		B738DR_nav_type = simDR_nav1_type
		B738DR_nav_tuned_type = B738DR_tuned_nav1_type
		B738DR_nav_tuned_no_id = B738DR_tuned_nav1_no_id
		B738DR_nav_tuned_crs = B738DR_tuned_nav1_crs
		B738DR_nav_freq = simDR_nav1_freq
		B738DR_nav_id_decode = B738DR_tuned_nav1_decode
		if simDR_nav1_horz_dsp == nil or simDR_nav1_horz_dsp == 0 then
			B738DR_nav_horz_dsp = 0
			B738DR_nav_hdef = -2.5
		else
			B738DR_nav_horz_dsp = 1
			if simDR_nav1_hdef_pilot == nil then
				B738DR_nav_hdef = -2.5
			else
				B738DR_nav_hdef = simDR_nav1_hdef_pilot
			end
		end
		if simDR_nav1_vdef_pilot == nil or simDR_nav1_vdef_pilot == 0 then
			B738DR_nav_vdef = 0
		else
			B738DR_nav_vdef = simDR_nav1_vdef_pilot
		end
		-- copilot
		B738DR_nav_flag_gs_fo = simDR_nav1_flag_gs
		B738DR_nav_vert_dsp_fo = simDR_nav1_vert_dsp
		B738DR_nav_flag_ft_fo = simDR_nav1_flag_ft
		B738DR_nav_nav_id_fo = simDR_nav1_nav_id
		B738DR_nav_dme_fo = simDR_nav1_dme
		B738DR_nav_has_dme_fo = simDR_nav1_has_dme
		B738DR_nav_obs_fo = simDR_nav1_obs_pilot
		B738DR_nav_bearing_fo = simDR_nav1_bearing
		B738DR_nav_course_fo = simDR_nav1_course_copilot
		B738DR_nav_type_fo = simDR_nav1_type
		B738DR_nav_tuned_type_fo = B738DR_tuned_nav1_type
		B738DR_nav_tuned_no_id_fo = B738DR_tuned_nav1_no_id
		B738DR_nav_tuned_crs_fo = B738DR_tuned_nav1_crs
		B738DR_nav_freq_fo = simDR_nav1_freq
		B738DR_nav_id_decode_fo = B738DR_tuned_nav1_decode
		if simDR_nav1_horz_dsp == nil or simDR_nav1_horz_dsp == 0 then
			B738DR_nav_hdef_fo = -2.5
			B738DR_nav_horz_dsp_fo = 0
		else
			B738DR_nav_horz_dsp_fo = 1
			if simDR_nav1_hdef_copilot == nil then
				B738DR_nav_hdef_fo = -2.5
			else
				B738DR_nav_hdef_fo = simDR_nav1_hdef_copilot
			end
		end
		if simDR_nav1_vdef_copilot == nil or simDR_nav1_vdef_copilot == 0 then
			B738DR_nav_vdef_fo = 0
		else
			B738DR_nav_vdef_fo = simDR_nav1_vdef_copilot
		end
	elseif B738DR_autopilot_vhf_source_pos == 0 then
		-- pilot
		B738DR_nav_flag_gs = simDR_nav1_flag_gs
		B738DR_nav_vert_dsp = simDR_nav1_vert_dsp
		B738DR_nav_flag_ft = simDR_nav1_flag_ft
		B738DR_nav_nav_id = simDR_nav1_nav_id
		B738DR_nav_dme = simDR_nav1_dme
		B738DR_nav_has_dme = simDR_nav1_has_dme
		B738DR_nav_obs = simDR_nav1_obs_pilot
		B738DR_nav_bearing = simDR_nav1_bearing
		B738DR_nav_course = simDR_nav1_course_pilot
		B738DR_nav_type = simDR_nav1_type
		B738DR_nav_tuned_type = B738DR_tuned_nav1_type
		B738DR_nav_tuned_no_id = B738DR_tuned_nav1_no_id
		B738DR_nav_tuned_crs = B738DR_tuned_nav1_crs
		B738DR_nav_freq = simDR_nav1_freq
		B738DR_nav_id_decode = B738DR_tuned_nav1_decode
		if simDR_nav1_horz_dsp == nil or simDR_nav1_horz_dsp == 0 then
			B738DR_nav_hdef = -2.5
			B738DR_nav_horz_dsp = 0
		else
			B738DR_nav_horz_dsp = 1
			if simDR_nav1_hdef_pilot == nil then
				B738DR_nav_hdef = -2.5
			else
				B738DR_nav_hdef = simDR_nav1_hdef_pilot
			end
		end
		if simDR_nav1_vdef_pilot == nil or simDR_nav1_vdef_pilot == 0 then
			B738DR_nav_vdef = 0
		else
			B738DR_nav_vdef = simDR_nav1_vdef_pilot
		end
		-- copilot
		B738DR_nav_flag_gs_fo = simDR_nav2_flag_gs
		B738DR_nav_vert_dsp_fo = simDR_nav2_vert_dsp
		B738DR_nav_flag_ft_fo = simDR_nav2_flag_ft
		B738DR_nav_nav_id_fo = simDR_nav2_nav_id
		B738DR_nav_dme_fo = simDR_nav2_dme
		B738DR_nav_has_dme_fo = simDR_nav2_has_dme
		B738DR_nav_obs_fo = simDR_nav2_obs_copilot
		B738DR_nav_bearing_fo = simDR_nav2_bearing
		B738DR_nav_course_fo = simDR_nav2_course_copilot
		B738DR_nav_type_fo = simDR_nav2_type
		B738DR_nav_tuned_type_fo = B738DR_tuned_nav2_type
		B738DR_nav_tuned_no_id_fo = B738DR_tuned_nav2_no_id
		B738DR_nav_tuned_crs_fo = B738DR_tuned_nav2_crs
		B738DR_nav_freq_fo = simDR_nav2_freq
		B738DR_nav_id_decode_fo = B738DR_tuned_nav2_decode
		if simDR_nav2_horz_dsp == nil or simDR_nav2_horz_dsp == 0 then
			B738DR_nav_hdef_fo = -2.5
			B738DR_nav_horz_dsp_fo = 0
		else
			B738DR_nav_horz_dsp_fo = 1
			if simDR_nav2_hdef_copilot == nil then
				B738DR_nav_hdef_fo = -2.5
			else
				B738DR_nav_hdef_fo = simDR_nav2_hdef_copilot
			end
		end
		if simDR_nav2_vdef_copilot == nil or simDR_nav2_vdef_copilot == 0 then
			B738DR_nav_vdef_fo = 0
		else
			B738DR_nav_vdef_fo = simDR_nav2_vdef_copilot
		end
	elseif B738DR_autopilot_vhf_source_pos == 1 then
		-- pilot
		B738DR_nav_flag_gs = simDR_nav2_flag_gs
		B738DR_nav_vert_dsp = simDR_nav2_vert_dsp
		B738DR_nav_flag_ft = simDR_nav2_flag_ft
		B738DR_nav_nav_id = simDR_nav2_nav_id
		B738DR_nav_dme = simDR_nav2_dme
		B738DR_nav_has_dme = simDR_nav2_has_dme
		B738DR_nav_obs = simDR_nav2_obs_pilot
		B738DR_nav_bearing = simDR_nav2_bearing
		B738DR_nav_course = simDR_nav2_course_pilot
		B738DR_nav_type = simDR_nav2_type
		B738DR_nav_tuned_type = B738DR_tuned_nav2_type
		B738DR_nav_tuned_no_id = B738DR_tuned_nav2_no_id
		B738DR_nav_tuned_crs = B738DR_tuned_nav2_crs
		B738DR_nav_freq = simDR_nav2_freq
		B738DR_nav_id_decode = B738DR_tuned_nav2_decode
		if simDR_nav2_horz_dsp == nil or simDR_nav2_horz_dsp == 0 then
			B738DR_nav_hdef = -2.5
			B738DR_nav_horz_dsp = 0
		else
			B738DR_nav_horz_dsp = 1
			if simDR_nav2_hdef_pilot == nil then
				B738DR_nav_hdef = -2.5
			else
				B738DR_nav_hdef = simDR_nav2_hdef_pilot
			end
		end
		if simDR_nav2_vdef_pilot == nil or simDR_nav2_vdef_pilot == 0 then
			B738DR_nav_vdef = 0
		else
			B738DR_nav_vdef = simDR_nav2_vdef_pilot
		end
		-- copilot
		B738DR_nav_flag_gs_fo = simDR_nav2_flag_gs
		B738DR_nav_vert_dsp_fo = simDR_nav2_vert_dsp
		B738DR_nav_flag_ft_fo = simDR_nav2_flag_ft
		B738DR_nav_nav_id_fo = simDR_nav2_nav_id
		B738DR_nav_dme_fo = simDR_nav2_dme
		B738DR_nav_has_dme_fo = simDR_nav2_has_dme
		B738DR_nav_obs_fo = simDR_nav2_obs_copilot
		B738DR_nav_bearing_fo = simDR_nav2_bearing
		B738DR_nav_course_fo = simDR_nav2_course_copilot
		B738DR_nav_type_fo = simDR_nav2_type
		B738DR_nav_tuned_type_fo = B738DR_tuned_nav2_type
		B738DR_nav_tuned_no_id_fo = B738DR_tuned_nav2_no_id
		B738DR_nav_tuned_crs_fo = B738DR_tuned_nav2_crs
		B738DR_nav_freq_fo = simDR_nav2_freq
		B738DR_nav_id_decode_fo = B738DR_tuned_nav2_decode
		if simDR_nav2_horz_dsp == nil or simDR_nav2_horz_dsp == 0 then
			B738DR_nav_hdef_fo = -2.5
			B738DR_nav_horz_dsp_fo = 0
		else
			B738DR_nav_horz_dsp_fo = 1
			if simDR_nav2_hdef_copilot == nil then
				B738DR_nav_hdef_fo = -2.5
			else
				B738DR_nav_hdef_fo = simDR_nav2_hdef_copilot
			end
		end
		if simDR_nav2_vdef_copilot == nil or simDR_nav2_vdef_copilot == 0 then
			B738DR_nav_vdef_fo = 0
		else
			B738DR_nav_vdef_fo = simDR_nav2_vdef_copilot
		end
	end
	
end


-- ROLL MODES
function B738_autopilot_hdg_sel_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_hdg_sel_pos = 1
		ap_heading_buf = 1
		ap_heading_used = true
	elseif phase == 2 then
		B738DR_autopilot_hdg_sel_pos = 0
	end
end

function B738_autopilot_vorloc_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_vorloc_pos = 1
		ap_heading_buf = 2
		ap_heading_used = true
	elseif phase == 2 then
		B738DR_autopilot_vorloc_pos = 0
	end
end

function B738_autopilot_app_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_app_pos = 1
		ap_heading_buf = 3
		ap_heading_used = true
		
		ap_speed_buf = 0
		ap_speed_used = false
		ap_speed_timer = 0
		
	elseif phase == 2 then
		B738DR_autopilot_app_pos = 0
	end
end

function B738_autopilot_lnav_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_lnav_pos = 1
		ap_heading_buf = 4
		ap_heading_used = true
	elseif phase == 2 then
		B738DR_autopilot_lnav_pos = 0
	end
end

-- PITCH MODES
function B738_autopilot_vs_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_vs_pos = 1
		ap_altitude_buf = 1
		ap_altitude_used = true
		
		ap_speed_buf = 0
		ap_speed_used = false
		ap_speed_timer = 0
		
	elseif phase == 2 then
		B738DR_autopilot_vs_pos = 0
	end
end

function B738_autopilot_lvl_chg_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_lvl_chg_pos = 1 
		ap_altitude_buf = 2
		ap_altitude_used = true
		
		ap_speed_buf = 0
		ap_speed_used = false
		ap_speed_timer = 0
		
	elseif phase == 2 then
		B738DR_autopilot_lvl_chg_pos = 0
	end
end

function B738_autopilot_alt_hld_press_CMDhandler(phase, duration)
	if phase == 0 then
		ap_altitude_buf = 3
		ap_altitude_used = true
		
		ap_speed_buf = 0
		ap_speed_used = false
		ap_speed_timer = 0
		
	elseif phase == 2 then
		B738DR_autopilot_alt_hld_pos = 0
	end
end

function B738_autopilot_vnav_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_vnav_pos = 1
		ap_altitude_buf = 4
		ap_altitude_used = true
		
		ap_speed_buf = 0
		ap_speed_used = false
		ap_speed_timer = 0
		
	elseif phase == 2 then
		B738DR_autopilot_vnav_pos = 0
	end
end

-- THROTLE MODES
function B738_autopilot_n1_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_n1_pos = 1
		-- if ap_altitude_buf ~= 0 and ap_heading_buf ~= 3 then
		-- if ap_heading_buf ~= 3 then
			ap_speed_buf = 1
			ap_speed_used = true
		-- end
	elseif phase == 2 then
		B738DR_autopilot_n1_pos = 0
	end
end

function B738_autopilot_speed_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_speed_pos = 1
		ap_speed_buf = 2
		ap_speed_used = true
	elseif phase == 2 then
		B738DR_autopilot_speed_pos = 0
	end
end

function vs_to_lvlchg()
	ap_pitch_mode = 2
	simDR_airspeed_dial_kts = 100
	-- new mcp
	B738DR_mcp_speed_dial = simDR_airspeed_dial
	update_mcp_speed_dial()
end

function B738_autopilot_disconnect_toggle_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_disconnect_pos == 0 then
			
			B738DR_autopilot_disconnect_pos = 1
			simDR_flight_dir_mode = 1
			ap_on = 0
			ap1_on = 0
			ap2_on = 0

			autopilot_cws_a_status = 0
			autopilot_cws_b_status = 0
			autopilot_cmd_b_status = 0
			autopilot_cmd_a_status = 0
			autopilot_cws_status = 0
			autopilot_cws_r_status = 0
			autopilot_cws_p_status = 0
			B738DR_flare_status = 0
			B738DR_rollout_status = 0
			B738DR_no_autoland = 0
			B738DR_retard_status = 0
			B738DR_single_ch_status = 0
			B738DR_fmc_descent_now = 0
			B738DR_fmc_mode = 0
			B738DR_autopilot_vnav_alt_pfd = 0
			B738DR_autopilot_vnav_pth_pfd = 0
			B738DR_autopilot_vnav_spd_pfd = 0
			B738DR_autopilot_fmc_spd_pfd = 0
			B738DR_autopilot_to_ga_pfd = 0
			B738DR_autopilot_thr_hld_pfd = 0
			B738DR_autopilot_ga_pfd = 0
			B738DR_autopilot_alt_acq_pfd = 0
			if ap_roll_mode == 9 or ap_roll_mode == 19 then
				ap_roll_mode = 3	-- APP
				ap_roll_mode_eng = 3	-- APP
			end
			if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
				ap_pitch_mode = 4	-- GS
				ap_pitch_mode_eng = 4	-- GS
			end
			B738DR_autoland_status = 0
			
			ils_test_enable = 0
			if ap_on == 1 then
				ap_disco_do = 1
			end
			
			B738DR_enable_servo_elev_pitch = 0
			
		elseif B738DR_autopilot_disconnect_pos == 1 then
			B738DR_autopilot_disconnect_pos = 0
		end
	end
end

function B738_autopilot_autothr_arm_toggle_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_autothr_arm_pos == 0 then
			B738DR_autopilot_autothr_arm_pos = 1
			if B738DR_dc_bus1_status ~= 0 then
				autopilot_autothrottle_status = 1
			end
			at_dis_time = 0
			if is_timer_scheduled(blink_timer_at) == true then
				stop_timer(blink_timer_at)
			end
			at_status = 1
		elseif B738DR_autopilot_autothr_arm_pos == 1 then
			B738DR_autopilot_autothr_arm_pos = 0
			autopilot_autothrottle_status = 0
			at_mode = 0
			simCMD_autothrottle_discon:once()	-- disconnect autothrotle
			at_status = 0
		end
		autothr_arm_pos = 1
	elseif phase == 2 then
		if B738DR_autopilot_autothr_arm_pos == 1 then
			at_status = 2
		end
		autothr_arm_pos = 0
	end
end

---- FLIGHT DIRECTORS ------------------------------------------

function B738_autopilot_flight_dir_toggle_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_fd_pos == 0 then
			B738DR_autopilot_fd_pos = 1
			if B738DR_autopilot_fd_fo_pos == 0 then
				if ap_on == 0 then
					autopilot_side = 0
				end
			end
		elseif B738DR_autopilot_fd_pos == 1 then
			B738DR_autopilot_fd_pos = 0
			if B738DR_autopilot_fd_fo_pos == 1 then
				if ap_on == 0 then
					autopilot_side = 1
				end
			else
				if ap_on == 0 then
					autopilot_side = 0
				end
			end
		end
		autopilot_master_first = autopilot_side
		master_light_block_act = 0
	end
end

function B738_autopilot_flight_dir_fo_toggle_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_fd_fo_pos == 0 then
			B738DR_autopilot_fd_fo_pos = 1
			if B738DR_autopilot_fd_pos == 0 then
				if ap_on == 0 then
					autopilot_side = 1
				end
			end
		elseif B738DR_autopilot_fd_fo_pos == 1 then
			B738DR_autopilot_fd_fo_pos = 0
			if ap_on == 0 then
				autopilot_side = 0
			end
		end
		autopilot_master_first = autopilot_side
		master_light_block_act = 0
	end
end

---- BANK ANGLE ---------------------------------------------

function B738_autopilot_bank_angle_up_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_bank_angle_pos == 0 then
			B738DR_autopilot_bank_angle_pos = 1
		elseif B738DR_autopilot_bank_angle_pos == 1 then
			B738DR_autopilot_bank_angle_pos = 2
		elseif B738DR_autopilot_bank_angle_pos == 2 then
			B738DR_autopilot_bank_angle_pos = 3
		elseif B738DR_autopilot_bank_angle_pos == 3 then
			B738DR_autopilot_bank_angle_pos = 4
		end
	end
end
		
function B738_autopilot_bank_angle_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_bank_angle_pos == 4 then
			B738DR_autopilot_bank_angle_pos = 3
		elseif B738DR_autopilot_bank_angle_pos == 3 then
			B738DR_autopilot_bank_angle_pos = 2
		elseif B738DR_autopilot_bank_angle_pos == 2 then
			B738DR_autopilot_bank_angle_pos = 1
		elseif B738DR_autopilot_bank_angle_pos == 1 then
			B738DR_autopilot_bank_angle_pos = 0
		end
	end
end	



-- COMMAND A&B

---B738DR_fmc_gw

function B738_autopilot_cmd_a_press_CMDhandler(phase, duration)
	if phase == 0 then
		
		local ap_disable = 0
		if B738DR_hyd_A_status == 0 or B738DR_ap_trim_pos ~= 0 then
			ap_disable = 1
		end
		
		local ap_on_gnd = 0
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then		-- if aircraft on the ground
			ap_on_gnd = 1
		end
		
		local crs1 = math.floor(B738DR_course_pilot + 0.5)
		local crs2 = math.floor(B738DR_course_copilot + 0.5)
		
		local tuned_crs = (math.floor(B738DR_nav_tuned_crs + 0.5) - crs1 + 360) % 360
		if tuned_crs > 180 then
			tuned_crs = tuned_crs - 360
		end
		local tuned_crs_gls = (math.floor(B738DR_gls_crs + 0.5) - crs1 + 360) % 360
		if tuned_crs_gls > 180 then
			tuned_crs_gls = tuned_crs_gls - 360
		end
		
		local autoland_disable = 1
		if autopilot_cmd_b_status == 1 and (ap_roll_mode == 3 or ap_roll_mode == 6 or ap_roll_mode == 17) and B738DR_autopilot_vhf_source_pos == 0 
		and simDR_radio_height_pilot_ft > 800 then
			autoland_disable = 0
		end
		if autoland_disable == 0 then
			if mmr_act_mode == 1 then
				if simDR_radio_height_pilot_ft < 1500 and simDR_approach_status == 2 
				and (simDR_nav1_freq ~= simDR_nav2_freq or crs1 ~= crs2 or B738DR_autopilot_vhf_source_pos ~= 0 or B738DR_mmr_cpt_act_mode ~= B738DR_mmr_fo_act_mode) 
				or math.abs(tuned_crs) > 6 then
					autoland_disable = 1
				end
			elseif mmr_act_mode == 2 then
				if simDR_radio_height_pilot_ft < 1500 and B738DR_gls_app_status == 2 
				and (B738DR_mmr_cpt_act_value ~= B738DR_mmr_fo_act_value or crs1 ~= crs2 or B738DR_autopilot_vhf_source_pos ~= 0 or B738DR_mmr_cpt_act_mode ~= B738DR_mmr_fo_act_mode) 
				or math.abs(tuned_crs_gls) > 6 then
					autoland_disable = 1
				end
			end
		end
		
		local ap_input_disable = 0
		local ap_input_cws_r = 0
		local ap_input_cws_p = 0
		local yoke_neutral = 0.1
		local fd_tollerance = 22.5	--12
		local fd_tollerance2 = 7.5	--7
		if B738DR_ap_yoke_tolerance ~= 0 then
			yoke_neutral = 0.25
		end
		if autopilot_cmd_b_status == 0 then
			if math.abs(simDR_pitch_ratio2) > yoke_neutral then
				ap_input_disable = 1
			elseif math.abs(simDR_roll_ratio2) > yoke_neutral then
				ap_input_disable = 1
			elseif B738DR_ap_yoke_tolerance == 0 then
				local test_input = math.abs(simDR_roll_AHARS_deg - B738DR_fdir_roll_main)
				if test_input > fd_tollerance then
					ap_input_cws_r = 1
				end
				test_input = math.abs(simDR_ahars_pitch_deg_pilot - B738DR_fdir_pitch_main)	--B738DR_fdir_pitch_pilot
				if test_input > fd_tollerance2 then
					ap_input_cws_p = 1
				end
			end
		end
		
		B738DR_autopilot_cmd_a_pos = 1
		
			if (autopilot_cmd_a_status == 0 or autopilot_cws_a_status == 1 or autopilot_cws_b_status == 1) 
			and B738DR_autopilot_disconnect_pos == 0 and ap_app_block_800 == 0 and ap_disable == 0 then
				if ap_input_disable ~= 0 then
					ap_dis_time = 0
					ap_disco2 = 1
					simCMD_disconnect:once()
					simDR_flight_dir_mode = 1
					autopilot_cmd_a_status = 0
					autopilot_cmd_b_status = 0
					ap_capt_disco_status = 1
					ap_fo_disco_status = 1
					ap_on_first = 1
				else
					autopilot_cws_status = 0
					autopilot_cws_r_status = 0
					autopilot_cws_p_status = 0
					autopilot_cws_a_status = 0
					autopilot_cws_b_status = 0
					
					-- CWS R/P
					if ap_roll_mode == 0 then
						autopilot_cws_r_status = 1
					end
					if ap_pitch_mode == 0 then
						autopilot_cws_p_status = 1
					end
						
						autopilot_cmd_a_status = 1
						B738DR_pitch_ratio2_old = simDR_pitch_ratio2
						roll_ratio2_old = simDR_roll_ratio2
						ap_dis_time = 0
						ap_capt_disco_status = 0
						ap_fo_disco_status = 0
						
						if is_timer_scheduled(ap_disable_test_yoke) == true then
							stop_timer(ap_disable_test_yoke)
						end
						run_after_time(ap_disable_test_yoke, 2)
						yoke_test_disabled = 1
						
						if autoland_disable == 0 then
							B738DR_autoland_status = 1
							cmd_first = 1	-- CMD B first
							ils_test_enable = 1
						else									-- CMD A on
							simCMD_autopilot_cws:stop()
							ap_on = 1
							cmd_first = 0	-- CMD A first
							autopilot_side = 0
							if is_timer_scheduled(master_light_block) == true then
								stop_timer(master_light_block)
							end
							master_light_block_act = 0
							if autopilot_master_fo_status == 1 then
								master_light_block_act = 1
								run_after_time(master_light_block, 1.5)
							end
							if ap_pitch_mode == 8 or ap_pitch_mode == 11 or B738DR_at_takeoff ~= 0 then	-- TAKEOFF mode
								ap_pitch_mode = 2	-- LVL CHG
								ap_pitch_mode_eng = 2
								B738DR_lvl_chg_mode = 1
								vnav_vs = 0
								init_climb = 0
								lvl_chg_co_status = 0
								lvl_chg_vs = 0
								if simDR_autopilot_altitude_mode ~= 5 then
									simCMD_autopilot_lvl_chg:once()
								end
								at_mode = 6
								if to_ga_mcp_changed == 0 then
									B738DR_mcp_speed_dial = mcp_speed_dial_mem + 20
								end
								simDR_airspeed_dial = B738DR_mcp_speed_dial
								update_mcp_speed_dial()
								B738DR_mcp_speed_dial = simDR_airspeed_dial
								update_mcp_speed_dial()
								
								if ap_roll_mode == 0 then
									ap_roll_mode = 1	-- HDG SEL
									hdg_turn = 0
								end
							elseif ap_pitch_mode == 10 then	-- F/D GA
									B738DR_fd_goaround = 0
									ap_pitch_mode = 2	-- LVL CHG
									if ap_roll_mode == 15 or ap_roll_mode == 0 then
										ap_roll_mode = 1	-- HDG SEL
									elseif ap_roll_mode == 18 then
										ap_roll_mode = 10	-- HDG/LNAV
										ap_roll_mode_eng = 10
										B738DR_lnav_engaged = 0
									end
								-- end
								hdg_turn = 0
							elseif (at_mode == 2 or at_mode == 9) and ap_pitch_mode_eng == 0 and ap_on_gnd == 0 then
								ap_pitch_mode = 2	-- LVL CHG mode
							elseif ap_pitch_mode_eng == 0 and ap_on_gnd == 0 then
								ap_pitch_mode = 1	-- VS mode
								ap_pitch_mode_eng = 1
								vs_co_status = 0
								simCMD_autopilot_vs:once()
								vert_spd_corr()
								at_mode = 2		-- SPEED mode
								simDR_airspeed_dial = simDR_airspeed_copilot
								B738DR_mcp_speed_dial = simDR_airspeed_dial
								update_mcp_speed_dial()
							end
							if B738DR_autopilot_lnav_status == 0 then
								autopilot_fms_nav_status = 0
							end
						end
					-- end
				end
			elseif autopilot_cmd_a_status == 1 then
				simCMD_disconnect:once()
				simDR_flight_dir_mode = 1
				autopilot_cmd_a_status = 0
				autopilot_cmd_b_status = 0
				ap_capt_disco_status = 1
				ap_fo_disco_status = 1
				if B738DR_autoland_status == 1 then
					if simDR_nav_status == 2 and simDR_approach_status == 0 then
						simCMD_autopilot_app:once()
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					elseif simDR_approach_status > 0 then
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					else
						if simDR_nav_status == 0 then
							simCMD_autopilot_lnav:once()
						end
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					end
				end
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_autoland_status = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				vorloc_only = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				if is_timer_scheduled(annun_flare) == true then
					stop_timer(annun_flare)
				end
			end
		--end
	elseif phase == 2 then
		B738DR_autopilot_cmd_a_pos = 0
	end
end

function B738_autopilot_cmd_b_press_CMDhandler(phase, duration)
	if phase == 0 then
		
		local ap_disable = 0
		if B738DR_hyd_B_status == 0 or B738DR_ap_trim_pos ~= 0 then
			ap_disable = 1
		end
		
		local ap_on_gnd = 0
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then		-- if aircraft on the ground
			ap_on_gnd = 1
		end
		
		local crs1 = math.floor(B738DR_course_pilot + 0.5)
		local crs2 = math.floor(B738DR_course_copilot + 0.5)
		
		local tuned_crs = (math.floor(B738DR_nav_tuned_crs + 0.5) - crs1 + 360) % 360
		if tuned_crs > 180 then
			tuned_crs = tuned_crs - 360
		end
		local tuned_crs_gls = (math.floor(B738DR_gls_crs + 0.5) - crs1 + 360) % 360
		if tuned_crs_gls > 180 then
			tuned_crs_gls = tuned_crs_gls - 360
		end
		
		local autoland_disable = 1
		if autopilot_cmd_a_status == 1 and (ap_roll_mode == 3 or ap_roll_mode == 6 or ap_roll_mode == 17) and B738DR_autopilot_vhf_source_pos == 0 
		and simDR_radio_height_pilot_ft > 800 then
			autoland_disable = 0
		end
		if autoland_disable == 0 then
			if mmr_act_mode == 1 then
				if simDR_radio_height_pilot_ft < 1500 and simDR_approach_status == 2 
				and (simDR_nav1_freq ~= simDR_nav2_freq or crs1 ~= crs2 or B738DR_autopilot_vhf_source_pos ~= 0 or B738DR_mmr_cpt_act_mode ~= B738DR_mmr_fo_act_mode) 
				or math.abs(tuned_crs) > 6 then
					autoland_disable = 1
				end
			elseif mmr_act_mode == 2 then
				if simDR_radio_height_pilot_ft < 1500 and B738DR_gls_app_status == 2 
				and (B738DR_mmr_cpt_act_value ~= B738DR_mmr_fo_act_value or crs1 ~= crs2 or B738DR_autopilot_vhf_source_pos ~= 0 or B738DR_mmr_cpt_act_mode ~= B738DR_mmr_fo_act_mode) 
				or math.abs(tuned_crs_gls) > 6 then
					autoland_disable = 1
				end
			end
		end
		
		local ap_input_disable = 0
		local ap_input_cws_r = 0
		local ap_input_cws_p = 0
		local yoke_neutral = 0.1
		local fd_tollerance = 22.5	--12
		local fd_tollerance2 = 7.5	--7
		if B738DR_ap_yoke_tolerance ~= 0 then
			yoke_neutral = 0.25
		end
		if autopilot_cmd_a_status == 0 then
			if math.abs(simDR_pitch_ratio2) > yoke_neutral then
				ap_input_disable = 1
			elseif math.abs(simDR_roll_ratio2) > yoke_neutral then
				ap_input_disable = 1
			elseif B738DR_ap_yoke_tolerance == 0 then
				local test_input = math.abs(simDR_roll_AHARS_deg_copilot - B738DR_fdir_roll_main)
				if test_input > fd_tollerance then
					ap_input_cws_r = 1
				end
				test_input = math.abs(simDR_ahars_pitch_deg_copilot - B738DR_fdir_pitch_copilot)
				if test_input > fd_tollerance2 then
					ap_input_cws_p = 1
				end
			end
		end
		
		B738DR_autopilot_cmd_b_pos = 1
		
			if (autopilot_cmd_b_status == 0 or autopilot_cws_a_status == 1 or autopilot_cws_b_status == 1) 
			and B738DR_autopilot_disconnect_pos == 0 and ap_app_block_800 == 0 and ap_disable == 0 then
				if ap_input_disable ~= 0 then
					ap_dis_time = 0
					ap_disco2 = 1
					simCMD_disconnect:once()
					simDR_flight_dir_mode = 1
					autopilot_cmd_a_status = 0
					autopilot_cmd_b_status = 0
					ap_capt_disco_status = 1
					ap_fo_disco_status = 1
					ap_on_first = 1
				else
					autopilot_cws_status = 0
					autopilot_cws_r_status = 0
					autopilot_cws_p_status = 0
					autopilot_cws_a_status = 0
					autopilot_cws_b_status = 0
					
					-- CWS R/P
					if ap_roll_mode == 0 then
						autopilot_cws_r_status = 1
					end
					if ap_pitch_mode == 0 then
						autopilot_cws_p_status = 1
					end
					
						autopilot_cmd_b_status = 1
						B738DR_pitch_ratio2_old = simDR_pitch_ratio2
						roll_ratio2_old = simDR_roll_ratio2
						ap_dis_time = 0
						ap_capt_disco_status = 0
						ap_fo_disco_status = 0
						
						if is_timer_scheduled(ap_disable_test_yoke) == true then
							stop_timer(ap_disable_test_yoke)
						end
						run_after_time(ap_disable_test_yoke, 2)
						yoke_test_disabled = 1
						
						if autoland_disable == 0 then
							B738DR_autoland_status = 1
							cmd_first = 0	-- CMD A first
							ils_test_enable = 1
						else									-- CMD B on
							ap_on = 1
							cmd_first = 1	-- CMD B first
							autopilot_side = 1
							if is_timer_scheduled(master_light_block) == true then
								stop_timer(master_light_block)
							end
							master_light_block_act = 0
							if autopilot_master_capt_status == 1 then
								master_light_block_act = 1
								run_after_time(master_light_block, 1.5)
							end
							if ap_pitch_mode == 8 or ap_pitch_mode == 11 or B738DR_at_takeoff ~= 0 then	-- TAKEOFF mode
								ap_pitch_mode = 2	-- LVL CHG
								ap_pitch_mode_eng = 2
								B738DR_lvl_chg_mode = 1
								vnav_vs = 0
								init_climb = 0
								lvl_chg_co_status = 0
								lvl_chg_vs = 0
								if simDR_autopilot_altitude_mode ~= 5 then
									simCMD_autopilot_lvl_chg:once()
								end
								at_mode = 6
								if to_ga_mcp_changed == 0 then
									B738DR_mcp_speed_dial = mcp_speed_dial_mem + 20
								end
								simDR_airspeed_dial = B738DR_mcp_speed_dial
								update_mcp_speed_dial()
								B738DR_mcp_speed_dial = simDR_airspeed_dial
								update_mcp_speed_dial()
								if ap_roll_mode == 0 then
									ap_roll_mode = 1	-- HDG SEL
									hdg_turn = 0
								end
							elseif ap_pitch_mode == 10 then	-- F/D GA
								-- if B738DR_fd_goaround > 1 then
									B738DR_fd_goaround = 0
									ap_pitch_mode = 2	-- LVL CHG
									if ap_roll_mode == 15 or ap_roll_mode == 0 then
										ap_roll_mode = 1	-- HDG SEL
									elseif ap_roll_mode == 18 then
										ap_roll_mode = 10	-- HDG/LNAV
										ap_roll_mode_eng = 10
										B738DR_lnav_engaged = 0
									end
								-- end
								hdg_turn = 0
							elseif (at_mode == 2 or at_mode == 9) and ap_pitch_mode_eng == 0 and ap_on_gnd == 0 then
								ap_pitch_mode = 2	-- LVL CHG mode
							elseif ap_pitch_mode_eng == 0 and ap_on_gnd == 0 then
								ap_pitch_mode = 1	-- VS mode
								ap_pitch_mode_eng = 1
								vs_co_status = 0
								simCMD_autopilot_vs:once()
								vert_spd_corr()
								at_mode = 2		-- SPEED mode
								simDR_airspeed_dial = simDR_airspeed_copilot
								B738DR_mcp_speed_dial = simDR_airspeed_dial
								update_mcp_speed_dial()
							end
							if B738DR_autopilot_lnav_status == 0 then
								autopilot_fms_nav_status = 0
							end
						end
				end
			elseif autopilot_cmd_b_status == 1 then
				simCMD_disconnect:once()
				simDR_flight_dir_mode = 1
				
				autopilot_cmd_b_status = 0
				autopilot_cmd_a_status = 0
				ap_capt_disco_status = 1
				ap_fo_disco_status = 1
				if B738DR_autoland_status == 1 then
					if simDR_nav_status == 2 and simDR_approach_status == 0 then
						simCMD_autopilot_app:once()
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					elseif simDR_approach_status > 0 then
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					else
						if simDR_nav_status == 0 then
							simCMD_autopilot_lnav:once()
						end
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					end
				end
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_autoland_status = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				vorloc_only = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				if is_timer_scheduled(annun_flare) == true then
					stop_timer(annun_flare)
				end
			end
		--end
	elseif phase == 2 then
		B738DR_autopilot_cmd_b_pos = 0
	end
end

-- CWS A&B
function B738_autopilot_cws_a_press_CMDhandler(phase, duration)
	
	local cws_disabled = 0
	
	if phase == 0 then
		B738DR_autopilot_cws_a_pos = 1
		
		local cws_on_gnd = 0
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			cws_on_gnd = 1
		end
		
		if B738DR_ap_trim_pos ~= 0 then
			cws_disabled = 1
		elseif B738DR_autoland_status == 1 or B738DR_autopilot_disconnect_pos == 1 or (simDR_altitude_pilot < 400 and cws_on_gnd == 0) then
			cws_disabled = 1
		elseif B738DR_gear_handle_pos == 1 and simDR_radio_height_pilot_ft < 150 and cws_on_gnd == 0 then
			cws_disabled = 1
		elseif ap_roll_mode == 2 and simDR_nav_status == 2 and (simDR_ground_spd * 1.94384449244) < 250 then
			cws_disabled = 1
		elseif ap_roll_mode == 3 and (simDR_nav_status == 2 or simDR_approach_status == 2) then
			cws_disabled = 1
		elseif B738DR_hyd_A_status == 0 then
			cws_disabled = 1
		end
		
		if autopilot_cws_a_status == 0 then
			if cws_disabled == 0 then
				autopilot_cmd_a_status = 0
				autopilot_cmd_b_status = 0
				ap_capt_disco_status = 0
				ap_fo_disco_status = 0
				simDR_flight_dir_mode = 1
				ap_on = 0
				ap1_on = 0
				ap2_on = 0
				
				autopilot_cws_a_status = 1
				autopilot_cws_b_status = 0
				autopilot_cws_status = 1	-- CWS R / CWS P
				autopilot_cws_r_status = 1
				autopilot_cws_p_status = 1
				
				autopilot_side = 0
				if B738DR_autopilot_lnav_status == 0 then
					autopilot_fms_nav_status = 0
				end
			end
		elseif autopilot_cws_a_status == 1 then
			autopilot_cws_a_status = 0
			autopilot_cws_status = 0
			autopilot_cws_r_status = 0
			autopilot_cws_p_status = 0
			autopilot_side = autopilot_master_first
		end
	elseif phase == 2 then
		B738DR_autopilot_cws_a_pos = 0
	end
end




function B738_autopilot_cws_b_press_CMDhandler(phase, duration)
	
	local cws_disabled = 0
	
	if phase == 0 then
		B738DR_autopilot_cws_b_pos = 1
		
		local cws_on_gnd = 0
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			cws_on_gnd = 1
		end
		
		if B738DR_ap_trim_pos ~= 0 then
			cws_disabled = 1
		elseif B738DR_autoland_status == 1 or B738DR_autopilot_disconnect_pos == 1 or (simDR_altitude_pilot < 400 and cws_on_gnd == 0) then
			cws_disabled = 1
		elseif B738DR_gear_handle_pos == 1 and simDR_radio_height_pilot_ft < 150 and cws_on_gnd == 0 then
			cws_disabled = 1
		elseif ap_roll_mode == 2 and simDR_nav_status == 2 then
			cws_disabled = 1
		elseif ap_roll_mode == 2 and simDR_nav_status == 2 and (simDR_ground_spd * 1.94384449244) < 250 then
			cws_disabled = 1
		elseif B738DR_hyd_B_status == 0 or B738DR_ap_trim_pos ~= 0 then
			cws_disabled = 1
		end
		
		
		if autopilot_cws_b_status == 0 then
			if cws_disabled == 0 then
				autopilot_cmd_a_status = 0
				autopilot_cmd_b_status = 0
				ap_capt_disco_status = 0
				ap_fo_disco_status = 0
				simDR_flight_dir_mode = 1
				ap_on = 0
				ap1_on = 0
				ap2_on = 0
				
				autopilot_cws_a_status = 0
				autopilot_cws_b_status = 1
				autopilot_cws_status = 1	-- CWS R / CWS P
				autopilot_cws_r_status = 1
				autopilot_cws_p_status = 1
				
				autopilot_side = 1
				
			end
		elseif autopilot_cws_b_status == 1 then
			autopilot_cws_b_status = 0
			autopilot_cws_status = 0
			autopilot_cws_r_status = 0
			autopilot_cws_p_status = 0
			autopilot_side = autopilot_master_first
		end
	elseif phase == 2 then
		B738DR_autopilot_cws_b_pos = 0
	end
end


function B738_autopilot_disconect_button_CMDhandler(phase, duration)
	
	if phase == 0 then
		B738DR_yoke_capt_ap_disengage = 1
		if ap_capt_disco_status == 0 then
			
			if autopilot_cmd_a_status == 1 or autopilot_cmd_b_status == 1 
			or autopilot_cws_a_status == 1 or autopilot_cws_b_status == 1 then
				ap_disco2 = 1
			end
			
			ap_on = 0
			ap1_on = 0
			ap2_on = 0
			
			simDR_flight_dir_mode = 1
			autopilot_cmd_a_status = 0
			autopilot_cmd_b_status = 0
			autopilot_cws_a_status = 0
			autopilot_cws_b_status = 0
			autopilot_cws_status = 0
			autopilot_cws_r_status = 0
			autopilot_cws_p_status = 0
			if B738DR_autoland_status == 1 then
				B738DR_autoland_status = 0
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				vorloc_only = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				if is_timer_scheduled(annun_flare) == true then
					stop_timer(annun_flare)
				end
				ap_disco2 = 1
				
				if ap_roll_mode == 9 or ap_roll_mode == 19 then
					ap_roll_mode = 3	-- APP
					ap_roll_mode_eng = 3	-- APP
				end
				if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
					ap_pitch_mode = 4	-- GS
					ap_pitch_mode_eng = 4	-- GS
				end
			end
			ap_capt_disco_status = 1
		else
			B738DR_ap_light_pilot = 1
			ap_capt_disco_status = 0
		end
	elseif phase == 2 then
		B738DR_yoke_capt_ap_disengage = 0
		B738DR_ap_light_pilot = 0
	end
end

function B738_vhf_nav_source_switch_rgt_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_vhf_source_pos == -1 then
			B738DR_autopilot_vhf_source_pos = 0
		elseif B738DR_autopilot_vhf_source_pos == 0 then
			B738DR_autopilot_vhf_source_pos = 1
		end
	end
end

function B738_vhf_nav_source_switch_lft_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_autopilot_vhf_source_pos == 1 then
			B738DR_autopilot_vhf_source_pos = 0
		elseif B738DR_autopilot_vhf_source_pos == 0 then
			B738DR_autopilot_vhf_source_pos = -1
		end
	end
end


---- MFD: ENG and SYS

function B738_mfd_sys_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_mfd_sys_pos = 1
		if B738DR_lowerDU_page2 == 0 then
			B738DR_lowerDU_page2 = 1
			if B738DR_lowerDU_page == 1 then
				B738DR_lowerDU_page = 0
			end
			B738DR_fd_cam = 0
		else
			B738DR_lowerDU_page2 = 0
		end
	elseif phase == 2 then
		B738DR_mfd_sys_pos = 0
	end
end

function B738_mfd_eng_CMDhandler(phase, duration)

	if phase == 0 then
		B738DR_mfd_eng_pos = 1
		if B738DR_lowerDU_page == 0 then
			B738DR_lowerDU_page = 1
			if B738DR_lowerDU_page2 == 1 then
				B738DR_lowerDU_page2 = 0
			end
			B738DR_fd_cam = 0
		elseif B738DR_lowerDU_page == 1 then
			B738DR_lowerDU_page = 2
			B738DR_fd_cam = 0
		else
			B738DR_lowerDU_page = 0
		end
	elseif phase == 2 then
		B738DR_mfd_eng_pos = 0
	end

end

--- A/P, A/T light buttons

function B738_ap_light_pilot_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_ap_light_pilot = 1
		ap_warn_off = 1
		ap_warn = 0
	elseif phase == 2 then
		B738DR_ap_light_pilot = 0
	end
end

function B738_at_light_pilot_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_at_light_pilot = 1
	elseif phase == 2 then
		B738DR_at_light_pilot = 0
	end
end

function B738_ap_light_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_ap_light_fo = 1
		ap_warn_off = 1
		ap_warn = 0
	elseif phase == 2 then
		B738DR_ap_light_fo = 0
	end
end

function B738_at_light_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_at_light_fo = 1
	elseif phase == 2 then
		B738DR_at_light_fo = 0
	end
end


function B738_to_ga2_CMDhandler(phase, duration)
	local ap_ga = 0
	local fd_ga = 0
	if phase == 0 then
		
		local at_takeoff_mem = B738DR_at_takeoff
		local fd_goaround_mem = B738DR_fd_goaround
		local ap_goaround_mem = B738DR_ap_goaround
		
		ap_app_block = 0
		
		--Takeoff mode
		if simDR_airspeed_pilot > 80 and simDR_radio_height_pilot_ft < 2000 and lift_off_150 == 0 and on_ground_30 == 1 then
			no_roll_mode()
			if ap_pitch_mode == 5 then
				ap_pitch_mode = 11
			else
				ap_pitch_mode = 8
			end
			to_after_80kts = 1
			if B738DR_autopilot_autothr_arm_pos ~= 0 then
				B738DR_at_takeoff = 1
				at_mode = 3 -- Takeoff
			end
		end
		if ap_pitch_mode ~= 8 and ap_pitch_mode ~= 11 then
		-- Takeoff with both F/D on
			if B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1 and simDR_airspeed_pilot < 60 then
				no_roll_mode()
				if ap_pitch_mode == 5 then
					ap_pitch_mode = 11
				else
					ap_pitch_mode = 8
				end
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 3 -- Takeoff
					B738DR_at_takeoff = 1
				end
			elseif simDR_airspeed_pilot <= 80 then
				no_roll_mode()
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					B738DR_at_takeoff = 1
					at_mode = 3 -- Takeoff
				end
			end
		elseif B738DR_at_takeoff == 0 then
			if B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1 and simDR_airspeed_pilot < 60 then
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 3 -- Takeoff
					B738DR_at_takeoff = 1
				end
			elseif simDR_airspeed_pilot <= 80 then
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 3 -- Takeoff
					B738DR_at_takeoff = 1
				end
			end
		end
		
		if ap_pitch_mode ~= 8 and ap_pitch_mode ~= 11 and B738DR_at_takeoff == 0 then
		--AP GoAround mode
			if B738DR_flare_status > 0 and B738DR_ap_goaround == 0 then
				if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then		-- if aircraft in flight
					if simDR_radio_height_pilot_ft < 2000 then
						ap_ga = 1
					else
						if simDR_flaps_ratio > 0 or simDR_glideslope_status == 2 or ap_pitch_mode == 4 then
							ap_ga = 1
						end
					end
				end
				if ap_ga == 1 then
					B738DR_ap_goaround = 1		-- first push
					ap_ga2_activate()
					if simDR_approach_status > 0 then
						simCMD_autopilot_app:once()
					end
					if simDR_nav_status > 0 then
						simCMD_autopilot_lnav:once()
					end
				end
			elseif B738DR_ap_goaround == 1 then
				B738DR_ap_goaround = 2			-- second push
			elseif ap_on ~= 0 then
				-- AP GoAround after touchdown
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then		-- if aircraft on ground
					if ap_goaround_block == 0 then
						-- A/T automatic off disengage
						if is_timer_scheduled(ap_at_off) == true then
							stop_timer(ap_at_off)
						end
						-- A/P off
						simCMD_disconnect:once()
						simDR_flight_dir_mode = 1
						autopilot_cmd_a_status = 0
						autopilot_cmd_b_status = 0
						ap_ga1_acivate()
					end
				end
			end
			--F/D GoAround mode
			if B738DR_ap_goaround == 0 and B738DR_flare_status == 0 then
				if B738DR_fd_on == 1 or (simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0) then	--or simDR_radio_height_pilot_ft > 400 then
					if B738DR_fd_goaround == 0 then
						-- simCMD_disconnect:once()
						if simDR_radio_height_pilot_ft < 2000 and ap_goaround_block == 0 then
							fd_ga = 1
							if is_timer_scheduled(ap_at_off) == true then
								stop_timer(ap_at_off)
							end
						elseif simDR_radio_height_pilot_ft >= 2000 then
							if simDR_flaps_ratio > 0 or simDR_glideslope_status == 2 or ap_pitch_mode == 4 then
								fd_ga = 1
								if is_timer_scheduled(ap_at_off) == true then
									stop_timer(ap_at_off)
								end
							end
						end
						if fd_ga == 1 then
							simCMD_disconnect:once()
							B738DR_fd_goaround = 1		-- first push
							fd_ga_activate()
						end
					elseif B738DR_fd_goaround == 1 then
						B738DR_fd_goaround = 2
					end
				end
			end
		end
		
		if fd_goaround_mem == 0 and ap_goaround_mem == 0 and at_takeoff_mem == 0 then
			if B738DR_fd_goaround == 1 or B738DR_ap_goaround == 1 or B738DR_at_takeoff == 1 then
				ap_heading_buf = 0
				ap_heading_used = false
				ap_heading_timer = 0
				
				ap_altitude_buf = 0
				ap_altitude_used = false
				ap_altitude_timer = 0
				
				ap_speed_buf = 0
				ap_speed_used = false
				ap_speed_timer = 0
				
				fma_roll_timer = 0
				fma_pitch_timer = 0
				fma_speed_timer = 0
			end
		end
	end
end

function up_dn_baro_pilot(val_hdg, val_mode)
	local baro_value = 0
	local val_hdg_add = val_hdg
	if val_mode == 1 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 60
	elseif val_mode == 2 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 360
	end
	
	if B738DR_baro_sel_pilot_show == 0 and B738DR_baro_set_std_pilot == 1 then
		baro_value = 29.92 + val_hdg_add
	else
		baro_value = B738DR_baro_sel_in_hg_pilot + val_hdg_add
	end
	baro_value = math.min(baro_value, 32)
	baro_value = math.max(baro_value, 22)
	B738DR_baro_sel_in_hg_pilot = baro_value
	if B738DR_dspl_ctrl_pnl == -1 then
		B738DR_baro_sel_in_hg_copilot = B738DR_baro_sel_in_hg_pilot
	end
end

function B738_baro_pilot_ctr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_baro_knob_capt = 0
	end
end

function B738_baro_pilot_up_slow_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_baro_knob_capt = 1
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(add_value_baro, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				if B738DR_efis_baro_mode_capt_pfd == 0 then
					add_value_baro = 0.01
				else
					add_value_baro = 0.02953
				end
				up_dn_baro_pilot(add_value_baro, 1)
			end
		end
	else
		B738DR_baro_knob_capt = 1
	end
end

function B738_baro_pilot_up_fast_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 or phase == 1 then
			B738DR_baro_knob_capt = 2
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(add_value_baro * 4, 1)
		end
	else
		B738DR_baro_knob_capt = 2
	end
end

function B738_baro_pilot_dn_slow_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 then
			B738DR_baro_knob_capt = -1
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(-add_value_baro, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				if B738DR_efis_baro_mode_capt_pfd == 0 then
					add_value_baro = 0.01
				else
					add_value_baro = 0.02953
				end
				up_dn_baro_pilot(-add_value_baro, 1)
			end
		end
	else
		B738DR_baro_knob_capt = -1
	end
end

function B738_baro_pilot_dn_fast_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl < 1 then
		if phase == 0 or phase == 1 then
			B738DR_baro_knob_capt = -2
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(-add_value_baro * 4, 1)
		end
	else
		B738DR_baro_knob_capt = -2
	end
end

function B738CMD_baro_pilot_up_CMDhandler(phase, duration)
	local add_value_baro = 0
	if phase == 0 then
		B738DR_baro_knob_capt = 1
		if B738DR_efis_baro_mode_capt_pfd == 0 then
			add_value_baro = 0.01
		else
			add_value_baro = 0.02953
		end
		up_dn_baro_pilot(add_value_baro, 0)
	elseif phase == 1 then
		if duration > 3.5 then
			B738DR_baro_knob_capt = 2
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(add_value_baro * 4, 1)
		elseif duration > 0.3 then
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(add_value_baro, 1)
		end
	elseif phase == 2 then
		B738DR_baro_knob_capt = 0
	end
end

function B738CMD_baro_pilot_dn_CMDhandler(phase, duration)
	local add_value_baro = 0
	if phase == 0 then
		B738DR_baro_knob_capt = -1
		if B738DR_efis_baro_mode_capt_pfd == 0 then
			add_value_baro = 0.01
		else
			add_value_baro = 0.02953
		end
		up_dn_baro_pilot(-add_value_baro, 0)
	elseif phase == 1 then
		if duration > 3.5 then
			B738DR_baro_knob_capt = -2
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(-add_value_baro * 4, 1)
		elseif duration > 0.3 then
			if B738DR_efis_baro_mode_capt_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_pilot(-add_value_baro, 1)
		end
	elseif phase == 2 then
		B738DR_baro_knob_capt = 0
	end
end

function up_dn_baro_copilot(val_hdg, val_mode)
	local baro_value = 0
	local val_hdg_add = val_hdg
	if val_mode == 1 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 60
	elseif val_mode == 2 then
		val_hdg_add = val_hdg_add * cmd_sim_period * 360
	end
	
	if B738DR_baro_sel_copilot_show == 0 and B738DR_baro_set_std_copilot == 1 then
		baro_value = 29.92 + val_hdg_add
	else
		baro_value = B738DR_baro_sel_in_hg_copilot + val_hdg_add
	end
	baro_value = math.min(baro_value, 32)
	baro_value = math.max(baro_value, 22)
	B738DR_baro_sel_in_hg_copilot = baro_value
	if B738DR_dspl_ctrl_pnl == 1 then
		B738DR_baro_sel_in_hg_pilot = B738DR_baro_sel_in_hg_copilot
	end
end

function B738_baro_copilot_ctr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_baro_knob_fo = 0
	end
end

function B738_baro_copilot_up_slow_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_baro_knob_fo = 1
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(add_value_baro, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				if B738DR_efis_baro_mode_fo_pfd == 0 then
					add_value_baro = 0.01
				else
					add_value_baro = 0.02953
				end
				up_dn_baro_copilot(add_value_baro, 1)
			end
		end
	else
		B738DR_baro_knob_fo = 1
	end
end

function B738_baro_copilot_up_fast_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 or phase == 1 then
			B738DR_baro_knob_fo = 2
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(add_value_baro * 4, 1)
		end
	else
		B738DR_baro_knob_fo = 2
	end
end

function B738_baro_copilot_dn_slow_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 then
			B738DR_baro_knob_fo = -1
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(-add_value_baro, 0)
		elseif phase == 1 then
			if duration > 0.3 then
				if B738DR_efis_baro_mode_fo_pfd == 0 then
					add_value_baro = 0.01
				else
					add_value_baro = 0.02953
				end
				up_dn_baro_copilot(-add_value_baro, 1)
			end
		end
	else
		B738DR_baro_knob_fo = -1
	end
end

function B738_baro_copilot_dn_fast_CMDhandler(phase, duration)
	local add_value_baro = 0
	if B738DR_dspl_ctrl_pnl > -1 then
		if phase == 0 or phase == 1 then
			B738DR_baro_knob_fo = -2
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(-add_value_baro * 4, 1)
		end
	else
		B738DR_baro_knob_fo = -2
	end
end


function B738CMD_baro_copilot_up_CMDhandler(phase, duration)
	local add_value_baro = 0
	if phase == 0 then
		B738DR_baro_knob_fo = 1
		if B738DR_efis_baro_mode_fo_pfd == 0 then
			add_value_baro = 0.01
		else
			add_value_baro = 0.02953
		end
		up_dn_baro_copilot(add_value_baro, 0)
	elseif phase == 1 then
		if duration > 3.5 then
			B738DR_baro_knob_fo = 2
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(add_value_baro * 4, 1)
		elseif duration > 0.5 then
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(add_value_baro, 1)
		end
	elseif phase == 2 then
		B738DR_baro_knob_fo = 0
	end
end

function B738CMD_baro_copilot_dn_CMDhandler(phase, duration)
	local add_value_baro = 0
	if phase == 0 then
		B738DR_baro_knob_fo = -1
		if B738DR_efis_baro_mode_capt_pfd == 0 then
			add_value_baro = 0.01
		else
			add_value_baro = 0.02953
		end
		up_dn_baro_copilot(-add_value_baro, 0)
	elseif phase == 1 then
		if duration > 3.5 then
			B738DR_baro_knob_fo = -2
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(-add_value_baro * 4, 1)
		elseif duration > 0.5 then
			if B738DR_efis_baro_mode_fo_pfd == 0 then
				add_value_baro = 0.01
			else
				add_value_baro = 0.02953
			end
			up_dn_baro_copilot(-add_value_baro, 1)
		end
	elseif phase == 2 then
		B738DR_baro_knob_fo = 0
	end
end

--- SPD INTV ---
function B738_autopilot_spd_interv_CMDhandler(phase, duration)
	if phase == 0 then
		local const_spd = 0
		local mach_to_ias = 0
		B738DR_autopilot_spd_interv_pos = 1
		if B738DR_spd_intv_but ~= 0 then
			if B738DR_autopilot_vnav_status == 1 and vnav_engaged == 1 then
				if B738DR_ap_spd_interv_status == 0 then
					B738DR_ap_spd_interv_status = 1
					if B738DR_flight_phase > 4 then
						vnav_desc_spd = 1
						if B738DR_vnav_desc_spd_disable == 0 then
							if math.abs(B738DR_vnav_alt_err) < 500 and (B738DR_descent_mode == 0 or B738DR_descent_mode == 2) and B738DR_vnav_common == 0 then
								B738DR_descent_mode = 2
								vnav_desc_spd = 0
							else
								B738DR_descent_mode = 3
							end
						else
							B738DR_descent_mode = 2
							vnav_desc_spd = 0
						end
						vnav_desc_protect_spd = 0
					end
					
				else
					B738DR_ap_spd_interv_status = 0
					spd_interv_co_used = 0
					vnav_desc_spd = 0
					if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
						B738DR_descent_mode = 0
					else
						B738DR_descent_mode = 2
					end
					vnav_desc_protect_spd = 0
					
					if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
						const_spd = 1
						if simDR_IAS > 45 then
							const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
						end
						mach_to_ias = (B738DR_fmc_climb_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
						if simDR_airspeed_is_mach == 0 then
							if mach_to_ias <= B738DR_fmc_climb_speed and mach_to_ias <= simDR_IAS then
								simCMD_autopilot_co:once()
							end
						else
							if mach_to_ias > B738DR_fmc_climb_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
								simCMD_autopilot_co:once()
							end
						end
					elseif B738DR_flight_phase == 2 then
						const_spd = 1
						if simDR_IAS > 45 then
							const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
						end
						mach_to_ias = (B738DR_fmc_cruise_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
						if simDR_airspeed_is_mach == 0 then
							if mach_to_ias <= B738DR_fmc_cruise_speed and mach_to_ias <= simDR_IAS then
								simCMD_autopilot_co:once()
							end
						else
							if mach_to_ias > B738DR_fmc_cruise_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
								simCMD_autopilot_co:once()
							end
						end
					else
						-- crossover altitude
						const_spd = 1
						if simDR_IAS > 45 then
							const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
						end
						mach_to_ias = (B738DR_fmc_descent_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
						if simDR_airspeed_is_mach == 0 then
							if mach_to_ias <= B738DR_fmc_descent_speed and mach_to_ias <= simDR_IAS then
								simCMD_autopilot_co:once()
							end
						else
							if mach_to_ias > B738DR_fmc_descent_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
								simCMD_autopilot_co:once()
							end
						end
					end
					
				end
			end
		end
	elseif phase == 2 then
		B738DR_autopilot_spd_interv_pos = 0
	end
end

function B738_ap_alt_up_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_ap_altitude_up:once()
	end
end

function B738_ap_alt_dn_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_ap_altitude_dn:once()
	end
end

function B738_ap_crs_pilot_up_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_course_pilot_up:once()
	end
end

function B738_ap_crs_pilot_dn_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_course_pilot_dn:once()
	end
end

function B738_ap_crs_copilot_up_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_course_copilot_up:once()
	end
end

function B738_ap_crs_copilot_dn_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_course_copilot_dn:once()
	end
end

function B738_ap_left_toga_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_left_toga_pos = 1
		simCMD_take_off_go_around:once()
	elseif phase == 2 then
		B738DR_autopilot_left_toga_pos = 0
	end
end

function B738_ap_right_toga_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_right_toga_pos = 1
		simCMD_take_off_go_around:once()
	elseif phase == 2 then
		B738DR_autopilot_right_toga_pos = 0
	end
end

function B738_ap_left_atds_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_left_at_diseng_pos = 1
		if B738DR_autopilot_autothr_arm_pos == 1 then
			B738DR_autopilot_autothr_arm_pos = 0
			autopilot_autothrottle_status = 0
			at_mode = 0
			simCMD_autothrottle_discon:once()	-- disconnect autothrotle
		end
		at_left_press = 1
	elseif phase == 2 then
		B738DR_autopilot_left_at_diseng_pos = 0
	end
end

function B738_ap_right_atds_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_autopilot_right_at_diseng_pos = 1
		if B738DR_autopilot_autothr_arm_pos == 1 then
			B738DR_autopilot_autothr_arm_pos = 0
			autopilot_autothrottle_status = 0
			at_mode = 0
			simCMD_autothrottle_discon:once()	-- disconnect autothrotle
		end
		at_right_press = 1
	elseif phase == 2 then
		B738DR_autopilot_right_at_diseng_pos = 0
	end
end


function B738_mic_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_mic_pos = 1
		simCMD_take_off_go_around:once()
	elseif phase == 2 then
		B738DR_mic_pos = 0
	end
end

function B738_mic_fo_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_mic_fo_pos = 1
		simCMD_take_off_go_around:once()
	elseif phase == 2 then
		B738DR_mic_fo_pos = 0
	end
end

function B738_ap_capt_dis_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_yoke_capt_ap_disengage = 1
		if ap_capt_disco_status == 0 then
			
			if autopilot_cmd_a_status == 1 or autopilot_cmd_b_status == 1 
			or autopilot_cws_a_status == 1 or autopilot_cws_b_status == 1 then
				ap_disco2 = 1
			end
			
			ap_on = 0
			ap1_on = 0
			ap2_on = 0
			
			simDR_flight_dir_mode = 1
			autopilot_cmd_a_status = 0
			autopilot_cmd_b_status = 0
			autopilot_cws_a_status = 0
			autopilot_cws_b_status = 0
			autopilot_cws_status = 0
			autopilot_cws_r_status = 0
			autopilot_cws_p_status = 0
			if B738DR_autoland_status == 1 then
				B738DR_autoland_status = 0
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				vorloc_only = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				if is_timer_scheduled(annun_flare) == true then
					stop_timer(annun_flare)
				end
				ap_disco2 = 1
				
				if ap_roll_mode == 9 or ap_roll_mode == 19 then
					ap_roll_mode = 3	-- APP
					ap_roll_mode_eng = 3	-- APP
				end
				if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
					ap_pitch_mode = 4	-- GS
					ap_pitch_mode_eng = 4	-- GS
				end
			end
			ap_capt_disco_status = 1
		else
			B738DR_ap_light_pilot = 1
			ap_capt_disco_status = 0
		end
	elseif phase == 2 then
		B738DR_yoke_capt_ap_disengage = 0
		B738DR_ap_light_pilot = 0
	end
end

function B738_ap_fo_dis_press_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_yoke_fo_ap_disengage = 1
		if ap_fo_disco_status == 0 then
			
			if autopilot_cmd_a_status == 1 or autopilot_cmd_b_status == 1 
			or autopilot_cws_a_status == 1 or autopilot_cws_b_status == 1 then
				ap_disco2 = 1
			end
			
			ap_on = 0
			ap1_on = 0
			ap2_on = 0
			
			simDR_flight_dir_mode = 1
			autopilot_cmd_a_status = 0
			autopilot_cmd_b_status = 0
			autopilot_cws_a_status = 0
			autopilot_cws_b_status = 0
			autopilot_cws_status = 0
			autopilot_cws_r_status = 0
			autopilot_cws_p_status = 0
			
			if B738DR_autoland_status == 1 then
				B738DR_autoland_status = 0
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				vorloc_only = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				if is_timer_scheduled(annun_flare) == true then
					stop_timer(annun_flare)
				end
				ap_disco2 = 1
				
				if ap_roll_mode == 9 or ap_roll_mode == 19 then
					ap_roll_mode = 3	-- APP
					ap_roll_mode_eng = 3	-- APP
				end
				if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
					ap_pitch_mode = 4	-- GS
					ap_pitch_mode_eng = 4	-- GS
				end
			end
			ap_fo_disco_status = 1
		else
			B738DR_ap_light_fo = 1
			ap_fo_disco_status = 0
		end
	elseif phase == 2 then
		B738DR_yoke_fo_ap_disengage = 0
		B738DR_ap_light_fo = 0
	end
end

function B738CMD_fd_cam_left_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_fd_cam_pos == 1 then
			B738DR_fd_cam_pos = 0
		elseif B738DR_fd_cam_pos == 0 then
			B738DR_fd_cam_pos = -1
		else
			B738DR_fd_cam_pos = -1
		end
	end
end

function B738CMD_fd_cam_right_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_fd_cam_pos == -1 then
			B738DR_fd_cam_pos = 0
		elseif B738DR_fd_cam_pos == 0 then
			B738DR_fd_cam_pos = 1
		else
			B738DR_fd_cam_pos = 1
		end
	end
end

function B738CMD_fd_cam_act_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_fd_cam_push_pos = 1
		if B738DR_fd_cam == 0 and B738DR_dc_bus2_status ~= 0 then
			if B738DR_fd_cam_pos == -1 then
				-- left
				if door_ratio_fwd_L == 0 then
					B738DR_fd_cam = 1
				elseif B738DR_hide_airstairs == 0 then
					B738DR_fd_cam = 7
				else
					B738DR_fd_cam = 4
				end
			elseif B738DR_fd_cam_pos == 0 then
				-- center
				if B738DR_flt_dk_door_ratio == 0 then
					B738DR_fd_cam = 2
				else
					B738DR_fd_cam = 5
				end
			else
				-- right
				if door_ratio_fwd_R == 0 then
					B738DR_fd_cam = 3
				else
					B738DR_fd_cam = 6
				end
			end
			if B738DR_lowerDU_page == 1 then
				B738DR_lowerDU_page = 0
			end
			B738DR_lowerDU_page2 = 0
		else
			B738DR_fd_cam = 0
		end
	elseif phase == 2 then
		B738DR_fd_cam_push_pos = 0
	end
end

function B738_allign_analog_horiz_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_allign_analog_horiz_pos = 1
	elseif phase == 2 then
		B738DR_allign_analog_horiz_pos = 0
	end
end

function B738CMD_cb_d9_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_cb_d9 == 0 then
			B738DR_cb_d9 = 1
		else
			B738DR_cb_d9 = 0
		end
	end
end

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

B738CMD_allign_analog_horiz			= create_command("laminar/B738/button/allign_analog_horiz", "Allign Horizon", B738_allign_analog_horiz_CMDhandler)

B738CMD_mic					= create_command("laminar/B738/push_button/mic", "MIC (TO/GA button)", B738_mic_CMDhandler)
B738CMD_mic_fo				= create_command("laminar/B738/push_button/mic_fo", "MIC FO (TO/GA button)", B738_mic_fo_CMDhandler)

B738CMD_autopilot_left_toga_press	= create_command("laminar/B738/autopilot/left_toga_press", "Left TO/GA", B738_ap_left_toga_press_CMDhandler)
B738CMD_autopilot_right_toga_press	= create_command("laminar/B738/autopilot/right_toga_press", "Right TO/GA", B738_ap_right_toga_press_CMDhandler)

B738CMD_autopilot_left_atds_press	= create_command("laminar/B738/autopilot/left_at_dis_press", "Left A/T disengage", B738_ap_left_atds_press_CMDhandler)
B738CMD_autopilot_right_atds_press	= create_command("laminar/B738/autopilot/right_at_dis_press", "Right A/T disengage", B738_ap_right_atds_press_CMDhandler)

B738CMD_autopilot_capt_disco_press	= create_command("laminar/B738/autopilot/capt_disco_press", "Captain A/P disengage", B738_ap_capt_dis_press_CMDhandler)
B738CMD_autopilot_fo_disco_press	= create_command("laminar/B738/autopilot/fo_disco_press", "FO A/P disengage", B738_ap_fo_dis_press_CMDhandler)


-- CAPT and F/O minimums
B738CMD_fo_minimums_up = create_command("laminar/B738/EFIS_control/fo/minimums_up", "FO Minimums select up", B738_fo_minimums_up_CMDhandler)
B738CMD_fo_minimums_dn = create_command("laminar/B738/EFIS_control/fo/minimums_dn", "FO Minimums select down", B738_fo_minimums_dn_CMDhandler)
B738CMD_cpt_minimums_up = create_command("laminar/B738/EFIS_control/cpt/minimums_up", "CAPT Minimums select up", B738_cpt_minimums_up_CMDhandler)
B738CMD_cpt_minimums_dn = create_command("laminar/B738/EFIS_control/cpt/minimums_dn", "CAPT Minimums select down", B738_cpt_minimums_dn_CMDhandler)

-- CAPT EFIS COMMANDS
B738CMD_dh_pilot_up			= create_command("laminar/B738/pfd/dh_pilot_up", "Captain DH up", B738_dh_pilot_up_CMDhandler)
B738CMD_dh_pilot_dn			= create_command("laminar/B738/pfd/dh_pilot_dn", "Captain DH down", B738_dh_pilot_dn_CMDhandler)
B738CMD_dh_pilot_ctr			= create_command("laminar/B738/pfd/dh_pilot_ctr", "Captain DH ctr", B738_dh_pilot_ctr_CMDhandler)
B738CMD_dh_pilot_up_slow		= create_command("laminar/B738/pfd/dh_pilot_up_slow", "Captain DH up slow", B738_dh_pilot_up_slow_CMDhandler)
B738CMD_dh_pilot_up_fast		= create_command("laminar/B738/pfd/dh_pilot_up_fast", "Captain DH up fast", B738_dh_pilot_up_fast_CMDhandler)
B738CMD_dh_pilot_dn_slow		= create_command("laminar/B738/pfd/dh_pilot_dn_slow", "Captain DH down slow", B738_dh_pilot_dn_slow_CMDhandler)
B738CMD_dh_pilot_dn_fast		= create_command("laminar/B738/pfd/dh_pilot_dn_fast", "Captain DH down fast", B738_dh_pilot_dn_fast_CMDhandler)

B738CMD_efis_wxr_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/wxr_press", "CAPT EFIS Weather", B738_efis_wxr_capt_CMDhandler)
B738CMD_efis_sta_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/sta_press", "CAPT EFIS Station", B738_efis_sta_capt_CMDhandler)
B738CMD_efis_wpt_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/wpt_press", "CAPT EFIS Waypoint", B738_efis_wpt_capt_CMDhandler)
B738CMD_efis_arpt_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/arpt_press", "CAPT EFIS Airport", B738_efis_arpt_capt_CMDhandler)
B738CMD_efis_data_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/data_press", "CAPT EFIS DATA", B738_efis_data_capt_CMDhandler)
B738CMD_efis_pos_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/pos_press", "CAPT EFIS Position", B738_efis_pos_capt_CMDhandler)
B738CMD_efis_terr_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/terr_press", "CAPT EFIS Terrain", B738_efis_terr_capt_CMDhandler)

B738CMD_efis_rst_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/rst_press", "CAPT EFIS Reset", B738_efis_rst_capt_CMDhandler)
B738CMD_efis_ctr_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/ctr_press", "CAPT EFIS Center", B738_efis_ctr_capt_CMDhandler)
B738CMD_efis_tfc_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/tfc_press", "CAPT EFIS Traffic", B738_efis_tfc_capt_CMDhandler)
B738CMD_efis_std_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/std_press", "CAPT EFIS Standard", B738_efis_std_capt_CMDhandler)

B738CMD_efis_mtrs_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/mtrs_press", "CAPT ALT in Meters", B738_efis_mtrs_capt_CMDhandler)
B738CMD_efis_fpv_capt 		= create_command("laminar/B738/EFIS_control/capt/push_button/fpv_press", "CAPT Flight Path Vector", B738_efis_fpv_capt_CMDhandler)

B738CMD_efis_baro_mode_capt_up 	= create_command("laminar/B738/EFIS_control/capt/baro_in_hpa_up", "CAPT Baro Mode HPA", B738_efis_baro_mode_capt_up_CMDhandler)
B738CMD_efis_baro_mode_capt_dn 	= create_command("laminar/B738/EFIS_control/capt/baro_in_hpa_dn", "CAPT Baro Mode IN", B738_efis_baro_mode_capt_dn_CMDhandler)

B738CMD_efis_vor1_capt_up 		= create_command("laminar/B738/EFIS_control/capt/vor1_off_up", "CAPT VOR1 Up", B738_efis_vor1_capt_up_CMDhandler)
B738CMD_efis_vor1_capt_dn 		= create_command("laminar/B738/EFIS_control/capt/vor1_off_dn", "CAPT VOR1 Down", B738_efis_vor1_capt_dn_CMDhandler)
B738CMD_efis_vor2_capt_up 		= create_command("laminar/B738/EFIS_control/capt/vor2_off_up", "CAPT VOR1 Up", B738_efis_vor2_capt_up_CMDhandler)
B738CMD_efis_vor2_capt_dn 		= create_command("laminar/B738/EFIS_control/capt/vor2_off_dn", "CAPT VOR1 Down", B738_efis_vor2_capt_dn_CMDhandler)

B738CMD_efis_map_range_capt_up	= create_command("laminar/B738/EFIS_control/capt/map_range_up", "CAPT EFIS Map range Up", B738_efis_map_range_capt_up_CMDhandler)
B738CMD_efis_map_range_capt_dn	= create_command("laminar/B738/EFIS_control/capt/map_range_dn", "CAPT EFIS Map range Down", B738_efis_map_range_capt_dn_CMDhandler)

B738CMD_efis_map_mode_capt_up 	= create_command("laminar/B738/EFIS_control/capt/map_mode_up", "CAPT MAP mode Up", B738_efis_map_mode_capt_up_CMDhandler)
B738CMD_efis_map_mode_capt_dn 	= create_command("laminar/B738/EFIS_control/capt/map_mode_dn", "CAPT MAP mode Down", B738_efis_map_mode_capt_dn_CMDhandler)


-- FO EFIS COMMANDS
B738CMD_dh_copilot_up		= create_command("laminar/B738/pfd/dh_copilot_up", "First Officer DH up", B738_dh_copilot_up_CMDhandler)
B738CMD_dh_copilot_dn		= create_command("laminar/B738/pfd/dh_copilot_dn", "First Officer DH down", B738_dh_copilot_dn_CMDhandler)
B738CMD_dh_copilot_ctr			= create_command("laminar/B738/pfd/dh_copilot_ctr", "First Officer DH ctr", B738_dh_copilot_ctr_CMDhandler)
B738CMD_dh_copilot_up_slow		= create_command("laminar/B738/pfd/dh_copilot_up_slow", "First Officer DH up slow", B738_dh_copilot_up_slow_CMDhandler)
B738CMD_dh_copilot_up_fast		= create_command("laminar/B738/pfd/dh_copilot_up_fast", "First Officer DH up fast", B738_dh_copilot_up_fast_CMDhandler)
B738CMD_dh_copilot_dn_slow		= create_command("laminar/B738/pfd/dh_copilot_dn_slow", "First Officer DH down slow", B738_dh_copilot_dn_slow_CMDhandler)
B738CMD_dh_copilot_dn_fast		= create_command("laminar/B738/pfd/dh_copilot_dn_fast", "First Officer DH down fast", B738_dh_copilot_dn_fast_CMDhandler)

B738CMD_efis_wxr_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/wxr_press", "fo EFIS Weather", B738_efis_wxr_fo_CMDhandler)
B738CMD_efis_sta_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/sta_press", "fo EFIS Station", B738_efis_sta_fo_CMDhandler)
B738CMD_efis_wpt_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/wpt_press", "fo EFIS Waypoint", B738_efis_wpt_fo_CMDhandler)
B738CMD_efis_arpt_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/arpt_press", "fo EFIS Airport", B738_efis_arpt_fo_CMDhandler)
B738CMD_efis_data_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/data_press", "fo EFIS DATA", B738_efis_data_fo_CMDhandler)
B738CMD_efis_pos_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/pos_press", "fo EFIS Position", B738_efis_pos_fo_CMDhandler)
B738CMD_efis_terr_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/terr_press", "fo EFIS Terrain", B738_efis_terr_fo_CMDhandler)

B738CMD_efis_rst_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/rst_press", "fo EFIS Reset", B738_efis_rst_fo_CMDhandler)
B738CMD_efis_ctr_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/ctr_press", "fo EFIS Center", B738_efis_ctr_fo_CMDhandler)
B738CMD_efis_tfc_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/tfc_press", "fo EFIS Traffic", B738_efis_tfc_fo_CMDhandler)
B738CMD_efis_std_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/std_press", "fo EFIS Standard", B738_efis_std_fo_CMDhandler)

B738CMD_efis_mtrs_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/mtrs_press", "fo ALT in Meters", B738_efis_mtrs_fo_CMDhandler)
B738CMD_efis_fpv_fo 		= create_command("laminar/B738/EFIS_control/fo/push_button/fpv_press", "fo Flight Path Vector", B738_efis_fpv_fo_CMDhandler)

B738CMD_efis_baro_mode_fo_up 	= create_command("laminar/B738/EFIS_control/fo/baro_in_hpa_up", "fo Baro Mode HPA", B738_efis_baro_mode_fo_up_CMDhandler)
B738CMD_efis_baro_mode_fo_dn 	= create_command("laminar/B738/EFIS_control/fo/baro_in_hpa_dn", "fo Baro Mode IN", B738_efis_baro_mode_fo_dn_CMDhandler)

B738CMD_efis_vor1_fo_up 		= create_command("laminar/B738/EFIS_control/fo/vor1_off_up", "fo VOR1 Up", B738_efis_vor1_fo_up_CMDhandler)
B738CMD_efis_vor1_fo_dn 		= create_command("laminar/B738/EFIS_control/fo/vor1_off_dn", "fo VOR1 Down", B738_efis_vor1_fo_dn_CMDhandler)

B738CMD_efis_vor2_fo_up 		= create_command("laminar/B738/EFIS_control/fo/vor2_off_up", "fo VOR1 Up", B738_efis_vor2_fo_up_CMDhandler)
B738CMD_efis_vor2_fo_dn 		= create_command("laminar/B738/EFIS_control/fo/vor2_off_dn", "fo VOR1 Down", B738_efis_vor2_fo_dn_CMDhandler)

B738CMD_efis_map_mode_fo_up 	= create_command("laminar/B738/EFIS_control/fo/map_mode_up", "FO MAP mode Up", B738_efis_map_mode_fo_up_CMDhandler)
B738CMD_efis_map_mode_fo_dn 	= create_command("laminar/B738/EFIS_control/fo/map_mode_dn", "FO MAP mode Down", B738_efis_map_mode_fo_dn_CMDhandler)

B738CMD_efis_map_range_fo_up	= create_command("laminar/B738/EFIS_control/fo/map_range_up", "FO EFIS Map range Up", B738_efis_map_range_fo_up_CMDhandler)
B738CMD_efis_map_range_fo_dn	= create_command("laminar/B738/EFIS_control/fo/map_range_dn", "FO EFIS Map range Down", B738_efis_map_range_fo_dn_CMDhandler)

----- AP COMMANDS

B738CMD_autopilot_n1_press				= create_command("laminar/B738/autopilot/n1_press", "N1 Mode", B738_autopilot_n1_press_CMDhandler)
B738CMD_autopilot_speed_press			= create_command("laminar/B738/autopilot/speed_press", "Speed Mode", B738_autopilot_speed_press_CMDhandler)
B738CMD_autopilot_lvl_chg_press			= create_command("laminar/B738/autopilot/lvl_chg_press", "Level Change Mode", B738_autopilot_lvl_chg_press_CMDhandler)
B738CMD_autopilot_vnav_press			= create_command("laminar/B738/autopilot/vnav_press", "Vertical NAV Mode", B738_autopilot_vnav_press_CMDhandler)
B738CMD_autopilot_co_press				= create_command("laminar/B738/autopilot/change_over_press", "IAS MACH Change Over", B738_autopilot_co_press_CMDhandler)

B738CMD_autopilot_lnav_press			= create_command("laminar/B738/autopilot/lnav_press", "FMS Lateral NAV Mode", B738_autopilot_lnav_press_CMDhandler)
B738CMD_autopilot_vorloc_press			= create_command("laminar/B738/autopilot/vorloc_press", "VOR Localizer Mode", B738_autopilot_vorloc_press_CMDhandler)
B738CMD_autopilot_app_press				= create_command("laminar/B738/autopilot/app_press", "Approach Mode", B738_autopilot_app_press_CMDhandler)
B738CMD_autopilot_hdg_sel_press			= create_command("laminar/B738/autopilot/hdg_sel_press", "Heading Select Mode", B738_autopilot_hdg_sel_press_CMDhandler)

B738CMD_autopilot_alt_hld_press			= create_command("laminar/B738/autopilot/alt_hld_press", "Altitude Hold Mode", B738_autopilot_alt_hld_press_CMDhandler)
B738CMD_autopilot_vs_press				= create_command("laminar/B738/autopilot/vs_press", "Vertical Speed Mode", B738_autopilot_vs_press_CMDhandler)

B738CMD_autopilot_disconnect_toggle		= create_command("laminar/B738/autopilot/disconnect_toggle", "AP Disconnect", B738_autopilot_disconnect_toggle_CMDhandler)
B738CMD_autopilot_autothr_arm_toggle	= create_command("laminar/B738/autopilot/autothrottle_arm_toggle", "Autothrottle ARM", B738_autopilot_autothr_arm_toggle_CMDhandler)
B738CMD_autopilot_flight_dir_toggle		= create_command("laminar/B738/autopilot/flight_director_toggle", "Flight Director", B738_autopilot_flight_dir_toggle_CMDhandler)
B738CMD_autopilot_flight_dir_fo_toggle	= create_command("laminar/B738/autopilot/flight_director_fo_toggle", "First Officer Flight Director", B738_autopilot_flight_dir_fo_toggle_CMDhandler)
B738CMD_autopilot_bank_angle_up			= create_command("laminar/B738/autopilot/bank_angle_up", "Bank Angle Increase", B738_autopilot_bank_angle_up_CMDhandler)
B738CMD_autopilot_bank_angle_dn			= create_command("laminar/B738/autopilot/bank_angle_dn", "Bank Angle Decrease", B738_autopilot_bank_angle_dn_CMDhandler)

B738CMD_autopilot_cmd_a_press			= create_command("laminar/B738/autopilot/cmd_a_press", "Command A", B738_autopilot_cmd_a_press_CMDhandler)
B738CMD_autopilot_cmd_b_press			= create_command("laminar/B738/autopilot/cmd_b_press", "Command B", B738_autopilot_cmd_b_press_CMDhandler)
B738CMD_autopilot_disconect_button		= create_command("laminar/B738/autopilot/disconnect_button", "AP Disconnect button", B738_autopilot_disconect_button_CMDhandler)

B738CMD_autopilot_cws_a_press			= create_command("laminar/B738/autopilot/cws_a_press", "Control Wheel Steering A", B738_autopilot_cws_a_press_CMDhandler)
B738CMD_autopilot_cws_b_press			= create_command("laminar/B738/autopilot/cws_b_press", "Control Wheel Steering B", B738_autopilot_cws_b_press_CMDhandler)

B738CMD_vhf_nav_source_switch_lft		= create_command("laminar/B738/toggle_switch/vhf_nav_source_lft", "VHF SOURCE LEFT", B738_vhf_nav_source_switch_lft_CMDhandler)
B738CMD_vhf_nav_source_switch_rgt		= create_command("laminar/B738/toggle_switch/vhf_nav_source_rgt", "VHF SOURCE RIGHT", B738_vhf_nav_source_switch_rgt_CMDhandler)


B738CMD_mfd_sys		 		= create_command("laminar/B738/LDU_control/push_button/MFD_SYS", "MFD SYS", B738_mfd_sys_CMDhandler)
B738CMD_mfd_eng		 		= create_command("laminar/B738/LDU_control/push_button/MFD_ENG", "MFD ENG", B738_mfd_eng_CMDhandler)

-- A/P, A/T light buttons
B738CMD_ap_light_pilot		 	= create_command("laminar/B738/push_button/ap_light_pilot", "A/P light captain button", B738_ap_light_pilot_CMDhandler)
B738CMD_at_light_pilot		 	= create_command("laminar/B738/push_button/at_light_pilot", "A/T light captain button", B738_at_light_pilot_CMDhandler)
B738CMD_ap_light_fo		 		= create_command("laminar/B738/push_button/ap_light_fo", "A/P light FO button", B738_ap_light_fo_CMDhandler)
B738CMD_at_light_fo		 		= create_command("laminar/B738/push_button/at_light_fo", "A/T light FO button", B738_at_light_fo_CMDhandler)

B738CMD_baro_pilot_up 		= create_command("laminar/B738/pilot/barometer_up", "Captain Barometer Up", B738CMD_baro_pilot_up_CMDhandler)
B738CMD_baro_pilot_dn 		= create_command("laminar/B738/pilot/barometer_down", "Captain Barometer Down", B738CMD_baro_pilot_dn_CMDhandler)
B738CMD_baro_pilot_ctr			= create_command("laminar/B738/pilot/barometer_ctr", "Captain DH ctr", B738_dh_pilot_ctr_CMDhandler)
B738CMD_baro_pilot_up_slow		= create_command("laminar/B738/pilot/barometer_up_slow", "Captain DH up slow", B738_baro_pilot_up_slow_CMDhandler)
B738CMD_baro_pilot_up_fast		= create_command("laminar/B738/pilot/barometer_up_fast", "Captain DH up fast", B738_baro_pilot_up_fast_CMDhandler)
B738CMD_baro_pilot_dn_slow		= create_command("laminar/B738/pilot/barometer_dn_slow", "Captain DH down slow", B738_baro_pilot_dn_slow_CMDhandler)
B738CMD_baro_pilot_dn_fast		= create_command("laminar/B738/pilot/barometer_dn_fast", "Captain DH down fast", B738_baro_pilot_dn_fast_CMDhandler)

B738CMD_baro_copilot_up 		= create_command("laminar/B738/copilot/barometer_up", "First Officer Barometer Up", B738CMD_baro_copilot_up_CMDhandler)
B738CMD_baro_copilot_dn 		= create_command("laminar/B738/copilot/barometer_down", "First Officer Barometer Down", B738CMD_baro_copilot_dn_CMDhandler)
B738CMD_baro_copilot_ctr			= create_command("laminar/B738/copilot/barometer_ctr", "First Officer Barometer ctr", B738_dh_copilot_ctr_CMDhandler)
B738CMD_baro_copilot_up_slow		= create_command("laminar/B738/copilot/barometer_up_slow", "First Officer Barometer up slow", B738_baro_copilot_up_slow_CMDhandler)
B738CMD_baro_copilot_up_fast		= create_command("laminar/B738/copilot/barometer_up_fast", "First Officer Barometer up fast", B738_baro_copilot_up_fast_CMDhandler)
B738CMD_baro_copilot_dn_slow		= create_command("laminar/B738/copilot/barometer_dn_slow", "First Officer Barometer down slow", B738_baro_copilot_dn_slow_CMDhandler)
B738CMD_baro_copilot_dn_fast		= create_command("laminar/B738/copilot/barometer_dn_fast", "First Officer Barometer down fast", B738_baro_copilot_dn_fast_CMDhandler)

B738CMD_autopilot_spd_interv		= create_command("laminar/B738/autopilot/spd_interv", "SPD intervention", B738_autopilot_spd_interv_CMDhandler)

B738CMD_fd_cam_left 		= create_command("laminar/B738/fd_cam_left", "Flightdeck cam left", B738CMD_fd_cam_left_CMDhandler)
B738CMD_fd_cam_right 		= create_command("laminar/B738/fd_cam_right", "Flightdeck cam right", B738CMD_fd_cam_right_CMDhandler)
B738CMD_fd_cam_act 			= create_command("laminar/B738/fd_cam_act", "Flightdeck cam activate toggle", B738CMD_fd_cam_act_CMDhandler)

B738CMD_cb_d9				= create_command("laminar/B738/toggle_switch/cb/d9", "Toggle Circuit breaker D-9", B738CMD_cb_d9_CMDhandler)

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

function vert_spd_down()
	if simDR_ap_vvi_dial > -7900 then
		if simDR_ap_vvi_dial >= -950 and simDR_ap_vvi_dial < 1100 then
			simDR_ap_vvi_dial = simDR_ap_vvi_dial - 50
			simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 50)
		else
			simDR_ap_vvi_dial = simDR_ap_vvi_dial - 100
			simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 100)
		end
	end
	if simDR_ap_vvi_dial < -7900 then
		simDR_ap_vvi_dial = -7900
	end
end

function vert_spd_up()
	if simDR_ap_vvi_dial < 6000 then
		if simDR_ap_vvi_dial > -1100 and simDR_ap_vvi_dial <= 950 then
			simDR_ap_vvi_dial = simDR_ap_vvi_dial + 50
			simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 50)
		else
			simDR_ap_vvi_dial = simDR_ap_vvi_dial + 100
			simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 100)
		end
	end
	if simDR_ap_vvi_dial > 6000 then
		simDR_ap_vvi_dial = 6000
	end
end

function B738_vert_spd_down_CMDhandler(phase, duration)
	if phase == 0 then
		vert_spd_down()
		vert_spd_timer = 0
		vert_spd_timer2 = 0
	elseif phase == 1 then
		if vert_spd_timer > 0.8 then
			vert_spd_timer2 = vert_spd_timer2 + SIM_PERIOD
			if vert_spd_timer2 > 0.05 then
				vert_spd_timer2 = 0
				vert_spd_down()
			end
		else
			vert_spd_timer = vert_spd_timer + SIM_PERIOD
		end
	else
		vert_spd_timer = 0
		vert_spd_timer2 = 0
	end
end

function B738_vert_spd_up_CMDhandler(phase, duration)
	if phase == 0 then
		vert_spd_up()
		vert_spd_timer = 0
		vert_spd_timer2 = 0
	elseif phase == 1 then
		if vert_spd_timer > 0.8 then
			vert_spd_timer2 = vert_spd_timer2 + SIM_PERIOD
			if vert_spd_timer2 > 0.05 then
				vert_spd_timer2 = 0
				vert_spd_up()
			end
		else
			vert_spd_timer = vert_spd_timer + SIM_PERIOD
		end
	else
		vert_spd_timer = 0
		vert_spd_timer2 = 0
	end
end

function B738_heading_sync_CMDhandler(phase, duration)
	--hdgsk
	if phase == 0 then
		B738DR_mcp_hdg_dial = simDR_ahars_mag_hdg
		B738DR_mcp_hdg_dial = math.floor(B738DR_mcp_hdg_dial + 0.5)
		if B738DR_mcp_hdg_dial >= 359.5 then
			B738DR_mcp_hdg_dial = 0
		end
	end
end

function B738_ap_heading_up2_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_ap_heading_up:once()
	end
end

function B738_ap_heading_dn2_CMDhandler(phase, duration)
	if phase == 0 then
		B738CMD_ap_heading_dn:once()
	end
end

function B738_disco_handler(phase, duration)
	if phase == 0 then
		simDR_flight_dir_mode = 1
		autopilot_cmd_a_status = 0
		autopilot_cmd_b_status = 0
		autopilot_cws_a_status = 0
		autopilot_cws_b_status = 0
		autopilot_cws_status = 0
		autopilot_cws_r_status = 0
		autopilot_cws_p_status = 0
		B738DR_flare_status = 0
		B738DR_rollout_status = 0
		B738DR_no_autoland = 0
		B738DR_retard_status = 0
		B738DR_single_ch_status = 0
		vorloc_only = 0
		ils_test_enable = 0
		ils_test_on = 0
		ils_test_ok2 = 0
		if B738DR_autoland_status ~= 0 then
			B738DR_autoland_status = 0
			if ap_roll_mode == 9 or ap_roll_mode == 19 then
				ap_roll_mode = 3	-- APP
				ap_roll_mode_eng = 3	-- APP
			end
			if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
				ap_pitch_mode = 4	-- GS
				ap_pitch_mode_eng = 4	-- GS
			end
		end
		if is_timer_scheduled(annun_flare) == true then
			stop_timer(annun_flare)
		end
		ap_on = 0
		ap1_on = 0
		ap2_on = 0
		autopilot_side = autopilot_master_first
		B738DR_enable_servo_elev_pitch = 0
	end
end

--*************************************************************************************--
--** 				             REPLACE X-PLANE COMMANDS                  	    	 **--
--*************************************************************************************--

simCMD_take_off_go_around	= replace_command("sim/autopilot/take_off_go_around", B738_to_ga2_CMDhandler)
simCMD_take_off_go_around2	= replace_command("sim/engines/TOGA_power", B738_to_ga2_CMDhandler)

simCMD_ap_alt_up			= replace_command("sim/autopilot/altitude_up", B738_ap_alt_up_CMDhandler)
simCMD_ap_alt_dn			= replace_command("sim/autopilot/altitude_down", B738_ap_alt_dn_CMDhandler)
simCMD_ap_crs_pilot_up		= replace_command("sim/radios/obs_HSI_up", B738_ap_crs_pilot_up_CMDhandler)
simCMD_ap_crs_pilot_dn		= replace_command("sim/radios/obs_HSI_down", B738_ap_crs_pilot_dn_CMDhandler)
simCMD_ap_crs_copilot_up	= replace_command("sim/radios/copilot_obs_HSI_up", B738_ap_crs_copilot_up_CMDhandler)
simCMD_ap_crs_copilot_dn	= replace_command("sim/radios/copilot_obs_HSI_down", B738_ap_crs_copilot_dn_CMDhandler)

simCMD_vert_spd_down		= replace_command("sim/autopilot/vertical_speed_down", B738_vert_spd_down_CMDhandler)
simCMD_vert_spd_up			= replace_command("sim/autopilot/vertical_speed_up", B738_vert_spd_up_CMDhandler)

simCMD_heading_sync			= replace_command("sim/autopilot/heading_sync", B738_heading_sync_CMDhandler)

B738CMD_ap_heading_up2 		= replace_command("sim/autopilot/heading_up", B738_ap_heading_up2_CMDhandler)
B738CMD_ap_heading_dn2 		= replace_command("sim/autopilot/heading_down", B738_ap_heading_dn2_CMDhandler)

simCMD_disconnect			= replace_command("sim/autopilot/servos_fdir_off", B738_disco_handler)

function vor_loc1_timer()
	vor_loc1_captured = 1
end

function vor_loc2_timer()
	vor_loc2_captured = 1
end

function B738_vor_loc_crs()
	
	-- NAV 1
	if B738DR_tuned_nav1_type < 2 then	-- VOR
		if is_timer_scheduled(vor_loc1_timer) == true then
			stop_timer(vor_loc1_timer)
		end
		vor_loc1_captured = 0
	elseif simDR_nav1_type == 0 then
		if is_timer_scheduled(vor_loc1_timer) == true then
			stop_timer(vor_loc1_timer)
		end
		vor_loc1_captured = 0
	elseif simDR_nav1_horz_dsp == 0 then
		if is_timer_scheduled(vor_loc1_timer) == true then
			stop_timer(vor_loc1_timer)
		end
		vor_loc1_captured = 0
	elseif simDR_nav1_hdef_pilot == nil then
		if is_timer_scheduled(vor_loc1_timer) == true then
			stop_timer(vor_loc1_timer)
		end
		vor_loc1_captured = 0
	elseif math.abs(simDR_nav1_hdef_pilot) < 0.5 then
		if vor_loc1_captured == 0 and is_timer_scheduled(vor_loc1_timer) == false then
			run_after_time(vor_loc1_timer, 8)
		end
	else
		if is_timer_scheduled(vor_loc1_timer) == true then
			stop_timer(vor_loc1_timer)
		end
	end
	
	local nav_obs_pilot = simDR_nav1_obs_pilot
	local nav_obs_tgt = 0
	local rel_obs_crs = 0
	if vor_loc1_captured == 0 then
		nav_obs_pilot = B738DR_course_pilot
	else
		rel_obs_crs = (B738DR_course_pilot - simDR_nav1_course_pilot + 360) % 360
		if rel_obs_crs > 180 then
			rel_obs_crs = rel_obs_crs - 360
		end
		if math.abs(rel_obs_crs) <= 90 then
			nav_obs_tgt = simDR_nav1_course_pilot
		else
			nav_obs_tgt = (simDR_nav1_course_pilot + 180) % 360
		end
		nav_obs_pilot = B738_set_animation_rate_sec(nav_obs_pilot, nav_obs_tgt, -360, 360, 0.3)
	end
	
	-- NAV 2
	if B738DR_tuned_nav2_type < 2 then	-- VOR
		if is_timer_scheduled(vor_loc2_timer) == true then
			stop_timer(vor_loc2_timer)
		end
		vor_loc2_captured = 0
	elseif simDR_nav2_type == 0 then
		if is_timer_scheduled(vor_loc2_timer) == true then
			stop_timer(vor_loc2_timer)
		end
		vor_loc2_captured = 0
	elseif simDR_nav2_horz_dsp == 0 then
		if is_timer_scheduled(vor_loc2_timer) == true then
			stop_timer(vor_loc2_timer)
		end
		vor_loc2_captured = 0
	elseif simDR_nav2_hdef_pilot == nil then
		if is_timer_scheduled(vor_loc2_timer) == true then
			stop_timer(vor_loc2_timer)
		end
		vor_loc2_captured = 0
	elseif math.abs(simDR_nav2_hdef_pilot) < 0.5 then
		if vor_loc2_captured == 0 and is_timer_scheduled(vor_loc2_timer) == false then
			run_after_time(vor_loc2_timer, 8)
		end
	else
		if is_timer_scheduled(vor_loc2_timer) == true then
			stop_timer(vor_loc2_timer)
		end
	end
	
	local nav_obs_copilot = simDR_nav2_obs_copilot
	if vor_loc2_captured == 0 then
		nav_obs_copilot = B738DR_course_copilot
	else
		rel_obs_crs = (B738DR_course_copilot - simDR_nav2_course_copilot + 360) % 360
		if rel_obs_crs > 180 then
			rel_obs_crs = rel_obs_crs - 360
		end
		if math.abs(rel_obs_crs) <= 90 then
			nav_obs_tgt = simDR_nav2_course_copilot
		else
			nav_obs_tgt = (simDR_nav2_course_copilot + 180) % 360
		end
		nav_obs_copilot = B738_set_animation_rate_sec(nav_obs_copilot, nav_obs_tgt, -360, 360, 0.3)
	end
	
	simDR_nav1_obs_pilot = nav_obs_pilot
	simDR_nav2_obs_pilot = nav_obs_pilot
	simDR_nav1_obs_copilot = nav_obs_copilot
	simDR_nav2_obs_copilot = nav_obs_copilot
	
end

---- Custom course pilot dial up / down
function up_dn_crs1_dial(val_hdg, val_mode)
	local mcp_crs1_dial = 0
	local val_crs1_add = val_hdg
	if val_mode == 1 then
		val_crs1_add = val_crs1_add * roundUpToIncrement(cmd_sim_period * 60, 1)
	elseif val_mode == 2 then
		val_crs1_add = val_crs1_add * 360 * cmd_sim_period
	end
	mcp_crs1_dial = (B738DR_course_pilot + val_crs1_add) % 360
	if mcp_crs1_dial < 0 then
		mcp_crs1_dial = mcp_crs1_dial + 360
	end
	mcp_crs1_dial = math.floor(mcp_crs1_dial + 0.5)
	if mcp_crs1_dial >= 359.5 then
		mcp_crs1_dial = 0
	end
	B738DR_course_pilot = mcp_crs1_dial
end

function B738_course_pilot_up_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_crs1_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_crs1_dial(1, 0)
		else
			up_dn_crs1_dial(1, 2)
		end
		cmd_ap_crs1 = 1
		cmd_ap_crs1_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_crs1_dial(1, 1)
	end
end

function B738_course_pilot_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_crs1_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_crs1_dial(-1, 0)
		else
			up_dn_crs1_dial(-1, 2)
		end
		cmd_ap_crs1 = 1
		cmd_ap_crs1_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_crs1_dial(-1, 1)
	end
end

---- Custom course pilot dial up / down
function up_dn_crs2_dial(val_hdg, val_mode)
	local mcp_crs2_dial = 0
	local val_crs2_add = val_hdg
	if val_mode == 1 then
		val_crs2_add = val_crs2_add * roundUpToIncrement(cmd_sim_period * 60, 1)
	elseif val_mode == 2 then
		val_crs2_add = val_crs2_add * 360 * cmd_sim_period
	end
	mcp_crs2_dial = (B738DR_course_copilot + val_crs2_add) % 360
	if mcp_crs2_dial < 0 then
		mcp_crs2_dial = mcp_crs2_dial + 360
	end
	mcp_crs2_dial = math.floor(mcp_crs2_dial + 0.5)
	if mcp_crs2_dial >= 359.5 then
		mcp_crs2_dial = 0
	end
	B738DR_course_copilot = mcp_crs2_dial
end

function B738_course_copilot_up_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_crs2_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_crs2_dial(1, 0)
		else
			up_dn_crs2_dial(1, 2)
		end
		cmd_ap_crs2 = 1
		cmd_ap_crs2_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_crs2_dial(1, 1)
	end
end

function B738_course_copilot_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_crs2_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_crs2_dial(-1, 0)
		else
			up_dn_crs2_dial(-1, 2)
		end
		cmd_ap_crs2 = 1
		cmd_ap_crs2_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_crs2_dial(-1, 1)
	end
end


B738CMD_course_pilot_up		= create_command("laminar/B738/autopilot/course_pilot_up", "CRS Pilot Up", B738_course_pilot_up_CMDhandler)
B738CMD_course_pilot_dn		= create_command("laminar/B738/autopilot/course_pilot_dn", "CRS Pilot Down", B738_course_pilot_dn_CMDhandler)
B738CMD_course_copilot_up		= create_command("laminar/B738/autopilot/course_copilot_up", "CRS Copilot Up", B738_course_copilot_up_CMDhandler)
B738CMD_course_copilot_dn		= create_command("laminar/B738/autopilot/course_copilot_dn", "CRS Copilot Down", B738_course_copilot_dn_CMDhandler)


---- Custom altitude dial up / down
function up_dn_alt_dial(val_kts, val_mode)
	local mcp_alt_dial = 0
	local val_alt_add = val_kts
	mcp_alt_dial = B738DR_mcp_alt_dial + val_alt_add
	mcp_alt_dial = roundUpToIncrement(mcp_alt_dial, 100)
	mcp_alt_dial = math.min(mcp_alt_dial, 50000)	--41000
	mcp_alt_dial = math.max(mcp_alt_dial, 0)
	B738DR_mcp_alt_dial = mcp_alt_dial
end

function B738_ap_altitude_up_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_alt_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_alt_dial(100, 0)
		else
			up_dn_alt_dial(1000, 2)
		end
		cmd_ap_alt = 1
		cmd_ap_alt_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_alt_dial(300, 1)
	end
end

function B738_ap_altitude_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_alt_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_alt_dial(-100, 0)
		else
			up_dn_alt_dial(-1000, 2)
		end
		cmd_ap_alt = 1
		cmd_ap_alt_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_alt_dial(-300, 1)
	end
end

--- AP altitude dial ft
B738CMD_ap_altitude_up 		= create_command("laminar/B738/autopilot/altitude_up", "AP altitude Up", B738_ap_altitude_up_CMDhandler)
B738CMD_ap_altitude_dn 		= create_command("laminar/B738/autopilot/altitude_dn", "AP altitude Down", B738_ap_altitude_dn_CMDhandler)


function up_dn_hdg_dial(val_hdg, val_mode)
	local mcp_hdg_dial = 0
	local val_hdg_add = val_hdg
	if val_mode == 1 then
		val_hdg_add = val_hdg_add * roundUpToIncrement(cmd_sim_period * 60, 1)
	elseif val_mode == 2 then
		val_hdg_add = val_hdg_add * 360 * cmd_sim_period
	end
	mcp_hdg_dial = (B738DR_mcp_hdg_dial + val_hdg_add) % 360
	if mcp_hdg_dial < 0 then
		mcp_hdg_dial = mcp_hdg_dial + 360
	end
	mcp_hdg_dial = math.floor(mcp_hdg_dial + 0.5)
	if mcp_hdg_dial >= 359.5 then
		mcp_hdg_dial = 0
	end
	B738DR_mcp_hdg_dial = mcp_hdg_dial
end

function B738_ap_heading_up_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_hdg_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_hdg_dial(1, 0)
		else
			up_dn_hdg_dial(1, 2)
		end
		cmd_ap_hdg = 1
		cmd_ap_hdg_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_hdg_dial(1, 1)
	end
end

function B738_ap_heading_dn_CMDhandler(phase, duration)
	if phase == 0 then
		if cmd_ap_hdg_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_hdg_dial(-1, 0)
		else
			up_dn_hdg_dial(-1, 2)
		end
		cmd_ap_hdg = 1
		cmd_ap_hdg_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_hdg_dial(-1, 1)
	end
end



B738CMD_ap_heading_up 		= create_command("laminar/B738/autopilot/heading_up", "AP heading Up", B738_ap_heading_up_CMDhandler)
B738CMD_ap_heading_dn 		= create_command("laminar/B738/autopilot/heading_dn", "AP heading Down", B738_ap_heading_dn_CMDhandler)


function up_dn_speed_dial(val_kts, val_mach, val_mode)
	local mcp_airspeed_dial = 0
	local mcp_airspeed_dial3 = 0
	local val_kts_add = val_kts
	local val_mach_add = val_mach
	if val_mode == 1 then
		val_kts_add = val_kts_add * roundUpToIncrement(cmd_sim_period * 40, 1)
		val_mach_add = val_mach_add * cmd_sim_period * 40
	elseif val_mode == 2 then
		val_kts_add = val_kts_add * roundUpToIncrement(cmd_sim_period * 200, 1)
		val_mach_add = val_mach_add * 200 * cmd_sim_period
	end
	if simDR_airspeed_is_mach == 0 then
		mcp_airspeed_dial = math.min(B738DR_mcp_speed_dial + val_kts_add, 340)
		mcp_airspeed_dial = math.max(mcp_airspeed_dial, 100)
		-- TEMP --
		mcp_airspeed_dial = math.floor(mcp_airspeed_dial)
		----------
	else
		mcp_airspeed_dial3 = 100 / (simDR_sos * 1.94384449244)
		mcp_airspeed_dial = math.min(B738DR_mcp_speed_dial + val_mach_add, 0.82)
		mcp_airspeed_dial = math.max(mcp_airspeed_dial, mcp_airspeed_dial3)
		mcp_airspeed_dial = math.max(mcp_airspeed_dial, 0.60)
		-- mcp_airspeed_dial = math.max(mcp_airspeed_dial, 0.40)
	end
	B738DR_mcp_speed_dial = mcp_airspeed_dial
	update_mcp_speed_dial()
end

function B738_ap_speed_up_handler(phase, duration)
	if phase == 0 then
		if cmd_ap_spd_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_speed_dial(1, 0.01, 0)
		else
			up_dn_speed_dial(1, 0.01, 2)
		end
		cmd_ap_spd = 1
		cmd_ap_spd_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_speed_dial(1, 0.01, 1)
	end
end

function B738_ap_speed_dn_handler(phase, duration)
	if phase == 0 then
		if cmd_ap_spd_timer == 0 or (B738DR_smart_ap_knob == 0 and simDR_vr_enable == 0) then
			up_dn_speed_dial(-1, -0.01, 0)
		else
			up_dn_speed_dial(-1, -0.01, 2)
		end
		cmd_ap_spd = 1
		cmd_ap_spd_timer = cmd_threshold_time
	elseif phase == 1 and duration > 0.5 then
		up_dn_speed_dial(-1, -0.01, 1)
	end
end

simCMD_ap_speed_up			= replace_command("sim/autopilot/airspeed_up", B738_ap_speed_up_handler)
simCMD_ap_speed_dn			= replace_command("sim/autopilot/airspeed_down", B738_ap_speed_dn_handler)

function void_func(phase, duration)
end

function B738_ap_knots_mach_toggle_handler(phase, duration)
	if phase == 0 then
		local mcp_airspeed_dial2 = B738DR_mcp_speed_dial
		local mcp_airspeed_dial3 = 0
		local kspd = 1
		if simDR_IAS > 45 then
			kspd = (simDR_TAS * 1.94384449244) / simDR_IAS
		end
		if simDR_airspeed_is_mach == 0 then
			mcp_airspeed_dial2 = (mcp_airspeed_dial2 * (simDR_sos * 1.94384449244)) / kspd
			mcp_airspeed_dial2 = math.min(mcp_airspeed_dial2, 340)
			mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, 100)
			B738DR_mcp_speed_dial = mcp_airspeed_dial2
			B738DR_mcp_speed_dial_kts = mcp_airspeed_dial2
		else
			mcp_airspeed_dial2 = (mcp_airspeed_dial2 * kspd) / (simDR_sos * 1.94384449244)
			mcp_airspeed_dial3 = (100 * kspd) / (simDR_sos * 1.94384449244)
			mcp_airspeed_dial2 = math.min(mcp_airspeed_dial2, 0.82)
			mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, mcp_airspeed_dial3)
			mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, 0.60)
			-- mcp_airspeed_dial2 = math.max(mcp_airspeed_dial2, 0.40)
			B738DR_mcp_speed_dial = mcp_airspeed_dial2
			B738DR_mcp_speed_dial_kts = (mcp_airspeed_dial2 * (simDR_sos * 1.94384449244)) / kspd
		end
	end
end


simCMD_ap_knots_mach_toggle	= wrap_command("sim/autopilot/knots_mach_toggle", void_func, B738_ap_knots_mach_toggle_handler)

--*************************************************************************************--
--** 				              WRAP X-PLANE COMMANDS                  	    	 **--
--*************************************************************************************--

--*************************************************************************************--
--** 					           OBJECT CONSTRUCTORS         		        		 **--
--*************************************************************************************--


--*************************************************************************************--
--** 				                  CREATE OBJECTS              	     			 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				                 SYSTEM FUNCTIONS           	    			 **--
--*************************************************************************************--

----- 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_animation_rate(current_value3, target3, min, max, speed)
	local calc_result = 0
	local calc_speed = speed
	if calc_speed <= 0 then
		calc_speed = 1
	end
	if target3 >= (max - 0.001) and current_value3 >= (max - 0.01) then
        return max
    elseif target3 <= (min + 0.001) and current_value3 <= (min + 0.01) then
        return min
    elseif current_value3 > max then
		return max
    elseif current_value3 < min then
		return min
	elseif current_value3 < target3 then
		calc_result = current_value3 + (SIM_PERIOD / calc_speed)
		if calc_result > target3 then
			calc_result = target3
		end
		return calc_result
	else
		calc_result = current_value3 - (SIM_PERIOD / calc_speed)
		if calc_result < target3 then
			calc_result = target3
		end
        return calc_result
    end
end

function B738_set_animation_rate_sec(current_value3, target3, min, max, speed)
	local calc_result = 0
	local calc_speed = speed
	local rel_crs_anim = 0
	if calc_speed <= 0 then
		calc_speed = 1
	end
	if target3 >= (max - 0.001) and current_value3 >= (max - 0.01) then
        return max
    elseif target3 <= (min + 0.001) and current_value3 <= (min + 0.01) then
        return min
    elseif current_value3 > max then
		return max
    elseif current_value3 < min then
		return min
	else
		rel_crs_anim = (target3 - current_value3 + 360) % 360
		if rel_crs_anim > 180 then
			rel_crs_anim = rel_crs_anim - 360
		end
		
		if rel_crs_anim > 0 then
			calc_result = (current_value3 + (SIM_PERIOD * calc_speed)) % 360
			
			rel_crs_anim = (target3 - calc_result + 360) % 360
			if rel_crs_anim > 180 then
				rel_crs_anim = rel_crs_anim - 360
			end
			
			if rel_crs_anim <= 0 then
				calc_result = target3
			end
			return calc_result
		else
			calc_result = (current_value3 - (SIM_PERIOD * calc_speed)) % 360
			if calc_result < 0 then
				calc_result = calc_result + 360
			end
			
			rel_crs_anim = (target3 - calc_result + 360) % 360
			if rel_crs_anim > 180 then
				rel_crs_anim = rel_crs_anim - 360
			end
			
			if rel_crs_anim >= 0 then
				calc_result = target3
			end
			return calc_result
		end
		-- if current_value3 < target3 then
			-- calc_result = current_value3 + (SIM_PERIOD * calc_speed)
			-- if calc_result > target3 then
				-- calc_result = target3
			-- end
			-- if current_value3 < 0 and target3 > 90 then
				-- calc_result = 180 - (calc_result - current_value3)
			-- end
			-- return calc_result
		-- else
			-- calc_result = current_value3 - (SIM_PERIOD * calc_speed)
			-- if calc_result < target3 then
				-- calc_result = target3
			-- end
			-- if current_value3 > 0 and target3 < -90 then
				-- calc_result = (current_value3 - calc_result) - 180
			-- end
			-- return calc_result
		-- end
	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 roundUp(x)
    return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
end

-- 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


function vert_spd_corr()
	if simDR_ap_vvi_dial >= -950 and simDR_ap_vvi_dial < 1000 then
		simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 50)
	else
		simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 100)
	end
	B738DR_ap_vvi_dial = simDR_ap_vvi_dial
end

-- AP COMMAND TIMERS

function cmd_ap_timer()
	cmd_sim_period = SIM_PERIOD
	if cmd_ap_spd_timer > 0 then
		cmd_ap_spd_timer = cmd_ap_spd_timer - cmd_sim_period
		if cmd_ap_spd_timer < 0 then
			cmd_ap_spd_timer = 0
		end
	end
	cmd_ap_spd = 0
	
	if cmd_ap_hdg_timer > 0 then
		cmd_ap_hdg_timer = cmd_ap_hdg_timer - cmd_sim_period
		if cmd_ap_hdg_timer < 0 then
			cmd_ap_hdg_timer = 0
		end
	end
	cmd_ap_hdg = 0
	
	if cmd_ap_alt_timer > 0 then
		cmd_ap_alt_timer = cmd_ap_alt_timer - cmd_sim_period
		if cmd_ap_alt_timer < 0 then
			cmd_ap_alt_timer = 0
		end
	end
	cmd_ap_alt = 0
	
	if cmd_ap_crs1_timer > 0 then
		cmd_ap_crs1_timer = cmd_ap_crs1_timer - cmd_sim_period
		if cmd_ap_crs1_timer < 0 then
			cmd_ap_crs1_timer = 0
		end
	end
	cmd_ap_crs1 = 0
	
	if cmd_ap_crs2_timer > 0 then
		cmd_ap_crs2_timer = cmd_ap_crs2_timer - cmd_sim_period
		if cmd_ap_crs2_timer < 0 then
			cmd_ap_crs2_timer = 0
		end
	end
	cmd_ap_crs2 = 0
	
end


---- BLINK TIMER ----------------

function blink_timer()

	if B738DR_blink == 1 then
		if blink_out == 0 then
			blink_out = 1
		else
			blink_out = 0
		end
		B738DR_blink = 0
	else
		B738DR_blink = 1
	end
	
end

function blink_timer_ap()
	
	if blink_out_ap == 0 then
		blink_out_ap = 1
	else
		blink_out_ap = 0
	end
	
end

function snd_ap_disco_timer()
	snd_ap_disco_delay = 0
end

function blink_timer_at()
	if blink_out_at == 0 then
		blink_out_at = 1
	else
		blink_out_at = 0
	end
	
	if at_dis_time > 0 then
		B738DR_at_disconnect = blink_out_at
	else
		B738DR_at_disconnect = 0
	end
end

--*****************************-
function blink_ap_at()
	local snd_ap_disconnect = 0
	local ap_disconnect_light = 0
	
	if ap_dis_time > 0 and autopilot_cws_status == 0 then
		snd_ap_disconnect = 1
		if snd_ap_disconnect_old == 0 then
			if is_timer_scheduled(snd_ap_disco_timer) == true then
				stop_timer(snd_ap_disco_timer)
			end
			snd_ap_disco_delay = 1
			run_after_time(snd_ap_disco_timer, 1.7)
		end
	end
	snd_ap_disconnect_old = snd_ap_disconnect
	
	if snd_ap_disco_delay == 0 then
		if snd_ap_disconnect ~= 0 then
			ap_disconnect_light = blink_out_ap
		end
	else
		ap_disconnect_light = blink_out_ap
		snd_ap_disconnect = 1
	end
	
	B738DR_ap_disconnect = ap_disconnect_light
	B738DR_snd_ap_disconnect = snd_ap_disconnect * B738DR_dc_stdbus_status
	
	if at_dis_time == 0 then
		B738DR_at_disconnect = 0
	end
end

function at_warn_high_timer()
	at_warn_high = 2
end

function at_warn_low_timer()
	at_warn_low = 2
end

function B738_at_warning()
	
	local at_warn = 0
	local flaps = 0
	if simDR_int_ver < 120000 then
		flaps = simDR_flaps_handle_ratio
	else
		flaps = simDR_flaps_handle_ratio_xp12
	end
	
	if simDR_radio_height_pilot_ft > 50 or simDR_radio_height_copilot_ft > 50 then
		if (at_mode == 2 or at_mode == 9) and flaps > 0 and B738DR_fd_goaround == 0 and B738DR_ap_goaround == 0 then
			if (B738DR_AP_airspeed > simDR_airspeed_dial_kts + 10) and B738DR_AP_acceleration > 0 then
				if at_warn_high == 0 then
					at_warn_high = 1
					if is_timer_scheduled(at_warn_high_timer) == true then
						stop_timer(at_warn_high_timer)
					end
					run_after_time(at_warn_high_timer, 6)
				end
			else
				at_warn_high = 0
			end
			if (B738DR_AP_airspeed < simDR_airspeed_dial_kts - 5) and B738DR_AP_acceleration < 0 then
				if at_warn_low == 0 then
					at_warn_low = 1
					if is_timer_scheduled(at_warn_low_timer) == true then
						stop_timer(at_warn_low_timer)
					end
					run_after_time(at_warn_low_timer, 6)
				end
			else
				at_warn_low = 0
			end
			
			if at_warn_high == 2 or at_warn_low == 2 then
				at_warn = 1
			end
		else
			at_warn_low = 0
			at_warn_high = 0
		end
	else
		at_warn_low = 0
		at_warn_high = 0
	end
	
	if B738DR_at_light_pilot == 1 or B738DR_at_light_fo == 1 then
		if at_warn ~= 0 then
			at_warn_reset = 1
		end
	end
	
	if at_warn_reset ~= 0 and at_warn ~= 0 then
		at_warn = 0
	elseif at_warn == 0 then
		at_warn_reset = 0
	end
	
	if at_warn_low == 0 then
		if is_timer_scheduled(at_warn_low_timer) == true then
			stop_timer(at_warn_low_timer)
		end
	end
	
	if at_warn_high == 0 then
		if is_timer_scheduled(at_warn_high_timer) == true then
			stop_timer(at_warn_high_timer)
		end
	end
	
	local at_warning1 = 0
	if B738DR_ap_disconnect1_test_switch_pos == 1 then
		at_warning1 = 1
	elseif B738DR_ap_disconnect1_test_switch_pos == 0 and at_warn == 1 then
		at_warning1 = B738DR_blink
	end
	
	local at_warning2 = 0
	if B738DR_ap_disconnect2_test_switch_pos == 1 then
		at_warning2 = 1
	elseif B738DR_ap_disconnect2_test_switch_pos == 0 and at_warn == 1 then
		at_warning2 = B738DR_blink
	end
	
	B738DR_at_fms_warn1 = at_warning1 * B738DR_brightness_level
	B738DR_at_fms_warn2 = at_warning2 * B738DR_brightness_level
	
	--------------------------
	
	local ap_cws_status = autopilot_cws_p_status + autopilot_cws_r_status
	if ap_on == 1 and (autopilot_cws_p_status ~= 0 or autopilot_cws_r_status ~= 0) then
		if ap_cws_status ~= ap_cws_status_old and ap_cws_status > ap_cws_status_old and ap_warn_off == 0 then
			ap_warn = 1
		end
		ap_cws_status_old = ap_cws_status
	else
		ap_warn = 0
		ap_warn_off = 0
		ap_cws_status_old = 0
	end
	ap_cws_status_old = ap_cws_status
	
	local ap_warning1 = 0
	if B738DR_ap_disconnect1_test_switch_pos == 1 then
		ap_warning1 = 1
	elseif B738DR_ap_disconnect1_test_switch_pos == 0 and ap_warn == 1 then
		ap_warning1 = blink_out
	end
	
	local ap_warning2 = 0
	if B738DR_ap_disconnect2_test_switch_pos == 1 then
		ap_warning2 = 1
	elseif B738DR_ap_disconnect2_test_switch_pos == 0 and ap_warn == 1 then
		ap_warning2 = blink_out
	end
	
	B738DR_ap_warn1 = ap_warning1 * B738DR_brightness_level
	B738DR_ap_warn2 = ap_warning2 * B738DR_brightness_level
end

function rec_thr_timer()
	blink_rec_thr_status = 0
end

function rec_thr2_timer()
	blink_rec_thr2_status = 0
end


function rec_hdg_timer()
	blink_rec_hdg_status = 0
end

function rec_alt_timer()
	blink_rec_alt_status = 0
end

function rec_alt_alert_timer()
	blink_rec_alt_alert_status = 0
end

function rec_cmd_timer1()
	blink_rec_cmd_status1 = 0
end

function rec_cmd_timer2()
	blink_rec_cmd_status2 = 0
end

function rec_sch_timer()
	blink_rec_sch_status = 0
end

function rec_schg_timer()
	blink_rec_schg_status = 0
end

function rec_cws_r_timer()
	blink_rec_cws_r_status = 0
end

function rec_cws_p_timer()
	blink_rec_cws_p_status = 0
end

function rec_spd_ind_timer()
	blink_rec_spd_ind = 0
end

function B738_PFD_flash2()

	if is_timer_scheduled(blink_timer) == false then
		run_at_interval(blink_timer, 0.2)
	end
	
	if fma_speed_timer == 0 then
		-- SPEED modes
		if pfd_spd_old ~= B738DR_pfd_spd_mode
		and B738DR_pfd_spd_mode > 1 then
			if is_timer_scheduled(rec_thr2_timer) == true then
				stop_timer(rec_thr2_timer)
			end
			blink_rec_thr2_status = 0
			if is_timer_scheduled(rec_thr_timer) == true then
				stop_timer(rec_thr_timer)
			end
			run_after_time(rec_thr_timer, 10.0)
			blink_rec_thr_status = 1
		end
		if pfd_spd_old ~= B738DR_pfd_spd_mode
		and B738DR_pfd_spd_mode == 1 then
			if is_timer_scheduled(rec_thr_timer) == true then
				stop_timer(rec_thr_timer)
			end
			blink_rec_thr_status = 0
			if is_timer_scheduled(rec_thr2_timer) == true then
				stop_timer(rec_thr2_timer)
			end
			run_after_time(rec_thr2_timer, 10.0)
			blink_rec_thr2_status = 1
		end
		
		if B738DR_pfd_spd_mode == 0 or B738DR_autopilot_autothr_arm_pos == 0 then
			if is_timer_scheduled(rec_thr_timer) == true then
				stop_timer(rec_thr_timer)
			end
			blink_rec_thr_status = 0
			if is_timer_scheduled(rec_thr2_timer) == true then
				stop_timer(rec_thr2_timer)
			end
			blink_rec_thr2_status = 0
		end
		pfd_spd_old = B738DR_pfd_spd_mode

		if blink_rec_thr_status == 0 then
			B738DR_rec_thr_modes = 0
		end
		if blink_rec_thr2_status == 0 then
			B738DR_rec_thr2_modes = 0
		end
	end
	
	if fma_roll_timer == 0 then
		-- ROLL modes
		if pfd_hdg_old ~= B738DR_pfd_hdg_mode
		and B738DR_pfd_hdg_mode > 0 then
			if is_timer_scheduled(rec_hdg_timer) == true then
				stop_timer(rec_hdg_timer)
			end
			run_after_time(rec_hdg_timer, 10.0)
			blink_rec_hdg_status = 1
		end
		
		if B738DR_pfd_hdg_mode == 0 then
			if is_timer_scheduled(rec_hdg_timer) == true then
				stop_timer(rec_hdg_timer)
			end
			blink_rec_hdg_status = 0
		end
		pfd_hdg_old = B738DR_pfd_hdg_mode

		if blink_rec_hdg_status == 0 then
			B738DR_rec_hdg_modes = 0
		end
	end
	
	if fma_pitch_timer == 0 then
		-- PITCH modes
		if pfd_alt_old ~= B738DR_pfd_alt_mode
		and B738DR_pfd_alt_mode > 0 then
			if is_timer_scheduled(rec_alt_timer) == true then
				stop_timer(rec_alt_timer)
			end
			run_after_time(rec_alt_timer, 10.0)
			blink_rec_alt_status = 1
		end
		
		if B738DR_pfd_alt_mode == 0 then
			if is_timer_scheduled(rec_alt_timer) == true then
				stop_timer(rec_alt_timer)
			end
			blink_rec_alt_status = 0
		end
		pfd_alt_old = B738DR_pfd_alt_mode

		if blink_rec_alt_status == 0 then
			B738DR_rec_alt_modes = 0
		end
	end
	
	-- CMD MODES
	local cmd_status1 = 0	-- 0-Off,  1-FD, 2-CMD, 3-LAND 3
	if B738DR_single_ch_status == 0 then
		if B738DR_autopilot_fd_pos == 0 then
			if ap_on == 0 then
				cmd_status1 = 0
			else
				cmd_status1 = 1
			end
		else
			if ap_on == 0 then
				cmd_status1 = 1
			else
				cmd_status1 = 2
			end
		end
		if B738DR_flare_status > 0 then
			cmd_status1 = 3
		end
	end
	if cmd_old1 ~= cmd_status1
	and cmd_status1 > 0 then
		if is_timer_scheduled(rec_cmd_timer1) == true then	-- 10 seconds
			stop_timer(rec_cmd_timer1)
		end
		run_after_time(rec_cmd_timer1, 10.0)
		blink_rec_cmd_status1 = 1
	end
	if cmd_status1 == 0 then
		if is_timer_scheduled(rec_cmd_timer1) == true then
			stop_timer(rec_cmd_timer1)
		end
		blink_rec_cmd_status1 = 0
	end
	cmd_old1 = cmd_status1
	if blink_rec_cmd_status1== 0 then
		B738DR_rec_cmd_modes1 = 0
	end
	
	local cmd_status2 = 0	-- 0-Off,  1-FD, 2-CMD, 3-LAND 3
	if B738DR_single_ch_status == 0 then
		if B738DR_autopilot_fd_fo_pos == 0 then
			if ap_on == 0 then
				cmd_status2 = 0
			else
				cmd_status2 = 1
			end
		else
			if ap_on == 0 then
				cmd_status2 = 1
			else
				cmd_status2 = 2
			end
		end
		if B738DR_flare_status > 0 then
			cmd_status2 = 3
		end
	end
	if cmd_old2 ~= cmd_status2
	and cmd_status2 > 0 then
		if is_timer_scheduled(rec_cmd_timer2) == true then	-- 10 seconds
			stop_timer(rec_cmd_timer2)
		end
		run_after_time(rec_cmd_timer2, 10.0)
		blink_rec_cmd_status2 = 1
	end
	if cmd_status2 == 0 then
		if is_timer_scheduled(rec_cmd_timer2) == true then
			stop_timer(rec_cmd_timer2)
		end
		blink_rec_cmd_status2 = 0
	end
	cmd_old2 = cmd_status2
	if blink_rec_cmd_status2== 0 then
		B738DR_rec_cmd_modes2 = 0
	end
	
	
	-- SINGLE CHANNEL
	if single_ch_status_old ~= B738DR_single_ch_status
	and B738DR_single_ch_status == 1 then
		if is_timer_scheduled(rec_sch_timer) == true then	-- 10 seconds
			stop_timer(rec_sch_timer)
		end
		run_after_time(rec_sch_timer, 10.0)
		blink_rec_sch_status = 1
	end
	if B738DR_single_ch_status == 0 then
		if is_timer_scheduled(rec_sch_timer) == true then
			stop_timer(rec_sch_timer)
		end
		blink_rec_sch_status = 0
	end
	single_ch_status_old = B738DR_single_ch_status
	if blink_rec_sch_status == 0 then
		B738DR_rec_sch_modes = 0
	end
	
	-- SINGLE CHANNEL G
	if single_ch_g_status_old ~= B738DR_single_ch_g_status
	and B738DR_single_ch_g_status == 1 then
		if is_timer_scheduled(rec_schg_timer) == true then	-- 10 seconds
			stop_timer(rec_schg_timer)
		end
		run_after_time(rec_schg_timer, 10.0)
		blink_rec_schg_status = 1
	end
	if B738DR_single_ch_g_status == 0 then
		if is_timer_scheduled(rec_schg_timer) == true then
			stop_timer(rec_schg_timer)
		end
		blink_rec_schg_status = 0
	end
	single_ch_g_status_old = B738DR_single_ch_g_status
	if blink_rec_schg_status == 0 then
		B738DR_rec_schg_modes = 0
	end
	
	-- CWS R mode
	if autopilot_cws_r_status_old ~= autopilot_cws_r_status
	and autopilot_cws_r_status == 1 then
		if is_timer_scheduled(rec_sch_timer) == true then	-- 10 seconds
			stop_timer(rec_cws_r_timer)
		end
		if is_timer_scheduled(rec_schg_timer) == true then	-- 10 seconds
			stop_timer(rec_cws_r_timer)
		end
		run_after_time(rec_cws_r_timer, 10.0)
		blink_rec_cws_r_status = 1
	end
	if autopilot_cws_r_status == 0 then
		if is_timer_scheduled(rec_cws_r_timer) == true then
			stop_timer(rec_cws_r_timer)
		end
		blink_rec_cws_r_status = 0
	end
	autopilot_cws_r_status_old = autopilot_cws_r_status
	if blink_rec_cws_r_status == 0 then
		B738DR_rec_cwr_r_modes = 0
	end
	
	-- CWS P mode
	if autopilot_cws_p_status_old ~= autopilot_cws_p_status
	and autopilot_cws_p_status == 1 then
		if is_timer_scheduled(rec_cws_p_timer) == true then	-- 10 seconds
			stop_timer(rec_cws_p_timer)
		end
		run_after_time(rec_cws_p_timer, 10.0)
		blink_rec_cws_p_status = 1
	end
	if autopilot_cws_p_status == 0 then
		if is_timer_scheduled(rec_cws_p_timer) == true then
			stop_timer(rec_cws_p_timer)
		end
		blink_rec_cws_p_status = 0
	end
	autopilot_cws_p_status_old = autopilot_cws_p_status
	if blink_rec_cws_p_status == 0 then
		B738DR_rec_cwr_p_modes = 0
	end
	
	-- ALT ALERT
	local altx = 0
	local capt_fo = 0	-- 0-captain, 1- first officer
	local alt_alert_disable = 0
	local alt_deviation_status = 0
	
	if ap_pitch_mode == 0 then
		alt_alert_disable = 1
	elseif ap_pitch_mode_eng == 7 and ap_pitch_mode == 7 then 	-- GP
		alt_alert_disable = 1
	elseif ap_pitch_mode_eng == 4 and ap_pitch_mode == 4 then 	-- GS
		alt_alert_disable = 1
	elseif simDR_flaps_ratio >= 0.75 then
		alt_alert_disable = 1
	end
	
	if alt_alert_disable == 0 then
		if (simDR_autopilot_altitude_mode == 4 or simDR_autopilot_altitude_mode == 5 
		or ap_pitch_mode_eng == 6) and ap_pitch_mode_eng ~= 3 and B738DR_vnav_alt_mode == 0 then	--ALT ACQ
			
			if autopilot_master_capt_status == 0 and autopilot_master_fo_status == 0 then
				if autopilot_cmd_b_status == 1 then
					capt_fo = 1
				end
			elseif autopilot_master_capt_status == 1 and autopilot_master_fo_status == 1 then		-- autoland
				capt_fo = cmd_first
			elseif autopilot_master_fo_status == 1 then
				capt_fo = 1
			end
			if capt_fo == 0 then
				altx = mcp_alt_dial_app - simDR_altitude_pilot
				if altx < 0 and simDR_vvi_fpm_pilot < 0 then
					altx = -altx
				end
				if simDR_autopilot_altitude_mode == 4 then
					if simDR_ap_vvi_dial > 0 and mcp_alt_dial_app < simDR_altitude_pilot then
						altx = 0
					elseif simDR_ap_vvi_dial < 0 and mcp_alt_dial_app > simDR_altitude_pilot then
						altx = 0
					end
				end
			else
				altx = mcp_alt_dial_app - simDR_altitude_copilot
				if altx < 0 and simDR_vvi_fpm_copilot < 0 then
					altx = -altx
				end
				if simDR_autopilot_altitude_mode == 4 then
					if simDR_ap_vvi_dial > 0 and mcp_alt_dial_app < simDR_altitude_copilot then
						altx = 0
					elseif simDR_ap_vvi_dial < 0 and mcp_alt_dial_app > simDR_altitude_copilot then
						altx = 0
					end
				end
			end
			
			if altx > 300 and altx < 900 then
				blink_rec_alt_alert_status = 1
			else
				blink_rec_alt_alert_status = 0
			end
			
			alt_deviation_active = 0
			
		elseif simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
			if autopilot_master_capt_status == 0 and autopilot_master_fo_status == 0 then
				if autopilot_cmd_b_status == 1 then
					capt_fo = 1
				end
			elseif autopilot_master_capt_status == 1 and autopilot_master_fo_status == 1 then		-- autoland
				capt_fo = cmd_first
			elseif autopilot_master_fo_status == 1 then
				capt_fo = 1
			end
			if capt_fo == 0 then
				altx = math.abs(B738DR_preselected_alt - simDR_altitude_pilot)
			else
				altx = math.abs(B738DR_preselected_alt - simDR_altitude_copilot)
			end
			
			if altx < 250 then
				alt_deviation_active = 1
			end
			if altx > 300 and altx < 900 and alt_deviation_active == 1 then
				alt_deviation_status = 1
			else
				alt_deviation_status = 0
			end
			
			if altx < 300 or alt_deviation_status == 1 then
				blink_rec_alt_alert_status = 0
			end
		else
			blink_rec_alt_alert_status = 0
			alt_deviation_status = 0
			alt_deviation_active = 0
		end
	else
		blink_rec_alt_alert_status = 0
		alt_deviation_status = 0
		alt_deviation_active = 0
	end
	
	-- speed kts/mach indicator
	if simDR_mach_no >= 0.4 then
		B738DR_spd_ind_mode = 1
	elseif simDR_mach_no <= 0.38 then
		B738DR_spd_ind_mode = 0
	end
	if B738DR_spd_ind_mode ~= spd_ind_mode_old then
		if is_timer_scheduled(rec_spd_ind_timer) == true then	-- 10 seconds
			stop_timer(rec_spd_ind_timer)
		end
		run_after_time(rec_spd_ind_timer, 10.0)
		blink_rec_spd_ind = 1
	end
	spd_ind_mode_old = B738DR_spd_ind_mode
	
	B738DR_rec_thr_modes = blink_rec_thr_status
	B738DR_rec_thr2_modes = blink_rec_thr2_status
	B738DR_rec_hdg_modes = blink_rec_hdg_status
	B738DR_rec_alt_modes = blink_rec_alt_status
	B738DR_rec_cmd_modes1 = blink_rec_cmd_status1
	B738DR_rec_cmd_modes2 = blink_rec_cmd_status2
	B738DR_rec_sch_modes = blink_rec_sch_status
	B738DR_rec_schg_modes = blink_rec_schg_status
	B738DR_rec_cwr_r_modes = blink_rec_cws_r_status
	B738DR_rec_cwr_p_modes = blink_rec_cws_p_status
	B738DR_rec_spd_ind = blink_rec_spd_ind
	
	B738DR_app_alt_alert = blink_rec_alt_alert_status
	
	if alt_deviation_status == 0 then
		B738DR_dev_alt_alert = 0
	else
		B738DR_dev_alt_alert = blink_out + 1
	end
	
	if blink_rec_alt_alert_status == 1 or alt_deviation_status == 1 then
		B738DR_rec_alt_alert = 1
	else
		B738DR_rec_alt_alert = 0
	end
	
	if B738DR_disable_fo_callout == 0 then
		B738DR_play_fo_1000_to_go = B738DR_rec_alt_alert
	else
		B738DR_play_fo_1000_to_go = 0
	end
	
end

function master_light_block()
	master_light_block_act = 0
end

function autopilot_system_lights2()
	
	if fma_speed_timer == 0 then
		-- SPEED modes
		if autopilot_autothrottle_status == 0 then
			-- none
			B738DR_pfd_spd_mode = 0
			B738DR_autopilot_speed_status = 0
		elseif ap_pitch_mode < 8 or ap_pitch_mode > 12 then
			B738DR_autopilot_thr_hld_pfd = 0
			if B738DR_autopilot_vnav_status == 1 and vnav_engaged == 1 then
				B738DR_autopilot_speed_status = 0
			else
				-- ARM / RETARD mode
				local disable_arm_mode = 0
				if ap_pitch_mode == 2 or (ap_pitch_mode == 5 and vnav_engaged == 1) then
					disable_arm_mode = 1
				end
				if disable_arm_mode == 0 then
					-- ARM
					if at_mode == 0 and at_mode_eng == 0 then
						B738DR_pfd_spd_mode = PFD_SPD_ARM
					end
				end
				-- N1 mode
				if at_mode == 1 and at_mode_eng == 1 then
					B738DR_pfd_spd_mode = PFD_SPD_N1
				-- SPD mode
				end
				if at_mode == 2 and at_mode_eng == 2 then
					B738DR_autopilot_speed_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_MCP_SPD
				else
					B738DR_autopilot_speed_status = 0
				end
			end
		elseif ap_pitch_mode == 8 or ap_pitch_mode == 11 then
			if B738DR_at_takeoff == 0 then
				if at_mode == 0 and B738DR_autopilot_autothr_arm_pos == 1 then
					B738DR_pfd_spd_mode = PFD_SPD_ARM
				end
			end
		end
		if B738DR_autopilot_autothr_arm_pos == 1 then
			if vnav_pfd_spd_mode >= 0 then
				B738DR_pfd_spd_mode = vnav_pfd_spd_mode
			elseif lvl_chg_pfd_spd_mode >= 0 then
				B738DR_pfd_spd_mode = lvl_chg_pfd_spd_mode
			end
		end
	end
	if B738DR_lights_test == 1 then
		B738DR_autopilot_speed_status = 1 * B738DR_brightness2_export
	end
	
	
	if fma_roll_timer == 0 then
		-- ROLL modes arm
		
		-- VOR LOC and APP
		
		if simDR_nav_status == 1 or simDR_approach_status == 1 --then
		or B738DR_gls_nav_status == 1 or B738DR_gls_app_status == 1 then
			B738DR_pfd_hdg_mode_arm = PFD_HDG_VOR_LOC_ARM
		else
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_VOR_LOC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_VOR_LOC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		end
		
		-- LNAV
		if ap_roll_mode == 4 and ap_roll_mode_eng == 4 then
			if B738DR_lnav_engaged == 0 then
				B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_ARM
			else
				if B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ARM then
					B738DR_pfd_hdg_mode_arm = 0
				end
			end
		elseif ap_roll_mode == 10 and ap_roll_mode_eng == 10 then
			B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_ARM
		else
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		end
		-- FAC
		if ap_roll_mode == 11 and ap_roll_mode_eng == 11 then
			if B738DR_fac_status == 1 then
				B738DR_pfd_hdg_mode_arm = PFD_HDG_FAC_ARM
			else
				if B738DR_pfd_hdg_mode_arm == PFD_HDG_FAC_ARM then
					B738DR_pfd_hdg_mode_arm = 0
				end
			end
		elseif ap_roll_mode == 7 and ap_roll_mode_eng == 7 then
			B738DR_pfd_hdg_mode_arm = PFD_HDG_FAC_ARM
		elseif ap_roll_mode == 8 and ap_roll_mode_eng == 8 then
			B738DR_pfd_hdg_mode_arm = PFD_HDG_FAC_ARM
		else
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_FAC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		end
		if ap_roll_mode ~= 9 then
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_ROLLOUT_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ROLLOUT_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		end
		
		-- ROLL modes
		if ap_roll_mode == 0 and ap_roll_mode_eng == 0 then
			B738DR_pfd_hdg_mode = 0
			B738DR_pfd_hdg_mode_arm = 0
		-- HDG SEL
		elseif ap_roll_mode == 1 and ap_roll_mode_eng == 1 then
			B738DR_pfd_hdg_mode = PFD_HDG_HDG_SEL
		-- VOR LOC
		elseif ap_roll_mode == 2 and ap_roll_mode_eng == 2 then
			B738DR_pfd_hdg_mode = PFD_HDG_VOR_LOC
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_VOR_LOC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		-- APP
		elseif ap_roll_mode == 3 and ap_roll_mode_eng == 3 then
			B738DR_pfd_hdg_mode = PFD_HDG_VOR_LOC
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_VOR_LOC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		-- HDG / LNAV
		elseif ap_roll_mode == 10 and ap_roll_mode_eng == 10 then
			B738DR_pfd_hdg_mode = PFD_HDG_HDG_SEL
			B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_ARM
		-- LNAV
		elseif ap_roll_mode == 4 and ap_roll_mode_eng == 4 then
			if B738DR_lnav_engaged > 0 then
				B738DR_pfd_hdg_mode = PFD_HDG_LNAV
				if B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ARM then
					B738DR_pfd_hdg_mode_arm = 0
				end
			else
				B738DR_pfd_hdg_mode = 0
			end
		-- LNAV / VOR LOC
		elseif ap_roll_mode == 5 and ap_roll_mode_eng == 5 then
			B738DR_pfd_hdg_mode = PFD_HDG_LNAV
			B738DR_pfd_hdg_mode_arm = PFD_HDG_VOR_LOC_ARM
		-- LNAV / APP GS GP
		elseif (ap_roll_mode == 6 and ap_roll_mode_eng == 6) or (ap_roll_mode == 13 and ap_roll_mode_eng == 13) then
			B738DR_pfd_hdg_mode = PFD_HDG_LNAV
			B738DR_pfd_hdg_mode_arm = PFD_HDG_VOR_LOC_ARM
		-- HDG / FAC
		elseif ap_roll_mode == 7 and ap_roll_mode_eng == 7 then
			B738DR_pfd_hdg_mode = PFD_HDG_HDG_SEL
			B738DR_pfd_hdg_mode_arm = PFD_HDG_FAC_ARM
		-- LNAV / FAC
		elseif ap_roll_mode == 8 and ap_roll_mode_eng == 8 then
			B738DR_pfd_hdg_mode = PFD_HDG_LNAV
			B738DR_pfd_hdg_mode_arm = PFD_HDG_FAC_ARM
		-- FAC
		elseif ap_roll_mode == 11 and ap_roll_mode_eng == 11 then
			-- if B738DR_fac_gp_engaged == 1 then
			if B738DR_fac_status == 2 then
				B738DR_pfd_hdg_mode = PFD_HDG_FAC
				if B738DR_pfd_hdg_mode_arm == PFD_HDG_FAC_ARM then
					B738DR_pfd_hdg_mode_arm = 0
				end
			else
				B738DR_pfd_hdg_mode = 0
			end
		-- VOR LOC / ROLLOUT
		elseif ap_roll_mode == 9 and ap_roll_mode_eng == 9 then
			B738DR_pfd_hdg_mode = PFD_HDG_VOR_LOC
			B738DR_pfd_hdg_mode_arm = PFD_HDG_ROLLOUT_ARM
		-- ROLLOUT
		elseif ap_roll_mode == 19 and ap_roll_mode_eng == 19 then
			B738DR_pfd_hdg_mode = PFD_HDG_ROLLOUT
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_ROLLOUT_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ROLLOUT_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		-- LOC GP
		elseif ap_roll_mode == 14 and ap_roll_mode_eng == 14 then
			B738DR_pfd_hdg_mode = PFD_HDG_VOR_LOC
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_VOR_LOC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		-- TO GA LOCK TRK
		elseif ap_roll_mode == 15 and ap_roll_mode_eng == 15 then
			B738DR_pfd_hdg_mode = 0
			B738DR_pfd_hdg_mode_arm = 0
		-- TO GA LOCK TRK / LNAV arm
		elseif ap_roll_mode == 18 and ap_roll_mode_eng == 18 then
			B738DR_pfd_hdg_mode = 0
			B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_ARM
		-- HDG / VOR LOC
		elseif ap_roll_mode == 16 and ap_roll_mode_eng == 16 then
			B738DR_pfd_hdg_mode = PFD_HDG_HDG_SEL
			B738DR_pfd_hdg_mode_arm = PFD_HDG_VOR_LOC_ARM
		-- HDG / APP GS
		elseif ap_roll_mode == 17 and ap_roll_mode_eng == 17 then
			B738DR_pfd_hdg_mode = PFD_HDG_HDG_SEL
			B738DR_pfd_hdg_mode_arm = PFD_HDG_VOR_LOC_ARM
		-- HDG / APP GP
		elseif ap_roll_mode == 12 and ap_roll_mode_eng == 12 then
			B738DR_pfd_hdg_mode = PFD_HDG_HDG_SEL
			B738DR_pfd_hdg_mode_arm = PFD_HDG_VOR_LOC_ARM
		end
		
		if B738DR_fd_on == 0 or recycled_fd_roll ~= 0 then
			if B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_VOR_LOC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ROLLOUT_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_FAC_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_LNAV_ARM then
				B738DR_pfd_hdg_mode_arm = 0
			end
		else
			if lnav_ma_armed ~= 0 then
				if B738DR_pfd_hdg_mode_arm == PFD_HDG_VOR_LOC_ARM then
					B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_VOR_LOC_ARM
				elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_ROLLOUT_ARM then
					B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_ROLLOUT_ARM
				elseif B738DR_pfd_hdg_mode_arm == PFD_HDG_FAC_ARM then
					B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_FAC_ARM
				else
					B738DR_pfd_hdg_mode_arm = PFD_HDG_LNAV_ARM
				end
			end
		end
	end
	

	if fma_pitch_timer == 0 then
		-- PITCH modes arm
		if ap_pitch_mode == 3 then
			if vs_armed == 0 then
				if B738DR_pfd_alt_mode_arm == PFD_ALT_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			else
				B738DR_pfd_alt_mode_arm = PFD_ALT_VS_ARM
			end
		else
			if B738DR_pfd_alt_mode_arm == PFD_ALT_VS_ARM then
				B738DR_pfd_alt_mode_arm = 0
			elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GS_VS_ARM then
				B738DR_pfd_alt_mode_arm = 0
			elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GP_VS_ARM then
				B738DR_pfd_alt_mode_arm = 0
			end
		end
		
		if B738DR_gls_gs_status == 1 then
			B738DR_pfd_alt_mode_arm = PFD_ALT_GS_ARM
		else
			if ap_roll_mode == 3 or ap_roll_mode == 6 or ap_roll_mode == 17 then
				B738DR_pfd_alt_mode_arm = PFD_ALT_GS_ARM
			else
				if B738DR_pfd_alt_mode_arm == PFD_ALT_GS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			end
		end
		
		if B738DR_gp_status == 1 then
			if vs_armed == 0 then
				B738DR_pfd_alt_mode_arm = PFD_ALT_GP_ARM
			else
				B738DR_pfd_alt_mode_arm = PFD_ALT_GP_VS_ARM
			end
		else
			if ap_roll_mode == 7 or ap_roll_mode == 8 or ap_roll_mode == 12 or ap_roll_mode == 13 then
				if vs_armed == 0 then
					B738DR_pfd_alt_mode_arm = PFD_ALT_GP_ARM
				else
					B738DR_pfd_alt_mode_arm = PFD_ALT_GP_VS_ARM
				end
			else
				if B738DR_pfd_alt_mode_arm == PFD_ALT_GP_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GP_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			end
		end
		
		if ap_pitch_mode == 13 then
			B738DR_pfd_alt_mode_arm = PFD_ALT_FLARE_ARM
		else
			if B738DR_pfd_alt_mode_arm == PFD_ALT_FLARE_ARM then
				B738DR_pfd_alt_mode_arm = 0
			end
		end
		
		
		if B738DR_autopilot_vnav_status == 1 then
			if vnav_engaged == 0 then
				if ap_pitch_mode ~= 3 then	--not ALT HLD
					B738DR_pfd_alt_mode_arm = PFD_ALT_VNAV_ARM
				end
			else
				if B738DR_pfd_alt_mode_arm == PFD_ALT_VNAV_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			end
		else
			if B738DR_pfd_alt_mode_arm == PFD_ALT_VNAV_ARM then
				B738DR_pfd_alt_mode_arm = 0
			end
		end
		if vnav_engaged == 0 then
			if B738DR_pfd_alt_mode == PFD_ALT_VNAV_SPD or B738DR_pfd_alt_mode == PFD_ALT_VNAV_PTH or B738DR_pfd_alt_mode == PFD_ALT_VNAV_ALT then
				B738DR_pfd_alt_mode = 0
			end
		end
		
		--PITCH modes
		if ap_pitch_mode < 8 or ap_pitch_mode > 12 then		--not TO/GA mode
			B738DR_autopilot_to_ga_pfd = 0
			B738DR_autopilot_thr_hld_pfd = 0
			B738DR_at_throttle_hold = 0	-- mode throttle HLD off
			to_after_80kts = 0
			B738DR_ap_goaround = 0
			B738DR_fd_goaround = 0
			fd_go_disable = 0
			ap_go_disable = 0
			
			if ap_pitch_mode == 0 and ap_pitch_mode_eng == 0 then
				B738DR_pfd_alt_mode = 0
				if B738DR_pfd_alt_mode_arm ~= PFD_ALT_GS_ARM and B738DR_pfd_alt_mode_arm ~= PFD_ALT_GP_ARM 
				and B738DR_pfd_alt_mode_arm ~= PFD_ALT_GS_VS_ARM and B738DR_pfd_alt_mode_arm ~= PFD_ALT_GP_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif (B738DR_pfd_alt_mode_arm == PFD_ALT_GS_ARM or B738DR_pfd_alt_mode_arm == PFD_ALT_GS_VS_ARM) and ap_roll_mode ~= 3 and ap_roll_mode ~= 6 then
					B738DR_pfd_alt_mode_arm = 0
				end
			-- V/S
			elseif ap_pitch_mode == 1 and ap_pitch_mode_eng == 1 then
				B738DR_pfd_alt_mode = PFD_ALT_VS
				if B738DR_pfd_alt_mode_arm == PFD_ALT_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GS_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GP_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			-- LVL CHG
			elseif ap_pitch_mode == 2 and ap_pitch_mode_eng == 2 then
				B738DR_pfd_alt_mode = PFD_ALT_MCP_SPD
			-- ALT HOLD
			elseif ap_pitch_mode == 3 and ap_pitch_mode_eng == 3 then
				B738DR_pfd_alt_mode = PFD_ALT_ALT_HOLD
			-- G/S
			elseif ap_pitch_mode == 4 and ap_pitch_mode_eng == 4 then
				B738DR_pfd_alt_mode = PFD_ALT_GS
				if B738DR_pfd_alt_mode_arm == PFD_ALT_GS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GS_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GP_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			elseif ap_pitch_mode == 6 and ap_pitch_mode_eng == 6 then
				B738DR_pfd_alt_mode = PFD_ALT_ALT_ACQ
			end
			
			-- LNAV -> APP
			if ap_roll_mode == 6 and ap_roll_mode_eng == 6 then
				if vs_armed == 0 then
					if B738DR_pfd_alt_mode ~= PFD_ALT_GS then
						B738DR_pfd_alt_mode_arm = PFD_ALT_GS_ARM
					end
				else
					if B738DR_pfd_alt_mode ~= PFD_ALT_GS_VS_ARM then
						B738DR_pfd_alt_mode_arm = PFD_ALT_GS_VS_ARM
					end
				end
			elseif ap_roll_mode == 3 and ap_roll_mode_eng == 3 then
				-- APP
				if vs_armed == 0 then
					if B738DR_pfd_alt_mode ~= PFD_ALT_GS then
						B738DR_pfd_alt_mode_arm = PFD_ALT_GS_ARM
					end
				else
					if B738DR_pfd_alt_mode ~= PFD_ALT_GS_VS_ARM then
						B738DR_pfd_alt_mode_arm = PFD_ALT_GS_VS_ARM
					end
				end
			end
			if simDR_glideslope_status == 2 --then
			or B738DR_gls_gs_status == 2 
			or ap_pitch_mode == 4 then
				B738DR_pfd_alt_mode = PFD_ALT_GS
				if B738DR_pfd_alt_mode_arm == PFD_ALT_GS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GS_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GP_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			end
			if B738DR_gp_status < 2 then
				if B738DR_pfd_alt_mode == PFD_ALT_GP then
					B738DR_pfd_alt_mode = 0
				end
			elseif B738DR_gp_status == 2 then
				B738DR_pfd_alt_mode = PFD_ALT_GP
				if B738DR_pfd_alt_mode_arm == PFD_ALT_GP_ARM then
					B738DR_pfd_alt_mode_arm = 0
				elseif B738DR_pfd_alt_mode_arm == PFD_ALT_GP_VS_ARM then
					B738DR_pfd_alt_mode_arm = 0
				end
			end
			if ap_pitch_mode == 14 then
				B738DR_pfd_alt_mode = PFD_ALT_FLARE
				B738DR_pfd_alt_mode_arm = 0
			end
		end
		if vnav_pfd_alt_mode >= 0 then
			B738DR_pfd_alt_mode = vnav_pfd_alt_mode
		end
	end
	
	-- MASTER CAPT / FO
	if B738DR_autoland_status == 1 and simDR_radio_height_pilot_ft < 1500 and B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1 
	and ap_on ~= 0 then
		if master_light_block_act == 0 then 
			autopilot_master_capt_status = 1
			autopilot_master_fo_status = 1
		end
	elseif ap_roll_mode == 3 and ap_pitch_mode == 4 and B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1 
	and ap_on == 0 then
		if master_light_block_act == 0 then 
			autopilot_master_capt_status = 1
			autopilot_master_fo_status = 1
		end
	elseif ap_pitch_mode == 8 or ap_pitch_mode == 11 then
		if master_light_block_act == 0 then 
			autopilot_master_capt_status = 1
			autopilot_master_fo_status = 1
		end
	else
		if B738DR_autopilot_fd_pos == 0
		and B738DR_autopilot_fd_fo_pos == 0 then
			if ap_on == 0 then
				autopilot_master_capt_status = 0
				autopilot_master_fo_status = 0
				if is_timer_scheduled(master_light_block) == true then
					stop_timer(master_light_block)
					master_light_block_act = 0
				end
			else
				if master_light_block_act == 0 then
					if autopilot_side == 0 then
						autopilot_master_capt_status = 1
						autopilot_master_fo_status = 0
						if B738DR_autoland_status == 0 then
							if autopilot_cmd_b_status == 1 then
								autopilot_cmd_a_status = 1
							end
							autopilot_cmd_b_status = 0
						end
					else
						autopilot_master_capt_status = 0
						autopilot_master_fo_status = 1
						if B738DR_autoland_status == 0 then
							if autopilot_cmd_a_status == 1 then
								autopilot_cmd_b_status = 1
							end
							autopilot_cmd_a_status = 0
						end
					end
					B738DR_autopilot_side = autopilot_side
					simDR_autopilot_side = autopilot_side
				end
			end
		else
			if ap_on == 0 then
				if master_light_block_act == 0 then
					if autopilot_side == 0 then
						autopilot_master_capt_status = 1
						autopilot_master_fo_status = 0
						if B738DR_autoland_status == 0 then
							if autopilot_cmd_b_status == 1 and B738DR_autopilot_fd_pos == 1 then
								autopilot_cmd_a_status = 1
							end
							autopilot_cmd_b_status = 0
						end
					else
						autopilot_master_capt_status = 0
						autopilot_master_fo_status = 1
						if B738DR_autoland_status == 0 then
							if autopilot_cmd_a_status == 1 and B738DR_autopilot_fd_fo_pos == 1 then
								autopilot_cmd_b_status = 1
							end
							autopilot_cmd_a_status = 0
						end
					end
					B738DR_autopilot_side = autopilot_side
					simDR_autopilot_side = autopilot_side
				end
			else
				if master_light_block_act == 0 then
					if autopilot_side == 0 then
						autopilot_master_capt_status = 1
						autopilot_master_fo_status = 0
						if B738DR_autoland_status == 0 then
							if autopilot_cmd_b_status == 1 then
								autopilot_cmd_a_status = 1
							end
							autopilot_cmd_b_status = 0
						end
					else
						autopilot_master_capt_status = 0
						autopilot_master_fo_status = 1
						if B738DR_autoland_status == 0 then
							if autopilot_cmd_a_status == 1 then
								autopilot_cmd_b_status = 1
							end
							autopilot_cmd_a_status = 0
						end
					end
					B738DR_autopilot_side = autopilot_side
					simDR_autopilot_side = autopilot_side
				end
			end
		end
	end
	
	if B738DR_lights_test == 1 then
		B738DR_autopilot_master_capt_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_master_fo_status = 1 * B738DR_brightness2_export
	else
		B738DR_autopilot_master_capt_status = autopilot_master_capt_status
		B738DR_autopilot_master_fo_status = autopilot_master_fo_status
	end
	
	if B738DR_autopilot_fd_pos == 0 and B738DR_autopilot_fd_fo_pos == 0 then
		B738DR_fd_on = 0
		if ap_on == 0 and cws_on == 0 then
			if fd_on_both ~= 0 then
				recycled_fd_roll = 1
			end
			if B738DR_fd_goaround == 0 and to_after_80kts == 0 then
				ap_roll_mode = 0
				ap_pitch_mode = 0
			else
				if fd_on_both ~= 0 then
					ap_roll_mode = 0
					ap_pitch_mode = 0
					B738DR_fd_goaround = 0
					B738DR_ap_goaround = 0
				end
			end
		end
		fd_on_both = 0
	else
		B738DR_fd_on = 1
	end
	
	if B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1 then
		fd_on_both = 1
	end
	
	if B738DR_autopilot_fd_pos == 0 or B738DR_autopilot_fd_fo_pos == 0 then
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then		-- if aircraft on ground
			if B738DR_autopilot_vnav_status == 1 and vnav_engaged == 0 then
				ap_pitch_mode = 0
			end
		end
	end
	
	if ap_roll_mode ~= 0 and ap_roll_mode ~= 15 and ap_roll_mode ~= 18 then
		recycled_fd_roll = 0
	end
	
	recycled_fd_roll = 0		--temporary

	-- PFD >> off-FD-CMD
	local fd_cmd = 0
	local fd_cmd_fo = 0
	local fd_cmd_show = 0
	local fd_cmd_fo_show = 0
	
	if ap_on == 1 then
		fd_cmd_show = 1
		fd_cmd = 2
		fd_cmd_fo_show = 1
		fd_cmd_fo = 2
	else
		if B738DR_autopilot_fd_pos == 1 or B738DR_fd_goaround ~= 0 or to_after_80kts ~= 0 then
			fd_cmd_show = 1
			fd_cmd = 1
		end
		if B738DR_autopilot_fd_fo_pos == 1 or B738DR_fd_goaround ~= 0 or to_after_80kts ~= 0 then
			fd_cmd_fo_show = 1
			fd_cmd_fo = 1
		end
	end
	
	B738DR_pfd_fd_cmd = fd_cmd
	B738DR_pfd_fd_cmd_fo = fd_cmd_fo
	B738DR_pfd_fd_cmd_show = fd_cmd_show			-- show/hide: FD/CMD on captain PFD
	B738DR_pfd_fd_cmd_fo_show = fd_cmd_fo_show	-- show/hide: FD/CMD on first officer PFD
	
	-- CMD / CWS
	if B738DR_lights_test == 1 then
		B738DR_autopilot_cmd_a_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_cmd_b_status = 1 * B738DR_brightness2_export
	elseif B738DR_autoland_status == 0 then
		B738DR_autopilot_cmd_a_status = autopilot_cmd_a_status
		B738DR_autopilot_cmd_b_status = autopilot_cmd_b_status
	else
		B738DR_autopilot_cmd_a_status = 1
		B738DR_autopilot_cmd_b_status = 1
	end
	if B738DR_lights_test == 1 then
		B738DR_autopilot_cws_a_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_cws_b_status = 1 * B738DR_brightness2_export
	else
		B738DR_autopilot_cws_a_status = autopilot_cws_a_status
		B738DR_autopilot_cws_b_status = autopilot_cws_b_status
	end
	
	B738DR_cws_status = autopilot_cws_status
	B738DR_cws_r_status = autopilot_cws_r_status
	B738DR_cws_p_status = autopilot_cws_p_status
	
	simDR_ap_fo_heading = simDR_ap_capt_heading
	
	-- ILS test - pointer blink
	if ils_test_on == 0 then
		B738DR_ils_pointer_disable = 0
	else
		B738DR_ils_pointer_disable = blink_out + 1
	end
	
	-- SHOW/HIDE IAS
	if vnav_engaged == 1 then
		-- VNAV on
		if B738DR_ap_spd_interv_status == 0 then
			B738DR_show_ias = 0
		else
			B738DR_show_ias = 1
		end
	elseif B738DR_ap_goaround > 0 or B738DR_fd_goaround > 0 then
		if fd_goaround_single == 99 then
			B738DR_show_ias = 1
		else
			B738DR_show_ias = 0
		end
	else
		B738DR_show_ias = 1
	end

end

function B738_bank_angle()
	
	if B738DR_heading_mode == 1 or B738DR_heading_mode == 7 or B738DR_heading_mode == 10 or B738DR_heading_mode == 12 or 
	B738DR_heading_mode == 16 or B738DR_heading_mode == 17 then
		if B738DR_autopilot_bank_angle_pos == 4 then
			simDR_bank_angle = 6
		elseif B738DR_autopilot_bank_angle_pos == 3 then
			simDR_bank_angle = 5
		elseif B738DR_autopilot_bank_angle_pos == 2 then
			simDR_bank_angle = 4
		elseif B738DR_autopilot_bank_angle_pos == 1 then
			simDR_bank_angle = 3
		elseif B738DR_autopilot_bank_angle_pos == 0 then
			simDR_bank_angle = 2
		end
	end
end

function B738_lnav3()

	local wca = 0
--	local ws = 0
	local wh = 0
	local tas = 0
	local awa = 0
	local relative_brg = 0
	local relative_brg2 = 0
	local relative_brg3 = 0
	local relative_brg4 = 0
	local relative_brg5 = 0
	local bearing_corr = 0
	local idx_corr = 0
	local idx_corr2 = 0
	local idx_dist = 0
	local hdg_corr = 0
	local mag_hdg = 0
	local mag_trk = 0	-- temporary without mag variantion
	local ap_hdg = 0
	local rnp = 0
	local fix_bank_angle = 0
	local fac_capture = 0
	local idx_rnp = 0
	local scale_horizont = 0
	local pom1 = 0
	local gnd_spd = 0
	local target_hdg = 0
	local max_idx_dist = 0
	local radii_turn = 0
	local integral_tgt = 0
	
	--B738DR_load_save
	local valid_data = false
	if loaded_flight_timer == 0 then
		valid_data = true
	end
	
	if ap_roll_mode == 4 and ap_roll_mode_eng == 4 then	-- LNAV
		if B738DR_lnav_engaged == 0 then
			if B738DR_fpln_nav_id == "VECTOR" or B738DR_fpln_nav_id == "DISCON" then
				B738DR_lnav_engaged = 0
			elseif simDR_radio_height_pilot_ft > 50 and valid_data then
			-- elseif simDR_altitude_pilot > B738DR_aerodrom_altitude + 50 then
				-- if B738DR_legs_intdir_act ~= 0 and B738DR_legs_intdir_crs == -1 then
					-- B738DR_lnav_engaged = 1
				-- elseif B738DR_fms_legs_offset_engage == 1 and B738DR_xtrack - B738DR_fms_legs_offset_dist > -3 and B738DR_xtrack - B738DR_fms_legs_offset_dist < 3 then
					-- B738DR_lnav_engaged = 1
				-- elseif B738DR_xtrack > -3 and B738DR_xtrack < 3 then
					-- B738DR_lnav_engaged = 1
				-- else
					-- B738DR_lnav_engaged = 2
					-- lnav_lock_hdg = simDR_ahars_mag_hdg
				-- end
				
				-- if B738DR_legs_intdir_act == 2 then
					-- if B738DR_fms_legs_offset_engage == 1 and B738DR_xtrack - B738DR_fms_legs_offset_dist > -3 and B738DR_xtrack - B738DR_fms_legs_offset_dist < 3 then
						-- B738DR_lnav_engaged = 1
					-- elseif B738DR_xtrack > -3 and B738DR_xtrack < 3 then
						-- B738DR_lnav_engaged = 1
					-- else
						-- B738DR_lnav_engaged = 2
						-- lnav_lock_hdg = simDR_ahars_mag_hdg
					-- end
				-- else
					-- B738DR_lnav_engaged = 2
					-- lnav_lock_hdg = simDR_ahars_mag_hdg
				-- end
				
				B738DR_lnav_engaged = 1
				
				ap_roll_mode = 4	-- LNAV
				ap_roll_mode_eng = 4
				lnav_tune_enable = 0
			end
		end
		hold_bank_angle_enable = 0
	elseif ap_roll_mode == 10 and ap_roll_mode_eng == 10 then	-- HDG/LNAV arm
		if B738DR_lnav_engaged == 0 then
			if B738DR_fpln_nav_id == "VECTOR" or B738DR_fpln_nav_id == "DISCON" then
				B738DR_lnav_engaged = 0
			elseif simDR_radio_height_pilot_ft > 50 and valid_data then
			-- elseif simDR_altitude_pilot > B738DR_aerodrom_altitude + 50 then
				-- if B738DR_legs_intdir_act ~= 0 and B738DR_legs_intdir_crs == -1 then
					-- B738DR_lnav_engaged = 1
				-- elseif B738DR_fms_legs_offset_engage == 1 and B738DR_xtrack - B738DR_fms_legs_offset_dist > -3 and B738DR_xtrack - B738DR_fms_legs_offset_dist < 3 then
					-- B738DR_lnav_engaged = 1
				-- elseif B738DR_xtrack > -3 and B738DR_xtrack < 3 then
					-- B738DR_lnav_engaged = 1
				-- else
					-- B738DR_lnav_engaged = 2
					-- lnav_lock_hdg = simDR_ahars_mag_hdg
				-- end
				if B738DR_legs_intdir_act == 2 then
					if B738DR_fms_legs_offset_engage == 1 and B738DR_xtrack - B738DR_fms_legs_offset_dist > -3 and B738DR_xtrack - B738DR_fms_legs_offset_dist < 3 then
						B738DR_lnav_engaged = 1
					elseif B738DR_xtrack > -3 and B738DR_xtrack < 3 then
						B738DR_lnav_engaged = 1
					-- else
						-- B738DR_lnav_engaged = 2
						-- lnav_lock_hdg = simDR_ahars_mag_hdg
					end
				else
					-- B738DR_lnav_engaged = 2
					-- lnav_lock_hdg = simDR_ahars_mag_hdg
					if B738DR_xtrack > -3 and B738DR_xtrack < 3 then
						B738DR_lnav_engaged = 1
					end
				end
				if B738DR_lnav_engaged > 0 then
					ap_roll_mode = 4	-- LNAV
					ap_roll_mode_eng = 4
					lnav_tune_enable = 0
				end
			end
			
		end
		hold_bank_angle_enable = 0
	elseif ap_roll_mode == 5 and ap_roll_mode_eng == 5 then		-- LNAV/VOR LOC arm
		if simDR_nav_status > 1 then
			ap_roll_mode = 2	-- VOR LOC
			ap_roll_mode_eng = 2
			B738DR_lnav_engaged = 0
			hold_bank_angle_enable = 1
			hold_bank_angle = B738DR_autopilot_bank_angle_pos 
		else
			hold_bank_angle_enable = 0
		end
	elseif ap_roll_mode == 6 and ap_roll_mode_eng == 6 then		-- LNAV/APP arm (G/S)
		if simDR_nav_status == 2 and simDR_approach_status == 0 then
			if B738DR_fms_ils_disable == 0 then
				simCMD_autopilot_app:once()
			end
			ap_roll_mode = 3
			ap_roll_mode_eng = 3
			B738DR_lnav_engaged = 0
			hold_bank_angle_enable = 1
			hold_bank_angle = B738DR_autopilot_bank_angle_pos 
		else
			hold_bank_angle_enable = 0
		end
		if simDR_approach_status == 2 then
			if ap_pitch_mode_eng ~= 4 and (simDR_glideslope_status == 2 or ap_pitch_mode == 4 ) then 	-- G/S engaged
				ap_pitch_mode = 4
				if at_mode ~= 2 then
					if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
						end
					end
				end
			end
		end
	elseif ap_roll_mode == 12 and ap_roll_mode_eng == 12 then		-- HDG/VOR LOC arm GP
		if simDR_nav_status > 1 then
			ap_roll_mode = 14	-- LOC GP
			ap_roll_mode_eng = 14
			B738DR_lnav_engaged = 0
		end
		hold_bank_angle_enable = 0
	elseif ap_roll_mode == 13 and ap_roll_mode_eng == 13 then		-- LNAV/VOR LOC arm GP
		if simDR_nav_status > 1 then
			ap_roll_mode = 14	-- LOC GP
			ap_roll_mode_eng = 14
			B738DR_lnav_engaged = 0
			hold_bank_angle_enable = 1
			hold_bank_angle = B738DR_autopilot_bank_angle_pos 
		else
			hold_bank_angle_enable = 0
		end
	elseif ap_roll_mode == 8 and ap_roll_mode_eng == 8 then		-- LNAV/FAC arm (G/P)
		if B738DR_fac_status == 0 then
			B738DR_fac_status = 1
		end
		hold_bank_angle_enable = 0
	else
		B738DR_lnav_engaged = 0
		if ap_roll_mode <= 1 then
			hold_bank_angle_enable = 0
		end
	end
	
	if lnav_ma_armed ~= 0 and valid_data then
		if B738DR_ap_goaround > 0 or B738DR_fd_goaround > 0 then
			
			B738DR_lnav_engaged = 0
			
			if simDR_radio_height_pilot_ft < 400 then
				if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
					if recycled_fd_roll == 0 then
						-- before MA
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 18
						B738DR_lnav_engaged = 0
					end
				else
					-- after MA
					if B738DR_ap_goaround > 0 then
						if simDR_radio_height_pilot_ft < 400 then
							if recycled_fd_roll == 0 then
								to_ga_hdg = simDR_ahars_mag_hdg
								ap_roll_mode = 18
								B738DR_lnav_engaged = 0
							end
						else
							ap_roll_mode = 4
							B738DR_lnav_engaged = 1
						end
					else
						if simDR_radio_height_pilot_ft < 50 then
							if recycled_fd_roll == 0 then
								to_ga_hdg = simDR_ahars_mag_hdg
								ap_roll_mode = 18
								B738DR_lnav_engaged = 0
							end
						else
							ap_roll_mode = 4
							B738DR_lnav_engaged = 1
						end
					end
				end
			else
				ap_roll_mode = 4
				B738DR_lnav_engaged = 1
			end
			lnav_ma_armed = 0
			
			if B738DR_lnav_engaged == 1 then
				ap_roll_mode = 4	-- LNAV
				ap_roll_mode_eng = 4
				if ap_pitch_mode == 4 then	--GS
					ap_pitch_mode = 0
				end
				lnav_tune_enable = 0
			end
			hold_bank_angle_enable = 0
		end
		
	end
	
	if B738DR_legs_intdir_act == 0 or legs_intdir ~= legs_intdir_old then	--or B738DR_legs_intdir_act == 2 then --or B738DR_wpt_path == "DF" or B738DR_wpt_path == "VM" then
		intdir_act = 0
	end
	legs_intdir_old = legs_intdir
	
	if B738DR_lnav_engaged == 1 then
		if simDR_autopilot_heading_mode ~= 1 then
			simCMD_autopilot_hdg:once()
		end
		
		-- if B738DR_legs_intdir_act == 0 or legs_intdir ~= legs_intdir_old then	--or B738DR_legs_intdir_act == 2 then --or B738DR_wpt_path == "DF" or B738DR_wpt_path == "VM" then
			-- intdir_act = 0
		-- else
			if intdir_act == 0 and B738DR_legs_intdir_act == 2 then
				intdir_act = 2
				turn_active = 0
				lnav_tune_enable = 0
				idx_corr2_anim = 0
				max_idx_dist_anim = 1.1
				B738DR_idx_corr3 = 0
			end
		-- end
		
		if intdir_act == 2 then
			if B738DR_legs_intdir_crs == -1 then
				intdir_act = 1
			elseif B738DR_xtrack > -3 and B738DR_xtrack < 3 then
				intdir_act = 1
			elseif B738DR_fms_legs_offset_engage == 1 and B738DR_xtrack - B738DR_fms_legs_offset_dist > -3 and B738DR_xtrack - B738DR_fms_legs_offset_dist < 3 then
				intdir_act = 1
			end
			ap_lnav_hdg = simDR_ahars_mag_hdg
			wind_correction()
		elseif intdir_act < 2 then
		
			-- heading to track
			mag_trk = B738DR_fmc_trk + simDR_mag_variation
			relative_brg = (B738DR_fmc_trk - simDR_ahars_mag_hdg + 360) % 360
			if relative_brg > 180 then
				relative_brg = relative_brg - 360
			end
			
			relative_brg2 = (B738DR_fmc_crs - simDR_ahars_mag_hdg + 360) % 360
			if relative_brg2 > 180 then
				relative_brg2 = relative_brg2 - 360
			end
			
			relative_brg3 = (mag_trk - simDR_ahars_mag_hdg + 360) % 360
			if relative_brg3 > 180 then
				relative_brg3 = relative_brg3 - 360
			end
			
			relative_brg4 = (mag_trk - simDR_mag_hdg + 360) % 360
			if relative_brg4 > 180 then
				relative_brg4 = relative_brg4 - 360
			end
			relative_brg4 = -relative_brg4
			
			relative_brg5 = (B738DR_fmc_crs - simDR_mag_hdg + 360) % 360
			if relative_brg5 > 180 then
				relative_brg5 = relative_brg5 - 360
			end
			
			-- if turn_active < 2 then
				-- -- if B738DR_fmc_trk_turn == 2 then
				-- if B738DR_fmc_trk_turn2 == 2 then
					-- -- left turn
					-- idx_corr = -100
					-- turn_active = 1
				-- -- elseif B738DR_fmc_trk_turn == 3 then
				-- elseif B738DR_fmc_trk_turn2 == 3 then
					-- -- right turn
					-- idx_corr = 100
					-- turn_active = 1
				-- end
			-- end
			
			-- -- if B738DR_fmc_trk_turn < 2 then
			-- if B738DR_fmc_trk_turn2 < 2 then
				-- turn_active = 0
			-- end
			
			turn_active = 0
			
			if turn_active == 1 then
				ap_hdg = (simDR_ahars_mag_hdg + idx_corr + 360) % 360
					-- if B738DR_fmc_trk_turn == 2 and relative_brg2 > -80 and relative_brg2 < 0 then
					if B738DR_fmc_trk_turn2 == 2 and relative_brg2 > -80 and relative_brg2 < 0 then
						turn_active = 2
						idx_corr = -45
					end
					-- if B738DR_fmc_trk_turn == 3 and relative_brg2 < 80 and relative_brg2 > 0 then
					if B738DR_fmc_trk_turn2 == 3 and relative_brg2 < 80 and relative_brg2 > 0 then
						turn_active = 2
						idx_corr = 45
					end
					simDR_bank_angle = 6
				-- end
				lnav_tune_enable = 0
				idx_corr2_anim = 0
				max_idx_dist_anim = 1.1
				B738DR_idx_corr3 = 0
			else
				-- if B738DR_legs_intdir_act == 1 and B738DR_legs_intdir_crs == -1 then
					-- -- Direct To Fix
					-- ap_hdg = B738DR_fmc_crs
					-- B738DR_idx_corr3 = 0
				-- elseif B738DR_legs_intdir_act == 3 then
					-- -- Direct To Fix
					-- ap_hdg = B738DR_fmc_crs
					-- B738DR_idx_corr3 = 0
				-- elseif B738DR_legs_intdir_act == 2 then
					-- -- Direct To Fix
					-- ap_hdg = B738DR_fmc_crs
					-- B738DR_idx_corr3 = 0
				if B738DR_nav_mode == 1 then
					-- Direct To Fix
					ap_hdg = B738DR_idx_corr2
					B738DR_idx_corr3 = 0
				elseif B738DR_nav_mode == 5 then
					ap_hdg = B738DR_idx_corr2
					B738DR_idx_corr3 = 0
				elseif B738DR_nav_mode == 9 then
					ap_hdg = B738DR_idx_corr2
					B738DR_idx_corr3 = 0
				elseif B738DR_nav_mode == 6 then
					ap_hdg = B738DR_idx_corr
					B738DR_idx_corr3 = 0
				elseif B738DR_nav_mode == 3 then	--and B738DR_hold_phase >= 0 and B738DR_hold_phase <= 16 then
					if B738DR_hold_phase == 0 or B738DR_hold_phase == 2 or B738DR_hold_phase == 5 or B738DR_hold_phase == 7 
					or B738DR_hold_phase == 9 or B738DR_hold_phase == 12 or B738DR_hold_phase == 15 then
						ap_hdg = B738DR_idx_corr
					else
						ap_hdg = B738DR_idx_corr2
					end
					B738DR_idx_corr3 = 0
				-- elseif B738DR_nav_mode == 4 or (B738DR_wpt_path == "RF" and (B738DR_nav_mode == 3 or B738DR_nav_mode == 7)) then
				-- --or B738DR_wpt_path == "AF" or B738DR_wpt_path == "RF" then
					
					-- ap_hdg = B738DR_idx_corr2
					
					-- B738DR_idx_corr3 = 0
					
				-- -- elseif B738DR_wpt_path == "HA" or B738DR_wpt_path == "HF" or B738DR_wpt_path == "HM" then
					-- -- -- Hold
					-- -- if B738DR_hold_phase == 0 then
						-- -- ap_hdg = B738DR_fmc_crs
					-- -- else
						-- -- ap_hdg = B738DR_fmc_trk
					-- -- end
					-- -- simDR_bank_angle = 6	--5
					-- -- fix_bank_angle = 1
					-- -- B738DR_idx_corr3 = 0
				elseif B738DR_nav_mode == 7 then
					if B738DR_pi_phase == 1 or B738DR_pi_phase == 3 or B738DR_pi_phase == 5 or B738DR_pi_phase == 7 or B738DR_pi_phase == 8 then
						ap_hdg = B738DR_idx_corr
					else
						ap_hdg = B738DR_idx_corr2
					end
					B738DR_idx_corr3 = 0
				elseif B738DR_nav_mode == 4 or B738DR_nav_mode == 10 then
					ap_hdg = B738DR_idx_corr2
					B738DR_idx_corr3 = 0
				elseif B738DR_wpt_path == "VM" or B738DR_wpt_path == "FM" then
					ap_hdg = (B738DR_fmc_trk + simDR_mag_variation + 360) % 360
					B738DR_idx_corr3 = 0
				-- nav mode = 0,....
				else
					ap_hdg = B738DR_idx_corr
					B738DR_idx_corr3 = 0
				end
				--bank angle
				if B738DR_fms_vref ~= 0 and (B738DR_AP_airspeed <= B738DR_fms_vref - 3) and B738DR_AP_airspeed > 45 and simDR_radio_height_pilot_ft < 1500 then
					simDR_bank_angle = 3	-- bank angle protection below Vref speed
				elseif B738DR_fms_v2_set ~= 0 and (B738DR_AP_airspeed <= B738DR_fms_v2_set - 17) and B738DR_AP_airspeed > 45 and simDR_radio_height_pilot_ft < 1500 then
					simDR_bank_angle = 3	-- bank angle protection below V2+20
				else
					local bank_altitude_pilot = simDR_altitude_pilot
					if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						bank_altitude_pilot = B738DR_preselected_alt
					end
					if fix_bank_angle == 0 then
						if bank_altitude_pilot <= 400 then
							simDR_bank_angle = 2	--1
						elseif bank_altitude_pilot <= 19000 then
							simDR_bank_angle = 6	--B738DR_fmc_bank_angle
						else
							if B738DR_nav_mode == 3 then
								simDR_bank_angle = 5	--5
							else
								simDR_bank_angle = 5	--4
							end
						end
					end
				end
			end
			
			ap_lnav_hdg = ap_hdg
			wind_correction()
			simDR_ap_capt_heading = (ap_hdg - B738DR_ap_wca + 360) % 360
			
		elseif intdir_act == 3 then
			ap_lnav_hdg = simDR_ahars_mag_hdg
		end
		
	elseif B738DR_lnav_engaged == 2 then
		if simDR_autopilot_heading_mode ~= 1 then
			simCMD_autopilot_hdg:once()
		end
		simDR_ap_capt_heading = lnav_lock_hdg
		if B738DR_xtrack > -3 and B738DR_xtrack < 3 then
			B738DR_lnav_engaged = 1
		end
		ap_lnav_hdg = simDR_ahars_mag_hdg
		wind_correction()
	else
		
		if ap_roll_mode == 1 then
			relative_brg = (B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg + 360) % 360
			if relative_brg > 180 then
				relative_brg = relative_brg - 360
			end
			-- test with delay 1 seconds
			if relative_brg > -7 and relative_brg < 7 then
				hdg_turn = 0
			end
			if relative_brg > 145 then
				if hdg_turn == 0 then
					hdg_turn = 1	-- right
				end
			elseif relative_brg < -145 then
				if hdg_turn == 0 then
					hdg_turn = 2	-- left
				end
			end
			if hdg_turn == 1 then
				if relative_brg < 0 or relative_brg > 175 then
					target_hdg = (simDR_ahars_mag_hdg + 175) % 360
				else
					target_hdg = B738DR_mcp_hdg_dial
				end
			elseif hdg_turn == 2 then
				if relative_brg > 0 or relative_brg < -175 then
					target_hdg = (simDR_ahars_mag_hdg - 175 + 360) % 360
				else
					target_hdg = B738DR_mcp_hdg_dial
				end
			else
				target_hdg = B738DR_mcp_hdg_dial
			end
			simDR_ap_capt_heading = target_hdg
		elseif ap_roll_mode == 15 or ap_roll_mode == 18 then	-- TO GA lock track
			simDR_ap_capt_heading = to_ga_hdg
			hdg_turn = 0
			if B738DR_pfd_alt_mode ~= PFD_ALT_TO_GA then
				ap_roll_mode = 1
				simDR_ap_capt_heading = B738DR_mcp_hdg_dial
			end
		else
			simDR_ap_capt_heading = B738DR_mcp_hdg_dial
			hdg_turn = 0
		end
		ap_lnav_hdg = simDR_ahars_mag_hdg
		wind_correction()
	end
	
	if B738DR_fpln_active ~= 0 then
		idx_corr = B738DR_xtrack
		if idx_corr < 0 then
			idx_corr = -idx_corr
			if idx_corr > B738DR_rnp then
				idx_corr = B738DR_rnp
			end
			scale_horizont = -B738_rescale(0, 0, B738DR_rnp, 2.5, idx_corr)
		else
			if idx_corr > B738DR_rnp then
				idx_corr = B738DR_rnp
			end
			scale_horizont = B738_rescale(0, 0, B738DR_rnp, 2.5, idx_corr)
		end
		B738DR_gps_horizont = scale_horizont
	end
	
end

function wind_correction()
	
	local awa = 0
	local wca = 0
	local mag_hdg = 0
	
	if B738DR_lnav_engaged == 1 then
		mag_hdg = ap_lnav_hdg
	-- elseif B738DR_fac_gp_engaged == 1 then
	elseif B738DR_fac_status == 2 then
		mag_hdg = B738DR_idx_corr5
	elseif ap_roll_mode == 7 and ap_roll_mode_eng == 7 then
		mag_hdg = B738DR_idx_corr5
	elseif ap_roll_mode == 8 and ap_roll_mode_eng == 8 then
		mag_hdg = B738DR_idx_corr5
	elseif ap_roll_mode == 11 and ap_roll_mode_eng == 11 then
		mag_hdg = B738DR_idx_corr5
	elseif B738DR_lpv_app_status == 2 then
		mag_hdg = B738DR_gls_corr
	elseif B738DR_gls_nav_status == 2 or B738DR_gls_app_status == 2 then
		mag_hdg = B738DR_gls_corr
	else
		mag_hdg = simDR_ahars_mag_hdg
	end
	
	local tas = simDR_TAS * 1.94384449244
	local wh = (simDR_wind_hdg + 180) % 360
	
	ap_wca_wind_hdg = B738_set_animation_rate_sec(ap_wca_wind_hdg, wh, -360, 360, 1.0)
	ap_wca_wind_spd = B738_set_animation_rate(ap_wca_wind_spd, simDR_wind_spd, 0, 200, 1)
	
	local relative_brg = (ap_wca_wind_hdg - mag_hdg + 360) % 360
	
	if tas > 40 then
		if relative_brg > 180 then
			relative_brg = relative_brg - 360
		end
		if relative_brg < -90 then
			awa = math.rad(180 + relative_brg)
			wca = math.asin(ap_wca_wind_spd * ((math.sin(awa) / tas)))
			wca = math.deg(-wca)
		elseif relative_brg < 0 then
			awa = math.rad(-relative_brg)
			wca = math.asin(ap_wca_wind_spd * ((math.sin(awa) / tas)))
			wca = math.deg(-wca)
		elseif relative_brg < 90 then
			awa = math.rad(relative_brg)
			wca = math.asin(ap_wca_wind_spd * ((math.sin(awa) / tas)))
			wca = math.deg(wca)
		else
			awa = math.rad(180 - relative_brg)
			wca = math.asin(ap_wca_wind_spd * ((math.sin(awa) / tas)))
			wca = math.deg(wca)
		end
	else
		wca = 0
		ap_wca_wind_hdg = wh
		ap_wca_wind_spd = simDR_wind_spd
	end
	
	B738DR_ap_wca = B738_set_animation_rate_sec(B738DR_ap_wca, wca, -360, 360, 1.0)
end


--- VNAV autopilot ---

function vnav_vvi_limit(in_vvi1, in_vvi2, in_min_vvi, in_vvi_speed)
	
	local in_vvi = in_vvi1 - in_vvi2
	local vvi_limit = 0
	if in_vvi1 >= in_vvi2 then
		vvi_limit = SIM_PERIOD * in_vvi_speed
	else
		vvi_limit = SIM_PERIOD * in_vvi_speed * 1.7
	end
	local result = in_vvi1
	
	if math.abs(in_vvi) > vvi_limit then
		if in_vvi < 0 then
			result = in_vvi2 - vvi_limit
		elseif in_vvi > 0 then
			result = in_vvi2 + vvi_limit
		end
	end
	if result > in_min_vvi then
		result = in_min_vvi
	end
	return result
end

function vnav_clb_spd_mode()
	if toga_was_engaged == 0 then
		B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
		B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
		if B738DR_autopilot_autothr_arm_pos == 1 then
			at_mode = 7
			B738DR_autopilot_n1_status = 1
			vnav_pfd_spd_mode = PFD_SPD_N1
			B738DR_at_throttle_hold = 0
		end
	else
		if simDR_altitude_pilot < B738DR_aerodrom_altitude + 800 and B738DR_lift_timer >= 10 then
			B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
			B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 3
				B738DR_autopilot_n1_status = 0
				vnav_pfd_spd_mode = PFD_SPD_THR_HLD
				B738DR_at_throttle_hold = 1	-- mode throttle HLD
			end
		elseif B738DR_thr_cutback == 1 then
			if (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
				B738DR_eng1_N1_thrust_trg = 1		--B738DR_eng1_N1_lim	--cutback
				B738DR_eng2_N1_thrust_trg = 1		--B738DR_eng2_N1_lim	--cutback
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 7
					B738DR_autopilot_n1_status = 1
					vnav_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			elseif simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_red_alt or B738DR_lift_timer >= 150 then
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 7
					B738DR_autopilot_n1_status = 1
					vnav_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			else
				B738DR_eng1_N1_thrust_trg = 1		--B738DR_eng1_N1_lim	--cutback
				B738DR_eng2_N1_thrust_trg = 1		--B738DR_eng2_N1_lim	--cutback
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 0	-- ARM
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					B738DR_autopilot_n1_status = 0
					vnav_pfd_spd_mode = PFD_SPD_ARM
				end
			end
		elseif simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_red_alt or B738DR_lift_timer >= 150 then
			B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
			B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 7
				B738DR_autopilot_n1_status = 1
				vnav_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			end
		else
			B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
			B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 0	-- ARM
				B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				B738DR_autopilot_n1_status = 0
				vnav_pfd_spd_mode = PFD_SPD_ARM
			end
		end
	end
end

function rest_wpt_spd_kts(in_spd)
	
	local result = in_spd
	
	if in_spd < 100 then
		local const_spd = 1
		if simDR_IAS > 45 then
			const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
		end
		result = (in_spd * (simDR_sos * 1.94384449244)) / const_spd
	end
	
	return result
end

function rest_wpt_spd_mach(in_spd)
	local result = in_spd
	
	if in_spd >= 100 then
		local const_spd = 1
		if simDR_IAS > 45 then
			const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
		end
		result = (in_spd * const_spd) / (simDR_sos * 1.94384449244)
	end
	
	return result
end

function B738_vnav6()

	local delta_vvi = 0
	local delta_alt_dial = 0
	local vnav_speed_delta2 = 0
	local speed_step = 0
	local delta_ap_alt = 0
	
	local flaps = simDR_flaps_ratio
	local v2_20_speed = math.floor(B738DR_fms_v2_set) + 15	--B738DR_fms_v2_15
	local v2 = 0
	local cca_alt = 0.0
	local nav_id = "*"
	local alt_x = 0
	local alt_x2 = 0
	local alt_x3 = 0
	local spd_250_10000 = 0
	
	local at_mode_old = 0
	local lvl_chg_fpm = 0
	local lvl_chg_alt = 0
	local lvl_chg_fpm2 = 0
	local lvl_chg_alt2 = 0
	local speed_step2 = 0
	local speed_step3 = 0
	local vnav_alt2 = 0
	local delta_alt_dial2 = 0
	local delta_vvi_mem = 0
	local vvi_limit = SIM_PERIOD * 75	--55 75 --150
	local vvi_des = 0
	local tgt_lim = 0
	local airspeed_dial = 0
	local const_spd = 0
	local mach_to_ias = 0
	
	local lvlchg_elev = math.max (simDR_elevation_m * 3.28, 0)
	lvlchg_elev = math.min(lvlchg_elev, 41000)
	local lvlchg_climb_rate = B738_rescale(0, 1.12, 41000, 0.45, lvlchg_elev) * 1.2
	local lvlchg_vvi = B738_rescale(0, 1100, 41000, 50, lvlchg_elev)	--1200
	local gw_lim = math.max(50, simDR_total_weight/1000)
	gw_lim = math.min(80, gw_lim)
	lvlchg_vvi = lvlchg_vvi * B738_rescale(50, 1, 80, 0.6, gw_lim)
	local lvlchg_vvi_d = B738_rescale(0, 250, 41000, 50, lvlchg_elev)	--1200
	local lvlchg_in_delta = B738_rescale(0, 4, 41000, 2, lvlchg_elev)	--2/1
	local delta_alt = 0
	
	local autopilot_n1_status_mem = 0
	-- local auto_descent_fail = 0
	
	local at_wakeup_show = 0
	
	local rest_wpt_spd = 0
	
	--B738DR_load_save
	local valid_data = false
	if loaded_flight_timer == 0 then
		if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
			valid_data = true
		end
	end
	
	if ap_pitch_mode == 11 then
		-- if simDR_radio_height_pilot_ft > 400 then
		if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
			if simDR_altitude_pilot > B738DR_aerodrom_altitude + 400 and valid_data then
				ap_pitch_mode = 5
				ap_pitch_mode_eng = 5
				vnav_engaged = 1
				toga_was_engaged = 1
				simDR_ap_altitude_dial_ft = mcp_alt_dial_app
				if B738DR_gp_status == 2 and ap_roll_mode == 11 then
					ap_roll_mode = 0
				end
				if B738DR_gls_app_status == 2 then
					ap_roll_mode = 0
					if simDR_autopilot_heading_mode == 1 then
						simCMD_autopilot_hdg:once()		-- HDG off
					end
				end
				B738DR_gls_app_status = 0
				B738DR_gls_gs_status = 0
				B738DR_lpv_app_status = 0
				B738DR_lpv_gs_status = 0
				
				B738DR_fd_goaround = 0
				B738DR_ap_goaround = 0
				init_climb = 0
				vnav_init = 0
				B738DR_vnav_alt_mode = 0
				B738DR_vnav_descent_disable = 0
				vnav_alt_hold = 0
				vnav_alt_hold_act = 0
				mcp_alt_hold = 99999
				
				if simDR_altitude_pilot < 26000 then
					if simDR_airspeed_is_mach == 1 then
						simCMD_autopilot_co:once()
					end
				end
			end
		end
	end
	
	if ap_pitch_mode ~= 8 and ap_pitch_mode ~= 11 and ap_pitch_mode ~= 0 then
		B738DR_at_takeoff = 0
	end
	
	-- if simDR_radio_height_pilot_ft < 350 or simDR_radio_height_pilot_ft > 3000 then
	if simDR_altitude_pilot < B738DR_aerodrom_altitude + 350 or simDR_altitude_pilot > B738DR_aerodrom_altitude + 3000 then
		toga_was_engaged = 0
	end
	
	B738DR_flaps_speed = 340
	B738DR_fmc_vnav_v2_20 = 340
	
	vnav_pfd_alt_mode = -1
	vnav_pfd_spd_mode = -1
	
	if ap_pitch_mode_eng == 5 and ap_pitch_mode == 5 and B738DR_autopilot_vnav_status == 1 then --ap_vnav_status == 2 then
		
		if vnav_engaged == 0 then
			if B738DR_flight_phase < 2 then
				-- if simDR_radio_height_pilot_ft > 400 or B738DR_missed_app_act > 0 then
				if simDR_altitude_pilot > B738DR_aerodrom_altitude + 400 and valid_data then
					vnav_engaged = 1
					B738DR_ap_spd_interv_status = 0
					spd_interv_co_used = 0
					vnav_desc_spd = 0
					if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
						B738DR_descent_mode = 0
					else
						B738DR_descent_mode = 2
					end
					vnav_desc_protect_spd = 0
				-- else
					-- vnav_engaged = 0
				end
			else
				vnav_engaged = 1
				B738DR_ap_spd_interv_status = 0
				spd_interv_co_used = 0
				vnav_desc_spd = 0
				if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
					B738DR_descent_mode = 0
				else
					B738DR_descent_mode = 2
				end
				vnav_desc_protect_spd = 0
			end
		end
		
		if vnav_engaged == 1 then
			
			if B738DR_fdir_pitch_active == 0 then
				if simDR_fdir_pitch_ovr == 1 then
					simDR_fdir_pitch_ovr = 0
				end
				B738DR_fdir_pitch_override = 0
			end
			
			if vnav_init == 0 then
				
				decel_dist_calc = false
				decel_dist_crz = 0
				fmc_cruise_speed_old = -1
				fmc_cruise_speed_mach_old = -1
				fmc_descent_speed_old = -1
				fmc_descent_speed_mach_old = -1
				
				-- B738DR_test_test5 = 0
				
				simDR_ap_altitude_dial_ft = mcp_alt_dial_app
				
				vnav_n1_dis_eng = 0
				retard_enabled = 0
				vnav_init2 = 0
				B738DR_vvi_dial_show = 0
				rest_wpt_alt_old = 0
				alt_x = at_mode
				B738DR_vnav_alt_mode = 0
				
				delta_alt_dial = mcp_alt_dial_app - simDR_altitude_pilot
				delta_vvi = B738DR_fmc_cruise_alt - simDR_altitude_pilot
				
				retard_timer = 3
				
				-- if B738DR_flight_phase < 2 or B738DR_missed_app_act > 0 or B738DR_flight_phase == 8 then
				if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
					-- takeoff, climb, go-arround
					vnav_alt_hld = 0
					vnav_vs = 0
					
					if ap_pitch_mode_old == 3 then	--or ap_pitch_mode_old == 6 then
						-- if B738DR_fmc_cruise_alt > 0 and simDR_ap_alt_hold_ft == B738DR_fmc_cruise_alt then
						if B738DR_fmc_cruise_alt > 0 and B738DR_preselected_alt == B738DR_fmc_cruise_alt then
							-- vnav cruise mode
							B738DR_flight_phase = 2
						else
							-- if B738DR_rest_wpt_alt ~= 0 and B738DR_rest_wpt_alt_t ~= 43 and simDR_ap_alt_hold_ft == B738DR_rest_wpt_alt then
							if B738DR_rest_wpt_alt ~= 0 and B738DR_rest_wpt_alt_t ~= 43 and B738DR_preselected_alt == B738DR_rest_wpt_alt then
								-- vnav path mode
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 9
								end
							elseif B738DR_rest_wpt_alt ~= 0 and B738DR_rest_wpt_alt_t ~= 43 and B738DR_AP_altitude > B738DR_rest_wpt_alt then
								-- VNAV ALT
								-- if B738DR_vnav_alt_disable == 0 then
									B738DR_vnav_alt_mode = 1
									vnav_alt_hld = 1
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 9
									end
								-- else
									-- B738DR_vnav_alt_mode = 0
									-- vnav_alt_hld = 0
									-- vnav_stop()
									-- simDR_ap_vvi_dial = 0
									-- B738DR_ap_vvi_dial = 0
									-- ap_pitch_mode = 3
									-- ap_pitch_mode_eng = 3
									-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										-- simCMD_autopilot_alt_hold:once()
									-- end
									-- if B738DR_autopilot_autothr_arm_pos == 1 then
										-- at_mode = 2
									-- end
								-- end
							-- elseif B738DR_fmc_cruise_alt > 0 and simDR_ap_alt_hold_ft > B738DR_fmc_cruise_alt then
							elseif B738DR_fmc_cruise_alt > 0 and B738DR_preselected_alt > B738DR_fmc_cruise_alt then
								-- VNAV ALT
								-- if B738DR_vnav_alt_disable == 0 then
									B738DR_vnav_alt_mode = 1
									vnav_alt_hld = 1
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 9
									end
								-- else
									-- B738DR_vnav_alt_mode = 0
									-- vnav_alt_hld = 0
									-- vnav_stop()
									-- simDR_ap_vvi_dial = 0
									-- B738DR_ap_vvi_dial = 0
									-- ap_pitch_mode = 3
									-- ap_pitch_mode_eng = 3
									-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										-- simCMD_autopilot_alt_hold:once()
									-- end
									-- if B738DR_autopilot_autothr_arm_pos == 1 then
										-- at_mode = 2
									-- end
								-- end
							else
								if B738DR_mcp_alt_dial > B738DR_AP_altitude then
									-- vnav spd climb
									vnav_clb_spd_mode()
									alt_x = at_mode
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
										if B738DR_autopilot_autothr_arm_pos == 0 then
											at_mode = alt_x
										end
									end
								else
									-- VNAV ALT
									B738DR_vnav_alt_mode = 1
									vnav_alt_hld = 1
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										B738DR_preselected_alt = simDR_ap_altitude_dial_ft
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 9
									end
								end
							end
						end
					elseif B738DR_rest_wpt_alt ~= 0 and B738DR_rest_wpt_alt_t ~= 43 and B738DR_AP_altitude > B738DR_rest_wpt_alt then
						-- VNAV ALT
						B738DR_vnav_alt_mode = 1
						vnav_alt_hld = 1
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							B738DR_preselected_alt = simDR_ap_altitude_dial_ft
							simCMD_autopilot_alt_hold:once()
						end
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 9
						end
					else
						if B738DR_mcp_alt_dial > B738DR_AP_altitude then
							-- vnav spd climb
							vnav_clb_spd_mode()
							alt_x = at_mode
							if simDR_autopilot_altitude_mode ~= 5 then
								simCMD_autopilot_lvl_chg:once()
								if B738DR_autopilot_autothr_arm_pos == 0 then
									at_mode = alt_x
								end
							end
						else
							-- VNAV ALT
							B738DR_vnav_alt_mode = 1
							vnav_alt_hld = 1
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								B738DR_preselected_alt = simDR_ap_altitude_dial_ft
								simCMD_autopilot_alt_hold:once()
							end
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 9
							end
						end
					end
					
				-- cruise
				elseif B738DR_flight_phase == 2 then
					if B738DR_nd_vert_path ~= 0  then
						if delta_alt_dial > 100 then
							-- vnav alt
							B738DR_vnav_alt_mode = 1
							vnav_alt_hld = 1
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								B738DR_preselected_alt = simDR_ap_altitude_dial_ft
								simCMD_autopilot_alt_hold:once()
							end
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 9
							end
						else
							-- descent
							vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_AP_vvi, 0, 90)
							B738DR_ap_vvi_dial = vvi_des
							if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
								simDR_ap_vvi_dial = B738DR_ap_vvi_dial
							elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
								simDR_ap_vvi_dial = B738DR_ap_vvi_dial
							end
							
							if simDR_autopilot_altitude_mode ~= 4 then
								simCMD_autopilot_vs_sel:once()
							end
							
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 7
							end
							
							retard_enabled = 1
							B738DR_fms_descent_now = 3
							B738DR_flight_phase = 5
							vnav_alt_hold_act = 0
							mcp_alt_hold = -1
						end
					else
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 9
						end
						
						local hold_cruise = false
						if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
							-- if B738DR_fmc_cruise_alt > 0 and simDR_ap_alt_hold_ft == B738DR_fmc_cruise_alt then
							if B738DR_fmc_cruise_alt > 0 and B738DR_preselected_alt == B738DR_fmc_cruise_alt then
								hold_cruise = true
							end
						end
						
						if hold_cruise == false then
						
							if delta_vvi > 100 then
								-- below 300 ft and more
								vnav_vs = 0
								if delta_alt_dial > 100 then
									-- cruise climb
									B738DR_flight_phase = 3
									vnav_alt_hld = 0
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 7
									end
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
										if B738DR_autopilot_autothr_arm_pos == 0 then
											at_mode = alt_x
										end
									end
								else
									-- vnav alt
									B738DR_vnav_alt_mode = 1
									vnav_alt_hld = 1
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										B738DR_preselected_alt = simDR_ap_altitude_dial_ft
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 9
									end
								end
							elseif delta_vvi < -100 then
								if delta_alt_dial > 100 then
									-- cruise descent
									B738DR_flight_phase = 4
									vnav_alt_hld = 0
									-- simDR_ap_vvi_dial = B738DR_AP_vvi
									-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
									
									-- vvi_des = vnav_vvi_limit(-1000, simDR_ap_vvi_dial, 0, 90)
									
									-- simDR_ap_vvi_dial = vvi_des
									-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
									
									vvi_des = vnav_vvi_limit(-1000, B738DR_AP_vvi, 0, 90)
									B738DR_ap_vvi_dial = vvi_des
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									
									if simDR_autopilot_altitude_mode ~= 4 then
										simCMD_autopilot_vs_sel:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 7
									end
								else
									-- vnav alt
									B738DR_vnav_alt_mode = 1
									vnav_alt_hld = 1
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										B738DR_preselected_alt = simDR_ap_altitude_dial_ft
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 9
									end
								end
							else
								-- in cruise altitude
								if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									B738DR_preselected_alt = simDR_ap_altitude_dial_ft
									simCMD_autopilot_alt_hold:once()
								end
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 9
								end
							end
						end
					end
				
				elseif B738DR_flight_phase == 3 then
					-- cruise climb
					if delta_vvi > 100 then
						-- cruise climb
						vnav_vs = 0
						if delta_alt_dial > 100 then
							-- cruise climb
							vnav_alt_hld = 0
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 7
								B738DR_autopilot_n1_status = 1
							end
							if simDR_autopilot_altitude_mode ~= 5 then
								simCMD_autopilot_lvl_chg:once()
								if B738DR_autopilot_autothr_arm_pos == 0 then
									at_mode = alt_x
								end
							end
						else
							-- vnav alt
							B738DR_vnav_alt_mode = 1
							vnav_alt_hld = 1
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								B738DR_preselected_alt = simDR_ap_altitude_dial_ft
								simCMD_autopilot_alt_hold:once()
							end
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 9
							end
						end
					else
						-- vnav alt
						B738DR_vnav_alt_mode = 1
						vnav_alt_hld = 1
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							B738DR_preselected_alt = simDR_ap_altitude_dial_ft
							simCMD_autopilot_alt_hold:once()
						end
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 9
						end
					end
				elseif B738DR_flight_phase == 4 then
					-- cruise descent
					if delta_vvi < -100 then
						-- cruise descent
						-- simDR_ap_vvi_dial = B738DR_AP_vvi	--simDR_vvi_fpm_pilot
						-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
						-- vvi_des = vnav_vvi_limit(-1000, simDR_ap_vvi_dial, 0, 90)
						-- simDR_ap_vvi_dial = vvi_des
						-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
						
						vvi_des = vnav_vvi_limit(-1000, B738DR_AP_vvi, 0, 90)
						B738DR_ap_vvi_dial = vvi_des
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
						
						if simDR_autopilot_altitude_mode ~= 4 then
							simCMD_autopilot_vs_sel:once()
						end
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 7
						end
					else
						-- vnav alt
						B738DR_vnav_alt_mode = 1
						vnav_alt_hld = 1
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							B738DR_preselected_alt = simDR_ap_altitude_dial_ft
							simCMD_autopilot_alt_hold:once()
						end
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 9
						end
					end
				else
					-- descent
						-- below cruise altitude
						if delta_alt_dial > 100 then
							-- vnav alt
							B738DR_vnav_alt_mode = 1
							vnav_alt_hld = 1
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								B738DR_preselected_alt = simDR_ap_altitude_dial_ft
								simCMD_autopilot_alt_hold:once()
							end
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 9
							end
							-- B738DR_test_test5 = 1
						elseif delta_alt_dial > -100 then
							if B738DR_rest_wpt_alt == 0 or B738DR_rest_wpt_alt_t == 45 then		-- ignored Below alt restrict
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then		-- 0.1
									vnav_alt = B738DR_td_alt2
								else
									vnav_alt = 0
								end
							else
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then
									if simDR_altitude_pilot < B738DR_td_alt2 then
										vnav_alt = B738DR_rest_wpt_alt
									else
										vnav_alt = math.max(B738DR_rest_wpt_alt, B738DR_td_alt2)
									end
								else
									vnav_alt = B738DR_rest_wpt_alt
								end
								rest_wpt_alt_old = B738DR_rest_wpt_alt
							end
							
							if vnav_alt == 0 then
								-- vnav alt
								B738DR_vnav_alt_mode = 1
								vnav_alt_hld = 1
								if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									B738DR_preselected_alt = simDR_ap_altitude_dial_ft
									simCMD_autopilot_alt_hold:once()
								end
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 9
								end
								-- B738DR_test_test5 = 2
							else
								if simDR_altitude_pilot < vnav_alt then
									-- vnav pth hold
									vnav_alt_hold_act = 1
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 2
									end
									B738DR_fms_descent_now = 3
									-- B738DR_test_test5 = 3
								else
									-- vnav alt
									B738DR_vnav_alt_mode = 1
									vnav_alt_hld = 1
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										B738DR_preselected_alt = simDR_ap_altitude_dial_ft
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 9
									end
									-- B738DR_test_test5 = 4
								end
							end
						else
							-- descent
							vnav_alt_hold_act = 0
							
							if B738DR_rest_wpt_alt == 0 or B738DR_rest_wpt_alt_t == 45 then		-- ignored Below alt restrict
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then
									vnav_alt = B738DR_td_alt2
								else
									vnav_alt = 0
								end
							else
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then
									if simDR_altitude_pilot < B738DR_td_alt2 then
										vnav_alt = B738DR_rest_wpt_alt
									else
										vnav_alt = math.max(B738DR_rest_wpt_alt, B738DR_td_alt2)
									end
								else
									vnav_alt = B738DR_rest_wpt_alt
								end
								rest_wpt_alt_old = B738DR_rest_wpt_alt
							end
							
							if vnav_alt == 0 then
								-- descent
								B738DR_fms_descent_now = 3
								
								vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_AP_vvi, 0, 90)
								B738DR_ap_vvi_dial = vvi_des
								if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
									simDR_ap_vvi_dial = B738DR_ap_vvi_dial
								elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
									simDR_ap_vvi_dial = B738DR_ap_vvi_dial
								end
								
								if simDR_autopilot_altitude_mode ~= 4 then
									simCMD_autopilot_vs_sel:once()
								end
								
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 7
								end
								-- B738DR_test_test5 = 5
							else
								if simDR_altitude_pilot < vnav_alt then
									-- vnav pth hold
									vnav_alt_hold_act = 1
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										B738DR_preselected_alt = simDR_ap_altitude_dial_ft
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 2
									end
									B738DR_fms_descent_now = 3
									-- B738DR_test_test5 = 6
								else
									if B738DR_vnav_alt_err < -200 then
										--above profile
										B738DR_fms_descent_now = 3
										
										vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_AP_vvi, 0, 90)
										B738DR_ap_vvi_dial = vvi_des
										
										if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										end
									else
										-- activate soon descent
										B738DR_fms_descent_now = 2
										B738DR_ap_vvi_dial = -1000
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									
									if simDR_autopilot_altitude_mode ~= 4 then
										simCMD_autopilot_vs_sel:once()
									end
									
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 7
									end
									-- B738DR_test_test5 = 7
								end
							end
							
							
						end
				end
				
				vnav_init = 1
				
			end
			
			-- Restrict 250 kts / 10000 ft
			spd_250_10000 = 340
			if B738DR_flight_phase == 2 then
				if B738DR_fmc_descent_r_speed1 ~= 0  then
					if B738DR_dist_ed ~= 0 and B738DR_dist_ed < B738DR_FL100_desc_dist then
						spd_250_10000 = B738DR_fmc_descent_r_speed1
						if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
							-- if simDR_ap_alt_hold_ft <= B738DR_fmc_descent_r_alt1 then
							if B738DR_preselected_alt <= B738DR_fmc_descent_r_alt1 then
								spd_250_10000 = B738DR_fmc_descent_r_speed1
							end
						else
							if simDR_altitude_pilot <= B738DR_fmc_descent_r_alt1 then
								spd_250_10000 = B738DR_fmc_descent_r_speed1
							end
						end
					elseif B738DR_fmc_climb_r_speed1 ~= 0 then
						if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
							-- if simDR_ap_alt_hold_ft <= B738DR_fmc_climb_r_alt1 then
							if B738DR_preselected_alt <= B738DR_fmc_climb_r_alt1 then
								spd_250_10000 = B738DR_fmc_climb_r_speed1
							end
						else
							if simDR_altitude_pilot <= B738DR_fmc_climb_r_alt1 then
								spd_250_10000 = B738DR_fmc_climb_r_speed1
							end
						end
					else
						if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
							-- if simDR_ap_alt_hold_ft <= B738DR_fmc_descent_r_alt1 then
							if B738DR_preselected_alt <= B738DR_fmc_descent_r_alt1 then
								spd_250_10000 = B738DR_fmc_descent_r_speed1
							end
						else
							if simDR_altitude_pilot <= B738DR_fmc_descent_r_alt1 then
								spd_250_10000 = B738DR_fmc_descent_r_speed1
							end
						end
					end
				elseif B738DR_fmc_climb_r_speed1 ~= 0 then
					if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						-- if simDR_ap_alt_hold_ft <= B738DR_fmc_climb_r_alt1 then
						if B738DR_preselected_alt <= B738DR_fmc_climb_r_alt1 then
							spd_250_10000 = B738DR_fmc_climb_r_speed1
						end
					else
						if simDR_altitude_pilot <= B738DR_fmc_climb_r_alt1 then
							spd_250_10000 = B738DR_fmc_climb_r_speed1
						end
					end
				end
			elseif B738DR_flight_phase <= 3 or B738DR_flight_phase > 7 then
				if B738DR_fmc_climb_r_speed1 ~= 0 then
					
					if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						-- if simDR_ap_alt_hold_ft <= B738DR_fmc_climb_r_alt1 then
						if B738DR_preselected_alt <= B738DR_fmc_climb_r_alt1 then
							spd_250_10000 = B738DR_fmc_climb_r_speed1
						end
					else
						if simDR_altitude_pilot <= B738DR_fmc_climb_r_alt1 then
							spd_250_10000 = B738DR_fmc_climb_r_speed1
						end
					end
				end
			else
				if B738DR_fmc_descent_r_speed1 ~= 0 then
					if B738DR_fmc_dist_to_dest < B738DR_FL100_dist_dest2 then
						spd_250_10000 = B738DR_fmc_descent_r_speed1
					elseif B738DR_fmc_dist_to_dest < B738DR_FL100_dist_dest then
						spd_250_10000 = B738DR_fmc_descent_r_speed1 + ((B738DR_fmc_dist_to_dest - B738DR_FL100_dist_dest2) / 0.128)		--DES_SPEED_COEF
						spd_250_10000 = math.min(spd_250_10000, B738DR_fmc_descent_speed)
						spd_250_10000 = math.floor(spd_250_10000)
					end
					if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						-- if simDR_ap_alt_hold_ft <= B738DR_fmc_descent_r_alt1 then
						if B738DR_preselected_alt <= B738DR_fmc_descent_r_alt1 then
							spd_250_10000 = B738DR_fmc_descent_r_speed1
						end
					else
						if simDR_altitude_pilot <= B738DR_fmc_descent_r_alt1 then
							spd_250_10000 = B738DR_fmc_descent_r_speed1
						end
					end
				end
				
			end
			
			-- PHASE CLIMB
			-- if B738DR_flight_phase < 2 or B738DR_missed_app_act > 0 or B738DR_flight_phase == 8 then
			if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
				
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					
					local ap_capture_alt = B738DR_AP_altitude + (B738DR_AP_vvi / 60 * 9)
					local vnav_alt_acq_act = false
					
					if B738DR_AP_altitude < simDR_ap_altitude_dial_ft then
						if ap_capture_alt >= simDR_ap_altitude_dial_ft then
							vnav_alt_acq_act = true
						end
					end
					
					if vnav_alt_acq_act == true then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
				end
				
				-- B738DR_test_test = B738DR_preselected_alt
				
				retard_enabled = 1
				-- crossover altitude
				const_spd = 1
				if simDR_IAS > 45 then
					const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
				end
				mach_to_ias = (B738DR_fmc_climb_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
				
				if B738DR_ap_spd_interv_status == 0 then
					if simDR_airspeed_is_mach == 0 then
						if mach_to_ias <= B738DR_fmc_climb_speed and mach_to_ias <= simDR_IAS then
							simCMD_autopilot_co:once()
						end
					else
						if mach_to_ias > B738DR_fmc_climb_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
							simCMD_autopilot_co:once()
						end
					end
				else
					-- climb
					if simDR_altitude_pilot > 26000 and spd_interv_co_used == 0 then
						if simDR_airspeed_is_mach == 0 then
							simCMD_autopilot_co:once()
						end
						spd_interv_co_used = 1
					elseif simDR_altitude_pilot < 26000 then
						spd_interv_co_used = 0
					end
				end
				
				-- flaps limit speeds
				if flaps == 0 then
					B738DR_flaps_speed = 340
				elseif flaps <= 0.25 then		-- flaps 1,2
					B738DR_flaps_speed = 230
				elseif flaps <= 0.375 then		-- flaps 5
					B738DR_flaps_speed = 230
				elseif flaps <= 0.5 then		-- flaps 10
					B738DR_flaps_speed = 205
				elseif flaps <= 0.625 then		-- flaps 15
					B738DR_flaps_speed = 195
				elseif flaps <= 0.75 then		-- flaps 25
					B738DR_flaps_speed = 185
				else
					B738DR_flaps_speed = 160
				end
				if B738DR_flaps_speed == 0 then
					if flaps == 0 then
						B738DR_flaps_speed = 340
					else
						B738DR_flaps_speed = 230
					end
				end
				-- V2+20 limit speeds
				if v2_20_speed == 0 then
					v2_20_speed = 180
				end
				
				if takeoff_eng_out == 1 and B738DR_eng_out_disable == 0 then
					-- if simDR_radio_height_pilot_ft < B738DR_eo_accel_alt then
					if simDR_altitude_pilot < B738DR_aerodrom_altitude + B738DR_eo_accel_alt then
						v2 = v2_20_speed - 15
						if B738DR_AP_airspeed >= v2 and B738DR_AP_airspeed <= v2_20_speed then
							v2_20_speed = B738DR_AP_airspeed
						elseif B738DR_AP_airspeed < v2 then
							v2_20_speed = v2
						end
					else
						if B738DR_fms_vref_40 ~= 0 then
							v2_20_speed = B738DR_fms_vref_40 + 70
						else
							v2_20_speed = 205
						end
					end
				elseif B738DR_eng_out == 1 and B738DR_eng_out_disable == 0 then
					if B738DR_fms_vref_40 ~= 0 then
						v2_20_speed = B738DR_fms_vref_40 + 70
					else
						v2_20_speed = 205
					end
				else
					-- if simDR_radio_height_pilot_ft > B738DR_accel_alt then
					if simDR_altitude_pilot > B738DR_aerodrom_altitude + B738DR_accel_alt then
						v2_20_speed = 340
						to_ga_mcp_changed = 0
					else
						if to_ga_mcp_changed == 1 then
							if simDR_airspeed_is_mach == 0 then
								mcp_speed_dial_mem = B738DR_mcp_speed_dial
							else
								mcp_speed_dial_mem = B738DR_mcp_speed_dial_kts
							end
							v2_20_speed = mcp_speed_dial_mem
							to_ga_mcp_changed = 2
						elseif to_ga_mcp_changed == 2 then
							v2_20_speed = mcp_speed_dial_mem
						else
							v2_20_speed = mcp_speed_dial_mem + 20
						end
					end
				end
				B738DR_fmc_vnav_v2_20 = v2_20_speed
				
				if v2_20_speed < 340 then
					if B738DR_flaps_speed < v2_20_speed then
						B738DR_flaps_speed = v2_20_speed
					end
				end
				-- limited VNAV speed
				-- B738DR_vnav_speed = B738DR_rest_wpt_spd2
				
				-- if B738DR_vnav_speed == 0 then
					-- B738DR_vnav_speed = 340
				-- end
				
				if B738DR_rest_wpt_spd2 == 0 then
					B738DR_vnav_speed = 340
				else
					-- if simDR_airspeed_is_mach == 0 then
						B738DR_vnav_speed = rest_wpt_spd_kts(B738DR_rest_wpt_spd2)
					-- else
						-- B738DR_vnav_speed = rest_wpt_spd_mach(B738DR_rest_wpt_spd2)
					-- end
				end
				
				local fmc_climb_speed_l_act = 0
				if B738DR_autopilot_autothr_arm_pos == 0 then
					B738DR_fmc_climb_speed_l = math.min (B738DR_flaps_speed, v2_20_speed, B738DR_vnav_speed, spd_250_10000)
					if B738DR_fmc_climb_speed_l == v2_20_speed then
						fmc_climb_speed_l_act = 1
					elseif B738DR_fmc_climb_speed_l == B738DR_flaps_speed then
						fmc_climb_speed_l_act = 2
					elseif B738DR_fmc_climb_speed_l == B738DR_vnav_speed then
						fmc_climb_speed_l_act = 3
					elseif B738DR_fmc_climb_speed_l == spd_250_10000 then
						fmc_climb_speed_l_act = 4
					end
				else
					if simDR_airspeed_is_mach == 0 then
						B738DR_fmc_climb_speed_l = math.min (B738DR_flaps_speed, v2_20_speed, B738DR_vnav_speed, spd_250_10000, B738DR_afs_spd_limit_max)
						if B738DR_fmc_climb_speed_l == v2_20_speed then
							fmc_climb_speed_l_act = 1
						elseif B738DR_fmc_climb_speed_l == B738DR_flaps_speed then
							fmc_climb_speed_l_act = 2
						elseif B738DR_fmc_climb_speed_l == B738DR_vnav_speed then
							fmc_climb_speed_l_act = 3
						elseif B738DR_fmc_climb_speed_l == spd_250_10000 then
							fmc_climb_speed_l_act = 4
						elseif B738DR_fmc_climb_speed_l == B738DR_afs_spd_limit_max then
							fmc_climb_speed_l_act = 1
						end
					else
						B738DR_fmc_climb_speed_l = math.min (B738DR_flaps_speed, v2_20_speed, B738DR_vnav_speed, spd_250_10000)
						if B738DR_fmc_climb_speed_l == v2_20_speed then
							fmc_climb_speed_l_act = 1
						elseif B738DR_fmc_climb_speed_l == B738DR_flaps_speed then
							fmc_climb_speed_l_act = 2
						elseif B738DR_fmc_climb_speed_l == B738DR_vnav_speed then
							fmc_climb_speed_l_act = 3
						elseif B738DR_fmc_climb_speed_l == spd_250_10000 then
							fmc_climb_speed_l_act = 4
						end
					end
				end
				
				if simDR_airspeed_is_mach == 0 then
					if B738DR_fmc_climb_speed_l < B738DR_fmc_climb_speed then
						B738DR_vnav_speed_trg = B738DR_fmc_climb_speed_l
						B738DR_fmc_speed_l_act = fmc_climb_speed_l_act
						if flaps == 0 and B738DR_pfd_flaps_up < 340 then
							if B738DR_vnav_speed_trg < B738DR_pfd_flaps_up then
								B738DR_vnav_speed_trg = B738DR_pfd_flaps_up
							end
						end
					else
						B738DR_fmc_climb_speed_l = 340
						B738DR_vnav_speed_trg = B738DR_fmc_climb_speed
						B738DR_fmc_speed_l_act = 0
					end
				else
					if B738DR_fmc_climb_speed_l == 340 then
						B738DR_fmc_climb_speed_l = 0.82
					else
						B738DR_fmc_climb_speed_l = rest_wpt_spd_mach(B738DR_fmc_climb_speed_l)
					end
					
					if B738DR_fmc_climb_speed_l < B738DR_fmc_climb_speed_mach then
						B738DR_vnav_speed_trg = B738DR_fmc_climb_speed_l
						B738DR_fmc_speed_l_act = fmc_climb_speed_l_act
					else
						B738DR_fmc_climb_speed_l = 0.82		--340
						B738DR_vnav_speed_trg = B738DR_fmc_climb_speed_mach
						B738DR_fmc_speed_l_act = 0
					end
				end
				
				if B738DR_rest_wpt_alt == 0 or B738DR_rest_wpt_alt_t == 43 then
					vnav_alt = math.min(B738DR_fmc_cruise_alt, mcp_alt_dial_app)
				else
					vnav_alt = math.min(B738DR_rest_wpt_alt, B738DR_fmc_cruise_alt, mcp_alt_dial_app)
				end
				-- VNAV ALT
				if B738DR_vnav_alt_mode == 0 then
				
					if vnav_alt_hld == 1 then
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 9		-- SPEED mode on with N1 limit
							-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
							vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
						end
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							B738DR_preselected_alt = simDR_ap_altitude_dial_ft
							simCMD_autopilot_alt_hold:once()
						end
						vnav_vs = 0
					end
					if vnav_vs == 1 and simDR_autopilot_altitude_mode == 5 then
						-- simDR_ap_vvi_dial = 850
						B738DR_ap_vvi_dial = 850
						vvi_trg = 850
						fmc_vvi_cur = simDR_vvi_fpm_pilot
						simCMD_autopilot_vs_sel:once()
					end
					
					if simDR_autopilot_altitude_mode == 5 then		-- LVL CHG
						delta_alt_dial = vnav_alt - simDR_altitude_pilot
						vnav_clb_spd_mode()
						-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_SPD
						vnav_pfd_alt_mode = PFD_ALT_VNAV_SPD
						if delta_alt_dial > 700 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
								B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
							end
							simDR_ap_altitude_dial_ft = mcp_alt_dial_app
						else
							simDR_ap_altitude_dial_ft = vnav_alt
						end
					elseif simDR_autopilot_altitude_mode == 4 then
						vnav_clb_spd_mode()
						-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_SPD
						vnav_pfd_alt_mode = PFD_ALT_VNAV_SPD
						delta_alt_dial = vnav_alt - simDR_altitude_pilot
						if delta_alt_dial > 700 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
								B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
							end
							simDR_ap_altitude_dial_ft = mcp_alt_dial_app
						else
							simDR_ap_altitude_dial_ft = vnav_alt
						end
					elseif simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 9		-- SPEED mode on with N1 limit
							-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
							vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
						end
						-- if simDR_ap_alt_hold_ft == B738DR_fmc_cruise_alt and vnav_alt_hld == 0 then
						if B738DR_preselected_alt == B738DR_fmc_cruise_alt and vnav_alt_hld == 0 then
							B738DR_flight_phase = 2
							B738DR_vnav_alt_mode = 0
						-- elseif simDR_ap_alt_hold_ft == mcp_alt_dial_app and B738DR_mcp_alt_dial < B738DR_rest_wpt_alt and vnav_alt_hld == 0 then
						elseif B738DR_preselected_alt == mcp_alt_dial_app and B738DR_mcp_alt_dial < B738DR_rest_wpt_alt and vnav_alt_hld == 0 then
							B738DR_vnav_alt_mode = 1
							simDR_ap_altitude_dial_ft = mcp_alt_dial_app
							vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
							vnav_vs = 0
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 9		-- SPEED mode on with N1 limit
								-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
								vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
							end
						else
							vnav_alt_hld = 1
							vnav_vs = 0
							simDR_ap_altitude_dial_ft = mcp_alt_dial_app
							-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_PTH
							vnav_pfd_alt_mode = PFD_ALT_VNAV_PTH
							if B738DR_rest_wpt_alt == 0 or B738DR_rest_wpt_alt_t == 43 then
								vnav_alt2 = B738DR_fmc_cruise_alt
							else
								vnav_alt2 = math.min(B738DR_rest_wpt_alt, B738DR_fmc_cruise_alt)
							end
							delta_alt_dial = vnav_alt2 - B738DR_preselected_alt		--simDR_ap_alt_hold_ft
							delta_alt_dial2 = mcp_alt_dial_app - B738DR_preselected_alt		--simDR_ap_alt_hold_ft
								
							if delta_alt_dial > 0 then
								if delta_alt_dial2 > 0 then
									vnav_clb_spd_mode()
									simCMD_autopilot_lvl_chg:once()
									
									vnav_alt_hld = 0
									B738DR_vnav_alt_mode = 0
								else
									B738DR_vnav_alt_mode = 1
									simDR_ap_altitude_dial_ft = mcp_alt_dial_app
									-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
									vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
									vnav_alt_hld = 0
									vnav_vs = 0
								end
							end
							if B738DR_vnav_alt_disable ~= 0 and B738DR_vnav_alt_mode ~= 0 then
							-- if B738DR_vnav_alt_disable ~= 0 then
								-- B738DR_pfd_alt_mode = PFD_ALT_ALT_HOLD
								vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
							-- elseif B738DR_vnav_alt_disable == 0 and B738DR_vnav_alt_mode ~= 0 then
								-- B738DR_vnav_alt_mode = 0
								-- vnav_alt_hld = 0
								-- vnav_stop()
								-- simDR_ap_vvi_dial = 0
								-- B738DR_ap_vvi_dial = 0
								-- ap_pitch_mode = 3
								-- ap_pitch_mode_eng = 3
								-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									-- simCMD_autopilot_alt_hold:once()
								-- end
								-- if B738DR_autopilot_autothr_arm_pos == 1 then
									-- at_mode = 2
								-- end
							end
							if vnav_alt_hld ~= 0 then
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 9		-- SPEED mode on with N1 limit
									-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
									vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
								end
							end
						end
					end
				else
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 9		-- SPEED mode on with N1 limit
						-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
						vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
					end
					if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
					simDR_ap_altitude_dial_ft = mcp_alt_dial_app
					if B738DR_vnav_alt_disable ~= 0 then
						-- B738DR_pfd_alt_mode = PFD_ALT_ALT_HOLD
						vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
					else
						-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
						vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
					end
					-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
					vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
					vnav_alt_hld = 0
					vnav_vs = 0
				end
				
			-- PHASE CRUISE
			elseif B738DR_flight_phase == 2 then
				
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					
					local ap_capture_alt = B738DR_AP_altitude + (B738DR_AP_vvi / 60 * 9)
					local vnav_alt_acq_act = false
					
					if B738DR_AP_altitude < simDR_ap_altitude_dial_ft then
						if ap_capture_alt >= simDR_ap_altitude_dial_ft then
							vnav_alt_acq_act = true
						end
					else
						if ap_capture_alt <= simDR_ap_altitude_dial_ft then
							vnav_alt_acq_act = true
						end
					end
					
					if vnav_alt_acq_act == true then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
				end
				
				retard_enabled = 1
				-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
				vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
				if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
					-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_PTH
					vnav_pfd_alt_mode = PFD_ALT_VNAV_PTH
					if at_mode ~= 9 and B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 9		-- SPEED mode on with N1 limit
					end
				end
				
				if flight_phase_old ~= B738DR_flight_phase then
					B738DR_vnav_alt_mode = 0
				end
				
				simDR_ap_altitude_dial_ft = math.min(mcp_alt_dial_app, B738DR_fmc_cruise_alt)
				
				-- if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and simDR_ap_alt_hold_ft ~= B738DR_fmc_cruise_alt and B738DR_fmc_cruise_alt ~= 0 then
				if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and B738DR_preselected_alt ~= B738DR_fmc_cruise_alt and B738DR_fmc_cruise_alt ~= 0 then
					if simDR_altitude_pilot < simDR_ap_altitude_dial_ft then
						-- simDR_ap_vvi_dial = 1000
						B738DR_ap_vvi_dial = 1000
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
						simCMD_autopilot_vs_sel:once()
					elseif simDR_altitude_pilot > B738DR_fmc_cruise_alt and simDR_altitude_pilot < (B738DR_fmc_cruise_alt + 500) then
						simDR_ap_altitude_dial_ft = B738DR_fmc_cruise_alt
						-- simDR_ap_vvi_dial = -1000
						B738DR_ap_vvi_dial = -1000
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
						simCMD_autopilot_vs_sel:once()
					end
				elseif simDR_autopilot_altitude_mode == 4 then
					if simDR_altitude_pilot < simDR_ap_altitude_dial_ft then
						-- simDR_ap_vvi_dial = 1000
						B738DR_ap_vvi_dial = 1000
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
					elseif simDR_altitude_pilot > B738DR_fmc_cruise_alt then
						-- simDR_ap_vvi_dial = -1000
						B738DR_ap_vvi_dial = -1000
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
						if simDR_altitude_pilot < (B738DR_fmc_cruise_alt + 500) then
							simDR_ap_altitude_dial_ft = B738DR_fmc_cruise_alt
						end
					end
				elseif simDR_autopilot_altitude_mode == 5 then
					if simDR_altitude_pilot < simDR_ap_altitude_dial_ft then
						-- simDR_ap_vvi_dial = 1000
						B738DR_ap_vvi_dial = 1000
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
					elseif simDR_altitude_pilot > B738DR_fmc_cruise_alt then
						-- simDR_ap_vvi_dial = -1000
						B738DR_ap_vvi_dial = -1000
						if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
							simDR_ap_vvi_dial = B738DR_ap_vvi_dial
						end
						if simDR_altitude_pilot < (B738DR_fmc_cruise_alt + 500) then
							simDR_ap_altitude_dial_ft = B738DR_fmc_cruise_alt
						end
					end
					simCMD_autopilot_vs_sel:once()
				else
					if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
				end
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 9		-- SPEED mode on with N1 limit
					-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
					vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
				end
				vnav_alt_hld = 0
				vnav_vs = 0
				
				-- crossover altitude
				if B738DR_rest_wpt_spd > 0 and B738DR_rest_wpt_spd < 1 and B738DR_ap_spd_interv_status == 0 then
					if simDR_airspeed_is_mach == 0 then
						simCMD_autopilot_co:once()
					end
				elseif B738DR_rest_wpt_spd >= 100 and B738DR_ap_spd_interv_status == 0 then
					if simDR_airspeed_is_mach ~= 0 then
						simCMD_autopilot_co:once()
					end
				else
					if B738DR_fmc_cruise_speed ~= 0 and B738DR_fmc_cruise_speed_mach ~= 0 then
						const_spd = 1
						if simDR_IAS > 45 then
							const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
						end
						mach_to_ias = (B738DR_fmc_cruise_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
						
						if B738DR_ap_spd_interv_status == 0 then
							if simDR_airspeed_is_mach == 0 then
								if mach_to_ias <= B738DR_fmc_cruise_speed then	--and mach_to_ias <= simDR_IAS then
									simCMD_autopilot_co:once()
								end
							else
								if mach_to_ias > B738DR_fmc_cruise_speed + 0.5 then	--and mach_to_ias > simDR_IAS + 0.5 then
									simCMD_autopilot_co:once()
								end
							end
						end
					elseif B738DR_fmc_cruise_speed ~= 0 then
						if B738DR_ap_spd_interv_status == 0 then
							if simDR_airspeed_is_mach ~= 0 then
								simCMD_autopilot_co:once()
							end
						end
					elseif B738DR_fmc_cruise_speed_mach ~= 0 then
						if B738DR_ap_spd_interv_status == 0 then
							if simDR_airspeed_is_mach == 0 then
								simCMD_autopilot_co:once()
							end
						end
					end
				end
				
				-- cruise speed
				if simDR_airspeed_is_mach == 0 then
					-- flaps limit speeds
					if flaps == 0 then
						B738DR_flaps_speed = 340
					elseif flaps <= 0.125 then		-- flaps 1,2
						B738DR_flaps_speed = 230
					elseif flaps <= 0.25 then		-- flaps 1,2
						B738DR_flaps_speed = 230
					elseif flaps <= 0.375 then		-- flaps 5
						B738DR_flaps_speed = 230
					elseif flaps <= 0.5 then		-- flaps 10
						B738DR_flaps_speed = 205
					elseif flaps <= 0.625 then		-- flaps 15
						B738DR_flaps_speed = 195
					elseif flaps <= 0.75 then		-- flaps 25
						B738DR_flaps_speed = 185
					else
						B738DR_flaps_speed = 160
					end
					if B738DR_flaps_speed == 0 then
						if flaps == 0 then
							B738DR_flaps_speed = 340
						else
							B738DR_flaps_speed = 230
						end
					end
					
					if B738DR_rest_wpt_spd > 0 then
						
						rest_wpt_spd = rest_wpt_spd_kts(B738DR_rest_wpt_spd)
						
						if B738DR_rest_wpt_spd_type == 0 then
							if B738DR_mcp_speed_dial < 100 then
								B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_cruise_speed, B738DR_flaps_speed)
							else
								if B738DR_fmc_dist < B738DR_decel_dist then
									B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_cruise_speed, B738DR_flaps_speed)
								else
									B738DR_vnav_speed_trg = math.min(spd_250_10000, B738DR_fmc_cruise_speed, B738DR_flaps_speed)
								end
							end
						else
							B738DR_vnav_speed_trg = rest_wpt_spd
						end
						if flaps == 0 then
							if B738DR_pfd_flaps_up == 340 then
								if B738DR_vnav_speed_trg < 210 then
									B738DR_vnav_speed_trg = 210
								end
							else
								if B738DR_vnav_speed_trg < B738DR_pfd_flaps_up then
									B738DR_vnav_speed_trg = B738DR_pfd_flaps_up
								end
							end
						end
					else
						B738DR_vnav_speed_trg = math.min(spd_250_10000, B738DR_fmc_cruise_speed, B738DR_flaps_speed)
					end
				else
					
					if B738DR_rest_wpt_spd > 0 then
						
						rest_wpt_spd = rest_wpt_spd_mach(B738DR_rest_wpt_spd)
						
						if B738DR_rest_wpt_spd_type == 0 then
							B738DR_vnav_speed_trg = math.min(B738DR_fmc_cruise_speed_mach, rest_wpt_spd)
						else
							B738DR_vnav_speed_trg = rest_wpt_spd
						end
					else
						B738DR_vnav_speed_trg = B738DR_fmc_cruise_speed_mach
					end
				end
				
				--------
				-- automatic descent
				vnav_desc_idle = 0
				if B738DR_missed_app_act == 0 then
					if B738DR_vnav_td_dist <= 0 and B738DR_td_idx ~= 0 then
						B738DR_vnav_alt_mode = 1
						-- auto_descent_fail = 1
						vnav_alt_hold_act = 0
						mcp_alt_hold = -1
						if B738DR_vnav_alt_disable == 0 then
							vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
						elseif B738DR_vnav_alt_disable ~= 0 then
							if simDR_ap_altitude_dial_ft < B738DR_fmc_cruise_alt then
								B738DR_vnav_alt_mode = 0
								vnav_stop()
								simDR_ap_vvi_dial = 0
								B738DR_ap_vvi_dial = 0
								ap_pitch_mode = 3
								ap_pitch_mode_eng = 3
								
								-- retard_enabled = 1
								-- B738DR_fms_descent_now = 3
								-- B738DR_flight_phase = 5
								-- vnav_alt_hold_act = 0
								-- mcp_alt_hold = -1
								
								if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									B738DR_preselected_alt = simDR_ap_altitude_dial_ft
									simCMD_autopilot_alt_hold:once()
								end
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 2
								end
							else
								vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
							end
						end
					elseif B738DR_vnav_alt_disable ~= 0 and B738DR_vnav_alt_mode == 1 then
						if simDR_ap_altitude_dial_ft < B738DR_fmc_cruise_alt then
							B738DR_vnav_alt_mode = 0
							vnav_stop()
							simDR_ap_vvi_dial = 0
							B738DR_ap_vvi_dial = 0
							ap_pitch_mode = 3
							ap_pitch_mode_eng = 3
							
							-- retard_enabled = 1
							-- B738DR_fms_descent_now = 3
							-- B738DR_flight_phase = 5
							-- vnav_alt_hold_act = 0
							-- mcp_alt_hold = -1
							
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								B738DR_preselected_alt = simDR_ap_altitude_dial_ft
								simCMD_autopilot_alt_hold:once()
							end
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2
							end
						else
							vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
						end
					-- -- elseif B738DR_vnav_td_dist > 0 and B738DR_vnav_td_dist < 2 and B738DR_td_idx ~= 0 then
					elseif B738DR_vnav_td_dist > 0 and B738DR_vnav_td_dist < 6 and B738DR_td_idx ~= 0 then
						if simDR_ap_altitude_dial_ft < B738DR_fmc_cruise_alt then
							
							if B738DR_fmc_cruise_speed ~= fmc_cruise_speed_old then
								fmc_cruise_speed_old = B738DR_fmc_cruise_speed
								decel_dist_calc = false
								decel_dist_crz = 0
							end
							
							if B738DR_fmc_cruise_speed_mach ~= fmc_cruise_speed_mach_old then
								fmc_cruise_speed_mach_old = B738DR_fmc_cruise_speed_mach
								decel_dist_calc = false
								decel_dist_crz = 0
							end
							
							if B738DR_fmc_descent_speed ~= fmc_descent_speed_old then
								fmc_descent_speed_old = B738DR_fmc_descent_speed
								decel_dist_calc = false
								decel_dist_crz = 0
							end
							
							if B738DR_fmc_descent_speed_mach ~= fmc_descent_speed_mach_old then
								fmc_descent_speed_mach_old = B738DR_fmc_descent_speed_mach
								decel_dist_calc = false
								decel_dist_crz = 0
							end
							
							if simDR_airspeed_is_mach == 0 then
								if B738DR_fmc_cruise_speed ~= 0 then
									if decel_dist_calc == false then
										decel_dist_crz = math.abs(B738DR_fmc_cruise_speed - B738DR_fmc_descent_speed) * 0.155
										decel_dist_calc = true
									end
									if decel_dist_crz > B738DR_vnav_td_dist then
										if B738DR_rest_wpt_spd > 0 then
											rest_wpt_spd = rest_wpt_spd_kts(B738DR_rest_wpt_spd)
											if B738DR_rest_wpt_spd_type == 0 then
												if B738DR_mcp_speed_dial < 100 then
													B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_descent_speed, B738DR_flaps_speed)
												else
													if B738DR_fmc_dist < B738DR_decel_dist then
														B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_descent_speed, B738DR_flaps_speed)
													else
														B738DR_vnav_speed_trg = math.min(spd_250_10000, B738DR_fmc_descent_speed, B738DR_flaps_speed)
													end
												end
											else
												B738DR_vnav_speed_trg = rest_wpt_spd
											end
											if flaps == 0 then
												if B738DR_pfd_flaps_up == 340 then
													if B738DR_vnav_speed_trg < 210 then
														B738DR_vnav_speed_trg = 210
													end
												else
													if B738DR_vnav_speed_trg < B738DR_pfd_flaps_up then
														B738DR_vnav_speed_trg = B738DR_pfd_flaps_up
													end
												end
											end
										else
											B738DR_vnav_speed_trg = math.min(spd_250_10000, B738DR_fmc_descent_speed, B738DR_flaps_speed)
										end
									end
								end
							else
								if B738DR_fmc_cruise_speed_mach ~= 0 then
									if decel_dist_calc == false then
										decel_dist_crz = math.abs(rest_wpt_spd_kts(B738DR_fmc_cruise_speed_mach) - rest_wpt_spd_kts(B738DR_fmc_descent_speed_mach)) * 0.155
										decel_dist_calc = true
									end
									if decel_dist_crz > B738DR_vnav_td_dist then
										if B738DR_rest_wpt_spd > 0 then
											rest_wpt_spd = rest_wpt_spd_mach(B738DR_rest_wpt_spd)
											if B738DR_rest_wpt_spd_type == 0 then
												B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed_mach, rest_wpt_spd)
											else
												B738DR_vnav_speed_trg = rest_wpt_spd
											end
										else
											B738DR_vnav_speed_trg = B738DR_fmc_descent_speed_mach
										end
									end
								end
							end
							
							if B738DR_vnav_alt_mode == 0 and B738DR_vnav_td_dist < 1 then
								vnav_desc_idle_time = 0
								-- vnav descent
								if B738DR_ed_found <= B738DR_legs_num then
									if B738DR_descent_mode == 0 or B738DR_descent_mode == 2 then
										if simDR_autopilot_altitude_mode ~= 4 then
										
											vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_AP_vvi, 0, 90)
											B738DR_ap_vvi_dial = vvi_des
											if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											end
										
											simCMD_autopilot_vs_sel:once()
										end
										at_mode = 2
										vnav_desc_spd = 0	-- change to VNAV PTH
										vnav_desc_idle = 1
										vnav_desc_idle_time = ((B738DR_vnav_td_dist * 1852) / simDR_ground_spd) - 2.5 	-- 1.9 / 1.4 sec before
										vnav_desc_idle_time = math.max(vnav_desc_idle_time, 0.5)
									else
										if simDR_autopilot_altitude_mode ~= 5 then
											simCMD_autopilot_lvl_chg:once()
										end
										vnav_desc_spd = 1	-- change to VNAV SPD
										at_mode = 7
										B738DR_eng1_N1_thrust_trg = 0
										B738DR_eng2_N1_thrust_trg = 0
										
										if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
											at_mode = 0
											if B738DR_autopilot_autothr_arm_pos == 0 then
												retard_timer = 0
											elseif retard_timer > 0 then
												retard_timer = retard_timer - SIM_PERIOD
												if retard_timer < 0 then
													retard_timer = 0
												end
											end
											if retard_timer == 0 then
												-- B738DR_pfd_spd_mode = PFD_SPD_ARM
												vnav_pfd_spd_mode = PFD_SPD_ARM
											else
												-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
												vnav_pfd_spd_mode = PFD_SPD_RETARD
											end
										else
											-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
											vnav_pfd_spd_mode = PFD_SPD_RETARD
											retard_timer = 3
										end
									end
								else
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
									end
									vnav_desc_spd = 1	-- change to VNAV SPD
									at_mode = 7
									B738DR_eng1_N1_thrust_trg = 0.0
									B738DR_eng2_N1_thrust_trg = 0.0
									
									if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
										at_mode = 0
										if B738DR_autopilot_autothr_arm_pos == 0 then
											retard_timer = 0
										elseif retard_timer > 0 then
											retard_timer = retard_timer - SIM_PERIOD
											if retard_timer < 0 then
												retard_timer = 0
											end
										end
										if retard_timer == 0 then
											-- B738DR_pfd_spd_mode = PFD_SPD_ARM
											vnav_pfd_spd_mode = PFD_SPD_ARM
										else
											-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
											vnav_pfd_spd_mode = PFD_SPD_RETARD
										end
									elseif B738DR_autopilot_autothr_arm_pos == 1 then
										-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
										vnav_pfd_spd_mode = PFD_SPD_RETARD
										retard_timer = 3
									else
										-- B738DR_pfd_spd_mode = PFD_SPD_ARM
										vnav_pfd_spd_mode = PFD_SPD_ARM
									end
								end
								retard_enabled = 1
								B738DR_fms_descent_now = 3
								B738DR_flight_phase = 5
								vnav_alt_hold_act = 0
								mcp_alt_hold = -1
							else
								vnav_desc_idle_time = 0
							end
						-- else
						elseif B738DR_vnav_td_dist < 0.5 then
							B738DR_vnav_alt_mode = 1
							-- auto_descent_fail = 1
							vnav_alt_hold_act = 0
							mcp_alt_hold = -1
							if B738DR_vnav_alt_disable == 0 then
								vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
							elseif B738DR_vnav_alt_disable ~= 0 then
								if simDR_ap_altitude_dial_ft < B738DR_fmc_cruise_alt then
									B738DR_vnav_alt_mode = 0
									vnav_stop()
									simDR_ap_vvi_dial = 0
									B738DR_ap_vvi_dial = 0
									ap_pitch_mode = 3
									ap_pitch_mode_eng = 3
									
									-- retard_enabled = 1
									-- B738DR_fms_descent_now = 3
									-- B738DR_flight_phase = 5
									-- vnav_alt_hold_act = 0
									-- mcp_alt_hold = -1
									
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										B738DR_preselected_alt = simDR_ap_altitude_dial_ft
										simCMD_autopilot_alt_hold:once()
									end
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 2
									end
								else
									vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
								end
							end
						end
					end
				end
				
			-- PHASE CRUISE CLIMB
			elseif B738DR_flight_phase == 3 then
				
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					
					local ap_capture_alt = B738DR_AP_altitude + (B738DR_AP_vvi / 60 * 9)
					local vnav_alt_acq_act = false
					
					if B738DR_AP_altitude < simDR_ap_altitude_dial_ft then
						if ap_capture_alt >= simDR_ap_altitude_dial_ft then
							vnav_alt_acq_act = true
						end
					end
					
					if vnav_alt_acq_act == true then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
				end
				
				retard_enabled = 1
				B738DR_vnav_alt_mode = 0
				-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
				vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
				-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_PTH
				vnav_pfd_alt_mode = PFD_ALT_VNAV_PTH
				
				-- crossover altitude
				const_spd = 1
				if simDR_IAS > 45 then
					const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
				end
				mach_to_ias = (B738DR_fmc_climb_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
				if B738DR_ap_spd_interv_status == 0 then
					if simDR_airspeed_is_mach == 0 then
						if mach_to_ias <= B738DR_fmc_climb_speed and mach_to_ias <= simDR_IAS then
							simCMD_autopilot_co:once()
						end
					else
						if mach_to_ias > B738DR_fmc_climb_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
							simCMD_autopilot_co:once()
						end
					end
				else
					-- climb
					if simDR_altitude_pilot > 26000 and spd_interv_co_used == 0 then
						if simDR_airspeed_is_mach == 0 then
							simCMD_autopilot_co:once()
						end
						spd_interv_co_used = 1
					elseif simDR_altitude_pilot < 26000 then
						spd_interv_co_used = 0
					end
				end
				
				-- cruise climb speed
				if simDR_airspeed_is_mach == 0 then
					B738DR_vnav_speed_trg = math.min(B738DR_fmc_climb_speed, spd_250_10000)
				else
					B738DR_vnav_speed_trg = B738DR_fmc_climb_speed_mach
				end
				
				vnav_alt = math.min(B738DR_fmc_cruise_alt, mcp_alt_dial_app)
				simDR_ap_altitude_dial_ft = vnav_alt
				
				-- if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and simDR_ap_alt_hold_ft == B738DR_fmc_cruise_alt then
				if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and B738DR_preselected_alt == B738DR_fmc_cruise_alt then
					B738DR_flight_phase = 2
				else
					if simDR_autopilot_altitude_mode ~= 5 then		-- LVL CHG
						if simDR_altitude_pilot < B738DR_fmc_cruise_alt then
							if simDR_autopilot_altitude_mode ~= 5 then		-- LVL CHG
								at_mode_old = at_mode
								simCMD_autopilot_lvl_chg:once()
								at_mode = at_mode_old
							end
						else
							B738DR_flight_phase = 2
						end
					end
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 7
						B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
						B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
						-- B738DR_pfd_spd_mode = PFD_SPD_N1
						vnav_pfd_spd_mode = PFD_SPD_N1
						-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_SPD
						vnav_pfd_alt_mode = PFD_ALT_VNAV_SPD
					end
				end
				
			-- PHASE CRUISE DESCENT
			elseif B738DR_flight_phase == 4 then
				
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					
					local ap_capture_alt = B738DR_AP_altitude + (B738DR_AP_vvi / 60 * 9)
					local vnav_alt_acq_act = false
					
					if B738DR_AP_altitude > simDR_ap_altitude_dial_ft then
						if ap_capture_alt <= simDR_ap_altitude_dial_ft then
							vnav_alt_acq_act = true
						end
					end
					
					if vnav_alt_acq_act == true then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
				end
				
				retard_enabled = 1
				B738DR_vnav_alt_mode = 0
				-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
				vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
				-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_PTH
				vnav_pfd_alt_mode = PFD_ALT_VNAV_PTH
				
				-- crossover altitude
				const_spd = 1
				if simDR_IAS > 45 then
					const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
				end
				mach_to_ias = (B738DR_fmc_descent_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
				if B738DR_ap_spd_interv_status == 0 then
					if simDR_airspeed_is_mach == 0 then
						if mach_to_ias <= B738DR_fmc_descent_speed and mach_to_ias <= simDR_IAS then
							simCMD_autopilot_co:once()
						end
					else
						if mach_to_ias > B738DR_fmc_descent_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
							simCMD_autopilot_co:once()
						end
					end
				else
					-- descent
					if simDR_altitude_pilot < 26000 and spd_interv_co_used == 0 then
						if simDR_airspeed_is_mach == 1 then
							simCMD_autopilot_co:once()
						end
						spd_interv_co_used = 1
					elseif simDR_altitude_pilot > 26000 then
						spd_interv_co_used = 0
					end
				end
				
				-- cruise climb speed
				if simDR_airspeed_is_mach == 0 then
					B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000)
				else
					B738DR_vnav_speed_trg = B738DR_fmc_descent_speed_mach
				end
				
				-- cruise speed
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 9		-- SPEED mode on with N1 limit
				end
				
				vnav_alt = math.max(B738DR_fmc_cruise_alt, mcp_alt_dial_app)
				simDR_ap_altitude_dial_ft = vnav_alt
				
				-- if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and simDR_ap_alt_hold_ft == B738DR_fmc_cruise_alt then
				if (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and B738DR_preselected_alt == B738DR_fmc_cruise_alt then
					B738DR_flight_phase = 2
				else
					if simDR_autopilot_altitude_mode ~= 4 then		-- VS
						if simDR_altitude_pilot > B738DR_fmc_cruise_alt then
							-- if simDR_ap_vvi_dial > 0 then
								-- B738DR_ap_vvi_dial = 0
							-- end
							if flight_phase_old ~= B738DR_flight_phase then
								B738DR_ap_vvi_dial = B738DR_AP_vvi
							end
							B738DR_ap_vvi_dial = vnav_vvi_limit(-1000, B738DR_ap_vvi_dial, 0, 90)
							-- if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
								-- simDR_ap_vvi_dial = B738DR_ap_vvi_dial
							-- elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
								-- simDR_ap_vvi_dial = B738DR_ap_vvi_dial
							-- end
							simDR_ap_vvi_dial = -1000
							if simDR_autopilot_altitude_mode ~= 4 then		-- VS mode
								simCMD_autopilot_vs_sel:once()
							end
						else
							B738DR_flight_phase = 2
						end
					else
						if simDR_altitude_pilot > B738DR_fmc_cruise_alt then
							B738DR_ap_vvi_dial = vnav_vvi_limit(-1000, B738DR_ap_vvi_dial, 0, 90)
						else
							B738DR_ap_vvi_dial = 0
						end
					end
				end
				
				-- capture extended idle descent path
				if B738DR_vnav_alt_err99 < 500 and B738DR_vnav_vvi99 < -1000 then
					retard_enabled = 1
					B738DR_fms_descent_now = 3
					B738DR_flight_phase = 5
					vnav_alt_hold_act = 0
					mcp_alt_hold = -1
				end
				
			-- PHASE DESCENT
			elseif B738DR_flight_phase > 4 and B738DR_flight_phase < 8 then
				
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					
					local ap_capture_alt = B738DR_AP_altitude + (B738DR_AP_vvi / 60 * 9)
					local vnav_alt_acq_act = false
					
					if B738DR_AP_altitude > simDR_ap_altitude_dial_ft then
						if ap_capture_alt <= simDR_ap_altitude_dial_ft then
							vnav_alt_acq_act = true
						end
					end
					
					if vnav_alt_acq_act == true then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
				end
				
				vnav_alt_hld = 0
				
				-- DESCENT NOW >>> 2
				if B738DR_fms_descent_now == 2 then
					
					-- VNAV ALT
					if B738DR_vnav_alt_mode == 0 then
					
						if B738DR_descent_mode == 1 or B738DR_descent_mode == 3 then
							vnav_desc_spd = 1
							if simDR_autopilot_altitude_mode ~= 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								simCMD_autopilot_lvl_chg:once()
								at_mode = 7
							end
						end
						
						-- prevent overspeed
						delta_vvi = 0
						if B738DR_vnav_desc_spd_disable == 0 and vnav_desc_spd == 0 then
							if simDR_airspeed_is_mach == 0 then
								if B738DR_AP_airspeed > 340 then 
									delta_vvi = 1
								else
									
									if B738DR_ap_spd_interv_status == 0 then
										alt_x = B738DR_vnav_speed_trg + 60
										alt_x3 = (0.82 * (simDR_sos * 1.94384449244)) / const_spd
										if alt_x > B738DR_flaps_speed then
											alt_x = B738DR_flaps_speed
										end
										if alt_x > alt_x3 then
											alt_x = alt_x3
										end
										alt_x = alt_x - B738DR_vnav_speed_trg
										if alt_x < 0 then
											alt_x = 0
										end
									end
									
									if alt_x > 0 then
										if B738DR_AP_airspeed > B738DR_vnav_speed_trg + alt_x then
											delta_vvi = 1
										end
									end
									
								end
							else
								if simDR_mach_no > 0.82 then
									delta_vvi = 1
								end
								
								if B738DR_ap_spd_interv_status == 0 then
									if simDR_IAS > 45 then
										const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
									end
									alt_x2 = (B738DR_vnav_speed_trg * (simDR_sos * 1.94384449244)) / const_spd
									
									alt_x = alt_x2 + 60
									if alt_x > B738DR_flaps_speed then
										alt_x = B738DR_flaps_speed
									end
									alt_x = alt_x - alt_x2
									
									if alt_x > 0 then
										if B738DR_AP_airspeed > alt_x2 + alt_x then
											delta_vvi = 1
										end
									end
								end
							end
						end
						if delta_vvi == 1 then
							if B738DR_vnav_alt_disable == 0 then
								-- switch to VNAV SPD
								vnav_desc_protect_spd = 1
								if simDR_autopilot_altitude_mode ~= 5 then
									simCMD_autopilot_lvl_chg:once()
									at_mode = 7
								end
								retard_enabled = 1
							else
								-- switch to LVL CHG
								ap_pitch_mode = 2
							end
						else
							if vnav_desc_protect_spd == 1 then
								if B738DR_vnav_alt_err < -70 and B738DR_vnav_alt_err > -150 then
									-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
									vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
									-- simDR_ap_vvi_dial = vvi_des
									B738DR_ap_vvi_dial = vvi_des
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									simCMD_autopilot_vs_sel:once()
									vnav_desc_protect_spd = 0 -- change to VNAV PTH
								end
							end
						end
						if B738DR_vnav_desc_spd_disable == 1 and simDR_autopilot_altitude_mode ~= 4 then
							-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
							vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
							-- simDR_ap_vvi_dial = vvi_des
							B738DR_ap_vvi_dial = vvi_des
							if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
								simDR_ap_vvi_dial = B738DR_ap_vvi_dial
							elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
								simDR_ap_vvi_dial = B738DR_ap_vvi_dial
							end
							simCMD_autopilot_vs_sel:once()
							vnav_desc_spd = 0
							vnav_desc_protect_spd = 0 -- change to VNAV PTH
						end
						
						if ignore_td_idx2 == 0 then
							if B738DR_rest_wpt_alt == 0 or B738DR_rest_wpt_alt_t == 45 then		-- ignored Below alt restrict
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then	--0.3
									vnav_alt = math.max(mcp_alt_dial_app, B738DR_td_alt2)
								else
									vnav_alt = mcp_alt_dial_app
								end
							else
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then
									if simDR_altitude_pilot < B738DR_td_alt2 then
										vnav_alt = math.max(B738DR_rest_wpt_alt, mcp_alt_dial_app)
									else
										vnav_alt = math.max(B738DR_rest_wpt_alt, mcp_alt_dial_app, B738DR_td_alt2)
									end
								else
									vnav_alt = math.max(B738DR_rest_wpt_alt, mcp_alt_dial_app)
								end
								rest_wpt_alt_old = B738DR_rest_wpt_alt
							end
						else
							vnav_alt = mcp_alt_dial_app
						end
						
						if B738DR_vnav_alt_err > 150 and simDR_altitude_pilot > 5000 then
							vnav_alt = math.max(vnav_alt, 5000)
						end
						
						if vnav_desc_spd == 0 then
						
							-- restrict altitude
							simDR_ap_altitude_dial_ft = vnav_alt
							if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
								if vnav_alt_hold_act == 0 then
									vnav_alt_hold_act = 1
									vnav_alt_hold = vnav_alt
									mcp_alt_hold = mcp_alt_dial_app
									at_mode = 2
									B738DR_fms_descent_now = 3
								end
								-- simDR_ap_vvi_dial = B738DR_AP_vvi	--simDR_vvi_fpm_pilot
								B738DR_ap_vvi_dial = B738DR_AP_vvi
								if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
									simDR_ap_vvi_dial = B738DR_ap_vvi_dial
								elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
									simDR_ap_vvi_dial = B738DR_ap_vvi_dial
								end
								delta_vvi = simDR_altitude_pilot - B738DR_preselected_alt		--simDR_ap_alt_hold_ft
								if delta_vvi < 150 then -- HOLD stabilized
									delta_alt_dial = simDR_altitude_pilot - vnav_alt
									delta_vvi = simDR_altitude_pilot - mcp_alt_dial_app
									-- delta_ap_alt = math.abs(simDR_ap_alt_hold_ft - B738DR_td_alt2)
									delta_ap_alt = math.abs(B738DR_preselected_alt - B738DR_td_alt2)
									if B738DR_td_alt2_hold == 0 then
										-- if B738DR_ed_alt2_hold == 0 or simDR_ap_alt_hold_ft ~= B738DR_ed_alt then
										if B738DR_ed_alt2_hold == 0 or B738DR_preselected_alt ~= B738DR_ed_alt then
											if B738DR_vnav_alt_err2 < 150 then	--100
												-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
												if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
													if simDR_autopilot_altitude_mode ~= 4 then
														simCMD_autopilot_vs_sel:once()
														-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
														vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
														-- simDR_ap_vvi_dial = vvi_des
														B738DR_ap_vvi_dial = vvi_des
														if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														end
														vnav_alt_hold_act = 0
													end
												-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
												elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													B738DR_vnav_alt_mode = 1
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
														-- auto_descent_fail = 1
													-- end
													-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
													vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
												end
											end
										end
									else
										if (B738DR_td_idx2 ~= 0 and B738DR_td_idx2 < B738DR_offset) or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist < B738DR_td_dist2 + 1.0) then
											if B738DR_vnav_alt_err2 < 150 then
												-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
												if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
													if simDR_autopilot_altitude_mode ~= 4 then	--and simDR_glideslope_status == 0 then
														simCMD_autopilot_vs_sel:once()
														-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
														vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
														-- simDR_ap_vvi_dial = vvi_des
														B738DR_ap_vvi_dial = vvi_des
														if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														end
														vnav_alt_hold_act = 0
													end
												-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
												elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													B738DR_vnav_alt_mode = 1
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
														-- auto_descent_fail = 1
													-- end
													-- -- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
													vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
												end
											end
										end
									end
								end
							else
								if vnav_desc_protect_spd == 0 then
									if simDR_autopilot_altitude_mode ~= 4 then
										simCMD_autopilot_vs_sel:once()
										at_mode = 2
									end
									if simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = 0
										B738DR_ap_vvi_dial = 0
									end
									-- vvi_des = vnav_vvi_limit(-1000, simDR_ap_vvi_dial, 0, 90)
									vvi_des = vnav_vvi_limit(-1000, B738DR_ap_vvi_dial, 0, 90)
									-- simDR_ap_vvi_dial = vvi_des
									B738DR_ap_vvi_dial = vvi_des
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
								else
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
										at_mode = 7
									end
								end
							end
							
						else	-- VNAV SPD descent
							
							-- restrict altitude
							simDR_ap_altitude_dial_ft = vnav_alt
							if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
								if vnav_alt_hold_act == 0 then
									vnav_alt_hold_act = 1
									vnav_alt_hold = vnav_alt
									mcp_alt_hold = mcp_alt_dial_app
									at_mode = 2
									B738DR_fms_descent_now = 3
								end
								delta_vvi = simDR_altitude_pilot - B738DR_preselected_alt	--simDR_ap_alt_hold_ft
								-- simDR_ap_vvi_dial = B738DR_AP_vvi
								B738DR_ap_vvi_dial = B738DR_AP_vvi
								if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
									simDR_ap_vvi_dial = B738DR_ap_vvi_dial
								elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
									simDR_ap_vvi_dial = B738DR_ap_vvi_dial
								end
								if delta_vvi < 150 then -- HOLD stabilized
									rest_wpt_alt_old = B738DR_rest_wpt_alt
									delta_alt_dial = simDR_altitude_pilot - vnav_alt
									delta_vvi = simDR_altitude_pilot - mcp_alt_dial_app
									-- delta_ap_alt = math.abs(simDR_ap_alt_hold_ft - B738DR_td_alt2)
									delta_ap_alt = math.abs(B738DR_preselected_alt - B738DR_td_alt2)
									if B738DR_td_alt2_hold == 0 then
										-- if B738DR_ed_alt2_hold == 0 or simDR_ap_alt_hold_ft ~= B738DR_ed_alt then
										if B738DR_ed_alt2_hold == 0 or B738DR_preselected_alt ~= B738DR_ed_alt then
											if B738DR_vnav_alt_err2 < 150 then
												-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
												if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
													if simDR_autopilot_altitude_mode ~= 4 then	--and simDR_glideslope_status == 0 then
														simCMD_autopilot_vs_sel:once()
														at_mode = 2
														B738DR_fms_descent_now = 3
														-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
														vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
														-- simDR_ap_vvi_dial = vvi_des
														B738DR_ap_vvi_dial = vvi_des
														if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														end
														vnav_alt_hold_act = 0
														vnav_desc_protect_spd = 0
														rest_wpt_alt_old = 0
													end
												-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
												elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													B738DR_vnav_alt_mode = 1
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
														-- auto_descent_fail = 1
													-- end
													-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
													vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
												end
											end
										end
									else
										if (B738DR_td_idx2 ~= 0 and B738DR_td_idx2 < B738DR_offset) or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist < B738DR_td_dist2 + 1.0) then
											if B738DR_vnav_alt_err2 < 150 then
												-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
												if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
													if simDR_autopilot_altitude_mode ~= 4 then
														simCMD_autopilot_vs_sel:once()
														at_mode = 2
														B738DR_fms_descent_now = 3
														-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
														vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
														-- simDR_ap_vvi_dial = vvi_des
														B738DR_ap_vvi_dial = vvi_des
														if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
															simDR_ap_vvi_dial = B738DR_ap_vvi_dial
														end
														vnav_alt_hold_act = 0
														vnav_desc_protect_spd = 0
														rest_wpt_alt_old = 0
													end
												-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
												elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													B738DR_vnav_alt_mode = 1
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
													-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
														-- auto_descent_fail = 1
													-- end
													-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
													vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
												end
											end
										end
									end
								end
							else
								if simDR_autopilot_altitude_mode ~= 5 then
									simCMD_autopilot_lvl_chg:once()
									at_mode = 7
								end
							end
						
						end
						
						-- catch VNAV path
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							if B738DR_td_alt2_hold == 0 then
								if B738DR_fms_descent_now == 2 and B738DR_vnav_alt_err2 < 150 then
									if simDR_autopilot_altitude_mode ~= 4 then
										simCMD_autopilot_vs_sel:once()
									end
									at_mode = 2
									B738DR_fms_descent_now = 3
									-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
									vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
									-- simDR_ap_vvi_dial = vvi_des
									B738DR_ap_vvi_dial = vvi_des
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									vnav_alt_hold_act = 0
									vnav_desc_protect_spd = 0
									vnav_desc_spd = 0
									rest_wpt_alt_old = 0
									vnav_alt_hold = B738DR_alt_hold_mem
								end
							else
								if (B738DR_td_idx2 ~= 0 and B738DR_td_idx2 < B738DR_offset) or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist < B738DR_td_dist2 + 1.0) then
									if B738DR_fms_descent_now == 2 and B738DR_vnav_alt_err2 < 150 then
										if simDR_autopilot_altitude_mode ~= 4 then
											simCMD_autopilot_vs_sel:once()
										end
										at_mode = 2
										B738DR_fms_descent_now = 3
										-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
										vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
										-- simDR_ap_vvi_dial = vvi_des
										B738DR_ap_vvi_dial = vvi_des
										if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										end
										vnav_alt_hold_act = 0
										vnav_desc_protect_spd = 0
										vnav_desc_spd = 0
										rest_wpt_alt_old = 0
										vnav_alt_hold = B738DR_alt_hold_mem
									end
								end
							end
						end
					else
						simDR_ap_altitude_dial_ft = mcp_alt_dial_app
						vnav_alt_hold_act = 0
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							B738DR_preselected_alt = simDR_ap_altitude_dial_ft
							simCMD_autopilot_alt_hold:once()
						end
					end
					
					if B738DR_vnav_alt_disable ~= 0 and B738DR_vnav_alt_mode ~= 0 then
					-- if B738DR_vnav_alt_disable ~= 0 then
						-- B738DR_pfd_alt_mode = PFD_ALT_ALT_HOLD
						vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
					-- elseif B738DR_vnav_alt_disable == 0 and B738DR_vnav_alt_mode ~= 0 then
						-- B738DR_vnav_alt_mode = 0
						-- vnav_stop()
						-- simDR_ap_vvi_dial = 0
						-- B738DR_ap_vvi_dial = 0
						-- if simDR_ap_alt_hold_ft == mcp_alt_dial_app then
							-- ap_pitch_mode = 3
							-- ap_pitch_mode_eng = 3
						-- end
						-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							-- simCMD_autopilot_alt_hold:once()
						-- else
							-- delta_vvi = simDR_altitude_pilot - simDR_ap_alt_hold_ft
							-- if delta_vvi > 70 then
								-- ap_pitch_mode = 6
								-- ap_pitch_mode_eng = 6
								-- altitude_dial_ft_old = B738DR_mcp_alt_dial
							-- end
						-- end
						-- if B738DR_autopilot_autothr_arm_pos == 1 then
							-- at_mode = 2
						-- end
					end
					
					-- crossover altitude
					const_spd = 1
					if simDR_IAS > 45 then
						const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
					end
					mach_to_ias = (B738DR_fmc_descent_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
					if B738DR_ap_spd_interv_status == 0 then
						if simDR_airspeed_is_mach == 0 then
							if mach_to_ias <= B738DR_fmc_descent_speed and mach_to_ias <= simDR_IAS then
								simCMD_autopilot_co:once()
							end
						else
							if mach_to_ias > B738DR_fmc_descent_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
								simCMD_autopilot_co:once()
							end
						end
					else
						-- descent
						if simDR_altitude_pilot < 26000 and spd_interv_co_used == 0 then
							if simDR_airspeed_is_mach == 1 then
								simCMD_autopilot_co:once()
							end
							spd_interv_co_used = 1
						elseif simDR_altitude_pilot > 26000 then
							spd_interv_co_used = 0
						end
					end
					
					-- descent speed
					
					-- flaps limit speeds
					B738DR_flaps_speed = 340
					if flaps == 0 then
						B738DR_flaps_speed = 340
					elseif flaps <= 0.125 then		-- flaps 1
						if B738DR_pfd_flaps_1 == 0 then
							B738DR_flaps_speed = 250
						else
							B738DR_flaps_speed = B738DR_pfd_flaps_1
						end
					elseif flaps <= 0.25 then		-- flaps 2
						if B738DR_pfd_flaps_2 == 0 then
							B738DR_flaps_speed = 230
						else
							B738DR_flaps_speed = B738DR_pfd_flaps_2
						end
					elseif flaps <= 0.375 then		-- flaps 5
						if B738DR_pfd_flaps_5 == 0 then
							B738DR_flaps_speed = 210
						else
							B738DR_flaps_speed = B738DR_pfd_flaps_5
						end
					elseif flaps <= 0.5 then		-- flaps 10
						if B738DR_pfd_flaps_10 == 0 then
							B738DR_flaps_speed = 200
						else
							B738DR_flaps_speed = B738DR_pfd_flaps_10
						end
					elseif flaps <= 0.625 then		-- flaps 15
						if B738DR_fms_vref_15 == 0 then
							if B738DR_pfd_flaps_15 == 0 then
								B738DR_flaps_speed = 190
							else
								B738DR_flaps_speed = B738DR_pfd_flaps_15
							end
						else
							B738DR_flaps_speed = B738DR_fms_vref_15
						end
					elseif flaps <= 0.75 then		-- flaps 25
						if B738DR_fms_vref_25 == 0 then
							B738DR_flaps_speed = 175
						else
							B738DR_flaps_speed = B738DR_fms_vref_25
						end
					elseif flaps <= 0.875 then		-- flaps 30
						if B738DR_fms_vref_30 == 0 then
							B738DR_flaps_speed = 165
						else
							B738DR_flaps_speed = B738DR_fms_vref_30
						end
					else		-- flaps 40
						if B738DR_fms_vref_40 == 0 then
							B738DR_flaps_speed = 155
						else
							B738DR_flaps_speed = B738DR_fms_vref_40
						end
					end
					
					if simDR_airspeed_is_mach == 0 then
						if B738DR_rest_wpt_spd_tgt > 0 then
							
							rest_wpt_spd = rest_wpt_spd_kts(B738DR_rest_wpt_spd_tgt)
							
							if B738DR_mcp_speed_dial < 100 then
								B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_descent_speed)
							else
								if B738DR_fmc_dist < B738DR_decel_dist then
									B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000, rest_wpt_spd)
								else
									B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000)
								end
							end
							if B738DR_flaps_speed == 340 then
								-- flaps up
								if B738DR_pfd_flaps_up == 340 then
									if B738DR_vnav_speed_trg < 210 then
										B738DR_vnav_speed_trg = 210
									end
								else
									if B738DR_vnav_speed_trg < B738DR_pfd_flaps_up then
										B738DR_vnav_speed_trg = B738DR_pfd_flaps_up
									end
								end
							else
								B738DR_vnav_speed_trg = B738DR_flaps_speed
							end
							
						else
							B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000, B738DR_flaps_speed)
						end
						if B738DR_fms_approach_speed > 0 and B738DR_approach_flaps_set == 1 then
							B738DR_vnav_speed_trg = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
						end
						
					else
						if B738DR_rest_wpt_spd_tgt > 0 then
							rest_wpt_spd = rest_wpt_spd_mach(B738DR_rest_wpt_spd_tgt)
							
							B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed_mach, rest_wpt_spd)
						else
							B738DR_vnav_speed_trg = B738DR_fmc_descent_speed_mach
						end
					end
					
					if B738DR_vnav_speed_trg == rest_wpt_spd and rest_wpt_spd ~= 0 and B738DR_vnav_speed_trg < B738DR_fmc_descent_speed and simDR_airspeed_is_mach == 0 then
						B738DR_fmc_descent_speed_l_act = 1
					elseif B738DR_vnav_speed_trg == rest_wpt_spd and rest_wpt_spd ~= 0 and B738DR_vnav_speed_trg < B738DR_fmc_descent_speed_mach and simDR_airspeed_is_mach ~= 0 then
						B738DR_fmc_descent_speed_l_act = 1
					elseif B738DR_vnav_speed_trg == spd_250_10000 and spd_250_10000 < 340 and spd_250_10000 < B738DR_fmc_descent_speed then
						B738DR_fmc_descent_speed_l_act = 2
					elseif B738DR_vnav_speed_trg == B738DR_flaps_speed and B738DR_flaps_speed < 340 then
						B738DR_fmc_descent_speed_l_act = 3
					elseif B738DR_fms_approach_speed > 0 and B738DR_approach_flaps_set == 1 and B738DR_vnav_speed_trg == B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr then
						B738DR_fmc_descent_speed_l_act = 3
					elseif B738DR_vnav_speed_trg == B738DR_pfd_flaps_up and B738DR_pfd_flaps_up < 340 then
						B738DR_fmc_descent_speed_l_act = 4
					else
						B738DR_fmc_descent_speed_l_act = 0
					end
				
				-- DESCENT NOW >>> 3
				else
					B738DR_fms_descent_now = 3
					
					if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						if B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist < B738DR_td_dist2 + 1.5 then
							if vnav_desc_idle == 0 and mcp_alt_dial_app < B738DR_td_alt2 then
								vnav_desc_idle = 1
								vnav_desc_idle_time = (((B738DR_fmc_dist - B738DR_td_dist2 - 0.1) * 1852) / simDR_ground_spd) -- - 1.9 	-- 1.4 sec before
								vnav_desc_idle_time = math.max(vnav_desc_idle_time, 0.5)
							end
						end
					end
					
					if vnav_desc_idle == 1 then
						if is_timer_scheduled(idle_timer) == false then
							run_after_time(idle_timer, vnav_desc_idle_time)
						end
					elseif vnav_desc_idle == 2 then
						at_mode = 7
						B738DR_eng1_N1_thrust_trg = 0.0
						B738DR_eng2_N1_thrust_trg = 0.0
						
						if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
							at_mode = 0
						elseif B738DR_autopilot_autothr_arm_pos == 1 then
							retard_timer = 3
						end
						vnav_desc_idle = 3
						retard_enabled = 1
					elseif vnav_desc_idle == 3 then
						at_mode = 7
						B738DR_eng1_N1_thrust_trg = 0.0
						B738DR_eng2_N1_thrust_trg = 0.0
						retard_enabled = 1
						
						if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
							at_mode = 0
							vnav_desc_idle = 0
							retard_enabled = 0
						elseif B738DR_autopilot_autothr_arm_pos == 1 then
							retard_timer = 3
						end
						retard_enabled = 1
					else
						if is_timer_scheduled(idle_timer) == true then
							stop_timer(idle_timer)
						end
					end
					
					delta_alt_dial = B738DR_fmc_cruise_alt - simDR_altitude_pilot
					if delta_alt_dial < 300 and (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) and mcp_alt_dial_app >= B738DR_fmc_cruise_alt then
						
						-- crossover altitude
						const_spd = 1
						if simDR_IAS > 45 then
							const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
						end
						mach_to_ias = (B738DR_fmc_descent_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
						if B738DR_ap_spd_interv_status == 0 then
							if simDR_airspeed_is_mach == 0 then
								if mach_to_ias <= B738DR_fmc_descent_speed and mach_to_ias <= simDR_IAS then
									simCMD_autopilot_co:once()
								end
							else
								if mach_to_ias > B738DR_fmc_descent_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
									simCMD_autopilot_co:once()
								end
							end
						else
							-- descent
							if simDR_altitude_pilot < 26000 and spd_interv_co_used == 0 then
								if simDR_airspeed_is_mach == 1 then
									simCMD_autopilot_co:once()
								end
								spd_interv_co_used = 1
							elseif simDR_altitude_pilot > 26000 then
								spd_interv_co_used = 0
							end
						end
						-- cruise speed
						if simDR_airspeed_is_mach == 0 then
							B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000)
						else
							B738DR_vnav_speed_trg = B738DR_fmc_descent_speed_mach
						end
						
						simDR_ap_altitude_dial_ft = mcp_alt_dial_app
					
					else
					
						-- VNAV ALT
						if B738DR_vnav_alt_mode == 0 then
							
							-- if B738DR_descent_mode == 1 or B738DR_descent_mode == 3 or (B738DR_vnav_alt_err < -200 and B738DR_vnav_desc_spd_disable == 0) then
								-- vnav_desc_spd = 1
								-- if simDR_autopilot_altitude_mode ~= 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									-- simCMD_autopilot_lvl_chg:once()
									-- at_mode = 7
								-- end
							-- end
							
							if B738DR_vnav_desc_spd_disable == 0 then
								
								if (B738DR_descent_mode == 1 or B738DR_descent_mode == 3 or (B738DR_vnav_common ~= 0 and B738DR_vnav_alt_err < -200))
								and B738DR_act_wpt_gp == 0 then
									vnav_desc_spd = 1
									if simDR_autopilot_altitude_mode ~= 5 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										simCMD_autopilot_lvl_chg:once()
										at_mode = 7
									end
								end
								
								if vnav_desc_spd == 1 then
									if B738DR_act_wpt_gp ~= 0 then
										if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
											if simDR_autopilot_altitude_mode ~= 4 then
												-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
												vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
												-- simDR_ap_vvi_dial = vvi_des
												B738DR_ap_vvi_dial = vvi_des
												if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
													simDR_ap_vvi_dial = B738DR_ap_vvi_dial
												elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
													simDR_ap_vvi_dial = B738DR_ap_vvi_dial
												end
												simCMD_autopilot_vs_sel:once()
											end
										end
										at_mode = 2
										vnav_desc_spd = 0 -- change to VNAV PTH
									end
								end
							else
								if vnav_desc_spd == 1 then
									if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										if simDR_autopilot_altitude_mode ~= 4 then
											-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
											vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
											-- simDR_ap_vvi_dial = vvi_des
											B738DR_ap_vvi_dial = vvi_des
											if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											end
											simCMD_autopilot_vs_sel:once()
										end
									end
									at_mode = 2
									vnav_desc_spd = 0 -- change to VNAV PTH
								end
							end
							
							-- prevent overspeed
							delta_vvi = 0
							if B738DR_vnav_desc_spd_disable == 0 and vnav_desc_spd == 0 then
								if simDR_airspeed_is_mach == 0 then
									if B738DR_AP_airspeed > 340 then
										delta_vvi = 1
									else
										
										if B738DR_ap_spd_interv_status == 0 then
											alt_x = B738DR_vnav_speed_trg + 60
											alt_x3 = (0.82 * (simDR_sos * 1.94384449244)) / const_spd
											if alt_x > B738DR_flaps_speed then
												alt_x = B738DR_flaps_speed
											end
											if alt_x > alt_x3 then
												alt_x = alt_x3
											end
											alt_x = alt_x - B738DR_vnav_speed_trg
											if alt_x < 0 then
												alt_x = 0
											end
										end
										
										if alt_x > 0 then
											if B738DR_AP_airspeed > B738DR_vnav_speed_trg + alt_x then
												delta_vvi = 1
											end
										end
									end
									
								else
									if simDR_mach_no > 0.82 then
										delta_vvi = 1
									else
										
										if B738DR_ap_spd_interv_status == 0 then
											if simDR_IAS > 45 then
												const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
											end
											alt_x2 = (B738DR_vnav_speed_trg * (simDR_sos * 1.94384449244)) / const_spd
											
											alt_x = alt_x2 + 60
											if alt_x > B738DR_flaps_speed then
												alt_x = B738DR_flaps_speed
											end
											alt_x = alt_x - alt_x2
											
											if alt_x > 0 then
												if B738DR_AP_airspeed > alt_x2 + alt_x then
													delta_vvi = 1
												end
											end
										end
									end
								end
							end
							
							if delta_vvi == 1 then
								if B738DR_vnav_alt_disable == 0 then
									-- switch to VNAV SPD
									vnav_desc_protect_spd = 1
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
										at_mode = 7
									end
								else
									-- switch to LVL CHG
									ap_pitch_mode = 2
								end
							else
								if vnav_desc_protect_spd == 1 then
									if B738DR_vnav_alt_err < -70 and B738DR_vnav_alt_err > -150 then
										-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
										vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
										-- simDR_ap_vvi_dial = vvi_des
										B738DR_ap_vvi_dial = vvi_des
										if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										end
										simCMD_autopilot_vs_sel:once()
										vnav_desc_protect_spd = 0 -- change to VNAV PTH
										at_mode = 2
									end
								end
							end
							if B738DR_vnav_desc_spd_disable == 1 then
								if simDR_autopilot_altitude_mode ~= 4 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
									-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
									vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
									-- simDR_ap_vvi_dial = vvi_des
									B738DR_ap_vvi_dial = vvi_des
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									simCMD_autopilot_vs_sel:once()
								end
								vnav_desc_spd = 0
								vnav_desc_protect_spd = 0 -- change to VNAV PTH
								if B738DR_ap_spd_interv_status == 0 then
									B738DR_descent_mode = 0
								else
									B738DR_descent_mode = 2
								end
							elseif vnav_desc_spd ~= 0 then
								--capture descent path
								if math.abs(B738DR_vnav_alt_err) < 200 then
									if simDR_autopilot_altitude_mode ~= 4 and simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
										-- if simDR_autopilot_altitude_mode ~= 4 then
											simCMD_autopilot_vs_sel:once()
										-- end
										-- at_mode = 2
										-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
										vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
										-- simDR_ap_vvi_dial = vvi_des
										B738DR_ap_vvi_dial = vvi_des
										if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
											simDR_ap_vvi_dial = B738DR_ap_vvi_dial
										end
										vnav_desc_spd = 0
										vnav_desc_protect_spd = 0 -- change to VNAV PTH
										if B738DR_ap_spd_interv_status == 0 then
											B738DR_descent_mode = 0
										else
											B738DR_descent_mode = 2
										end
									end
								end
							end
							
							if B738DR_rest_wpt_alt == 0 or B738DR_rest_wpt_alt_t == 45 then		-- ignored Below alt restrict
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then
									vnav_alt = math.max(mcp_alt_dial_app, B738DR_td_alt2)
								else
									vnav_alt = mcp_alt_dial_app
								end
							else
								if B738DR_td_idx2 > B738DR_offset or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist > B738DR_td_dist2 + 1.0) then
									if simDR_altitude_pilot < B738DR_td_alt2 then
										vnav_alt = math.max(B738DR_rest_wpt_alt, mcp_alt_dial_app)
									else
										vnav_alt = math.max(B738DR_rest_wpt_alt, mcp_alt_dial_app, B738DR_td_alt2)
									end
								else
									vnav_alt = math.max(B738DR_rest_wpt_alt, mcp_alt_dial_app)
								end
								rest_wpt_alt_old = B738DR_rest_wpt_alt
							end
							
							if B738DR_lpv_gs_status == 2 then
								local r_delta_alt = simDR_altitude_pilot + 650
								if B738DR_mcp_alt_dial > r_delta_alt then
									vnav_alt = B738DR_mcp_alt_dial
								else
									r_delta_alt = r_delta_alt + 1000
									vnav_alt = roundUpToIncrement(r_delta_alt, 1000)
								end
							end
							
							if vnav_desc_spd == 0 then
								-- restrict altitude
								simDR_ap_altitude_dial_ft = vnav_alt
								if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
									if vnav_alt_hold_act == 0 then
										vnav_alt_hold_act = 1
										vnav_alt_hold = vnav_alt
										if B738DR_vnav_app_active == 0 then
											mcp_alt_hold = mcp_alt_dial_app
										else
											mcp_alt_hold = 0
										end
										at_mode = 2
									end
									delta_vvi = simDR_altitude_pilot - B738DR_preselected_alt	--simDR_ap_alt_hold_ft
									-- simDR_ap_vvi_dial = B738DR_AP_vvi
									B738DR_ap_vvi_dial = B738DR_AP_vvi
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									if delta_vvi < 150 then -- HOLD stabilized
										delta_alt_dial = simDR_altitude_pilot - vnav_alt
										delta_vvi = simDR_altitude_pilot - mcp_alt_dial_app
										-- delta_ap_alt = math.abs(simDR_ap_alt_hold_ft - B738DR_td_alt2)
										delta_ap_alt = math.abs(B738DR_preselected_alt - B738DR_td_alt2)
										if B738DR_td_alt2_hold == 0 then
											-- if B738DR_ed_alt2_hold == 0 or simDR_ap_alt_hold_ft ~= B738DR_ed_alt then
											if B738DR_ed_alt2_hold == 0 or B738DR_preselected_alt ~= B738DR_ed_alt then
												if B738DR_vnav_alt_err2 < 150 then
													-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
													if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
														if simDR_autopilot_altitude_mode ~= 4 then
															simCMD_autopilot_vs_sel:once()
															-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
															vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
															-- simDR_ap_vvi_dial = vvi_des
															B738DR_ap_vvi_dial = vvi_des
															if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
																simDR_ap_vvi_dial = B738DR_ap_vvi_dial
															elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
																simDR_ap_vvi_dial = B738DR_ap_vvi_dial
															end
															vnav_alt_hold_act = 0
														end
													-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
														B738DR_vnav_alt_mode = 1
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
															-- auto_descent_fail = 1
														-- end
														-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
														vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
													end
												end
											end
										else
											if (B738DR_td_idx2 ~= 0 and B738DR_td_idx2 < B738DR_offset) or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist < B738DR_td_dist2 + 1.0) then
												if B738DR_vnav_alt_err2 < 150 then
													-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
													if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
														if simDR_autopilot_altitude_mode ~= 4 then
															simCMD_autopilot_vs_sel:once()
															-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
															vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
															-- simDR_ap_vvi_dial = vvi_des
															B738DR_ap_vvi_dial = vvi_des
															if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
																simDR_ap_vvi_dial = B738DR_ap_vvi_dial
															elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
																simDR_ap_vvi_dial = B738DR_ap_vvi_dial
															end
															vnav_alt_hold_act = 0
														end
													-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
														B738DR_vnav_alt_mode = 1
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
															-- auto_descent_fail = 1
														-- end
														-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
														vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
													end
												end
											end
										end
									end
								else
									if vnav_desc_protect_spd == 0 then
										if simDR_autopilot_altitude_mode ~= 4 then
											simCMD_autopilot_vs_sel:once()
											at_mode = 2
										end
										if B738DR_td_alt2_hold == 1 and simDR_altitude_pilot > B738DR_td_alt2 then
											-- simDR_ap_vvi_dial = math.min(B738DR_vnav_vvi, -50)
											B738DR_ap_vvi_dial = math.min(B738DR_vnav_vvi, -50)
											if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											end
										else
											-- vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, simDR_ap_vvi_dial, 0, 90)
											vvi_des = vnav_vvi_limit(B738DR_vnav_vvi, B738DR_ap_vvi_dial, 0, 90)
											-- simDR_ap_vvi_dial = vvi_des
											B738DR_ap_vvi_dial = vvi_des
											if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
												simDR_ap_vvi_dial = B738DR_ap_vvi_dial
											end
										end
									else
										if simDR_autopilot_altitude_mode ~= 5 then
											simCMD_autopilot_lvl_chg:once()
											at_mode = 7
										end
									end
								end
								
							else	-- VNAV SPD descent
								
								-- restrict altitude
								simDR_ap_altitude_dial_ft = vnav_alt
								if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
									if vnav_alt_hold_act == 0 then
										vnav_alt_hold_act = 1
										vnav_alt_hold = vnav_alt
										if B738DR_vnav_app_active == 0 then
											mcp_alt_hold = mcp_alt_dial_app
										else
											mcp_alt_hold = 0
										end
										at_mode = 2
									end
									delta_vvi = simDR_altitude_pilot - B738DR_preselected_alt	--simDR_ap_alt_hold_ft
									-- simDR_ap_vvi_dial = B738DR_AP_vvi
									B738DR_ap_vvi_dial = B738DR_AP_vvi
									if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
										simDR_ap_vvi_dial = B738DR_ap_vvi_dial
									end
									if delta_vvi < 150 then -- HOLD stabilized
										rest_wpt_alt_old = B738DR_rest_wpt_alt
										delta_alt_dial = simDR_altitude_pilot - vnav_alt
										delta_vvi = simDR_altitude_pilot - mcp_alt_dial_app
										-- delta_ap_alt = math.abs(simDR_ap_alt_hold_ft - B738DR_td_alt2)
										delta_ap_alt = math.abs(B738DR_preselected_alt - B738DR_td_alt2)
										if B738DR_td_alt2_hold == 0 then
											-- if B738DR_ed_alt2_hold == 0 or simDR_ap_alt_hold_ft ~= B738DR_ed_alt then
											if B738DR_ed_alt2_hold == 0 or B738DR_preselected_alt ~= B738DR_ed_alt then
												if B738DR_vnav_alt_err2 < 150 then
													-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
													if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
														if simDR_autopilot_altitude_mode ~= 5 then
															simCMD_autopilot_lvl_chg:once()
															at_mode = 7
															vnav_alt_hold_act = 0
														end
													-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
														B738DR_vnav_alt_mode = 1
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
															-- auto_descent_fail = 1
														-- end
														-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
														vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
													end
												end
											end
										else
											if (B738DR_td_idx2 ~= 0 and B738DR_td_idx2 < B738DR_offset) or (B738DR_td_idx2 == B738DR_offset and B738DR_fmc_dist < B738DR_td_dist2 + 1.0) then
												if B738DR_vnav_alt_err2 < 150 then
													-- if delta_alt_dial > 160 and mcp_alt_dial_app < simDR_ap_alt_hold_ft then	--175
													if delta_alt_dial > 160 and mcp_alt_dial_app < B738DR_preselected_alt then	--175
														if simDR_autopilot_altitude_mode ~= 5 then
															simCMD_autopilot_lvl_chg:once()
															at_mode = 7
															vnav_alt_hold_act = 0
														end
													-- elseif mcp_alt_dial_app >= simDR_ap_alt_hold_ft and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
													elseif mcp_alt_dial_app >= B738DR_preselected_alt and (delta_ap_alt > 99 or B738DR_td_idx2 == 0) then
														B738DR_vnav_alt_mode = 1
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < simDR_ap_alt_hold_ft + 50) or B738DR_vnav_alt_err2 < -100 then
														-- if (B738DR_vnav_alt_err2 < -50 and simDR_altitude_pilot < B738DR_preselected_alt + 50) or B738DR_vnav_alt_err2 < -100 then
															-- auto_descent_fail = 1
														-- end
														-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
														vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
													end
												end
											end
										end
									end
								else
									if simDR_autopilot_altitude_mode ~= 5 then
										simCMD_autopilot_lvl_chg:once()
										at_mode = 7
									end
								end
							
							end
						
						else
							simDR_ap_altitude_dial_ft = mcp_alt_dial_app
							vnav_alt_hold_act = 0
							if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								B738DR_preselected_alt = simDR_ap_altitude_dial_ft
								simCMD_autopilot_alt_hold:once()
							end
						end
						
						if B738DR_vnav_alt_disable ~= 0 and B738DR_vnav_alt_mode ~= 0 then
						-- if B738DR_vnav_alt_disable ~= 0 then
							-- B738DR_pfd_alt_mode = PFD_ALT_ALT_HOLD
							vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
						-- elseif B738DR_vnav_alt_disable == 0 and B738DR_vnav_alt_mode ~= 0 then
							-- B738DR_vnav_alt_mode = 0
							-- vnav_alt_hld = 0
							-- vnav_stop()
							-- simDR_ap_vvi_dial = 0
							-- B738DR_ap_vvi_dial = 0
							-- if simDR_ap_alt_hold_ft == mcp_alt_dial_app then
								-- ap_pitch_mode = 3
								-- ap_pitch_mode_eng = 3
							-- end
							-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
								-- simCMD_autopilot_alt_hold:once()
							-- else
								-- delta_vvi = simDR_altitude_pilot - simDR_ap_alt_hold_ft
								-- if delta_vvi > 70 then
									-- ap_pitch_mode = 6
									-- ap_pitch_mode_eng = 6
									-- altitude_dial_ft_old = B738DR_mcp_alt_dial
								-- end
							-- end
							-- if B738DR_autopilot_autothr_arm_pos == 1 then
								-- at_mode = 2
							-- end
						end
						
						-- crossover altitude
						const_spd = 1
						if simDR_IAS > 45 then
							const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
						end
						mach_to_ias = (B738DR_fmc_descent_speed_mach * (simDR_sos * 1.94384449244)) / const_spd
						if B738DR_ap_spd_interv_status == 0 then
							if simDR_airspeed_is_mach == 0 then
								if mach_to_ias <= B738DR_fmc_descent_speed and mach_to_ias <= simDR_IAS then
									simCMD_autopilot_co:once()
								end
							else
								if mach_to_ias > B738DR_fmc_descent_speed + 0.5 and mach_to_ias > simDR_IAS + 0.5 then
									simCMD_autopilot_co:once()
								end
							end
						else
							-- descent
							if simDR_altitude_pilot < 26000 and spd_interv_co_used == 0 then
								if simDR_airspeed_is_mach == 1 then
									simCMD_autopilot_co:once()
								end
								spd_interv_co_used = 1
							elseif simDR_altitude_pilot > 26000 then
								spd_interv_co_used = 0
							end
						end
						
						-- descent speed
						-- flaps limit speeds
						B738DR_flaps_speed = 340
						if flaps == 0 then
							B738DR_flaps_speed = 340
						elseif flaps <= 0.125 then		-- flaps 1
							if B738DR_pfd_flaps_1 == 0 then
								B738DR_flaps_speed = 250
							else
								B738DR_flaps_speed = B738DR_pfd_flaps_1
							end
						elseif flaps <= 0.25 then		-- flaps 2
							if B738DR_pfd_flaps_2 == 0 then
								B738DR_flaps_speed = 230
							else
								B738DR_flaps_speed = B738DR_pfd_flaps_2
							end
						elseif flaps <= 0.375 then		-- flaps 5
							if B738DR_pfd_flaps_5 == 0 then
								B738DR_flaps_speed = 210
							else
								B738DR_flaps_speed = B738DR_pfd_flaps_5
							end
						elseif flaps <= 0.5 then		-- flaps 10
							if B738DR_pfd_flaps_10 == 0 then
								B738DR_flaps_speed = 200
							else
								B738DR_flaps_speed = B738DR_pfd_flaps_10
							end
						elseif flaps <= 0.625 then		-- flaps 15
							if B738DR_fms_vref_15 == 0 then
								if B738DR_pfd_flaps_15 == 0 then
									B738DR_flaps_speed = 190
								else
									B738DR_flaps_speed = B738DR_pfd_flaps_15
								end
							else
								B738DR_flaps_speed = B738DR_fms_vref_15
							end
						elseif flaps <= 0.75 then		-- flaps 25
							if B738DR_fms_vref_25 == 0 then
								B738DR_flaps_speed = 175
							else
								B738DR_flaps_speed = B738DR_fms_vref_25
							end
						elseif flaps <= 0.875 then		-- flaps 30
							if B738DR_fms_vref_30 == 0 then
								B738DR_flaps_speed = 165
							else
								B738DR_flaps_speed = B738DR_fms_vref_30
							end
						else		-- flaps 40
							if B738DR_fms_vref_40 == 0 then
								B738DR_flaps_speed = 155
							else
								B738DR_flaps_speed = B738DR_fms_vref_40
							end
						end
						
						if simDR_airspeed_is_mach == 0 then
							if B738DR_rest_wpt_spd_tgt > 0 then
								
								rest_wpt_spd = rest_wpt_spd_kts(B738DR_rest_wpt_spd_tgt)
								
								if B738DR_mcp_speed_dial < 100 then
									B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_descent_speed)
								else
									if B738DR_fmc_dist < B738DR_decel_dist then
										B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000, rest_wpt_spd)
									else
										B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000)
									end
								end
								if B738DR_flaps_speed == 340 then
									if B738DR_pfd_flaps_up == 340 then
										if B738DR_vnav_speed_trg < 210 then
											B738DR_vnav_speed_trg = 210
										end
									else
										if B738DR_vnav_speed_trg < B738DR_pfd_flaps_up then
											B738DR_vnav_speed_trg = B738DR_pfd_flaps_up
										end
									end
								else
									B738DR_vnav_speed_trg = B738DR_flaps_speed
								end
							else
								B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000, B738DR_flaps_speed)
							end
							if B738DR_fms_approach_speed > 0 and B738DR_approach_flaps_set == 1 then
								B738DR_vnav_speed_trg = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
							end
						else
							if B738DR_rest_wpt_spd_tgt > 0 then
								
								rest_wpt_spd = rest_wpt_spd_mach(B738DR_rest_wpt_spd_tgt)
								
								B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed_mach, rest_wpt_spd)
							else
								B738DR_vnav_speed_trg = B738DR_fmc_descent_speed_mach
							end
						end
						
						if B738DR_vnav_speed_trg == rest_wpt_spd and rest_wpt_spd ~= 0 and B738DR_vnav_speed_trg < B738DR_fmc_descent_speed then
							B738DR_fmc_descent_speed_l_act = 1
						elseif B738DR_vnav_speed_trg == spd_250_10000 and spd_250_10000 < 340 and spd_250_10000 < B738DR_fmc_descent_speed then
							B738DR_fmc_descent_speed_l_act = 2
						elseif B738DR_vnav_speed_trg == B738DR_flaps_speed and B738DR_flaps_speed < 340 then
							B738DR_fmc_descent_speed_l_act = 3
						elseif B738DR_fms_approach_speed > 0 and B738DR_approach_flaps_set == 1 and B738DR_vnav_speed_trg == B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr then
							B738DR_fmc_descent_speed_l_act = 3
						elseif B738DR_vnav_speed_trg == B738DR_pfd_flaps_up and B738DR_pfd_flaps_up < 340 then
							B738DR_fmc_descent_speed_l_act = 4
						else
							B738DR_fmc_descent_speed_l_act = 0
						end
						
					end
				end
				
				-- VNAV ALT
				if B738DR_vnav_alt_mode == 0 then
					if B738DR_lock_idle_thrust == 0 then
						if vnav_desc_spd == 0 and vnav_desc_protect_spd == 0 then
							local enable_fmc_spd = 0
							if simDR_airspeed_is_mach == 0 then
								
								if B738DR_at_wakeup ~= 0 and B738DR_ap_spd_interv_status == 0 and B738DR_vnav_desc_spd_disable == 0 then
									at_wakeup_show = 1
									B738DR_spd_wdn = B738DR_vnav_wakeup_idle	--5
									alt_x = B738DR_vnav_speed_trg + 60
									if simDR_IAS > 45 then
										const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
									end
									alt_x3 = (0.82 * (simDR_sos * 1.94384449244)) / const_spd
									if alt_x > B738DR_flaps_speed then
										alt_x = B738DR_flaps_speed
									end
									if alt_x > alt_x3 then
										alt_x = alt_x3
									end
									alt_x = alt_x - B738DR_vnav_speed_trg
									if alt_x < 0 then
										alt_x = 0
									end
									B738DR_spd_wup = alt_x
								end
								
								if B738DR_fms_descent_now == 2 then
									enable_fmc_spd = 1
								elseif simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
									enable_fmc_spd = 1
								elseif B738DR_ed_found_type ~= 0 then
									enable_fmc_spd = 1
								-- elseif B738DR_descent_mode == 2 and B738DR_ap_spd_interv_status ~= 0 then
								elseif B738DR_ap_spd_interv_status ~= 0 then
									enable_fmc_spd = 1
								elseif vnav_desc_idle ~= 3 then
									-- alt_x = B738DR_vnav_speed_trg - 6	--14		-- 5kts
									alt_x = B738DR_vnav_speed_trg - B738DR_vnav_wakeup_idle - 1
									if B738DR_flaps_speed < 340 then
										alt_x = math.max(alt_x, B738DR_flaps_speed + 5)
									end
									if B738DR_AP_airspeed < alt_x and B738DR_AP_acceleration < 0.05 then
										enable_fmc_spd = 1
									elseif B738DR_vnav_desc_spd_disable == 1 then
										enable_fmc_spd = 1
									end
								end
								
								if enable_fmc_spd ~= 0 then
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 2
										-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
										retard_enabled = 1
									end
								elseif (B738DR_AP_airspeed > B738DR_vnav_speed_trg - 2) and (simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3) 	-- - 2
								and B738DR_vnav_desc_spd_disable == 0 and retard_enabled ~= 0 then
									retard_enabled = 0
									at_mode = 7
									B738DR_eng1_N1_thrust_trg = 0.0
									B738DR_eng2_N1_thrust_trg = 0.0
									
									if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
										at_mode = 0
										if B738DR_autopilot_autothr_arm_pos == 0 then
											retard_timer = 0
										end
									elseif B738DR_autopilot_autothr_arm_pos == 1 then
										retard_timer = 3
									end
								else
									if at_mode == 7 then
										B738DR_eng1_N1_thrust_trg = 0.0
										B738DR_eng2_N1_thrust_trg = 0.0
										
										if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
											at_mode = 0
											if B738DR_autopilot_autothr_arm_pos == 0 then
												retard_timer = 0
											end
										elseif B738DR_autopilot_autothr_arm_pos == 1 then
											retard_timer = 3
										end
									elseif at_mode ~= 0 then
										if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
											at_mode = 0
										end
									end
								end
							else
								local const_spd = 1
								if B738DR_at_wakeup ~= 0 and B738DR_ap_spd_interv_status == 0 and B738DR_vnav_desc_spd_disable == 0 then
									at_wakeup_show = 1
									B738DR_spd_wdn = B738DR_vnav_wakeup_idle	--5
									
									if simDR_IAS > 45 then
										const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
									end
									alt_x2 = (B738DR_vnav_speed_trg * (simDR_sos * 1.94384449244)) / const_spd
									alt_x3 = (0.82 * (simDR_sos * 1.94384449244)) / const_spd
									
									alt_x = alt_x2 + 60
									if alt_x > B738DR_flaps_speed then
										alt_x = B738DR_flaps_speed
									end
									if alt_x > alt_x3 then
										alt_x = alt_x3
									end
									alt_x = alt_x - alt_x2
									if alt_x < 0 then
										alt_x = 0
									end
									B738DR_spd_wup = alt_x
								end
								
								if B738DR_fms_descent_now == 2 then
									enable_fmc_spd = 1
								elseif simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
									enable_fmc_spd = 1
								-- elseif B738DR_descent_mode == 2 and B738DR_ap_spd_interv_status ~= 0 then
								elseif B738DR_ap_spd_interv_status ~= 0 then
									enable_fmc_spd = 1
								elseif vnav_desc_idle ~= 3 then
									
									
									const_spd = 1
									if simDR_IAS > 45 then
										const_spd = (simDR_TAS * 1.94384449244) / simDR_IAS
									end
									alt_x = (B738DR_vnav_speed_trg * (simDR_sos * 1.94384449244)) / const_spd
									
									-- if simDR_mach_no < (B738DR_vnav_speed_trg - 0.06) then
									-- if simDR_IAS < (alt_x - 6) and B738DR_AP_acceleration < 0.05 then
									if simDR_IAS < (alt_x - B738DR_vnav_wakeup_idle - 1) and B738DR_AP_acceleration < 0.05 then
										enable_fmc_spd = 1
									elseif B738DR_vnav_desc_spd_disable == 1 then
										enable_fmc_spd = 1
									end
								end
								
								if enable_fmc_spd ~= 0 then
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 2
										-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
										retard_enabled = 1
									end
								elseif (simDR_mach_no > B738DR_vnav_speed_trg - 0.01) and (simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3)
								and B738DR_vnav_desc_spd_disable == 0 and retard_enabled ~= 0 then
									retard_enabled = 0
									at_mode = 7
									B738DR_eng1_N1_thrust_trg = 0.0
									B738DR_eng2_N1_thrust_trg = 0.0
									
									if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
										at_mode = 0
										if B738DR_autopilot_autothr_arm_pos == 0 then
											retard_timer = 0
										end
									elseif B738DR_autopilot_autothr_arm_pos == 1 then
										retard_timer = 3
									end
								else
									if at_mode == 7 then
										B738DR_eng1_N1_thrust_trg = 0.0
										B738DR_eng2_N1_thrust_trg = 0.0
										
										if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
											at_mode = 0
											if B738DR_autopilot_autothr_arm_pos == 0 then
												retard_timer = 0
											end
										elseif B738DR_autopilot_autothr_arm_pos == 1 then
											retard_timer = 3
										end
									elseif at_mode ~= 0 then
										if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
											at_mode = 0
										end
									elseif at_mode == 0 then
										if B738DR_autopilot_autothr_arm_pos == 0 then
											retard_timer = 0
										end
									end
								end
							end
						else
							-- VNAV SPD
							if B738DR_autopilot_autothr_arm_pos == 1 then
								if simDR_autopilot_altitude_mode == 5 then
									B738DR_eng1_N1_thrust_trg = 0.0
									B738DR_eng2_N1_thrust_trg = 0.0
									
									if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
										at_mode = 0
										if B738DR_autopilot_autothr_arm_pos == 0 then
											retard_timer = 0
										end
									elseif B738DR_autopilot_autothr_arm_pos == 1 then
										retard_timer = 3
									end
								elseif simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
									at_mode = 2
								end
							end
						end
					else
						if B738DR_autopilot_autothr_arm_pos == 1 then
							if vnav_desc_spd == 0 or simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
								at_mode = 2
								-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
								vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
								retard_enabled = 1
							else
								retard_enabled = 0
								B738DR_eng1_N1_thrust_trg = 0.0
								B738DR_eng2_N1_thrust_trg = 0.0
								at_mode = 7
								
								if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
									if B738DR_autopilot_autothr_arm_pos == 0 then
										retard_timer = 0
									end
								elseif B738DR_autopilot_autothr_arm_pos == 1 then
									retard_timer = 3
								end
							end
						end
					end
					B738DR_autopilot_vnav_alt_pfd = 0	-- PFD: no VNAV ALT
					if vnav_desc_spd == 0 or at_mode == 2 then
						if vnav_desc_protect_spd == 0 or at_mode == 2 then
							-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_PTH
							vnav_pfd_alt_mode = PFD_ALT_VNAV_PTH
						else
							-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_SPD
							vnav_pfd_alt_mode = PFD_ALT_VNAV_SPD
						end
					else
						-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_SPD
						vnav_pfd_alt_mode = PFD_ALT_VNAV_SPD
					end
				else
					if B738DR_autopilot_autothr_arm_pos == 1 or simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
						at_mode = 2
						-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
						vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
					end
					if B738DR_vnav_alt_disable == 0 then
						-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_ALT
						vnav_pfd_alt_mode = PFD_ALT_VNAV_ALT
					else
						-- B738DR_pfd_alt_mode = PFD_ALT_ALT_HOLD
						vnav_pfd_alt_mode = PFD_ALT_ALT_HOLD
					end
				end
				
				
				if at_mode == 0 then
					-- B738DR_pfd_spd_mode = PFD_SPD_ARM
					
					if B738DR_autopilot_autothr_arm_pos == 0 then
						retard_timer = 0
					elseif B738DR_thrust1_leveler ~= 0 or B738DR_thrust2_leveler ~= 0 then
						retard_timer = 0
					elseif retard_timer > 0 then
						retard_timer = retard_timer - SIM_PERIOD
						if retard_timer < 0 then
							retard_timer = 0
						end
					end
					if retard_timer == 0 then
						-- B738DR_pfd_spd_mode = PFD_SPD_ARM
						vnav_pfd_spd_mode = PFD_SPD_ARM
					else
						-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
						vnav_pfd_spd_mode = PFD_SPD_RETARD
					end
					
				elseif at_mode == 7 then
					if B738DR_eng1_N1_thrust_trg == 0 and B738DR_eng2_N1_thrust_trg == 0 then
						if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
							if B738DR_autopilot_autothr_arm_pos == 0 then
								retard_timer = 0
							elseif retard_timer > 0 then
								retard_timer = retard_timer - SIM_PERIOD
								if retard_timer < 0 then
									retard_timer = 0
								end
							end
							if retard_timer == 0 then
								-- B738DR_pfd_spd_mode = PFD_SPD_ARM
								vnav_pfd_spd_mode = PFD_SPD_ARM
							else
								-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
								vnav_pfd_spd_mode = PFD_SPD_RETARD
							end
						elseif B738DR_autopilot_autothr_arm_pos == 1 then
							-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
							vnav_pfd_spd_mode = PFD_SPD_RETARD
							retard_timer = 3
						else
							-- B738DR_pfd_spd_mode = PFD_SPD_ARM
							vnav_pfd_spd_mode = PFD_SPD_ARM
						end
					else
						-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
						vnav_pfd_spd_mode = PFD_SPD_RETARD
					end
				else
					-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
					vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
				end
			end
			
			local spd_lim = math.min(simDR_ground_spd, 225)
			spd_lim = math.max(spd_lim, 100)
			
			local min_climb = 100
			if simDR_altitude_pilot > 28000 then
				min_climb = 20
			end
			
			
			-------------------------------------------
			if B738DR_fdir_pitch_active == 0 then
			
			if B738DR_ap_spd_interv_status == 0 then
				
				
				B738DR_mcp_speed_dial = B738DR_vnav_speed_trg
				update_mcp_speed_dial()
				
				-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and (B738DR_flight_phase < 2 or B738DR_missed_app_act > 0) then
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and (B738DR_flight_phase < 2 or B738DR_flight_phase == 8) then
					
					-- climb VNAV SPD
					vnav_vs = 0
					init_climb = 0
					if vnav_tgt_spd ~= B738DR_mcp_speed_dial_kts then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed and vnav_tgt_spd < B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						elseif B738DR_mcp_speed_dial_kts < B738DR_AP_airspeed and vnav_tgt_spd > B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						end
					end
					vnav_tgt_spd = B738DR_mcp_speed_dial_kts
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					
					if vnav_step_init < 25 and delta_alt > vnav_step_init then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							-- accelerate
							if B738DR_speed_ratio < 0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio < 1.15 then
								if simDR_vvi_fpm_pilot > lvlchg_vvi + 100 then
									vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								elseif simDR_vvi_fpm_pilot < lvlchg_vvi then
									vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
									vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
								end
							elseif B738DR_speed_ratio > 1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio > 1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						else
							-- decelerate
							if B738DR_speed_ratio > -0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio > -1.15 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
							elseif B738DR_speed_ratio < -1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio < -1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						end
					else
						vnav_step_init = 25
					end
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if delta_alt <= vnav_step_init then
						vnav_step_init = delta_alt
						vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
						delta_alt = B738DR_mcp_speed_dial_kts
					else
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							delta_alt = B738DR_AP_airspeed + vnav_step_init
						else
							delta_alt = B738DR_AP_airspeed - vnav_step_init
						end
					end
					
					simDR_airspeed_dial_kts = delta_alt
					
				elseif simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and B738DR_flight_phase > 4 and (B738DR_descent_mode == 1 or B738DR_descent_mode == 3) then
					-- descent VNAV SPD
					vnav_vs = 0
					init_climb = 0
					if vnav_tgt_spd ~= B738DR_mcp_speed_dial_kts then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed and vnav_tgt_spd < B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						elseif B738DR_mcp_speed_dial_kts < B738DR_AP_airspeed and vnav_tgt_spd > B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						end
					end
					vnav_tgt_spd = B738DR_mcp_speed_dial_kts
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if vnav_step_init < 25 and delta_alt > vnav_step_init then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							-- accelerate
							if B738DR_speed_ratio < 0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio < 1.15 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
							elseif B738DR_speed_ratio > 1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio > 1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						else
							-- decelerate
							if B738DR_speed_ratio > -0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio > -1.15 then
								if simDR_vvi_fpm_pilot < -lvlchg_vvi_d - 100 then
									vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								elseif simDR_vvi_fpm_pilot > -lvlchg_vvi_d then
									vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
									vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
								end
							elseif B738DR_speed_ratio < -1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio < -1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						end
					else
						vnav_step_init = 25
					end
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if delta_alt <= vnav_step_init then
						vnav_step_init = delta_alt
						vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
						delta_alt = B738DR_mcp_speed_dial_kts
					else
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							delta_alt = B738DR_AP_airspeed + vnav_step_init
						else
							delta_alt = B738DR_AP_airspeed - vnav_step_init
						end
					end
					
					simDR_airspeed_dial_kts = delta_alt
					
				else
					vnav_step_init = lvlchg_in_delta
					vnav_tgt_spd = B738DR_mcp_speed_dial_kts
				end
				
			else
				--------------------
				-- ** SPD INTV ** --
				--------------------
				
				-- if B738DR_flight_phase < 2 or B738DR_missed_app_act > 0 then
				if B738DR_flight_phase < 2 or B738DR_flight_phase == 8 then
					
					
					-- climb VNAV SPD
					vnav_vs = 0
					init_climb = 0
					if vnav_tgt_spd ~= B738DR_mcp_speed_dial_kts then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed and vnav_tgt_spd < B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						elseif B738DR_mcp_speed_dial_kts < B738DR_AP_airspeed and vnav_tgt_spd > B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						end
					end
					vnav_tgt_spd = B738DR_mcp_speed_dial_kts
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if vnav_step_init < 25 and delta_alt > vnav_step_init then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							-- accelerate
							if B738DR_speed_ratio < 0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio < 1.15 then
								if simDR_vvi_fpm_pilot > lvlchg_vvi + 100 then
									vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								elseif simDR_vvi_fpm_pilot < lvlchg_vvi then
									vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
									vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
								end
							elseif B738DR_speed_ratio > 1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio > 1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						else
							-- decelerate
							if B738DR_speed_ratio > -0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio > -1.15 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
							elseif B738DR_speed_ratio < -1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio < -1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						end
					else
						vnav_step_init = 25
					end
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if delta_alt <= vnav_step_init then
						vnav_step_init = delta_alt
						vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
						delta_alt = B738DR_mcp_speed_dial_kts
					else
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							delta_alt = B738DR_AP_airspeed + vnav_step_init
						else
							delta_alt = B738DR_AP_airspeed - vnav_step_init
						end
					end
					
					simDR_airspeed_dial_kts = delta_alt
					
				elseif simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and B738DR_flight_phase > 4 and (B738DR_descent_mode == 1 or B738DR_descent_mode == 3) then
					-- descent VNAV SPD
					vnav_vs = 0
					init_climb = 0
					if vnav_tgt_spd ~= B738DR_mcp_speed_dial_kts then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed and vnav_tgt_spd < B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						elseif B738DR_mcp_speed_dial_kts < B738DR_AP_airspeed and vnav_tgt_spd > B738DR_AP_airspeed then
							vnav_step_init = lvlchg_in_delta
						end
					end
					vnav_tgt_spd = B738DR_mcp_speed_dial_kts
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if vnav_step_init < 25 and delta_alt > vnav_step_init then
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							-- accelerate
							if B738DR_speed_ratio < 0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio < 1.15 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
							elseif B738DR_speed_ratio > 1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio > 1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						else
							-- decelerate
							if B738DR_speed_ratio > -0.35 then
								vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate)
							elseif B738DR_speed_ratio > -1.15 then
								if simDR_vvi_fpm_pilot < -lvlchg_vvi_d - 100 then
									vnav_step_init = vnav_step_init + (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								elseif simDR_vvi_fpm_pilot > -lvlchg_vvi_d then
									vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
									vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
								end
							elseif B738DR_speed_ratio < -1.35 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * math.max(0.2, lvlchg_climb_rate * 0.35))
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							elseif B738DR_speed_ratio < -1.22 then
								vnav_step_init = vnav_step_init - (SIM_PERIOD * lvlchg_climb_rate * 0.35)
								vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
							end
							
						end
					else
						vnav_step_init = 25
					end
					
					delta_alt = math.abs(B738DR_AP_airspeed - B738DR_mcp_speed_dial_kts)
					if delta_alt <= vnav_step_init then
						vnav_step_init = delta_alt
						vnav_step_init = math.max(vnav_step_init, lvlchg_in_delta)
						delta_alt = B738DR_mcp_speed_dial_kts
					else
						if B738DR_mcp_speed_dial_kts > B738DR_AP_airspeed then
							delta_alt = B738DR_AP_airspeed + vnav_step_init
						else
							delta_alt = B738DR_AP_airspeed - vnav_step_init
						end
					end
					
					simDR_airspeed_dial_kts = delta_alt
					
				else
					vnav_vs = 0
					init_climb = 0
					-- if B738DR_pfd_alt_mode ~= PFD_ALT_VNAV_SPD then
					if vnav_pfd_alt_mode ~= PFD_ALT_VNAV_SPD then
						-- B738DR_pfd_spd_mode = PFD_SPD_MCP_SPD
						vnav_pfd_spd_mode = PFD_SPD_MCP_SPD
					end
					
					vnav_step_init = lvlchg_in_delta
					vnav_tgt_spd = B738DR_mcp_speed_dial_kts
				end
			end
			
			-------------------------------------------
			-- pitch act
			-------------------------------------------
			else
				
				if B738DR_ap_spd_interv_status == 0 then
					B738DR_mcp_speed_dial = B738DR_vnav_speed_trg
					update_mcp_speed_dial()
				end
				simDR_airspeed_dial = B738DR_mcp_speed_dial
				
			end
			-------------------------------------------
			
			-----------------------------------------------------------------------------------------
			
			if B738DR_autopilot_autothr_arm_pos == 0 then
				at_mode = 0
				-- B738DR_pfd_spd_mode = 0
				vnav_pfd_spd_mode = 0
				vnav_n1_dis_eng = 0
			else
				if B738DR_ap_spd_interv_status == 0 then
					-- if B738DR_flight_phase < 2 or B738DR_missed_app_act > 0 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8 then
					if B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8 then
						if at_mode == 7 then
							if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos 
							or B738DR_ap_spd_interv_status ~= ap_spd_interv_status_old then
								B738DR_autopilot_n1_status = 1
								-- B738DR_pfd_spd_mode = PFD_SPD_N1
								vnav_pfd_spd_mode = PFD_SPD_N1
								vnav_n1_dis_eng = 0
							elseif vnav_n1_dis_eng ~= 0 then
								B738DR_autopilot_n1_status = 0
								at_mode = 0
								-- B738DR_pfd_spd_mode = PFD_SPD_ARM
								vnav_pfd_spd_mode = PFD_SPD_ARM
							else
								B738DR_autopilot_n1_status = 1
								-- B738DR_pfd_spd_mode = PFD_SPD_N1
								vnav_pfd_spd_mode = PFD_SPD_N1
							end
						elseif at_mode ~= 3 and at_mode ~= 0 then
							B738DR_autopilot_n1_status = 0
							-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
							vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
							vnav_n1_dis_eng = 0
						end
					else
						B738DR_autopilot_n1_status = 0
						vnav_n1_dis_eng = 0
					end
				else
					if B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8 then
						if at_mode == 7 then
							if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos 
							or B738DR_ap_spd_interv_status ~= ap_spd_interv_status_old then
								B738DR_autopilot_n1_status = 1
								-- B738DR_pfd_spd_mode = PFD_SPD_N1
								vnav_pfd_spd_mode = PFD_SPD_N1
								vnav_n1_dis_eng = 0
							elseif vnav_n1_dis_eng ~= 0 then
								B738DR_autopilot_n1_status = 0
								at_mode = 0
								-- B738DR_pfd_spd_mode = PFD_SPD_ARM
								vnav_pfd_spd_mode = PFD_SPD_ARM
							else
								B738DR_autopilot_n1_status = 1
								-- B738DR_pfd_spd_mode = PFD_SPD_N1
								vnav_pfd_spd_mode = PFD_SPD_N1
							end
						elseif at_mode ~= 3 and at_mode ~= 0 then
							B738DR_autopilot_n1_status = 0
							-- B738DR_pfd_spd_mode = PFD_SPD_FMC_SPD
							vnav_pfd_spd_mode = PFD_SPD_FMC_SPD
							vnav_n1_dis_eng = 0
						end
					else
						B738DR_autopilot_n1_status = 0
						-- B738DR_pfd_spd_mode = PFD_SPD_MCP_SPD
						vnav_n1_dis_eng = 0
					end
					-- B738DR_autopilot_n1_status = 0
					-- B738DR_pfd_spd_mode = PFD_SPD_MCP_SPD
					-- vnav_n1_dis_eng = 0
				end
			end
			
			
			-----------------------------------------------------------------------------------------
			
		else
			vnav_vs = 0
			init_climb = 0
			vnav_step_init = lvlchg_in_delta
			vnav_tgt_spd = B738DR_mcp_speed_dial_kts
			
			vnav_n1_dis_eng = 0
			
			B738DR_ap_spd_interv_status = 0
			spd_interv_co_used = 0
			vnav_desc_spd = 0
			if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
				B738DR_descent_mode = 0
			else
				B738DR_descent_mode = 2
			end
			vnav_desc_protect_spd = 0
		end
		
		if (B738DR_vnav_alt_disable == 0 and B738DR_vnavalt_althld ~= 0) or B738DR_vnav_alt_disable ~= 0 then
			if B738DR_vnav_alt_mode ~= 0 then
				local vnavalt_to_hdgalt = 0 
				-- if B738DR_flight_phase < 2 or B738DR_missed_app_act > 0 or B738DR_flight_phase == 3 or B738DR_flight_phase > 7 then
				if B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase > 7 then
					-- if B738DR_mcp_alt_dial > simDR_ap_alt_hold_ft then
					if B738DR_mcp_alt_dial > B738DR_preselected_alt then
						vnavalt_to_hdgalt = 1
					end
				elseif B738DR_flight_phase == 2 then
					-- if B738DR_mcp_alt_dial ~= simDR_ap_alt_hold_ft then
					if B738DR_mcp_alt_dial ~= B738DR_preselected_alt then
						vnavalt_to_hdgalt = 1
					end
				else
					-- if B738DR_mcp_alt_dial < simDR_ap_alt_hold_ft then
					if B738DR_mcp_alt_dial < B738DR_preselected_alt then
						vnavalt_to_hdgalt = 1
					-- elseif auto_descent_fail ~= 0 then
						-- vnavalt_to_hdgalt = 1
					end
				end
				if vnavalt_to_hdgalt ~= 0 then
					vnav_stop()
					ap_pitch_mode = 3
					ap_pitch_mode_eng = 3
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					-- if B738DR_flight_phase == 2 then
						-- retard_enabled = 1
						-- B738DR_fms_descent_now = 3
						-- B738DR_flight_phase = 5
						-- vnav_alt_hold_act = 0
						-- mcp_alt_hold = -1
					-- end
					if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 2		-- MCP SPD
					end
				end
			end
		end
	else
		vnav_engaged = 0
		init_climb = 0
		vnav_vs = 0
		vnav_step_init = lvlchg_in_delta
		vnav_tgt_spd = B738DR_mcp_speed_dial_kts
		vnav_n1_dis_eng = 0
		
		B738DR_vnav_alt_mode = 0
		vnav_alt_hld = 0
		B738DR_ap_spd_interv_status = 0
		spd_interv_co_used = 0
		vnav_desc_spd = 0
		if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
			B738DR_descent_mode = 0
		else
			B738DR_descent_mode = 2
		end
		vnav_desc_protect_spd = 0
		B738DR_fmc_climb_speed_l = 0
		
		if B738DR_flight_phase == 5 or B738DR_flight_phase == 6 or B738DR_flight_phase == 7 then
			-- B738DR_fmc_descent_speed_l_act
			--spd_250_10000
			
			if B738DR_fmc_descent_r_speed1 ~= 0 then
				if B738DR_fmc_dist_to_dest < B738DR_FL100_dist_dest2 then
					spd_250_10000 = B738DR_fmc_descent_r_speed1
				elseif B738DR_fmc_dist_to_dest < B738DR_FL100_dist_dest then
					spd_250_10000 = B738DR_fmc_descent_r_speed1 + ((B738DR_fmc_dist_to_dest - B738DR_FL100_dist_dest2) / 0.128)		--DES_SPEED_COEF
					spd_250_10000 = math.min(spd_250_10000, B738DR_fmc_descent_speed)
					spd_250_10000 = math.floor(spd_250_10000)
				end
				if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
					-- if simDR_ap_alt_hold_ft <= B738DR_fmc_descent_r_alt1 then
					if B738DR_preselected_alt <= B738DR_fmc_descent_r_alt1 then
						spd_250_10000 = B738DR_fmc_descent_r_speed1
					end
				else
					if simDR_altitude_pilot <= B738DR_fmc_descent_r_alt1 then
						spd_250_10000 = B738DR_fmc_descent_r_speed1
					end
				end
			end
			
			-- descent speed
			-- flaps limit speeds
			B738DR_flaps_speed = 340
			if flaps == 0 then
				B738DR_flaps_speed = 340
			elseif flaps <= 0.125 then		-- flaps 1
				if B738DR_pfd_flaps_1 == 0 then
					B738DR_flaps_speed = 250
				else
					B738DR_flaps_speed = B738DR_pfd_flaps_1
				end
			elseif flaps <= 0.25 then		-- flaps 2
				if B738DR_pfd_flaps_2 == 0 then
					B738DR_flaps_speed = 230
				else
					B738DR_flaps_speed = B738DR_pfd_flaps_2
				end
			elseif flaps <= 0.375 then		-- flaps 5
				if B738DR_pfd_flaps_5 == 0 then
					B738DR_flaps_speed = 210
				else
					B738DR_flaps_speed = B738DR_pfd_flaps_5
				end
			elseif flaps <= 0.5 then		-- flaps 10
				if B738DR_pfd_flaps_10 == 0 then
					B738DR_flaps_speed = 200
				else
					B738DR_flaps_speed = B738DR_pfd_flaps_10
				end
			elseif flaps <= 0.625 then		-- flaps 15
				if B738DR_fms_vref_15 == 0 then
					if B738DR_pfd_flaps_15 == 0 then
						B738DR_flaps_speed = 190
					else
						B738DR_flaps_speed = B738DR_pfd_flaps_15
					end
				else
					B738DR_flaps_speed = B738DR_fms_vref_15
				end
			elseif flaps <= 0.75 then		-- flaps 25
				if B738DR_fms_vref_25 == 0 then
					B738DR_flaps_speed = 175
				else
					B738DR_flaps_speed = B738DR_fms_vref_25
				end
			elseif flaps <= 0.875 then		-- flaps 30
				if B738DR_fms_vref_30 == 0 then
					B738DR_flaps_speed = 165
				else
					B738DR_flaps_speed = B738DR_fms_vref_30
				end
			else		-- flaps 40
				if B738DR_fms_vref_40 == 0 then
					B738DR_flaps_speed = 155
				else
					B738DR_flaps_speed = B738DR_fms_vref_40
				end
			end
			
			if simDR_airspeed_is_mach == 0 then
				if B738DR_rest_wpt_spd_tgt > 0 then
					
					rest_wpt_spd = rest_wpt_spd_kts(B738DR_rest_wpt_spd_tgt)
					
					if B738DR_mcp_speed_dial < 100 then
						B738DR_vnav_speed_trg = math.min(spd_250_10000, rest_wpt_spd, B738DR_fmc_descent_speed)
					else
						if B738DR_fmc_dist < B738DR_decel_dist then
							B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000, rest_wpt_spd)
						else
							B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000)
						end
					end
					if B738DR_flaps_speed == 340 then
						if B738DR_pfd_flaps_up == 340 then
							if B738DR_vnav_speed_trg < 210 then
								B738DR_vnav_speed_trg = 210
							end
						else
							if B738DR_vnav_speed_trg < B738DR_pfd_flaps_up then
								B738DR_vnav_speed_trg = B738DR_pfd_flaps_up
							end
						end
					else
						B738DR_vnav_speed_trg = B738DR_flaps_speed
					end
				else
					B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed, spd_250_10000, B738DR_flaps_speed)
				end
				if B738DR_fms_approach_speed > 0 and B738DR_approach_flaps_set == 1 then
					B738DR_vnav_speed_trg = B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr
				end
			else
				if B738DR_rest_wpt_spd_tgt > 0 then
					
					rest_wpt_spd = rest_wpt_spd_mach(B738DR_rest_wpt_spd_tgt)
					
					B738DR_vnav_speed_trg = math.min(B738DR_fmc_descent_speed_mach, rest_wpt_spd)
				else
					B738DR_vnav_speed_trg = B738DR_fmc_descent_speed_mach
				end
			end
			
			if B738DR_vnav_speed_trg == rest_wpt_spd and rest_wpt_spd ~= 0 and B738DR_vnav_speed_trg < B738DR_fmc_descent_speed then
				B738DR_fmc_descent_speed_l_act = 1
			elseif B738DR_vnav_speed_trg == spd_250_10000 and spd_250_10000 < 340 and spd_250_10000 < B738DR_fmc_descent_speed then
				B738DR_fmc_descent_speed_l_act = 2
			elseif B738DR_vnav_speed_trg == B738DR_flaps_speed and B738DR_flaps_speed < 340 then
				B738DR_fmc_descent_speed_l_act = 3
			elseif B738DR_fms_approach_speed > 0 and B738DR_approach_flaps_set == 1 and B738DR_vnav_speed_trg == B738DR_fms_approach_speed + B738DR_fms_approach_wind_corr then
				B738DR_fmc_descent_speed_l_act = 3
			elseif B738DR_vnav_speed_trg == B738DR_pfd_flaps_up and B738DR_pfd_flaps_up < 340 then
				B738DR_fmc_descent_speed_l_act = 4
			else
				B738DR_fmc_descent_speed_l_act = 0
			end
			
			-- B738DR_ap_spd_interv_status
		end
	end
	
	B738DR_at_wakeup_show = at_wakeup_show
	
	ap_spd_interv_status_old = B738DR_ap_spd_interv_status

end


function idle_timer()
	vnav_desc_idle = 2
end

function B738_lvl_chg()
	
	local airspeed_dial = 0
	
	lvl_chg_pfd_spd_mode = -1
	
	if ap_pitch_mode_eng == 2 and ap_pitch_mode == 2 then
		
		local disable_lvlchg = 0
		
		if simDR_autopilot_altitude_mode ~= 5 then
			if math.abs(B738DR_AP_altitude - B738DR_mcp_alt_dial) >= 100 then
				if B738DR_AP_altitude > B738DR_mcp_alt_dial then
					B738DR_lvl_chg_mode = 0		-- descent
				else
					B738DR_lvl_chg_mode = 1		-- climb
				end
				simCMD_autopilot_lvl_chg:once()
			else
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = 0
				ap_pitch_mode = 3
				ap_pitch_mode_eng = 3
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					B738DR_preselected_alt = simDR_ap_altitude_dial_ft
					simCMD_autopilot_alt_hold:once()
				end
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 2
				end
			end
		else
			if B738DR_lvl_chg_mode == 0 then
				-- active descent
				if B738DR_AP_altitude < B738DR_mcp_alt_dial then
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					ap_pitch_mode = 3
					ap_pitch_mode_eng = 3
					altitude_dial_ft_old = B738DR_mcp_alt_dial
					if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
					if B738DR_autopilot_autothr_arm_pos ~= 0 then
						at_mode = 2
					end
					disable_lvlchg = 1
				end
			else
				-- active climb
				if B738DR_AP_altitude > B738DR_mcp_alt_dial then
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					ap_pitch_mode = 3
					ap_pitch_mode_eng = 3
					altitude_dial_ft_old = B738DR_mcp_alt_dial
					if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
					if B738DR_autopilot_autothr_arm_pos ~= 0 then
						at_mode = 2
					end
					disable_lvlchg = 1
				end
			end
		end
		
		if simDR_autopilot_altitude_mode == 5 and disable_lvlchg == 0 then
			if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 6		-- SPEED LVL CHG
					if pitch_mode_old ~= ap_pitch_mode_eng and (pitch_mode_old ~= 5 or vnav_engaged == 0) then
						if B738DR_lvl_chg_mode == 0 then
							retard_timer = 3
						else
							retard_timer = 0
						end
					else
						retard_timer = 0
					end
				end
			end
			
			if B738DR_lvl_chg_mode == 0 then	-- descent mode
				if at_mode == 6 then
					B738DR_eng1_N1_thrust_trg = 0.0
					B738DR_eng2_N1_thrust_trg = 0.0
					B738DR_autopilot_n1_status = 0
					if B738DR_thrust1_leveler == 0 and B738DR_thrust2_leveler == 0 then
						at_mode = 0
						if B738DR_autopilot_autothr_arm_pos == 0 then
							retard_timer = 0
						elseif retard_timer > 0 then
							retard_timer = retard_timer - SIM_PERIOD
							if retard_timer < 0 then
								retard_timer = 0
							end
						end
						if retard_timer == 0 then
							-- B738DR_pfd_spd_mode = PFD_SPD_ARM
							lvl_chg_pfd_spd_mode = PFD_SPD_ARM
						else
							-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
							lvl_chg_pfd_spd_mode = PFD_SPD_RETARD
						end
					else
						-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
						lvl_chg_pfd_spd_mode = PFD_SPD_RETARD
						retard_timer = 3
					end
					B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
				elseif at_mode == 0 then
					if B738DR_autopilot_autothr_arm_pos == 0 then
						retard_timer = 0
					elseif B738DR_thrust1_leveler ~= 0 or B738DR_thrust2_leveler ~= 0 then
						retard_timer = 0
					elseif retard_timer > 0 then
						retard_timer = retard_timer - SIM_PERIOD
						if retard_timer < 0 then
							retard_timer = 0
						end
					end
					if retard_timer == 0 then
						-- B738DR_pfd_spd_mode = PFD_SPD_ARM
						lvl_chg_pfd_spd_mode = PFD_SPD_ARM
					else
						-- B738DR_pfd_spd_mode = PFD_SPD_RETARD
						lvl_chg_pfd_spd_mode = PFD_SPD_RETARD
					end
				end
				if simDR_altitude_pilot < 26000 and lvl_chg_co_status == 0 and simDR_airspeed_is_mach == 1 then
					simCMD_autopilot_co:once()
					lvl_chg_co_status = 1
				end
				
			else	-- climb mode
				if at_mode == 1 or at_mode == 6 then
					B738DR_autopilot_n1_status = 1
					B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
					B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
					-- B738DR_pfd_spd_mode = PFD_SPD_N1
					lvl_chg_pfd_spd_mode = PFD_SPD_N1
				end
				if simDR_altitude_pilot > 26000 and lvl_chg_co_status == 0 and simDR_airspeed_is_mach == 0 then
					simCMD_autopilot_co:once()
					lvl_chg_co_status = 1
				end
				
			end
		end
	end
end

--lvlchgsk

function B738_alt_hld()

	local alt_x = 0
	
	if ap_pitch_mode_eng == 3 and ap_pitch_mode == 3 then
		
		if simDR_autopilot_altitude_mode ~= 6 then
			B738DR_preselected_alt = simDR_ap_altitude_dial_ft
			simCMD_autopilot_alt_hold:once()
		end
		
		if B738DR_altitude_mode ~= 3 then
			alt_hld_mcp_alt = B738DR_mcp_alt_dial
		end
		
		if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then	-- ALT HLD
			if alt_hld_mcp_alt ~= B738DR_mcp_alt_dial then
				vs_armed_enable = 1
			end
			-- if math.abs(simDR_ap_alt_hold_ft - B738DR_mcp_alt_dial) >= 100 and vs_armed_enable ~= 0 then
			if math.abs(B738DR_preselected_alt - B738DR_mcp_alt_dial) >= 100 and vs_armed_enable ~= 0 then
				-- B738DR_pfd_alt_mode_arm = PFD_ALT_VS_ARM
				-- B738DR_pfd_alt_mode_arm = PFD_ALT_GS_VS_ARM
				vs_armed = 1
				-- if B738DR_mcp_alt_dial > simDR_ap_alt_hold_ft then
				if B738DR_mcp_alt_dial > B738DR_preselected_alt then
					if simDR_ap_vvi_dial > 0 then
						-- VS engaged
						ap_pitch_mode = 1
						ap_pitch_mode_eng = 1
						simCMD_autopilot_vs:once()
						simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
						if simDR_ap_vvi_dial < 50 then
							simDR_ap_vvi_dial = 50
						end
						B738DR_ap_vvi_dial = simDR_ap_vvi_dial
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
							at_mode_eng = 0
						end
						
					elseif simDR_ap_vvi_dial < 0 then
						-- VS engaged
						ap_pitch_mode = 1
						ap_pitch_mode_eng = 1
						simCMD_autopilot_vs:once()
						simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
						if simDR_ap_vvi_dial > -50 then
							simDR_ap_vvi_dial = -50
						end
						B738DR_ap_vvi_dial = simDR_ap_vvi_dial
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
							at_mode_eng = 0
						end
					end
				-- elseif B738DR_mcp_alt_dial < simDR_ap_alt_hold_ft then
				elseif B738DR_mcp_alt_dial < B738DR_preselected_alt then
					if simDR_ap_vvi_dial < 0 then
						-- VS engaged
						ap_pitch_mode = 1
						ap_pitch_mode_eng = 1
						simCMD_autopilot_vs:once()
						simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
						if simDR_ap_vvi_dial > -50 then
							simDR_ap_vvi_dial = -50
						end
						B738DR_ap_vvi_dial = simDR_ap_vvi_dial
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
							at_mode_eng = 0
						end
					elseif simDR_ap_vvi_dial > 0 then
						-- VS engaged
						ap_pitch_mode = 1
						ap_pitch_mode_eng = 1
						simCMD_autopilot_vs:once()
						simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
						if simDR_ap_vvi_dial < 50 then
							simDR_ap_vvi_dial = 50
						end
						B738DR_ap_vvi_dial = simDR_ap_vvi_dial
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
							at_mode_eng = 0
						end
					end
				end
			else
				vs_armed = 0
			end
		else
			vs_armed = 0
			vs_armed_enable = 0
		end
		
		if ap_pitch_mode_eng ~= pitch_mode_old or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 2		-- SPEED on
			end
		end
	else
		vs_armed = 0
		vs_armed_enable = 0
		alt_hold_captured = 0
	end
end

function B738_alt_acq()

	alt_acq_disable = 0
	-- local alt_acq_status = 0
	-- local vnav_desc_rest = 0
	if ap_pitch_mode_eng == 5 then
		-- if vnav_init == 0 then
			alt_acq_disable = 1
		-- else
			-- alt_acq_disable = 1
		-- end
	elseif ap_pitch_mode_eng == 7 then		--G/P
		alt_acq_disable = 1
	elseif B738DR_autoland_status == 1 and vorloc_only ~= 0 then
		alt_acq_disable = 1
	elseif ap_pitch_mode_eng == 4 then	-- G/S
		alt_acq_disable = 1
	elseif ap_pitch_mode_eng > 7 and altitude_mode_old ~= 4 and altitude_mode_old ~= 5 then
		alt_acq_disable = 1
	elseif ap_pitch_mode_eng == 2 then	--LVL CHG
		if B738DR_lvl_chg_mode == 0 then
			-- active descent
			if B738DR_AP_altitude < mcp_alt_dial_app then
				alt_acq_disable = 1
			end
		else
			-- active climb
			if B738DR_AP_altitude > mcp_alt_dial_app then
				alt_acq_disable = 1
			end
		end
	elseif ap_pitch_mode_eng == 1 then	--VS
		if simDR_ap_vvi_dial < 0 then
			-- active descent
			if B738DR_AP_altitude < mcp_alt_dial_app then
				alt_acq_disable = 1
			end
		elseif simDR_ap_vvi_dial > 0 then
			-- active climb
			if B738DR_AP_altitude > mcp_alt_dial_app then
				alt_acq_disable = 1
			end
		end
	end
	
	-- autopilot_altitude_mode_old = simDR_autopilot_altitude_mode
	
	if alt_acq_disable == 0 and ap_pitch_mode ~= 6 then
		-- if ap_pitch_mode ~= 3 and (simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3) then
			-- if ap_pitch_mode_old ~= 0 or autopilot_cws_p_status ~= 0 then
				-- -- alt_acq_status = 1
				-- ap_pitch_mode = 6
				-- ap_pitch_mode_eng = 6
				-- altitude_dial_ft_old = B738DR_mcp_alt_dial
				-- vnav_stop()
				-- simDR_ap_vvi_dial = 0
				-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
				-- if B738DR_autopilot_autothr_arm_pos == 1 then
					-- at_mode = 2		-- SPEED on
					-- at_mode_eng = 0
				-- end
			-- end
		-- end
		
		if ap_pitch_mode ~= 3 then
			
			local ap_capture_alt = B738DR_AP_altitude + (B738DR_AP_vvi / 60 * 9)
			local alt_acq_act = false
			
			if B738DR_AP_altitude < B738DR_mcp_alt_dial then
			-- if B738DR_AP_altitude < simDR_ap_altitude_dial_ft then
				if ap_capture_alt >= B738DR_mcp_alt_dial then
					alt_acq_act = true
				end
			else
				if ap_capture_alt <= B738DR_mcp_alt_dial then
					alt_acq_act = true
				end
			end
			
			if alt_acq_act == true then
				-- B738DR_preselected_alt = simDR_ap_altitude_dial_ft
				simCMD_autopilot_alt_hold:once()
				B738DR_preselected_alt = B738DR_mcp_alt_dial
				ap_pitch_mode = 6
				ap_pitch_mode_eng = 6
				altitude_dial_ft_old = B738DR_mcp_alt_dial
				vnav_stop()
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = simDR_ap_vvi_dial
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 2		-- SPEED on
					at_mode_eng = 0
				end
			end
		end
	end
	
	if ap_pitch_mode == 6 then
		
		if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
			
			if B738DR_mcp_alt_dial ~= altitude_dial_ft_old then
				
				-- if math.abs(B738DR_AP_altitude - simDR_ap_alt_hold_ft) <= 30 then
				if math.abs(B738DR_AP_altitude - B738DR_preselected_alt) <= 30 then
					ap_pitch_mode = 3
					ap_pitch_mode_eng = 3
					vnav_stop()
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = simDR_ap_vvi_dial
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 2		-- SPEED on
						at_mode_eng = 0
					end
					-- alt_acq_status = 0
				-- elseif simDR_ap_alt_hold_ft > B738DR_AP_altitude then
				elseif B738DR_preselected_alt > B738DR_AP_altitude then
					-- VS engaged
					simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
					ap_pitch_mode = 1
					ap_pitch_mode_eng = 1
					simCMD_autopilot_vs:once()
					simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
					if simDR_ap_vvi_dial < 50 then
						simDR_ap_vvi_dial = 50
					end
					B738DR_ap_vvi_dial = simDR_ap_vvi_dial
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 2		-- SPEED on
						at_mode_eng = 0
					end
				else
					-- VS engaged
					simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
					ap_pitch_mode = 1
					ap_pitch_mode_eng = 1
					simCMD_autopilot_vs:once()
					simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
					if simDR_ap_vvi_dial > -50 then
						simDR_ap_vvi_dial = -50
					end
					B738DR_ap_vvi_dial = simDR_ap_vvi_dial
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 2		-- SPEED on
						at_mode_eng = 0
					end
				end
			-- elseif math.abs(B738DR_AP_altitude - simDR_ap_alt_hold_ft) <= 30 then
			elseif math.abs(B738DR_AP_altitude - B738DR_preselected_alt) <= 30 then
				ap_pitch_mode = 3
				ap_pitch_mode_eng = 3
				vnav_stop()
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = simDR_ap_vvi_dial
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 2		-- SPEED on
					at_mode_eng = 0
				end
				-- alt_acq_status = 0
			else
				if ap_pitch_mode_eng ~= pitch_mode_old or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 2		-- SPEED on
						at_mode_eng = 0
					end
				end
			end
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
					if B738DR_autopilot_autothr_arm_pos == 1 then
						at_mode = 2		-- SPEED on
					end
				end
			end
		else
			-- alt_acq_status = 0
			ap_pitch_mode = 0
		end
	end
	
	-- B738DR_ap_alt_acq_status = alt_acq_status
	
end


function B738_vs()

	if ap_pitch_mode_eng == 1 and ap_pitch_mode == 1 then
		
		if simDR_autopilot_altitude_mode ~= 4 then
			if simDR_ap_vvi_dial == 0 then
				simCMD_autopilot_vs:once()
			else
				simCMD_autopilot_vs_sel:once()
			end
		end
		
		simDR_ap_altitude_dial_ft = mcp_alt_dial_app
		
		if mcp_alt_vs_dial > B738DR_AP_altitude then
			if simDR_altitude_pilot > 26000 and vs_co_status == 0 and simDR_airspeed_is_mach == 0 then
				simCMD_autopilot_co:once()
				vs_co_status = 1
			end
		else
			if simDR_altitude_pilot < 26000 and vs_co_status == 0 and simDR_airspeed_is_mach == 1 then
				simCMD_autopilot_co:once()
				vs_co_status = 1
			end
		end
		
		if ap_pitch_mode_eng ~= pitch_mode_old or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 2		-- SPEED on
			end
		end
		vs_first = 1
		
		B738DR_ap_vvi_dial = simDR_ap_vvi_dial
	else
		vs_first = 0
		initial_vs_alt = 41000
		mdah_was_enable = 0
	end
end

function B738_gls_ap()
	
	local gls_alt = 0
	local gls_horz_dev = 0
	local gls_vert_dev = 0
	local gls_xtrack = 0
	local gls_crs = 0
	local gls_type = 0
	
	if autopilot_cmd_a_status == 0 and autopilot_cmd_b_status == 0 then
		if B738DR_autopilot_vhf_source_pos == -1 then
			mmr_act_mode = B738DR_mmr_cpt_act_mode
			gls_active = B738DR_gls1_active
			gls_alt = B738DR_gls1_alt
			gls_horz_dev = B738DR_gls1_horz_dev
			gls_vert_dev = B738DR_gls1_vert_dev
			gls_xtrack = B738DR_gls1_xtrack
			gls_crs = B738DR_gls1_crs
			gls_type = B738DR_gls1_type
		elseif B738DR_autopilot_vhf_source_pos == 0 then
			mmr_act_mode = B738DR_mmr_cpt_act_mode
			gls_active = B738DR_gls1_active
			gls_alt = B738DR_gls1_alt
			gls_horz_dev = B738DR_gls1_horz_dev
			gls_vert_dev = B738DR_gls1_vert_dev
			gls_xtrack = B738DR_gls1_xtrack
			gls_crs = B738DR_gls1_crs
			gls_type = B738DR_gls1_type
		else
			mmr_act_mode = B738DR_mmr_fo_act_mode
			gls_active = B738DR_gls2_active
			gls_alt = B738DR_gls2_alt
			gls_horz_dev = B738DR_gls2_horz_dev
			gls_vert_dev = B738DR_gls2_vert_dev
			gls_xtrack = B738DR_gls2_xtrack
			gls_crs = B738DR_gls2_crs
			gls_type = B738DR_gls2_type
		end
	elseif autopilot_cmd_a_status == 1 then
		if B738DR_autopilot_vhf_source_pos == -1 then
			mmr_act_mode = B738DR_mmr_cpt_act_mode
			gls_active = B738DR_gls1_active
			gls_alt = B738DR_gls1_alt
			gls_horz_dev = B738DR_gls1_horz_dev
			gls_vert_dev = B738DR_gls1_vert_dev
			gls_xtrack = B738DR_gls1_xtrack
			gls_crs = B738DR_gls1_crs
			gls_type = B738DR_gls1_type
		elseif B738DR_autopilot_vhf_source_pos == 0 then
			mmr_act_mode = B738DR_mmr_cpt_act_mode
			gls_active = B738DR_gls1_active
			gls_alt = B738DR_gls1_alt
			gls_horz_dev = B738DR_gls1_horz_dev
			gls_vert_dev = B738DR_gls1_vert_dev
			gls_xtrack = B738DR_gls1_xtrack
			gls_crs = B738DR_gls1_crs
			gls_type = B738DR_gls1_type
		else
			mmr_act_mode = B738DR_mmr_fo_act_mode
			gls_active = B738DR_gls2_active
			gls_alt = B738DR_gls2_alt
			gls_horz_dev = B738DR_gls2_horz_dev
			gls_vert_dev = B738DR_gls2_vert_dev
			gls_xtrack = B738DR_gls2_xtrack
			gls_crs = B738DR_gls2_crs
			gls_type = B738DR_gls2_type
		end
	else
		if B738DR_autopilot_vhf_source_pos == -1 then
			mmr_act_mode = B738DR_mmr_cpt_act_mode
			gls_active = B738DR_gls1_active
			gls_alt = B738DR_gls1_alt
			gls_horz_dev = B738DR_gls1_horz_dev
			gls_vert_dev = B738DR_gls1_vert_dev
			gls_xtrack = B738DR_gls1_xtrack
			gls_crs = B738DR_gls1_crs
			gls_type = B738DR_gls1_type
		elseif B738DR_autopilot_vhf_source_pos == 0 then
			mmr_act_mode = B738DR_mmr_fo_act_mode
			gls_active = B738DR_gls2_active
			gls_alt = B738DR_gls2_alt
			gls_horz_dev = B738DR_gls2_horz_dev
			gls_vert_dev = B738DR_gls2_vert_dev
			gls_xtrack = B738DR_gls2_xtrack
			gls_crs = B738DR_gls2_crs
			gls_type = B738DR_gls2_type
		else
			mmr_act_mode = B738DR_mmr_fo_act_mode
			gls_active = B738DR_gls2_active
			gls_alt = B738DR_gls2_alt
			gls_horz_dev = B738DR_gls2_horz_dev
			gls_vert_dev = B738DR_gls2_vert_dev
			gls_xtrack = B738DR_gls2_xtrack
			gls_crs = B738DR_gls2_crs
			gls_type = B738DR_gls2_type
		end
	end
	B738DR_gls_xtrack = gls_xtrack
	B738DR_gls_crs = gls_crs
	
	if mmr_act_mode ~= mmr_act_mode_old then
		if ap_roll_mode == 3 then
			-- VOR/LOC APP
			ap_roll_mode = 0
			if simDR_approach_status > 0 or B738DR_gls_app_status > 0 then
				if ap_pitch_mode == 4 then
					ap_pitch_mode = 0
				end
			end
			
		elseif ap_roll_mode == 6 then
			-- LNAV/VORLOC APP
			ap_roll_mode = 4 	--LNAV
		elseif ap_roll_mode == 17 then
			-- HDG/VORLOC APP
			ap_roll_mode = 1 	--HDG
		end
		
		B738DR_gls_app_status = 0
		B738DR_gls_nav_status = 0
		B738DR_gls_gs_status = 0
		
	end
	mmr_act_mode_old = mmr_act_mode
	
	-- GLS
	if gls_active ~= 0 and gls_type == 0 then
		--
		if B738DR_gls_nav_status == 1 and simDR_radio_height_pilot_ft > 100 then
			if math.abs(gls_horz_dev) < 2.0 then
				B738DR_gls_nav_status = 2
				if ap_roll_mode ~= 2 then
					ap_roll_mode = 2
					ap_roll_mode_eng = 2
				end
				B738DR_lnav_engaged = 0
			end
		end
		
		if B738DR_gls_app_status == 1 and simDR_radio_height_pilot_ft > 100 then
			if math.abs(gls_horz_dev) < 2.0 then
				B738DR_gls_app_status = 2
				if B738DR_fms_ils_disable == 0 then
					if ap_roll_mode ~= 3 then
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					end
				else
					if ap_roll_mode ~= 14 then
						ap_roll_mode = 14
						ap_roll_mode_eng = 14
					end
				end
				B738DR_lnav_engaged = 0
			end
		end
		
		if B738DR_gls_app_status == 2 and B738DR_gls_gs_status == 1 and B738DR_fms_ils_disable == 0 then
			if gls_vert_dev < 1.2 and gls_vert_dev > -1.2 then
				B738DR_gls_gs_status = 2
				if ap_pitch_mode ~= 4 then
					ap_pitch_mode = 4
					ap_pitch_mode_eng = 4
				end
				B738DR_retard_status = 0				-- PFD: no RETARD
				B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
				vnav_stop()
				vnav_engaged = 0
			end
			if ap_pitch_mode == 4 then
				if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
					if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
						end
					end
				end
			end
		end
		
		-- GLS NAV
		if B738DR_gls_nav_status == 2 or B738DR_gls_app_status == 2 then
			if simDR_autopilot_heading_mode ~= 1 then
				simCMD_autopilot_hdg:once()
			end
			simDR_ap_capt_heading = (B738DR_gls_corr - B738DR_ap_wca + 360) % 360
			
		end
		
		-- GLS GS
		if B738DR_gls_gs_status == 2 then
			
			local r_delta_alt = simDR_altitude_pilot + 650
			if B738DR_mcp_alt_dial > r_delta_alt then
				simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
			else
				r_delta_alt = r_delta_alt + 1000
				simDR_ap_altitude_dial_ft = roundUpToIncrement(r_delta_alt, 1000 )
			end
			
			if gls_pth_alt_old == 0 then
				gls_pth_alt_old = gls_alt
				gls_vvi_cur = simDR_vvi_fpm_pilot
			end
			local gls_pth_fpm = ((gls_alt - gls_pth_alt_old) / SIM_PERIOD) * 60		-- gls pth fpm
			local gls_alt_cor = gls_alt - (simDR_elevation_m * 3.28)
			local gls_vvi_trg = 0
			
			gls_pth_alt_old = gls_alt
			
			if gls_alt_cor > 700 then
				gls_vvi_trg = 0
			else
				if math.abs(gls_alt_cor) < 300 then
					gls_alt_cor = (gls_alt_cor / 350) * 1.57
					gls_alt_cor = math.sin(gls_alt_cor) * 1200
				else
					gls_alt_cor = math.min(gls_alt_cor, 700)
					gls_alt_cor = math.max(gls_alt_cor, -700)
					if gls_alt_cor > 0 then
						gls_alt_cor = 1170 + (gls_alt_cor - 300) * 2.2
					else
						gls_alt_cor = ((gls_alt_cor + 300) * 2.2) - 1170
					end
				end
				
				gls_vvi_trg = gls_pth_fpm + gls_alt_cor
				
				gls_vvi_trg = math.max (gls_vvi_trg, -5500)
				gls_vvi_trg = math.min (gls_vvi_trg, 0)
			end
			
			gls_vvi_cur = B738_set_animation_rate(gls_vvi_cur, gls_vvi_trg, -5500, 5500, 0.005)
			
			if simDR_autopilot_altitude_mode ~= 4 then
				simDR_ap_vvi_dial = gls_vvi_cur
				simCMD_autopilot_vs_sel:once()
			end
			-- simDR_ap_vvi_dial = gls_vvi_cur
			B738DR_ap_vvi_dial = gls_vvi_cur
			if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
				simDR_ap_vvi_dial = B738DR_ap_vvi_dial
			elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
				simDR_ap_vvi_dial = B738DR_ap_vvi_dial
			end
		else
			gls_pth_alt_old = 0
		end
	else
		gls_pth_alt_old = 0
	end
	
	-- -- LPV
	-- if gls_active ~= 0 and gls_type == 1 then
		
		-- if B738DR_lnav_engaged == 0 then
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
		-- else
			-- if B738DR_lpv_app_status == 0 and simDR_radio_height_pilot_ft > 400 then
				-- if math.abs(gls_horz_dev) < 0.7 then
					-- B738DR_lpv_app_status = 2
				-- end
			-- end
			
			-- if B738DR_lpv_app_status == 2 then
				-- simDR_ap_capt_heading = (B738DR_gls_corr - B738DR_ap_wca + 360) % 360
			-- end
		-- end
		
		-- if vnav_engaged == 0 then
			-- B738DR_lpv_gs_status = 0
			-- lpv_pth_alt_old = 0
		-- else
			-- if B738DR_lpv_app_status == 2 and B738DR_lpv_gs_status == 0 and simDR_radio_height_pilot_ft > 400 then
				-- if gls_vert_dev < 1.2 and gls_vert_dev > -0.5 then
					-- B738DR_lpv_gs_status = 2
				-- end
			-- end
			
			-- if B738DR_lpv_gs_status == 2 then
				
				-- local r_delta_alt = simDR_altitude_pilot + 650
				-- if B738DR_mcp_alt_dial > r_delta_alt then
					-- simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
				-- else
					-- r_delta_alt = r_delta_alt + 1000
					-- simDR_ap_altitude_dial_ft = roundUpToIncrement(r_delta_alt, 1000 )
				-- end
				
				-- if lpv_pth_alt_old == 0 then
					-- lpv_pth_alt_old = gls_alt
					-- lpv_vvi_cur = simDR_vvi_fpm_pilot
				-- end
				-- local lpv_pth_fpm = ((gls_alt - lpv_pth_alt_old) / SIM_PERIOD) * 60		-- gls pth fpm
				-- local lpv_alt_cor = gls_alt - (simDR_elevation_m * 3.28)
				-- local lpv_vvi_trg = 0
				
				-- lpv_pth_alt_old = gls_alt
				
				-- if lpv_alt_cor > 700 then
					-- lpv_vvi_trg = 0
				-- else
					-- if math.abs(lpv_alt_cor) < 300 then
						-- lpv_alt_cor = (lpv_alt_cor / 350) * 1.57
						-- lpv_alt_cor = math.sin(lpv_alt_cor) * 1200
					-- else
						-- lpv_alt_cor = math.min(lpv_alt_cor, 700)
						-- lpv_alt_cor = math.max(lpv_alt_cor, -700)
						-- if lpv_alt_cor > 0 then
							-- lpv_alt_cor = 1170 + (lpv_alt_cor - 300) * 2.2
						-- else
							-- lpv_alt_cor = ((lpv_alt_cor + 300) * 2.2) - 1170
						-- end
					-- end
					
					-- lpv_vvi_trg = lpv_pth_fpm + lpv_alt_cor
					
					-- lpv_vvi_trg = math.max (lpv_vvi_trg, -5500)
					-- lpv_vvi_trg = math.min (lpv_vvi_trg, 0)
				-- end
				
				-- lpv_vvi_cur = B738_set_animation_rate(lpv_vvi_cur, lpv_vvi_trg, -5500, 5500, 0.005)
				
				-- if simDR_autopilot_altitude_mode ~= 4 then
					-- simDR_ap_vvi_dial = lpv_vvi_cur
					-- simCMD_autopilot_vs_sel:once()
				-- end
				-- simDR_ap_vvi_dial = lpv_vvi_cur
				-- B738DR_ap_vvi_dial = simDR_ap_vvi_dial
				-- B738DR_pfd_alt_mode = PFD_ALT_VNAV_PTH
				-- B738DR_vnav_alt_mode = 0
			-- else
				-- lpv_pth_alt_old = 0
			-- end
		-- end
	-- else
		-- lpv_pth_alt_old = 0
		-- B738DR_lpv_app_status = 0
		-- B738DR_lpv_gs_status = 0
	-- end
	
	if mmr_act_mode < 2 or gls_type ~= 0 then
		if B738DR_gls_nav_status > 0 or B738DR_gls_app_status > 0 then
			ap_roll_mode = 0
		end
		if B738DR_gls_gs_status > 0 then
			ap_pitch_mode = 0
			-- simDR_ap_vvi_dial = roundUpToIncrement(simDR_ap_vvi_dial, 100)
			-- simDR_ap_vvi_dial = roundUpToIncrement(B738DR_ap_vvi_dial, 100)
			B738DR_ap_vvi_dial = roundUpToIncrement(B738DR_ap_vvi_dial, 100)
			if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
				simDR_ap_vvi_dial = B738DR_ap_vvi_dial
			elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
				simDR_ap_vvi_dial = B738DR_ap_vvi_dial
			end
		end
		B738DR_gls_app_status = 0
		B738DR_gls_nav_status = 0
		B738DR_gls_gs_status = 0
		
		if simDR_approach_status > 1 and simDR_glideslope_status > 1 then
			ap_app_block = 1
		elseif simDR_nav_status > 1 and vorloc_only ~= 0 then
			ap_app_block = 1
		-- elseif B738DR_fac_gp_engaged == 1 and B738DR_gp_status == 2 then
		elseif B738DR_fac_status == 2 and B738DR_gp_status == 2 then
			ap_app_block = 1
		else
			ap_app_block = 0
		end
	else
		if B738DR_gls_app_status == 0 then
			B738DR_gls_gs_status = 0
		end
		if B738DR_gls_app_status > 1 and B738DR_gls_gs_status > 1 then
			ap_app_block = 1
		-- elseif B738DR_fac_gp_engaged == 1 and B738DR_gp_status == 2 then
		elseif B738DR_fac_status == 2 and B738DR_gp_status == 2 then
			ap_app_block = 1
		else
			ap_app_block = 0
		end
		
	end
end

function B738_app()
	
	if mmr_act_mode <= 1 then
		if ap_roll_mode == 3 and ap_roll_mode_eng == 3 then
			if simDR_nav_status == 0 then
				simCMD_autopilot_lnav:once()			-- VOR LOC on
			elseif vorloc_only == 0 then
				if mmr_act_mode == 0 then	-- VOR mode
					B738DR_ap_ils_active = 0
					if simDR_glideslope_status == 2 or ap_pitch_mode == 4 then
						simCMD_disconnect:once()
						autopilot_cmd_a_status = 0
						autopilot_cmd_b_status = 0
						ap_pitch_mode = 0
						ap_roll_mode = 3
					elseif simDR_approach_status > 0 then
						simCMD_autopilot_app:once()
						simCMD_autopilot_lnav:once()
					end
				else	-- ILS mode
					B738DR_ap_ils_active = 1
					
					if simDR_nav_status == 2 and simDR_approach_status == 0 and B738DR_fms_ils_disable == 0 then
						simCMD_autopilot_app:once()
						B738DR_lnav_engaged = 0
					-- elseif simDR_approach_status == 2 and simDR_glideslope_armed == 0 and simDR_glideslope_status == 0 then
						-- simCMD_autopilot_app:once()
						-- B738DR_lnav_engaged = 0
					-- elseif simDR_approach_status == 2 and simDR_autopilot_heading_mode ~= 3 then
					-- elseif simDR_approach_status == 2 and simDR_glideslope_armed == 0 then
						-- simCMD_autopilot_app:once()
						-- B738DR_lnav_engaged = 0
					end
				end
				if ap_pitch_mode_eng ~= 4 and ap_pitch_mode_eng ~= 13 and ap_pitch_mode_eng ~= 14 then	--flare
					if simDR_approach_status == 2 then
						if simDR_glideslope_status == 2 then 	-- G/S engaged
							ap_pitch_mode = 4
						end
					end
				end
			end
			if ap_pitch_mode == 4 then
				if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
					if ap_pitch_mode_eng ~= pitch_mode_old or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
						end
					end
				end
			end
		else
			B738DR_ap_ils_active = 0
		end
	else
		B738DR_ap_ils_active = 0
	end
	
end

function B738_hdg()

	if ap_roll_mode == 1 and ap_roll_mode_eng == 1 then
		if simDR_autopilot_heading_mode ~= 1 then
			simCMD_autopilot_hdg:once()
		end
		
		local target_hdg = 0
		local relative_brg = (B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg + 360) % 360
		if relative_brg > 180 then
			relative_brg = relative_brg - 360
		end
		if relative_brg > - 7 and relative_brg < 7 then
			hdg_turn = 0
		end
		if relative_brg > 175 then
			if hdg_turn == 0 then
				hdg_turn = 1	-- right
			end
		elseif relative_brg < -175 then
			if hdg_turn == 0 then
				hdg_turn = 2	-- left
			end
		end
		if hdg_turn == 1 then
			if relative_brg < 0 then
				target_hdg = (simDR_ahars_mag_hdg + 175) % 360
			else
				target_hdg = B738DR_mcp_hdg_dial
			end
		elseif hdg_turn == 2 then
			if relative_brg > 0 then
				target_hdg = (simDR_ahars_mag_hdg - 175 + 360) % 360
			else
				target_hdg = B738DR_mcp_hdg_dial
			end
		else
			target_hdg = B738DR_mcp_hdg_dial
		end
		simDR_ap_capt_heading = target_hdg
	elseif ap_roll_mode == 10 and ap_roll_mode_eng == 10 then
		if simDR_autopilot_heading_mode ~= 1 then
			simCMD_autopilot_hdg:once()
		end
		
		local target_hdg = 0
		local relative_brg = (B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg + 360) % 360
		if relative_brg > 180 then
			relative_brg = relative_brg - 360
		end
		if relative_brg > - 7 and relative_brg < 7 then
			hdg_turn = 0
		end
		if relative_brg > 175 then
			if hdg_turn == 0 then
				hdg_turn = 1	-- right
			end
		elseif relative_brg < -175 then
			if hdg_turn == 0 then
				hdg_turn = 2	-- left
			end
		end
		if hdg_turn == 1 then
			if relative_brg < 0 then
				target_hdg = (simDR_ahars_mag_hdg + 175) % 360
			else
				target_hdg = B738DR_mcp_hdg_dial
			end
		elseif hdg_turn == 2 then
			if relative_brg > 0 then
				target_hdg = (simDR_ahars_mag_hdg - 175 + 360) % 360
			else
				target_hdg = B738DR_mcp_hdg_dial
			end
		else
			target_hdg = B738DR_mcp_hdg_dial
		end
		simDR_ap_capt_heading = target_hdg
	elseif ap_roll_mode == 16 and ap_roll_mode_eng == 16 then
		if simDR_nav_status == 2 or B738DR_gls_nav_status == 2 then
			ap_roll_mode = 2
			ap_roll_mode_eng = 2
		else
			if simDR_autopilot_heading_mode ~= 1 then
				simCMD_autopilot_hdg:once()
			end
		end
	elseif ap_roll_mode == 17 and ap_roll_mode_eng == 17 then
		if simDR_nav_status == 2 or B738DR_gls_app_status == 2 then
			ap_roll_mode = 3
			ap_roll_mode_eng = 3
		else
			if simDR_autopilot_heading_mode ~= 1 then
				simCMD_autopilot_hdg:once()
			end
			
			local target_hdg = 0
			local relative_brg = (B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg + 360) % 360
			if relative_brg > 180 then
				relative_brg = relative_brg - 360
			end
			if relative_brg > - 7 and relative_brg < 7 then
				hdg_turn = 0
			end
			if relative_brg > 175 then
				if hdg_turn == 0 then
					hdg_turn = 1	-- right
				end
			elseif relative_brg < -175 then
				if hdg_turn == 0 then
					hdg_turn = 2	-- left
				end
			end
			if hdg_turn == 1 then
				if relative_brg < 0 then
					target_hdg = (simDR_ahars_mag_hdg + 175) % 360
				else
					target_hdg = B738DR_mcp_hdg_dial
				end
			elseif hdg_turn == 2 then
				if relative_brg > 0 then
					target_hdg = (simDR_ahars_mag_hdg - 175 + 360) % 360
				else
					target_hdg = B738DR_mcp_hdg_dial
				end
			else
				target_hdg = B738DR_mcp_hdg_dial
			end
			simDR_ap_capt_heading = target_hdg
		end
	end

end

function B738_vorloc()

	if ap_roll_mode == 2 and ap_roll_mode_eng == 2 then
		-- stay in VOR LOC
		if mmr_act_mode < 2 then
			if simDR_nav_status == 0 then
				simCMD_autopilot_lnav:once()			-- VOR LOC on
			end
			B738DR_gls_nav_status = 0
			B738DR_gls_app_status = 0
			B738DR_gls_gs_status = 0
		else
			if B738DR_gls_nav_status == 0 then
				B738DR_gls_nav_status = 1
			end
			if simDR_approach_status > 0 then
				simCMD_autopilot_app:once()			-- VOR LOC on
			end
			if simDR_nav_status > 0 then
				simCMD_autopilot_lnav:once()			-- VOR LOC on
			end
		end
	end

end


function B738_fac()
	
	local wca = 0
	local wh = 0
	local tas = 0
	local awa = 0
	local relative_brg = 0
	local mag_hdg = 0
	local mag_trk = 0	-- temporary without mag variantion
	local ap_hdg = 0
	local rnp_deg = 0
	local bearing_corr = 0
	local idx_corr = 0
	local idx_dist = 0
	local relative_brg2 = 0
	local relative_brg3 = 0
	local relative_brg5 = 0
	local fix_bank_angle = 0
	local fac_capture = 0
	local idx_rnp = 0
	local pom1 = 0
	local gnd_spd = 0
	local max_idx_dist = 0
	local integral_tgt = 0
	
	if ap_roll_mode == 11 and ap_roll_mode_eng == 11 then		-- FAC APP (G/P)
		if B738DR_fac_status == 0 then
			B738DR_fac_status = 1
		elseif B738DR_fac_status == 2 then
			B738DR_lnav_engaged = 0
		end
		hold_bank_angle_enable = 0
	elseif ap_roll_mode == 7 and ap_roll_mode_eng == 7 then		-- HDG/FAC arm (G/P)
		if B738DR_fac_status == 0 then
			B738DR_fac_status = 1
		end
		hold_bank_angle_enable = 0
	elseif ap_roll_mode == 8 and ap_roll_mode_eng == 8 then		-- LNAV/FAC arm (G/P)
		if B738DR_fac_status == 0 then
			B738DR_fac_status = 1
		end
		hold_bank_angle_enable = 0
	
	else
		if B738DR_fac_status ~= 0 then
			B738DR_fac_gp_engaged = 0
			B738DR_fac_status = 0
		end
	end
	
	-- FAC status
	if B738DR_fac_status == 1 then
		mag_trk = (B738DR_fac_trk + simDR_mag_variation + 360) % 360
		relative_brg = (mag_trk - simDR_ahars_mag_hdg + 360) % 360
		if relative_brg > 180 then
			relative_brg = relative_brg - 360
		end
		if relative_brg > -90 and relative_brg < 90 then
			if B738DR_fac_xtrack > -0.4 and B738DR_fac_xtrack < 0.4 then
				fac_capture = 1
			elseif B738DR_fac_xtrack > -2.4 and B738DR_fac_xtrack < 2.4 then
				local idx_corr5_w = (B738DR_idx_corr5 - B738DR_ap_wca + 360) % 360
				relative_brg = (idx_corr5_w - simDR_ahars_mag_hdg + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				if B738DR_fac_xtrack > 0 then
					if relative_brg > 0 then
						fac_capture = 1
					end
				else
					if relative_brg < 0 then
						fac_capture = 1
					end
				end
			end
		end
		-- engage FAC
		if B738DR_pfd_gp_path == 1 and fac_capture == 1 and B738DR_nd_fac_horizontal ~= 0 then
		-- if fac_capture == 1 and B738DR_nd_fac_horizontal ~= 0 then
			if ap_roll_mode == 7 or ap_roll_mode == 8 or ap_roll_mode == 11 then
				B738DR_fac_gp_engaged = 1
			end
			ap_roll_mode = 11	-- FAC
			ap_roll_mode_eng = 11
			B738DR_fac_status = 2
			B738DR_lnav_engaged = 0
		end
	end
	
	if B738DR_fac_gp_engaged == 1 then
		
		if simDR_autopilot_heading_mode ~= 1 then
			simCMD_autopilot_hdg:once()
		end
		
		ap_hdg = B738DR_idx_corr5
		
		B738DR_idx_corr3 = 0
		
		--bank angle
		if B738DR_fms_vref ~= 0 and (B738DR_AP_airspeed <= B738DR_fms_vref - 3) and B738DR_AP_airspeed > 45 and simDR_radio_height_pilot_ft < 1500 then
			simDR_bank_angle = 3	-- bank angle protection below Vref speed
		else
			simDR_bank_angle = 5	--4
		end
		
		simDR_ap_capt_heading = (ap_hdg - B738DR_ap_wca + 360) % 360
		
	end
	
	-- show FAC dots and pointer
	if B738DR_pfd_gp_path == 1 then
		if B738DR_fac_xtrack < -2.5 then
			B738DR_fac_horizont = -2.5
		elseif B738DR_fac_xtrack > 2.5 then
			B738DR_fac_horizont = 2.5
		else
			B738DR_fac_horizont = B738DR_fac_xtrack
		end
	end
	
	-- FAC disengage
	if ap_roll_mode == 11 and B738DR_fac_gp_engaged == 1 and (B738DR_pfd_gp_path == 0 or B738DR_nd_fac_horizontal == 0) then
		if ap_on ~= 0 then
			simCMD_disconnect:once()
			autopilot_cmd_a_status = 0
			autopilot_cmd_b_status = 0
			ap_on = 0
			ap1_on = 0
			ap2_on = 0
			ap_capt_disco_status = 1
			ap_fo_disco_status = 1
		end
		ap_roll_mode = 0
		ap_pitch_mode = 0
	end
end

function B738_gp()
	local r_delta_alt = 0
	local vvi_des = 0
	
	if ap_pitch_mode_eng == 7 and ap_pitch_mode == 7 then	-- G/P -> LOC/VNAV
		if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 2
			end
		end
		
		if pitch_mode_old ~= ap_pitch_mode_eng then
			simDR_ap_vvi_dial = B738DR_AP_vvi	--simDR_vvi_fpm_pilot
		end
		-- vvi_des = vnav_vvi_limit(B738DR_gp_vvi, simDR_ap_vvi_dial, 0, 65)
		vvi_des = vnav_vvi_limit(B738DR_gp_vvi, B738DR_ap_vvi_dial, 0, 65)
		-- simDR_ap_vvi_dial = vvi_des
		B738DR_ap_vvi_dial = vvi_des
		if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
			simDR_ap_vvi_dial = B738DR_ap_vvi_dial
		elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
			simDR_ap_vvi_dial = B738DR_ap_vvi_dial
		end
		
		r_delta_alt = simDR_altitude_pilot + 650
		if B738DR_mcp_alt_dial > r_delta_alt then
			simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
		else
			simDR_ap_altitude_dial_ft = 0	-- ignored MCP alt dial
		end
		
		if simDR_autopilot_altitude_mode ~= 4 then
			simCMD_autopilot_vs_sel:once()
		end
	end
end

function B738_loc_gp()
	
	-- local r_delta_alt = 0
	
	-- LOC GP
	if ap_roll_mode == 14 and ap_roll_mode_eng == 14 then		-- LOC GP
		-- if simDR_nav_status == 0 then
			-- simCMD_autopilot_lnav:once()			-- VOR LOC on
		if simDR_nav_status == 0 and B738DR_gls_app_status == 0 then
			ap_roll_mode = 0
			B738DR_loc_gp_engaged = 0
		elseif simDR_nav_status == 1 or B738DR_gls_app_status == 1 then
			B738DR_loc_gp_engaged = 1
		else
			B738DR_loc_gp_engaged = 2
		end
	else
		B738DR_loc_gp_engaged = 0
	end
	
	-- LOC GP disengage
	if ap_roll_mode == 14 and ap_pitch_mode == 7 and B738DR_pfd_gp_path == 0 and B738DR_loc_gp_engaged == 2 then
		ap_roll_mode = 0
	end
	-- if ap_pitch_mode == 7 and B738DR_pfd_gp_path == 0 and B738DR_loc_gp_engaged == 2 then
		-- ap_pitch_mode = 0
	-- end
	
end

function B738_bellow_400ft()
	if simDR_radio_height_pilot_ft > 410 then
		bellow_400ft = 0
	end
	if simDR_radio_height_pilot_ft < 390 then
		bellow_400ft = 1
	end
end


--- CUSTOM AUTOLAND
function annun_flare()
	B738DR_single_ch_status = 0		-- SINGLE CH off
	if B738DR_autoland_install == 0 and B738DR_autoland_fail_status == 0 then
		B738DR_rollout_status = 1
		ap_roll_mode = 9
	end
	if B738DR_autoland_fail_status < 2 then
		B738DR_flare_status = 1			-- FLARE armed
		ap_pitch_mode = 13
		ap_pitch_mode_eng = 13
	end
	ils_test_ok2 = 1
end

function ils_test_off()
	ils_test_on = 0
	ils_test_ok = 1
end

function ap_at_off()

	simCMD_autothrottle_discon:once()	-- disconnect autothrotle
	autopilot_autothrottle_status = 0
	B738DR_autopilot_autothr_arm_pos = 0
	at_mode = 0
	at_on_first = 0

end



function B738_adjust_flare()

	local flare_vvi = simDR_vvi_fpm_pilot
	local flare_delta_vvi = flare_vvi - flare_vvi_old
	
	B738DR_vvi_trend = flare_vvi + (8 * flare_delta_vvi)	--22 18 16 14 8 4
	
	if yoke_off == 0 then
		if B738DR_vvi_trend > -50 then	-- -50
			yoke_off = 1
		elseif simDR_pitch_elec > 7 then
			yoke_off = 1
		end
	else
		if simDR_pitch_elec > 7 then
			yoke_off = 1
		elseif B738DR_vvi_trend < -50 and simDR_radio_height_pilot_ft < 27 then
			yoke_off = 0
		end
	end
	
	flare_vvi_old = flare_vvi

end


function B738_ap_autoland()
	local delta_vvi = 0.0
	local vvi_dial_act = 0
	local vvi_dial_trg = 0
	local ra_fdpitch = 0
	local r_delta_alt = 0
	
	if B738DR_flare_status == 1 then		-- if FLARE armed
		
		if pitch_mode_old ~= ap_pitch_mode_eng or at_on_old ~= B738DR_autopilot_autothr_arm_pos then
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 2
			end
		end
		-- if at_mode ~= 2 and B738DR_autopilot_autothr_arm_pos == 1 then
			-- at_mode = 2
		-- end
		
		if simDR_radio_height_pilot_ft < 150 then	--150 350 300
			if vorloc_only == 0 then
				if mmr_act_mode == 1 then
					simCMD_autopilot_lnav:once()
					simCMD_autopilot_lnav:once()
				elseif mmr_act_mode == 2 then
					B738DR_gls_nav_status = 2
					B738DR_gls_app_status = 0
					B738DR_gls_gs_status = 0
				end
				vorloc_only = 1
				simCMD_autopilot_vs:once()
				-- simDR_ap_vvi_dial = -simDR_ground_spd * 9.7192225 --* 1.05
				B738DR_ap_vvi_dial = -simDR_ground_spd * 9.7192225 --* 1.05
				if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
					simDR_ap_vvi_dial = B738DR_ap_vvi_dial
				elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
					simDR_ap_vvi_dial = B738DR_ap_vvi_dial
				end
				
			end
		elseif simDR_radio_height_pilot_ft < 400 and (simDR_vvi_fpm_pilot < -1000 or simDR_vvi_fpm_pilot > -500) then
			if vorloc_only == 0 then
				if mmr_act_mode == 1 then
					simCMD_autopilot_lnav:once()
					simCMD_autopilot_lnav:once()
				elseif mmr_act_mode == 2 then
					B738DR_gls_nav_status = 2
					B738DR_gls_app_status = 0
					B738DR_gls_gs_status = 0
				end
				vorloc_only = 1
				simCMD_autopilot_vs:once()
				B738DR_ap_vvi_dial = -simDR_ground_spd * 9.7192225 --* 1.05
				if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
					simDR_ap_vvi_dial = B738DR_ap_vvi_dial
				elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
					simDR_ap_vvi_dial = B738DR_ap_vvi_dial
				end
				
			end
		end
		
		if vorloc_only == 1 then
			r_delta_alt = simDR_altitude_pilot + 650
			if B738DR_mcp_alt_dial > r_delta_alt then
				simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
			else
				simDR_ap_altitude_dial_ft = 0	-- ignored MCP alt dial
			end
		end
		
		if simDR_radio_height_pilot_ft < 54 then  -- at 46 41 47 ft FLARE
		
			B738DR_flare_status = 2			-- FLARE engaged
			
			-- if B738DR_autoland_install == 0 then
				-- B738DR_rollout_status = 2		-- ROLLOUT engaged
				-- ap_roll_mode_eng = 9	--ROLLOUT armed
				-- ap_roll_mode = 9
			-- end
			
			if B738DR_autoland_install == 0 and B738DR_autoland_fail_status == 0 then
				B738DR_rollout_status = 2		-- ROLLOUT engaged
				ap_roll_mode_eng = 9	--ROLLOUT armed
				ap_roll_mode = 9
			end
			
			ap_pitch_mode = 14	-- FLARE engaged
			ap_pitch_mode_eng = 14
			
			rudder_out = simDR_heading_ratio2
			
			-- throttle = eng1_N1_thrust_cur
			-- throttle2 = eng2_N1_thrust_cur
			if B738DR_autopilot_autothr_arm_pos == 1 and at_mode ~= 0 then
				simDR_autothrottle_enable = 0
				at_mode = 6
				at_mode_eng = 6
				B738DR_eng1_N1_thrust_trg = 2
				B738DR_eng2_N1_thrust_trg = 2
				retard_timer = 3
			end
			
			fd_cur = B738DR_fdir_pitch_main
			delta_vvi = -simDR_vvi_fpm_pilot
			delta_vvi = math.max(650, delta_vvi)
			delta_vvi = math.min(750, delta_vvi)
			
			B738DR_fd_target = fd_cur + 2
			if B738DR_fd_target < 2 then
				B738DR_fd_target = 2			-- minimum FLARE pitch
			end
			if B738DR_fd_target < fd_cur then		-- minimum nose up
				B738DR_fd_target = fd_cur + 2
			end
			if B738DR_fd_target > 7 then		-- maximum nose up
				B738DR_fd_target = 7
			end
			flare_vvi_old = simDR_vvi_fpm_pilot
			if is_timer_scheduled(B738_adjust_flare) == false then
				run_at_interval(B738_adjust_flare, 0.5)	--0.3)
			end
			yoke_off = 0
			
			ra_fdpitch = math.min(2.5, B738DR_fdir_pitch_main)
			ra_fdpitch = math.max(0, ra_fdpitch)
			
			B738DR_flare_ratio = B738_rescale(0, 6.1, 2.5, 4.2, ra_fdpitch)		--6.1 / 3.8
			
			local gw_lim = math.max(40, simDR_total_weight/1000)
			gw_lim = math.min(80, gw_lim)
			
			local flare_spd = simDR_airspeed_pilot + (simDR_airspeed_pilot - simDR_ground_spd * 1.9438444)
			
			flare_ratio_yoke = B738_rescale(40, 0.10, 80, 0.20, gw_lim)	-- 0.06/0.20
			
			flare_spd = math.max(flare_spd, 125)
			flare_spd = math.min(flare_spd, 165)
			flare_ratio_yoke = flare_ratio_yoke * B738_rescale(125, 1.05, 165, 0.9, flare_spd) * B738_rescale(580, 0.85, 1000, 1.1, delta_vvi)
			flare_ratio_yoke = flare_ratio_yoke * 0.56	--0.40	--0.35 0.56 0.66 0.7
			
			B738DR_pitch_ratio2_old = simDR_pitch_ratio2
			
			vvi_trend2 = simDR_vvi_fpm_pilot
			flare_vvi_old2 = simDR_vvi_fpm_pilot
			if simDR_autopilot_altitude_mode ~= 4 then
				simCMD_autopilot_vs:once()
			end
			vvi_trend2 = math.max(vvi_trend2, -850)
			vvi_trend2 = math.min(vvi_trend2, -600)
			-- simDR_ap_vvi_dial = vvi_trend2
			B738DR_ap_vvi_dial = vvi_trend2
			if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
				simDR_ap_vvi_dial = B738DR_ap_vvi_dial
			elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
				simDR_ap_vvi_dial = B738DR_ap_vvi_dial
			end
		end
-----------------------------------------------------------------------
		
		
	end
	
	local throttle_to_retard = 0
	local control_pitch_ratio = 0
	local pitch_predict = 0
	
	if B738DR_flare_status == 2 then		-- if FLARE engaged
	
		B738DR_autopilot_alt_mode_pfd = 0	-- PFD ALT mode: blank
		
		local radio_height_al = math.max(simDR_radio_height_pilot_ft, 0)
		radio_height_al = math.min(radio_height_al, 40)
		B738DR_ap_vvi_dial = B738_rescale(0, -30, 40, vvi_trend2, radio_height_al)
		if B738DR_ap_vvi_dial <= 0 and simDR_ap_vvi_dial > 0 then
			simDR_ap_vvi_dial = B738DR_ap_vvi_dial
		elseif B738DR_ap_vvi_dial > 0 and simDR_ap_vvi_dial <= 0 then
			simDR_ap_vvi_dial = B738DR_ap_vvi_dial
		end
		
		if simDR_vvi_fpm_pilot >= 0 then
			if B738DR_thrust1_leveler ~= 0 or B738DR_thrust2_leveler ~= 0 then
				throttle_to_retard = 1
				retard_timer = 3
			end
		elseif simDR_radio_height_pilot_ft < 33 then	--27
			if B738DR_thrust1_leveler ~= 0 or B738DR_thrust2_leveler ~= 0 then
				throttle_to_retard = 1
				retard_timer = 3
			end
		end
		
		B738DR_vvi_last = simDR_vvi_fpm_pilot	-- touch down vvi
		
		if at_mode ~= 0 then
			if B738DR_autopilot_autothr_arm_pos == 1 then
				if throttle_to_retard == 1 then
					B738DR_eng1_N1_thrust_trg = 0
					B738DR_eng2_N1_thrust_trg = 0
					if B738DR_thrust1_leveler == 0 or B738DR_thrust2_leveler == 0 then
						if B738DR_autopilot_autothr_arm_pos == 0 then
							retard_timer = 0
						elseif retard_timer > 0 then
							retard_timer = retard_timer - SIM_PERIOD
							if retard_timer < 0 then
								retard_timer = 0
							end
						end
						if retard_timer == 0 then
							B738DR_pfd_spd_mode = PFD_SPD_ARM
							at_mode = 0
						else
							B738DR_pfd_spd_mode = PFD_SPD_RETARD
						end
					else
						B738DR_pfd_spd_mode = PFD_SPD_RETARD
						retard_timer = 3
					end
				else
					B738DR_eng1_N1_thrust_trg = 2
					B738DR_eng2_N1_thrust_trg = 2
					if simDR_radio_height_pilot_ft < 33 then
						if retard_timer > 0 then
							retard_timer = retard_timer - SIM_PERIOD
							if retard_timer < 0 then
								retard_timer = 0
							end
						end
						if retard_timer == 0 then
							B738DR_pfd_spd_mode = PFD_SPD_ARM
							at_mode = 0
						else
							B738DR_pfd_spd_mode = PFD_SPD_RETARD
						end
					else
						B738DR_pfd_spd_mode = PFD_SPD_MCP_SPD
					end
				end
			else
				retard_timer = 0
			end
		else
			retard_timer = 0
		end
		
	end
-----------------------------------------------------------------------
	local hdg_target = 0
	local relative_brg = 0
	local relative_brg2 = 0
	local hdg_err = 0
	local hdg_spd = 0
	local lim_rudder = 0
	
-----------------------------------------------------------------------
	if (simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 
	or simDR_on_ground[2] == 1) and B738DR_load_save ~= 4 then		-- if aircraft on ground
		yoke_off = 0
		if B738DR_autoland_status == 1 then 	-- autoland active
			-- throttle = 0
			if B738DR_rollout_status == 1 or B738DR_rollout_status == 2 then
				B738DR_rollout_status = 3
				ap_roll_mode = 19	--ROLLOUT engaged
				ap_roll_mode_eng = 19
				rudder_out = simDR_heading_ratio2
			end
			if B738DR_flare_status ~= 3 then
				if cmd_first == 0 then
					autopilot_cmd_b_status = 0
				else
					autopilot_cmd_a_status = 0
				end
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				B738DR_flare_status = 3
				vorloc_only = 0
				if simDR_autopilot_altitude_mode == 4 then
					simCMD_autopilot_vs:once()
				end
				if is_timer_scheduled(B738_adjust_flare) == true then
					stop_timer(B738_adjust_flare)
				end
				simDR_pitch_ratio2 = 0
				B738DR_pitch_ratio2_old = 0
				simDR_roll_ratio2 = 0
				roll_ratio2_old = 0
			end
			
			if at_mode ~= 0 then
				if B738DR_autopilot_autothr_arm_pos == 1 then
					if B738DR_thrust1_leveler ~= 0 or B738DR_thrust2_leveler ~= 0 then
						throttle_to_retard = 1
						retard_timer = 3
					end
					if throttle_to_retard == 1 then
						B738DR_eng1_N1_thrust_trg = 0
						B738DR_eng2_N1_thrust_trg = 0
						if B738DR_thrust1_leveler == 0 or B738DR_thrust2_leveler == 0 then
							if retard_timer > 0 then
								retard_timer = retard_timer - SIM_PERIOD
								if retard_timer < 0 then
									retard_timer = 0
								end
							end
							if retard_timer == 0 then
								B738DR_pfd_spd_mode = PFD_SPD_ARM
								at_mode = 0
							else
								B738DR_pfd_spd_mode = PFD_SPD_RETARD
							end
						else
							B738DR_pfd_spd_mode = PFD_SPD_RETARD
							retard_timer = 1
						end
					else
						B738DR_eng1_N1_thrust_trg = 2
						B738DR_eng2_N1_thrust_trg = 2
						if retard_timer > 0 then
							retard_timer = retard_timer - SIM_PERIOD
							if retard_timer < 0 then
								retard_timer = 0
							end
						end
						if retard_timer == 0 then
							B738DR_pfd_spd_mode = PFD_SPD_ARM
							at_mode = 0
						else
							B738DR_pfd_spd_mode = PFD_SPD_RETARD
						end
					end
				else
					retard_timer = 0
				end
			else
				retard_timer = 0
			end
			
			if simDR_airspeed_pilot < 50 then	--80
				B738DR_rollout_status = 0
				B738DR_flare_status = 0
				B738DR_no_autoland = 0
				B738DR_autoland_status = 0
				simDR_heading_ratio2 = 0
				if ap_roll_mode == 9 or ap_roll_mode == 19 then
					ap_roll_mode = 3	-- APP
					ap_roll_mode_eng = 3	-- APP
				end
				if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
					ap_pitch_mode = 4	-- GS
					ap_pitch_mode_eng = 4	-- GS
				end
			end
			
		end
		if aircraft_was_on_air == 1 and glare_load ~= 0 then
			aircraft_was_on_air = 0
			if at_mode ~= 4 and at_mode ~= 5 then		-- if aircraft touch down
				if is_timer_scheduled(ap_at_off) == false then
					run_after_time(ap_at_off, 3.0)
				end
			end
		end
	end
--	end
end

function fd_own()
	
	B738DR_altitude_mode = ap_pitch_mode
	B738DR_altitude_mode2 = ap_pitch_mode_eng
	B738DR_heading_mode = ap_roll_mode
	B738DR_speed_mode = at_mode
	B738DR_speed_mode2 = at_mode_eng
	
	if B738DR_load_save == 4 then
		B738DR_fdir_pitch_override = 0
		simDR_fdir_pitch_ovr = 1
		simDR_fdir_roll_ovr = 1
	else
		if B738DR_fdir_pitch_active == 0 then
			if ap_pitch_mode < 8 or ap_pitch_mode > 12 then
				if simDR_fdir_pitch_ovr ~= 0 then
					simDR_fdir_pitch_ovr = 0
				end
				B738DR_fdir_pitch_override = 0
			end
		else
			if simDR_fdir_pitch_ovr == 0 then
				simDR_fdir_pitch_ovr = 1
			end
			B738DR_fdir_pitch_override = 0
		end
		
		if B738DR_fdir_roll_active == 0 then
			simDR_fdir_roll_ovr = 0
		else
			simDR_fdir_roll_ovr = 1
		end
		
	end
end

function B738_fmc_vorloc_lnav()
	
	vorloc_lnav_enable = B738DR_vorloc_lnav_enable
	if simDR_radio_height_pilot_ft > 2000 then
		if simDR_flaps_ratio == 0 and ap_pitch_mode ~= 4 then	--and simDR_glideslope_status ~= 2 and B738DR_gls_gs_status ~= 2 then
			vorloc_lnav_enable = 0
		end
	end
	
	if B738DR_vorloc_lnav == 0 then
		lnav_ma_armed = 0
	elseif vorloc_lnav_enable ~= 0 then
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			if B738DR_fd_on == 0 and simDR_ground_spd * 1.9438444 < 80 then
				lnav_ma_armed = 0
			elseif simDR_ground_spd * 1.9438444 < 20 then
				lnav_ma_armed = 0
			end
		else
			if (B738DR_flight_phase > 4 and B738DR_flight_phase < 8) and B738DR_ap_goaround == 0 and B738DR_fd_goaround == 0 then
				lnav_ma_armed = 1
			end
		end
	else
		lnav_ma_armed = 0
	end
end

function B738_ap_system()
	
	local crs1 = math.floor(B738DR_course_pilot + 0.5)
	local crs2 = math.floor(B738DR_course_copilot + 0.5)
	local tuned_crs = (math.floor(B738DR_nav_tuned_crs + 0.5) - crs1 + 360) % 360
	if tuned_crs > 180 then
		tuned_crs = tuned_crs - 360
	end
	local tuned_crs_gls = (math.floor(B738DR_gls_crs + 0.5) - crs1 + 360) % 360
	if tuned_crs_gls > 180 then
		tuned_crs_gls = tuned_crs_gls - 360
	end
	
	if B738DR_autoland_status == 0 then
		if is_timer_scheduled(B738_adjust_flare) == true then
			stop_timer(B738_adjust_flare)
		end
	elseif B738DR_autoland_status == 1 then
		
		if ap_on == 0 then
			simCMD_disconnect:once()
			simDR_flight_dir_mode = 1
		-- F/D on
			autopilot_cmd_a_status = 0
			autopilot_cmd_b_status = 0
			B738DR_autoland_status = 0
			B738DR_flare_status = 0
			B738DR_rollout_status = 0
			B738DR_no_autoland = 0
			B738DR_retard_status = 0
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
			at_mode = 0
			vorloc_only = 0
			ils_test_enable = 0
			ils_test_on = 0
			ils_test_ok2 = 0
		end
	end
	
	local lost_ac_ap = 0
	-- if (autopilot_cmd_a_status == 1 or autopilot_cws_a_status == 1 or B738DR_autoland_status == 1) and B738DR_load_save == 0 then
	if (autopilot_cmd_a_status == 1 or autopilot_cws_a_status == 1 or B738DR_autoland_status == 1) and glare_load ~= 0 then
		if B738DR_dc_bus1_status == 0 then
			lost_ac_ap = 1
		elseif B738DR_irs_source ~= 0 and B738DR_autoland_status == 1 then
			lost_ac_ap = 1
		elseif ac_tnsbus1_status_old ~= B738DR_ac_tnsbus1_status and B738DR_ac_tnsbus1_status == 0 then
			lost_ac_ap = 1
		elseif source_off_bus1_old ~= B738DR_source_off_bus1x then
			lost_ac_ap = 1
		elseif B738DR_lost_ac_ap_a ~= 0 then
			lost_ac_ap = 1
		end
	end
	-- if B738DR_autopilot_autothr_arm_pos == 1 and B738DR_load_save == 0 then
	if B738DR_autopilot_autothr_arm_pos == 1 and glare_load ~= 0 then
		if B738DR_dc_bus1_status == 0 then
			if autothr_arm_pos == 0 then
				B738DR_autopilot_autothr_arm_pos = 0
			end
			at_mode = 0
			simCMD_autothrottle_discon:once()	-- disconnect autothrotle
			autopilot_autothrottle_status = 0
		elseif ac_tnsbus1_status_old ~= B738DR_ac_tnsbus1_status and B738DR_ac_tnsbus1_status == 0 then
			if autothr_arm_pos == 0 then
				B738DR_autopilot_autothr_arm_pos = 0
			end
			at_mode = 0
			simCMD_autothrottle_discon:once()	-- disconnect autothrotle
			autopilot_autothrottle_status = 0
		end
	end
	ac_tnsbus1_status_old = B738DR_ac_tnsbus1_status
	source_off_bus1_old = B738DR_source_off_bus1x	--simDR_gen_off_bus[0]
	
	-- if (autopilot_cmd_b_status == 1 or autopilot_cws_b_status == 1 or B738DR_autoland_status == 1) and B738DR_load_save == 0 then
	if (autopilot_cmd_b_status == 1 or autopilot_cws_b_status == 1 or B738DR_autoland_status == 1) and glare_load ~= 0 then
		if B738DR_dc_bus2_status == 0 then
			lost_ac_ap = 1
		elseif B738DR_irs_source ~= 0 and B738DR_autoland_status == 1 then
			lost_ac_ap = 1
		elseif ac_tnsbus2_status_old ~= B738DR_ac_tnsbus2_status and B738DR_ac_tnsbus2_status == 0 then
			lost_ac_ap = 1
		elseif source_off_bus2_old ~= B738DR_source_off_bus2x then
			lost_ac_ap = 1
		elseif B738DR_lost_ac_ap_b ~= 0 then
			lost_ac_ap = 1
		end
	end
	ac_tnsbus2_status_old = B738DR_ac_tnsbus2_status
	source_off_bus2_old = B738DR_source_off_bus2x
	
	if B738DR_batbus_status ~= 0 then
		first_time_baro_cpt = 0
		first_time_baro_fo = 0
	end
	
	if lost_ac_ap == 1 then
		simCMD_disconnect:once()
		simDR_flight_dir_mode = 1
		
		vnav_desc_protect_spd = 0
		autopilot_cmd_a_status = 0
		autopilot_cmd_b_status = 0
		autopilot_cws_a_status = 0
		autopilot_cws_b_status = 0
		autopilot_cws_status = 0
		autopilot_cws_r_status = 0
		autopilot_cws_p_status = 0
		B738DR_autoland_status = 0
		B738DR_flare_status = 0
		B738DR_rollout_status = 0
		B738DR_no_autoland = 0
		B738DR_retard_status = 0
		B738DR_single_ch_status = 0
		vorloc_only = 0
		ils_test_enable = 0
		ils_test_on = 0
		ils_test_ok2 = 0
		if is_timer_scheduled(annun_flare) == true then
			stop_timer(annun_flare)
		end
	end
	
	
	if mmr_act_mode == 1 then
		if B738DR_autoland_status == 1 
		and simDR_glideslope_status == 2
		and simDR_radio_height_pilot_ft < 1500 	-- 1300 ft
		and B738DR_pfd_alt_mode == PFD_ALT_GS
		and ils_test_ok == 1 then
			if B738DR_flare_status == 0 then
				if is_timer_scheduled(annun_flare) == false then
					run_after_time(annun_flare, 2.5)	--1.5
				end
			end
		end
	elseif mmr_act_mode == 2 then
		if B738DR_autoland_status == 1 
		and B738DR_gls_gs_status == 2
		and B738DR_pfd_alt_mode == PFD_ALT_GS
		and simDR_radio_height_pilot_ft < 1500 	-- 1300 ft
		and ils_test_ok == 1 then
			if B738DR_flare_status == 0 then
				if is_timer_scheduled(annun_flare) == false then
					run_after_time(annun_flare, 2.5)	--1.5
				end
			end
		end
	end
	
	if B738DR_autoland_install == 0 and B738DR_autoland_fail_status == 0 then
		if B738DR_rollout_status == 0 then
			if B738DR_flare_status > 0 then
				B738DR_rollout_status = 1
				ap_roll_mode = 9
			end
		end
	else
		B738DR_rollout_status = 0
	end
	
	if simDR_radio_height_pilot_ft > 100 
	and aircraft_was_on_air == 0 then		-- aircraft above 100 ft RA
		aircraft_was_on_air = 1
	end
	
	if ap_on == 1 then
		if B738DR_autoland_status == 1 then
			if simDR_radio_height_pilot_ft < 350 and B738DR_flare_status == 0 then
				-- disconnect ap
				simCMD_disconnect:once()
				simDR_flight_dir_mode = 1
				
				vnav_desc_protect_spd = 0
				autopilot_cmd_a_status = 0
				autopilot_cmd_b_status = 0
				autopilot_cws_a_status = 0
				autopilot_cws_b_status = 0
				autopilot_cws_status = 0
				autopilot_cws_r_status = 0
				autopilot_cws_p_status = 0
				B738DR_autoland_status = 0
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				vorloc_only = 0
				ils_test_enable = 0
				ils_test_on = 0
				ils_test_ok2 = 0
				if is_timer_scheduled(annun_flare) == true then
					stop_timer(annun_flare)
				end
			elseif simDR_radio_height_pilot_ft < 500 and B738DR_flare_status == 0 then
				-- autoland_fail
				-- no autoland
				B738DR_single_ch_status = 0			-- SINGLE CH off
				B738DR_no_autoland = 1
			elseif mmr_act_mode == 1 then
				if simDR_radio_height_pilot_ft < 1500 and simDR_approach_status == 2 and B738DR_pfd_hdg_mode == PFD_HDG_VOR_LOC
				and (simDR_nav1_freq ~= simDR_nav2_freq or crs1 ~= crs2 or B738DR_autopilot_vhf_source_pos ~= 0 or B738DR_mmr_cpt_act_mode ~= B738DR_mmr_fo_act_mode) 
				or math.abs(tuned_crs) > 6 then
					-- cancel autoland
					B738DR_autoland_status = 0
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					ils_test_enable = 0
					ils_test_on = 0
					ils_test_ok2 = 0
					if B738DR_autopilot_side == 0 then
						autopilot_cmd_b_status = 0			-- CMD B light off
					else
						autopilot_cmd_a_status = 0			-- CMD A light off
					end
					B738DR_autopilot_app_status = 1		-- APP light on
					B738DR_flare_status = 0				-- FLARE status off
					B738DR_rollout_status = 0
					B738DR_no_autoland = 0
					B738DR_single_ch_status = 1			-- SINGLE CH on
				elseif B738DR_flare_status == 0 and (simDR_approach_status == 2 or simDR_nav_status == 2) and B738DR_gp_status == 0 and B738DR_pfd_hdg_mode == PFD_HDG_VOR_LOC then
					if ils_test_ok2 == 0 then
						B738DR_single_ch_status = 1			-- SINGLE CH on
					else
						B738DR_single_ch_status = 0			-- SINGLE CH off
					end
				else
					B738DR_single_ch_status = 0			-- SINGLE CH off
				end
			else
				if simDR_radio_height_pilot_ft < 1500 and B738DR_gls_app_status == 2 and B738DR_pfd_hdg_mode == PFD_HDG_VOR_LOC
				and (B738DR_mmr_cpt_act_value ~= B738DR_mmr_fo_act_value or crs1 ~= crs2 or B738DR_autopilot_vhf_source_pos ~= 0 or B738DR_mmr_cpt_act_mode ~= B738DR_mmr_fo_act_mode) 
				or math.abs(tuned_crs_gls) > 6 then
					B738DR_autoland_status = 0
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					ils_test_enable = 0
					ils_test_on = 0
					ils_test_ok2 = 0
					if B738DR_autopilot_side == 0 then
						autopilot_cmd_b_status = 0			-- CMD B light off
					else
						autopilot_cmd_a_status = 0			-- CMD A light off
					end
					B738DR_autopilot_app_status = 1		-- APP light on
					B738DR_flare_status = 0				-- FLARE status off
					B738DR_rollout_status = 0
					B738DR_no_autoland = 0
					B738DR_single_ch_status = 1			-- SINGLE CH on
				elseif B738DR_flare_status == 0 and (B738DR_gls_app_status == 2 or B738DR_gls_nav_status == 2) and B738DR_gp_status == 0 and B738DR_pfd_hdg_mode == PFD_HDG_VOR_LOC then
					if ils_test_ok2 == 0 then
						B738DR_single_ch_status = 1			-- SINGLE CH on
					else
						B738DR_single_ch_status = 0			-- SINGLE CH off
					end
				else
					B738DR_single_ch_status = 0			-- SINGLE CH off
				end
			end
		else
			if ap_roll_mode == 3 and ap_roll_mode_eng == 3 and (simDR_nav_status == 2 or simDR_approach_status == 2) and B738DR_pfd_hdg_mode == PFD_HDG_VOR_LOC then
				B738DR_single_ch_status = 1			-- SINGLE CH on
			else
				B738DR_single_ch_status = 0			-- SINGLE CH off
			end
		end
		servo_yoke = 1
	else
		B738DR_single_ch_status = 0			-- SINGLE CH off
		servo_yoke = 0
	end
	
	
	if mmr_act_mode == 1 then
		if ils_test_enable == 1 and simDR_glideslope_status == 2 and B738DR_pfd_alt_mode == PFD_ALT_GS
		and simDR_radio_height_pilot_ft < 1480 then
			ils_test_on = 1
			if is_timer_scheduled(ils_test_off) == false then
				run_after_time(ils_test_off, 7.0)
			end
			ils_test_enable = 0
		end
	elseif mmr_act_mode == 2 then
		if ils_test_enable == 1 and B738DR_gls_gs_status == 2 and B738DR_pfd_alt_mode == PFD_ALT_GS
		and simDR_radio_height_pilot_ft < 1480 then
			ils_test_on = 1
			if is_timer_scheduled(ils_test_off) == false then
				run_after_time(ils_test_off, 7.0)
			end
			ils_test_enable = 0
		end
	end
	
	if simDR_autopilot_side == 0 or ap_on == 0 then
		B738DR_AP_airspeed = simDR_airspeed_pilot
		B738DR_AP_airspeed_mach = simDR_airspeed_mach_pilot
		B738DR_AP_altitude = simDR_altitude_pilot
		B738DR_AP_vvi = simDR_vvi_fpm_pilot
		B738DR_AP_acceleration = simDR_airspeed_accel_pilot
		
	else
		B738DR_AP_airspeed = simDR_airspeed_copilot
		B738DR_AP_airspeed_mach = simDR_airspeed_mach_copilot
		B738DR_AP_altitude = simDR_altitude_copilot
		B738DR_AP_vvi = simDR_vvi_fpm_copilot
		B738DR_AP_acceleration = simDR_airspeed_accel_copilot
	end
	
	if (B738DR_fac_status == 2 or B738DR_gls_app_status == 2 or B738DR_gls_nav_status == 2 or simDR_nav_status == 2 or simDR_approach_status == 2)
	and B738DR_gp_status ~= 0 and (B738DR_pfd_hdg_mode == PFD_HDG_FAC or B738DR_pfd_hdg_mode == PFD_HDG_VOR_LOC) then
		if simDR_radio_height_pilot_ft < 100 then
			if ap_on == 0 then
				B738DR_ap_warning = 0
				B738DR_fmod_ian_autopilot = 0
			else
				B738DR_ap_warning = blink_out + 1	--blink_out_ap
				B738DR_fmod_ian_autopilot = 1
			end
			B738DR_single_ch_g_status = 0
		else
			if ap_on == 0 then
				B738DR_single_ch_g_status = 0
			else
				B738DR_single_ch_g_status = 1
			end
			B738DR_ap_warning = 0
			B738DR_fmod_ian_autopilot = 0
		end
	else
		B738DR_single_ch_g_status = 0
		B738DR_ap_warning = 0
		B738DR_fmod_ian_autopilot = 0
	end
	
	-- if B738DR_dc_stdbus_status == 0 and B738DR_dc_bus2_status == 0 and B738DR_load_save == 0 then
	if B738DR_dc_stdbus_status == 0 and B738DR_dc_bus2_status == 0 and glare_load ~= 0 then
		radio_dh_pilot = -1		--100
		radio_dh_copilot = -1	--100
		
		baro_dh_pilot = -1001	--200
		baro_dh_copilot = -1001		--200
		
		radio_dh_pilot_mem = 100	-- -1		--100
		baro_dh_pilot_mem = 200		-- -1001	--200
		
		radio_dh_copilot_mem = 100	-- -1	--100
		baro_dh_copilot_mem = 200	-- -1001	--200
		
		if B738DR_minim_capt == 0 then	--radio DH
			B738DR_dh_pilot = -1
		else
			B738DR_dh_pilot = -1001
		end
		if B738DR_minim_fo == 0 then	--radio DH
			B738DR_dh_copilot = -1
		else
			B738DR_dh_copilot = -1001
		end
		
		B738DR_minim_capt_show = 0
		B738DR_minim_fo_show = 0
		
		B738DR_lowerDU_page = 1
		B738DR_lowerDU_page2 = 0
		
	end
end

function B738_ap_servo()
	local servo_out = 0
	local pitch_err = 0
	local pitch_err_full = 0
	local pid_input_roll = 0
	local lim_pitch = 0
	local lim_pitch2 = 0
	local spd_lim = 0
	local err_lim = 0
	local servo_change_dir = 0
	local err_fast_abs = 0
	local err_fast_abs2 = 0
	local sigma = math.max(simDR_sigma, 0.2)
	sigma = math.min(sigma, 1)
	local predict = B738_rescale(0.2, 1.4, 1, 0.6, sigma)	--1.8	--2.0	--1.8	--B738DR_predict
	
	if servo_yoke ~= servo_yoke_old then
		servo_yoke_old = servo_yoke
		if servo_yoke == 1 then
			B738DR_pitch_ratio2_old = simDR_pitch_ratio2
			roll_ratio2_old = simDR_roll_ratio2
			pitch_ratio_diff = 0
			roll_ratio_diff = 0
			servo_i_pitch = 0
			servo_ii_pitch = 0
			fdir_pitch_old = B738DR_fdir_pitch_main
			last_err_pitch = 0
			servo_k_pitch_old = 0
			pitch_ratio_trend = 0
			pitch_ratio_last_trend = 0
		end
	end
	
	B738DR_enable_servo = servo_yoke
	
	if autopilot_cws_p_status == 0 then
		mem_attitude = 0
		mem_attitude_deg = 0
	end
	
	if ap1_on == 1 then
		pid_input_roll = simDR_roll_AHARS_deg + (roll_ratio_pilot * predict)
	else
		pid_input_roll = simDR_roll_AHARS_deg_copilot + (roll_ratio_copilot * predict)
	end
	
	if B738DR_enable_servo == 1 then
		
		spd_lim = math.min(simDR_ground_spd, 225)
		spd_lim = math.max(spd_lim, 135)
		
		if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then		-- if aircraft on ground
			if ap_pitch_mode ~= 0 and autopilot_cws_p_status == 0 then
				B738DR_enable_servo_pitch = 1
			else
				servo_i_pitch = 0
				servo_ii_pitch = 0
				fdir_pitch_old = B738DR_fdir_pitch_main
				last_err_pitch = 0
				servo_k_pitch_old = 0
				pitch_ratio_trend = 0
				pitch_ratio_last_trend = 0
				B738DR_enable_servo_pitch = 0
			end
			
			
			if ap_roll_mode ~= 0 and autopilot_cws_r_status == 0 then
				servo_out = (B738DR_fdir_roll_main - pid_input_roll) * 2
				
				err_fast_abs2 = math.min(math.abs(servo_out), 12)
				err_fast_abs2 = math.max(err_fast_abs2, 4)
				
				err_lim = B738_rescale(135, 2.5, 225, 1.3, spd_lim) * 2
				
				local roll_elec_deg_pilot = math.min(math.abs(simDR_roll_elec_deg_pilot), 30)	--25
				roll_elec_deg_pilot = math.max(roll_elec_deg_pilot, 20)		--20
				if simDR_roll_elec_deg_pilot > 20 and servo_out > 0 then
					err_lim = err_lim * B738_rescale(20, 1.0, 30, 0.5, roll_elec_deg_pilot)		--25/30
				elseif simDR_roll_elec_deg_pilot < -20 and servo_out < 0 then
					err_lim = err_lim * B738_rescale(20, 1.0, 30, 0.5, roll_elec_deg_pilot)		--25/30
				end
				
				servo_out = math.min(servo_out, err_lim)	--3
				servo_out = math.max(servo_out, -err_lim)	-- -3
				
				-- B738DR_test_test5 = servo_out
				
				lim_pitch = math.abs(roll_ratio2_old)
				lim_pitch = math.min(lim_pitch, 0.8)
				lim_pitch = B738_rescale(0, 0.0018, 0.8, 0.0024, lim_pitch) * 2
				
				if (roll_ratio2_old > 0 and servo_out < 0) or (roll_ratio2_old < 0 and servo_out > 0) then
					lim_pitch = lim_pitch * 2.2	--1.6	--2.2
				elseif err_fast_abs2 > 4 then
					lim_pitch = lim_pitch * B738_rescale(4, 1.0, 12, 1.5, err_fast_abs2)
				end
				
				roll_err_accu = roll_err_accu + (servo_out * 0.01 * B738_rescale(0, 0.001, 2.5, 0, err_lim) * B738_rescale(0.2, 0.8, 1, 1, sigma))	--3
				if servo_out == 0 then
					roll_err_accu = 0
				elseif servo_out < 0 and roll_err_accu > 0 then
					roll_err_accu = 0
				elseif servo_out > 0 and roll_err_accu < 0 then
					roll_err_accu = 0
				end
				-- servo_out = servo_out + roll_err_accu
				
				lim_pitch = lim_pitch * B738_rescale(0.2, 0.8, 1, 1, sigma)
				
				servo_out = servo_out * lim_pitch * B738_rescale(135, 1.0, 225, 0.9, spd_lim) + roll_err_accu
				servo_out = servo_out * SIM_PERIOD * 20	--50
				
				servo_out = roll_ratio2_old + servo_out
				servo_out = math.min(servo_out, 0.8)
				servo_out = math.max(servo_out, -0.8)
				simDR_roll_ratio2 = servo_out
			else
				roll_err_accu = 0
			end
		
			-- TRIM
			local enable_trim = 0
			if ap_pitch_mode ~= 0 and B738DR_flare_status < 2 and autopilot_cws_p_status == 0 then
				if ap_pitch_mode >= 8 and ap_pitch_mode <= 12 then
					enable_trim = 1
				elseif simDR_radio_height_pilot_ft > 300 then
					enable_trim = 1
				elseif simDR_radio_height_pilot_ft > 100 and B738DR_autoland_status ~= 0 then
					enable_trim = 1
				end
			end
			
			if B738DR_ap_trim_pos ~= 0 then
				enable_trim = 0
			end
			
			if enable_trim == 1 then
				B738DR_enable_servo_elev_pitch = 1
			else
				B738DR_enable_servo_elev_pitch = 0
			end
		else
			B738DR_enable_servo_pitch = 0
			B738DR_enable_servo_roll = 0
			B738DR_enable_servo_elev_pitch = 0
			roll_err_accu = 0
		end
		
		-- ROLLOUT
		if B738DR_autoland_install == 0 then
			if (B738DR_rollout_status == 2 and simDR_radio_height_pilot_ft < 8) or B738DR_rollout_status == 3 then
				B738DR_enable_servo_yaw = 1
				rudder_out = B738DR_servo_yaw
				rudder_out = math.min(rudder_out, 1)
				rudder_out = math.max(rudder_out, -1)
				simDR_heading_ratio2 = rudder_out
			else
				B738DR_enable_servo_yaw = 0
			end
		else
			B738DR_enable_servo_yaw = 0
		end
	else
		B738DR_enable_servo_pitch = 0
		B738DR_enable_servo_roll = 0
		B738DR_enable_servo_yaw = 0
		B738DR_enable_servo_elev_pitch = 0
		roll_err_accu = 0
	end
	
	roll_ratio2_old = simDR_roll_ratio2
end

function B738_cws_system()
	
	local servo_out = 0
	local cws_disabled = 0
	
	if B738DR_autoland_status == 1 or B738DR_autopilot_disconnect_pos == 1 
	or simDR_altitude_pilot < 400 then
		cws_disabled = 1
	elseif B738DR_gear_handle_pos == 1 and simDR_radio_height_pilot_ft < 150 then
		cws_disabled = 1
	elseif ap_roll_mode == 2 and simDR_nav_status == 2 and (simDR_ground_spd * 1.94384449244) < 250 then
		cws_disabled = 1
	elseif ap_roll_mode == 3 and (simDR_nav_status == 2 or simDR_approach_status == 2) then
		cws_disabled = 1
	elseif B738DR_yoke_disco_ap < 2 then
		cws_disabled = 1
	elseif simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		cws_disabled = 1
	end
	
	
	if autopilot_cws_status == 0 and ap_on == 1 and B738DR_yoke_disco_ap == 2 then
		if simDR_altitude_pilot < 400 or simDR_radio_height_pilot_ft < 150 then
			if B738DR_brunner_enable ~= 0 and brunner_active ~= 0 then
				if pitch_ratio_diff > 0.22 then
					simCMD_disconnect:once()
					simDR_flight_dir_mode = 1
					
					autopilot_cmd_a_status = 0
					autopilot_cmd_b_status = 0
					ap_capt_disco_status = 1
					ap_fo_disco_status = 1
					if B738DR_autoland_status == 1 then
						if ap_roll_mode == 9 or ap_roll_mode == 19 then
							ap_roll_mode = 3	-- APP
							ap_roll_mode_eng = 3	-- APP
						end
						if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
							ap_pitch_mode = 4	-- GS
							ap_pitch_mode_eng = 4	-- GS
						end
					end
					B738DR_autoland_status = 0
					B738DR_flare_status = 0
					B738DR_rollout_status = 0
					B738DR_no_autoland = 0
					B738DR_retard_status = 0
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					vorloc_only = 0
					ap_on = 0
					ap1_on = 0
					ap2_on = 0
				end
			elseif pitch_ratio_diff > 0.11 then
				simCMD_disconnect:once()
				simDR_flight_dir_mode = 1
				
				autopilot_cmd_a_status = 0
				autopilot_cmd_b_status = 0
				ap_capt_disco_status = 1
				ap_fo_disco_status = 1
				if B738DR_autoland_status == 1 then
					if ap_roll_mode == 9 or ap_roll_mode == 19 then
						ap_roll_mode = 3	-- APP
						ap_roll_mode_eng = 3	-- APP
					end
					if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
						ap_pitch_mode = 4	-- GS
						ap_pitch_mode_eng = 4	-- GS
					end
				end
				B738DR_autoland_status = 0
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_retard_status = 0
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = 0
				vorloc_only = 0
				ap_on = 0
				ap1_on = 0
				ap2_on = 0
			end
		end
	end
	
	-- manual override AP with CWS
	if ap_on == 1 and cws_disabled == 0 then
		-- CWS with CMD
		
		if pitch_ratio_diff > 0.11 and autopilot_cws_p_status == 0 then
			
			autopilot_cws_p_status = 1
			ap_pitch_mode = 0
		end
			
		if roll_ratio_diff > 0.11 and autopilot_cws_r_status == 0 then
			
			autopilot_cws_r_status = 1
			ap_roll_mode = 0
			
		end
	end
	
end


function check_yoke()
	if servo_yoke == 1 then
		
		local diff = math.abs(simDR_pitch_ratio2 - B738DR_pitch_ratio2_old)
		if ap_pitch_mode == 0 and ap_on == 1 then
			pitch_ratio_diff = 0
		elseif diff > 0.11 then
			pitch_ratio_diff = diff
		else
			pitch_ratio_diff = 0
		end
		
		diff = math.abs(simDR_roll_ratio2 - roll_ratio2_old)
		if ap_roll_mode == 0 and ap_on == 1 then
			roll_ratio_diff = 0
		elseif diff > 0.11 then
			roll_ratio_diff = diff
		else
			roll_ratio_diff = 0
		end
		
	else
		pitch_ratio_diff = 0
		roll_ratio_diff = 0
	end
end

function B738_ian_gs_changed()
	if B738DR_ian_not_install ~= ian_install_old then
		if B738DR_ian_not_install == 1 then
			-- IAN to OFF
			if ap_roll_mode == 7 then
				--LNAV/LOC GP APP
				ap_roll_mode = 5 	--LNAV/VORLOC
			elseif ap_roll_mode == 8 then
				--LNAV/FAC GP APP
				ap_roll_mode = 4 	--LNAV
			elseif ap_roll_mode == 11 then
				--FAC GP APP
				ap_roll_mode = 0
				ap_pitch_mode = 0
			elseif ap_roll_mode == 12 then
				--HDG/LOC GP APP
				ap_roll_mode = 16 	--HDG/VORLOC
			elseif ap_roll_mode == 13 then
				--LNAV/LOC GP APP
				ap_roll_mode = 4 	--LNAV
			elseif ap_roll_mode == 14 then
				--LOC GP APP
				ap_roll_mode = 2 	--VOR/LOC
				ap_pitch_mode = 0
			end
			B738DR_fms_ils_disable = 0
		end
	end
	ian_install_old = B738DR_ian_not_install
	
	if B738DR_fms_ils_disable ~= fms_ils_disable_old then
		if B738DR_fms_ils_disable == 1 then
			-- ILS to OFF
			if ap_roll_mode == 3 then
				-- VOR/LOC APP
				if B738DR_ap_side == 0 then
					if B738DR_mmr_act_mode == 1 then
						if simDR_approach_status > 0 then
							simCMD_autopilot_app:once()
							if ap_pitch_mode == 4 then
								ap_pitch_mode = 0
							end
						end
						if simDR_nav_status == 0 then
							simCMD_autopilot_lnav:once()			-- VOR LOC on
						end
					elseif B738DR_mmr_act_mode == 2 then
						if B738DR_gls_app_status > 0 then
							if ap_pitch_mode == 4 then
								ap_pitch_mode = 0
							end
							B738DR_gls_app_status = 0
							B738DR_gls_gs_status = 0
						end
						if B738DR_gls_nav_status == 0 then
							B738DR_gls_nav_status = 1
						end
					end
				else
					if B738DR_mmr_act_mode_fo == 1 then
						if simDR_approach_status > 0 then
							simCMD_autopilot_app:once()
							if ap_pitch_mode == 4 then
								ap_pitch_mode = 0
							end
						end
						if simDR_nav_status == 0 then
							simCMD_autopilot_lnav:once()			-- VOR LOC on
						end
					elseif B738DR_mmr_act_mode_fo == 2 then
						if B738DR_gls_app_status > 0 then
							if ap_pitch_mode == 4 then
								ap_pitch_mode = 0
							end
							B738DR_gls_app_status = 0
							B738DR_gls_gs_status = 0
						end
						if B738DR_gls_nav_status == 0 then
							B738DR_gls_nav_status = 1
						end
					end
				end
			elseif ap_roll_mode == 6 then
				-- LNAV/VORLOC APP
				ap_roll_mode = 4 	--LNAV
			elseif ap_roll_mode == 17 then
				-- HDG/VORLOC APP
				ap_roll_mode = 1 	--HDG
			end
		end
	end
	fms_ils_disable_old = B738DR_fms_ils_disable
	
	local ils_gls_not_tuned = 0
	if B738DR_pfd_gp_path == 0 then
		if B738DR_ap_side == 0 then
			if B738DR_mmr_act_mode == 0 then
				-- not ILS/GLS
				ils_gls_not_tuned = 1
			end
		else
			if B738DR_mmr_act_mode_fo == 0 then
				-- not ILS/GLS
				ils_gls_not_tuned = 1
			end
		end
	end
	if ils_gls_not_tuned_old ~= ils_gls_not_tuned and ils_gls_not_tuned ~= 0 then
		if ap_roll_mode == 3 then
			-- VOR/LOC APP
			ap_roll_mode = 0
			if simDR_approach_status > 0 or B738DR_gls_app_status > 0 then
				if ap_pitch_mode == 4 then
					ap_pitch_mode = 0
				end
			end
			
		elseif ap_roll_mode == 6 then
			-- LNAV/VORLOC APP
			ap_roll_mode = 4 	--LNAV
		elseif ap_roll_mode == 17 then
			-- HDG/VORLOC APP
			ap_roll_mode = 1 	--HDG
		end
	end
	ils_gls_not_tuned_old = ils_gls_not_tuned
end

function B738_mcp_dial()
	
	local mcp_alt_dial2 = B738DR_mcp_alt_dial
	local temp_DR = simDR_altitude_pilot
	mcp_alt_dial2 = math.min(mcp_alt_dial2, temp_DR + 400)
	mcp_alt_dial2 = math.max(mcp_alt_dial2, temp_DR - 400)
	B738DR_mcp_alt_dial2 = mcp_alt_dial2
	
	local mcp_speed_dial_kts2 = B738DR_mcp_speed_dial_kts
	temp_DR = simDR_airspeed_pilot
	mcp_speed_dial_kts2 = math.max(mcp_speed_dial_kts2, temp_DR - 59)
	if temp_DR < 45 then
		mcp_speed_dial_kts2 = math.min(mcp_speed_dial_kts2, 104)
	else
		mcp_speed_dial_kts2 = math.min(mcp_speed_dial_kts2, temp_DR + 59)
	end
	B738DR_mcp_speed_dial_kts2 = mcp_speed_dial_kts2
	
	local mcp_alt_dial2_fo = B738DR_mcp_alt_dial
	temp_DR = simDR_altitude_copilot
	mcp_alt_dial2_fo = math.min(mcp_alt_dial2_fo, temp_DR + 400)
	mcp_alt_dial2_fo = math.max(mcp_alt_dial2_fo, temp_DR - 400)
	B738DR_mcp_alt_dial2_fo = mcp_alt_dial2_fo
	
	local mcp_speed_dial_kts2_fo = B738DR_mcp_speed_dial_kts
	temp_DR = simDR_airspeed_copilot
	mcp_speed_dial_kts2_fo = math.max(mcp_speed_dial_kts2_fo, temp_DR - 59)
	if temp_DR < 45 then
		mcp_speed_dial_kts2_fo = math.min(mcp_speed_dial_kts2_fo, 104)
	else
		mcp_speed_dial_kts2_fo = math.min(mcp_speed_dial_kts2_fo, temp_DR + 59)
	end
	B738DR_mcp_speed_dial_kts2_fo = mcp_speed_dial_kts2_fo
	
end

function vnav_stop()
	
	B738DR_autopilot_vnav_alt_pfd = 0	-- PFD: VNAV ALT
	B738DR_autopilot_vnav_pth_pfd = 0	-- PFD: VNAV PTH
	B738DR_autopilot_vnav_spd_pfd = 0	-- PFD: VNAV SPD
	
	B738DR_ap_spd_interv_status = 0
	spd_interv_co_used = 0
	vnav_desc_spd = 0
	if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
		B738DR_descent_mode = 0
	else
		B738DR_descent_mode = 2
	end
	vnav_desc_protect_spd = 0
	vnav_engaged = 0
	vnav_desc_spd = 0
	vnav_desc_protect_spd = 0
	
	vnav_init = 0
	vnav_init2 = 1
	
	if ap_pitch_mode > 7 then
		simDR_ap_vvi_dial = 0
		B738DR_ap_vvi_dial = 0
	else
		if ap_pitch_mode == 1 then
			-- simDR_ap_vvi_dial = roundDownToIncrement(simDR_ap_vvi_dial, 100)
			simDR_ap_vvi_dial = roundDownToIncrement(B738DR_ap_vvi_dial, 100)
			B738DR_ap_vvi_dial = simDR_ap_vvi_dial
		elseif ap_pitch_mode_eng == 5 then
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
		end
		
		if B738DR_autopilot_autothr_arm_pos == 1 then
			if at_mode ~= 2 and B738DR_fd_goaround == 0 and B738DR_ap_goaround == 0 then
				if B738DR_autopilot_n1_status == 1 then
					at_mode = 1
				else
					at_mode = 0
				end
			end
		end
	end
	simDR_ap_altitude_dial_ft = B738DR_mcp_alt_dial
	B738DR_alt_hold_mem = 0
	
end

function B738_cmd_revert()
	if ap_on == 1 then
		autopilot_cws_r_status = 0
		autopilot_cws_p_status = 0
		if ap_roll_mode == 0 then
			autopilot_cws_r_status = 1
		end
		if ap_pitch_mode == 0 then
			autopilot_cws_p_status = 1
		end
	elseif autopilot_cws_status ~= 0 then
		autopilot_cws_r_status = 1
		autopilot_cws_p_status = 1
	else
		autopilot_cws_r_status = 0
		autopilot_cws_p_status = 0
	end
end

function B738_ap_logic()

	local delta_alt = 0
	local altitude_dial = 0
	local airspeed_dial = 0
	local alt_x = 0
	local thrust = 0
	local allign_ok = 0
	
	if B738DR_load_save == 4 then
		loaded_flight_timer = 5	--3
	elseif loaded_flight_timer > 0 then
		if SIM_PERIOD < 0.1 then
			loaded_flight_timer = loaded_flight_timer - SIM_PERIOD
		end
		if loaded_flight_timer < 0 then
			loaded_flight_timer = 0
		end
	end
	
	if simDR_flight_dir_mode == 0 then
		autopilot_cmd_a_status = 0
		autopilot_cmd_b_status = 0
		autopilot_cws_a_status = 0
		autopilot_cws_b_status = 0
		autopilot_cws_status = 0
		autopilot_cws_r_status = 0
		autopilot_cws_p_status = 0
		simDR_flight_dir_mode = 1
		simDR_airspeed_dial = mem_airspeed_dial
		-- new mcp
		B738DR_mcp_speed_dial = simDR_airspeed_dial
		update_mcp_speed_dial()
		
	end
	mem_airspeed_dial = simDR_airspeed_dial
	mem_speed_mode = simDR_autothrottle_status
	
	if simDR_ap_vvi_dial == 0 then
		B738DR_vvi_dial_show = 0
	elseif ap_pitch_mode == 1 then
		B738DR_vvi_dial_show = 1
	else
		B738DR_vvi_dial_show = 0
	end
	
	if B738DR_autoland_status == 1 then
		ap_on = 1
		ap1_on = 1
		ap2_on = 1
	elseif autopilot_cmd_a_status == 1 then
		ap_on = 1
		ap1_on = 1
		ap2_on = 0
	elseif autopilot_cmd_b_status == 1 then
		ap_on = 1
		ap1_on = 0
		ap2_on = 1
	else
		ap_on = 0
		ap1_on = 0
		ap2_on = 0
		cmd_first = 0
		ils_test_enable = 0
		ils_test_on = 0
		ils_test_ok = 0
		ils_test_ok2 = 0
	end
	
	if B738DR_ap_trim_pos ~= 0 and ap_on ~= 0 then
		-- disconnect A/P
		simCMD_disconnect:once()
		simDR_flight_dir_mode = 1
		
		autopilot_cmd_a_status = 0
		autopilot_cmd_b_status = 0
		ap_capt_disco_status = 1
		ap_fo_disco_status = 1
		
		if B738DR_autoland_status == 1 then
			if ap_roll_mode == 9 or ap_roll_mode == 19 then
				ap_roll_mode = 3	-- APP
				ap_roll_mode_eng = 3	-- APP
			end
			if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
				ap_pitch_mode = 4	-- GS
				ap_pitch_mode_eng = 4	-- GS
			end
		end
		B738DR_autoland_status = 0
		B738DR_flare_status = 0
		B738DR_rollout_status = 0
		B738DR_no_autoland = 0
		B738DR_retard_status = 0
		simDR_ap_vvi_dial = 0
		B738DR_ap_vvi_dial = 0
		vorloc_only = 0
		ap_on = 0
		ap1_on = 0
		ap2_on = 0
	end
	
	local disable_mcp_alt = 0
	if ap_on ~= 0 then
		--if B738DR_autopilot_vnav_status == 1 or ap_pitch_mode_eng == 7 then
		if ap_pitch_mode == 5 or ap_pitch_mode_eng == 7 then
			disable_mcp_alt = 1
		end
	end
	if ap_pitch_mode == 3 and ap_pitch_mode_eng ~= 3 then
		disable_mcp_alt = 1
	elseif B738DR_gls_gs_status == 2 then
		disable_mcp_alt = 1
	end
	if disable_mcp_alt == 0 then
		simDR_ap_altitude_dial_ft = mcp_alt_dial_app
	end
	
	local update_airspeed_dial = 0
	if B738DR_ap_spd_interv_status == 0 and vnav_engaged == 1 then
		-- if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and (B738DR_flight_phase < 2 or B738DR_missed_app_act > 0) then
		if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 and (B738DR_flight_phase < 2 or B738DR_flight_phase == 8) then
			update_airspeed_dial = 1
		end
	end
	if ap_pitch_mode == 8 or ap_pitch_mode == 11 or B738DR_at_takeoff ~= 0 then
		-- Takeoff / LVL CHG
		update_airspeed_dial = 1
		if to_ga_mcp_changed == 0 then
			if simDR_airspeed_pilot < 60 then
				if simDR_airspeed_is_mach == 0 then
					mcp_speed_dial_mem = B738DR_mcp_speed_dial
				else
					mcp_speed_dial_mem = B738DR_mcp_speed_dial_kts
				end
			elseif mcp_speed_dial_mem ~= B738DR_mcp_speed_dial then
				to_ga_mcp_changed = 1
			end
		end
	else
		if vnav_engaged == 0 then
			to_ga_mcp_changed = 0
			if simDR_airspeed_is_mach == 0 then
				mcp_speed_dial_mem = B738DR_mcp_speed_dial
			else
				mcp_speed_dial_mem = B738DR_mcp_speed_dial_kts
			end
		end
	end
	
	if update_airspeed_dial == 0 then
		simDR_airspeed_dial = B738DR_mcp_speed_dial
		update_mcp_speed_dial()
	end
	
	if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
		if simDR_autopilot_altitude_mode ~= altitude_mode_old then
			B738DR_alt_hold_mem = simDR_ap_altitude_dial_ft
		end
	else
		B738DR_alt_hold_mem = 0
	end
	
	if loaded_flight_timer == 0 then
		if B738DR_irs_left_mode == 2 or B738DR_irs_right_mode == 2 then
			allign_ok = 2
		elseif B738DR_irs_left_mode == 3 or B738DR_irs_right_mode == 3 then
			allign_ok = 1
		end
		if allign_ok == 0 then
			ap_roll_mode = 0
			ap_pitch_mode = 0
			
			-- IRS off
			if ap_on ~= 0 then
				simDR_flight_dir_mode = 1
				ap_on = 0
				ap1_on = 0
				ap2_on = 0
				
				autopilot_cws_a_status = 0
				autopilot_cws_b_status = 0
				autopilot_cmd_b_status = 0
				autopilot_cmd_a_status = 0
				autopilot_cws_status = 0
				autopilot_cws_r_status = 0
				autopilot_cws_p_status = 0
				B738DR_flare_status = 0
				B738DR_rollout_status = 0
				B738DR_no_autoland = 0
				B738DR_autoland_status = 0
				B738DR_retard_status = 0
				B738DR_single_ch_status = 0
				B738DR_fmc_descent_now = 0
				B738DR_fmc_mode = 0
				B738DR_autopilot_vnav_alt_pfd = 0
				B738DR_autopilot_vnav_pth_pfd = 0
				B738DR_autopilot_vnav_spd_pfd = 0
				B738DR_autopilot_fmc_spd_pfd = 0
				B738DR_autopilot_to_ga_pfd = 0
				B738DR_autopilot_thr_hld_pfd = 0
				B738DR_autopilot_ga_pfd = 0
				B738DR_autopilot_alt_acq_pfd = 0
				ap_roll_mode = 0
				ap_pitch_mode = 0
				B738DR_ap_spd_interv_status = 0
				spd_interv_co_used = 0
				vnav_desc_spd = 0
				if B738DR_fmc_descent_speed == B738DR_fmc_descent_speed_init and B738DR_fmc_descent_speed_mach == B738DR_fmc_descent_speed_mach_init then
					B738DR_descent_mode = 0
				else
					B738DR_descent_mode = 2
				end
				vnav_desc_protect_spd = 0
				ils_test_enable = 0
				if ap_on == 1 then
					ap_disco_do = 1
				end
				B738DR_enable_servo_elev_pitch = 0
			end
		elseif allign_ok == 1 then
			-- LNAV
			if (ap_roll_mode > 3 and ap_roll_mode < 7) or ap_roll_mode == 8 or ap_roll_mode == 13 then
				ap_roll_mode = 0
			end
			-- VNAV
			if ap_pitch_mode == 5 then
				ap_pitch_mode = 0
			end
		end
	end
	
	if B738DR_vnav_disconnect == 1 then
		B738DR_vnav_disconnect = 0
		if ap_pitch_mode == 5 then
			if B738DR_vnav_alt_mode == 0 then
				ap_pitch_mode = 0
			else
				ap_pitch_mode = 3
			end
		end
	elseif B738DR_vnav_disconnect == 2 then
		B738DR_vnav_disconnect = 0
		if ap_pitch_mode == 5 then
			-- ap_pitch_mode = 0
			-- ap_pitch_mode_eng = 0
			-- vnav_stop()
			if B738DR_vnav_alt_mode == 0 then
				ap_pitch_mode = 0
				ap_pitch_mode_eng = 0
				vnav_stop()
			else
				ap_pitch_mode = 3
			end
		end
	end
	if B738DR_lnav_disconnect == 1 then
		B738DR_lnav_disconnect = 0
		if (ap_roll_mode > 3 and ap_roll_mode < 7) or ap_roll_mode == 8 or ap_roll_mode == 13 then
			ap_roll_mode = 0
		end
		B738DR_lnav_engaged = 0
	end
	
	roll_mode_old = ap_roll_mode_eng
	pitch_mode_old = ap_pitch_mode_eng
	
	local vorloc_was_eng = 0
	if ap_roll_mode ~= ap_roll_mode_eng then
		
		-- ROLL MODES: 0-off, 1-HDG SEL, 2-VOR/LOC, 3-APP, 4-LNAV
		if ap_roll_mode == 0 then
			ap_roll_mode_eng = 0
			if simDR_approach_status > 0 then
				simCMD_autopilot_app:once()
				if ap_pitch_mode == 4 then
					ap_pitch_mode = 0
				end
			end
			if simDR_nav_status > 0 then
				simCMD_autopilot_lnav:once()
			end
			if B738DR_gls_nav_status > 0 or B738DR_gls_app_status > 0 or B738DR_gls_gs_status > 0 then
				B738DR_gls_app_status = 0
				B738DR_gls_nav_status = 0
				B738DR_gls_gs_status = 0
				if ap_pitch_mode == 4 then
					ap_pitch_mode = 0
				end
			end
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			if simDR_autopilot_heading_mode == 1 then
				simCMD_autopilot_hdg:once()		-- HDG off
			end
			autopilot_fms_nav_status = 0
			B738DR_gp_status = 0
			B738DR_lnav_engaged = 0
			
		-- HDG mode
		elseif ap_roll_mode == 1 then
			if simDR_approach_status > 0 then
				if simDR_approach_status > 1 then
					vorloc_was_eng = 1
				end
				simCMD_autopilot_app:once()
				if ap_pitch_mode == 4 then
					ap_pitch_mode = 0
				end
			end
			if simDR_nav_status > 0 then			-- if VOR LOC or LNAV
				if simDR_nav_status > 1 then
					vorloc_was_eng = 1
				end
				simCMD_autopilot_lnav:once()			-- VOR LOC, LNAV off
				simDR_autopilot_source = 0
				simDR_autopilot_fo_source = 0
			end
			if ap_pitch_mode == 4 then
				ap_pitch_mode = 0
			end
			if B738DR_gls_gs_status == 2 then
				vorloc_was_eng = 1
				if simDR_autopilot_altitude_mode == 4 then
					simCMD_autopilot_vs:once()
				end
			end
			B738DR_gls_app_status = 0
			B738DR_gls_nav_status = 0
			B738DR_gls_gs_status = 0
			
			if simDR_autopilot_heading_mode ~= 1 then
				simCMD_autopilot_hdg:once()
			end
			autopilot_fms_nav_status = 0
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			if (ap_roll_mode_eng == 3 and vorloc_was_eng == 0) or ap_roll_mode_eng == 6 then
				ap_roll_mode = 17	-- HDG / APP
				ap_roll_mode_eng = 17
				if simDR_nav_status == 0 then
					simCMD_autopilot_lnav:once()			-- VOR LOC on
				end
			elseif (ap_roll_mode_eng == 2 and vorloc_was_eng == 0) or ap_roll_mode_eng == 5 then
				ap_roll_mode = 16	-- HDG / VOR LOC
				ap_roll_mode_eng = 16
				if simDR_nav_status == 0 then
					simCMD_autopilot_lnav:once()			-- VOR LOC on
				end
			else
				ap_roll_mode_eng = 1	--HDG
			end
		-- VOR/LOC mode
		elseif ap_roll_mode == 2 then
			
			if simDR_approach_status > 0 then
				simCMD_autopilot_app:once()
			end
			
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			
			if ap_pitch_mode == 4 then
				ap_pitch_mode = 0
			end
			
			if mmr_act_mode <= 1 then
				B738DR_gls_app_status = 0
				B738DR_gls_nav_status = 0
				if B738DR_gls_gs_status == 2 then
					if simDR_autopilot_altitude_mode == 4 then
						simCMD_autopilot_vs:once()
					end
				end
				B738DR_gls_gs_status = 0
				
				if simDR_nav_status == 0 then
					simCMD_autopilot_lnav:once()			-- VOR LOC on
				end
				
				
			else
				B738DR_gls_nav_status = B738DR_gls_app_status
				if B738DR_gls_nav_status == 0 then
					B738DR_gls_nav_status = 1
				end
				B738DR_gls_app_status = 0
				
			end
			
			if ap_roll_mode_eng == 4 or ap_roll_mode_eng == 6 then
				ap_roll_mode = 5		-- LNAV / VOR LOC
				ap_roll_mode_eng = 5
			elseif ap_roll_mode_eng == 1 or ap_roll_mode_eng == 17 or ap_roll_mode_eng == 0 then
				if simDR_nav_status == 1 or B738DR_gls_nav_status == 1 then
					ap_roll_mode = 16	-- HDG / VOR LOC
					ap_roll_mode_eng = 16
				else
					ap_roll_mode_eng = 2	-- VOR LOC
				end
			else
				ap_roll_mode_eng = 2	-- VOR LOC
			end
			
		-- APP mode
		elseif ap_roll_mode == 3 then
			
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			
			local ils_gls_not_tuned = 0
			if B738DR_ap_side == 0 then
				if B738DR_mmr_act_mode == 0 then
					-- not ILS/GLS
					ils_gls_not_tuned = 1
				end
			else
				if B738DR_mmr_act_mode_fo == 0 then
					-- not ILS/GLS
					ils_gls_not_tuned = 1
				end
			end
			
			if B738DR_ian_not_install == 0 then
				-- IAN installed
				if simDR_approach_status > 0 then
					simCMD_autopilot_app:once()
				end
				if simDR_nav_status > 0 then
					simCMD_autopilot_lnav:once()
				end
				
				if ils_gls_not_tuned == 0 then
					-- ILS/GLS tuned
					if mmr_act_mode <= 1 then
						if simDR_nav_status == 0 then
							simCMD_autopilot_lnav:once()			-- VOR LOC on
						end
						
						if B738DR_gls_nav_status > 0 or B738DR_gls_app_status > 0 or B738DR_gls_gs_status > 0 then
							if B738DR_gls_gs_status == 2 then
								if simDR_autopilot_altitude_mode == 4 then
									simCMD_autopilot_vs:once()
								end
							end
							B738DR_gls_app_status = 0
							B738DR_gls_nav_status = 0
							B738DR_gls_gs_status = 0
						end
						
					else
						B738DR_gls_app_status = B738DR_gls_nav_status
						if B738DR_gls_app_status == 0 then
							B738DR_gls_app_status = 1
						end
						if B738DR_gls_gs_status == 2 then
							if simDR_autopilot_altitude_mode == 4 then
								simCMD_autopilot_vs:once()
							end
						end
						if B738DR_fms_ils_disable == 0 then
							B738DR_gls_gs_status = 1
						else
							B738DR_gls_gs_status = 0
						end
						B738DR_gls_nav_status = 0
					end
					
					if B738DR_fms_ils_disable == 0 then	--or ian_info == 0 then
						-- ILS/GLS
						if ap_roll_mode_eng == 4 or ap_roll_mode_eng == 5 then
							ap_roll_mode = 6		-- LNAV / APP
							ap_roll_mode_eng = 6
						elseif ap_roll_mode_eng == 1 or ap_roll_mode_eng == 16 or ap_roll_mode_eng == 0 then
							if simDR_nav_status == 1 or B738DR_gls_app_status == 1 then
								ap_roll_mode = 17	-- HDG / APP
								ap_roll_mode_eng = 17
							else
								ap_roll_mode_eng = 3	-- APP
								if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
									if B738DR_autopilot_autothr_arm_pos == 1 then
										at_mode = 2		-- SPEED on
									end
								end
							end
						else
							ap_roll_mode_eng = 3	-- APP
							if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 2		-- SPEED on
								end
							end
						end
					else
						-- IAN info active
						if ap_roll_mode_eng == 1 then
							ap_roll_mode = 12	-- HDG/LOC arm GP
							ap_roll_mode_eng = 12
						elseif ap_roll_mode_eng == 4 then
							ap_roll_mode = 13	-- LNAV/LOC arm GP
							ap_roll_mode_eng = 13
						else
							ap_roll_mode = 14	-- LOC GP
							ap_roll_mode_eng = 14
							if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
								if B738DR_autopilot_autothr_arm_pos == 1 then
									at_mode = 2		-- SPEED on
								end
							end
						end
					end
				else
					-- ILS/GLS not tuned
					B738DR_gls_app_status = 0
					B738DR_gls_nav_status = 0
					B738DR_gls_gs_status = 0
					
					if ap_roll_mode_eng == 1 then
						ap_roll_mode = 7	-- HDG/FAC arm
						ap_roll_mode_eng = 7
					elseif ap_roll_mode_eng == 4 then
						ap_roll_mode = 8	-- LNAV/FAC arm
						ap_roll_mode_eng = 8
					-- elseif ap_roll_mode_eng == 21 then
						-- ap_roll_mode = 7	-- HDG/FAC arm
						-- ap_roll_mode_eng = 7
					-- elseif ap_roll_mode_eng == 22 then
						-- ap_roll_mode = 8	-- LNAV/FAC arm
						-- ap_roll_mode_eng = 8
					else
						ap_roll_mode = 11	-- FAC
						ap_roll_mode_eng = 11
						if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2		-- SPEED on
							end
						end
					end
				end
			else
				-- IAN not installed
				if simDR_approach_status > 0 then
					simCMD_autopilot_app:once()
				end
				if mmr_act_mode <= 1 then
					if simDR_nav_status == 0 then
						simCMD_autopilot_lnav:once()			-- VOR LOC on
					end
					
					if B738DR_gls_nav_status > 0 or B738DR_gls_app_status > 0 or B738DR_gls_gs_status > 0 then
						if B738DR_gls_gs_status == 2 then
							if simDR_autopilot_altitude_mode == 4 then
								simCMD_autopilot_vs:once()
							end
						end
						B738DR_gls_app_status = 0
						B738DR_gls_nav_status = 0
						B738DR_gls_gs_status = 0
					end
					
				else
					B738DR_gls_app_status = B738DR_gls_nav_status
					if B738DR_gls_app_status == 0 then
						B738DR_gls_app_status = 1
					end
					if B738DR_gls_gs_status == 2 then
						if simDR_autopilot_altitude_mode == 4 then
							simCMD_autopilot_vs:once()
						end
					end
					B738DR_gls_gs_status = 1
					B738DR_gls_nav_status = 0
				end
				
				if ap_roll_mode_eng == 4 or ap_roll_mode_eng == 5 then
					ap_roll_mode = 6		-- LNAV / APP
					ap_roll_mode_eng = 6
				elseif ap_roll_mode_eng == 1 or ap_roll_mode_eng == 16 or ap_roll_mode_eng == 0 then
					if simDR_nav_status == 1 or B738DR_gls_app_status == 1 then
						ap_roll_mode = 17	-- HDG / APP
						ap_roll_mode_eng = 17
					else
						ap_roll_mode_eng = 3	-- APP
						if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2		-- SPEED on
							end
						end
					end
				else
					ap_roll_mode_eng = 3	-- APP
					if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
						if B738DR_autopilot_autothr_arm_pos == 1 then
							at_mode = 2		-- SPEED on
						end
					end
				end
			end
			
			
		-- LNAV mode
		elseif ap_roll_mode == 4 then
			if simDR_approach_status > 0 then
				if simDR_approach_status > 1 then
					vorloc_was_eng = 1
				end
				simCMD_autopilot_app:once()
			end
			if ap_pitch_mode == 4 then
				ap_pitch_mode = 0
			end
			
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			
			-- if B738DR_gls_gs_status == 2 then
			if B738DR_gls_app_status == 2 then
				vorloc_was_eng = 1
				if simDR_autopilot_altitude_mode == 4 then
					simCMD_autopilot_vs:once()
				end
			end
			B738DR_gls_app_status = 0
			B738DR_gls_nav_status = 0
			B738DR_gls_gs_status = 0
			
			if simDR_nav_status > 0 then
				if simDR_nav_status > 1 then
					vorloc_was_eng = 1
				end
				simCMD_autopilot_lnav:once()
			end
			if ap_roll_mode_eng == 1 and B738DR_lnav_prior_arm == 1 then
				ap_roll_mode = 10		-- HDG/LNAV
				ap_roll_mode_eng = 10
			elseif (ap_roll_mode_eng == 3 and vorloc_was_eng == 0) or ap_roll_mode_eng == 17 then
				ap_roll_mode = 6	-- LNAV / APP
				ap_roll_mode_eng = 6
				if simDR_nav_status == 0 then
					simCMD_autopilot_lnav:once()			-- VOR LOC on
				end
			elseif (ap_roll_mode_eng == 2 and vorloc_was_eng == 0) or ap_roll_mode_eng == 16 then
				ap_roll_mode = 5	-- LNAV / VOR LOC
				ap_roll_mode_eng = 5
				if simDR_nav_status == 0 then
					simCMD_autopilot_lnav:once()			-- VOR LOC on
				end
			else
				ap_roll_mode_eng = 4	-- LNAV
			end
		
		elseif ap_roll_mode == 9 then	-- VOR/LOC / ROLLOUT
			ap_roll_mode_eng = 9
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
		elseif ap_roll_mode == 15 then
			ap_roll_mode_eng = 15
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			if simDR_approach_status > 0 then
				simCMD_autopilot_app:once()
			end
			if ap_pitch_mode == 4 then
				ap_pitch_mode = 0
			end
			if simDR_nav_status > 0 then			-- if VOR LOC or LNAV
				simCMD_autopilot_lnav:once()			-- VOR LOC, LNAV off
				simDR_autopilot_source = 0
				simDR_autopilot_fo_source = 0
			end
			--vnav_stop()
			if simDR_autopilot_heading_mode ~= 1 then
				simCMD_autopilot_hdg:once()
			end
			autopilot_fms_nav_status = 0
			if B738DR_pfd_alt_mode ~= PFD_ALT_TO_GA then
				ap_roll_mode = 1
			end
		elseif ap_roll_mode == 18 then
			ap_roll_mode_eng = 18
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
			if simDR_approach_status > 0 then
				simCMD_autopilot_app:once()
			end
			if ap_pitch_mode == 4 then
				ap_pitch_mode = 0
			end
			if simDR_nav_status > 0 then			-- if VOR LOC or LNAV
				simCMD_autopilot_lnav:once()			-- VOR LOC, LNAV off
				simDR_autopilot_source = 0
				simDR_autopilot_fo_source = 0
			end
			if simDR_autopilot_heading_mode ~= 1 then
				simCMD_autopilot_hdg:once()
			end
			autopilot_fms_nav_status = 0
			if B738DR_pfd_alt_mode ~= PFD_ALT_TO_GA then
				ap_roll_mode = 1
			end
		elseif ap_roll_mode == 19 then	-- ROLLOUT
			ap_roll_mode_eng = 19
		end
	
	end

	if B738DR_fac_gp_engaged == 1 then
		-- if B738DR_pfd_gp_path == 0 then
			-- B738DR_gp_status = 1
			-- if ap_pitch_mode == 7 then
				-- ap_pitch_mode = 0
			-- end
		-- else
			if B738DR_gp_status < 2 then
				if B738DR_gp_err_pfd < 100 and B738DR_gp_err_pfd > -200 then
					ap_pitch_mode = 7	-- FAC G/P
					B738DR_gp_status = 2
					if at_mode ~= 2 then
						if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2		-- SPEED on
							end
						end
					end
				else
					B738DR_gp_status = 1
				end
			end
		-- end
	elseif B738DR_loc_gp_engaged == 2 then
		if B738DR_pfd_gp_path == 0 then
			B738DR_gp_status = 1
			if ap_pitch_mode == 7 then	-- G/P
				ap_pitch_mode = 0
			end
		else
			if B738DR_gp_status < 2 then
				if B738DR_gp_err_pfd < 100 and B738DR_gp_err_pfd > -200 then
					ap_pitch_mode = 7	-- LOC G/P
					B738DR_gp_status = 2
					if at_mode ~= 2 then
						if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2		-- SPEED on
							end
						end
					end
				else
					B738DR_gp_status = 1
				end
			end
		end
	elseif ap_roll_mode == 7 and ap_roll_mode_eng == 7 then	-- HDG/FAC GP
		B738DR_gp_status = 1
	elseif ap_roll_mode == 8 and ap_roll_mode_eng == 8 then	-- LNAV/FAC GP
		B738DR_gp_status = 1
	elseif ap_roll_mode == 12 and ap_roll_mode_eng == 12 then	-- HDG/LOC GP
		B738DR_gp_status = 1
	elseif ap_roll_mode == 13 and ap_roll_mode_eng == 13 then	-- LNAV/LOC GP
		B738DR_gp_status = 1
	elseif ap_roll_mode == 14 and ap_roll_mode_eng == 14 then	-- LOC GP
		if B738DR_gp_status == 0 then
			B738DR_gp_status = 1
		end
	else
		B738DR_gp_status = 0
		if ap_pitch_mode_eng == 7 then
			ap_pitch_mode = 0
		end
	end
	
	if vorloc_only == 0 then
		if mmr_act_mode == 1 then
			if simDR_approach_status == 2 then
				if ap_pitch_mode ~= 4 and ap_pitch_mode ~= 13 and ap_pitch_mode ~= 14 and simDR_glideslope_status == 2 then 	-- G/S engaged
					ap_pitch_mode = 4
					if at_mode ~= 2 then
						if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2		-- SPEED on
							end
						end
					end
				end
			end
		elseif mmr_act_mode == 2 then
			if B738DR_gls_app_status == 2 then
				if ap_pitch_mode ~= 4 and ap_pitch_mode ~= 13 and ap_pitch_mode ~= 14 and B738DR_gls_gs_status == 2 then 	-- G/S engaged
					ap_pitch_mode = 4
					if at_mode ~= 2 then
						if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
							if B738DR_autopilot_autothr_arm_pos == 1 then
								at_mode = 2		-- SPEED on
							end
						end
					end
				end
			end
		end
	end
	
	
	if B738DR_autobrake_RTO_arm == 2 then
		if ap_pitch_mode == 8 or ap_pitch_mode == 11 or B738DR_at_takeoff ~= 0 then
			ap_pitch_mode = 0
			B738DR_at_takeoff = 0
		end
	end
	
	if ap_pitch_mode ~= ap_pitch_mode_eng then
		
		-- PITCH MODES: 0-off, 1-V/S, 2-LVL CHG, 3-ALT HLD, 4-G/S, 5-VNAV, 6-ALT ACQ, 7-RNAV G/P
		if ap_pitch_mode == 0 then --and ap_pitch_mode_eng ~= 0 then
			vnav_stop()
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
			ap_pitch_mode_eng = 0
			if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
				simCMD_autopilot_alt_hold:once()	-- ALT HLD , ALT ACQ off
			elseif simDR_autopilot_altitude_mode == 5 then
				simCMD_autopilot_lvl_chg:once()		-- LVL CHG off
			elseif simDR_autopilot_altitude_mode == 4 then
				simCMD_autopilot_vs:once()			-- V/S off
			end
			B738DR_gls_gs_status = 0
			B738DR_lpv_app_status = 0
			B738DR_lpv_gs_status = 0
		
		elseif ap_pitch_mode == 1 then 		-- V/S
			-- ap_pitch_mode_eng = 1
			vs_co_status = 0
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			if B738DR_gp_status == 2 and ap_roll_mode == 11 then
				ap_roll_mode = 0
			end
			-- if B738DR_gls_app_status == 2 then
				-- ap_roll_mode = 0
				-- if simDR_autopilot_heading_mode == 1 then
					-- simCMD_autopilot_hdg:once()		-- HDG off
				-- end
			-- end
			-- B738DR_gls_app_status = 0
			-- B738DR_gls_gs_status = 0
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
			
			if math.abs(B738DR_AP_altitude - B738DR_mcp_alt_dial) >= 100 then
				local ap_vvi_dial = 0
				-- if simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6 then
				if ap_pitch_mode_eng == 3 or ap_pitch_mode_eng == 6 
				or (ap_pitch_mode_eng == 5 and (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6)) then
					ap_vvi_dial = 0
				else
					ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
				end
				if B738DR_mcp_alt_dial > B738DR_AP_altitude then
					if ap_vvi_dial < 0 then
						ap_vvi_dial = 0
					end
				else
					if ap_vvi_dial > 0 then
						ap_vvi_dial = 0
					end
				end
				if simDR_autopilot_altitude_mode ~= 4 then
					simCMD_autopilot_vs:once()
				end
				simDR_ap_vvi_dial = ap_vvi_dial
				B738DR_ap_vvi_dial = simDR_ap_vvi_dial
				ap_pitch_mode_eng = 1
			else
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = 0
				ap_pitch_mode = 3
				ap_pitch_mode_eng = 3
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					B738DR_preselected_alt = simDR_ap_altitude_dial_ft
					simCMD_autopilot_alt_hold:once()
				end
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 2
				end
			end
			
			vs_co_status = 0
			
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 2		-- SPEED on
				end
			end
		
		elseif ap_pitch_mode == 2 then 	-- LVL CHG
			vnav_stop()
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			ap_pitch_mode_eng = 2
			if B738DR_gp_status == 2 and ap_roll_mode == 11 then
				ap_roll_mode = 0
			end
			-- if B738DR_gls_app_status == 2 then
				-- ap_roll_mode = 0
				-- if simDR_autopilot_heading_mode == 1 then
					-- simCMD_autopilot_hdg:once()		-- HDG off
				-- end
			-- end
			-- B738DR_gls_app_status = 0
			-- B738DR_gls_gs_status = 0
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
			
			vnav_vs = 0
			init_climb = 0
			lvl_chg_co_status = 0
			lvl_chg_vs = 0
			
			if math.abs(B738DR_AP_altitude - B738DR_mcp_alt_dial) >= 100 then
				-- if at_mode ~= 0 then
					-- -- speed mode
					-- airspeed_dial = B738DR_mcp_speed_dial
				-- else
					-- if simDR_airspeed_is_mach == 0 then
						-- airspeed_dial = B738DR_AP_airspeed
					-- else
						-- airspeed_dial = B738DR_AP_airspeed_mach
					-- end
				-- end
				if B738DR_AP_altitude > B738DR_mcp_alt_dial then
					B738DR_lvl_chg_mode = 0		-- descent
				else
					B738DR_lvl_chg_mode = 1		-- climb
				end
				if simDR_autopilot_altitude_mode ~= 5 then
					simCMD_autopilot_lvl_chg:once()
				end
				
				if pitch_mode_old == 5 then	--and B738DR_vnav_engaged ~= 0 then
					-- speed mode
					simDR_airspeed_dial = B738DR_mcp_speed_dial
					update_mcp_speed_dial()
				end
				
				if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
					if B738DR_autopilot_autothr_arm_pos ~= 0 then
						at_mode = 6
						at_mode_eng = 20
					end
				end
			else
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = 0
				ap_pitch_mode = 3
				ap_pitch_mode_eng = 3
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					B738DR_preselected_alt = simDR_ap_altitude_dial_ft
					simCMD_autopilot_alt_hold:once()
				end
				if B738DR_autopilot_autothr_arm_pos ~= 0 then
					at_mode = 2
				end
			end
			
		elseif ap_pitch_mode == 3 then 	-- ALT HLD
			vnav_stop()
			ap_pitch_mode_eng = 3
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			if B738DR_gp_status == 2 and ap_roll_mode == 11 then
				ap_roll_mode = 0
			end
			-- if B738DR_gls_app_status == 2 then
				-- ap_roll_mode = 0
				-- if simDR_autopilot_heading_mode == 1 then
					-- simCMD_autopilot_hdg:once()		-- HDG off
				-- end
			-- end
			
			-- B738DR_gls_app_status = 0
			-- B738DR_gls_gs_status = 0
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
			
			altitude_dial_ft_old = simDR_ap_altitude_dial_ft
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
			
			if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
				B738DR_preselected_alt = B738DR_AP_altitude	--simDR_ap_altitude_dial_ft
				simCMD_autopilot_alt_hold:once()
			else
				B738DR_preselected_alt = simDR_ap_altitude_dial_ft
				simCMD_autopilot_alt_hold:once()
				simCMD_autopilot_alt_hold:once()
			end
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if B738DR_autopilot_autothr_arm_pos == 1 then
					B738DR_retard_status = 0				-- PFD: no RETARD
					B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
					at_mode = 2		-- SPEED on
				end
			end
		elseif ap_pitch_mode == 4 then 	-- G/S (APP)
			vnav_stop()
			ap_pitch_mode_eng = 4
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
			B738DR_retard_status = 0				-- PFD: no RETARD
			B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 2		-- SPEED on
				end
			end
		elseif ap_pitch_mode == 5 then 	-- VNAV
			ap_pitch_mode_eng = 5
			if B738DR_gp_status == 2 and ap_roll_mode == 11 then
				ap_roll_mode = 0
			end
			-- if B738DR_gls_app_status == 2 then
				-- ap_roll_mode = 0
				-- if simDR_autopilot_heading_mode == 1 then
					-- simCMD_autopilot_hdg:once()		-- HDG off
				-- end
			-- end
			-- B738DR_gls_app_status = 0
			-- B738DR_gls_gs_status = 0
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
			
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			init_climb = 0
			vnav_init = 0
			B738DR_vnav_descent_disable = 0
			vnav_alt_hold = 0
			vnav_alt_hold_act = 0
			mcp_alt_hold = 99999
			
			B738DR_vnav_alt_mode = 0
			vnav_alt_hld = 0
			
			if simDR_altitude_pilot < 26000 then
				if simDR_airspeed_is_mach == 1 then
					simCMD_autopilot_co:once()
				end
			end
			to_ga_mcp_changed = 0
		elseif ap_pitch_mode == 6 then	-- ALT ACQ
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			-- if B738DR_gls_app_status == 2 then
				-- ap_roll_mode = 0
				-- if simDR_autopilot_heading_mode == 1 then
					-- simCMD_autopilot_hdg:once()		-- HDG off
				-- end
			-- end
			-- B738DR_gls_app_status = 0
			-- B738DR_gls_gs_status = 0
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
			
			vnav_stop()
			ap_pitch_mode_eng = 6
			B738DR_fd_goaround = 0
			if B738DR_gp_status == 2 and ap_roll_mode == 11 then
				ap_roll_mode = 0
			end
			if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
				altitude_dial = simDR_altitude_pilot 
				B738DR_preselected_alt = simDR_ap_altitude_dial_ft
				simCMD_autopilot_alt_hold:once()
				simDR_ap_altitude_dial_ft = altitude_dial
			end
			B738DR_retard_status = 0				-- PFD: no RETARD
			B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if B738DR_autopilot_autothr_arm_pos == 1 then
					B738DR_retard_status = 0				-- PFD: no RETARD
					B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
					at_mode = 2		-- SPEED on
				end
			end
		elseif ap_pitch_mode == 7 then 		-- G/P
			vnav_stop()
			ap_pitch_mode_eng = 7
			B738DR_fd_goaround = 0
			B738DR_ap_goaround = 0
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				if B738DR_autopilot_autothr_arm_pos == 1 then
					at_mode = 2		-- SPEED on
				end
			end
			-- B738DR_gls_app_status = 0
			-- B738DR_gls_gs_status = 0
			-- B738DR_lpv_app_status = 0
			-- B738DR_lpv_gs_status = 0
		
		elseif ap_pitch_mode == 8 then 		-- Takeoff
			-- ap_pitch_mode = 8
			ap_pitch_mode_eng = 8
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 3 -- Takeoff
			end
			B738DR_at_throttle_hold = 0	-- mode throttle HLD off
			B738DR_ap_goaround = 0
			B738DR_fd_goaround = 0
			ap_pitch_mode_old = ap_pitch_mode_eng
			B738DR_autopilot_n1_status = 0
			simDR_autothrottle_enable = 0	-- speed off
			if B738DR_autopilot_lnav_status == 1 then
				if simDR_approach_status > 0 then
					simCMD_autopilot_app:once()
				end
				if simDR_nav_status > 0 then
					simCMD_autopilot_lnav:once()
				end
				if ap_roll_mode ~= 1 then
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 18
						to_ga_hdg = simDR_ahars_mag_hdg
					else
						ap_roll_mode = 10		-- HDG/LNAV
						ap_roll_mode_eng = 10
					end
				end
			else
				if ap_roll_mode ~= 1 then
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 15
						to_ga_hdg = simDR_ahars_mag_hdg
					else
						ap_roll_mode = 1	-- HDG
					end
				end
			end
			to_thrust_set = 0
			fd_go_disable = 0
			ap_go_disable = 0
			to_ga_mcp_changed = 0
		elseif ap_pitch_mode == 9 then 		-- AP Go-Arround
			ap_pitch_mode_eng = 9
			vnav_stop()
		elseif ap_pitch_mode == 10 then 		-- FD Go-Arround
			ap_pitch_mode_eng = 10
			vnav_stop()
		elseif ap_pitch_mode == 11 then 		-- Takeoff / VNAV armed
			ap_pitch_mode_eng = 11
			vnav_stop()
			vnav_engaged = 0
			init_climb = 0
			B738DR_vnav_descent_disable = 0
			vnav_alt_hold = 0
			vnav_alt_hold_act = 0
			mcp_alt_hold = 99999
			if simDR_altitude_pilot < 26000 then
				if simDR_airspeed_is_mach == 1 then
					simCMD_autopilot_co:once()
				end
			end
			
			if B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 3 -- Takeoff
			end
			B738DR_at_throttle_hold = 0	-- mode throttle HLD off
			B738DR_ap_goaround = 0
			B738DR_fd_goaround = 0
			ap_pitch_mode_old = ap_pitch_mode_eng
			B738DR_autopilot_n1_status = 0
			simDR_autothrottle_enable = 0	-- speed off
			if B738DR_autopilot_lnav_status == 1 then
				if simDR_approach_status > 0 then
					simCMD_autopilot_app:once()
				end
				if simDR_nav_status > 0 then
					simCMD_autopilot_lnav:once()
				end
				if ap_roll_mode ~= 1 then
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 18
						to_ga_hdg = simDR_ahars_mag_hdg
					else
						ap_roll_mode = 10		-- HDG/LNAV
						ap_roll_mode_eng = 10
					end
				end
			else
				if ap_roll_mode ~= 1 then
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 15
						to_ga_hdg = simDR_ahars_mag_hdg
					else
						ap_roll_mode = 1	-- HDG
					end
				end
			end
			to_thrust_set = 0
			fd_go_disable = 0
			ap_go_disable = 0
			to_ga_mcp_changed = 0
			
		elseif ap_pitch_mode == 12 then 		-- FD Go-Arround after touchdown
			ap_pitch_mode_eng = 12
			vnav_stop()
		elseif ap_pitch_mode == 13 then 		-- GS / FLARE arm
			ap_pitch_mode_eng = 13
		elseif ap_pitch_mode == 14 then 		-- FLARE
			ap_pitch_mode_eng = 14
		end
	
	end
	
	if ap_pitch_mode ~= 8 and ap_pitch_mode ~= 11 then
		hdg_sel_to = 0
	end
	
	turn_off_dual_cmd()
	
	-- AUTOPILOT BUTTONS LIGHTS
	if ap_roll_mode == 0 then
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 0
		autopilot_fms_nav_status = 0
	elseif ap_roll_mode == 1 then	-- HDG
		if hdg_sel_to == 0 then
			B738DR_autopilot_hdg_sel_status = 1
		else
			B738DR_autopilot_hdg_sel_status = 0
		end
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 0
		autopilot_fms_nav_status = 0
	elseif ap_roll_mode == 2 then	-- VOR/LOC
		if simDR_nav_status == 2 or B738DR_gls_nav_status == 2 then
			B738DR_autopilot_hdg_sel_status = 0
		end
		B738DR_autopilot_vorloc_status = 1
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 0
		autopilot_fms_nav_status = 0
	elseif ap_roll_mode == 3 then	-- APP
		if B738DR_autoland_status == 1 then
			if simDR_glideslope_status == 2 
			or vorloc_only == 1 then
				B738DR_autopilot_app_status = 0		-- turn off APP light status
			end
		elseif ap_pitch_mode == 4 or ap_pitch_mode == 7 then
			B738DR_autopilot_app_status = 0		-- turn off APP light status
		else
			B738DR_autopilot_app_status = 1
		end
		
		-- if simDR_approach_status == 2 or B738DR_gls_app_status == 2 then
		if simDR_nav_status == 2 or simDR_approach_status == 2 or B738DR_gls_app_status == 2 then
			B738DR_autopilot_hdg_sel_status = 0
		end
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_lnav_status = 0
		autopilot_fms_nav_status = 0
	elseif ap_roll_mode == 4 then	-- LNAV
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 1
	elseif ap_roll_mode == 5 then	-- LNAV / VOR LOC
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 1
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 1
	elseif ap_roll_mode == 6 then	-- LNAV / APP
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 1
	elseif ap_roll_mode == 7 then	-- HDG / FAC
		B738DR_autopilot_hdg_sel_status = 1
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 8 then	-- LNAV / FAC
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 1
	elseif ap_roll_mode == 10 then	-- HDG / LNAV arm
		if hdg_sel_to == 0 then
			B738DR_autopilot_hdg_sel_status = 1
		else
			B738DR_autopilot_hdg_sel_status = 0
		end
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 1
	elseif ap_roll_mode == 11 then	-- FAC
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		-- if B738DR_fac_gp_engaged == 1 and B738DR_gp_status == 2 then
		if B738DR_fac_status == 2 and B738DR_gp_status == 2 then
			B738DR_autopilot_app_status = 0
		else
			B738DR_autopilot_app_status = 1
		end
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 12 then	-- HDG / LOC arm GP
		B738DR_autopilot_hdg_sel_status = 1
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 13 then	-- LNAV / LOC arm GP
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 1
	elseif ap_roll_mode == 14 then	-- LOC GP
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 15 then	-- TO GA LOCK TRK
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 16 then	-- HDG / VOR LOC
		B738DR_autopilot_hdg_sel_status = 1
		B738DR_autopilot_vorloc_status = 1
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 17 then	-- HDG / APP
		B738DR_autopilot_hdg_sel_status = 1
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 1
		B738DR_autopilot_lnav_status = 0
	elseif ap_roll_mode == 18 then	-- TO GA LOCK TRK / LNAV arm
		B738DR_autopilot_hdg_sel_status = 0
		B738DR_autopilot_vorloc_status = 0
		B738DR_autopilot_app_status = 0
		B738DR_autopilot_lnav_status = 1
	end
	
	if ap_pitch_mode == 0 then
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		B738DR_autopilot_vnav_status = 0
		alt_hold_captured = 0
	elseif ap_pitch_mode == 1 then		-- V/S
		B738DR_autopilot_vs_status = 1
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		B738DR_autopilot_vnav_status = 0
		alt_hold_captured = 0
	elseif ap_pitch_mode == 2 then	-- LVL CHG
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 1
		B738DR_autopilot_alt_hld_status = 0
		B738DR_autopilot_vnav_status = 0
		alt_hold_captured = 0
	elseif ap_pitch_mode == 3 then	-- ALT HLD
		if alt_hold_captured == 0 then
			B738DR_autopilot_alt_hld_status = 0
		else
			B738DR_autopilot_alt_hld_status = 1
		end
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		-- if B738DR_alt_hold_vnav == 0 then
			B738DR_autopilot_vnav_status = 0
		-- else
			-- B738DR_autopilot_vnav_status = 1
		-- end
	elseif ap_pitch_mode == 4 then	-- G/S
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		if simDR_glideslope_status == 2 or B738DR_gls_gs_status == 2 or ap_pitch_mode == 4 then
			B738DR_autopilot_alt_hld_status = 0
		end
		B738DR_autopilot_vnav_status = 0
		alt_hold_captured = 0
	elseif ap_pitch_mode == 5 then	-- VNAV
		B738DR_autopilot_vnav_status = 1
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		alt_hold_captured = 0
	elseif ap_pitch_mode == 6 then		-- ALT ACQ
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		-- if B738DR_alt_hold_vnav == 0 then
			-- B738DR_autopilot_vnav_status = 0
		-- else
			-- B738DR_autopilot_vnav_status = 1
		-- end
	elseif ap_pitch_mode == 7 then	-- RNAV G/P or LOC G/P
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		B738DR_autopilot_vnav_status = 0
		alt_hold_captured = 0
	elseif ap_pitch_mode == 11 then	-- Takeoff / VNAV
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		B738DR_autopilot_vnav_status = 1
		alt_hold_captured = 0
	else
		B738DR_autopilot_vs_status = 0
		B738DR_autopilot_lvl_chg_status = 0
		B738DR_autopilot_alt_hld_status = 0
		B738DR_autopilot_vnav_status = 0
		alt_hold_captured = 0
	end
	
	if B738DR_lights_test == 1 then
		B738DR_autopilot_hdg_sel_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_vorloc_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_app_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_lnav_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_vs_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_lvl_chg_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_alt_hld_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_vnav_status = 1 * B738DR_brightness2_export
	end
	
	B738DR_autopilot_lnav_status2 = B738DR_autopilot_lnav_status
	B738DR_autopilot_vnav_status2 = B738DR_autopilot_vnav_status
	
	approach_status_old = simDR_approach_status
	glideslope_status_old = simDR_glideslope_status
	ap_pitch_mode_old = ap_pitch_mode_eng
	ap_roll_mode_old = ap_roll_mode_eng
end


function B738_ap_at_disconnect()

	if autopilot_cws_a_status == 1 or autopilot_cws_b_status == 1 then
		cws_on = 1
	else
		cws_on = 0
	end

	local ap_dis_enable = 0
	if ap_on ~= ap_on_first and ap_on == 0 then
		-- AP disconect
		ap_dis_enable = 1
	end
	if cws_on ~= cws_on_first and cws_on == 0 and ap_on == 0 then
		ap_dis_enable = 1
	end
	ap_on_first = ap_on
	cws_on_first = cws_on
	
	if B738DR_autopilot_disconnect_pos ~= ap_disco_first then
		if B738DR_autopilot_disconnect_pos == 1 and ap_disco_do == 1 then
			-- AP disconect
			ap_dis_enable = 1
		end
		if B738DR_autopilot_disconnect_pos == 0 then
			ap_disco_do = 0
			ap_dis_enable = 0
			ap_dis_time = 0
		end
	end
	ap_disco_first = B738DR_autopilot_disconnect_pos
	
	local at_dis_enable = 0
	
	if B738DR_autopilot_autothr_arm_pos ~= at_on_first then
		if B738DR_autopilot_autothr_arm_pos == 0 then
			-- AT disconect
			at_dis_enable = 1
		end
	end
	at_on_first = B738DR_autopilot_autothr_arm_pos
	
	if B738DR_autopilot_disco2 == 1 and ap_disco2 == 0 then
		ap_dis_enable = 0
		ap_dis_time = 0
		ap_capt_disco_status = 0
		ap_fo_disco_status = 0
	end
	if B738DR_ap_light_pilot == 1 or B738DR_ap_light_fo == 1 then
		ap_dis_enable = 0
		ap_dis_time = 0
		ap_capt_disco_status = 0
		ap_fo_disco_status = 0
	end
	if is_timer_scheduled(blink_timer_at) == true then
		if B738DR_at_light_pilot == 1 or B738DR_at_light_fo == 1 or at_left_press == 1 or at_right_press == 1 then
			at_dis_enable = 0
			at_dis_time = 0
			stop_timer(blink_timer_at)
		end
	end
	at_left_press = 0
	at_right_press = 0
	
	
	if ap_dis_enable == 1 then
		ap_dis_time = 40	-- 40 * 0.5sec = 20 seconds
		if is_timer_scheduled(blink_timer_ap) == false then
			run_at_interval(blink_timer_ap, 0.4)
		end
		blink_out_ap = 0
		blink_timer_ap()
	end
	if at_dis_enable == 1 then
		at_dis_time = 40	-- 40 * 0.5sec = 20 seconds
		if is_timer_scheduled(blink_timer_at) == true then
			stop_timer(blink_timer_at)
		end
		blink_out_at = 0
		if is_timer_scheduled(blink_timer_at) == false then
			run_at_interval(blink_timer_at, 0.4)
		end
		blink_out_at = 0
		blink_timer_at()
	end

end

function B738_at_logic()
	
	local ap_thrust = 0.0
	
	-- AT MODES: 0-off, 1-N1, 2-MCP SPD, 3-Takeoff, 4-A/P GA, 5-F/D GA, 6-LVL CHG speed, 7-VNAV speed, 8-A/P GA after touchdown, 9 - MCP SPD with N1 limit
	
	if B738DR_autobrake_RTO_arm == 2 then			-- terminate TAKEOFF mode
		at_mode = 0
	end
	if B738DR_mixture_ratio1 > 0.5 and B738DR_mixture_ratio2 > 0.5 then
		ap_thrust = math.abs(B738DR_thrust1_leveler - B738DR_thrust2_leveler)
		if ap_thrust > 0.1 and B738DR_ap_goaround == 0 and B738DR_fd_goaround == 0 and at_mode ~= 0 then
			if B738DR_autopilot_autothr_arm_pos == 1 and autothr_arm_pos == 0 then
				if autopilot_autothrottle_status == 1 then
					simCMD_autothrottle_discon:once()	-- disconnect autothrotle
					autopilot_autothrottle_status = 0
				end
				B738DR_autopilot_autothr_arm_pos = 0
				at_mode = 0
				at_status = 0
			end
		end
	end
	
	if B738DR_autopilot_autothr_arm_pos == 0 then
		at_mode = 0
		B738DR_autopilot_n1_status = 0
		B738DR_at_takeoff = 0
	end
	
	if at_mode ~= 0 then
		-- if B738DR_dc_bus1_status == 0 and B738DR_load_save == 0 then
		if B738DR_dc_bus1_status == 0 and glare_load ~= 0 then
			at_mode = 0
			simCMD_autothrottle_discon:once()	-- disconnect autothrotle
			autopilot_autothrottle_status = 0
		elseif B738DR_autopilot_autothr_arm_pos == 1 and (simDR_eng_N1[0] < 18 or simDR_eng_N1[1] < 18) then	-- A/T not working
			at_mode = 0
		end
	end
	
	if at_mode == 0 and at_mode_eng ~= 0 then
		at_mode_eng = 0
		B738DR_autopilot_n1_status = 0
		simDR_autothrottle_enable = 0	-- speed off
		B738DR_at_throttle_hold = 0	-- mode throttle HLD off
	-- N1 mode
	elseif at_mode == 1 and at_mode_eng ~= 1 then
		at_mode_eng = 1
		B738DR_at_throttle_hold = 0	-- mode throttle HLD off
		B738DR_retard_status = 0				-- PFD: no RETARD
		if ap_pitch_mode == 2 
		and simDR_autopilot_altitude_mode == 5 then
			simCMD_autopilot_lvl_chg:once()		-- LVL CHG off
			ap_pitch_mode = 0
			ap_pitch_mode_eng = 0
		end
			
		if B738DR_fdir_pitch_active == 0 then
			simDR_fdir_pitch_ovr = 0
		end
		if B738DR_autopilot_speed_status == 1 then
			simDR_autothrottle_enable = 0	-- speed off
		end
		B738DR_autopilot_n1_status = 1
		B738DR_autopilot_speed_status = 0
		B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
		B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
		B738DR_retard_status = 0
	
		fd_go_disable = 0
		ap_go_disable = 0
	
	-- SPD speed with N1 limit
	elseif at_mode == 2 and at_mode_eng ~= 2 then
		at_mode_eng = 2
		B738DR_at_throttle_hold = 0	-- mode throttle HLD off
		B738DR_retard_status = 0				-- PFD: no RETARD
		B738DR_autopilot_n1_pfd = 0				-- PFD: no N1
		B738DR_autopilot_n1_status = 0
		if B738DR_fdir_pitch_active == 0 then
			simDR_fdir_pitch_ovr = 0
		end
		if ap_pitch_mode == 2 
		and simDR_autopilot_altitude_mode == 5 then
			simCMD_autopilot_lvl_chg:once()		-- LVL CHG off
			ap_pitch_mode = 0
			ap_pitch_mode_eng = 0
		end
		simDR_autothrottle_enable = 0
		fd_go_disable = 0
		ap_go_disable = 0
		B738DR_at_takeoff = 0
	
	-- TAKEOFF mode
	elseif at_mode == 3 and at_mode_eng ~= 3 then
		at_mode_eng = 3
		to_thrust_set = 0
		B738DR_autopilot_speed_status = 0
	-- AP GoAround mode
	elseif at_mode == 4 and at_mode_eng ~= 4 then
		at_mode_eng = 4
		B738DR_autopilot_speed_status = 0
		B738DR_at_takeoff = 0
		
	-- FD GoAround mode
	elseif at_mode == 5 and at_mode_eng ~= 5 then
		at_mode_eng = 5
		B738DR_autopilot_speed_status = 0
		B738DR_at_takeoff = 0
		
	-- LVL CHG mode
	elseif at_mode == 6 and at_mode_eng ~= 6 then
		at_mode_eng = 6
		
		B738DR_at_throttle_hold = 0	-- mode throttle HLD off
		if B738DR_fdir_pitch_active == 0 then
			simDR_fdir_pitch_ovr = 0
		end
		simDR_autothrottle_enable = 0	-- speed off
		fd_go_disable = 0
		ap_go_disable = 0
		B738DR_at_takeoff = 0
	
	-- VNAV speed
	elseif at_mode == 7 and at_mode_eng ~= 7 then
		at_mode_eng = 7
		B738DR_at_throttle_hold = 0	-- mode throttle HLD off
		if B738DR_fdir_pitch_active == 0 then
			simDR_fdir_pitch_ovr = 0
		end
		simDR_autothrottle_enable = 0	-- speed off
		fd_go_disable = 0
		ap_go_disable = 0
		B738DR_at_takeoff = 0
	
	-- AP GoAround mode after touchdown
	elseif at_mode == 8 and at_mode_eng ~= 8 then
		at_mode_eng = 8
		ap_go_disable = 0
		B738DR_autopilot_speed_status = 0
		B738DR_at_takeoff = 0
		
	-- SPD speed with N1 limit
	elseif at_mode == 9 and at_mode_eng ~= 9 then
		at_mode_eng = 9
		B738DR_at_throttle_hold = 0	-- mode throttle HLD off
		if B738DR_fdir_pitch_active == 0 then
			simDR_fdir_pitch_ovr = 0
		end
		simDR_autothrottle_enable = 0
		fd_go_disable = 0
		ap_go_disable = 0
		B738DR_at_takeoff = 0
	end
	
end

function no_roll_mode()
	if simDR_approach_status > 0 then
		simCMD_autopilot_app:once()
		if ap_pitch_mode == 4 then
			ap_pitch_mode = 0
		end
	end
	if simDR_nav_status > 0 then
		simCMD_autopilot_lnav:once()
	end
	if B738DR_gls_nav_status > 0 or B738DR_gls_app_status > 0 or B738DR_gls_gs_status > 0 then
		B738DR_gls_app_status = 0
		B738DR_gls_nav_status = 0
		B738DR_gls_gs_status = 0
		if ap_pitch_mode == 4 then
			ap_pitch_mode = 0
		end
	end
	B738DR_lpv_app_status = 0
	B738DR_lpv_gs_status = 0
	if simDR_autopilot_heading_mode == 1 then
		simCMD_autopilot_hdg:once()		-- HDG off
	end
	autopilot_fms_nav_status = 0
	B738DR_gp_status = 0
	B738DR_lnav_engaged = 0
end

function ap_ga1_acivate()
	no_roll_mode()
	if B738DR_autopilot_autothr_arm_pos == 1 then
		at_mode = 8
		B738DR_pfd_spd_mode = PFD_SPD_GA
	end
	B738DR_at_throttle_hold = 0	-- mode throttle HLD off
	B738DR_fd_goaround = 0
	to_after_80kts = 0
	B738DR_at_takeoff = 0
	ap_pitch_mode = 12
	ap_pitch_mode_eng = 12
	B738DR_autoland_status = 0
	B738DR_flare_status = 0
	B738DR_rollout_status = 0
	B738DR_no_autoland = 0
	B738DR_retard_status = 0
	vorloc_only = 0
	B738DR_autopilot_n1_status = 0
	B738DR_pfd_alt_mode = PFD_ALT_TO_GA
	simDR_autothrottle_enable = 0
	simDR_ap_vvi_dial = 0
	B738DR_ap_vvi_dial = 0
	
	if simDR_autopilot_altitude_mode ~= 5 then
		simCMD_autopilot_lvl_chg:once()		--LVL CHG on
	end
	
	if B738DR_fd_on ~= 0 then
		if simDR_radio_height_pilot_ft < 400 then
			if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
				-- before MA
				if B738DR_takeoff_hdg == 0 then
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 18
						B738DR_lnav_engaged = 0
					else
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 15
					end
				else
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 10
						B738DR_lnav_engaged = 0
					else
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 1
					end
				end
				lnav_ma_armed = 0
			else
				-- after MA
				if B738DR_takeoff_hdg == 0 then
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 18
						B738DR_lnav_engaged = 0
					else
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 15
					end
				else
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 10
						B738DR_lnav_engaged = 0
					else
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 1
					end
				end
				lnav_ma_armed = 0
			end
		else
			if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
				ap_roll_mode = 4
				B738DR_lnav_engaged = 1
			elseif ap_roll_mode == 3 then
				ap_roll_mode = 2
			elseif ap_roll_mode ~= 4 then
				if B738DR_takeoff_hdg == 0 then
					ap_roll_mode = 15
					to_ga_hdg = simDR_ahars_mag_hdg
				else
					ap_roll_mode = 1
					to_ga_hdg = simDR_ahars_mag_hdg
				end
			end
		end
	else
		if B738DR_takeoff_hdg == 0 then
			ap_roll_mode = 15
			to_ga_hdg = simDR_ahars_mag_hdg
		else
			ap_roll_mode = 1
			to_ga_hdg = simDR_ahars_mag_hdg
		end
	end
	B738DR_ap_goaround = 3
	fd_go_disable = 0
	ap_go_disable = 0
	
end

function ap_ga2_activate()
	no_roll_mode()
	if B738DR_autopilot_autothr_arm_pos == 1 then
		at_mode = 4
		B738DR_pfd_spd_mode = PFD_SPD_GA
	end
	B738DR_at_throttle_hold = 0	-- mode throttle HLD off
	B738DR_fd_goaround = 0
	to_after_80kts = 0
	B738DR_at_takeoff = 0
	B738DR_autoland_status = 0
	B738DR_flare_status = 0
	B738DR_rollout_status = 0
	B738DR_no_autoland = 0
	B738DR_retard_status = 0
	vorloc_only = 0
	B738DR_autopilot_n1_status = 0
	B738DR_pfd_alt_mode = PFD_ALT_TO_GA
	simDR_ap_vvi_dial = 0
	B738DR_ap_vvi_dial = 0
	if B738DR_fd_on ~= 0 then
		if simDR_radio_height_pilot_ft < 400 then
			if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
				-- before MA
				if B738DR_takeoff_hdg == 0 then
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 18
						B738DR_lnav_engaged = 0
					else
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 15
					end
				else
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 10
						B738DR_lnav_engaged = 0
					else
						-- if simDR_radio_height_pilot_ft < 50 then
							-- to_ga_hdg = simDR_ahars_mag_hdg
							-- ap_roll_mode = 15
						-- else
							to_ga_hdg = simDR_ahars_mag_hdg
							ap_roll_mode = 1
						-- end
					end
				end
			else
				-- after MA
				if B738DR_takeoff_hdg == 0 then
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 18
						B738DR_lnav_engaged = 0
					else
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 15
					end
				else
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 10
						B738DR_lnav_engaged = 0
					else
						-- if simDR_radio_height_pilot_ft < 50 then
							-- to_ga_hdg = simDR_ahars_mag_hdg
							-- ap_roll_mode = 15
						-- else
							to_ga_hdg = simDR_ahars_mag_hdg
							ap_roll_mode = 1
						-- end
					end
				end
			end
		else
			if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
				ap_roll_mode = 4
				B738DR_lnav_engaged = 1
			elseif ap_roll_mode == 3 then
				ap_roll_mode = 2
			elseif ap_roll_mode ~= 4 then
				if B738DR_takeoff_hdg == 0 then
					ap_roll_mode = 15
					to_ga_hdg = simDR_ahars_mag_hdg
				else
					ap_roll_mode = 1
					to_ga_hdg = simDR_ahars_mag_hdg
				end
			end
		end
	else
		if B738DR_takeoff_hdg == 0 then
			ap_roll_mode = 15
			to_ga_hdg = simDR_ahars_mag_hdg
		else
			-- if simDR_radio_height_pilot_ft < 50 then
				-- ap_roll_mode = 15
				-- to_ga_hdg = simDR_ahars_mag_hdg
			-- else
				ap_roll_mode = 1
				to_ga_hdg = simDR_ahars_mag_hdg
			-- end
		end
	end
	
	ap_pitch_mode = 9
	ap_pitch_mode_eng = 9
	ap_pitch_mode_old = ap_pitch_mode_eng
	ap_roll_mode_old = ap_roll_mode_eng
	fd_go_disable = 0
	ap_go_disable = 0
end

function fd_ga_activate()
	no_roll_mode()
	vnav_stop()
	if simDR_approach_status > 0 then
		simCMD_autopilot_app:once()
	end
	if simDR_nav_status > 0 then
		simCMD_autopilot_lnav:once()
	end
	B738DR_fac_gp_engaged = 0
	B738DR_loc_gp_engaged = 0
	B738DR_gls_nav_status = 0
	B738DR_gls_app_status = 0
	B738DR_gls_gs_status = 0
	
	if B738DR_autopilot_autothr_arm_pos == 1 then
		at_mode = 5
		B738DR_pfd_spd_mode = PFD_SPD_GA
	end
	B738DR_at_throttle_hold = 0	-- mode throttle HLD off
	ap_pitch_mode = 10
	ap_pitch_mode_eng = 10
	B738DR_ap_goaround = 0
	to_after_80kts = 0
	B738DR_at_takeoff = 0
	
	mcp_alt_dial_lock = 0
	mcp_alt_dial_app = B738DR_mcp_alt_dial
	simDR_ap_altitude_dial_ft = mcp_alt_dial_app
	
	if simDR_autopilot_altitude_mode ~= 5 then
		simCMD_autopilot_lvl_chg:once()		--LVL CHG on
	end
	
	if B738DR_fd_on ~= 0 then
		if simDR_radio_height_pilot_ft < 400 then
			if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
				-- before MA
				if B738DR_takeoff_hdg == 0 then
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 18
						B738DR_lnav_engaged = 0
					else
						if simDR_radio_height_pilot_ft < 50 then
							to_ga_hdg = simDR_ahars_mag_hdg
							if ap_roll_mode == 4 then
								ap_roll_mode = 18
								B738DR_lnav_engaged = 0
							else
								ap_roll_mode = 15
								to_ga_hdg = simDR_ahars_mag_hdg
							end
						else
							ap_roll_mode = 15
							to_ga_hdg = simDR_ahars_mag_hdg
						end
					end
				else
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						to_ga_hdg = simDR_ahars_mag_hdg
						ap_roll_mode = 10
						B738DR_lnav_engaged = 0
					else
						if simDR_radio_height_pilot_ft < 50 then
							to_ga_hdg = simDR_ahars_mag_hdg
							if ap_roll_mode == 4 then
								ap_roll_mode = 10
								B738DR_lnav_engaged = 0
							else
								ap_roll_mode = 1
								to_ga_hdg = simDR_ahars_mag_hdg
							end
						else
							ap_roll_mode = 1
							to_ga_hdg = simDR_ahars_mag_hdg
						end
					end
				end
			else
				-- after MA
				if B738DR_takeoff_hdg == 0 then
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						if simDR_radio_height_pilot_ft < 50 then
							to_ga_hdg = simDR_ahars_mag_hdg
							ap_roll_mode = 18
							B738DR_lnav_engaged = 0
						else
							ap_roll_mode = 4
							B738DR_lnav_engaged = 1
						end
					else
						if simDR_radio_height_pilot_ft < 50 then
							to_ga_hdg = simDR_ahars_mag_hdg
							if ap_roll_mode == 4 then
								ap_roll_mode = 18
								B738DR_lnav_engaged = 0
							else
								ap_roll_mode = 15
								to_ga_hdg = simDR_ahars_mag_hdg
							end
						else
							ap_roll_mode = 15
							to_ga_hdg = simDR_ahars_mag_hdg
						end
					end
				else
					if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
						if simDR_radio_height_pilot_ft < 50 then
							to_ga_hdg = simDR_ahars_mag_hdg
							ap_roll_mode = 10
							B738DR_lnav_engaged = 0
						else
							ap_roll_mode = 4
							B738DR_lnav_engaged = 1
						end
					else
						if simDR_radio_height_pilot_ft < 50 then
							to_ga_hdg = simDR_ahars_mag_hdg
							if ap_roll_mode == 4 then
								ap_roll_mode = 10
								B738DR_lnav_engaged = 0
							else
								ap_roll_mode = 1
								to_ga_hdg = simDR_ahars_mag_hdg
							end
						else
							ap_roll_mode = 1
							to_ga_hdg = simDR_ahars_mag_hdg
						end
					end
				end
			end
		else
			if lnav_ma_armed ~= 0 and recycled_fd_roll == 0 then
				ap_roll_mode = 4
				B738DR_lnav_engaged = 1
			-- elseif ap_roll_mode == 3 then
				-- ap_roll_mode = 2
			-- elseif ap_roll_mode ~= 4 then
			else
				if B738DR_takeoff_hdg == 0 then
					ap_roll_mode = 15
					to_ga_hdg = simDR_ahars_mag_hdg
				else
					ap_roll_mode = 1
					to_ga_hdg = simDR_ahars_mag_hdg
				end
			end
		end
	else
		if B738DR_takeoff_hdg == 0 then
			ap_roll_mode = 15
			to_ga_hdg = simDR_ahars_mag_hdg
		else
			ap_roll_mode = 1
			to_ga_hdg = simDR_ahars_mag_hdg
		end
	end
	
	simDR_ap_vvi_dial = 0
	B738DR_ap_vvi_dial = 0
	B738DR_retard_status = 0
	B738DR_autopilot_n1_pfd = 0
	B738DR_autopilot_n1_status = 0
	B738DR_pfd_alt_mode = PFD_ALT_TO_GA
	ap_pitch_mode_old = ap_pitch_mode_eng
	ap_roll_mode_old = ap_roll_mode_eng
	fd_go_disable = 0
	ap_go_disable = 0
	
end


function B738_n1()
	
	B738DR_autopilot_autothrottle_status = autopilot_autothrottle_status
	if at_mode == 1 and at_mode_eng == 1 then
		B738DR_autopilot_n1_status = 1
		B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
		B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
	elseif B738DR_lights_test == 1 then
		B738DR_autopilot_n1_status = 1 * B738DR_brightness2_export
		B738DR_autopilot_autothrottle_status = 1 * B738DR_brightness2_export
	elseif B738DR_lights_test == 0 and B738DR_lights_test ~= lights_test_old then
		if at_mode == 2 or at_mode == 9 or at_mode == 0 then
			B738DR_autopilot_n1_status = 0
		end
	end
	lights_test_old = B738DR_lights_test
end


function lift_off()

	lift_off_150 = 1	-- 150 seconds after lift off

end

function B738_lift_off()

	local lo_timer_enable = 1
	
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 
	or simDR_on_ground[2] == 1 then
		lo_timer_enable = 0
		lift_off_150 = 0
	end
	
	if lo_timer_enable == 1 and lift_off_150 == 0 then
		if is_timer_scheduled(lift_off) == false then
			run_after_time(lift_off, 150)
		end
	end

end

function on_ground()

	on_ground_30 = 1	-- 50 seconds on the ground

end

function B738_on_ground()

	local og_timer_enable = 0
	
	if simDR_on_ground[0] == 1 and simDR_on_ground[1] == 1 
	and simDR_on_ground[2] == 1 then
		og_timer_enable = 1
	end
	if simDR_airspeed_pilot < 20 then
		on_ground_30 = 1
	end
	
	if simDR_radio_height_pilot_ft > 500 then
		on_ground_30 = 0
	end
	
	if og_timer_enable == 1 and on_ground_30 == 0 then
		if is_timer_scheduled(on_ground) == false then
			run_after_time(on_ground, 40)
		end
	end

end

function B738_goaround_block()
 
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		if simDR_airspeed_pilot > 60 and on_ground_30 == 0 then
			ap_goaround_block = 0
		end
	else
		ap_goaround_block = 0
	end

end



function B738_ap_takeoff()

	local delta_thr = 0
	local delta_thr2 = 0
	local airspeed_dial = 0
	local v2 = 0
	
	if ap_pitch_mode == 8 or ap_pitch_mode == 11 then
		if simDR_radio_height_pilot_ft < 400
		and B738DR_fd_on == 0 
		and to_after_80kts == 0 then					-- terminate TAKEOFF mode
			at_mode = 0
			if ap_pitch_mode == 11 then
				ap_pitch_mode = 5
			else
				ap_pitch_mode = 0
			end
			B738DR_at_takeoff = 0
		end
	end
	
	local pitch_mode_takeoff = 0
	if ap_pitch_mode == 8 or ap_pitch_mode == 11 then
		pitch_mode_takeoff = 1
	end
	
	-- if ap_pitch_mode == 8 or ap_pitch_mode == 11 then
	if B738DR_at_takeoff ~= 0 or pitch_mode_takeoff ~= 0 then
		
		if simDR_radio_height_pilot_ft > 50 and ap_roll_mode == 18 then
			ap_roll_mode = 4
		end
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 
		or simDR_on_ground[2] == 1 then		-- if aircraft on ground
			if simDR_airspeed_pilot < 60 then
				-- N1 TAKEOFF thrust
				-- if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
					-- at_mode = 3
					-- at_mode_eng = 3
					-- to_thrust_set = 0
				if B738DR_autopilot_autothr_arm_pos == 0 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = 0
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					to_thrust_set = 0
				elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 and B738DR_at_takeoff ~= 0 then
					at_mode = 3
					at_mode_eng = 3
					to_thrust_set = 0
				end
				if at_mode == 3 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = PFD_SPD_N1
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
				end
				
				if pitch_mode_takeoff ~= 0 then
					B738DR_pfd_alt_mode = PFD_ALT_TO_GA
				end
				
				if B738DR_autopilot_speed_status == 1 then
					simDR_autothrottle_enable = 0	-- speed off
				end
				B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				
			elseif simDR_airspeed_pilot < 84 then	-- < 84 kts
				-- if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
					-- at_mode = 3
					-- at_mode_eng = 3
					-- to_thrust_set = 0
				if B738DR_autopilot_autothr_arm_pos == 0 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = 0
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 and B738DR_at_takeoff ~= 0 then
					at_mode = 3
					at_mode_eng = 3
					to_thrust_set = 0
				end
				if at_mode == 3 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = PFD_SPD_N1
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
				end
				if pitch_mode_takeoff ~= 0 then
					B738DR_pfd_alt_mode = PFD_ALT_TO_GA
				end
				B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
			else
				-- THR HLD thrust
				-- if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
					-- at_mode = 3
					-- at_mode_eng = 3
					-- to_thrust_set = 0
				if B738DR_autopilot_autothr_arm_pos == 0 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = 0
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 and B738DR_at_takeoff ~= 0 then
					at_mode = 3
					at_mode_eng = 3
					to_thrust_set = 0
				end
				
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				
				if at_mode == 3 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = PFD_SPD_THR_HLD
					-- B738DR_pfd_alt_mode = PFD_ALT_TO_GA
					if to_thrust_set == 0 then
						
						delta_thr = math.abs(B738DR_eng1_req_N1 - (B738DR_eng1_N1 * 0.01))
						delta_thr2 = math.abs(B738DR_eng2_req_N1 - (B738DR_eng2_N1 * 0.01))
						if delta_thr < 0.05 or delta_thr2 < 0.05 then
							B738DR_at_throttle_hold = 1	-- mode throttle HLD
							to_thrust_set = 1
						end
					else
						B738DR_at_throttle_hold = 1	-- mode throttle HLD
					end
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
				if pitch_mode_takeoff ~= 0 then
					B738DR_pfd_alt_mode = PFD_ALT_TO_GA
				end
			end
			
		-- elseif simDR_radio_height_pilot_ft < B738DR_accel_alt then	-- acceleration height
		elseif simDR_altitude_pilot < B738DR_aerodrom_altitude + B738DR_accel_alt then
			
			if pitch_mode_takeoff ~= 0 then
				if B738DR_fms_v2_set == 0 then
					v2 = 165
				else
					if B738DR_eng_out == 0 then
						v2 = B738DR_fms_v2_set + 15
					else
						v2 = B738DR_fms_v2_set
						if simDR_airspeed_pilot >= v2 and simDR_airspeed_pilot <= B738DR_fms_v2_set + 15 then
							v2 = simDR_airspeed_pilot
						end
					end
				end
				
				if simDR_autopilot_altitude_mode ~= 5 then
					if simDR_airspeed_is_mach ~= 0 then
						simCMD_autopilot_co:once()
					end
					simCMD_autopilot_lvl_chg:once()
				end
				
				B738DR_pfd_alt_mode = PFD_ALT_TO_GA
			end
			
			-- if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				-- at_mode = 3
				-- at_mode_eng = 3
				-- to_thrust_set = 0
			if B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 and B738DR_at_takeoff ~= 0 then
				at_mode = 3
				at_mode_eng = 3
				to_thrust_set = 0
			end
			
			-- if simDR_radio_height_pilot_ft < 800 then
			if simDR_altitude_pilot < B738DR_aerodrom_altitude + 800 and B738DR_lift_timer >= 10 then
				
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				
				if at_mode == 3 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = PFD_SPD_THR_HLD
					if to_thrust_set == 0 then
						
						delta_thr = math.abs(B738DR_eng1_req_N1 - (B738DR_eng1_N1 * 0.01))
						delta_thr2 = math.abs(B738DR_eng2_req_N1 - (B738DR_eng2_N1 * 0.01))
						if delta_thr < 0.05 or delta_thr2 < 0.05 then
							B738DR_at_throttle_hold = 1	-- mode throttle HLD
							to_thrust_set = 1
						end
					else
						B738DR_at_throttle_hold = 1	-- mode throttle HLD
					end
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			elseif B738DR_thr_cutback == 1 then
				-- if simDR_radio_height_pilot_ft >= B738DR_thr_cut_alt and simDR_radio_height_pilot_ft < B738DR_thr_red_alt then
				if (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
					B738DR_eng1_N1_thrust_trg = 1		--B738DR_eng1_N1_lim	--cutback
					B738DR_eng2_N1_thrust_trg = 1		--B738DR_eng2_N1_lim	--cutback
					if at_mode == 3 then
						at_mode = 1
						at_mode_eng = 1
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					elseif at_mode == 1 then
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0
					end
				-- elseif simDR_radio_height_pilot_ft >= B738DR_thr_red_alt then
				elseif (simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_red_alt or B738DR_lift_timer >= 150) then
					B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
					B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
					if at_mode == 3 then
						at_mode = 1
						at_mode_eng = 1
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					elseif at_mode == 1 then
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0
					end
				else
					if at_mode == 3 then
						B738DR_autopilot_n1_status = 0
						B738DR_pfd_spd_mode = PFD_SPD_THR_HLD
						if to_thrust_set == 0 then
							B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
							B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
							
							delta_thr = math.abs(B738DR_eng1_req_N1 - (B738DR_eng1_N1 * 0.01))
							delta_thr2 = math.abs(B738DR_eng2_req_N1 - (B738DR_eng2_N1 * 0.01))
							if delta_thr < 0.05 or delta_thr2 < 0.05 then
								B738DR_at_throttle_hold = 1	-- mode throttle HLD
								to_thrust_set = 1
							end
						else
							B738DR_at_throttle_hold = 1	-- mode throttle HLD
						end
					elseif at_mode == 1 then
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0
					end
				end
			-- elseif simDR_radio_height_pilot_ft >= B738DR_thr_red_alt then
			elseif simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_red_alt or B738DR_lift_timer >= 150 then
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				if at_mode == 3 then
					at_mode = 1
					at_mode_eng = 1
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			else
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				if at_mode == 3 then
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = PFD_SPD_THR_HLD
					if to_thrust_set == 0 then
						delta_thr = math.abs(B738DR_eng1_req_N1 - (B738DR_eng1_N1 * 0.01))
						delta_thr2 = math.abs(B738DR_eng2_req_N1 - (B738DR_eng2_N1 * 0.01))
						if delta_thr < 0.05 or delta_thr2 < 0.05 then
							B738DR_at_throttle_hold = 1	-- mode throttle HLD
							to_thrust_set = 1
						end
					else
						B738DR_at_throttle_hold = 1	-- mode throttle HLD
					end
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			end
			
			-- B738DR_pfd_alt_mode = PFD_ALT_TO_GA
			
			if B738DR_eng_out == 0 then
				if to_ga_mcp_changed ~= 0 then
					simDR_airspeed_dial = B738DR_mcp_speed_dial
					update_mcp_speed_dial()
				else
					simDR_airspeed_dial = mcp_speed_dial_mem + 20
					update_mcp_speed_dial()
				end
			else
				if to_ga_mcp_changed ~= 0 then
					simDR_airspeed_dial = B738DR_mcp_speed_dial
					update_mcp_speed_dial()
				else
					simDR_airspeed_dial = v2
					update_mcp_speed_dial()
				end
			end
		else
			
			if pitch_mode_takeoff ~= 0 then
				if B738DR_fms_v2_set == 0 then
					v2 = 165
				else
					if B738DR_eng_out == 0 then
						v2 = B738DR_fms_v2_set + 15
					else
						v2 = B738DR_fms_v2_set
						if simDR_airspeed_pilot >= v2 and simDR_airspeed_pilot <= B738DR_fms_v2_set + 15 then
							v2 = simDR_airspeed_pilot
						end
					end
				end
				
				if simDR_autopilot_altitude_mode ~= 5 then
					if simDR_airspeed_is_mach ~= 0 then
						simCMD_autopilot_co:once()
					end
					simCMD_autopilot_lvl_chg:once()
				end
				
				B738DR_pfd_alt_mode = PFD_ALT_TO_GA
			end
			
			if B738DR_eng_out == 0 then
				if to_ga_mcp_changed ~= 0 then
					simDR_airspeed_dial = B738DR_mcp_speed_dial
					update_mcp_speed_dial()
				else
					simDR_airspeed_dial = mcp_speed_dial_mem + 20
					update_mcp_speed_dial()
				end
			else
				if to_ga_mcp_changed ~= 0 then
					simDR_airspeed_dial = B738DR_mcp_speed_dial
					update_mcp_speed_dial()
				else
					simDR_airspeed_dial = v2
					update_mcp_speed_dial()
				end
			end
			
			-- if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				-- at_mode = 3
				-- at_mode_eng = 3
				-- to_thrust_set = 0
			if B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 and B738DR_at_takeoff ~= 0 then
				at_mode = 3
				at_mode_eng = 3
				to_thrust_set = 0
			end
			
			if B738DR_thr_cutback == 1 then
				-- if simDR_radio_height_pilot_ft >= B738DR_thr_cut_alt and simDR_radio_height_pilot_ft < B738DR_thr_red_alt then
				if (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
					B738DR_eng1_N1_thrust_trg = 1		--B738DR_eng1_N1_lim	--cutback
					B738DR_eng2_N1_thrust_trg = 1		--B738DR_eng2_N1_lim	--cutback
					if at_mode == 3 then
						at_mode = 1
						at_mode_eng = 1
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					elseif at_mode == 1 then
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0
					end
				-- elseif simDR_radio_height_pilot_ft >= B738DR_thr_red_alt then
				elseif simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_red_alt or B738DR_lift_timer >= 150 then
					B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
					B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
					if at_mode == 3 then
						at_mode = 1
						at_mode_eng = 1
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					elseif at_mode == 1 then
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0
					end
					-- B738DR_pfd_alt_mode = PFD_ALT_TO_GA
				else
					if at_mode == 3 then
						at_mode = 0	-- ARM
						B738DR_at_throttle_hold = 0	-- mode throttle HLD off
						B738DR_autopilot_n1_status = 0
						B738DR_pfd_spd_mode = PFD_SPD_ARM
					elseif at_mode == 1 then
						B738DR_autopilot_n1_status = 1
						B738DR_pfd_spd_mode = PFD_SPD_N1
						B738DR_at_throttle_hold = 0
					end
				end
			-- elseif simDR_radio_height_pilot_ft >= B738DR_thr_red_alt then
			elseif simDR_altitude_pilot >= B738DR_aerodrom_altitude + B738DR_thr_red_alt or B738DR_lift_timer >= 150 then
				B738DR_eng1_N1_thrust_trg = 1		--eng1_N1_thrust	-- N1 THRUST
				B738DR_eng2_N1_thrust_trg = 1		--eng2_N1_thrust	-- N1 THRUST
				if at_mode == 3 then
					at_mode = 1
					at_mode_eng = 1
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			else
				if at_mode == 3 then
					at_mode = 0	-- ARM
					B738DR_at_throttle_hold = 0	-- mode throttle HLD off
					B738DR_autopilot_n1_status = 0
					B738DR_pfd_spd_mode = PFD_SPD_ARM
				elseif at_mode == 1 then
					B738DR_autopilot_n1_status = 1
					B738DR_pfd_spd_mode = PFD_SPD_N1
					B738DR_at_throttle_hold = 0
				end
			end
			
			-- B738DR_pfd_alt_mode = PFD_ALT_TO_GA
			
		end
		
		local n1_status_cfy = B738DR_autopilot_n1_status
		if B738DR_cfy_enable == 1 then
			if B738DR_pfd_spd_mode == PFD_SPD_N1 then
				-- B738DR_autopilot_n1_status = 1
				n1_status_cfy = 1
			end
		end
		B738DR_autopilot_n1_status_cfy = n1_status_cfy
	else
		-- if B738DR_flare_status ~= 0 and at_mode == 6 and (throttle > 0 or throttle2 > 0) then
		if B738DR_autoland_status ~= 0 and at_mode == 6 then
			B738DR_autopilot_n1_status_cfy = 1
		else
			B738DR_autopilot_n1_status_cfy = B738DR_autopilot_n1_status
		end
		-- B738DR_autopilot_n1_status_cfy = B738DR_autopilot_n1_status
	end
	ap_pitch_mode_old = ap_pitch_mode_eng
	
end

function B738_ap_goaround()

	local flaps_speed_loc = 0
	local flaps = simDR_flaps_ratio
	local airspeed_dial = 0
	
	-- A/P GoAround
	if B738DR_ap_goaround > 0 and B738DR_ap_goaround < 3 and ap_pitch_mode == 9 then
		
		if simDR_radio_height_pilot_ft < 400 then
			if ap_on == 0 
			and B738DR_fd_on == 0 then		-- terminate AP GoAround mode
				at_mode = 0
				if ap_roll_mode == 0 or ap_roll_mode == 15 then
					ap_roll_mode = 1
				end
				ap_pitch_mode = 2
				B738DR_ap_goaround = 0
				B738DR_autopilot_to_ga_pfd = 0		-- PFD pitch> TO/GA
			end
		end
		
		if B738DR_ap_goaround ~= 0 then
			if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
				-- before MA
				if B738DR_fd_on ~= 0 then
					if simDR_radio_height_pilot_ft > 400 then
						if B738DR_takeoff_hdg == 0 then
							if ap_roll_mode == 18 then
								ap_roll_mode = 4
							end
						else
							if ap_roll_mode == 10 then
								ap_roll_mode = 4
							end
						end
					end
				else
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 15
					else
						ap_roll_mode = 1
					end
				end
			else
				-- after MA
				if B738DR_fd_on ~= 0 then
					if simDR_radio_height_pilot_ft > 400 then
						if B738DR_takeoff_hdg == 0 then
							if ap_roll_mode == 18 then
								ap_roll_mode = 4
							end
						else
							if ap_roll_mode == 10 then
								ap_roll_mode = 4
							end
						end
					end
				else
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 15
					else
						ap_roll_mode = 1
					end
				end
			end
		end
		
		if B738DR_ap_goaround == 1 then		-- first push
			
			if simDR_autopilot_altitude_mode ~= 5 then
				simCMD_autopilot_lvl_chg:once()		--LVL CHG on
			end
			
			if flaps < 0.125 then		-- flaps 1 -> flaps UP
				flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
			elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
				flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
			elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
				flaps_speed_loc = 184
			elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
				flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
			elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
				flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
			elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
				flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
			elseif flaps < 1 then		-- flaps 40 -> flaps 30
				flaps_speed_loc = 155
			else		-- flaps 40
				flaps_speed_loc = 155
			end
			simDR_airspeed_dial = flaps_speed_loc
			-- new mcp
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
			
			if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 4
				at_mode_eng = 4
				B738DR_pfd_spd_mode = PFD_SPD_GA
				B738DR_autopilot_n1_status = 0
			elseif B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 then
				at_mode = 4
				at_mode_eng = 4
				B738DR_pfd_spd_mode = PFD_SPD_GA
				B738DR_autopilot_n1_status = 0
			elseif at_mode == 1 then
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			end
			
			B738DR_eng1_N1_thrust_trg = 1		--N1_goaround_thrust * 0.94		-- N1 GOAROUND THRUST
			B738DR_eng2_N1_thrust_trg = 1		--N1_goaround_thrust * 0.94		-- N1 GOAROUND THRUST
			
		elseif B738DR_ap_goaround == 2 then	-- second push
			if simDR_autopilot_altitude_mode ~= 5 then
				simCMD_autopilot_lvl_chg:once()		--LVL CHG on
			end
			
			if flaps < 0.125 then		-- flaps 1 -> flaps UP
				flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
			elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
				flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
			elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
				flaps_speed_loc = 184
			elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
				flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
			elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
				flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
			elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
				flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
			elseif flaps < 1 then		-- flaps 40 -> flaps 30
				flaps_speed_loc = 155
			else		-- flaps 40
				flaps_speed_loc = 155
			end
			simDR_airspeed_dial = flaps_speed_loc
			-- new mcp
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
			simDR_autothrottle_enable = 0
			
			if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				-- at_mode = 4
				-- at_mode_eng = 4
				-- B738DR_pfd_spd_mode = PFD_SPD_GA
				-- B738DR_autopilot_n1_status = 0
				at_mode = 1
				at_mode_eng = 1
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			elseif B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			-- elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 then
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode ~= 1 then
				at_mode = 1
				at_mode_eng = 1
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
				-- at_mode = 4
				-- at_mode_eng = 4
				-- B738DR_pfd_spd_mode = PFD_SPD_GA
				-- B738DR_autopilot_n1_status = 0
			-- elseif at_mode == 1 then
				-- B738DR_autopilot_n1_status = 1
				-- B738DR_pfd_spd_mode = PFD_SPD_N1
				-- B738DR_at_throttle_hold = 0
			end
			
			B738DR_eng1_N1_thrust_trg = 1		--N1_goaround_thrust		-- N1 FULL GOAROUND THRUST
			B738DR_eng2_N1_thrust_trg = 1		--N1_goaround_thrust		-- N1 FULL GOAROUND THRUST
		end
		
	-- A/P GoAround after touchdown
	elseif B738DR_ap_goaround == 3 and ap_pitch_mode == 12 then
		if B738DR_fd_on == 0 or ap_on == 0 then
			at_mode = 0
			B738DR_ap_goaround = 0
			if ap_roll_mode == 0 or ap_roll_mode == 15 then
				ap_roll_mode = 1
			end
			ap_pitch_mode = 2
			
		else
			
			if simDR_radio_height_pilot_ft > 50 and ap_roll_mode == 18 then
				ap_roll_mode = 4
			end
			
			if simDR_autopilot_altitude_mode ~= 5 then
				simCMD_autopilot_lvl_chg:once()		--LVL CHG on
			end
			
			if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 8
				at_mode_eng = 8
				B738DR_pfd_spd_mode = PFD_SPD_GA
				B738DR_autopilot_n1_status = 0
			elseif B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 then
				at_mode = 8
				at_mode_eng = 8
				B738DR_pfd_spd_mode = PFD_SPD_GA
				B738DR_autopilot_n1_status = 0
			elseif at_mode == 1 then
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			end
			
			B738DR_eng1_N1_thrust_trg = 1		--N1_goaround_thrust		-- N1 FULL GOAROUND THRUST
			B738DR_eng2_N1_thrust_trg = 1		--N1_goaround_thrust		-- N1 FULL GOAROUND THRUST
			
		end
		
	
	-- F/D GoAround
	elseif B738DR_fd_goaround > 0 and ap_pitch_mode == 10 then
		if B738DR_fd_goaround ~= 0 then
			if B738DR_flight_phase > 1 and B738DR_flight_phase < 8 then
				-- before MA
				if B738DR_fd_on ~= 0 then
					if simDR_radio_height_pilot_ft > 50 then
						if B738DR_takeoff_hdg == 0 then
							if ap_roll_mode == 18 then
								ap_roll_mode = 4
							end
						else
							if ap_roll_mode == 10 then
								ap_roll_mode = 4
							end
						end
					end
				else
					if B738DR_takeoff_hdg == 0 then
						ap_roll_mode = 15
					else
						ap_roll_mode = 1
					end
				end
			else
				-- after MA
				if B738DR_fd_on ~= 0 then
					if simDR_radio_height_pilot_ft > 50 then
						if B738DR_takeoff_hdg == 0 then
							if ap_roll_mode == 18 then
								ap_roll_mode = 4
							end
						else
							if ap_roll_mode == 10 then
								ap_roll_mode = 4
							end
						end
					end
				end
			end
		end
		
		if B738DR_fd_goaround == 1 then		-- first push
			
			if simDR_autopilot_altitude_mode ~= 5 then
				simCMD_autopilot_lvl_chg:once()		--LVL CHG on
			end
			
			if fd_goaround_single == 0 then
				if simDR_engine_on[0] ~= 0 and simDR_engine_on[1] ~= 0 then
					-- dual engine
					if flaps < 0.125 then		-- flaps 1 -> flaps UP
						flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
					elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
						flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
					elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
						flaps_speed_loc = 184
					elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
						flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
					elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
						flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
					elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
						flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
					elseif flaps < 1 then		-- flaps 40 -> flaps 30
						flaps_speed_loc = 155
					else		-- flaps 40
						flaps_speed_loc = 155
					end
				else
					fd_goaround_single = 99
					if simDR_airspeed_dial < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial = B738DR_pfd_min_maneuver_speed
					end
				end
				-- new mcp
				B738DR_mcp_speed_dial = simDR_airspeed_dial
				update_mcp_speed_dial()
			elseif fd_goaround_single < 10 then
				if simDR_engine_on[0] ~= 0 and simDR_engine_on[1] ~= 0 then
					-- dual engine
					if flaps < 0.125 then		-- flaps 1 -> flaps UP
						flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
					elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
						flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
					elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
						flaps_speed_loc = 184
					elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
						flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
					elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
						flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
					elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
						flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
					elseif flaps < 1 then		-- flaps 40 -> flaps 30
						flaps_speed_loc = 155
					else		-- flaps 40
						flaps_speed_loc = 155
					end
					simDR_airspeed_dial = flaps_speed_loc
				else
					simDR_airspeed_dial = B738DR_fms_approach_speed
					fd_goaround_single = 99
					if simDR_airspeed_dial < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial = B738DR_pfd_min_maneuver_speed
					end
				end
				-- new mcp
				B738DR_mcp_speed_dial = simDR_airspeed_dial
				update_mcp_speed_dial()
			elseif fd_goaround_single < 99 then
				if flaps < 0.125 then		-- flaps 1 -> flaps UP
					flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
				elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
					flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
				elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
					flaps_speed_loc = 184
				elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
					flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
				elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
					flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
				elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
					flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
				elseif flaps < 1 then		-- flaps 40 -> flaps 30
					flaps_speed_loc = 155
				else		-- flaps 40
					flaps_speed_loc = 155
				end
				if simDR_engine_on[0] == 0 or simDR_engine_on[1] == 0 then
					if simDR_airspeed_pilot > flaps_speed_loc + 5 then
						simDR_airspeed_dial = simDR_airspeed_pilot
					else
						simDR_airspeed_dial = flaps_speed_loc
					end
					fd_goaround_single = 99
					if simDR_airspeed_dial < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial = B738DR_pfd_min_maneuver_speed
					end
				else
					simDR_airspeed_dial = flaps_speed_loc
				end
				-- new mcp
				B738DR_mcp_speed_dial = simDR_airspeed_dial
				update_mcp_speed_dial()
			end
			
			if fd_goaround_single < 10 then
				fd_goaround_single = fd_goaround_single + SIM_PERIOD
				if fd_goaround_single >= 99 then
					fd_goaround_single = 11
				end
			end
			
			if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 5
				at_mode_eng = 5
				B738DR_pfd_spd_mode = PFD_SPD_GA
				B738DR_autopilot_n1_status = 0
			elseif B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 then
				at_mode = 5
				at_mode_eng = 5
				B738DR_pfd_spd_mode = PFD_SPD_GA
				B738DR_autopilot_n1_status = 0
			elseif at_mode == 1 then
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			end
			
			B738DR_eng1_N1_thrust_trg = 1		--N1_goaround_thrust * 0.94		-- N1 GOAROUND THRUST
			B738DR_eng2_N1_thrust_trg = 1		--N1_goaround_thrust * 0.94		-- N1 GOAROUND THRUST
			
		elseif B738DR_fd_goaround == 2 then	-- second push
			
			if simDR_autopilot_altitude_mode ~= 5 then
				simCMD_autopilot_lvl_chg:once()		--LVL CHG on
			end
			
			if fd_goaround_single == 0 then
				if simDR_engine_on[0] ~= 0 and simDR_engine_on[1] ~= 0 then
					-- dual engine
					if flaps < 0.125 then		-- flaps 1 -> flaps UP
						flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
					elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
						flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
					elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
						flaps_speed_loc = 184
					elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
						flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
					elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
						flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
					elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
						flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
					elseif flaps < 1 then		-- flaps 40 -> flaps 30
						flaps_speed_loc = 155
					else		-- flaps 40
						flaps_speed_loc = 155
					end
				else
					fd_goaround_single = 99
					if simDR_airspeed_dial < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial = B738DR_pfd_min_maneuver_speed
					end
				end
				-- new mcp
				B738DR_mcp_speed_dial = simDR_airspeed_dial
				update_mcp_speed_dial()
			elseif fd_goaround_single < 10 then
				if simDR_engine_on[0] ~= 0 and simDR_engine_on[1] ~= 0 then
					-- dual engine
					if flaps < 0.125 then		-- flaps 1 -> flaps UP
						flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
					elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
						flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
					elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
						flaps_speed_loc = 184
					elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
						flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
					elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
						flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
					elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
						flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
					elseif flaps < 1 then		-- flaps 40 -> flaps 30
						flaps_speed_loc = 155
					else		-- flaps 40
						flaps_speed_loc = 155
					end
					simDR_airspeed_dial = flaps_speed_loc
				else
					simDR_airspeed_dial = B738DR_fms_approach_speed
					fd_goaround_single = 99
					if simDR_airspeed_dial < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial = B738DR_pfd_min_maneuver_speed
					end
				end
				-- new mcp
				B738DR_mcp_speed_dial = simDR_airspeed_dial
				update_mcp_speed_dial()
			elseif fd_goaround_single < 20 then
				if flaps < 0.125 then		-- flaps 1 -> flaps UP
					flaps_speed_loc = B738_rescale(0, 222, 0.125, 204, flaps)
				elseif flaps < 0.375 then		-- flaps 5 -> flaps 1
					flaps_speed_loc = B738_rescale(0.125, 204, 0.375, 184, flaps)
				elseif flaps < 0.5 then		-- flaps 10 -> flaps 5
					flaps_speed_loc = 184
				elseif flaps < 0.625 then		-- flaps 15 -> flaps 10
					flaps_speed_loc = B738_rescale(0.5, 184, 0.625, 174, flaps)
				elseif flaps < 0.75 then		-- flaps 25 -> flaps 15
					flaps_speed_loc = B738_rescale(0.625, 174, 0.75, 164, flaps)
				elseif flaps < 0.875 then		-- flaps 30 -> flaps 25
					flaps_speed_loc = B738_rescale(0.75, 164, 0.875, 155, flaps)
				elseif flaps < 1 then		-- flaps 40 -> flaps 30
					flaps_speed_loc = 155
				else		-- flaps 40
					flaps_speed_loc = 155
				end
				if simDR_engine_on[0] == 0 or simDR_engine_on[1] == 0 then
					if simDR_airspeed_pilot > flaps_speed_loc + 5 then
						simDR_airspeed_dial = simDR_airspeed_pilot
					else
						simDR_airspeed_dial = flaps_speed_loc
					end
					fd_goaround_single = 99
					if simDR_airspeed_dial < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial = B738DR_pfd_min_maneuver_speed
					end
				else
					simDR_airspeed_dial = flaps_speed_loc
				end
				-- new mcp
				B738DR_mcp_speed_dial = simDR_airspeed_dial
				update_mcp_speed_dial()
			end
			
			if fd_goaround_single < 10 then
				fd_goaround_single = fd_goaround_single + SIM_PERIOD
				if fd_goaround_single >= 99 then
					fd_goaround_single = 11
				end
			end
			
			simDR_autothrottle_enable = 0
			
			if at_on_old ~= B738DR_autopilot_autothr_arm_pos and B738DR_autopilot_autothr_arm_pos == 1 then
				at_mode = 1
				at_mode_eng = 1
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			elseif B738DR_autopilot_autothr_arm_pos == 0 then
				B738DR_autopilot_n1_status = 0
				B738DR_pfd_spd_mode = 0
			elseif B738DR_autopilot_autothr_arm_pos == 1 and at_mode ~= 1 then
				at_mode = 1
				at_mode_eng = 1
				B738DR_autopilot_n1_status = 1
				B738DR_pfd_spd_mode = PFD_SPD_N1
				B738DR_at_throttle_hold = 0
			end
			
			B738DR_eng1_N1_thrust_trg = 1		--N1_goaround_thrust		-- N1 FULL GOAROUND THRUST
			B738DR_eng2_N1_thrust_trg = 1		--N1_goaround_thrust		-- N1 FULL GOAROUND THRUST
			
		end
	else
		fd_goaround_single = 0
	end
	ap_pitch_mode_old = ap_pitch_mode_eng
	ap_roll_mode_old = ap_roll_mode_eng

end

function B738_fd_show()

	-- if to_after_80kts == 0 then		-- if no Takeoff without F/D on
		if ap_pitch_mode >= 8 and ap_pitch_mode <= 12 then
			B738DR_fd_pitch_pilot_show = 1
			B738DR_fd_pitch_copilot_show = 1
			B738DR_fd_roll_pilot_show = 1
			B738DR_fd_roll_copilot_show = 1
		else
			if ap_pitch_mode == 0 then
				B738DR_fd_pitch_pilot_show = 0
				B738DR_fd_pitch_copilot_show = 0
			else
				if B738DR_pfd_alt_mode_arm == PFD_ALT_VNAV_ARM and ap_pitch_mode == 5 then
					B738DR_fd_pitch_pilot_show = 0
					B738DR_fd_pitch_copilot_show = 0
				else
					B738DR_fd_pitch_pilot_show = B738DR_autopilot_fd_pos
					B738DR_fd_pitch_copilot_show = B738DR_autopilot_fd_fo_pos
				end
			end
			if ap_pitch_mode == 4 or ap_pitch_mode == 7 or B738DR_rollout_status > 1 or B738DR_autoland_status == 1 then
				if simDR_radio_height_pilot_ft < 50 then
					B738DR_fd_pitch_pilot_show = 0
				end
				if simDR_radio_height_copilot_ft < 50 then
					B738DR_fd_pitch_copilot_show = 0
				end
			end
			
			if ap_roll_mode == 0 then
				B738DR_fd_roll_pilot_show = 0
				B738DR_fd_roll_copilot_show = 0
			else
				if B738DR_lnav_engaged == 0 and ap_roll_mode == 4 then
					B738DR_fd_roll_pilot_show = 0
					B738DR_fd_roll_copilot_show = 0
				else
					B738DR_fd_roll_pilot_show = B738DR_autopilot_fd_pos
					B738DR_fd_roll_copilot_show = B738DR_autopilot_fd_fo_pos
				end
			end
			if ap_roll_mode == 3 or ap_roll_mode == 2 or B738DR_rollout_status > 1 or B738DR_autoland_status == 1 then
				if simDR_radio_height_pilot_ft < 40 then
					B738DR_fd_roll_pilot_show = 0
				end
				if simDR_radio_height_copilot_ft < 40 then
					B738DR_fd_roll_copilot_show = 0
				end
			end
		end
	-- else
		-- B738DR_fd_roll_pilot_show = 1
		-- B738DR_fd_pitch_pilot_show = 1
		-- B738DR_fd_roll_copilot_show = 1
		-- B738DR_fd_pitch_copilot_show = 1
	-- end
	
	if B738DR_single_cue_fd ~= 0 then
		if B738DR_fd_pitch_pilot_show == 0 and B738DR_fd_roll_pilot_show == 0 then
			B738DR_fd_pilot_show = 0
		else
			B738DR_fd_pilot_show = 1
		end
		if B738DR_fd_pitch_copilot_show == 0 and B738DR_fd_roll_copilot_show == 0 then
			B738DR_fd_copilot_show = 0
		else
			B738DR_fd_copilot_show = 1
		end
	end
	
end


---- IAS and ALT DISAGREE ----

function ias_disagree_timer()
	ias_disagree = 1
end

function alt_disagree_timer()
	alt_disagree = 1
end

function B738_ias_alt_disagree()

	local delta_airspeed = 0
	local delta_alt = 0
	
	-- IAS disagree
	delta_airspeed = simDR_airspeed_pilot - simDR_airspeed_copilot
	if delta_airspeed < 0 then
		delta_airspeed = -delta_airspeed
	end
	if delta_airspeed > 5 then
		if is_timer_scheduled(ias_disagree_timer) == false then
			run_after_time(ias_disagree_timer, 5)	-- 5 seconds
		end
	else
		if is_timer_scheduled(ias_disagree_timer) == true then
			stop_timer(ias_disagree_timer)
		end
		ias_disagree = 0
	end
	
	-- ALT disagree
	delta_alt = simDR_altitude_pilot - simDR_altitude_copilot
	if delta_alt < 0 then
		delta_alt = -delta_alt
	end
	if delta_alt > 200 then
		if is_timer_scheduled(alt_disagree_timer) == false then
			run_after_time(alt_disagree_timer, 5)	-- 5 seconds
		end
	else
		if is_timer_scheduled(alt_disagree_timer) == true then
			stop_timer(alt_disagree_timer)
		end
		alt_disagree = 0
	end
	B738DR_ias_disagree = ias_disagree
	B738DR_alt_disagree = alt_disagree

end

function B738_pfd_runway()
	local pfd_runway_show = 0
	local radio_height = 0
	local expand = 0
	
	if B738DR_nav_horz_dsp == nil or B738DR_nav_horz_dsp == 0 then
		 pfd_runway_show = 0
	elseif B738DR_nav_hdef < -2.49 or B738DR_nav_hdef > 2.49 then
		pfd_runway_show = 0
	elseif B738DR_autopilot_pfd_mode == 1 or B738DR_autopilot_pfd_mode == 6 then
		radio_height = simDR_radio_height_pilot_ft
		if radio_height < 2500 then
			pfd_runway_show = 1
			if B738DR_autopilot_pfd_mode == 1 and B738DR_pfd_loc_exp == 1 then
				expand = 1
			elseif B738DR_autopilot_pfd_mode == 6 and B738DR_pfd_gls_exp == 1 then
				expand = 1
			end
			if expand == 0 then
				if B738DR_nav_hdef > -2.5 and B738DR_nav_hdef < 2.5 then
					B738DR_pfd_runway_x = B738_rescale(-2.5, -87.5, 2.5, 87.5, B738DR_nav_hdef)
				elseif B738DR_nav_hdef <= -2.5 then
					B738DR_pfd_runway_x = -87.5
				else
					B738DR_pfd_runway_x = 87.5
				end
			else
				if B738DR_nav_hdef > -0.5 and B738DR_nav_hdef < 0.5 then
					B738DR_pfd_runway_x = B738_rescale(-0.5, -70, 0.5, 70, B738DR_nav_hdef)
				elseif B738DR_nav_hdef <= -0.5 then
					B738DR_pfd_runway_x = -70
				else
					B738DR_pfd_runway_x = 70
				end
			end
			if radio_height < 0 then
				radio_height = 0
			end
			if radio_height < 200 then
				B738DR_pfd_runway_y = B738_rescale(0, 0, 200, -70, radio_height)
			else
				B738DR_pfd_runway_y = -70
			end
		end
	end
	B738DR_pfd_runway_show = pfd_runway_show

	pfd_runway_show = 0
	radio_height = 0
	expand = 0
	if B738DR_nav_horz_dsp_fo == nil or B738DR_nav_horz_dsp_fo == 0 then
		pfd_runway_show = 0
	elseif B738DR_nav_hdef_fo < -2.49 or B738DR_nav_hdef_fo > 2.49 then
		pfd_runway_show = 0
	elseif B738DR_autopilot_pfd_mode_fo == 1 or B738DR_autopilot_pfd_mode_fo == 6 then
		radio_height = simDR_radio_height_copilot_ft
		if radio_height < 2500 then
			pfd_runway_show = 1
			if B738DR_autopilot_pfd_mode_fo == 1 and B738DR_pfd_loc_exp_fo == 1 then
				expand = 1
			elseif B738DR_autopilot_pfd_mode_fo == 6 and B738DR_pfd_gls_exp_fo == 1 then
				expand = 1
			end
			if expand == 0 then
				if B738DR_nav_hdef_fo > -2.5 and B738DR_nav_hdef_fo < 2.5 then
					B738DR_pfd_runway_x_fo = B738_rescale(-2.5, -87.5, 2.5, 87.5, B738DR_nav_hdef_fo)
				elseif B738DR_nav_hdef_fo <= -2.5 then
					B738DR_pfd_runway_x_fo = -87.5
				else
					B738DR_pfd_runway_x_fo = 87.5
				end
			else
				if B738DR_nav_hdef_fo > -0.5 and B738DR_nav_hdef_fo < 0.5 then
					B738DR_pfd_runway_x_fo = B738_rescale(-0.5, -70, 0.5, 70, B738DR_nav_hdef_fo)
				elseif B738DR_nav_hdef_fo <= -0.5 then
					B738DR_pfd_runway_x_fo = -70
				else
					B738DR_pfd_runway_x_fo = 70
				end
			end
			if radio_height < 0 then
				radio_height = 0
			end
			if radio_height < 200 then
				B738DR_pfd_runway_y_fo = B738_rescale(0, 0, 200, -70, radio_height)
			else
				B738DR_pfd_runway_y_fo = -70
			end
		end
	end
	B738DR_pfd_runway_show_fo = pfd_runway_show
end

-- Draw green circle
function B738_draw_arc()

	local delta_alt = 0
	local arc_dist = 0
	local arc_dist2 = 0
	local arc_time = 0
	local arc_zoom = 0
	local arc_vvi = 0
	
	if (B738DR_capt_map_mode == 2 or B738DR_fo_map_mode == 2) then
		if ap_pitch_mode == 1 then		-- V/S
			arc_vvi = simDR_vvi_fpm_pilot
			if arc_vvi > 100 and B738DR_mcp_alt_dial > simDR_altitude_pilot then
				delta_alt = B738DR_mcp_alt_dial - simDR_altitude_pilot
			elseif arc_vvi < -100 and B738DR_mcp_alt_dial < simDR_altitude_pilot then
				delta_alt = simDR_altitude_pilot - B738DR_mcp_alt_dial
				arc_vvi = -arc_vvi
			else
				arc_vvi = 0
			end
			if arc_vvi ~= 0 then 
				arc_time = delta_alt / (arc_vvi / 60) 	-- secs
				arc_dist = simDR_ground_spd * arc_time * 0.00054	-- m to NM
				
				-- Captain
				if B738DR_capt_map_mode == 2 then
					arc_zoom = 0
					if B738DR_efis_map_range_capt == 0 then	-- 5 NM
						arc_zoom = 0.226
					elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
						arc_zoom = 0.113
					elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
						arc_zoom = 0.0565
					elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
						arc_zoom = 0.02825
					elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
						arc_zoom = 0.014125
					elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
						arc_zoom = 0.0070625
					elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
						arc_zoom = 0.00353125
					elseif B738DR_efis_map_range_capt == 7 then	-- 640 NM
						arc_zoom = 0.001765625
					end
					if arc_zoom == 0 then
						B738DR_green_arc_show = 0
					else
						arc_dist2 = arc_dist * arc_zoom
						if B738DR_capt_vsd_map_mode == 1 then
							arc_dist2 = arc_dist2 + 0.57878	-- adjust center
						elseif B738DR_capt_exp_map_mode == 0 then
							arc_dist2 = arc_dist2 + 0.465294
						end
						if arc_dist2 < 0.005 or arc_dist2 > 1.0 then
							B738DR_green_arc_show = 0
						else
							B738DR_green_arc = arc_dist2
							B738DR_green_arc_show = 1
						end
					end
				else
					B738DR_green_arc_show = 0
				end
				
				-- First Officer
				if B738DR_fo_map_mode == 2 then
					arc_zoom = 0
					if B738DR_efis_map_range_fo == 0 then	-- 5 NM
						arc_zoom = 0.226
					elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
						arc_zoom = 0.113
					elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
						arc_zoom = 0.0565
					elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
						arc_zoom = 0.02825
					elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
						arc_zoom = 0.014125
					elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
						arc_zoom = 0.0070625
					elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
						arc_zoom = 0.00353125
					elseif B738DR_efis_map_range_fo == 7 then	-- 640 NM
						arc_zoom = 0.001765625
					end
					if arc_zoom == 0 then
						B738DR_green_arc_fo_show = 0
					else
						arc_dist2 = arc_dist * arc_zoom
						if B738DR_fo_vsd_map_mode == 1 then
							arc_dist2 = arc_dist2 + 0.57878	-- adjust center
						elseif B738DR_fo_exp_map_mode == 0 then
							arc_dist2 = arc_dist2 + 0.465294
						end
						if arc_dist2 < 0.005 or arc_dist2 > 1.0 then
							B738DR_green_arc_fo_show = 0
						else
							B738DR_green_arc_fo = arc_dist2
							B738DR_green_arc_fo_show = 1
						end
					end
				else
					B738DR_green_arc_fo_show = 0
				end
			else
				B738DR_green_arc_show = 0
				B738DR_green_arc_fo_show = 0
			end
		elseif ap_pitch_mode == 2 then		-- LVL CHG
			arc_vvi = simDR_vvi_fpm_pilot
			if arc_vvi > 100 and B738DR_mcp_alt_dial > simDR_altitude_pilot then
				delta_alt = B738DR_mcp_alt_dial - simDR_altitude_pilot
			elseif arc_vvi < -100 and B738DR_mcp_alt_dial < simDR_altitude_pilot then
				delta_alt = simDR_altitude_pilot - B738DR_mcp_alt_dial
				arc_vvi = -arc_vvi
			else
				arc_vvi = 0
			end
			if arc_vvi ~= 0 then
				arc_time = delta_alt / (arc_vvi / 60)	-- secs
				arc_dist = simDR_ground_spd * arc_time * 0.00054	-- m to NM
				
				-- Captain
				if B738DR_capt_map_mode == 2 then
					arc_zoom = 0
					if B738DR_efis_map_range_capt == 0 then	-- 5 NM
						arc_zoom = 0.226
					elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
						arc_zoom = 0.113
					elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
						arc_zoom = 0.0565
					elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
						arc_zoom = 0.02825
					elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
						arc_zoom = 0.014125
					elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
						arc_zoom = 0.0070625
					elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
						arc_zoom = 0.00353125
					elseif B738DR_efis_map_range_capt == 7 then	-- 640 NM
						arc_zoom = 0.001765625
					end
					if arc_zoom == 0 then
						B738DR_green_arc_show = 0
					else
						arc_dist2 = arc_dist * arc_zoom
							if B738DR_capt_vsd_map_mode == 1 then
								arc_dist2 = arc_dist2 + 0.57878	-- adjust center
							elseif B738DR_capt_exp_map_mode == 0 then
								arc_dist2 = arc_dist2 + 0.465294
							end
						if arc_dist2 < 0.005 or arc_dist2 > 1.0 then
							B738DR_green_arc_show = 0
						else
							B738DR_green_arc = arc_dist2
							B738DR_green_arc_show = 1
						end
					end
				else
					B738DR_green_arc_show = 0
				end
				
				-- First Officer
				if B738DR_fo_map_mode == 2 then
					arc_zoom = 0
					if B738DR_efis_map_range_fo == 0 then	-- 5 NM
						arc_zoom = 0.226
					elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
						arc_zoom = 0.113
					elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
						arc_zoom = 0.0565
					elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
						arc_zoom = 0.02825
					elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
						arc_zoom = 0.014125
					elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
						arc_zoom = 0.0070625
					elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
						arc_zoom = 0.00353125
					elseif B738DR_efis_map_range_fo == 7 then	-- 640 NM
						arc_zoom = 0.001765625
					end
					if arc_zoom == 0 then
						B738DR_green_arc_fo_show = 0
					else
						arc_dist2 = arc_dist * arc_zoom
						if B738DR_fo_vsd_map_mode == 1 then
							arc_dist2 = arc_dist2 + 0.57878	-- adjust center
						elseif B738DR_fo_exp_map_mode == 0 then
							arc_dist2 = arc_dist2 + 0.465294
						end
						if arc_dist2 < 0.005 or arc_dist2 > 1.0 then
							B738DR_green_arc_fo_show = 0
						else
							B738DR_green_arc_fo = arc_dist2
							B738DR_green_arc_fo_show = 1
						end
					end
				else
					B738DR_green_arc_fo_show = 0
				end
			else
				B738DR_green_arc_show = 0
				B738DR_green_arc_fo_show = 0
			end
		elseif ap_pitch_mode == 5 then		-- VNAV
			if simDR_autopilot_altitude_mode == 4 or simDR_autopilot_altitude_mode == 5 then	-- VS or LVL CHG
				arc_vvi = simDR_vvi_fpm_pilot
				if arc_vvi > 100 and B738DR_mcp_alt_dial > simDR_altitude_pilot then
					delta_alt = B738DR_mcp_alt_dial - simDR_altitude_pilot
				elseif arc_vvi < -100 and B738DR_mcp_alt_dial < simDR_altitude_pilot then
					delta_alt = simDR_altitude_pilot - B738DR_mcp_alt_dial
					arc_vvi = -arc_vvi
				else
					arc_vvi = 0
				end
				if arc_vvi ~= 0 then
					arc_time = delta_alt / (arc_vvi / 60)	-- secs
					arc_dist = simDR_ground_spd * arc_time * 0.00054	-- m to NM
				
					-- Captain
					if B738DR_capt_map_mode == 2 then
						arc_zoom = 0
						if B738DR_efis_map_range_capt == 0 then	-- 5 NM
							arc_zoom = 0.226
						elseif B738DR_efis_map_range_capt == 1 then	-- 10 NM
							arc_zoom = 0.113
						elseif B738DR_efis_map_range_capt == 2 then	-- 20 NM
							arc_zoom = 0.0565
						elseif B738DR_efis_map_range_capt == 3 then	-- 40 NM
							arc_zoom = 0.02825
						elseif B738DR_efis_map_range_capt == 4 then	-- 80 NM
							arc_zoom = 0.014125
						elseif B738DR_efis_map_range_capt == 5 then	-- 160 NM
							arc_zoom = 0.0070625
						elseif B738DR_efis_map_range_capt == 6 then	-- 320 NM
							arc_zoom = 0.00353125
						elseif B738DR_efis_map_range_capt == 7 then	-- 640 NM
							arc_zoom = 0.001765625
						end
						if arc_zoom == 0 then
							B738DR_green_arc_show = 0
						else
							arc_dist2 = arc_dist * arc_zoom
							if B738DR_capt_vsd_map_mode == 1 then
								arc_dist2 = arc_dist2 + 0.57878	-- adjust center
							elseif B738DR_capt_exp_map_mode == 0 then
								arc_dist2 = arc_dist2 + 0.465294
							end
							if arc_dist2 < 0.005 or arc_dist2 > 1.0 then
								B738DR_green_arc_show = 0
							else
								B738DR_green_arc = arc_dist2
								B738DR_green_arc_show = 1
							end
						end
					else
						B738DR_green_arc_show = 0
					end
					
					-- First Officer
					if B738DR_fo_map_mode == 2 then
						arc_zoom = 0
						if B738DR_efis_map_range_fo == 0 then	-- 5 NM
							arc_zoom = 0.226
						elseif B738DR_efis_map_range_fo == 1 then	-- 10 NM
							arc_zoom = 0.113
						elseif B738DR_efis_map_range_fo == 2 then	-- 20 NM
							arc_zoom = 0.0565
						elseif B738DR_efis_map_range_fo == 3 then	-- 40 NM
							arc_zoom = 0.02825
						elseif B738DR_efis_map_range_fo == 4 then	-- 80 NM
							arc_zoom = 0.014125
						elseif B738DR_efis_map_range_fo == 5 then	-- 160 NM
							arc_zoom = 0.0070625
						elseif B738DR_efis_map_range_fo == 6 then	-- 320 NM
							arc_zoom = 0.00353125
						elseif B738DR_efis_map_range_fo == 7 then	-- 640 NM
							arc_zoom = 0.001765625
						end
						if arc_zoom == 0 then
							B738DR_green_arc_fo_show = 0
						else
							arc_dist2 = arc_dist * arc_zoom
							if B738DR_fo_vsd_map_mode == 1 then
								arc_dist2 = arc_dist2 + 0.57878	-- adjust center
							elseif B738DR_fo_exp_map_mode == 0 then
								arc_dist2 = arc_dist2 + 0.465294
							end
							if arc_dist2 < 0.005 or arc_dist2 > 1.0 then
								B738DR_green_arc_fo_show = 0
							else
								B738DR_green_arc_fo = arc_dist2
								B738DR_green_arc_fo_show = 1
							end
						end
					else
						B738DR_green_arc_fo_show = 0
					end
				else
					B738DR_green_arc_show = 0
					B738DR_green_arc_fo_show = 0
				end
			else
				B738DR_green_arc_show = 0
				B738DR_green_arc_fo_show = 0
			end
		else
			B738DR_green_arc_show = 0
			B738DR_green_arc_fo_show = 0
		end
	else
		B738DR_green_arc_show = 0
		B738DR_green_arc_fo_show = 0
	end
end


function timer_efis_disagr()
	B738DR_efis_disagree = 1
end

function timer_efis_disagr_fo()
	B738DR_efis_disagree_fo = 1
end

function B738_efis()
	
	local efis_disagree = 0
	local nav_type_vor = 0
	
	nav_type_vor = B738DR_tuned_nav1_type
	B738DR_nav_vor = nav_type_vor
	
	if B738DR_capt_map_mode == 0 then
		-- APP
		if B738DR_mmr_act_mode == 0 then
			if B738DR_ian_info == 0 then
				efis_disagree = 1
			end
		end
	elseif B738DR_capt_map_mode == 1 then
		-- VOR
		if B738DR_mmr_act_mode ~= 0 then
			efis_disagree = 1
		end
	end
	
	
	
	if efis_disagree == 0 then
		B738DR_efis_disagree = 0
		if is_timer_scheduled(timer_efis_disagr) == true then
			stop_timer(timer_efis_disagr)
		end
	else
		if B738DR_efis_disagree == 0 then
			if is_timer_scheduled(timer_efis_disagr) == false then
				run_after_time(timer_efis_disagr, 1.8)
			end
		end
	end
	
	efis_disagree = 0
	
	nav_type_vor = B738DR_tuned_nav2_type
	B738DR_nav_vor_fo = nav_type_vor
	
	if B738DR_fo_map_mode == 0 then
		-- APP
		if B738DR_mmr_act_mode_fo == 0 then
			if B738DR_ian_info_fo == 0 then
				efis_disagree = 1
			end
		end
	elseif B738DR_fo_map_mode == 1 then
		-- VOR
		if B738DR_mmr_act_mode_fo ~= 0 then
			efis_disagree = 1
		end
	end
	
	if efis_disagree == 0 then
		B738DR_efis_disagree_fo = 0
		if is_timer_scheduled(timer_efis_disagr_fo) == true then
			stop_timer(timer_efis_disagr_fo)
		end
	else
		if B738DR_efis_disagree_fo == 0 then
			if is_timer_scheduled(timer_efis_disagr_fo) == false then
				run_after_time(timer_efis_disagr_fo, 1.8)
			end
		end
	end
	
	
	local adjust_y = 0
	local nd_zoom = 0
	local track_nd_dspl = 0
	local relative_brg = 0
	if cpt_pos_enable ~= 0 then
		if B738DR_capt_map_mode == 2 then
			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
			if B738DR_capt_vsd_map_mode == 1 then
				adjust_y = 5.1	-- adjust center
			elseif B738DR_capt_exp_map_mode == 0 then
				adjust_y = 4.1	-- adjust center
			end
			if simDR_gps_fail == 0 or simDR_gps2_fail == 0 then
				B738DR_gps1_pos_show = 1
				B738DR_gps1_pos_x = 0
				B738DR_gps1_pos_y = adjust_y
			else
				B738DR_gps1_pos_show = 0
			end
			if B738DR_irs_nd_allign == 0 then
				B738DR_adiru1_pos_show = 1
				B738DR_adiru1_pos_x = 0.04 * nd_zoom
				B738DR_adiru1_pos_y = -0.2 * nd_zoom + adjust_y
			else
				B738DR_adiru1_pos_show = 0
			end
			if B738DR_irs_nd_allign2 == 0 then
				B738DR_adiru2_pos_show = 1
				B738DR_adiru2_pos_x = -0.01 * nd_zoom
				B738DR_adiru2_pos_y = 0.17 * nd_zoom + adjust_y
			else
				B738DR_adiru2_pos_show = 0
			end
			
			
			if B738DR_track_up == 0 then
				-- HDG-UP
				track_nd_dspl = simDR_ahars_mag_hdg
			else
				-- TRK-UP
				if simDR_ground_spd < 45 then
					track_nd_dspl = simDR_ahars_mag_hdg
				else
					track_nd_dspl = simDR_mag_hdg
				end
			end
			
			if B738DR_vor1_show ~= 0 then
				relative_brg = (simDR_nav1_bearing - track_nd_dspl + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				B738DR_vor1_pos_show = 1
				B738DR_vor1_pos_x = 0
				B738DR_vor1_pos_y = adjust_y
				B738DR_vor1_pos_crs = "R-" .. string.format("%03d", (simDR_nav1_bearing + 180) % 360)
				if relative_brg <= 0 then
					B738DR_vor1_pos_rotate = (90 + relative_brg + 360) % 360
					B738DR_vor1_pos_lr = 0
				else
					B738DR_vor1_pos_rotate = (90 + relative_brg + 360 + 180) % 360
					B738DR_vor1_pos_lr = 1
				end
			else
				B738DR_vor1_pos_show = 0
			end
			
			if B738DR_vor2_show ~= 0 then
				relative_brg = (simDR_nav2_bearing - track_nd_dspl + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				B738DR_vor2_pos_show = 1
				B738DR_vor2_pos_x = 0
				B738DR_vor2_pos_y = adjust_y
				B738DR_vor2_pos_crs = "R-" .. string.format("%03d", (simDR_nav2_bearing + 180) % 360)
				if relative_brg <= 0 then
					B738DR_vor2_pos_rotate = (90 + relative_brg + 360) % 360
					B738DR_vor2_pos_lr = 0
				else
					B738DR_vor2_pos_rotate = (90 + relative_brg + 360 + 180) % 360
					B738DR_vor2_pos_lr = 1
				end
			else
				B738DR_vor2_pos_show = 0
			end
		else
			B738DR_gps1_pos_show = 0
			B738DR_adiru1_pos_show = 0
			B738DR_adiru2_pos_show = 0
			B738DR_vor1_pos_show = 0
			B738DR_vor2_pos_show = 0
		end
		B738DR_gps2_pos_show = 0
	else
		B738DR_gps1_pos_show = 0
		B738DR_adiru1_pos_show = 0
		B738DR_gps2_pos_show = 0
		B738DR_adiru2_pos_show = 0
		B738DR_vor1_pos_show = 0
		B738DR_vor2_pos_show = 0
	end
	
	if fo_pos_enable ~= 0 then
		adjust_y = 0
		if B738DR_fo_map_mode == 2 then
			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
			if B738DR_fo_vsd_map_mode == 1 then
				adjust_y = 5.1	-- adjust center
			elseif B738DR_fo_exp_map_mode == 0 then
				adjust_y = 4.1	-- adjust center
			end
			if simDR_gps_fail == 0 or simDR_gps2_fail == 0 then
				B738DR_gps2_pos_fo_show = 1
				B738DR_gps2_pos_x_fo = 0
				B738DR_gps2_pos_y_fo = adjust_y
			else
				B738DR_gps2_pos_fo_show = 0
			end
			if B738DR_irs_nd_allign == 0 then
				B738DR_adiru1_pos_fo_show = 1
				B738DR_adiru1_pos_x_fo = 0.04 * nd_zoom
				B738DR_adiru1_pos_y_fo = -0.2 * nd_zoom + adjust_y
			else
				B738DR_adiru2_pos_fo_show = 0
			end
			if B738DR_irs_nd_allign2 == 0 then
				B738DR_adiru2_pos_fo_show = 1
				B738DR_adiru2_pos_x_fo = -0.01 * nd_zoom
				B738DR_adiru2_pos_y_fo = 0.17 * nd_zoom + adjust_y
			else
				B738DR_adiru2_pos_fo_show = 0
			end
			
			if B738DR_track_up == 0 then
				-- HDG-UP
				track_nd_dspl = simDR_ahars_mag_hdg
			else
				-- TRK-UP
				if simDR_ground_spd < 45 then
					track_nd_dspl = simDR_ahars_mag_hdg
				else
					track_nd_dspl = simDR_mag_hdg
				end
			end
			
			if B738DR_vor1_show ~= 0 then
				relative_brg = (simDR_nav1_bearing - track_nd_dspl + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				B738DR_vor1_pos_fo_show = 1
				B738DR_vor1_pos_x_fo = 0
				B738DR_vor1_pos_y_fo = adjust_y
				B738DR_vor1_pos_crs_fo = "R-" .. string.format("%03d", (simDR_nav1_bearing + 180) % 360)
				if relative_brg <= 0 then
					B738DR_vor1_pos_rotate_fo = (90 + relative_brg + 360) % 360
					B738DR_vor1_pos_lr_fo = 0
				else
					B738DR_vor1_pos_rotate_fo = (90 + relative_brg + 360 + 180) % 360
					B738DR_vor1_pos_lr_fo = 1
				end
			else
				B738DR_vor1_pos_fo_show = 0
			end
			
			if B738DR_vor2_show ~= 0 then
				relative_brg = (simDR_nav2_bearing - track_nd_dspl + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				B738DR_vor2_pos_fo_show = 1
				B738DR_vor2_pos_x_fo = 0
				B738DR_vor2_pos_y_fo = adjust_y
				B738DR_vor2_pos_crs_fo = "R-" .. string.format("%03d", (simDR_nav2_bearing + 180) % 360)
				if relative_brg <= 0 then
					B738DR_vor2_pos_rotate_fo = (90 + relative_brg + 360) % 360
					B738DR_vor2_pos_lr_fo = 0
				else
					B738DR_vor2_pos_rotate_fo = (90 + relative_brg + 360 + 180) % 360
					B738DR_vor2_pos_lr_fo = 1
				end
			else
				B738DR_vor2_pos_fo_show = 0
			end
		else
			B738DR_gps2_pos_fo_show = 0
			B738DR_adiru2_pos_fo_show = 0
			B738DR_adiru1_pos_fo_show = 0
			B738DR_vor1_pos_fo_show = 0
			B738DR_vor2_pos_fo_show = 0
		end
		B738DR_gps1_pos_fo_show = 0
	else
		B738DR_gps2_pos_fo_show = 0
		B738DR_adiru2_pos_fo_show = 0
		B738DR_gps1_pos_fo_show = 0
		B738DR_adiru1_pos_fo_show = 0
		B738DR_vor1_pos_fo_show = 0
		B738DR_vor2_pos_fo_show = 0
	end

end

-- Baro altimeter
function B738_efis_baro()

	-- Captain
	if B738DR_baro_set_std_pilot == 1 then	-- set STD
		if baro_sel_old ~= B738DR_baro_sel_in_hg_pilot then
			B738DR_baro_sel_pilot_show = 1
		end
		if B738DR_flight_phase > 3 then
			if simDR_altitude_pilot < B738DR_trans_lvl then
				B738DR_baro_std_box_pilot_show = 1
				B738DR_baro_box_pilot_show = 0
			else
				B738DR_baro_std_box_pilot_show = 0
				B738DR_baro_box_pilot_show = 0
			end
		else
			B738DR_baro_std_box_pilot_show = 0
			B738DR_baro_box_pilot_show = 0
		end
	else
		simDR_barometer_setting_capt = B738DR_baro_sel_in_hg_pilot
		if B738DR_flight_phase < 4 then
			if simDR_altitude_pilot > B738DR_trans_alt 
			and simDR_barometer_setting_capt ~= 29.92 then
				B738DR_baro_box_pilot_show = 1
				B738DR_baro_std_box_pilot_show = 0
			else
				B738DR_baro_box_pilot_show = 0
				B738DR_baro_std_box_pilot_show = 0
			end
		else
			B738DR_baro_box_pilot_show = 0
			B738DR_baro_std_box_pilot_show = 0
		end
	end

	-- First Officer
	if B738DR_baro_set_std_copilot == 1 then	-- set STD
		if baro_sel_co_old ~= B738DR_baro_sel_in_hg_copilot then
			B738DR_baro_sel_copilot_show = 1
		end
		if B738DR_flight_phase > 3 then
			if simDR_altitude_copilot < B738DR_trans_lvl then
				B738DR_baro_std_box_copilot_show = 1
				B738DR_baro_box_copilot_show = 0
			else
				B738DR_baro_std_box_copilot_show = 0
				B738DR_baro_box_copilot_show = 0
			end
		else
			B738DR_baro_std_box_copilot_show = 0
			B738DR_baro_box_copilot_show = 0
		end
	else
		simDR_barometer_setting_fo = B738DR_baro_sel_in_hg_copilot
		if B738DR_flight_phase < 4 then
			if simDR_altitude_copilot > B738DR_trans_alt 
			and simDR_barometer_setting_fo ~= 29.92 then
				B738DR_baro_box_copilot_show = 1
				B738DR_baro_std_box_copilot_show = 0
			else
				B738DR_baro_box_copilot_show = 0
				B738DR_baro_std_box_copilot_show = 0
			end
		else
			B738DR_baro_box_copilot_show = 0
			B738DR_baro_std_box_copilot_show = 0
		end
	end

end

-- function rst_ghust_detect()
	-- ghust_detect = 0
-- end

function radio_height_vvi()
	if simDR_radio_height_pilot_ft < 3000 then
		if radio_height_sum < 64000 then
			radio_height_sum = radio_height_sum + simDR_radio_height_pilot_ft
			radio_height_num = radio_height_num + 1
		end
	else
		radio_height_num = 0
	end
end

function speed_ratio_timer2()
	
	ratio_timer = ratio_timer + SIM_PERIOD
	if ratio_timer > 0.1 then
		B738DR_vvi_pilot_ratio = (simDR_vvi_fpm_pilot - vvi_pilot_old) / ratio_timer
		vvi_pilot_old = simDR_vvi_fpm_pilot
		
		B738DR_eng1_N1_ratio = (B738DR_eng1_N1 - eng1_N1_old) / ratio_timer
		eng1_N1_old = B738DR_eng1_N1
		
		B738DR_eng2_N1_ratio = (B738DR_eng2_N1 - eng2_N1_old) / ratio_timer
		eng2_N1_old = B738DR_eng2_N1
		
		ratio_timer = 0
		radio_height_vvi()
	end
end


-- Speed ratio, Vertical speed ratio
function speed_ratio_timer()

	local airspeed = 0
	if autopilot_cmd_b_status == 1 then
		airspeed = simDR_airspeed_copilot
	else
		airspeed = simDR_airspeed_pilot
	end
	
	B738DR_speed_ratio = airspeed - airspeed_pilot_old
	airspeed_pilot_old = airspeed
	
	local vert_speed = simDR_vvi_fpm_pilot
	B738DR_v_speed_ratio = vert_speed - v_speed_pilot_old
	v_speed_pilot_old = vert_speed
	
	local radio_height = 0
	if radio_height_num == 0 then
		radio_height = simDR_radio_height_pilot_ft
	else
		radio_height = radio_height_sum / radio_height_num
	end
	local radio_height_ratio = (radio_height - radio_height_old) * 60	-- fpm
	if simDR_vvi_fpm_pilot < 0 then
		radio_height_ratio = radio_height_ratio - simDR_vvi_fpm_pilot
	end
	radio_height_old = radio_height
	radio_height_num = 0
	radio_height_sum = 0
	
	if radio_h_tick == 0 then
		radio_h_ratio_s = radio_height_ratio
		radio_h_tick = 1
	elseif radio_h_tick < 6 then
		radio_h_ratio_s = radio_h_ratio_s + radio_height_ratio
		radio_h_tick = radio_h_tick + 1
	else
		radio_h_ratio_s = radio_h_ratio_s + radio_height_ratio
		B738DR_radio_height_ratio = radio_h_ratio_s / radio_h_tick
		radio_h_tick = 0
	end
	
	local altitude_pilot = simDR_altitude_pilot
	B738DR_altitude_pilot_ratio = altitude_pilot - altitude_pilot_old
	altitude_pilot_old = altitude_pilot
	
	local air_on_acf = simDR_air_on_acf
	B738DR_air_on_acf_ratio = air_on_acf - air_on_acf_old
	air_on_acf_old = air_on_acf
	
	local wind_hdg_ratio = simDR_wind_hdg - wind_hdg_old
	if wind_hdg_ratio < 0 then
		wind_hdg_ratio = -wind_hdg_ratio
	end
	wind_hdg_ratio = wind_hdg_ratio % 360
	if wind_hdg_ratio > 180 then
		wind_hdg_ratio = wind_hdg_ratio - 360
	end
	local wind_hdg_rel_ratio = wind_hdg_ratio - wind_hdg_rel_old
	if wind_hdg_rel_ratio < 0 then
		wind_hdg_rel_ratio = -wind_hdg_rel_ratio
	end
	
	wind_hdg_old = simDR_wind_hdg
	wind_hdg_rel_old = wind_hdg_ratio
	
	local wind_spd_ratio = simDR_wind_spd - wind_spd_old
	if wind_spd_ratio < 0 then
		wind_spd_ratio = -wind_spd_ratio
	end
	wind_spd_old = simDR_wind_spd
	
	-- if wind_hdg_rel_ratio > 5 or wind_spd_ratio > 2.1 then
		-- ghust_detect = 1
		-- if is_timer_scheduled(rst_ghust_detect) == true then
			-- stop_timer(rst_ghust_detect)
		-- end
		-- run_after_time(rst_ghust_detect, 10)
	-- else
		-- if is_timer_scheduled(rst_ghust_detect) == false then
			-- ghust_detect = 0
		-- end
	-- end
	
end

function B738_radio_height()
	
	local height = 0
	
	-- pilot
	height = simDR_radio_height_pilot_ft - 4
	if height < 100 then
		B738DR_agl_pilot = roundDownToIncrement(height, 2) + 4
	elseif height < 500 then
		B738DR_agl_pilot = roundDownToIncrement(height, 10) + 4
	else
		B738DR_agl_pilot = roundDownToIncrement(height, 20) + 4
	end
	
	-- copilot
	height = simDR_radio_height_copilot_ft - 4
	if height < 100 then
		B738DR_agl_copilot = roundDownToIncrement(height, 2) + 4
	elseif height < 500 then
		B738DR_agl_copilot = roundDownToIncrement(height, 10) + 4
	else
		B738DR_agl_copilot = roundDownToIncrement(height, 20) + 4
	end
end

function dh_min_off_pilot()
	dh_min_enable_pilot = 1
end

function dh_min_off_copilot()
	dh_min_enable_copilot = 1
end


function B738_baro_radio2()
	
	local alt_dh = 0
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		dh_min_block_pilot = 1
		dh_min_block_copilot = 1
	end
	-- captain
	
	if B738DR_minim_capt == 0 then	--radio DH
		if baro_dh_pilot < -1000 then
			B738DR_baro_min_cpt_show = -1
		else
			B738DR_baro_min_cpt_show = 0
		end
		if B738DR_dh_pilot < 0 then
			simDR_dh_pilot = 0
			dh_minimum_pilot = 0
		else
			simDR_dh_pilot = B738DR_dh_pilot
			if simDR_radio_height_pilot_ft < B738DR_dh_pilot and simDR_radio_height_pilot_ft > 0 then
				dh_minimum_pilot = 1
			end
			alt_dh = B738DR_dh_pilot + 75
			if simDR_radio_height_pilot_ft > alt_dh then
				dh_minimum_pilot = 0
				dh_min_block_pilot = 0
			end
		end
	else	-- baro DH
		simDR_dh_pilot = 0
		if baro_dh_pilot < -1000 then
			B738DR_baro_min_cpt_show = -1
		else
			B738DR_baro_min_cpt_show = 1
		end
		if B738DR_dh_pilot < -1000 then
			dh_minimum_pilot = 0
		else
			if simDR_altitude_pilot < B738DR_dh_pilot and simDR_altitude_pilot > -1000 then
				dh_minimum_pilot = 1
			end
			alt_dh = B738DR_dh_pilot + 75
			if simDR_altitude_pilot > alt_dh then
				dh_minimum_pilot = 0
				dh_min_block_pilot = 0
			end
		end
	end
	
	local dh_flash = 0
	if dh_minimum_pilot == 0 or dh_min_block_pilot == 1 then
		dh_flash = 0		-- green
		dh_min_enable_pilot = 0
		if is_timer_scheduled(dh_min_off_pilot) == true then
			stop_timer(dh_min_off_pilot)
		end
	else
		if dh_min_enable_pilot == 0 then
			if B738DR_blink == 0 then
				dh_flash = 1		-- amber
			else
				dh_flash = 2		-- off
			end
			if is_timer_scheduled(dh_min_off_pilot) == false then
				run_after_time(dh_min_off_pilot, 3)	-- 3 seconds
			end
		else
			dh_flash = 1		-- amber
		end
	end
	
	if B738DR_minim_capt == 0 then	--radio DH
		B738DR_dh_minimum_pilot2_ra = dh_flash
		B738DR_dh_minimum_pilot2 = 0
	else
		B738DR_dh_minimum_pilot2 = dh_flash
		B738DR_dh_minimum_pilot2_ra = 0
	end
	
	if baro_dh_pilot == -1001 then
		B738DR_baro_min_cpt = 200
	else
		B738DR_baro_min_cpt = baro_dh_pilot
	end
	
	-- first officer
	if B738DR_minim_fo == 0 then	--radio DH
		if baro_dh_copilot < -1000 then
			B738DR_baro_min_fo_show = -1
		else
			B738DR_baro_min_fo_show = 0
		end
		if B738DR_dh_copilot < 0 then
			simDR_dh_copilot = 0
			dh_minimum_copilot = 0
		else
			simDR_dh_copilot = B738DR_dh_copilot
			if simDR_radio_height_copilot_ft < B738DR_dh_copilot and simDR_radio_height_copilot_ft > 0 then
				dh_minimum_copilot = 1
			end
			alt_dh = B738DR_dh_copilot + 75
			if simDR_radio_height_copilot_ft > alt_dh then
				dh_minimum_copilot = 0
				dh_min_block_copilot = 0
			end
		end
	else	-- baro DH
		first_time_baro_fo = 1
		simDR_dh_copilot = 0
		if B738DR_dh_copilot < -1000 then
			simDR_dh_copilot = 0
			dh_minimum_copilot = 0
			B738DR_baro_min_fo_show = -1
		else
			B738DR_baro_min_fo_show = 1
			if simDR_altitude_copilot < B738DR_dh_copilot and simDR_altitude_copilot > -1000 then
				dh_minimum_copilot = 1
			end
			alt_dh = B738DR_dh_copilot + 75
			if simDR_altitude_copilot > alt_dh then
				dh_minimum_copilot = 0
				dh_min_block_copilot = 0
			end
		end
	end
	
	dh_flash = 0
	if dh_minimum_copilot == 0 or dh_min_block_copilot == 1 then
		dh_flash = 0		-- green
		dh_min_enable_copilot = 0
		if is_timer_scheduled(dh_min_off_copilot) == true then
			stop_timer(dh_min_off_copilot)
		end
	else
		if dh_min_enable_copilot == 0 then
			if B738DR_blink == 0 then
				dh_flash = 1		-- amber
			else
				dh_flash = 2		-- off
			end
			if is_timer_scheduled(dh_min_off_copilot) == false then
				run_after_time(dh_min_off_copilot, 3)	-- 3 seconds
			end
		else
			dh_flash = 1		-- amber
		end
	end
	
	if B738DR_minim_fo == 0 then	--radio DH
		B738DR_dh_minimum_copilot2_ra = dh_flash
		B738DR_dh_minimum_copilot2 = 0
	else
		B738DR_dh_minimum_copilot2 = dh_flash
		B738DR_dh_minimum_copilot2_ra = 0
	end
	
	if baro_dh_copilot == -1001 then
		B738DR_baro_min_fo = 200
	else
		B738DR_baro_min_fo = baro_dh_copilot
	end
	
end

-- ILS info on PFD
function ils_radio_pfd()

	if B738DR_autopilot_vhf_source_pos == -1 then
		B738DR_pfd_nav1_pilot = 1
		B738DR_pfd_nav2_pilot = 0
		B738DR_pfd_nav1_copilot = 1
		B738DR_pfd_nav2_copilot = 0
	elseif B738DR_autopilot_vhf_source_pos == 0 then
		B738DR_pfd_nav1_pilot = 1
		B738DR_pfd_nav2_pilot = 0
		B738DR_pfd_nav1_copilot = 0
		B738DR_pfd_nav2_copilot = 1
	elseif B738DR_autopilot_vhf_source_pos == 1 then
		B738DR_pfd_nav1_pilot = 0
		B738DR_pfd_nav2_pilot = 1
		B738DR_pfd_nav1_copilot = 0
		B738DR_pfd_nav2_copilot = 1
	end
	
	-- LOC expanded
	local delta_crs_loc = 0
	local not_loc = 0
	
	if B738DR_mmr_cpt_act_mode == 1 then
		if B738DR_pfd_hdg_mode ~= PFD_HDG_VOR_LOC then
			pfd_loc_exp = 0
		elseif B738DR_nav_horz_dsp == 0 then
			if simDR_ground_spd < 15 or simDR_radio_height_pilot_ft > 200 then
				pfd_loc_exp = 0
			end
		elseif B738DR_autopilot_vhf_source_pos <= 0 then
			not_loc = 0
			if B738DR_tuned_nav1_type < 2 then
				not_loc = 1
			end
			if not_loc == 0 then
				-- delta_crs_loc = math.abs(B738DR_course_pilot - simDR_mag_hdg)
				delta_crs_loc = (B738DR_course_pilot - simDR_mag_hdg + 360) % 360
				if delta_crs_loc > 180 then
					delta_crs_loc = delta_crs_loc - 360
				end
				if delta_crs_loc < 0 then
					delta_crs_loc = -delta_crs_loc
				end
				if math.abs(simDR_nav1_hdef_pilot) > 0.8 or delta_crs_loc > 8 then
					pfd_loc_exp = 0
				elseif math.abs(simDR_nav1_hdef_pilot) < 0.6 and delta_crs_loc <= 5 then
					pfd_loc_exp = 1
				end
			else
				pfd_loc_exp = 0
			end
		else
			not_loc = 0
			if B738DR_tuned_nav2_type < 2 then
				not_loc = 1
			end
			if not_loc == 0 then
				-- delta_crs_loc = math.abs(B738DR_course_pilot - simDR_mag_hdg)
				delta_crs_loc = (B738DR_course_pilot - simDR_mag_hdg + 360) % 360
				if delta_crs_loc > 180 then
					delta_crs_loc = delta_crs_loc - 360
				end
				if delta_crs_loc < 0 then
					delta_crs_loc = -delta_crs_loc
				end
				if math.abs(simDR_nav2_hdef_pilot) > 0.8 or delta_crs_loc > 8 then
					pfd_loc_exp = 0
				elseif math.abs(simDR_nav2_hdef_pilot) < 0.6 and delta_crs_loc <= 5 then
					pfd_loc_exp = 1
				end
			else
				pfd_loc_exp = 0
			end
		end
	else
		pfd_loc_exp = 0
	end
	
	if B738DR_mmr_fo_act_mode == 1 then
		if B738DR_pfd_hdg_mode ~= PFD_HDG_VOR_LOC then
			pfd_loc_exp_fo = 0
		elseif B738DR_nav_horz_dsp_fo == 0 then
			if simDR_ground_spd < 15 or simDR_radio_height_copilot_ft > 200 then
				pfd_loc_exp_fo = 0
			end
		elseif B738DR_autopilot_vhf_source_pos <= -1 then
			not_loc = 0
			if B738DR_tuned_nav1_type < 2 then
				not_loc = 1
			end
			if not_loc == 0 then
				-- delta_crs_loc = math.abs(B738DR_course_copilot - simDR_mag_hdg)
				delta_crs_loc = (B738DR_course_copilot - simDR_mag_hdg + 360) % 360
				if delta_crs_loc > 180 then
					delta_crs_loc = delta_crs_loc - 360
				end
				if delta_crs_loc < 0 then
					delta_crs_loc = -delta_crs_loc
				end
				if math.abs(simDR_nav1_hdef_copilot) > 0.8 or delta_crs_loc > 8 then
					pfd_loc_exp_fo = 0
				elseif math.abs(simDR_nav1_hdef_copilot) < 0.6 and delta_crs_loc <= 5 then
					pfd_loc_exp_fo = 1
				end
			else
				pfd_loc_exp_fo = 0
			end
		else
			not_loc = 0
			if B738DR_tuned_nav2_type < 2 then
				not_loc = 1
			end
			if not_loc == 0 then
				-- delta_crs_loc = math.abs(B738DR_course_copilot - simDR_mag_hdg)
				delta_crs_loc = (B738DR_course_copilot - simDR_mag_hdg + 360) % 360
				if delta_crs_loc > 180 then
					delta_crs_loc = delta_crs_loc - 360
				end
				if delta_crs_loc < 0 then
					delta_crs_loc = -delta_crs_loc
				end
				if math.abs(simDR_nav2_hdef_copilot) > 0.8 or delta_crs_loc > 8 then
					pfd_loc_exp_fo = 0
				elseif math.abs(simDR_nav2_hdef_copilot) < 0.6 and delta_crs_loc <= 5 then
					pfd_loc_exp_fo = 1
				end
			else
				pfd_loc_exp_fo = 0
			end
		end
	else
		pfd_loc_exp_fo = 0
	end
	
	B738DR_pfd_loc_exp = pfd_loc_exp
	B738DR_pfd_loc_exp_fo = pfd_loc_exp_fo
	B738DR_pfd_gls_exp = pfd_gls_exp
	B738DR_pfd_gls_exp_fo = pfd_gls_exp_fo
	
	pfd_fac_exp = 0
	pfd_fac_exp_fo = 0
	B738DR_pfd_fac_exp = pfd_fac_exp
	B738DR_pfd_fac_exp_fo = pfd_fac_exp_fo

end

function pfd_spd_tapes(in_value)
	
	local digit = in_value
	
	if digit > 999 then
		digit = 999
	elseif digit < 45 then
		digit = 45
	end
	local digit1 = math.floor(digit/100)
	local frac1 = digit * 0.01
	frac1 = frac1 - math.floor(frac1)
	if frac1 < 0.99 then
		frac1 = 0
	elseif frac1 > 0.999 then
		frac1 = 1
	else
		frac1 = B738_rescale(0.99, 0, 0.999, 1.0, frac1)
	end
	
	local digit2 = digit - (digit1 * 100)
	digit2 = math.floor(digit2/10)
	local frac2 = digit * 0.1
	frac2 = frac2 - math.floor(frac2)
	if frac2 < 0.9 then
		frac2 = 0
	elseif frac1 > 0.99 then
		frac1 = 1
	else
		frac2 = B738_rescale(0.9, 0, 0.99, 1.0, frac2)
	end
	
	local digit3 = digit - (digit1 * 100) - (digit2 * 10)
	
	return digit1 + frac1, digit2 + frac2, digit3
end

function pfd_alt_tapes(in_value)
	
	local digit = 0
	local digit1 = 0
	local digit2 = 0
	local digit3 = 0
	local frac1 = 0
	local frac2 = 0
	local frac3 = 0
	
	if in_value < 0 then
		digit = -in_value
		if digit > 9999 then
			digit = 9999
		end
	else
		digit = in_value
		if digit > 99999 then
			digit = 99999
		end
	end
	
	digit1 = math.floor(digit/10000)
	frac1 = digit * 0.0001
	frac1 = frac1 - math.floor(frac1)
	if frac1 < 0.998 then
		frac1 = 0
	elseif frac1 > 0.9999 then
		frac1 = 1
	else
		frac1 = B738_rescale(0.998, 0, 0.9999, 1.0, frac1)
	end
	
	digit2 = digit - (digit1 * 10000)
	digit2 = math.floor(digit2/1000)
	frac2 = digit * 0.001
	frac2 = frac2 - math.floor(frac2)
	if frac2 < 0.98 then
		frac2 = 0
	elseif frac2 > 0.999 then
		frac2 = 1
	else
		frac2 = B738_rescale(0.98, 0, 0.999, 1.0, frac2)
	end
	
	digit3 = digit - (digit1 * 10000) - (digit2 * 1000)
	digit3 = math.floor(digit3/100)
	frac3 = digit * 0.01
	frac3 = frac3 - math.floor(frac3)
	if frac3 < 0.8 then
		frac3 = 0
	elseif frac3 > 0.99 then
		frac3 = 1
	else
		frac3 = B738_rescale(0.8, 0, 0.99, 1.0, frac3)
	end
	
	digit1 = digit1 + frac1
	digit2 = digit2 + frac2
	digit3 = digit3 + frac3
	
	if in_value < 0 then
		digit1 = 0	-- -digit1
		if digit2 > 9 then
			digit2 = -9
		else
			digit2 = -digit2
		end
		if digit3 > 9 then
			digit3 = -9
		else
			digit3 = -digit3
		end
	end
	
	return digit1, digit2, digit3
end

function stdby_alt_tapes(in_value)
	
	local digit = 0
	local digit1 = 0
	local digit2 = 0
	local frac1 = 0
	local frac2 = 0
	
	if in_value < 0 then
		digit = -in_value
		if digit > 9999 then
			digit = 9999
		end
	else
		digit = in_value
		if digit > 99999 then
			digit = 99999
		end
	end
	
	digit1 = math.floor(digit/10000)
	frac1 = digit * 0.0001
	frac1 = frac1 - math.floor(frac1)
	if frac1 < 0.998 then
		frac1 = 0
	elseif frac1 > 0.9999 then
		frac1 = 1
	else
		frac1 = B738_rescale(0.998, 0, 0.9999, 1.0, frac1)
	end
	
	digit2 = digit - (digit1 * 10000)
	digit2 = math.floor(digit2/1000)
	frac2 = digit * 0.001
	frac2 = frac2 - math.floor(frac2)
	if frac2 < 0.98 then
		frac2 = 0
	elseif frac2 > 0.999 then
		frac2 = 1
	else
		frac2 = B738_rescale(0.998, 0, 0.9999, 1.0, frac2)
	end
	
	digit1 = digit1 + frac1
	digit2 = digit2 + frac2
	
	if in_value < 0 then
		digit1 = 0	-- -digit1
		if digit2 > 9 then
			digit2 = -9
		else
			digit2 = -digit2
		end
	end
	
	return digit1, digit2
end

function B738_pfd_tapes()
	local dig1 = 0
	local dig2 = 0
	local dig3 = 0
	
	dig1, dig2, dig3 = pfd_spd_tapes(math.min(simDR_airspeed_pilot, 450))
	B738DR_pfd_ias_dig1_pilot = dig1
	B738DR_pfd_ias_dig2_pilot = dig2
	B738DR_pfd_ias_dig3_pilot = dig3
	
	dig1, dig2, dig3 = pfd_spd_tapes(math.min(simDR_airspeed_copilot, 450))
	B738DR_pfd_ias_dig1_copilot = dig1
	B738DR_pfd_ias_dig2_copilot = dig2
	B738DR_pfd_ias_dig3_copilot = dig3
	
	dig1, dig2, dig3 = pfd_alt_tapes(simDR_altitude_pilot)
	B738DR_pfd_alt_dig1_pilot = dig1
	B738DR_pfd_alt_dig2_pilot = dig2
	B738DR_pfd_alt_dig3_pilot = dig3
	
	dig1, dig2, dig3 = pfd_alt_tapes(simDR_altitude_copilot)
	B738DR_pfd_alt_dig1_copilot = dig1
	B738DR_pfd_alt_dig2_copilot = dig2
	B738DR_pfd_alt_dig3_copilot = dig3
	
	dig1, dig2 = stdby_alt_tapes(B738DR_standby_altimeter)
	B738DR_stdby_alt_dig1 = dig1
	B738DR_stdby_alt_dig2 = dig2
end

function B738_flight_director()
	
	if simDR_fdir_pitch_ovr ~= fdir_pitch_ovr_old then
		fdir_pitch_ovr_old = simDR_fdir_pitch_ovr
		if fdir_pitch_ovr_old == 0 then
			fdir_pitch_anim = 1
		else
			fdir_pitch_anim = 0
		end
	end
	
	if ap_roll_mode == 0 then
		simDR_fdir_roll = 0
	end
	-- if ap_pitch_mode == 0 or B738DR_pfd_alt_mode_arm == PFD_ALT_VNAV_ARM then
		-- if B738DR_single_cue_fd ~= 0 then
			-- if simDR_fpm < 500 then
				-- simDR_fdir_pitch = simDR_ahars_pitch_deg_pilot
			-- end
		-- end
	-- end
	
	B738DR_fdir_pitch_main = simDR_fdir_pitch
	B738DR_fdir_roll_main = simDR_fdir_roll
	
	local fdir_temp = 0
	local fdir_temp2 = 0
	
	fdir_temp = B738DR_fdir_pitch_main - simDR_ahars_pitch_deg_pilot
	fdir_temp = math.max(fdir_temp, -15)
	fdir_temp = math.min(fdir_temp, 15)
	
	fdir_temp2 = B738DR_fdir_pitch_main - simDR_ahars_pitch_deg_copilot
	fdir_temp2 = math.max(fdir_temp2, -15)
	fdir_temp2 = math.min(fdir_temp2, 15)
	
	local fdir_roll_temp = 0
	local fdir_roll_temp2 = 0
	
	fdir_roll_temp = B738DR_fdir_roll_main - simDR_roll_AHARS_deg
	fdir_roll_temp = math.max(fdir_roll_temp, -30)
	fdir_roll_temp = math.min(fdir_roll_temp, 30)
	
	fdir_roll_temp2 = B738DR_fdir_roll_main - simDR_roll_AHARS_deg_copilot
	fdir_roll_temp2 = math.max(fdir_roll_temp2, -30)
	fdir_roll_temp2 = math.min(fdir_roll_temp2, 30)
	
	if fdir_pitch_anim == 0 then
		B738DR_fdir_pitch_pilot = fdir_temp
		B738DR_fdir_pitch_copilot = fdir_temp2
	else
		if math.abs(B738DR_fdir_pitch_pilot - fdir_temp) < 1 then
			fdir_pitch_anim = 0
		end
		if math.abs(B738DR_fdir_pitch_copilot - fdir_temp2) < 1 then
			fdir_pitch_anim = 0
		end
		B738DR_fdir_pitch_pilot = B738_set_anim_value(B738DR_fdir_pitch_pilot, fdir_temp, -15.0, 15.0, 2)
		B738DR_fdir_pitch_copilot = B738_set_anim_value(B738DR_fdir_pitch_copilot, fdir_temp2, -15.0, 15.0, 2)
	end
	B738DR_fdir_roll_pilot = fdir_roll_temp
	B738DR_fdir_roll_copilot = fdir_roll_temp2
	
	if B738DR_fd_helper == 0 or ap_on == 1 or simDR_radio_height_pilot_ft < 50 then
		B738DR_fdir_roll_helper = 0
		B738DR_fdir_pitch_helper = 0
		B738DR_fdir_roll_helper_fo = 0
		B738DR_fdir_pitch_helper_fo = 0
	else
		if autopilot_master_capt_status == 1 or autopilot_master_fo_status == 1 then
			local fdir_roll_helper = 0
			local fdir_pitch_helper = 0
			local fd_tollerance = 12
			local fd_tollerance2 = 7
			if B738DR_ap_yoke_tolerance ~= 0 then
				fd_tollerance = 18
				fd_tollerance2 = 13
			end
			local test_input = math.abs(simDR_roll_AHARS_deg - B738DR_fdir_roll_main)
			if test_input > fd_tollerance then
				fdir_roll_helper = 1 * B738DR_blink
			else
				fdir_roll_helper = 0
			end
			test_input = math.abs(simDR_ahars_pitch_deg_pilot - B738DR_fdir_pitch_main)	--B738DR_fdir_pitch_pilot
			if test_input > fd_tollerance2 then
				fdir_pitch_helper = 1 * B738DR_blink
			else
				fdir_pitch_helper = 0
			end
			if autopilot_master_capt_status == 1 then
				B738DR_fdir_roll_helper = fdir_roll_helper
				B738DR_fdir_pitch_helper = fdir_pitch_helper
			else
				B738DR_fdir_roll_helper = 0
				B738DR_fdir_pitch_helper = 0
			end
			if autopilot_master_fo_status == 1 then
				B738DR_fdir_roll_helper_fo = fdir_roll_helper
				B738DR_fdir_pitch_helper_fo = fdir_pitch_helper
			else
				B738DR_fdir_roll_helper_fo = 0
				B738DR_fdir_pitch_helper_fo = 0
			end
		else
			B738DR_fdir_roll_helper = 0
			B738DR_fdir_pitch_helper = 0
			B738DR_fdir_roll_helper_fo = 0
			B738DR_fdir_pitch_helper_fo = 0
		end
	end
end

function B738_hyd_sys()
	
	if loaded_flight_timer == 0 then
		-- CMD A and CMD B (autoland)
		if B738DR_hyd_A_status == 0 or B738DR_hyd_B_status == 0 
		or B738DR_dc_stdbus_status == 0 or B738DR_dc_bus2_status == 0 then
			if autopilot_cmd_a_status == 1 and autopilot_cmd_b_status == 1 then
				-- disengage A/P and autoland
					simDR_flight_dir_mode = 1
				-- F/D on
					-- ap_roll_mode = 0
					-- ap_pitch_mode = 0
					autopilot_cmd_a_status = 0
					autopilot_cmd_b_status = 0
					B738DR_autoland_status = 0
					B738DR_flare_status = 0
					B738DR_rollout_status = 0
					B738DR_no_autoland = 0
					B738DR_retard_status = 0
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					at_mode = 0
					vorloc_only = 0
					ils_test_enable = 0
					ils_test_on = 0
					ils_test_ok2 = 0
			end
		end
		-- CMD A
		if B738DR_hyd_A_status == 0 or B738DR_dc_stdbus_status == 0 then
			if autopilot_cmd_a_status == 1 or autopilot_cws_a_status == 1 then
				-- disengaged A/P (A)
				simDR_flight_dir_mode = 1
					-- F/D on only
				autopilot_cws_a_status = 0
				autopilot_cmd_a_status = 0
				autopilot_cws_status = 0
				autopilot_cws_r_status = 0
				autopilot_cws_p_status = 0
				B738DR_single_ch_status = 0
				-- ap_roll_mode = 0
				-- ap_pitch_mode = 0
			end
		end
		-- CMD B
		if B738DR_hyd_B_status == 0 or B738DR_dc_bus2_status == 0 then
			if autopilot_cmd_b_status == 1 or autopilot_cws_b_status == 1 then
				-- disengaged A/P (B)
				simDR_flight_dir_mode = 1
					-- F/D on only
				autopilot_cws_b_status = 0
				autopilot_cws_status = 0
				autopilot_cws_r_status = 0
				autopilot_cws_p_status = 0
				autopilot_cmd_b_status = 0
				B738DR_single_ch_status = 0
				-- ap_roll_mode = 0
				-- ap_pitch_mode = 0
				
			end
		end
		
		-- -- A/T disarm
		local on_the_ground = simDR_on_ground[0] + simDR_on_ground[1] + simDR_on_ground[2]
		on_the_ground = math.min(on_the_ground, 1)
		-- if eng1_N1_thrust == 0 or eng2_N1_thrust == 0 or (B738DR_fms_N1_mode == 13 and on_the_ground == 1) then
		if B738DR_fms_N1_mode == 13 and on_the_ground == 1 then
			if B738DR_autopilot_autothr_arm_pos == 1 and autothr_arm_pos == 0 then
				B738DR_autopilot_autothr_arm_pos = 0
				autopilot_autothrottle_status = 0
				at_mode = 0
				simCMD_autothrottle_discon:once()	-- disconnect autothrotle
				at_status = 0
			end
		end
	end

end

function hdg_line_timer()
	hide_hdg_line = 1
end

function hdg_line_timer_fo()
	hide_hdg_line_fo = 1
end

function B738_nd()
	
	local adj_y = 0
	local roll_lim = 0
	local alpha = 0
	
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		B738DR_fpv_horiz = 0
		B738DR_fpv_horiz_fo = 0
		B738DR_fpv_vert = 0
		B738DR_fpv_vert_fo = 0
		B738DR_fpv_vert2 = 0
	else
		B738DR_fpv_horiz = (simDR_ahars_mag_hdg - simDR_mag_hdg + 360) % 360
		if B738DR_fpv_horiz > 180 then
			B738DR_fpv_horiz = B738DR_fpv_horiz - 360
		end
		if B738DR_fpv_horiz > 15 then
			B738DR_fpv_horiz = 15
		elseif B738DR_fpv_horiz < -15 then
			B738DR_fpv_horiz = -15
		end
		B738DR_fpv_horiz_fo = (simDR_ahars_mag_hdg_fo - simDR_mag_hdg_fo + 360) % 360
		if B738DR_fpv_horiz_fo > 180 then
			B738DR_fpv_horiz_fo = B738DR_fpv_horiz_fo - 360
		end
		if B738DR_fpv_horiz_fo > 15 then
			B738DR_fpv_horiz_fo = 15
		elseif B738DR_fpv_horiz_fo < -15 then
			B738DR_fpv_horiz_fo = -15
		end
		
		roll_lim = -simDR_roll_AHARS_deg
		if roll_lim > 89 then
			roll_lim = 89
		elseif roll_lim < -89 then
			roll_lim = -89
		end
		
		alpha = simDR_alpha
		
		adj_y = B738DR_fpv_horiz * 5.2 * math.tan(math.rad(roll_lim))
		
		if B738DR_fpv_horiz < 0 then
			B738DR_fpv_vert = (-alpha * 5.2) * math.cos(math.rad(roll_lim)) + adj_y
		else
			B738DR_fpv_vert = (-alpha * 5.2) * math.cos(math.rad(roll_lim)) - adj_y
		end
		
		if B738DR_fpv_vert < -104 then
			B738DR_fpv_vert = -104
		elseif B738DR_fpv_vert > 78 then
			B738DR_fpv_vert = 78
		end
		
		B738DR_fpv_vert2 = simDR_alpha
		
		roll_lim = -simDR_roll_AHARS_deg_copilot
		if roll_lim > 89 then
			roll_lim = 89
		elseif roll_lim < -89 then
			roll_lim = -89
		end
		
		adj_y = B738DR_fpv_horiz * 5.2 * math.tan(math.rad(roll_lim))
		
		if B738DR_fpv_horiz < 0 then
			B738DR_fpv_vert_fo = (-alpha * 5.2) * math.cos(math.rad(roll_lim)) + adj_y
		else
			B738DR_fpv_vert_fo = (-alpha * 5.2) * math.cos(math.rad(roll_lim)) - adj_y
		end
		
		if B738DR_fpv_vert_fo < -104 then
			B738DR_fpv_vert_fo = -104
		elseif B738DR_fpv_vert_fo > 78 then
			B738DR_fpv_vert_fo = 78
		end
	end
	
	local relative_brg = 0
	
	B738DR_hdg_bug_tape = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg
	if simDR_ground_spd < 45 then
		B738DR_gnd_trk_tape = 0
	else
		B738DR_gnd_trk_tape = simDR_mag_hdg - simDR_ahars_mag_hdg
	end
	
	-- CAPTAIN
	if B738DR_capt_map_mode < 2 then
		B738DR_mcp_hdg_dial_nd = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg
		B738DR_nd_crs = B738DR_course_pilot - simDR_ahars_mag_hdg
		if B738DR_capt_exp_map_mode == 0 then
			B738DR_mcp_hdg_dial_nd_show = 0
		else
			B738DR_mcp_hdg_dial_nd_show = 1
		end
		if cpt_tcas_enable == 0 then
			B738DR_EFIS_TCAS_on = 0
		else
			B738DR_EFIS_TCAS_on = 1
		end
	elseif B738DR_capt_map_mode == 2 then
		if B738DR_track_up == 0 then
			-- HDG-UP
			B738DR_mcp_hdg_dial_nd = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg
			B738DR_nd_crs = B738DR_course_pilot - simDR_ahars_mag_hdg
			if B738DR_capt_exp_map_mode == 0 then
				B738DR_mcp_hdg_dial_nd_show = 2
			else
				B738DR_mcp_hdg_dial_nd_show = 1
			end
		else
			-- TRK-UP
			if simDR_ground_spd < 45 then
				-- TRK/HDG
				B738DR_mcp_hdg_dial_nd = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg
				B738DR_nd_crs = B738DR_course_pilot - simDR_ahars_mag_hdg
				if B738DR_capt_exp_map_mode == 0 then
					B738DR_mcp_hdg_dial_nd_show = 2
				else
					B738DR_mcp_hdg_dial_nd_show = 1
				end
			else
				B738DR_mcp_hdg_dial_nd = B738DR_mcp_hdg_dial - simDR_mag_hdg
				if B738DR_capt_exp_map_mode == 0 then
					B738DR_mcp_hdg_dial_nd_show = 2
				else
					B738DR_mcp_hdg_dial_nd_show = 1
				end
			end
		end
		
		if cpt_tcas_enable == 0 then
			B738DR_EFIS_TCAS_on = 0
		else
			B738DR_EFIS_TCAS_on = 1
		end
		
	else
		B738DR_mcp_hdg_dial_nd_show = 0
		B738DR_EFIS_TCAS_on = 0
	end
	
	if simDR_ground_spd < 45 then
		B738DR_track_up_active = 0
	else
		B738DR_track_up_active = 1
	end
	-- ND basic
		if B738DR_capt_map_mode <= 2 then
			if B738DR_capt_map_mode == 2 then
				-- MAP mode
				if B738DR_track_up == 0 then
					-- HDG-UP
					B738DR_hdg_mag_nd = -simDR_ahars_mag_hdg
					relative_brg = (simDR_ahars_mag_hdg - simDR_mag_hdg + 360) % 360
					if relative_brg > 180 then
						relative_brg = relative_brg - 360
					end
					if simDR_ground_spd < 45 then
						B738DR_track_nd = 0
					else
						B738DR_track_nd = relative_brg
					end
					B738DR_hdg_bug_nd = 0
				else
					-- TRK-UP
					if simDR_ground_spd < 45 then
						-- TRK/HDG
						B738DR_hdg_mag_nd = -simDR_ahars_mag_hdg
						relative_brg = (simDR_ahars_mag_hdg - simDR_mag_hdg + 360) % 360
						if relative_brg > 180 then
							relative_brg = relative_brg - 360
						end
						B738DR_hdg_bug_nd = 0
						B738DR_track_nd = 0
					else
						-- TRK
						B738DR_hdg_mag_nd = -simDR_mag_hdg
						B738DR_track_nd = 0
						relative_brg = (simDR_ahars_mag_hdg - simDR_mag_hdg + 360) % 360
						if relative_brg > 180 then
							relative_brg = relative_brg - 360
						end
						B738DR_hdg_bug_nd = relative_brg
					end
				end
				
			else
				-- APP, VOR mode
				B738DR_hdg_mag_nd = -simDR_ahars_mag_hdg
				relative_brg = (simDR_ahars_mag_hdg - simDR_mag_hdg + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				if simDR_ground_spd < 45 then
					B738DR_track_nd = 0
				else
					B738DR_track_nd = relative_brg
				end
				B738DR_hdg_bug_nd = 0
				
				relative_brg = (B738DR_course_pilot - simDR_ahars_mag_hdg + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				B738DR_relative_hdg_crs_pilot = relative_brg
			end
		end
		
	-- FIRST OFFICER
	if B738DR_fo_map_mode < 2 then
		B738DR_mcp_hdg_dial_nd_fo = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg_fo
		B738DR_nd_crs_fo = B738DR_course_copilot - simDR_ahars_mag_hdg_fo
		if B738DR_fo_exp_map_mode == 0 then
			B738DR_mcp_hdg_dial_nd_fo_show = 0
		else
			B738DR_mcp_hdg_dial_nd_fo_show = 1
		end
		if fo_tcas_enable == 0 then
			B738DR_EFIS_TCAS_on_fo = 0
		else
			B738DR_EFIS_TCAS_on_fo = 1
		end
	elseif B738DR_fo_map_mode == 2 then
		if B738DR_track_up == 0 then
			-- HDG-UP
			B738DR_mcp_hdg_dial_nd_fo = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg_fo
			B738DR_nd_crs_fo = B738DR_course_copilot - simDR_ahars_mag_hdg_fo
			if B738DR_fo_exp_map_mode == 0 then
				B738DR_mcp_hdg_dial_nd_fo_show = 2
			else
				B738DR_mcp_hdg_dial_nd_fo_show = 1
			end
		else
			if simDR_ground_spd < 45 then
				-- TRK/HDG
				B738DR_mcp_hdg_dial_nd_fo = B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg_fo
				B738DR_nd_crs_fo = B738DR_course_copilot - simDR_ahars_mag_hdg_fo
				if B738DR_fo_exp_map_mode == 0 then
					B738DR_mcp_hdg_dial_nd_fo_show = 2
				else
					B738DR_mcp_hdg_dial_nd_fo_show = 1
				end
			else
				-- TRK-UP
				B738DR_mcp_hdg_dial_nd_fo = B738DR_mcp_hdg_dial - simDR_mag_hdg_fo
				if B738DR_fo_exp_map_mode == 0 then
					B738DR_mcp_hdg_dial_nd_fo_show = 2
				else
					B738DR_mcp_hdg_dial_nd_fo_show = 1
				end
			end
		end
		if fo_tcas_enable == 0 then
			B738DR_EFIS_TCAS_on_fo = 0
		else
			B738DR_EFIS_TCAS_on_fo = 1
		end
	else
		B738DR_mcp_hdg_dial_nd_fo_show = 0
		B738DR_EFIS_TCAS_on_fo = 0
	end
	
	-- ND basic
		if B738DR_fo_map_mode <= 2 then
			if B738DR_fo_map_mode == 2 then
				-- MAP mode
				if B738DR_track_up == 0 then
					-- HDG-UP
					B738DR_hdg_mag_nd_fo = -simDR_ahars_mag_hdg_fo
					relative_brg = (simDR_ahars_mag_hdg_fo - simDR_mag_hdg_fo + 360) % 360
					if relative_brg > 180 then
						relative_brg = relative_brg - 360
					end
					if simDR_ground_spd < 45 then
						B738DR_track_nd_fo = 0
					else
						B738DR_track_nd_fo = relative_brg
					end
					B738DR_hdg_bug_nd_fo = 0
				else
					if simDR_ground_spd < 45 then
						-- TRK/HDG
						B738DR_hdg_mag_nd_fo = -simDR_ahars_mag_hdg_fo
						relative_brg = (simDR_ahars_mag_hdg_fo - simDR_mag_hdg_fo + 360) % 360
						if relative_brg > 180 then
							relative_brg = relative_brg - 360
						end
						B738DR_track_nd_fo = 0
						B738DR_hdg_bug_nd_fo = 0
					else
					-- TRK-UP
						B738DR_hdg_mag_nd_fo = -simDR_mag_hdg_fo
						B738DR_track_nd_fo = 0
						relative_brg = (simDR_ahars_mag_hdg_fo - simDR_mag_hdg_fo + 360) % 360
						if relative_brg > 180 then
							relative_brg = relative_brg - 360
						end
						B738DR_hdg_bug_nd_fo = relative_brg
					end
				end
			else
				-- APP, VOR mode
				B738DR_hdg_mag_nd_fo = -simDR_ahars_mag_hdg_fo
				relative_brg = (simDR_ahars_mag_hdg_fo - simDR_mag_hdg_fo + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				
				if simDR_ground_spd < 45 then
					B738DR_track_nd_fo = 0
				else
					B738DR_track_nd_fo = relative_brg
				end
				B738DR_hdg_bug_nd_fo = 0
				
				relative_brg = (B738DR_course_copilot - simDR_ahars_mag_hdg_fo + 360) % 360
				if relative_brg > 180 then
					relative_brg = relative_brg - 360
				end
				B738DR_relative_hdg_crs_copilot = relative_brg
			end
		end
	
	-- PFD HDG BUG
	local hdg_bug_pfd = (B738DR_mcp_hdg_dial - simDR_ahars_mag_hdg + 360) % 360
	if hdg_bug_pfd > 180 then
		hdg_bug_pfd = hdg_bug_pfd - 360
	end
	if hdg_bug_pfd < -36 then
		B738DR_hdg_bug_pfd = -36
	elseif hdg_bug_pfd > 36 then
		B738DR_hdg_bug_pfd = 36
	else
		B738DR_hdg_bug_pfd = hdg_bug_pfd
	end
	
	-- PFD TRACK LINE
	if simDR_ground_spd < 45 then
		hdg_bug_pfd = simDR_ahars_mag_hdg
	else
		hdg_bug_pfd = simDR_mag_hdg
	end
	hdg_bug_pfd = (hdg_bug_pfd - simDR_ahars_mag_hdg + 360) % 360
	if hdg_bug_pfd > 180 then
		hdg_bug_pfd = hdg_bug_pfd - 360
	end
	if hdg_bug_pfd < -35 then
		B738DR_track_pfd = -35
	elseif hdg_bug_pfd > 35 then
		B738DR_track_pfd = 35
	else
		B738DR_track_pfd = hdg_bug_pfd
	end
	
	-- HDG LINE
	if B738DR_capt_map_mode == 2 then
		if simDR_approach_status == 2 or B738DR_lnav_engaged >= 1 then
			if B738DR_mcp_hdg_dial ~= mcp_hdg_dial_old then
				hide_hdg_line = 0
				if is_timer_scheduled(hdg_line_timer) == true then
					stop_timer(hdg_line_timer)
				end
				run_after_time(hdg_line_timer, 10)
			else
				if is_timer_scheduled(hdg_line_timer) == false and hide_hdg_line == 0 then
					run_after_time(hdg_line_timer, 10)
				end
			end
		else
			if is_timer_scheduled(hdg_line_timer) == true then
				stop_timer(hdg_line_timer)
			end
			hide_hdg_line = 0
		end
	else
		if is_timer_scheduled(hdg_line_timer) == true then
			stop_timer(hdg_line_timer)
		end
		hide_hdg_line = 0
	end
	
	if B738DR_fo_map_mode == 2 then
		if simDR_approach_status == 2 or B738DR_lnav_engaged >= 1 then
			if B738DR_mcp_hdg_dial ~= mcp_hdg_dial_old then
				hide_hdg_line_fo = 0
				if is_timer_scheduled(hdg_line_timer_fo) == true then
					stop_timer(hdg_line_timer_fo)
				end
				run_after_time(hdg_line_timer_fo, 10)
			else
				if is_timer_scheduled(hdg_line_timer_fo) == false and hide_hdg_line_fo == 0 then
					run_after_time(hdg_line_timer_fo, 10)
				end
			end
		else
			if is_timer_scheduled(hdg_line_timer_fo) == true then
				stop_timer(hdg_line_timer_fo)
			end
			hide_hdg_line_fo = 0
		end
	else
		if is_timer_scheduled(hdg_line_timer_fo) == true then
			stop_timer(hdg_line_timer_fo)
		end
		hide_hdg_line_fo = 0
	end

	if hide_hdg_line == 0 and B738DR_capt_map_mode == 2 then
		B7378DR_hdg_line_enable = 1
	else
		B7378DR_hdg_line_enable = 0
	end
	if hide_hdg_line_fo == 0 and B738DR_fo_map_mode == 2 then
		B7378DR_hdg_line_fo_enable = 1
	else
		B7378DR_hdg_line_fo_enable = 0
	end
	
	mcp_hdg_dial_old = B738DR_mcp_hdg_dial
	
	-- TERRAIN
	if B738DR_gpws_terr_pos ~= 0 then
		-- flag TERR inhibited
		B738DR_efis_terr_inhibited = 1
	elseif B738DR_gpws_terr_warn > 0 then
		-- deteced Terrain warning
		if simDR_efis_wxr_on == 1 then
			simCMD_efis_wxr:once()
		end
		if simDR_efis_wxr_on_copilot == 1 then
			simCMD_efis_wxr_copilot:once()
		end
		B738DR_efis_wxr_on = 0
		B738DR_efis_fo_wxr_on = 0
		cpt_terr_enable = 1
		fo_terr_enable = 1
		B738DR_efis_terr_inhibited = 0
	elseif B738DR_windshear == 1 and B738DR_enable_gpwstest_long == 0 and B738DR_enable_gpwstest_short == 0 then
		-- detected windshear
		if B738DR_efis_wxr_on == 1 then
			cpt_terr_enable = 0
		end
		if B738DR_efis_fo_wxr_on == 1 then
			fo_terr_enable = 0
		end
		B738DR_efis_wxr_on = 1
		B738DR_efis_fo_wxr_on = 1
		B738DR_efis_terr_inhibited = 0
	else
		B738DR_efis_terr_inhibited = 0
	end
	
	local terr_enable = 0
	local terr_fail = 0
	
	if B738DR_gpws_terr_pos == 0 then
		terr_enable = cpt_terr_enable
	end
	if B738DR_irs_nd_allign ~= 0 then
		if terr_enable ~= 0 then
			terr_fail = 1
		end
		terr_enable = 0
	elseif B738DR_capt_exp_map_mode == 0 and B738DR_capt_map_mode < 2 then
		terr_enable = 0
	elseif B738DR_capt_map_mode == 3 then
		terr_enable = 0
	end
	B738DR_efis_terr_on = terr_enable
	B738DR_efis_terr_fail = terr_fail
	if terr_enable ~= 0 then
		B738DR_efis_terr_warn = B738DR_gpws_terr_warn
	else
		B738DR_efis_terr_warn = 0
	end
	
	terr_fail = 0
	if B738DR_gpws_terr_pos == 0 then
		terr_enable = fo_terr_enable
	end
	if B738DR_irs_nd_allign ~= 0 then
		if terr_enable ~= 0 then
			terr_fail = 1
		end
		terr_enable = 0
	elseif B738DR_fo_exp_map_mode == 0 and B738DR_fo_map_mode < 2 then
		terr_enable = 0
	elseif B738DR_fo_map_mode == 3 then
		terr_enable = 0
	end
	B738DR_efis_fo_terr_on = terr_enable
	B738DR_efis_fo_terr_fail = terr_fail
	if terr_enable ~= 0 then
		B738DR_efis_terr_warn_fo = B738DR_gpws_terr_warn
	else
		B738DR_efis_terr_warn_fo = 0
	end
	
	
	-- WEATHER RADAR
	if simDR_int_ver < 120000 then
		-- one weather radar at the same time
		if B738DR_efis_wxr_on == 0 and B738DR_efis_fo_wxr_on == 0 then
			-- turn off WX
			if simDR_efis_wxr_on == 1 then
				simCMD_efis_wxr:once()
			end
		else
			if B738DR_efis_wxr_on == 0 then
				-- First Officer
				if B738DR_fo_map_mode <= 2 then
					-- APP, VOR mode
					if B738DR_fo_exp_map_mode == 0 then
						-- turn off WX for CTR mode
						if simDR_efis_wxr_on == 1 then
							simCMD_efis_wxr:once()
						end
					elseif B738DR_efis_map_range_fo == 0 then
						-- turn off WX for range 5NM
						if simDR_efis_wxr_on == 1 then
							simCMD_efis_wxr:once()
						end
					else
						-- turn on WW for First Officer
						if simDR_efis_wxr_on == 0 then
							simCMD_efis_wxr:once()
						end
						if simDR_int_ver >= 120000 then
							simDR_efis_map_range = B738DR_efis_map_range_fo
						else
							simDR_efis_map_range = B738DR_efis_map_range_fo - 1
						end
					end
				else
					-- turn off WX for PLN mode
					if simDR_efis_wxr_on == 1 then
						simCMD_efis_wxr:once()
					end
				end
			else
				-- Captain
				if B738DR_capt_map_mode <= 2 then
					-- APP, VOR mode
					if B738DR_capt_exp_map_mode == 0 then
						-- turn off WX for CTR mode
						if simDR_efis_wxr_on == 1 then
							simCMD_efis_wxr:once()
						end
					elseif B738DR_efis_map_range_capt == 0 then
						-- turn off WX for range 5NM
						if simDR_efis_wxr_on == 1 then
							simCMD_efis_wxr:once()
						end
					else
					-- turn on WX for Captain
						if simDR_efis_wxr_on == 0 then
							simCMD_efis_wxr:once()
						end
						if simDR_int_ver >= 120000 then
							simDR_efis_map_range = B738DR_efis_map_range_capt
						else
							simDR_efis_map_range = B738DR_efis_map_range_capt - 1
						end
					end
				else
					-- turn off WX for PLN mode
					if simDR_efis_wxr_on == 1 then
						simCMD_efis_wxr:once()
					end
				end
			end
		end
	else
		-- Captain
		if B738DR_efis_wxr_on == 0 then
			if simDR_efis_wxr_on == 1 then
				simCMD_efis_wxr:once()
			end
		else
			if B738DR_capt_map_mode <= 2 then
				-- APP, VOR mode
				if B738DR_capt_exp_map_mode == 0 then
					-- turn off WX for CTR mode
					if simDR_efis_wxr_on == 1 then
						simCMD_efis_wxr:once()
					end
				-- elseif B738DR_efis_map_range_capt == 0 then
					-- -- turn off WX for range 5NM
					-- if simDR_efis_wxr_on == 1 then
						-- simCMD_efis_wxr:once()
					-- end
				else
				-- turn on WX for Captain
					if simDR_efis_wxr_on == 0 then
						simCMD_efis_wxr:once()
					end
					simDR_efis_map_range = B738DR_efis_map_range_capt
				end
			else
				-- turn off WX for PLN mode
				if simDR_efis_wxr_on == 1 then
					simCMD_efis_wxr:once()
				end
			end
		end
		
		
		-- First Officer
		if B738DR_efis_fo_wxr_on == 0 then
			if simDR_efis_wxr_on_copilot == 1 then
				simCMD_efis_wxr_copilot:once()
			end
		else
			if B738DR_fo_map_mode <= 2 then
				-- APP, VOR mode
				if B738DR_fo_exp_map_mode == 0 then
					-- turn off WX for CTR mode
					if simDR_efis_wxr_on_copilot == 1 then
						simCMD_efis_wxr_copilot:once()
					end
				-- elseif B738DR_efis_map_range_fo == 0 then
					-- -- turn off WX for range 5NM
					-- if simDR_efis_wxr_on_copilot == 1 then
						-- simCMD_efis_wxr_copilot:once()
					-- end
				else
				-- turn on WX for Captain
					if simDR_efis_wxr_on_copilot == 0 then
						simCMD_efis_wxr_copilot:once()
					end
					simDR_efis_map_range_fo = B738DR_efis_map_range_fo
				end
			else
				-- turn off WX for PLN mode
				if simDR_efis_wxr_on_copilot == 1 then
					simCMD_efis_wxr_copilot:once()
				end
			end
		end
	end
	
	-- WX Fail
	local wx_fail = 0
	local wx_fail_fo = 0
	if B738DR_irs_nd_allign ~= 0 then
		if B738DR_efis_wxr_on ~= 0 then
			wx_fail = 1
		end
		if B738DR_efis_fo_wxr_on ~= 0 then
			wx_fail_fo = 1
		end
	end
	B738DR_efis_wx_fail = wx_fail
	B738DR_efis_fo_wx_fail = wx_fail_fo
	
	
	-- ND Rings
	local efis_rings = 0
	if B738DR_nd_rings ~= 0 then
		if B738DR_capt_exp_map_mode == 1 and B738DR_capt_map_mode <= 2 then
			if B738DR_efis_terr_on == 1 or B738DR_efis_wxr_on == 1 or B738DR_EFIS_TCAS_on == 1 then
				efis_rings = 1
			end
		end
	end
	B738DR_efis_rings = efis_rings
	
	efis_rings = 0
	if B738DR_nd_rings ~= 0 then
		if B738DR_fo_exp_map_mode == 1 and B738DR_fo_map_mode <= 2 then
			if B738DR_efis_fo_terr_on == 1 or B738DR_efis_fo_wxr_on == 1 or B738DR_EFIS_TCAS_on_fo == 1 then
				efis_rings = 1
			end
		end
	end
	B738DR_efis_fo_rings = efis_rings
	
	if B738DR_efis_wxr_on ~= 0 or B738DR_efis_terr_on ~= 0 or B738DR_efis_terr_fail ~= 1 then
		B738DR_efis_wxr_terr = 1
	else
		B738DR_efis_wxr_terr = 0
	end
	if B738DR_efis_fo_wxr_on ~= 0 or B738DR_efis_fo_terr_on ~= 0 or B738DR_efis_fo_terr_fail ~= 1 then
		B738DR_efis_fo_wxr_terr = 1
	else
		B738DR_efis_fo_wxr_terr = 0
	end
	
	if B738DR_efis_vor_on == 0 and B738DR_efis_apt_on == 0 and B738DR_efis_fix_on == 0 then
		B738DR_efis_nav = 0
	else
		B738DR_efis_nav = 1
	end
	if B738DR_efis_fo_vor_on == 0 and B738DR_efis_fo_apt_on == 0 and B738DR_efis_fo_fix_on == 0 then
		B738DR_efis_fo_nav = 0
	else
		B738DR_efis_fo_nav = 1
	end
	
end


function B738_ctr_exp_mode()
	
	if B738DR_capt_map_mode ~= capt_map_mode_old then
		if B738DR_capt_map_mode > 1 and capt_map_mode_old < 2 then
			-- MAP/PLN
			if capt_exp_vor_app_mode ~= 0 then
				capt_exp_map_mode = 1
				capt_vsd_map_mode = 0
			else
				capt_exp_map_mode = 0
			end
		elseif B738DR_capt_map_mode < 2 and capt_map_mode_old > 1 then
			-- APP/VOR
			if capt_exp_map_mode == 0 or capt_vsd_map_mode ~= 0 then
				capt_exp_vor_app_mode = 0
			else
				capt_exp_vor_app_mode = 1
			end
		end
	end
	capt_map_mode_old = B738DR_capt_map_mode
	
	if B738DR_fo_map_mode ~= fo_map_mode_old then
		if B738DR_fo_map_mode > 1 and fo_map_mode_old < 2 then
			-- MAP/PLN
			if fo_exp_vor_app_mode ~= 0 then
				fo_exp_map_mode = 1
				fo_vsd_map_mode = 0
			else
				fo_exp_map_mode = 0
			end
		elseif B738DR_fo_map_mode < 2 and fo_map_mode_old > 1 then
			-- APP/VOR
			if fo_exp_map_mode == 0 or fo_vsd_map_mode ~= 0 then
				fo_exp_vor_app_mode = 0
			else
				fo_exp_vor_app_mode = 1
			end
		end
	end
	fo_map_mode_old = B738DR_fo_map_mode
	
	-- Captain
	if B738DR_capt_map_mode < 2 then
		-- APP/VOR mode
		B738DR_capt_exp_map_mode = capt_exp_vor_app_mode
		B738DR_capt_vsd_map_mode = 0
	elseif B738DR_capt_map_mode == 2 then
		-- MAP mode
		B738DR_capt_exp_map_mode = capt_exp_map_mode
		B738DR_capt_vsd_map_mode = capt_vsd_map_mode
	else
		-- PLN mode
		B738DR_capt_exp_map_mode = 1
		B738DR_capt_vsd_map_mode = 0
	end
	
	-- First Officier
	if B738DR_fo_map_mode < 2 then
		-- APP/VOR mode
		B738DR_fo_exp_map_mode = fo_exp_vor_app_mode
		B738DR_fo_vsd_map_mode = 0
	elseif B738DR_fo_map_mode == 2 then
		-- MAP mode
		B738DR_fo_exp_map_mode = fo_exp_map_mode
		B738DR_fo_vsd_map_mode = fo_vsd_map_mode
	else
		-- PLN mode
		B738DR_fo_exp_map_mode = 1
		B738DR_fo_vsd_map_mode = 0
	end
	
	if B738DR_nd_refresh ~= 0 and nd_refreshed == false then
		B738DR_nd_refresh = B738DR_nd_refresh - SIM_PERIOD
		if B738DR_nd_refresh < 0 then
			B738DR_nd_refresh = 0
		end
	end
	if B738DR_nd_refresh_fo ~= 0 and nd_refreshed_fo == false then
		B738DR_nd_refresh_fo = B738DR_nd_refresh_fo - SIM_PERIOD
		if B738DR_nd_refresh_fo < 0 then
			B738DR_nd_refresh_fo = 0
		end
	end
	nd_refreshed = false
	nd_refreshed_fo = false
end

function B738_vib_flaps()
	local int, frac = math.modf(os.clock())
	local seed = math.random(1, frac*1000.0)
	math.randomseed(seed)
	
	if simDR_pause == 0 then
		local vib_max = 0
		local vib_spd = 0
		
		if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
			vib_spd = math.min(simDR_ground_spd, 72)
			vib_spd = math.max(vib_spd, 42)
			
			if vib_spd < 50 then
				vib_max = B738_rescale(42, 0, 50, 1.1, vib_spd)	--0.75
			elseif vib_spd < 64 then
				vib_max = 0.75
			else
				vib_max = B738_rescale(64, 1.1, 72, 0, vib_spd)	--0.75
			end
		end
		
		local eng_force = math.min(simDR_faxil_prop, 50000)
		eng_force = math.max(eng_force, 3000)
		vib_max = vib_max * B738_rescale(3000, 1.1, 50000, 2.8, eng_force)
		
		-- left
		local vib_flaps = math.min(simDR_flap1_deg[0], 48)
		vib_flaps = math.max(vib_flaps, 15)
		
		local vib_max_flp = vib_max * B738_rescale(15, 0, 48, 1.5, vib_flaps)
		vib_max_flp = math.min (vib_max_flp, 3.7)
		local vib_half_flp = vib_max_flp * 0.5
		
		local vib = math.random() * vib_max_flp
		local vib_force = vib - vib_half_flp
		B738DR_flap1_deg[0] = simDR_flap1_deg[0] + (vib_force * 0.9)
		
		vib = math.random() * vib_max_flp
		vib_force = vib - vib_half_flp
		B738DR_flap2_deg[2] = simDR_flap2_deg[2] + vib_force
		
		-- right
		vib_flaps = math.min(simDR_flap1_deg[1], 48)
		vib_flaps = math.max(vib_flaps, 15)
		vib_max_flp = vib_max * B738_rescale(15, 0, 48, 2, vib_flaps)
		vib_half_flp = vib_max_flp * 0.5
		
		vib = math.random() * vib_max_flp
		vib_force = vib - vib_half_flp
		B738DR_flap1_deg[1] = simDR_flap1_deg[1] + (vib_force * 0.9)
		
		vib = math.random() * vib_max_flp
		vib_force = vib - vib_half_flp
		B738DR_flap2_deg[3] = simDR_flap2_deg[3] + vib_force
	end
end

function ap_disable_test_yoke()
	yoke_test_disabled = 0
end

function B738_yoke()
	
	local yoke_pitch = 0
	local yoke_roll = 0
	
	local int, frac = math.modf(os.clock())
	local seed = math.random(1, frac*1000.0)
	math.randomseed(seed)
	if shake_cpt > 0 then
		shake_cpt = math.random() * 0.04
		shake_cpt = -shake_cpt
	else
		shake_cpt = math.random() * 0.04
	end
	if shake_fo > 0 then
		shake_fo = math.random() * 0.04
		shake_fo = -shake_fo
	else
		shake_fo = math.random() * 0.04
	end
	
	if B738DR_yoke_disco_ap ~= 0 then
		
		local disconnect_ap_yoke = 0
		
		if autopilot_cmd_a_status == 1 or autopilot_cmd_b_status == 1 or B738DR_autoland_status == 1 then
			if yoke_test_disabled == 0 and autopilot_cws_status == 0 and B738DR_yoke_disco_ap == 1 then
					if B738DR_brunner_enable ~= 0 and brunner_active ~= 0 then
						if pitch_ratio_diff > 0.22 or roll_ratio_diff > 0.22 then
							disconnect_ap_yoke = 1
						end
					else
						if pitch_ratio_diff > 0.11 or roll_ratio_diff > 0.11 then
							disconnect_ap_yoke = 1
						end
					end
			end
		end
		
		if disconnect_ap_yoke == 1 then
			-- disconnect A/P
			simCMD_disconnect:once()
			simDR_flight_dir_mode = 1
			
			autopilot_cmd_a_status = 0
			autopilot_cmd_b_status = 0
			ap_capt_disco_status = 1
			ap_fo_disco_status = 1
			if B738DR_autoland_status == 1 then
				-- ap_roll_mode = 3	-- APP
				-- ap_pitch_mode = 4	-- G/S
				if ap_roll_mode == 9 or ap_roll_mode == 19 then
					ap_roll_mode = 3	-- APP
					ap_roll_mode_eng = 3	-- APP
				end
				if ap_pitch_mode == 13 or ap_pitch_mode == 14 then
					ap_pitch_mode = 4	-- GS
					ap_pitch_mode_eng = 4	-- GS
				end
			end
			B738DR_autoland_status = 0
			B738DR_flare_status = 0
			B738DR_rollout_status = 0
			B738DR_no_autoland = 0
			B738DR_retard_status = 0
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
			vorloc_only = 0
			ap_on = 0
			ap1_on = 0
			ap2_on = 0
		end
	end
	
	
	if ap_on == 1 then
		B738DR_yoke_pitch = simDR_pitch_ratio2
		B738DR_yoke_roll = simDR_roll_ratio2
		if autopilot_cws_p_status ~= 0 and B738DR_yoke_to_center == 1 then
			if math.abs(B738DR_joy_axis_pitch) < 0.05 then
				simDR_pitch_ratio2 = B738_set_anim_value(simDR_pitch_ratio2, 0, -1.0, 1.0, 6)
				B738DR_pitch_ratio2_old = simDR_pitch_ratio2
			end
		end
		if  autopilot_cws_r_status ~= 0 and B738DR_yoke_to_center == 1 then
			if math.abs(B738DR_joy_axis_roll) < 0.05 then
				simDR_roll_ratio2 = B738_set_anim_value(simDR_roll_ratio2, 0, -1.0, 1.0, 6)
				roll_ratio2_old = simDR_roll_ratio2
			end
		end
	else
		B738DR_yoke_pitch = simDR_pitch_ratio2
		B738DR_yoke_roll = simDR_roll_ratio2
		if math.abs(B738DR_joy_axis_pitch) < 0.05 and B738DR_yoke_to_center == 1 then
			simDR_pitch_ratio2 = B738_set_anim_value(simDR_pitch_ratio2, 0, -1.0, 1.0, 6)
		end
		if math.abs(B738DR_joy_axis_roll) < 0.05 and B738DR_yoke_to_center == 1 then
			simDR_roll_ratio2 = B738_set_anim_value(simDR_roll_ratio2, 0, -1.0, 1.0, 6)
		end
	end
	
	B738DR_yoke_pitch2 = B738DR_yoke_pitch
	
	shake_cpt = shake_cpt + B738DR_yoke_pitch
	shake_fo = shake_fo + B738DR_yoke_pitch
	if simDR_radio_height_pilot_ft > 50 then
		if simDR_airspeed_pilot < B738DR_pfd_min_speed or simDR_aoa > 14.4 then	--16/13.5
			shake_on1 = 1
		elseif simDR_airspeed_pilot > (B738DR_pfd_min_speed + 2) then
			shake_on1 = 0
		end
		if simDR_airspeed_copilot < B738DR_pfd_min_speed or simDR_aoa > 14.4 then	--16/13.5
			shake_on2 = 1
		elseif simDR_airspeed_copilot > (B738DR_pfd_min_speed + 2) then
			shake_on2 = 0
		end
	else
		shake_on1 = 0
		shake_on2 = 0
	end
	
	if simDR_aircraft_on_ground == 0 then
		if shake_on1 == 1 and B738DR_dc_stdbus_status == 1 and B738DR_ac_tns_bus1_pow4 == 1 then
			B738DR_shake_on_cpt = 1
			B738DR_yoke_pitch = shake_cpt
		else
			B738DR_shake_on_cpt = 0
		end
		if shake_on2 == 1 and B738DR_dc_bus2_status == 1 and B738DR_ac_tns_bus2_pow4 == 1 then
			B738DR_shake_on_fo = 1
			B738DR_yoke_pitch2 = shake_fo
		else
			B738DR_shake_on_fo = 0
		end
	else
		if B738DR_stall_test_active1 == 1 then
			B738DR_shake_on_cpt = 1
			B738DR_yoke_pitch = shake_cpt
		else
			B738DR_shake_on_cpt = 0 
		end
		if B738DR_stall_test_active2 == 1 then
			B738DR_shake_on_fo = 1
			B738DR_yoke_pitch2 = shake_fo
		else
			B738DR_shake_on_fo = 0 
		end
	end
	
	last_yoke_pitch = B738DR_yoke_pitch
	last_yoke_roll = B738DR_yoke_roll
end


function B738_yoke_replay()
	B738DR_yoke_pitch = simDR_pitch_ratio2 + simDR_servo_pitch_ratio
	B738DR_yoke_pitch2 = B738DR_yoke_pitch
	B738DR_yoke_roll = simDR_roll_ratio2 + simDR_servo_roll_ratio
end

function B738_spd_ratio()
	time_spd_ratio = time_spd_ratio + SIM_PERIOD
	if time_spd_ratio > 0.1 then
		
		spd_ratio_old = simDR_airspeed_accel_pilot - spd_ratio
		spd_ratio = simDR_airspeed_accel_pilot
		spd_ratio_old = spd_ratio
		
		-- hdg_ratio = (simDR_ahars_mag_hdg - hdg_ratio_old + 360) % 360
		-- if hdg_ratio > 180 then
			-- hdg_ratio = hdg_ratio - 360
		-- end
		-- hdg_ratio = hdg_ratio / time_spd_ratio
		-- hdg_ratio_old = simDR_ahars_mag_hdg
		
		-- if simDR_nav1_hdef_pilot == nil then
			-- hdef_ratio = 0
			-- hdef_ratio_old = 0
		-- else
			-- hdef_ratio = (simDR_nav1_hdef_pilot - hdef_ratio_old) / time_spd_ratio
			-- hdef_ratio_old = simDR_nav1_hdef_pilot
		-- end
		
		-- pitch_ratio_pilot = (simDR_ahars_pitch_deg_pilot - pitch_ratio_pilot_old) / time_spd_ratio
		-- pitch_ratio_pilot_old = simDR_ahars_pitch_deg_pilot
		
		-- pitch_ratio_copilot = (simDR_ahars_pitch_deg_copilot - pitch_ratio_copilot_old) / time_spd_ratio
		-- pitch_ratio_copilot_old = simDR_ahars_pitch_deg_copilot
		
		roll_ratio_pilot = (simDR_roll_AHARS_deg - roll_ratio_pilot_old) / time_spd_ratio
		roll_ratio_pilot_old = simDR_roll_AHARS_deg
		
		roll_ratio_copilot = (simDR_roll_AHARS_deg_copilot - roll_ratio_copilot_old) / time_spd_ratio
		roll_ratio_copilot_old = simDR_roll_AHARS_deg_copilot
		
		time_spd_ratio = 0
		
	end

end

function B738_afs_protect3()
	afs_protect_speed = 0
	if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
		if simDR_reverse_act[0] ~= 3 and simDR_reverse_act[1] ~= 3 then
			if B738DR_autopilot_autothr_arm_pos == 1 and at_mode == 0 and B738DR_at_throttle_hold == 0 and ap_pitch_mode ~= 5 
			and ap_pitch_mode ~= 8 and ap_pitch_mode ~= 11 then
				if simDR_airspeed_pilot > B738DR_afs_spd_limit_max + 2 then
					if simDR_airspeed_dial_kts > B738DR_afs_spd_limit_max then
						simDR_airspeed_dial_kts = B738DR_afs_spd_limit_max
						-- new mcp
						B738DR_mcp_speed_dial = simDR_airspeed_dial
						update_mcp_speed_dial()
					end
					if ap_pitch_mode == 2 then
						ap_pitch_mode = 1
					end
					at_mode = 2
				elseif simDR_airspeed_pilot < B738DR_pfd_min_maneuver_speed - 3 then
					if simDR_airspeed_dial_kts < B738DR_pfd_min_maneuver_speed then
						simDR_airspeed_dial_kts = B738DR_pfd_min_maneuver_speed
						-- new mcp
						B738DR_mcp_speed_dial = simDR_airspeed_dial
						update_mcp_speed_dial()
					end
					if ap_pitch_mode == 2 then
						ap_pitch_mode = 1
					end
					at_mode = 2
				end
			end
		end
	end
end

function B738_eng_out()
	
	local engine1_out = 0
	local engine2_out = 0
	local kill_engine1 = 0
	local kill_engine2 = 0
	
	if simDR_eng1_separate == 6 then
		if simDR_engine_mixture[0] >= 0.5 and simDR_eng_N2[0] < 50 and B738DR_engine01_fire_ext_switch_pos_arm == 0 then
			engine1_out = 1
		end
		kill_engine1 = 1
	elseif simDR_ground_spd > 31 or simDR_eng1_fail == 6 then
		if simDR_engine_mixture[0] >= 0.5 and simDR_eng_N2[0] < 50 and B738DR_engine01_fire_ext_switch_pos_arm == 0 and B738DR_engine1_starter_pos ~= 0 then
			engine1_out = 1
		end
	end
	if simDR_eng2_separate == 6 then
		if simDR_engine_mixture[1] >= 0.5 and simDR_eng_N2[1] < 50 and B738DR_engine02_fire_ext_switch_pos_arm == 0 then
			engine2_out = 1
		end
		kill_engine2 = 1
	elseif simDR_ground_spd > 31 or simDR_eng2_fail == 6 then
		if simDR_engine_mixture[1] >= 0.5 and simDR_eng_N2[1] < 50 and B738DR_engine02_fire_ext_switch_pos_arm == 0 and B738DR_engine2_starter_pos ~= 0 then
			engine2_out = 1
		end
	end
	
	local lowerDU_auto = 0
	if simDR_radio_height_pilot_ft > 10 then
		if engine1_out == 1 or engine2_out == 1 then
			if B738DR_eng_out == 0 then
				B738DR_eng_out = 1
				if simDR_altitude_pilot < B738DR_aerodrom_altitude + B738DR_eo_accel_alt and B738DR_flight_phase < 2 then
					takeoff_eng_out = 1
				end
			end
			lowerDU_auto = 1
		elseif B738DR_mixture_ratio1 < 0.1 or B738DR_mixture_ratio2 < 0.1 then
			lowerDU_auto = 1
		end
	elseif engine1_out == 1 or engine2_out == 1 then
		lowerDU_auto = 1
	end
	
	if lowerDU_auto ~= 0 then
		if B738DR_lowerDU_page ~= 1 and lowerDU_set == 0 then
			B738DR_lowerDU_page = 1
			B738DR_lowerDU_page2 = 0
			lowerDU_set = 1
			B738DR_fd_cam = 0
		end
	else
		B738DR_eng_out = 0
		lowerDU_set = 0
		B738DR_eng_out_disable = 0
		takeoff_eng_out = 0
	end
	
	B738DR_eng1_out = engine1_out
	B738DR_eng2_out = engine2_out
	
	B738DR_kill_engine1 = 0		--kill_engine1
	B738DR_kill_engine2 = 0		--kill_engine2
	
	if B738DR_lowerDU_page == 2 or B738DR_ff_eicas == 1 then
		B738DR_ff_eicas_show = 1
	else
		B738DR_ff_eicas_show = 0
	end
end
--*************************************************************************************--
--** 				               XLUA EVENT CALLBACKS       	        			 **--
--*************************************************************************************--

--function aircraft_load() end

function aircraft_unload()
	simDR_throttle_override = 0
	if simDR_ap_capt_heading == nil then
		simDR_ap_capt_heading = 0
	elseif simDR_ap_capt_heading < 0 or simDR_ap_capt_heading > 360 then
		simDR_ap_capt_heading = 0
	end
end

function flight_start()

B738DR_efis_vor1_capt_pos = 1
B738DR_efis_vor2_capt_pos = 1
simDR_vor1_capt = 2
simDR_vor2_capt = 2
B738DR_minim_capt = 0
B738DR_efis_vor1_fo_pos = 1
B738DR_efis_vor2_fo_pos = 1
simDR_vor1_fo = 2
simDR_vor2_fo = 2
B738DR_minim_fo = 0
B738DR_capt_map_mode = 2
B738DR_fo_map_mode = 2
simDR_EFIS_mode = 2

simDR_efis_ndb	= 0
simDR_bank_angle = 5
simDR_autopilot_source = 0
simDR_autopilot_fo_source = 1
simDR_autopilot_side = 0
B738DR_autopilot_bank_angle_pos = 3

autopilot_cmd_a_status = 0
autopilot_cmd_b_status = 0
autopilot_cws_a_status = 0
autopilot_cws_b_status = 0
autopilot_cws_status = 0
autopilot_cws_r_status = 0
autopilot_cws_p_status = 0
autopilot_master_capt_status = 0
autopilot_master_fo_status = 0

B738DR_autopilot_hdg_sel_status = 0
B738DR_autopilot_vorloc_status = 0
B738DR_autopilot_app_status = 0
B738DR_autopilot_lnav_status = 0
B738DR_autopilot_vs_status = 0
B738DR_autopilot_lvl_chg_status = 0
B738DR_autopilot_alt_hld_status = 0
B738DR_autopilot_vnav_status = 0
B738DR_autopilot_fd_pos = 0
B738DR_autopilot_fd_fo_pos = 0
B738DR_autopilot_n1_pfd = 0
B738DR_autopilot_n1_status = 0
B738DR_autopilot_vnav_alt_pfd = 0
B738DR_autopilot_vnav_pth_pfd = 0
B738DR_autopilot_vnav_spd_pfd = 0
B738DR_autopilot_fmc_spd_pfd = 0
ap_roll_mode = 0
ap_pitch_mode = 0
ap_roll_mode_eng = 0
ap_pitch_mode_eng = 0
B738DR_autopilot_autothr_arm_pos = 0
autopilot_autothrottle_status = 0
B738DR_autopilot_autothrottle_status = 0
load_plane2 = 0

B738DR_autoland_status = 0
B738DR_flare_status = 0
B738DR_rollout_status = 0
B738DR_no_autoland = 0
B738DR_retard_status = 0
B738DR_single_ch_status = 0
vorloc_only = 0
B738DR_ils_pointer_disable = 0
aircraft_was_on_air = 0
B738DR_fmc_mode = 0
B738DR_fmc_descent_now = 0
B738DR_blink = 0
B738DR_mach_disable = 0
B738DR_kts_disable = 0
B738DR_was_on_cruise = 0
mem_airspeed_dial = 100
mem_speed_mode = 0
at_mode = 0
at_mode_eng = 0
ap_pitch_mode_old = 0
ap_roll_mode_old = 0
approach_status_old = 0
glideslope_status_old = 0
B738DR_autopilot_to_ga_pfd = 0
B738DR_autopilot_thr_hld_pfd = 0
B738DR_autopilot_ga_pfd = 0
B738DR_autopilot_alt_acq_pfd = 0
takeoff_n1 = 0
B738DR_ap_goaround = 0
B738DR_fd_goaround = 0
B738DR_at_takeoff = 0
cmd_first = 0
ils_test_enable = 0
ils_test_on = 0
ils_test_ok = 0
ils_test_ok2 = 0
ap_app_block_800 = 0
B738DR_lowerDU_page = 2
B738DR_lowerDU_page2 = 0
lift_off_150 = 0
on_ground_30 = 1
ap_goaround_block = 0
B738DR_pfd_vorloc_lnav = 0
ias_disagree = 0
alt_disagree = 0
lights_test_old = 0

eng1_N1_man = 0.984
eng2_N1_man = 0.984

B738DR_at_throttle_hold = 0

B738DR_mcp_alt_dial = simDR_ap_altitude_dial_ft
if B738DR_mcp_alt_dial < 0 then
	B738DR_mcp_alt_dial = 0
end
simDR_ap_vvi_dial = 0
B738DR_ap_vvi_dial = 0

--- FLARE CONSTANTS ---
B738DR_thrust_vvi_1 = -200
B738DR_thrust_vvi_2 = -520
B738DR_thrust_ratio_1 = 0.06
B738DR_thrust_ratio_2 = 0.03	--0.003
B738DR_flare_ratio = 2.0	--0.6	--1.2
B738DR_pitch_ratio = 2.2	--1.6	--1.8	--2.1	--2.2	--2.22
B738DR_pitch_offset = 0.85

B738DR_baro_sel_in_hg_pilot = 29.92
B738DR_baro_sel_in_hg_copilot = 29.92

B738DR_EFIS_TCAS_on = 1
B738DR_EFIS_TCAS_on_fo = 1
simDR_EFIS_TCAS = 0
cpt_tcas_enable = 1
fo_tcas_enable = 1
cpt_terr_enable = 0
fo_terr_enable = 0
cpt_pos_enable = 0
fo_pos_enable = 0

B738DR_ap_spd_interv_status = 0
spd_interv_co_used = 0

simDR_fdir_pitch = 0
simDR_fdir_pitch_ovr = 0

vnav_vs = 0
lvl_chg_vs = 0
vnav_alt_hld = 0
ap_spd_interv_status_old = 0
at_mode_vnav = 0
altitude_dial_ft_old = 0
bellow_400ft = 0

if is_timer_scheduled(speed_ratio_timer) == false then
	run_at_interval(speed_ratio_timer, 1)
end

B738DR_pfd_nav1_pilot = 0
B738DR_pfd_nav2_pilot = 0
B738DR_pfd_nav1_copilot = 0
B738DR_pfd_nav2_copilot = 0

B738DR_fms_descent_now = 0

reverse_max_enable1 = 0
reverse_max_on1 = 0
reverse_max_enable2 = 0
reverse_max_on2 = 0

baro_dh_pilot_disable = 0
baro_dh_copilot_disable = 0

radio_dh_pilot = -1
radio_dh_copilot = -1

baro_dh_pilot = -1001
baro_dh_copilot = -1001

radio_dh_pilot_mem = 100		-- -1		--100
baro_dh_pilot_mem = 200			-- -1001	--200

radio_dh_copilot_mem = 100		-- -1	--100
baro_dh_copilot_mem = 200		-- -1001	--200

if B738DR_minim_capt == 0 then	--radio DH
	B738DR_dh_pilot = -1
else
	B738DR_dh_pilot = -1001
end
B738DR_minim_capt_show = 0

if B738DR_minim_fo == 0 then	--radio DH
	B738DR_dh_copilot = -1
else
	B738DR_dh_copilot = -1001
end
B738DR_minim_fo_show = 0

blink_rec_cmd_status1 = 0
blink_rec_cmd_status2 = 0
blink_rec_sch_status = 0
blink_rec_schg_status = 0
blink_rec_cws_r_status = 0
blink_rec_cws_p_status = 0
blink_rec_alt_alert_status = 0
blink_rec_spd_ind = 0
B738DR_spd_ind_mode = 0
spd_ind_mode_old = 0
cmd_old1 = 0
cmd_old2 = 0
single_ch_status_old = 0
single_ch_g_status_old = 0
autopilot_cws_r_status_old = 0
autopilot_cws_p_status_old = 0
B738DR_vnav_descent_disable = 0
dh_minimum_pilot = 0
dh_minimum_copilot = 0
dh_min_block_pilot = 0
dh_min_block_copilot = 0

first_time_baro_cpt = 0
first_time_baro_fo = 0

if simDR_startup_running == 0 then
	B738DR_efis_vor_on = 0
	B738DR_efis_apt_on = 0
	B738DR_efis_fix_on = 0
	B738DR_efis_wxr_on = 0
	B738DR_efis_wxr_capt = 0
	B738DR_efis_fo_vor_on = 0
	B738DR_efis_fo_apt_on = 0
	B738DR_efis_fo_fix_on = 0
	B738DR_efis_fo_wxr_on = 0
	simDR_efis_wxr_on = 0
	timer_gyro = 0
	ac_stdbus_timer = 0
	roll_elec_deg_stdby = -45
	pitch_elec_deg_stdby = 65
else
	B738DR_efis_vor_on = 1
	B738DR_efis_apt_on = 1
	B738DR_efis_fix_on = 0
	B738DR_efis_wxr_on = 0
	B738DR_efis_wxr_capt = 0
	B738DR_efis_fo_vor_on = 1
	B738DR_efis_fo_apt_on = 1
	B738DR_efis_fo_fix_on = 0
	B738DR_efis_fo_wxr_on = 0
	timer_gyro = 8
	ac_stdbus_timer = 30
	roll_elec_deg_stdby = 0
	pitch_elec_deg_stdby = 0
end

B738DR_allign_analog_horiz_pos = 0

simDR_efis_vor_on = 0
simDR_efis_apt_on = 0
simDR_efis_fix_on = 0
simDR_efis_wxr_on = 0
simDR_efis_sub_mode = 2
if simDR_int_ver < 120000 then
	simDR_efis_map_mode = 0
else
	simDR_efis_sub_mode_copilot = 2
	simDR_efis_vor_on_copilot = 0
	simDR_efis_apt_on_copilot = 0
	simDR_efis_fix_on_copilot = 0
	simDR_efis_wxr_on_copilot = 0
	simDR_map_range_steps[0] = 5
	simDR_map_range_steps[1] = 10
	simDR_map_range_steps[2] = 20
	simDR_map_range_steps[3] = 40
	simDR_map_range_steps[4] = 80
	simDR_map_range_steps[5] = 160
	simDR_map_range_steps[6] = 320
	simDR_map_range_steps[7] = 640
end

B738DR_efis_data_capt_status = 0
B738DR_efis_data_fo_status = 0

B738DR_efis_map_range_capt = 1
B738DR_efis_map_range_fo = 1

B738DR_capt_exp_map_mode = 1
B738DR_fo_exp_map_mode = 1

vnav_init = 0
vnav_init2 = 0
vs_first = 0
initial_vs_alt = 0
mdah_was_enable = 0

vnav_alt_hold = 0
vnav_alt_hold_act = 0
mcp_alt_hold = 0
rest_wpt_alt_old = 0
vnav_engaged = 0
B738DR_lnav_engaged = 0
ap_alt_hold_arm = 0
toga_was_engaged = 0
thr1_anim = 0
thr2_anim = 0

ap_on = 0
ap1_on = 0
ap2_on = 0
ap_on_first = 0
at_on_first = 0
cws_on = 0
cws_on_first = 0
ap_disco_first = 0
ap_dis_time = 0
at_dis_time = 0

roll_mode_old = 0
pitch_mode_old = 0
at_on_old = 0
vnav_desc_spd = 0
vnav_desc_protect_spd = 0
ap_disco_do = 0

B738DR_loc_gp_engaged = 0


B738DR_mcp_hdg_dial = 0
mcp_hdg_dial_old = 0

at_status = 0
retard_timer = 0

axis_throttle1_old = 0
axis_throttle2_old = 0
ap_disco2 = 0
turn_active = 0
intdir_act = 0
master_light_block_act = 0
autopilot_side = 0
autopilot_master_first = 0

autothr_arm_pos = B738DR_autopilot_autothr_arm_pos

hide_hdg_line = 0
hide_hdg_line_fo = 0
dspl_ctrl_pnl_old = 0
in_hpa_cnt = 0

B738DR_alt_hold_mem = 0
altitude_mode_old = 0
flight_phase_old = 0

capt_exp_map_mode = 1
fo_exp_map_mode = 1
capt_vsd_map_mode = 0
fo_vsd_map_mode = 0
capt_exp_vor_app_mode = 1
fo_exp_vor_app_mode = 1

B738DR_AP_airspeed = 0
B738DR_AP_airspeed_mach = 0

B738DR_yoke_capt_checklist = 1
B738DR_yoke_fo_checklist = 1
ap_capt_disco_status = 0
ap_fo_disco_status = 0

shake_on1 = 0
shake_on2 = 0
lowerDU_set = 0
takeoff_eng_out = 0
vnav_desc_idle = 0
vnav_desc_idle_time = 0.5

radio_height_sum = 0
radio_height_num = 0
radio_h_ratio_s = 0
radio_h_tick = 0
hdg_sel_to = 0
to_ga_mcp_changed = 0
mcp_speed_dial_mem = 0

thrust1_leveler_mem = 0
thrust2_leveler_mem = 0
was_replay = 0
alt_deviation_active = 0
lnav_tune_enable = 0
idx_corr2_anim = 0
max_idx_dist_anim = 1.1
max_idx_dist_anim2 = 1.2
integral = 1.85

ratio_timer = 0
	
	throttle1_use_old = simDR_throttle[0]
	throttle2_use_old = simDR_throttle[1]
	throttle_all_use_old = simDR_throttle_all
	
	servo_yoke = 0
	servo_yoke_old = 0
	roll_ratio2_old = 0
	mem_attitude = 0
	roll_err_accu = 0
	mem_attitude_deg = 0
	B738DR_ap_wca = 0
	ap_wca_wind_hdg = 0
	ap_wca_wind_spd = 0
	
	pitch_ratio_trend = 0
	pitch_ratio_last_trend = 0
	
	roll_ratio_pilot = 0
	roll_ratio_pilot_old = 0
	roll_ratio_copilot = 0
	roll_ratio_copilot_old = 0
	
	fdir_pitch_anim = 0
	fdir_pitch_ovr_old = 0
	pitch_ratio_diff = 0
	roll_ratio_diff = 0
	servo_i_pitch = 0
	servo_ii_pitch = 0
	fdir_pitch_old = 0
	last_err_pitch = 0
	servo_k_pitch_old = 0
	
	pfd_loc_exp = 0
	pfd_loc_exp_fo = 0
	pfd_fac_exp = 0
	pfd_fac_exp_fo = 0
	pfd_gls_exp = 0
	pfd_gls_exp_fo = 0
	
	mcp_alt_dial_lock = 0
	mcp_alt_dial_app = B738DR_mcp_alt_dial
	mcp_alt_dial_old = B738DR_mcp_alt_dial
	mcp_alt_vs_lock = 0
	mcp_alt_vs_dial = B738DR_mcp_alt_dial
	mcp_alt_vs_dial_old = B738DR_mcp_alt_dial
	
	B738DR_gls_nav_status = 0
	B738DR_gls_app_status = 0
	B738DR_gls_gs_status = 0
	gls_pth_alt_old = 0
	gls_vvi_cur = 0
	lpv_vvi_cur = 0
	lpv_pth_alt_old = 0
	gls_active = 0
	mmr_act_mode = 0
	mmr_act_mode_old = 0
	gls_type = 0
	ac_tnsbus1_status_old = 0
	ac_tnsbus2_status_old = 0
	source_off_bus1_old = 0
	source_off_bus2_old = 0
	flare_status_mem = 0
	autothr_arm_pos_mem = 0
	autothrottle_status_mem = 0
	B738DR_enable_servo = 0
	vor_loc1_captured = 0
	vor_loc2_captured = 0
	afs_protect_speed = 0
	snd_ap_disconnect_old = 0
	snd_ap_disco_delay = 0
	ignore_td_idx2 = 0
	both_fd_was_on = 0
	B738DR_fdir_pitch_active = 1
	B738DR_fdir_roll_active = 1
	vnav_n1_dis_eng = 0
	alt_hold_captured = 0
	B738DR_autopilot_lvl_chg_status = 0
	vs_armed = 0
	vs_armed_enable = 0
	alt_hld_mcp_alt = 0
	ap_warn = 0
	ap_warn_off = 0
	ap_cws_status_old = 0
	at_warn_low = 0
	at_warn_high = 0
	at_warn_reset = 0
	B738DR_fd_cam_pos = 0
	B738DR_fd_cam_push_pos = 0
	B738DR_fd_cam = 0
	vorloc_lnav_enable = 0
	lnav_ma_armed = 0
	-- autopilot_altitude_mode_old = 0
	autoland_install = -1
	fd_on_both = 0
	recycled_fd_roll = 0
	fms_ils_disable_old = B738DR_fms_ils_disable
	ian_install_old = B738DR_ian_not_install
	ils_gls_not_tuned_old = 1
	fd_goaround_single = 0
	capt_map_mode_old = -1
	fo_map_mode_old = -1
	B738DR_cb_d9 = 0
	simDR_flight_dir_mode = 1
	loaded_flight_timer = 5		--0

	ap_heading_buf = 0
	ap_heading_timer = 0
	ap_heading_used = false

	ap_altitude_buf = 0
	ap_altitude_timer = 0
	ap_altitude_used = false

	ap_speed_buf = 0
	ap_speed_timer = 0
	ap_speed_used = false
	
	fma_roll_timer = 0
	fma_pitch_timer = 0
	fma_speed_timer = 0
	
	if simDR_int_ver >= 120000 then
		max_REV = 0.69
		max_THR = 1.12
		shift_off = 0.224
	else
		max_REV = 0.57
		max_THR = 1.08
		shift_off = 0.27
	end
	shift_thr = 0
	show_ias_old = 0
	
	if is_timer_scheduled(ap_disable_test_yoke) == true then
		stop_timer(ap_disable_test_yoke)
	end
	run_after_time(ap_disable_test_yoke, 2)
	yoke_test_disabled = 1
	
	glare_load = 0
	
	decel_dist_calc = false
	decel_dist_crz = 0
	fmc_cruise_speed_old = -1
	fmc_cruise_speed_mach_old = -1
	fmc_descent_speed_old = -1
	fmc_descent_speed_mach_old = -1

end

function unlock_mcp_alt()
	mcp_alt_dial_lock = 0
end

function ignore_td_idx2_timer()
	ignore_td_idx2 = 0
end

function B738_set_state()
	
	local jj = 0
	local state_cmd = B738DR_state_cmd
	local state_idx = 74	--to 92
	
	if string.len(state_cmd) >= 74 then
	-- for jj = 1, string.len(state_cmd) do
	for jj = state_idx, string.len(state_cmd) do
		if jj == state_idx then		--73
			-- EFIS CPT VOR1
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_vor1_capt_pos = -1
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_efis_vor1_capt_pos = 0
			else
				B738DR_efis_vor1_capt_pos = 1
			end
			if B738DR_efis_vor1_capt_pos == 1 then
				simDR_vor1_capt = 2
			else
				simDR_vor1_capt = 1
			end
		elseif jj == state_idx + 1 then		--74
			-- EFIS CPT VOR2
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_vor2_capt_pos = -1
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_efis_vor2_capt_pos = 0
			else
				B738DR_efis_vor2_capt_pos = 1
			end
			if B738DR_efis_vor2_capt_pos == 1 then
				simDR_vor2_capt = 2
			else
				simDR_vor2_capt = 1
			end
		elseif jj == state_idx + 2 then		--75
			-- EFIS FO VOR1
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_vor1_fo_pos = -1
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_efis_vor1_fo_pos = 0
			else
				B738DR_efis_vor1_fo_pos = 1
			end
			if B738DR_efis_vor1_fo_pos == 1 then
				simDR_vor1_fo = 2
			else
				simDR_vor1_fo = 1
			end
		elseif jj == state_idx + 3 then		--76
			-- EFIS FO VOR2
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_vor2_fo_pos = -1
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_efis_vor2_fo_pos = 0
			else
				B738DR_efis_vor2_fo_pos = 1
			end
			if B738DR_efis_vor2_fo_pos == 1 then
				simDR_vor2_fo = 2
			else
				simDR_vor2_fo = 1
			end
		elseif jj == state_idx + 4 then		--77
			-- EFIS CPT MAP MODE
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_capt_map_mode = 0
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_capt_map_mode = 1
			elseif string.sub(state_cmd, jj, jj) == "2" then
				B738DR_capt_map_mode = 2
			else
				B738DR_capt_map_mode = 3
			end
		elseif jj == state_idx + 5 then		--78
			-- EFIS FO MAP MODE
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_fo_map_mode = 0
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_fo_map_mode = 1
			elseif string.sub(state_cmd, jj, jj) == "2" then
				B738DR_fo_map_mode = 2
			else
				B738DR_fo_map_mode = 3
			end
		elseif jj == state_idx + 6 then		--79
			-- EFIS CPT RANGE
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_map_range_capt = 0
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_efis_map_range_capt = 1
			elseif string.sub(state_cmd, jj, jj) == "2" then
				B738DR_efis_map_range_capt = 2
			elseif string.sub(state_cmd, jj, jj) == "3" then
				B738DR_efis_map_range_capt = 3
			elseif string.sub(state_cmd, jj, jj) == "4" then
				B738DR_efis_map_range_capt = 4
			elseif string.sub(state_cmd, jj, jj) == "5" then
				B738DR_efis_map_range_capt = 5
			elseif string.sub(state_cmd, jj, jj) == "6" then
				B738DR_efis_map_range_capt = 6
			else
				B738DR_efis_map_range_capt = 7
			end
		elseif jj == state_idx + 7 then		--80
			-- EFIS FO RANGE
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_map_range_fo = 0
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_efis_map_range_fo = 1
			elseif string.sub(state_cmd, jj, jj) == "2" then
				B738DR_efis_map_range_fo = 2
			elseif string.sub(state_cmd, jj, jj) == "3" then
				B738DR_efis_map_range_fo = 3
			elseif string.sub(state_cmd, jj, jj) == "4" then
				B738DR_efis_map_range_fo = 4
			elseif string.sub(state_cmd, jj, jj) == "5" then
				B738DR_efis_map_range_fo = 5
			elseif string.sub(state_cmd, jj, jj) == "6" then
				B738DR_efis_map_range_fo = 6
			else
				B738DR_efis_map_range_fo = 7
			end
		elseif jj == state_idx + 8 then		--81
			-- AP disconnect
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_autopilot_disconnect_pos = 0
			else
				B738DR_autopilot_disconnect_pos = 1
			end
		elseif jj == state_idx + 9 then		--82
			-- FD captain
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_autopilot_fd_pos = 0
			else
				B738DR_autopilot_fd_pos = 1
			end
		elseif jj == state_idx + 10 then		--83
			-- FD FO
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_autopilot_fd_fo_pos = 0
			else
				B738DR_autopilot_fd_fo_pos = 1
			end
		elseif jj == state_idx + 11 then		--84
			-- AT arm
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_autopilot_autothr_arm_pos = 0
			else
				B738DR_autopilot_autothr_arm_pos = 1
			end
		elseif jj == state_idx + 12 then		--85
			-- Bank angle
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_autopilot_bank_angle_pos = 0
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_autopilot_bank_angle_pos = 1
			elseif string.sub(state_cmd, jj, jj) == "2" then
				B738DR_autopilot_bank_angle_pos = 2
			elseif string.sub(state_cmd, jj, jj) == "3" then
				B738DR_autopilot_bank_angle_pos = 3
			else
				B738DR_autopilot_bank_angle_pos = 4
			end
		-- elseif jj == state_idx + 13 then		--86
			-- -- N1 set source
			-- if string.sub(state_cmd, jj, jj) == "0" then
				-- B738DR_n1_set_source = -2
			-- elseif string.sub(state_cmd, jj, jj) == "1" then
				-- B738DR_n1_set_source = -1
			-- elseif string.sub(state_cmd, jj, jj) == "2" then
				-- B738DR_n1_set_source = 0
			-- else
				-- B738DR_n1_set_source = 1
			-- end
		elseif jj == state_idx + 14 then		--87
			-- EFIS Baro mode CPT
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_baro_mode_capt = 0
			else
				B738DR_efis_baro_mode_capt = 1
			end
		elseif jj == state_idx + 15 then		--88
			-- Minimums CPT
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_minim_capt = 0
			else
				B738DR_minim_capt = 1
			end
			if B738DR_minim_capt == 0 then	--radio DH
				B738DR_dh_pilot = -1
			else
				B738DR_dh_pilot = -1001
			end
			B738DR_minim_capt_show = 0
			radio_dh_pilot = -1
			baro_dh_pilot = -1001
			radio_dh_pilot_mem = 100	-- -1
			baro_dh_pilot_mem = 200		-- -1001
		elseif jj == state_idx + 16 then		--89
			-- EFIS Baro mode FO
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_efis_baro_mode_fo = 0
			else
				B738DR_efis_baro_mode_fo = 1
			end
		elseif jj == state_idx + 17 then		--90
			-- Minimums FO
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_minim_fo = 0
			else
				B738DR_minim_fo = 1
			end
			if B738DR_minim_fo == 0 then	--radio DH
				B738DR_dh_copilot = -1
			else
				B738DR_dh_copilot = -1001
			end
			B738DR_minim_fo_show = 0
			radio_dh_copilot = -1
			baro_dh_copilot = -1001
			radio_dh_copilot_mem = 100	-- -1
			baro_dh_copilot_mem = 200	-- -1001
		elseif jj == state_idx + 18 then		--91
			-- VHF SOURCE
			if string.sub(state_cmd, jj, jj) == "0" then
				B738DR_autopilot_vhf_source_pos = -1
			elseif string.sub(state_cmd, jj, jj) == "1" then
				B738DR_autopilot_vhf_source_pos = 0
			else
				B738DR_autopilot_vhf_source_pos = 1
			end
		end
	end
	end
	
end


function B738_create_state()
	
	local state_cmd = B738DR_state_cmd
	
	-- EFIS CPT VOR1
	if B738DR_efis_vor1_capt_pos == -1 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_efis_vor1_capt_pos == 0 then
		state_cmd = state_cmd .. "1"
	else
		state_cmd = state_cmd .. "2"
	end
	-- EFIS CPT VOR2
	if B738DR_efis_vor2_capt_pos == -1 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_efis_vor2_capt_pos == 0 then
		state_cmd = state_cmd .. "1"
	else
		state_cmd = state_cmd .. "2"
	end
	-- EFIS FO VOR1
	if B738DR_efis_vor1_fo_pos == -1 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_efis_vor1_fo_pos == 0 then
		state_cmd = state_cmd .. "1"
	else
		state_cmd = state_cmd .. "2"
	end
	-- EFIS FO VOR2
	if B738DR_efis_vor2_fo_pos == -1 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_efis_vor2_fo_pos == 0 then
		state_cmd = state_cmd .. "1"
	else
		state_cmd = state_cmd .. "2"
	end
	-- EFIS CPT MAP MODE
	if B738DR_capt_map_mode == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_capt_map_mode == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_capt_map_mode == 2 then
		state_cmd = state_cmd .. "2"
	else
		state_cmd = state_cmd .. "3"
	end
	-- EFIS FO MAP MODE
	if B738DR_fo_map_mode == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_fo_map_mode == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_fo_map_mode == 2 then
		state_cmd = state_cmd .. "2"
	else
		state_cmd = state_cmd .. "3"
	end
	-- EFIS CPT RANGE
	if B738DR_efis_map_range_capt == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_efis_map_range_capt == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_efis_map_range_capt == 2 then
		state_cmd = state_cmd .. "2"
	elseif B738DR_efis_map_range_capt == 3 then
		state_cmd = state_cmd .. "3"
	elseif B738DR_efis_map_range_capt == 4 then
		state_cmd = state_cmd .. "4"
	elseif B738DR_efis_map_range_capt == 5 then
		state_cmd = state_cmd .. "5"
	elseif B738DR_efis_map_range_capt == 6 then
		state_cmd = state_cmd .. "6"
	else
		state_cmd = state_cmd .. "7"
	end
	-- EFIS FO RANGE
	if B738DR_efis_map_range_fo == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_efis_map_range_fo == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_efis_map_range_fo == 2 then
		state_cmd = state_cmd .. "2"
	elseif B738DR_efis_map_range_fo == 3 then
		state_cmd = state_cmd .. "3"
	elseif B738DR_efis_map_range_fo == 4 then
		state_cmd = state_cmd .. "4"
	elseif B738DR_efis_map_range_fo == 5 then
		state_cmd = state_cmd .. "5"
	elseif B738DR_efis_map_range_fo == 6 then
		state_cmd = state_cmd .. "6"
	else
		state_cmd = state_cmd .. "7"
	end
	-- Disconnect AP bar
	if B738DR_autopilot_disconnect_pos == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- FD captain
	if B738DR_autopilot_fd_pos == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- FD FO
	if B738DR_autopilot_fd_fo_pos == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- AT arm
	if B738DR_autopilot_autothr_arm_pos == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- Bank angle
	if B738DR_autopilot_bank_angle_pos == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_autopilot_bank_angle_pos == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_autopilot_bank_angle_pos == 2 then
		state_cmd = state_cmd .. "2"
	elseif B738DR_autopilot_bank_angle_pos == 3 then
		state_cmd = state_cmd .. "3"
	else
		state_cmd = state_cmd .. "4"
	end
	-- -- N1 set source
	-- if B738DR_n1_set_source == -2 then
		-- state_cmd = state_cmd .. "0"
	-- elseif B738DR_n1_set_source == -1 then
		-- state_cmd = state_cmd .. "1"
	-- elseif B738DR_n1_set_source == 0 then
		-- state_cmd = state_cmd .. "2"
	-- else
		-- state_cmd = state_cmd .. "3"
	-- end
	state_cmd = state_cmd .. "0"
	-- EFIS Baro CPT
	if B738DR_efis_baro_mode_capt == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- Minimums CPT
	if B738DR_minim_capt == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- EFIS Baro CPT
	if B738DR_efis_baro_mode_fo == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- Minimums CPT
	if B738DR_minim_fo == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	
	if B738DR_autopilot_vhf_source_pos == -1 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_autopilot_vhf_source_pos == 0 then
		state_cmd = state_cmd .. "1"
	else
		state_cmd = state_cmd .. "2"
	end
	
	B738DR_state_cmd = state_cmd
	
end

function B738_code_mem2()
	if simDR_int_ver > 113601 then
		local idx_mem = 15	-- 16 - 1
		for mm = 1, 7 do
			if B738DR_pfd_spd_mode == mm then
				simDR_mem2[idx_mem + mm] = 1
			else
				simDR_mem2[idx_mem + mm] = 0
			end
		end
		idx_mem = 22	-- 15 + 7
		for mm = 1, 5 do
			if B738DR_pfd_hdg_mode == mm then
				simDR_mem2[idx_mem + mm] = 1
			else
				simDR_mem2[idx_mem + mm] = 0
			end
		end
		idx_mem = 27	-- 22 + 5
		for mm = 1, 11 do
			if B738DR_pfd_alt_mode == mm then
				simDR_mem2[idx_mem + mm] = 1
			else
				simDR_mem2[idx_mem + mm] = 0
			end
		end
		idx_mem = 38	-- 27 + 11
		for mm = 1, 4 do
			if B738DR_pfd_hdg_mode_arm == mm then
				simDR_mem2[idx_mem + mm] = 1
			else
				simDR_mem2[idx_mem + mm] = 0
			end
		end
		idx_mem = 42	-- 38 + 4
		for mm = 1, 5 do
			if B738DR_pfd_alt_mode_arm == mm then
				simDR_mem2[idx_mem + mm] = 1
			else
				simDR_mem2[idx_mem + mm] = 0
			end
		end
		simDR_mem2[48] = B738DR_single_ch_status
		if B738DR_flare_status == 0 then	--AUTOLAND
			simDR_mem2[49] = 0
		else
			simDR_mem2[49] = 1
		end
		simDR_mem2[50] = B738DR_cws_r_status
		simDR_mem2[51] = B738DR_cws_p_status
		if B738DR_pfd_fd_cmd == 0 then
			simDR_mem2[52] = 0
			simDR_mem2[53] = 0
		elseif B738DR_pfd_fd_cmd == 1 then
			simDR_mem2[52] = 1
			simDR_mem2[53] = 0
		else
			simDR_mem2[52] = 0
			simDR_mem2[53] = 1
		end
		
		simDR_mem2[54] = B738DR_autopilot_n1_status
		simDR_mem2[55] = B738DR_autopilot_speed_status
		simDR_mem2[56] = B738DR_autopilot_hdg_sel_status
		simDR_mem2[57] = B738DR_autopilot_vorloc_status
		simDR_mem2[58] = B738DR_autopilot_app_status
		simDR_mem2[59] = B738DR_autopilot_lnav_status
		simDR_mem2[60] = B738DR_autopilot_vs_status
		simDR_mem2[61] = B738DR_autopilot_lvl_chg_status
		simDR_mem2[62] = B738DR_autopilot_alt_hld_status
		simDR_mem2[63] = B738DR_autopilot_vnav_status
		simDR_mem2[64] = B738DR_autopilot_cmd_a_status
		simDR_mem2[65] = B738DR_autopilot_cmd_b_status
		simDR_mem2[66] = B738DR_autopilot_cws_a_status
		simDR_mem2[67] = B738DR_autopilot_cws_b_status
		simDR_mem2[68] = B738DR_autopilot_autothr_arm_pos
		simDR_mem2[69] = autopilot_autothrottle_status
		
		flare_status_mem = B738DR_flare_status
		autothr_arm_pos_mem = B738DR_autopilot_autothr_arm_pos
		autothrottle_status_mem = autopilot_autothrottle_status
		
		simDR_mem2[70] = B738DR_touchdown[0]
		simDR_mem2[71] = B738DR_touchdown[1]
		simDR_mem2[72] = B738DR_touchdown[2]
		
	end
end

function is_mem_on(mem_val)
	if mem_val ~= 0 then
		return 1
	else
		return 0
	end
end

function B738_decode_mem2()
	if simDR_int_ver > 113601 then
		local idx_mem = 15
		B738DR_pfd_spd_mode = 0
		for mm = 1, 7 do
			if simDR_mem2[idx_mem + mm] ~= 0 then
				B738DR_pfd_spd_mode = mm
			end
		end
		idx_mem = 22
		B738DR_pfd_hdg_mode = 0
		for mm = 1, 5 do
			if simDR_mem2[idx_mem + mm] ~= 0 then
				B738DR_pfd_hdg_mode = mm
			end
		end
		idx_mem = 27
		B738DR_pfd_alt_mode = 0
		for mm = 1, 11 do
			if simDR_mem2[idx_mem + mm] ~= 0 then
				B738DR_pfd_alt_mode = mm
			end
		end
		idx_mem = 38
		B738DR_pfd_hdg_mode_arm = 0
		for mm = 1, 4 do
			if simDR_mem2[idx_mem + mm] ~= 0 then
				B738DR_pfd_hdg_mode_arm = mm
			end
		end
		idx_mem = 42
		B738DR_pfd_alt_mode_arm = 0
		for mm = 1, 5 do
			if simDR_mem2[idx_mem + mm] ~= 0 then
				B738DR_pfd_alt_mode_arm = mm
			end
		end
		
		B738DR_single_ch_status = is_mem_on(simDR_mem2[48])
		B738DR_flare_status = is_mem_on(simDR_mem2[49])
		B738DR_cws_r_status = is_mem_on(simDR_mem2[50])
		B738DR_cws_p_status = is_mem_on(simDR_mem2[51])
		if is_mem_on(simDR_mem2[52]) then
			B738DR_pfd_fd_cmd = 1
		elseif is_mem_on(simDR_mem2[53]) then
			B738DR_pfd_fd_cmd = 2
		else
			B738DR_pfd_fd_cmd = 0
		end
		
		B738DR_autopilot_n1_status = is_mem_on(simDR_mem2[54])
		B738DR_autopilot_speed_status = is_mem_on(simDR_mem2[55])
		B738DR_autopilot_hdg_sel_status = is_mem_on(simDR_mem2[56])
		B738DR_autopilot_vorloc_status = is_mem_on(simDR_mem2[57])
		B738DR_autopilot_app_status = is_mem_on(simDR_mem2[58])
		B738DR_autopilot_lnav_status = is_mem_on(simDR_mem2[59])
		B738DR_autopilot_vs_status = is_mem_on(simDR_mem2[60])
		B738DR_autopilot_lvl_chg_status = is_mem_on(simDR_mem2[61])
		B738DR_autopilot_alt_hld_status = is_mem_on(simDR_mem2[62])
		B738DR_autopilot_vnav_status = is_mem_on(simDR_mem2[63])
		B738DR_autopilot_cmd_a_status = is_mem_on(simDR_mem2[64])
		B738DR_autopilot_cmd_b_status = is_mem_on(simDR_mem2[65])
		B738DR_autopilot_cws_a_status = is_mem_on(simDR_mem2[66])
		B738DR_autopilot_cws_b_status = is_mem_on(simDR_mem2[67])
		B738DR_autopilot_autothr_arm_pos = is_mem_on(simDR_mem2[68])
		autopilot_autothrottle_status = is_mem_on(simDR_mem2[69])
		
		B738DR_touchdown[0] = simDR_mem2[70]
		B738DR_touchdown[1] = simDR_mem2[71]
		B738DR_touchdown[2] = simDR_mem2[72]
	end
end

function load_save(in_mode)
	raw_table('glare')
	local glare = {}
	raw_table('glare_sw')
	local glare_sw = {}
	local ggg = 0
	
	if B738DR_load_save == 4 then
		for ggg = 1, 250 do
			glare[ggg] = B738DR_glare[ggg-1]
			glare_sw[ggg] = B738DR_glare_sw[ggg-1]
		end
		
		null_vvi = glare[1]
		ap_pitch_mode = glare[2]
		ap_roll_mode = glare[3]
		ap_pitch_mode_eng = glare[4]
		ap_roll_mode_eng = glare[5]
		ap_on = glare[6]
		ap1_on = glare[7]
		ap2_on = glare[8]
		ap_on_first = glare[9]
		at_on_first = glare[10]
		cws_on = glare[11]
		cws_on_first = glare[12]
		ap_disco_first = glare[13]
		ap_dis_time = glare[14]
		at_dis_time = glare[15]
		mem_airspeed_dial = glare[16]
		mem_speed_mode = glare[17]
		at_mode = glare[18]
		at_mode_eng = glare[19]
		ap_pitch_mode_old = glare[20]
		ap_roll_mode_old = glare[21]
		approach_status_old = glare[22]
		glideslope_status_old = glare[23]
		takeoff_n1 = glare[24]
		cmd_first = glare[25]
		ap_app_block = glare[26]
		ap_app_block_800 = glare[27]
		ils_test_enable = glare[28]
		ils_test_on = glare[29]
		ils_test_ok = glare[30]
		ils_test_ok2 = ils_test_ok
		fd_cur = glare[31]
		ap_vnav_status = glare[32]
		ap_lnav_status = glare[33]
		to_after_80kts = glare[34]
		lift_off_150 = glare[35]
		on_ground_30 = glare[36]
		ap_goaround_block = glare[37]
		to_thrust_set = glare[38]
		ias_disagree = glare[39]
		alt_disagree = glare[40]
		vnav_alt = glare[41]
		B738DR_at_throttle_hold = glare[42]
		init_climb = glare[43]
		baro_sel_old = glare[44]
		baro_sel_co_old = glare[45]
		fmc_vvi_cur = glare[46]
		vvi_trg = glare[47]
		vnav_vs = glare[48]
		lvl_chg_vs = glare[49]
		vnav_alt_hld = glare[50]
		altitude_dial_ft_old = glare[51]
		reverse_max_enable1 = glare[52]
		reverse_max_on1 = glare[53]
		reverse_max_enable2 = glare[54]
		reverse_max_on2 = glare[55]
		radio_dh_pilot = glare[56]
		baro_dh_pilot = glare[57]
		baro_dh_pilot_disable = glare[58]
		radio_dh_copilot = glare[59]
		baro_dh_copilot = glare[60]
		baro_dh_copilot_disable = glare[61]
		radio_dh_pilot_mem = glare[62]
		baro_dh_pilot_mem = glare[63]
		radio_dh_copilot_mem = glare[64]
		baro_dh_copilot_mem = glare[65]
		dh_minimum_pilot = glare[66]
		dh_minimum_copilot = glare[67]
		dh_min_enable_pilot = glare[68]
		dh_min_enable_copilot = glare[69]
		dh_min_block_pilot = glare[70]
		dh_min_block_copilot = glare[71]
		vnav_init = glare[72]
		vnav_init2 = glare[73]
		vs_first = glare[74]
		vnav_alt_hold = glare[75]
		vnav_alt_hold_act = glare[76]
		mcp_alt_hold = glare[77]
		rest_wpt_alt_old = glare[78]
		vnav_engaged = glare[79]
		ap_alt_hold_arm = glare[80]
		roll_mode_old = glare[81]
		pitch_mode_old = glare[82]
		at_on_old = glare[83]
		ap_disco_do = glare[84]
		vnav_desc_spd = glare[85]
		vnav_desc_protect_spd = glare[86]
		ap_disco2 = glare[87]
		turn_active = glare[88]
		intdir_act = glare[89]
		thr1_anim = glare[90]
		thr2_anim = glare[91]
		fd_go_disable = glare[92]
		ap_go_disable = glare[93]
		at_status = glare[94]
		lvl_chg_co_status = glare[95]
		at_left_press = glare[96]
		at_right_press = glare[97]
		master_light_block_act = glare[98]
		cpt_tcas_enable = glare[99]
		fo_tcas_enable = glare[100]
		cpt_terr_enable = glare[101]
		fo_terr_enable = glare[102]
		cpt_pos_enable = glare[103]
		fo_pos_enable = glare[104]
		autothr_arm_pos = glare[105]
		autopilot_side = glare[106]
		autopilot_master_first = glare[107]
		mcp_hdg_dial_old = glare[108]
		hide_hdg_line = glare[109]
		hide_hdg_line_fo = glare[110]
		alt_acq_disable = glare[111]
		dspl_ctrl_pnl_old = glare[112]
		in_hpa_cnt = glare[113]
		shake_cpt = glare[114]
		shake_fo = glare[115]
		relative_brg_old = glare[116]
		gear_deploy_0_old = glare[117]
		gear_deploy_1_old = glare[118]
		gear_deploy_2_old = glare[119]
		yoke_test_disabled = glare[120]
		alt_deviation_active = glare[121]
		lnav_tune_enable = glare[122]
		idx_corr2_anim = glare[123]
		max_idx_dist_anim = glare[124]
		max_idx_dist_anim2 = glare[125]
		integral = glare[126]
		lvlchg_step_init = glare[127]
		vnav_step_init = glare[128]
		hold_bank_angle_enable = glare[129]
		hold_bank_angle = glare[130]
		pfd_loc_exp = glare[131]
		pfd_loc_exp_fo = glare[132]
		pfd_fac_exp = glare[133]
		pfd_fac_exp_fo = glare[134]
		pfd_gls_exp = glare[135]
		pfd_gls_exp_fo = glare[136]
		vor_loc1_captured = glare[137]
		vor_loc2_captured = glare[138]
		autopilot_cmd_a_status = glare[139]
		autopilot_cmd_b_status = glare[140]
		autopilot_cws_a_status = glare[141]
		autopilot_cws_b_status = glare[142]
		autopilot_cws_status = glare[143]
		autopilot_cws_r_status = glare[144]
		autopilot_cws_p_status = glare[145]
		autopilot_fms_nav_status = glare[146]
		aircraft_was_on_air = glare[147]
		bellow_400ft = glare[148]
		altitude_dial_ft = glare[149]
		yoke_pitch = glare[150]
		yoke_pitch2 = glare[151]
		vorloc_only = glare[152]
		throttle = glare[153]
		throttle2 = glare[153]
		altitude_mode_old = glare[154]
		flight_phase_old = glare[155]
		capt_exp_map_mode = glare[156]
		fo_exp_map_mode = glare[157]
		capt_vsd_map_mode = glare[158]
		fo_vsd_map_mode = glare[159]
		capt_exp_vor_app_mode = glare[160]
		fo_exp_vor_app_mode = glare[161]
		ap_capt_disco_status = glare[162]
		ap_fo_disco_status = glare[163]
		shake_on1 = glare[164]
		shake_on2 = glare[165]
		lowerDU_set = glare[166]
		takeoff_eng_out = glare[167]
		vnav_desc_idle = glare[168]
		vnav_desc_idle_time = glare[169]
		mcp_speed_dial_old = glare[170]
		flaps_ratio_old = glare[171]
		hdg_turn = glare[172]
		-- eng1_N1_thrust_cur_old = glare[173]
		-- eng2_N1_thrust_cur_old = glare[174]
		hdg_sel_to = glare[175]
		mcp_alt_dial_lock = glare[175]
		mcp_alt_dial_app = glare[176]
		mcp_alt_dial_old = glare[177]
		lnav_lock_hdg = glare[178]
		retard_enabled = glare[179]
		throttle1_use_old = glare[180]
		throttle2_use_old = glare[181]
		throttle_all_use_old = (throttle1_use_old + throttle2_use_old) / 2
		at_mode_change = glare[182]
		yoke_off = glare[183]
		mem_attitude = glare[184]
		mem_attitude_deg = glare[185]
		servo_yoke = glare[186]
		servo_yoke_old = glare[187]
		if servo_yoke == 1 then
			B738DR_pitch_ratio2_old = simDR_pitch_ratio2
			roll_ratio2_old = simDR_roll_ratio2
			-- ap_elevator_trim = simDR_elevator_trim
			-- trim_target = ap_elevator_trim
			-- ap_new_trim = 0
			-- ap_new_trim_disable = 0
			pitch_ratio_diff = 0
			roll_ratio_diff = 0
			servo_i_pitch = 0
			servo_ii_pitch = 0
			last_err_pitch = 0
			servo_k_pitch_old = 0
			pitch_ratio_trend = 0
			pitch_ratio_last_trend = 0
		end
		fdir_pitch_anim = glare[188]
		fdir_pitch_ovr_old = glare[189]
		to_ga_hdg = glare[190]
		vvi_trend2 = glare[191]
		flare_vvi_old2 = glare[192]
		mcp_speed_dial_mem = glare[193]
		to_ga_mcp_changed = glare[194]
		gls_pth_alt_old = glare[195]
		gls_vvi_cur = glare[196]
		lpv_vvi_cur = glare[197]
		lpv_pth_alt_old = glare[198]
		gls_active = glare[199]
		mmr_act_mode = glare[200]
		mmr_act_mode_old = glare[201]
		gls_type = glare[202]
		ac_tnsbus1_status_old = glare[203]
		ac_tnsbus2_status_old = glare[204]
		source_off_bus1_old = glare[205]
		source_off_bus2_old = glare[206]
		lvlchg_tgt_spd = glare[207]
		vnav_tgt_spd = glare[208]
		afs_protect_speed = glare[209]
		snd_ap_disconnect_old = glare[210]
		snd_ap_disco_delay = glare[211]
		ignore_td_idx2 = glare[212]
		-- was_fmc_speed = glare[213]
		ap_lnav_hdg = glare[214]
		B738DR_retard_status = glare[215]
		B738DR_rollout_status = glare[216]
		B738DR_gls_gs_status = glare[217]
		B738DR_autoland_status = glare[218]
		B738DR_fac_gp_engaged = glare[219]
		B738DR_loc_gp_engaged = glare[220]
		B738DR_gls_app_status = glare[221]
		B738DR_gls_nav_status = glare[222]
		B738DR_gls_crs = glare[223]
		B738DR_lpv_app_status = glare[224]
		B738DR_lpv_gs_status = glare[225]
		B738DR_lnav_engaged = glare[226]
		B738DR_mcp_speed_dial = glare[227]
		B738DR_mcp_alt_dial = glare[228]
		B738DR_mcp_hdg_dial = glare[229]
		B738DR_course_pilot = glare[230]
		B738DR_course_copilot = glare[231]
		simDR_ap_vvi_dial = glare[232]
		B738DR_ap_vvi_dial = simDR_ap_vvi_dial
		B738DR_vnav_alt_mode = glare[233]
		autopilot_autothrottle_status = glare[234]
		B738DR_baro_set_std_pilot = glare[235]
		B738DR_baro_sel_in_hg_pilot = glare[236]
		B738DR_efis_baro_mode_capt = glare[237]
		B738DR_efis_baro_mode_capt_pfd = glare[238]
		B738DR_baro_sel_pilot_show = glare[239]
		B738DR_baro_box_pilot_show = glare[240]
		B738DR_baro_std_box_pilot_show = glare[241]
		B738DR_baro_set_std_copilot = glare[242]
		B738DR_baro_sel_in_hg_copilot = glare[243]
		B738DR_efis_baro_mode_fo = glare[244]
		B738DR_efis_baro_mode_fo_pfd = glare[245]
		B738DR_baro_sel_copilot_show = glare[246]
		B738DR_baro_box_copilot_show = glare[247]
		B738DR_baro_std_box_copilot_show = glare[248]
		B738DR_ap_spd_interv_status = glare[249]
		B738DR_flare_status = glare[250]
		
		--SW
		B738DR_efis_vor1_capt_pos = glare_sw[1]
		if B738DR_efis_vor1_capt_pos == 1 then
			simDR_vor1_capt = 2
		else
			simDR_vor1_capt = 1
		end
		B738DR_efis_vor2_capt_pos = glare_sw[2]
		if B738DR_efis_vor2_capt_pos == 1 then
			simDR_vor2_capt = 2
		else
			simDR_vor2_capt = 1
		end
		B738DR_efis_vor1_fo_pos = glare_sw[3]
		if B738DR_efis_vor1_fo_pos == 1 then
			simDR_vor1_fo = 2
		else
			simDR_vor1_fo = 1
		end
		B738DR_efis_vor2_fo_pos = glare_sw[4]
		if B738DR_efis_vor2_fo_pos == 1 then
			simDR_vor2_fo = 2
		else
			simDR_vor2_fo = 1
		end
		B738DR_capt_map_mode = glare_sw[5]
		B738DR_fo_map_mode = glare_sw[6]
		B738DR_efis_map_range_capt = glare_sw[7]
		B738DR_efis_map_range_fo = glare_sw[8]
		B738DR_autopilot_disconnect_pos = glare_sw[9]
		B738DR_autopilot_fd_pos = glare_sw[10]
		B738DR_autopilot_fd_fo_pos = glare_sw[11]
		B738DR_autopilot_autothr_arm_pos = glare_sw[12]
		B738DR_autopilot_bank_angle_pos = glare_sw[13]
		-- B738DR_n1_set_source = glare_sw[14]
		B738DR_efis_baro_mode_capt = glare_sw[15]
		B738DR_minim_capt = glare_sw[16]
		B738DR_efis_baro_mode_fo = glare_sw[17]
		B738DR_minim_fo = glare_sw[18]
		B738DR_lvl_chg_mode = glare_sw[19]
		simDR_fdir_pitch_ovr = glare_sw[20]
		alt_hold_captured = glare_sw[21]
		initial_vs_alt = glare_sw[22]
		mdah_was_enable = glare_sw[23]
		vorloc_lnav_enable = glare_sw[24]
		B738DR_vorloc_lnav_enable = glare_sw[25]
		B738DR_vorloc_lnav = glare_sw[26]
		lnav_ma_armed = glare_sw[27]
		B738DR_autoland_install = glare_sw[28]
		first_time_baro_cpt = glare_sw[29]
		first_time_baro_fo = glare_sw[30]
		B738DR_fac_status = glare_sw[31]
		recycled_fd_roll = glare_sw[32]
		-- B738DR_alt_hold_vnav = glare_sw[32]
		B738DR_at_takeoff = glare_sw[33]
		spd_interv_co_used = glare_sw[34]
		
		B738DR_efis_vor_on = glare_sw[35]
		B738DR_efis_apt_on = glare_sw[36]
		B738DR_efis_fix_on = glare_sw[37]
		B738DR_efis_wxr_on = glare_sw[38]
		B738DR_efis_wxr_capt = glare_sw[39]
		B738DR_efis_fo_vor_on = glare_sw[40]
		B738DR_efis_fo_apt_on = glare_sw[41]
		B738DR_efis_fo_fix_on = glare_sw[42]
		B738DR_efis_fo_wxr_on = glare_sw[43]
		simDR_efis_wxr_on = glare_sw[44]
		B738DR_efis_wxr_terr = glare_sw[45]
		B738DR_efis_fo_wxr_terr = glare_sw[46]
		
		B738DR_efis_vor1_capt_pfd = glare_sw[47]
		B738DR_efis_vor2_capt_pfd = glare_sw[48]
		B738DR_efis_vor1_fo_pfd = glare_sw[49]
		B738DR_efis_vor2_fo_pfd = glare_sw[50]
		
		timer_gyro = glare_sw[51]
		roll_elec_deg_stdby = glare_sw[52]
		pitch_elec_deg_stdby = glare_sw[53]
		ac_stdbus_timer = glare_sw[54]
		
		B738DR_minim_capt_show = glare_sw[55]
		B738DR_minim_fo_show = glare_sw[56]
		B738DR_dh_pilot = glare_sw[57]
		B738DR_dh_copilot = glare_sw[58]
		
		blink_rec_thr_status = 0
		blink_rec_thr2_status = 0
		blink_rec_hdg_status = 0
		blink_rec_alt_status = 0
		blink_rec_alt_alert_status = 0
		blink_rec_cmd_status1 = 0
		blink_rec_cmd_status2 = 0
		blink_rec_sch_status = 0
		blink_rec_schg_status = 0
		blink_rec_cws_r_status = 0
		blink_rec_cws_p_status = 0
		blink_rec_spd_ind = 0
		B738DR_spd_ind_mode = 0
		spd_ind_mode_old = 0
		
		if is_timer_scheduled(annun_flare) == true then
			stop_timer(annun_flare)
		end
		if is_timer_scheduled(ils_test_off) == true then
			stop_timer(ils_test_off)
		end
	elseif in_mode ~= 0 then
		for ggg = 1, 250 do
			glare[ggg] = 0
			glare_sw[ggg] = 0
		end
		
		glare[1] = null_vvi
		glare[2] = ap_pitch_mode
		glare[3] = ap_roll_mode
		glare[4] = ap_pitch_mode_eng
		glare[5] = ap_roll_mode_eng
		glare[6] = ap_on
		glare[7] = ap1_on
		glare[8] = ap2_on
		glare[9] = ap_on_first
		glare[10] = at_on_first
		glare[11] = cws_on
		glare[12] = cws_on_first
		glare[13] = ap_disco_first
		glare[14] = ap_dis_time
		glare[15] = at_dis_time
		glare[16] = mem_airspeed_dial
		glare[17] = mem_speed_mode
		glare[18] = at_mode
		glare[19] = at_mode_eng
		glare[20] = ap_pitch_mode_old
		glare[21] = ap_roll_mode_old
		glare[22] = approach_status_old
		glare[23] = glideslope_status_old
		glare[24] = takeoff_n1
		glare[25] = cmd_first
		glare[26] = ap_app_block
		glare[27] = ap_app_block_800
		glare[28] = ils_test_enable
		glare[29] = ils_test_on
		glare[30] = ils_test_ok
		glare[31] = fd_cur
		glare[32] = ap_vnav_status
		glare[33] = ap_lnav_status
		glare[34] = to_after_80kts
		glare[35] = lift_off_150
		glare[36] = on_ground_30
		glare[37] = ap_goaround_block
		glare[38] = to_thrust_set
		glare[39] = ias_disagree
		glare[40] = alt_disagree
		glare[41] = vnav_alt
		glare[42] = B738DR_at_throttle_hold
		glare[43] = init_climb
		glare[44] = baro_sel_old
		glare[45] = baro_sel_co_old
		glare[46] = fmc_vvi_cur
		glare[47] = vvi_trg
		glare[48] = vnav_vs
		glare[49] = lvl_chg_vs
		glare[50] = vnav_alt_hld
		glare[51] = altitude_dial_ft_old
		glare[52] = reverse_max_enable1
		glare[53] = reverse_max_on1
		glare[54] = reverse_max_enable2
		glare[55] = reverse_max_on2
		glare[56] = radio_dh_pilot
		glare[57] = baro_dh_pilot
		glare[58] = baro_dh_pilot_disable
		glare[59] = radio_dh_copilot
		glare[60] = baro_dh_copilot
		glare[61] = baro_dh_copilot_disable
		glare[62] = radio_dh_pilot_mem
		glare[63] = baro_dh_pilot_mem
		glare[64] = radio_dh_copilot_mem
		glare[65] = baro_dh_copilot_mem
		glare[66] = dh_minimum_pilot
		glare[67] = dh_minimum_copilot
		glare[68] = dh_min_enable_pilot
		glare[69] = dh_min_enable_copilot
		glare[70] = dh_min_block_pilot
		glare[71] = dh_min_block_copilot
		glare[72] = vnav_init
		glare[73] = vnav_init2
		glare[74] = vs_first
		glare[75] = vnav_alt_hold
		glare[76] = vnav_alt_hold_act
		glare[77] = mcp_alt_hold
		glare[78] = rest_wpt_alt_old
		glare[79] = vnav_engaged
		glare[80] = ap_alt_hold_arm
		glare[81] = roll_mode_old
		glare[82] = pitch_mode_old
		glare[83] = at_on_old
		glare[84] = ap_disco_do
		glare[85] = vnav_desc_spd
		glare[86] = vnav_desc_protect_spd
		glare[87] = ap_disco2
		glare[88] = turn_active
		glare[89] = intdir_act
		glare[90] = thr1_anim
		glare[91] = thr2_anim
		glare[92] = fd_go_disable
		glare[93] = ap_go_disable
		glare[94] = at_status
		glare[95] = lvl_chg_co_status
		glare[96] = at_left_press
		glare[97] = at_right_press
		glare[98] = master_light_block_act
		glare[99] = cpt_tcas_enable
		glare[100] = fo_tcas_enable
		glare[101] = cpt_terr_enable
		glare[102] = fo_terr_enable
		glare[103] = cpt_pos_enable
		glare[104] = fo_pos_enable
		glare[105] = autothr_arm_pos
		glare[106] = autopilot_side
		glare[107] = autopilot_master_first
		glare[108] = mcp_hdg_dial_old
		glare[109] = hide_hdg_line
		glare[110] = hide_hdg_line_fo
		glare[111] = alt_acq_disable
		glare[112] = dspl_ctrl_pnl_old
		glare[113] = in_hpa_cnt
		glare[114] = shake_cpt
		glare[115] = shake_fo
		glare[116] = relative_brg_old
		glare[117] = gear_deploy_0_old
		glare[118] = gear_deploy_1_old
		glare[119] = gear_deploy_2_old
		glare[120] = yoke_test_disabled
		glare[121] = alt_deviation_active
		glare[122] = lnav_tune_enable
		glare[123] = idx_corr2_anim
		glare[124] = max_idx_dist_anim
		glare[125] = max_idx_dist_anim2
		glare[126] = integral
		glare[127] = lvlchg_step_init
		glare[128] = vnav_step_init
		glare[129] = hold_bank_angle_enable
		glare[130] = hold_bank_angle
		glare[131] = pfd_loc_exp
		glare[132] = pfd_loc_exp_fo
		glare[133] = pfd_fac_exp
		glare[134] = pfd_fac_exp_fo
		glare[135] = pfd_gls_exp
		glare[136] = pfd_gls_exp_fo
		glare[137] = vor_loc1_captured
		glare[138] = vor_loc2_captured
		glare[139] = autopilot_cmd_a_status
		glare[140] = autopilot_cmd_b_status
		glare[141] = autopilot_cws_a_status
		glare[142] = autopilot_cws_b_status
		glare[143] = autopilot_cws_status
		glare[144] = autopilot_cws_r_status
		glare[145] = autopilot_cws_p_status
		glare[146] = autopilot_fms_nav_status
		glare[147] = aircraft_was_on_air
		glare[148] = bellow_400ft
		glare[149] = altitude_dial_ft
		glare[150] = yoke_pitch
		glare[151] = yoke_pitch2
		glare[152] = vorloc_only
		glare[153] = throttle
		glare[154] = altitude_mode_old
		glare[155] = flight_phase_old
		glare[156] = capt_exp_map_mode
		glare[157] = fo_exp_map_mode
		glare[158] = capt_vsd_map_mode
		glare[159] = fo_vsd_map_mode
		glare[160] = capt_exp_vor_app_mode
		glare[161] = fo_exp_vor_app_mode
		glare[162] = ap_capt_disco_status
		glare[163] = ap_fo_disco_status
		glare[164] = shake_on1
		glare[165] = shake_on2
		glare[166] = lowerDU_set
		glare[167] = takeoff_eng_out
		glare[168] = vnav_desc_idle
		glare[169] = vnav_desc_idle_time
		glare[170] = mcp_speed_dial_old
		glare[171] = flaps_ratio_old
		glare[172] = hdg_turn
		-- glare[173] = eng1_N1_thrust_cur_old
		-- glare[174] = eng2_N1_thrust_cur_old
		glare[175] = hdg_sel_to
		glare[175] = mcp_alt_dial_lock
		glare[176] = mcp_alt_dial_app
		glare[177] = mcp_alt_dial_old
		glare[178] = lnav_lock_hdg
		glare[179] = retard_enabled
		glare[180] = throttle1_use_old
		glare[181] = throttle2_use_old
		glare[182] = at_mode_change
		glare[183] = yoke_off
		glare[184] = mem_attitude
		glare[185] = mem_attitude_deg
		glare[186] = servo_yoke
		glare[187] = servo_yoke_old
		glare[188] = fdir_pitch_anim
		glare[189] = fdir_pitch_ovr_old
		glare[190] = to_ga_hdg
		glare[191] = vvi_trend2
		glare[192] = flare_vvi_old2
		glare[193] = mcp_speed_dial_mem
		glare[194] = to_ga_mcp_changed
		glare[195] = gls_pth_alt_old
		glare[196] = gls_vvi_cur
		glare[197] = lpv_vvi_cur
		glare[198] = lpv_pth_alt_old
		glare[199] = gls_active
		glare[200] = mmr_act_mode
		glare[201] = mmr_act_mode_old
		glare[202] = gls_type
		glare[203] = ac_tnsbus1_status_old
		glare[204] = ac_tnsbus2_status_old
		glare[205] = source_off_bus1_old
		glare[206] = source_off_bus2_old
		glare[207] = lvlchg_tgt_spd
		glare[208] = vnav_tgt_spd
		glare[209] = afs_protect_speed
		glare[210] = snd_ap_disconnect_old
		glare[211] = snd_ap_disco_delay
		glare[212] = ignore_td_idx2
		-- glare[213] = was_fmc_speed
		glare[214] = ap_lnav_hdg
		glare[215] = B738DR_retard_status
		glare[216] = B738DR_rollout_status
		glare[217] = B738DR_gls_gs_status
		glare[218] = B738DR_autoland_status
		glare[219] = B738DR_fac_gp_engaged
		glare[220] = B738DR_loc_gp_engaged
		glare[221] = B738DR_gls_app_status
		glare[222] = B738DR_gls_nav_status
		glare[223] = B738DR_gls_crs
		glare[224] = B738DR_lpv_app_status
		glare[225] = B738DR_lpv_gs_status
		glare[226] = B738DR_lnav_engaged
		glare[227] = B738DR_mcp_speed_dial
		glare[228] = B738DR_mcp_alt_dial
		glare[229] = B738DR_mcp_hdg_dial
		glare[230] = B738DR_course_pilot
		glare[231] = B738DR_course_copilot
		glare[232] = simDR_ap_vvi_dial
		glare[233] = B738DR_vnav_alt_mode
		glare[234] = autopilot_autothrottle_status
		glare[235] = B738DR_baro_set_std_pilot
		glare[236] = B738DR_baro_sel_in_hg_pilot
		glare[237] = B738DR_efis_baro_mode_capt
		glare[238] = B738DR_efis_baro_mode_capt_pfd
		glare[239] = B738DR_baro_sel_pilot_show
		glare[240] = B738DR_baro_box_pilot_show
		glare[241] = B738DR_baro_std_box_pilot_show
		glare[242] = B738DR_baro_set_std_copilot
		glare[243] = B738DR_baro_sel_in_hg_copilot
		glare[244] = B738DR_efis_baro_mode_fo
		glare[245] = B738DR_efis_baro_mode_fo_pfd
		glare[246] = B738DR_baro_sel_copilot_show
		glare[247] = B738DR_baro_box_copilot_show
		glare[248] = B738DR_baro_std_box_copilot_show
		glare[249] = B738DR_ap_spd_interv_status
		glare[250] = B738DR_flare_status
		
		--SW
		glare_sw[1] = B738DR_efis_vor1_capt_pos
		glare_sw[2] = B738DR_efis_vor2_capt_pos
		glare_sw[3] = B738DR_efis_vor1_fo_pos
		glare_sw[4] = B738DR_efis_vor2_fo_pos
		glare_sw[5] = B738DR_capt_map_mode
		glare_sw[6] = B738DR_fo_map_mode
		glare_sw[7] = B738DR_efis_map_range_capt
		glare_sw[8] = B738DR_efis_map_range_fo
		glare_sw[9] = B738DR_autopilot_disconnect_pos
		glare_sw[10] = B738DR_autopilot_fd_pos
		glare_sw[11] = B738DR_autopilot_fd_fo_pos
		glare_sw[12] = B738DR_autopilot_autothr_arm_pos
		glare_sw[13] = B738DR_autopilot_bank_angle_pos
		-- glare_sw[14] = B738DR_n1_set_source
		glare_sw[15] = B738DR_efis_baro_mode_capt
		glare_sw[16] = B738DR_minim_capt
		glare_sw[17] = B738DR_efis_baro_mode_fo
		glare_sw[18] = B738DR_minim_fo
		glare_sw[19] = B738DR_lvl_chg_mode
		glare_sw[20] = simDR_fdir_pitch_ovr
		glare_sw[21] = alt_hold_captured
		glare_sw[22] = initial_vs_alt
		glare_sw[23] = mdah_was_enable
		glare_sw[24] = vorloc_lnav_enable
		glare_sw[25] = B738DR_vorloc_lnav_enable
		glare_sw[26] = B738DR_vorloc_lnav
		glare_sw[27] = lnav_ma_armed
		glare_sw[28] = B738DR_autoland_install
		glare_sw[29] = first_time_baro_cpt
		glare_sw[30] = first_time_baro_fo
		glare_sw[31] = B738DR_fac_status
		glare_sw[32] = recycled_fd_roll
		-- glare_sw[32] = B738DR_alt_hold_vnav
		glare_sw[33] = B738DR_at_takeoff
		glare_sw[34] = spd_interv_co_used
		glare_sw[35] = B738DR_efis_vor_on
		glare_sw[36] = B738DR_efis_apt_on
		glare_sw[37] = B738DR_efis_fix_on
		glare_sw[38] = B738DR_efis_wxr_on
		glare_sw[39] = B738DR_efis_wxr_capt
		glare_sw[40] = B738DR_efis_fo_vor_on
		glare_sw[41] = B738DR_efis_fo_apt_on
		glare_sw[42] = B738DR_efis_fo_fix_on
		glare_sw[43] = B738DR_efis_fo_wxr_on
		glare_sw[44] = simDR_efis_wxr_on
		glare_sw[45] = B738DR_efis_wxr_terr
		glare_sw[46] = B738DR_efis_fo_wxr_terr
		glare_sw[47] = B738DR_efis_vor1_capt_pfd
		glare_sw[48] = B738DR_efis_vor2_capt_pfd
		glare_sw[49] = B738DR_efis_vor1_fo_pfd
		glare_sw[50] = B738DR_efis_vor2_fo_pfd
		glare_sw[51] = timer_gyro
		glare_sw[52] = roll_elec_deg_stdby
		glare_sw[53] = pitch_elec_deg_stdby
		glare_sw[54] = ac_stdbus_timer
		
		glare_sw[55] = B738DR_minim_capt_show
		glare_sw[56] = B738DR_minim_fo_show
		glare_sw[57] = B738DR_dh_pilot
		glare_sw[58] = B738DR_dh_copilot
		
		for ggg = 1, 250 do
			B738DR_glare[ggg-1] = glare[ggg]
			if ggg <= 60 then	--to 250
				B738DR_glare_sw[ggg-1] = glare_sw[ggg]
			end
		end
		
	end
	
end

--function flight_crash() end

function before_physics()
	
	if B738DR_kill_glareshield == 0 and load_plane2 ~= 0 then
		B738_nav_source_swap()
	end
	
	if B738DR_dc_bus1_status == 0 or B738DR_dc_bus2_status == 0 then
		if simDR_airspeed_is_mach ~= 0 then
			simCMD_autopilot_co:once()
		end
		if simDR_airspeed_dial ~= 100 then
			simDR_airspeed_dial = 100
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
		end
	end
	
	if B738DR_show_ias ~= show_ias_old and B738DR_show_ias == 1 and simDR_airspeed_is_mach ~= 0 then
		--check mach speed dial
		if simDR_airspeed_dial < 0.60 then
			simCMD_autopilot_co:once()
			simDR_airspeed_dial_kts = simDR_IAS
			-- new mcp
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
		end
	end
	if simDR_airspeed_is_mach == 0 then
		if simDR_airspeed_dial > 340 then
			simDR_airspeed_dial = 340
			-- new mcp
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
		elseif simDR_airspeed_dial < 100 then
			simDR_airspeed_dial = 100
			-- new mcp
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
		end
	else
		if simDR_airspeed_dial > 0.82 then
			simDR_airspeed_dial = 0.82
			-- new mcp
			B738DR_mcp_speed_dial = simDR_airspeed_dial
			update_mcp_speed_dial()
		else
			if B738DR_show_ias == 0 then
				if simDR_airspeed_dial < 0.40 then
					simDR_airspeed_dial = 0.40
					-- new mcp
					B738DR_mcp_speed_dial = simDR_airspeed_dial
					update_mcp_speed_dial()
				end
			else
				if simDR_airspeed_dial < 0.60 then
					simDR_airspeed_dial = 0.60
					-- new mcp
					B738DR_mcp_speed_dial = simDR_airspeed_dial
					update_mcp_speed_dial()
				end
			end
		end
	end
	show_ias_old = B738DR_show_ias

end


-- MCP
-- ROLL MODES
function ap_hdg_cmd()
	if ap_app_block == 0 then
		if B738DR_autopilot_hdg_sel_status == 0 then
			if ap_roll_mode == 4 and (simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1) then
				ap_roll_mode = 10
				ap_roll_mode_eng = 10
				if simDR_autopilot_heading_mode ~= 1 then
					simCMD_autopilot_hdg:once()
				end
			else
				if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 or B738DR_fd_goaround > 0 then
					ap_roll_mode = 1
				end
			end
			hdg_turn = 0
			hdg_sel_to = 0
		else
			if ap_roll_mode == 10 and (simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1) then
				if B738DR_fpln_nav_id == "VECTOR" or B738DR_fpln_nav_id == "DISCON" then
					ap_roll_mode = 0
				else
					ap_roll_mode = 4
				end
			else
				if B738DR_fd_goaround > 0 or B738DR_ap_goaround > 0 then
					ap_roll_mode = 15
					to_ga_hdg = simDR_ahars_mag_hdg
				else
					ap_roll_mode = 0
				end
			end
		end
	else
		if B738DR_autopilot_hdg_sel_status == 1 then
			ap_roll_mode = 0
		end
	end
end

function ap_vor_loc_cmd()
	if ap_app_block == 0 then
		if B738DR_autopilot_vorloc_status == 0 then
			if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
				ap_roll_mode = 2
			end
		else
			if B738DR_autopilot_hdg_sel_status == 1 then
				ap_roll_mode = 1
			elseif B738DR_autopilot_lnav_status == 1 then
				ap_roll_mode = 4
			else
				ap_roll_mode = 0
			end
		end
	else
		if B738DR_autopilot_vorloc_status == 1 then
			if B738DR_autopilot_hdg_sel_status == 1 then
				ap_roll_mode = 1	-- HDG
			elseif B738DR_autopilot_lnav_status == 1 then
				ap_roll_mode = 4
			else
				ap_roll_mode = 0
			end
		end
	end
end

function ap_app_cmd()
	local ap_block_fdga = 0
	if B738DR_fd_goaround ~= 0 and B738DR_fd_on == 0 then
		ap_block_fdga = 1
	end
	
	local ils_gls_not_tuned = 0
	if B738DR_ap_side == 0 then
		if B738DR_mmr_act_mode == 0 then
			-- not ILS/GLS
			ils_gls_not_tuned = 1
		end
	else
		if B738DR_mmr_act_mode_fo == 0 then
			-- not ILS/GLS
			ils_gls_not_tuned = 1
		end
	end
	
	if B738DR_ian_not_install == 0 then
		-- IAN installed
		if ils_gls_not_tuned ~= 0 or B738DR_fms_ils_disable == 1 then
			local ian_enabled = 0
			local delta_crs_loc = 0
			if B738DR_ap_side == 0 then
				if B738DR_ian_info ~= 0 then
					delta_crs_loc = math.abs(math.floor(B738DR_course_pilot + 0.5) - (B738DR_fac_trk + simDR_mag_variation + 360) % 360)
					if delta_crs_loc <= 6 then
						ian_enabled = 1
					end
				end
			else
				if B738DR_ian_info_fo ~= 0 then
					delta_crs_loc = math.abs(math.floor(B738DR_course_copilot + 0.5) - (B738DR_fac_trk + simDR_mag_variation + 360) % 360)
					if delta_crs_loc <= 6 then
						ian_enabled = 1
					end
				end
			end
			if ian_enabled == 0 then
				ap_block_fdga = 1
			end
		end
	else
		-- IAN not installed
		if ils_gls_not_tuned ~= 0 then
			ap_block_fdga = 1
		end
	end
	
	
	if ap_app_block == 0 and ap_block_fdga == 0 then
		if B738DR_autopilot_app_status == 0 then
			if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
				ap_roll_mode = 3
			end
		else
			if B738DR_autopilot_hdg_sel_status == 1 then
				ap_roll_mode = 1
			elseif B738DR_autopilot_lnav_status == 1 then
				ap_roll_mode = 4
			else
				ap_roll_mode = 0	-- HDG
			end
		end
	else
		if B738DR_autopilot_app_status == 1 then
			if B738DR_autopilot_hdg_sel_status == 1 then
				ap_roll_mode = 1	-- HDG
			elseif B738DR_autopilot_lnav_status == 1 then
				ap_roll_mode = 4
			else
				ap_roll_mode = 0
			end
		end
	end
end

function ap_lnav_cmd()
	if B738DR_end_route == 0 and ap_app_block == 0 then
		if ap_lnav_status == 0 then
			if B738DR_autopilot_lnav_status == 0 then
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1 and B738DR_lnav_prior_arm == 1 then
					-- and ap_pitch_mode ~= 8 and ap_pitch_mode ~= 11 and B738DR_at_takeoff == 0 then
						if B738DR_autopilot_hdg_sel_status == 1 then
							ap_roll_mode = 10
							ap_roll_mode_eng = 10
							B738DR_lnav_engaged = 0
						elseif ap_roll_mode == 15 then
							B738DR_lnav_engaged = 0
							ap_roll_mode = 18
						else
							B738DR_lnav_engaged = 0
							ap_roll_mode = 4
						end
					end
				elseif B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
					
					local lnav_enabled = 0
					if B738DR_fpln_nav_id == "VECTOR" or B738DR_fpln_nav_id == "DISCON" then
						lnav_enabled = 0
					else
						if B738DR_fms_legs_offset_engage == 1 and B738DR_xtrack - B738DR_fms_legs_offset_dist > -3 and B738DR_xtrack - B738DR_fms_legs_offset_dist < 3 then
							lnav_enabled = 1
						elseif B738DR_xtrack > -3 and B738DR_xtrack < 3 then
							lnav_enabled = 1
						-- else
							-- lnav_enabled = 2
						end
						
						-- if lnav_enabled == 2 then
							
							-- local intercept_hdg = 0
							-- -- 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
							-- -- elseif B738DR_legs_intdir_act == 2 then
								-- -- if B738DR_legs_intdir_dist < 3 then
									-- -- intercept_hdg = 1
								-- -- end
							-- -- end
							
							-- intercept_hdg = 1
							
							-- if intercept_hdg == 0 then
								-- lnav_enabled = 0
							-- end
						-- end
					end
					
					if lnav_enabled == 0 then
						B738CMD_msg_not_intcpt:once()
					else
						if ap_roll_mode == 15 then
							B738DR_lnav_engaged = 0
							ap_roll_mode = 18
						else
							B738DR_lnav_engaged = 0
							ap_roll_mode = 4
						end
					end
				end
			else
				if (ap_pitch_mode == 8 or ap_pitch_mode == 11) and simDR_radio_height_pilot_ft < 50 then
					ap_roll_mode = 1	-- HDG
				elseif ap_roll_mode == 18 and simDR_radio_height_pilot_ft < 50 then
					ap_roll_mode = 15
					to_ga_hdg = simDR_ahars_mag_hdg
				elseif B738DR_autopilot_hdg_sel_status == 1 then
					ap_roll_mode = 1
					B738DR_lnav_engaged = 0
				else
					ap_roll_mode = 0
					B738DR_lnav_engaged = 0
				end
			end
		end
	else
		if B738DR_autopilot_lnav_status == 1 then
			if B738DR_autopilot_hdg_sel_status == 1 then
				ap_roll_mode = 1	-- HDG
			else
				ap_roll_mode = 0
			end
		end
	end
end

-- PITCH MODES
function ap_vs_cmd()
	local ap_block_fdga = 0
	if B738DR_fd_goaround ~= 0 and B738DR_fd_on == 0 then
		ap_block_fdga = 1
	end
	
	if ap_app_block == 0 and ap_block_fdga == 0 then
		if B738DR_autopilot_vs_status == 0 then
			if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
				if ap_pitch_mode == 8 or ap_pitch_mode == 11 then
					B738DR_mcp_speed_dial = simDR_airspeed_dial
					update_mcp_speed_dial()
				end
				-- if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then	-- ALT HLD
				if ap_pitch_mode == 6 or ap_pitch_mode == 3 
				or (ap_pitch_mode == 5 and (simDR_autopilot_altitude_mode == 3 or simDR_autopilot_altitude_mode == 6)) then	-- ALT HLD
					-- if math.abs(simDR_ap_alt_hold_ft - B738DR_mcp_alt_dial) >= 100 then
					if math.abs(B738DR_preselected_alt - B738DR_mcp_alt_dial) >= 100 then
						vnav_stop()
						simDR_ap_vvi_dial = 0
						B738DR_ap_vvi_dial = 0
						ap_pitch_mode = 1
					elseif ap_pitch_mode == 8 then
						ap_pitch_mode = 0
					elseif ap_pitch_mode == 11 then
						ap_pitch_mode = 5
					else
						vnav_stop()
						simDR_ap_vvi_dial = 0
						B738DR_ap_vvi_dial = 0
						ap_pitch_mode = 3
						ap_pitch_mode_eng = 3
						if B738DR_autopilot_autothr_arm_pos ~= 0 then
							at_mode = 2
						end
					end
				elseif math.abs(B738DR_AP_altitude - B738DR_mcp_alt_dial) >= 100 then
					vnav_stop()
					ap_pitch_mode = 1
					simDR_ap_vvi_dial = roundDownToIncrement(B738DR_AP_vvi, 100)
					B738DR_ap_vvi_dial = simDR_ap_vvi_dial
					
				else
					vnav_stop()
					simDR_ap_vvi_dial = 0
					B738DR_ap_vvi_dial = 0
					ap_pitch_mode = 3
					ap_pitch_mode_eng = 3
					if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
						B738DR_preselected_alt = simDR_ap_altitude_dial_ft
						simCMD_autopilot_alt_hold:once()
					end
					if B738DR_autopilot_autothr_arm_pos ~= 0 then
						at_mode = 2
					end
				end
				
				if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
					if ap_pitch_mode == 1 then
						if is_timer_scheduled(vs_to_lvlchg) == true then
							stop_timer(vs_to_lvlchg)
						end
						run_after_time(vs_to_lvlchg, 0.5)
					end
				end
			end
		else
			ap_pitch_mode = 0
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
		end
	else
		if B738DR_autopilot_vs_status == 1 then
			simDR_ap_vvi_dial = 0
			ap_pitch_mode = 0
			B738DR_ap_vvi_dial = 0
		end
	end
end

function ap_lvl_chg_cmd()

	local ap_block_fdga = 0
	if B738DR_fd_goaround ~= 0 and B738DR_fd_on == 0 then
		ap_block_fdga = 1
	end
	
	if ap_app_block == 0 and ap_block_fdga == 0 then
		if B738DR_autopilot_lvl_chg_status == 0 then
			if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
				if simDR_autopilot_altitude_mode == 6 or simDR_autopilot_altitude_mode == 3 then
					-- if math.abs(simDR_ap_alt_hold_ft - B738DR_mcp_alt_dial) >= 100 then
					if math.abs(B738DR_preselected_alt - B738DR_mcp_alt_dial) >= 100 then
						ap_pitch_mode = 2
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							simDR_airspeed_dial_kts = 100
							-- new mcp
							B738DR_mcp_speed_dial = simDR_airspeed_dial
							update_mcp_speed_dial()
						elseif at_mode == 0 then
							simDR_airspeed_dial_kts = B738DR_AP_airspeed
							-- new mcp
							B738DR_mcp_speed_dial = simDR_airspeed_dial
							update_mcp_speed_dial()
						end
					else
						vnav_stop()
						simDR_ap_vvi_dial = 0
						B738DR_ap_vvi_dial = 0
						ap_pitch_mode = 3
						ap_pitch_mode_eng = 3
						if B738DR_autopilot_autothr_arm_pos ~= 0 then
							at_mode = 2
						end
					end
				else
					if math.abs(B738DR_AP_altitude - B738DR_mcp_alt_dial) >= 100 then
						ap_pitch_mode = 2
						if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
							simDR_airspeed_dial_kts = 100
							-- new mcp
							B738DR_mcp_speed_dial = simDR_airspeed_dial
							update_mcp_speed_dial()
						elseif at_mode == 0 then
							simDR_airspeed_dial_kts = B738DR_AP_airspeed
							-- new mcp
							B738DR_mcp_speed_dial = simDR_airspeed_dial
							update_mcp_speed_dial()
						end
					else
						vnav_stop()
						simDR_ap_vvi_dial = 0
						B738DR_ap_vvi_dial = 0
						ap_pitch_mode = 3
						ap_pitch_mode_eng = 3
						if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
							B738DR_preselected_alt = simDR_ap_altitude_dial_ft
							simCMD_autopilot_alt_hold:once()
						end
						if B738DR_autopilot_autothr_arm_pos ~= 0 then
							at_mode = 2
						end
					end
				end
			end
		else
			if simDR_on_ground[0] == 0 and simDR_on_ground[1] == 0 and simDR_on_ground[2] == 0 then
				ap_pitch_mode = 0
				at_mode = 0
				simDR_ap_vvi_dial = 0
				B738DR_ap_vvi_dial = 0
			end
		end
	else
		if B738DR_autopilot_lvl_chg_status == 1 then
			ap_pitch_mode = 0
			at_mode = 0
			simDR_ap_vvi_dial = 0
			B738DR_ap_vvi_dial = 0
		end
	end
end

function ap_alt_cmd()
	local ap_block_fdga = 0
	if B738DR_fd_goaround ~= 0 and B738DR_fd_on == 0 then
		ap_block_fdga = 1
	end
	
	if ap_app_block == 0 and ap_block_fdga == 0 then
		if ap_pitch_mode ~= 3 then
			if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
				ap_pitch_mode = 3
				alt_hold_captured = 1
			end
		else
			if alt_hold_captured ~= 0 then
				ap_pitch_mode = 0
				alt_hold_captured = 0
			end
		end
	else
		if ap_pitch_mode == 3 and alt_hold_captured ~= 0 then
			ap_pitch_mode = 0
			alt_hold_captured = 0
		end
	end
end

function ap_vnav_cmd()
	local ap_block_fdga = 0
	if B738DR_fd_goaround ~= 0 and B738DR_fd_on == 0 then
		ap_block_fdga = 1
	end
	
	local vnav_enable2 = 0
	if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
		if B738DR_autopilot_fd_pos == 1 and B738DR_autopilot_fd_fo_pos == 1
		and B738DR_end_route == 0 and B738DR_no_perf == 0
		and ap_app_block == 0 and ap_block_fdga == 0 then
			-- if ap_pitch_mode ~= 8 and B738DR_at_takeoff == 0 then
				vnav_enable2 = 1
			-- end
		end
	else
		if B738DR_end_route == 0 and B738DR_no_perf == 0
		and ap_app_block == 0 and ap_block_fdga == 0 then 
			vnav_enable2 = 1
		end
	end
	
	if vnav_enable2 == 1 then
		if ap_vnav_status == 0 then
			if B738DR_autopilot_vnav_status == 0 then
				if B738DR_fd_on == 1 or ap_on == 1 or cws_on == 1 then
					-- VNAV ALT
					if simDR_on_ground[0] == 1 or simDR_on_ground[1] == 1 or simDR_on_ground[2] == 1 then
						if ap_pitch_mode == 8 then
							ap_pitch_mode = 11
						else
							B738DR_vnav_alt_mode = 0
							ap_pitch_mode = 5
							simDR_ap_vvi_dial = 0
							B738DR_ap_vvi_dial = 0
						end
					else
						if ap_pitch_mode == 3 then		-- ALT HLD
							if B738DR_flight_phase ~= 2 then
								B738DR_vnav_alt_mode = 1
								vnav_alt_hld = 1
							else
								B738DR_vnav_alt_mode = 0
							end
							ap_pitch_mode = 5
							simDR_ap_vvi_dial = 0
						else
							B738DR_vnav_alt_mode = 0
							ap_pitch_mode = 5
							simDR_ap_vvi_dial = 0
						end
						B738DR_ap_vvi_dial = simDR_ap_vvi_dial
					end
				end
			else
				if ap_pitch_mode == 11 then
					vnav_engaged = 0
					ap_pitch_mode = 8
					ap_pitch_mode_eng = 8
				else
					ap_pitch_mode = 0
					vnav_engaged = 0
					B738DR_mcp_speed_dial = simDR_airspeed_dial
					update_mcp_speed_dial()
					at_mode = 0
				end
			end
		end
	else
		if B738DR_autopilot_vnav_status == 1 then
			if ap_pitch_mode == 11 then
				ap_pitch_mode = 8
			else
				ap_pitch_mode = 0
				vnav_engaged = 0
			end
		end
	end
end

-- THROTLE MODE
function ap_n1_cmd()
	if B738DR_autopilot_autothr_arm_pos == 1 
	and (simDR_eng_N1[0] > 18 or simDR_eng_N1[1] > 18) then
		if B738DR_autopilot_n1_status == 0 then
			if ap_pitch_mode == 2 and B738DR_lvl_chg_mode == 1 then
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					at_mode = 6
				end
			elseif ap_pitch_mode == 5 and (B738DR_flight_phase < 2 or B738DR_flight_phase == 3 or B738DR_flight_phase == 8) and vnav_engaged ~= 0 then
				if simDR_autopilot_altitude_mode ~= 6 and simDR_autopilot_altitude_mode ~= 3 then
					at_mode = 7
					B738DR_autopilot_n1_status = 1
					vnav_n1_dis_eng = 0
				end
			elseif ap_pitch_mode == 0 or (ap_pitch_mode >= 8 and ap_pitch_mode <= 12) then
				at_mode = 1
			end
		else
			if ap_pitch_mode == 5 and vnav_engaged ~= 0 then
				vnav_n1_dis_eng = 1
			end
			at_mode = 0
		end
	end
end

function ap_speed_cmd()
	local disable_speed = 0
	if ap_pitch_mode >= 8 and ap_pitch_mode <= 12 then
		disable_speed = 1
	end
	if B738DR_autopilot_autothr_arm_pos == 1 and disable_speed == 0 
	and (simDR_eng_N1[0] > 18 or simDR_eng_N1[1] > 18) then
		if B738DR_autopilot_speed_status == 0 then
			if ap_pitch_mode == 3 or ap_pitch_mode == 6 or ap_pitch_mode == 1 or ap_pitch_mode == 4 or ap_pitch_mode == 7 then
				at_mode = 2
			elseif ap_pitch_mode == 5 then
				if B738DR_flight_phase == 2 or (B738DR_flight_phase > 3 and B738DR_flight_phase < 8) then
					if simDR_autopilot_altitude_mode ~= 5 then
						at_mode = 6
					end
				end
			elseif ap_pitch_mode == 0 then
				at_mode = 2
			end
		else
			at_mode = 0
		end
	end
end

function ap_button_req()
	
	local ap_roll_mem = ap_roll_mode
	local ap_pitch_mem = ap_pitch_mode
	local ap_speed_mem = at_mode
	
	if ap_heading_buf == 0 then
		ap_heading_timer = 0
		ap_heading_used = false
	else
		if ap_heading_used == true then
			ap_heading_used = false
			ap_heading_timer = 0.33
		else
			if ap_heading_timer > 0 then
				ap_heading_timer = ap_heading_timer - SIM_PERIOD
				if ap_heading_timer <= 0 then
					ap_heading_timer = 0
					if ap_heading_buf == 1 then
						-- HDG
						ap_hdg_cmd()
					elseif ap_heading_buf == 2 then
						-- VOR LOC
						ap_vor_loc_cmd()
					elseif ap_heading_buf == 3 then
						-- APP
						ap_app_cmd()
					elseif ap_heading_buf == 4 then
						-- LNAV
						ap_lnav_cmd()
					end
					if ap_roll_mem ~= ap_roll_mode and ap_roll_mode ~= 0 then
						fma_roll_timer = 0.22
						if ap_heading_buf == 3 then
							fma_pitch_timer = 0.22
						end
						if ap_heading_buf >= 3 then
							fma_speed_timer = 0.22
						end
					end
					ap_heading_buf = 0
				end
			end
		end
	end
	
	if ap_altitude_buf == 0 then
		ap_altitude_timer = 0
		ap_altitude_used = false
	else
		if ap_altitude_used == true then
			ap_altitude_used = false
			ap_altitude_timer = 0.33
		else
			if ap_altitude_timer > 0 then
				ap_altitude_timer = ap_altitude_timer - SIM_PERIOD
				if ap_altitude_timer <= 0 then
					ap_altitude_timer = 0
					if ap_altitude_buf == 1 then
						-- VS
						ap_vs_cmd()
					elseif ap_altitude_buf == 2 then
						-- LVL CHG
						ap_lvl_chg_cmd()
					elseif ap_altitude_buf == 3 then
						-- ALT HLD
						ap_alt_cmd()
					elseif ap_altitude_buf == 4 then
						-- VNAV
						ap_vnav_cmd()
					end
					if ap_pitch_mem ~= ap_pitch_mode and ap_pitch_mode ~= 0 then
						fma_pitch_timer = 0.22
						fma_speed_timer = 0.22
					end
					ap_altitude_buf = 0
				end
			end
		end
	end
	
	if ap_speed_buf == 0 then
		ap_speed_timer = 0
		ap_speed_used = false
	else
		if ap_speed_used == true then
			ap_speed_used = false
			ap_speed_timer = 0.33
		else
			if ap_speed_timer > 0 then
				ap_speed_timer = ap_speed_timer - SIM_PERIOD
				if ap_speed_timer <= 0 then
					ap_speed_timer = 0
					if ap_speed_buf == 1 then
						-- N1
						ap_n1_cmd()
					elseif ap_speed_buf == 2 then
						-- SPEED
						ap_speed_cmd()
					end
					if ap_speed_mem ~= at_mode and at_mode ~= 0 then
						fma_speed_timer = 0.22
					end
					ap_speed_buf = 0
				end
			end
		end
	end
end

function after_physics()
	
	if B738DR_kill_glareshield == 0 then
		
		if load_plane2 == 0 then
			load_plane2 = 1
			simDR_bank_angle = 5
			if B738DR_state_cmd2 == 0 then
				if string.len(B738DR_state_cmd) ~= 0 then
					B738_set_state()
					
					radio_dh_pilot = -1
					radio_dh_copilot = -1
					
					baro_dh_pilot = -1001
					baro_dh_copilot = -1001
					
					radio_dh_pilot_mem = 100	-- -1		--100
					baro_dh_pilot_mem = 200		-- -1001	--200
					
					radio_dh_copilot_mem = 100		-- -1	--100
					baro_dh_copilot_mem = 200		-- -1001	--200
					
					if B738DR_minim_capt == 0 then	--radio DH
						B738DR_dh_pilot = -1
					else
						B738DR_dh_pilot = -1001
					end
					B738DR_minim_capt_show = 0
					
					if B738DR_minim_fo == 0 then	--radio DH
						B738DR_dh_copilot = -1
					else
						B738DR_dh_copilot = -1001
					end
					B738DR_minim_fo_show = 0
				end
			elseif B738DR_state_cmd2 == 5 then
				B738_create_state()
				B738DR_state_cmd2 = 6
			end
			if simDR_int_ver >= 120000 then
				simDR_map_range_steps[0] = 5
				simDR_map_range_steps[1] = 10
				simDR_map_range_steps[2] = 20
				simDR_map_range_steps[3] = 40
				simDR_map_range_steps[4] = 80
				simDR_map_range_steps[5] = 160
				simDR_map_range_steps[6] = 320
				simDR_map_range_steps[7] = 640
			end
		elseif load_plane2 == 1 then
			load_plane2 = 2
			simDR_throttle_override = 1
			simDR_nav1_obs_pilot = math.floor(simDR_nav1_obs_pilot + 0.5)
			simDR_nav2_obs_pilot = simDR_nav1_obs_pilot
			B738DR_course_pilot = simDR_nav1_obs_pilot
			simDR_nav1_obs_copilot = math.floor(simDR_nav1_obs_copilot + 0.5)
			simDR_nav2_obs_copilot = simDR_nav1_obs_copilot
			B738DR_course_copilot = simDR_nav1_obs_copilot
			if (simDR_ap_capt_heading > 360) or (simDR_ap_capt_heading < 0) then
				B738DR_mcp_hdg_dial = 0
			else
				B738DR_mcp_hdg_dial = math.floor(simDR_ap_capt_heading + 0.5)
				if B738DR_mcp_hdg_dial >= 359.5 then
					B738DR_mcp_hdg_dial = 0
				end
			end
			mcp_hdg_dial_old = B738DR_mcp_hdg_dial
			-- simDR_bank_angle = 5
			
		else
			
			if B738DR_load_save == 4 then
				glare_load = 0
				loaded_flight_timer = 5
			end
			
			if was_replay == 1 then
				B738DR_thrust1_leveler = thrust1_leveler_mem
				B738DR_thrust2_leveler = thrust2_leveler_mem
				B738DR_yoke_pitch = last_yoke_pitch
				B738DR_yoke_roll = last_yoke_roll
				B738DR_flare_status = flare_status_mem
				B738DR_autopilot_autothr_arm_pos = autothr_arm_pos_mem
				autopilot_autothrottle_status = autothrottle_status_mem
			end
			if B738DR_state_cmd2 == 0 then
				if string.len(B738DR_state_cmd) ~= 0 then
					B738_set_state()
					
					radio_dh_pilot = -1
					radio_dh_copilot = -1
					
					baro_dh_pilot = -1001
					baro_dh_copilot = -1001
					
					radio_dh_pilot_mem = 100		-- -1		--100
					baro_dh_pilot_mem = 200			-- -1001	--200
					
					radio_dh_copilot_mem = 100		-- -1	--100
					baro_dh_copilot_mem = 200		-- -1001	--200
					
					if B738DR_minim_capt == 0 then	--radio DH
						B738DR_dh_pilot = -1
					else
						B738DR_dh_pilot = -1001
					end
					B738DR_minim_capt_show = 0
					
					if B738DR_minim_fo == 0 then	--radio DH
						B738DR_dh_copilot = -1
					else
						B738DR_dh_copilot = -1001
					end
					B738DR_minim_fo_show = 0
				end
			elseif B738DR_state_cmd2 == 5 then
				B738_create_state()
				B738DR_state_cmd2 = 6
			end
			
			load_save(0)
			
			check_yoke()
			speed_ratio_timer2()
			
			ap_button_req()
			
			elevation_ft = simDR_elevation_m * 3.28083989501 --ft
			
			if B738DR_activate_pitch_mode >= 0 then
				if ap_pitch_mode == 5 and vnav_engaged ~= 0 then
					ap_pitch_mode = B738DR_activate_pitch_mode
				end
				B738DR_activate_pitch_mode = -1
				ap_altitude_buf = 0
				ap_altitude_timer = 0
				ap_altitude_used = false
			end
			
			if B738DR_autoland_install ~= autoland_install then
				if B738DR_autoland_install == 0 and B738DR_autoland_fail_status == 0 then
					if B738DR_rollout_status == 0 then
						if B738DR_flare_status > 0 then
							B738DR_rollout_status = 1
							ap_roll_mode = 9
						end
					end
				else
					B738DR_rollout_status = 0
					if B738DR_flare_status > 0 then
						ap_roll_mode = 3
						ap_roll_mode_eng = 3
					end
				end
			end
			autoland_install = B738DR_autoland_install
			
			if B738DR_ignore_td_idx2 ~= 0 then
				ignore_td_idx2 = 1
				if is_timer_scheduled(ignore_td_idx2_timer) == false then
					run_after_time(ignore_td_idx2_timer, 5)
				else
					stop_timer(ignore_td_idx2_timer)
					run_after_time(ignore_td_idx2_timer, 5)
				end
			end
			B738DR_ignore_td_idx2 = 0
			B738_vor_loc_crs()
			if ap_pitch_mode_eng == 1 or ap_pitch_mode_eng == 2 then	-- VS or LVL CHG
				if B738DR_mcp_alt_dial ~= mcp_alt_dial_old then
					if is_timer_scheduled(unlock_mcp_alt) == true then
						stop_timer(unlock_mcp_alt)
					end
					run_after_time(unlock_mcp_alt, 1.3)
					mcp_alt_dial_lock = 1
				end
				if mcp_alt_dial_lock == 0 then
					mcp_alt_dial_app = B738DR_mcp_alt_dial
				end
			elseif B738DR_flight_phase == 6 or B738DR_flight_phase == 7 then
				-- FMC Approach mode
				if B738DR_missed_app_alt ~= 0 and B738DR_AP_altitude < B738DR_missed_app_alt - 300 then
					if B738DR_mcp_alt_dial ~= mcp_alt_dial_old then
						if is_timer_scheduled(unlock_mcp_alt) == true then
							stop_timer(unlock_mcp_alt)
						end
						run_after_time(unlock_mcp_alt, 1.3)
						mcp_alt_dial_lock = 1
					end
					if mcp_alt_dial_lock == 0 or B738DR_mcp_alt_dial < mcp_alt_dial_old then
						mcp_alt_dial_app = B738DR_mcp_alt_dial
					end
				else
					if is_timer_scheduled(unlock_mcp_alt) == true then
						stop_timer(unlock_mcp_alt)
					end
					mcp_alt_dial_lock = 0
					mcp_alt_dial_app = B738DR_mcp_alt_dial
				end
			else
				if is_timer_scheduled(unlock_mcp_alt) == true then
					stop_timer(unlock_mcp_alt)
				end
				mcp_alt_dial_lock = 0
				mcp_alt_dial_app = B738DR_mcp_alt_dial
			end
			mcp_alt_dial_old = B738DR_mcp_alt_dial
			
			B738_ctr_exp_mode()
			B738_nav1_nav2_source()
			B738_efis_baro()
			B738_ian_gs_changed()
			B738_ap_logic()
			B738_cmd_revert()
			B738_at_logic()
			
			B738_vnav6()
			B738_alt_acq()
			
			B738_ap_takeoff()
			B738_ap_goaround()
			B738_ap_autoland()
			B738_ap_system()
			B738_fmc_vorloc_lnav()
			B738_lnav3()
			B738_fac()
			B738_loc_gp()
			B738_gp()
			
			B738_lvl_chg()
			
			B738_vs()
			B738_alt_hld()
			B738_app()
			B738_gls_ap()
			B738_hdg()
			B738_vorloc()
			
			autopilot_system_lights2()
			B738_PFD_flash2()
			if fma_roll_timer > 0 then
				fma_roll_timer = fma_roll_timer - SIM_PERIOD
				if fma_roll_timer < 0 then
					fma_roll_timer = 0
				end
			end
			if fma_pitch_timer > 0 then
				fma_pitch_timer = fma_pitch_timer - SIM_PERIOD
				if fma_pitch_timer < 0 then
					fma_pitch_timer = 0
				end
			end
			if fma_speed_timer > 0 then
				fma_speed_timer = fma_speed_timer - SIM_PERIOD
				if fma_speed_timer < 0 then
					fma_speed_timer = 0
				end
			end
			
			B738_bellow_400ft()
			B738_ap_at_disconnect()
			B738_n1()
			B738_lift_off()
			B738_on_ground()
			B738_goaround_block()
			fd_own()
			
			B738DR_vnav_engaged = vnav_engaged
			B738_fd_show()
			B738_ias_alt_disagree()
			B738_afs_protect3()
			
			B738_draw_arc()
			B738_pfd_runway()
			B738_radio_height()
			B738_baro_radio2()
			ils_radio_pfd()
			
			B738_efis()
			B738_flight_director()
			B738_pfd_tapes()
			B738_hyd_sys()
			B738_bank_angle()
			at_on_old = B738DR_autopilot_autothr_arm_pos
			if B738DR_xhsi_enable == 0 then
				simDR_efis_sub_mode = 2
				if simDR_int_ver < 120000 then
					simDR_efis_map_mode = 0
				else
					simDR_efis_sub_mode_copilot = 2
				end
			end
			
			if simDR_int_ver >= 120000 then
				simDR_map_range_steps[0] = 5
				simDR_map_range_steps[1] = 10
				simDR_map_range_steps[2] = 20
				simDR_map_range_steps[3] = 40
				simDR_map_range_steps[4] = 80
				simDR_map_range_steps[5] = 160
				simDR_map_range_steps[6] = 320
				simDR_map_range_steps[7] = 640
			end
			
			B738_nd()
			B738_spd_ratio()
			B738_eng_out()
			B738_ctrl_panel()
			B738_ap_servo()
			B738_cws_system()
			
			B738_yoke()
			B738_vib_flaps()
			altitude_mode_old = simDR_autopilot_altitude_mode
			flight_phase_old = B738DR_flight_phase
			ap_pitch_mode_old2 = ap_pitch_mode
			
			if B738DR_autopilot_vnav_status == 0 and B738DR_autoland_status == 0 then
				B738DR_ap_vvi_pos = simDR_ap_vvi_dial
			end
			
			if simDR_airspeed_is_mach == 0 then
				if simDR_airspeed_dial > 340 then
					simDR_airspeed_dial = 340
					B738DR_mcp_speed_dial = simDR_airspeed_dial
					update_mcp_speed_dial()
				end
			else
				if simDR_airspeed_dial > 0.82 then
					simDR_airspeed_dial = 0.82
					B738DR_mcp_speed_dial = simDR_airspeed_dial
					update_mcp_speed_dial()
				end
			end
			B738DR_ap_on = ap_on
			if ap_on == 0 then
				B738DR_ap_side = 0
			elseif ap1_on == 1 and ap2_on == 1 then
				B738DR_ap_side = 1
			elseif ap1_on == 1 then
				B738DR_ap_side = 0
			else
				B738DR_ap_side = 2
			end
			
			if autopilot_master_capt_status ~= 0 and autopilot_master_fo_status ~= 0 then
				B738DR_fd_side = 1
			elseif autopilot_master_fo_status ~= 0 then
				B738DR_fd_side = 2
			else
				B738DR_fd_side = 0
			end
			
			blink_ap_at()
			B738_at_warning()
			simDR_mem[14] = B738DR_thrust1_leveler
			simDR_mem[15] = B738DR_thrust2_leveler
			thrust1_leveler_mem = B738DR_thrust1_leveler
			thrust2_leveler_mem = B738DR_thrust2_leveler
			B738_code_mem2()
			was_replay = 0
			
			if B738DR_dc_bus2_status == 0 or B738DR_fd_cam_disable ~= 0 or B738DR_ife_pass_seat_pos == 0 then
				B738DR_fd_cam = 0
			elseif B738DR_fd_cam ~= 0 then
				if B738DR_fd_cam_pos == -1 then
					-- left
					if door_ratio_fwd_L == 0 then
						B738DR_fd_cam = 1
					elseif B738DR_hide_airstairs == 0 then
						B738DR_fd_cam = 7
					else
						B738DR_fd_cam = 4
					end
				elseif B738DR_fd_cam_pos == 0 then
					-- center
					if B738DR_flt_dk_door_ratio == 0 then
						B738DR_fd_cam = 2
					else
						B738DR_fd_cam = 5
					end
				else
					-- right
					if door_ratio_fwd_R == 0 then
						B738DR_fd_cam = 3
					else
						B738DR_fd_cam = 6
					end
				end
			end
			
			B738DR_outflow_valve_dig = (math.floor((B738DR_outflow_valve * 150 / 7.5) + 0.5) * 7.5) - 75
			
			if B738DR_brunner_enable ~= 0 and brunner_active ~= 0 then
				if ap_on == 0 then
					BruDR_ext_ctrl = 1	-- AP off
				else
					BruDR_ext_ctrl = 4	-- AP pitch/roll
					--BruDR_ext_ctrl = 5	-- AP pitch/roll/yaw
				end
			end
			B738_mcp_dial()
			
			
			-- if B738DR_load_save == 0 then
			if glare_load ~= 0 then
				if B738DR_ac_stdbus_status == 0 then
					roll_elec_deg_stdby = B738_set_animation_rate(B738DR_roll_elec_deg_stdby, -45, -180, 180, 0.04)
					pitch_elec_deg_stdby = B738_set_animation_rate(B738DR_pitch_elec_deg_stdby, 65, -180, 180, 0.04)
					timer_gyro = 0
					B738DR_roll_elec_deg_stdby = roll_elec_deg_stdby
					B738DR_pitch_elec_deg_stdby = pitch_elec_deg_stdby
					ac_stdbus_timer = 0
				else
					if ac_stdbus_timer < 30 then
						ac_stdbus_timer = ac_stdbus_timer + SIM_PERIOD
						B738DR_roll_elec_deg_stdby = roll_elec_deg_stdby
						B738DR_pitch_elec_deg_stdby = pitch_elec_deg_stdby
					else
						if B738DR_allign_analog_horiz_pos ~= 0 then
							if timer_gyro == 0 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, 0, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, 90, -180, 180, 0.03)
								if roll_elec_deg_stdby == 0 and pitch_elec_deg_stdby == 90 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 1 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, 30, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, -37.5, -180, 180, 0.03)
								if roll_elec_deg_stdby == 30 and pitch_elec_deg_stdby == -37.5 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 2 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, 30, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, 0, -180, 180, 0.03)
								if roll_elec_deg_stdby == 30 and pitch_elec_deg_stdby == 0 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 3 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, -5, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, -15, -180, 180, 0.03)
								if roll_elec_deg_stdby == -5 and pitch_elec_deg_stdby == -15 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 4 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, 3, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, 10, -180, 180, 0.03)
								if roll_elec_deg_stdby == 3 and pitch_elec_deg_stdby == 10 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 5 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, -1.5, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, -5, -180, 180, 0.03)
								if roll_elec_deg_stdby == -1.5 and pitch_elec_deg_stdby == -5 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 6 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, 0, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, 0, -180, 180, 0.03)
								if roll_elec_deg_stdby == 0 and pitch_elec_deg_stdby == 0 then
									timer_gyro = timer_gyro + 1
								end
							elseif timer_gyro == 7 then
								roll_elec_deg_stdby = B738_set_animation_rate(roll_elec_deg_stdby, -simDR_roll_elec_deg_pilot, -180, 180, 0.03)
								pitch_elec_deg_stdby = B738_set_animation_rate(pitch_elec_deg_stdby, -simDR_pitch_elec, -180, 180, 0.03)
								
								if roll_elec_deg_stdby == -simDR_roll_elec_deg_pilot and pitch_elec_deg_stdby == -simDR_pitch_elec then
									timer_gyro = timer_gyro + 1
								end
							end
							
							if timer_gyro <= 7 then
								B738DR_roll_elec_deg_stdby = roll_elec_deg_stdby
								B738DR_pitch_elec_deg_stdby = pitch_elec_deg_stdby
							else
								B738DR_roll_elec_deg_stdby = B738_set_animation_rate(B738DR_roll_elec_deg_stdby, simDR_roll_elec_deg_pilot + roll_elec_deg_stdby, -180, 180, 0.01)
								B738DR_pitch_elec_deg_stdby = B738_set_animation_rate(B738DR_pitch_elec_deg_stdby, simDR_pitch_elec + pitch_elec_deg_stdby, -180, 180, 0.01)
							end
						else
							B738DR_roll_elec_deg_stdby = B738_set_animation_rate(B738DR_roll_elec_deg_stdby, simDR_roll_elec_deg_pilot + roll_elec_deg_stdby, -180, 180, 0.01)
							B738DR_pitch_elec_deg_stdby = B738_set_animation_rate(B738DR_pitch_elec_deg_stdby, simDR_pitch_elec + pitch_elec_deg_stdby, -180, 180, 0.01)
						end
					end
					
					if B738DR_roll_elec_deg_stdby < -180 then
						B738DR_roll_elec_deg_stdby = -180
					elseif B738DR_roll_elec_deg_stdby > 180 then
						B738DR_roll_elec_deg_stdby = 180
					end
					
					if B738DR_pitch_elec_deg_stdby < -180 then
						B738DR_pitch_elec_deg_stdby = -180
					elseif B738DR_pitch_elec_deg_stdby > 180 then
						B738DR_pitch_elec_deg_stdby = 180
					end
				end
			end
		end
		
		if B738DR_spd_intv_but == 0 and B738DR_ap_spd_interv_status ~= 0 then
			B738CMD_autopilot_spd_interv:once()
		end
		
		B738DR_nose_down = simDR_on_ground[0]
		
		local ap_test = autopilot_cmd_a_status + autopilot_cmd_b_status + autopilot_cws_a_status + autopilot_cws_b_status
		if ap_test == 0 then
			B738DR_autopilot_cfy = 0
		else
			B738DR_autopilot_cfy = 1
		end
		if B738DR_autopilot_n1_status_cfy == 0 and B738DR_autopilot_at_status_cfy == 0 then
			B738DR_autothrottle_cfy = 0
		else
			B738DR_autothrottle_cfy = 1
		end
		if simDR_flaps_ratio < 0.05 then
			B738DR_trim_wheel_speed_cfy = 0
		else
			B738DR_trim_wheel_speed_cfy = 1
		end
		
		cmd_ap_timer()
		
		-- if B738DR_load_save ~= 3 then
			load_save(1)
		-- end
		if glare_load == 0 then
			glare_load = 1
		end
	end
	
end

 function after_replay()
	was_replay = 1
	B738_radio_height()
	B738_flight_director()
	B738_pfd_tapes()
	B738_yoke_replay()
	B738_mcp_dial()
	B738DR_thrust1_leveler = simDR_mem[14]
	B738DR_thrust2_leveler = simDR_mem[15]
	B738_decode_mem2()
	B738_vib_flaps()
	B738DR_pfd_runway_show = 0
	B738DR_pfd_runway_show_fo = 0
	-- speed kts/mach indicator
	if simDR_mach_no >= 0.4 then
		B738DR_spd_ind_mode = 1
	elseif simDR_mach_no <= 0.38 then
		B738DR_spd_ind_mode = 0
	end
end



--*************************************************************************************--
--** 				               SUB-MODULE PROCESSING       	        			 **--
--*************************************************************************************--

-- dofile("")
