Record Census Data UK.fh_lua

--[[
@Title: Record Census Data UK
@Type: Source-driven Data Entry
@Subtype: "Census Return"
@Author: Jane Taubman
@Contributors: Helen Wright
@Version: 1.8
@Keywords: 
@LastUpdated: August 2023
@GH #17 #18 #20  #30 #44 #52 #57 #112 #119 #121 #132 #134
@Description: 
This plugin allows the entry of details about multiple individuals and creates records for them if needed, as well as recording census, occupation and birth facts. Where religion is recorded in the census, a religion fact is also created.

Supports most surviving UK and Ireland Censuses

England and Wales 1841 - 1921 plus 1939 Register

Scotland 1841 - 1921

Isle of Man 1841 - 1921 (you should create an Isle of Man region)

Channel Islands 1841 - 1921 (you should create a Channel Islands region)

Ireland 1901 - 1911 (No census was taken in 1921 in either Eire or Northern Ireland)

Note:  The Welsh form was slightly different to the English one,  so if the Location selected for the Census does not end in England or Wales in years where there was a difference, the plugin will prompt to ask if the English or Welsh form should be used.
]]

fhInitialise(7)
fh = require("fhUtils")
fh.setIupDefaults()
stringx.import()
require("lfs")

------------------------------------------------------------------------ Define all upvalues (globals)
local pRegions, pCite, iRegion, iYear, ptrHead
local tPtrList = {}
local tMsgList = {}
local tPtrUnlinked = {}
local tActionList = {}
local ptrHead = fhNewItemPtr()
local sPluginName = stringx.strip(fhGetContextInfo("CI_PLUGIN_NAME"))
local tUsedList = { ["BIRT.PLAC"] = fhGetDataList("PLACES") }
------------------------------------------------------------------------ Main processing

function main()
	pCite = fh.loadPreparedCitation()
	pRegions = loadRegions()
	if not pCite.result then
		fh.getParam(sPluginName, pCite.err)
		return
	end
	-- Ensure all needed fields have been completed

	if not (pCite:checkRequired("DT-YEAR", "PL-LOCATION", "NM-HEAD_OF_HOUSEHOLD", "EN-REGION")) then
		fh.getParam(
			sPluginName,
			"Not all required fields set,\nplease ensure you have entered region, year, head of household and location. "
		)
		return
	end
	-- Validate the Type field is supported
	local sRegionName = pCite:getValue("EN-REGION")
	local tRegion

	local dtDate = pCite:getValue("DT-YEAR")
	local dpDate = dtDate:GetDatePt1()
	iYear = dpDate:GetYear()
	-- iYear = tonumber(pCite:getValue('DT-YEAR'):GetDisplayText())

	-- Special Case Processing England and Wales
	local sPlace = pCite:getValue("PL-LOCATION")
	if sRegionName == "England and Wales" and iYear >= 1891 then
		if sPlace:endswith("Wales") then
			sRegionName = "Wales"
		elseif sPlace:endswith("England") then
		else
			local r = fh.getParam(
				sPluginName,
				"Please select England or Wales\nTo skip this prompt in future ensure your location ends with England or Wales",
				{},
				{ "England", "Wales" }
			)
			if r.button_pressed == "Wales" then
				sRegionName = "Wales"
			elseif r.button_pressed == "England" then
			else
				return -- no button pressed
			end
		end
	end

	for k, v in ipairs(pRegions) do
		for k2, v2 in ipairs(v.names) do
			if v2 == sRegionName then
				tRegion = v
				iRegion = k
				pRegions[iRegion]["name"] = sRegionName
			end
		end
	end
	if not tRegion then
		local sMessage = 'Sorry "' .. sRegionName .. '" is not supported.\n\nThe following regions are available:\n'
		for k, v in ipairs(pRegions) do
			sMessage = sMessage .. "\n" .. table.concat(v.names, "\n")
		end
		fh.getParam(sPluginName, sMessage)
		return
	end

	if not tRegion[iYear] then
		local tYears = {}
		for k, v in pairs(tRegion) do
			table.insert(tYears, tonumber(k))
		end
		table.sort(tYears)
		fh.getParam(
			sPluginName,
			'Sorry "'
				.. sRegionName
				.. '" is not supported for '
				.. iYear
				.. ".\n"
				.. table.concat(tYears, " ")
				.. " are available"
		)
		return
	end
	-- If month entered check date against template
	if dpDate:GetMonth() ~= 0 then
		local dtCensusDate = fhNewDate()
		dtCensusDate:SetValueAsText(tRegion[iYear].date)
		if dtCensusDate:Compare(dtDate) ~= 0 then
			fh.getParam(
				sPluginName,
				"The date entered ("
					.. dtDate:GetValueAsText()
					.. ") does not match the Census for "
					.. sRegionName
					.. " in "
					.. iYear
					.. " which was "
					.. dtCensusDate:GetValueAsText()
					.. ".\n If you just specify a year the date will be completed for you on the facts."
			)
			return
		end
	end
	local tCensus = tRegion[iYear]
	pRegions:loadTemplate(iRegion, iYear)
	-------------------------------------------------------------------------------------- Show Main Dialog
	local sHead = "Head of Household: " .. (pCite:getValue("NM-HEAD_OF_HOUSEHOLD") or "")
	sHead = sHead .. "\nPlace: " .. (pCite:getValue("PL-LOCATION") or "")
	sHead = sHead .. "\nAddress: " .. (pCite:getValue("AD-ADDRESS") or "")
	dlg = defineWindow("Enter Source Facts for " .. sRegionName .. " " .. iYear, sHead)
	dlgName = iup.GetName(dlg)
	iup.SetGlobal("PARENTDIALOG", dlgName)

	-- Set Dialog to have FH as a parent and prep any iup prebuilt componments to use the dialog.
	iup.SetAttribute(dlg, "NATIVEPARENT", fhGetContextInfo("CI_PARENT_HWND")) -- Set the parent window handle
	iup.SetHandle("main", dlg)
	iup.SetGlobal("PARENTDIALOG", "main")
	dlg:showxy(iup.CENTERPARENT, iup.CENTERPARENT)
	if iup.MainLoopLevel() == 0 then
		iup.MainLoop()
	end
end
---------------------------------------------------------------------------------------------- define Main Window

function defineWindow(sTitle, sMessage)
	txtbox = iup.multiline({
		expand = "YES",
		size = "200x120",
		readonly = "YES",
		BGCOLOR = "#eeeeee",
		value = "",
	})
	local btnAddLine = iup.button({
		title = "Add Detail Line",
		expand = "YES",
		padding = "4",
	})
	local btnComplete = iup.button({
		title = "Complete and Close",
		expand = "YES",
		padding = "4",
		action = function()
			complete()
			return iup.CLOSE
		end,
	})

	function btnAddLine.action()
		s = doDetail()
		if s then
			txtbox.value = txtbox.value .. s .. "\n"
			txtbox.value = txtbox.value .. "------------------------------------------------------------\n"
			txtbox.SCROLLTOPOS = txtbox.value:len()
		end
		fhUpdateDisplay()
	end
	dlg = iup.dialog({
		title = sTitle,
		dialogframe = "YES",
		size = "400x200",
		resize = "YES",

		iup.vbox({
			alignment = "ACENTER",
			gap = "10",
			margin = "10x10",
			iup.label({ title = sMessage, expand = "YES" }),
			--	lblText,

			iup.hbox({
				btnAddLine,
				btnComplete,
			}),
			iup.hbox({
				txtbox,
			}),
		}),
	})

	function dlg.close_cb()
		if table.getn(tPtrList) > 0 then
			local r = fhMessageBox(
				"Warning:\nInformation already entered has been updated. To undo updates use Edit>Undo Plugin Updates from the menu.",
				"MB_OKCANCEL",
				"MB_ICONSTOP"
			)
			if r == "Cancel" then
				return iup.ignore
			end
		end
		complete()
	end

	iup.SetAttribute(dlg, "NATIVEPARENT", fhGetContextInfo("CI_PARENT_HWND")) -- Set the parent window handle
	iup.SetHandle("iupmain", dlg) -- name the dialog
	iup.SetGlobal("PARENTDIALOG", "iupmain") -- set it to be the default parent for getParam and iup dialogs.

	return dlg
end
------------------------------------------------------------------------------ Replace Text from Source

function complete()
	iCount = pRegions[iRegion][iYear]["template"]["count"] or 0
	if iCount > 0 then
		local rt = fhNewRichText()
		local tx = pRegions[iRegion][iYear].template.body

		-- Remove unused template fields
		tx = tx:gsub("{.-}", "")

		rt:SetText(tx, true, true)
		local ptrTextFromSource = fhGetItemPtr(pCite.source, "~.TEXT")
		if ptrTextFromSource:IsNull() then
			ptrTextFromSource = fhCreateItem("TEXT", pCite.source)
		end

		addResultLine(ptrTextFromSource, "Added")
		fhSetValueAsRichText(ptrTextFromSource, rt)
		if table.getn(tPtrUnlinked) > 0 then
			outputUnrelatedWarning()
		end
		outputResultSet()
	end
end
-------------------------------------------------------------------------------- Output Unrelated Reminder on Finish

function outputUnrelatedWarning()
	local sMessage = "Reminder:\nThe following records have been added as unrelated individuals"
	if #tPtrUnlinked == 1 then
		sMessage = "Reminder:\nThe following record has been added as an unrelated individual"
	end
	for _, pi in ipairs(tPtrUnlinked) do
		if pi:IsNotNull() then
			sMessage = sMessage
				.. "\n"
				.. fhGetDisplayText(pi)
				.. "["
				.. fhGetRecordId(pi)
				.. "] "
				.. " "
				.. fhCallBuiltInFunction("LifeDates", pi)
		end
		pi:MoveNext()
	end
	sMessage = sMessage
		.. '\n\nYou can now attach them to your tree as needed using the "Add Existing Record" option against the correct relatives.  \nRemember to turn on Automatic Source Citation for your prepared citation before commencing to ensure citations are created'
	fh.getParam(sPluginName, sMessage)
end
-------------------------------------------------------------------------------- Output Result Set

function outputResultSet()
	local tblResults = fh.createResultTable()
	-- Define Columns
	tblResults.indi = { title = "Record" }
	tblResults.lifedates = { title = "Life Dates", type = "text", width = 40 }
	tblResults.id = {
		title = "id",
		type = "integer",
		align = "align_right",
		width = 20,
	}
	tblResults.item = { title = "Item", width = 220 }
	tblResults.action = { title = "Action", type = "text", width = 120 }
	tblResults.label = { title = "Item Type", type = "text", width = 120 }
	tblResults.warn = { title = "Warning", type = "text", width = 120 }
	local pRec = fhNewItemPtr()
	for i, pi in ipairs(tPtrList) do
		-- Add Row
		tblResults:newRow()
		pRec:MoveToRecordItem(pi)
		-- Set Columns
		tblResults.item:set(pi:Clone())
		local b = fhCallBuiltInFunction("IsFact", pi)
		if b then
			local w = fhCallBuiltInFunction("GetDataWarning", fhGetItemPtr(pi, "~.DATE"), 1)
			tblResults.warn:set(w)
		end
		tblResults.indi:set(pRec:Clone())
		tblResults.label:set(ptrDescription(pi))
		tblResults.action:set(tMsgList[i] or "")
		if fhGetTag(pRec) == "INDI" then
			tblResults.lifedates:set(fhCallBuiltInFunction("LifeDates", pRec))
		end
		tblResults.id:set(fhGetRecordId(pRec))
		pi:MoveNext()
	end
	local sTitle = fhGetDisplayText(pCite.source) or ""
	fhOutputResultSetTitles(
		sTitle .. " - Updated Items and Records",
		sTitle .. " - Updated Items and Records",
		" Date: %#x"
	)
	tblResults:outputResults()
end
-------------------------------------------------------------------------------- Add information to the "log" memo

function addResultLine(ptr, msg)
	table.insert(tPtrList, ptr:Clone())
	table.insert(tMsgList, msg)
	txtbox.value = txtbox.value .. fhGetDisplayText(ptr) .. " " .. msg .. "\n"
end

function addUnlinked(ptr)
	table.insert(tPtrUnlinked, ptr:Clone())
end
---------------------------------------------------------------------------------------------- Pointer Description

function ptrDescription(ptr)
	local strDesc = ""
	local strTag = fhGetTag(ptr)
	if not (fhHasParentItem(ptr)) then
		strDesc = "Record (" .. fhGetTag(ptr) .. ")"
	else
		local strTitle = string.match(fhGetDisplayText(ptr), "([%a%w%s]*):")
		if not strTitle then
			strTitle = string.match(fhGetDisplayText(ptr), "(%a*)")
		end
		strDesc = strTitle .. " (" .. strTag .. ")"
	end
	return strDesc
end
-------------------------------------------------------------------------------- Add Birth

function birthdlg(sMessage, fDate, fPlace, sName)
	-- birth range
	local dtTo = fhNewDate()
	dtTo:SetValueAsText(pRegions[iRegion][iYear]["date"])
	local dtFrom = fhNewDate(iYear - 110)
	local fields = {
		{
			tag = "BIRT.DATE",
			type = "DATE",
			label = "Birth Date",
			dr = "BIRT.DATE",
			value = fDate,
			range = { dtFrom, dtTo },
		},
		{
			tag = "BIRT.PLAC",
			type = "STRING",
			label = "Where Born",
			dr = "BIRT.PLAC",
			value = fPlace,
		},
	}
	local buttons = { "Add Birth", "Skip" }
	local s = {}
	s["BIRT.PLAC"] = fh.createPlaceList()
	local r = fh.getParam(sPluginName, sMessage, fields, buttons, s)
	if r.ok then
		return r.results["BIRT.PLAC"], r.results["BIRT.DATE"]
	else
		return false
	end
end
--------------------------------------------------------------------------------- Enter Main Details

--- Calculate a birth date from a given date and age
-- If the date is not a simple date, the first date point is used for the calculation
-- @param dtStartDate (fhDate object)
-- @param strAge (a string defining an age in ymwd)
-- @return dtBirth (fhDate object), or nil
-- @strError error string if strAge is invalid, or 'Success'
-- @iYears, iMonths, iDays if strAge is valid (Years Months and Days extracted from strAge as integers or false for any element not found)

function calcBirth(dtStartDate, strAge)
	--Extract the individual elements from strAge and convert them to numbers of Years, Months and Days
	--Any element which doesn't exist in strAge is set to false

	local iYears = tonumber(string.match(strAge, "(%d+)$")) or tonumber(string.match(strAge, "(%d+)y")) or false
	local iMonths = tonumber(string.match(strAge, "(%d+)m")) or false
	local iDays = tonumber(string.match(strAge, "(%d+)d")) or false
	local iWeeks = tonumber(string.match(strAge, "(%d+)w")) or false
	if iWeeks then
		if iDays then
			iDays = iDays + 7 * iWeeks
		else
			iDays = 7 * iWeeks
		end
	end
	if iYears or iMonths or iDays then
		-- subtract the age from dtDate to get the calculated birth date
		local dpStartDate1 = dtStartDate:GetDatePt1()
		local dpCalcDate1 = fhCallBuiltInFunction(
			"CalcDate",
			dpStartDate1,
			-1 * utils.choose(iYears, iYears, 0),
			-1 * utils.choose(iMonths, iMonths, 0),
			-1 * utils.choose(iDays, iDays, 0)
		)
		--Decompose calculated date point
		local cYear = dpCalcDate1:GetYear()
		local cMonth = dpCalcDate1:GetMonth()
		local cDay = dpCalcDate1:GetDay()

		local dtBirthDate = fhNewDate()
		--adjust precision based on contents of strAge
		if iDays then
			--no need to adjust precision
			dtBirthDate:SetSimpleDate(dpCalcDate1, "cal")
		elseif iMonths then
			--remove Days element of date
			dtBirthDate:SetSimpleDate(fhNewDatePt(cYear, cMonth), "cal")
		else
			--remove Months element of date
			dtBirthDate:SetSimpleDate(fhNewDatePt(cYear), "cal")
		end
		return dtBirthDate, "Success", iYears, iMonths, iDays
	end
	return nil, "No age element (ymwd) found"
end

function doDetail(sType, iRecordId)
	local ptrRecord
	sType = "Existing"
	local fields = tablex.deepcopy(pRegions[iRegion][iYear]["fields"])
	table.insert(fields, 1, {
		tag = "RECORD",
		label = "Record",
		type = "RECORD",
		recordtype = "INDI",
		buttontitle = "Select Individual ...",
		prompt = pickIndividual,
		minlength = 1,
	})
	local template_fields = pRegions[iRegion][iYear]["template"]["detail_fields"]

	local sTitle = pRegions[iRegion]["name"] .. " " .. iYear .. " Census Data Input"

	-- Create Shortcut tables
	local s = {}
	s["BIRT.PLAC"] = tUsedList["BIRT.PLAC"]
	s["RELATION"] = { "Head", "Wife", "Son", "Daughter", "Father", "Mother" }
	s["OCCU"] = fhGetDataList("OCCUPATIONS")
	s["EMPLOYMENT"] = { "Employer", "Own Account", "Private" }
	tblStandardWorkplace = { "No fixed place", "At home" }
	tblWorkplace = tablex.copy(tblStandardWorkplace)
	s["WORKPLACE"] = tablex.insertvalues(tblWorkplace, tUsedList["BIRT.PLAC"])

	local iName = 0
	for i, field in ipairs(fields) do
		if field.tag == "NAME" then
			iName = i
		end
	end
	if ptrHead:IsNull() and iName > 0 then
		-- Set value for Name
		fields[iName].value = pCite:getValue("NM-HEAD_OF_HOUSEHOLD")
	end
	-- Prompt for Details
	local r = fh.getParam(sTitle, "Enter name and other information as shown on Census", fields, { "OK", "Cancel" }, s)
	if not r.ok then
		-- Cancel pressed or Window Closed
		return
	end

	-- Results for 'RECORD' when used with pickIndividual are a table
	-- ptr=record pointer (null for create)
	-- name=string
	-- fam=recordpointer (null if new family required
	-- tab 1=Select, 2=Create, 3=Name Only
	-- Relation 1=Spouse, 2=Child, 3=Other

	local sType = r.results["RECORD"].tab

	sRelType = r.results["RECORD"].relation or 3
	ptrRecord = r.results["RECORD"].ptr or fhNewItemPtr()
	ptrFam = r.results["RECORD"].fam or fhNewItemPtr()

	if sType == 1 then -- Selected Existing Record
	elseif sType == 2 then -- Create Record
		-- Create Record
		ptrRecord = createIndi(r.results["RECORD"].name)
		if r.results["SEX"] then
			local fSex = fhCreateItem("SEX", ptrRecord, true)
			fhSetValueAsText(fSex, r.results["SEX"])
			addResultLine(fSex, "Added")
		end
		-- Attach to Family As needed
		local sRelType = r.results["RECORD"].relation
		if (sRelType == 1 or sRelType == 2) and ptrFam and ptrFam:IsNull() then
			-- Create New Family Record for Head
			ptrFam = createFamilyAsSpouse(ptrHead)
		end
		if sRelType == 4 and ptrFam and ptrFam:IsNull() then -- Parent
			ptrFam = createFamilyAsChild(ptrHead)
		end

		if sRelType == 1 then
			-- Add Spouse to Family
			addFamilyAsSpouse(ptrRecord, ptrFam)
		elseif sRelType == 2 then
			-- Add Child to Family
			addFamilyAsChild(ptrRecord, ptrFam)
		elseif sRelType == 3 then
			addUnlinked(ptrRecord:Clone())
		elseif sRelType == 4 then
			addFamilyAsSpouse(ptrRecord, ptrFam)
		end
	end
	if ptrRecord:IsNotNull() then
		addData(ptrRecord, r.results, fields)
		if ptrHead:IsNull() then
			ptrHead:MoveTo(ptrRecord)
		end
		sResult = "\nRecord " .. fhGetDisplayText(ptrRecord) .. " updated"
	else
		-- Name only
		sResult = r.results["NAME"] .. " added to Text From Source"
	end
	-- Add detail line to body
	pRegions:addDetailLine(r.results, fields, ptrRecord)

	return sResult, ptrRecord
end

function addData(ptrRecord, result, t)
	-- Parameters
	-- ptrRecord  - Record pointer for current individual
	-- result - Result fields from Detail Prompt
	-- Census Fields from pRegions

	local dDate = fhNewDate()
	local d = pRegions[iRegion][iYear]["date"]
	if d then
		dDate:SetValueAsText(d)
	end
	local dPlace = pCite:getValue("PL-LOCATION")
	local dAddr = pCite:getValue("AD-ADDRESS")
	local fAge = result["AGE"]
	local fBirthPlace = result["BIRT.PLAC"]
	if fBirthPlace then
		table.addUnique(tUsedList["BIRT.PLAC"], fBirthPlace)
	end
	local fBirthDate = result["BIRT.DATE"]

	for i, v in ipairs(t) do
		if result[v.tag] then
			local fValue = result[v.tag]
			if v.dr then
				-- Have Data Reference potentialLy do something
				if v.dr == "SEX" then
					local fSex = fhGetItemPtr(ptrRecord, "~.SEX")
					if fSex:IsNull() then
						local fSex = fhCreateItem("SEX", ptrRecord, true)
						fhSetValueAsText(fSex, fValue)
						addResultLine(fSex, "Added")
					end
				end
				if v.dr == "OCCU" and fValue and stringx.strip(fValue) ~= "" then
					local fworkPlace = result["WORKPLACE"]
					if fworkPlace and not tablex.find(tblStandardWorkplace, fworkPlace) then
						table.addUnique(tUsedList["BIRT.PLAC"], fworkPlace)
					end
					crtFact(ptrRecord, fValue, v.dr, dDate, fAge, fworkPlace, result["WORKADDRESS"])
					if result["WORKADDRESS"] then
						result["WORKPLACE"] = result["WORKADDRESS"] .. "," .. result["WORKPLACE"] -- concatenate address and place for template
					end
				end
				if v.dr == "EDUC" and fValue and fValue ~= "" then
					crtFact(ptrRecord, fValue, v.dr, dDate, fAge, nil, nil)
				end
				if v.dr == "RELI" and fValue and stringx.strip(fValue) ~= "" then
					crtFact(ptrRecord, fValue, v.dr, dDate, fAge, nil)
				end
			end
		end
		-- Specials
		if v.dr == "CENS" then
			local ptrCens = fhGetItemPtr(ptrRecord, "~.CENS[year=" .. iYear .. "]")
			if ptrCens:IsNotNull() then
				if
					yes(
						"Census exists as follows "
							.. fhGetDisplayText(ptrCens)
							.. " for "
							.. fhGetDisplayText(ptrRecord)
							.. " do you want to replace it?"
					)
				then
					fhDeleteItem(ptrCens)
					crtFact(ptrRecord, nil, v.dr, dDate, fAge, dPlace, dAddr)
				end
			else
				crtFact(ptrRecord, nil, v.dr, dDate, fAge, dPlace, dAddr)
			end
		end
		-- Create Birth?
		if v.dr == "BIRT" then
			local dtBirth = fhNewDate()
			local strError
			local iAgeInYears
			if fBirthDate then
				dtBirth = fBirthDate
			elseif fAge then
				dtBirth, strError, iAgeInYears =
					calcBirth(dDate, utils.choose(fAge == "Under one month", "1m 0d", fAge))

				local dp1 = dtBirth:GetDatePt1()
				if iYear == 1841 and iAgeInYears and iAgeInYears > 15 and iAgeInYears % 5 == 0 then
					-- 1841 Census ages were supposed to be rounded down to the nearest 5 years for people over 15
					local sAge = tostring(iAgeInYears - 4)
					local dtMax = calcBirth(dDate, sAge)
					local dp2 = dtMax:GetDatePt1()
					dtBirth:SetRange("between", dp1, dp2)
				end
				if iYear == 1921 and fAge == "Under one month" then
					--Date of birth will be between one month ago and census date
					local dp2 = dDate:GetDatePt1()
					dtBirth:SetRange("between", dp1, dp2)
				end
			end
			local fFact, sAction = fh.createUpdateFact(ptrRecord, "BIRT", "Birth", (fBirthPlace or ""), dtBirth)
			if fFact and fFact:IsNotNull() then
				pCite:appendCitation(fFact)
				addResultLine(fFact, sAction)
			end
		end
	end
end

------------------------------------------------------------------------------------------------ table.addUnique

function table.addUnique(t, ...)
	for _, v in ipairs({ ... }) do
		local bFound = false
		if type(v) == "string" then
			for _, s in ipairs(t) do
				if s == v then
					bFound = true
					break
				end
			end
			if not bFound then
				table.insert(t, v)
			end
		end
	end
end
------------------------------------------------------------------------------------------------ crtFact

function crtFact(ptr, value, tag, date, age, place, addr)
	fFact = fh.createFact(ptr, tag, place, date, addr, value, age)
	pCite:appendCitation(fFact)
	addResultLine(fFact, "Added")
end

------------------------------------------------------------------------------------------------- Build Family Information

function createIndi(sName)
	local ptrRecord = fhCreateItem("INDI")
	pCite:appendCitation(ptrRecord)
	addResultLine(ptrRecord, "Created")
	local ptrName = fhCreateItem("NAME", ptrRecord)
	fhSetValueAsText(ptrName, sName)
	pCite:appendCitation(ptrName)
	addResultLine(ptrName, "Added")
	return ptrRecord
end

function createFamilyAsChild(ptrIndi)
	-- Create Family As Child
	local ptrFam = fhCreateItem("FAM")
	local ptrFams = fhCreateItem("FAMC", ptrIndi)
	fhSetValueAsLink(ptrFams, ptrFam)
	addResultLine(ptrFam, "Created")
	return ptrFam
end

function createFamilyAsSpouse(ptrIndi)
	-- Create Family As Spouse
	local ptrFam = fhCreateItem("FAM")
	local ptrFams = fhCreateItem("FAMS", ptrIndi)
	fhSetValueAsLink(ptrFams, ptrFam)
	addResultLine(ptrFam, "Created")
	return ptrFam
end

function addFamilyAsChild(ptrIndi, ptrFam)
	-- Add Family As Child
	local ptrFamc = fhCreateItem("FAMC", ptrIndi)
	fhSetValueAsLink(ptrFamc, ptrFam)
	return ptrFam
end

function addFamilyAsSpouse(ptrIndi, ptrFam)
	-- Add Family As Spouse
	local ptrFams = fhCreateItem("FAMS", ptrIndi)
	fhSetValueAsLink(ptrFams, ptrFam)
	return ptrFam
end
---------------------------------------------------------------------------------------- yes - prompt Yes/No
function yes(sQuestion)
	local r = fh.getParam(sPluginName, sQuestion, {}, { "Yes", "No" })
	return r.ok
end
---------------------------------------------------------------------------------------- Pick Individual

function pickIndividual(values)
	return fh.pickIndividualPrompt(values, iYear, ptrHead)
end

--
---------------------------------------------------------------------------------------------------------- Load Regions
--

function loadRegions()
	local sPlugin = sPluginName
	local function mkdir(path)
		local sep = "\\"
		local pStr = ""
		for dir in path:gmatch("[^" .. sep .. "]+") do
			pStr = pStr .. dir .. sep
			lfs.mkdir(pStr)
		end
	end
	local sFolder = fhGetContextInfo("CI_APP_DATA_FOLDER") .. "\\AutoText for Plugins\\" .. sPlugin .. "\\"
	-- Check folder exists and create if not
	mkdir(sFolder)
	-- Create Region Table
	local t = {}

	------------------------------------------------------- functions for loading templates and updating details

	function t.loadTemplate(self, iRegion, iYear)
		local f = sFolder .. self[iRegion][iYear]["template_name"]
		local d, e = file.read(f)
		if e then
			-- Create File from default
			d = self[iRegion][iYear]["template_default"]
			file.write(f, self[iRegion][iYear]["template_default"])
		end
		self[iRegion][iYear]["template"] = {}
		-- load detail line
		local p = "{detail}(.*){/detail}"
		local _, _, x = d:find(p)
		-- no detail line set blank
		if not x then
			x = ""
		end
		-- Remove Detail Line
		s = "{detail}" .. x .. "{/detail}"
		d = fh.richTextReplace(d, s, "")
		self[iRegion][iYear]["template"]["body"] = d
		x = stringx.split(x, ",")
		self[iRegion][iYear]["template"]["detail_fields"] = x
		local s = ""
		for k, v in ipairs(x) do
			if k > 1 then
				s = s .. "|"
			end
			s = s .. "{" .. v .. "}"
		end
		s = s .. ""
		self[iRegion][iYear]["template"]["line"] = s
		-- Replace all citation fields in AutoText
		for k, v in pairs(pCite.fields) do
			local n = pCite:getDisplayValue(k) or ""
			self[iRegion][iYear]["template"]["body"] =
				fh.richTextReplace(self[iRegion][iYear]["template"]["body"], "{" .. k .. "}", n)
		end
		-- Set Census Date
		self[iRegion][iYear]["template"]["body"] =
			fh.richTextReplace(self[iRegion][iYear]["template"]["body"], "{YEAR}", tostring(iYear))
		self[iRegion][iYear]["template"]["body"] =
			fh.richTextReplace(self[iRegion][iYear]["template"]["body"], "{DATE}", self[iRegion][iYear]["date"])
	end

	function t.addDetailLine(self, values, fields, ptr)
		local s = self[iRegion][iYear]["template"]["line"]
		for k, v in ipairs(fields) do
			local sV = values[v.tag]
			if v.type == "DATE" then
				sV = sV:GetValueAsText()
			end
			if sV == nil then
				sV = " "
			end
			local sV = tostring(sV) or ""
			-- If it's the NAME field add link to Record
			if v.tag == "NAME" and ptr and ptr:IsNotNull() then
				s = stringx.replace(s, "{" .. v.tag .. "}", fh.richTextRecordLink(ptr, sV))
			else
				s = fh.richTextReplace(s, "{" .. v.tag .. "}", sV)
			end
		end
		self[iRegion][iYear]["template"]["body"] =
			stringx.replace(self[iRegion][iYear]["template"]["body"], "", s .. "\n")
		self[iRegion][iYear]["template"]["count"] = (self[iRegion][iYear]["template"]["count"] or 0) + 1
	end
	------------------------------------------------------------------------------------------ Census Definitions

	--England and Wales
	t[1] = {
		names = { "England and Wales", "England" },

		[1841] = {
			date = "6 Jun 1841",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "INCOUNTY",
					type = "LIST",
					value = "",
					label = "Born in County",
					prompts = { "Yes", "No", "Not known", "" },
					values = { "Yes", "No", "NK", "" },
				},
				{
					tag = "FOREIGN",
					type = "LIST",
					label = "England,Scotland,Ireland,Foreign",
					prompts = { "", "E", "S", "I", "F" },
					values = { "", "E", "S", "I", "F" },
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1841 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
				.. '\n\n Name | Sex | Age | Occupation | Where Born | Foreign etc. \n\n{detail}NAME,SEX,AGE,OCCU,INCOUNTY,FOREIGN{/detail}',
		},
		[1851] = {
			date = "30 Mar 1851",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = 0,
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1851 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n\n\n\n Name | Relation | Condition | Sex | Age | Occupation | Where Born | Infirmity \n\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,BIRT.PLAC,INFIRMITY{/detail}',
		},
		[1861] = {
			date = "7 Apr 1861",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = 0,
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1861 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n\n\n\n Name | Relation | Condition | Sex | Age | Occupation | Where Born | Infirmity \n\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,BIRT.PLAC,INFIRMITY{/detail}',
		},
		[1871] = {
			date = "2 Apr 1871",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = 0,
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1871 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n\n\n\n Name | Relation | Condition | Sex | Age | Occupation | Where Born | Infirmity \n\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,BIRT.PLAC,INFIRMITY{/detail}',
		},
		[1881] = {
			date = "3 Apr 1881",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = 0,
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1881 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n\n\n\n Name | Relation | Condition | Sex | Age | Occupation | Where Born | Infirmity \n\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,BIRT.PLAC,INFIRMITY{/detail}',
		},
		[1891] = {
			date = "5 Apr 1891",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = 0,
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "EMPLOY",
					type = "LIST",
					label = "Employment",
					value = "",
					prompts = {
						"",
						"Employer",
						"Employed",
						"Neither Employer or Employed",
					},
					values = {
						"",
						"Employer",
						"Employed",
						"Neither Employer or Employed",
					},
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1891 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
				.. 'n\n\n Name | Relation | Condition | Sex | Age | Occupation | Employment | Where Born | Infirmity \n'
				.. "\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,BIRT.PLAC,INFIRMITY{/detail}",
		},
		[1901] = {
			date = "31 Mar 1901",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = "",
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "EMPLOY",
					type = "LIST",
					label = "Employment Status",
					prompts = { "", "Employer", "Worker", "Own Account" },
					values = { "", "Employer", "Worker", "Own Account" },
					value = "",
				},
				{
					tag = "ATHOME",
					type = "LIST",
					label = "At Home",
					value = "",
					prompts = { "", "At Home" },
					values = { "", "At Home" },
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1901 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
				.. '\n\n\n'
				.. " Name | Relation | Condition | Sex | Age | Occupation | Employ Status | At Home | Where Born | Infirmity \n\n"
				.. "{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,ATHOME,BIRT.PLAC,INFIRMITY{/detail}",
		},
		[1911] = {
			date = "2 Apr 1911",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "STATUS",
					type = "LIST",
					label = "Marriage Status",
					value = "",
					values = { "Single", "Married", "Widower", "Widow" },
				},
				{
					tag = "YEARS",
					type = "STRING",
					label = "Years Married",
					value = "",
				},
				{ tag = "CHILDREN_BORN", type = "NUMBER", label = "Children Born" },
				{
					tag = "CHILDREN_LIVING",
					type = "NUMBER",
					label = "Children Living",
				},
				{ tag = "CHILDREN_DIED", type = "NUMBER", label = "Children Died" },
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{ tag = "INDUSTRY", type = "STRING", label = "Industry", value = "" },
				{
					tag = "EMPLOY",
					type = "LIST",
					label = "Employment Status",
					prompts = { "", "Employer", "Worker", "Own Account" },
					values = { "", "Employer", "Worker", "Own Account" },
					value = "",
				},
				{
					tag = "ATHOME",
					type = "LIST",
					label = "At Home",
					value = "",
					prompts = { "", "At Home" },
					values = { "", "At Home" },
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "NATIONALITY",
					type = "STRING",
					label = "Nationality",
					value = "",
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1911 Census UK.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n\n'
				.. '\n\n'
				.. " Name | Relation | Sex | Age | Married | Years | Chd Born | Chd Living | Chd Died | Occupation | Industry | Employ Status | At Home | Where Born | Nationality | Infirmity \n"
				.. "\n{detail}NAME,RELATION,SEX,AGE,STATUS,YEARS,CHILDREN_BORN,CHILDREN_LIVING,CHILDREN_DIED,OCCU,INDUSTRY,EMPLOY,ATHOME,BIRT.PLAC,NATIONALITY,INFIRMITY{/detail}",
		},
		[1921] = {
			date = "19 Jun 1921",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age (in years and months)",
					dr = "AGE",
					value = "",
					mask = "(/d+)|(/d+[ym])|(/d+y/s/d+m)|Under one month)",
					minlength = 1,
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "CONDITION",
					type = "LIST",
					label = "Marriage or Orphanhood",
					value = "",
					values = {
						"Single",
						"Married",
						"Widowed",
						"D",
						"Both Alive",
						"Father Dead",
						"Mother Dead",
						"Both Dead",
					},
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "NATIONALITY",
					type = "STRING",
					label = "Nationality",
					value = "",
				},
				{
					tag = "EDUC",
					dr = "EDUC",
					type = "LIST",
					label = "School",
					value = "",
					values = { "", "Whole-time", "Part-time" },
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "EMPLOYMENT",
					type = "STRING",
					label = "Employment",
					value = "",
				},
				{
					tag = "WORKPLACE",
					type = "STRING",
					label = "Place of Work",
					value = "",
					dr = "OCCU.PLACE",
				},
				{
					tag = "WORKADDRESS",
					type = "STRING",
					label = "Address of Work",
					value = "",
					dr = "OCCU.ADDR",
				},
				{
					tag = "CHILDREN_LIVING",
					type = "STRING",
					label = "Total children living under 16",
					mask = "(/d+)|None)",
				},
				{
					tag = "CHILDREN_0",
					type = "NUMBER",
					label = "Children under 1",
				},
				{
					tag = "CHILDREN_1",
					type = "NUMBER",
					label = "Children aged 1",
				},
				{
					tag = "CHILDREN_2",
					type = "NUMBER",
					label = "Children aged 2",
				},
				{
					tag = "CHILDREN_3",
					type = "NUMBER",
					label = "Children aged 3",
				},
				{
					tag = "CHILDREN_4",
					type = "NUMBER",
					label = "Children aged 4",
				},
				{
					tag = "CHILDREN_5",
					type = "NUMBER",
					label = "Children aged 5",
				},
				{
					tag = "CHILDREN_6",
					type = "NUMBER",
					label = "Children aged 6",
				},
				{
					tag = "CHILDREN_7",
					type = "NUMBER",
					label = "Children aged 7",
				},
				{
					tag = "CHILDREN_8",
					type = "NUMBER",
					label = "Children aged 8",
				},
				{
					tag = "CHILDREN_9",
					type = "NUMBER",
					label = "Children aged 9",
				},
				{
					tag = "CHILDREN_10",
					type = "NUMBER",
					label = "Children aged 10",
				},
				{
					tag = "CHILDREN_11",
					type = "NUMBER",
					label = "Children aged 11",
				},
				{
					tag = "CHILDREN_12",
					type = "NUMBER",
					label = "Children aged 12",
				},
				{
					tag = "CHILDREN_13",
					type = "NUMBER",
					label = "Children aged 13",
				},
				{
					tag = "CHILDREN_14",
					type = "NUMBER",
					label = "Children aged 14",
				},
				{
					tag = "CHILDREN_15",
					type = "NUMBER",
					label = "Children aged 15",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1921 Census England.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
				.. '\n\n'
				.. " NAME and SURNAME  | RELATIONSHIP to Head of Household  | AGE in Years and Months  | SEX  | MARRIAGE or ORPHANHOOD  | BIRTHPLACE  | NATIONALITY  | ATTENDING SCHOOL  | OCCUPATION  | EMPLOYMENT  | PLACE OF WORK  | TOTAL CHILDREN LIVING UNDER 16  | Under one  | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10  | 11  | 12  | 13  | 14  | 15  \n"
				.. "\n{detail}NAME,RELATION,AGE,SEX,CONDITION,BIRT.PLAC,NATIONALITY,EDUC,OCCU,EMPLOYMENT,WORKPLACE,CHILDREN_LIVING,CHILDREN_0,CHILDREN_1,CHILDREN_2,CHILDREN_3,CHILDREN_4,CHILDREN_5,CHILDREN_6,CHILDREN_7,CHILDREN_8,CHILDREN_9,CHILDREN_10,CHILDREN_11,CHILDREN_12,CHILDREN_13,CHILDREN_14,CHILDREN_15{/detail}",
		},
	}
	--
	-- Ireland
	--
	t[2] = {
		names = { "Ireland" },

		[1901] = {
			date = "31 Mar 1901",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELI",
					label = "Religious Profession",
					type = "STRING",
					dr = "RELI",
					value = "",
					minlength = 1,
				},
				{
					tag = "EDUCATION",
					label = "Education",
					type = "LIST",
					value = "",
					values = { "Read & Write", "Read", "Can not read" },
					minlength = 1,
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "CONDITION",
					label = "Condition",
					type = "LIST",
					prompts = { "Unmarried", "Married", "Widowed" },
					value = "",
					values = { "Unmarried", "Married", "Widowed" },
				},
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "LANGUAGE",
					type = "LIST",
					label = "Irish Language",
					value = " ",
					values = { " ", "Irish", "Irish & English" },
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1901 Census Ireland.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
				.. '\n\n\n'
				.. " Name | Relation | Religious Profession | Education | Age | Sex | Occupation | Marriage | Where Born | Irish Language | Infirmity \n\n"
				.. "{detail}NAME,RELATION,RELI,EDUCATION,AGE,SEX,OCCU,CONDITION,BIRT.PLAC,LANGUAGE,INFIRMITY{/detail}",
		},
		[1911] = {
			date = "2 Apr 1911",
			["fields"] = {
				{
					tag = "NAME",
					label = "Name",
					type = "STRING",
					dr = "NAME",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELATION",
					label = "Relationship",
					type = "STRING",
					value = "",
					minlength = 1,
				},
				{
					tag = "RELIGION",
					label = "Religious Profession",
					type = "STRING",
					dr = "RELI",
					value = "",
					minlength = 1,
				},
				{
					tag = "EDUCATION",
					label = "Education",
					type = "LIST",
					value = "",
					values = { "Read & Write", "Read", "Can not read" },
					minlength = 1,
				},
				{
					tag = "AGE",
					type = "STRING",
					label = "Age",
					dr = "AGE",
					value = "",
					mask = "/d+[dwmy]",
					minlength = 1,
				},
				{
					tag = "SEX",
					type = "LIST",
					label = "Sex",
					dr = "SEX",
					value = "",
					prompts = { "Male", "Female", "Unknown" },
					values = { "M", "F", "U" },
				},
				{
					tag = "OCCU",
					dr = "OCCU",
					type = "STRING",
					label = "Occupation",
					value = "",
				},
				{
					tag = "STATUS",
					label = "Married",
					type = "LIST",
					prompts = { "Married", "Widow", "Widower", "Single" },
					value = "",
					values = { "Married", "Widow", "Widower", "Single" },
				},
				{
					tag = "YEARS",
					type = "STRING",
					label = "Years Married",
					value = "",
				},
				{ tag = "CHILDREN_BORN", type = "NUMBER", label = "Children Born" },
				{
					tag = "CHILDREN_LIVING",
					type = "NUMBER",
					label = "Children Living",
				},
				{ tag = "CHILDREN_DIED", type = "NUMBER", label = "Children Died" },
				{
					tag = "BIRT.PLAC",
					type = "STRING",
					label = "Where Born",
					dr = "BIRT.PLAC",
					value = "",
				},
				{
					tag = "LANGUAGE",
					type = "LIST",
					label = "Irish Language",
					value = " ",
					values = { " ", "Irish", "Irish & English" },
				},
				{
					tag = "INFIRMITY",
					type = "STRING",
					label = "Infirmity",
					value = "",
				},
				{ tag = "CENS", dr = "CENS" },
				{ tag = "BIRT", dr = "BIRT" },
			},
			["template_name"] = "1911 Census Ireland.ftf",
			["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
				.. '\n\n\n'
				.. " Name | Relation | Religious Profession | Education | Age | Sex | Occupation | Married | Years | Chd Born | Chd Living | Chd Died | Where Born | Irish Language | Infirmity \n\n"
				.. "{detail}NAME,RELATION,RELIGION,EDUCATION,AGE,SEX,OCCU,STATUS,YEARS,CHILDREN_BORN,CHILDREN_LIVING,CHILDREN_DIED,BIRT.PLAC,LANGUAGE,INFIRMITY{/detail}",
		},
		--there was no 1921 census in Ireland or Northen Ireland
	}

	--
	-- Create Welsh variants for 1891, 1901, 1911, 1921 based on English
	--

	local fieldWelsh = {
		tag = "LANGUAGE",
		type = "LIST",
		label = "Language Spoken",
		value = "Both",
		values = { "Both", "Welsh", "English" },
	}
	t[3] = tablex.deepcopy(t[1])
	t[3].names = { "Wales" }
	t[3][1891]["template_name"] = "1891 Census Wales.ftf"
	t[3][1891]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n Name | Relation | Condition | Sex | Age | Occupation | Employment | Where Born | Infirmity | Language Spoken\n'
		.. "\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,BIRT.PLAC,INFIRMITY,LANGUAGE{/detail}"
	table.insert(t[3][1891]["fields"], 9, fieldWelsh)

	t[3][1901]["template_name"] = "1901 Census Wales.ftf"
	t[3][1901]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Condition | Sex | Age | Occupation | Employ Status | At Home | Where Born | Infirmity | Language Spoken\n\n"
		.. "{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,ATHOME,BIRT.PLAC,INFIRMITY,LANGUAGE{/detail}"
	table.insert(t[3][1901]["fields"], 8, fieldWelsh)

	t[3][1911]["template_name"] = "1911 Census Wales.ftf"
	t[3][1911]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Sex | Age | Married | Years | Chd Born | Chd Living | Chd Died | Occupation | Industry | Employ Status | At Home | Where Born | Nationality | Infirmity | Language Spoken\n"
		.. "\n{detail}NAME,RELATION,SEX,AGE,STATUS,YEARS,CHILDREN_BORN,CHILDREN_LIVING,CHILDREN_DIED,OCCU,INDUSTRY,EMPLOY,ATHOME,BIRT.PLAC,NATIONALITY,INFIRMITY,LANGUAGE{/detail}"
	table.insert(t[3][1911]["fields"], 16, fieldWelsh)

	t[3][1921]["template_name"] = "1921 Census Wales.ftf"
	t[3][1921]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n'
		.. " NAME and SURNAME  | RELATIONSHIP to Head of Household  | AGE in Years and Months  | SEX  | MARRIAGE or ORPHANHOOD  | BIRTHPLACE  | NATIONALITY  | ATTENDING SCHOOL  | OCCUPATION  | EMPLOYMENT  | PLACE OF WORK  | TOTAL CHILDREN LIVING UNDER 16  | Under one  | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10  | 11  | 12  | 13  | 14  | 15 | LANGUAGE Spoken \n"
		.. "\n{detail}NAME,RELATION,AGE,SEX,CONDITION,BIRT.PLAC,NATIONALITY,EDUC,OCCU,EMPLOYMENT,WORKPLACE,CHILDREN_LIVING,CHILDREN_0,CHILDREN_1,CHILDREN_2,CHILDREN_3,CHILDREN_4,CHILDREN_5,CHILDREN_6,CHILDREN_7,CHILDREN_8,CHILDREN_9,CHILDREN_10,CHILDREN_11,CHILDREN_12,CHILDREN_13,CHILDREN_14,CHILDREN_15,LANGUAGE{/detail}"
	table.insert(t[3][1921]["fields"], 30, fieldWelsh)

	--
	-- Create Scottish Variant based on English
	--

	local fieldScottish = {
		tag = "LANGUAGE",
		type = "LIST",
		label = "Language Spoken",
		value = " ",
		values = { " ", "Gaelic", "Gaelic & English" },
	}
	local fieldInSchool = {
		tag = "INSCHOOL",
		type = "STRING",
		label = "Children in Education",
		value = "",
	}
	local fieldWindows = {
		tag = "WINDOWS",
		type = "STRING",
		label = "Rooms with windows",
		value = "",
	}

	t[4] = tablex.deepcopy(t[1])
	t[4].names = { "Scotland" }

	--1841 and 1851 are identical to England and Wales

	t[4][1861]["template_name"] = "1861 Census Scotland.ftf"
	table.insert(t[4][1861]["fields"], fieldInSchool) --add extra fields to the end
	table.insert(t[4][1861]["fields"], fieldWindows)
	t[4][1861]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Condition | Sex | Age | Occupation | Where Born | Infirmity |Children in Education | Rooms with Windows\n"
		.. "\n{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,BIRT.PLAC,INFIRMITY,INSCHOOL,WINDOWS{/detail}"

	t[4][1871]["template_name"] = "1871 Census Scotland.ftf"
	table.insert(t[4][1871]["fields"], fieldInSchool)
	table.insert(t[4][1871]["fields"], fieldWindows)
	t[4][1871]["template_default"] = t[4][1861]["template_default"]

	t[4][1881]["template_name"] = "1881 Census Scotland.ftf"
	table.insert(t[4][1881]["fields"], fieldWindows)

	t[4][1881]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Condition | Sex | Age | Occupation | Where Born | Infirmity | Rooms with Windows\n\n"
		.. "{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,BIRT.PLAC,INFIRMITY,WINDOWS{/detail}"

	t[4][1891]["template_name"] = "1891 Census Scotland.ftf"
	table.insert(t[4][1891]["fields"], fieldWindows) -- at the end
	table.insert(t[4][1891]["fields"], 9, fieldScottish)

	t[4][1891]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. 'n\n\n'
		.. " Name | Relation | Condition | Sex | Age | Occupation | Employment | Where Born | Language | Infirmity | Rooms with Windows\n\n"
		.. "{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,BIRT.PLAC,LANGUAGE,INFIRMITY,WINDOWS{/detail}"

	t[4][1901]["template_name"] = "1901 Census Scotland.ftf"
	table.insert(t[4][1901]["fields"], fieldWindows) --add windows to the end
	table.insert(t[4][1901]["fields"], 10, fieldScottish)

	t[4][1901]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Condition | Sex | Age | Occupation | Employment | At Home | Where Born | Language | Infirmity | Rooms with Windows\n\n"
		.. "{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,ATHOME,BIRT.PLAC,LANGUAGE,INFIRMITY,WINDOWS{/detail}"

	t[4][1911] = {
		date = "2 Apr 1911",
		["fields"] = {
			fieldWindows,
			{
				tag = "NAME",
				label = "Name",
				type = "STRING",
				dr = "NAME",
				value = "",
				minlength = 1,
			},
			{
				tag = "RELATION",
				label = "Relationship",
				type = "STRING",
				value = "",
				minlength = 1,
			},
			{
				tag = "SEX",
				type = "LIST",
				label = "Sex",
				dr = "SEX",
				prompts = { "Male", "Female", "Unknown" },
				values = { "M", "F", "U" },
				value = "",
			},
			{
				tag = "AGE",
				type = "STRING",
				label = "Age",
				dr = "AGE",
				value = "",
				mask = "/d+[dwmy]",
				minlength = 1,
			},
			fieldScottish,
			{
				tag = "STATUS",
				type = "LIST",
				label = "Marriage Status",
				values = { "Single", "Married", "Widower", "Widow" },
				value = "",
			},
			{ tag = "YEARS", type = "STRING", label = "Years Married", value = "" },
			{ tag = "CHILDREN_BORN", type = "NUMBER", label = "Children Born" },
			{ tag = "CHILDREN_LIVING", type = "NUMBER", label = "Children Living" },
			{ tag = "OCCU", dr = "OCCU", type = "STRING", label = "Occupation" },
			{ tag = "INDUSTRY", type = "STRING", label = "Industry" },
			{
				tag = "EMPLOY",
				type = "LIST",
				label = "Employment Status",
				prompts = { "", "Employer", "Worker", "Own Account" },
				values = { "", "Employer", "Worker", "Own Account" },
				value = "",
			},
			{
				tag = "ATHOME",
				type = "LIST",
				label = "At Home",
				prompts = { "", "At Home" },
				values = { "", "At Home" },
				value = "",
			},
			{
				tag = "BIRT.PLAC",
				type = "STRING",
				label = "Where Born",
				dr = "BIRT.PLAC",
				value = "",
			},
			{
				tag = "NATIONALITY",
				type = "STRING",
				label = "Nationality",
				value = "",
			},
			{ tag = "INFIRMITY", type = "STRING", label = "Infirmity", value = "" },
			{ tag = "CENS", dr = "CENS" },
			{ tag = "BIRT", dr = "BIRT" },
		},
		["template_name"] = "1911 Census Scotland.ftf",
		["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
			.. '\n\n\n'
			.. " Rooms with Windows | Name | Relation | Sex | Age | Language | Married | Years | Chd Born | Chd Living | Occupation | Industry | Employ Status | At Home | Where Born | Nationality | Infirmity \n\n"
			.. "{detail}WINDOWS,NAME,RELATION,SEX,AGE,LANGUAGE,STATUS,YEARS,CHILDREN_BORN,CHILDREN_LIVING,OCCU,INDUSTRY,EMPLOY,ATHOME,BIRT.PLAC,NATIONALITY,INFIRMITY{/detail}",
	}
	t[4][1921] = {
		date = "19 Jun 1921",
		["fields"] = {
			{
				tag = "HSROOMS",
				type = "STRING",
				label = "Houses (Rooms)",
				value = "",
			},
			{
				tag = "HSPERSONS",
				type = "STRING",
				label = "Houses (Persons)",
				value = "",
			},
			{
				tag = "HDROOMS",
				type = "STRING",
				label = "Holdings (Rooms)",
				value = "",
			},
			{
				tag = "HDPERSONS",
				type = "STRING",
				label = "Holdings (Persons)",
				value = "",
			},
			{
				tag = "NAME",
				label = "Name",
				type = "STRING",
				dr = "NAME",
				value = "",
				minlength = 1,
			},
			{
				tag = "RELATION",
				label = "Relationship",
				type = "STRING",
				value = "",
				minlength = 1,
			},
			{
				tag = "AGE",
				type = "STRING",
				label = "Age (in years and months)",
				dr = "AGE",
				value = "",
				mask = "(/d+)|(/d+[ym])|(/d+y/s/d+m)|Under one month)",
				minlength = 1,
			},
			{
				tag = "SEX",
				type = "LIST",
				label = "Sex",
				dr = "SEX",
				value = "",
				prompts = { "Male", "Female", "Unknown" },
				values = { "M", "F", "U" },
			},
			{
				tag = "CONDITION",
				type = "LIST",
				label = "Marriage or Orphanhood",
				value = "",
				values = {
					"S",
					"M",
					"W",
					"D",
					"BA",
					"FD",
					"MD",
					"BD",
				},
			},
			{
				tag = "BIRT.PLAC",
				type = "STRING",
				label = "Where Born",
				dr = "BIRT.PLAC",
				value = "",
			},
			{
				tag = "NATIONALITY",
				type = "STRING",
				label = "Nationality",
				value = "",
			},
			fieldScottish,
			{
				tag = "OCCU",
				dr = "OCCU",
				type = "STRING",
				label = "Occupation",
				value = "",
			},
			{
				tag = "STATUS",
				type = "LIST",
				label = "Status",
				value = " ",
				values = { "E", "OA", "OW", "W" },
			},
			{
				tag = "EMPLOYMENT",
				type = "STRING",
				label = "Employment",
				value = "",
			},
			{
				tag = "HEALTHINSURANCE",
				type = "LIST",
				label = "Health Insurance",
				value = " ",
				values = { "Y", "N" , "M", "F"},
			},
			{
				tag = "CHILDREN_LIVING",
				type = "STRING",
				label = "Total children living under 16",
				mask = "(/d+)|None)",
			},
			{
				tag = "CHILDREN_0",
				type = "NUMBER",
				label = "Children under 1",
			},
			{
				tag = "CHILDREN_1",
				type = "NUMBER",
				label = "Children aged 1",
			},
			{
				tag = "CHILDREN_2",
				type = "NUMBER",
				label = "Children aged 2",
			},
			{
				tag = "CHILDREN_3",
				type = "NUMBER",
				label = "Children aged 3",
			},
			{
				tag = "CHILDREN_4",
				type = "NUMBER",
				label = "Children aged 4",
			},
			{
				tag = "CHILDREN_5",
				type = "NUMBER",
				label = "Children aged 5",
			},
			{
				tag = "CHILDREN_6",
				type = "NUMBER",
				label = "Children aged 6",
			},
			{
				tag = "CHILDREN_7",
				type = "NUMBER",
				label = "Children aged 7",
			},
			{
				tag = "CHILDREN_8",
				type = "NUMBER",
				label = "Children aged 8",
			},
			{
				tag = "CHILDREN_9",
				type = "NUMBER",
				label = "Children aged 9",
			},
			{
				tag = "CHILDREN_10",
				type = "NUMBER",
				label = "Children aged 10",
			},
			{
				tag = "CHILDREN_11",
				type = "NUMBER",
				label = "Children aged 11",
			},
			{
				tag = "CHILDREN_12",
				type = "NUMBER",
				label = "Children aged 12",
			},
			{
				tag = "CHILDREN_13",
				type = "NUMBER",
				label = "Children aged 13",
			},
			{
				tag = "CHILDREN_14",
				type = "NUMBER",
				label = "Children aged 14",
			},
			{
				tag = "CHILDREN_15",
				type = "NUMBER",
				label = "Children aged 15",
			},
			{ tag = "CENS", dr = "CENS" },
			{ tag = "BIRT", dr = "BIRT" },
		},
		["template_name"] = "1921 Census Scotland.ftf",
		["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n\n'
			.. '\n'
			.. 'HOUSES\nRooms|HOUSES\nPersons|HOLDINGS\nRooms|HOLDINGS\nPersons|NAME and SURNAME|RELATIONSHIP\nto Head of Household|AGE Years and Months|SEX|MARRIAGE or ORPHANHOOD|BIRTHPLACE|NATIONALITY|GAELIC|PERSONAL OCCUPATION|STATUS|EMPLOYMENT|NATIONAL HEALTH INSURANCE|DEPENDENT CHILDREN\nNumber| Under one | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 \n\n'
			.. "{detail}HSROOMS,HSPERSONS,HDROOMS,HDPERSONS,NAME,RELATION,AGE,SEX,CONDITION,BIRT.PLAC,NATIONALITY,LANGUAGE,OCCU,STATUS,EMPLOYMENT,HEALTHINSURANCE,CHILDREN_LIVING,CHILDREN_0,CHILDREN_1,CHILDREN_2,CHILDREN_3,CHILDREN_4,CHILDREN_5,CHILDREN_6,CHILDREN_7,CHILDREN_8,CHILDREN_9,CHILDREN_10,CHILDREN_11,CHILDREN_12,CHILDREN_13,CHILDREN_14,CHILDREN_15{/detail}",
	}

	--
	-- Create IOM Variant based on English
	--

	t[5] = tablex.deepcopy(t[1])
	t[5].names = { "Isle of Man" }
	local fieldManx = {
		tag = "LANGUAGE",
		type = "LIST",
		label = "Language Spoken",
		value = "Both",
		values = { "Manx", "English", "Both" },
	}

	t[5][1901]["template_name"] = "1901 Census Isle of Man.ftf"
	t[5][1901]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Condition | Sex | Age | Occupation | Employ Status | At Home | Where Born | Infirmity | Language Spoken\n\n"
		.. "{detail}NAME,RELATION,CONDITION,SEX,AGE,OCCU,EMPLOY,ATHOME,BIRT.PLAC,INFIRMITY,LANGUAGE{/detail}"
	table.insert(t[5][1901]["fields"], 8, fieldManx)

	t[5][1911]["template_name"] = "1911 Census Isle of Man.ftf"
	t[5][1911]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n\n'
		.. " Name | Relation | Sex | Age | Married | Years | Chd Born | Chd Living | Chd Died | Occupation | Industry | Employ Status | At Home | Where Born | Nationality | Infirmity | Language Spoken\n"
		.. "\n{detail}NAME,RELATION,SEX,AGE,STATUS,YEARS,CHILDREN_BORN,CHILDREN_LIVING,CHILDREN_DIED,OCCU,INDUSTRY,EMPLOY,ATHOME,BIRT.PLAC,NATIONALITY,INFIRMITY,LANGUAGE{/detail}"
	table.insert(t[5][1911]["fields"], 16, fieldManx)

	t[5][1921]["template_name"] = "1921 Census Isle of Man.ftf"
	t[5][1921]["template_default"] = '{YEAR} {EN-REGION} Census\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
		.. '\n\n'
		.. " NAME and SURNAME  | RELATIONSHIP to Head of Household  | AGE in Years and Months  | SEX  | MARRIAGE or ORPHANHOOD  | BIRTHPLACE  | NATIONALITY  | ATTENDING SCHOOL  | OCCUPATION  | EMPLOYMENT  | PLACE OF WORK  | TOTAL CHILDREN LIVING UNDER 16  | Under one  | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10  | 11  | 12  | 13  | 14  | 15 | LANGUAGE Spoken \n"
		.. "\n{detail}NAME,RELATION,AGE,SEX,CONDITION,BIRT.PLAC,NATIONALITY,EDUC,OCCU,EMPLOYMENT,WORKPLACE,CHILDREN_LIVING,CHILDREN_0,CHILDREN_1,CHILDREN_2,CHILDREN_3,CHILDREN_4,CHILDREN_5,CHILDREN_6,CHILDREN_7,CHILDREN_8,CHILDREN_9,CHILDREN_10,CHILDREN_11,CHILDREN_12,CHILDREN_13,CHILDREN_14,CHILDREN_15,LANGUAGE{/detail}"
	table.insert(t[5][1921]["fields"], 30, fieldManx)

	--
	-- Create Channel Islands Variant based on English
	--

	t[6] = tablex.deepcopy(t[1])
	t[6].names = { "Channel Islands" }

	--Now add 1939 to England and Wales; not supported for Scotland Irelan IoM and Channel Islands

	local table1939 = {
		date = "29 Sep 1939",
		["fields"] = {
			{
				tag = "NAME",
				type = "STRING",
				label = "Name",
				dr = "NAME",
				value = "",
				minlength = 1,
			},
			{
				tag = "SEX",
				type = "LIST",
				label = "Sex",
				dr = "SEX",
				value = "",
				prompts = { "Male", "Female", "Unknown" },
				values = { "M", "F", "U" },
			},
			{
				tag = "BIRT.DATE",
				type = "DATE",
				label = "Birth Date",
				dr = "BIRT.DATE",
				value = fhNewDate(),
				range = { fhNewDate(1830), fhNewDate(1939, 9, 29) },
			},
			{
				tag = "TYPE",
				type = "LIST",
				label = "Type",
				value = "-",
				values = {
					"-",
					"O-Officer",
					"S-Servant",
					"P-Patient",
					"I-Inmate",
					"V-Visitor",
				},
				prompts = {
					"-",
					"O-Officer",
					"S-Servant",
					"P-Patient",
					"I-Inmate",
					"V-Visitor",
				},
			},
			{
				tag = "STATUS",
				type = "LIST",
				label = "Marriage Status",
				value = 0,
				values = { "Single", "Married", "Widowed", "Divorced" },
				prompts = { "Single", "Married", "Widowed", "Divorced" },
			},
			{
				tag = "OCCU",
				type = "STRING",
				label = "Occupation",
				dr = "OCCU",
				value = "",
			},
			{ tag = "NOTE", type = "STRING", label = "Note", value = "" },
			{ tag = "CENS", dr = "CENS" },
			{ tag = "BIRT", dr = "BIRT" },
		},
		["template_name"] = "1939 Register UK.ftf",
		["template_default"] = '{YEAR} {EN-REGION} Register\n\nEnumeration Date: {DATE}\n\nReference: {TX-REFERENCE} Location: {PL-LOCATION} Address: {AD-ADDRESS}\n'
			.. '\n\n\n'
			.. " Name | OVSP OR I | M or F | Birth Date|Status|Occupation|Note\n"
			.. "\n{detail}NAME,TYPE,SEX,BIRT.DATE,STATUS,OCCU,NOTE{/detail}",
	}

	t[1][1939] = table1939
	t[3][1939] = table1939

	return t
end
----------------------------------------------------- Call Main
main()

Source:Record-Census-Data-UK-10.fh_lua