{Application 'POPSTANLFS' logic file generated by CSPro}
PROC GLOBAL

// For fill in question text for name of day of one week before interview
string weekBeforeInterviewDate;
// Name tag if proxy status = 1
string nameTag;
// Arrays for setvalueset
array codes(100);
array string stringCodes(100);
array string labels(100);

// Determine if member of household in demographics roster
// is eligible for labor section.
// Pass in the index (occurrence number) of the household
// member in the demographics roster.


function isEligibleForLabor(personNumber)
	isEligibleForLabor = DEMAG1(personNumber) >= 15;
end;

// Determine if checkbox is empty (no options selected)


function checkboxHasSelection(string checkbox)
	checkboxHasSelection = length(strip(checkbox)) > 0;
end;


function dayOfWeek(yyyymmdd)
	numeric referenceDay = 20170827; // This was be a Sunday
	numeric diff = datediff(referenceDay, yyyymmdd, "d");
	dayOfWeek = diff % 7 + 1;
end;


function string dayOfWeekToString(dayNum)
	// Convert a numeric day of the week (1 for Sunday, 2 for Tuesday...)
	// to a string representation in the currently selected language.
	string dayName;
	recode dayNum => dayName;
	  1  => tr("Sunday");
	  2  => tr("Monday");
	  3  => tr("Tuesday");
	  4  => tr("Wednesday");
	  5  => tr("Thursday");
	  6  => tr("Friday");
	  7  => tr("Saturday");
	endrecode;
	 dayOfWeekToString = dayName;
end;


function hello()
	errmsg("Hello");
end;

//  Array to display list of household members
array string householdMembersArray(20, 4);

// Display list of household members from demographic roster
// and have interviewer pick a member.
// Return row number of member chosen.


function pickFromHouseholdRoster()
	//  Fill array from demographics roster
	numeric personNumber;
	do personNumber = 1 while personNumber <= totocc(DEM_ROSTER)
		householdMembersArray(personNumber, 1) = strip(NAME(personNumber));
		householdMembersArray(personNumber, 2) = getlabel(DEMSEX_VS1, visualvalue(DEMSEX(personNumber)));
		if visualvalue(DEMSEX(personNumber)) = 1 then
			householdMembersArray(personNumber, 3) = getlabel(DEMREL_MALE, visualvalue(DEMREL(personNumber)));
		elseif visualvalue(DEMSEX(personNumber)) = 2 then
			householdMembersArray(personNumber, 3) = getlabel(DEMREL_FEMALE, visualvalue(DEMREL(personNumber)));
		else
			householdMembersArray(personNumber, 3) = "";
		endif;
		householdMembersArray(personNumber, 4) = edit("ZZ9", visualvalue(DEMAG1(personNumber)));
	enddo;

	//  Mark end of array
	householdMembersArray(personNumber, 1) = "";

	pickFromHouseholdRoster = showarray(householdMembersArray, title(tr("Name"), tr("Sex"), tr("Relationship"), tr("Age")));
end;

// List of modules in labor section.
// Column 1 is module label, column 2 is name of start field.
numeric numberOfModules = 7;
array string modules(numberOfModules,2) =
	"Proxy" 				 "PXYSTART"
	"At work" 				 "EMPSTART"
	"Temporary Absence" 	 "ABSSTART"
	"Agriculture and Market" "AGFSTART"
	"Main Job" 				 "MJBSTART"
	"Working Time" 			 "WTSTART"
	"Job Search" 			 "SRHSTART";

// Used to display list of enabled modules in accept.
array string enabledModules(7);

// Display list of existing labor record occurrences in nice format (NAME, SEX, REL, AGE).
// Return row number of member chosen.


function pickFromLaborForms()
	//  Fill array from labor form
	numeric laborRowNumber;
	do laborRowNumber = 1 while laborRowNumber <= totocc(LABOR_FORM)
		numeric demoRowNumber = visualvalue(LBPPNO(laborRowNumber));
		householdMembersArray(laborRowNumber, 1) = strip(NAME(demoRowNumber));
		householdMembersArray(laborRowNumber, 2) = getlabel(DEMSEX_VS1, visualvalue(DEMSEX(demoRowNumber)));
		if visualvalue(DEMSEX(demoRowNumber)) = 1 then
			householdMembersArray(laborRowNumber, 3) = getlabel(DEMREL_MALE, visualvalue(DEMREL(demoRowNumber)));
		elseif visualvalue(DEMSEX(demoRowNumber)) = 2 then
			householdMembersArray(laborRowNumber, 3) = getlabel(DEMREL_FEMALE, visualvalue(DEMREL(demoRowNumber)));
		else
			householdMembersArray(laborRowNumber, 3) = "";
		endif;
		householdMembersArray(laborRowNumber, 4) = edit("ZZ9", visualvalue(DEMAG1(demoRowNumber)));
	enddo;

	//  Mark end of array
	householdMembersArray(laborRowNumber, 1) = "";

	pickFromLaborForms = showarray(householdMembersArray, title(tr("Name"), tr("Sex"), tr("Relationship"), tr("Age")));
end;

// Show list of enabled labor modules in accept menu for an occurrence in
// repeating labor form. Returns row number chosen.


function pickLaborModule(laborPersonNumber)

	// Loop through module array and display accept box listing only
	// modules that are enabled for occurrence laborPersonNumber
	// (based on start module field being set to 1).
	numeric nextEnabled = 1;
	do numeric moduleIndex = 1 while moduleIndex <= numberOfModules
		string moduleLabel = modules(moduleIndex, 1);
		// Get start field name from array
		string moduleStartField = modules(moduleIndex, 2);
		// If start field is set to 1 then it is enabled.
		if getvaluenumeric(moduleStartField,laborPersonNumber)  = 1 then
			enabledModules(nextEnabled) = moduleLabel;
			nextEnabled = nextEnabled + 1;
		endif;
	enddo;
	if nextEnabled > 1 then
		// Close array
		enabledModules(nextEnabled) = "";
		numeric pickedIndexInEnabled = accept(tr("Choose module"), enabledModules);

		// Translate result to index modules array
		string pickedLabel = enabledModules(pickedIndexInEnabled);
		numeric pickedIndexInModules;
		do numeric i = 1 while i <= numberOfModules
			if pickedLabel = modules(i, 1) then
				pickedIndexInModules = i;
			endif;
		enddo;

		pickLaborModule = pickedIndexInModules;
	else
		// Nothing to pick
		pickLaborModule = 0;
	endif;
end;

// Move to a field value based on field name in string variable and occurrence number.


function moveToFieldWithOccurrence(string fieldName, occurrence)
	move to maketext("%s(%d)", fieldName, occurrence) advance;
end;

// Display navigation aide


function goto()
	numeric section = accept(tr("Go To?"), tr("Identifiers"), tr("Demographics"), tr("Labor"));

	if section = 1 then
		move to GH_FORM advance;
	elseif section = 2 then
		numeric personNumber = pickFromHouseholdRoster();
		if personNumber > 0 then
			move to NAME(personNumber) advance;
		endif;
	elseif section = 3 then
		numeric laborPersonNumber = pickFromLaborForms();
		if laborPersonNumber > 0 then
			numeric moduleIndex = pickLaborModule(laborPersonNumber);
			if moduleIndex > 0 then
				string selectedModuleStartField = modules(moduleIndex, 2);
				moveToFieldWithOccurrence(selectedModuleStartField, laborPersonNumber);
			endif;
		endif;
	endif;
end;

// Show household summary in poup message


function showHouseholdSummary()

	numeric males = 0;
	numeric females = 0;
	do numeric i = 1 while i <= totocc(DEM_ROSTER)
		if visualvalue(DEMSEX(i)) = 1 then
			inc(males);
		elseif visualvalue(DEMSEX(i)) = 2 then
			inc(females);
		endif;
	enddo;
	errmsg("Head of Household: %s, Total Members: %d, Women: %d, Men: %d",
		strip(NAME(1)), males + females, females, males);
end;

function showCalculator()
	if getos() = 10 then
		// Windows
		execsystem("calc.exe");
	elseif getos() = 20 then
		// Android
		execsystem("app:com.google.android.calculator");
	else
		// Windows Universal
		errmsg("Sorry, calculator not supported.");
	endif;
end;

PROC POPSTANLFS_FF

preproc

	userbar(clear);
	userbar(add button, "Say Hello", hello);
	userbar(add button, "Go to...", goto);
	userbar(add button, "Household Summary", showHouseholdSummary);
	userbar(add button, "Calculator", showCalculator);
	userbar(show);


PROC POPSTANLFS_QUEST

preproc

	// Set occurrence labels to match existing data
	// in case where we return to existing data.
	do numeric i = 1 while i <= totocc(DEM_ROSTER)
		setocclabel(DEM_ROSTER(i), NAME(i));
	enddo;

	do numeric i = 1 while i <= totocc(LABOR_FORM)
		if visualvalue(LBPPNO(i)) > 0 then
			setocclabel(LABOR_FORM(i), NAME(visualvalue(LBPPNO(i))));
		endif;
	enddo;


PROC GH_FORM


PROC PROVINCE
preproc
// Retreive province from menu program via pff file parameter
if sysparm("TO_PROVINCE") <> "" then
	PROVINCE = tonumber(sysparm("TO_PROVINCE"));
	
	// Make field protected so that it cannot be modified by
	// interviewer
	setproperty(PROVINCE, "Protected", "Yes");
	
endif;
PROC DISTRICT
preproc
// Retreive district from menu program via pff file parameter
if sysparm("TO_DISTRICT") <> "" then
	DISTRICT = tonumber(sysparm("TO_DISTRICT"));
	
	// Make field protected so that it cannot be modified by
	// interviewer
	setproperty(DISTRICT, "Protected", "Yes");
	
endif;

onfocus

	// Build value set for district based on selected province
	numeric nextEntry = 1;
	forcase DISTRICT_DICT where DI_PROVINCE_CODE = PROVINCE do
		codes(nextEntry) = DI_DISTRICT_CODE;
		labels(nextEntry) = DI_DISTRICT_NAME;
		nextEntry = nextEntry + 1;
	endfor;
	codes(nextEntry) = notappl;
	setvalueset(DISTRICT, codes, labels);

postproc

	// Verify that province and district combination is valid
	if not loadcase(DISTRICT_DICT, PROVINCE, DISTRICT) then
		errmsg("District code %d is not valid for province %d", DISTRICT, PROVINCE);
		reenter;
	endif;
PROC GHDWNO
preproc
// Retreive dwelling unit from menu program via pff file parameter
if sysparm("TO_GHDWNO") <> "" then
	GHDWNO = tonumber(sysparm("TO_GHDWNO"));
	
	// Make field protected so that it cannot be modified by
	// interviewer
	setproperty(GHDWNO, "Protected", "Yes");
	
endif;
PROC GHHHNO
preproc
// Retreive household number from menu program via pff file parameter
if sysparm("TO_GHHHNO") <> "" then
	GHHHNO = tonumber(sysparm("TO_GHHHNO"));
	
	// Make field protected so that it cannot be modified by
	// interviewer
	setproperty(GHHHNO, "Protected", "Yes");
	
endif;
PROC GHAREA
preproc
if sysparm("TO_GHAREA") <> "" then
	GHAREA = tonumber(sysparm("TO_GHAREA"));
	
	// Make field protected so that it cannot be modified by
	// interviewer
	setproperty(GHAREA, "Protected", "Yes");
	
endif;
PROC GHVFDT

preproc

	// Prefill interview with current date
	if visualvalue(GHVFDT) = notappl then
		GHVFDT = sysdate("YYYYMMDD");
	endif;

postproc

	// Fill in string versions of interview date and 7 days before interview date
	// to use fills in question text
	weekBeforeInterviewDate = dayOfWeekToString(dayOfWeek(dateadd(GHVFDT, -8, "d")));


PROC GHHRPL
preproc
if sysparm("TO_GHHRPL") <> "" then
	GHHRPL = tonumber(sysparm("TO_GHHRPL"));
	
	// Make field protected so that it cannot be modified by
	// interviewer
	setproperty(GHHRPL, "Protected", "Yes");
	
endif;
PROC GHINTID
preproc

if sysparm("TO_GHINTID") <> "" then
	GHINTID = tonumber(sysparm("TO_GHINTID"));
	
	// Operator ID will appear in field notes
	setoperatorid(sysparm("TO_GHINTID"));
	
else
	// Allow entry of blank since not interviewer id was passed in from pff
	GHINTID = 999;
endif;
PROC DEM_FORM


PROC DEM_ROSTER


PROC NAME

	// Set occurrence label of demographics to name of individual
	setocclabel(DEM_ROSTER, NAME);

	// Set label of case in case listing to ids plus name of head of household
	setcaselabel(POPSTANLFS_DICT, maketext("P:%d-DI:%d-DW:%d-HH:%d %s", PROVINCE,
					DISTRICT, GHDWNO, GHHHNO, strip(NAME(1))));


PROC DEMREL


onfocus

	// Use appropriate value set for gender
	if DEMSEX = 1 then
		setvalueset(DEMREL, DEMREL_MALE);
	else
		setvalueset(DEMREL, DEMREL_FEMALE);
	endif;

postproc

	// Head of household must be in first row of roster
	if curocc() = 1 and DEMREL(1) <> 1 then
		errmsg("The first household member listed must be the head of household")
		select("Change relationship", DEMREL, "Change name of first member", NAME);
	endif;

	if curocc() <> 1 and DEMREL = 1 then
		errmsg(tr("emDEMRELOneHead"));
		reenter;
	endif;


PROC DEMAG1

preproc

	// Fill in age based on date of birth if year of birth is known
	if DEMBTH_YEAR <> 9999 then
		// Year of birth is known, calculate age
		numeric dateOfBirthForAgeCalc;
		if DEMBTH_DAY = 99 and DEMBTH_MONTH <> 99 then
			// Day is unknown, month is known - assume middle of month
			dateOfBirthForAgeCalc = 15 + DEMBTH_MONTH * 100 + DEMBTH_YEAR * 10000;
		elseif DEMBTH_DAY <> 99 and DEMBTH_MONTH = 99 then
			// Day is known, month unknown - assume July
			dateOfBirthForAgeCalc = DEMBTH_DAY + 7 * 100 + DEMBTH_YEAR * 10000;
		elseif DEMBTH_DAY = 99 and DEMBTH_MONTH = 99 then
			// Day and month both unknown - assume July first
			dateOfBirthForAgeCalc = 0701 + DEMBTH_YEAR * 10000;
		else
			// All known - use actual date
			dateOfBirthForAgeCalc = DEMBTH;
		endif;

		DEMAG1 = datediff(dateOfBirthForAgeCalc, GHVFDT, "y");
		noinput;
	endif;

postproc

	// Ensure that head of household is 15 or over
	if DEMREL = 1 and DEMAG1 < 15 then
		warning(tr("%s is head of household and is only %d years old. Head of household should be at least 15 years old."), strip(NAME), DEMAG1)
		select(tr("Fix relationship"), DEMREL, tr("Fix age"), DEMBTH_DAY, tr("Ignore"), continue);
	endif;

	// Ensure that son/daughter of head is not older than head
	numeric ageDiff = DEMAG1(1) - DEMAG1;
	if DEMREL = 3 and ageDiff < 12 then
		warning(tr("%s is %s's child but the age difference is %d years. Parent should be at least 12 years older than child."),
					strip(NAME), strip(NAME(1)), ageDiff)
		select(tr("Correct relationship of ") + strip(NAME), DEMREL,
			   maketext("Reenter age of %s (%d years)", strip(NAME), DEMAG1), DEMBTH_DAY,
			   maketext("Correct age of %s (%d years)", strip(NAME(1)), DEMAG1(1)), DEMBTH_DAY(1),
			   tr("Ignore"), continue);
	endif;


PROC DEMEDL

preproc

	// Skip level of education for under 5
	ask if DEMAG1 >= 5;

postproc

	// Must be 18 or older for tertiary level education
	if DEMEDL = 7 and DEMAG1 < 18 then
		warning(tr("%s has tertiary level of education but is only %d years old. Must be at least 18 years old for this level of education."),
			strip(NAME), DEMAG1)
		select(tr("Fix age"), DEMBTH_DAY, tr("Fix level of education"), DEMEDL, tr("Ignore"), continue);
	endif;


PROC DEMEDC

preproc

	// Skip currently attending for under 5 and over 29
	ask if DEMAG1 in 5:29;


PROC DEMMORE

	// End roster if no more household members
	if DEMMORE = 2 then
		endgroup;
	endif;


PROC GHHHSIZE

preproc

	// Set household size to size of demographic roster
	GHHHSIZE = totocc(DEM_ROSTER);


PROC GHNELIGIBLE

preproc

	// Compute number of household members aged 15 or above
	GHNELIGIBLE = count(DEM_REC where isEligibleForLabor(curocc()));


PROC LBPPNO

onfocus

	// Build value set from all eligible members of household roster
	numeric nextValueSetEntry = 1;
	do numeric nextPerson = 1 while nextPerson <= totocc(DEM_ROSTER)
		// Check if eligible for labor section
		numeric existingOccInLaborRoster = seek(LBPPNO = PPNO(nextPerson));
		if isEligibleForLabor(nextPerson) and existingOccInLaborRoster = 0 or existingOccInLaborRoster >= curocc() then
			// Add to value set
			codes(nextValueSetEntry) = PPNO(nextPerson);
			labels(nextValueSetEntry) = NAME(nextPerson);
			nextValueSetEntry = nextValueSetEntry + 1;
		endif;
	enddo;
	codes(nextValueSetEntry) = notappl;
	setvalueset(LBPPNO, codes, labels);

postproc

	// Check that person number is valid
	if not LBPPNO in 1:totocc(DEM_ROSTER) then
		errmsg("Invalid line number");
		reenter;
	endif;

	numeric alreadyEntered = 0;
	do numeric i = curocc() + 1 while i <= totocc(LABOR_FORM)
		if visualvalue(LBPPNO(i)) = LBPPNO then
			alreadyEntered = 1;
		endif;
	enddo;
	if alreadyEntered then
		warning("The labor information for %s was already entered. Are you sure you want to enter it again?", strip(NAME(LBPPNO)))
		select("Choose a different household member", LBPPNO, "Ignore", continue);
	endif;

	// Set occurrence label for labor roster to name from demographics roster
	setocclabel(LABOR_FORM, NAME(LBPPNO));
PROC PXYSTART

preproc

	{
		errmsg("Name %s Age %d", NAME(LBPPNO), DEMAG1(LBPPNO));
		if DEMAG1(LBPPNO) >= 15 then
			errmsg("Age >= 15");
		endif;
		if DEMAG1(LBPPNO) < 15 then
			errmsg("Age < 15");
		endif;
		if DEMAG1(LBPPNO) = notappl then
			errmsg("Age is blank");
		endif;
	}

	// Module universe - persons of working age
	if not isEligibleForLabor(LBPPNO) then
		skip to PXYEND;
	else
		PXYSTART = 1;
	endif;


PROC PXYRSP

preproc

	// Skip to next household member if age under 15
	if DEMAG1(LBPPNO) < 15 then
		skip to PXYEND;
	endif;


PROC PXYCHK

	// Skip PXYPPN and PXYREL if respondent themselves is answering questions
	if PXYCHK = 1 then // skip
		nameTag = NAME(LBPPNO);
		skip to EMPSTART;
	else
		nameTag = "";
	endif;


PROC PXYPPN

onfocus

	// Build value set from all eligible members of household roster
	numeric nextValueSetEntry = 1;
	do numeric nextPerson = 1 while nextPerson <= totocc(DEM_ROSTER)
		// Don't allow someone to be proxy for themselves
		if PPNO(nextPerson) <> LBPPNO then
			// Add to value set
			codes(nextValueSetEntry) = PPNO(nextPerson);
			labels(nextValueSetEntry) = NAME(nextPerson);
			nextValueSetEntry = nextValueSetEntry + 1;
		endif;
	enddo;
	codes(nextValueSetEntry) = notappl;
	setvalueset(PXYPPN, codes, labels);


PROC PXYREL

preproc

	// Prefill with relationship from demographic roster for head of household
	if LBPPNO = 1 then
		recode DEMREL(PXYPPN) => PXYREL;
			2 => 1; // spouse
			3 => 2; // son/daughter
			4 => 3; // mother/father
			5 => 4; // other relative
			6,7 => 5; // not related
		endrecode;
		noinput;
	endif;

postproc

	{
		NO LONGER NEEDED DUE TO prefill of PXYREL
	// Ensure that DEMREL and PXYREL are consistent for head of household
	if LBPPNO = 1 then
		// Head of household is subject of interview - check that
		// DEMREL matches PXYREL
		// Coding schemes for the two are different so convert both
		// to DEMREL scheme
		if PXYREL = 1 and DEMREL(PXYPPN) <> 2 or // spouse/partner
		   PXYREL = 2 and DEMREL(PXYPPN) <> 3 or // son/daughter
		   PXYREL = 3 and DEMREL(PXYPPN) <> 4 or // mother/father
		   PXYREL = 4 and DEMREL(PXYPPN) <> 5 or // other relative
		   PXYREL = 5 and not DEMREL(PXYPPN) in 6,7 then // unrelated/domestic worker
			errmsg("Proxy %s has relationship to head %s but has relationship %s in demographic section.", strip(NAME(PXYPPN)), getlabel(PXYREL_VS1, PXYREL), getlabel(DEMREL_VS1, DEMREL(PXYPPN)))
			select("Correct proxy relationship", PXYREL, "Correct proxy name", PXYPPN, "Correct relationship in demographic section", DEMREL(PXYPPN));
		endif;
	endif;
	}


PROC PXYEND

preproc

	PXYEND = 1;


PROC EMPSTART

preproc

	// Module universe - persons of working age
	if DEMAG1(LBPPNO) < 15 then
		skip to EMPEND;
	else
		EMPSTART = 1;
	endif;


PROC EMPPAY

	// Skip to next module for those who worked for pay
	if EMPPAY = 1 then
		skip to EMPEND;
	endif;


PROC EMPPFT

	// Skip to next module for those who ran business for profit
	if EMPPFT = 1 then
		skip to EMPEND;
	endif;


PROC EMPEND

preproc

	EMPEND = 1;


PROC ABSSTART

preproc

	// Module universe for temporary absence module: working age who are not employed at work
	if not(DEMAG1(LBPPNO) >= 15 and EMPFAM = 2) then
		skip to ABSEND;
	else
		ABSSTART = 1;
	endif;


PROC ABSJOB

postproc

	// End of module if no job/business in last week
	if ABSJOB = 2 then
		skip to ABSEND;
	endif;


PROC ABSSEA

preproc

	// Only ask if reason for absence is low/off season
	ask if ABSWHY = 2;


PROC ABSDUR

preproc

	// Only ask if absent due to off-season, education leave, other personal leave, temporary layoff, no clients or materials,
	// bad weather, strike or labour dispute, long-term disability, other
	ask if ABSWHY in 2,7:13;


PROC ABSPAY

preproc

	// Only ask for persons absent due to off season, persons absent for more than 3 months
	// and persons absent not sure when they will return to same job
	ask if ABSDUR in 2,97;


PROC ABSWHY_OTHER

preproc

	// Only ask for other specify if other was selected
	ask if ABSWHY = 13;


PROC ABSEND

preproc

	ABSEND = 1;


PROC AGFSTART

preproc

	// Module universe - persons of working age not working for pay in the reference week
	if not(DEMAG1(LBPPNO) >= 15 and EMPPAY <> 1) then
		skip to AGFEND;
	else
		AGFSTART = 1;
	endif;


PROC AGFCHK

preproc

	//Ask this question if EMPPFT=01 or EMPFAM=01 or ABSWHY=03:06 or (ABSSEA=1 or ABSWHY=07:13 and (ABSDUR=1 or ABSPAY=1))
	ask If (EMPPFT=1 or EMPFAM=1 or ABSWHY in 3:6 or ABSSEA=1 or (ABSWHY in 7:13 and (ABSDUR=1 or ABSPAY=1)));

postproc

	// Ensure that at least one option is checked
	if not checkboxHasSelection(AGFCHK) then
		errmsg("Please choose at least one option");
		reenter;
	endif;




PROC AGFANY

PREPROC

	//If ABSJOB=2 or ABSWHY=1 or (ABSWHY in 7:13 and (ABSPAY=2 or ABSPAY=97)) or ABSSEA=2
	ask if ABSJOB=2 or ABSWHY=1 or (ABSWHY in 7:13 and (ABSPAY=2 or ABSPAY=97));


PROC AGFMKT

preproc

	// Ask question only if option 1 or 2 is selected and option 3 is not selected or
	// didn't previously report work but worked in farming/fishing last week
	ask if ((pos("1", AGFCHK) > 0 or pos("2", AGFCHK) >0) and pos("3", AGFCHK) = 0) or AGFANY = 1;


PROC AGFMAI

	//if the individual produce for sale and family use

preproc

	ask if AGFMKT = 3 ;

postproc

	// Warning: change the next to MULTJB when grouping the modules
	if AGFMAI = 1 then
		skip to AGFEND;

	elseif AGFMAI = 2 then
		skip to AGFHIR

	elseif AGFMAI = 3 then

		skip to AGFHIS;
	endif;


PROC AGFHIS

preproc

	ask if AGFMAI = 3 ;


PROC AGFHIR

preproc

	ask if AGFMKT = 2 or AGFMAI =2 or AGFHIS = 2 ;


PROC AGFGDS000

preproc

	ask if AGFHIR = 2;


PROC AGFOCC000

preproc

	ask if AGFHIR = 2;


PROC AGFDYS

preproc

	ask if AGFHIR = 2;


PROC AGFHRS

preproc

	ask if AGFHIR = 2;


PROC AGFEND

preproc

	AGFEND = 1;


PROC MJBSTART

preproc

	// Need to add the missing variables from the Agriculture Module (AGFMKT=01 OR AGFMAI=01 OR AGFHIS=01 OR AGFHIR=01)
	if not( DEMAG1(LBPPNO) >= 15 AND ( EMPPAY=1 or  (length(AGFCHK) = 1 and AGFCHK="3")) ) then
		skip to MJBEND;
	else
		MJBSTART = 1;
	endif;


PROC MJBPLC

onfocus

	// Customize first entry of value set based on proxy/no proxy
	codes(1) = 1;
	codes(2) = 2;
	codes(3) = 3;
	codes(4) = 4;
	codes(5) = 5;
	codes(6) = 6;
	codes(7) = 7;
	codes(8) = 8;
	codes(9) = notappl;

	if PXYCHK = 1 then
		labels(1) = getlabel(MJBPLC_VS_NO_PROXY, 1);
	else
		labels(1) = maketext(getlabel(MJBPLC_VS_PROXY, 1), strip(NAME(LBPPNO)));
	endif;
	labels(2) = getlabel(MJBPLC_VS1, 2);
	labels(3) = getlabel(MJBPLC_VS1, 3);
	labels(4) = getlabel(MJBPLC_VS1, 4);
	labels(5) = getlabel(MJBPLC_VS1, 5);
	labels(6) = getlabel(MJBPLC_VS1, 6);
	labels(7) = getlabel(MJBPLC_VS1, 7);
	labels(8) = getlabel(MJBPLC_VS1, 8);
	setvalueset(MJBPLC, codes, labels);


PROC MJBSAL

preproc

	ask if MJBEST in 2:5;


PROC MJBTRI

preproc

	ask if MJBEST in 3:4 or (MJBEST in 2,5 and MJBSAL = 1);


PROC MJBDEE

preproc

	ask if MJBEST in 3:4 and  MJBSAL <> 1;

onfocus

	// Create dynamic value set to put you/NAME as fill in second option
	codes(1) = 1;
	labels(1) = getlabel(MJBDEE_VS1, 1);
	codes(2) = 2;
	if PXYCHK = 1 then
		labels(2) = tr("YOU ARE RESPONSIBLE FOR THAT");
	else
		labels(2) = maketext("%s IS RESPONSIBLE FOR THAT", toupper(strip(NAME(LBPPNO))));
	endif;

	codes(3) = notappl; // mark end of value set in array
	setvalueset(MJBDEE, codes, labels);


PROC MJBINS

preproc

	ask if MJBEST in 3:4 or  (MJBEST in 2,5 and MJBSAL = 1);


PROC MJBEND

preproc

	MJBEND = 1;


PROC DMWSTART

preproc

	if not(MJBPLC = 2 and MJBINS = 4 and MJBTRI = 1) then
		skip to DMWEND;
	else
		DMWSTART = 1;
	endif;


PROC DMWHBE

preproc

	ask if DMWHHB = 1;


PROC DMWCHK

preproc

	ask if DMWHHB in 2,3 or DMWHBE = 2;


PROC DMWOCC

preproc

	ask if DMWCHK = 1;


PROC DMWEND

preproc

	DMWEND = 1;


PROC IMJSTART

preproc

	// Need to add the missing variables from the Agriculture Module (AGFMKT=01 OR AGFMAI=01 OR AGFHIS=01 OR AGFHIR=01)
	if not( DEMAG1(LBPPNO) >= 15 AND ( EMPPAY=1 or  (length(AGFCHK) = 1 and AGFCHK="3")) ) then
		skip to IMJEND;
	else
		IMJSTART = 1;
	endif;


PROC IMJREF

preproc

	ask if MULTJB in 1:2 and DMWCHK <> 1;


PROC IMJNAM

preproc

	ask if IMJREF = 1;


PROC IMJMAC

preproc

	ask if MULTJB in 1,2 and DMWCHK <> 1;


PROC IMJEND

preproc

	IMJEND = 1;


PROC DMJSTART

preproc

	// Need to add the missing variables from the Agriculture Module (AGFMKT=01 OR AGFMAI=01 OR AGFHIS=01 OR AGFHIR=01)
	if not( DEMAG1(LBPPNO) >= 15 AND ( EMPPAY=1 or  (length(AGFCHK) = 1 and AGFCHK="3")) ) then
		skip to DMJEND;
	else
		DMJSTART  = 1;
	endif;


PROC DMJSYR

preproc

	ask if MULTJB in 1,2;


PROC DMJSMO

preproc

	ask if MULTJB in 1,2;


PROC DMJEND

preproc

	DMJEND = 1;


PROC OMJSTART

preproc

	// Need to add the missing variables from the Agriculture Module (AGFMKT=01 OR AGFMAI=01 OR AGFHIS=01 OR AGFHIR=01)
	if not( DEMAG1(LBPPNO) >= 15 AND ( EMPPAY=1 or  (length(AGFCHK) = 1 and AGFCHK="3")) ) then
		skip to OMJEND;
	else
		OMJSTART = 1;
	endif;


PROC OMJMTD

preproc

	ask if MULTJB in 1:2 and DMWCHK <> 1;


PROC OMJTLE

preproc

	ask if MULTJB in 1:2 and DMWCHK <> 1;


PROC OMJISCO_LOOKUP

preproc

	ask if MULTJB in 1:2 and DMWCHK <> 1;

		// Save start value of field so we only do code lookup
		// if it changes.
		string initialOMJISCO_LOOKUP = OMJISCO_LOOKUP;

postproc

	// Use selcase to show codes based on lookup key
	// but only if field has changed or has not been coded
	// yet. This avoids it being shown every time you pass through
	// the field.
	if initialOMJISCO_LOOKUP <> OMJISCO_LOOKUP or strip(OMJISCO) = "" then
		{
		 // this version uses "start with"
		 if selcase(ISCO_INDEX_DICT, tolower(strip(OMJISCO_LOOKUP))) include (ISCO_08) then
		}
		// This version uses substring
		if selcase(ISCO_INDEX_DICT, "")
			include (ISCO_08)
			where pos(tolower(strip(OMJISCO_LOOKUP)), ENGLISH_TITLE) > 0 then
			// Copy selected code to OMJISCO field
			OMJISCO = ISCO_08;
		else
			// Canceled - keep them in this field
			reenter;
		endif;
	endif;


PROC OMJEND

preproc

	OMJEND = 1;


PROC WTSTART

preproc

	if not( DEMAG1(LBPPNO) >= 15 and EMPPAY=1 or AGFCHK = "3"  ) then
		skip to WTEND;
	else
		WTSTART = 1;
	endif;


PROC ACDYSM

	numeric AtWorkCheck = 0;
	numeric i;

	// loop to check persons who worked at least one day in reference week.
	do i=1 while i <= 7
		AtWorkCheck = AtWorkCheck + pos( edit( "9" , i ) , ACDYSM);
		i = i + 1;
	enddo;

	if AtWorkCheck = 0 and pos( "8", ACDYSM ) > 0 then // SKip pattern for those that did not work in the reference week.
		skip to USHRSM;
	endif;

	if AtWorkCheck <> 0 and pos( "8" , ACDYSM ) > 0 then
		errmsg("Option 8 can only be used for person who were absent the whole week."  );
		reenter;
	endif;

	// Ensure that at least one option is checked
	if not checkboxHasSelection(ACDYSM) then
		errmsg("Please choose at least one option");
		reenter;
	endif;



PROC ACHRSM


preproc

	// Ask hours only for days worked in last week
	if pos( edit( "9" , curocc(ACHRSM_ROSTER) ) , ACDYSM ) = 0 then
		if curocc(ACHRSM_ROSTER)=7  then // to be made dynamic
			endgroup;
		else
			skip to ACHRSM(curocc(LABOR_FORM), curocc(ACHRSM_ROSTER)+1);
		endif;
	endif;


postproc

	if  ACHRSM( curocc(LABOR_FORM) , curocc(ACHRSM_ROSTER) ) = 98.0 then
		skip to ACHRM1;
	endif;


PROC ACHRM1

preproc

	ask if ACHRSM has 98;

postproc

	if ACHRM1 <> 998 then
		skip to ACEQUM;
	elseif ACHRM1 = 998 then
		skip to USHRSM;
	endif;


PROC ACHRCM


	if ACHRCM = 1 then
		skip to NUMJBS;
	endif;


PROC ACHRSUMMJ

preproc

	ACHRSUMMJ = sum(ACHRSM);
	noinput;



PROC ACHRUM

	if ACHRUM = 998 then
		skip to USHRSM;
	endif;


PROC ACEQUM

	if ACEQUM = 1 then
		skip to NUMJBS;
	endif;


PROC WTEND

preproc

	WTEND = 1;


PROC SRHSTART

preproc;

	//Module universe - persons of working age not in employment - taken from Kieran's questionnaire for Search Module Logic
	if DEMAG1(LBPPNO) < 15 OR (EMPPAY=1 OR (pos("3",AGFCHK)>0) OR AGFMKT  = 1 OR AGFMKT=3) then
		SRHSTART = 0;
	else
		SRHSTART = 1;
	endif;

postproc

	if SRHSTART = 0 then
		skip to END_SRH;
	endif;


PROC SRHJOB

preproc

	ask if DEMAG1(LBPPNO)>=15 and (AGFANY=2 or AGFMKT=3);


PROC SRHBUS

preproc

	//Only ask of those who did not search for a job (and were of working age and not employed)
	ask if SRHJOB=2;


PROC SRHMTD

preproc

	//only ask of those who looked for a job or tried to start a business
	ask if SRHJOB=1 or SRHBUS=1;


PROC SRHMTD_OTHER

preproc

	//only ask for those who said the method was other
	ask if SRHMTD=13;


PROC SRHACT

preproc

	//only ask for those who indicated reading advertisements as their main method
	ask if SRHMTD=3;


PROC SRHOTH

preproc

	//only ask for those who did some additional search activity (SRHACT=1)
	ask if SRHACT=1;


PROC SRHOTH_OTHER

preproc

	//only ask for those who said the method was other
	ask if SRHACT=13;


PROC SRHDUR

preproc

	//Only ask of those who looked for a job or tried to start a business
	ask if SRHJOB=1 or SRHBUS=1;


PROC SRHYER

preproc

	ask if SRHJOB=2 and SRHBUS=2;


PROC SRHDWY

preproc

	//Only ask those who wanted to work
	ask if SRHDWY=1;


PROC SRHDWY_OTH

preproc

	//Only ask those with a 'Other' reason for not seeking work


PROC SRHFTR

preproc

	//Ask those who gave 'Waiting to start a new job or business' as the reason for not seeking work (SRHDWY=4)
	//or those who gave their reason for absence as waiting to start a new job or business (ABSWHY=1)
	ask if (ABSWHY=1 and AGFANY=2) or SRHDWY=4;


PROC SRHFAN

preproc

	//Ask for future starters
	ask if (ABSWHY=1 and AGFANY=2) OR SRHDWY=4;


PROC END_SRH

preproc

	END_SRH=1;


PROC INDIVEND

preproc

	INDIVEND = 1;

postproc

	// End looping form after completing last individual interview
	if curocc() = GHNELIGIBLE then
		endgroup;
	endif;


