﻿Type=Activity
Version=2.00
FullScreen=False
IncludeTitle=True
@EndOfDesignText@
'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Dim pnlInput, pnlSelect As Panel

	'Vacation Display Panel Objects
	Dim Panel1 As Panel
	Dim spShipName As Spinner
	Dim spVendor As Spinner
	Dim txtBookingNbr As EditText
	Dim txtDescription As EditText 
	Dim txtCabin As EditText
	Dim lblDepartDate As Label
	Dim lblReturnDate As Label
	Dim lblReturnDateCaption As Label
	Dim txtDescription As EditText
	Dim lblDepositDue As Label
	Dim lblFinalDue As Label
	Dim lblKeyboardDisplay As Label
	Dim txtGeneralNotes As EditText
	Dim lblPaxCount As Label
	Dim txtVacationCost As EditText
	Dim btnUpdate As Button
	Dim btnViewItin As Button
	Dim lblCruiseShipCaption As Label
	Dim lblDays As Label
	Dim pnlKeyboard As Panel
	
	'Miscellaneous Working-Storage
	Dim DateDialog As DateDialog
	Dim NbrDialog As NumberDialog 
	Dim mapVendor As Map
	Dim mapShipName As Map
	
	Dim SQL1 As SQL
	Dim Cursor1 As Cursor

	Dim returnedKeyValue As String
	Dim enterKeyPressed As Boolean
	
	Dim IME As IME
	
	Dim targetDirectory As String

End Sub

Sub Activity_Create(FirstTime As Boolean)

	Activity.LoadLayout("VacationDisplay")	
	
	If FirstTime = True Then
		Main.targetDirectory = DBUtils.CopyDBFromAssets ("dCruiseVacation.sqlite")
		SQL1.Initialize (Main.targetDirectory, "dCruiseVacation.sqlite", True)
		LoadVendorFromTable
	End If

	lblCruiseShipCaption.Visible = False
	spShipName.Visible = False
	
	lblReturnDateCaption.Visible = False
	lblReturnDate.Text = ""

End Sub

Sub Activity_Resume

	Dim VendorShipRowId As String

	If SQL1.IsInitialized = False Then
		SQL1.Initialize (Main.targetDirectory, "dCruiseVacation.sqlite", True)
		LoadVendorFromTable
	End If
	
	Log(Main.iVacationRowId)
	
	If Main.iVacationRowId > 0 Then

	Log("RowID > 0")
		Cursor1 = SQL1.ExecQuery("SELECT * from tVacations Where VacationRowId = '" & Main.iVacationRowId & "'")
	
		If Cursor1.RowCount > 0 Then
			For i = 0 To Cursor1.RowCount - 1        
				Cursor1.Position = i        
				txtDescription.Text = Cursor1.GetString("Descr")
				lblDepartDate.Text =  Cursor1.GetString("DepartDate")
				lblReturnDate.Text = Cursor1.GetString("ReturnDate")
				spVendor.SelectedIndex = Cursor1.GetInt("VendorRowId")
				lblPaxCount.Text = Cursor1.GetInt("PaxCount")
				lblDays.Text = Cursor1.GetInt("NbrDays")
				txtGeneralNotes.Text = Cursor1.GetString("GeneralNotes")
				txtBookingNbr.Text = Cursor1.GetString("Confirmation")
				txtCabin.Text = Cursor1.GetString("Stateroom")

				If Cursor1.GetString("TotalCost") = "" Then 
					txtVacationCost.Text = NumberFormat2(Cursor1.GetString("TotalCost"),0,2,2,False)
				Else
					txtVacationCost.Text = NumberFormat2(0,0,2,2,False)
				End If
				
				If Cursor1.GetString("DepositDueDate") <> "0" Then			
					lblDepositDue.Text = Cursor1.GetString("DepositDueDate")
				End If
				
				If Cursor1.GetString("FinalDueDate") <> "0" Then			
					lblFinalDue.Text = Cursor1.GetString("FinalDueDate")
				End If
				
				lblReturnDateCaption.Visible = True
				lblReturnDate.Visible = True

		' Determine which Ship Name to Display
		
				VendorShipRowId = Cursor1.GetString("VendorShipRowId")
				spVendor_ItemClick(VendorShipRowId,spVendor.SelectedItem)
				
				For x = 0 To mapShipName.Size - 1
					If mapShipName.GetValueAt(x)  = VendorShipRowId Then
						spShipName.SelectedIndex = x
					End If
				Next
				
			Next    
		
		Cursor1.Close
		
		End If
		
	End If
	
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean    

	If KeyCode = KeyCodes.KEYCODE_BACK Then        
		If Msgbox2("Exit Program?", "EXIT", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then             
			'Activity.finish        
		Else             
			Return True        
		End If    
	End If
	
End Sub

Sub spVendor_ItemClick (Position As Int, Value As Object)
	
	Dim id As String    
	id = mapVendor.Get(Value)
	
	'If user pressed the "SELECT .... " option, Make Cruise Ships Invisible.
	If id = 0 Then 
		lblCruiseShipCaption.Visible = False
		spShipName.Visible = False
		Return
	End If
		
	spShipName.Clear
	mapShipName.Initialize
	
	lblCruiseShipCaption.Visible = True
	spShipName.Visible = True
	spShipName.Add("** Select Cruise Ship **")
	mapShipName.Put("** Select Cruise Ship **",0)
	
	Cursor1 = SQL1.ExecQuery("SELECT VendorShipRowId, Descr from tVendorShips WHERE VendorRowId = '" & id & "' ORDER BY DESCR")   
	
	If Cursor1.RowCount > 0 Then
		For i = 0 To Cursor1.RowCount - 1        
			Cursor1.Position = i        
			spShipName.Add(Cursor1.GetString("Descr"))
			mapShipName.Put(Cursor1.GetString("Descr"),Cursor1.GetString("VendorShipRowId"))
		Next    
	End If
	
	Cursor1.Close
	
	IME.SetCustomFilter(txtVacationCost, txtVacationCost.INPUT_TYPE_NUMBERS, "0123456789.")
	
End Sub

Sub btnViewItin_Click
	
End Sub
Sub btnUpdate_Click
	
	Dim dTicksDepartDate As Long
	Dim dTicksDepositDue As Long
	Dim dTicksFinalDue As Long
	
	'Perform Validations
	
	' ** Required Fields **
	
	If txtDescription.Text = "" Then
		rc = Msgbox2("Please Enter a Description for Your Cruise Vacation.","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		txtDescription.RequestFocus 
		Return
	End If
	
	If spShipName.SelectedIndex = 0 Then
		rc = Msgbox2("Please Select a Cruise Ship from the List.","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		spShipName.RequestFocus 
		Return
	End If

	If lblDepartDate.Text = "" Then
		rc = Msgbox2("Please Enter the Departure Date of Your Vacation.","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblDepartDate.RequestFocus 
		Return 
	End If
	
	If lblDays.Text = "" Then
		rc = Msgbox2("Please Enter the Number Days for Your Vacation.","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblDays.RequestFocus 
		Return
	End If

	If lblDays.Text = "0" Then
		rc = Msgbox2("Zero is Not Allowed, Please Enter the Number of Days for Your Vacation..","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblDays.RequestFocus 
		Return
	End If

	If lblPaxCount.Text = "" Then
		rc = Msgbox2("Please Enter the Number of People on this Trip.","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblPaxCount.RequestFocus 
		Return
	End If

	If lblPaxCount.Text = "0" Then
		rc = Msgbox2("Zero is Not Allowed, Please Enter the Number of People on this Trip.","Required Information is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblPaxCount.RequestFocus 
		Return
	End If

	If lblDepartDate.Text = "" Then
		dTicksDepartDate  = 0
	Else
		dTicksDepartDate = DateTime.DateParse(lblDepartDate.Text)	
	End If
	
	If lblDepositDue.Text = "" Then
		dTicksDepositDue = 0
	Else
		dTicksDepositDue = DateTime.DateParse(lblDepositDue.Text)	
	End If
	
	If lblFinalDue.Text = "" Then
		dTicksFinalDue = 0
	Else
		dTicksFinalDue = DateTime.DateParse(lblFinalDue.Text)	
	End If
	
	If dTicksDepositDue > dTicksDepartDate Then
		rc = Msgbox2("The Deposit Due Date Must Be Before the Departure Date.","Invalid Date Information Entered","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblDepositDue.RequestFocus 
		Return 
	End If

	If dTicksFinalDue> dTicksDepartDate Then
		rc = Msgbox2("The Final Payment Due Date Must Be Before the Departure Date.","Invalid Date Information Entered" ,"OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblFinalDue.RequestFocus 
		Return 
	End If
	
	If dTicksFinalDue < dTicksDepositDue Then
		rc = Msgbox2("The Final Due Date Must Be Greater Than the Deposit Due Date.","Invalid Date Information Entered","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblFinalDue.RequestFocus 
		Return 
	End If
	
	If spVendor.SelectedIndex = 0 Then
		rc = Msgbox2("Please Select a Cruise Line for Your Vacation.","Required Information Is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		spVendor.RequestFocus 
		Return 
	End If

	If spShipName.Visible = True Then
		If spShipName.SelectedIndex = 0 Then
			rc = Msgbox2("Please Select a Cruise Ship for Your Vacation.","Required Information Is Missing","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
			spShipName.RequestFocus 
			Return 
		End If
	End If

'	All Edits Have Passed, Write the Record to the Database

	Dim ListOfMaps As List
	ListOfMaps.Initialize
	
	Dim m As Map
	m.Initialize
	Dim editMap As Map
	editMap.Initialize
	
	If Main.iVacationRowId > 0 Then
		editMap.Put("VacationRowId",Main.iVacationRowId)
	Else
		m.Put("Descr", txtDescription.Text)
		m.Put("DepartDate", lblDepartDate.Text)
		m.Put("ReturnDate", lblReturnDate.Text)
		Dim id As String    
		id = mapVendor.Get(spVendor.SelectedItem)
		m.Put("VendorRowId", id)
		id = mapShipName.Get(spShipName.SelectedItem)
		m.Put("VendorShipRowId", id)
		m.Put("Stateroom",txtCabin.Text)
		m.Put("PaxCount",lblPaxCount.Text)
		m.Put("TotalCost",txtVacationCost.Text)
		m.Put("Confirmation",txtBookingNbr.Text)
		m.Put("BookingDate",DateTime.now)
		m.Put("DepositDueDate",lblDepositDue.Text)
		m.Put("FinalDueDate",lblFinalDue.Text)
		m.Put("GeneralNotes",txtGeneralNotes.Text)
		m.Put("DateTimeCreated",DateTime.Now)
		m.Put("DateTimeUpdated",DateTime.Now)
		m.Put("NbrDays",lblDays.Text)	
		ListOfMaps.Add(m)
	End If

	If Main.iVacationRowId = 0 Then
		'New Record 
		DBUtils.InsertMaps(SQL1, "tVacations", ListOfMaps)
	Else
		'Edit Record
		DBUtils.UpdateRecord(SQL1,"tVacations","Descr", txtDescription.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","DepartDate", lblDepartDate.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","ReturnDate", lblReturnDate.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","VendorRowId", mapVendor.Get(spVendor.SelectedItem),editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","VendorShipRowId", mapShipName.Get(spShipName.SelectedItem),editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","Stateroom", txtCabin.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","PaxCount", lblPaxCount.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","TotalCost", txtVacationCost.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","Confirmation", txtBookingNbr.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","DepositDueDate", lblDepositDue.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","FinalDueDate", lblFinalDue.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","GeneralNotes", txtGeneralNotes.Text,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","DateTimeUpdated", DateTime.Now,editMap)	
		DBUtils.UpdateRecord(SQL1,"tVacations","NbrDays", lblDays.Text,editMap)	
	End If
	
	ToastMessageShow("Your Vacation Has Been Updated",False)
	
	txtDescription.Text = ""
	lblDepartDate.Text = ""
	lblDays.Text = ""
	lblPaxCount.Text = ""
	lblDepositDue.Text = ""
	lblFinalDue.Text = ""
	txtCabin.Text = ""
	txtBookingNbr.Text = ""
	txtVacationCost.Text = ""
	txtGeneralNotes.Text = ""
	spVendor.Clear
	spShipName.Clear
	spShipName.Visible = False

	Activity.Finish
	
End Sub

Sub DateDialogDisplay(strDate As String, HeaderCaption As String, TitleCaption As String) As String

	Dim dateTicks As Long
	dateTicks = 0
	If strDate <> "" Then
		dateTicks = DateTime.DateParse(strDate)	
	End If
			
	Dim RC As Int    
	If dateTicks > 0 Then
		DateDialog.Year = DateTime.GetYear(dateTicks)
	Else
		DateDialog.Year = DateTime.GetYear(DateTime.now)
	End If
	
	If dateTicks > 0 Then
		DateDialog.Month = DateTime.GetMonth(dateTicks)
	Else
		DateDialog.Month = DateTime.GetMonth(DateTime.now)
	End If
	
	If dateTicks > 0 Then
		DateDialog.DayOfMonth = DateTime.GetDayOfMonth(dateTicks)
	Else
		DateDialog.DayOfMonth = DateTime.GetDayOfMonth(DateTime.now)
	End If
	
	RC = DateDialog.Show(HeaderCaption, TitleCaption.ToUpperCase, "Ok", "Cancel", "", Null)    
	If RC =  DialogResponse.POSITIVE  Then        
		Return DateTime.date(DateDialog.dateTicks )
	Else
		Return strDate	
	End If
End Sub
Sub lblDepartDate_Click
	lblDepartDate.Text = DateDialogDisplay(lblDepartDate.Text, "Select Your Departure Date","Departure Date Selection")

Dim dTicksDepartDate As Long
	If lblDays.Text <> "" Then
		dTicksDepartDate = DateTime.DateParse(lblDepartDate.Text)	
		lblReturnDate.Text = DateTime.Date(DateTime.Add(dTicksDepartDate,0,0,lblDays.Text))
		lblReturnDate.Visible = True
		lblReturnDateCaption.Visible = True
	End If

End Sub
Sub lblDepositDue_Click
	lblDepositDue.Text = DateDialogDisplay(lblDepositDue.Text, "Select Your Deposit Due Date","Deposit Due Date Selection")
End Sub
Sub lblFinalDue_Click
	lblFinalDue.Text = DateDialogDisplay(lblFinalDue.Text, "Select Your Final Payment Due Date","Final Due Date Selection")
End Sub
Sub lblDays_LongClick
	lblDays.Text = ""
End Sub
Sub lblDays_Click

	Dim rc As Int
	Dim iDays As Int
	Dim dTicksDepartDate As Long
	rc = NumericKeyboardPopUp("How Many Days is Your Cruise?")
	If rc = -1 Then
		lblDays.Text = lblKeyboardDisplay.Text	
	End If
	
	If lblDays.Text <> "" Then
		iDays = lblDays.Text
	Else
		iDays = -1
	End If

	If iDays = 0 Then 
		rc = Msgbox2("Cannot have Zero Days on Your Cruise. ","Invalid Entry","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblDays_Click
		Return 
	End If
	
	If lblDepartDate.Text <> "" Then
		dTicksDepartDate = DateTime.DateParse(lblDepartDate.Text)	
		lblReturnDate.Text = DateTime.Date(DateTime.Add(dTicksDepartDate,0,0,lblDays.Text))
		lblReturnDate.Visible = True
		lblReturnDateCaption.Visible = True
	End If
	
End Sub

Sub lblPaxCount_LongClick
	lblPaxCount.Text = ""
End Sub
Sub lblPaxCount_Click

	Dim rc As Int
	Dim iPaxCount As Int
	rc = NumericKeyboardPopUp("Enter The Number Of Passengers.")
	If rc = -1 Then
		lblPaxCount.Text = lblKeyboardDisplay.Text	
	End If

	If lblPaxCount.Text <> "" Then
		iPaxCount = lblPaxCount.Text
	Else
		iPaxCount = -1
	End If
		
	If iPaxCount = 0 Then 
		rc = Msgbox2("Cannot have Zero Passengers on Your Cruise.   Values are 1, 2, 3 or 4.","Invalid Entry","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblPaxCount_Click
		Return 
	End If

	If iPaxCount > 4 Then 
		rc = Msgbox2("Too Many Passengers Entered.   Values are 1, 2, 3 or 4.","Over Limit","OK","","",LoadBitmap (File.DirAssets, "Exclamation.bmp"))
		lblPaxCount_Click
		Return 
	End If
	
End Sub
Sub LoadVendorFromTable()

	Cursor1 = SQL1.ExecQuery("SELECT VendorRowId, Descr from tVendor")    
	spVendor.Clear
	mapVendor.Initialize
	
	spVendor.Add("** Select Cruise Line **")
	mapVendor.Put("** Select Cruise Line **",0)

	If Cursor1.RowCount > 0 Then
		For i = 0 To Cursor1.RowCount - 1        
			Cursor1.Position = i        
			spVendor.Add(Cursor1.GetString("Descr"))
			mapVendor.Put(Cursor1.GetString("Descr"),Cursor1.GetString("VendorRowId"))
		Next    
	End If
	
	spVendor.SelectedIndex = 0

End Sub
Sub lblDepositDue_LongClick
	lblDepositDue.Text = ""
End Sub
Sub lblFinalDue_LongClick
	lblFinalDue.Text = ""
End Sub
Sub lblDepartDate_LongClick
	lblDepartDate.Text = ""
End Sub
Sub btnNumKeyboard_Click

	Dim Send As Button
		
	Send = Sender
	
	Select Send.Tag
	
		Case "Clear"
			lblKeyboardDisplay.Text = ""
		Case "ENTER"
			Return
		Case Else
			lblKeyboardDisplay.Text = lblKeyboardDisplay.Text & Send.Tag
		End Select

End Sub
Sub NumericKeyboardPopUp(sCaption As String) As Int

	Dim cd As CustomDialog2
	Dim pnl As Panel
	
	pnl.Initialize("pnl")
	pnl.LoadLayout ("NumericKeyboard.bal")
	cd.AddView(pnl, 20%x, 50%y) ' sizing relative to the screen size is probably best
	ret = cd.Show(sCaption, "Enter", "Cancel", "", Null)		
	Return ret
	
End Sub

