<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, temporary set new target and submit form
            var loadingMask = $('loading-mask');
            var formElem    = $(this.formId);
            var oldTarget   = formElem.target;
            var oldAction   = formElem.action;

            Element.clonePosition(loadingMask, $$('#html-body .wrapper')[0], {offsetLeft:-2})
            toggleSelectsUnderBlock(loadingMask, false);
            loadingMask.show();
            setLoaderPosition();
            formElem.target = this.ifrElemName;

            if (newActionUrl) {
                formElem.action = newActionUrl;
            }
            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)
    {
        var loadingMask = $('loading-mask');
        $(this.ifrElemName).remove();
        toggleSelectsUnderBlock(loadingMask, true);
        loadingMask.hide();
        this.postToFrameProcessResponse(response);
    };

    /**
     * Process response JSON.
     *
     * @param object response
     * @return void
     */
    varienForm.prototype.postToFrameProcessResponse = function(response)
    {
        if ('object' != typeof(response)) {
            return alert('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) {if ($(val.key)) $(val.key)[pair.key] = val.value;});
                    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>
