#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	append_none,
	append_integrate,
	append_baseline,
	append_peak_centers,
	append_peak_properties,
};
enum{
	mode_rows,
	mode_cols,
};
//put additional include files here

#ifdef	_FOR_SMART_LOADING_ONLY
#include <event_utils.h> /// Hong 02/03/10 QA80-15016 FIX_X_DATA_TYPE_CHANGE_WHEN_RECALCUTE
#include <..\OriginLab\WksOperation.h>
#include <..\OriginLab\WizOperation.h>

#include <..\OriginLab\XFCore.h>
#include <..\OriginLab\XFWiz.h>
#include <..\OriginLab\XFWizNavigation.h>
#include <..\OriginLab\XFWizManager.h>
#include <..\OriginLab\XFWizScript.h>
#include <..\OriginLab\XFWizDlg.h>
#include <..\OriginLab\FitWizCoreBase.h>

#include <..\OriginLab\Graph_utils.h>
#include <..\OriginLab\nlsf_utils.h>
#include <..\OriginLab\curve_utils.h>
#include <..\OriginLab\grobj_utils.h>
#include <..\OriginLab\XFGraph_Utils.h>
#endif	/// _FOR_SMART_LOADING_ONLY

#include <..\Originlab\XFunctionEx.h>
#include <..\OriginLab\XFWizard_utils.h>
#include <..\OriginLab\PAWizCore.h>
#include <..\OriginLab\PAWiz.h>

#include <..\OriginLab\WksColLabels.h>
/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 08/10/09 QA80-13998 ACTIVE_SUMMARY_BOOK_ONLY_IF_IN_ACTIVE_FOLDER		*
 *	Jasmine 08/24/09 QA81-14165 DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION	*
 *  Iris 9/4/2009 QA80-14165 SCRIPT_MODE_NEED_THROW_ERROR_FOR_PRO_OPTION_IN_REGULAR_BUILD*
 *  Iris 9/21/2009 FIX_NOT_SHOW_PRO_IN_J_AND_G_OS								*
 *	Folger 10/09/09 BETTER_INPUT_ERROR_HANDLING_IN_PAMULTIY						*
 *	Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
 *	Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS		*
 *	Kenny 12/24/2009 QA81-14875-P2 PROGRESS_BAR_IN_BUILTIN_TOOL_SHOULD_NOT_KEEP_TOPMOST
 *  Iris 2/02/2010 QA81-14165 ALSO_SHOW_PRO_IN_PRO_VERSION						*
 *	Folger 02/02/10 ADD_BOOK_SHEET_IDENTIFIER									*
 *	Folger 06/28/10 ORG-153-P3 FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
 *------------------------------------------------------------------------------*/

//put your own support static functions here
#define		ITEM_SEPARATOR					'|'
#define		STR_ITEM_SEPARATOR				"|"
#define		STR_EMPTY_COMBO					" " STR_ITEM_SEPARATOR

#define		E_STR_PEAK_PROPERTIES			"Peak Properties"
#define		STR_PEAK_PROPERTIES				_L(E_STR_PEAK_PROPERTIES)
#define		STR_PEAK_PROPERTIES_REG			STR_PEAK_PROPERTIES + " " + STR_PRO_POSTFIX

static bool _check_error(TreeNode& trGetN, string& strErrMsg)
{
	if ( get_input_xyrange_num(trGetN.iy) < 1 )
	{
		strErrMsg = XFERR_AT_LEAST_ONE_XYRANGE;
		return false;
	}
	
	//if ( !_is_append_sheet_aplpicable_with_theme(trGetN.theme.strVal, trGetN.append.nVal) )
	//{
		//strErrMsg = XFERR_NOT_APPLICABLE_APPEND_SHEET;
		//return false;
	//}
	
	if ( trGetN.number.nVal <= 0 )
	{
		strErrMsg.Format("%d:%s", CER_INVALID_POSITIVE_INTEGER, STR_STARTING_ROW_NUMBER);
		return false;
	}
	
	return true;
}

static	void	_check_append_pipe_char(string& str)
{
	if ( str.IsEmpty() )
		str = STR_EMPTY_COMBO;
	
	if ( str.Find(ITEM_SEPARATOR) < 0 )
		str += STR_ITEM_SEPARATOR;
}

static	string	_init_xf_theme_list(TreeNode& trGetN, vector<string>* pvsThemes = NULL)
{
	vector<string>		vsList;
	vector<string>		vsFilName;
	
	okutil_theme_get_class_settings("pa", &vsFilName, &vsList, THTYPE_ANALYSIS, true, false, false);

	string		strCombo;
	strCombo.SetTokens(vsList, ITEM_SEPARATOR);
	_check_append_pipe_char(strCombo);
	
	string	strDefault = strCombo.GetToken(0, ITEM_SEPARATOR);
	if ( trGetN )
	{
		trGetN.theme.SetAttribute(STR_COMBO_ATTRIB, strCombo);
		trGetN.theme.strVal = strDefault;
	}
	
	if ( NULL != pvsThemes )
		*pvsThemes = vsList;
	
	return strDefault;
}

static	void	_check_delete_op_and_outpus(int nIndex)
{
	OperationBase&		op = Project.Operations.GetOperation(nIndex);
	if ( op )
		op.DestroyInternal(TRUE);
}

///------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
static	void	_update_data_indentifiers(TreeNode& trGetN)
{
	batch_update_data_indentifiers(trGetN, "dataid");
}
///------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET

///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
static	void	_check_get_fit_stats_results(vector<string>& vsStats, vector<string>& vsLabels)
{
	char		szSheet[MAXLINE];
	LT_get_str(LTVAR_LAST_REPORT, szSheet, MAXLINE);
	Worksheet		wks;
	okxf_resolve_string_get_origin_object(szSheet, &wks);
	if ( wks )
	{
		Tree		tr;
		if ( wks.GetReportTree(tr, NULL, 0, GRT_TYPE_RESULTS, TRUE) )
		{
			TreeNode	trC1 = tr.RegStats.C1;
			if ( trC1 )
			{
				foreach ( TreeNode trNode in trC1.Children )
				{
					vsStats.Add(trNode.strVal);
					string	str;
					trNode.GetAttribute(STR_LABEL_ATTRIB, str);
					vsLabels.Add(str);
				}
			}
		}
	}
}

static	BOOL	_append_fit_stats_results(Worksheet& wksSummary, const vector<string>& vsStats, int nColStart, int nSize, int nRowStart)
{
	for ( int ii=0; ii<vsStats.GetSize(); ++ii )
	{
		if ( !batch_check_append_col_results(wksSummary, nColStart + ii, nSize, vsStats[ii], nRowStart) )
			return FALSE;
	}
	
	return TRUE;
}
///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS

#define		USING_PROGRESS_BOX

enum
{
	PABATCHPROCESS_FAILS_TO_GET_APPEND_SHEET = -100,
};

#define		CHECK_DELETE_ANALYSIS_OUTPUT(_nOpIndex)		\
			if ( bDeleteAnalysisOutputs )				\
				_check_delete_op_and_outpus(_nOpIndex);

static	int		_pa_batch_process(const XYRange&	iy
								, LPCSTR			lpcszTheme
								, LPCSTR			lpcszAppend
								, Worksheet& 		wksSummary
								, bool				bAppendByRows
								, bool				bClearSummarySheetBeforeAppend
								, bool				bDeleteAnalysisOutputs
								, LPCSTR			lpcszScriptAfterEachProcess
								, LPCSTR			lpcszScriptAtTheEnd
								, int				nRowStart
								, bool				bAppendLabelRows
								///------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
								, LPCSTR			lpcszLabel
								///------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
								///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
								, bool				bAddStatsResults
								///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
								)
{
	bool	bNeedAppend = NULL != lpcszAppend;
	
	DWORD	dwRules = DRR_NO_FACTORS | DRR_GET_DEPENDENT | DRR_NO_WEIGHTS;
	int		nNumData = iy.GetNumData(dwRules);
	
	XFBase		xf("pa");
	string		strTheme = lpcszTheme;		/// XFBase::SetArgs does not accpet LPCSTR, very ugly ...
	xf.SetArgs(1, strTheme);
	
	#ifdef		USING_PROGRESS_BOX
	bool		bHide = IsWindowVisible(GetWindow()) ? false : true;
	///Kenny 12/24/2009 QA81-14875-P2 PROGRESS_BAR_IN_BUILTIN_TOOL_SHOULD_NOT_KEEP_TOPMOST
	//progressBox	pb("", PBOX_TOPMOST, bHide);
	progressBox	pb("", PBOX_DEFAULT_STYLE, bHide);
	///End QA81-14875-P2 PROGRESS_BAR_IN_BUILTIN_TOOL_SHOULD_NOT_KEEP_TOPMOST
	pb.SetText(_L("Peak Analyzer Processing") + STR_THREE_DOTS, PBOXT_TITLE);
	pb.SetRange(0, nNumData);
	#endif		/// USING_PROGRESS_BOX
	
	///------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
	int		nLabelType = RCLT_INVALID;
	///------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
	
	///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
	int		nFitStatsColStart = 0;
	///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
	
	LT_execute("type -mb 0");
	///------ Folger 06/28/10 ORG-153-P3 FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
	BOOL	bSizeReady = FALSE;
	///------ End FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
	for ( int ii=0; ii<nNumData; ++ii )
	{
		DataRange	drSub;
		if ( !iy.GetSubRange(drSub, dwRules, ii) )
			return -1;
		
		string				strRange;
		drSub.GetRangeString(strRange);
		///------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
		if ( RCLT_INVALID == nLabelType )
		{
			Worksheet	wks;
			int			c1, c2;
			drSub.GetRange(wks, c1, c2);
			if ( wks )
			{
				nLabelType = batch_get_label_type_by_name(wks, lpcszLabel);
				ASSERT(RCLT_INVALID != nLabelType);
			}
		}
		///------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
		
		#ifdef		USING_PROGRESS_BOX
		if ( !bHide )
			pb.CheckReopen();
		string	strTips;
		strTips.Format("%d/%d %s", ii + 1, nNumData, strRange);
		if ( pb.Set(ii) )
			pb.SetText(strTips, PBOXT_MIDCENTER);
		else
			break;
		#endif		/// USING_PROGRESS_BOX
			
		int		nOpIndex = Project.Operations.GetCount();
		
		//xf.SetArgs(drSub, strTheme, 1);
		xf.SetArg(0, drSub);
		xf.Evaluate();
		
		///------ Folger 10/09/09 BETTER_INPUT_ERROR_HANDLING_IN_PAMULTIY
		int	nCode = 0;
		if( xf.GetLastError(nCode) )
			continue;
		///------ End BETTER_INPUT_ERROR_HANDLING_IN_PAMULTIY
		
		if ( bNeedAppend )
		{
			DWORD	dwCntrl = CPYT_EXTERN_UPDATE_ORIGIN | (bAppendLabelRows ? 0 : (wksSummary.GetPage().GetType() == EXIST_EXTERN_WKS ?  0 : CPYT_COPY_COLUMN_LABELS)) | CPYT_COPY_COLUMN_FORMAT | CPYT_COPY_COLUMN_DESIGNATIONS;
			int		nColOffset = 0;
			///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			vector<string>		vsFitStats;
			vector<string>		vsLabels;
			///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			if ( bAppendByRows )
			{
				dwCntrl |= CPYT_APPEND_BY_ROW;
				nColOffset = 1;
				///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
				if ( bAddStatsResults )
					_check_get_fit_stats_results(vsFitStats, vsLabels);
				///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			}

			char		szSheet[MAXLINE];
			LT_get_str(lpcszAppend, szSheet, MAXLINE);
			Worksheet		wksSrc;
			okxf_resolve_string_get_origin_object(szSheet, &wksSrc);
			if ( !wksSrc )
			{
				CHECK_DELETE_ANALYSIS_OUTPUT(nOpIndex)
				return PABATCHPROCESS_FAILS_TO_GET_APPEND_SHEET;
			}
			
			///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			int		nSummaryR1 = -1, nSummaryR2 = -1;
			///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			
			///------ Folger 06/28/10 ORG-153-P3 FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
			//if ( 0 == ii )
			if ( !bSizeReady )
			///------ End FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
			{
				///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
				wksSummary.GetBounds(nSummaryR1, 0, nSummaryR2, -1);
				///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
				
				if ( bClearSummarySheetBeforeAppend )
				{
					if ( bAppendByRows )
					{
						if ( 0 == wksSummary.GetNumCols() )
							wksSummary.SetSize(-1, wksSrc.GetNumCols());
						else
							wksSummary.ClearData(CWOD_SHRINK_COLS, 0, -1, nRowStart);
						wksSummary.SetBounds(0, 0, nRowStart - 1, -1);
						nSummaryR2 = nRowStart - 1;
					}
					else
					{
						wksSummary.SetSize(-1, 0);
					}
				}
				///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
				if ( bAddStatsResults )
				{
					nFitStatsColStart = wksSrc.GetNumCols() + 1;
					int		nColSize = nFitStatsColStart + vsFitStats.GetSize();
					if ( nColSize > wksSummary.GetNumCols() )
						wksSummary.SetSize(-1, nColSize);
				}
				///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
				if ( bAppendLabelRows )
				{
					///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
					//append_label_rows(wksSummary, wksSrc, nColOffset);
					append_label_rows(wksSummary, wksSrc, nColOffset, nSummaryR2 + 1);
					///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
					if ( nColOffset > 0 )
					{
						wksSummary.SetCell(nSummaryR2 + 1, 0, BATCH_STR_LABEL_DATASET_NAME);
						///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
						for ( int jj=0; jj<vsLabels.GetSize(); ++jj )
							wksSummary.SetCell(nSummaryR2 + 1, nFitStatsColStart + jj, vsLabels[jj]);
						
						wksSummary.GetBounds(nSummaryR1, 0, nSummaryR2, -1);
						///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
					}
				}
				else
				{
					if ( nColOffset > 0 )
					{
						Column	col1(wksSummary, 0);
						if ( col1 )
							col1.SetLongName(BATCH_STR_LABEL_DATASET_NAME);
						///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
						for ( int jj=0; jj<vsLabels.GetSize(); ++jj )
						{
							col1.Attach(wksSummary, nFitStatsColStart + jj);
							col1.SetLongName(vsLabels[jj]);
						}
						///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
					}
				}
				///------ Folger 06/28/10 ORG-153-P3 FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
				bSizeReady = TRUE;
				///------ End FIRST_XYRANGE_RECALCULATION__MAKE_WRONG_INCLUDE_FIT_STATS
			}
			
			wksSrc.CopyTo(wksSummary, 0, -1, 0, -1, -1 - nColOffset, -1, dwCntrl);
			
			if ( 0 != nColOffset )
			{
				int					nR1, nR2;
				wksSrc.GetBounds(nR1, 0, nR2, -1);
				///------ Folger 02/02/10 ADD_BOOK_SHEET_IDENTIFIER
				/////------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
				//string		str = strRange;
				//if ( RCLT_RANGE != nLabelType )
				//{
					//Column		colY;
					//XYRange		xy(drSub);
					//if ( xy.GetYColumn(colY) && colY )
					//{
						//colY.GetExtendedLabel(str, nLabelType);
					//}
				//}
				/////------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
				XYRange		xy(drSub);
				string		str = batch_get_data_identifier_string(xy, nLabelType);
				///------ End ADD_BOOK_SHEET_IDENTIFIER
				///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
				//vector<string>		vsValues(nR2 - nR1 + 1);
				//for ( int jj=0; jj<vsValues.GetSize(); ++jj )
					//vsValues[jj] = str;
				
				//Dataset			dsS(wksSummary, 0);
				//vector<string>	vs;
				//dsS.GetStringArray(vs);
				//vs.Append(vsValues);
				//dsS.PutStringArray(vs);
				int		nSize = nR2 - nR1 + 1;
				batch_check_append_col_results(wksSummary, 0, nSize, str, nSummaryR2 + 1);
				
				_append_fit_stats_results(wksSummary, vsFitStats, nFitStatsColStart, nSize, nSummaryR2 + 1);
				///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			}
			
			CHECK_DELETE_ANALYSIS_OUTPUT(nOpIndex)
		}
		
		if ( NULL != lpcszScriptAfterEachProcess && '\0' != *lpcszScriptAfterEachProcess )
			LT_execute(lpcszScriptAfterEachProcess);
	}
	LT_execute("type -mb -1");
	
	if ( bNeedAppend )
	{
		///------ Folger 08/10/09 QA80-13998 ACTIVE_SUMMARY_BOOK_ONLY_IF_IN_ACTIVE_FOLDER
		if ( is_same_folder(wksSummary.GetPage().GetFolder(), Project.ActiveFolder()) )
		///------ End ACTIVE_SUMMARY_BOOK_ONLY_IF_IN_ACTIVE_FOLDER
			wksSummary.CheckShowActivate();
	}
	
	if ( NULL != lpcszScriptAtTheEnd && '\0' != *lpcszScriptAtTheEnd )
		LT_execute(lpcszScriptAtTheEnd);
	
	return 0;
}

static	int		_get_goal_from_theme(LPCSTR lpcszTheme)
{
	PAWizManager	paWizMngr;
	PAWizTheme*		ppaWizTheme = (PAWizTheme*)paWizMngr.GetWizTheme();
	return ppaWizTheme->GetGoal(lpcszTheme);
}

static	int		_get_valid_append_indices_by_theme(vector<int>& vnIndices, LPCSTR lpcszTheme)
{
	vnIndices.SetSize(1);
	vnIndices[0] = append_none;
	int		nGoal = _get_goal_from_theme(lpcszTheme);
	switch ( nGoal )
	{
	case PA_GOAL_INTEGRATE_PEAK:
		vnIndices.Add(append_integrate);
		break;
		
	case PA_GOAL_CREATE_BASELINE:
		vnIndices.Add(append_baseline);
		break;
		
	case PA_GOAL_SUBTRACT_BASELINE:
		break;
		
	case PA_GOAL_FIND_PEAK:
		vnIndices.Add(append_baseline);
		vnIndices.Add(append_peak_centers);
		break;
		
	case PA_GOAL_FIT_PEAK:
		vnIndices.Add(append_baseline);
		vnIndices.Add(append_peak_properties);
		break;
		
	default:
		return -1;
	}
	
	return vnIndices.GetSize();
}

static	bool	_is_append_sheet_aplpicable_with_theme(LPCSTR lpcszTheme, int nAppend)
{
	vector<int>		vnIndices;
	_get_valid_append_indices_by_theme(vnIndices, lpcszTheme);
	vector<uint>		vnTmp;	
	return vnIndices.Find(vnTmp, nAppend) > 0;
}

static	string&		_original_append_list()
{
	static	string		str;
	return str;
}

static	void		_upate_append_list_by_theme(TreeNode& trGetN)
{
	vector<int>		vnIndices;
	_get_valid_append_indices_by_theme(vnIndices, trGetN.theme.strVal);
	
	vector<string>		vs;
	_original_append_list().GetTokens(vs, ITEM_SEPARATOR);
	for ( int ii=vs.GetSize()-1; ii>=0; --ii )
	{
		vector<uint>	vn;
		if ( vnIndices.Find(vn, ii) <= 0 )
			vs.RemoveAt(ii);
	}
	
	string		str;
	str.SetTokens(vs, ITEM_SEPARATOR);
	trGetN.append.SetAttribute(STR_COMBO_ATTRIB, str);		
	
	convert_int_vector_to_string_vector(vnIndices, vs);
	str.SetTokens(vs, ITEM_SEPARATOR);
	trGetN.append.SetAttribute(STR_INTMAP_ATTRIB, str);
	if ( vnIndices.GetSize() > 1 )
		trGetN.append.nVal = vnIndices[1];
	else
		trGetN.append.nVal = append_none;
	
	_check_hide_output_controls(trGetN);
}

static	void	_check_hide_output_controls(TreeNode& trGetN)
{
	BOOL	bShow = TRUE;
	if ( append_none == trGetN.append.nVal )
	{
		bShow = FALSE;
	}
	trGetN.dataid.Show = trGetN.number.Show = trGetN.hint_append.Show = trGetN.ow.Show = bShow;
	
	trGetN.remove.Enable = bShow;
	if ( !bShow )
		trGetN.remove.nVal = FALSE;
	
	///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
	_check_hide_fit_result(trGetN);
	///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
}

///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
static	void	_check_hide_fit_result(TreeNode& trGetN)
{
	trGetN.fitresult.Show = append_peak_properties == trGetN.append.nVal && mode_rows == trGetN.mode.nVal;
}
///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS

static	void	_check_disable_append_row_number(TreeNode& trGetN)
{
	trGetN.number.Enable = (trGetN.clear.nVal && mode_rows == trGetN.mode.nVal )? ENABLE : DISABLE;
}

static	void	_check_disable_append_label_rows(TreeNode& trGetN)
{
	trGetN.label.Enable = mode_rows == trGetN.mode.nVal ? ENABLE : DISABLE;
}


void paMultiY(const XYRange& iy, const string& theme, const int& append, const int& fitresult, const string& hint_append, Worksheet& ow, const int& remove, const string& dataid, const int& number, const int& clear, const int& label, const int& mode, const string& loopscript, const string& endscript)
{
	if ( iy.GetNumData(DRR_GET_DEPENDENT) < 1 )
		XF_THROW(XFERR_AT_LEAST_ONE_XYRANGE);
	
	vector<string>		vsThemes;
	_init_xf_theme_list(NULL, &vsThemes);
	if ( vsThemes.Find(theme) < 0 )
	{
		string		str;
		ocu_load_msg_str(XFERR_INVALID_THEME, &str, theme, NULL, "PA");
		XF_THROW(str);
	}
	
	if ( !_is_append_sheet_aplpicable_with_theme(theme, append) )
	{
		XF_THROW(XFERR_NOT_APPLICABLE_APPEND_SHEET);
	}
	
	LPCSTR	lpcszAppend = NULL;
	switch ( append )
	{
	case append_integrate:
		lpcszAppend = LTVAR_PA_INTEGRATE_PEAK;
		break;
		
	case append_baseline:
		lpcszAppend = LTVAR_PA_BASELINE;
		break;
		
	case append_peak_centers:
		lpcszAppend = LTVAR_PA_PEAK_CENTER;
		break;
		
	case append_peak_properties:
		lpcszAppend = LTVAR_PEAK_CHARACTERISTIC_REPORT;
		break;
		
	default:
		break;
	}
	
	//theme.Remove('<');
	//theme.Remove('>');
	
	if ( number <= 0 )
		XF_THROW_EX(CER_INVALID_POSITIVE_INTEGER, STR_STARTING_ROW_NUMBER);
	bool	bAppendLabelRows = label;
	if ( mode_cols == mode )
		bAppendLabelRows = false;
	
	///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
	bool	bAppendByRows = mode_rows == mode;
	bool	bAddStatsResults = bAppendByRows && append_peak_properties == append && fitresult;
	///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
	
	int		nRet = 0;
	if ( (nRet = _pa_batch_process(iy, theme, lpcszAppend, ow, bAppendByRows, clear, remove, loopscript, endscript, number - 1, bAppendLabelRows, dataid, bAddStatsResults)) < 0 )
	{
		if ( PABATCHPROCESS_FAILS_TO_GET_APPEND_SHEET == nRet )
			XF_THROW(XFERR_RESULT_SHEET_TO_APPEND_NOT_FOUND);
	}
}

//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 paMultiY_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return paMultiY_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if ( GETNE_ON_INIT == nEventID )
	{
		_upate_append_list_by_theme(trGetN);
		///------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
		_update_data_indentifiers(trGetN);
		///------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
		_check_disable_append_row_number(trGetN);
		_check_disable_append_label_rows(trGetN);
	}
	
	if ( NULL != lpcszNodeName )
	{
		if ( 0 == lstrcmp(lpcszNodeName, "append") )
		{
			_check_hide_output_controls(trGetN);
		}
		else if ( 0 == lstrcmpi(lpcszNodeName, "clear") )
		{
			_check_disable_append_row_number(trGetN);
		}
		else if ( 0 == lstrcmpi(lpcszNodeName, "mode") )
		{
			_check_disable_append_row_number(trGetN);
			_check_disable_append_label_rows(trGetN);
			///------ Folger 12/11/09 QA81-14733 PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
			_check_hide_fit_result(trGetN);
			///------ End PA_MULTIY_SUPPORT_APPEND_FIT_STATS_RESULTS
		}
		else if ( 0 == lstrcmpi(lpcszNodeName, "theme") )
		{
			_upate_append_list_by_theme(trGetN);
		}
		///------ Folger 11/06/09 QA81-14603 PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET
		else if ( 0 == lstrcmpi(lpcszNodeName, "iy") )
		{
			_update_data_indentifiers(trGetN);
		}
		///------ End PAMULTIY_SUPPORT_INSERT_DATA_INDENTIFIER_IN_SUMMARY_SHEET			
	}
	
	bOKEnable = _check_error(trGetN, strErrMsg);
	///Jasmine 08/24/09 QA81-14165 DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION
	if( bOKEnable && !is_pro_version() && append_peak_properties == trGetN.append.nVal )
	{
		trGetN.append.nVal = append_integrate;
		_check_hide_output_controls(trGetN);
		strErrMsg = XFERR_PRO_OPTION_WARNNING_MSG;
		strErrMsg += ":" + STR_PEAK_PROPERTIES_REG;
	}
	///End DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION
	
	return true;

}

int paMultiY_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 paMultiY_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( nGetNDialog > 0 )
	{
		trGetN.hint_append.Enable = ENABLE_READ_ONLY;
		trGetN.hint_append.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_USE_LABEL);
		
		string		str = _init_xf_theme_list(trGetN);
		/// Theme List should not be localized
		trGetN.theme.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);
		str.TrimRight();
		if ( str.IsEmpty() )
		{
			warning_msg_box(XFERR_AT_LEAST_ONE_THEME, true);
			nRet = XFEVT_ABORT;
			return;
		}
		
		//if ( !is_pro_version() ) /// Iris 2/02/2010 QA81-14165 ALSO_SHOW_PRO_IN_PRO_VERSION
		{
			/// remove Peak Properties option
			trGetN.append.GetAttribute(STR_COMBO_ATTRIB, str);
			///Jasmine 08/24/09 QA81-14165 DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION
			//int		nLastPipeChar = str.ReverseFind(ITEM_SEPARATOR);
			//if ( nLastPipeChar >= 0 )
			//{
				//str = str.Left(nLastPipeChar);
				//trGetN.append.SetAttribute(STR_COMBO_ATTRIB, str);
			//}
			/// Iris 9/21/2009 FIX_NOT_SHOW_PRO_IN_J_AND_G_OS
			/*
			string strPro = STR_PEAK_PROPERTIES;
			string strReg = STR_PEAK_PROPERTIES_REG;
			int nRet = str.Replace(strPro, strReg);
			ASSERT(1 == nRet);
			trGetN.append.SetAttribute(STR_COMBO_ATTRIB, str);
			trGetN.append.SetAttribute(STR_DISABLED_INDEX_START_ATTRIB, append_peak_properties);
			*/
			vector<string> vsCombo;
			str.GetTokens(vsCombo, '|');
			bool bReplaced = false;
			for( int ii=0; ii < vsCombo.GetSize(); ii++)
			{
				if( compare_string_localization(vsCombo[ii], E_STR_PEAK_PROPERTIES, true) )
				{
					vsCombo[ii] = STR_PEAK_PROPERTIES_REG;
					bReplaced = true;
				}				
			}
			if( bReplaced )
			{
				str.SetTokens(vsCombo, '|');
				trGetN.append.SetAttribute(STR_COMBO_ATTRIB, str);
			}
			///end FIX_NOT_SHOW_PRO_IN_J_AND_G_OS
			///End DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION
		}
		
		trGetN.append.GetAttribute(STR_COMBO_ATTRIB, _original_append_list());
	}
		
	///Jasmine 08/24/09 QA81-14165 DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION
	if( !is_pro_version() && append_peak_properties == trGetN.append.nVal )
	{
		/// Iris 9/4/2009 QA80-14165 SCRIPT_MODE_NEED_THROW_ERROR_FOR_PRO_OPTION_IN_REGULAR_BUILD
		if( 0 == nGetNDialog )
		{
			warning_msg_box(XFERR_PRO_OPTION_WARNNING_MSG, STR_PEAK_PROPERTIES_REG, false, 'E');
			nRet = XFEVT_ABORT;
			return;
		}
		else
		///end SCRIPT_MODE_NEED_THROW_ERROR_FOR_PRO_OPTION_IN_REGULAR_BUILD
		{
			trGetN.append.nVal = append_integrate;
		}
	}
	///End DISPLAY_BUT_DISABLE_PRO_OPTION_IN_REG_VERSION
	
	if ( nGetNDialog < 0 || 0 == nGetNDialog && !(XFEVT_PROCEED_WITH_DLG & dwCntrl) )		/// on OK
	{
		if ( append_none == trGetN.append.nVal )
			trGetN.ow.SetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, 0);
	}
}
//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 paMultiY_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
