--[[
*****************************************************************************************
* Program Script Name	:	B738.LATLON
*
* 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

--]]


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

-- IRS entry mode
IRS_entry_mode_lat = 1
IRS_entry_mode_lon = 2
IRS_entry_mode_hdg = 3

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

irs_dspl_test_enable = 0
irs_dspl_test_run = 0

alignment_left_enable = 0
alignment_right_enable = 0
alignment_left_status = 0
alignment_right_status = 0
irs_left_mode = 0
irs_right_mode = 0
irs_left_status = 0
irs_right_status = 0
irs_enable = 1
irs_enable2 = 1
irs_flashing = 0
irs_time = 0
alignment_right_entry = 0
alignment_left_entry = 0
irs_left_restart = 0
irs_right_restart = 0
irs_left_fail = 0
irs_right_fail = 0
actual_pos = "-----.-------.-"
irs_left_test = 0
irs_right_test = 0
irs_hdg_set = "---"
irs2_hdg_set = "---"

align_lat = 0
align_lon = 0

first_time = 0
irs_first_time = 0

irs_align_fail_left = 0
irs_align_fail_right = 0
irs_dc_fail_left = 0
irs_dc_fail_right = 0

irs_entry_mod = 0
irs_entry_pos_lat = ""
irs_entry_pos_lon = ""
irs_entry_hdg = ""
irs_entry_reset = 0

raw_table('disable_key')
disable_key = {}
for ii = 0, 9 do
	disable_key[ii] = 0
end

irs_ent_light = 0
irs_clr_light = 0

irs_align_left = 0
irs_align_right = 0

irs_dc_timer_off = 0

flight_loaded = 0

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

--*************************************************************************************--
--** 				             FIND X-PLANE DATAREFS            			    	 **--
--*************************************************************************************--

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

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

simDR_true_track		= find_dataref("sim/flightmodel/position/hpath")
simDR_ground_speed		= find_dataref("sim/flightmodel/position/groundspeed")

simDR_wind_hdg		= find_dataref("sim/cockpit2/gauges/indicators/wind_heading_deg_mag")
simDR_wind_spd		= find_dataref("sim/cockpit2/gauges/indicators/wind_speed_kts")

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

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_true_heading		= find_dataref("sim/cockpit/misc/compass_indicated")
simDR_vpath		= find_dataref("sim/flightmodel/position/vpath")

simDR_radio_height_pilot_ft		= find_dataref("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot")

simDR_overwr_gps			= find_dataref("sim/operation/override/override_gps")
DRsim_fms_override			= find_dataref("sim/operation/override/override_fms_advance")

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



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

B738DR_irs_pos_fmc			= find_dataref("laminar/B738/FMS/irs_pos_fmc")
B738DR_irs_hdg_fmc_set		= find_dataref("laminar/B738/FMS/irs_hdg_fmc_set")
B738DR_irs2_hdg_fmc_set		= find_dataref("laminar/B738/FMS/irs2_hdg_fmc_set")
B738DR_irs_pos_fmc_set		= find_dataref("laminar/B738/FMS/irs_pos_fmc_set")
B738DR_irs2_pos_fmc_set		= find_dataref("laminar/B738/FMS/irs2_pos_fmc_set")
B738DR_last_pos_str			= find_dataref("laminar/B738/FMS/last_pos_str")

B738DR_engine_no_running_state 		= find_dataref("laminar/B738/fms/engine_no_running_state")

B738DR_align_time			= find_dataref("laminar/B738/FMS/align_time")

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_batbus_status		= find_dataref("laminar/B738/electric/batbus_status")

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

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

B738DR_load_save			= find_dataref("laminar/B738/load_save")
B738DR_latlon				= find_dataref("laminar/B738/latlon")
B738DR_latlon_sw			= find_dataref("laminar/B738/latlon_sw")

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



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

B738DR_irs_ent_light		= create_dataref("laminar/B738/irs_ent_light", "number")
B738DR_irs_clr_light		= create_dataref("laminar/B738/irs_clr_light", "number")

B738DR_latitude_deg			= find_dataref("laminar/B738/latitude_deg")
B738DR_latitude_min			= find_dataref("laminar/B738/latitude_min")
B738DR_latitude_NS			= find_dataref("laminar/B738/latitude_NS")
B738DR_longitude_deg		= find_dataref("laminar/B738/longitude_deg")
B738DR_longitude_min		= find_dataref("laminar/B738/longitude_min")
B738DR_longitude_EW			= find_dataref("laminar/B738/longitude_EW")

B738DR_irs_left1			= create_dataref("laminar/B738/irs_left1", "number")
B738DR_irs_left2			= create_dataref("laminar/B738/irs_left2", "number")
B738DR_irs_right1			= create_dataref("laminar/B738/irs_right1", "number")
B738DR_irs_right2			= create_dataref("laminar/B738/irs_right2", "number")

B738DR_irs_ns_show			= create_dataref("laminar/B738/ns_show", "number")
B738DR_irs_ew_show			= create_dataref("laminar/B738/ew_show", "number")
B738DR_irs_lat_deg_show		= create_dataref("laminar/B738/lat_deg_show", "number")
B738DR_irs_lat_min_show		= create_dataref("laminar/B738/lat_min_show", "number")
B738DR_irs_lon_deg_show		= create_dataref("laminar/B738/lon_deg_show", "number")
B738DR_irs_lon_min_show		= create_dataref("laminar/B738/lon_min_show", "number")
B738DR_irs_decimals_show	= create_dataref("laminar/B738/decimals_show", "number")
B738DR_irs_left1_show		= create_dataref("laminar/B738/irs_left1_show", "number")
B738DR_irs_left2_show		= create_dataref("laminar/B738/irs_left2_show", "number")
B738DR_irs_right1_show		= create_dataref("laminar/B738/irs_right1_show", "number")
B738DR_irs_right2_show		= create_dataref("laminar/B738/irs_right2_show", "number")
B738DR_irs_allign_show		= create_dataref("laminar/B738/irs_allign_show", "number")

B738DR_irs_entry			= create_dataref("laminar/B738/irs_entry", "array[11]")
B738DR_irs_entry_len		= create_dataref("laminar/B738/irs_entry_len", "number")
B738DR_irs_entry_pos_show	= create_dataref("laminar/B738/irs_entry_pos_show", "number")
B738DR_irs_entry_hdg_show	= create_dataref("laminar/B738/irs_entry_hdg_show", "number")

B738DR_irs_align_fail_1		= create_dataref("laminar/B738/annunciator/irs/align_fail_left", "number")
B738DR_irs_align_fail_2		= create_dataref("laminar/B738/annunciator/irs/align_fail_right", "number")

-- ANNUNANCIATES
B738DR_irs_align_fail_right		= create_dataref("laminar/B738/annunciator/irs_align_fail_right", "number")
B738DR_irs_align_fail_left		= create_dataref("laminar/B738/annunciator/irs_align_fail_left", "number")
B738DR_irs_align_right			= create_dataref("laminar/B738/annunciator/irs_align_right", "number")
B738DR_irs_align_left			= create_dataref("laminar/B738/annunciator/irs_align_left", "number")

B738DR_irs_align_left2		= find_dataref("laminar/B738/annunciator/irs_align_left2")
B738DR_irs_align_right2		= find_dataref("laminar/B738/annunciator/irs_align_right2")

B738DR_irs_dc_fail_left			= create_dataref("laminar/B738/annunciator/irs_dc_fail_left", "number")
B738DR_irs_on_dc_left			= create_dataref("laminar/B738/annunciator/irs_on_dc_left", "number")
B738DR_irs_dc_fail_right		= create_dataref("laminar/B738/annunciator/irs_dc_fail_right", "number")
B738DR_irs_on_dc_right			= create_dataref("laminar/B738/annunciator/irs_on_dc_right", "number")

B738DR_irs_left_fail			= create_dataref("laminar/B738/annunciator/irs_left_fail", "number")
B738DR_irs_right_fail			= create_dataref("laminar/B738/annunciator/irs_right_fail", "number")

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

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

B738DR_adiru_left_power			= create_dataref("laminar/B738/adiru_left_power", "number")
B738DR_adiru_right_power		= create_dataref("laminar/B738/adiru_right_power", "number")

B738DR_irs_pfd_allign			= create_dataref("laminar/B738/irs/pfd_allign", "number")
B738DR_irs_pfd_allign_hdg		= create_dataref("laminar/B738/irs/pfd_allign_hdg", "number")
B738DR_irs_pfd_allign2			= create_dataref("laminar/B738/irs/pfd_allign2", "number")
B738DR_irs_pfd_allign_hdg2		= create_dataref("laminar/B738/irs/pfd_allign_hdg2", "number")
B738DR_irs_nd_allign			= find_dataref("laminar/B738/irs/nd_allign")
B738DR_irs_nd_allign_gs			= find_dataref("laminar/B738/irs/nd_allign_gs")

B738DR_irs_nd_allign2			= find_dataref("laminar/B738/irs/nd_allign2")
B738DR_irs_nd_allign_gs2		= find_dataref("laminar/B738/irs/nd_allign_gs2")

B738DR_irs_nd_allign_12			= find_dataref("laminar/B738/irs/nd_allign12")


B738DR_alignment_left_remain		= find_dataref("laminar/B738/irs/alignment_left_remain")
B738DR_alignment_right_remain		= find_dataref("laminar/B738/irs/alignment_right_remain")

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

B738DR_irs_running			= create_dataref("laminar/B738/irs/irs_running", "number")

--test						= create_dataref("laminar/B738/irs/test", "number")
--test2						= create_dataref("laminar/B738/irs/test2", "number")

B738DR_irs_key_0_pos		= create_dataref("laminar/B738/push_button/irs_key_0", "number")
B738DR_irs_key_1_pos		= create_dataref("laminar/B738/push_button/irs_key_1", "number")
B738DR_irs_key_2_pos		= create_dataref("laminar/B738/push_button/irs_key_2", "number")
B738DR_irs_key_3_pos		= create_dataref("laminar/B738/push_button/irs_key_3", "number")
B738DR_irs_key_4_pos		= create_dataref("laminar/B738/push_button/irs_key_4", "number")
B738DR_irs_key_5_pos		= create_dataref("laminar/B738/push_button/irs_key_5", "number")
B738DR_irs_key_6_pos		= create_dataref("laminar/B738/push_button/irs_key_6", "number")
B738DR_irs_key_7_pos		= create_dataref("laminar/B738/push_button/irs_key_7", "number")
B738DR_irs_key_8_pos		= create_dataref("laminar/B738/push_button/irs_key_8", "number")
B738DR_irs_key_9_pos		= create_dataref("laminar/B738/push_button/irs_key_9", "number")
B738DR_irs_key_ent_pos		= create_dataref("laminar/B738/push_button/irs_key_ent", "number")
B738DR_irs_key_clr_pos		= create_dataref("laminar/B738/push_button/irs_key_clr", "number")

B738DR_irs_was_full_allign 	= find_dataref("laminar/B738/irs/irs_was_full_allign")
B738DR_irs_was_full_allign2 = find_dataref("laminar/B738/irs/irs_was_full_allign2")

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

function B738DR_irs_sys_dspl_DRhandler() end
function B738DR_irs_dspl_sel_DRhandler() end
function B738DR_irs_dspl_sel_brt_DRhandler() end

function B738DR_kill_lat_lon_DRhandler() end

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

B738DR_kill_lat_lon	= create_dataref("laminar/B738/perf/kill_lat_lon", "number", B738DR_kill_lat_lon_DRhandler)

B738DR_irs_left 				= find_dataref("laminar/B738/toggle_switch/irs_left")
B738DR_irs_right 				= find_dataref("laminar/B738/toggle_switch/irs_right")
B738DR_irs_source 				= find_dataref("laminar/B738/toggle_switch/irs_source")
B738DR_irs_sys_dspl = 			create_dataref("laminar/B738/toggle_switch/irs_sys_dspl", "number", B738DR_irs_sys_dspl_DRhandler)
B738DR_irs_dspl_sel = 			create_dataref("laminar/B738/toggle_switch/irs_dspl_sel", "number", B738DR_irs_dspl_sel_DRhandler)
B738DR_irs_dspl_sel_brt = 		create_dataref("laminar/B738/toggle_switch/irs_dspl_sel_brt", "number", B738DR_irs_dspl_sel_brt_DRhandler)

B738DR_irs1_restart			= find_dataref("laminar/B738/toggle_switch/irs_restart")
B738DR_irs2_restart			= find_dataref("laminar/B738/toggle_switch/irs2_restart")

B738DR_irs_timer				= create_dataref("laminar/B738/irs/irs_timer", "number")

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

-- IRS NAVIGATION SWITCH
function B738_irs_source_left_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_source == 0 then
			B738DR_irs_source = -1
		elseif B738DR_irs_source == 1 then
			B738DR_irs_source = 0
		end
	end
end

function B738_irs_source_right_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_source == -1 then
			B738DR_irs_source = 0
		elseif B738DR_irs_source == 0 then
			B738DR_irs_source = 1
		end
	end
end

-- IRS L SWITCH
function B738_irs_L_left_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_left == 1 then
			B738DR_irs_left = 0
		elseif B738DR_irs_left == 2 then
			B738DR_irs_left = 1
			alignment_left_status = 0
			B738DR_irs_pos_set = "*****.*******.*"
			if B738DR_irs_right < 2 then
				B738DR_irs_pos_fmc_set = ""
				B738DR_irs_hdg_fmc_set = "---"
			end
		elseif B738DR_irs_left == 3 then
			B738DR_irs_left = 2
		end
	end
end

function B738_irs_L_right_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_left == 0 then
			B738DR_irs_left = 1
			alignment_left_enable = 1		-- start alignment
		elseif B738DR_irs_left == 1 then
			B738DR_irs_left = 2
		elseif B738DR_irs_left == 2 then
			B738DR_irs_left = 3
			alignment_left_entry = 0
		end
	end
end

-- IRS R SWITCH
function B738_irs_R_left_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_right == 1 then
			B738DR_irs_right = 0
		elseif B738DR_irs_right == 2 then
			B738DR_irs_right = 1
			alignment_right_status = 0
			B738DR_irs2_pos_set = "*****.*******.*"
			if B738DR_irs_left < 2 then
				B738DR_irs_pos_fmc_set = ""
				B738DR_irs_hdg_fmc_set = "---"
			end
		elseif B738DR_irs_right == 3 then
			B738DR_irs_right = 2
		end
	end
end

function B738_irs_R_right_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_right == 0 then
			B738DR_irs_right = 1
			alignment_right_enable = 1		-- start alignment
		elseif B738DR_irs_right == 1 then
			B738DR_irs_right = 2
		elseif B738DR_irs_right == 2 then
			B738DR_irs_right = 3
			alignment_right_entry = 0
		end
	end
end

-- IRS SYSTEM DISPLAY SWITCH
function B738_irs_sys_dspl_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_sys_dspl == 0 then
			B738DR_irs_sys_dspl = 1
		elseif B738DR_irs_sys_dspl == 1 then
			B738DR_irs_sys_dspl = 0
		end
	end
end

-- IRS DISPLAY SELECT
function B738_irs_dspl_sel_left_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_dspl_sel == 1 then
			B738DR_irs_dspl_sel = 0
		elseif B738DR_irs_dspl_sel == 2 then
			B738DR_irs_dspl_sel = 1
		elseif B738DR_irs_dspl_sel == 3 then
			B738DR_irs_dspl_sel = 2
		elseif B738DR_irs_dspl_sel == 4 then
			B738DR_irs_dspl_sel = 3
		end
	elseif phase == 2 then
		if B738DR_irs_dspl_sel == 0 then	-- spring to TK/GS
			B738DR_irs_dspl_sel = 1
			irs_dspl_test_enable = 1
			irs_dspl_test_run = 1
		end
	end
end

function B738_irs_dspl_sel_right_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_dspl_sel == 0 then
			B738DR_irs_dspl_sel = 1
		elseif B738DR_irs_dspl_sel == 1 then
			B738DR_irs_dspl_sel = 2
		elseif B738DR_irs_dspl_sel == 2 then
			B738DR_irs_dspl_sel = 3
		elseif B738DR_irs_dspl_sel == 3 then
			B738DR_irs_dspl_sel = 4
		end
	end
end

-- IRS DISPLAY BRIGHT KNOB
function B738_irs_dspl_brt_left_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_dspl_sel_brt >= 0.1 then
			B738DR_irs_dspl_sel_brt = B738DR_irs_dspl_sel_brt - 0.1
		end
	end
end

function B738_irs_dspl_brt_right_CMDhandler(phase, duration)
	if phase == 0 then
		if B738DR_irs_dspl_sel_brt <= 0.9 then
			B738DR_irs_dspl_sel_brt = B738DR_irs_dspl_sel_brt + 0.1
		end
	end
end

-- IRS numpad function
function clear_disable_key()
	local ii = 0
	for ii = 0, 9 do
		disable_key[ii] = 0
	end
end

function set_disable_key(first_key, last_key)
	local ii = 0
	for ii = first_key, last_key do
		disable_key[ii] = 1
	end
end

function irs_check_deg()
	
	local check_len = 0
	
	if irs_entry_mod == IRS_entry_mode_lat then
		check_len = string.len(irs_entry_pos_lat)
		clear_disable_key()
		if check_len == 1 then
			disable_key[9] = 1
		elseif check_len == 3 then
			set_disable_key(6, 9)
		end
	elseif irs_entry_mod == IRS_entry_mode_lon then
		check_len = string.len(irs_entry_pos_lon)
		clear_disable_key()
		if check_len == 0 then
			set_disable_key(0, 9)
			disable_key[4] = 0
			disable_key[6] = 0
		elseif check_len == 1 then
			set_disable_key(2, 9)
		elseif check_len == 2 then
			if string.sub(irs_entry_pos_lon, 2, 2) == "1" then
				set_disable_key(8, 9)
			end
		elseif check_len == 4 then
			set_disable_key(6, 9)
		end
	elseif irs_entry_mod == IRS_entry_mode_hdg then
		clear_disable_key()
		check_len = string.len(irs_entry_hdg)
		if check_len == 0 then
			set_disable_key(4, 9)
		elseif check_len == 1 then
			if string.sub(irs_entry_hdg, 1, 1) == "3" then
				set_disable_key(6, 9)
			end
		end
	else
		clear_disable_key()
		set_disable_key(0, 9)
		disable_key[2] = 0
		disable_key[5] = 0
		disable_key[8] = 0
	end
	
end



-- IRS Keypad
function B738_irs_key_0_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_0_pos = 1
		if disable_key[0] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "0"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "0"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "0"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_0_pos = 0
	end
end

function B738_irs_key_1_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_1_pos = 1
		if disable_key[1] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "1"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "1"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "1"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_1_pos = 0
	end
end

function B738_irs_key_2_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_2_pos = 1
		if disable_key[2] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == 0 then
					irs_entry_mod = IRS_entry_mode_lat
					irs_entry_pos_lat = "N"
					irs_ent_light = 1
					irs_entry_pos_lon = ""
				elseif irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "2"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "2"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "2"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_2_pos = 0
	end
end

function B738_irs_key_3_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_3_pos = 1
		if disable_key[3] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "3"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "3"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "3"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_3_pos = 0
	end
end

function B738_irs_key_4_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_4_pos = 1
		if disable_key[4] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) == 0 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "W"
				elseif irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "4"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "4"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "4"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_4_pos = 0
	end
end

function B738_irs_key_5_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_5_pos = 1
		if disable_key[5] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "5"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "5"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == 0 then
					irs_entry_mod = IRS_entry_mode_hdg
					irs_entry_hdg = ""
					irs_ent_light = 1
					irs_clr_light = 1
				elseif irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "5"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_5_pos = 0
	end
end

function B738_irs_key_6_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_6_pos = 1
		if disable_key[6] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) == 0 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "E"
				elseif irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "6"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "6"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "6"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_6_pos = 0
	end
end

function B738_irs_key_7_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_7_pos = 1
		if disable_key[7] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "7"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "7"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "7"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_7_pos = 0
	end
end

function B738_irs_key_8_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_8_pos = 1
		if disable_key[8] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == 0 then
					irs_entry_mod = IRS_entry_mode_lat
					irs_entry_pos_lat = "S"
					irs_ent_light = 1
					irs_entry_pos_lon = ""
				elseif irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "8"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "8"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "8"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_8_pos = 0
	end
end

function B738_irs_key_9_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_9_pos = 1
		if disable_key[9] == 0 then
			if B738DR_irs_dspl_sel == 2 then
				if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) < 6 then
					irs_entry_pos_lat = irs_entry_pos_lat .. "9"
				elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) < 7 then
					irs_entry_pos_lon = irs_entry_pos_lon .. "9"
				end
				irs_check_deg()
			elseif B738DR_irs_dspl_sel == 4 then
				if irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) < 3 then
					irs_entry_hdg = irs_entry_hdg .. "9"
				end
				irs_check_deg()
			end
		end
	elseif phase == 2 then
		B738DR_irs_key_9_pos = 0
	end
end

function B738_irs_key_ent_CMDhandler(phase, duration)
	
	local hdg_num = 0
	
	if phase == 0 then
		B738DR_irs_key_ent_pos = 1
		if irs_entry_mod == IRS_entry_mode_lat and string.len(irs_entry_pos_lat) == 6 then
			irs_entry_mod = IRS_entry_mode_lon
			irs_clr_light = 1
		elseif irs_entry_mod == IRS_entry_mode_lon and string.len(irs_entry_pos_lon) == 7 then
			-- set IRS position
			B738DR_irs_pos_set = string.sub(irs_entry_pos_lat, 1, -2) .. "." .. string.sub(irs_entry_pos_lat, -1, -1)
			B738DR_irs_pos_set = B738DR_irs_pos_set .. string.sub(irs_entry_pos_lon, 1, -2) .. "." .. string.sub(irs_entry_pos_lon, -1, -1)
			B738DR_irs2_pos_set = B738DR_irs_pos_set
			irs_entry_mod = 0
			irs_ent_light = 0
			irs_clr_light = 0
		elseif irs_entry_mod == IRS_entry_mode_hdg and string.len(irs_entry_hdg) == 3 then
			-- set IRS heading
			irs_hdg_set = irs_entry_hdg
			irs2_hdg_set = irs_hdg_set
			irs_entry_mod = 0
			irs_ent_light = 0
			irs_clr_light = 0
		end
		irs_check_deg()
	elseif phase == 2 then
		B738DR_irs_key_ent_pos = 0
	end
end

function B738_irs_key_clr_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_key_clr_pos = 1
		if irs_entry_mod == IRS_entry_mode_lat then
			irs_entry_mod = 0
			irs_entry_pos_lat = ""
			irs_entry_pos_lon = ""
			irs_entry_hdg = ""
			irs_ent_light = 0
			irs_clr_light = 0
		elseif irs_entry_mod == IRS_entry_mode_lon then
			if string.len(irs_entry_pos_lon) == 0 then
				irs_entry_mod = IRS_entry_mode_lat
				irs_clr_light = 0
			else
				irs_entry_pos_lon = ""
			end
		elseif irs_entry_mod == IRS_entry_mode_hdg then
			irs_entry_mod = 0
			irs_entry_pos_lat = ""
			irs_entry_pos_lon = ""
			irs_entry_hdg = ""
			irs_ent_light = 0
			irs_clr_light = 0
		end
		irs_check_deg()
	elseif phase == 2 then
		B738DR_irs_key_clr_pos = 0
	end
end

-- entry FMC to IRS position
function B738_fmc_to_irs_pos_CMDhandler(phase, duration)
	if phase == 0 then
		B738DR_irs_pos_set = B738DR_irs_pos_fmc_set
		B738DR_irs2_pos_set = B738DR_irs_pos_fmc_set
	end
end

-- entry FMC to IRS heading
function B738_fmc_to_irs_hdg_CMDhandler(phase, duration)
	if phase == 0 then
		irs_hdg_set = B738DR_irs_hdg_fmc_set
		irs2_hdg_set = B738DR_irs_hdg_fmc_set
	end
end

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

B738CMD_irs_source_left = create_command("laminar/B738/toggle_switch/irs_source_left", "IRS source left", B738_irs_source_left_CMDhandler)
B738CMD_irs_source_right = create_command("laminar/B738/toggle_switch/irs_source_right", "IRS source right", B738_irs_source_right_CMDhandler)
B738CMD_irs_L_left = create_command("laminar/B738/toggle_switch/irs_L_left", "IRS L left", B738_irs_L_left_CMDhandler)
B738CMD_irs_L_right = create_command("laminar/B738/toggle_switch/irs_L_right", "IRS L right", B738_irs_L_right_CMDhandler)
B738CMD_irs_R_left = create_command("laminar/B738/toggle_switch/irs_R_left", "IRS R left", B738_irs_R_left_CMDhandler)
B738CMD_irs_R_right = create_command("laminar/B738/toggle_switch/irs_R_right", "IRS R right", B738_irs_R_right_CMDhandler)
B738CMD_irs_sys_dspl = create_command("laminar/B738/toggle_switch/irs_sys_dspl", "IRS system display", B738_irs_sys_dspl_CMDhandler)
B738CMD_irs_dspl_sel_left = create_command("laminar/B738/toggle_switch/irs_dspl_sel_left", "IRS display select left", B738_irs_dspl_sel_left_CMDhandler)
B738CMD_irs_dspl_sel_right = create_command("laminar/B738/toggle_switch/irs_dspl_sel_right", "IRS display select right", B738_irs_dspl_sel_right_CMDhandler)
B738CMD_irs_dspl_brt_left = create_command("laminar/B738/toggle_switch/irs_dspl_sel_brt_left", "IRS display bright left", B738_irs_dspl_brt_left_CMDhandler)
B738CMD_irs_dspl_brt_right = create_command("laminar/B738/toggle_switch/irs_dspl_sel_brt_right", "IRS display bright right", B738_irs_dspl_brt_right_CMDhandler)

B738CMD_irs_key_0		= create_command("laminar/B738/push_button/irs_key_0", "IRS keypad 0", B738_irs_key_0_CMDhandler)
B738CMD_irs_key_1		= create_command("laminar/B738/push_button/irs_key_1", "IRS keypad 1", B738_irs_key_1_CMDhandler)
B738CMD_irs_key_2		= create_command("laminar/B738/push_button/irs_key_2", "IRS keypad 2", B738_irs_key_2_CMDhandler)
B738CMD_irs_key_3		= create_command("laminar/B738/push_button/irs_key_3", "IRS keypad 3", B738_irs_key_3_CMDhandler)
B738CMD_irs_key_4		= create_command("laminar/B738/push_button/irs_key_4", "IRS keypad 4", B738_irs_key_4_CMDhandler)
B738CMD_irs_key_5		= create_command("laminar/B738/push_button/irs_key_5", "IRS keypad 5", B738_irs_key_5_CMDhandler)
B738CMD_irs_key_6		= create_command("laminar/B738/push_button/irs_key_6", "IRS keypad 6", B738_irs_key_6_CMDhandler)
B738CMD_irs_key_7		= create_command("laminar/B738/push_button/irs_key_7", "IRS keypad 7", B738_irs_key_7_CMDhandler)
B738CMD_irs_key_8		= create_command("laminar/B738/push_button/irs_key_8", "IRS keypad 8", B738_irs_key_8_CMDhandler)
B738CMD_irs_key_9		= create_command("laminar/B738/push_button/irs_key_9", "IRS keypad 9", B738_irs_key_9_CMDhandler)
B738CMD_irs_key_ent		= create_command("laminar/B738/push_button/irs_key_ent", "IRS keypad ENT", B738_irs_key_ent_CMDhandler)
B738CMD_irs_key_clr		= create_command("laminar/B738/push_button/irs_key_clr", "IRS keypad CLR", B738_irs_key_clr_CMDhandler)

B738CMD_fmc_to_irs_pos	= create_command("laminar/B738/push_button/fmc_to_irs_pos", "not for use", B738_fmc_to_irs_pos_CMDhandler)
B738CMD_fmc_to_irs_hdg	= create_command("laminar/B738/push_button/fmc_to_irs_hdg", "not for use", B738_fmc_to_irs_hdg_CMDhandler)

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



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



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



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



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



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

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

-- function B738_rescale(in1, out1, in2, out2, x)
    -- if x < in1 then return out1 end
    -- if x > in2 then return out2 end
    -- return out1 + (out2 - out1) * (x - in1) / (in2 - in1)
-- end

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

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

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

    return z

end


function latitude()

local lat = (math.abs(simDR_latitude))

local lat_deg = (math.floor(lat))

local lat_min_dec = (math.fmod(lat,1.0)*600)

local lat_min_dec2 = (string.format("%06.3f",lat_min_dec))

B738DR_latitude_deg = lat_deg
B738DR_latitude_min = lat_min_dec2
B738DR_latitude_NS = 0
if simDR_latitude < 0 then
	B738DR_latitude_NS = 1
end

end


function longitude()

local lon = (math.abs(simDR_longitude))

local lon_deg = (math.floor(lon))

local lon_min_dec = (math.fmod(lon,1.0)*600)

local lon_min_dec2 = (string.format("%06.3f",lon_min_dec))

B738DR_longitude_deg = lon_deg
B738DR_longitude_min = lon_min_dec2
B738DR_longitude_EW = 0
if simDR_longitude < 0 then
	B738DR_longitude_EW = 1
end


end

function dist_to_latlon(latlon_str)
	
	local dist_lat = 0
	local dist_lon  = 0
	
	dist_lat = tonumber(string.sub(latlon_str, 2, 3)) + (tonumber(string.sub(latlon_str, 4, 7)) / 60)
	if string.sub(latlon_str, 1, 1) == "S" then
		dist_lat = -dist_lat
	end
	
	dist_lon = tonumber(string.sub(latlon_str, 9, 11)) + (tonumber(string.sub(latlon_str, 12, 15)) / 60)
	if string.sub(latlon_str, 8, 8) == "W" then
		dist_lon = -dist_lon
	end
	
	return nd_calc_dist2(simDR_latitude, simDR_longitude, dist_lat, dist_lon)
	
end

function nd_calc_dist2(req_lat, req_lon, req_lat2, req_lon2)
	
	local nd_lat = math.rad(req_lat)
	local nd_lat2 = math.rad(req_lat2)
	
	local nd_dlat = math.rad(req_lat2-req_lat)
	local nd_dlon = math.rad(req_lon2-req_lon)
	
	local nd_a = math.sin(nd_dlat/2)*math.sin(nd_dlat/2)+math.cos(nd_lat)*math.cos(nd_lat2)*math.sin(nd_dlon/2)*math.sin(nd_dlon/2)
	local nd_b = 2 * math.atan2(math.sqrt(nd_a), math.sqrt(1-nd_a))
	local nd_dis = nd_b * 3440.064795	--nm
	
	return nd_dis
end

function B738_random_irs_time()
	
	local lat_irs = math.abs(simDR_latitude)
	
	if B738DR_align_time == 1 then
		-- LONG
		irs_time = 150
	elseif B738DR_align_time == 2 then
		-- SHORT
		irs_time = 60
	else
		-- REAL
		lat_irs = math.min(lat_irs, 82)
		if lat_irs <= 19 then
			irs_time = B738_rescale(0, 300, 19, 320, lat_irs)
		elseif lat_irs <= 40 then
			irs_time = B738_rescale(19, 320, 40, 400, lat_irs)
		elseif lat_irs <= 70 then
			irs_time = B738_rescale(40, 400, 70, 610, lat_irs)
		else
			irs_time = B738_rescale(70, 610, 82, 1020, lat_irs)
		end
	end
	irs_time = roundUpToIncrement(irs_time, 1)
end

function B738_irs_dspl_test_off()
	irs_dspl_test_run = 0
	irs_align_fail_left = 0
	B738DR_irs_align_left = 0
	irs_dc_fail_left = 0
	B738DR_irs_on_dc_left = 0
	irs_align_fail_right = 0
	B738DR_irs_align_right = 0
	irs_dc_fail_right = 0
	B738DR_irs_on_dc_right = 0
end

function B738_irs_display_data(in_mode)

	local irs_sys_dspl = B738DR_irs_sys_dspl
	local irs_dspl_sel = B738DR_irs_dspl_sel
	local used_x = 0
	local alignment_remain = 0
	local alignment_entry = 0
	local irs_status = 0
	local irs_mode = 0
	local true_track = 0
	local irs_fail = 0
	local alignment_status = 0
	local irs_allign_show = 0
	local ii = 0
	local show_display = false
	
	B738DR_irs_entry_len = 0
	B738DR_irs_entry_pos_show = 0
	B738DR_irs_entry_hdg_show = 0
	
	-- run test IRS DSPL SEL
	if irs_dspl_test_enable == 1 
	and B738DR_irs_dspl_sel > 0 then
		irs_dspl_test_enable = 0
		if is_timer_scheduled(B738_irs_dspl_test_off) == false then
			run_after_time(B738_irs_dspl_test_off, 10)	-- 10 seconds
		end
	end
	
	if irs_dspl_test_run == 1 or (B738DR_lights_test == 1 and simDR_radio_height_pilot_ft < 50) then
		if irs_left_status == 1
		or irs_right_status == 1 then
			-- test running
			B738DR_irs_left1 = 88
			B738DR_irs_left2 = 888
			B738DR_irs_right1 = 188
			B738DR_irs_right2 = 888
			B738DR_irs_left1_show = 1
			B738DR_irs_left2_show = 1
			B738DR_irs_right1_show = 1
			B738DR_irs_right2_show = 1
			B738DR_irs_ns_show = 2
			B738DR_irs_ew_show = 2
			B738DR_irs_lat_deg_show = 0
			B738DR_irs_lat_min_show = 0
			B738DR_irs_lon_deg_show = 0
			B738DR_irs_lon_min_show = 0
			B738DR_irs_decimals_show = 1
		end
	else
		if in_mode == 0 then
			if irs_sys_dspl == 0 then
				alignment_remain = (B738DR_alignment_left_remain / 60) + 1
				if B738DR_irs_left == 0 then
					irs_status = 0
				else
					irs_status = irs_left_status
				end
				irs_mode = irs_left_mode
				true_track = simDR_mag_hdg
				irs_fail = irs_left_fail
				alignment_entry = alignment_left_entry
				alignment_status = alignment_left_status
				
				show_display = true
			end
		else
			if irs_sys_dspl ~= 0 then
				alignment_remain = (B738DR_alignment_right_remain / 60) + 1
				if B738DR_irs_right == 0 then
					irs_status = 0
				else
					irs_status = irs_right_status
				end
				irs_mode = irs_right_mode
				true_track = simDR_mag_hdg_fo
				irs_fail = irs_right_fail
				alignment_entry = alignment_right_entry
				alignment_status = alignment_right_status
				
				show_display = true
			end
		end
		
		if show_display == true then
			B738DR_irs_left1_show = 0
			B738DR_irs_right1_show = 0
			
			if irs_status == 0 then
				B738DR_irs_ns_show = 0
				B738DR_irs_ew_show = 0
				B738DR_irs_lat_deg_show = 0
				B738DR_irs_lat_min_show = 0
				B738DR_irs_lon_deg_show = 0
				B738DR_irs_lon_min_show = 0
				B738DR_irs_decimals_show = 0
				B738DR_irs_left2_show = 0
				B738DR_irs_right2_show = 0
			elseif irs_fail == 1 then
				B738DR_irs_left1 = 0
				B738DR_irs_right1 = 0
				B738DR_irs_left2 = 0
				B738DR_irs_right2 = 0
				B738DR_irs_ns_show = 0
				B738DR_irs_ew_show = 0
				B738DR_irs_lat_deg_show = 0
				B738DR_irs_lat_min_show = 0
				B738DR_irs_lon_deg_show = 0
				B738DR_irs_lon_min_show = 0
				B738DR_irs_decimals_show = 0
				B738DR_irs_left2_show = 0
				B738DR_irs_right2_show = 0
			else
				if irs_dspl_sel == 4 and irs_entry_mod == IRS_entry_mode_hdg then 
					
					-- display heading entry
					
					B738DR_irs_entry_len = string.len(irs_entry_hdg)
					B738DR_irs_entry_hdg_show = 1
					
					used_x = B738DR_irs_entry_len
					if used_x > 0 then
						for ii = 1, used_x do
							B738DR_irs_entry[ii-1] = tonumber(string.sub(irs_entry_hdg, ii, ii))
						end
					end
					
					B738DR_irs_left2 = 0
					B738DR_irs_right2 = 0
					B738DR_irs_ns_show = 0
					B738DR_irs_ew_show = 0
					B738DR_irs_lat_deg_show = 0
					B738DR_irs_lat_min_show = 0
					B738DR_irs_lon_deg_show = 0
					B738DR_irs_lon_min_show = 0
					B738DR_irs_decimals_show = 0
					B738DR_irs_left2_show = 0
					B738DR_irs_right2_show = 0
					
					--ENT/CLR lights
					
					
				elseif irs_dspl_sel == 2 and (irs_entry_mod == IRS_entry_mode_lat or irs_entry_mod == IRS_entry_mode_lon) then 
					
					-- display position entry
					
					B738DR_irs_entry_len = string.len(irs_entry_pos_lat) + string.len(irs_entry_pos_lon)
					B738DR_irs_ns_show = 1
					B738DR_irs_ew_show = 0
					B738DR_irs_decimals_show = 1
					
					used_x = string.len(irs_entry_pos_lat)
					if used_x > 1 then
						for ii = 2, used_x do
							B738DR_irs_entry[ii-2] = tonumber(string.sub(irs_entry_pos_lat, ii, ii))
						end
					end
					
					B738DR_latitude_NS = 0
					if string.sub(irs_entry_pos_lat, 1, 1) == "S" then
						B738DR_latitude_NS = 1
					end
					
					used_x = string.len(irs_entry_pos_lon)
					if used_x == 1 then
						B738DR_irs_ew_show = 1
					elseif used_x > 1 then
						for ii = 2, used_x do
							B738DR_irs_entry[ii+3] = tonumber(string.sub(irs_entry_pos_lon, ii, ii))
						end
						B738DR_irs_ew_show = 1
					end
					
					B738DR_longitude_EW = 0
					if string.sub(irs_entry_pos_lon, 1, 1) == "W" then
						B738DR_longitude_EW = 1
					end
					
					B738DR_irs_entry_pos_show = 1
					
					B738DR_irs_lat_deg_show = 0
					B738DR_irs_lat_min_show = 0
					B738DR_irs_lon_deg_show = 0
					B738DR_irs_lon_min_show = 0
					B738DR_irs_left2_show = 0
					B738DR_irs_right2_show = 0
					B738DR_irs_left1 = 0
					B738DR_irs_right1 = 0
					B738DR_irs_left2 = 0
					B738DR_irs_right2 = 0
				else
					if irs_mode == 1 then
						if irs_dspl_sel == 2 and alignment_status == 1 then 
							-- PPOS -> latitude / longtitude
							B738DR_irs_ns_show = 1
							B738DR_irs_ew_show = 1
							B738DR_irs_lat_deg_show = 1
							B738DR_irs_lat_min_show = 1
							B738DR_irs_lon_deg_show = 1
							B738DR_irs_lon_min_show = 1
							B738DR_irs_decimals_show = 1
							B738DR_irs_left2_show = 0
							B738DR_irs_right2_show = 0
							B738DR_irs_left1 = 0
							B738DR_irs_right1 = 0
							B738DR_irs_left2 = 0
							B738DR_irs_right2 = 0
						elseif irs_dspl_sel == 4 then
							-- alignment remain in minutes
							B738DR_irs_left1 = 0
							B738DR_irs_right1 = 0
							B738DR_irs_left2 = 0
							B738DR_irs_right2 = 0
							B738DR_irs_ns_show = 0
							B738DR_irs_ew_show = 0
							B738DR_irs_lat_deg_show = 0
							B738DR_irs_lat_min_show = 0
							B738DR_irs_lon_deg_show = 0
							B738DR_irs_lon_min_show = 0
							B738DR_irs_decimals_show = 0
							B738DR_irs_right2_show = 0
							B738DR_irs_left2_show = 0
							if alignment_remain > 1 then
								irs_allign_show = 1
								if alignment_remain > 15 then
									B738DR_irs_right1 = 15
								else
									B738DR_irs_right1 = alignment_remain
								end
							end
						else
							B738DR_irs_left1 = 0
							B738DR_irs_right1 = 0
							B738DR_irs_left2 = 0
							B738DR_irs_right2 = 0
							B738DR_irs_ns_show = 0
							B738DR_irs_ew_show = 0
							B738DR_irs_lat_deg_show = 0
							B738DR_irs_lat_min_show = 0
							B738DR_irs_lon_deg_show = 0
							B738DR_irs_lon_min_show = 0
							B738DR_irs_decimals_show = 0
							B738DR_irs_left2_show = 0
							B738DR_irs_right2_show = 0
						end
					elseif irs_mode == 2 then	-- < 3
						if irs_dspl_sel == 1 then
							-- TK/GS -> true track course / ground speed knots
							used_x = true_track
							B738DR_irs_left2 = math.floor(used_x + 0.5)
							used_x = simDR_ground_speed * 1.94384
							B738DR_irs_right2 = math.floor(used_x + 0.5)
							B738DR_irs_ns_show = 0
							B738DR_irs_ew_show = 0
							B738DR_irs_lat_deg_show = 0
							B738DR_irs_lat_min_show = 0
							B738DR_irs_lon_deg_show = 0
							B738DR_irs_lon_min_show = 0
							B738DR_irs_decimals_show = 0
							B738DR_irs_left2_show = 1
							B738DR_irs_right2_show = 1
						elseif irs_dspl_sel == 2 then
							-- PPOS -> latitude / longtitude
							B738DR_irs_ns_show = 1
							B738DR_irs_ew_show = 1
							B738DR_irs_lat_deg_show = 1
							B738DR_irs_lat_min_show = 1
							B738DR_irs_lon_deg_show = 1
							B738DR_irs_lon_min_show = 1
							B738DR_irs_decimals_show = 1
							B738DR_irs_left2_show = 0
							B738DR_irs_right2_show = 0
						elseif irs_dspl_sel == 3 then
							-- WIND -> wind direction / wind speed knots
							used_x = simDR_wind_spd
							B738DR_irs_right2 = math.floor(used_x + 0.5)
							if B738DR_irs_right2 == 0 then	-- no wind
								used_x = 0
							else
								used_x = simDR_wind_hdg
							end
							B738DR_irs_left2 = math.floor(used_x + 0.5)
							B738DR_irs_ns_show = 0
							B738DR_irs_ew_show = 0
							B738DR_irs_lat_deg_show = 0
							B738DR_irs_lat_min_show = 0
							B738DR_irs_lon_deg_show = 0
							B738DR_irs_lon_min_show = 0
							B738DR_irs_decimals_show = 0
							B738DR_irs_left2_show = 1
							B738DR_irs_right2_show = 1
						elseif irs_dspl_sel == 4 then
						-- HDG/STS -> true heading / status code
							used_x = simDR_true_heading
							B738DR_irs_left2 = math.floor(used_x + 0.5)
							used_x = 024
							B738DR_irs_right2 = math.floor(used_x + 0.5)
							B738DR_irs_ns_show = 0
							B738DR_irs_ew_show = 0
							B738DR_irs_lat_deg_show = 0
							B738DR_irs_lat_min_show = 0
							B738DR_irs_lon_deg_show = 0
							B738DR_irs_lon_min_show = 0
							B738DR_irs_decimals_show = 0
							B738DR_irs_left2_show = 1
							B738DR_irs_right2_show = 1
						end
					elseif irs_mode == 3 then
						-- display only attitude and heading info
						used_x = simDR_vpath
						B738DR_irs_left2 = math.floor(used_x + 0.5)
						used_x = simDR_true_heading
						B738DR_irs_right2 = math.floor(used_x + 0.5)
						B738DR_irs_ns_show = 0
						B738DR_irs_ew_show = 0
						B738DR_irs_lat_deg_show = 0
						B738DR_irs_lat_min_show = 0
						B738DR_irs_lon_deg_show = 0
						B738DR_irs_lon_min_show = 0
						B738DR_irs_decimals_show = 0
						B738DR_irs_left2_show = 1
						B738DR_irs_right2_show = 1
					else
						B738DR_irs_right2 = 0
						B738DR_irs_left2 = 0
						B738DR_irs_ns_show = 0
						B738DR_irs_ew_show = 0
						B738DR_irs_lat_deg_show = 0
						B738DR_irs_lat_min_show = 0
						B738DR_irs_lon_deg_show = 0
						B738DR_irs_lon_min_show = 0
						B738DR_irs_decimals_show = 0
						B738DR_irs_left2_show = 1
						B738DR_irs_right2_show = 1
					end
				end
			end
		-- else
			-- B738DR_irs_left1 = 0
			-- B738DR_irs_left2 = 0
			-- B738DR_irs_right1 = 0
			-- B738DR_irs_right2 = 0
			-- B738DR_irs_left1_show = 0
			-- B738DR_irs_left2_show = 0
			-- B738DR_irs_right1_show = 0
			-- B738DR_irs_right2_show = 0
			-- B738DR_irs_ns_show = 0
			-- B738DR_irs_ew_show = 0
			-- B738DR_irs_lat_deg_show = 0
			-- B738DR_irs_lat_min_show = 0
			-- B738DR_irs_lon_deg_show = 0
			-- B738DR_irs_lon_min_show = 0
			-- B738DR_irs_decimals_show = 0
		end
	end
	
	B738DR_irs_allign_show = irs_allign_show
	
end


function B738_irs_left_off()
	irs_left_status = 0		-- irs left off
	irs_left_mode = 0
	B738DR_irs_was_full_allign = 0
	alignment_left_entry = 0
end

function B738_irs_left_align()
	if B738DR_alignment_left_remain > 0 then
		B738DR_alignment_left_remain = B738DR_alignment_left_remain - 1
	else
		B738DR_alignment_left_remain = 0
	end
end

function B738_irs_right_off()
	irs_right_status = 0		-- irs right off
	irs_right_mode = 0
	B738DR_irs_was_full_allign2 = 0
	alignment_right_entry = 0
end

function B738_irs_right_align()
	if B738DR_alignment_right_remain > 0 then
		B738DR_alignment_right_remain = B738DR_alignment_right_remain - 1
	else
		B738DR_alignment_right_remain = 0
	end
end

function B738_irs_test_left2()
	irs_left_test = 0
end

function B738_irs_test_right2()
	irs_right_test = 0
end

function B738_irs_test_left()
	if B738DR_ac_tnsbus1_status == 1 then
		B738DR_irs_on_dc_left = 0
	end
	--irs_left_test = 0
	if is_timer_scheduled(B738_irs_test_left2) == false then
		run_after_time(B738_irs_test_left2, 2.5)		-- 1.5 seconds
	end
end

function B738_irs_test_right()
	if B738DR_ac_tnsbus2_status == 1 then
		B738DR_irs_on_dc_right = 0
	end
	--irs_right_test = 0
	if is_timer_scheduled(B738_irs_test_right2) == false then
		run_after_time(B738_irs_test_right2, 2.5)		-- 1.5 seconds
	end
end

function B738_run_irs_left()
	if is_timer_scheduled(B738_irs_left_align) == false then
		run_at_interval(B738_irs_left_align, 1)	-- every 60 seconds
	else
		stop_timer(B738_irs_left_align)
		run_at_interval(B738_irs_left_align, 1)	-- every 60 seconds
	end
	B738_random_irs_time()
	
	if is_timer_scheduled(B738_irs_test_left2) == true then
		stop_timer(B738_irs_test_left2)
	end
	
	if irs_left_status ~= 1 then
		if is_timer_scheduled(B738_irs_test_left) == false then
			run_after_time(B738_irs_test_left, 4)		-- 4 seconds
		else
			stop_timer(B738_irs_test_left)
			run_after_time(B738_irs_test_left, 4)		-- 4 seconds
		end
		B738DR_irs_on_dc_left = 1 * B738DR_brightness_level
		irs_left_test = 1
	elseif irs_left_status == 1 and B738DR_irs_was_full_allign ~= 0 then
		irs_time = 0
	end
	B738DR_alignment_left_remain = irs_time
end

function B738_run_irs_right()
	if is_timer_scheduled(B738_irs_right_align) == false then
		run_at_interval(B738_irs_right_align, 1)	-- every 60 seconds
	else
		stop_timer(B738_irs_right_align)
		run_at_interval(B738_irs_right_align, 1)	-- every 60 seconds
	end
	B738_random_irs_time()
	
	if is_timer_scheduled(B738_irs_test_right2) == true then
		stop_timer(B738_irs_test_right2)
	end
	
	if irs_right_status ~= 1 then
		if is_timer_scheduled(B738_irs_test_right) == false then
			run_after_time(B738_irs_test_right, 4)		-- 4 seconds
		else
			stop_timer(B738_irs_test_right)
			run_after_time(B738_irs_test_right, 4)		-- 4 seconds
		end
		B738DR_irs_on_dc_right = 1 * B738DR_brightness_level
		irs_right_test = 1
	elseif irs_left_status == 1 and B738DR_irs_was_full_allign2 ~= 0 then
		irs_time = 0
	end
	B738DR_alignment_right_remain = irs_time
end



---- TEMPORARY - WAITING FOR INPUT ----
function B738_entry_left()
	alignment_left_entry = 1
	irs_left_mode = 1	-- align mode
end
function B738_entry_right()
	alignment_right_entry = 1
	irs_right_mode = 1	-- align mode
end
---- TEMPORARY - WAITING FOR INPUT ----

function align_accept_left()
	irs_left_mode = 2	-- nav mode
	B738DR_irs_pos = actual_pos
	alignment_left_status = 1
end

function align_accept_right()
	irs_right_mode = 2	-- nav mode
	B738DR_irs2_pos = actual_pos
	alignment_right_status = 1
end

function realign_accept_left()
	B738DR_irs_pos = actual_pos
	alignment_left_status = 1
end

function realign_accept_right()
	B738DR_irs2_pos = actual_pos
	alignment_right_status = 1
end

function B738_irs_system(in_side)

	local delta_lat = 0
	local delta_lon = 0
	local aircraft_move = 0
	local align_dist = 0
	local align_dist_ok = 0
	
	if in_side == 0 then
		local irs_lft = B738DR_irs_left
		
		if irs_lft == 0 then
			-- stop align
			if is_timer_scheduled(B738_irs_left_align) == true then
				stop_timer(B738_irs_left_align)
			end
			if is_timer_scheduled(B738_irs_test_left2) == true then
				stop_timer(B738_irs_test_left2)
			end
			-- start shutdown cycle
			if is_timer_scheduled(B738_irs_left_off) == false then
				run_after_time(B738_irs_left_off, 20)	-- 30 seconds shutdown cycle
			end
			-- if irs_left_mode < 2 then
				-- irs_left_status = 0		-- irs left off
			-- end
			B738DR_irs_on_dc_left = 0
			alignment_left_entry = 0
			irs_left_mode = 0
			irs_left_restart = 0
			irs_left_fail = 0
			alignment_left_status = 0
			B738DR_irs_pos = "-----.-------.-"
		else
			if is_timer_scheduled(B738_irs_left_off) == true then
				stop_timer(B738_irs_left_off)
			end
			if alignment_left_enable == 1 then		-- run alignment
				alignment_left_enable = 0
				irs_left_mode = 1	-- align mode
				irs_left_restart = 0
	--			align_lat = simDR_latitude
	--			align_lon = simDR_longitude
				B738_run_irs_left()
			end
			irs_left_status = 1		-- irs left on
			if irs_lft == 3 then
				-- att mode
				if is_timer_scheduled(B738_entry_left) == false then	 -- delay for ATT mode
					run_after_time(B738_entry_left, 3)
				end
				if alignment_left_entry == 1 then
					B738DR_alignment_left_remain = -1		-- NAV align off
					if is_timer_scheduled(B738_irs_left_align) == true then
						stop_timer(B738_irs_left_align)
					end
					if irs_hdg_set ~= "---" then
						local test_x = simDR_true_heading
						test_x = math.floor(test_x + 0.5)
						local test_y = tonumber(irs_hdg_set) - test_x
						if test_y < 0 then
							test_y = -test_y
						end
						if irs_left_fail == 0 and test_y < 2 then
							irs_left_mode = 3	-- att mode
							alignment_left_status = 1
						else
							irs_left_fail = 1
							irs_left_mode = 4
							alignment_left_status = 0
						end
					end
				end
			else
				if is_timer_scheduled(B738_entry_left) == true then
					stop_timer(B738_entry_left)
				end
			end
			if B738DR_alignment_left_remain == 0 then
				if is_timer_scheduled(B738_irs_left_align) == true then
					stop_timer(B738_irs_left_align)
				end
				-- if irs_lft == 1 and irs_left_mode ~= 2 and irs_left_mode ~= 4 then
				if irs_lft == 1 and irs_left_mode ~= 4 then
					-- align mode
					irs_left_mode = 1	-- align mode
					-- alignment_left_entry = 0
					
					if B738DR_irs_was_full_allign ~= 0 then
						if alignment_left_status == 0 then ---and alignment_right_status == 0 then
							-- flashing, waiting for entry
							if B738DR_irs_pos_set ~= "*****.*******.*" then
								align_dist_ok = 0
								align_dist = dist_to_latlon(B738DR_irs_pos_set)
								if align_dist < 4 then	-- to 4NM
									align_dist_ok = 1
								end
								if align_dist_ok == 1 then
									if is_timer_scheduled(realign_accept_left) == false then
										run_after_time(realign_accept_left, 2)
									end
								else
									-- IRS fail
									irs_left_fail = 1
									irs_left_mode = 4
									alignment_left_status = 0
									alignment_left_entry = 0
								end
							end
						else
							-- alignment complete
								B738DR_irs_pos = actual_pos
								alignment_left_status = 1
						end
					end
				elseif irs_lft == 2 and irs_left_mode ~= 4 then
					-- nav mode
					if irs_right_mode == 2 then
						irs_left_mode = 2	-- nav mode
						B738DR_irs_pos = actual_pos
						alignment_left_status = 1
						alignment_left_entry = 1
					end
					if alignment_left_status == 0 then ---and alignment_right_status == 0 then
						-- flashing, waiting for entry
						if B738DR_irs_pos_set ~= "*****.*******.*" then
							align_dist_ok = 0
							align_dist = dist_to_latlon(B738DR_irs_pos_set)
							if align_dist < 4 then	-- to 4NM
								align_dist_ok = 1
							end
							if align_dist_ok == 1 then
								if is_timer_scheduled(align_accept_left) == false then
									run_after_time(align_accept_left, 2)
								end
							else
								-- IRS fail
								irs_left_fail = 1
								irs_left_mode = 4
								alignment_left_status = 0
								alignment_left_entry = 0
							end
						end
					else
						-- alignment complete
							irs_left_mode = 2	-- nav mode
							B738DR_irs_pos = actual_pos
							alignment_left_status = 1
					end
				end
			end
			--if alignment_left_status == 1 then
			if alignment_left_status == 1 and irs_left_mode == 2 then
				B738DR_irs_pos = actual_pos
			end
		end
		B738DR_irs_left_mode = irs_left_mode
	
	else
	
		--------------------------------------
		-- IRS 2
		--------------------------------------
		
		local irs_rgt = B738DR_irs_right
		if irs_rgt == 0 then
			-- stop align
			if is_timer_scheduled(B738_irs_right_align) == true then
				stop_timer(B738_irs_right_align)
			end
			if is_timer_scheduled(B738_irs_test_right2) == true then
				stop_timer(B738_irs_test_right2)
			end
			-- start shutdown cycle
			if is_timer_scheduled(B738_irs_right_off) == false then
				run_after_time(B738_irs_right_off, 20)	-- 30 seconds shutdown cycle
			end
			if is_timer_scheduled(B738_entry_right) == true then	 -- waiting for entry
				stop_timer(B738_entry_right)
			end
			-- if irs_right_mode < 2 then
				-- irs_right_status = 0		-- irs left off
			-- end
			B738DR_irs_on_dc_right = 0
			alignment_right_entry = 0
			irs_right_mode = 0
			irs_right_restart = 0
			irs_right_fail = 0
			B738DR_irs2_pos = "-----.-------.-"
			alignment_right_status = 0
		else
			if is_timer_scheduled(B738_irs_right_off) == true then
				stop_timer(B738_irs_right_off)
			end
			if alignment_right_enable == 1 then		-- run alignment
				alignment_right_enable = 0
				irs_right_mode = 1	-- align mode
				irs_right_restart = 0
				B738_run_irs_right()
			end
			irs_right_status = 1		-- irs right on
			if irs_rgt == 3 then
				-- att mode
				if is_timer_scheduled(B738_entry_right) == false then	 -- delay for ATT mode
					run_after_time(B738_entry_right, 3)
				end
				if alignment_right_entry == 1 then
					B738DR_alignment_right_remain = -1		-- NAV align off
					if is_timer_scheduled(B738_irs_right_align) == true then
						stop_timer(B738_irs_right_align)
					end
					if irs2_hdg_set ~= "---" and irs_right_mode ~= 3 then
						local test_x = simDR_true_heading
						test_x = math.floor(test_x + 0.5)
						local test_y = tonumber(irs2_hdg_set) - test_x
						if test_y < 0 then
							test_y = -test_y
						end
						if irs_right_fail == 0 and test_y < 2 then
							irs_right_mode = 3	-- att mode
							alignment_right_status = 1
						else
							irs_right_fail = 1
							irs_right_mode = 4
							alignment_right_status = 0
						end
					end
				end
			else
				if is_timer_scheduled(B738_entry_right) == true then
					stop_timer(B738_entry_right)
				end
			end
			if B738DR_alignment_right_remain == 0 then
				if is_timer_scheduled(B738_irs_right_align) == true then
					stop_timer(B738_irs_right_align)
				end
				-- if irs_rgt == 1 and irs_right_mode ~= 2 and irs_right_mode ~= 4 then
				if irs_rgt == 1 and irs_right_mode ~= 4 then
					-- align mode
					irs_right_mode = 1	-- align mode
					-- alignment_right_entry = 0
					if B738DR_irs_was_full_allign2 ~= 0 then
						if alignment_right_status == 0 then
							-- flashing, waiting for entry
							if B738DR_irs2_pos_set ~= "*****.*******.*" then
								align_dist_ok = 0
								align_dist = dist_to_latlon(B738DR_irs_pos_set)
								if align_dist < 4 then	-- to 4NM
									align_dist_ok = 1
								end
								if align_dist_ok == 1 then
									if is_timer_scheduled(align_accept_right) == false then
										run_after_time(align_accept_right, 2)
									end
								else
									-- IRS fail
									irs_right_fail = 1
									irs_right_mode = 4
									alignment_right_status = 0
									alignment_right_entry = 0
								end
							end
						else
							-- alignment complete
							B738DR_irs2_pos = actual_pos
							alignment_right_status = 1
						end
					end
					
				elseif irs_rgt == 2 and irs_right_mode ~= 4 then
					-- nav mode
					if irs_left_mode == 2 then
						irs_right_mode = 2	-- nav mode
						B738DR_irs_pos = actual_pos
						alignment_right_status = 1
						alignment_right_entry = 1
					end
					if alignment_right_status == 0 then
						-- flashing, waiting for entry
							if B738DR_irs2_pos_set ~= "*****.*******.*" then
							align_dist_ok = 0
							align_dist = dist_to_latlon(B738DR_irs_pos_set)
							if align_dist < 4 then	-- to 4NM
								align_dist_ok = 1
							end
							if align_dist_ok == 1 then
								if is_timer_scheduled(align_accept_right) == false then
									run_after_time(align_accept_right, 2)
								end
							else
								-- IRS fail
								irs_right_fail = 1
								irs_right_mode = 4
								alignment_right_status = 0
								alignment_right_entry = 0
							end
						end
					else
						-- alignment complete
						irs_right_mode = 2	-- nav mode
						B738DR_irs2_pos = actual_pos
						alignment_right_status = 1
					end
				end
			end
--			if alignment_right_status == 1 then
			if alignment_right_status == 1 and irs_right_mode == 2 then
				B738DR_irs2_pos = actual_pos
			end
		end
		-- B738DR_irs_right_mode = irs_right_mode
	end
	
	delta_lat = align_lat - simDR_latitude
	if delta_lat < 0 then
		delta_lat = -delta_lat
	end
	delta_lon = align_lon - simDR_longitude
	if delta_lon < 0 then
		delta_lon = -delta_lon
	end
	if delta_lat > 0.000009 and delta_lon > 0.000009 then
		aircraft_move = 1
		align_lat = simDR_latitude
		align_lon = simDR_longitude
	else
		aircraft_move = 0
	end
	
	if B738DR_alignment_left_remain > 0 and aircraft_move == 1 
	and irs_left_restart == 0 then
		irs_left_restart = 1
		B738DR_irs1_restart = 1
		if is_timer_scheduled(B738_irs_left_align) == true then
			stop_timer(B738_irs_left_align)
		end
	end
	
	if irs_left_restart == 1 and aircraft_move == 0 
	and B738DR_alignment_left_remain > 0 then
		irs_left_restart = 0
		B738_run_irs_left()
	end
	
	if B738DR_alignment_right_remain > 0 and aircraft_move == 1 
	and irs_right_restart == 0 then
		irs_right_restart = 1
		B738DR_irs2_restart = 1
		if is_timer_scheduled(B738_irs_right_align) == true then
			stop_timer(B738_irs_right_align)
		end
	end
	
	if irs_right_restart == 1 and aircraft_move == 0 
	and B738DR_alignment_right_remain > 0 then
		irs_right_restart = 0
		B738_run_irs_right()
	end
	
end

function B738_irs_annun(in_side)

	local irs_lft = B738DR_irs_left
	local irs_rgt = B738DR_irs_right
	
	if in_side == 0 then
		if irs_lft == 0 then
			irs_align_fail_left = 0		-- on, if fault entry only
			if irs_left_status == 0 then
				B738DR_irs_align_left = 0
				irs_align_left = 0
			else
				B738DR_irs_align_left = 1 * B738DR_brightness_level	-- shutdown cycle
				irs_align_left = 0
			end
		else
			if irs_dspl_test_run == 1 then
				irs_align_fail_left = 1
				B738DR_irs_align_left = 1 * B738DR_brightness_level
				irs_dc_fail_left = 1
				B738DR_irs_on_dc_left = 1 * B738DR_brightness_level
				irs_align_left = 0
			else
				if irs_left_fail == 1 then
					irs_align_fail_left = 1
					B738DR_irs_align_left = 0
					irs_align_left = 0
				elseif irs_left_mode == 1 then		-- mode ALIGN
					if irs_left_test == 1 then
						B738DR_irs_align_left = 0
						irs_align_left = 0
					elseif irs_lft == 3 then
						-- flashing
						B738DR_irs_align_left = irs_flashing * B738DR_brightness_level
						irs_align_left = 1
					elseif irs_lft == 2 and B738DR_alignment_left_remain == 0 then
						-- flashing
						B738DR_irs_align_left = irs_flashing * B738DR_brightness_level
						irs_align_left = 1
					else
						B738DR_irs_align_left = 1 * B738DR_brightness_level
						irs_align_left = 1
					end
				elseif irs_left_mode == 4 then
					-- fault
					irs_align_fail_left = 1
					irs_align_left = 0
				else	-- mode NAV and ATT
					if irs_lft == 1 then
						-- fast realign
						B738DR_irs_align_left = 1 * B738DR_brightness_level
						irs_align_left = 1
					else
						B738DR_irs_align_left = 0
						irs_align_left = 0
					end
				end
			end
		end
	
	else
	
		----------------------------------------
		-- IRS right
		----------------------------------------
		if irs_enable2 == 1 then
			if irs_rgt == 0 then
				irs_align_fail_right = 0		-- on, if fault entry only
				if irs_right_status == 0 then
					B738DR_irs_align_right = 0
				else
					B738DR_irs_align_right = 1 * B738DR_brightness_level	-- shutdown cycle
				end
				irs_align_right = 0
			else
				if irs_dspl_test_run == 1 then
					irs_align_fail_right = 1
					B738DR_irs_align_right = 1 * B738DR_brightness_level
					irs_dc_fail_right = 1
					B738DR_irs_on_dc_right = 1 * B738DR_brightness_level
					irs_align_right = 0
				else
					if irs_right_fail == 1 then
						irs_align_fail_right = 1
						B738DR_irs_align_right = 0
						irs_align_right = 0
					elseif irs_right_mode == 1 then		-- mode ALIGN
						if irs_right_test == 1 then
							B738DR_irs_align_right = 0
							irs_align_right = 0
						elseif irs_rgt == 3 then
							B738DR_irs_align_right = irs_flashing * B738DR_brightness_level
							irs_align_right = 1
						elseif irs_rgt == 2 and B738DR_alignment_right_remain == 0 then
							-- flashing
							B738DR_irs_align_right = irs_flashing * B738DR_brightness_level
							irs_align_right = 1
						else
							B738DR_irs_align_right = 1 * B738DR_brightness_level
							irs_align_right = 1
						end
					elseif irs_right_mode == 4 then
						-- fault
						irs_align_fail_right = 1
						irs_align_right = 0
					else	-- mode NAV and ATT
						if irs_rgt == 1 then
							-- fast realign
							B738DR_irs_align_right = 1 * B738DR_brightness_level
							irs_align_right = 1
						else
							B738DR_irs_align_right = 0
							irs_align_right = 0
						end
					end
				end
			end
		end
	end
end


function B738_irs_source(in_mode)

	local irs_lft = B738DR_irs_left
	local irs_rgt = B738DR_irs_right
	local irs_source = B738DR_irs_source
	
	if in_mode == 0 then
		if irs_source == -1 then		-- both L
			if irs_left_mode == 2 or irs_left_mode == 3 then
				B738DR_irs_left_fail = 0
			else
				B738DR_irs_left_fail = 1 * B738DR_brightness_level
			end
		elseif irs_source == 0 then		-- normal
			if irs_left_mode == 2 or irs_left_mode == 3 then
				B738DR_irs_left_fail = 0
			else
				B738DR_irs_left_fail = 1 * B738DR_brightness_level
			end
		elseif irs_source == 1 then		-- both R
			if irs_right_mode == 2 or irs_right_mode == 3 then
				B738DR_irs_left_fail = 0
			else
				B738DR_irs_left_fail = 1 * B738DR_brightness_level
			end
		end
	else
		if irs_source == -1 then		-- both L
			if irs_left_mode == 2 or irs_left_mode == 3 then
				B738DR_irs_right_fail = 0
			else
				B738DR_irs_right_fail = 1 * B738DR_brightness_level
			end
		elseif irs_source == 0 then		-- normal
			if irs_right_mode == 2 or irs_right_mode == 3 then
				B738DR_irs_right_fail = 0
			else
				B738DR_irs_right_fail = 1
			end
		elseif irs_source == 1 then		-- both R
			if irs_right_mode == 2 or irs_right_mode == 3 then
				B738DR_irs_right_fail = 0
			else
				B738DR_irs_right_fail = 1 * B738DR_brightness_level
			end
		end
	end

end

function B738_irs_electric()

	-- if flight_loaded == 0 then
	if first_time >= 2 then
		-- IRS working if BUS 1 or BUS 2 > 10 Volts
		
		if B738DR_batbus_status == 0 then
			irs_enable = 0
			B738DR_irs_align_left = 0
			irs_align_fail_left = 0
			B738DR_irs_on_dc_left = 0
			irs_align_left = 0
		else
			if irs_enable == 0 then
				if B738DR_ac_stdbus_status ~= 0 then
					irs_enable = 1
					if B738DR_irs_pos_set == "*****.*******.*" and B738DR_irs_pos_fmc_set ~= "" then
						B738DR_irs_pos_set = B738DR_irs_pos_fmc_set
					end
					if B738DR_irs_left ~= 0 then
						alignment_left_enable = 1		-- start alignment
					end
				end
			end
		end
		
		if B738DR_batbus_status == 0 then
			irs_enable2 = 0
			B738DR_irs_align_right = 0
			irs_align_fail_right = 0
			irs_align_right = 0
			
			B738DR_irs_on_dc_right = 0
			
			irs_dc_timer_off = 0
		else
			if irs_enable2 == 0 then
				if B738DR_ac_tnsbus2_status ~= 0 then
					irs_enable2 = 1
					irs_dc_timer_off = 0
					if B738DR_irs2_pos_set == "*****.*******.*" and B738DR_irs_pos_fmc_set ~= "" then
						B738DR_irs2_pos_set = B738DR_irs_pos_fmc_set
					end
					if B738DR_irs_right ~= 0 then
						alignment_right_enable = 1		-- start alignment
					end
				end
			end
		end
		
		if irs_dc_timer_off ~= 0 then
			irs_dc_timer_off = irs_dc_timer_off - SIM_PERIOD
			if irs_dc_timer_off <= 0 then
				irs_dc_timer_off = 0
				irs_enable2 = 0
			end
		end
		
		if irs_enable2 ~= 0 then
			if B738DR_ac_tnsbus2_status == 0 then
				if irs_dc_timer_off == 0 then
					-- timer 5 mins to OFF
					irs_dc_timer_off = 300
				end
			else
				irs_dc_timer_off = 0
			end
		end
	end
	
	if irs_enable == 0 and irs_enable2 == 0 then
		set_disable_key(0, 9)
		irs_entry_reset = 0
		irs_entry_mod = 0
		irs_ent_light = 0
		irs_clr_light = 0
	end
	
	if (B738DR_irs_left == 0 or irs_enable == 0) and (B738DR_irs_right == 0 or irs_enable2 == 0) then
		set_disable_key(0, 9)
		irs_entry_mod = 0
		irs_entry_pos_lat = ""
		irs_entry_pos_lon = ""
		irs_entry_hdg = ""
		irs_entry_reset = 0
		irs_ent_light = 0
		irs_clr_light = 0
	else
		if irs_entry_mod == 0 and irs_entry_reset == 0 then
			--clear_disable_key()
			set_disable_key(0, 9)
			disable_key[2] = 0
			disable_key[5] = 0
			disable_key[8] = 0
			irs_entry_reset = 1
			irs_entry_mod = 0
			irs_entry_pos_lat = ""
			irs_entry_pos_lon = ""
			irs_entry_hdg = ""
			irs_ent_light = 0
			irs_clr_light = 0
		end
	end
	
	B738DR_irs_ent_light = irs_ent_light
	B738DR_irs_clr_light = irs_clr_light
	
	if irs_enable ~= 0 or irs_enable2 ~= 0 then
		if irs_dspl_test_run == 1 or (B738DR_lights_test == 1 and simDR_radio_height_pilot_ft < 50) then
			if irs_left_status == 1
			or irs_right_status == 1 then
				B738DR_irs_ent_light = 1 * B738DR_brightness_level
				B738DR_irs_clr_light = 1 * B738DR_brightness_level
			end
		end
	end
	
end

function B738_irs_flash()

	if irs_flashing == 0 then
		irs_flashing = 1
	else
		irs_flashing = 0
	end

end


function B738_gps_pos()

	local position_min = ""
	local position_deg = ""
	local position = ""
  local pos_deg = ""
  local pos_min = ""

	pos_deg = string.format("%02d", B738DR_latitude_deg)
	pos_min = string.format("%05.1f", B738DR_latitude_min)
	if B738DR_latitude_NS == 1 then
		position = ("S" .. pos_deg)
	else
		position = ("N" .. pos_deg)
	end
	position = position .. string.sub(pos_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(pos_min, 3, 3)
	pos_deg = string.format("%03d", B738DR_longitude_deg)
	pos_min = string.format("%05.1f", B738DR_longitude_min)
	if B738DR_longitude_EW == 1 then
		position = (position .. "W")
	else
		position = (position .. "E")
	end
	position = position .. pos_deg
	position = position .. string.sub(pos_min, 1, 2)
	position = position .. "."
	position = position .. string.sub(pos_min, 3, 3)

	actual_pos = position
	
	if simDR_gps_fail == 0 then
		B738DR_gps_pos = position
	else
		B738DR_gps_pos = "-----.-------.-"
	end

	if simDR_gps2_fail == 0 then
		B738DR_gps2_pos = position
	else
		B738DR_gps2_pos = "-----.-------.-"
	end

end

function pfd_nd()
	
	local irs_nd_allign_gs = 0
	local irs_pfd_allign = 0
	local irs_pfd_allign_hdg = 0
	local irs_nd_allign = 0
	local fms_override = 0
	
	local irs_nd_allign_gs2 = 0
	local irs_pfd_allign2 = 0
	local irs_pfd_allign_hdg2 = 0
	local irs_nd_allign2 = 0
	local fms_override2 = 0
	
	if B738DR_irs_left_mode == 2 then
		-- NAV mode
		if B738DR_irs_left_fail ~= 0 then
			irs_pfd_allign = 1
			irs_pfd_allign_hdg = 1
			irs_nd_allign = 1
			irs_nd_allign_gs = 1
			fms_override = 1
		else
			irs_pfd_allign = 0
			irs_pfd_allign_hdg = 0
			irs_nd_allign = 0
			fms_override = 0
		end
	elseif B738DR_irs_left_mode == 3 then
		-- ATT mode
		if B738DR_irs_left_fail ~= 0 then
			irs_pfd_allign = 1
			irs_pfd_allign_hdg = 1
			irs_nd_allign = 1
			irs_nd_allign_gs = 1
			fms_override = 1
		else
			irs_pfd_allign = 0
			irs_pfd_allign_hdg = 0
			irs_nd_allign = 0	--3
			fms_override = 0
		end
		B738DR_irs_pos ="-----.-------.-"
	else
		if B738DR_irs_left_mode == 0 then
			irs_pfd_allign = 1
			irs_pfd_allign_hdg = 1
			irs_nd_allign = 1
			irs_nd_allign_gs = 1
			B738DR_irs_pos_set = "*****.*******.*"
			irs_hdg_set = "---"
		else
			if B738DR_irs_pos_set == "*****.*******.*" and B738DR_irs2_pos_set ~= "*****.*******.*" then
				B738DR_irs_pos_set = B738DR_irs2_pos_set
			end
			local align_running = 0
			if B738DR_irs_left_mode > 0 and irs_left_test ~= 1 then
				align_running = 1
			end
			if align_running == 0 then
				-- B738DR_irs_pfd_allign = 1
				irs_pfd_allign = 1	--2
				irs_pfd_allign_hdg = 1
				irs_nd_allign = 1
				irs_nd_allign_gs = 1
			else
				-- B738DR_irs_pfd_allign = 2
				irs_pfd_allign = 3
				irs_pfd_allign_hdg = 2
				irs_nd_allign = 2
				if B738DR_irs_pos_set == "*****.*******.*" then
					irs_nd_allign_gs = 2
				end
			end
		end
		fms_override = 1
	end
	
	if B738DR_irs_right_mode == 2 then
		-- NAV mode
		if B738DR_irs_right_fail ~= 0 then
			irs_pfd_allign2 = 1
			irs_pfd_allign_hdg2 = 1
			irs_nd_allign2 = 1
			irs_nd_allign_gs2 = 1
			fms_override2 = 1
		else
			irs_pfd_allign2 = 0
			irs_pfd_allign_hdg2 = 0
			irs_nd_allign2 = 0
			fms_override2 = 0
		end
	elseif B738DR_irs_right_mode == 3 then
		-- ATT mode
		if B738DR_irs_right_fail ~= 0 then
			irs_pfd_allign2 = 1
			irs_pfd_allign_hdg2 = 1
			irs_nd_allign2 = 1
			irs_nd_allign_gs2 = 1
			fms_override2 = 1
		else
			irs_pfd_allign2 = 0
			irs_pfd_allign_hdg2 = 0
			irs_nd_allign2 = 0	--3
			fms_override2 = 0
		end
		B738DR_irs2_pos = "-----.-------.-"
	else
		if B738DR_irs_right_mode == 0 then
			irs_pfd_allign2 = 1
			irs_pfd_allign_hdg2 = 1
			irs_nd_allign2 = 1
			irs_nd_allign_gs2 = 1
			B738DR_irs2_pos_set = "*****.*******.*"
			irs2_hdg_set = "---"
		else
			if B738DR_irs2_pos_set == "*****.*******.*" and B738DR_irs_pos_set ~= "*****.*******.*" then
				B738DR_irs2_pos_set = B738DR_irs_pos_set
			end
			local align_running = 0
			if B738DR_irs_right_mode > 0 and irs_right_test ~= 1 then
				align_running = 1
			end
			if align_running == 0 then
				irs_pfd_allign2 = 1	--2
				irs_pfd_allign_hdg2 = 1
				irs_nd_allign2 = 1
				irs_nd_allign_gs2 = 1
			else
				irs_pfd_allign2 = 3
				irs_pfd_allign_hdg2 = 2
				irs_nd_allign2 = 2
				if B738DR_irs2_pos_set == "*****.*******.*" then
					irs_nd_allign_gs2 = 2
				end
			end
		end
		fms_override2 = 1
	end
	
	if B738DR_irs_source == 0 then
		-- IRS 1/2
		B738DR_irs_pfd_allign = irs_pfd_allign
		B738DR_irs_pfd_allign_hdg = irs_pfd_allign_hdg
		B738DR_irs_nd_allign = irs_nd_allign
		B738DR_irs_nd_allign_gs = irs_nd_allign_gs
		
		B738DR_irs_pfd_allign2 = irs_pfd_allign2
		B738DR_irs_pfd_allign_hdg2 = irs_pfd_allign_hdg2
		B738DR_irs_nd_allign2 = irs_nd_allign2
		B738DR_irs_nd_allign_gs2 = irs_nd_allign_gs2
		
		if fms_override == 0 and fms_override2 == 0 then
			DRsim_fms_override = 0
		else
			DRsim_fms_override = 1
		end
	elseif B738DR_irs_source == -1 then
		-- IRS 1
		B738DR_irs_pfd_allign = irs_pfd_allign
		B738DR_irs_pfd_allign_hdg = irs_pfd_allign_hdg
		B738DR_irs_nd_allign = irs_nd_allign
		B738DR_irs_nd_allign_gs = irs_nd_allign_gs
		
		B738DR_irs_pfd_allign2 = irs_pfd_allign
		B738DR_irs_pfd_allign_hdg2 = irs_pfd_allign_hdg
		B738DR_irs_nd_allign2 = irs_nd_allign
		B738DR_irs_nd_allign_gs2 = irs_nd_allign_gs
		
		DRsim_fms_override = fms_override
	else
		-- IRS 2
		B738DR_irs_pfd_allign = irs_pfd_allign2
		B738DR_irs_pfd_allign_hdg = irs_pfd_allign_hdg2
		B738DR_irs_nd_allign = irs_nd_allign2
		B738DR_irs_nd_allign_gs = irs_nd_allign_gs2
		
		B738DR_irs_pfd_allign2 = irs_pfd_allign2
		B738DR_irs_pfd_allign_hdg2 = irs_pfd_allign_hdg2
		B738DR_irs_nd_allign2 = irs_nd_allign2
		B738DR_irs_nd_allign_gs2 = irs_nd_allign_gs2
		
		DRsim_fms_override = fms_override2
	end
	
	if irs_pfd_allign == 0 and irs_pfd_allign2 == 0 then
		B738DR_irs_nd_allign_12 = 0
	elseif irs_pfd_allign == 2 and irs_pfd_allign2 == 2 then
		B738DR_irs_nd_allign_12 = 2
	else
		B738DR_irs_nd_allign_12 = 1
	end
	
	if B738DR_irs_left_mode > 0 or B738DR_irs_right_mode > 0 then
		B738DR_irs_running = 1
	else
		B738DR_irs_running = 0
	end
	
	
	
end

function turn_around_state()
	if first_time < 2 then
		if flight_loaded ~= 0 then
			if irs_left_mode == 2 then
				B738DR_irs_left = 2
				irs_left_mode = 2
				irs_left_status = 1
				simDR_gps_fail = 0
				B738DR_alignment_left_remain = 0
				alignment_left_entry = 0
				latitude()
				longitude()
				B738_gps_pos()
				B738DR_irs_pos = B738DR_gps_pos
				alignment_left_status = 1
			end
			if irs_right_mode == 2 then
				B738DR_irs_right = 2
				irs_right_mode = 2
				irs_right_status = 1
				simDR_gps2_fail = 0
				B738DR_alignment_right_remain = 0
				alignment_right_entry = 0
				latitude()
				longitude()
				B738_gps_pos()
				B738DR_irs2_pos = B738DR_gps2_pos
				alignment_right_status = 1
			end
			B738DR_irs_left_mode = irs_left_mode
			B738DR_irs_right_mode = irs_right_mode
		elseif (simDR_startup_running == 0 and B738DR_engine_no_running_state == 1) or simDR_startup_running == 1 then
			-- IRS alignment
			B738DR_irs_left = 2
			B738DR_irs_right = 2
			irs_left_mode = 2
			irs_right_mode = 2
			irs_left_status = 1
			irs_right_status = 1
			simDR_gps_fail = 0
			simDR_gps2_fail = 0
			B738DR_alignment_left_remain = 0
			B738DR_alignment_right_remain = 0
			alignment_right_entry = 0
			alignment_left_entry = 0
			latitude()
			longitude()
			B738_gps_pos()
			B738DR_irs_pos = B738DR_gps_pos
			B738DR_irs2_pos = B738DR_gps2_pos
			alignment_left_status = 1
			alignment_right_status = 1
		end
		
		if irs_first_time ~= 0 then
			if SIM_PERIOD < 1 then
				first_time = first_time + SIM_PERIOD
			end
		else
			irs_first_time = 1
		end
	else
		flight_loaded = 0
	end
	B738DR_irs_timer = first_time
end

function B738_set_state()
	
	local jj = 0
	local state_cmd = B738DR_state_cmd
	local state_idx = 132	--131	-- to 135
	
	if string.len(state_cmd) >= 131 then
		for jj = 131, string.len(state_cmd) do
			if jj == state_idx then		--131
				-- IRS source
				if string.sub(state_cmd, jj, jj) == "0" then
					B738DR_irs_source = -1
				elseif string.sub(state_cmd, jj, jj) == "1" then
					B738DR_irs_source = 0
				else
					B738DR_irs_source = 1
				end
			elseif jj == state_idx + 1 then		--132
				-- IRS Left
				if string.sub(state_cmd, jj, jj) == "0" then
					B738DR_irs_left = 0
				elseif string.sub(state_cmd, jj, jj) == "1" then
					B738DR_irs_left = 1
				elseif string.sub(state_cmd, jj, jj) == "2" then
					B738DR_irs_left = 2
				else
					B738DR_irs_left = 3
				end
			elseif jj == state_idx + 2 then		--133
				-- IRS Right
				if string.sub(state_cmd, jj, jj) == "0" then
					B738DR_irs_right = 0
				elseif string.sub(state_cmd, jj, jj) == "1" then
					B738DR_irs_right = 1
				elseif string.sub(state_cmd, jj, jj) == "2" then
					B738DR_irs_right = 2
				else
					B738DR_irs_right = 3
				end
			elseif jj == state_idx + 3 then		--134
				-- IRS Sys diplay
				if string.sub(state_cmd, jj, jj) == "0" then
					B738DR_irs_sys_dspl = 0
				else
					B738DR_irs_sys_dspl = 1
				end
			elseif jj == state_idx + 4 then		--135
				-- IRS Right
				if string.sub(state_cmd, jj, jj) == "0" then
					B738DR_irs_dspl_sel = 0
				elseif string.sub(state_cmd, jj, jj) == "1" then
					B738DR_irs_dspl_sel = 1
				elseif string.sub(state_cmd, jj, jj) == "2" then
					B738DR_irs_dspl_sel = 2
				elseif string.sub(state_cmd, jj, jj) == "3" then
					B738DR_irs_dspl_sel = 3
				else
					B738DR_irs_dspl_sel = 4
				end
			end
		end
	end
end

function B738_create_state()
	
	local state_cmd = B738DR_state_cmd
	
	-- IRS source
	if B738DR_irs_source == -1 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_irs_source == 0 then
		state_cmd = state_cmd .. "1"
	else
		state_cmd = state_cmd .. "2"
	end
	-- IRS Left
	if B738DR_irs_left == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_irs_left == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_irs_left == 2 then
		state_cmd = state_cmd .. "2"
	else
		state_cmd = state_cmd .. "3"
	end
	-- IRS Right
	if B738DR_irs_right == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_irs_right == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_irs_right == 2 then
		state_cmd = state_cmd .. "2"
	else
		state_cmd = state_cmd .. "3"
	end
	-- IRS Sys display
	if B738DR_irs_sys_dspl == 0 then
		state_cmd = state_cmd .. "0"
	else
		state_cmd = state_cmd .. "1"
	end
	-- IRS Displays select
	if B738DR_irs_dspl_sel == 0 then
		state_cmd = state_cmd .. "0"
	elseif B738DR_irs_dspl_sel == 1 then
		state_cmd = state_cmd .. "1"
	elseif B738DR_irs_dspl_sel == 2 then
		state_cmd = state_cmd .. "2"
	elseif B738DR_irs_dspl_sel == 3 then
		state_cmd = state_cmd .. "3"
	else
		state_cmd = state_cmd .. "4"
	end
	
	B738DR_state_cmd = state_cmd
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, 50 do
			glare[ggg] = B738DR_latlon[ggg-1]
			glare_sw[ggg] = B738DR_latlon_sw[ggg-1]
		end
		
		irs_dspl_test_enable = glare[1]
		irs_dspl_test_run = glare[2]
		alignment_left_enable = glare[3]
		alignment_right_enable = glare[4]
		alignment_left_status = glare[5]
		alignment_right_status = glare[6]
		irs_left_mode = glare[7]
		irs_right_mode = glare[8]
		B738DR_irs_left_mode = irs_left_mode
		B738DR_irs_right_mode = irs_right_mode
		irs_left_status = glare[9]
		irs_right_status = glare[10]
		irs_enable = glare[11]
		irs_enable2 = glare[12]
		irs_flashing = glare[13]
		irs_time = glare[14]
		alignment_right_entry = glare[15]
		alignment_left_entry = glare[16]
		irs_left_restart = glare[17]
		irs_right_restart = glare[18]
		irs_left_fail = glare[19]
		irs_right_fail = glare[20]
		irs_left_test = glare[21]
		irs_right_test = glare[22]
		align_lat = glare[23]
		align_lon = glare[24]
		irs_align_fail_left = glare[25]
		irs_align_fail_right = glare[26]
		irs_dc_fail_left = glare[27]
		irs_dc_fail_right = glare[28]
		irs_entry_mod = glare[29]
		irs_entry_reset = glare[30]
		disable_key[0] = glare[31]
		disable_key[1] = glare[32]
		disable_key[2] = glare[33]
		disable_key[3] = glare[34]
		disable_key[4] = glare[35]
		disable_key[5] = glare[36]
		disable_key[6] = glare[37]
		disable_key[7] = glare[38]
		disable_key[8] = glare[39]
		disable_key[9] = glare[40]
		irs_ent_light = glare[41]
		irs_clr_light = glare[42]
		
		--SW
		B738DR_irs_source = glare_sw[1]
		B738DR_irs_left = glare_sw[2]
		B738DR_irs_right = glare_sw[3]
		B738DR_irs_sys_dspl = glare_sw[4]
		B738DR_irs_dspl_sel = glare_sw[5]
		B738DR_irs_was_full_allign = glare[43]
		B738DR_irs_was_full_allign2 = glare[44]
		
		if B738DR_irs_was_full_allign ~= 0 or B738DR_irs_was_full_allign2 ~= 0 then
			latitude()
			longitude()
			B738_gps_pos()
		end
		if B738DR_irs_was_full_allign ~= 0 then
			if B738DR_gps_pos == "" then
				B738DR_irs_pos ="-----.-------.-"
				B738DR_irs_pos_set = "*****.*******.*"
			else
				B738DR_irs_pos = B738DR_gps_pos
				B738DR_irs_pos_set = B738DR_irs_pos
			end
		else
			B738DR_irs_pos ="-----.-------.-"
			B738DR_irs_pos_set = "*****.*******.*"
		end
		
		if B738DR_irs_was_full_allign2 ~= 0 then
			if B738DR_gps_pos == "" then
				B738DR_irs2_pos = "-----.-------.-"
				B738DR_irs2_pos_set = "*****.*******.*"
			else
				B738DR_irs2_pos = B738DR_gps2_pos
				B738DR_irs2_pos_set = B738DR_irs2_pos
			end
		else
			B738DR_irs2_pos = "-----.-------.-"
			B738DR_irs2_pos_set = "*****.*******.*"
		end
		
		
		flight_loaded = 1
		irs_first_time = 0
		first_time = 0
	elseif in_mode ~= 0 then
		for ggg = 1, 50 do
			glare[ggg] = 0
			glare_sw[ggg] = 0
		end
		
		glare[1] = irs_dspl_test_enable
		glare[2] = irs_dspl_test_run
		glare[3] = alignment_left_enable
		glare[4] = alignment_right_enable
		glare[5] = alignment_left_status
		glare[6] = alignment_right_status
		glare[7] = irs_left_mode
		glare[8] = irs_right_mode
		glare[9] = irs_left_status
		glare[10] = irs_right_status
		glare[11] = irs_enable
		glare[12] = irs_enable2
		glare[13] = irs_flashing
		glare[14] = irs_time
		glare[15] = alignment_right_entry
		glare[16] = alignment_left_entry
		glare[17] = irs_left_restart
		glare[18] = irs_right_restart
		glare[19] = irs_left_fail
		glare[20] = irs_right_fail
		glare[21] = irs_left_test
		glare[22] = irs_right_test
		glare[23] = align_lat
		glare[24] = align_lon
		glare[25] = irs_align_fail_left
		glare[26] = irs_align_fail_right
		glare[27] = irs_dc_fail_left
		glare[28] = irs_dc_fail_right
		glare[29] = irs_entry_mod
		glare[30] = irs_entry_reset
		glare[31] = disable_key[0]
		glare[32] = disable_key[1]
		glare[33] = disable_key[2]
		glare[34] = disable_key[3]
		glare[35] = disable_key[4]
		glare[36] = disable_key[5]
		glare[37] = disable_key[6]
		glare[38] = disable_key[7]
		glare[39] = disable_key[8]
		glare[40] = disable_key[9]
		glare[41] = irs_ent_light
		glare[42] = irs_clr_light
		glare[43] = B738DR_irs_was_full_allign
		glare[44] = B738DR_irs_was_full_allign2
		
		--SW
		glare_sw[1] = B738DR_irs_source
		glare_sw[2] = B738DR_irs_left
		glare_sw[3] = B738DR_irs_right
		glare_sw[4] = B738DR_irs_sys_dspl
		glare_sw[5] = B738DR_irs_dspl_sel
		
		for ggg = 1, 50 do
			B738DR_latlon[ggg-1] = glare[ggg]
			if ggg <= 10 then
				B738DR_latlon_sw[ggg-1] = glare_sw[ggg]
			end
		end
	end
	
end

--*************************************************************************************--
--** 				               XLUA EVENT CALLBACKS       	        			 **--
--*************************************************************************************--

--function aircraft_load() end

--function aircraft_unload() end

function flight_start() 

	if simDR_startup_running == 0 then
		B738DR_irs_left = 0
		B738DR_irs_right = 0
		irs_left_mode = 0
		irs_right_mode = 0
		irs_left_status = 0
		irs_right_status = 0
		B738DR_alignment_left_remain = -1
		B738DR_alignment_right_remain = -1
		alignment_left_status = 0
		alignment_right_status = 0
		alignment_right_entry = 0
		alignment_left_entry = 0
		B738DR_irs_pos ="-----.-------.-"
		B738DR_irs2_pos = "-----.-------.-"
		B738DR_irs_pos_set = "*****.*******.*"
		B738DR_irs2_pos_set = "*****.*******.*"
		B738DR_irs_was_full_allign = 0
		B738DR_irs_was_full_allign2 = 0
		irs_align_left = 0
		irs_align_right = 0
	else
		-- Engine runnning
		B738DR_irs_left = 2
		B738DR_irs_right = 2
		irs_left_mode = 2
		irs_right_mode = 2
		irs_left_status = 1
		irs_right_status = 1
		simDR_gps_fail = 0
		simDR_gps2_fail = 0
		B738DR_alignment_left_remain = 0
		B738DR_alignment_right_remain = 0
		alignment_right_entry = 0
		alignment_left_entry = 0
		latitude()
		longitude()
		B738_gps_pos()
		B738DR_irs_pos = B738DR_gps_pos
		B738DR_irs2_pos = B738DR_gps2_pos
		alignment_left_status = 1
		alignment_right_status = 1
		B738DR_irs_pos_set = B738DR_irs_pos
		B738DR_irs2_pos_set = B738DR_irs2_pos
		B738DR_irs_was_full_allign = 1
		B738DR_irs_was_full_allign2 = 1
		irs_align_left = 1
		irs_align_right = 1
	end
	
	-- Always
	B738DR_irs_dspl_sel = 2
	B738DR_irs_dspl_sel_brt = 1
	
	irs_dspl_test_enable = 0
	irs_dspl_test_run = 0
	alignment_left_enable = 0
	alignment_right_enable = 0
	irs_enable = 1
	irs_enable2 = 1
	irs_left_restart = 0
	irs_right_restart = 0
	irs_left_fail = 0
	irs_right_fail = 0
	-- B738DR_irs_pos_set = "*****.*******.*"
	-- B738DR_irs2_pos_set = "*****.*******.*"
	irs_hdg_set = "---"
	irs2_hdg_set = "---"

	if is_timer_scheduled(B738_irs_flash) == false then
		run_at_interval(B738_irs_flash, 0.5)
	end
	
	clear_disable_key()
	irs_entry_reset = 0
	irs_entry_mod = 0
	irs_entry_pos_lat = ""
	irs_entry_pos_lon = ""
	irs_entry_hdg = ""
	B738DR_irs_ent_light = 0
	B738DR_irs_ent_light = 0
	irs_ent_light = 0
	irs_clr_light = 0
	irs_dc_timer_off = 0
	first_time = 0
	irs_first_time = 0
	flight_loaded = 0
	
	B738DR_irs_left_mode = irs_left_mode
	B738DR_irs_right_mode = irs_right_mode
	
	if irs_left_mode == 0 then
		irs_enable = 0
	else
		irs_enable = 1
	end
	if irs_right_mode == 0 then
		irs_enable2 = 0
	else
		irs_enable2 = 1
	end
end

--function flight_crash() end

-- function before_physics()

	-- if B738DR_kill_lat_lon == 0 then
		-- B738_irs_electric()
	-- end

-- end

function after_physics() 

	if B738DR_kill_lat_lon == 0 then
		if B738DR_state_cmd2 == 0 then
			if string.len(B738DR_state_cmd) ~= 0 then
				B738_set_state()
			end
		elseif B738DR_state_cmd2 == 9 then
			B738_create_state()
			B738DR_state_cmd2 = 10
		end
		load_save(0)
		B738_irs_electric()
		latitude()
		longitude()
		B738_gps_pos()
		
		if irs_enable == 1 then
			if irs_left_mode == 2 then
				if B738DR_irs_pos_set == "*****.*******.*" and B738DR_irs_pos ~= "-----.-------.-" then
					B738DR_irs_pos_set = B738DR_irs_pos
				end
			end
			B738_irs_system(0)
			if B738DR_irs_sys_dspl == 0 then
				B738_irs_display_data(0)
			end
			B738_irs_annun(0)
			B738_irs_source(0)
			B738DR_irs_status = irs_left_status
			-- DC lights
			if B738DR_irs_left > 0 then
				if B738DR_ac_stdbus_status == 0 then
					B738DR_irs_on_dc_left = 1 * B738DR_brightness_level
				else
					if is_timer_scheduled(B738_irs_test_left) == false then
						B738DR_irs_on_dc_left = 0
					end
				end
			end
		else
			B738DR_irs_status = 0
			
			irs_left_mode = 0
			irs_left_status = 0
			B738DR_alignment_left_remain = -1
			alignment_left_status = 0
			alignment_left_entry = 0
			B738DR_irs_pos ="-----.-------.-"
			B738DR_irs_was_full_allign = 0
			
			alignment_left_enable = 0
			-- irs_enable = 1
			irs_left_restart = 0
			irs_left_fail = 0
			B738DR_irs_pos_set = "*****.*******.*"
			irs_hdg_set = "---"
			
			if B738DR_irs_sys_dspl == 0 then
				irs_dspl_test_enable = 0
				irs_dspl_test_run = 0
				clear_disable_key()
				irs_entry_reset = 0
				irs_entry_mod = 0
				irs_entry_pos_lat = ""
				irs_entry_pos_lon = ""
				irs_entry_hdg = ""
				B738DR_irs_ent_light = 0
				B738DR_irs_clr_light = 0
				irs_ent_light = 0
				irs_clr_light = 0
			end
			
			if is_timer_scheduled(B738_irs_test_left) == false then
				B738DR_irs_on_dc_left = 0
			end
		end
		
		if irs_enable2 == 1 then
			if irs_right_mode == 2 then
				if B738DR_irs2_pos_set == "*****.*******.*" and B738DR_irs2_pos ~= "-----.-------.-" then
					B738DR_irs2_pos_set = B738DR_irs2_pos
				end
			end
			B738_irs_system(1)
			if B738DR_irs_sys_dspl ~= 0 then
				B738_irs_display_data(1)
			end
			B738_irs_annun(1)
			B738_irs_source(1)
			-- B738DR_irs2_status = irs_right_status
			-- DC lights
			if B738DR_irs_right > 0 then
				if B738DR_ac_tnsbus2_status == 0 then
					B738DR_irs_on_dc_right = 1 * B738DR_brightness_level
				else
					if is_timer_scheduled(B738_irs_test_right) == false then
						B738DR_irs_on_dc_right = 0
					end
				end
			end
		else
			B738DR_irs2_status = 0
			
			irs_right_mode = 0
			irs_right_status = 0
			B738DR_alignment_right_remain = -1
			alignment_right_status = 0
			alignment_right_entry = 0
			B738DR_irs2_pos = "-----.-------.-"
			B738DR_irs_was_full_allign2 = 0
			
			alignment_right_enable = 0
			-- irs_enable2 = 1
			irs_right_restart = 0
			irs_right_fail = 0
			B738DR_irs2_pos_set = "*****.*******.*"
			irs2_hdg_set = "---"
			
			if B738DR_irs_sys_dspl ~= 0 then
				irs_dspl_test_enable = 0
				irs_dspl_test_run = 0
				clear_disable_key()
				irs_entry_reset = 0
				irs_entry_mod = 0
				irs_entry_pos_lat = ""
				irs_entry_pos_lon = ""
				irs_entry_hdg = ""
				B738DR_irs_ent_light = 0
				B738DR_irs_clr_light = 0
				irs_ent_light = 0
				irs_clr_light = 0
			end
			
			if is_timer_scheduled(B738_irs_test_right) == false then
				B738DR_irs_on_dc_right = 0
			end
		end
		
		B738DR_irs_left_mode = irs_left_mode
		B738DR_irs_right_mode = irs_right_mode
		
		B738DR_irs_status = irs_left_status
		B738DR_irs2_status = irs_right_status
		
		local clr_irs_display = false
		if irs_enable == 0 and irs_enable2 == 0 then
			clr_irs_display = true
		elseif irs_enable == 1 and B738DR_irs_sys_dspl ~= 0 and irs_enable2 == 0 then
			clr_irs_display = true
		elseif irs_enable2 == 1 and B738DR_irs_sys_dspl == 0 and irs_enable == 0 then
			clr_irs_display = true
		end
		
		if clr_irs_display == true then
			B738DR_irs_left1 = 0
			B738DR_irs_left2 = 0
			B738DR_irs_right1 = 0
			B738DR_irs_right2 = 0
			B738DR_irs_left1_show = 0
			B738DR_irs_left2_show = 0
			B738DR_irs_right1_show = 0
			B738DR_irs_right2_show = 0
			B738DR_irs_ns_show = 0
			B738DR_irs_ew_show = 0
			B738DR_irs_lat_deg_show = 0
			B738DR_irs_lat_min_show = 0
			B738DR_irs_lon_deg_show = 0
			B738DR_irs_lon_min_show = 0
			B738DR_irs_decimals_show = 0
			B738DR_irs_allign_show = 0
		end
		
		B738DR_irs_align_left2 = irs_align_left		--B738DR_irs_align_left
		B738DR_irs_align_right2 = irs_align_right	--B738DR_irs_align_right
		
		B738DR_adiru_left_power		= irs_enable
		B738DR_adiru_right_power	= irs_enable2
		
		if B738DR_lights_test == 1 and simDR_radio_height_pilot_ft < 50 then
			B738DR_irs_align_left = 1 * B738DR_brightness_level
			B738DR_irs_align_fail_left = 1 * B738DR_brightness_level
			B738DR_irs_align_right = 1 * B738DR_brightness_level
			B738DR_irs_align_fail_right = 1 * B738DR_brightness_level
			B738DR_irs_on_dc_left = 1 * B738DR_brightness_level
			B738DR_irs_on_dc_right = 1 * B738DR_brightness_level
			B738DR_irs_dc_fail_left = 1 * B738DR_brightness_level
			B738DR_irs_dc_fail_right = 1 * B738DR_brightness_level
		else
			B738DR_irs_align_fail_left = irs_align_fail_left * B738DR_brightness_level
			B738DR_irs_align_fail_right = irs_align_fail_right * B738DR_brightness_level
			B738DR_irs_dc_fail_left = irs_dc_fail_left * B738DR_brightness_level
			B738DR_irs_dc_fail_right = irs_dc_fail_right * B738DR_brightness_level
		end
		B738DR_irs_align_fail_1 = irs_align_fail_left * B738DR_brightness_level
		B738DR_irs_align_fail_2 = irs_align_fail_right * B738DR_brightness_level
		pfd_nd()
		turn_around_state()
		-- if B738DR_load_save ~= 3 then
			load_save(1)
		-- end
	end

end

--function after_replay() end


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

-- dofile("")



