//////////////////////////////////////////////////////////////////////
// Filename:	ANOVA2Way.OGS
// Purpose:		Event handlers for ANOVA2Way dialog box
// Creation:	by GJL, 4/20/01
//////////////////////////////////////////////////////////////////////

[OnOpen]
	dialog.start(OStat.DLL,ANOVA2Way,ANOVA2Way.OPS);
	return 0;

[OnInit]
	define DocEnd {run.section(Origin.OGS,DocEnd);};
	ANOVA2Way!SignificanceLevelEBX.v1$=$(5/100,.2);
	ANOVA2Way!InteractionsCHKBX=0;
	ANOVA2Way!SpecifyLevelsRB=1;
	%A="ANOVA2Way!LevelsACMBX";
	%Z=$ANOVA2Way.FactorALevels;
	run.section(DBRoutines,PopCMBXwZStringTokens,"%A");
	ANOVA2Way!LevelsACMBX.Choice=1;
	%A="ANOVA2Way!LevelsBCMBX";
	%Z=$ANOVA2Way.FactorBLevels;
	run.section(DBRoutines,PopCMBXwZStringTokens,"%A");
	ANOVA2Way!LevelsBCMBX.Choice=1;
	ANOVA2Way!AvailableDataLBX.multSel=2;
	%A="ANOVA2Way!AvailableDataLBX";
	run.section(DBRoutines,PopLBXwAllData,"%A");
	ANOVA2Way!SelectedDataLCNTRL.multSel=2;
	run.section(,InitLCNTRLColumns);
	SelectAll=0;
	ANOVA2Way!SelectedDataLCNTRL.CopySel(ANOVA2Way!AvailableDataLBX);
	run.section(,SetNLevelsEBX);
	ANOVA2Way!PowerAnalysisEBX.v1$=ANOVA2Way!SignificanceLevelEBX.v1$;
	ANOVA2Way!SampleSizeEBX.v1$=50,100,200;
	ANOVA2Way!SampleSizeEBX.Enable=ANOVA2Way!PowerAnalysisCHKBX & ANOVA2Way!SampleSizeCHKBX;
	return 0;

[InitLCNTRLColumns]
	ANOVA2Way!SelectedDataLCNTRL.reset(1);
	ANOVA2Way!SelectedDataLCNTRL.addCol(Selected Data,0);
	ANOVA2Way!SelectedDataLCNTRL.colWidth1=$ANOVA2Way.SelectedDataColWidth;
	run.section(,ReInitLCNTRLColumns);
	return 0;
	
[ReInitLCNTRLColumns]
	run.section(,DeleteLCNTRLColumns);
	if(ANOVA2Way!SpecifyLevelsRB==1)
	{
		run.section(,AddLevelsColumns);
		run.section(,SetNLevelsEBX);
	}
	else
	{
		run.section(,AddVariableTypeColumn);
		Dii=3;
		run.section(,InitVariableTypeColumn);
	}
	return 0;
	
[DeleteLCNTRLColumns]
	jj=ANOVA2Way!SelectedDataLCNTRL.numCols;
	for(ii=jj;ii>1;ii--)
	{
		ANOVA2Way!SelectedDataLCNTRL.delCol(ii);
	}
	return 0;
	
[AddLevelsColumns]	
	ANOVA2Way!SelectedDataLCNTRL.addCol(Factor A,0);
	ANOVA2Way!SelectedDataLCNTRL.addCol(Factor B,0);
	ANOVA2Way!SelectedDataLCNTRL.colWidth1=$ANOVA2Way.SelectedDataColWidth;
	ANOVA2Way!SelectedDataLCNTRL.colWidth2=$ANOVA2Way.FactorLevelsColWidth;
	ANOVA2Way!SelectedDataLCNTRL.colWidth3=$ANOVA2Way.FactorLevelsColWidth;
	ANOVA2Way!SelectedDataBTN$=$ANOVA2Way.SelectedDataNGT2;
	return 0;	
	
[AddVariableTypeColumn]
	ANOVA2Way!SelectedDataLCNTRL.addCol(Variable Type,0);
	ANOVA2Way!SelectedDataLCNTRL.colWidth1=$ANOVA2Way.SelectedDataColWidth;
	ANOVA2Way!SelectedDataLCNTRL.colWidth2=$ANOVA2Way.VariableTypesColWidth;
	ANOVA2Way!SelectedDataBTN$=$ANOVA2Way.SelectedDataNEQ3;	
	return 0;	

[InitVariableTypeColumn]
	run.section(,SetNLevelsEBX);
	if(jj==3)
	{
		Dii=3;
		run.section(,VariableTypeBTN.OnClick);
	}
	else
	{
		loop(ii,1,jj)
		{
			ANOVA2Way!SelectedDataLCNTRL.V$(ii)_2$="";
		}
	}
	return 0;

[SetNLevelsEBX]
	jj=ANOVA2Way!SelectedDataLCNTRL.numItems;
	ANOVA2Way!NLevelsEBX$=$(jj);
	return 0;

[SpecifyLevelsRB.OnClick]
	run.section(,ReInitLCNTRLColumns);
	jj=ANOVA2Way!SelectedDataLCNTRL.numItems; // reselect all selected items for display purposes
	loop(ii,1,jj)
	{
		if(ANOVA2Way!SelectedDataLCNTRL.A$(ii)==1)
		{
			ANOVA2Way!SelectedDataLCNTRL.A$(ii)=0;
			ANOVA2Way!SelectedDataLCNTRL.A$(ii)=1;
		}
	}
	return 0;

[ToolBar.AddDataBTN.OnClick]
	// Add selected items in Available Data list box to Selected Data list control
	jj=ANOVA2Way!AvailableDataLBX.numSels;
	loop(ii,1,jj)
	{
		%A=ANOVA2Way!AvailableDataLBX.S$(ii)$;
		if(run.section(,CheckIfInSelectedDataLCNTRL,"%A")==0)
		{ 
			ANOVA2Way!SelectedDataLCNTRL.add(%A);
		}
	}
	// Initialize levels columns in list control
	if(ANOVA2Way!SpecifyLevelsRB==1)
		run.section(,SetNLevelsEBX);
	else
		run.section(,InitVariableTypeColumn);
	return 0;
	
[CheckIfInSelectedDataLCNTRL]
	%A=%1;
	Tjj=ANOVA2Way!SelectedDataLCNTRL.numItems;
	loop(Tii,1,Tjj)
	{
		%B=ANOVA2Way!SelectedDataLCNTRL.V$(Tii)_1$;
		if("%A"=="%B")
		{
			ANOVA2Way!SelectedDataLCNTRL.A$(Tii)=1;
			type -b;
			delete -v Tii;
			delete -v Tjj;
			return 1;
		}
	}
	delete -v Tii;
	delete -v Tjj;
	return 0;
	
[ToolBar.RemoveDataBTN.OnClick]
	ANOVA2Way!SelectedDataLCNTRL.Del();
	if(ANOVA2Way!SpecifyLevelsRB==1)
		run.section(,SetNLevelsEBX);
	else
		run.section(,InitVariableTypeColumn);
	return 0;

[SelectedDataBTN.OnClick]
	jj=ANOVA2Way!SelectedDataLCNTRL.numItems;
	if(ANOVA2Way!SelectedDataLCNTRL.numSels==0)
		SelectAll=1;
	else
	{
		if(ANOVA2Way!SelectedDataLCNTRL.numSels==jj)
			SelectAll=0;
		else
			SelectAll=mod(SelectAll+1,2);
	}
	loop(ii,1,jj)
	{
		ANOVA2Way!SelectedDataLCNTRL.A$(ii)=SelectAll;
	}
	return 0;

[VariableTypeBTN.OnClick]
	jj=ANOVA2Way!SelectedDataLCNTRL.numItems;
	if(jj==3)
	{
		if(run.section(DBRoutines.OGS,LoadOStatWorkspace)!=0)
			return 1;
		run.section(DBRoutines.OGS,SaveAndSetAtOC);
		Dii=mod(Dii,3)+1;
		loop(ii,1,3)
		{
			ANOVA2Way!SelectedDataLCNTRL.V$(ii)_2$=osANOVA2Way_Get_Localized_Var_Name(mod(ii+(3-Dii),3)+1)$;
		}
		run.section(DBRoutines.OGS,RestoreAtOC);
	}
	return 0;

[VariableTypeSwitchBTN.OnClick]
	jj=ANOVA2Way!SelectedDataLCNTRL.numItems;
	if(jj==3)
	{
		switch(Dii)
		{
			case 1:
				%A=ANOVA2Way!SelectedDataLCNTRL.V2_2$;
				ANOVA2Way!SelectedDataLCNTRL.V2_2$=ANOVA2Way!SelectedDataLCNTRL.V3_2$;
				ANOVA2Way!SelectedDataLCNTRL.V3_2$=%A;
				break;
			case 2:
				%A=ANOVA2Way!SelectedDataLCNTRL.V1_2$;
				ANOVA2Way!SelectedDataLCNTRL.V1_2$=ANOVA2Way!SelectedDataLCNTRL.V3_2$;
				ANOVA2Way!SelectedDataLCNTRL.V3_2$=%A;
				break;
			case 3:
				%A=ANOVA2Way!SelectedDataLCNTRL.V1_2$;
				ANOVA2Way!SelectedDataLCNTRL.V1_2$=ANOVA2Way!SelectedDataLCNTRL.V2_2$;
				ANOVA2Way!SelectedDataLCNTRL.V2_2$=%A;
				break;
		}
	}
	return 0;

[LevelsACMBX.OnEditChange]
	run.section(,UpdateLCNTRLColumns,"LevelsACMBX" 2);
	return 0;
	
[LevelsACMBX.OnSelChange]
	run.section(,UpdateLCNTRLColumns,"LevelsACMBX" 2);
	return 0;

[FactorABTN.OnClick]
	%A="ANOVA2Way!LevelsACMBX";
	%B=ANOVA2Way!LevelsACMBX$;
	ANOVA2Way!LevelsACMBX.Choice=run.section(DBRoutines.OGS,CheckIfItemIsInList,"%A" "%B" 1 0);
	run.section(,UpdateLCNTRLColumns,"LevelsACMBX" 2);
	return 0;

[LevelsBCMBX.OnEditChange]
	run.section(,UpdateLCNTRLColumns,"LevelsBCMBX" 3);
	return 0;
	
[LevelsBCMBX.OnSelChange]
	run.section(,UpdateLCNTRLColumns,"LevelsBCMBX" 3);
	return 0;

[FactorBBTN.OnClick]
	%A="ANOVA2Way!LevelsBCMBX";
	%B=ANOVA2Way!LevelsBCMBX$;
	ANOVA2Way!LevelsBCMBX.Choice=run.section(DBRoutines.OGS,CheckIfItemIsInList,"%A" "%B" 1 0);
	run.section(,UpdateLCNTRLColumns,"LevelsBCMBX" 3);
	return 0;

[UpdateLCNTRLColumns]
	%A=ANOVA2Way!%1$;
	jj=ANOVA2Way!SelectedDataLCNTRL.numSels;
	loop(ii,1,jj)
	{
		ANOVA2Way!SelectedDataLCNTRL.S$(ii)_%2$=%A;
	}
	return 0;

[SignificanceLevelEBX.OnEditChange]
	ANOVA2Way!PowerAnalysisEBX.v1$=ANOVA2Way!SignificanceLevelEBX.v1$;
	return 0;

[PowerAnalysisCHKBX.OnClick]
	ANOVA2Way!SampleSizeEBX.Enable=ANOVA2Way!PowerAnalysisCHKBX &
								   ANOVA2Way!SampleSizeCHKBX;
	return 0;

[SampleSizeCHKBX.OnClick]
	ANOVA2Way!SampleSizeEBX.Enable=ANOVA2Way!SampleSizeCHKBX;
	return 0;
			
[ComputeBTN.OnClick]
	run.section(DBRoutines.OGS,SaveAndSetAtOC);
	if(run.section(,CheckSettings)==0)
	{
		run.section(,OpenAndDirectOutputToResultsLog);
		run.section(,ANOVA2Way);
		run.section(,CloseAndRedirectOutput);
	}
	run.section(DBRoutines.OGS,RestoreAtOC);
	return 0;
	
[CheckSettings]
	nData=ANOVA2Way!SelectedDataLCNTRL.numItems;
	if(ANOVA2Way!SpecifyLevelsRB==1) // Specify levels by Datasets
	{
		if(nData<3)
		{
			run.section(DBRoutines.OGS,ReportError,$ANOVA2Way.NoDataFail1);
			return 1;
		}
		loop(ii,1,nData)
		{
			%A=ANOVA2Way!SelectedDataLCNTRL.V$(ii)_1$;
			%B=$OStatCommon.SelectedDatasetNoExist;
			if(run.section(DBRoutines.OGS,CheckDatasetExists,"%B" "%A")!=0)
				return 1;
			/// GJL 4/11/02 QA70-1404 UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
			%B=%[%A,'_'];
			if(%B!page.extInfo==1) // If data set is in Excel workbook update Origin
			{
				win -o %B
				{
					layer -s;
					set %B -ui;
				}
			}
			/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
			%B=ANOVA2Way!SelectedDataLCNTRL.V$(ii)_2$;
			if(%[%B]==0)
			{
				run.section(DBRoutines.OGS,ReportError,$ANOVA2Way.UnspecifiedLevel);
				return 1;
			}
			%B=ANOVA2Way!SelectedDataLCNTRL.V$(ii)_3$;
			if(%[%B]==0)
			{
				run.section(DBRoutines.OGS,ReportError,$ANOVA2Way.UnspecifiedLevel);
				return 1;
			}
		}
	}
	else // Specify levels by classification variables
	{
		if(nData!=3)
		{
			run.section(DBRoutines.OGS,ReportError,$ANOVA2Way.NoDataFail2);
			return 1;
		}
		%A=ANOVA2Way!SelectedDataLCNTRL.V1_1$;
		/// GJL 4/11/02 QA70-1404 UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
		%B=$OStatCommon.SelectedDatasetNoExist;
		if(run.section(DBRoutines.OGS,CheckDatasetExists,"%B" "%A")!=0)
			return 1;
		%B=%[%A,'_'];
		if(%B!page.extInfo==1) // If data set is in Excel workbook update Origin
		{
			win -o %B
			{
				layer -s;
				set %B -ui;
			}
		}
		/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
		get %A -e nPts1;
		loop(ii,2,nData)
		{
			%A=ANOVA2Way!SelectedDataLCNTRL.V$(ii)_1$;
			%B=$OStatCommon.SelectedDatasetNoExist;
			if(run.section(DBRoutines.OGS,CheckDatasetExists,"%B" "%A")!=0)
				return 1;
			/// GJL 4/11/02 QA70-1404 UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
			%B=%[%A,'_'];
			if(%B!page.extInfo==1) // If data set is in Excel workbook update Origin
			{
				win -o %B
				{
					layer -s;
					set %B -ui;
				}
			}
			/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
			get %A -e nPts2;
			if(nPts1!=nPts2)
			{
				run.section(DBRoutines.OGS,ReportError,$ANOVA2Way.UnequalN);
				return 1;
			}
		}
	}
	
	%A=ANOVA2Way!SignificanceLevelEBX.v1$;	
	%B=$OStatCommon.SigLevelGT0LT1;
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%B" 0 1 (%A))!=0)
		return 1;
	
	%A=ANOVA2Way!PowerAnalysisEBX.v1$;
	%B=$OStatCommon.PowerSingleNumeric;
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%B" 0 1 (%A))!=0)
		return 1;
	
	%A=ANOVA2Way!SampleSizeEBX.v1$;
	%B=$OStatCommon.SSCommaSepList;
	if(run.section(DBRoutines.OGS,CheckMultiplePositiveIntegers,"%B" "%A")!=0)
		return 1;
	
	if(run.section(DBRoutines.OGS,LoadOStatWorkspace)!=0)
		return 1;
	
	return 0;

[OpenAndDirectOutputToResultsLog]
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	%A=ANOVA2Way!SelectedDataLCNTRL.V1_1$;
	%B=%[%A,'_'];
	win -o %B {type.BeginResults()}; // RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	return 0;

[ANOVA2Way]
	SigLevel=ANOVA2Way!SignificanceLevelEBX.v1;
	Interactions=ANOVA2Way!InteractionsCHKBX;
	SpecifyLevelsBy=ANOVA2Way!SpecifyLevelsRB;
	nData=ANOVA2Way!SelectedDataLCNTRL.numItems;
	Power=ANOVA2Way!PowerAnalysisCHKBX+2*ANOVA2Way!SampleSizeCHKBX;
	PowerAlpha=ANOVA2Way!PowerAnalysisEBX.v1;
	%A=ANOVA2Way!SampleSizeEBX.v1$;	
	_SS={%A};
	Test=ANOVA2Way!BonferroniCHKBX+2*ANOVA2Way!ScheffeCHKBX+4*ANOVA2Way!TukeyCHKBX;

	jj=osANOVA2Way(SigLevel,Interactions,SpecifyLevelsBy,nData,Power,PowerAlpha,"_SS",Test);
	if(exist(_SS)==1)
		delete _SS;

	if(jj==0) // If successfull completion save settings in ANOVA2 DB to OPS file and return
		dialog.SaveSettingsToOPS(OStat.DLL,ANOVA2Way);
	
	if(mod(jj,999)!=0) // If jj is not 0 (no error) and is not 999 (abort with no error message)...
		run.section(DBRoutines.OGS,ReportError,$ANOVA2Way.ComputationalError); // Output error message

	return jj;

[CloseAndRedirectOutput]
	type.EndResults();
	type.Redirection=SaveRedirection; // restore previous redirection.
	return 0;

[OnCancel]
	run.section(,CleanUp);
	return 0;

[CleanUp]
	if(exist(_SS)==1)
		delete _SS;
	delete -v DBindex;
	delete -v nPts1;
	delete -v nPts2;
	delete -v SelectAll;
	delete -v SigLevel;
	delete -v Interactions;
	delete -v SpecifyLevelsBy;
	delete -v nData;
	delete -v Power;
	delete -v PowerAlpha;
	delete -v Test;
	delete -v Dii;
	delete -v SaveRedirection;
	delete -v ii;
	delete -v jj;
	%A="";%B="";%Z="";
	return 0;