Option Explicit

'Create an array to hold the image names, tree names, and tree codes
'for the different tree species
Dim theTrees(26,2)
theTrees(0,0) = "LondonPlane.bmp"
theTrees(1,0) = "NorwayMaple.bmp"
theTrees(2,0) = "SugarMaple.bmp"
theTrees(3,0) = "RedMaple.bmp"
theTrees(4,0) = "SycamoreMaple.bmp"
theTrees(5,0) = "SilverMaple.bmp"
theTrees(6,0) = "Hawthorn.bmp"
theTrees(7,0) = "PinOak.bmp"
theTrees(8,0) = "RedOak.bmp"
theTrees(9,0) = "Sweetgum.bmp"
theTrees(10,0) = "Tuliptree.bmp"
theTrees(11,0) = "WillowOak.bmp"
theTrees(12,0) = "BlackTupelo.bmp"
theTrees(13,0) = "JapaneseFloweringCherry.bmp"
theTrees(14,0) = "Linden.bmp"
theTrees(15,0) = "Hackberry.bmp"
theTrees(16,0) = "CalleryPear.bmp"
theTrees(17,0) = "JapaneseZelkova.bmp"
theTrees(18,0) = "AmericanElm.bmp"
theTrees(19,0) = "Hophornbeam.bmp"
theTrees(20,0) = "Ginkgo.bmp"
theTrees(21,0) = "Ash.bmp"
theTrees(22,0) = "Ailanthus.bmp"
theTrees(23,0) = "JapaneseSophora.bmp"
theTrees(24,0) = "Honeylocust.bmp"
theTrees(25,0) = "Unknown.bmp"
theTrees(26,0) = "Other.bmp"

theTrees(0,1) = "London Plane"
theTrees(1,1) = "Norway Maple"
theTrees(2,1) = "Sugar Maple"
theTrees(3,1) = "Red Maple"
theTrees(4,1) = "Sycamore Maple"
theTrees(5,1) = "Silver Maple"
theTrees(6,1) = "Hawthorn"
theTrees(7,1) = "Pin Oak"
theTrees(8,1) = "Red Oak"
theTrees(9,1) = "Sweetgum"
theTrees(10,1) = "Tuliptree"
theTrees(11,1) = "Willow Oak"
theTrees(12,1) = "Black Tupelo"
theTrees(13,1) = "Japanese Flowering Cherry"
theTrees(14,1) = "Linden"
theTrees(15,1) = "Hackberry"
theTrees(16,1) = "Callery Pear"
theTrees(17,1) = "Japanese Zelkova"
theTrees(18,1) = "American Elm"
theTrees(19,1) = "Hophornbeam"
theTrees(20,1) = "Ginkgo"
theTrees(21,1) = "Ash"
theTrees(22,1) = "Ailanthus"
theTrees(23,1) = "Japanese Sophora"
theTrees(24,1) = "Honeylocust"
theTrees(25,1) = "Unknown"
theTrees(26,1) = "Other"

theTrees(0,2) = "PLAC"
theTrees(1,2) = "ACPL"
theTrees(2,2) = "ACSA2"
theTrees(3,2) = "ACRU"
theTrees(4,2) = "ACPS"
theTrees(5,2) = "ACSA1"
theTrees(6,2) = "CRPH"
theTrees(7,2) = "QUPA"
theTrees(8,2) = "QURU"
theTrees(9,2) = "LIST"
theTrees(10,2) = "LITU"
theTrees(11,2) = "QUPH"
theTrees(12,2) = "NYSY"
theTrees(13,2) = "PRSE"
theTrees(14,2) = "TICO"
theTrees(15,2) = "CEOC"
theTrees(16,2) = "PYCA"
theTrees(17,2) = "ZESE"
theTrees(18,2) = "ULAM"
theTrees(19,2) = "OSVI"
theTrees(20,2) = "GIBI"
theTrees(21,2) = "PRPE"
theTrees(22,2) = "AIAL"
theTrees(23,2) = "SOJA"
theTrees(24,2) = "GLTR"
theTrees(25,2) = "UNK"
theTrees(26,2) = "OTHER"

'Global variables used in form initialization
Dim hasVisited
Dim firstRun
Dim firstLoad
Dim otherTreeValue

'Global variable used for the current image selected
'Dim theCurrentImage
'Global variable to hold reference to the EditForm object
Dim objEditForm
Set objEditForm = Layer.Forms("EDITFORM")
'Global variable to hold reference to the IdentifyForm object
Dim objIdForm
Set objIdForm = Layer.Forms("IDENTIFYFORM")
'Global variable to hold reference to the DBH entry form object
Dim objDBHForm
Set objDBHForm = Layer.Forms("frmDBH")

'Performs any desired initialization required when the IDENTIFY FORM loads
Sub InitializeIdForm
	Dim theTreeCodeControl
	Dim theTreeImageControl
	Dim theTreeNameControl
	Set theTreeCodeControl = objIdForm.Pages("treeTab1").Controls("txtTreeCode")
	Set theTreeImageControl = objIdForm.Pages("treeTab1").Controls("imgTreeIdImage")
	Set theTreeNameControl = objIdForm.Pages("treeTab1").Controls("lblTreeName")
	Dim theImageNameId
	Dim i
	For i=0 To 26
		If theTreeCodeControl.value = theTrees(i,2) Then
			theImageNameId = i
		End If
	Next
	If theImageNameId = "" Then
		theImageNameId = 26
	End If
	theTreeImageControl.value = theTrees(theImageNameId,0)
	If theImageNameId = 26 Then
		theTreeNameControl.value = theTreeCodeControl.value
	Else
		theTreeNameControl.value = theTrees(theImageNameId,1)
	End If
	theTreeCodeControl.visible = False
End Sub

'Performs any desired initialization required when the EDIT FORM loads
Sub InitializeEditForm
	'Form is visited for the first time
	firstRun = True
	hasVisited = True
	Dim theCurrentImage
	Dim theTextSliderControl
	Dim theTreeCodeControl
	Dim theOtherSpeciesControl
	Dim theImgTreeImageControl
	Dim theLblTreeNameControl
	Dim theSliderControl
	Dim theDBHControls
	Dim theDBHEntryControls
	
	Set theTextSliderControl = objEditForm.Pages("treeTab1").Controls("txtSliderValue")
	Set theTreeCodeControl = objEditForm.Pages("treeTab1").Controls("txtTreeCode")
	Set theOtherSpeciesControl = objEditForm.Pages("treeTab1").Controls("txtOtherSpecies")
	Set theImgTreeImageControl = objEditForm.Pages("treeTab1").Controls("imgTreeImage")
	Set theLblTreeNameControl = objEditForm.Pages("treeTab1").Controls("lblTreeName")
	Set theSliderControl = objEditForm.Pages("treeTab1").Controls("sldTreeImage")
	
	For i=1 To 6
		Set theDBHControls = objEditForm.Pages("treeTab1").Controls.Item(i)
		theDBHControls.Visible = False
	Next
	For i=1 To 6
		Set theDBHEntryControls = objDBHForm.Pages("DBHTab1").Controls.Item(i)
		theDBHEntryControls = theDBHControls.Value
	Next
	
	theTreeCodeControl.visible = False
	theTextSliderControl.visible = False
	theOtherSpeciesControl.Enabled = False
	
	Dim i
	If objEditForm.Mode = 2 Then
		For i=0 To 26
			If theTreeCodeControl.Value = theTrees(i,2) Then
				theCurrentImage = i
			End If
		Next
	End If
	
	If objEditForm.Mode = 3 Then
		theCurrentImage = 0
	End If
	
	If theCurrentImage = "" Then
		theCurrentImage = 26
	End If
	If theCurrentImage = 26 Then
		theLblTreeNameControl.value = theTreeCodeControl.Value
		theOtherSpeciesControl.Value = theTreeCodeControl.Value
	Else
		theLblTreeNameControl.value = theTrees(theCurrentImage,1)
	End If
	
	theImgTreeImageControl.Value = theTrees(theCurrentImage,0)
	'Check if this form is being opened for the first time
	If firstRun = False Then
		theLblTreeNameControl.Value = theTrees(theCurrentImage,1)
	End If
	theSliderControl.Value = theCurrentImage
	'Check if this form is being opened for the first time
	If firstRun = True Then
		theTreeCodeControl.Value = theLblTreeNameControl.Value
	Else
		theTreeCodeControl.Value = theTrees(theCurrentImage,2)
	End If
	theTextSliderControl.Value = theCurrentImage
	'Form has been opened previously
	firstRun = False
End Sub

'Performs any desired initialization required when the tree layer is added to the map
Sub InitializeTreeLayer
	Dim theControl
	Set theControl = ThisEvent.Object
	theControl.Editable = True
End Sub

'Change the picture of the selected species as the
'user changes the slider value
Sub ChangePicture
	If objEditForm.Mode <> 2 Or hasVisited = True Then
		Dim theCurrentImage
		Dim thePictureControl
		Dim theLabelControl
		Dim theTreeCodeControl
		Dim theSliderTextControl
		Dim theSliderControl
		
		Set thePictureControl = objEditForm.Pages("treeTab1").Controls("imgTreeImage")
		Set theLabelControl = objEditForm.Pages("treeTab1").Controls("lblTreeName")
		Set theSliderTextControl = objEditForm.Pages("treeTab1").Controls("txtSliderValue")
		Set theTreeCodeControl = objEditForm.Pages("treeTab1").Controls("txtTreeCode")
		Set theSliderControl = objEditForm.Pages("treeTab1").Controls("sldTreeImage")

		theCurrentImage = theSliderControl.Value
		thePictureControl.value = theTrees(theCurrentImage,0)
		'Check to see if the form is being opened for the first time
		If firstRun = False Then
			theLabelControl.value = theTrees(theCurrentImage,1)
			theTreeCodeControl.value = theTrees(theCurrentImage,2)
		End If
		firstLoad = False
	End If
End Sub

'If there is a pit at the location, enable the other
'associated controls (pit condition And pit size)
Sub EnableCondition
	Dim thePitConditionControl
	Dim thePitSizeControl
	Set thePitConditionControl = objEditForm.Pages("treeTab3").Controls("cboPitCondition")
	Set thePitSizeControl = objEditForm.Pages("treeTab3").Controls("txtPitSize")
	thePitConditionControl.enabled = True
	thePitSizeControl.enabled = True
End Sub

'If there is a pit at the location, disable the other
'associated controls (pit condition And pit size)
Sub DisableCondition
	Dim thePitConditionControl
	Dim thePitSizeControl
	Set thePitConditionControl = objEditForm.Pages("treeTab3").Controls("cboPitCondition")
	Set thePitSizeControl = objEditForm.Pages("treeTab3").Controls("txtPitSize")
	'thePitConditionControl.Clear

	thePitConditionControl.AddItem "Good","Good"

	thePitSizeControl.Value = 0
	thePitConditionControl.enabled = False
	thePitSizeControl.enabled = False
End Sub

'Prompt the user to select a digital picture taken with a camera
'The image name is placed in the shapefile
Sub SelectPhotoFile	
Dim theTxtPictureImageControl
Dim theImgPictureImageControl
Set theTxtPictureImageControl = objEditForm.Pages("treeTab4").Controls("txtPictureImage")
Set theImgPictureImageControl = objEditForm.Pages("treeTab4").Controls("imgPictureImage")
'Allow the user to select a .jpg or .bmp file for the tree photo
	Dim strFilter, strTitle, lngFlags, strResult
	'Set the arguments of the Open dialog box
	strFilter = "JPG Files|*.jpg|BMP Files|*.bmp"
	strTitle = "Select Tree Photo"
	lngFlags = &H1000   'only allow existing files to be specified
	'Show the Open dialog box and get the result
	strResult = CommonDialog.ShowOpen(,strFilter,strTitle,lngFlags)
	If Not IsEmpty(strResult) Then  
		theTxtPictureImageControl.Value = strResult
		theImgPictureImageControl.Value = strResult
	End If
End Sub

'Validate the numeric fields. Must be positive value
' If the value is positive but > the max value, confirm correctness
Sub ValidateValue(theValue, theObject)
	If ThisEvent.Object.Visible Then
		Dim dblMaxValue
		Dim theMessageObject
		dblMaxValue = theValue
		theMessageObject = theObject
		Dim dblEnteredValue
		dblEnteredValue = CDbl(ThisEvent.Object.Value)
		If dblEnteredValue <=0 Then
			ThisEvent.Result = False
			ThisEvent.MessageText = "Please enter a positive value for the " & _
			theMessageObject & "."
			ThisEvent.MessageType = vbExclamation
		End If
		If dblEnteredValue > dblMaxValue Then
			ThisEvent.Result = False
			ThisEvent.MessageText = CStr(dblEnteredValue) & " seems high for the " & _
			theMessageObject & " of a tree." & _
			" Are you sure you want to enter this value?"
			ThisEvent.MessageType = vbExclamation + vbYesNo
		End If
	End If
End Sub

'Check to see if a user specified species is required
Sub SetOtherSpecies
	Dim theObject
	Dim theOtherSpeciesControl
	Dim theTreeCodeControl
	Dim theLabelControl
	Set theObject = ThisEvent.object
	Set theOtherSpeciesControl = objEditForm.Pages("treeTab1").Controls("txtOtherSpecies")
	Set theTreeCodeControl = objEditForm.Pages("treeTab1").Controls("txtTreeCode")
	Set theLabelControl = objEditForm.Pages("treeTab1").Controls("lblTreeName")
	'If the species is not present in the provided list, prompt the user to enter it by hand
	If theObject.value = "OTHER" Then
		theOtherSpeciesControl.Visible = True
		theOtherSpeciesControl.Enabled = True
		theOtherSpeciesControl.Value = "Enter Species Name"
	Else
		theOtherSpeciesControl.Visible = False
		theOtherSpeciesControl.Enabled = False
		theOtherSpeciesControl.Value = theTreeCodeControl.Value
	End If
	If firstLoad = True And otherTreeValue <> "" Then
		theLabelControl.Value = otherTreeValue
		theOtherSpeciesControl.Value = otherTreeValue
		otherTreeValue = ""
	End If
End Sub

'Open a new form to display sample tree density images
Sub OpenDensity
	Layer.Forms("frmDensityForm").Show
End Sub

'Populate the image control with the density images
Sub SetDensityImage
	Dim theObject
	Dim theDensityImageControl
	Set theObject = Layer.Forms("frmDensityForm")
	Set theDensityImageControl = theObject.Pages("treeTab1").Controls("imgDensity")
	theDensityImageControl.Value = "foliageDensity.bmp"
End Sub

'Reset variable used to determine state of form
Sub ResetVisited
	hasVisited = False
End Sub

'Open a new form to display the entry form for DBH values
Sub OpenDBH
	If ThisEvent.Object.Value = "" Then
		msgbox "Please select a value from 1 to 6"
	Else
		Layer.Forms("frmDBH").Show
	End If
End Sub

'Performs any desired initialization required when the DBH entry FORM loads
Sub InitializeDBHForm
	Dim theObject
	Dim theDBHControl
	Dim theDBHNumControl
	Dim i
	
	Set theObject = ThisEvent.Object
	Set theDBHNumControl = ObjEditForm.Pages("TreeTab1").Controls("cboTrunks")
	
	For i=1 To (6-theDBHNumControl.Value)
		Set theDBHControl = theObject.Pages("DBHTab1").Controls.Item(7-i)
		theDBHControl.Visible = False
	Next
	For i=1 To (6-theDBHNumControl.Value)
		Set theDBHControl = theObject.Pages("DBHTab1").Controls.Item(13-i)
		theDBHControl.Visible = False
	Next
End Sub

'Update the values in the EDIT form based on entries in the DBH entry form
Sub UpdateDBH(theDBHControlNumber)
	Dim theObject
	Dim theDBHControl
	Dim theControlNumber
	
	theControlNumber = theDBHControlNumber
	Set theObject = ThisEvent.Object
	Set theDBHControl = objEditForm.Pages("treeTab1").Controls.Item(theControlNumber)
	theDBHControl.Value = theObject.Value
End Sub

'Sets the slider value
Sub SetCorrectSliderValue
	Dim theTreeCode, theSliderControl
	Set theSliderControl = objEditForm.Pages("treeTab1").Controls("sldTreeImage")
	theTreeCode = objEditForm.Fields("SPECIES").Value
	theSliderControl.Value = GetCorrectSliderValue(theTreeCode)
	
	Call ChangePicture
End Sub

'Based on input tree code, gets the slider value
Function GetCorrectSliderValue (p_strTreeCode)
	Select Case p_strTreeCode
		Case "PLAC"
			GetCorrectSliderValue = 0
		Case "ACPL"
			GetCorrectSliderValue = 1
		Case "ACSA2"
			GetCorrectSliderValue = 2
		Case "ACRU"
			GetCorrectSliderValue = 3
		Case "ACPS"
			GetCorrectSliderValue = 4
		Case "ACSA1"
			GetCorrectSliderValue = 5
		Case "CRPH"
			GetCorrectSliderValue = 6
		Case "QUPA"
			GetCorrectSliderValue = 7
		Case "QURU"
			GetCorrectSliderValue = 8
		Case "LIST"
			GetCorrectSliderValue = 9
		Case "LITU"
			GetCorrectSliderValue = 10
		Case "QUPH"
			GetCorrectSliderValue = 11
		Case "NYSY"
			GetCorrectSliderValue = 12
		Case "PRSE"
			GetCorrectSliderValue = 13
		Case "TICO"
			GetCorrectSliderValue = 14
		Case "CEOC"
			GetCorrectSliderValue = 15
		Case "PYCA"
			GetCorrectSliderValue = 16
		Case "ZESE"
			GetCorrectSliderValue = 17
		Case "ULAM"
			GetCorrectSliderValue = 18
		Case "OSVI"
			GetCorrectSliderValue = 19
		Case "GIBI"
			GetCorrectSliderValue = 20
		Case "PRPE"
			GetCorrectSliderValue = 21
		Case "AIAL"
			GetCorrectSliderValue = 22
		Case "SOJA"
			GetCorrectSliderValue = 23
		Case "GLTR"
			GetCorrectSliderValue = 24
		Case "UNK"
			GetCorrectSliderValue = 25
		Case Else
			GetCorrectSliderValue = 26
	End Select
	If GetCorrectSliderValue = 26 Then
		Dim theTreeCode
		theTreeCode = objEditForm.Fields("SPECIES").Value
		otherTreeValue = theTreeCode
	End If
	firstLoad = True
End Function

Sub CheckImage
	Dim thePictureImage, thePictureText
	Set thePictureimage = objEditForm.Pages("treeTab4").Controls("imgPictureImage")
	Set thePictureText = objEditForm.Pages("treeTab4").Controls("txtPictureImage")
	If thePictureText.Value <> "" Then
		thePictureImage.Value = thePictureText.Value
	End If
End Sub