<!--#include file="../inc/class_uploadpureasp.asp" -->		
<!--#include file="config.asp" -->

<%
dim Path, EditorSource, EditorSourceObject
Path = trim(request("Path"))
EditorSource = trim(request("EditorSource"))
EditorSourceObject = trim(request("EditorSourceObject"))
%>

<script language="javascript">
	function ShowHideAutoResize(){
		if(document.getElementById("CreateThumb").checked == true){
			document.getElementById("AutoResize").style.display = "block";
		}else{
			document.getElementById("AutoResize").style.display = "none";
		}
	}
	
	function ValidateThumb(){
		if(document.getElementById("CreateThumb").checked == true){
			// Width
			if (document.getElementById("MaxWidth").value == ""){alert("Please enter a correct width");document.getElementById("MaxWidth").focus();return false;}
			if (isNaN(document.getElementById("MaxWidth").value)){alert("Please enter a correct width");document.getElementById("MaxWidth").focus();return false;}
			if (document.getElementById("ResizeOption").value == "percent" && (document.getElementById("MaxWidth").value-0) > 100){alert("Please enter a correct width less than 100%");document.getElementById("MaxWidth").focus();return false;}
	
			// Height
			if (document.getElementById("MaxHeight").value == ""){alert("Please enter a correct height");document.getElementById("MaxHeight").focus();return false;}
			if (isNaN(document.getElementById("MaxHeight").value)){alert("Please enter a correct height");document.getElementById("MaxHeight").focus();return false;}
			if (document.getElementById("ResizeOption").value == "percent" && (document.getElementById("MaxHeight").value-0) > 100){alert("Please enter a correct height less than 100%");document.getElementById("MaxHeight").focus();return false;}
		
			// Thumb name
			if (document.getElementById("ThumbName").value == ""){alert("Please enter a thumb name");document.getElementById("ThumbName").focus();return false;}
			
			// Form action
			document.getElementById("Upload").action = "<%=request.ServerVariables("URL")%>?Upload=true" + 
			"&EditorSource=" + ("<%=server.URLEncode(EditorSource)%>") + 
			"&EditorSourceObject=" + ("<%=server.URLEncode(EditorSourceObject)%>") + 
			"&Path=" + ("<%=server.URLEncode(Path)%>") + 
			"&CreateThumb=" + document.getElementById("CreateThumb").checked + 
			"&MaxWidth=" + document.getElementById("MaxWidth").value + 
			"&MaxHeight=" + document.getElementById("MaxHeight").value + 
			"&ResizeOption=" + document.getElementById("ResizeOption").value + 
			"&ThumbName=" + (document.getElementById("ThumbName").value);

		}
		
		if(document.getElementById("CreateThumb").checked == false){
			// Form action
			document.getElementById("Upload").action = "<%=request.ServerVariables("URL")%>?Upload=true" + 
			"&EditorSource=<%=server.URLEncode(EditorSource)%>" + 
			"&EditorSourceObject=<%=server.URLEncode(EditorSourceObject)%>" + 
			"&Path=<%=server.URLEncode(Path)%>";		
		}
		
		return false;
	}
	
</script>

<style type="text/css">
<!--
body{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size:8.5pt;
	color: #000000;
	margin: 0px;
	padding: 0px;
	background: #D6DFF7;
}

input{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size:8pt;
}

select{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size:8pt;
}

/* Page */
.PageMessageWarning{
	font-family: Verdana, sans-serif;
	font-size: 8.5pt;
	color: white;
	background-color: red;
	padding: 2px;

}

.PageMessageSuccess{
	font-family: Verdana, sans-serif;
	font-size: 8.5pt;
	color: white;
	background-color: green;
	padding: 2px;

}

-->
</style>

<%
dim CreateThumb, MaxWidth, MaxHeight, ResizeOption, ThumbName

MaxWidth = trim(request("MaxWidth"))
MaxHeight = trim(request("MaxHeight"))
ResizeOption = trim(request("ResizeOption"))
ThumbName = trim(request("ThumbName"))

if MaxWidth = "" or isNumeric(MaxWidth) = false then MaxWidth = UploadThumbWidth
if MaxHeight = "" or isNumeric(MaxHeight) = false then MaxHeight = UploadThumbHeight
if ResizeOption = "" then ResizeOption = UploadThumbResizeOption
if ThumbName = "" then ThumbName = UploadThumbName

CreateThumb = trim(request("CreateThumb"))
if CreateThumb <> "" then
	CreateThumb = true
else
	CreateThumb = false
end if

' Upload files
if request("Upload") = "true" then
	dim FileUpload, FileUploadCount
	
	Select case lcase(FileManagerUploadComponent)
		Case "persists", "aspupload" 
			Set FileUpload = Server.CreateObject("Persits.Upload")
			FileUpload.OverwriteFiles = True
			FileUploadCount = FileUpload.Save(EditorRootFolderPath & Path)
			
			For Each File in FileUpload.Files
				'Response.Write File.Name & "= " & File.Path & " (" & File.Size &" bytes)<BR>"
				
				' Create thumb
				if CreateThumb then Call CreateThumbnail(File.Name)
				
			Next
			
		Case "pureasp"

			Set FileUpload = New FileUploader
			FileUpload.Upload()

			For Each File In FileUpload.Files.Items
				' Save the file
				File.SaveToDisk EditorRootFolderPath & Path
				FileUploadCount = FileUploadCount + 1		

				'Response.Write "File Name:" & File.FileName & "<br>"
				'Response.Write "File Size:" & File.FileSize & "<br>"
				'Response.Write "File Type:" & File.ContentType & "<br>"

				' Create thumb
				if CreateThumb then Call CreateThumbnail(File.FileName)
				
			Next
			
	End Select

	Set FileUpload = nothing

	' Error Checking
	if err.number <> 0 then
		response.write "<div id=""PageMessage"" class=""PageMessageWarning"">Could not upload files. Please try again</div>"
		err.clear
	else
		response.write "<script language=JavaScript>alert('Uploaded " & FileUploadCount & " file(s) successfully'); " & _
		"self.parent.location.href='filemanager.asp?EditorSource=" & server.URLEncode(EditorSource) & _
		"&EditorSourceObject=" & server.URLEncode(EditorSourceObject) & _
		"&Path=" & server.URLEncode(Path) & "';</script>"
	end if
	
end if

Sub CreateThumbnail(ImageFile)
	' Validate it is a jpg image and constrain proportions
	if right(lcase(ImageFile),4) = ".jpg" then
	
		dim FileName, Jpeg
		
		' Duplicate image to make thumb
		FileName = EditorRootFolderPath & Path & "\" & ImageFile
		
		' Copy file
		dim ActionFS
		set ActionFS = CreateObject("Scripting.FileSystemObject")
			if ActionFS.FileExists(FileName) then ActionFS.CopyFile(FileName), (replace(FileName,".jpg",ThumbName & ".jpg"))
		set ActionFS = nothing
		
		FileName = replace(FileName,".jpg",ThumbName & ".jpg")
		
		' Resize jpg
		Set Jpeg = Server.CreateObject("Persits.Jpeg")
		Jpeg.Open FileName

		' Width > Height
		if Jpeg.Width > Jpeg.Height then
			if lcase(ResizeOption) = "px" then
				Jpeg.Height = CInt(Jpeg.Height/CInt(Jpeg.Width/MaxWidth))
				Jpeg.Width = MaxWidth
			elseif (ResizeOption) = "percent" then
				Jpeg.Height = CInt(Jpeg.Height*(1/(100/MaxWidth)))
				Jpeg.Width = CInt(Jpeg.Width*(1/(100/MaxWidth)))
			end if
		else
			' Height > Width
			if lcase(ResizeOption) = "px" then
				Jpeg.Width = CInt(Jpeg.Width/CInt(Jpeg.Height/MaxHeight))
				Jpeg.Height = MaxHeight
			elseif (ResizeOption) = "percent" then
				Jpeg.Height = CInt(Jpeg.Height*(1/(100/MaxHeight)))
				Jpeg.Width = CInt(Jpeg.Width*(1/(100/MaxHeight)))
			end if						
		end if

		Jpeg.Save FileName
		Set Jpeg = nothing
							
	end if

End Sub
%>

<form action="?Upload=true&EditorSource=<%=server.URLEncode(EditorSource)%>&EditorSourceObject=<%=server.URLEncode(EditorSourceObject)%>&Path=<%=server.HTMLEncode(Path)%>" method="post" name="Upload" class="text" id="Upload" enctype="multipart/form-data" onsubmit="ValidateThumb(); this.Submit.disabled=true">
1 <INPUT TYPE="FILE" SIZE="9" NAME="FILE1"><BR>
2 <INPUT TYPE="FILE" SIZE="9" NAME="FILE2"><BR>
3 <INPUT TYPE="FILE" SIZE="9" NAME="FILE3"><BR>
4 <INPUT TYPE="FILE" SIZE="9" NAME="FILE4"><BR>
5 <INPUT TYPE="FILE" SIZE="9" NAME="FILE5"><BR>
<br />
<input type="submit" name="Submit" value="  Upload  ">
<br />
<%if UploadThumbWidth > 0 then%>
<input name="CreateThumb" type="checkbox" id="CreateThumb" value="true" onclick="ShowHideAutoResize()" />
 Create Thumb (.jpg)
 <div id="AutoResize" style="background-color:#D6DFF7; padding:2px; margin:2px; border-top:1px solid #F1F1F1; display:none;">
	W
  <input name="MaxWidth" type="text" id="MaxWidth" size="2" maxlength="5" value="<%=UploadThumbWidth%>" onblur="ValidateThumb()" />
  x H
  <input name="MaxHeight" type="text" id="MaxHeight" size="2" maxlength="5" value="<%=UploadThumbHeight%>" onblur="ValidateThumb()"> 
  <select name="ResizeOption" id="ResizeOption" onchange="ValidateThumb()">
    <option value="px">px</option>
    <option value="percent">%</option>
  </select>
  <br />
  image<input name="ThumbName" type="text" id="ThumbName" onchange="ValidateThumb()" value="<%=UploadThumbName%>" size="10" maxlength="100" />
  .jpg
</div>
<%end if%>
<div style="font-size:10px; font-family:Verdana, Arial, Helvetica, sans-serif;">(Uploads to '<%=Path%>')</div>
</form>