function fuelCalc(){
	var thisform = document.forms.fuelinfo;
    if(thisform.fuel_type_current[0].checked == false && thisform.fuel_type_current[1].checked == false){
		window.alert("Please select the fuel type for your current vehicle!");
		thisform.fuel_type_current[0].focus();
	}else if(thisform.fuel_current.value == ""){
		window.alert("Please enter the price of "+thisform.fuel_word_current.value+" for your current vehicle!");
		thisform.fuel_current.focus();
	}else if(is_numeric(thisform.fuel_current.value) == false){
		window.alert("The current price of "+thisform.fuel_word_current.value+" for your current vehicle was not a number!");
		thisform.fuel_current.focus();
	}else if(thisform.mpg_current.value == ""){
		window.alert("Please enter the fuel consumption of your current vehicle!");
		thisform.mpg_current.focus();
	}else if(is_numeric(thisform.mpg_current.value) == false){
		window.alert("The fuel consumption of your current vehicle was not a number!");
		thisform.mpg_current.focus();
	}else if(thisform.fuel_type_new[0].checked == false && thisform.fuel_type_new[1].checked == false){
		window.alert("Please select the fuel type for the new vehicle!");
		thisform.fuel_type_new[0].focus();
	}else if(thisform.fuel_new.value == ""){
		window.alert("Please enter the price of "+thisform.fuel_word_new.value+" for the new vehicle!");
		thisform.fuel_new.focus();
	}else if(is_numeric(thisform.fuel_new.value) == false){
		window.alert("The price of "+thisform.fuel_word_new.value+" for the new vehicle was not a number!");
		thisform.fuel_new.focus();
	}else if(thisform.mpg_new.value == ""){
		window.alert("Please enter the fuel consumption of the new vehicle!");
		thisform.mpg_new.focus();
	}else if(is_numeric(thisform.mpg_new.value) == false){
		window.alert("The fuel consumption of the new vehicle was not a number!");
		thisform.mpg_new.focus();
    }else if(thisform.annual_mileage.value == ""){
		window.alert("Please enter your approximate annual mileage!");
		thisform.annual_mileage.focus();
	}else if(is_numeric(thisform.annual_mileage.value) == false){
		window.alert("Your approximate annual mileage was not a number!");
		thisform.annual_mileage.focus();
	}else{

		var litres_per_gallon = 4.54609188;
		var current_ppy = ((thisform.annual_mileage.value / thisform.mpg_current.value) * litres_per_gallon) * thisform.fuel_current.value;
		var new_ppy = ((thisform.annual_mileage.value / thisform.mpg_new.value) * litres_per_gallon) * thisform.fuel_new.value;
		var montly_saving = ((current_ppy - new_ppy) / 12) / 100;
		var yearly_saving = montly_saving*12;

		if(montly_saving < 0){
			document.getElementById("fuelcalc_saving").innerHTML = "<span id='fuelcalc_nosaving'>Sorry, according to our calculations<br />the vehicle you selected does not save you money.</span>";
		}else{
			var montly_saving = "&pound;"+montly_saving.toFixed(2);
			var yearly_saving = "&pound;"+yearly_saving.toFixed(2);

			document.getElementById("fuelcalc_saving").innerHTML = "Your new selected vehicle will save you:<br /><span id='fuelcalc_savefigure'>"+montly_saving+" per month! <span id='fuelcalc_smalland'>and that's</span> "+yearly_saving+" per year!</span>";
		}
	}
}

function is_numeric(string){
	var validchars = "0123456789.";
	var isnum = true;
	var thischar;

	for(i=0;i<string.length && isnum==true;i++){ 
		thischar = string.charAt(i); 
		if(validchars.indexOf(thischar) == -1){
			isnum = false;
		}
	}
   return isnum;
}

function setFuelType(vehicle){
	var thisform = document.forms.fuelinfo;
	if(thisform["fuel_type_"+vehicle][1].checked == true){
		var this_fuel = "diesel";
	}else{
		var this_fuel = "petrol";
	}
	thisform["fuel_word_"+vehicle].value = this_fuel;
	thisform["fuel_"+vehicle].value = thisform["cost_"+this_fuel].value;
}