//--------------------------------------------------------------------------
// Filename:  FitCmp.ogs
// Purpose:   Handle events for the 'Fit Comparison' dialog.
//--------------------------------------------------------------------------


[OnInit]
	// This section called when dialog is shown for the first time.
	run.section(, FillData);
	run.section(, FillCategory);
	run.section(, FillFunction);


[OnOk]
	// This section called when the OK button is clicked.


[OnCancel]
	// This section called when the Cancel button is clicked.
	delete -v FitCmp*;


[OnApply]
	// This section called when the Apply button is clicked.


[OnHelp]
	// This section called when the Help button is clicked.


[cCategory.OnSelChange]
	// User selected a different category
	run.section(,FillFunction);


[FillData]
	// Fill Dataset combo boxes with Y datasets
	%M = "";
	%N = "";
	if(exist(%H) == 3)	// If it's a graph, default to first two datasets
	{
		count=1;
		doc -e DY {
			if(count == 1) %M = %C;
			if(count == 2) %N = %C;
			count++;
		}
	}
	else
	{
		if(exist(%H) == 2)	// If it's a worksheet ...
		{
			if(wks.c1 != wks.c2)	// and there is a selection
			{
				%M=%(%H, wks.c1);	// default to first and last selection
				%N=%(%H, wks.c2);
			}
			else
			{
				%M=%(%H, %(%H, @Y+, 1));
				%N=%(%H, %(%H, @Y-, wks.ncols));
			}
		}
	}
	FitCmp!cData1.reset();
	FitCmp!cData2.reset();
	FitCmptruecount = 0;
	FitCmpchoice1 = 1;
	FitCmpchoice2 = 1;
	doc -e W {
		%B=%[%H, 1:4];
		if("NLSF" != "%B")
		{
			for(FITCMPjj=1; FITCMPjj<=wks.ncols; FITCMPjj++)
			{
				if(wks.col$(FITCMPjj).type == 1)
				{
					%A=wks.col$(FITCMPjj).name$;
					%A=%H_%A;
					FitCmp!cData1.Add(%A);
					FitCmp!cData2.Add(%A);
					FitCmptruecount++;
					if("%A"=="%M") FitCmpchoice1 = FitCmptruecount;
					if("%A"=="%N") FitCmpchoice2 = FitCmptruecount;
				}
			}
		}
	}
	FitCmp!cData1.choice = FitCmpchoice1;
	if(FitCmpchoice2 <= FitCmptruecount) FitCmp!cData2.choice = FitCmpchoice2; else FitCmp!cData1.choice = FitCmpchoice1;


[FillCategory]
	// Fill Category combo box with NLSF Categories
	FitCmp!cCategory.reset();
	ini.file$ = NLSF.INI;
	ini.getstr(A, None, Function Group, NLSF Control, NLSF.INI); // Current default group
	count = ini.getsection(Category);
	for(FITCMPii = 1; FITCMPii <= count; FITCMPii++)
	{
		%B = ini.keyname$(FITCMPii)$;
		FitCmp!cCategory.Add(%B);
		if("%A"=="%B") FitCmp!cCategory.choice = FITCMPii;
	}


[FillFunction]
	// Fill Function combo box with NLSF Functions
	FitCmp!cFunction.reset();
	ini.file$ = NLSF.INI;
	%A = FitCmp!cCategory.v$(FitCmp!cCategory.choice)$;
	count = ini.getsection(%A);
	%A = ini.%A.Default Function$;
	if(%[%A] == 0) %A = NONE;
	for(FITCMPii = 1,FitCmpvcnt = 1;FITCMPii <= count; FITCMPii++)
	{
		%M = ini.keyname$(FITCMPii)$;
		%N = ini.keyvalue$(FITCMPii)$;
		if(("%M" != "Default Function") && ("%M" != "SaveOption"))
		{
			FitCmp!cFunction.Add(%M);
			if("%M" == "%A") FitCmp!cFunction.choice = FitCmpvcnt;
			FitCmpvcnt++;
		}
	}
	if(FitCmp!cFunction.choice == 0) FitCmp!cFunction.choice = 1;


[Exec.OnClick]
	// Runs when the user clicks Compare button in the dialog
	if(run.section(,Init) != 0) break 1;
	%P=%H;
	%A = FitCmp!cData1.choice$;
	%B = FitCmp!cData2.choice$;
	%M = FitCmp!cCategory.choice$;
	%N = FitCmp!cFunction.choice$;
	create.wksname$ = TmpResult;
	create.enumwks = 1;
	create.wkslabel$ = $FitCmp.WksLabel;
	create.template$ = CREATE;
	create.npts = 200;
	create.enumerate = 0;
	create.numtypes = 4;
	create.type1 = 4;
	create.type2 = 1;
	create.type3 = 1;
	create.type4 = 1;
	create.wks(A B Ares Bres);
	%O = create.wksname$;
	FitCmpSaved = type.redirection;
	type.redirection = 16;
	// USER can set LabTalk variable OFitCmpCL to use their own Confidence Level
	// Value defaults to 95
	if(OFitCmpCL == 0/0) OFitCmpCL = 95;
	FitCmpCL = OFitCmpCL / 100;	// Convert percent to fraction
	FitCmpCL = (1 + FitCmpCL) / 2;	// Required by ttable function
	// This is the OriginC function : two datasets, a worksheet, a category and a function
	FitCmpresult = CompareFits(%A, %B, %O, %M, %N);
	type.redirection = FitCmpSaved;
	clearworksheet %O;
	win -cd %O;
	if("%H"!="%P") win -cd %H;
	delete YFIT;
	delete -v FitCmp*;


[Refresh.OnClick]
	// Runs when user clicks the Refresh button in the dialog
	run.section(,FillData);	
// The last two lines are commented out to preserve user-selections
// If you typically modify NLSF functions/categories while using the Fit Comparison Tool
// then un-comment these lines so Refresh will reload Categories and Functions
//	run.section(,FillCategory);
//	run.section(,FillFunction);


[Main]
	// Main entry point, called from the menu
	// if(run.section(,Init)==0)	
	dialog.start(FitCmp,FitCmp);


[Init]
	// Load and compile the OriginC code
	if(run.LoadOC("OriginLab\FitCmp.ocw",4)!=0)
	{
		type -b $FitCmp.FitCompareNotLoaded;
		return 1;
	}
	if(FitCmpTrueCount == 0) return 1;
	return 0;

