<script type="text/javascript">
    'use strict'
	function check(el) {
		if (el.checked=="") el.checked="checked";
	}

    /**
     * Check if user tries to update an
     * OPEN instance from source version < 29
     * to target version >= 29 to inform him
     * about the feature limitations.
     *
     * @param successHandler
     */
    function open29NoticeHandler(successHandler) {
        const thresholdRevision = 202429000;
        const currentRevision = [{$g_info.revision}];
        const selectedRevision = $$('input[name=dir]:checked').first();
        const targetRevision = selectedRevision && selectedRevision.getAttribute('data-revision');

        const isSourceBefore29 = currentRevision < thresholdRevision;
        const isTargetAfter29 = !targetRevision || targetRevision >= thresholdRevision;
        const isOpen = '[{$g_product_info.type}]' === 'OPEN';

        if (isOpen && isSourceBefore29 && isTargetAfter29) {
            $('overlay').show();
            $('modal').show();

            let counter = 10;
            const counterInterval = setInterval(() => {
                $('counter').innerHTML = `Please wait 00:${(--counter).toString().padStart(2, '0')} seconds...`
                if (counter <= 0) {
                    clearInterval(counterInterval);
                    $('confirm').enable();
                    $('confirm').removeClassName('disabled');
                    $('counter').remove();
                }
            }, 1000);
            return;
        }
        successHandler();
    }
</script>

[{include '../partials/open-29-notice.tpl'}]

<h2>Available Updates</h2>

[{if !$licence_error}]
	<h3>New Versions</h3>

	<p>New version can be retrieved via <a href="[{$site_url}]">[{$site_url}]</a>.</p>

	[{if is_array($g_update)}]
		There is a newer version available:

		<h3>[{$g_update.title}] - [{$g_update.version}]</h3>
		<p>Date: [{$g_update.release}] (rev [{$g_update.revision}])</p>
		<input type="hidden" name="download" value="1" />
		<input type="hidden" name="dl_file" value="[{$g_update.revision}]" />
		<input type="button" class="button" name="btn_download"  value="Download" onClick="if (!$('isys_form').down('[name=\'download\']').getValue().blank()) {$('isys_form').submit();}" />
	[{else}]
		[{if $g_downloaded}]
			Download successfull.
		[{else}]
			[{if $g_update_message}]
				<p class="bold message [{$g_update_message_class}]">[{$g_update_message}]</p>
			[{else}]
				<input type="hidden" id="check_update" name="check_update" value="false" />
				<input type="button" class="button" name="btn_check"  value="Check for a new version" onClick="$('check_update').setValue('true'); $('isys_form').submit();" />
			[{/if}]
		[{/if}]
	[{/if}]

	<h3>You can update to the following already downloaded versions:</h3>
	<table cellpadding="2" cellspacing="0" width="100%" class="listing" style="margin-top:15px;">
		<colgroup>
			<col width="100" />
		</colgroup>
		<thead>
			<tr>
				<th>Use</th>
				<th>Name</th>
				<th>Version</th>
				<th>Requirements</th>
			</tr>
		</thead>
		<tbody>
			[{if is_array($g_dirs) && count($g_dirs)>0}]
				[{foreach from=$g_dirs item=l_dir}]
				[{counter print=false assign="i"}]
				[{if $l_dir.changelog != "n/a"}]
				<div id="changelog_[{$l_dir.revision}]" class="changelog" style="display:none;">
					<div class="innerchangelog">
						<div class="header">
							<div class="header_left">Changelog</div>
							<div class="header_right">
								<a class="link" onclick="new Effect.Fade('changelog_[{$l_dir.revision}]', {duration:0.2});">close</a>
							</div>
							<div style="clear:both"></div>
						</div>
						<div class="bottom"><pre>[{$l_dir.changelog}]</pre></div>
					</div>
				</div>
				[{/if}]

				<tr class="[{cycle values="even,odd"}]" [{*onclick="check($('dir_[{$i}]'));"*}]>
					<td>
						<input
                            type="radio"
                            id="dir_[{$i}]"
                            name="dir"
                            value="[{$l_dir.directory}]"
                            [{if !empty($l_dir.revision) && $l_dir.revision < $l_dir.requirement.revision}]disabled="disabled"[{else}]checked="checked"[{/if}]
                            data-revision="[{$l_dir.revision}]"
                        />
					</td>
					<td>

					[{if $l_dir.revision < $g_info.revision}]<span style="color:#005e20;">[{/if}]
					[{if $l_dir.revision eq $g_info.revision}]<span style="font-weight:bold;">[{/if}]

					[{if !empty($l_dir.revision) && $l_dir.revision < $l_dir.requirement.revision}]<span style="color:#999;">[{/if}]

						[{$l_dir.title}][{if $l_dir.changelog != "n/a"}] (<a class="link" onclick="new Effect.Appear('changelog_[{$l_dir.revision}]', {duration:0.3});">see changelog</a>)[{/if}]

					</span>

					</td>
					<td>
						[{if !empty($l_dir.revision)}]
							[{$l_dir.version}] (rev [{$l_dir.revision}])
						[{/if}]
						</td>
					<td>
						[{if !empty($l_dir.revision)}]
							[{if $l_dir.revision < $l_dir.requirement.revision}]
								<strong style="color:red;">
									[{$l_dir.requirement.version}]
									[{if $l_dir.requirement.revision}] rev [{$l_dir.requirement.revision}][{/if}]
								</strong>
							[{else}]
								[{$l_dir.requirement.version}]
								[{if $l_dir.requirement.revision}] rev [{$l_dir.requirement.revision}][{/if}]
							[{/if}]
						[{/if}]
					</td>
				</tr>

				[{/foreach}]
			[{else}]
				<tr>
					<td colspan="2"><span>There don't seem to be any versions to update to.</span></td>
				</tr>
			[{/if}]
		</tbody>
	</table>

	<p>Select a version and click "Next >>" to go to the next step.</p>
	<span>Your current version is: <strong>[{$g_info.version|default:"<= 0.9"}] (rev [{$g_info.revision|default:"<= 2500"}])</strong></span>

[{else}]
	<br />
	<div class="exception p10">
		[{$licence_error}]
	</div>
[{/if}]
