public ActionResult Create()
		{
			//Create Item 
			ClientTelephony clientTelephony = new ClientTelephony();

			//AccessRights
			RolesRepository rolesRepository = new RolesRepository();
			if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
			{
				ViewData["Message"] = "You do not have access to this item";
				return View("Error");
			}

			ClientTelephonyVM clientTelephonyVM = new ClientTelephonyVM();

			clientTelephonyVM.ClientTelephony = clientTelephony;
			
			//Countries
			clientTelephonyVM.Countries = new SelectList(countryRepository.GetAllCountries().ToList(), "CountryCode", "CountryNameWithInternationalPrefixCode");
			
			//HierarchyTypes
			clientTelephonyVM.HierarchyTypes = new SelectList(clientTelephonyRepository.GetAllHierarchyTypes().ToList(), "Value", "Text");

            //TelephoneTypes
            clientTelephonyVM.TelephoneTypes = new SelectList(telephonyTypeRepository.GetAllTelephonyTypes().ToList(), "TelephonyTypeId", "TelephonyTypeDescription");

            //TravelerBackOfficeTypes
            clientTelephonyVM.TravelerBackOfficeTypes = new SelectList(travelerBackOfficeTypeRepository.GetAllTravelerBackOfficeTypes().ToList(), "TravelerBackOfficeTypeCode", "TravelerBackOfficeTypeDescription");

            //CallerEnteredDigitDefinitionTypes
            CallerEnteredDigitDefinitionTypeRepository callerEnteredDigitDefinitionTypeRepository = new CallerEnteredDigitDefinitionTypeRepository();
            clientTelephonyVM.CallerEnteredDigitDefinitionTypes = new SelectList(callerEnteredDigitDefinitionTypeRepository.GetAllCallerEnteredDigitDefinitionTypes().ToList(), "CallerEnteredDigitDefinitionTypeId", "CallerEnteredDigitDefinitionTypeDescription");

            return View(clientTelephonyVM);
		}
		// GET: /Edit
		public ActionResult Edit(int id)
		{
			//Get Item 
			ClientTelephony clientTelephony = new ClientTelephony();
			clientTelephony = clientTelephonyRepository.GetClientTelephony(id);

			//Check Exists
			if (clientTelephony == null)
			{
				ViewData["ActionMethod"] = "EditGet";
				return View("RecordDoesNotExistError");
			}

			//AccessRights
			RolesRepository rolesRepository = new RolesRepository();
			if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
			{
				ViewData["Message"] = "You do not have access to this item";
				return View("Error");
			}

			ClientTelephonyVM clientTelephonyVM = new ClientTelephonyVM();

			clientTelephonyVM.ClientTelephony = clientTelephony;

			//Countries
			clientTelephonyVM.Countries = new SelectList(countryRepository.GetAllCountries().ToList(), "CountryCode", "CountryNameWithInternationalPrefixCode", clientTelephony.CountryCode);

			//HierarchyTypes
			clientTelephonyVM.HierarchyTypes = new SelectList(clientTelephonyRepository.GetAllHierarchyTypes().ToList(), "Value", "Text", clientTelephony.HierarchyType);

			//TelephoneTypes
			clientTelephonyVM.TelephoneTypes = new SelectList(telephonyTypeRepository.GetAllTelephonyTypes().ToList(), "TelephonyTypeId", "TelephonyTypeDescription", clientTelephony.TelephonyTypeId);

			//TravelerBackOfficeTypes
			clientTelephonyVM.TravelerBackOfficeTypes = new SelectList(travelerBackOfficeTypeRepository.GetAllTravelerBackOfficeTypes().ToList(), "TravelerBackOfficeTypeCode", "TravelerBackOfficeTypeDescription", clientTelephony.TravelerBackOfficeTypeCode);

            //CallerEnteredDigitDefinitionTypes
            CallerEnteredDigitDefinitionTypeRepository callerEnteredDigitDefinitionTypeRepository = new CallerEnteredDigitDefinitionTypeRepository();
            clientTelephonyVM.CallerEnteredDigitDefinitionTypes = new SelectList(callerEnteredDigitDefinitionTypeRepository.GetAllCallerEnteredDigitDefinitionTypes().ToList(), "CallerEnteredDigitDefinitionTypeId", "CallerEnteredDigitDefinitionTypeDescription", clientTelephony.CallerEnteredDigitDefinitionTypeId);

            clientTelephonyRepository.EditForDisplay(clientTelephony);

			return View(clientTelephonyVM);
		}
        // GET: /Edit
        public ActionResult Edit(string id, string dn)
        {
            //Get ClientSubUnitTelephony
            ClientSubUnitTelephony clientSubUnitTelephony = new ClientSubUnitTelephony();

            clientSubUnitTelephony = clientSubUnitTelephonyRepository.GetClientSubUnitTelephony(id, dn);

            //Check Exists
            if (clientSubUnitTelephony == null)
            {
                ViewData["ActionMethod"] = "EditGet";
                return(View("RecordDoesNotExistError"));
            }

            //Access Rights
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToClientSubUnit(id))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            ClientSubUnitTelephonyVM clientSubUnitTelephonyVM = new ClientSubUnitTelephonyVM();

            clientSubUnitTelephonyVM.ClientSubUnitTelephony = clientSubUnitTelephony;

            //Get ClientSubUnit
            ClientSubUnit clientSubUnit = new ClientSubUnit();

            clientSubUnit = clientSubUnitRepository.GetClientSubUnit(id);
            clientSubUnitTelephonyVM.ClientSubUnit = clientSubUnit;

            //Identifier Drop-down
            CallerEnteredDigitDefinitionTypeRepository callerEnteredDigitDefinitionTypeRepository = new CallerEnteredDigitDefinitionTypeRepository();

            clientSubUnitTelephonyVM.CallerEnteredDigitDefinitionTypes = new SelectList(callerEnteredDigitDefinitionTypeRepository.GetAllCallerEnteredDigitDefinitionTypes().ToList(), "CallerEnteredDigitDefinitionTypeId", "CallerEnteredDigitDefinitionTypeDescription", clientSubUnitTelephony.CallerEnteredDigitDefinitionTypeId);

            //Show Create Form
            return(View(clientSubUnitTelephonyVM));
        }
        public ActionResult Create(ClientSubUnitTelephonyVM clientSubUnitTelephonyVM)
        {
            string clientSubUnitGuid = clientSubUnitTelephonyVM.ClientSubUnitTelephony.ClientSubUnitGuid;

            ClientSubUnit clientSubUnit = new ClientSubUnit();

            clientSubUnit = clientSubUnitRepository.GetClientSubUnit(clientSubUnitGuid);

            //Check Exists
            if (clientSubUnit == null)
            {
                ViewData["ActionMethod"] = "CreatePost";
                return(View("RecordDoesNotExistError"));
            }

            //Access Rights
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToClientSubUnit(clientSubUnitGuid))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            ClientSubUnitTelephony clientSubUnitTelephony = new ClientSubUnitTelephony();

            clientSubUnitTelephony = clientSubUnitTelephonyRepository.GetClientSubUnitTelephony(clientSubUnitGuid, clientSubUnitTelephonyVM.ClientSubUnitTelephony.DialedNumber);
            if (clientSubUnitTelephony != null)
            {
                ModelState.AddModelError("ClientSubUnitTelephony.DialedNumber", "This number already exists");

                CallerEnteredDigitDefinitionTypeRepository callerEnteredDigitDefinitionTypeRepository = new CallerEnteredDigitDefinitionTypeRepository();
                clientSubUnitTelephonyVM.CallerEnteredDigitDefinitionTypes = new SelectList(callerEnteredDigitDefinitionTypeRepository.GetAllCallerEnteredDigitDefinitionTypes().ToList(), "CallerEnteredDigitDefinitionTypeId", "CallerEnteredDigitDefinitionTypeDescription");


                clientSubUnitTelephonyVM.ClientSubUnit = clientSubUnit;
                return(View(clientSubUnitTelephonyVM));
            }

            //Update  Model from Form
            try
            {
                UpdateModel <ClientSubUnitTelephony>(clientSubUnitTelephonyVM.ClientSubUnitTelephony, "ClientSubUnitTelephony");
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }



            try
            {
                clientSubUnitTelephonyRepository.Add(clientSubUnitTelephonyVM.ClientSubUnitTelephony);
            }
            catch (SqlException ex)
            {
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }


            return(RedirectToAction("List", new { id = clientSubUnitGuid }));
        }