/*********************************************************************
AJAX FUNCTIONS
**********************************************************************/

/*
* a generic ajax function for getting JSON data
* adds a timestamp to the options so caching doesn't occur
*
* data = {Method:"", Param:""}
* callback = function to call after success
*/

function GetJSON(data, callbackFunction)
{
    $.extend(data, { timestamp: +new Date() });
    $.getJSON('Ajax.ashx', data, callbackFunction);
}

function GetAjax(data, options, callbackFunction)
{
    //if (options.cache !== undefined && options.cache == false)
    //    $.extend(data, { timestamp: +new Date() });
 
    var defaults = {
        url: "Ajax.ashx",
        type: "GET",
        data: (data),
        cache: false,
        dataType: "html",
        global: false,
        async: true,
        success: callbackFunction
    };

    $.extend(defaults, options);

    $.ajax(defaults);
}

/*
* refresh combo values with JSON object data
* data must be a JSON object or compatible js array
* combo - combo box to be refreshed
* data format - [{id:'blah', value:'blah'}]
* data access - data.id data.value
*/
function RefreshCombo(combo, data)
{
    if (combo === null || combo === "undefined")
        return;

    combo.empty();
    var options = "";

    $.each(data, function(i, item) {
        combo.append("<option value='" + item.id + "'>" + item.value + "</option>");
        //options += "<option value='" + item.id + "'>" + item.value + "</option>";

        // another way but has some browser differences
        //combo[0]; // to get the DOM object
        //var option = new Option(item.value, item.id)
        //combo.add(option, [integer]); // ie
        //combo.add(option, [null|option]); // other
    });

    //combo.html(options);
}

/*
* sets a session var through an ajax call
*/
function SetSessionVarWithAjax(sSessionVar, sValue)
{
    $.ajax({
        url: "Ajax.ashx",
        type: "GET",
        data:
        ({
            Method: "SetSessionVarWithAjax",
            sSessionVar: sSessionVar,
            sValue: sValue
        }),
        dataType: "html",
        global: false
    });
}
