#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	tail_two,
	tail_upperl,
	tail_lower,
};
#include <event_utils.h>
//put additional include files here

//put your own support static functions here

static double* _get_pointer(vector& v, int nSize)
{
	double* ptemp = NULL;	
	if(v)
	{	
		v.SetSize(nSize);
		ptemp = v;
	}	
	return	ptemp; 
}

///Kyle 11/25/2009 QA80-14591-P1 MAKE_MATRIX_DIMENSIONS_MATCH
static void _fill_missing_to_match_dimension(matrix& mat, int nCols)
{
	int nOldCols = mat.GetNumCols();
	int nRows = mat.GetNumRows();
	if( nOldCols > nCols )
	{
		ASSERT(false);
		return;
	}

	matrix mNANUM;
	mNANUM.SetSize(nRows, nCols - nOldCols);
	mNANUM = NANUM;

	mat.SetSize(nRows, nCols, true);
	mat.SetSubMatrix(mNANUM, nOldCols);
}
///End MAKE_MATRIX_DIMENSIONS_MATCH



void rowttest2(const Range& irng1, const Range& irng2, const double& mean, const int& tail, vector& statistic, vector& df, vector& prob)
{
	
	int nErr = check_1_data(irng1, true);
	if (CER_NO_ERROR == nErr)
		nErr += check_1_data(irng2, true);
	if (CER_NO_ERROR != nErr)
	{
		if (nErr == CER_COL_NUM_TOO_FEW_EX)
			XF_THROW_EX(nErr, 1)
		else
			XF_THROW(nErr);
	}
	
	matrix mat1, mat2;
	///Kyle 11/25/2009 QA80-14591-P4 KEEP_THE_EMPTY_ROWS_TO_BE_ONE_TO_ONE_CORRESPONDING_WITH_THE_SOURCE_DATA
	//if(!get_data_from_dr_to_mat(irng1, mat1) || !get_data_from_dr_to_mat(irng2, mat2))
	if(!get_data_from_dr_to_mat(irng1, mat1, false) || !get_data_from_dr_to_mat(irng2, mat2, false))
	///End KEEP_THE_EMPTY_ROWS_TO_BE_ONE_TO_ONE_CORRESPONDING_WITH_THE_SOURCE_DATA
		XF_THROW(XFERR_FAILED_GET_DATA_FROM_DATARANGE_TO_MAT);
	
	int nRows = min(mat1.GetNumRows(), mat2.GetNumRows());
	///Kyle 11/25/2009 QA80-14591-P1 MAKE_MATRIX_DIMENSIONS_MATCH
	//int nCols = min(mat1.GetNumCols(), mat2.GetNumCols());
	int nCols = max(mat1.GetNumCols(), mat2.GetNumCols());
	if( nCols > mat1.GetNumCols() )
		_fill_missing_to_match_dimension(mat1, nCols);
	else if( nCols > mat2.GetNumCols() )
		_fill_missing_to_match_dimension(mat2, nCols);
	///End MAKE_MATRIX_DIMENSIONS_MATCH
	
	mat1.SetSize(nRows, nCols, true);
	mat2.SetSize(nRows, nCols, true);
	
	double	*pStat = _get_pointer(statistic, nRows);	
	double	*pDF   = _get_pointer(df, nRows);
	double	*pProb  = _get_pointer(prob, nRows);
	
	nErr = ocmath_row_ttest2(nRows, nCols, mat1, mat2, mean, tail, pStat, pDF, pProb);
	if ( nErr != OE_NOERROR )
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nErr);
	
}

//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 rowttest2_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return rowttest2_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	return ttest_event(trGetN, bOKEnable, strErrMsg, _L("Mean1 - Mean2"));	
	

}

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