// Filename: NLSFWiz.OGS
// Creation: 07/09/01
// Author: Ryan Toomey
// Purpose: NLSF Wizard Script File - Fits one dataset using Dialog Builder Wizard
/// RVD 7/19/2002 QA70-1860 v7.0346 NLSF_BETTER_ADD_FUNCTION
/// ML 8/2/2002 QA70-1860 REWRITING_ADDING_FUNCTIONS
/// SDB 5/16/2003 QA70-4484 OUTPUT_CURVE_ACTIVE_GRAPH

[LaunchMe]	
	dialog.start(NLSFwiz,NLSFwiz);
	return 0;
	

//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// Select Data Page Scripts ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////


[Step.Data.OnInit]	
	if (exist(%H)==2)
	{
		if (exist(%C)!=1 || %(%H,@Y#,2)!=wks.iscolsel())
			{
				OpenErrorNLSFWiz=1;
				type -b $NLSFwiz.LaunchMe2;
				dialog.close(NLSFwiz,NLSFwiz);
				return;
			}			
	};
	if (exist(%H)==3)
		{
			if (exist(%C)!=1)
				{
					OpenErrorNLSFWiz=1;
					type -b $NLSFwiz.LaunchMe4;
					dialog.close(NLSFwiz,NLSFwiz);
					return;
				}
			if (exist(%[%C,"_"])!=2)
				{
					edit %C;
					win -ch 1;
				}
		}
		
	get %C -e ii;	
	if ((ii-ismasked(0,%C))<2)
		{
			OpenErrorNLSFWiz=1;
			type -b $NLSFwiz.LaunchMe3;
			dialog.close(NLSFwiz,NLSFwiz);
			return;
		}

	del -v OpenErrorNLSFWiz;
	nlsf.init(); // initialize fitter
	nlsf.begin(); // begin fitting
	
	
	// initialize hidden controls
	NLSFwiz!ActiveWindowTitle$="%H"; // store name of window from which wizard was launched
	NLSFwiz!ActiveLayerNumber.v1$=$(page.active); 
	//del -b activeLayer; // delete variable right away since it's not needed
	NLSFwiz!MagnificationSpin.min=0; // set min for hidden spin
	NLSFwiz!MagnificationSpin.max=10; // set max for hidden spin
	NLSFwiz!MagnificationSpin=0; // init hidden spin to 0

	%B=%C; // snag %C
	%A=xof(%B); // snag associated X dataset for %C

	run.section(,ResetLimits); // clears any previous default range settings

	// store original active dataset and its associated x dataset
	NLSFwiz!ActiveX$="%A";
	NLSFwiz!ActiveY$="%B";
	
	// initialize Data page controls
	NLSFwiz!Step.Data.RadioActive=1; // set Active dataset radio button as selected
	NLSFwiz!Step.Data.SelectIndep$="%A"; // display associated X dataset	
	run.section(,CreateDepVarList,%B); // create Y dataset list and set active dataset as selected
	NLSFwiz!FitData$="%B"; // display dataset to be fitted in Current Selections group

	// initialize Plot Attributes controls
	ini.file$="NLSFwizard.ini";
	NLSFwiz!scatter=$(ini.SystemVariables.Scatter); 
	NLSFwiz!line=$(ini.SystemVariables.Line);
	NLSFwiz!SymbSizeDropDown=$(ini.SystemVariables.SymbolSizeIndex);// initialize symbol size droplist
	NLSFwiz!ConnectDropDown=$(ini.SystemVariables.LineConnectIndex); // initialize Connect droplist
	if(exist(%H)==3)
	{
		NLSFwiz!ScaleType=layer.x.type; // set scaleType to that of active graph
	}
	else
	{
		NLSFwiz!ScaleType=1; // set scaleType to linear if start from worksheet
	}

	// initialize buttons
	run.section(,Toolbar1Control,1); // enable toolbar 1
	NLSFwiz!Toolbar1.DemagnifyButton.enable=0; // disable until magnify (enlarger) is used
	NLSFwiz!Toolbar2.RescaleButton.enable=0; // disable until magnify is used
	run.section(,Toolbar2Control,0); // disable toolbar 2
	run.section(,Toolbar3Control,0); // disable toolbar 3, including parameters display button
	NLSFwiz!Finish.enable=0; // disable Finish button	
	
	// initialize fitting function so we can determine if peaks page should be available
	ini.getstr(A,,Function,NLSF Control,NLSF.ini); // get default func in default cat
	// Begin QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
	nlsf.msgPrompt=0;
	nlsf.func$=%A; // set fitting function
	nlsf.msgPrompt=1;
	// End QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
	run.section(,HideShowPeaksPage); // hide or show peaks page
	return 0;
	
	
[ResetLimits]
	// hidden edit boxes on main page
	NLSFwiz!defaultFrom.v1=-1;
	NLSFwiz!defaultTo.v1=-1;
	return 0;
	

[CreateDepVarList]

	NLSFwiz!Step.Data.SelectDep.reset(); // reset droplist
	ii=1; 
	doc -e W
	{
		doc -e D
		{
			if(%H!wks.col$(colnum(%C)).type==1 && %[%C,5]!="NLSF")
			{
			
					NLSFwiz!Step.Data.SelectDep.v$(ii)$=%C;
					if("%1"=="%C")
						{
						NLSFwiz!Step.Data.SelectDep.Choice=ii; // set selected dataset to active dataset
						}
				ii+=1;
			}
		}
	}
	return 0;
	

[SetLimitDefaults]
	// sets hidden default From and To text boxes
	// sets index values for default From and To
	// sets visible current From and To text boxes

	if(NLSFwiz!defaultFrom.v1==-1 && NLSFwiz!defaultTo.v1==-1)
	{
		%W=NLSFwiz!Step.Data.GraphOnData.graph$;	
		win -oa %W 
		{
			limit %2;
			NLSFwiz!defaultFrom.v1=limit.xmin; // get min X value
			NLSFwiz!defaultTo.v1=limit.xmax; // get max X value

			NLSFwiz!fromIndex.v1=list(limit.xmin,%1); // get min X value index
			NLSFwiz!toIndex.v1=list(limit.xmax,%1); // get max X value index

			NLSFwiz!Step.Data.FromTextBox$=$(NLSFwiz!defaultFrom.v1); // set From text box in wizard
			NLSFwiz!Step.Data.ToTextBox$=$(NLSFwiz!defaultTo.v1); // set To text box in wizard
		}
	}
	return 0;
	
	
[Step.Data.Active]
	// create graph in wizard
	NLSFwiz!graph.create(NLSFWiz);
	NLSFwiz!Step.Data.GraphOnData.attach();
	NLSFwiz!Step.Data.GraphOnData.SetRef();

	// get initial values	
	%A="NLSFwiz!Step.Data.GraphOnData.graph$";
	NLSFwiz!GraphNameHolder$=%A; // stores property as page marker
	%B=NLSFwiz!Step.Data.SelectIndep$;
	%K=NLSFwiz!Step.Data.SelectDep.s1$;

	// plot active dataset into graph,set up plot/axis attributes, and set nlsf fit data properties
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	win -oa %W
		{	
			dotool 0;
			page.active=1;
			layer.x.type=NLSFwiz!ScaleType; // set embedded graph scale type
			ini.file$="NLSFwizard.ini";
			run.section(,ReplotData,%K $(ini.SystemVariables.Scatter) $(ini.SystemVariables.Line)); // plot fit data & set plot attributes
			layer -a;
			doc -uw;
			run.section(,SetLimitDefaults,%B %K); // set hidden edit boxes to default range from and to values
			run.section(,SetNlsfFitData,%B %K); // initialize x and y datasets to be used during fitting
			nlsf.dataBegin=$(NLSFwiz!defaultFrom.v1); // initialize From to be used during fitting
			nlsf.dataEnd=$(NLSFwiz!defaultTo.v1); // initialize To to be used during fitting
		}

	// enable or disable appropriate buttons on page
	run.section(,PlotAttributesControl,1);
	run.section(,FinishControl); // enable/disable finish button
	NLSFwiz!Step.Pal=0; // enable Next Button
	return 0;
	

[Step.Data.RadioActive.OnClick]
	// this applies to whenever the status of the radio button group is altered
	// therefore, if the manual selection radio is clicked, the value is 2
	if (NLSFwiz!Step.data.radioactive.choice==1)
	{
		run.section(,resetToActive);
	}
	else
	{
		%B=NLSFwiz!ActiveY;
		run.section(,CreateDepVarList,%B); // refreshes list in case user left wizard & created datasets
		NLSFwiz!Step.Data.SelectDep.choice=1; // select first dataset in list
		run.section(,Step.Data.SelectDep.OnSelChange); // update plot and nlsf object
	}
	return 0;
	

[ResetToActive]	
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	win -oa %W
	{
		dotool 0;
		page.active=1;
		layer -a;
		doc -uw;
	}
	
	NLSFwiz!Finish.enable=0; // disable finish
	// need to reset nlsf properties to make sure Finish button stays disabled 
	//nlsf.init(); // this should do it
			
	// clean up - delete fit worksheet, reset range settings
	%A=NLSFwiz!GraphNameHolder$;
	run.section(,Cleanup,%A);
	run.section(,ResetLimits);
	
	// get original dataset and associated x dataset
	%A=NLSFwiz!ActiveX$;
	%B=NLSFwiz!ActiveY$;

	run.section(,CreateDepVarList,%B); // recreate Y dataset list and set to active dataset
	NLSFwiz!Step.Data.SelectIndep$=%A; // reset X static text to 1st
	NLSFwiz!FitData$=%B; // reset static text displaying current fit data
	
	run.section(,SetLimitDefaults,%A %B); // set hidden edit boxes to original dataset's range values
	NLSFwiz!Step.Data.FromTextBox$=$(NLSFwiz!defaultFrom.v1); // initial value for Range From text box
	NLSFwiz!Step.Data.ToTextBox$=$(NLSFwiz!defaultTo.v1); // initial value for Range To text box

	
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	win -oa %W
	{
		if(exist(YFIT)==1)
			layer -e YFIT;
		layer -e %C; // remove active dataset
		run.section(,ReplotData,%B $(NLSFwiz!Scatter) $(NLSFwiz!Line)); // replot original data
		// remove bands or residuals if the buttons are down
		if(NLSFwiz!Toolbar3.ResidualsButton==0)
			run.section(,Toolbar3.ResidualsButton.OnClick);
		if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
			run.section(,RemoveBands,"LCL");
		if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
			run.section(,RemoveBands,"LPL");

		// reassign original data as fit data
		run.section(,SetNlsfFitData,%A %B);
		nlsf.dataBegin=$(NLSFwiz!defaultFrom.v1); // initialize From to be used during fitting
		nlsf.dataEnd=$(NLSFwiz!defaultTo.v1); // initialize To to be used during fitting		
	}
	return 0;
	

[Step.Data.SelectDep.OnSelChange]
	NLSFwiz!Finish.enable=0; // disable finish
		
	// delete previous fit's worksheet
	%A=NLSFwiz!GraphNameHolder$;
	run.section(,Cleanup,%A); // clean up nlsf properties and graph (of fit curves)

	%B=NLSFwiz!Step.Data.SelectDep.s1$; // get Y dataset name
	%A=xof(%B); // get associated X dataset name
	NLSFwiz!Step.Data.SelectIndep$=%A; // display X dataset name on Data page
	NLSFwiz!FitData$=%B; // display Y dataset name at bottom of wizard
	
	run.section(,ResetLimits); // clears any previous default range settings 
	run.section(,SetLimitDefaults,%A %B); // set hidden/visible edit boxes to default range from and to values
		
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	win -oa %W
	{
		dotool 0;
		page.active = 1;
		layer -o 1 
		{
			if(exist(YFIT)==1)
			{
				nlsf.end(13);
				nlsf.cleanupfitdata();
				nlsf.begin();
			}
			layer -c; // get list of data plots in layer
			layer -e %Z; // remove all data plots in layer
			run.section(,ReplotData,(%B) $(NLSFwiz!Scatter) $(NLSFwiz!Line)); // plot new data
			layer -a; // rescale 
			doc -uw; // refresh
		}

		// update fit data
		run.section(,SetNlsfFitData,%A %B);
		nlsf.dataBegin=$(NLSFwiz!defaultFrom.v1); // initialize From to be used during fitting
		nlsf.dataEnd=$(NLSFwiz!defaultTo.v1); // initialize To to be used during fitting
	}
	return 0;
	
	
[Step.Data.SelectRangeButton.OnClick]
	if(run.LoadOC("OriginLab\App_Utils.C")!=0)
	{
		type -b "OriginC routines failed to load properly";      
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC); // turns off OriginC from labtalk
	
	%W=NLSFwiz!GraphNameHolder$;
	run.section(,Cleanup,%W);
	%A=NLSFwiz!Step.Data.SelectDep.s1$;
	win -oa %W
	{
		dotool -dh 4;
		dotool -l;
		run.section(,HideShowPositionGroup,1);
		def quittoolbox {
				run.section(,HideShowPositionGroup,0);
				win -oa %W
				{
					// set wizard properties
					limit %A;
					NLSFwiz!Step.Data.FromTextBox$=$(limit.xmin);
					NLSFwiz!Step.Data.ToTextBox$=$(limit.xmax);
	
					// set nlsf properties
					nlsf.dataBegin=$(mks1);
					nlsf.dataEnd=$(mks2);
	
					layer -o 1
					{
						// rescale graph in wizard to mks1 and mks2
						set %A -b mks1;
						set %A -e mks2;
						layer -e _marker;
						doc -uw;
					}
					_marker[1]=-1;_marker[2]=-1;
					mks1=-1;mks2=-1;
					layer -a;
					Write_Data_Display_Text(""); // clear data display
					run.section(DBRoutines,RestoreAtOC); // reset OriginC from LabTalk
				}
				NLSFwis!Next.SetFocus();
				}
	}
	return 0;

	
[HideShowPositionGroup]
	NLSFwiz!Step.Data.MarkerPosition.Show=%1;
	NLSFwiz!Step.Data.XPosition.Show=%1;
	NLSFwiz!Step.Data.XPosTextBox.Show=%1;
	NLSFwiz!Step.Data.YPosition.Show=%1;
	NLSFwiz!Step.Data.YPosTextBox.Show=%1;
	if(%1==0)
	{
		del -m ontool;
	}
	else
	{
		def ontool {
			NLSFwiz!Step.Data.XPosTextBox.v1=x;
			NLSFwiz!Step.Data.YPosTextBox.v1=y;
		}
	}
	return 0;
	

[Step.Data.FullRangeButton.OnClick]
	// select pointer tool
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	win -oa %W
	{
		dotool 0;
	}
	
		
	%A=NLSFwiz!GraphNameHolder$;
	run.section(,Cleanup,%A);

	%B=NLSFwiz!Step.Data.SelectDep.s1$;

	// set wizard properties
	NLSFwiz!Step.Data.FromTextBox$=$(NLSFwiz!defaultFrom.v1); // rangeFrom taken from [Step.Data.OnInit]
	NLSFwiz!Step.Data.ToTextBox$=$(NLSFwiz!defaultTo.v1); // rangeTo taken from [Step.Data.OnInit]

	// replot data with full range
	win -oa %W
	{
		// set nlsf properties
		nlsf.dataBegin=$(NLSFwiz!fromIndex.v1);
		nlsf.dataEnd=$(NLSFwiz!toIndex.v1);

		set %B -b $(NLSFwiz!fromIndex.v1);
		set %B -e $(NLSFwiz!toIndex.v1);
		
		layer -a;
		doc-uw;
	}
	
	// make SetDataDisplayText function available and clear Data Display
	if(run.LoadOC("OriginLab\App_Utils.C")!=0)
	{
		type -b "OriginC routines failed to load properly";      
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC); // turns off OriginC from labtalk
	Write_Data_Display_Text(""); // clear data display
	run.section(DBRoutines,RestoreAtOC); // reset OriginC from LabTalk
	return 0;
	

[Step.Data.FromTextBox.OnKillFocus]
	%A=NLSFwiz!GraphNameHolder$;
	run.section(,Cleanup,%A);

	%A=NLSFwiz!Step.Data.SelectIndep$;
	%B=NLSFwiz!Step.Data.SelectDep.s1$;
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	limit %B;
	// check to see if the value entered is legit
	if(NLSFwiz!Step.Data.FromTextBox.v1<NLSFwiz!defaultFrom.v1 || NLSFwiz!Step.Data.FromTextBox.v1>NLSFwiz!defaultTo.v1)
		{
		NLSFwiz!Step.Data.FromTextBox$=$(NLSFwiz!defaultFrom.v1);
		win -oa %W
			{
				set %B -b $(NLSFwiz!fromIndex.v1);
				layer -a;
				doc -uw;
			}
		}
	else
		{
		win -oa %W
			{
				set %B -b $(NLSFwiz!fromIndex.v1);
				ii=xindex(NLSFwiz!Step.Data.FromTextBox.v1,%B); // get new X From ROW value
				tempRangeFrom=$(%A[ii]); // get new X From Range value
				set %B -b ii; // set range begin using new ROW From value
				layer -a;
				doc -uw;

				NLSFwiz!Step.Data.FromTextBox.v1$=$(tempRangeFrom); // display new X From Range value
				nlsf.dataBegin=$(ii); // set beginning of fitting range to new ROW From value
			}
		}
	return 0;
	

[Step.Data.ToTextBox.OnKillFocus]
	%A=NLSFwiz!GraphNameHolder$;
	run.section(,Cleanup,%A);

	%A=NLSFwiz!Step.Data.SelectIndep$;
	%B=NLSFwiz!Step.Data.SelectDep.s1$;
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	limit %B;
	// check to see if the value entered is legit
	if(NLSFwiz!Step.Data.ToTextBox.v1<NLSFwiz!defaultFrom.v1|| NLSFwiz!Step.Data.ToTextBox.v1>NLSFwiz!defaultTo.v1)
	{	
		NLSFwiz!Step.Data.ToTextBox$=$(NLSFwiz!defaultTo.v1);
		win -oa %W
			{
				set %B -e $(NLSFwiz!toIndex.v1);
				layer -a;
				doc -uw;
			}
	}
	else
	{		
		win -oa %W
			{
				set %B -e $(NLSFwiz!toIndex.v1);
				ii=xindex(NLSFwiz!Step.Data.ToTextBox.v1,%B); // get new X To ROW value
				tempRangeTo=$(%A[ii]); // get new X To Range value
				set %B -e ii; //set range end using new ROW To value
				layer -a;
				doc -uw;

				NLSFwiz!Step.Data.ToTextBox.v1$=$(tempRangeTo); // display new X To Range value
				nlsf.dataEnd=$(ii); // set end of fitting range to new ROW To value
			}
	}
	return 0;


//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// Select Function Page Scripts ////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////



[Step.Function.OnInit]

	%A="FunctionPage";
	NLSFwiz!GraphNameHolder$=%A;

	NLSFwiz!Step.Function.PreviewGroup.SetRef(); // set preview group as reference
	/// JJ 8-10-01 t9287
	NLSFwiz!Step.Function.PreviewEq.SetRef();
	NLSFwiz!Step.Function.PreviewPic.SetRef();
	/// end t9287
	return 0;
	
	
[InitCat]
	catNum=0;
	// Begin QA 571 Part 1 : RJT : 6/28/02
	%A=NLSFwiz!OPSCategory$; // hidden edit box that stores category saved to ops file

	if(%A=="")
	{	
		ini.getstr(A,,Function Group,NLSF Control,NLSF.ini); // get default category name
	}
	
	ini.file$="nlsf.ini"; // prepare nlsf.ini for processing
	ini.getsection(Category); // get [Category] section
	// loop to determine default category key number
	loop(ii,1,ini.keynum)
	{
		%K=ini.keyname$(ii)$;
		if("%K"=="%A")
			catNum=ii; // store key number of default category
	}
	run.section(,ListCat); // list values in Category
	NLSFwiz!Step.Function.CatList.choice=$(catNum); // set selected category to default
	
	NLSFwiz!OPSCategory$=%A; // send selected category to OPS storage
	// End QA 571 Part 1 : RJT : 6/28/02
	return 0;
	
		
[InitFunc]
	
	// list the functions, get the default func in the cat,
	// check the default func. If it's not listed in the func list in the wizard,
	// select the first function. Don't set it as default in NLSF.ini though
	jj=0;
	funcNum=0;
	// Begin QA 571 Part 2 : RJT : 6/28/02
	%A=NLSFwiz!OPSFunction$; // hidden edit box that stores function saved to ops file 
	%B=NLSFwiz!OPSCategory$; // hidden edit box that stores category saved to ops file
	if(%[%A]==0 || %[%B]==0)
	{
		ini.getstr(B,,Function Group,NLSF Control,NLSF.ini); // get default category
		ini.getstr(A,,Function,NLSF Control,NLSF.ini); // get default func in default cat
	}
	NLSFwiz!OPSFunction$="%A"; // send function name to OPS storage
	NLSFwiz!OPSCategory$="%B"; // send category name to OPS storage

	// End QA 571 Part 2 : RJT : 6/28/02
	ini.file$="nlsf.ini"; // prepare nlsf.ini for processing	
	ini.getsection(%B); // get section for default category
	if(ini.keynum>0)
	{
		loop(ii,1,ini.keynum)	
		{
			%K=ini.keyname$(ii)$;
			if(("%K"!="Default Function") && ("%K"!="SaveOption"))
			{ 
				jj++; // bump up count
				if("%K"=="%A")
					funcNum=jj;	// get key number for default func in default cat						
			}
		}
	}
	else
	{
		NLSFwiz!Step.Pal=2; // disable Next
		return 1;
	}
	// choose function based on if default function is usable in wizard
	if(nlsf.numindep>1 || nlsf.numdep>1)
		NLSFwiz!Step.Function.FuncList.choice=1; // set selected function to default function
	else
		NLSFwiz!Step.Function.FuncList.choice=$(funcNum); // set selected function to default function
	
	
	// Begin QA 2014 : RJT : 6/28/02
	// replaced UpdateFitFunc section call with this:
	NLSFwiz!FitFunc$="%A"; // set Function display on wizard to latest function	
	// Begin QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
	nlsf.msgPrompt=0;
	nlsf.cleanupfitdata();
	nlsf.func$="%A"; // set fitting function for nlsf
	nlsf.msgPrompt=1;
	// End QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
	run.section(,ShowPreview); // show eq and pic previews
	run.section(,HideShowPeaksPage); // determine if selected fitting function needs peaks page
	NLSFwiz!Finish.enable=0; // disable Finish
	run.section(,Toolbar3Control,0); // disable toolbar 3
	// End QA 2014 : RJT : 6/28/02
	return 0;

[CheckSaveOption]
	// Begin QA 83 : RJT : 5/1/02
	%A=NLSFwiz!Step.Function.CatList.s1$;
	// End QA 83 : RJT : 5/1/02
	ini.getstr(S,,SaveOption,%A,nlsf.ini);
	if((%[%S]==0) || (%[%S]>0 && %S=="0"))
	{
		return 0; // not protected
	}
	return 1; // category is protected
	

[FunctionPageButtonControl]
	NLSFwiz!Step.Function.AddFunc.enable=%1;
	NLSFwiz!Step.Function.DeleteFunc.enable=%1;
	NLSFwiz!Step.Function.DeleteCat.enable=%1;
	NLSFwiz!Step.Function.EditCat.enable=%1;
	return 0;
	

[Step.Function.Active]	
	if(NLSFwiz!Step.Function.CatList.choice==0 || NLSFwiz!Step.Function.FuncList.choice==0)
	{
		run.section(,InitCat); // initialize category based on default
		run.section(,ListFunc); // list values in OBF section
		run.section(,InitFunc); // initialize function based on default in default category
		NLSFwiz!Step.Function.PreviewPic.Init(); // initiate pic preview
		NLSFwiz!Step.Function.PreviewEq.Init(); // initiate equation preview
		run.section(,ShowPreview); // show both previews

		// determine if add and delete for functions should be disabled by checking SaveOption
		if(run.section(,CheckSaveOption))
		{
			run.section(,FunctionPageButtonControl,0);	
		}
		else
		{
			run.section(,FunctionPageButtonControl,1);
		}
	
		NLSFwiz!Finish.enable=0; // disable Finish
		run.section(,Toolbar3Control,0); // disable Toolbar3 buttons
	}
	%A="FunctionPage";
	NLSFwiz!GraphNameHolder$=%A;  // stores property as page marker
	
	run.section(,PlotAttributesControl,0);
	NLSFwiz!Toolbar1.EnlargerButton.enable=0;

	run.section(,FinishControl); // enable/disable finish button
	return 0;
	

[Step.Function.CatList.OnSelChange]
	
	%A=NLSFwiz!Step.Function.CatList.s1$; // get selected category name
	
	// Begin QA 571 Part 3 : RJT : 6/28/02
	NLSFwiz!OPSCategory$="%A"; //send selection to hidden edit box for OPS storage
	// End QA 571 Part 3 : RJT : 6/28/02
	ini.setstr(%A,Function Group,NLSF Control,nlsf.ini); // send selection to nlsf.ini as default category
	ini.get.fileName$="nlsf.ini"; // get nlsf.ini file
	ini.getsection(Category); // get Category section + access to its ini.keynum
	if(ini.keynum<=0)
	{
		NLSFwiz!Step.Function.CatList.reset(); // keynum<=0 for catlist is highly unlikely, but good to address
		NLSFwiz!Step.Function.FuncList.reset();
		NLSFwiz!Step.pal=2; // disable Next button		
	}
	else
	{
		ini.getsection(%A); // get selected section in nlsf.ini file
		if(ini.keynum>0)
		{
			run.section(,ListFunc); // list functions in selected category
			NLSFwiz!Step.Function.FuncList=1; // select first function by default
			run.section(,UpdateFitFunc); // update nlsf.func$
			NLSFwiz!Step.pal=0; // enable Next button	
		} 
		else 
		{	
			NLSFwiz!Step.Function.FuncList.reset();
			// Begin : QA 2418 : UPDATE_PREVIEW_FOR_NO_FUNC : 8/6/02 : RJT
			run.section(,UpdateFitFunc);
			// End : QA 2418 : UPDATE_PREVIEW_FOR_NO_FUNC : 8/6/02 : RJT
			NLSFwiz!Step.pal=2; // disable Next button
		}
	}

	// need to check if selected category is protected before allowing Add
	if(run.section(,CheckSaveOption))
	{
		run.section(,FunctionPageButtonControl,0);
	}
	else
	{
		run.section(,FunctionPageButtonControl,1);
	}
	return 0;
	
		
[Step.Function.NewCat.OnClick]
	getstring ($NLSFwiz.NewCat1A) ($NLSFwiz.NewCat1B) ($NLSFwiz.NewCat1C); // ask for new category
	if(run.section(,CheckCat,"%B"))
	{
		type -b $NLSFwiz.NewCat2;
		return 1; // check to see if the new category name already exists
	}
	
	// begin QA 672 : RJT : 4/29/02
	if("%B"=="Category")
	{
		type -b $NLSFwiz.NewCat4;
		return 2; // prevent if name is 'Category'
	}
	// end QA 672 : RJT : 4/29/02
	
	// Begin QA 571 Part 4 : RJT : 6/28/02
	NLSFwiz!OPSCategory$="%B"; // send new category to OPS storage
	NLSFwiz!OPSFunction$=""; // set OPS storage for function to empty
	// End QA 571 Part 4 : RJT : 6/28/02
	
	nlsf.addCategory(%B); // add category to nlsf.ini
	run.section(,UpdateCatList,1); // update category list in wizard
	run.section(,FunctionPageButtonControl,1); // enable function page buttons
	run.section(,UpdateFuncList,1); // reset function list and disable Next
	// Begin : QA 2418 : UPDATE_PREVIEW_FOR_NO_FUNC : 8/6/02 : RJT
	run.section(,UpdateFitFunc);
	type -b $NLSFwiz.NewCat3; // remind the user to add a fitting function to the new category
	// End : QA 2418 : UPDATE_PREVIEW_FOR_NO_FUNC : 8/6/02 : RJT
	return 0;
	

[CheckCat]	

	
	// probably could have used ini.check(%1) here	
	ini.file$="nlsf.ini";
	ini.getsection(Category);
	loop(ii,1,ini.keynum)
	{
	%A=ini.keyname$(ii)$;
		if("%1"=="%A")
		{
			return 1;
		}
	}
	return 0;

[Step.Function.EditCat.OnClick]	
	%A=NLSFwiz!Step.Function.CatList.s1$; // get selected category name	
	getstring ($NLSFwiz.EditCat1A) (%A) ($NLSFwiz.EditCat1B); // bring up window to edit Category
	
	if(run.section(,CheckCat,"%B"))
	{
		type -b $NLSFwiz.NewCat2;
		return 1; // check to see if the new category name already exists
	}
	
	// begin QA 672 : RJT : 4/29/02
	if("%B"=="Category")
	{
		type -b $NLSFwiz.NewCat4;
		return 2; // prevent if name is 'Category'
	}
	// end QA 672 : RJT : 4/29/02
	
	// only rename if names aren't the same - this is the last hurdle to jump 
	if("%A"!="%B")
	{
		// Begin QA 571 Part 5 : RJT : 6/28/02
		NLSFwiz!OPSCategory$="%B"; // store new category name in hidden control for OPS
		// End QA 571 Part 5 : RJT : 6/28/02
		nlsf.renameCategory(%A,%B); // rename - replaces add/copy/delete
		run.section(,UpdateCatList,2 $(NLSFwiz!Step.Function.CatList.choice)); // update cat list and select edited cat
		run.section(,UpdateFuncList,2 $(NLSFwiz!Step.Function.FuncList.choice)); // update func list to those in selected cat and select appropriate func
		run.section(,UpdateFitFunc); // update nlsf.func$
	}
	return 0;
	

[Step.Function.DeleteCat.OnClick]	
	if(run.section(,CancelDeleteCat))
		return 1;
	%A=NLSFwiz!Step.Function.CatList.s1$; // get name of selected category
	nlsf.deleteCategory(%A); // delete selected category from nlsf.ini
	run.section(,UpdateCatList,3 $(NLSFwiz!Step.Function.CatList.choice)); // update category list
	
	%A=NLSFwiz!Step.Function.CatList.s1$; // get name of newly selected category
	NLSFwiz!OPSCategory$="%A"; // assign newly selected category to ops storage
	
	run.section(,UpdateFuncList,3); // update function list to that of category above deleted category 
	// unless it was 1st - then select the 1st
	run.section(,UpdateFitFunc); // update nlsf.func$
	return 0;
	
	
[CancelDeleteCat]
	%A=NLSFwiz!Step.Function.CatList.s1$;	
	type -n $NLSFwiz.CancelDeleteCat;
	return 0;
	
	
[Step.Function.FuncList.OnSelChange]
	run.section(,UpdateFitFunc); // update nlsf.func$
	NLSFwiz!Step.pal=0; // enable Next button
	return 0;
	

[Step.Function.DeleteFunc.OnClick]
	%A=NLSFwiz!Step.Function.CatList.s1$;
	%B=NLSFwiz!Step.Function.FuncList.s1$;

	if(run.section(,CancelDeleteFunc,%B))
		return 1;

	ini.file$="nlsf.ini";
	ini.section$=%A;
	ini.setstr(,%B,%A,nlsf.ini);

	run.section(,UpdateFuncList,4 $(NLSFwiz!Step.Function.FuncList.choice));
	run.section(,UpdateFitFunc);
	return 0;
	

[CancelDeleteFunc]
	%W="%1?";
	%Z=$NLSFwiz.CancelDeleteFunc;
	type -n %Z %W;
	return 0;
	

[Step.Function.AddFunc.OnClick]
	%W=NLSFwiz!Step.Function.CatList.s1$;
	// need to check if selected category is protected before allowing Add
	if(run.section(,CheckSaveOption))
	{	
		type -b $NLSFwiz.AddFunc1;
		return 1;
	}

	getfilename *.fdf; // get fdf file and path names into string vars
	/// ML 8/2/2002 QA70-1860 REWRITING_ADDING_FUNCTIONS
	///// RVD 7/19/2002 QA70-1860 v7.0346 NLSF_BETTER_ADD_FUNCTION
	////nlsf.addfunc(%W,%B%A); // add fitting function to FitFunc folder and nlsf.ini file
	//nlsf.addfunc(%W,%B%A,1); // add fitting function to FitFunc folder and nlsf.ini file
	///// end NLSF_BETTER_ADD_FUNCTION
	if ( 0.0 != nlsf.addfunc(%W,%B%A,0,B) )	// no copy by default, and return the actual .FDF pathname into %B
	{
		return 1;
	}
	/// end REWRITING_ADDING_FUNCTIONS

	// add in functionality
	// allow function to list as long as there is only 1 indep and 1 dep variable
	/// ML 8/2/2002 QA70-1860 REWRITING_ADDING_FUNCTIONS
	//ini.file$=%B%A; // get fdf file
	ini.file$=%B; // get fdf file
	/// end REWRITING_ADDING_FUNCTIONS
	ini.getsection(Independent Variables);
	numind=$(ini.keynum); // read number of independent variables
	ini.getsection(Dependent Variables);
	numdep=$(ini.keynum); // read number of dependent variables

	if(numind==1 && numdep==1)
	{
		run.section(,ListFunc); // relist functions with it added
		NLSFwiz!Step.Function.FuncList=NLSFwiz!Step.Function.FuncList.numItems; // set selected function to added function
		run.section(,UpdateFitFunc); // update nlsf.func$
		NLSFwiz!Step.pal=0; // enable Next button
	}
	else
	{
		type -b $NLSFwiz.AddFunc2;
	}
	return 0;
	

[Step.Function.PreviousFunc.OnClick]
	currentFunc=NLSFwiz!Step.Function.FuncList;
	if (NLSFwiz!Step.Function.FuncList!=1)
		{
		NLSFwiz!Step.Function.FuncList=(currentFunc-1); // move back one
		}
	else
		{
		NLSFwiz!Step.Function.FuncList=NLSFwiz!Step.Function.FuncList.NumItems; // step to end of list
		}
	run.section(,UpdateFitFunc); // update nlsf.func$
	return 0;
	
	
[Step.Function.NextFunc.OnClick]	
	currentFunc=NLSFwiz!Step.Function.FuncList;
	if(currentFunc!=NLSFwiz!Step.Function.FuncList.NumItems)
		{
		NLSFwiz!Step.Function.FuncList=(currentFunc+1); // move up one
		}	
	else
		{
		NLSFwiz!Step.Function.FuncList=1; //step to beginning of list
		}
	run.section(,UpdateFitFunc); // update nlsf.func$
	return 0;
	
	

/***********************************************************************/
/* Sections used more than once for Function Page of Wizard are below: */
/***********************************************************************/



[UpdateCatList]
	// If this section is called from:
	// NewCat - the new one must be selected after refresh
	// EditCat - the edited one must be selected after refresh
	// DeleteCat - the category which bumps up from below should be selected unless the one deleted was last

	NLSFwiz!Step.Function.CatList.reset();

	ini.get.fileName$="nlsf.ini"; // prepare nlsf.ini for editing
	ini.getSection(Category); // get category section
	run.section(,ListCat);
	switch(%1)
	{
	case 1:
		// update category selection after new category
		NLSFwiz!Step.Function.CatList.choice=NLSFwiz!Step.Function.CatList.numItems; // select the new or edited category	
		break;
	case 2:
		// update category selection after edit category
		NLSFwiz!Step.Function.CatList.choice=%2;
		break;
	case 3:
		// update category selection after delete category
		if("%2"==(ini.keynum+1))
			{
				NLSFwiz!Step.Function.CatList.choice=ini.keynum; // last cat was deleted so select last cat again
			}
		else
			{
				NLSFwiz!Step.Function.CatList.choice=%2; // select SelectedCat again
			}
			%A=NLSFwiz!Step.Function.CatList.s1$; // get for use in [UpdateFuncList]
			ini.setstr(%A,Function Group,NLSF Control,nlsf.ini); // set as default category
		break;
	default:
		break;
	};
	return 0;
	

[UpdateFuncList]
	NLSFwiz!Step.Function.FuncList.reset();
	switch(%1)
	{
	case 1:
		// empty function list after new category
		NLSFwiz!Step.Function.Funclist.reset(); // clear function list (since category is new)
		NLSFwiz!Step.pal=2; // disable next button for page 2
		break;
	case 2:
		// select appropriate function after edit category
		run.section(,ListFunc);
		NLSFwiz!Step.Function.FuncList.choice=%2;
		break;
	case 3:
		// select first function in list after delete category
		run.section(,ListFunc);	
		NLSFwiz!Step.Function.FuncList.choice=1;	
		break;
	case 4:
		// select appropriate function in list after delete func
		// if the function deleted is the last function in the list, 
		// select the last function in the list after that
		run.section(,ListFunc);
//		%B=NLSFwiz!Step.Function.CatList.s1$;
//		ini.file$="nlsf.ini";
//		ini.getsection(%B);
		if(%2==ini.keynum)
		{		
			NLSFwiz!Step.Function.FuncList.choice=ini.keynum-1;
		}
		else
		{
			if("%2"==1)
			{
			NLSFwiz!Step.Function.FuncList.choice=1;
			}
			else
			{
			NLSFwiz!Step.Function.FuncList.choice=%2;
			}
		}
		break;
	default:
		break;
	};
	return 0;


[ListCat]
	loop(ii,1,ini.keynum)
	{
		NLSFwiz!Step.Function.CatList.v$(ii)$=ini.keyname$(ii)$; // build category list
	}
	return 0;
	

[ListFunc]
	// have to skip Default Function and SaveOption keys
	// must also eliminate functions that have > 1 indep/dep variable
	NLSFwiz!Step.Function.FuncList.reset();
	%A=NLSFwiz!Step.Function.CatList.s1$;
	ini.file$="nlsf.ini";
	ini.getsection(%A);	
	jj=1; // increment for function list

	if(ini.keynum>0)
	{
		loop(ii,1,ini.keynum)	
		{
			%B=ini.keyname$(ii)$;
			if(("%B"!="Default Function") && ("%B"!="SaveOption"))
			{
				// Begin QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
				nlsf.msgPrompt=0;
				nlsf.func$=%B;
				nlsf.msgPrompt=1;
				// End QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
				if((nlsf.numDepend==1) && (nlsf.numIndep==1))
				{
					NLSFwiz!Step.Function.FuncList.v$(jj)$=%B; 
					jj++;	// bump up function list count
				}			
			}
		}
	}
	else
	{
		NLSFwiz!Step.Pal=2; // disable Next for page 2
	}
	layer -a; // rescales outside graph which messes up for some reason
	return 0;
	

[UpdateFitFunc]
	// if another fitting function is selected after having fit the data
	// the param init needs to run again.
	%A=NLSFwiz!GraphNameHolder$;	
	run.section(,CleanUp,%A); // check for nlsf worksheet and delete if there

	%A=NLSFwiz!Step.Function.FuncList.s1$;
	// Begin : QA 2418 : UPDATE_PREVIEW_FOR_NO_FUNC : 8/6/02 : RJT
	//if(%A!="")
	//{
	// Begin QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
	if(%A=="")
	{
		nlsf.msgPrompt=0;
	}
	nlsf.end(13);
	nlsf.cleanupfitdata();
	NLSFwiz!OPSFunction$="%A"; // assign selected function as the one to save to OPS if user chooses
	ini.setstr(%A,Function,NLSF Control,nlsf.ini); // send selected function to nlsf.ini as default
	NLSFwiz!FitFunc$="%A"; // set Function display on wizard to latest function	
	//nlsf.begin();	// moved to step.fit.oninit section QA 2396
	nlsf.func$="%A"; // set fitting function for nlsf
	nlsf.msgPrompt=1;
	// End QA 2620 : SUPPRESS_FUNCTION_NOT_SELECTED_MESSAGE : RJT : 8/8/02
	//}
	// End : QA 2418 : UPDATE_PREVIEW_FOR_NO_FUNC : 8/6/02 : RJT

	run.section(,ShowPreview); // show eq and pic previews
	run.section(,HideShowPeaksPage); // determine if selected fitting function needs peaks page
	
	// if residuals/conf/pred are on, remove them, and pop up and disable the toolbar buttons
	if(NLSFwiz!Toolbar3.ResidualsButton=0)
		run.section(,Toolbar3.ResidualsButton.OnClick);
	if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
		run.section(,RemoveBands,"LCL");
	if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
		run.section(,RemoveBands,"LPL");
	
	NLSFwiz!Finish.enable=0; // disable Finish
	run.section(,Toolbar3Control,0); // disable toolbar 3		
	return 0;
	
	
[ShowPreview]	
	NLSFwiz!Step.Function.PreviewPic.Update(); 
	NLSFwiz!Step.Function.PreviewEq.Update();
	return 0;


//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Peaks Page Scripts ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

[HideShowPeaksPage]

	// the key here is I test if numreplica is not equal to -1

	if(nlsf.numReplica!=-1)	
	{
		;NLSFwiz!WizardMap.showPeaks=1; // enable peaks page
	}
	else
	{
		;NLSFwiz!WizardMap.showPeaks=0; // disable peaks page
		;NLSFwiz!Page.Peaks.multiPeaks.enable=0; // disable mutliple peaks check box
	}
	return 0;
	

[Step.Peaks.OnInit]
	// initialize peaks group
	NLSFwiz!Step.Peaks.PeakNumSpin.value=1;
	NLSFwiz!Step.Peaks.PeakNumSpin.min=1;
	NLSFwiz!Step.Peaks.PeakNumSpin.max=30; // 4/29/02 : QA 1486 : RJT	
	NLSFwiz!Finish.enable=0;
	
	if(nlsf.numreplica==-1)
	{
		NLSFwiz!Step.Peaks.multiPeaks=0;
		NLSFwiz!Step.Peaks.multiPeaks.enable=0;
	}
	return 0;
	
		
[Step.Peaks.Active]	
	NLSFwiz!graph.create(NLSFWiz);
	NLSFwiz!Step.Peaks.GraphOnPeaks.attach();
	NLSFwiz!Step.Peaks.GraphOnPeaks.SetRef();

	%A="NLSFwiz!Step.Peaks.GraphOnPeaks.graph$";
	NLSFwiz!GraphNameHolder$=%A; // stores property as page marker	
	%B=NLSFwiz!Step.Data.SelectIndep$; // get independent variable dataset name
	%K=NLSFwiz!Step.Data.SelectDep.s1$; // get dependent variable dataset name
		
	%W=NLSFwiz!Step.Peaks.GraphOnPeaks.graph$; 
	win -oa %W
	{
		dotool 0;
		page.active=1;
	}	
	run.section(,SetNLSFFitData,%B %K);
	run.section(,PlotAttributesControl,1);
	
	// need to make the check box for multiple peaks affect this	
	if(exist(_Peak_Y)!=1 && NLSFwiz!Step.Peaks.multiPeaks==1 && NLSFwiz!Step.Peaks.PeakNumSpin.value!=1)
	{
		NLSFwiz!Step.Pal=3; // disable Next button til peaks are selected
	}
	else
	{
		NLSFwiz!Step.Pal=0; // enable Next button if peak dataset exists
	}
	
	NLSFwiz!Toolbar1.EnlargerButton.enable=1;
	run.section(,FinishControl); // enable/disable finish button
	return 0;
	
[Step.Peaks.multiPeaks.onClick]		
	if(NLSFwiz!Step.Peaks.multiPeaks!=1)
	{
		NLSFwiz!Step.Pal=0; // enable next button for page 3
		NLSFwiz!Step.Peaks.PeakNumSpin.value=1; // reset number of peaks to 1
		nlsf.numReplica=0; // reset number of replicas to 0
	}
	return 0;
	
	
[Step.Peaks.PeakNumTextBox.OnEditChange]
	if(NLSFwiz!Step.Peaks.PeakNumSpin.value>1)
	{
		NLSFwiz!Step.Pal=3; // disable next button for page 3
		// need to clear current parameters too if possible
		nlsf.numReplica=NLSFwiz!Step.Peaks.PeakNumSpin.value-1;
	}
	else
	{
		NLSFwiz!Step.Pal=0; // enable next button for page 3
		nlsf.numReplica=0; // set number of replicas to 0
	}
	return 0;
	
	
[Step.Peaks.PeakInit.OnClick]
	numPeaks=NLSFwiz!Step.Peaks.PeakNumSpin.value;
	nlsf.numReplica=numPeaks-1;
	
	// Begin QA 1486 : RJT : 4/29/02 
	if(numPeaks > 30) 
	{
		ty -b $Peaks.PeakErr;
		break 1;
	}
	// END QA 1486 : RJT : 4/29/02
	
	%A=NLSFwiz!GraphNameHolder$;
	win -o %A
	{
		del _Peak_Y;
		create _Peak_Y -X 30; // QA 1486 : RJT : 4/29/02 

		run.section(,PeakParams); 
		// Begin QA #1464 - RJT - 4/15/2002
		def quitToolbox 
		{
			run.section(NLSFwiz.OGS,PeakCenters);
			NLSFwiz!Step.Pal=0; // enable Next button for page 3
		};
		// End QA #1464 - RJT - 4/15/2002

		GetPts -n _Peak_Y NumPeaks [$NLSFwiz.PeakInit];
	}
	return 0;
	
	
[PeakParams]
	numPeaks=NLSFwiz!Step.Peaks.PeakNumSpin.value;
	nlsf.numReplica=numPeaks-1;
	integ -q %C;

	if(integ.dx < 0) 
	{
		integ.dx *=-1;
		integ.area *= -1;
	}
	/* if x is in descending order, we need to turn these around */
	//def QuitFit [del _Peak_Y];

	limit %C;

	x0 = limit.xmin;
	if(abs(x0/(limit.xmax-x0)) < 0.05) 
		x0 = 0;		
	y0 = limit.ymin;
	if(abs(y0/(limit.ymax-y0)) < 0.05)
		y0 = 0;
	
	/* integration was carried out from zero, we need
	 * to remove the offset */
	
	integ.area -= y0*(limit.xmax-limit.xmin);
	integ.area/=NumPeaks;
	integ.dx/=NumPeaks;
	loop(i,1,NumPeaks) 
	{
		if (NumPeaks==1)
		{
			A=integ.area;
			W=integ.dx;
		}
		else
		{
			loop(jj,1,nlsf.npara)
			{
				%A=nlsf.n$(jj)$;
				if(%[%A,1:1]=="A")
					(%A)=integ.area;
				if(%[%A,1:1]=="w")
					(%A)=integ.dx;
			}
		}
	}
	return 0;


[PeakCenters]
	numPeaks=NLSFwiz!Step.Peaks.PeakNumSpin.value;
	nlsf.numReplica=numPeaks-1;
	loop(i,1,NumPeaks) 
		{
		if (NumPeaks==1)
			XC=_Peak_A[i];
		else
			XC$(i)=_Peak_A[i]; 
		}
	return 0;
	
	
[Step.Peaks.ClearPeaks.OnClick]

	del _Peak_Y;
	del _integ_area;
	%A=NLSFwiz!GraphNameHolder$;
	win -o %A
	{
		layer -a;
		doc -uw;
	}
	NLSFwiz!Step.Pal=3; // disable Next button for page 3
	return 0;
	
	
//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Weighting Page Scripts /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////



[Step.PandW.OnInit]
	// initialize weighting group and nlsf properties
	NLSFwiz!Step.PandW.SelectWeightingMethod=1;
	NLSFwiz!Step.PandW.WeightingFormula$="w=1"; // no weighting
	nlsf.w$="";
	nlsf.wtype=0;
	
	// initialize Error Dataset (Err) list  and disable
	NLSFwiz!Step.PandW.SelectWeightingDataset.reset();
	run.section(,ListAllDatasets);
	NLSFwiz!Step.PandW.SelectWeightingDataset.enable=0;	
	NLSFwiz!Finish.enable=0; // disable Finish
	return 0;
	

[Step.PandW.Active]	
	// if this if statement isn't first, it won't execute
	run.section(,FinishControl); // enable/disable finish button
	
	NLSFwiz!graph.create(NLSFWiz);
	NLSFwiz!Step.PandW.GraphOnWeighting.attach();
	NLSFwiz!Step.PandW.GraphOnWeighting.SetRef();
	
	%A="NLSFwiz!Step.PandW.GraphOnWeighting.graph$";
	NLSFwiz!GraphNameHolder$=%A;  // stores property as page marker		
	%B=NLSFwiz!Step.Data.SelectIndep$;
	%K=NLSFwiz!Step.Data.SelectDep.s1$;
	
	%W=NLSFwiz!Step.PandW.GraphOnWeighting.graph$;	
	win -oa %W
	{
		dotool 0;
		page.active=1;
	}
	run.section(,SetNLSFFitData,%B %K);
	run.section(,PlotAttributesControl,1);
	NLSFwiz!Toolbar1.EnlargerButton.enable=1; // enable enlarger button
	NLSFwiz!Step.Pal=0; // enable next button for page 4
	
	// Begin QA 2644 : FORCE_CORRECT_WEIGHTING_DATASET_SELECTION : RJT : 8/08/02
	run.section(,Step.PandW.SelectWeightingMethod.OnSelChange);
	// End QA 2644 : FORCE_CORRECT_WEIGHTING_DATASET_SELECTION : RJT : 8/08/02

	return 0;
	
	
[Step.PandW.SelectWeightingMethod.OnSelChange]	
	run.section(,RemovePlottedError);
	NLSFwiz!Step.PandW.SelectWeightingDataset.reset();
	choice=NLSFwiz!Step.PandW.SelectWeightingMethod.Choice;
	switch(choice)
	{
	case 1:
		run.section(,ListAllDatasets);
		NLSFwiz!Step.PandW.WeightingFormula$="w=1"; // no weighting
		NLSFwiz!Step.PandW.SelectWeightingDataset.enable=0;
		nlsf.w$="";
		nlsf.wtype=0;
		break;
	case 2:
		%T=$NLSFwiz.Dataset;
		NLSFwiz!Step.PandW.WeightingFormula$="w=1/(%T)^2"; // arbitrary weighting
		run.section(,ListAllDatasets);
		NLSFwiz!Step.PandW.SelectWeightingDataset.enable=1;
		NLSFwiz!Step.PandW.SelectWeightingDataset=1;
		%B=NLSFwiz!Step.PandW.SelectWeightingDataset.s1$;
		nlsf.wtype=3;
		nlsf.w$=%B;
		break;
	case 3:
		%T=$NLSFwiz.Dataset;
		NLSFwiz!Step.PandW.WeightingFormula$="w=(%T)"; // direct weighting
		run.section(,ListAllDatasets);
		NLSFwiz!Step.PandW.SelectWeightingDataset.enable=1;
		NLSFwiz!Step.PandW.SelectWeightingDataset=1;
		%B=NLSFwiz!Step.PandW.SelectWeightingDataset.s1$;
		nlsf.wtype=4;
		nlsf.w$=%B;
		break;
	case 4:
		run.section(,CheckForErrorDatasets);
		if(numErrSets<=0)
		{
			type -b $NLSFwiz.Weighting1;
			NLSFwiz!Step.PandW.SelectWeightingMethod=1;
			NLSFwiz!Step.PandW.WeightingFormula$="w=1"; // no weighting
			NLSFwiz!Step.PandW.SelectWeightingDataset.enable=0;
			nlsf.w$="";
			nlsf.wtype=0;
		}
		else
		{
			%T=$NLSFwiz.Sigma;	
			NLSFwiz!Step.PandW.WeightingFormula$="w=1/(%T)^2"; // instrumental weighting	
			%B=NLSFwiz!Step.Data.SelectDep.s1$; // fit data name	
			%K=%[%B,"_"]; // get worksheet name 
			
			// Begin: RJT 2/22/02 NLSFWIZ_INSTRUMENTAL_WEIGHTING_FIX for QA917
			NLSFwiz!Step.PandW.SelectWeightingDataset.enable=1;
			run.section(,ListErrorDatasets,%K); // list available Yerr in %K worksheet
			%A=errof(%B); // get associated error dataset
			if("%A"=="")
			{
				start=colnum(%B); // locate starting point by getting fit data column number in wks
				win -o %K
				{
					// look for err dataset closest to right of dep variable
					wks.cNameMode=128; // set mode to full dataset name
					if(start<$(%K!wks.ncols))
					{
						loop(ii,(start+1),%K!wks.ncols)
						{
							if(%K!wks.col$(ii).type==3)
							{
								%A=wks.cName$(ii)$;
								break;
							}
						}
					}
					// look for err dataset closest to the left of dep var if none found on right
					if(start>1)
					{
						if("%A"=="")
						{
							for(ii=(start-1);ii>0;ii--)
							{
								if(%K!wks.col$(ii).type==3)
								{
									%A=wks.cName$(ii)$;
									break;
								}
							}
						}
					}
		
				}
			}
			// End: RJT 2/22/02 NLSFWIZ_INSTRUMENTAL_WEIGHTING_FIX for QA917
			loop(ii,1,NLSFwiz!Step.PandW.SelectWeightingDataset.numItems)
			{
				%M=NLSFwiz!Step.PandW.SelectWeightingDataset.v$(ii)$;
				if("%A"=="%M")
					break;
			}
			NLSFwiz!Step.PandW.SelectWeightingDataset.choice=ii;
			nlsf.wType=1;
			nlsf.w$=%A;

			%W=NLSFwiz!Step.PandW.GraphOnWeighting.graph$;
			win -oa %W
				{
					layer -i %A;  // plot error dataset 
					set %A -or %C; // associate it with fit data
					layer -a; // rescale
				}
		}
		break;
	case 5:
		run.section(,ListAllDatasets);
		%W=NLSFwiz!Step.PandW.GraphOnWeighting.graph$;
		win -oa %W
		{		
			loop(ii,1,NLSFwiz!Step.PandW.SelectWeightingDataset.numitems)
			{
				if("%C"==NLSFwiz!Step.PandW.SelectWeightingDataset.v$(ii)$)
					break; // determines at what point active dataset occurs in weighting dataset list
			}
		}
		NLSFwiz!Step.PandW.SelectWeightingDataset.choice=ii;
		%T=$NLSFwiz.DepVar;
		NLSFwiz!Step.PandW.WeightingFormula$="w=1/(%T)"; // statistical weighting
		NLSFwiz!Step.PandW.SelectWeightingDataset.enable=0;
		nlsf.wtype=2;
		break;		
	default:
		break;
	};
	return 0;
	

[ListAllDatasets]
	// list all datasets except those that begin with _ or don't have an _
	numDatasets=0;
	doc -e S
	{
		%L=%[%C,'_']; // get text to left of _
		%M=%[%C,>'_']; // get text to right of _
		%N=%[%C,5];
		length1=%[%L]; // get length of text to left of _
		length2=%[%M]; // get length of text to right of _
		// if lengths exist then list the dataset
		if(length1>0 && length2>0 && "%N"!="NLSF")
		{
			numDatasets+=1;
			NLSFwiz!Step.PandW.SelectWeightingDataset.v$(numDatasets)$=%C; // add all datasets to list
		}
	}
	return 0;
	

[CheckForErrorDatasets]
	numErrSets=0;
	doc -e W
	{
		doc -e D
		{
			if(%H!wks.col$(colnum(%C)).type==3)
			{
				numErrSets+=1;
			}
		}
	}
	return 0;


[ListErrorDatasets]
	count=0;		
	win -o %1 {
			doc -e D
			{
				if(%1!wks.col$(colnum(%C)).type==3)
				{
					count++;
					NLSFwiz!Step.PandW.SelectWeightingDataset.v$(count)$="%C"; // add error columns to list
				}
			}
	}
	return 0;
	

[Step.PandW.SelectWeightingDataset.OnSelChange]	
	%A=NLSFwiz!Step.PandW.SelectWeightingDataset.s1$; // get weighting dataset
	choice=NLSFwiz!Step.PandW.SelectWeightingMethod.Choice; // get index value of weighting method
	run.section(,RemovePlottedError); // remove any error dataset if plotted
	if(choice==4)
	{
		%W=NLSFwiz!Step.PandW.GraphOnWeighting.graph$;
		win -oa %W
			{
				layer -i %A;
				set %A -or %C;
				nlsf.wType=1;
				layer -a;
				doc -uw; 
			}
	}
	nlsf.w$=%A; // set weighting dataset
	return 0;


[RemovePlottedError]
	%W=NLSFwiz!Step.PandW.GraphOnWeighting.graph$;
	win -oa %W
	{
		layer -o 1
		{
			doc -e D 
			{
				%K=%[%C,"_"];
				%L=%C;
				if(%K!wks.col$(colnum(%C)).type==3)
				{
					layer -e %L;
					layer -a;
					doc -uw;
				}
			}	
		}
	}
	return 0;
	

//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Fit Page Scripts ////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

[Step.Fit.OnInit]
	NLSFwiz!graph.create(NLSFWiz);
	NLSFwiz!Step.Fit.GraphOnFit.attach();
	NLSFwiz!Step.Fit.GraphOnFit.SetRef();

	%A="NLSFwiz!Step.Fit.GraphOnFit.graph$";
	NLSFwiz!GraphNameHolder$=%A; // stores property as page marker

	%B=NLSFwiz!Step.Data.SelectIndep$;
	%K=NLSFwiz!Step.Data.SelectDep.s1$;
	run.section(NLSFWiz,SetNLSFFitData,%B %K);
	run.section(,PlotAttributesControl,1);
	
	%W=NLSFwiz!Step.Fit.GraphOnFit.graph$;
	win -oa %W
		{
			dotool 0;
			page.active=1;
			// begin QA 2396 : RJT : 7/22/02
			nlsf.begin();
			// end QA 2396 : RJT : 7/22/02			
			if(nlsf.niter<=0)
			{
				if(exist(_Peak_Y)==1)
				{
					nlsf.compileparainit=0; // disable parameter init
					nlsf.parainit=0; // disable parameter init
				}
				else
				{
					nlsf.compileparainit=1; // enable parameter init
					nlsf.parainit=1; // enable parameter init
				}
				// begin QA 2593 : RJT : 8/5/02 : CORRECT_ARBITRARY_WEIGHTING_DATASET
				//run.section(,Step.PandW.SelectWeightingMethod.OnSelChange); // set weighting method
				// end QA 2593 : RJT : 8/5/02 : CORRECT_ARBITRARY_WEIGHTING_DATASET
				nlsf.iterateEX(1000);
				NLSFwiz!Step.Fit.ChiSqrTextBox.get(Z);
				%T=$NLSFwiz.ChiSqr1;
				%U=$NLSFwiz.ChiSqr2;
				%B=$NLSFWiz.LM;
				%Z=%Z
%B
%T
%U;
				NLSFwiz!Step.Fit.ChiSqrTextBox.set(Z);
			}
		}

	nlsf.updateparams();
	return 0;
	
[Step.Fit.Active]
	NLSFwiz!graph.create(NLSFWiz);
	NLSFwiz!Step.Fit.GraphOnFit.attach();
	NLSFwiz!Step.Fit.GraphOnFit.SetRef();
	%A="NLSFwiz!Step.Fit.GraphOnFit.graph$";
	NLSFwiz!GraphNameHolder$=%A; // stores property as page marker

	run.section(,FinishNextControl,4); // enable/disable Finish and Next
	run.section(,Toolbar3Control,1); // enable Toolbar 3 buttons
		
	// initialize the iterations list
	ini.getstr(A,,Iteration,NLSF Control,NLSF.ini); // get default # of iterations from nlsf.ini
	iterations="%A"; // set variable to that value
	loop(ii,1,NLSFwiz!Step.Fit.IterationsList.numitems)
	{
		if(iterations==NLSFwiz!Step.Fit.IterationsList.v$(ii))
			break; // determines at what point "iterations" occurs in iterations list
	}
	NLSFwiz!Step.Fit.IterationsList.choice=ii; // init iterations list to default in nlsf.ini
	run.section(,Step.Fit.IterationsList.OnSelChange); // initialize iteration button and parameter dialog
	ini.getstr(A,,tolerance,NLSF Control,NLSF.ini); // initialize tolerance from NLSFwizard.ini
	NLSFwiz!Step.Fit.ToleranceTextBox$="%A";

	// initialize confidence and prediction band settings' controls
	ini.file$="NLSFwizard.ini";
	NLSFwiz!Step.Fit.ConfLevelTextBox$=ini.SystemVariables.ConfLevel$;
	NLSFwiz!Step.Fit.ConfLevelSpin.min=1;
	NLSFwiz!Step.Fit.ConfLevelSpin.max=99;
	NLSFwiz!Step.Fit.ConfLevelSpin.value=95;

	NLSFwiz!Step.Fit.PredLevelTextBox$=ini.SystemVariables.PredLevel$;
	NLSFwiz!Step.Fit.PredLevelSpin.min=1;
	NLSFwiz!Step.Fit.PredLevelSpin.max=99;
	NLSFwiz!Step.Fit.PredLevelSpin.value=95;	
	return 0;
	
	
[Step.Fit.ConfLevelTextBox.OnEditchange]
	conf=NLSFwiz!Step.Fit.ConfLevelSpin.value;
	ini.setstr($(conf),ConfLevel,SystemVariables,NLSFwizard.ini);
	
	// if the confidence bands are present in the graph, update them?
	if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
	{
		NLSFwiz!Toolbar3.ConfidenceBandsButton=1;
		run.section(,RemoveBands,"LCL");
		run.section(,Toolbar3.ConfidenceBandsButton.OnClick);
	}
	return 0;
	
	
[Step.Fit.PredLevelTextBox.OnEditchange]
	pred=NLSFwiz!Step.Fit.PredLevelSpin.value;
	ini.setstr($(pred),PredLevel,SystemVariables,NLSFwizard.ini);
	
	// if the prediction bands are present in the graph, update them
	if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
	{
		NLSFwiz!Toolbar3.PredictionBandsButton=1;
		run.section(,RemoveBands,"LPL");
		run.section(,Toolbar3.PredictionBandsButton.OnClick);
	}
	return 0;
		

[Step.Fit.IterationsList.OnSelChange]	
	%A=NLSFwiz!Step.Fit.IterationsList.s1$; // get new iterations number
	NLSFwiz!Step.Fit.IterateButton$=$NLSFwiz.IterateButton; // from origin.omg
	nlsf.maxIter=$(%A); // sets iteration droplist in Parameters Display
	nlsf.updateparams(); // update Parameters Display
	ini.setstr(%A,Iteration,NLSF Control,NLSF.ini); // sets iteration value in nlsf.ini
	return 0;
	
	
[Step.Fit.ToleranceTextBox.OnEditChange]	
	return 0;
	

[Step.Fit.ToleranceTextBox.OnKillFocus]	
	%A=NLSFwiz!Step.Fit.ToleranceTextBox$;
	ini.setstr(A,tolerance,NLSF Control,NLSF.ini); // update tolerance in nlsf.ini
	return 0;
	
	
[Step.Fit.ToleranceTextBox.OnPressEnter]	
	run.section(,Step.Fit.ToleranceTextBox.OnKillFocus); // update tolerance in nlsf.ini
	NLSFwiz!Step.Fit.IterateButton.setfocus(); // set focus allow immediate iterations
	return 0;
		

[Step.Fit.ChiSqButton.OnClick]
	%W=NLSFwiz!Step.Fit.GraphOnFit.graph$;
	win -oa %W
		{
			NLSFwiz!Step.Fit.ChiSqrTextBox$=""; // reset Iteration Results box
			nlsf.iterateEX(0); // put fit on graph
			NLSFwiz!Step.Fit.ChiSqrTextBox.v1$=$NLSFwiz.ChiSqr1; // set Iteration Results text
			run.section(,FinishNextControl,4); // enable/disable Finish and Next
		}
	nlsf.updateparams();
	return 0;
	

[Step.Fit.IterateButton.OnClick]
	%W=NLSFwiz!Step.Fit.GraphOnFit.graph$;
	%K=NLSFwiz!Step.Fit.IterationsList.s1$;
	nlsf.maxIter="%K";
	
	win -oa %W
	{
		page.active=1;
		nlsf.tolerance=NLSFwiz!Step.Fit.ToleranceTextBox.v1;
		nlsf.updateChiSqr(---------------);
		nlsf.output(%W);
		nlsf.iterateEX(nlsf.maxIter);
		nlsf.output();
		NLSFwiz!Step.Fit.ChiSqrTextBox.get(Z);
		%T=$NLSFwiz.ChiSqr1;
		%U=$NLSFwiz.ChiSqr2;
		%B=$NLSFWiz.LM;
		%Z=%Z
%B
%T
%U;

		NLSFwiz!Step.Fit.ChiSqrTextBox.set(Z);
		// recreate bands if either is enabled
		if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
		{
			run.section(,RemoveBands,"LCL");
			NLSFwiz!Toolbar3.ConfidenceBandsButton=1;
			run.section(,Toolbar3.ConfidenceBandsButton.OnClick);
		}
		if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
		{
			run.section(,RemoveBands,"LPL");
			NLSFwiz!Toolbar3.PredictionBandsButton=1;
			run.section(,Toolbar3.PredictionBandsButton.OnClick);
		}
		// GJL QA2538 8/2/02 REPLOT_RESIDUALS_ON_ITERATE
		// update residuals if enabled
		if(NLSFwiz!Toolbar3.ResidualsButton==0)
		{
			layer -o 2
			{
				layer -e %C; // remove old residuals
				nlsf.output(%W);
				nlsf.residuals(); // plot new residuals
				nlsf.output();
				layer -a; // rescale layers
			}
			run.section(,UpdateResidualsStyle,%W);
		}
		// End GJL QA2538 8/2/02 REPLOT_RESIDUALS_ON_ITERATE
	}
	run.section(,Toolbar3Control,1); // enable toolbar3 buttons
	run.section(,FinishNextControl,4); // enable/disable Finish and Next		
	nlsf.updateparams(); // update Parameters Display
	return 0;
	
	
[Step.Fit.ResetFit.OnClick]
	%W=NLSFwiz!Step.Fit.GraphOnFit.graph$; // get graph name
	NLSFwiz!Step.Fit.ChiSqrTextBox$=""; // clear Iteration Results window
	nlsf.updateChiSqr( ); // clear window in Parameters Display
	
	win -oa %W
	{
		// reset parameters - use auto init unless user has defined peaks manually
		if(exist(_Peak_Y)==1)
		{
			run.section(,PeakParams);
			run.section(,PeakCenters);
			nlsf.compileparainit=0;				
			nlsf.parainit=0;
		}
		else
		{
			nlsf.compileparainit=1;
			nlsf.parainit=1;
		}
		nlsf.iterateEX(0); // put curve on graph
	}
		
	// disable conf, pred, and residual buttons
	run.section(,Toolbar3Control,0); // enable toolbar 3 buttons
	NLSFwiz!Toolbar3.ParametersDisplayButton.enable=1; // reenable PD button
	run.section(,FinishNextControl,4); // enable/disable Finish and Next	
	return 0;
	
			

//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Parameters Dialog Box Scripts ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

[OnIterate]
	%W=NLSFwiz!GraphNameHolder$;
	win -oa %W
	{
		page.active=1;
		nlsf.output(%W);
//		nlsf.iterateEX(0);
		nlsf.output();
		NLSFwiz!Step.Fit.ChiSqrTextBox.get(Z);
		%T=$NLSFwiz.ChiSqr1;
		%U=$NLSFwiz.ChiSqr2;
		%B=$NLSFWiz.LM;
		%Z=%Z
%B
%T
%U;
		NLSFwiz!Step.Fit.ChiSqrTextBox.set(Z);
		
		if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
		{
			run.section(,RemoveBands,"LCL");
			NLSFwiz!Toolbar3.ConfidenceBandsButton=1;
			run.section(,Toolbar3.ConfidenceBandsButton.OnClick);
		}
		if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
		{
			run.section(,RemoveBands,"LPL");
			NLSFwiz!Toolbar3.PredictionBandsButton=1;
			run.section(,Toolbar3.PredictionBandsButton.OnClick);
		}
		// GJL QA2538 8/2/02 REPLOT_RESIDUALS_ON_ITERATE
		// update residuals if enabled
		if(NLSFwiz!Toolbar3.ResidualsButton==0)
		{
			layer -o 2
			{
				layer -c; // get list of data plots in layer
				layer -e %Z; // remove all data plots in layer (old residuals and sometimes yfit QA2587)
				nlsf.output(%W);
				nlsf.residuals(); // plot new residuals
				nlsf.output();
				layer -a; // rescale layers
			}
			run.section(,UpdateResidualsStyle,%W);
		}
		// End GJL QA2538 8/2/02 REPLOT_RESIDUALS_ON_ITERATE
	}
	run.section(,Toolbar3Control,1); // enable toolbar 3
	run.section(,FinishNextControl,4); // enable/disable Finish and Next
	return 0;
	

[OnMaxIterChange]
	NLSFwiz!Step.Fit.IterationsList.s1$=$(nlsf.maxIter);
	run.section(,Step.Fit.IterationsList.OnSelChange);
	return 0;
	

[OnStatus]
	if("%1"!="NULL")
	{
		%A=%1; // set %A equal to status bar message(s) being held in %1 from type -qo
		nlsf.updatechisqr(%A);
		NLSFwiz!Step.Fit.ChiSqrTextBox.get(Z); // get text already in chi-sqr text box
		%Z=%Z
%A; // add %A to %Z text
		length=%[%Z]; 			
		if(length>6000)
		{
			%Z=""; // don't let %Z get too big and cause crash
		}
		NLSFwiz!Step.Fit.ChiSqrTextBox.set(Z); // set chi-sqr text box to new %Z
	}
	return 0;
	

[OnDestroyDlg]
	NLSFwiz!Toolbar3.ParametersDisplayButton=1;
	return 0;
	
 	

//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// Results Page Scripts //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

[Step.Results.OnInit]	
	ini.file$="NLSFwizard.ini";
	%A=NLSFwiz!ActiveWindowTitle$; // get name of active window when wizard was launched
	 
	NLSFwiz!Step.Results.CurveOutputList.v1$=$NLSFwiz.Output1;
	NLSFwiz!Step.Results.CurveOutputList.v2$=$NLSFwiz.Output2;	
	if(exist(%A)==3)
		{
			NLSFwiz!Step.Results.CurveOutputList.v3$=$NLSFwiz.Output3;
		}
	// if CurevOutputList set to 3 (active graph) 
	// and then launch from wks next time perform fit
	// select New Graph for curveOutput
	if(exist(%A)==2 && (ini.SystemVariables.OutputCurve==3))
		NLSFwiz!Step.Results.CurveOutputList=2;
	else
		NLSFwiz!Step.Results.CurveOutputList=$(ini.SystemVariables.OutputCurve);

	NLSFwiz!Step.Results.ResidualsOutputList.v1$=$NLSFwiz.Output1;
	NLSFwiz!Step.Results.ResidualsOutputList.v2$=$NLSFwiz.Output2;
	NLSFwiz!Step.Results.ResidualsOutputList.v3$=$NLSFwiz.Output4;
	NLSFwiz!Step.Results.ResidualsOutputList=$(ini.SystemVariables.OutputResiduals);

	NLSFwiz!Step.Results.ConfBandCheckBox=$(ini.SystemVariables.IncludeConf);
	NLSFwiz!Step.Results.PredBandCheckBox=$(ini.SystemVariables.IncludePred);
	NLSFwiz!Step.Results.PasteToPlotCheckBox=$(ini.SystemVariables.IncludeResults);

	NLSFwiz!Step.Results.ResultsLogCheckBox=$(ini.SystemVariables.OutputRLog);
	NLSFwiz!Step.Results.ResultsWorksheet=$(ini.SystemVariables.OutputParamWks);
	%B=ini.SystemVariables.ParamWksName$;
	NLSFwiz!Step.Results.ResultsWorksheetName$="%B";
	return 0;
	
	
[Step.Results.Active]	
	NLSFwiz!graph.create(NLSFWiz);
	NLSFwiz!Step.Results.GraphOnResults.attach();
	NLSFwiz!Step.Results.GraphOnResults.SetRef();
	
	%A="NLSFwiz!Step.Results.GraphOnResults.graph$";
	NLSFwiz!GraphNameHolder$=%A; // stores property as page marker
	%B=NLSFwiz!Step.Data.SelectIndep$;
	%K=NLSFwiz!Step.Data.SelectDep.s1$;
	
	%W=NLSFwiz!Step.Results.GraphOnResults.graph$;		
	win -oa %W
	{
		dotool 0;
		page.active=1;
		run.section(,PlotAttributesControl,1);
	}
	
	run.section(,FinishControl); // enable/disable finish button
	//NLSFwiz!Toolbar3.ParametersDisplayButton.enable=0; // disable the PD button	
	return 0;
	
	
[Step.Results.PasteToPlotCheckBox.OnClick]
	if(NLSFwiz!Step.Results.PasteToPlotCheckBox)
		nlsf.pastetoplot=1;
	else
		nlsf.pastetoplot=0;
	return 0;
	

[Step.Results.ResultsLogCheckBox.OnClick]
	/*if(NLSFwiz!Step.Results.ResultsLogCheckBox)
		type -b "// need to output to results log";
		
	else
		type -b "// do not output to results log";*/
		

[Step.Results.CreateGResults.OnClick]
	curveChoice=NLSFwiz!Step.Results.CurveOutputList.choice; // 1 or 2 (wks), 1, 2, 3 (graph)
	residualsChoice=NLSFwiz!Step.Results.ResidualsOutputList.choice; // 1, 2, 3
	confChoice=NLSFwiz!Step.Results.ConfBandCheckBox; // 0 or 1
	predChoice=NLSFwiz!Step.Results.PredBandCheckBox; // 0 or 1
	resultsChoice=NLSFwiz!Step.Results.PasteToPlotCheckBox; // 0 or 1 (depends on curveOutput)
	run.section(,SaveGRSettings,$(curveChoice) $(residualsChoice) $(confChoice) $(predChoice) $(resultsChoice));
	
	// controls fit curve output and pred/conf output
	switch(curveChoice)
	{
	case 1: // no curve requested
		break;
	case 2: // curve in new graph requested
		run.section(,CurveOutput,2);
		if(confChoice==1)
			run.section(,ConfOutput,2);
		if(predChoice==1)
			run.section(,PredOutput,2);
		break; 
	case 3: // curve in active graph requested
		run.section(,CurveOutput,3);
		if(confChoice==1)
			run.section(,ConfOutput,3);
		if(predChoice==1)
			run.section(,PredOutput,3);
		break; 
	default:
		break;
	};	
	
	// controls fit label output
	if(resultsChoice==1)
		run.section(,ResultsOutput,$(curveChoice));
	
	// controls residuals output
	switch(residualsChoice)
	{
	case 1: // no residuals requested
		break;
	case 2: // residuals in new graph requested
		%B="Residuals";
		for(ii=1;1==1;ii++) 
		{
			if(exist(%B$(ii))!=3)
				break;
		};	
		win -t plot NLSFWizResiduals %B$(ii); // open NLSFwiz.otp - name using NLSFwiz$(ii)
		
		run.section(,WindowLabel,%B$(ii)); // create residuals window label
		NLSFwiz!LatestResidualsGraph$=%B$(ii);
		win -o %B$(ii)
		{
			nlsf.residuals(); // creates residuals as hidden column in fitdata's worksheet
			run.section(,AxisLabels,%B$(ii) %C);
			run.section(,UpdateResidualsStyle,%B$(ii));
			layer1.x.type=NLSFwiz!ScaleType.Choice; // set scale to same as graph in wizard
			layer -a; // rescale residuals layer
		}
		
		break;
	case 3: // residuals in same graph as curve requested
		// if curveOutput=1 do not create residuals
		// if curveOutput=2 send residuals to new graph different layer
		// if curveOutput=3 send residuals to active graph (layer To Be Determined)
		if(curveChoice!=1)
			run.section(,ResidualsOutput,$(curveChoice));
		break; 
	default:
		break;
	};
	return 0;
	
	
[SaveGRSettings]	
	// %1=curveChoice
	// %2=residualsChoice 
	// %3=confChoice 
	// %4=predChoice
	// %5=resultsChoice
	
	ini.setstr(%1,OutputCurve,SystemVariables,NLSFwizard.ini);
	ini.setstr(%2,OutputResiduals,SystemVariables,NLSFwizard.ini);
	ini.setstr(%3,IncludeConf,SystemVariables,NLSFwizard.ini);
	ini.setstr(%4,IncludePred,SystemVariables,NLSFwizard.ini);
	ini.setstr(%5,IncludeResults,SystemVariables,NLSFwizard.ini);
	return 0;	
	
	
[WindowLabel]	
	%K=nlsf.func$;
	%W=nlsf.fitdata$;
	%1!page.title=3;
	%1!page.label$=$NLSFwiz.WindowLabel; // uses %K and %W
	return 0;
	
	
[AxisLabels]	

	%1!yl.text$="%2";
	%1!yl.fSize=24;
	%Z=xof(%2);
	%1!xb.text$="%Z";
	%1!xb.fSize=24;
	return 0;
	
	
[CurveOutput]	
	%W=NLSFwiz!GraphNameHolder$;
	win -oa %W
	{
		page.active=1;
		%A=%C;
		get %C -b bgrow; // get beginning of wizard display range
		get %C -e edrow; // get end of wizard display range
	}
	
	switch(%1)
	{
	case 2:
		
		// create new graph 
		%B="NLSFwiz";
		for(ii=1;1==1;ii++) 
		{
			if(exist(%B$(ii))!=3)
				break;
		};
		%B=%B$(ii);
		win -t plot NLSFwizResults %B; // open NLSFwiz.otp - name using NLSFwiz$(ii)		
		page.icons=1; // show layer icons
		run.section(,WindowLabel,%B); // create window label for new graph window
		NLSFwiz!LatestResultsGraph$=%B; // store name of this new graph
		// add original data
		win -oa %B
		{
			run.section(,AxisLabels,%B %A);
			legend;
		
			// BEGIN 8/5/02 : RJT : QA 2565 : REMEMBER_PLOT_STYLE_DURING_OUTPUT
			if(NLSFwiz!Scatter==1)
			{
				if(NLSFwiz!Line==1)
					layer -i202 %A;
				else
					layer -i201 %A;

				%M=NLSFwiz!SymbSizeDropDown.s1$; // get symbol size from wizard
				set %A -z %M; // set original dataset symbol size
				set %A -kf 1; // set interior to Open
			}
			else
			{
				layer -i200 %A; // add original data as line + symbol
				set %A -z 0; // set symbol size to zero
			}
			if(NLSFwiz!Line==1)
			{
				%M=NLSFwiz!ConnectDropDown.choice; // get line connect type from wizard
				run.section(,SetLineConnection,"%M"); // set line connection 
			}
			else
			{
				run.section(,SetLineConnection,1); // set line connect to none
			}
			// END 8/5/02 : RJT : QA 2565 : REMEMBER_PLOT_STYLE_DURING_OUTPUT
			
			layer1.x.type=NLSFwiz!ScaleType.Choice; // set scale to same as graph in wizard
			set %A -c 20; // set color to gray
			set %A -b $(bgrow); // set begin to that in wizard
			set %A -e $(edrow); // set end to that in wizard
			
			// Begin 8/05/02 : RJT : QA 2564 : INCLUDE_WEIGHTING_DATA_DURING_OUTPUT
			run.section(,WeightingOutput); // add instrumental weighting dataset if needed		
			// End 8/05/02 : RJT : QA 2564 : INCLUDE_WEIGHTING_DATA_DURING_OUTPUT
			
			layer -a; // rescale the graph
			doc -uw; // refresh

			// add fit curve
			nlsf.output(%B);
			nlsf.end(12);
			nlsf.output();
		}
		// reset variables
		bgrow=-1;	
		edrow=-1;		
		break;
	case 3:
		// activate original graph and create fit curve there
		%K=NLSFwiz!ActiveWindowTitle$;
		%L=NLSFwiz!ActiveLayerNumber.v1$;
		win -o %K
		{
			// Begin 8/05/02 : RJT : QA 2564 : INCLUDE_WEIGHTING_DATA_DURING_OUTPUT
			run.section(,WeightingOutput); // add instrumental weighting dataset if needed		
			// End 8/05/02 : RJT : QA 2564 : INCLUDE_WEIGHTING_DATA_DURING_OUTPUT
			page.active=(%L);
			nlsf.output(%K);			
			//---- SDB 5/16/2003 QA70-4484 OUTPUT_CURVE_ACTIVE_GRAPH
			// CPY: this was commented out before, need to ask SDB why, he just uncommented it to fix 4484
			// SDB: inadvertently commented during cleanup of code 
			nlsf.end(12);
			//---- end 	OUTPUT_CURVE_ACTIVE_GRAPH
			nlsf.output();
		}
		break;
	default:
		break;
	}
	return 0;
	

[WeightingOutput]
	// Begin 8/05/02 : RJT : QA 2564 : INCLUDE_WEIGHTING_DATA_DURING_OUTPUT					
	if(nlsf.wType==1)
	{
		%A=NLSFwiz!Step.PandW.SelectWeightingDataset.s1$; // get name of error dataset 
		layer -i %A;  // plot error dataset 
		set %A -or %C; // associate it with fit data
	}	
	return 0;
	// End 8/05/02 : RJT : QA 2564 : INCLUDE_WEIGHTING_DATA_DURING_OUTPUT	
	
[ResidualsOutput]
	// the curve has already been created in its requested graph so that 
	// graph is active. The residuals can therefore be created without 
	// any graph name test...I think.
	
	// active graph may have other layers in it so this needs to be smart
	// if there is one layer, create a new one and put residuals in it
	// if there are >1 layers, add residuals to layer with fit data and fit curve

	var=%1; // %1 is 2 if fit curve is going to new graph, 3 for active graph
	if(var==2)
	{
		%A=NLSFwiz!LatestResultsGraph$;
		win -o %A
		{
			nlsf.output(%A);
			run.section(,CreateResidualsLayer,%A); // put residuals in new layer (2) of new graph
			run.section(,UpdateResidualsStyle,%A);
			nlsf.output();
		}
	}
	if(var==3)
	{
		%A=NLSFwiz!ActiveWindowTitle$;
		%B=NLSFwiz!ActiveLayerNumber$;
		win -oa %A
		{
			if(page.nLayers>1)	
			{		
				page.active=$(%B);
				nlsf.output(%A);
				nlsf.residuals(); // plot residuals in active layer instead of creating a new layer
				nlsf.output();
				layer -a;
			}		
			else
			{	
				layer -o 1 {layer 75 50 10 10;};
				layer -n; // new layer
				layer -o 2 {
					layer 75 20 10 70;
					layer2.x.type=layer1.x.type;
					layer2.x.from=layer1.x.from;
					layer2.x.to=layer1.x.to;
					layer2.link=1; // link 2 to 1
					layer2.x.link=1; // set x axis link as 1to1
					layer2.x.inc=layer1.x.inc;
					layer2.x.atZero=1; // display line at y=0	
					nlsf.output(%A);
					nlsf.residuals(); // plot residuals
					nlsf.output();
					layer -a; // rescale layers
					
				};
			}
			run.section(,UpdateResidualsStyle,%A);
		}
	}

	return 0;
	
	
[ConfOutput]
	if(%1==2)
		%T=NLSFwiz!LatestResultsGraph$; // get name of newly created results graph
	if(%1==3)
		%T=NLSFwiz!ActiveWindowTitle$; // get name of active graph prior to wizard launch
	if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
	{
		%W=NLSFwiz!GraphNameHolder$;
		win -oa %W
		{
			page.active=1;
			doc -e D
			{
				%A=%[%C,>"_"];
				%B="LCL";
				if("%A"=="%B")
				{
					%K=%C; // store LCL dataset name
					%L="%[%C,"_"]_UCL";
				}
			}
		}
		win -o %T
		{
			layer -i200 %K; // include LCL into results graph as line plot
			layer -i200 %L; // include UCL into results graph as line plot
			set %K -c 4; // set LCL to blue
			set %L -c 4; // set UCL to blue
			layer -a;
		}
	}
	else
	{
		win -o %T
		{
			perc=NLSFwiz!Step.Fit.ConfLevelSpin.value;
			nlsf.conf=perc/100;
			nlsf.compileparainit=0;
			nlsf.parainit=0;
			nlsf.iterateex(0);
			nlsf.output(%T);
			nlsf.createCurves(C);
			nlsf.output();
			layer -e yfit;
			layer -a;
		}
	}
	return 0;
	
	
[PredOutput]
	if(%1==2)
		%T=NLSFwiz!LatestResultsGraph$; // get name of newly created results graph
	if(%1==3)
		%T=NLSFwiz!ActiveWindowTitle$; // get name of active graph prior to wizard launch
		
	if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
	{
		%W=NLSFwiz!GraphNameHolder$;
		win -oa %W
		{
			doc -e D
			{
				%A=%[%C,>"_"];
				%B="LPL";
				if("%A"=="%B")
				{
					%K=%C;
					%L="%[%C,"_"]_UPL";
				}
			}
		}
		win -o %T
		{
			layer -i200 %K; // include LPL into results graph
			layer -i200 %L; // include UPL into results graph
			set %K -c 5; // set LPL to cyan
			set %L -c 5; // set UPL to cyan
			layer -a;
		}
	}
	else
	{
		perc=NLSFwiz!Step.Fit.PredLevelSpin.value;
		nlsf.Pred=perc/100;
		nlsf.compileparainit=0;
		nlsf.parainit=0;
		nlsf.iterateex(0);
		nlsf.output(%T);
		nlsf.createCurves(P);
		nlsf.output();
		layer -e yfit;
		layer -a;
	}
	return 0;
	
	
[ResultsOutput]	
	if(%1==2)
		%T=NLSFwiz!LatestResultsGraph$; // get name of newly created results graph
	if(%1==3)
		%T=NLSFwiz!ActiveWindowTitle$; // get name of active graph prior to wizard launch
	
	nlsf.output(%T); // redirect nlsf output from wizard graph to results graph
	nlsf.pasteParams(plot); // send fit label to graph
	nlsf.output(); // restore nlsf output to wizard graph
	return 0;
	
	
[Step.Results.CreatePResults.OnClick]	
	%A=NLSFwiz!Step.Results.ResultsWorksheetName$;
	paramWks=NLSFwiz!Step.Results.ResultsWorksheet;
	pasteLog=NLSFwiz!Step.Results.ResultsLogCheckBox;
	run.section(,SavePRSettings,$(paramWks) $(pasteLog) %A);
	
	if(paramWks==1)
	{	
		if("%A"=="")
		{
			type -b $NLSFwiz.NoWksName;
			NLSFwiz!Step.Results.ResultsWorksheetName.SetFocus();
			return 1;
		}		
		stat.name(1,%A); // figure out what new paramwks will be
		%B=stat.name.worksheet$; // set %B to that name
		nlsf.paramwks("%A"); // create Results Worksheet (auto incremented)
		run.section(,WindowLabel,%B); // create param wks window label
	}

	if(pasteLog==1)
	{
		nlsf.pasteParams(Results); // output fit parameters to Results Log
	}
	return 0;
	
	
[SavePRSettings]	
	// %1=paramWks
	// %2=pasteLog
	// %3=name of parameters worksheet
	
	ini.setstr(%1,OutputParamWks,SystemVariables,NLSFwizard.ini);
	ini.setstr(%2,OutputRLog,SystemVariables,NLSFwizard.ini);
	ini.setstr(%3,ParamWksName,SystemVariables,NLSFwizard.ini);
	return 0;
	

[Step.Results.SaveOPS.onClick]
	// if NLSFwiz!Step.Results.SaveOPS==1 
	// bring up Buttons Settings dialog to save as OPS
	


//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Button Scripts /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////


[Toolbar1.EnlargerButton.OnClick]
	// Begin QA #1463 - RJT - 4/15/2002
	def QuitToolBox
	{
		run.section(NLSFwiz.OGS,Toolbar2Control,1);
		NLSFwiz!Toolbar1.DemagnifyButton.enable=1;
	}
	// End QA #1463 - RJT - 4/15/2002
	
	%A=NLSFwiz!GraphNameHolder$;
	win -oa %A
	{
		dotool 1;
		dotool -l;
		
	}
	NLSFwiz!MagnificationSpin++;
	return 0;
	

[Toolbar1.DemagnifyButton.OnClick]
	%A=NLSFwiz!GraphNameHolder$;
	win -oa %A
	{
		dotool 16;
	}
	NLSFwiz!MagnificationSpin--; // bump down EnlargerCount
	if(NLSFwiz!MagnificationSpin==0)
	{
		run.section(,Toolbar2Control,0);
		NLSFwiz!Toolbar1.DemagnifyButton.enable=0;
	}	
	return 0;
	
		
[Toolbar2.ScaleLeftButton.OnClick]
	run.section(,ScaleLeftOrDown,x y);
	return 0;
	
	
[Toolbar2.ScaleDownButton.OnClick]
	run.section(,ScaleLeftOrDown,y x);
	return 0;
	
	
[ScaleLeftOrDown]
	%A=NLSFwiz!GraphNameHolder$;
	win -oa %A
	{
		ii=%11;
		jj=%12;
		page.active=1;
		layer1.%2.rescale=1;
		layer -a;
		layer1.%2.rescale=2;

		if((ii-abs(.25*(jj-ii)))<%11)
		{
			%12=%11+(jj-ii);
		}
		else
		{
			%11=ii-.25*(jj-ii);
			%12=jj-.25*(jj-ii);
		}
		page.active=1;
	}
	return 0;
		

[Toolbar2.scaleRightButton.OnClick]
	run.section(,ScaleRightOrUp,x y);
	return 0;
	
	
[Toolbar2.ScaleUpButton.OnClick]
	run.section(,ScaleRightOrUp,y x);	
	return 0;
	

[ScaleRightOrUp]
	// %1 and %2 hold 'x' or 'y' 

	%A=NLSFwiz!GraphNameHolder$;
	win -oa %A
	{
		ii=%11;
		jj=%12;
		page.active=1;
		layer1.%2.rescale=1;
		layer -a;
		layer1.%2.rescale=2;
		if((jj+abs(.25*(jj-ii)))>%12)
		{
			%11=%12-(jj-ii);
		}
		else
		{
			%11=ii+.25*(jj-ii);
			%12=jj+.25*(jj-ii);
		}
		page.active=1;
	}
	return 0;
	

[Toolbar2.RescaleButton.OnClick]
	%A=NLSFwiz!GraphNameHolder$;
	win -oa %A
	{
		layer -o 1 {layer -a;}
		if(page.nlayers==2)
		{
		layer -o 2 {layer -a;}
		}
	}
	NLSFwiz!MagnificationSpin=0;
	run.section(,Toolbar2Control,0); // disable toolbar2
	NLSFwiz!Toolbar1.DemagnifyButton.enable=0; // disable demagnify
	return 0;
	
	
[Toolbar3.ParametersDisplayButton.OnClick]
	if(NLSFwiz!Toolbar3.ParametersDisplayButton==0)
	{
		nlsf.closeparams();
		NLSFwiz!Toolbar3.ParametersDisplayButton=1;
	}
	else
	{
		nlsf.updateparams();	
		nlsf.parameters();
		NLSFwiz!Toolbar3.ParametersDisplayButton=0;
	}	
	return 0;
	

[Toolbar3.ResidualsButton.OnClick]
	%A=NLSFwiz!GraphNameHolder$;
	win -oa %A
	{
		if(NLSFwiz!Toolbar3.ResidualsButton==1)
		{
			run.section(,CreateResidualsLayer,%A);
			run.section(,UpdateResidualsStyle,%A);
			NLSFwiz!Toolbar3.ResidualsButton=0;
		}
		else
		{
			page.active=2; 
			layer -d; // delete layer 2 
			doc -uw; 
			layer -o 1 {
				layer 75 80 10 10; // resize layer 1
				doc -uw; // refresh graph to display layer 1 at correct size
				};
			NLSFwiz!Toolbar3.ResidualsButton=1;
		}
	}
	return 0;
	
		
[CreateResidualsLayer]	
	win -oa %1
	{
		layer -o 1 {layer 75 50 10 10;};
		layer -n; // new layer
		layer -o 2 {
				layer 75 20 10 70;
				layer2.x.type=layer1.x.type;
				layer2.x.from=layer1.x.from;
				layer2.x.to=layer1.x.to;
				layer2.link=1; // link 2 to 1
				layer2.x.link=1; // set x axis link as 1to1
				layer2.x.inc=layer1.x.inc;
				layer2.x.atZero=1; // display line at y=0	
				nlsf.output(%A);
				nlsf.residuals(); // plot residuals
				nlsf.output();
				layer -a; // rescale layers
				};
	}
	return 0;
	
[UpdateResidualsStyle]
	// graph name passed in %1
	// get residue dataset name 
	// then change its attributes
	win -o %1
	{	
		doc -e LW
		{
			doc -e D
			{
				%B=%[%C,>'_'];
				if("%B"=="residue")
				{	
					layer -i202 %C; // plot as line + symbol
					set %C -l 0; // set as scatter
					set %C -k 2; // set as circle
					set %C -c 1; // set as black
					set %C -lv 1; // show drop lines
				}
			}
		}
	}
			
			
[Toolbar3.ConfidenceBandsButton.OnClick]
	%W=NLSFwiz!GraphNameHolder$;
	if(NLSFwiz!Toolbar3.ConfidenceBandsButton==1)
	{
		perc=NLSFwiz!Step.Fit.ConfLevelSpin.value;
		nlsf.conf=(perc/100);
		win -oa %W
		{
			layer -o 1
			{
				nlsf.output(%W);
				nlsf.compileparainit=0;
				nlsf.parainit=0;
				nlsf.iterateex(0);
				nlsf.createCurves(C); // create confidence bands
				nlsf.output();
				layer -a;
			}
		}
		NLSFwiz!Toolbar3.ConfidenceBandsButton=0;
	}
	else
	{
		run.section(,RemoveBands,"LCL"); // remove confidence bands
		NLSFwiz!Toolbar3.ConfidenceBandsButton=1;
	}
	return 0;
	

[Toolbar3.PredictionBandsButton.OnClick]
	%W=NLSFwiz!GraphNameHolder$;
	if(NLSFwiz!Toolbar3.PredictionBandsButton==1)
	{
		perc=NLSFwiz!Step.Fit.PredLevelSpin.value;
		nlsf.pred=(perc/100);
		win -oa %W
		{
			layer -o 1
			{
				nlsf.output(%W);
				nlsf.compileparainit=0;
				nlsf.parainit=0;
				nlsf.iterateex(0);
				nlsf.createCurves(P); // create prediction bands
				nlsf.output();
				layer -a; 
			}
		}
		NLSFwiz!Toolbar3.PredictionBandsButton=0;
	}
	else
	{
		run.section(,RemoveBands,"LPL"); // remove prediction bands
		NLSFwiz!Toolbar3.PredictionBandsButton=1;
	}
	return 0;
	
	
[RemoveBands]
	%W=NLSFwiz!GraphNameHolder$;
	win -oa %W
	{
		page.active=1;
		%B=%1; // %1 holds either LCL or LPL
		doc -e D
		{
			%A=%[%C,>"_"];
			if("%A"=="%B")
			{
				%A=%[%C,"_"];
				win -cd %A; // deletes associated NLSF worksheet
				layer -a;
			}
		}
		
	}
	return 0;
		

//////////////////////////////////////////////////////////////////////////////////////////
////////////////Sections used more than once for All Pages of Wizard /////////////////////
//////////////////////////////////////////////////////////////////////////////////////////


[ReplotData]
	// %1 is the dataset name
	// %2 is 1 or 0 for scatter plot
	// %3 is 1 or 0 for line plot
	ini.file$="NLSFwizard.ini";
	%S=ini.SystemVariables.SymbolSize$;
	layer -i202 %1; // plot as line + symbol
	set %1 -c 20; // set color to gray
	if("%2"==1)
	{
		set %1 -k 1; // square
		set %1 -kf 1; // interior open
		set %1 -z (%S); // set size
	}
	else
	{
		set %1 -k 0; // none
	}
	if("%3"==1)
	{
		run.section(,ConnectDropDown.OnSelChange); // set line connect
	}
	else
	{
		set %1 -l 0; // no connection
	}
	
	layer -a; // rescale 
	doc -uw; // refresh
	return 0;
	

[Scatter.OnClick]
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	ini.setstr($(NLSFwiz!Scatter),Scatter,SystemVariables,NLSFwizard.ini);
	if(NLSFwiz!Scatter==0)
	{
		NLSFwiz!SymbSizeDropDown.enable=0;
	}
	else
	{
		NLSFwiz!SymbSizeDropDown.enable=1;
	}		
	win -oa %W
	{
		run.section(,ReplotData,%C $(NLSFwiz!Scatter) $(NLSFwiz!Line));
	}
	return 0;
	

[Line.OnClick]
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;	
	ini.setstr($(NLSFwiz!Line),Line,SystemVariables,NLSFwizard.ini);
	if(NLSFwiz!Line==0)
	{
		NLSFwiz!ConnectDropDown.enable=0;
	}
	else
	{
		NLSFwiz!ConnectDropDown.enable=1;
	}	
	win -oa %W
	{
		run.section(,ReplotData,%C $(NLSFwiz!Scatter) $(NLSFwiz!Line));
	}
	return 0;
	
	
[SymbSizeDropDown.OnSelChange]
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	%S=NLSFwiz!SymbSizeDropDown.s1$;
	win -oa %W
	{
		set %C -z (%S);
	}
	ini.setstr($(NLSFwiz!SymbSizeDropDown.s1),SymbolSize,SystemVariables,NLSFwizard.ini);
	ini.setstr($(NLSFwiz!SymbSizeDropDown),SymbolSizeIndex,SystemVariables,NLSFwizard.ini);
	return 0;
	
		
[ConnectDropDown.OnSelChange]
	%W=NLSFwiz!Step.Data.GraphOnData.graph$;
	win -oa %W
	{
		run.section(,SetLineConnection,NLSFwiz!ConnectDropDown.Choice);
		ini.setstr($(NLSFwiz!ConnectDropDown.Choice),LineConnectIndex,SystemVariables,NLSFwizard.ini);
	}
	return 0;
	
	
[SetLineConnection]
	if(%1==1)				
		set %C -l 0; // none
	if(%1==2) 
		set %C -l 1; // straight
	if(%1==3) 
		set %C -l 2; // 2 pt segment
	if(%1==4)
		set %C -l 3; // 3 pt segment
	if(%1==5)
		set %C -l 8; // B-Spline
	if(%1==6)
		set %C -l 9; // Spline
	if(%1==7)
		set %C -l 11; // step horz
	if(%1==8)
		set %C -l 12; // step vert
	if(%1==9)
		set %C -l 13; // step (H) ctr
	if(%1==10)
		set %C -l 14; // step (V) ctr
	if(%1==11)
		set %C -l 15; // Bezier
	return 0;
	
	
[ScaleType.OnSelChange]
	%W=NLSFwiz!Step.Data.GraphOnData.Graph$;
	win -oa %W
	{
		run.section(,SetScaleType,$(NLSFwiz!ScaleType.Choice));
	}
	return 0;
	
	
[SetScaleType]
	%W=NLSFwiz!Step.Data.GraphOnData.Graph$;
	win -oa %W
	{
		page.active=1; // activate layer 1
		layer1.x.type=%1; // set layer 1 scale type 
		layer -a; // rescale layer 1
		if(page.nLayers>1)
		{
			page.active=2; // activate layer 2
			layer2.x.type=%1; // set layer 2 scale type
			layer -a; // rescale layer 2
		}	
	}
	return 0;
	

[SetNlsfFitData]
	%W=NLSFwiz!GraphNameHolder$;	
	win -oa %W 
	{
		// set the selected category
		%S=NLSFwiz!Step.Function.CatList.s1$; 
		nlsf.category(%S); // set category
		nlsf.x$=%1; // set X dataset to be used in fitting
		nlsf.fitData$=%2; // set Y dataset to be used in fitting
		NLSFwiz!Step.Data.SelectIndep$=%1; // set static text for indep variable (x dataset)
		NLSFwiz!FitData$=%2; // display fit data in Current Selections group
	}
	return 0;
	

[Toolbar1Control]
	NLSFwiz!Toolbar1.EnlargerButton.enable=%1;
	NLSFwiz!Toolbar1.DemagnifyButton.enable=%1;
	return 0;
	
	
[Toolbar2Control]
	NLSFwiz!Toolbar2.ScaleLeftButton.enable=%1;
	NLSFwiz!Toolbar2.ScaleRightButton.enable=%1;
	NLSFwiz!Toolbar2.ScaleUpButton.enable=%1;
	NLSFwiz!Toolbar2.ScaleDownButton.enable=%1;
	NLSFwiz!Toolbar2.RescaleButton.enable=%1;
	return 0;
	
	
[Toolbar3Control]
	NLSFwiz!Toolbar3.ParametersDisplayButton.enable=%1;
	NLSFwiz!Toolbar3.ResidualsButton.enable=%1;
	NLSFwiz!Toolbar3.ConfidenceBandsButton.enable=%1;
	NLSFwiz!Toolbar3.PredictionBandsButton.enable=%1;
	return 0;
	
	
[PlotAttributesControl]
	%A=NLSFwiz!GraphNameHolder$;	
	NLSFWiz!Scatter.enable=%1;
	NLSFWiz!Line.enable=%1;

	if(NLSFwiz!Scatter==0)
	{
		NLSFwiz!SymbSizeDropDown.enable=0;
	}
	else
	{
		NLSFwiz!SymbSizeDropDown.enable=1;
	}
	if(NLSFwiz!Line==0)
	{
		NLSFwiz!ConnectDropDown.enable=0;
	}
	else
	{
		NLSFwiz!ConnectDropDown.enable=1;
	}
		
	if("%A"=="FunctionPage")
	{
		NLSFWiz!SymbSizeDropDown.enable=0;
		NLSFwiz!ConnectDropDown.enable=0;
	}

	NLSFwiz!ScaleType.enable=%1;
	NLSFwiz!PlotText1.enable=%1;
	NLSFwiz!PlotText2.enable=%1;
	NLSFwiz!PlotText3.enable=%1;
	NLSFwiz!PlotText4.enable=%1;
	return 0;
	

[CleanUp]
	// %1 holds NLSFwiz!GraphNameHolder$ - set to "FunctionPage" if on Function Page)
	%A=%1;
	%B=nlsf.funcCol$;
	nlsf.funcX$="";
	nlsf.funcCol$="";

	win -oa %A 
	{
		if("%A"!="FunctionPage")
		{
			page.active=1;
			layer -e %B; // remove fit data
			// remove bands or residuals if the buttons are down
			if(NLSFwiz!Toolbar3.ResidualsButton==0)
				run.section(,Toolbar3.ResidualsButton.OnClick);
			if(NLSFwiz!Toolbar3.ConfidenceBandsButton==0)
				run.section(,Toolbar3.ConfidenceBandsButton.OnClick);
			if(NLSFwiz!Toolbar3.PredictionBandsButton==0)
				run.section(,Toolbar3.PredictionBandsButton.OnClick);
			doc -uw; // refresh graph to display layer 1 at correct size
			layer -a; // rescale layer 1
		}
	}	
	// disable toolbar3
	run.section(,Toolbar3Control,0);	
	return 0;
	
	
[OnWindowChange]	
	if(exist(%H)==1)
	{
		type -b hello;
	}
	

[OnCancel]
	%A=NLSFwiz!GraphNameHolder$;
	if(run.section(,Cancel))
		return 1;
	
	nlsf.cleanupfitdata(); // clean up nlsf properties.
	nlsf.end(13); // end with no output
	nlsf.init(); // reset fitter
		
	run.section(,DeleteHiddenDatasets);
	run.section(,DeleteVariables);
	return 0;
		

[Cancel]	
	if(OpenErrorNLSFWiz!=1)
		type -n $NLSFwiz.CancelPrompt;
	return 0;
	
	
[FinishControl]	
	if(nlsf.dof>0 && nlsf.chisqr!=0/0)
	{	
		NLSFwiz!Finish.enable=1; // enable Finish
	}
	else
	{
		NLSFwiz!Finish.enable=0; // disable Finish
	}	
	return 0;
	
	
[FinishNextControl]	
	if(nlsf.dof>0 && nlsf.chisqr!=0/0)
	{
		NLSFwiz!Step.Pal=0; // enable Next button
		NLSFwiz!Finish.enable=1; // enable Finish button
	}
	else
	{
		NLSFwiz!Step.Pal=%1; // disable Next button
		NLSFwiz!Finish.enable=0; // disable Finish button
	}
	return 0;
	

[OnFinish]	
	// use NLSFwiz!Step.Results.SaveOPS to determine if Button Settings should come up
	record=NLSFwiz!Step.Results.SaveOPS;
	%A=NLSFwiz!GraphNameHolder$;
	
	run.section(,Step.Results.CreateGResults.OnClick);
	run.section(,Step.Results.CreatePResults.OnClick);

	nlsf.cleanupfitdata(); // clean up nlsf properties
	nlsf.end(13); // end with no output
	nlsf.init(); // reset fitter
	
	run.section(,DeleteHiddenDatasets);
	run.section(,DeleteVariables);
	run.section(,ResetLimits);
	
	if(record==1)
	{
		AM=NLSFwiz!AutoMode;
		if(AM==0)
		{
			dialog.ButtonSave(%YNLSFWiz_OPS.bmp);
		}
	}
	del record;
	return 0;
	
	
[DeleteHiddenDatasets]
	if(exist(_Peak_Y)==1)
	{
		del _Peak_Y;
		del _integ_area;
	}
	return 0;
	
[DeleteVariables]

	// general variables
	del -v ii;
	del -v jj;
	del -v choice;
	del -v perc;
	del -v OpenErrorNLSFWiz;

	// data page
	del -v temprangefrom;
	del -v temprangeto;
	
	// function page
	del -v catNum;
	del -v funcNum;
	
	del -v numInd;
	del -v numDep;
	del -v currentFunc;
	
	// peaks page
	del -v multiPeaks;
	del -v numPeaks;
	
	// weighting page
	del -v length1;
	del -v length2;
	del -v numDatasets;
	del -v numErrSets;
	del -v start;
	
	// fit page
	del -v count;
	del -v iterations;
	del -v conf;
	del -v pred;
	del -v length;
	
	// results page
	del -v curveChoice;
	del -v residualsChoice;
	del -v confChoice;
	del -v predChoice;
	del -v resultsChoice;
	del -v paramWks;
	del -v pasteLog;
	del -v bgrow;
	del -v edrow;
	del -v var;
	
	// plot attributes group
	del -v scatter;
	del -v line;
	return 0;

	
	
	
	