public ActionResult Regler()
 {
     Initiate();
     using (var db = new NRDbContext())
     {
         Content con = db.Content.Find(new Guid("00A8EAD6-0A74-E411-BF1A-C485084553DE"));
         Basedata.Content = con == null ? new Content() : con;
     }
     //00A8EAD6-0A74-E411-BF1A-C485084553DE
     return View(Basedata);
 }
        public void Initiate()
        {
            using (var db = new NRDbContext())
            {
                IdnMapping idn = new IdnMapping();
                Guid associationId = (Guid)this.RouteData.Values["associationId"];

                Association association = db.Associations.Include(l => l.PageAbout).Include(l => l.PageLink).Include(l => l.PagePress).Include(l => l.PageSponsor).Include(l => l.Sponsors).Where(a => a.AssociationID == associationId).FirstOrDefault(); //Find(associationId);
                //db.Entry(association).Collection("PageAbout").Load();
                if (association == null) throw new NullReferenceException("Association");

                Basedata.AssociationID = association.AssociationID;

                GetBoard();

                Basedata.SEO = this.RouteData.Values["SEO"] == null ? false : (bool)this.RouteData.Values["SEO"];
               
                Basedata.NetworkID = association.NetworkID;
                Basedata.AssociationName = string.Format(General.BrandTitleAssociation,association.Name);
                Basedata.Host = string.Format("http://{0}.natteravnene.dk", association.Name.ValidDKDomainName());
                Basedata.MainUrl = Request.Url.Host == idn.GetAscii(string.Format("{0}.natteravnene.dk", association.Name.ValidDKDomainName()));
                Basedata.urlCanonical = urlCanonical(association.Name);
                Basedata.Established = (DateTime)association.Established;
                
                Basedata.CVRNR = association.CVRNR;
                if (Basedata.Chairmann != null)
                {
                    Basedata.AssociationEmail = string.IsNullOrWhiteSpace(association.AssociationEmail) ? Basedata.Chairmann.Email : association.AssociationEmail;
                    Basedata.ContactPhone = string.IsNullOrWhiteSpace(association.ContactPhone) ? Basedata.Chairmann.Mobile : association.ContactPhone;
                }
                else
                {
                    Basedata.AssociationEmail = association.AssociationEmail;
                    Basedata.ContactPhone = association.ContactPhone;
                }

               


                
                Basedata.PageAbout = tagReplace(association.PageAbout);
                Basedata.UseLinksPage = association.UseLinksPage;
                Basedata.PageLink = tagReplace(association.PageLink);
                Basedata.UsePressPage = association.UsePressPage;
                Basedata.PagePress = tagReplace(association.PagePress);
                Basedata.PageSponsor = tagReplace(association.PageSponsor);
                Basedata.Sponsors = association.Sponsors.Where(S => S.Finish == null || (DateTime)S.Finish > DateTime.Now).OrderBy(S => S.Sequence).ToList();
                Basedata.UseSponsorPage = association.UseSponsorPage & Basedata.Sponsors.Any();

               

                string LogoDirSetting = ConfigurationManager.AppSettings["Logos"];

                if (string.IsNullOrWhiteSpace(LogoDirSetting)) { throw new ArgumentNullException(); }

                ViewBag.LocalLogoPath = Path.Combine(LogoDirSetting, Basedata.AssociationID + ".jpg");

                if (!System.IO.File.Exists(Server.MapPath(ViewBag.LocalLogoPath)))
                {
                    ViewBag.LocalLogoPath = Path.Combine(LogoDirSetting, "Natteravnene_logo.jpg"); ;
                }

                ViewBag.UseSponsorerPage = Basedata.UseSponsorPage;
                ViewBag.UseLinksPage = Basedata.UseLinksPage;
                ViewBag.UsePressPage = Basedata.UsePressPage;
                ViewBag.Canonical = Basedata.urlCanonical;
                ViewBag.Association = Basedata.AssociationName;
                ViewBag.Host = Basedata.Host;
                ViewBag.AssociationContactPhone = Basedata.ContactPhone;
                ViewBag.AssociationContactEmail = Basedata.AssociationEmail;
                ViewBag.CVR = association.CVRNR;
                ViewBag.AssociationZIP = association.Zip;
                ViewBag.AssociationCity = association.City;
                ViewBag.AssociationCountry = (int)association.Country == 0 ? null : association.Country.DisplayName();
                ViewBag.Founded = association.Established == null ? null : ((DateTime)association.Established).ToString("yyyy-MM-dd");
            }

        }
        public void GetBoard()
        {
            using (var db = new NRDbContext())
            {
                Basedata.Chairmann = db.Memberships
                .Where(M => M.AssociationID == Basedata.AssociationID & M.BoardFunction == BoardFunctionType.Chairman)
                .Select(M => M.Person).FirstOrDefault();

                Basedata.Accountant = db.Memberships
                .Where(M => M.AssociationID == Basedata.AssociationID & M.BoardFunction == BoardFunctionType.Accountant)
                .Select(M => M.Person).FirstOrDefault();

                Basedata.BoardMembers = db.Memberships
                .Where(M => M.AssociationID == Basedata.AssociationID & M.BoardFunction == BoardFunctionType.BoardMember)
                .Select(M => M.Person)
                .OrderBy(m => String.Concat(m.FirstName, " ", m.FamilyName)).ToList();
            }

        }
 public void GetNews(Guid ID)
 {
     using (var db = new NRDbContext())
     {
         Basedata.theNews = db.News.Find(ID);
     }
 }
 public void GetEvent(Guid ID)
 {
     using (var db = new NRDbContext())
     {
         Basedata.Event =  db.Events.Find(ID);
     }
 }
        public void AllNewsActivity()
        {
            using (var db = new NRDbContext())
            {

                Basedata.News = db.News
                    .Where(e => (e.Distribution == LevelType.National | (e.Distribution == LevelType.Network & e.DistributionLink == Basedata.NetworkID) |
                    (e.Distribution == LevelType.Local & e.DistributionLink == Basedata.AssociationID)) & (e.Depublish == null | e.Depublish > DateTime.Now) & (e.Publish < DateTime.Now))
                    .OrderByDescending(e => e.Publish)
                    .ToList();


                Basedata.Events = db.Events
                .Where(e => (e.Distribution == LevelType.National | (e.Distribution == LevelType.Network & e.DistributionLink == Basedata.NetworkID) |
                (e.Distribution == LevelType.Local & e.DistributionLink == Basedata.AssociationID)) & e.Finish > DateTime.Now).OrderBy(e => e.Start)
                .ToList();
            }
        }
        public void Events(int? id)
        {
            using (var db = new NRDbContext())
            {

                Basedata.Events = db.Events
                .Where(e => (e.Distribution == LevelType.National | (e.Distribution == LevelType.Network & e.DistributionLink == Basedata.NetworkID) |
                (e.Distribution == LevelType.Local & e.DistributionLink == Basedata.AssociationID)) & e.Finish > DateTime.Now).OrderBy(e => e.Start)
                .ToList();
            }
        }
        public void News(int? id)
        {
            using (var db = new NRDbContext())
            {

                Basedata.News = db.News
                    .Where(e => (!e.Internal  & (e.Distribution == LevelType.National | (e.Distribution == LevelType.Network & e.DistributionLink == Basedata.NetworkID) |
                    (e.Distribution == LevelType.Local & e.DistributionLink == Basedata.AssociationID))) & (e.Depublish == null | e.Depublish > DateTime.Now) & (e.Publish < DateTime.Now))
                    .OrderByDescending(e => e.Publish)
                    .Skip(10 * id ?? 0).Take(10)
                    .ToList();
            }
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {

            var fullAddress = httpContext.Request.Headers["Host"].Split('.');


            if (fullAddress.Length < 2 | fullAddress.Length > 4) return false;
            if (fullAddress.Length == 4 & fullAddress[0].ToLower() != "www") return false;

            if (!values.ContainsKey("associationId"))
            {
                var associationSubdomain = fullAddress[0];
                if (fullAddress.Length == 4) associationSubdomain = fullAddress[1];

                var subDomainList = new Dictionary<string, Guid>();
                Guid associationId = Guid.Empty;


                if (HttpContext.Current.Cache["subDomainList"] == null)
                {
                    using (var db = new NRDbContext())
                    {
                        var Associations = db.Associations.Where(A => A.Status == NR.Models.AssociationStatus.Active);
                        IdnMapping idn = new IdnMapping();
                        foreach (Association A in Associations)
                        {
                            if (!subDomainList.ContainsKey(A.Name.ValidDKDomainName())) subDomainList.Add(idn.GetAscii(A.Name.ValidDKDomainName()), A.AssociationID);
                            if (!subDomainList.ContainsKey(A.Name.ValidDomainName())) subDomainList.Add(idn.GetAscii(A.Name.ValidDomainName()), A.AssociationID);

                            if (A.URL != null)
                            {
                                string[] Urls = A.URL.Split(',');
                                foreach (string url in Urls)
                                {
                                    if (!string.IsNullOrWhiteSpace(url) && !subDomainList.ContainsKey(url.ValidDKDomainName())) subDomainList.Add(idn.GetAscii(url.ValidDKDomainName()), A.AssociationID);
                                }
                            }
                        }
                    }
                    subDomainList.Add("lokal", new Guid("9fdf690d-5b04-e511-8272-005056aa2abc"));
                    subDomainList.Add("lokaltest", new Guid("2737ed51-d5d6-e411-826d-005056aa2abc"));
                    HttpContext.Current.Cache.Insert("subDomainList", subDomainList, null, DateTime.Now.AddMinutes(30), System.Web.Caching.Cache.NoSlidingExpiration);
                }
                else
                {
                    subDomainList = (Dictionary<string, Guid>)HttpContext.Current.Cache["subDomainList"];
                }


                if (subDomainList.ContainsKey(associationSubdomain.ToLower()))
                {
                    associationId = subDomainList[associationSubdomain.ToLower()];
                }
                else
                {
                    return false;
                }

                if (httpContext.Request.Headers["Host"].Contains("natteravnene.dk")) values.Add("SEO", true);

                values.Add("associationId", associationId);
            }

            return true;
        }
        public ActionResult BlivNatteravn(TrialFormModel result)
        {
            Initiate();
            if (ModelState.IsValid)
            {
                Lead lead = new Lead();

                lead.FirstName = result.FirstName;
                lead.FamilyName = result.LastName;
                lead.Zip = result.Zip;
                lead.Email = result.Email;
                lead.Phone = result.Phone;
                lead.AssociationID = Basedata.AssociationID;
                lead.Created = DateTime.Now;
                lead.Lastchanged = DateTime.Now;

                if (lead.Zip.Length != 0 & lead.Zip.Length < 2)
                {
                    lead.Zip += "_";
                }

                WriteLeadLogFile(lead);

                using (var db = new NRDbContext())
                {
                    db.Entry(lead).State = EntityState.Added;

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        LogFile.Write(e, "Save file");
                        return PartialView("_BlivNatteravnError", result);

                    }
                }

                return PartialView("_BlivNatteravnSucces", result);
            }
            
            return PartialView("_BlivNatteravn", result);
        }