<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * @see \Magento\Customer\Block\Adminhtml\System\Config\Validatevat
 */
?>
<script>
require(['prototype'], function(){

//<![CDATA[
    function validateVat() {
        var validationMessage = $('validation_result');

        params = {
            country: $('<?php /* @escapeNotVerified */ echo $block->getMerchantCountryField(); ?>').value,
            vat: $('<?php /* @escapeNotVerified */ echo $block->getMerchantVatNumberField(); ?>').value
        };

        new Ajax.Request('<?php /* @escapeNotVerified */ echo $block->getAjaxUrl() ?>', {
            parameters: params,
            onSuccess: function(response) {
                var result = '<?php /* @escapeNotVerified */ echo __('Error during VAT Number verification.') ?>';
                try {
                    if (response.responseText.isJSON()) {
                        response = response.responseText.evalJSON();
                        result = response.message;
                    }
                    if (response.valid == 1) {
                        validationMessage.removeClassName('hidden').addClassName('success')
                    } else {
                        validationMessage.removeClassName('hidden').addClassName('error')
                    }
                } catch (e) {
                    validationMessage.removeClassName('hidden').addClassName('error')
                }
                validationMessage.update(result);
            }
        });
    }

    window.validateVat = validateVat;
//]]>

});
</script>
<div class="actions actions-validate-vat">
    <div id="validation_result" class="message-validation hidden"></div>
    <button onclick="javascript:validateVat(); return false;" class="action-validate-vat" type="button" id="<?php echo $block->getHtmlId() ?>">
        <span><?php echo $block->escapeHtml($block->getButtonLabel()) ?></span>
    </button>
</div>
