﻿///<reference path="MicrosoftAjax.js"/>

var _pendingDestinationCode = undefined;

function showRoutesPopup()
{
    $find("ctl00_ContentPlaceHolder1_ctlAllRoutesPopup_panPopup_ModalPopupExtender").show();
}

function selectRoute(originCode, destinationCode)
{
    $find("ctl00_ContentPlaceHolder1_ctlAllRoutesPopup_panPopup_ModalPopupExtender").hide();

    var ddlOrigin = $get("ctl00_ContentPlaceHolder1_ddlstFrom");
    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_ContentPlaceHolder1_cascadingDdlstTo");
        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_ContentPlaceHolder1_ddlstTo");
        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_ContentPlaceHolder1_cascadingDdlstTo");
        ddlDestination_CascadingDropDown._onChange();

        _pendingDestinationCode = undefined;
    }
}