﻿if (!this.AJAX) {
    this.AJAX = {};
}

if (typeof AJAX.call !== 'function') {
    AJAX.call = function (methodName, data, callback) {
        $.ajax({
            type: "POST",
            url: ajaxPageUrl + methodName,
            data: data,
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                try {
                    var responseParsed = parseJSON(response);
                    callback(responseParsed);
                }
                catch (e) {
                    ajaxFailed(e)
                }
            },
            failure: ajaxFailed
        });
    };
}

function toJSONString(parameterName, value) {
    return '"' + parameterName + '"' + ":" + JSON.stringify(value);
}

function ajaxFailed(message) {
    var htmlForm = '';
    htmlForm += '<div id="modalForm">';
    htmlForm += "<p>The action couldn't be completed: <br />" + message + "</p>";
    htmlForm += '</div>';
    $(htmlForm).dialog({
        modal: true,
        draggable: true,
        resizable: false,
        closeOnEscape: true,
        title: 'Ajax failed',
        buttons: { "Ok": function () { $(this).dialog("destroy").remove(); } },
        close: function () { $(this).dialog("destroy").remove(); }
    });
}

function AjaxFireAndForgetSuccess(response) {
}

function parseJSON(s) {
    return JSON.parse(s, function (key, value) {
        var type;
        if (value && typeof value === 'object') {
            type = value.type;
            if (typeof type === 'string' && typeof window[type] === 'function') {
                return new (window[type])(value);
            }
        }
        return value;
    });
}

function recordFormFocus(obj) { }
