﻿var jsonpage = './stw/JSON.aspx';
var jsondebug = 0;

var JSONhttpobject;

function JSONGetJSONhttpobject() {
    var xmlHTTPobj;
    xmlHTTPobj = null;
    //if (JSONhttpobject && JSONhttpobject  == 'undefined')
    //{return;/*object already recreated.*/}

    if (window.XMLHttpRequest) {
        try { xmlHTTPobj = new XMLHttpRequest; }
        catch (d) { }
    }
    else if (window.ActiveXObject) {
        for (var i = 0, xmlp; xmlp = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"][i++]; ) {
            try { xmlHTTPobj = new ActiveXObject(xmlp); break }
            catch (d) { }
        }
    }
    return xmlHTTPobj;
}

/*
    Return the JSON data for the requested parameters
    Pass parameters as a query string (i.e. "TransType=WEEKLYSTATS&BegDate=2011-01-01&EndDate=2011-01-01")
*/
function JSONGetData(params) {
    //Check the JSON page
    if (jsonpage == null) {
        jsonpage = './stw/JSON.aspx';
    }
    if (jsondebug == null) {
        jsondebug = 0;
    }

    //Initialize http object
    JSONhttpobject = JSONGetJSONhttpobject();

    //Send the request
    JSONhttpobject.open("POST", jsonpage, false);
    JSONhttpobject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    JSONhttpobject.send(params);
    var strResponse = '';
    if (JSONhttpobject.readyState == 4 && JSONhttpobject.status == 200) {
        //Get the response string
        strResponse = JSONhttpobject.responseText;
    }

    if (jsondebug > 0) {
        alert('strResponse: \n' + strResponse);
    }

    //Get the JSON object
    var obj = eval('(' + strResponse + ')');

    //Done
    return obj;
}

