function selectOrig( orig)
{
  document.fb_input.rmselect.value = 1;
  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)
{
  document.fb_input.rmselect.value = 1;
  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 updateRouteMap( orig, dest)
{
  var idx  = document.fb_input.origin.selectedIndex;
  if( idx != undefined) {
    var orig = document.fb_input.origin[idx].value;
  } else {
    var orig = document.fb_input.origin.value;
  }

  var idx  = document.fb_input.destin.selectedIndex;
  if( idx != undefined) {
    var dest = document.fb_input.destin[idx].value;
  } else {
    var dest = document.fb_input.destin.value;
  }

  if( window.setFrom && orig) {
    setFrom( orig);
    if( dest) {
      setTo( dest);
    }
  }

}

function updateDestination( formName, oldOrig, oldDest)
{
  if( updateDestination.arguments.length == 0) {
    var formName = 'fb_input';

    var idx  = document.forms[formName].origin.selectedIndex;
    var orig = document.forms[formName].origin[idx].value;

    var idx  = document.forms[formName].destin.selectedIndex;
    var dest = document.forms[formName].destin[idx].value;
  } else if( updateDestination.arguments.length == 1) {
    var idx  = document.forms[formName].origin.selectedIndex;
    var orig = document.forms[formName].origin[idx].value;

    var idx  = document.forms[formName].destin.selectedIndex;
    var dest = document.forms[formName].destin[idx].value;
  } else {
    var orig = oldOrig;
    var dest = oldDest;
  }

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

  for(i = listitems.length - 1 ; i > lastchild ; i--) {
    document.forms[formName].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.forms[formName].destin.appendChild(og);
          }

          var og = document.createElement('optgroup');
          og.label = land;
        } else {
          var o = document.createElement('option');
          o.value = '';
          o.innerHTML = land.toUpperCase();
          document.forms[formName].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.forms[formName].destin.appendChild(o);
      }
    }

    if( countrygroups && optgroup) {
      document.forms[formName].destin.appendChild(og);
    }
  }
}

// hide inbound for oneway bookings
function updateInboundOutbound( value)
{
  if( value == 1) {
     getId('header1').style.visibility = 'hidden';
     getId('dates1').style.visibility  = 'hidden';
     getId('month1').style.visibility  = 'hidden';
     getId('cal1').style.visibility    = 'hidden';
  } else {
     getId('header1').style.visibility = 'visible';
     getId('dates1').style.visibility  = 'visible';
     getId('month1').style.visibility  = 'visible';
     getId('cal1').style.visibility    = 'visible';
  }
}

// update the calendar
function updateCalendar( seg, formName, noHotelUpdate)
{
  if( updateCalendar.arguments.length == 1) {
    var formName = 'fb_input';
  }

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

  if( typeof document.forms[formName].elements['day0'] != 'undefined'){
    var day0        = Number( document.forms[formName].day0.value);
    var arr_month0  = document.forms[formName].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.forms[formName].month0.value);
      var year0     = thisyear;
    }

    if( formName == 'fb_input') {
      var range0      = Number( document.forms[formName].range0.value);
    } else {
      range0 = 0;
    }
  } else {
    var day0 = false;
    var month0 = false;
    var year0 = false;
    var range0 = false;
  }

  if( typeof document.forms[formName].elements['day1'] != 'undefined'){
    var day1       = Number( document.forms[formName].day1.value);
    var arr_month1  = document.forms[formName].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.forms[formName].month0.value);
      var year1     = thisyear;
    }
  } else {
    var day1 = false;
    var month1 = false;
    var year1 = false;
  }

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

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

  // for hotels check-out date must be greater than check-in date
  if( formName == 'ot_input') {
    if( month1 == month0 && day1 <= day0 && year1 == year0) {
      d1 = document.ot_input.day0.selectedIndex + 1;
      stop = maxday( month1, year1);
      if( d1 >= stop) {
        document.ot_input.month1.selectedIndex = document.ot_input.month1.selectedIndex + 1;
        document.ot_input.day1.selectedIndex = 0;
      } else {
        document.ot_input.day1.selectedIndex = d1;
      }
    }
  }

  // check if we have to update the other calendar
  if( day0 && month0) {
    d0 = document.forms[formName].day0.selectedIndex;
    if( day1){
      var d1 = document.forms[formName].day1.selectedIndex;
    } else {
      var d1 = false;
    }
    m0 = document.forms[formName].month0.selectedIndex;
    if( month1){
      m1 = document.forms[formName].month1.selectedIndex;
    } else {
      m1 = false;
    }

    if( seg == 0) {
      if( d1 >= 0 && m1 >= 0 ){
        if( document.forms[formName].month1 && m0 > m1) {
          document.forms[formName].month1.selectedIndex = m0;
          month1 = month0;
          m1 = m0;
        }

        if( document.forms[formName].day1 && m0 == m1 && d0 > d1) {
          document.forms[formName].day1.selectedIndex = d0;
          day1=day0;
        }
      }
    }
  }

  // get the number of days for outbound month
  stop = maxday( month0, year0);
  if( day0 > stop) {
    day0 = stop;
    document.forms[formName].day0.selectedIndex = day0 - 1;
  }

  // get the number of days for inbound month
  stop = maxday( month1, year1);
  if( day1 > stop) {
    day1 = stop;
    document.forms[formName].day1.selectedIndex = day1 - 1;
  }

  if( getId( 'id_month0') && getId( 'otaform')) {
    updateHotelCalendar( 0);
  }
}

// update the hotel calendar
function updateHotelCalendar( seg, formName)
{
  if( updateHotelCalendar.arguments.length == 1) {
    var formName = 'ot_input';
  }

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

  if( typeof document.forms[formName].elements['day0'] != 'undefined'){
    var day0        = Number( document.forms[formName].day0.value);
    var arr_month0  = document.forms[formName].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.forms[formName].month0.value);
      var year0     = thisyear;
    }

    if( formName == 'fb_input') {
      var range0      = Number( document.forms[formName].range0.value);
    } else {
      range0 = 0;
    }
  } else {
    var day0 = false;
    var month0 = false;
    var year0 = false;
    var range0 = false;
  }

  if( typeof document.forms[formName].elements['day1'] != 'undefined'){
    var day1       = Number( document.forms[formName].day1.value);
    var arr_month1  = document.forms[formName].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.forms[formName].month0.value);
      var year1     = thisyear;
    }
  } else {
    var day1 = false;
    var month1 = false;
    var year1 = false;
  }

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

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

  // for hotels check-out date must be greater than check-in date
  if( formName == 'ot_input') {
    if( month1 == month0 && day1 <= day0 && year1 == year0) {
      d1 = document.ot_input.day0.selectedIndex + 1;
      stop = maxday( month1, year1);
      if( d1 >= stop) {
        document.ot_input.month1.selectedIndex = document.ot_input.month1.selectedIndex + 1;
        document.ot_input.day1.selectedIndex = 0;
      } else {
        document.ot_input.day1.selectedIndex = d1;
      }
    }
  }

  // get the number of days for outbound month
  stop = maxday( month0, year0);
  if( day0 > stop) {
    day0 = stop;
    document.forms[formName].day0.selectedIndex = day0 - 1;
  }

  // get the number of days for inbound month
  stop = maxday( month1, year1);
  if( day1 > stop) {
    day1 = stop;
    document.forms[formName].day1.selectedIndex = day1 - 1;
  }

  setNumNights();
}

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, formName, calType)
{
  if( updateDay.arguments.length == 2) {
    var formName = 'fb_input';
  }

  var dayel = 'day';

  document.forms[formName].elements[dayel + seg].selectedIndex = day - 1;
  if( calType == 'hotel') {
    updateHotelCalendar( seg);
  } else {
    updateCalendar( seg, formName);
  }
}

 function updateMonth( dir, seg, formName, calType)
 {

   if( updateMonth.arguments.length == 2) {
    var formName = 'fb_input';
   }

   var monthel = 'month';
   curmonth = document.forms[formName ].elements[monthel + seg].selectedIndex;

   newmonth = curmonth + dir;
   if( newmonth < 0)  newmonth = 11;
   if( newmonth > 11) newmonth = 0;

   document.forms[formName].elements[monthel + seg].selectedIndex = + newmonth;

   if( calType == 'hotel') {
      updateHotelCalendar( seg);
   } else {
      updateCalendar( seg, formName);
   }
 }

function checkLegs()
{
  var leg = document.getElementsByName( 'legs');
  if( leg[0].checked == false && leg[1].checked == false) {
    leg[0].checked = true;
    updateInboundOutbound( '2');
  }
}

var posX;
var posY;

if( document.addEventListener) {
  document.addEventListener( 'mouseup', mouse_pos, true);
} else {
  if( document.captureEvents) {
    document.captureEvents( Event.MOUSEUP);
    document.onmouseup = mouse_pos;
  } else {
    document.body.onmouseup = mouse_pos;
  }
}

function mouse_pos( ev)
{
  if( !ev) ev = window.event;

  posX = ev.screenX;
  posY = ev.screenY;
}

var active_element;
var bIsMSIE = false;

function initiateSelfClosing()
{
  if (navigator.appName == "Microsoft Internet Explorer") {
    active_element = CalendarWindow.document.activeElement;
    CalendarWindow.document.onfocusout = closeWnd;
    bIsMSIE = true;
  } else {
    CalendarWindow.onblur = closeWnd;
  }
}

function closeWnd()
{
  if (CalendarWindow != null) {
    if (bIsMSIE && (active_element != CalendarWindow.document.activeElement)) {
      active_element = CalendarWindow.document.activeElement;
    } else {
      CalendarWindow.close();
    }
  }
}

// Dynamic Input JS for OJT and Flight Airport search
// handler for updating airport/citylist
function updateInput( event, type, seltype, locationType, value, originvalue)
{
  if( event.keyCode == 40 ||
      event.keyCode == 38 ||
      event.keyCode == 13 ||
      event.keyCode == 9  ||
      event.keyCode == 27) {

    if( IE || WEBKIT) {
      return keypressHandler( event, type, seltype);
    }

    return false;
  }

  if( value == '' ||
      (type === 'flt' && value.length < 1) ||
      (type === 'ota' && value.length < 3) ||
      (seltype != 'origin' && seltype != 'destin')) {

    hideorigin( type);
    hidedestin( type);
    return true;
  }

  if( typeof originvalue == 'undefined') {
    originvalue = '';
  }

  g( type + '_' + seltype + '_code').value = '';

  switch( seltype) {
  case 'origin':
    originmodified = true;
    if( originruns[type]) {
      window.clearTimeout( originruns[type]);
    }

    originruns[type] = window.setTimeout( 'startSearch( \"'+ type + '\", \"origin\", \"' + locationType + '\", \"'+ value +'\")', delay);
    break;
  case 'destin':
    destinmodified = true;
    if( destinruns[type]) {
      window.clearTimeout( destinruns[type]);
    }

    destinruns[type] = window.setTimeout( 'startSearch( \"'+ type + '\", \"destin\", \"' + locationType + '\", \"'+ value +'\", \"'+ originvalue +'\")', delay);
    break;
  }

  return true;
}

// result handler origin
function originResultHandler( data)
{
  result  = data['result'];
  type    = data['type'];
  seltype = data['seltype'];
  scrid   = data['scrid'];

  originruns[type] = false;

  if( result == 'Error retrieving airports' ||
      result == '<ul></ul>') {
    getId( type + '_search_origin').style.display = 'none';

    return;
  }

  getId( type + '_search_origin').innerHTML = result;
  getId( type + '_search_origin').style.display = '';
  originshown[type] = true;

  var evenodd = false;
  listwrap = false;

  // add onlick handlers
  var container = getId( type + '_search_origin');

  for( i=0; i<container.childNodes.length; i++) {
    if( container.childNodes[i].nodeName == 'UL') {
      listwrap = container.childNodes[i];
      break;
    }
  }

  if( !listwrap) {
    return;
  }

  for( i=0; i<listwrap.childNodes.length; i++) {
    if( listwrap.childNodes[i].nodeName == 'LI') {
      listwrap.childNodes[i].setAttribute( 'nodesid', i);
      listwrap.childNodes[i].setAttribute( 'searchtype', type);
      listwrap.childNodes[i].setAttribute( 'origdestin', 'origin');
      theid = listwrap.childNodes[i].getAttribute( 'id');

      if( evenodd) {
        listwrap.childNodes[i].className = 'ac_odd';
      } else {
        listwrap.childNodes[i].className = 'ac_even'
      }

      listwrap.childNodes[i].onclick = finishSearch;
      listwrap.childNodes[i].onmouseover = highlightselected;
      listwrap.childNodes[i].onmouseout =  unhighlight;

      evenodd = !evenodd;
    }
  }

  originselected[type] = -1;
  writeCache( type, currentcachekey[type], result);
  jsonobj.removeScriptTag( scrid);
  replaceCombos( 'replace');
}

// result handler destination
function destinResultHandler( data)
{
  result  = data['result'];
  type    = data['type'];
  seltype = data['seltype'];
  scrid   = data['scrid'];

  destinruns[type] = false;

  if( result == 'Error retrieving airports' ||
      result == '<ul></ul>') {
    getId( type + '_search_destin').style.display = 'none';
    return;
  }

  getId( type + '_search_destin').innerHTML = result;
  getId( type + '_search_destin').style.display = '';
  destinshown[type] = true;

  evenodd = false;
  listwrap = false;

  // add onlick handlers
  var container = getId( type + '_search_destin');

  for( i=0; i<container.childNodes.length; i++) {
    if( container.childNodes[i].nodeName == 'UL') {
      listwrap = container.childNodes[i];
      break;
    }
  }

  if( !listwrap) {
    return;
  }

  for( i=0; i<listwrap.childNodes.length; i++) {
    if( listwrap.childNodes[i].nodeName == 'LI') {
      listwrap.childNodes[i].setAttribute( 'nodesid', i);
      listwrap.childNodes[i].setAttribute( 'searchtype', type);
      listwrap.childNodes[i].setAttribute( 'origdestin', 'destin');
      theid = listwrap.childNodes[i].getAttribute( 'id');

      if( evenodd) {
        listwrap.childNodes[i].className = 'ac_odd';
      } else {
        listwrap.childNodes[i].className = 'ac_even'
      }

      listwrap.childNodes[i].onclick = finishSearch;
      listwrap.childNodes[i].onmouseover = highlightselected;
      listwrap.childNodes[i].onmouseout =  unhighlight;

      evenodd = !evenodd;
    }
  }

  destinselected[type] = -1;
  writeCache( type, currentcachekey[type], result);
  jsonobj.removeScriptTag( scrid);
  replaceCombos( 'replace');
}

// cacheresult handler
function cacheResultHandler( type, seltype, cache)
{
  if( result == 'Error retrieving airports' ||
      result == '<ul></ul>') {
    getId( type + '_search_' + seltype).style.display = 'none';
    return;
  }

  getId( type + '_search_' + seltype).innerHTML = cache;
  getId( type + '_search_' + seltype).style.display = '';

  if( seltype == 'origin') {
    originshown[type] = true;
  } else {
    destinshown[type] = true;
  }

  var evenodd = false;
  listwrap = false;

  // add onlick handlers
  var container = getId( type + '_search_' + seltype);

  for( i=0; i<container.childNodes.length; i++) {
    if( container.childNodes[i].nodeName == 'UL') {
      listwrap = container.childNodes[i];
      break;
    }
  }

  if( !listwrap) {
    return;
  }

  for( i=0; i<listwrap.childNodes.length; i++) {
    if( listwrap.childNodes[i].nodeName == 'LI') {
      theid = listwrap.childNodes[i].getAttribute( 'id');

      listwrap.childNodes[i].setAttribute( 'nodesid', i);
      listwrap.childNodes[i].setAttribute( 'searchtype', type);
      listwrap.childNodes[i].setAttribute( 'origdestin', seltype);

      if( evenodd) {
        listwrap.childNodes[i].className = 'ac_odd';
      } else {
        listwrap.childNodes[i].className = 'ac_even'
      }

      listwrap.childNodes[i].onclick = finishSearch;
      listwrap.childNodes[i].onmouseover = highlightselected;
      listwrap.childNodes[i].onmouseout =  unhighlight;

      evenodd = !evenodd;
    }
  }

  if( seltype == 'origin') {
    originselected[type] = -1;
  } else {
    destinselected[type] = -1;
  }

  replaceCombos( 'replace');
}

// called to handle selected airport
function finishSearch()
{
  if( arguments.length > 0) {
    element = arguments[0];
  } else {
    element = this;
  }

  if( element.type == 'click') {
    element = element.currentTarget;
  }

  type = element.getAttribute( 'searchtype');
  if( !type) {
    return;
  }

  seltype = element.getAttribute( 'origdestin');

  txt = element.innerHTML;
  code = element.getAttribute( 'gw');

  getId( type + '_' + seltype + '_text').value = txt;
  getId( type + '_' + seltype + '_code').value = code;
  getId( type + '_' + seltype + '_ojt_code').value = element.getAttribute( 'id');

  getId( type + '_search_'+ seltype).style.display = 'none';

  if( seltype == 'origin') {
    originselected[type] = -1;
    originshown[type] = false;
  } else {
    destinselected[type] = -1;
    destinshown[type] = false;
  }

  replaceCombos( 'restore');
}

// handles special keys for scrolling, return and tab key
function keypressHandler( event, type, seltype)
{
  // down key
  if( event.keyCode == 40) {
    scrollDown( type, seltype);
    return false;
  // up key
  } else if( event.keyCode == 38) {
    scrollUp( type, seltype);
    return false;
  // return
  } else if( event.keyCode == 13) {
    selectAirport( type, seltype);
    return false;
  // tab
  } else if( event.keyCode == 9 &&
             ( originshown[type] || destinshown[type])) {
    selectAirport( type, seltype);
    return false;
  // esc
  } else if( event.keyCode == 27) {
    getId( type + '_' + seltype +'_text').value = '';
    hideDelayed( type, seltype);
    return false;
  } else {
    if( getId( type + '_' + seltype +'_text').value == '') {
      getId( type + '_' + seltype + '_code').value = '';
      getId( type + '_' + seltype + '_ojt_code').value = '';
    }
  }

  return true;
}

// scrolldown handler
function scrollDown( type, seltype)
{
  if( (seltype == 'origin' && !originshown[type]) ||
      (seltype == 'destin' && !destinshown[type])) {
    return true;
  }

  if( seltype == 'origin') {
    sel = originselected[type];
  } else {
    sel = destinselected[type];
  }

  var container = getId( type + '_search_' + seltype);

  for( i=0; i<container.childNodes.length; i++) {
    if( container.childNodes[i].nodeName == 'UL') {
      listwrap = container.childNodes[i];
      break;
    }
  }

  if( !listwrap) {
    return;
  }

  var evenodd = false;
  var hltd = false;

  if( sel + 1 >= listwrap.childNodes.length) {
    sel = -1;
  }

  for( i=0; i<listwrap.childNodes.length; i++) {
    if( listwrap.childNodes[i].nodeName == 'LI') {

      if( evenodd) {
        listwrap.childNodes[i].className = 'ac_odd';
      } else {
        listwrap.childNodes[i].className = 'ac_even';
      }

      if( i > sel && hltd == false) {
        listwrap.childNodes[i].className = 'ac_over';
        sel = i;
        hltd = true;
      }

      evenodd = !evenodd;
    }
  }

  if( seltype == 'origin') {
    originselected[type] = sel;
  } else {
    destinselected[type] = sel;
  }
}

// scroll up handler
function scrollUp( type, seltype)
{
  if( (seltype == 'origin' && !originshown[type]) ||
      (seltype == 'destin' && !destinshown[type])) {
    return true;
  }

  if( seltype == 'origin') {
    sel = originselected[type];
  } else {
    sel = destinselected[type];
  }

  var container = getId( type + '_search_' + seltype);

  for( i=0; i<container.childNodes.length; i++) {
    if( container.childNodes[i].nodeName == 'UL') {
      listwrap = container.childNodes[i];
      break;
    }
  }

  if( !listwrap) {
    return;
  }

  if( listwrap.childNodes.length % 2) {
    var evenodd = false;
  } else {
    var evenodd = true;
  }

  var hltd = false;

  if( sel <= 0) {
    sel = listwrap.childNodes.length;
  }

  for( i=listwrap.childNodes.length -1; i >= 0; i--) {
    if( listwrap.childNodes[i].nodeName == 'LI') {
      if( evenodd) {
        listwrap.childNodes[i].className = 'ac_odd';
      } else {
        listwrap.childNodes[i].className = 'ac_even';
      }

      if( i < sel && hltd == false) {
        listwrap.childNodes[i].className = 'ac_over';
        sel = i;
        hltd = true;
      }

      evenodd = !evenodd;
    }
  }

  if( seltype == 'origin') {
    originselected[type] = sel;
  } else {
    destinselected[type] = sel;
  }

}

// called when airport is selected by click/return/tab
function selectAirport( type, seltype)
{
  if( (seltype == 'origin' && !originshown[type]) ||
      (seltype == 'destin' && !destinshown[type])) {
    return true;
  }

  if( seltype == 'origin') {
    sel = originselected[type];
  } else {
    sel = destinselected[type];
  }

  if( sel == -1) {
    sel = 0;
  }

  var container = getId( type + '_search_' + seltype);

  for( i=0; i<container.childNodes.length; i++) {
    if( container.childNodes[i].nodeName == 'UL') {
      listwrap = container.childNodes[i];
      break;
    }
  }

  if( !listwrap) {
    return;
  }


  var evenodd = false;
  var hltd = false;

  if( sel == -1 && listwrap.childNodes.length == 1) {
    sel = 0;
  }

  for( i=0; i<listwrap.childNodes.length; i++) {
    if( listwrap.childNodes[i].nodeName == 'LI' &&
        i == sel) {

      finishSearch( listwrap.childNodes[i]);
    }
  }
}

// call hide list of airports for origin with a delay
// show hint again when input is empty
function hideDelayed( type, seltype)
{
  if( seltype == 'origin') {
    if( getId( type + '_origin_text').value == '') {
      getId( type + '_origin_text').value = originhint[type];
      originmodified = false;
    }

    if( originshown[type]) {
      if( type == 'ota') {
        window.setTimeout( 'hideorigin("ota")', 200);
      } else {
        window.setTimeout( 'hideorigin("flt")', 200);
      }
    }
  } else if( seltype == 'destin') {
    if( getId( type + '_destin_text').value == '') {
      getId( type + '_destin_text').value = destinhint[type];
      destinmodified = false;
    }

    if( destinshown[type]) {
      if( type == 'ota') {
        window.setTimeout( 'hidedestin("ota")', 200);
      } else {
        window.setTimeout( 'hidedestin("flt")', 200);
      }
    }
  }
}

// hide origin list
function hideorigin( type)
{
  if( originshown[type]) {
    getId( type + '_search_origin').style.display = 'none';
    getId( type + '_search_origin').innerHTML = '';
    originshown[type] = false;
  }

  replaceCombos( 'restore');
}

// hide destin list
function hidedestin( type)
{
  if( destinshown[type]) {
    getId( type + '_search_destin').style.display = 'none';
    getId( type + '_search_destin').innerHTML = '';
    destinshown[type] = false;
  }

  replaceCombos( 'restore');
}

// hide hint on focus
// show info in list
function hideHint( type, seltype)
{
  if( seltype == 'origin' &&
    getId( type + '_origin_text').value == originhint[type]) {
    getId( type + '_origin_text').value = ''

    getId( type + '_search_origin').innerHTML = origininfo[type];
    getId( type + '_search_origin').style.display = '';
    originshown[type] = true;
  } else if( seltype == 'destin' &&
             getId( type + '_destin_text').value == destinhint[type]) {
    getId( type + '_destin_text').value = ''

    getId( type + '_search_destin').innerHTML = destininfo[type];
    getId( type + '_search_destin').style.display = '';
    destinshown[type] = true;
  }
}

// mark entry as highlighted
function highlightselected()
{
  elem = this;
  elem.className = 'ac_over';

  type   = elem.getAttribute( 'searchtype');
  seltype = elem.getAttribute( 'origdestin');

  if( seltype == 'origin') {
    originselected[type] = elem.getAttribute( 'nodesid');
  } else {
    destinselected[type] = elem.getAttribute( 'nodesid');
  }
}

// unhighlight element
function unhighlight()
{
  elem = this;

  id = elem.getAttribute( 'nodesid');
  if( id % 2) {
    elem.className = 'ac_odd';
  } else {
    elem.className = 'ac_even';
  }
}

// write cache where cachekey is escaped version of input
function writeCache( type, cachekey, cachedata)
{
  cache[type][cachekey] = cachedata;
}

// get cache
function getCache( type, cachekey)
{
  if( typeof cache[type][cachekey] == 'undefined') {
    return false;
  } else {
    return cache[type][cachekey];
  }
}

var submit = false;
var cleardelay=3000;

function do_submit()
{
  if( submit == true) {
    return false;
  }

  submit = true;
  clear_submit_delayed();
  return true;
}

function clear_submit_delayed()
{
  if( submit == true) {
    window.setTimeout( "clear_submit()", cleardelay);
    return true;
  }

  return false;
}


function clear_submit()
{
  if( submit == true) {
    submit = false;
    return true;
  }

  return false;
}

// function checkInputModified
// Checks if something was inserted in origin/destin input fields
function checkInputModified()
{
  if( !originmodified) {
    getId( 'flt_origin_text').value = '';

  }

  if( !destinmodified) {
    getId( 'flt_destin_text').value = '';
  }
}

// calendar
CalendarWindow = null;
function calendarPopup( ref, formName, calType)
{
  if( calendarPopup.arguments.length == 1) {
    var formName = 'fb_input';
  }

  var day;
  var my;

  day = Number( document.forms[formName].elements['day' + ref].value);
  my = document.forms[formName].elements['month' + ref].value;

  var tmp   = my.split( '-');
  var month = Number( tmp[0]);
  var year  = Number( tmp[1]);

  if( formName == 'fb_input') {
    var range = Number( document.fb_input.range0.value);
  } else {
    range = 0;
  }

  if( CalendarWindow && !CalendarWindow.closed) {
    CalendarWindow.close();
  }

  var calendarX = posX + 50;
  var calendarY = posY - 100;

  CalendarWindow = window.open( '', 'calendar','location=0,menubar=0,toolbar=0,status=0,scrollbars=auto,resizable=0,width=250,height=200,left='+ calendarX +',screenX='+ calendarX +',top=' + calendarY +',screenY='+ calendarY +'');
  CalendarWindow.document.open();
  CalendarWindow.document.writeln('<html><head>');
  CalendarWindow.document.writeln('<title>MetJet</title>');
  CalendarWindow.document.writeln('<style type=\"text/css\">');
  CalendarWindow.document.writeln('a {text-decoration: underline; color: #152649;}');
  CalendarWindow.document.writeln('a:hover {text-decoration: underline; color: #FFFFFF;}');
  CalendarWindow.document.writeln('h1 {font-size: 24px; color: #FFFFFF;font-weight:normal;margin:0;padding:0;}');
  CalendarWindow.document.writeln('body, p, td, th, div, input, select, button, legend {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #FFFFFF;}');
  CalendarWindow.document.writeln('#depDate{border: 1px solid #ccc;padding:5px;background-


: #efefef;}');
  CalendarWindow.document.writeln('table.cal_table{border-collapse:collapse;border-collapse:collapse;border-collapse:collapse;border-collapse:collapse;}');
  CalendarWindow.document.writeln('.cal_month{color: #666;font-weight:bold;text-align:center;}');
  CalendarWindow.document.writeln('.cal_day_week {background-color: #4f4f4f;border: 1px solid #666;color: #fff;font-weight:bold;}');
  CalendarWindow.document.writeln('.cal_day_past {background-color:#ddd;border: 1px solid #666;color: #666;}');
  CalendarWindow.document.writeln('.cal_day_past_sel {background-color:#e0e99d;border: 1px solid #666;color: #fff;font-weight: bold;}');
  CalendarWindow.document.writeln('.cal_day_sel {background-color:#cdda32;border: 1px solid #666;font-weight: bold;color: #333;}');
  CalendarWindow.document.writeln('.cal_day_fut {background-color: #fff;border: 1px solid #666;color: #fff;}');
  CalendarWindow.document.writeln('.cal_day_fut_sel {background-color:#cdda32;border: 1px solid #666;color: #fff;}');
  CalendarWindow.document.writeln('</style>');
  CalendarWindow.document.writeln('</head>'+'<'+'b'+'o'+'d'+'y onload=\"window.opener.initiateSelfClosing()\">');
  CalendarWindow.document.writeln('<form><div id=\"kalender\">&nbsp;</div></form>');
  CalendarWindow.document.writeln('</'+'b'+'o'+'d'+'y></html>');
  CalendarWindow.document.close();
  CalendarWindow.document.getElementById( 'kalender').innerHTML = calendar( day, month, year, range, ref, formName, calType);
  CalendarWindow.focus();
}

// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page
// to access data from any server in the web, which is really useful.
// Unfortunately, the data is returned in the form of a script. That script
// can deliver the data, but it runs with the same authority as scripts on
// the base page, so it is able to steal cookies or misuse the authorization
// of the user with the server. A rogue script can do destructive things to
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");

    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE + '&scrid=' + this.scriptId);
    this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function ( scrid) {
    // Destroy the script tag
    if( this.headLoc && g( scrid) != '') {
      this.headLoc.removeChild( g( scrid));
    }
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}



