public static void InitSession()
        {
            InitAppURLs();
            try
            {
                // 11/22/2005 Paul.  Always initialize the theme and language.
                HttpSessionState Session = HttpContext.Current.Session;
                Session["USER_SETTINGS/THEME"]   = SplendidDefaults.Theme();
                Session["USER_SETTINGS/CULTURE"] = SplendidDefaults.Culture();
                Session["themeURL"] = Sql.ToString(HttpContext.Current.Application["rootURL"]) + "Themes/" + SplendidDefaults.Theme() + "/";
                // 11/19/2005 Paul.  AUTH_USER is the clear indication that NTLM is enabled.
                if (Security.IsWindowsAuthentication())
                {
                    string[] arrUserName  = HttpContext.Current.User.Identity.Name.Split('\\');
                    string   sUSER_DOMAIN = arrUserName[0];
                    string   sUSER_NAME   = arrUserName[1];
                    bool     bIS_ADMIN    = HttpContext.Current.User.IsInRole("BUILTIN\\Administrators") ||
                                            HttpContext.Current.User.IsInRole(sUSER_DOMAIN + "\\SplendidCRM Administrators") ||
                                            HttpContext.Current.User.IsInRole(HttpContext.Current.Server.MachineName + "\\SplendidCRM Administrators") ||
                                            HttpContext.Current.User.IsInRole(sUSER_DOMAIN + "\\Domain Admins");

                    LoginUser(sUSER_NAME, String.Empty, String.Empty, String.Empty, sUSER_DOMAIN, bIS_ADMIN);
                }
                else
                {
                    // 11/22/2005 Paul.  Assume portal user for the unauthenticated screen as that is the least restrictive.
                    Security.PORTAL_ONLY = true;
                    LoadUserPreferences(Guid.Empty, String.Empty, String.Empty);
                }
            }
            catch (Exception ex)
            {
                SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex.Message);
                HttpContext.Current.Response.Write(ex.Message);
            }
        }
        public static void LoadUserPreferences(Guid gID, string sTheme, string sCulture)
        {
            HttpApplicationState Application = HttpContext.Current.Application;
            HttpSessionState     Session     = HttpContext.Current.Session;
            string sApplicationPath          = Sql.ToString(Application["rootURL"]);

            DbProviderFactory dbf = DbProviderFactories.GetFactory();

            using (IDbConnection con = dbf.CreateConnection())
            {
                string sSQL;
                sSQL = "select *           " + ControlChars.CrLf
                       + "  from vwUSERS_Edit" + ControlChars.CrLf
                       + " where ID = @ID    " + ControlChars.CrLf;
                using (IDbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sSQL;
                    Sql.AddParameter(cmd, "@ID", gID);
                    con.Open();
                    using (IDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (rdr.Read())
                        {
                            string sUSER_PREFERENCES = Sql.ToString(rdr["USER_PREFERENCES"]);
                            if (!Sql.IsEmptyString(sUSER_PREFERENCES))
                            {
                                XmlDocument xml = InitUserPreferences(sUSER_PREFERENCES);
                                Session["USER_PREFERENCES"] = xml.OuterXml;
                                // 11/19/2005 Paul.  Not sure why the login screen has the language, but it would seem to allow overriding the default.
                                if (Sql.IsEmptyString(sCulture))
                                {
                                    sCulture = XmlUtil.SelectSingleNode(xml, "culture").Replace("_", "-");
                                }
                                // 11/22/2005 Paul.  The theme can be overridden as well.
                                if (Sql.IsEmptyString(sTheme))
                                {
                                    sTheme = XmlUtil.SelectSingleNode(xml, "theme").Replace("_", "-");
                                }
                                Session["USER_SETTINGS/CULTURE"] = sCulture;
                                Session["USER_SETTINGS/THEME"]   = sTheme;
                                Session["themeURL"] = sApplicationPath + "Themes/" + sTheme + "/";
                                Session["USER_SETTINGS/DATEFORMAT"] = XmlUtil.SelectSingleNode(xml, "dateformat");
                                Session["USER_SETTINGS/TIMEFORMAT"] = XmlUtil.SelectSingleNode(xml, "timeformat");
                                // 01/21/2006 Paul.  It is useful to have quick access to email address.
                                Session["USER_SETTINGS/MAIL_FROMNAME"]    = XmlUtil.SelectSingleNode(xml, "mail_fromname");
                                Session["USER_SETTINGS/MAIL_FROMADDRESS"] = XmlUtil.SelectSingleNode(xml, "mail_fromaddress");
                                // 05/09/2006 Paul.  Initialize the numeric separators.
                                Session["USER_SETTINGS/GROUP_SEPARATOR"]   = XmlUtil.SelectSingleNode(xml, "num_grp_sep");
                                Session["USER_SETTINGS/DECIMAL_SEPARATOR"] = XmlUtil.SelectSingleNode(xml, "dec_sep");
                                try
                                {
                                    Session["USER_SETTINGS/TIMEZONE"] = Sql.ToGuid(XmlUtil.SelectSingleNode(xml, "timezone")).ToString();
                                }
                                catch
                                {
                                    SplendidError.SystemError(new StackTrace(true).GetFrame(0), "Invalid USER_SETTINGS/TIMEZONE: " + XmlUtil.SelectSingleNode(xml, "timezone"));
                                }
                                try
                                {
                                    Session["USER_SETTINGS/CURRENCY"] = XmlUtil.SelectSingleNode(xml, "currency_id");
                                }
                                catch
                                {
                                    SplendidError.SystemError(new StackTrace(true).GetFrame(0), "Invalid USER_SETTINGS/CURRENCY: " + XmlUtil.SelectSingleNode(xml, "currency_id"));
                                }

                                DataView vwCurrencies = new DataView(SplendidCache.Currencies());
                                vwCurrencies.RowFilter = "ID = '" + XmlUtil.SelectSingleNode(xml, "currency_id") + "'";
                                if (vwCurrencies.Count > 0)
                                {
                                    Session["USER_SETTINGS/CURRENCY_SYMBOL"] = Sql.ToString(vwCurrencies[0]["SYMBOL"]);
                                }
                            }
                        }
                    }
                }
                // 11/21/2005 Paul.  New users may not have any settings, so we need to initialize the defaults.
                // It is best to do it here rather than wrap the variables in a function that would return the default if null.
                sCulture = Sql.ToString(Session["USER_SETTINGS/CULTURE"]);
                sTheme   = Sql.ToString(Session["USER_SETTINGS/THEME"]);
                string sDateFormat = Sql.ToString(Session["USER_SETTINGS/DATEFORMAT"]);
                string sTimeFormat = Sql.ToString(Session["USER_SETTINGS/TIMEFORMAT"]);
                string sTimeZone   = Sql.ToString(Session["USER_SETTINGS/TIMEZONE"]);
                string sCurrencyID = Sql.ToString(Session["USER_SETTINGS/CURRENCY"]);
                if (Sql.IsEmptyString(sCulture))
                {
                    Session["USER_SETTINGS/CULTURE"] = SplendidDefaults.Culture();
                }
                if (Sql.IsEmptyString(sTheme))
                {
                    sTheme = SplendidDefaults.Theme();
                    Session["USER_SETTINGS/THEME"] = sTheme;
                    Session["themeURL"]            = sApplicationPath + "Themes/" + sTheme + "/";
                }
                if (Sql.IsEmptyString(sDateFormat))
                {
                    Session["USER_SETTINGS/DATEFORMAT"] = SplendidDefaults.DateFormat();
                }
                // 11/12/2005 Paul.  "m" is not valid for .NET month formatting.  Must use MM.
                // 11/12/2005 Paul.  Require 4 digit year.  Otherwise default date in Pipeline of 12/31/2100 would get converted to 12/31/00.
                if (SplendidDefaults.IsValidDateFormat(sDateFormat))
                {
                    Session["USER_SETTINGS/DATEFORMAT"] = SplendidDefaults.DateFormat(sDateFormat);
                }
                if (Sql.IsEmptyString(sTimeFormat))
                {
                    Session["USER_SETTINGS/TIMEFORMAT"] = SplendidDefaults.TimeFormat();
                }
                if (Sql.IsEmptyString(sCurrencyID))
                {
                    Session["USER_SETTINGS/CURRENCY"] = SplendidDefaults.CurrencyID();
                }
                if (Sql.IsEmptyString(sTimeZone))
                {
                    Session["USER_SETTINGS/TIMEZONE"] = SplendidDefaults.TimeZone();
                }

                // 05/09/2006 Paul.  Use defaults when necessary.
                string sGROUP_SEPARATOR   = Sql.ToString(Session["USER_SETTINGS/GROUP_SEPARATOR"]);
                string sDECIMAL_SEPARATOR = Sql.ToString(Session["USER_SETTINGS/DECIMAL_SEPARATOR"]);
                if (Sql.IsEmptyString(sGROUP_SEPARATOR))
                {
                    Session["USER_SETTINGS/GROUP_SEPARATOR"] = SplendidDefaults.GroupSeparator();
                }
                if (Sql.IsEmptyString(sDECIMAL_SEPARATOR))
                {
                    Session["USER_SETTINGS/DECIMAL_SEPARATOR"] = SplendidDefaults.DecimalSeparator();
                }
            }
        }
        public static XmlDocument InitUserPreferences(string sUSER_PREFERENCES)
        {
            XmlDocument xml = null;

            try
            {
                xml = new XmlDocument();
                if (!sUSER_PREFERENCES.StartsWith("<?xml "))
                {
                    sUSER_PREFERENCES = XmlUtil.ConvertFromPHP(sUSER_PREFERENCES);
                }

                xml.LoadXml(sUSER_PREFERENCES);

                HttpApplicationState Application = HttpContext.Current.Application;
                string sCulture    = L10N.NormalizeCulture(XmlUtil.SelectSingleNode(xml, "culture"));
                string sTheme      = XmlUtil.SelectSingleNode(xml, "theme");
                string sDateFormat = XmlUtil.SelectSingleNode(xml, "dateformat");
                string sTimeFormat = XmlUtil.SelectSingleNode(xml, "timeformat");
                string sTimeZone   = XmlUtil.SelectSingleNode(xml, "timezone");
                string sCurrencyID = XmlUtil.SelectSingleNode(xml, "currency_id");
                if (Sql.IsEmptyString(sCulture))
                {
                    XmlUtil.SetSingleNode(xml, "culture", SplendidDefaults.Culture());
                }
                if (Sql.IsEmptyString(sTheme))
                {
                    XmlUtil.SetSingleNode(xml, "theme", SplendidDefaults.Theme());
                }
                if (Sql.IsEmptyString(sDateFormat))
                {
                    XmlUtil.SetSingleNode(xml, "dateformat", SplendidDefaults.DateFormat());
                }
                // 11/12/2005 Paul.  "m" is not valid for .NET month formatting.  Must use MM.
                // 11/12/2005 Paul.  Require 4 digit year.  Otherwise default date in Pipeline of 12/31/2100 would get converted to 12/31/00.
                if (SplendidDefaults.IsValidDateFormat(sDateFormat))
                {
                    XmlUtil.SetSingleNode(xml, "dateformat", SplendidDefaults.DateFormat(sDateFormat));
                }
                if (Sql.IsEmptyString(sTimeFormat))
                {
                    XmlUtil.SetSingleNode(xml, "timeformat", SplendidDefaults.TimeFormat());
                }
                if (Sql.IsEmptyString(sCurrencyID))
                {
                    XmlUtil.SetSingleNode(xml, "currency_id", SplendidDefaults.CurrencyID());
                }
                // 09/01/2006 Paul.  Only use timez if provided.  Otherwise we will default to GMT.
                if (Sql.IsEmptyString(sTimeZone) && !Sql.IsEmptyString(XmlUtil.SelectSingleNode(xml, "timez")))
                {
                    int nTimez = Sql.ToInteger(XmlUtil.SelectSingleNode(xml, "timez"));
                    sTimeZone = SplendidDefaults.TimeZone(nTimez);
                    XmlUtil.SetSingleNode(xml, "timezone", sTimeZone);
                }
                // 09/01/2006 Paul.  Default TimeZone was not getting set properly.
                if (Sql.IsEmptyString(sTimeZone))
                {
                    sTimeZone = SplendidDefaults.TimeZone();
                    XmlUtil.SetSingleNode(xml, "timezone", sTimeZone);
                }
            }
            catch (Exception ex)
            {
                SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex.Message);
            }
            return(xml);
        }