/*
	testnl

*! VERSION 2.0.3  21jun2011

*/

VERSION 12.0

INCLUDE _std_large
DEFINE _dlght 320
INCLUDE header

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

SCRIPT PREINIT
BEGIN
	create STRING main_hypotmp
	create STRING main_hypotmp2
	create STRING main_hypo_childdlg
	create STRING main_hypo_child_result
	create STRING main_hypo_cmd_output

	create ARRAY main_hypo_list

	create SVECTOR mainHYPOCommandDisplayList
	create SVECTOR mainHYPOCommandList
	create SVECTOR mainHYPOCommandValues
	create SVECTOR mainHYPOStatusList
	create SVECTOR mainHYPOChildDialogList
	create SVECTOR mainHYPOHasNoError

	create DOUBLE main_hypo_count
	create DOUBLE main_hypo_position
	create DOUBLE main_hypo_arraysize

	main.bu_hypo_edit.disable
	main.bu_hypo_disable.disable
	main.bu_hypo_enable.disable
END

DIALOG main, tabtitle("Main")						///
	label("testnl - Test nonlinear hypotheses after estimation")
BEGIN
  TEXT tx_hypo			_lft	_top	_comb4_1 .,		///
	label("Hypotheses:")
  DEFINE holdy @y
  LISTBOX lb_hypoBuild		_lft	_ss	200	95,		///
	contents(main_hypo_list)					///
	ondblclick(program main_hypo_edit)				///
	onselchange(program main_hypo_sel_changed)			///
	nomemory
  BUTTON bu_hypo_new		+210	@	80	.,		///
	label("Create...")						///
	onpush(program main_hypo_create)				///
	tooltip("Create hypotheses")
  BUTTON bu_hypo_edit		@	_ms	@	.,		///
	label("Edit")							///
	onpush(program main_hypo_edit)					///
	tooltip("Edit hypotheses")
  BUTTON bu_hypo_disable	@	_ms	@	.,		///
	label("Disable")						///
	onpush(program main_hypo_disable)				///
	tooltip("Click to disable an hypotheses")
  BUTTON bu_hypo_enable	@	_ms	@	.,		///
	label("Enable")							///
	onpush(program main_hypo_enable)				///
	tooltip("Click to enable a previously disabled hypotheses")
  TEXTBOX tb_hypo_note		_lft	+25	_iwd	_tb2,		///
	label(`"Press "Create" to define an hypotheses"')

  CHECKBOX ck_mtest		_lft	+35	_cwd3_1	_ht10,		///
	groupbox							///
	clickon(script mtest_on)					///
	clickoff(script mtest_off)					///
	label("Do multiple testing")
  DEFINE holdy @y
  TEXT tx_mtest2		_ilft	_ss	_cwd3_4	.,		///
	label("Adjust p-values by:")
  RADIO rb_noad			@	_ss	@	., first	///
	option(mtest(noadjust))						///
	label("No adjustment")
  RADIO rb_bonf			@	_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_nsvy		_lft3_2	holdy	_comb3_1 .,		///
	option(nosvyadjust)						///
	label("Compute unadjusted Wald tests for survey results")
  CHECKBOX ck_iter		_lft3_2	_ls	_ckwd	.,		///
	clickon(script iter_on)						///
	clickoff(script iter_off)					///
	label("")
  SPINNER sp_iter		_cksep	@	_spwd	.,		///
	default(100)							///
	min(2)								///
	max(1000000000)							///
	option(iterate)							///
	label("Maximum number of iterations")
  DEFINE holdx @x
  TEXT tx_iter			_spsep	@	_ckspr3c .,		///
	label("Maximum number of iterations used")
  TEXT tx_iter2			holdx	_ss	_comb3_3 .,		///
	label("to find the optimal step size for numerical")
  TEXT tx_iter3			@	_vss	@	.,		///
	label("derivatives of the hypotheses to be tested")
END

PROGRAM main_hypo_create
BEGIN
	call main_hypo_count.setvalue class mainHYPOChildDialogList.size
	call main_hypo_count.increment
	call main_hypo_count.withvalue main_hypo_childdlg.setvalue	///
		"main_hypo@"
	call program main_show_hypo_child
END

PROGRAM main_hypo_edit
BEGIN
	call mainHYPOCommandDisplayList.findstr class main.lb_hypoBuild.value
	call main_hypo_position.setvalue class				///
		mainHYPOCommandDisplayList.position
	call main_hypo_position.withvalue main_hypo_childdlg.setvalue	///
		class mainHYPOChildDialogList.values[@]

	call program main_show_hypo_child_edit
END

PROGRAM main_show_hypo_child
BEGIN
	call main_hypo_childdlg.withvalue create CHILD testnl_hypo AS @
	call main_hypo_childdlg.withvalue @.setExitString		///
		"main_hypo_child_result"
	call main_hypo_childdlg.withvalue @.setExitAction		///
		"program main_hypo_child_done"

	call main_hypo_count.withvalue main_hypotmp.setvalue "Hypotheses @"
	call main_hypo_childdlg.withvalue main_hypotmp2.setvalue "@.settitle"
	call main_hypotmp2.append `" ""'
	call main_hypotmp2.append class main_hypotmp.value
	call main_hypotmp2.append `"""'
	call main_hypotmp2.withvalue @
END

PROGRAM main_show_hypo_child_edit
BEGIN
	call main_hypo_childdlg.withvalue create CHILD testnl_hypo AS @
	call main_hypo_childdlg.withvalue @.setExitString		///
		"main_hypo_child_result"
	call main_hypo_childdlg.withvalue @.setExitAction		///
		"program main_hypo_child_done"

	call main_hypo_position.withvalue main_hypotmp.setvalue	///
		class mainHYPOCommandDisplayList.values[@]
	call main_hypo_childdlg.withvalue main_hypotmp2.setvalue "@.settitle"
	call main_hypotmp2.append `" ""'
	call main_hypotmp2.append class main_hypotmp.value
	call main_hypotmp2.append `"""'
	call main_hypotmp2.withvalue @
END

PROGRAM main_hypo_child_done
BEGIN
	call mainHYPOChildDialogList.findstr class main_hypo_childdlg.value
	call main_hypo_position.setvalue class				///
		mainHYPOChildDialogList.position
	if main_hypo_position {
		call main_hypo_position.withvalue main_hypotmp.setvalue ///
			class mainHYPOStatusList.values[@]
		if main_hypotmp.iseq("active") {
			call main_hypo_position.withvalue		///
				mainHYPOCommandDisplayList.store class	///
				main_hypo_position.value "Hypotheses @"
			call mainHYPOCommandValues.store class		///
				main_hypo_position.value class		///
				main_hypo_child_result.value
			call main_hypo_position.withvalue		///
				main_hypotmp2.setvalue "Hypotheses @"
		}
		else {
			call main_hypo_position.withvalue		///
				main_hypotmp2.setvalue "Hypotheses @"

			call main_hypotmp2.withvalue			///
				mainHYPOCommandDisplayList.store	///
				class main_hypo_position.value "@ (disabled)"
		}

		call mainHYPOCommandList.store class			///
			main_hypo_position.value class			///
			main_hypo_child_result.value

		call mainHYPOCommandDisplayList.copyToArray main_hypo_list
		call main.lb_hypoBuild.repopulate
		call main.lb_hypoBuild.setvalue class main_hypotmp2.value
	}
	else {
		call main_hypo_arraysize.setvalue class		///
			mainHYPOChildDialogList.size
		call main_hypo_arraysize.increment

		/* Store child dialog name */
		call mainHYPOChildDialogList.store class		///
			main_hypo_arraysize.value class			///
			main_hypo_childdlg.value

		call main_hypo_arraysize.withvalue			///
			mainHYPOCommandDisplayList.store class		///
			main_hypo_arraysize.value "Hypotheses @"

		/* Store child dialog result string */
		call mainHYPOCommandList.store class			///
			main_hypo_arraysize.value			///
			class main_hypo_child_result.value

		/* Store child dialog result string for final command
		   For disable/enable */
		call mainHYPOCommandValues.store class			///
			main_hypo_arraysize.value class		///
			main_hypo_child_result.value

		/* Store disable/enable status */
		call mainHYPOStatusList.store				///
			class main_hypo_arraysize.value "active"

		call mainHYPOCommandDisplayList.copyToArray main_hypo_list
		call main.lb_hypoBuild.repopulate
		call main_hypo_arraysize.withvalue			///
			main.lb_hypoBuild.setvalue "Hypotheses @"
	}

	call program HYPOckHasNoErrorOpt
END

PROGRAM main_hypo_sel_changed
BEGIN
	call mainHYPOCommandDisplayList.findstr class main.lb_hypoBuild.value
	call main_hypo_position.setvalue class			///
		mainHYPOCommandDisplayList.position
	call main_hypo_position.withvalue main_hypotmp.setvalue	///
		class mainHYPOStatusList.values[@]
	if main_hypo_position {
		call main.bu_hypo_edit.enable
		call main_hypo_position.withvalue			///
			main.tb_hypo_note.setlabel			///
			class mainHYPOCommandList.values[@]
	}

	if main_hypotmp.iseq("disable") {
		call main.bu_hypo_enable.enable
		call main.bu_hypo_disable.disable
	}
	if main_hypotmp.iseq("active") {
		call main.bu_hypo_enable.disable
		call main.bu_hypo_disable.enable
	}
END

PROGRAM main_hypo_disable
BEGIN
	call mainHYPOCommandDisplayList.findstr class main.lb_hypoBuild.value
	call main_hypo_position.setvalue class				///
		mainHYPOCommandDisplayList.position

	call main_hypotmp.setvalue class main.lb_hypoBuild.value
	call mainHYPOStatusList.store class main_hypo_position.value "disable"
	call mainHYPOCommandValues.store class main_hypo_position.value ""
	call main_hypotmp.withvalue mainHYPOCommandDisplayList.store	///
		class main_hypo_position.value "@ (disabled)"

	call mainHYPOCommandDisplayList.copyToArray main_hypo_list
	call main_hypotmp.withvalue main_hypotmp2.setvalue "@ (disabled)"
	call main.lb_hypoBuild.repopulate
	call main.lb_hypoBuild.setvalue class main_hypotmp2.value
END

PROGRAM main_hypo_enable
BEGIN
	call mainHYPOCommandDisplayList.findstr class main.lb_hypoBuild.value
	call main_hypo_position.setvalue class				///
		mainHYPOCommandDisplayList.position

	call mainHYPOStatusList.store class main_hypo_position.value "active"
	call main_hypo_position.withvalue mainHYPOCommandValues.store	///
		class main_hypo_position.value class			///
		mainHYPOCommandList.values[@]

	call main_hypo_position.withvalue				///
		mainHYPOCommandDisplayList.store			///
		class main_hypo_position.value "Hypotheses @"

	call mainHYPOCommandDisplayList.copyToArray main_hypo_list
	call main_hypo_position.withvalue main_hypotmp2.setvalue	///
		class mainHYPOCommandDisplayList.values[@]
	call main.lb_hypoBuild.repopulate
	call main.lb_hypoBuild.setvalue class main_hypotmp2.value
END

PROGRAM main_lb_hypoBuild_output
BEGIN
	call mainHYPOCommandValues.copyToString main_hypo_cmd_output
	call main_hypo_arraysize.setvalue class mainHYPOStatusList.size
	if main_hypo_cmd_output.iseq("") &				///
		main_hypo_arraysize.iseq(0) {
		stopbox stop "Hypotheses not defined. "			///
"Please enter a hypotheses before proceeding."
	}
	if main_hypo_cmd_output.iseq("") & main_hypo_arraysize.isgt(0) ///
		& main.lb_hypoBuild.isvisible() {
		stopbox stop "All hypotheses are disabled. "		///
"Either define a new hypotheses or enable an existing hypotheses "	///
"before proceeding."
	}

	if main_hypo_cmd_output & main.lb_hypoBuild.isvisible() {
		put main_hypo_cmd_output
	}
END


SCRIPT iter_on
BEGIN
	main.sp_iter.enable
END

SCRIPT iter_off
BEGIN
	main.sp_iter.disable
END

SCRIPT mtest_on
BEGIN
	main.tx_mtest2.enable
	main.rb_noad.enable
	main.rb_bonf.enable
	main.rb_holm.enable
	main.rb_sidak.enable
END

SCRIPT mtest_off
BEGIN
	main.tx_mtest2.disable
	main.rb_noad.disable
	main.rb_bonf.disable
	main.rb_holm.disable
	main.rb_sidak.disable
END

PROGRAM command
BEGIN
	put "testnl "
	put " " /program main_lb_hypoBuild_output
	beginoptions
		if main.ck_mtest {
			option radio(main rb_noad rb_bonf rb_holm rb_sidak)
		}
		option main.ck_nsvy
		if main.ck_iter{
			optionarg main.sp_iter
		}
	endoptions
END
