/// <summary> Constructor for a new instance of the Users_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="currentMode"> Mode / navigation information for the current request</param>
        /// <param name="Code_Manager"> List of valid collection codes, including mapping from the Sobek collections to Greenstone collections</param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from a user edit or from reseting a user's password is handled here in the constructor </remarks>
        public Users_AdminViewer(User_Object User, SobekCM_Navigation_Object currentMode, Aggregation_Code_Manager Code_Manager, Custom_Tracer Tracer)
            : base(User)
        {
            Tracer.Add_Trace("Users_AdminViewer.Constructor", String.Empty);

            this.currentMode = currentMode;

            // Ensure the user is the system admin
            if ((User == null) || (!User.Is_System_Admin))
            {
                currentMode.Mode = Display_Mode_Enum.My_Sobek;
                currentMode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                currentMode.Redirect();
                return;
            }

            // Set the action message to clear initially
            actionMessage = String.Empty;
            codeManager = Code_Manager;

            // Get the user to edit, if there was a user id in the submode
            editUser = null;
            if (currentMode.My_Sobek_SubMode.Length > 0)
            {
                try
                {
                    int edit_userid = Convert.ToInt32(currentMode.My_Sobek_SubMode.Replace("a", "").Replace("b", "").Replace("c", "").Replace("v", ""));

                    // Check this admin's session for this user object
                    Object sessionEditUser = HttpContext.Current.Session["Edit_User_" + edit_userid];
                    if (sessionEditUser != null)
                        editUser = (User_Object)sessionEditUser;
                    else
                    {
                        editUser = SobekCM_Database.Get_User(edit_userid, Tracer);
                        editUser.Should_Be_Able_To_Edit_All_Items = false;
                        if (editUser.Editable_Regular_Expressions.Any(thisRegularExpression => thisRegularExpression == "[A-Z]{2}[A-Z|0-9]{4}[0-9]{4}"))
                        {
                            editUser.Should_Be_Able_To_Edit_All_Items = true;
                        }
                    }
                }
                catch (Exception)
                {
                    actionMessage = "Error while handing your request";
                }
            }

            // Determine the mode
            mode = Users_Admin_Mode_Enum.List_Users_And_Groups;
            if (editUser != null)
            {
                mode = currentMode.My_Sobek_SubMode.IndexOf("v") > 0 ? Users_Admin_Mode_Enum.View_User : Users_Admin_Mode_Enum.Edit_User;
            }
            else
            {
                currentMode.My_Sobek_SubMode = String.Empty;
            }

            // Perform post back work
            if (currentMode.isPostBack)
            {
                if (mode == Users_Admin_Mode_Enum.List_Users_And_Groups)
                {
                    try
                    {
                        string reset_value = HttpContext.Current.Request.Form["admin_user_reset"];
                        if (reset_value.Length > 0)
                        {
                            int userid = Convert.ToInt32(reset_value);
                            User_Object reset_user = SobekCM_Database.Get_User(userid, Tracer);

                            // Create the random password
                            StringBuilder passwordBuilder = new StringBuilder();
                            Random randomGenerator = new Random(DateTime.Now.Millisecond);
                            while (passwordBuilder.Length < 12)
                            {
                                switch (randomGenerator.Next(0, 3))
                                {
                                    case 0:
                                        int randomNumber = randomGenerator.Next(65, 91);
                                        if ((randomNumber != 79) && (randomNumber != 75)) // Omit the 'O' and the 'K', confusing
                                            passwordBuilder.Append((char)randomNumber);
                                        break;

                                    case 1:
                                        int randomNumber2 = randomGenerator.Next(97, 123);
                                        if ((randomNumber2 != 111) && (randomNumber2 != 108) && (randomNumber2 != 107))  // Omit the 'o' and the 'l' and the 'k', confusing
                                            passwordBuilder.Append((char)randomNumber2);
                                        break;

                                    case 2:
                                        // Zero and one is omitted in this range, confusing
                                        int randomNumber3 = randomGenerator.Next(50, 58);
                                        passwordBuilder.Append((char)randomNumber3);
                                        break;
                                }
                            }
                            string password = passwordBuilder.ToString();

                            // Reset this password
                            if (!SobekCM_Database.Reset_User_Password(userid, password, true, Tracer))
                            {
                                actionMessage = "ERROR reseting password";
                            }
                            else
                            {

                                if (SobekCM_Database.Send_Database_Email(reset_user.Email, "my" + currentMode.SobekCM_Instance_Abbreviation.ToUpper() + " Password Reset", reset_user.Full_Name + ",\n\nYour my" + currentMode.SobekCM_Instance_Abbreviation.ToUpper() + " password has been reset to a temporary password.  The first time you logon, you will be required to change it.\n\n\tUsername: "******"\n\tPassword: "******"\n\nYour password is case-sensitive and must be entered exactly as it appears above when logging on.\n\nIf you have any questions or problems logging on, feel free to contact us at " + SobekCM_Library_Settings.System_Email + ", or reply to this email.\n\n" + currentMode.Base_URL + "my/home\n", false, false, -1, -1))
                                {
                                    if ((user.UserID == 1) || (user.UserID == 2))
                                        actionMessage = "Reset of password (" + password + ") for '" + reset_user.Full_Name + "' complete";
                                    else
                                        actionMessage = "Reset of password for '" + reset_user.Full_Name + "' complete";
                                }
                                else
                                {
                                    if ((user.UserID == 1) || (user.UserID == 2))
                                        actionMessage = "ERROR while sending new password (" + password + ") to '" + reset_user.Full_Name + "'!";
                                    else
                                        actionMessage = "ERROR while sending new password to '" + reset_user.Full_Name + "'!";
                                }
                            }
                        }
                    }
                    catch
                    {
                        actionMessage = "ERROR while checking postback";
                    }
                }

                if ((mode == Users_Admin_Mode_Enum.Edit_User) && (editUser != null))
                {
                    // Determine which page you are on
                    int page = 1;
                    if (currentMode.My_Sobek_SubMode.IndexOf("b") > 0)
                        page = 2;
                    else if (currentMode.My_Sobek_SubMode.IndexOf("c") > 0)
                        page = 3;

                    // Get a reference to this form
                    NameValueCollection form = HttpContext.Current.Request.Form;
                    string[] getKeys = form.AllKeys;

                    // Get the curret action
                    string action = form["admin_user_save"];

                    bool successful_save = true;
                    switch (page)
                    {
                        case 1:
                            string editTemplate = "Standard";
                            List<string> projects = new List<string>();
                            List<string> templates = new List<string>();

                            // First, set some flags to FALSE
                            editUser.Can_Submit = false;
                            editUser.Is_Internal_User = false;
                            editUser.Should_Be_Able_To_Edit_All_Items = false;
                            editUser.Is_System_Admin = false;
                            editUser.Is_Portal_Admin = false;
                            editUser.Include_Tracking_In_Standard_Forms = false;

                            // Step through each key
                            foreach (string thisKey in getKeys)
                            {
                                switch (thisKey)
                                {
                                    case "admin_user_submit":
                                        editUser.Can_Submit = true;
                                        break;

                                    case "admin_user_internal":
                                        editUser.Is_Internal_User = true;
                                        break;

                                    case "admin_user_editall":
                                        editUser.Should_Be_Able_To_Edit_All_Items = true;
                                        break;

                                    case "admin_user_deleteall":
                                        editUser.Can_Delete_All = true;
                                        break;

                                    case "admin_user_sysadmin":
                                        editUser.Is_System_Admin = true;
                                        break;

                                    case "admin_user_portaladmin":
                                        editUser.Is_Portal_Admin = true;
                                        break;

                                    case "admin_user_includetracking":
                                        editUser.Include_Tracking_In_Standard_Forms = true;
                                        break;

                                    case "admin_user_edittemplate":
                                        editTemplate = form["admin_user_edittemplate"];
                                        break;

                                    case "admin_user_organization":
                                        editUser.Organization = form["admin_user_organization"];
                                        break;

                                    case "admin_user_college":
                                        editUser.College = form["admin_user_college"];
                                        break;

                                    case "admin_user_department":
                                        editUser.Department = form["admin_user_department"];
                                        break;

                                    case "admin_user_unit":
                                        editUser.Unit = form["admin_user_unit"];
                                        break;

                                    case "admin_user_org_code":
                                        editUser.Organization_Code = form["admin_user_org_code"];
                                        break;

                                    default:
                                        if (thisKey.IndexOf("admin_user_template_") == 0)
                                        {
                                            templates.Add(thisKey.Replace("admin_user_template_", ""));
                                        }
                                        if (thisKey.IndexOf("admin_user_project_") == 0)
                                        {
                                            projects.Add(thisKey.Replace("admin_user_project_", ""));
                                        }
                                        break;
                                }
                            }

                            // Determine the name for the actual edit templates from the combo box selection
                            editUser.Edit_Template_Code = "edit";
                            editUser.Edit_Template_MARC_Code = "editmarc";
                            if (editTemplate == "internal")
                            {
                                editUser.Edit_Template_Code = "edit_internal";
                                editUser.Edit_Template_MARC_Code = "editmarc_internal";
                            }

                            // Determine if the projects and templates need to be updated
                            bool update_templates_projects = false;
                            if ((templates.Count != editUser.Templates.Count) || (projects.Count != editUser.Default_Metadata_Sets.Count))
                            {
                                update_templates_projects = true;
                            }
                            else
                            {
                                // Check all of the templates
                                if (templates.Any(template => !editUser.Templates.Contains(template)))
                                {
                                    update_templates_projects = true;
                                }

                                // Check all the projects
                                if (!update_templates_projects)
                                {
                                    if (projects.Any(project => !editUser.Default_Metadata_Sets.Contains(project)))
                                    {
                                        update_templates_projects = true;
                                    }
                                }
                            }

                            // Update the templates and projects, if requested
                            if (update_templates_projects)
                            {
                                // Get the last defaults
                                string default_project = String.Empty;
                                string default_template = String.Empty;
                                if (editUser.Default_Metadata_Sets.Count > 0)
                                    default_project = editUser.Default_Metadata_Sets[0];
                                if (editUser.Templates.Count > 0)
                                    default_template = editUser.Templates[0];

                                // Now, set the user's template and projects
                                editUser.Clear_Default_Metadata_Sets();
                                editUser.Clear_Templates();
                                foreach (string thisProject in projects)
                                {
                                    editUser.Add_Default_Metadata_Set(thisProject, false);
                                }
                                foreach (string thisTemplate in templates)
                                {
                                    editUser.Add_Template(thisTemplate, false);
                                }

                                // Try to add back the defaults, which won't do anything if
                                // the old defaults aren't in the new list
                                editUser.Set_Current_Default_Metadata(default_project);
                                editUser.Set_Default_Template(default_template);
                            }
                            break;

                        case 2:
                            // Check the user groups for update
                            bool update_user_groups = false;
                            DataTable userGroup = SobekCM_Database.Get_All_User_Groups(Tracer);
                            List<string> newGroups = new List<string>();
                            foreach (DataRow thisRow in userGroup.Rows)
                            {
                                if (form["group_" + thisRow["UserGroupID"]] != null)
                                {
                                    newGroups.Add(thisRow["GroupName"].ToString());
                                }
                            }

                            // Should we add the new user groups?  Did it change?
                            if (newGroups.Count != editUser.User_Groups.Count)
                            {
                                update_user_groups = true;
                            }
                            else
                            {
                                foreach (string thisGroup in newGroups)
                                {
                                    if (!editUser.User_Groups.Contains(thisGroup))
                                    {
                                        update_user_groups = true;
                                        break;
                                    }
                                }
                            }
                            if (update_user_groups)
                            {
                                editUser.Clear_UserGroup_Membership();
                                foreach (string thisUserGroup in newGroups)
                                    editUser.Add_User_Group(thisUserGroup);
                            }
                            break;

                        case 3:
                            Dictionary<string, User_Editable_Aggregation> aggregations = new Dictionary<string, User_Editable_Aggregation>();

                            // Step through each key
                            foreach (string thisKey in getKeys)
                            {
                                if (thisKey.IndexOf("admin_project_onhome_") == 0)
                                {
                                    string select_project = thisKey.Replace("admin_project_onhome_", "");
                                    if (aggregations.ContainsKey(select_project))
                                    {
                                        aggregations[select_project].OnHomePage = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(select_project, new User_Editable_Aggregation(select_project, String.Empty, false, false, false, true, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_select_") == 0)
                                {
                                    string select_project = thisKey.Replace("admin_project_select_", "");
                                    if (aggregations.ContainsKey(select_project))
                                    {
                                        aggregations[select_project].CanSelect = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(select_project, new User_Editable_Aggregation(select_project, String.Empty, true, false, false, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_editall_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditItems = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(edit_project, new User_Editable_Aggregation(edit_project, String.Empty, false, true, false, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_edit_metadata_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_metadata_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditMetadata = true;
                                    }
                                    else
                                    {
                                        User_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.CanEditMetadata = true;
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_edit_behavior_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_behavior_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditBehaviors = true;
                                    }
                                    else
                                    {
                                        User_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.CanEditBehaviors = true;
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_perform_qc_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_perform_qc_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanPerformQc = true;
                                    }
                                    else
                                    {
                                        User_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.CanPerformQc = true;
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_upload_files_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_upload_files_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanUploadFiles = true;
                                    }
                                    else
                                    {
                                        User_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.CanUploadFiles = true;
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_change_visibility_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_change_visibility_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanChangeVisibility = true;
                                    }
                                    else
                                    {
                                        User_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.CanChangeVisibility = true;
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_can_delete_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_can_delete_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanDelete = true;
                                    }
                                    else
                                    {
                                        User_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.CanDelete = true;
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_curator_") == 0)
                                {
                                    string admin_project = thisKey.Replace("admin_project_curator_", "");
                                    if (aggregations.ContainsKey(admin_project))
                                    {
                                        aggregations[admin_project].IsCurator = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(admin_project, new User_Editable_Aggregation(admin_project, String.Empty, false, false, true, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_admin_") == 0)
                                {
                                    string admin_project = thisKey.Replace("admin_project_admin_", "");
                                    if (aggregations.ContainsKey(admin_project))
                                    {
                                        aggregations[admin_project].IsAdmin = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(admin_project, new User_Editable_Aggregation(admin_project, String.Empty, false, false, false, false, true));
                                    }
                                }
                            }

                            // Determine if the aggregations need to be edited
                            bool update_aggregations = false;
                            if (aggregations.Count != editUser.Aggregations.Count)
                            {
                                update_aggregations = true;
                            }
                            else
                            {
                                // Build a dictionary of the user aggregations as well
                                Dictionary<string, User_Editable_Aggregation> existingAggr = editUser.Aggregations.ToDictionary(thisAggr => thisAggr.Code);

                                // Check all the aggregations
                                foreach (User_Editable_Aggregation adminAggr in aggregations.Values)
                                {
                                    if (existingAggr.ContainsKey(adminAggr.Code))
                                    {
                                        if ((adminAggr.CanSelect != existingAggr[adminAggr.Code].CanSelect) || (adminAggr.CanEditMetadata != existingAggr[adminAggr.Code].CanEditMetadata) ||
                                            (adminAggr.CanEditBehaviors != existingAggr[adminAggr.Code].CanEditBehaviors) || (adminAggr.CanPerformQc != existingAggr[adminAggr.Code].CanPerformQc) ||
                                            (adminAggr.CanUploadFiles != existingAggr[adminAggr.Code].CanUploadFiles) || (adminAggr.CanChangeVisibility != existingAggr[adminAggr.Code].CanChangeVisibility) ||
                                            (adminAggr.CanDelete != existingAggr[adminAggr.Code].CanDelete) || (adminAggr.IsCurator != existingAggr[adminAggr.Code].IsCurator) || (adminAggr.OnHomePage != existingAggr[adminAggr.Code].OnHomePage))
                                        {
                                            update_aggregations = true;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        update_aggregations = true;
                                        break;
                                    }
                                }
                            }

                            // Update the aggregations, if requested
                            if (update_aggregations)
                            {
                                editUser.Clear_Aggregations();
                                if (aggregations.Count > 0)
                                {
                                    foreach (User_Editable_Aggregation dictionaryAggregation in aggregations.Values)
                                    {
                                        editUser.Add_Aggregation(dictionaryAggregation.Code, dictionaryAggregation.Name, dictionaryAggregation.CanSelect, dictionaryAggregation.CanEditMetadata, dictionaryAggregation.CanEditBehaviors, dictionaryAggregation.CanPerformQc, dictionaryAggregation.CanUploadFiles, dictionaryAggregation.CanChangeVisibility, dictionaryAggregation.CanDelete, dictionaryAggregation.IsCurator, dictionaryAggregation.OnHomePage, dictionaryAggregation.IsAdmin, false);
                                    }
                                }
                            }
                            break;
                    }

                    // Should this be saved to the database?
                    if (action == "save")
                    {
                        // Save this user
                        SobekCM_Database.Save_User(editUser, String.Empty, user.Authentication_Type, Tracer);

                        // Update the basic user information
                        SobekCM_Database.Update_SobekCM_User(editUser.UserID, editUser.Can_Submit, editUser.Is_Internal_User, editUser.Should_Be_Able_To_Edit_All_Items, editUser.Can_Delete_All, editUser.Is_System_Admin, editUser.Is_Portal_Admin, editUser.Include_Tracking_In_Standard_Forms, editUser.Edit_Template_Code, editUser.Edit_Template_MARC_Code, true, true, true, Tracer);

                        // Update projects, if necessary
                        if (editUser.Default_Metadata_Sets.Count > 0)
                        {
                            if (!SobekCM_Database.Update_SobekCM_User_DefaultMetadata(editUser.UserID, editUser.Default_Metadata_Sets, Tracer))
                            {
                                successful_save = false;
                            }
                        }

                        // Update templates, if necessary
                        if (editUser.Templates.Count > 0)
                        {
                            if (!SobekCM_Database.Update_SobekCM_User_Templates(editUser.UserID, editUser.Templates, Tracer))
                            {
                                successful_save = false;
                            }
                        }

                        // Save the aggregations linked to this user
                        if (!SobekCM_Database.Update_SobekCM_User_Aggregations(editUser.UserID, editUser.Aggregations, Tracer))
                        {
                            successful_save = false;
                        }

                        // Save the user group links
                        DataTable userGroup = SobekCM_Database.Get_All_User_Groups(Tracer);
                        Dictionary<string, int> groupnames_to_id = new Dictionary<string, int>();
                        foreach (DataRow thisRow in userGroup.Rows)
                        {
                            groupnames_to_id[thisRow["GroupName"].ToString()] = Convert.ToInt32(thisRow["UserGroupID"]);
                        }
                        foreach (string userGroupName in editUser.User_Groups)
                        {
                            SobekCM_Database.Link_User_To_User_Group(editUser.UserID, groupnames_to_id[userGroupName]);
                        }

                        // Forward back to the list of users, if this was successful
                        if (successful_save)
                        {
                            // Clear the user from the sessions
                            HttpContext.Current.Session["Edit_User_" + editUser.UserID] = null;

                            // Redirect the user
                            currentMode.My_Sobek_SubMode = String.Empty;
                            currentMode.Redirect();
                        }
                    }
                    else
                    {
                        // Save to the admins session
                        HttpContext.Current.Session["Edit_User_" + editUser.UserID] = editUser;
                        currentMode.My_Sobek_SubMode = action;
                        currentMode.Redirect();
                    }
                }
            }
        }
        /// <summary> Constructor for a new instance of the Users_AdminViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </param>
        /// <remarks> Postback from a RequestSpecificValues.Current_User edit or from reseting a RequestSpecificValues.Current_User's password is handled here in the constructor </remarks>
        public Users_AdminViewer(RequestCache RequestSpecificValues)
            : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("Users_AdminViewer.Constructor", String.Empty);

            // Ensure the user is the system admin
            if ((RequestSpecificValues.Current_User == null) || (!RequestSpecificValues.Current_User.Is_System_Admin))
            {
                RequestSpecificValues.Current_Mode.Mode = Display_Mode_Enum.My_Sobek;
                RequestSpecificValues.Current_Mode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Set the action message to clear initially
            actionMessage = String.Empty;

            // Get the user to edit, if there was a user id in the submode
            editUser = null;
            if ( !String.IsNullOrEmpty(RequestSpecificValues.Current_Mode.My_Sobek_SubMode))
            {
                try
                {
                    int edit_userid = Convert.ToInt32(RequestSpecificValues.Current_Mode.My_Sobek_SubMode.Replace("a", "").Replace("b", "").Replace("c", "").Replace("d", "").Replace("v", ""));

                    // Check this admin's session for this RequestSpecificValues.Current_User object
                    Object sessionEditUser = HttpContext.Current.Session["Edit_User_" + edit_userid];
                    if (sessionEditUser != null)
                        editUser = (User_Object)sessionEditUser;
                    else
                    {
                        editUser = Engine_Database.Get_User(edit_userid, RequestSpecificValues.Tracer);
                        editUser.Should_Be_Able_To_Edit_All_Items = false;
                        if (editUser.Editable_Regular_Expressions.Any(ThisRegularExpression => ThisRegularExpression == "[A-Z]{2}[A-Z|0-9]{4}[0-9]{4}"))
                        {
                            editUser.Should_Be_Able_To_Edit_All_Items = true;
                        }
                    }
                }
                catch (Exception)
                {
                    actionMessage = "Error while handing your request";
                }
            }

            // Determine the mode
            mode = Users_Admin_Mode_Enum.List_Users_And_Groups;
            if (editUser != null)
            {
                mode = RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("v") > 0 ? Users_Admin_Mode_Enum.View_User : Users_Admin_Mode_Enum.Edit_User;
            }
            else
            {
                RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
            }

            // Determine if TEI is enabled (and pull configuration if it is)
            if (mode == Users_Admin_Mode_Enum.Edit_User)
            {
                tei_plugin_enabled = false;
                if ((UI_ApplicationCache_Gateway.Configuration.Extensions != null) &&
                    (UI_ApplicationCache_Gateway.Configuration.Extensions.Get_Extension("TEI") != null) &&
                    (UI_ApplicationCache_Gateway.Configuration.Extensions.Get_Extension("TEI").Enabled))
                {
                    // TEI enabled
                    tei_plugin_enabled = true;

                    // Try to pull the configuration from the cache, otherwise create it manually
                    teiConfig = HttpContext.Current.Cache.Get("TEI.Configuration") as TEI_Configuration;

                    // Did not find it in the cache
                    if (teiConfig == null)
                    {
                        // Build the new object then
                        string plugin_directory = Path.Combine(UI_ApplicationCache_Gateway.Settings.Servers.Application_Server_Network, "plugins", "tei");
                        teiConfig = new TEI_Configuration(plugin_directory);

                        // Store on the cache for several minutes
                        HttpContext.Current.Cache.Insert("TEI.Configuration", teiConfig, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(2));
                    }
                }
            }

            // Perform post back work
            if (RequestSpecificValues.Current_Mode.isPostBack)
            {
                if (mode == Users_Admin_Mode_Enum.List_Users_And_Groups)
                {
                    try
                    {
                        string reset_value = HttpContext.Current.Request.Form["admin_user_reset"];
                        if (reset_value.Length > 0)
                        {
                            int userid = Convert.ToInt32(reset_value);
                            User_Object reset_user = Engine_Database.Get_User(userid, RequestSpecificValues.Tracer);

                            // Create the random password
                            StringBuilder passwordBuilder = new StringBuilder();
                            Random randomGenerator = new Random(DateTime.Now.Millisecond);
                            while (passwordBuilder.Length < 12)
                            {
                                switch (randomGenerator.Next(0, 3))
                                {
                                    case 0:
                                        int randomNumber = randomGenerator.Next(65, 91);
                                        if ((randomNumber != 79) && (randomNumber != 75)) // Omit the 'O' and the 'K', confusing
                                            passwordBuilder.Append((char)randomNumber);
                                        break;

                                    case 1:
                                        int randomNumber2 = randomGenerator.Next(97, 123);
                                        if ((randomNumber2 != 111) && (randomNumber2 != 108) && (randomNumber2 != 107))  // Omit the 'o' and the 'l' and the 'k', confusing
                                            passwordBuilder.Append((char)randomNumber2);
                                        break;

                                    case 2:
                                        // Zero and one is omitted in this range, confusing
                                        int randomNumber3 = randomGenerator.Next(50, 58);
                                        passwordBuilder.Append((char)randomNumber3);
                                        break;
                                }
                            }
                            string password = passwordBuilder.ToString();

                            // Reset this password
                            if (!SobekCM_Database.Reset_User_Password(userid, password, true, RequestSpecificValues.Tracer))
                            {
                                actionMessage = "ERROR reseting password";
                            }
                            else
                            {
                                if ( Email_Helper.SendEmail(reset_user.Email, "my" + RequestSpecificValues.Current_Mode.Instance_Abbreviation.ToUpper() + " Password Reset", reset_user.Full_Name + ",\n\nYour my" + RequestSpecificValues.Current_Mode.Instance_Abbreviation.ToUpper() + " password has been reset to a temporary password.  The first time you logon, you will be required to change it.\n\n\tUsername: "******"\n\tPassword: "******"\n\nYour password is case-sensitive and must be entered exactly as it appears above when logging on.\n\nIf you have any questions or problems logging on, feel free to contact us at " + UI_ApplicationCache_Gateway.Settings.Email.System_Email + ", or reply to this email.\n\n" + RequestSpecificValues.Current_Mode.Base_URL + "my/home\n", false, RequestSpecificValues.Current_Mode.Instance_Name))
                                {
                                    if ((RequestSpecificValues.Current_User.UserID == 1) || (RequestSpecificValues.Current_User.UserID == 2))
                                        actionMessage = "Reset of password (" + password + ") for '" + reset_user.Full_Name + "' complete";
                                    else
                                        actionMessage = "Reset of password for '" + reset_user.Full_Name + "' complete";
                                }
                                else
                                {
                                    if ((RequestSpecificValues.Current_User.UserID == 1) || (RequestSpecificValues.Current_User.UserID == 2))
                                        actionMessage = "ERROR while sending new password (" + password + ") to '" + reset_user.Full_Name + "'!";
                                    else
                                        actionMessage = "ERROR while sending new password to '" + reset_user.Full_Name + "'!";
                                }
                            }
                        }

                        string delete_value = HttpContext.Current.Request.Form["admin_user_group_delete"];
                        if (delete_value.Length > 0)
                        {
                            int deleteId = Convert.ToInt32(delete_value);
                            int result = SobekCM_Database.Delete_User_Group(deleteId, null);
                            switch (result)
                            {
                                case 1:
                                    actionMessage = "Succesfully deleted user group";
                                    break;

                                case -1:
                                    actionMessage = "ERROR while deleting user group - Cannot delete a user group which is still linked to users";
                                    break;

                                case -2:
                                    actionMessage = "ERROR - You cannot delete a special user group";
                                    break;

                                case -3:
                                    actionMessage = "ERROR while deleting user group - unknown exception caught";
                                    break;

                            }
                            return;
                        }
                    }
                    catch
                    {
                        actionMessage = "ERROR while checking postback";
                    }
                }

                if ((mode == Users_Admin_Mode_Enum.Edit_User) && (editUser != null))
                {
                    // Determine which page you are on
                    int page = 1;
                    if (RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("b") > 0)
                        page = 2;
                    else if (RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("c") > 0)
                        page = 3;

                    // Allow page 4 if TEI is enabled
                    if ((tei_plugin_enabled) && ((RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("d") > 0)))
                        page = 4;

                    // Get a reference to this form
                    NameValueCollection form = HttpContext.Current.Request.Form;
                    string[] getKeys = form.AllKeys;

                    // Get the curret action
                    string action = form["admin_user_save"];

                    // If this is CANCEL, get rid of the currrent edit object in the session
                    if (action == "cancel")
                    {
                        // Clear the RequestSpecificValues.Current_User from the sessions
                        HttpContext.Current.Session["Edit_User_" + editUser.UserID] = null;

                        // Redirect the RequestSpecificValues.Current_User
                        RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        return;
                    }

                    bool successful_save = true;
                    switch (page)
                    {
                        case 1:
                            string editTemplate = "Standard";
                            List<string> projects = new List<string>();
                            List<string> templates = new List<string>();

                            // First, set some flags to FALSE
                            editUser.Can_Submit = false;
                            editUser.Is_Internal_User = false;
                            editUser.Should_Be_Able_To_Edit_All_Items = false;
                            editUser.Is_System_Admin = false;
                            editUser.Is_Portal_Admin = false;
                            editUser.Include_Tracking_In_Standard_Forms = false;
                            editUser.Can_Delete_All = false;

                            if ((UI_ApplicationCache_Gateway.Settings.Servers.isHosted) && (RequestSpecificValues.Current_User.Is_Host_Admin))
                            {
                                editUser.Is_Host_Admin = false;
                            }

                            // Step through each key
                            foreach (string thisKey in getKeys)
                            {
                                switch (thisKey)
                                {
                                    case "admin_user_submit":
                                        editUser.Can_Submit = true;
                                        break;

                                    case "admin_user_internal":
                                        editUser.Is_Internal_User = true;
                                        break;

                                    case "admin_user_editall":
                                        editUser.Should_Be_Able_To_Edit_All_Items = true;
                                        break;

                                    case "admin_user_deleteall":
                                        editUser.Can_Delete_All = true;
                                        break;

                                    case "admin_user_host":
                                        editUser.Is_Host_Admin = true;
                                        break;

                                    case "admin_user_sysadmin":
                                        editUser.Is_System_Admin = true;
                                        break;

                                    case "admin_user_portaladmin":
                                        editUser.Is_Portal_Admin = true;
                                        break;

                                    case "admin_user_includetracking":
                                        editUser.Include_Tracking_In_Standard_Forms = true;
                                        break;

                                    case "admin_user_edittemplate":
                                        editTemplate = form["admin_user_edittemplate"];
                                        break;

                                    case "admin_user_organization":
                                        editUser.Organization = form["admin_user_organization"];
                                        break;

                                    case "admin_user_college":
                                        editUser.College = form["admin_user_college"];
                                        break;

                                    case "admin_user_department":
                                        editUser.Department = form["admin_user_department"];
                                        break;

                                    case "admin_user_unit":
                                        editUser.Unit = form["admin_user_unit"];
                                        break;

                                    case "admin_user_org_code":
                                        editUser.Organization_Code = form["admin_user_org_code"];
                                        break;

                                    default:
                                        if (thisKey.IndexOf("admin_user_template_") == 0)
                                        {
                                            templates.Add(thisKey.Replace("admin_user_template_", ""));
                                        }
                                        if (thisKey.IndexOf("admin_user_project_") == 0)
                                        {
                                            projects.Add(thisKey.Replace("admin_user_project_", ""));
                                        }
                                        break;
                                }
                            }

                            // Determine the name for the actual edit templates from the combo box selection
                            editUser.Edit_Template_Code_Simple = "edit";
                            editUser.Edit_Template_Code_Complex = "editmarc";
                            if (editTemplate == "internal")
                            {
                                editUser.Edit_Template_Code_Simple = "edit_internal";
                                editUser.Edit_Template_Code_Complex = "editmarc_internal";
                            }

                            // Determine if the projects and templates need to be updated
                            bool update_templates_projects = false;
                            if ((templates.Count != editUser.Templates.Count) || (projects.Count != editUser.Default_Metadata_Sets.Count))
                            {
                                update_templates_projects = true;
                            }
                            else
                            {
                                // Check all of the templates
                                if (templates.Any(template => !editUser.Templates.Contains(template)))
                                {
                                    update_templates_projects = true;
                                }

                                // Check all the projects
                                if (!update_templates_projects)
                                {
                                    if (projects.Any(project => !editUser.Default_Metadata_Sets.Contains(project)))
                                    {
                                        update_templates_projects = true;
                                    }
                                }
                            }

                            // Update the templates and projects, if requested
                            if (update_templates_projects)
                            {
                                // Get the last defaults
                                string default_project = String.Empty;
                                string default_template = String.Empty;
                                if (editUser.Default_Metadata_Sets.Count > 0)
                                    default_project = editUser.Default_Metadata_Sets[0];
                                if (editUser.Templates.Count > 0)
                                    default_template = editUser.Templates[0];

                                // Now, set the RequestSpecificValues.Current_User's template and projects
                                editUser.Clear_Default_Metadata_Sets();
                                editUser.Clear_Templates();
                                foreach (string thisProject in projects)
                                {
                                    editUser.Add_Default_Metadata_Set(thisProject, false);
                                }
                                foreach (string thisTemplate in templates)
                                {
                                    editUser.Add_Template(thisTemplate, false);
                                }

                                // Try to add back the defaults, which won't do anything if
                                // the old defaults aren't in the new list
                                editUser.Set_Current_Default_Metadata(default_project);
                                editUser.Set_Default_Template(default_template);
                            }
                            break;

                        case 2:
                            // Check the RequestSpecificValues.Current_User groups for update
                            bool update_user_groups = false;
                            List<User_Group> userGroup = Engine_Database.Get_All_User_Groups(RequestSpecificValues.Tracer);
                            List<string> newGroups = new List<string>();
                            foreach (User_Group thisRow in userGroup)
                            {
                                if (form["group_" + thisRow.UserGroupID] != null)
                                {
                                    newGroups.Add(thisRow.Name);
                                }
                            }

                            // Should we add the new RequestSpecificValues.Current_User groups?  Did it change?
                            if (newGroups.Count != editUser.User_Groups.Count)
                            {
                                update_user_groups = true;
                            }
                            else
                            {
                                foreach (string thisGroup in newGroups)
                                {
                                    if (!editUser.User_Groups.Contains(thisGroup))
                                    {
                                        update_user_groups = true;
                                        break;
                                    }
                                }
                            }
                            if (update_user_groups)
                            {
                                editUser.Clear_UserGroup_Membership();
                                foreach (string thisUserGroup in newGroups)
                                    editUser.Add_User_Group(thisUserGroup);
                            }
                            break;

                        case 3:
                            Dictionary<string, User_Permissioned_Aggregation> aggregations = new Dictionary<string, User_Permissioned_Aggregation>();

                            // Step through each key
                            foreach (string thisKey in getKeys)
                            {
                                if (thisKey.IndexOf("admin_project_onhome_") == 0)
                                {
                                    string select_project = thisKey.Replace("admin_project_onhome_", "");
                                    if (aggregations.ContainsKey(select_project))
                                    {
                                        aggregations[select_project].OnHomePage = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(select_project, new User_Permissioned_Aggregation(select_project, String.Empty, false, false, false, true, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_select_") == 0)
                                {
                                    string select_project = thisKey.Replace("admin_project_select_", "");
                                    if (aggregations.ContainsKey(select_project))
                                    {
                                        aggregations[select_project].CanSelect = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(select_project, new User_Permissioned_Aggregation(select_project, String.Empty, true, false, false, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_editall_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditItems = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(edit_project, new User_Permissioned_Aggregation(edit_project, String.Empty, false, true, false, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_edit_metadata_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_metadata_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditMetadata = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false) {CanEditMetadata = true};
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_edit_behavior_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_behavior_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditBehaviors = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false) {CanEditBehaviors = true};
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_perform_qc_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_perform_qc_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanPerformQc = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false) {CanPerformQc = true};
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_upload_files_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_upload_files_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanUploadFiles = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false) {CanUploadFiles = true};
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_change_visibility_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_change_visibility_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanChangeVisibility = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false) {CanChangeVisibility = true};
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_can_delete_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_can_delete_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanDelete = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false) {CanDelete = true};
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_curator_") == 0)
                                {
                                    string admin_project = thisKey.Replace("admin_project_curator_", "");
                                    if (aggregations.ContainsKey(admin_project))
                                    {
                                        aggregations[admin_project].IsCurator = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(admin_project, new User_Permissioned_Aggregation(admin_project, String.Empty, false, false, true, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_admin_") == 0)
                                {
                                    string admin_project = thisKey.Replace("admin_project_admin_", "");
                                    if (aggregations.ContainsKey(admin_project))
                                    {
                                        aggregations[admin_project].IsAdmin = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(admin_project, new User_Permissioned_Aggregation(admin_project, String.Empty, false, false, false, false, true));
                                    }
                                }
                            }

                            // Determine if the aggregationPermissions need to be edited
                            bool update_aggregations = false;
                            if (editUser.PermissionedAggregations == null || (aggregations.Count != editUser.PermissionedAggregations.Count))
                            {
                                update_aggregations = true;
                            }
                            else
                            {
                                // Build a dictionary of the RequestSpecificValues.Current_User aggregationPermissions as well
                                Dictionary<string, User_Permissioned_Aggregation> existingAggr = editUser.PermissionedAggregations.ToDictionary(ThisAggr => ThisAggr.Code);

                                // Check all the aggregationPermissions
                                foreach (User_Permissioned_Aggregation adminAggr in aggregations.Values)
                                {
                                    if (existingAggr.ContainsKey(adminAggr.Code))
                                    {
                                        if ((adminAggr.CanSelect != existingAggr[adminAggr.Code].CanSelect) || (adminAggr.CanEditMetadata != existingAggr[adminAggr.Code].CanEditMetadata) ||
                                            (adminAggr.CanEditBehaviors != existingAggr[adminAggr.Code].CanEditBehaviors) || (adminAggr.CanPerformQc != existingAggr[adminAggr.Code].CanPerformQc) ||
                                            (adminAggr.CanUploadFiles != existingAggr[adminAggr.Code].CanUploadFiles) || (adminAggr.CanChangeVisibility != existingAggr[adminAggr.Code].CanChangeVisibility) ||
                                            (adminAggr.CanDelete != existingAggr[adminAggr.Code].CanDelete) || (adminAggr.IsCurator != existingAggr[adminAggr.Code].IsCurator) ||
                                            (adminAggr.OnHomePage != existingAggr[adminAggr.Code].OnHomePage) || (adminAggr.IsAdmin != existingAggr[adminAggr.Code].IsAdmin))
                                        {
                                            update_aggregations = true;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        update_aggregations = true;
                                        break;
                                    }
                                }
                            }

                            // Update the aggregationPermissions, if requested
                            if (update_aggregations)
                            {
                                editUser.Clear_Aggregations();
                                if (aggregations.Count > 0)
                                {
                                    foreach (User_Permissioned_Aggregation dictionaryAggregation in aggregations.Values)
                                    {
                                        editUser.Add_Aggregation(dictionaryAggregation.Code, dictionaryAggregation.Name, dictionaryAggregation.CanSelect, dictionaryAggregation.CanEditMetadata, dictionaryAggregation.CanEditBehaviors, dictionaryAggregation.CanPerformQc, dictionaryAggregation.CanUploadFiles, dictionaryAggregation.CanChangeVisibility, dictionaryAggregation.CanDelete, dictionaryAggregation.IsCurator, dictionaryAggregation.OnHomePage, dictionaryAggregation.IsAdmin, false);
                                    }
                                }
                            }
                            break;

                        // TEI plug-in permissions
                        case 4:

                            // First, check to see if TEI is enabled
                            if (form["admin_user_tei_enabled"] == null)
                            {
                                // If the setting is already the same, no need to update the database
                                if (editUser.Get_Setting("TEI.Enabled", "false") != "false")
                                {
                                    if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.Enabled", "false") )
                                        editUser.Add_Setting("TEI.Enabled", "false");
                                }
                            }
                            else
                            {
                                // If the setting is already the same, no need to update the database
                                if (editUser.Get_Setting("TEI.Enabled", "false") != "true")
                                {
                                    if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.Enabled", "true"))
                                        editUser.Add_Setting("TEI.Enabled", "true");
                                }
                            }

                            // Now, look for XSLT file links
                            foreach (string thisFileName in teiConfig.XSLT_Files)
                            {
                                // Look for this checkbox
                                if (form["admin_user_tei_xslt_" + thisFileName.ToLower()] == null)
                                {
                                    // If the setting is already the same, no need to update the database
                                    if (editUser.Get_Setting("TEI.XSLT." + thisFileName.ToUpper(), "false") != "false")
                                    {
                                        if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.XSLT." + thisFileName.ToUpper(), "false"))
                                            editUser.Add_Setting("TEI.XSLT." + thisFileName.ToUpper(), "false");
                                    }
                                }
                                else
                                {
                                    // If the setting is already the same, no need to update the database
                                    if (editUser.Get_Setting("TEI.XSLT." + thisFileName.ToUpper(), "false") != "true")
                                    {
                                        if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.XSLT." + thisFileName.ToUpper(), "true"))
                                            editUser.Add_Setting("TEI.XSLT." + thisFileName.ToUpper(), "true");
                                    }
                                }
                            }

                            // Look for CSS file links
                            foreach (string thisFileName in teiConfig.CSS_Files)
                            {
                                // Look for this checkbox
                                if (form["admin_user_tei_css_" + thisFileName.ToLower()] == null)
                                {
                                    // If the setting is already the same, no need to update the database
                                    if (editUser.Get_Setting("TEI.CSS." + thisFileName.ToUpper(), "false") != "false")
                                    {
                                       if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.CSS." + thisFileName.ToUpper(), "false") )
                                           editUser.Add_Setting("TEI.CSS." + thisFileName.ToUpper(), "false");
                                    }
                                }
                                else
                                {
                                    // If the setting is already the same, no need to update the database
                                    if (editUser.Get_Setting("TEI.CSS." + thisFileName.ToUpper(), "false") != "true")
                                    {
                                        if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.CSS." + thisFileName.ToUpper(), "true"))
                                            editUser.Add_Setting("TEI.CSS." + thisFileName.ToUpper(), "true");
                                    }
                                }
                            }

                            // Look for mapping file links
                            foreach (string thisFileName in teiConfig.Mapping_Files)
                            {
                                // Look for this checkbox
                                if (form["admin_user_tei_mapping_" + thisFileName.ToLower()] == null)
                                {
                                    // If the setting is already the same, no need to update the database
                                    if (editUser.Get_Setting("TEI.MAPPING." + thisFileName.ToUpper(), "false") != "false")
                                    {
                                        if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.MAPPING." + thisFileName.ToUpper(), "false"))
                                            editUser.Add_Setting("TEI.MAPPING." + thisFileName.ToUpper(), "false");
                                    }
                                }
                                else
                                {
                                    // If the setting is already the same, no need to update the database
                                    if (editUser.Get_Setting("TEI.MAPPING." + thisFileName.ToUpper(), "false") != "true")
                                    {
                                        if ( SobekCM_Database.Set_User_Setting(editUser.UserID, "TEI.MAPPING." + thisFileName.ToUpper(), "true"))
                                            editUser.Add_Setting("TEI.MAPPING." + thisFileName.ToUpper(), "true");
                                    }
                                }
                            }

                            break;
                    }

                    // Should this be saved to the database?
                    if (action == "save")
                    {
                        // Save this user
                        SobekCM_Database.Save_User(editUser, String.Empty, RequestSpecificValues.Current_User.Authentication_Type, RequestSpecificValues.Tracer);

                        // Update the basic user information
                        SobekCM_Database.Update_SobekCM_User(editUser.UserID, editUser.Can_Submit, editUser.Is_Internal_User, editUser.Should_Be_Able_To_Edit_All_Items, editUser.Can_Delete_All, editUser.Is_System_Admin, editUser.Is_Host_Admin, editUser.Is_Portal_Admin, editUser.Include_Tracking_In_Standard_Forms, editUser.Edit_Template_Code_Simple, editUser.Edit_Template_Code_Complex, true, true, true, RequestSpecificValues.Tracer);

                        // Update projects, if necessary
                        if (editUser.Default_Metadata_Sets.Count > 0)
                        {
                            if (!SobekCM_Database.Update_SobekCM_User_DefaultMetadata(editUser.UserID, editUser.Default_Metadata_Sets, RequestSpecificValues.Tracer))
                            {
                                successful_save = false;
                            }
                        }

                        // Update templates, if necessary
                        if (editUser.Templates_Count > 0)
                        {
                            if (!SobekCM_Database.Update_SobekCM_User_Templates(editUser.UserID, editUser.Templates, RequestSpecificValues.Tracer))
                            {
                                successful_save = false;
                            }
                        }

                        // Save the aggregationPermissions linked to this user
                        if (editUser.PermissionedAggregations_Count > 0)
                        {
                            if (!SobekCM_Database.Update_SobekCM_User_Aggregations(editUser.UserID, editUser.PermissionedAggregations, RequestSpecificValues.Tracer))
                            {
                                successful_save = false;
                            }
                        }

                        // Save the user group links
                        List<User_Group> userGroup = Engine_Database.Get_All_User_Groups(RequestSpecificValues.Tracer);
                        Dictionary<string, int> groupnames_to_id = new Dictionary<string, int>();
                        foreach (User_Group thisRow in userGroup)
                        {
                            groupnames_to_id[thisRow.Name] = Convert.ToInt32(thisRow.UserGroupID);
                        }
                        foreach (string userGroupName in editUser.User_Groups)
                        {
                            SobekCM_Database.Link_User_To_User_Group(editUser.UserID, groupnames_to_id[userGroupName]);
                        }

                        // Forward back to the list of users, if this was successful
                        if (successful_save)
                        {
                            // Clear the RequestSpecificValues.Current_User from the sessions
                            HttpContext.Current.Session["Edit_User_" + editUser.UserID] = null;

                            // Redirect the RequestSpecificValues.Current_User
                            RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
                            UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        }
                    }
                    else
                    {
                        // Save to the admins session
                        HttpContext.Current.Session["Edit_User_" + editUser.UserID] = editUser;
                        RequestSpecificValues.Current_Mode.My_Sobek_SubMode = action;
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    }
                }
            }
        }