var est0 		= new Array();
var est1 		= new Array();

$.each ($('.s_estacion:eq(0) optgroup'), function (i, val) { est0[i] = $(this).html(); });
$.each ($('.s_estacion:eq(1) optgroup'), function (i, val) { est1[i] = $(this).html(); });

//
    $('.s_linea:eq(0)').attr('name','origen_linea');
    $('.s_linea:eq(0) option[value='+(typeof origen_linea == 'undefined' ? '':origen_linea)+']').attr('selected','selected');
    $('.s_estacion:eq(0) option[value='+(typeof origen == 'undefined' ? '':origen)+']').attr('selected','selected');
    $('.s_estacion:eq(0)').attr('name','origen');
    
//
$('.s_estacion:eq(0) option:eq(0)').html('Estación de Origen');
$('.s_linea:eq(0)').change(function () {
  v = $(this).val();
  $('.s_estacion:eq(0)').val(0);
  $('.s_estacion:eq(0) optgroup').remove();
  $('.s_estacion:eq(0)').append('<optgroup>'+est0[v]+'</optgroup>');
  if (v > 0) { $('.s_estacion:eq(0) option[value='+(typeof origen == 'undefined' ? '':origen)+']').attr('selected','selected'); origen = '';}
}).change();			

//
    $('.s_linea:eq(1)').attr('name','destino_linea');
    $('.s_linea:eq(1) option[value='+(typeof destino_linea == 'undefined' ? '':destino_linea)+']').attr('selected','selected');
    $('.s_estacion:eq(1) option[value='+(typeof destino == 'undefined' ? '':destino)+']').attr('selected','selected');
    $('.s_estacion:eq(1)').attr('name','destino');
//
$('.s_estacion:eq(1) option:eq(0)').html('Estación de Destino');
$('.s_linea:eq(1)').change(function () {
  v = $(this).val();
  $('.s_estacion:eq(1)').val(0);
  $('.s_estacion:eq(1) optgroup').remove();
  $('.s_estacion:eq(1)').append('<optgroup>'+est1[v]+'</optgroup>');
  if (v > 0) { $('.s_estacion:eq(1) option[value='+(typeof destino == 'undefined' ? '':destino)+']').attr('selected','selected'); destino = '';}
}).change();


$('#ruta').submit(function () {
  document.location = 'planificador/'+$('.s_estacion:eq(0)').val()+'/'+$('.s_estacion:eq(1)').val();
  return false;
})