var bookPostBackElement;

function ShowRooms(ddlRoomsID) {
    var ddlRooms = $get(ddlRoomsID);
    if (ddlRooms != null) {
        var roomsCount = ddlRooms.options[ddlRooms.selectedIndex].text;

        if (roomsCount > 0) {
            var divRoomDetails1 = $get('divRoomDetails1');
            var divRoomDetails2 = $get('divRoomDetails2');
            var divRoomDetails3 = $get('divRoomDetails3');
            var divRoomDetails4 = $get('divRoomDetails4');
            var divRoomDetails5 = $get('divRoomDetails5');

            if (divRoomDetails1 != null && divRoomDetails2 != null && divRoomDetails3 != null &&
                divRoomDetails4 != null && divRoomDetails5 != null)

                if (roomsCount == 5) {
                divRoomDetails2.style.display = '';
                divRoomDetails3.style.display = '';
                divRoomDetails4.style.display = '';
                divRoomDetails5.style.display = '';
            }
            else {
                divRoomDetails5.style.display = 'none';
                if (roomsCount == 4) {
                    divRoomDetails2.style.display = '';
                    divRoomDetails3.style.display = '';
                    divRoomDetails4.style.display = '';
                }
                else {
                    divRoomDetails4.style.display = 'none';
                    if (roomsCount == 3) {
                        divRoomDetails2.style.display = '';
                        divRoomDetails3.style.display = '';
                    }
                    else {
                        divRoomDetails3.style.display = 'none';
                        if (roomsCount == 2)
                            divRoomDetails2.style.display = '';
                        else
                            divRoomDetails2.style.display = 'none';
                    }
                }
            }
        }
    }
}

function ShowChildAges(ddlChildrenID, ddlChildAge1ID, ddlChildAge2ID) {
    var ddlChildren = $get(ddlChildrenID);

    if (ddlChildren != null) {
        var ddlChildAge1 = $get(ddlChildAge1ID);
        var ddlChildAge2 = $get(ddlChildAge2ID);

        if (ddlChildAge1 != null && ddlChildAge2 != null) {
            var childrenCount = ddlChildren.options[ddlChildren.selectedIndex].text;

            switch (childrenCount) {
                case '0':
                    ddlChildAge1.style.display = 'none';
                    ddlChildAge2.style.display = 'none';
                    break;
                case '1':
                    ddlChildAge1.style.display = '';
                    ddlChildAge2.style.display = 'none';
                    break;
                case '2':
                    ddlChildAge1.style.display = '';
                    ddlChildAge2.style.display = '';
            }
        }
    }
}

function RecalculatePeopleCount(ddlAdultsID, ddlChildrenID, divChildrenID, ddlChildAge1ID, ddlChildAge2ID) {
    var ddlChildren = $get(ddlChildrenID);
    var ddlAdults = $get(ddlAdultsID);
    var divChildren = $get(divChildrenID);
    var ddlChildAge1 = $get(ddlChildAge1ID);
    var ddlChildAge2 = $get(ddlChildAge2ID);

    if (ddlAdults != null && ddlChildren != null && divChildren != null) {
        var adultsCount = ddlAdults.options[ddlAdults.selectedIndex].text;
        ddlChildren.options.length = 0;
        divChildren.style.display = '';
        if (ddlChildAge1 != null && ddlChildAge2 != null) {
            ddlChildAge1.style.display = 'none';
            ddlChildAge2.style.display = 'none';
        }
        switch (adultsCount) {
            case '1':
            case '2':
                for (i = 0; i <= 2; i++) {
                    ddlChildren.options[ddlChildren.options.length] = new Option(i, i, false, false);
                }
                ddlChildren.selectedIndex = 0;
                break;
            case '3':
                for (i = 0; i <= 1; i++) {
                    ddlChildren.options[ddlChildren.options.length] = new Option(i, i, false, false);
                }
                ddlChildren.selectedIndex = 0;
                break;
            case '4':
                divChildren.style.display = 'none';
        }
    }
}

function InitializeRequest(sender, args) {
    bookPostBackElement = args.get_postBackElement();
    if (bookPostBackElement != null) {
        var litWaitText = $get(litWaitTextID);
        if (litWaitText != null) {
            if (bookPostBackElement.id == btnSearch) {
                litWaitText.innerHTML = 'Please wait while we check availability...';
            }
            else if (bookPostBackElement.id == btnBookPackage) {
                litWaitText.innerHTML = 'Booking selected package...';
            }
            else {
                litWaitText.innerHTML = 'Processing your request. Wait please...';
            }
            $get(pnlMainID).style.display = "none";
            $get(pnlWaitID).style.display = "block";
        }
    }
}

function EndRequest(sender, args) {
    if (bookPostBackElement != null) {
        $get(pnlWaitID).style.display = "none";
        $get(pnlMainID).style.display = "block";
    }
    if (args.get_error() && args.get_error().name == 'Sys.WebForms.PageRequestManagerTimeoutException') {
        alert('There was a problem communicating with the Eurostar reservation system - please try again.');
        // remember to set errorHandled = true to keep from getting a popup from the AJAX library itself 
        args.set_errorHandled(true);
    }
}

function ShowNextImage(hotelImageID, imageInfoList, nextImageButtonID, previousImageButtonID, imageIndexHFID) {
    var hotelImage = $get(hotelImageID);
    var imageIndexHF = $get(imageIndexHFID);

    if (hotelImage != null && imageInfoList != null && imageIndexHF != null) {
        var nextImageIndex = parseInt(imageIndexHF.value) + 1;
        var nextImageButton = $get(nextImageButtonID);
        var previousImageButton = $get(previousImageButtonID);

        if (imageInfoList.length > nextImageIndex * 2) {
            // ImageInfoList array contains image urls at even positions and image captions at odd positions.
            hotelImage.src = imageInfoList[nextImageIndex * 2];
            hotelImage.title = imageInfoList[nextImageIndex * 2 + 1];
            imageIndexHF.value = nextImageIndex;

            if (previousImageButton != null) {
                previousImageButton.disabled = false;
                previousImageButton.className = "enabledLink";
            }

            if (imageInfoList.length - 2 == nextImageIndex * 2)
                if (nextImageButton != null) {
                nextImageButton.disabled = true;
                nextImageButton.className = "disabledLink";
            }
        }
        else {
            nextImageButton.disabled = true;
            nextImageButton.className = "disabledLink";
        }
    }
}

function ShowPreviousImage(hotelImageID, imageInfoList, nextImageButtonID, previousImageButtonID, imageIndexHFID) {
    var hotelImage = $get(hotelImageID);
    var imageIndexHF = $get(imageIndexHFID);

    if (hotelImage != null && imageInfoList != null && imageIndexHF != null) {
        var nextImageIndex = parseInt(imageIndexHF.value) - 1;
        var nextImageButton = $get(nextImageButtonID);
        var previousImageButton = $get(previousImageButtonID);

        if (nextImageIndex >= 0) {
            // ImageInfoList array contains image urls at even positions and image captions at odd positions.
            hotelImage.src = imageInfoList[nextImageIndex * 2];
            hotelImage.title = imageInfoList[nextImageIndex * 2 + 1];
            imageIndexHF.value = nextImageIndex;

            if (nextImageButton != null) {
                nextImageButton.disabled = false;
                nextImageButton.className = "enabledLink";
            }

            if (nextImageIndex == 0)
                if (previousImageButton != null) {
                previousImageButton.disabled = true;
                previousImageButton.className = "disabledLink";
            }
        }
        else {
            previousImageButton.disabled = true;
            previousImageButton.className = "disabledLink";
        }
    }
}

function ShowOriginalImage(hotelImageID) {
    var hotelImage = $get(hotelImageID);

    if (hotelImage != null) {
        var popupWin = window.open(hotelImage.src, 'Image', 'location,width=350,height=300,top=300,left=300,scrollbars=yes');
        popupWin.focus();
    }
}

function ChangeTrain(tpTrainsID, tpHotelsID) {
    location.hash = "TabPanel";
    var tpTrains = $get(tpTrainsID);
    var tpHotels = $get(tpHotelsID);
    if (tpTrains != null && tpHotels != null) {
        //tpTrains.click();
        tpTrains.style.display = "block";
        tpTrains.style.visibility = "visible";
        tpHotels.style.display = "none";
        tpHotels.style.visibility = "hidden";

        if (tpTrains.parentNode.parentNode.childNodes[0].className == "ajax__tab_header") {
            tpTrains.parentNode.parentNode.childNodes[0].childNodes[1].className = "ajax__tab_active";
            tpTrains.parentNode.parentNode.childNodes[0].childNodes[0].className = "";
        }
        else if (tpTrains.parentNode.parentNode.childNodes[1].className == "ajax__tab_header") {
            tpTrains.parentNode.parentNode.childNodes[1].childNodes[2].className = "ajax__tab_active";
            tpTrains.parentNode.parentNode.childNodes[1].childNodes[1].className = "";
        }
    }
}

function ChangeHotel(tpTrainsID, tpHotelsID) {
    location.hash = "TabPanel";
    var tpTrains = $get(tpTrainsID);
    var tpHotels = $get(tpHotelsID);
    if (tpTrains != null && tpHotels != null) {
        //tpHotels.click();
        tpTrains.style.display = "none";
        tpTrains.style.visibility = "hidden";
        tpHotels.style.display = "block";
        tpHotels.style.visibility = "visible";

        if (tpTrains.parentNode.parentNode.childNodes[0].className == "ajax__tab_header") {
            tpTrains.parentNode.parentNode.childNodes[0].childNodes[1].className = "";
            tpTrains.parentNode.parentNode.childNodes[0].childNodes[0].className = "ajax__tab_active";
        }
        else if (tpTrains.parentNode.parentNode.childNodes[1].className == "ajax__tab_header") {
            tpTrains.parentNode.parentNode.childNodes[1].childNodes[2].className = "";
            tpTrains.parentNode.parentNode.childNodes[1].childNodes[1].className = "ajax__tab_active";
        }
    }
}

function SyncCalendar(mainCalendarBoxID, mainCalendarBehaviorID, dependentCalendarBehaviorID) {
    var mainCalendarBox = document.getElementById(mainCalendarBoxID);
    var oldDateString = mainCalendarBox.PreviousValue;
    var currentDateString = mainCalendarBox.value;

    mainCalendarBox.PreviousValue = mainCalendarBox.value;

    var oldDate = DateFromString(oldDateString);
    var currentDate = DateFromString(currentDateString);
    var mainCalendar = $find(mainCalendarBehaviorID);

    mainCalendar.set_selectedDate(currentDate);

    var millisecondsDiff = currentDate.getTime() - oldDate.getTime();
    var dependentCalendar = $find(dependentCalendarBehaviorID);
    var selectedDateMilliseconds = dependentCalendar._selectedDate.getTime();

    dependentCalendar.set_selectedDate(new Date(selectedDateMilliseconds + millisecondsDiff));
}

function DateFromString(dateString) {
    var dayStr = dateString.substr(0, 2);
    var monthStr = dateString.substr(3, 2);
    var yearStr = dateString.substr(6, 4);
    var dt = new Date();
    dt.setFullYear(parseInt(yearStr, 10), (parseInt(monthStr, 10) - 1), parseInt(dayStr, 10));
    return dt;
}
