﻿/// <reference path="jQuery.intellisense.js" />
/// <reference path="json2.js" />

function serviceProxy(serviceUrl) {
    var _I = this;
    this.serviceUrl = serviceUrl;

    // *** Call a wrapped object
    this.invoke = function(method, data, callback, error, complete, bare) {
        // *** Convert input data into JSON - REQUIRES Json2.js        
        var json = JSON2.stringify(data);

        // *** The service endpoint URL                
        var url = _I.serviceUrl + method;

        $.ajax({
            url: url,
            data: json,
            type: "POST",
            processData: false,
            contentType: "application/json",
            timeout: 10000,
            async:false,
            dataType: "text",  // not "json" we'll parse                    
            success:
            function(res) {
                if (!callback) return;

                // *** Use json library so we can fix up MS AJAX dates                        
                var result = JSON2.parse(res);

                // *** Bare message IS result                        
                if (bare)
                { callback(result); return; }

                // *** Wrapped message contains top level object node                        
                // *** strip it off                        
                for (var property in result) {
                    callback(result[property]);
                    break;
                }
            },
            error: function(xhr, textStatus, errorThrown) {
                if (!error) return;
                if (textStatus == "timeout")
                    error({ Message: "Process is taking longer than expected and has timed out.  Please refresh the page and try what you were doing again.  If you continue to get this error, please contact tech support." });
                if (xhr.responseText) {
                    var err = JSON2.parse(xhr.responseText);
                    if (err)
                        error(err);
                    else
                        error({ Message: "Unknown server error." })
                }
                return;
            },
            complete: function(XMLHttpRequest, textStatus) {
                if (complete != null)
                    complete((textStatus == "success"));
            }
        });
    }
}