#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>

//////////////// Modification Log //////////////////////
//----- CPY 11/4/07 LOCALIZATION_NEEDED
////////////////////////////////////////////////////////

#include <okocUtils.h>
//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
#include <../OriginLab/TemplateOrganizer.h>
#define STR_INVALID_SAVE_PATH	_L("System Folder or Group Folder")

//static int s_nGetNDialog = 0;

///Jasmine 02/10/09 CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
static bool s_bAskIfExist;
///End CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST

//#define STR_SYSTEM_PATH 	okutil_get_origin_path(ORIGIN_PATH_SYSTEM, NULL, TRUE)
//#define STR_USER_PATH 		okutil_get_origin_path(ORIGIN_PATH_USER, NULL, TRUE)
//#define STR_TEMPLATE_XML	STR_USER_PATH + "template.xml"
//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML

/*
*  Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
*	Jasmine 06/05/07 TEMPLATE_CHANGE_UNASSIGNED_TO_AUTO
*	Jasmine 07/04/07 ALLOW_SAVE_TO_DIFFERENT_PATH_AND_UPDATE_ATTRIBUTE_IN_ORGANIZER
*	Jasmine 07/04/07 ADD_PATH_TRACK
*	Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
*	Folger 08/01/07 INIT_DLG_NO_MATTER_WITH_OR_WITHOUT_DLG
*	Jasmine 08/15/07 QA80-10155 NO_MODIFICATION_TO_GROUP_FOLDER_FILE
*	Sophy 10/28/08 QA80-12442 SHOW_WARNING_WHEN_SAVE_READ_ONLY_FILE
*	Jasmine 01/24/09 #4259 ADD_LOAD_SYSTEM_THEME_CHECK_BOX
*	Jasmine 02/03/09 #4259 ADD_HINT_FOR_LOAD_SYSTEM_THEME_CHECK_BOX
*	Jasmine 02/04/09 EXTENSION_IS_ADDED_IN_BRANCH_LABEL
*	Jasmine 02/04/09 QA80-13028 SHOW_USER_DEFIND_CATEGORY_INSTEAD_OF_BUILT_IN_CATEGORY_WHEN_SAVING
*	Kyle 02/04/2009 #4259 ADD_HINT_FOR_MESSAGE_FOR_BUILT_IN_TEMPLATE
*	Jasmine 02/04/09 MAX_ASK_SHOW_BUILT_IN_TEMPLATE_HINT_IN_ERRMSG
*	Jasmine 02/06/09 CP_ASK_TURN_OFF_LOADSYS_CHECKBOX_IF_SAVE_BUILTIN_TEMPLATE
*	Jasmine 02/06/09 USE_REMINDER_MSG_FOR_BUILT_IN_TEMPLATE
*	Jasmine 02/09/09 REMOVE_OPTION_BRANCH
*	Jasmine 02/10/09 CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
*	Jasmine 02/17/09 SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
*	Jasmine 02/24/09 PROTECT_LOCALIZATION_FOLDER
*	Jasmine 03/30/09 SHOW_DESCRIPTION_STORED_IN_TEMPLATE
*	Jasmine 08/12/09 HARD_CODE_TO_GET_FILE_EXTENSION
*/ 

///Jasmine 02/24/09 PROTECT_LOCALIZATION_FOLDER
static string _get_localization_folder()
{
	string strLocalization = okutil_get_language_path(true);
	strLocalization = GetAppPath(true) + strLocalization.GetToken(0, '\\') + "\\";
	strLocalization.MakeUpper();
	
	return strLocalization;
}
///End PROTECT_LOCALIZATION_FOLDER

///Jasmine 02/17/09 SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
static bool _override_exe_folder(LPCSTR lpcszFile)
{
	string strFolder = GetFilePath(lpcszFile);
	if(lstrcmpi(strFolder, STR_USER_PATH) == 0 || lstrcmpi(strFolder, STR_SYSTEM_PATH) == 0)
	{
		string strExeFile = STR_SYSTEM_PATH + GetFileName(lpcszFile);
		if( strExeFile.IsFile() )
			return true;
		
		///Jasmine 02/24/09 PROTECT_LOCALIZATION_FOLDER
		string strLocalization = _get_localization_folder();
		char szTemp[MAXFULLPATH];
		lstrcpy(szTemp, strLocalization);
		if(FindFile(GetFileName(lpcszFile), szTemp) == true) 
			return true;
		///End PROTECT_LOCALIZATION_FOLDER
	}

	return false;
}
///End SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER

///Jasmine 01/24/09 #4259 ADD_LOAD_SYSTEM_THEME_CHECK_BOX
static bool _is_load_system_theme(Page& page)
{	
	///Jasmine 02/06/09 CP_ASK_TURN_OFF_LOADSYS_CHECKBOX_IF_SAVE_BUILTIN_TEMPLATE
	string strCurrentFile = page_get_template_name(page);
	///Jasmine 02/17/09 SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
	//if( is_built_in_template(page.GetType(), GetFileName(strCurrentFile, true), strCurrentFile) )
	///Jasmine 02/24/09 PROTECT_LOCALIZATION_FOLDER
	//if(lstrcmpi( GetFilePath(strCurrentFile), STR_SYSTEM_PATH) == 0)
	string strCurrentPath = GetFilePath(strCurrentFile);
	if(lstrcmpi(STR_SYSTEM_PATH, strCurrentPath) == 0 || strCurrentPath.Find( _get_localization_folder() )>= 0 )
		return false;
	///End SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
	///End CP_ASK_TURN_OFF_LOADSYS_CHECKBOX_IF_SAVE_BUILTIN_TEMPLATE
	
	string strLT;
	strLT.Format("%s!page.cntrl", page.GetName());
	double dd;
	LT_get_var(strLT, &dd);
	DWORD dwCntrl = dd;
	return !(dwCntrl & PPDWCNTRL_IGNORE_SYSTEM_THEME);
}

static void _set_load_system_theme(Page& page, bool bLoad)
{
	string strLT;
	strLT.Format("%s!page.cntrl", page.GetName());
	double dd;
	LT_get_var(strLT, &dd);
	DWORD dwCntrl = dd;
	
	if(bLoad)
		dwCntrl &= ~PPDWCNTRL_IGNORE_SYSTEM_THEME;
	else
		dwCntrl |= PPDWCNTRL_IGNORE_SYSTEM_THEME;
	
	strLT.Format("page.cntrl = %d", dwCntrl);	
	page.LT_execute(strLT);
}
///End ADD_LOAD_SYSTEM_THEME_CHECK_BOX
	
//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
//static bool	_find_template(TreeNode& trBranch, string strTemplate, string& strFile, string& strCate)
//{
	//TreeNode tr;
	//if(trBranch.IsValid())
		//tr = trBranch.FindNodeByAttribute(STR_LABEL_ATTRIB, strTemplate);
	//if(!tr.IsValid() || !tr.Parent().IsValid())
		//return false;
	//tr.GetAttribute(STR_FILENAME_ATTRIB, strFile);
	//tr = tr.Parent();
	//tr.GetAttribute(STR_LABEL_ATTRIB, strCate);
	//if(!lstrlen(strCate))
		//strCate = tr.tagName;
	//return true;
//}
//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML

//------ Folger 09/29/07 NEW_DISIGN_FOR_SAVING_TEMPLATE_XML
static void _remove_template_label_attribue(TreeNode &tr)
{
	foreach(TreeNode trBranch in tr.Children)
	{
		foreach(TreeNode trCategory in trBranch.Children)
		{
			octree_remove_attribute(&trCategory, STR_LABEL_ATTRIB);
		}
	}
}
//------ End NEW_DISIGN_FOR_SAVING_TEMPLATE_XML

static void _set_category_default_value(TreeNode& trGetN, int nType, string strCategory)
{
	///Jasmine 02/04/09 QA80-13028 SHOW_USER_DEFIND_CATEGORY_INSTEAD_OF_BUILT_IN_CATEGORY_WHEN_SAVING
	//if(!lstrlen(strCategory))
	if( !lstrlen(strCategory) || 0 == strCategory.CompareNoCase(STR_BUILT_IN_CATEGORY) )
	{
		strCategory = "UserDefined";	
	}
	///End SHOW_USER_DEFIND_CATEGORY_INSTEAD_OF_BUILT_IN_CATEGORY_WHEN_SAVING
	
	if( !lstrlen(trGetN.category.strVal) || PDS_AUTO == okutil_cvt_str_to_predefined_type(trGetN.category.strVal) )
		trGetN.category.strVal = strCategory;
	
	vector<string> vsCategories;
	//------ Folger 10/10/07 FIX_BUGS_ABOUT_TEMPLATE_CATEGORY
	//okutil_get_template_categories(STR_TEMPLATE_XML, &vsCategories, nType);
	if (okutil_get_template_categories(STR_TEMPLATE_XML, &vsCategories, nType) == 0)
		okutil_get_template_categories(STR_SYSTEM_TEMPLATE_XML, &vsCategories, nType);
	//------ End FIX_BUGS_ABOUT_TEMPLATE_CATEGORY
	if(-1 == vsCategories.Find(strCategory))
		vsCategories.Add(strCategory);
	if(-1 == vsCategories.Find("UserDefined"))
		vsCategories.Add("UserDefined");
	string strList;
	strList.SetTokens(vsCategories, '|');	
	
	trGetN.category.SetAttribute(STR_COMBO_ATTRIB, "|" + strList);
	trGetN.category.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);		//------ Folger 11/09/07 COMBO_TEXT_NO_NEEDED_LOCALIZE
}
//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
bool _is_path_allow_saving(string strPath)
{
	vector<string> vsPaths;
	vsPaths.Add(okutil_get_origin_path(ORIGIN_PATH_SYSTEM, NULL, TRUE));
	vsPaths.Add(okutil_get_origin_path(ORIGIN_PATH_GROUP, NULL, TRUE));
	for (int ii=0; ii<vsPaths.GetSize(); ii++)
	{
		if (strPath.CompareNoCase(vsPaths[ii]) == 0)
			return false;
	}
	//------ Folger 09/29/07 NOT_ALLOW_SAVE_IN_LOCALIZATION_FOLDER
	///Jasmine 02/24/09 PROTECT_LOCALIZATION_FOLDER
	////hard code, need to clean up later
	//string strLocalization = okutil_get_language_path(true);
	//strLocalization.Delete(strLocalization.GetLength() - 2, 2);
	//strLocalization.Insert(0, GetAppPath(true));
	//strLocalization.MakeUpper();
	string strLocalization = _get_localization_folder();
	///End PROTECT_LOCALIZATION_FOLDER
	if (strPath.Find(strLocalization) == 0)
		return false;
	//------ End NOT_ALLOW_SAVE_IN_LOCALIZATION_FOLDER
	
	return true;
}
//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
static bool _init_dlg(TreeNode& trGetN)
{
	if(!trGetN.IsValid())
		return false;	
	//////page//////
	string strPage = trGetN.pg.strVal;
	strPage.TrimLeft('[');
	strPage.TrimRight(']');
	Page pg(strPage);
	if(!pg.IsValid())
		return error_report("Error! Page is invalid!");
	//---- CPY 8/17/06 QA70-8428 ADD_SAVE_AS_OPTION
	trGetN.pg.Enable = ENABLE_READ_ONLY;
	//----
	
	//---- Jake 04/06/07 ADD_SAVE_TEMPLATE_AS_OPTION
	trGetN.savetemplate.Enable = ENABLE_READ_ONLY;
	//---- end ADD_SAVE_TEMPLATE_AS_OPTION
	
	int nType = pg.GetType();
	string strExt = page_get_template_ext(nType);
	////////Template Name////////
	string strTemplate = trGetN.template.strVal, strFileCurrent;
	if(!lstrlen(strTemplate) || PDS_AUTO == okutil_cvt_str_to_predefined_type(strTemplate))	///Jasmine 06/05/07 TEMPLATE_CHANGE_UNASSIGNED_TO_AUTO
	{
		strFileCurrent = page_get_template_name(pg);
		//------ Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
		//strTemplate = GetFileName(strFileCurrent, true);
		//trGetN.template.strVal = strTemplate;
		strTemplate = okutil_composite_name_from_file_path(strFileCurrent, "Templates");	//find the proper template name
		trGetN.template.strVal = GetFileName(strFileCurrent, true);		//name show in dialog is without "(User)" when user-defined
		//------ End TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
	}
	
	///Jasmine 03/30/09 SHOW_DESCRIPTION_STORED_IN_TEMPLATE
	string strDesc = trGetN.desc.strVal;	
	if( !lstrlen(strDesc) || PDS_AUTO == okutil_cvt_str_to_predefined_type(strDesc) )
	{
		strDesc.Empty();
		okutil_get_Origin_file_comment(strFileCurrent, &strDesc);
		trGetN.desc.strVal = strDesc;	
	}
	///End SHOW_DESCRIPTION_STORED_IN_TEMPLATE
	
	////////Category////////
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	//Tree trXML;
	//load_template_tree(trXML);//trXML.Load(STR_TEMPLATE_XML);
	//TreeNode trBranch = tree_get_node_by_id(trXML, nType);
	//string strFile, strCate;
	//_find_template(trBranch, strTemplateSuffix, strFile, strCate);
	
	TemplateOrganizer to;
	Tree trXML;
	to.GetTemplateListFile(trXML);
	TreeNode trBranch = tree_get_node_by_id(trXML, nType);
	TreeNode trTemplate, trCategory;	
	to.FindTemplate(trBranch, strTemplate, trTemplate, trCategory);
	string strFile, strCate;
	if (trTemplate)
		trTemplate.GetAttribute(STR_FILENAME_ATTRIB, strFile);
	if (trCategory)
		trCategory.GetAttribute(STR_LABEL_ATTRIB, strCate);
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	
	_set_category_default_value(trGetN, nType, strCate);
	
	//---- Jake 04/06/07 ADD_SAVE_TEMPLATE_AS_OPTION
	string strTemplateType;
	if(trBranch)
		trBranch.GetAttribute(STR_LABEL_ATTRIB, strTemplateType);
	//---Jasmine 02/04/09 EXTENSION_IS_ADDED_IN_BRANCH_LABEL, brach label used to be "Graph" and now it's "Graph Template (*.otp)"
	//trGetN.savetemplate.strVal=strTemplateType+" Template [*"+strExt+"]";
	trGetN.savetemplate.strVal = strTemplateType;
	//---end
	//---- end ADD_SAVE_TEMPLATE_AS_OPTION
	
	///Jasmine 01/24/09 #4259 ADD_LOAD_SYSTEM_THEME_CHECK_BOX
	if( EXIST_PLOT == nType )
	{
		trGetN.loadsys.Show = true;
	
		if( PDS_AUTO == okutil_cvt_str_to_predefined_type(trGetN.loadsys.strVal) )
			trGetN.loadsys.nVal = _is_load_system_theme(pg);
	}
	else 
		trGetN.loadsys.Show = false;
	
	///Jasmine 02/03/09 #4259 ADD_HINT_FOR_LOAD_SYSTEM_THEME_CHECK_BOX
	trGetN.hint.SetAttribute(STR_ATTRIB_DISPFMT, DISPLAY_EDITOR_USE_LABEL | DISPLAY_EDITOR_NEXT_LINE | DISPLAY_EDITOR_WRAPPED);
	trGetN.hint.Show = false;
	if(trGetN.loadsys.Show != false && trGetN.loadsys.nVal == true)
	{
		trGetN.hint.Show = true;
	}
	///End ADD_HINT_FOR_LOAD_SYSTEM_THEME_CHECK_BOX
	///End ADD_LOAD_SYSTEM_THEME_CHECK_BOX
	
	///Jasmine 02/09/09 REMOVE_OPTION_BRANCH
	/*
	////////Option///////////
	if(trGetN.option)
	{
		//----- CPY 11/4/07 LOCALIZATION_NEEDED
		// I have put in _L in all needed places withouth separate comments
		//-----
		///Set Default///
		//GETN_CHECK(setdefault,"Set Default", false) ///Jasmine 10/17/06, wait SetDefaultTemplate
	}	
	*/	
	if( !lstrlen(trGetN.filepath.strVal) || PDS_AUTO == okutil_cvt_str_to_predefined_type(trGetN.filepath.strVal) )
	{
		if(lstrlen(strFileCurrent))
			strFile = strFileCurrent;
		
		trGetN.filepath.strVal = lstrlen(strFile) && _is_path_allow_saving(GetFilePath(strFile))? GetFilePath(strFile) : STR_USER_PATH;///set user folder as default
	}
	
	string strSave;
	strSave.Format("...Save:[%s] *%s",strTemplateType, strExt);
   	trGetN.filepath.SetAttribute(STR_COMBO_ATTRIB, strSave);
   	trGetN.filepath.SetAttribute(STR_FILENAME_ATTRIB, trGetN.filepath.strVal + trGetN.template.strVal + strExt);
   	trGetN.filepath.SetAttribute(STR_DLG_OPTIONS_ATTRIB, FDLG_SUPPRESS_WARNING_SAVE_TO_EXISTING|FDLG_FILENAME_BOX_READ_ONLY);
   	tree_set_browse_combo_attrib(trGetN.filepath, NULL, false);	///Jasmine 07/04/07 ADD_PATH_TRACK
	
   	 
   	trGetN.emf.Show = nType == EXIST_PLOT;
   	trGetN.bmp.Show = nType == EXIST_PLOT;
   	///End REMOVE_OPTION_BRANCH
   	
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	//return true;
	return _is_path_allow_saving(GetFilePath(strFileCurrent));
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
}

 

void template_saveas(const Page& pg, const string& category, const string& template, const string& desc, const string& savetemplate, const string& filepath, const int& emf, const int& bmp, const int& loadsys, const string& hint, const int& asksave)
{
	 	
	//------ Folger 03/11/08 QA80-11175 SAVE_LAYOUT_TEMPLATE
	if ( pg.GetType() == EXIST_LAYOUT )
	{
		string strFile = page_get_template_name(pg) + ".otp";
		string strName = pg.GetName();
		string strMsg;
		ocu_load_msg_str(XFWARN_SAVE_AS, &strMsg, strName, NULL, strFile);
		if ( IDYES == MessageBox(GetWindow(), strMsg, _L("Attention!"), MB_YESNO | MB_DEFBUTTON2 | MB_ICONEXCLAMATION) )
		{
			LT_execute("save -Tb 200 %H %A;");
		}
		return;
	}
	//------
	
	//////get page and its type//////
	if( !pg.IsValid() || !lstrlen(template) )//invalid page or no template name!
		XF_THROW(CER_INVALID_ARGUMENT);
	int nType = pg.GetType();
	string strExt = page_get_template_ext(nType);
	//////check path//////
	string strPath = filepath;
	if(PDS_AUTO == okutil_cvt_str_to_predefined_type(strPath) || !lstrlen(strPath))// strParam is <auto> or nothing
		strPath = STR_USER_PATH;
	if(!strPath.IsPath())
		XF_THROW(CER_INVALID_FILEPATH);
	if(strPath.Right(1).CompareNoCase("\\"))
		strPath += "\\";
	//////check template name//////
	string strTemplate = GetFileName(template, true);
	///Jasmine 06/05/07 TEMPLATE_CHANGE_UNASSIGNED_TO_AUTO
	if(!lstrlen(template) || PDS_AUTO == okutil_cvt_str_to_predefined_type(template))
	{
		string strFileCurrent = page_get_template_name(pg);
		strTemplate = GetFileName(strFileCurrent, true);
	}
	///End TEMPLATE_CHANGE_UNASSIGNED_TO_AUTO
	string strFullPathFile = strPath + strTemplate + strExt;
	string strTemplateSuffix = okutil_composite_name_from_file_path(strFullPathFile, "Templates");	//------ Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
	
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	//Tree trXML;
	//load_template_tree(trXML);//trXML.Load(STR_TEMPLATE_XML);
	//TreeNode trBranch = tree_get_node_by_id(trXML, nType);
	//string strFile, strCate;
	//_find_template(trBranch, strTemplateSuffix, strFile, strCate);//check if template name exist in template organizer
	
	TemplateOrganizer to;
	Tree trXML;
	to.GetTemplateListFile(trXML);
	TreeNode trBranch = tree_get_node_by_id(trXML, nType);
	//------ Folger 10/10/07 CORRECT_BEHAVIOR_OF_FINDING_CATEGORY_AND_TEMPLATE
	//TreeNode trTemplate, trCategory;	
	//to.FindTemplate(trBranch, strTemplateSuffix, trTemplate, trCategory);
	//string strCate;
	//if (trCategory)
		//trCategory.GetAttribute(STR_LABEL_ATTRIB, strCate);
		//
	//if (category.CompareNoCase(strCate))
		//trCategory = to.NewCategory(trBranch, category);
	
	TreeNode trCategory;
	if (!to.FindCategory(trBranch, category, trCategory))
		trCategory = to.NewCategory(trBranch, category);
	//------ End CORRECT_BEHAVIOR_OF_FINDING_CATEGORY_AND_TEMPLATE
	
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	///Jasmine 07/04/07 ALLOW_SAVE_TO_DIFFERENT_PATH_AND_UPDATE_ATTRIBUTE_IN_ORGANIZER
	/*
	bool bBuiltIn = is_built_in_template(nType, strTemplate, strFile);
	while(!bBuiltIn && lstrlen(strFile) && strFullPathFile.CompareNoCase(strFile))//the same name, the same path
	{
		///Cheney 2007-5-30 UPDATE_XF_ERR_THROW
		//string strWarning = "Template name is used by "+ strFile +"\nPlease select another name.";
		string strWarning;
		ocu_load_msg_str(XFWARN_TEMPLATE_NAME_IS_USED, &strWarning, strFile);
		///end UPDATE_XF_ERR_THROW
		GETN_TREE(trRename)
			GETN_STR(Name, "Template Name", strTemplate)
		if(!GetNBox(trRename, "Rename", strWarning, NULL, NULL, NULL))
			return;//XF_THROW(XFERR_INVALID_OPERATION);//crash
		strTemplate = GetFileName(trRename.Name.strVal, true);
		strFile.Empty();
		strCate.Empty();
		_find_template(trBranch, strTemplate, strFile, strCate);
	}
	*/
	///End ALLOW_SAVE_TO_DIFFERENT_PATH_AND_UPDATE_ATTRIBUTE_IN_ORGANIZER
	//////check file//////
	//strFullPathFile = strPath + strTemplate + strExt;		//------ Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
	
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	
	//if (s_nGetNDialog == 1)
	//------ Folger 02/27/08 QA80-11175 AUTO_SAVE_SYSTEM_GROUP_TEMPLATE_INTO_UFF
	//if ( asksave )
	///Jasmine 02/09/09 REMOVE_OPTION_BRANCH
	//if ( asksave || option.filepath.strVal.CompareNoCase(GetFilePath(page_get_template_name(pg))) )
	///Jasmine 02/10/09 CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
	if(s_bAskIfExist)
	{
		if ( asksave || filepath.CompareNoCase(GetFilePath(page_get_template_name(pg))) )
		///End REMOVE_OPTION_BRANCH
		//------
		{
			if(strFullPathFile.IsFile())
			{
				///Cheney 2007-5-30 UPDATE_XF_ERR_THROW
				//string strWarning = strFullPathFile + " already exists!\nDo you want to replace it?";
				string strWarning;
				ocu_load_msg_str(XFWARN_TEMPLATE_ALREADY_EXISTS, &strWarning, strFullPathFile);
				///end UPDATE_XF_ERR_THROW
				if(IDNO == MessageBox(NULL, strWarning, _L("Save As"), MB_YESNO|MB_ICONEXCLAMATION))
					XF_THROW(XFERR_INVALID_OPERATION);
			}
		}
		//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	}
	///End CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
	
	//////update template.xml//////
	///Jasmine 07/04/07 ALLOW_SAVE_TO_DIFFERENT_PATH_AND_UPDATE_ATTRIBUTE_IN_ORGANIZER
	//if(bBuiltIn)//need update file name
	//{	
	
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	//bool bReplace = lstrlen(category) && strCate.CompareNoCase(category);	//------ Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
		//foreach(TreeNode trCate in trBranch.Children)
		//{
			//TreeNode trTemp = trCate.FindNodeByAttribute(STR_LABEL_ATTRIB, strTemplateSuffix);
			//if(trTemp.IsValid())
			//{
				////------ Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
				////trTemp.SetAttribute(STR_FILENAME_ATTRIB, strFullPathFile);
				//if (bReplace)
				//{
					//string strWarning;
					//strWarning.Format("%s is in Category %s.\nDo you want to move it to Category %s ?",
										//strTemplateSuffix, strCate, category);
					//if(IDYES == MessageBox(NULL, strWarning, "Save As", MB_YESNO|MB_ICONEXCLAMATION))
					//{
						//trTemp.Remove();
					//}
					//else
						//bReplace = false;
				//}
				////------ End TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
			//}
		//}
		//trXML.Save(STR_TEMPLATE_XML);
		//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
		
	//}
	///End ALLOW_SAVE_TO_DIFFERENT_PATH_AND_UPDATE_ATTRIBUTE_IN_ORGANIZER
	//------ Folger 08/01/07 TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
	//if(lstrlen(category) && category.CompareNoCase(strCate))//a template can add to more than one categories	
	
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	//if (bReplace)
	////------ End TEMPLATE_CAN_ONLY_SAVED_IN_ONE_CATEGORY
	//{
		//int nRet = okutil_add_template(STR_TEMPLATE_XML, category, strFullPathFile, nType);//nRet = 0 if success else nRet = 1
		////FileCopy(STR_TEMPLATE_XML, STR_GROUP_TEMPLATE_XML);	///Jasmine 08/15/07 QA80-10155 NO_MODIFICATION_TO_GROUP_FOLDER_FILE
	//}
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	
	///Jasmine 01/24/09 #4259 ADD_LOAD_SYSTEM_THEME_CHECK_BOX
	if( EXIST_PLOT == pg.GetType() )
		_set_load_system_theme(pg, loadsys);
	///End ADD_LOAD_SYSTEM_THEME_CHECK_BOX
	
	//////save template//////
	//string desc = option.desc? option.desc.strVal : "";	///Jasmine 02/09/09 REMOVE_OPTION_BRANCH
	DWORD	dwCtrl = PAGE_ST_ADD_TO_MRU_LIST; 	/// SY 2007-03-07 QA70-9388 v8.0577 MRU_FOR_TEMPLATE_OGW	
	
	BOOL bRet;
	if(nType == EXIST_PLOT)
	{
		LPCSTR lpcszEMF, lpcszBMP;
		string strEMF(strPath + strTemplate +".emf"), strBMP(strPath + strTemplate +".bmp");
		if( emf && pg.SaveEMF(strEMF) )
			lpcszEMF = strEMF;
		if( bmp && pg.SaveBitmap(strBMP) )
			lpcszBMP = strBMP;
		bRet = pg.SaveTemplate(strFullPathFile, lpcszBMP, lpcszEMF, desc, dwCtrl);	
	}
	else
		bRet = pg.SaveTemplate(strFullPathFile, NULL, NULL, desc, dwCtrl);	
	///Jasmine 10/23/08 QA80-12442 SHOW_WARNING_WHEN_SAVE_READ_ONLY_FILE	
	if(!bRet)	
	{
		popup_errmsg_save_readonly_file(strFullPathFile);
		XF_THROW(XFERR_INVALID_OPERATION);
	}
	///End SHOW_WARNING_WHEN_SAVE_READ_ONLY_FILE
		
	///Jasmine 04/13/07 PRINT_XF_MSG_TO_SCRIPT_WINDOW
	if(strFullPathFile.IsFile())
	{
		update_recent_path_list(GetFilePath(strFullPathFile));	///Jasmine 07/04/07 ADD_PATH_TRACK
		string strMessage;
		///Cheney 2007-6-18 UPDATE_XF_ERR_THROW_NEED_WAIT_SIM_NEW_FUNC_TO_REPLACE
		//strMessage.Format("Save template as %s", strFullPathFile);
		ocu_load_msg_str(XF_TEMPLATE_SAVE_SUCCESS, &strMessage, strFullPathFile);
		///end UPDATE_XF_ERR_THROW_NEED_WAIT_SIM_NEW_FUNC_TO_REPLACE
		///Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
		//xf_print_msg(strMessage);
		okoc_out_msg(strMessage, 'I');
		///End REPLACE_BY_OKOC_OUT_MSG
		
		///Jasmine 02/04/09 MAX_ASK_SHOW_BUILT_IN_TEMPLATE_HINT_IN_ERRMSG
		///Jasmine 02/17/09 SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
		//if(is_built_in_template( nType, template, GetAppPath(TRUE) ) == true)
		if( _override_exe_folder(strFullPathFile) )
			okoc_out_msg(_L("A template file with the same name exists in the Origin Program Folder. Origin will use your custom template instead."), 'I');			
		///End SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER		
	}
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	to.AddTemplate(trCategory, strFullPathFile);	
	octree_delete_nodes_by_missing_attribute(&trXML, STR_USER_PATH_ATTRIB);
	//------ Folger 09/29/07 NEW_DISIGN_FOR_SAVING_TEMPLATE_XML
	octree_remove_attribute(&trXML, STR_FILENAME_ATTRIB);
	_remove_template_label_attribue(trXML);
	//------ End NEW_DISIGN_FOR_SAVING_TEMPLATE_XML
	///Sophy 10/28/08 QA80-12442 SHOW_WARNING_WHEN_SAVE_READ_ONLY_FILE    
	//trXML.Save(STR_TEMPLATE_FILE);
	if( !trXML.Save(STR_TEMPLATE_XML) )
		popup_errmsg_save_readonly_file( STR_TEMPLATE_XML, GetWindow() );
	///end SHOW_WARNING_WHEN_SAVE_READ_ONLY_FILE
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	///End PRINT_XF_MSG_TO_SCRIPT_WINDOW
}

//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 template_saveas_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return template_saveas_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//---- CPY 8/17/06 QA70-8428 ADD_SAVE_AS_OPTION 
	// should not change page display format
	/*
	if(nEventID > 0 && !lstrcmpi(lpcszNodeName, "page"))
	{
		string strPage = trGetN.page.strVal;
		strPage.TrimLeft('[');
		strPage.TrimRight(']');
		Page pg(strPage);
		if(!pg.IsValid())
			return false;
		_on_page_change(pg, trGetN);
	}
	*/
	////find template.xml
	//string strXML = STR_TEMPLATE_XML;
	//if(!strXML.IsFile())
		//construct_template_organizer_tree();
	//------ Folger 08/01/07 INIT_DLG_NO_MATTER_WITH_OR_WITHOUT_DLG
	//if(GETNE_ON_INIT == nEventID)
		//_init_dlg(trGetN);
	//------ End INIT_DLG_NO_MATTER_WITH_OR_WITHOUT_DLG
	//----
	//check
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	if (nEventID == GETNE_ON_INIT)
	{
		if (!_is_path_allow_saving(trGetN.filepath.strVal))
		{
			strErrMsg = XFERR_PROTECT_SAVE_PATH;
			strErrMsg += ":" + STR_INVALID_SAVE_PATH;//_L("System Folder or Group Folder");
			bOKEnable = false;
			return false;
		}
	}
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	if(nEventID > 0)
	{
		if(trGetN.category.IsValid() && !lstrlen(trGetN.category.strVal))
		{
			strErrMsg = CWARN_NOT_ADD_TO_TEMPLATE_ORG;
			bOKEnable = false;
			return false;
		}
		if(trGetN.template.IsValid() && !lstrlen(trGetN.template.strVal))
		{
			strErrMsg = CER_EMPTY_VARIABLE;
			strErrMsg +=":"+ "template";
			bOKEnable = false;
			return false;
		}
		if(trGetN.filepath.IsValid() && (!lstrlen(trGetN.filepath.strVal) || !trGetN.filepath.strVal.IsPath()))
		{
			strErrMsg = CER_INVALID_FILEPATH;
			bOKEnable = false;
			return false;
		}
	}
	///Jasmine 10/09/06 WAIT_NEW_SAVEAS_DB
	if(nEventID > 0 && ( !lstrcmpi(lpcszNodeName, "template") || !lstrcmpi(lpcszNodeName, "filepath") ))
	{
		string strFile;
		trGetN.filepath.GetAttribute(STR_FILENAME_ATTRIB, strFile);
		if(!lstrcmpi(lpcszNodeName, "filepath") && trGetN.template)
		{
			string strPath = trGetN.filepath.strVal;//update from editbox
			if(strPath.Right(1).CompareNoCase("\\"))
				strPath += "\\";
			strFile = strPath + GetFileName(strFile);
			trGetN.template.strVal = GetFileName(strFile, true);
			trGetN.filepath.strVal = GetFilePath(strFile);
		}
		///Jasmine 08/12/09 HARD_CODE_TO_GET_FILE_EXTENSION
		//strFile = GetFilePath(strFile) + trGetN.template.strVal + "." + strFile.GetToken(1, '.');
		string strExt;
		separate_file_name_ext(strFile, NULL, &strExt);		
		strFile = GetFilePath(strFile) + trGetN.template.strVal +  "." + strExt;
		///End HARD_CODE_TO_GET_FILE_EXTENSION
		trGetN.filepath.SetAttribute(STR_FILENAME_ATTRIB, strFile);
	}
	//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	if (nEventID > 0 && !_is_path_allow_saving(trGetN.filepath.strVal))
	{
		strErrMsg = XFERR_PROTECT_SAVE_PATH;
		strErrMsg += ":" + STR_INVALID_SAVE_PATH;
		bOKEnable = false;
		return false;
	}
	//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
	
	///Jasmine 02/03/09 #4259 ADD_HINT_FOR_LOAD_SYSTEM_THEME_CHECK_BOX
	if(trGetN.hint.Show == false && nEventID > 0 && lstrcmpi(lpcszNodeName, "loadsys") == 0)
	{
		ASSERT(trGetN.loadsys.Show != false);
		if(trGetN.loadsys.nVal)
			trGetN.hint.Show = true;
	}
	///End ADD_HINT_FOR_LOAD_SYSTEM_THEME_CHECK_BOX
	
	//Jasmine 02/06/09 USE_REMINDER_MSG_FOR_BUILT_IN_TEMPLATE
	if(GETNE_ON_OK == nEventID)
	{
		//Jasmine 02/04/09 MAX_ASK_SHOW_BUILT_IN_TEMPLATE_HINT_IN_ERRMSG
		/////Kyle 02/04/2009 #4259 ADD_HINT_FOR_MESSAGE_FOR_BUILT_IN_TEMPLATE
		//_check_show_hint_built_in_template(trGetN);
		/////End ADD_HINT_FOR_MESSAGE_FOR_BUILT_IN_TEMPLATE
		string strPage = trGetN.pg.strVal;
		strPage.TrimLeft('[');
		strPage.TrimRight(']');
		Page pg(strPage);
		
		///Jasmine 02/17/09 SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
		//if(pg && is_built_in_template( pg.GetType(), trGetN.template.strVal, GetAppPath(TRUE) ) == true && lstrcmpi(trGetN.filepath.strVal, STR_USER_PATH) == 0)
		string strFullPathFile = trGetN.filepath.strVal + trGetN.template.strVal + page_get_template_ext( pg.GetType() );
		if( _override_exe_folder(strFullPathFile) )
		{
			//strErrMsg = CER_BUILT_IN_TEMPLATE_NAME;
			string strWarning;
			ocu_load_msg_str(CER_BUILT_IN_TEMPLATE_NAME, &strWarning);
			if(IDCANCEL == MessageBox(GetWindow(), strWarning, _L("Save As"), MB_OKCANCEL|MB_ICONEXCLAMATION))
			{
				bOKEnable = false;
				return false;
			}
		}
		///End SHOW_BUILT_IN_MEG_FOR_ALL_TEMPLATE_IN_EXE_FOLDER
		///End MAX_ASK_SHOW_BUILT_IN_TEMPLATE_HINT_IN_ERRMSG	
		
		///Jasmine 02/10/09 CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
		if(strFullPathFile.IsFile())
		{
			string strWarning;
			ocu_load_msg_str(XFWARN_TEMPLATE_ALREADY_EXISTS, &strWarning, strFullPathFile);
			if(IDOK == MessageBox(GetWindow(), strWarning, _L("Save As"), MB_OKCANCEL|MB_ICONEXCLAMATION))
			{
				s_bAskIfExist = false;
			}
			else
			{
				bOKEnable = false;
				return false;
			}
				
		}
		///End CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
	}
	///End USE_REMINDER_MSG_FOR_BUILT_IN_TEMPLATE	
	
	return true;

}

int template_saveas_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return 0;
}
//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 template_saveas_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	//------ Folger 03/11/08 QA80-11175 SAVE_LAYOUT_TEMPLATE
	string strPage = trGetN.pg.strVal;
	strPage.TrimLeft('[');
	strPage.TrimRight(']');
	Page pg(strPage);
	if ( pg && pg.GetType() == EXIST_LAYOUT )
		return;
	//------
		
	//---- CPY 8/17/06 QA70-8428 ADD_SAVE_AS_OPTION 
	//if(1 == nGetNDialog)
	//	_init_dlg(trGetN);
	//----
	//------ Folger 08/01/07 INIT_DLG_NO_MATTER_WITH_OR_WITHOUT_DLG
	if (nGetNDialog >= 0)
	{
		///Jasmine 02/10/09 CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
		s_bAskIfExist = true;
		///End CHANCE_TO_MAKE_CHANGE_IF_FILE_EXIST
		
		//s_nGetNDialog = nGetNDialog;
		//------ Folger 08/24/07 USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML
		//_init_dlg(trGetN);
		//------ Folger 01/07/08 QA70-10908 SHOULD_SAVE_TEMPLATE_INTO_UFF_WHEN_CALL_FROM_SCRIPT
		//asksave is a flag to indicate whether XFCall comes from context menu or not
		//if (!_init_dlg(trGetN) && nGetNDialog == 0)
		if ( !_init_dlg(trGetN) && trGetN.asksave.nVal == 0 )
		//------
		{
			//------ Folger 02/27/08 QA80-11175 AUTO_SAVE_SYSTEM_GROUP_TEMPLATE_INTO_UFF
			//int nErrCode = XFERR_PROTECT_SAVE_PATH;
			//warning_msg_box(nErrCode, STR_INVALID_SAVE_PATH, 1, false, 'E', false);
			//nRet = XFEVT_ABORT;
			int nErrCode = XFERR_PROTECT_SAVE_PATH;
			warning_msg_box(nErrCode, STR_INVALID_SAVE_PATH, 1, false, 'I', false);
			//------ End AUTO_SAVE_SYSTEM_GROUP_TEMPLATE_INTO_UFF
		}
		//------ End USE_TEMPLATE_ORGANIZER_TO_ACCESS_TEMPLATE_XML	}
	}
	//------ End INIT_DLG_NO_MATTER_WITH_OR_WITHOUT_DLG
}
//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 template_saveas_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
