#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

#include <event_utils.h>		///------ Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG

//---------CPY 1/25/2008 created this XF
/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
 *	Folger 10/20/2010 ORG-1286-P1 MORE_TOOLS_FAILED_TO_COPY_SAMPLING_INTERVAL_FROM_INPUT
 *------------------------------------------------------------------------------*/

void fitpoly(const XYRange& iy, const int& polyorder, vector& coef, XYRange& oy, int& N, double& AdjRSq, double& RSqCOD, vector& err)
{
	
	vector vInputX, vInputY, vOutputY;
	int nSize;
	
	// Copy X, Y data from XYRange into vectors
	if( !iy.GetData(vInputY, vInputX) )
		XF_THROW(XFERR_GET_DATA_FAILED);
	
	nSize=vInputX.GetSize();
	vector vCoeff, vErr;
	RegStats stats;
	int nErr = fitpoly(polyorder, vInputX, vInputY, nSize, vCoeff, vErr, &stats);
	if(nErr)
		XF_THROW(XFERR_FAIL_PERFORM_FITTING);
	
	N = stats.N;
	AdjRSq = stats.AdjRSq;
	RSqCOD = stats.RSqCOD;
	if(err)
		err = vErr;
	if(coef)
		coef = vCoeff;
	
	// Set the output dataset
	if(oy)
	{
		vOutputY.SetSize(nSize);
		ocmath_polynomial(vInputX, vOutputY, nSize, vCoeff, polyorder);
		///------ Folger 10/20/2010 ORG-1286-P1 MORE_TOOLS_FAILED_TO_COPY_SAMPLING_INTERVAL_FROM_INPUT
		//oy.SetData(&vOutputY, &vInputX, 0, NULL, DRS_SET_COL_DESIGNATIONS);
		set_xyrange_data_ex(oy, &vOutputY, &vInputX, 0, iy);
		///------ End MORE_TOOLS_FAILED_TO_COPY_SAMPLING_INTERVAL_FROM_INPUT
	}
	
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int fitpoly_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return fitpoly_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int fitpoly_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return -1;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void fitpoly_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///------ Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
	if ( nGetNDialog > 0 )		/// open dialog
		check_convert_optional_outputs_to_new(trGetN);
	///------ End CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int fitpoly_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
