<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script>
function runTest()
{
    description("This tests accessing form elements by name. "
        + "IE only lets you look up names under the first name the element had and "
        + "does not respond to name changes. Firefox remembers every name item has been "
        + "accessed with, but forgets items that have not been accessed. "
        + "This test has been written to expect the Firefox behavior.");

    form = document.getElementById('form');
    a = document.getElementById('a');
    b = document.getElementById('b');

    shouldBe('form.length', '2');
    shouldBe('form.original', 'a');
    shouldBe('form.originalB', 'b');
    shouldBe('form.second', 'undefined');
    shouldBe('form.third', 'undefined');
    shouldBe('form.elements.original', 'a');
    shouldBe('form.elements.originalB', 'b');
    shouldBe('form.elements.second', 'undefined');
    shouldBe('form.elements.third', 'undefined');

    debug('');   
    debug("change the form item a's name to thisWillBeRemembered");
    debug('');   
    a.name="thisWillBeRemembered";
    debug("get the variable value through form element");
    shouldBe('form.thisWillBeRemembered', 'a');
    debug('');   
    debug("now change the form item a's name to thisWillBeRememberedToo");
    debug("access it in boolean context");
    a.name="thisWillBeRememberedToo";
    debug('');   
    if (form.thisWillBeRememberedToo)
        debug('accessed form.thisWillBeRememberedToo');
    debug('');   
    debug("now change the form item a's name to thisWillBeForgotten");
    debug('');   
    a.name="thisWillBeForgotten";
    debug("get the variable value through collection");
    shouldBe('form.elements.thisWillBeForgotten', 'a');
    debug('');   
    debug("now change the form item a's name to thisWillBeForgottenToo, but don't access it afterwards");
    a.name="thisWillBeForgottenToo";
        
    debug('');    
    debug("now change the form item a's name to second");
    debug('');
    a.name="second";
    
    shouldBe('form.length', '2');
    shouldBe('form.original', 'a');
    shouldBe('form.originalB', 'b');
    shouldBe('form.second', 'a');
    shouldBe('form.third', 'undefined');
    shouldBe('form.elements.original', 'undefined');
    shouldBe('form.elements.originalB', 'b');
    shouldBe('form.elements.second', 'a');
    shouldBe('form.elements.third', 'undefined');

    debug('');
    debug("now change the form item a's name to third");
    debug('');

    a.name="third";

    shouldBe('form.length', '2');
    shouldBe('form.original', 'a');
    shouldBe('form.originalB', 'b');
    shouldBe('form.second', 'a');
    shouldBe('form.third', 'a');
    shouldBe('form.elements.original', 'undefined');
    shouldBe('form.elements.originalB', 'b');
    shouldBe('form.elements.second', 'undefined');
    shouldBe('form.elements.third', 'a');

    debug('');
    debug("now change form item b's name to second");
    debug('');

    b.name="second";

    shouldBe('form.length', '2');
    shouldBe('form.original', 'a');
    shouldBe('form.originalB', 'b');
    shouldBe('form.second', 'b');
    shouldBe('form.elements.original', 'undefined');
    shouldBe('form.elements.originalB', 'undefined');
    shouldBe('form.elements.second', 'b');

    debug('');
    debug("now change a form item b's name to third");
    debug('');

    form.originalB.name="third";

    shouldBe('form.length', '2');
    shouldBe('form.original', 'a');
    shouldBe('form.originalB', 'b');
    shouldBe('form.second', 'b');
    shouldBe('form.third.length', '2');
    shouldBe('form.third[0]', 'a');
    shouldBe('form.third[1]', 'b');
    shouldBe('form.elements.original', 'undefined');
    shouldBe('form.elements.originalB', 'undefined');
    shouldBe('form.elements.second', 'undefined');
    shouldBe('form.elements.third.length', '2');
    shouldBe('form.elements.third[0]', 'a');
    shouldBe('form.elements.third[1]', 'b');
    
    debug('');
    debug("now change a form item b's name to fourth");
    debug('');

    form.originalB.name="fourth";
    
    shouldBe('form.third', 'a');
    shouldBe('form.third.length', 'undefined');
    shouldBe('form.elements.third', 'a');
    shouldBe('form.elements.third.length', 'undefined');

    debug('');
    debug("now remove element a");
    debug('');

    form.removeChild(a);

    shouldBe('form.length', '1');
    shouldBeUndefined('form.original');
    shouldBe('form.originalB', 'b');
    shouldBe('form.second', 'b');
    shouldBeUndefined('form.third');
    shouldBe('form.fourth', 'b');
    shouldBe('form.elements.original', 'undefined');
    shouldBe('form.elements.originalB', 'undefined');
    shouldBe('form.elements.second', 'undefined');
    shouldBe('form.elements.third', 'undefined');
    shouldBe('form.elements.fourth', 'b');
 
    debug('');
    debug("check that we no longer remember the past names of a");
    debug('');

    shouldBe('form.thisWillBeForgotten', 'undefined');
    shouldBe('form.thisWillBeForgottenToo', 'undefined');
    shouldBeUndefined('form.thisWillBeRemembered');
    shouldBeUndefined('form.thisWillBeRememberedToo');
    debug('');
}
</script>
</head>
<body>
<form id='form'>
<input type='hidden' id='a' name='original'>
<input type='hidden' id='b' name='originalB'>
</form>
<p id="description"></p>
<div id="console"></div>
<script>runTest();</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
