'*********************************************************** 
'GENERAL DATABASE SUBS... 

FUNCTION CountTotalEntries(Null AS INTEGER) AS INTEGER
DIM CTED AS INTEGER
'Count the total number of bound commands... 
FOR CTED = 1 TO 400 STEP 1
	IF Bound(CTED).RealName = SPACE$(30) THEN
		'Yep, that's the last entry. 
		EXIT FOR
	END IF
NEXT CTED
CountTotalEntries = CTED
END FUNCTION

SUB ShowBindForm
'Check that we can actually bind any more, or if a warning should be 
'issued... 
Dummy = 0
Dummy2 = CountTotalEntries(0)
IF Dummy2 > 399 THEN
	CMessageBox("Incredible! You have 400 bindings! But now you can't add any more! Sorry about that! If you want to add some more, you'll have to delete some first. Go to the bind editor to accomplish this.",btOk,bmMessage)
	Dummy = 1
	EXIT SUB
END IF
IF (Dummy2 > 350) AND (Dummy = 0) THEN
	CMessageBox("Wow! You have managed to accumulate "+STR$(Dummy2)+" bindings! Please be careful from now on, as the maximum number of bindings is 400. Just keep this in mind.",btOk,bmMessage)
END IF

'Reset the form... 
BoundNameBox.Text = ""
RealNameBox.Text = ""
FilePanel.Visible = True
OtherPanel.Visible = False
ExecBox.Text = ""
FileBox.Text = ""
FoldBox.Text = ""
URLBox.Text = ""
OpenNewWindowCheck.Checked = True
PlainOpenCheck.Checked = True
OpenWCommandLine.Checked = False
OpenWithExplorerCheck.Checked = False
CustomCommandBox.Text = ""
BFRB1.Checked = True

'Refill applet combo box... 
PopulateCPanelList(1)

'Then show it... 
BindForm.Show
BindForm.Repaint
SetFocus(BoundNameBox.Handle)
END SUB

SUB CloseBindForm
BindForm.Visible = False
SELECT CASE BindTime
	CASE btBindEditor
		'Just hide the form... and maybe an update... 
		BindTime = 0
		UpdateBindEditor
	CASE btCmdLine
		'Just do nothing... 
		BindTime = 0
	CASE ELSE
		'Show the console again... 
		AnimateDown
END SELECT
END SUB

SUB ShowBindFilled
'Show the bind form with some pre-filled data. 

'Check that we can actually bind any more, or if a warning should be 
'issued... 
Dummy = 0
Dummy2 = CountTotalEntries(0)
IF Dummy2 > 399 THEN
	CMessageBox("Incredible! You have 400 bindings! But now you can't add any more! Sorry about that! If you want to add some more, you'll have to delete some first. Go to the bind editor to accomplish this.",btOk,bmMessage)
	Dummy = 1
	EXIT SUB
END IF
IF (Dummy2 > 350) AND (Dummy = 0) THEN
	CMessageBox("Wow! You have managed to accumulate "+STR$(Dummy2)+" bindings! Please be careful from now on, as the maximum number of bindings is 400. Just keep this in mind.",btOk,bmMessage)
END IF

BoundNameBox.Text = ""
RealNameBox.Text = ""
FilePanel.Visible = True
OtherPanel.Visible = False
ExecBox.Text = ""
FileBox.Text = ""
FoldBox.Text = ""
URLBox.Text = ""
OpenNewWindowCheck.Checked = True
PlainOpenCheck.Checked = True
OpenWCommandLine.Checked = False
OpenWithExplorerCheck.Checked = False
CustomCommandBox.Text = ""

BoundNameBox.Text = Commands$(2)
RealNameBox.Text = Commands$(3)
PopulateCPanelList(1)
'Select type... 
SELECT CASE UCASE$(Commands$(5))
	CASE "EXEC"
		ExecBox.Text = Commands$(4)
		BFRB1.Checked = True
		FilePanel.Visible = True
		OtherPanel.Visible = False
	CASE "FILE"
		FileBox.Text = Commands$(4)
		BFRB2.Checked = True
		FilePanel.Visible = True
		OtherPanel.Visible = False
	CASE "FOLDER"
		FoldBox.Text = Commands$(4)
		BFRB3.Checked = True
		FilePanel.Visible = True
		OtherPanel.Visible = False
	CASE "WEB"
		URLBox.Text = Commands$(4)
		BFRB4.Checked = True
		OtherPanel.Visible = True
		FilePanel.Visible = False
	CASE "CPANEL"
		AppletComboBox.ItemIndex = 1
		BFRB5.Checked = True
		OtherPanel.Visible = True
		FilePanel.Visible = False
	CASE "CUST"
		CustomCommandBox.Text = Commands$(4)
		BFRB6.Checked = True
		OtherPanel.Visible = True
		FilePanel.Visible = False
END SELECT

IF UCASE$(Command$(6)) = "D" THEN
	'Show the dialog box... 
	AnimateUp
	BindForm.Show
ELSE
	'Just bind it... 
	AddBinding
END IF
END SUB

SUB BindFormSelectType(Sender AS QRadioButton)
'Ok, change the appearance of the Bind form depending on exactly 
'which radio button is checked... 
'So, decide which panel is to be shown... 
FilePanel.Visible = False
OtherPanel.Visible = False
IF Sender.Tag < 4 THEN
	'Show the file panel... 
	FilePanel.Visible = True
ELSE
	OtherPanel.Visible = True
END IF
END SUB

SUB ChooseFileFold(Sender AS QButton)
'Choose which item to select, and select it... 
SELECT CASE Sender.Tag
	CASE 1
		'Select executable... 
		OpenDlg.Filter = "Executable files (*.exe,*.com,*.bat)|*.exe;*.com;*.bat|All Files (*.*)|*.*"
		OpenDlg.FilterIndex = 1
		OpenDlg.Caption = "What do you want to run today?"
		OpenDlg.FileName = ExecBox.Text
		IF OpenDlg.Execute THEN
			ExecBox.Text = OpenDlg.FileName
		END IF
	CASE 2
		'Select file... 
		OpenDlg.Filter = "All Files (*.*)|*.*|Picture files (*.bmp,*.gif,*.jpg,*.pcx,*.wmf)|*.bmp;*.gif;*.jpg;*.pcx;*.wmf|Music files (*.wav,*.mid,*.rmi,*.mp3)|*.wav;*.mid;*.rmi;*.mp3|Text files (*.doc,*.rtf,*.txt)|*.doc;*.rtf;*.txt"
		OpenDlg.FilterIndex = 1
		OpenDlg.Caption = "Pick some cool file to open..."
		OpenDlg.FileName = FileBox.Text
		IF OpenDlg.Execute THEN
			FileBox.Text = OpenDlg.FileName
		END IF
	CASE 3
		'Select folder... 
		Dummy$ = BrowseForFolder(OptionsForm.Handle, "Choose a folder to have a look at...")
		IF Dummy$ = "" THEN
			'Do nothing... 
		ELSE
			FoldBox.Text = Dummy$
		END IF
	CASE 4
		OpenDlg.Filter = "Web pages (*.htm,*.html)|*.htm*|Shortcuts (*.url,)|*.url|All Files (*.*)|*.*"
		OpenDlg.FilterIndex = 1
		OpenDlg.Caption = "Where do we want to visit today?"
		IF OpenDlg.Execute THEN
			URLBox.Text = "file://"+OpenDlg.FileName
		END IF
END SELECT
END SUB

SUB AddBinding
DIM CMG$ AS STRING
'Add the binding to the internal database... 
'Do error checking first (with the funny messages!) 
'General error checking... 
'Bind name/real name boxes... 
IF BoundNameBox.Text = "" THEN
	'You need to enter text in the bound name box! 
	CMessageBox("Uh oh... you need to have a keyword in the Bound Name box! Please do try again!",bmError,btOk)
	BindForm.Show
	EXIT SUB
END IF
IF INSTR(BoundNameBox.Text," ") <> 0 THEN
	'You have spaces in the bound name! 
	CMessageBox("Gee... somebody put spaces in the Bound Name! Better get rid of them!",bmError,btOk)
	EXIT SUB
END IF
IF RealNameBox.Text = "" THEN
	'You need to enter a real name! 
	CMG$ = "Houston, we have a problem. [beep]"+CHR$(13)
	CMG$ = CMG$ + "Go ahead, Odyssey. What is your problem? [beep]"+CHR$(13)
	CMG$ = CMG$ + "It seems somebody forgot to enter a Real Name. [beep]"+CHR$(13)
	CMG$ = CMG$ + "Ahh... that's a problem. We're onto it, Odyssey. [beep]"+CHR$(13)
	CMG$ = CMG$ + "Thankyou. Signing off. [beep]"
	CMessageBox(CMG$,bmError,btOk)
	BindForm.Show
	EXIT SUB
END IF

'Check to see if you are trying to bind an internal command... 
FOR Dummy = 1 TO InternalCount
	IF UCASE$(BoundNameBox.Text) = InternalCommands$(Dummy) THEN
		CMessageBox("Tut tut tut. You shouldn't try things like this. They can be harmful - but not to me. You are trying to bind a keyword which is already bound to an internal command. So, please choose another keyword, and try again.",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
NEXT Dummy

'Check to see parameters respective to each type of binding... 
IF BFRB1.Checked = True THEN
	'Check dirbox and file list box... 
	IF (FileExists(ExecBox.Text) = False) THEN
		CMessageBox("A small error of judgement, I'm sure, but executable specified in the box does not exist! Please try again! ",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
END IF
IF BFRB2.Checked = True THEN
	IF (FileExists(FileBox.Text) = False) THEN
		CMessageBox("Whoops... I always seem to make errors. Please don't follow in my trait! Make sure the file in the file box exists, because it doesn't seem to!",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
END IF
IF BFRB3.Checked = True THEN
	IF (DirExists(FoldBox.Text) = False) THEN
		CMessageBox("Deary me... it would appear, according to my initial checks, that the folder in the box does not exist! Please try again!",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
END IF
IF BFRB4.Checked = True THEN
	IF URLBox.Text = "" THEN
		CMG$ = "WARNING: 404 Error"+CHR$(13)
		CMG$ = CMG$ + "The file or script you have requested does not exist."+CHR$(13)
		CMG$ = CMG$ + "This means that you didn't enter an address to visit. Please try again."+CHR$(13)
		CMG$ = CMG$ + "Should this continually fail please contact your network administrator."
		CMessageBox(CMG$,bmError,btOk)
	END IF
	IF OpenNewWindowCheck.Checked = False THEN
		'Verify... 
		IF CMessageBox("WARNING: You have opted NOT to visit the specified address in a new window. Please do note that should you type in the keyword bound to this file, the address will be opened in any open browser window. Your dog will be annoyed at losing the webpage it was visiting. Are you sure you want to keep things this way?",bmWarning,btYesNo) = mrNo THEN
			OpenNewWindowCheck.Checked = True
		END IF
	END IF
	'Last check: a URL type. 
	IF INSTR(URLBox.Text,"://") = 0 THEN
		'Oh dear... no type! 
		IF CMessageBox("Alright then! What type are you? Oh, I mean, what type is this webpage? For it to work correctly, you need to proceed the URL with either a http://, file://, ftp://, gopher://, or something similar. So please add something like that, and then try again. If you want, I can insert a http:// for you. Do you want me to do that?",bmError,btYesNo) = mrYes THEN
			'Add in a http:// 
			Dummy2$ = URLBox.Text
			URLBox.Text = "http://"+Dummy2$
		END IF
		BindForm.Show
		EXIT SUB
	END IF
END IF
IF BFRB5.Checked = True THEN
	IF AppletComboBox.ItemIndex < 0 THEN
		CMessageBox("Do you like apples? I would hope so. But you know, you can really show your respect for apples by selecting an control panel applet to be run from the list. Then the great master apple knows that all his little brothers and sisters are loved. He will thank you for such an act of respect.",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
END IF
IF BFRB6.Checked = True THEN
	IF CustomCommandBox.Text = "" THEN
		CMessageBox("I was just wondering... if somebody selects an option for a reason, you would expect them to follow that option through, unless they had a reason for not doing so. So, if you are going to select to enter a custom command, could you please type in a custom command? Ta.",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
END IF

'Last one: is this keyword already taken? Or is the Real Name already used? 
Dummy2 = CountTotalEntries(0)
FOR Dummy = 1 TO (Dummy2-1)
	IF RTRIM$(Bound(Dummy).BoundName) = BoundNameBox.Text THEN
		CMessageBox("In this world, (at time of writing), there are around six billion people. And there is one fact about all these people that is truly amazing: none of them are the same. And, the same applies to bindings. Each one is different... well, it would be, if you would choose another bound name, as the one you are trying to use already exists. Could you please change it? Then we can go back to our individual world. Thankyou.",bmError,btOk)
		BindForm.Show
		EXIT SUB
	END IF
	IF RTRIM$(Bound(Dummy).RealName) = RealNameBox.Text THEN
		IF CMessageBox("Isn't it strange when you can look at two books and find that whilst their names are the same, the contents are completely different. That sort of thing is occuring right now, with the bindings. You have given this binding the same real name as another binding. This does not mean that it won't work, but it could mean that this item is already bound. Do you still want to proceed?",bmError,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
	IF (BFRB1.Checked = True) AND (UCASE$(RTRIM$(Bound(Dummy).BoundFile)) = UCASE$(ExecBox.Text)) THEN
		IF CMessageBox("I wonder... how many identical copies of a book do you have? Well... maybe you might want to consider the fact that the file you are trying to bind has already been bound to another command. This will cause no problems. For your reference, this file is already bound to the keyword "+RTRIM$(Bound(Dummy).BoundName)+", which has a real name of "+RTRIM$(Bound(Dummy).RealName)+". The file in question is "+RTRIM$(Bound(Dummy).BoundFile)+". Do you want to bind it anyway?",bmWarning,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
	IF (BFRB2.Checked = True) AND (UCASE$(RTRIM$(Bound(Dummy).BoundFile)) = UCASE$(FileBox.Text)) THEN
		IF CMessageBox("Dear me. It's 9:14 PM, on a Wednesday night, and I'm sitting here racking my brains for witty comments to write when an error occurs. Well, my witty comments are running out. So I'll just give it to you straight: the file you are trying to bind is already bound in another command. For your reference, this file is already bound to the keyword "+RTRIM$(Bound(Dummy).BoundName)+", which has a real name of "+RTRIM$(Bound(Dummy).RealName)+". The file in question is "+RTRIM$(Bound(Dummy).BoundFile)+". You you want to bind it anyway (you might regret it)?",bmError,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
	IF (BFRB3.Checked = True) AND (UCASE$(RTRIM$(Bound(Dummy).BoundFile)) = UCASE$(FoldBox.Text)) THEN
		IF CMessageBox("Hey, do you know how to make a floppy-disk bomb? Just get a floppy disk and... Oh, sorry. I wasn't paying any attention. So what did you want? Oh dear... an error. According to my list... you are trying to bind a folder that is already bound to another keyword. Oh, that's nothing serious. Complete details... ahh, here, down the bottom...  'this folder is already bound to the keyword "+RTRIM$(Bound(Dummy).BoundName)+", which has a real name of "+RTRIM$(Bound(Dummy).RealName)+". The file in question is "+RTRIM$(Bound(Dummy).BoundFile)+".' Well, this isn't serious, but are you sure you want to bind it again?",bmError,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
	IF (BFRB4.Checked = True) AND (RTRIM$(Bound(Dummy).BoundFile) = URLBox.Text) THEN
		IF CMessageBox("Cool... oops! Shouldn't say that. This is no laughing matter. It would appear that you have already bound this web address to another keyword. For your reference, this webpage is already bound to the keyword "+RTRIM$(Bound(Dummy).BoundName)+", which has a real name of "+RTRIM$(Bound(Dummy).RealName)+". The address in question is "+RTRIM$(Bound(Dummy).BoundFile)+". Do you really want to bind it again?",bmError,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
	IF (BFRB5.Checked = True) AND (RTRIM$(Bound(Dummy).BoundFile) = AppletComboBox.Item(AppletComboBox.ItemIndex)) THEN
		IF CMessageBox("Apples, apples, everywhere. Applets, applets, crossreferenced everywhere. Ok, I'm not a poet, but at least you can hope that I'm a decent programmer! I write semi-decent programs that tell you when to have referenced a control panel applet by more than one keyword... which is exactly why this error message is here. For your reference, this applet is already bound to the keyword "+RTRIM$(Bound(Dummy).BoundName)+", which has a real name of "+RTRIM$(Bound(Dummy).RealName)+". The applet in question is "+RTRIM$(Bound(Dummy).BoundFile)+". Do you want to make another reference to this applet?",bmError,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
	IF (BFRB6.Checked = True) AND (RTRIM$(Bound(Dummy).BoundFIle) = CustomCommandBox.Text) THEN
		IF CMessageBox("Ok, I'm sick of making up witty remarks for errors. That's enough for tonight. At 9:40 PM, it's time to go to bed. I have a big school day tommorow, and I need to get my sleep. So here we go: you are trying to bind a custom command when the custom command is already bound to another keyword. Why would you want to do this? (Don't answer that). For your reference, this custom command is already bound to the keyword "+RTRIM$(Bound(Dummy).BoundName)+", which has a real name of "+RTRIM$(Bound(Dummy).RealName)+". The custom command in question is "+RTRIM$(Bound(Dummy).BoundFile)+". So, do ya still want to bind it?",bmError,btYesNo) = mrNo THEN
			BindForm.Show
			EXIT SUB
		END IF
	END IF
NEXT Dummy

'Well! That was fun... well, it was three hours ago... 

'Ok, done all the error checking (I hope!) 
'Actually add the item... 
Dummy = CountTotalEntries(0)
'Blank entry at dummy! 
Bound(Dummy).BoundName = BoundNameBox.Text
Bound(Dummy).RealName = RealNameBox.Text
IF BFRB1.Checked = True THEN Bound(Dummy).BoundType = bExec
IF BFRB1.Checked = True THEN Bound(Dummy).BoundFile = ExecBox.Text
IF BFRB2.Checked = True THEN Bound(Dummy).BoundType = bFile
IF BFRB2.Checked = True THEN Bound(Dummy).BoundFile = FileBox.Text
IF BFRB3.Checked = True THEN Bound(Dummy).BoundType = bFold
IF BFRB3.Checked = True THEN Bound(Dummy).BoundFile = FoldBox.Text
IF BFRB3.Checked = True THEN
	IF OpenWithExplorerCheck.Checked = True THEN
		Bound(Dummy).ExtraInfo = 1
	ELSEIF OpenWCommandLine.Checked = True THEN
		Bound(Dummy).ExtraInfo = 2
	END IF
END IF
IF BFRB4.Checked = True THEN Bound(Dummy).BoundType = bWeb
IF BFRB4.Checked = True THEN Bound(Dummy).BoundFile = URLBox.Text
IF BFRB4.Checked = True THEN Bound(Dummy).ExtraInfo = OpenWithExplorerCheck.Checked
IF BFRB5.Checked = True THEN Bound(Dummy).BoundType = bCPanel
IF BFRB5.Checked = True THEN Bound(Dummy).BoundFile = AppletComboBox.Item(AppletComboBox.ItemIndex)
IF BFRB6.Checked = True THEN Bound(Dummy).BoundType = bCust
IF BFRB6.Checked = True THEN Bound(Dummy).BoundFile = CustomCommandBox.Text

'Bye bye bind form! 
AddHistory("Binding sucessfully added!",hReturn)
CloseBindForm

END SUB

SUB ShowBindEditor
'Show bindings... 
UpdateBindEditor
'First the control panel list box... 
AppletChCombo.Enabled = True
PopulateCPanelList(2)
'Clear other form elements... 
BoundNmBox.Text = ""
RealNmBox.Text = ""
BoundTypCombo.ItemIndex = -1
BoundItmBox.Text = ""
AppletChCombo.ItemIndex = 1
'Show the form... 
BindEditForm.Visible = True
BindEditForm.Repaint
END SUB

SUB CloseBindEditor
BindEditForm.Visible = False
AnimateDown
END SUB

SUB BindEditorBind
BindTime = btBindEditor
ShowBindForm
END SUB

SUB RemoveBinding
'Confirm deletion... 
IF CMessageBox("Watch out - if you delete these bindings, you will never see them again! Are you sure you want to delete them?",bmWarning,btYesNo) = mrYes THEN
	'Count how many bindings (Reference for later) 
	Dummy2 = CountTotalEntries(0)
	'Then remove the selected item... 
	Bound(BindingListBox.ItemIndex+1) = BoundBlank
	FOR Dummy = (BindingListBox.ItemIndex+1) TO Dummy2
		'Move everything up... 
		Bound(Dummy) = Bound(Dummy+1)
	NEXT Dummy
	'Update the box... 
	UpdateBindEditor
END IF
END SUB

SUB UpdateBindEditor
'Update the list view in the bind editor... 
'Clear the box first... 
BindingListBox.Clear
'Then add the items... 
Dummy2 = CountTotalEntries(0)-1
'Do a count up, and check how many bindings you have... 
Dummy = 0
IF Dummy2 > 399 THEN
	CMessageBox("Incredible! You have 400 bindings! But now you can't add any more! Sorry about that! If you want to add some more, you'll have to delete some first.",btOk,bmMessage)
	Dummy = 1
END IF
IF (Dummy2 > 350) AND (Dummy = 0) THEN
	CMessageBox("Wow! You have managed to accumulate "+STR$(Dummy2)+" bindings! Please be careful from now on, as the maximum number of bindings is 400. Just keep this in mind.",btOk,bmMessage)
END IF
FOR Dummy = 1 TO Dummy2
	BindingListBox.AddItems(RTRIM$(Bound(Dummy).RealName))
	BindingListBox.AddSubItem(Dummy-1,RTRIM$(Bound(Dummy).BoundName))
	IF Bound(Dummy).BoundType = bExec THEN BindingListBox.AddSubItem(Dummy-1,"Executable")
	IF Bound(Dummy).BoundType = bFile THEN BindingListBox.AddSubItem(Dummy-1,"File")
	IF Bound(Dummy).BoundType = bFold THEN BindingListBox.AddSubItem(Dummy-1,"Folder")
	IF Bound(Dummy).BoundType = bWeb THEN BindingListBox.AddSubItem(Dummy-1,"Web page")
	IF Bound(Dummy).BoundType = bCPanel THEN BindingListBox.AddSubItem(Dummy-1,"C. Panel")
	IF Bound(Dummy).BoundType = bCust THEN BindingListBox.AddSubItem(Dummy-1,"Custom")
	BindingListBox.AddSubItem(Dummy-1,RTRIM$(Bound(Dummy).BoundFile))
	BindingListBox.Item(Dummy-1).ImageIndex = Bound(Dummy).BoundType - 1
NEXT Dummy
'And, hey presto! Done! 
END SUB

SUB SortEntries
BindEditForm.Visible = False
SortForm.Show
SortForm.Repaint
'Do sort... 
Dummy2 = CountTotalEntries(0)-2
IF Dummy2 < 1 THEN
	EXIT SUB
END IF
Dummy3 = False
SortProgress.Min = 0
SortProgress.Max = Dummy2
WHILE 1
SortProgress.Position = 0
Dummy3 = False
FOR Dummy = 1 TO Dummy2
	IF UCASE$(Bound(Dummy).RealName) > UCASE$(Bound(Dummy+1).RealName) THEN
		BoundSwap = Bound(Dummy)
		Bound(Dummy) = Bound(Dummy+1)
		Bound(Dummy+1) = BoundSwap
		Dummy3 = True
	END IF
	SortProgress.Position = SortProgress.Position + 1
NEXT Dummy
IF Dummy3 = False THEN
	EXIT WHILE
END IF
DoEvents
WEND
SortForm.Visible = False
UpdateBindEditor
BindEditForm.Visible = True
END SUB

SUB SecSort
'Sort from the command line... 
SortForm.Show
SortForm.Repaint
'Do sort... 
Dummy2 = CountTotalEntries(0)-2
IF Dummy2 < 1 THEN
	AddHistory("No items or not enough items to sort!",hReturn)
	EXIT SUB
END IF
Dummy3 = False
SortProgress.Min = 0
SortProgress.Max = Dummy2
WHILE 1
SortProgress.Position = 0
Dummy3 = False
FOR Dummy = 1 TO Dummy2
	IF UCASE$(Bound(Dummy).RealName) > UCASE$(Bound(Dummy+1).RealName) THEN
		BoundSwap = Bound(Dummy)
		Bound(Dummy) = Bound(Dummy+1)
		Bound(Dummy+1) = BoundSwap
		Dummy3 = True
	END IF
	SortProgress.Position = SortProgress.Position + 1
NEXT Dummy
IF Dummy3 = False THEN
	EXIT WHILE
END IF
DoEvents
WEND
SortForm.Visible = False
END SUB

SUB ShowEditBox
'Update the edit section of the bindeditor. 
Dummy = BindingListBox.ItemIndex+1
BoundNmBox.Text = RTRIM$(Bound(Dummy).BoundName)
RealNmBox.Text = RTRIM$(Bound(Dummy).RealName)
BoundTypCombo.ItemIndex = (Bound(Dummy).BoundType-1)

AppletChCombo.Enabled = True
'Select appropriate item should we have of selected a CPanel thingy... 
IF (Bound(Dummy).BoundType = bCPanel) THEN
	'Select index... 
	BoundItmBox.Enabled = False
	BoundItmBox.Text = Bound(Dummy).BoundFile
	BoundItmBox.Color = clBtnFace
	AppletChCombo.Color = &HFFFFFF
	FOR Dummy2 = 1 TO CPTot
		IF RTRIM$(Bound(Dummy).BoundFile) = AppletChCombo.Item(Dummy2-1) THEN
			AppletChCombo.ItemIndex = Dummy2-1
		END IF
	NEXT Dummy2
ELSE
	AppletChCombo.ItemIndex = 0
	AppletChCombo.Enabled = False
	AppletChCombo.Color = clBtnFace
	BoundItmBox.Enabled = True
	BoundItmBox.Color = &HFFFFFF
	BoundItmBox.Text = RTRIM$(Bound(Dummy).BoundFile)
END IF

OpenPlnCheck.Enabled = False
OpenNewWindCheck.Enabled = False
OpenWExCheck.Enabled = False
OpenWCmdLine.Enabled = False

IF Bound(Dummy).BoundType = bFold THEN
	'Set the checkboxes... 
	OpenNewWindCheck.Visible = False
	OpenPlnCheck.Enabled = True
	OpenPlnCheck.Visible = True
	OpenWExCheck.Enabled = True
	OpenWCmdLine.Enabled = True
	OpenPlnCheck.Checked = True
	OpenWExCheck.Checked = False
	OpenWCmdLine.Checked = False
	SELECT CASE Bound(Dummy).ExtraInfo
		CASE 1
			OpenWExCheck.Checked = True
		CASE 2
			OpenWCmdLine.Checked = True
		CASE ELSE
			PlainOpenCheck.Checked = True
	END SELECT
END IF

ChangeBoundBtn.Enabled = True
IF Bound(Dummy).BoundType = bWeb THEN
	OpenPlnCheck.Enabled = False
	OpenPlnCheck.Visible = False
	OpenNewWindCheck.Enabled = True
	OpenNewWindCheck.Visible = True
	OpenNewWindCheck.Checked = Bound(Dummy).ExtraInfo
	OpenWExCheck.Enabled = False
	OpenWCmdLine.Enabled = False
END IF
END SUB

SUB AcceptEditBox
'Get rid of edit form, but record the values... 
'Do error checks first... 
IF BoundNmBox.Text = "" THEN
	CMessageBox("There has been a minor problem which just came to my attention. You failed to enter a bound name. Please try again.",bmError,btOk)
	EXIT SUB
END IF


IF INSTR(BoundNmBox.Text," ") <> 0 THEN
	CMessageBox("As much as it may be nice to put spaces in the bound name, you can't. So please go back and remove any spaces in it! Thankyou.",bmError,btOk)
	EXIT SUB
END IF

IF RealNmBox.Text = "" THEN
	CMessageBox("Is a rose by any other name a rose? Well, I don't know about that, but you need to have a real name. Please enter one and try again!",bmError,btOk)
	EXIT SUB
END IF

'Check to see if you are trying to bind an internal command... 
FOR Dummy = 1 TO InternalCount
	IF UCASE$(BoundNmBox.Text) = InternalCommands$(Dummy) THEN
		CMessageBox("Sometimes I wonder. Some people think they can get away with anything. In this program, you can't. So please enter a keyword which IS NOT an internal command, and try again. Thanks!",bmError,btOk)
		EXIT SUB
	END IF
NEXT Dummy

SELECT CASE BoundTypCombo.ItemIndex
	CASE IS < 2
		IF FileExists(BoundItmBox.Text) = False THEN
			Dummy$ = "!!! WARNING !!!"+CHR$(13)
			Dummy$ = Dummy$ + "! SYSTEM ERROR !"+CHR$(13)
			Dummy$ = Dummy$ + "File not found!"+CHR$(13)
			Dummy$ = Dummy$ + "Please enter a valid filename!"+CHR$(13)
			CMessageBox(Dummy$,bmError,btOk)
			EXIT SUB
		END IF
	CASE 2
		IF DirExists(BoundItmBox.Text) = False THEN
			CMessageBox("Oh dear... no... no... this can't be happening! That folder doesn't exist! No... we're all going to die! No.........",bmError,btOk)
			EXIT SUB
		END IF
	CASE 3 TO 4
		IF BoundItmBox.Text = "" THEN
			CMessageBox("Romeo, Romeo, what web page did you want to visit? Or did you want to put in a custom command? Please tell thou soon... (In other words, you haven't entered a web page or a custom command string. Please try again.)",bmError,btOk)
			EXIT SUB
		END IF
	CASE 5
		'Well... there is no fear of you not selecting a control panel applet... 
END SELECT

Dummy = BindingListBox.ItemIndex+1
Bound(Dummy).BoundName = BoundNmBox.Text
Bound(Dummy).RealName = RealNmBox.Text
Bound(Dummy).BoundType = BoundTypCombo.ItemIndex+1
Bound(Dummy).BoundFile = BoundItmBox.Text
Bound(Dummy).ExtraInfo = OpenNewWindCheck.Checked
IF Bound(Dummy).BoundType = bFold THEN
	IF OpenWExCheck.Checked = True THEN
		Bound(Dummy).ExtraInfo = 1
	ELSEIF OpenWCmdLine.Checked = True THEN
		Bound(Dummy).ExtraInfo = 2
	END IF
END IF
IF Bound(Dummy).BoundType = bCPanel THEN
	Bound(Dummy).BoundFile = AppletCHCombo.Item(AppletChCombo.ItemIndex)
END IF
UpdateBindEditor

END SUB

SUB SelectControls
'Select which controls to show... 
IF BoundTypCombo.ItemIndex < 2 THEN
	'It's a file/executable! 
	OpenWExCheck.Enabled = False
	AppletChCombo.Enabled = False
	AppletChCombo.Color = clBtnFace
	BoundItmBox.Enabled = True
	BoundItmBox.Color = &HFFFFFF
	OpenPlnCheck.Enabled = False
	OpenNewWindCheck.Enabled = False
	OpenWExCheck.Enabled = False
	OpenWCmdLine.Enabled = False
	ChangeBoundBtn.Enabled = True
END IF
IF BoundTypCombo.ItemIndex = 2 THEN
	'It's a folder! 
	OpenWExCheck.Enabled = True
	AppletChCombo.Enabled = False
	AppletChCombo.Color = clBtnFace
	BoundItmBox.Enabled = True
	BoundItmBox.Color = &HFFFFFF
	OpenNewWindCheck.Enabled = False
	OpenNewWindCheck.Visible = False
	OpenPlnCheck.Enabled = True
	OpenPlnCheck.Visible = True
	OpenWExCheck.Enabled = True
	OpenWCmdLine.Enabled = True
	ChangeBoundBtn.Enabled = True
END IF
IF BoundTypCombo.ItemIndex = 5 THEN
	'Control panel applet! 
	OpenWExCheck.Enabled = False
	AppletChCombo.Enabled = True
	AppletChCombo.Color = &HFFFFFF
	BoundItmBox.Enabled = False
	BoundItmBox.Color = clBtnFace
	OpenPlnCheck.Enabled = False
	OpenNewWindCheck.Enabled = False
	OpenWExCheck.Enabled = False
	OpenWCmdLine.Enabled = False
	ChangeBoundBtn.Enabled = False
END IF
IF BoundTypCombo.ItemIndex = 3 THEN
	'A web page! 
	AppletChCombo.Enabled = False
	AppletChCombo.Color = clBtnFace
	BoundNmBox.Enabled = True
	BoundItmBox.Enabled = True
	BoundItmBox.Color = &HFFFFFF
	OpenPlnCheck.Enabled = False
	OpenPlnCheck.Visible = False
	OpenNewWindCheck.Enabled = True
	OpenNewWindCheck.Visible = True
	OpenWExCheck.Enabled = False
	OpenWCmdLine.Enabled = False
	ChangeBoundBtn.Enabled = True
END IF
IF BoundTypCombo.ItemIndex = 4 THEN
	'A custom command... 
	AppletChCombo.Enabled = False
	AppletChCombo.Color = clBtnFace
	BoundNmBox.Enabled = True
	BoundItmBox.Enabled = True
	BoundItmBox.Color = &HFFFFFF
	OpenPlnCheck.Enabled = False
	OpenNewWindCheck.Enabled = False
	OpenWExCheck.Enabled = False
	OpenWCmdLine.Enabled = False
	ChangeBoundBtn.Enabled = False
END IF
'That should about fix it! 
END SUB

SUB SelectFileFolder
'Choose a file or folder for the edit bind form... 
SELECT CASE Bound(BindingListBox.ItemIndex+1).BoundType
	CASE bExec
		'Select file... 
		OpenDlg.Filter = "Executable files (*.exe,*.com,*.bat)|*.exe;*.com;*.bat|All Files (*.*)|*.*"
		OpenDlg.FilterIndex = 1
		OpenDlg.Caption = "What do you now want to run today?"
		OpenDlg.FileName = BoundItmBox.Text
		IF OpenDlg.Execute THEN
			BoundItmBox.Text = OpenDlg.FileName
		END IF
	CASE bFile
		'Select file... 
		OpenDlg.Filter = "All Files (*.*)|*.*|Picture files (*.bmp,*.gif,*.jpg,*.pcx,*.wmf)|*.bmp;*.gif;*.jpg;*.pcx;*.wmf|Music files (*.wav,*.mid,*.rmi,*.mp3)|*.wav;*.mid;*.rmi;*.mp3|Text files (*.doc,*.rtf,*.txt)|*.doc;*.rtf;*.txt"
		OpenDlg.FilterIndex = 1
		OpenDlg.Caption = "Pick another cool file to open..."
		OpenDlg.FileName = BoundItmBox.Text
		IF OpenDlg.Execute THEN
			BoundItmBox.Text = OpenDlg.FileName
		END IF
	CASE bFold
		'Select folder... 
		Dummy$ = BrowseForFolder(OptionsForm.Handle, "Choose a folder to have a look at...")
		IF Dummy$ = "" THEN
			'Do nothing... 
		ELSE
			BoundItmBox.Text = Dummy$
		END IF
	CASE bWeb
		OpenDlg.Filter = "Web pages (*.htm,*.html)|*.htm*|Shortcuts (*.url,)|*.url|All Files (*.*)|*.*"
		OpenDlg.FilterIndex = 1
		OpenDlg.Caption = "Let's look at a web page!"
		IF OpenDlg.Execute THEN
			BoundItmBox.Text = "file://"+OpenDlg.FileName
		END IF
END SELECT
END SUB

SUB StartImport
'Get the import form ready! 
'Ask for which file, and list the bindings in the file. 
OpenDlg.Caption = "International imports... from which file?"
OpenDlg.Filter = "CFW Files (*.cfw)|*.cfw|All Files (*.*)|*.*"
OpenDlg.FilterIndex = 1
OpenDlg.FileName = ""
IF OpenDlg.Execute THEN
	'Clear the array... 
	FOR Dummy = 1 TO 200
		ImportStrings$(Dummy) = ""
	NEXT Dummy
	ImportForm.Show
	'Open the file and read from it. 
	IF FileExists(OpenDlg.FileName) THEN
		Files.Open(OpenDlg.FileName,fmOpenRead)
		Dummy2 = 0
		ImportListBox.Clear
		FOR Dummy = 1 TO Files.LineCount
			IF Files.EOF THEN EXIT FOR
			Dummy$ = Files.ReadLine
			IF UCASE$(LEFT$(Dummy$,4)) = "BIND" THEN
				'It's a bound item... 
				'Record it... 
				ImportStrings$(Dummy2) = Dummy$
				'Split it... 
				SplitString(Dummy$)
				'Stick it in the list box... 
				ImportListBox.AddItems(Commands$(3))
				ImportListBox.AddSubItem(Dummy2,Commands$(2))
				IF VAL(Commands$(4)) = bExec THEN ImportListBox.AddSubItem(Dummy2,"Executable")
				IF VAL(Commands$(4)) = bFile THEN ImportListBox.AddSubItem(Dummy2,"File")
				IF VAL(Commands$(4)) = bFold THEN ImportListBox.AddSubItem(Dummy2,"Folder")
				IF VAL(Commands$(4)) = bWeb THEN ImportListBox.AddSubItem(Dummy2,"Web page")
				IF VAL(Commands$(4)) = bCPanel THEN ImportListBox.AddSubItem(Dummy2,"C. Panel")
				IF VAL(Commands$(4)) = bCust THEN ImportListBox.AddSubItem(Dummy2,"Custom")
				ImportListBox.AddSubItem(Dummy2,Commands$(5))
				ImportListBox.Item(Dummy2).ImageIndex = VAL(Commands$(4))-1
				Dummy2 = Dummy2 + 1
			END IF
		NEXT Dummy
		Files.Close
	END IF
ELSE
	'Do nothing! 
END IF
END SUB

SUB CancelImport
ImportForm.Visible = False
UpdateBindEditor
END SUB

SUB DoImport
Dummy2 = CountTotalEntries(0)
FOR Dummy = 0 TO (ImportListBox.ItemCount-1)
	IF ImportListBox.Item(Dummy).Selected = True THEN
		Dummy$ = ImportStrings$(Dummy)
		SplitString(Dummy$)
		'Error check the files/folders now... 
		Dummy5 = True 'This is to say wether we add it or not... 
		SELECT CASE VAL(Commands$(4))
			CASE IS < 2
				IF FileExists(Commands$(5)) THEN
					IF CMessageBox("WARNING: FATAL ERROR! The binding that you are trying to import links to a file that does not exist! The binding is "+Commands$(3)+", with a bound name of "+Commands$(4)+", and the file in question is "+Commands$(5)". Click yes to add it anyway, or no to not add it.",bmWarning, btYesNo) = mrNo THEN
						'No, don't add it... 
						Dummy5 = False
					ELSE
						Dummy5 = True
					END IF
				END IF
			CASE 2
				IF DirExists(Commands$(5)) THEN
					IF CMessageBox("Uh oh... This is not good! The binding you are trying to import links to a folder that does not exist! The binding is "+Commands$(3)+", with a bound name of "+Commands$(4)+", and the folder in question is "+Commands$(5)". Click yes to add it anyway, or no to not add it.",bmWarning,btYesNo) = mrNo THEN
						'No, don't add it... 
						Dummy5 = False
					ELSE
						Dummy5 = True
					END IF
				END IF
			CASE ELSE
				'I guess that I can't check this. So no need to! 
		END SELECT

		'Ok, check if that bound name already exists. Check for more duplicates while your at it... 
		FOR Dummy6 = 1 TO CountTotalEntries(0)-1
			'Checks we just run... 
			IF RTRIM$(Bound(Dummy6).BoundName) = Commands$(2) THEN
				Dummy2$ = "A minor slip up, I'm sure, but please correct me if I am wrong - but it looks like the binding you are trying to import has the same bound name as an existing binding. If you add it, you won't be able to run it, but you should go into the bind editor and change it as soon as you can. Should you not add it... well, I think you know what happens if you don't add it."+CHR$(13)
				Dummy2$ = Dummy2$ + "Stats - Item to be imported | Currently bound item"+CHR$(13)
				Dummy2$ = Dummy2$ + "Bound Name in question: "+Commands$(2)+CHR$(13)
				Dummy2$ = Dummy2$ + "Real Name: "+Commands$(3)+" | "+RTRIM$(Bound(Dummy6).RealName)+CHR$(13)
				Dummy2$ = Dummy2$ + "File bound: "+Commands$(5)+" | "+RTRIM$(Bound(Dummy6).BoundFile)+CHR$(13)
				Dummy2$ = Dummy2$ + "So please make your choice: bind this anyway or not?"
				IF CMessageBox(Dummy2$,bmWarning,btYesNo) = mrNo THEN
					Dummy5 = False
				ELSE
					Dummy5 = True
				END IF
			END IF
			IF RTRIM$(Bound(Dummy6).RealName) = Commands$(3) THEN
				Dummy2$ = "Oh, NO! NO! IT'S COMING... Oops... I should be progamming, not watching a movie. Still, you disturbed me! Anyway, It's come to my attention that the item you are trying to import has the same real name as a currently bound item."+CHR$(13)
				Dummy2$ = Dummy2$ + "Stats - Item to be imported | Currently bound item"+CHR$(13)
				Dummy2$ = Dummy2$ + "Bound Name: "+Commands$(2)+" | "+Bound(Dummy6).BoundName+CHR$(13)
				Dummy2$ = Dummy2$ + "Real Name in question: "+Commands$(3)+CHR$(13)
				Dummy2$ = Dummy2$ + "File bound: "+Commands$(5)+" | "+RTRIM$(Bound(Dummy6).BoundFile)+CHR$(13)
				Dummy2$ = Dummy2$ + "Do you want to add the item anyway?"
				IF CMessageBox(Dummy2$,bmWarning,btYesNo) = mrNo THEN
					Dummy5 = False
				ELSE
					Dummy5 = True
				END IF
			END IF
			IF RTRIM$(Bound(Dummy6).BoundFile) = Commands$(5) THEN
				Dummy2$ = "You hear the screech of tires and turn around to see two cars collide head on. Metal and body parts are flicked eveywhere over the surrounding area. Well, maybe not, but there's a much more sutle collision which is currently affecting this. It looks like the item bound to the command you are trying to import is already bound to something else!"+CHR$(13)
				Dummy2$ = Dummy2$ + "Stats - Item to be imported | Currently bound item"+CHR$(13)
				Dummy2$ = Dummy2$ + "Bound Name: "+Commands$(2)+" | "+Bound(Dummy6).BoundName+CHR$(13)
				Dummy2$ = Dummy2$ + "Real Name: "+Commands$(3)+" | "+RTRIM$(Bound(Dummy6).RealName)+CHR$(13)
				Dummy2$ = Dummy2$ + "Item in question: "+Commands$(5)+CHR$(13)
				Dummy2$ = Dummy2$ + "Make your choice... Import or not?"
				IF CMessageBox(Dummy2$,bmWarning,btYesNo) = mrNo THEN
					Dummy5 = False
				ELSE
					Dummy5 = True
				END IF
			END IF
		NEXT Dummy6

		'Actually do the import... 
		IF Dummy5 = True THEN
			Bound(Dummy2).BoundName = Commands$(2)
			Bound(Dummy2).RealName = Commands$(3)
			Bound(Dummy2).BoundType = VAL(Commands$(4))
			Bound(Dummy2).BoundFile = Commands$(5)
			Bound(Dummy2).ExtraInfo = VAL(Commands$(6))
			'Ok, done import. 
			Dummy2 = Dummy2 + 1
		END IF
	END IF
NEXT Dummy
END SUB