function handleSearchUpdatePanelUpdating() {
    var errorContainer = getControl('errorContainer');
    if (errorContainer != null) {
        errorContainer.style.display = 'none';
    }
    getControl('mainContainer').style.display = 'none';
    getControl('waitContainer').style.display = 'block';
    var totalFareBox = getControl('totalFareCannonballBox');
    if (totalFareBox) { totalFareBox.style.display = 'none'; }

    setSearchButtonsDisableState(true);
}

function handleSearchUpdatePanelUpdated(valueProxyControl, errorContainerProxy) {
    setSearchButtonsDisableState(false);

    var value = getControl(valueProxyControl).innerHTML;
    if (value != '') {
        window.location.href = value;
    } else {
        getControl('waitContainer').style.display = 'none';
        var totalFareBox = getControl('totalFareCannonballBox');
        if (totalFareBox) { totalFareBox.style.display = 'block'; }
        var errorContainer = getControl('errorContainer');
        if (errorContainer != null) {
            getControl('errorContainer').innerHTML = getControl(errorContainerProxy).innerHTML;
            getControl('errorContainer').style.display = 'block';
        }
    }
}

function setSearchButtonsDisableState(disabled) {
    var buttonIds = ['ctl00_searchPanel_searchImageButton', 'ctl00_smallFlightSearchPanel_searchDatesImageButton', 'ctl00_smallFlightSearchPanel_searchAirlinesButton'];
    for (var i = 0, buttonId; buttonId = buttonIds[i]; i++) {
        var button = $get(buttonId);
        if (button != null) {
            button.disabled = disabled;
        }
    }
}

function setLayoutForOneWay() {
    getControl('searchPanel_oneWayAndReturnContainer').style.display = '';
    getControl('searchPanel_multiCityContainer').style.display = 'none';
    showAirlineOrAllianceSearch();
    getControl('returnDateTimeRow').style.display = 'none';
    hideMultiLeg();
    setLayoutForStopover();
}

function setLayoutForReturn() {
    getControl('searchPanel_oneWayAndReturnContainer').style.display = '';
    getControl('searchPanel_multiCityContainer').style.display = 'none';
    showAirlineOrAllianceSearch();
    getControl('returnDateTimeRow').style.display = '';
    hideMultiLeg();
    setLayoutForStopover();
}

function setLayoutForMultiCity() {
    getControl('searchPanel_oneWayAndReturnContainer').style.display = 'none';
    getControl('searchPanel_multiCityContainer').style.display = '';
    getControl('airlineRow').style.display = 'none';
    getControl('allianceRow').style.display = 'none';
    getControl('airlineOrAllianceHyperlinkRow').style.display = 'none';
    showMultiLeg();
}

function setLayoutForStopover() {
    if (getControl('searchPanel_withStopoverCheckBox').checked) {
        getControl('searchPanel_departDateUserControl_timeDropDownList').style.display = 'none';
        getControl('searchPanel_returnDateUserControl_timeDropDownList').style.display = 'none';
        if (getControl('searchPanel_returnRadioButton').checked) {
            getControl('stopoverRadioButtonContainer').style.display = '';
            if (getControl('searchPanel_stopoverOnTheWayThereRadioButton').checked) {
                getControl('searchPanel_inboundStopoverPanel').style.display = 'none';
                getControl('searchPanel_outboundStopoverPanel').style.display = '';
            } else {
                getControl('searchPanel_inboundStopoverPanel').style.display = '';
                getControl('searchPanel_outboundStopoverPanel').style.display = 'none';
            }
        } else {
            getControl('stopoverRadioButtonContainer').style.display = 'none';
            getControl('searchPanel_inboundStopoverPanel').style.display = 'none';
            getControl('searchPanel_outboundStopoverPanel').style.display = '';
        }
    } else {
        getControl('stopoverRadioButtonContainer').style.display = 'none';
        getControl('searchPanel_departDateUserControl_timeDropDownList').style.display = '';
        getControl('searchPanel_returnDateUserControl_timeDropDownList').style.display = '';
        getControl('searchPanel_outboundStopoverPanel').style.display = 'none';
        getControl('searchPanel_inboundStopoverPanel').style.display = 'none';
    }
}

function setLayoutForSelectedOption() {
    if (getControl('searchPanel_returnRadioButton').checked) {
        setLayoutForReturn();
    } else if (getControl('searchPanel_oneWayRadioButton').checked) {
        setLayoutForOneWay();
    } else if (getControl('searchPanel_multiCityRadioButton').checked) {
        setLayoutForMultiCity();
    }
}

function getControl(controlId) {
    var result = $get(controlId);
    if (!result) {
        result = $get("ctl00_" + controlId);
    }

    return result;
}

function hideControl(containerId) {
    getControl(containerId).style.display = 'none';
}

function showControl(containerId) {
    getControl(containerId).style.display = 'block';
}

function showRow(containerId) {
    if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
        showControl(containerId);
    } else {
        getControl(containerId).style.display = 'table-row';
    }
}

function getElementDisplayProperty(element) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(element, "").display;
    } else if (document.defaultView && document.defaultView.getComputedStyle) {
        return (document.defaultView.getComputedStyle(element, null) ? 'block' : 'none');
    } else {
        return element.currentStyle.display;
    }
}

function showMultiLeg() {
    var homepageContentContainer = $get('homepageContentContainer');
    var multiLegContainer = $get('homepageMultiLegContainer');
    var homepageMultiLegFlashContainer = $get('homepageMultiLegFlashContainer');
    var errorContainer = $get('ctl00_errorContainer');
    var waitContainer = $get('waitContainer');
    var mainContainer = $get('mainContainer');

    var homepageContentVisibleState;
    var errorContainerVisibleState;
    var waitContainerVisibleState;

    if (homepageContentContainer) {
        homepageContentVisibleState = getElementDisplayProperty(homepageContentContainer);
    }

    if (errorContainer) {
        errorContainerVisibleState = getElementDisplayProperty(errorContainer);
    }

    if (waitContainer) {
        waitContainerVisibleState = getElementDisplayProperty(waitContainer);
    }

    if (waitContainerVisibleState != 'block') {
        if (homepageContentVisibleState == 'block') {
            homepageContentContainer.style.display = 'none';
            mainContainer.style.display = 'block';
            errorContainer.style.display = 'none'
            multiLegContainer.style.display = 'block';
            homepageMultiLegFlashContainer.style.display = 'block';
        } else if (errorContainerVisibleState != 'none') {
            mainContainer.style.display = 'block';
            errorContainer.style.display = 'none'
            homepageContentContainer.style.display = 'none';
            multiLegContainer.style.display = 'block';
            homepageMultiLegFlashContainer.style.display = 'block';
        }
    }
}

function hideMultiLeg() {
    var multiLegContainer = $get('homepageMultiLegContainer');
    var homepageMultiLegFlashContainer = $get('homepageMultiLegFlashContainer');
    var homepageContentContainer = $get('homepageContentContainer');
    var errorContainer = $get('ctl00_errorContainer');
    var mainContainer = $get('mainContainer');

    var multilegContentVisibleState;
    var errorContainerVisibleState;

    if (multiLegContainer) {
        multilegContentVisibleState = getElementDisplayProperty(multiLegContainer);
        errorContainerVisibleState = getElementDisplayProperty(errorContainer);

        if (multilegContentVisibleState == 'block') {
            multiLegContainer.style.display = 'none';
            homepageMultiLegFlashContainer.style.display = 'none';
            if (homepageContentContainer) {
                homepageContentContainer.style.display = 'block';
            }
        } else if (errorContainerVisibleState == 'block') {
            mainContainer.style.display = 'block';
            errorContainer.style.display = 'none'
            if (homepageContentContainer) {
                homepageContentContainer.style.display = 'block';
            }
        }
    }
}

function validateReturnOrOneWayDeparturePort(sender, args) {
    validateReturnOrOneWayPort(sender, args, 'Please enter a departure city.');
}

function validateReturnOrOneWayArrivalPort(sender, args) {
    validateReturnOrOneWayPort(sender, args, 'Please enter an arrival city.');
}

function validateReturnOrOneWayPort(sender, args, errMsg) {
    if (!$get(sender.multiCityRadioButton).checked) {
        args.IsValid = (args.Value != '');
        if (!args.IsValid) {
            sender.innerHTML = errMsg;
        }
    } else {
        args.IsValid = true;
    }
}

function validateMultiCityDestinationPort(sender, args) {
    validateMultiCityPort(sender, args, 'Please enter a destination city.');
}

function validateMultiCityArrivalPort(sender, args) {
    validateMultiCityPort(sender, args, 'Please enter an arrival city.');
}

function validateMultiCityPort(sender, args, errMsg) {
    if ($get(sender.multiCityRadioButton).checked) {
        if (sender.requiredField == 'True' || $get(sender.otherPort).value != '') {
            args.IsValid = (args.Value != '');
            if (!args.IsValid) {
                sender.innerHTML = errMsg;
            }
        } else {
            if (!fspValidating && !Page_IsValid && typeof (Page_ClientValidate) == 'function') {
                fspValidating = true;
                try {
                    Page_ClientValidate(sender.validationGroup);
                } finally {
                    fspValidating = false;
                }
            }
            args.IsValid = true;
        }
    }
}

var fspValidating = false;

function validateOutboundStopoverPort(sender, args) {
    if ($get(sender.withStopoverCheckBox).checked && ($get(sender.oneWayRadioButton).checked || ($get(sender.returnRadioButton).checked && $get(sender.stopoverOnTheWayThereRadioButton).checked))) {
        args.IsValid = (args.Value != '');
        if (!args.IsValid) {
            sender.innerHTML = 'Please enter a stopover city.';
        }
    } else {
        args.IsValid = true;
    }
}

function validateInboundStopoverPort(sender, args) {
    if ($get(sender.withStopoverCheckBox).checked && $get(sender.returnRadioButton).checked && $get(sender.stopoverOnTheWayBackRadioButton).checked) {
        args.IsValid = (args.Value != '');
        if (!args.IsValid) {
            sender.innerHTML = 'Please enter a stopover city.';
        }
    } else {
        args.IsValid = true;
    }
}

function showAirlineOrAllianceSearch() {
    var airlineRow = getControl('airlineRow');
    if (getControl(airlineRow.searchHiddenField).value == 'Alliance') {
        showAllianceSearch();
    } else {
        showAirlineSearch();
    }
}

function showAirlineSearch() {
    var airlineRow = getControl('airlineRow');
    showRow('airlineOrAllianceHyperlinkRow');
    hideControl(airlineRow.airlineLink);
    showControl(airlineRow.allianceLink);
    hideControl('allianceRow');
    showRow('airlineRow');
    getControl(airlineRow.searchHiddenField).value = 'Airline';
}

function showAllianceSearch() {
    var airlineRow = getControl('airlineRow');
    showRow('airlineOrAllianceHyperlinkRow');
    showControl(airlineRow.airlineLink);
    hideControl(airlineRow.allianceLink);
    showRow('allianceRow');
    hideControl('airlineRow');
    getControl(airlineRow.searchHiddenField).value = 'Alliance';
}

function setFlightId(value) {
    $get("ctl00_flightIdHiddenField").value = value;
}

function setFlightQueryCommand(value) {
    $get("ctl00_flightQueryCommandHiddenField").value = value;
}

Sys.Application.notifyScriptLoaded();
