<html>
<head>
<title>Implicit Form Submission</title>
<script>

var currentTest = 0;

// match IE and FF unless specified otherwise.
var allTests = [
    [ "Single text input", "!text", "y" ],
    [ "Single text input with submit disabled", "!text,-submit", "n" ],
    [ "Multiple text inputs", "!text,text,text", "n" ],
    [ "Multiple text inputs with submit", "!text,text,text,submit", "y" ],
    [ "Multiple text inputs with submit disabled", "!text,text,text,-submit", "n" ],
    [ "Multiple text inputs and multiple submits, first submit disabled", "!text,text,text,-submit,submit", "n" ],
    [ "Text input and text area, text input focused", "!text,textarea", "y" ],
    [ "Text input and text area and a submit, text input focused", "!text,textarea,submit", "y" ],
    [ "Text input and text area and a disabled submit, text input focused", "!text,textarea,-submit", "n" ],
    [ "Text input and checkbox, text input focused", "!text,checkbox", "y" ],
    [ "Text input and radio, text input focused", "!text,radio", "y" ],
    [ "Text input and text area, textarea focused", "text,!textarea", "n" ],
    [ "Text input and checkbox, checkbox focused", "text,!checkbox", "n" ], // match IE, not FF.
    [ "Text input and radio, radio focused", "text,!radio", "n" ], // match IE, not FF.
    [ "Single radio", "!radio", "n" ], // match IE, not FF.
    [ "Single checkbox", "!checkbox", "n" ],
    [ "Single checkbox with a submit", "!checkbox,submit", "y" ],
    [ "Single checkbox with a submit disabled", "!checkbox,-submit", "n" ],
    [ "Single select", "!select", "n" ],
    [ "Select with a submit", "!select,submit", "y" ], // match neither FF nor IE, instead follow logic.
    [ "Select with a disabled submit", "!select,-submit", "n" ],
    [ "Multi-line select with a submit", "!selectBox,submit", "y" ], // match neither FF nor IE, instead follow logic.
    [ "Multi-line select with a disabled submit", "!selectBox,-submit", "n" ],
    [ "Text field and single select, text focused", "!text,select", "y" ],
    [ "Text field and single select, select focused", "text,!select", "n" ],
    [ "Multiple text inputs with a button", "!text,text,button", "y"],
    [ "Multiple text inputs with a disabled button", "!text,text,-button", "n"]
];

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var results = {
    submissionReported: false,
    data: [],
    submitted: function()
    {
        this.submissionReported = true;
        if (!window.eventSender)
            this.testCompleted();
    },
    testCompleted: function()
    {
        this.data.push(this.submissionReported ? 'y' : 'n');
        this.submissionReported = false;
        runNextTest();
    },
    publish: function()
    {
        document.getElementById("log").innerHTML = allTests.map(function(manifest, i)
        {
            return manifest[0] + " should " + (manifest[2] == 'n' ? "not" : "") + " submit: " + (this.data[i] == manifest[2] ? "PASS" : "FAIL");
        }, this).join("<br>");
    }
};

function runNextTest()
{
    ++currentTest;
    if (currentTest < allTests.length) {
        runTest();
        return;
    }
    document.getElementById('arena').textContent = '';
    results.publish();
    if (window.testRunner)
        testRunner.notifyDone();
}

function buildAndTestForm(arena, inputs, description)
{
    arena.textContent = '';
    var form = arena.appendChild(document.createElement("form"));
    form.addEventListener('submit', function(evt) {
        results.submitted();
        evt.preventDefault();
    })
    if (!window.eventSender)
        form.appendChild(document.createElement("p")).innerHTML = "Press Enter key";
    inputs.forEach(function(type, i)
    {
        var focused;
        if (type[0] == '!') {
            type = type.substr(1);
            focused = true;
        }
        var disabled;
        if (type[0] == '-') {
            type = type.substr(1);
            disabled = true;
        }
        var control;
        if (type == "textarea") {
            control = document.createElement(type);
        } else if (type == "select") {
            control = document.createElement(type);
            control.options.add(new Option("a"));
        } else if (type == "selectBox") {
            control = document.createElement("select");
            control.size = 5;
            control.options.add(new Option("a"));
        } else if (type == "button") {
            control = document.createElement(type);
            control.type = "submit";
        } else {
            control = document.createElement("input");
            control.type = type;
        }
        control.id = focused ? "focused" : ("input" + i);
        control.disabled = !!disabled;
        form.appendChild(control);
    });
    var input = document.getElementById("focused");
    if (input)
        input.focus();
    if (window.eventSender) {
        eventSender.keyDown("\r", []);
        results.testCompleted();
    } else {
        var a = document.createElement("a");
        a.href = "javascript:results.testCompleted();";
        a.innerText = "Click if didn't submit";
        arena.appendChild(a);
    }
}

function runTest()
{
    var manifest = allTests[currentTest];
    buildAndTestForm(document.getElementById('arena'), manifest[1].split(','), manifest[0]);
}

</script>
</head>
<body onload="runTest()">
    <p>Tests various combinations of form elements and how implicit submission works with them.
    <div id="arena"></div>
    <div id="log"></div>
</body>
</html>
