public void macroPropertyCreate(object sender, EventArgs e)
		{
			TextBox macroPropertyAliasNew = (TextBox)((Control)sender).Parent.FindControl("macroPropertyAliasNew");
			TextBox macroPropertyNameNew = (TextBox)((Control)sender).Parent.FindControl("macroPropertyNameNew");
			DropDownList macroPropertyTypeNew = (DropDownList)((Control)sender).Parent.FindControl("macroPropertyTypeNew");
			bool _goAhead = true;
			if (macroPropertyAliasNew.Text !=
				ui.Text("general", "new", this.getUser()) + " " + ui.Text("general", "alias", this.getUser()))
			{


				foreach (cms.businesslogic.macro.MacroProperty mp in m_macro.Properties)
				{
					if (mp.Alias == macroPropertyAliasNew.Text)
					{
						_goAhead = false;
						break;

					}
				}

				if (_goAhead)
				{
					MacroProperty mp = new MacroProperty();
					mp.Macro = m_macro;
					mp.Public = true;
					mp.Type = new MacroPropertyType(int.Parse(macroPropertyTypeNew.SelectedValue));
					mp.Alias = macroPropertyAliasNew.Text;
					mp.Name = macroPropertyNameNew.Text;
					mp.Save();

					m_macro.RefreshProperties();
					macroPropertyBind();
				}
			}
		}
		protected void Page_Load(object sender, EventArgs e)
		{
			m_macro = new Macro(Convert.ToInt32(Request.QueryString["macroID"]));

			if (!IsPostBack)
			{

				ClientTools
					.SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree<loadMacros>().Tree.Alias)
					.SyncTree("-1,init," + m_macro.Id.ToString(), false);

				string tempMacroAssembly = m_macro.Assembly ?? "";
				string tempMacroType = m_macro.Type ?? "";

				PopulateFieldsOnLoad(m_macro, tempMacroAssembly, tempMacroType);

				// Check for assemblyBrowser
				if (tempMacroType.IndexOf(".ascx") > 0)
					assemblyBrowserUserControl.Controls.Add(
						new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('" + IOHelper.ResolveUrl(SystemDirectories.Umbraco) + "/developer/macros/assemblyBrowser.aspx?fileName=" + macroUserControl.Text +
										   "&macroID=" + m_macro.Id.ToString() +
										   "', 'Browse Properties', true, 475,500); return false;\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));
				else if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty)
					assemblyBrowser.Controls.Add(
						new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('" + IOHelper.ResolveUrl(SystemDirectories.Umbraco) + "/developer/macros/assemblyBrowser.aspx?fileName=" + macroAssembly.Text +
										   "&macroID=" + m_macro.Id.ToString() + "&type=" + macroType.Text +
										   "', 'Browse Properties', true, 475,500); return false\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));

				// Load elements from macro
				macroPropertyBind();

				// Load xslt files from default dir
				PopulateXsltFiles();

				// Load python files from default dir
				PopulatePythonFiles();

				// Load usercontrols
				PopulateUserControls(IOHelper.MapPath(SystemDirectories.UserControls));
				userControlList.Items.Insert(0, new ListItem("Browse usercontrols on server...", string.Empty));

			}
			else
			{
				int macroID = Convert.ToInt32(Request.QueryString["macroID"]);

				string tempMacroAssembly = macroAssembly.Text;
				string tempMacroType = macroType.Text;
				string tempCachePeriod = cachePeriod.Text;
				if (tempCachePeriod == string.Empty)
					tempCachePeriod = "0";
				if (tempMacroAssembly == string.Empty && macroUserControl.Text != string.Empty)
					tempMacroType = macroUserControl.Text;

				SetMacroValuesFromPostBack(m_macro, Convert.ToInt32(tempCachePeriod), tempMacroAssembly, tempMacroType);
				
				m_macro.Save();				
				
				// Save elements
				foreach (RepeaterItem item in macroProperties.Items)
				{
					HtmlInputHidden macroPropertyID = (HtmlInputHidden)item.FindControl("macroPropertyID");
					TextBox macroElementName = (TextBox)item.FindControl("macroPropertyName");
					TextBox macroElementAlias = (TextBox)item.FindControl("macroPropertyAlias");
					CheckBox macroElementShow = (CheckBox)item.FindControl("macroPropertyHidden");
					DropDownList macroElementType = (DropDownList)item.FindControl("macroPropertyType");

					MacroProperty mp = new MacroProperty(int.Parse(macroPropertyID.Value));
					mp.Type = new MacroPropertyType(int.Parse(macroElementType.SelectedValue));
					mp.Alias = macroElementAlias.Text;
					mp.Name = macroElementName.Text;
					mp.Save();

				}
				// Flush macro from cache!
				if (UmbracoSettings.UseDistributedCalls)
					dispatcher.Refresh(
						new Guid("7B1E683C-5F34-43dd-803D-9699EA1E98CA"),
						macroID);
				else
					macro.GetMacro(macroID).removeFromCache();

                ClientTools.ShowSpeechBubble(speechBubbleIcon.save, "Macro saved", "");


				// Check for assemblyBrowser
				if (tempMacroType.IndexOf(".ascx") > 0)
					assemblyBrowserUserControl.Controls.Add(
						new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('developer/macros/assemblyBrowser.aspx?fileName=" + macroUserControl.Text +
							"&macroID=" + Request.QueryString["macroID"] +
								"', 'Browse Properties', true, 500, 475); return false\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));
				else if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty)
					assemblyBrowser.Controls.Add(
						new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('developer/macros/assemblyBrowser.aspx?fileName=" + macroAssembly.Text +
							"&macroID=" + Request.QueryString["macroID"] + "&type=" + macroType.Text +
								"', 'Browse Properties', true, 500, 475); return false\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));
			}
		}
        protected void Page_Load(object sender, EventArgs e)
        {
            m_macro = new Macro(Convert.ToInt32(Request.QueryString["macroID"]));

            if (!IsPostBack)
            {

                ClientTools
                    .SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree<loadMacros>().Tree.Alias)
                    .SyncTree(m_macro.Id.ToString(), false);

                macroName.Text = m_macro.Name;
                macroAlias.Text = m_macro.Alias;
                string tempMacroAssembly = m_macro.Assembly == null ? "" : m_macro.Assembly;
                string tempMacroType = m_macro.Type == null ? "" : m_macro.Type;
                macroXslt.Text = m_macro.Xslt;
                macroPython.Text = m_macro.ScriptingFile;
                cachePeriod.Text = m_macro.RefreshRate.ToString();

                macroRenderContent.Checked = m_macro.RenderContent;
                macroEditor.Checked = m_macro.UseInEditor;
                cacheByPage.Checked = m_macro.CacheByPage;
                cachePersonalized.Checked = m_macro.CachePersonalized;

                // Populate either user control or custom control
                if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty)
                {
                    macroAssembly.Text = tempMacroAssembly;
                    macroType.Text = tempMacroType;
                }
                else
                {
                    macroUserControl.Text = tempMacroType;
                }

                // Check for assemblyBrowser
                if (tempMacroType.IndexOf(".ascx") > 0)
                    assemblyBrowserUserControl.Controls.Add(
                        new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('" + umbraco.IO.IOHelper.ResolveUrl(umbraco.IO.SystemDirectories.Umbraco) + "/developer/macros/assemblyBrowser.aspx?fileName=" + macroUserControl.Text +
                                           "&macroID=" + m_macro.Id.ToString() +
                                           "', 'Browse Properties', true, 475,500); return false;\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));
                else if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty)
                    assemblyBrowser.Controls.Add(
                        new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('" + umbraco.IO.IOHelper.ResolveUrl(umbraco.IO.SystemDirectories.Umbraco) + "/developer/macros/assemblyBrowser.aspx?fileName=" + macroAssembly.Text +
                                           "&macroID=" + m_macro.Id.ToString() + "&type=" + macroType.Text +
                                           "', 'Browse Properties', true, 475,500); return false\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));

                // Load elements from macro
                macroPropertyBind();

                // Load xslt files from default dir
                populateXsltFiles();

                // Load python files from default dir
                populatePythonFiles();

                // Load usercontrols
                populateUserControls(IOHelper.MapPath(SystemDirectories.Usercontrols) );
                userControlList.Items.Insert(0, new ListItem("Browse usercontrols on server...", string.Empty));
                userControlList.Attributes.Add("onChange",
                    "document.getElementById('" + macroUserControl.ClientID + "').value = this[this.selectedIndex].value;");

            }
            else
            {
                int macroID = Convert.ToInt32(Request.QueryString["macroID"]);
                string tempMacroAssembly = macroAssembly.Text;
                string tempMacroType = macroType.Text;
                string tempCachePeriod = cachePeriod.Text;

                if (tempCachePeriod == string.Empty)
                    tempCachePeriod = "0";

                if (tempMacroAssembly == string.Empty && macroUserControl.Text != string.Empty)
                    tempMacroType = macroUserControl.Text;

                // Save macro
                m_macro.UseInEditor = macroEditor.Checked;
                m_macro.RenderContent = macroRenderContent.Checked;
                m_macro.CacheByPage = cacheByPage.Checked;
                m_macro.CachePersonalized = cachePersonalized.Checked;
                m_macro.RefreshRate = Convert.ToInt32(tempCachePeriod);
                m_macro.Alias = macroAlias.Text;
                m_macro.Name = macroName.Text;
                m_macro.Assembly = tempMacroAssembly;
                m_macro.Type = tempMacroType;
                m_macro.Xslt = macroXslt.Text;
                m_macro.ScriptingFile = macroPython.Text;
                m_macro.Save();

                // Save elements
                foreach (RepeaterItem item in macroProperties.Items)
                {
                    HtmlInputHidden macroPropertyID = (HtmlInputHidden)item.FindControl("macroPropertyID");
                    TextBox macroElementName = (TextBox)item.FindControl("macroPropertyName");
                    TextBox macroElementAlias = (TextBox)item.FindControl("macroPropertyAlias");
                    CheckBox macroElementShow = (CheckBox)item.FindControl("macroPropertyHidden");
                    DropDownList macroElementType = (DropDownList)item.FindControl("macroPropertyType");

                    MacroProperty mp = new MacroProperty(int.Parse(macroPropertyID.Value));
                    mp.Public = macroElementShow.Checked;
                    mp.Type = new MacroPropertyType(int.Parse(macroElementType.SelectedValue));
                    mp.Alias = macroElementAlias.Text;
                    mp.Name = macroElementName.Text;
                    mp.Save();

                }
                // Flush macro from cache!
                if (UmbracoSettings.UseDistributedCalls)
                    dispatcher.Refresh(
                        new Guid("7B1E683C-5F34-43dd-803D-9699EA1E98CA"),
                        macroID);
                else
                    new macro(macroID).removeFromCache();

                // Check for assemblyBrowser
                if (tempMacroType.IndexOf(".ascx") > 0)
                    assemblyBrowserUserControl.Controls.Add(
                        new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('developer/macros/assemblyBrowser.aspx?fileName=" + macroUserControl.Text +
                            "&macroID=" + Request.QueryString["macroID"] +
                                "', 'Browse Properties', true, 500, 475); return false\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));
                else if (tempMacroType != string.Empty && tempMacroAssembly != string.Empty)
                    assemblyBrowser.Controls.Add(
                        new LiteralControl("<br/><button onClick=\"UmbClientMgr.openModalWindow('developer/macros/assemblyBrowser.aspx?fileName=" + macroAssembly.Text +
                            "&macroID=" + Request.QueryString["macroID"] + "&type=" + macroType.Text +
                                "', 'Browse Properties', true, 500, 475); return false\" class=\"guiInputButton\"><img src=\"../../images/editor/propertiesNew.gif\" align=\"absmiddle\" style=\"width: 18px; height: 17px; padding-right: 5px;\"/> Browse properties</button>"));
            }
        }