<html>
<script src="resetCookies.js"></script>
<script>
resetCookies();

if (window.testRunner)
    testRunner.dumpAsText();

window.onmessage = function(evt)
{
    if (evt.data == "showCookies") {
        showCookies();
        return;
    } else if (evt.data.split(" ")[0] == "sendXHR") {
        sendXHR(evt.data.split(" ")[1]);
        return;
    } else if (evt.data == "resetCookiesAndNotifyDone") {
        resetCookiesAndNotifyDone();
        return;
    } else
        console.log("Unknown message.");
}

function sortCookie(cookie)
{
    var items = cookie.split("; ");
    items.sort();
    return items.join("; ");
}

var stage = 1;
function showCookies()
{
    console.log("Test stage " + stage++ + " document.cookie is: " + sortCookie(document.cookie));
    parent.window.postMessage("done", "*");
}

function sendXHR(queryCommand)
{
    var baseurl = "http://localhost:8000/cookies/resources/cookie-utility.py";
    var url = queryCommand ? baseurl + "?queryfunction=" + queryCommand : baseurl;
    console.log(url);
    var req = new XMLHttpRequest();
    req.open('GET', url, false);
    req.send();

    if (req.status == 200)
        console.log("XHR response - " + req.responseText);
    else
        console.log("xhr error");
    
    parent.window.postMessage("done", "*");    
}

function resetCookiesAndNotifyDone()
{
    resetCookies();
    if (window.testRunner)
        testRunner.notifyDone();
}

</script>
<body>
HELLO THERE
</body>
</html>
