/* state = 1 - show element
   state = 0 - hide element
*/
function toggleDiv(divID, state){
    if(document.layers){ //netscape 4+

       document.layers[divID].visibility = state ? "show" : "hide";
       document.layers[divID].display = state ? "block" : "none";

    } else if(document.getElementById) { //netscape 6 internetExplorer 5+

        document.getElementById(divID).style.visibility = state ? "visible" : "hidden";
        document.getElementById(divID).style.display = state ? "block" : "none";

    } else {    //Internet Explorer 4

        document.all[divID].style.visibility = state ? "visible" : "hidden";
        document.all[divID].style.display = state ? "block" : "none";

    }
}

    function displayHotel(){
        toggleDiv('hotelTable', 1);
        toggleDiv('carTable', 0);
        toggleDiv('flightTable', 0);
        toggleDiv('cruiseTable', 0);

        document.getElementById('air').checked=false;
        document.getElementById('car').checked=false;
        document.getElementById('hotel').checked=true;
        document.getElementById('cruise').checked=false;
	}

	function displayCar(){
        toggleDiv('hotelTable', 0);
        toggleDiv('carTable', 1);
        toggleDiv('flightTable', 0);
        toggleDiv('cruiseTable', 0);

        document.getElementById('air').checked=false;
        document.getElementById('car').checked=true;
        document.getElementById('hotel').checked=false;
        document.getElementById('cruise').checked=false;
	}

	function displayCruise(){
        toggleDiv('hotelTable', 0);
        toggleDiv('carTable', 0);
        toggleDiv('flightTable', 0);
        toggleDiv('cruiseTable', 1);

        document.getElementById('air').checked=false;
        document.getElementById('car').checked=false;
        document.getElementById('hotel').checked=false;
        document.getElementById('cruise').checked=true;
	}

	function displayFlight(){
        toggleDiv('hotelTable', 0);
        toggleDiv('carTable', 0);
        toggleDiv('flightTable', 1);
        toggleDiv('cruiseTable', 0);

        document.getElementById('air').checked=true;
        document.getElementById('car').checked=false;
        document.getElementById('hotel').checked=false;
        document.getElementById('cruise').checked=false;
	}

   function loadDefaultDates() {
        var airArrival = new Date();
        var airDeparture = new Date();
        var carArrival = new Date();
        var carDeparture = new Date();
        var hotelArrival = new Date();
        var hotelDeparture = new Date();

        var hotelAdvanceArrival = 21;
        var hotelAdvanceDeparture = 23;
        var carAdvanceArrival = 7;
        var carAdvanceDeparture = 8;
        var airAdvanceArrival = 14;
        var airAdvanceDeparture = 15;

        airArrival.setDate( airArrival.getDate() + airAdvanceArrival );
        airDeparture.setDate( airDeparture.getDate() + airAdvanceDeparture );
        carArrival.setDate( carArrival.getDate() + carAdvanceArrival );
        carDeparture.setDate( carDeparture.getDate() + carAdvanceDeparture );
        hotelArrival.setDate( hotelArrival.getDate() + hotelAdvanceArrival );
        hotelDeparture.setDate( hotelDeparture.getDate() + hotelAdvanceDeparture );

        document.forms['airForm'].departureMonth.value = airArrival.getMonth();
        document.forms['airForm'].returnMonth.value = airDeparture.getMonth();
        document.forms['airForm'].departureDay.value = airArrival.getDate();
        document.forms['airForm'].returnDay.value = airDeparture.getDate();

        document.forms['carForm'].pickUpMonth.value = carArrival.getMonth();
        document.forms['carForm'].dropOffMonth.value = carDeparture.getMonth();
        document.forms['carForm'].pickUpDay.value = carArrival.getDate();
        document.forms['carForm'].dropOffDay.value = carDeparture.getDate();

        document.forms['hotForm'].arrivalMonth.value = hotelArrival.getMonth();
        document.forms['hotForm'].departureMonth.value = hotelDeparture.getMonth();
        document.forms['hotForm'].arrivalDay.value = hotelArrival.getDate();
        document.forms['hotForm'].departureDay.value = hotelDeparture.getDate();

    }

    function validateDepWindow(formName) {
        if (document.forms[formName].tempDepTime[document.forms[formName].tempDepTime.selectedIndex].value==('M')) {
            window.document.forms[formName].tripWindow.value='5';
            window.document.forms[formName].departureTime.value='7AM';
        } else if (document.forms[formName].tempDepTime[document.forms[formName].tempDepTime.selectedIndex].value==('A')) {
            window.document.forms[formName].tripWindow.value='5';
            window.document.forms[formName].departureTime.value='3PM';
        } else if (document.forms[formName].tempDepTime[document.forms[formName].tempDepTime.selectedIndex].value==('E')) {
            window.document.forms[formName].tripWindow.value='5';
            window.document.forms[formName].departureTime.value='8PM';
        } else if(document.forms[formName].tempDepTime[document.forms[formName].tempDepTime.selectedIndex].value==('ANT')) {
            window.document.forms[formName].tripWindow.value='9';
            window.document.forms[formName].departureTime.value='12PM';
        } else {
            window.document.forms[formName].tripWindow.value='5';
            window.document.forms[formName].departureTime.value=document.forms[formName].tempDepTime.value;
        }
    }

    function validateRetWindow(formName) {
        if (document.forms[formName].tempRetTime[document.forms[formName].tempRetTime.selectedIndex].value==('M')) {
            window.document.forms[formName].returnTime.value='7AM';
        } else if (document.forms[formName].tempRetTime[document.forms[formName].tempRetTime.selectedIndex].value==('A')) {
            window.document.forms[formName].returnTime.value='3PM';
        } else if (document.forms[formName].tempRetTime[document.forms[formName].tempRetTime.selectedIndex].value==('E')) {
            window.document.forms[formName].returnTime.value='8PM';
        } else if(document.forms[formName].tempRetTime[document.forms[formName].tempRetTime.selectedIndex].value==('ANT')) {
            window.document.forms[formName].returnTime.value='12PM';
        } else {
            window.document.forms[formName].returnTime.value=window.document.forms[formName].tempRetTime.value;
        }
	   }

// Second


// Third
function groups(form) {
    if (form.numberOfRooms.selectedIndex == 8) {
        window.location="http://travel.ian.com/index.jsp?pageName=groups&cid=84550";
        return false;
    }
    return submitGuestInfoForm(form);
}

// Fourth
    function setChildAge(room, child, age) {
        if (childAgesPerRoom[room] == null) {
            childAgesPerRoom[room] = new Array();
        }
        childAgesPerRoom[room][child] = age;
    }

    function setNumAdults(room, numAdults) {
        adultsPerRoom[room] = numAdults;
    }

    function setNumChildren(room, numChildren) {
        childrenPerRoom[room] = numChildren;
        refresh();
    }

    function setNumRooms(x) {
        numRooms = x;
        for (i = 0; i < x; i++) {
            if (adultsPerRoom[i] == null) {
                adultsPerRoom[i] = 2;
            }
            if (childrenPerRoom[i] == null) {
                childrenPerRoom[i] = 0;
            }
        }
        refresh();
    }

    function renderRoomSelect() {
        var x = '';
        x += '<select name="numberOfRooms" onchange="setNumRooms(this.options[this.selectedIndex].value);">';
        for (var i = 1; i < 9; i++) {
            x += '<option value="'+i+'"'+(numRooms == i ? ' selected' : '')+'>' + i;
        }
        x += '<option value="9"'+(numRooms == 9 ? ' selected' : '')+'>9+';
        x += '</select>';
        return x;
    }

    function refresh() {
        maxChildren = 0;
        for (var i = 0; i < numRooms; i++) {
            if (childrenPerRoom[i] > maxChildren) {
                maxChildren = childrenPerRoom[i];
            }
        }

        var x = '';
        if (adultHelp.length > 0) {
            x = adultHelp + "<p>\n";
        }

        if (numRooms > 8) {
            x += textRooms;
            x += renderRoomSelect();

        } else {
            x += '<table border="0" cellspacing="2" cellpadding="0">\n';
            x += '<tr><td'+cellStyle+'>'+textRooms+pad+'</td>';
            if (numRooms > 1) {
                x += '<td'+cellStyle+'>&nbsp;</td>';
            }
            x += '<td'+cellStyle+'><nobr>'+textAdults+pad+'</nobr></td><td'+cellStyle+'><nobr>'+textChildren+pad+'</nobr></td></tr>\n';
            for (var i = 0; i < numRooms; i++) {
                x += '<tr><td'+cellStyle+'>';
                if (i == 0) {
                    x += renderRoomSelect();
                } else {
                    x += '&nbsp;';
                }
                x += '</td>';
                if (numRooms > 1) {
                    x += '<td'+cellStyle+'><nobr>'+getValue(textRoomX, i+1)+pad + '</nobr></td>';
                }
                x += '<td'+cellStyle+'>';
                x += buildSelect('room-' + i + '-adult-total', 'setNumAdults(' + i + ', this.options[this.selectedIndex].value)', 1, 4, adultsPerRoom[i]);
                x += '</td><td'+cellStyle+'>';
                x += buildSelect('room-' + i + '-child-total', 'setNumChildren(' + i + ', this.options[this.selectedIndex].value)', 0, 6, childrenPerRoom[i]);
                x += '</td></tr>\n';
            }
            x += '</table>\n';

            var didHeader = false;
            for (var i = 0; i < numRooms; i++) {
                if (childrenPerRoom[i] > 0) {
                    if (!didHeader) {
                        x += '<table border="0" cellpadding="0" cellspacing="2">\n';
                        x += '<tr><td'+cellStyle+' colspan="'+(maxChildren+1)+'">';
                        x += '<img src="/images/p.gif" width="1" height="5"><br>';
                        x += childHelp;
                        x += '<img src="/images/p.gif" width="1" height="5"><br>';
                        x += '</td></tr>\n<tr><td'+cellStyle+'>&nbsp;</td>';
                        for (var j = 0; j < maxChildren; j++) {
                            x += '<td'+cellStyle+'><nobr>'+getValue(textChildX, j+1)+pad+'</nobr></td>\n';
                        }
                        didHeader = true;
                    }
                    x += '</tr>\n<tr><td'+cellStyle+'><nobr>'+getValue(textRoomX, i+1)+pad+'</nobr></td>';
                    for (var j = 0; j < childrenPerRoom[i]; j++) {
                        x += '<td'+cellStyle+'>';
                        var def = -1;
                        if (childAgesPerRoom[i] != null) {
                            if (childAgesPerRoom[i][j] != null) {
                                def = childAgesPerRoom[i][j];
                            }
                        }
                        x += '<select name="room-'+i+'-child-'+j+'-age" onchange="setChildAge('+i+', '+j+', this.options[this.selectedIndex].value);">';
                        x += '<option value="-1"'+(def == -1 ? ' selected' : '')+'>-?-';
                        x += '<option value="0"'+(def == 0 ? ' selected' : '')+'>&lt;1';
                        for (var k = 1; k <= 18; k++) {
                            x += '<option value="'+k+'"'+(def == k ? ' selected' : '')+'>'+k;
                        }
                        x += '</td>';
                    }
                    if (childrenPerRoom[i] < maxChildren) {
                        for (var j = childrenPerRoom[i]; j < maxChildren; j++) {
                            x += '<td'+cellStyle+'>&nbsp;</td>';
                        }
                    }
                    x += '</tr>\n';
                }
            }
            if (didHeader) {
                x += '</table>\n';
            }
        }

        document.getElementById("hot-search-params").innerHTML = x;
    }

    function buildSelect(name, onchange, min, max, selected) {
        var x = '<select name="' + name + '"';
        if (onchange != null) {
            x += ' onchange="' + onchange + '"';
        }
        x +='>\n';
        for (var i = min; i <= max; i++) {
            x += '<option value="' + i + '"';
            if (i == selected) {
                x += ' selected';
            }

            x += '>' + i + '\n';
        }
        x += '</select>';
        return x;
    }

    function validateGuests(form) {
        if (numRooms < 9) {
            var missingAge = false;
            for (var i = 0; i < numRooms; i++) {
                var numChildren = childrenPerRoom[i];
                if (numChildren != null && numChildren > 0) {
                    for (var j = 0; j < numChildren; j++) {
                        if (childAgesPerRoom[i] == null || childAgesPerRoom[i][j] == null || childAgesPerRoom[i][j] == -1) {
                            missingAge = true;
                        }
                    }
                }
            }

            if (missingAge) {
                alert(textChildError);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    function submitGuestInfoForm(form) {
        if (!validateGuests(form)) {
            return false;
        }
        return true;
    }

    function getValue(str, val) {
        return str.replace(/\?/g, val);
    }
    
    // six
    
                    function populateMonths() {
                var date = new Date();
                var month = date.getMonth();
                var year = date.getFullYear();
                var i;

                //clear option list
                ClearOptions(monthOptionList);

                //Add first option "All Dates" to the option list
                AddToOptionList(monthOptionList ,"ALL", "All Dates");

                //add months to the option list
                for(i=0; i<numOfMonths; i++) {
                var value = (month+1)+"/1/"+year;
                var Text = months[month]+" "+year;
                AddToOptionList(monthOptionList, value, Text);
                month++;
                if( month > 11 ) {
                month = 0;
                year++;
                }
                }

                }

                function ClearOptions(OptionList) {
                // Always clear an option list from the last entry to the first
                for (x = OptionList.length; x >= 0; x = x - 1) {
                OptionList[x] = null;
                }
                }


                function AddToOptionList(OptionList, OptionValue, OptionText) {
                // Add option to the bottom of the list
                OptionList[OptionList.length] = new Option(OptionText, OptionValue);
                }


