<?php
/**
 * OpenMage
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available at https://opensource.org/license/afl-3-0-php
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
 * @copyright   Copyright (c) 2022 The OpenMage Contributors (https://www.openmage.org)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<script type="text/javascript">
//<![CDATA[
    /**
     * Name and ID for iframe for data POST-ing.
     *
     * @var string
     */
    varienForm.prototype.ifrElemName = 'import_post_target_frame';

    /**
     * Post form data to dynamic iframe.
     *
     * @param string newActionUrl OPTIONAL Change form action to this if specified
     * @return void
     */
    varienForm.prototype.postToFrame = function(newActionUrl)
    {
        if(this.validate()){
            // create dynamic frame
            if (!$(this.ifrElemName)) {
                $('html-body').insert({
                    bottom:'<iframe name="' + this.ifrElemName + '" id="' + this.ifrElemName + '" style="display:none;"/>'
                });
            }

            // show mask
            showLoader();

            // temporary set new target and submit form
            var formElem    = $(this.formId);
            var oldTarget   = formElem.target;
            var oldAction   = formElem.action;

            formElem.target = this.ifrElemName;

            if (newActionUrl) {
                formElem.action = newActionUrl;
            }
            formElem.action += (formElem.action.lastIndexOf('?') != -1 ? '&' : '?')
                + 'form_key=' + encodeURIComponent(formElem.form_key.value);
            this._submit();
            formElem.target = oldTarget;
            formElem.action = oldAction;
        }
    };

    /**
     * Actions after data post complete. Hide load mask, remove iframe, run response processing method.
     *
     * @param object response
     * @return void
     */
    varienForm.prototype.postToFrameComplete = function(response)
    {
        $(this.ifrElemName).remove();
        hideLoader();
        this.postToFrameProcessResponse(response);
    };

    /**
     * Process response JSON.
     *
     * @param object response
     * @return void
     */
    varienForm.prototype.postToFrameProcessResponse = function(response)
    {
        if ('object' != typeof(response)) {
            return alert('<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Invalid response')) ?>');
        }
        $H(response).each(function(pair) {
            switch (pair.key) {
                case 'show':
                case 'clear':
                case 'hide':
                    $H(pair.value).each(function(val) {if ($(val.value)) $(val.value)[pair.key]();});
                    break;
                case 'innerHTML':
                case 'value':
                    $H(pair.value).each(function(val) {
                        var el = $(val.key);
                        if (el) {
                            el[pair.key] = val.value;
                            // if element does not visible
                            el.offsetWidth || el.up('div.entry-edit').show();
                        }
                    });
                    break;
                case 'removeClassName':
                case 'addClassName':
                    $H(pair.value).each(function(val) {if ($(val.key)) $(val.key)[pair.key](val.value);});
                    break;
                default:
                    alert(pair.key + ': ' + pair.value);
                    break;
            }
        });
    };

    /**
     * Send form data to Start action when file is validated.
     *
     * @param string newActionUrl
     * @param string sourceFileField
     * @return void
     */
    varienForm.prototype.startImport = function(newActionUrl, sourceFileField)
    {
        $(sourceFileField).removeClassName('required-entry');
        this.postToFrame(newActionUrl);
        $(sourceFileField).addClassName('required-entry');
    };
//]]>
</script>
