﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

function AjaxError(XMLHttpRequest, textStatus, errorThrown) {
    try {
        // get the .NET Exception from the JSON text
        var err = eval("(" + XMLHttpRequest.responseText + ")");
        // available properties: ExceptionType, Message, and StackTrace
        alert(err.Message + '\n\n_____________________\n\n' + err.StackTrace);
    }
    catch (e) {
        // responseText is most likely not a valid JSON object - that's why eval isn't working
        if (XMLHttpRequest.status == 404) {
            // friendly alert
            alert(XMLHttpRequest.statusText);
        }
        else if (XMLHttpRequest.status == 500) {
            // output the html to the browser - less friendly - but required
            document.clear();
            document.write(XMLHttpRequest.responseText);
        }
        else {
            alert('Unhandled status code ' + XMLHttpRequest.status + ': ' + XMLHttpRequest.statusText + '\n\n_____________________\n\n' + e);
        }

    }
}

function initAjaxSetup() {
    $.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        error: AjaxError,
        data: "{}",
        dataFilter: function(data) {
            var msg;
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
                msg = JSON.parse(data);
            else
                msg = eval('(' + data + ')');

            if (msg.hasOwnProperty('d'))
                return msg.d;
            else
                return msg;
        }
    });
}

$