/// <summary>
        /// saves the current filter tree as a prepared filter tree
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveAsPrepared_Click(object sender, EventArgs e)
        {
            Parser p = GetFilterTreeParser();

            if (p == null)
            {
                Message.Exclaim("Filter tree is blank.");
                return;
            }
            if (p.RootFilters == null || p.RootFilters.Length < 1)
            {
                Message.Exclaim("No filter tree is active.");
                return;
            }

            NameForm nf = new NameForm((string)PreparedFilterTrees.SelectedItem);

            do
            {
                nf.StartPosition = FormStartPosition.CenterParent;
                if (nf.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                if (string.IsNullOrEmpty(nf.FileName))
                {
                    return;
                }
                if (!PreparedFilterTrees.Items.Contains(nf.FileName))
                {
                    break;
                }
            }while (!Message.YesNo(nf.FileName + " already exists. Do you want to overwrite it?"));

            string prepared_filter_tree_name = nf.FileName;
            string prepared_filter_tree_file = prepared_filter_tree_dir + "\\" + prepared_filter_tree_name + "." + Program.FilterTreeFileExtension;

            if (!Directory.Exists(prepared_filter_tree_dir))
            {
                Directory.CreateDirectory(prepared_filter_tree_dir);
            }

            FileInfo fi = new FileInfo(prepared_filter_tree_file);

            if (fi.Exists)
            {
                Directory.CreateDirectory(deleted_prepared_filter_tree_dir);
                string new_old_file = deleted_prepared_filter_tree_dir + "\\" + prepared_filter_tree_name + "_" + DateTime.Now.ToString("yyMMddHHmmss") + "." + Program.FilterTreeFileExtension;
                fi.MoveTo(new_old_file);
                Message.Inform("The old prepared filter tree was moved to the folder:\n" + deleted_prepared_filter_tree_dir);
            }

            p.WriteFilterTreeToXmlFile(prepared_filter_tree_file);
            load_prepared_filter_trees();

            FilterTreeChanged = false;
        }
        /// <summary>
        /// saves current filter collection into file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                string file      = null;
                string file_name = FilterTreeName.Text;
                if (file_name != null)
                {
                    file_name.Trim();
                }
                if (!string.IsNullOrEmpty(FilterTreeFileDir.Text) && !string.IsNullOrEmpty(file_name))
                {
                    file = FilterTreeFileDir.Text + "\\" + file_name;
                    if (!file_name.EndsWith("." + Program.FilterTreeFileExtension, StringComparison.InvariantCultureIgnoreCase))
                    {
                        file += "." + Program.FilterTreeFileExtension;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(file_name))
                    {
                        file_name = "_" + DateTime.Now.ToString("yyMMddHHmmss") + "." + Program.FilterTreeFileExtension;
                    }

                    SaveFileDialog d = new SaveFileDialog();
                    d.OverwritePrompt = false;
                    d.FileName        = file_name;
                    d.Title           = "Save the current filter tree to a file";
                    d.AddExtension    = true;
                    d.DefaultExt      = Program.FilterTreeFileExtension;
                    d.Filter          = "Filter tree files (*." + Program.FilterTreeFileExtension + ")|*." + Program.FilterTreeFileExtension + "|All files (*.*)|*.*";
                    if (!string.IsNullOrEmpty(Settings.Default.LastFilterTreeFile))
                    {
                        d.InitialDirectory = Path.GetDirectoryName(Settings.Default.LastFilterTreeFile);
                    }
                    if (d.ShowDialog(this) != DialogResult.OK)
                    {
                        return;
                    }
                    Settings.Default.LastFilterTreeFile = d.FileName;
                    Settings.Default.Save();
                    file = d.FileName;
                }

                Parser p = GetFilterTreeParser();
                if (p == null)
                {
                    return;
                }
                if (p.RootFilters == null || p.RootFilters.Length < 1)
                {
                    return;
                }

                FileInfo fi = new FileInfo(file);
                if (fi.Exists)
                {
                    string old_dir = fi.Directory + "\\_rewritten_filters";
                    Directory.CreateDirectory(old_dir);
                    string old_file = old_dir + "\\" + fi.Name.Insert(fi.Name.LastIndexOf("."), "_" + DateTime.Now.ToString("yyMMddHHmmss"));
                    fi.MoveTo(old_file);
                }

                p.WriteFilterTreeToXmlFile(file);

                int p1 = file.LastIndexOf(@"\") + 1;
                int p2 = file.LastIndexOf(".");
                FilterTreeName.Text = file.Substring(p1, p2 - p1);

                FilterTreeChanged = false;

                if (!string.IsNullOrWhiteSpace(Document.File))
                {
                    string ft_folder = Path.GetDirectoryName(file);
                    if (Settings.Default.FilterTreeFolder2SourceFolder.Contains(ft_folder))
                    {
                        Settings.Default.FilterTreeFolder2SourceFolder.Remove(ft_folder);
                    }
                    Settings.Default.FilterTreeFolder2SourceFolder.Add(ft_folder, Path.GetDirectoryName(Document.File));
                    if (Settings.Default.FilterTreeFolder2SourceFolder.Count > 30)
                    {
                        Settings.Default.FilterTreeFolder2SourceFolder.RemoveAt(0);
                    }
                    Settings.Default.Save();
                }
            }
            catch (Exception ex)
            {
                Message.Error(ex);
            }
        }