//  Filename: calc_car.js  //  Function: function to calculate car benefit  //  Author: Barry Hood -- design@delinear.co.uk  //  Date: 2005.05.06
function set_tax_format(form_name){
	var registration_date=document.getElementById(form_name).registration_date.value;
	if(registration_date=="before"){
		document.getElementById('show_emission').style.display="none";
		document.getElementById('show_capacity').style.display="block";
		}else{
		document.getElementById('show_emission').style.display="block";
		document.getElementById('show_capacity').style.display="none";
	}
}
function reset_showhide_elements(){
	document.getElementById('show_emission').style.display="block";
	document.getElementById('show_capacity').style.display="none";
	return true;
}
function car_calculator(form_name){
	var list_price=strip_common_elements(document.getElementById(form_name).list_price.value);
	var capital_contribution=strip_common_elements(document.getElementById(form_name).capital_contribution.value);
	if(!capital_contribution){
		capital_contribution=0;
	}
	var emission_figure=strip_common_elements(document.getElementById(form_name).emission_figure.value);
	var employee_contributions=strip_common_elements(document.getElementById(form_name).employee_contributions.value);
	if(!employee_contributions){
		employee_contributions=0;
	}
	var registration_date=document.getElementById(form_name).registration_date.value;
	var engine_capacity=document.getElementById(form_name).engine_capacity.value;
	var employer_fuel_payments=document.getElementById(form_name).employer_fuel_payments.value;
	var fuel_type=document.getElementById(form_name).fuel_type.value;
	var list_percentage=0;var car_benefit_amount=0;
	var fuel_benefit_amount=0;
	var total_benefit_amount=0;
	var taxable_price=0;
	var working_percentage=0;
	var error_array=new Array;
	var error_count=0;
	if(!check_number_valid(list_price)){
		Error_array[error_count]="\tThe list price";
		error_count++;
	}
	if(registration_date!="before"&&emission_figure&&!check_number_valid(emission_figure)){
		error_array[error_count]="\tThe emission figure";
		error_count++;
	}
	if(capital_contribution&&!check_number_valid(capital_contribution)){
		error_array[error_count]="\tThe capital contribution";
		error_count++;
	}
	if(employee_contributions&&!check_number_valid(employee_contributions)){
		error_array[error_count]="\tThe employee contributions";
		error_count++;
	}
	if(error_array.length>0){
		var error_message;
		error_message=(error_array.length>1)?"The following fields are incorrect. Please check the fields and try again:\n\n":"The following field is incorrect. Please check the fields and try again:\n\n";
		for(i=0;i<error_array.length;i++){
			error_message+=error_array[i];
			error_message+="\n";
		}
		document.getElementById(form_name).list_percentage.value='';
		document.getElementById(form_name).car_benefit_amount.value='';
		document.getElementById(form_name).fuel_benefit_amount.value='';
		document.getElementById(form_name).total_benefit_amount.value='';
		alert(error_message);
		return false;
	}
	if (parseFloat(capital_contribution)>parseFloat(list_price)){
		alert("The employee capital contribution cannot be more than the list price of the vehicle. Please amend this and try again.");
		return false;
	}
	taxable_price=(parseFloat(capital_contribution)>5000?parseFloat(list_price)-5000:parseFloat(list_price)-parseFloat(capital_contribution));
	taxable_price=(taxable_price>80000?80000:taxable_price);
	if(registration_date=="before"){
		if(engine_capacity=="1400"){
			working_percentage=15;
		}
	  else if(engine_capacity=="2000"){
		  working_percentage=22;
	  }
		else if(engine_capacity=="2001"){
			working_percentage=32;
		}
		else {
			alert("Please select an engine capacity");
			return false;
		}
	}
	else{
			if(fuel_type!="electric"&&!check_number_valid(emission_figure)){
				alert("You must select an emission figure for vehicles registered after 31st December 1997");
				return false;
			}else
			{
				emission_figure=parseFloat(emission_figure);
				emission_figure=Math.floor(emission_figure/5)*5;
				
				if(emission_figure<140)
				{
					emission_figure=140;
				}
			
				if(emission_figure>240)
				{
					emission_figure=240;
				}
				working_percentage=Math.floor(emission_figure/5)-13;
		}
	}
	if(fuel_type=="electric")
	{
		working_percentage=(registration_date=="before"?15:9);
	}
	else if(fuel_type=="diesel"){
		if(registration_date=="after"){
			if(emission_figure<235){
				working_percentage+=3;
			}
			else if(emission_figure<240){
				working_percentage+=2;
			}
			else if(emission_figure<245){
				working_percentage+=1;
			}
		}
	}
	var working_fraction=(working_percentage/100);
	Employee_contributions=(employee_contributions>(working_fraction*taxable_price)?working_fraction*taxable_price:employee_contributions);
	car_benefit_amount=(working_fraction*taxable_price)-employee_contributions;
	fuel_benefit_amount=(employer_fuel_payments=="yes"?(14400*working_fraction):0);
	total_benefit_amount=(car_benefit_amount+fuel_benefit_amount);
	document.getElementById(form_name).list_percentage.value=format_number_output(working_percentage,'0',',')+'%';
	document.getElementById(form_name).car_benefit_amount.value=format_number_output(car_benefit_amount,2,',','£');
	document.getElementById(form_name).fuel_benefit_amount.value=format_number_output(fuel_benefit_amount,2,',','£');
	document.getElementById(form_name).total_benefit_amount.value=format_number_output(total_benefit_amount,2,',','£');
}