<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="CSEasyWMGenerate.aspx.vb" Inherits="CSEasyWM.CSEasyWMGenerate" Async="True" %>
<%@ register tagprefix="ex" namespace="Exact.Web.UI.Controls" assembly="Exact.Web.UI.Controls" %>

<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="Exact.css" />
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            ShowHideElements();
            //if (SysGet('GenerateNow') == '1')
            //    GenerateDocuments();
        });
        function ShowHideElements() {
            if (SysGet('UIAction') == '1') { // confirm
                hideElement('csTemplateDocument');
                hideElement('csSelectionFields');
                hideElement('csIntegrationFields');
                hideElement('csOutputFormats');
                if (SysGet('HideGenerateSection') == '1')
                    hideElement('csGenerate');
            }
            else if (SysGet('UIAction') == '3') { // generate
                hideElement('csTemplateDocument');
                hideElement('csSelectionFields');
                hideElement('csIntegrationFields');
                hideElement('csOutputFormats');
                hideElement('csConfirmation');
            }
            else {
                hideElement('csConfirmation');
                if (SysGet('HideGenerateSection') == '1')
                    hideElement('csGenerate');
            }
        }

        function hideElement(name) {
            $("[id$='" + name + "']").hide();
        }
        function showElement(name) {
            $("[id$='" + name + "']").show();
        }

        function GenerateDocuments() {
            //SysSet('GenerateNow', 0);

            GenerateUpdateUI(0, 1, true);

            var checkInterval = 500; // 500ms check
            var timeoutDuration = 90000; // 90 seconden
            var elapsed = 0;

            var checkDownload = setInterval(function () {
                if (document.cookie.indexOf('fileDownload=true') !== -1) {
                    GenerateUpdateUI(0, 0, false);

                    clearInterval(checkDownload);
                    document.cookie = "fileDownload=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
                } else {
                    elapsed += checkInterval;
                    if (elapsed >= timeoutDuration) {
                        GenerateUpdateUI(0, 0, false);

                        clearInterval(checkDownload);
                        SysAlert(421015, 'Download has not been started. Please try again.');
                    }
                }
            }, checkInterval);
        }

        function GenerateUpdateUI(iPreview, iGenerate, bStart) {
            if (iPreview == 1)
                SysSet('UIAction', 2);
            if (iGenerate == 1)
                SysSet('UIAction', 3);

            if (bStart) {
                hideElement('csConfirmation');
                showElement('csGenerate');
                hideElement('infoMsg');
                hideElement('errNotifications');

                SysSubmit(1);
                sysIsSubmitted = false;
                sysWasSubmitted = false;

                $('#btnGenerate').attr('disabled', 'disabled');
                $('#btnPreview').attr('disabled', 'disabled');
            } else {
                showElement('csConfirmation');
                hideElement('csGenerate');
                showElement('infoMsg');
                showElement('errNotifications');

                SysWaitMessage(false);
                $('#btnGenerate').removeAttr('disabled');
                $('#btnPreview').removeAttr('disabled');
            }
        }


        function GenerateDocumentsPreview() {
            GenerateUpdateUI(1, 0, true);

            var checkInterval = 500; // 500ms check
            var timeoutDuration = 90000; // 90 seconden
            var elapsed = 0;

            var checkDownload = setInterval(function () {
                if (document.cookie.indexOf('fileDownload=true') !== -1) {
                    GenerateUpdateUI(0, 0, false);

                    clearInterval(checkDownload);
                    document.cookie = "fileDownload=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
                } else {
                    elapsed += checkInterval;
                    if (elapsed >= timeoutDuration) {
                        GenerateUpdateUI(0, 0, false);

                        clearInterval(checkDownload);
                        SysAlert(421015, 'Download has not been started. Please try again.');
                    }
                }
            }, checkInterval);
        }
    </script>
</head>
<body>


<ep:form id="Baco" runat="server">

<ex:inputfield runat="server" id="DefinitionID" validationas="Guid" hidden="True" CreateClientControl="True" />
<ex:inputfield runat="server" id="iid" validationas="Guid" hidden="True" CreateClientControl="True" />
<ex:inputfield runat="server" id="mc" validationas="Guid" hidden="True" CreateClientControl="True" />
<ex:inputfield runat="server" id="ival" hidden="True" CreateClientControl="True" />
<ex:inputfield runat="server" id="idesc" hidden="True" CreateClientControl="True" />

<ex:inputfield runat="server" id="UIAction" validationas="Int" validationdefault="0" ValidationLowerRange="0" ValidationUpperRange="3" hidden="True" CreateClientControl="True" />
<ex:inputfield runat="server" id="PreviousUIAction" validationas="Int" validationdefault="0" ValidationLowerRange="0" ValidationUpperRange="3" hidden="True" CreateClientControl="True" />

<%--<ex:inputfield runat="server" id="GenerateNow" validationas="Int" validationdefault="0" ValidationLowerRange="0" ValidationUpperRange="1" hidden="True" CreateClientControl="True" />--%>

<ex:inputfield runat="server" id="HideGenerateSection" validationas="Int" validationdefault="1" ValidationLowerRange="0" ValidationUpperRange="1" hidden="True" CreateClientControl="True" />
<ex:inputfield runat="server" id="Source" validationas="Int" validationdefault="0" hidden="True" CreateClientControl="True" />

<ex:inputfield runat="server" id="ReferenceURL" hidden="true" CreateClientControl="true" />

<ex:CardControl runat="server" ID="Card" Action="Show">
    <ex:Header runat="server" ID="hdr" caption="WordMerge" captionid="421001">
        <ex:HeaderButton ID="hbDefinition" ButtonStyle="Customize" Tooltip="WordMerge Definition" TooltipID="421008" />
        <ex:HeaderButton ID="hbFavorite" ButtonStyle="Favorite" /> 
    </ex:Header>

    <ex:ButtonBar runat="server" ID="Buttons">
        <ex:Button runat="server" ID="btnPrevious" ButtonStyle="Custom" Caption="Back" CaptionID="8365" />
        <ex:Button runat="server" ID="btnNext" ButtonStyle="Custom" Caption="Confirm" CaptionID="6593" />
        <ex:Button runat="server" ID="btnPreview" ButtonStyle="Custom" Caption="Preview" CaptionID="23150" />
        <ex:Button runat="server" ID="btnGenerate" ButtonStyle="Custom" Caption="Generate" CaptionID="330" />
        <ex:Button runat="server" ID="btnClose" ButtonStyle="Close" />
    </ex:ButtonBar>

    <ex:bcerrormessages runat="server" id="errMsg" />

    <ex:cardrow runat="server" id="crDefinition">
	    <ex:cardcolumn runat="server" id="ccDefinition">
		    <ex:cardsection runat="server" id="csDefinition" caption="WordMerge Definition" captionid="421008">                
                <ex:cardfield runat="server" id="cfDefinition" caption="WordMerge Definition" captionid="421008">
                    <ex:InputField runat="server" id="DefinitionTitle" ReadOnly="True" />
			    </ex:cardfield>
                <ex:cardfield runat="server" id="cfDefinitionDescription">
                    <ex:Literal runat="server" id="DefinitionDescription" />
			    </ex:cardfield>
                <ex:cardfield runat="server" id="cfDefinitionInTest">
                    <ex:Literal runat="server" id="DefinitionInTest" Caption="The definition is in Test mode, documents will not be stored in the database." CaptionID="421062" />
			    </ex:cardfield>
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

    <ex:cardrow runat="server" id="crTemplateDocument">
	    <ex:cardcolumn runat="server" id="ccTemplateDocument">
		    <ex:cardsection runat="server" id="csTemplateDocument" caption="Template" captionid="9844">                
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

    <ex:cardrow runat="server" id="crSelectionFields">
	    <ex:cardcolumn runat="server" id="ccSelectionFields">
		    <ex:cardsection runat="server" id="csSelectionFields" caption="Selection fields" captionid="421036">
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

    <ex:cardrow runat="server" id="crIntegrationFields">
	    <ex:cardcolumn runat="server" id="ccIntegrationFields">
		    <ex:cardsection runat="server" id="csIntegrationFields" caption="Integration" captionid="421106">
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

    <ex:cardrow runat="server" id="crOutputFormats">
	    <ex:cardcolumn runat="server" id="ccOutputFormats">
		    <ex:cardsection runat="server" id="csOutputFormats" caption="Output formats" captionid="421038">
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

    <ex:cardrow runat="server" id="crConfirmation">
	    <ex:cardcolumn runat="server" id="ccConfirmation">
		    <ex:cardsection runat="server" id="csConfirmation" caption="Confirm" captionid="6593">                
                <ex:cardfield runat="server" id="cfEditWord" caption="Edit in Word link" captionid="421280" >
                    <ex:checkbox runat="server" id="cbEditWord" />
                </ex:cardfield>
                <ex:cardfield runat="server" id="cfOpenSingle" caption="Open with 1 document" captionid="421281" >
                    <ex:checkbox runat="server" id="cbOpenSingle" />
                </ex:cardfield>
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

    <ex:cardrow runat="server" id="crGenerate">
	    <ex:cardcolumn runat="server" id="ccGenerate">
		    <ex:cardsection runat="server" id="csGenerate" caption="Generating..." captionid="7476">                
                <asp:Literal runat="server" id="litGenerating" />
        		<ex:bcerrormessages runat="server" id="infoMsg" Mode="Notification" />
                <ex:bcerrormessages runat="server" id="errNotifications" Mode="Notification" />
		    </ex:cardsection>
	    </ex:cardcolumn>
    </ex:cardrow>

</ex:CardControl>

</ep:form>
</body>
</html>
