#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_num,
	type_str,
};
enum{
	cond_value_eq,
	cond_value_lt,
	cond_value_le,
	cond_value_gt,
	cond_value_ge,
	cond_value_ne,
};
enum{
	lookin_active_sheet,
	lookin_active_book,
	lookin_all_in_active_folder,
	lookin_recursive_in_active_folder,
	lookin_open_in_active_folder,
	lookin_all_in_project,
	lookin_specified,
};
//put additional include files here
#include <getnbox.h> // temp solution
#include <event_utils.h>/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF

/*------------------------------------------------------------------------------*
/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
/// Iris 7/16/2009 CHECK_ERROR_ON_ACRIVE_IF_PAGE_VALID_AND_IF_VALID_WORKSHEETPAGE
/// Iris 7/17/2009 QA80-13941-P2 FIND_WHAT_CANNOT_BE_EMPTY_KEEP_SAME_AS_FINDREPLACE_DLG
/// CPY 7/28/09 SCRIPT_BROKEN_IN_NEW_81_BUILDS_TO_PROCESS_SALES_DB_DATA
/// Iris 7/29/2009 WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
/// Sophy 8/11/2009 QA80-14091 v8.1091 TREAT_NUMERIC_INPUT_PARAMETER_AS_MISSING_IF_NOT_SPECIFIED
/// Sophy 8/12/2009 QA80-13941-P5 LESS_THAN_GREATER_THAN_DONT_SUPPORT_TOLERANCE
/// Sophy 9/3/2009 TOLERANCE_ONLY_AVAILABLE_WHEN_MANIPULATE_NUMERIC_DATA
///	Kenny 09/23/2009 QA81-13631-P1 FIND_REPLACE_SHOULD_SUPPORT_FIND_BLANK_CELLS
/// CPY 10/6/09 QA70-14418 REPLACE_MISSING_WITH_VAL_NEED_OPTION_TO_REPLACE_BLANK_AND_TEXT_AS_WELL
/// Kenny 10/16/2009 QA81-13631-P1 MORE_WORKS_ON_SUPPORT_FIND_REPLACE_WITH_BLANK
/// Kenny 10/16/2009 QA81-13631-P2 SUPPORT_FIND_REPLACE_WITH_BLANK_MISSING_TEXT
*------------------------------------------------------------------------------*/

//put your own support static functions here
#define STR_NODE_RANGE "rng"
enum{
	REPLACE_TYPE_NUMERIC = 0,
	REPLACE_TYPE_TEXT,
};

enum{
	REPLACE_CONDITION_EQUAL = 0,
	REPLACE_CONDITION_LESSTHAN,
	REPLACE_CONDITION_LESSTHAN_EQUAL,
	REPLACE_CONDITION_GREATER,
	REPLACE_CONDITION_GREATER_EQUAL,
	REPLACE_CONDITION_LESSTHAN_GREATER,
};

enum{
	MATCH_WHOLE_WORD_ONLY,
	MATCH_CASE,
};

/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
static void _update_range_control_show(TreeNode& trGetN)
{
	bool bShow = (lookin_specified == trGetN.lookin.nVal);
	trGetN.rng.Show = bShow;
}

/// Iris 7/29/2009 WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
/*
static int _get_active_page_type()
{
	Page pg = Project.Pages();
	if(pg)
		return pg.GetType();
	return EXIST_NONE;
}
*/
///end WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE

static int _get_lookin_options(int nSel)
{
	if( lookin_specified == nSel )
		return OPTION_SPECIFIED;	
	
	/// Iris 7/29/2009 WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
	//return get_lookin_options(nSel, _get_active_page_type());
	return get_lookin_options(nSel, EXIST_WKS); // can input wks type with hard code since this xf only for worksheet
	///end WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
}
///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF

static void _update_tree_show(TreeNode& trGetN)
{
	int nType = trGetN.type.nVal;
	if(nType == REPLACE_TYPE_NUMERIC)
	{
		trGetN.find_str.Show = false;
		trGetN.cond_str.Show = false;
		trGetN.replace_str.Show = false;
		trGetN.con_case.Show = false;
		trGetN.wildcards.Show = false; /// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		
		trGetN.find_value.Show = true;
		trGetN.cond_value.Show = true;
		trGetN.replace_value.Show = true;
		///Sophy 9/3/2009 TOLERANCE_ONLY_AVAILABLE_WHEN_MANIPULATE_NUMERIC_DATA
		//trGetN.tolerance.Show = true;
		if ( trGetN.cond_value.nVal == REPLACE_CONDITION_EQUAL || trGetN.cond_value.nVal == REPLACE_CONDITION_LESSTHAN_EQUAL
			|| trGetN.cond_value.nVal == REPLACE_CONDITION_GREATER_EQUAL )
			trGetN.tolerance.Show = true;
		else
			trGetN.tolerance.Show = false;
		///end TOLERANCE_ONLY_AVAILABLE_WHEN_MANIPULATE_NUMERIC_DATA
		trGetN.use_abs.Show = true;
		trGetN.keep_sign.Show = true;
		trGetN.set_missing.Show = true;
		//trGetN.hint_missing.Show = true; ///Sophy 8/16/2009, this node is not used, and should not be visible from GUI
	}
	else if(nType == REPLACE_TYPE_TEXT)
	{
		trGetN.find_str.Show = true;
		trGetN.cond_str.Show = true;
		trGetN.replace_str.Show = true;
		trGetN.con_case.Show = true;
		trGetN.wildcards.Show = true; /// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		
		trGetN.find_value.Show = false;
		trGetN.cond_value.Show = false;
		trGetN.replace_value.Show = false;
		trGetN.tolerance.Show = false;
		trGetN.use_abs.Show = false;
		trGetN.keep_sign.Show = false;
		trGetN.set_missing.Show = false;
		//trGetN.hint_missing.Show = false;///Sophy 8/16/2009, this node is not used, and should not be visible from GUI
	}
	///Sophy 9/3/2009 TOLERANCE_ONLY_AVAILABLE_WHEN_MANIPULATE_NUMERIC_DATA
	/////Sophy 8/12/2009 QA80-13941-P5 LESS_THAN_GREATER_THAN_DONT_SUPPORT_TOLERANCE
	//if ( trGetN.cond_value.nVal == REPLACE_CONDITION_EQUAL || trGetN.cond_value.nVal == REPLACE_CONDITION_LESSTHAN_EQUAL
		//|| trGetN.cond_value.nVal == REPLACE_CONDITION_GREATER_EQUAL )
		//trGetN.tolerance.Show = true;
	//else
		//trGetN.tolerance.Show = false;
	/////end LESS_THAN_GREATER_THAN_DONT_SUPPORT_TOLERANCE
	///end TOLERANCE_ONLY_AVAILABLE_WHEN_MANIPULATE_NUMERIC_DATA
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	trGetN.cond_str.Enable = !trGetN.wildcards.nVal;
	if( trGetN.wildcards.nVal )
		trGetN.cond_str.nVal = 1;
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
}

/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
//static void _get_param_values_from_tree(TreeNode& trGetN, int& type, double& find_value, string& find_str, int& cond_value, int& cond_str, double& replace_value, string& replace_str, 
//										double& tolerance, int& use_abs, int& keep_sign, int& set_missing, int& con_case, int &undo)
static void _get_param_values_from_tree(TreeNode& trGetN, int& lookin, int& type, double& find_value, string& find_str, int& cond_value, int& cond_str, double& replace_value, string& replace_str, 
										double& tolerance, int& use_abs, int& keep_sign, int& set_missing, int& con_case, int& skiplink, int& wildcards, int &undo)
///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
{
	type = trGetN.type.nVal;
	find_value = trGetN.find_value.dVal;
	find_str = trGetN.find_str.strVal; 
	cond_value = trGetN.cond_value.nVal;
	cond_str = trGetN.cond_str.nVal; 
	replace_value = trGetN.replace_value.dVal;
	replace_str = trGetN.replace_str.strVal;
	tolerance = trGetN.tolerance.dVal;
	use_abs = trGetN.use_abs.nVal;
	keep_sign = trGetN.keep_sign.nVal;
	set_missing = trGetN.set_missing.nVal;
	con_case = trGetN.con_case.nVal;
	undo = trGetN.undo.nVal;
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	lookin = trGetN.lookin.nVal; 
	skiplink = trGetN.skip_link.nVal;
	wildcards = trGetN.wildcards.nVal;
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF	
}

/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
//static int _do_wks_replace(Range& rng, int type, double find_value, string find_str, int cond_value, int cond_str, double replace_value, string replace_str, double tolerance, int use_abs, int keep_sign, int set_missing, int con_case, int undo)
static int _do_wks_replace(Range& rng, int type, double find_value, string find_str, int cond_value, int cond_str, double replace_value, string replace_str, double tolerance, int use_abs, int keep_sign, int set_missing, int con_case, int skiplink, int wildcards, int undo)
///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
{
	string strRange;
	int r1, r2, c1, c2;
	Datasheet ds;
	rng.GetRange(0, r1, c1, r2, c2, ds, &strRange);
	Worksheet wks = ds;
	if(!wks.IsValid())
		return XFERR_CURRENT_SELECTION_NOT_WKS;
	
	uint nOption = WKSREPL_REPLACE_MATCHING_PART_ONLY;
	if(type == REPLACE_TYPE_NUMERIC)
	{		
		switch(cond_value)
		{
		case REPLACE_CONDITION_EQUAL:
			nOption = WKSREPL_TEST_EQUAL;
			break;
		case REPLACE_CONDITION_LESSTHAN:
			nOption = WKSREPL_TEST_LESSTHAN;
			break;
		case REPLACE_CONDITION_LESSTHAN_EQUAL:
			nOption = WKSREPL_TEST_LESSTHAN|WKSREPL_TEST_EQUAL;
			break;
		case REPLACE_CONDITION_GREATER:
			nOption = WKSREPL_TEST_GREATER;
			break;
		case REPLACE_CONDITION_GREATER_EQUAL:
			nOption = WKSREPL_TEST_GREATER|WKSREPL_TEST_EQUAL;
			break;
		case REPLACE_CONDITION_LESSTHAN_GREATER:
			nOption = WKSREPL_TEST_GREATER|WKSREPL_TEST_LESSTHAN;
			break;
		default:
			return XFERR_INTOPTN_OUT_OF_RANGE;
			break;
		}
		
		if(use_abs)
			nOption |= WKSREPL_USE_ABSOLUTE_VALUE_IN_TEST;
	
		if(keep_sign)
			nOption |= WKSREPL_KEEP_ORIGINAL_SIGN_WHEN_TEST_RESULT_IS_TRUE;
	
		if(set_missing)
			nOption |= WKSREPL_SET_TO_MISSING_VALUE_WHEN_TEST_RESULT_IS_FALSE;
		
		/// Kenny 09/23/2009 QA81-13631-P1 FIND_REPLACE_SHOULD_SUPPORT_FIND_BLANK_CELLS
		/// Kenny 10/16/2009 QA81-13631-P2 SUPPORT_FIND_REPLACE_WITH_BLANK_MISSING_TEXT
		if ( NANUM == find_value )
		{
		/// End QA81-13631-P2 SUPPORT_FIND_REPLACE_WITH_BLANK_MISSING_TEXT
			Tree tr;
			TreeNode trGetN;
			XFExeContext*	pExeCtxt = Project.GetCurrentXFExeCtxt();
			if(pExeCtxt)
			{    
			    pExeCtxt->GetGUI(tr, trGetN);
			    //---- CPY 10/6/09 QA70-14418 REPLACE_MISSING_WITH_VAL_NEED_OPTION_TO_REPLACE_BLANK_AND_TEXT_AS_WELL
			    //if ( trGetN.find_value.strVal.IsEmpty() )
			    string str = trGetN.find_value.strVal;
			    /// Kenny 10/16/2009 QA81-13631-P1 MORE_WORKS_ON_SUPPORT_FIND_REPLACE_WITH_BLANK
			    // "" should be treat as finding blank
			    okutil_check_remove_quotes(&str);
			    /// End QA81-13631-P1 MORE_WORKS_ON_SUPPORT_FIND_REPLACE_WITH_BLANK
			    if(str.IsEmpty())
			    	nOption |= WKSREPL_NUMERIC_FIND_BLANK;
			    else
			    {
			    	/// Kenny 10/16/2009 QA81-13631-P2 SUPPORT_FIND_REPLACE_WITH_BLANK_MISSING_TEXT
			    	//if(str[0]=='\"') {str.TrimLeft('\"');str.TrimRight('\"');}
			    	//if(str == "--") // any other text value that is not -- will lead to NANUM but will not skip blank and text
			    	//{
			    		//ASSERT(find_value==NANUM);
			    		//nOption |= WKSREPL_NUMERIC_NANUM_SKIP_BLANK_TEXT;
			    	//}
			    	// if the text value is "--", that means we should skip balnk and text
			    	// otherwise it should be treated the same as what "-" means (missing/blank/text), 
			    	// in which case we should skip blank & text only when the condition is "!="
			    	if( str == "--" || REPLACE_CONDITION_LESSTHAN_GREATER == cond_value )
			    	{
			    		nOption |= WKSREPL_NUMERIC_NANUM_SKIP_BLANK_TEXT;
			    	}
			    	/// End QA81-13631-P2 SUPPORT_FIND_REPLACE_WITH_BLANK_MISSING_TEXT
			    }
			    //----
	
			    /// Kenny 10/16/2009 QA81-13631-P1 MORE_WORKS_ON_SUPPORT_FIND_REPLACE_WITH_BLANK
			    //if ( trGetN.replace_value.strVal.IsEmpty() )
			    string strReplace = trGetN.replace_value.strVal;
			    okutil_check_remove_quotes(&strReplace);
			    if( strReplace.IsEmpty() )
			    /// End QA81-13631-P1 MORE_WORKS_ON_SUPPORT_FIND_REPLACE_WITH_BLANK
			    	nOption |= WKSREPL_NUMERIC_REPL_BLANK;
			}
		}	/// Kenny 10/16/2009 QA81-13631-P2 SUPPORT_FIND_REPLACE_WITH_BLANK_MISSING_TEXT
		/// End FIND_REPLACE_SHOULD_SUPPORT_FIND_BLANK_CELLS
	}
	else if(type == REPLACE_TYPE_TEXT)
	{
		if(cond_str)
			nOption = WKSREPL_TEST_STR_MATCH_WHOLE_WORD;
		/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		/*else
			nOption = WKSREPL_TEST_STR_WILDCARD_MATCHING;*/
		if(wildcards)
			nOption |= WKSREPL_TEST_STR_WILDCARD_MATCHING;
		///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		
		if(con_case)
			nOption |= WKSREPL_TEST_STR_CASE_SENSITIVE;	
	}
	else
		return XFERR_INTOPTN_OUT_OF_RANGE;
	
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	if(skiplink)
		nOption |= WKSREPL_SKIP_LINKED_CELL;
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF	
	
	int nNumRanges = rng.GetNumRanges();
	for(int ii = 0; ii < nNumRanges; ii++)
	{
		if(type == REPLACE_TYPE_NUMERIC)
			rng.Replace(find_value, replace_value, nOption, ii, tolerance, undo);
		else
			rng.Replace(find_str, replace_str, nOption, ii, undo);
	}
	Page wksP;
	wks.GetParent(wksP);
	wksP.Refresh();
	return 0;
}

#define DO_WKS_REPLACE _do_wks_replace(drSub, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case, skiplink, wildcards, undo)


//------ Folger 01/18/08 QA70-10946 SUPPORT_MULTIPLE_SHEETS_OPERATION_WITH_SHEET_RANGE_LIST_CLASS
/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
//static int _do_replace(Range& rng, int type, double find_value, string find_str, int cond_value, int cond_str, double replace_value, string replace_str, double tolerance, int use_abs, int keep_sign, int set_missing, int con_case, int undo)
static int _do_replace(int lookin, Range& rng, int type, double find_value, string find_str, int cond_value, int cond_str, double replace_value, string replace_str, double tolerance, int use_abs, int keep_sign, int set_missing, int con_case, int skiplink, int wildcards, int undo)
///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
{
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	int nLookInRange = _get_lookin_options(lookin);
	
	if( OPTION_SPECIFIED == nLookInRange )
	{
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		SheetRangesList rngList(rng);
		int nNumSheet = rngList.GetNumSheets();
		
		int nRet = 0;
		for ( int ii=0; ii<nNumSheet; ++ii )
		{
			DataRange drSub;
			if ( rngList.GetWksSubRange(ii, drSub) )
			{
				/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
				//if ( (nRet = _do_wks_replace(drSub, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case, undo)) != 0)
				if ( (nRet = DO_WKS_REPLACE) != 0 )
				///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
					return nRet;
			}
		}	
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	}
	else if ( OPTION_ACTIVE_MATRIX_OBJECT == nLookInRange || OPTION_ACTIVE_SHEET == nLookInRange )
	{
		const bool bGetActiveMatrixObjectOnly = OPTION_ACTIVE_MATRIX_OBJECT == nLookInRange;
		DataRange 	drSub;
		Datasheet 	ds;
		bool 		bPerformInSelectedRange = false;		
		if ( get_active_sheet_range_ex(drSub, bPerformInSelectedRange, &ds, bGetActiveMatrixObjectOnly) )
		{
			int nRet = DO_WKS_REPLACE;
			if ( 0 == nRet && drSub.IsValid() )
			{
				Page pg = ds.GetPage();
				pg.Refresh(TRUE);
			}
		}
	}
	else
	{
		vector<string> vsBookNames;
		/// Iris 7/29/2009 WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
		//get_folder_pages_name(vsBookNames, nLookInRange, 0, NULL, NULL, _get_active_page_type());
		get_folder_pages_name(vsBookNames, nLookInRange, 0, NULL, NULL, EXIST_WKS); // can input wks type with hard code since this xf only for worksheet
		///end WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
		if( vsBookNames.GetSize() == 0 )
			return -1;
	
		for(int index = 0; index < vsBookNames.GetSize(); index++ )
		{
			Page pg(vsBookNames[index]);
			
			int nRet = -1;
			foreach(Layer lay in pg.Layers)
			{
				Datasheet ds(lay);
				DataRange drSub;
				drSub.Add("Range1", ds, 0, 0, -1, -1);
				nRet = DO_WKS_REPLACE;
			}
			
			if( 0 == nRet )
			{
				pg.Refresh(TRUE);
			}
		}
	}	
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	return 0;
}
//------


void wreplace(Range& rng, const int& type, const double& find_value, const string& find_str, const int& cond_value, const double& replace_value, const string& replace_str, const double& tolerance, const int& use_abs, const int& keep_sign, const int& set_missing, const int& cond_str, const int& con_case, const int& skip_link, const int& wildcards, const int& undo, const int& lookin)
{
	
	//------ Folger 01/18/08 QA70-10946 SUPPORT_MULTIPLE_SHEETS_OPERATION_WITH_SHEET_RANGE_LIST_CLASS
	//int nRet = _do_wks_replace(rng, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case);
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	//int nRet = _do_replace(rng, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case, undo);
	int nRet = _do_replace(lookin, rng, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case, skip_link, wildcards, undo);
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	//------
	if(nRet != 0)
		XF_THROW(nRet);
	
}

//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 wreplace_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return wreplace_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	bool bIsChangingData = lstrcmpi(lpcszNodeName, STR_NODE_RANGE) == 0? true:false;
	Range rData;
	TreeNode trRange = trGetN.rng;
	bOKEnable = okxf_resolve_tree_construct_range(&trRange, &rData);
	if(GETNE_ON_INIT == nEventID || bIsChangingData)
	{
		if(rData.IsValid())
		{
			string strRange;
			int r1, r2, c1, c2;
			Datasheet ds;
			rData.GetRange(0, r1, c1, r2, c2, ds, &strRange);
			Worksheet wks = ds;
			if(!wks.IsValid())
			{
				strErrMsg = XFERR_CURRENT_SELECTION_NOT_WKS;
				bOKEnable = false;
			}
		}
	}
	
	/// Iris 7/17/2009 QA80-13941-P2 FIND_WHAT_CANNOT_BE_EMPTY_KEEP_SAME_AS_FINDREPLACE_DLG
	string strFindWhat;
	if( type_num == trGetN.type.nVal )
	{
		///Sophy 8/11/2009 QA80-14091 v8.1091 TREAT_NUMERIC_INPUT_PARAMETER_AS_MISSING_IF_NOT_SPECIFIED
		//In SR4 & SR5, if input parameter of double type is not specified, in GUI it will be filled with "<unassigned>", while in SR6, it just leave it totally empty
		//and allow user to find empty, in all these cases, SR4&SR5&SR6 don't check the input but all treat it as missing value while finding and replacing values.
		//In 8.1, if not specified, it displayes like "--", so here we should keep old logic to allow user find unassigned value and treat it as NANUM
		//strFindWhat = trGetN.find_value.IsEmpty()? "" : (string)trGetN.find_value.dVal;
		strFindWhat = trGetN.find_value.IsEmpty()? (string)NANUM : (string)trGetN.find_value.dVal;
		///end TREAT_NUMERIC_INPUT_PARAMETER_AS_MISSING_IF_NOT_SPECIFIED
	}
	else
	{
		strFindWhat = trGetN.find_str.IsEmpty()? "" : trGetN.find_str.strVal;
	}
	strFindWhat.TrimLeft();
	strFindWhat.TrimRight();
	if( 0 == strlen(strFindWhat) )
	{
		strErrMsg = IDS_FIND_WHAT_CANNOT_EMPTY;
		bOKEnable = false;
	}
	///end FIND_WHAT_CANNOT_BE_EMPTY_KEEP_SAME_AS_FINDREPLACE_DLG
	
	_update_tree_show(trGetN);
	
	if(GETNE_ON_APPLY == nEventID)
	{
		int type, cond_value, cond_str, use_abs, keep_sign, set_missing, con_case, undo;
		double find_value, replace_value, tolerance;
		string find_str, replace_str; 
		/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		//_get_param_values_from_tree(trGetN, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign,set_missing, con_case, undo);
		int lookin, skiplink, wildcards;
		_get_param_values_from_tree(trGetN, lookin, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign,set_missing, con_case, skiplink, wildcards, undo);
		///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		//------ Folger 01/18/08 QA70-10946 SUPPORT_MULTIPLE_SHEETS_OPERATION_WITH_SHEET_RANGE_LIST_CLASS
		//int nRet = _do_wks_replace(rData, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case);
		/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		//int nRet = _do_replace(rData, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case, undo);
		int nRet = _do_replace(lookin, rData, type, find_value, find_str, cond_value, cond_str, replace_value, replace_str, tolerance, use_abs, keep_sign, set_missing, con_case, skiplink, wildcards, undo);
		///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
		//------
		if(nRet != 0)
		{
			strErrMsg = nRet;
			bOKEnable = false;
		}	
		///Sophy 8/10/2009 QA80-13941-P3 REPLACE_NUMERIC_VALUE_WITH_SET_MISSING_WHEN_CONDITION_IS_FALSE_CAUSE_WHOLE_COLUMN_AS_MISSING_VALUES
		else //_do_replace successfully executed, no need to execute the body of OnApplyButton, which will call _do_replace again and result in wrong result.
		{
			NOTIFY_APPLY_EVENT_NO_CAUSE_CALL_BODY
			bOKEnable = false;
		}
		///end REPLACE_NUMERIC_VALUE_WITH_SET_MISSING_WHEN_CONDITION_IS_FALSE_CAUSE_WHOLE_COLUMN_AS_MISSING_VALUES
	}
	return false;

}

int wreplace_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 wreplace_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	/// Iris 7/14/2009 QA80-13941 SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	if(0 == nGetNDialog) // dialog is not used
	{
		if(trGetN.wildcards.nVal && !trGetN.cond_str.nVal)
			trGetN.cond_str.nVal = 1;
	}
	///end SUPPORT_MORE_OPTIONS_FOR_WREPLACE_AND_MREPLACE_XF
	//---- CPY 7/28/09 SCRIPT_BROKEN_IN_NEW_81_BUILDS_TO_PROCESS_SALES_DB_DATA
	/*
	/// Iris 7/16/2009 CHECK_ERROR_ON_ACRIVE_IF_PAGE_VALID_AND_IF_VALID_WORKSHEETPAGE
	Page pg = Project.Pages();
	if( !pg ||EXIST_WKS != pg.GetType() )
	{
		warning_msg_box(CER_NO_WORKSHEET, false);
		nRet = XFEVT_ABORT;
		return;
	}
	///end CHECK_ERROR_ON_ACRIVE_IF_PAGE_VALID_AND_IF_VALID_WORKSHEETPAGE
	*/
	//---- end SCRIPT_BROKEN_IN_NEW_81_BUILDS_TO_PROCESS_SALES_DB_DATA
	
	/// Iris 7/29/2009 WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
	Worksheet wks;
	okxf_resolve_string_get_origin_object(trGetN.rng.strVal, &wks);
	if( !wks )
	{
		warning_msg_box(CER_INVALID_WKS, false);
		nRet = XFEVT_ABORT;
	}	
	///end WREPLACE_AND_MREPLACE_XF_FIX_ALWAYS_ASSUME_REPLACE_RANGE_IS_ACTIVE_PAGE
	
}
//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 wreplace_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
