﻿///<reference path="MicrosoftAjax.js"/>

var _pendingDestinationCode = undefined;

function refreshOneWay()
{
    refreshSearchOneWay("ctl00_Body");
}

function load()
{
    
    
    // ASP.NET needs to know if JavaScript is enabled - this is done by us
    // setting the value of this hidden field. If the code-behind sees this
    // text in the hidden field, then it knows that JavaScript is enabled.
    $get("ctl00_Body_ctlSearch_hidJavaScriptDetector").value = "JavaScript";

    // A back-button issue has been seen in Firefox where ddlOrigin gets disabled
    // when the user goes back to Default.aspx from BestFares.aspx.
    // The reason for this is not known.
    // The code below sorts this out though.
    var ddlOrigin = $get("ctl00_Body_ctlSearch_ddlOrigin");
    if (ddlOrigin.disabled)
    {
        ddlOrigin.disabled = false;
        ddlOrigin.selectedIndex = 0;
        
        var ddlDestination = $get("ctl00_Body_ctlSearch_ddlDestination");
        ddlDestination.disabled = true;
    }

    refreshOneWay();
}

function selectRoute(originCode, destinationCode)
{
    $find("ctl00_Body_ctlSearch_ctlAllRoutesPopup_panPopup_ModalPopupExtender").hide();

    var ddlOrigin = $get("ctl00_Body_ctlSearch_ddlOrigin");
    if (ddlOrigin.value != originCode)
    {
        ddlOrigin.value = originCode;

        // We need to fire the cascading dropdown extender that populates the list
        // of destination stations...
        var ddlDestination_CascadingDropDown = $find("ctl00_Body_ctlSearch_ddlDestination_CascadingDropDown");
        ddlDestination_CascadingDropDown._onParentChange();

        _pendingDestinationCode = destinationCode;
    }
    else
    {
        // The origin is already correctly set, so we can just set the destination
        _pendingDestinationCode = destinationCode;
        setDestination();
    }
}
function setDestination()
{
    if (_pendingDestinationCode != undefined)
    {
        var ddlDestination = $get("ctl00_Body_ctlSearch_ddlDestination");
        ddlDestination.value = _pendingDestinationCode;

        // Note sure why this is needed - but we need to tell the CascadingDropDownExtender that we've
        // changed the selection in ddlDestination, otherwise the selected value in ddlDestination
        // gets lost in the postback
        var ddlDestination_CascadingDropDown = $find("ctl00_Body_ctlSearch_ddlDestination_CascadingDropDown");
        ddlDestination_CascadingDropDown._onChange();
        _pendingDestinationCode = undefined;
    }
}
