<%

	' for use with IMAGE_ResizeImage()
	Const	C_UPLOAD_PRIMARY	= 1
	Const	C_UPLOAD_ALTERNATE	= 2
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function IMAGE_ResizeImage(sFilename, iImageType)
	
		Dim sFilePath, fso, intXSize, intYSize, sTemp
		
		Set fso = server.CreateObject("Scripting.FileSystemObject")
		
		'mini -60
		'small -150
		'tiny -35
		'<root> - 450

		If ( iImageType = C_UPLOAD_PRIMARY ) Then
			sFilePath	= sOWImagesDir & "/"
		ElseIf ( iImageType = C_UPLOAD_ALTERNATE ) Then
			sFilePath	= sOWImagesDir & "/alternates/"
		End If
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= Server.MapPath(sFilePath)

		If (NOT fso.FolderExists(sFilePath)) Then
			fso.CreateFolder(sFilePath)
		End If
		If (NOT fso.FolderExists(sFilePath & "\small")) Then
			fso.CreateFolder(sFilePath & "\small")
		End If
		If (NOT fso.FolderExists(sFilePath & "\mini")) Then
			fso.CreateFolder(sFilePath & "\mini")
		End If
		If (NOT fso.FolderExists(sFilePath & "\tiny")) Then
			fso.CreateFolder(sFilePath & "\tiny")
		End If
	
		If ( sOWImageComponent = "ASPIMAGE" ) Then
			Set objASPImage = Server.CreateObject("AspImage.Image")
			objASPImage.LoadImage(sFilePath & "\" & sFileName)
			If ( (objASPImage.ImageFormat=1) Or (objASPImage.ImageFormat=2) Or (objASPImage.ImageFormat=3) ) And (objASPImage.MaxX>0) Then
				' resize the small image
				intXSize = 150
				intYSize = Int((intXSize / objASPImage.MaxX) * objASPImage.MaxY)
				objASPImage.Resize intXSize, intYSize
				objASPImage.FileName = (sFilePath & "\small\" & sFileName)
				objASPImage.SaveImage

				' resize the mini image
				intXSize = 60
				objASPImage.LoadImage(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.MaxX) * objASPImage.MaxY)
				objASPImage.Resize intXSize, intYSize
				objASPImage.FileName = (sFilePath & "\mini\" & sFileName)
				objASPImage.SaveImage
				
				' resize the tiny image
				intXSize = 35
				objASPImage.LoadImage(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.MaxX) * objASPImage.MaxY)
				objASPImage.Resize intXSize, intYSize
				objASPImage.FileName = (sFilePath & "\tiny\" & sFileName)
				objASPImage.SaveImage
				
				' last resize the primary image (do this last so the other images are being created from the unmodified original version
				intXSize = 450
				objASPImage.LoadImage(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.MaxX) * objASPImage.MaxY)
				objASPImage.Resize intXSize, intYSize
				objASPImage.FileName = (sFilePath & "\" & sFileName)
				objASPImage.SaveImage
			Else
				sTemp = LANG_DT( "UPLOADIMAGE_ASPIMAGEERROR", "Unable to use ASPImage to create a thumbnail from the source image (1:2:) file type." )
				sTemp = Replace(sTemp, "1:", sFilePath)
				IMAGE_ResizeImage = Replace(sTemp, "2:", sFileName)
			End If
			Set objASPImage = Nothing
		ElseIf ( sOWImageComponent = "GFLAX" ) Then
			Set objASPImage = Server.CreateObject("GflAx.GflAx")
			objASPImage.EnableLZW = TRUE
			On Error Resume Next
			objASPImage.LoadBitmap(sFilePath & "\" & sFileName)
			If ( Err.Number <> 0 ) Then
				sTemp = LANG_DT( "UPLOADIMAGE_GFLAXERROR", "Unable to create images for file: <b>1:</b>. The file type is not supported by the GflAx component." )
				IMAGE_ResizeImage = Replace(sTemp, "1:", sFileName)
			Else
				' resize the small image
				intXSize = 150
				objASPImage.LoadBitmap(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.Width) * objASPImage.Height)
				objASPImage.LoadThumbnail sFilePath & "\" & sFileName, CDbl(intXSize), CDbl(intYSize)
				objASPImage.SaveFormat = 1
				objASPImage.SaveBitmap sFilePath & "\small\" & sFileName
				
				' resize the mini image
				intXSize = 60
				objASPImage.LoadBitmap(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.Width) * objASPImage.Height)
				objASPImage.LoadThumbnail sFilePath & "\" & sFileName, CDbl(intXSize), CDbl(intYSize)
				objASPImage.SaveFormat = 1
				objASPImage.SaveBitmap sFilePath & "\mini\" & sFileName
				
				' resize the tiny image
				intXSize = 35
				objASPImage.LoadBitmap(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.Width) * objASPImage.Height)
				objASPImage.LoadThumbnail sFilePath & "\" & sFileName, CDbl(intXSize), CDbl(intYSize)
				objASPImage.SaveFormat = 1
				objASPImage.SaveBitmap sFilePath & "\tiny\" & sFileName
				
				intXSize = 450
				objASPImage.LoadBitmap(sFilePath & "\" & sFileName)
				intYSize = Int((intXSize / objASPImage.Width) * objASPImage.Height)
				objASPImage.LoadThumbnail sFilePath & "\" & sFileName, CDbl(intXSize), CDbl(intYSize)
				objASPImage.SaveFormat = 1
				objASPImage.SaveBitmap sFilePath & "\" & sFileName
			End If
			On Error Goto 0
			Set objASPImage = Nothing
		ElseIf ( sOWImageComponent = "PERSIST" ) Then
			Set objJPEG = Server.CreateObject("Persits.Jpeg")
			' resize the small image
			intXSize = 150
			objJPEG.Open( sFilePath & "\" & sFileName )
			objJPEG.Interpolation = 1
			objJPEG.Width = intXSize
			objJPEG.Height = objJPEG.OriginalHeight * iSize / objJPEG.OriginalWidth
			objJPEG.Sharpen 1, 120
			objJPEG.Save sFilePath & "\small\" & sFileName
			
			' resize the mini image
			intXSize = 60
			objJPEG.Open( sFilePath & "\" & sFileName )
			objJPEG.Interpolation = 1
			objJPEG.Width = intXSize
			objJPEG.Height = objJPEG.OriginalHeight * iSize / objJPEG.OriginalWidth
			objJPEG.Sharpen 1, 120
			objJPEG.Save sFilePath & "\mini\" & sFileName
			
			' resize the tiny image
			intXSize = 35
			objJPEG.Open( sFilePath & "\" & sFileName )
			objJPEG.Interpolation = 1
			objJPEG.Width = intXSize
			objJPEG.Height = objJPEG.OriginalHeight * iSize / objJPEG.OriginalWidth
			objJPEG.Sharpen 1, 120
			objJPEG.Save sFilePath & "\tiny\" & sFileName
			
			' resize the tiny image
			intXSize = 450
			objJPEG.Open( sFilePath & "\" & sFileName )
			objJPEG.Interpolation = 1
			objJPEG.Width = intXSize
			objJPEG.Height = objJPEG.OriginalHeight * iSize / objJPEG.OriginalWidth
			objJPEG.Sharpen 1, 120
			objJPEG.Save sFilePath & "\" & sFileName
			Set objJPEG = Nothing
		ElseIf ( sOWImageComponent = "IMAGEMAGICK" ) Then
			Set objImageMagick = CreateObject("ImageMagickObject.MagickImage.1")
			objImageMagick.Convert "-thumbnail=150x", (sFilePath & "\" & sFileName), (sFilePath & "\small\" & sFileName)
			objImageMagick.Convert "-thumbnail=60x", (sFilePath & "\" & sFileName), (sFilePath & "\mini\" & sFileName)
			objImageMagick.Convert "-thumbnail=35x", (sFilePath & "\" & sFileName), (sFilePath & "\tiny\" & sFileName)
			objImageMagick.Convert "-thumbnail=450x", (sFilePath & "\" & sFileName), (sFilePath & "\" & sFileName)
			Set objImageMagick = Nothing
		End If
		
		IMAGE_ResizeImage = sTemp
	

	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This saves the file (if any)													*
	'*																					*
	'************************************************************************************
	Function G_UPLOAD_SaveFile(sFormFieldName, sRootFolder, sThumbDir, sDestFilename)
		Dim sFilePath, sError, fso

		G_UPLOAD_SaveFile	= ""
		
		If ( objUpload(sFormFieldName).Length > 0 ) Then
			' check to make sure that the file is not too big
			aFileSize	= objUpload(sFormFieldName).Length
			If ( aFileSize > 25000000 ) Then
				sError = "Please upload a smaller (in k) image."
			Else
				sFilePath	= "../../" & sRootFolder & sThumbDir
				sFilePath	= Replace(sFilePath, "\", "/")
				sFilePath	= Replace(sFilePath, "//", "/")
				sFilePath	= server.mappath(sFilePath)
				' Make sure the directories exist...make it if it doesnt
				Set fso = CreateObject("Scripting.FileSystemObject")
				If (NOT fso.FolderExists(sFilePath)) Then
					fso.CreateFolder(sFilePath)
				End If
				Set fso = Nothing
				' save it
				objUpload(sFormFieldName).SaveAs sFilePath & "\" & sDestFilename
			End If
		End If
		G_UPLOAD_SaveFile = sError
		
	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This deletes the file (if any). 												*
	'*																					*
	'************************************************************************************
	Sub DeleteProductImage(sFileName, sPath)
	
		Dim sFilePath, lf, f
	
		Set lf = server.createObject("Scripting.FileSystemObject")
		sFilePath	= "../../" & sOWImagesDir & "/" & sPath
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= server.mappath(sFilePath) & "\" & sFileName
		If ( lf.FileExists(sFilePath) ) Then	
			Set f = lf.GetFile(sFilePath)
			f.Delete
			Set f = Nothing
		End If
		Set lf = Nothing
	
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function DeleteAllImages(sFileName)
	
		Dim lf, f
	
		Set lf = server.createObject("Scripting.FileSystemObject")
		
		sFilePath	= "../../" & sOWImagesDir
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= server.mappath(sFilePath) & "\" & sFileName
		If ( lf.FileExists(sFilePath) ) Then	
			Set f = lf.GetFile(sFilePath)
			f.Delete
			Set f = Nothing
		End If
		
		sFilePath	= "../../" & sOWImagesDir
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= server.mappath(sFilePath) & "\small\" & sFileName
		If ( lf.FileExists(sFilePath) ) Then	
			Set f = lf.GetFile(sFilePath)
			f.Delete
			Set f = Nothing
		End If
		
		sFilePath	= "../../" & sOWImagesDir
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= server.mappath(sFilePath) & "\mini\" & sFileName
		If ( lf.FileExists(sFilePath) ) Then	
			Set f = lf.GetFile(sFilePath)
			f.Delete
			Set f = Nothing
		End If
		
		sFilePath	= "../../" & sOWImagesDir
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= server.mappath(sFilePath) & "\tiny\" & sFileName
		If ( lf.FileExists(sFilePath) ) Then	
			Set f = lf.GetFile(sFilePath)
			f.Delete
			Set f = Nothing
		End If

		Set lf = Nothing
	
	End Function
	'************************************************************************************
%>