var assertions = new Array();

function getRequest()
{
    var sendRequest;
    if (window.XMLHttpRequest) {
        sendRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        try {
            sendRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) {
            try {
                sendRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                alert("Could not create httpRequest");
            }
        }
    }
    return sendRequest;
}

function runTests()
{
    for (i in assertions) {
        assertions[i]();
    }
}

function loadAssertion(path)
{
    var request = getRequest();
    request.onreadystatechange = function()
    {
        if (request.readyState == 4) {
            if (request.status == 200) {
                try {
                    eval(request.responseText);
                }
                catch(e) {
                    alert(e);
                }
            }
            else {
                alert("Unable to load " + path);
            }
        }
    };
    request.open("GET", path, true);
    request.send(null);
}

function register(statement, testcases, resultIds, func, note)
{
    assertions.push(func);
    var body = document.getElementById("tests");

    // New row
    var row = document.createElement("tr");
    body.appendChild(row);

    // Statement
    var cell = document.createElement("td");
    var text = document.createTextNode(statement);
    cell.className = "statement";
    cell.appendChild(text);
    row.appendChild(cell);

    if(note != null) {
        var space = document.createTextNode(" ");
        cell.appendChild(space);
        var ref = document.createElement("a");
        ref.href = "#" + note;
        var refText = document.createTextNode("[" + note + "]");
        ref.appendChild(refText);
        cell.appendChild(ref);
    }

    cell = document.createElement("td");
    cell.className = "scenarios";
    row.appendChild(cell);
    var table = document.createElement("table");
    cell.appendChild(table);
    var tbody = document.createElement("tbody");
    table.appendChild(tbody);
    for(var i = 0; i < testcases.length; i++) {
        var trow = document.createElement("tr");
        tbody.appendChild(trow);
        var tcell = document.createElement("td");
        trow.appendChild(tcell);
        text = document.createTextNode(testcases[i]);
        tcell.appendChild(text);
        tcell.className = "testcase";

        tcell = document.createElement("td");
        tcell.id = resultIds[i];
        tcell.className = "result";
        trow.appendChild(tcell);
    }
}

function loadAssertions()
{
    loadAssertion("tests/suite.js");
}

