public ActionResult AddContact(long id, int state)
        {
            int type = 1;
            CCFolder folderDetails = null;

            AddContactViewModel folderFields = new AddContactViewModel();
            folderFields.FolderFields = CCFieldRepository.CCFolderFields.Where(fid => fid.FolderID == id).ToList();
            folderFields.FolderID = id;

            folderDetails = CCFolderRepository.FolderDetails(id);
            if (folderDetails.isCrimeDiary == true)
                folderFields.isCrimeDiary = true;
            else
                folderFields.isCrimeDiary = false;

            if (folderDetails != null) { type = folderDetails.Type; }
            else { type = 1; }
            folderFields.FolderType = type;

            if (state == 1) { ViewBag.Message = "Failed to import contact"; }
            else { ViewBag.Message = ""; }
            return View(folderFields);
        }
        public ActionResult AddContact(AddContactViewModel objContact)
        {
            Account accountObj = (Account)Session["account"];
            string timeZone = (string)Session["timeZone"];
            int type = 1;
            ItemsImporter importcontact = new ItemsImporter(CCFieldRepository, CCFieldValueRepository, CCItemRepository);
            CCFolder folderDetails = null;
            folderDetails = CCFolderRepository.FolderDetails(objContact.FolderID);
            if (folderDetails != null) { type = folderDetails.Type; }
            else { type = 1; }
            bool res = importcontact.ImportSingleContact(objContact, type, accountObj.AccountGUID, timeZone);

            LimitationsViewModel limitationsObj = (LimitationsViewModel)Session["limitations"];
            HelperFunctions HF = new HelperFunctions();
            limitationsObj = HF.updateAccountLimitations(accountObj);
            Session["limitations"] = limitationsObj;

            if (res) return RedirectToAction("Items", "Folder", new { id = objContact.FolderID });
            else { return RedirectToAction("AddContact", "Folder", new { id = objContact.FolderID, state = 1 }); }
        }
        public ActionResult ViewFields(long ID)
        {
            TempData["SelectedMenu"] = "Manage";
            TempData["SelectedSubMenu"] = "ManageFolders";
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];

            var fields = CCFieldRepository.CCFolderFields.Where(u => u.FolderID == ID & u.AccountGUID == accountObj.AccountGUID & u.isActive == true).ToList();

            List<String> objFields = new List<string>();

            CCFolder folderDetails = null;
            folderDetails = CCFolderRepository.FolderDetails(ID);

            if (folderDetails.Type == 1)
            {
                if (accountObj.PlanID == 1)
                    objFields = FieldsConfigHelper.GetFieldForContactSimple();
                else if (accountObj.PlanID == 2)
                    objFields = FieldsConfigHelper.GetFieldForContactBusiness();
                else
                    objFields = FieldsConfigHelper.GetFieldForContactFull();
            }
            else
            {
                if(folderDetails.isCrimeDiary == true)
                    objFields = FieldsConfigHelper.GetFieldForCrimeDiaryAppointment();
                else
                    objFields = FieldsConfigHelper.GetFieldForAppointmentSimple();
            }

            foreach (var field in fields)
            {
                foreach (var ccfield in objFields)
                {
                    if (ccfield == field.FieldCaption)
                    {
                        field.isMandatory = true;
                        break;
                    }
                }
            }

            AddContactViewModel folderFields = new AddContactViewModel();
            folderFields.FolderFields = fields;

            int type = 1;

            if (folderDetails != null) { type = folderDetails.Type; }
            else { type = 1; }
            folderFields.FolderType = type;

            if (fields.Count == 0)
            {
                return RedirectToAction("FieldMessage", "Folder", new { id = ID });
            }
            else
            {

                return View(folderFields);
            }
        }
        public bool ImportSingleContact(AddContactViewModel objContact, int type,string accountGUID, string timeZone)
        {
            List<string> fieldValus = objContact.FolderValues.Split('|').ToList();
            List<CCFieldValue> ObjFieldValues = new List<CCFieldValue>();
            List<CCFieldValue> savedfields = new List<CCFieldValue>();
            AddDedupeViewModel dedupe = new AddDedupeViewModel();
            DateTime startTime;
            DateTime endTime;
            string notes = "";

            long contectID = CCItemRepository.CreateContact(objContact.FolderID, accountGUID);
            if (contectID > 0)
            {
                List<CCFolderField> folderFields = new List<CCFolderField>();
                folderFields = CCFieldRepository.CCFolderFields.Where(fid => fid.FolderID == objContact.FolderID & fid.AccountGUID==accountGUID).ToList();

                int i = 0;
                foreach (var field in fieldValus)
                {
                    if (field != "")
                    {
                        var fieldName = folderFields[i].FieldCaption;
                        if (type == 1)
                        {
                            if (fieldName == "First Name") { dedupe.FirstName = field; }
                            if (fieldName == "Middle Name") { dedupe.MiddleName = field; }
                            if (fieldName == "Last Name") { dedupe.LastName = field; }
                            if (fieldName == "Company") { dedupe.CompanyName = field; }
                            if (fieldName == "Email Address") { dedupe.Email = field; }
                        }
                        else
                        {
                            if (fieldName == "Subject") { dedupe.Subject = field; }
                            if (fieldName == "Start Time")
                            {
                                string format = "yyyy-MM-dd HH:mm";
                                startTime = DateTime.Parse(field);
                                dedupe.StartDateTime = ConvertLocaltoUTC(startTime, timeZone).ToString(format);
                            }
                            if (fieldName == "End Time")
                            {
                                string format = "yyyy-MM-dd HH:mm";
                                //endTime = DateTimeOffset.Parse(field).UtcDateTime;
                                //dedupe.EndDateTime = endTime.ToString(format);
                                endTime = DateTime.Parse(field);
                                dedupe.EndDateTime = ConvertLocaltoUTC(endTime, timeZone).ToString(format);
                            }
                        }

                        if (fieldName == "Notes")
                            notes = field;

                        var fieldID = folderFields[i].FieldID;
                        i++;

                        CCFieldValue objFieldValue = new CCFieldValue();
                        objFieldValue.FieldID = fieldID;
                        if (fieldName == "Start Time") { objFieldValue.Value = dedupe.StartDateTime; }
                        else if (fieldName == "End Time") { objFieldValue.Value = dedupe.EndDateTime; }
                        else
                        {
                            if (field == " ") objFieldValue.Value = String.Empty;
                            else objFieldValue.Value = field;
                        }
                        objFieldValue.ItemID = contectID;
                        objFieldValue.LastUpdated = DateTime.UtcNow;
                        objFieldValue.AccountGUID = accountGUID;
                        ObjFieldValues.Add(objFieldValue);
                    }
                }
                savedfields = CCFieldValueRepository.SaveFieldsObjValues(ObjFieldValues);
            }
            if (savedfields.Count() > 0)
            {
                bool res = UpdateContact(dedupe, contectID, type, notes);
                return true;
            }
            else return false;
        }