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

//put your own support static functions here




void kstest(const Range& irng, double& stat, double& df, double& prob)
{
	
	/// *****		Terminate if Error Setting		***** ///
	int nRet = check_1_data(irng);
	if (CER_NO_ERROR != nRet)
		XF_THROW(nRet);
	
	vector vData;
	DWORD dwPlotID;
	irng.GetData(DRR_NO_WEIGHTS|DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vData);
	
	
	if (vData.GetSize() < 3)	
		XF_THROW_EX(CER_DATA_TOO_FEW, 3);
	
	if (vec_is_equal(vData))
		XF_THROW(CER_ALL_ELEMENT_EQUAL);
	
	/// *****		Kolmogorov-Smirnov Normality Test		***** ///
	vector vPar(2);
	int nParaType = PARA_ESTIMATED;
	
	NormTestResults KSRes;
	nRet=stats_kolmogorov_smirnov_test(vData, nParaType, vPar, KSRes);
	if (nRet != STATS_NO_ERROR)
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	
	if (stat != NULL)	stat = KSRes.TestStat;
	if (df != NULL)	df = KSRes.DOF;
	if (prob != NULL)	prob = KSRes.Prob;		
				
}

//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 kstest_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return kstest_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErr = CER_NO_ERROR;
	
	Range rTemp;
	TreeNode trTemp = trGetN.irng;
	bool bRet;
	//------- Folger 12/20/07 FIX_BUG_OF_ERROR_CHECKING
	//if (!lstrcmp(lpcszNodeName, "irng") || nEventID == GETNE_ON_INIT)
		//bRet = okxf_resolve_tree_construct_range(&trTemp, &rTemp);
	//if(!bRet || check_1_data(rTemp) != CER_NO_ERROR)
		//nErr = CER_COL_NUM_NOT_ONE;
	if ( !lstrcmp(lpcszNodeName, "irng") || nEventID == GETNE_ON_INIT )
	{
		bRet = okxf_resolve_tree_construct_range(&trTemp, &rTemp);
		if ( !bRet || check_1_data(rTemp) != CER_NO_ERROR )
			nErr = CER_COL_NUM_NOT_ONE;
	}
	//------
	
	
	
	int nstatUse = -1, ndfUse = -1, nprobUse = -1;
	trGetN.stat.GetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, nstatUse);
	trGetN.df.GetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, ndfUse);
	trGetN.prob.GetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, nprobUse);
	
	if (nErr == CER_NO_ERROR && nstatUse + ndfUse + nprobUse == 0)
		nErr = CER_SUPPRESS_ALL_OUTPUT;
	
	if(CER_NO_ERROR != nErr)
	{
		strErrMsg = nErr;
		bOKEnable = false;
	}
	
	return true;
	

}

int kstest_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//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 kstest_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//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 kstest_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
