public string RegisterClient(ClientRegistrationRequest request)
        {
            try
            {
                using (var scope = new TransactionScope())
                using (var db = new LomsContext())
                {
                    var email = request.Email.Trim().ToLower();

                    //check id user with such email existed already
                    var count = (from u in db.AssociationUsers
                                 where u.AssociationId == CurrentAssociationId && u.Email == email
                                 select u).Count();

                    if (count != 0)
                        return "User with such email is already registered!";

                    //create membership
                    MembershipCreateStatus status;
                    var userMembership = Membership.CreateUser(email, request.Password, email, "Am i client?", "Yes", false, out status);
                    switch (status)
                    {
                        case MembershipCreateStatus.Success:
                            break;
                        //case MembershipCreateStatus.InvalidPassword:
                        //    throw new ApplicationException("Invalid password.");
                        default:
                            throw new ApplicationException("Cannot create user account.", new ApplicationException(status.ToString()));
                    }
                    Guid aspNetUserId = (Guid)userMembership.ProviderUserKey;


                    //add role
                    if (!Roles.RoleExists(RoleName.Client))
                        Roles.CreateRole(RoleName.Client);
                    Roles.AddUserToRole(email, RoleName.Client);

                    //create user 
                    AssociationUser user = new AssociationUser();

                    user.AssociationId = CurrentAssociationId;
                    user.CountryId = request.CountryId;
                    user.Email = email;

                    //name
                    user.Prefix = NamePrefix.All.FirstOrDefault(p => p.Id == request.NamePrefixId);
                    user.FirstName = request.FirstName.ToUpper();
                    user.LastName = request.LastName.ToUpper();
                    //phone
                    user.OfficePhone = request.OfficePhone;
                    user.MobilePhone = request.MobilePhone;
                    user.HomePhone = request.HomePhone;
                    user.DefaultPhoneType = request.DefaultPhoneType;

                    user.IsTravelAgency = request.IsTravelAgency;
                    user.PositionTitle = request.Position;

                    if (user.IsTravelAgency)
                        user.IataNumber = request.IataNumber.ToUpper();

                    user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow;

                    user.AspNetUserId = aspNetUserId;

                    db.AssociationUsers.ApplyChanges(user);
                    db.SaveChanges();

                    //home address
                    AssociationUserAddress homeAddress = new AssociationUserAddress();
                    homeAddress.Nickname = user.FullName + " HOME";
                    homeAddress.AssociationUserId = user.Id;
                    homeAddress.Type = AddressType.Home;
                    homeAddress.CountryId = user.CountryId;

                    //work address
                    AssociationUserAddress workAddress = new AssociationUserAddress();
                    workAddress.Nickname = user.FullName + " WORK";
                    workAddress.AssociationUserId = user.Id;
                    workAddress.CountryId = user.CountryId;
                    workAddress.Type = AddressType.Work;

                    workAddress.BusinessName = request.BusinessName.ToUpper();
                    workAddress.BuildingName = request.BuildingName.ToUpper();
                    workAddress.Address1 = request.Address1.ToUpper();
                    workAddress.Address2 = request.Address2.ToUpper();

                    if (request.SuburbId != 0)
                    {
                        workAddress.CountryId = null;
                        workAddress.StateId = null;
                        workAddress.SuburbId = request.SuburbId;
                    }
                    else
                    {
                        if (request.StateId != 0)
                        {
                            workAddress.CountryId = null;
                            workAddress.StateId = request.StateId;
                            workAddress.SuburbId = null;
                        }

                        workAddress.SuburbName = request.SuburbName.ToUpper();
                        workAddress.SuburbCode = request.SuburbCode.ToUpper();
                    }

                    db.AssociationUserAddresses.ApplyChanges(homeAddress);
                    db.AssociationUserAddresses.ApplyChanges(workAddress);
                    db.SaveChanges();

                    AssociationUserActivation activation = new AssociationUserActivation();
                    activation.UserId = user.Id;
                    activation.Guid = Guid.NewGuid();
                    activation.ExpiryTime = DateTime.UtcNow.AddHours(2.0);  //expiry
                    db.AssociationUserActivations.ApplyChanges(activation);
                    db.SaveChanges();

                    var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId);
                    if (emailProvider != null)
                    {
                        var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId);

                        var uri = HttpContext.Current.Request.Url;

                        string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                        string activtionLink = Path.Combine(baseUrl + string.Format("/#Activation/{0}", activation.Guid.ToString("D")));
                        string contactUsLink = Path.Combine(baseUrl + "/#Contact");

                        var emailTemplate = new EmailTemplate("OnlineRegistrationActivation");
                        emailTemplate["UserName"] = user.FullName.ToUpper();
                        emailTemplate["AssociationName"] = association.Name.ToUpper();
                        emailTemplate["ActivationLink"] = activtionLink;
                        emailTemplate["ContactUsLink"] = contactUsLink;

                        var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html);
                        emailProvider.SendMail(user.Email, association.Name + " Account activation", emailTemplate.Txt, null, avBody, true);
                    }

                    scope.Complete();
                }

                return null;
            }
            catch (Exception ex)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(ex.Message);
                if (ex.InnerException != null)
                {
                    builder.AppendLine(ex.InnerException.Message);
                    if (ex.InnerException.InnerException != null)
                        builder.AppendLine(ex.InnerException.InnerException.Message);
                }
                return builder.ToString();
            }
        }
        public string ResetClientPassword(string email)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(email))
                    return "Email cannot be empty!";

                email = email.Trim().ToLower();

                using (var db = new LomsContext())
                {
                    //check id user with such email existed already
                    var existedUser = (from u in db.AssociationUsers
                                       where u.AssociationId == CurrentAssociationId && u.Email == email
                                       select u).SingleOrDefault();

                    if (existedUser == null)
                        return "Not a registered user!";

                    if (!existedUser.HasOnlineAccess)
                        return "Not a with online access!";

                    var pwdReset = db.AssociationUserPasswordResets.SingleOrDefault(r => r.AssociationUserId == existedUser.Id);
                    if (pwdReset == null)
                    {
                        pwdReset = new AssociationUserPasswordReset();
                        pwdReset.AssociationUserId = existedUser.Id;
                    }

                    pwdReset.Guid = Guid.NewGuid();
                    pwdReset.Time = DateTime.UtcNow.AddHours(2.0);
                    db.AssociationUserPasswordResets.ApplyChanges(pwdReset);
                    db.SaveChanges();

                    var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId);
                    if (emailProvider != null)
                    {
                        var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId);

                        var uri = HttpContext.Current.Request.Url;

                        string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                        string activtionLink = Path.Combine(baseUrl + string.Format("/#PasswordReset/{0}", pwdReset.Guid.ToString("D")));
                        string contactUsLink = Path.Combine(baseUrl + "/#Contact");

                        var emailTemplate = new EmailTemplate("PasswordReset");
                        emailTemplate["UserName"] = existedUser.FullName.ToUpper();
                        emailTemplate["ActivationLink"] = activtionLink;
                        emailTemplate["ContactUsLink"] = contactUsLink;

                        var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html);
                        emailProvider.SendMail(existedUser.Email, association.Name + " - Reset you Password", emailTemplate.Txt, null, avBody, true);
                    }

                    return "";
                }
            }
            catch (Exception ex)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(ex.Message);
                if (ex.InnerException != null)
                {
                    builder.AppendLine(ex.InnerException.Message);
                    if (ex.InnerException.InnerException != null)
                        builder.AppendLine(ex.InnerException.InnerException.Message);
                }
                return builder.ToString();
            }
        }