protected void Button1_Click(object sender, EventArgs e)
    {
       try
        {
            ValidateIssurance();
            // new RobotTableAdapters.InsuranceCompanyTableAdapter().Insert();

            RobotTableAdapters.CollateralType_SalaryContractTableAdapter robot = new RobotTableAdapters.CollateralType_SalaryContractTableAdapter();
            RobotTableAdapters.InsuranceTableAdapter robotInsuranceTable = new RobotTableAdapters.InsuranceTableAdapter();
            //initialize values
            int collateralId = Convert.ToInt32(Request.QueryString["collateralId"]);
            int CollateralTypeId = Convert.ToInt32(Request.QueryString["CollateralTypeId"]);


            double varValueOfContract = 0; double varAPG = 0;

            varValueOfContract = Convert.ToDouble(ValueOfContract.Text);
            varAPG = Convert.ToDouble(APG.Text);

            //insert values into CollateralType_Cash  and retrieve max id as collateralDetailsId
            int collateralDetailsId = Convert.ToInt32(robot.proc_insert_contract(NameOfInsuringBody.Text, OriginalContractor.Text, Convert.ToDateTime(ContractAwardDate.Value),
                                        PurposeOfContract.Text, RefNo.Text, varValueOfContract, varAPG, TenureOfContract.Text, Convert.ToDateTime(MaturityDate.Value),
                                        collateralId, Smart.GetEmployeeID(), thirdparty.Text, "NULL",2));

            hiddenDetailsId.Text = collateralDetailsId.ToString();
            //see if he filled the insurance form, then run the block of code that will do the insrt into insurance table
            if (FinInsuranceStatus_id.SelectedValue == "1" || FinInsuranceStatus_id.SelectedValue == "3")
            {
                int varPeriodOfInsuranceCover = 0; double varAmountInsured = 0;
                varPeriodOfInsuranceCover = Convert.ToInt32(FinPeriodofInsuranceCover.Text);
                varAmountInsured = Convert.ToDouble(FinAmountInsured.Text);

                robotInsuranceTable.Insert(Convert.ToInt32(FinInsuranceStatus_id.SelectedValue), Convert.ToInt32(InsuranceCover.SelectedValue), collateralDetailsId, CollateralTypeId,
                    FinInsuranceCompany.Text, Convert.ToDateTime(FinDateOfInsurance.Value), varPeriodOfInsuranceCover, varAmountInsured, FinKeyClause.Text, Smart.GetEmployeeID(), Convert.ToInt32(FinInsuranceCompany.SelectedValue));

            }
            else
            {
                robotInsuranceTable.Insert(Convert.ToInt32(FinInsuranceStatus_id.SelectedValue), null, collateralDetailsId, CollateralTypeId,
                    null, null, null, null, null, Smart.GetEmployeeID(), null);
            }



            //enable and move focus to next tab, disable current tab
            ASPxPageControl1.TabPages[1].Enabled = true;

            ASPxPageControl1.ActiveTabIndex = 1;
        }
        catch (Exception ex)
        {
          _errorMessageSalary.Text = ex.Message;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        String selectedType = Request.QueryString["CollateralTypeID"];
        String collateraldetails_id = Request.QueryString["collateraldetails_id"];
        String collateralId = Request.QueryString["collateralId"];
        String loansID = Request.QueryString["loansID"];
        RobotTableAdapters.CollateralType_SalaryContractTableAdapter rbot = new RobotTableAdapters.CollateralType_SalaryContractTableAdapter();
       


        switch (selectedType)
        {
            case "1":
                  Response.Redirect("smart_collateral2_property.aspx?collateralId="+collateralId+"&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                break;
            case "2": Response.Redirect("smart_collateral2_cash.aspx?collateralId=" + collateralId + "&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                break;
            case "3": Response.Redirect("smart_collateral2_stockHypo.aspx?collateralId=" + collateralId + "&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                break;
            case "4": Response.Redirect("smart_collateral2_car.aspx?collateralId=" + collateralId + "&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                break;
            case "5":
                String bull = rbot.getContractTypeID(Convert.ToInt32(collateralId), Convert.ToInt32(selectedType), Convert.ToInt32(collateraldetails_id)).ToString();
                if (bull == "1")
                {
                    Response.Redirect("smart_collateral2_salary.aspx?collateralId=" + collateralId + "&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                }
                else if (bull == "2")
                {
                    Response.Redirect("smart_collateral2_contract.aspx?collateralId=" + collateralId + "&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                }
                 break;
            case "6": Response.Redirect("smart_collateral2_guaranty.aspx?collateralId=" + collateralId + "&CollateralTypeId=" + selectedType + "&collateraldetails_id=" + collateraldetails_id + "&loansID=" + loansID);//Redirects the user to the aproperiate page 
                 break;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ValidateIssurance();
            //  new RobotTableAdapters.InsuranceCompanyTableAdapter().Insert();
            CheckBoxList1_Load();
            RobotTableAdapters.CollateralType_SalaryContractTableAdapter robot = new RobotTableAdapters.CollateralType_SalaryContractTableAdapter();
            RobotTableAdapters.InsuranceTableAdapter robotInsuranceTable = new RobotTableAdapters.InsuranceTableAdapter();
            //initialize values
            int collateralId = Convert.ToInt32(Request.QueryString["collateralId"]);
            int CollateralTypeId = Convert.ToInt32(Request.QueryString["CollateralTypeId"]);
            String collateraldetails_id = Request.QueryString["collateraldetails_id"];
            int collateraldetailsid = Convert.ToInt32(Request.QueryString["collateraldetails_id"]);

            double varValueOfContract = 0; double varAPG = 0;

            varValueOfContract = Convert.ToDouble(ValueOfContract.Text);
            varAPG = Convert.ToDouble(APG.Text);

            //insert values into CollateralType_Cash  and retrieve max id as collateralDetailsId

            robot.Update(NameOfInsuringBody.Text, OriginalContractor.Text, DateTime.Now,
                                    PurposeOfContract.Text, RefNo.Text, varValueOfContract, varAPG, TenureOfContract.Text, DateTime.Now,
                                    collateralId, Smart.GetEmployeeID(), thirdparty.Text, "NULL", Convert.ToInt32(contratType.SelectedValue), collateraldetailsid);

            hiddenDetailsId.Text = collateraldetailsid.ToString();
            int insureID = Convert.ToInt32(InsuranceID.Value);
            //see if he filled the insurance form, then run the block of code that will do the insrt into insurance table
            int varPeriodOfInsuranceCover = 0; double varAmountInsured = 0;
            varPeriodOfInsuranceCover = Convert.ToInt32(FinPeriodofInsuranceCover.Text);
            varAmountInsured = Convert.ToDouble(FinAmountInsured.Text);
            if (FinInsuranceStatus_id.SelectedValue == "2")
            {
                robotInsuranceTable.Update(Convert.ToInt32(FinInsuranceStatus_id.SelectedValue), null, collateraldetailsid, CollateralTypeId,
                   null, null, null, null, null, Smart.GetEmployeeID(), null, insureID);

            }
            else
            {
                robotInsuranceTable.Update(Convert.ToInt32(FinInsuranceStatus_id.SelectedValue), Convert.ToInt32(InsuranceCover.SelectedValue), collateraldetailsid, CollateralTypeId,
                     FinInsuranceCompany.Text, Convert.ToDateTime(FinDateOfInsurance.Value), Convert.ToInt32(FinPeriodofInsuranceCover.Text), Convert.ToDouble(FinAmountInsured.Text), FinKeyClause.Text, Smart.GetEmployeeID(), Convert.ToInt32(FinInsuranceCompany.SelectedValue), insureID);
            }

            int CollateralDetailsId = Convert.ToInt32(hiddenDetailsId.Text);

            RobotTableAdapters.documents_submittedTableAdapter robotDocs = new RobotTableAdapters.documents_submittedTableAdapter();
            RobotTableAdapters.documents_attachmentTableAdapter robotAttachment = new RobotTableAdapters.documents_attachmentTableAdapter();

            //delete should happen before insert, just incase the guy goes back to check or uncheck documents in the checklist tab

            robotDocs.DeleteExistingCheckedDocs(collateralId, CollateralDetailsId, CollateralTypeId);
            DataTable checkDocs = robotDocs.getSubmitedDocumentsById(collateralId, CollateralDetailsId, CollateralTypeId);

            foreach (DataRow row in checkDocs.Rows)
            {
                foreach (ListItem item in CheckBoxList1.Items)
                {
                    if (row["document_id"].ToString() == item.Value)
                        item.Selected = true;
                }
            }

            //enable and move focus to next tab, disable current tab
            ASPxPageControl1.TabPages[1].Enabled = true;

            //documenttype_id.SelectedIndex = 2;
            ASPxPageControl1.ActiveTabIndex = 1;
        }
        catch (Exception ex)
        {
            _errorMessageSalary.Text = ex.Message;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        String collateraldetails_id = Request.QueryString["collateraldetails_id"];

        if (!IsPostBack)
        {
          //  FinInsuranceStatus_id.SelectedIndex = 1;
           
            ASPxPageControl1.TabPages[0].Enabled = true;
            ASPxPageControl1.TabPages[1].Enabled = true;
            ASPxPageControl1.TabPages[2].Enabled = false;
            ASPxPageControl1.TabPages[3].Enabled = false;
             
            ASPxPageControl1.ActiveTabIndex = 0;
            int collateraldetailsid = Convert.ToInt32(Request.QueryString["collateraldetails_id"]);

            //this code is for population of fields when update mode is set
            RobotTableAdapters.CollateralType_SalaryContractTableAdapter Engine = new RobotTableAdapters.CollateralType_SalaryContractTableAdapter();


            DataTable returnedData = Engine.getSalaryContractById(Convert.ToInt32(collateraldetails_id));

            //TEXTBOXES
            NameOfInsuringBody.Text = returnedData.Rows[0]["NameOfInsuringBody"].ToString();
            thirdparty.Text = returnedData.Rows[0]["thirdparty"].ToString();
           
            DateTime dt;
            dt = Convert.ToDateTime(returnedData.Rows[0]["ContractAwardDate"].ToString());
            ContractAwardDate.Value = ((DateTime)dt).ToString("MM/dd/yyyy");

            RefNo.Text = returnedData.Rows[0]["RefNo"].ToString();
            APG.Text = returnedData.Rows[0]["APG"].ToString();
            OriginalContractor.Text = returnedData.Rows[0]["OriginalContractor"].ToString();
            PurposeOfContract.Text = returnedData.Rows[0]["PurposeOfContract"].ToString();
            ValueOfContract.Text = returnedData.Rows[0]["ValueOfContract"].ToString();
            TenureOfContract.Text = returnedData.Rows[0]["TenorOfContract"].ToString();
            //documenttype_id.SelectedValue = "";//(returnedData.Rows[0]["documenttype_id"].ToString()=="-1") ? "-1" : returnedData.Rows[0]["documenttype_id"].ToString() ;
            //documenttype_id.SelectedValue = returnedData.Rows[0]["documenttype_id"].ToString();
            if(returnedData.Rows[0]["documenttype_id"].ToString() != "-1")
                documenttype_id.SelectedValue = returnedData.Rows[0]["documenttype_id"].ToString();
            //DROPDOWN

            contratType.SelectedValue = returnedData.Rows[0]["contractTypeID"].ToString();


        try
        {

            RobotTableAdapters.InsuranceTableAdapter InsuranceEngine = new RobotTableAdapters.InsuranceTableAdapter();


            DataTable InsuranceData = InsuranceEngine.proc_getInsuranceById(Convert.ToInt32(collateraldetails_id), 5);

            //TEXTBOXES

			 FinInsuranceStatus_id.SelectedValue = InsuranceData.Rows[0]["InsuranceStatus_id"].ToString();
             InsuranceID.Value = InsuranceData.Rows[0]["Insurance_id"].ToString();
			  if (InsuranceData.Rows[0]["InsuranceStatus_id"].ToString() == "1" || InsuranceData.Rows[0]["InsuranceStatus_id"].ToString() == "3")
            {
			
				FinPeriodofInsuranceCover.Text = InsuranceData.Rows[0]["PeriodOfInsuranceCover"].ToString();
				FinKeyClause.Text = InsuranceData.Rows[0]["KeyClause"].ToString();
				FinAmountInsured.Text = InsuranceData.Rows[0]["AmountInsured"].ToString();
           // FinDateOfInsurance.Value = InsuranceData.Rows[0]["DateOfInsurance"].ToString();

				DateTime dt1;
				dt1 = Convert.ToDateTime(InsuranceData.Rows[0]["DateOfInsurance"].ToString());
				FinDateOfInsurance.Value = ((DateTime)dt1).ToString("MM/dd/yyyy");

            //DROPDOWNS

				FinInsuranceCompany.SelectedValue = InsuranceData.Rows[0]["InsuranceCompany"].ToString();
				InsuranceCover.SelectedValue = InsuranceData.Rows[0]["InsuranceCover_id"].ToString();
			}
        }
        catch (Exception)
        {


        }
        }
       
        CheckBoxList1_Load();
    }