﻿/// <reference path="Edentity.Global.js" />

Edentity.RegisterNamespace("Cosmo.WebServices");

(function(WebServices) {

    WebServices.CallAsmx = function(webserviceUrl, data, fnSuccess, fnError, ajaxObjectOverrides) {
        /// <summary>
        /// Provides defaults for making a webservice call to a local ASMX.
        /// </summary>
        /// <param name="webserviceUrl" type="String">The URL of the webservice to call.</param>
        /// <param name="data" type="String">The string representing the post data to send to the service.</param>
        /// <param name="fnSuccess" type="Function">The function to run on a successful callback.</param>
        /// <param name="fnError" type="Function">(Optional) The function to run on a failed request.</param>
        /// <param name="ajaxObjectOverrides" type="Object">(Optional) An object containing overrides for the object used to make the jQuery.ajax request.</param>
        
        var ajaxObject = $.extend({
            type: "POST",
            url: webserviceUrl,
            data: data,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: fnSuccess,
            error: fnError}, ajaxObjectOverrides);
            
        $.ajax(ajaxObject);
    }
    
    WebServices.CallJsonp = function(webserviceUrl, callbackFnName, ajaxObjectOverrides) {
        /// <summary>
        /// Provides defaults for making a webservice call to a WCF service using jsonp.
        /// </summary>
        /// <param name="webserviceUrl" type="String">The URL of the webservice to call.</param>
        /// <param name="callbackFnName" type="String">The name of the callback function for this request.</param>
        /// <param name="ajaxObjectOverrides" type="Object">(Optional) An object containing overrides for the object used to make the jQuery.ajax request.</param>
        var ajaxObject = $.extend({
            url: appendToQueryString(webserviceUrl, "method", callbackFnName),
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp"}, ajaxObjectOverrides);
            
        $.ajax(ajaxObject);
    }
    
    function appendToQueryString(url, paramName, paramValue) {
        var concatChar = (url.indexOf("?") > -1) ? "&" : "?";
        return url + concatChar + paramName + "=" + paramValue;
    }

})(Cosmo.WebServices);
