﻿/**
 * 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;
    var jqxhr =
        $j.ajax({
            url :'/ajax/FlightPlanStations.ashx',
            data: ({
                type: params.type,
                affiliatestation: params.affiliate,
                culture: params.rdCulture
            })
        })
        .success(function(transport) {
            stations_fill( transport, params.node, params.dontEnable );
            if ( params.callback ) {
                params.callback()
            }
        })
        .complete( function() {
            if ( params.indicator ) {
                params.indicator.hide();
            }
        })
}
function stations_fill( transport, node, dontEnable )
{
    var default_opt_label = $j('#fp_form_destination option:first').val();
    $j('#fp_form_destination').children().remove();
    $j('#fp_form_destination').html( 
         "<option selected=\"selected\">"+default_opt_label+"</option>"
    );   

    if(typeof node.get === "function") {
        node = node.get(0);
    }
    $j(transport.childNodes).find("station").each(function (idx, item){
        var newOpt = document.createElement( 'option' ),
            $item = $j(item);
        newOpt.text = $item.text();
        newOpt.value = $item.attr('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: $j(originNode).val(),
                node: destinationNode,
                rdCulture: rdCulture,
                indicator: indicator
            } );
        stations_destination_change( destinationNode, calendarweekNode );
      }  
    }
}
function stations_destination_change( destinationNode, calendarweekNode )
{
    if(typeof destinationNode.get === "function") {
        destinationNode = destinationNode.get(0);
    }
    
    if(typeof calendarweekNode.get === "function") {
        calendarweekNode = calendarweekNode.get(0);
    }
    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;
        }
    }
}
