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);
		}
        //Add Data From Linked Tables for Display
        public void EditForDisplay(ClientTelephony clientTelephony)
        {
            //Hierarchy
            if (clientTelephony.HierarchyType == "ClientTopUnit")
            {
                ClientTopUnitRepository clientTopUnitRepository = new ClientTopUnitRepository();
                ClientTopUnit           clientTopUnit           = clientTopUnitRepository.GetClientTopUnit(clientTelephony.HierarchyItem);
                if (clientTopUnit != null)
                {
                    clientTelephony.HierarchyName = clientTopUnit.ClientTopUnitName;
                }
            }
            else if (clientTelephony.HierarchyType == "ClientSubUnit")
            {
                ClientSubUnitRepository clientSubUnitRepository = new ClientSubUnitRepository();
                ClientSubUnit           clientSubUnit           = clientSubUnitRepository.GetClientSubUnit(clientTelephony.HierarchyItem);
                if (clientSubUnit != null)
                {
                    clientTelephony.HierarchyName = clientSubUnit.ClientSubUnitName;
                }
            }

            //MainNumberFlag
            clientTelephony.MainNumberFlagNullable = (clientTelephony.MainNumberFlag == true) ? true : false;
        }
        //Delete in DB
        public void Delete(ClientTelephony clientTelephony)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

            db.spDesktopDataAdmin_DeleteClientTelephony_v1(
                clientTelephony.ClientTelephonyId,
                adminUserGuid,
                clientTelephony.VersionNumber
                );
        }
        // GET: /View
        public ActionResult ViewItem(int id)
        {
            //Check Exists
            ClientTelephony clientTelephony = new ClientTelephony();
            clientTelephony = clientTelephonyRepository.GetClientTelephony(id);
            if (clientTelephony == null)
            {
                ViewData["ActionMethod"] = "ViewGet";
                return View("RecordDoesNotExistError");
            }

            clientTelephonyRepository.EditForDisplay(clientTelephony);
            return View(clientTelephony);
        }
		// 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);
		}
		public ActionResult Edit(ClientTelephony clientTelephony)
		{
			//Check Access Rights to Domain
			if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
			{
				ViewData["Message"] = "You do not have access to this item";
				return View("Error");
			}

			//Update  Model from Form
			try
			{
				UpdateModel(clientTelephony);
			}
			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");
			}

			//Database Update
			try
			{
				clientTelephonyRepository.Update(clientTelephony);
			}
			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");
		}
		public ActionResult Delete(ClientTelephonyVM clientTelephonyVM)
		{
			//Get Item 
			ClientTelephony clientTelephony = new ClientTelephony();
			clientTelephony = clientTelephonyRepository.GetClientTelephony(clientTelephonyVM.ClientTelephony.ClientTelephonyId);

			//Check Exists
			if (clientTelephonyVM.ClientTelephony == null)
			{
				ViewData["ActionMethod"] = "DeletePost";
				return View("RecordDoesNotExistError");
			}

			//Check Access Rights to Domain
			if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
			{
				ViewData["Message"] = "You do not have access to this item";
				return View("Error");
			}

			//Delete Item
			try
			{
				clientTelephonyRepository.Delete(clientTelephony);
			}
			catch (SqlException ex)
			{
				//Versioning Error - go to standard versionError page
				if (ex.Message == "SQLVersioningError")
				{
					ViewData["ReturnURL"] = "/ClientTelephony.mvc/Delete/" + clientTelephony.ClientTelephonyId;
					return View("VersionError");
				}

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

			//Return
			return RedirectToAction("List");
		}
		public ActionResult Delete(int id)
		{
			ClientTelephonyVM clientTelephonyVM = new ClientTelephonyVM();

			ClientTelephony clientTelephony = new ClientTelephony();
			clientTelephony = clientTelephonyRepository.GetClientTelephony(id);

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

			clientTelephonyVM.ClientTelephony = clientTelephony;

			clientTelephonyRepository.EditForDisplay(clientTelephony);

			return View(clientTelephonyVM);
		}
        //Add to DB
        public void Add(ClientTelephony clientTelephony)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

            db.spDesktopDataAdmin_InsertClientTelephony_v1(
                clientTelephony.ClientTelephonyDescription,
                clientTelephony.PhoneNumber,
                clientTelephony.CountryCode,
                clientTelephony.HierarchyType,
                clientTelephony.HierarchyItem,
                clientTelephony.MainNumberFlagNullable,
                clientTelephony.TelephonyTypeId,
                clientTelephony.TravelerBackOfficeTypeCode,
                clientTelephony.ExpiryDate,
                clientTelephony.InternationalPrefixCode,
                clientTelephony.PhoneNumberwithInternationalPrefixCode,
                clientTelephony.CallerEnteredDigitDefinitionTypeId,
                clientTelephony.ClientSnSDescription,
                clientTelephony.ClientSnSButtonText,
                adminUserGuid
                );
        }
 public ClientTelephonyVM(ClientTelephony clientTelephony)
 {
     ClientTelephony = clientTelephony;
 }