Sub my_load()
	If rootpath <> "" AND Right(rootpath, 1) <> "\" Then 
		rootpath = rootpath & "\"
	End If
	On Error Resume Next
	EasyView1.UseDriverSetup = true
	EasyView1.TwainConfigFile = "easyscan.twain.ini"
	EasyView1.ISISConfigFile = "easyscan.isis.ini"
	
	If OpenFileMode = "" Then OpenFileMode = 1

	winpath = EasyView1.Helper.ShellGetFolderPath(40)
	If winpath = "" Then
		MsgBox("Impossibile salvare demo-pref.ini su disco locale")
	Else
		Set fso = CreateObject("Scripting.FileSystemObject")
		prefpath = winpath & "\demo-pref.ini"
		ScannerName 					= GetINIString("Setup", "ScannerName", "-", prefpath)
	End If

	If UCase(Driver) = "ISIS" AND ScannerName <> "" Then
		EasyView1.ISISDriver = GetINIString("Setup", ScannerName, "", prefpath)
		EasyView1.ISISPageSize = GetINIString("Isis", "ISISPageSize",  "", prefpath)
		EasyView1.ISISXRes = GetINIString("Isis", "ISISXRes", "", prefpath)
		EasyView1.ISISYRes = GetINIString("Isis", "ISISYRes", "", prefpath) 
		EasyView1.ISISBitsPerSample = GetINIString("Isis", "ISISBitsPerSample", "", prefpath) 
		EasyView1.ISISSamplesPerPixel = GetINIString("Isis", "ISISSamplesPerPixel", "", prefpath) 
		EasyView1.ISISScanMode = GetINIString("Isis", "ISISScanMode", "", prefpath) 
		EasyView1.ISISTop = GetINIString("Isis", "ISISTop", "", prefpath) 
		EasyView1.ISISLeft = GetINIString("Isis", "ISISLeft", "", prefpath) 
		EasyView1.ISISRight = GetINIString("Isis", "ISISRight", "", prefpath) 
		EasyView1.ISISBottom = GetINIString("Isis", "ISISBottom", "", prefpath) 
		EasyView1.ISISCompression = GetINIString("Isis", "ISISCompression", "", prefpath) 
		EasyView1.ISISContrast = GetINIString("Isis", "ISISContrast", "", prefpath) 
		EasyView1.ISISBrightness = GetINIString("Isis", "ISISBrightness", "", prefpath) 
	End If
	

	If PreLoadForm = 1 Then	load_form()
	init_list(rootpath)
End Sub

Sub load_form()
	On Error Resume Next
	If FormPath = "" Then
		EasyView1.FormPath = EasyView1.Helper.BrowseForFolder(0, "Scegliere la directory di lavoro", "")
		FormPath = EasyView1.FormPath
		If EasyView1.FormPath = "" Then Exit Sub
	Else
		EasyView1.FormPath = FormPath
	End If
	Set fso = CreateObject("Scripting.FileSystemObject")
	If fso.FolderExists(FormPath) Then
		Set f = fso.GetFolder(FormPath)
		Set fc = f.Files
		For each f1 in fc
			EasyView1.LoadForm(Mid(f1.name,1,Len(f1.name)-4))
		Next
		EasyView1.AnalizeMode 2
	End If
End Sub

Sub on_select()
	If UCase(Driver) = "TWAIN" Then
		EasyView1.SelectSource()
		savePref "Setup", "ScannerName", EasyView1.TwainScannerName
	Else
		EasyView1.ISISSelectScanner()
		If EasyView1.ISISScannerName <> "" AND EasyView1.ISISDriver <> "" Then
			WriteINIString "Setup", EasyView1.ISISScannerName, EasyView1.ISISDriver, prefpath
			savePref "Setup", "ScannerName", EasyView1.ISISScannerName
		End If
	End If
End Sub
	
Sub on_setup()
	If UCase(Driver) = "TWAIN" Then
		EasyView1.TwainScannerSetup()
	Else
		EasyView1.ISISScannerSetup()
		savePref "Isis", "ISISPageSize", EasyView1.ISISPageSize
		savePref "Isis", "ISISXRes", EasyView1.ISISXRes
		savePref "Isis", "ISISYRes", EasyView1.ISISYRes
		savePref "Isis", "ISISBitsPerSample", EasyView1.ISISBitsPerSample
		savePref "Isis", "ISISSamplesPerPixel", EasyView1.ISISSamplesPerPixel
		savePref "Isis", "ISISScanMode", EasyView1.ISISScanMode
		savePref "Isis", "ISISTop", EasyView1.ISISTop
		savePref "Isis", "ISISLeft", EasyView1.ISISLeft
		savePref "Isis", "ISISRight", EasyView1.ISISRight
		savePref "Isis", "ISISBottom", EasyView1.ISISBottom
		savePref "Isis", "ISISCompression", EasyView1.ISISCompression
		savePref "Isis", "ISISContrast", EasyView1.ISISContrast
		savePref "Isis", "ISISBrightness", EasyView1.ISISBrightness
	End If
End Sub

Sub on_acquire()
	If rootpath = "" Then on_browse()
	If rootpath = "" Then Exit Sub
	setCounter("r")
	If UCase(Driver) = "TWAIN" Then
		ret = EasyView1.TwainAcquire()
	Else
		EasyView1.IsisThread 1
		ret = EasyView1.ISISAcquire()
	End If
	If ret = 2 Then MsgBox "Inserire la carta"
End Sub

Sub EasyView1_TwainPage()
	EasyView1Page()
End Sub

Sub EasyView1_ISISPage()
	EasyView1Page()
End Sub

Sub EasyView1Page()
	Name = CStr(counter)
	Name = String(8 - len(name), "0") & name & ".TIF"
	EasyView1.ImageName = rootpath & Name
	counter = counter + 1
	AddFileList(Name)
	EasyView1.CompMode 32773
	If EasyView1.Save() <> 0 Then MsgBox "Errore nel salvataggio locale dei file"
End Sub

Sub AddFileList(text)
	Set oList = document.all.oFileList
	Set oOption = document.createElement("OPTION")
	oList.options.add(oOption)
	oOption.innerText = text
	oOption.value = text
End Sub 

Sub init_list(path)
	On Error Resume Next
	EasyView1.FitMode = 3
	If path <> "" Then
		rootpath = path
		Dim fso, f, f1, fc, s, oList
		Set fso = CreateObject("Scripting.FileSystemObject")
		If fso.FolderExists(rootpath) Then
			Set f = fso.GetFolder(rootpath)
			Set fc = f.Files
			Set oList = document.all.oFileList
			oList.length = 0
			For each f1 in fc
				ext = UCase(fso.getextensionname(f1.name))
				If ext = "TIF" or ext = "JPG" OR ext = "PDF" Then
					AddFileList(f1.name)
				End If
			Next
			Set fso = nothing
		Else
			msgbox "La directory non esiste"
		End If
		setCounter("r")
	End If
End Sub 

Sub on_browse()
	path = rootpath
	path = EasyView1.Helper.BrowseForFolder(0, "Scegliere la directory di lavoro", path)
	If path = "" Then Exit Sub
	rootpath = path & "\"
	init_list(rootpath)
	Easyview1.Close
End Sub

Sub on_fileclick()
	If (oList.selectedIndex <> -1) Then
		curfilename = rootpath & oList.options(oList.selectedIndex).text
		EasyView1.OpenFileMode OpenFileMode
		EasyView1.Open(curfilename)
	End If
End Sub

Sub on_fit()
	If EasyView1.FitMode = 3 OR fit = 1 Then
		EasyView1.FitMode = 1
		If fit = 0 Then
			EasyView1.StartY = 0
			fit = 1 - fit
		Else
			EasyView1.StartY EasyView1.ScaledImageLength/2
			fit = 1 - fit
		End If
	Else
		EasyView1.FitMode = 3
	End If
End Sub

Sub On_process(com)
	On Error Resume Next
	Dim fso, f, f1, fc, s, oList
	Dim oElementi, oDropDrown, oOpzioni
	Dim sSelezionati, sItems, customerID, a

	Set fso = CreateObject("Scripting.FileSystemObject")
	If fso.FolderExists(rootpath) then
		Set oElementi = Document.reg.Elements
		Set oDropDrown = oElementi("file")
		m = 0
		For each oOpzioni in oDropDrown.Options
			If oOpzioni.Selected Then 
				ext = UCase(fso.getextensionname(oOpzioni.text))
				If ext = "TIF" OR ext = "JPG" OR ext = "PDF" Then
					m = m + 1
				End If
			End If
		Next
		EasyView1.CompMode 4
		For each oOpzioni in oDropDrown.Options
			If oOpzioni.Selected Then 
				ext = UCase(fso.getextensionname(oOpzioni.text))
				If ext = "TIF" OR ext = "JPG" OR ext = "PDF" Then
					If m > 1 Then
						EasyView1.OpenFileMode OpenFileMode
						EasyView1.Open(rootpath & oOpzioni.text)
					End If
					Select Case (com)
						Case "deskew"
							Easyview1.deskew
						Case "crop"
							Easyview1.crop
						Case "OCR"
							EasyView1.FormPath = rootpath
							Easyview1.Analize 4
						Case "PDF"
							fname = Mid(rootpath & oOpzioni.text, 1, Len(rootpath & oOpzioni.text) - 3) & "PDF"
							EasyView1.ImageName = fname
							EasyView1.CompMode = -3
							EasyView1.Save()
						Case "ODR"
							EasyView1.FormPath = rootpath
							EasyView1.ViewGrid = True
							EasyView1.Analize 5
							EasyView1.Mode = 0
						Case "ODR-I"
							EasyView1.ViewGrid = True
						    EasyView1.Process ("EXPORTMODE=1")
							EasyView1.Analize 6
						Case "rotate"
							EasyView1.Rotate("90")
						Case "autorotate"
							EasyView1.Process ("AUTOROT=0,20,0")
						Case "spotremoval"
							EasyView1.Process("IMGPRC= SPOT " + document.reg.spot.value)
						Case "thresholding"
							EasyView1.ThresholdMode 3
							Easyview1.Threshold document.reg.thres.value
							EasyView1.Thresholding
						Case "analize"
							a = EasyView1.Analize(0)
							If a = 4 Then MsgBox("Form non riconosciuto")
						Case "save"
							EasyView1.Save()
						Case Else
							Msgbox("Errore Di esecuzione")
					End Select
					if com <> "analize" AND com <> "ODR-I" AND com <> "save" Then
						If SaveOnFly = 1 Then
							EasyView1.Save()
						End If
					End If
				End If
			End If
		Next 
		Set fso = nothing
		'MsgBox "Operazione Terminata"
	Else
		MsgBox "La directory non esiste"
	End If
End Sub

Sub on_keydown()
	If window.event.keycode = 46 Then
		on_deleteFile()
		init_list(rootpath)
		easyview1.close
	End If
End Sub

Sub on_deleteFile()
	Set fso = CreateObject("Scripting.FileSystemObject")
	If fso.FolderExists(rootpath) Then
		For each oOpzioni in oList.Options
			If oOpzioni.Selected Then 
				If fso.FileExists(rootpath & oOpzioni.text) Then
					Set file = fso.GetFile (rootpath & oOpzioni.text)
					file.Delete
				End If
			End If
		Next 
		Set fso = nothing
		init_list(rootpath)
	End If
End Sub

Sub savePref(area,pref,v)
	WriteINIString area, pref, v, prefpath
End Sub

Sub SetCounter(op) 'r => read, w => write
	Set fso = CreateObject("Scripting.FileSystemObject")
	If op = "r" Then
		If fso.FileExists(rootpath & "counter") Then
			Set inFile = fso.OpenTextFile(rootpath & "counter", 1, False)
			Do While Not inFile.AtEndOfStream
				counter = inFile.ReadLine()
			Loop
		Else
			Set inFile = fso.OpenTextFile(rootpath & "counter", 2, True)
			inFile.Write 1
			counter = 1
		End If
	Else
		Set inFile = fso.OpenTextFile(rootpath & "counter", 2, True)
		inFile.Write counter
	End If
	inFile.Close
	Set inFile = Nothing
	Set fso = Nothing
End Sub

Sub	EasyView1Finish()
	setCounter("w")
	init_list(rootpath)
End Sub

Sub EasyView1_TwainFinish()
	EasyView1Finish()
End Sub

Sub EasyView1_ISISFinish()
	EasyView1Finish()
End Sub

Sub on_bodykeydown()
	If window.event.ctrlKey Then
		Select Case window.event.keycode
			Case 32			' CTRL + Space => FIT 
				on_fit()
				oList.focus()
				window.event.cancelBubble = true
			Case Else
				Exit Sub
		End Select
	End If
	If window.event.shiftKey Then 
		Select Case window.event.keycode
			Case 32			' SHIFT + Space => MOSTRA DOCUMENTI MULTIPAGINA
				If cp = EasyView1.Pages - 1 Then
					cp = 0
				Else
					cp = cp + 1
				End If
				EasyView1.CurrentPage = cp
				oList.focus()
				window.event.cancelBubble = true
			Case Else
				Exit Sub
		End Select
	End If
End Sub
