/// <summary>
        /// Handles the Delete event of the gDefinedType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gDefinedType_Delete( object sender, RowEventArgs e )
        {
            var definedValueService = new DefinedValueService();
            var definedTypeService = new DefinedTypeService();

            DefinedType type = definedTypeService.Get( e.RowKeyId );

            if ( type != null )
            {
                string errorMessage;
                if ( !definedTypeService.CanDelete( type, out errorMessage ) )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                    return;
                }

                // if this DefinedType has DefinedValues, see if they can be deleted
                var definedValues = definedValueService.GetByDefinedTypeId( type.Id ).ToList();

                foreach ( var value in definedValues )
                {
                    if ( !definedValueService.CanDelete( value, out errorMessage ) )
                    {
                        mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }
                }

                RockTransactionScope.WrapTransaction( () =>
                {
                    foreach ( var value in definedValues )
                    {
                        definedValueService.Delete( value, CurrentPersonId );
                        definedValueService.Save( value, CurrentPersonId );
                    }

                    definedTypeService.Delete( type, CurrentPersonId );
                    definedTypeService.Save( type, CurrentPersonId );
                } );
            }

            gDefinedType_Bind();
        }
        /// <summary>
        /// Handles the Delete event of the rGridType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void rGridType_Delete( object sender, RowEventArgs e )
        {
            DefinedType type = new DefinedTypeService().Get( (int)rGridType.DataKeys[e.RowIndex]["id"] );

            var valueService = new DefinedValueService();
            var typeService = new DefinedTypeService();

            if ( type != null )
            {
                // if this DefinedType has DefinedValues, delete them
                var hasDefinedValues = valueService
                .GetByDefinedTypeId( type.Id )
                .ToList();

                foreach ( var value in hasDefinedValues )
                {
                    valueService.Delete( value, CurrentPersonId );
                    valueService.Save( value, CurrentPersonId );
                }

                typeService.Delete( type, CurrentPersonId );
                typeService.Save( type, CurrentPersonId );
            }

            rGridType_Bind();
        }
        /// <summary>
        /// Handles the Click event of the btnSaveType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSaveType_Click( object sender, EventArgs e )
        {
            DefinedType definedType = null;
            DefinedTypeService typeService = new DefinedTypeService();

            int definedTypeId = hfDefinedTypeId.ValueAsInt();

            if ( definedTypeId == 0 )
            {
                definedType = new DefinedType();
                definedType.IsSystem = false;
                definedType.Order = 0;
                typeService.Add( definedType, CurrentPersonId );
            }
            else
            {
                DefinedTypeCache.Flush( definedTypeId );
                definedType = typeService.Get( definedTypeId );
            }

            definedType.Name = tbTypeName.Text;
            definedType.Category = tbTypeCategory.Text;
            definedType.Description = tbTypeDescription.Text;
            definedType.FieldTypeId = int.Parse( ddlTypeFieldType.SelectedValue );

            if ( !definedType.IsValid )
            {
                // Controls will render the error messages                    
                return;
            }

            RockTransactionScope.WrapTransaction( () =>
            {
                typeService.Save( definedType, CurrentPersonId );

                // get it back to make sure we have a good Id
                definedType = typeService.Get( definedType.Guid );
            } );

            var qryParams = new Dictionary<string, string>();
            qryParams["definedTypeId"] = definedType.Id.ToString();
            NavigateToPage( RockPage.Guid, qryParams );
        }
        protected void btnSaveType_Click( object sender, EventArgs e )
        {
            using ( new Rock.Data.UnitOfWorkScope() )
            {
                DefinedTypeService typeService = new DefinedTypeService();

                DefinedType definedType;

                int typeId = (( hfIdType.Value ) != null && hfIdType.Value != String.Empty ) ? Int32.Parse( hfIdType.Value ) : 0;

                if ( typeId == 0 )
                {
                    definedType = new DefinedType();
                    definedType.IsSystem = false;
                    definedType.Order = 0;
                    typeService.Add( definedType, CurrentPersonId );
                }
                else
                {
                    Rock.Web.Cache.DefinedTypeCache.Flush( typeId );
                    definedType = typeService.Get( typeId );
                }

                definedType.Name = tbTypeName.Text;
                definedType.Category = tbTypeCategory.Text;
                definedType.Description = tbTypeDescription.Text;
                definedType.FieldTypeId = Int32.Parse( ddlTypeFieldType.SelectedValue );

                typeService.Save( definedType, CurrentPersonId );
            }

            rGridType_Bind();

            pnlTypeDetails.Visible = false;
            pnlTypes.Visible = true;
        }