<div id="popSaveCart"></div>
<script type="text/javascript">
    $(function () {
        var buttonIndicator;
        $("#popSaveCart").dxPopup({
            title: widgetOptions.popupTitle || 'Save Cart',
            contentTemplate: $('#popSaveCart-template'),
            width: widgetOptions.popupWidth || 450,
            height: widgetOptions.popupHeight || 250,
            onShowing: function () {
                $("#txtSavedCartName").dxTextBox({
                    placeholder: widgetOptions.nameFieldEmptyText || 'Cart Name',
                    width: widgetOptions.nameFieldWidth || 400,
                    maxLength: 50
                });
                $("#btnSaveCart").dxButton({
                    text: 'Save Cart',
                    onClick: function (e) {
                        e.component.option('text', 'Saving...');
                        buttonIndicator.option('visible', true);
                        saveCart($('#txtSavedCartName').dxTextBox('instance').option('value'));
                    },
                    template: function(data, container) {
                        $("<div class='button-indicator DT-Button'></div><span class='dx-button-text DT-Button'>" + data.text + "</span>").appendTo(container);
                        buttonIndicator = container.find(".button-indicator").dxLoadIndicator({
                            visible: false,
                            width: 24, 
                            height: 24, 
                        }).dxLoadIndicator("instance");
                    }
                });
            },
            onHidden: function () {
                $('#txtSavedCartName').dxTextBox('instance').option('value', '');
                $('.message').css("display", "none");
                $('.form').css("display", "block");
            },
            animation: {
                hide: "{ type: 'pop', duration: 400, from: { position: { my: 'center', at: 'center', of: window } }, to: { position: { my: 'top', at: 'bottom', of: window } }} (iOS)",
                show: "{ type: 'pop', duration: 400, from: { position: { my: 'top', at: 'bottom', of: window } }, to: { position: { my: 'center', at: 'center', of: window } }} (iOS)"
            },
        });
        function saveCart(name) {
            MakeAJAXCall("Cart.SaveCart", { Name: name.replace("'","\\'") }, validateSavedCart)
        }
        function validateSavedCart(DATA) {
            if (DATA != '') {
                var d = JSON.parse(DATA);
                var result = d.Result;
                $('.form').css("display", "none");
                $('.message').css("display", "block");
                $("h3.message").text(result.Message);
                if (result.Success) {
                    setTimeout(function () { $("#popSaveCart").dxPopup("hide"); }, 2000)
                }
            }
        }
    });
</script>
<script type="text/html" id="popSaveCart-template">
    <% var instructionText = widgetOptions.instructionText || 'Enter the name you would like to assign to your Saved Cart.' %>
    <div class="popupWrapper form">
        <p>
            <%= instructionText %>
        </p>
        <div id="txtSavedCartName"></div>
        <br />
        <div id="btnSaveCart"></div>
    </div>
    <div class="popupWrapper message">
        <h3 class="message"></h3>
    </div>
</script>
<style type="text/css">
    .popupWrapper{text-align:center;padding:20px}#button,#button .dx-button-content{padding:0}#button .button-indicator{height:24px;width:24px;display:inline-block;vertical-align:middle;margin-right:10px}.message{display:none}
</style>