/* Shows the room info lines with titles and inputs for num adults and num children */
function showRooms(roomPrefix) {
    roomsSelected = new Number(numRoomsSelected());
    for (row = 1; row <= totalPossibleRooms; row++) {
        if (row <= roomsSelected && !isGroup()) {
            eval("document.getElementById('" + roomPrefix + row + "').style.display = ''");
            document.getElementById('adult' + roomPrefix + row).disabled = false;
            document.getElementById('child' + roomPrefix + row).disabled = false;
        } else {
            eval("document.getElementById('" + roomPrefix + row + "').style.display = 'none'");
            document.getElementById('adult' + roomPrefix + row).disabled = true;
            document.getElementById('child' + roomPrefix + row).disabled = true;
        }
    }
}

/*
    Control the display of the room information. Display the correct title on the row for the specified room.
    Display the child count dropdown. Display the column titles and the room rows in the child age section.
    When I say "display" here, I mean display or don't display depending on varioius conditions.

    @param roomId identifies the room the children belong to (e.g. 1, 2, 3)
    @param ageRowName the age row id/name that for the room (e.g. room1ages, room2ages, etc)
    @param ageItemPrefix the prefix name for each room/age combination (e.g. room1age1 - room1age is the prefix)

*/
function showRoomInfo(roomId, ageRowName, ageItemPrefix) {
    var childBoxPrefix = 'childroom';
    var childSelected = document.getElementById(childBoxPrefix + roomId).value;
    eval("document.getElementById('" + childBoxPrefix + roomId + "').style.display = ''");
    if (childSelected > 0 && numRoomsSelected() >= roomId && !isGroup()) {
        eval("document.getElementById('" + ageRowName + "').style.display = ''");
        showAges(roomId, childBoxPrefix+roomId, ageItemPrefix);
    } else {
        eval("document.getElementById('" + ageRowName + "').style.display = 'none'");
        showAges(roomId, childBoxPrefix+roomId, ageItemPrefix);
    }
}

/*
    Displays/hides the age selection for each child in each room

    @param childBoxName the name of the selection box for the number of children for a room (e.g. childRoom1, childRoom2, etc)
    @param ageItemPrefix the prefix name for each room/age combination (e.g. room1age1 - room1age is the prefix)
*/
function showAges(roomId, childBoxName, ageItemPrefix) {
    var childSelected = document.getElementById(childBoxName).value;
    childSelected = new Number(childSelected);
    for (row = 1; row <= totalPossibleChildren; row++) {
        var elem = document.getElementById(ageItemPrefix + row);
		  if (elem) {
            var ageFormElement = "room"+roomId+"child"+row;
			if (row <= childSelected) {
                elem.style.display = "";
                document.getElementById(ageFormElement).disabled = false;
            } else {
                elem.style.display = "none";
                document.getElementById(ageFormElement).disabled = true;
            }
        }
    }
}

/*
    Shows the titles above the ages (e.g. child 1, child 2, etc) depending of how many children there
    are in the row with the most children for which we need ages.

    @param childBoxPrefix the prefix name for the number of children per room (e.g. childRoom)
    @param ageItemPrefix the prefix name for each room/age combination (e.g. room1age1 - room1age is the prefix)
*/
function showAgeTitles() {
    var childBoxPrefix = 'childroom';
    var ageTitlePrefix = 'ageTitle';
    showAgeMessage();
    var maxChildren = 0;
    if (!isGroup()) {
        for (room = 1; room <= numRoomsSelected(); room++) {
            childCnt = document.getElementById(childBoxPrefix + room).value;
            if (childCnt > maxChildren) {
                maxChildren = childCnt;
            }
        }
    }
    if (maxChildren > 0) {
        document.getElementById("ageTitles").style.display = '';
    } else {
        document.getElementById("ageTitles").style.display = 'none';
    }
    for (title = 1; title <= maxChildren; title++) {
        var elem = document.getElementById(ageTitlePrefix + title);
        if (elem) {
            elem.style.display = '';
        }
    }
/* Broke Bits Here */
    for (title = maxChildren; title < totalPossibleChildren; title++) {
        var elem = document.getElementById(ageTitlePrefix + title);
        if (elem) {
            elem.style.display = 'none';
        }
    }
}

/*
    Shows the instructional message for selecting the individual ages.  The message should not be
    shown if not needed.

    @param childBoxPrefix the prefix name for the number of children per room (e.g. childRoom)
*/
function showAgeMessage() {
    var childBoxPrefix = 'childroom';
    var maxChildren = 0;
    var childCnt = 0;
    if (!isGroup()) {
        for (room = 1; room <= numRoomsSelected(); room++) {
            childCnt = eval("document.getElementById('" + childBoxPrefix + room + "').value");
            if (childCnt > 0) {
                break;
            }
        }
    }
    if (childCnt > 0) {
        document.getElementById('ageMessage').style.display = '';
    } else {
        document.getElementById('ageMessage').style.display = 'none';
    }
}

function numRoomsSelected(){
    var roomsSelected = document.getElementById("rooms").value;
    return roomsSelected;
}

function isGroup() {
    var roomsSelected = document.getElementById("rooms").value;
    return (roomsSelected > totalPossibleRooms);
}

/*    update the numbers in the dropdowns for number of adults and number of children */
function updateAdultChildDropdowns() {
    var adults;
    var children;
    
    roomsSelected = new Number(numRoomsSelected());
    
    adults = document.getElementById("adultroom1");
    if (adults) {   // handle the non NS4 form 
        for (j = 0 ; j < totalPossibleRooms ; ++j) {
            adults = document.getElementById("adultroom" + j);
            updateAdultDropdown(adults, 1, roomMaxGuests);
            
            children = document.getElementById("childroom" + j);
            updateChildDropdown(children, roomMaxChildren);

            if (j <= roomsSelected && !isGroup()) {
                adults.disabled=false;
                children.disabled=false;
            } else {
                adults.disabled=true;
                children.disabled=true;
            }
        }
    }
}

/*
    This function handles all the view updates.  Element events should call into this function (e.g. onClick, onChange, etc)
*/
function updateView(){  

    var adults = document.getElementById("adultroom1");
    if (adults) {
        // This is the non-NS4 part        
        
        showRooms('room');
        
        for (var i = 1 ; i <= totalPossibleRooms ; ++i) {
            showRoomInfo(i, 'room' + i + 'ages', 'room' + i + 'age');
        }
        showAgeTitles();
    }
}

/* Enable only some of the hidden fields on the NS4 form based on the number of rooms selected */
function enableDisableCounts() {
    var count = numRoomsSelected();
    for (var i = 0 ; i < totalPossibleRooms ; ++i) {
        document.myform["child[" + i + "]"].disabled = !(i < count);
    }    
    updateAdultChildDropdowns();
    updateHiddenAdultCounts();
    updateHiddenChildCounts();
}
