// Filename:    PR.ogs
// Creation:    by Joe Przechocki, 7/27/95
// Purpose:     Modularized Code for performing Polynomial Regression 
//              from Menu or PR Toolbar.
// Modifications:
// RKM 12/11/98 v6.0141 REPORT_TO_OUTPUTLOG
// SDB 1/7/99   v6.0148 REPORT_TO_OUTPUTLOG
// LAS 1/10/02 v6.9174 added support for FindX and FindY
////////////////////////////////////////////////////////////////////////////////
// Main Code which is run based on if user hits FIT or SAVE on toolbar
////////////////////////////////////////////////////////////////////////////////

[Tab.Operation.OnInit]
	PR!Tab.Operation.Spin.Min=1;
	PR!Tab.Operation.Spin.Max=9;
	PR!Tab.Operation.Spin.Value=2;
	
	PR!Tab.Operation.FindXedit.enable=0;
	PR!Tab.Operation.FindYedit.enable=0;
	PR!Tab.Operation.FindXButn.enable=0;
	PR!Tab.Operation.FindYButn.enable=0;

[Tab.Settings.OnInit]
	PR!Tab.Settings.Points$=20;
	PR!Tab.Settings.RangeMargin$=10;
	PR!Tab.Settings.Confidence.v1=95;
	PR!Tab.Settings.Simple=1;

[Tab.Operation.Fit.OnClick]
	if(exist(%H)==3 && Layer.is3D==1)
	{
		type -b $general.2DGraphsOnly;
		return 1;
	}

	stat.reset();				//Reset the LabSts40.DLL
	if (PR!Tab.Settings.FitallCurves==1 && exist(%H)==3)
	{
		//Concatenate a list of Y datasets
		count=0;%Z="";doc -e DY {%Z=%Z %C;count++;};
		loop (ii,1,count)
		{
			%A=%[%Z,#ii];
			if (run.section(,SelectionCk,%A)==0 && run.section(,OptionsCk)==0) 
			{
				run.section(,MakeFit,%A);        //If Options are OK then fit
				run.section(,Columns);        //Now insert data into raw wks
			}
		}
	}
	else
	{
		if (run.section(,SelectionCk,%C)==0 && run.section(,OptionsCk)==0) 
		{
			run.section(,MakeFit,%C);        //If Options are OK then fit
			run.section(,Columns);        //Now insert data into raw wks
		}
		else 
			return 1;
	}
	
	
	if (PR!Tab.Settings.FitAllCurves==0  &&  PR!tab.settings.ApparentFit==0)
	{
	
		PR!Tab.Operation.StaticText$=$Regression.ForDataSet;
		PR!Tab.Operation.FindXedit.enable=1;
		PR!Tab.Operation.FindYedit.enable=1;
		PR!Tab.Operation.FindXButn.enable=1;
		PR!Tab.Operation.FindYButn.enable=1;
		
	}
	else
	{
	
		PR!Tab.Operation.StaticText$=$Regression.Calculate;
		PR!Tab.Operation.FindXedit.enable=0;
		PR!Tab.Operation.FindYedit.enable=0;
		PR!Tab.Operation.FindXButn.enable=0;
		PR!Tab.Operation.FindYButn.enable=0;
		
	}
		
	
	return 0;

	

[ReportError]
	
	type -b;				//BEEP!
	type -b %1;				//Type out the appropriate error message
	type -q "":				//Clear up the Status Bar

	
////////////////////////////////////////////////////////////////////////////////
// Menu PR Code
////////////////////////////////////////////////////////////////////////////////
[FitPolynomial]

	if (run.section(,SelectionCk,%C)==0 && run.section(,SimpleFit)==0)
		run.section(,MenuPROutput);


////////////////////////////////////////////////////////////////////////////////
// Modular Code for performing Polynomial Regression follows
////////////////////////////////////////////////////////////////////////////////

[SelectionCk]
	/* check user settings                                          */
	/* -------------------------------------------------------------*/
	/* Check the users data selection                               */

	if (exist(%H)!=3 && exist(%H)!=2)
	{
		run.section(,ReportError,$General.2DGraphsOrWks);
		return 1;
	}

	if (exist(%H)==3)
	{
		if (%1=="")
		{
			run.section(,ReportError,$Regression.NoData);
			return 1;
		}
		else
		{
			stat.data$=%1;
			%B=errof(%1)$;
			if (exist(%B)==1)
				stat.errbarData$=%B;
			else
				stat.errbarData$="";
		} 
	} 
	
	if (exist(%H)==2)
	{
		if ((wks.sel<=1)||((wks.col$(wks.c1).type==4)&&(wks.sel&16==16)))
		{
			run.section(,ReportError,$Regression.BadOptions);
			return 1;
		}
		else
		{
			switch (wks.col$(wks.c1).type)
			{
				case 1://Y column is first select
					stat.data$=%(%H,wks.c1);        
					break;
				case 4://X column is first selected
					stat.data$=%(%H,%(%H,@YS,1));
					break;
				default:
					run.section(,ReportError,$Regression.BadOptions);
					return 1;       //Return ERROR for bad selection
			}; 
			%A=stat.data$;%B=errof(%A)$;
			if ((exist(%B)==1)&&(colnum(%B)<=wks.c2)) 
				stat.errbarData$=%B;
			else
				stat.errbarData$="";
		}
	}
	
	return 0;					//Return OK
	
////////////////////////////////////////////////////////////////////////////////
// Check/Update Options Settings on SETTINGS page of toolbar
////////////////////////////////////////////////////////////////////////////////
[OptionsCk]
	//User can specify any number for fit POINTS or CONFIDENCE level
	//Toolbar only displays integer however remembers entered number
	//therefore we need to set the actual number to the nearest integer.

	PR!Tab.Settings.Points.v1=nint(PR!Tab.Settings.Points.v1);

	//If user has Error-Bars-As-Weight OFF we definitely do not use error Bars
	if (PR!Tab.Operation.ErrorAsWeight==0)
		stat.ErrBarData$="";

	if (PR!Tab.Settings.Confidence.v1<=0 || PR!Tab.Settings.Confidence.v1>=100)
	{
		run.section(,ReportError,$Regression.BadConfidence);
		return 1;
	}

	PR!Tab.Settings.Confidence.v1=int(PR!Tab.Settings.Confidence.v1*10+.5)/10;
	
	/* Need at least 2 pts to create a line                 */
	if (PR!Tab.Settings.Points.v1<2)
	{
		run.section(,ReportError,$Regression.BadNoOfPts);
		return 1;
	}

	//Error bar options is ON but there is no Error Bars to be found!!!
	%B=stat.errbarData$;
	if (PR!Tab.Operation.ErrorAsWeight && %B=="")
	{
		run.section(,ReportError,$Regression.NoErrBars);
		return 1;
	}
	
	/* Insert Residual Column requires a new column to be in existance before pr() */
	%A=stat.data$;

	if (PR!Tab.Settings.ResidualData && exist(%[%A,'_'])!=2)
	{
		run.section(,ReportError,$General.NoRawWks);
		return 1;
	}
	if (PR!Tab.Settings.ResidualData)
	{
		win -o %[%A,'_']
		{
			wks.col=colnum(%A);
			for (;1;)
			{
				if (wks.col$(wks.col+1).type==3 || wks.col$(wks.col+1).type==5)
					wks.col+=1;
				else
					break;
			}
			if (PR!Tab.Operation.ErrorAsWeight && wks.col<colnum(%B))
				wks.col=colnum(%B);		//if no error-bar designations found, force to be dataset MAPPED AS error

			wks.col+=1;     //Insert after Y or any Error Bar data...
			wks.insert(Residual);
			wks.insert(Fit);	//Insert with automatic enumeration of column names
			stat.resData$=%(%H,wks.col+1);
			PR!Tab.Settings.Residual.v1=wks.col+1;//Store location of Residuals
		}; 
	} 
	
	/* -------------------------------------------------------------*/
	return 0;// return Ok
	
	
////////////////////////////////////////////////////////////////////////////////
// Fitting Module
////////////////////////////////////////////////////////////////////////////////

[MakeFit]
	/* Perform PR */
	/* -------------------------------------------------------------*/
	if (exist(%H)==2)
	{
		/* plot approved wks selection*/
		worksheet -p 201 scatter;
		if (PR!Tab.Operation.ErrorAsWeight)
		{
			%A=stat.ErrBarData$;//Ensure inclusion of bars
			layer -i %A;
		} 
	
		//Note! Place code here to modify plot if desired.
	} 

	layer -i;                       /* to clear style templates */

	if (PR!Tab.Operation.ErrorAsWeight)
		stat.ChiSqrErr=PR!Tab.Operation.UseChiSquare;
	else
		stat.ChiSqrErr=1;
	
	// Setup the Fit Curve
	%A=stat.data$;
	create.wksName$="PolyFit";
	create.wksLabel$=$Regression.PolyFit;
	create.template$="CREATE";
	create.npts=PR!Tab.Settings.Points.v1;
	create.enumwks=1;
	create.numtypes=1;			
	create.type1=4;				//Only explicitly specify 1st type as X
	create.typen=1;				//Set TypeN=Y column type

	if (PR!Tab.Operation.ConfidenceBands==0 && PR!Tab.Operation.PredictionBands==0)
		create.wks(A %A);
	if (PR!Tab.Operation.ConfidenceBands==1 && PR!Tab.Operation.PredictionBands==0)
	{
		create.wks(A %A UCL LCL);
		%B=create.data3$;
		stat.UCLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRUpperConf);

		%B=create.data4$;
		stat.LCLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRLowerConf);
	}
	if (PR!Tab.Operation.ConfidenceBands==0 && PR!Tab.Operation.PredictionBands==1)
	{
		create.wks(A %A UPL LPL);

		%B=create.data3$;
		stat.UPLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRUpperPred);

		%B=create.data4$;
		stat.LPLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRLowerPred);
	}

	if (PR!Tab.Operation.ConfidenceBands==1 && PR!Tab.Operation.PredictionBands==1)
	{
		create.wks(A %A UCL LCL UPL LPL);

		%B=create.data3$;
		stat.UCLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRUpperConf);

		%B=create.data4$;
		stat.LCLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRLowerConf);

		%B=create.data5$;
		stat.UPLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRUpperPred);

		%B=create.data6$;
		stat.LPLdata$=%B;
		run.section(Origin,SetWksLabel,%B $Regression.PRLowerPred);
	}

	%B=create.data1$;	
	stat.fitXdata$=%B;

	%B=create.data2$;
	stat.fitYdata$=%B;
	run.section(Origin,SetWksLabel,%B $Regression.PolyFit);

	stat.PR.order=PR!Tab.Operation.Spin.Value;
	stat.confLevel=PR!Tab.Settings.Confidence.v1/100;
	
	/* New codes, CPY v3.74 */
	stat.MakeX.FitNpts=PR!Tab.Settings.Points.v1;
	
	limit %A;
	
	if (PR!Tab.Settings.SpanXAxis)
	{
		stat.MakeX.FitX1=layer.x.from;
		stat.MakeX.FitX2=layer.x.to;
	}
	else
	{
		stat.MakeX.FitX1=limit.xmin;
		stat.MakeX.FitX2=limit.xmax;
	}

	stat.makeX.margin=PR!Tab.Settings.SpanXAxis?0:PR!Tab.Settings.RangeMargin.v1;
	stat.makex();
	
	err=stat.PR();                                  /* Polynomial Regression */
	#!err=;

	/* Check to see if the DLL had trouble */	

	if (err==1)
	{
		type -b;				//BEEP!
		type -b $general.BADCalculation;	
		return 1;
	}
	
	/* Check to see if the DLL reports bad calculation due to error bars */
	if (err==6)
	{
		type -b;                                //BEEP!
		type -b $regression.badErrBar;   	//Error!
		return 1;                       	//Return Error
	}
	
	/* Prepare the plot to receive datasets                 */
	/* ---------------------------------------------------- */
	if (PR!Tab.Settings.SpanXAxis)
	{
		PR!Tab.Settings.Temp.v1=layer.x.rescalemargin;
		layer.x.rescalemargin=0;
	}


	%B=create.data2$;
	layer -i200 %B;                   	// plot into layer
	set %B -c 2;                        	// set to red
	
	/* Include green confidence line curves */
	if(PR!Tab.Operation.ConfidenceBands)
	{
		%B=stat.UCLdata$;
		layer -i200 %B;
		set %B -c 3;
	
		%B=stat.LCLdata$;
		layer -i200 %B;
		set %B -c 3;
	}
	/* Include blue confidence line curves */
	if(PR!Tab.Operation.PredictionBands)
	{
		%B=stat.UPLdata$;
		layer -i200 %B;
		set %B -c 4;
	
		%B=stat.LPLdata$;
		layer -i200 %B;
		set %B -c 4;
	}
	legend;						//Update the legend
	
	if (PR!Tab.Settings.SpanXAxis)
		layer.x.rescalemargin=PR!Tab.Settings.Temp.v1;


	/* ---------------------------------------------------- */
	/*   Output Parameters and ANOVA Table (optional)       */
	/* ---------------------------------------------------- */
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	type.BeginResults();		/// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG

	if (layer.x.space!=1 || layer.y.space!=1)
	{
		%A=%1;
		type $regression.PRApparent;
		type $regression.PREqnApparent;
		type $regression.PRmsgApparent1;
		type $regression.PRmsgApparent2;
		if (stat.ErrBarData$!="")
		{
			%B=stat.errBarData$;
			type $regression.UsingWeight;
		}
	}
	else
	{
		%A=%1;
		type $regression.PerformPR;
		type $regression.EqnForPR$(stat.pr.order);
		if (stat.ErrBarData$!="")
		{
			%B=stat.errBarData$;
			type $regression.UsingWeight;
		}
	}
	
	
	type;
	
	if (PR!Tab.Settings.Simple==1)
	{
		type $regression.ShortParaHeader1;
		separator 4;
		type "A\t$(Stat.PR.A)\t$(stat.PR.Ase)";
		for (PR!Tab.Settings.Temp.v1=1;PR!Tab.Settings.Temp.v1<=stat.pr.order;PR!Tab.Settings.Temp.v1+=1)
			/* ----- B Parameters and their errors ------------*/
			type "B$(PR!Tab.Settings.Temp.v1)\t$(Stat.PR.B$(PR!Tab.Settings.Temp.v1))\t$(stat.PR.Bse$(PR!Tab.Settings.Temp.v1))";
		separator 4;
		
		type;
		type $regression.ShortParaHeader3;
		separator 4;
		if (stat.pValue<0.0001) %A="<0.0001"; else %A=$(stat.pValue);
		type "$(Stat.COD)\t$(Stat.SD)\t$(stat.npts)\t%A";
		separator 4;
		type;
	}
	if (PR!Tab.Settings.Simple==2)
	{
		type $regression.LongParaHeader1;
		separator 5;

		if (stat.PR.Ap<0.0001) %A="<0.0001"; else %A=$(stat.PR.Ap);
		type "A\t$(Stat.PR.A)\t$(stat.PR.Ase)\t$(stat.PR.A/stat.PR.Ase)\t%A";
		for (PR!Tab.Settings.Temp.v1=1;PR!Tab.Settings.Temp.v1<=stat.pr.order;PR!Tab.Settings.Temp.v1+=1)
		{
			/* ----- B Parameters and their errors ------------*/
			if (stat.PR.Bp$(PR!Tab.Settings.Temp.v1)<0.0001) %A="<0.0001"; else %A=$(stat.PR.Bp$(PR!Tab.Settings.Temp.v1));
			type "B$(PR!Tab.Settings.Temp.v1)\t$(Stat.PR.B$(PR!Tab.Settings.Temp.v1))\t$(stat.PR.Bse$(PR!Tab.Settings.Temp.v1))\t$(stat.PR.B$(PR!Tab.Settings.Temp.v1)/stat.PR.Bse$(PR!Tab.Settings.Temp.v1))\t%A";
		}
		separator 5;
		
		type;
		type $regression.LongParaHeader2A;	//Annotated Long Parameter Header
		separator 5;
		type "$(stat.COD)\t$(stat.AdRSq)\t$(Stat.SD)\t$(stat.npts)";
		separator 5;
	}
	if (PR!Tab.Settings.Simple==2)
	{
		/* ---------------- Type out the Confidence Intervals...------- */
		type;
		%A=PR!Tab.Settings.Confidence$;
		type $regression.ConfHeader;
		separator 5;
		type "A\t$(Stat.PR.A-(Stat.PR.ASE*Stat.PR.t))\t$(Stat.PR.A+(Stat.PR.ASE*Stat.PR.t))";
		for (PR!Tab.Settings.Temp.v1=1;PR!Tab.Settings.Temp.v1<=stat.pr.order;PR!Tab.Settings.Temp.v1+=1)
		{
			type "B$(PR!Tab.Settings.Temp.v1)\t$(Stat.PR.B$(PR!Tab.Settings.Temp.v1)-(Stat.PR.BSE$(PR!Tab.Settings.Temp.v1)*Stat.PR.t))\t$(Stat.PR.B$(PR!Tab.Settings.Temp.v1)+(Stat.PR.BSE$(PR!Tab.Settings.Temp.v1)*Stat.PR.t))";
		}
		/* ---------------- Type out the ANOVA Table...---------------- */
		separator 5;
		type;
		type $regression.AnovaHeader;
		separator 5;
		type $regression.AnovaDesc1;
		type $regression.AnovaDesc2;
		separator 5;
		type -l $regression.model;                       //The word MODEL in current language
		type "\t$(stat.df1)\t$(stat.ssr)\t$(stat.msr)\t$(stat.fvalue)";
		type -l $regression.error;                       //The word ERROR in current language
		type "\t$(stat.df2)\t$(stat.sse)\t$(stat.mse)";
		type -l $regression.total;                       //The word TOTAL in current language
		type "\t$(stat.df3)\t$(stat.SSto)";
		separator 5;
		type;
	
		type $regression.AnovaDesc3;
		separator 5;
		if (stat.pValue<0.0001) %A="<0.0001"; else %A=$(stat.pValue);
		type  "%A";
		separator 5;
		type;
	}

	
////////////////////////////////////////////////////////////////////////////////
// Output Fit and Residual into new columns of the Raw Data Worksheet
////////////////////////////////////////////////////////////////////////////////
[Columns]
	/* Calculate Fit & Residual Data based on experimental X values */
	/* -------------------------------------------------------------*/
	
	%A=stat.data$;
	
	if (PR!Tab.Settings.ResidualData)
	{
		type -qp 0;                                     //Disable Origin arithmetic progress msg
		win -o %[%A,'_']
		{						//Fit=Raw Data - Residual
			wks.col=colnum(%A);
			wcol(PR!Tab.Settings.Residual.v1-1)=wcol(wks.col)-wcol(PR!Tab.Settings.Residual.v1);
			set %H -ue;				//Update worksheet if Excel
		};
		type -qp 1;                                     //Enable Origin arithmetic progress msg
		%A=%[%A,'_'];
		type $regression.SavedFit&Res;
		
	}
	
	type.EndResults();
	type.Redirection=SaveRedirection; // restore previous redirection.
	delete -v SaveRedirection;
	return 0;						//Return OK
	
	/* -------------------------------------------------------------*/
	
	
	
	
////////////////////////////////////////////////////////////////////////////////
// Simplistic PR Fitting for Menu
////////////////////////////////////////////////////////////////////////////////


[SimpleFit]
	
	/* Perform PR */
	/* -------------------------------------------------------------*/

	%A=errof(%C)$;
	if (exist(%A)==1)
	{
		stat.ErrBarData$=%A;
		layer -i %A;
	}
	
	%A=Stat.ErrBarData$;
	if(exist(%A)==1)
		stat.ChiSqrErr=0;
	else
		stat.ChiSqrErr=1;

	/* setup the fit curve */
	%A=stat.data$;
	limit %A;
	stat.MakeX.FitX1=limit.xmin;
	stat.MakeX.FitX2=limit.xmax;
	stat.MakeX.FitNpts=stat.MakeX.FitNpts<1?20:stat.MakeX.FitNpts;
	stat.pr.order=stat.pr.order<1 || stat.pr.order>9?2:stat.pr.order;		//Initialize for now...
							//Now Get User Settings...
	GetNumber	$PR.SimpleFitOrder stat.pr.order
			$PR.SimpleFitpts stat.MakeX.fitnpts
			$PR.SimpleFitXmin stat.MakeX.fitx1
			$PR.SimpleFitXMax stat.MakeX.fitx2
			$PR.SimpleFitShow Formula:2
			$PR.SimpleFitPolynomial;
	if(stat.pr.order<=0 || stat.pr.order>9 || stat.MakeX.fitnpts < 2 || stat.MakeX.fitx1==stat.MakeX.fitx2) 
	{
		type -b $regression.badoptions; 
		break 1;
	}

	create.wksName$="PolyFit";
	create.wksLabel$=$Regression.PolyFit;
	create.template$="CREATE";
	create.npts=stat.MakeX.fitnpts;
	create.enumerate=1;
	create.numtypes=1;					//Only explicitly specify 1st type as X
	create.type1=4;
	create.typen=1;						//Set TypeN=Y column type
	
	create.delete(%C);
	create.wks(A %C);

	%B=create.data1$;
	stat.fitXdata$=%B;
	%B=create.data2$;
	stat.fitYdata$=%B;
	%A=%C;
	run.section(Origin,SetWksLabel,%B $Regression.PolyFit);

	stat.Apparent=PR!tab.settings.ApparentFit;				//Always Apparent

	stat.MakeX.margin=10;
	stat.makex();
	
	err=stat.PR();					/* Polynomial Regression */

	if (err==1)
	{
		type -b;				//BEEP!
		type -b $general.BADCalculation;	
		return 1;
	}

	/* Check to see if the DLL reports bad calculation due to error bars */
	if (err==6)
	{
		type -b;                                //BEEP!
		type -b $regression.badErrBar;   //Error!
		return 1;                       //Return Error
	}
	
	%A=create.data2$;
	layer -i200 %A;                     // plot into layer
	set %A -c 2;                        // set to red
	
	if (Formula) 
	{
		%Z="$(stat.PR.A)";
		for (i=1;i<=stat.PR.order;i++) 
		{
			if (stat.PR.B$(i)<0)
				%K="$(stat.PR.B$(i))";
			else
				%K="+$(stat.PR.B$(i))";
			if (i>1)
				%Z="%Z%K X\+($(i))";
			else
				%Z="%Z%K X";
		}
		%Z=Y =%Z;
		label -p 10 0 -s -sa %Z;
	}

	legend;
	return 0;				//Return OK
////////////////////////////////////////////////////////////////////////////////
// Menu PR Simple Output - no possibility of Through Zero
////////////////////////////////////////////////////////////////////////////////

[MenuPROutput]
	%A=stat.data$;
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 v6.0148 REPORT_TO_OUTPUTLOG
	type.BeginResults();		/// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG
	type $regression.performPR;
	type $regression.EqnForPR$(stat.pr.order);

	/* Now check to see if there is error data and if it is in plot */
	%B=errof(%A)$;
	if (exist(%B)==1)
		type $regression.UsingWeight;
	type;
	type $regression.ShortParaHeader1;
	separator 4;
	type "A\t$(Stat.PR.A)\t$(stat.PR.Ase)";
	for (i=1;i<=stat.pr.order;i+=1)
	/* ----- B Parameters and their errors ------------*/
		type "B$(i)\t$(Stat.PR.B$(i))\t$(stat.PR.Bse$(i))";
	separator 4;

	type;
	type $regression.ShortParaHeader3;
	separator 4;
	if (stat.pValue<0.0001) %A="<0.0001"; else %A=$(stat.pValue);
	type "$(Stat.COD)\t$(Stat.SD)\t$(stat.npts)\t%A";
	separator 4;
	type;
	type.EndResults();
	type.Redirection=SaveRedirection; // restore previous redirection.
	delete -v SaveRedirection;
	return 0;


[Tab.Operation.ErrorAsWeight.OnClick]
	PR!Tab.Operation.UseChiSquare=PR!Tab.Operation.ErrorAsWeight==0?1:0;

[Tab.Operation.FindXButn.Onclick]
	
	//type -b "Find X";
	
	if(run.LoadOC("OriginLab\FitToolsGetXfromY.C")!=0)
	{
		type -b "$Regression.CFileNoLoad";          
		return 1;
	}
	run.section(DBRoutines,SaveAndSetAtOC);
	
	//////////////prepare for callin functions/////////////////
	//
		%A=create.data2$;
	//	limit %A;
	//
	//////////////////////////////////////////////////////////
	
		
	if(PR!Tab.Operation.FindYedit.v1 == 0/0)
		run.section(,ReportError,$Regression.LRNoY);
	else
	{
		%Z=stat.pr.a;
		loop(ii,1,stat.pr.order)
		{
			%Z=%Z, stat.pr.b$(ii);
		};
		PR!Tab.Operation.FindXedit.v1=PolyGetXfromY(PR!Tab.Operation.FindYedit.v1,"%A",%Z);
	}
	run.section(DBRoutines,RestoreAtOC);
	
[Tab.Operation.FindYButn.Onclick]
	
	//type -b "Find Y";
	
	
	if(PR!Tab.Operation.FindXedit.v1 == 0/0)
		run.section(,ReportError,$Regression.LRNoX);
	else
	{
		%Z=stat.pr.a;
		loop(ii,1,stat.pr.order)
		{
			%Z=%Z, stat.pr.b$(ii);
		};
		PR!Tab.Operation.FindYedit.v1=poly(PR!Tab.Operation.FindXedit.v1,%Z);
	}
	
		

	
[OnCancel]

	PR!Tab.Operation.StaticText$=$Regression.Calculate;
	PR!Tab.Operation.FindXedit.enable=0;
	PR!Tab.Operation.FindYedit.enable=0;
	PR!Tab.Operation.FindXButn.enable=0;
	PR!Tab.Operation.FindYButn.enable=0;
	PR!TAB.OPERATION.FINDXEDIT.V1$="";
	PR!TAB.OPERATION.FINDYEDIT.V1$="";
	return 0;