<%@ taglib uri="/struts-tags" prefix="ww" %>
<ww:set name="successMessageKey">${param.messagePrefix}.testconnection.success</ww:set>
<ww:set name="failureMessageKey">${param.messagePrefix}.testconnection.invalid</ww:set>
<ww:property value="webResourceManager.requireResource('crowd.web.resources:extract-error-message')"/>
        function testConfiguration() {
            var data = {};
            AJS.$("#${param.formId}").serializeArray().map(function(x){data[x.name] = x.value;});
            var $testConnectionButton = AJS.$('#test-connection');
            $testConnectionButton.prop("disabled", true);
            var $testConnectionSpinner = AJS.$("#test-connection-spinner");
            $testConnectionSpinner.spin();
            AJS.$.ajax({
                url: AJS.contextPath() + "/rest/directorymanagement/1/directory/test${param.directoryType}/${param.directoryId}",
                type: 'POST',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json'
            }).done(function() {

                require(['aui/flag'], function(flag) {
                    var myFlag = flag({
                        type: 'success',
                        title: '<ww:property value="getText('directorycrowd.testconnection.flag.success.title')"/>',
                        body: '<div class="aui-message-wrapped-body"><ww:property value="getText(#successMessageKey)"/></div>'
                    });
                    setTimeout(function() {
                        myFlag.close();
                    }, 5000);
                });
            }).fail(function(jqxhr) {
                if (jqxhr.status == 401) {
                    window.location.replace(AJS.contextPath() + "/console/login.action");
                } else {
                    require(['aui/flag'], function (flag) {
                        flag({
                            type: 'error',
                            title: '<ww:property value="getText('directorycrowd.testconnection.flag.failure.title')"/>',
                            body: '<div class="aui-message-wrapped-body"><ww:property value="getText(#failureMessageKey)"/>' + " " + Crowd.extractErrorMessage(jqxhr.responseText) + "</div>"
                        });
                    });
                }
            }).always(function() {
                $testConnectionButton.prop("disabled", false);
                $testConnectionSpinner.spinStop();
            });
        }