﻿/**
 * params can contain:
 *   - type (origin, destination)
 *   - affiliate (three letter code)
 *   - node (dropdown node to update)
 *   - rdCulture (culture code)
 *   - indicator (indicator div)
 *   - dontEnable (boolean)
 *   - callback (callback function which will be called after update)
 */
function stations_update( params )
{
    params.node.disabled = true;
    new Ajax.Request( '/ajax/FlightPlanStations.ashx',
      {
        parameters: { type: params.type, affiliatestation: params.affiliate, culture: params.rdCulture },
        onLoading: function() { if ( params.indicator ) { params.indicator.show(); } },
        onComplete: function() { if ( params.indicator ) { params.indicator.hide(); } },
        onSuccess: function( transport ) { stations_fill( transport, params.node, params.dontEnable ); if ( params.callback ) { params.callback(); } }
        //onException: function( request, exception ) { alert( exception ); }
      }
    );
}

function stations_fill( transport, node, dontEnable )
{
    if ( transport == null || transport.responseXML == null || transport.responseXML.documentElement == null )
    {
      return;
    }
    var xmlDoc = transport.responseXML.documentElement;
    if ( xmlDoc.firstChild == null  )
    {
      return;
    }
    for( var i = node.length - 1; i >= 1; i-- ) // don't remove first element
    {
      node.remove( i );
    }
           
    var stations = xmlDoc.getElementsByTagName( 'station' );
    // add XML data to dropdown
    for( var j = 0; j < stations.length; j++ )
    {
      var newOpt = document.createElement( 'option' );
      
      if ( stations[ j ].childNodes.length > 0 )
      {
        if ( stations[ j ].textContent )
        {
          newOpt.text = stations[ j ].textContent;
        }
        else
        {
          newOpt.text = stations[ j ].childNodes[ 0 ].nodeValue;
        }
       
        newOpt.value = stations[ j ].getAttribute( 'tlc' );

        try
        {
          node.add( newOpt, null );
        }
        catch( ex )
        {
          node.add( newOpt, node.length ); // this is for IE
        }
      }
    }
    
    if ( dontEnable == true ) { return; }
    node.disabled = false;
}

function stations_origin_change( originNode, destinationNode, calendarweekNode, rdCulture, indicator )
{
    if ( originNode.selectedIndex == 0 )
    {
      if ( destinationNode != null )
      {
        destinationNode.disabled = true;
        stations_destination_change( destinationNode, calendarweekNode );
      }
    }
    else
    {
      if ( destinationNode != null )
      {
        stations_update( { type: 'destination', affiliate: originNode.value, node: destinationNode, rdCulture: rdCulture, indicator: indicator } );
        stations_destination_change( destinationNode, calendarweekNode );
      }  
    }
}

function stations_destination_change( destinationNode, calendarweekNode )
{
    if ( calendarweekNode == null )
    {
      return;
    }
    if ( destinationNode.selectedIndex == 0 || destinationNode.disabled )
    {
      calendarweekNode.disabled = true;
    }
    else
    {
      calendarweekNode.disabled = false;
    }
}

function stations_preselect( dropdown, tlc )
{
    for ( var i = 0; i < dropdown.length; i++ )
    {
        if ( dropdown[ i ].value == tlc )
        {
            dropdown[ i ].selected = true;
            break;
        }
    }
}