﻿jQuery.actionParam = function (key, value) {
    //paramObj will store the key value pairs for the ajax call
    //if 'this' is a plain object we will use this as the paramObj
    //otherwise we will create a new empty one
    var paramObj = jQuery.isPlainObject(this) ? this : {};

    //give our paramObj the function to add more parameters, this
    //is used in this method to make things easier and allows for
    //chaining params when using the action param
    paramObj.actionParam = $.actionParam;

    //Arrays use the keys with indexes and recursively call
    //actionParam on the array values
    if (jQuery.isArray(value)) {
        for (var i = 0; i < value.length; i++) {
            paramObj.actionParam(key + '[' + i + ']', value[i]);
        }
    }

    //Dates we use the GMT string just to make sure it will
    //get converted nicely in the MVC model binder
    else if (value instanceof Date) {
        
        //paramObj[key] = value.toGMTString();
        paramObj[key] = value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate();

    }

    //object will recursively call the actionParam but appending
    //more informaiton into the key
    else if (typeof (value) == 'object') {
        for (var valueKey in value) {
            paramObj.actionParam(key + '.' + valueKey, value[valueKey]);
        }
    }

    //if we got this far its not an object, or an array so it
    //is a primitive, we can just add it to the paramObj
    else {
        paramObj[key] = value;
    }

    return paramObj;
};


