
(function($) {
	// Retrieve JSON data from a .NET web service and unpack the data correctly before passing it on the the
	// success handler.  To call a JSON web service, the arguments must be passed as a serialized JSON object, the
	// contentType must be "application/json; charset=utf-8", and the dataType must be "json".
	// To prevent certain scripting attacks, JSON enabled .NET web services wrap the response.  The response
	// has the format "{ 'd': '<data>' }"
	$.ajaxDotNetJson = function(options) {
		function successProxy(result) {
			// Deserialize and pass the result data to the success handler
			options.success(typeof (result.d) == 'object' ? result.d : eval("(" + result.d + ")"));
		}

		return $.ajax({
			type: "POST",
			url: options.serviceUrl + "/" + options.methodName,
			data: JSON.stringify(options.methodArguments),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: successProxy,
			error: options.error
		});
	}
})(jQuery);

(function($) {
    $.ajaxDotNetWWResult = function(options) {
        var success = options.success;

        function successProxy(result) {
            if (!result.Invalid) {
                success(result.Payload);
            } else {
                options.invalid(result.ErrorMessage);
            }
        }

        var error = options.error;
        
        function errorProxy(xmlHttpRequest, errorText, exception) {
            if (xmlHttpRequest.status == 401 && typeof (options.authenticationFailure) != "undefined") {
                options.authenticationFailure();
            } else if (typeof (error) != "undefined") {
                error(xmlHttpRequest, errorText, exception);
            }
        }

        options.success = successProxy;
        options.error = errorProxy;

        return $.ajaxDotNetJson(options);
    }
})(jQuery);