protected void btnMemberPrev_Click(object sender, EventArgs e)
        {
            SForce.SforceService sfdcbindingMembership = new SForce.SforceService();
            SForce.LoginResult   LoginResult           = sfdcbindingMembership.login(strUserName, strPasswd);
            sfdcbindingMembership.Url = LoginResult.serverUrl;
            sfdcbindingMembership.SessionHeaderValue = new SForce.SessionHeader();

            sfdcbindingMembership.SessionHeaderValue.sessionId = LoginResult.sessionId;
            queryResultMembership = sfdcbindingMembership.query(strQuerySelectFromMembership);


            int nMemberIndex = (int)Session["MemberIndex"];

            if (nMemberIndex > 0)
            {
                nMemberIndex--;
            }

            Session["MemberIndex"] = nMemberIndex;

            SForce.Membership__c membership = (SForce.Membership__c)queryResultMembership.records[nMemberIndex];
            txtContactId.Text        = membership.Id;
            txtMembershipName.Text   = membership.Name;
            txtMembershipStatus.Text = membership.c4g_Membership_Status__c;
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            rfvMedicareEligibility.Validate();
            rfvMedicareAandB.Validate();
            rfvParticipantProgram.Validate();

            if ((String)Session["AccountId"] != null)
            {
                strAccountId = (String)Session["AccountId"];
            }

            if ((String)Session["ContactId"] != null)
            {
                strPrimaryContactId = (String)Session["ContactId"];
            }
            else
            {
                String strQueryForPrimaryContactId = "select Id from Contact where cmm_Household__c = '" + strAccountId + "' " +
                                                     "and cmm_Household_Role__c = 'Head of Household'";

                SForce.QueryResult qrPrimaryContactId = Sfdcbinding.query(strQueryForPrimaryContactId);

                if (qrPrimaryContactId.size > 0)
                {
                    SForce.Contact ctPrimaryContactId = qrPrimaryContactId.records[0] as SForce.Contact;

                    strPrimaryContactId = ctPrimaryContactId.Id;
                }
            }
            if ((String)Session["SpouseId"] != null)
            {
                strSpouseId = (String)Session["SpouseId"];
            }
            else
            {
                String strQueryForSpouseId = "select Id from Contact where cmm_Household__c = '" + strAccountId + "' " +
                                             "and cmm_Household_Role__c = 'Spouse'";

                SForce.QueryResult qrSpouseId = Sfdcbinding.query(strQueryForSpouseId);

                if (qrSpouseId.size > 0)
                {
                    SForce.Contact ctSpouseId = qrSpouseId.records[0] as SForce.Contact;

                    strSpouseId = ctSpouseId.Id;
                }
            }


            SForce.Contact ctUpdate = new SForce.Contact();

            ctUpdate.Id = strPrimaryContactId;
            ctUpdate.cmm_Household__c      = strAccountId;
            ctUpdate.cmm_Household_Role__c = "Head of Household";

            // whether or not qualifying medicare
            if (rbListMedicareYesNo.SelectedIndex == 0)
            {
                ctUpdate.c4g_Qualifies_for_Medicare__c          = true;
                ctUpdate.c4g_Qualifies_for_Medicare__cSpecified = true;
            }
            if (rbListMedicareYesNo.SelectedIndex == 1)
            {
                ctUpdate.c4g_Qualifies_for_Medicare__c          = false;
                ctUpdate.c4g_Qualifies_for_Medicare__cSpecified = true;
            }

            // whether or not qualifying medicare A and B
            if (rbListMedicareAandB.SelectedIndex == 0)
            {
                ctUpdate.c4g_Qualifies_for_Medicare_A_and_B__c          = true;
                ctUpdate.c4g_Qualifies_for_Medicare_A_and_B__cSpecified = true;
            }
            if (rbListMedicareAandB.SelectedIndex == 1)
            {
                ctUpdate.c4g_Qualifies_for_Medicare_A_and_B__c          = false;
                ctUpdate.c4g_Qualifies_for_Medicare_A_and_B__cSpecified = true;
            }

            String strPrimaryProgram = ddlParticipantProgram.SelectedValue.Substring(0, ddlParticipantProgram.SelectedValue.IndexOf('(')).Trim();

            String strQueryForPrimaryProgramId = "select Id from c4g_Plan__c where Name = '" + strPrimaryProgram + "'";

            SForce.QueryResult qrPrimaryProgramId = Sfdcbinding.query(strQueryForPrimaryProgramId);

            if (qrPrimaryProgramId.size > 0)
            {
                SForce.c4g_Plan__c primaryPlan = qrPrimaryProgramId.records[0] as SForce.c4g_Plan__c;

                ctUpdate.c4g_Plan__c = primaryPlan.Id;

                SForce.SaveResult[] updateResultsPrimary = Sfdcbinding.update(new SForce.sObject[] { ctUpdate });

                if (updateResultsPrimary[0].success)
                {
                    // the primary member's program info added successfully
                }
            }

            String strSpouseProgram = String.Empty;

            if (ddlSpouseProgram.Items.Count != 0)
            {
                strSpouseProgram = ddlSpouseProgram.SelectedValue.Substring(0, ddlSpouseProgram.SelectedValue.IndexOf('(')).Trim();
            }

            String strQueryForSpouseProgramId = "select Id from c4g_Plan__c where Name = '" + strSpouseProgram + "'";

            SForce.QueryResult qrSpouseProgramId = Sfdcbinding.query(strQueryForSpouseProgramId);

            if (qrSpouseProgramId.size > 0)
            {
                SForce.c4g_Plan__c spousePlan = qrSpouseProgramId.records[0] as SForce.c4g_Plan__c;

                SForce.Contact ctSpouseUpdate = new SForce.Contact();
                ctSpouseUpdate.Id          = strSpouseId;
                ctSpouseUpdate.c4g_Plan__c = spousePlan.Id;

                SForce.SaveResult[] updateResultsSpouse = Sfdcbinding.update(new SForce.sObject[] { ctSpouseUpdate });

                if (updateResultsSpouse[0].success)
                {
                    // the spouse's program info is added
                }
            }

            String strChildProgram = String.Empty;

            if (ddlChildrenProgram.Items.Count != 0)
            {
                strChildProgram = ddlChildrenProgram.SelectedValue.Substring(0, ddlChildrenProgram.SelectedValue.IndexOf('(')).Trim();
            }

            String strQueryForChildProgramId = "select Id from c4g_Plan__c where Name = '" + strChildProgram + "'";

            SForce.QueryResult qrChildProgramId = Sfdcbinding.query(strQueryForChildProgramId);

            if (qrChildProgramId.size > 0)
            {
                SForce.c4g_Plan__c childPlan = qrChildProgramId.records[0] as SForce.c4g_Plan__c;

                foreach (String strChildId in lstChildId)
                {
                    SForce.Contact ctChildUpdate = new SForce.Contact();

                    ctChildUpdate.Id          = strChildId;
                    ctChildUpdate.c4g_Plan__c = childPlan.Id;

                    SForce.SaveResult[] updateResultsChild = Sfdcbinding.update(new SForce.sObject[] { ctChildUpdate });

                    if (updateResultsChild[0].success)
                    {
                        // the child's program info is added
                    }
                }
            }

            ///// This section insert church info to membership detail aspx

            String strQueryForHeadOfHouseholdId = "select Id from Contact where cmm_Household__c = '" + strAccountId + "' " + "and cmm_Household_Role__c = 'Head of Household'";

            SForce.QueryResult qrHeadOfHouseholdId = Sfdcbinding.query(strQueryForHeadOfHouseholdId);

            //String strHeadOfHouseholdId = null;

            if (qrHeadOfHouseholdId.size > 0)
            {
                SForce.Contact ctHead = (SForce.Contact)qrHeadOfHouseholdId.records[0];

                String strHeadOfHouseholdId = ctHead.Id;


                SForce.Account acctChurchInfo = new SForce.Account();


                acctChurchInfo.RecordTypeId            = "01237000000R6cjAAC";
                acctChurchInfo.Name                    = txtChurchName.Text;
                acctChurchInfo.Senior_Pastor_s_Name__c = txtSeniorPastor.Text;

                if (txtChurchStreetAddress.Text != String.Empty)
                {
                    acctChurchInfo.ShippingStreet = txtChurchStreetAddress.Text;
                }
                if (txtChurchZipCode.Text != String.Empty)
                {
                    acctChurchInfo.ShippingPostalCode = txtChurchZipCode.Text;
                }
                if (ddlChurchState.Items != null)
                {
                    acctChurchInfo.ShippingState = ddlChurchState.SelectedItem.Text;
                }
                if (ddlChurchCity.Items != null)
                {
                    acctChurchInfo.ShippingCity = ddlChurchCity.SelectedItem.Text;
                }
                if (txtChurchPhone.Text != String.Empty)
                {
                    acctChurchInfo.Phone = txtChurchPhone.Text;
                }

                SForce.SaveResult[] saveAcctResults = Sfdcbinding.create(new SForce.sObject[] { acctChurchInfo });

                if (saveAcctResults[0].success)
                {
                    SForce.Contact ctHeadOfHousehold = new SForce.Contact();

                    ctHeadOfHousehold.Id = strHeadOfHouseholdId;
                    //ctHeadOfHousehold.Referral_Source__c = ddlReferredBy.SelectedValue;
                    ctHeadOfHousehold.c4g_Church__c = saveAcctResults[0].id;

                    SForce.SaveResult[] updateResults = Sfdcbinding.update(new SForce.sObject[] { ctHeadOfHousehold });

                    if (updateResults[0].success)
                    {
                        // notify the user that church information added to household
                    }
                }
            }

            if (ddlReferredBy.SelectedValue != "")
            {
                if (ddlReferredBy.SelectedValue == "Member Referral" && txtMembershipNumber.Text != "" && ddlReferrerName.SelectedValue != "")
                {
                    String strMembershipNumber = String.Empty;

                    if (txtMembershipNumber.Text.Contains("MEMB-"))
                    {
                        strMembershipNumber = txtMembershipNumber.Text;
                    }
                    else
                    {
                        strMembershipNumber = txtMembershipNumber.Text;
                        if (txtMembershipNumber.Text.Length < 7)
                        {
                            for (int i = txtMembershipNumber.Text.Length; i < 7; i++)
                            {
                                strMembershipNumber = '0' + strMembershipNumber;
                            }
                        }
                        strMembershipNumber = "MEMB-" + strMembershipNumber;
                    }

                    String strQueryForContact = "select Id from Contact where c4g_Membership__r.Name = '" + strMembershipNumber + "' and Name = '" + ddlReferrerName.SelectedValue + "'";

                    SForce.QueryResult qrContactForMembership = Sfdcbinding.query(strQueryForContact);

                    if (qrContactForMembership.size > 0)
                    {
                        SForce.Contact       ctReferrer      = qrContactForMembership.records[0] as SForce.Contact;
                        SForce.Membership__c memReferralInfo = new SForce.Membership__c();

                        memReferralInfo.Paying_Member__c = strPrimaryContactId;
                        memReferralInfo.Referred_By__c   = ddlReferredBy.SelectedValue;
                        memReferralInfo.Referrer__c      = ctReferrer.Id;
                        if ((String)Session["Email"] != null)
                        {
                            memReferralInfo.Email__c = (String)Session["Email"];
                        }

                        SForce.SaveResult[] saveResults = Sfdcbinding.create(new SForce.sObject[] { memReferralInfo });

                        if (saveResults[0].success)
                        {
                            // Membership object is created successfully
                            String             strQueryForFamilyMembers = "select Id from Contact where cmm_Household__c = '" + strAccountId + "'";
                            SForce.QueryResult qrFamilyMembers          = Sfdcbinding.query(strQueryForFamilyMembers);

                            if (qrFamilyMembers.size > 0)
                            {
                                for (int i = 0; i < qrFamilyMembers.size; i++)
                                {
                                    SForce.Contact ctFamilyMemberId = qrFamilyMembers.records[i] as SForce.Contact;

                                    SForce.Contact ctFamilyMember = new SForce.Contact();
                                    ctFamilyMember.Id = ctFamilyMemberId.Id;
                                    ctFamilyMember.c4g_Membership__c = saveResults[0].id;

                                    SForce.SaveResult[] updateResults = Sfdcbinding.update(new SForce.sObject[] { ctFamilyMember });
                                    if (updateResults[0].success)
                                    {
                                        // Membership object added to each member of the household
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    SForce.Membership__c memReferralInfo = new SForce.Membership__c();

                    memReferralInfo.Paying_Member__c = strPrimaryContactId;
                    memReferralInfo.Referred_By__c   = ddlReferredBy.SelectedValue;
                    if ((String)Session["Email"] != null)
                    {
                        memReferralInfo.Email__c = (String)Session["Email"];
                    }

                    SForce.SaveResult[] saveResults = Sfdcbinding.create(new SForce.sObject[] { memReferralInfo });

                    if (saveResults[0].success)
                    {
                        // Membership object is created successfully

                        // Membership object is created successfully
                        String             strQueryForFamilyMembers = "select Id from Contact where cmm_Household__c = '" + strAccountId + "'";
                        SForce.QueryResult qrFamilyMembers          = Sfdcbinding.query(strQueryForFamilyMembers);

                        if (qrFamilyMembers.size > 0)
                        {
                            for (int i = 0; i < qrFamilyMembers.size; i++)
                            {
                                SForce.Contact ctFamilyMemberId = qrFamilyMembers.records[i] as SForce.Contact;

                                SForce.Contact ctFamilyMember = new SForce.Contact();
                                ctFamilyMember.Id = ctFamilyMemberId.Id;
                                ctFamilyMember.c4g_Membership__c = saveResults[0].id;

                                SForce.SaveResult[] updateResults = Sfdcbinding.update(new SForce.sObject[] { ctFamilyMember });
                                if (updateResults[0].success)
                                {
                                    // Membership object added to each member of the household
                                }
                            }
                        }
                    }
                }
            }



            SForce.Account acctPrimary = new SForce.Account();
            acctPrimary.Id = strAccountId;
            acctPrimary.cmm_Account_Creation_Step_Code__c = "5";

            SForce.SaveResult[] srAccount = Sfdcbinding.update(new SForce.sObject[] { acctPrimary });

            if (srAccount[0].success)
            {
                Session["PreviousPage"] = "MembershipDetails";
                Response.Redirect("~/PaymentInfo.aspx");
            }
        }
        protected void btnLoad_Click(object sender, EventArgs e)
        {
            //Sfdcbinding = new SForce.SforceService();
            //CurrentLoginResult = Sfdcbinding.login(strUserName, strPasswd);
            //Sfdcbinding.Url = CurrentLoginResult.serverUrl;
            //Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

            //Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
            //strQuerySelectFromContact = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
            //queryResult = Sfdcbinding.query(strQuerySelectFromContact);

            //nTotalRecord = queryResult.size - 1;

            if (queryResult.size > 0)
            {
                contact = (SForce.Contact)queryResult.records[nCurrentIndex];

                txtIndividualId.Text = contact.Individual_ID__c;
                txtId.Text           = contact.Id;
                //SForce.Membership__c member = (SForce.Membership__c) contact.Memberships__r.records[0];

                if (contact.c4g_Membership__r != null)
                {
                    txtMembershipId.Text            = contact.c4g_Membership__r.Id;
                    txtMembershipNameInContact.Text = contact.c4g_Membership__r.Name;
                }
                else
                {
                    txtMembershipId.Text            = "";
                    txtMembershipNameInContact.Text = "";
                }


                //if (contact.c4g_Membership__r != null)
                //{
                //    txtMembershipId.Text = contact.c4g_Membership__r.Id;
                //    txtMembershipInContact.Text = contact.c4g_Membership__r.Name;
                //}
                //else if (contact.c4g_Membership__r == null)
                //{
                //    txtMembershipId.Text = "";
                //    txtMembershipInContact.Text = "";
                //}


                //txtMembershipId.Text = member.Id;
                //txtMembershipNameInContact.Text = member.Name; ;
                txtFirstName.Text = contact.FirstName;
                txtLastName.Text  = contact.LastName;

                if (contact.MailingAddress != null)
                {
                    txtStreetAddress.Text = contact.MailingAddress.street;
                    txtCity.Text          = contact.MailingAddress.city;
                    txtState.Text         = contact.MailingAddress.state;
                    txtZip.Text           = contact.MailingAddress.postalCode;
                }
                else if (contact.MailingAddress == null)
                {
                    txtStreetAddress.Text = "";
                    txtCity.Text          = "";
                    txtState.Text         = "";
                    txtZip.Text           = "";
                }
                txtMobilePhone.Text = contact.MobilePhone;
                txtPhone.Text       = contact.Phone;
                txtEmail.Text       = contact.Email;
            }

            btnFirst.Enabled            = false;
            btnPrev.Enabled             = false;
            btnCreateNewContact.Enabled = true;
            btnEdit.Enabled             = true;
            btnDelete.Enabled           = true;
            lblErrorMessage.Text        = "";

            ////////////////////////////////////////////////////////////////////////////////
            // Testing for select from membership
            SForce.SforceService sfdcbindingMembership = new SForce.SforceService();
            SForce.LoginResult   LoginResult           = sfdcbindingMembership.login(strUserName, strPasswd);
            sfdcbindingMembership.Url = LoginResult.serverUrl;
            sfdcbindingMembership.SessionHeaderValue = new SForce.SessionHeader();

            sfdcbindingMembership.SessionHeaderValue.sessionId = LoginResult.sessionId;
            queryResultMembership = sfdcbindingMembership.query(strQuerySelectFromMembership);


            //strQueryMembershipIdInContact

            /////////////////////////////////////
            // Testing for Membership field in Contact
            SForce.QueryResult queryMembershipInContact = sfdcbindingMembership.query(strQueryMembershipIdInContact);
            //SForce.QueryResult queryContactInnerJoinMembership = sfdcbindingMembership.query(strQueryContactInnerJoinMembership);

            if (queryMembershipInContact.size > 0)
            {
                SForce.Contact contactMembership = (SForce.Contact)queryMembershipInContact.records[0];

                txtMembershipInContact.Text = contactMembership.c4g_Membership__c;
            }

            if (queryResultMembership.size > 0)
            {
                SForce.Membership__c membership = (SForce.Membership__c)queryResultMembership.records[0];
                //SForce.Contact contact = (SForce.Contact)queryResultMembership.records[0];

                txtContactId.Text        = membership.Id;
                txtMembershipName.Text   = membership.Name;
                txtMembershipStatus.Text = membership.c4g_Membership_Status__c;

                //txtContactId.Text = contact.Id;
                //txtMembershipName.Text = contact.Name; ;  //membership.Name;
                //txtMembershipStatus.Text = contact.c4g_Membership_Status__c;   //membership.c4g_Membership_Status__c;
                int nMemberIndex = 0;
                Session["MemberIndex"] = nMemberIndex;
            }

            if (queryResultTask.size > 0)
            {
                SForce.Task task = (SForce.Task)queryResultTask.records[nCurrentIndexTask];

                txtTaskName.Text    = task.Id;
                txtTaskComment.Text = task.Description;

                Session["TaskIndex"] = ++nCurrentIndexTask;

                Session["CurrentTaskId"] = task.Id;
            }
        }