#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <event_utils.h>
#include <report_utils.h>
#include <o8dlg.h>

//put additional include files here

//put your own support static functions here


void PSS_ANOVA1(const int& CalcType, const double& Treatments, const double& CSS, const double& SD, const double& Alpha, const string& HypotSizes, const string& Power, const int& MoreSize, const vector& SizeCol, const int& MorePower, const vector& PowerCol, ReportTree& rt)
{
	
	/// *****		Terminate if Error Setting		***** ///
	string strData;
	int nMoreData;
	vector vDataCol;
	if (CalcType == CAL_POWER)
	{
		strData = HypotSizes;
		nMoreData = MoreSize;
		if (1 == nMoreData) vDataCol = SizeCol;
	}
	else
	{
		strData = Power;
		nMoreData = MorePower; 
		if (1 == nMoreData) vDataCol = PowerCol;
	}
	
	vector vSizePow;
	
	int nRet;
	nRet = pss_check_indata(SD, Alpha, CalcType, strData, nMoreData, vDataCol, vSizePow);
	if (nRet != OE_NOERROR)
		XF_THROW(nRet);
		
	/// *****		Sample Size and Power for ANOVA		***** ///
	ANOVAPowerOpts stPowerOpts;
	
	stPowerOpts.Level=Treatments;
	stPowerOpts.CSS=CSS;
	stPowerOpts.SD=SD;
	
	vector vOutput;
	vOutput.SetSize(vSizePow.GetSize());
	double dLowestPow;
	
	if(CalcType == CAL_SAMPLE_SIZE)
	{
		nRet = ocmath_anova_samplesize(Alpha, vSizePow, vSizePow.GetSize(), &stPowerOpts, vOutput);
		double nSizeLow = 2.0;
		ocmath_anova_powers1(Alpha,&nSizeLow,1,&stPowerOpts,&dLowestPow);//Count the lowest power
	}
	else
		nRet = ocmath_anova_powers1(Alpha, vSizePow, vSizePow.GetSize(), &stPowerOpts, vOutput);
	
	
	string strErr;
	switch(nRet)
	{
		case STATS_NO_ERROR:
			break;
		case STATS_ERROR_POWER_TOO_SMALL:
			strErr = pss_get_err_str(dLowestPow);
			break;
		case STATS_ERROR_SAMPLESIZE_TOO_LARGE:
			ocu_load_err_msg_str(FOOTNOTE_SAMPLE_SIZE_TOO_LARGE, &strErr);
			break;
		default:
			XF_THROW_EX(CER_UNKNOWN_ERROR_EX , nRet);
			break;
	}
		
	//Put result to report tree	
	string strFootNote;
	strFootNote.Format(_L("# Treatments = %g;  CSS of Means = %g;  SD = %g"), Treatments, CSS, SD);	
	strFootNote += strErr;
	vector vPower, vSampleSize;
	vPower = (CalcType == CAL_POWER) ? vOutput : vSizePow;
	vSampleSize = (CalcType == CAL_SAMPLE_SIZE) ? vOutput : vSizePow;
	rt_add_pss_res(rt, CalcType, Alpha, vSampleSize, vPower, strFootNote);
	
		
}

//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 PSS_ANOVA1_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return PSS_ANOVA1_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	return pss_general_event(trGetN, bOKEnable, strErrMsg);
	

}

int PSS_ANOVA1_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 PSS_ANOVA1_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	//------ fisher 11/16/07 ADD_MAP_ID_TO_CHM
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_PSS_ANOVA1);
	
	///Kyle 06/01/2009 USE_COMMA_AS_DELIMITER_IN_GOS
	if(is_xf_startup(nGetNDialog, dwCntrl))
	{
		trGetN.Power.strVal = check_set_sys_decimal_char(trGetN.Power.strVal, ".");
	}
	///End USE_COMMA_AS_DELIMITER_IN_GOS
	
	if ((dwCntrl&LTXF_THEME_USED) && nGetNDialog <= 0)
	{
		bool bOKEnable; 
		string strErrMsg;
		pss_general_event(trGetN, bOKEnable, strErrMsg);
		if (lstrcmp(strErrMsg,""))
			nRet = XFEVT_PROCEED_WITH_DLG;		
	}
	
}
//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 PSS_ANOVA1_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
