Imports CAMS_ADMIN
Imports CAMS_BL
Imports System.Data
Imports System.Text
Imports System.Globalization
Imports System.IO


Public Class frmCase
    Inherits CAMSBasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object
    Protected WithEvents txtCaseId As System.Web.UI.WebControls.TextBox
    Protected WithEvents ddlStatus As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlReason As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtStage As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtPostcode As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtPMFirst As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtPMAmended As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtPMComp As System.Web.UI.WebControls.TextBox
    Protected WithEvents rbReminderOn As System.Web.UI.WebControls.RadioButton
    Protected WithEvents rbReminderOff As System.Web.UI.WebControls.RadioButton
    Protected WithEvents lblStatusIndicator As System.Web.UI.WebControls.Label
    Protected WithEvents btnAppl As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents txtApplNIN As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtApplAddress As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtApplSurname As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtApplName As System.Web.UI.WebControls.TextBox
    Protected WithEvents hdEvent As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdAction As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdSectionId As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdTemp As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents dvReminders As System.Web.UI.HtmlControls.HtmlGenericControl
    Protected WithEvents btnCancel As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents btnDelete As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents btnSave As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents pnlVisitOnGoing As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlArchitect As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlContractorEstimates As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlClientDocumentation As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlFormalApplication As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlSL21 As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlWorkCompleted As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlComments As System.Web.UI.WebControls.Panel
    Protected WithEvents txtVisit_VisitTarget As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtVisit_RefSch As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtVisit_ClientContact As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtVisit_ClientVisited As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtVisit_AmendedSch As System.Web.UI.WebControls.TextBox
    Protected WithEvents ddlVisit_HA As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlVisit_GT As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtVisit_RefNum As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkVisit_Sim As System.Web.UI.WebControls.CheckBox
    Protected WithEvents ddlVisit_GT_Sim As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtVisit_RefNum_Sim As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgVisit_RefSch_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_RefSch_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_ClientContact_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_ClientContact_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_ClientVisited_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_ClientVisited_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_AmendedSch_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgVisit_AmendedSch_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents ddlArch_Arch As System.Web.UI.WebControls.DropDownList
    Protected WithEvents btnArch_Arch As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents txtArch_SPReq As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtArch_SPRec As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtArch_WDReq As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtArch_WDRec As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkArch_BCPR As System.Web.UI.WebControls.CheckBox
    Protected WithEvents imgArch_SPReq_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_SPReq_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_SPRec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_SPRec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_WDReq_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_WDReq_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_WDRec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgArch_WDRec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents ddlEst_Contractor As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtEst_Req As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtEst_Rec As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgEst_Req_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgEst_Req_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgEst_Rec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgEst_Rec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents btnEst_Add As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents btnEst_Upd As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents btnEst_Rem As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents btnEst_Asn As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents chkDR_ProofTitle As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtDR_ProofReq As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDR_ProofRec As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgDR_ProofReq_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_ProofReq_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_ProofRec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_ProofRec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents chkDR_LndCons As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtDR_LndConsReq As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDR_LndConsRec As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkDR_MortCons As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtDR_MortConsReq As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDR_MortConsRec As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgDR_LndConsReq_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_LndConsReq_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_LndConsRec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_LndConsRec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_MortConsReq_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_MortConsReq_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_MortConsRec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgDR_MortConsRec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents txtFA_TargetApp As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_AppDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_ApprovalDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_RevSch As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgFA_AppDate_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_AppDate_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_ApprovalDate_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_ApprovalDate_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_RevSch_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_RevSch_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents txtFA_TargetIssue As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkFA_A As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkFA_B As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkFA_C As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtFA_SL20Req As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_SL20Rec As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgFA_SL20Req_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_SL20Req_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_SL20Rec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_SL20Rec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents rbFA_Fold As System.Web.UI.WebControls.RadioButton
    Protected WithEvents rbFA_Other As System.Web.UI.WebControls.RadioButton
    Protected WithEvents txtFA_Other As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_PortionAmount As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_FoldInterest As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFA_Issued As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgFA_Issued_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgFA_Issued_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents ddlSL21_Contractor As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlSL21_TA As System.Web.UI.WebControls.DropDownList
    Protected WithEvents btnSL21_Contractor As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents txtSL21_ConfAssig As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtSL21_OnSite As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgSL21_ConfAssig_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgSL21_ConfAssig_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgSL21_OnSite_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgSL21_OnSite_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents txtWC_Target As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkWC_FinalInvoice As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkWC_BuildingControl As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkWC_Niceic As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkWC_CommCard As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkWC_HR7 As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkWC_CompCard As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkWC_Other As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtWC_Other As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_CompOnSite As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_CompPaid As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkWC_SnagList As System.Web.UI.WebControls.CheckBox
    Protected WithEvents imgWC_CompOnSite_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgWC_CompOnSite_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgWC_CompPaid_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgWC_CompPaid_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents txtWC_GrantType As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_OverallCost As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_Grant As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_SGrantType As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_SOverallCost As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_SGrant As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_TotalOverallCost As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_TotalGrant As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_PortionAmount As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_DateRec As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtWC_FoldInterest As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgWC_DateRec_Cal As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents imgWC_DateRec_Era As System.Web.UI.HtmlControls.HtmlImage
    Protected WithEvents txtComm_NewComm As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtComm_PrevComm As System.Web.UI.WebControls.TextBox
    Protected WithEvents dgCoEstimates As System.Web.UI.WebControls.DataGrid
    Protected WithEvents hdCEItemID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdRowId As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdStatus As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents dvFA_FO As System.Web.UI.HtmlControls.HtmlGenericControl
    Protected WithEvents hdFA_RevSch As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdAccess As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdFA_Issued As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdAccessGroup As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents btnEst_Contractor As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents hdbtnOpenContratorID As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents dlReminders As System.Web.UI.WebControls.DataList
    Protected WithEvents ddlVisit_GO As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lblSbCP As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbFI As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbOC As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbG As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbSOC As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbSG As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbTOC As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbTG As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbCPA As System.Web.UI.WebControls.Label
    Protected WithEvents lblSbFIn As System.Web.UI.WebControls.Label
    Protected WithEvents hdVisit_AmendedSch As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdVisit_GOPrefix As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdWC_CompPaid As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents mnuCams As Menu

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private myDTFI As New DateTimeFormatInfo
    Private strDateFormat As String = "dd/MM/yyyy"

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myDTFI.ShortDatePattern = strDateFormat

        'Access Validation to page
        If Not Me.IsPostBack Then
            Dim oEC_User As EC_User = Me.GetUserFromSession

            If oEC_User Is Nothing Then
                Me.RedirectToLogin(Constants.CONST_ERROR_SESSION_EXPIRED)
                Me.Response.End()
            End If

            If Me.HasAccessTo(Me.GetControlID("frmCase.aspx"), oEC_User.AccessGroup) = False Then
                Me.RedirectToMsgPage(Constants.CONST_ERROR_ACCESSDENIED_TOPAGE)
            End If

            'Define action
            If Not Request.QueryString("action") Is Nothing Then
                hdAction.Value = Request.QueryString("action").Trim.ToUpper  'Update
            Else
                hdAction.Value = "I" 'Insert
            End If
            'end

            If oEC_User.AccessGroup = Constants.CONST_ACCESSGROUP_TA And hdAction.Value = "I" Then
                Me.RedirectToMsgPage(Constants.CONST_ERROR_ACCESSDENIED_TOPAGE)
            End If

            'Initial values
            LoadControls()
            DefaultControlValue(oEC_User)
            ValidateAccessToControls(oEC_User)
            'End Initial values

        End If
        'End Access Validation

        'Menu
        mnuCams.CurrentGroup = Constants.MENU_NEW_ID
        mnuCams.CurrentItem = Constants.MENU_NEW_CASE_ID
        'End Menu

        'attributes for controls
        txtApplNIN.Attributes.Add("onkeypress", "javascript:if(event.keyCode==13)fnBringClient();")
        chkVisit_Sim.Attributes.Add("onclick", "fnChkVisit_Sim_Checked()")
        chkFA_B.Attributes.Add("onclick", "fnchkFA_B_Checked()")
        rbFA_Fold.Attributes.Add("onclick", "fnMoneyAssignedTo()")
        rbFA_Other.Attributes.Add("onclick", "fnMoneyAssignedTo()")
        ddlVisit_GT.Attributes.Add("onchange", "fnGrantTypeOnChange()")
        ddlVisit_GT_Sim.Attributes.Add("onchange", "fnSGrantTypeOnChange()")
        txtFA_PortionAmount.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decOK')")
        txtFA_FoldInterest.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decOK')")
        txtFA_PortionAmount.Attributes.Add("onblur", "fnFA_PortionAmount_onBlur()")
        txtFA_FoldInterest.Attributes.Add("onblur", "fnFA_FoldInterest_onBlur()")
        chkWC_Other.Attributes.Add("onclick", "fnFAChkWC_Other_checked()")
        txtWC_OverallCost.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decOK')")
        txtWC_SOverallCost.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decOK')")
        txtWC_Grant.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decOK')")
        txtWC_SGrant.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decOK')")
        txtWC_OverallCost.Attributes.Add("onblur", "fnCalculateTOverallCost()")
        txtWC_SOverallCost.Attributes.Add("onblur", "fnCalculateTOverallCost()")
        txtWC_Grant.Attributes.Add("onblur", "fnCalculateTGrantCost()")
        txtWC_SGrant.Attributes.Add("onblur", "fnCalculateTGrantCost()")
        'end attributes

        'Postback
        If IsPostBack Then
            If hdEvent.Value = "save" Then
                Save()
            ElseIf hdEvent.Value = "selrow" Then
                SelectRow(Convert.ToInt32(hdRowId.Value))
                ShowCoEstimate(Convert.ToInt32(hdRowId.Value))
            ElseIf hdEvent.Value = "updcontrol_ddlArch_Arch" Then
                Update_ddlArch_Arch()
            ElseIf hdEvent.Value = "updcontrol_SL21_Contractor" Then
                Update_ddl_Contractor("SL21")
            ElseIf hdEvent.Value = "updcontrol_EST_Contractor" Then
                Update_ddl_Contractor("ESTIMATES")
            ElseIf hdEvent.Value = "onest_add" Then
                AddCoEstimate()
            ElseIf hdEvent.Value = "onest_upd" Then
                UpdateCoEstimate()
            ElseIf hdEvent.Value = "onest_rem" Then
                DeleteCoEstimate()
            ElseIf hdEvent.Value = "onest_asn" Then
                AssignCoEstimate()
            ElseIf hdEvent.Value = "bring_client" Then
                BringClient(txtApplNIN.Text.Trim)
            ElseIf hdEvent.Value = "updcontrol_clients" Then
                BringClient(hdTemp.Value.Trim)
            ElseIf hdEvent.Value = "cancel" Then
                SetCancel()
            End If
        Else
            If hdAction.Value = "U" Then
                LoadCase()
            End If

        End If
        'End postback

        'Added by Raul Callupe
        If Me.IsPostBack = False Then
            Dim stbOutput As New StringBuilder
            stbOutput.Append("<script language='Javascript'>")
            stbOutput.Append("var pag = window.parent.frames(0);")
            If hdAction.Value = "I" Then
                stbOutput.Append("pag.document.all.lblPath.innerHTML='New/Case'")
            Else
                stbOutput.Append("pag.document.all.lblPath.innerHTML='View/Edit Case'")
            End If
            stbOutput.Append("</script>")
            Response.Write(stbOutput.ToString)
        End If
        'End Added


    End Sub

#Region "Loading Controls"
    Private Sub Update_ddlArch_Arch()
        Dim oCC_Case As New CC_Case
        Dim dsData As DataSet

        Try
            dsData = oCC_Case.GetDataForControls

            If Not dsData Is Nothing Then
                Util.FillDropDownList(dsData.Tables("ARCHITECTS").DefaultView, ddlArch_Arch, "PROFESSIONALID", "FULLNAME", True, New ListItem("", ""))
                Util.SelectValueFromDropDown(ddlArch_Arch, hdTemp.Value)
            End If
        Catch
        Finally
            hdEvent.Value = String.Empty
            hdTemp.Value = String.Empty
        End Try

    End Sub

    Private Sub Update_ddl_Contractor(ByVal strSection As String)
        Dim oCC_Case As New CC_Case
        Dim dsData As DataSet

        Try
            dsData = oCC_Case.GetDataForControls

            If Not dsData Is Nothing Then
                If strSection = "SL21" Then
                    Util.FillDropDownList(dsData.Tables("CONTRACTORS").DefaultView, ddlSL21_Contractor, "CONTRACTORID", "COMPANYNAME", True, New ListItem("", ""))
                    Util.SelectValueFromDropDown(ddlSL21_Contractor, hdTemp.Value)
                ElseIf strSection = "ESTIMATES" Then
                    Util.FillDropDownList(dsData.Tables("CONTRACTORS").DefaultView, ddlEst_Contractor, "CONTRACTORID", "COMPANYNAME", True, New ListItem("", ""))
                    Util.SelectValueFromDropDown(ddlEst_Contractor, hdTemp.Value)
                End If

            End If
        Catch
        Finally
            hdEvent.Value = String.Empty
            hdTemp.Value = String.Empty
        End Try

    End Sub

    Private Sub LoadControls()
        Dim oCC_Case As New CC_Case
        Dim dsData As DataSet

        Try
            If hdAction.Value.Trim.Equals("I") Then
                InitialValuesCoEstimates()
            End If

            dsData = oCC_Case.GetDataForControls

            If Not dsData Is Nothing Then
                Util.FillDropDownList(dsData.Tables("STATUS").DefaultView, ddlStatus, "IDSEQUENCE", "STATUS", False)
                Util.FillDropDownList(dsData.Tables("REASONS").DefaultView, ddlReason, "IDSEQUENCE", "DESCRIPTION", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("HA").DefaultView, ddlVisit_HA, "USERID", "FULLNAME", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("GRANTTYPES").DefaultView, ddlVisit_GT, "GRANTTYPEID", "GRANTTYPEID", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("GRANTTYPES").DefaultView, ddlVisit_GT_Sim, "GRANTTYPEID", "GRANTTYPEID", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("ARCHITECTS").DefaultView, ddlArch_Arch, "PROFESSIONALID", "FULLNAME", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("CONTRACTORS").DefaultView, ddlEst_Contractor, "CONTRACTORID", "COMPANYNAME", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("CONTRACTORS").DefaultView, ddlSL21_Contractor, "CONTRACTORID", "COMPANYNAME", True, New ListItem("", ""))
                Util.FillDropDownList(dsData.Tables("TA").DefaultView, ddlSL21_TA, "USERID", "FULLNAME", True, New ListItem("", ""))
            End If

        Catch

        End Try

    End Sub

    Private Sub DefaultControlValue(ByVal oEC_User As EC_User)

        hdAccessGroup.Value = oEC_User.AccessGroup

        If ddlStatus.Items.Count > 0 Then
            Util.SelectValueFromDropDown(ddlStatus, "N") 'normal
        Else
            ddlStatus.Items.Add(New ListItem("Not Available", ""))
        End If

        If ddlReason.Items.Count = 0 Then ddlReason.Items.Add(New ListItem("Not Available", ""))

        If ddlVisit_HA.Items.Count > 0 Then
            If oEC_User.AccessGroup = Constants.CONST_ACCESSGROUP_HA Then
                Util.SelectValueFromDropDown(ddlVisit_HA, oEC_User.UserID)
                ddlVisit_HA_SelectedIndexChanged(Nothing, Nothing)
            End If
        Else
            ddlVisit_HA.Items.Add(New ListItem("Not Available", ""))
        End If

        If ddlVisit_GT.Items.Count = 0 Then ddlVisit_GT.Items.Add(New ListItem("Not Available", ""))

        If ddlVisit_GO.Items.Count = 0 Then ddlVisit_GO.Items.Add(New ListItem("", ""))

        If ddlVisit_GT_Sim.Items.Count = 0 Then ddlVisit_GT_Sim.Items.Add(New ListItem("Not Available", ""))

        If ddlArch_Arch.Items.Count = 0 Then ddlArch_Arch.Items.Add(New ListItem("Not Available", ""))

        If ddlEst_Contractor.Items.Count = 0 Then ddlEst_Contractor.Items.Add(New ListItem("Not Available", ""))

        If ddlSL21_Contractor.Items.Count = 0 Then ddlSL21_Contractor.Items.Add(New ListItem("Not Available", ""))

        If ddlSL21_TA.Items.Count = 0 Then ddlSL21_TA.Items.Add(New ListItem("Not Available", ""))

        txtVisit_RefNum.Enabled = False
        ddlVisit_GT_Sim.Enabled = False
        txtVisit_RefNum_Sim.Enabled = False
        rbFA_Fold.Checked = False
        rbFA_Other.Checked = False
        dvFA_FO.Disabled = True
        txtFA_Other.Enabled = False

        Me.lblSbCP.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbOC.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbSOC.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbG.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbSG.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbFI.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbFIn.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbTG.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbTOC.Text = ConfigurationSettings.AppSettings("Symbol")
        Me.lblSbCPA.Text = ConfigurationSettings.AppSettings("Symbol")

    End Sub

    Private Sub ManageMainSection(ByVal booActive As Boolean)
        ddlStatus.Enabled = booActive
        ddlReason.Enabled = booActive
        txtApplNIN.ReadOnly = Not booActive
        btnAppl.Disabled = Not booActive
    End Sub

    Private Sub ManageSectionVisitOG(ByVal booActive As Boolean)
        imgVisit_RefSch_Cal.Visible = booActive
        imgVisit_RefSch_Era.Visible = booActive
        imgVisit_ClientContact_Cal.Visible = booActive
        imgVisit_ClientContact_Era.Visible = booActive
        imgVisit_ClientVisited_Cal.Visible = booActive
        imgVisit_ClientVisited_Era.Visible = booActive
        imgVisit_AmendedSch_Cal.Visible = booActive
        imgVisit_AmendedSch_Era.Visible = booActive
        ddlVisit_HA.Enabled = booActive
        ddlVisit_GO.Enabled = booActive
        ddlVisit_GT.Enabled = booActive
        ddlVisit_GT_Sim.Enabled = booActive
        txtVisit_RefNum.Enabled = booActive
        txtVisit_RefNum_Sim.Enabled = booActive
        chkVisit_Sim.Enabled = booActive

        Dim oUser As EC_User = Me.GetUserFromSession

        If Not oUser Is Nothing Then
            If oUser.AccessGroup = Constants.CONST_ACCESSGROUP_HA Then
                ddlVisit_HA.Enabled = False
            End If

        End If
    End Sub

    Private Sub ManageSectionArchitect(ByVal booActive As Boolean)
        ddlArch_Arch.Enabled = booActive
        btnArch_Arch.Disabled = Not booActive
        imgArch_SPRec_Cal.Visible = booActive
        imgArch_SPRec_Era.Visible = booActive
        imgArch_SPReq_Cal.Visible = booActive
        imgArch_SPReq_Era.Visible = booActive
        imgArch_WDRec_Cal.Visible = booActive
        imgArch_WDRec_Era.Visible = booActive
        imgArch_WDReq_Cal.Visible = booActive
        imgArch_WDReq_Era.Visible = booActive
        chkArch_BCPR.Enabled = booActive
    End Sub

    Private Sub ManageSectionCoEstimates(ByVal booActive As Boolean)
        ddlEst_Contractor.Enabled = booActive
        btnEst_Contractor.Disabled = Not booActive
        imgEst_Rec_Cal.Visible = booActive
        imgEst_Rec_Era.Visible = booActive
        imgEst_Req_Cal.Visible = booActive
        imgEst_Req_Era.Visible = booActive
        btnEst_Add.Disabled = Not booActive
        btnEst_Upd.Disabled = Not booActive
        btnEst_Rem.Disabled = Not booActive
        btnEst_Asn.Disabled = Not booActive
    End Sub

    Private Sub ManageSectionClientDocumentation(ByVal booActive As Boolean)
        chkDR_LndCons.Enabled = booActive
        chkDR_MortCons.Enabled = booActive
        chkDR_ProofTitle.Enabled = booActive
        imgDR_LndConsRec_Cal.Visible = booActive
        imgDR_LndConsRec_Era.Visible = booActive
        imgDR_LndConsReq_Cal.Visible = booActive
        imgDR_LndConsReq_Era.Visible = booActive
        imgDR_MortConsRec_Cal.Visible = booActive
        imgDR_MortConsRec_Era.Visible = booActive
        imgDR_MortConsReq_Cal.Visible = booActive
        imgDR_MortConsReq_Era.Visible = booActive
        imgDR_ProofRec_Cal.Visible = booActive
        imgDR_ProofRec_Era.Visible = booActive
        imgDR_ProofReq_Cal.Visible = booActive
        imgDR_ProofReq_Era.Visible = booActive
    End Sub

    Private Sub ManageSectionFormalApplication(ByVal booActive As Boolean, Optional ByVal validateAccessGroup As Boolean = True)
        imgFA_AppDate_Cal.Visible = booActive
        imgFA_AppDate_Era.Visible = booActive
        imgFA_ApprovalDate_Cal.Visible = booActive
        imgFA_ApprovalDate_Era.Visible = booActive
        imgFA_RevSch_Cal.Visible = booActive
        imgFA_RevSch_Era.Visible = booActive
        imgFA_SL20Rec_Cal.Visible = booActive
        imgFA_SL20Rec_Era.Visible = booActive
        imgFA_SL20Req_Cal.Visible = booActive
        imgFA_SL20Req_Era.Visible = booActive
        chkFA_A.Enabled = booActive
        chkFA_B.Enabled = booActive
        chkFA_C.Enabled = booActive
        rbFA_Fold.Enabled = booActive
        rbFA_Other.Enabled = booActive
        txtFA_Other.ReadOnly = Not booActive
        txtFA_PortionAmount.ReadOnly = Not booActive
        txtFA_FoldInterest.ReadOnly = Not booActive
        imgFA_Issued_Cal.Visible = booActive
        imgFA_Issued_Era.Visible = booActive

        If validateAccessGroup Then
            Dim oUser As EC_User = Me.GetUserFromSession

            If Not oUser Is Nothing Then
                If oUser.AccessGroup = Constants.CONST_ACCESSGROUP_TA Then
                    imgFA_Issued_Cal.Visible = True
                    imgFA_Issued_Era.Visible = True
                End If

            End If
        End If


    End Sub

    Private Sub ManageSectionSL21(ByVal booActive As Boolean, Optional ByVal validateAccessGroup As Boolean = True)
        ddlSL21_TA.Enabled = booActive
        ddlSL21_Contractor.Enabled = booActive
        btnSL21_Contractor.Disabled = Not booActive
        imgSL21_ConfAssig_Cal.Visible = booActive
        imgSL21_ConfAssig_Era.Visible = booActive
        imgSL21_OnSite_Cal.Visible = booActive
        imgSL21_OnSite_Era.Visible = booActive

        If validateAccessGroup Then
            Dim oUser As EC_User = Me.GetUserFromSession

            If Not oUser Is Nothing Then
                If oUser.AccessGroup = Constants.CONST_ACCESSGROUP_HA Then
                    ddlSL21_TA.Enabled = True
                ElseIf oUser.AccessGroup = Constants.CONST_ACCESSGROUP_TA Then
                    ddlSL21_TA.Enabled = False
                End If

            End If
        End If

    End Sub

    Private Sub ManageSectionWorkCompleted(ByVal booActive As Boolean)
        chkWC_BuildingControl.Enabled = booActive
        chkWC_CommCard.Enabled = booActive
        chkWC_CompCard.Enabled = booActive
        chkWC_FinalInvoice.Enabled = booActive
        chkWC_HR7.Enabled = booActive
        chkWC_Niceic.Enabled = booActive
        chkWC_Other.Enabled = booActive
        chkWC_SnagList.Enabled = booActive
        txtWC_Other.ReadOnly = Not booActive
        imgWC_CompOnSite_Cal.Visible = booActive
        imgWC_CompOnSite_Era.Visible = booActive
        imgWC_CompPaid_Cal.Visible = booActive
        imgWC_CompPaid_Era.Visible = booActive
        imgWC_DateRec_Cal.Visible = booActive
        imgWC_DateRec_Era.Visible = booActive
        txtWC_OverallCost.ReadOnly = Not booActive
        txtWC_Grant.ReadOnly = Not booActive
        txtWC_SOverallCost.ReadOnly = Not booActive
        txtWC_SGrant.ReadOnly = Not booActive
    End Sub

    Private Sub ManageSectionComments(ByVal booActive As Boolean)
        txtComm_NewComm.Enabled = booActive
    End Sub

    Private Sub ManageControlButtons(ByVal booActive As Boolean)
        btnDelete.Visible = booActive
        btnSave.Visible = booActive
    End Sub

    Private Sub ValidateAccessToControls(ByVal oEC_User As EC_User)
        Dim strAccessIDs As String = ""

        If Me.HasAccessTo(Me.GetControlID(dvReminders.ClientID), oEC_User.AccessGroup) = False Then
            rbReminderOn.Enabled = False
            rbReminderOff.Enabled = False
        End If

        'Access to Tabs
        If Me.HasAccessTo(Me.GetControlID(pnlVisitOnGoing.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionVisitOG(False)
            strAccessIDs &= "0"
        Else
            ManageSectionVisitOG(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlArchitect.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionArchitect(False)
            strAccessIDs &= "0"
        Else
            ManageSectionArchitect(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlContractorEstimates.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionCoEstimates(False)
            strAccessIDs &= "0"
        Else
            ManageSectionCoEstimates(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlClientDocumentation.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionClientDocumentation(False)
            strAccessIDs &= "0"
        Else
            ManageSectionClientDocumentation(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlFormalApplication.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionFormalApplication(False)
            strAccessIDs &= "0"
        Else
            ManageSectionFormalApplication(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlSL21.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionSL21(False)
            strAccessIDs &= "0"
        Else
            ManageSectionSL21(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlWorkCompleted.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionWorkCompleted(False)
            strAccessIDs &= "0"
        Else
            ManageSectionWorkCompleted(True)
            strAccessIDs &= "1"
        End If

        If Me.HasAccessTo(Me.GetControlID(pnlComments.ClientID), oEC_User.AccessGroup) = False Then
            ManageSectionComments(False)
            strAccessIDs &= "0"
        Else
            ManageSectionComments(True)
            strAccessIDs &= "1"
        End If

        Select Case hdAction.Value
            Case "I"
                btnDelete.Visible = False

                If oEC_User.AccessGroup = Constants.CONST_ACCESSGROUP_TA Then
                    ManageMainSection(False)
                    ManageControlButtons(False) 'cannot create and not delete
                End If
            Case "U"
                btnDelete.Visible = True
                btnSave.Visible = True

                If oEC_User.AccessGroup = Constants.CONST_ACCESSGROUP_TA Then
                    btnDelete.Visible = False
                End If
        End Select

        If strAccessIDs.Length = 8 Then
            hdAccess.Value = strAccessIDs
        Else
            hdAccess.Value = "00000000"
        End If

    End Sub

    Private Sub BlockAllControls()
        ManageMainSection(False)
        ManageSectionVisitOG(False)
        ManageSectionArchitect(False)
        ManageSectionCoEstimates(False)
        ManageSectionClientDocumentation(False)
        ManageSectionFormalApplication(False, False)
        ManageSectionSL21(False, False)
        ManageSectionWorkCompleted(False)
        ManageSectionComments(False)
        ManageControlButtons(False)
        hdAccess.Value = "00000000"
    End Sub

    Private Sub EnableCommentsToSave()
        ManageSectionComments(True)
        btnSave.Visible = True
        hdAccess.Value = "00000001"
    End Sub

    Private Sub ValidateAccessToCase(ByVal oCase As EC_Case)
        Dim oUser As EC_User = Me.GetUserFromSession

        If oUser Is Nothing Then
            Me.RedirectToLogin(Constants.CONST_ERROR_SESSION_EXPIRED)
            Me.Response.End()
        End If

        If oUser.AccessGroup = Constants.CONST_ACCESSGROUP_HA Then

            If oUser.UserID.ToString.Equals(oCase.HA) = False Then
                BlockAllControls()
            Else
                'validate stages                   
                If (oCase.Stage = Constants.CONST_CASESTAGE_PRELIM Or oCase.Stage = Constants.CONST_CASESTAGE_OG Or _
                                        oCase.Stage = Constants.CONST_CASESTAGE_APP Or oCase.Stage = Constants.CONST_CASESTAGE_APQ) = False Then
                    BlockAllControls()
                End If

                'To save comments
                EnableCommentsToSave()

            End If

        ElseIf oUser.AccessGroup = Constants.CONST_ACCESSGROUP_TA Then
            If oUser.UserID.ToString.Equals(oCase.TA) = False Then
                BlockAllControls()
            Else
                'validate stages
                If (oCase.Stage = Constants.CONST_CASESTAGE_SL21 Or oCase.Stage = Constants.CONST_CASESTAGE_OS Or _
                    oCase.Stage = Constants.CONST_CASESTAGE_COS Or oCase.Stage = Constants.CONST_CASESTAGE_COMP) = False Then
                    BlockAllControls()
                End If

                'To save comments
                EnableCommentsToSave()

            End If
        End If

    End Sub

#End Region

#Region "Controls' events"

    Private Sub SetCancel()

        Me.RedirectToHome()

    End Sub

    Private Sub ddlVisit_HA_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlVisit_HA.SelectedIndexChanged
        Dim oCC_GO As New CC_GrantOffice
        Dim dsGO As DataSet

        ddlVisit_GO.Items.Clear()
        txtVisit_RefNum.Text = String.Empty

        If ddlVisit_HA.SelectedValue.Equals(String.Empty) Then
            ddlVisit_GO.Items.Add(New ListItem("", ""))
            Exit Sub
        End If

        Try
            dsGO = oCC_GO.GetGrantOfficeByAdvisor(ddlVisit_HA.SelectedValue)

            If Not dsGO Is Nothing Then
                Util.FillDropDownList(dsGO, ddlVisit_GO, "GRANTOFFICEID", "NAME", True, New ListItem("", ""))

                'txtVisit_RefNum.Text = IIf(oEC_GO.Postcode.Length = 7, oEC_GO.Postcode.Substring(2, 3), String.Empty)
            End If
        Catch

        Finally
            If ddlVisit_GO.Items.Count = 0 Then
                ddlVisit_GO.Items.Add(New ListItem("", ""))
            End If

        End Try

    End Sub

    Private Function GetGOPrefix() As String
        Dim strPrefix As String = ""

        If ddlVisit_GO.SelectedValue.Equals(String.Empty) Then
            strPrefix = ""
        Else
            Dim oCC_GO As New CC_GrantOffice
            Dim oEC_GO As EC_GrantOffice
            Dim position As Integer
            Dim Prefix As String

            Try
                oEC_GO = oCC_GO.GetGrantOfficeByID(ddlVisit_GO.SelectedValue.Trim)
                If Not oEC_GO Is Nothing Then
                    Do While position <= oEC_GO.Postcode.Length
                        Try
                            Dim nNumb As Integer = Convert.ToInt32(Mid(oEC_GO.Postcode, position, 1))
                            Prefix = Prefix + Mid(oEC_GO.Postcode, position, 1)
                        Catch

                        End Try
                        position = position + 1
                    Loop
                    strPrefix = IIf(oEC_GO.Postcode.Length = 7, Prefix, String.Empty) & "-"
                End If
            Catch ex As Exception
                strPrefix = ""
            End Try

        End If

        Return strPrefix

    End Function
    Private Sub ddlVisit_GO_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlVisit_GO.SelectedIndexChanged
        hdVisit_GOPrefix.Value = GetGOPrefix()

        If ddlVisit_GT.SelectedValue.Trim.Equals(String.Empty) = False Then
            txtVisit_RefNum.Text = hdVisit_GOPrefix.Value
        End If

        If chkVisit_Sim.Checked Then
            If ddlVisit_GT_Sim.SelectedValue.Trim.Equals(String.Empty) = False Then
                txtVisit_RefNum_Sim.Text = hdVisit_GOPrefix.Value
            End If

        End If
    End Sub

    Private Sub dgCoEstimates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCoEstimates.ItemDataBound
        If e.Item.ItemType <> ListItemType.Header Then
            e.Item.Cells(0).Text = "<a href=""#"" onclick=""fnSelRow(" & e.Item.ItemIndex.ToString & ")""><img src=""../images/view.gif"" border=0></a>"
        End If
    End Sub

    Private Sub SelectRow(ByVal rowID As Integer)

        Try
            If dgCoEstimates.Items.Count > 0 Then
                'Setting the action as an updating
                hdCEItemID.Value = dgCoEstimates.Items(rowID).Cells(1).Text

                For i As Integer = 0 To dgCoEstimates.Items.Count - 1
                    If i Mod 2 = 0 Then
                        dgCoEstimates.Items(i).CssClass = "PAGE_GRID_ITEM"
                    Else
                        dgCoEstimates.Items(i).CssClass = "PAGE_GRID_ALTERNATING"
                    End If
                Next

                dgCoEstimates.Items(rowID).CssClass = "PAGE_GRID_ITEM_SELECTED"
            End If
        Catch

        End Try

    End Sub
#End Region

#Region "Save"

    Private Sub Save()

        If hdAction.Value.Equals("I") Then
            Insert()
        ElseIf hdAction.Value.Equals("U") Then
            Update()
        ElseIf hdAction.Value.Equals("D") Then
            Delete()
        End If

        hdEvent.Value = String.Empty

    End Sub

    Private Sub Insert()
        Dim oEC_Case As EC_Case = SetProperties()
        Dim oCC_Case As New CC_Case
        Dim strMsg As String

        If Not oEC_Case Is Nothing Then
            If oCC_Case.SaveCase(oEC_Case, "M", strMsg) Then
                Me.RedirectToMsgPage(Constants.CONST_MSG_INSERT_SUCCES, "&action=u&caseid=" & oEC_Case.CaseID)
            Else
                Me.RedirectToMsgPage(Constants.CONST_MSG_INSERT_NOSUCCES)
            End If

        End If

    End Sub

    Private Sub Update()
        Dim oEC_Case As EC_Case = SetProperties()
        Dim oCC_Case As New CC_Case
        Dim strMsg As String

        If Not oEC_Case Is Nothing Then
            If oCC_Case.UpdateCase(oEC_Case, "M", strMsg) Then
                SendNotification(oEC_Case)
                Me.RedirectToMsgPage(Constants.CONST_MSG_UPDATE_SUCCES, "&action=u&caseid=" & oEC_Case.CaseID)
            Else
                Me.RedirectToMsgPage(Constants.CONST_MSG_UPDATE_NOSUCCES)
            End If

        End If
    End Sub

    Private Sub Delete()
        Dim oEC_Case As EC_Case = SetProperties()
        Dim oCC_Case As New CC_Case
        Dim strMsg As String

        If Not oEC_Case Is Nothing Then
            If oCC_Case.DeleteCase(oEC_Case, strMsg) Then
                Me.RedirectToMsgPage(Constants.CONST_MSG_DELETE_SUCCES, "&goto=home")
            Else
                Me.RedirectToMsgPage(Constants.CONST_MSG_DELETE_NOSUCCES)
            End If

        End If
    End Sub

    Private Function SetProperties() As EC_Case
        Dim oEC_Case As New EC_Case
        Dim oEC_User As EC_User = Me.GetUserFromSession

        With oEC_Case
            .CaseID = txtCaseId.Text.Trim
            .ApplIDN = txtApplNIN.Text.Trim.ToUpper

            'Si se quiere cerrar un caso sin establecer el estado a cancelado
            If txtWC_CompPaid.Text.Trim.Equals(String.Empty) = False And ddlStatus.SelectedValue.Equals(Constants.CONST_CASESTATUS_CANCELLED) = False Then
                'entonces se cierra el caso
                .Status = Constants.CONST_CASESTATUS_CLOSED
            Else
                'si el caso está cerrado(hdwc_comppaid lleno), y se quiere reabrir(txtwc_comppaid vacio)
                'y el combo de estado diferente de cancelado
                If txtWC_CompPaid.Text.Trim.Equals(String.Empty) = True And _
                    hdWC_CompPaid.Value.Trim.Equals(String.Empty) = False And _
                     ddlStatus.SelectedValue.Equals(Constants.CONST_CASESTATUS_CANCELLED) = False Then
                    'se establece a normal, para que luego se calcule el estatus
                    .Status = Constants.CONST_CASESTATUS_NORMAL
                Else
                    .Status = ddlStatus.SelectedValue.Trim
                End If

            End If

            .ReasonId = ddlReason.SelectedValue
            .Stage = txtStage.Text.Trim

            If rbReminderOn.Checked Then
                .Reminder = "1"
            ElseIf rbReminderOff.Checked Then
                .Reminder = "0"
            Else
                .Reminder = String.Empty
            End If

            'Visit/on going
            .GrantTypeId = ddlVisit_GT.SelectedValue.Trim
            .GrantOfficeId = ddlVisit_GO.SelectedValue.Trim
            .HA = ddlVisit_HA.SelectedValue.Trim
            .OGAmdSche = txtVisit_AmendedSch.Text
            .OGCContact = txtVisit_ClientContact.Text
            .OGCVisited = txtVisit_ClientVisited.Text
            .OGRefNum = txtVisit_RefNum.Text.Trim.ToUpper
            .OGRefSche = txtVisit_RefSch.Text
            .OGSGrantTypeId = ddlVisit_GT_Sim.SelectedValue

            If chkVisit_Sim.Checked Then
                .OGSimult = "Y"
            Else
                .OGSimult = "N"
            End If

            .OGSRefNum = txtVisit_RefNum_Sim.Text.Trim.ToUpper
            'End visit/on going

            'Architect
            .ProfessionalId = ddlArch_Arch.SelectedValue

            If chkArch_BCPR.Checked Then
                .ArchBControl = "Y"
            Else
                .ArchBControl = "N"
            End If

            .ArchSPRec = txtArch_SPRec.Text
            .ArchSPReq = txtArch_SPReq.Text
            .ArchWDRec = txtArch_WDRec.Text
            .ArchWDReq = txtArch_WDReq.Text
            'End Architect

            'Contractor Estimates
            .CoEstimates = SetCoEstimates()
            'End Contractor Estimates

            'Client's Documentation
            If chkDR_LndCons.Checked Then
                .CDLandCons = "Y"
            Else
                .CDLandCons = "N"
            End If
            .CDLandRec = txtDR_LndConsRec.Text
            .CDLandReq = txtDR_LndConsReq.Text

            If chkDR_MortCons.Checked Then
                .CDMortCons = "Y"
            Else
                .CDMortCons = "N"
            End If
            .CDMortRec = txtDR_MortConsRec.Text
            .CDMortReq = txtDR_MortConsReq.Text

            If chkDR_ProofTitle.Checked Then
                .CDProof = "Y"
            Else
                .CDProof = "N"
            End If
            .CDProofRec = txtDR_ProofRec.Text
            .CDProofReq = txtDR_ProofReq.Text
            'End Client's Documentation

            'Formal Application
            .AppAproval = txtFA_ApprovalDate.Text
            .AppClientPortion = txtFA_PortionAmount.Text.Trim
            .AppDate = txtFA_AppDate.Text
            .AppFoldInt = txtFA_FoldInterest.Text.Trim

            If rbFA_Fold.Checked Then
                .AppMoney = "F"
            ElseIf rbFA_Other.Checked Then
                .AppMoney = "O"
            Else
                .AppMoney = String.Empty
            End If

            .AppMoneyDesc = txtFA_Other.Text.Trim
            .AppRevSch = txtFA_RevSch.Text

            If chkFA_A.Checked Then
                .AppSL20A = "Y"
            Else
                .AppSL20A = "N"
            End If

            If chkFA_B.Checked Then
                .AppSl20B = "Y"
            Else
                .AppSl20B = "N"
            End If

            If chkFA_C.Checked Then
                .AppSL20C = "Y"
            Else
                .AppSL20C = "N"
            End If

            .AppSL20Rec = txtFA_SL20Rec.Text
            .AppSL20Req = txtFA_SL20Req.Text
            .AppSL21 = txtFA_Issued.Text
            'End Formal Application

            'SL21
            .ContractorId = ddlSL21_Contractor.SelectedValue
            .TA = ddlSL21_TA.SelectedValue
            .SL21ConfAssig = txtSL21_ConfAssig.Text
            .SL21OnSite = txtSL21_OnSite.Text
            'End SL21

            'Work Completed
            If chkWC_BuildingControl.Checked Then
                .WCBControl = "Y"
            Else
                .WCBControl = "N"
            End If

            If chkWC_CommCard.Checked Then
                .WCCommCard = "Y"
            Else
                .WCCommCard = "N"
            End If

            If chkWC_CompCard.Checked Then
                .WCCompCard = "Y"
            Else
                .WCCompCard = "N"
            End If

            If chkWC_HR7.Checked Then
                .WCHR7 = "Y"
            Else
                .WCHR7 = "N"
            End If

            If chkWC_Niceic.Checked Then
                .WCNICEIC = "Y"
            Else
                .WCNICEIC = "N"
            End If

            If chkWC_FinalInvoice.Checked Then
                .WCFinalInv = "Y"
            Else
                .WCFinalInv = "N"
            End If

            If chkWC_Other.Checked Then
                .WCOtherDoc = "Y"
            Else
                .WCOtherDoc = "N"
            End If

            If chkWC_SnagList.Checked Then
                .WCSnag = "Y"
            Else
                .WCSnag = "N"
            End If

            .WCDescDoc = txtWC_Other.Text.Trim

            .WCCOMP = txtWC_CompPaid.Text
            'If .WCCOMP.Trim.Equals(String.Empty) = False Then
            '.WCBControl = "Y"
            'chkWC_BuildingControl.Checked = True
            'End If

            .WCCOS = txtWC_CompOnSite.Text
            .WCDateRec = txtWC_DateRec.Text
            .WCGrant = txtWC_Grant.Text.Trim
            .WCOverCost = txtWC_OverallCost.Text.Trim
            .WCSGrant = txtWC_SGrant.Text.Trim
            .WCSOverCost = txtWC_SOverallCost.Text.Trim
            'End Work Completed

            If Not oEC_User Is Nothing Then
                'Comments
                Dim sbComm As New StringBuilder

                If ddlStatus.SelectedValue.Trim.Equals(Constants.CONST_CASESTATUS_CANCELLED) Then
                    'el caso ha sido cancelado
                    Dim strStatusDesc As String = ""

                    Select Case hdStatus.Value
                        Case Constants.CONST_CASESTATUS_NORMAL
                            strStatusDesc = "Normal"
                        Case Constants.CONST_CASESTATUS_ATTENTION
                            strStatusDesc = "Attention"
                        Case Constants.CONST_CASESTATUS_IMMATTENTION
                            strStatusDesc = "Immediate Attention"
                        Case Constants.CONST_CASESTATUS_CLOSED
                            strStatusDesc = "Closed"
                        Case Constants.CONST_CASESTATUS_CANCELLED
                            strStatusDesc = "Cancelled"
                    End Select

                    'sbComm.Append(vbCrLf & "-- " & Now.ToShortDateString.ToString(myDTFI) & " - " & oEC_User.Login & " --------------------------")
                    sbComm.Append(vbCrLf & "-- " & Now.Day & "/" & Now.Month & "/" & Now.Year & " - " & oEC_User.Login & " --------------------------")
                    sbComm.Append(vbCrLf & "Status has changed from " & strStatusDesc & " to Cancelled")
                    sbComm.Append(vbCrLf & vbCrLf)
                ElseIf hdStatus.Value.Equals(Constants.CONST_CASESTATUS_CANCELLED) And ddlStatus.SelectedValue.Trim.Equals(Constants.CONST_CASESTATUS_NORMAL) Then
                    'de cancelado a normal
                    'sbComm.Append(vbCrLf & "-- " & Now.ToShortDateString.ToString(myDTFI) & " - " & oEC_User.Login & " --------------------------")
                    sbComm.Append(vbCrLf & "-- " & Now.Day & "/" & Now.Month & "/" & Now.Year & " - " & oEC_User.Login & " --------------------------")
                    sbComm.Append(vbCrLf & "Status has changed from Cancelled to Normal")
                    sbComm.Append(vbCrLf & vbCrLf)
                End If

                If txtComm_NewComm.Text.Trim.Equals(String.Empty) = False Then
                    'sbComm.Append(vbCrLf & "-- " & Now.ToShortDateString.ToString(myDTFI) & " - " & oEC_User.Login & " --------------------------")
                    sbComm.Append(vbCrLf & "-- " & Now.Day & "/" & Now.Month & "/" & Now.Year & " - " & oEC_User.Login & " --------------------------")
                    sbComm.Append(vbCrLf & txtComm_NewComm.Text)
                    sbComm.Append(vbCrLf & vbCrLf)
                End If

                sbComm.Append(txtComm_PrevComm.Text)

                .Comments = sbComm.ToString
                'End Comments


                .AddedBy = oEC_User.Login
                .ChangedBy = oEC_User.Login
            End If

        End With

        Return oEC_Case

    End Function

    Private Function SetCoEstimates() As EC_CoEstimates()
        Dim oEC_CoEstimates() As EC_CoEstimates
        Dim nContID As Integer

        For i As Integer = 0 To dgCoEstimates.Items.Count - 1
            Try
                nContID = Convert.ToInt32(dgCoEstimates.Items(i).Cells(1).Text)

                If oEC_CoEstimates Is Nothing Then
                    ReDim Preserve oEC_CoEstimates(0)
                    oEC_CoEstimates(0) = New EC_CoEstimates
                Else
                    ReDim Preserve oEC_CoEstimates(oEC_CoEstimates.Length)
                    oEC_CoEstimates(oEC_CoEstimates.Length - 1) = New EC_CoEstimates
                End If

                With oEC_CoEstimates(oEC_CoEstimates.GetUpperBound(0))
                    .ContractorID = nContID

                    Try
                        .DateReq = DateTime.Parse(dgCoEstimates.Items(i).Cells(3).Text, myDTFI)
                        .DateReq = dgCoEstimates.Items(i).Cells(3).Text
                    Catch
                        .DateReq = String.Empty
                    End Try

                    Try
                        .DateRec = DateTime.Parse(dgCoEstimates.Items(i).Cells(4).Text, myDTFI)
                        .DateRec = dgCoEstimates.Items(i).Cells(4).Text
                    Catch
                        .DateRec = String.Empty
                    End Try


                    .NewEstimate = dgCoEstimates.Items(i).Cells(5).Text


                End With

            Catch

            End Try

        Next

        Return oEC_CoEstimates
    End Function

#End Region

#Region "Case's properties"

    Private Sub LoadCase()
        If Not Request.QueryString("action") Is Nothing AndAlso Not Request.QueryString("caseid") Is Nothing Then
            Dim oCC_Case As New CC_Case
            Dim strCaseID As String
            Dim oEC_Case As EC_Case

            Try
                strCaseID = Request.QueryString("caseid")
                oEC_Case = oCC_Case.GetCaseByID(strCaseID)

                If Not oEC_Case Is Nothing Then

                    With oEC_Case
                        txtCaseId.Text = .CaseID.Trim.ToUpper
                        txtApplNIN.Text = .ApplIDN.Trim
                        BringClient(.ApplIDN.Trim)

                        Util.SelectValueFromDropDown(ddlStatus, .Status.Trim)
                        hdStatus.Value = .Status.Trim
                        Select Case .Status.Trim
                            Case Constants.CONST_CASESTATUS_NORMAL
                                lblStatusIndicator.BackColor = Color.Green
                            Case Constants.CONST_CASESTATUS_ATTENTION
                                lblStatusIndicator.BackColor = Color.Yellow
                            Case Constants.CONST_CASESTATUS_IMMATTENTION
                                lblStatusIndicator.BackColor = Color.Red
                            Case Constants.CONST_CASESTATUS_CLOSED
                                lblStatusIndicator.BackColor = Color.Blue
                            Case Constants.CONST_CASESTATUS_CANCELLED
                                lblStatusIndicator.BackColor = Color.Orange
                        End Select

                        Util.SelectValueFromDropDown(ddlReason, .ReasonId.Trim)

                        txtStage.Text = .Stage.Trim

                        If .Reminder.Trim.Equals("1") Then
                            rbReminderOn.Checked = True
                            rbReminderOff.Checked = False
                        Else
                            rbReminderOn.Checked = False
                            rbReminderOff.Checked = True
                        End If
                        'TODO:Cargar Reminders

                        'Visit/on going
                        Util.SelectValueFromDropDown(ddlVisit_GT, .GrantTypeId.Trim)

                        Util.SelectValueFromDropDown(ddlVisit_HA, .HA.Trim)
                        ddlVisit_HA_SelectedIndexChanged(Nothing, Nothing)

                        Util.SelectValueFromDropDown(ddlVisit_GO, .GrantOfficeId)
                        hdVisit_GOPrefix.Value = GetGOPrefix()

                        txtVisit_AmendedSch.Text = .OGAmdSche
                        hdVisit_AmendedSch.Value = .OGAmdSche

                        txtVisit_ClientContact.Text = .OGCContact
                        txtVisit_ClientVisited.Text = .OGCVisited
                        txtVisit_RefNum.Text = .OGRefNum
                        txtVisit_RefSch.Text = .OGRefSche
                        Util.SelectValueFromDropDown(ddlVisit_GT_Sim, .OGSGrantTypeId.Trim)

                        If .OGSimult = "Y" Then
                            chkVisit_Sim.Checked = True
                        Else
                            chkVisit_Sim.Checked = False
                        End If

                        txtVisit_RefNum_Sim.Text = .OGSRefNum
                        'End visit/on going

                        'Architect
                        Util.SelectValueFromDropDown(ddlArch_Arch, .ProfessionalId)

                        If .ArchBControl = "Y" Then
                            chkArch_BCPR.Checked = True
                        Else
                            chkArch_BCPR.Checked = False
                        End If

                        txtArch_SPRec.Text = .ArchSPRec
                        txtArch_SPReq.Text = .ArchSPReq
                        txtArch_WDRec.Text = .ArchWDRec
                        txtArch_WDReq.Text = .ArchWDReq
                        'End Architect

                        'Contractor Estimates
                        LoadCoEstimates(.CoEstimates)
                        'End Contractor Estimates

                        'Client's Documentation
                        If .CDLandCons = "Y" Then
                            chkDR_LndCons.Checked = True
                        Else
                            chkDR_LndCons.Checked = False
                        End If

                        txtDR_LndConsRec.Text = .CDLandRec
                        txtDR_LndConsReq.Text = .CDLandReq

                        If .CDMortCons = "Y" Then
                            chkDR_MortCons.Checked = True
                        Else
                            chkDR_MortCons.Checked = False
                        End If

                        txtDR_MortConsRec.Text = .CDMortRec
                        txtDR_MortConsReq.Text = .CDMortReq

                        If .CDProof = "Y" Then
                            chkDR_ProofTitle.Checked = True
                        Else
                            chkDR_ProofTitle.Checked = False
                        End If

                        txtDR_ProofRec.Text = .CDProofRec
                        txtDR_ProofReq.Text = .CDProofReq
                        'End Client's Documentation

                        'Formal Application
                        txtFA_ApprovalDate.Text = .AppAproval
                        txtFA_PortionAmount.Text = .AppClientPortion.Trim
                        txtFA_AppDate.Text = .AppDate
                        txtFA_FoldInterest.Text = .AppFoldInt.Trim

                        If .AppMoney = "F" Then
                            rbFA_Fold.Checked = True
                            txtFA_Other.Text = String.Empty
                        ElseIf .AppMoney = "O" Then
                            rbFA_Other.Checked = True
                            txtFA_Other.Text = .AppMoneyDesc.Trim
                        Else
                            rbFA_Fold.Checked = False
                            rbFA_Other.Checked = False
                        End If

                        txtFA_RevSch.Text = .AppRevSch
                        hdFA_RevSch.Value = .AppRevSch

                        If .AppSL20A = "Y" Then
                            chkFA_A.Checked = True
                        Else
                            chkFA_A.Checked = False
                        End If

                        If .AppSl20B = "Y" Then
                            chkFA_B.Checked = True
                        Else
                            chkFA_B.Checked = False
                        End If

                        If .AppSL20C = "Y" Then
                            chkFA_C.Checked = True
                        Else
                            chkFA_C.Checked = False
                        End If

                        txtFA_SL20Rec.Text = .AppSL20Rec
                        txtFA_SL20Req.Text = .AppSL20Req

                        txtFA_Issued.Text = .AppSL21
                        hdFA_Issued.Value = .AppSL21
                        'End Formal Application

                        'SL21
                        Util.SelectValueFromDropDown(ddlSL21_Contractor, .ContractorId)
                        Util.SelectValueFromDropDown(ddlSL21_TA, .TA)

                        txtSL21_ConfAssig.Text = .SL21ConfAssig
                        txtSL21_OnSite.Text = .SL21OnSite
                        'End SL21

                        'Work Completed

                        If .WCBControl = "Y" Then
                            chkWC_BuildingControl.Checked = True
                        Else
                            chkWC_BuildingControl.Checked = False
                        End If

                        If .WCCommCard = "Y" Then
                            chkWC_CommCard.Checked = True
                        Else
                            chkWC_CommCard.Checked = False
                        End If

                        If .WCCompCard = "Y" Then
                            chkWC_CompCard.Checked = True
                        Else
                            chkWC_CompCard.Checked = False
                        End If

                        If .WCHR7 = "Y" Then
                            chkWC_HR7.Checked = True
                        Else
                            chkWC_HR7.Checked = False
                        End If

                        If .WCNICEIC = "Y" Then
                            chkWC_Niceic.Checked = True
                        Else
                            chkWC_Niceic.Checked = False
                        End If

                        If .WCFinalInv = "Y" Then
                            chkWC_FinalInvoice.Checked = True
                        Else
                            chkWC_FinalInvoice.Checked = False
                        End If

                        If .WCOtherDoc = "Y" Then
                            chkWC_Other.Checked = True
                            txtWC_Other.Text = .WCDescDoc.Trim
                        Else
                            chkWC_Other.Checked = False
                            txtWC_Other.Text = String.Empty
                        End If

                        If .WCSnag = "Y" Then
                            chkWC_SnagList.Checked = True
                        Else
                            chkWC_SnagList.Checked = False
                        End If


                        txtWC_CompPaid.Text = .WCCOMP
                        hdWC_CompPaid.Value = .WCCOMP

                        txtWC_CompOnSite.Text = .WCCOS
                        txtWC_DateRec.Text = .WCDateRec
                        txtWC_Grant.Text = .WCGrant.Trim
                        txtWC_OverallCost.Text = .WCOverCost.Trim
                        txtWC_SGrant.Text = .WCSGrant.Trim
                        txtWC_SOverallCost.Text = .WCSOverCost.Trim
                        'End Work Completed

                        'Comments
                        txtComm_PrevComm.Text = .Comments.Trim
                        'End Comments

                        'Load targets
                        LoadTargets()

                        'Load Perfomance Measures
                        SetPerfomanceMeasure()

                        'Load Reminders
                        If rbReminderOn.Checked Then
                            Dim dsRems As DataSet = oCC_Case.GetRemindersByCase(.CaseID)

                            If Not dsRems Is Nothing Then
                                If dsRems.Tables(0).Rows.Count > 0 Then
                                    dlReminders.Visible = True
                                    dlReminders.DataSource = dsRems.Tables(0).DefaultView
                                    dlReminders.DataBind()
                                End If
                            End If

                        Else
                            dlReminders.Visible = False
                        End If

                        'Validate access to Case
                        ValidateAccessToCase(oEC_Case)
                    End With

                End If

            Catch

            End Try
        End If

    End Sub

    Private Sub LoadCoEstimates(ByVal oCoEstimates() As EC_CoEstimates)

        If oCoEstimates Is Nothing OrElse oCoEstimates.Length = 0 Then
            Me.InitialValuesCoEstimates()
            Exit Sub
        End If

        Dim dtCoEst As New DataTable("COESTIMATES")

        dtCoEst.Columns.Add("CONTRACTORID")
        dtCoEst.Columns.Add("COMPANYNAME")
        dtCoEst.Columns.Add("DATEREQ")
        dtCoEst.Columns.Add("DATEREC")
        dtCoEst.Columns.Add("NEWESTIMATE")

        Dim drow As DataRow

        For i As Integer = 0 To oCoEstimates.Length - 1
            Try
                drow = dtCoEst.NewRow
                drow("CONTRACTORID") = oCoEstimates(i).ContractorID.ToString
                drow("COMPANYNAME") = oCoEstimates(i).Name
                drow("DATEREQ") = oCoEstimates(i).DateReq
                drow("DATEREC") = oCoEstimates(i).DateRec
                drow("NEWESTIMATE") = oCoEstimates(i).NewEstimate
                dtCoEst.Rows.Add(drow)
            Catch

            End Try
        Next

        dgCoEstimates.DataSource = dtCoEst.DefaultView
        dgCoEstimates.DataBind()

    End Sub

    Private Sub LoadTargets()
        Dim oCC_Case As New CC_Case

        Try
            txtVisit_VisitTarget.Text = oCC_Case.GetVisitTarget(txtVisit_ClientContact.Text, txtVisit_RefSch.Text)
            txtFA_TargetApp.Text = oCC_Case.GetApplicationTarget(txtVisit_ClientVisited.Text, txtVisit_AmendedSch.Text, ddlVisit_GT.SelectedValue.Trim, ddlVisit_GT_Sim.SelectedValue.Trim)
            txtFA_TargetIssue.Text = oCC_Case.GetSL21Target(txtFA_ApprovalDate.Text, txtFA_RevSch.Text)
            txtWC_Target.Text = oCC_Case.GetCompTarget(txtFA_ApprovalDate.Text, ddlVisit_GT.SelectedValue.Trim, ddlVisit_GT_Sim.SelectedValue.Trim)
        Catch

        End Try

    End Sub

    Private Sub BringClient(ByVal strClientID As String)
        Dim oEC_Client As EC_Client
        Dim oCC_Client As New CC_Client

        Try
            oEC_Client = oCC_Client.GetClientByID(strClientID)

            If Not oEC_Client Is Nothing Then
                With oEC_Client
                    txtApplNIN.Text = strClientID
                    txtApplAddress.Text = .Address.Trim
                    txtApplName.Text = .Name.Trim
                    txtApplSurname.Text = .Surname.Trim
                    txtPostcode.Text = .Postcode.Trim
                End With
            Else
                Throw New Exception("No reference")
            End If
        Catch
            txtApplAddress.Text = String.Empty
            txtApplName.Text = String.Empty
            txtApplSurname.Text = String.Empty
            txtPostcode.Text = String.Empty
        End Try
    End Sub

#End Region

#Region "CoEstimates"
    Private Sub ShowCoEstimate(ByVal nIndex As Integer)
        Dim nContID As Integer

        Try
            nContID = Convert.ToInt32(hdCEItemID.Value)
            Util.SelectValueFromDropDown(ddlEst_Contractor, nContID.ToString)
            txtEst_Req.Text = IIf(dgCoEstimates.Items(nIndex).Cells(3).Text = "&nbsp;", "", dgCoEstimates.Items(nIndex).Cells(3).Text)
            txtEst_Rec.Text = IIf(dgCoEstimates.Items(nIndex).Cells(4).Text = "&nbsp;", "", dgCoEstimates.Items(nIndex).Cells(4).Text)
        Catch
            Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
            txtEst_Req.Text = String.Empty
            txtEst_Rec.Text = String.Empty
            Exit Sub
        End Try
    End Sub

    Private Sub RefreshCoEstimatesList()
        Dim dtCoEst As New DataTable("COESTIMATES")

        dtCoEst.Columns.Add("CONTRACTORID")
        dtCoEst.Columns.Add("COMPANYNAME")
        dtCoEst.Columns.Add("DATEREQ")
        dtCoEst.Columns.Add("DATEREC")
        dtCoEst.Columns.Add("NEWESTIMATE")

        Dim drow As DataRow

        For i As Integer = 0 To dgCoEstimates.Items.Count - 1
            Try
                drow = dtCoEst.NewRow
                drow("CONTRACTORID") = Convert.ToInt32(dgCoEstimates.Items(i).Cells(1).Text)
                drow("COMPANYNAME") = dgCoEstimates.Items(i).Cells(2).Text
                drow("DATEREQ") = dgCoEstimates.Items(i).Cells(3).Text
                drow("DATEREC") = dgCoEstimates.Items(i).Cells(4).Text

                drow("NEWESTIMATE") = dgCoEstimates.Items(i).Cells(5).Text

                If txtFA_ApprovalDate.Text.Trim.Equals(String.Empty) = False Then
                    drow("NEWESTIMATE") = "Y"
                Else
                    drow("NEWESTIMATE") = "N"
                End If


                dtCoEst.Rows.Add(drow)
            Catch

            End Try

        Next

        dgCoEstimates.DataSource = dtCoEst.DefaultView
        dgCoEstimates.DataBind()
    End Sub

    Private Sub CleanHidden()
        hdEvent.Value = String.Empty
        hdCEItemID.Value = String.Empty
        hdRowId.Value = String.Empty

    End Sub

    Private Sub InitialValuesCoEstimates()
        Dim dtCoEst As New DataTable("COESTIMATES")

        dtCoEst.Columns.Add("CONTRACTORID")
        dtCoEst.Columns.Add("COMPANYNAME")
        dtCoEst.Columns.Add("DATEREQ")
        dtCoEst.Columns.Add("DATEREC")
        dtCoEst.Columns.Add("NEWESTIMATE")

        Dim drow As DataRow

        For i As Integer = 0 To 3
            drow = dtCoEst.NewRow
            drow("CONTRACTORID") = String.Empty
            drow("COMPANYNAME") = String.Empty
            drow("DATEREQ") = String.Empty
            drow("DATEREC") = String.Empty
            drow("NEWESTIMATE") = String.Empty
            dtCoEst.Rows.Add(drow)
        Next

        dgCoEstimates.Columns(0).Visible = False
        dgCoEstimates.DataSource = dtCoEst.DefaultView
        dgCoEstimates.DataBind()
    End Sub

    Private Sub AddCoEstimate()

        If ddlEst_Contractor.SelectedValue.Equals(String.Empty) OrElse txtEst_Req.Text.Equals(String.Empty) Then
            CleanHidden()
            Exit Sub
        End If

        Dim dtCoEst As New DataTable("COESTIMATES")
        Dim dcPrimKey(0) As DataColumn

        dtCoEst.Columns.Add("CONTRACTORID")
        dtCoEst.Columns.Add("COMPANYNAME")
        dtCoEst.Columns.Add("DATEREQ")
        dtCoEst.Columns.Add("DATEREC")
        dtCoEst.Columns.Add("NEWESTIMATE")

        dcPrimKey(0) = dtCoEst.Columns("CONTRACTORID")
        dtCoEst.PrimaryKey = dcPrimKey

        Dim drow As DataRow
        Dim nContID As Integer

        For i As Integer = 0 To dgCoEstimates.Items.Count - 1
            Try
                nContID = Convert.ToInt32(dgCoEstimates.Items(i).Cells(1).Text)
                drow = dtCoEst.NewRow
                drow("CONTRACTORID") = nContID
                drow("COMPANYNAME") = dgCoEstimates.Items(i).Cells(2).Text
                drow("DATEREQ") = dgCoEstimates.Items(i).Cells(3).Text
                drow("DATEREC") = dgCoEstimates.Items(i).Cells(4).Text
                drow("NEWESTIMATE") = dgCoEstimates.Items(i).Cells(5).Text
                dtCoEst.Rows.Add(drow)
            Catch

            End Try

        Next

        If ddlEst_Contractor.SelectedValue.Equals(String.Empty) = False And txtEst_Req.Text.Equals(String.Empty) = False Then
            'Added by Raul Callupe - 27/04/2006
            If Not dtCoEst.Rows.Find(ddlEst_Contractor.SelectedValue) Is Nothing Then

                Exit Sub
            End If
            'End Added

            drow = dtCoEst.NewRow
            drow("CONTRACTORID") = ddlEst_Contractor.SelectedValue
            drow("COMPANYNAME") = ddlEst_Contractor.SelectedItem.Text
            drow("DATEREQ") = txtEst_Req.Text
            drow("DATEREC") = txtEst_Rec.Text

            If txtFA_ApprovalDate.Text.Trim.Equals(String.Empty) = False Then
                drow("NEWESTIMATE") = "Y"
            Else
                drow("NEWESTIMATE") = "N"
            End If

            dtCoEst.Rows.Add(drow)
        End If

        dgCoEstimates.Columns(0).Visible = True
        dgCoEstimates.DataSource = dtCoEst.DefaultView
        dgCoEstimates.DataBind()

        Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
        txtEst_Rec.Text = String.Empty
        txtEst_Req.Text = String.Empty
        CleanHidden()

    End Sub

    Private Sub DeleteCoEstimate()
        Dim nContID As Integer

        Try
            nContID = Convert.ToInt32(hdCEItemID.Value)
        Catch
            Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
            txtEst_Rec.Text = String.Empty
            txtEst_Req.Text = String.Empty
            CleanHidden()
            Exit Sub
        End Try

        If hdRowId.Value.Equals(String.Empty) = False Then
            Dim dtCoEst As New DataTable("COESTIMATES")

            dtCoEst.Columns.Add("CONTRACTORID")
            dtCoEst.Columns.Add("COMPANYNAME")
            dtCoEst.Columns.Add("DATEREQ")
            dtCoEst.Columns.Add("DATEREC")
            dtCoEst.Columns.Add("NEWESTIMATE")

            Dim drow As DataRow

            For i As Integer = 0 To dgCoEstimates.Items.Count - 1
                If dgCoEstimates.Items(i).Cells(1).Text.Equals(String.Empty) = False Then
                    drow = dtCoEst.NewRow
                    drow("CONTRACTORID") = dgCoEstimates.Items(i).Cells(1).Text
                    drow("COMPANYNAME") = dgCoEstimates.Items(i).Cells(2).Text
                    drow("DATEREQ") = dgCoEstimates.Items(i).Cells(3).Text
                    drow("DATEREC") = dgCoEstimates.Items(i).Cells(4).Text
                    drow("NEWESTIMATE") = dgCoEstimates.Items(i).Cells(5).Text
                    dtCoEst.Rows.Add(drow)
                End If
            Next

            dtCoEst.Rows(hdRowId.Value).Delete()
            dtCoEst.AcceptChanges()

            dgCoEstimates.DataSource = dtCoEst.DefaultView
            dgCoEstimates.DataBind()

            'Verifying "assigned work" inside sl21

            If dtCoEst.Select("CONTRACTORID='" & ddlSL21_Contractor.SelectedValue & "'").Length = 0 Then
                Util.SelectValueFromDropDown(ddlSL21_Contractor, String.Empty)
            End If
            'end


        End If

        Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
        txtEst_Rec.Text = String.Empty
        txtEst_Req.Text = String.Empty
        CleanHidden()

        If dgCoEstimates.Items.Count = 0 Then
            InitialValuesCoEstimates()
        End If

    End Sub

    Private Sub UpdateCoEstimate()

        Try
            Dim nContID As Integer = Convert.ToInt32(hdCEItemID.Value)
        Catch
            Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
            txtEst_Rec.Text = String.Empty
            txtEst_Req.Text = String.Empty
            CleanHidden()
            Exit Sub
        End Try

        If ddlEst_Contractor.SelectedValue.Equals(String.Empty) OrElse txtEst_Req.Text.Equals(String.Empty) Then
            hdEvent.Value = String.Empty
            Exit Sub
        End If

        If hdRowId.Value.Equals(String.Empty) = False Then
            Dim dtCoEst As New DataTable("COESTIMATES")

            dtCoEst.Columns.Add("CONTRACTORID")
            dtCoEst.Columns.Add("COMPANYNAME")
            dtCoEst.Columns.Add("DATEREQ")
            dtCoEst.Columns.Add("DATEREC")
            dtCoEst.Columns.Add("NEWESTIMATE")

            Dim drow As DataRow

            For i As Integer = 0 To dgCoEstimates.Items.Count - 1
                Try
                    If dgCoEstimates.Items(i).Cells(1).Text.Equals(String.Empty) = False Then
                        drow = dtCoEst.NewRow
                        drow("CONTRACTORID") = Convert.ToInt32(dgCoEstimates.Items(i).Cells(1).Text)
                        drow("COMPANYNAME") = dgCoEstimates.Items(i).Cells(2).Text
                        drow("DATEREQ") = dgCoEstimates.Items(i).Cells(3).Text
                        drow("DATEREC") = dgCoEstimates.Items(i).Cells(4).Text

                        drow("NEWESTIMATE") = dgCoEstimates.Items(i).Cells(5).Text

                        dtCoEst.Rows.Add(drow)
                    End If
                Catch

                End Try

            Next

            Try
                dtCoEst.Rows(hdRowId.Value)("CONTRACTORID") = ddlEst_Contractor.SelectedValue
                dtCoEst.Rows(hdRowId.Value)("COMPANYNAME") = ddlEst_Contractor.SelectedItem.Text
                dtCoEst.Rows(hdRowId.Value)("DATEREQ") = txtEst_Req.Text
                dtCoEst.Rows(hdRowId.Value)("DATEREC") = txtEst_Rec.Text
                'dtCoEst.Rows(hdRowId.Value)("NEWESTIMATE") = dgCoEstimates.Items(hdRowId.Value).Cells(5).Text
                'Added by Vanessa Ayala 05/05/2006
                If txtFA_ApprovalDate.Text.Trim.Equals(String.Empty) = False Then
                    dtCoEst.Rows(hdRowId.Value)("NEWESTIMATE") = "Y"
                Else
                    dtCoEst.Rows(hdRowId.Value)("NEWESTIMATE") = "N"
                End If
                'EndAdded
                dtCoEst.AcceptChanges()
            Catch

            End Try

            dgCoEstimates.DataSource = dtCoEst.DefaultView
            dgCoEstimates.DataBind()
        End If

        Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
        txtEst_Rec.Text = String.Empty
        txtEst_Req.Text = String.Empty
        CleanHidden()
    End Sub

    Private Sub AssignCoEstimate()
        Try
            Dim nContID As Integer = Convert.ToInt32(hdCEItemID.Value)
        Catch
            Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
            txtEst_Rec.Text = String.Empty
            txtEst_Req.Text = String.Empty
            CleanHidden()
            Exit Sub
        End Try

        If hdCEItemID.Value.Equals(String.Empty) = False Then
            Util.SelectValueFromDropDown(ddlSL21_Contractor, hdCEItemID.Value)
        End If
        RefreshCoEstimatesList()
        Util.SelectValueFromDropDown(ddlEst_Contractor, String.Empty)
        txtEst_Rec.Text = String.Empty
        txtEst_Req.Text = String.Empty
        CleanHidden()
    End Sub
#End Region

#Region "Performance Measure"
    Private Sub SetPerfomanceMeasure()
        Dim dDateA As Date
        Dim dDateB As Date

        Try
            'Added by Vanessa Ayala 08/05/2006
            If txtVisit_VisitTarget.Text.Trim.Equals(String.Empty) Then
                txtPMFirst.Text = String.Empty
            Else
                If txtVisit_ClientVisited.Text.Trim.Equals(String.Empty) Then
                    dDateA = DateTime.Parse(Date.Today.ToString(strDateFormat), myDTFI)
                    dDateB = DateTime.Parse(txtVisit_VisitTarget.Text, myDTFI)
                    If dDateA >= dDateB Then
                        txtPMFirst.Text = "N"
                    Else
                        txtPMFirst.Text = String.Empty
                    End If
                Else
                    dDateA = DateTime.Parse(txtVisit_ClientVisited.Text, myDTFI)
                    dDateB = DateTime.Parse(txtVisit_VisitTarget.Text, myDTFI)
                    If dDateA <= dDateB Then
                        txtPMFirst.Text = "Y"
                    Else
                        txtPMFirst.Text = "N"
                    End If
                End If
            End If
            'End Add


            'If Not txtVisit_ClientVisited.Text.Trim.Equals(String.Empty) And Not txtVisit_VisitTarget.Text.Trim.Equals(String.Empty) Then
            '    dDateA = DateTime.Parse(txtVisit_ClientVisited.Text, myDTFI)
            '    dDateB = DateTime.Parse(txtVisit_VisitTarget.Text, myDTFI)

            '    If dDateA <= dDateB Then
            '        txtPMFirst.Text = "Y"
            '    Else
            '        txtPMFirst.Text = "N"
            '    End If
            'Else
            '    txtPMFirst.Text = String.Empty
            'End If

        Catch ex As Exception
            txtPMFirst.Text = String.Empty
        End Try

        Try

            'Added by Vanessa Ayala 08/05/2006
            If txtFA_TargetApp.Text.Trim.Equals(String.Empty) Then
                txtPMAmended.Text = String.Empty
            Else
                If txtFA_AppDate.Text.Trim.Equals(String.Empty) Then
                    dDateA = DateTime.Parse(Date.Today.ToString(strDateFormat), myDTFI)
                    dDateB = DateTime.Parse(txtFA_TargetApp.Text, myDTFI)
                    If dDateA >= dDateB Then
                        txtPMAmended.Text = "N"
                    Else
                        txtPMAmended.Text = String.Empty
                    End If
                Else
                    dDateA = DateTime.Parse(txtFA_AppDate.Text, myDTFI)
                    dDateB = DateTime.Parse(txtFA_TargetApp.Text, myDTFI)
                    If dDateA <= dDateB Then
                        txtPMAmended.Text = "Y"
                    Else
                        txtPMAmended.Text = "N"
                    End If
                End If
            End If
            'End Add



            'If Not txtFA_AppDate.Text.Trim.Equals(String.Empty) And Not txtFA_TargetApp.Text.Trim.Equals(String.Empty) Then
            '    dDateA = DateTime.Parse(txtFA_AppDate.Text, myDTFI)
            '    dDateB = DateTime.Parse(txtFA_TargetApp.Text, myDTFI)

            '    If dDateA <= dDateB Then
            '        txtPMAmended.Text = "Y"
            '    Else
            '        txtPMAmended.Text = "N"
            '    End If
            'Else
            '    txtPMAmended.Text = String.Empty
            'End If
        Catch ex As Exception
            txtPMAmended.Text = String.Empty
        End Try

        Try

            'Added by Vanessa Ayala 08/05/2006
            If txtWC_Target.Text.Trim.Equals(String.Empty) Then
                txtPMComp.Text = String.Empty
            Else
                If txtWC_CompPaid.Text.Trim.Equals(String.Empty) Then
                    dDateA = DateTime.Parse(Date.Today.ToString(strDateFormat), myDTFI)
                    dDateB = DateTime.Parse(txtWC_Target.Text, myDTFI)
                    If dDateA >= dDateB Then
                        txtPMComp.Text = "N"
                    Else
                        txtPMComp.Text = String.Empty
                    End If
                Else
                    dDateA = DateTime.Parse(txtWC_CompPaid.Text, myDTFI)
                    dDateB = DateTime.Parse(txtWC_Target.Text, myDTFI)
                    If dDateA <= dDateB Then
                        txtPMComp.Text = "Y"
                    Else
                        txtPMComp.Text = "N"
                    End If
                End If
            End If
            'End Add


            'If Not txtWC_CompPaid.Text.Trim.Equals(String.Empty) And Not txtWC_Target.Text.Trim.Equals(String.Empty) Then
            '    dDateA = DateTime.Parse(txtWC_CompPaid.Text, myDTFI)
            '    dDateB = DateTime.Parse(txtWC_Target.Text, myDTFI)

            '    If dDateA <= dDateB Then
            '        txtPMComp.Text = "Y"
            '    Else
            '        txtPMComp.Text = "N"
            '    End If
            'Else
            '    txtPMComp.Text = String.Empty
            'End If

        Catch ex As Exception
            txtPMComp.Text = String.Empty
        End Try

    End Sub
#End Region

#Region "Send Notificiation"

    Private Sub SendNotification(ByVal prmCase As EC_Case)
        Dim oUser As EC_User = Me.GetUserFromSession
        Dim nHA As Integer = 0
        Dim nTA As Integer = 0

        If prmCase.HA.Equals(String.Empty) = False Then
            nHA = Convert.ToInt32(prmCase.HA)
        Else
            Exit Sub
        End If

        If prmCase.TA.Equals(String.Empty) = False Then
            nTA = Convert.ToInt32(prmCase.TA)
        Else
            Exit Sub
        End If

        If prmCase.AppSL21.Trim.Equals(hdFA_Issued.Value.Trim) = False Then

            If Not oUser Is Nothing Then
                If prmCase.AppSL21.Trim.Equals(String.Empty) = False And (oUser.AccessGroup = Constants.CONST_ACCESSGROUP_HA Or oUser.AccessGroup = Constants.CONST_ACCESSGROUP_AO) Then
                    PrepareNotification(NotificationType.SL21Issued, nTA)
                ElseIf prmCase.AppSL21.Trim.Equals(String.Empty) And (oUser.AccessGroup = Constants.CONST_ACCESSGROUP_TA Or oUser.AccessGroup = Constants.CONST_ACCESSGROUP_AO) Then
                    PrepareNotification(NotificationType.CaseUnlocked, nHA)
                End If
            End If

        End If

    End Sub

    Private Sub PrepareNotification(ByVal prmNotifType As NotificationType, ByVal prmUserId As Integer)
        Dim oCC_User As New CC_User
        Dim dsMa As DataSet = oCC_User.GetManagersToNotify
        Dim _recipients() As Recipient
        Dim oNotif As New Notification(txtCaseId.Text.Trim, txtApplNIN.Text.Trim)

        If Not dsMa Is Nothing Then

            For i As Integer = 0 To dsMa.Tables(0).Rows.Count - 1
                ReDim Preserve _recipients(i)

                _recipients(i) = New Recipient

                With _recipients(i)
                    .Email = dsMa.Tables(0).Rows(i)("EMAIL")
                    .SendAs = SendType.SEND_Cc
                End With

            Next
        End If

        'Get HA or TA's mail
        Dim strTo As String
        Dim oEC_User As EC_User = oCC_User.GetUserByID(prmUserId)

        If Not oEC_User Is Nothing Then
            strTo = oEC_User.Email
        End If

        If _recipients Is Nothing OrElse _recipients.Length = 0 Then
            ReDim Preserve _recipients(0)
            _recipients(0) = New Recipient

            _recipients(0).Email = strTo
            _recipients(0).SendAs = SendType.SEND_To
        Else
            ReDim Preserve _recipients(_recipients.Length)
            _recipients(_recipients.GetUpperBound(0)) = New Recipient

            _recipients(_recipients.GetUpperBound(0)).Email = strTo
            _recipients(_recipients.GetUpperBound(0)).SendAs = SendType.SEND_To
        End If
        'End

        oNotif.Recipients = _recipients
        oNotif.Notification_Type = prmNotifType

        Try
            oNotif.ProcessNotification()
        Catch ex As Exception

        End Try

    End Sub

#End Region

End Class
