Compile subroutine Lock_Record(Table_name, Table_handle, Record_ID, Direction, Allow_abort, Ignore_self_lock, Success)

/*
	This subroutine is used to perform locks and unlocks on whatever record ID
	is passed. If the ID is already locked, it will loop continuously until
	the lock is released by the other station. If the current station already
	has the lock, a message window will be displayed indicating the situation.
	After pressing <Enter>, the routine will automatically return with Success
	set as No$.
*/

$insert APP_INSERTS
$insert MSG_EQUATES

Declare function Create_Dialog, Get_Property
Declare subroutine Yield, End_Dialog, Set_Property

If Assigned(Table_name) else Table_name = ""
If Assigned(Table_handle) else Table_handle = ""
If Assigned(Record_ID) else Record_ID = ""
If Assigned(Direction) else Direction = ""
If Assigned(Allow_abort) else Allow_abort = No$
If Assigned(Ignore_self_lock) else Ignore_self_lock = No$

GoSub Setup_Msg_Vars

On Direction GoSub Lock_Record, Unlock_Record

Return

Lock_Record:
	NLR			 = ""
	Success      = No$
	Quit_locking = No$
	Status       = ""
	Loop_counter = 0
	Open Table_name to Table_handle then
		Loop
			Loop_counter += 1
			Lock Table_handle, Record_ID then
				End_Dialog(NLR)
				Success = 1
			end else
				Status = Status()
			end
		Until (Success EQ 1) OR (Success EQ 2) OR (Quit_locking EQ Yes$)
			Begin Case
				Case Status EQ 1
					If Ignore_self_lock EQ Yes$ else
						rv = Msg(@Window, Msg_not_locked, "", "", Record_ID:@FM:Table_name)
						Quit_locking = Yes$
					end
					Success = 2
				Case Loop_counter EQ 1
					// Allow_abort is passed to NDW_LOCK_RECORD which will display or hide the cancel
					// button accordingly
					Parent = Get_Property(@Window, "@PARENT")				; // Check first for a pre-determined parent
					If Parent else Parent = Get_Property(@Window, "PARENT") ; // If no pre-determined parent exists, get the real parent
					Parent = Parent[1, "F."]								; // Make sure only the window is returned
					Parent = "FRW_MAIN"
					NLR = Create_Dialog("NDW_LOCK_RECORD", Parent, True$, Record_ID:@FM:Table_name:@FM:@Window:@FM:Allow_abort)
					Set_Property(Parent, "ENABLED", No$)
					GoSub Pause
				Case Otherwise$
					Text = "Number of attempts: ":Loop_counter
					Set_Property(NLR:".STA_MESSAGE2", "TEXT", Text)
					GoSub Pause
			End Case
		Repeat
		Set_Property(Parent, "ENABLED", Yes$)
	end else
		rv = Msg(@Window, Msg_open_table, "", "", Table_name)
	end
return

Unlock_Record:
	Unlock Table_handle, Record_ID else
		*rv = Msg(@Window, Msg_unlock_error, "", "", Record_ID:@FM:Table_name)
	end
return

Setup_Msg_Vars:
	Msg_struct = ""
	Msg_struct<MTYPE$>		= "BO"
	Msg_struct<MMODAL$>		= "A"
	Msg_struct<MICON$>		= "!"
	Msg_struct<MDEFBTN$>	= "1"
	Msg_struct<MCOL$>		= "-2"
	Msg_struct<MROW$>		= "-2"
	Msg_struct<MJUST$>		= "T"
	Msg_struct<MFONT$>		= Tahoma_8_Reg$
	Msg_struct<MCAPTION$>	= "Lock Record"
	Msg_struct<MMASKINPUT$> = "0"
	Msg_struct<MCLIPBMP$>	= "1"
	Msg_struct<MLITERAL$>	= "0"
	Msg_struct<MREQRESP$>	= "0"

	Msg_not_locked = Msg_struct
	Msg_not_locked<MTEXT$> = "Cannot lock the %1% record from the %2% table because this station already has it locked."

	Msg_open_table = Msg_struct
	Msg_open_table<MTEXT$> = "Error opening the %1% table."

	Msg_unlock_error = Msg_struct
	Msg_unlock_error<MTEXT$> = "Error Unlocking the %1% record from the %2% table."
return

Pause:
	Now = Time()
	Loop
		Yield()
		Quit_locking = Get_Property(NLR, "@QUIT")
		hWnd = Get_Property(NLR, "HANDLE")
		If hWnd EQ "" then Quit_locking = Yes$ ; // Means the NDW_LOCK_RECORD was closed
	Until (Time() GE Now + 5) OR (Quit_locking)
	Repeat
return
