Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class AdressenDetailControlQuick

#Region "Properties"

    Public Property IDNR As String
        Get
            Return IDNRTextBox.Text
        End Get
        Set(value As String)
            If IDNRTextBox.Text <> value Then
                IDNRTextBox.Text = value
            End If
        End Set
    End Property



    Public Property Firma As String
        Get
            Return FirmaTextBox.Text
        End Get
        Set(value As String)
            FirmaTextBox.Text = value
        End Set
    End Property


    Public Property NameFamilie As String
        Get
            Return NameTextBox.Text
        End Get
        Set(value As String)
            NameTextBox.Text = value
        End Set
    End Property

    Public Property Vorname As String
        Get
            Return VornameTextBox.Text
        End Get
        Set(value As String)
            VornameTextBox.Text = value
        End Set
    End Property

    Public Property Name1 As String
        Get
            Return Name1TextBox.Text
        End Get
        Set(value As String)
            Name1TextBox.Text = value
        End Set
    End Property

    Public Property Land As String
        Get
            Return LandComboBox.SelectedValue
        End Get
        Set(value As String)
            If value IsNot Nothing And Not value = "" Then LandComboBox.SelectedValue = value
        End Set
    End Property

    Public Property PLZ As String
        Get
            Return PLZComboBox.Text
        End Get
        Set(value As String)
            PLZComboBox.Text = value
        End Set
    End Property

    Public Property Ort As String
        Get
            Return OrtComboBox.Text
        End Get
        Set(value As String)
            OrtComboBox.Text = value
        End Set
    End Property

    Public Property Adresse As String
        Get
            If TextBoxNr.Text <> "" Then
                Return AdresseTextBox.Text & " " & TextBoxNr.Text
            End If
            Return AdresseTextBox.Text
        End Get
        Set(value As String)
            AdresseTextBox.Text = value
        End Set
    End Property

    Public Property Tel As String
        Get
            Return TelTextBox.Text
        End Get
        Set(value As String)
            TelTextBox.Text = value
            ButtonCall.Visible = IsTelefonNumber(value)
        End Set
    End Property

    Public Property Mobil As String
        Get
            Return MobilTextBox.Text
        End Get
        Set(value As String)
            MobilTextBox.Text = value
            ButtonCallMob.Visible = IsTelefonNumber(value)
        End Set
    End Property

    Public Property Email As String
        Get
            Return EmailTextBox.Text
        End Get
        Set(value As String)
            EmailTextBox.Text = value
        End Set
    End Property

#End Region

    Dim fPLZ As New FunctionsPLZ

    Private Sub Control_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        fPLZ.Init(LandComboBox, PLZComboBox, OrtComboBox, AdresseTextBox)
        fPLZ.FillLand()
    End Sub

    Private Sub ButtonPLZSelect_Click(sender As Object, e As System.EventArgs) Handles ButtonPLZSelect.Click
        Try
            Dim plz As New PLZSelector With {
                .Ort = OrtComboBox,
                .PLZ = PLZComboBox,
                .Land = LandComboBox
            }
            plz.ShowDialog()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub PLZComboBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles PLZComboBox.Validating
        Try
            If LandComboBox.SelectedValue IsNot Nothing Then
                Dim ort = FirstRow("select Ort from grPLZ where Land =" & LandComboBox.SelectedValue & " and PLZ ='" & PLZComboBox.SelectedValue & "'")
                If Not IsNull(ort) Then
                    OrtComboBox.Text = ort
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub LandComboBox_Leave(sender As Object, e As EventArgs) Handles LandComboBox.Leave
        PLZComboBox.Focus()
    End Sub

    Private Sub PLZComboBox_Leave(sender As Object, e As EventArgs) Handles PLZComboBox.Leave
        'OrtComboBox.Focus()
    End Sub

    Private Sub OrtComboBox_Leave(sender As Object, e As EventArgs) Handles OrtComboBox.Leave
        'AdresseTextBox.Focus()
    End Sub

    Private Sub AdresseTextBox_Leave(sender As Object, e As EventArgs) Handles AdresseTextBox.Leave
        'TelTextBox.Focus()
    End Sub

    Private Sub EmailTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles EmailTextBox.Validating

        Dim errorMsg As String = ""
        If Not ValidEmailAddress(EmailTextBox.Text, errorMsg) Then
            ' Cancel the event and select the text to be corrected by the user.
            e.Cancel = True
            EmailTextBox.Select(0, EmailTextBox.Text.Length)

            ' Set the ErrorProvider error with the text to display. 
            ErrorProvider1.SetError(EmailTextBox, errorMsg)
        End If
    End Sub


    Private Sub EmailTextBox_Validated(sender As Object, e As System.EventArgs) Handles EmailTextBox.Validated
        ' If all conditions have been met, clear the error provider of errors.
        ErrorProvider1.SetError(EmailTextBox, "")
    End Sub

    Public Function ValidateEmail(strCheck As String) As Boolean
        Try
            Dim vEmailAddress As New System.Net.Mail.MailAddress(strCheck)
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    Private Function ValidEmailAddress(emailAddress As String, ByRef errorMessage As String) As Boolean
        Try

            ' Confirm there is text in the control.
            If EmailTextBox.Text.Length = 0 Then
                ' errorMessage = "E-mail address is required."
                Return True
            End If

            ' Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
            If emailAddress.IndexOf("@") > -1 Then
                If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) Then
                    errorMessage = ""
                    Return True
                End If
            End If

            errorMessage = "Die E-mail-Addresse ist nicht gültig." + ControlChars.Cr + _
              "Beispiel 'someone@example.com' "
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub ButtonZustellgebiete_Click(sender As Object, e As EventArgs) Handles ButtonZustellgebiete.Click
        Try
            Dim zgb = New Zustellgebiete With {
                .Ort = OrtComboBox,
                .PLZ = PLZComboBox,
                .Land = LandComboBox,
                .Strasse = AdresseTextBox
            }
            zgb.ShowDialog()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EmailTextBox_Leave(sender As Object, e As EventArgs) Handles EmailTextBox.Leave
        'Me.Parent.Focus()
    End Sub

    Private Sub TableLayoutPanel1_Enter(sender As Object, e As EventArgs) Handles TableLayoutPanel1.Enter
        NameTextBox.Focus()
    End Sub

    Private Sub ButtonCall_Click(sender As Object, e As EventArgs) Handles ButtonCall.Click
        Try
            ModuleGlobal.RequestCommand(New IntraSellCallCommand With {.Number = Tel}) '("call;" & Tel)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ButtonCallMob_Click(sender As Object, e As EventArgs) Handles ButtonCallMob.Click
        Try
            ModuleGlobal.RequestCommand(New IntraSellCallCommand With {.Number = Mobil}) '("call;" & Mobil)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub MobilTextBox_TextChanged(sender As Object, e As EventArgs) Handles MobilTextBox.TextChanged
        ButtonCallMob.Visible = IsTelefonNumber(Mobil)
    End Sub

    Private Sub TelTextBox_TextChanged(sender As Object, e As EventArgs) Handles TelTextBox.TextChanged
        ButtonCall.Visible = IsTelefonNumber(Tel)
    End Sub

    Private Sub TextBoxNr_Validated(sender As Object, e As EventArgs) Handles TextBoxNr.Validated

    End Sub

    Private Sub IDNRTextBox_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles IDNRTextBox.MouseDoubleClick
        OeffneKundenMaske(IDNR, True)
    End Sub

    Private Sub AdressenDetailControl_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
        Try
            PLZComboBox.SelectionLength = 0
            OrtComboBox.SelectionLength = 0
            LandComboBox.SelectionLength = 0
            AdresseTextBox.SelectionLength = 0
        Catch ex As Exception

        End Try
    End Sub
End Class
