#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	xy_xcol,
	xy_ycol,
	xy_noxy,
};
enum{
	xlabel_none,
	xlabel_row1,
	xlabel_param,
};
enum{
	ylabel_none,
	ylabel_row1,
	ylabel_param,
};
#include <GetNBox.h> 
#include <event_utils.h>
#include <o8dlg.h>
#include <stats_utils.h> ///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
//put your own support static functions here

#include <..\Originlab\WksColLabels.h>		///------ Folger 03/25/09 CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 03/25/09 CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
 *	Folger 04/16/09 QA80-10817-P5 WARNING_MSG_SHOULD_NOT_THROW_TWICE_IN_BODY	*
 *	Folger 11/06/09 QA81-14609 XF_SUPPORT_LABEL_INDENTIFIER_FROM_LT				*
 *------------------------------------------------------------------------------*/

enum{
	NONE,
	FIRST_ROW,
	SYSTEM_OR_USER_PARAMETERS,
};

static void _get_all_show_parameters_values_col(Column& col, vector<string>& vStrValues)
{
	///------ Folger 03/25/09 CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
	//vector<int> vnIDs;
	//Worksheet wks;
	//col.GetParent(wks);
	//wks_get_show_labels( wks, vnIDs);
	//for(int ii = 0; ii < vnIDs.GetSize(); ii++)
	//{
		//string str = "";
		//if(vnIDs[ii] >= RCLT_PARAM && vnIDs[ii] < RCLT_PARAM + PARAMS_MAX_NUM)
		//{
			//col.GetParameter(str, vnIDs[ii]-RCLT_PARAM);
			//vStrValues.Add(str);
		//}
	//}
//
	//vector<string> vsValues, vStr;
	//get_user_parameters(col, vStr, vsValues, true);
	//vStrValues.Append(vsValues);
	get_all_shown_parameters_names_and_values(col, NULL, &vStrValues);
	///------ End CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
}

static bool _get_all_show_parameters_names(Worksheet& wks, vector<string>& vStrParams)
{
	///------ Folger 03/25/09 CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
	//vector<int> vnIDs;
	//wks_get_show_labels( wks, vnIDs);
	//for(int ii = 0; ii < vnIDs.GetSize(); ii++)
	//{
		//string str = "Parameters";
		//if(vnIDs[ii] >= RCLT_PARAM && vnIDs[ii] < RCLT_PARAM + PARAMS_MAX_NUM)
		//{
			//if(vnIDs[ii] != RCLT_PARAM)
				//str = str + " "+ ftoa(vnIDs[ii]-RCLT_PARAM+1);
			//
			//vStrParams.Add(str);
		//}
	//}
	//Column col(wks, 0);
	//if(!col)
		//return false;
	//
	//vector<string> vStr, vsValues;
	//get_user_parameters(col, vStr, vsValues, true);
	//vStrParams.Append(vStr);
	//return vStrParams.GetSize() > 0;
	Column col(wks, 0);
	if(!col)
		return false;
	
	return get_all_shown_parameters_names_and_values(col, &vStrParams, NULL) > 0 ? true : false;
	///------ End CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
}

static bool _init_tree_by_wks(Worksheet& wks, TreeNode& trGetN)
{
	vector<string> vStrParams;
	if(!_get_all_show_parameters_names(wks, vStrParams))
		return false;
	
	string strParamList;
	strParamList.SetTokens(vStrParams, '|');
	strParamList += "|";
	trGetN.param.SetAttribute(STR_COMBO_ATTRIB, strParamList);
	trGetN.param.strVal = vStrParams[0];
	
	return true;
}

static void _update_parameter_show_by_type(TreeNode& trGetN, int nParamType)
{
	switch(nParamType)
	{
	case NONE:
	case FIRST_ROW:
		trGetN.param.Show = false;
		break;
	case SYSTEM_OR_USER_PARAMETERS:
		trGetN.param.Show = true;
		break;
	default:
		trGetN.param.Show = false;
		break;
	}
}

static void _update_tree_show(TreeNode& trGetN)
{
	int bXY = trGetN.xy.nVal;
	switch(bXY)
	{
	case xy_xcol:
		
		trGetN.ycol.Show = true;
		trGetN.xlabel.Show = true;
		int nParamType = trGetN.xlabel.nVal;
		 _update_parameter_show_by_type(trGetN, nParamType);
		trGetN.xcol.Show = false;
		trGetN.xcol.nVal = 0;
		trGetN.ylabel.Show = false;
		trGetN.ylabel.nVal = ylabel_none;
		trGetN.tol.Show = true;
		trGetN.lr.Show = true;
		break;
		
	case xy_ycol:
		trGetN.xcol.Show = true;
		trGetN.ylabel.Show = true;
		int nParamType = trGetN.ylabel.nVal;
		 _update_parameter_show_by_type(trGetN, nParamType);
		trGetN.ycol.Show = false;
		trGetN.ycol.nVal = 0;
		trGetN.xlabel.Show = false;
		trGetN.xlabel.nVal = xlabel_none;
		trGetN.tol.Show = true;
		trGetN.lr.Show = true;
		break;
		
	case xy_noxy:
		trGetN.xcol.Show = false;
		trGetN.xcol.nVal = 0;
		trGetN.ylabel.Show = false;
		trGetN.ylabel.nVal = ylabel_none;
		trGetN.ycol.Show = false;
		trGetN.ycol.nVal = 0;
		trGetN.xlabel.Show = false;
		trGetN.xlabel.nVal = xlabel_none;
		trGetN.tol.Show = false;
		trGetN.param.Show = false;
		trGetN.lr.Show = false;
		break;
	default:
		break;
	}
	
}

static bool _check_worksheet(const Worksheet& wks, int* r1 = NULL, int* r2 = NULL, int* c1 = NULL, int* c2 =NULL)
{
	if (!wks.IsValid())
		return false;
	
	int iRow1, iRow2;
	int iCol1 = 0; 
	int iCol2 = wks.GetNumCols() - 1;
		
	wks.GetRange(iRow1, iRow2, iCol1, iCol2, false);

	if (r1)	*r1 = iRow1;
	if (r2)	*r2 = iRow2;
	if (c1)	*c1 = iCol1;
	if (c2)	*c2 = iCol2;

	if (iRow2-iRow1 < 0 || iCol2-iCol1 < 0)
		return false;
	
	return true;		
}

///------ Folger 11/06/09 QA81-14609 XF_SUPPORT_LABEL_INDENTIFIER_FROM_LT
//static int _get_one_parameter_values_wks(Worksheet& wks, vector& dParamValues, int nParamIndex = 0, int c1 = 0, int c2 = -1)
//{
	//if(c2 < 0)
		//c2 = wks.GetNumCols()-1;
	//
	//if(c2 < c1)
		//return -1;
	//
	//for(int nCol = c1; nCol <= c2; nCol++)
	//{
		//Column col(wks, nCol);
		//if(col)
		//{
			//vector<string> vStrValues;
			//_get_all_show_parameters_values_col(col, vStrValues);
			//if(nParamIndex < 0 || nParamIndex > vStrValues.GetSize()-1)
				//return -2;
			//
			//string str =  vStrValues[nParamIndex];
			//if(str.IsEmpty())
				//return -3;//Patamter has empty value
			//
			//dParamValues.Add(atof(str));
		//}
		//else
			//return -4;
	//}
	//
	//return 0;
//}
static	int		_get_one_param_values_in_wks(Worksheet& wks, vector& dParamValues, LPCSTR lpcszParam, int c1 = 0, int c2 = -1)
{
	if(c2 < 0)
		c2 = wks.GetNumCols()-1;
	
	if(c2 < c1)
		return -1;
	
	
	int nLabelType = get_label_type_by_name_or_indentifier(wks, lpcszParam);
	for(int nCol = c1; nCol <= c2; nCol++)
	{
		Column col(wks, nCol);
		if(col)
		{
			string		str;
			col.GetExtendedLabel(str, nLabelType);
			dParamValues.Add(atof(str));
		}
		else
			return -4;
	}
	
	return 0;
}
///------ End XF_SUPPORT_LABEL_INDENTIFIER_FROM_LT

///Arvin 01/24/08 QA70-10823 XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
//static int _check_even(vector& vxy, double dTol)
///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
//static bool _check_even(vector& vxy, double dTol)
static bool _check_even(vector& vxy, double& dFluctPerc, double dTolPerc)
///end IMPROVEMENT_OF_W2M
///end XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
{
	vector vdiff;
	vxy.Difference(vdiff);
	double min, max;
	vdiff.GetMinMax(min, max);
	///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
	//if(fabs(min - max) < dTol)
	//Calculate mean value
	double dMean;
	///Arvin 04/15/08 QA70-11107 IMPROVEMENT_FLUCTUATION_FORMULA
	//vxy.Sum(dMean);
	//dMean = dMean/(1.0*vxy.GetSize());
	vdiff.Sum(dMean);
	dMean = dMean/(1.0*vdiff.GetSize());
	///end IMPROVEMENT_FLUCTUATION_FORMULA
	//Calculate the fluctuation percent 
	///Arvin 04/15/08 QA70-11107 IMPROVEMENT_FLUCTUATION_FORMULA
	//dFluctPerc = round(fabs(min - max)/fabs(dMean)*100, 2);
	dFluctPerc = round(fabs(max - dMean)/fabs(dMean)*100, 2);
	///END IMPROVEMENT_FLUCTUATION_FORMULA
	if(dFluctPerc <= dTolPerc) 
	///end IMPROVEMENT_OF_W2M
		///Arvin 01/24/08 QA70-10823 XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
		//return -2;
		return true; 
		///end XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
	///Arvin 01/24/08 QA70-10823 XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES	
	//.return 0;
	return false;
	///end XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
}

///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
static bool _linear_regression(const vector& vx, const vector& vy, double& dParamA, double& dParamB)
{
	int nSize = vy.GetSize();
	vector 			vWeight(nSize);
	vWeight = 1;
	LROptions		sLROptions;
	FitParameter	arrstFitParameter[3];
	int nRet = stats_linear_fit(vx, vy, vWeight, sLROptions, arrstFitParameter);
	if(nRet != STATS_NO_ERROR)
		return false;
	dParamA = arrstFitParameter[0].Value;
	dParamB = arrstFitParameter[1].Value;
	return true;
}

static double _get_y_from_x_by_lr(double dParamA, double dParamB, double x)
{
	 //double dParamA, dParamB;
	 //if(!_linear_regression(vy, dParamA, dParamB))
	 //	return false;
	 
	 double y = dParamA + x * dParamB;
	 return y;
}

static bool _get_y_from_and_to_value_by_lr(vector& vy, double& dFrom, double& dTo)
{
	int nSize = vy.GetSize();
	vector vx;
	vx.Data(1, nSize);
	double dParamA, dParamB;
	if(!_linear_regression(vx, vy, dParamA, dParamB))
		return false;
	dFrom 	= _get_y_from_x_by_lr(dParamA, dParamB, vx[0]);
	dTo 	= _get_y_from_x_by_lr(dParamA, dParamB, vx[nSize-1]);
	return true;
}
///end IMPROVEMENT_OF_W2M

void w2m(const Worksheet& iw, const int& xy, const int& xlabel, const int& ylabel, const string& param, const int& xcol, const int& ycol, const double& tol, const int& lr, const int& trim, MatrixObject& om)
{
	
	///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
	if(tol < 0.0 || tol > 100)
		XF_THROW(W2M_WRONG_TOL_VALUE);
	///end IMPROVEMENT_OF_W2M
	//	Convert a worksheet to a matrix by cell-to-cell mapping
	if( (xy == xy_xcol && xcol == 1) || (xy == xy_ycol && ycol == 1))
		XF_THROW(XFERR_W2M_WRONG_OPTIONS);
		
	int r1, r2, c1, c2;
	if (!_check_worksheet(iw, &r1, &r2, &c1, &c2))
	{
		XF_THROW(CER_NO_NUMERIC_WORKSHEET);		
	}
	
	if(xy < xy_xcol || xy > xy_noxy)
	{
		XF_THROW(XFERR_INVLIAD_XY);
	}		
	
	bool xrow, yrow;
	xrow = (xlabel == xlabel_row1 && xy != xy_noxy) ? true : false;
	yrow = (ylabel == ylabel_row1 && xy != xy_noxy) ? true : false;
	bool bNoneX, bNoneY;
	bNoneX = (xlabel == xlabel_none && xy != xy_noxy) ? true : false;
	bNoneY = (ylabel == xlabel_none && xy != xy_noxy) ? true : false;
	//if columns of iw have different data type, should set them to be same 
	DataRange dr;
	dr.Add("Range", iw, r1, c1, r2, c2);
	int nType;
	if(!check_cols_in_range_with_same_datatype(dr, &nType))
	{
		string str1;
		str1 = (nType == FSI_DOUBLE) ? "double" : "long";
		XF_WARN_EX(CWARN_DIFF_COL_TYPE, str1);
		
		if(!om.SetInternalData(nType))
			XF_THROW(XFERR_FAIL_SET_DATA_TYPE);
	
	}
	
	double cmin = 1, cmax = c2 - c1 + 1, rmin = 1, rmax = r2 - r1 +1;
	
	if(xy==xy_xcol && xrow || xy==xy_ycol && yrow)
		rmax = rmax - 1;
	
	if(xy==xy_xcol && ycol || xy==xy_ycol && xcol)
		cmax = cmax - 1;
	
	
	int tempc1 =c1;	
	//The first row data are X/Y coordinates
	///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
	//double dTolerance = tol;
	double dTolPerc = tol;
	///end IMPROVEMENT_OF_W2M
	vector vxy;
	if(xy==xy_xcol && xrow || xy==xy_ycol && yrow)
	{	
		matrix mtmp;
		
		int nc1 = c1;
		if(xy==xy_xcol && ycol || xy==xy_ycol && xcol)
			nc1 = nc1 > 0 ? nc1 : nc1+1; 
			
		mtmp.CopyFromWks(iw, nc1, c2, r1, r1);   //sandy 2006-6-9
		mtmp.GetAsVector(vxy);
		///Arvin 01/24/08 QA70-10823 XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
		//int nRet = _check_even(vxy, dTolerance);
		//switch(nRet)
		//{
		//case -1: //all values are same
			//XF_WARN(XFWARN_1ST_ROW_ALL_SAME);
			//break;
		//case -2: //not even space
			//XF_WARN(CWARN_1ST_ROW_UNEVEN_SPACE);
			//break;
		//case 0:
			//vxy.GetMinMax(cmin, cmax);	
			//break;
		//}
		///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
		//if(!_check_even(vxy, dTolerance))
		//{
		//	XF_THROW(CWARN_1ST_ROW_UNEVEN_SPACE);	
		//}
		//vxy.GetMinMax(cmin, cmax);
		double dFluctPerc;
		if(!_check_even(vxy, dFluctPerc, dTolPerc))
		{
			string strPerc = ftoa(dFluctPerc) + "%";
			if(xrow)
			{
				XF_THROW_EX(W2M_X_DATA_UNEVEN_SPACE, strPerc);
			}
			else
				XF_THROW_EX(W2M_Y_DATA_UNEVEN_SPACE, strPerc);
		}
		///Do linear regression
		if(lr)
		{
			if(!_get_y_from_and_to_value_by_lr(vxy, cmin, cmax))
				XF_THROW(W2M_DO_LR_FAILED);	
		}
		else
		{
			cmin = vxy[0];
			int nTo = vxy.GetSize()-1;  
			cmax = vxy[nTo];
		}
		///END IMPROVEMENT_OF_W2M
		///end XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
		r1++;
	}
	
	//The first column data are X/Y coordinates.
	if(xy==xy_xcol&&ycol || xy==xy_ycol&&xcol)
	{
		matrix mtmp;
		int nr1 = r1;
		if(xy==xy_xcol&&xrow || xy==xy_ycol&&yrow)
			nr1 = nr1 > 0 ? nr1 : nr1+1; 
			
		mtmp.CopyFromWks(iw, tempc1, tempc1, nr1, r2);
		if(iw.Columns(tempc1).GetInternalData() == FSI_COMPLEX)
			XF_THROW(XFERR_X_OR_Y_COMPLEX);
		
		mtmp.GetAsVector(vxy);
		///Arvin 01/24/08 QA70-10823 XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
		//int nRet = _check_even(vxy, dTolerance);
		//switch(nRet)
		//{
		//case -1: //all values are same
			//XF_WARN(XFWARN_1ST_COL_ALL_SAME);
			//break;
		//case -2: //not even space
			//XF_WARN(CWARN_1ST_COL_UNEVEN_SPACE);
			//break;
		//case 0:
			//vxy.GetMinMax(rmin, rmax);	
			//break;
		//}
		///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
		//if(!_check_even(vxy, dTolerance))
		//{
		//	XF_WARN(CWARN_1ST_COL_UNEVEN_SPACE);	
		//}
		//vxy.GetMinMax(rmin, rmax);
		double dFluctPerc;
		if(!_check_even(vxy, dFluctPerc, dTolPerc))
		{
			string strPerc = ftoa(dFluctPerc) + "%";
			if(xcol)
			{
				XF_THROW_EX(W2M_X_DATA_UNEVEN_SPACE, strPerc);
			}
			else
				XF_THROW_EX(W2M_Y_DATA_UNEVEN_SPACE, strPerc);	
		}
		if(lr)
		{
			if(!_get_y_from_and_to_value_by_lr(vxy, rmin, rmax))
				XF_THROW(W2M_DO_LR_FAILED);	
		}
		else
		{
			rmin = vxy[0];
			int nTo = vxy.GetSize();  
			rmax = vxy[nTo-1];
		}
		///END IMPROVEMENT_OF_W2M
		///end XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
		c1++;
	}
	//User selected parameter data are x/y coordinates.
	if(xy==xy_xcol && !xrow && !bNoneX || xy==xy_ycol && !yrow && !bNoneY)
	{
		///------ Folger 11/06/09 QA81-14609 XF_SUPPORT_LABEL_INDENTIFIER_FROM_LT
		//vector<string> vStrParams;
		//if(!_get_all_show_parameters_names(iw, vStrParams))
			//XF_THROW(XFERR_WORKSHEET_HAS_NO_SHOW_PARAMS);
		
		//int nParamIndex;
		//if(xy==xy_xcol)
			//nParamIndex = vStrParams.Find(param);
		//else
			//nParamIndex = vStrParams.Find(param);
		
		//if(nParamIndex < 0 || nParamIndex >= vStrParams.GetSize())
		//{
			//XF_WARN(XFWARN_PARAMETER_INVALID);
		//}
		//else		///------ Folger 04/16/09 QA80-10817-P5 WARNING_MSG_SHOULD_NOT_THROW_TWICE_IN_BODY
		//{
			//vector dParamValues;
			//int nRet = _get_one_parameter_values_wks(iw, dParamValues, nParamIndex, c1, c2);
		vector dParamValues;
		int	nRet = _get_one_param_values_in_wks(iw, dParamValues, param, c1, c2);
		///------ End XF_SUPPORT_LABEL_INDENTIFIER_FROM_LT
		if(nRet == -3)
		{
			XF_WARN(XFWARN_PARAMETER_HAS_EMPTY); //Has empty value
		}
		///------ Folger 04/16/09 QA80-10817-P5 WARNING_MSG_SHOULD_NOT_THROW_TWICE_IN_BODY
		//else if(nRet == -2)
		//{
			//XF_WARN(XFWARN_PARAMETER_INVALID);   //Select invalid parameter
		//}
		///------ End WARNING_MSG_SHOULD_NOT_THROW_TWICE_IN_BODY
		else 
		{
			///Arvin 01/24/08 QA70-10823 XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
			//int nRet = _check_even(dParamValues, dTolerance);
			//switch(nRet)
			//{
			//case -1: //all values are same
				//XF_WARN(XFWARN_PARAMETER_ALL_SAME);
				//break;
			//case -2: //not even space
				//XF_WARN(XFWARN_PARAMETER_UNEVEN_SPACE);
				//break;
			//case 0:
				//dParamValues.GetMinMax(cmin, cmax);	
				//break;
			//}
			///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
			//if(!_check_even(dParamValues, dTolerance))
			//{
			//	XF_WARN(XFWARN_PARAMETER_UNEVEN_SPACE);	
			//}
			//dParamValues.GetMinMax(cmin, cmax);
			double dFluctPerc;
			if(!_check_even(dParamValues, dFluctPerc, dTolPerc))
			{
				string strPerc = ftoa(dFluctPerc) + "%";
				if(xy==xy_xcol)
				{
					XF_THROW_EX(W2M_X_DATA_UNEVEN_SPACE, strPerc);
				}
				else
					XF_THROW_EX(W2M_Y_DATA_UNEVEN_SPACE, strPerc);	
			}
			if(lr)
			{
				if(!_get_y_from_and_to_value_by_lr(dParamValues, cmin, cmax))
					XF_THROW(W2M_DO_LR_FAILED);	
			}
			else
			{
				cmin = dParamValues[0];
				int nTo = dParamValues.GetSize();  
				cmax = dParamValues[nTo-1];
			}
			///end IMPROVEMENT_OF_W2M
			///end XY_MAP_SHOULD_SUPPORTS_SAME_CORDINATES_VALUES
		}
	}
	
	if(xy==xy_xcol && bNoneX || xy==xy_ycol && bNoneY)
	{
		cmin = 1.0;
		cmax = 10;
	}
	
	om.SetNumRows(r2-r1+1);
	om.SetNumCols(c2-c1+1);
	matrixbase& mat = om.GetDataObject();
	
	bool bRet = mat.CopyFromWks(iw, c1, c2, r1, r2);
	
	if(trim)
	{
		mat.Transpose();
		mat.RemoveEmptyRows();
		mat.Transpose();
		mat.RemoveEmptyRows();
	}
	
	if(xy==xy_ycol)
	{
		mat.Transpose();
		if(!om.SetXY(rmin, cmin, rmax, cmax))
			XF_THROW(XFERR_SET_XYMAP_FAILED);
	}
	else if(xy==xy_xcol)
	{
		if(!om.SetXY(cmin, rmin, cmax, rmax))
			XF_THROW(XFERR_SET_XYMAP_FAILED);
	}
	else
	{
		if(!om.SetXY(cmin, rmin, cmax, rmax))
			XF_THROW(XFERR_SET_XYMAP_FAILED);
	}
	
	if (!bRet)
	{
		XF_THROW(XFERR_FAIL_TO_COPY_FROM_WKS_TO_MAT);
	}
	
}

//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 w2m_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return w2m_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	int nErrCode = CER_NO_ERROR;
	Worksheet	wks;
	okxf_resolve_string_get_origin_object(trGetN.iw.strVal, &wks);
	if(!_check_worksheet(wks))
	{
		nErrCode = CER_NO_NUMERIC_WORKSHEET;
	}
	
	///Arvin 02/18/08 QA70-11107 IMPROVEMENT_OF_W2M
	if(trGetN.tol.dVal < 0 || trGetN.tol.dVal > 100)
	{
		nErrCode = W2M_WRONG_TOL_VALUE;	
	}
	///end IMPROVEMENT_OF_W2M
	if (CER_NO_ERROR != nErrCode)
	{
		strErrMsg = nErrCode;
		bOKEnable = false;
	}
	
	bool bIsChangingWks = lstrcmpi(lpcszNodeName,"iw") == 0? true:false;
	if(GETNE_ON_INIT == nEventID || bIsChangingWks)           
		_init_tree_by_wks(wks, trGetN);
	_update_tree_show(trGetN);
	
	vector<string> vStrParams;
	bool bShow = (trGetN.xlabel.Show && trGetN.xlabel.nVal == SYSTEM_OR_USER_PARAMETERS) ||
		             (trGetN.ylabel.Show && trGetN.ylabel.nVal == SYSTEM_OR_USER_PARAMETERS);
	if(!_get_all_show_parameters_names(wks, vStrParams) && bShow)
	{
		trGetN.param.Enable = DISABLE;		///------ Folger 03/25/09 CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
		bOKEnable = false;
		strErrMsg = XFERR_WORKSHEET_HAS_NO_SHOW_PARAMS;
	}
	///------ Folger 03/25/09 CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
	else
	{
		trGetN.param.Enable = ENABLE;
	}
	///------ End CLEAN_UP_CODES_ABOUT_GET_ALL_SHWON_PARAMETER_NAMES_AND_VALUES
	
	return true;
	

}

int w2m_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 w2m_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//------ fisher 11/28/07 ADD_MAP_ID_TO_CHM
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_w2m);
}
//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 w2m_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
