﻿/**
* EXECAJAX Plugin
* Description: Execute Ajax JSON call with options and callback
* Notes: \
* Author: Ferla Daniele
* Email: duff@desdinova.it
* Version: 1.1.0
*
* Usage:
* $.execAjax("index.aspx/GetString", { name: $('#txtValue1').val(), code: $('#txtValue2').val() }, { 'beforeSend': BeforeSend, 'success': Success, 'error': Error, 'complete': Complete });
*
**/

; (function($) {

    $.execAjax = function(path, options, callbacks) {
        var calls = {
            'beforeSend': null,
            'success': null,
            'error': null,
            'complete': null
        };
        if (callbacks)
            $.extend(calls, callbacks);
            
        var optionsJSON = "{";
        if (options != null) {
            for (var i in options) {
                optionsJSON += "'" + i + "':'" + options[i] + "',";
            }
            if (optionsJSON.length > 1)
                optionsJSON = optionsJSON.substr(0, optionsJSON.length - 1);
        }
        optionsJSON += "}";
        $.ajax({
            type: "POST",
            url: path,
            data: optionsJSON,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: calls.beforeSend,
            success: calls.success,
            error: calls.error,
            complete: calls.complete
        });
    }

})(jQuery);

