function fixprice(pid, factor) {                // puts the calculated price into the perl
	var ovr_price = co2_emit*factor;
	
	// minimum price is £5, unless zero
	if ( (ovr_price < 5.00) && (ovr_price > 0.00) ) ovr_price = 5.00;
	
	document.getElementById('ovr_' + pid).value = ovr_price;
	
	return true;
}

function populate(city, country) {							// fill the city airport select
	var selected_country = country_list[document.getElementById(country).value];
	//need to clear the list
	var list = document.getElementById(city);
	for (i=list.length - 1; i>=0; i--) {
		 list.remove(i);
	}

	// first add the no choice made option
	var option = document.createElement('option');
	option.value = 0;
	option.appendChild(document.createTextNode(city_name[0]));
	document.forms['carbon'].elements[city].appendChild(option);
	
	// now add variable options
	for (var index=1; index<=city_count; index++) {
		var option = document.createElement('option');
		 if (selected_country == country_name[index]) {
			 option.value = index;
			 option.appendChild(document.createTextNode(city_name[index]));
			 document.forms['carbon'].elements[city].appendChild(option);
		 };
	};
	distance(); //refresh the distances
}

function distance()
{
	// get lat, long for two cities
	var lat_1=parseFloat(city_lat[document.getElementById("city1").value]);
	var lat_2=parseFloat(city_lat[document.getElementById("city2").value]);
	var lon_1=parseFloat(city_long[document.getElementById("city1").value]);
	var lon_2=parseFloat(city_long[document.getElementById("city2").value]);

	//get the value of 'way', whether it's return (2) or one way (1)
	var way_val = parseFloat(document.getElementById("way").value);
	
	//get the number of passengers, pax_val, from pax
	var pax_val = parseFloat(document.getElementById("pax").value);
	
	// Compute spherical coordinates
	var rho = 6378.1; // earth radius in km
	// convert latitude and longitude to spherical coordinates in radians; phi = 90 - latitude
	var phi_1 = (90.0 - lat_1)*Math.PI/180.0;
	var phi_2 = (90.0 - lat_2)*Math.PI/180.0;
	// theta = longitude
	var theta_1 = lon_1*Math.PI/180.0;
	var theta_2 = lon_2*Math.PI/180.0;
	
	// compute spherical distance from spherical coordinates
	var d = rho*Math.acos( Math.sin(phi_1)*Math.sin(phi_2)*Math.cos(theta_1 - theta_2) + Math.cos(phi_1)*Math.cos(phi_2) );
	
	// if d is not a number yet because the select lists haven't been completed
	if (isNaN(d)) d = 0.0;
	
	// if either city has zero lat and long we haven't defined it yet
	if (lat_1 == 0.000 && lon_1 == 0.000) d=0.0;
	if (lat_2 == 0.000 && lon_2 == 0.000) d=0.0;
	
	//return journey?
	d = d * way_val;
	
	//co2 is distance travelled * emissions factor (depends on long/short haul) * RFI * uplift factor * passenger no.
	var emissions_factor_long = 0.1106;
	var emissions_factor_short= 0.0983;
	var rfi = 1.9;
	var uplift_factor = 1.09;
	
	co2_emit = d*rfi*uplift_factor*pax_val; //co2_emit is a global variable
	
	if (d<2400.0) {  // three hour flight at 500 mph
		co2_emit = co2_emit*emissions_factor_short;
		} else {
		co2_emit = co2_emit*emissions_factor_long;
	}
	
	//calculate cost of CO2 equivalent
	co2_emit = co2_emit / 1000.0; //tonnes
	co2_cost_solar = co2_emit * co2_tonne_solar;
	co2_cost_biogas = co2_emit * co2_tonne_biogas;
	co2_cost_tree = co2_emit * co2_tonne_tree;
	
	// minimum price displayed is £5 unless zero, also fixed for PERL in fixprice()
	if ( (co2_cost_solar  < 5.00) && (co2_cost_solar  > 0.00) ) co2_cost_solar  = 5.00;
	if ( (co2_cost_biogas < 5.00) && (co2_cost_biogas > 0.00) ) co2_cost_biogas = 5.00;
	if ( (co2_cost_tree   < 5.00) && (co2_cost_tree   > 0.00) ) co2_cost_tree   = 5.00;
	
	// Display result in kilometers, CO2 in tonnes
	document.getElementById("distance_id").firstChild.nodeValue = d.toFixed(1);
	document.getElementById("co2_id1").firstChild.nodeValue = co2_emit.toFixed(2);
	document.getElementById("co2_cost_solar_id").firstChild.nodeValue = co2_cost_solar.toFixed(2);
	document.getElementById("co2_cost_biogas_id").firstChild.nodeValue = co2_cost_biogas.toFixed(2);
	document.getElementById("co2_cost_tree_id").firstChild.nodeValue = co2_cost_tree.toFixed(2);
	
	//for (var count=0; count<=country_count; count++) { // code to rewrite country list
	//	document.write('country_list[');
	//	document.write(count);
	//	document.write(']="');
	//	document.write(country_list[count]);
	//	document.write('";');
	//	document.write('<br />');
	//}
	
	return;
}

function car_distance()
{
	var fuel_type=document.getElementById("fuel").value;		//1 petrol; 2 diesel
	var engine=document.getElementById("engine_size").value;	//1 small; 2 medium; 3 large
	var d=parseFloat(document.getElementById("miles_travelled").value);		//defaults to 0
	
	d = d.toFixed(1);
	if (d < 0) d = -d; //can't be negative
	
	//multiplier is the factor to convert to tonnes of CO2
	var multiplier = 1.0;
	
	if (fuel_type==1 && engine==1) multiplier = 0.29;
	if (fuel_type==1 && engine==2) multiplier = 0.35;
	if (fuel_type==1 && engine==3) multiplier = 0.48;
	if (fuel_type==2 && engine==1) multiplier = 0.24;
	if (fuel_type==2 && engine==2) multiplier = 0.30;
	if (fuel_type==2 && engine==3) multiplier = 0.42;
	
	co2_emit = d*multiplier; //co2_emit is a global variable
		
	//calculate cost of CO2 equivalent
	co2_emit        = co2_emit / 1000.0; //tonnes
	co2_cost_solar  = co2_emit *  co2_tonne_solar;
	co2_cost_biogas = co2_emit *  co2_tonne_biogas;
	co2_cost_tree   = co2_emit *  co2_tonne_tree; 
	
	// minimum price displayed is £5 unless zero, also fixed for PERL in fixprice()
	if ( (co2_cost_solar  < 5.00) && (co2_cost_solar  > 0.00) ) co2_cost_solar  = 5.00;
	if ( (co2_cost_biogas < 5.00) && (co2_cost_biogas > 0.00) ) co2_cost_biogas = 5.00;
	if ( (co2_cost_tree   < 5.00) && (co2_cost_tree   > 0.00) ) co2_cost_tree   = 5.00;
									 
	// CO2 in tonnes
	document.getElementById("co2_id2").firstChild.nodeValue = co2_emit.toFixed(2);
	document.getElementById("co2_cost_solar_id2").firstChild.nodeValue = co2_cost_solar.toFixed(2);
	document.getElementById("co2_cost_biogas_id2").firstChild.nodeValue = co2_cost_biogas.toFixed(2);
	document.getElementById("co2_cost_tree_id2").firstChild.nodeValue = co2_cost_tree.toFixed(2);
	
	return;
}
