
if(top != self){
  top.location.href = self.location;
}

var undefined;
mapimages = new Array();
sel_airport = '';
doloadmaps = 1;

function loadmaps()
{
  dimg = new Image();
  dimg.src = map_src_prefix + 'mapa_front.gif';
  for( var ap in airport) {
    img = new Image();	
    if (doloadmaps && inArray( routemap, ap)) {
      img.src = map_src_prefix + ap + '.gif';
    } else {
      img = dimg;
    }
    mapimages[ap] = img
  }
}

function findFieldValueIndex( fld, value)
{
  if( !fld)
    return -1;

  for( i = 0; i < fld.options.length; i++) {
    if( fld.options[i].value == value) {
      return i;
    }
  }

  return -1;
}

function findArrayPos( arr, value)
{
  if( arr) {
    for( i = 0; i < arr.length; i++) {
      if( arr[i] == value) {
        return i;
      }
    }
  }

  return -1;
}

function setMapBackground (imageURL) {
  id = 'map_route';

  if (document.layers)
    document[id].background.src = imageURL == 'none' ? null : imageURL;
  else if (document.all)
    document.all[id].style.backgroundImage = imageURL == 'none' ? 'none' : 'url(' + imageURL + ')';
  else if (document.getElementById)
    document.getElementById(id).style.backgroundImage = imageURL == 'none' ? 'none' : 'url(' + imageURL + ')';
}

function hide()
{
  if( sel_airport == '') {
    setMapBackground( map_src_prefix+"fg.gif");
  } else if( mapimages[sel_airport]) {
    setMapBackground( mapimages[sel_airport].src);
  }
}


function show( airport, clicked)
{
  if( clicked == undefined) clicked = 0;  

  if( clicked == 0) {
    if( sel_airport == '' && mapimages[airport]) {
      setMapBackground( mapimages[airport].src);
    }

    return false;
  }

  if( clicked == 2) {
    if( mapimages[airport]) {
      setMapBackground( mapimages[airport].src);
    }

    sel_airport = airport;
    return false;
  }
  
  var form = document.forms['busquedaVueloF'];	

  if( form) {
    idx = findArrayPos( routes[sel_airport], airport);
    if( sel_airport == '' || idx < 0) {
	  	
      fld = form.origen;
      i = findFieldValueIndex( fld, airport);
      if( i >= 0) {
        fld.selectedIndex = i;
        updateDestination();
      } else {
        alert( infoMsg);
      }
	  
    } else {
      if( sel_airport != '') {
        // que el seleccionado sea un destino valido
        fld = form.destino;
        i = findFieldValueIndex( fld, airport);		
        if( i >= 0) {
          fld.selectedIndex = i;
          // eliminar ultimas 2 lineas, si se quiere que permanezca la ultima seleccion
          sel_airport = '';
          hide();
          return false;
        }
		
      }
    }
  }

  sel_airport = airport;

  return false;
}

function showInfoBox( airport) {
  if (airport == undefined) {
    alert( infoMsg);
    return false;
  }

  // determina si el aeropuerto seleccionado es origen
  var isOrigin = 1;
  if (sel_airport != '' && document.forms['busquedaVueloF']) {
    idx = findArrayPos( routes[sel_airport], airport);
    isOrigin = (idx < 0);
  }

  if (isOrigin) {
    alert( infoMsg);
    return false;
  } else {
    return show( airport,1);
  }
  return false;
}


function updateDestination()
{

  var form = document.forms['busquedaVueloF'];	

  var idx  = form.origen.selectedIndex;
  var orig = form.origen[idx].value;

  var idx  = form.destino.selectedIndex;
  var dest = form.destino[idx].value;

  if( !form) {
    return;
  }
  
  var optindent = '';
  var listitems = form.destino.childNodes;
  if( browser == 'Opera') {
    lastchild = 0;
  } else {
    lastchild = -1;
  }
  for(i = listitems.length - 1 ; i > lastchild ; i--) {
    form.destino.removeChild(listitems[i]);
  }

  var sel = 0;
  if( orig != '' && routes[orig]) {
    var prevland = '';
    for( i = 0; orig != '' && i < routes[orig].length; i++) {
      var code = routes[orig][i];
      var name = airport[code];
      var o = document.createElement('option');
      o.value = code;
      o.innerHTML = optindent+name;
      if( code == dest) {
        o.selected = true;
      }
      form.destino.appendChild(o);

    }   
  }

  show( orig, 2);
}

function inArray(arr, key)
{    
  for (var i=0; i<arr.length; i++) {
    if (arr[i] == key) {
      return 1;
    }
  }
  return 0;
}

