﻿Imports System.Net.Mail
Imports System.IO
Imports System.Net
Imports System.Web.Script.Serialization

Partial Class Contact
    Inherits System.Web.UI.Page
    Dim CMS As New CMS
    Dim email As New Email

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        errorhelp.Visible = False
        errorIndustry.Visible = False
        errorPartner.Visible = False
        nameerror.Visible = False
        mailerror.Visible = False
        validerror.Visible = False
        whenerror.Visible = False
        If Page.IsPostBack Then

        Else

        End If
    End Sub

    Private Sub clearform()
        errorhelp.Visible = False
        errorIndustry.Visible = False
        errorPartner.Visible = False
        nameerror.Visible = False
        mailerror.Visible = False
        validerror.Visible = False
        whenerror.Visible = False
        txtname.Text = ""
        txtEmail.Text = ""
        txtPhone.Text = ""
        txtCompany.Text = ""
        txtWeb.Text = ""
        txtTitle.Text = ""
        txtindustry.Text = ""
        txthelp.Text = ""
        TextMessage.Text = ""
        industrylist.ClearSelection()
        growthlist.ClearSelection()
        helplist.ClearSelection()
        engagementlist.ClearSelection()
        agencybtn.ClearSelection()
        partnershiplist.ClearSelection()
    End Sub

    Public Class MyObject
        Public Property success() As String
            Get
                Return m_success
            End Get
            Set(value As String)
                m_success = value
            End Set
        End Property
        Private m_success As String
    End Class

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles submit.Click



        Dim check = True
        Dim Response As String = Request("g-recaptcha-response")
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(Convert.ToString("https://www.recaptcha.net/recaptcha/api/siteverify?secret=6LczYqgUAAAAAM0FvHVKTi9FOHXhwLsGBKL9yuSl&response=") & Response), HttpWebRequest)

        Try
            Using wResponse As WebResponse = req.GetResponse()

                Using readStream As New StreamReader(wResponse.GetResponseStream())
                    Dim jsonResponse As String = readStream.ReadToEnd()
                    Dim js As New JavaScriptSerializer()
                    Dim data As MyObject = js.Deserialize(Of MyObject)(jsonResponse)

                    check = Convert.ToBoolean(data.success)
                End Using
            End Using
        Catch ex As Exception
            check = False
        End Try

        Dim name As String = Server.HtmlEncode(txtname.Text.Trim)
        If name = "" Then
            nameerror.Visible = True
            Check = False
        Else
            nameerror.Visible = False
        End If
        Dim emailstring As String = Server.HtmlEncode(txtEmail.Text.Trim)
        Dim emailvalidation As Boolean = ValidateEmail(emailstring)
        If emailstring = "" Then
            mailerror.Visible = True
            Check = False
        Else
            mailerror.Visible = False
            If emailvalidation Then
                validerror.Visible = False
            Else
                validerror.Visible = True
                Check = False
            End If
        End If

        Dim contact_no As String = Server.HtmlEncode(txtPhone.Text.Trim)
        Dim company As String = Server.HtmlEncode(txtCompany.Text.Trim)
        Dim web As String = Server.HtmlEncode(txtWeb.Text.Trim)
        Dim title As String = Server.HtmlEncode(txtTitle.Text.Trim)

        Dim industry As String = ""
        For Each ckbox In industrylist.Items
            If ckbox.Selected Then
                industry += ckbox.Text.ToString + "."
                If Server.HtmlEncode(txthelp.Text.Trim) <> "" Then
                    industry += "Others:" + txtindustry.Text
                End If
            End If
        Next
        If industry = "" Then
            errorIndustry.Visible = True
            Check = False
        Else
            errorIndustry.Visible = False
        End If

        Dim growth As String = Server.HtmlEncode(growthlist.SelectedValue)

        Dim help As String = ""
        For Each ckbox In industrylist.Items
            If ckbox.Selected Then
                help += ckbox.Text.ToString + "."
            End If
        Next
        If help = "" Then
            errorhelp.Visible = True
            Check = False
        Else
            errorhelp.Visible = False
        End If

        Dim engagement As String = Server.HtmlEncode(engagementlist.SelectedValue)

        Dim time As String = Server.HtmlEncode(txthelp.Text.Trim)
        If time = "" Then
            whenerror.Visible = True
            Check = False
        Else
            whenerror.Visible = False
        End If

        Dim agency As String = Server.HtmlEncode(agencybtn.SelectedValue)

        Dim value As String = ""
        Dim valuecount As Int16 = 0
        For Each ckbox In partnershiplist.Items
            If ckbox.Selected Then
                value += ckbox.Text.ToString + "."
                valuecount += 1
            End If
        Next
        If value = "" Or valuecount <> 3 Then
            errorPartner.Visible = True
            Check = False
        Else
            errorPartner.Visible = False
        End If

        If Check Then
            If Email.sendMail(name, emailstring, contact_no, company, web, title, industry, growth, help, engagement, time, agency, value, Server.HtmlEncode(TextMessage.Text.Trim)) Then
                clearform()
                MsgBox("Thank You for your Enquiry", "We will get back to you soon.", "success")
            Else
                MsgBox("There was an error processing your request.", "", "error")
            End If
        Else
            MsgBox("Missing Fields", "", "error")
            ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", " $('html, body').animate({scrollTop: $('#contact-wrapper-right').offset().top}, 1000);", True)
        End If
    End Sub

    Private Sub MsgBoxInformRedirect(ByVal header As String, ByVal msg As String, ByVal type As String, ByVal location As String)
        'Begin building the script
        Dim strScript As String = "<script nonce='7476TA28N3uUHP90oW0f9l4683Y' type=""text/javascript"">" & vbCrLf
        strScript += "swal({title: '" & header & "',text: '" & msg & "', type:'" & type & "', confirmButtonColor: 'red'},function(){window.location.href = '" & location & "';});" & vbCrLf
        strScript += "</script>"
        'Register the script for the client side
        ClientScript.RegisterStartupScript(GetType(String), "MsgBoxRedirect", strScript)
    End Sub

    Private Sub MsgBox(ByVal header As String, ByVal msg As String, ByVal type As String)
        'Begin building the script
        Dim strScript As String = "<script nonce='7476TA28N3uUHP90oW0f9l4683Y' type=""text/javascript"">" & vbCrLf
        strScript += "swal({title: '" & header & "',text: '" & msg & "', type:'" & type & "', confirmButtonColor: 'red'});" & vbCrLf
        strScript += "</script>"
        'Register the script for the client side
        ClientScript.RegisterStartupScript(GetType(String), "MsgBox", strScript)
    End Sub

    Public Function ValidateEmail(ByVal 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
End Class


