protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {
            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["invitationID"]))
            {
                invitation_id = Convert.ToInt32(Request.QueryString["invitationID"]);

                qPtl_Invitation invite = new qPtl_Invitation(invitation_id);
                invite.InvitationAudience = ddlInvitationAudience.SelectedValue;
                invite.InvitationAudienceName = txtAudienceName.Text;
                invite.Available = rblAvailable.SelectedValue;
                invite.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                invite.LastModified = DateTime.Now;
                if (!String.IsNullOrEmpty(Convert.ToString(rdtStartTime.SelectedDate)))
                    invite.StartDate = rdtStartTime.SelectedDate;
                if (!String.IsNullOrEmpty(Convert.ToString(rdtEndTime.SelectedDate)))
                    invite.EndDate = rdtEndTime.SelectedDate;

                if (!String.IsNullOrEmpty(ddlSchools.SelectedValue))
                {
                    if (invite.SpaceID > 0)
                    {
                        qSoc_Space space = new qSoc_Space(invite.SpaceID);
                        space.SchoolID = Convert.ToInt32(ddlSchools.SelectedValue);
                        space.Update();
                    }
                }

                invite.Update();
            }
            else
            {
                int functional_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_InvitationFunctionalRoleID"]);
                int member_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_DefaultRoleID"]);
                int parent_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_ParentRoleID"]);
                int moderator_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_ModeratorRoleID"]);
                string invitation_type = Convert.ToString(ConfigurationManager.AppSettings["register_InvitationType"]);
                int invite_length = Convert.ToInt32(ConfigurationManager.AppSettings["register_InvitationLength"]);

                DateTime start_time = new DateTime();
                DateTime end_time = new DateTime();

                start_time = Convert.ToDateTime(rdtStartTime.SelectedDate);
                end_time = Convert.ToDateTime(rdtEndTime.SelectedDate);

                if (ddlInvitationAudience.SelectedValue == "individual")
                {
                    var invite = qPtl_Invitation.GenerateInvite(0, start_time, end_time, Convert.ToInt32(Context.Items["UserID"]), 0, invite_length, invitation_type, member_role_id, functional_role_id);

                    invite.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    invite.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    invite.LastModified = DateTime.Now;
                    invite.MarkAsDelete = 0;
                    invite.InvitationAudience = ddlInvitationAudience.SelectedValue;
                    invite.InvitationAudienceName = txtAudienceName.Text;
                    invite.Available = rblAvailable.SelectedValue;
                    invite.InvitationStatus = "Redeemable";
                    invite.CurrRedemptions = 0;
                    invite.MaxRedemptions = 1;

                    invite.Update();
                    invitation_id = invite.InvitationID;
                }
                else if (ddlInvitationAudience.SelectedValue == "group")
                {
                    var invite = qPtl_Invitation.GenerateInvite(0, start_time, end_time, Convert.ToInt32(Context.Items["UserID"]), 0, invite_length, invitation_type, member_role_id, functional_role_id);

                    invite.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    invite.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    invite.LastModified = DateTime.Now;
                    invite.MarkAsDelete = 0;
                    invite.InvitationAudience = ddlInvitationAudience.SelectedValue;
                    invite.InvitationAudienceName = txtAudienceName.Text;
                    invite.Available = rblAvailable.SelectedValue;
                    invite.InvitationStatus = "Redeemable";
                    invite.CurrRedemptions = 0;
                    if (!String.IsNullOrEmpty(txtMaxRedemptions.Text))
                        invite.MaxRedemptions = Convert.ToInt32(txtMaxRedemptions.Text);

                    invite.Update();
                    invitation_id = invite.InvitationID;
                }
                else if (ddlInvitationAudience.SelectedValue == "family" || ddlInvitationAudience.SelectedValue == "new family")
                {
                    int family_id = 0;
                    string audience_name = string.Empty;

                    if (ddlInvitationAudience.SelectedValue == "new family")
                    {
                        // create family and add information
                        qPtl_Family family = new qPtl_Family();

                        family.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        family.Available = rblAvailable.SelectedValue;
                        family.Created = DateTime.Now;
                        family.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                        family.LastModified = DateTime.Now;
                        family.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                        family.MarkAsDelete = 0;
                        family.FamilyName = txtAudienceName.Text;
                        family.ContactFirstName = txtFirstName.Text;
                        family.ContactLastName = txtLastName.Text;
                        family.ContactAddress1 = txtAddress1.Text;
                        family.ContactAddress2 = txtAddress2.Text;
                        family.ContactCity = txtCity.Text;
                        family.ContactStateProvince = txtStateProvince.Text;
                        family.ContactPostalCode = txtPostalCode.Text;
                        family.ContactCountry = txtCountry.Text;
                        family.ContactPhone1 = txtPhone1.Text;
                        if (!String.IsNullOrEmpty(ddlPhone1Type.SelectedValue))
                            family.ContactPhone1Type = ddlPhone1Type.SelectedValue;
                        family.ContactPhone2 = txtPhone2.Text;
                        if (!String.IsNullOrEmpty(ddlPhone2Type.SelectedValue))
                            family.ContactPhone2Type = ddlPhone2Type.SelectedValue;
                        family.ContactEmail = txtEmail.Text;
                        family.ContactRelationship = txtRelationship.Text;

                        family.Insert();

                        family_id = family.FamilyID;
                        audience_name = txtAudienceName.Text;
                    }
                }
                else if (ddlInvitationAudience.SelectedValue == "moderated group" || ddlInvitationAudience.SelectedValue == "new moderated group")
                {
                    int space_id = 0;
                    string group_audience_name = string.Empty;

                    if (ddlInvitationAudience.SelectedValue == "new moderated group" || ddlInvitationAudience.SelectedValue == "moderatedGroup")
                    {
                        // create space
                        qSoc_Space space = new qSoc_Space();
                        space.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        space.Available = rblAvailable.SelectedValue;
                        space.Created = DateTime.Now;
                        space.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                        space.LastModified = DateTime.Now;
                        space.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                        space.MarkAsDelete = 0;
                        space.SpaceShortName = txtModeratedGroupNameShort.Text;
                        space.SpaceName = txtModeratedGroupName.Text;
                        space.AccessMode = "invitation";
                        space.VisibleInDirectory = "No";
                        space.SpaceType = ddlGroupType.SelectedValue;
                        if (!String.IsNullOrEmpty(ddlSchools.SelectedValue))
                        {
                            space.SchoolID = Convert.ToInt32(ddlSchools.SelectedValue);
                        }
                        space.Insert();

                        space_id = space.SpaceID;
                        group_audience_name = txtModeratedGroupName.Text;
                    }
                    else
                    {
                        space_id = Convert.ToInt32(ddlExistingGroups.SelectedValue);
                        qSoc_Space space = new qSoc_Space(space_id);
                        group_audience_name = space.SpaceShortName;
                    }

                    // create necessary invitations -- moderators
                    int num_moderators = Convert.ToInt32(txtNumGroupModerators.Text);
                    for (int i = 0; i < num_moderators; i++)
                    {
                        var invite = qPtl_Invitation.GenerateInvite(0, start_time, end_time, Convert.ToInt32(Context.Items["UserID"]), 0, invite_length, invitation_type, moderator_role_id, functional_role_id);
                        invite.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        invite.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                        invite.LastModified = DateTime.Now;
                        invite.MarkAsDelete = 0;
                        invite.InvitationAudience = "moderated group";
                        invite.InvitationAudienceName = group_audience_name;
                        invite.Available = rblAvailable.SelectedValue;
                        invite.InvitationStatus = "Redeemable";
                        invite.CurrRedemptions = 0;
                        invite.MaxRedemptions = 1;
                        invite.SpaceID = space_id;
                        invite.Update();
                    }

                    // create single teen invitation
                    var invite_t = qPtl_Invitation.GenerateInvite(0, start_time, end_time, Convert.ToInt32(Context.Items["UserID"]), 0, invite_length, invitation_type, member_role_id, functional_role_id);
                    invite_t.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    invite_t.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    invite_t.LastModified = DateTime.Now;
                    invite_t.MarkAsDelete = 0;
                    invite_t.InvitationAudience = "moderated group";
                    invite_t.InvitationAudienceName = group_audience_name;
                    invite_t.Available = rblAvailable.SelectedValue;
                    invite_t.InvitationStatus = "Redeemable";
                    invite_t.CurrRedemptions = 0;
                    if (!String.IsNullOrEmpty(txtNumTeens.Text))
                        invite_t.MaxRedemptions = Convert.ToInt32(txtNumTeens.Text);
                    else
                        invite_t.MaxRedemptions = -1;
                    invite_t.SpaceID = space_id;
                    invite_t.Update();
                    invitation_id = invite_t.InvitationID;

                    qSoc_Space existing_space = new qSoc_Space(space_id);
                    existing_space.SpaceCode = invite_t.InviteCode;
                    existing_space.Update();
                }
            }

            if (!String.IsNullOrEmpty(Request.QueryString["invitationID"]))
            {
                lblMessage.Text = "*** Record Successfully Updated ***";
                lblMessageBottom.Text = "*** Record Successfully Updated ***";
                plhInvitationCode.Visible = true;
            }
            else
            {
                Response.Redirect(Request.Url.ToString() + "&mode=add-successful&invitationID=" + invitation_id);
            }
        }
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {
            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["invitationID"]))
            {
                invitation_id = Convert.ToInt32(Request.QueryString["invitationID"]);

                qPtl_Invitation invite = new qPtl_Invitation(invitation_id);
                invite.InvitationAudience = ddlInvitationAudience.SelectedValue;
                invite.InvitationAudienceName = txtAudienceName.Text;
                invite.Available = rblAvailable.SelectedValue;
                invite.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                invite.LastModified = DateTime.Now;
                if (!String.IsNullOrEmpty(Convert.ToString(rdtStartTime.SelectedDate)))
                    invite.StartDate = rdtStartTime.SelectedDate;
                if (!String.IsNullOrEmpty(Convert.ToString(rdtEndTime.SelectedDate)))
                    invite.EndDate = rdtEndTime.SelectedDate;

                invite.Update();
            }
            else
            {
                int functional_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_InvitationFunctionalRoleID"]);
                int member_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_DefaultRoleID"]);
                int parent_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_ParentRoleID"]);
                int moderator_role_id = Convert.ToInt32(ConfigurationManager.AppSettings["register_ModeratorRoleID"]);
                string invitation_type = Convert.ToString(ConfigurationManager.AppSettings["register_InvitationType"]);
                int invite_length = Convert.ToInt32(ConfigurationManager.AppSettings["register_InvitationLength"]);

                DateTime start_time = new DateTime();
                DateTime end_time = new DateTime();

                start_time = Convert.ToDateTime(rdtStartTime.SelectedDate);
                end_time = Convert.ToDateTime(rdtEndTime.SelectedDate);

                int family_id = 0;
                string audience_name = string.Empty;

                if (ddlInvitationAudience.SelectedValue == "new family")
                {
                    // create family and add information
                    qPtl_Family family = new qPtl_Family();

                    family.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    family.Available = rblAvailable.SelectedValue;
                    family.Created = DateTime.Now;
                    family.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    family.LastModified = DateTime.Now;
                    family.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    family.MarkAsDelete = 0;
                    family.FamilyName = txtAudienceName.Text;
                    family.ContactFirstName = txtFirstName.Text;
                    family.ContactLastName = txtLastName.Text;
                    family.ContactAddress1 = txtAddress1.Text;
                    family.ContactAddress2 = txtAddress2.Text;
                    family.ContactCity = txtCity.Text;
                    family.ContactStateProvince = txtStateProvince.Text;
                    family.ContactPostalCode = txtPostalCode.Text;
                    family.ContactCountry = txtCountry.Text;
                    family.ContactPhone1 = txtPhone1.Text;
                    if (!String.IsNullOrEmpty(ddlPhone1Type.SelectedValue))
                        family.ContactPhone1Type = ddlPhone1Type.SelectedValue;
                    family.ContactPhone2 = txtPhone2.Text;
                    if (!String.IsNullOrEmpty(ddlPhone2Type.SelectedValue))
                        family.ContactPhone2Type = ddlPhone2Type.SelectedValue;
                    family.ContactEmail = txtEmail.Text;
                    family.ContactRelationship = txtRelationship.Text;

                    family.Insert();

                    family_id = family.FamilyID;
                    audience_name = txtAudienceName.Text;
                }
            }

            if (!String.IsNullOrEmpty(Request.QueryString["invitationID"]))
            {
                lblMessage.Text = "*** Record Successfully Updated ***";
                lblMessageBottom.Text = "*** Record Successfully Updated ***";
                plhInvitationCode.Visible = true;
            }
            else
            {
                Response.Redirect(Request.Url.ToString() + "&mode=add-successful&invitationID=" + invitation_id);
            }
        }
    }