#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	menu_single,
	menu_multiple,
};

#include <GetNBox.h>
#include <xfbase.h>	// Hong 01/17/07 ADD_SPARKLINES
#include <iw_xf.h>
#include <..\originlab\fu_utils.h>// Hong 9/14/06 REMOVE_IMPORT_RELATED_TO_FU_UTILS
#include <..\originlab\AscImpOptions.h>
#include <..\originlab\theme_utils.h>
#include <..\originlab\Import_Utils.h>// Hong 10/24/06 FIX_COMPILE_ERROR
/// Hong 04/12/07 v8.0599 CENTRALIZE_IMPASC_TO_CALL_IWBASE
#include <..\originlab\iwbase.h>
//#include <..\originlab\iw_filter.h>
/// end CENTRALIZE_IMPASC_TO_CALL_IWBASE

///=========end include headers

///////////////////////////////////////////////////////////////////////
//////// modification log
/// CPY 12/5/06 LT_CALL_ALSO_SHOULD_OPEN_DLG_IF_NO_FILE_SPECIFIED
/// Hong 03/12/07 QA80-9418-S1 ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
/// Sim 03-17-2007 CHECK_ERROR_FOR_INVALID_RANGE
/// Sim 03-20-2007 FIX_APPEND_COMMENT_WHEN_REIMPORT
/// Hong 03/26/07 v8.0589b USE_NEW_GETNBOX_TO_FIX_UPDATE_BTN_FAIL
/// Hong 03/26/07 v8.0589b DISABLE_VARIABLE_RESTRICTION
/// Hong 03/27/07 v8.0590 UPDATE_USE_KEY_FOR_MENU_STRING
/// Hong 04/12/07 v8.0599 CENTRALIZE_IMPASC_TO_CALL_IWBASE
/// Hong 04/13/07 v8.0600 AVOID_DUPLICATE_SPARKLINE
/// Hong 04/17/07 v8.0602b FIX_RUNTIME_ERROR_IF_EMPTY_DATA_FILE
/// Hong 04/17/07 v8.0602b FIX_IMPASC_FAIL_REIMPORT_CHANGE_OPTION
/// Sim 04-26-2007 FIX_AVOID_TO_OPEN_XF_DLG_WITHOUT_FILENAME
/// Hong 04/28/07 QA80-9702 CLEARLY_NEW_OBJECT_NEEDED
/// Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
/// Sim 05-18-2007 REMOVE_UID_VARIABLE
/// Sim 05-24-2007 FIX_AVOID_TO_ADD_SPARKLINES_TWICE
/// Sim 05-24-2007 KEEP_RANGE_INFO_FOR_OUTPUT
/// Sim 05-24-2007 SUPPORT_CONVERT_OPTIONS_TO_FILTER
/// Sim 05-25-2007 MOVE_SPARKLINE_TO_OPTIONS_TREE
/// Sim 06-14-2007 FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
/// Sim 06-14-2007 FIX_GETN_IMPORT_BUTTON_NOT_WORK
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// CPY 7/11/2007 QA70-10010 MOST_RECENT_IMPORT_EXPORT_NEED_LTXF_FROM_MRU_MENU
/// Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
/// CPY 9/1/2007 QA70-10231 SHOW_THEME_IN_FILE_DIALOG
/// Sim 11-07-2007 CLEAN_LOCALIZATION_CODE
/// Folger 11/13/07 LOCALIZE_CUSTOM_BUTTON
/// Hong 11/21/07 QA80-10541 MATRIX_NOT_SUPPORT_STRART_NEW_ROWS_MODE
/// Hong 12/06/07 v8.0762 IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
/// Sim 03-11-2008 FIX_ALL_TEXT_FILE_FROM_SCRIPT_SHOULD_NOT_DUMP_ERR_MSG
/// Sim 04-30-2008 FIX_UPDATE_ASCIMPORT_OPTIONS_TO_SHEET_THEME
/// Hong 07/23/08 v8.0907 IMPROVE_IMPORT_ERROR_MSG
/// Sim 08-07-2008 v8.0918 QA80-11969 MULTI_ASC_IMP_SUPPORT_TEMPLATE
/// Sim 08-11-2008 v8.0919 QA80-11969 LIST_TEMPLATE_NAME
/// Folger 08/22/08 QA80-12074 ADD_UTILITY_FUNCTION_FOR_INITIAL_TEMPLATE_LIST
/// Folger 10/10/08 NO_NEED_TO_SAVE_SHEET_THEME_AFTER_IMPORT_ACSII_INTO_MATRIX_AND_EXCEL
/// Folger 11/21/08 QA80-12315 v8.0977 ASCII_IMPORT_SHOULD_NOT_PROVIDE_DEFAULT_COLUMN_FORMATS
/// Kyle 01/04/2009 QA70-12890-P4 CENTRALIZE_CODE_TO_GET_AND_UPDATE_CUSTOM_DATE_FORMAT
/// Hong 01/09/09 v8.0995 CORRECT_IMPASC_ERROR_RETURN_CHECKING
/// Hong 08/26/09 QA80-13839-P1 FIX_IMPASC_REIMPORT_APPEND_REDUNDANT_FILENAME_INTO_PG_COMMENT
/// Folger 09/29/09 QA80-14392 IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS
/// Folger 10/21/09 QA81-14506 FAILS_TO_IMPORT_CSV_FILE_IN_SAMPLES_FOLDER_UNDER_GOS_BY_DEFAULT
/// Folger 05/19/10 QA81-14392-P1 FAILED_TO_DUMP_NUMERIC_SEPARATOR_WHEN_GENERATE_SCRIPT_FROM_IMPASC
/// Folger 06/28/10 ORG-155 ASCII_IMPORT_SPEED_IMPROVE
/// Hong 08/08/08 QA80-11980 SAVE_IMPORT_EXTRACTED_VARIABLE_IN_USER_DEFINED_PARAMETERS
///////////////////////////////////////////////////////////////////////

//----- CPY 9/30/06 MUST_SHOW_FROM_MENU_AND_SIMPLE_NO_XF_DIALOG
enum { // menu variable
	MENU_OPTN_SINGLE_FILE = 0,
	MENU_OPTN_MULTIPLE_FILES,
	MENU_OPTN_OPTIONS,
};
	
#define MENU_OPTN_USE_WKS_SETTINGS MENU_OPTN_SINGLE_FILE
//-----

///---Sim 11-16-2006 RELIABLE_OK_ENABLE
static bool s_bFileValid = true;
static bool s_bRangeValid = true; ///---Sim 03-17-2007 CHECK_ERROR_FOR_INVALID_RANGE
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = s_bFileValid && s_bRangeValid; //also can be _OK_ENABLE = s_bFileValid && s_bSetting
///---END RELIABLE_OK_ENABLE
/// Hong 01/17/07 CLEAN_LACAL_FUNCTION_TO_STATIC
//int get_open_box_by_file_group(string& strFile, LPCSTR lpcszGroup, LPCSTR lpcszTitle, BOOL bMultiFiles);

static BOOL construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo)
/// end CLEAN_LACAL_FUNCTION_TO_STATIC
{
	int nCount = vsFileNames.GetSize();
	string strFileName, strTag, strInfo;
	ASCIMP ascimp; 
	//TreeNode trFile, trFileInfo; ///---Sim 11-08-2006 NO_NEED
	if ( trInfo )
		trInfo.RemoveChildrenWithPrefix();
	GETN_USE(trInfo)
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		strFileName = vsFileNames[nIndex];
		/// Hong 12/06/07 v8.0762 IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
		strFileName = cvt_predefined_path_to_real_path(strFileName);
		/// end IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
		if ( !strFileName.IsFile() )
			return false;
		///---Sim 11-08-2006 FIX_SET_TAGNAME_AS_FILE_NAME
		//string strTag = GetFileName(strFileName, true);
		//strTag.MakeValidCName('a');
		strTag.Format("file%d", nIndex+1);
		///---END FIX_SET_TAGNAME_AS_FILE_NAME
//		trFile = trInfo.AddNode(strTag);
//		trFile.SetAttribute(STR_LABEL_ATTRIB, GetFileName(strFileName, false));
//		trFileInfo = trFile.AddNode("FileInfo");
		strInfo.Format("File size: %s;", GetFileSize(strFileName));
		if(AscImpReadFileStruct(strFileName,&ascimp)==0)
		{
			strInfo.Format( "%s Number of columns detected: %d;",strInfo, ascimp.iNumColumns);
		}
		GETN_STR(strTag, GetFileName(strFileName, false), strInfo )
		GETN_READ_ONLY
//		GETN_MULTILINE_TEXT(INFO, "HeaderInfo", strInfo) 
	}
	return true;
}

///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
static bool file_name_event(TreeNode& trGetN, string& strErrMsg)
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.trfiles;
	
	// clean the trFiles on GUI
	trNode.RemoveChildrenWithPrefix();
	
	if ( !check_imported_file_name(strFileName, strErrMsg) )
		return false;
	
	StringArray vsFileNames;
	separate_multiline_files(strFileName, vsFileNames);
	if ( !construct_tree_setting(vsFileNames, trNode) )
	{
		// clean the trFiles on GUI
		trNode.RemoveChildrenWithPrefix();
		strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
		return false;
	}
	
	return true;
}
///---END SUPPORT_DND_OPEN_DIALOG

/// Hong 03/26/07 v8.0589b USE_NEW_GETNBOX_TO_FIX_UPDATE_BTN_FAIL
static int ascii_option_getn_event(TreeNode& tr, int nRow, int nEvent, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& getNContainer, string& strAux, string& strErrMsg)
{
	DECLARE_BUTTON_ENABLES
	
	if(nEvent == GETNE_ON_INIT)
	{
		//------ Folger 11/13/07 LOCALIZE_CUSTOM_BUTTON
		//tr.SetAttribute(STR_CUSTOM_BUTTONS_ATTRIB, "OK=Import|Cancel=Close|Update");
		vector<string> vsTmp(3);
		vsTmp[0] = "OK=" + _L("Import");
		vsTmp[1] = _L("Cancel") + '=' + _L("Close");
		vsTmp[2] = _L("Update");
		string strTmp;
		strTmp.SetTokens(vsTmp, '|');
		tr.SetAttribute(STR_CUSTOM_BUTTONS_ATTRIB, strTmp);
		//------ End LOCALIZE_CUSTOM_BUTTON
		//tr.SetAttribute(STR_CUSTOM_BUTTONS_ATTRIB, "OK|Close|Import|Update");
		return true;
	}
	
	if(nEvent == GETNE_ON_CUSTOM_BUTTON1)
	{
		return PEVENT_GETN_RET_TO_CLOSE;
	}
	
	return false;
}
/// end USE_NEW_GETNBOX_TO_FIX_UPDATE_BTN_FAIL

///Sophy 10/22/2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_EXCEL_CSV_IMPORT need to use it in impCSV.OXF, so centralize to theme_utils.h
/*
//----- CPY 9/1/2007 QA70-10231 SHOW_THEME_IN_FILE_DIALOG
static string _get_show_options_label(const TreeNode& trGetN)
{
	string str = STR_SHOW_OPT_DLG;
	string strTheme;
	int themeType = theme_get_display_name(trGetN, strTheme);
	if(themeType >= PDS_USER)
	///---Sim 11-07-2007 CLEAN_LOCALIZATION_CODE
		//str += ": " + strTheme;	
	{
		if ( themeType > PDS_USER )
			strTheme = GetLocalized(strTheme);
		str += ": " + strTheme;
	}
	///---END CLEAN_LOCALIZATION_CODE
	return str;
}
//-----
*/
///end SUPPORT_SAVE_SHEET_THEME_FOR_EXCEL_CSV_IMPORT

/// Hong 09/03/07 QA80-9932 FIX_REIMPORT_IMP_WIZ_FROM_CLICKBOARD_FAIL
bool _is_data_from_clickboard(LPCSTR LPCSZFullFileName)
{
	char sz[MAXFULLPATH];
	DWORD dw = GetTempPath(MAXFULLPATH, sz);
	if ( dw )
	{
		string strPath = GetFilePath(LPCSZFullFileName);
		if ( 0 == strPath.CompareNoCase(sz) )
			return true;
	}
	
	return false;
}

bool _update_clickboard_data_into_file(LPCSTR LPCSZFullFileName)
{
	string strClipboardText;
	GetClipboardText(strClipboardText);
	
	stdioFile filTemp(LPCSZFullFileName, file::modeCreate | file::modeWrite | file::shareExclusive | file::typeText );
	
	filTemp.WriteString(strClipboardText);
	filTemp.Close();
	
	return true;
}
/// end FIX_REIMPORT_IMP_WIZ_FROM_CLICKBOARD_FAIL

///---Sim 08-11-2008 v8.0919 QA80-11969 LIST_TEMPLATE_NAME
//------ Folger 08/22/08 QA80-12074 ADD_UTILITY_FUNCTION_FOR_INITIAL_TEMPLATE_LIST
/*
enum{
	TEMPLATE_WORKSHEET	= 0x0001,
	TEMPLATE_MATRIX		= 0x0002,
	TEMPLATE_PLOT		= 0x0004,
};
*/
//------ End ADD_UTILITY_FUNCTION_FOR_INITIAL_TEMPLATE_LIST

bool _init_template_list(TreeNode& trTemplate, DWORD dwType = TEMPLATE_WORKSHEET|TEMPLATE_MATRIX)
{
	//------ Folger 08/22/08 QA80-12074 ADD_UTILITY_FUNCTION_FOR_INITIAL_TEMPLATE_LIST
	/*
	StringArray saTemplates;
	if ( dwType & TEMPLATE_WORKSHEET )
	{
		GetFileNames(saTemplates, NULL, "*.otw", GFIF_FILE_RECURSIVE | GFIF_STRIP_START_PATH);
		GetFileNames(saTemplates, NULL, "*.ogw", GFIF_FILE_RECURSIVE | GFIF_STRIP_START_PATH);
	}
	if ( dwType & TEMPLATE_MATRIX )
	{
		GetFileNames(saTemplates, NULL, "*.otm", GFIF_FILE_RECURSIVE | GFIF_STRIP_START_PATH);
		GetFileNames(saTemplates, NULL, "*.ogm", GFIF_FILE_RECURSIVE | GFIF_STRIP_START_PATH);
	}
	if ( dwType & TEMPLATE_PLOT )
	{
		GetFileNames(saTemplates, NULL, "*.otp", GFIF_FILE_RECURSIVE | GFIF_STRIP_START_PATH);
		GetFileNames(saTemplates, NULL, "*.ogg", GFIF_FILE_RECURSIVE | GFIF_STRIP_START_PATH);
	}

	string	strAttr;
	if ( !trTemplate.GetAttribute(STR_COMBO_ATTRIB, strAttr) )
		return false;
	
	int nPos;
	nPos = strAttr.Find(STR_ATTR_BROWSE_COMBO_LIST_DELIMITER);
	if ( nPos >= 0 )
	{
		strAttr = strAttr.Left(nPos); // remove last recent list
	}
	
	strAttr += STR_ATTR_BROWSE_COMBO_LIST_DELIMITER;
	
	string strListString;
	strListString.SetTokens(saTemplates,'|');
	strListString.Insert(0,strAttr);
	
	trTemplate.ID = ONODETYPE_BROWSECOMBO;
	trTemplate.SetAttribute(STR_COMBO_ATTRIB, strListString);
	return true;
	*/
	return init_template_list(trTemplate, dwType);
	//------ End ADD_UTILITY_FUNCTION_FOR_INITIAL_TEMPLATE_LIST
}

DWORD _get_template_type(const TreeNode& trRange)
{
	int nPageType = EXIST_NONE;
	
	DataRange dr;
	if ( okxf_resolve_tree_construct_range(&trRange, &dr) )
	{
		int c1,c2; 
		Datasheet lyTarget;
		Page pgTarget;
		if ( GetRangeInfo(dr, lyTarget, pgTarget, c1, c2) )
		{
			nPageType = pgTarget.GetType();
		}
	}
	
	switch ( nPageType )
	{
	case EXIST_WKS:
		return TEMPLATE_WORKSHEET;
	case EXIST_MATRIX:
		return TEMPLATE_MATRIX;
	}
	
	return TEMPLATE_WORKSHEET|TEMPLATE_MATRIX;
}
///---END LIST_TEMPLATE_NAME

///------ Folger 09/29/09 QA80-14392 IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS
class ImportNuermicSeparatorSettingHelper
{
public:
	ImportNuermicSeparatorSettingHelper(LPCSTR lpcszFile)
	{
		m_strFile = lpcszFile;
		LT_get_var("@NS", &m_rr);
		LT_set_var("@NS", 1);
	}
	~ImportNuermicSeparatorSettingHelper()
	{
		LT_set_var("@NS", m_rr);
	}
	
	int		GetSeparator()
	{
		return check_get_numeric_separator_from_file(m_strFile);
	}
	
private:
	double	m_rr;
	string	m_strFile;
};

static	void	_check_update_numeric_separator_setting_from_file(TreeNode& trGetN)
{
	string		strTheme;
	tree_get_theme_file_name(trGetN, strTheme);
	if ( theme_is_factory_default(strTheme) )
	{
		TreeNode	trNumericSeparator = tree_get_node_by_id(trGetN, IDE_ASCIMP_NUMSEP, true);
		if ( trNumericSeparator && 1 == octree_get_auto_support(&trNumericSeparator) )
		{
			///------ Folger 05/19/10 QA81-14392-P1 FAILED_TO_DUMP_NUMERIC_SEPARATOR_WHEN_GENERATE_SCRIPT_FROM_IMPASC
			//octree_set_auto_support(&trNumericSeparator, 0);
			octree_set_auto_support(&trNumericSeparator, -1);
			///------ End FAILED_TO_DUMP_NUMERIC_SEPARATOR_WHEN_GENERATE_SCRIPT_FROM_IMPASC
			vector<string>		vsFiles;
			separate_multiline_files(trGetN.fname.strVal, vsFiles);
			if ( vsFiles.GetSize() > 0 )
			{
				///------ Folger 10/21/09 QA81-14506 FAILS_TO_IMPORT_CSV_FILE_IN_SAMPLES_FOLDER_UNDER_GOS_BY_DEFAULT
				//string		strSamples = okutil_get_origin_path(ORIGIN_PATH_SYSTEM, "Samples\\");
				//strSamples.MakeUpper();
				//vsFiles[0].MakeUpper();
				//
				//if ( vsFiles[0].Find(strSamples) == 0 )
				if ( is_builtin_sample_file(vsFiles[0]) )
				///------ End FAILS_TO_IMPORT_CSV_FILE_IN_SAMPLES_FOLDER_UNDER_GOS_BY_DEFAULT
				{
					ImportNuermicSeparatorSettingHelper		clHelper(vsFiles[0]);
					int		nSeparator = clHelper.GetSeparator();
					if ( NF_IS_UNKNOWN != nSeparator )
						trNumericSeparator.nVal = nSeparator;
				}
			}
		}
	}
}
///------ End IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS


void impASC(const string& fname, const int& menu, const TreeNode& trfiles, const TreeNode& options, Range& orng, TreeNode& trheader, const TreeNode& trFilter, int& findex, const int& reimp)
{
	
	//vector vImportedRangeUID; ///---Sim 02-06-2007 GET_RANGE_UID_OF_IMPORTED_FILE ///---Sim 05-18-2007 REMOVE_UID_VARIABLE remove
	
	int c1,c2; 
	Datasheet lyTarget;
	Page pgTarget;
	if ( !GetRangeInfo(orng, lyTarget, pgTarget, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	
	///---Sim 03-09-2007 TEMP_FIX_HIGHLINE_REPLACE_DATA
	if ( !reimp )
	{		
		/// Hong 09/08/07 v8.0698b FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
		/*
		if ( orng )
		{
			orng.Reset();
			orng.Add(lyTarget, 0);
		}
		*/
		reset_range_to_first_obj(orng, lyTarget);
		/// end FIX_FAIL_DUMP_OUTPUT_RANGE_ORNG
	}
	///---END TEMP_FIX_HIGHLINE_REPLACE_DATA
	
	Tree trBaseInfo; 
	
	// generate a usable filter setting
	//TreeNode tnUsingFilter;
	Tree trUsingFilter;
	fuPrepareDefault(trUsingFilter, FILTER_TYPE_ASCII);
	/// Hong 03/07/07 FIX_FAIL_IMPORT_TO_MATRIX_FROM_MENU
	fuSetTargetPage(trUsingFilter, EXIST_NONE); 
	/// end FIX_FAIL_IMPORT_TO_MATRIX_FROM_MENU
	
	//------ Folger 11/21/08 QA80-12315 v8.0977 ASCII_IMPORT_SHOULD_NOT_PROVIDE_DEFAULT_COLUMN_FORMATS
	fuSetFormats(trUsingFilter, "");
	//------
	
	/// Hong 04/12/07 v8.0599 CENTRALIZE_IMPASC_TO_CALL_IWBASE
	if ( trFilter && trFilter.GetNodeCount()>0 )
	{
		trUsingFilter = trFilter;
	}
	/// end CENTRALIZE_IMPASC_TO_CALL_IWBASE
	
	///---Sim 05-24-2007 SUPPORT_CONVERT_OPTIONS_TO_FILTER
	/*
	/// Hong 04/13/07 v8.0600 AVOID_DUPLICATE_SPARKLINE
	// later should clean code in impasc, move all operation to IWASCII class
	fuSetSparkLine(trUsingFilter, sparklines);
	/// end AVOID_DUPLICATE_SPARKLINE
	
	tree_copy_values_by_id(options, trUsingFilter);
	ASCIMP ascimp;
	if ( !GetNTreeToASCIMP(ascimp,options) )
		XF_THROW(IMPERR_ASC_FAILED_CONV_TREE_TO_STRUCT); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	fuSetASCIMP(trUsingFilter, ascimp);
	*/
	OptionsToFilter(options, trUsingFilter);
	///---END SUPPORT_CONVERT_OPTIONS_TO_FILTER
	
	///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
	/*
	///---Sim 05-25-2007 MOVE_SPARKLINE_TO_OPTIONS_TREE
	BOOL bSparkLine;
	bSparkLine = fuGetSparkLine(trUsingFilter);
	///---END MOVE_SPARKLINE_TO_OPTIONS_TREE
	*/
	///---END SUPPORT_SMART_SPARKLINE
		
	// let rename become centralized, don't rename in low level
	bool bRenameWks, bRenameWkBk, bRenameWkBkComm, bRenameColComm;
	fuDisableAllRename(trUsingFilter, &bRenameWks, &bRenameWkBk, &bRenameWkBkComm, &bRenameColComm);
	
	ASCIMP ascimp; ///---Sim 05-25-2007
	fuGetASCIMP(trUsingFilter, ascimp); // ascimp is used for import data function on low level
	
	fuSetAllRename(trUsingFilter, &bRenameWks, &bRenameWkBk, &bRenameWkBkComm, &bRenameColComm); // recover filter
	
	//int nVer;
	//if ( fuGetDouble(trFilter, IDE_DISP_IMP_ORIGINVER, nVer) 
		//&& FILTER_TYPE_ASCII == fuGetType(trFilter) )
	//{
		//trUsingFilter = trFilter;
	//}
	
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames);
	int nFileCount = vsFileNames.GetSize();
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_FILENAME); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
	///---Sim 03-11-2008 FIX_ALL_TEXT_FILE_FROM_SCRIPT_SHOULD_NOT_DUMP_ERR_MSG
	// I don't see any sense to make file info on body. These info only show on GUI. no need remake again on body.
	/*
	if ( MENU_OPTN_MULTIPLE_FILES == menu && trfiles && 0 == trfiles.GetNodeCount()  )
	{
		if ( !construct_tree_setting(vsFileNames, trfiles) )
		{
			XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			return;
		}
	}
	*/
	///---END FIX_ALL_TEXT_FILE_FROM_SCRIPT_SHOULD_NOT_DUMP_ERR_MSG
	
	int nR1, nR2; ///---Sim 03-07-2007 CENTRALIZE_DATA_RANGE_INFO
	
	/// Hong 01/17/07 ADD_SPARKLINES
	int nC1, nC2;
	///---Sim 08-06-2007 SUPPORT_SMART_SPARKLINE
	/*
	XFBase xfSparklines("sparklines");
	if( bSparkLine ) ///---Sim 05-25-2007 MOVE_SPARKLINE_TO_OPTIONS_TREE
	{
		if( !xfSparklines )
		{
			XF_THROW(CER_FAILED_LOAD_SPK_LINE_XF);
			return;
		}
		xfSparklines.SetArg("sel", 0);
	}
	/// end ADD_SPARKLINES
	*/
	///---END SUPPORT_SMART_SPARKLINE
	
	int nRet = -1;
	int	nPageType = pgTarget.GetType();
	/// Hong 11/21/07 QA80-10541 MATRIX_NOT_SUPPORT_STRART_NEW_ROWS_MODE
	if ( EXIST_MATRIX == nPageType && ASCIMP_MODE_APPEND_ROWS == ascimp.iMode )
	{
		ascimp.iMode = ASCIMP_MODE_NEW_SHEETS;
		fuSet1stImportMode(trUsingFilter, ASCIMP_MODE_NEW_SHEETS);
		fuSetImportMode(trUsingFilter, ASCIMP_MODE_NEW_SHEETS);
	}
	/// end MATRIX_NOT_SUPPORT_STRART_NEW_ROWS_MODE
	ASCIMPRESULT air;
	
	bool	bSaveImpInfo = !fuIsNotSaveImportInfoToPage(trUsingFilter); /// Hong 08/08/08 QA80-11980 SAVE_IMPORT_EXTRACTED_VARIABLE_IN_USER_DEFINED_PARAMETERS
	
	int nIndexLayer = pgTarget.Layers.Count() - 1;
	for ( int ii = 0; ii < nFileCount; ii++ )
	{	
		/// Hong 12/06/07 v8.0762 IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
		string strOriginFname = vsFileNames[ii];
		vsFileNames[ii] = cvt_predefined_path_to_real_path(vsFileNames[ii]);
		/// end IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
		///---Sim 04-30-2007 TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
		/*
		if ( 0 == ii && !reimp )
		{
			int nImportMode = get_first_file_import_mode(FILE_TYPE_ASCII);
			TreeNode trInValid;
			
			lyTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, trInValid, true, orng, nImportMode);
		}
		else
		*/
		//{
		lyTarget = GetTargetLayer(pgTarget, nIndexLayer, ii, trUsingFilter, true, orng);//, ascimp.iMode); ///---Sim 05-25-2007 NO_USE
		//}
		///---END TEMP_FIX_IMPORT_MODE_FOR_SINGLE_FILE
	
		///---Sim 10-30-2006 MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE
		/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
		//Tree trInfo;
		//trInfo.Replace(trBaseInfo);
		string strFile;
		strFile.Format("File%d", ii + 1); // file index to be 1
		///------ Folger 06/28/10 ORG-155 ASCII_IMPORT_SPEED_IMPROVE
		//TreeNode trInfo = trBaseInfo.AddNode(strFile);
		TreeNode trInfo = fuCheckAddFileInfoNode(trBaseInfo, strFile);
		///------ End ASCII_IMPORT_SPEED_IMPROVE
		///---END MAKE_INDIVIDUAL_FILE_INFO_FOR_EACH_FILE
		
		nRet = -1;
		switch (nPageType)
		{
		case EXIST_WKS:
		//case EXIST_EXTERN_WKS: /// Hong 06/28/07 QA80-9970 SIMPLY_IMPORT_DATA_INTO_EXCEL_SHEET_AS_OTHERS_NOT_SUPPORT_NOW
			///// AW 11/06/06 MOVE_IMPORTASC_TO_DATASHEET
			/*
			//if ( GetNTreeToASCIMP(ascimp,trOptions) );
			//{	
			ASCIMPRESULT air;
			Worksheet wks(lyTarget);
			if ( !wks )
				XF_THROW(XFERR_INVALID_ARG);  // need right error message here!!!
			//if ( nRet = wks.ImportASCII(fname, ascimp, &air) )
			if ( nRet = wks.ImportASCII(vsFileNames[ii], ascimp, &air, trInfo) )
				XF_THROW(CER_INVALID_DATA);	 // need right error message here!!!
			//}
			//// AW 10/16/06 SAVE_THEME_IN_RIGHT_WKS
			// need save theme in the wks where data imported, like new book, new sheet mode
			theme_active_book_sheet_storage(trOptions, "impASC", PDS_SHEET, false);
			/// END SAVE_THEME_IN_RIGHT_WKS
			break;
			*/
			/// END MOVE_IMPORTASC_TO_DATASHEET
			/// Hong 04/12/07 v8.0599 CENTRALIZE_IMPASC_TO_CALL_IWBASE
			// right now, as IWBASE nly support worksheet, later should expand to support matrix too
			/// Hong 04/28/07 QA80-9702 CLEARLY_NEW_OBJECT_NEEDED
			//IWBase& iwFilter = (IWBase&) Project.FindClass("IWASCII");
			IWBase& iwFilter = (IWBase&) Project.FindClass("IWASCII");
			/// end CLEARLY_NEW_OBJECT_NEEDED
			/// Hong 04/17/07 v8.0602b FIX_IMPASC_FAIL_REIMPORT_CHANGE_OPTION
			// add this node into trInfo for reimport XF to determine if ASCII imported by impASC
			//Tree trInfo;
			TreeNode trImpXF = trInfo.AddNumericNode(1, "impASC", TRGP_CHECK);
			trImpXF.Show = 0;
			trImpXF.SetAttribute(STR_LABEL_ATTRIB, "call from XF");
			/// end FIX_IMPASC_FAIL_REIMPORT_CHANGE_OPTION
			/// Hong 12/06/07 v8.0762 IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
			// add this node into inInfo for save origin file name into binary storage
			TreeNode trOriginFname = trInfo.AddTextNode(strOriginFname, "OriginFileName");
			trOriginFname.Show = 0;
			/// end IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
			
			iwFilter.SetReimport(reimp);
			iwFilter.InitFileArray();
			iwFilter.AddFile(vsFileNames[ii], true );
			///// end SUPPORT_MUTIIL_FILES
			iwFilter.SetReimport(reimp); /// Hong 08/26/09 QA80-13839-P1 FIX_IMPASC_REIMPORT_APPEND_REDUNDANT_FILENAME_INTO_PG_COMMENT
			if ( nRet = iwFilter.Execute(lyTarget, ii, vsFileNames[ii], trInfo, trUsingFilter) )
			/// EJP 2008-02-26 v8.0812 QA80-4808 STOP_IMPORT_IF_ANY_COLS_ARE_LOCKED
			///	XF_THROW_EX(IMPERR_INVALID_FILE, GetFileName(vsFileNames[ii]));
			{
				int nID = nRet;
				string str;
	
				switch( nRet )
				{
				/// Hong 01/09/09 v8.0995 CORRECT_IMPASC_ERROR_RETURN_CHECKING
				//case ERR_WKS_COL_LOCKED:
				case IMPERR_WKS_HAS_COL_LOCKED:
				/// end CORRECT_IMPASC_ERROR_RETURN_CHECKING
					nID = XFERR_WORKSHEET_HAS_COL_LOCKED;				
					break;
				/// Hong 07/23/08 v8.0907 IMPROVE_IMPORT_ERROR_MSG
				/* maybe need more precise error message
				case IMPERR_INVALID_FILE:
					str = GetFileName(vsFileNames[ii]);
					break;
				default:
					nID = IMPERR_ERROR_NUMBER;
					str.Format("%d", nRet);					
					break;
				*/
				default:
					nID = IMPERR_INVALID_FILE;
					str = GetFileName(vsFileNames[ii]);
				/// end IMPROVE_IMPORT_ERROR_MSG
				}
				XF_THROW_EX(nID, str);
			}
			/// end STOP_IMPORT_IF_ANY_COLS_ARE_LOCKED
	
			DataRange drImported;
			drImported = iwFilter.GetDatRange();
			Datasheet lyTemp;
			Page pgTemp;
			GetRangeInfo(drImported, lyTemp, pgTemp, c1, c2);
			
			break;
			/// end CENTRALIZE_IMPASC_TO_CALL_IWBASE
		/// Hong 06/28/07 QA80-9970 SIMPLY_IMPORT_DATA_INTO_EXCEL_SHEET_AS_OTHERS_NOT_SUPPORT_NOW
		case EXIST_EXTERN_WKS:
		/// end SIMPLY_IMPORT_DATA_INTO_EXCEL_SHEET_AS_OTHERS_NOT_SUPPORT_NOW
		case EXIST_MATRIX:
	
			///// AW 11/06/06 MOVE_IMPORTASC_TO_DATASHEET
			Datasheet DaSheet(lyTarget);
			if ( !DaSheet )
				XF_THROW(IMPERR_INVALID_DATASHEET); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
			
			if ( nRet = DaSheet.ImportASCII(vsFileNames[ii], ascimp, &air, trInfo) )	
				XF_THROW_EX(IMPERR_INVALID_FILE, GetFileName(vsFileNames[ii])); ///---Sim 11-07-2006 HANDLE_ERR_MESSAGE_USE_DLL
			/// Hong 01/17/07 ADD_SPARKLINES
			nC1 = air.nC1;
			nC2 = air.nC2;
			///---Sim 03-07-2007 CENTRALIZE_DATA_RANGE_INFO
			nR1 = air.nR1;
			nR2 = air.nR2;
			///---END CENTRALIZE_DATA_RANGE_INFO
	
			/// end ADD_SPARKLINES
			/// Hong 11/10/06 SET_DIMENSION_OF_MATRIX
			if( EXIST_MATRIX == nPageType )
			{
				MatrixLayer mt(lyTarget);
				MatrixObject mo(mt, 0);
				int nCol, nRow;
				nCol = mo.GetNumCols();
				nRow = mo.GetNumRows();
				mo.SetXY(1, 1, nCol, nRow);
			}
			/// end SET_DIMENSION_OF_MATRIX
			//------ Folger 10/10/08 NO_NEED_TO_SAVE_SHEET_THEME_AFTER_IMPORT_ACSII_INTO_MATRIX_AND_EXCEL
			//theme_active_book_sheet_storage(options, "impASC", PDS_SHEET, false);
			//------
			
			break;
			///// END MOVE_IMPORTASC_TO_DATASHEET
		/// AW 11/02/06 ADD_IMP_ASC_FOR_GRAPH
		// later....
		case EXIST_PLOT:
			/*
			GraphLayer gl(lyTarget);
			if( !gl )
				XF_THROW(XFERR_INVALID_ARG);  // need right error message here!!!
		
			// create a new page
			WorksheetPage pgTemp;
			string	strTemplate = LabTalk.System.Wks.DefTemplate$;
			
			pgTemp.Create(strTemplate, CREATE_HIDDEN);
			
			Worksheet wksTemp = pgTemp.Layers();
			wksTemp.GetASCIMP(ascimp);
			string strDesignation;
			strDesignation = fuGetDesignations(tnUsingFilter);
			 
			if( strDesignation.IsEmpty() )
				strDesignation = "XY";
			wksTemp.SetColDesignations(strDesignation);
			
			if ( nRet = wksTemp.ImportASCII(vsFileNames[ii], ascimp, &air) )
				XF_THROW(CER_INVALID_DATA);	 // need right error message here!!!
	
			gl.AddPlot(wksTemp);
			*/
			break;
		//// END ADD_IMP_ASC_FOR_GRAPH
		default:
			// to do
		}
		
		//if ( 0 == nRet && !trFilter )  
		if ( 0 == nRet )
		{
			///---Sim 05-24-2007 KEEP_RANGE_INFO_FOR_OUTPUT
			///---Sim 03-11-2007 QA80-9435 CENTRALIZE_RANGE_TREE_BOTH_ON_VC_AND_OC
			//DataRange dr;
			//dr.Add(lyTarget, nC1, STR_RANGE_STR, nC2, nR1, nR2);
			Tree trDataRange;
			bool bRangeInInfoTree = false;
			TreeNode trDataRangeOriginal = tree_get_node_by_tagname(trInfo, "DataRange", true);
			if ( trDataRangeOriginal )
			{
				bRangeInInfoTree  = true;
				
				// temporary fix
				if ( trDataRangeOriginal.Datarange )
					trDataRangeOriginal = trDataRangeOriginal.Datarange;
					
				trDataRange.Replace(trDataRangeOriginal);
				//trDataRangeOriginal.Remove();
			}
			///---END KEEP_RANGE_INFO_FOR_OUTPUT
			else
			{
				getRangeInfo(trDataRange, lyTarget, nC1, nC2, nR1, nR2);
			}
				
			DataRange dr;
			dr = get_and_check_range(trDataRange);
			///---END QA80-9435 CENTRALIZE_RANGE_TREE_BOTH_ON_VC_AND_OC
			
			// rename 
			///---Sim 03-20-2007 FIX_APPEND_COMMENT_WHEN_REIMPORT
			DWORD dwCtrl = 0;
			dwCtrl |= (reimp) ? COVER_LAST_ADDED_COMMENT : 0;
			if ( EXIST_MATRIX == nPageType ) /// Hong 06/29/07 QA80-9910-P3 FIX_DUPLICATE_OPERATION_BY_CENTRALIZE_IMPASC_TO_CALL_IWBASE
				setPageAndLayerName(dr, vsFileNames[ii], trUsingFilter, dwCtrl);
			///---END FIX_APPEND_COMMENT_WHEN_REIMPORT
			
			///---Sim 05-24-2007 FIX_AVOID_TO_ADD_SPARKLINES_TWICE
			/*
			/// Hong 01/17/07 ADD_SPARKLINES
			if( sparklines )
			{
				Worksheet wks(lyTarget);
				xfSparklines.SetArg("c1", nC1+1); // XF sparklines use LT index
				xfSparklines.SetArg("c2", nC2+1);
				/// end FIX_SPARK_LINE_DISAPPEAR
				xfSparklines.SetArg("wks", wks);
				if ( !xfSparklines.Evaluate() )
				{
					XF_THROW(CER_SPK_LINE_RUN_ERROR); ///---Sim 11-09-2006 HANDLE_ERROR_MESSAGE
					return;	
				}
			}
			/// end ADD_SPARKLINES
			*/
			///---END FIX_AVOID_TO_ADD_SPARKLINES_TWICE
			
			///---Sim 03-11-2007 QA80-9435 CENTRALIZE_RANGE_TREE_BOTH_ON_VC_AND_OC
			// add data range info
			//Tree trDataRange;
			//int UID = getRangeInfo(trDataRange, dr);
			//vImportedRangeUID.Add(UID);
			//vImportedRangeUID.Add(dr.GetUID(TRUE)); ///---Sim 05-18-2007 REMOVE_UID_VARIABLE remove
			///---END QA80-9435 CENTRALIZE_RANGE_TREE_BOTH_ON_VC_AND_OC
			
			// if current file cover existing file data, need update info of that files
			/// Hong 04/12/07 v8.0599 CENTRALIZE_IMPASC_TO_CALL_IWBASE
			//if ( !reimp )
			if ( !reimp &&  EXIST_MATRIX == nPageType )
			/// end CENTRALIZE_IMPASC_TO_CALL_IWBASE
				impinfo_UpdateExistingData(pgTarget, dr);
			
	
			///---Sim 03-11-2007 QA80-9435 CENTRALIZE_RANGE_TREE_BOTH_ON_VC_AND_OC
			///---Sim 03-07-2007 CENTRALIZE_DATA_RANGE_INFO
			//// remove range tree from low level
			//TreeNode trTemp = tree_get_node_by_tagname(trInfo, "DataRange", true);
			//if ( trTemp )
				//trTemp.Remove();
			///---END QA80-9435 CENTRALIZE_RANGE_TREE_BOTH_ON_VC_AND_OC
				
				//impinfo_AddFile(lyTarget, fname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_XFUNC, trFilter);
			//impinfo_AddFile(lyTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_ASCII, trUsingFilter); ///---Sim 02-12-2007 UPDATA_FILTER_TREE
			/// Hong 03/12/07 QA80-9418-S1 ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
			//impinfo_AddFile(lyTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_ASCII, trUsingFilter, ii + 1, trDataRange);
			/// Hong 04/12/07 v8.0599 CENTRALIZE_IMPASC_TO_CALL_IWBASE
			/// Hong 08/08/08 QA80-11980 SAVE_IMPORT_EXTRACTED_VARIABLE_IN_USER_DEFINED_PARAMETERS
			//if ( EXIST_MATRIX == nPageType )
			if ( EXIST_MATRIX == nPageType && bSaveImpInfo )
			/// end SAVE_IMPORT_EXTRACTED_VARIABLE_IN_USER_DEFINED_PARAMETERS
			///	end CENTRALIZE_IMPASC_TO_CALL_IWBASE
			///---Sim 05-24-2007 KEEP_RANGE_INFO_FOR_OUTPUT
			{
				if ( bRangeInInfoTree )
					trDataRangeOriginal.Remove(); // trDataRangeOriginal in trInfo
				/// Hong 12/06/07 v8.0762 IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
				//impinfo_AddFile(lyTarget, vsFileNames[ii], trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_ASCII, trUsingFilter, ii + 1, trDataRange, reimp);
				impinfo_AddFile(lyTarget, strOriginFname, trInfo, ASCIMP_MODE_NEW_SHEETS, FILTER_TYPE_ASCII, trUsingFilter, ii + 1, trDataRange, reimp);
				/// end IMPORT_SUPPORT_RELATIVE_PATH_FOR_REIMPORT_SAMPLE_REQUIREMENT
				if ( bRangeInInfoTree )
					trInfo.AddNode(trDataRange); // recover range info to trInfo					
			}
			///---END KEEP_RANGE_INFO_FOR_OUTPUT remove
						
			/// end ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
			///---END CENTRALIZE_DATA_RANGE_INFO
		}
		/// Hong 04/17/07 v8.0602b FIX_RUNTIME_ERROR_IF_EMPTY_DATA_FILE
		//lyTarget = Project.ActiveLayer(); /// Hong 09/10/07 v8.0699b FIX_TARGET_LAYER_CHANGE_TO_PAGE_ACTIVE_LAYER_UNWANTED
		/* for sim's bug, need better fix in VC level of ImportASCII
		///---Sim 11-2-2006 FIX_EMPTY_SHEET_PROBLEM_FOR_ADDITIONAL_END_LINE_ON_MULTI_STRUCT_FILE
		lyTarget = Project.ActiveLayer();
		if ( is_sheet_empty(lyTarget) )
		{
			lyTarget.Destroy();
			lyTarget = Project.ActiveLayer(); // don't let lyTarget be invalid
		}
		///---END FIX_EMPTY_SHEET_PROBLEM_FOR_ADDITIONAL_END_LINE_ON_MULTI_STRUCT_FILE
		*/
		/// end FIX_RUNTIME_ERROR_IF_EMPTY_DATA_FILE
		///---Sim 11-2-2006 SHOULD_GO_ON_IMPORT_FROM_CURRENT_ACTIVE_SHEET
		pgTarget = lyTarget.GetPage();
		nIndexLayer = lyTarget.GetIndex();
		///---END SHOULD_GO_ON_IMPORT_FROM_CURRENT_ACTIVE_SHEET
	}
	///---END SUPPORT_MULTI_FILE
	//refresh_organizer_gui(pgTarget); ///---Sim 06-25-2007 UPDATE_ORGANIZER
	if ( trheader )
		trheader.Replace(trBaseInfo.Clone(), true, true);/// Hong 01/10/06 FINFO_IS_ONLY_OUTPUT
}

//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 impASC_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impASC_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	//printf("nEventID=%d, node = %s\n", nEventID, lpcszNodeName);
	//out_tree(trGetN);
	
	///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
		/*
	if(GETNE_ON_INIT == nEventID)
	{
		// may need to do something on dialog init
	}
	*/
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	{
		///Kyle 01/04/2009 QA70-12890-P4 CENTRALIZE_CODE_TO_GET_AND_UPDATE_CUSTOM_DATE_FORMAT
		TreeNode trDateFormat = tree_get_node_by_id(trGetN.options, IDE_ASCIMP_DATEFORMAT, true);
		if(trDateFormat)
		{
			update_custom_date_format_combo_attrib(trDateFormat);
			trDateFormat.strVal = EMPTY2NONE(trDateFormat.strVal);
		}
		///End CENTRALIZE_CODE_TO_GET_AND_UPDATE_CUSTOM_DATE_FORMAT
	
		///---Sim 08-11-2008 v8.0919 QA80-11969 LIST_TEMPLATE_NAME
		TreeNode trTemplate = tree_get_node_by_id(trGetN.options, IDE_DISP_IMP_PAGETEMPLATE, true);
		_init_template_list(trTemplate, _get_template_type(trGetN.orng));
		///---END LIST_TEMPLATE_NAME
		
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		///---Sim 03-17-2007 CHECK_ERROR_FOR_INVALID_RANGE
		s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
		if(!s_bRangeValid)
			strErrMsg = XFERR_INVALID_RANGE;
		///---END CHECK_ERROR_FOR_INVALID_RANGE
		OK_SURE(bOKEnable);
		return true;
	}
	///---END SUPPORT_DND_OPEN_DIALOG
	
	// show file info when file name is changed
	string strTagName = lpcszNodeName;
	if(strTagName == "fname") // file name changed, either on start of later user changed
	{
		///---Sim 11-20-2006 SUPPORT_DND_OPEN_DIALOG
		s_bFileValid = file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
		/*
		s_bFileValid = true; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
		OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
		/// Hong 11/1/06 ERROR_MESSAGE
		string strFileName = trGetN.fname.strVal;
		int nErrCode = CER_NO_ERROR;
		int nInvalidFileIdx = 0;
		if( strFileName.IsEmpty ())
		{
			nErrCode = CER_EMPTY_FILENAME;
		}
		else if( !is_valid_filename(strFileName, nInvalidFileIdx) )
		{
			nErrCode = CER_INVALID_FILENAME_INDEX;
		}
		if(CER_NO_ERROR != nErrCode)
		{
			strErrMsg = nErrCode;
			
			if ( CER_INVALID_FILENAME_INDEX == nErrCode )
			{
				strErrMsg += ":";
				strErrMsg += nInvalidFileIdx;
			}
		
			s_bFileValid = false; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
	
			//clear info
			trGetN.info.RemoveChildrenWithPrefix();
			return true;
		}
		/// end ERROR_MESSAGE
		ASCIMP	ascimp;
		StringArray vsFileNames;
		TreeNode trInfo = trGetN.info;
		separate_multiline_files(strFileName, vsFileNames);
		if ( !construct_tree_setting(vsFileNames, trInfo) )
		{
			/// Hong 11/1/06 ERROR_MESSAGE
			//XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			//return false;
			trInfo.RemoveChildrenWithPrefix();// clean the trInfo on GUI
			strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
			s_bFileValid = false; ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			OK_SURE(bOKEnable); ///---Sim 11-16-2006 RELIABLE_OK_ENABLE
			/// end ERROR_MESSAGE			
		}
		return true;
		*/
		///---END SUPPORT_DND_OPEN_DIALOG
	}
	
	///---Sim 03-17-2007 CHECK_ERROR_FOR_INVALID_RANGE
	//check range if is invalid
	if( 0 == lstrcmp(lpcszNodeName, "orng") )
	{
		s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
		if(!s_bRangeValid)
			strErrMsg = CER_INVALID_PAGE;
		
		OK_SURE(bOKEnable);
		return true;
	}
	///---END CHECK_ERROR_FOR_INVALID_RANGE
	
	///------ Folger 05/19/10 QA81-14392-P1 FAILED_TO_DUMP_NUMERIC_SEPARATOR_WHEN_GENERATE_SCRIPT_FROM_IMPASC
	if( 0 == lstrcmp(lpcszNodeName, "NumericSeparator") )
	{
		TreeNode	trNumericSeparator = tree_get_node_by_id(trGetN, IDE_ASCIMP_NUMSEP, true);
		octree_set_auto_support(&trNumericSeparator, -1);
	}
	///------ End FAILED_TO_DUMP_NUMERIC_SEPARATOR_WHEN_GENERATE_SCRIPT_FROM_IMPASC
	
	return false;

}

int impASC_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	string strTagName = lpcszVarName;
	
	if(strTagName == "options")
	{
		construct_ascii_import_options(tr, false);
		return 0;
	}
	/// Hong 03/21/07 NEED_TRUN_ON_MAKE_TREE_FOR_TRFILES
	else if(strTagName == "trfiles")
	{
		return 0;
	}
	/// end NEED_TRUN_ON_MAKE_TREE_FOR_TRFILES
	return -1;
}
//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 impASC_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	/// EJP 2007-05-30 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG
	int nMenu = trGetN.menu.nVal;
	string strFuncName("impASC ");
	DWORD dwFlags;
	if( nMenu )
	{
		strFuncName += "Multi";
		///---Sim 08-07-2008 v8.0918 QA80-11969 MULTI_ASC_IMP_SUPPORT_TEMPLATE
		// xf getn tree support template option
		//dwFlags = OPENBOX_FLAG_MULTIFILES | OPENBOX_FLAG_USETEMPLATE;
		dwFlags = OPENBOX_FLAG_MULTIFILES;
		///---END v8.0918 QA80-11969 MULTI_ASC_IMP_SUPPORT_TEMPLATE
	}
	else
	{
		strFuncName += "Single";
		dwFlags = 0;
	}
	/// end OC_ACCESS_TO_FDLOG
	
	//------------ CPY 12/5/06 LT_CALL_ALSO_SHOULD_OPEN_DLG_IF_NO_FILE_SPECIFIED
	string strFileName = trGetN.fname.strVal;
	
	/// Hong 09/03/07 QA80-9932 FIX_REIMPORT_IMP_WIZ_FROM_CLICKBOARD_FAIL
	if ( trGetN.reimp.nVal && _is_data_from_clickboard(strFileName) )
	{
		_update_clickboard_data_into_file(strFileName);
	}
	/// end FIX_REIMPORT_IMP_WIZ_FROM_CLICKBOARD_FAIL
	
	///---Sim 04-26-2007 FIX_AVOID_TO_OPEN_XF_DLG_WITHOUT_FILENAME
	//if(0 == nGetNDialog && (LTXF_FROM_GUI_PROMPT & dwCntrl) && is_empty_or_unassigned(strFileName))
	if(0 == nGetNDialog && is_empty_or_unassigned(strFileName))
	///---END FIX_AVOID_TO_OPEN_XF_DLG_WITHOUT_FILENAME
	{
		/// EJP 2007-05-30 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG
		///int nFilesRet = get_open_box_by_file_group(strFileName, "ASCII", "", false, true);
		int nShowOptionsDlg = (dwFlags & OPENBOX_FLAG_MULTIFILES ? 1 : 0);
		int nFilesRet = get_open_box_by_file_group(strFileName, "ASCII", "",
			dwFlags | OPENBOX_FLAG_FORCECHECKSTATE, NULL, NULL,
			///Sophy 10/22/2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_EXCEL_CSV_IMPORT
			//_get_show_options_label(trGetN), &nShowOptionsDlg, strFuncName);
			get_show_options_label(trGetN), &nShowOptionsDlg, strFuncName);
			///end SUPPORT_SAVE_SHEET_THEME_FOR_EXCEL_CSV_IMPORT
		if( nShowOptionsDlg )
			nRet = XFEVT_PROCEED_WITH_DLG;
		/// end OC_ACCESS_TO_FDLOG
	
		if ( 0 == nFilesRet )
		{
			nRet = XFEVT_ABORT;
			return;
		}
		trGetN.fname.strVal = strFileName;
	}
	//------------
	if(nGetNDialog > 0)
	{
		//trGetN.uid.Show = 0; ///---Sim 05-18-2007 REMOVE_UID_VARIABLE remove
		trGetN.trheader.Show = 0; 
		//trGetN.orng.Show = 0; ///---Sim 03-17-2007 CHECK_ERROR_FOR_INVALID_RANGE
		//trGetN.trOptions.Show = 0;
		trGetN.trFilter.Show = 0;
		trGetN.menu.Show = 0;
		trGetN.findex.Show = 0;
		
		/// Hong 03/27/07 v8.0590 MORE_RESTRICTION_FOR_REIMPORT
		if( trGetN.reimp.nVal )
			trGetN.orng.Enable = 2;
		/// end MORE_RESTRICTION_FOR_REIMPORT
		
		//--- CPY 9/30/06 MUST_SHOW_FROM_MENU_AND_SIMPLE_NO_XF_DIALOG
		/// Hong 12/05/06 FIX_SCRIPT_GETN_DIALOG_NOT_OPEN
		if(dwCntrl & LTXF_FROM_GUI_MENU) // added menu check, if not from menu, we should not do these things here
		/// end FIX_SCRIPT_GETN_DIALOG_NOT_OPEN
		{
			/// EJP 2007-05-31 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG, moved to top of function
			///int nMenu = trGetN.menu.nVal;
			/// end OC_ACCESS_TO_FDLOG
	
			//if ( MENU_OPTN_MULTIPLE_FILES != nMenu )
			if ( menu_multiple != nMenu ) /// Hong 03/27/07 v8.0590 UPDATE_USE_KEY_FOR_MENU_STRING
			{
				///---Sim 06-14-2007 FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
				//TreeNode trOptions;
				//trOptions = trGetN.options;
				// load theme
				//theme_active_book_sheet_storage(trOptions, "impASC", PDS_SHEET, true);
				//----- CPY 9/1/2007 QA70-10231 SHOW_THEME_IN_FILE_DIALOG
				// should not do this here, already centralized control in xfunctionc.
				//theme_active_book_sheet_storage(trGetN, "impASC", PDS_SHEET, true);
				//----
				///---END FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
				/// Hong 03/26/07 v8.0589b DISABLE_VARIABLE_RESTRICTION
				//if ( MENU_OPTN_OPTIONS == nMenu )
				/// Hong 03/27/07 v8.0590 UPDATE_USE_KEY_FOR_MENU_STRING
				//if ( MENU_OPTN_SINGLE_FILE != nMenu )
				if ( menu_single != nMenu )
				/// end UPDATE_USE_KEY_FOR_MENU_STRING
				/// end DISABLE_VARIABLE_RESTRICTION
				{
					///---Sim 06-14-2007 FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
					TreeNode trOptions;
					trOptions = trGetN.options;
					///---END FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
					/// Hong 03/26/07 v8.0589b USE_NEW_GETNBOX_TO_FIX_UPDATE_BTN_FAIL
					//GetNBox(nRet, trOptions, _L("ASCII Import Options"), _L("Import"), _L("Update"), _L("Active Worksheet import option"), NULL, GetWindow());
					///---Sim 06-14-2007 FIX_GETN_IMPORT_BUTTON_NOT_WORK
					int nRetVal;
					//nRet = GetNBox(trOptions, ascii_option_getn_event, _L("ASCII Import Options"), _L("Active Worksheet import option"));
					nRetVal = GetNBox(trOptions, ascii_option_getn_event, _L("ASCII Import Options"), _L("Active Worksheet import option"));
					///---END FIX_GETN_IMPORT_BUTTON_NOT_WORK
					/// end USE_NEW_GETNBOX_TO_FIX_UPDATE_BTN_FAIL
					
					///---Sim 06-14-2007 FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
					Tree trSaveGetN;
					///---Sim 04-30-2008 FIX_UPDATE_ASCIMPORT_OPTIONS_TO_SHEET_THEME
					//trSaveGetN.Replace(trGetN.Clone(), FALSE);
					//trSaveGetN.AddNode(trOptions);
					trSaveGetN = trGetN;
					///---END FIX_UPDATE_ASCIMPORT_OPTIONS_TO_SHEET_THEME
					///---END FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
					
					///---Sim 06-14-2007 FIX_GETN_IMPORT_BUTTON_NOT_WORK
					//if ( IDOK == nRet )
					if ( IDOK == nRetVal )
					///---END FIX_GETN_IMPORT_BUTTON_NOT_WORK
					{
						//// AW 10/16/06 SAVE_THEME_IN_RIGHT_WKS
						// need save theme in the wks where data imported, like new book, new sheet mode
						//theme_active_book_sheet_storage(trOptions, "impASC", PDS_SHEET, false);
						///---Sim 06-14-2007 FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
						//okutil_theme_update_last_used("impASC", &trOptions);
						//okutil_theme_update_last_used("impASC", &trSaveGetN);
						///---END FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
						nMenu = 0;
					}
					else 
					{
						///---Sim 06-14-2007 FIX_GETN_IMPORT_BUTTON_NOT_WORK
						//if ( IDCANCEL != nRet )
						if ( IDCANCEL != nRetVal )
						///---END FIX_GETN_IMPORT_BUTTON_NOT_WORK
						{
							///---Sim 06-14-2007 FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
							//theme_active_book_sheet_storage(trOptions, "impASC", PDS_SHEET, false);	
							//okutil_theme_update_last_used("impASC", &trOptions);
							theme_active_book_sheet_storage(trSaveGetN, "impASC", PDS_SHEET, false);	
							
							// not really import file, should not update last used theme
							//okutil_theme_update_last_used("impASC", &trSaveGetN); 
							///---END FIX_SAVE_ASCII_OPTION_TO_SHEET_THEME
						}
						nRet = XFEVT_ABORT;
						return;
					}
				}
			}
			///---Sim 09-29-2006 HANDLE_USER_CANCEL_OPEN_FILE_BOX
			//get_open_box_by_file_group(strFileName, "ASCII", "", trGetN.menu.nVal);
	
			/// EJP 2007-05-30 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG
			// We want options check box on for both single and multi ASCII import file dialogs.
			///	/// SY 2006-10-02 v8.0493 QA70-9015 THEME_FILENAME_WORKBOOK_AND_WORKSHEET
			///	///	int nFilesRet = get_open_box_by_file_group(strFileName, "ASCII", "", trGetN.menu.nVal);
			///	int nFilesRet = get_open_box_by_file_group(strFileName, "ASCII", "", nMenu, true);
			///	/// end THEME_FILENAME_WORKBOOK_AND_WORKSHEET
			int nShowOptionsDlg = (dwFlags & OPENBOX_FLAG_MULTIFILES ? 1 : 0);
			//------- CPY 7/11/2007 QA70-10010 MOST_RECENT_IMPORT_EXPORT_NEED_LTXF_FROM_MRU_MENU
			if(dwCntrl & LTXF_FROM_MRU_MENU)
				nShowOptionsDlg = 0;
			//-------
			int nFilesRet = get_open_box_by_file_group(strFileName, "ASCII", "",
			    ///Sophy 10/22/2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_EXCEL_CSV_IMPORT
				//dwFlags, NULL, NULL, _get_show_options_label(trGetN), &nShowOptionsDlg, strFuncName);
				dwFlags, NULL, NULL, get_show_options_label(trGetN), &nShowOptionsDlg, strFuncName);
				///end SUPPORT_SAVE_SHEET_THEME_FOR_EXCEL_CSV_IMPORT
			if( 0 == nShowOptionsDlg )
				nRet = XFEVT_PROCEED_NO_DLG;
			/// end OC_ACCESS_TO_FDLOG
	
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				return;
			}
			///---END HANDLE_USER_CANCEL_OPEN_FILE_BOX
			trGetN.fname.strVal = strFileName;
			///------ Folger 09/29/09 QA80-14392 IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS
			_check_update_numeric_separator_setting_from_file(trGetN);
			///------ End IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS
	
			/// EJP 2007-05-30 v8.0631 QA70-9796 OC_ACCESS_TO_FDLOG
			///	/// AW 09/29/06 QA80-9015 MORE_ON_IMPORT_SINGLE_ASCII_FILE
			///	if ( MENU_OPTN_SINGLE_FILE == nMenu )
			///	{
			///		//trGetN.menu.nVal = MENU_OPTN_USE_WKS_SETTINGS;
			///		nRet = XFEVT_PROCEED_NO_DLG;
			///			return;
			//	}
			///	/// END MORE_ON_IMPORT_SINGLE_ASCII_FILE
			if( MENU_OPTN_SINGLE_FILE == nMenu )
				return;
			/// end OC_ACCESS_TO_FDLOG
		}
		StringArray vsFileNames;
		separate_multiline_files(strFileName, vsFileNames);
		TreeNode trInfo = trGetN.trfiles;
		if ( !construct_tree_setting(vsFileNames, trInfo) )
		{
			///---Sim 06-19-2007 IMPROVE_WARNING_MESSAGE_POST_FUNCTION
			//XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
			warning_msg_box(CER_INVALID_IMPORT_DATA_FILE, false, 'E');
			///---END IMPROVE_WARNING_MESSAGE_POST_FUNCTION
			return ;
		}
	}
	if ( nGetNDialog >= 0 )
	{
		TreeNode trFilter = trGetN.trFilter; 
		if ( trFilter && trFilter.GetNodeCount()>0 )
		{
			///---Sim 05-25-2007 MOVE_SPARKLINE_TO_OPTIONS_TREE
			//trGetN.sparklines.nVal = fuGetSparkLine(trFilter); // Hong 01/17/07 ADD_SPARKLINES
			///---END MOVE_SPARKLINE_TO_OPTIONS_TREE
			//FilterToOptions(trFilter, trGetN.options);
			FilterToOptions(trFilter, trGetN.options, trGetN.reimp.nVal);
		}
		/// AW 10/05/06 No need load from wks as it will load last used theme
	//		else
	//		{
	//				theme_active_book_sheet_storage(trGetN.trOptions, "impASC", PDS_SHEET, true);	
	//		}
		///------ Folger 09/29/09 QA80-14392 IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS
		_check_update_numeric_separator_setting_from_file(trGetN);
		///------ End IMPASC_FAILS_TO_IMPORT_NUMERIC_DATA_ON_GOS_WITH_DEFAULT_OPTIONS
	}
	
}
//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 impASC_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
