Option Explicit On 
Option Strict On

Imports System.Data
Imports System.Data.SqlClient
Imports System.DateTime
Imports System.Web.Mail


Public MustInherit Class AlumniForm
	Inherits System.Web.UI.UserControl
	Protected WithEvents txtLName As System.Web.UI.WebControls.TextBox
	Protected WithEvents lblLastNameError As System.Web.UI.WebControls.Label
	Protected WithEvents txtFName As System.Web.UI.WebControls.TextBox
	Protected WithEvents lblFirstNameError As System.Web.UI.WebControls.Label
	Protected WithEvents txtMaidenName As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtGraduationYear As System.Web.UI.WebControls.TextBox
	Protected WithEvents lblGraduationYearError As System.Web.UI.WebControls.Label
	Protected WithEvents txtPhone_Code As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPhone As System.Web.UI.WebControls.TextBox
	Protected WithEvents lblPhoneError As System.Web.UI.WebControls.Label
	Protected WithEvents txtEmail As System.Web.UI.WebControls.TextBox
	Protected WithEvents lblEmailError As System.Web.UI.WebControls.Label
	Protected WithEvents txtPAddress1 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPAddress2 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPCity As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPState As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPCountry As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCAddress1 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCAddress2 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCCity As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCState As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCCountry As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtComment As System.Web.UI.WebControls.TextBox
	Protected WithEvents btnSave As System.Web.UI.WebControls.Button
	Protected WithEvents txtPZipcode As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtCZipcode As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtHighSchool As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblHighSchoolError As System.Web.UI.WebControls.Label
    Protected WithEvents txtGraduationCollege As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblGraduationCollege As System.Web.UI.WebControls.Label
    Protected WithEvents chkSameasPermanent As System.Web.UI.WebControls.CheckBox

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
	End Sub

#End Region
	'these events helps the manipulation of evetns in this main.aspx form
	Public Event AlumniAdded()
	Public Event AlumniError()

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		chkSameasPermanent.Attributes.Add("onclick", "return same_as_permanent_address();")
	End Sub

	Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If txtLName.Text.Trim.Length <= 0 Then
			lblLastNameError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		Else
			lblLastNameError.Visible = False
		End If

		If txtFName.Text.Trim.Length <= 0 Then
			lblFirstNameError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		Else
			lblFirstNameError.Visible = False
		End If

		If txtGraduationYear.Text.Trim.Length <= 0 OrElse CType(Val(txtGraduationYear.Text), Integer) <= 0 Then
			lblGraduationYearError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		Else
			lblGraduationYearError.Visible = False
		End If

		If txtPhone_Code.Text.Trim.Length <= 0 Then
			lblPhoneError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		End If

		If txtPhone.Text.Trim.Length <= 0 Then
			lblPhoneError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		End If
		If txtPhone_Code.Text.Trim.Length > 0 And txtPhone.Text.Trim.Length > 0 Then
			lblPhoneError.Visible = False
		End If

		If txtEmail.Text.Trim.Length <= 0 Then
			lblEmailError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		Else
			lblEmailError.Visible = False
		End If

		Dim regEx As New System.Text.RegularExpressions.Regex("^[\w\-\.']+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*?\.[a-zA-Z]{2,4}$")
		If regEx.IsMatch(txtEmail.Text.Trim) = False Then
			lblEmailError.Text = "Invalid email address was entered"
			lblEmailError.Visible = True
			RaiseEvent AlumniError()
			Exit Sub
		End If

        If txtHighSchool.Text.Trim.Length <= 0 Then
            lblHighSchoolError.Visible = True
            RaiseEvent AlumniError()
            Exit Sub
        Else
            lblHighSchoolError.Visible = False
        End If

        If txtGraduationCollege.Text.Trim.Length <= 0 Then
            lblGraduationCollege.Visible = True
            RaiseEvent AlumniError()
            Exit Sub
        Else
            lblGraduationCollege.Visible = False
        End If

        Dim AutoID As Integer
        Dim Mailto As String
        Dim cmd As New SqlCommand
        Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))

        Try
            With cmd
                .CommandText = "Insert into as_AlumniForm (LastName,FirstName,MaidenName,YearGraduated,PermAddress1,PermAddress2,PermCity,PermState,PermZipcode,PermCountry," & _
                 "CurrentAddress1,CurrentAddress2,CurrentCity,CurrentState,CurrentZipcode,CurrentCountry,Email,PhoneExt,PhoneNo,Comments,PostDate,IP,HighSchool,GraduationCollege)" & _
                 "	Values(@LastName,@FirstName,@MaidenName,@YearGraduated,@PAddress1,@PAddress2,@PCity,@PState,@PZipcode,@PCountry," & _
                 "@CAddress1,@CAddress2,@CCity,@CState,@CZipcode,@CCountry,@Email,@PhoneExt,@PhoneNo,@Comments,@PostDate,@IP,@HighSchool,@GCollege); Select @@identity;"

                .Parameters.Add("@FirstName", SqlDbType.VarChar, 255)
                .Parameters("@FirstName").Value = txtFName.Text

                .Parameters.Add("@LastName", SqlDbType.VarChar, 255)
                .Parameters("@LastName").Value = txtLName.Text

                .Parameters.Add("@MaidenName", SqlDbType.VarChar, 255)
                .Parameters("@MaidenName").Value = txtMaidenName.Text

                .Parameters.Add("@YearGraduated", SqlDbType.Int, 4)
                .Parameters("@YearGraduated").Value = CType(txtGraduationYear.Text, Integer)

                .Parameters.Add("@PAddress1", SqlDbType.VarChar, 255)
                .Parameters("@PAddress1").Value = txtPAddress1.Text

                .Parameters.Add("@PAddress2", SqlDbType.VarChar, 255)
                .Parameters("@PAddress2").Value = txtPAddress2.Text

                .Parameters.Add("@PCity", SqlDbType.VarChar, 50)
                .Parameters("@PCity").Value = txtPCity.Text

                .Parameters.Add("@PState", SqlDbType.VarChar, 50)
                .Parameters("@PState").Value = txtPState.Text

                .Parameters.Add("@PZipcode", SqlDbType.VarChar, 10)
                .Parameters("@PZipcode").Value = txtPZipcode.Text

                .Parameters.Add("@PCountry", SqlDbType.VarChar, 50)
                .Parameters("@PCountry").Value = txtPCountry.Text

                .Parameters.Add("@CAddress1", SqlDbType.VarChar, 255)
                .Parameters("@CAddress1").Value = txtCAddress1.Text

                .Parameters.Add("@CAddress2", SqlDbType.VarChar, 255)
                .Parameters("@CAddress2").Value = txtCAddress2.Text

                .Parameters.Add("@CCity", SqlDbType.VarChar, 50)
                .Parameters("@CCity").Value = txtCCity.Text

                .Parameters.Add("@CState", SqlDbType.VarChar, 50)
                .Parameters("@CState").Value = txtCState.Text

                .Parameters.Add("@CZipcode", SqlDbType.VarChar, 10)
                .Parameters("@CZipcode").Value = txtCZipcode.Text

                .Parameters.Add("@CCountry", SqlDbType.VarChar, 50)
                .Parameters("@CCountry").Value = txtCCountry.Text

                .Parameters.Add("@PhoneExt", SqlDbType.VarChar, 3)
                .Parameters("@PhoneExt").Value = txtPhone_Code.Text

                .Parameters.Add("@PhoneNo", SqlDbType.VarChar, 25)
                .Parameters("@PhoneNo").Value = txtPhone.Text

                .Parameters.Add("@Email", SqlDbType.VarChar, 255)
                .Parameters("@Email").Value = txtEmail.Text

                .Parameters.Add("@Comments", SqlDbType.Text)
                .Parameters("@Comments").Value = txtComment.Text

                .Parameters.Add("@PostDate", SqlDbType.DateTime)
                .Parameters("@PostDate").Value = DateTime.Now

                .Parameters.Add("@IP", SqlDbType.VarChar, 20)
                .Parameters("@IP").Value = Request.ServerVariables("REMOTE_ADDR")

                .Parameters.Add("@HighSchool", SqlDbType.NVarChar, 100)
                .Parameters("@HighSchool").Value = txtHighSchool.Text

                .Parameters.Add("@GCollege", SqlDbType.NVarChar, 100)
                .Parameters("@GCollege").Value = txtGraduationCollege.Text

                .Connection = conn
                .CommandType = CommandType.Text
                If conn.State = ConnectionState.Closed Then conn.Open()
                'Response.Write(.CommandText & vbCrLf)
                AutoID = CInt(.ExecuteScalar())
            End With


            Dim cmd1 As New SqlCommand

            'now to send email to the admin notification emails
            With cmd1
                .Parameters.Clear()
                .CommandText = "Select AlumniEmails from as_AdminUsers where userid='admin'"
                .CommandType = CommandType.Text
                If conn.State = ConnectionState.Closed Then conn.Open()
                .Connection = conn
                Mailto = CType(.ExecuteScalar(), String)
            End With
        Catch ex As Exception
            'Response.Write(ex.ToString)
            RaiseEvent AlumniError()
            Exit Sub
        End Try

        'send email only if there is any 
        If Mailto.Trim.Length > 0 Then
            Try
                Dim email As New System.Web.Mail.MailMessage
                With email
                    .Subject = "Alumni Form Notification"
                    .BodyFormat = MailFormat.Html
                    .From = "Alumni Form Notification <" & CType(ConfigurationSettings.AppSettings("FROM_EMAIL"), String) & ">"
                    .To = Mailto
                    .Priority = MailPriority.High
                    .Body = "<HTML><Body><p><font face='verdana,arial' size=2>A new alumni form has been filled by <B>" & txtFName.Text & " " & txtLName.Text & "</b><BR><BR>Dated: " & System.DateTime.Now & "<BR><BR><BR>**Do not reply to this email</Body></HTML>"
                End With
                Mail.SmtpMail.SmtpServer = CType(ConfigurationSettings.AppSettings("SMTP_SERVER"), String)      '"localhost"


                Mail.SmtpMail.Send(email)
            Catch ex As Exception
                Response.Write(ex.ToString())
                Return
                'do nothing if you cannot send email
            End Try

        End If

        'raisse the event as the census is added now
        RaiseEvent AlumniAdded()


        '<SAQIB ADDED LINES>
        If conn.State = ConnectionState.Open Then conn.Close()
        '<SAQIB ADDED LINES/>
    End Sub
End Class
