var emissions	= new Array;
var emissions2	= new Array;
var cillinders	= new Array;
var cillinders2	= new Array;

var agesReqHandler;
xmlhttp (

	function(reqOb) {

		agesEntry	= getChildren (getChildren (reqOb.responseXML, 'ages')[0], 'age');
		carAges		= new Array;

		for (i in agesEntry) {

			min		= parseInt (getChildren(agesEntry[i], 'min')[0].firstChild.nodeValue);
			max		= parseInt (getChildren(agesEntry[i], 'max')[0].firstChild.nodeValue);
			reduction	= parseFloat (getChildren(agesEntry[i], 'reduction')[0].firstChild.nodeValue);
			carAges.push({
				'min'		: min,
				'max'		: max,
				'reduction'	: reduction
			});

		}

	},

	url = 'getAges.php',

	agesReqHandler

);

function computeValues() {

/*
A = emisia de CO2, exprimata în grame/km înscrisa de constructor în documentele de identificare, cu ocazia omologarii de tip
B = taxa specifica, exprimata în euro/1 gram CO2, prevazuta în coloana 3 din anexa nr.1
C = cilindree (capacitatea cilindrica) 
D = taxa specifica pe cilindree, prevazuta în coloana 3 din anexa nr. 2 
E = cota de reducere a taxei prevazuta în coloana 2 din anexa nr. 4
*/

	form	= document.forms['taxesForm'];
	A	= parseFloat(form.emission.value);
	B	= parseFloat(form.euroPerGrCO2.value);
	C	= parseFloat(form.cillinder.value);
	D	= parseFloat(form.euroPerCM3.value);
	E	= parseFloat(form.reduction.value);

	filter	= form.dieselFilter.options[form.dieselFilter.selectedIndex].value == 'yes';

	if (A + '' == 'NaN') A = 0;
	if (B + '' == 'NaN') B = 0;
	if (C + '' == 'NaN') C = 0;
	if (D + '' == 'NaN') D = 0;
	if (E + '' == 'NaN') E = 0;

	pollution = form.pollutionId.options[form.pollutionId.selectedIndex].text;
	switch (pollution) {

		case 'Euro3':
		case 'Euro4':
		case 'Euro5':
		case 'Euro6':
			form.euroValue.value	= ( ( A * B * 30 / 100) + (C * D * 70 / 100) ) * (100 - E) / 100;
			form.ronValue.value	= form.euroValue.value * parseFloat(form.eur.value);
			break;
		case 'Non-euro':
		case 'Euro1':
		case 'Euro2':
			form.euroValue.value	= C * D * (100 - E) / 100;
			form.ronValue.value	= form.euroValue.value * parseFloat(form.eur.value);
			break;

	}

	if (filter) {

		form.euroValue.value = parseFloat (form.euroValue.value) - parseFloat (form.euroValue.value) * 0.25;
		form.ronValue.value = parseFloat (form.ronValue.value) - parseFloat (form.ronValue.value) * 0.25;

	}

	syncForms();

}

function computeValues2() {

/*
A = emisia de CO2, exprimata în grame/km înscrisa de constructor în documentele de identificare, cu ocazia omologarii de tip
B = taxa specifica, exprimata în euro/1 gram CO2, prevazuta în coloana 3 din anexa nr.1
C = cilindree (capacitatea cilindrica) 
D = taxa specifica pe cilindree, prevazuta în coloana 3 din anexa nr. 2 
E = cota de reducere a taxei prevazuta în coloana 2 din anexa nr. 4
*/

	form	= document.forms['taxesForm2'];
	A	= parseFloat(form.emission.value);
	B	= parseFloat(form.euroPerGrCO2.value);
	C	= parseFloat(form.cillinder.value);
	D	= parseFloat(form.euroPerCM3.value);
	E	= parseFloat(form.reduction.value);

	filter	= form.dieselFilter.options[form.dieselFilter.selectedIndex].value == 'yes';

	if (A + '' == 'NaN') A = 0;
	if (B + '' == 'NaN') B = 0;
	if (C + '' == 'NaN') C = 0;
	if (D + '' == 'NaN') D = 0;
	if (E + '' == 'NaN') E = 0;

	pollution = form.pollutionId.options[form.pollutionId.selectedIndex].text;
	switch (pollution) {

		case 'Euro3':
		case 'Euro4':
		case 'Euro5':
		case 'Euro6':
			form.euroValue.value	= ( ( A * B * 30 / 100) + (C * D * 70 / 100) ) * (100 - E) / 100;
			form.ronValue.value	= form.euroValue.value * parseFloat(form.eur.value);
			break;
		case 'Non-euro':
		case 'Euro1':
		case 'Euro2':
			form.euroValue.value	= C * D * (100 - E) / 100;
			form.ronValue.value	= form.euroValue.value * parseFloat(form.eur.value);
			break;

	}

	if (filter) {

		form.euroValue.value = parseFloat (form.euroValue.value) - parseFloat (form.euroValue.value) * 0.25;
		form.ronValue.value = parseFloat (form.ronValue.value) - parseFloat (form.ronValue.value) * 0.25;

	}

	syncForms();

}

function changePollution(ob) {

	switch (ob.options[ob.selectedIndex].text) {

		case 'Non-euro':
		case 'Euro1':
		case 'Euro2':
			ob.form.emission.value		= '';
			ob.form.emission.disabled	= true;
			ob.form.emission.className	= 'readonly';

			ob.form.consume.value		= '';
			ob.form.consume.disabled	= true;
			ob.form.consume.className	= 'readonly';

			ob.form.motorisation.selectedIndex	= 0;
			ob.form.motorisation.disabled		= true;
			ob.form.motorisation.className		= 'readonly';

			break;
		default:
			ob.form.emission.disabled	= false;
			ob.form.emission.className	= '';

			ob.form.consume.disabled	= false;
			ob.form.consume.className	= '';

			ob.form.motorisation.disabled	= false;
			ob.form.motorisation.className	= '';

			break;

	}

	getEmissions(ob);
	getCillinders(ob);

}

function changePollution2(ob) {

	switch (ob.options[ob.selectedIndex].text) {

		case 'Non-euro':
		case 'Euro1':
		case 'Euro2':
			ob.form.emission.value		= '';
			ob.form.emission.disabled	= true;
			ob.form.emission.className	= 'readonly';

			ob.form.consume.value		= '';
			ob.form.consume.disabled	= true;
			ob.form.consume.className	= 'readonly';

			ob.form.motorisation.selectedIndex	= 0;
			ob.form.motorisation.disabled		= true;
			ob.form.motorisation.className		= 'readonly';

			break;
		default:
			ob.form.emission.disabled	= false;
			ob.form.emission.className	= '';

			ob.form.consume.disabled	= false;
			ob.form.consume.className	= '';

			ob.form.motorisation.disabled	= false;
			ob.form.motorisation.className	= '';

			break;

	}

	getEmissions2(ob);
	getCillinders2(ob);

}

function changeEmission(ob) {

	form = ob.form;
	form.euroPerGrCO2.value = '';

	if (ob.value.length) {

		ob.form.consume.disabled	= true;
		ob.form.consume.className	= 'readonly';
		ob.form.motorisation.disabled	= true;
		ob.form.motorisation.className	= 'readonly';

	} else {

		ob.form.consume.disabled	= false;
		ob.form.consume.className	= '';
		ob.form.motorisation.disabled	= false;
		ob.form.motorisation.className	= '';

	}

	if (
		ob.value != (emission = parseFloat(ob.value)) ||
		!emissions.length
	) return;

	for (i in emissions)
		if (
			(emission >= emissions[i]['min'] &&
			emission <= emissions[i]['max']) ||
			emission > emissions[i]['max']
		) form.euroPerGrCO2.value = emissions[i]['price'];

	computeValues();

}

function changeEmission2(ob) {

	form = ob.form;
	form.euroPerGrCO2.value = '';

	if (ob.value.length) {

		ob.form.consume.disabled	= true;
		ob.form.consume.className	= 'readonly';
		ob.form.motorisation.disabled	= true;
		ob.form.motorisation.className	= 'readonly';

	} else {

		ob.form.consume.disabled	= false;
		ob.form.consume.className	= '';
		ob.form.motorisation.disabled	= false;
		ob.form.motorisation.className	= '';

	}

	if (
		ob.value != (emission = parseFloat(ob.value)) ||
		!emissions2.length
	) return;

	for (i in emissions2)
		if (
			(emission >= emissions2[i]['min'] &&
			emission <= emissions2[i]['max']) ||
			emission > emissions2[i]['max']
		) form.euroPerGrCO2.value = emissions2[i]['price'];

	computeValues2();

}

function computeEmission() {

	form			= document.forms['taxesForm'];
	form.emission.value	= '';
	form.euroPerGrCO2.value	= '';

	if (!emissions.length) return;

	consume		= parseFloat(form.consume.value.length ? form.consume.value : 0);
	motorisation	= form.motorisation.options[form.motorisation.options.selectedIndex].text;

	emission = 0; switch (motorisation) {

		case 'Benzina':	emission = parseFloat (consume) * 23.8; break;
		case 'Diesel':	emission = parseFloat (consume) * 26.5; break;
		case 'GPL':	emission = parseFloat (consume) * 17.8; break;

	}
	form.emission.value = emission;

	for (i in emissions)
		if (
			(emission >= emissions[i]['min'] &&
			emission <= emissions[i]['max']) ||
			emission > emissions[i]['max']
		) form.euroPerGrCO2.value = emissions[i]['price'];

	computeValues();

}

function computeEmission2() {

	form			= document.forms['taxesForm2'];
	form.emission.value	= '';
	form.euroPerGrCO2.value	= '';

	if (!emissions2.length) return;

	consume		= parseFloat(form.consume.value.length ? form.consume.value : 0);
	motorisation	= form.motorisation.options[form.motorisation.options.selectedIndex].text;

	emission = 0; switch (motorisation) {

		case 'Benzina':	emission = parseFloat (consume) * 23.8; break;
		case 'Diesel':	emission = parseFloat (consume) * 26.5; break;
		case 'GPL':	emission = parseFloat (consume) * 17.8; break;

	}
	form.emission.value = emission;

	for (i in emissions2)
		if (
			(emission >= emissions2[i]['min'] &&
			emission <= emissions2[i]['max']) ||
			emission > emissions2[i]['max']
		) form.euroPerGrCO2.value = emissions2[i]['price'];

	computeValues2();

}

function changeConsume(ob) {

	computeEmission();

	if (
		ob.value.length ||
		ob.form.motorisation.selectedIndex > 0
	) {

		ob.form.emission.disabled	= true;
		ob.form.emission.className	= 'readonly';

	} else {

		ob.form.emission.disabled	= false;
		ob.form.emission.className	= '';


	}

	computeValues();

}

function changeConsume2(ob) {

	computeEmission2();

	if (
		ob.value.length ||
		ob.form.motorisation.selectedIndex > 0
	) {

		ob.form.emission.disabled	= true;
		ob.form.emission.className	= 'readonly';

	} else {

		ob.form.emission.disabled	= false;
		ob.form.emission.className	= '';


	}

	computeValues2();

}

function changeMotorisation(ob) {

	computeEmission();

	if (
		ob.selectedIndex > 0 ||
		ob.form.consume.value.length
	) {

		ob.form.emission.disabled	= true;
		ob.form.emission.className	= 'readonly';

	} else {

		ob.form.emission.disabled	= false;
		ob.form.emission.className	= '';

	}

	computeValues();

}

function changeMotorisation2(ob) {

	computeEmission2();

	if (
		ob.selectedIndex > 0 ||
		ob.form.consume.value.length
	) {

		ob.form.emission.disabled	= true;
		ob.form.emission.className	= 'readonly';

	} else {

		ob.form.emission.disabled	= false;
		ob.form.emission.className	= '';

	}

	computeValues2();

}

function changeAge() {

	form = document.forms['taxesForm'];
	form.reduction.value = '';

	if (parseInt (form.age.value) != (age = form.age.value)) return;

	unit	= form.ageUnit.options[form.ageUnit.selectedIndex].value;
	age	*= unit == 'year' ? 12 : 1;

	for (i in carAges)
		if (
			age > carAges[i]['min'] &&
			age <= carAges[i]['max']
		) form.reduction.value = carAges[i]['reduction'];

	computeValues();

}

function changeAge2() {

	form = document.forms['taxesForm2'];
	form.reduction.value = '';

	if (parseInt (form.age.value) != (age = form.age.value)) return;

	unit	= form.ageUnit.options[form.ageUnit.selectedIndex].value;
	age	*= unit == 'year' ? 12 : 1;

	for (i in carAges)
		if (
			age > carAges[i]['min'] &&
			age <= carAges[i]['max']
		) form.reduction.value = carAges[i]['reduction'];

	computeValues2();

}

function changeCillinder(ob) {

	ob.form.euroPerCM3.value = '';

	if (
		ob.value != (cillinder = parseFloat(ob.value)) ||
		!cillinders.length
	) return;

	for (i in cillinders)
		if (
			(cillinder >= cillinders[i]['min'] &&
			cillinder <= cillinders[i]['max']) ||
			cillinder > cillinders[i]['max']
		) ob.form.euroPerCM3.value = cillinders[i]['price'];

	computeValues();

}

function changeCillinder2(ob) {

	ob.form.euroPerCM3.value = '';

	if (
		ob.value != (cillinder = parseFloat(ob.value)) ||
		!cillinders2.length
	) return;

	for (i in cillinders2)
		if (
			(cillinder >= cillinders2[i]['min'] &&
			cillinder <= cillinders2[i]['max']) ||
			cillinder > cillinders2[i]['max']
		) ob.form.euroPerCM3.value = cillinders2[i]['price'];

	computeValues2();

}

function changeDieselFilter(ob) {

	computeValues();

}

function changeDieselFilter2(ob) {

	computeValues2();

}

function getEmissions(ob) {

	form = ob.form;
	if (ob.selectedIndex <= 0) return;

	pollutionId = ob.value;

	var emissionsReqHandler;
	xmlhttp (

		function(reqOb) {

			emissionsEntry	= getChildren (getChildren (reqOb.responseXML, 'emissions')[0], 'emission');
			emissions	= new Array;

			for (i in emissionsEntry) {

				min	= parseInt (getChildren(emissionsEntry[i], 'min')[0].firstChild.nodeValue);
				max	= parseInt (getChildren(emissionsEntry[i], 'max')[0].firstChild.nodeValue);
				price	= parseFloat (getChildren(emissionsEntry[i], 'price')[0].firstChild.nodeValue);
				emissions.push({
					'min'	: min,
					'max'	: max,
					'price'	: price
				});

			}

		},

		url = 'getEmissions.php?pollutionId=' + pollutionId,

		emissionsReqHandler

	);

	computeValues();

}

function getEmissions2(ob) {

	form = ob.form;
	if (ob.selectedIndex <= 0) return;

	pollutionId = ob.value;

	var emissionsReqHandler2;
	xmlhttp (

		function(reqOb) {

			emissionsEntry	= getChildren (getChildren (reqOb.responseXML, 'emissions')[0], 'emission');
			emissions2	= new Array;

			for (i in emissionsEntry) {

				min	= parseInt (getChildren(emissionsEntry[i], 'min')[0].firstChild.nodeValue);
				max	= parseInt (getChildren(emissionsEntry[i], 'max')[0].firstChild.nodeValue);
				price	= parseFloat (getChildren(emissionsEntry[i], 'price')[0].firstChild.nodeValue);
				emissions2.push({
					'min'	: min,
					'max'	: max,
					'price'	: price
				});

			}

		},

		url = 'getEmissions2.php?pollutionId=' + pollutionId,

		emissionsReqHandler2

	);

	computeValues2();

}

function getCillinders(ob) {

	form = ob.form;
	if (ob.selectedIndex <= 0) return;

	pollutionId = ob.value;

	var cillindersReqHandler;
	xmlhttp (

		function(reqOb) {

			cillindersEntry	= getChildren (getChildren (reqOb.responseXML, 'cillinders')[0], 'cillinder');
			cillinders	= new Array;

			for (i in cillindersEntry) {

				min	= parseInt (getChildren(cillindersEntry[i], 'min')[0].firstChild.nodeValue);
				max	= parseInt (getChildren(cillindersEntry[i], 'max')[0].firstChild.nodeValue);
				price	= parseFloat (getChildren(cillindersEntry[i], 'price')[0].firstChild.nodeValue);
				cillinders.push({
					'min'	: min,
					'max'	: max,
					'price'	: price
				});

			}

		},

		url = 'getCillinders.php?pollutionId=' + pollutionId,

		cillindersReqHandler

	);

	computeValues();

}

function getCillinders2(ob) {

	form = ob.form;
	if (ob.selectedIndex <= 0) return;

	pollutionId = ob.value;

	var cillindersReqHandler2;
	xmlhttp (

		function(reqOb) {

			cillindersEntry	= getChildren (getChildren (reqOb.responseXML, 'cillinders')[0], 'cillinder');
			cillinders2	= new Array;

			for (i in cillindersEntry) {

				min	= parseInt (getChildren(cillindersEntry[i], 'min')[0].firstChild.nodeValue);
				max	= parseInt (getChildren(cillindersEntry[i], 'max')[0].firstChild.nodeValue);
				price	= parseFloat (getChildren(cillindersEntry[i], 'price')[0].firstChild.nodeValue);
				cillinders2.push({
					'min'	: min,
					'max'	: max,
					'price'	: price
				});

			}

		},

		url = 'getCillinders2.php?pollutionId=' + pollutionId,

		cillindersReqHandler2

	);

	computeValues2();

}

function syncForms() {

	form1	= document.forms['taxesForm2'];
	form2	= document.forms['taxesForm'];
	/*form2	= document.forms['newSpecialTaxesForm'];*/

	form2.pollutionId.selectedIndex = -1;
	for (i = 0; i < form2.pollutionId.options.length; i++)
		if (form2.pollutionId.options[i].text == form1.pollutionId.options[form1.pollutionId.selectedIndex].text) {

			form2.pollutionId.selectedIndex = i;
			break;

		}

	form2.euroPerCM3.value	= form1.euroPerCM3.value;
	/*form2.cillinder.value	= form1.cillinder.value;
	form2.emission.value	= form1.emission.value;
	form2.euroPerGrCO2.value= form1.euroPerGrCO2.value;*/
	form2.age.value		= form1.age.value;

	form2.motorisation.selectedIndex = form1.motorisation.selectedIndex;
	form2.dieselFilter.selectedIndex = form1.dieselFilter.selectedIndex;

	age	= parseInt (form1.age.value);
	unit	= form1.ageUnit.options[form1.ageUnit.selectedIndex].value;
	age	*= unit == 'year' ? 12 : 1;

	form2.age.selectedIndex = -1;

	for (i in ages)
		if (
			age > ages[i]['min'] &&
			age <= ages[i]['max']
		) {

			for (j = 0; j < form2.age.options.length; j++)
				if (form2.age.options[j].value == i) {

					form2.age.selectedIndex = j;
					break;

				}

			break;

		}

	form2.reduction.value = form1.reduction.value;

	/*updateNewFields2();*/

	document.getElementById('eurDifference').innerHTML =
		parseFloat(form2.euroValue.value) -
		parseFloat(form1.euroValue.value);

	document.getElementById('ronDifference').innerHTML =
		parseFloat(form2.ronValue.value) -
		parseFloat(form1.ronValue.value);

}