public static bool EditLeagueContact(Guid leagueId, ContactDisplayBasic contact)
        {
            try
            {
                var dc = new ManagementContext();
                var con = dc.LeagueContacts.Where(x => x.Contact.ContactId == contact.ContactId).FirstOrDefault();

                con.Contact.CompanyName = contact.CompanyName;
                con.Contact.Firstname = contact.FirstName;
                con.Contact.Lastname = contact.LastName;
                con.Contact.Link = contact.Link;

                int countryId = Convert.ToInt32(contact.CountryId);
                ContactCard.ContactCardFactory.UpdateAddressToContact(contact.Address1, contact.Address2, contact.CityRaw, contact.StateRaw, contact.Zip, AddressTypeEnum.None, con.Contact.ContactCard, dc.Countries.Where(x => x.CountryId == countryId).FirstOrDefault());
                ContactCard.ContactCardFactory.UpdatePhoneNumberToCard(contact.PhoneNumber, con.Contact.ContactCard, String.Empty);
                ContactCard.ContactCardFactory.UpdateEmailToContactCard(contact.Email, con.Contact.ContactCard);
                
                con.Notes = contact.Notes;
                con.ContactTypeEnum = (byte)((ContactTypeForOrganizationEnum)Enum.Parse(typeof(ContactTypeForOrganizationEnum), contact.ContactTypeSelected));
                con.League = con.League;
                con.Contact = con.Contact;
                int c = dc.SaveChanges();
                return c > 0;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
 public ActionResult AddContact(string type)
 {
     try
     {
         ContactDisplayBasic con = new ContactDisplayBasic();
         con.ContactType = (ContactTypeEnum)Enum.Parse(typeof(ContactTypeEnum), type);
         var countries = RDN.Library.Classes.Location.LocationFactory.GetCountries();
         ViewBag.Countries = new SelectList(countries, "CountryId", "Name");
         ViewData["ContactTypeForOrg"] = con.ContactTypeForOrg.ToSelectList();
         NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(Request.Url.Query);
         string u = nameValueCollection["u"];
         if (u == SiteMessagesEnum.s.ToString())
         {
             SiteMessage message = new SiteMessage();
             message.MessageType = SiteMessageType.Success;
             message.Message = "Successfully Added New Contact.";
             this.AddMessage(message);
         }
         return View(con);
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
     return Redirect("~/?u=" + SiteMessagesEnum.sww);
 }
        public static bool AddLeagueContact(Guid leagieId, ContactDisplayBasic contact)
        {
            try
            {
                var dc = new ManagementContext();
                DataModels.Contacts.Contact con = new DataModels.Contacts.Contact();
                con.CompanyName = contact.CompanyName;
                con.Firstname = contact.FirstName;
                con.Lastname = contact.LastName;
                con.Link = contact.Link;
                con.ContactCard = new DataModels.ContactCard.ContactCard();
                if (!String.IsNullOrEmpty(contact.Address1) || !String.IsNullOrEmpty(contact.Address2) || !String.IsNullOrEmpty(contact.CityRaw) || !String.IsNullOrEmpty(contact.StateRaw))
                {
                    int countryId = Convert.ToInt32(contact.CountryId);
                    ContactCard.ContactCardFactory.AddAddressToContact(contact.Address1, contact.Address2, contact.CityRaw, contact.StateRaw, contact.Zip, AddressTypeEnum.None, con.ContactCard, dc.Countries.Where(x => x.CountryId == countryId).FirstOrDefault());
                }
                if (!String.IsNullOrEmpty(contact.PhoneNumber))
                {
                    ContactCard.ContactCardFactory.AddPhoneNumberToCard(contact.PhoneNumber, con.ContactCard);
                }
                if (!String.IsNullOrEmpty(contact.Email))
                {
                    ContactCard.ContactCardFactory.AddEmailToContactCard(contact.Email, con.ContactCard);
                }
                dc.Contacts.Add(con);

                var league = dc.Leagues.Where(x => x.LeagueId == leagieId).FirstOrDefault();
                LeagueContacts lc = new LeagueContacts();
                lc.Notes = contact.Notes;
                lc.ContactTypeEnum = (byte)contact.ContactTypeForOrg;
                lc.Contact = con;
                lc.League = league;
                league.Contacts.Add(lc);

                int c = dc.SaveChanges();
                return c > 0;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
        public static RDN.Portable.Classes.League.Classes.League DisplayLeague(DataModels.League.League league, bool isPublic = false, bool isRemovedFromLeague = false)
        {
            try
            {
                RDN.Portable.Classes.League.Classes.League leagueTemp = new Portable.Classes.League.Classes.League();
                leagueTemp.LeagueId = league.LeagueId;
                leagueTemp.Name = league.Name;
                //leagues don't put the http in their profile.
                if (!String.IsNullOrEmpty(league.WebSite) && !league.WebSite.Contains("http://"))
                    leagueTemp.Website = "http://" + league.WebSite;
                else
                    leagueTemp.Website = league.WebSite;
                if (!String.IsNullOrEmpty(league.Twitter) && !league.Twitter.Contains("http://"))
                    leagueTemp.Twitter = "http://" + league.Twitter;
                else
                    leagueTemp.Twitter = league.Twitter;
                if (!String.IsNullOrEmpty(league.Instagram) && !league.Instagram.Contains("http://"))
                    leagueTemp.Instagram = "http://" + league.Instagram;
                else
                    leagueTemp.Instagram = league.Instagram;
                if (!String.IsNullOrEmpty(league.Facebook) && !league.Facebook.Contains("http://"))
                    leagueTemp.Facebook = "http://" + league.Facebook;
                else
                    leagueTemp.Facebook = league.Facebook;
                leagueTemp.RuleSetsPlayedEnum = (RuleSetsUsedEnum)league.RuleSetsPlayedEnum;
                leagueTemp.ShopUrl = StoreGateway.GetShopUrl(league.LeagueId);

                leagueTemp.SubscriptionPeriodEnds = league.SubscriptionPeriodEnds.GetValueOrDefault();
                leagueTemp.Groups = LeagueGroupFactory.DisplayGroups(league.Groups.Where(x => x.IsGroupRemoved == false).ToList());
                if (league.Founded.HasValue)
                    leagueTemp.Founded = league.Founded.Value;
                //we use the contact card Id as the join code for "making contact with the league" 
                //instead of creating a new id 
                //and when I don't want to release to use the public LeagueId as the join key.
                leagueTemp.JoinCode = league.LeagueJoinCode;

                if (league.ContactCard != null)
                {
                    if (league.ContactCard.Addresses.FirstOrDefault() != null)
                    {
                        leagueTemp.TimeZone = league.ContactCard.Addresses.FirstOrDefault().TimeZone;
                        leagueTemp.Address = league.ContactCard.Addresses.FirstOrDefault().Address1;
                        leagueTemp.City = league.ContactCard.Addresses.FirstOrDefault().CityRaw;
                        leagueTemp.State = league.ContactCard.Addresses.FirstOrDefault().StateRaw;
                        leagueTemp.ZipCode = league.ContactCard.Addresses.FirstOrDefault().Zip;
                        if (league.ContactCard.Addresses.FirstOrDefault().Country != null)
                        {
                            leagueTemp.Country = league.ContactCard.Addresses.FirstOrDefault().Country.Name;
                            leagueTemp.CountryId = league.ContactCard.Addresses.FirstOrDefault().Country.CountryId;
                        }
                    }
                    if (league.ContactCard.Emails.FirstOrDefault() != null)
                        leagueTemp.Email = league.ContactCard.Emails.Where(x => x.IsDefault == true).FirstOrDefault().EmailAddress;
                    //int numberType = Convert.ToInt32(CommunicationTypeEnum.PhoneNumber);
                    var phone = league.ContactCard.Communications.Where(x => x.CommunicationTypeEnum == (byte)CommunicationTypeEnum.PhoneNumber).FirstOrDefault();
                    if (phone != null)
                        leagueTemp.PhoneNumber = phone.Data;

                }
                if (league.Logo != null)
                    leagueTemp.Logo = new PhotoItem(league.Logo.ImageUrl, league.Logo.IsPrimaryPhoto, league.Logo.AlternativeText);

                if (league.InternalWelcomePicture != null)
                    leagueTemp.InternalWelcomeImage = new PhotoItem(league.InternalWelcomePicture.ImageUrl, league.InternalWelcomePicture.IsPrimaryPhoto, league.InternalWelcomePicture.AlternativeText);

                leagueTemp.InternalWelcomeMessage = league.InternalWelcomeMessage;

                //sets culture as united states english
                if (league.CultureLCID == 0)
                    leagueTemp.CultureSelected = 1033;
                else
                    leagueTemp.CultureSelected = league.CultureLCID;

                if (league.LastModified > new DateTime(2013, 11, 23) || league.Created > new DateTime(2013, 11, 23))
                {
                    leagueTemp.InternalWelcomeMessageHtml = leagueTemp.InternalWelcomeMessage;
                }
                else if (league.Created < new DateTime(2013, 11, 23))
                {
                    RDN.Library.Util.MarkdownSharp.Markdown markdown = new RDN.Library.Util.MarkdownSharp.Markdown();
                    markdown.AutoHyperlink = true;
                    markdown.AutoNewLines = true;
                    markdown.LinkEmails = true;
                    if (!String.IsNullOrEmpty(leagueTemp.InternalWelcomeMessage))
                    {
                        leagueTemp.InternalWelcomeMessageHtml = markdown.Transform(HtmlSanitize.FilterHtmlToWhitelist(leagueTemp.InternalWelcomeMessage));
                        leagueTemp.InternalWelcomeMessageHtml = leagueTemp.InternalWelcomeMessageHtml.Replace("</p>", "</p><br/>");
                    }
                }

                List<LeagueMember> members = new List<LeagueMember>();
                if (isPublic)
                    members = league.Members.Where(x => x.HasLeftLeague == isRemovedFromLeague && x.Member.IsProfileRemovedFromPublic == false).ToList();
                else
                    members = league.Members.Where(x => x.HasLeftLeague == isRemovedFromLeague).ToList();

                leagueTemp.LeagueMembers.AddRange(MemberDisplayFactory.IterateMembersForDisplay(members).OrderBy(x => x.DerbyName));

                try
                {
                    foreach (var color in league.Colors)
                    {
                        if (color != null)
                        {
                            var c = Color.FromArgb(color.Color.ColorIdCSharp);
                            var hex = ColorTranslator.ToHtml(c);
                            ColorDisplay d = new ColorDisplay();
                            d.ColorId = color.ColorId;
                            d.CSharpColor = color.Color.ColorIdCSharp;
                            d.HexColor = hex;
                            d.NameOfColor = color.Color.ColorName;
                            leagueTemp.ColorTempSelected += d.HexColor + ";";
                            leagueTemp.ColorsSelected += d.HexColor + ";";
                            leagueTemp.Colors.Add(d);
                        }

                    }
                }
                catch (Exception exception)
                {
                    ErrorDatabaseManager.AddException(exception, exception.GetType());
                }


                foreach (var team in league.Teams)
                {
                    TeamDisplay teamTemp = new TeamDisplay();
                    teamTemp.TeamName = team.Name;
                    teamTemp.TeamId = team.TeamId;
                    teamTemp.Description = team.Description;
                    leagueTemp.Teams.Add(teamTemp);
                }
                foreach (var con in league.Contacts)
                {
                    try
                    {
                        ContactDisplayBasic c = new ContactDisplayBasic();
                        c.ContactId = con.Contact.ContactId;
                        c.FirstName = con.Contact.Firstname;
                        c.LastName = con.Contact.Lastname;
                        c.ContactTypeForOrg = (ContactTypeForOrganizationEnum)con.ContactTypeEnum;
                        c.ContactTypeSelected = c.ContactTypeForOrg.ToString();
                        if (con.Contact.ContactCard.Addresses.FirstOrDefault() != null)
                        {
                            c.ContactCard = new Portable.Classes.ContactCard.ContactCard();
                            Portable.Classes.ContactCard.Address add = new Portable.Classes.ContactCard.Address();

                            add.Address1 = con.Contact.ContactCard.Addresses.FirstOrDefault().Address1;
                            add.Address2 = con.Contact.ContactCard.Addresses.FirstOrDefault().Address2;
                            add.CityRaw = con.Contact.ContactCard.Addresses.FirstOrDefault().CityRaw;
                            add.Zip = con.Contact.ContactCard.Addresses.FirstOrDefault().Zip;
                            add.StateRaw = con.Contact.ContactCard.Addresses.FirstOrDefault().StateRaw;
                            if (con.Contact.ContactCard.Addresses.FirstOrDefault().Country != null)
                                add.Country = con.Contact.ContactCard.Addresses.FirstOrDefault().Country.Name;
                            c.Address1 = con.Contact.ContactCard.Addresses.FirstOrDefault().Address1;
                            c.Address2 = con.Contact.ContactCard.Addresses.FirstOrDefault().Address2;
                            c.CityRaw = con.Contact.ContactCard.Addresses.FirstOrDefault().CityRaw;
                            c.Zip = con.Contact.ContactCard.Addresses.FirstOrDefault().Zip;
                            c.StateRaw = con.Contact.ContactCard.Addresses.FirstOrDefault().StateRaw;
                            if (con.Contact.ContactCard.Addresses.FirstOrDefault().Country != null)
                            {
                                c.CountryId = con.Contact.ContactCard.Addresses.FirstOrDefault().Country.CountryId;
                                c.CountryName = con.Contact.ContactCard.Addresses.FirstOrDefault().Country.Name;
                            }
                            c.ContactCard.Addresses.Add(add);
                        }
                        if (con.Contact.ContactCard.Emails.FirstOrDefault() != null)
                            c.Email = con.Contact.ContactCard.Emails.FirstOrDefault().EmailAddress;

                        var phone = con.Contact.ContactCard.Communications.OrderByDescending(x => x.Created).FirstOrDefault();
                        if (phone != null)
                            c.PhoneNumber = phone.Data;
                        c.CompanyName = con.Contact.CompanyName;
                        c.Notes = con.Notes;
                        c.Link = con.Contact.Link;
                        leagueTemp.Contacts.Add(c);
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }


                foreach (var federation in league.Federations)
                {
                    FederationDisplay fed = new FederationDisplay();
                    fed.FederationId = federation.Federation.FederationId;
                    fed.FederationName = federation.Federation.Name;
                    fed.MembershipId = federation.InternalLeagueIdForFederation;

                    leagueTemp.Federations.Add(fed);
                }

                return leagueTemp;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return null;
        }
        public ActionResult AddContact(ContactDisplayBasic contact)
        {
            try
            {
                bool createAnother = false;
                if (Request.Form["createAnother"] != null)
                    createAnother = true;

                bool isSuccess = false;

                if (contact.ContactType == ContactTypeEnum.league)
                {
                    var memId = RDN.Library.Classes.Account.User.GetMemberId();
                    var league = MemberCache.GetLeagueOfMember(memId);
                    isSuccess = RDN.Library.Classes.Contacts.Contact.AddLeagueContact(league.LeagueId, contact);
                    MemberCache.ClearLeagueMembersCache(league.LeagueId);
                    MemberCache.ClearLeagueMembersApiCache(league.LeagueId);
                }

                if (createAnother && isSuccess)
                    return Redirect(Url.Content("~/contact/add/" + contact.ContactType + "?u=" + SiteMessagesEnum.s));
                else if (isSuccess)
                    return Redirect(Url.Content("~/" + contact.ContactType + "/contacts?u=" + SiteMessagesEnum.s));
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return Redirect("~/?u=" + SiteMessagesEnum.sww);
        }
        public ActionResult EditContact(ContactDisplayBasic contact)
        {
            try
            {

                bool isSuccess = false;

                if (contact.ContactType == ContactTypeEnum.league)
                {
                    var memId = RDN.Library.Classes.Account.User.GetMemberId();
                    var league = MemberCache.GetLeagueOfMember(memId);
                    isSuccess = RDN.Library.Classes.Contacts.Contact.EditLeagueContact(league.LeagueId, contact);
                    MemberCache.ClearLeagueMembersCache(league.LeagueId);
                    MemberCache.ClearLeagueMembersApiCache(league.LeagueId);
                }

                if (isSuccess)
                    return Redirect(Url.Content("~/" + contact.ContactType + "/contacts?u=" + SiteMessagesEnum.su));
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return Redirect("~/?u=" + SiteMessagesEnum.sww);
        }