﻿/// <reference path="jquery-1.3.2.js" />
/// <reference path="json2.min.js" />

var ajaxPagePath = null;
function PageMethod(methodName, dto, successMethod, errorMethod) {
    var pagePath;
    if (ajaxPagePath) {
        pagePath = ajaxPagePath;
    }
    else {
        pagePath = window.location.pathname;
    }
    var oData;
    try {
        oData = JSON.stringify(dto);
    }
    catch (e) {
        alert("error string" + e.message);
    }
    $.ajax({
        type: "POST",
        url: pagePath + "/" + methodName,
        contentType: "application/json; charset=utf-8",
        data: oData,
        dataType: "json",
        success: successMethod,
        error: function(response, settings) {
            var responseObject;
            try {
                if (response.responseText) {
                    responseObject = JSON.parse(response.responseText);
                }
            } catch (e)
            { }
            if (responseObject && responseObject.Message &&
                    responseObject.Message.indexOf("Object reference not set") >= 0) {
                location.href = location.href;
                return;
            }
            if (errorMethod) {
                errorMethod(response, settings);
            }
            else {
                if (responseObject) {
                    alert(responseObject.Message);
                }
            }
        }
    });
}
function WebServiceMethod(serviceUrl, methodName, dto, successMethod, errorMethod) {
    var oData;
    try {
        oData = JSON.stringify(dto);
    }
    catch (e) {
        alert("error string" + e.message);
    }
    $.ajax({
        type: "POST",
        url: serviceUrl + "/" + methodName,
        contentType: "application/json; charset=utf-8",
        data: oData,
        dataType: "json",
        success: successMethod,
        error: errorMethod
    });
}
