<?php
/**
 * Copyright 2013 Zendesk.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
?>
<button style="" onclick="javascript:checkZendApiConnection(); return false;" class="scalable" type="button" id="<?php echo $this->getHtmlId() ?>">
    <span><?php echo $this->escapeHtml($this->getButtonLabel()) ?></span>
</button>
<script type="text/javascript">
    function checkZendApiConnection() {
        var request = new Ajax.Request('<?php echo $this->getUrl('adminhtml/zendesk/checkOutbound'); ?>', {
            method: 'get',
            parameters: {
                domain: document.getElementById('zendesk_general_domain').value,
                username: document.getElementById('zendesk_general_email').value,
                password: document.getElementById('zendesk_general_password').value
            },
            onCreate: function() {
                document.getElementById('zendesk-api-connection-results').innerHTML = '';
            },
            onSuccess: function(transport) {
                if (transport.responseJSON.success === true) {
                    var message = '<ul class="messages"><li class="success-msg">' + transport.responseJSON.msg + '</li></ul>';
                    document.getElementById('zendesk-api-connection-results').innerHTML = message;
                } else {
                    var message = '<ul class="messages"><li class="error-msg">' + transport.responseJSON.msg +
                              '</li></ul>';
                    document.getElementById('zendesk-api-connection-results').innerHTML = message;
                }
            }
        });
    }
</script>
<div id="zendesk-api-connection-results"></div>

