Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Partial Class BusinessAssociates_SSARegistration
    Inherits System.Web.UI.Page
    Shared g_User As String
    Shared g_Company As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblMsg1.Visible = False
        lblMsg2.Visible = False
        If (Not IsPostBack) Then
            g_User = "000008I"
            g_Company = "00006Z4"

            txtOtherMedium.Visible = False
            FillCity()
        End If
    End Sub

    Public Sub FillCity()
        ddlCurrentLocation.Items.Clear()
        Fill_Combo("Select City_Kid, City_Name from City_Master where City_IsDeleted='0' and City_CompanyId='" & g_Company.Trim & "' order by City_Name", ddlCurrentLocation)
        lstPreferredLocation.Items.Clear()
        Fill_ListBox("Select City_Kid, City_Name from City_Master where City_IsDeleted='0' and City_CompanyId='" & g_Company.Trim & "' order by City_Name", lstPreferredLocation)
    End Sub

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim DocumentStr1, DocumentStr2, DocumentStr3 As String

        DocumentStr1 = ""
        DocumentStr2 = ""
        DocumentStr3 = ""

        If (Document1.HasFile) Then
            If Path.GetExtension(Document1.FileName).ToLower = ".pdf" Then
                DocumentStr1 = UploadDocument(Document1)
                If DocumentStr1.Trim = "" Then
                    Exit Sub
                End If
            Else
                CreateMessageAlert(Me, "Please Select .pdf File Only!", "StrKeyVal")             'Display the Message
                Document1.Focus()
                Exit Sub
            End If
        End If

        If (Document2.HasFile) Then
            If Path.GetExtension(Document2.FileName).ToLower = ".pdf" Then
                DocumentStr2 = UploadDocument(Document2)
                If DocumentStr2.Trim = "" Then
                    Exit Sub
                End If
            Else
                CreateMessageAlert(Me, "Please Select .pdf File Only!", "StrKeyVal")             'Display the Message
                Document2.Focus()
                Exit Sub
            End If
        End If

        If (Document3.HasFile) Then
            If Path.GetExtension(Document3.FileName).ToLower = ".pdf" Then
                DocumentStr3 = UploadDocument(Document3)
                If DocumentStr3.Trim = "" Then
                    Exit Sub
                End If
            Else
                CreateMessageAlert(Me, "Please Select .pdf File Only!", "StrKeyVal")             'Display the Message
                Document3.Focus()
                Exit Sub
            End If
        End If

        Dim PreferredLocList As String


        PreferredLocList = ""

        Dim I As Integer

        For I = 1 To lstPreferredLocation.Items.Count - 1
            If lstPreferredLocation.Items(I).Selected = True Then
                PreferredLocList = PreferredLocList & lstPreferredLocation.Items(I).Text & ","
            End If
        Next

        Try
            txtSSAID.Text = System.Guid.NewGuid.ToString



            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim con As New Data.SqlClient.SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

            cmd.CommandText = "Insert_SSA_Proc"
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.Connection = con

            con.Open()
            cmd.Parameters.Add("@SSAID", Data.SqlDbType.VarChar).Value = txtSSAID.Text.Trim
            cmd.Parameters.Add("@FirstName", Data.SqlDbType.NVarChar, 100).Value = txtFirstName.Text.Trim
            cmd.Parameters.Add("@LastName", Data.SqlDbType.NVarChar, 100).Value = txtLastName.Text.Trim

            If ddlGender.SelectedIndex > 0 Then
                cmd.Parameters.Add("@Gender", Data.SqlDbType.NVarChar, 50).Value = ddlGender.SelectedValue
            End If

            If txtDateOfBirth.Text.Trim <> "" Then
                If IsDate(txtDateOfBirth.Text.Trim) Then
                    cmd.Parameters.Add("@DateOFBirth", Data.SqlDbType.DateTime).Value = txtDateOfBirth.Text.Trim
                Else
                    showmessage("Please enter valid date", lblErrorData1)
                    Exit Sub
                End If
            Else
            End If

            If ddlMedium.SelectedValue = "Other" Then
                If txtOtherMedium.Text.Trim <> "" Then
                    cmd.Parameters.Add("@Medium", Data.SqlDbType.NVarChar, 100).Value = txtOtherMedium.Text.Trim
                Else
                    showmessage("Please Enter Medium", lblErrorData1)
                    txtOtherMedium.Focus()
                    Exit Sub
                End If
            Else
                If ddlMedium.SelectedIndex > 0 Then
                    cmd.Parameters.Add("@Medium", Data.SqlDbType.NVarChar, 100).Value = ddlMedium.SelectedValue
                End If
            End If

            If ddlEduQualification.SelectedIndex > 0 Then
                cmd.Parameters.Add("@EduQualification", Data.SqlDbType.NVarChar, 100).Value = ddlEduQualification.SelectedValue
            End If

            If ddlCurrentLocation.SelectedIndex > 0 Then
                cmd.Parameters.Add("@CurrentLocation", Data.SqlDbType.NVarChar, 100).Value = ddlCurrentLocation.SelectedItem.Text.Trim
            End If

            cmd.Parameters.Add("@ProfQualification", Data.SqlDbType.NVarChar, 100).Value = txtProfQualification.Text.Trim

            cmd.Parameters.Add("@PreferredLocation", Data.SqlDbType.NVarChar, 100).Value = PreferredLocList.Trim
            cmd.Parameters.Add("@Address", Data.SqlDbType.NVarChar, 100).Value = txtAddress.Text.Trim
            cmd.Parameters.Add("@EmailAddress", Data.SqlDbType.NVarChar, 100).Value = txtEmailID.Text.Trim

            If ddlCurrentEmployment.SelectedIndex > 0 Then
                If ddlCurrentEmployment.SelectedValue = "Yes" Then
                    cmd.Parameters.Add("@CurrentEmployment", Data.SqlDbType.Bit).Value = 1
                ElseIf ddlCurrentEmployment.SelectedValue = "No" Then
                    cmd.Parameters.Add("@CurrentEmployment", Data.SqlDbType.Bit).Value = 0
                End If
            End If

            cmd.Parameters.Add("@CompanyName", Data.SqlDbType.NVarChar, 100).Value = txtCompanyName.Text.Trim
            cmd.Parameters.Add("@Designation", Data.SqlDbType.NVarChar, 100).Value = txtDesignation.Text.Trim

            If txtJoiningDate.Text.Trim <> "" Then
                If IsDate(txtJoiningDate.Text.Trim) Then
                    cmd.Parameters.Add("@JoiningDate", Data.SqlDbType.DateTime).Value = txtJoiningDate.Text.Trim
                Else
                    showmessage("Please enter valid date", lblErrorData1)
                    txtJoiningDate.Focus()
                    Exit Sub
                End If
            End If

            cmd.Parameters.Add("@Experience", Data.SqlDbType.NVarChar, 100).Value = txtExperience.Text.Trim

            cmd.Parameters.Add("@Document1", Data.SqlDbType.NVarChar, 100).Value = DocumentStr1.Trim
            cmd.Parameters.Add("@Document2", Data.SqlDbType.NVarChar, 100).Value = DocumentStr2.Trim
            cmd.Parameters.Add("@Document3", Data.SqlDbType.NVarChar, 100).Value = DocumentStr3.Trim
            cmd.Parameters.Add("@Password", Data.SqlDbType.NVarChar, 100).Value = ""
            cmd.Parameters.Add("@Status", Data.SqlDbType.NVarChar, 50).Value = "Pending"
            cmd.Parameters.Add("@CompanyID", Data.SqlDbType.NVarChar, 10).Value = "00006Z4"
            cmd.Parameters.Add("@FormName", Data.SqlDbType.NVarChar, 100).Value = Me.Title.ToString
            cmd.Parameters.Add("@Mode", Data.SqlDbType.NVarChar, 100).Value = "Insert"

            cmd.ExecuteNonQuery()

            clearfields()
            showmessage("We value your time and effort to get registered with us. We will get back to you shortly and confirm your registration as SSA", lblErrorData1)
            'email()
        Catch ex As Exception
            showmessage("Please try again later.", lblErrorData1)
        Finally
            con.Close()
            con.Dispose()
        End Try



    End Sub

    Protected Sub clearfields()
        Try

            txtCompanyName.Text = ""
            txtAddress.Text = ""
            txtDateOfBirth.Text = ""
            txtDesignation.Text = ""
            txtEmailID.Text = ""
            txtExperience.Text = ""
            txtFirstName.Text = ""
            txtJoiningDate.Text = ""
            txtlastname.Text = ""
            txtOtherMedium.Text = ""
            txtProfQualification.Text = ""
            ddlCurrentEmployment.SelectedIndex = 0
            ddlCurrentLocation.SelectedIndex = 0
            ddlEduQualification.SelectedIndex = 0
            ddlgender.SelectedIndex = 0
            ddlmedium.SelectedIndex = 0
        Catch ex As Exception
        End Try
    End Sub

    Public Sub email()
        Dim StrMail As String = ""
        Dim objmessage As New MailMessage()
        StrMail = "<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">"
        StrMail = StrMail & "<html>" & "<head>" & "<meta http-equiv=""Content-Type"""
        StrMail = StrMail & "<content=""text/html""; charset=""iso-8859-1"">"
        StrMail = StrMail & "<title>Contact Us</title>" & "</head><body><P align=left>"
        StrMail = StrMail & "<table width='739' border='0' cellspacing='0' cellpadding='0' align='left' >"
        StrMail = StrMail & "<tr><td>"
        StrMail = StrMail & "<B>" & "New Sales And Service Associate Registration :-" & "</B>"
        StrMail = StrMail & "</td></tr><tr><td>"
        StrMail = StrMail & "Name :" & txtFirstName.Text.Trim & " " & txtLastName.Text.Trim
        StrMail = StrMail & "</td></tr><tr><td>"
        StrMail = StrMail & "Email :" & txtEmailID.Text.Trim
        StrMail = StrMail & "</td></tr></table></p></body></html>"
        objmessage.Subject = "New Sales And Service Associate Registration"
        objmessage.Body = StrMail
        objmessage.From = txtEmailID.Text.Trim
        objmessage.To = "sushma@atcomaart.com"
        ' objmessage.Cc = "rajeshmishra@atcomaart.com;ramendra@atcomaart.com"
        objmessage.BodyFormat = MailFormat.Html
        SmtpMail.SmtpServer = "atcoex02"
        SmtpMail.Send(objmessage)
    End Sub

    Private Function UploadDocument(ByVal FileUpload As FileUpload) As String
        Dim strFileName As String
        Dim Name As String
        strFileName = Mid(FileUpload.FileName.ToString, FileUpload.FileName.ToString.LastIndexOf("\") + 2, Len(FileUpload.FileName.ToString))

        Dim MyPath As String
        If strFileName.Trim <> "" Then
            Name = System.IO.Path.GetFileName(strFileName)
            MyPath = Server.MapPath("~/BusinessAssociates/Documents/") + Name
            If IO.File.Exists(MyPath) Then
                showmessage(Name & " is already Exists. Save it in Different Name", lblErrorData1)
                Return ""
                Exit Function
            End If
            FileUpload.SaveAs(MyPath)
            Return Name
        End If
    End Function


    Protected Sub ddlMedium_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If ddlMedium.SelectedIndex > 0 Then
            If ddlMedium.SelectedValue = "Other" Then
                txtOtherMedium.Visible = True
                txtOtherMedium.Focus()
            Else
                txtOtherMedium.Visible = False
                ddlEduQualification.Focus()
            End If
        End If
    End Sub
End Class
