public ActionResult AddCredentials(ExLogOnViewModel model, bool hosted = false)
        {
            EWSCode ewscode = new EWSCode();
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            TempData["SelectedMenu"] = "Manage";
            TempData["SelectedSubMenu"] = "ManageCredentials";
            string selectedButton = string.Empty;
            if (model.ExchangeType == 1) { selectedButton = model.SelectedButtonInhouse; }
            else { selectedButton = model.SelectedButton; }
            long credentialID = 0;
            model.ExistingConnections = CCConnectinRepository.CCSubscriptions.Where(folderID => folderID.AccountGUID == accountObj.AccountGUID & folderID.SecondaryAccount != "").ToList().GroupBy(x => x.SecondaryAccount).Select(y => y.First()).ToList();

            if (selectedButton == "auto")
            {
                ConnectionConfig cc = ewscode.AutoDiscoverConnectionDetails(model.Credentials.EmailAddress, model.Credentials.Password);
                //model.ExistingCredentials = CCCredentialRepository.Credentials.Where(aid => aid.AccountID == userObj.AccountID).ToList();
                model.ExistingCredentials = CCCredentialRepository.Credentials.Where(aguid => aguid.AccountGUID == accountObj.AccountGUID).ToList();
                model.ReturnUrl = "~/Folder/SelectFolder?src=" + "src" + "&primid=" + model.selectedFolderID;

                if (cc != null)
                {
                    //save crediantal
                    credentialID = SaveCredentials(model, selectedButton, hosted, cc.url, cc.version.ToString());

                    ViewBag.url = cc.url;
                    ViewBag.msucess = "Connection successful and saved.";
                    ViewBag.mfail = "";
                    ViewBag.tmessage = "sucess";
                    ViewBag.pass = Encryption.DecryptStringAES(model.Credentials.Password, rand);
                    model.ServerVer = cc.version.ToString();
                    Session["secondaryAccount"] = model.SecondaryAccount;
                    Session["accessType"] = model.AccessType;
                    string accessTypes = Session["accessType"].ToString();
                    Session["NewCredentialObject"] = model;
                }
                else
                {
                    ViewBag.url = "";
                    ViewBag.mfail = "Error: Auto-discover failed";
                    ViewBag.msucess = "";
                    ViewBag.tmessage = "fail";
                    ViewBag.pass = model.Credentials.Password;
                }
                model.CreatedCredentialID = credentialID;

                return View(model);
            }
            else if (selectedButton == "test")
            {

                string version = ewscode.StandardConnection(model.Credentials.EmailAddress, model.Credentials.Password, model.Credentials.URL);
                model.ExistingCredentials = CCCredentialRepository.Credentials.Where(aguid => aguid.AccountGUID == accountObj.AccountGUID).ToList();
                model.ReturnUrl = "~/Folder/SelectFolder?src=" + "src" + "&primid=" + model.selectedFolderID;
                if (version != "" && version != "404" && version != "401" && version != "null")
                {
                    //save crediantal
                    credentialID = SaveCredentials(model, selectedButton, hosted, model.Credentials.URL, version.ToString());

                    ViewBag.url = model.Credentials.URL;
                    ViewBag.pass = Encryption.DecryptStringAES(model.Credentials.Password, rand);
                    ViewBag.msucess = "Connection successful and saved.";
                    ViewBag.mfail = "";
                    ViewBag.tmessage = "sucess";
                    model.ServerVer = version.ToString();

                }
                else
                {
                    ViewBag.url = "";
                    if (version == "") { ViewBag.mfail = "Connection Fail"; }
                    else if (version == "404") { ViewBag.mfail = "Error: The URL was not found. Please check the URL."; }
                    else if (version == "401") { ViewBag.mfail = "Error: Server returned 'Unauthorized'. Please check the username and password"; }
                    else if (version == "null") { ViewBag.mfail = "Error: URL cannot be null. Please enter the URL."; }
                    else { ViewBag.mfail = "Connection Fail"; }
                    ViewBag.msucess = "";
                    ViewBag.tmessage = "fail";
                }

                model.CreatedCredentialID = credentialID;
                return View(model);
            }
            else if (selectedButton == "autoInhouse")
            {
                ConnectionConfig cc = ewscode.AutoDiscoverConnectionDetails(model.EmailAddressIn, model.Password);
                model.ExistingCredentials = CCCredentialRepository.Credentials.Where(aguid => aguid.AccountGUID == accountObj.AccountGUID).ToList();
                model.ReturnUrl = "~/Folder/SelectFolder?src=" + "src" + "&primid=" + model.selectedFolderID;

                if (cc != null)
                {
                    //save crediantal
                    credentialID = SaveCredentials(model, selectedButton, hosted, cc.url, cc.version.ToString());

                    ViewBag.urlin = cc.url;
                    ViewBag.msucessin = "Connection successful and saved.";
                    ViewBag.mfailin = "";
                    ViewBag.tmessagein = "";
                    ViewBag.passin = model.Password;
                    model.ServerVersionInhouse = cc.version.ToString();
                    Session["secondaryAccount"] = model.SecondaryAccountIn;
                    Session["accessType"] = model.AccessTypeIn;
                }
                else
                {
                    ViewBag.urlin = "";
                    ViewBag.mfailin = "Error: Auto-discover failed";
                    ViewBag.msucessin = "";
                    ViewBag.tmessagein = "";
                    ViewBag.passin = model.Password;
                }
                model.CreatedCredentialIDIn = credentialID;
                return View(model);

            }
            else if (selectedButton == "testInhouse")
            {
                model.ExistingCredentials = CCCredentialRepository.Credentials.Where(aguid => aguid.AccountGUID == accountObj.AccountGUID).ToList();
                string version = ewscode.StandardConnectionToInHouseExchanges(model.UserName, model.Password, model.URL, model.Domain);
                model.ReturnUrlInhouse = "~/Folder/SelectFolder?src=" + "src" + "&primid=" + model.selectedFolderIDInhouse;

                if (version != "" && version != "404" && version != "401" && version != "null")
                {
                    //save crediantal
                    credentialID = SaveCredentials(model, selectedButton, hosted, model.URL, version.ToString());

                    ViewBag.urlin = model.URL;
                    ViewBag.passin = model.Password;
                    ViewBag.msucessin = "Connection successful and saved.";
                    ViewBag.mfailin = "";
                    ViewBag.tmessagein = "sucess";
                    model.ServerVersionInhouse = version.ToString();

                }
                else
                {

                    ViewBag.urlin = "";
                    if (version == "") { ViewBag.mfailin = "Connection Fail"; }
                    else if (version == "404") { ViewBag.mfailin = "Error: The URL was not found. Please check the URL."; }
                    else if (version == "401") { ViewBag.mfailin = "Error: Server returned 'Unauthorized'. Please check the username and password"; }
                    else if (version == "null") { ViewBag.mfailin = "Error: URL cannot be null. Please enter the URL."; }
                    else { ViewBag.mfailin = "Connection Fail"; }
                    ViewBag.msucessin = "";
                    ViewBag.tmessagein = "fail";
                }
                model.CreatedCredentialIDIn = credentialID;
                return View(model);

            }
            else if (selectedButton == "setUpConnection")
            {
                Session["NewCredential"] = "Yes";
                return RedirectToAction("setUpNewConnection", "Folder");
            }
            else
            {
                Session["NewCredential"] = "Yes";
                return RedirectToAction("ManageCredentials", "Folder");
            }
        }