/// <summary>
        /// Get client user from the id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ClientUser GetClientUser(int id)
        {
            var context = new dbDataContext();
            // Set up the query to retrieve client user
            var clientUser = context.tbl_ClientUsers.FirstOrDefault(t => t.ClientUserId == id);
            if (clientUser != null)
            {
                var cUser = new ClientUser
                {
                    ClientId = clientUser.ClientId,
                    ClientUserId = clientUser.ClientUserId,
                    Email = clientUser.Email,
                    Forename = clientUser.Forename,
                    Surname = clientUser.Surname,
                    Password = clientUser.Password,
                    JobTitle = clientUser.JobTitle,
                    Mobile = clientUser.Mobile,
                    Telephone = clientUser.Telephone,
                    IosApp = clientUser.IosApp,
                    IsActive = clientUser.IsActive,
                    CreatedDate = clientUser.CreatedDate,
                    FooterTemplate = GetFooterTemplate(id)
                };

                var docs = Documents.GetDocumentByDocType(new[] { 9 }, clientUser.ClientUserId).ToList();
                if (docs.Count > 0)
                {
                    cUser.ProfileImage = docs[0];
                }
                return cUser;
            }
            return null;
        }
        /// <summary>
        /// Validate client user
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ClientUser ValidateClientUser(string username, string password)
        {
            var context = new dbDataContext();
            // Set up the query to retrieve client user
            var clientUser = context.tbl_ClientUsers.FirstOrDefault(t => t.IsActive && String.Compare(t.Email, username, StringComparison.OrdinalIgnoreCase) == 0 && String.Compare(t.Password, password, StringComparison.OrdinalIgnoreCase) == 0);
            if (clientUser != null)
            {
                var cUser = new ClientUser
                {
                    ClientId = clientUser.ClientId,
                    ClientUserId = clientUser.ClientUserId,
                    Email = clientUser.Email,
                    Forename = clientUser.Forename,
                    Surname = clientUser.Surname,
                    Password = clientUser.Password,
                    Telephone = clientUser.Telephone,
                    JobTitle = clientUser.JobTitle,
                    Mobile = clientUser.Mobile,
                    IosApp = clientUser.IosApp,
                    Published = clientUser.IsActive,
                    CreatedDate = clientUser.CreatedDate
                };

                var docs = Documents.GetDocumentByDocType(new[] { 9 }, clientUser.ClientUserId).ToList();
                if (docs.Count > 0)
                {
                    cUser.ProfileImage = docs[0];
                }
                return cUser;
            }
            return null;
        }
        /// <summary>
        /// This function add/edit client users
        /// </summary>
        /// <param name="clientUser"></param>
        public int AddEditClientUsers(ClientUser clientUser)
        {
            var context = new dbDataContext();
            // Create or retrieve the job
            var cUser = context.tbl_ClientUsers.FirstOrDefault(t => t.ClientUserId == clientUser.ClientUserId) ??
                        new tbl_ClientUser();

            // Assign client user values
            cUser.ClientId = clientUser.ClientId;
            cUser.ClientUserId = clientUser.ClientUserId;
            cUser.Email = clientUser.Email;
            cUser.Forename = clientUser.Forename;
            cUser.Surname = clientUser.Surname;
            if (!string.IsNullOrEmpty(clientUser.Password))
            {
                cUser.Password = clientUser.Password;
            }
            cUser.JobTitle = clientUser.JobTitle;
            cUser.Mobile = clientUser.Mobile;
            cUser.Telephone = clientUser.Telephone;
            cUser.IosApp = clientUser.IosApp;
            cUser.IsActive = clientUser.IsActive;
            var isAdd = false;
            try
            {
                // Add/Update Job
                if (cUser.ClientUserId <= 0)
                {
                    isAdd = true;
                    cUser.CreatedDate = DateTime.Now;
                    context.tbl_ClientUsers.InsertOnSubmit(cUser);
                }
                context.SubmitChanges();

                //save the footer template
                var fTemplate = context.tbl_EmailFooters.FirstOrDefault(t => t.ClientUserId == clientUser.ClientUserId);
                if (clientUser.FooterTemplate != null)
                {
                    if (fTemplate == null)
                        fTemplate = new tbl_EmailFooter();
                    fTemplate.ClientUserId = cUser.ClientUserId;
                    fTemplate.FooterTemplate = clientUser.FooterTemplate.Template;
                    fTemplate.InUse = true;
                    if (fTemplate.FooterTemplateId <= 0)
                        context.tbl_EmailFooters.InsertOnSubmit(fTemplate);
                }
                else if (fTemplate != null) // template found, but not in use
                    fTemplate.InUse = false;
                // submit the changes
                context.SubmitChanges();
            }
            catch (Exception)
            {
                return -1;
            }
            // Add History
            new Histories().AddHistory(new History
               {
                   RefId = clientUser.ClientUserId,
                   RefType = "ClientUser",
                   ClientUserId = clientUser.UpdatedBy,
                   TypeId = isAdd ? 12 : 13,
                   SubRefType = "ClientUser",
                   SubRefId = clientUser.UpdatedBy
               });


            return cUser.ClientUserId;
        }