<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Test select.add() with optgroup');
	
var mySelect;
	
function resetSelection() {
    mySelect = document.createElement('select');
    mySelect.appendChild(new Option("0", "0", false, false));
    mySelect.appendChild(new Option("1", "1", false, false));
    mySelect.appendChild(new Option("2", "2", false, false));
}

function deepCopy() {
    var copy = [];
    for(var i = 0; i < mySelect.options.length; ++i)
        copy.push(mySelect.options[i].value);
    return copy.join(",");
}

function createOption(value) {
    return new Option(value + "X", value, false, false);
}

function createGroup(value1, value2) {
    var group = document.createElement('optgroup');
    group.appendChild(new Option(value1 + "X", value1, false, false));
    group.appendChild(new Option(value2 + "Y", value2, false, false));
    return group;
}

function testAdd1(element) {
    resetSelection();
    mySelect.add(element);
    return deepCopy();
}

function testAdd2(newelement, element) {
    mySelect.add(newelement, element);
    return deepCopy();
}

function testAdd3(newelement, index) {	
    resetSelection();
    mySelect.add(newelement, index);
    return deepCopy();
}

resetSelection();
debug('list elements are = ' + deepCopy());
shouldBeEqualToString('testAdd3(createGroup("Y1", "Y2"), null)', '0,1,2,Y1,Y2');
shouldBeEqualToString('testAdd3(createGroup("Y3", "Y4"), 0)', 'Y3,Y4,0,1,2');
shouldBeEqualToString('testAdd3(createGroup("Y5", "Y6"), 2)', '0,1,Y5,Y6,2');
shouldBeEqualToString('testAdd1(createGroup("Y7", "Y8"))', '0,1,2,Y7,Y8');
shouldBeEqualToString('testAdd1(createOption("Y9"))', '0,1,2,Y9');
resetSelection();
debug('list elements are = ' + deepCopy());
shouldBeEqualToString('testAdd2(createOption("Y10"), mySelect.options[2])', '0,1,Y10,2');
shouldBeEqualToString('testAdd2(createOption("Y11"), mySelect.options[1])', '0,Y11,1,Y10,2');
resetSelection();
debug('list elements are = ' + deepCopy());
shouldBeEqualToString('testAdd2(createGroup("Y12", "Y13"), mySelect.options[1])', '0,Y12,Y13,1,2');
shouldBeEqualToString('testAdd2(createGroup("Y14", "Y15"), mySelect.options[3])', '0,Y12,Y13,Y14,Y15,1,2');
resetSelection();
debug('list elements are = ' + deepCopy());
shouldThrow('testAdd2(createOption("Y16"), createOption("Y17"))');

</script>
</body>
</html>
