﻿//***** AJAX OBJECT *****//
var AjaxObj = function() {
    this.httpRequest = null;
    this.service = null;
    this.method = null;
    this.data = null;
    this.success = null;
    this.error = null;
    this.timeout = 20; // Default 20 second timeout.
    this.parameters = null;
}

AjaxObj.prototype.POST = function() {
    this.execute("POST");
}

AjaxObj.prototype.GET = function() {
    this.execute("GET");
}

AjaxObj.prototype.execute = function(type) {
    // Stop current request if it is executing.
    if (this.httpRequest != null) {
        try {
            this.httpRequest.abort();
        }
        catch (exception) {
        }
    }

    // Convert parameters to json.
    if (this.parameters != null)
        this.data = JSON.stringify(this.parameters);

    this.httpRequest = $j.ajax({
        type: type,
        timeout: 1000 * this.timeout,
        //url: "/webservices/" + this.service + "/" + this.method,
        url: "/layouts/serviceproxy.aspx?s=" + this.service + "&m=" + this.method,
        data: this.data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: this.onSuccess.bind(this),
        error: this.onError.bind(this)
    });
}

AjaxObj.prototype.onSuccess = function(data) {
    this.httpRequest = null;
    if (this.success != null) {
        if (data == null)
            this.success(null);
        else
            this.success(AjaxObj.expand(data)); //NOTE: If not using service proxy: this.success(AjaxObj.expand(data.d));
    }
}

AjaxObj.prototype.onError = function(data) {
    this.httpRequest = null;
    if (this.error != null) {
        var exception = eval('(' + data.responseText + ')');
        this.error(exception.Message);
    }
}

//***** STATIC AJAX FUNCTIONS *****//

// General expand function that takes the object to
// expand. A check is made on the object type and if
// an expander is present for the given type. If not, 
// the original object is returned.
AjaxObj.expand = function(obj) {
    if (obj == null) return null;

    // Recursively expand array elements.
    if (CommonHelper.isArray(obj)) {
        var arr = new Array();
        for (var i = 0; i < obj.length; i++)
            arr.push(AjaxObj.expand(obj[i]));
        return arr;
    }

    var type = obj["__type"];
    if (type == undefined) return obj;
    var expander = Expanders[type];
    if (expander == undefined) return obj;
    return expander(obj);
}

// General shrink function that takes the object to
// shrink and the type of the object. A check is made
// if a shrinker is present for the given type. If
// not, the original object is returned.
AjaxObj.shrink = function(obj, type) {
    if (obj == null) return null;

    // Recursively shrink array elements.
    if (CommonHelper.isArray(obj)) {
        var arr = new Array();
        for (var i = 0; i < obj.length; i++)
            arr.push(AjaxObj.shrink(obj[i], type));
        return arr;
    }

    var shrinker = Shrinkers[type];
    if (shrinker == undefined) return obj;
    return shrinker(obj);
}
