/*
  pac

*!  VERSION 2.0.3  15jun2011

*/

VERSION 12.0

INCLUDE _std_xlarge
DEFINE _dlght 300
INCLUDE header
HELP hlp1, view("help pac")
RESET res1

SCRIPT POSTINIT
BEGIN
	script gr_plots_setDefaultDropline
	script gr_ciplots_setDefaultArea
END

DIALOG main, tabtitle("Main")					///
	label("pac - Graph partial autocorrelations with confidence intervals")
BEGIN
  DEFINE _x _xsetbu
  DEFINE _y _top
  INCLUDE _bu_tsset
  TEXT     tx_var	_lft	_top	_cwd2	.,		///
	label("Variable:")
  VARNAME  vn_var	@	_ss	_vnwd	.,		///
	label("Variable")

  GROUPBOX gb_opt	@	+35	_iwd	_ht17h,		///
	label("Options")
  RADIO    rb_lags1	_indent	_ss	_inwd	.,		///
	onclickon(gaction main.sp_lags2.disable)		///
	first							///
	label("Use default number of autocorrelations -- min([n/2]-2,40)")
  RADIO    rb_lags2	@	_ss	@	.,		///
	onclickon(gaction main.sp_lags2.enable)			///
	last							///
	label("Use specified number of autocorrelations")
  DEFINE _x @x
  SPINNER  sp_lags2	_indent2 _ss	_spwd	.,		///
	min(1) max(100) default(1)				///
	option(lags)

  CHECKBOX ck_gen	_ilft	_ls	_ibwd	.,		///
	onclickon(script graph_off)				///
	onclickoff(program graph_on)				///
	label("Generate a variable to hold partial autocorrelations: (suppresses the graph)")
  EDIT     ed_gen	_indent2 _ss	_vnwd	.,		///
	max(32)							///
	option(nograph generate)				///
	label("Variable to hold estimated partial autocorrelation")
  TEXT     tx_gen	_vnsep	@	200	.,		///
	label("New variable name")

  CHECKBOX ck_yw	_ilft	_ls	_ibwd	.,		///
	option(yw)						///
	onclickon(script main_srv_disable)			///
	onclickoff(program main_srv_enable)			///
	label("Calculate partial autocorrelations using Yule-Walker equations (disables SRV)")

  DEFINE _x _ilft
  DEFINE _y _ls
  DEFINE _cx _sprb
  INCLUDE _sp_level
END

SCRIPT main_srv_disable
BEGIN
	srv.ck_srv.disable
	srv.bu_srv.disable
END

PROGRAM main_srv_enable
BEGIN
	call srv.ck_srv.enable
	if srv.ck_srv {
		call srv.bu_srv.enable
	}
	else {
		call srv.bu_srv.disable
	}
END
SCRIPT graph_off
BEGIN
	main.ed_gen.enable
	main.tx_gen.enable

	script main_srv_disable

	script gr_plots_disable
	script gr_ciplots_disable
	script gr_addplots_disable
	script gr_yaxis_disable
	script gr_xaxis_disable
	script gr_titles_disable
	script gr_legend_disable
	script gr_overall_disable
END

PROGRAM graph_on
BEGIN
	call main.ed_gen.disable
	call main.tx_gen.disable
	
	if ! main.ck_yw {
		call program main_srv_enable
	}

	call script gr_plots_enable
	call script gr_ciplots_enable
	call script gr_addplots_enable
	call script gr_yaxis_enable
	call script gr_xaxis_enable
	call script gr_titles_enable
	call script gr_legend_enable
	call script gr_overall_enable
END

INCLUDE ifin

INCLUDE gr_plots
INCLUDE gr_ciplots

SCRIPT srv_PREINIT
BEGIN
	create STRING srvResults
END

SCRIPT srv_POSTINIT
BEGIN
	program checkSrvResults
END

DIALOG srv, tabtitle("SRV plot")
BEGIN
  CHECKBOX ck_srv	_lft	_top	_iwd	.,		///
	option(srv)						///
	onclickon(srv.bu_srv.enable)				///
	onclickoff(srv.bu_srv.disable)				///
	label("Include standardized residual variances in graph (SRV)")
  BUTTON   bu_srv	+20	_ms	130	.,		///
	label("SRV plot properties  ")				///
	onpush(script show_srv_dialog)
END

SCRIPT show_srv_dialog
BEGIN
	create CHILD gr_plots AS srvOpts, allowsubmit
	srvOpts.setExitString srvResults
	srvOpts.setOkAction "program checkSrvResults"
	srvOpts.setSubmitAction "script srvSubmit"
	srvOpts.settitle "SRV plot properties"
END
PROGRAM checkSrvResults
BEGIN
	if srvResults {
		call srv.bu_srv.setlabel "SRV plot properties *"
	}
	else {
		call srv.bu_srv.setlabel "SRV plot properties  "
	}		
END
SCRIPT srvSubmit
BEGIN
	program checkSrvResults
	Submit
END

INCLUDE gr_addplots
INCLUDE gr_yaxis
INCLUDE gr_xaxis
INCLUDE gr_titles
INCLUDE gr_legend
INCLUDE gr_overall

PROGRAM command
BEGIN
	put "pac "
	varlist main.vn_var
	INCLUDE _ifin_pr
	beginoptions
		if main.rb_lags2 {
			optionarg main.sp_lags2
		}
		if main.ck_gen {
			require main.ed_gen
			optionarg main.ed_gen
		}
		INCLUDE _level_main_pr
		option main.ck_yw

		put " " /program gr_plots_output
		put " " /program gr_ciplots_output
		
		option srv.ck_srv
		if srvResults & !D(srv.bu_srv) {
			put " srvopts(" srvResults ") "
		}
		
		put " " /program gr_addplots_output
		put " " /program gr_yaxis_output
		put " " /program gr_xaxis_output
		put " " /program gr_titles_output
		put " " /program gr_legend_output
		put " " /program gr_overall_output
	endoptions
END
