﻿///<reference path="jquery-1.2.6-intellisense.js"/>

function CallWebMethod(fn, paramArray, successFn, errorFn, page) {
    ///<summary>
    ///  This function executes a callback to an ASP.NET [WebMethod]
    ///</summary>
    ///<param name="fn">
    /// The name of the [WebMethod]
    ///</param>
    ///<param name="paramArray">
    ///  Array containtng the arguments for the method call
    ///  Each parameter is specified by name and value
    ///  Example ["Name","John Doe","Age",32]
    ///</param>
    ///<param name="successFn">
    /// Callback function to handle result
    /// Example: HandleResult(result)
    ///</param>
    ///<param name="errorFn">
    /// Callback function to handle error
    /// Example: HandleError(error)
    ///</param>
    var pagePath = "http://localhost:17000/WebMethods.aspx";
    //if (pagePath.charAt(pagePath.length-1) == '/')
        //pagePath += 'default.aspx';
    
    //Create list of parameters in the form:
    //{"paramName1":"paramValue1","paramName2":"paramValue2"}
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    //Call the page method
    $.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}

