protected void grdInsuranceCompaniesCostsByClientProject_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("DataEditInsuranceCompaniesByClientProject");
            if (Page.IsValid)
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int refId = Int32.Parse(((Label)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("lblRefIdEditInsuranceCompaniesByClientProject")).Text);
                DateTime date = (DateTime)((RadDatePicker)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("tkrdpDateEditInsuranceCompaniesByClientProject")).SelectedDate;

                int insuranceCompanyId = Int32.Parse(((DropDownList)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("ddlInsuranceCompaniesEditInsuranceCompaniesByClientProject")).Text);
                string name = ((DropDownList)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("ddlInsuranceCompaniesEditInsuranceCompaniesByClientProject")).SelectedItem.Text;

                int projectId = Int32.Parse(((Label)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("lblProjectIdEditInsuranceCompaniesByClientProject")).Text);

                decimal rate = decimal.Round(decimal.Parse(((TextBox)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("tbxRateEditInsuranceCompaniesByClientProject")).Text), 2);
                decimal rateCad = 0;
                decimal rateUsd = 0;

                ProjectGateway projectGateway = new ProjectGateway();
                projectGateway.LoadByProjectId(projectId);

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    rateCad = rate;
                }
                else
                {
                    rateUsd = rate;
                }

                string comment = ((TextBox)grdInsuranceCompaniesCostsByClientProject.Rows[e.RowIndex].FindControl("tbxCommentEditInsuranceCompaniesByClientProject")).Text;
                bool deleted = false;

                // Update Data
                ActualCostsAddInsuranceCompaniesCosts actualCostsAddInsuranceCompaniesCosts = new ActualCostsAddInsuranceCompaniesCosts(actualCostsAddTDS);
                actualCostsAddInsuranceCompaniesCosts.Update(projectId, refId, insuranceCompanyId, date, rateCad, rateUsd, comment, deleted, companyId, name);

                // Store dataset
                Session.Remove("insuranceCompaniesCostsByClientProjectDummy");

                Session["actualCostsAddTDS"] = actualCostsAddTDS;
                Session["insuranceCompaniesCosts"] = actualCostsAddTDS.InsuranceCompaniesCosts;

                insuranceCompaniesCosts = actualCostsAddTDS.InsuranceCompaniesCosts;
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdInsuranceCompaniesCostsByClientProject_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // InsuranceCompanies Gridview, if the gridview is edition mode
            if (grdInsuranceCompaniesCostsByClientProject.EditIndex >= 0)
            {
                grdInsuranceCompaniesCostsByClientProject.UpdateRow(grdInsuranceCompaniesCostsByClientProject.EditIndex, true);
            }

            // Delete InsuranceCompanies
            int projectId = (int)e.Keys["ProjectID"];
            int refId = (int)e.Keys["RefID"];

            // Delete costs details
            ActualCostsAddInsuranceCompaniesCosts actualCostsAddInsuranceCompaniesCosts = new ActualCostsAddInsuranceCompaniesCosts(actualCostsAddTDS);
            actualCostsAddInsuranceCompaniesCosts.Delete(projectId, refId);

            // Store dataset
            Session.Remove("insuranceCompaniesCostsByClientProjectDummy");

            Session["actualCostsAddTDS"] = actualCostsAddTDS;
            Session["insuranceCompaniesCosts"] = actualCostsAddTDS.InsuranceCompaniesCosts;

            insuranceCompaniesCosts = actualCostsAddTDS.InsuranceCompaniesCosts;
        }
        private void grdInsuranceCompaniesCostsByClientProjectDetailAdd()
        {
            if (FooterValidateInsuranceCompaniesByClientProject())
            {
                Page.Validate("DataNewInsuranceCompaniesByClientProject");
                if (Page.IsValid)
                {
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    DateTime date = (DateTime)((RadDatePicker)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("tkrdpDateFooterInsuranceCompaniesByClientProject")).SelectedDate;

                    int insuranceCompanyId = Int32.Parse(((DropDownList)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("ddlInsuranceCompaniesFooterInsuranceCompaniesByClientProject")).Text);
                    string name = ((DropDownList)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("ddlInsuranceCompaniesFooterInsuranceCompaniesByClientProject")).SelectedItem.Text;

                    int clientId = Int32.Parse(((DropDownList)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("ddlClientFooterInsuranceCompaniesByClientProject")).SelectedValue);
                    string client = ((DropDownList)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("ddlClientFooterInsuranceCompaniesByClientProject")).SelectedItem.Text;

                    int projectId = Int32.Parse(((DropDownList)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("ddlProjectFooterInsuranceCompaniesByClientProject")).SelectedValue);
                    string project = ((DropDownList)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("ddlProjectFooterInsuranceCompaniesByClientProject")).SelectedItem.Text;

                    decimal rate = decimal.Round(decimal.Parse(((TextBox)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("tbxRateFooterInsuranceCompaniesByClientProject")).Text), 2);
                    string comment = ((TextBox)grdInsuranceCompaniesCostsByClientProject.FooterRow.FindControl("tbxCommentFooterInsuranceCompaniesByClientProject")).Text;
                    bool deleted = false;
                    bool inDatabase = false;
                    decimal rateCad = 0;
                    decimal rateUsd = 0;
                    ProjectGateway projectGateway = new ProjectGateway();
                    projectGateway.LoadByProjectId(projectId);

                    if (projectGateway.GetCountryID(projectId) == 1) //Canada
                    {
                        rateCad = rate;
                        rateUsd = rate;
                    }
                    else
                    {
                        rateCad = rate;
                        rateUsd = rate;
                    }

                    // Insert Data
                    ActualCostsAddInsuranceCompaniesCosts actualCostsAddInsuranceCompaniesCosts = new ActualCostsAddInsuranceCompaniesCosts(actualCostsAddTDS);
                    actualCostsAddInsuranceCompaniesCosts.Insert(projectId, insuranceCompanyId, date, rateCad, rateUsd, comment, deleted, companyId, inDatabase, name, client, project, clientId);

                    // Store dataset
                    Session.Remove("insuranceCompaniesCostsByClientProjectDummy");

                    Session["actualCostsAddTDS"] = actualCostsAddTDS;
                    Session["insuranceCompaniesCosts"] = actualCostsAddTDS.InsuranceCompaniesCosts;

                    insuranceCompaniesCosts = actualCostsAddTDS.InsuranceCompaniesCosts;

                    grdInsuranceCompaniesCostsByClientProject.DataBind();
                    grdInsuranceCompaniesCostsByClientProject.PageIndex = grdInsuranceCompaniesCostsByClientProject.PageCount - 1;
                }
            }
        }
        private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int loginId = Convert.ToInt32(Session["loginID"]);

                // Save costs information
                if (cbxSubcontractorCosts.Checked)
                {
                    if (rbtnSubcontractorCostsBySubcontractor.Checked)
                    {
                        ActualCostsAddSubcontractorCosts actualCostsAddSubcontractorCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
                        actualCostsAddSubcontractorCosts.Save(companyId);
                    }
                    else
                    {
                        if (rbtnSubcontractorCostsByProjectAndClient.Checked)
                        {
                            ActualCostsAddSubcontractorCosts actualCostsAddSubcontractorCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
                            actualCostsAddSubcontractorCosts.Save(companyId);
                        }
                    }
                }

                if (cbxHotelCosts.Checked)
                {
                    ActualCostsAddHotelCosts actualCostsAddHotelCosts = new ActualCostsAddHotelCosts(actualCostsAddTDS);
                    actualCostsAddHotelCosts.Save(companyId);
                }

                if (cbxBondingCosts.Checked)
                {
                    ActualCostsAddBondingCompaniesCosts actualCostsAddBondingCompaniesCosts = new ActualCostsAddBondingCompaniesCosts(actualCostsAddTDS);
                    actualCostsAddBondingCompaniesCosts.Save(companyId);
                }

                if (cbxInsuranceCosts.Checked)
                {
                    ActualCostsAddInsuranceCompaniesCosts actualCostsAddInsuranceCompaniesCosts = new ActualCostsAddInsuranceCompaniesCosts(actualCostsAddTDS);
                    actualCostsAddInsuranceCompaniesCosts.Save(companyId);
                }

                if (cbxOtherCosts.Checked)
                {
                    ActualCostsAddOtherCosts actualCostsAddOtherCosts = new ActualCostsAddOtherCosts(actualCostsAddTDS);
                    actualCostsAddOtherCosts.Save(companyId);
                }

                DB.CommitTransaction();

                // Store datasets
                actualCostsAddTDS.AcceptChanges();
                Session["actualCostsAddTDS"] = actualCostsAddTDS;

            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private string GetSummary()
        {
            string summary = "";
            int companyId = Int32.Parse(hdfCompanyId.Value);

            // Summary for Subcontractor costs
            if (cbxSubcontractorCosts.Checked)
            {
                if (rbtnSubcontractorCostsBySubcontractor.Checked)
                {
                    summary += "NEW SUBCONTRACTORS COSTS \n";
                    summary += "SUBCONTRACTOR:  " + ddlSubcontractor.SelectedItem.Text + "\n";

                    ActualCostsAddSubcontractorCosts actualCostsAddSubcontractorCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
                    summary += actualCostsAddSubcontractorCosts.GetSummary(companyId, "\n", "BySubcontractor");
                }
                else
                {
                    if (rbtnSubcontractorCostsByProjectAndClient.Checked)
                    {
                        summary += "NEW SUBCONTRACTORS COSTS \n";
                        summary += "CLIENT:  " + ddlClientByClientProject.SelectedItem.Text + "\n";
                        summary += "PROJECT:  " + ddlProjectByClientProject.SelectedItem.Text + "\n";

                        ActualCostsAddSubcontractorCosts actualCostsAddSubcontractorCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
                        summary += actualCostsAddSubcontractorCosts.GetSummary(companyId, "\n", "ByClientProject");
                    }
                }
            }

            if (cbxHotelCosts.Checked)
            {
                summary += "NEW HOTEL COSTS \n";

                ActualCostsAddHotelCosts actualCostsAddHotelCosts = new ActualCostsAddHotelCosts(actualCostsAddTDS);
                summary += actualCostsAddHotelCosts.GetSummary(companyId, "\n");
            }

            if (cbxBondingCosts.Checked)
            {
                summary += "NEW BONDING COMPANIES COSTS \n";

                ActualCostsAddBondingCompaniesCosts actualCostsAddBondingCompaniesCosts = new ActualCostsAddBondingCompaniesCosts(actualCostsAddTDS);
                summary += actualCostsAddBondingCompaniesCosts.GetSummary(companyId, "\n");
            }

            if (cbxInsuranceCosts.Checked)
            {
                summary += "NEW INSURANCE COMPANIES COSTS \n";

                ActualCostsAddInsuranceCompaniesCosts actualCostsAddInsuranceCompaniesCosts = new ActualCostsAddInsuranceCompaniesCosts(actualCostsAddTDS);
                summary += actualCostsAddInsuranceCompaniesCosts.GetSummary(companyId, "\n");
            }

            if (cbxOtherCosts.Checked)
            {
                summary += "NEW OTHER COSTS \n";

                ActualCostsAddOtherCosts actualCostsAddOtherCosts = new ActualCostsAddOtherCosts(actualCostsAddTDS);
                summary += actualCostsAddOtherCosts.GetSummary(companyId, "\n");
            }

            return summary;
        }