#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	firstmode_book,
	firstmode_sheet,
};
enum{
	impmode_book,
	impmode_sheet,
};
//put additional include files here
#include <..\originlab\theme_utils.h> ///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
#include <..\originlab\Import_Utils.h>
#include <..\originlab\fu_utils.h>
#include <GetNBox.h>
#include <ocGrid.h> /// Hong 12/25/07 v8.0775 EXCEL_SUPPORT_SHEET_INFO
//////////////////////////////////////////////////
// modification log
//-----------------------------------------------
// CPY 6/7/07 QA70-9891 LOAD_EXCEL_OPTION_TO_SKIP_STYLES
/// Sim 06-25-2007 UPDATE_ORGANIZER
/// ML 8/20/2007 QA70-10272 OPTIONALLY_READ_ONLY_STYLES_BEYOND_LAST_DATA_UP_TO_A_CERTAIN_NUMBER
/// Sim 08-23-2007 FIX_DUPLICATE_EMPTY_PAGE
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Sim 12-11-2007 SUPPORT_APPLY_HEADER_SETTING_TO_ALL_SHEETS
// CPY 12/12/07 CHANGE_VAR_NAMES_FOR_EASIER_LT_READING
/// Hong 12/27/07 QA80-10625 FIX_EXCEL_EMPTY_SHEET_REPLACE_BY_FOLLOWING_SHEET
/// Hong 01/03/08 QA80-10872 BETTER_DEFAULT_BITS_WELL_SUPPORT_CUSTOM_FORMAT
/// Hong 01/16/08 QA80-10541 IMPROVE_EXCEL_DISPLAY_ITEM
/// Hong 03/15/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
/// Hong 03/21/08 v8.0829 EXCEL_IMPORT_REQUIRED_SAME_SHEET_NAME_AS_EXCEL_BY_USER
/// Hong 03/24/08 QA80-11223 EXCEL_NEED_RENAME_BOOK_SUPPORT
/// Hong 04/21/08 QA80-10541-P21 FIX_MULTI_IMPMODE_FAIL_WORK_BY_NEW_BOOK
/// Hong 06/03/08 v8.0876 USER_ASK_FOR_FIRST_MODE_SUPPORT_NEW_SHEET
/// Hong 07/23/08 v8.0907 IMPROVE_IMPORT_EXCEL_ERROR_MESSAGE
/// Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
/// Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
/// Sim 10-30-2008 v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
/// Sim 03-03-2009 QA80-13208 SUPPORT_SPECIFY_CUSTOMIZED_DESINATION_IN_EXCEL_DLG
/// Sim 03-04-2009 QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
/// Folger 07/16/09 INCORRECT_ENGLISH_SOURCE_STRINGS_CAME_INTO_OLOCAL
/// Hong 02/25/10 QA80-13946 IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
/// Hong 03/05/10 FIX_EXCEL_IMPORT_RANGE_ONLY_CONTAIN_FIRST_COLUMN
//////////////////////////////////////////////////

//put your own support static functions here
///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
#define EXCEL_1ST_FILE_IMPORT_MODE_LIST 	LIST_IMPORT_MODE_BOOK_SHEET
#define EXCEL_IMPORT_MODE_LIST	(EXCEL_1ST_FILE_IMPORT_MODE_LIST & (~IW_FLAG_IMPMODE_REPLACE))
///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING

static bool s_bFileValid = true;
static bool s_bRangeValid = true;
#define OK_SURE(_OK_ENABLE) _OK_ENABLE = (s_bFileValid && s_bRangeValid);

///---Sim 03-03-2009 QA80-13208 SUPPORT_SPECIFY_CUSTOMIZED_DESINATION_IN_EXCEL_DLG
//#define STR_ALL_DESIGANATIONS "None|YY|XYY|XY XY|XYY XYY|XYYY XYYY|XYYErr XYYErr|XYZZ|XYZ XYZ"
#define STR_ALL_DESIGANATIONS "|None|YY|XYY|XY XY|XYY XYY|XYYY XYYY|XYYErr XYYErr|XYZZ|XYZ XYZ"
///---END QA80-13208 SUPPORT_SPECIFY_CUSTOMIZED_DESINATION_IN_EXCEL_DLG
///---Sim 03-04-2009 QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
#define STR_DESIGN_UNCHANGED _LE("<Unchanged>")
///---END QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
#define STR_COMBO_NUMBER "|0|1|2|3|4|5|6|7|8|9|10"
#define STR_COMBO_INDEX "|<none>|1|2|3|4|5|6|7|8|9|10"
#define STR_COMBO_ALL_SHEET "First Sheet|All Sheets"
///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING

/// Hong 03/15/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
//////////////////class COMExcelImport////////////////////////////////
class COMExcelImport
{
public:
	COMExcelImport();
	~COMExcelImport();
	
public:
	bool Create() { m_objExcelApp = CreateObject("excel.application"); return IsValid(); }
	// return whether excel com object create successfully or not
	bool IsValid() { return m_objExcelApp ? true : false; }
	
	/// Hong 03/15/08 v8.0825 CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
	//int GetWorksheetNames(StringArray& saWksNames, LPCSTR lpcszFileName);
	int GetWorksheetNames(StringArray& saWksNames, LPCSTR lpcszFileName, vector<bool>& vbVisible);
	/// end CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
	// make sure wks passed is valid
	int ImportExcelSheet(Worksheet& wks, LPCSTR lpcszFileName, LPCSTR lpcszSheetName);
	
private:
	int getExcelBook(LPCSTR lpcszFileName);
	
private:	
	Object	m_objExcelApp;
	Object	m_objWkbk;
	string	m_strFileName;
};
//////////////////class COMExcelImport Implementation////////////////////
COMExcelImport::COMExcelImport()
{}

COMExcelImport::~COMExcelImport()
{
	// this is to prevent Excel from asking user to save when Quit
	if ( m_objWkbk )
		m_objWkbk.Close(FALSE); 
	if ( IsValid() ) 
		m_objExcelApp.Quit();
}
/// Hong 03/15/08 v8.0825 CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
//int COMExcelImport::GetWorksheetNames(StringArray& saWksNames, LPCSTR lpcszFileName)
int COMExcelImport::GetWorksheetNames(StringArray& saWksNames, LPCSTR lpcszFileName, vector<bool>& vbVisible)
/// end CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
{
	int nRet = 0 ;
	if ( (nRet = getExcelBook(lpcszFileName)) < 0 )
		return nRet;
		
	// Loop over each worksheet in the Excel workbook
	foreach ( Object objWks in m_objWkbk.Worksheets )
	{
		saWksNames.Add(objWks.Name);
		vbVisible.Add(objWks.Visible); /// Hong 03/15/08 v8.0825 CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
	}
	
	return saWksNames.GetSize();
}

int COMExcelImport::ImportExcelSheet(Worksheet& wks, LPCSTR lpcszFileName, LPCSTR lpcszSheetName)
{
	int nRet = 0 ;
	if ( (nRet = getExcelBook(lpcszFileName)) < 0 )
		return nRet;
	
	Object	objWks;
	objWks = m_objWkbk.Worksheets(lpcszSheetName);
	if ( !objWks )
		return -4;
	
	Object oUsedRange;
	try {
		// Get Used Range. Use Range is the range inside the worksheet which contains data.
		oUsedRange = objWks.UsedRange;
	
		// Get top left cell coordinates:
		int				nr1 = oUsedRange.Row;
		int				nc1 = oUsedRange.Column;
		
		_VARIANT		varValues;
		// wandering if we should use Value2 		
		// Get all the values:
		varValues = oUsedRange.Value;
		
		// Put the values into the worksheet:
		wks.PutData(varValues, nr1 - 1, nc1 - 1);
	}
	catch(int nErr)
	{
		nRet = nErr;		// failed
	}
	/// Hong 03/21/08 v8.0829 EXCEL_IMPORT_REQUIRED_SAME_SHEET_NAME_AS_EXCEL_BY_USER
	wks.SetName(lpcszSheetName, OCD_ENUM_NEXT);
	/// end EXCEL_IMPORT_REQUIRED_SAME_SHEET_NAME_AS_EXCEL_BY_USER
	return nRet;
}

int COMExcelImport::getExcelBook(LPCSTR lpcszFileName)
{
	if ( !IsValid() )
		return -1;
	
	string strFile(lpcszFileName);
	if ( !strFile.IsFile() )
		return -2;
		
	// if input another file or not open yet
	if ( m_strFileName.Compare(strFile) || !m_objWkbk )
	{		
		m_strFileName = strFile;
		// close the old one to save memory
		if ( m_objWkbk )
			m_objWkbk.Close(FALSE);
		
		Object	objWkbks;
		objWkbks = m_objExcelApp.Workbooks;
		// we do not support import charts, so no need do link update
		// http://msdn2.microsoft.com/en-us/library/bb179167.aspx
		// If the argument is 0, no charts are created.
		m_objWkbk = objWkbks.Open(strFile, 0, TRUE);
		if ( !m_objWkbk )
			return -3;
	}
	return 0;
}
//////////////////end of COMExcelImport Implementation///////////////////
/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY

///---Sim 03-04-2009 QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
static string get_designation_list()
{
	///------ Folger 07/16/09 INCORRECT_ENGLISH_SOURCE_STRINGS_CAME_INTO_OLOCAL
	//string strDesign = _L(STR_DESIGN_UNCHANGED);
	string strDesign = GetLocalized(STR_DESIGN_UNCHANGED);
	///------ End INCORRECT_ENGLISH_SOURCE_STRINGS_CAME_INTO_OLOCAL
	strDesign.Insert(0, '|');
	strDesign += STR_ALL_DESIGANATIONS;
	return strDesign;	
}
///---END QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO

///---Sim 05-30-2007 ADD_DESIGNATION_OPTIONS
static void set_designations(Worksheet& wks, LPCSTR lpcszDesignations)
{
	string strDesignations(lpcszDesignations);
	
	///---Sim 03-04-2009 QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
	if (compare_string_localization(strDesignations, STR_DESIGN_UNCHANGED))
		strDesignations.Empty();
	///---END QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
	
	BOOL bRepeat = FALSE;
	int i = strDesignations.Find(' ');
	if( i >= 0 )
	{
		bRepeat = TRUE;
		strDesignations.Delete(0, i + 1);
	}
	strDesignations.Replace("YErr", "E");
	strDesignations.Replace("None", "N");

	wks.SetColDesignations(strDesignations, bRepeat);
}
///---END ADD_DESIGNATION_OPTIONS

static BOOL _construct_tree_FileInfo(string strFileName, TreeNode& trInfo)
{
	if ( !strFileName.IsFile() )
		return false;
	string strInfo;
	strInfo.Format("File Size: %s ", GetFileSize(strFileName));
	trInfo.strVal = strInfo;
	trInfo.Enable = 0;
	return true;
}

/// Hong 03/14/08 QA80-11130 OC_NEED_MORE_DETAIL_ERROR_CODE
/// Hong 02/25/10 QA80-13946 IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
//int cvt_excel_err_code(int nErrCode)
int cvt_excel_err_code(int nErrCode, LPCSTR lpcszFileName = NULL)
/// end IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
{	
	/// Hong 02/25/10 QA80-13946 IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
	string		strExt;
	if ( (EXCEL_ERR_FILE_ALREADY_EXIST == nErrCode || EXCEL_ERR_UNKNOWN == nErrCode) &&
		separate_file_name_ext(lpcszFileName, NULL, &strExt) && 0 == strExt.CompareNoCase("XLSX") )
		return XFERR_EXCEL_2007_REQUIRED;
	/// end IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
	switch (nErrCode)
	{
	case EXCEL_ERR_SHARE_VIOLATION:
		nErrCode = OKERR_SHARE_VIOLATION;
		break;
	case EXCEL_ERR_FILE_ALREADY_EXIST:
		nErrCode = XFERR_NOT_NATIVE_EXCEL_FILE;	
		break;
	case EXCEL_ERR_FILE_NO_FOUND:
	case EXCEL_ERR_UNKNOWN:
	default:
		nErrCode = CER_INVALID_IMPORT_DATA_FILE;
		break;
	}
	return nErrCode;
}
/// end OC_NEED_MORE_DETAIL_ERROR_CODE
/// Hong 03/14/08 QA80-11130 OC_NEED_MORE_DETAIL_ERROR_CODE
/**$
	Parameter:
		pIndex = [output] output the file index which construct fail, if NULL, will format file name into pstrErrMsg
							Notice: when call form body, use default, when call from body, must pass pIndex
*/
//static BOOL _construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo)
/// Hong 03/14/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY		
//static BOOL _construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo, string* pstrErrMsg = NULL, int* pIndex = NULL)
/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
//static BOOL _construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo, bool bExcelImport, string* pstrErrMsg = NULL, int* pIndex = NULL)
static BOOL _construct_tree_setting(StringArray& vsFileNames, TreeNode& trInfo, bool bExcelImport, string* pstrErrMsg = NULL, int* pIndex = NULL, LPCSTR lpcszSheetSelection = NULL)
/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY		
/// end OC_NEED_MORE_DETAIL_ERROR_CODE
{
	if ( trInfo )
		trInfo.RemoveChildrenWithPrefix();
	
	TreeNode trFile;
	string strTag;
	/// Hong 03/15/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	COMExcelImport comExcel;
	if ( bExcelImport )
		comExcel.Create();
	/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
	string					strSheetSelection(lpcszSheetSelection);
	vector<string>			vstrSheetsSelections;
	if ( !strSheetSelection.IsEmpty() )
	{
		strSheetSelection.GetTokens(vstrSheetsSelections, ':');
	}
	/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
	int nCount = vsFileNames.GetSize();
	for ( int nIndex = 0; nIndex < nCount; nIndex++ )
	{
		strTag.Format("file%d", nIndex+1);
		trFile = trInfo.AddNode(strTag);
		trFile.SetAttribute(STR_LABEL_ATTRIB, GetFileName(vsFileNames[nIndex], false));
		
		// Load File Info
		TreeNode trFileInfo;
		trFileInfo = trFile.AddNode("FileInfo");
		trFileInfo.SetAttribute(STR_LABEL_ATTRIB, _L("File Info"));	/// Max 07-11-09 add label		
		if ( !_construct_tree_FileInfo(vsFileNames[nIndex], trFileInfo) )
		{	
			/// Hong 03/14/08 QA80-11130 OC_NEED_MORE_DETAIL_ERROR_CODE		
			if ( pstrErrMsg )
			{
				*pstrErrMsg = CER_INVALID_IMPORT_DATA_FILE;
				if ( pIndex )
				{
					*pIndex = nIndex;
				}
				else
				{
					*pstrErrMsg += ":" + GetFileName(vsFileNames[nIndex]);
				}
			}
			/// end OC_NEED_MORE_DETAIL_ERROR_CODE	
			return false;		
		}			

		/// Hong 12/25/07 v8.0775 EXCEL_SUPPORT_SHEET_INFO		
		vector<string> vsSheets;
		/// Hong 03/15/08 v8.0825 CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
		vector<bool> vbVisible;
		/// end CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
		/// Hong 03/15/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY		
		//int nn = og_get_excel_sheet_names(vsFileNames[nIndex], &vsSheets);
		int nn = 0;
		if ( bExcelImport )		
			/// Hong 03/15/08 v8.0825 CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
			//nn = comExcel.GetWorksheetNames(vsSheets, vsFileNames[nIndex]);		
			nn = comExcel.GetWorksheetNames(vsSheets, vsFileNames[nIndex], vbVisible);	
			/// end CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
		else
			nn = og_get_excel_sheet_names(vsFileNames[nIndex], &vsSheets);
		/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
		if ( nn <= 0 )
		{	
			/// Hong 03/14/08 QA80-11130 OC_NEED_MORE_DETAIL_ERROR_CODE		
			//return false;
			if ( pstrErrMsg )
			{
				/// Hong 02/25/10 QA80-13946 IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
				//*pstrErrMsg = cvt_excel_err_code(nn);			
				*pstrErrMsg = cvt_excel_err_code(nn, vsFileNames[nIndex]);
				/// end IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
				if ( pIndex )
				{
					*pIndex = nIndex;
				}
				else
				{
					*pstrErrMsg += ":" + GetFileName(vsFileNames[nIndex]);
				}
			}
			/// end OC_NEED_MORE_DETAIL_ERROR_CODE
			return false;
		}
		/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
	#pragma xor(push, FALSE)
		vector<BOOL>		vnCheck(vsSheets.GetSize());
		vnCheck = 1;
		bool				bLTHasValidSelection = false;
		for ( int mm = 0; mm < vstrSheetsSelections.GetSize(); mm++ )		
		{
			ASSERT(vnCheck.GetSize() == vsSheets.GetSize());
			int				nPos = vsSheets.Find(vstrSheetsSelections[mm]);
			if ( -1 != nPos )
			{
				if ( !bLTHasValidSelection )
					bLTHasValidSelection = true;
				vnCheck[nPos] = 0;
				continue;
			}
			
			int				nLTPos = atoi(vstrSheetsSelections[mm]);
			if ( 0 < nLTPos && nLTPos <= vnCheck.GetSize() )
			{
				if ( !bLTHasValidSelection )
					bLTHasValidSelection = true;
				vnCheck[nLTPos - 1] = 0;
			}
		}
		if ( bLTHasValidSelection )
			vnCheck ^= 1;
	#pragma xor(pop)
		/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
		TreeNode trFileNode, trNode;
		trFileNode = trFile.AddNode("FileSheet");
		trFileNode.SetAttribute(STR_LABEL_ATTRIB, _L("File Sheet(s)"));
		trFileNode.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_CHECK_CONTROL | GETNBRANCH_CHECK_CONTROL_ONE_EVENT);
		for (int ii = 0; ii < nn; ii++)
		{
			/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
			//trNode = trFileNode.AddNumericNode(1, "ExcelSheet", TRGP_CHECK);
			ASSERT(vnCheck.GetSize() == vsSheets.GetSize());
			trNode = trFileNode.AddNumericNode(vnCheck[ii], "ExcelSheet", TRGP_CHECK);
			/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
			trNode.SetAttribute(STR_LABEL_ATTRIB, vsSheets[ii]);
			/// Hong 03/15/08 v8.0825 CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
			if ( ii < vbVisible.GetSize() && 0 == vbVisible[ii] )
			{
				trNode.SetAttribute(STR_ATTRIB_LABEL_COLOR, (string)COLOR_ORANGE);
			}
			/// end CP_ASKED_FOR_HIDDEN_SHEET_INFO_IN_GUI_BY_COLOR
		}
		/// end EXCEL_SUPPORT_SHEET_INFO
	}
	//trInfo.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN);
	return true;
}

static bool _file_name_event(TreeNode& trGetN, string& strErrMsg)
{
	string strFileName = trGetN.fname.strVal;
	TreeNode trNode = trGetN.info;
	
	trNode.RemoveChildrenWithPrefix();
	
	if (!check_imported_file_name(strFileName, strErrMsg))
		return false;
	
	StringArray vsFileNames;
	separate_multiline_files(strFileName, vsFileNames);
	/// Hong 03/14/08 QA80-11130 OC_NEED_MORE_DETAIL_ERROR_CODE
	//if ( !_construct_tree_setting(vsFileNames, trNode) )
	//{
	//	// clean the info on GUI
	//	trNode.RemoveChildrenWithPrefix();
	//	strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
	/// Hong 03/14/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	//if ( !_construct_tree_setting(vsFileNames, trNode, &strErrMsg ) )
	/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
	//if ( !_construct_tree_setting(vsFileNames, trNode, trGetN.excel.nVal, &strErrMsg ) )
	if ( !_construct_tree_setting(vsFileNames, trNode, trGetN.excel.nVal, &strErrMsg, NULL, trGetN.sheet.strVal) )
	/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
	/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	{
		trNode.RemoveChildrenWithPrefix();
	/// end OC_NEED_MORE_DETAIL_ERROR_CODE
		return false;
	}
	
	return true;
}

/// Hong 03/14/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
static bool has_excel_installed()
{
	COMExcelImport comExcel;
	return comExcel.Create();	
}
/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY

///---Sim 10-30-2008 v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
static void generate_using_filter_tree(TreeNode& trUsingFilter, const TreeNode& trGetNOptions,
	const int& firstmode, const int& impmode, const int& excel, const int& cellfmts, const int& emptycols, const int& mainheader,
	const int& sname, const int& lname, const int& unit, const int& cmt1, const int& cmt2,
	const int& removerow, const string& desig, const int& allsheets)
{
	Tree trGetN;
	TreeNode tr = trGetN.AddNode("options");
	if ( tr )
		tr.Replace(trGetNOptions, true, true);
	trGetN.firstmode.nVal = firstmode;
	trGetN.impmode.nVal = impmode;
	trGetN.excel.nVal = excel;
	trGetN.cellfmts.nVal = cellfmts;
	trGetN.emptycols.nVal = emptycols;
	trGetN.mainheader.nVal = mainheader;
	trGetN.sname.nVal = sname;
	trGetN.lname.nVal = lname;
	trGetN.unit.nVal = unit;
	trGetN.cmt1.nVal = cmt1;
	trGetN.cmt2.nVal = cmt2;
	trGetN.removerow.nVal = removerow;
	trGetN.desig.strVal = desig;
	trGetN.allsheets.nVal = allsheets;
	
	///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
	update_getn_LT_variables(trGetN, STR_XF_NAME_EXCEL, true);
	///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING

	///---Sim 04-27-2010 ORG-29-P2 IMP_2D_BIN_SUPPORT_FILTER_SETTINGS_AND_SAVE_FILE_INFO_INTO_BOOK
	// not safe modify for 8.1 SR3, better to do it on 8.2
	// /*
	if ( trGetN.options )
	{
		///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
		TreeNode trGetNLT = trGetN.options.GetNode(STR_NODE_NAME_GETN_LT);
		if ( trGetNLT )
			trGetNLT.Show = true;
		///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
	
		trUsingFilter.Replace(trGetN.options, true, true, true);
	}
	else
	{
		fuPrepareDefault(trUsingFilter, FILTER_TYPE_XFUNC);
	}	
	// */
	// generate_using_filter_tree_by_getn_tree(trUsingFilter, trGetN.options);
	///---END ORG-29-P2 IMP_2D_BIN_SUPPORT_FILTER_SETTINGS_AND_SAVE_FILE_INFO_INTO_BOOK
}
///---END v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE


void impExcel(const string& fname, const TreeNode& info, const int& firstmode, const int& impmode, const int& excel, const int& cellfmts, const int& emptycols, const int& mainheader, const int& sname, const int& lname, const int& unit, const int& cmt1, const int& cmt2, const int& removerow, const string& desig, const int& allsheets, const TreeNode& options, Range& orng, TreeNode& finfo, const string& sheet)
{
	int c1,c2; 
	//Datasheet ds;
	//orng.GetRange(ds, c1, c2);
	WorksheetPage wp;
	Worksheet wks;
	if ( !GetRangeInfo(orng, wks, wp, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	//Worksheet ws = ds;
	if ( !wks )
		XF_THROW(CER_INVALID_WKS);
	//Page pgTarget;
	//ws.GetParent(pgTarget);
	//
	//
	//Tree trBaseInfo; 
	//if ( finfo )
		//trBaseInfo = finfo;
	///---Sim 10-30-2008 v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
	/*
	TreeNode tnUsingFilter; // filter is none
	/// Hong 03/24/08 QA80-11223 EXCEL_NEED_RENAME_BOOK_SUPPORT
	double dDefault = 0.;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, dDefault) )
		tnUsingFilter = options;
	/// end EXCEL_NEED_RENAME_BOOK_SUPPORT
	//
	////support default filter is empty
	//double dDefault = 0.;
	//if ( fuGetDouble(trFilter, IDE_DISP_IMP_ORIGINVER, dDefault) )
		//tnUsingFilter = trFilter;
	//
	//int nFileImportMode = _GetImportModeFromListIndex(impmode);
	//if ( ASCIMP_MODE_AUTO == nFileImportMode )
		//nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
	/// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT	
	//int nFileImportMode = impmode;
	int nFileImportMode = ASCIMP_MODE_NEW_BOOKS;
	/// Hong 06/03/08 v8.0876 USER_ASK_FOR_FIRST_MODE_SUPPORT_NEW_SHEET
	switch ( firstmode )
	{
	case firstmode_sheet:
		nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
		break;
	case firstmode_book:	
		nFileImportMode = ASCIMP_MODE_NEW_BOOKS;
		break;
	}
	/// end USER_ASK_FOR_FIRST_MODE_SUPPORT_NEW_SHEET
	/// Hong 03/24/08 QA80-11223 EXCEL_NEED_RENAME_BOOK_SUPPORT
	fuSet1stImportMode(tnUsingFilter, nFileImportMode);
	/// end EXCEL_NEED_RENAME_BOOK_SUPPORT
	/// end EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
	/// Hong 04/21/08 QA80-10541-P21 FIX_MULTI_IMPMODE_FAIL_WORK_BY_NEW_BOOK
	int nMultiImpMode;
	switch ( impmode )
	{
	case impmode_sheet:
		nMultiImpMode = ASCIMP_MODE_NEW_SHEETS;
		break;
	case impmode_book:		
	default:
		nMultiImpMode = ASCIMP_MODE_NEW_BOOKS;
		break;
	}
	fuSetImportMode(tnUsingFilter, nMultiImpMode);
	/// end FIX_MULTI_IMPMODE_FAIL_WORK_BY_NEW_BOOK	
	*/
	Tree tnUsingFilter;
	generate_using_filter_tree(tnUsingFilter, options,
		firstmode, impmode, excel, cellfmts, emptycols, mainheader,
		sname, lname, unit, cmt1, cmt2,
		removerow, desig, allsheets);
	///---END v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
	StringArray vsFileNames;
	separate_multiline_files(fname, vsFileNames);
	int nFileCount = vsFileNames.GetSize();
	if ( nFileCount <= 0 )
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	
	/// Hong 12/25/07 v8.0775 EXCEL_SUPPORT_SHEET_INFO
	if ( info.IsEmpty() || 0 == info.GetNodeCount() )
	{
		/// Hong 03/14/08 QA80-11130 OC_NEED_MORE_DETAIL_ERROR_CODE
		//_construct_tree_setting(vsFileNames, info);
		string strErrMsg;
		int nIndex;
		/// Hong 03/14/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
		//if ( !_construct_tree_setting(vsFileNames, info, &strErrMsg, &nIndex) )
		/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
		//if ( !_construct_tree_setting(vsFileNames, info, excel, &strErrMsg, &nIndex) )
		if ( !_construct_tree_setting(vsFileNames, info, excel, &strErrMsg, &nIndex, sheet) )
		/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
		/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
		{
			XF_THROW_EX(strErrMsg, vsFileNames[nIndex]);
		}
		/// end OC_NEED_MORE_DETAIL_ERROR_CODE
	}
	/// end EXCEL_SUPPORT_SHEET_INFO
	
	bool bApplyToAllSheets = allsheets;	///---Sim 12-11-2007 SUPPORT_APPLY_HEADER_SETTING_TO_ALL_SHEETS
	int nShortName	= sname - 1;
	int nLongName	= lname - 1;
	int nUnit		= unit - 1;
	/// Hong 11/02/07 QA80-10622 IMPORT_EXCEL_SUPPORT_MULTILINES_COMMENTS
	//int nComment	= comment - 1;
	int nCommentFrom	= cmt1-1; //cmtfrm - 1;CPY 12/12/07 CHANGE_VAR_NAMES_FOR_EASIER_LT_READING
	int nCommentTo	= cmt2-1; //cmtto - 1;CPY 12/12/07 CHANGE_VAR_NAMES_FOR_EASIER_LT_READING
	/// end IMPORT_EXCEL_SUPPORT_MULTILINES_COMMENTS
	bool bRemoveRows = removerow;
	//bool bShowLabels = showlabel;
	
	/// Hong 12/25/07 v8.0775 EXCEL_SUPPORT_SHEET_INFO
	int nFileIndex = 0;
	int nIndexLayer = wks.GetIndex();
	XLSReadCntrl stCtrl;
	/// Hong 01/03/08 QA80-10872 BETTER_DEFAULT_BITS_WELL_SUPPORT_CUSTOM_FORMAT
	//stCtrl.m_dwRXC = RXC_VALUE;
	stCtrl.m_dwRXC = RXC_DEFAULT_VALUE;
	/// end BETTER_DEFAULT_BITS_WELL_SUPPORT_CUSTOM_FORMAT
	if ( cellfmts )
		stCtrl.m_dwRXC |= RXC_STYLE;
	stCtrl.m_nMaxNumEmptyColumnsBeyondLastData = emptycols;	
	/// Hong 03/15/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	COMExcelImport comExcel;
	if ( 1 == excel )	
		comExcel.Create();
	waitCursor wc;
	/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	foreach (TreeNode trFileNode in info.Children)
	{
		/// Hong 04/21/08 QA80-10541-P21 FIX_MULTI_IMPMODE_FAIL_WORK_BY_NEW_BOOK
		///// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
		//if ( impmode_sheet == impmode && 0 != nFileIndex )
		//	nFileImportMode = ASCIMP_MODE_NEW_SHEETS;
		///// end EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
		/// end FIX_MULTI_IMPMODE_FAIL_WORK_BY_NEW_BOOK
		///---Sim 10-30-2008 v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
		//wks = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, nFileImportMode);
		wks = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, ASCIMP_MODE_NEW_BOOKS);
		///---END v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
		int nSheet = 0;
		int nErr = 0;
		bool bFirstSheet = true;
		DataRange drImported;
		/// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
		//int nFindIndex = 0; /// Hong 12/27/07 QA80-10625 FIX_EXCEL_EMPTY_SHEET_REPLACE_BY_FOLLOWING_SHEET		
		/// end EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
		foreach (TreeNode trSheet in trFileNode.FileSheet.Children)
		{			
			if ( trSheet.nVal )
			{
				/// Hong 12/27/07 QA80-10625 FIX_EXCEL_EMPTY_SHEET_REPLACE_BY_FOLLOWING_SHEET
				// skip last imported worksheet
				//int nSht = page_find_empty_sheet(wp);
				/// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
				//int nSht = page_find_empty_sheet(wp, nFindIndex);
				int nSht = page_find_empty_sheet(wp, nIndexLayer);
				/// end EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
				/// end FIX_EXCEL_EMPTY_SHEET_REPLACE_BY_FOLLOWING_SHEET
				if(nSht < 0)
					nSht = wp.AddLayer();
				wks = wp.Layers(nSht);
				/// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
				//nFindIndex = nSht + 1; /// Hong 12/27/07 QA80-10625 FIX_EXCEL_EMPTY_SHEET_REPLACE_BY_FOLLOWING_SHEET
				nIndexLayer = nSht + 1;
				/// end EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
				
				string strFileName = vsFileNames[nFileIndex];	
				/// Hong 03/15/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
				//nErr = wks.ImportExcel(strFileName, nSheet, &stCtrl);
				if ( 1 == excel )
				{
					string strWorksheet;
					trSheet.GetAttribute(STR_LABEL_ATTRIB, strWorksheet);
					nErr = comExcel.ImportExcelSheet(wks, vsFileNames[nFileIndex], strWorksheet);
				}
				else
					nErr = wks.ImportExcel(strFileName, nSheet, &stCtrl);
				/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
				if ( nErr < 0 )					
					break;
	
				if ( bFirstSheet || bApplyToAllSheets )
				{
					setLabelValueFromRows(wks, nShortName, nLongName, nUnit, nCommentFrom, bRemoveRows, true, nCommentTo, mainheader);
					set_designations(wks, desig);
				}
				/// Hong 03/05/10 FIX_EXCEL_IMPORT_RANGE_ONLY_CONTAIN_FIRST_COLUMN
				//drImported.Add(wks, 0, STR_RANGE_STR);
				drImported.Add(wks, 0, STR_RANGE_STR, -1);
				/// end FIX_EXCEL_IMPORT_RANGE_ONLY_CONTAIN_FIRST_COLUMN
				bFirstSheet = false;
			}	
			nSheet++;		
		}
		if ( nErr < 0 )
		{
			/// Hong 07/23/08 v8.0907 IMPROVE_IMPORT_EXCEL_ERROR_MESSAGE
			if ( 1 == excel )
			{
			/// end IMPROVE_IMPORT_EXCEL_ERROR_MESSAGE
				string strWarning;
				strWarning.Format(_L("%s import failed!"), vsFileNames[nFileIndex]);
				warning_msg_box(strWarning, true);
			/// Hong 07/23/08 v8.0907 IMPROVE_IMPORT_EXCEL_ERROR_MESSAGE
			}
			else
			{
				/// Hong 02/25/10 QA80-13946 IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
				//int		nErrCode = cvt_excel_err_code(nErr);
				int		nErrCode = cvt_excel_err_code(nErr, vsFileNames[nFileIndex]);
				/// end IMPROVE_ERR_MSG_FOR_FAIL_IMPORT_XSLX_FILE
				warning_msg_box(nErrCode, GetFileName(vsFileNames[nFileIndex]), true);
			}
			/// end IMPROVE_IMPORT_EXCEL_ERROR_MESSAGE
		}		
		else
		{
			/// Hong 03/24/08 QA80-11223 EXCEL_NEED_RENAME_BOOK_SUPPORT
			DWORD dwCtrl = 0;
			//dwCtrl |= (reimp) ? COVER_LAST_ADDED_COMMENT : 0;
			setPageAndLayerName(drImported, vsFileNames[nFileIndex], tnUsingFilter, dwCtrl);
			/// end EXCEL_NEED_RENAME_BOOK_SUPPORT
		
			Tree trRange, trInfo;			
			if (drImported && drImported.GetNumData() > 0)
				getRangeInfo(trRange, drImported);
			///---Sim 10-30-2008 v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
			//impinfo_AddFile(wks, vsFileNames[nFileIndex], trInfo, nFileImportMode, FILTER_TYPE_XFUNC, tnUsingFilter, 1, trRange, false, IMP_INFO_ADDFILE_NO_AUTO_SIZE);
			impinfo_AddFile(wks, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_BOOKS, FILTER_TYPE_XFUNC, tnUsingFilter, 1, trRange, false, IMP_INFO_ADDFILE_NO_AUTO_SIZE);
			///---END v9.963b QA80-12484 IMPORT_EXCEL_PAGE_INFO_SUPPORT_TO_GENERATE_FILTER_FILE
		}
		nFileIndex++;
	}
	/*
	/// end EXCEL_SUPPORT_SHEET_INFO
	//int nC1;
	int nIndexLayer = wks.GetIndex();
	for (int nFileIndex = 0; nFileIndex < nFileCount; nFileIndex++ )
	{
		///---Sim 08-23-2007 FIX_DUPLICATE_EMPTY_PAGE
		//wks = GetTargetLayer(wp, nIndexLayer, nFileIndex, tnUsingFilter, true, orng, nFileImportMode);
		///---END FIX_DUPLICATE_EMPTY_PAGE
		//		
		Tree trInfo; /// Hong 07/05/07 v8.0655 ADD_ORGANIZER_INFO
		//trInfo.Replace(trBaseInfo);
		//
		//CSVFile csvFile;
		//int nRet = csvFile.Import(vsFileNames[nFileIndex], ws, trInfo, 0 , nC1);
		//if ( nRet )
		//{
			//XF_THROW_EX(nRet, vsFileNames[nFileIndex]);
		//}
		//
		//string strLT;
		//strLT.Format("doc -aimport %s;", vsFileNames[nFileIndex]);
		//LT_execute(strLT);
		//------- CPY 6/7/07 QA70-9891 LOAD_EXCEL_OPTION_TO_SKIP_STYLES
		DWORD dwCntrl = XLIMP_SKIP_STYLES;
		//if(cellformats)CPY 12/12/07 CHANGE_VAR_NAMES_FOR_EASIER_LT_READING
		if(cellfmts)
			dwCntrl = 0;
		//-------
		/// ML 8/20/2007 QA70-10272 OPTIONALLY_READ_ONLY_STYLES_BEYOND_LAST_DATA_UP_TO_A_CERTAIN_NUMBER
		//int nErr = wp.OpenExcel(XL_IMPORT, vsFileNames[nFileIndex], NULL, dwCntrl);
		int		nErr = wp.OpenExcel(XL_IMPORT, vsFileNames[nFileIndex], NULL, dwCntrl, emptycols);
		/// end OPTIONALLY_READ_ONLY_STYLES_BEYOND_LAST_DATA_UP_TO_A_CERTAIN_NUMBER
		if( !wp || (nErr < 0))
		{
			/// Hong 08/17/07 v8.0683 CLEAN_ERR_MSG
			//printf("Import Excel failed, error code = %d\n", nErr);
			string strWarning;
			strWarning.Format("%s import failed!", vsFileNames[nFileIndex]);
			warning_msg_box("Import Excel failed", true);
			/// end CLEAN_ERR_MSG
			continue;
		}
			
		///---Sim 12-11-2007 SUPPORT_APPLY_HEADER_SETTING_TO_ALL_SHEETS
	*/	/*
		//WorksheetPage wp = Project.Pages();
		wks = wp.Layers();
		//printf("Opened [%s] as an Origin workbook (%s) with Sheet '%s' active", vsFileNames[nFileIndex], wp.GetName(), wks.GetName());
		
		//if ( wp )
		//{
			//for ( int nLayer = 0; nLayer < wp.Layers.Count(); nLayer++ )
			//{
				//wks = wp.Layers(nLayer);
		//setLabelValueFromRows(wks, nShortName, nLongName, nUnit, nComment, bRemoveRows, bShowLabels);
		/// Hong 11/02/07 QA80-10622 IMPORT_EXCEL_SUPPORT_MULTILINES_COMMENTS
		//setLabelValueFromRows(wks, nShortName, nLongName, nUnit, nComment, bRemoveRows, true);
		setLabelValueFromRows(wks, nShortName, nLongName, nUnit, nCommentFrom, bRemoveRows, true, nCommentTo);
		/// end IMPORT_EXCEL_SUPPORT_MULTILINES_COMMENTS
			//}
		//}
		
		///---Sim 05-30-2007 ADD_DESIGNATION_OPTIONS
		set_designations(wks, desig);
		///---END ADD_DESIGNATION_OPTIONS
		*/
	/*	int nFirstSheet = 0; // always new page, first sheet index always is 0
		int nNumSheets;
		if ( bApplyToAllSheets )
			nNumSheets = wp.Layers.Count();
		else
			nNumSheets = nFirstSheet + 1; // only for first sheet
		
		for (int ii = nFirstSheet; ii < nNumSheets; ii++)
		{
			wks = wp.Layers(ii);
			if ( wks )
			{
				setLabelValueFromRows(wks, nShortName, nLongName, nUnit, nCommentFrom, bRemoveRows, true, nCommentTo);
				set_designations(wks, desig);
			}
		}		
		
		wks = wp.Layers();
		///---END SUPPORT_APPLY_HEADER_SETTING_TO_ALL_SHEETS
		
		/// AW 07/11/07 SHOULD_NOT_AUTO_SIZE_IN_EXCEL_IMPORT
		//impinfo_AddFile(wks, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_BOOKS, FILTER_TYPE_XFUNC, tnUsingFilter); /// Hong 07/05/07 v8.0655 ADD_ORGANIZER_INFO
		impinfo_AddFile(wks, vsFileNames[nFileIndex], trInfo, ASCIMP_MODE_NEW_BOOKS, FILTER_TYPE_XFUNC, tnUsingFilter, 1, NULL, false, IMP_INFO_ADDFILE_NO_AUTO_SIZE);
		/// END SHOULD_NOT_AUTO_SIZE_IN_EXCEL_IMPORT
	}
	//refresh_organizer_gui(wp); ///---Sim 06-25-2007 UPDATE_ORGANIZER
	*/
}

//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 impExcel_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impExcel_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	OK_SURE(bOKEnable);
	
	///---Sim 05-30-2007 ADD_DESIGNATION_OPTIONS
	if ( GETNE_ON_INIT == nEventID )
	{
		///---Sim 03-04-2009 QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
		//trGetN.desig.SetAttribute(STR_COMBO_ATTRIB, STR_ALL_DESIGANATIONS);
		trGetN.desig.SetAttribute(STR_COMBO_ATTRIB, get_designation_list());
		///---END QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
		trGetN.desig.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);
		if ( trGetN.desig.strVal.IsEmpty() )
			trGetN.desig.strVal = GetLocalized(STR_DESIGN_UNCHANGED);
		///---END QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
	
		//trGetN.impmode.nVal = ASCIMP_MODE_NEW_BOOKS; /// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
	}
	///---END ADD_DESIGNATION_OPTIONS
	
	/// Hong 01/16/08 QA80-10541 IMPROVE_EXCEL_DISPLAY_ITEM
	if ( 0 == lstrcmp(lpcszNodeName, "cmt1") || GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID )
	{
		trGetN.cmt2.Enable = (trGetN.cmt1.nVal > 0);
	}
	/// end IMPROVE_EXCEL_DISPLAY_ITEM
	
	/// Hong 03/14/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	if ( GETNE_ON_THEME == nEventID )
	{		
		if( !has_excel_installed() )
		{
			trGetN.excel.Show = FALSE;
			trGetN.excel.nVal = 0;
		}
		else
		{
			trGetN.excel.Show = TRUE;
		}
		if ( trGetN.excel.nVal )
		{
			trGetN.cellfmts.Enable = DISABLE;		
			trGetN.emptycols.Enable = DISABLE;		
		}
	}
	if ( 0 == lstrcmp(lpcszNodeName, "excel") )
	{		
		trGetN.cellfmts.Enable = trGetN.excel.nVal ? DISABLE : ENABLE;
		trGetN.emptycols.Enable = trGetN.excel.nVal ? DISABLE : ENABLE;
		return true;
	}
	/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
	
	///support D&D show Dlg, if trFiles no child, should construct it
	if ( (GETNE_ON_INIT == nEventID && trGetN.info.Children.Count() == 0) || GETNE_ON_THEME == nEventID )
	{
		//trGetN.impmode.nVal = ASCIMP_MODE_NEW_BOOKS;
		
		s_bFileValid = _file_name_event(trGetN, strErrMsg);
		s_bRangeValid = !trGetN.orng.strVal.IsEmpty();
		if(!s_bRangeValid)
			strErrMsg = CER_INVALID_PAGE;
		
		OK_SURE(bOKEnable);	
		return true;
	}
	
	//check file name if is invalid
	if ( 0 == lstrcmp(lpcszNodeName, "fname") )
	{
		s_bFileValid = _file_name_event(trGetN, strErrMsg);
		OK_SURE(bOKEnable);
		return true;
	}
	
	//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;
	}
	return false;
	

}

int impExcel_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	/// Hong 03/24/08 QA80-11223 EXCEL_NEED_RENAME_BOOK_SUPPORT
	if ( 0 == strcmp(lpcszVarName, "options") )
	{
		///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
		/*
		constructOptionsForXF(tr, true, false, false, false, false);
		// remove worksheet rename options
		TreeNode 	trRenameWks, trWksTrimFNameFrom, trWksTrimFNameTo, trAppendColComm;
		trRenameWks			= tree_get_node_by_id(tr, IDE_XFIMP_RENAMEWKS, true);
		trWksTrimFNameFrom	= tree_get_node_by_id(tr, IDE_DISPIMP_RENAMEWKSTRIMFNAMEFROM, true);
		trWksTrimFNameTo	= tree_get_node_by_id(tr, IDE_DISPIMP_RENAMEWKSTRIMFNAMETO, true);
		trAppendColComm 	= tree_get_node_by_id(tr, IDE_XFIMP_FILENAME2COLCOMMENT, true);
		trRenameWks.Remove();
		trWksTrimFNameFrom.Remove();
		trWksTrimFNameTo.Remove();
		trRenameWks.Remove();
		trAppendColComm.Remove();
		*/
		constructOptionsForXF(tr, true, true, false, false, false);
		
		fuSetRenameWks(tr, 0);
		fuXFSetAppendFileNameToColumnComments(tr, false);
		TreeNode 	trRenameWks, trWksTrimFNameFrom, trWksTrimFNameTo, trAppendColComm;
		trRenameWks			= tree_get_node_by_id(tr, IDE_XFIMP_RENAMEWKS, true);
		trWksTrimFNameFrom	= tree_get_node_by_id(tr, IDE_DISPIMP_RENAMEWKSTRIMFNAMEFROM, true);
		trWksTrimFNameTo	= tree_get_node_by_id(tr, IDE_DISPIMP_RENAMEWKSTRIMFNAMETO, true);
		trAppendColComm 	= tree_get_node_by_id(tr, IDE_XFIMP_FILENAME2COLCOMMENT, true);
		trRenameWks.Show = false;
		trWksTrimFNameFrom.Show = false;
		trWksTrimFNameTo.Show = false;
		trAppendColComm.Show = false;
	
		
		TreeNode trGetNLT = check_add_getn_LT_to_filter_options(tr);
		TreeNode trNode;
		
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, EXCEL_1ST_FILE_IMPORT_MODE_LIST);
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			//tr1stImportMode.nVal = ASCIMP_MODE_NEW_BOOKS;
		}
		trNode = trGetNLT.AddNode("firstmode");
		trNode.Replace(tr1stImportMode, true, true);
		tr1stImportMode.Remove();
		
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, EXCEL_IMPORT_MODE_LIST);
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			//trImportMode.nVal = ASCIMP_MODE_NEW_BOOKS;
		}
		trNode = trGetNLT.AddNode("impmode");
		trNode.Replace(trImportMode, true, true);
		trImportMode.Remove();
	
		trNode = trGetNLT.AddNode("excel");
		trNode.ID = TRGP_CHECK;
		trNode.DataID = GET_FILTER_USER_DATAID(3);
		
		trNode = trGetNLT.AddNode("cellfmts");
		trNode.ID = TRGP_CHECK;
		trNode.DataID = GET_FILTER_USER_DATAID(4);
		
		trNode = trGetNLT.AddNode("emptycols");
		trNode.DataID = GET_FILTER_USER_DATAID(5);
		
		trNode = trGetNLT.AddNode("mainheader");
		trNode.ID = TRGP_ENUM_COMBO;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_NUMBER);
		trNode.DataID = GET_FILTER_USER_DATAID(6);
		
		trNode = trGetNLT.AddNode("sname");
		trNode.ID = TRGP_ENUM_COMBO;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_INDEX);
		trNode.DataID = GET_FILTER_USER_DATAID(7);
		
		trNode = trGetNLT.AddNode("lname");
		trNode.ID = TRGP_ENUM_COMBO;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_INDEX);
		trNode.DataID = GET_FILTER_USER_DATAID(8);
		
		trNode = trGetNLT.AddNode("unit");
		trNode.ID = TRGP_ENUM_COMBO;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_INDEX);
		trNode.DataID = GET_FILTER_USER_DATAID(9);
		
		trNode = trGetNLT.AddNode("cmt1");
		trNode.ID = TRGP_ENUM_COMBO;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_INDEX);
		trNode.DataID = GET_FILTER_USER_DATAID(10);
		
		trNode = trGetNLT.AddNode("cmt2");
		trNode.ID = TRGP_ENUM_COMBO;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_INDEX);
		trNode.DataID = GET_FILTER_USER_DATAID(11);
		
		trNode = trGetNLT.AddNode("removerow");
		trNode.ID = TRGP_CHECK;
		trNode.DataID = GET_FILTER_USER_DATAID(12);
		
		TreeNode trDesig = tree_get_node_by_id(tr, IDE_DISP_IMP_COLDESIGN, true);
		trNode = trGetNLT.AddNode("desig");
		trNode.Replace(trDesig, true, true);
		trDesig.Remove();
		trNode.Show = true;
		trNode.ID = TRGP_STR;
		///---Sim 03-04-2009 QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
		//trNode.SetAttribute(STR_COMBO_ATTRIB, STR_ALL_DESIGANATIONS);
		trNode.SetAttribute(STR_COMBO_ATTRIB, get_designation_list());
		///---END QA80-13208-P1 ADD_UNCHANGE_TO_DESINATION_COMBO
		trNode.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);
		
		trNode = trGetNLT.AddNode("allsheets");
		trNode.ID = TRGP_ENUM_LIST;
		trNode.SetAttribute(STR_COMBO_ATTRIB, STR_COMBO_ALL_SHEET);
		trNode.DataID = GET_FILTER_USER_DATAID(14);
		///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
	
		fuSetXFunctionName(tr, "impExcel");
	
		return 0;
	}
	/// end EXCEL_NEED_RENAME_BOOK_SUPPORT
	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 impExcel_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( nGetNDialog > 0 )
	{
		trGetN.removerow.Show = 0;
		//trGetN.impmode.Show = 0; /// Hong 12/28/07 v8.0776 EXCEL_OPEN_MULTI_IMPMODE_SUPPORT
		//trGetN.orng.Show = 0;
		trGetN.finfo.Show = 0;
		///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
		//trGetN.trFilter.Show = 0;		
		///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING		
		/// Hong 12/22/08 QA80-12828 v8.0989c EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
		trGetN.sheet.strVal = "";
		/// end EXCEL_SUPPORT_SPECIFY_SHEET_SELECTION_IN_LT
	}
	//if(nGetNDialog >= 0)
	//{
		//if ( LTXF_FROM_GUI_MENU == dwCntrl )
			//trGetN.fname.strVal = "";
	//}
	if(nGetNDialog > -1)
	{
		/// Hong 03/14/08 QA80-11223 SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY
		if ( !has_excel_installed() )
		{
			trGetN.excel.Show = false;
			trGetN.excel.nVal = 0;			
		}		
		trGetN.cellfmts.Enable = trGetN.excel.nVal ? DISABLE : ENABLE; // not support with import by excel right now
		trGetN.emptycols.Enable = trGetN.excel.nVal ? DISABLE : ENABLE; 
		/// end SPEEDUP_IMPORVE_USING_EXCEL_DIRECTLY		
		bool bResetFileInfo = false; ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		
		string strFileName = trGetN.fname.strVal;
		
		/// Hong 04/10/07 v8.0597b IGNORE_FNAME$_WHEN_FROM_MENU
		if ( LTXF_FROM_GUI_MENU & dwCntrl ) // if from menu, ignore default value fname$
			strFileName = "";
		/// end IGNORE_FNAME$_WHEN_FROM_MENU
		
		if(is_empty_or_unassigned(strFileName))
		{			
			/// Hong 05/31/07 v8.0630 MORE_ON_ACCESS_TO_FDLOG
			//int nFilesRet = get_open_box_by_file_group(strFileName, "Excel", "Excel Files", true);
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_open_box_by_file_group(strFileName, "Excel", _L("Excel Files"),
				//	OPENBOX_FLAG_MULTIFILES, NULL, NULL, _L("Show Options Dialog"), &nShowOptionsDlg);
				///---Sim 10-27-2008 QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				//nFilesRet = get_open_box_by_file_group(strFileName, "Excel", NULL,
					//OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				nFilesRet = get_open_box_by_file_group(strFileName, "Excel", NULL,
					OPENBOX_FLAG_MULTIFILES, NULL, NULL, get_show_options_label(trGetN), &nShowOptionsDlg);
				///---END QA80-12422 SUPPORT_SAVE_SHEET_THEME_FOR_ALL_SHEET_IMPORT_FILE
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				//nFilesRet = get_open_box_by_file_group(strFileName, "Excel", _L("Excel Files"), true);
				nFilesRet = get_open_box_by_file_group(strFileName, "Excel", NULL, true); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			/// end MORE_ON_ACCESS_TO_FDLOG
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				//warning_msg_box(XFERR_UI_USER_CANCEL, false);
				return;
			}
			trGetN.fname.strVal = strFileName;
			bResetFileInfo = true; ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		} ///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION			
		
		string strErrMsg;
		///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		//if( !_file_name_event(trGetN, strErrMsg) )
		if( !_file_name_event(trGetN, strErrMsg) ) // no reset bit?
		///---END QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		{
			/// Hong 03/21/07 v8.0586 FIX_FAIL_DUMP_CORRECT_ERROR_STRING
			//nRet = XFEVT_ABORT;
			//warning_msg_box(strErrMsg, false);
			int nErrMsg = atoi(strErrMsg);
			nRet = XFEVT_ABORT;
			warning_msg_box(nErrMsg, false, 'E'); // should pass an int ///---Sim 06-19-2007 IMPROVE_WARNING_MESSAGE_POST_FUNCTION
			/// end FIX_FAIL_DUMP_CORRECT_ERROR_STRING
			return;
		}
		///---Sim 03-24-2009 QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION			
		//}
		///---END QA80-13342 SUPPORT_OC_ACCESS_IMP_FILE_DATA_SELECTION
		
		///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
		/*
		///---Sim 05-30-2007 ADD_DESIGNATION_OPTIONS
		TreeNode trFilter = trGetN.trFilter; 
		if ( trFilter && trFilter.GetNodeCount() > 0 )
		{
			trGetN.desig.strVal = fuGetDesignations(trFilter);
		}
		///---END ADD_DESIGNATION_OPTIONS
		*/
		///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING		
	}	
}
//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 impExcel_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
