﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Partial Class TextAreaUploadImages
    Inherits System.Web.UI.Page
    Protected _imagePath As String
    Protected _location As String
    Shared img As Integer

    Public Property ImagePath() As String
        Get
            Return _imagePath
        End Get
        Set(ByVal value As String)
            _imagePath = value
        End Set
    End Property
    Public Property Location() As String
        Get
            Return _location
        End Get
        Set(ByVal value As String)
            _location = value
        End Set
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            img = 0
            'Button1.Attributes.Add("onclientclick", "alert('asdasdasd');return false;") '"return CheckFileUpload('" & UploadImage.ClientID.ToString & "','" & txtHeight.ClientID.ToString & "','" & txtWidth.ClientID.ToString & "');")

           

        End If
        ' Put user code to initialize the page here
    End Sub
    Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
        If Page.IsPostBack Then
            PageBody.Attributes.Add("onbeforeunload", ((("insertsImage('" & Me.ImagePath & "','") + Me.Location & "','") + txtHeight.Text & "','") + txtWidth.Text & "','" + txtAlterNateText.Text.ToString + "');")
        Else
            txtHeight.Attributes.Add("onkeypress", "return JJCheckNumericValue(event)")
            txtWidth.Attributes.Add("onkeypress", "return JJCheckNumericValue(event)")
        End If
        

        MyBase.OnPreRender(e)
    End Sub

#Region "Web Form Designer generated code"
    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
        '
        ' CODEGEN: This call is required by the ASP.NET Web Form Designer.
        '
        InitializeComponent()
        MyBase.OnInit(e)
    End Sub

    ''' <summary>
    ''' Required method for Designer support - do not modify
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()

    End Sub
#End Region

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'If heightValidator.IsValid AndAlso widthValidator.IsValid Then
        Dim elementToInsert As String = Request.QueryString("f")
        If UploadImage.PostedFile IsNot Nothing AndAlso UploadImage.PostedFile.ContentLength > 0 Then
            Try
                Dim fileName As String = System.IO.Path.GetFileName(UploadImage.PostedFile.FileName)
                Dim fileLocation As String = Server.MapPath("~/Images/article images/" + fileName)  '(Request.QueryString("path") & "/") + fileName

                Dim filetype As String = System.IO.Path.GetExtension(UploadImage.PostedFile.FileName)
                Dim j As Integer = Len(filetype)
                'Dim fname As String = fileup.FileName
                Dim file As String = Mid(fileName, 1, Len(fileName) - j)

                While (System.IO.File.Exists(fileLocation))
                    img = img + 1
                    fileName = file & img & filetype
                    fileLocation = Server.MapPath("~/Images/article images/" & file & img & filetype)  '(Request.QueryString("path") & "/") + file + img + filetype
                End While

                Dim fLocation As String
                fLocation = Request.QueryString("path") & "/" + fileName

                Me.ImagePath = Server.HtmlEncode(fLocation)
                Me.Location = elementToInsert
                Button1.Enabled = False
                UploadImage.PostedFile.SaveAs(fileLocation)
                'Threading.Thread.Sleep(3000)
            Catch ex As Exception
                Response.Write("<script language=javascript>alert('" & ex.Message.ToString().Replace("\", "\\") & "');</script>")

            Finally
                Button1.Enabled = True
            End Try
        End If
        'End If
    End Sub


    Protected Overloads Overrides Sub OnUnload(ByVal e As EventArgs)
        ' Close current page
        If ((Me.ImagePath <> "") AndAlso (Me.ImagePath IsNot Nothing)) AndAlso ((Me.Location <> "") AndAlso (Me.Location IsNot Nothing)) Then
            HttpContext.Current.Response.Write("<script language=javascript>self.close();</script>")
        End If

        ' Call base OnUnload
        MyBase.OnUnload(e)
    End Sub
End Class
