﻿// Make a request to a given page
// We put this here so anything can import this functionality.
// USAGE:
// [CallService]
//     <Page and method to be called> I.e. "Default.aspx/HelloWorld"
//     <json serialized text, use JSON.stringify in json2.js> I.e.     var list = [field1, field2]; var jsonText = JSON.stringify({ list: list });
//     <Pass a fucntion name to be called if successful.>
//     <Pass a fucntion name to be called if an error occurs.>
function CallService(pageMethod, jsonText, SuccessFunction, ErrorFunction) {
    AjaxCall(pageMethod, jsonText, true, SuccessFunction, ErrorFunction); // Async by default
}

function AjaxCall(pageMethod, jsonText, asynchronous, SuccessFunction, ErrorFunction) {

    // If empty, set json text to empty.
    if (jsonText == null || jsonText == "")
        jsonText = "{}";

    $.ajax({
        type: "POST",
        url: pageMethod,
        async: asynchronous,
        data: jsonText,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: SuccessFunction,
        error: ErrorFunction
    });
}
