<scriptlet>
<implements type="Automation" id="dispatcher">
	<property name="PluginEvent">
		<get/>
	</property>
	<property name="PluginDescription">
		<get/>
	</property>
	<property name="PluginFileFilters">
		<get/>
	</property>
	<property name="PluginIsAutomatic">
		<get/>
	</property>
	<property name="PluginUnpackedFileExtension">
		<get/>
	</property>
	<property name="PluginExtendedProperties">
		<get/>
	</property>
	<method name="UnpackFile"/>
	<method name="PackFile"/>
	<method name="IsFolder"/>
	<method name="UnpackFolder"/>
	<method name="PackFolder"/>
	<method name="ShowSettingsDialog"/>
</implements>

<script language="VBS">

'/////////////////////////////////////////////////////////////////////////////
'    This is a plugin for WinMerge.
'    It will display the text content of MS Excel files.
'    Copyright (C) 2005-2014 Takashi Sawanaka
'
'    This program is free software; you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation; either version 2 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program; if not, write to the Free Software
'    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
'

Option Explicit

Const RegKeyPath = "HKCU\Software\Thingamahoochie\WinMerge\Plugins\CompareMSExcelFiles.sct\"
Const pbGroup = 6
Dim MsgCannotGetMacros
MsgCannotGetMacros = "Cannot get Macros." & vbCrLf & _
	"   To allow WinMerge to compare macros, use MS Office to alter the settings in the Macro Security for the current application." & vbCrLf & _
	"   The Trust access to Visual Basic Project feature should be turned on to use this feature in WinMerge." & vbCrLf

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim wsh: Set wsh = CreateObject("WScript.Shell")

Function isAccessibleVBAProject(wbk)
	Dim count
	count = -1
	On Error Resume Next
	count = wbk.VBProject.VBComponents.Count
	isAccessibleVBAProject = (count > 0)
End Function

Function regRead(Key, DefaultValue)
	regRead = DefaultValue
	On Error Resume Next
	regRead = wsh.RegRead(Key)
End Function

Function writeObjectProperties(fo, items)
	On Error Resume Next
	Dim o
	For Each o In items
		fo.WriteLine o.Name & ": " & o.Value
	Next
End Function

Function QuoteIfNeeded(str)
	Dim fQuote
	If InStr(str, vbTab) > 0 Then
		fQuote = True
	ElseIf InStr(str, """") > 0 Then
		fQuote = True
	ElseIf InStr(str, vbLf) > 0 Then
		fQuote = True
	End If
	If fQuote Then
		QuoteIfNeeded = """" & Replace(str, """", """""") & """"
	Else
		QuoteIfNeeded = str
	End If
End Function

Function writeCellValues(fo, sht)
	Dim varCells, row, col, ary()
	varCells = sht.UsedRange.Value
	If TypeName(varCells) = "String" Then
		fo.WriteLine QuoteIfNeeded(varCells)
	ElseIf Not IsEmpty(varCells) Then
		ReDim ary(UBound(varCells, 2))
		On Error Resume Next
		For row = 1 To UBound(varCells, 1)
			For col = 1 To UBound(varCells, 2)
				ary(col - 1) = QuoteIfNeeded(CStr(varCells(row, col)))
				If Err.Number <> 0 Then
					ary(col - 1) = "Error" & Err.Number
					Err.Clear
				End If
			Next
			fo.WriteLine Join(ary, vbTab)
		Next
	End If
End Function

Function GetAddr(row, col)
    Dim c
    c = col - 1
    Do
        GetAddr = Chr(65 + (c Mod 26)) & GetAddr
        c = (c \ 26) - 1
    Loop While c >= 0
    GetAddr = GetAddr & row
End Function

Function writeFormulas(fo, sht)
	Dim row, col, rowOffset, colOffset, varCells, formula
	rowOffset = sht.UsedRange.Row
	colOffset = sht.UsedRange.Column
	varCells = sht.UsedRange.Formula
	If TypeName(varCells) = "String" Then
		fo.WriteLine GetAddr(rowOffset - 1, colOffset - 1) & ": " & varCells
	Else
		For row = 1 To UBound(varCells, 1)
			For col = 1 To UBound(varCells, 2)
				formula = varCells(row, col)
				If Left(formula, 1) = "=" Then 
					fo.WriteLine GetAddr(row + rowOffset - 1, col + colOffset - 1) & ": " & formula
				End If
			Next
		Next
	End If
End Function

Function getUsedRangeIncludingShapes(sht)
	Dim shp, rng
	Dim col_max, row_max
	col_max = 0
	row_max = 0
	For Each shp In sht.Shapes
		Set rng = shp.BottomRightCell
		If row_max < rng.Row + rng.Rows.Count - 1 Then row_max = rng.Row + rng.Rows.Count - 1
		If col_max < rng.Column + rng.Columns.Count - 1 Then col_max = rng.Column + rng.Columns.Count - 1
	Next
	Set rng = sht.UsedRange
	If row_max < rng.Row + rng.Rows.Count - 1 Then row_max = rng.Row + rng.Rows.Count - 1
	If col_max < rng.Column + rng.Columns.Count - 1 Then col_max = rng.Column + rng.Columns.Count - 1
	Set getUsedRangeIncludingShapes = sht.Range("A1", sht.Cells(row_max, col_max))
End Function

Sub ungroupShapes(sht)
    On Error Resume Next
    Dim cnt, shp
    Do
        cnt = sht.Shapes.Count
        For Each shp In sht.Shapes
            If shp.Type = pbGroup Then shp.ungroup
        Next
    Loop While cnt <> sht.Shapes.Count
End Sub

Function writeTextsInShapes(fo, sht)
	Dim shp
	On Error Resume Next
	For Each shp In sht.Shapes
		fo.WriteLine shp.Name & ": " & shp.TextFrame.Characters.Text
	Next
	On Error GoTo 0
End Function

Function writeHeadersAndFooters(fo, sht)
	On Error Resume Next
	fo.WriteLine "LeftHeader: " & sht.PageSetup.LeftHeader
	fo.WriteLine "CenterHeader: " & sht.PageSetup.CenterHeader
	fo.WriteLine "RightHeader: " & sht.PageSetup.RightHeader
	fo.WriteLine "LeftFooter: " & sht.PageSetup.LeftFooter
	fo.WriteLine "CenterFooter: " & sht.PageSetup.CenterFooter
	fo.WriteLine "RightFooter: " & sht.PageSetup.RightFooter
	On Error GoTo 0
End Function

Function getModuleExtension(cmp)
	Select Case cmp.Type
	Case 2
		getModuleExtension = ".cls"
	Case 3
		getModuleExtension = ".frm"
	Case Else
		getModuleExtension = ".bas"
	End Select
End Function

Sub Sleep(sec)
	if sec = 0 Then Exit Sub
	wsh.Run "ping.exe localhost -n " & sec, 0, True
End Sub

Function saveRangeAsImage(sht, rng, filename)
	Dim obj
	Dim shtNew
	Dim wbkNew
	Dim oldSheetsInNewWorkbook
	Dim i

	saveRangeAsImage = True

	On Error Resume Next
	Err.Clear

	sht.Activate
	With sht.Parent.Windows(1)
		.DisplayGridlines = False
		.View = 1
	End With

	oldSheetsInNewWorkbook = sht.Application.SheetsInNewWorkbook
	sht.Application.SheetsInNewWorkbook = 1
	set wbkNew = sht.Application.Workbooks.Add
	sht.Application.SheetsInNewWorkbook = oldSheetsInNewWorkbook
	Set shtNew = wbkNew.Sheets(1)
	Set obj = wbkNew.Charts.Add
	Set obj = obj.Location(2, shtNew.Name) ' xlLocationAsObject=2

	If sht.Application.Version <= 11 Then
		obj.Parent.Width = rng.Width + 8
		obj.Parent.Height = rng.Height + 8
	Else
		obj.Parent.Width = rng.Width
		obj.Parent.Height = rng.Height
	End If

	For i = 0 to 1
		If sht.Application.Version < 14 Then
			rng.CopyPicture 1, 2 ' xlScreen=1, xlBitmap=2
			If Err.Number = 0 Then
				Sleep i
				obj.Paste
				obj.Export filename, "PNG"
			ElseIf i = 0 Then
				Sleep 1
			End If
		Else
			rng.Copy
			If Err.Number = 0 Then
				Sleep i
				shtNew.Pictures.Paste
				obj.Export filename, "PNG"
			ElseIf i = 0 Then
				Sleep 1
			End If
		End If

		If Err.Number = 0 Then Exit For
		If i = 0 Then Err.Clear
	Next

	wbkNew.Close False

	saveRangeAsImage = (Err.Number = 0)

End Function

Function getTimeStamp()
    Dim dt, tm, ms
    tm = Timer
    ms = Fix((tm - Fix(tm)) * 1000)
    tm = Fix(tm)
    getTimeStamp = (tm \ (60 * 60)) & ":" & ((tm \ 60) Mod 60) & ":" & (tm Mod 60) & "." & ms
End Function

Function findRowByPosition(sht, ByVal rowBegin, ByVal rowEnd, ByVal pos)
    Dim height, rowPrev, row, rowBeginOrg
    row = rowEnd
    rowBeginOrg = rowBegin
    rowPrev = row
    Do
        height = sht.Range("A" & rowBeginOrg & ":A" & row).height
        If height < pos Then
            rowBegin = row
        Else
            rowEnd = row
        End If
        row = (rowEnd - rowBegin) \ 2 + rowBegin
        If row = rowPrev Then
            findRowByPosition = row
            Exit Function
        End If
        rowPrev = row
    Loop
End Function

Function findColumnByPosition(sht, ByVal columnBegin, ByVal columnEnd, ByVal pos)
    Dim width, columnPrev, column, columnBeginOrg
    column = columnEnd
    columnBeginOrg = columnBegin
    columnPrev = column
    Do
        width = sht.Range(sht.Cells(1, columnBeginOrg), sht.Cells(1, column)).width
        If width < pos Then
            columnBegin = column
        Else
            columnEnd = column
        End If
        column = (columnEnd - columnBegin) \ 2 + columnBegin
        If column = columnPrev Then
            findColumnByPosition = column
            Exit Function
        End If
        columnPrev = column
    Loop
End Function

Function saveSheetAsImage(sht, basefilename)
	Dim rngUsed, rngImage, row, rowEnd, column, columnEnd, filename, numX, numY, imageWidth, imageHeight
	imageWidth = regRead(RegKeyPath & "ImageWidth", 1000)
	imageHeight = regRead(RegKeyPath & "ImageHeight", 3000)
	Set rngUsed = getUsedRangeIncludingShapes(sht)
	numX = 1
	column = rngUsed.Column
	Do 
		columnEnd = findColumnByPosition(sht, column, rngUsed.Column + rngUsed.Columns.Count - 1, imageWidth)
		numY = 1
		row = rngUsed.Row
		Do 
			rowEnd = findRowByPosition(sht, row, rngUsed.Row + rngUsed.Rows.Count - 1, imageHeight)
			Set rngImage = sht.Range(sht.Cells(row, column), sht.Cells(rowEnd, columnEnd))
			filename = basefilename & "(" & numX & "-" & numY & ").png"
			If Not saveRangeAsImage(sht, rngImage, filename) Then
				Dim shtNew
				Set shtNew = sht.Parent.Sheets.Add
				shtNew.Range("A1") = getTimeStamp() & ": Error" & Err.Number & ": " & Err.Description
				shtNew.Columns.AutoFit
				saveRangeAsImage shtNew, shtNew.Range("A1"), filename
				shtNew.Delete
			End If
			row = rowEnd + 1
			numY = numY + 1
		Loop While row < rngUsed.row + rngUsed.Rows.Count - 1 
		column = columnEnd + 1
		numX = numX + 1
	Loop While column < rngUsed.column + rngUsed.Columns.Count - 1 
End Function

Function saveSheetAsHTML(sht, filename)
    Dim pobj
    Set pobj = sht.Parent.PublishObjects.Add(1, filename, sht.Name, "", 0, "CompareMSExcelFilesPlugin", "") ' 1 = xlSourceSheet, 0 = xlHtmlStatic
    pobj.Publish True
    pobj.AutoRepublish = False
End Function

Function escape(sheetName)
    escape = Replace(sheetName, "%", "%25")
    escape = Replace(escape, "<", "%3C")
    escape = Replace(escape, ">", "%3E")
    escape = Replace(escape, "|", "%7C")
    Select Case Left(UCase(escape), 3)
    Case "CON", "AUX", "NUL"
        If Len(escape) = 3 Then
            escape = "%" & escape
        End If
    Case "COM", "LPT"
        If Len(escape) = 4 And IsNumeric(Mid(escape, 4, 1)) Then
            escape = "%" & escape
        End If
    End Select
End Function

Function get_PluginEvent()
	get_PluginEvent = "FILE_FOLDER_PACK_UNPACK"
End Function

Function get_PluginDescription()
	get_PluginDescription = "Display the text content of MS Excel files"
End Function

Function get_PluginFileFilters()
	get_PluginFileFilters = "\.xls(\..*)?$;\.xlsx(\..*)?$;\.xlsm(\..*)?$;\.xlsb(\..*)?;\.xla(\..*)?$;\.xlax(\..*)?$;\.xltx(\..*)?$;\.xltm(\..*)?$"
End Function

Function get_PluginIsAutomatic()
	get_PluginIsAutomatic = True
End Function

Function get_PluginUnpackedFileExtension()
	get_PluginUnpackedFileExtension = ".tsv"
End Function

Function get_PluginExtendedProperties()
	get_PluginExtendedProperties = "ProcessType=Content Extraction;FileType=MS-Excel;MenuCaption=MS-Excel"
End Function

Function UnpackFile(fileSrc, fileDst, pbChanged, pSubcode)
	Dim fo
	Dim xl
	Dim wbk
	Dim sht
	Dim cmp
	Dim fileSrc2

	Set fo = fso.CreateTextFile(fileDst, True, True)

	Set xl = CreateObject("Excel.Application")
	xl.EnableEvents = False
	xl.DisplayAlerts = False

	fileSrc2 = fileSrc
	If fso.GetExtensionName(fileSrc2) = "lnk" Then
		fileSrc2 = wsh.CreateShortcut(fileSrc2).TargetPath
		If Not fso.FileExists(fileSrc2) Then
			Err.Raise 30001, "CompareMSExcelFiles.sct", fileSrc & ": Target file '" & fileSrc2 & "' not found"
		End If 
	End If
	Set wbk = xl.Workbooks.Open(fileSrc2, regRead(RegKeyPath & "UpdateLinks", 0),,,,,,,,,,,,True)

	On Error Resume Next

	If regRead(RegKeyPath & "CompareDocumentProperties", False) Then
		fo.WriteLine "[Document Properties]"
		writeObjectProperties fo, wbk.BuiltinDocumentProperties
		fo.WriteLine ""
	End If

	If regRead(RegKeyPath & "CompareNames", True) Then
		fo.WriteLine "[Names]"
		writeObjectProperties fo, wbk.Names
		fo.WriteLine ""
	End If

	For Each sht In wbk.Worksheets
		If regRead(RegKeyPath & "CompareCellValues", True) Then
			fo.WriteLine "[" & sht.Name & "]"
			writeCellValues fo, sht
			fo.WriteLine ""
		End If
		If regRead(RegKeyPath & "CompareFormulas", False) Then
			fo.WriteLine "[" & sht.Name & ".Formulas]"
			writeFormulas fo, sht
			fo.WriteLine ""
		End If
		If regRead(RegKeyPath & "CompareTextsInShapes", True) Then
			fo.WriteLine "[" & sht.Name & ".Shapes]"
			ungroupShapes sht
			writeTextsInShapes fo, sht
			fo.WriteLine ""
		End If
		If regRead(RegKeyPath & "CompareHeadersAndFooters", True) Then
			fo.WriteLine "[" & sht.Name & ".HeadersAndFooters]"
			writeHeadersAndFooters fo, sht
			fo.WriteLine ""
		End If
	Next

	If regRead(RegKeyPath & "CompareVBAMacros", True) Then
		If Not isAccessibleVBAProject(wbk) Then
			fo.WriteLine MsgCannotGetMacros
		End If
		For Each cmp In wbk.VBProject.VBComponents
			fo.WriteLine "[CodeModule." & cmp.Name & "]"
			If cmp.CodeModule.CountOfLines > 0 Then
				fo.WriteLine cmp.CodeModule.Lines(1, cmp.CodeModule.CountOfLines)
			End If
			fo.WriteLine ""
		Next
	End If

	Set sht = Nothing
	wbk.Close
	Set wbk = Nothing
	xl.Quit
	Set xl = Nothing
	fo.Close
	Set fo = Nothing

	pbChanged = True
	pSubcode = 0
	UnpackFile = True

End Function

Function PackFile(fileSrc, fileDst, pbChanged, pSubcode)
	PackFile = False
End Function

Function IsFolder(file)
	IsFolder = regRead(RegKeyPath & "UnpackToFolder", False)
End Function

Function UnpackFolder(fileSrc, folderDst, pbChanged, pSubcode)
	Dim fo
	Dim xl
	Dim wbk
	Dim sht
	Dim cmp
	Dim digit
	Dim zf
	Dim No
	Dim fileSrc2

	If Not fso.FolderExists(folderDst) Then fso.CreateFolder folderDst

	Set xl = CreateObject("Excel.Application")
	xl.EnableEvents = False
	xl.DisplayAlerts = False

	Dim backgroundChecking
	backgroundChecking = xl.ErrorCheckingOptions.BackgroundChecking
	xl.ErrorCheckingOptions.BackgroundChecking = False

	fileSrc2 = fileSrc
	If fso.GetExtensionName(fileSrc2) = "lnk" Then
		fileSrc2 = wsh.CreateShortcut(fileSrc2).TargetPath
		If Not fso.FileExists(fileSrc2) Then
			Err.Raise 30001, "CompareMSExcelFiles.sct", fileSrc & ": Target file '" & fileSrc2 & "' not found"
		End If 
	End If
	Set wbk = xl.Workbooks.Open(fileSrc2, regRead(RegKeyPath & "UpdateLinks", 0),,,,,,,,,,,,True)

	On Error Resume Next

	digit = Len(CStr(wbk.Worksheets.Count))
	If digit <= 0 Then digit = 1
	zf = String(digit, "0")

	If regRead(RegKeyPath & "CompareDocumentProperties", False) Then
		Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(" & zf & ")DocumentProperties.txt"), True, True)
		writeObjectProperties fo, wbk.BuiltinDocumentProperties
		fo.Close
	End If

	If regRead(RegKeyPath & "CompareNames", True) Then
		Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(" & zf & ")Names.txt"), True, True)
		writeObjectProperties fo, wbk.Names
		fo.Close
	End If

	No = 1
	For Each sht In wbk.Worksheets
		No = Right(zf & CInt(No), digit)
		If regRead(RegKeyPath & "CompareCellValues", True) Then
			Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(" & No & ")" & escape(sht.Name) & ".tsv"), True, True)
			writeCellValues fo, sht
			fo.Close
		End If

		If regRead(RegKeyPath & "CompareFormulas", False) Then
			Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(" & No & ")" & escape(sht.Name) & "_Formulas.txt"), True, True)
			writeFormulas fo, sht
			fo.Close
		End If

		If regRead(RegKeyPath & "CompareTextsInShapes", True) Then
			Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(" & No & ")" & escape(sht.Name) & "_Shapes.txt"), True, True)
			ungroupShapes sht
			writeTextsInShapes fo, sht
			fo.Close
		End If

		If regRead(RegKeyPath & "CompareHeadersAndFooters", True) Then
			Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "(" & No & ")" & escape(sht.Name) & "_HeadersAndFooters.txt"), True, True)
			writeHeadersAndFooters fo, sht
			fo.Close
		End If

		If regRead(RegKeyPath & "CompareWorksheetsAsImage", True) Then
			saveSheetAsImage sht, fso.BuildPath(folderDst, "(" & No & ")" & escape(sht.Name))
		End If

		If regRead(RegKeyPath & "CompareWorksheetsAsHTML", False) Then
			saveSheetAsHTML sht, fso.BuildPath(folderDst, "(" & No & ")" & escape(sht.Name) & ".html")
		End If
		No = CInt(No) + 1
	Next
	If regRead(RegKeyPath & "CompareVBAMacros", True) Then
		If Not isAccessibleVBAProject(wbk) Then
			Set fo = fso.CreateTextFile(fso.BuildPath(folderDst, "CannotGetMacros.bas"), True, True)
			fo.WriteLine MsgCannotGetMacros
			fo.Close
		End If

		For Each cmp In wbk.VBProject.VBComponents
			cmp.Export fso.BuildPath(folderDst, cmp.Name & getModuleExtension(cmp))
		Next
	End If

	Set sht = Nothing
	wbk.Close
	Set wbk = Nothing
	xl.ErrorCheckingOptions.BackgroundChecking = backgroundChecking
	xl.Quit
	Set xl = Nothing
	Set fo = Nothing

	pbChanged = True
	pSubcode = 0
	UnpackFolder = True
End Function

Function PackFolder(fileSrc, folderDst, pbChanged, pSubcode)
	PackFolder = False
End Function

Function ShowSettingsDialog()
	Dim tname: tname = fso.BuildPath(fso.GetSpecialFolder(2), fso.GetTempName() & ".hta")
	Dim tfile: Set tfile = fso.CreateTextFile(tname)
	Dim mshta
	tfile.Write getResource("dialog1")
	tfile.Close
	mshta = wsh.ExpandEnvironmentStrings("%SystemRoot%\mshta.exe")
	If Not fso.FileExists(mshta) Then
		mshta = wsh.ExpandEnvironmentStrings("%SystemRoot%\SysWOW64\mshta.exe")
	End If
	Run wsh, """" & mshta & """ """ & tname & """"
	fso.DeleteFile tname 
End Function

Sub Run(sh, cmd)
	sh.Run cmd, 1, True
End Sub

</script>

<resource id="dialog1">
<![CDATA[
<html>
  <head>
    <title>CompareMSExcelFiles.sct WinMerge Plugin Options</title>
    <meta content="text/html" charset="Shift_JIS">
    <style>
      body { background-color: lightgray; }
      ul { list-style:none; }
    </style>
    <script type="text/javascript">
        var REGKEY_PATH = "HKCU\\Software\\Thingamahoochie\\WinMerge\\Plugins\\CompareMSExcelFiles.sct\\";

        function regRead(key, defaultValue) {
            try {
                return (new ActiveXObject("WScript.Shell")).RegRead(key);
            } catch (e) {
                return defaultValue;
            }
        }

        function regWrite(key, value, type) {
            (new ActiveXObject("WScript.Shell")).RegWrite(key, value, type);
        }

        function onload() {
            var w = 800, h = 600;
            window.resizeTo(w, h);
            window.moveTo((screen.width - w) / 2, (screen.height - h) / 2);

            cboLanguage.selectedIndex = navigator.browserLanguage.substr(0, 2) === 'ja' ? 1 : 0;
            setLanguage(navigator.browserLanguage);
            chkUnpackToFolder.checked = regRead(REGKEY_PATH + "UnpackToFolder", false);
            chkUpdateLinks.checked = (regRead(REGKEY_PATH + "UpdateLinks", 0) == 3);
            chkCompareDocumentProperties.checked = regRead(REGKEY_PATH + "CompareDocumentProperties", false);
            chkCompareNames.checked = regRead(REGKEY_PATH + "CompareNames", true);
            chkCompareCellValues.checked = regRead(REGKEY_PATH + "CompareCellValues", true);
            chkCompareWorksheetsAsImage.checked = regRead(REGKEY_PATH + "CompareWorksheetsAsImage", true);
            chkCompareWorksheetsAsHTML.checked = regRead(REGKEY_PATH + "CompareWorksheetsAsHTML", false);
            txtImageWidth.value = regRead(REGKEY_PATH + "ImageWidth", 1000);
            txtImageHeight.value = regRead(REGKEY_PATH + "ImageHeight", 3000);
            chkCompareFormulas.checked = regRead(REGKEY_PATH + "CompareFormulas", false);
            chkCompareTextsInShapes.checked = regRead(REGKEY_PATH + "CompareTextsInShapes", true);
            chkCompareHeadersAndFooters.checked = regRead(REGKEY_PATH + "CompareHeadersAndFooters", true);
            chkCompareVBAMacros.checked = regRead(REGKEY_PATH + "CompareVBAMacros", true);
            chkUnpackToFolder_onclick();
            chkCompareWorksheetsAsImage_onclick();
            chkCompareWorksheetsAsHTML_onclick();
        }

        function setLanguage(lang) {
            var div = document.getElementById("language") || document.createElement('div')
            div.id = "language";
            var html = '&shy;<style>';
            if (lang.substr(0, 2).toLowerCase() === 'ja') {
                html += '.en { display: none } .ja { display: inline }';
            } else {
                html += '.ja { display: none } .en { display: inline }';
            }
            div.innerHTML = html;
            if (!document.getElementById("language"))
                document.body.appendChild(div);
        }

        function cboLanguage_onclick() {
            setLanguage(cboLanguage.options[cboLanguage.selectedIndex].text);
        }

        function chkUnpackToFolder_onclick() {
            if (!chkUnpackToFolder.checked) {
                chkCompareWorksheetsAsImage.checked = false;
                chkCompareWorksheetsAsHTML.checked = false;
            }
        }

        function chkCompareWorksheetsAsImage_onclick() {
            if (chkCompareWorksheetsAsImage.checked)
                chkUnpackToFolder.checked = true;
        }

        function chkCompareWorksheetsAsHTML_onclick() {
            if (chkCompareWorksheetsAsHTML.checked)
                chkUnpackToFolder.checked = true;
        }

        function btnOk_onclick() {
            regWrite(REGKEY_PATH + "UnpackToFolder", chkUnpackToFolder.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "UpdateLinks", chkUpdateLinks.checked ? 3 : 0, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareDocumentProperties", chkCompareDocumentProperties.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareNames", chkCompareNames.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareCellValues", chkCompareCellValues.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareWorksheetsAsImage", chkCompareWorksheetsAsImage.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareWorksheetsAsHTML", chkCompareWorksheetsAsHTML.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "ImageWidth", Number(txtImageWidth.value), "REG_DWORD");
            regWrite(REGKEY_PATH + "ImageHeight", Number(txtImageHeight.value), "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareFormulas", chkCompareFormulas.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareTextsInShapes", chkCompareTextsInShapes.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareHeadersAndFooters", chkCompareHeadersAndFooters.checked, "REG_DWORD");
            regWrite(REGKEY_PATH + "CompareVBAMacros", chkCompareVBAMacros.checked, "REG_DWORD"); window.close();
        }

        function btnCancel_onclick() {
            window.close();
        }

        function onlyNumeric() {
            var k = event.keyCode;

            if (event.shiftKey) {
                if (k == 37 || k == 39) {
                    return true;
                }
            } else if (event.altKey) {
                return true;
            } else if (event.ctrlKey) {
                return true;
            } else if (k == 8 || k == 9 || k == 37 || k == 39 || k == 46 /* || k == 144 */
                || (48 <= k && k <= 57) || (96 <= k && k <= 105)) {
                return true;
            }

            event.returnValue = false;
            return false;
        }

        function onPaste() {
            var paste = (event.clipboardData || window.clipboardData).getData("text");
            if (!paste.match(/^[0-9]{0,4}$/)) {
                paste = "";
                if (event.preventDefault) {
                    event.preventDefault();
                }
                event.returnValue = false;
                return false;
            } else {
                return true;
            }
        }
    </script>
  </head>
  <body onload="onload();">
    <div>
      <ul>
        <li>
          <label class="en">Language: </label>
          <label class="ja">言語: </label>
          <select id="cboLanguage" onclick="cboLanguage_onclick();" >
            <option value="English">English</option>
            <option value="Japanese">Japanese</option>
          </select>
        </li>
      </ul>
      <ul>
        <li>
          <input id="chkUnpackToFolder" type="checkbox" onclick="chkUnpackToFolder_onclick();"/>
          <label class="en">Extract workbook data to multiple files</label>
          <label class="ja">ワークブックの情報を複数ファイルに展開する</label>
        </li>
        <li>
          <input id="chkUpdateLinks" type="checkbox" />
          <label class="en">Update external references(links)</label>
          <label class="ja">比較時、外部参照(リンク)を更新する</label>
        </li>
        <li>
          <input id="chkCompareDocumentProperties" type="checkbox" />
          <label class="en">Compare document properties</label>
          <label class="ja">ドキュメントプロパティを比較する</label>
        </li>
        <li>
          <input id="chkCompareNames" type="checkbox" />
          <label class="en">Compare names</label>
          <label class="ja">名前の定義を比較する</label>
        </li>
        <li>
          <input id="chkCompareCellValues" type="checkbox" />
          <label class="en">Compare cell values</label>
          <label class="ja">各セルの値を比較する</label>
        </li>
        <li>
          <input id="chkCompareWorksheetsAsImage" type="checkbox" onclick="chkCompareWorksheetsAsImage_onclick();"/>
          <label class="en">Compare worksheets as image (very slow)</label>
          <label class="ja">ワークシートを画像化して比較する (処理時間増大)</label>
          <span class="en"> - Image split size: </span>
          <span class="ja"> - 画像分割サイズ: </span>
          <input id="txtImageWidth"  type="text" size="5" maxlength="4" onkeyDown="return onlyNumeric();" onpaste="return onPaste();" />
          <span> x </span>
          <input id="txtImageHeight" type="text" size="5" maxlength="4" onkeyDown="return onlyNumeric();" onpaste="return onPaste();" />
        </li>
        <li>
          <input id="chkCompareWorksheetsAsHTML" type="checkbox" onclick="chkCompareWorksheetsAsHTML_onclick();"/>
          <label class="en">Compare worksheets as HTML</label>
          <label class="ja">ワークシートをHTMLとして保存して比較する</label>
        </li>
        <li>
          <input id="chkCompareFormulas" type="checkbox" />
          <label class="en">Compare formulas</label>
          <label class="ja">数式を比較する</label>
        </li>
        <li>
          <input id="chkCompareTextsInShapes" type="checkbox" />
          <label class="en">Compare texts in shapes</label>
          <label class="ja">図形内のテキストを比較する</label>
        </li>
        <li>
          <input id="chkCompareHeadersAndFooters" type="checkbox" />
          <label class="en">Compare headers and footers</label>
          <label class="ja">ヘッダーとフッターを比較する</label>
        </li>
        <li>
          <input id="chkCompareVBAMacros" type="checkbox" />
          <label class="en">Compare VBA macros</label>
          <label class="ja">VBA マクロを比較する</label>
        </li>
      </ul>
      <ul>
        <li>
          <input class="en" type="button" onclick="btnOk_onclick();" value="OK" />
          <input class="en" type="button" onclick="btnCancel_onclick();" value="Cancel" />
          <input class="ja" type="button" onclick="btnOk_onclick();" value="OK" />
          <input class="ja" type="button" onclick="btnCancel_onclick();" value="キャンセル" />
        </li>
      </ul>
    </div>
  </body>
</html>
]]>
</resource>

</scriptlet>
