/// <summary> Constructor for a new instance of the IP_Restrictions_AdminViewer class </summary>
        /// <param name="RequestSpecificValues"> All the necessary, non-global data specific to the current request </param>
        /// <remarks> Postback from handling an edit or new item aggregation alias is handled here in the constructor </remarks>
        public IP_Restrictions_AdminViewer(RequestCache RequestSpecificValues) : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("IP_Restrictions_AdminViewer.Constructor", String.Empty);

            // Set some defaults
            entered_title   = String.Empty;
            entered_notes   = String.Empty;
            entered_message = String.Empty;

            // Ensure the RequestSpecificValues.Current_User is the system admin
            if ((RequestSpecificValues.Current_User == null) || ((!RequestSpecificValues.Current_User.Is_System_Admin) && (!RequestSpecificValues.Current_User.Is_Portal_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;
            }

            // Determine if there is an specific IP address range for editing
            int index = -1;

            if (!String.IsNullOrEmpty(RequestSpecificValues.Current_Mode.My_Sobek_SubMode))
            {
                if (!Int32.TryParse(RequestSpecificValues.Current_Mode.My_Sobek_SubMode, out index))
                {
                    index = -1;
                }
            }

            // If there was an index included, try to pull the information about it
            thisRange = null;
            details   = null;
            if (index >= 1)
            {
                thisRange = UI_ApplicationCache_Gateway.IP_Restrictions[index];
                if (thisRange != null)
                {
                    details = SobekCM_Database.Get_IP_Restriction_Range_Details(thisRange.RangeID, RequestSpecificValues.Tracer);
                }
            }

            readOnlyMode = true;
            if (((RequestSpecificValues.Current_User.Is_System_Admin) && (!UI_ApplicationCache_Gateway.Settings.Servers.isHosted)) ||
                (RequestSpecificValues.Current_User.Is_Host_Admin))
            {
                readOnlyMode = false;
            }

            if ((RequestSpecificValues.Current_Mode.isPostBack) && (RequestSpecificValues.Current_User.Is_System_Admin))
            {
                if (readOnlyMode)
                {
                    return;
                }

                // Get a reference to this form
                NameValueCollection form = HttpContext.Current.Request.Form;

                string action = form["action"].Trim();

                if (action == "new")
                {
                    // Pull the main values
                    entered_title   = form["new_admin_title"].Trim();
                    entered_notes   = form["new_admin_notes"].Trim();
                    entered_message = form["new_admin_message"].Trim();

                    if ((entered_title.Length == 0) || (entered_message.Length == 0))
                    {
                        actionMessage = "Both title and message are required fields";
                    }
                    else
                    {
                        if (SobekCM_Database.Edit_IP_Range(-1, entered_title, entered_notes, entered_message, RequestSpecificValues.Tracer))
                        {
                            actionMessage = "Saved new IP range '" + entered_title + "'";

                            entered_title   = String.Empty;
                            entered_notes   = String.Empty;
                            entered_message = String.Empty;

                            // Need to recalcualte the IP range membership for the current user
                            HttpContext.Current.Session["IP_Range_Membership"] = null;
                        }
                        else
                        {
                            actionMessage = "Error saving new IP range '" + entered_title + "'";
                        }
                    }
                }
                else if (action == "delete")
                {
                    int id_to_delete = Int32.Parse(form["admin_ip_delete"]);

                    string delete_title = UI_ApplicationCache_Gateway.IP_Restrictions[id_to_delete].Title;

                    if (SobekCM_Database.Delete_IP_Range(id_to_delete, RequestSpecificValues.Tracer))
                    {
                        actionMessage = "Deleted IP range '" + delete_title + "'";

                        // Need to recalcualte the IP range membership for the current user
                        HttpContext.Current.Session["IP_Range_Membership"] = null;
                    }
                    else
                    {
                        actionMessage = "Error deleting new IP range '" + delete_title + "'";
                    }
                }
                else if ((details != null) && (thisRange != null))
                {
                    try
                    {
                        // Pull the main values
                        string title   = form["admin_title"].Trim();
                        string notes   = form["admin_notes"].Trim();
                        string message = form["admin_message"].Trim();

                        if (title.Length == 0)
                        {
                            title = thisRange.Title;
                        }

                        // Edit the main values in the database
                        SobekCM_Database.Edit_IP_Range(thisRange.RangeID, title, notes, message, RequestSpecificValues.Tracer);
                        thisRange.Title = title;
                        thisRange.Notes = notes;
                        thisRange.Item_Restricted_Statement = message;

                        // Now check each individual IP address range
                        string[] getKeys         = form.AllKeys;
                        int      single_ip_index = 0;
                        foreach (string thisKey in getKeys)
                        {
                            // Is this for a new ip address?
                            if (thisKey.IndexOf("admin_ipstart_") == 0)
                            {
                                // Get the basic information for this single ip address
                                string ip_index    = thisKey.Replace("admin_ipstart_", "");
                                string thisIpStart = form["admin_ipstart_" + ip_index].Trim();
                                string thisIpEnd   = form["admin_ipend_" + ip_index].Trim();
                                string thisIpNote  = form["admin_iplabel_" + ip_index].Trim();

                                // Does this match an existing IP range?
                                if ((ip_index.IndexOf("new") < 0) && (single_ip_index < details.Tables[1].Rows.Count))
                                {
                                    // Get the pre-existing IP row
                                    DataRow ipRow      = details.Tables[1].Rows[single_ip_index];
                                    int     singleIpId = Convert.ToInt32(ipRow[0]);
                                    if (thisIpStart.Length == 0)
                                    {
                                        SobekCM_Database.Delete_Single_IP(singleIpId, RequestSpecificValues.Tracer);
                                    }
                                    else
                                    {
                                        // Is this the same?
                                        if ((thisIpStart != ipRow[1].ToString().Trim()) || (thisIpEnd != ipRow[2].ToString().Trim()) || (thisIpNote != ipRow[3].ToString().Trim()))
                                        {
                                            int edit_point_count = thisIpStart.Count(ThisChar => ThisChar == '.');

                                            if (edit_point_count == 3)
                                            {
                                                SobekCM_Database.Edit_Single_IP(singleIpId, thisRange.RangeID, thisIpStart, thisIpEnd, thisIpNote, RequestSpecificValues.Tracer);
                                            }
                                        }
                                    }

                                    // Be ready to look at the next pre-existing IP range
                                    single_ip_index++;
                                }
                                else
                                {
                                    // Just add this as a new single ip address
                                    if (thisIpStart.Length > 0)
                                    {
                                        int add_point_count = thisIpStart.Count(ThisChar => ThisChar == '.');

                                        if (add_point_count == 3)
                                        {
                                            SobekCM_Database.Edit_Single_IP(-1, thisRange.RangeID, thisIpStart, thisIpEnd, thisIpNote, RequestSpecificValues.Tracer);
                                        }
                                    }
                                }
                            }
                        }

                        // Need to recalcualte the IP range membership for the current user
                        HttpContext.Current.Session["IP_Range_Membership"] = null;
                    }
                    catch (Exception)
                    {
                        actionMessage = "Error saving IP range";
                    }
                }


                // Repopulate the restriction table
                DataTable ipRestrictionTbl = SobekCM_Database.Get_IP_Restriction_Ranges(RequestSpecificValues.Tracer);
                if (ipRestrictionTbl != null)
                {
                    UI_ApplicationCache_Gateway.IP_Restrictions.Populate_IP_Ranges(ipRestrictionTbl);
                }

                // Forward back to the main form
                if (String.IsNullOrEmpty(actionMessage))
                {
                    RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
                    UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                }
            }
        }
        /// <summary> Constructor for a new instance of the Aliases_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="currentMode"> Mode / navigation information for the current request</param>
        /// <param name="IP_Restrictions"> List of all IP restrictions ranges used in this digital library to restrict access to certain digital resources </param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from handling an edit or new item aggregation alias is handled here in the constructor </remarks>
        public IP_Restrictions_AdminViewer(User_Object User, SobekCM_Navigation_Object currentMode, IP_Restriction_Ranges IP_Restrictions, Custom_Tracer Tracer) : base(User)
        {
            Tracer.Add_Trace("IP_Restrictions_AdminViewer.Constructor", String.Empty);

            ipRestrictionInfo = IP_Restrictions;
            this.currentMode  = currentMode;

            // Ensure the user is the system admin
            if ((User == null) || (!User.Is_System_Admin))
            {
                currentMode.My_Sobek_Type = My_Sobek_Type_Enum.Home;
                HttpContext.Current.Response.Redirect(currentMode.Redirect_URL(), false);
            }

            // Determine if there is an specific IP address range for editing
            index = -1;
            if (currentMode.My_Sobek_SubMode.Length > 0)
            {
                if (!Int32.TryParse(currentMode.My_Sobek_SubMode, out index))
                {
                    index = -1;
                }
            }

            // If there was an index included, try to pull the information about it
            thisRange = null;
            details   = null;
            if ((index >= 1) && (index <= ipRestrictionInfo.Count))
            {
                thisRange = ipRestrictionInfo[index - 1];
                if (thisRange != null)
                {
                    details = SobekCM_Database.Get_IP_Restriction_Range_Details(thisRange.RangeID, Tracer);
                }
            }

            if ((currentMode.isPostBack) && (details != null) && (thisRange != null))
            {
                try
                {
                    // Get a reference to this form
                    NameValueCollection form = HttpContext.Current.Request.Form;

                    // Pull the main values
                    string title   = form["admin_title"].Trim();
                    string notes   = form["admin_notes"].Trim();
                    string message = form["admin_message"].Trim();

                    if (title.Length == 0)
                    {
                        title = thisRange.Title;
                    }

                    // Edit the main values in the database
                    SobekCM_Database.Edit_IP_Range(thisRange.RangeID, title, notes, message, Tracer);
                    thisRange.Title = title;
                    thisRange.Notes = notes;
                    thisRange.Item_Restricted_Statement = message;

                    // Now check each individual IP address range
                    string[] getKeys         = form.AllKeys;
                    int      single_ip_index = 0;
                    foreach (string thisKey in getKeys)
                    {
                        // Is this for a new ip address?
                        if (thisKey.IndexOf("admin_ipstart_") == 0)
                        {
                            // Get the basic information for this single ip address
                            string ip_index    = thisKey.Replace("admin_ipstart_", "");
                            string thisIpStart = form["admin_ipstart_" + ip_index].Trim();
                            string thisIpEnd   = form["admin_ipend_" + ip_index].Trim();
                            string thisIpNote  = form["admin_iplabel_" + ip_index].Trim();

                            // Does this match an existing IP range?
                            if ((ip_index.IndexOf("new") < 0) && (single_ip_index < details.Tables[1].Rows.Count))
                            {
                                // Get the pre-existing IP row
                                DataRow ipRow      = details.Tables[1].Rows[single_ip_index];
                                int     singleIpId = Convert.ToInt32(ipRow[0]);
                                if (thisIpStart.Length == 0)
                                {
                                    SobekCM_Database.Delete_Single_IP(singleIpId, Tracer);
                                }
                                else
                                {
                                    // Is this the same?
                                    if ((thisIpStart != ipRow[1].ToString().Trim()) || (thisIpEnd != ipRow[2].ToString().Trim()) || (thisIpNote != ipRow[3].ToString().Trim()))
                                    {
                                        int edit_point_count = thisIpStart.Count(thisChar => thisChar == '.');

                                        if (edit_point_count == 3)
                                        {
                                            SobekCM_Database.Edit_Single_IP(singleIpId, thisRange.RangeID, thisIpStart, thisIpEnd, thisIpNote, Tracer);
                                        }
                                    }
                                }

                                // Be ready to look at the next pre-existing IP range
                                single_ip_index++;
                            }
                            else
                            {
                                // Just add this as a new single ip address
                                if (thisIpStart.Length > 0)
                                {
                                    int add_point_count = thisIpStart.Count(thisChar => thisChar == '.');

                                    if (add_point_count == 3)
                                    {
                                        SobekCM_Database.Edit_Single_IP(-1, thisRange.RangeID, thisIpStart, thisIpEnd, thisIpNote, Tracer);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // Some error caught while handling postback
                }

                // Repopulate the restriction table
                DataTable ipRestrictionTbl = SobekCM_Database.Get_IP_Restriction_Ranges(Tracer);
                if (ipRestrictionTbl != null)
                {
                    IP_Restrictions.Populate_IP_Ranges(ipRestrictionTbl);
                }

                // Forward back to the main form
                currentMode.My_Sobek_SubMode = String.Empty;
                HttpContext.Current.Response.Redirect(currentMode.Redirect_URL());
            }
        }