Option Explicit On 
Option Strict On

Imports System.Data
Imports System.IO
Imports System.Data.SqlClient

Public Class SetSlogan
	Inherits System.Web.UI.Page
	Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
	Protected WithEvents txtSlogan As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtIntroLine1 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtIntroLine2 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtIntroLine3 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtIntroLine4 As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtIntroLine5 As System.Web.UI.WebControls.TextBox
	Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("sConnection"))
	Dim cmd As New SqlCommand()

#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

	Private Sub WriteIntroTextFiles()
		Dim txtPath As String = Server.MapPath(ConfigurationSettings.AppSettings("IntroPath"))
		Dim f1 As String = txtPath & ConfigurationSettings.AppSettings("file1") & ".txt"
		Dim f2 As String = txtPath & ConfigurationSettings.AppSettings("file2") & ".txt"
		Dim f3 As String = txtPath & ConfigurationSettings.AppSettings("file3") & ".txt"
		Dim f4 As String = txtPath & ConfigurationSettings.AppSettings("file4") & ".txt"
		Dim f5 As String = txtPath & ConfigurationSettings.AppSettings("file5") & ".txt"

		Dim swFile As File
		Dim sw As IO.StreamWriter
		If swFile.Exists(f1) Then swFile.Delete(f1)
		sw = swFile.CreateText(f1)
		sw.NewLine = vbCrLf
		sw.WriteLine(ConfigurationSettings.AppSettings("file1") & "=" & txtIntroLine1.Text.Trim)
		sw.Close()

		If swFile.Exists(f2) Then swFile.Delete(f2)
		sw = swFile.CreateText(f2)
		sw.NewLine = vbCrLf
		sw.WriteLine(ConfigurationSettings.AppSettings("file2") & "=" & txtIntroLine2.Text.Trim)
		sw.Close()

		If swFile.Exists(f3) Then swFile.Delete(f3)
		sw = swFile.CreateText(f3)
		sw.NewLine = vbCrLf
		sw.WriteLine(ConfigurationSettings.AppSettings("file3") & "=" & txtIntroLine3.Text.Trim)
		sw.Close()

		If swFile.Exists(f4) Then swFile.Delete(f4)
		sw = swFile.CreateText(f4)
		sw.NewLine = vbCrLf
		sw.WriteLine(ConfigurationSettings.AppSettings("file4") & "=" & txtIntroLine4.Text.Trim)
		sw.Close()

		If swFile.Exists(f5) Then swFile.Delete(f5)
		sw = swFile.CreateText(f5)
		sw.NewLine = vbCrLf
		sw.Write(ConfigurationSettings.AppSettings("file5") & "=" & txtIntroLine5.Text.Trim)
		sw.Close()
	End Sub

	Private Sub ReadIntroTextFiles()
		'Determine physical path of folder where the txt files are located
		Dim txtPath As String = Server.MapPath(ConfigurationSettings.AppSettings("IntroPath"))
		Dim f1 As String = txtPath & ConfigurationSettings.AppSettings("file1") & ".txt"
		Dim f2 As String = txtPath & ConfigurationSettings.AppSettings("file2") & ".txt"
		Dim f3 As String = txtPath & ConfigurationSettings.AppSettings("file3") & ".txt"
		Dim f4 As String = txtPath & ConfigurationSettings.AppSettings("file4") & ".txt"
		Dim f5 As String = txtPath & ConfigurationSettings.AppSettings("file5") & ".txt"

		Dim sr As IO.StreamReader = New IO.StreamReader(f1)
		Dim s As String = sr.ReadToEnd()
		Dim c(0) As Char
		c(0) = Chr(Asc("="))
		Dim sArr As String() = s.Split(c)
		txtIntroLine1.Text = sArr(1).Trim
		sr.Close()

		sr = New IO.StreamReader(f2)
		s = sr.ReadToEnd()
		sArr = s.Split(c)
		txtIntroLine2.Text = sArr(1).Trim
		sr.Close()

		sr = New IO.StreamReader(f3)
		s = sr.ReadToEnd()
		sArr = s.Split(c)
		txtIntroLine3.Text = sArr(1).Trim
		sr.Close()

		sr = New IO.StreamReader(f4)
		s = sr.ReadToEnd()
		sArr = s.Split(c)
		txtIntroLine4.Text = sArr(1).Trim
		sr.Close()

		sr = New IO.StreamReader(f5)
		s = sr.ReadToEnd()
		sArr = s.Split(c)
		txtIntroLine5.Text = sArr(1).Trim
		sr.Close()

	End Sub

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		If Not Page.IsPostBack Then
			Call ReadIntroTextFiles()

			conn.Open()
			With cmd
				.CommandText = " Select * from as_Pages where ParentID=-1"
				.CommandType = CommandType.Text
				.Connection = conn
				Dim dr As SqlDataReader = .ExecuteReader

				If dr.Read() Then
					txtSlogan.Text = (CType(dr.Item("BodyHeading"), String))
					txtTitle.Text = (CType(dr.Item("PageName"), String))
				End If

			End With
		End If

	End Sub

	Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
		With cmd
			.CommandText = "Update as_Pages set PageName=@PageName,BodyHeading=@Heading where ParentID=-1"
			.CommandType = CommandType.Text
			.Connection = conn
			If conn.State = ConnectionState.Closed Then conn.Open()
			.Parameters.Add(New SqlParameter("@PageName", txtTitle.Text))
			.Parameters.Add(New SqlParameter("@Heading", txtSlogan.Text))

			.ExecuteNonQuery()
		End With

		Call WriteIntroTextFiles()

	End Sub
End Class
