var tm;
function callbackDone(data, textStatus, jqXHR)
{
    // jqXHR.getResponseHeader("ETag")
    $("#Status").text(jqXHR.status);
    $("#StatusText").text(textStatus);
    $("#Duration").text(Math.floor(new Date - tm) + "ms");
    var txt = String(jqXHR.responseText);
    txt = txt.replace(/\012/ig, "\n\r"); // beter in IE
    $("#Result").text(txt);

    var objects_count = 1;
    if (jqXHR.responseText.indexOf("[") > 0)
    {
      var objects_name = jqXHR.responseText.substring(5, jqXHR.responseText.indexOf("[")-3);
      var objects_count = data[objects_name].length;
    }
    $("#Records").text(objects_count);
}
function callbackFail(jqXHR, textStatus, errorThrown)
{
    // FcltMgr.alert("FAIL: " + (typeof errorThrown == "string"?errorThrown:errorThrown.message));
    $("#Status").text(jqXHR.status + " " + (typeof errorThrown == "string"?errorThrown:errorThrown.message)).css("background-color", "red");
    $("#StatusText").text(textStatus);
    var txt = String(jqXHR.responseText);
    txt = txt.replace(/\012/ig, "\n\r"); // beter in IE
    $("#Result").text(txt);
}
function doSubmit()
{
    $("#Status").text("Loading ... ").css("background-color", "");
    $("#StatusText").text("");
    $("#Records").text("");
    $("#Result").text("");
    var url = $("#url").val();
    var method = $("[name=method]:checked").val();
    if (method != "GET")
        var data = $("#xml").val();

    var apikey = $("#apikey").val();
    //jQuery.support.cors = true; // in IE niet al te moeilijk doen over cross-domain.
    tm = new Date;
    $.ajax(url, { type: method,
                  data: data,
                  headers: { "X-FACILITOR-API-Key": apikey }
                  // dan werkt de header niet dataType: "jsonp"
                }
          ).done(callbackDone).fail(callbackFail);
    //http_request.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
}
function doBuild()
{
    var urlbase = $("#urlbase").val();
    var api = $("#api").val();
    var format = $("[name=format]:checked").val();
    var pretty = $("#pretty:checked").val() == "on";
    var logging= $("#log:checked").val() == "on";
    var apikey = $("#apikey").val();
    var scope = $("#scope").val();
    var facid = $("#facid").val() || "UWVA";
    var url = urlbase + "/api2/" + api + "." + format;
    url += "?fac_id="+facid;
    if (scope)
        url += "&scope="+scope;
    if (pretty)
        url += "&pretty=1";
//        url += "?pretty=1";
    if (logging)
        url += "&logging=1";
    $("#url").val(url);
    $("#urltest").html(url + (pretty?"&":"?") + "apikey=" + apikey);
}

function doInit()
{
    var path = "appl/api2";
    $.getJSON("get_apinames.asp?3", process_api_list);
}

function process_api_list(data)
{
    $("#api").empty();
    $.each( data.apinames
          , function (index, value)
            {
                apiname = data.apinames[index];
                $("#api").append("<option value="+ apiname +">" + apiname + "</option>");
            }
          );
}