Imports EDSL.DAL
Imports EDSL.Entities

Partial Public Class EmailAlertForm
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim sectorID As New Integer
            Dim locationID As New Integer

            Dim SectorValues As New dbMainDropDownFunctions
            Dim lstSector As List(Of entMainDropDown) = SectorValues.GetDropDownValues(7)

            ddlSector.Items.Add(New ListItem("Please Select..", ""))
            For Each sector As entMainDropDown In lstSector
                ddlSector.Items.Add(New ListItem(sector.TextDisplay, sector.DropDownID))
            Next

            Dim locationValues As New dbMainLocations
            Dim lstLocations As List(Of entMainLocations) = locationValues.GetAll()

            ddlLocation.Items.Add(New ListItem("Please Select..", ""))
            For Each location As entMainLocations In lstLocations
                ddlLocation.Items.Add(New ListItem(location.LocationValue, location.LocationID))
            Next

            If Request.QueryString("pid") <> "" Then
                'Get information of sectorID and location ID for the job
                Dim objJob As New dbMainProduct
                Dim entJob As New entMainProduct
                entJob = objJob.GetItem(Convert.ToInt32(Request.QueryString("pid")))

                With entJob
                    ddlSector.SelectedValue = .SectorID
                    ddlLocation.SelectedValue = .Location
                End With
            End If
            Dim salaryRange As New dbMainDropDownFunctions
            Dim lstSalaryRange As List(Of entMainDropDown) = salaryRange.GetDropDownValues(10)

            For Each salary As entMainDropDown In lstSalaryRange
                ddlSalaryFrom.Items.Add(New ListItem(salary.TextDisplay, salary.Value))
                ddlSalaryTo.Items.Add(New ListItem(salary.TextDisplay, salary.Value))

            Next
        End If
        If Request.QueryString("t") = "t" Then
            divEmailalertsform.Visible = False
            Dim pageInfo As HtmlControl = Me.Parent.FindControl("divContent")
            Dim pageHeader As HtmlControl = Me.Parent.FindControl("divPageHead")
            pageHeader.Visible = False
            pageInfo.Visible = False

            Dim dbMessage As New dbMainMessage
            divThankyou.Visible = True
            divThankyou.InnerHtml = dbMessage.GetMessage(1).ToString()
        End If

    End Sub
    Protected Sub customvalidator1_servervalidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = True
        Dim minSalary As Decimal = Convert.ToDecimal(ddlSalaryFrom.SelectedValue.ToString())
        Dim maxSalary As Decimal = Convert.ToDecimal(ddlSalaryTo.SelectedValue.ToString())

        If maxSalary < minSalary Then

            args.IsValid = False
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        ' Add the email alert to the database
        If Page.IsValid Then

            Dim successful As New Integer
            Dim emailAlert As New entMainEmailAlert
            Dim objEmailAlert As New dbMainEmailAlert
            Dim objMessage As New dbMainMessage

            With emailAlert
                Dim sectorID As New Integer
                Dim locationID As New Integer
                If ddlSector.SelectedValue.ToString() <> "" Then
                    sectorID = Convert.ToInt32(ddlSector.SelectedValue.ToString())
                End If
                If ddlLocation.SelectedValue.ToString() <> "" Then
                    locationID = Convert.ToInt32(ddlLocation.SelectedValue.ToString())
                End If

                .NameOfCandidate = txtName.Text
                .EmailAddress = txtEmail.Text
                .SalaryFrom = Convert.ToDecimal(ddlSalaryFrom.SelectedValue.ToString())
                .SalaryTo = Convert.ToDecimal(ddlSalaryTo.SelectedValue.ToString())
                .SectorID = sectorID
                .LocationID = locationID
                .AlertType = Convert.ToInt32(ddlFrequency.SelectedValue.ToString())

                successful = objEmailAlert.SaveItem(emailAlert)

                If successful = 0 Then
                    Response.Redirect("EmailAlert.aspx?t=t")
                Else
                    divThankyou.Visible = True
                    divThankyou.InnerHtml = objMessage.GetMessage(10).ToString()
                End If
            End With


        End If

    End Sub
End Class