/*
	as_probit_model.idlg

	This is an INCLUDE file for the -model- and -model2- tabs that go 
	with -asmprobit- and -asroprobit- dialog.

*!  VERSION 1.1.2  15may2011

	// SCRIPTS and PROGRAMS for external use
	    * PROGRAM as_probit_model_vars_output
	    * PROGRAM as_probit_model_opts_output
	    * SCRIPT as_probit_set_cmd_asmprobit
*/

SCRIPT 	as_probit_set_cmd_asmprobit	
BEGIN
	model2.ck_reverse.hide
END

SCRIPT main_PREINIT
BEGIN
	script _constraints_main_setOptionalOn
END

DIALOG main, tabtitle("Model")						
BEGIN
  INCLUDE _dviv

  TEXT     tx_case		_lft	_ls	_vnwd	.,		///
	label("Case variable:")
  DEFINE holdy @y
  VARNAME  vn_case		@	_ss	@	.,		///
	label("Case variable") option(case)
  TEXT     tx_casevars		_vlx	holdy	_vlwd	.,		///
	label("Case-specific variables: (optional)")
  VARLIST  vl_casevars		@	_ss	_vlwd	.,		///
	label("Case-specific variables") option(casevars) 		///
	allowcat
  TEXT     tx_alternatives	_lft	_ls	_iwd	.,		///
	label("Alternatives variable:")
  VARNAME  vn_alternatives	@	_ss	_vnwd	.,		///
	label("Alternatives variable")					///
	option(alternatives)

  DEFINE _x _lft
  DEFINE _y _ls
  DEFINE _cx _lw80
  DEFINE _bux _slw80
  INCLUDE _constraints
  
  DEFINE _x _lft
  DEFINE _xw _iwd
  INCLUDE _ck_collinear
END

INCLUDE _constraints_sc 

SCRIPT model2_PREINIT
BEGIN
	create STRING matrixResult
END

DIALOG model2, tabtitle("Model 2")
BEGIN
  BUTTON   bu_matinput   	_rj100	_top    100	.,		///
	label("Input matrix...") onpush(program show_matrix_input)	///
	tooltip("Input matrix by hand")

  GROUPBOX gb_corr		_lft	_ss	_iwd	_ht8,		///
	label("Correlation structure of the latent-variable errors")
  RADIO	   rb_unstructured	_indent	_ss	_cwd3_1	.,		///
	label("Unstructured (default)") first option(NONE)		///
	onclickon(program check_notransform)
  DEFINE holdy @y
  RADIO    rb_exchangeable	@	_ss	@	.,		///
	label("Exchangeable")						///
	option(correlation(exchangeable))				///
	onclickon(program check_notransform)
  RADIO    rb_independent	@	_ss	@	.,		///
	label("Independent")						///
	option(correlation(independent))				///
	onclickon(program check_notransform)
  RADIO    rb_cpattern	    _lft3_02	holdy  _comb3_1	.,		///
	label("Specify matrix for correlation pattern")			///
	onclickon(script cpattern_on)					///
	onclickoff(script cpattern_off)					///
	option(NONE)
  COMBOBOX cb_cpattern		220	_ss	_vnwd	.,		///
  	label("Matrix") dropdown contents(matrix)
  TEXT     tx_cpattern		_vnsep	@	150	.,		///
	label("Matrix")
  RADIO	   rb_cfixed	    _lft3_02	_ss    _comb3_1	.,		///
	onclickon(script cfixed_on)					///
	onclickoff(script cfixed_off)					///
	last option(NONE)						///
	label("Specify matrix for fixed and free correlation parameters")
  COMBOBOX cb_cfixed		220	_ss	_vnwd	.,		///
  	label("Matrix") dropdown contents(matrix)
  TEXT     tx_cfixed		_vnsep	@	150	.,		///
	label("Matrix")

  GROUPBOX gb_stddev		_lft	_xls	_iwd	_ht8,		///
	label("Variance structure of the latent-variable errors")

  RADIO	   rb_heteroske		_indent	_ss	_cwd3_1	.,		///
	label("Heteroskedastic (default)") first option(NONE)		///
	onclickon(program check_notransform)
  DEFINE holdy @y
  RADIO    rb_homoske		@	_ss	@	.,		///
	label("Homoskedastic") option(stddev(homoskedastic))		///
	onclickon(program check_notransform)
  RADIO    rb_vpattern	     _lft3_02	holdy  _comb3_1	.,		///
	label("Specify matrix for standard deviation pattern")		///
	onclickon(script vpattern_on)					///
	onclickoff(script vpattern_off)					///
	option(NONE)
  COMBOBOX cb_vpattern		220	_ss	_vnwd	.,		///
  	label("Matrix") dropdown contents(matrix)
  TEXT     tx_vpattern		_vnsep	@	150	.,		///
	label("Matrix")
  RADIO	   rb_vfixed	     _lft3_02	_ss    _comb3_1	.,		///
	onclickon(script vfixed_on)					///
	onclickoff(script vfixed_off)					///
	last option(NONE)						///
	label("Specify matrix for fixed and free standard deviation parameters")
  COMBOBOX cb_vfixed		220	_ss	_vnwd	.,		///
  	label("Matrix") dropdown contents(matrix)
  TEXT     tx_vfixed		_vnsep	@	150	.,		///
	label("Matrix")

  CHECKBOX ck_factor		_lft	_xls	_cwd1	.,		///
	onclickon(program factor_on)					///
	onclickoff(program factor_off)					///
	label("Use factor covariance structure")
  SPINNER  sp_factor   		_lft2   @ 	_spwd	.,		///
	label("Dimension")						///
	min(1) max(20) default(1)					///
	option(factor)
  TEXT     tx_factor  	 	_spsep	@	_spr2b	.,		///
	label("Factor dimension")

  CHECKBOX ck_structural	_lft	_ms	_cwd1	.,		///
	onclickon(program structural_on)				///
	onclickoff(program structural_off)				///
	option(structural) label("Use structural covariance parameterization")
  CHECKBOX ck_noconstant	_lft2	@	_cwd1	.,		///
	option(noconstant) label("Suppress alternative-specific constant terms")
  CHECKBOX ck_basealt		_lft	_ms	_cwd1	.,		///
	onclickon(script basealt_on)					///
	onclickoff(script basealt_off)					///
	label("Specify alternative to normalize location")
  DEFINE holdy @y
  EDIT     en_basealt	       _indent2	_ss	_en14wd	.,		///
  	label("Base alternative")
  TEXT     tx_basealt		_en14sep @	120	.,		///
	label("Base alternative")

  CHECKBOX ck_scalealt		_lft2	holdy	_cwd1	.,		///
	onclickon(script scalealt_on)					///
	onclickoff(script scalealt_off)					///
	label("Specify alternative to normalize scale")
  EDIT     en_scalealt	       _indent2	_ss	_en14wd	.,		///
  	label("Scale alternative")
  TEXT     tx_scalealt		_en14sep @	120	.,		///
	label("Scale alternative")
  CHECKBOX ck_altwise		_lft	_ms	_cwd1	.,		///
	option(altwise) label("Use alternativewise deletion")
  CHECKBOX ck_reverse		_lft2	@	_cwd1	.,		///
	option(reverse) label("Interpret lowest rank as the best")
END

PROGRAM show_matrix_input
BEGIN
	call create CHILD matrix_input
	if ! _rc {
		call matrix_input.setExitString matrixResult
		call matrix_input.setExitAction "program getMatrixResult"
		call matrix_input.callthru "isChildContext.settrue"
	}
END
PROGRAM getMatrixResult
BEGIN
	call create ARRAY matrix_tempvals
	call matrix_tempvals.Arrdropall
	
	call model2.cb_cpattern.withvalue matrix_tempvals.Arrpush "@"
	call model2.cb_cfixed.withvalue matrix_tempvals.Arrpush "@"
	call model2.cb_vpattern.withvalue matrix_tempvals.Arrpush "@"
	call model2.cb_vfixed.withvalue matrix_tempvals.Arrpush "@"

	call model2.cb_cpattern.repopulate
	call model2.cb_cfixed.repopulate
	call model2.cb_vpattern.repopulate
	call model2.cb_vfixed.repopulate

	call model2.cb_cpattern.setvalue class matrix_tempvals[1]
	call model2.cb_cfixed.setvalue class matrix_tempvals[2]
	call model2.cb_vpattern.setvalue class matrix_tempvals[3]
	call model2.cb_vfixed.setvalue class matrix_tempvals[4]
END

PROGRAM check_notransform
BEGIN
	if ((!model2.rb_unstructured.isenabled() | model2.rb_unstructured) ///
	    | (!model2.rb_heteroske.isenabled() | model2.rb_heteroske))    ///
	{
		call model2.ck_structural.enable
		if model2.ck_structural {
			call rpt.ck_notransform.enable
			call rpt.tx_notransform.enable
		}
	}
	else {
		call model2.ck_structural.disable
		call rpt.ck_notransform.disable
		call rpt.tx_notransform.disable
	}
END

PROGRAM factor_on
BEGIN
	call model2.gb_corr.disable
	call model2.rb_unstructured.disable
	call model2.rb_exchangeable.disable
	call model2.rb_independent.disable
	call model2.rb_cpattern.disable
	call model2.cb_cpattern.disable
	call model2.tx_cpattern.disable
	call model2.rb_cfixed.disable
	call model2.cb_cfixed.disable
	call model2.tx_cfixed.disable
	
	call model2.gb_stddev.disable
	call model2.rb_heteroske.disable
	call model2.rb_homoske.disable
	call model2.rb_vpattern.disable
	call model2.cb_vpattern.disable
	call model2.tx_vpattern.disable
	call model2.rb_vfixed.disable
	call model2.cb_vfixed.disable
	call model2.tx_vfixed.disable

	call model2.bu_matinput.disable	

	call model2.sp_factor.enable
	call model2.tx_factor.enable
	
	call program check_notransform
END


PROGRAM factor_off
BEGIN
	call model2.gb_corr.enable
	call model2.rb_unstructured.enable
	call model2.rb_exchangeable.enable
	call model2.rb_independent.enable
	call model2.rb_cpattern.enable
	if (model2.rb_cpattern) {
		call model2.cb_cpattern.enable
		call model2.tx_cpattern.enable
	}
	call model2.rb_cfixed.enable
	if (model2.rb_cfixed) {
		call model2.cb_cfixed.enable
		call model2.tx_cfixed.enable
	}
	
	call model2.gb_stddev.enable
	call model2.rb_heteroske.enable
	call model2.rb_homoske.enable
	call model2.rb_vpattern.enable
	if (model2.rb_vpattern) {
		call model2.cb_vpattern.enable
		call model2.tx_vpattern.enable
	}
	call model2.rb_vfixed.enable
	if (model2.rb_vfixed) {
		call model2.cb_vfixed.enable
		call model2.tx_vfixed.enable
	}

	call model2.bu_matinput.enable	

	call model2.sp_factor.disable
	call model2.tx_factor.disable
	
	call program check_notransform
END

PROGRAM structural_on
BEGIN
	call rpt.ck_notransform.enable
	call rpt.tx_notransform.enable
END

PROGRAM structural_off
BEGIN
	call rpt.ck_notransform.disable
	call rpt.tx_notransform.disable
END

SCRIPT basealt_on
BEGIN
	model2.en_basealt.enable
	model2.tx_basealt.enable
END

SCRIPT basealt_off
BEGIN
	model2.en_basealt.disable
	model2.tx_basealt.disable
END

SCRIPT scalealt_on
BEGIN
	model2.en_scalealt.enable
	model2.tx_scalealt.enable
END

SCRIPT scalealt_off
BEGIN
	model2.en_scalealt.disable
	model2.tx_scalealt.disable
END

SCRIPT cpattern_on
BEGIN
	model2.cb_cpattern.enable
	model2.tx_cpattern.enable
	program check_notransform
END

SCRIPT cpattern_off
BEGIN
	model2.cb_cpattern.disable
	model2.tx_cpattern.disable
	program check_notransform
END

SCRIPT cfixed_on
BEGIN
	model2.cb_cfixed.enable
	model2.tx_cfixed.enable
	program check_notransform
END

SCRIPT cfixed_off
BEGIN
	model2.cb_cfixed.disable
	model2.tx_cfixed.disable
	program check_notransform
END

SCRIPT vpattern_on
BEGIN
	model2.cb_vpattern.enable
	model2.tx_vpattern.enable
	program check_notransform
END

SCRIPT vpattern_off
BEGIN
	model2.cb_vpattern.disable
	model2.tx_vpattern.disable
	program check_notransform
END

SCRIPT vfixed_on
BEGIN
	model2.cb_vfixed.enable
	model2.tx_vfixed.enable
	program check_notransform
END

SCRIPT vfixed_off
BEGIN
	model2.cb_vfixed.disable
	model2.tx_vfixed.disable
	program check_notransform
END

PROGRAM as_probit_model_vars_output
BEGIN
	varlist main.vn_dv [main.vl_iv]
END

PROGRAM as_probit_model_opts_output
BEGIN
	require main.vn_case
	optionarg main.vn_case
	require main.vn_alternatives
	optionarg main.vn_alternatives
	optionarg main.vl_casevars
	INCLUDE _constraints_main_pr
	option main.ck_collinear

	option model2.rb_exchangeable
	option model2.rb_independent
	if model2.rb_cpattern.isenabled() & model2.rb_cpattern {
		require model2.cb_cpattern
		put "correlation(pattern "
		put model2.cb_cpattern
		put ") "
	}
	if model2.rb_cfixed.isenabled() & model2.rb_cfixed {
		require model2.cb_cfixed
		put "correlation(fixed "
		put model2.cb_cfixed
		put ") "
	}
	option model2.rb_homoske
	if model2.rb_vpattern.isenabled() & model2.rb_vpattern {
		require model2.cb_vpattern
		put "stddev(pattern "
		put model2.cb_vpattern
		put ") "
	}
	if model2.rb_vfixed.isenabled() & model2.rb_vfixed {
		require model2.cb_vfixed
		put "stddev(fixed "
		put model2.cb_vfixed
		put ") "
	}
	option model2.ck_structural
	option model2.ck_noconstant
	if model2.ck_basealt.isenabled() & model2.ck_basealt {
		require model2.en_basealt
		put "base("
		put model2.en_basealt
		put ") "
	}
	if model2.ck_scalealt.isenabled() & model2.ck_scalealt {
		require model2.en_scalealt
		put "scale("
		put model2.en_scalealt
		put ") "
	}
	option model2.ck_altwise
	option model2.ck_reverse
	optionarg model2.sp_factor
END
