Partial Class Country_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()
                FillGrid("")                  'Fills the DatagGrid with All Records
                Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, g_User, "Country Master")
                BtnDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            End If
            txtCountryCode.MaxLength = 20 : txtCountryName.MaxLength = 50
            'txtCountryName.Attributes.Add("onblur", "return CheckNull('txtCityName');")
            txtCountryName.Attributes.Add("onkeypress", "return BlockNumericValue(event);")
            '  txtCityName.Attributes.Add("onblur", "return toUpper('txtCityName');")
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub gvCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvCountry.SelectedIndexChanged
        Try
            txtCountryId.Text = gvCountry.SelectedRow.Cells(1).Text     'Assign the value of cell to the textField
            txtCountryCode.Text = gvCountry.SelectedRow.Cells(2).Text     'Assign the value of cell to the textField
            txtCountryName.Text = gvCountry.SelectedRow.Cells(3).Text     'Assign the value of cell to the textField
            UnLockTextBox(True)                                             'Enable the TextFields
            Call GridClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False
            txtCountryName.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                 'Display the Error Message
        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
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(True)                                 'Enable the TextFields
            Call AddClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtCountryId.Text = NewPrimaryKey(Me)
            txtCountryCode.Text = Generate_Code("select count(*) from Country_Master where Country_Code='", "CNT")
            txtCountryName.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 Trim(txtCountryCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Country Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtCountryName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Country Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from Country_Master where Country_Code='" & txtCountryCode.Text.Trim & "'and Country_isdeleted='0' and Country_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Country Code Already Present !", "StrKeyVal")      'Display the Message
                txtCountryCode.Text = "" : txtCountryCode.Focus()
                Exit Sub
            End If
            If IsAlreadyPresent("select count(*) from Country_Master where Country_Name='" & txtCountryName.Text.Trim & "'and Country_isdeleted='0' and Country_CompanyID='" & g_Company & "'") Then ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Country Name Already Present !", "StrKeyVal")      'Display the Message
                txtCountryName.Text = "" : txtCountryName.Focus()
                Exit Sub
            End If
            UpdateData("Insert")   'Inserts the Record
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call SaveClickVisible(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 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 Trim(txtCountryCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Country Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If
            If Trim(txtCountryName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Country Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If

            If IsAlreadyPresent("select count(*) from country_Master where country_code!='" & Trim(txtCountryCode.Text) & "' and  country_Name='" & txtCountryName.Text.Trim & "'  and Country_isdeleted='0' and Country_CompanyID='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Country Name Already Present !", "StrKeyVal")      'Display the Message
                txtCountryName.Text = "" : txtCountryName.Focus()
                Exit Sub
            End If
            UpdateData("Update")  'Updates the record
            CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call EditClickVisible(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 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(txtCountryCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
            '    CreateMessageAlert(Me, "Country Code Cannot be Null !", "StrKeyVal")      'Display the Message 
            '    txtCountryCode.Focus()
            '    Exit Sub
            'End If
            'If Trim(txtCountryName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
            '    CreateMessageAlert(Me, "Country Name Cannot be Null !", "StrKeyVal")      'Display the Message 
            '    txtCountryName.Focus()
            '    Exit Sub
            'End If
            'If IsAlreadyPresent("select count(*) from Country_Master where Country_Code='" & txtCountryCode.Text.Trim & "'and Country_isdeleted='0' and Country_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 DeleteClickVisible(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
            txtCountryName.AutoPostBack = False
            Call FillGrid("")
            gvCountry.SelectedIndex = -1
            Call CancelClickVisible(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 SearchClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        txtCountryName.Enabled = True
        txtCountryName.AutoPostBack = True
        'Enable txtPartTypeName TextField
        Call txtCountryName_TextChanged(sender, e)
        txtCountryName.Focus()                             'Set Focus to txtPartTypeName TextField
    End Sub

    Protected Sub txtCountryName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCountryName.TextChanged
        If Search = True Then                               'If Search Mode is true then
            Call FillGrid(Trim(txtCountryName.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> 19/06/2007 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Sub FillGrid(ByVal strID As String)
        Try
            Dim strQuery As String
            If strID.Trim.Length = 0 Then
                strQuery = "select distinct country_Kid,Country_Code,Country_Name from Country_Master where Country_isdeleted='0' and Country_companyId='" & g_Company & "' Order by Country_Name"
            Else
                strQuery = "select distinct country_Kid,Country_Code,Country_Name from Country_Master where Country_isdeleted='0' and Country_companyId='" & g_Company & "' and Country_name like'" & strID.Trim & "%' Order by Country_Name"
            End If
            gvCountry.Columns(1).Visible = True
            gvCountry.DataSource = getTable(strQuery.Trim, "Country")
            gvCountry.DataBind()
            gvCountry.Columns(1).Visible = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="ClearFields">
    '   <Desc> For Clearing the contents of TextFields </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 19/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            txtCountryCode.Text = ""                           'Assign Null to the TextField
            txtCountryName.Text = ""
            txtCountryId.Text = ""                               'Assign Null to the TextField
        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> 19/06/07 </DateOfCreation>
    '   <Developer> Durga </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            txtCountryName.Enabled = Action                    'Enable / Disable the TextField
        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("@Country_Kid", Data.SqlDbType.NVarChar, 50).Value = txtCountryId.Text.Trim
        sqlcoll.Add("@Country_Code", Data.SqlDbType.NVarChar, 50).Value = txtCountryCode.Text.Trim
        sqlcoll.Add("@Country_Name", Data.SqlDbType.NVarChar, 50).Value = txtCountryName.Text.Trim
        sqlcoll.Add("@Country_FinancialYearID", Data.SqlDbType.NVarChar, 50).Value = g_FinYear
        sqlcoll.Add("@Country_CompanyID", Data.SqlDbType.NVarChar, 50).Value = g_Company
        sqlcoll.Add("@Country_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("Country_Master_Proc", sqlcoll).StartsWith("True") Then
            FillGrid("")
            cmd = Nothing
            sqlcoll = Nothing
        End If
    End Sub

   
    Protected Sub gvCountry_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvCountry.PageIndex = e.NewPageIndex
        FillGrid("")
        gvCountry.SelectedIndex = -1
        ClearFields()
        UnLockTextBox(False)
        Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
    End Sub

  
End Class