protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Validate("AddEdit");
            if (IsValid)
            {
                if (calDateOfBirth.SelectedDate > DateTime.Today)
                {
                    DynamicControlBinding.SetLabelTextWithCssClass(
                        lblMessageAddEdit,
                        "Invalid date of birth",
                        LabelStyleNames.ErrorMessage);
                    return;
                }

                if (ddlBillingType.SelectedItem.Text.ToUpper() == "AUTO PAYMENT")
                {
                    cuvCreditCardNo.Validate();
                    if (cuvCreditCardNo.IsValid)
                    {
                        if (ddlBillingCardType.SelectedIndex == 0 ||
                            ddlBillingBank.SelectedIndex == 0 ||
                            String.IsNullOrEmpty(txtBillingCardHolderID.Text) ||
                            String.IsNullOrEmpty(txtBillingCardHolderName.Text) ||
                            String.IsNullOrEmpty(txtBillingCardNo.Text))
                        {
                            DynamicControlBinding.SetLabelTextWithCssClass(
                                lblMessageAddEdit,
                                "Auto payment information is incomplete",
                                LabelStyleNames.ErrorMessage);
                            return;
                        }
                    }

                    if (txtDuesAmount.Text.ToDefaultNumber <decimal>() == 0 ||
                        ddlMonthlyDuesItem.SelectedValue.ToDefaultNumber <int>() == 0)
                    {
                        DynamicControlBinding.SetLabelTextWithCssClass(
                            lblMessageAddEdit,
                            "Both <b>Dues Amount</b> and <b>Monthly Dues Item</b> fields must be specified if billing type is manual payment",
                            LabelStyleNames.ErrorMessage);
                        return;
                    }
                }

                if (chkGenerateNewBarcodeCustomer.Checked)
                {
                    if (chkFather.Checked && (
                            String.IsNullOrEmpty(txtFatherName.Text)))
                    {
                        DynamicControlBinding.SetLabelTextWithCssClass(
                            lblStatusParent,
                            "Parent Information (Father) is incomplete",
                            LabelStyleNames.ErrorMessage);
                        return;
                    }

                    if (chkMother.Checked && (
                            String.IsNullOrEmpty(txtMotherName.Text)))
                    {
                        DynamicControlBinding.SetLabelTextWithCssClass(
                            lblStatusParent,
                            "Parent Information (Mother) is incomplete",
                            LabelStyleNames.ErrorMessage);
                        return;
                    }
                }


                switch (RowID)
                {
                case 0:
                    contractProvider.Add(
                        calDate.SelectedDate,
                        !chkGenerateNewBarcodeCustomer.Checked,
                        txtCustomerBarcode.Text,
                        chkIsTransfer.Checked,
                        txtCustomerFirstName.Text,
                        txtCustomerLastName.Text,
                        calDateOfBirth.SelectedDate,
                        Convert.ToInt32(ddlFindBranch.SelectedValue),
                        Convert.ToInt32(ddlPackage.SelectedValue),
                        null,
                        calEffectiveDate.SelectedDate,
                        Convert.ToInt32(ddlBillingType.SelectedValue),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : Convert.ToInt32(ddlBillingCardType.SelectedValue),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : Convert.ToInt32(ddlBillingBank.SelectedValue),
                        txtBillingCardNo.Text,
                        txtBillingCardHolderName.Text,
                        txtBillingCardHolderID.Text,
                        new DateTime(Convert.ToInt32(ddlCardExpiredYear.SelectedValue),
                                     Convert.ToInt32(ddlCardExpiredMonth.SelectedValue),
                                     DateTime.DaysInMonth(Convert.ToInt32(ddlCardExpiredYear.SelectedValue),
                                                          Convert.ToInt32(ddlCardExpiredMonth.SelectedValue))),
                        'P',
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1  ? 0 : Convert.ToInt32(ddlMonthlyDuesItem.SelectedValue),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : txtDuesAmount.Text.ToDefaultNumber <decimal>(),
                        calNextDuesDate.SelectedDate,
                        calExpiredDate.SelectedDate,
                        txtHomePhone.Text,
                        txtCellPhone.Text,
                        txtMailingAddress.Text,
                        txtMailingZipCode.Text,
                        txtAddress.Text,
                        txtZipCode.Text,
                        Convert.ToInt32(ddlArea.SelectedValue),
                        txtSchoolID.Text.ToDefaultNumber <int>(),
                        chkFather.Checked,
                        txtFatherName.Text,
                        txtIDCardNoFather.Text,
                        chkFatherBirthDateUnknown.Checked ? (DateTime?)null : calBirthDateFather.SelectedDate,
                        txtFatherPhone.Text,
                        txtFatherEmail.Text,
                        chkMother.Checked,
                        txtMotherName.Text,
                        txtIDCardNoMother.Text,
                        chkMotherBirthDateUnknown.Checked ? (DateTime?)null : calBirthDateMother.SelectedDate,
                        txtMotherPhone.Text,
                        txtMotherEmail.Text,
                        txtNotes.Text,
                        ddlRenewalOrUpgrade.SelectedValue);
                    break;

                default:
                    contractProvider.Update(
                        RowID,
                        Convert.ToInt32(ddlPackage.SelectedValue),
                        calDate.SelectedDate,
                        calEffectiveDate.SelectedDate,
                        Convert.ToInt32(ddlBillingType.SelectedValue),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : Convert.ToInt32(ddlBillingCardType.SelectedValue),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : Convert.ToInt32(ddlBillingBank.SelectedValue),
                        txtBillingCardNo.Text,
                        txtBillingCardHolderName.Text,
                        txtBillingCardHolderID.Text,
                        new DateTime(Convert.ToInt32(ddlCardExpiredYear.SelectedValue),
                                     Convert.ToInt32(ddlCardExpiredMonth.SelectedValue),
                                     DateTime.DaysInMonth(Convert.ToInt32(ddlCardExpiredYear.SelectedValue),
                                                          Convert.ToInt32(ddlCardExpiredMonth.SelectedValue))),
                        txtHomePhone.Text,
                        txtCellPhone.Text,
                        txtMailingAddress.Text,
                        txtMailingZipCode.Text,
                        txtAddress.Text,
                        txtZipCode.Text,
                        Convert.ToInt32(ddlArea.SelectedValue),
                        txtSchoolID.Text.ToDefaultNumber <int>(),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : Convert.ToInt32(ddlMonthlyDuesItem.SelectedValue),
                        Convert.ToInt32(ddlBillingType.SelectedValue) == 1 ? 0 : txtDuesAmount.Text.ToDefaultNumber <decimal>(),
                        calNextDuesDate.SelectedDate,
                        calExpiredDate.SelectedDate,
                        chkFather.Checked,
                        txtFatherName.Text,
                        txtIDCardNoFather.Text,
                        chkFatherBirthDateUnknown.Checked ? (DateTime?)null : calBirthDateFather.SelectedDate,
                        txtFatherPhone.Text,
                        txtFatherEmail.Text,
                        chkMother.Checked,
                        txtMotherName.Text,
                        txtIDCardNoMother.Text,
                        chkMotherBirthDateUnknown.Checked ? (DateTime?)null : calBirthDateMother.SelectedDate,
                        txtMotherPhone.Text,
                        txtMotherEmail.Text,
                        txtNotes.Text);
                    break;
                }
                Refresh();
            }
        }
        catch (Exception ex)
        {
            WebFormHelper.SetLabelTextWithCssClass(lblMessage, ex.Message, LabelStyleNames.ErrorMessage);
        }
    }