Partial Class Location_Master
    Inherits System.Web.UI.Page
    Shared Search As Boolean
    Shared g_User As String
    Shared g_Company As String
    Shared g_FinYear As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            If (Not IsPostBack) Then        'If IsPostBack is false then
                ClearFields()               'Clear the TextFields
                UnLockTextBox(False)        'Unlock (Enable) TextFields
                'g_User = "U1"
                'g_Company = "COM1"
                g_User = Session("g_User").ToString
                g_Company = Session("g_Company").ToString
                g_FinYear = Session("g_FinYear").ToString()
                FillCombo()
                FillGrid("", "")                  'Fills the DatagGrid with All Records
                Call CancelClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, g_User, "Location Master")
            End If
            txtLocCode.MaxLength = 20 : txtLocName.MaxLength = 50
            txtLocName.Attributes.Add("onblur", "return CheckNull('txtLocName');")
            txtLocName.Attributes.Add("onkeypress", "return BlockNumericValue(event);")
            '  txtLocName.Attributes.Add("onblur", "return toUpper('txtLocName');")
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub

    Protected Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try
            'If CBool(Session("Add_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Add New Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            'FillCombo()
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(True)
            'Enable the TextFields
            FillCombo()
            Call AddClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtLocId.Text = NewPrimaryKey(Me)
            txtLocCode.Text = Generate_Code("select count(*) from Location_Master where Location_Code='", "LOC")
            txtLocName.Focus()                                 'Put the Focus to CountryCode TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            'If CBool(Session("Add_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Add New Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            If CheckValidData("Add") = True Then
                UpdateData("Insert")   'Inserts the Record
                CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
                ClearFields()                                                                   'Clear the TextFields
                UnLockTextBox(False)                                                            'Disable the TextFields
                Call SaveClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Try
            'If CBool(Session("Modify_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Modify An Existing Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            If CheckValidData("Update") = True Then
                UpdateData("Update")  'Updates the record
                CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
                ClearFields()                                                                   'Clear the TextFields
                UnLockTextBox(False)                                                            'Disable the TextFields
                Call EditClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            'If CBool(Session("Delete_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Delete An Existing Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            If Trim(txtLocCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Location Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtLocCode.Focus()
                Exit Sub
            End If
            If Trim(txtLocName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Location Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtLocName.Focus()
                Exit Sub
            End If
            'If IsAlreadyPresent("select count(*) from Loc_Master where Loc_Code='" & txtLocCode.Text.Trim & "'and Loc_Isdeleted='0' and Loc_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
            '    CreateMessageAlert(Me, " Unable to Delete, Referenced to Other Records... !", "StrKeyVal")      'Display the Message
            '    BtnCancel_Click(sender, e)
            '    Exit Sub
            'End If
            UpdateData("Delete")  'Deletes the record                                               'Execute the Query
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call DeleteClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(False)                                'Disable the TextFields
            Search = False
            txtLocName.AutoPostBack = False
            Call FillGrid("", "")
            Call CancelClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
        Search = True                                       'Set Search Mode True
        Call SearchClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        txtLocName.Enabled = True
        txtLocName.AutoPostBack = True
        'Enable txtPartTypeName TextField
        ' Call txtLocName_TextChanged(sender, e)
        txtLocName.Focus()                             'Set Focus to txtPartTypeName TextField
    End Sub

    Protected Sub gvLoc_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvLoc.SelectedIndexChanged
        Try
            FillCombo()
            txtLocId.Text = gvLoc.SelectedRow.Cells(1).Text     'Assign the value of cell to the textField
            txtLocCode.Text = gvLoc.SelectedRow.Cells(2).Text     'Assign the value of cell to the textField
            txtLocName.Text = gvLoc.SelectedRow.Cells(3).Text     'Assign the value of cell to the textField
            ddlCountryName.ClearSelection()
            ddlCountryName.Items.FindByText(gvLoc.SelectedRow.Cells(6).Text).Selected = True
            Dim Qry As String
            Qry = "Select Distinct State_Kid,State_Name from State_Master " & _
                  " where State_CountryID='" & ddlCountryName.SelectedItem.Value & "' " & _
                  " and State_IsDeleted='0' and State_CompanyID='" & g_Company & "' order by State_Name "
            ddlStateName.Items.Clear()
            Fill_Combo(Qry, ddlStateName)
            ddlStateName.ClearSelection()
            ddlStateName.Items.FindByText(gvLoc.SelectedRow.Cells(5).Text).Selected = True

            Dim Qryy As String
            Qryy = "Select Distinct City_Kid,City_Name from City_Master " & _
                      " where City_StateID='" & ddlStateName.SelectedItem.Value & "' " & _
                      " and City_IsDeleted='0' and City_CompanyID='" & g_Company & "' order by City_Name "
            ddlCityName.Items.Clear()
            Fill_Combo(Qryy, ddlCityName)
            ddlCityName.ClearSelection()
            ddlCityName.Items.FindByText(gvLoc.SelectedRow.Cells(4).Text).Selected = True
            UnLockTextBox(True)                                             'Enable the TextFields
            Call GridClick(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False
            txtLocName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                 'Display the Error Message
        End Try
    End Sub
    Protected Sub ddlCountryName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountryName.SelectedIndexChanged
        Try
            Dim Qry As String
            Qry = "Select Distinct State_Kid,State_Name from State_Master " & _
                  " where State_CountryID='" & ddlCountryName.SelectedItem.Value & "' " & _
                  " and State_IsDeleted='0' and State_CompanyID='" & g_Company & "' order by State_Name "
            Fill_Combo(Qry, ddlStateName)
            ddlCityName.Items.Clear()
            Call FillGrid("", "Country")
            ddlStateName.Focus()
            Qry = Nothing
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try

    End Sub

    Protected Sub ddlStateName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStateName.SelectedIndexChanged
        Dim Qry As String
        Try
            Qry = "Select Distinct City_Kid,City_Name from City_Master " & _
                  " where City_StateID='" & ddlStateName.SelectedItem.Value & "' " & _
                  " and City_IsDeleted='0' and City_CompanyID='" & g_Company & "' order by City_Name "
            Fill_Combo(Qry, ddlCityName)
            Call FillGrid("", "State")
            ddlCityName.Focus()
            Qry = Nothing
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub
    Protected Sub ddlCityName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Call FillGrid("", "City")
            ddlCityName.Focus()
        Catch Ex As Exception
            CreateMessageAlert(Me, Ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub txtLocName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLocName.TextChanged
        If Search = True Then                               'If Search Mode is true then
            Call FillGrid(Trim(txtLocName.Text), "")  'Fill the Grid filtering on txtStockLocationName 
        End If
        If BtnSave.Enabled Then BtnSave.Focus()
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="FillGrid">
    '   <Desc> For Filling the GridView </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 21/06/2007 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Sub FillGrid(ByVal strID As String, ByVal strCTR As String)
        Try
            Dim strQuery As String
            strQuery = "select distinct Location_Kid,Location_Code as Location_Code,Location_Name as Location_Name,City_Name as CName,State_Name as SName,Country_Name as [Country] from location_Master,City_Master," & _
                           "State_Master,Country_Master where Location_CityID=City_Kid and City_StateID=State_Kid and State_CountryID=Country_Kid and Location_Master.Location_IsDeleted='0' " & _
                           "and Location_Master.Location_CompanyID='" & g_Company & "' and City_Master.City_IsDeleted='0' and City_Master.City_CompanyID='" & g_Company & "' and State_Master.State_IsDeleted='0' and State_Master.State_CompanyID='" & g_Company & "' and Country_Master.Country_IsDeleted='0' and Country_Master.Country_CompanyID='" & g_Company & "'"
            If strID.Trim.Length > 0 Then
                strQuery &= " and Location_Master.Location_Name like'" & strID.Trim & "%' order by Location_Master.Location_Name"
            End If
            If strCTR.Trim = "Country" Then
                strQuery &= " and Country_Master.Country_Kid='" & ddlCountryName.SelectedValue.Trim & "' order by Location_Master.Location_Name"
            End If
            If strCTR.Trim = "State" Then
                strQuery &= " and State_Master.State_Kid='" & ddlStateName.SelectedValue.Trim & "' order by Location_Master.Location_Name"
            End If
            If strCTR.Trim = "City" Then
                strQuery &= " and City_Master.City_Kid='" & ddlCityName.SelectedValue.Trim & "' order by Location_Master.Location_Name"
            End If
            gvLoc.Columns(1).Visible = True
            gvLoc.DataSource = getTable(strQuery.Trim, "Location")
            gvLoc.DataBind()
            gvLoc.Columns(1).Visible = False
            strQuery = Nothing
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="FillCombo">
    '   <Desc> For Filling the DropDownLists </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 21/06/2007 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------

    Private Sub FillCombo()
        Fill_Combo("Select Country_Kid,Country_Name from Country_Master where Country_IsDeleted='0'and Country_CompanyID='" & g_Company & "' order by Country_Name", ddlCountryName)
        ddlCountryName.ClearSelection()
        ddlCountryName.Items.FindByText("India").Selected = True
        Fill_Combo("Select State_Kid,State_Name from State_Master where State_IsDeleted='0' and State_CountryId = '" & ddlCountryName.SelectedItem.Value & "'and State_CompanyID='" & g_Company & "' order by State_Name", ddlStateName)
        ddlCityName.Items.Clear()
        'Fill_Combo("Select City_Kid,City_Name from City_Master where City_IsDeleted='0' and City_CompanyID='" & g_Company & "' order by City_Name", ddlCityName)
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="ClearFields">
    '   <Desc> For Clearing the contents of TextFields </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 21/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            txtLocCode.Text = ""                           'Assign Null to the TextField
            txtLocName.Text = ""
            txtLocId.Text = ""
            ddlCountryName.ClearSelection()
            ddlCountryName.SelectedIndex = 0
            ddlStateName.ClearSelection()
            ddlStateName.SelectedIndex = 0
            ddlCityName.ClearSelection()
            ddlCityName.SelectedIndex = 0
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="UnLockTextBox">
    '   <Desc> For Locking & UnLocking the TextFields </Desc>
    '   <Input> 
    '   <Param Name="Action">
    '   True or False
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 20/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            txtLocName.Enabled = Action                    'Enable / Disable the TextField
            ddlCountryName.Enabled = Action
            ddlStateName.Enabled = Action
            ddlCityName.Enabled = Action
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="UpdateData">
    '   <Desc> For Inserting,Updating and Delete Records from DataBase </Desc>
    '   <Input> 
    '   <Param Name="strMode">
    '   Insert,Update or Delete
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 20/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Private Sub UpdateData(ByVal strMode As String)
        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
        sqlcoll.Add("@Location_Kid", Data.SqlDbType.NVarChar, 50).Value = txtLocId.Text.Trim
        sqlcoll.Add("@Location_Code", Data.SqlDbType.NVarChar, 50).Value = txtLocCode.Text.Trim
        sqlcoll.Add("@Location_Name", Data.SqlDbType.NVarChar, 50).Value = txtLocName.Text.Trim
        sqlcoll.Add("@Location_CityID", Data.SqlDbType.NVarChar, 50).Value = ddlCityName.SelectedValue.Trim
        sqlcoll.Add("@Location_FinancialYearID", Data.SqlDbType.NVarChar, 50).Value = g_FinYear
        sqlcoll.Add("@Location_CompanyID", Data.SqlDbType.NVarChar, 50).Value = g_Company
        sqlcoll.Add("@Location_UserID", Data.SqlDbType.NVarChar, 50).Value = g_User
        sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = Me.Title.ToString
        sqlcoll.Add("@Mode", Data.SqlDbType.Char, 15).Value = strMode.Trim
        If ExecQuery("Location_Master_Proc", sqlcoll).StartsWith("True") Then
            FillGrid("", "")
            cmd = Nothing
            sqlcoll = Nothing
        End If
    End Sub
    Private Function CheckValidData(ByVal strSender As String) As Boolean
        If Trim(txtLocCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
            CreateMessageAlert(Me, "Location Code Cannot be Null !", "StrKeyVal")      'Display the Message 
            Exit Function
        End If
        If Trim(txtLocName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
            CreateMessageAlert(Me, "Location Name Cannot be Null !", "StrKeyVal")      'Display the Message
            Return False
            Exit Function
        End If
        If strSender.Trim = "Add" Then
            If IsAlreadyPresent("select count(*) from Location_Master where Location_Code='" & txtLocCode.Text.Trim & "'and Location_isdeleted='0' and Location_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Location Code Already Present !", "StrKeyVal")      'Display the Message
                txtLocCode.Text = "" : txtLocCode.Focus()
                Return False
                Exit Function
            End If
        End If

        If IsAlreadyPresent("select count(*) from Location_Master where Location_Name='" & txtLocName.Text.Trim & "'and Location_isdeleted='0' and Location_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
            CreateMessageAlert(Me, " Location Name Already Present !", "StrKeyVal")      'Display the Message
            txtLocName.Text = "" : txtLocName.Focus()
            Return False
            Exit Function
        End If
        If ddlCountryName.SelectedIndex = 0 Then
            CreateMessageAlert(Me, " Select Country !", "StrKeyVal")      'Display the Message
            ddlCountryName.Focus()
            Return False
            Exit Function
        End If
        If ddlCityName.SelectedIndex = 0 Then
            CreateMessageAlert(Me, " Select City !", "StrKeyVal")      'Display the Message
            ddlCityName.Focus()
            Return False
            Exit Function
        End If
        If ddlStateName.SelectedIndex = 0 Then
            CreateMessageAlert(Me, " Select State !", "StrKeyVal")      'Display the Message
            ddlStateName.Focus()
            Return False
            Exit Function
        End If
        Return True
    End Function

   
End Class
