function gj_do_lookup(control, targetspan) {
  var action = '' + control.value + '&sp=' + targetspan;

  // Hide the popup list
  sbm_invi(targetspan + 'l', true);
  // Hide the previous selected airport details
  sbm_invi(targetspan + 's', true);

// August 2007 JW - we also hide the need valid airports message
// And set our status to invalid for the duration of the request processing
  sbm_invi('gj_calcerror', true);
  var y = new sbm_getObj(targetspan + '_valid'); if (y.obj) y.obj.value = 0;
// End Aug 2007

  // We only do a lookup if we have an entry
  if (control.value != '') {
    // Set the display panel to say we are looking up
    sbm_invi(targetspan + 'm', false);
  
    sbm_sndReq('gj_airport_lookup.php', action);
  } else {
    // We have selected no airport so clear the message
    sbm_invi(targetspan + 'm', true);
    // Mark the entry invalid
    var x = new sbm_getObj(targetspan+'_valid'); if (x.obj) x.obj.value = 0;
  }

  return true;
}

// Nasty global needed 
var __gj_passthrough_var;
function gj_calculate_air_travel(control, trynum) {
  // First up we need to work out the value of the radio button. Default to return
  var radioValue = 2
  for (i=control.form.gj_ow.length-1; i>-1; i--) {
    if (control.form.gj_ow[i].checked) {
      radioValue = control.form.gj_ow[i].value;
      break;
    }
  }

  var action='1&gj_fa=' + control.form.gj_fa.value + '&gj_va=' + control.form.gj_va.value + '&gj_ta=' + control.form.gj_ta.value + '&gj_np=' + control.form.gj_np.value + '&gj_ow=' + radioValue + '&io=' + control.form.indiv_organ.value;

  // First verify that we have valid to and from fields
  if (control.form.gj_fa_valid.value == 0 ||
      control.form.gj_ta_valid.value == 0) {
// August 2007 JW - It might be that we don't have the results back from the previous call yet so wait 500ms
	if (trynum < 2) {
	  // We have to use a very ugly global to pass our control through the
	  // settimeout function. Well designed, java people :-p
	  __gj_passthrough_var = control;
	  var functionCall = 'gj_calculate_air_travel(__gj_passthrough_var, '+(trynum+1)+')';
	  setTimeout(functionCall, 1500);
	  return false;
	}
// End Aug 2007 fix.
    sbm_invi('gj_calcerror', false);
    sbm_invi('gj_cam', true);
  } else {
    // We have valid airports so let's go
    // Update the messages
    sbm_invi('gj_calcerror', true);
    sbm_invi('gj_cam', false);
    // And fire our request at the server
    sbm_sndReq('gj_calculate_emissions.php', action);
  }
}

function gj_calculate_gift(control) {
  // We make an ajax call to the server so get the purchase button
  var action='1&gj_co2=' + control.form.gj_co2.value + '&io=' + control.form.indiv_organ.value;

  // First verify that we have a valid amount of co2
  if (control.form.gj_co2.value < 1) {
    sbm_invi('gj_calcerror', false);
    sbm_invi('gj_cam', true);
  } else {
    // We have valid airports so let's go
    // Update the messages
    sbm_invi('gj_calcerror', true);
    sbm_invi('gj_cam', false);
    // And fire our request at the server
    sbm_sndReq('gj_calculate_gift.php', action);
  }

  return false;
}
function gj_calculate_fuel(control) {
  // First verify that the user has specified at least 10 liters of one fuel
  if ((control.form.gj_fuel_1.value < 10) && 
      (control.form.gj_fuel_2.value < 10) &&
      (control.form.gj_fuel_3.value < 10) &&
      (control.form.gj_fuel_4.value < 10)) {
    sbm_invi('gj_calcerror', false);
    sbm_invi('gj_cam', true);
    return false;
  } 

  // Update the messages
  sbm_invi('gj_calcerror', true);
  sbm_invi('gj_cam', false);

  // We make an ajax call to the server so compose the GET-vars...
  var action = '1&io=' + control.form.indiv_organ.value;
  action += '&gj_fuel_1=' + control.form.gj_fuel_1.value; 
  action += '&gj_fuel_2=' + control.form.gj_fuel_2.value; 
  action += '&gj_fuel_3=' + control.form.gj_fuel_3.value; 
  action += '&gj_fuel_4=' + control.form.gj_fuel_4.value; 

  // And fire our request at the server
  sbm_sndReq('gj_calculate_fuel.php', action);

  return false;
}
function gj_calculate_home(control) {

  // Update the messages
  sbm_invi('gj_cam', false);

  // We make an ajax call to the server so compose the GET-vars...
  var action = '1&io=' + control.form.indiv_organ.value;
  action += '&gj_home_1=' + control.form.gj_home_1.value; 
  action += '&gj_home_1_tick=' + control.form.gj_home_1_tick.checked; 
  action += '&gj_home_2=' + control.form.gj_home_2.value; 
  action += '&gj_home_3=' + control.form.gj_home_3.value; 
  action += '&gj_home_4=' + control.form.gj_home_4.value; 

  // And fire our request at the server
  sbm_sndReq('gj_calculate_home.php', action);

  return false;
}

function get_radio_value(radio_control) {
  var rad_val = 1;
  for (var i=0; i < radio_control.length; i++) {
    if (radio_control[i].checked) {
      rad_val = radio_control[i].value;
      break;
    }
  }
  return rad_val;
}

function gj_calculate_cartravel(control) {
  // First we check that the fields are populated and valid
  if (control.form.gj_consumption.value == '' ||
      control.form.gj_consumption.value <= 0 ||
      control.form.gj_mileage.value == '' ||
      control.form.gj_mileage.value <= 0) {
    sbm_invi('gj_calcerror', false);
    sbm_invi('gj_calcerror_2', true);
    sbm_invi('gj_cam', true);
    return false;
  }

  if (control.form.gj_proportion_renewable.value < 0 ||
      control.form.gj_proportion_renewable.value > 100) {
    sbm_invi('gj_calcerror', true);
    sbm_invi('gj_calcerror_2', false);
    sbm_invi('gj_cam', true);
    return false;
  }
  
  // We make an ajax call to the server. We need
  var action = '1&gj_co=' + control.form.gj_consumption.value;
  action += '&io=' + control.form.indiv_organ.value;
  action += '&cu=' + get_radio_value(control.form.gj_cons_units);
  action += '&mi=' + control.form.gj_mileage.value;
  action += '&mu=' + get_radio_value(control.form.gj_mileage_unit);
  action += '&ft=' + get_radio_value(control.form.gj_fuel_type);
  action += '&pn=' + control.form.gj_proportion_renewable.value;

  // Update the messages
  sbm_invi('gj_calcerror', true);
  sbm_invi('gj_calcerror_2', true);
  sbm_invi('gj_cam', false);

  // And fire our request at the server
  sbm_sndReq('gj_calculate_cartravel.php', action);

  return false;
}

function calc_consumption(control) {
  // This is a utility function used by the cartravel form
  // to update the consumption field when the car type combo
  // is changed
  if (car_cons_stored) {
    for (var i=0; i < car_cons_stored.length; i++) {
      if (car_cons_stored[i][0] == control.value) {
        // This is the correnct entry
        control.form.gj_consumption.value = car_cons_stored[i][1];
        // And set the right radio to be selected. This is hardcoded to the second item
        // in the radio array (l/100km).
        for (var j=0; j<control.form.gj_cons_units.length; j++) {
          if (j==consumption_units_stored ) {
            control.form.gj_cons_units[j].checked = true;
          } else {
            control.form.gj_cons_units[j].checked = false;
          }
        }
        break;
      }
    }
  } else {
    alert('Car consumption array not stored in document!');
  }
}

function check_visible(control) {
  alert(control.style.display);
  if (control.style.display == 'none') {
    return false;
  }
  return true;
}

function popup(mylink, windowname) {
  if (! window.focus)return true;
  var href;
  if (typeof(mylink) == 'string')
     href=mylink;
  else
     href=mylink.href;

  var wina = 200;
  var winb = 100;

  window.open(href, windowname, 'top='+winb+',left='+wina+',width=400,height=200,scrollbars=yes');
  return false;
}

