/*
  order

*!  VERSION 1.1.9  07jun2011

*/

VERSION 11.0

INCLUDE _std_medium
DEFINE _dlght 300
INCLUDE header

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

SCRIPT POSTINIT_PROGRAM
BEGIN
	if __MESSAGE {
		call main.vl_vars.setvalue class __MESSAGE.value
	}
END

DIALOG main, label("order - Reorder variables in dataset")		///
	tabtitle("Main")
BEGIN
  TEXT tx_vars		_lft	_top	_iwd	.,			///
	label("Variables to reorder:")
  VARLIST vl_vars	@	_ss	_iwd	.,			///
	label("Variables to reorder")
  GROUPBOX gb_action	@	_ls	_iwd	_ht14,			///
	label("New position")
  RADIO rb_first	_ilft	_mls	_ibwd	., first		///
	onclickon(program alpha_sequential_ck)				///
	onclickoff(program alpha_sequential_ck)				///
	option(first) 							///
	label("Place variables to reorder at the beginning of the dataset")
  RADIO rb_last		@	_ss	@	.,			///
	onclickon(program alpha_sequential_ck)				///
	onclickoff(program alpha_sequential_ck)				///
	option(last) nomemory						///
	label("Place variables to reorder at the end of the dataset")
  RADIO rb_before	_ilft	_ss	_ibwd	.,			///
	onclickon(program main_before_ck)				///
	onclickoff(program main_before_ck)				///
	label("Place variables to reorder before specified variable")
  VARNAME vn_before	_indent2 _ss	_vnwd	.,			///
	option(before)							///
	label("Place variables to reorder before specified variable")
  RADIO rb_after	_ilft	_mls	_ibwd	., last			///
	onclickon(program main_after_ck)				///
	onclickoff(program main_after_ck)				///
	label("Place variables to reorder after specified variable")
  VARNAME vn_after	_indent2 _ss	_vnwd	.,			///
	option(after)							///
	label("Place variables to reorder after specified variable")
  CHECKBOX ck_alpha	_lft	+45	_ibwd	.,			///
	onclickon(program alpha_sequential_ck)				///
	onclickoff(program alpha_sequential_ck)				///
	option(alphabetic)						///
	label("Sort variables alphabetically")
  CHECKBOX ck_sequential @	_ms	@	.,			///
	onclickon(program alpha_sequential_ck)				///
	onclickoff(program alpha_sequential_ck)				///
	option(sequential)						///
	label("Alphabetize varlist keeping numbers sequential")
END

PROGRAM main_before_ck
BEGIN
	call program alpha_sequential_ck
	if main.rb_before {
		call main.vn_before.enable
	}
	else {
		call main.vn_before.disable
	}
END

PROGRAM main_after_ck
BEGIN
	call program alpha_sequential_ck
	if main.rb_after {
		call main.vn_after.enable
	}
	else {
		call main.vn_after.disable
	}
END

PROGRAM alpha_sequential_ck
BEGIN
	if (!main.ck_alpha | !main.ck_sequential) {
		call main.tx_vars.setlabel "Variables to reorder:"
	}
	if (main.ck_alpha) {
		call main.tx_vars.setlabel "Variables to reorder:  (leave empty for all variables)"
		call main.ck_sequential.disable	
	}
	if (!main.ck_alpha) {
		call main.ck_sequential.enable
	}
	if (main.ck_sequential) {
		call main.tx_vars.setlabel "Variables to reorder:  (leave empty for all variables)"
		call main.ck_alpha.disable
	}
	if (!main.ck_sequential) {
		call main.ck_alpha.enable
	}
END

PROGRAM command
BEGIN
	put "order "
	if (main.ck_alpha | main.ck_sequential) {
		varlist [main.vl_vars]
		if (main.vl_vars.iseq("")) {
			put "_all"	
		}
	}
	else {
		varlist main.vl_vars
	}

	beginoptions
		if main.rb_before {
			require main.vl_vars
			require main.vn_before
			optionarg main.vn_before
		}
		if main.rb_after {
			require main.vl_vars
			require main.vn_after
			optionarg main.vn_after
		}
		option main.rb_first
		option main.rb_last
		option main.ck_sequential
		option main.ck_alpha
	endoptions
END

