/*
  test

*!  VERSION 1.3.0  07sep2011

*/

VERSION 11.0

INCLUDE _std_xlarge
DEFINE _dlght 250
INCLUDE header

HELP hlp1, view("help test")
RESET res1

SCRIPT PREINIT
BEGIN
	create STRING main_spec_tmp
	create STRING main_spec_tmp2
	create STRING main_spec_childdlg
	create STRING main_spec_dlgname
	create STRING main_spec_child_result
	create STRING main_spec_cmd_output

	create ARRAY main_spec_list

	create SVECTOR mainSPECCommandDisplayList
	create SVECTOR mainSPECCommandList
	create SVECTOR mainSPECCommandValues
	create SVECTOR mainSPECStatusList
	create SVECTOR mainSPECChildDialogList

	create DOUBLE main_spec_count
	create DOUBLE main_spec_position
	create DOUBLE main_spec_arraysize

	main.bu_spec_edit.disable
	main.bu_spec_disable.disable
	main.bu_spec_enable.disable

	create STRING has_svy
	program check_svy
END

SCRIPT POSTINIT
BEGIN
	mainSPECCommandDisplayList.copyToArray main_spec_list
	main.lb_specBuild.repopulate
END

PROGRAM check_svy
BEGIN
	call has_svy.setvalue e(prefix)
	if has_svy.iseq("svy") {
		call opt.ck_nosvyadjust.enable
	}
	else {
		call opt.ck_nosvyadjust.disable
	}
END

PROGRAM ON_DOTPROMPT
BEGIN
	call program check_svy
END


DIALOG main, label("test - Test linear hypotheses after estimation")	///
	tabtitle("Main")
BEGIN
  TEXT tx_spec			_lft	_top	220	.,		///
	label("Specification:")

  LISTBOX lb_specBuild		@	_ss	@	160,		///
	contents(main_spec_list)					///
	ondblclick(program main_spec_edit)				///
	onselchange(program main_spec_sel_changed)			///
	nomemory
  BUTTON bu_spec_create		240	@	80	.,		///
	label("Create")							///
	onpush(program main_spec_create)				///
	tooltip("Edit specification")
  BUTTON bu_spec_edit		@	_ms	@	.,		///
	label("Edit")							///
	onpush(program main_spec_edit)					///
	tooltip("Edit specification")
  BUTTON bu_spec_disable	@	_ms	@	.,		///
	label("Disable")						///
	onpush(program main_spec_disable)				///
	tooltip("Click to disable a specification")
  BUTTON bu_spec_enable		@	_ms	@	.,		///
	label("Enable")							///
	onpush(program main_spec_enable)				///
	tooltip("Click to enable a previously disabled specification")
  TEXTBOX tb_spec_note		_lft	+90	_iwd	_tb2,		///
	label(`"Press "Create" to define a specification"')
END

PROGRAM main_spec_create
BEGIN
	call main_spec_count.setvalue class mainSPECChildDialogList.size
	call main_spec_count.increment
	call main_spec_tmp.setvalue "main_spec_"
	call main_spec_tmp.append class main_spec_count.value
	call main_spec_tmp.withvalue main_spec_childdlg.setvalue @
	call program main_show_spec_child
END

PROGRAM main_spec_edit
BEGIN
	call mainSPECCommandDisplayList.findstr class			///
		main.lb_specBuild.value
	call main_spec_position.setvalue class				///
		mainSPECCommandDisplayList.position
	call main_spec_position.withvalue				///
		main_spec_childdlg.setvalue				///
		class mainSPECChildDialogList.values[@]
	call program main_show_spec_child_edit
END

PROGRAM main_show_spec_child
BEGIN
	call main_spec_tmp.setvalue "test_syntax AS "
	call main_spec_tmp.append class main_spec_childdlg.value
	call main_spec_tmp.withvalue create CHILD @
	call main_spec_childdlg.withvalue @.setExitString		///
		"main_spec_child_result"
	call main_spec_childdlg.withvalue @.setExitAction		///
		"program main_spec_child_create_done"

	call main_spec_count.withvalue					///
		main_spec_tmp.setvalue "Specification @"
	call main_spec_childdlg.withvalue				///
		main_spec_tmp2.setvalue "@.settitle"
	call main_spec_tmp2.append `" ""'
	call main_spec_tmp2.append class main_spec_tmp.value
	call main_spec_tmp2.append `"""'
	call main_spec_tmp2.withvalue @
END

PROGRAM main_show_spec_child_edit
BEGIN
	call main_spec_tmp.setvalue "test_syntax AS "
	call main_spec_tmp.append class main_spec_childdlg.value
	call main_spec_tmp.withvalue create CHILD @
	call main_spec_childdlg.withvalue @.setExitString		///
		"main_spec_child_result"
	call main_spec_childdlg.withvalue @.setExitAction		///
		"program main_spec_child_edit_done"

	call main_spec_position.withvalue				///
		main_spec_tmp.setvalue					///
		class mainSPECCommandDisplayList.values[@]
	call main_spec_childdlg.withvalue				///
		main_spec_tmp2.setvalue "@.settitle"
	call main_spec_tmp2.append `" ""'
	call main_spec_tmp2.append class main_spec_tmp.value
	call main_spec_tmp2.append `"""'
	call main_spec_tmp2.withvalue @
END

PROGRAM main_spec_child_create_done
BEGIN
	call main_spec_arraysize.setvalue class				///
		mainSPECChildDialogList.size
	call main_spec_arraysize.increment

	/* Store child dialog name */
	call mainSPECChildDialogList.store class			///
		main_spec_arraysize.value class main_spec_childdlg.value

	/* Store listbox display specification string */
	call main_spec_arraysize.withvalue				///
		mainSPECCommandDisplayList.store class			///
		main_spec_arraysize.value "Specification @"

	/* Store child dialog result string */
	call mainSPECCommandList.store class				///
		main_spec_arraysize.value				///
		class main_spec_child_result.value

	/* Store child dialog result string for final command
	   For disable/enable */
	call mainSPECCommandValues.store class				///
		main_spec_arraysize.value				///
		class main_spec_child_result.value

	/* Store disable/enable status */
	call mainSPECStatusList.store class				///
		main_spec_arraysize.value "active"

	call mainSPECCommandDisplayList.copyToArray main_spec_list
	call main.lb_specBuild.repopulate
	call main_spec_arraysize.withvalue				///
		main.lb_specBuild.setvalue "Specification @"
END

PROGRAM main_spec_child_edit_done
BEGIN
	call mainSPECChildDialogList.findstr class			///
		main_spec_childdlg.value
	call main_spec_position.setvalue class				///
		mainSPECChildDialogList.position
	call main_spec_position.withvalue main_spec_tmp.setvalue	///
		class mainSPECStatusList.values[@]
	if main_spec_tmp.iseq("active") {
		call main_spec_position.withvalue			///
			mainSPECCommandDisplayList.store class		///
			main_spec_position.value "Specification @"
		call mainSPECCommandValues.store class			///
			main_spec_position.value class			///
			main_spec_child_result.value
		call main_spec_position.withvalue			///
			main_spec_tmp2.setvalue "Specification @"
	}
	else {
		call main_spec_position.withvalue			///
			main_spec_tmp2.setvalue "Specification @"

		call main_spec_tmp2.withvalue				///
			mainSPECCommandDisplayList.store		///
			class main_spec_position.value "@ (disabled)"
	}

	call mainSPECCommandList.store class				///
		main_spec_position.value class				///
		main_spec_child_result.value

	call mainSPECCommandDisplayList.copyToArray main_spec_list
	call main.lb_specBuild.repopulate
	call main.lb_specBuild.setvalue class main_spec_tmp2.value
END

PROGRAM main_spec_sel_changed
BEGIN
	call mainSPECCommandDisplayList.findstr class			///
		main.lb_specBuild.value
	call main_spec_position.setvalue class				///
		mainSPECCommandDisplayList.position
	call main_spec_position.withvalue main_spec_tmp.setvalue	///
		class mainSPECStatusList.values[@]
	if main_spec_position {
		call main.bu_spec_edit.enable
		call main_spec_position.withvalue			///
			main.tb_spec_note.setlabel			///
			class mainSPECCommandList.values[@]
	}

	if main_spec_tmp.iseq("disable") {
		call main.bu_spec_enable.enable
		call main.bu_spec_disable.disable
	}
	if main_spec_tmp.iseq("active") {
		call main.bu_spec_enable.disable
		call main.bu_spec_disable.enable
	}
END

PROGRAM main_spec_disable
BEGIN
	call mainSPECCommandDisplayList.findstr class			///
		main.lb_specBuild.value
	call main_spec_position.setvalue class				///
		mainSPECCommandDisplayList.position

	call main_spec_tmp.setvalue class main.lb_specBuild.value
	call mainSPECStatusList.store class				///
		main_spec_position.value "disable"
	call mainSPECCommandValues.store class main_spec_position.value ""
	call main_spec_tmp.withvalue					///
		mainSPECCommandDisplayList.store			///
		class main_spec_position.value "@ (disabled)"

	call mainSPECCommandDisplayList.copyToArray main_spec_list
	call main_spec_tmp.withvalue main_spec_tmp2.setvalue		///
		"@ (disabled)"
	call main.lb_specBuild.repopulate
	call main.lb_specBuild.setvalue class main_spec_tmp2.value
END

PROGRAM main_spec_enable
BEGIN
	call mainSPECCommandDisplayList.findstr class			///
		main.lb_specBuild.value
	call main_spec_position.setvalue class				///
		mainSPECCommandDisplayList.position

	call mainSPECStatusList.store class				///
		main_spec_position.value "active"
	call main_spec_position.withvalue				///
		mainSPECCommandValues.store				///
		class main_spec_position.value class			///
		mainSPECCommandList.values[@]

	call main_spec_position.withvalue				///
		mainSPECCommandDisplayList.store			///
		class main_spec_position.value "Specification @"

	call mainSPECCommandDisplayList.copyToArray main_spec_list
	call main_spec_position.withvalue				///
		main_spec_tmp2.setvalue class				///
		mainSPECCommandDisplayList.values[@]
	call main.lb_specBuild.repopulate
	call main.lb_specBuild.setvalue class main_spec_tmp2.value
END

DIALOG opt, label("")  tabtitle("Options")
BEGIN
  CHECKBOX ck_mtest		_lft	_top	_cwd3_1	_ht10,		///
	groupbox							///
	clickon(script opt_mtest_on)					///
	clickoff(script opt_mtest_off)					///
	label("Do multiple testing")
  TEXT tx_mtest2		_ilft	_ss	_cwd3_4	.,		///
	label("Adjust p-values by:")
  RADIO rb_noadjust		@	_ss	@	., first	///
	option(mtest(noadjust))						///
	label("No adjustment")
  RADIO rb_bonferroni		@	_ss	@	.,		///
	option(mtest(bonferroni))					///
	label("Bonferroni")
  RADIO rb_holm			@	_ss	@	.,		///
	option(mtest(holm))						///
	label("Holm")
  RADIO rb_sidak		@	_ss	@	., last		///
	option(mtest(sidak))						///
	label("Sidak")
  CHECKBOX ck_coef		_lft3_2	_top	_comb3_1 .,		///
	option(coef)							///
	label("Display estimated constrained coefficients")
  CHECKBOX ck_accumulate	@	_ms	@	.,		///
	option(accumulate)						///
	label("Test hypothesis jointly with previously tested hypotheses")
  CHECKBOX ck_notest		@	_ms	@	.,		///
	option(notest)							///
	label("Suppress the output")
  CHECKBOX ck_common		@	_ms	@	.,		///
	option(common)							///
	label("Test only variables common to all the equations")
  CHECKBOX ck_constant		@	_ms	@	.,		///
	option(constant)						///
	label("Include the constant in coefficients to be tested")
  CHECKBOX ck_nosvyadjust	_lft	_xls	_iwd	.,		///
	option(nosvyadjust)						///
	label("Compute unadjusted Wald tests for survey results")
  CHECKBOX ck_minimum		_lft	_ls	_iwd	.,		///
	option(minimum)							///
	label("Perform test with the constant, then drop terms until the test becomes")
  TEXT tx_min			_cksep	_vss	_inwd	.,		///
	label("nonsingular, then test without the constant on the remaining terms")
END

SCRIPT opt_mtest_on
BEGIN
	opt.tx_mtest2.enable
	opt.rb_noadjust.enable
	opt.rb_bonferroni.enable
	opt.rb_holm.enable
	opt.rb_sidak.enable
	opt.ck_accumulate.disable
	opt.ck_notest.disable
END

SCRIPT opt_mtest_off
BEGIN
	opt.tx_mtest2.disable
	opt.rb_noadjust.disable
	opt.rb_bonferroni.disable
	opt.rb_holm.disable
	opt.rb_sidak.disable
	opt.ck_accumulate.enable
	opt.ck_notest.enable
END

PROGRAM opt_output
BEGIN
	if opt.ck_mtest {
		option radio(opt rb_noadjust rb_bonferroni rb_holm rb_sidak)
	}
	option opt.ck_coef
	if opt.ck_accumulate.isenabled() {
		option opt.ck_accumulate
	}
	if opt.ck_notest.isenabled() {
		option opt.ck_notest
	}
	option opt.ck_common
	option opt.ck_constant
	option opt.ck_nosvyadjust
	option opt.ck_minimum
END

PROGRAM command
BEGIN
	call mainSPECCommandValues.copyToString main_spec_cmd_output

	put "test "
	if main_spec_cmd_output.iseq("") {
                stopbox stop "Specification not defined. "             ///
"Please enter a specification before proceeding."
	}
	put main_spec_cmd_output
	beginoptions
		put " " /program opt_output
	endoptions
END
