function selectOrig( orig)
{
  for( i = 0; i < document.fb_input.origin.length; i++) {
    if( orig == document.fb_input.origin[i].value) {
      document.fb_input.origin.selectedIndex = i;
    }
  }
  updateDestination();
  document.fb_input.destin.selectedIndex = 0;
}

function selectDest( dest)
{
  for( i = 0; i < document.fb_input.destin.length; i++) {
    if( dest == document.fb_input.destin[i].value) {
      document.fb_input.destin.selectedIndex = i;
    }
  }
}

function updateDestination()
{
  var idx  = document.fb_input.origin.selectedIndex;
  var orig = document.fb_input.origin[idx].value;

  var idx  = document.fb_input.destin.selectedIndex;
  var dest = document.fb_input.destin[idx].value;

  if( !document.fb_input) {
    return;
  }

  // old browsers can't handle the update destination functionality
  if( optgroup || !countrygroups){
    var optindent = '';
  } else {
    var optindent = '&nbsp;&nbsp;&nbsp;';
  }

  // remove all options except first header
  document.fb_input.destin.length = 1;
  // now remove all optgroups except first header
  var listitems = document.fb_input.destin.childNodes;
  if( browser == 'Opera') {
    lastchild = 0;
  } else {
    lastchild = 1;
  }
  for(i = listitems.length - 1 ; i > lastchild ; i--) {
    document.fb_input.destin.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 land = country[code];

      if( countrygroups && prevland != land) {
        if( optgroup) {
          if( prevland != '') {
            document.fb_input.destin.appendChild(og);
          }
          var og = document.createElement('optgroup');
          og.label = land;
        } else {
          var o = document.createElement('option');
          o.value = '';
          o.innerHTML = land.toUpperCase();
          document.fb_input.destin.appendChild(o);
        }
        prevland = land;
      }

      var o = document.createElement('option');
      o.value = code;
      o.innerHTML = optindent+name;
      if( code == dest) {
        o.selected = true;
      }
      if( countrygroups && optgroup) {
        og.appendChild(o);
      } else {
        document.fb_input.destin.appendChild(o);
      }

    }
    if( countrygroups && optgroup) {
      document.fb_input.destin.appendChild(og);
    }
  }

}

// hide inbound for oneway bookings
function updateInboundOutbound( value)
{
  if( value == 1) {
     document.getElementById('header1').style.display = 'none';
     document.getElementById('dates1').style.display = 'none';
     document.getElementById('daterange1').style.display = 'none';
  } else {
     document.getElementById('header1').style.display = '';
     document.getElementById('dates1').style.display = '';
     document.getElementById('daterange1').style.display = '';
  }
}

// update the calendar
function updateCalendar( seg)
{
  if( typeof document.fb_input.elements['day0'] != 'undefined'){
    var day0        = Number( document.fb_input.day0.value);
    var arr_month0  = document.fb_input.elements['month0'].value.split('-');
    if( arr_month0.length == 2){
      var month0    = parseInt(1 * arr_month0[0]);
      var year0     = arr_month0[1];
    } else {
      var month0    = Number( document.fb_input.month0.value);
      var year0     = d.getYear();
    }
    var range0      = Number( document.fb_input.range0.value);
  } else {
    var day0 = false;
    var month0 = false;
    var year0 = false;
    var range0 = false;
  }

  if( typeof document.fb_input.elements['day1'] != 'undefined'){
    var day1       = Number( document.fb_input.day1.value);
    var arr_month1  = document.fb_input.elements['month1'].value.split('-');
    if(arr_month1.length == 2){
      var month1    = parseInt(1 * arr_month1[0]);
      var year1     = arr_month1[1];
    } else {
      var month1    = Number( document.fb_input.month0.value);
      var year1     = d.getYear();
    }
    var range1      = Number( document.fb_input.range1.value);
  } else {
    var day1 = false;
    var month1 = false;
    var year1 = false;
    var range1 = false;
  }

  var d         = new Date();
  var thisday   = d.getDate();
  var thismonth = d.getMonth()+1;
  var thisyear  = d.getYear();

  if( thisyear < 999) thisyear += 1900;
  if( year0 < 999) year0       += 1900;
  if( year1 < 999) year1       += 1900;

  // in current month only today + future is allowed
  if( month0 == thismonth && day0 < thisday && year0 == thisyear) {
    day0 = thisday;
    document.fb_input.day0.selectedIndex = day0 - 1;
  }

  // in current month only today + future is allowed
  if( month1 == thismonth && day1 < thisday && year1 == thisyear) {
    day1 = thisday;
    document.fb_input.day1.selectedIndex = day1 - 1;
  }

  // get the number of days for outbound month
  stop = maxday( month0, year0);

  if( day0 > stop) {
    day0 = stop;
    document.fb_input.day0.selectedIndex = day0 - 1;
  }

  // get the number of days for inbound month
  stop = maxday( month1, year1);

  if( day1 > stop) {
    day1 = stop;
    document.fb_input.day1.selectedIndex = day1 - 1;
  }
  // check if we have to update the other calendar
  if( day0 && month0) {
    d0 = document.fb_input.day0.selectedIndex;
    if( day1){
      var d1 = document.fb_input.day1.selectedIndex;
    } else {
      var d1 = false;
    }
    m0 = document.fb_input.month0.selectedIndex;
    if( month1){
      m1 = document.fb_input.month1.selectedIndex;
    } else {
      m1 = false;
    }

    if( seg == 0) {
      if( d1 >= 0 && m1 >= 0 ){
        if( document.fb_input.month1 && m0 > m1) {
          document.fb_input.month1.selectedIndex = m0;
          month1 = month0;
        }

        if( document.fb_input.day1 && m0 == m1 && d0 > d1) {
          document.fb_input.day1.selectedIndex = d0;
          day1=day0;
        }
      }
    }
  }
}

function maxday( month, year)
{
  var time = new Date( year, month-1, 1);
  var max = 31;

  if( month == 4 || month == 6 || month == 9 || month == 11) {
    --max;
  }

  if( month == 2) {
    max = max - 3;

    if( year % 4 == 0) {
      max++;
    }

    if( year % 100 == 0) {
      max--;
    }

    if( year % 400 == 0) {
      max++;
    }
  }

  return max;
}

function updateDay( day, seg)
{
 eval( 'document.fb_input.day' + seg + '.selectedIndex=' + (day - 1));
       updateCalendar( seg);
}

function initializeObjects()
{
  objlist = document.getElementsByTagName("object");
  for ( var thisobj = 0; thisobj < objlist.length; thisobj++) {
    objlist[thisobj].outerHTML = objlist[thisobj].outerHTML;
  }
}


function initializeScreen()
{
  if( window.fb_input_init) {
    fb_input_init();
  }
}

function openpop( t, w, h, a)
{
  if( t) {
    pos = t.indexOf( '?');
    if( pos > 1) {
      t = t + '&screen_type=popup';
    } else {
      t = t + '?screen_type=popup';
    }

    t = t + '&btisess=BTI03_ATG_LV_en_B700E3284256BFF3A12F2451D909F8E3';

    if( !w || !h) {
      w = 410;
      h = 450;
    }

    if( !a) {
      a = '';
    }

    popwin = window.open( t+'#'+a, 'btiPopup', 'resizable=yes,status=no,width='+w+',height='+h+',scrollbars=1,left=0,top=0');
    popwin.focus();
  }
}

function openhelp( screen, anchor)
{
  if( !anchor) { anchor=''; }
  if( !screen) { screen='fb_input_help'; }
  openpop( 'https://flights.anothertravelguide.com/app/he.fly?hescreen='+screen, '', '', anchor);
}

function formatPrice( pric, curr, precision)
{
  if( precision===null) {
    precision = 2;
  }
  ret = '';
  ret = ret.replace( /<curr>/, curr);
  ret = ret.replace( /<price>/, pric.toFixed( precision));
  ret = ret.replace( /\./, '.');

  return ret;
}