﻿    function calcPassengers(c, i) {
        var MAX_NUMBER_OF_PASSENGERS = 6;
        var childNum = Math.min(MAX_NUMBER_OF_PASSENGERS - c.value, 2);
        
        var child = document.getElementById("r" + i + "Children");
        deleteSelectOptionsPass(child);

        setOptionValuesPass(child, childNum);
        showAgesLabel();
        showAges("0", 'r' + i + 'Ch1Age', 'r' + i + 'Ch2Age');
    }

    function setOptionValuesPass(objSelect, numberOfOptions) {
        for (var i = 0; i <= numberOfOptions; i++) {
            objSelect.options[i] = new Option(i, i);
        }

        objSelect.options[0].selected = true;
    }

    function deleteSelectOptionsPass(objSelect) {
        while (objSelect.options.length != 0) {
            objSelect.options[0] = null;
        }
    }
    
    function showRooms(c) {
        var divR2 = document.getElementById("Div2");
        var divR3 = document.getElementById("Div3");
        var divR4 = document.getElementById("Div4");
        var divR5 = document.getElementById("Div5");

        if (c == null || divR2 == null || divR3 == null || divR4 == null || divR5 == null) return;

        var cValue;

        if (c.value == undefined) {
            cValue = c;
        }
        else {
            cValue = c.value;
        }

        switch (cValue) {
            case "1":
                divR2.className = "rrhid";
                divR3.className = "rrhid";
                divR4.className = "rrhid";
                divR5.className = "rrhid";
                break;
            case "2":
                divR2.className = "rr";
                divR3.className = "rrhid";
                divR4.className = "rrhid";
                divR5.className = "rrhid";
                break;
            case "3":
                divR2.className = "rr";
                divR3.className = "rr";
                divR4.className = "rrhid";
                divR5.className = "rrhid";
                break;
            case "4":
                divR2.className = "rr";
                divR3.className = "rr";
                divR4.className = "rr";
                divR5.className = "rrhid";
                break;
            case "5":
                divR2.className = "rr";
                divR3.className = "rr";
                divR4.className = "rr";
                divR5.className = "rr";
                break;
        }

        hiddenAges(c);
    }

    function hiddenAges(c) {
        var ageR2c1Control = document.getElementById("r2Ch1Age");
        var ageR2c2Control = document.getElementById("r2Ch2Age");
        var r2ChildrenControl = document.getElementById("r2Children");

        var ageR3c1Control = document.getElementById("r3Ch1Age");
        var ageR3c2Control = document.getElementById("r3Ch2Age");
        var r3ChildrenControl = document.getElementById("r3Children");

        var ageR4c1Control = document.getElementById("r4Ch1Age");
        var ageR4c2Control = document.getElementById("r4Ch2Age");
        var r4ChildrenControl = document.getElementById("r4Children");

        var ageR5c1Control = document.getElementById("r5Ch1Age");
        var ageR5c2Control = document.getElementById("r5Ch2Age");
        var r5ChildrenControl = document.getElementById("r5Children");

        if (c == null || ageR2c1Control == null || ageR2c2Control == null || r2ChildrenControl == null || ageR3c1Control == null || ageR3c2Control == null || r3ChildrenControl == null || ageR4c1Control == null || ageR4c2Control == null || r4ChildrenControl == null || ageR5c1Control == null || ageR5c2Control == null || r5ChildrenControl == null) return;

        var cValue;

        if (c.value == undefined) {
            cValue = c;
        }
        else {
            cValue = c.value;
        }

        switch (cValue) {
            case "1":
                ageR2c1Control.style["display"] = "none";
                ageR2c2Control.style["display"] = "none";
                r2ChildrenControl.options[0].selected = true;

                ageR3c1Control.style["display"] = "none";
                ageR3c2Control.style["display"] = "none";
                r3ChildrenControl.options[0].selected = true;

                ageR4c1Control.style["display"] = "none";
                ageR4c2Control.style["display"] = "none";
                r4ChildrenControl.options[0].selected = true;

                ageR5c1Control.style["display"] = "none";
                ageR5c2Control.style["display"] = "none";
                r5ChildrenControl.options[0].selected = true;
                break;
            case "2":
                ageR3c1Control.style["display"] = "none";
                ageR3c2Control.style["display"] = "none";
                r3ChildrenControl.options[0].selected = true;

                ageR4c1Control.style["display"] = "none";
                ageR4c2Control.style["display"] = "none";
                r4ChildrenControl.options[0].selected = true;

                ageR5c1Control.style["display"] = "none";
                ageR5c2Control.style["display"] = "none";
                r5ChildrenControl.options[0].selected = true;
                break;
            case "3":
                ageR4c1Control.style["display"] = "none";
                ageR4c2Control.style["display"] = "none";
                r4ChildrenControl.options[0].selected = true;

                ageR5c1Control.style["display"] = "none";
                ageR5c2Control.style["display"] = "none";
                r5ChildrenControl.options[0].selected = true;
                break;
            case "4":
                ageR5c1Control.style["display"] = "none";
                ageR5c2Control.style["display"] = "none";
                r5ChildrenControl.options[0].selected = true;
                break;
            case "5":
                break;
        }
    }

    function showAges(c, agec1, agec2) {
        if (c == null || agec1 == null || agec2 == null) return;

        var cValue;
        var agec1Control = null;
        var agec2Control = null;

        agec1Control = document.getElementById(agec1);
        agec2Control = document.getElementById(agec2);

        if (agec1Control == null || agec2Control == null) return;

        if (c.value == undefined) {
            cValue = c;
        }
        else { 
            cValue = c.value;
        }

        switch (cValue) {
            case "0":
                agec1Control.style["display"] = "none";
                agec2Control.style["display"] = "none";
                break;
            case "1":
                agec1Control.style["display"] = "block";
                agec2Control.style["display"] = "none";
                break;
            case "2":
                agec1Control.style["display"] = "block";
                agec2Control.style["display"] = "block";
                break;
        }
        showAgesLabel();
    }

    function getSelectControl(selectId) {
        var selectControlsCollection = null;

        selectControlsCollection = document.getElementsByTagName("select");

        if (selectControlsCollection == null) return null;

        for (var i = 0; i < selectControlsCollection.length; i++) {
            var objSelect = selectControlsCollection[i];

            if (objSelect.id.toLowerCase() == selectId.toLowerCase()) return objSelect;
        }
    }

    function showAgesLabel() {
        var agec0Control = document.getElementById("r1AgeChildrenLabel");
        var agesc0Control = document.getElementById("r1AgesChildrenLabel");
        var r1ChildrenControl = document.getElementById("r1Children");
        var r2ChildrenControl = document.getElementById("r2Children");
        var r3ChildrenControl = document.getElementById("r3Children");
        var r4ChildrenControl = document.getElementById("r4Children");
        var r5ChildrenControl = document.getElementById("r5Children");

        if (agec0Control == null || agesc0Control == null || r1ChildrenControl == null || r2ChildrenControl == null || r3ChildrenControl == null || r4ChildrenControl == null || r5ChildrenControl == null) return;

        if ((r1ChildrenControl.selectedIndex == "2") || (r2ChildrenControl.selectedIndex == "2") || (r3ChildrenControl.selectedIndex == "2") || (r4ChildrenControl.selectedIndex == "2") || (r5ChildrenControl.selectedIndex == "2")) {
            agec0Control.style["display"] = "none";
            agesc0Control.style["display"] = "block";
        }
        else {
            if ((r1ChildrenControl.selectedIndex == "1") || (r2ChildrenControl.selectedIndex == "1") || (r3ChildrenControl.selectedIndex == "1") || (r4ChildrenControl.selectedIndex == "1") || (r5ChildrenControl.selectedIndex == "1")) {
                agec0Control.style["display"] = "block";
                agesc0Control.style["display"] = "none";
            }
            else {
                agec0Control.style["display"] = "none";
                agesc0Control.style["display"] = "none";
            }
        }
    }
