//////////////////////////////////////////////////////////////////////
// Filename:	ANOVA1Way.OGS
// Purpose:		Event handlers for ANOVA1Way dialog box
// Creation:	by GJL, 4/2/01
// Modification Log:
//////////////////////////////////////////////////////////////////////

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

[OnInit]
	define DocEnd {run.section(Origin.OGS,DocEnd);};
	%A="ANOVA1Way!AvailableDataLBX";
	run.section(DBRoutines,PopLBXwNonTextData,"%A");
	ANOVA1Way!AvailableDataLBX.multSel=2;
	ANOVA1Way!SelectedDataLBX.CopySel(ANOVA1Way!AvailableDataLBX);
	ANOVA1Way!SelectedDataLBX.multSel=2;
	ANOVA1Way!SignificanceLevelEBX.v1$=$(5/100,.2);
	ANOVA1Way!PowerAnalysisEBX.v1$=ANOVA1Way!SignificanceLevelEBX.v1$;
	ANOVA1Way!SampleSizeEBX.v1$=50,100,200;
	ANOVA1Way!SampleSizeEBX.Enable=ANOVA1Way!PowerAnalysisCHKBX & ANOVA1Way!SampleSizeCHKBX;
	return 0;

[ToolBar.AddDataBTN.OnClick]
	jj=ANOVA1Way!AvailableDataLBX.numSels;
	loop(ii,1,jj)
	{
		%A=ANOVA1Way!AvailableDataLBX.S$(ii)$;
		run.section(,HighLightIfAlreadyInSelectedDataLBX,"%A");
	}
	ANOVA1Way!SelectedDataLBX.CopySel(ANOVA1Way!AvailableDataLBX);
	return 0;

[HighLightIfAlreadyInSelectedDataLBX]
	%A=%1;
	Tjj=ANOVA1Way!SelectedDataLBX.numItems;
	loop(Tii,1,Tjj)
	{
		%B=ANOVA1Way!SelectedDataLBX.V$(Tii)$;
		if("%A"=="%B")
		{
			ANOVA1Way!SelectedDataLBX.A$(Tii)=1;
			type -b;
			delete -v Tii;
			delete -v Tjj;
			return 1;
		}
	}
	delete -v Tii;
	delete -v Tjj;
	return 0;

[ToolBar.RemoveDataBTN.OnClick]
	ANOVA1Way!SelectedDataLBX.Del();
	return 0;

[SignificanceLevelEBX.OnEditChange]
	ANOVA1Way!PowerAnalysisEBX.v1$=ANOVA1Way!SignificanceLevelEBX.v1$;
	return 0;
	
[PowerAnalysisCHKBX.OnClick]
	ANOVA1Way!SampleSizeEBX.Enable=ANOVA1Way!PowerAnalysisCHKBX &
								   ANOVA1Way!SampleSizeCHKBX;
	return 0;

[SampleSizeCHKBX.OnClick]
	ANOVA1Way!SampleSizeEBX.Enable=ANOVA1Way!SampleSizeCHKBX;
	return 0;
			
[ComputeBTN.OnClick]
	run.section(DBRoutines.OGS,SaveAndSetAtOC);
	if(run.section(,CheckSettings)==0)
	{
		run.section(,OpenAndDirectOutputToResultsLog);
		run.section(,ANOVA);
		run.section(,TestsForEqualVariance);
		run.section(,MeansComparison);
		run.section(,PowerAnalysis);
		run.section(,CloseAndRedirectOutput);
		run.section(,CleanUp);
	}
	run.section(DBRoutines.OGS,RestoreAtOC);
	dialog.SaveSettingsToOPS(OStat.DLL,ANOVA1Way);
	return 0;

[CheckSettings]
	nData=ANOVA1Way!SelectedDataLBX.numItems;
	if(nData<2)
	{
		run.section(DBRoutines.OGS,ReportError,$ANOVA1Way.NoDataFail);
		return 1;
	}
	
	loop(ii,1,nData)
	{
		%A=ANOVA1Way!SelectedDataLBX.V$(ii)$;
		%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;
			}
		}
		sum(%A);npts=sum.n; // Don't count masked or missing values
		if(npts<1)
		{
			run.section(DBRoutines.OGS,ReportError,$OStatCommon.TooFewDataPts);
			return 1;
		}
		/// GJL 4/11/02 QA70-1404 End UPDATE_ORIGIN_FOR_EXCEL_WORKBOOK
	}
	
	%A=ANOVA1Way!SignificanceLevelEBX.v1$;	
	%B=$OStatCommon.SigLevelGT0LT1;
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%B" 0 1 (%A))!=0)
		return 1;
	
	%A=ANOVA1Way!PowerAnalysisEBX.v1$;
	%B=$OStatCommon.PowerSingleNumeric;
	if(run.section(DBRoutines.OGS,CheckSingleNumericGTandLT,"%B" 0 1 (%A))!=0)
		return 1;
	
	%A=ANOVA1Way!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=ANOVA1Way!SelectedDataLBX.V1$
	%B=%[%A,'_'];
	win -o %B {type.BeginResults()}; // RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	return 0;

[ANOVA]
	run.section(,ComputeFandP,"RawData");
	// Save ANOVA values stored in reused global variables for later use in Power calculations
	// and Means Comparisons
	ANOVAnData=nData;
	ANOVAnpts=npts;
	ANOVAdf1=df1;
	ANOVAdf2=df2;
	ANOVAssb=ssb;
	ANOVAmse=mse;
	_ANOVAMEAN=_MEAN;
	_ANOVANPTS=_NPTS;
	run.section(,DisplayFandP,"RawData");
	return 0;
	
[TestsForEqualVariance]
	if(ANOVA1Way!LeveneCHKBX)
	{
		run.section(,ComputeFandP,"Levene");
		run.section(,DisplayFandP,"Levene");
	}
	if(ANOVA1Way!BrownForsytheCHKBX)
	{
		run.section(,ComputeFandP,"BrownForsythe");
		run.section(,DisplayFandP,"BrownForsythe");		
	}
	return 0;

[ComputeFandP]
	stat.DS.more=1;
	%B=%1;
	nData=ANOVA1Way!SelectedDataLBX.numItems;
	SigLevel=ANOVA1Way!SignificanceLevelEBX.v1;
	_MEAN=data(0,0,nData);
	_NPTS=_MEAN;
	_SD=_MEAN;
	_SSE=_MEAN;
	npts=0;
	gmean=0;
	for(ii=1;ii<=nData;ii++) 
	{
		if(exist(_TEMP)==1)
			delete _TEMP;
		%A=ANOVA1Way!SelectedDataLBX.v$(ii)$;
		_TEMP=treplace(%A,0,0,2);    // This makes masked values become missing values 
		stat.DS.data$=_TEMP;         //  when copying the current dataset to _TEMP where as 
		stat.DS();                   //  vector assignment does not handle masking
		switch(%B)
		{
			case "RawData":
				break;
			case "Levene":
				_TEMP=(_TEMP-stat.DS.mean)^2;
				stat.DS();
				break;
			case "BrownForsythe":
				_TEMP=abs(_TEMP-stat.DS.median);
				stat.DS();			
				break;
		}
		npts+=stat.DS.size;
		gmean+=stat.DS.sum;
		_MEAN[ii]=stat.DS.mean;
		_NPTS[ii]=stat.DS.size;
		_SD[ii]=stat.DS.sd;
		_SSE[ii]=stat.DS.sd^2*(stat.DS.size-1);
	}
	gmean/=npts;
	ssb=0;
	for(ii=1;ii<=nData;ii++)
	{ 
		ssb+=_NPTS[ii]*(_MEAN[ii]-gmean)^2;
	}
	stat.DS.data$=_SSE;
	stat.DS();
	sse=stat.DS.sum;
	df1=nData-1;
	df2=npts-nData;
	msb=ssb/df1;
	mse=sse/df2;
	F=msb/mse;
	P=1-invf(F,df1,df2);
	return 0;

[DisplayFandP]
	%B=%1;
	
	// If raw data output Summary Statistics for raw datasets
	if("%B"=="RawData")
		osSummary_Stat_Output_to_ResultsLog(3,nData); // iTest=3 for One-Way ANOVA and number of datasets=nData

	// Output an F Table for One-Way ANOVA or for the Levene or Brown-Forsythe Test of Equal Variance  
	osANOVA1_Table_Output_to_ResultsLog(%B,df1,ssb,msb,F,P,df2,sse,mse,SigLevel);

	return 0;

[MeansComparison]
	Test=ANOVA1Way!BonferroniCHKBX+2*ANOVA1Way!ScheffeCHKBX+4*ANOVA1Way!TukeyCHKBX;
	if(Test)
	{
		SigLevel=ANOVA1Way!SignificanceLevelEBX.v1;
		ii=osANOVA1Way_Means_Comparison(Test,ANOVAnData,"_ANOVANPTS",ANOVAdf2,"_ANOVAMEAN",ANOVAmse,SigLevel);
		if(ii!=0)
		{
			run.section(DBRoutines.OGS,ReportError,$ANOVA1Way.MeansComparisonError);
			return ii;
		}
	}
	return 0;

[PowerAnalysis]
	if(ANOVA1Way!PowerAnalysisCHKBX)
	{
		run.section(,ComputeActualPower);
		if(ANOVA1Way!SampleSizeCHKBX)
			run.section(,ComputeHypotheticalPowers);
		run.section(,DisplayPower);
	}
	return 0;

[ComputeActualPower]
	alpha=1-ANOVA1Way!PowerAnalysisEBX.v1;
	Fval=ftable(alpha,ANOVAdf1,ANOVAdf2);
	nc=ANOVAssb/ANOVAmse;
	power=osANOVA_Power(Fval,ANOVAdf1,ANOVAdf2,nc);
	return 0;

[ComputeHypotheticalPowers]
	%A=ANOVA1Way!SampleSizeEBX$;
	_SS={%A};
	_SSPOW={%A};
	get _SS -e jj;
	loop(ii,1,jj)
	{
		ssnc=(_SS[ii]/ANOVAnpts)*nc;
		ssdf2=_SS[ii]-ANOVAnData;
		Fval=ftable(alpha,ANOVAdf1,ssdf2);		
		_SSPOW[ii]=osANOVA_Power(Fval,ANOVAdf1,ssdf2,ssnc);
	}
	return 0;

[DisplayPower]
	iPowerFlag=ANOVA1Way!PowerAnalysisCHKBX+2*ANOVA1Way!SampleSizeCHKBX;
	alpha=ANOVA1Way!PowerAnalysisEBX.v1;
	osPower_Output_to_ResultsLog(iPowerFlag,alpha,0,ANOVAnpts,power,"_SS","_SSPOW");	
	return 0;

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

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

[CleanUp]
	delete -v ii;
	delete -v jj;
	delete -v nData;
	delete -v npts;
	delete -v gmean;
	delete -v df1;
	delete -v df2;
	delete -v ssb;
	delete -v msb;
	delete -v sse;
	delete -v mse;
	delete -v F;
	delete -v P;
	delete -v ANOVAnData;
	delete -v ANOVAnpts;
	delete -v ANOVAdf1;
	delete -v ANOVAdf2;
	delete -v ANOVAssb;
	delete -v ANOVAmse;
	delete -v Fval;
	delete -v alpha;
	delete -v nc;
	delete -v power;
	delete -v iPowerFlag;
	delete -v ssdf2;
	delete -v ssnc;	
	delete -v Test;
	delete -v SigLevel;
	delete -v SaveRedirection;
	delete _SS;
	delete _SSPOW;
	delete _MEAN;
	delete _NPTS;
	delete _SD;
	delete _SSE;
	delete _TEMP;
	delete _ANOVAMEAN;
	delete _ANOVANPTS;
	%A="";%B="";
	return 0;